diff --git a/.gitattributes b/.gitattributes index 485b7c41a7cb9bde0e5229ee7c12d8a333bc703e..3b75860ace5ae51f657f9cc36fe4d2a66f71d6bd 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1244,3 +1244,7 @@ western_ai/usa/stable-diffusion-webui/modules/Roboto-Regular.ttf filter=lfs diff western_ai/usa/stable-diffusion-webui/modules/textual_inversion/test_embedding.png filter=lfs diff=lfs merge=lfs -text western_ai/usa/stable-diffusion-webui/screenshot.png filter=lfs diff=lfs merge=lfs -text western_ai/usa/tensorflow/tensorflow/c/testdata/tf_record filter=lfs diff=lfs merge=lfs -text +western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/chunked_saved_model/chunked_model/saved_model.cpb filter=lfs diff=lfs merge=lfs -text +western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/bad_node_attr/variables/variables.data-00000-of-00001 filter=lfs diff=lfs merge=lfs -text +western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/const_with_no_value/variables/variables.data-00000-of-00001 filter=lfs diff=lfs merge=lfs -text +western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/negative_shape/variables/variables.data-00000-of-00001 filter=lfs diff=lfs merge=lfs -text diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/BUILD b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..67f5ad10a97311ae4bac373a947691f40e4f773a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/BUILD @@ -0,0 +1,682 @@ +#Description: +# TensorFlow SavedModel. + +load("@xla//third_party/rules_python/python:py_binary.bzl", "py_binary") +load( + "//tensorflow:tensorflow.bzl", + "if_android", + "if_google", + "if_mobile", + "if_not_mobile", + "if_not_windows_or_mac", + "tf_cc_test", +) +load("//tensorflow:tensorflow.default.bzl", "filegroup") +load( + "//tensorflow/core/platform:build_config_root.bzl", + "if_static", +) +load("//tensorflow/core/platform:rules_cc.bzl", "cc_library") +load( + "//tensorflow/security/fuzzing:tf_fuzzing.bzl", + "tf_cc_fuzz_test", +) + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:license"], + default_visibility = ["//visibility:public"], + licenses = ["notice"], +) + +exports_files([ + "loader.h", + "testdata/chunked_saved_model/chunked_model/saved_model.cpb", + "testdata/chunked_saved_model/chunked_model/saved_model.pbtxt", +]) + +cc_library( + name = "constants", + hdrs = ["constants.h"], + deps = ["@com_google_absl//absl/strings"], +) + +cc_library( + name = "signature_constants", + hdrs = ["signature_constants.h"], +) + +cc_library( + name = "tag_constants", + hdrs = ["tag_constants.h"], +) + +# copybara:uncomment_begin(google-only) +# cc_library( +# name = "mobile_only_deps", +# visibility = ["//visibility:private"], +# deps = if_mobile(["//tensorflow/core:portable_tensorflow_lib"]), +# ) +# copybara:uncomment_end + +cc_library( + name = "reader", + srcs = ["reader.cc"], + hdrs = ["reader.h"], + deps = [ + ":constants", + ":metrics", + ":util", + "//tensorflow/core:protos_all_cc", + "@com_google_absl//absl/status:statusor", + ] + if_google([ + "//tensorflow/tools/proto_splitter:merge", + ]) + if_not_mobile([ + # TODO(b/111634734): :lib and :protos_all contain dependencies that + # cannot be built on mobile platforms. Instead, include the appropriate + # tf_lib depending on the build platform. + "@com_google_absl//absl/memory:memory", + "//tensorflow/core:lib", + "//tensorflow/core/util/tensor_bundle:byteswaptensor", + ]), +) + +tf_cc_test( + name = "reader_test", + srcs = ["reader_test.cc"], + data = [ + ":saved_model_test_files", + ], + linkstatic = 1, + deps = [ + ":constants", + ":metrics", + ":reader", + ":tag_constants", + "//tensorflow/core:lib", + "//tensorflow/core:test", + "//tensorflow/core/platform:resource_loader", + "@com_google_googletest//:gtest_main", + ], +) + +cc_library( + name = "loader_default_deps", + visibility = ["//visibility:private"], + deps = if_static([ + "//tensorflow/core:all_kernels", + "//tensorflow/core:direct_session", + ]) + if_google(if_static(["//tensorflow/core/platform/default/build_config:tensorflow_platform_specific"])), +) + +cc_library( + name = "loader", + hdrs = ["loader.h"], + deps = [ + ":loader_lite", + ] + select({ + "//tensorflow:android": [], + "//tensorflow:ios": [], + "//tensorflow:tflite_converter": ["//tensorflow/core:direct_session"] + if_google([ + "//tensorflow/core/platform/default/build_config:tensorflow_platform_specific", + ]), + "//conditions:default": [":loader_default_deps"], + }) + if_not_mobile([ + "//tensorflow/core:core_cpu", + "//tensorflow/core:lib", + "//tensorflow/core:ops", + "//tensorflow/core:protos_all_cc", + ]) + if_android([ + "//tensorflow/core:portable_tensorflow_lib", + ]), +) + +cc_library( + name = "loader_lite", + hdrs = ["loader.h"], + deps = if_static([ + ":loader_lite_impl", + ]) + if_not_mobile([ + "//tensorflow/core:core_cpu", + "//tensorflow/core:lib", + "//tensorflow/core:protos_all_cc", + ]), +) + +cc_library( + name = "loader_lite_impl", + srcs = ["loader.cc"], + hdrs = ["loader.h"], + deps = [ + ":constants", + ":fingerprinting", + ":loader_util", + ":reader", + "@com_google_absl//absl/status", + "@com_google_absl//absl/strings", + ] + if_not_mobile([ + ":metrics", + ":util", + "//tensorflow/core:core_cpu", + "//tensorflow/core:framework", + "//tensorflow/core:lib", + "//tensorflow/core:lib_internal", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core/util/tensor_bundle:naming", + ]), + alwayslink = 1, +) + +cc_library( + name = "bundle_v2", + srcs = ["bundle_v2.cc"], + hdrs = ["bundle_v2.h"], + deps = [ + ":constants", + ":fingerprinting", + ":metrics", + ":reader", + ":util", + "//tensorflow/core:lib", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core/platform:errors", + "//tensorflow/core/platform:strcat", + "//tensorflow/core/platform:tstring", + "//tensorflow/core/util/tensor_bundle", + "//tensorflow/core/util/tensor_bundle:byteswaptensor", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/log", + "@com_google_absl//absl/status", + "@jsoncpp_git//:jsoncpp", + "@tsl//tsl/platform:errors", + "@tsl//tsl/platform:statusor", + "@tsl//tsl/platform:strcat", + ], +) + +cc_library( + name = "loader_util", + srcs = ["loader_util.cc"], + hdrs = ["loader_util.h"], + deps = [":constants"] + if_not_mobile([ + "//tensorflow/core:lib", + "//tensorflow/core:lib_internal", + "//tensorflow/core:protos_all_cc", + ]), +) + +tf_cc_test( + name = "bundle_v2_test", + srcs = ["bundle_v2_test.cc"], + data = [ + ":saved_model_test_files", + ], + linkstatic = 1, + deps = [ + ":bundle_v2", + ":metrics", + "//tensorflow/core:lib", + "//tensorflow/core:test", + "//tensorflow/core:test_main", + "//tensorflow/core/platform:test", + "//tensorflow/core/protobuf:for_core_protos_cc", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@jsoncpp_git//:jsoncpp", + "@tsl//tsl/platform:statusor", + ], +) + +tf_cc_test( + name = "saved_model_bundle_test", + srcs = ["saved_model_bundle_test.cc"], + data = [ + ":saved_model_test_files", + ], + linkstatic = 1, + deps = [ + ":constants", + ":loader", + ":metrics", + ":reader", + ":signature_constants", + ":tag_constants", + "//tensorflow/core:lib", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core:tensorflow", + "//tensorflow/core:test", + "//tensorflow/core:test_main", + "//tensorflow/core:testlib", + ], +) + +tf_cc_test( + name = "saved_model_bundle_lite_test", + srcs = ["saved_model_bundle_lite_test.cc"], + data = [ + ":saved_model_test_files", + ], + linkstatic = 1, + deps = [ + ":constants", + ":loader", + ":signature_constants", + ":tag_constants", + "//tensorflow/core:lib", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core:tensorflow", + "//tensorflow/core:test", + "//tensorflow/core:test_main", + "//tensorflow/core:testlib", + "@com_google_absl//absl/status", + ], +) + +# A subset of the TF2 saved models can be generated with this tool. +py_binary( + name = "testdata/generate_saved_models", + srcs = ["testdata/generate_saved_models.py"], + data = [ + ":saved_model_asset_data", + ":saved_model_static_hashtable_asset_data", + ], + strict_deps = True, + deps = [ + "//tensorflow/python/client:session", + "//tensorflow/python/compat:v2_compat", + "//tensorflow/python/eager:def_function", + "//tensorflow/python/framework:dtypes", + "//tensorflow/python/framework:ops", + "//tensorflow/python/framework:tensor_spec", + "//tensorflow/python/module", + "//tensorflow/python/ops:io_ops", + "//tensorflow/python/ops:lookup_ops", + "//tensorflow/python/ops:variables", + "//tensorflow/python/platform:client_testlib", + "//tensorflow/python/saved_model", + "//tensorflow/python/saved_model:save_options", + "//tensorflow/python/trackable:asset", + "@absl_py//absl:app", + ], +) + +# copybara:uncomment_begin(google-only) +# +# py_binary( +# name = "testdata/generate_chunked_models", +# srcs = ["testdata/generate_chunked_models.py"], +# strict_deps = True, +# deps = [ +# "//tensorflow/python/compat:v2_compat", +# "//tensorflow/python/eager:def_function", +# "//tensorflow/python/framework:constant_op", +# "//tensorflow/python/lib/io:file_io", +# "//tensorflow/python/module", +# "//tensorflow/python/saved_model:loader", +# "//tensorflow/python/saved_model:save", +# "//tensorflow/python/saved_model:save_options", +# "//tensorflow/python/util:compat", +# "//tensorflow/tools/proto_splitter:constants", +# "//tensorflow/tools/proto_splitter/python:saved_model", +# "//third_party/py/numpy", +# "@absl_py//absl:app", +# "@absl_py//absl/flags", +# ], +# ) +# +# copybara:uncomment_end + +# TODO(b/32673259): add a test to continuously validate these files. +filegroup( + name = "saved_model_test_files", + srcs = glob([ + "testdata/AssetModule/**", + "testdata/half_plus_two_pbtxt/**", + "testdata/half_plus_two_main_op/**", + "testdata/half_plus_two/**", + "testdata/half_plus_two_v2/**", + "testdata/x_plus_y_v2_debuginfo/**", + "testdata/CyclicModule/**", + "testdata/StaticHashTableModule/**", + "testdata/VarsAndArithmeticObjectGraph/**", + "testdata/fuzz_generated/**", + "testdata/SimpleV1Model/**", + "testdata/OptimizerSlotVariableModule/**", + "testdata/chunked_saved_model/**", + ]), +) + +filegroup( + name = "saved_model_fingerprinting_test_files", + srcs = glob([ + "testdata/bert2/**", + "testdata/bert1/**", + "testdata/half_plus_two_pbtxt/**", + ]), +) + +alias( + name = "saved_model_half_plus_two", + actual = ":saved_model_test_files", +) + +filegroup( + name = "saved_model_asset_data", + srcs = [ + "testdata/test_asset.txt", + ], +) + +filegroup( + name = "saved_model_static_hashtable_asset_data", + srcs = [ + "testdata/static_hashtable_asset.txt", + ], +) + +exports_files( + glob([ + "testdata/half_plus_two_pbtxt/**", + "testdata/half_plus_two_main_op/**", + "testdata/half_plus_two/**", + "testdata/half_plus_two_v2/**", + "testdata/x_plus_y_v2_debuginfo/**", + "testdata/CyclicModule/**", + "testdata/VarsAndArithmeticObjectGraph/**", + "testdata/fuzz_generated/**", + ]), +) + +# Linked directly into ":tensorflow_framework". +cc_library( + name = "metrics_impl", + srcs = [ + "metrics.cc", + "metrics.h", + ], + visibility = [ + "//tensorflow:__pkg__", + "//tensorflow/python:__pkg__", + ], + deps = [ + "//tensorflow/core:protos_all_cc", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@jsoncpp_git//:jsoncpp", + ] + if_not_mobile([ + "//tensorflow/core:lib", + ]) + if_android([ + "//tensorflow/core:portable_tensorflow_lib_lite", + ]), + alwayslink = True, +) + +cc_library( + name = "metrics", + hdrs = ["metrics.h"], + visibility = [ + "//tensorflow/cc/saved_model/image_format:__subpackages__", + "//tensorflow/python/saved_model:__subpackages__", + ], + deps = if_static([ + ":metrics_impl", + ]) + if_not_mobile([ + "//tensorflow/core:lib", + ]) + if_android([ + "//tensorflow/core:portable_tensorflow_lib_lite", + ]) + [ + "//tensorflow/core/protobuf:for_core_protos_cc", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + ], +) + +tf_cc_test( + name = "metrics_test", + size = "small", + srcs = ["metrics_test.cc"], + deps = [ + ":metrics", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core:test", + "//tensorflow/core:test_main", + "//tensorflow/core/platform:status_matchers", + "@com_google_absl//absl/status", + "@com_google_googletest//:gtest", + "@jsoncpp_git//:jsoncpp", + "@tsl//tsl/platform:statusor", + ], +) + +cc_library( + name = "util", + srcs = ["util.cc"], + hdrs = ["util.h"], + deps = [ + "//tensorflow/core:framework", + "//tensorflow/core/framework:tensor_proto_cc", + "//tensorflow/core/platform:errors", + "//tensorflow/core/platform:status", + "//tensorflow/core/protobuf:for_core_protos_cc", + ] + if_not_mobile(["//tensorflow/core:lib"]) + if_android(["//tensorflow/core:portable_tensorflow_lib_lite"]), +) + +cc_library( + name = "test_utils", + testonly = True, + hdrs = ["test_utils.h"], + visibility = ["//visibility:public"], + deps = [ + "//tensorflow/core:test", + "//tensorflow/core/platform:protobuf", + ], +) + +tf_cc_test( + name = "util_test", + size = "small", + srcs = ["util_test.cc"], + deps = [ + ":test_utils", + ":util", + "//tensorflow/core:lib", + "//tensorflow/core:test", + "//tensorflow/core:test_main", + "//tensorflow/core/framework:tensor_shape_proto_cc", + "//tensorflow/core/framework:tensor_testutil", + "//tensorflow/core/protobuf:for_core_protos_cc", + "@tsl//tsl/platform:status_matchers", + ], +) + +# Linked directly into ":tensorflow_framework". +cc_library( + name = "fingerprinting_impl", + srcs = [ + "fingerprinting.cc", + "fingerprinting.h", + ], + visibility = [ + "//tensorflow:__pkg__", + "//tensorflow/python:__pkg__", + ], + deps = [ + ":constants", + ":fingerprinting_x_platform_utils", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core/graph/regularization:simple_delete", + "//tensorflow/core/graph/regularization:util", + "//tensorflow/core/util/tensor_bundle:naming", + "@com_google_absl//absl/container:btree", + "@com_google_absl//absl/log", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/strings:str_format", + "@tsl//tsl/platform:protobuf", + "@tsl//tsl/platform:types", + ] + if_not_mobile([ + "//tensorflow/core:lib", + ]) + if_android([ + "//tensorflow/core:portable_tensorflow_lib_lite", + ]) + if_not_windows_or_mac([ + ":fingerprinting_utils", + "//tensorflow/tools/proto_splitter/cc:util", + ]), + alwayslink = True, +) + +cc_library( + name = "fingerprinting", + hdrs = ["fingerprinting.h"], + visibility = [ + "//learning/brain/contrib/hub/server/ingestion:__subpackages__", + "//learning/brain/contrib/tpu_modeling:__subpackages__", + "//learning/gemini/deployment/disaggregation/wiz_cc:__subpackages__", + "//learning/metadata/artifactoid/cc:__subpackages__", + "//learning/tfx/pipeline/util:__subpackages__", + "//tensorflow/core/tfrt:__subpackages__", + "//tensorflow/python/saved_model:__subpackages__", + ], + deps = if_static([ + ":fingerprinting_impl", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/status:statusor", + "//tensorflow/core:protos_all_cc", + ]) + if_not_mobile([ + "//tensorflow/core:lib", + ]) + if_android([ + "//tensorflow/core:portable_tensorflow_lib_lite", + ]), +) + +cc_library( + name = "fingerprinting_utils", + srcs = ["fingerprinting_utils.cc"], + hdrs = ["fingerprinting_utils.h"], + visibility = ["//visibility:private"], + deps = [ + ":constants", + ":fingerprinting_x_platform_utils", + "//tensorflow/core:lib", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core/util/tensor_bundle:naming", + "//tensorflow/tools/proto_splitter:chunk_proto_cc", + "//tensorflow/tools/proto_splitter:merge", + "//tensorflow/tools/proto_splitter/cc:util", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/strings:str_format", + "@riegeli//riegeli/bytes:fd_reader", + "@riegeli//riegeli/records:record_reader", + ], + alwayslink = True, +) + +cc_library( + name = "fingerprinting_x_platform_utils", + srcs = ["fingerprinting_x_platform_utils.cc"], + hdrs = ["fingerprinting_x_platform_utils.h"], + deps = [ + "@com_google_absl//absl/numeric:int128", + "@com_google_absl//absl/strings:str_format", + "@tsl//tsl/platform:random", + ], +) + +tf_cc_test( + name = "fingerprinting_utils_test", + srcs = ["fingerprinting_utils_test.cc"], + data = [ + "//tensorflow/tools/proto_splitter/testdata:many-field.cpb", + "//tensorflow/tools/proto_splitter/testdata:split-standard.cpb", + ], + tags = [ + "no_mac", # b/291933687 + "no_windows", # b/291001524 + ], + deps = [ + ":fingerprinting_utils", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core/platform:path", + "//tensorflow/core/platform:protobuf", + "//tensorflow/tools/proto_splitter:chunk_proto_cc", + "//tensorflow/tools/proto_splitter/cc:util", + "//tensorflow/tools/proto_splitter/testdata:test_message_proto_cc", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_google_googletest//:gtest_main", + "@tsl//tsl/platform:errors", + "@tsl//tsl/platform:protobuf", + "@tsl//tsl/platform:status_matchers", + "@tsl//tsl/platform:statusor", + "@tsl//tsl/platform:test", + "@xla//xla/tsl/lib/core:status_test_util", + ], +) + +tf_cc_test( + name = "fingerprinting_chunked_test", + size = "small", + srcs = ["fingerprinting_chunked_test.cc"], + data = [ + ":saved_model_fingerprinting_test_files", + ":saved_model_test_files", + ], + tags = [ + "no_mac", # b/291933687 + "no_windows", # b/291001524 + ], + deps = [ + ":fingerprinting", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core:test", + "//tensorflow/core/platform:path", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_googletest//:gtest_main", + "@tsl//tsl/platform:statusor", + ], +) + +# copybara:uncomment_end + +tf_cc_test( + name = "fingerprinting_test", + size = "small", + srcs = ["fingerprinting_test.cc"], + data = [ + ":saved_model_fingerprinting_test_files", + ":saved_model_test_files", + ], + deps = [ + ":fingerprinting", + "//tensorflow/cc:cc_ops", + "//tensorflow/core:lib", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core:test", + "//tensorflow/core:test_main", + "//tensorflow/core/protobuf:for_core_protos_cc", + "@com_google_absl//absl/numeric:int128", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_google_googletest//:gtest", + ], +) + +tf_cc_fuzz_test( + name = "saved_model_fuzz", + srcs = ["saved_model_fuzz.cc"], + deps = [ + ":constants", + ":loader", + ":tag_constants", + "//tensorflow/core:core_cpu", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core:tensorflow", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/README.md b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/README.md new file mode 100644 index 0000000000000000000000000000000000000000..d979f5dde657be6f68421a6332127dc5783f8f00 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/README.md @@ -0,0 +1 @@ + diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/bundle_v2.cc b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/bundle_v2.cc new file mode 100644 index 0000000000000000000000000000000000000000..1052c5f751cff02e00bf5c8c6fdd185dca56b312 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/bundle_v2.cc @@ -0,0 +1,234 @@ +/* Copyright 2016 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/cc/saved_model/bundle_v2.h" + +#include +#include +#include + +#include "absl/container/flat_hash_set.h" +#include "absl/log/log.h" +#include "absl/status/status.h" +#include "tensorflow/cc/saved_model/constants.h" +#include "tensorflow/cc/saved_model/fingerprinting.h" +#include "tensorflow/cc/saved_model/metrics.h" +#include "tensorflow/cc/saved_model/reader.h" +#include "tensorflow/core/framework/types.pb.h" +#include "tensorflow/core/platform/byte_order.h" +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/platform/errors.h" +#include "tensorflow/core/platform/path.h" +#include "tensorflow/core/platform/strcat.h" +#include "tensorflow/core/platform/tstring.h" +#include "tensorflow/core/protobuf/saved_model.pb.h" +#include "tensorflow/core/protobuf/saved_object_graph.pb.h" +#include "tensorflow/core/protobuf/trackable_object_graph.pb.h" +#include "tensorflow/core/util/tensor_bundle/byte_swap_tensor.h" +#include "tensorflow/core/util/tensor_bundle/tensor_bundle.h" +#include "tsl/platform/errors.h" +#include "tsl/platform/statusor.h" +#include "tsl/platform/strcat.h" + +namespace tensorflow { +namespace { + +using strings::StrCat; + +// `tensorflow::SavedModelV2Bundle::Load` API label. +constexpr char kCCLoadBundleV2Label[] = "cc_load_bundle_v2"; + +absl::Status ReadCheckpointObjectGraph(BundleReader* bundle_reader, + TrackableObjectGraph* object_graph) { + Tensor object_graph_tensor; + TF_RETURN_WITH_CONTEXT_IF_ERROR( + bundle_reader->Lookup(kObjectGraphProtoKey, &object_graph_tensor), + "SavedModel checkpoint does not contain object graph."); + if (object_graph_tensor.dtype() != DT_STRING || + object_graph_tensor.dims() != 0 || + object_graph_tensor.NumElements() != 1) { + return absl::Status( + absl::StatusCode::kFailedPrecondition, + "SavedModel checkpoint object graph was not the correct type."); + } + + if (!object_graph->ParseFromString(object_graph_tensor.scalar()())) { + return absl::Status( + absl::StatusCode::kFailedPrecondition, + "SavedModel checkpoint object graph could not be deserialized."); + } + return absl::OkStatus(); +} + +} // namespace + +absl::Status SavedModelV2Bundle::Load(const std::string& export_dir, + SavedModelV2Bundle* const bundle) { + metrics::SavedModelReadApi(kCCLoadBundleV2Label).IncrementBy(1); + SavedModel saved_model_proto; + TF_RETURN_IF_ERROR(ReadSavedModel(export_dir, &saved_model_proto)); + metrics::SavedModelReadPath().Set(export_dir); + + // Load MetaGraphDef. + // In version 2 SavedModels, there is only one MetaGraphDef. + if (saved_model_proto.meta_graphs_size() != 1) { + return absl::Status( + absl::StatusCode::kInvalidArgument, + strings::StrCat( + "SavedModelV2 should have exactly one MetaGraphDef but actually ", + "contains ", saved_model_proto.meta_graphs_size())); + } + bundle->meta_graph_def_ = + std::move(*saved_model_proto.mutable_meta_graphs(0)); + + // Correct the endiness of Tensor content on big-endian system + if (!port::kLittleEndian) { + TF_RETURN_IF_ERROR( + ByteSwapTensorContentInMetaGraphDef(&(bundle->meta_graph_def_))); + } + + // Load GraphDebugInfo. + TF_RETURN_IF_ERROR( + ReadSavedModelDebugInfoIfPresent(export_dir, &bundle->debug_info_)); + + const std::string variables_dir = + io::JoinPath(export_dir, kSavedModelVariablesDirectory); + if (!Env::Default()->FileExists(variables_dir).ok()) { + LOG(INFO) + << "No checkpoint found, assuming this is a program-only SavedModel"; + } else { + // Load the variables checkpoint reader. + const std::string variables_prefix = + io::JoinPath(variables_dir, kSavedModelVariablesFilename); + bundle->variable_reader_ = + std::make_unique(Env::Default(), variables_prefix); + TF_RETURN_WITH_CONTEXT_IF_ERROR( + bundle->variable_reader_->status(), + "Unable to load SavedModel variables checkpoint from ", + variables_prefix); + + // Deserialize the object graph proto from the tensor bundle. + TF_RETURN_IF_ERROR(ReadCheckpointObjectGraph( + bundle->variable_reader_.get(), &bundle->trackable_object_graph_)); + } + // Read the fingerprint. + auto fingerprint_proto = + saved_model::fingerprinting::ReadSavedModelFingerprint(export_dir); + if (fingerprint_proto.ok()) { + metrics::SavedModelReadFingerprint().Set( + metrics::MakeFingerprintJson(fingerprint_proto.value())); + + TF_ASSIGN_OR_RETURN( + std::string path_and_singleprint, + metrics::MakeSavedModelPathAndSingleprint( + export_dir, saved_model::fingerprinting::Singleprint( + fingerprint_proto.value()))); + metrics::SavedModelReadPathAndSingleprint().Set(path_and_singleprint); + } + + return absl::OkStatus(); +} + +absl::Status SavedModelV2Bundle::VisitObjectsToRestore( + RestoreObjectsCallback callback) { + if (saved_object_graph().nodes_size() == 0 || + trackable_object_graph().nodes_size() == 0) { + return absl::OkStatus(); + } + + // Start from root nodes of both the SavedObjectGraph and TrackableObjectGraph + // and descend to leaves. Note that the TrackableObjectGraph can have cycles + // (as can the SavedObjectGraph). + // This is detected and cycle edges are skipped. + const SavedObject* root_saved_object = &saved_object_graph().nodes(0); + const TrackableObjectGraph::TrackableObject* root_trackable_object = + &trackable_object_graph().nodes(0); + absl::flat_hash_set trackable_node_ids; + return RecurseObjectsToRestore(root_saved_object, 0, root_trackable_object, + std::string(), &trackable_node_ids, + std::move(callback)); +} + +absl::Status SavedModelV2Bundle::RecurseObjectsToRestore( + const SavedObject* saved_object, int saved_object_node_id, + const TrackableObjectGraph::TrackableObject* trackable_object, + std::string object_name, absl::flat_hash_set* seen_trackable_node_ids, + RestoreObjectsCallback callback) { + // Callback if any attributes or slot variables. + // Note that the root is always excluded from the search (it can never + // be a restorable object). This matches some logic on the Python side. + if (saved_object_node_id != 0 && + (trackable_object->attributes_size() > 0 || + trackable_object->slot_variables_size() > 0)) { + TF_RETURN_WITH_CONTEXT_IF_ERROR( + callback(saved_object_node_id, *trackable_object), "Unable to restore ", + object_name); + } + + for (const auto& trackable_child_ref : trackable_object->children()) { + const auto& local_name = trackable_child_ref.local_name(); + + // Compute the full child name. + std::string child_name; + if (object_name.empty()) { + child_name = local_name; + } else { + child_name = strings::StrCat(object_name, ".", local_name); + } + + // Descend down the trackable graph. + int trackable_child_node_id = trackable_child_ref.node_id(); + if (!seen_trackable_node_ids->insert(trackable_child_node_id).second) { + // Cycle or duplicate detected - ignore this branch. + continue; + } + if (trackable_child_node_id < 0 || + trackable_child_node_id >= trackable_object_graph().nodes_size()) { + return errors::FailedPrecondition( + strings::StrCat("Illegal trackable child node id for ", child_name)); + } + const auto* trackable_child = + &trackable_object_graph().nodes(trackable_child_node_id); + + // Descend down the saved object graph. + int saved_child_node_id = -1; + const SavedObject* saved_child = nullptr; + for (const auto& saved_child_ref : saved_object->children()) { + if (saved_child_ref.local_name() == local_name) { + // Found. + saved_child_node_id = saved_child_ref.node_id(); + if (saved_child_node_id >= 0 && + saved_child_node_id < saved_object_graph().nodes_size()) { + saved_child = &saved_object_graph().nodes(saved_child_node_id); + } + break; + } + } + + if (!saved_child) { + return absl::Status( + absl::StatusCode::kFailedPrecondition, + strings::StrCat("Could not find saved object to restore for ", + child_name)); + } + + TF_RETURN_IF_ERROR(RecurseObjectsToRestore( + saved_child, saved_child_node_id, trackable_child, child_name, + seen_trackable_node_ids, callback)); + } + return absl::OkStatus(); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/bundle_v2.h b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/bundle_v2.h new file mode 100644 index 0000000000000000000000000000000000000000..eafe32463c3cf82c9f2cc61672e4b8b2a496c2f5 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/bundle_v2.h @@ -0,0 +1,90 @@ +/* Copyright 2016 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. +==============================================================================*/ + +// Helpers for loading the persistent representation of a SavedModelV2. +// Please note that this is depended on by code that does not make use of +// the full runtime and its dependencies should be restricted. + +#ifndef TENSORFLOW_CC_SAVED_MODEL_BUNDLE_V2_H_ +#define TENSORFLOW_CC_SAVED_MODEL_BUNDLE_V2_H_ + +#include +#include +#include + +#include "absl/container/flat_hash_set.h" +#include "tensorflow/core/framework/graph_debug_info.pb.h" +#include "tensorflow/core/lib/core/status.h" +#include "tensorflow/core/protobuf/meta_graph.pb.h" +#include "tensorflow/core/protobuf/saved_object_graph.pb.h" +#include "tensorflow/core/protobuf/trackable_object_graph.pb.h" +#include "tensorflow/core/util/tensor_bundle/tensor_bundle.h" + +namespace tensorflow { + +/// Represents a version 2 SavedModel that is loaded from storage (but not yet +/// loaded into an executable in-memory representation). +class SavedModelV2Bundle { + public: + using RestoreObjectsCallback = std::function; + + /// Loads persistent representations for a SavedModelV2 from the specified + /// export directory. + static absl::Status Load(const std::string& export_dir, + SavedModelV2Bundle* bundle); + + /// MetaGraphDef from the loaded SavedModel. + MetaGraphDef& meta_graph_def() { return meta_graph_def_; } + + /// SavedObjectGraph from the MetaGraphDef. + const SavedObjectGraph& saved_object_graph() { + return meta_graph_def().object_graph_def(); + } + + /// TrackableObjectGraph loaded from the variable_reader() checkpoint. + TrackableObjectGraph& trackable_object_graph() { + return trackable_object_graph_; + } + + /// BundleReader for accessing the variables bundle. + BundleReader* variable_reader() { return variable_reader_.get(); } + + /// The GraphDebugInfo (or nullptr if none). + GraphDebugInfo* debug_info() { return debug_info_.get(); } + + /// Restores objects, invoking the callback with the node id in the + /// saved_object_graph() and the corresponding TrackableObject from the + /// trackable_object_graph(). The callback may use the variable_reader() but + /// must not modify the underlying saved_object_graph(). + absl::Status VisitObjectsToRestore(RestoreObjectsCallback callback); + + private: + absl::Status RecurseObjectsToRestore( + const SavedObject* saved_object, int saved_object_node_id, + const TrackableObjectGraph::TrackableObject* trackable_object, + std::string object_name, + absl::flat_hash_set* seen_trackable_node_ids, + RestoreObjectsCallback callback); + + MetaGraphDef meta_graph_def_; + TrackableObjectGraph trackable_object_graph_; + std::unique_ptr variable_reader_; + std::unique_ptr debug_info_; +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_CC_SAVED_MODEL_BUNDLE_V2_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/bundle_v2_test.cc b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/bundle_v2_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..3b165e3f0da3423e6fdb09eb417a684ff37d2bfa --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/bundle_v2_test.cc @@ -0,0 +1,158 @@ +/* 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 "tensorflow/cc/saved_model/bundle_v2.h" + +#include +#include +#include +#include +#include + +#include "absl/status/status.h" +#include "absl/strings/match.h" +#include "absl/strings/str_cat.h" +#include "json/json.h" +#include "json/reader.h" +#include "json/value.h" +#include "tensorflow/cc/saved_model/metrics.h" +#include "xla/tsl/lib/core/status_test_util.h" +#include "tensorflow/core/platform/path.h" +#include "tensorflow/core/platform/test.h" +#include "tensorflow/core/protobuf/trackable_object_graph.pb.h" +#include "tsl/platform/statusor.h" + +namespace tensorflow { +namespace { + +constexpr char kTestData[] = "cc/saved_model/testdata"; + +class BundleV2Test : public ::testing::Test { + protected: + BundleV2Test() {} + + void RestoreVarsAndVerify(SavedModelV2Bundle* bundle, + std::vector expected_names) { + // Collect saved_node_id, full_name, checkpoint_key into a vector. + using RestoredVarType = std::tuple; + std::vector restored_vars; + TF_ASSERT_OK(bundle->VisitObjectsToRestore( + [&](int saved_node_id, + const TrackableObjectGraph::TrackableObject& trackable_object) + -> absl::Status { + for (const auto& attr : trackable_object.attributes()) { + if (attr.name() == "VARIABLE_VALUE") { + restored_vars.emplace_back(saved_node_id, attr.full_name(), + attr.checkpoint_key()); + } + } + return absl::OkStatus(); + })); + + // Should be one of each var name restored. + for (const auto& expected_name : expected_names) { + EXPECT_EQ(1, std::count_if(restored_vars.begin(), restored_vars.end(), + [&](RestoredVarType t) { + return std::get<1>(t) == expected_name; + })); + } + + for (const auto& restored_var : restored_vars) { + // Each restored var should match a SavedObjectGraph node with the same + // variable name. + const auto& saved_node = + bundle->saved_object_graph().nodes(std::get<0>(restored_var)); + EXPECT_EQ(std::get<1>(restored_var), saved_node.variable().name()); + + // And should be able to load it from the tensor_bundle. + Tensor value; + TF_ASSERT_OK( + bundle->variable_reader()->Lookup(std::get<2>(restored_var), &value)); + } + } +}; + +TEST_F(BundleV2Test, LoadsVarsAndArithmeticObjectGraph) { + const std::string export_dir = io::JoinPath( + testing::TensorFlowSrcRoot(), kTestData, "VarsAndArithmeticObjectGraph"); + + SavedModelV2Bundle bundle; + TF_ASSERT_OK(SavedModelV2Bundle::Load(export_dir, &bundle)); + + // Ensure that there are nodes in the trackable_object_graph. + EXPECT_GT(bundle.trackable_object_graph().nodes_size(), 0); + + RestoreVarsAndVerify(&bundle, {"variable_x", "variable_y", "child_variable"}); +} + +TEST_F(BundleV2Test, LoadsCyclicModule) { + const std::string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), kTestData, "CyclicModule"); + + SavedModelV2Bundle bundle; + TF_ASSERT_OK(SavedModelV2Bundle::Load(export_dir, &bundle)); + + // Ensure that there are nodes in the trackable_object_graph. + EXPECT_GT(bundle.trackable_object_graph().nodes_size(), 0); + + RestoreVarsAndVerify(&bundle, {"MyVariable"}); +} + +TEST_F(BundleV2Test, UpdatesMetrics) { + const std::string kCCLoadBundleV2Label = "cc_load_bundle_v2"; + const int read_count = metrics::SavedModelReadCount("2").value(); + const int api_count = + metrics::SavedModelReadApi(kCCLoadBundleV2Label).value(); + const std::string export_dir = io::JoinPath( + testing::TensorFlowSrcRoot(), kTestData, "VarsAndArithmeticObjectGraph"); + + SavedModelV2Bundle bundle; + TF_ASSERT_OK(SavedModelV2Bundle::Load(export_dir, &bundle)); + + EXPECT_EQ(metrics::SavedModelReadCount("2").value(), read_count + 1); + EXPECT_EQ(metrics::SavedModelReadApi(kCCLoadBundleV2Label).value(), + api_count + 1); + // Check that the gauge contains the path and fingerprint. + EXPECT_EQ(metrics::SavedModelReadPath().value(), export_dir); + + Json::Value fingerprint = Json::objectValue; + Json::Reader reader = Json::Reader(); + reader.parse(metrics::SavedModelReadFingerprint().value(), fingerprint); + EXPECT_EQ(fingerprint["saved_model_checksum"].asUInt64(), + 15788619162413586750ULL); + EXPECT_EQ(fingerprint["graph_def_program_hash"].asUInt64(), + 706963557435316516ULL); + EXPECT_EQ(fingerprint["signature_def_hash"].asUInt64(), + 5693392539583495303ULL); + EXPECT_EQ(fingerprint["saved_object_graph_hash"].asUInt64(), + 12074714563970609759ULL); + EXPECT_EQ(fingerprint["checkpoint_hash"].asUInt64(), 10788359570789890102ULL); + + TF_ASSERT_OK_AND_ASSIGN( + auto path_and_singleprint, + metrics::ParseSavedModelPathAndSingleprint( + metrics::SavedModelReadPathAndSingleprint().value())); + auto [path, singleprint] = path_and_singleprint; + EXPECT_TRUE(absl::StrContains( + path, absl::StrCat(kTestData, "/VarsAndArithmeticObjectGraph"))); + EXPECT_EQ(singleprint, + "706963557435316516/" // graph_def_program_hash + "5693392539583495303/" // signature_def_hash + "12074714563970609759/" // saved_object_graph_hash + "10788359570789890102"); // checkpoint_hash +} + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/constants.h b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/constants.h new file mode 100644 index 0000000000000000000000000000000000000000..2b1634252050ff020e82870d939b7af2e0e589d2 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/constants.h @@ -0,0 +1,82 @@ +/* Copyright 2016 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. +==============================================================================*/ + +#ifndef TENSORFLOW_CC_SAVED_MODEL_CONSTANTS_H_ +#define TENSORFLOW_CC_SAVED_MODEL_CONSTANTS_H_ + +namespace tensorflow { + +// SavedModel assets directory. +inline constexpr char kSavedModelAssetsDirectory[] = "assets"; + +// SavedModel assets.extra directory. +inline constexpr char kSavedModelAssetsExtraDirectory[] = "assets.extra"; + +// SavedModel assets key for graph collection-def. +inline constexpr char kSavedModelAssetsKey[] = "saved_model_assets"; + +/// SavedModel legacy init op collection key. Used in v1 SavedModels. +inline constexpr char kSavedModelLegacyInitOpKey[] = "legacy_init_op"; + +/// SavedModel main op collection key. Used in v1 SavedModels. +inline constexpr char kSavedModelMainOpKey[] = "saved_model_main_op"; + +// CollectionDef key for the SavedModel train op. +// Not exported while export_all_saved_models is experimental. +inline constexpr char kSavedModelTrainOpKey[] = "saved_model_train_op"; + +// Schema version for SavedModel. +inline constexpr int kSavedModelSchemaVersion = 1; + +// SavedModel proto filename prefix. +inline constexpr char kSavedModelFilenamePrefix[] = "saved_model"; +// SavedModel proto filename. +inline constexpr char kSavedModelFilenamePb[] = "saved_model.pb"; + +// SavedModel chunked proto filename. +inline constexpr char kSavedModelFilenameCpb[] = "saved_model.cpb"; + +// SavedModel text format proto filename. +inline constexpr char kSavedModelFilenamePbTxt[] = "saved_model.pbtxt"; + +// Subdirectory where debugging related files are written. +inline constexpr char kSavedModelDebugDirectory[] = "debug"; + +// File name for GraphDebugInfo protocol buffer which corresponds to the +// SavedModel. +inline constexpr char kSavedModelDebugInfoFilenamePb[] = + "saved_model_debug_info.pb"; + +// Directory in which to save the SavedModel variables. +inline constexpr char kSavedModelVariablesDirectory[] = "variables"; + +// SavedModel variables filename. +inline constexpr char kSavedModelVariablesFilename[] = "variables"; + +// SavedModel SignatureDef keys for the initialization and train ops. Used in +// V2 SavedModels. +inline constexpr char kSavedModelInitOpSignatureKey[] = "__saved_model_init_op"; +inline constexpr char kSavedModelTrainOpSignatureKey[] = + "__saved_model_train_op"; + +// Key in the TensorBundle for the object graph proto. +inline constexpr char kObjectGraphProtoKey[] = "_CHECKPOINTABLE_OBJECT_GRAPH"; + +// Filename for the FingerprintDef protocol buffer. +inline constexpr char kFingerprintFilenamePb[] = "fingerprint.pb"; + +} // namespace tensorflow + +#endif // TENSORFLOW_CC_SAVED_MODEL_CONSTANTS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/BUILD b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..dc97332849b05cf324089e68b2c3787fb7929655 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/BUILD @@ -0,0 +1,85 @@ +# Experimental C++ SavedModel Header Only APIs. See RFC +# https://github.com/tensorflow/community/pull/207 + +load("//tensorflow/core/platform:rules_cc.bzl", "cc_library") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:license"], + # This is intentionally public + default_visibility = [ + "//visibility:public", + ], + licenses = ["notice"], +) + +cc_library( + name = "concrete_function", + hdrs = [ + "concrete_function.h", + ], + deps = [ + ":function_metadata", + "//tensorflow/c/eager:c_api", + "//tensorflow/c/experimental/saved_model/public:concrete_function", + "//tensorflow/cc/experimental/base/public:status", + ], +) + +cc_library( + name = "concrete_function_list", + hdrs = [ + "concrete_function_list.h", + ], + deps = [ + ":concrete_function", + "//tensorflow/c/experimental/saved_model/public:concrete_function_list", + ], +) + +cc_library( + name = "function_metadata", + hdrs = [ + "function_metadata.h", + ], + deps = [ + "//tensorflow/c/experimental/saved_model/public:function_metadata", + ], +) + +cc_library( + name = "saved_model_api", + hdrs = [ + "saved_model_api.h", + ], + deps = [ + ":concrete_function", + ":concrete_function_list", + ":signature_def_function", + "//tensorflow/c/experimental/saved_model/public:saved_model_api", + "//tensorflow/cc/experimental/base/public:runtime", + "//tensorflow/cc/experimental/base/public:status", + ], +) + +cc_library( + name = "signature_def_function", + hdrs = [ + "signature_def_function.h", + ], + deps = [ + ":signature_def_function_metadata", + "//tensorflow/c/eager:c_api", + "//tensorflow/c/experimental/saved_model/public:signature_def_function", + "//tensorflow/cc/experimental/base/public:status", + ], +) + +cc_library( + name = "signature_def_function_metadata", + hdrs = [ + "signature_def_function_metadata.h", + ], + deps = [ + "//tensorflow/c/experimental/saved_model/public:signature_def_function_metadata", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/concrete_function.h b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/concrete_function.h new file mode 100644 index 0000000000000000000000000000000000000000..90053902b5b6055f10839b2e018a139a42cd7b02 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/concrete_function.h @@ -0,0 +1,61 @@ +/* Copyright 2020 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. +==============================================================================*/ + +#ifndef TENSORFLOW_CC_SAVED_MODEL_EXPERIMENTAL_PUBLIC_CONCRETE_FUNCTION_H_ +#define TENSORFLOW_CC_SAVED_MODEL_EXPERIMENTAL_PUBLIC_CONCRETE_FUNCTION_H_ + +#include + +#include "tensorflow/c/eager/c_api.h" +#include "tensorflow/c/experimental/saved_model/public/concrete_function.h" +#include "tensorflow/cc/experimental/base/public/status.h" +#include "tensorflow/cc/saved_model/experimental/public/function_metadata.h" + +namespace tensorflow { +namespace experimental { +namespace cc { + +// ConcreteFunction is an executable "function" loaded from a SavedModelAPI. +class ConcreteFunction final { + public: + // TODO(bmzhao): Adding ConcreteFunction::Run in subsequent CL, since + // it depends on tensorflow::cc::Tensor and tensorflow::cc::TensorHandle + + // Returns FunctionMetadata associated with this ConcreteFunction. + const FunctionMetadata* GetFunctionMetadata(); + + private: + friend class SavedModelAPI; + friend class ConcreteFunctionList; + + // TODO(bmzhao): Consider adding a macro for wrapping/unwrapping + // when moving out of experimental. + static ConcreteFunction* wrap(TF_ConcreteFunction* p) { + return reinterpret_cast(p); + } + static TF_ConcreteFunction* unwrap(ConcreteFunction* p) { + return reinterpret_cast(p); + } +}; + +inline const FunctionMetadata* ConcreteFunction::GetFunctionMetadata() { + return FunctionMetadata::wrap(TF_ConcreteFunctionGetMetadata(unwrap(this))); +} + +} // namespace cc +} // namespace experimental +} // namespace tensorflow + +#endif // TENSORFLOW_CC_SAVED_MODEL_EXPERIMENTAL_PUBLIC_CONCRETE_FUNCTION_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/concrete_function_list.h b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/concrete_function_list.h new file mode 100644 index 0000000000000000000000000000000000000000..7e21d1039e4ee500588e31e98c6c0d6ec7bff549 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/concrete_function_list.h @@ -0,0 +1,63 @@ +/* Copyright 2020 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. +==============================================================================*/ + +#ifndef TENSORFLOW_CC_SAVED_MODEL_EXPERIMENTAL_PUBLIC_CONCRETE_FUNCTION_LIST_H_ +#define TENSORFLOW_CC_SAVED_MODEL_EXPERIMENTAL_PUBLIC_CONCRETE_FUNCTION_LIST_H_ + +#include + +#include "tensorflow/c/experimental/saved_model/public/concrete_function_list.h" +#include "tensorflow/cc/saved_model/experimental/public/concrete_function.h" + +namespace tensorflow { +namespace experimental { +namespace cc { + +// ConcreteFunctionList helps convert an opaque pointer to an array of +// ConcreteFunction pointers to a std::vector. +class ConcreteFunctionList { + public: + // Converts this object to a std::vector + std::vector ToVector(); + + private: + friend class SavedModelAPI; + // Wraps a TF_ConcreteFunctionList. Takes ownership of list. + explicit ConcreteFunctionList(TF_ConcreteFunctionList* list) : list_(list) {} + + struct TFConcreteFunctionListDeleter { + void operator()(TF_ConcreteFunctionList* p) const { + TF_DeleteConcreteFunctionList(p); + } + }; + std::unique_ptr list_; +}; + +inline std::vector ConcreteFunctionList::ToVector() { + int size = TF_ConcreteFunctionListSize(list_.get()); + std::vector result; + result.reserve(size); + for (int i = 0; i < size; ++i) { + result.push_back( + ConcreteFunction::wrap(TF_ConcreteFunctionListGet(list_.get(), i))); + } + return result; +} + +} // namespace cc +} // namespace experimental +} // namespace tensorflow + +#endif // TENSORFLOW_CC_SAVED_MODEL_EXPERIMENTAL_PUBLIC_CONCRETE_FUNCTION_LIST_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/function_metadata.h b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/function_metadata.h new file mode 100644 index 0000000000000000000000000000000000000000..529bf0197b4e0bb0e761f582154c96994419ca83 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/function_metadata.h @@ -0,0 +1,47 @@ +/* Copyright 2020 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. +==============================================================================*/ + +#ifndef TENSORFLOW_CC_SAVED_MODEL_EXPERIMENTAL_PUBLIC_FUNCTION_METADATA_H_ +#define TENSORFLOW_CC_SAVED_MODEL_EXPERIMENTAL_PUBLIC_FUNCTION_METADATA_H_ + +#include + +#include "tensorflow/c/experimental/saved_model/public/function_metadata.h" + +namespace tensorflow { +namespace experimental { +namespace cc { + +// FunctionMetadata stores additional function information, including +// optional signaturedef feeds/fetches (for TF1-based ConcreteFunctions), +// a valid function path (for TF2-based ConcreteFunctions), and +// the types + number of inputs and outputs. +class FunctionMetadata final { + // TODO(bmzhao): Add getters here as necessary. + private: + friend class ConcreteFunction; + static FunctionMetadata* wrap(TF_FunctionMetadata* p) { + return reinterpret_cast(p); + } + static TF_FunctionMetadata* unwrap(FunctionMetadata* p) { + return reinterpret_cast(p); + } +}; + +} // namespace cc +} // namespace experimental +} // namespace tensorflow + +#endif // TENSORFLOW_CC_SAVED_MODEL_EXPERIMENTAL_PUBLIC_FUNCTION_METADATA_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/saved_model_api.h b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/saved_model_api.h new file mode 100644 index 0000000000000000000000000000000000000000..f5bde4a8136d7e54948103d78b24272e050703ed --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/saved_model_api.h @@ -0,0 +1,155 @@ +/* Copyright 2020 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. +==============================================================================*/ + +#ifndef TENSORFLOW_CC_SAVED_MODEL_EXPERIMENTAL_PUBLIC_SAVED_MODEL_API_H_ +#define TENSORFLOW_CC_SAVED_MODEL_EXPERIMENTAL_PUBLIC_SAVED_MODEL_API_H_ + +#include +#include +#include +#include + +#include "tensorflow/c/experimental/saved_model/public/saved_model_api.h" +#include "tensorflow/cc/experimental/base/public/runtime.h" +#include "tensorflow/cc/experimental/base/public/status.h" +#include "tensorflow/cc/saved_model/experimental/public/concrete_function.h" +#include "tensorflow/cc/saved_model/experimental/public/concrete_function_list.h" +#include "tensorflow/cc/saved_model/experimental/public/signature_def_function.h" + +namespace tensorflow { +namespace experimental { +namespace cc { + +// SavedModelAPI offers a way to load Tensorflow Saved Models +// (https://www.tensorflow.org/guide/saved_model) and execute saved +// tf.functions or legacy SignatureDefs in a TF2-idiomatic fashion. +// See RFC 207 +// (https://github.com/tensorflow/community/blob/master/rfcs/20200218-tf-c-saved-model.md) +// TODO(bmzhao): Add an e2e example here, once ConcreteFunction::Run is added. +class SavedModelAPI { + public: + // Load a SavedModel from `dirname`. + // + // Params: + // saved_model_path - A directory filepath that the SavedModel is at. + // runtime - A runtime used to load SavedModelAPI. `runtime` must outlive the + // returned TF_SavedModel pointer. + // tags - Optional set of tags. If tags = nullptr, we expect the SavedModel + // to contain a single Metagraph (as for those exported from TF2's + // `tf.saved_model.save`). If tags != nullptr, we load the metagraph + // matching the tags: + // https://github.com/tensorflow/tensorflow/blob/428cdeda09aef81e958eeb274b83d27ad635b57b/tensorflow/core/protobuf/meta_graph.proto#L50-L56 + // status - Set to OK on success and an appropriate error on failure. + // Returns: + // If status is not OK, returns nullptr. + static std::unique_ptr Load( + const std::string& saved_model_path, const Runtime& runtime, + Status* status, const std::unordered_set* tags = nullptr); + + // Retrieve a function from the TF2 SavedModel via function path. + // + // Params: + // function_path - A string containing the path from the root saved python + // object to a tf.function method. + // status - Set to OK on success and an appropriate error on failure. + // Returns: + // If status is not OK, returns nullptr. Otherwise, returns a + // tensorflow::cc::ConcreteFunction pointer. The lifetime of this pointer + // is bound to SavedModelAPI it was loaded from. + ConcreteFunction* GetConcreteFunction(const std::string& function_path, + Status* status); + + // Retrieve a function from the TF SavedModel via a SignatureDef key. + // + // Params: + // signature_def_key - String key of SignatureDef map of a SavedModel: + // https://github.com/tensorflow/tensorflow/blob/69b08900b1e991d84bce31f3b404f5ed768f339f/tensorflow/core/protobuf/meta_graph.proto#L89 + // status - Set to OK on success and an appropriate error on failure. + // Returns: + // If status is not OK, returns nullptr. Otherwise, returns a + // tensorflow::cc::ConcreteFunction pointer. The lifetime of this pointer + // is bound to SavedModelAPI it was loaded from. + SignatureDefFunction* GetSignatureDefFunction( + const std::string& function_path, Status* status); + + // SavedModelAPI is movable, but not copyable. + SavedModelAPI(SavedModelAPI&&) = default; + SavedModelAPI& operator=(SavedModelAPI&&) = default; + + private: + SavedModelAPI(const SavedModelAPI&) = delete; + SavedModelAPI& operator=(const SavedModelAPI&) = delete; + + explicit SavedModelAPI(TF_SavedModel* model) : saved_model_(model) {} + struct TFSavedModelDeleter { + void operator()(TF_SavedModel* p) const { TF_DeleteSavedModel(p); } + }; + std::unique_ptr saved_model_; +}; + +inline std::unique_ptr SavedModelAPI::Load( + const std::string& saved_model_path, const Runtime& runtime, Status* status, + const std::unordered_set* tags) { + TF_SavedModel* saved_model = nullptr; + + if (tags == nullptr) { + saved_model = + TF_LoadSavedModel(saved_model_path.c_str(), runtime.GetTFEContext(), + status->GetTFStatus()); + } else { + std::vector tags_vector; + tags_vector.reserve(tags->size()); + for (const std::string& tag : *tags) { + tags_vector.push_back(tag.c_str()); + } + saved_model = TF_LoadSavedModelWithTags( + saved_model_path.c_str(), runtime.GetTFEContext(), tags_vector.data(), + tags_vector.size(), status->GetTFStatus()); + } + + if (!status->ok()) { + return nullptr; + } + + // We can't use std::make_unique here because of its interaction with a + // private constructor: https://abseil.io/tips/134 + return std::unique_ptr(new SavedModelAPI(saved_model)); +} + +inline ConcreteFunction* SavedModelAPI::GetConcreteFunction( + const std::string& function_path, Status* status) { + TF_ConcreteFunction* function = TF_GetSavedModelConcreteFunction( + saved_model_.get(), function_path.c_str(), status->GetTFStatus()); + if (!status->ok()) { + return nullptr; + } + return ConcreteFunction::wrap(function); +} + +inline SignatureDefFunction* SavedModelAPI::GetSignatureDefFunction( + const std::string& function_path, Status* status) { + TF_SignatureDefFunction* function = TF_GetSavedModelSignatureDefFunction( + saved_model_.get(), function_path.c_str(), status->GetTFStatus()); + if (!status->ok()) { + return nullptr; + } + return SignatureDefFunction::wrap(function); +} + +} // namespace cc +} // namespace experimental +} // namespace tensorflow + +#endif // TENSORFLOW_CC_SAVED_MODEL_EXPERIMENTAL_PUBLIC_SAVED_MODEL_API_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/signature_def_function.h b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/signature_def_function.h new file mode 100644 index 0000000000000000000000000000000000000000..fdb1980fa1289856c97de5ca6e7a98ed8345d39a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/signature_def_function.h @@ -0,0 +1,89 @@ +/* Copyright 2020 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. +==============================================================================*/ + +#ifndef TENSORFLOW_CC_SAVED_MODEL_EXPERIMENTAL_PUBLIC_SIGNATURE_DEF_FUNCTION_H_ +#define TENSORFLOW_CC_SAVED_MODEL_EXPERIMENTAL_PUBLIC_SIGNATURE_DEF_FUNCTION_H_ + +#include + +#include "tensorflow/c/eager/c_api.h" +#include "tensorflow/c/experimental/saved_model/public/signature_def_function.h" +#include "tensorflow/cc/experimental/base/public/status.h" +#include "tensorflow/cc/saved_model/experimental/public/signature_def_function_metadata.h" + +namespace tensorflow { +namespace experimental { +namespace cc { + +// SignatureDefFunctions are functions that correspond to either: +// "signatures" saved from a TF2 SavedModel APIs: +// https://github.com/tensorflow/tensorflow/blob/8ce0600f58ed84a8c84a7bbdb014d1f09e44f4c8/tensorflow/python/saved_model/save.py#L830-L854 +// Or the "SignatureDefMap" saved from TF1 SavedModel APIs: +// https://github.com/tensorflow/tensorflow/blob/8ce0600f58ed84a8c84a7bbdb014d1f09e44f4c8/tensorflow/python/saved_model/load_v1_in_v2_test.py#L170-L174 +// In both cases, a SignatureDef is serialized as a SignatureDef protobuf: +// https://github.com/tensorflow/tensorflow/blob/8ce0600f58ed84a8c84a7bbdb014d1f09e44f4c8/tensorflow/core/protobuf/meta_graph.proto#L260-L330 +// and represents a computation defined by a TF subgraph. +// These Signatures were primarily designed to be interoperable with the legacy +// TF 1 Session-based C++ SavedModelBundle loading APIs: +// https://github.com/tensorflow/tensorflow/blob/26c4ee0c833e74f94d0102d8b005c41a28b44445/tensorflow/cc/saved_model/loader.h#L96-L108 +// SignatureDefFunctions have different semantics from regular TF2 +// ConcreteFunctions, and are mainly intended provide a serving-friendly +// transition point from the TF1 Session API. +// First, SignatureDefFunctions have different calling conventions. +// SignatureDefFunctions' inputs and outputs are constrained to **flattened +// lists of TensorHandles only**. They do not support more exotic input/output +// types (like optionals, generators, etc). Additionally, this flattening means +// they will not preserve the exact interface of the original tf.function they +// were traced from, as things like composite tensors decay into their +// internal dense tensor representation. +// Second, all inputs and outputs are "named", and these names are load bearing +// (eg: they are part of the interface of tensorflow_serving): +// https://github.com/tensorflow/serving/blob/e0d247b2e4050713194b8fad0be24a0636df7209/tensorflow_serving/apis/predict.proto#L21 +// https://github.com/tensorflow/serving/blob/e0d247b2e4050713194b8fad0be24a0636df7209/tensorflow_serving/apis/predict.proto#L39 +// The name of each input/output is stored in the corresponding tf::Argument in +// SignatureDefFunctionMetadata::arguments(). Users must ensure the order of +// TensorHandles passed to the function matches with the order of named +// arguments. Similarly the name of the outputs is stored in +// SignatureDefFunctionMetadata::returns(). +class SignatureDefFunction final { + public: + // Returns FunctionMetadata associated with this ConcreteFunction. + const SignatureDefFunctionMetadata* GetFunctionMetadata(); + + private: + friend class SavedModelAPI; + friend class ConcreteFunctionList; + + // TODO(bmzhao): Consider adding a macro for wrapping/unwrapping + // when moving out of experimental. + static SignatureDefFunction* wrap(TF_SignatureDefFunction* p) { + return reinterpret_cast(p); + } + static TF_SignatureDefFunction* unwrap(SignatureDefFunction* p) { + return reinterpret_cast(p); + } +}; + +inline const SignatureDefFunctionMetadata* +SignatureDefFunction::GetFunctionMetadata() { + return SignatureDefFunctionMetadata::wrap( + TF_SignatureDefFunctionGetMetadata(unwrap(this))); +} + +} // namespace cc +} // namespace experimental +} // namespace tensorflow + +#endif // TENSORFLOW_CC_SAVED_MODEL_EXPERIMENTAL_PUBLIC_SIGNATURE_DEF_FUNCTION_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/signature_def_function_metadata.h b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/signature_def_function_metadata.h new file mode 100644 index 0000000000000000000000000000000000000000..b938eacf28f9b717406c6d02d20c0c0497b26e9d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/signature_def_function_metadata.h @@ -0,0 +1,47 @@ +/* Copyright 2020 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. +==============================================================================*/ + +#ifndef TENSORFLOW_CC_SAVED_MODEL_EXPERIMENTAL_PUBLIC_SIGNATURE_DEF_FUNCTION_METADATA_H_ +#define TENSORFLOW_CC_SAVED_MODEL_EXPERIMENTAL_PUBLIC_SIGNATURE_DEF_FUNCTION_METADATA_H_ + +#include + +#include "tensorflow/c/experimental/saved_model/public/signature_def_function_metadata.h" + +namespace tensorflow { +namespace experimental { +namespace cc { + +// SignatureDefFunctionMetadata stores additional information on each input +// and output's names, dtypes, and shape. +class SignatureDefFunctionMetadata final { + // TODO(bmzhao): Add getters here as necessary. + private: + friend class SignatureDefFunction; + static SignatureDefFunctionMetadata* wrap( + TF_SignatureDefFunctionMetadata* p) { + return reinterpret_cast(p); + } + static TF_SignatureDefFunctionMetadata* unwrap( + SignatureDefFunctionMetadata* p) { + return reinterpret_cast(p); + } +}; + +} // namespace cc +} // namespace experimental +} // namespace tensorflow + +#endif // TENSORFLOW_CC_SAVED_MODEL_EXPERIMENTAL_PUBLIC_SIGNATURE_DEF_FUNCTION_METADATA_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/tests/BUILD b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/tests/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..fb18d16fc3453361e980acdc30964d786678437c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/tests/BUILD @@ -0,0 +1,28 @@ +# Tests for the C++ header-only SavedModelAPI. +load("//tensorflow:tensorflow.bzl", "tf_cc_test") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:license"], + licenses = ["notice"], +) + +tf_cc_test( + name = "saved_model_api_test", + srcs = [ + "saved_model_api_test.cc", + ], + data = [ + "//tensorflow/cc/saved_model:saved_model_half_plus_two", + ], + deps = [ + "//tensorflow/c:tf_status_headers", + "//tensorflow/cc/experimental/base/public:runtime", + "//tensorflow/cc/experimental/base/public:runtime_builder", + "//tensorflow/cc/experimental/base/public:status", + "//tensorflow/cc/saved_model/experimental/public:saved_model_api", + "//tensorflow/core:lib", + "//tensorflow/core:test", + "//tensorflow/core:test_main", + "@com_google_absl//absl/strings:string_view", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/tests/saved_model_api_test.cc b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/tests/saved_model_api_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..83505d13fb94292c1c3dd5ce22927b31c607bdbc --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/tests/saved_model_api_test.cc @@ -0,0 +1,98 @@ +/* Copyright 2020 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/cc/saved_model/experimental/public/saved_model_api.h" + +#include +#include +#include + +#include "absl/strings/string_view.h" +#include "tensorflow/c/tf_status.h" +#include "tensorflow/cc/experimental/base/public/runtime.h" +#include "tensorflow/cc/experimental/base/public/runtime_builder.h" +#include "tensorflow/cc/experimental/base/public/status.h" +#include "tensorflow/core/platform/path.h" +#include "tensorflow/core/platform/stringpiece.h" +#include "tensorflow/core/platform/test.h" + + +namespace { + +using tensorflow::experimental::cc::Runtime; +using tensorflow::experimental::cc::RuntimeBuilder; +using tensorflow::experimental::cc::SavedModelAPI; +using tensorflow::experimental::cc::Status; + +constexpr char kTestData[] = "cc/saved_model/testdata"; + +std::string SavedModelPath(absl::string_view saved_model_dir) { + return tensorflow::io::JoinPath(tensorflow::testing::TensorFlowSrcRoot(), + kTestData, saved_model_dir); +} + +// This value parameterized test allows us to test both TFRT +// and non TFRT runtimes. +// https://github.com/google/googletest/blob/dcc92d0ab6c4ce022162a23566d44f673251eee4/googletest/docs/advanced.md#value-parameterized-tests +class CPPSavedModelAPITest : public ::testing::TestWithParam {}; + +TEST_P(CPPSavedModelAPITest, LoadsSavedModelWithTags) { + Status status; + RuntimeBuilder builder; + bool use_tfrt = GetParam(); + if (use_tfrt) { + GTEST_SKIP(); // TODO(chky) : Enable this once TFRT is open sourced. + } + + builder.SetUseTFRT(use_tfrt); + std::unique_ptr runtime = builder.Build(&status); + ASSERT_TRUE(status.ok()) << status.message(); + + std::string model_dir = SavedModelPath("VarsAndArithmeticObjectGraph"); + std::unordered_set tags = {"serve"}; + std::unique_ptr model = + SavedModelAPI::Load(model_dir, *runtime, &status, &tags); + + // TODO(bmzhao): Change this to expect TF_OK when loading is implemented. + // That unblocks writing other tests that require a TF_SavedModel*, + // like loading a ConcreteFunction. This test at least checks that the + // C API builds and can be minimally run. + EXPECT_EQ(status.code(), TF_UNIMPLEMENTED); +} + +TEST_P(CPPSavedModelAPITest, LoadsSavedModel) { + Status status; + RuntimeBuilder builder; + bool use_tfrt = GetParam(); + if (use_tfrt) { + GTEST_SKIP(); // TODO(chky) : Enable this once TFRT is open sourced. + } + + builder.SetUseTFRT(use_tfrt); + std::unique_ptr runtime = builder.Build(&status); + ASSERT_TRUE(status.ok()) << status.message(); + + std::string model_dir = SavedModelPath("VarsAndArithmeticObjectGraph"); + std::unique_ptr model = + SavedModelAPI::Load(model_dir, *runtime, &status); + + EXPECT_EQ(status.code(), TF_OK) << status.message(); +} + +INSTANTIATE_TEST_SUITE_P(RuntimeAgnosticCPPSavedModelTests, + CPPSavedModelAPITest, ::testing::Bool()); + +} // namespace + diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting.cc b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting.cc new file mode 100644 index 0000000000000000000000000000000000000000..b0f01aee235a325e3d27e6168c922273c9b687d8 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting.cc @@ -0,0 +1,285 @@ +/* Copyright 2022 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/cc/saved_model/fingerprinting.h" + +#include +#include + +#include "absl/container/btree_map.h" +#include "absl/log/log.h" +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" +#include "absl/strings/strip.h" +#include "tensorflow/cc/saved_model/constants.h" +#include "tensorflow/cc/saved_model/fingerprinting_x_platform_utils.h" +#include "tensorflow/core/framework/versions.pb.h" +#include "tensorflow/core/graph/regularization/simple_delete.h" +#include "tensorflow/core/graph/regularization/util.h" +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/platform/file_system_helper.h" +#include "tensorflow/core/platform/fingerprint.h" +#include "tensorflow/core/platform/path.h" +#include "tensorflow/core/platform/protobuf.h" // IWYU pragma: keep +#include "tensorflow/core/protobuf/fingerprint.pb.h" +#include "tensorflow/core/protobuf/meta_graph.pb.h" +#include "tensorflow/core/protobuf/saved_model.pb.h" +#include "tensorflow/core/protobuf/saved_object_graph.pb.h" +#include "tensorflow/core/util/tensor_bundle/naming.h" +// b/291933687, b/291001524 +#if !defined(PLATFORM_WINDOWS) && !defined(__APPLE__) +#include "tensorflow/cc/saved_model/fingerprinting_utils.h" +#include "tensorflow/tools/proto_splitter/cc/util.h" +#include "tsl/platform/errors.h" +#include "tsl/platform/statusor.h" +#endif +// IWYU pragma: no_include "third_party/protobuf/io/coded_stream.h" +// IWYU pragma: no_include "third_party/protobuf/io/zero_copy_stream_impl_lite.h" + +namespace tensorflow::saved_model::fingerprinting { + +namespace { + +using ::tensorflow::protobuf::Map; +// NOLINTNEXTLINE: clang-tidy missing-includes false positive +using ::tensorflow::protobuf::io::CodedOutputStream; +// NOLINTNEXTLINE: clang-tidy missing-includes false positive +using ::tensorflow::protobuf::io::StringOutputStream; + +// TODO(b/290063184): remove when USM is GA +uint64_t HashCheckpointIndexFile(absl::string_view model_dir) { + std::string meta_filename = MetaFilename(io::JoinPath( + model_dir, kSavedModelVariablesDirectory, kSavedModelVariablesFilename)); + std::string data; + absl::Status read_status = + ReadFileToString(Env::Default(), meta_filename, &data); + if (read_status.ok()) { + return tensorflow::Fingerprint64(data); + } else { + LOG(WARNING) << "Failed to read checkpoint file: " << read_status; + return 0; + } +} + +uint64_t HashSavedModel(const SavedModel& saved_model) { + std::string saved_model_serialized; + { + // Local scope guarantees coded stream will be trimmed (ensures + // serialization determinism). + // Unfortunately the saving process itself isn't deterministic, so the + // checksum may still change since the saved_model proto may be different. + StringOutputStream stream(&saved_model_serialized); + CodedOutputStream output(&stream); + output.SetSerializationDeterministic(true); + saved_model.SerializeToCodedStream(&output); + } + return tensorflow::Fingerprint64(saved_model_serialized); +} + +uint64_t RegularizeAndHashSignatureDefs( + const Map& signature_def_map) { + // Sort `signature_def_map`, which is an unordered map from string keys to + // SignatureDefs. + absl::btree_map sorted_signature_defs; + sorted_signature_defs.insert(signature_def_map.begin(), + signature_def_map.end()); + uint64_t result_hash = 0; + for (const auto& item : sorted_signature_defs) { + result_hash = + FingerprintCat64(result_hash, tensorflow::Fingerprint64(item.first)); + std::string signature_def_serialized; + { + StringOutputStream stream(&signature_def_serialized); + CodedOutputStream output(&stream); + output.SetSerializationDeterministic(true); + item.second.SerializeToCodedStream(&output); + } + result_hash = FingerprintCat64( + result_hash, tensorflow::Fingerprint64(signature_def_serialized)); + } + return result_hash; +} + +// The SavedObjectGraph contains two parts: the list of nodes and the map of +// concrete functions. Regularization treats these two parts separately. +absl::StatusOr RegularizeAndHashSavedObjectGraph( + const SavedObjectGraph& object_graph_def) { + // Sort `concrete_functions`, which is an unordered map from function names to + // SavedConcreteFunction, using the suffix UID of the function name. Assumes + // that the trackable children are listed in a deterministic order during + // serialization. + absl::btree_map uid_to_function_names; + for (const auto& [name, concrete_function] : + object_graph_def.concrete_functions()) { + // All valid function names should end in an UID. + TF_ASSIGN_OR_RETURN(int64_t uid, graph_regularization::GetSuffixUID(name)); + uid_to_function_names.insert({uid, name}); + } + uint64_t result_hash = 0; + for (const auto& [uid, function_name] : uid_to_function_names) { + // Hash the function name (with the UID stripped). + result_hash = FingerprintCat64(result_hash, + tensorflow::Fingerprint64(absl::StripSuffix( + function_name, std::to_string(uid)))); + // Hash the serialized concrete function. + std::string concrete_function_serialized; + { + StringOutputStream stream(&concrete_function_serialized); + CodedOutputStream output(&stream); + output.SetSerializationDeterministic(true); + object_graph_def.concrete_functions() + .at(function_name) + .SerializeToCodedStream(&output); + } + result_hash = FingerprintCat64( + result_hash, tensorflow::Fingerprint64(concrete_function_serialized)); + } + // TODO(b/241294832): Complete canonicalization of `object_graph_def.nodes`. + return result_hash; +} + +void SetFingerprintUUID(FingerprintDef* fingerprint_def) { + // Assign a random UUID to the fingerprint. This can happen regardless of + // whether the SavedModel was read successfully. It serves as a unique + // identifier for the model even in situations where the SavedModel is + // non-existent or unreadable. + fingerprint_def->set_uuid(CreateRandomUUID()); +} + +// Creates a FingerprintDef proto from a SavedModel and the checkpoint meta file +// (.index) in `export_dir`. +absl::StatusOr CreateFingerprintDefPb( + absl::string_view export_dir, std::string pb_file) { + // Version of the code that produced the fingerprint. + // Note corresponding version definition in + // FingerprintingUtils.cc:CreateFingerprintDefCpb() and in + // Fingerprinting.cc:CreateReducedFingerprintDef() + const int kFingerprintProducer = 4; + + SavedModel saved_model; + TF_RETURN_IF_ERROR(ReadBinaryProto(Env::Default(), pb_file, &saved_model)); + + // Create a copy of `metagraph` which will be used and mutated for fingerprint + // computation. + FingerprintDef fingerprint_def; + MetaGraphDef* metagraph = saved_model.mutable_meta_graphs(0); + // Set fingerprint field #1. + fingerprint_def.set_saved_model_checksum(HashSavedModel(saved_model)); + // Set fingerprint field #2. + graph_regularization::SimpleDelete(*metagraph->mutable_graph_def()); + fingerprint_def.set_graph_def_program_hash( + graph_regularization::ComputeHash(metagraph->graph_def())); + // Set fingerprint field #3. + fingerprint_def.set_signature_def_hash( + RegularizeAndHashSignatureDefs(metagraph->signature_def())); + // Set fingerprint field #4. + TF_ASSIGN_OR_RETURN( + uint64_t object_graph_hash, + RegularizeAndHashSavedObjectGraph(metagraph->object_graph_def())); + fingerprint_def.set_saved_object_graph_hash(object_graph_hash); + // Set fingerprint field #5. + fingerprint_def.set_checkpoint_hash(HashCheckpointIndexFile(export_dir)); + SetFingerprintUUID(&fingerprint_def); + // Set version of the fingerprint. + VersionDef* version = fingerprint_def.mutable_version(); + version->set_producer(kFingerprintProducer); + + return fingerprint_def; +} + +absl::StatusOr CreateReducedFingerprintDef() { + // Version of the code that produced the fingerprint. + // Note corresponding version definition in + // FingerprintingUtils.cc:CreateFingerprintDefCpb() and in + // Fingerprinting.cc:CreateFingerprintDefPb() + const int kFingerprintProducer = 5; + + FingerprintDef fingerprint_def; + SetFingerprintUUID(&fingerprint_def); + // Set version of the fingerprint. + VersionDef* version = fingerprint_def.mutable_version(); + version->set_producer(kFingerprintProducer); + + return fingerprint_def; +} + +} // namespace + +absl::StatusOr CreateFingerprintDef( + absl::string_view export_dir) { + std::string prefix = io::JoinPath(export_dir, kSavedModelFilenamePrefix); + +#if !defined(PLATFORM_WINDOWS) && !defined(__APPLE__) + absl::StatusOr only_contains_pb = + tools::proto_splitter::OnlyContainsPb(prefix); + // TODO: b/455882293 - Enable reporting errors if the .[c]pb files are + // important but still missing. + if (only_contains_pb.ok()) { + if (*only_contains_pb) { + return CreateFingerprintDefPb(export_dir, absl::StrCat(prefix, ".pb")); + } + return CreateFingerprintDefCpb(export_dir, absl::StrCat(prefix, ".cpb")); + } + // At this point we have neither saved_model.pb nor saved_model.cpb. + return CreateReducedFingerprintDef(); // Only sets the UUID. +#else // The following runs on Windows and Mac. + absl::StatusOr fingerprint_def = + CreateFingerprintDefPb(export_dir, absl::StrCat(prefix, ".pb")); + if (!fingerprint_def.ok()) { + return CreateReducedFingerprintDef(); + } + return fingerprint_def; +#endif +} + +absl::StatusOr ReadSavedModelFingerprint( + absl::string_view export_dir) { + const std::string fingerprint_pb_path = + io::JoinPath(export_dir, kFingerprintFilenamePb); + TF_RETURN_IF_ERROR(Env::Default()->FileExists(fingerprint_pb_path)); + + FingerprintDef fingerprint_proto; + absl::Status result = + ReadBinaryProto(Env::Default(), fingerprint_pb_path, &fingerprint_proto); + if (!result.ok()) return result; + + return fingerprint_proto; +} + +std::string Singleprint(uint64_t graph_def_program_hash, + uint64_t signature_def_hash, + uint64_t saved_object_graph_hash, + uint64_t checkpoint_hash) { + return std::to_string(graph_def_program_hash) + "/" + + std::to_string(signature_def_hash) + "/" + + std::to_string(saved_object_graph_hash) + "/" + + std::to_string(checkpoint_hash); +} + +std::string Singleprint(const FingerprintDef& fingerprint) { + return Singleprint( + fingerprint.graph_def_program_hash(), fingerprint.signature_def_hash(), + fingerprint.saved_object_graph_hash(), fingerprint.checkpoint_hash()); +} + +absl::StatusOr Singleprint(absl::string_view export_dir) { + TF_ASSIGN_OR_RETURN(const FingerprintDef fingerprint_def, + ReadSavedModelFingerprint(export_dir)); + return Singleprint(fingerprint_def); +} + +} // namespace tensorflow::saved_model::fingerprinting diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting.h b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting.h new file mode 100644 index 0000000000000000000000000000000000000000..10c8272825a0493aede061ca428ec9118269f6bf --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting.h @@ -0,0 +1,47 @@ +/* Copyright 2022 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. +==============================================================================*/ + +#ifndef TENSORFLOW_CC_SAVED_MODEL_FINGERPRINTING_H_ +#define TENSORFLOW_CC_SAVED_MODEL_FINGERPRINTING_H_ + +#include + +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" +#include "tensorflow/core/protobuf/fingerprint.pb.h" + +namespace tensorflow::saved_model::fingerprinting { + +// Creates a FingerprintDef proto from a SavedModel (regular or chunked) and the +// checkpoint meta file (.index) in `export_dir`. +absl::StatusOr CreateFingerprintDef( + absl::string_view export_dir); + +// Loads the `fingerprint.pb` from `export_dir`, returns an error if there is +// none. +absl::StatusOr ReadSavedModelFingerprint( + absl::string_view export_dir); + +// Canonical fingerprinting ID for a SavedModel. +std::string Singleprint(uint64_t graph_def_program_hash, + uint64_t signature_def_hash, + uint64_t saved_object_graph_hash, + uint64_t checkpoint_hash); +std::string Singleprint(const FingerprintDef& fingerprint); +absl::StatusOr Singleprint(absl::string_view export_dir); + +} // namespace tensorflow::saved_model::fingerprinting + +#endif // TENSORFLOW_CC_SAVED_MODEL_FINGERPRINTING_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_chunked_test.cc b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_chunked_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..eee8b1bb6d4be47293bfb8b3d7c0fc31c226069d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_chunked_test.cc @@ -0,0 +1,52 @@ +/* Copyright 2023 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 "absl/container/flat_hash_set.h" +#include "tensorflow/cc/saved_model/fingerprinting.h" +#include "tensorflow/core/platform/path.h" +#include "tensorflow/core/platform/test.h" +#include "tensorflow/core/protobuf/fingerprint.pb.h" +#include "tsl/platform/statusor.h" + +namespace tensorflow::saved_model::fingerprinting { + +namespace { + +TEST(FingerprintingTest, TestChunkedProto) { + const std::string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), "cc/saved_model/testdata", + "chunked_saved_model/chunked_model"); + TF_ASSERT_OK_AND_ASSIGN(FingerprintDef fingerprint_pb, + CreateFingerprintDef(export_dir)); + EXPECT_GT(fingerprint_pb.saved_model_checksum(), 0); + // We test for multiple fingerprints due to non-determinism when building with + // different compilation_mode flag options. + EXPECT_THAT(absl::flat_hash_set( + {906548630859202535U, 9562420523583756263U}), + ::testing::Contains(fingerprint_pb.graph_def_program_hash())); + EXPECT_EQ(fingerprint_pb.signature_def_hash(), 1043582354059066488U); + EXPECT_THAT(absl::flat_hash_set( + {2766043449526180728U, 11894619660760763927U}), + ::testing::Contains(fingerprint_pb.saved_object_graph_hash())); + EXPECT_EQ(fingerprint_pb.checkpoint_hash(), 0); +} + +} // namespace +} // namespace tensorflow::saved_model::fingerprinting diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_test.cc b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..db1b80d911dccf5532f97c16c7e1c5d2722c9006 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_test.cc @@ -0,0 +1,210 @@ +/* Copyright 2022 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/cc/saved_model/fingerprinting.h" + +#include + +#include +#include "absl/numeric/int128.h" +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/numbers.h" +#include "absl/strings/string_view.h" +#include "tensorflow/core/framework/graph.pb.h" +#include "tensorflow/core/framework/versions.pb.h" +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/platform/path.h" +#include "tensorflow/core/platform/test.h" +#include "tensorflow/core/platform/types.h" +#include "tensorflow/core/protobuf/fingerprint.pb.h" +#include "tensorflow/core/protobuf/saved_model.pb.h" +#include "tsl/platform/statusor.h" + +namespace tensorflow::saved_model::fingerprinting { + +namespace { + +absl::StatusOr ReadSavedModel(absl::string_view file_dir) { + std::string file_path = io::JoinPath(file_dir, "saved_model.pb"); + std::string serialized_saved_model; + auto status = + ReadFileToString(Env::Default(), file_path, &serialized_saved_model); + if (!status.ok()) { + return status; + } + SavedModel saved_model_pb; + saved_model_pb.ParseFromString(serialized_saved_model); + return saved_model_pb; +} + +TEST(FingerprintingTest, TestCreateFingerprint) { + const std::string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), "cc/saved_model/testdata", + "VarsAndArithmeticObjectGraph"); + TF_ASSERT_OK_AND_ASSIGN(SavedModel saved_model_pb, + ReadSavedModel(export_dir)); + TF_ASSERT_OK_AND_ASSIGN(FingerprintDef fingerprint_def, + CreateFingerprintDef(export_dir)); + + EXPECT_GT(fingerprint_def.saved_model_checksum(), 0); + EXPECT_EQ(fingerprint_def.graph_def_program_hash(), 10127142238652115842U); + EXPECT_EQ(fingerprint_def.signature_def_hash(), 15570736222402453744U); + EXPECT_EQ(fingerprint_def.saved_object_graph_hash(), 3678101440349108924U); + + // The uuid is a random number (as string), but it should be a number > 0. + absl::uint128 uuid = 0; + EXPECT_TRUE(absl::SimpleAtoi(fingerprint_def.uuid(), &uuid)) + << "String to Uint128 conversion failed. " + << "UUID from proto, and Uint128Max(): \n" + << fingerprint_def.uuid() << "\n" + << absl::Uint128Max(); + EXPECT_GT(uuid, 0); + + // TODO(b/242348400): The checkpoint hash is non-deterministic, so we cannot + // check its value here. + EXPECT_GT(fingerprint_def.checkpoint_hash(), 0); +} + +TEST(FingerprintingTest, TestCreateFingerprintForPbtxtWorks) { + // This test ensures that we get a minimal fingerprint with an uuid, even + // when the SavedModel cannot be read. + const std::string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), "cc/saved_model/testdata", + "half_plus_two_pbtxt"); + TF_ASSERT_OK_AND_ASSIGN(FingerprintDef fingerprint_def, + CreateFingerprintDef(export_dir)); + + EXPECT_EQ(fingerprint_def.saved_model_checksum(), 0); + EXPECT_EQ(fingerprint_def.graph_def_program_hash(), 0); + EXPECT_EQ(fingerprint_def.signature_def_hash(), 0); + EXPECT_EQ(fingerprint_def.saved_object_graph_hash(), 0); + EXPECT_EQ(fingerprint_def.checkpoint_hash(), 0); + + // The uuid is a random number (as string), but it should be a number > 0. + absl::uint128 uuid = 0; + EXPECT_TRUE(absl::SimpleAtoi(fingerprint_def.uuid(), &uuid)) + << "String to Uint128 conversion failed. " + << "UUID from proto, and Uint128Max(): \n" + << fingerprint_def.uuid() << "\n" + << absl::Uint128Max(); + EXPECT_GT(uuid, 0); + // version().producer()differs between WIN and non-WIN platforms. + EXPECT_GT(fingerprint_def.version().producer(), 3); +} + +// Compare the fingerprints of two models saved by calling +// `tf.saved_model.save` twice in a row in the same program. +TEST(FingerprintingTest, TestCompareFingerprintForTwoModelSavedTwice) { + const std::string export_dir = io::JoinPath( + testing::TensorFlowSrcRoot(), "cc/saved_model/testdata", "bert1"); + + TF_ASSERT_OK_AND_ASSIGN(SavedModel saved_model_pb, + ReadSavedModel(export_dir)); + TF_ASSERT_OK_AND_ASSIGN(FingerprintDef fingerprint_def, + CreateFingerprintDef(export_dir)); + + const std::string export_dir2 = io::JoinPath( + testing::TensorFlowSrcRoot(), "cc/saved_model/testdata", "bert2"); + TF_ASSERT_OK_AND_ASSIGN(SavedModel saved_model_pb2, + ReadSavedModel(export_dir2)); + TF_ASSERT_OK_AND_ASSIGN(FingerprintDef fingerprint_def2, + CreateFingerprintDef(export_dir2)); + + // While the saved_model serialization is deterministic, the model saving and + // proto construction is not. Therefore, we can't compare the two + // fingerprints' saved_model_checksums. + EXPECT_GT(fingerprint_def.saved_model_checksum(), 0); + EXPECT_GT(fingerprint_def2.saved_model_checksum(), 0); + EXPECT_EQ(fingerprint_def.graph_def_program_hash(), + fingerprint_def2.graph_def_program_hash()); + EXPECT_EQ(fingerprint_def.signature_def_hash(), + fingerprint_def2.signature_def_hash()); + EXPECT_EQ(fingerprint_def.saved_object_graph_hash(), + fingerprint_def2.saved_object_graph_hash()); + EXPECT_NE(fingerprint_def.uuid(), fingerprint_def2.uuid()); +} + +TEST(FingerprintingTest, TestFingerprintComputationDoesNotMutateModel) { + const std::string export_dir = io::JoinPath( + testing::TensorFlowSrcRoot(), "cc/saved_model/testdata", "bert1"); + TF_ASSERT_OK_AND_ASSIGN(SavedModel saved_model_pb, + ReadSavedModel(export_dir)); + TF_ASSERT_OK_AND_ASSIGN(FingerprintDef fingerprint_def, + CreateFingerprintDef(export_dir)); + TF_ASSERT_OK_AND_ASSIGN(FingerprintDef fingerprint_def2, + CreateFingerprintDef(export_dir)); + + EXPECT_EQ(fingerprint_def.saved_model_checksum(), + fingerprint_def2.saved_model_checksum()); +} + +TEST(FingerprintingTest, TestFingerprintHasVersion) { + const std::string export_dir = io::JoinPath( + testing::TensorFlowSrcRoot(), "cc/saved_model/testdata", "bert1"); + TF_ASSERT_OK_AND_ASSIGN(SavedModel saved_model_pb, + ReadSavedModel(export_dir)); + TF_ASSERT_OK_AND_ASSIGN(FingerprintDef fingerprint_def, + CreateFingerprintDef(export_dir)); + EXPECT_EQ(fingerprint_def.version().producer(), 4); +} + +TEST(FingerprintingTest, TestHashCheckpointForModelWithNoVariables) { + const std::string export_dir = io::JoinPath( + testing::TensorFlowSrcRoot(), "cc/saved_model/testdata", "bert1"); + TF_ASSERT_OK_AND_ASSIGN(SavedModel saved_model_pb, + ReadSavedModel(export_dir)); + TF_ASSERT_OK_AND_ASSIGN(FingerprintDef fingerprint_def, + CreateFingerprintDef(export_dir)); + EXPECT_EQ(fingerprint_def.checkpoint_hash(), 0); +} + +TEST(FingerprintingTest, TestReadValidFingerprint) { + const std::string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), "cc/saved_model/testdata", + "VarsAndArithmeticObjectGraph"); + TF_ASSERT_OK_AND_ASSIGN(FingerprintDef fingerprint_pb, + ReadSavedModelFingerprint(export_dir)); + EXPECT_EQ(fingerprint_pb.saved_model_checksum(), 15788619162413586750u); +} + +TEST(FingerprintingTest, TestReadNonexistentFingerprint) { + const std::string export_dir = io::JoinPath( + testing::TensorFlowSrcRoot(), "cc/saved_model/testdata", "AssetModule"); + EXPECT_EQ(ReadSavedModelFingerprint(export_dir).status().code(), + absl::StatusCode::kNotFound); +} + +TEST(FingerprintingTest, TestSingleprint) { + const std::string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), "cc/saved_model/testdata", + "VarsAndArithmeticObjectGraph"); + const std::string const_singleprint = + "706963557435316516/5693392539583495303/12074714563970609759/" + "10788359570789890102"; + TF_ASSERT_OK_AND_ASSIGN(std::string singleprint, Singleprint(export_dir)); + EXPECT_EQ(singleprint, const_singleprint); + TF_ASSERT_OK_AND_ASSIGN(FingerprintDef fingerprint_pb, + ReadSavedModelFingerprint(export_dir)); + EXPECT_EQ(Singleprint(fingerprint_pb), const_singleprint); + EXPECT_EQ(Singleprint(fingerprint_pb.graph_def_program_hash(), + fingerprint_pb.signature_def_hash(), + fingerprint_pb.saved_object_graph_hash(), + fingerprint_pb.checkpoint_hash()), + const_singleprint); +} + +} // namespace +} // namespace tensorflow::saved_model::fingerprinting diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_utils.cc b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_utils.cc new file mode 100644 index 0000000000000000000000000000000000000000..c98c5145d43a05f6b0486bb74262ef857c51f3c3 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_utils.cc @@ -0,0 +1,488 @@ +/* Copyright 2023 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/cc/saved_model/fingerprinting_utils.h" + +#include +#include +#include +#include +#include +#include + +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" +#include "riegeli/bytes/fd_reader.h" // from @riegeli +#include "riegeli/records/record_reader.h" // from @riegeli +#include "tensorflow/cc/saved_model/constants.h" +#include "tensorflow/cc/saved_model/fingerprinting_x_platform_utils.h" +#include "tensorflow/core/framework/function.pb.h" +#include "tensorflow/core/framework/graph.pb.h" +#include "tensorflow/core/framework/op_def.pb.h" +#include "tensorflow/core/framework/versions.pb.h" +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/platform/file_system_helper.h" +#include "tensorflow/core/platform/fingerprint.h" +#include "tensorflow/core/platform/path.h" +#include "tensorflow/core/protobuf/fingerprint.pb.h" +#include "tensorflow/core/protobuf/meta_graph.pb.h" +#include "tensorflow/core/protobuf/saved_model.pb.h" +#include "tensorflow/core/protobuf/saved_object_graph.pb.h" +#include "tensorflow/core/util/tensor_bundle/naming.h" +#include "tensorflow/tools/proto_splitter/cc/util.h" +#include "tensorflow/tools/proto_splitter/chunk.pb.h" +#include "tensorflow/tools/proto_splitter/merge.h" +#include "tsl/platform/errors.h" +#include "tsl/platform/statusor.h" +// IWYU pragma: no_include "third_party/protobuf/repeated_ptr_field.h" +// IWYU pragma: no_include "third_party/protobuf/io/coded_stream.h" +// IWYU pragma: no_include "third_party/protobuf/io/zero_copy_stream_impl_lite.h" + +namespace tensorflow::saved_model::fingerprinting { + +using ::tensorflow::proto_splitter::ChunkedField; +using ::tensorflow::proto_splitter::ChunkedMessage; +using ::tensorflow::proto_splitter::ChunkInfo; +using ::tensorflow::proto_splitter::ChunkMetadata; +using ::tensorflow::proto_splitter::FieldIndex; +using tools::proto_splitter::Field; +using tools::proto_splitter::FieldType; +using tools::proto_splitter::GetChunkMetadata; +using tools::proto_splitter::GetFieldTypes; +using tools::proto_splitter::GetMutableField; +using tools::proto_splitter::GetRiegeliReader; +using tools::proto_splitter::Merger; +using tools::proto_splitter::MutableFieldResult; +using tools::proto_splitter::ReadChunk; + +namespace fingerprinting_utils_internal { + +using ::tensorflow::protobuf::Map; +using ::tensorflow::protobuf::Message; +using ::tensorflow::protobuf::RepeatedPtrField; +// NOLINTNEXTLINE: clang-tidy missing-includes false positive +using ::tensorflow::protobuf::io::CodedOutputStream; +// NOLINTNEXTLINE: clang-tidy missing-includes false positive +using ::tensorflow::protobuf::io::StringOutputStream; + +absl::StatusOr fieldTagMatches(const RepeatedPtrField& a, + const RepeatedPtrField& b) { + int matches = 0; + for (int i = 0; i == matches && i < a.size() && i < b.size(); i++) { + switch (b[i].kind_case()) { + case ::tensorflow::proto_splitter::FieldIndex::KindCase::kField: + if (a.at(i).has_field() && a.at(i).field() == b.at(i).field()) { + matches += 1; + } + break; + case ::tensorflow::proto_splitter::FieldIndex::KindCase::kIndex: + if (a.at(i).has_index() && a.at(i).index() == b.at(i).index()) { + matches += 1; + } + break; + case ::tensorflow::proto_splitter::FieldIndex::KindCase::kMapKey: + if (a.at(i).has_map_key()) { + const ::tensorflow::proto_splitter::FieldIndex_MapKey& key = + b.at(i).map_key(); + const ::tensorflow::proto_splitter::FieldIndex_MapKey& chunked_key = + a.at(i).map_key(); + switch (key.type_case()) { + case ::tensorflow::proto_splitter::FieldIndex::MapKey::TypeCase::kS: + if (chunked_key.has_s() && chunked_key.s() == key.s()) { + matches += 1; + } + break; + case ::tensorflow::proto_splitter::FieldIndex::MapKey::TypeCase:: + kBoolean: + if (chunked_key.has_boolean() && + chunked_key.boolean() == key.boolean()) { + matches += 1; + } + break; + case ::tensorflow::proto_splitter::FieldIndex::MapKey::TypeCase:: + kUi32: + if (chunked_key.has_ui32() && chunked_key.ui32() == key.ui32()) { + matches += 1; + } + break; + case ::tensorflow::proto_splitter::FieldIndex::MapKey::TypeCase:: + kUi64: + if (chunked_key.has_ui64() && chunked_key.ui64() == key.ui64()) { + matches += 1; + } + break; + case ::tensorflow::proto_splitter::FieldIndex::MapKey::TypeCase:: + kI32: + if (chunked_key.has_i32() && chunked_key.i32() == key.i32()) { + matches += 1; + } + break; + case ::tensorflow::proto_splitter::FieldIndex::MapKey::TypeCase:: + kI64: + if (chunked_key.has_i64() && chunked_key.i64() == key.i64()) { + matches += 1; + } + break; + case ::tensorflow::proto_splitter::FieldIndex::MapKey::TypeCase:: + TYPE_NOT_SET: + default: + return absl::FailedPreconditionError( + "Encountered unknown field_tag.map_key type."); + } + } + break; + case FieldIndex::KindCase::KIND_NOT_SET: + default: + return absl::FailedPreconditionError( + "Encountered unknown field_tag kind."); + } + } + return matches; +} + +absl::StatusOr<::tensorflow::proto_splitter::ChunkedMessage> +PruneChunkedMessage( + const ::tensorflow::proto_splitter::ChunkedMessage& chunked_message, + riegeli::RecordReader>& reader, + std::vector chunks_info, + std::vector> target_fields_list) { + ::tensorflow::proto_splitter::ChunkedMessage pruned_chunked_message; + if (chunked_message.has_chunk_index()) { + pruned_chunked_message.set_chunk_index(chunked_message.chunk_index()); + } + // For each chunked_field, check if it matches any of the supplied + // target_fields, and copy over the relevant data. + for (const ChunkedField& chunked_field : chunked_message.chunked_fields()) { + for (const auto& target_fields : target_fields_list) { + TF_ASSIGN_OR_RETURN( + int matches, + fieldTagMatches(chunked_field.field_tag(), target_fields)); + if (matches == chunked_field.field_tag_size()) { + // chunked_field_tags is an initial subsequence of target_fields, which + // means the chunked_field is relevant and the necessary data should be + // copied over. + auto cf = std::make_unique(); + cf->mutable_field_tag()->CopyFrom(chunked_field.field_tag()); + TF_ASSIGN_OR_RETURN( + *cf->mutable_message(), + PruneChunkedMessage(chunked_field.message(), reader, chunks_info, + target_fields_list)); + pruned_chunked_message.mutable_chunked_fields()->AddAllocated( + cf.release()); + } + } + } + return pruned_chunked_message; +} + +std::string SerializeProto(const Message& message) { + std::string serialized_message; + { + // local scope guarantees coded stream will be trimmed (ensures determinism) + StringOutputStream stream(&serialized_message); + CodedOutputStream output(&stream); + output.SetSerializationDeterministic(true); + message.SerializeToCodedStream(&output); + } + return serialized_message; +} + +absl::StatusOr HashFields( + const ChunkedMessage& chunked_message, + riegeli::RecordReader>& reader, + const std::vector& chunks_info, + const RepeatedPtrField& field_tags, Message* merged_message) { + uint64_t field_checksum = 0; + // Find chunked_fields that match the field_tags. + for (const ChunkedField& chunked_field : chunked_message.chunked_fields()) { + const RepeatedPtrField chunked_field_tags = + chunked_field.field_tag(); + const ChunkedMessage& chunked_message = chunked_field.message(); + // Number of sequential field_tag matches. + TF_ASSIGN_OR_RETURN(int matches, + fieldTagMatches(chunked_field_tags, field_tags)); + + if (chunked_message.has_chunk_index() && matches == field_tags.size()) { + // chunked_field_tags are an exact match with field_tags. Hash referenced + // chunk. + TF_ASSIGN_OR_RETURN( + std::string chunk, + ReadChunk(reader, chunks_info[chunked_message.chunk_index()])); + field_checksum = FingerprintCat64(field_checksum, Fingerprint64(chunk)); + } else if (matches == field_tags.size()) { + // chunked_field_tags are an exact match, but chunked_field is further + // broken down into separate chunked_fields (no chunk_index). Hash those + // chunked_fields. + TF_ASSIGN_OR_RETURN(uint64_t hash, + HashFields(chunked_message, reader, chunks_info, + field_tags, merged_message)); + field_checksum = FingerprintCat64(field_checksum, hash); + } else if (chunked_message.has_chunk_index() && + matches == chunked_field_tags.size()) { + // chunked_field_tags are a partial match (an initial segment/subsequence + // of field_tags). Merge chunk in, attempt to locate & hash the target + // field by recursing. + TF_ASSIGN_OR_RETURN(std::vector fields, + GetFieldTypes(chunked_field_tags)); + for (const auto& field : fields) { + TF_ASSIGN_OR_RETURN(MutableFieldResult mfr, + GetMutableField(merged_message, field)); + merged_message = + mfr.parent->GetReflection()->MutableMessage(mfr.parent, mfr.field); + } + TF_ASSIGN_OR_RETURN( + std::string chunk, + ReadChunk(reader, chunks_info[chunked_message.chunk_index()])); + merged_message->ParseFromString(chunk); + TF_ASSIGN_OR_RETURN(uint64_t hash, + HashFields(chunked_message, reader, chunks_info, + field_tags, merged_message)); + field_checksum = FingerprintCat64(field_checksum, hash); + } else if (matches == chunked_field_tags.size()) { + // chunk_field_tags are a partial match, but chunked_field is broken down. + // Merge chunked_fields in, attempt to locate & hash target field. + for (const ChunkedField& cf : chunked_message.chunked_fields()) { + TF_ASSIGN_OR_RETURN(uint64_t hash, + HashFields(cf.message(), reader, chunks_info, + field_tags, merged_message)); + field_checksum = FingerprintCat64(field_checksum, hash); + } + } + } + return field_checksum; +} + +inline RepeatedPtrField GraphDefFieldTags() { + // SavedModel.meta_graphs[0].graph_def + FieldIndex meta_graph_field_tag; + meta_graph_field_tag.set_field(2); + FieldIndex meta_graph_index_field_tag; + meta_graph_index_field_tag.set_index(0); + FieldIndex graph_def_field_tag; + graph_def_field_tag.set_field(2); + RepeatedPtrField graph_def_field_tags; + graph_def_field_tags.Add(FieldIndex(meta_graph_field_tag)); + graph_def_field_tags.Add(FieldIndex(meta_graph_index_field_tag)); + graph_def_field_tags.Add(FieldIndex(graph_def_field_tag)); + + return graph_def_field_tags; +} + +inline RepeatedPtrField SignatureDefFieldTags() { + // SavedModel.meta_graphs[0].signature_def + FieldIndex meta_graph_field_tag; + meta_graph_field_tag.set_field(2); + FieldIndex meta_graph_index_field_tag; + meta_graph_index_field_tag.set_index(0); + FieldIndex signature_def_field_tag; + signature_def_field_tag.set_field(5); + RepeatedPtrField signature_def_field_tags; + signature_def_field_tags.Add(FieldIndex(meta_graph_field_tag)); + signature_def_field_tags.Add(FieldIndex(meta_graph_index_field_tag)); + signature_def_field_tags.Add(FieldIndex(signature_def_field_tag)); + + return signature_def_field_tags; +} + +inline RepeatedPtrField SavedObjectGraphFieldTags() { + // SavedModel.meta_graphs[0].object_graph_def + FieldIndex meta_graph_field_tag; + meta_graph_field_tag.set_field(2); + FieldIndex meta_graph_index_field_tag; + meta_graph_index_field_tag.set_index(0); + FieldIndex saved_object_graph_field_tag; + saved_object_graph_field_tag.set_field(7); + RepeatedPtrField saved_object_graph_field_tags; + saved_object_graph_field_tags.Add(FieldIndex(meta_graph_field_tag)); + saved_object_graph_field_tags.Add(FieldIndex(meta_graph_index_field_tag)); + saved_object_graph_field_tags.Add(FieldIndex(saved_object_graph_field_tag)); + + return saved_object_graph_field_tags; +} + +absl::StatusOr PrunedSavedModel( + absl::string_view export_dir, + riegeli::RecordReader>& reader, + const std::vector& chunks_info, ChunkMetadata& chunk_metadata) { + SavedModel saved_model; + ChunkMetadata pruned_chunk_metadata; + pruned_chunk_metadata.mutable_chunks()->CopyFrom(chunk_metadata.chunks()); + TF_ASSIGN_OR_RETURN( + *pruned_chunk_metadata.mutable_message(), + PruneChunkedMessage(chunk_metadata.message(), reader, chunks_info, + {GraphDefFieldTags(), SignatureDefFieldTags(), + SavedObjectGraphFieldTags()})); + // Read into saved_model. + TF_RETURN_IF_ERROR( + Merger::ReadPartial(io::JoinPath(export_dir, kSavedModelFilenamePrefix), + pruned_chunk_metadata, &saved_model)); + return saved_model; +} + +absl::StatusOr HashMessage( + Message* message, const ChunkedMessage& chunked_message, + riegeli::RecordReader>& reader, + const std::vector& chunks_info, + const RepeatedPtrField& field_tags) { + uint64_t total_message_hash = Fingerprint64(SerializeProto(*message)); + TF_ASSIGN_OR_RETURN( + uint64_t message_hash, + HashFields(chunked_message, reader, chunks_info, field_tags, message)); + return FingerprintCat64(total_message_hash, message_hash); +} + +absl::StatusOr HashGraphDef( + ::tensorflow::GraphDef* graph_def, const ChunkedMessage& chunked_message, + riegeli::RecordReader>& reader, + const std::vector& chunks_info) { + // TODO(adamcogdell): here we assume that graph_def (top-level) is contained + // in a single chunk, which may not be the case + return HashMessage(graph_def, chunked_message, reader, chunks_info, + GraphDefFieldTags()); +} + +absl::StatusOr HashSignatureDef( + const Map& signature_def_map, + const ChunkedMessage& chunked_message, + riegeli::RecordReader>& reader, + const std::vector& chunks_info) { + uint64_t signature_def_hash = 0; + std::vector> + signature_def_sorted(signature_def_map.begin(), signature_def_map.end()); + std::sort(signature_def_sorted.begin(), signature_def_sorted.end(), + [](const std::pair& a, + const std::pair& b) { + return a.first < b.first; + }); + for (const auto& signature_def : signature_def_sorted) { + uint64_t signature_def_pair_hash = + FingerprintCat64(Fingerprint64(signature_def.first), + Fingerprint64(SerializeProto(signature_def.second))); + signature_def_hash = + FingerprintCat64(signature_def_hash, signature_def_pair_hash); + SignatureDef signature_def_val = signature_def.second; + TF_ASSIGN_OR_RETURN( + uint64_t signature_def_entry_hash, + HashFields(chunked_message, reader, chunks_info, + SignatureDefFieldTags(), &signature_def_val)); + signature_def_hash = + FingerprintCat64(signature_def_hash, signature_def_entry_hash); + } + return signature_def_hash; +} + +absl::StatusOr HashSavedObjectGraph( + ::tensorflow::SavedObjectGraph* saved_object_graph, + const ChunkedMessage& chunked_message, + riegeli::RecordReader>& reader, + const std::vector& chunks_info) { + return HashMessage(saved_object_graph, chunked_message, reader, chunks_info, + SavedObjectGraphFieldTags()); +} + +} // namespace fingerprinting_utils_internal + +using fingerprinting_utils_internal::HashFields; +using fingerprinting_utils_internal::HashGraphDef; +using fingerprinting_utils_internal::HashSavedObjectGraph; +using fingerprinting_utils_internal::HashSignatureDef; +using fingerprinting_utils_internal::PrunedSavedModel; +using fingerprinting_utils_internal::SerializeProto; + +uint64_t HashCheckpointIndexFile(absl::string_view model_dir) { + std::string meta_filename = MetaFilename(io::JoinPath( + model_dir, kSavedModelVariablesDirectory, kSavedModelVariablesFilename)); + std::string data; + absl::Status read_status = + ReadFileToString(Env::Default(), meta_filename, &data); + if (read_status.ok()) { + return tensorflow::Fingerprint64(data); + } else { + return 0; + } +} + +absl::StatusOr CreateFingerprintDefCpb( + absl::string_view export_dir, std::string cpb_file) { + // Version of the code that produced the fingerprint. + const int kFingerprintProducer = 2; + + TF_ASSIGN_OR_RETURN(auto reader, GetRiegeliReader(cpb_file)); + + auto read_metadata = GetChunkMetadata(reader); + if (!read_metadata.ok()) { + reader.Close(); + return absl::FailedPreconditionError( + absl::StrCat("Couldn't read ChunkMetadata from chunked proto.\n", + read_metadata.status().ToString())); + } + ChunkMetadata chunk_metadata = read_metadata.value(); + + std::vector chunks_info = std::vector( + chunk_metadata.chunks().begin(), chunk_metadata.chunks().end()); + + FingerprintDef fingerprint_def; + SavedModel saved_model; + + // Set the saved_model_checksum. + TF_ASSIGN_OR_RETURN(uint64_t saved_model_hash, + HashFields(chunk_metadata.message(), reader, chunks_info, + {}, &saved_model)); + saved_model_hash = FingerprintCat64( + saved_model_hash, Fingerprint64(SerializeProto(saved_model))); + fingerprint_def.set_saved_model_checksum(saved_model_hash); + + // Fill saved_model with only relevant chunk(s). + TF_ASSIGN_OR_RETURN( + saved_model, + PrunedSavedModel(export_dir, reader, chunks_info, chunk_metadata)); + + TF_ASSIGN_OR_RETURN( + uint64_t graph_def_program_hash, + HashGraphDef(saved_model.mutable_meta_graphs(0)->mutable_graph_def(), + chunk_metadata.message(), reader, chunks_info)); + fingerprint_def.set_graph_def_program_hash(graph_def_program_hash); + + // TODO(adamcogdell): HashSignatureDef relies on the signatue_def map being + // populated with all of its entries, which may not be the case + TF_ASSIGN_OR_RETURN( + uint64_t signature_def_hash, + HashSignatureDef(saved_model.meta_graphs(0).signature_def(), + chunk_metadata.message(), reader, chunks_info)); + fingerprint_def.set_signature_def_hash(signature_def_hash); + + TF_ASSIGN_OR_RETURN( + uint64_t saved_object_graph_hash, + HashSavedObjectGraph( + saved_model.mutable_meta_graphs(0)->mutable_object_graph_def(), + chunk_metadata.message(), reader, chunks_info)); + fingerprint_def.set_saved_object_graph_hash(saved_object_graph_hash); + + fingerprint_def.set_checkpoint_hash(HashCheckpointIndexFile(export_dir)); + + // Assign a random UUID to the fingerprint. + fingerprint_def.set_uuid(fingerprinting::CreateRandomUUID()); + reader.Close(); + + // Set version of the fingerprint. + VersionDef* version = fingerprint_def.mutable_version(); + version->set_producer(kFingerprintProducer); + + return fingerprint_def; +} + +} // namespace tensorflow::saved_model::fingerprinting diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_utils.h b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..3700dd799b12ed33d45784840b3b629b60c1c3d5 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_utils.h @@ -0,0 +1,137 @@ +/* Copyright 2023 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. +==============================================================================*/ + +#ifndef TENSORFLOW_CC_SAVED_MODEL_FINGERPRINTING_UTILS_H_ +#define TENSORFLOW_CC_SAVED_MODEL_FINGERPRINTING_UTILS_H_ + +#include +#include +#include + +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" +#include "riegeli/bytes/fd_reader.h" // from @riegeli +#include "riegeli/records/record_reader.h" // from @riegeli +#include "tensorflow/core/framework/graph.pb.h" +#include "tensorflow/core/platform/protobuf.h" // IWYU pragma: keep +#include "tensorflow/core/protobuf/fingerprint.pb.h" +#include "tensorflow/core/protobuf/meta_graph.pb.h" +#include "tensorflow/core/protobuf/saved_model.pb.h" +#include "tensorflow/core/protobuf/saved_object_graph.pb.h" +#include "tensorflow/tools/proto_splitter/chunk.pb.h" + +namespace tensorflow::saved_model::fingerprinting { + +namespace fingerprinting_utils_internal { + +using ::tensorflow::protobuf::Map; +using ::tensorflow::protobuf::Message; +using ::tensorflow::protobuf::RepeatedPtrField; + +// Number of sequential FieldIndex matches of `a` in `b`. (Length of initial +// subsequence.) +// Example: `a = {4, 2}`, `b = {4, 2, 1, 3}`, `fieldTagMatches(a, b) == 2` +absl::StatusOr fieldTagMatches( + const RepeatedPtrField<::tensorflow::proto_splitter::FieldIndex>& a, + const RepeatedPtrField<::tensorflow::proto_splitter::FieldIndex>& b); + +// Pull out the relevant data within `chunked_message`. A `chunked_field` is +// relevant if its `field_tags` are an initial subsequence any of the +// `target_fields` in the provided `target_fields_list`. +absl::StatusOr<::tensorflow::proto_splitter::ChunkedMessage> +PruneChunkedMessage( + const ::tensorflow::proto_splitter::ChunkedMessage& chunked_message, + riegeli::RecordReader>& reader, + std::vector<::tensorflow::proto_splitter::ChunkInfo> chunks_info, + std::vector> + target_fields_list); + +// Deterministically serializes the proto `message`. +std::string SerializeProto(const Message& message); + +// Uses metadata contained in `chunked_message` to hash fields within the +// data accessed by the `reader` using `chunks_info`. +absl::StatusOr HashFields( + const ::tensorflow::proto_splitter::ChunkedMessage& chunked_message, + riegeli::RecordReader>& reader, + const std::vector<::tensorflow::proto_splitter::ChunkInfo>& chunks_info, + const RepeatedPtrField<::tensorflow::proto_splitter::FieldIndex>& + field_tags, + Message* merged_message); + +// Gets the field tags for `graph_def`.::tensorflow +inline RepeatedPtrField<::tensorflow::proto_splitter::FieldIndex> +GraphDefFieldTags(); + +// Gets the field tags for `signature_def`. +inline RepeatedPtrField<::tensorflow::proto_splitter::FieldIndex> +SignatureDefFieldTags(); + +// Gets the field tags for `saved_object_graph`. +inline RepeatedPtrField<::tensorflow::proto_splitter::FieldIndex> +SavedObjectGraphFieldTags(); + +// Returns a `SavedModel` containing only fields (up to those) specified by +// `GraphDefFieldTags()`, `SignatureDefFieldTags()`, and +// `SavedObjectGraphFieldTags()`. +absl::StatusOr PrunedSavedModel( + absl::string_view export_dir, + riegeli::RecordReader>& reader, + const std::vector<::tensorflow::proto_splitter::ChunkInfo>& chunks_info, + ::tensorflow::proto_splitter::ChunkMetadata& chunk_metadata); + +// Hashes the contents of `message` specified by `field_tags`. +absl::StatusOr HashMessage( + Message* message, + const ::tensorflow::proto_splitter::ChunkedMessage& chunked_message, + riegeli::RecordReader>& reader, + const std::vector<::tensorflow::proto_splitter::ChunkInfo>& chunks_info, + const RepeatedPtrField<::tensorflow::proto_splitter::FieldIndex>& + field_tags); + +// Hashes the contents of `graph_def`. +absl::StatusOr HashGraphDef( + tensorflow::GraphDef* graph_def, + const ::tensorflow::proto_splitter::ChunkedMessage& chunked_message, + riegeli::RecordReader>& reader, + const std::vector<::tensorflow::proto_splitter::ChunkInfo>& chunks_info); + +// Hashes the contents of `signature_def`. +absl::StatusOr HashSignatureDef( + const Map& signature_def_map, + const ::tensorflow::proto_splitter::ChunkedMessage& chunked_message, + riegeli::RecordReader>& reader, + const std::vector<::tensorflow::proto_splitter::ChunkInfo>& chunks_info); + +// Hashes the contents of `saved_object_graph`. +absl::StatusOr HashSavedObjectGraph( + tensorflow::SavedObjectGraph* saved_object_graph, + const ::tensorflow::proto_splitter::ChunkedMessage& chunked_message, + riegeli::RecordReader>& reader, + const std::vector<::tensorflow::proto_splitter::ChunkInfo>& chunks_info); + +} // namespace fingerprinting_utils_internal + +// Returns the hash of the checkpoint .index file, 0 if there is none. +uint64_t HashCheckpointIndexFile(absl::string_view model_dir); + +// Creates a FingerprintDef proto from a chunked SavedModel and the checkpoint +// meta file (.index) in `export_dir`. +absl::StatusOr CreateFingerprintDefCpb( + absl::string_view export_dir, std::string cpb_file); + +} // namespace tensorflow::saved_model::fingerprinting + +#endif // TENSORFLOW_CC_SAVED_MODEL_FINGERPRINTING_UTILS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_utils_test.cc b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_utils_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..ea00d86940fa96e6e2dfe1f93fc811402812ee12 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_utils_test.cc @@ -0,0 +1,396 @@ +/* Copyright 2023 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/cc/saved_model/fingerprinting_utils.h" + +#include +#include +#include + +#include +#include +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" +#include "xla/tsl/lib/core/status_test_util.h" +#include "tensorflow/core/framework/graph.pb.h" +#include "tensorflow/core/platform/path.h" +#include "tensorflow/core/protobuf/meta_graph.pb.h" +#include "tensorflow/core/protobuf/saved_object_graph.pb.h" +#include "tensorflow/tools/proto_splitter/cc/util.h" +#include "tensorflow/tools/proto_splitter/chunk.pb.h" +#include "tensorflow/tools/proto_splitter/testdata/test_message.pb.h" +#include "tsl/platform/errors.h" +#include "tsl/platform/status_matchers.h" +#include "tsl/platform/statusor.h" +#include "tsl/platform/test.h" +// IWYU pragma: no_include "third_party/protobuf/io/zero_copy_stream_impl_lite.h" +// IWYU pragma: no_include "third_party/protobuf/util/message_differencer.h" + +namespace tensorflow::saved_model::fingerprinting { + +namespace { + +using fingerprinting_utils_internal::fieldTagMatches; +using fingerprinting_utils_internal::HashFields; +using fingerprinting_utils_internal::HashGraphDef; +using fingerprinting_utils_internal::HashSavedObjectGraph; +using fingerprinting_utils_internal::HashSignatureDef; +using fingerprinting_utils_internal::PruneChunkedMessage; +using fingerprinting_utils_internal::SerializeProto; +using ::tensorflow::proto_splitter::ChunkedField; +using ::tensorflow::proto_splitter::ChunkedMessage; +using ::tensorflow::proto_splitter::ChunkInfo; +using ::tensorflow::proto_splitter::ChunkMetadata; +using ::tensorflow::proto_splitter::FieldIndex; +using ::tensorflow::proto_splitter_testdata::ManyFields; +using ::tensorflow::protobuf::Message; +using ::tensorflow::protobuf::RepeatedPtrField; +using ::tensorflow::protobuf::TextFormat; +// NOLINTNEXTLINE: clang-tidy missing-includes false positive +using ::tensorflow::protobuf::io::ArrayInputStream; +// NOLINTNEXTLINE: clang-tidy missing-includes false positive +using ::tensorflow::protobuf::util::MessageDifferencer; +using tools::proto_splitter::GetChunkMetadata; +using tools::proto_splitter::GetRiegeliReader; +using tsl::testing::IsOkAndHolds; +using tsl::testing::TensorFlowSrcRoot; + +absl::Status ParseTextProto(absl::string_view text_proto, + Message* parsed_proto) { + TextFormat::Parser parser; + // Attempt to parse as text. + ArrayInputStream input_stream(text_proto.data(), text_proto.size()); + if (parser.Parse(&input_stream, parsed_proto)) { + return absl::OkStatus(); + } + parsed_proto->Clear(); + return absl::InvalidArgumentError( + absl::StrCat("Could not parse text proto: ", text_proto)); +} + +absl::StatusOr> +ExtractFieldTags(absl::string_view chunked_field_text_proto) { + ChunkedField chunked_field; + TF_RETURN_IF_ERROR(ParseTextProto(chunked_field_text_proto, &chunked_field)); + return chunked_field.field_tag(); +} + +TEST(FingerprintingTest, TestFieldTagMatchesInitialSubsequence) { + TF_ASSERT_OK_AND_ASSIGN(RepeatedPtrField field_tags, + ExtractFieldTags(R"pb( + field_tag { field: 2 } + field_tag { index: 1505 } + field_tag { field: 5 } + field_tag { map_key { ui32: 123 } } + )pb")); + RepeatedPtrField field_tags_sub; + field_tags_sub.CopyFrom(field_tags); + field_tags_sub.DeleteSubrange(2, 2); + + EXPECT_THAT(fieldTagMatches(field_tags_sub, field_tags), + absl_testing::IsOkAndHolds(2)); +} + +TEST(FingerprintingTest, TestFieldTagMatchesNoninitialSubsequence) { + TF_ASSERT_OK_AND_ASSIGN(RepeatedPtrField field_tags, + ExtractFieldTags(R"pb( + field_tag { field: 2 } + field_tag { index: 1505 } + field_tag { field: 5 } + field_tag { map_key { ui32: 123 } } + )pb")); + RepeatedPtrField field_tags_sub; + field_tags_sub.CopyFrom(field_tags); + field_tags_sub.DeleteSubrange(0, 2); + + EXPECT_THAT(fieldTagMatches(field_tags_sub, field_tags), + absl_testing::IsOkAndHolds(0)); +} + +TEST(FingerprintingTest, TestFieldTagMatchesIdenticalSubsequence) { + TF_ASSERT_OK_AND_ASSIGN(RepeatedPtrField field_tags, + ExtractFieldTags(R"pb( + field_tag { field: 2 } + field_tag { index: 1505 } + field_tag { field: 5 } + field_tag { map_key { ui32: 123 } } + )pb")); + RepeatedPtrField field_tags_sub; + field_tags_sub.CopyFrom(field_tags); + + EXPECT_THAT(fieldTagMatches(field_tags_sub, field_tags), + absl_testing::IsOkAndHolds(4)); +} + +TEST(FingerprintingTest, TestFieldTagMatchesSuperSubsequence) { + TF_ASSERT_OK_AND_ASSIGN(RepeatedPtrField field_tags, + ExtractFieldTags(R"pb( + field_tag { field: 2 } + field_tag { index: 1505 } + field_tag { field: 5 } + field_tag { map_key { ui32: 123 } } + )pb")); + RepeatedPtrField field_tags_sub; + field_tags_sub.CopyFrom(field_tags); + field_tags_sub.Add()->set_field(6); + + EXPECT_THAT(fieldTagMatches(field_tags_sub, field_tags), + absl_testing::IsOkAndHolds(4)); +} + +TEST(FingerprintingTest, TestPruneChunkedMessageSingleTarget) { + std::string cpb_file = io::JoinPath( + TensorFlowSrcRoot(), "tools/proto_splitter/testdata", "many-field.cpb"); + TF_ASSERT_OK_AND_ASSIGN(auto reader, GetRiegeliReader(cpb_file)); + + auto read_metadata = GetChunkMetadata(reader); + if (!read_metadata.ok()) { + reader.Close(); + TF_ASSERT_OK(read_metadata.status()); + } + ChunkMetadata chunk_metadata = read_metadata.value(); + + std::vector chunks_info = std::vector( + chunk_metadata.chunks().begin(), chunk_metadata.chunks().end()); + + FieldIndex field_one_field_tag; + field_one_field_tag.set_field(1); + FieldIndex repeated_field_field_tag; + repeated_field_field_tag.set_field(2); + FieldIndex repeated_field_index_field_tag; + repeated_field_index_field_tag.set_index(1); + RepeatedPtrField target_field_tags; + target_field_tags.Add(FieldIndex(field_one_field_tag)); + target_field_tags.Add(FieldIndex(repeated_field_field_tag)); + target_field_tags.Add(FieldIndex(repeated_field_index_field_tag)); + + ChunkedMessage pruned_chunked_message; + TF_ASSERT_OK_AND_ASSIGN( + pruned_chunked_message, + PruneChunkedMessage(chunk_metadata.message(), reader, chunks_info, + {target_field_tags})); + + std::string expected_pruned_chunked_message_text_proto = R"pb( + chunk_index: 0 + chunked_fields { + field_tag { field: 1 } + message { chunk_index: 1 } + } + )pb"; + ChunkedMessage expected_pruned_chunked_message; + TF_ASSERT_OK(ParseTextProto(expected_pruned_chunked_message_text_proto, + &expected_pruned_chunked_message)); + ASSERT_TRUE(MessageDifferencer::Equals(pruned_chunked_message, + expected_pruned_chunked_message)); +} + +TEST(FingerprintingTest, TestPruneChunkedMessageMultiTarget) { + std::string cpb_file = io::JoinPath( + TensorFlowSrcRoot(), "tools/proto_splitter/testdata", "many-field.cpb"); + TF_ASSERT_OK_AND_ASSIGN(auto reader, GetRiegeliReader(cpb_file)); + + auto read_metadata = GetChunkMetadata(reader); + if (!read_metadata.ok()) { + reader.Close(); + TF_ASSERT_OK(read_metadata.status()); + } + ChunkMetadata chunk_metadata = read_metadata.value(); + + std::vector chunks_info = std::vector( + chunk_metadata.chunks().begin(), chunk_metadata.chunks().end()); + + // ManyFields.field_one.repeated_field[1] + FieldIndex field_one_field_tag; + field_one_field_tag.set_field(1); + FieldIndex repeated_field_field_tag; + repeated_field_field_tag.set_field(2); + FieldIndex repeated_field_index_field_tag; + repeated_field_index_field_tag.set_index(1); + RepeatedPtrField target_one_field_tags; + target_one_field_tags.Add(FieldIndex(field_one_field_tag)); + target_one_field_tags.Add(FieldIndex(repeated_field_field_tag)); + target_one_field_tags.Add(FieldIndex(repeated_field_index_field_tag)); + + // ManyFields.nested_map_bool[true].string_field + FieldIndex nested_map_bool_field_tag; + nested_map_bool_field_tag.set_field(7); + FieldIndex nested_map_bool_mapkey_field_tag; + nested_map_bool_mapkey_field_tag.mutable_map_key()->set_boolean(true); + FieldIndex string_field_field_tag; + string_field_field_tag.set_field(3); + RepeatedPtrField target_two_field_tags; + target_two_field_tags.Add(FieldIndex(nested_map_bool_field_tag)); + target_two_field_tags.Add(FieldIndex(nested_map_bool_mapkey_field_tag)); + target_two_field_tags.Add(FieldIndex(string_field_field_tag)); + + ChunkedMessage pruned_chunked_message; + TF_ASSERT_OK_AND_ASSIGN( + pruned_chunked_message, + PruneChunkedMessage(chunk_metadata.message(), reader, chunks_info, + {target_one_field_tags, target_two_field_tags})); + + std::string expected_pruned_chunked_message_text_proto = R"pb( + chunk_index: 0 + chunked_fields { + field_tag { field: 1 } + message { chunk_index: 1 } + } + chunked_fields { + field_tag { field: 7 } + field_tag { map_key { boolean: true } } + message { chunk_index: 2 } + } + )pb"; + ChunkedMessage expected_pruned_chunked_message; + TF_ASSERT_OK(ParseTextProto(expected_pruned_chunked_message_text_proto, + &expected_pruned_chunked_message)); + ASSERT_TRUE(MessageDifferencer::Equals(pruned_chunked_message, + expected_pruned_chunked_message)); +} + +TEST(FingerprintingTest, TestPruneChunkedMessageNoTarget) { + std::string cpb_file = io::JoinPath( + TensorFlowSrcRoot(), "tools/proto_splitter/testdata", "many-field.cpb"); + TF_ASSERT_OK_AND_ASSIGN(auto reader, GetRiegeliReader(cpb_file)); + + auto read_metadata = GetChunkMetadata(reader); + if (!read_metadata.ok()) { + reader.Close(); + TF_ASSERT_OK(read_metadata.status()); + } + ChunkMetadata chunk_metadata = read_metadata.value(); + + std::vector chunks_info = std::vector( + chunk_metadata.chunks().begin(), chunk_metadata.chunks().end()); + + ChunkedMessage pruned_chunked_message; + TF_ASSERT_OK_AND_ASSIGN( + pruned_chunked_message, + PruneChunkedMessage(chunk_metadata.message(), reader, chunks_info, {})); + + std::string expected_pruned_chunked_message_text_proto = R"pb( + chunk_index: 0 + )pb"; + ChunkedMessage expected_pruned_chunked_message; + TF_ASSERT_OK(ParseTextProto(expected_pruned_chunked_message_text_proto, + &expected_pruned_chunked_message)); + ASSERT_TRUE(MessageDifferencer::Equals(pruned_chunked_message, + expected_pruned_chunked_message)); +} + +TEST(FingerprintingTest, TestSerializeProto) { + std::string many_fields_text_proto = R"pb( + string_field: "abc123" + )pb"; + ManyFields many_fields; + TF_ASSERT_OK(ParseTextProto(many_fields_text_proto, &many_fields)); + ASSERT_EQ(SerializeProto(many_fields), many_fields.SerializeAsString()); +} + +TEST(FingerprintingTest, TestHashFieldsV2) { + std::string cpb_file = io::JoinPath( + TensorFlowSrcRoot(), "tools/proto_splitter/testdata", "many-field.cpb"); + TF_ASSERT_OK_AND_ASSIGN(auto reader, GetRiegeliReader(cpb_file)); + + auto read_metadata = GetChunkMetadata(reader); + if (!read_metadata.ok()) { + reader.Close(); + TF_ASSERT_OK(read_metadata.status()); + } + ChunkMetadata chunk_metadata = read_metadata.value(); + + std::vector chunks_info = std::vector( + chunk_metadata.chunks().begin(), chunk_metadata.chunks().end()); + + ManyFields many_fields; + TF_ASSERT_OK_AND_ASSIGN(uint64_t many_fields_hash, + HashFields(chunk_metadata.message(), reader, + chunks_info, {}, &many_fields)); + ASSERT_EQ(many_fields_hash, 14850154939410192811U); +} + +TEST(FingerprintingTest, TestHashGraphDef) { + std::string cpb_file = + io::JoinPath(TensorFlowSrcRoot(), "tools/proto_splitter/testdata", + "split-standard.cpb"); + TF_ASSERT_OK_AND_ASSIGN(auto reader, GetRiegeliReader(cpb_file)); + + auto read_metadata = GetChunkMetadata(reader); + if (!read_metadata.ok()) { + reader.Close(); + TF_ASSERT_OK(read_metadata.status()); + } + ChunkMetadata chunk_metadata = read_metadata.value(); + + std::vector chunks_info = std::vector( + chunk_metadata.chunks().begin(), chunk_metadata.chunks().end()); + + GraphDef graph_def; + EXPECT_THAT( + HashGraphDef(&graph_def, chunk_metadata.message(), reader, chunks_info), + absl_testing::IsOkAndHolds(16782272393894422524U)); +} + +TEST(FingerprintingTest, TestHashSignatureDef) { + std::string cpb_file = + io::JoinPath(TensorFlowSrcRoot(), "tools/proto_splitter/testdata", + "split-standard.cpb"); + TF_ASSERT_OK_AND_ASSIGN(auto reader, GetRiegeliReader(cpb_file)); + + auto read_metadata = GetChunkMetadata(reader); + if (!read_metadata.ok()) { + reader.Close(); + TF_ASSERT_OK(read_metadata.status()); + } + ChunkMetadata chunk_metadata = read_metadata.value(); + + std::vector chunks_info = std::vector( + chunk_metadata.chunks().begin(), chunk_metadata.chunks().end()); + + ::tensorflow::protobuf::Map signature_def_map; + SignatureDef signature_def; + EXPECT_THAT(HashSignatureDef(signature_def_map, chunk_metadata.message(), + reader, chunks_info), + absl_testing::IsOkAndHolds(0)); +} + +TEST(FingerprintingTest, TestHashSavedObjectGraph) { + std::string cpb_file = + io::JoinPath(TensorFlowSrcRoot(), "tools/proto_splitter/testdata", + "split-standard.cpb"); + TF_ASSERT_OK_AND_ASSIGN(auto reader, GetRiegeliReader(cpb_file)); + + auto read_metadata = GetChunkMetadata(reader); + if (!read_metadata.ok()) { + reader.Close(); + TF_ASSERT_OK(read_metadata.status()); + } + ChunkMetadata chunk_metadata = read_metadata.value(); + + std::vector chunks_info = std::vector( + chunk_metadata.chunks().begin(), chunk_metadata.chunks().end()); + + SavedObjectGraph saved_object_graph; + EXPECT_THAT( + HashSavedObjectGraph(&saved_object_graph, chunk_metadata.message(), + reader, chunks_info), + absl_testing::IsOkAndHolds(17454850744699451884U)); +} + +} // namespace + +} // namespace tensorflow::saved_model::fingerprinting diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_x_platform_utils.cc b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_x_platform_utils.cc new file mode 100644 index 0000000000000000000000000000000000000000..7662378861653e562a0b34ea45b48769be5b3b29 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_x_platform_utils.cc @@ -0,0 +1,36 @@ +/* Copyright 2023 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/cc/saved_model/fingerprinting_x_platform_utils.h" + +#include + +#include "absl/numeric/int128.h" +#include "absl/strings/str_format.h" +#include "tsl/platform/random.h" + +// UINT64MAX is 18'446'744'073'709'551'615 (20 digits) +// UINT128MAX is 340'282'366'920'938'463'463'374'607'431'768'211'455 (39 dgts) +// After sqrt(INT64MAX) = 4'294'967'296 (4B models), it's 50% likely to be +// duplicates in the ID space. In comparison, sqrt(UINT128MAX) = UINT64MAX, +// meaning that we can continue generating unique IDs for a lot longer time +// if the UUID is generated from two random UINT64s. This can be replaced by +// random::New128() if that becomes available. +std::string tensorflow::saved_model::fingerprinting::CreateRandomUUID() { + absl::uint128 uuid_1 = tsl::random::New64(); + absl::uint128 uuid_2 = tsl::random::New64(); + absl::uint128 uuid_complete = (uuid_1 << 64) | uuid_2; + return absl::StrFormat("%020d", uuid_complete); +} diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_x_platform_utils.h b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_x_platform_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..6c1eb4069e396442c6e669a209ac0a882ecffe33 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_x_platform_utils.h @@ -0,0 +1,28 @@ +/* Copyright 2023 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. +==============================================================================*/ + +#ifndef TENSORFLOW_CC_SAVED_MODEL_FINGERPRINTING_X_PLATFORM_UTILS_H_ +#define TENSORFLOW_CC_SAVED_MODEL_FINGERPRINTING_X_PLATFORM_UTILS_H_ + +#include + +namespace tensorflow::saved_model::fingerprinting { + +// Returns a random UUID (128 bits random) as a string. +std::string CreateRandomUUID(); + +} // namespace tensorflow::saved_model::fingerprinting + +#endif // TENSORFLOW_CC_SAVED_MODEL_FINGERPRINTING_X_PLATFORM_UTILS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/image_format/BUILD b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/image_format/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..217a6748f9b343a027108f08be51db114252ec6a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/image_format/BUILD @@ -0,0 +1,39 @@ +# On-disk serialization format for TensorFlow models. + +load( + "//tensorflow:tensorflow.bzl", + "if_not_windows_or_mac", +) +load("//tensorflow/core/platform:rules_cc.bzl", "cc_library") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:license"], + default_visibility = [ + "//tensorflow/cc/experimental/tfa:__subpackages__", + "//tensorflow/cc/saved_model:__subpackages__", + "//tensorflow/tools/tfg_graph_transforms:__subpackages__", + ], + licenses = ["notice"], +) + +cc_library( + name = "internal_api", + srcs = ["internal_api.cc"], + hdrs = ["internal_api.h"], + deps = [ + "//tensorflow/cc/saved_model:metrics", + "//tensorflow/cc/saved_model:util", + "//tensorflow/core/platform", + "//tensorflow/core/platform:env", + "//tensorflow/core/protobuf:for_core_protos_cc", + "//tensorflow/tools/proto_splitter/cc:max_size", + "@com_google_absl//absl/log", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/strings:cord", + ] + if_not_windows_or_mac([ + "//tensorflow/tools/proto_splitter:merge", + "//tensorflow/tools/proto_splitter/cc:saved_model_splitter", + ]), +) diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/image_format/README.md b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/image_format/README.md new file mode 100644 index 0000000000000000000000000000000000000000..0b008180069fbaae66c10ef25aec7cc817fe9385 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/image_format/README.md @@ -0,0 +1,21 @@ +# SavedModel Image Format + +Everything related to the SavedModel Image format belongs in this directory. +If you are a TensorFlow Python user, you can try this format by setting the +`experimental_image_format` option: + +``` +tf.savedmodel.save( + model, path, + options=tf.saved_model.SaveOptions(experimental_image_format=True) +) +``` + +When this option is enabled, exported SavedModels with proto size > 2GB will +automatically save with the new format (`.cpb` instead of `.pb`). + + + +The official TF APIs (TF1/TF2 python or C++ loading) have already been +integrated to handle the new format, but some downstream converters may not +have been updated. diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/image_format/internal_api.cc b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/image_format/internal_api.cc new file mode 100644 index 0000000000000000000000000000000000000000..8a1610998367bad0f4a6cd7eab5d3dda02b9c59c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/image_format/internal_api.cc @@ -0,0 +1,143 @@ +/* Copyright 2023 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/cc/saved_model/image_format/internal_api.h" + +#include +#include + +#include "absl/log/log.h" +#include "absl/status/status.h" +#include "absl/strings/cord.h" +#include "absl/strings/str_cat.h" +#include "tensorflow/cc/saved_model/metrics.h" +#include "tensorflow/cc/saved_model/util.h" +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/platform/file_system_helper.h" +#include "tensorflow/core/protobuf/saved_model.pb.h" +#include "tensorflow/tools/proto_splitter/cc/max_size.h" +// TODO(b/291933687), TODO(b/291001524) +#if !defined(PLATFORM_WINDOWS) && !defined(__APPLE__) +#include "tensorflow/tools/proto_splitter/cc/saved_model_splitter.h" +#include "tensorflow/tools/proto_splitter/merge.h" +#endif +#define IS_OSS false +namespace tensorflow { +namespace image_format { + +absl::Status ReadSavedModel(const std::string& file_prefix, + SavedModel* saved_model_proto) { + LOG(INFO) << "Reading SavedModel from: " << file_prefix; + +#if defined(PLATFORM_WINDOWS) || defined(__APPLE__) + const std::string saved_model_pb_path = absl::StrCat(file_prefix, ".pb"); + TF_ASSIGN_OR_RETURN( + bool saved_model_pb_exists, + internal::FileExists(Env::Default(), saved_model_pb_path)); + if (saved_model_pb_exists) { + absl::Status result = + ReadBinaryProto(Env::Default(), saved_model_pb_path, saved_model_proto); + if (result.ok()) { + metrics::SavedModelReadCount( + saved_model::GetWriteVersion(*saved_model_proto)) + .IncrementBy(1); + } + return result; + } +#endif + + // TODO(b/295208714): add pbtxt support to Merger::Read + const std::string saved_model_pbtxt_path = + absl::StrCat(file_prefix, ".pbtxt"); + auto saved_model_pbtxt_exists = + internal::FileExists(Env::Default(), saved_model_pbtxt_path); + if (saved_model_pbtxt_exists.value_or(false)) { + absl::Status result = ReadTextProto(Env::Default(), saved_model_pbtxt_path, + saved_model_proto); + if (result.ok()) { + metrics::SavedModelReadCount( + saved_model::GetWriteVersion(*saved_model_proto)) + .IncrementBy(1); + } + return result; + } + +#if !defined(PLATFORM_WINDOWS) && !defined(__APPLE__) + absl::Status result = + tools::proto_splitter::Merger::Read(file_prefix, saved_model_proto); + if (result.ok()) { + metrics::SavedModelReadCount( + saved_model::GetWriteVersion(*saved_model_proto)) + .IncrementBy(1); + } + return result; +#endif + + return absl::Status( + absl::StatusCode::kNotFound, + absl::StrCat("Could not find SavedModel .pb or .pbtxt at supplied " + "file prefix: ", + file_prefix, + ". Check that " + "the directory exists and that you have the right " + "permissions for accessing it.")); +} + +absl::Status WriteSavedModel(SavedModel* saved_model_proto, + const std::string& file_prefix) { +#if !defined(PLATFORM_WINDOWS) && !defined(__APPLE__) + tools::proto_splitter::SavedModelSplitter splitter(saved_model_proto); + return splitter.Write(file_prefix).status(); +#else + return absl::UnimplementedError( + "WriteSavedModel not implemented for Windows or MacOS."); +#endif +} + +absl::StatusOr> WriteSavedModelToString( + SavedModel* saved_model_proto) { +#if !defined(PLATFORM_WINDOWS) && !defined(__APPLE__) + tools::proto_splitter::SavedModelSplitter splitter(saved_model_proto); + return splitter.WriteToString(); +#else + return absl::UnimplementedError( + "WriteSavedModelToString not implemented for Windows or MacOS."); +#endif +} + +#if !IS_OSS +// TODO(b/311769337): Define the function unconditionally after tf oss +// dependency is updated to protobuf v22.x. +absl::StatusOr> WriteSavedModelToCord( + SavedModel* saved_model_proto) { + tools::proto_splitter::SavedModelSplitter splitter(saved_model_proto); + return splitter.WriteToCord(); +} +#endif + +absl::Status WriteSavedModel(SavedModel* saved_model_proto, + const std::string& file_prefix, + int debug_max_size) { +#if !defined(PLATFORM_WINDOWS) && !defined(__APPLE__) + tools::proto_splitter::DebugSetMaxSize(debug_max_size); + return WriteSavedModel(saved_model_proto, file_prefix); +#else + return absl::UnimplementedError( + "WriteSavedModel not implemented for Windows or MacOS."); +#endif +} + +} // namespace image_format +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/image_format/internal_api.h b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/image_format/internal_api.h new file mode 100644 index 0000000000000000000000000000000000000000..8f779bac6b29401e1abf40224ac2f0dde6a64ecf --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/image_format/internal_api.h @@ -0,0 +1,65 @@ +/* Copyright 2023 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. +==============================================================================*/ + +#ifndef TENSORFLOW_CC_SAVED_MODEL_IMAGE_FORMAT_INTERNAL_API_H_ +#define TENSORFLOW_CC_SAVED_MODEL_IMAGE_FORMAT_INTERNAL_API_H_ + +#include +#include + +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/cord.h" +#include "tensorflow/core/protobuf/saved_model.pb.h" + +#define IS_OSS false + +namespace tensorflow { +namespace image_format { + +// Reads the SavedModel proto from {file_prefix}{.pb|.cpb}. +// Returns a failure status when the SavedModel file does not exist. +absl::Status ReadSavedModel(const std::string& file_prefix, + SavedModel* saved_model_proto); + +// Writes the SavedModel proto to a file or to string. If the proto is < the +// protobuf maximum size, then it will be serialized as a `.pb` proto binary. +// When larger than the maximum size, the SavedModel proto is destructively +// separated into chunks and written to +// `.cpb` (chunked proto). +// +// Write SavedModel to {file_prefix}{.pb|.cpb}. +absl::Status WriteSavedModel(SavedModel* saved_model_proto, + const std::string& file_prefix); +// Writes the SavedModel proto to std::string +// The bool field record whether it's saved as a chunked protobuf (true) or +// regular protobuf (false) +absl::StatusOr> WriteSavedModelToString( + SavedModel* saved_model_proto); +#if !IS_OSS +absl::StatusOr> WriteSavedModelToCord( + SavedModel* saved_model_proto); +#endif + +// See above. The `debug_max_size` argument can be used to the maximum size to +// less than 2GB for testing purposes. +absl::Status WriteSavedModel(SavedModel* saved_model_proto, + const std::string& file_prefix, + int debug_max_size); + +} // namespace image_format +} // namespace tensorflow + +#endif // TENSORFLOW_CC_SAVED_MODEL_IMAGE_FORMAT_INTERNAL_API_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/loader.cc b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/loader.cc new file mode 100644 index 0000000000000000000000000000000000000000..b82d860d9280415bc39021eafd000447cf67a9cb --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/loader.cc @@ -0,0 +1,575 @@ +/* Copyright 2016 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/cc/saved_model/loader.h" + +#include +#include +#include +#include + +#include "absl/status/status.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/str_join.h" +#include "tensorflow/cc/saved_model/constants.h" +#include "tensorflow/cc/saved_model/fingerprinting.h" +#include "tensorflow/cc/saved_model/loader_util.h" +#include "tensorflow/cc/saved_model/metrics.h" +#include "tensorflow/cc/saved_model/reader.h" +#include "tensorflow/cc/saved_model/util.h" +#include "tensorflow/core/framework/attr_value.pb.h" +#include "tensorflow/core/framework/function.pb.h" +#include "tensorflow/core/framework/graph_debug_info.pb.h" +#include "tensorflow/core/framework/node_def.pb.h" +#include "tensorflow/core/framework/op_def.pb.h" +#include "tensorflow/core/framework/tensor.pb.h" +#include "tensorflow/core/lib/io/path.h" +#include "tensorflow/core/lib/monitoring/counter.h" +#include "tensorflow/core/lib/monitoring/sampler.h" +#include "tensorflow/core/lib/strings/str_util.h" +#include "tensorflow/core/lib/strings/strcat.h" +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/platform/errors.h" +#include "tensorflow/core/platform/file_system_helper.h" +#include "tensorflow/core/platform/statusor.h" +#include "tensorflow/core/platform/threadpool_options.h" +#include "tensorflow/core/protobuf/meta_graph.pb.h" +#include "tensorflow/core/protobuf/saver.pb.h" +#include "tensorflow/core/public/session.h" +#include "tensorflow/core/public/session_options.h" +#include "tensorflow/core/util/tensor_bundle/naming.h" + +namespace tensorflow { +namespace { + +auto* load_attempt_count = monitoring::Counter<2>::New( + "/tensorflow/cc/saved_model/load_attempt_count", + "The number of times a SavedModel was successfully loaded.", "model_path", + "status"); +auto* load_latency = monitoring::Counter<1>::New( + "/tensorflow/cc/saved_model/load_latency", + "Latency in microseconds for SavedModels that were successfully loaded.", + "model_path"); +auto* load_latency_by_stage = monitoring::Sampler<2>::New( + { + "/tensorflow/cc/saved_model/load_latency_by_stage", // metric name + "Distribution of wall time spent (in microseconds) in each stage " + "(restore graph from disk, run init graph op, etc) when loading the " + "model", + "model_path", + "stage", + }, + // Scale of 10, power of 1.8 with bucket count 37 (~258 minutes). + monitoring::Buckets::Exponential(10, 1.8, 37)); + +constexpr char kLoadAttemptFail[] = "fail"; +constexpr char kLoadAttemptSuccess[] = "success"; +// `tensorflow::LoadSavedModel` API label. +constexpr char kCCLoadLabel[] = "cc_load"; + +uint64 GetLatencyMicroseconds(const uint64 start_microseconds) { + const uint64 end_microseconds = EnvTime::NowMicros(); + // Avoid clock skew. + if (end_microseconds < start_microseconds) return 0; + return end_microseconds - start_microseconds; +} + +// Ensure that constant tensors loaded from the saved model have valid shape. +// Also ensure that constant nodes have a value assigned to them. +// TODO(b/154763635): this is temporary and will be replaced with a better audit +static absl::Status ValidateNode(const NodeDef& node) { + const auto node_iterator = node.attr().find("value"); + if (node_iterator != node.attr().end()) { + AttrValue node_value = node_iterator->second; + if (node_value.has_tensor()) { + const PartialTensorShape node_shape(node_value.tensor().tensor_shape()); + if (node_shape.num_elements() < 0) { + return absl::FailedPreconditionError(absl::StrCat( + "Saved model contains node \"", node.name(), "\" (op \"", node.op(), + "\") which initializes from a tensor with ", + node_shape.num_elements(), " elements")); + } + } + } else if (node.op() == "Const") { + return absl::FailedPreconditionError(absl::StrCat( + "Saved model contains node \"", node.name(), + "\" which is a constant tensor but no value has been provided")); + } + return absl::OkStatus(); +} + +static absl::Status ValidateFunctionNotRecursive(const FunctionDef& function) { + const auto& function_name = function.signature().name(); + for (const auto& node : function.node_def()) { + if (node.op() == function_name) { + return absl::FailedPreconditionError(absl::StrCat( + "Function ", function_name, + " is self recursive and TensorFlow does not support this scenario.")); + } + } + + return absl::OkStatus(); +} + +static absl::Status ValidateSavedTensors(const GraphDef& graph_def) { + for (const auto& node : graph_def.node()) { + TF_RETURN_IF_ERROR(ValidateNode(node)); + } + + if (graph_def.has_library()) { + const FunctionDefLibrary& library = graph_def.library(); + for (const auto& function : library.function()) { + for (const auto& node : function.node_def()) { + TF_RETURN_IF_ERROR(ValidateNode(node)); + } + + // Also check that there is no recursivity in the library + TF_RETURN_IF_ERROR(ValidateFunctionNotRecursive(function)); + } + } + + return absl::OkStatus(); +} + +Tensor CreateStringTensor(const string& value) { + Tensor tensor(DT_STRING, TensorShape({})); + tensor.scalar()() = value; + return tensor; +} + +void AddAssetsTensorsToInputs(const absl::string_view export_dir, + const std::vector& asset_file_defs, + std::vector>* inputs) { + if (asset_file_defs.empty()) { + return; + } + for (auto& asset_file_def : asset_file_defs) { + Tensor assets_file_path_tensor = CreateStringTensor(io::JoinPath( + export_dir, kSavedModelAssetsDirectory, asset_file_def.filename())); + inputs->push_back( + {asset_file_def.tensor_info().name(), assets_file_path_tensor}); + } +} + +// Like Session::Run(), but uses the Make/Run/ReleaseCallable() API to avoid +// leaving behind non-GC'ed state. +// +// Detailed motivation behind this approach, from ashankar@: +// +// Each call to Session::Run() that identifies a new subgraph (based on feeds +// and fetches) creates some datastructures that live as long as the session +// (the partitioned graph, associated executors etc.). +// +// A pathological case of this would be if say the initialization op +// (main_op/legacy_init_op) involves the use of a large constant. Then we +// allocate memory for that large constant that will just stick around till the +// session dies. With this Callable mechanism, that memory will be released +// right after ReleaseCallable returns. +// +// However, the resource manager state remains. +absl::Status RunOnce(const RunOptions& run_options, + const std::vector>& inputs, + const std::vector& output_tensor_names, + const std::vector& target_node_names, + std::vector* outputs, RunMetadata* run_metadata, + Session* session) { + CallableOptions callable_options; + std::vector feed_tensors; + *callable_options.mutable_run_options() = run_options; + for (const auto& input : inputs) { + const string& name = input.first; + const Tensor& tensor = input.second; + callable_options.add_feed(name); + feed_tensors.push_back(tensor); + } + for (const string& output_tensor_name : output_tensor_names) { + callable_options.add_fetch(output_tensor_name); + } + for (const string& target_node_name : target_node_names) { + callable_options.add_target(target_node_name); + } + + Session::CallableHandle callable_handle; + TF_RETURN_IF_ERROR(session->MakeCallable(callable_options, &callable_handle)); + const absl::Status run_status = session->RunCallable( + callable_handle, feed_tensors, outputs, run_metadata); + // Be sure to call ReleaseCallable() regardless of the outcome of + // RunCallable(). + session->ReleaseCallable(callable_handle).IgnoreError(); + return run_status; +} + +// RunInitOp will return OK if the initialization op was run successfully. +// An empty init_op_name indicates that there are no init ops to run. +absl::Status RunInitOp(const RunOptions& run_options, const string& export_dir, + const MetaGraphDef& meta_graph_def, + const std::vector& asset_file_defs, + Session* session, const string& init_op_name) { + if (!init_op_name.empty()) { + LOG(INFO) << "Running initialization op on SavedModel bundle at path: " + << export_dir; + std::vector> inputs; + AddAssetsTensorsToInputs(export_dir, asset_file_defs, &inputs); + RunMetadata run_metadata; + return RunOnce(run_options, inputs, {}, {init_op_name}, + nullptr /* outputs */, &run_metadata, session); + } + return absl::OkStatus(); +} + +absl::Status RunRestore(const RunOptions& run_options, const string& export_dir, + const absl::string_view restore_op_name, + const absl::string_view variable_filename_const_op_name, + const std::vector& asset_file_defs, + Session* session) { + LOG(INFO) << "Restoring SavedModel bundle."; + // Find path to variables to be restored in export directory. + const string variables_directory = + io::JoinPath(export_dir, kSavedModelVariablesDirectory); + // Check for saver checkpoints in v2 format. Models exported in the checkpoint + // v2 format will have a variables.index file. The corresponding + // variables are stored in the variables.data-?????-of-????? files. + const string variables_index_path = io::JoinPath( + variables_directory, MetaFilename(kSavedModelVariablesFilename)); + TF_ASSIGN_OR_RETURN( + bool variables_index_exists, + internal::FileExists(Env::Default(), variables_index_path)); + if (!variables_index_exists) { + LOG(INFO) << "The specified SavedModel has no variables; no checkpoints " + "were restored. File does not exist: " + << variables_index_path; + return absl::OkStatus(); + } + const string variables_path = + io::JoinPath(variables_directory, kSavedModelVariablesFilename); + + // Add variables to the graph. + Tensor variables_path_tensor(DT_STRING, TensorShape({})); + variables_path_tensor.scalar()() = variables_path; + + std::vector> inputs = { + {string(variable_filename_const_op_name), variables_path_tensor}}; + + AddAssetsTensorsToInputs(export_dir, asset_file_defs, &inputs); + + RunMetadata run_metadata; + return RunOnce(run_options, inputs, {}, {string(restore_op_name)}, + nullptr /* outputs */, &run_metadata, session); +} + +} // namespace + +SavedModelBundleInterface::~SavedModelBundleInterface() = default; + +absl::Status LoadMetagraphIntoSession(const SessionOptions& session_options, + const MetaGraphDef& meta_graph, + std::unique_ptr* session) { + Session* session_p = nullptr; + TF_RETURN_IF_ERROR(NewSession(session_options, &session_p)); + session->reset(session_p); + TF_RETURN_IF_ERROR(ValidateSavedTensors(meta_graph.graph_def())); + return (*session)->Create(meta_graph.graph_def()); +} + +absl::Status LoadGraphDefIntoSession(const SessionOptions& session_options, + GraphDef graph_def, + std::unique_ptr* session) { + Session* session_p = nullptr; + TF_RETURN_IF_ERROR(NewSession(session_options, &session_p)); + session->reset(session_p); + TF_RETURN_IF_ERROR(ValidateSavedTensors(graph_def)); + return (*session)->Create(std::move(graph_def)); +} + +absl::Status LoadSavedModelInternal(const SessionOptions& session_options, + const RunOptions& run_options, + const string& export_dir, + const std::unordered_set& tags, + SavedModelBundle* const bundle) { + TF_RETURN_IF_ERROR(ReadMetaGraphDefFromSavedModel(export_dir, tags, + &bundle->meta_graph_def)); + TF_RETURN_IF_ERROR( + ReadSavedModelDebugInfoIfPresent(export_dir, &bundle->debug_info)); + TF_RETURN_IF_ERROR(LoadMetagraphIntoSession( + session_options, bundle->meta_graph_def, &bundle->session)); + TF_RETURN_IF_ERROR(RestoreSession(run_options, bundle->meta_graph_def, + export_dir, &bundle->session)); + return absl::OkStatus(); +} + +namespace { +// Session wrapper that prevents calls to Session::Create(), Session::Extend(), +// and the deprecated partial-run methods. +// +// Limiting the available methods on a returned Session gives us the option +// to replace the Session with a cut-down implementation, without breaking any +// users. +class LiteSessionWrapper : public Session { + public: + explicit LiteSessionWrapper(std::unique_ptr wrapped) + : wrapped_(std::move(wrapped)) {} + + absl::Status Create(const GraphDef& graph) override { + return absl::UnimplementedError("Session::Create()"); + } + absl::Status Create(GraphDef&& graph) override { + return absl::UnimplementedError("Session::Create()"); + } + + absl::Status Extend(const GraphDef& graph) override { + return absl::UnimplementedError("Session::Extend()"); + } + absl::Status Extend(GraphDef&& graph) override { + return absl::UnimplementedError("Session::Extend()"); + } + + absl::Status Run(const std::vector>& inputs, + const std::vector& output_tensor_names, + const std::vector& target_node_names, + std::vector* outputs) override { + return wrapped_->Run(inputs, output_tensor_names, target_node_names, + outputs); + } + + absl::Status Create(const RunOptions& run_options, + const GraphDef& graph) override { + return absl::UnimplementedError("Session::Create()"); + } + absl::Status Extend(const RunOptions& run_options, + const GraphDef& graph) override { + return absl::UnimplementedError("Session::Extend()"); + } + absl::Status Create(const RunOptions& run_options, + GraphDef&& graph) override { + return absl::UnimplementedError("Session::Create()"); + } + absl::Status Extend(const RunOptions& run_options, + GraphDef&& graph) override { + return absl::UnimplementedError("Session::Extend()"); + } + absl::Status Close(const RunOptions& run_options) override { + return wrapped_->Close(run_options); + } + + absl::Status Run(const RunOptions& run_options, + const std::vector>& inputs, + const std::vector& output_tensor_names, + const std::vector& target_node_names, + std::vector* outputs, + RunMetadata* run_metadata) override { + return wrapped_->Run(run_options, inputs, output_tensor_names, + target_node_names, outputs, run_metadata); + } + + absl::Status Run( + const RunOptions& run_options, + const std::vector>& inputs, + const std::vector& output_tensor_names, + const std::vector& target_tensor_names, + std::vector* outputs, RunMetadata* run_metadata, + const thread::ThreadPoolOptions& threadpool_options) override { + return wrapped_->Run(run_options, inputs, output_tensor_names, + target_tensor_names, outputs, run_metadata, + threadpool_options); + } + + absl::Status PRunSetup(const std::vector& input_names, + const std::vector& output_names, + const std::vector& target_nodes, + string* handle) override { + return absl::UnimplementedError("Session::PRunSetup()"); + } + + absl::Status PRun(const string& handle, + const std::vector>& inputs, + const std::vector& output_names, + std::vector* outputs) override { + return absl::UnimplementedError("Session::PRun()"); + } + + absl::Status ListDevices(std::vector* response) override { + return wrapped_->ListDevices(response); + } + + absl::Status Close() override { return wrapped_->Close(); } + + absl::Status LocalDeviceManager(const DeviceMgr** device_mgr) override { + return wrapped_->LocalDeviceManager(device_mgr); + } + + absl::Status MakeCallable(const CallableOptions& callable_options, + CallableHandle* out_handle) override { + return wrapped_->MakeCallable(callable_options, out_handle); + } + + absl::Status RunCallable(CallableHandle handle, + const std::vector& feed_tensors, + std::vector* fetch_tensors, + RunMetadata* run_metadata) override { + return wrapped_->RunCallable(handle, feed_tensors, fetch_tensors, + run_metadata); + } + + absl::Status RunCallable( + CallableHandle handle, const std::vector& feed_tensors, + std::vector* fetch_tensors, RunMetadata* run_metadata, + const thread::ThreadPoolOptions& threadpool_options) override { + return wrapped_->RunCallable(handle, feed_tensors, fetch_tensors, + run_metadata, threadpool_options); + } + + absl::Status ReleaseCallable(CallableHandle handle) override { + return wrapped_->ReleaseCallable(handle); + } + + absl::Status Finalize() override { return wrapped_->Finalize(); } + + private: + const std::unique_ptr wrapped_; +}; +} // namespace + +absl::Status LoadSavedModelInternal(const SessionOptions& session_options, + const RunOptions& run_options, + const string& export_dir, + const std::unordered_set& tags, + SavedModelBundleLite* const bundle) { + MetaGraphDef meta_graph_def; + TF_RETURN_IF_ERROR( + ReadMetaGraphDefFromSavedModel(export_dir, tags, &meta_graph_def)); + std::unique_ptr session; + TF_RETURN_IF_ERROR(LoadGraphDefIntoSession( + session_options, std::move(*meta_graph_def.mutable_graph_def()), + &session)); + TF_RETURN_IF_ERROR( + RestoreSession(run_options, meta_graph_def, export_dir, &session)); + *bundle = SavedModelBundleLite( + std::make_unique(std::move(session)), + std::move(*meta_graph_def.mutable_signature_def())); + return absl::OkStatus(); +} + +template +absl::Status LoadSavedModelGeneric(const SessionOptions& session_options, + const RunOptions& run_options, + const string& export_dir, + const std::unordered_set& tags, + BundleType* const bundle) { + metrics::SavedModelReadApi(kCCLoadLabel).IncrementBy(1); + auto fingerprint_proto = + saved_model::fingerprinting::ReadSavedModelFingerprint(export_dir); + if (fingerprint_proto.ok()) { + // Set gauge cell with saved_model_checksum. + metrics::SavedModelReadFingerprint().Set( + std::to_string(fingerprint_proto->saved_model_checksum())); + } + + // TODO(robson): Add tests for the counters. + const uint64 start_microseconds = Env::Default()->NowMicros(); + const absl::Status status = LoadSavedModelInternal( + session_options, run_options, export_dir, tags, bundle); + auto log_and_count = [&](const string& status_str) { + LOG(INFO) << "SavedModel load for tags { " << absl::StrJoin(tags, " ") + << " }; Status: " << status_str << ": " << status << ". Took " + << GetLatencyMicroseconds(start_microseconds) << " microseconds."; + load_attempt_count->GetCell(export_dir, status_str)->IncrementBy(1); + }; + if (status.ok()) { + log_and_count(kLoadAttemptSuccess); + metrics::SavedModelReadPath().Set(export_dir); + } else { + log_and_count(kLoadAttemptFail); + } + load_latency->GetCell(export_dir) + ->IncrementBy(GetLatencyMicroseconds(start_microseconds)); + return status; +} + +absl::Status LoadSavedModel(const SessionOptions& session_options, + const RunOptions& run_options, + const string& export_dir, + const std::unordered_set& tags, + SavedModelBundle* const bundle) { + return LoadSavedModelGeneric(session_options, run_options, + export_dir, tags, bundle); +} + +absl::Status RestoreSession(const RunOptions& run_options, + const MetaGraphDef& meta_graph, + const string& export_dir, + std::unique_ptr* session) { + const uint64 read_start_microseconds = Env::Default()->NowMicros(); + std::vector asset_file_defs; + TF_RETURN_IF_ERROR(internal::GetAssetFileDefs(meta_graph, &asset_file_defs)); + if (meta_graph.has_saver_def()) { + TF_RETURN_IF_ERROR(RunRestore(run_options, export_dir, + meta_graph.saver_def().restore_op_name(), + meta_graph.saver_def().filename_tensor_name(), + asset_file_defs, session->get())); + } + // Record walltime spent in restoring graph from disk, but postpone metric + // increments until graph init finishes. + const uint64 restore_graph_walltime = + GetLatencyMicroseconds(read_start_microseconds); + + const uint64 graph_init_start_microseconds = Env::Default()->NowMicros(); + string init_op_name; + TF_RETURN_IF_ERROR( + internal::GetInitOp(export_dir, meta_graph, &init_op_name)); + TF_RETURN_IF_ERROR(RunInitOp(run_options, export_dir, meta_graph, + asset_file_defs, session->get(), init_op_name)); + load_latency_by_stage->GetCell(export_dir, "restore_graph") + ->Add(restore_graph_walltime); + // Record wall time spent in init op. + load_latency_by_stage->GetCell(export_dir, "init_graph") + ->Add(GetLatencyMicroseconds(graph_init_start_microseconds)); + return absl::OkStatus(); +} + +absl::Status LoadSavedModel(const SessionOptions& session_options, + const RunOptions& run_options, + const string& export_dir, + const std::unordered_set& tags, + SavedModelBundleLite* const bundle) { + SessionOptions rewritten_options(session_options); + // We disallow calls to Session::Extend() on the returned session, so we can + // reduce memory consumption by not storing the original GraphDef. + rewritten_options.config.mutable_experimental() + ->set_optimize_for_static_graph(true); + // Disallowing the `RunOptions.output_partition_graphs` option (typically used + // in debugging and tests) allows us to reduce memory consumption further by + // not storing the rewritten subgraph for each signature. + rewritten_options.config.mutable_experimental() + ->set_disable_output_partition_graphs(true); + // TODO(mrry): Consider specializing the session creation to reduce peak + // RAM consumption by using `Session::Create(GraphDef&&)`. + TF_RETURN_IF_ERROR(LoadSavedModelGeneric(rewritten_options, run_options, + export_dir, tags, bundle)); + return absl::OkStatus(); +} + +bool MaybeSavedModelDirectory(const string& export_dir) { + const string saved_model_pb_path = + io::JoinPath(export_dir, kSavedModelFilenamePb); + const string saved_model_cpb_path = + io::JoinPath(export_dir, kSavedModelFilenameCpb); + const string saved_model_pbtxt_path = + io::JoinPath(export_dir, kSavedModelFilenamePbTxt); + return Env::Default()->FileExists(saved_model_pb_path).ok() || + Env::Default()->FileExists(saved_model_cpb_path).ok() || + Env::Default()->FileExists(saved_model_pbtxt_path).ok(); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/loader.h b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/loader.h new file mode 100644 index 0000000000000000000000000000000000000000..11b731a9976965c710a02c7f0d96035a301d4632 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/loader.h @@ -0,0 +1,150 @@ +/* Copyright 2016 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. +==============================================================================*/ + +/// SavedModel loading functions and SavedModelBundle struct. + +#ifndef TENSORFLOW_CC_SAVED_MODEL_LOADER_H_ +#define TENSORFLOW_CC_SAVED_MODEL_LOADER_H_ + +#include +#include + +#include "tensorflow/core/framework/graph_debug_info.pb.h" +#include "tensorflow/core/lib/core/status.h" +#include "tensorflow/core/protobuf/meta_graph.pb.h" +#include "tensorflow/core/public/session.h" + +namespace tensorflow { + +/// Represents a SavedModel that is loaded from storage. +class SavedModelBundleInterface { + public: + virtual ~SavedModelBundleInterface(); + + /// Returns the TensorFlow Session that can be used to interact with the + /// SavedModel. + virtual Session* GetSession() const = 0; + + /// Returns a map from signature name to SignatureDef for all signatures in + /// in the SavedModel. + virtual const protobuf::Map& GetSignatures() const = 0; +}; + +/// SavedModel representation once the SavedModel is loaded from storage. +/// +/// NOTE: Prefer to use SavedModelBundleLite in new code, as it consumes less +/// RAM. +struct SavedModelBundle : public SavedModelBundleInterface { + /// A TensorFlow Session does not Close itself on destruction. To avoid + /// resource leaks, we explicitly call Close on Sessions that we create. + ~SavedModelBundle() override { + if (session) { + session->Close().IgnoreError(); + } + } + + SavedModelBundle() = default; + + Session* GetSession() const override { return session.get(); } + const protobuf::Map& GetSignatures() const override { + return meta_graph_def.signature_def(); + } + + std::unique_ptr session; + MetaGraphDef meta_graph_def; + std::unique_ptr debug_info; +}; + +// A version of SavedModelBundle that avoids storing a potentially large +// MetaGraphDef. Prefer to use SavedModelBundleLite in new code. +class SavedModelBundleLite : public SavedModelBundleInterface { + public: + SavedModelBundleLite() = default; + SavedModelBundleLite(SavedModelBundleLite&& other) = default; + SavedModelBundleLite& operator=(SavedModelBundleLite&& other) = default; + + SavedModelBundleLite(std::unique_ptr session, + protobuf::Map signatures) + : session_(std::move(session)), signatures_(std::move(signatures)) {} + + /// A TensorFlow Session does not Close itself on destruction. To avoid + /// resource leaks, we explicitly call Close on Sessions that we create. + ~SavedModelBundleLite() override { + if (session_) { + session_->Close().IgnoreError(); + } + } + + Session* GetSession() const override { return session_.get(); } + const protobuf::Map& GetSignatures() const override { + return signatures_; + } + + private: + std::unique_ptr session_; + protobuf::Map signatures_; +}; + +// Restore variable and resources in the SavedModel export dir for the +// indicated metagraph. +// The recommended way to load a saved model is to call LoadSavedModel, +// which provides an already initialized Metagraph, Session, and DebugInfo. +absl::Status RestoreSession(const RunOptions& run_options, + const MetaGraphDef& meta_graph, + const string& export_dir, + std::unique_ptr* session); + +// Initialize a session which wraps this metagraph. +// The recommended way to load a saved model is to call LoadSavedModel, +// which provides an already initialized Metagraph, Session, and DebugInfo. +absl::Status LoadMetagraphIntoSession(const SessionOptions& session_options, + const MetaGraphDef& meta_graph, + std::unique_ptr* session); + +/// Loads a SavedModel from the specified export directory. The MetaGraphDef +/// to be loaded is identified by the supplied tags, corresponding exactly to +/// the set of tags used at SavedModel build time. Stores a SavedModel bundle in +/// *bundle with a session and the requested MetaGraphDef, if found. +/// +/// NOTE: Prefer the overload that takes a SavedModelBundleLite* in new code. +absl::Status LoadSavedModel(const SessionOptions& session_options, + const RunOptions& run_options, + const string& export_dir, + const std::unordered_set& tags, + SavedModelBundle* bundle); + +/// Loads a SavedModel from the specified export directory. The MetaGraphDef +/// to be loaded is identified by the supplied tags, corresponding exactly to +/// the set of tags used at SavedModel build time. Stores a SavedModel bundle +/// in *bundle with a session created from the requested MetaGraphDef if found. +/// +/// This overload creates a SavedModelBundleLite, which consumes less RAM than +/// an equivalent SavedModelBundle. +absl::Status LoadSavedModel(const SessionOptions& session_options, + const RunOptions& run_options, + const string& export_dir, + const std::unordered_set& tags, + SavedModelBundleLite* bundle); + +/// Checks whether the provided directory could contain a SavedModel. Note that +/// the method does not load any data by itself. If the method returns `false`, +/// the export directory definitely does not contain a SavedModel. If the method +/// returns `true`, the export directory may contain a SavedModel but provides +/// no guarantee that it can be loaded. +bool MaybeSavedModelDirectory(const std::string& export_dir); + +} // namespace tensorflow + +#endif // TENSORFLOW_CC_SAVED_MODEL_LOADER_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/loader_util.cc b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/loader_util.cc new file mode 100644 index 0000000000000000000000000000000000000000..8cd1246fafffb4fb8a908483500249d1869b3e6b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/loader_util.cc @@ -0,0 +1,96 @@ +/* Copyright 2016 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/cc/saved_model/loader_util.h" + +#include + +#include "tensorflow/cc/saved_model/constants.h" +#include "tensorflow/core/lib/strings/strcat.h" +#include "tensorflow/core/platform/errors.h" +#include "tensorflow/core/platform/protobuf_internal.h" + +namespace tensorflow { +namespace internal { + +// A SavedModel may store the name of the initialization op to run in the +// in the SignatureDef (v2) or a collection (v1). If an init_op collection +// exists, then the collection must contain exactly one op. +absl::Status GetInitOp(const string& export_dir, + const MetaGraphDef& meta_graph_def, + string* init_op_name) { + const auto& sig_def_map = meta_graph_def.signature_def(); + const auto& init_op_sig_it = + meta_graph_def.signature_def().find(kSavedModelInitOpSignatureKey); + if (init_op_sig_it != sig_def_map.end()) { + const auto& sig_def_outputs = init_op_sig_it->second.outputs(); + const auto& sig_def_outputs_it = + sig_def_outputs.find(kSavedModelInitOpSignatureKey); + if (sig_def_outputs_it == sig_def_outputs.end()) { + return errors::FailedPrecondition("Could not find output ", + kSavedModelInitOpSignatureKey); + } + *init_op_name = sig_def_outputs_it->second.name(); + return absl::OkStatus(); + } + + const auto& collection_def_map = meta_graph_def.collection_def(); + string init_op_collection_key; + if (collection_def_map.find(kSavedModelMainOpKey) != + collection_def_map.end()) { + init_op_collection_key = kSavedModelMainOpKey; + } else { + init_op_collection_key = kSavedModelLegacyInitOpKey; + } + + const auto init_op_it = collection_def_map.find(init_op_collection_key); + if (init_op_it != collection_def_map.end()) { + if (init_op_it->second.node_list().value_size() != 1) { + return errors::FailedPrecondition( + strings::StrCat("Expected exactly one main op in : ", export_dir)); + } + *init_op_name = init_op_it->second.node_list().value(0); + } + return absl::OkStatus(); +} + +absl::Status GetAssetFileDefs(const MetaGraphDef& meta_graph_def, + std::vector* asset_file_defs) { + // With SavedModel v2, we write asset file def into metagraph instead of + // collection, so read from metagraph first. + if (meta_graph_def.asset_file_def_size() > 0) { + for (const auto& asset : meta_graph_def.asset_file_def()) { + asset_file_defs->push_back(asset); + } + return absl::OkStatus(); + } + // Fall back to read from collection to be backward compatible with v1. + const auto& collection_def_map = meta_graph_def.collection_def(); + const auto assets_it = collection_def_map.find(kSavedModelAssetsKey); + if (assets_it == collection_def_map.end()) { + return absl::OkStatus(); + } + const auto& any_assets = assets_it->second.any_list().value(); + for (const auto& any_asset : any_assets) { + AssetFileDef asset_file_def; + TF_RETURN_IF_ERROR( + ParseAny(any_asset, &asset_file_def, "tensorflow.AssetFileDef")); + asset_file_defs->push_back(asset_file_def); + } + return absl::OkStatus(); +} + +} // namespace internal +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/loader_util.h b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/loader_util.h new file mode 100644 index 0000000000000000000000000000000000000000..d7235f9fb17faffdddb5dfbf9a1e9d5dd78d63aa --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/loader_util.h @@ -0,0 +1,40 @@ +/* Copyright 2016 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. +==============================================================================*/ + +#ifndef TENSORFLOW_CC_SAVED_MODEL_LOADER_UTIL_H_ +#define TENSORFLOW_CC_SAVED_MODEL_LOADER_UTIL_H_ + +#include + +#include "tensorflow/core/lib/core/status.h" +#include "tensorflow/core/protobuf/meta_graph.pb.h" + +namespace tensorflow { +namespace internal { + +// A SavedModel may store the name of the initialization op to run in the +// in the SignatureDef (v2) or a collection (v1). If an init_op collection +// exists, then the collection must contain exactly one op. +absl::Status GetInitOp(const string& export_dir, + const MetaGraphDef& meta_graph_def, + string* init_op_name); + +absl::Status GetAssetFileDefs(const MetaGraphDef& meta_graph_def, + std::vector* asset_file_defs); + +} // namespace internal +} // namespace tensorflow + +#endif // TENSORFLOW_CC_SAVED_MODEL_LOADER_UTIL_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/metrics.cc b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/metrics.cc new file mode 100644 index 0000000000000000000000000000000000000000..4eb172c39cfbf8385e17cfd3bcedcb04ee182939 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/metrics.cc @@ -0,0 +1,324 @@ +/* Copyright 2021 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/cc/saved_model/metrics.h" + +#include +#include +#include +#include + +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" +#include "json/config.h" +#include "json/json.h" +#include "json/writer.h" +#include "tensorflow/core/lib/monitoring/counter.h" +#include "tensorflow/core/lib/monitoring/gauge.h" +#include "tensorflow/core/lib/monitoring/sampler.h" +#include "tensorflow/core/protobuf/fingerprint.pb.h" + +namespace tensorflow { +namespace metrics { + +namespace { + +// Counter that tracks total number and `write_version` of SavedModels written. +auto* saved_model_write_counter = monitoring::Counter<1>::New( + "/tensorflow/core/saved_model/write/count", + "The number of SavedModels successfully written.", "write_version"); + +// Counter that tracks total number and `write_version` of SavedModels read. +auto* saved_model_read_counter = monitoring::Counter<1>::New( + "/tensorflow/core/saved_model/read/count", + "The number of SavedModels successfully loaded.", "write_version"); + +// Counter that tracks number of calls for each SavedModel write API. Summing +// across "api_label" is not expected to equal the ".../write/count" cell value +// because programs can invoke more than one API to save a single SM and +// because the API may error out before successfully writing a SM. +auto* saved_model_write_api = monitoring::Counter<1>::New( + "/tensorflow/core/saved_model/write/api", + "The API used to write the SavedModel.", "api_label"); + +// Counter that tracks number of calls for each SavedModel read API. Summing +// across "api_label" is not expected to equal the ".../read/count" cell value +// because programs can invoke more than one API to load a single SM and +// because the API may error out before successfully reading a SM. +auto* saved_model_read_api = monitoring::Counter<1>::New( + "/tensorflow/core/saved_model/read/api", + "The API used to load the SavedModel.", "api_label"); + +// Gauge that contains the fingerprint (saved_model_checksum) of the newly +// written SavedModel. +auto* saved_model_write_fingerprint = monitoring::Gauge::New( + "/tensorflow/core/saved_model/write/fingerprint", + "The fingerprint (saved_model_checksum) of the exported SavedModel."); + +// Gauge that contains the path (saved_model_path) of the newly written +// SavedModel. +auto* saved_model_write_path = monitoring::Gauge::New( + "/tensorflow/core/saved_model/write/path", + "The path (saved_model_path) of the exported SavedModel."); + +// Gauge that contains the path (saved_model_path) and the singleprint +// (concatenation of graph_def_program_hash, signature_def_hash, +// saved_object_graph_hash, and checkpoint_hash) of the newly written +// SavedModel. +auto* saved_model_write_path_and_singleprint = + monitoring::Gauge::New( + "/tensorflow/core/saved_model/write/path_and_singleprint", + "The path (saved_model_path) and singleprint (concatenation of " + "graph_def_program_hash, signature_def_hash, saved_object_graph_hash, " + "and checkpoint_hash) of the newly written SavedModel."); + +// Gauge that contains the fingerprint (saved_model_checksum) of the loaded +// SavedModel. +auto* saved_model_read_fingerprint = monitoring::Gauge::New( + "/tensorflow/core/saved_model/read/fingerprint", + "The fingerprint (saved_model_checksum) of the loaded SavedModel."); + +// Gauge that contains the path (saved_model_path) of the loaded SavedModel. +auto* saved_model_read_path = monitoring::Gauge::New( + "/tensorflow/core/saved_model/read/path", + "The path (saved_model_path) of the loaded SavedModel."); + +// Gauge that contains the path (saved_model_path) and the singleprint +// (concatenation of graph_def_program_hash, signature_def_hash, +// saved_object_graph_hash, and checkpoint_hash) of the loaded SavedModel. +auto* saved_model_read_path_and_singleprint = + monitoring::Gauge::New( + "/tensorflow/core/saved_model/read/path_and_singleprint", + "The path (saved_model_path) and singleprint (concatenation of " + "graph_def_program_hash, signature_def_hash, saved_object_graph_hash, " + "and checkpoint_hash) of the loaded SavedModel."); + +// Gauge that marks whether or not the fingerprint.pb file was found when +// loading the SavedModel. +// Can hold one of the following string values: +// - "FOUND" +// - "NOT_FOUND" +// - "ERROR" +auto* saved_model_found_fingerprint_on_load = + monitoring::Gauge::New( + "/tensorflow/core/saved_model/found_fingerprint_on_load", + "Whether or not the fingerprint.pb file was found when loading the " + "SavedModel."); + +// Distribution of checkpoint write durations. +auto* checkpoint_write_durations = monitoring::Sampler<1>::New( + { + "/tensorflow/core/checkpoint/write/write_durations", // Metric name. + "Distribution of the wall time duration in microseconds of the " + "checkpoint write operation.", // Metric description. + "api_label" // Cell label. + }, + // Scale of 1000, growth factor of 1.5 with upper bound of ~184 minutes. + monitoring::Buckets::Exponential(1000, 1.5, 41)); + +// Distribution of checkpoint read durations. +auto* checkpoint_read_durations = monitoring::Sampler<1>::New( + { + "/tensorflow/core/checkpoint/read/read_durations", // Metric name. + "Distribution of the wall time duration in microseconds of the " + "checkpoint read operation.", // Metric description. + "api_label" // Cell label. + }, + // Scale of 1000, growth factor of 1.5 with upper bound of ~184 minutes. + monitoring::Buckets::Exponential(1000, 1.5, 41)); + +// Distribution of async checkpoint write durations. +auto* async_checkpoint_write_durations = monitoring::Sampler<1>::New( + { + "/tensorflow/core/checkpoint/write/async_write_durations", // Metric + // name. + "Distribution of the wall time duration in microseconds of the async " + "checkpoint write operation", // Metric description. + "api_label" // Cell label. + }, + // Scale of 1000, growth factor of 1.5 with upper bound of ~184 minutes. + monitoring::Buckets::Exponential(1000, 1.5, 41)); + +// Counter that accumulates total time elapsed between module import time and +// the last successful Checkpoint write prior to job preemption or completion. +auto* checkpoint_training_time_saved = monitoring::Counter<1>::New( + "/tensorflow/core/checkpoint/write/training_time_saved", + "Total time in microseconds elapsed between two consecutive write " + "operations in a single job or between Checkpoint construction and the " + "first write operation.", + "api_label"); + +// Counter that records filesize (MB) of written checkpoint. Contains two cells: +// (api_label, filesize). Cardinality should not be an issue as the filesize +// should be equal among all checkpoints written per job. +auto* checkpoint_size = monitoring::Counter<2>::New( + "/tensorflow/core/checkpoint/write/checkpoint_size", + "Size of checkpoint (.index and sharded data files), rounded to the " + "nearest 100 MB.", + "api_label", "filesize"); + +} // namespace + +// Counter that records how long it took to execute the checkpoint sharding +// callback in microseconds. +auto* sharding_callback_duration = monitoring::Counter<0>::New( + "/tensorflow/core/checkpoint/sharding/callback_duration", + "Sharding callback execution duration in microseconds."); + +// Counter that records how many checkpoint shard files were written during +// saving. +auto* num_checkpoint_shards_written = monitoring::Counter<0>::New( + "/tensorflow/core/checkpoint/sharding/num_checkpoint_shards_written", + "Number of checkpoint shard files written during saving."); + +// String gauge which describes the callback used to shard the checkpoint during +// saving. +auto* sharding_callback_description = monitoring::Gauge::New( + "/tensorflow/core/checkpoint/sharding/callback_description", + "Describes the callback used to shard the checkpoint during saving."); + +monitoring::CounterCell& SavedModelWriteCount(absl::string_view write_version) { + return *saved_model_write_counter->GetCell(std::string(write_version)); +} + +monitoring::CounterCell& SavedModelReadCount(absl::string_view write_version) { + return *saved_model_read_counter->GetCell(std::string(write_version)); +} + +monitoring::CounterCell& SavedModelWriteApi(absl::string_view api_label) { + return *saved_model_write_api->GetCell(std::string(api_label)); +} + +monitoring::CounterCell& SavedModelReadApi(absl::string_view api_label) { + return *saved_model_read_api->GetCell(std::string(api_label)); +} + +monitoring::GaugeCell& SavedModelReadFingerprint() { + return *saved_model_read_fingerprint->GetCell(); +} + +monitoring::GaugeCell& SavedModelReadPath() { + return *saved_model_read_path->GetCell(); +} + +monitoring::GaugeCell& SavedModelReadPathAndSingleprint() { + return *saved_model_read_path_and_singleprint->GetCell(); +} + +monitoring::GaugeCell& SavedModelWriteFingerprint() { + return *saved_model_write_fingerprint->GetCell(); +} + +monitoring::GaugeCell& SavedModelWritePath() { + return *saved_model_write_path->GetCell(); +} + +monitoring::GaugeCell& SavedModelWritePathAndSingleprint() { + return *saved_model_write_path_and_singleprint->GetCell(); +} + +std::string MakeFingerprintJson(FingerprintDef fingerprint_def) { + Json::Value fingerprint = Json::objectValue; + fingerprint["saved_model_checksum"] = + Json::UInt64(fingerprint_def.saved_model_checksum()); + fingerprint["graph_def_program_hash"] = + Json::UInt64(fingerprint_def.graph_def_program_hash()); + fingerprint["signature_def_hash"] = + Json::UInt64(fingerprint_def.signature_def_hash()); + fingerprint["saved_object_graph_hash"] = + Json::UInt64(fingerprint_def.saved_object_graph_hash()); + fingerprint["checkpoint_hash"] = + Json::UInt64(fingerprint_def.checkpoint_hash()); + + Json::StreamWriterBuilder json_factory; + return Json::writeString(json_factory, fingerprint); +} + +absl::StatusOr MakeSavedModelPathAndSingleprint( + std::string path, std::string singleprint) { + if (path.empty()) { + return absl::InvalidArgumentError( + "Invalid path_and_singleprint argument. Empty path."); + } + if (singleprint.empty()) { + return absl::InvalidArgumentError( + "Invalid path_and_singleprint argument. Empty singleprint."); + } + return absl::StrCat(path, ":", singleprint); +} + +absl::StatusOr> +ParseSavedModelPathAndSingleprint(std::string path_and_singleprint) { + size_t delimiter = path_and_singleprint.rfind(':'); + if (delimiter == std::string::npos) { + return absl::InvalidArgumentError( + "Invalid path_and_singleprint argument. Found no delimeter."); + } + std::string path = path_and_singleprint.substr(0, delimiter); + if (path.empty()) { + return absl::InvalidArgumentError( + "Invalid path_and_singleprint argument. Empty path."); + } + std::string singleprint = path_and_singleprint.substr(delimiter + 1); + if (singleprint.empty()) { + return absl::InvalidArgumentError( + "Invalid path_and_singleprint argument. Empty singleprint."); + } + return std::pair(path, singleprint); +} + +monitoring::GaugeCell& SavedModelFoundFingerprintOnLoad() { + return *saved_model_found_fingerprint_on_load->GetCell(); +} + +monitoring::SamplerCell& CheckpointReadDuration(absl::string_view api_label) { + return *checkpoint_read_durations->GetCell(std::string(api_label)); +} + +monitoring::SamplerCell& CheckpointWriteDuration(absl::string_view api_label) { + return *checkpoint_write_durations->GetCell(std::string(api_label)); +} + +monitoring::SamplerCell& AsyncCheckpointWriteDuration( + absl::string_view api_label) { + return *async_checkpoint_write_durations->GetCell(std::string(api_label)); +} + +monitoring::CounterCell& TrainingTimeSaved(absl::string_view api_label) { + return *checkpoint_training_time_saved->GetCell(std::string(api_label)); +} + +monitoring::CounterCell& CheckpointSize(absl::string_view api_label, + int64_t filesize) { + return *checkpoint_size->GetCell(std::string(api_label), + std::to_string(filesize)); +} + +monitoring::CounterCell& ShardingCallbackDuration() { + return *sharding_callback_duration->GetCell(); +} + +monitoring::CounterCell& NumCheckpointShardsWritten() { + return *num_checkpoint_shards_written->GetCell(); +} + +monitoring::GaugeCell& ShardingCallbackDescription() { + return *sharding_callback_description->GetCell(); +} + +} // namespace metrics +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/metrics.h b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/metrics.h new file mode 100644 index 0000000000000000000000000000000000000000..1fa1f14cc672e9be3cdb10873dbf0d6b81002a0b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/metrics.h @@ -0,0 +1,147 @@ +/* Copyright 2021 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. +==============================================================================*/ +// APIs for accessing SavedModel and checkpoint metric objects. +// +// In order to collect the data from these metrics, please add the metrics to +// the provided monitoring platform. Unless configured with a user-specified +// monitoring platform, the data is not collected in OSS. + +#ifndef TENSORFLOW_CC_SAVED_MODEL_METRICS_H_ +#define TENSORFLOW_CC_SAVED_MODEL_METRICS_H_ + +#include +#include +#include + +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" +#include "tensorflow/core/lib/monitoring/counter.h" +#include "tensorflow/core/lib/monitoring/gauge.h" +#include "tensorflow/core/lib/monitoring/sampler.h" +#include "tensorflow/core/protobuf/fingerprint.pb.h" + +namespace tensorflow { +namespace metrics { + +const char kFingerprintFound[] = "FOUND"; +const char kFingerprintNotFound[] = "NOT_FOUND"; +const char kFingerprintError[] = "ERROR"; + +// Returns "/tensorflow/core/saved_model/write/count" cell. This metric +// has 1 field "write_version", which is equal to the +// `tensorflow::libexport::GetWriteVersion` of the protobuf and should be +// incremented when a SavedModel has been successfully written. +monitoring::CounterCell& SavedModelWriteCount(absl::string_view write_version); + +// Returns "/tensorflow/core/saved_model/read/count" cell. This metric +// has 1 field "write_version", which is equal to the +// `tensorflow::libexport::GetWriteVersion` of the protobuf, and should be +// incremented when a SavedModel has been successfully read. +monitoring::CounterCell& SavedModelReadCount(absl::string_view write_version); + +// Returns "/tensorflow/core/saved_model/write/api" cell. This metric has 1 +// field "api_label" which corresponds to a SavedModel write API. The cell for +// `foo` should be incremented when the write API `foo` is called. +monitoring::CounterCell& SavedModelWriteApi(absl::string_view api_label); + +// Returns "/tensorflow/core/saved_model/read/api" cell. This metric has 1 +// field "api_label" which corresponds to a SavedModel read API. The cell for +// `foo` should be incremented when the read API `foo` is called. +monitoring::CounterCell& SavedModelReadApi(absl::string_view api_label); + +// Returns "/tensorflow/core/saved_model/write/fingerprint" cell, which contains +// the saved_model_checksum of the SM's fingerprint when it is exported. +monitoring::GaugeCell& SavedModelWriteFingerprint(); + +// Returns "/tensorflow/core/saved_model/write/path" cell, which contains +// the saved_model_path of the SM when it is exported. +monitoring::GaugeCell& SavedModelWritePath(); + +// Returns "/tensorflow/core/saved_model/write/path_and_fingerprint" cell, which +// contains the path (saved_model_path) and fingerprint (concatenation of +// graph_def_program_hash, signature_def_hash, saved_object_graph_hash, +// and checkpoint_hash) of the SavedModel when it is exported. +monitoring::GaugeCell& SavedModelWritePathAndSingleprint(); + +// Returns "/tensorflow/core/saved_model/read/fingerprint" cell, wich contains +// the saved_model_checksum of the SM's fingerprint when it is imported. +monitoring::GaugeCell& SavedModelReadFingerprint(); + +// Returns "/tensorflow/core/saved_model/read/path" cell, wich contains +// the saved_model_path of the SM when it is imported. +monitoring::GaugeCell& SavedModelReadPath(); + +// Returns "/tensorflow/core/saved_model/read/path_and_fingerprint" cell, which +// contains the path (saved_model_path) and singleprint (concatenation of +// graph_def_program_hash, signature_def_hash, saved_object_graph_hash, +// and checkpoint_hash) of the SavedModel when it is imported. +monitoring::GaugeCell& SavedModelReadPathAndSingleprint(); + +// Returns the fingerprint as a Json string. +std::string MakeFingerprintJson(FingerprintDef fingerprint_def); + +// Returns canonical string concatenation of path and singleprint. +absl::StatusOr MakeSavedModelPathAndSingleprint( + std::string path, std::string singleprint); + +// Returns path and singleprint as a pair, parsed canonically from the string +// metric. +absl::StatusOr> +ParseSavedModelPathAndSingleprint(std::string path_and_singleprint); + +// Returns string status indicating whether or not the fingerprint.pb file was +// found when loading the SavedModel. +monitoring::GaugeCell& SavedModelFoundFingerprintOnLoad(); + +// Returns "/tensorflow/core/checkpoint/read/read_durations" cell belonging to +// field `api_label`. +monitoring::SamplerCell& CheckpointReadDuration(absl::string_view api_label); + +// Returns "/tensorflow/core/checkpoint/write/write_durations" cell belonging to +// field `api_label`. +monitoring::SamplerCell& CheckpointWriteDuration(absl::string_view api_label); + +// Returns "/tensorflow/core/checkpoint/write/async_write_durations" cell +// belonging to field `api_label`. +monitoring::SamplerCell& AsyncCheckpointWriteDuration( + absl::string_view api_label); + +// Returns "/tensorflow/core/checkpoint/write/training_time_saved" cell +// belonging to field `api_label`. +monitoring::CounterCell& TrainingTimeSaved(absl::string_view api_label); + +// Returns "/tensorflow/core/checkpoint/write/checkpoint_size" cell +// belonging to field (`api_label`, `filesize`). +monitoring::CounterCell& CheckpointSize(absl::string_view api_label, + int64_t filesize); + +// Returns "/tensorflow/core/checkpoint/sharding/callback_duration" cell which +// describes how long it took to execute the checkpoint sharding callback in +// microseconds. +monitoring::CounterCell& ShardingCallbackDuration(); + +// Returns "/tensorflow/core/checkpoint/sharding/num_checkpoint_shards_written" +// cell which describes how many checkpoint shard files were written during +// saving. +monitoring::CounterCell& NumCheckpointShardsWritten(); + +// Returns "/tensorflow/core/checkpoint/sharding/callback_description" cell +// which describes the callback used to shard the checkpoint during saving. +monitoring::GaugeCell& ShardingCallbackDescription(); + +} // namespace metrics +} // namespace tensorflow + +#endif // TENSORFLOW_CC_SAVED_MODEL_METRICS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/metrics_test.cc b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/metrics_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..3f441eefee8c571b9d296915218e3348818333c9 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/metrics_test.cc @@ -0,0 +1,208 @@ +/* Copyright 2021 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/cc/saved_model/metrics.h" + +#include + +#include +#include +#include "absl/status/status.h" +#include "json/json.h" +#include "json/reader.h" +#include "tensorflow/core/platform/status_matchers.h" +#include "tensorflow/core/platform/test.h" +#include "tensorflow/core/protobuf/fingerprint.pb.h" +#include "tsl/platform/statusor.h" + +namespace tensorflow { +namespace metrics { +// The value of the cells for each metric persists across tests. + +TEST(MetricsTest, TestSavedModelWrite) { + EXPECT_EQ(SavedModelWriteApi("foo").value(), 0); + SavedModelWriteApi("foo").IncrementBy(1); + EXPECT_EQ(SavedModelWriteApi("foo").value(), 1); + + EXPECT_EQ(SavedModelWriteCount("1").value(), 0); + SavedModelWriteCount("1").IncrementBy(1); + EXPECT_EQ(SavedModelWriteCount("1").value(), 1); +} + +TEST(MetricsTest, TestSavedModelRead) { + SavedModelReadApi("bar").IncrementBy(1); + EXPECT_EQ(SavedModelReadApi("bar").value(), 1); + SavedModelReadCount("2").IncrementBy(1); + EXPECT_EQ(SavedModelReadCount("2").value(), 1); + + SavedModelReadApi("baz").IncrementBy(1); + EXPECT_EQ(SavedModelReadApi("baz").value(), 1); + SavedModelReadCount("2").IncrementBy(1); + EXPECT_EQ(SavedModelReadCount("2").value(), 2); +} + +TEST(MetricsTest, TestCheckpointRead) { + EXPECT_EQ(CheckpointReadDuration("foo").value().num(), 0); + CheckpointReadDuration("foo").Add(100); + EXPECT_EQ(CheckpointReadDuration("foo").value().num(), 1); +} + +TEST(MetricsTest, TestCheckpointWrite) { + EXPECT_EQ(CheckpointWriteDuration("foo").value().num(), 0); + CheckpointWriteDuration("foo").Add(100); + EXPECT_EQ(CheckpointWriteDuration("foo").value().num(), 1); +} + +TEST(MetricsTest, TestAsyncCheckpointWrite) { + EXPECT_EQ(AsyncCheckpointWriteDuration("foo").value().num(), 0); + AsyncCheckpointWriteDuration("foo").Add(100); + EXPECT_EQ(AsyncCheckpointWriteDuration("foo").value().num(), 1); +} + +TEST(MetricsTest, TestTrainingTimeSaved) { + EXPECT_EQ(TrainingTimeSaved("foo").value(), 0); + TrainingTimeSaved("foo").IncrementBy(100); + EXPECT_EQ(TrainingTimeSaved("foo").value(), 100); +} + +TEST(MetricsTest, TestCheckpointSize) { + EXPECT_EQ(CheckpointSize("foo", 10).value(), 0); + CheckpointSize("foo", 10).IncrementBy(1); + EXPECT_EQ(CheckpointSize("foo", 10).value(), 1); +} + +TEST(MetricsTest, TestWriteFingerprint) { + EXPECT_EQ(SavedModelWriteFingerprint().value(), ""); + SavedModelWriteFingerprint().Set("foo"); + EXPECT_EQ(SavedModelWriteFingerprint().value(), "foo"); + SavedModelWriteFingerprint().Set("bar"); + EXPECT_EQ(SavedModelWriteFingerprint().value(), "bar"); +} + +TEST(MetricsTest, TestWritePath) { + EXPECT_EQ(SavedModelWritePath().value(), ""); + SavedModelWritePath().Set("foo"); + EXPECT_EQ(SavedModelWritePath().value(), "foo"); + SavedModelWritePath().Set("bar"); + EXPECT_EQ(SavedModelWritePath().value(), "bar"); +} + +TEST(MetricsTest, TestWritePathAndSingleprint) { + EXPECT_EQ(SavedModelWritePathAndSingleprint().value(), ""); + SavedModelWritePathAndSingleprint().Set("foo"); + EXPECT_EQ(SavedModelWritePathAndSingleprint().value(), "foo"); + SavedModelWritePathAndSingleprint().Set("bar"); + EXPECT_EQ(SavedModelWritePathAndSingleprint().value(), "bar"); + + EXPECT_EQ( + MakeSavedModelPathAndSingleprint("path", "singleprint").value_or(""), + "path:singleprint"); +} + +TEST(MetricsTest, TestInvalidMakePathAndSingleprint) { + EXPECT_THAT(MakeSavedModelPathAndSingleprint("", "singleprint"), + absl_testing::StatusIs(absl::StatusCode::kInvalidArgument)); + EXPECT_THAT(MakeSavedModelPathAndSingleprint("path", ""), + absl_testing::StatusIs(absl::StatusCode::kInvalidArgument)); +} + +TEST(MetricsTest, TestReadFingerprint) { + EXPECT_EQ(SavedModelReadFingerprint().value(), ""); + SavedModelReadFingerprint().Set("foo"); + EXPECT_EQ(SavedModelReadFingerprint().value(), "foo"); + SavedModelReadFingerprint().Set("bar"); + EXPECT_EQ(SavedModelReadFingerprint().value(), "bar"); +} + +TEST(MetricsTest, TestReadPath) { + EXPECT_EQ(SavedModelReadPath().value(), ""); + SavedModelReadPath().Set("foo"); + EXPECT_EQ(SavedModelReadPath().value(), "foo"); + SavedModelReadPath().Set("bar"); + EXPECT_EQ(SavedModelReadPath().value(), "bar"); +} + +TEST(MetricsTest, TestReadPathAndSingleprint) { + EXPECT_EQ(SavedModelReadPathAndSingleprint().value(), ""); + SavedModelReadPathAndSingleprint().Set("foo"); + EXPECT_EQ(SavedModelReadPathAndSingleprint().value(), "foo"); + SavedModelReadPathAndSingleprint().Set("bar"); + EXPECT_EQ(SavedModelReadPathAndSingleprint().value(), "bar"); + + TF_ASSERT_OK_AND_ASSIGN( + auto path_singleprint, + ParseSavedModelPathAndSingleprint("path/model:name:singleprint")); + auto [path, singleprint] = path_singleprint; + EXPECT_EQ(path, "path/model:name"); + EXPECT_EQ(singleprint, "singleprint"); +} + +TEST(MetricsTest, TestMakeFingerprintJson) { + FingerprintDef fingerprint; + fingerprint.set_saved_model_checksum(1); + fingerprint.set_graph_def_program_hash(2); + fingerprint.set_signature_def_hash(3); + fingerprint.set_saved_object_graph_hash(4); + fingerprint.set_checkpoint_hash(5); + + std::string serialized_fingerprint_json = MakeFingerprintJson(fingerprint); + + EXPECT_EQ( + serialized_fingerprint_json, + "{\n\t\"checkpoint_hash\" : 5,\n\t\"graph_def_program_hash\" : " + "2,\n\t\"saved_model_checksum\" : 1,\n\t\"saved_object_graph_hash\" : " + "4,\n\t\"signature_def_hash\" : 3\n}"); + + Json::Value fingerprint_json = Json::objectValue; + Json::Reader reader = Json::Reader(); + reader.parse(serialized_fingerprint_json, fingerprint_json); + EXPECT_EQ(fingerprint_json["saved_model_checksum"].asUInt64(), 1); + EXPECT_EQ(fingerprint_json["graph_def_program_hash"].asUInt64(), 2); + EXPECT_EQ(fingerprint_json["signature_def_hash"].asUInt64(), 3); + EXPECT_EQ(fingerprint_json["saved_object_graph_hash"].asUInt64(), 4); + EXPECT_EQ(fingerprint_json["checkpoint_hash"].asUInt64(), 5); +} + +TEST(MetricsTest, TestFoundFingerprintOnLoad) { + EXPECT_EQ(SavedModelFoundFingerprintOnLoad().value(), ""); + + SavedModelFoundFingerprintOnLoad().Set(kFingerprintFound); + EXPECT_EQ(SavedModelFoundFingerprintOnLoad().value(), "FOUND"); + SavedModelFoundFingerprintOnLoad().Set(kFingerprintNotFound); + EXPECT_EQ(SavedModelFoundFingerprintOnLoad().value(), "NOT_FOUND"); + SavedModelFoundFingerprintOnLoad().Set(kFingerprintError); + EXPECT_EQ(SavedModelFoundFingerprintOnLoad().value(), "ERROR"); +} + +TEST(MetricsTest, TestShardingCallbackDuration) { + EXPECT_EQ(ShardingCallbackDuration().value(), 0); + ShardingCallbackDuration().IncrementBy(100); + EXPECT_EQ(ShardingCallbackDuration().value(), 100); +} + +TEST(MetricsTest, TestNumCheckpointShardsWritten) { + EXPECT_EQ(NumCheckpointShardsWritten().value(), 0); + NumCheckpointShardsWritten().IncrementBy(10); + EXPECT_EQ(NumCheckpointShardsWritten().value(), 10); +} + +TEST(MetricsTest, TestShardingCallbackDescription) { + EXPECT_EQ(ShardingCallbackDescription().value(), ""); + ShardingCallbackDescription().Set("foo"); + EXPECT_EQ(ShardingCallbackDescription().value(), "foo"); +} + +} // namespace metrics +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/python/BUILD b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/python/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..74a0a0eaf7a40a96a2ab8be59e43acfec45fb760 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/python/BUILD @@ -0,0 +1,18 @@ +# Description: +# CLIF wrappers for TensorFlow SavedModels. + +load("//tensorflow/core/platform:build_config.bzl", "tf_py_clif_cc") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:license"], + default_visibility = ["//visibility:public"], + licenses = ["notice"], +) + +tf_py_clif_cc( + name = "loader", + srcs = ["loader.clif"], + deps = [ + "//tensorflow/cc/saved_model:loader", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/python/loader.clif b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/python/loader.clif new file mode 100644 index 0000000000000000000000000000000000000000..5de9a2e103d325f93caa2b25ff1cb9597798565f --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/python/loader.clif @@ -0,0 +1,4 @@ +from "third_party/tensorflow/cc/saved_model/loader.h": + namespace `tensorflow`: + class SavedModelBundle: + def __init__(self) diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/reader.cc b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/reader.cc new file mode 100644 index 0000000000000000000000000000000000000000..6a4ff024950eae97be0d272d47c42fc1b10ee91e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/reader.cc @@ -0,0 +1,163 @@ +/* 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 "tensorflow/cc/saved_model/reader.h" + +#include +#include +#include +#include + +#include "absl/memory/memory.h" +#include "absl/status/statusor.h" +#include "tensorflow/cc/saved_model/constants.h" +#include "tensorflow/cc/saved_model/metrics.h" +#include "tensorflow/cc/saved_model/util.h" +#include "tensorflow/core/framework/attr_value.pb.h" +#include "tensorflow/core/framework/function.pb.h" +#include "tensorflow/core/framework/graph.pb.h" +#include "tensorflow/core/framework/node_def.pb.h" +#include "tensorflow/core/framework/tensor.pb.h" +#include "tensorflow/core/lib/io/path.h" +#include "tensorflow/core/lib/strings/str_util.h" +#include "tensorflow/core/lib/strings/strcat.h" +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/platform/file_system_helper.h" +#include "tensorflow/core/platform/path.h" +#include "tensorflow/core/platform/statusor.h" +#include "tensorflow/core/platform/types.h" +#include "tensorflow/core/protobuf/meta_graph.pb.h" +#include "tensorflow/core/protobuf/saved_model.pb.h" +#include "tensorflow/core/util/tensor_bundle/byte_swap_tensor.h" +// Placeholder for protosplitter merger include. + +#define IS_OSS true + +namespace tensorflow { + +absl::StatusOr FindMetaGraphDef( + const std::unordered_set& tags, SavedModel* saved_model_proto) { + LOG(INFO) << "Reading meta graph with tags { " << absl::StrJoin(tags, " ") + << " }"; + for (MetaGraphDef& graph_def : *saved_model_proto->mutable_meta_graphs()) { + // Get tags from the graph_def. + std::unordered_set graph_tags; + for (const string& tag : graph_def.meta_info_def().tags()) { + graph_tags.insert(tag); + } + // Match with the set of tags provided. + if (graph_tags == tags) { + MetaGraphDef* meta_graph_def = &graph_def; + // Correct the endiness of Tensor content on big-endian system + if (!port::kLittleEndian) { + TF_RETURN_IF_ERROR(ByteSwapTensorContentInMetaGraphDef(meta_graph_def)); + } + return meta_graph_def; + } + } + return Status( + absl::StatusCode::kNotFound, + absl::StrCat( + "Could not find meta graph def matching supplied tags: { ", + absl::StrJoin(tags, " "), + " }. To inspect available tag-sets in the SavedModel, please " + "use the SavedModel CLI: `saved_model_cli`")); +} + +// Reads the SavedModel proto from saved_model.pb in `export_dir`. +// Returns a failure status when the SavedModel file does not exist. +Status ReadSavedModel(absl::string_view export_dir, + SavedModel* saved_model_proto) { + LOG(INFO) << "Reading SavedModel from: " << export_dir; + + if (IS_OSS) { + const std::string saved_model_pb_path = + io::JoinPath(export_dir, kSavedModelFilenamePb); + TF_ASSIGN_OR_RETURN( + bool saved_model_pb_exists, + internal::FileExists(Env::Default(), saved_model_pb_path)); + if (saved_model_pb_exists) { + Status result = ReadBinaryProto(Env::Default(), saved_model_pb_path, + saved_model_proto); + if (result.ok()) { + metrics::SavedModelReadCount( + saved_model::GetWriteVersion(*saved_model_proto)) + .IncrementBy(1); + } + return result; + } + } + + const std::string saved_model_pbtxt_path = + io::JoinPath(export_dir, kSavedModelFilenamePbTxt); + auto saved_model_pbtxt_exists = + internal::FileExists(Env::Default(), saved_model_pbtxt_path); + if (saved_model_pbtxt_exists.value_or(false)) { + absl::Status result = ReadTextProto(Env::Default(), saved_model_pbtxt_path, + saved_model_proto); + if (result.ok()) { + metrics::SavedModelReadCount( + saved_model::GetWriteVersion(*saved_model_proto)) + .IncrementBy(1); + } + return result; + } + + if (!IS_OSS) { + // Only use Merger outside of OSS. + // Placeholder for protosplitter merger call. + } + + return absl::Status( + absl::StatusCode::kNotFound, + absl::StrCat("Could not find SavedModel .pb or .pbtxt at supplied " + "export directory path: ", + export_dir, + ". Check that " + "the directory exists and that you have the right " + "permissions for accessing it.")); +} + +Status ReadMetaGraphDefFromSavedModel(absl::string_view export_dir, + const std::unordered_set& tags, + MetaGraphDef* const meta_graph_def) { + SavedModel saved_model_proto; + TF_RETURN_IF_ERROR(ReadSavedModel(export_dir, &saved_model_proto)); + TF_ASSIGN_OR_RETURN(MetaGraphDef * m, + FindMetaGraphDef(tags, &saved_model_proto)); + *meta_graph_def = std::move(*m); + return absl::OkStatus(); +} + +absl::Status ReadSavedModelDebugInfoIfPresent( + absl::string_view export_dir, + std::unique_ptr* debug_info_proto) { + LOG(INFO) << "Reading SavedModel debug info (if present) from: " + << export_dir; + + const string debug_info_pb_path = + io::JoinPath(export_dir, "debug", "saved_model_debug_info.pb"); + TF_ASSIGN_OR_RETURN(bool debug_info_pb_exists, + internal::FileExists(Env::Default(), debug_info_pb_path)); + if (debug_info_pb_exists) { + GraphDebugInfo debug_info; + TF_RETURN_IF_ERROR( + ReadBinaryProto(Env::Default(), debug_info_pb_path, &debug_info)); + *debug_info_proto = std::make_unique(std::move(debug_info)); + } + return absl::OkStatus(); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/reader.h b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/reader.h new file mode 100644 index 0000000000000000000000000000000000000000..0212ef06f541779f48d40eb875c0237c20046d87 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/reader.h @@ -0,0 +1,59 @@ +/* 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. +==============================================================================*/ + +/// Functions to read the SavedModel proto, or parts of it. + +#ifndef TENSORFLOW_CC_SAVED_MODEL_READER_H_ +#define TENSORFLOW_CC_SAVED_MODEL_READER_H_ + +#include +#include + +#include "absl/status/statusor.h" +#include "tensorflow/core/framework/graph_debug_info.pb.h" +#include "tensorflow/core/lib/core/status.h" +#include "tensorflow/core/platform/types.h" +#include "tensorflow/core/protobuf/meta_graph.pb.h" +#include "tensorflow/core/protobuf/saved_model.pb.h" + +namespace tensorflow { +absl::Status ReadSavedModel(absl::string_view export_dir, + SavedModel* saved_model_proto); + +// Finds and returns the MetaGraphDef (within the provided SavedModel) that +// matches the given set of tags. The lifetime of the returned MetaGraphDef is +// the same as the lifetime of `saved_model_proto`. +// +// FindMetaGraphDef returns a failure status when no MetaGraphDef matches the +// provided tags. +absl::StatusOr FindMetaGraphDef( + const std::unordered_set& tags, SavedModel* saved_model_proto); + +// Reads the SavedModel proto from saved_model.pb(txt) in the given directory, +// finds the MetaGraphDef that matches the given set of tags and writes it to +// the `meta_graph_def` parameter. Returns a failure status when the SavedModel +// file does not exist or no MetaGraphDef matches the tags. +absl::Status ReadMetaGraphDefFromSavedModel( + absl::string_view export_dir, const std::unordered_set& tags, + MetaGraphDef* meta_graph_def); + +// Store debug info from the SavedModel export dir. +absl::Status ReadSavedModelDebugInfoIfPresent( + absl::string_view export_dir, + std::unique_ptr* debug_info_proto); + +} // namespace tensorflow + +#endif // TENSORFLOW_CC_SAVED_MODEL_READER_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/reader_test.cc b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/reader_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..a54d03879fe3101a5776747ee554d7859f2b855d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/reader_test.cc @@ -0,0 +1,143 @@ +/* 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 "tensorflow/cc/saved_model/reader.h" + +#include +#include "tensorflow/cc/saved_model/constants.h" +#include "tensorflow/cc/saved_model/metrics.h" +#include "tensorflow/cc/saved_model/tag_constants.h" +#include "tensorflow/core/lib/core/status.h" +#include "tensorflow/core/lib/core/status_test_util.h" +#include "tensorflow/core/lib/io/path.h" +#include "tensorflow/core/lib/strings/str_util.h" +#include "tensorflow/core/platform/path.h" +#include "tensorflow/core/platform/resource_loader.h" + +namespace tensorflow { +namespace { + +string TestDataPbTxt() { + return io::JoinPath("tensorflow", "cc", "saved_model", "testdata", + "half_plus_two_pbtxt", "00000123"); +} + +string TestDataSharded() { + return io::JoinPath("tensorflow", "cc", "saved_model", "testdata", + "half_plus_two", "00000123"); +} + +string ChunkedSavedModel() { + return io::JoinPath("tensorflow", "cc", "saved_model", "testdata", + "chunked_saved_model", "chunked_model"); +} + +string NonChunkedSavedModel() { + return io::JoinPath("tensorflow", "cc", "saved_model", "testdata", + "chunked_saved_model", "non_chunked_model"); +} + +class ReaderTest : public ::testing::Test { + protected: + ReaderTest() {} + + void CheckMetaGraphDef(const MetaGraphDef& meta_graph_def) { + const auto& tags = meta_graph_def.meta_info_def().tags(); + EXPECT_TRUE(std::find(tags.begin(), tags.end(), kSavedModelTagServe) != + tags.end()); + EXPECT_NE(meta_graph_def.meta_info_def().tensorflow_version(), ""); + EXPECT_EQ( + meta_graph_def.signature_def().at("serving_default").method_name(), + "tensorflow/serving/predict"); + } +}; + +TEST_F(ReaderTest, TagMatch) { + MetaGraphDef meta_graph_def; + + const string export_dir = GetDataDependencyFilepath(TestDataSharded()); + TF_ASSERT_OK(ReadMetaGraphDefFromSavedModel(export_dir, {kSavedModelTagServe}, + &meta_graph_def)); + CheckMetaGraphDef(meta_graph_def); +} + +TEST_F(ReaderTest, NoTagMatch) { + MetaGraphDef meta_graph_def; + + const string export_dir = GetDataDependencyFilepath(TestDataSharded()); + absl::Status st = ReadMetaGraphDefFromSavedModel(export_dir, {"missing-tag"}, + &meta_graph_def); + EXPECT_FALSE(st.ok()); + EXPECT_TRUE(absl::StrContains( + st.message(), + "Could not find meta graph def matching supplied tags: { missing-tag }")) + << st.message(); +} + +TEST_F(ReaderTest, NoTagMatchMultiple) { + MetaGraphDef meta_graph_def; + + const string export_dir = GetDataDependencyFilepath(TestDataSharded()); + absl::Status st = ReadMetaGraphDefFromSavedModel( + export_dir, {kSavedModelTagServe, "missing-tag"}, &meta_graph_def); + EXPECT_FALSE(st.ok()); + EXPECT_TRUE(absl::StrContains( + st.message(), "Could not find meta graph def matching supplied tags: ")) + << st.message(); +} + +TEST_F(ReaderTest, InvalidExportPath) { + MetaGraphDef meta_graph_def; + + const string export_dir = GetDataDependencyFilepath("missing-path"); + absl::Status st = ReadMetaGraphDefFromSavedModel( + export_dir, {kSavedModelTagServe}, &meta_graph_def); + EXPECT_FALSE(st.ok()); +} + +TEST_F(ReaderTest, ReadSavedModelDebugInfoIfPresent) { + const string export_dir = GetDataDependencyFilepath(TestDataSharded()); + std::unique_ptr debug_info_proto; + TF_ASSERT_OK(ReadSavedModelDebugInfoIfPresent(export_dir, &debug_info_proto)); +} + +TEST_F(ReaderTest, MetricsNotUpdatedFailedRead) { + MetaGraphDef meta_graph_def; + const int read_count_v1 = metrics::SavedModelReadCount("1").value(); + const int read_count_v2 = metrics::SavedModelReadCount("2").value(); + + const string export_dir = GetDataDependencyFilepath("missing-path"); + absl::Status st = + ReadMetaGraphDefFromSavedModel(export_dir, {"serve"}, &meta_graph_def); + + EXPECT_FALSE(st.ok()); + EXPECT_EQ(metrics::SavedModelReadCount("1").value(), read_count_v1); + EXPECT_EQ(metrics::SavedModelReadCount("2").value(), read_count_v2); +} + +TEST_F(ReaderTest, MetricsUpdatedSuccessfulRead) { + MetaGraphDef meta_graph_def; + const int read_count_v1 = metrics::SavedModelReadCount("1").value(); + + const string export_dir = GetDataDependencyFilepath(TestDataSharded()); + absl::Status st = + ReadMetaGraphDefFromSavedModel(export_dir, {"serve"}, &meta_graph_def); + EXPECT_EQ(metrics::SavedModelReadCount("1").value(), read_count_v1 + 1); +} + +// Placeholder for protosplitter merger merge test. + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/saved_model_bundle_lite_test.cc b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/saved_model_bundle_lite_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..6cff3f71ab09f9d02d001cbe5740aa941cde8a38 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/saved_model_bundle_lite_test.cc @@ -0,0 +1,266 @@ +/* Copyright 2016 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 "absl/status/status.h" +#include "tensorflow/cc/saved_model/constants.h" +#include "tensorflow/cc/saved_model/loader.h" +#include "tensorflow/cc/saved_model/signature_constants.h" +#include "tensorflow/cc/saved_model/tag_constants.h" +#include "tensorflow/core/example/example.pb.h" +#include "tensorflow/core/example/feature.pb.h" +#include "tensorflow/core/framework/tensor_testutil.h" +#include "tensorflow/core/lib/core/status.h" +#include "tensorflow/core/lib/core/status_test_util.h" +#include "tensorflow/core/lib/io/path.h" +#include "tensorflow/core/lib/strings/str_util.h" +#include "tensorflow/core/platform/test.h" +#include "tensorflow/core/protobuf/config.pb.h" + +namespace tensorflow { +namespace { + +constexpr char kTestDataPbTxt[] = + "cc/saved_model/testdata/half_plus_two_pbtxt/00000123"; +constexpr char kTestDataMainOp[] = + "cc/saved_model/testdata/half_plus_two_main_op/00000123"; +constexpr char kTestDataSharded[] = + "cc/saved_model/testdata/half_plus_two/00000123"; +constexpr char kTestDataInitOpV2[] = + "cc/saved_model/testdata/half_plus_two_v2/00000123"; + +class LoaderTest : public ::testing::Test { + protected: + LoaderTest() {} + + string MakeSerializedExample(float x) { + tensorflow::Example example; + auto* feature_map = example.mutable_features()->mutable_feature(); + (*feature_map)["x"].mutable_float_list()->add_value(x); + return example.SerializeAsString(); + } + + void ValidateAssets(const string& export_dir, + const SavedModelBundleLite& bundle) { + const string asset_directory = + io::JoinPath(export_dir, kSavedModelAssetsDirectory); + const string asset_filename = "foo.txt"; + const string asset_filepath = io::JoinPath(asset_directory, asset_filename); + TF_EXPECT_OK(Env::Default()->FileExists(asset_filepath)); + + std::vector path_outputs; + TF_ASSERT_OK( + bundle.GetSession()->Run({}, {"filename_tensor:0"}, {}, &path_outputs)); + ASSERT_EQ(1, path_outputs.size()); + + test::ExpectTensorEqual( + test::AsTensor({"foo.txt"}, TensorShape({})), path_outputs[0]); + } + + void CheckSavedModelBundle(const string& export_dir, + const SavedModelBundleLite& bundle) { + ValidateAssets(export_dir, bundle); + // Retrieve the regression signature from the bundle. + const auto& signature_def = bundle.GetSignatures().at("regress_x_to_y"); + + const string input_name = signature_def.inputs().at(kRegressInputs).name(); + const string output_name = + signature_def.outputs().at(kRegressOutputs).name(); + + std::vector serialized_examples; + for (float x : {0, 1, 2, 3}) { + serialized_examples.push_back(MakeSerializedExample(x)); + } + + // Validate the half plus two behavior. + Tensor input = + test::AsTensor(serialized_examples, TensorShape({4})); + std::vector outputs; + TF_ASSERT_OK(bundle.GetSession()->Run({{input_name, input}}, {output_name}, + {}, &outputs)); + ASSERT_EQ(outputs.size(), 1); + test::ExpectTensorEqual( + outputs[0], + test::AsTensor({2, 2.5, 3, 3.5}, TensorShape({4, 1}))); + + // Validate the `output_partition_graphs` is not supported. + RunOptions run_options; + run_options.set_output_partition_graphs(true); + RunMetadata run_metadata; + absl::Status s = + bundle.GetSession()->Run(run_options, {{input_name, input}}, + {output_name}, {}, &outputs, &run_metadata); + ASSERT_TRUE(absl::IsInvalidArgument(s)); + } +}; + +// Test for resource leaks related to TensorFlow session closing requirements +// when loading and unloading large numbers of SavedModelBundles. +// TODO(sukritiramesh): Increase run iterations and move outside of the test +// suite. +TEST_F(LoaderTest, ResourceLeakTest) { + SavedModelBundleLite bundle; + SessionOptions session_options; + RunOptions run_options; + + const string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded); + for (int i = 0; i < 100; ++i) { + TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir, + {kSavedModelTagServe}, &bundle)); + CheckSavedModelBundle(export_dir, bundle); + } +} + +TEST_F(LoaderTest, ExtendFailsTest) { + SavedModelBundleLite bundle; + SessionOptions session_options; + RunOptions run_options; + + const string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded); + TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir, + {kSavedModelTagServe}, &bundle)); + absl::Status s = bundle.GetSession()->Extend({}); + ASSERT_TRUE(absl::IsUnimplemented(s)); +} + +TEST_F(LoaderTest, TagMatch) { + SavedModelBundleLite bundle; + SessionOptions session_options; + RunOptions run_options; + + const string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded); + TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir, + {kSavedModelTagServe}, &bundle)); + CheckSavedModelBundle(export_dir, bundle); +} + +TEST_F(LoaderTest, NoTagMatch) { + SavedModelBundleLite bundle; + RunOptions run_options; + SessionOptions session_options; + + const string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded); + absl::Status st = LoadSavedModel(session_options, run_options, export_dir, + {"missing-tag"}, &bundle); + EXPECT_FALSE(st.ok()); + EXPECT_TRUE(absl::StrContains( + st.message(), + "Could not find meta graph def matching supplied tags: { missing-tag }")) + << st.message(); +} + +TEST_F(LoaderTest, NoTagMatchMultiple) { + SavedModelBundleLite bundle; + RunOptions run_options; + SessionOptions session_options; + + const string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded); + absl::Status st = + LoadSavedModel(session_options, run_options, export_dir, + {kSavedModelTagServe, "missing-tag"}, &bundle); + EXPECT_FALSE(st.ok()); + EXPECT_TRUE(absl::StrContains( + st.message(), "Could not find meta graph def matching supplied tags: ")) + << st.message(); +} + +TEST_F(LoaderTest, SessionCreationFailure) { + SavedModelBundleLite bundle; + // Use invalid SessionOptions to cause session creation to fail. Default + // options work, so provide an invalid value for the target field. + SessionOptions session_options; + constexpr char kInvalidTarget[] = "invalid target"; + session_options.target = kInvalidTarget; + RunOptions run_options; + + const string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded); + absl::Status st = LoadSavedModel(session_options, run_options, export_dir, + {kSavedModelTagServe}, &bundle); + EXPECT_FALSE(st.ok()); + EXPECT_TRUE(absl::StrContains(st.message(), kInvalidTarget)) << st.message(); +} + +TEST_F(LoaderTest, PbtxtFormat) { + SavedModelBundleLite bundle; + SessionOptions session_options; + RunOptions run_options; + + const string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataPbTxt); + TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir, + {kSavedModelTagServe}, &bundle)); + CheckSavedModelBundle(export_dir, bundle); +} + +TEST_F(LoaderTest, MainOpFormat) { + SavedModelBundleLite bundle; + SessionOptions session_options; + RunOptions run_options; + + const string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataMainOp); + TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir, + {kSavedModelTagServe}, &bundle)); + CheckSavedModelBundle(export_dir, bundle); +} + +TEST_F(LoaderTest, InvalidExportPath) { + SavedModelBundleLite bundle; + RunOptions run_options; + SessionOptions session_options; + + const string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), "missing-path"); + absl::Status st = LoadSavedModel(session_options, run_options, export_dir, + {kSavedModelTagServe}, &bundle); + EXPECT_FALSE(st.ok()); +} + +TEST_F(LoaderTest, MaybeSavedModelDirectory) { + // Valid SavedModel directory. + const string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded); + EXPECT_TRUE(MaybeSavedModelDirectory(export_dir)); + + // Directory that does not exist. + const string missing_export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), "missing-path"); + EXPECT_FALSE(MaybeSavedModelDirectory(missing_export_dir)); + + // Directory that exists but is an invalid SavedModel location. + const string invalid_export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), "cc/saved_model"); + EXPECT_FALSE(MaybeSavedModelDirectory(invalid_export_dir)); +} + +TEST_F(LoaderTest, SavedModelInitOpV2Format) { + SavedModelBundleLite bundle; + SessionOptions session_options; + RunOptions run_options; + + const string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataInitOpV2); + TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir, + {kSavedModelTagServe}, &bundle)); + CheckSavedModelBundle(export_dir, bundle); +} + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/saved_model_bundle_test.cc b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/saved_model_bundle_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..8cee5af670b17cec5bbf093552720787de90df14 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/saved_model_bundle_test.cc @@ -0,0 +1,405 @@ +/* Copyright 2016 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/cc/saved_model/constants.h" +#include "tensorflow/cc/saved_model/loader.h" +#include "tensorflow/cc/saved_model/metrics.h" +#include "tensorflow/cc/saved_model/reader.h" +#include "tensorflow/cc/saved_model/signature_constants.h" +#include "tensorflow/cc/saved_model/tag_constants.h" +#include "tensorflow/core/example/example.pb.h" +#include "tensorflow/core/example/feature.pb.h" +#include "tensorflow/core/framework/tensor_testutil.h" +#include "tensorflow/core/lib/core/status.h" +#include "tensorflow/core/lib/core/status_test_util.h" +#include "tensorflow/core/lib/io/path.h" +#include "tensorflow/core/lib/strings/str_util.h" +#include "tensorflow/core/platform/test.h" +#include "tensorflow/core/protobuf/meta_graph.pb.h" + +namespace tensorflow { +namespace { + +constexpr char kTestDataPbTxt[] = + "cc/saved_model/testdata/half_plus_two_pbtxt/00000123"; +constexpr char kTestDataMainOp[] = + "cc/saved_model/testdata/half_plus_two_main_op/00000123"; +constexpr char kTestDataSharded[] = + "cc/saved_model/testdata/half_plus_two/00000123"; +constexpr char kTestDataInitOpV2[] = + "cc/saved_model/testdata/half_plus_two_v2/00000123"; +constexpr char kTestDataV2DebugInfo[] = + "cc/saved_model/testdata/x_plus_y_v2_debuginfo"; +constexpr char kTestFuzzGeneratedNegativeShape[] = + "cc/saved_model/testdata/fuzz_generated/negative_shape"; +constexpr char kTestFuzzGeneratedConstWithNoValue[] = + "cc/saved_model/testdata/fuzz_generated/const_with_no_value"; +constexpr char kTestFuzzGeneratedBadNodeAttr[] = + "cc/saved_model/testdata/fuzz_generated/bad_node_attr"; +constexpr char kTestCyclicModule[] = "cc/saved_model/testdata/CyclicModule"; +constexpr char kTestSimpleV1Model[] = "cc/saved_model/testdata/SimpleV1Model"; +constexpr char kVarsAndArithmeticObjectGraph[] = + "cc/saved_model/testdata/VarsAndArithmeticObjectGraph"; +// This is the value in testdata/VarsAndArithmeticObjectGraph/fingerprint.pb +constexpr char kV2ModuleSavedModelChecksum[] = "15788619162413586750"; + +class LoaderTest : public ::testing::Test { + protected: + LoaderTest() {} + + string MakeSerializedExample(float x) { + tensorflow::Example example; + auto* feature_map = example.mutable_features()->mutable_feature(); + (*feature_map)["x"].mutable_float_list()->add_value(x); + return example.SerializeAsString(); + } + + void ValidateAssets(const string& export_dir, + const SavedModelBundle& bundle) { + const string asset_directory = + io::JoinPath(export_dir, kSavedModelAssetsDirectory); + const string asset_filename = "foo.txt"; + const string asset_filepath = io::JoinPath(asset_directory, asset_filename); + TF_EXPECT_OK(Env::Default()->FileExists(asset_filepath)); + + std::vector path_outputs; + TF_ASSERT_OK( + bundle.session->Run({}, {"filename_tensor:0"}, {}, &path_outputs)); + ASSERT_EQ(1, path_outputs.size()); + + test::ExpectTensorEqual( + test::AsTensor({"foo.txt"}, TensorShape({})), path_outputs[0]); + } + + void CheckSavedModelBundle(const string& export_dir, + const SavedModelBundle& bundle) { + ValidateAssets(export_dir, bundle); + // Retrieve the regression signature from meta graph def. + const auto& signature_def = bundle.GetSignatures().at("regress_x_to_y"); + + const string input_name = signature_def.inputs().at(kRegressInputs).name(); + const string output_name = + signature_def.outputs().at(kRegressOutputs).name(); + + std::vector serialized_examples; + for (float x : {0, 1, 2, 3}) { + serialized_examples.push_back(MakeSerializedExample(x)); + } + + // Validate the half plus two behavior. + Tensor input = + test::AsTensor(serialized_examples, TensorShape({4})); + std::vector outputs; + TF_ASSERT_OK(bundle.session->Run({{input_name, input}}, {output_name}, {}, + &outputs)); + ASSERT_EQ(outputs.size(), 1); + test::ExpectTensorEqual( + outputs[0], + test::AsTensor({2, 2.5, 3, 3.5}, TensorShape({4, 1}))); + } +}; + +// Test for resource leaks related to TensorFlow session closing requirements +// when loading and unloading large numbers of SavedModelBundles. +// TODO(sukritiramesh): Increase run iterations and move outside of the test +// suite. +TEST_F(LoaderTest, ResourceLeakTest) { + SavedModelBundle bundle; + SessionOptions session_options; + RunOptions run_options; + + const string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded); + for (int i = 0; i < 100; ++i) { + TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir, + {kSavedModelTagServe}, &bundle)); + CheckSavedModelBundle(export_dir, bundle); + } +} + +TEST_F(LoaderTest, TagMatch) { + SavedModelBundle bundle; + SessionOptions session_options; + RunOptions run_options; + + const string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded); + TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir, + {kSavedModelTagServe}, &bundle)); + CheckSavedModelBundle(export_dir, bundle); +} + +TEST_F(LoaderTest, ReadMetaGraphFromSavedModel) { + SavedModelBundle bundle; + SessionOptions session_options; + RunOptions run_options; + + const string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded); + TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir, + {kSavedModelTagServe}, &bundle)); + MetaGraphDef actual_metagraph; + TF_ASSERT_OK(ReadMetaGraphDefFromSavedModel(export_dir, {kSavedModelTagServe}, + &actual_metagraph)); + EXPECT_EQ(actual_metagraph.DebugString(), + bundle.meta_graph_def.DebugString()); +} + +TEST_F(LoaderTest, RestoreSession) { + SavedModelBundle bundle; + SessionOptions session_options; + RunOptions run_options; + + const string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded); + TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir, + {kSavedModelTagServe}, &bundle)); + + SavedModelBundle actual_bundle; + const std::unordered_set tags = {kSavedModelTagServe}; + TF_ASSERT_OK(ReadMetaGraphDefFromSavedModel(export_dir, tags, + &actual_bundle.meta_graph_def)); + TF_ASSERT_OK(LoadMetagraphIntoSession( + session_options, actual_bundle.meta_graph_def, &actual_bundle.session)); + TF_ASSERT_OK(RestoreSession(run_options, actual_bundle.meta_graph_def, + export_dir, &actual_bundle.session)); + CheckSavedModelBundle(export_dir, actual_bundle); +} + +TEST_F(LoaderTest, NoTagMatch) { + SavedModelBundle bundle; + RunOptions run_options; + SessionOptions session_options; + + const string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded); + absl::Status st = LoadSavedModel(session_options, run_options, export_dir, + {"missing-tag"}, &bundle); + EXPECT_FALSE(st.ok()); + EXPECT_TRUE(absl::StrContains( + st.message(), + "Could not find meta graph def matching supplied tags: { missing-tag }")) + << st.message(); +} + +TEST_F(LoaderTest, NoTagMatchMultiple) { + SavedModelBundle bundle; + RunOptions run_options; + SessionOptions session_options; + + const string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded); + absl::Status st = + LoadSavedModel(session_options, run_options, export_dir, + {kSavedModelTagServe, "missing-tag"}, &bundle); + EXPECT_FALSE(st.ok()); + EXPECT_TRUE(absl::StrContains( + st.message(), "Could not find meta graph def matching supplied tags: ")) + << st.message(); +} + +TEST_F(LoaderTest, SessionCreationFailure) { + SavedModelBundle bundle; + // Use invalid SessionOptions to cause session creation to fail. Default + // options work, so provide an invalid value for the target field. + SessionOptions session_options; + constexpr char kInvalidTarget[] = "invalid target"; + session_options.target = kInvalidTarget; + RunOptions run_options; + + const string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded); + absl::Status st = LoadSavedModel(session_options, run_options, export_dir, + {kSavedModelTagServe}, &bundle); + EXPECT_FALSE(st.ok()); + EXPECT_TRUE(absl::StrContains(st.message(), kInvalidTarget)) << st.message(); +} + +TEST_F(LoaderTest, PbtxtFormat) { + SavedModelBundle bundle; + SessionOptions session_options; + RunOptions run_options; + + const string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataPbTxt); + TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir, + {kSavedModelTagServe}, &bundle)); + CheckSavedModelBundle(export_dir, bundle); +} + +TEST_F(LoaderTest, MainOpFormat) { + SavedModelBundle bundle; + SessionOptions session_options; + RunOptions run_options; + + const string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataMainOp); + TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir, + {kSavedModelTagServe}, &bundle)); + CheckSavedModelBundle(export_dir, bundle); +} + +TEST_F(LoaderTest, InvalidExportPath) { + SavedModelBundle bundle; + RunOptions run_options; + SessionOptions session_options; + + const string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), "missing-path"); + absl::Status st = LoadSavedModel(session_options, run_options, export_dir, + {kSavedModelTagServe}, &bundle); + EXPECT_FALSE(st.ok()); +} + +TEST_F(LoaderTest, MaybeSavedModelDirectory) { + // Valid SavedModel directory. + const string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded); + EXPECT_TRUE(MaybeSavedModelDirectory(export_dir)); + + // Directory that does not exist. + const string missing_export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), "missing-path"); + EXPECT_FALSE(MaybeSavedModelDirectory(missing_export_dir)); + + // Directory that exists but is an invalid SavedModel location. + const string invalid_export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), "cc/saved_model"); + EXPECT_FALSE(MaybeSavedModelDirectory(invalid_export_dir)); +} + +TEST_F(LoaderTest, SavedModelInitOpV2Format) { + SavedModelBundle bundle; + SessionOptions session_options; + RunOptions run_options; + + const string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataInitOpV2); + TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir, + {kSavedModelTagServe}, &bundle)); + CheckSavedModelBundle(export_dir, bundle); +} + +TEST_F(LoaderTest, SavedModelV2DebugInfo) { + SavedModelBundle bundle; + SessionOptions session_options; + RunOptions run_options; + + const string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataV2DebugInfo); + TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir, + {kSavedModelTagServe}, &bundle)); + + // This SavedModel has debug info, so we should have loaded it. + EXPECT_NE(bundle.debug_info.get(), nullptr); +} + +TEST_F(LoaderTest, NegativeShapeDimension) { + SavedModelBundle bundle; + RunOptions run_options; + SessionOptions session_options; + + const string export_dir = io::JoinPath(testing::TensorFlowSrcRoot(), + kTestFuzzGeneratedNegativeShape); + absl::Status st = LoadSavedModel(session_options, run_options, export_dir, + {kSavedModelTagServe}, &bundle); + EXPECT_FALSE(st.ok()); + EXPECT_NE(st.message().find("initializes from a tensor with -1 elements"), + std::string::npos); +} + +TEST_F(LoaderTest, ConstNoValue) { + SavedModelBundle bundle; + RunOptions run_options; + SessionOptions session_options; + + const string export_dir = io::JoinPath(testing::TensorFlowSrcRoot(), + kTestFuzzGeneratedConstWithNoValue); + absl::Status st = LoadSavedModel(session_options, run_options, export_dir, + {kSavedModelTagServe}, &bundle); + EXPECT_FALSE(st.ok()); + EXPECT_NE(st.message().find("constant tensor but no value has been provided"), + std::string::npos); +} + +TEST_F(LoaderTest, BadNodeAttr) { + SavedModelBundle bundle; + RunOptions run_options; + SessionOptions session_options; + + const string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), kTestFuzzGeneratedBadNodeAttr); + absl::Status st = LoadSavedModel(session_options, run_options, export_dir, + {kSavedModelTagServe}, &bundle); + EXPECT_FALSE(st.ok()); + EXPECT_NE(st.message().find("constant tensor but no value has been provided"), + std::string::npos); +} + +TEST_F(LoaderTest, UpdateMetricsV2) { + SavedModelBundle bundle; + SessionOptions session_options; + RunOptions run_options; + const string kCCLoadLabel = "cc_load"; + + const int read_count_v2 = metrics::SavedModelReadCount("2").value(); + const int api_count = metrics::SavedModelReadApi(kCCLoadLabel).value(); + const string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), kTestCyclicModule); + TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir, + {kSavedModelTagServe}, &bundle)); + + EXPECT_EQ(metrics::SavedModelReadCount("2").value(), read_count_v2 + 1); + EXPECT_EQ(metrics::SavedModelReadApi(kCCLoadLabel).value(), api_count + 1); +} + +TEST_F(LoaderTest, UpdateMetricsV1) { + SavedModelBundle bundle; + SessionOptions session_options; + RunOptions run_options; + const string kCCLoadLabel = "cc_load"; + + const int read_count_v1 = metrics::SavedModelReadCount("1").value(); + const int read_count_v2 = metrics::SavedModelReadCount("2").value(); + + const int api_count = metrics::SavedModelReadApi(kCCLoadLabel).value(); + const string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), kTestSimpleV1Model); + TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir, + {kSavedModelTagServe}, &bundle)); + + EXPECT_EQ(metrics::SavedModelReadCount("1").value(), read_count_v1 + 1); + EXPECT_EQ(metrics::SavedModelReadCount("2").value(), read_count_v2); + EXPECT_EQ(metrics::SavedModelReadApi(kCCLoadLabel).value(), api_count + 1); +} + +TEST_F(LoaderTest, UpdateFingerprintMetrics) { + SavedModelBundle bundle; + SessionOptions session_options; + RunOptions run_options; + + const string export_dir = + io::JoinPath(testing::TensorFlowSrcRoot(), kVarsAndArithmeticObjectGraph); + TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir, + {kSavedModelTagServe}, &bundle)); + + EXPECT_EQ(metrics::SavedModelReadPath().value(), export_dir); + + EXPECT_EQ(metrics::SavedModelReadFingerprint().value(), + kV2ModuleSavedModelChecksum); +} + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/saved_model_fuzz.cc b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/saved_model_fuzz.cc new file mode 100644 index 0000000000000000000000000000000000000000..86e046ff8824061bd392d91ece7fc874eca4798e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/saved_model_fuzz.cc @@ -0,0 +1,44 @@ +/* Copyright 2023 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 "fuzztest/fuzztest.h" +#include "tensorflow/cc/saved_model/constants.h" +#include "tensorflow/cc/saved_model/loader.h" +#include "tensorflow/cc/saved_model/tag_constants.h" +#include "tensorflow/core/protobuf/saved_model.pb.h" +#include "tensorflow/core/public/session_options.h" + +namespace tensorflow::fuzzing { +namespace { + +void FuzzLoadSavedModel(const SavedModel& model) { + SavedModelBundleLite bundle; + SessionOptions session_options; + RunOptions run_options; + + string export_dir = "ram://"; + TF_CHECK_OK(tsl::WriteBinaryProto(tensorflow::Env::Default(), + export_dir + kSavedModelFilenamePb, model)); + + LoadSavedModel(session_options, run_options, export_dir, + {kSavedModelTagServe}, &bundle) + .IgnoreError(); +} +FUZZ_TEST(SavedModelFuzz, FuzzLoadSavedModel); + +} // namespace +} // namespace tensorflow::fuzzing diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/signature_constants.h b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/signature_constants.h new file mode 100644 index 0000000000000000000000000000000000000000..42ee9df5d0eabe85dcd480f0650947e8528451e5 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/signature_constants.h @@ -0,0 +1,69 @@ +/* Copyright 2016 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. +==============================================================================*/ + +#ifndef TENSORFLOW_CC_SAVED_MODEL_SIGNATURE_CONSTANTS_H_ +#define TENSORFLOW_CC_SAVED_MODEL_SIGNATURE_CONSTANTS_H_ + +namespace tensorflow { + +/// Key in the signature def map for `default` serving signatures. The default +/// signature is used in inference requests where a specific signature was not +/// specified. +static constexpr char kDefaultServingSignatureDefKey[] = "serving_default"; + +//////////////////////////////////////////////////////////////////////////////// +/// Classification API constants. + +/// Classification inputs. +static constexpr char kClassifyInputs[] = "inputs"; + +/// Classification method name used in a SignatureDef. +static constexpr char kClassifyMethodName[] = "tensorflow/serving/classify"; + +/// Classification classes output. +static constexpr char kClassifyOutputClasses[] = "classes"; + +/// Classification scores output. +static constexpr char kClassifyOutputScores[] = "scores"; + +//////////////////////////////////////////////////////////////////////////////// +/// Predict API constants. + +/// Predict inputs. +static constexpr char kPredictInputs[] = "inputs"; + +/// Predict method name used in a SignatureDef. +static constexpr char kPredictMethodName[] = "tensorflow/serving/predict"; + +/// Predict outputs. +static constexpr char kPredictOutputs[] = "outputs"; + +//////////////////////////////////////////////////////////////////////////////// +/// Regression API constants. + +/// Regression inputs. +static constexpr char kRegressInputs[] = "inputs"; + +/// Regression method name used in a SignatureDef. +static constexpr char kRegressMethodName[] = "tensorflow/serving/regress"; + +/// Regression outputs. +static constexpr char kRegressOutputs[] = "outputs"; + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace tensorflow + +#endif // TENSORFLOW_CC_SAVED_MODEL_SIGNATURE_CONSTANTS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/tag_constants.h b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/tag_constants.h new file mode 100644 index 0000000000000000000000000000000000000000..cbaca19f57a8721bb83ee28f78fb6a322ae5cab7 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/tag_constants.h @@ -0,0 +1,35 @@ +/* Copyright 2016 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. +==============================================================================*/ + +#ifndef TENSORFLOW_CC_SAVED_MODEL_TAG_CONSTANTS_H_ +#define TENSORFLOW_CC_SAVED_MODEL_TAG_CONSTANTS_H_ + +namespace tensorflow { + +/// Tag for the `gpu` graph. +constexpr char kSavedModelTagGpu[] = "gpu"; + +/// Tag for the `tpu` graph. +constexpr char kSavedModelTagTpu[] = "tpu"; + +/// Tag for the `serving` graph. +constexpr char kSavedModelTagServe[] = "serve"; + +/// Tag for the `training` graph. +constexpr char kSavedModelTagTrain[] = "train"; + +} // namespace tensorflow + +#endif // TENSORFLOW_CC_SAVED_MODEL_TAG_CONSTANTS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/test_utils.h b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/test_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..1ce980895432e344c5903b5bd9a769436639c2da --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/test_utils.h @@ -0,0 +1,53 @@ +/* Copyright 2021 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. +==============================================================================*/ +#ifndef TENSORFLOW_CC_SAVED_MODEL_TEST_UTILS_H_ +#define TENSORFLOW_CC_SAVED_MODEL_TEST_UTILS_H_ + +#include +#include + +#include "tensorflow/core/platform/protobuf.h" +#include "tensorflow/core/platform/test.h" + +namespace tensorflow::saved_model { + +// TODO(b/229726259) Switch to OSS version after it's available. +// Simple implementation of a proto matcher comparing string representations. +// Only works as ShapeProto's textual representation is deterministic. +class ProtoStringMatcher { + public: + explicit ProtoStringMatcher(const tensorflow::protobuf::Message& expected) + : expected_(expected.DebugString()) {} + + template + bool MatchAndExplain(const Message& p, + ::testing::MatchResultListener*) const { + return p.DebugString() == expected_; + } + + void DescribeTo(::std::ostream* os) const { *os << expected_; } + void DescribeNegationTo(::std::ostream* os) const { + *os << "not equal to expected message: " << expected_; + } + + private: + const std::string expected_; +}; + +inline ::testing::PolymorphicMatcher EqualsProto( + const tensorflow::protobuf::Message& x) { + return ::testing::MakePolymorphicMatcher(ProtoStringMatcher(x)); +} + +} // namespace tensorflow::saved_model + +#endif // TENSORFLOW_CC_SAVED_MODEL_TEST_UTILS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/AssetModule/assets/test_asset.txt b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/AssetModule/assets/test_asset.txt new file mode 100644 index 0000000000000000000000000000000000000000..0f7662808df495ceb05d53f3891879c3f58e1d8e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/AssetModule/assets/test_asset.txt @@ -0,0 +1 @@ +TEST ASSET FILE CONTENTS diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/AssetModule/saved_model.pb b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/AssetModule/saved_model.pb new file mode 100644 index 0000000000000000000000000000000000000000..4a4357f64e985b334c757bbe853c3fee5687b2b9 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/AssetModule/saved_model.pb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1fd1d22913556f2cab68ebf061c6f6ce2621acf431089882612e20502618d362 +size 8248 diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/AssetModule/variables/variables.data-00000-of-00001 b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/AssetModule/variables/variables.data-00000-of-00001 new file mode 100644 index 0000000000000000000000000000000000000000..4105bb4c15e473b2f00dd3ae3236c7ef8c2328bf Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/AssetModule/variables/variables.data-00000-of-00001 differ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/AssetModule/variables/variables.index b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/AssetModule/variables/variables.index new file mode 100644 index 0000000000000000000000000000000000000000..3d903ca79a2904080c70501148466371b2e96f59 Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/AssetModule/variables/variables.index differ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/CyclicModule/debug/saved_model_debug_info.pb b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/CyclicModule/debug/saved_model_debug_info.pb new file mode 100644 index 0000000000000000000000000000000000000000..6056022db8c95b27bd29b73b37ed95b1fe02d177 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/CyclicModule/debug/saved_model_debug_info.pb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7ea2b79ed9fe236ef4db0cf67ec8598ef7d6cb60f56079f79d7211154f18abe0 +size 3125 diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/CyclicModule/saved_model.pb b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/CyclicModule/saved_model.pb new file mode 100644 index 0000000000000000000000000000000000000000..e3da4697e5184ae4c8ff9a7a8282169930719d5d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/CyclicModule/saved_model.pb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:82ef22f55a11772eac73e0c13a053f6d42b9aadbd5c53f7a144cc40fa0b00d06 +size 7016 diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/CyclicModule/variables/variables.data-00000-of-00001 b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/CyclicModule/variables/variables.data-00000-of-00001 new file mode 100644 index 0000000000000000000000000000000000000000..5aae628729461cadf883b778f1a488ca0754572b Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/CyclicModule/variables/variables.data-00000-of-00001 differ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/CyclicModule/variables/variables.index b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/CyclicModule/variables/variables.index new file mode 100644 index 0000000000000000000000000000000000000000..3fc2cec2b60b94e2da079b69e8d035962dd297cd Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/CyclicModule/variables/variables.index differ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/OptimizerSlotVariableModule/saved_model.pb b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/OptimizerSlotVariableModule/saved_model.pb new file mode 100644 index 0000000000000000000000000000000000000000..dcdba97030882a958fcdaf68296180e97b6a8dfb --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/OptimizerSlotVariableModule/saved_model.pb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:986042bb43617f2e8c1255ca6b722dd0d61ddd1e3e48b3bf1756d9852feeba0c +size 14012 diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/OptimizerSlotVariableModule/variables/variables.data-00000-of-00001 b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/OptimizerSlotVariableModule/variables/variables.data-00000-of-00001 new file mode 100644 index 0000000000000000000000000000000000000000..7c0ec1ca1a5c1623323aae9941f1c7b99a5cf51c Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/OptimizerSlotVariableModule/variables/variables.data-00000-of-00001 differ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/OptimizerSlotVariableModule/variables/variables.index b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/OptimizerSlotVariableModule/variables/variables.index new file mode 100644 index 0000000000000000000000000000000000000000..baf09619c03319f7145fd71c5bb4e6b92b6b8713 Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/OptimizerSlotVariableModule/variables/variables.index differ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/SimpleV1Model/saved_model.pb b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/SimpleV1Model/saved_model.pb new file mode 100644 index 0000000000000000000000000000000000000000..cd0f6edef92d8bbdd4266acf738e3b6be5cdbade --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/SimpleV1Model/saved_model.pb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0f583928af62c558954b7e1517b59dc8a278766dc783984429c5898ca5e86e7e +size 5139 diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/SimpleV1Model/variables/variables.data-00000-of-00001 b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/SimpleV1Model/variables/variables.data-00000-of-00001 new file mode 100644 index 0000000000000000000000000000000000000000..9c12e8adf98ba3c9bdad8481352f1dac07f3df52 Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/SimpleV1Model/variables/variables.data-00000-of-00001 differ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/SimpleV1Model/variables/variables.index b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/SimpleV1Model/variables/variables.index new file mode 100644 index 0000000000000000000000000000000000000000..421556d9b47d537992d7e1dbc43e590e808801f4 Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/SimpleV1Model/variables/variables.index differ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/StaticHashTableModule/assets/static_hashtable_asset.txt b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/StaticHashTableModule/assets/static_hashtable_asset.txt new file mode 100644 index 0000000000000000000000000000000000000000..46416a6918a44aac601c9d054fa6e6973f4dc98a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/StaticHashTableModule/assets/static_hashtable_asset.txt @@ -0,0 +1,4 @@ +foo +bar +baz +wombat diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/StaticHashTableModule/saved_model.pb b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/StaticHashTableModule/saved_model.pb new file mode 100644 index 0000000000000000000000000000000000000000..9967e9d9e7ee1ccd36a60ced2864fa15979705fd --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/StaticHashTableModule/saved_model.pb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0a749ec112922fb36ce41ad3e26f4ef96075cf1fcc99c243c05bb1ac4ad617e6 +size 14204 diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/StaticHashTableModule/variables/variables.data-00000-of-00001 b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/StaticHashTableModule/variables/variables.data-00000-of-00001 new file mode 100644 index 0000000000000000000000000000000000000000..f6d62d9a51c7440c26c963708ac9ea5eb5fbed66 Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/StaticHashTableModule/variables/variables.data-00000-of-00001 differ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/StaticHashTableModule/variables/variables.index b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/StaticHashTableModule/variables/variables.index new file mode 100644 index 0000000000000000000000000000000000000000..df6c85e578347341989e42a877ca8d166bc92972 Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/StaticHashTableModule/variables/variables.index differ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/VarsAndArithmeticObjectGraph/debug/saved_model_debug_info.pb b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/VarsAndArithmeticObjectGraph/debug/saved_model_debug_info.pb new file mode 100644 index 0000000000000000000000000000000000000000..118bd6954b799ffd868005774f6b20cb64ec4b22 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/VarsAndArithmeticObjectGraph/debug/saved_model_debug_info.pb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1a98391cec6662213b643b5a54f225a57ea022403025f4c8aa41c90739d01ddd +size 7109 diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/VarsAndArithmeticObjectGraph/fingerprint.pb b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/VarsAndArithmeticObjectGraph/fingerprint.pb new file mode 100644 index 0000000000000000000000000000000000000000..a28f25d449c62d72367fd99ca8637ef3bf97b212 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/VarsAndArithmeticObjectGraph/fingerprint.pb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:edb81257576eaff491e57d28ab7ef542649ad8034e53dd9adc167739f398a16c +size 57 diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/VarsAndArithmeticObjectGraph/saved_model.pb b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/VarsAndArithmeticObjectGraph/saved_model.pb new file mode 100644 index 0000000000000000000000000000000000000000..545e968fb059998831dcb3f29ef8fab46d969b07 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/VarsAndArithmeticObjectGraph/saved_model.pb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:aa7d80f2bde3e12d15303d06f32b6d781207e56e542d1c281c1bb60529c0b711 +size 12498 diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/VarsAndArithmeticObjectGraph/variables/variables.data-00000-of-00001 b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/VarsAndArithmeticObjectGraph/variables/variables.data-00000-of-00001 new file mode 100644 index 0000000000000000000000000000000000000000..307f83f85c4ef7c106c87961ac09b68989f8ba80 Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/VarsAndArithmeticObjectGraph/variables/variables.data-00000-of-00001 differ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/VarsAndArithmeticObjectGraph/variables/variables.index b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/VarsAndArithmeticObjectGraph/variables/variables.index new file mode 100644 index 0000000000000000000000000000000000000000..e025dc724eac6ac99bfe951ad82a2ad226571e73 Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/VarsAndArithmeticObjectGraph/variables/variables.index differ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/bert1/saved_model.pb b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/bert1/saved_model.pb new file mode 100644 index 0000000000000000000000000000000000000000..1c1f48c52fadcaaf6fdc6121c0e9c27eaa19a1a9 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/bert1/saved_model.pb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:64e5da474f824a8c67c8c8750c95a0bef62c3b41e00ef908e4f1727f2edf2943 +size 1658033 diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/bert2/saved_model.pb b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/bert2/saved_model.pb new file mode 100644 index 0000000000000000000000000000000000000000..9756f0fc891e6fdbb7d53890ae9335c80a11eca1 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/bert2/saved_model.pb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:603a16687aed4992ef3b950f0ccaa0b77ff23782c6e8a910eb6a97b2bfb7468f +size 1659513 diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/chunked_saved_model/chunked_model/saved_model.cpb b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/chunked_saved_model/chunked_model/saved_model.cpb new file mode 100644 index 0000000000000000000000000000000000000000..21c31e136a1ac482a5f1dc99e4b8bd2f59d86b57 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/chunked_saved_model/chunked_model/saved_model.cpb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d4864a8f03ce7dbcfb9558c8c8c6173e5f341cfa4681a6d74b2d28cdbf9f65b0 +size 171472 diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/chunked_saved_model/chunked_model/saved_model.pbtxt b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/chunked_saved_model/chunked_model/saved_model.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..a3d5f69ed97c5b2794519121f660afb7d145faa9 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/chunked_saved_model/chunked_model/saved_model.pbtxt @@ -0,0 +1,2063 @@ +saved_model_schema_version: 1 +meta_graphs { + meta_info_def { + stripped_op_list { + op { + name: "Const" + output_arg { + name: "output" + type_attr: "dtype" + } + attr { + name: "value" + type: "tensor" + } + attr { + name: "dtype" + type: "type" + } + } + op { + name: "Identity" + input_arg { + name: "input" + type_attr: "T" + } + output_arg { + name: "output" + type_attr: "T" + } + attr { + name: "T" + type: "type" + } + } + op { + name: "MergeV2Checkpoints" + input_arg { + name: "checkpoint_prefixes" + type: DT_STRING + } + input_arg { + name: "destination_prefix" + type: DT_STRING + } + attr { + name: "delete_old_dirs" + type: "bool" + default_value { + b: true + } + } + attr { + name: "allow_missing_files" + type: "bool" + default_value { + b: false + } + } + is_stateful: true + } + op { + name: "NoOp" + } + op { + name: "Pack" + input_arg { + name: "values" + type_attr: "T" + number_attr: "N" + } + output_arg { + name: "output" + type_attr: "T" + } + attr { + name: "N" + type: "int" + has_minimum: true + minimum: 1 + } + attr { + name: "T" + type: "type" + } + attr { + name: "axis" + type: "int" + default_value { + i: 0 + } + } + } + op { + name: "PartitionedCall" + input_arg { + name: "args" + type_list_attr: "Tin" + } + output_arg { + name: "output" + type_list_attr: "Tout" + } + attr { + name: "Tin" + type: "list(type)" + has_minimum: true + } + attr { + name: "Tout" + type: "list(type)" + has_minimum: true + } + attr { + name: "f" + type: "func" + } + attr { + name: "config" + type: "string" + default_value { + s: "" + } + } + attr { + name: "config_proto" + type: "string" + default_value { + s: "" + } + } + attr { + name: "executor_type" + type: "string" + default_value { + s: "" + } + } + } + op { + name: "Placeholder" + output_arg { + name: "output" + type_attr: "dtype" + } + attr { + name: "dtype" + type: "type" + } + attr { + name: "shape" + type: "shape" + default_value { + shape { + unknown_rank: true + } + } + } + } + op { + name: "RestoreV2" + input_arg { + name: "prefix" + type: DT_STRING + } + input_arg { + name: "tensor_names" + type: DT_STRING + } + input_arg { + name: "shape_and_slices" + type: DT_STRING + } + output_arg { + name: "tensors" + type_list_attr: "dtypes" + } + attr { + name: "dtypes" + type: "list(type)" + has_minimum: true + minimum: 1 + } + is_stateful: true + } + op { + name: "SaveV2" + input_arg { + name: "prefix" + type: DT_STRING + } + input_arg { + name: "tensor_names" + type: DT_STRING + } + input_arg { + name: "shape_and_slices" + type: DT_STRING + } + input_arg { + name: "tensors" + type_list_attr: "dtypes" + } + attr { + name: "dtypes" + type: "list(type)" + has_minimum: true + minimum: 1 + } + is_stateful: true + } + op { + name: "Select" + input_arg { + name: "condition" + type: DT_BOOL + } + input_arg { + name: "t" + type_attr: "T" + } + input_arg { + name: "e" + type_attr: "T" + } + output_arg { + name: "output" + type_attr: "T" + } + attr { + name: "T" + type: "type" + } + } + op { + name: "ShardedFilename" + input_arg { + name: "basename" + type: DT_STRING + } + input_arg { + name: "shard" + type: DT_INT32 + } + input_arg { + name: "num_shards" + type: DT_INT32 + } + output_arg { + name: "filename" + type: DT_STRING + } + } + op { + name: "StatefulPartitionedCall" + input_arg { + name: "args" + type_list_attr: "Tin" + } + output_arg { + name: "output" + type_list_attr: "Tout" + } + attr { + name: "Tin" + type: "list(type)" + has_minimum: true + } + attr { + name: "Tout" + type: "list(type)" + has_minimum: true + } + attr { + name: "f" + type: "func" + } + attr { + name: "config" + type: "string" + default_value { + s: "" + } + } + attr { + name: "config_proto" + type: "string" + default_value { + s: "" + } + } + attr { + name: "executor_type" + type: "string" + default_value { + s: "" + } + } + is_stateful: true + is_distributed_communication: true + } + op { + name: "StaticRegexFullMatch" + input_arg { + name: "input" + type: DT_STRING + } + output_arg { + name: "output" + type: DT_BOOL + } + attr { + name: "pattern" + type: "string" + } + } + op { + name: "StringJoin" + input_arg { + name: "inputs" + type: DT_STRING + number_attr: "N" + } + output_arg { + name: "output" + type: DT_STRING + } + attr { + name: "N" + type: "int" + has_minimum: true + } + attr { + name: "separator" + type: "string" + default_value { + s: "" + } + } + } + } + tags: "serve" + tensorflow_version: "2.14.0" + tensorflow_git_version: "unknown" + stripped_default_attrs: true + } + graph_def { + node { + name: "Const" + op: "Const" + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: 150 + } + dim { + size: 150 + } + } + } + } + } + attr { + key: "dtype" + value { + type: DT_DOUBLE + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_DOUBLE + tensor_shape { + dim { + size: 150 + } + dim { + size: 150 + } + } + tensor_content: "`\250S-\322\210\346?\021\261\230\257\371\257\340?\301\244\352\345g\221\346?\006\231\202C\203#\342?\241\001AY7\240\355?\031\275\343\257\231\272\340?X6B\007s\006\306?\364\216\221\020\241\'\303?\022\273\025\211)\010\336?\330[\324\264\201\231\323?\357\010\212\342\230\037\347?\240\272\301Fe\033\344?\3029J\332\352.\343?\2648\250\3644\034\317?\206\341\225\036\302\331\330?\200t\364\331Z\327\214?\364\341\007\n\201\002\350?b2\254e\243\006\327?\240ra\\\004#\255?\016\247\200\0006\362\334?z\203\247\034\253d\354?\320@pj\240\337\325?JM\251<\340\370\334?JQ{\352=w\330?\214\317D\r\267\314\336?\020\343!\237\026\300\336?\350\333\235\231r6\305?3l\223\335\343\323\347?\300z\344\275j\300\251?\201\345\321\227P\\\344?\031\030$\225\031\302\356?t\351d4\266D\315?\362\306m\265\254\037\354?\206\361\305F\324\002\343?\337\224\201\217\252\211\341?\302T\205x\271\205\345?!D7\244w\311\352?>U\244\250\333\260\330?@\2650\377 \360\271?\200#{\327H\032\206?\224\341\252\022\016\010\317?\024x\360l\215k\356?\300\263\234tb\230\206?Hbo\003\220<\320?`X\350\222H\202\256?\355\337\272\376Re\355?d\222bo}\365\340?\010\340*H\3636\352?\214;\2522\221\\\311?\305+\264\333\236\326\354?\225\257\272\270\242\026\342?\257\341V\245j\325\351?\300>\220\350\235\341\272?\226\\\n6\231_\335?h\323\323\200\220\366\344?\310P\000\351\353\255\330?i\233a\344\352\356\356?\'\365\371\206\330l\340?Ne\357\327\3001\323?\240\216\327_u8\260?x\224\220U\314\240\320?\256\177\267\346\234\n\334?+\302\250>\251\247\340?\214\352\273\013\263\274\322?\002\3351R\334v\347?J\343e\r\342\232\325?\230(\024\2345p\327?\362q\251\300;\346\345?Q\033\263\"y\320\346?\270\r\327\200i\347\302?\352\252\376i\252s\340?\252&\177,\253\r\332?*R]]\016\032\344?!\206{\017\226\026\353?\212S!\333!\247\325?\373U%\354\246\351\347?\230s\00607\212\306?2\355\376\323\343\315\325?\330%\252\212\332\313\333?\370\032.\244\267o\344?\224\212\324\371\264\300\331?\307\363\241\366\322\237\340?\250\275\000S9\373\301?\2449\254\237\025\323\316?\266\360mQ\264\016\327?Tof\271\267l\354?\341\234\036\301\311\314\347?\320\352FQ\tv\264?\005\250\300b\376\203\342?\000\026\366\351\034\372\335?\253\224\337 \370\266\343?\003\342nG\263\274\341?\214\233\200\032\264\331\351?o\'\253#2{\346?N\252\240p\323\236\343?\324\366\311\364\007\367\301?Z\234\361W6/\335?\"\020\002\307\260\357\344?\2562a\306\203s\323?PEG\003L\213\256?8\035\275$\253n\276?\310\305\315\306\n=\310?8\267\034\341w\315\314?p0\363\373x\205\276?Me\253\226\332\252\345?\3168\310M)\367\325?\036DE\220\344Z\326?\240\321G3\256x\250?r\233 \\$\374\323?ZN\367,\334d\357?l\212b\024\242\350\333?\366\213\206\005M4\342?a\254\273\355E?\357?\240u\026a>\374\271?\025\023\354G\305I\344?\177\340\332\272\237\230\346?2JJ\010>\264\350?d\032\276\263\350k\324?z\224\346\303\244j\327?8\205\237\244V\376\355?\246\371\363b#\r\321?`I\342\023\\4\307?\340\220\\c\307[\347?\004m\312\301\370\214\331?R\372E\331\034\206\336?\226c\2617K\360\351?\270\312\214\272\026\274\267?a\276\370az\007\353?\345n\364\321$[\345?b\366c\333\274\325\335?\021\n\232\0045O\342?`vl%\376\356\274?Y\254\363\014\256`\354?k\2025\t\266/\341?l\211\245\325\014{\344?&\350\033W|%\347?\306\260qD\274!\327?\0104c\000\334h\344?!\010\322L\364\001\356?\301\357:\023\302\304\351?\241ip1/\256\351?4H2j\352\321\326?FVN\224\335\273\324?oc\240=V\236\350?U\212h\212Wf\357?\350v\356M\325u\341?\010]\344\016\200Y\310?\370\355\271\371\217m\332?(\231\312\2514\270\263?\333w\367\036\333\224\342?0\375M\240\326\301\355?4\364\311\317\342\313\331?\200L\321\356O\356\237?\202J\032#Uv\345?6\206~j\010\357\345?@\343|\003\217,\261?\020\362\314o\n\226\346?t\030\024A{\013\303?\n%\230NQ\034\356?\222^I\033tN\345?\356\037\364T\177\363\323?`\322\277)\207\250\242?\214\325\233Wp\022\303?\303\360\213\241\311\200\351?\336\025\020\265\'\372\322?\374\225\257\'\273_\326?\314\006\010v\223\037\302?4\340\213\366\255\271\326?j<\335\005\005|\327?`J2\016\336\357\235?H\t\274\323\023\032\331?\000DVCL\033\315?\213\256\240\007\004\n\347?V=I\277\303\033\335?\2170\n\256\200K\355?@\231\257,\036\021\335?\370\345[7j#\306?\374\215\r\031iE\321?\374\374\357\235\263\'\321?\221\203\003\023\306?\354? 7\250\016g\013\274?h\250=\321d/\356?\031~\374V\016\233\340?t<\247~\313F\346?X\031\353\2008\252\353?P\364\033X\232\360\303?+((\241\023\360\341?\363\305\217\013&\235\342?\372\333\242\005~\025\345?\rO\277fe\211\350?\267\353\313B\303\315\351?\273\333K\251Y`\357?\332\324\375\367\310\034\345?\000\347\030\311\251\255\245?\235\231\376\010\'l\341?\317W\231\207q\366\342?uN7\336\351\017\344?$z\334\326I\013\337?~\361D\250\216\361\341?;\230O\371j\301\353?X\336\017\037\340\244\277?H\032^\021!\241\310?\266 xM\036 \337?\327\276\023\377\240\207\345?Wp\031\300\344=\341?\226\335\037\320E\003\340?b\331O\3301\313\325?0\373\\\377}\304\303?~lI\201\231)\334?\370x\n\320\261\266\325?C\353\342qB\260\356?\240\251\223\315\n\371\352?\222\362+\304\273V\326?\340f\322\334\370\357\234?\000\274\n\245`\324\\?\214}Yz\247\363\314?\202\2645P\352\327\354?\244Vv\321\372X\356?\230\312\003\032\372\376\321?\274\352\332\014\325s\306?8\263\370\322\016X\347?\215\240\3432.b\351?\202\277\361=\005#\340?\2721j4S\362\330?\207u\237\035\315\006\350?\231\374\231\0075=\355?\310\257fb\222`\320?NwJ~fB\332?x\255^\247\242\030\347?T\310 \341{\021\335?x`\335\031\207\274\321?\374\3254\240\355\342\336?T\203\247\024<\336\340?\240\212c\264\247\360\221?\022\316\352\340n\227\351?:\336|\377 \274\350?\013\230\2577\261\350\345?$>\375\255y\225\340?\267\017aLp\203\341?\234=\311\332\317\273\330?X\204\254\326\217\r\336?\320\257\337\325\366\335\256?\\\363\264\231\313\"\331?b\2620\026\361\315\352?-4\203\'\241k\347?nYg\366\006^\350?X5/X\224W\350?P(\010\363\004a\341?\220Q\'\240\010\332\273?\360R\232k\2229\336?\200\273\243Bg\321\267?\350\316FP\347(\332?\372\367\220\307\"\354\351?l\300\240\311\237\223\306?@\315c\264\007\250\246?\014\026\243\007B\314\336?P%\037\221B\357\343?c\034u\210\206e\357?\360\377\334}^\311\267?\"\311\032\305\330\364\351?HQ\253\213\251\225\351?\016\250\251\335\245\253\342?\017\2149\267\241\314\345?\272`\265k\316\013\320?\347\260OP\317\330\342?HvQs\302h\274?:Df\226\262\312\327?\236\235\004\260hE\332?\340\324\356A\245d\244?\300\245\270\270\217\234\302?/\227\3576(;\354?\005\014?\213!\315\342?$R\365\322\254\034\300??\020\255\354\264\307\343?\310S\200\207\221\376\313?\324\205iE\262\026\314?8\246\007\254\313-\325?<\001\274\331\0025\352?^\003\207\374JR\355?\304\034\242\n\355>\336?\371\2423\024\"\273\342?f\321\356\335\262\355\334?8\315\212\344\351W\345?\236b0\322\225\240\340?\034]\313\261\000\025\350?\340\370\255V(\265\241?\256\035\245\006\237*\347?p\250x\334\214 \264?U7\252p\310Z\343?\031\275j\321M\261\341?\264\362\262\217\324\311\323?\207)X?\007\363\345?dv\370m\207\356\353?pd\337\002[\203\325?_,MN\032a\356?$\244\227j\360\223\323?\355a\335\006\267\303\353?\010\361\351B\231\r\310?\340\017\321\177\2117\304?4n\342\345!\276\322?\336J\257y\224\201\350?\017\026\365OO\207\345?\250N7\313\222(\356?\250\325QMK\372\315?}\365\373\331\036\305\353?\266veT\202\365\352?\252\316\n\266\234~\332?\340T\001\027+\324\327?\234\375\240%\233\220\330?\014!f\373Qd\357?\0009k\372\374$\243?\244\t\022T\t\231\333?\351:@\362F\371\341?\002\035\305\007\223\364\320?\330\222\324\277\\\325\324?J8\002\200\305\333\346? \212tJ\346&\340?\250\260\014&\025\'\314?\300\277\"\031\355\300\254?+\016\220\236*c\347?\361\261\023\360\254\361\354?fe\331\203\025\032\326?hL\344\312\331\344\267?\323Yt\273\365\350\346?\254!{\352g\224\343?\316\375\236\222u\035\337?\217\314F\373Tt\353?\350\242o\317\204^\346?\314\353\357\262\276\331\340?\254\276{\3219!\315?\305\272@\370\376\337\347?0\251~\324\314\010\254?\000n\335\n\274C\334?\350\360\373\352\377\224\276?%\236\320\2759\201\351?\210\306AWq\232\306?L\302\301\256\345\350\334?`\332\305K\274.\242?\202dU\307\320\274\327?D\331\322\026\365U\314?%\177\237\005\331X\340?\321\360\357\353\243\344\353?P\026\314\013&\327\252?d\304\364$\024\255\346?\255\263\331\024^&\350?\025siS\000\023\353?$\203\213\222\206\374\311?kIg\266\326\240\357?c\031\022\354\322\372\346?H\344 9\0078\314?s\304\330\211\270\240\343?\026\250\305\232\316\333\322?\254\327\266VY\370\310?\267\375)v\210\207\357?\214\222\001\231m`\314?6p\302g\354\341\342?\360\265|+\305\215\300?\";\340\372\354<\342?\016\307\006\363\372\331\327?\360\315d\003\375\324\257?~\364\001\3347O\343?<_\n\316\264\262\301?\3524\253\322\255\234\342?6\020x \216V\342?\277\260I\252+\204\351?p\036\263f\360I\326?\352\263P$K\373\330?\207F\224\000\324\340\352?\3676O]\313\301\342?\202;\372\000[\327\332?L<\307\2433+\321?8$\023\"?n\327?\250\314\340\226\363I\322?\210c\t\264\024\244\344?2\375\233B\"\224\357?4T\201^)\000\302?dfu\317tZ\311?N3\300\2167I\321?\244\251B\341\361\305\304?\200\330\" \350E\357?\203\003\325\2725K\357?c\022\227?\036\316\353?L\231+\001\223i\354?\350\035\247\323\\\236\355?\236m\335af\334\352?\363\334L\2472\026\340?\370\354\223\317\354\244\312?\266\033\301D\327\206\323?4d)\260xw\325?|S\013\000\036g\340?Q\021K?\356~\340?\360\317\311\022\240C\357?\272\221D\332\222\340\332?\226\366\301\277\352\332\336?E\230T\277\320^\345?\274_\031\026\215\322\317?fy\016\3300\204\337?[\322c\214\334<\344?jb\017\357\021^\324?\274\227e\002\322\227\347?\370\005]\242^\t\345?\375\366\037\2630~\346?\206D\026\241\035\034\356?\270 \257\"\202-\317?/\341\276\306H\374\342?\260\034\020\317\r\"\337?\230\231]\021\245\223\271?C\272E|Z\264\352?0\302\000\276\245\242\340?\223\362\225W3\271\355?\344%\257\354$\037\333?\330\246\371jM\254\306?\200\257\320\333J\354\351?\300\254\311\206^5\227?\364y7\301\316.\307?\314\232W\tF\021\307?T+P&\326\250\352?\360G\3128\226\342\247?\262\214\007\016\017\335\340?|\270hH+{\330?K)\373\331\235B\340?0\313\313\0248M\262?\343\322\240m\203\251\355?x\307\333)\022s\322?\317o\t>\376<\341?\255P|\025Q\374\345?\'\320\037\230\250!\357?\356\343\002gV\366\344?d\021\340\001\007\332\334?\020=\221\330\336\335\352?`\2538B\251\324\225?\360\177\017:K\246\335?\202\316\366\265\004\245\347?\326\230\352\244Z\242\335?T\270\0176~\345\307?\220\3209\311\206\007\273?\204\"\r.\275\375\343?\023\352\003;\376}\340?\024u\266\223M\343\344?@h\3242\365\340\333?\251\n\240V\202\002\343?\261\334\'[w\247\340?Pn\331;\347a\262?\235y%\367\251\331\344?\014c \003\035\364\334?x\307\007\n\274\014\262?\374\023\300\362\226\323\350?\320\225\355\177$\333\333?})z\257\370\364\352?\333F\356\357\200\024\342?\244$\027\272\376o\342?\360\327\231\365\375\272\354?\000\266hU\246\324\341?\200N\036[\010\202\347?\227\025\303\353\223t\357?`\220\237\311\237R\300?~y\375\006\354\n\333?8\2353\'\360\375\324?`\004c\363\354\274\325?@~=66\202\332?\251A\274\251\214Y\342?J0\247c\201\225\341?\340\'t\315\354\217\264?@9\010\316\201/\216?%\374\224\3037\233\344?<\341\037\013{\232\354?\320\017\363\373~H\270?\311[\313\322}-\351?s\246\005;\325k\350?\210\251\252\r\334`\335?z\371\203\327\315H\354?\320\330$K\272U\336?\022\224\301R\236\336\341?\360\022\320on\025\345?\360\201c\244/\303\350?\264\247\327P\313\225\336?\237T\266\r\006\245\352?\272\"\262\256(D\355?\325\254\365\033\243\n\354?\024\307p/jV\316?V\332\210\312\212\343\324?\327\366N~\212\237\357?\200\025\252\263\371\036\207?\224wVd9\013\325?\250g\356\221f!\263?\300|\343\330\220\334\331?\362\037)\215\373\227\335?\372\360\000\212\302\244\346?\274~g\223\257\324\332?\024s\255WE\250\346?`b>xO\263\262?eR\007\367\273\233\341?/\330\326\361\312\037\347?\315h\316I\213\275\340?\300\373[gEk\305?2?I\021\262f\347?\250\377(\347\311\332\314?\300Q?o\240\315\274?\323~1\254-p\343?X\277&\tuL\271?\025\327\376\034j\217\347?\010\224\341\343\244U\264?\310n\233~\336\337\322?\337\343F\202\023\303\345?`[\225\010!\371\304?\211s\254\013\024G\345?\024\222\006\314\340\020\300?;\244\255\356\270\324\347?\020#\353\360\371\301\246?\\\000\013\240(\022\330?\276\331i\214\312\017\344?\345\346\000W\313i\353?\360MD\247\005\003\320?\030\201i\3634\344\313?\254B\031k\023\310\304?\030\316\030\215\204\335\267?#\233EM\230\344\354?bi\363u$\240\333?<\373\377D}G\310?\\%\003]\026\270\357?\224\005o67?\342?\342\020s\247k\"\325?\374_\324\343}q\313?%\236\234:\301\300\350?\000\010<=\000j\350?\230\3253\000\301\353\301?\244\367D\214\232\022\337?\364\ne\374\030\331\346?\213\342\225\372\n^\346?\274\260%\351V\010\340?x\220\353Q\347\330\304?\203v\200\266r\374\345?\242\375%8J\273\334?\007\245-U\362|\352?H\262\236f\2065\350?F\300\017\362\255\245\356?\212r\334\320C!\324?x\311\034D\037\362\303?/\207CE?x\342?,L\301d\275:\335?\342\335\330\031I\021\335?\237\342\001P\204\032\344?\010\241dYX~\327?\320\366\272\355\236z\304?\000\3667B6l\\?q\271\331\354c\301\341?\006<\226\337\254\342\354?\200\006k\025\2207\263?\001\361#\000\252i\346?\265\025\3546\367\017\340?\266\001\201\330\315\257\337?P\010\374!\n\372\327?@\267\3463\313x\211?wg/\336\363&\340?\346P\275zh\267\332?\264\230M[0\234\323?\365\244\303O\261W\343?\222\203\235\016\274\271\341?\277<\014k&\000\340?\230p\267J\315T\264?\230\364u\225\256\271\342?\314;\031\002\034\234\331?\240\373\236\217\372\230\333?\252\317A\217r\000\331?\000C\037\247\354r\325?\007V\013\374y/\345?\014\006\377\236j\353\331?\365\027\033\317\031\216\344?T\300\204$D\244\305?\000\343\237\272#\376\250?(\323N\277>O\345?A\275\036\343\037\375\346?|0\007m\020&\340?\214\277\336\001\247\313\314?\250\272\301\302\327\261\270?\372\365\037\034x$\346?B\260\370\212\346u\331?\270TLY\316\223\331?\270\341\300`\324P\304?\2721>\233D\037\341?\274\344+\257\204D\337?\225\005\036\205\034\037\356?\201]\221\237?\016\351?s\205\267\201\314\330\354?.@Re\253\r\327?\360T&\025\315\327\317?fd\021\242\360I\352?\200\362W\261\366\355\342?H\267\305$\313?\356?\347\206\241`\275\010\344?\340\314\177\344\017\230\235?\304\016T\252\207\031\330?\362\213\227$\331W\356?:\017C\205MT\323?\373\205\004\232X\366\350?@E\231\002\302O\357?z\3457\266\260\214\337?\205\210\037\265\210M\343?D\372\2760b6\343?r\362W\267k\333\341?>*\236an\244\353?\177\372fS\361R\344?N.\322\026\026t\321?\321\246\032\340\235\261\343?\000\373.\206\0312\261?\254\200k68[\331?L\256\225\036\006\t\347?%\023-QQ\252\356?z\021\3365\355\316\336?\221\260\225F<*\343?\366\035\300\344]G\356?\323\023t\021?\341\344?? \323\310\023\203\341?\200\216\331n8{\230?\010\334X\315\255]\300?qImhT\205\355?\302V\016\265el\323?\373\356\350\323\323\032\350?JeL{\205s\340?\006\027v\271\353\245\345?\377g\252<\032\376\341?R\213U\327\r\337\346?\240w\255\256r.\305?`\221X\270\240\\\264?0\3065.\365&\300?;\245`\266\025\203\341?\177|0\211\273\357\347?\0271TE\272\326\353?\250\226\n\036j\270\301?(\332\2610\033\352\300?\350\027BQ\215J\350?\235\t`\0307\225\353?\213\355\241E\"C\342?F\247\301\212\202\217\335?tXE*?P\314?\025\276?k\216\n\354?\311%\272\034\003\373\353?w\213\337=\276|\351?p\273\034k\227Z\304?\337=\023\267\230\241\353?`\376\004P\030\226\345?\3204\233\r\0258\306?T\343\204.)`\356?\326\355,\376\027P\333?{+\336\252\332\346\347?\300\270;\225\327\306\252?\000\023\275W+\r\247?h?\303\332\341\252\343?\324\214J\264x\032\344?\000\246P\354\312\024\205?\2729\326\220w\001\321?\230\t\367\354\355\325\304?h\310\234\233\tc\310?\355\003\254\223L\217\353?\314\"{\235\365\377\355?]\324\307\351^\205\355?\242_\224\023iu\344?X\316 \026\320\301\347?EVW\036E0\354?E\373>\366\211\357\344?\210a\272\353\350,\341?|\020\247\217\024\347\315?\275\210\244\226\226t\343?p\351\242\266\246\377\246?\032`\262\302\214C\352?\300\020\331\t\304\371\333?@\222 \370\204n\302?\240\346LQH}\245?\300\016eNYX\331?`Q;\201\031\272\223?\020XX\364\2661\254?\000\214\'*\"\352x?\024\343Z\336\352+\303?\240es*\026\316\253?rX1]\365\340\353?\320\247,\030\237\374\240?\252s*\303~\312\336? \255\216(\274O\317?L\335\267\373\375\301\310?\215\207i\225\024\020\346?\314\322\361\322\364\375\356?\244\222\2564>\320\323?\240\374\005\307\265\343\267?5;\216\206j\272\352?\230\\\323g\362G\323?P\226\367-:\317\271?\265\317\305\025\240\226\340?$\326\336]\311\252\326?\320\376\275&$i\303?\n[\231\316\374\372\357?\000\026\030GW\377\230?0\200\237\315~\326\276?\032\215\243\215^n\353?\370\225*\025\310\307\342?_\177\324v\371\325\346?2\364\371RB\231\320?|\344\331\266D\264\344?\310]w\301\270|\340?\321\000l\262\233\271\341?\033\260\023\246F\001\345?i\233s\233\3000\347?lw\364jMB\300?\316~\277\027}D\351?;yTV(S\354?\000~\t\036@\276\334?\233\237Muh;\351?\021\213\2437F\257\347?R\375,p\266\247\351?\356\354\277\354w\032\322?\234\306Ru\0009\301?}!\252\244 \021\354?0B\322.B\265\300?|/2\203\340\370\330?P\335N18\227\253?8\353\242\262\203\t\324?\350v\n\334\264\204\337?rTY=\303\300\345?\300\365*\277\272\365\266?\022\271\373\307Sn\346?\376\354]]\226\326\320?\320\207iU\234\311\345?\303\017,\321\236\336\343?(\303mb\022;\271?\3325\362\017K\236\332?\016Z\376I:n\336?\330$\266\307\232K\324?\214\0374\357\330M\330?\220\026kq\221\261\322?\301\241K\2017g\350?~\211IL\2463\320?\004\344\030\030\036\365\333?.\325n\311@h\354?\017\217\264~;\r\357?\216;\224\206\240\235\357?\330\212\302c\323y\341?\303\271u\345\371\323\347?4\262|\336L7\337?@\345\030\332\375\376\241?\224R\255\333+\360\311?\362\320\201\305\235\214\327?\010Q~\326\255\331\307? x\250|I{\277?\341g\376DIo\345?\300\205\353E\027T\332?X\352\357V)\002\330?\034\250\351uk\001\336?$\263]$wR\300?\250&\2147W\210\356?\250\017\030\362\311E\346?j\n!\253\201\230\342?}9:\375g\361\356?#\\\342\203\3503\341?H5\371\002;\254\263?(\306\234\205\346\373\312?\305u\"\247\314\222\351?\360M\232;;\321\274?\000\3373x\356\035\271?\354\3724;\300=\331?\222\347Q\023\236\027\343?\250\"|\327A\306\337?\360:\354l\253\013\312?\n\001?\332\332\326\356?!%\257\274N\361\346?|\360*\271\304\033\330?\317\006J\256F\003\353?Ak72|\267\345?\2416\335V\216:\347?\204o/\337V\305\341?\344\320(Z\332\310\341?2\252y\370\000\260\357?H\220\317\332\375\013\303?I>\261J\302\004\341?z\235\215\200w\t\322?\301\253\232\271\331\347\357?\002\243\271\356Ku\352?{\304\356\266\307I\350?\010Is\316\263_\305?p,\264\321)\023\332?VyBm&g\320?\210\311\216g \304\321?r\265\343\037\360\357\332?F\026\271!\345;\350?g\251\214\276Z\206\344?\3424[\241\265.\355?\253nG\266\276\316\350?4\243;\266\310\275\335?x\310\262\364^\001\353?8<\tU\361m\305?\2626t\342\216-\353?,\347\223,\273\034\314?\376\262\200J:\347\351?\366\325D\371Y\310\356?dE\324\330e6\333?\2260\024!\350{\356?!:\265\361%\266\347?H\'\247\271\266\365\332?@\212\274A\341-\320?\013\336\314k7\373\344?\301\006]\240\307\301\353?\304V\214o\306\352\316?\314\315\253\237\270c\345?\000\357\215\376\r&e?\242g\331\334|L\320?\342\356\227a\006C\350?\224\374\305Z;8\343?\352\222_\354jX\355?%~\"\270\036\363\346?t\202\206F\353\327\343? \234\323o\006;\324?\355\'5s\372.\357?}\021W\004V\271\342?\202,\224s\216\220\351?\266\215\001\3700\245\320?\277\237\316\273\302\204\346?\332\rv^i\260\337?\302y|m\230!\320?Pb\331Z\273&\311?\330,lg\324\024\276?\007\n_\03780\354?\354\312$\036\022\311\344?\301O\321\312\0105\355?\340d\200<\375o\271?\2258(\324\300\033\351??\250G\247\2400\354?p\251|S\267\267\300?\306MRFCF\346?\211O\027\373\371t\355?D\321g\234!X\327?\200M\023(\276\277\324?P\251\207\021\375\225\241?\000\026i{y\r\307? \024_\276L\246\357?dG\212\302\022\350\335?\033V\303\010\277\216\347?\244\254\007\272CP\343?=2\350\245}\022\347?NW\331\257\020p\356?\275V\245\214\355\273\350?u\324(\270\376\362\340?$pe)\321\270\341?p\354D\252\210.\332?\345\305{\354\213K\350?\367J\354\340\031\367\343?c\353\033\331\346\335\357?\3723d\216\030\314\353?h,\207\321\226\004\275?\322\036 /\201\304\356?0\266C\006\243\352\264?\255\331\223\331\233r\353?`S~u\323-\306?md\250\221\277\276\353?\320\026\357\316\232\376\354?\200\267n\236\271ru?\304\277\360\034qj\306?\214\362\264\234\337\256\350?\310\342\266d\316\236\331?\3747\345\253\231(\337?#\032d\034\374\275\343?\367RK\366<\030\357?t\316F\262?o\353?\024\334Z<\203\035\341?X~\230y\303\036\303?P\024\266\276\304:\274?\300h\353Y\332h\332?~\302Xun\237\320?\341\251\250\323\350\371\355?\316\003\250C\267\361\326?\200\036\306\272k\322\256?\224\264\3105)\177\322?uP\334\002UR\353?\362\014\341hp\222\336?\307\330\260;\203\341\340?\327Y\314q\3402\351?\000\013e\306\331\362\202?\024\023\236\025q\307\325?\222\251!\246\273\255\320?\036Z9\2015Q\347?F=\254CMX\344?\330\253bO=+\357?8\302\206\206\204u\313?\213\233\365C\006\276\352?y5\331U\377\326\341?\363\356\nb\322\244\353?\315\200\334r\013\200\342?\002\351\326\217\210\353\356?P,E-\010a\275?\232\001S#\255\210\332?5\347s\035\272\331\347?4\345\341\000\300\214\310?\364=E+{\300\337?`\225\203f\376\206\244?\336?\300\32610<%\346?\210\367\3647J\273\357?\004\275\351\250\2753\335?\202W\275\2459\326\321?\370\010\364\272_\245\312?\363\315%wz\033\341?\363\001\241\373\252\233\350?\240Q7\353&\303\313?\263>\304\035\272L\356?ld\275v\307\216\352?.9yA31\325?\263K\251\034\335\246\343?\272\314\347\311Pc\333?\210\363\276t\240}\327?:\327yG\304V\356?\346\303l\024\340\303\325?@\231\002gP\321\263?\263H\230\242\305\205\345?V\263\256\227\323\'\321?\212\220\370cj\345\327?\204\030W\003\352\\\310?\000G\000\020\022\232\222?\356\002Z\2137n\351?\242+\333\216>j\351?\024\025\177\234w\221\341?n\362\027\240\240\350\340?^?JX\275\305\324?\000\233\312\363/\371u?x\006\215\323\200\341\352?\260\227\222\204\304\030\303?P\036\223~t\006\254?\014\306C\320\315\367\336?\350\006)es^\261?\2428\034\255\204\311$\325*\324?d\350~\204e\311\316? \311\265\035\262#\254?(^\331,\334k\354?$\223\364\233B\232\311?P\360\305\r\261@\251?$p\267r\311\022\343? \244!\341?`\253?o\324\355^w\276\352?\036\347\n\227\250\254\352?$\216\345\343\336*\303?\004r\203Ge\364\335?X\272-Q\013\000\266?:\2528\356!Q\354?wL\351\022\330U\344?\2306\r\214<>\323?\323\356 y,\271\352?]\346@9g\324\343?\306\272Q\217\266.\321?\220\246_\"Q\035\331?\320(\274\251k\304\317?\267Y\222r\311\263\345?\347\352\262\332\341\353\345?\314\226\276\376\354\244\312?Dh\322C\230F\306?E\321O\234/\022\353?6Z\344:F\221\352?(\207\034\013\350N\276?\327\320W\031\305\177\352?\332/\334nu\220\351?\000N\253\300X\356\311?H\322\025%\304\254\265?\204\234\264\234\224G\320?\030<\212hE\373\337?\371\224[k\010\256\352?\350I\327\014\232F\342?\"\243\227%\017\252\355?@\335}4\013\373\273?C\372jH\030\332\346?\317\220\364n\350H\340?tW\360\277;\n\324?\260\0318\343|\"\330?\000\231\231O2\014u?\000\2178\343_\347\247?\300\262\000g\260\314\341?\324wU\005KE\305?\240\250\252\377\363\272\344?G\323\030\013\210n\356?\205\036\244\353w\230\340?\222\273\354KOA\320?@\340\2460,Z\243?6|\315\023\322\244\337?\002CPEzm\322?\016\206[~ I\357?<|*\030=2\312?u\270.P:\267\346?\277\370\007\252\321K\351?8\036l`\201\017\344?\000\235\307|\215N\273?V\206-#6\002\320?\t\006\2153\314\225\344?\240\363)\231\376,\311?\002\243,\250\035\306\326?\335\310\005\305gx\353?q\261\034d\257\221\342?o\344\321\257j\300\343?W\342\370e\315\241\357?\n\235\013V:Q\327?]\273\205D\256\321\347?\002\005A\314\234\005\324?\273\315\271\300]\223\343?h\3233\315\270@\325?\376|@\016?Y\335?P\351\253\036\335;\346?\220\323\022\354\365X\305?=)\303d\220%\342?\267m\036a@\030\340?\372\013Lv\3123\331?b\"\253\335\323%\355?~\330\266h\220b\353?\226\',\"\254\273\322?\306\037\323\267\000\021\357?4\205P\254\002\026\357?@m\313\275\031\347\204?A\313\275\212\374\217\344?n\250\262\034g.\345?\002\361v\202\013%\337?[\2525\274\204\244\357?\250\216P\255\204\256\274?\311\325\302\232\337\246\357?\376\027\311.\247|\345?lgUlh\233\331?\300-q\241\210\305\237?\2164e\002\335$\331?\215\271\360C\231\203\357?\360\026\222\237\003\235\326?n\024\225~\374\340\356?\247X\000W\013T\357?;\361\205M<\223\345?E,\217\307{\010\347?h\272\311\2414\226\342?\'6\022\010\371\364\357?%\220)\003\313\224\354?`\272\223\034\201\206\302?k\231h\324\301\274\340?T\277E\250M\346\300?\230\332\245\250\246\314\345?\034\213\307\005\275\220\343?\034\037[\021\271\003\341?\030\010\356\377.s\312?\232B\325\217\027\005\354?\214\\K\317\343\216\305?\347a>\247f\033\342?S\346\236\234\027T\343?v\000\204\262\270\\\345?\t\201\331W@\217\351?;\005\250\251T\001\340?\346\367#f\320=\333?\310\007\215\026\025[\274?\362}(\337\307\235\357?\212\031\353\007\220\271\333?\316\014@n\261\344\356?\200\343v\016\266\243\227?\023\203`|\217\314\345?A\225\234\'\247\374\341?\200\364\272T\324a\340?\350\264\303\370\252\226\324?\235\023B\266\\\261\340?C\252\334\254\236S\347?\365\203<\375\222\314\343?\034\002\334\376\277\026\350?\337\013\246\217\335\263\357?\214\236\2225\363 \341?\025\216\300\361>\254\356?\036\003\237u~\354\323?8\031\210\222\262\374\345?M\252\027\341q9\354?co\211.\360\347\351?\230\266\r\307s\322\356?\224\377\325n\363P\353?\300\002\222\2063\245\306?\207HE\004\010\341\355?|\331\335\237\370u\337?\250\242U\254\026e\265?0\302\363\030\221\225\306?@\177\026F\312\036\206?\365\274r\327}1\344?\003\262\010\255\221%\357?\024\372\037\306\014\276\343?\010\002\371h\300p\347?\323\233,]t\000\352?\265)\306`\226\267\344?\010\372\037fvf\322?p,\"\210\222$\242?\204\023\364\206\223\261\350?\311]y+V\213\347?A\034\246\277\335$\342?\203\001Is\214\205\357?\242\366_\303\220y\350?\010i\'e\320\353\315?\340I\317m\346\264\332?o\\_\255\000\n\352?\265\355I\t\3627\345?\273\3505X(\372\357?\306\"!\212\227\241\335?X\021\316\266\016\216\325?\210\0070\226\241S\306?]6\2116\233\300\340?\300\352$\333\373Y\273?\017\007\026g\356\004\355?P\317Qv\3319\250?\375\352\030[\357\310\355?\007$\005\333\312\316\343?\237~\315\321vN\340?\331F\232O\025\240\345?\010\360\252_\254\014\353?0\033\275\262!(\323?X\207\031\303\300g\356?\275\242\261\rJ\260\346?\343\025\222=\304\312\343?\234\260\231\215\362w\317?\200\207\'\026\222\300\203?@\277\377/\220\200\252?\306R\207\237k\036\327?\205\332VN\005\337\353? )\017?M\243\233?_Z&\265\r\371\340?\364-]j\316\307\337?L?\204\010[\304\330?RF\254\346;\366\324?a\000Pb.k\350?.\374\341*Q\r\355?\357\236\306\305\344\333\347?Na\231\363\037\372\345?\236R\345\214\314\257\322?\241mU3\005\037\346?\275.\360\n\"\317\353?\254J\371\037P\311\336?207mV\325\323?-\377\246#\342(\350?6\345*\317\027+\346?R\337\243\334\033>\343?\304\n\017\2520\317\340?[\227\206\034\365\225\347?l\344\306\361\'\n\314?\344\330\323\253}7\310?\214D\027-`i\347?\002\302\256\304\375\376\352?\010\367 E=\241\313?i\326t\330\242\001\346?\206z\306\205\2658\332?\261\tJ\272\203\376\341?\200\362z\260!o\216? \177\255\277L\302\346?\236\354\373\'\311\277\327?Sm*\313\225H\343?.\311D}f\032\350?X\034N\377\264o\276?\320\277S\360\335w\262?\362\313z\317S\254\344?\001\010\247\324\367\243\351?8\021\266\353\301\206\327?\327\234P\265\\\353\355?L\364\022(\334\304\331?\224\014\302{\250\346\305?\001\214\236\351\355\032\353?X\251s^Z\305\303?d)Ml$\247\321?j\033\243\':\373\350?\363tOk^\222\357?\370\274y\210\357\307\357?(\210P\3600`\265?B\324>&\327\022\350?>7#\212\201\325\326?fd\000V\366\273\356?i:\331%A\311\342?\004\2752\273\014\367\333?\230\255\203T\262Y\261?k\034N\345]\003\346?\002H,\240U\200\344?V\343\r\261\\\215\341?\026?\222\300\362\361\340?\341\277\217VgK\345?\365\005\372*=\317\355?h\350#\350k8\275?\247[\025\247f\020\352?\254\354\303\371\373\021\300?\233,\307\230\271\243\351?\004R\247\230\265\376\323?\324\2376o\341\365\331?\036\261\244y\277\235\321?`\\k\337_]\266?@vO\231It\352?\215\036A\376\364o\352?L\207m\240\017T\310?\214+\034\371\271\006\317?0\337\023\326s\236\354?P\351\353\320a\327\257?\002\206\262K\334\005\351?D\260\302\230\250b\352?*y\013\333U\211\331?\276\323\001F\336\330\344?\240\373%\364\222\201\255?Z\r+\251\343(\357?\n<\274io\313\320?x17\014\027\377\327?\270F\350;\350\233\307?\"\306~\245\307\345\351?\212\33127PN\342?H\343O\243\2265\331?/\304\000\330M\020\340?+j\033\274Y\017\354?\204\201\347\006~h\342?`\361\3442\251\t\331?\340\312\362+\367\241\254?>\005DL\036\311\350?\024}X\205\\\326\355?\300\020\322iD#\236?\370u\001?\357\005\265?\217\262\360\2004\312\344?H\'\234\266\300\336\351?p\006F\302-\340\322?\362J\361\205\017\317\323?|h\325Kb\377\300?\325\245v\035\032{\344?\336\007f\006_s\322?\310Z\277\005o\010\305?\376H8\241\370\267\354?/\344u\322\354\013\342?\320\030\202\255M\257\305?\335/3\312\002\315\356?\271\2743\254up\341?\264\246\350\362NU\320?\317!N\344\371|\344?,Y\263\275d\322\335?\337\354\311\201\256\311\350?\224\335\026y\022y\321?\016,.\342\020\376\330?P\274\226*\250\325\267?}\330\363\013\353\312\355?\356\006(\t\2060\344?\231I\222\243\264\320\352?\245\274\337\200L\004\352?\240\215\300\032\372X\320?(\374-\262\267a\276?\330x\357AL\231\325?\014\316F7\242\312\325?\200\\\024*\n\226\326?\263`\254\014\206\203\344?5\177$\355$\004\354?\274+\3559\035\357\340?\356\307\216\205\260\335\342?S\362\363\263\2356\345?\241r:\264\277r\345?Dcp\206\251k\313?\253\375m\000\212\251\342?\374h\201(\214\t\357?]\333\234;\332\204\350?\3760\3126\240\326\330?\307\326\314\321\256\016\357?\361\204<\026]\216\344?i]\'\327\177b\351?\241\260\3603\214O\347?\354\362\001\270R\374\315?Js2B\260\006\344?\376\3220tTT\340?\312\t\341\202\340\271\353?\266\372\205\231\274G\353?\214a\230\371\313\267\313?6\237\243o\244D\346?\256\275l\267G&\351?{\302.\220t\230\340?\334B\320\202^\226\345?\366\276z\203\271\301\350?7\344\261\332n\342\344?\002G\357O\253\316\320?t\343\010\262\030\031\352?\030+\323\314\212\301\320?\014\364\342\212\212\320\305?\244\352~!o\233\347?t\202GyI\357\347?\216\036\331?\275;#\300%\235\353?\030\243\243?]\001\343?\246~\"\270\352\233\350?[\214Y\231\235w\342?\250$\342\304\224k\263?8\261\333\240\310\306\307?\302\301\177\271\260\202\355?T\002\004\220W|\327?\340z\204\226&\313\301?\254\213A\360\036\251\322?0\325f\226\244\350\351?\270S\262\023\007\265\314?\364\306L?R\232\332?\346\264\003\263\242\323\334?\014R\267\256C\000\345?\033\230~\016U\000\357?\271N\226\301\250\214\355?h\356s:\241\254\260?=\2062\270o\233\356? %vb\203\336\251?\335\2718\3145_\352?X\322\246=\236P\332?\374\211\361|)i\357?\307 \263\264c)\346?\331\001U1\345z\343?\236\264~nl\302\351?\267\316\260\354\215Z\342?Hqe\037i\352\357?p\226\276VT\240\260?(\3506\366H\003\266? B\355\330e\245\225?\036\245\200\024ff\353?A;N\2709\265\345?\000\217\315\221\356\273\243?\224\264\201\365\241F\312?\251\006\357\341\215\367\353?&\231\353w\225\246\324?W\025\020\230L\335\346?\242p\013pX\217\334?\200\014=?{Y\210?@\240zy\263_\257?|\025\210\326n\357\343?\335/\242s\023@\352?\272\215\231\177*\001\321?\335\245c-\262h\344?\007|\003\272\210\223\342?\371\r\255\001\334\024\354?\274\212J\277>E\323?\374\362\027\357:0\312?\222\343\206\233\236W\353?Z\251\013@\030\366\325?\352Er\323\345_\325?\352\215|\303\001\364\345?\3557\220\262\237\377\355?l\256\226z\235\363\354?\273&6\036\341\177\357?\360i\334\303\246\266\333?\310\004\2545\307\311\356?\'\301R\343\223\332\345?\244\373\304\034\2005\331?\000=\331|\234\331j?\344#\306Z\2533\356?\276\307\233en\023\340?\\\231Z\201Z\334\316?\321O\2278\311\336\342?\235\201\224x\261\257\346?\000\332\0242\332(\255?\215\234\277\000\243\267\357?\0141q\310\203\244\323?\375\177\337h\276\273\354?\260/\317p\n\265\267?L\200\241\271\306r\322?\2753LG\367@\357?\220\364!d\345k\337? r\363\033\t5\311?\236g\006\323\254\"\352?x\225\013\354\231\025\357?\200\225&\nPg\300?`/\267j\343\212\353?\230A\232u\330\036\351?(\177\010\243\2246\337?h|=\302\252\256\330?\022%H\356zU\352?8JcE\232b\342?\300\2468\302)\013\322?\337\337C\t\226\317\344?B\213\271\354\3364\357?\240\311g\230C\365\222?\3463\206\310\277.\326?\270z\231\016\345\216\265?\261]\216\323)\330\345?x\263\210\225\207*\342?\375iO\257\237\313\351?\031\340\233C;V\355?\366\227\355\244\266\212\346?\2205\221\361\274\374\312?\372\202WC\351V\354?d\334C\266\333\340\311?2#\215\007\323K\357?\022J\253\211\251\t\320?s\244\261g\330\225\351?\204\033\214\020 `\321?F\t\367\324y\307\321?\326\337\205c>\037\340?{\373\200\342\230\020\357?e,C?+\314\341?\000\240kQQ%\214?\260\240\033\333\006\241\332?D\354\264&:?\324?\232\374\312\215u\264\320?&\262\265\027\336L\357?\320\333\237\\_\363\316?\304\231\345\r\261\337\342?\212\307l\367\017\354\355?\000Y\001.\357\230\275?\026\255\373BK\262\334?\360h\260\363\352*\311?\370\366~D\2038\302?F\246q|n\273\353?\364\240\2609\252\223\317?\201\232\345\030\322\253\351?\004\245\241/n\272\323?p\035\002\245\221\033\310?\274\342\377\241\017:\306?2\024\034D( \356?P\252\315|:\224\261?\227\323g\274cM\354?\000\263nTr\303\330?\362L\027X0)\340?\270K6\357\253s\352?)E\207\036X\207\346?\234\321\212QV\346\321?\321`\037\352U8\352?H(\334\204\324\322\264?}:\312rWI\344?\230\005\366s\2448\343?\3410\320sK-\346?uY\024$\216\242\342?\242\203\316\206\317\017\345?x\241D1\360 \333?\220\373u[\"\240\300?\210^\241\356\246;\313?\002\2003P\203J\326?bzL&?]\347?\334\251[)\330/\340?\007:\345\261\t6\340?-\353\321\302\303\005\342?\264\301l\343\303\003\332?D\334@e6\244\313?\373>O\240\006\362\346?f\231\3543\267\020\351?\030\330\314\336\304N\322?>\271\365\322\213\361\353?\034\262&>U\212\334?G\320\252B;\220\356?\254\t\233H\351L\307?\356\271S\330\236\354\345?\213\021x\267\206\313\340?\200K\375\272\205\"\202?\320\036\222?$\031\343?\030?\345\332\201\243\354?\024\000r[\236\023\306?\345\361\350\314t\267\357?\310\212\306\303B\266\335?\230\274\235v%\202\276?>\211\366\241\362d\355?\332\nQ\213Q\351\333?\230|\216+o^\343?\306*\270\216\214\035\345?\200Zo\250v\017\217?\300\325\273<\346!\213?`\372\036\321Ho\277?\t\340\266:>\342\351?@\351z\247x~\351?k\376\214p\002\303\343?\032\256\306\312\271v\333?HH\340\2270 \314?\222\205\302\367\262\027\351?\237Kg\242o=\355?0\246\205\2245\356\302?\357u\310x(\234\343?\3710\232\202\363\304\344?\240\317:lO>\241?B\313\030\014mQ\330?\316\222\321\242U\016\322?.\017\333?\245T\357?9S\007\215%\330\350?\034\357\205\215F\305\317?\242\251[*\217\020\352?hi\211\0134\322\332?:`L\326\201S\356?\270\324\266\254\345\336\335?H,\234\3552D\317?\350\343p\003\365\343\331?L\366!\022\233\362\334?yMJ\344\255\333\346?\355\033\215\265\263\027\355?L\361\002126\315?\362\243E\363\311\030\356?1\2420\357\214\353\351?\376l\202:\227\340\344?0\344\373\334\214\200\326?\216\206\265;:\232\351?\207T\217p\n\320\347?\224\030\366R\343\216\300? \273|=\341\337\266?t\204\320\3502\375\303?\200Z\233O{\037\323?\212c\335O\010\260\353?{\374\324\341\216\031\355?8D\027+\026\016\352?K\321\352\257\007\235\352?\203\276\313bLU\344?Xdi^p\301\323?\276b\205\345#\025\320?\324\203\026x\024\365\345?.O\264\003\032t\353?\\\034\007\273M\013\345?\335\310\010oU\274\344?\244\247R~\266\232\301?yw\016\236\np\356?\205\275\2169\321\030\341?\210#\317k\347J\267?\2521!z`\362\347?(:\346?3X\330?\333\362\001\261D\017\347?\344\004\342\375\207\016\353?r\331\264G\336\363\353?\236\333H\177\244Z\330?\264\256\275n\2057\345?\310\232\"R\346w\357?\314\225\005\014s\316\347?`\270l\233P\036\346?\252\251\006\355\357\253\354?\200\n:\225\270+\237?jL\033\300\252\337\321?\n\314l7$\232\344?\017\320\256\220\370\234\344?\247e\237\246Vk\351?j\227\330\217Z\363\355?\3358\275\237\036\261\345?,\240\220J\266\002\340?\220\303\026`+\253\273?\274\3579\033\251\233\341?\220\2044Y]\372\310?\302%\252NI\275\350?\220\327R!\225\307\323?$BR\234GZ\322?\365(\324\253\027\214\345?|\031u\273\361e\324?f\371`&\255\034\341?\367~\226\204d\\\356?\2000,\367!\200\221?\254\001\003\256}3\341?K\3330<\2317\343?(\020H\367 \377\276?,\221\236\363\251\017\315?\020\034\343\352g\307\250?\250\235\004\021\2715\275?\220\0361\016>\340\253?$\323\3431\321\005\334?\000r\'X\253UY?r\354P\025eE\333?\0304\223\252\206\351\345?\2358\252\226\203\201\352?.\212\232\323b^\352?\200\247\250\t\334S\261?\317\377\034\224\"\021\352?\370\313=\232\266a\356?\260D\002\306s!\271?7\204\213t\306\274\351?\272\2212\003\265\353\345?\230\216\236\222h\006\324?H\356\033 VM\323?\300\252\361\023\000o\244?\204\034}Q\232\256\344?\351\243\262\031\272c\350?n\034\234\206\315\353\330?\340\256\322\032\3351\354?\300\227\314\330\016e\263?\014v\300\0034-\351?ST\367o&\370\351?`F\234\214\356t\333? n\340\207t\357\241?\016\315\266\231\327L\336?\265\354u\226\346\335\344?\277)\300m\260\334\346?\307\272,\03592\352?\334\315\346\361+\345\331?\3405\266\202\315\005\301?4\272@\334[a\303?\200v\343x\027\236\250?e\206\"\354ic\353?\244nM\306\037o\305?\366#(\317\216f\344?\344\001\327\276\350\273\315?\\\250\217\320>B\306?j\0311\207\254\270\334?\374\262\330l\032\317\320?\253\000\036\332S\264\343?\316\272\177{\313l\341?]h~\365\370\335\351?P\327\307\335\036\335\337?\023|\356\352\302\256\347?iB\n\330-|\346?\335\203\304=9\217\353? \r\203$\212\261\224?\331\355L_L\250\341?4\031\365\327C\316\330?\000\233\204&\362X\256?@\302/\323Ue\205?]\335]\341\237\264\354?\370\205\270\230<\275\335?H\335\264\036`\261\312?\342\335\n\tr\211\353?\356@Z\353.\363\341?\300\272\034\256\2145\331?pv\306#\324\032\320?8\364i\020\037:\354?\230\201\260k\036\010\267?.e\323B\036\326\326?c\301\353\036UI\353?x\211\334]\253\007\261?\240W\206\222wj\323?g\351\377{R\321\351?\204\341REtj\316?\377\226\377\003}%\346?\360\212\006;\035\374\263?\365n/\316;Q\355?/\216\036\240\3473\341?\267\003\357\262\330\037\353?\224j\351\244NN\350?{\32116\003\373\350?0D\031\317-\302\322?N\315Z\342<\331\325?\016x\031\217\346\013\322?\240\212@gSC\354?\200\323\275Z\266\265\251?Y\177\241dSC\347?|\340,\242\215\313\352?\370\366\266\236\033_\333?+\033um\363d\341?\036\033\253q\277t\324?\330\3669\305W\207\326?\206\340\217\336z~\321?\343wd\367\330\030\345?\017\215\0231\347\014\356?\035N@\261uC\346?z\256\266\021\016{\323?\224q\250(\211\206\322?oO\200\347Z\222\356?H\230\256\352\224\320\300?r\234>\222\346\321\350?\014\313\266\222:[\356?\006YN\034\235\230\327?\030*`F@\327\306?3\246\371\307}\361\351?\312yn\017\267\332\324?\221\215 \370F\270\350?/U\030\006\r\322\342?P2\002\215\246\212\346?P\213n\211\034\351\313?\240d\247\311\\\277\351?\230\3362\002U\233\271?<\336\307\333\023\014\351?\006Y\227\346\016\325\355? B\035\t\210\275\223?\030\035V\253z\302\356?\005\004\3258\rS\354?\236\000;\217:\016\331?\240?\037\200\272+\355?N,\355\336\331/\354?\316!\016\366?\n\350?\360\007\224\277\331_\301?$\020\314\246\026\373\336?\232\323\331$\n\246\334?\215\213\353\334\307I\342?F\260u\376B\003\345?\300\250\312V\220\246\341?\000\243\202\375\024\335\314?\264q\275\331f\250\317?#\245:\212}R\357?\226\200\003\036\213\302\341?\231\203\220\232A\245\347?\346\203Hx\273\353\324?\010\r\200v\273\221\275?@?\\cJ\277\335?WM\377h\364\315\346?HL\016\272\020\206\275?\t\316\035\321\363\n\352?IO\036\375\252\274\353?\354\367\031m\013\027\313?\017\357\224BE\271\356?\316\264\037X\212\004\347?\311I\r)Z\261\343?\266\0145\275\235\325\321?\245*\313\344\273H\343?\246\t\325F\301\007\350?(\316\254p?L\317?\276\3658\261\340\377\336?$\265\311F2\243\352?\025\332\231\005\256\323\345?+\036\226\244F\235\341?\347\360\204H_]\354?\265\033\314\235\033\303\343?7~M\343,c\351?*\005G\311ef\356?\342{j\341\335\003\355?`\025\246\022\270\357\255?\323\303\202l\262H\344?\375Wd\310~n\357?h\345S\267\374\315\275?.\032`{\251z\346?r\354=\t\223\036\334?|\323m\243\355E\324?X)\213\037\272\263\260?\363Y\037\0310\356\345?|\311\031y|\315\330?\352nE]\010\373\357?\232\177d\3629\356\323?\335\265\n@/\376\356?C\356\224\375\203\366\351?\352\r\300\002wx\356?\342\377\250t\261\363\320?\352saF\325\201\351?\323z\264\225S\233\344?T\370\210\222A\354\340?\331GRQ\355P\357?L!\020/\250w\354?\322\275\364\222\320]\323?jJ\3751U\337\341?\325\260U\253\316O\346?p\210\332\036](\270? \024St=\201\264?|\251\272,zY\357?\260/\rK\213\330\355?\3642k\253\272\335\314?\340\2718\363\304\034\351?H\265\200V\301\305\310?@c\000!s\260\350?$g@\030\315}\343?:[Avj#\340?\260\362\241\357\346\307\352?\363\310\374\217\342d\340?*m\367\204M\272\354?\213+g\225SS\355?\207y\257\267i\277\354?p4F\'\225)\272?\030\3733\375\274\306\327?\210\275%\037\036\234\351?D\202\232\340\374\262\324?$\r1\367&\236\352?\341\277\002\373\\\374\350?>\347w\362|h\355?~\\\224\276\364\023\357?\274.\336=\026<\332?\342\020\325\263\230,\321?1\350\372\337\371\303\357?\311\257W\326\367\365\346?V-\006\004\r\345\354?\266\266\317%\306b\345?\000N7\347Q\322\210?\022 \274\351\355g\337?`\212\372\302\367\007\270?\234\305\354\2229H\345?\270\336\013=\256\324\355?J\262\225,\320:\336?\317\275\262\203\227\016\346?\310\233\"\274\312\321\326?\300\254\330\021`\332\351?2\360\251\307\236\207\323?@\260\256w\251\352\215?\325\276\375\242\013\266\345?T\326_\371U\232\315?\007*&)z\275\357?\364\021\375\036Q\247\313?\013kfW\312\356\346?0\213\244J\266\275\330?\247\276dA\022j\342?Hl }\360\371\322?h\275U,\213\201\317?\300\241\364\003\271K\343?$\001\361\200\354\353\303?0\204w\250\321\037\351?\021\t\300\333\227`\347?\224w\t\243mu\347?h\305M\005\237\366\263?\263L*\225|H\357?\341.\277\231\271/\347?4,\363p\352\006\301?\034\300\373\227x\230\340?p!\305\322f\205\261?\213\235\276\"\233\026\343?l\343\354\373\321p\341?\214\257>s\243\004\321?\340}\315\253\321\007\256?0\305\2272W{\265?D\221\232\215\365\023\307?\224\247\366\342A\243\333?\241\316h\313\355u\354?\373x\201\037\224\004\350?.\001f\345c\022\331?\245;\247\306u\r\344?\262\275V,$@\357?P\265[\276T\236\347?\"\007\261k(<\355?\251\224/\366\362}\350?\256n\261\277-\270\357?\302\223X\246\345l\335?\020!\2049<\233\351?\022\300\307\313\342]\357?\362\303^\027\364\025\351?Apf\207\200\014\345?\242\356b\023\2277\341?\037Y\263\250\254k\346?\264H_=k\025\335?`$\301\2104\035\351?k\320\342\302\215a\343?\010h\032\3532\035\261?m\226u\364x\333\340?T\013\007\254`\364\337?\302\304\224\016\376\254\345?P\225\231\010Y/\331?\346=\334\020;\234\341?\023x\370A\335\301\354?\025\361\306\322i\206\343?\272Ke\032Y\007\354?\360d\024xfs\341?\210\247WP\236\246\276?\302\307\2653\325\304\355?\310\346Y\305\277\024\354?\225v\222\017\323\037\343?\266\347XQg\304\330?JD\232\310>\247\355?\376\245\224\335\363V\350?\224\322\333\006\326\025\313?Em\026\303\373^\350?\236\327H^\324.\323?\200c#\211\0328\242?\264\374\255\006\345\032\314?\361[\337\235\033B\346?\264\346\257b\376\216\334?\252O\327\301\303\236\350?\366,\263\010W\265\340?]s\r\030,7\357?\233y\265\345\033\263\341?\346\361\316\344\334I\347?\027\025\253\306\305\204\340?x\263\rX\375\177\350?\260!\222r\234\n\240?\354\177yAX$\327?Z\023\356\263x\303\323?_\225#\272\020\370\354?]2\252b?\200\343?\217\036\331o9\236\340?\025~\304\375\330/\353?S\016\306\332\206d\355?\336\033\005\2065\346\347?\354\200p\331|\035\341?L0\337B\016\226\307?\261\362/\356L\273\352?\240\350\374C\334\244\232?`\345\027\'\361$\314?\252\344m\005\332\371\337?\365z\213{\226\021\341?\260\241\357\310\277\257\325?,?S\225:\033\332?\370\311*\003\030X\271?\242B\270\264\314(\340?\272J \233\305\364\342?\000*\243\217\206cb?\206.\3165\n\344\331?\255\377\305c0\004\346?\211\232\346)\003\213\357?QiZ]\310\025\356?\204I\221\252\357\327\322?\242\202A\215\367\237\354?\322\247\234*E\333\333?pb\240 \220\322\341?\330\362l\364BV\332?\204%\214\036\016\367\337?\000B]\317)N\232?\340\263M#\345u\261?\224\257h7Ii\335?|\r\"\261O\367\320?\001\336\307\017\3230\350?d(\200\371\306\322\315?T\023N`\221\364\314?\202\307\037l\212\002\345?\224\030\326Z\343U\350?`\243q\226\254x\347?,\275\237I52\356?\000\235\034p\253i\240?g\267\374g,1\340?e\357?\\u\214\251\010\024\350?\242\343|at\335\354?\211\303v\37332\345?\213\240\274Z0\320\350?\206?t,`U\337?\031>\207\017\2661\350?\264\027En\235\345\304?\214\371Gv}\274\301?\326*\021\242\036\352\326?\333\"\026#\2421\354?\200\312\333r\306xr?T\250xi\226K\351?,Th\252N\220\331?TF\3654D\343\322?\215[\370\207q\210\356?^\263}k\255;\346?\n\261O M0\346?\300\373be\332\343\340?[\340\334\272\264\306\354?\350\335\231\016:\243\275?\261\211\214;ka\353?L\013\033\243sp\337?:\360Z\273\354\210\341?\200%\276\264\306\022w?\200M\032\351[\'\334? \333\017Rg\266\336?\342\333\231-\241&\334?d\017\2412l\241\342?H\226\212.\225\345\351?\200l\027\336B\356\266?v\365:\372\240\332\336?\2200ND\227\374\335?\345\227\260\352k\333\346?\262I0\340;\327\337?h3`A\'\017\341?\364\345\203\357E\254\341?\000\351\330\266j\272\276?J\2043\323\264\356\327?)\252\262g&\222\342?\275M\372q\356E\355?5\022\312@\260\274\346?\274\216\0031\037\013\300?\370\022\3035\313\212\315?\326\230\266n\2236\320?\346\241E\221!D\343?\240\325\327\"\343D\326?@6\360\306*$\340?bi\272\024\263\233\355?\"\026\344P?\340\357?|\363\215\374\004E\323?\214\253\304\235\370\270\343?\227\243\211\t7\245\352?Ds\n\212\033\230\343?vf}J\201g\344?D\321Q\002>\024\355?w\370\242\033\313\260\352?\353M\r\240\273R\344?\260\273,\030\255r\353?\n,\215\0037[\325?D\314\350X\r\231\346?\272g\335b\234J\354?(%\301wU\365\346?\274\277\374/\364\035\313?\276P_S8\t\330?\035\205\347\347\326\004\346?\240K\301\250\306\306\266?\230\030\371\262gQ\345?\000\2468\221\3367\352?\014N\235\030\311;\307?@\355A\257\306\340\334?\210\240\237\352\310\351\305?x\316\272IM3\326?\024\014}\250zz\333?\354\r\023\n\234P\350?\232\353ZZoF\340?\3261\223\n\255\210\351? \371Z\215\304)\302?$MQw\237m\312?\t\033\350mv\230\352?\010q\t\275\322\323\331?~\306!\277d=\322?\202\221`\221/?\352?\210o\255WJ\277\327?U\224&\235\322\212\352?\010\324\034/\342@\276?\034\026\021\177\304:\303?\026\007\204\363\226\001\345?s\330\033\360J\004\352?E\322\375\235\277\320\357?\200\271\326o>\375\267?\2445q/\332\247\312?u;\233\232\275\233\357?\242Yk\254\336.\326?\246A\245!\024@\356?b)U\311\237:\354?\224\204R\223\3557\326?\200e\3146`6s?S\025\207|\343\330\344?\214I\333Vg\361\326?\004\353\364\220\301\335\343?\342\252\010\371k/\332?\277\347\317\347U\240\355?\243\362p\271\010\006\355?L\235N\257h*\346?\203\245\243\037HH\355?\007\225\247\212X\327\357?\352\236\350\207]\257\347?\210z\246I\373\363\336?p\310\371\036\321\313\341?mk\327[\226\232\340?\374\353\005\3207\366\320?\360\317sV\247\372\273?6\341$/\230\032\354?\271Oa\362C\272\347?\350\227(\373\363\367\355?0\366<\370C,\345?\312=\017^\306\304\350?\354\245Z\334\303\254\356?\256;f4%\334\333?\220,\222~\274\026\270?\030\020\377\216\223\376\354?\356\310\215\254m7\347?\234\2603\254u\206\346?\022\257<9w\314\321?44@\031\033\352\301?\020\206{\322\307\310\267?\373\010\327&Y\025\342?$\237\2748M\246\337?\000\210\033\376\335\333{?\276B1yT\250\332?\034V\360\016\240\026\345?\344\270\265{FG\327?\304\366q\312\261\314\347?\0267\350\014\216\263\353?W\2556oq\372\344?\342\375\230q\244\037\340?\315?\020\312s\213\343?\346\315\014`%J\331?XC2\345\003j\350?\212\242\023\305\2428\354?gX\302\324\345\367\342?\337\002\327\023\256\246\341?\374\003!\n\353\221\330?\212_Q\315\033\353\357?\0204\241\352\007\275\346?[\205(\314\264\272\344?\240\024sQ\3463\336?\206\376C\300\302|\344?|\313\005\020\313\320\330?\377\365\025\243K\t\343?g9\350J\352\310\344?\330G\214\213h\252\331?\254\351!\351--\344?\322\376G\005Z\314\345?G\307G1\374I\346?PU\210\'\232\222\260?\000\267\024h\345\n\245?\250\310\325\231\306\255\341?\020\242`\017Ks\314?\032\311;\352y\235\332?Zx\020ll5\322?m\264\357\"\201\r\347?J\272{\304A\260\331?P\035\037Rm\225\245?\037L\267d\035\367\355?\014m=\216\356\300\352?\361\312\3426\362\346\355?\326\2654\205\001\320\357?\017\344\356\332\322\234\347?\370\333\233\313\244\337\351?\334m\343\260;\341\355?\032\364\266\363\332Z\355?\264[Vh\310\306\305?\320\2662\267*\230\330?\205x\275\231f\245\347?\254v\'\205\007\316\340? GNQ\017\210\226?\001a\266\340\266q\344?p\267\240|< \255?U\247\367`\025;\344?\256\311\325q\226f\355?Sk\001\004\300}\350?\310+:\253\361\237\301?\004\356Q\365O\230\312?f\003\210\032tG\325?W\273\2614\034\024\356?\274H\254\0216\360\300?z\352\252%F^\352?h\266\020\201O\005\301?\376\235\314\001\3373\327?m\246\300s\233{\357?\344\373\344\317\215Q\300?n\002|\273\275\370\331?\220\321n\220\\\265\277?K[Q:\341\235\341?&Q4\005\361\234\347?DaWJ\256\305\305?\275M\331\333\216\317\344?\334\325\365\377\014\000\331?\330\376d;\246+\356?\033(\\f\262\022\345?H@\245A\220\324\343??mBa\013\314\350?\216\355(\227\240\027\354?@2Xu7\013\343?>\333\207\320\200\270\355?\340\362\200\0334J\353?\347\241\226g\004\364\342?j\253\246\234@\035\354?V/s\314\374\252\345?\206\264`e\212\035\323?c\365\331o\352\264\345?\212\026)w\216-\353?\010\200)!\341\361\345?zf\220\313\220\021\320?P0n\361\371\020\265?;Tb&J\300\352?\202?L\005/x\344?uK1\310B\020\345?@3o\332C>\240?\037\252\254\232\n\227\356?\370Oym\306\271\335?\034\314\351\315y9\310?\000\212;\026O\026|?,E\221\212&\205\314?<\217\233XfO\337?\222y\362\307\0052\352?\354\020\201G\2028\323?\330\230\337\334-j\315?\271\033\277E\365\223\344?RA?\203\007\223\325?\240\261\245K\217\261\232?\231\253\r\010|\031\354?R\252\027\272\370s\326?d\356WwT\236\305?\334M\363\000\010f\347?F-\375\304\355\032\351?J\376\225\320\204\364\336?\305z\342!\240\007\351?\250\274\313Tu\366\315?\000J\037\262\nC\233?E\177\251\210\'\302\346?\314\304\255\273\305\240\310?{E\234@\202\325\345?\360\310l\000r-\326?\360M\354\227r2\243?\020 \026\037\246(\310?\025\314\227U_/\355?61\267\032\307\274\337?^\025\257\003\316m\354?D\212Y\006\274\341\306?\250\333\335\271Y\227\326?\300\013\267\320\332\276\306?B\214+SQ\003\325?\344\336\227\336\253\233\327?\230\340\331\037x\032\331?2x\320\024\356\374\350?4\235r\276\021\027\323?v\244\377;\223J\356?\231\023F&\236z\346?\314pWW\025\021\316?\"\243\310\351Q\311\341?\320S\201R_\025\343?,\306|t\025\324\340?M<%3\236\301\340?T\2320\366\"\243\325?\200/iG\266q\331?Y\373?\31405\341?\242\204\036\377\020\327\321?\332\017h\357\205\333\341?y\036\301hYC\344?\236w\3431?z\333?\224\207E\026\244\324\345?\200\014\203\325c\n\310?\276\235\245\262\024b\347?\027p4\353\177w\347?K\202\020*\014\301\356?X\344\037\024\"\344\327?0yxj}\347\256?\222c\3116\352\263\323?\026\217\375z\n:\324?\320\262\027\356t\327\312?<\205f\214\210\322\300?\344\242\261y\350\010\307?\230\031\033\007\234\350\311?\222\365\200e\257\313\321?)\267\025zf)\341?\252\250\203h\376\034\342?\376`w\354\\\373\320?\"\302J)\021\210\332?\207X\304V5Q\353?\200\351~J\224\223\305?\240\316So\317\304\271?^/H\343\217\273\326?\226U\215*E\010\346?\332\272\350`\026C\341?\3743\224\216;u\305?|\360\272\214{\272\343?\256\303\035[\021N\325?w\340\370*\2762\341?(\354;\265\312\270\315?\354\032\346\026\006\341\311?\003P*}\026 \354?\016:\217\255\315\211\355?t\033\006v\206\330\301?v\'\354$`\006\335?\246\241=\207\022\020\324?\244\267\332o?e\340?\000\332B\363~\215\315?l\271^\275\237S\315?\205+\2066\230\374\344?\356\016&\030\373\306\352?\220\373\335h\251\321\274?\226[\363\037i\241\357?]$n\023\272\r\346?1\027N\273\226\253\344?\234}U\350t\024\306?\342 \024%\212\237\343?*\252\210\265\001\231\332?|j\360\234\245t\320?`\277G(\036(\275?\2070e\030\363|\341?\322\021\327\006#\373\331?c \373\306}O\354?\300\261NG\r\350\307?\344\350\221\2474\030\347?3\203M\226<\261\347?D\240\025Z\256\375\302?0\342C\025e\340\240?\002\256\275\355\003\003\350?C:.\262\220\232\342?)\034\0345NH\352?\324\203\201\027\005\256\321?\364\000;\021\214&\314?~\272\254\004\ri\324?\007?g\017\351\362\343?\313\310\250\231\342\307\341?\177DOG\212\204\355?\014\3548\240l\236\311?\320\251\255\345\274\334\305?|a\265Q\361\362\346?\274\345xC\256\233\303?P\3610\350\244\374\266?\200\330\234qmz\315?R\250\341[\240\340\352?`x\227HD\021\300?\267\303>\375\322\300\357?\036^\365~#r\324?\"\201\263<\016.\324?\300\001\245?\1778\316?\226W\3227\321\275\333?\000\020\334\\\235\300\262? \177,-\274\331\273?\202`b\3063k\330?\000\225\340\263=\024\263?\272(\324\031\204\037\356?\235\313j\352\270[\353?\370\306\307\204n\231\317?\344\353J-T\364\316?(\223\322\247s\335\340?\354s\177O8\234\346?\000%+\3316-\226?,(X\315\201\263\353?tz\345\253oY\357?@\235^7\213\t\346?\277\022\337v2\246\344?\010<\352n\256\342\312?t\337o&#@\353?\241\245f\225R\310\354?x\222\346\375\3265\340?\360\354I]\324\375\305?|S}\322\032\276\331?\261\275ayM\275\346?\343\221\345\260\256\364\344?\236\376)\004_\346\341?\304\017\204|v\252\346?*\265^_\235&\351?\000\243\205\230\367\027u?\360\277!\2347\023\256?\200\363\273NE2\333?\343\200\366$u\231\347?.\211\314\360\001s\337?\275\277\177EH+\351?8R\025\245\302\352\327?\350\353\361;\207/\327?\014\313]x\255\224\321?\347\220\365\300\0308\345?R\025\010\006\3078\331?\266\210\035\225\303;\343?@UJ\201\363\340\232?\220$\235}?\326\244?\\\2241\242\354\330\315?\033+\n7N\305\344?\227\322\362E7\020\350?\310\330>\234\2623\320?\246\037\240A\234\274\354?\222\2328V\261\017\325?\300;\270\345=\177\340?J4\375\367\346\017\354?\341\331\326\336]\234\341?\300\213\275;Sy\224?8L:>\304\322\314?Ta\274\342\313\305\330?\301\310\350\2078\'\352?\373\357oT\372/\350?\3209\220\346\374\263\301?\236\350~\225\330A\342?\370*\225\335\360\241\351?\000\223\324\230\265~\341?\224h\364*\302-\337?\316TiW\2709\336?\354\213Y\"O\361\355?\230\273s\250\367\030\330?\"a\264`\226\030\326?h\307\017\352\372.\323?V\301\240\034\021\020\343?\255U\316k\363\323\354?\315\257\270m\267\031\346?\016\2077pa\006\331?\3551\237\032t\276\357?\303K\260\265(\247\352?\"\274X\272\311\r\357?\034\034t^A\315\303?\310\247\257\257s\257\335?$A\267\303&\316\331?Z\246u]\216\202\351?\244\030\004B8\346\353?\027\325\024I\027\322\347?\266O\315\\\214D\345?{\317\252\017\264\206\355?\361\017\272\031\010\333\343?\262\021\234\373\274J\326?\226\344\2320\361T\335?\226\206F\222F\210\336?\217Ph\345|\"\353?\320\264\272\270\333`\301?\000\"gW\"js?$\021\323\004z\275\324?\314\354\350 `\335\335?Y.\367\311s)\353?C\235L\272u\304\356?\017\342`\341\334p\342?X\r\305%\337L\321?j\370\257\001s.\351?\\\266\2623\0203\325?\216\'s\311\253]\352?6\033}\'d\022\344?f+\205&\370\214\327?\212\241\242\r!\031\323?\300\257\370G\001d\230?(\341\000\023\202\205\316?\350\337\037\265\035\364\273?\307u\366\344\025\005\354?j\\\255M\210\254\327?\002\362\034\301\321\031\321?Xbi\375@\335\353?x\224\035\245\207\370\322?2\202R\347\366\321\337?\024\325\277\310\350)\311?\360w\3429\207\263\345?\r\300.a\034!\353?s\247\321\035\314\014\355?W\315\304\001I\365\347?\364\351\264\241\002\362\311? )\326\357\334t\312?\200\377\342W\245\236\257?\316~\271\314\266\343\341?\272\\p\372\000\354\337?v\302\271\311c\341\357?\254\014\250\244\321\030\336?\204\347\003\206\363\000\311?\000w<\302\302\246\343?\030G<\035 t\327?\264\004\371Q\307\010\321?Gxu~w\013\357?d\253\000\035\016\030\335?\2645k4\263\305\314?\237\231\343\204\342\212\355?\205xz\243\342j\340?L\275\2651\215\330\325?\340\242p\035\n$\271?D\362\370\262e\031\344?=\332|\276V\252\355?\300\303b\340H\036\233?\004\333\351\312\302\241\340?H\004\3564\\A\321?\002\307\361\211\235X\331?G?q\374fe\352?\332Q\344B}\014\323?&:\364\367M\010\347?\030\n\\\'\214\"\353?\261\262S\020[\311\345?\204Q\\:A\326\330?\261w\351h;#\356?Qq\335\300\324K\352?\253\207S\353g\265\341?h\323k\250\367 \317?)<$\0348y\342?\214\377d\336[_\323?\204\006X6\362\263\320?\360\341\324\026\236\365\322?\301D\024\370\304`\344?\344\341\310%\241l\301?\006\257\242\361Gm\342?\3307\235\211\330\312\333?`\367\007\377DQ\335?\320\034\204\020\346\334\350?\244\204\2309\223g\333?\210&\177\004\311<\345?\022\007\310\313\267\300\346?\240\025\034Os\203\234?\320\032m\250\243f\341?\314\334\035\321\213)\343?\277u\355,\3232\344?@F\343\245[.\252?\344\335\316\267\3725\351?\220i\274Pe\214\257?@\340&\207wa\306?\005T4m\2228\345?\316S\202\025\323}\353?\360\217\026\212+]\337?(z\365\274!Y\321?Y\276\302\255\270\277\342?n\322\2503\003\356\347?`E\355\212A|\277?x&\207\005\245\265\341?\224\277\224\270\363}\344?:I\216\202\310\242\336?\332\020Ah\256\260\343?\317\r\'\360\257\020\355?\300\302\322]Q\'\236?\227\244\026T\277\371\344?\237o\273\202~\022\347?\2401i\331R\035\223?\037\260d\371jT\345?\212I\360g\232\235\322?~pX\212?\353\337?\330s=\020Jl\276?\256$\021\274\031\221\333?x\270\001\016\224\251\300?r\303\326w\2777\354?W\200\211\361\023\262\346?\264\316\374]\365z\355?\372\\\022\251\234=\335?\274}\014e\330\373\313?\000\230\232\225#\017\257?\000\033\233bt\345\200?\200\215\356\2054<\344?\210H(\352\030\351\334?\216\276\337\272\245\266\342?u\212!*\361`\356?\030\375\243~JO\344?i\271\375j2~\341?\r\347\205@=\336\345?l<\345?\346\"\335?\300:\371\375w\r\350?\265\212uPJg\342?\014cr7\266R\312?\346\310\374\345c\211\337?\307_\301\344\266\017\350?\354\317\331\3475U\350?@b\370\003\224u\337?\000\303\225\022\215\351\332?\224\237]\227UR\322?\360\272\276E\361\016\341?\374\361\025?\271\271\305?W\3027\260v\013\353?\362\223\206!\265\322\337?\270J\005\246\007\037\323?\271\300\017\204\242p\351?\306\237\327\200;&\334?\360\244\352\324\317S\250?>\355\260\205+f\355?`{i\231\277\033\306?x\037\246|Yk\327?\206\211V7vZ\327?PJ\254\335\022\211\345?\327\260\335\202\216\360\350?\017\352\021\340\235\202\341?\014\302\233<|\377\332?M\300\327=D/\355?\220\305\366\323,a\244? \205H\262$\232\324?,~\376\247\317I\331?8\347sT&\204\357?6\003\207~\357H\335?\346+5\304\200$\322?\337\371\336\375T\321\340?2\307\231j\310\220\333?|q\021\203\337\025\315?me\005\334\356\232\347?\320E\320\330\332\325\317?Tz\232c\256x\313?\320\020\323\332,\207\321?#\231\320\226\335\316\356?\244\\\340\304I\201\300?\220\001\373f\213\273\327?\264\236(\361\313?\332?\004@|\230\220&\345?\242yi\243H\220\347?h\256\373du&\306?\370f\316\2649=\322?lC\240e\317\036\356?\300[a\235)Q\252?\374V7,\014\262\316?S\232\341\210\270\026\346?\271W\025\374!\026\341?\022l\001iin\342?.\306\263\346\020\250\354?T\216\010\230\025\254\306?B\262\013\021)\270\334?\000\017\333\347\273\023\345?\344\276\264f\225\037\320?\252+b\266T@\323?W\217\230\247\345\222\356?\300\202\3422U0\244?#\311\236(\225\232\343?\2309\331\317\224\320\314?\320\375\256\236\036x\327?D\320\006\247\364\037\331?`\027\215\020\216b\264?d\230\375\235\241\257\346?\353\344\256\336\320\364\347?\000\234O\0053\225T?\332\007\372no\377\327?\320\266}\237\241\316\341?0\340;\\\370S\352?^b\222\3062\262\343?\007L\334\230\341\316\356?\333\370\222\225\240\301\344?x\313\010+\343,\326?G \303F\0105\353?\362\223\005`\231\361\337?{z\237!U\351\350?\014n}N\2078\353?*[&\010\205t\334?0\324\271\256b2\321?s\333\266|\025\233\341?\224\360\343\256T\353\325?\352J\221\344\243h\333?\346E\347~0\177\350?\276\310o\354\254k\354?\266\213g\245H\304\343?\302\245\3176\036\r\330?\274\375\272\355\312H\347?\2340\374Z\'\205\314?t\025\017\325\220l\332?\324\373\255\377\215^\326?\320\342e\270\314)\265?W\244\372N\203\r\352?\252d\277-\345.\331?FIE}kP\323?H\207h\306\220\003\320?D\333zS7\210\345?r\001\332\336@\373\323?Qa\244]\035\034\356?\030\214\331\246\220=\341?l\255\300\020:\"\313?A\251\377+Hv\342?\024\365\333\005\243\243\332?\327\351\214\314\350\333\350?\264\004x3\227\376\322?\353g-\341H\373\356?8\013\367\003a\344\332?\270\361\003z57\275?\316\'!=\272\201\320?B\227r\360\221\247\356?\000w\221Je\226\332?`~v\025\275v\302?\372\3221|\211p\347?ze\017)\364P\353?\2748\003\004\375\261\323?\210_9\307XW\312?7\330\362\321\032R\356?h\026\tP\n\247\312?\251\265;\365\353\302\346?\240\017\340\366\231\332\273?\377\376y:\023\354\354?\340\"\345;]3\343?\276\223H\303[\274\334?\026\256R\020y:\320?\271\347(h!L\340?\334{P\376\240I\356?\262(\004\212\343\034\356?\220]o@P\253\243?l\005\307\215\334\377\333?\251B\324]\233\036\352?\034\336?H\275\335\322?\250G!\2769\023\356?\353\375\256\252\360\366\345?\020\204}K0\320\346?\014>\300\237\247\277\303?\304\330\242&]y\354?^s\336\233\022\023\344?\202\3719\204\305\313\347?\026g\036\352PX\334?\037\362\244\347\2326\340?\034\346Iq\233\260\356?\310LJt\270\014\322?\313s\364\361\300\233\356?\034^\034b-m\312?\"\370)\321}\023\321?\030\021\025\266;\302\323?\262.\0030\035\362\352?t(q\312\236r\310?\300\'\244+\350\303\270?:]\204^\374%\350?\274\223\340e*-\321?\373\240b\245\252\373\350?\310\302\017\353\035\203\307?\010\337\332\345\302\211\260?\317\0375\247\354\273\346?\300O\272Y\177\013\207?F,\364\023l|\353?\333K*5(T\347?\006\033\300\223Ky\320?\366\307.\350\302\252\345?T$\335\272wP\305?\364\031\007\325\2560\306?\024\207}\272\216\266\350?hI\214\r|\322\300?\\\"\367\334@\273\346?X\253\246\337\345,\347?\310\325d\357Pz\331?X\227\255t\353V\314?\300s\213\212j*\323?<\361\230\310u\007\324?\241sU+\231\377\350?\020B\254\214\270\327\320?\320\247h\303}\016\316?\010QeM\233\214\343?`\205\345eJM\232?\317\275I$h\022\357?\010\033\265\342\230n\317?\317z\345+i?\347?v\317H\376\217\376\341?\300\257\327\322\317\320\355?P\333\3675\251\313\273?\304\254+q\336\010\310?\312V\336\276\3173\337?=\337\3657\313\246\341?\306\372\332dR\215\321?j4\200\264\216\t\343?^\020Zj\2071\340?\270\330\010H\177\001\321?T\276\313\312\3127\313?\373\220\330\236\376\316\356?v\362\231\356q\214\357?=\357\002\357\3322\351?\202p\tvm\315\343?`\246\331\237_4\316?\024\215\nO\254\215\327?\203\351\311\247\226\207\341?,&\364p\211\257\340?@\025\273\313\302\005\357?s\273\313a\331\202\341?^\316\343\260\226y\327?Ui\324\273\363B\352?\000\024\237\2573\302e?\020\025\254\256p\355\255?4K\210\357(I\343?h\3603\017?\325\334?\374a\207 \232\002\346?J\032\332\255A\356\340?\210\2721x\260 \326?\251\314i4<;\357?6Sa]3\\\326?\261W5iu\031\346?\0041\'|@U\336?G\314\301mk!\351?\206z\255mz}\323?\257\r\025c\"n\355?\262U\n\3020&\324?\214\354\346\237\0309\340?\254c6\037]\206\315?\334\316\243\372\310\225\357?\356\214\333\315\274\003\321?0\231L\245\024@\240?\300\236\314o\312\010\300?\201n\"oZD\344?@\346,<$\215\234?]\217[\337\342\264\341?$XZ\246\300l\341?\245n\025a\305\234\355?p\370\330)q\352\301?Rs~v\004\370\346?\006\323\023b\016\201\322?Q\022\305\375\020\220\355?\030+&\300\002\354\264?\321.\020\213\204\315\354?\346\017\202\215\325j\321?\374\336\350d\026s\323?\262\246\005W\211_\347?\'! \344\270r\342?\324ei\3700k\353?hl\036c. \337?\320\317\310\215@\216\323?H\210;\227\253\221\331?\266\232\226\370H\334\357?j\336\240$,\340\325?`\213\361\256\341a\327?\210\010\220\373q2\260?\237\336X\301\303\333\345?\300<\365\375Y\352\261?2/\226o\337\324\350?=\3464\223\333\334\351?Mx\206\0244\025\347?/\313\310\3441\357\343?9\231,K1\345\345?\250E\244\332-[\350?\302Te&\021M\346?\230\273\324\372\270\361\354?\334&\354\001J_\351?\204O\376+\n\245\325?B\202e `\307\330?\252\314t!\224\265\346?\344?\350\220\177\312\036O\327?\335o\326?\374G\305\263v\336\355?\357J\203\314]O\352?\020\031\361\320\373\346\346?\371\305\322\237\301\232\350?\335d;7$K\354?\"\362\024R\013[\331?\010\0018,\274\025\331?\266\335\360\324\256\025\341?6\327\252\277\205/\324?\344\365m\262-\004\333?\246\024\251\225,\337\334?\232[t\363=\013\324?\330\217\3211\016a\345?\217\300Ut\316\336\340?K\327AC\351\007\341?\310\315\014jd\376\327?\322;\202D\037\245\337?\000P\373\371\251\362\317?\344mI\207\354\217\326?/\252\030\037*\274\346?\210\001|]\323\351\300?\236\207ve\204\331\331?\020\304\rDe&\257?lGF\360~\374\341?\342\001\231\254\222\304\320?\240\006\362>\243\"\356?\002w[oM\274\335?\366\323\2376\"U\341?\177\302\017\353\031S\344?\032\020\032\302\3006\340?Rx\341\014\250G\354??kZt\307\026\342?j]X\351h\350\335?\362S;T!u\355?\307\n\r\360\245\277\350?\373\204$7\366\020\343?\260\005\376b\323\220\346?\020\316\232\\\356\267\326?\361\376T|T\233\346?\2142\340|H!\303?\342\273d\225\211d\346?\364\006\207\353\373\267\313?H\230P\315;\243\324?\362S|\23204\340?\034\001s\021\236\325\310?\204\2069w8$\345?x\316\307\333\355\033\343?/\376;\211\306\372\346?\034\214M:\264u\332?\360\214\245\265\356\246\241?\324v3\324\313x\341?\276\315\374\312\351P\355?\254\315\242b\232\036\333?\200\002{\357\301\237\305?P\336ed7\274\256?\3520\374\311\216\345\356?P\027_\372\352\254\240?|dD\210\365u\307?@\237\206\\\2438\257?DiD\335j\227\342?\347\354\027\310\020e\354?7\006\237\321\256\211\340?->\325\374\273\252\347?,\333|\226\366\324\304?\272\001\370\306MV\322?\360P\322\330\010s\331?r)\374\346\223\177\354?A\212C\212\267\002\344?j\237\221\220l\243\350?\2764\027/\243\271\342?\024\227>\214\307{\317?\310\001w\215@\253\312?t\316XA\332\267\316?\306\213\334P-\314\326?0\006h\263!H\250?\223P\251\252\271\373\347?P\231\021P\264Z\337?sj\035\200\024\304\350?\340\227u\005\237\346\274?\343\0024\03798\352?\211Q_\356\364\205\354?P\205]\237\274\313\340?9B\344\262W\365\346?\272\003\360\267U_\337?0h\024\"\022r\346?\330\317tz\217\r\307?\256\350\352\221.\210\332?mCU\231\324\305\354?\334Tx%s\324\320? \352\202v\230\'\322?\330zbj>\010\314?\244-t\366/?\332?$\034\246\021\243\320\320?h\203\014b\0043\315?\356.\346\364[\232\322?\317\345\334\373\255\375\341?\000}Dl\242(\254?\336\326\273\353\245F\352?\271Z\254\004\242\356\351?\030\023_\377\205G\261?\000\300\302$ZZq?\270\302\331[\207M\305?\205 \245\030\224\031\354?@\275\377\020\270\002\334?\271\313\330{\030N\345?P5\255Ln\237\272?$\316a\203\311\005\344?,\032u\347\002\346\340?`\022H\371Z=\275?h\212\261\354\305E\273?\370\014J]\\\332\341?\360\365\331\006\253\366\250?f\341d\3015(\336?\306\250\t#43\353?\036\327\341\223y*\330?\340\247\375\275\222g\350?\350\360\326a\314\252\336?\350\371[\365\364I\343?B\\\277\264\001\362\356?\270\255/\266\3218\303?\016\363\322 u\337\356?0\320\007\261\247\371\256?\326A\204Y;\347\342?\227\215\272\222\233\r\341? \335!\355\305\254\303?|L\233\'N\212\354?\300\261\335\240W#\243?J\230\247Q\360B\342?\244j\325\301\202\332\304?n\023L6\030\267\332?\264O\366f\270\275\300?\310\306<\264\311<\355?D}\273%\373\305\344?\253\320\250\312\377\352\355?\025\235\2053d\226\340?\244\013\370\233\347w\332?\006F&a\235\263\345?\002\204`\331A/\327?\'_\320\337>\371\350?\230\342x\335Ze\335?\354c\207l&\377\326?\200\345\272e\271\271\246?\332\013\262\323\353\256\330?hb\016\346\333\211\337?\314-\317\222\211\220\303?\010\027\300J\006\343\334?\300\370@\256\231\205\265?1\267\320\257N\327\350?\270\372\245\3217\234\300?n\3573\312Sp\351?\240\222nM\266\304\334?\257\007\256\316Ru\345?W\035\343 \345\325\343?1\225G|\364\250\342?!\023\355\262\334/\346?\306s\352\273\266 \341?\353M\224\354jq\344?\210t\244\nP\004\302?\317\232\233m\332\006\350?$\022\001Y\250\000\330?\367\212\307\026\001l\342?\310c\217 <^\352?(\314\251o\013W\276?$\224\321/\275\250\311?H\314x\014U\266\353?2z^\363\032u\333?\305\327!y+\334\355?\337\300\256\030\3359\353?6\266\n\010\233\006\325?4\332\335\005{\345\356?\210i\033bZ\244\305?|\341\374*1\373\303?~\337i21D\331?h\033\356\277\304\340\264? \322\241YI5\240?\030<\361\373\321L\353?\001D\025|Q\331\350?\317\001`T\353\316\341?\224]/\351\221\\\341?H\216\017NI\341\325?\014\232.\242\037_\354?zj\237\006+\345\332?\360b\r\245s\352\265?Hn\300A\222b\357?I\177c3+\271\344?\235\000\250\020\244\320\354?\346\026\005\241\232\026\335?\352\222/Gr\005\346?\241\t\272\242Sg\342?\306w\237\341-A\336?9\262\302\\=[\355?\354\272\375\222z\243\310?\272\313\370S$\000\322?R\310p\224\337\255\327?\300\272\207\326\365A\275?\372-<\361\362\355\325?\216\013\005D\326\352\330?\260N\014\353v\355\312?$\257B\"\027\371\353?\256\014\010<\210z\354?8\271\371\013\016\370\271?x\001\303\241\315\353\327?\250\016\277\320\212O\300?#V\314X}\305\353?0\237\204\270\207J\335?8\0224sYY\305?\270\2753\241t\022\276?\014q\3746o)\336?L\211\021\356\221Y\330?R\024\262=\341\240\326?(C\321O\224\234\277?\215\345\241\310\3079\340?lY\241\354z\212\302?$}\373\272\010#\321?)\237\324\302\356\273\341?\270\024\010\365I\213\333?\254\211\201v\226\330\336?\266 \231\335A\317\324?S\'z\004\315\266\344?8\255\331q\342.\323?\030}\245\256l\325\341?@W 5\021\000\334?h\242\330=\257\364\345?p\205\321\t\276\331\255?\010\353\204t\364\254\272?\345\023\256\035\014\210\346?\000\372\315\303\356\205\300?\t\317Y\360\031E\347?f\t\206ii\254\350?\320I\202\341\341\026\267?\010\270\3575a\245\356?\246\364\232\343\245}\345?Vz\374\302\001\031\336?\360\367ah\374s\266?(\025\t9\246\347\341?\210\343\344\330\224\254\267?\274yX\367\301T\325?\340\212A}\304\207\265?\027\3378\354G\355\347?h\305\013\350T\302\351?P@Y:\374z\325?\266\215\256qQ\224\323?\262&\314\341C\335\342?\226\311\332\267\324d\357?\261z\250HU\222\343?\261*X\304\253\240\340?D\347(=\340\355\300?\230\247\312d\357\313\266?G\256)t1I\346?\'\353\261\263\214\327\342?\311\001\033\370(\274\356?\334\256\260\257MR\333?\024\247`\322zS\346?.z\343\260\342\244\344?\205\177\256\327e\264\340?\032/\260\237\304p\326?\240^N\233\202\265\334?\244\331\021\233\201\262\345? v\037=\024/\355?\330K\260\035\235\232\325?\233\0332\317\365\376\346?\307\017\240d\237\300\346?\257\207\260\245\210f\356?\036{!\254\244\t\347?S\036d&k\327\353?\000\017-@\245\242\317?n4\354\207Z \327?\000v\030P6\237\351?\336UJ\017h5\347?\230B\215\272\350\240\337?\362\270\333 X{\341?\014\357m\334\024g\337?p\264\306\211\327\323\271?\000\340*\276Z\267$?\240\360\300\006l\030\326?h\216A\021\233\315\321?(\341\361\026\325\220\315?\214(\216\241\317\000\351?\030%\255\311\217\213\301?x,\035q\356S\333?\220\037\303Z\024\324\242?\374\346\243\n\274\r\324?\023k\226\313\025\003\347?\377\"b\255<\243\345?#P*}\233\017\347?\362P\"7-\204\334?\355\300FR\032\305\354?\267\365\216<\335G\346?\004\007\224E\250i\301?\260\325]_\232\272\242?\332\"\266\230&\265\344?1\344\004&\324\250\355?\314a\312Q\227\325\312?\336M\033\356)A\330?\005\233\r\"\300\332\345?$\2510\306\344\306\300? \204\004_f\314\353?\242\326w\265\232\376\351?\200(\324\356\244\257\353?K;\365:\245\352\352?\370@\207\025\2753\323?\204\265D\336\016\024\337?2\322h\207\010\365\346?\002`VOCf\325?\277\201UM\001\234\343?\330\267\006vw\007\327?\212(\307\n\235F\346?\"6\006N\277\002\336?\310\277\025~\035\317\277?K\362c\227\264*\357?\276\352\260 \325G\342?D\332M\004\335\211\313?\360\334H\217jE\340?&Z\3362\032\256\341?\244\231m\014L\266\343?`\027\231\215\207\250\237?\223\030%C\255\224\354?\356W\010+P\275\353?\361`\243n\246\377\356?l\003W\321\3429\330?\364\251\233G\2324\316?\245\\BFc\221\341?\220u\224\226\026\235\301?\245T\"Y\243\265\350?\360\200\257\010K\206\265?\230j\356\227\257\316\325?\237\030>\311Jm\356?\270K\377\253\376\225\331?j&\3064O\323\346?\313\306?\332\210V\353?\273\324=C\203\214\350?\354V\327+\262\001\355?\0004{\014\276\314C?\002\236\266\270\374\r\354?\332\323\036\020z2\333?e\202\2406\361e\345?\"8jh\356\360\333?\260M\377\220U\315\353?\'M\212\252F\310\340?\304`\256K\257Z\351?\372\364\013zN0\353?\340\363\r:\266\355\236?\000\206\372\267\276\227a?\347\'\'2\332/\354?P\245{\312:\224\306?\320bl\304A@\247?\352\252W\205\"\305\327?\233yv\346zL\341?S\020|\260oF\341?\202\305\013\316%A\354?\3403\000^vE\244?\026\204\303\177)\303\343?PMj\243~\240\276?\200\000K\313\363\207\254?42O`\303\240\343?\200\234jfz\212s?\322\3202&\2202\357?\344.\254;\252\245\325?\260n<\270X\312\316?\330\216\345\027\210]\267?\305\270\324I\336B\341?P\302\320\315\300\022\340?R\024k (\206\342?\254\243\331\302+l\324?\214:7\345&\257\330?v\210\253\342\225\031\322?p\356Sg\326\347\272?\260\014\210\2240\232\274?\000\266\337\304\347V\255?\244@ho\360\262\331?F\310\353\262p/\357?\370\003\363\313jv\304?\272\r\031Sz\020\335?\246\334t6(4\325?\272\216\240R\243\022\340?\200\347\344\0253\343\302?\234\264V\363\347\240\355?\000j\031\217l\256\204?\000\032{\306R\242\257?\310\020\277\302\230]\272?\200!\212\3530y\274?\360\212T\332\014n\304?\206\033\356\014\311Y\357?\345\202(\026G\243\351?x\306\200\016\"*\301?\356\275}H\355\356\353?\036%5\\\367$\324?\031\224gO\262\363\354?\036\215Q\243in\347?>\330\303\032K\356\330?\274\362\352\335x\010\356?(\356\217\001\017\016\310?j \002\246\366\364\327?\256Il\267-G\347?\030\271\325\344\n\273\337?\222]\254\"N\347\327?\214k\270\220\352\250\357?l2\017A\0053\336?\232\311YP4*\352?x0\317\324^\255\313?\336\t%j-{\350?\010\230B\260\203\n\314?\234\331x0-\316\356?8{&.t\024\336?O\262\266\023\342\354\342?^YPJ\310\214\326?\204\023.\352\006\014\317?\004S\232\256\005]\305?\323\240\327\240\200x\350?\206\230\201\343\260Z\357?hD\322.\234\310\322?\352\305A\325B\207\351?\276f\003\377Wb\344?\000\241=\360f\315\340?\370\355\267BlK\277?\350\226\004\007\367a\270?\272\304\371\010$\257\322?\014\035\312n\314\262\315?\257\223;\256[\276\350?\206`\316\332\247\346\337?xK\234\2006\215\320?\020\2359\211\003\205\276?\214v\202\336\331W\321?\234\366H\246\024\367\310?\317\3079v\304[\356?\250V\220Y\342v\355?\344\037\007\215\017\035\355?\254\272\257\323p{\321?n\233\'R\371M\327?pV\332\306\272\374\257?\365q.L\374\245\344?\207l\313\343\036\371\352?K\303\215\350p\350\341?\354\303\210]\246D\333?\346\206\270\254\036B\355?\211\224|!6@\343?\374\263u\217\240\317\313?\235\\O\262\226\311\356?`\212\224&\203\220\334?4\005\224\n\251\222\350?n\001O\313p\021\321?\210ca\310\022c\314?dx\347\026\266^\327?\200\203\022Q\221\277\237?\204\214^\305\333v\304?\024|W\035W\330\345?\340\000c\274\275s\317?\310\372\215\217q4\353?\215\337\312\222y\303\343?~\010\260[Tj\342?\305\357\rZej\351?\340\3043\222\232\374\260?\322?\376\333e\233\343?\374\240\026](]\322?_\374c;\352N\352?j\2749=\365\224\356?0\244,\277\035\341\336?\250\322\353>F\266\344?\340\000\"\341\250\237\327?!\323\242\373\234\242\354?\224\'\217\340\010}\301? \222\232\231\021g\350?\215\031\275\364\310F\355?\334]\304^\360\000\300?\330\004\252\016l\013\303?\234\005\352\302\267\210\347?\024\032\035\254\240\366\353?\304\230{.x0\353?\304\350\320\0279\376\306?0\213\2178\263&\333?\344\266\010\372\362\364\352?X\221k\311\375/\344?#\2445\304\007\004\340?\341I3\027\277\313\350?\200\3478\367wqq?Ib\222\346Nw\346?\370VKL\352<\302?\000=\212\247\200c~?\360 !\233|\332\302?\213\262$^\310W\345?h\214\352\207\316z\261?k\200\001o\277\334\355?a!\334L\267E\353?\273\346\2665\200\254\354?9CR\245\271t\342?R6\246\253\r\370\343?P7\350\241\2710\270?=y\304\327\2667\356?l\025kf#\304\330?Y\367\312\016\306\346\356?t\233\341\325\r\331\336?\214=\006\343\016*\313?\213{\352\303\025\347\352?\342a\031H\346\257\336?\206\001\014\341\374\330\355?\340\017\3708\267n\344?[ \237\314_q\345?&\277\362\250\372\207\332?\263V:\274\374`\351?\226LYo?\207\343?\227\026\250\272\271\n\357?\210\203\347\327 \237\335?(\303\216\251n\317\317?E\277\350\263\014U\351?\336\020\021\366\315\226\323?g\303\317w\007\261\355?\030\264\310\020[\367\272?D\2358\346\270/\305?\014\227\233QxK\310?\366\257\235\257\355\201\352?\362\2429\035Z;\326?\364;j\034\240}\310?\365\n\n\237\251\254\350?\231\211r\302H\363\345?|D\022Yd\247\313?\366;\003\225N\021\353?\342\274g\277\242q\322?\302\2327\013\237\273\324?\237\227\334\275\374\225\347?\235j:F_8\342?~\177<\030\333\265\344?F3\027\355|\233\355?\360\372\261\252\014\002\325?\363\274W$\033A\342?\204\225\004Ed*\321?\340\025\315S\200\365\252?xB]H\317>\330?\0009|\202\305\031}?\370\257\362\347S\201\335?R\320p\331\224<\345?8\244:h\221\206\304?l\032y\203\336S\314?\320\0236!m\224\273?e\346\364\304~\036\340?\010.u\311k\321\274?\"P(Y8U\351?n\345Z\234\004x\346?\272\025~\302\027W\337?\350\343\363\341\277{\266?\025$\341\220\2035\352?\036\252H\303\343\177\327?K\002\'\301&\303\352?\342\354\241\177`\013\322?\266~\251:}f\355?2d\265j)f\325?r\251lH\025\n\323?\020%\005\254Z\232\324?\231%\014TLW\352?\326\327{\363\323\377\341?\022\263NQ\343 \336?+\010\324\306\270\037\355?\\\365\033\245n\266\320?\364\343\253\201\022\277\342?\330Z\272\310\370\256\262?,1M\277zS\324?\200\340\341\300\345\002\264?i\214\310\213\367\374\354?d\342\010\243D6\343?\320\037\014\214.*\243?q\337w}\033=\346?0E\'\026&\277\337?\000\204\315\227\236\004\314?n\313\034\344\347/\321?T\207}Ds\036\323?v\336P1\236\324\335?^\343\232N#\354\355?\354!\252\035\326c\333?2\256x^\307\002\345?( <\336\355\322\300?x\246\203\302\227c\307?\022\206\007\242\243\247\355?gd\211\264\336\010\343?\0344\322O\317\246\332?\237\031\345\347\226\326\354?\233z\177\247u\277\357?\3627&1\010\360\345?\227\223\241\"\355Y\340?\000\371w\022\200V\245?\213\220\340}\035$\356?p\036\211\250\214\227\257?\330\326s\354\316K\303?\315u~\270\267\365\346?\350\322\215\223\213\223\300?>\014\207\263\326\257\343?OB\n)\022\272\342?\354!\316P\305\261\306?\325\2669\006\304]\355?\351+&\372\372W\355?h\347k;\"Y\344?\202\350iZ\177u\344?\000G\013\000\377\222\273?\360\004\004\002\213s\350?\372K\236\271\330l\346?\240\243\336c\177\223\335?m\003V6\227;\353?\022\225\250m\307\034\326?\207b\236(T\222\354?\322\226\376\244\216\343\324?\\R\301GT\'\320?\232&\2727\221\002\321?\234\025mA\355\014\352?\232\355\003\345\211\232\340?\204\255\005\323+l\333?\220\317\001\365\247\237\316?\004<\220Pf\212\310?Z\323\223;\020/\330?\014^\327\251\211\010\337?\210\247\027\233\315\006\354?:V\027\032\263\362\331?0\025\010\244\027\361\246?\002\346\241\360\027\343\345?rGD~\014;\320?vf\270\347\315\035\325?\246\241\361\032.:\344?\246\207\240\303\306\334\356?HJ\nlY\271\312?\306,u\253\314\031\332? \243\352\366\t\236\321?\314n\0179cs\306?\264\020\227Z.\334\305?\240u\277#\217\350\273?\036d\336\"r\264\351?\333\000\033\025\243\314\352?\275\023q\037V\267\341?\034\023~w\342\"\354?\354\217\002\241` \354?\n\373\2377\375\240\336?\276vdX\247H\331?L_D\215\"\237\311?\260c3\254\312\324\352?\364_\246\251\310\343\303?\303\3643B\033\311\354?H\336\362\260\016\333\264?9z\017\213\352`\345?\034\357W\346\373-\333?\030~\376\t\017\265\321?$;\r\324(~\357?\231\205dF)\232\342?k\365N\004\025\332\341?\201\250\264\326\007\'\343?#Q\034\270\224\304\351?n\250II\247\254\335?F\2272\266\270I\325?\200\231~p\371Q\317?*0\230\311Gk\335?\037h\335A\333\"\354?\310\024R\026\3748\335?\2439\024\r\226a\341?8\341O\225\244%\337?\331[\023\203\3032\353?0X\354\351+\211\335?\220\002\\\322\223\333\317?$\350[X\263\022\303?j\034o\227\367s\321?\334\300I\231\207-\356?\000zZr\260\233n?\003\220t\037-y\353?\274\311pt\353\033\314?\0007\345MN\341\250?(U\263U\240W\301?\202\311\032\200\n\252\325?\223\366\314\3169\007\340?`\367\241\216\232\203\223?04\357|\224B\321?+9\035\204\332W\340?\r\275\214\022X\327\353?\024\020\353a\205\311\311?\246\310\325@\325_\354?^\344\220\316.\323\343?!t*\320\315#\345?\372J\375z\232Z\356?\270\353\341\270\020f\265?\274Ie~W\315\320?\005\337P\\\033\032\350?\251\250\322\007c\372\343?\224f\3606\237\255\303?\240($t\372=\273?;\235x\240D\345\340?\240\347\022Y1>\255?\344A\\\220nS\302?;\027c\017W\224\343?t\315*\000\312\304\330?\016\020m\210\343\357\337?`\206\263Lf\031\321?\204\227\253x\202\332\323?\301\245)i\225\327\354?\0138\244-\352s\347?\376\366`\265rF\324?\300\035c!\213\037\216?\002\030\322\216<}\343?h\327\364\363\023N\276?\245\257\213\302\337B\346?\302\223 \374\007\241\326?\000\263\013!\326z\262?\314\311\250t\023\306\332?@2w\326\"\311\303?v\331\004\036\250^\345?9\347\343\225\251e\342?>\206\331\230M\212\335?m\373\032\314\263)\357? \243\001\343\350\353\220?\010\301\275\231\316\201\322?\200\276\335\367\366\343\271?\322\025v\037\360@\346?8r\266<\224\367\345?V\'\242\366\225\322\325?St\373i!\004\343?v\"\266\344{|\336?\022\363\250\374\020\242\334?\366\206\372\032\342\321\354?\366Ycg\307\275\357?@k@\305(\215\316?\026N\330\222\274\274\357?\277_\260\307\tC\340?\277\256\266\2563\315\344?\210\254xw:\224\330?\3315DQ\237\347\341?Mxj[\032I\345?|\321\344\224D\340\304?\351\352\021e}\037\350?\314\237\232\177a\334\322?\310\004\241\325\237t\260?\032\027S\035@\000\357?\324\362u\276\242\334\343?\001\r\257\2638S\352?\006Q\355\360\233g\322?\2558\341\311\213\217\356?\223\312\344\205W8\347?\007\376\001\332\2257\356?\014|\3275\301\275\324?\276\325\207\016\206\311\326?r\225]$\233\240\334?\\\210\3334\270\345\321?\312(\323\272\363\341\344?e(]\3670B\351?\000\204#\304\232\240[?q\343)8\230z\351?\000\200G7?\320[?\244c\n\231\323\312\354?\010A_\026n\310\326?\245\225.\267\307^\357?P\207\320\005\314\252\275?\374\007rs\353\304\350?\220/\rdd\374\240?Xf\216^\242\264\310?`\366\252]\034\201\337?. \272P\272\315\344?\\$\351\333\313W\304?\000\325\203\332\213G\272?{:\257\303\377\003\345?~V\006\247\215\r\354?\256vLAF\206\344?z\303u%\263\354\334?\256|\001\\\234\255\323?\000\020\210\233^\211|?\244\314\226Py\224\321?0\265]T\341\241\257?w\301\037\246\300R\346?\210|\276\347\203\246\266?%\025\004\223\317\005\351?``\010h\'w\221?\260\301M\206b\030\325?\230\246\254\217i\240\354?\021\202\264\241\313b\353?\224\300\221\203\253\205\350?!UH1m\336\351?\260T\200\303P7\356?R^q\315f\265\320?\206[\026%\204\370\340?\000\2067F\377\036n?\344\0073G-\216\317?\260/\203q\370\315\344?^\027\224\025\250\215\325?,\375\017\317\225\332\326?\264\237\177\216zC\312?4\256W{\334J\307?\033W\270.\n1\355?|=t\326\243P\326?\n\014\250g\267\377\335?\377\315\034\tp\204\352?\344\"\025\210\304f\320?\240,0\303p\001\230?\346$\310\233c\237\324?EK\327\311\3451\346?\036\365\266(Q\200\344?\344\212\345\324\001n\322?\303\245<\203\363u\347?y\366\363\270\232\250\354?\\]\332\0045\014\305?F\213\n\321C\333\331?V\350\n\275\003\221\340?\224\227\233\025\372\231\312?\217`2\326\014B\343?^rOE>\204\327?\322\034Od5\347\354? P\203\362\0100\323?\367\325\205\036&Y\343?\272 \301\257\004k\334?\261\372\377\213\212\326\344?\263\331\205\210G\216\341?t\335\217\321\216\006\350??A\2779\343\250\346?\017D\230\310\332\271\343?\255\0046p\373o\346?\246=:\322J \321?\003\367\360F\'\220\350?\226\242\227}\213\"\324?\2150\374\251]!\350?\354I\306\370+[\312?\215/\362\nl\256\346?\306a\352\263\201\331\343?\036J.\212\247\240\336?!\3675\363( \354?8[5\341z\345\337?\3346\216|\014\340\351?\332vW\331\355\274\352?V\201\215\365\216\264\350?^\376\272\360-\002\351?M\312\226[\036\351\355?\206B\177\224~Q\326?\200\230\3670l\362\215?\260\210\205\306\223R\345?\223\222\371\226\\\006\342?\305\244b\343\266\274\347? ~\333\273\3625\225?\317\257?\257~y\344?L\325\336\026\023?\304?8P\356a@\332\322?-h\271S\\\375\352?~(\355\220Y\342\331?[uZJ\262\227\346?f\013r%dt\337?\204W\'\\\314z\332?@\375*\354\032h\221?*\375\253\375\305y\336?\225\357`\231\300;\346?r\3338\004\014\272\345?\320\277\r\331P\036\326?\304\204\360\\\014\014\320?X\204\236\266\230\350\352?\266\337q\225\307\351\332?b \355\253f\261\353?x\367\374\361\340\037\311?\222Z\216\323\377\245\335?_J\320\346:\217\341?L\n\201\323lH\342?\371\206\247iH\002\341?\242\264\017\234\210%\324?~M\214om\324\323?H&F1\001\254\335?\350\310q\227z\010\274?\202=Uu\230\201\347?\370\325 |a{\306?\277\262\236\257\212c\345?m\006\230\304~\222\342?:&\031B\242\304\322?\301\330\365V\336S\341?\256(T+\243\211\320?\000D\030p\214\310\306?\332\273mF\321x\336?\240\376\342\200\004o\227?\370\317\210\304\240!\270?\230\302e\211e;\310?\243\232\231\2126\221\340?\000\'\3010wY\241?8y(\370\226\357\337?p 2\360\033\352\303?;\274Z/*\311\357?\323\006\201\2756\317\344?\003wB0\256\342\346?\240\247\2766\256\227\340?Fe<*\315\016\335?`\257\224\2104|\267?\275P\307\310\031\376\344?p\264\335q\311\031\273?~8\241\275=q\322?\300\303\340\213\034\346\330?p\016\237\'7\234\277?\340D\005\200\222\363\310?LB\307\370t\337\311?\200\014\304\230\307\261w?\253^\301\336\022\362\342?\210t\n\302E\007\350?\344Ho#\223\376\317?\274C\375q\031\355\334?@\227\337Ah\025\265?\030\320%\034\027\032\264?\301x\0356\223_\357?o\337Bw\230\222\351?\026!\320\300\371\267\355?\3202\226>\234\375\317?\330\000\261\246\364\300\303?[Q\"\312\376\002\346?(\300\002\340\315\002\337?m\223\343p\244n\355?\316\214\371\252\351\177\345?\334*\356\264\317\262\312?`.c\025\231E\274?\2107\310\021M\035\272?>.\2248`\323\351?\203\264\224V\361\200\357?|v\367h\020\252\354?\376\024\026\302^@\342?p{\2511\"\010\265?\350\210\023\316\215\"\320?\252\312\230\264\360\225\351?X,R{\262\367\354?\010/\214\365^/\305?tOhg\254\207\341?6\300\240\251\014\227\331??q\341k\002:\356?~\370\2445P7\327?b\014\034\347-\256\354?\007/\005\242\333\365\343?\263\271G\364P\227\351?mv,\267\370=\315\323?V\263\237\206\251\212\331?\242\017\371\365lg\350?\370\024\255$\007\241\261?\016\031\314O\2055\336?0\362\010\277Rq\254?\036\342\262\'\266\343\347?Py/\2448\357\345?d\370]\314\303\037\306?\234\220U)\245\220\336?\240\300\337S_4\220?\210\272.\250\241{\303?\360\035s+\376\027\356?Fq\274<\315\313\343?\267\330\013\353~\255\346?\027\366\020\235\215\030\344?\n\372\252\334x\326\322?\013\244\035\352\r\022\355?x\273\034b\325\257\335?!\371\r\254.\240\343?M\226@\243(L\346?L\177q\245\220\026\327?\313t9i\201\006\352?8\334.\020|D\300?\240\263\tu\327\361\334?\304\212\206\027\275\010\311?\340z\312VVb\220?)\262Y-\017\273\345?\347\021\014;\320\212\354?,\327\311\263\324Z\325?\242\257\204$\240\206\352?\034~\000b\365\220\310?$\370\251\n\347?\343\033#{\"|\344?\244I\006B\374\201\342?\336\257\3149\025\273\353?\227\031\361\215!\026\356?\331B\021\246\n\260\351?\340j|\244\376K\274?Pk7H\244^\335?6\372Q\035\320\332\324?\"\035\021\260n\321\322?\300H$>\213,\205?\261\204\355XE\315\347?\273\023\273j\203\326\344?\243w\342\032\232\252\340?\267\353\232\255_\350\351?h\002\001\223\323K\342?\240M\265\335C\266\350?\016\327?T\367\221\350?\036\306\311E\347\377\346?\342\332\215\262\245y\332?td\235\320\222\307\310? p\323$\276o\224?\204\025\212s\307\246\331?\214\247M\377`\277\343?D\327k\234\202n\345?F`\344n\362\027\351?\016\002\254\"\322\316\345?\374\007]\005\326\263\306?`K\277\350\253\363\270?\337\036\261\354m\037\355?\210\237=^({\357?Z#/\315\326\004\343?\252\366\326b\010\375\335?>H^lh\332\323?\210\274\323?L\262\343?\300\271U\002 \004\304?Xh3\200Q\331\345?*\010I\366(\352\326?[\334\252\3418\025\342?\240F:\356A\261\227?hb\240\370\315\232\312?2\306:\026\303\017\343?\"\'\241\316OO\341?<\263j\246\201\r\357?\274XE\025\007\233\324?\270i\225P3\372\300?)a\301.]\r\344?&5.\311n\311\342?\252]\020\336~Q\343?s\314=\221\271\\\353?D\373\234\325h\035\340?\3360\300\347\005l\345?\2724\315\237\364\177\343?E\215}\365x\007\341?d\214#\263\2220\302?\314kj\352\311\234\350?,\310\227\214}\003\355?`\237\236\032\320\345\235?\000\337\t\226W\256p?kN\314b\317\336\342?\302Y\235\033\307\206\330?\352\354\310L\347\227\343?\346\243\031\265Bo\351?,`V~\013s\334?F.+N\226\330\346?0\3213\030\305!\277?\200\022\363\030\031\367\252?\321\321\330x\2533\355?\320B\323\302g\363\247?\350\215!\274\013a\306?|\nk\266C\t\352?\361Y:\217\360\360\351? \231#\26290\266?\320/\007\270\366F\323?\260^e\357\240\376\301?>\006\250\363\342\014\347?&\271\266l\200g\350?\374Z\027\274K$\345?=\363\277\235\024\372\342?\352[9Y@\201\344?\242\351\324\237$\311\343?\003\364\230&R\375\356?`\326\rlk\271\330?Q\026\367\307\204O\343?\240@f\030T@\275?(\217Se\022\202\353?\374\223\037\364\257?\332?\230u/0O\302\327?m\325\274\003$\214\355?\244\266p\315Cm\323?\252\370\260\000\337\356\340?\370\004\261\366\362=\341?v\n_Il\353\346?\002\206\032X,\343\325?\361?\306 ,\216\343?X\242\345\236y\304\357?\300hhd\273\345\307?\332U\202n*\033\325?\242\363\326\204o;\342?4\222J\255\324F\320?S\336\254\221\356j\357?n.#\003\340,\356?\005\216,L3\030\357?x}p\n\363\n\340?\030j\3117) \355?\220\224V\316\245\n\241?\350\302\t\277\216\340\356?\266\356\336+\253y\346?q\276?\306V\322\353?\t\347~\352\201{\340?\016?\223^\346O\333?88m\204\336\371\262?\316W\3166\\\026\340?\014\001\177\355G.\302?*D\334&*\037\345?\364\200\226\272e]\307?\374f7\3270_\316?\"\356\226\204\315\225\350?\222\265\202\217\3709\351?\230\250\2079O6\323?\336\221\364\026\302\023\340?\013\2525CA\266\353?\236t/\261\211\303\326?V&\350kCI\354?x\344\246\023\355\311\352?\227\267\t*\020P\347?\2246\355\024\265\316\350?\006\026\t\235\302f\354?2`\231\366\203B\342?\220\240\373~\261\343\354?\364\305FT\021\243\324?_9\241U{\267\343?AQ\177k-\204\352?:\273\270!/\026\345?\010\024\343\254ro\356?\340\223\375\000\275\364\222?\2549iF\007s\305?,N\363\356\034\027\342?T%\017\212\207\232\354?\272\177UtKi\322?\264\266c\223\\\373\305?v\365\324=\261\271\343?\351\017\252\316&\222\344?\254\251\r\237\323\377\343?m\357a }9\356?~\311y\202\373_\345?\246S^Z\240\347\347? \003\2031M\264\271?\350\230\037\3513\032\270?\250\004eq\r\230\307?\237\305S$\314M\341?\206+\360z/\216\337?\260\360\300-\206G\344?B\305\201C\362\316\341?\220$d\202v\000\265?.\366=;\263~\337?\007\022\203\005\372\351\353?H\216l\360LF\262?$\271\t?\371V\310?v\"\030\305\226\204\325?\300\225\272H\224\274\226?<\261\322\177\231\023\336?\227z5\237)r\340?0\030\215x\263q\343?\000\25532\362\230e?=J\305\320\236\302\352?v\206\365\331\"F\321?\355\334\n\006\373\027\346?\233\301\354\305\202\017\341?\244\333\213\203\226\032\317?\\\251{x\345\373\310?N\034B&\017\275\340?\337ym\333\261Q\346?\026\032\266C\0008\334?9\203Bx|\315\347?@P}q\356\347\210?\004\350^\307\362z\314?\250\275\330d5@\346?\t>\206\342\324\346\340?D|\024\373\315p\336?h_\rK\340o\340?\302d>F\213\226\353?,\026\007E\345.\320?\352?\271\245\234\261\337?\016\335\222\2436\320\353?\265S&I\353\325\351?\n\256\202\377\253z\350?\252\302\n0 \200\324?\003\204si\235\004\353?\214s\266\250\237\221\317?\344\261\177\373\347B\331?v\202\"\003i\001\323?\224\340\250\372\237\205\315?\r\014|\037\367\203\344?\032\207\365.\010\031\324?\251\034\207_\315e\352?\256\346q\345\1777\326?\305Y\351R\320e\341?\324\353\357\220`\235\331?\216$!\237\270\273\352?\035\327N\023\343P\346?D\333\025}\317\357\341?\361\377\035l{q\354?\264\017K\206\244\007\303?\3462,\305\304\325\333?\325t\254\020\250U\357?[\010\\\312\022\010\354?w\327-\277\243^\354?`\242[\004F\305\334?\340\304\000(\370R\276?N\006\375$\030\277\324?%\026\271(Q[\344?\034x\273?\204g\311?\021\032\235\355\320\222\355?HjT\273\333\326\273?@\243\306\236\334Y\346?$\037O\204\376\252\307?~:\340\027\241\243\352?\000T\"\337\324\341O?\000\035p\361\230\351i?\020\227\246G@\304\337?(\377n\236\361\226\344?\321\224:\261L\347\357?H\314\335\2226a\271?\264\032~\236G\236\310?\334\233\3003(\326\345?\273\251\203\272\022 \357?\240K5\370\\\355\307?l\307\020q\211\223\315?\345\261%\275N\255\340?\037BB\214,\322\345?\026\3425\346\310\224\352?\374\215f\t<\251\305?_\035\2043\364\273\355?\005\363Y\210=~\351?$\t\327\311$\256\304?\250\255,\220\205q\274?U}+$\334\022\350?=\215#\231\t\201\351?\0346\254\251\345\232\333?\3409ip\300\325\251?X\244\236\250\235H\323?$c|\344\022\004\356?\317\360\245W\201\257\345?\254b\250\2322\267\317?\r\362\014\026:\332\357?P\350)\366\204@\331?\004\026\250\007/\210\331?3\245\273h\337a\342?<`\273&HW\357?\316R\266\264\0034\350?\345h\'\014\200\025\355?\362\252wpf\335\320?\001\363z\205\003\022\353?\240\004\315\025p\003\267?`\320Y\227D/\250?\264R\332\2478\r\327?\320&\343\010w\361\313?\000\037\312\351\261\342\317?\010\341\344\244\357\177\317?\222\2618>\225\010\351?\215\035\222\r+\307\340?Ab\27507\200\350?l\371\246\347\242J\315?H\253\013\347T\366\337?e\267\n\215D(\346?P_\033\022\312\230\355?\350\006\364w\344\203\333?x\214\375\263\266\374\351?\242~\023\205\322\317\335?\3061\240b(\210\350?\372K%\327\275\234\327?<\034\367$\234\317\316?n\232$H\353P\352?S\344\025d]t\340?\2009\256/\376\361\310?{&\365Guz\340?\034\2376E\260o\340?C\3012\314e\332\356?k#%c\330\276\341?k\271`^_3\357?\256\255\260\\\241\242\347?*\226\311\232P\336\332?\\\352\274A\342\360\346?\004\2642\177\362;\357?\200\362\334\377[\307\223?T\301D\275c\004\317?\024m\254\323m\022\347?\304\264\231\222Tp\315?\260W\245\210\373\316\343?*\030\371\177\031U\323?\330\235p\307a\265\344?\245RvA\355*\350?\203\313G\377\225\251\341?\242\204\30494k\351?\240{\3076\330D\337?\250\274\226r\255\035\351?\0002\007lA\372\211?\350z\341\270\016m\270?\001\\L\010Q\376\353?\342\337\334`?y\322?`\250+J\243\222\342?\024\326\265\263y\336\330?9]\027\014\026(\342?0+?\333\347\344\243?2\'\357C\363\177\354?H\374\2657\202y\335? \037\373\236\322w\316?\250\025H\377}\036\336?P\240\332\020\035\210\244?\226\270\256yn\300\356?\373\007\353\222\312\223\342?\256g\017li\301\332?\202\254Kh\034\274\335?fox\037p^\345?\310f\220f\216\201\351?\350\315R\365\363x\332?T\265\"6\220\265\300?\241\303\2043\227Y\351?8^2\264\317P\262?\220\317\353\210U\327\333?\201\021\323\300 T\343?K\332\353\301\304\256\355?P%,I\270\244\306?\223\371\226\206[}\344?\274VkP9\213\314?m_\371k~\253\343?T\255\005\3506\022\327?\023i\215B\361\021\341?\223g\207\305\340\216\347?\304\252f\323\266-\346?\034G\236&N\305\321?\2039\374Q\211\372\355?\354]\344\240h&\350?\306\360=\371!m\323?m2 \222i\211\351?\272\375\326\240R\224\322?\360\2404y\027\210\314?\240\002.V\267W\264?\230\347\261\347\233\327\340?\364\031\316P\303\247\313?\250\347A\373U=\304?hK\360\202Y\247\345?\274\\\3175\n8\331?\273\236\210\327W\316\340?\224d\337\006P!\335?lMI\223\375\242\344?a7\300\233\203\233\356?\270\226\372~\327\304\301?\004\251b\253\036\252\324?\336r\307\206\205J\321?@\265\016R~\221\323?\001\332\247=\217M\341?z\274\273\347\253\325\343?J7\276)F\261\323?\020\335\377:\313\236\250?\340\360O\264\235\343\246?q\342\374\335Y2\347?r\227\226\r;\013\323?|\333\302\272\253\026\343?h\272\342\263g\002\353?\206b\305y\003\016\344?\376\367\376\234{&\355?\020\021B\272\240R\303?\360&\236\367\370\322\312?\200]\350\033Z\305\313?\260{_\320\016\352\275?3\312\353l\345\035\341?\\$\374[\237\244\303?d(r\272\227\333\357?\370\257\245\253\224\354\343?\031\263EGV\013\343?\365\370}\335.{\357?_\203d\302t\033\353?\016\020\255\350\213\033\330?\200~[f\025?s?\"\354\300\247Lp\324?\000!{\tQ?\325?p\301O\204\227\366\253?24\256\026h\213\350?l$?s\313\325\330?1\273 \221\323\375\340?x\316\367z\346\005\301?\0102\nP\257`\332?\002\312\233\361q\254\345?\343\3575R\354\354\342?Sqc\325\307k\344?\260\332>\267\205\203\325?C\n7\325BK\353?\270R)_\021-\267?\214\010\362\214\002]\330?~\253\013\347\244>\357?x\023\030\036\"\321\277?\316E\317\020y\371\350?js/\226\242B\341?<\341l\\G\311\324?y\266\270Tv\200\341?4$\013}\221X\302?\2368)~\226\340\330?h\310\375\225sW\345?x}\213\252dd\263?\000e\204\317K<\316?ye\355X\005%\350?1\032\214\211x|\356?\002c\203\n]\232\323?+\277=\251\233\200\346?\363\260\017\3212L\344?d\275\330\033\312=\354?T\243\232,\331\346\343?\017\177?&P\025\346?\2030RN\202L\353?Kf\254\366y\322\352?\264(\322\245e\345\346?+\333t/K\256\354?7X\352L\266h\352?@\023\3013\226H\207?\354s\327\005\274M\314?*\322\001o\243\231\330?Z\375\377}\3703\322? }:UPz\253?\301\217U$\343\321\353?3\027\332\365\364\177\346?s;\034\347\006\232\345?W\002\270\024\202\335\346?D\2007u\362\010\351?\312\004W].X\332?l\226\207\344\375\271\320?l#\024\024?\'\331?\364tv\353\316\034\356?\000\237\365\273\'\340\351?&\230r\365\206\245\343?HBw\215\032\345\335? Qd\331e\216\250?h7\261\030\363\371\307?d\364\257\207i\320\323?\210(\310B\361/\330?\001}\004\235E\r\341?\200M\273\361\327\270\202?tv\372>!:\354?\244\351\244\357\032\224\304?\273\207lhv\344\342?@ g\333vW\316?\310sO}\263}\327?P\334\020\2414\010\345?\253lc\001B\024\356?4<\256[\207\326\307?\370=C\240\374\225\313?]A\302}w+\352?}\266\037\326\324\303\347?J\214\r\313\360\342\356?\202\314d8J\343\345?\2204\317Y\315\344\316?\243`\034\304\313\252\352?\031u,O\351\272\343?\310u\004Ki0\264?\014\304\'\210\261\266\325?\235t^\263\257\023\353?@\315\236\005\305\035\233?\245p\000\332!!\351?\353\325\201x\n\'\355?\260*\240\230zE\275?\003`\262Y\253\336\344?\220\276\200\330\216\316\323?e\031\254\216\335\251\356?\210\005\343R\347\r\325?jP\216\022\371\242\352?\246\303\244\010\177;\327?\220\022\n\272\323\264\241?\230\367\2538\177\321\304?\337$\001\013\217\005\357?\200c\237\352\213\302\305?\036\273Iz\251j\335?H\'\314\322\020B\266?J\3446*\252\007\336?i9\3756>:\353?p;O\t\177\314\266?\013m\372\246\022\032\354?\nA\246\216{V\356?m\205\300Y\316\221\353?\003/\341\214\330\035\357?p\261\373\t\037\243\273?\372\353\250\034\216Q\354?\036\324\243\277t]\322?.\005\336\363<\313\333?@\274\305\232\177\213\272?,QZ\374\032\353\347?@\343\355*\031A\323?@F\254W/b\303?\271\342^F\232\307\344?:\322V\256\"\023\336?Xk\013\254\'\264\300?J\310\365\245X\360\343?\036\020\260\004\275\201\330?x*.v\202G\317?6`\266S\016\371\346?P\0348\356\346w\306?\325\355\240-\r\370\352?8y\217\245L\325\353?A8\301p+\346\351?;\207>c\324\363\355?\373\332Hv\021R\353?\001o\374\022\005e\351?\234J\006\275?\251\323?\333\303X\313\246\320\351?I\345\242\222K\334\347?\312\220\3761\331\363\335?H\001b@\330\301\260?\315\251\217\250\002\355\355?0\245t;k\372\322?d\354P\025\212`\345?X\206\311\250@5\306?P\361\214\034\266\026\305?\233U\\|\226\265\340?8\347^\216U,\266?6\027Q\224\220\177\352?\220\220\204\007\to\276?\300\330\270\325\257\250\234?00{\225\301\364\251?/\254\314\345\337\264\341?\262f\352\371\337\351\321?\260\324!\005~U\307?\000\3008>\242\363Y?&\264\002H\031e\322?X\213\243\211\376[\333?\300+\322\356\336\246\233?\006\323\277\0300\247\357?\034\2679B)\035\344?I\246\260\3422\261\342?\233cu\315*\350\340?n\307\332&{#\322?\374\317W\310\t\253\303?\330\250/\005\236\225\271?P\324\345\205Q\243\276?\224\336\010JN\177\327?p\036+,\344\311\315?\362\227\203`T\312\340?\014\353\031\226;\031\322?\350\222\032\201h6\270?,\334#\206\365Y\314?\270z\375\202=B\330?`=\021\246\335\005\262?\206=\311\014\334u\346?|\2144\020\n\354\305?\200>e\236&\013\212?r\222\263K\346\210\333?\264\203\356O\356\211\351?`\302\030\266\231\204\351?\000*\n)\212fU?\010\233\351\226ZV\267?\270\013b\356M\021\326?vr\320m,\254\323?FV\224;p\316\327?@\245\261\365\375\242\257?\2062\324Q\350*\327?\034\255\317K\016\005\354?\332\377SX\224\354\353?\260\300G\276bM\251?\204C\t\355\017\326\314?\275\320\223\367\336\312\343?5\233i\347\233\035\354?\375\230\360y\306\201\357?\00087\225\211B\262?4\335\030\3462M\320?h\020\001iu\231\340?b\252\030\353N\375\327?\320\205\037d\254W\353?6\307\353)1\241\332?4\246G\275\366\001\317?p\001epg\332\276?u\210\337\021*\271\347?\021\366\025\016O.\352?\233\257\367[\370y\353?\214+\256\305\336W\334?faS\307\231\236\350?\000\320\037^-C\256?1\"\357\245\236\251\351?\314\223\370\372Z\004\346?\361\220\220\207\246s\343?\220Lb=\032a\302?\020\006\377T\251\020\332?]\322j\320\225S\351?\2449\017\330I\300\342?L\276\360W\2006\336?x\220 \256\337\"\306?T\034dN!\275\346?R{\022R\023\277\341?!-\251\302\304\r\341?0\323\254\246vV\341?b\241\276g\336w\355?J\013\242\267\212W\335?G\226\307\177\273\216\355?\320\370,\357|j\332?\222\010\362^\253\326\320?\3600m?\200e\277?\244\346+\320M\346\345?\030\227\2419<]\316?\310eDD^\365\344?\214\000?0K\252\351?\013\272|\346\247s\352?\200-2\276\004\352\237?\010\274\213\244@e\344?h\357~\373Q@\355?\322A!\212>C\351?>\231\276\355\336\367\332?\210\256\235t\r\214\274?l\232\241\204\336f\314?\0001\221\230\257/\336?\210\026\207\251\240\313\351?\260\262\026\211\374s\325?\245\\\215D\247\003\357?\304\364\022e\341\313\320?\354;E\022i\332\316?\224&\370\226\034A\346?ym\224\251Y\347\340?`\365R\336\2278\343?\034\222q`{+\330?L!#\236m\n\322?0\220\216%\245\036\345?\200\037\266\342IU\240?$\226\224\274J\231\343?\331\004\375%\237\374\354?5\247S\035\020\000\343?Y\343\3312\275\262\357?\261G\004\343\242\366\357?Z=a\234\263\224\340?&\326\343fa\360\354?\244\263\021\241W\317\327?\210\000S}\313)\347?\370\024\343z\235\273\272?\345\243\246\352\263\002\353?\333\201\213\317e\213\346?\022\3311\240\017\311\326?4\236\226\356\243\376\327?Dj\233\247OW\357?\355\304\216\361[\010\341?\251\260.i\262\260\351?@\000:\304\\\315\346?l\205.\"\3444\353?\304;\030\216\016\270\341?\326\363\206B\266\014\322? \365w\031\322V\262?\273\322^\314\315S\340?\245T9\237\231\255\343?t\3528+\303\303\344?\374~\314o\313\236\347?\344\313Jysm\350?\221\237\000\347\032\246\353?0:F\356\363\263\302?\360jo\214\276\247\306?\226\314K\367\361\335\351?~\263\247Qf\370\345?B\322\211\335\237\303\354?L\263C\r;T\340?d0E\210\016\326\331?\016]\027I3\215\324?\252.\024\027\300\357\334?\000`\201\232\036\316s?\336\226oK&\333\327?X[\243 \262\365\354?(o\304\212c\254\304?\210\\\232\273\006\177\333?&\017\024\263f\333\327?@#K\321Ld\306?\300}lQ\364;\222?c\001i\211\235S\354?\002\222+1\r\312\337?\177\253\356P\031\264\352?\004{n\306\002\242\303?\340\204\3379\201\274\230?.U\271\215z,\343?\224gN\237\224\307\350?PI\273\220\236k\241?\000%\222z_D\307?\300\346FB\250\007\214?\364\361\347\260\346d\310?\340\263\031\272Ha\252?\360V\272\231\377\344\343?^\226-0p\202\356?\247&\224\322\005\002\356?\240\037\030\205$\260\230?[M\236\003\000\231\342?\256\301\213\306\240\231\333?\256\177z\214f\267\350?\260\034Y7-\257\352?2H\205\201\221E\354?\020^B\352\346b\273?pt\n\350\243?\260?6\333i,%\271\350?U\363\367\222\r1\347?\372j\023\tp\207\322?\200R,\373\016\361u?\031\374T\267\t\244\351?\006Q\226h\013\314\340?\264\272\342\3331c\354?:zs\006\203}\337?\034\377E\263\010\373\332?\203\355%\200\230X\357?PW\034\377\367l\342?\300\344\316\272\240\350\345?\240f(\200_4\237?\332;\242A\261\026\352?00\215s\376-\316?\031\237\317*C\352\350?\224\233\340\tjQ\357?d\305\273\nd\366\332?\204\303\221\264\032w\301?T#\004H\353\304\330?\336\333\177\263\215\341\353?\007rw\r\300u\354?\000\313\377\322\301t\226?\252\261\244\036\333\215\355?\021\234ulr\340\354?\2541*a\252\207\326?zVj\237\344T\335?\367\212\333\310+\025\342?\210+\253\250\262\236\357?C\264\233\3013\375\353?\255@\240\224\214\013\351?8\354\341\315M\020\276?\276\224\001R\032/\340?\334\370\325\363\036\036\321?H\270\360\247\232\004\262?\252\351\243\023F\376\325?wH\001\355\274\025\357?vQ\330\303Q\225\333?\020\302\367\333_\210\271?,\354.\335LQ\335?\302\241\312B\333\227\331?\323\244jQ\356L\345?\206\003\210W\332\355\346?(\216k\354F\360\331?(h\353\241\266R\356?uo\342\327\340!\352?\240\020l\025\343b\344?\000\305v\360\311\347\276?\366\266*Z\017\"\346?1\320\304\245\237\322\351?\355\327\316\316G\026\351?n\353m\207W\005\342?\017@:\237\345\216\356?Swz\230\374]\340?\302,p\322ac\323?P\376<\230M>\265?\363\034\245\026a\246\353?\250!\037\035*\371\350?\250`h\233\227c\277?E\004\n\306\342\256\356?\300\331\322\355\030\226\330?\n`\211^ZY\333?5\330\253\267<\330\352?\357*&\352\201\351\356?\230\262P\027?\344\323?x\354\2464\014\270\263?\250\305Sn\202A\344?\330\230\350\3347\342\335?\201\260H\220\310M\342?\225\'jD\nc\353?\007\376\262\350Z*\343?b\027\214\177Vc\334?\030\r\010\306#\237\306?\207\302k\264\346z\351?&\032b\235\237\221\355?\260X\374r\344\031\353?,\211\243\345\252\315\305?T_\375\375V\377\337?l\377\367z\211a\323?\336\267\013\3141\340\321?\320M\027f\303\234\253?\375\351{\345\243\350\353?\372\006\004E\004\r\355?R\237.6\215t\327?V\224\'\207\026j\333?\2600\242\001\305r\312?\366\\G0\221\256\344?q\256\t\001\250\266\345?\212*a\346\246\376\326?\351\002\330\177M\326\344?f\242$}\016v\323?\253\322\334\311#\307\356?\010\"o`4\236\357?\3253\352+\233\027\356?p\335\337v\2460\267?\177\235\n\206\335*\350?\031\003\307\266P\307\344?L)\243\302\001\305\311?\010\003l\237\231F\267?p\320H\007\363\253\334?\362\037\377\266R\361\357?m\345\307\201CN\353?\213vn\370\233\021\347?^k*\214p\"\342? \007\375\217\331,\241?@\242\355\345[\215\277?^\201\327\036\251\206\336?\260k\017\376\200\254\343?H\346^)\025\376\266?x]\326S\330\017\306?\277\177\227\313\273T\354?\252=F\237\317\271\347?\330k\242\342\356\241\313?Q\371\372\2367S\347?\342\353\323<\253\006\346?\3100i\330\010\350\267?\217f\010\321;7\341?\002\302\353`?Z\351?\220h\326\262\376D\344?!\226A\r\322\337\347?h\332\024\017\340\247\261?R4\030 \020\037\351?\312v\327\227E/\357?D\376\203\303D\n\313?$\247\255\210$\211\330?\305\010\270\265\013\002\356?\265\002\002\2650\232\341?\006\340\225\362y\202\347?(\314\335\316\300p\265?\007\376\253\211w\017\341?\341&H\177!\227\343?\310\020\363Y\265\000\316?\212\032\234\377L\342\354?\235\032\215\234q\313\346?[\301\255\363\251\207\346?\262\312s\226O\204\354?\370\364\023\304\241 \315?\010\025\340\367\267\276\272?\275c\212\217\231\321\342?5\003\034K|\206\357?\227\251gb\200y\347?88\354\261#9\277?w|\364\321\233/\356?H8.\376\303\307\340?\360\365c\263\000\214\265?x\337\303\215J\241\275?G\234SIC\375\356?YH\021\010wX\351?0>\376!\2572\263?(\331\n\340\374.\302?x\302\225q~\233\276?H\004\263\202>\337\330?93+\343I\013\352?\004\325H\030*\314\335?\233\006f\241\230\000\355?Kl\307!\3601\356?~\r\342\231\033\361\331?\337\277\327\354A\262\350?\354\177=\236y\270\342?\342\231\223&2\243\340?U\201\010(\333\357\352?\230Uk\3009\334\327?\251\301\274\245P\267\346?r\274\236\366t\202\327?O8\271PK\233\352?\r\246^\312\260\321\340?\304f\3138w\230\336?\034\224\024\200\300\322\340?_\2019~\255F\346?\000\234\tT]\356\225?$p\256\253C\371\357?\266\032s\310j\203\321?\336LTo^\023\324?\254C\316\300\216\233\331?Ht\031;\211W\344?n\212\323\311\347\377\343?\"\240\033_h\242\334?\374\253b\"\346~\342?\330O\002d\274M\305?\3027\010\371\3156\352?y\352\304y|H\354?P\177\030\364\010\324\350?\371g\245.\331\267\341?\352p\364Y\261\370\334?\367\336\273\253\256\332\341?\327\352\2508\300r\346?8\036p\357\372\264\272?\264\021\270\005\032w\335?j\332\211\024\366\225\345?\243\306\227\361\021|\341?\314\303\215m \362\304?*\177P\355\273\010\343?.N\352*\000b\321?6F\225\250\354G\343?\251\270\300r]S\343?\370\246\324f-\277\273?\226\307\0264\307\245\351?n|\321\250`\240\337?\310\326$O${\324?\240\313;\235\256\350\343?\262\226\273[m%\355?\\t-\030\335\346\343?\366d\034\367\0376\355?\032H)\242\036\013\342?\000+\240d^\013\327?\017\360\343BQ\314\340?Vq\'E\231\263\345?P6|\345>?\270?|\242\033\222\237\366\307?7\"\027\256\260U\355?~\257j,\242\273\354?,H\236Y\232f\352?\274\357\247\356L4\345?\364\206V\311\222\366\332?\315\010\033\352N\365\351?\325RG\217\274\250\354?\203\362mU\311\323\341?\376\367\261\336`\021\350?J\020\302\343\325l\356?\021\177S\231tB\340?\010\370\t_\306\t\326?b(-_\236\001\326?}\246\030\032;}\346?@eD\177\365\224\326?\204\233\234j\235\024\353?\262w\037\235\360\004\320?\000C\236P\010\016\331?\264\343\344\006$\216\324?\240\373\031\3645^\234?u\246E\272St\340?\354\221#\000\210n\354?\274\303\013H\036\240\324?\240\355\303\264\231N\305?~}\261\366i\255\325?dN\220\277\343\235\315?\300\023\211\244\266\242\346?\036\317\354\360\\\225\357?\361\320Si&$\356?\235\324\366L\204\335\342?\024LR\325XK\353?\224n\005\211\335\360\333?gl\301W]=\357?\\\257~$\253c\353?\377\317\367&\317\354\350?\255\327\240\262\260\357\346?\240\312\024\376\371g\251?L4\330\365^\003\321?\320\200\2268\022S\244?\250\356v\221)\210\303?`^\3029\370]\316?K\2314sd\223\351?\013\036\336\347\313\343\351?J\234\233\273\223\332\322?\364\222a\034\224Y\325?\216\023\342\313\0260\340?\020\336\213\231j\'\313?\375\201\301\305 L\356?\036\"A\024\002\360\324?D\032\255\231\350\372\302?\010\034\203\014\302\224\344?\000:\317R5\342\324?\332\232\266\303\314f\356?H\367\307/\030\203\307?=\241=\313\373\256\340?\270\274\304W?.\323?\200\302\2243\\\364\356?\267F\016^\274-\345?\331\224T\367@#\354?\210\032\247\366\341\005\302?\220@V\320\332<\246?J\322\n\'R\333\347?\234\247\211t\244@\341?\024\307\262\264y\341\342?\262\234\253\001\232\232\357?\rw;A\022P\343?\233-\000\330\0223\350?\207C\206\311\000\345\351?\311\014\021\373\020\320\353?~\263\270\007\311(\337?m.\257\224\037\027\341?\343oN1rU\356?Z?\254\223\337N\345?J\304\301\300K\004\333?|\336\326\335\331q\336?p\001\003\252!B\254?\010\031:1\315M\277?\342\254T$\235\272\322?`\204\250\177mk\222?g\232}l\201J\346?\252 s\305\356\002\355?k\233\227W\220\223\345?\244\335\235\214\370\370\356?!=\177\026\013\023\352?\304\017\271A\351H\303?\347r\335\242Y\372\357?\317\006!;\3531\350?L\033\002 c\252\345?\336\nb\255\275\225\320?#z\032/\340\276\356?t<7T=\254\353?)\2236\037Y\006\356?\213\305)\200\313y\340?\260\006\227,\376\006\344? \t\370\327\217\277\273?\240\225\217\265\230=\264?\300Q>\261\n\031\275?\243\245\213I\022\230\342?\205q\255i\014\276\356?\340\265\323\344 \254\350?\004\367\263\210x\035\312?0\260\020\n\334\343\323?\326\034\225\323\302\023\342?\354\343G\362\264\327\323?<[\267 9\370\300?\014\236\217\355\260\207\357?\376 \333\220d\243\347?\342[\t\264\376\241\340?I\ruY\036G\355?x\027\r\\\357\367\351?\376\274\366\037\320-\341?\244\002\037\341=\000\352?\250\254\275\\N~\335?\240H\030w&\311\330?\000\207\276 \366v\316?\320\3656\2638t\246?t?\025\301L\312\343?\200V\'\360\350Pq?\263,3\375\202\367\355?\004\203\211\233\232~\347?5\030\364V\364A\341?@>\037.k\305\341?\350\236\033\317\341\322\353?\232\316\260\305i2\352?\354\243\337\2421\204\357?\332\264\013\271\350\031\321?\202\307\215\025\270\230\350?\322?\377\350,3\320?$6*\355K\300\315?\250*(#\234*\276?\240\254\307\202\361d\355?\246F\032\335\301\035\327?\010B\305\0107u\342?dL\257t\030\277\331?0 \333\337\002L\357?;\026}\237\222>\345?\363\256dq\362u\343?B\226\216\235\260\017\351?\000D\016\024\202\004\337?\020\266\262\333\340\200\252?4\267\214v\215\305\305?\260\337\303\032\343\336\243? \261\013\016E\332\357?P(^\314z8\252?A\022\250\352\3648\357?\354v\263R}9\354?\010\007X4\207@\341?\3529\026 E?\327?`\311N\310\035\021\257?\355\3509[\273p\352?\347\372\231x\235\226\353?\362\234N@\304L\343?\252\354\250\271\364\200\330?F\273\\\020\251\210\324?\200\347\262\224\353*\260?\347\t\354K\351m\345?\3606\000q\370\235\315?nf\246\227=}\352?\220\trv\324\000\311?%\243\0317i\262\355?\330\007;\377R\353\335?4\217\367\226cN\334?t\001\035\334\260I\347?\\\215\302\200\262\262\337?K4\343\311r2\353?\032\n/|\366i\335?\r;x\355\023\366\353?H\341\225:\211M\301?\271=L\205\304z\344?\250\n\036nK\365\267?\200\003k\372\306\254\263?\362\207\315i+%\344?\213+\014\253\020&\347?\212\235\251A\017\267\354? \362\375\274\213\347\260?f\320\276\245\216\247\345?\230\035\013\271\316e\271?\303\014\331\223;d\356?\224l(\232\240\323\335?<\210\375\372H\r\352?\210(\331\324\0014\343?Iw\3015\005B\340?\205\264\243\265<<\351?\024\356cb\020\255\340?\204\213=\025\'C\307?\251\261J\257x\020\353?X\375\362\222\024\005\333?^3F\350\355\353\356?\016{\334\032\270.\337?:\266:\266}\000\321?p+\227s\312\255\273?\333@\313\n\345\322\357?\216\260\344\014\260\267\321?V\216\200\375\233\353\345?>\035\0322\341\007\341?\310.\305\241_d\326?\262!\221\255\350\'\356?l\0262G|\363\357?\014\3141\251\010A\332?\350@=\207\366A\302?\260\363\330s\007Z\333?\267p\356\266\370a\346?\005\032\311\372U\231\344?\000\003\337p\245=\254?h\031\217!\261\235\324?\377\241\350\276\302i\351?\323Dta\320\251\350?\222\3701\312\016\252\336?^\217\357\016l\322\347?\237|o\361\335\200\357?\210!\202,\302H\355?\362\227u\025|\305\350?\320\237\324^\353.\342?\320\024/k\262\202\260?\351*W\321]\253\345?\252\313\023\204\370\315\350?+\340\305\256\221\'\354?\245\007\207.U\220\342?x\246\010!\321(\353?\036\235F-\273\301\353?@\2101\235/\304\210?\316\020pu&\000\334?4\344\244|\242F\333?\234\227\352&\037\237\314?LP\356\302\213\331\330?b\333\\\341N\313\325?$c\212kF\301\302?\024f\246y\347\330\351?\210Y\201P\252\321\267?\367\001\325\033\035\374\341?`\217\000\036\235\333\342?\254iG\246\343\367\324?\303]\223\252\256Z\347?\364\212\037\310\374\374\306?\337\201\244\200\341a\355?`,\220!\206\263\276?\\c\307cM\307\314?\t\'\267)I\343\355?,\375\014X\360\225\326?\024\326c\377\327\202\333?P\"\261*\352k\243?l\214/\314\232\036\317?4B\262Q\0056\317?\014\261\037\363@\005\330?\340\n\264qLm\320?\200\275\256\366\313\314\341?\350|U\311\326\316\354?\016\236\336FA6\335?\210@O\030Uv\266?\255:3s\230a\350?0*\007\017n\357\303?\023\303\3651\256\006\354?\310\322T\264l\337\261?\\\301=\030<\317\347?\313o\322X\200_\340?z\034\355\005\304\307\343??\337\3731\201\325\342?\264\231\037a\032\255\320?\n\002\241H\276\331\355?\236\257\366\264\244\231\323?\260\365\320m:i\267?\301\272\337\024 f\354?\372\251hET\304\357?\260\265\000<\210\335\275?Zu\306\247\016,\323?t\016\213\232^\302\311?\212\360\343\335\250\354\332?\200\025F.B\336\314?\360\353\037\326p\230\251?jM\370\340`\025\355?\007\2431\231\345b\344?\220k\210!P\364\304?Y\002\224\226&\264\356?\027\326#>\362\320\340?\326\261\200\301\207\233\341?p\347c\201\247:\243?\000S \365\000q\336?\026\005X&\221@\325?\000 \336\337\271\221\234?\256\346\035$\367\237\350?\260\025f.>\366\344?`d\223>\216\323\330?~\220\302\231\265\'\345?D\020\241x\273\003\334?<\377\014\326\3473\335?\320\224\222C\261\031\326?\320\276\347A\210\210\337?\230\360}\"I$\357?\244\000\321{6\350\352?`w\220x\350\224\265?\346\225S\363\255\021\350?\364g\221\'\264\344\312?\377\231R\032\0005\353?\240\254\277Kw{\331?\304\375\0033R\223\320?\330#GbZ\217\346?pSC\221g<\253?\372\000\037\r\326?\346\266\026\320B\233\357?\370ZS\345v\344\274?\260Q\240m\217+\353?|u|\235\355\367\303?\212\275 \211\374s\355?$2\211d\t\035\346?\370\232E1\305z\307?|A\2208!\243\320?\000\274\307~\367\203\300?8{\005\002\270\033\334?\340\264x\257\205\274\304?\2048\275\275s[\335?\031,\242qy%\343?\240\364\257\036\312\204\274?\300\376\r\206\256\024\217?\3063\254cL\306\335?u\347\250\356R\352\347?\343\340\260\332D\345\341?`J\333aR2\235?\274\243\244\276\031,\340?\340\343}\271\311\352\260?\352\230\223\263\'\377\344?\nI\023\246O\310\357?}\211>\366\220w\342?0Y\030\332\301m\335?\324\203\257\327\220\343\315?H\362>E\035c\264?\315\241\r\"\300\036\357?\215\251\343\312\255G\356?\244\201pP\360b\337?}\346L\350J\336\352?49\010P\007\221\311?\325\021\267\364\222\253\353?\200\343\250{Io\227??\3106n\311\320\345?\364d\207\331\3314\327?\354\3045g\303\375\326?\264\353\034\242\306\010\334?\023\213z\230\251m\342?\374f3\264B\033\345?\354X\356\353\234\237\352?\200Ia\022(K\244?G\372)\240\010\367\355?(K\205o\305\200\336?\263\264\231\255\333\207\340?\236w\263A\310R\353?\200W\233\217\003\311\235\265?\230iI\221\310\203\273?\025\245v\252\023}\354?\370\371kow\375\312?\354\2775\316;\345\356?j\324\310D\256\227\344?R~\020\314\014$\323?\334e\267\033\034\345\333?P\264\363%\231\003\270?\322\351@M<\260\320? \266\341\244\206W\330?r$*9\251g\335?\030\342\371s\247\305\356?`\024\005-.P\316?\013AT\031\345-\354?\364JG\355\217\230\343?&\222,Z\317p\347?\300\226:\316\304\271\205?2\037\033\013B\023\354?\022\367\356\377\227O\341??S\311\335Px\356?\026\002\204\242\210\375\323?:2t\347k7\343?@\303X\215QA\357?8\357\202a\255I\357?\0247\374~\322\333\334?P\256\213\031\334o\342?|>\375\nY%\310?\332\333\204\365\034\260\331?\355W\374\024\027\322\342?Y\026h:\250\326\342?\244\262\204\273\361_\321?\"\274\2359&m\321?\310\300;?\0272\325?\020\000uL\365\270\303?\216\252;\n\322w\331?\315\257\372\343\305\302\354?\274\324\322\222\007X\350?lq\2728\3240\351?nd$\025\345\374\320?\271F\212b|Z\347?\324$r&\232\303\344?\216\346~r\354\312\342?\370\263\331\255\265?\200?V\"\025<\271?\\\317\037\205\034\226\343?\024\216\024p\310{\314?d:\202O\333\253\341?\006\2021\235M5\323?\356lt\353h}\325?q\262--\001\354\353?\300Z\341\320R \330?\263x?rc\035\347?\300o\202\214N_\266?\232\236\377\260\233\317\330?\207\276\320\216\275\210\343?m\322\232\225\362\272\347?z\244\377\234b\037\352?\332\203\305_}\237\351?\316\037\277H\270R\332?\300\324\nz\272n\242?~8\013\334wn\352?4\021v,\330\336\346?\312(j>SK\350?A\260F\377\216\325\350?\007J=kdG\345?[\320\206\372\230\270\344?P\013\311m\277J\270?\350A&\n\032{\354?\320\334\365\213\216^\313?]-\230\177/,\352?(\303\362@\211\\\320?\257\261\376\371\331\230\350?`\236\216\230c%\303?d\344k\210\331\017\323?(\372UX\304\005\277?\222)z\345E\231\322?\025\330z\006\016\201\354?^`.\252\224\327\337?8\270\232\025\026\306\270?6W\354\213{\347\347?\246\333\226\306\303\347\333?\256\367\214\323>j\322?\204\352\326\252Y\335\353?d\321\253\205\235e\335?\336\212\251\352V1\334?}E\306\255\223\365\342?\344\260z\t|\357\340?R\326\323\241R\'\340?\250\217\321\331\217\210\272?\2741\024\2042\207\357?\306\305HV\312\006\337?\220\214\326\005\323O\263?\370\225:\374\302\203\355?\273\3659\320\207E\345?\014j\354?>\271[y\206M\322?H\016\3242\230{\313?P\214>q\002\350\321?\217\250\305\226N\247\357?\200\233\313\214)>\211?x\254\252\211!(\345?\225\\\033\346\215x\345?\306\n\"\362~\036\322?\027\302h:\000\216\354?T\031\246\315W\337\304?\340\\\367\351B<\225?\333\027)x\025-\346?\010\036{\2702h\342?\202\235?\017\272\256\340?\207\237\365\241\362\303\357?\006\212\034\315-\376\326?\210\202\327\257\341\254\332?x\225\265\013\024\276\350?)K\344\235ch\351?\352\2642\001\236\333\356?\241\226\013\262\\\036\357?.\033d\020\204\360\323?\272=\237$j\222\332?\314\tcC\214Y\345?\310\315]6\245\325\345?\364#\007\t\236\212\337?\360\311<\230C\374\351?@\377\340kG\013\334?*\201p\321\202+\331?~\r\022\213e\307\351?\352\244\270\324\301\275\347?h|\227\336\253\"\351?\3135\010\364\204\354\342?g\273\010\'Sr\357?\230\322\240\221\020\236\356?\311R\227\263\376\366\354?\311\232\351\013\333\277\344?\240\010H\313D\303\274?<\305T\323\014\312\331?\234hkQP\264\322?\234\366\342\354\277c\322?\304\032@\372#\342\351?m\003/\312\322z\357?\240\026\336)\343\360\310?\230\200d\342\224\321\346?1\346\t\271\322P\343?\246\264\350\230\250\032\327?(\300\253\311\2715\305?\322\346\025\004b\272\323?\306\3602\023\232;\353?\240R\023\351\'y\344?,9\214U\307\244\303?\303\204\351;\265\223\343?\350Iz\026.\247\325?\270\016E\010-%\303?9x\203e\206Z\343?`\312\234\313\037\375\330?\0345k\374\217\330\336?\322\342_R($\342?\257}\026h\306\224\352?>\334N7\356z\321?&\306Q\003:\356\337?XIGkl\255\315?\302\241\210\351!\257\321? \247\242N=}\231?\350\371\264z\335\215\344?*^\321\214ol\355?H\236\320\006\354\376\350?1S\tg\035\361\350?\177:\253r\343\320\354?\200\2444h\347.\347?\3466\214a\351\352\337? @h4Z\257\272?H\3322\213\367U\324?\247\250\330aY\222\347?\304|\275}\025\241\312?\360\035\322\351\027\205\327?e\003\342\215W\251\354?\372\200\026\317\263[\330?\372&:\302\206\221\344?\317D\307u\001\241\343? l\322\317x\366\324?\360>6\215\272\033\242?\306\375\000\200j\274\353?\006)T\200\346\241\332?\352\265}\272\225\301\353? eX\231\373=\257?\014\302\253\331\236\221\355?\220v\343\351\360\341\337?\263v\313M\311Q\356?\244\225U\271\"\335\353?\200\035H\231.n\326?)\270\352\024\300x\346?\353\245\032\342\245}\350? \270\362H\362&\346?k|H\334\362\231\357?\220\261\273c\361 \307?I\362\241N\316\206\353?\240\302\272\300\340\216\246?SU\337\331\010\371\356?1G\315\312!\271\341?\253P2\221\270\333\340?_WG\023\205h\342?\216\317\030\027\301\347\326?\257\020,\242I\242\342?\362\367#;\332)\327?^ |\241\027\376\333?\320H\226\177=h\335?V\022\303\371\020\341\337?\320|>\220\307\324\320?\017*\231=2a\341?\036\312\263\250\255\377\347?\324;\3614\321\272\354?K=]\036\251~\345?$}\020=\237\270\322?\005U\225\326\205\317\351?\200\351=\033\200E\341?\006\302\335\334\361\315\357?[\034\315=,\245\353?\014\']4\324\275\316?\237\225\275|]r\356?L`\265\204\005\314\307?@\216\032\005qK\202?&\270z\0328\213\354?.v\\\331\234<\347?\324\003<\354\023_\315?x[]\313q\307\265?\374\274\242l1\377\324?CS\014\242c\264\347?\275\020\001\335g\222\340?4]\234\277?\210\351?0SU\333z\263\313?\263\'\323)gf\346?dJ5\n\207\334\333?FSN\373\\\005\333?\343(`\216Zu\345?\220V\007\372\376_\273?<\237\223\331\337\005\314?\000\000[\214\3327\334?#\344\361\300\367\033\353?T\304)8~\\\350?\004\272\374\013=E\345?\032n\202\364\036\033\350?(\277\177h\2475\271?\250\257\345\311\210\332\310?\242`\r\035<\004\324?\246\245\212\0307\025\344?\200\332|\027\337\035\261?\223\355j\034\3530\343?0\261\244\376\262\345\310?/\376,\235\350\236\353?\200\352\t/N\255\245?w\244\204\253\311{\356?\030\206\370D\025\301\261?L\031\034\276qN\305?*\034$\362\255\251\343?7,\242\241b\372\352?S\236]\335\336\"\357?\312z\243\202N\217\324?4\271\316XDM\353?\240_p\\zK\250?\345\\\235\240\353W\353?\364m\340\373\002W\351?D\245FC\026\227\350?\377\330tE)\005\340?\037\2046Cn.\344?t@&\372.\362\314?!\221\236|\207\324\341?\013j\003x\035\024\342?\000\3218\355\337S\317?\350PX#\321\252\300?\344J\207[\325\323\302?H\360\216\203\005\257\311?\262\351@\333\3220\345?I\342\207\325\014\247\343?^\246\272\242\t\027\323?\0107\202\010\254\004\313?\0000u\317c\000\222?\330\373\361\222\263\267\260?\004\313\366\212SV\352?^B(R\326\007\353?\006\334!\025\270\215\334?\272\344qt[\'\356?\242bU\300uU\356?\272\355\360\337\344\237\344?\017\307\226\032\247\356\341?@\240\224x1\352\232?]6\305vO\003\341?4\215\255\272\214\007\342?\350x&\274}b\315?(fI\312\255\273\334?\352\014\252:\275\300\352?\371\322Tm\253\366\345?\220\215:\tF\370\356?\360\005\n\234\273\334\252?@q%^k\022\334?k{e\206z\335\343?\320\013M\020}\244\342?0A\0022\200H\334?\235\202\023\024\362\354\344?\346\247B\345l\217\341?\020\240\233\325\000 \323?b#\216S\022\264\331?\335\013\\B\257\267\352?\302\022\344\332\307\212\322?D\340M\033\327\'\341?K;\261\234(V\346?\010e\232\235\244=\263?\232\345\241sY\350\333?\332`Wu\2005\340?\000C\367\256Bz\321?\230`X\206\036\233\310?\014\313taS\001\315?\005\37252\315)\350?\344^}o\027I\333?~h\357U\271\310\341?\210\370\267\370\302i\335?\244}\215D\006\316\317?rq|u\244B\347?\010\264\316\023T[\330?\000@\013\2250\306\030?C\027\375\222#\343\351?\212g\355c\013&\324?\314>h&\275Y\310?>o\334\247\030\255\332?\017pO.\215^\343?\366\030n\023:.\357?`\035\177\330O\354\222?\334F\212\277R\367\327?\337\\\244\233.,\347?\254\3123\274Vw\305?`\370\353\351\373\252\346?^n\031i\274\312\356?Pa\"fXs\317?k\033\370\t\213\335\340?\370(\236\235\007\374\262?z\217\2531\310\261\326?X\002\277\\\013\013\277?\220G\0103M\316\252?\371.\231\327 z\350?}\020\340\"\366\360\355?\314AN\205\330\006\303?\025\001`]\337\225\354?o\221\310\355B\250\347?\320\336\371\247D\222\254?eiL\274Eo\355?h\277>\370\271v\306?\254\022@\311\321\365\340?p\002OEU\210\302?D\327\323\226\255\367\304?\304%t\265\263>\317?\304h5/\026\277\307?D\305\003\313\033\335\306?\344~4x\251e\350?\304\303\202bi+\317?\325\212\254\214x\350\355?*#\242\271\027C\347?8\222_\031h\312\312?\226\335\3629\217\026\345?D\270Nq\016\252\334?U\272RR\315\005\350?\214-\342Y\341\267\357?\\pY\2568\'\312?\036\344l\032\332\322\324?\005X\31052\007\350?\241\207u\225\224\023\344?\036\330,G}M\357?t\007\211\251n\372\303?\310AoJz>\324?\023\213\327\214\014%\342?\343\362\016\035E\260\347?\264yk\322\004\305\315?x\315\002\301\230\027\301?\007\223b\330\302z\344?\240\362G\226\372]\227?\207\000\262\250\033\327\345?\031\036\202\273\241t\353?@Hn\245\244\001\321?%\320\210\222C\267\351?\301\020\330\034\260\337\340?p\017V\212\347j\246?\277\344\371\245\267\362\341?0\360Yp0]\300?df\275\241T~\301?\352\314\364\223l8\337?v\370\302\274\275\217\327?\254\345\223\354,\253\314?\304\033J.\324\210\322?H\361\340\276T\260\350?\036\"\3009Tf\341?b\334\261\311\215}\354?\030\030\361\371\355+\265?\342\310\237\305\260\335\344?\260\013\315PM\005\351?\374~d\311\330\250\335?-\222\035\312\0335\343?\035\000\303;\224\016\352?N,\342\273\245|\332?\350\310\264AK\217\326?]\317k8]\357\355?\206\216\211\216\rE\333?\371\373\227\372\212n\342?\t\203V\220nW\351?\222Gim\305\344\356?p\333N\225\027!\243?l\037\023\356\335\216\344?\253\243\263H\224\226\345?\211\374\271\2311M\347?n\271\376\235,_\356?\340\237\231\007\272\202\265?2\366\"0\232E\332?2\035\225\207\341\342\353?}\322\210\371\273\347\355?\254\305\2770V*\354?\300\342\201\254\360\323\342?\341k31\264}\356?/\023e\313\360$\347?\347\256m\225\316\326\346?t\275=&\003\315\305?\255\206\371\033\200\224\346?!\201+\221_o\340?\255\352\023\356\3206\354?\202^>$\220\353\325?-\366\305\016\372{\352?\350\022\003\031\372\004\335?o}n\014Ag\352??\2757\207S\234\341?\256\002\n\271\t=\320?`=q\263d*\267?\274\330\374\320\3440\306?\370\037i\204&\222\320?\264\314\367\024\347\030\305?`|\367\333\223%\264?\200\351\251\2644\227\237?\326 4\017\223`\351?\0355\344`O-\351?/Sk\'\374g\346?8\014\322\2528\273\345?f\013R{\242-\327?\372w\007\250\036;\333?\355w}\255\337\356\344?\370\237\003\273\206T\272?\200#_\346~\273\356??\031\036\212\344\325\356? \311\024\0245\373\333?\200\2415\233\377\311\267?\340\250\355h2\360\237?\274\333\325C\205\250\331?\034\257;\307\034\336\335?\252\224\320\302_\016\351?\310>t|\353\003\304?\225\270$\200\022\315\347?*\n\260<\310p\353?\374`+\307dy\323?\326\026\001\035e\036\340?x\342\241zD\242\323?_\233\275\301\360#\354?\343;\275((\333\344?Vh\261\360s\314\353?\340z2uO@\263?\223\014\032/\n\247\357?\254\032\270\351sg\344?\224\340\353\227!\226\341?(\265J\2354\207\311?\010p\233!\215<\322?xl.))\231\274?k\370\241\374\033\031\353?\2745\0207\352]EF\325?\000v\264\025X\\\341? \356\024\221\2320\265?\201\332^\235\244\354\351?4\032g\227QB\314?\263\037f\274\347\r\343?\006\265\355.\265>\335?D\342r2:k\336?\200\360\373\033j?\256?\034\352\006\322=p\325?\210m\215\222\024M\303?$\243\243S\001\351\347?\204XZ\227\262g\311?P\202\231pI\325\306?\033\230\347!\346\271\353?H\367\252\335\367O\357?\300]7\000Zb\357?F\327\351\000,p\330?\375\265\215Q\033\333\345?N\233\3709\355\003\332?4\347\032w]\312\341?\037\261U\0050G\354?\\\342\200I\273,\302?\232\006\271\226\010\303\356?\303^\341E\231n\356?\276\004\202\ng\003\326?F\376\027q\354[\333?}\232\276\007\310\222\351?\312!\303\003\253\334\341?I\347\277V\005\377\346?\262\253\273T\331\343\323?\256)T\213\027e\353?\346\315\265\021*\"\357?\340\223\346w\242\346\310?\315*\214\254&\033\356?6\201\255\004\021@\341?\'\214[\366\334\342\356?<\306\006\"\373\215\306?\026\240\266:(.\336?r\177\363\332%\363\357?\014\315\024R\234p\331?\200B\317Uf\270\230?\027v\342\007\2168\341?\266z\261\257m~\346?\024\337\010U\337\210\311?\000\204\023\262\313\375\334?\000\321OH\225\355\322?\\f;\256\330l\334?[\310A\330`t\344?\334;\327d9\306\304?\244\230\3411]\314\306?\234\351U>aw\325?\356dXs\355\364\342?\360j!\'\274C\241?\247e\244\356\026\355\347?;\257\250\300Z\366\355?\270\"\322\226(\233\323?\232\354\337\200P\305\342?\ra\202\221\213\215\344?\006a\3305\"S\357?\200v\234\036\3620\254?\3541tW\257\321\321?\026\271\010\363\374\322\334?\024x\037U)\374\306?w\314\264&\335\026\356?r\311\3438:h\327?$\364\225\237.,\330?K8\244\0306\243\347?\256|z\256\360\226\333?8\217o2\353\\\276?S\214e\204;\177\357?\362\374,\241\235\322\323?\004\200\205\273(\305\340?6\215\023a\033J\336?\027\025\302\253HN\350?\034\314<\242M6\356?``c\367\326\257\317?}uy\377\201@\354?\320M\366\237\203!\313?R\210i\230nL\337?\253\224\014?s\\\345?A\210\356wo=\356?\346\216\371\375!_\355?\236\014\010\274\244#\354?\221\264\363\006^\362\351?\030\350\2121\235\005\320?l\035\365i\216i\317?\324\207\246\000\214\021\347?\316HW\363\361k\343?\035\2454\214\260\014\357?\313\255\350\306W\203\355?\234\261\340{u\n\325?a)7\240-h\350?\330\252\324\253\232\322\310?\243X\"r\224\302\340?&\377\013\317V\204\344?\273\373e\3720\026\352?Q\252\003\2677C\342?ok\234\315\277_\340?\340\326\361\303\030\243\353?\304\"\024\'\241\001\350?\3467N^\252\337\332?\300-\006%/P\330?YbM\321\000 \345?`\201\271\274:j\327?\354\316\207\327*!\326?\230\263\237\004\002\373\314?=\207@m\374\315\347?x\033\226\377\034\312\346?\314\370\372Vu\353\357?\010L\354G\034\205\333?\222\322\'\253\343\204\350?\236mU\307\235k\333?&G\330\304H\334\342?\306\027;\234\215\263\342?6\002\271gCe\325?5\032\333h&\345\355?@nD4~L\314?\361\250\215\375\t%\347?0\377\006\332\322\276\272?@L\361\227\322\317\304?nH\004\010\227\021\350?\374\016\257\2160V\356?B\333|\026\274-\351?\\\231\302T\016\000\327?b\304\356\225\374\025\324?\037D\236\326uV\354?P\033-\272\353\024\270?(\005A>B\234\311?\3701\316h\207\272\264?f\356\344\275B\350\321?\200x\235C\255\373\336?\267\336K~\037#\342?\200\031h*C\262\202?\310Aeb\266C\266?\232\030\0348>K\326?x\022t_\306>\302?#?\214\374\034`\342?\362\376\325\312}\300\336?\006\217S\216g[\353?SR\276\257\235\311\342?~~\312\330\330P\337?p\235\331q\n\010\327?\0304\250I\314\030\330?^\t\272\233jo\347?\312\"4k\366\230\341?h\301,\276S\313\305?Ps\330\336\305\313\347?a\032o3N>\342?\033\331nt\354\002\356?\274W\205\351\264l\312?I\375\020Y%#\341?\231#\230\314\t!\345?\362\364w\000nL\333?(/\270l\352\216\312?\0000Y\372\010F6?\212U\240lWK\344?\306D\222\357\3073\344?DG\337\'\375\332\314?\343\307\021\014\275\374\350?\330Z\254\\wX\334?`CO \263o\254?\224uU\373\273\177\340?\224>\266\2160 \307?z\2417\307\272\017\353?\247w\235\217\014\223\340?\200U\216)Y\271\340?\354\\\310_p\216\324?\"\026w\0169w\353?\023\n\217\253\240k\353?\350\322\233-\r\321\307?`\336Y$\026\261\337?\254\222\216\312O\332\347?\336\206\356\tm`\337?\212\305{s\201W\337?5H\361\243\374\370\351?H\337\252p\256S\316?>k.\"\225\334\354?\020?\226\266g@\311?\0005Uz\326\301\247?\032\225\266\370\0247\342?\000\270\230\252\237\326h?pZ\374\267O\007\277?@hT\314v\207\342?,J\231\216\372>\333?\320Fr\336\t\245\303?\206\314\024gKf\355?\000\324\002Ryi\274?\374\362\262}\243:\302?H;\rz(\212\326?\352\225(\306nP\351?\256\025\350mk\376\352?\330\246\371\221\024\322\315?\020\214\005c\020\304\345?\022\022\033EH0\350?\200\356yk\237`\272?\264c\007\025\n\252\311?P\023\325\334\330\301\347?d\307u\016\243\263\315?\244\364\t\014K\244\305?t\306\377\331\265\313\350?dM\343\277\030\020\317?o\261\251p>\324\341?\260kld\025\300\317?W\000*R-\375\354?\332\340\336\037!\320\331?xt\310\363\240\265\311?\250~\230\260\330\204\305?8\325\376\236\303\020\350?\212\347;7\216N\334?\221\000\202\031\317\302\355?h}\004\276\246\233\330?\300\014\331\0079\256\264?\277@6Yk\236\350?\331VP(\310\244\341?\276M\'\017\361s\325?WK\212A\246\\\346?\277B\204\240\"\272\357?\207\2201\341\253?\020\272\243\345\344\373\353?\364\211c\352\036\022\331?\332\333\303\332o/\320?\226\005\003\022\317\351\355?T\241q\213\217\235\342?\200E\016\016\007G\202?\356\371\247@\347%\337?\032v$\264\344\262\320?\003*\265\235\232D\342?\332\306)\003*\023\340?$\2300\214_A\330?\344\024{({\252\316?W\351\337\307W\367\340?\3226,W\\\204\352?\373\342b\371\205\271\344?\200\267\276\034I\003\224?\354\345Uy\242\254\311?\032\335+\377k%\340?\003|\224\231\243\333\356?\001\ro\352\350\366\341?\220\225&\316u\232\345?\005\005Bn\331`\342?B\274P\367F\200\340?\307!c\222\225\262\354?\003\260\235`\212\260\341?P\271\016\265\343\014\274?\343\006;^\344\215\345?\244\343d6\257J\320?\320F\311\362i\022\260?\341*\335\340\3260\355?\277\271RS\215-\353?C=\013\375sm\347?\254)\226\032\360\355\353?p\201\211+ \330\324?(\305\233\302{+\351?\2007\374\256\364\255\267?\350\252>\005\360\306\267?\312\363\310\203\311m\326?\237\274\200\300p\327\340?\244\235\035\252\260r\350?\250\314\023\356\265\225\327?x1M\244H:\346?\347\000\333\353\336 \353?\332I \333w\312\336? \330\351\250\334\350\322?F\333@\264\361\037\352?\270\202Dv\266\320\312?\272M\343N\233\322\340?\230n\357K\375;\273?\210J\357\\\007\214\307?\205.y\331\233\255\343?k;\217\252\351\210\346?[\317\255Y=\\\351?l\316\311U\276z\332?\326\205\365\376\244\355\352?\321\262\204\']\r\343?d(&\026$\000\320?\340\022\330\272`\327\235?\312\320\205y\317\217\326?98\322Y2\344\341?:\220\274\013\274\304\341?\246\307\036\177N\311\332?`\264\266\305\013\252\331?\242 Q\034K\217\351?us\342\273[\204\345?7H\217\244\252r\355?<\004\214 &C\336?\320YX\2629\222\263?\340(\323N\326\232\220?\010\017~\230\004[\322?\010\237\036\177\352\203\302?\364\313@\322\025-\316?\364\326$\213\353\220\316?\307\215\366\006Po\357?\002\233\360BM\263\327?\201\355o\310\364\252\357?r\200\314\367\231\271\357? L\250sf\177\322?\200\024\322\304e\336\227?\204!x\372\010\240\307? \253\214\217\352\202\274?S\274\207R\361\366\355?\000w\035w3\332\325?X{M\300\372\247\305?q\263\376\207\364\023\351?x\222\010\232\025\260\335?\342\264\325P3.\320?7\243\216\334\230\037\353?\306\377\314&\255\310\322?T\262\253\242\234^\352?\232\334Z$\305a\351?u\2535\276\325\263\342?X\210{]\340\312\350?\322\320\2016|\366\326?P\221\354\236\2718\311?\373\264y2\n\237\343?V\026\033\360\273\263\325?\360\320\212\002\227\217\271?\034\372B\032|_\337?\002\257{\002\004\341\353?\204\\FlK\356\300?T\013\377\264.\337\315?\371\244\0319\333\347\345?p]\220\230\315\177\356?\240\221\356?\001\341\267?\252\3754~\374\220\335?V4w\341\267\304\327?\036\276\211\002\323\311\357?e\261\344m\266k\347?\324\255\2326Q\006\334?v\307\347\262w\250\325?\260\020\023\315j\237\303?`H\270T\315\315\240?C\355\007\006\300\325\351?^\371r>\324\004\333?8\2002\r\177\037\264?\024mX\213\025\t\337?\237kl\t\371\277\344?\374\017\021ug-\303?\370%\277X\330&\343?\215\231\325\020\302\214\340?\300\232\264S\225\177\306?\230\017C\3064\030\353?\334\007\335\031 \000\354?\366\267\237-d\306\326?\250d#Z\305\301\334?\200\252\2278L\033\251?Fq\306 \232\234\347?\270\227\242\275\206\310\276??\260\204OT-\350?\322\352\222\342\303\275\337?\357\321\307A\241g\347? ~\327\333\2234\265?P\300\177\352\262\276\330?\303n\032\211\252\317\346?\230\230\310w.\265\310?\240\0234\033\021\344\337?\327\034rl\241\345\352?c9\006k\020.\343?\347j\373\302\214 \357?\262\250\217*\353 \345?\2251\256\306\347\362\342?e\271\257\027\002\254\356?$vcCoy\352?\267\312\324\306\325l\347?X\354\364\303\\\316\326?\354\242\266\177\241\233\320?h\332\242\230\243\265\260?\014]\251\344B\340\337?\364\343\355\3036;\307?D\230z\206\204?\320?\200\347\366G\203\240p?\232%,9\203f\321?\000\232b\352\323\332\270?\310\375\217y\335@\266?0\310\231f\301\340\245?\010\332s\007\357p\350?\344\346$\373\3539\320?\373R\335\360k\226\355?Ku\274\361\rY\341?\3440+\017\212\230\352?>C\034\254\347\356\342?TV\332\241\007\241\307?\240_fg\026%\356?\276\253\254I\014\236\323?ms\007N\267\314\352?,\227T\323\320\202\333?dn\266\014\214[\336?`\313\327\3166\224\261?0\302\005tQ\251\300?\036C\350\310{T\356?\360\244}\036\260\207\246?\202\215Qa^\226\326?\0003\327GV%\261?@\025\332O\315\364\327?\2044\311^\r\353\306?Us\246yfX\351?\304\227\255\205\327\232\315?\274\353\237@\246\323\351?\016\t\261\2245\207\330?\224\226\366\341\370\357\300?\210\3039\361\004D\315?\374\021\216!\205J\302?\300G}\227No\242?\346\017\276\260{\370\351?\206\203\241\310J\321\341?h\356P\241\177\330\260?\220(\316\r<\336\330?]L!\231%^\354?\351\326\3709\337\313\357?0\324\2162\351\347\242?\013*v\025\034\363\346?\016\023\345\022N\000\352??`\377\216\363\342\352?)\003\356\276\241@\343?\010\222KF\374@\321?\230x*Ju\202\304?\266\221;.\243n\354?\2334Oy;j\340?\371\326j`\325\230\344?z\002.\305M\310\341?\240\035.]\036X\236?\305k\262\2404\004\353?\210`\227\237?Z\302?)\313\345(o\020\340?\303\'n;\032T\354?\362\003\317\016\345-\350?\0148GPA\352\357?\225\237Y\023\000\371\353?ry{\322\251#\321?\204\003\270\360\304\035\354?X\347\330.\274\205\303?X{\274\360\315\312\355?\267\273/\240\346\355\342?\204\201\357+>e\333?\274\014\336\367\353\342\313?2:r\224.\255\325?b\005\341\002Kn\321?\026I\225g\303\002\326?\347\342\330\336$\226\345?\246Sd\343#\260\353?@h6i\225\026\317?\356\306\344V\320\201\335?\220M\014\270\333\361\246?D\260\020}\2121\343?\254\252\027o\321\030\317?\360\312%*\272H\330?,\177\341\022\376\213\311?32\313\356(\222\347?Z\364\365_a_\320?\270\025\300\323\216\344\315?2\315\033\363\354\376\345?\207\345\020\006\342(\352?\265]\211\344\267\262\347?@\353\030\227@\'\235?\022\246l\353\370^\350?\340\362Q\312y\360\232?\r\340GA\306,\345?\222\317\316\007ru\320?(\2525\260(j\260?\310\343\357\037\003\006\306?4\020\013\366\277\327\312?z\230]uVU\351?%\001\217{L\344\357?h\251a\245\247b\346?&\225\310\014f\035\332?fE!\321\344Z\343?\\`\006si)\351?J\225\213\314g^\346?\021\221\306)\366I\351?nM!{/J\341?\2205\374ur\245\241?\"\317S\301`\267\320?\007\033z\263\353\013\353?~\014\216\316\3609\342?vLT\212\226\303\340?\212m\344\303?\350\324?\210\272\010\311\205u\314?]\303h\314\216l\357?\3477r\277\032\251\347?\207\333\305\312\304\022\357?\204\016c\202\200\356\335?tn\351\361\324\017\341?\351\362\'\335W%\347?H]\377\273\235=\273?\300\033|\021\252\360\215?\360\337u\273~B\356?\220\220w\257\217p\247?\2144\016\254\201\034\323?\213\311\202\357\232P\354?\240\201_\371\223P\237?\034\353\356\322\330\271\342?\200T\364L\212\322\312?\367\n\t\033>\360\356?i\364$\307\276w\351?#\035C\342\260\277\352?4\241[u\253d\331?\370VzL\3018\265?\220W\021\206g\034\276?\345\004\245\033\305\014\356?b4\376\320Zp\352?$\227\237\352\327\332\317?\227\226\216|\351\275\356?\357Y#\374pQ\351?U5\310\024\357+\342?\350E\252\265\233\365\276?\300\316\365}m\275\321?t\020p.\355\323\342?pb\"n5\277\327?\340\202F\026p\r\347?j6\314t\300\370\331?f\225\002\235N\375\321?x\354s\0010x\313?\230\203\310\220>\330\317?\377UU(\3613\350?>\362\223\037\343\222\352?bE\235\271\366c\322?^\253@L-v\351?\000!7\240K\312e?\372_k&rA\342?\\\031-\213\266\373\335?\370\341z\257\251\251\337?\270\031\023?\3440\327?\236\0331\262\207@\357?\210\322\237\300o\357\277?\235\243\343C\266\002\356?\222\217\020]\224@\356?\000\374\346*\t\355\342? ]\004\251W\311\255?\316N:D4\333\324?\002Iw\037\0106\355?\226\243\025=\257\303\321?\013\233\017wd\241\343?c\027\312\346\022\321\357?W@l\014\363\312\353?:~5\220\270\363\336?x6o\264H-\346?x\221\314\357\'\276\277?\204};\316\035M\307?\374b\300<\255\337\323?\332%nl\203\205\353?\231~\370\027{Y\354?\214F\211\264\000u\316?l\023\252\005\367\314\300?\347-\247\022\337\304\343?\251>\t\212\026\215\340?\270\014\321a!\355\306?\235\271\310:\376\233\341?\350\363\273MS\223\312?\366M\010Y)\246\332?;Q\331\353\367\347\351?=Q!%\377\234\346?\007\300>\341Q\254\345?\220\000\266\310E\325\313?U\2230\001;\354\346?\274jm\256&\264\353?Bk\217q/\030\337?\027R\225Z\"q\352?\276\357\030O\326\244\347?u\"/\262;\300\346?A\213\254}\177X\340?\240\373\031w\233\322\300?5\344\231\216\220&\352?\r\032\266f\376\223\345?\020\317\007E_\224\335?\360\307\230\247\230\232\337? An\330e\353\263?l\322:\360a\337\317?Z\212T\205!\333\353?-\242\341|^N\353?(\213>\017\225#\311?\000\225i-\255\277\312?\264\353P\377\031>\334?\377\327&\226\216\217\352?\257\260N\260\035a\344?P\223\001T\027\366\342?\252\311(\370\261\216\334?\356\314\355\3761y\346?\315\331\323(k\217\344?\350\022\032\266\304\245\324?\255(\323\024\265\244\351?\006s\346\235\377\210\345?\316\244Z\307\221\252\351?\372\243\344\364\306*\332?(+|\225B*\315?3\314\350CPW\352?\020)\222F\246q\266?\350$\3450e\250\264?\375\320\342%\204\350\352?\360~n\326\000X\311?`jA(d[\346?\200|\025\211s3\224?\177\\\226\363(\344\346?\374\210G\373\362\212\355?|\345\235\362e`\347?\324\257\277EK7\305?@\236\341\252\261N\247?\224\366\375\0034s\333?\221\241\2529\000\277\352?\274\022\230\356\335\271\325?\270\232\350\207x\324\327?l\325\373\247\256\316\332?_l&\227\3625\343?\262 \365S\0015\334?\256\366\267\334\233\366\352?V\356Z\273\367\341\343?\340?\340\253\333\363\033W\265?\003\033\253\224\201\324\346?\360mR\216\214\257\337?F\177S\r\212\277\340?\027i\343k\007\347\356?\341F\200\252\201W\351?:\035\027\353\3132\351?\211\217\314\r\362+\347?B\r\212\202\356@\331?\232\216db\241\031\334?\313\236\307\230\337\353\345?\234\t\370j\0142\331? \305@\306S\346\247?\256\201\"J\337\007\355?8[80\222\270\301?&\344o\213-+\353?\375\\;\037{\324\353?c\303\2312\250p\355?\254\243a\213\223\227\334?9\201\266J\256G\345?\3624[\371\307\256\340?\222\322\326\201\273[\355?\3506\222\337\003Q\352?\021pd\310\266\025\343?\006\014]\3053/\341?\037\267\255\301p8\342?\364\265@\264U\035\312?\335i\361\'\031\377\354?\r\010|\363\'\350\346?\210p\351?\010Q\343?\000\267\302\324\300\037\246?\372\265m\257N\277\342?\370!}\234U\236\324?\344?\215X\352,\356?\247c\204\312x\206\351?@\211\\_\001\344\205?\263\010\331\220Gq\341?\'\334\234\230\211\347\356?h/7\006w\250\336?)\000\2507i\310\347?\216\013P\324k\326\351?-j\340\275\232T\345?\000\231:\307\373\262\342?\250z#B\272f\303?`A\034\265\256\024\343?\000\374k4O\327\322?`\2561\251\236\337\336?@\342=\310\325\241\333?\302\010\024W[\250\325?k\237T\026\237@\355?\276UN\300(\n\324?\235\203Y\374^\326\354?\036%B\257N\211\322?@\263*@\377e\231?H;\211\336&\232\347?\340\014\372\301X\327\307?X\207S-\371S\344?^\300\305\034\337\005\330?\033\266\3727E1\340? \352?\356\"\253\256?\302*\210J?\310\331?p3a\325\234\306\265?!\271\314\216\206\236\350?8H\250\377[2\326?\376\332\253\366\036\261\344?\"\324\005\010\245\323\356?\237rq\323\232,\347?\002\374\237x\337/\326?\013d\343#b2\344?41sz74\304?\314\'\255j\310\243\317?\250\310\303W\335\272\352?\340\262\0309hO\256?\365({\361\372-\352?f\331a\2079~\330?\243\207Tu\216)\350?\216\202#\323\337\202\323?\210m\032p\007\323\267?\344&\352\224\201\301\334?5\016\212\266D\203\343?b\276\3223\350j\343?}\037\273;D\215\350?xn[\251\366w\266?\344\377J\313\272\211\343?h\251b\013O\236\321?\320\033\220\031\230\373\242?\320\263N\363=\351\271?\246\250\000\177\267\211\344?\313Io\300\007\337\351?\303z\272\252jQ\357? \342?\334\266\202\313?\t.\367\3633\205\343?\360A\374\353\213\\\344?\004#\331t\225\236\356?l\316\210\246GN\302?*\001\210\214p\024\332?N9a\254\2726\336?\0263\336\272\344$\332?\020\220\'\237%}\320?\3708D\327/\031\332?\273\300u6\0056\355?\224\277\361\203\347\227\327?\377\031\214\t\320d\353?t\247\360\346\215\335\307?\224-\020u2\255\350?D\020\005?\010)\321?\370\335\335\261\334\214\303?\'\037\362\330\260\010\353?6\302\365\203z@\335?\320\276\276\037\002,\346?v\034=\3137A\333?\314F\325\375\225\200\323?Hk@\230\300\242\343?\325a\202\323\221/\354?\267\\q\377\372\371\342?_[\202\201H`\343?\361%\274\356\225\240\342?\341\235b\277\372\354\357?\"q\311\310+\252\320?mI\212!\344?Q\212\350\231J`\356?\300\'r\343@\n\325?4<\276\223\020\222\324?\214k\3501\007\370\341?\264\270\0305\027\335\351?\372X\245N^\\\351?l\350\354Tz\t\337?&\215\270\270s\327\325?@\352c\334\241\350\317?<\261\303\263\"O\311?\325\300\204\225\3533\344?\013`\301Z\"\324\351?\2334|\363\031\010\352?\245\260\363\027\301\276\351?\241\216\247\243\303\004\340?\240\323\t\3664a\307?\214\206*\210\353%\300?@\360Z\034.\354\230?PCs\326\r\245\253?%8\327\003?\002\354?\361\003\336k\313\n\346?(,}\037Y}\337?\357\263\226\2200\344\355?\240\277o \242\017\254?\230\366U/\004T\341?R\035\022\342$\352\347?\336inm\265\007\345?\260V\312\2774\377\340?\215J\035\'n2\356?\257x[be\205\344?n\220\373\235)x\346?oU\301\264CD\350?\250\231?\t\315\267\335?\224>\tQ6\361\323?\232#$\222YE\347?P\352\222t\305\366\323?XQ\003\022f\220\262?\354=\002\002\\*\345?\230\017!:\202\342\314?F\313D\346\242\261\334?\331\243\250\014X\226\345?`M^y\360\320\247?\322\035.4\235\266\331?\030\277\351\373\'\242\270?\210W\215e\350\314\312?)_\353\336\243h\342?\210Z&8\371)\273?\310P\030\314\253[\302?\334\375Z\210E\252\300?\360\234\374\324\344?\250?\316\253o\002|N\321?\210-\225\261\331\335\340?\216V\"N\014G\343?\204\226\316\277\304\231\313?X\250l`W\333\300?=\355N#v\242\347?\360\306\334}}|\260?@D\334UM\251\313?\024\3219\261\353\207\345?\216\363\324\274\210{\353?L@&\231?#\332?\3056/$\010q\351?\244\237t\333\r>\351?\300\250&\325\376]\277?>z=i\2649\350?\013\265\257\377\205\225\345?\217V\232\204W-\350?\376/\247rrh\346?\240\347vC\227\013\300?\tRq\022\007y\344?Zh\274\252\267X\333?H\223\253\244\014\256\314?\000p\033\262l\325\354?\245\2205\253v\367\352?\264\352\314M\330\300\305?\257r\346\026\337\032\355?\306\216\203>\2712\350?D\242\032A\017k\303?@\245\355\234\220W\321?\2566\016\003\343m\330?\346\206o\321\027\343\356?\350~\025\367\254\215\336?\331?Y\375\375\372\355?\320L7\037\003\355\323?\002=\024G\316\025\354? \211\030\'\242\350\253?\237\365\230\246\300\205\341?\322\037\261|\272z\323?P\252\0041\357m\255?8\344\330\022\354\255\342?\360\377bf\0050\334?\344V\230jB\356\320?\321C\310*/\306\354?\202\340\236\312:P\325?\014\027\237?\336\302as@\205\357?CgKV@w\346?\222\356\331Y\301\006\346?\031\265B\345\206\340\340?\000&$\243\336s{?@\361\2010\270\304\300?\021\366e\031I\275\342?\255\250\264k_\177\344?\001\251Us,\307\352?P\rB77K\326?\240\301\204on\242\240?`e\272l\222\202\314?\217\351:\332\t\327\355?N/f\237\311\312\330?\0342\322\366o\353\327?\0228\362\247\3425\346?<\321\362\3661\233\311?D\205\375\\N\026\344?X\366|\352[i\314?\200\023\t\373\244\204\204?[\275\330\366\317N\344?\017\302\036C7J\355?\200\263\245j\211\376\255?V\010\326\342\320*\356?}\250Y\311%,\343?\273\366\303\n\001\302\355?P\023titn\303?\240T\245\364E\206\242?|\244\005qy\212\322?\032\007l,g\301\352?k\271~)\232k\350?\020\300v.\262?\266?\000s$F\371\246\340?\243\315\t)D\377\357?7\272\352\231@\272\344?\010\370\315\013c\367\311?T\253\"\250\033J\323?\340\307e>Y\321\231?\313\241\231\352\331\235\352?p#\361yR\236\324?4\325\032\r\036P\353?@\002\0163\246\352\326?\300s\264\177\264\343\322?\003y\362bu\321\347?H\014\212\352!\347\272?\034\352\246\004\206\343\331?\241\200\233\351\023\356\345?\323\203\304\020\255\330\346?\025\251\367\014\031\377\351?H\227G\027\242\240\274?H\315\244\2504\352\301?W\227\035\006\371\257\353?>\270n\246\":\324?x\335\367%\234-\271?\340\325\364\346\007\025\254?Z\236\226\002\270\250\356?\230+\212\243S\271\270?\256\275\325!vT\351?\"K\303M\004\251\324?\332=\'\000\031\217\327?\321\t\264\344\216+\354?E(\316\002gx\356?A\264\351\346\200\247\351?0\025\347\322\020\246\315?\216_$$y\315\345?\302\252\n\321N\t\351?\307\227X\234\024e\347?s{9\002\300\243\346?\254\267f\237(\032\306?0{\022\272\374\217\255?\370\232\212\324\217\t\264?\250 \275\253\251\224\347?n\"~l\033\343\354?\224nmi0c\335?<\244M\321\304H\327?\274\371\037\371\375\343\352?\230\035\2167\255\341\337?O\200NZ\2478\353?\220\346\325\202\341,\265?\360A\027-\243\344\270?\236\275\333N\311\343\324?\260\363}8~&\273?0C\243\366\3017\345?\216Y\351\026_\276\334?\302\0316\251\260\303\320?\002\277\263\330JW\340?\207=\221V\376\016\347?E\217\"\242\0313\347?\3274\320\357jL\353?\270\250\013z7E\351?\330\212\000\374\302G\260?\370*\363\271vm\335?\013\323\332*\2300\346?$\304\213!jg\335?#\017\005#\313\346\352?\004\254\343\006\357\023\313?\250\254\016\362\362\003\356?X\217\240H{\263\273?0\033\345\322\365\201\302?\302\021\367\276\250\343\336?\300\326\025\005\311D\343?U\306v\340\343\230\350?\244\365]\002\202B\316?(C\366p\363H\274?N\003a\300Z\211\320?P\313(\341^\262\243?\214\330\314\244&$\300?\302\333\207\372}\200\357?\353\251X\227\251\213\343?3\225#\004\034\245\340?\230\226\331=\023\331\303?\'=\263sT\331\341?\275^\325\243!]\357?\354\317\211\306\345\225\312?\347L\250\021\221\021\344?MStw*#\346?D\223\234\006\025T\313?\025\266\2112V\311\346?\250\205@\326\313w\261?v\232\tY\340\260\320??\374\245\306>\271\341?\315\216\306\233\304\215\351?Fs\357i\257\366\347?\000xX,3\006F?\375\253\203]?^\350?\200Ck\346^)\257?S\250;\306\367\307\353?)\324\031\031\021@\352?-*\314&\271\007\356?AOW\025\014\227\356?P\0235\022Ed\271?\014\226\363\374A\273\325?\2016CCy\014\357?B\324^V\276\025\354?@w\230\222\325\226\210?\374\030\001\0024\027\354?\324\2579\206\321\263\337?n\373i\017P\275\322?\350J\220\010s<\277?\017\377\357\313t\262\351?\220\372\233\027\223s\355?3\253\341et\035\340?+\307\344\256\364\371\341?\034\276\3103`\262\351?\301\222\016!Zn\354?\304\rM\331\257\257\340?\364\316\232\277\312q\301?\r\336\265\320Lk\344?\254\346\334o\374-\300?d\265\333\344\307\243\351?x\016\306}\030\273\354?\244\0269RA\'\302?K\371bl\317\203\355?0\013\023[\305\306\346?\252\332\232\250M=\340?\374}\307\247\242\224\343?\250\230\254\217\237\026\320?\002\274\200u\256\375\331?dF\\\331\246\203\344?@>\224)\274\020\202?\330YZ\025\201t\341?\306\315\227\335\207f\340?8\233\"\250\037{\353?\350@?\364\330\344\326?\240\261\307\365.Q\226?j\237Y\t!}\356?^\350\261`\211z\351?\226x(\024\317\374\337?\222\311\372\273I\016\347?\226)\303\013\n9\346?\334\260Z}\305\037\307?\324\206}\277\232B\341?\243_%onF\352?\2106Q\036z\251\332?\351lk;\033\232\341?\310FA\023\201L\313?f_\347\311\007\204\323?\245\3456\205\303\340\347?n\0228\317\213\006\331?D\343\361\253\206\343\356?\334\010\277\026^\r\316?N\277\364\353\003\302\327?\320\241\262\223\374;\353?\233\315\341\354\321\007\341?\016\301\237\235h\320\350?\"\2660\033Q\321\334?\356s\207\013[\023\327?\327\221\376\370\351\376\347?`\373\342wZ\333\226?P\003\267\264O\310\320?\266e;r&\005\346?\230\261\034\325\231s\331?\212\313H\r\223R\335?\231\261h\352\375\017\345?n\355\2115\340<\347?\354\032&%\031\224\320?\323h\266|\005\227\346?H}\027\002D\240\342?\240\004+\227\315\315\315?@c!\336\0355\356?\031C\315w}+\352?*\201\3707\'\237\346?\350\243\202\216x\217\260?\246\027\n\325\275\201\334?1K\264a\273(\344?\320\370)\330\242\361\305?0\'a\330sG\322?H\255\370\345V\251\325?\266\215\031\346\222m\327?\206\t/\255iV\334?\241GJS\371\276\355?\221\325\320\036\334\025\350?@`\006\300z \244?\214\343Bl\007\350\321?\\m\032\2475&\356??:\253\244.b\340?w\274\305\275\217\212\357?u\373\326D\021\227\347?\350\272\365\010\306Z\306?\010\361\001\276\200\356\345?F\206%\321S\013\325?\230\363l\203d\t\357?\237+\261\306\266o\346?\306\255\242\377mJ\332?\256A94\275r\357?tN\371\234B\262\354?<\221\207\315\243\332\354?\324\323\2317\2325\357?\214\3044\021+ \323?\310\177\217\2010\244\322?\324x\232\256.\237\351?WF\351\007m\340\341?J,\302\264\301D\343?`\033\227\022\264\347\350?>B\035\214\261d\347?\340\203C?\252t\263?\304}#\222\223\240\346?n\030\302\371\364\326\327?#+F\235K\376\340?\230\340)\001w\020\306?3^w\001\3472\345?\200\013F\235T\243\345?\230\315\327~|\225\331?\323w\320\276/h\345?8\215\313\035\304i\275?\220\325\244\027@\243\260?\215\021\325\177\200\322\344?B<\212\214\004\246\331?\350\026\\\306\017}\333?\024\274^\033\252,\335?,\203<\334\364Q\312?\210\2431\007\005\205\307?R\2678\020\270\201\337?\260\354m9\274\303\324?\327\302\304\264\007\036\342?Zh\371+\0313\352?U\210\205\372hE\352?\024A\316\367\211\242\346?HG6VU\010\303?];\022\344_\005\355?$\336\301\364L\214\334?\316\226\035n~\035\356?I\235+\367H{\355?h\266\260\212\277E\326?\000\026\222\206\313\362\217?\300\033\330\177\037\216\272?\360`\352Q;\214\326?\346\304\353zo\344\357?k\2014\025\020h\351?\320\346ULO\037\331?G\021Z\363w\200\344?\214v\207lg\221\323?0\327\346z\322k\267?l\243\205.\220\000\337?\264\021%\312\377\301\321?\3240\242\030\315\342\307?\374\251\311\341]+\321?EC5\224\342\255\345?\317MQ\032`7\344?\376\3742\274\002?\355?\024\034x\314\351\252\312?\231vQ\306\033\326\357?h\365\334\342.\357\275?\366\321i;\231\250\345?`\335\036\030l\003\242?\000\002eI\352MP?\222Pc\223\242\204\341?0\370`\320p!\272?\004\366\333D4\"\304?\000Z\247\n\004\200\306?\270A\363N\223Y\267?\256\312\330\325\223Q\325?\320\371\262?)\030\272?r\343\372\236s\002\341?rf%\261\\7\347?\340\\\245\004\034\211\352?D\334c\3744s\343?\311\216\223\202\315 \346?\252DA\226\220\231\335?i\2323S\204\373\343?0\265\251\230tc\313?\230\323\260I`D\323? \241\332\342?@dh\217\025n\337?\254\370\370\034\270\211\301?\234\260\253\343\366\373\337?\245\013>\352}w\357?\356]\204\313~\214\334?j\306o2D]\343?\341\205 *G3\347?f\3269:\243<\320? S\256A\236\267\223?\300\240\207\320\002\330\247?\231)a\n\351\240\341?\370\320\344?\271\031\305?v\261\034?\254b\326?th\332}b\303\350?\311\027iD\367\366\356?\377\n-\212\240\244\351?\004\271+\355q\325\303?bKh\227\246m\351?<\033\234\245\236\354\342?|X$\275\241\271\313?\370\375\376F\330q\267?\313\245!\262\357k\352?\253\364\325\036\216\272\353?\213e\307h\373\n\347?\035\261Q\026\376!\346?\004\361\210U#\300\344?\246\032\216\244\275;\344?\2221\031\340G\337\323?\210z\014(\362\013\325?\001\225\257Ek+\341?+\341\275\374~7\357?\202\273=E\233\032\353?\354\213?IgA\325?>w\337\232\347\367\322?\326-\3238<\335\323?l^>\357\226\236\305?\230\003\363\000\354\264\301?J\301\330w\227-\325?\216>R\352\336P\325?\210\245\330\325K\'\264?\350\3141\345O\373\347?`\2647|\375\311\220?\230\267S\241\267H\272?Y\"\"\020\016\363\346?\305?\337X5\353\207\343\356?\200\277\320<\177;\222?\254\260/d\033\323\336?\330\237\243\242\371J\321?\366;\270\270\t\217\331?\253\326\2361Y\245\353?\340VbJO\204\301?\305S\234#\366\"\350?\376\032\277\007p\262\331?\324\224Q\332\340\322\307?\032\027\356.\215\013\331?\253\331\361e\253.\344?\324\017h\367\321\227\307?\252c<\303\370n\357?\360\263X%\312-\252?\014\267\\\241\235H\357?Y\277\n\r\0140\344?\260u%\304\010T\314?\377o\303\017\331\372\345?\\\317\031\242nP\353?m\220\200\367\346X\350?\271\366\207\372ht\345?8\261\227\335\312\241\337?o\223}\351\035r\340?6w\201\2572\351\323?\344u\364\306\2449\317?N\033);\311\204\357?s\024J\311\271\330\354? \315\336\307Z\000\271?t\207!\202\036^\347?2\221\027\261\240\002\352?W\024yOT\230\354?\270\030\334\264$\366\350?#!\267\363\037s\346?\2641\311%n\001\323?}\374=b\r\\\356?68,A\223\302\323?\312\301\025\224\027?\337?i\311%\251\243&\342?\235\301\202\364\032\235\351?\240\242\343\246\250!\220?\330\246\371\037\235$\276?H.\235\n\016\264\340?KC\220\210\301\217\341?\3241+\340\366?\324?48\254\341\227\036\323?RA\025\222\347\217\324?\353\304\371\345\236Z\344?Z\206J*\367\372\350?\234\250Q\n\271\231\332?4_\226\013\356\310\355?\302^\362\317f\312\331?\200\2031\206z\t\265?9\302\332Z\346w\356?\322\031\030\346\244 \341?\340*\200z\207\357\335?F\\\3578\201\315\342?H[\224\243\270\350\276?\210)b#{\304\335?,Lq!\256\251\310?\376\242\312\033\240\020\347?\236\317\245gA\205\336?\0045\272\206\373\216\332?\030\311S\345\340W\320?p\311\034\340D\024\301?\226\251\036\25555\330?\030\226\271\351+\006\275?\003\252\205\215+\322\340?\204\242\362g\214+\345?\377\203\272\016\217Y\344?\346\213=]\317\304\342?\\\313@\026A\365\315?\324n\266\272\322`\335?5\334\244d=6\351?\215J6\205QQ\343?(\267\231\017\255\'\313?P\227\262J\236\316\352?\220\322\334_0\373\264?6\242\341\346\030\375\350?\277\017`\251\360\037\357?\\\350\255\001\274.\353?nY\220\002\3145\333?d\'\255S\0279\327?\335c\267\326!2\340?\274\036D+\202\207\300?\246\210m\227 \247\345?\240/\304\305\224^\240?\254\271\031k\004\034\306?I^\204\307\327\224\356?-\241\222QM\214\340?\274R\220\272\324.\352?\000\262l\t\254%\317?\260\236y\253\177~\252?|\302h2i\337\336?\210\202 \261\037_\277?\035\233\352\016\225Z\345?\260,R\341\001>\263?P\'c\027\270\361\334?\27211\215\277\374\341?%\276\037U\334\035\356?@\321\200#\343\327\324?\352\271X\363\241\275\346?\224\037\314\304\345\010\322?\255\376Ck\222\376\341?\350\260a\033\352n\340?\305\363;E\362\332\340?,\365^!\337\347\356?\002z\332\010\020Z\323?\310*\n\375\342>\324?\254\370f\213>\333\301?\024\344-\'\357\222\357?\231\016g2/F\346?a\310\260]l`\350?\371\2125\214\344\227\347?\000u\273\224\217N\311?\364\223\230wm=\316?8[E\303RY\276?\312\023\332\303\031\305\334?\357tu[\261\023\346?@\025\216\\\rF\221?|\023\037\001\237\365\302?nn\236+\252\306\334?\026A \376\315\337\347?\277\020\223?5\'\355?\310t\316\354\336\237\301?\366\230\345\004\324\336\344?T\\;i\304\217\310?\000\021\321\014\347\345\350?L0\27407\276\346?\341\363DZ\322\256\342?\301R\235\241\023o\347?\344W9\224\001\023\314?\2213Z8E}\340?<\274\235t\017\247\300?d\264\200&i\354\323?g\334,\264\376\240\341?\200\324\247\3067\205\260?HD\332D\245\215\313?\316L\310\025%\177\331?\022\362\310\277\216\361\354?\270G\317\250\307H\357?0\002!\241\211\314\333?`\304\320_T:\303?`2(Y\3659\223?`\320C\260k\177\252?0\205\017\017\247\261\331?\376,\214I\0314\323?\2422\221\365\362\206\354?\362\030\351\177+\202\330?\230J,\301\\\317\354?\346aZF\t\177\326?p}\177\021\320\234\242?\332\323\211\226\210\r\353?\020t\312\0361\341\355?\272\211\211\340\327e\320?\336l-h\2448\350?\303\232\215\245\335\232\344?a\347\267\214\000\320\354?\214\352\004h\003\343\340?\346\n\263\034\300\353\342?\306\340\3129NU\325?\024\317\204\235Z\332\343?\352\005\227G\264d\327?\331@\231I\357)\356?za\352\265\206\013\344?t\251\245\303~\367\352?`l\224ja\340\234?\200aqv\335\367\272?\250\330\214K\005\007\311?)vy\200\317\007\356?\210\037=\344\000\264\350?\275\263\274mP\300\347?\233c\274(\356\314\342?\270u\354O8j\276?\020\352\316-`\024\262?R\273\003^Zh\326?\200&\221\321\326\032\213?$\301\211\033\rk\357?\037\307\020\315\376\210\353?\252Z\177\270\344\204\335?r)-\034a\314\336?\301\235.\306\016\013\342?\032\211g\254\021\005\340?\005\273\212O\303\024\352?8Gu\202\216\211\322?\233/\327L.\230\343?\300\314F\303w4\247?\377h:\323\206\023\354?\032\343\034\251\266\212\321?\275\270\203\032[\213\355?@\003\231\246&\377\220?\256\224\334\017\035\263\344?\274^\210\373Ws\326?>\347\302\321L\266\325?X\214\215n\323\007\342?\345\300\263[\332\260\341?\200\\u\225\215S\265?nC\351\231\216\016\333?\014n\2753\361\337\300?\345\304\r\314\323\203\346?\350\025\256\202]d\306?@\255}\221\230\013\326?8\2667<3P\265?\020\355h\"n(\307?\366\242\373\245\221\225\352?\344\365k>S\202\347?\340Z\005x:i\272?\250o\254\372\243\226\275?)9$$\356\337\352?\350\310\300v\347\316\331?B\353\352\2060\256\343?j\354\024\245\004-\336?\030\205\321\226\256\216\322? \277\3142\214\337\241?8\273\275\033B\202\263?\200\214$\212\276\236p?\030\323\027\261\301)\303?\367\031\262\275,\300\345?|-\t\347\310\223\301?r>\366\224R<\353?\274\026\330\344\352}\354?\034\246(\246.\225\354?p\005\273\006\342#\316?S`\207\243S\321\357?\n\377L\322w\306\340?\321\030{\003\265!\355?\003\024\014\270\322\003\342?\207{\310\270t*\340?\244\274\261\276C\220\313?\024\026F\027\220\263\344?|d\036\223\310\255\313?\220-\010\317\351\020\346?\316\213\330\320\010t\330?(|@\270\330a\265?Kl$\304\306\342\350?\322\271\325[\251Y\340?\344?\364&\204,\302?\004J\361\3568\253\315?\325e\374\373\003l\352?\250\022\256\265\020\201\317?oz\262I\360\030\343?Pk\245\334c\231\246?\020\373\252l\3526\310?\207\274\305s\243\005\345?\202Y\336\022-3\327?H_~R\250\020\321?B\231r\234\232\016\351?+\r\026;\363\217\353?\260\336\201e\350\363\241?\254\263_\251`\322\350?`i\r\242\"m\305?Sk\312)}o\345?\360+\263\026c\016\311?q\273S\201U\t\352?\246\374\214\356Y\357\332?\366(\375\317\356\234\334?\344\334\034\032^\357\317?\024M\311\324\t\331\336?2\232\315\024>_\344?%\035,+V\210\341? \3434W\324\266\343?Py\373\'\177\337\356?\220t9\301g\345\354?R-\035\277\343\327\327?\034\234\223\tD\264\343?\247\321B\017\362\200\356?\240Vv\0165\211\324?r\250RQ\334\'\335?j}\025\254\327\034\330?v\303\032k\307Q\332?\340\210\033\347\035\036\314?\340\3537\356\246$\225?d*\205\204\033\007\316?\261XJ\212\221a\357?P\007\342\200\006\332\314?\305UHe\367Y\347?F0\240VW\264\350?\260\251\303\251Xz\255?\364\002\246mA}\300?\361&\313 U\265\354?\220\265\344\324\362\337\324?\250\335\333j\246\272\336?V\330=2\014W\337?@\013.\3603\360\336?\235\375\360\037\374\372\344?\204\200;\237\250\206\346?\n\255p\370G\222\352?pM\347i\2457\347?\\\246\232\313\214\211\344?\020\006\375!|3\332?\370\301\355_\344y\274?\361f\244\350i\351\347?\3107M\274s+\321?tq\006\202x1\306?\3670\334\215RS\341?|\251\225\035\321\277\311?\202\037\306\360 \023\356?\207\364\032\251\271?\344?\364\373e\336f\037\306?\200\nj\177\010\024\254?\300~\232\023p\t\310?\327\352\016nOG\350?\353\271\267\030\330\303\355?\374\300\377\005E\017\344?\030_\250\265\211\201\332?\333Q\242L\211\357\355?\360et\023\277{\255?\006.\256\306k^\335?\274fM\250Lr\343?\264d\325\2335\016\314?\217:\266\302EV\342?\345c41<,\356?\201T\014\363\0341\343?\360F9\023\266\343\245?\200.\262\3558w\306?\006Q\001\2453v\356?[1\367\023\215w\352?\340\207\341:\324\316\223?\220J\014\314\216\214\247?\334\335\352\324\376\276\324?G\211AVij\356?YW\232\311\005\256\344?P\025W\205\233\361\343?(\324\306U\334\243\325?\364\230@\031\035\313\336?\354\016\310\352\351\036\336?\200\210\323\236\325\247\246?\230%Y\032\336\216\352?\016\252\244\010dd\356?\243\306%\013\017\250\355? L&\033\241\321\262?wn!E,\236\341?\274\312)\336\266\320\354?\3373)!t\331\340?P*)\037\303!\241?\007\364`^?\265\354?\\\347g\320\245\005\353?\332\315\336aA\327\327?\220\223K\364\307\317\354?\026\333\t-\252\265\337?\211\347%T\305\373\350?\257-\205{\320\332\344?\303QR\327\324\222\346?\020by\313\341q\265?L\335D\274\2060\353?\360\232\021\321\017\234\255?\245\302d\237\016\216\353?\343\377O\261\357\242\351?a\303\265u$\375\342?qC\017\276\375\361\346?\342\031\023\212v\010\322?d\251\275:\241\021\346?\222&o\321^\346\353?\373?\251;9\240\352?/\303\356\017\331O\354?\343\021\272,\216j\346?\004\"\336Mb[\305?I*^\377&\032\345?\316\376\224\026\245\264\345?\204\341hC\300\252\321?\356P%\031\324;\343?\362\213\372\314\2202\350?^;\264cN>\351?\330\222\365M\245\240\331?Z\322\257x5*\346?\035|f]\336\231\353?l\032\261^\037\261\300?w>!5\261\204\352?\236S\335\177%O\322?!\223:\322\037\363\356?s\006\230\272*5\341?\031\036YD\363\263\354?\354\365\025\231\307\217\307?\342\342f\273N\033\340?:\n\346\317\364\211\326?\030q\225\241-\201\311?n\200\003\315:\000\341?\205w\311\342\205\311\354?\033\225Uz\\\332\352?\360\026\322\005\356\t\277?\"\335\n$$\372\334?H\261>\301\277\236\302?\223\336\002\300\305,\353?#8r\246\005\340\350?\216\356z\255\376\205\350?\356\264\240$O\334\332?\267-Cy$\326\353?B\'P\3076\026\344?\270\227\035\237\023\327\277?\331D6\230\253\330\342?\264\211F\354\377W\301?\000\245\t\325\333h\342?\342d\226lho\336?\360\177a\367)\257\317?n@\2743\020\013\350?&\031W\333\277\224\351?\302y\3410\264\244\321?\370r\312\261\312<\325?\340\220\220=\307\243\306?\315\237\3030cM\346?Y\3155\'P\342\354?\222l\277\220\332&\325?\000\004\302\2614\032\256?fa\000T\241\316\342?\270\252b\354w=\274?\320:\274<\360\237\340?6Y.\263=e\340?\374X\352\n\201\255\327?\013\020\253\026\274C\351?\002\255\031\036\3212\334?\214]\313\275\367\233\345?\335\367\236\341\345a\343?\027\031\261\035UR\352?\346\222)\336\016\246\344?@\322^w\'\307\203?\"t\315r9z\332?S\t\300AJ\332\342?\365\025p\027\027\200\346?\\/lx\262\'\355?\004[P\334\272v\335?\003O\020ML\307\347?\030\030;\235\355\311\357?\372\315\252\006W7\324?u\241|\344\010y\353?\036\026\371S\362\326\334?\nP\265\251G\260\322?\364\361hy\325\233\305? \270\350\270\220u\303?\370<\253E4_\267?\354\361\205\035{[\347?J\255\177M9 \342?d\035]\021\006H\335?\310\007q)\220\233\306?W\256el\332j\357?RL-\3618\267\321?\230\354\311H\005\003\330?2\315\235.2\010\341?\274\024\332\344\236\r\317?\204\354\236\236.u\323?\320\270g\024V\366\273?rx2\025\307V\356?\224PN\034\370\237\335?\366\020WM\213\201\351?\364)\321\274\272\006\326?\226\235o\224\366<\351?H\335\227\013\005s\276?\006A#+\005\315\340?\200\230GI\374\207\261?@\200zbY\022\236?\\\303\233\351\325\014\306?\204\266\"n\024\316\336?\253\261\312\r`\207\356?\030\257^\367N\220\327?\334\177Y\212\005M\340?,\245\177o\314N\357?\023.MR\013\016\356?\336\013\036\237\322\035\352?\021\260\302qs;\351?\333\017\224\334\303\306\350?tZ\316zs\177\344?\230\3616\024\216M\351?\260IL\003\327\177\302?,@\363\344\036\253\303?c\320\n\243ce\357?\305\251B\363N\254\352?\362\260\272\000\265\207\332?\000\356i\376\2709}?\002\262k\2700\177\330?\340av\013)\025\304?\202\021d\376\207u\331?\256\246\220\233E\272\357?\3009K;\263\377\252?\336\234\264N\221t\357?\300`\203\372Ux\326?\030\276\216\370\037}\275?\200r\277\373[\364\307?\222\'\312\026\266\005\320?\252\341\361u\2102\333?0Si\003\347\233\333?n\224\356f+\306\355?\304c\001\232`8\320?0\251I\207\031<\342?\020W\362\301\237\001\251?x\377l\343\210t\341?\\gh\300\020\212\335?Pi\031\233\002\t\253?\0204\254N\266<\315?\000t\006\313H\307\334?\254%\212P\n\361\310?l\035*\243\262Q\345?\001\312\303v\032\325\345?\320\374\023\220\261\202\274?\233\210\277\323\310\345\351?\030]\204\210\302\257\333?1\336\304\266\366`\342?Pb\014BuZ\314?\214\021\367\300N\334\325?\204\364\177\221\316\334\317?\010^\204\177u]\330?\016\017\276\021f\360\336?F\363\344\320\217I\332?\272+`\337\210M\340?i\316:\017\274\232\354?f\334\354\230\320\340\347?l]\371L\303l\342?\210\332E\375\273\252\307?\312*\252\276\310\246\321?\357\302\376\222\336/\343?p[\323-\277\205\313?dG\263\021\325\230\320?`\000\326;\214\366\252?${\235WH\253\355?(~&\263%\377\310?\301\236\356\362+\246\342?\356\355\3625\"n\346?\007\216Ci<\304\350?\370\202\365N\022\223\331?\232\277D\001\262\303\347?\237\367K\351vg\350?P\351\363e\247,\340?T_v\337e\325\330?\020\303\216\343z\227\326?\307\211n\320\224\036\342?8\232\252\363\341\374\273?\355l\235@KP\345?\212\216V\204\r\341\344?T\311\271\373\354+\327?\320S\205G8\007\353?0\310\366\205\356\211\303?[\373\213\\\221k\355?$\274\237@p\300\343?\353\352O\250|\200\357?\325\025B\350\'\300\353?\350v\037\256\014\'\323?K\003\201>\027\375\353?\206\201u\254F\223\334?\320\256\22457\337\346?\246\231\210\013\315\356\327?}\371P\254S\355\350?\021\363\213T\273P\344?N\330+\260K\004\325?q\374\346P\377\224\347?\2745\324\353Kz\347?8\204F\302\033\205\316?\010*\032w\313T\352?\210\213+6\351\360\275?\334r\341\023q\014\321?\364\264\242\037\333\355\353?#\332\277\250Y\010\342?\370\340\224n1\364\353?Y\264\344y\0303\343?\330\031Mw\200\n\350?X\216c\024\244\273\340?\006\266\216G\337M\333?>`\340TgQ\327?\250hk\202\362\336\267?\\}v7\235\327\322?\230\347\366_E\345\325?0\373pl\356\005\257?{\334\317\253\242\262\352?\316\275\211\314\220\303\347?7`O\305\354\002\341?\310\377*k\306\375\317?\371\340\204K\263\333\340?@\250{\262[\177\210?\222Wk\010s\025\341?\370\220\371\232\013\326\273?\212c\032\350D3\321?1\253l\264\r\224\357?\232\\\303\245\330%\320?\1774\256\037l)\347?\272\2777\306\037\031\357?\334[h\257\377~\313?``\206Pg\010\243?1\354\346\346wU\340?L[@\272\323V\324?\004\371\031\246\314\304\307?\010(\311\355\0311\321?\020\324$\320\271\016\246?2\030\201a\262\241\351?\250\241\232\037T\031\317?0\373e\317\\)\244?\210F\231BH;\340? \315\301\333j\346\247? \302\246\354\307\306\355?\300\030Z\330RH\220?\r{\200\236?\220\343?2\370\034\276z*\353?\374U\032\323*Q\332?0m\375\206\214\242\323?&\215S\367\240\306\327?\007\030\324N9\350\354?@j\327U\302\247\316?\274\242\352\266\312\342\310?=\227,j\t\324\357?\tq\363V\313C\351?\025r\317\266\035b\344?Im\033\037\202w\340?\206y\330\322\205\275\344?r^\3545O\302\326?\327\203h\223\267y\350?\354\316z\226\351\263\355?\300\373(\002\234G\260?\027\352h\255\216u\355?\350\315$\204\244\"\310?\306Y\327(|\252\324?\330;\303r\244\204\343?|\335gw^\014\333?x\313\314\360\223\265\260?\000*\337\"\206v\223?Z\247@\0166T\330?\322q|\235\272\274\357?\246\360/\264{\004\350?J\'\202>\303\335\333?@\350\202_\233\025\250?\366<\273{\247\t\353?\237c\313\r\010r\351?\237`\373J\346\252\345?\275]\350\024\3605\353?\000\322\235\276\352\340g?\010\226\374\316Z}\357?8\236]\n\367Z\355?\253#\022\245dT\354?\331\275\263\224/\241\356?\330\306\311\220\027\215\330?\271\351\324\004\2278\346?M\254\221E\314\366\345?|\204\013\333bD\352?6I\357\360\301\331\337?\350Nl\277\312m\333? \335\307\335\n1\315?\374\341\301\247mC\307?\"K\014C\224(\324?\242\344v\002\261\356\330?\2744^\227\003\262\306?\370\022\t\304g\026\264?\014ZB\266\372R\327?7b\217$K;\353?pm\037~7[\273?\020\333I1\237(\260?\030o\312\036\t{\274?`%\267\303&\234\226?\032\241-\027\032%\351?\336\334\035\266S\273\342?\366\334\353h\006\213\350?\370\374Z\361\264\360\260?%m\207\317\277\017\352?\364\001L\236\026\352\316?\223\365\371\232\326b\347?.~T\213N\371\340?\220C\310\027\007,\251?\240\'X\\_\353\274?0$0\307\030\232\331?\370\0260\276\273\337\326?>\217\204z\247r\336?`J\216\330F\027\255?\340\353yd\rq\357?\364\343\365\233v\346\345?\000q\303w\266-\341?\360z\261*\3132\345?\252dX\2522\206\342?\331\333w\"h\013\356?\205\002%\036\373\374\346?\240\310\311\224{\035\353?\360\223\365\331\013\271\317?\250\315%\224l\013\262?A\274\003q\014X\354?\234\303fji\350\345?\np\266\272\3604\357?\340U\3601J}\230?u\262\224e5\251\353?^\305/\226n\211\336?\205\355\345e\252\321\341?@\340\235A\022\033\263?Ha|\3215\250\332?\001,\t\256}\177\357?%\372DB)V\350?8Q\274:\342\237\304?\000\364\246\031`\ra?\240\"i\typ\262?\340\207\207\340\377\237\274?\\K4\025\270\214\302?q\374\254\2112\370\342?\260:v^\372\327\330?`\r\347\203\351\203\340?Z\375\364@M\324\340?\"\376,\032\277\007\346?\333b\255_i\311\344?\226\\\346\332\242I\321?8\303QV5\361\263?[$Ri\322\320\355?\030m\001i,\217\300?$\037f\222.\004\306?\236%\302~\215]\351?\345\2615\031\271G\345?\272\355\361%\244\333\320?\370\325Z\235\256\310\337?\300\026i=i\243\240? \361Mu\"\250\322?\377\331\'\275o\255\341?\003\263\'\177\235\346\356?/\227\324\226\370>\355?\274\032\365\027\014\317\305?\000\236\353=\3543\266?l\323\351\230\301\232\343?\212\203o\0361u\320?\314\000\255\363\332y\307?\220\010\352\004\033\263\267?.\006\n\016\332M\343?\270\346@\360\342\276\310?Mg\230)\233\020\344?!\302\344\035\202\033\340?\214\257LTN\225\316?\340\324_yOd\262?\000\305Rb9!\313?\242~\035\2432v\337?\306>\245\021\305\230\352?\340\2007K_\025\316?j~!\0235\240\342?\\\321\031\361\320P\314?\242;g#\"\005\351?\332:Hv\357x\331?\263\225\3223U\267\340?\330u\005@\243\032\337?\202q\337\037\344\010\353?\320\026\273?\264$\306?`,\305\305\177\203\315?@\\\t\306\000\253\217?\347/\"\340O\255\346?\350\320\376\365\034\221\331?F}e=\321\300\355?\020\347\232\022]\316\265? \212\265JD\032\242?9R\254S G\355? @;\3337\004\347?\200\323\370\345\255\325\275?\314\277\r\267\257\007\352?\330\013f\344\214\001\322?\316\357\257\340\217\310\335?\"\304\014\216\246,\354?\24676\236\317\317\354?:\237\016\244y\214\336?\r9\362\210\276\200\340?\326\210F\341{\343\331?\212`:\013A\371\326?\035S\003\364\314\023\343?\340\010u\363\225\336\277?\306\234\356\362\275%\336?md\013}{U\342?\034\323\225\313,\241\352?\326R=v\\\263\341?G_\204R\272\223\350?\361,\320\257[\204\352?\376\013\241K\226\277\322?\270Nf\260\032\203\310?b\320MU=\324\350?\2730v\025\200\357\351?w\302\0173I4\352?\302\033>\343\365,\320?\232\245\311\242+v\321?\232\221\"|\243\246\340?W\274\200a\336\003\343?|\341\014Y\244\263\327?%\270\342HQm\354?\260L\353\372\304\354\246?\275\302\266H\332[\343?\242N\030Y\027o\354?\324 C\036\231K\355?\377\270QX\340\362\351?\316\336\370V\236\311\333?\300\n\334\034\263\236\223?j\273\266\336\341\316\330?8\200?\355*\353\350?\260\263\226jC\006\246?x\237\307\351Y\302\266?%\t\206Jp!\356?B\363dy\024\336\346?\032[\265\246>V\335?,\332]\270\0108\353?\004A\007\255T\320\331?\343>\343\221uv\356?\224\024%\367c\311\355?#\177 A\261\004\355?D\244\214t\2651\311?\334\315x\354`\315\323?rj0,V\t\353?7\363\356\232\244\022\342?y\225\273\027+u\342?\240`\036\277p~\253?x\210\367\301;\231\337?\232v8\020\031\363\324?\350U\023a%\020\325?\347\014\353\345\232\262\347?\220N\342b\264p\347?4\213\226C_\020\345?9\0172\\\252D\347?\270e\277\264\320\371\267?\300\341\311\002;\277\263?\315\245\325j*\340\351?\260=\211\334\006\034\356?\200\320\356\272\205(\347?\230!\027_\253D\315?\303\256\033g]$\345?x\241\002GL\244\347?}\345\360\202\213\356\357?\250%1!\206\315\321?\353%a\3616^\340?\377+7\242\352\014\345?\274\253\006^T\202\315?\340\232\224\370\361\233\231?Hm\302\016%\301\321?]\224%}\335f\353?\310y\235}\205\r\332?\307\324\033\315\263\271\356?P\217\247\376&E\355?\252{+Z\255\254\333?@\033\265\367\371v\351?\2408S\276.\326\275?\310\270\262\205\345\027\340?\230\357y\016\036\311\305?\350\275S\253sz\273?\030\000\302\024\2207\272?\230\277#b{+\342??\242\335\342\004\004\354?6X|\3576\243\333?\374^\206p\365D\333?]\270J\206\020\263\352?l\037eW:N\330?\263\347~t\210\377\350?@?f+\213\236\224?\000\010\217\324ap\337?:p\025\253\016\301\340?$\343\"c\020\037\311?f\337\370:\025\377\354?\204\242\257\272\023\304\304?\010-\307\210\\\036\266?\000\245y\257\317\366\216?0\347\274\005\nj\303?\340\222\321|ds\354?\224[i\274$~\331?t\035N\276\032l\321?|\350a\037\314\365\347?_S\247\275\004\t\342?\376\034\354)#]\333?w\273\263\220\245\356\345?\225\201\341\003\314\310\356?~\215\316\320\325\201\342?d\314\351\003a\232\302?\361\034i\367\226\237\346?\346\033\017\337\262\"\354?r&\360\3143\312\345?\362\261\247\240\"\032\330?)\352RG\276\356\341?:\370\313\315\270\222\355?\025\260\325\336e\005\346?C\220\364\007S\024\346?o\001\024\236w1\344?6K\213\203\374\375\336?\020p.x\266\212\335?H\225\317\"\275(\263?\005\207B@7\250\341?\020\3531\362\347\007\325?r\313!\214K\216\337?k\257\353\257\025\257\347?\303\321/\352\263h\342? \272G\265\273\277\241?\024\323 \354\003I\344?\000\030,z\340(V?H\0026\250A`\266?\351X\300.\223V\354?se\214\245\272\257\343?\254\335\227\032?\010\354?\350%Z\025\270l\340?\030\355J\304[\005\314?\241G\301`k\224\347?F\035@i/\222\323?1f\346\225\002\267\357?I\301\234\270\010\351\343?\2610\317\037\306\251\340?\274u=\256\377|\334?\367\332\223\370\'\231\357?F\376\370\3146\242\327?P}\352\315\310\306\240?\344ci\223\351\363\320?\274\274,8D\312\326?d\253%\231\332\364\325?\264\263q\200\301\247\300?\235\221\334\221\351x\354?G\204\037\226\315\024\345?p\202\335n\331*\332?\010\351=\226<\240\306?\364\231>H\375g\300?8\340e\237yL\324?P\301\307\3735\025\240?\262\274\035xx\030\347?\360\037\342\3551\233\352?\000\340i\247\372\025\330?L8\002\350\207A\342?\330\223>I\357?\344-\006\367\024w\307?\270\374\232v\003\323\271?|V\211\013\231\351\310?\002d\333\313k8\326?b\366\226od\255\325?\272\3133\224;\230\322?\"\\\264K5\025\352?Y\235w\223\014i\346?b\256\302\236\341{\347?\004?\017m\\\303\354?\352\2257\2248\351\321?TB\203\363\0204\316?rc\376\220>W\344?+\304\357R2t\356? \030\237\272\246r\316?\356\211.\301\3354\343?L2\302DQ\306\344?*G\261\303)g\322?.a;a\204\212\325?\361Ii\376\227\235\350?\"\356\207\213\"\264\355?\242\276\337\243w:\332?\352\255\263\225\223I\343??\021]\tB\177\355?\314d\213Mf\205\304?C+s\262}\364\340?x;\024P\261\202\357?\026\334\332\000~\211\352?\374\"\204/\357\177\317?\304\306\036\357\002P\321?B\350`?\340.\332?&\0239\324\314\342\344?\004V\223pE\237\312?\374W\227\275\010\317\325?wf\324\243\373b\357?LX\364\360\363\225\342?]\343\0251\257W\346?\330\253T72\351\324?J\2558\315\205Q\341?\301I\265\223B\037\340?L\202\250\030\275\013\305?\\\220\320\300\323\274\317?\020\220yD\233\001\331?\2544\201b\223\334\302?\270wS\003\233h\356?\376\352.\025B\320\346??Fd\305\271\372\355?\365K\336jw&\341?@\231\342\344\310\001\311?\n\244\201o@\023\344?$>\327P7\024\334?\270$}I\277\375\300?\252\256\3004\234\325\344?\030\266\226\231h\215\260?\340\216\223h\361,\356?\002\201\025\022D\014\322?\352\\\240U\'\322\325?\260\367\245\325\223\244\261?\274\336\230\367}\362\355?\214\260\271\022Yl\357?\037\354\020\212\266\223\344?\344w\364\272\'\263\315?:\367\027\204&\227\336?48\325(O\275\322?\342\373\343yZf\324?ZPME\250{\342?\350\337\"\373\212x\270?\215\217\203\311\224v\347?h\350\24735M\350?\260\227u\025\254R\276?@:\236\r.\260\250?\234\370|\301\205\221\307?\302\314\206\322]\370\353?\206\367\262\241\262\010\334?\315\242E\324E\223\347?\000Z|8\026\237a?z{\213\030\005L\330?=N\330\307j2\354?V\023\t^\244\333\344?Pq*\356K\235\312?\021\216\245\214\214\314\340?B\035_\311\013\027\327?\200I\331\241\370u\205?\264\255\2100\347\020\342?\007\264.\036u\352\345?gV\310GVd\345?\331a\255d\026\214\346?\320\327\020\216R\346\327?\333gnG\242\374\352?@w\341A\224J\316?\000\025\365\365e\244\232?\262\\\014Z\321\021\335?B[K:\374u\346?\036^7\375\322\342?X\300\361\221&\246\305?x\206\030\377\366\315\312?\355\206\277\344\006\302\356?4#\360U\224C\352?n\317RE;\212\343?#\"4\372^\357\355?\214\207\t\345\241\r\317?q\032\004\314\367q\352?\374V\000w\367\364\333?\004\327\226\365\0062\310?B9{\201\2009\351?\001\005X\005;\034\353?\002\035\367\256u>\354?4/\237;\"\347\322?\013E\np\354\310\346?\344R\241\244\023*\353?\334B\314K\367\007\316?(\331\222\370\276\313\354?`01\332\220\211\305?.T\010\3570\345\322?x\366Y\344Wn\305?\224\340f\212\253[\340?\332\241B\333{\252\337?No\177\317\374K\326?\340\243\265\024?8\341?}\374*\3402\006\350?@\350o{\261\\\342?\244z\203\021\345\267\322?O\254\250\312\321\356\351?d\013\267\230;\262\347?(\317\205\300\026\237\264?B\347U\'\351\n\341?\004\342\217\312\177\242\332?\373\263\271\265~\370\344?&\'\024\252\331\242\345?\233\003\346\2528\342\340?@\346\324a\260\343\324?\300\326\321\236T`\236?\364Z\337^\361\242\353?\250\340\263\253\340}\335?\366\014\323\263{J\336?\300\241\313\261\036\304\333?\350\344\036~\371\241\326?bv\201\013\201~\330?2\276\202\t:\255\357?25\002\2755>\340?\n\\\3611\017!\342?\366\304\024\360\035\337\321?\254\022\301\007\233\304\344?\300\375\211=\246\223\345?\022\3735\330%M\341?\314\321\\aK\326\337? d\000\001!\243\234?\332\256\224\036\200\342\350?\374?\033\'p\241\344?(~T\330;@\271?X\"\373\272(\014\342?e\333o\232\233\371\357?\360w`@\216K\252?U\370\000C\277Q\354?[n?\332q\332\354?<\256\355\333\353S\310?@\233n\217\322P\250?\302[b\021v\271\337?\n\t\3115\024\315\342?B7\227\327\336\375\355?9\265X\214\000\034\352?\025\267v\313\302\214\352?\240\375\373\357\326\311\257?\035\\\327\273\245\316\354?\374\243D\261\235V\351?p\221\321\350z\203\246?\323;\377T\317$\352?\034AA6D\241\352?\376\355-\206N\317\342?\317g\\\256+\302\346?\225\037\335-\373\247\347?\245f.\254\324\213\346?\204\346\267\036+v\306?5+\216o\207\314?\024\343s|\353\272\342?\200g\325\232\326\227\337?\245v\334l[N\350?\335\256\215\3510\362\347?\315\311K\345\304\r\356?)0\343\351\244\212\347?\304\204\224T\005\262\343?\320\320\210!}\213\336?%T\210Qn\303\346?\355V\216\352\333\r\345?}\003\275r\255W\346?\360\270\273\277L\245\243?dS\241\304y\232\335?v\201\370-Qb\330?z\311\362\371\243h\321?\250\322\311t\212u\343?&Ga\241f\335\350?\375\322dM\230\243\356?6p\314@\006E\325?C:S\230Vx\350?\334\274\220\202g,\332?P\347\207\214\tW\275?^\313\325W\233\002\334?\035\335x\235\365\265\351?\036\360_n\377\240\341?\240=\216%\250\021\332?6\026^\250\013\347\344?{\343\237\373\030\304\354?\352\016\331\277\343\366\341?\344e\010M\037\023\323?b\025\323w\254\204\325?\320\177O\250\217v\260?\244\3629{\177{\326?>=\313wX\367\324?J!\322\213\336L\320?,\t5\017\023\235\311?0\230!D\345!\305?L\2733o\213\250\340?\030\355\031\366\\\217\341?\031\342\205\223\177\204\343?\260\246\335\266\245\"\307?\261\377\326\336\252\202\357?\366\327\211\312\313e\325?\212\254N\320\200v\337?\374\3759\266\3029\305?\013u\342\017\262\342\346?fh\216\311\337\231\336?\234\215\211yNn\345?\010\2779\200\352a\355?\230\305:|)\307\334?\240\037\277\336s\020\273?\375\274\334\000+\337\354?\364%8`D\274\353?\260}\014\000\232{\270?L\236\224\027\002\223\346?0\000\312\177\267\301\267?\004+Uu\020\016\304?\202P:i\251\305\342?\036\232\212\006l\346\353?\020M\r\"\330\016\301?`\321\247\301\000P\240?@7\177\216T\271\326?\n\202Y{/J\325?\277\352\024\177\211\357\355?^~c<\366\215\327?\200\261b\305.\230\227?\310\357a\310=o\276?\2441md4v\316?\230k\343\365@\361\354? \021\261\232\337)\253?\236\005\025\372\314\332\333?\240)7\025\031\367\323?\366\330K\360\200\322\321?\276\3730\023\232t\353?\020\320\2324^\035\307?2\000\343\177\223\224\327?\210\271\337Q\t\314\343?\345\256\211\221\356s\356?c\237~\022\264\017\344?[\243\240P\305C\345?r(\005\245\305\247\353?x\251\351\322y\337\321?\340Z\237\373\366z\227?\000\206\014\242\236\375v?\252\303\373\307\264\215\355?\273\031\314\221\327\350\352?\332(qT\220{\327?v9\371\361\316\002\341?\327\244h=\305\006\341?\034\n\216\003\365\237\314?FC8\300\333Q\356?\310\357\227]\300\010\310?\300\006V\263/\255\341\356?h`YR1\177\354?\000?\035\225\332\000o?@X\3112\257\337\245?D\263\253:\"w\354?\020]\013\251\330\333\254?\320\007\274\252\275W\272?\342)6\237\330`\340?C\334\310\031\242\200\353?\'\305H\214\016/\355?B\0001\245\216^\357?\253o\273\324\t\223\342?\220r\307gR(\340?\247\275Ej\310\336\346?\324<}\356D\004\313?<\375P\231\341\272\305?B\257\227\023\360\327\335?\232\244\027\347P2\340?\274%\227d\325,\355?N\231\340\254\276t\346?hR\347\217-P\310? \247\r\311\367\232\316?\310<\352\300\310\037\300?\340V\232gKs\317?d\343A\007\316)\341?$\001_g\2758\354?\370\307;\016\243\n\317?\250\237\356\237\373g\336?|\247\307\373\234\335\313?v\342>>\252\275\340?3\355\035\335\313\004\350?,\221&\365\266\276\321?\000t\370\372\030$\223?\220\200\264K{\254\261?\244\322\306\210$\263\337?\312>\'\230\250\206\323?\3700\216{\207\031\332?\3577\353W\253\334\353?Z\217\333\003\327B\333?\240\246\260\272\225c\262?\325QY\204\"\352\340?$\321\331\352\223g\340?\300-#S\345C\220?\332\234\035kt\274\335?\374\257\004\333k\361\352?\021\030\217\257\351\332\341?\251\332\\n\374\211\352?\354\356Q24\213\347?\0211\215\202E\376\342?\300gu\276\220\037\203?\244mt.\331_\314?pkT\235\227e\273?\030o\344\276\233\316\337?F\200\245\017\t@\333?\243\232>\014\275\235\350?\326\004\2003q\240\340?\200\020\003[\242\361\337?~\263\366\322.\024\353?r\245\221*\206r\347?M\352Q\374\255\352\352?\375\222\2628\310p\344?\336\315\001V\305%\352?\3021O\033\261l\355?L\310@\037 p\327?\n\307\234n\236w\356?4\376\350\2601\002\317? (?\304\230J\244?\206\242\306\026\200m\357?\310\\\\,(\341\357?((u\262j\014\305?,\3546\014\353`\322?zlz\253\235\217\333?\314p \317\302\303\330?T3\354N\300\027\320?\266;\315y\3373\342?\2014\213\341<\222\343?\000d\346T\235Q\202?\205\277\002YNH\343?\220\n\350\262\355\306\306?Z\267y\254,\256\336?\340RDI\344\234\274?bE\2039\367x\352?\204\362Q\243x\266\303?\254\206\273\007\250+\343?^\277\027\302 \327\344?\302\023;\221\035\300\323?0\204e\354\322\355\264?\2676\314\007\300\263\347?\220^0\217\036\301\274?A\303\370\315\342\321\352?\010%\003\016>l\340?HC\025[\214\367\261?\206\352\301vQK\351?|K:y\242\363\324?\014\216\350q\335+\326?u\364v\300\213:\340?\032`P\357\272\313\341?@]\371\316\255\007\336?\361Q\014\217\0022\343?g$\225\r\363#\357?\2052\326b9\t\341?\310\t\367\202\257K\333?t?\304c\213\323\321?p\222\322\022\024\333\241?\207\276\302\304\024-\354?}\\\2237#\350\341?\316\223\314\206H\367\343?\177!\321\327\202\326\347?^,\306\322\215?\320?(\262\355\004#,\274?\264\030\373\310`\243\340?\347\307\\\267\004\274\345?\002\014\356\224\3002\343?\316\241\262r\\\240\341?\003G\207U\244\264\347?h3.\301\212\022\265?$\235#\304\003\373\340?\250\351\236?&\016\276?\374\317\230P.m\311?\336.\3577\251X\344?@\302|\3063S\335?\340\230\255@\247?\325?\331\327\265\336\r\241\350?\366\t\273\213\341\340\327?\272h\364\3758\251\351?J\211Ij.\216\341?\212\371@H\027,\324?\240vR\t1r\306?\252\025\312\305\031\364\353?l\262\372\305\277\344\323?\347\241Q\021\215\205\357?0\027\016\246\327[\323?\316Waf&\215\342?P\273\301\374\243\354\263?\026\207:\214\024\332\335?z\273\257\017\\\264\351?zZjhoo\325?\240t\346\030\315\232\327?\302\307cC\034\236\321?\n\277M\003\037\233\340?Jk\242b\334\271\331?\035\366\362VKx\355?\206\r\235\226\220\222\346?\224|\223\222\215\355\303?\272\347V7D\013\331?\270\334\323\251\321r\332?*\374/\363K0\346?\206F\017\344\310\000\343?\350\331\rJ\206r\337?\3661\203\234\203\214\351?\221N\322\220JW\347?\222C\215\256\220f\356?0,-\010D\303\253?\016\377\177\252\\\316\332?\232z\317\314\002/\330?\200\265{%\374\323\351?\2346n\"^\370\333?\362\322\274DJ\303\357?\261\233\006\330\006\377\355?\206x\221\326\037\363\332?\342+\300+VF\324?\220?\255,D\250\276?\374\213\373IJ\025\323?P8G!\206\333\271?\220:)\332S\370\313?\344\300Du\371\025\303?H\377\356\376\013Y\323?0j\216\032\3737\340?\306\357z{\210\241\330?\260n#\376%\t\261?\340\205l\232x\274\266?\036}\237\244\314\305\334?/H;TU\322\343?B\371^=oM\344?\030\324\300\222\3069\263?\331\3626\310\233\314\353?\340\216\0370\237_\275?\220\323\2322\215\354\276?\230\305\271\224|\362\261?6Ft\310o\355\353?\330\233\315J\315\016\274?\312:\237|#\247\352?l\207\250&~\273\340?\262\224\335\326\275\003\356?\354>\321\251\343x\314?\204\350\001\226v-\314?\240\035\211\232\226\217\315?\346\230\014\245\364\342\350?\330\n\025\000\322\210\330?\257\262\010\255\275\375\344?\232e\203\270\353~\327?\271ju\370\345\302\340?&mqZt}\337?\362\223\315\262\351\325\337?\370\353f\036O:\302?@\262\357\244\006\251\342?V\307\'\373\036\251\321?\3432\260\332\231\232\345?\231\345\274\343\371\263\341?\024\370\320,\275~\307?64\353\177\251\"\331?\241\302\024\266A\030\356?-\010\342\032\023\004\346?z&n-\306r\340?n\350\000\267\007\313\334?\344\223?N\352\031\307?+\004\207\216*\350\344?\304nG\317L\320\303? 1hq\024z\310?\310\203a\3013\211\353?\2654o\376\314\023\356?\223\031^}G\036\354?\227c\274\354\"\300\341?XnL\324L?\266?So\312\200\234\314\350?\000;{\271\2569\247?dl\020\025\247\367\355?9\340\236\222\326\301\347?\2345\246\261S\317\306?\360\346\260\034=o\336?\020k\303\n\343\000\355?ft=\217\3674\323?\340\362\204\025\023L\270?ae&\230\222\350\350?Hy\313\024\275\224\274?\271\337OR\245\357\356?\324\017\215\302\010\212\354?T\0058\226\261\\\344?\025e\354\251bf\352?\027\247|\2040\331\353?alTf\330\231\355?\274\355r\244\256\265\302?3\270\023a~V\351?\2303\316\335\376\257\270?\306\r\251\\f\213\355?\256\1778b<6\342?\376h\235-\307\333\343?8l\017#\025A\327?VjZ\377\033Y\345?\007\255J\201\333K\341?j\002^+6\257\325?\3368[\234\370\371\340?\264\261\300&\370h\346?\2201z\210\253l\304?\353\"\206\367\327V\352?`\016$\255\254b\313?l\205\201\016z\346\344?H\327\205\301\020*\304?\354\335D{Y\265\353?so\375mA:\343?\254\306\211\256\201\265\311?\310\013\347\343\347R\265?\240\215\201lkd\243?\274dN\211\342\351\347?\266\207\335\323\271}\355?t]\242\333_Q\351?\202\005\374\346\023e\324?\001\247\343\t\241\205\357?x*\206`\225\321\263?\024D\233\030y\211\340?t\237CUWC\322?\235\343LD\311h\355?\232\314\241\'\373\016\346?\310[T$0\n\346?9\356\3549\266\355\350?PF,\275\333\275\256?\326WJv\307\026\352?0y1o\320\317\330?xv\2106\334\301\346?\224\0031;>:\322?\004\230 rqc\300?T\320\316>\265j\347?\347\201\377B=d\356?\236m\200\237\272\202\357?&\200(\215\353\224\354?\342<\257u\031\271\347?2?\332\205\260]\323?\2605zC|\274\355?;e\307\003\305\351\345?<\023x\3518\r\336?\323+5\340\014\274\352?$\230\343\tm\362\326?\010\371\3169uz\337?\250 w\220\221I\347?:_3\005L\255\320?\335V\031S\276\225\344?$\266\370^\275\372\332?X\333\213Q\034\274\263?\220\356\343\345I\353\346?s\371\3348\330\217\343?K4\306\375\200\277\354?8D\273\224\276\326\351?\026W.\336C\311\325?\326\224p j=\343?st\214Q\342\303\354?\274\033.\343\373\203\354?\246\363&\005\210\203\346?\251\342w\275\203\304\344?\274\320\250\017\235\235\326?z!f\200\244\205\325?\022\250\177B\352\352\356?8\013\021\215O\222\262?\374[\232\236\014l\330?\340\3546@\021\177\307?\010\004\370\320\323\271\333?b\035n` \274\342?.?M\347u\324\356?\364\345\237\027\004\347\324?\"_\214\276\303\027\356?\\\215Y\375\266\n\332?\300\265;y:\323\276?1\216\"\327\255\026\345?\"\335\3132\215\271\355?b\016\252i\270(\345?!]\273\344\241\244\346?\331\215\276\2336\243\356?pQ\327\276\035\356\265?\320\244\306{-\312\312?|\265\334\360=c\305?\370\241\023\273o\233\345?\0205\257\234\267\211\342?\364G\213/\376\365\315?\200\247=xw\337\266?.l\352\341\\j\333?Y\310\261\267\254I\357?\302\221}\035\327y\345?\202d\035\276#\206\340?\260u8\021\220\357\344?\222\360\271\256\377\231\330?\206\317\311\236\260x\324?hs\035H8\232\304?\000\244\002/n\010\271?X\346\347\234\223^\265?d\035d\3648?\326?\000hm\300\342\263D?\014\262&\231\364W\313?\214G:\227\332y\324?l\234\311\020\331S\334?o\017XK\264Z\355?%\023\335v \335\354?\014\314\364ke\201\353?\222Y\246\333\202+\323?\214;\321\322S\307\347?\034\327\234~\374}\341?\340\363\254\344A\371\340?x,I\3616\251\304?\270\273--\273\364\354?\010Vcy\240v\276?+k\250d\332\367\342?\337\004\251\222-+\353?\200\306\001\263Kh\274?~\366\234E{\372\336?tO\336y3R\351?yu\351Z\016?\352?\320\274C\201{S\255?2\020\210\223\016K\355?S\003\354\256~\213\345?\000\321\324u\275,\350?\317\261Q\365\221\360\343?\221\246\014\334\345\272\351?\321\2500\203q\306\352?)8\031\371`\366\356?\350\023K\305]\031\271?h&B\227\033m\310?\020\2600 \273\256\265?\250\336\016\376\005!\352?v\230\224$\216Y\326?@\367Z\262\335\346\264?L\206e\364 #\345?\036\244\351W&\313\356?lY)\221,\356\311?\310G\346\313\262-\355?\250\201\375Q\030F\266? B\350P\2132\332?`f\2073\2411\253?d~MW\010\324\327?\310\375^\316\225\030\351?\331k\312-\332\210\355?\242~\035\037>\224\337?\301b\201\262\031\036\345?\n\332\2333\345\027\355?\360\014\236\225\023\332\242?\344\245\337\'#\'\317?\004\t\177\033\244\266\325?`\323z\353.W\245?\033\227d%wC\341?\372\t\023\367\222\252\330?0\277\022l\322[\267?\020\214\246\337[\250\312?\340\037b\204\333\275\233?\231\035\235g\213\230\351?\356\324L\211\006\232\343?P\332j\t)\251\354?Y;\212\374z!\351?\220ow\365NA\336?}7\271=yK\341?%\347\317!fM\342?\013>\333\217\213{\346?\204\372;[S\310\323?\356\240\003}\231O\354?L\201=\177\337\256\301?psU|\340E\275?\021\267\352\035\235C\351?\001\334\345\016\013W\343?\334\036{Ix~\310?\305?w\024\224\256\347?\344\355\034\213\271`\332?\324\343\335\005\310$\345?\234?\237\315\354?\322?\242U\333}Ta\354?Hy\310\222\000\336\266?\310\275\345^\355\252\262?\034KV\254%\321\306?\240\333\277\013Z\311\306?\260=\024fq\315\332?>\271(\257\303_\355?`\372&\322\377p\337?,\021\034j\\\266\330?\210\372\246\261K\334\314?B3\235ZO8\346?H/B\321c\252\332?\264\001\003&#$\300? \310]\203G\016\252?\311\353\350\023\260\210\355?\343\240\206\310? \344?\306{\275\215\224\025\331?*\251V\373\231\340\332?\361\271B\275\206#\353?z\240\333E\354L\322?\036QH\346\210\347\333?h\321\325fs&\353?\3560yif\275\350?\325\003\263!{}\356?\370w\225|T\231\326?\360\232&\r\014\307\303?C\361W\360\260\002\357?\020~t\210\204\010\241?\226\220\000\t\337\336\320?\003\204\264\375\233W\351?r\207\300M\223\246\345?\013\313\005Fr!\344?Js>B%\177\333?\337\214>M\'g\355?\370\\*E\313\267\351?T\t\242\200\313\306\350?Jh\275\370\365$\346?~\357fnyO\337?\236;\013X\211\007\320?\2729\363\307\347>\324?\340Is\301\027~\305?\360\245d\372t\256\247?\\\200C\t\244\020\313?^`\246\026i\276\320?\304\2064\020\373:\325?@\rF\335w\313\270?\274\255~8\330\211\333?>\313=\323\372\317\336?\024\246\357\353\311$\300?\262\024r?\n\t\343?P8r\250\221\305\273?^\037\301\240\377\252\334?\234 \336\306j\214\302?\020-\022\343a\350\261?\210P\251p\"\226\323?\210\346\222Z\025Y\344?H\\\220C8\214\276?P\360\314v\036\254\263?SUu\332/\271\342?\200\3441\003\2342\217?\306\211Dm\303w\342?\304#\221\331\221\257\326?\366\230\361\004}\375\353?\240\233\005\271\n>\321?\000Zl\240{\004j?\226?%O\030\225\332?|p\206ZF\272\333?P$\251~\376\206\334?\272\375\323\233\217\323\330?\263Car\250\273\354?\024\347\277\354\035>\316?H7z\226\020J\351?\324\306\324\245\\;\316?/\006)\272\343\307\344?\301\251md\364d\350?h9\361\0069\332\311?\333\260\350\306.\242\352?\006\203,\246\037Q\352?\326dos\222/\351?\315\245UTX,\345?\331!\271\227{\350\351?hR0\373\234\326\302?d\214G\037\275q\327?\352Mu\362\000\214\343?I\353\23354\"\350?\360\3602\3128\'\354?\264,\337\216\001j\341?\366\203\354Z7\275\325?\272\ne\034\226\221\345?\362\365|h\037E\326?V\024\256\201\356\374\354?\004C*I\236H\315?\030\030\234);2\271?`\343\271\007v\233\224?3\035.\367b1\357?\300N#$\'\266\272?\360&\357\001\036\324\353?^\353\177L\273\217\357?\232\250rf\246]\334?)\343\2542\340\342\342?\354\275$`\225`\317?>1\265\370E\201\354?l\334\246z\365M\354?\270,\207S\320\001\335?\234Nr\245\207@\331?\254\225Z\217\251+\341?>\037\246\227R\024\321?\360V\322\245\200\343\337?\331\203\207|\367\037\351? \037\034:@w\310?0n\264\363\301\244\334?<\254\007|V\036\344?\364\266\332\254k\212\325?;4\001\211G\214\343?\342\361\360T\273I\343?/\367nP0U\353?\214\353\336\017\334\242\333?}\022)$\357\361\340?\237\274\323\206\212\224\343?-\030L\374\240J\344?\000\343\311\240\3176\331?\032w\305\255o\034\331?`A\007\311\323\001\271?\267C\314\241\235\336\351?\214\311B\177\375\234\336?\226\332\374\375\274\010\322?f\204\207\275\246\377\334?\202%\243\270\020\325\324?p\250\307\233\024\211\302?\222\345`\261\033\305\335?8\364\347\035\276e\337?\216\215\217j\371\257\356?6W\245\247\347\301\322?\210{\252s\025R\300?\366\347\234\330oO\347?\225\377)\216\377\215\340?J\037\265T\'\222\326?By\202\234\302\237\325?\200\221\247\323](\221?\231\277\357\342n{\351?$K\346-w\032\327?\006,wMm\376\357?`\214\202*? \242?l\207\357\'\356\331\324?\016\3304y%\213\343?\374[\336\362\331\377\324?\220\003\373|\330\345\260?[\334\315\315\311-\345?\335\263\360\024\351\267\346?(Xm\211\202\372\263?\362<\373\236\'\307\336?W\356*\350X`\354?4\007\332\216\005\360\302?8\324\375\277gI\315?\276M9v\362p\337?\222Z\377\217\030\340\323?\375\255\334\304\375\030\341?\220\223\211\375\034\322\271?\032P\311$\316\200\322?\316\3556\207\322\307\351?f\022\226:\226\247\336?P\360\257>t\347\333?d\n\234\004\347\376\306?Z\025\t+\300\036\344?{\352\353\3159\303\344?@j\352\210\236:\265?\343\212\230\213\003;\342?p\240\334wS\304\310?n\207\351C\nw\352?\030b\252%!\217\270?\232\025m\303;U\344?\033\234\217\327\251\005\357?\360G\346{\302H\352?\220*\t~-T\254?\256\334S\337\305\342\337?\2003E\177\212\036\266?\274\300\375\207yr\333?\334\3458\275\244^\354?.\035\215\232K\324\346?\334\035\217\341rE\325?\247\031\337D\200l\344?\240L\325\301\203U\356?\373\241\004\005\221\317\347?H\\\010\325n\034\302?O\334\037\374\024$\346?`\247\350\265\335\204\341?\354\026uI\037-\353?\016\356m\\-\030\337?\300\221v\217\376\244\245?\206\345\"\200\263\255\356?\3168 \235H\245\322?\346 x\261u\236\324?\230\022\323\272\267k\313?/\241`\260cb\350?N\022}\004\021\203\343?\270\256k\020\272k\327?\364\243\343\267\026\303\316?\232L\22101\245\332?A\362\030\017 \247\342?p<\341i3B\246?t\301\332}\215(\300?\000.\304E2-\341?\260\3242*IG\252?hV7\257O\336\322?5S\363g\n7\346?\365\035\313\035\372\323\352?8\n?\355\250\314\264?\227\245wI\210\257\344?\\\376r\223\014\242\300?\207+\205\332}\311\355?\244\n\316<~h\343?\234\005C\365\346\003\302?xh\004\024\021\264\356?\232\221E\235\377\256\355?\355\037\324h\375\244\343?A\334{\243\177\327\347?mg\253\034\206[\354?\200\027\333\315X\"\270?\275\016\317*b\017\347?\3248\337-\004\027\331?X(\345I<\274\275?.\317\315\331\'3\324?c\030Jl\013a\353?\240\007\305\001\224\332\263?l\275\033\374\321\301\310?\200\301\025\031G\243\204?\251k\000\375\237O\357?@\276\3141U\311\232?\002\014\'\201c\261\346?\362Y(\361\374x\343?\200\3247\340\304d\252?i\37385\177\357\353?C\2734\374X\214\343?TA\301\357\246o\302?\010|\201\\(\253\261?\367\300\362\231>l\342?\265_\\2\022t\347?`\277\233\200x\347\321?\262)\265+\356\206\350?V#\351Y[p\347?\033\331N\317\232.\357?~ms\242$\000\325?\\\000\264\352\307\213\350?\230\240\222U\022\027\265?\256?\003L+\343\356?`oRn\303\216\357?K\203ax\033\376\353?R\370\233\230\217i\324?,R\352\203\311\366\306?\036\221 e\362\242\357?\030\302\353\346ZK\327?\337R\302\265\027\246\354?\375uB}=\205\351?\260\027Y\235d\022\310?\235\'\342q\225\217\344?\326h\256\241\000\324\324?xJ\222\310!\322\330?\322\210\245\255\217\310\343?6M{\014\271\354\322?{!(#\377\325\354?\310\241\035\346?\202\301?\354\212{\340\005\333\323?Q\317\357\365\242?\354?\242s\307\306\213\306\347?\206iu\220F\313\333?P&\376s\014\310\351?p:o\263\215)\314?\252\372\3252\254\026\331?\206\325\362\236\345\253\330?p\341Y\244o\356\245?{\203\351\272\014a\340?DM\026\374v\007\300?\340\350\\k8\253\353?\323*A\026\312*\356?\220dT\202\363\336\243?\014)Z\t\037\\\344?\340\343\262\250g\254\315?\340O\024\224\337<\325?R\376A\241\343]\335?I*g\303\3177\353?\004\033\013n\210\n\330?\260#\364\331\264\364\320?\360\302\177\330\224H\247?\204\255\277w\007\345\300? \003\213\235?6\302?\201\350N\3237\352\350?\204\006\223T\206\263\316?\252B<\310{\367\350?\216P\215\236\361\316\357?k\221Gf\363}\353?\264B\016T\213-\353?\024u\331V\327n\312?\306\207\365\311\034Q\357?\306~\263\355\222\217\351?pQ\214<\245\023\337?\270\213*\244\364!\346?\255x\237\036\374\304\344?8\360OI\372\225\377\344?\370\226=\376)\265\357?\210\350\2738\364\345\262?d\023\302\225\233x\321?\207\310\205\317\201e\352?\325x\216`NY\343?/\355:\211+\026\341?\330\344\030\315\007/\333?\300\374\262\325Wc\256?\264\244a\322\031<\315?\276\370\342O\214\010\353?\326*\363\241\273\346\332?N>\220\332\023\374\323?\300\311\261\213o\025\246?\334D\0217O/\300?9\3151\317\207!\343?{\315\350A\3716\341?\200\331k\013\324\001\262?\334\267\322\031^\312\343?D\224Iu\013\331\326?\332i]!\324\332\347?\350\3671\371\353q\320?\014\224\353f\033\213\334?\027\350\001\367F\212\347?t\321\234\337\350\327\321?86\274=\3230\314?\302\373c2\374\305\337?\200\3313\265\223I\257?\334\3605q\211\226\353?5\333\334\201\204\350\341?\242\263i\232\275\235\345?\240\031\376\216\317y\357?\266\364k~-\253\327?6f\237\\\236%\335?\346\323)t \306\331?\306\333]\201J\365\346?t/\342c\227\001\317?\230\355\201+\351\202\261?\205`\244\255\351\\\354?\320\223\311\241e\312\276?/T\014\010ZB\346?\224G&w\301\206\313?\232I\324[2\305\344?\010@]2?\035\354?\3269<^ZO\344?m\351Bk\332\323\345?bY\232F\3658\350?\350\312\035\375\354\356\272?\240\335kR\211\304\313?\r\315s\241\275\342\350?\304\037\262{\357]\303?\3561{\203P\235\333?Z\261/\266B\326\346?\t\342\341\017\252\021\342?\0373h:%g\353?\035\337\335h>\207\340?8IqV\273i\307?\"\327:\207\n\340\353?\320\007E aA\314?<-jl\231h\340?\351\2406\204Oi\341?m\311!\021#\303\346?\310\205\002S\241\305\357?\304\022R\253Ae\307?pm\365u\245\307\263?\240\315o\330\306\323\315?\324\013\372\326I\234\351? gqx\336\210\263?\300g\253\377{\032\251?}\255\327i\342\361\346?\346\030T\266\000]\334?`\014\362F\303\225\276?\344X\274\32767\312?\370\262QRF\205\340?\210\234\361\242\360.\334?)\213\257\3510\213\343?D\000c\324\2757\302?g{\2406\244\313\354?\323\244\331\314\216\016\350?\2063T\257\317\034\353?*(#%=\364\335?IO\002Z\017x\340?\"\203A\331\nc\346?\225\374\322\370\374s\352?\230\032B]\250\272\300?\310\342\273X \267\313?\255\354]\025\216j\352?\220z=\030=r\347?y\366*\036h\360\341?\300\013\252\310\017X\212?Y\230\325\361\026\313\347?NP\020\250*(\326?\356\303)\273\203\200\346?\000\020\0000@\272F?\241\n\311\375\361\247\027\350?\312]0c\241\316\335?\2771{pw\342\344?>\220\333\330\266\256\342?\020\231\211~*\035\267?\364\365*\242eb\330?yH\245\016\007c\347?}\222\034\227\356\210\355? \356\360\021M\325\327?\200+K\212\024z\304?\324k\334\350%V\334?\224g\325\347\233=\301?\302\305\366\"J\020\331?{>u\276\272 \345?J1\300\006\346%\325?\360\330i\025\252d\340?\263\204\356\265\276\254\346?\200Z\372p\322\213q?\374\262*B\035!\310?(\344n,\311\226\330?8\305\"\352\261Q\262??\226\026\032fe\352?\370\254\247w\261\033\326?h\261z\236n\252\311?\257}a\260-\367\354?@\021\010}\270\277\336?\000\207@\375\207\254\244?D\275\250s\017x\325?\036\202\213\343\000\242\354?`Pi\t\201F\262? \236eA\032g\313?\t\036\367\377b\353\352?\260~\002y@&\347?\364\273\231Q\215@\330?z\347\260)nd\327?\367\257\222\373\263\231\355?\233$\224\225\311\341\352? \264\232Ji\323\342?\330\215m![\246\337?\235\2219\377\350\245\345?c\212\313\324\360\317\342?\2402\316\231\367o\234?X,\224\222+\313\275? w\367\2201\022\306?\252\030\023\200\244\340\322?`\'4\315[\305\354?\220\227\372@\255\250\356?4\234\345\2104\t\321?\330i\273Nrz\306?@\240\361C\225.\243?V\314\251\274\275?\350?\334y\372S\005\254\344?\010\306R;\237\355\311?\370\271\032\254\263g\307?D\200\021\256\3619\325?I\344q\253\037\030\342?B\350\211\226sc\355?*\261\rD\276y\335?czp\311\371p\352?D\331\265\271\034\374\351?}Y\367\333\303r\344?+\036\2365D\307\352? \2025\022\'\347\250?\276\010\3469\205S\325?\000g\nq(>\210?\332\343\205\310\375\262\325?jE\273\367\006\353\345?\320<~Z\030\337\313?\016u\022\341\377\274\325?\323\024q~\261\347\355?\231\2169\204fi\343?t\234\210\215\n\320\322?\207\357\236\317\223C\351?\346\305\302H\0279\341?L\204\343\357\254\310\315?ps\323\006\271\013\313?\220\325\033\362\022P\332?]n\273\004\262\357\351?\376*\357\221\334;\331?\001z\004\340j\002\352?1\254e\200\014B\357?z\032\214X\371\357\333?h\'\307\013\003\341\263??\023?\017\361v\353?\371\332\'m\377\304\343?\232\333\251}A\010\345?\344g\261\233\210\266\304?\3444\262\205\210b\326?J\327\370\347xa\356?\320u\256\3738a\344?\n\270\2333\212\234\346?\312sj\336\310\211\320?\362\236\3148*\030\350?FsEV\014=\357?\300\320\332K\253\356\211?J\2723L\'\317\324?\350\225T?\006j\350?\tr)\236z\006\352?\3206\021\302o\366\244?4~ft\314\017\336?\240\200#\344\320(\343?k\254\365\271\253\230\342?\201\204\252K\007\247\343?\300\314\313\201\213[\270?.\242\256%6\353\344?\035\267R2X\\\357?\023+B\361\256\320\343?8R\232\010U\345\352?+\';\260{\362\350?\266E\375\t\3656\342?\020\227S\337;M\345? \2569\201\2434\310?D\276\217\013\202&\344?\000x>\356\261\243\242?\027\344U8E\312\352?4\022)N\243\004\300?\226\371\321\320\245\234\345?\256+w\002CO\336?\336YJ\275La\332?d\371\245\317@!\316?X\225,\225\323\237\326?\267\330\212>\023+\355?@\253\211\253\370\275\251?\032\357\200}&U\321?\372d\304L\261\200\332?I\032\244\212R\370\343?\3067\330\360\210k\342?\000(\203\0068/\305?\275\221J\212\376j\356?(d\236b\337h\357?\200\347\342\242f\330q?\340\344\002*v\275\241?\340\361(\366%\306\323?\200\271\006\247\nI\204?R\241\204\246\rg\325?\244\263\2465\367s\320?\330K\270\035\250\305\301?jc\233\003\205/\333?GwS4R\n\350?\337\300}M6q\342?:p\225)L5\350?`>\337\340\261\010\253?\360\343\304?\221\357\326?\334\035R:\027C\347?N\320\370\316\351T\326?\266c\217|)\263\323?J\217\214\213\366\231\357?\n]\2467\312\024\321?\276^\345\320x\274\330?\366\342O4\313\031\353?\240\310\272\204\330{\230?0k\375\364}\375\266?\344.\000\032\267Y\340?T\346\004K\363\221\351?:\341\245\347\255\035\331?Q]\250\355 \007\345?i\342\225\376\331\356\356?\320\267\221j\321\"\275?\007\324\313\343\212A\352?\360\2668\037\245=\311?p\373\216!D\337\333?\336\257 \252\355\201\335?\200\346\014h\t\353\341?\305yS\301.\231\351?T\350\241\372\177\365\331?\020\032\037nq\224\253?`\242\020\\\037o\341?\213\177P\277\205\240\341?\203l\345\n\\\007\352?\320\320h\3028\r\243?\331\276K\033c\344\341?\207+a\265\305\221\347?\234\204\373}\234B\327?\214YW\300T\001\356?\240\327\010\306g~\304?\317\332\307\310\254p\351?\241\302\200-~\020\350?\3406\331\376\2251\353?R\"\236\241H\355\344?\001\373\000[\357[\342?o\025\344\026\261\340\342?v\035\345*0.\355?QGm\355\310z\351?\032\351\200/\017\251\344?\202\004\005&v\002\350?\374.OZ\324\301\353?\027yc\214j\231\352?\254\225\245\261\034\233\311?\202\353\032n*T\354?\">\037\363\267\272\351?h9\251Ye\366\336?`\2656>\004\330\357?\200gX\224u7\245?\\1F\034^\364\312?\310\010\352\356\255\237\355?\300\266\002\005\003\213\240?\000F\204\366\274\"\354?\370A\365\265\351S\341?\336]\210\370$\241\323?\\\t\350=\366u\312?\n\221\271\367\365\350\324?\311\242\341#\253,\357?.\207^Ee\246\351?\314\000\342\274\002c\317?\374\335\2737\021\352\356?\004v%\211bK\351?3\016\325\346\354-\345?\024!r\353\000w\344?MN\325\314\331-\352?\307\212~\022\375\251\356?8\317-\375\037f\327?fw~\274\003\177\343?\362\\y\336\024!\325?\204JJ\356\350v\355?\277\315\344\372_`\351?\326yOw\027Z\330?\036\3422\344\340\326\331?\034\2105\003\255\364\342?\260\n\326\3422\354\332?zN\277\230\320\300\330?\024\207P:\260\201\341?8gt\362\233~\351?\230\371p\304-e\310?<\343g\330\366S\346?\255<\"\240\177\341\345?\232>$zf\016\352?\264\270G\201t\221\352?x\034 \272\374\006\322?\335\377(\326\352\306\355?\274\r\346;;j\306?\264R\210\360\000S\321?\202c\343\361\261\t\347?\001\323+\336\014\332\345?\330\371\271s\351)\273?\324\351\325\242\230\243\313?\371\310,\'\361]\350?\270[,\306\326\360\330?\250\336\\\335F\000\265?\030Q\354-ib\332?\2003B\301V\214\310?\340\r\177\361\036U\226?\000\203\351\317\360\321\214?\364%|\323O\337\336?\315wJGf\360\347?\316\363\234k1[\335?\307W\006\265\234[\340?\342\340n\256\235~\324?F\320>\330\264\"\332?\236\262C\316\351t\351?g\036\223\376pc\355?\345\304\350\310\016\245\351?\330+\232H\350\033\356?~=l\n\364Y\357?\014#0\'\305\022\331?\211\305>\263\033/\345?vp\272\256.\263\336?1\364\034\233\274\034\354?@+msk\265\275?\370F\365F{\372\312?Ll\212\375q:\320?Y3H\313\352\301\352?$~\365*\340\021\350?\276\330\025p\231v\343?|\013\2411\373\216\331?\004\220I`\221$\322?\030\0179i\003$\347?\330\257#\211\\M\326?\227<\211\013H\014\354?d~K\013`\240\323?\004\002\022K\214\001\311?\300\246\377\202\353g\337?\316\267\331\224*\276\324?0\323\t\374n\310\245?\217\267\304pt#\351?\022`\356_`\364\342?\030\307ai\324\353\331?\276\223\027\325[\010\323?t\214\001\253n\353\337?\235pIk\267d\341?+\037\'\307\274\250\347?|\'\325\257(\363\352?\243>\332\302\023g\345?;,\034N\362\232\354?LKX\240WR\312?XA\353\036\025\310\331?^\345\270\216u\022\350?\010k\352\231\234K\274?RI\346\310\030\225\337?\230\261HP\201\261\312?\240K\346\332(\020\230?\377\205\275\271\000l\356?\220\210\261\004\207\232\245?\3700\255\035\226\361\300?\2561_\376]R\331?\255y\253\267\317E\354?\374\234l\226t\233\325?+\241\331b\211\022\351?6\177\315\235\277x\344?\224\257\206=R\214\340?\271\342?\332\216\'\350?\014\302R[)\371\307?\304\256\204\332E=\300?x\344\025\354a\344\273?\217\377y\357L\260\353?\365\034\275@\220#\356?\234\334`,\301\301\302?\032\344\371\243\274\215\342?Z.>\334\243\203\337?\322\025\014\347\324x\351?eHpt*\022\353?\304\335\266\251\3759\317?\365\225v){\266\340?a\002\027D&&\350?\312\020~\350\241r\320?\250lLa\035\201\315?\304\257\3347T\205\312?\274\343\231.\035\200\307?\343\326\342\360 \351\351?\220\002\351\226\204I\275?\316\346\263\304\304\302\351?\203y+CA\251\341?\210\243\304;\270\234\304?R\337\275\'\032j\352?x\351@\027|\006\261?z\371g\270\243\n\342?t\357\303\230\353\037\321? N\243\017\223\270\313?E\267\335\225v\000\346?dF\365L^\264\341?nU\214\021>2\320?B;\375\330\317\001\341?O\225V\266-\236\353?\373K-\336\374\316\346?b\225\355\352\010K\323?p@R\201d\255\356?\" \233]\272\273\346?\320uF\3135\213\350?\327\235\354(>V\340?5\277(\356\026\t\345?\nr\234#\332\261\347?\003\230l\013\242\327\353?X\017!\337)\376\313?^\267r\245\356\313\357?\200\266224\026\317?Fpf\247\215\006\350?\200\360\203\255S;\246?;X\241,(\205\355?\335\031\261\274\032x\353?\366!\265@\325\245\353?*\253\362\314\2141\327?\340N\263l>x\307?\020(p\300\303\317\355?\264\371\261\245\312A\347?,\201\270\203!C\326?\32786i#B\357?\226\324\377\013d\034\321?\241(\251\235q\260\347?\326\027p\274\354\257\351?d\373[{\007\301\357?\354\204\n\216\\\202\336?\256[\r\365\314\373\327?\272\024\202\244Um\346?\262\177On\022s\341?\250\363u%\215\325\344?\310 /\325*\231\351?\242\327\355\232\354\203\322?\334\3231@\244\"\312?*\376$Y)\212\337?Td\364\000O\210\350?\224_\323\241\247$\322?\205\264\350T\204u\342?\200\201\256\252\267\212\273?\221\314K\270\267\276\344?\202jYM\316H\330?v\342l\372~\241\334?N\235\317\264 \342\355?\364_\261\312K}\313?\221\345\014\356\242F\351?\254}\335;Og\333?p{\337\373\365X\312?(\2228\013\206\236\336?\370\277\212\345\201\244\336?\374o\224F\304&\313?\214\375\3700\347\222\316?\000+x\3736\237t?H#\237E\021)\306?\323\335\376\323\200\017\357?)\255Q\232.M\350?f\007\200\267\257\037\343?\343\035\210\311a:\356?\324\356G\241\037M\345?\026\002v\274\363\325\331?0\272\346N8\327\246?\233F\320\356\374\311\341?`zo\266Z\364\310?\023=\215\317\'\323\354?\320\330\372\270\2272\350?x\204v\262\213b\320?\326Z\277\226bj\340?\000\274\343\036\207^\243?E\340\241\034\'\335\356?ZFuH\326(\327?\221\304\244\2412\326\346?\360\227\214\210Kg\355?\200\'#\324\266(\257?\214\366\252\267\227\264\304?k!\333|J\323\347?zs4\324-\334\354?\000i\315\001\245\020\336?\307\362\360V\036\334\351?\363\264\250\324\320\367\344?\246\315\033\370q\357\356?.\234\212\345\033r\357?\274\340\003\374\035\276\325?a[\314\332\361\211\353?>4\010\346\334\022\333?F18\333\274\332\350?h7^{\177R\357?\240:\030\220h\306\335?d\341\000fy\'\300?\311\235\304\014\325e\342?r\215\010\031^\n\337?\000h*\2463\3256?R\377\212)\350\010\352?\262WLSY\326\322?r\316`|\247\370\321?j\233\250+}\231\346?#71\035E\216\357?\300C\t\260\004\340\307?\370\372\351\375\035\256\327?\215\235Z\035\004\t\354?\224\020\246\321\253\353\340?\034.K\003\341\372\324?\206\231V\327\375\330\324?^\227\212\315@Q\322?\344\310\322A\032\353\331?=\3426\267!X\357?\346\342\t\263f\307\344?\340),\276\211\322\274?I<\202\306\325\372\345?8\3332?>E\303?X^p.\005\304\266?\030.\370\347\026\241\333?\nxU;\2755\330?\276\373\252\212P$\323?vR\257\221\301\021\341?\002g\3451z\341\355?*R\236&\0169\322?wv\201\350\\\224\345?v;\213\257k\345\326?b\340\363j]\234\337?\300\3428\270]\000\312?\332j-S`n\325?\200\036\367\365;/\311?\206\253\006\026\377\356\323?\231\013\201\244\026A\342?.\356\235\327D\215\321?V\367\0231\327\307\340?P\345\251\014pq\316?H<\324f\341\201\305?\272\355j\204\272\347\333?\245\331(\037\211\022\352?_\261\265v\241\311\344?\374\366\326)\037\255\351?\214\n\266VI\321\355?v\334\022{V6\353?k&VI2^\344?\'\006\306xm/\356?\\\3206\353\201\300\330?\334\314\020\035>\314\352?\310\200\364\375\245V\343?H\206\217{\006N\322?\256\330\261\326^\245\322?X\3716\227\226\333\274?\263\">\241W\307\346?\264ee\257jV\312?v\214\002\275A\366\341?\'#\327{\004\370\356?\220\203\003\331\206z\333?\364\010\007\204S\213\326? \220\004\224\026\366\330?\202\rG\"\300\373\342?\314\371\362\310&/\344?Qt\333*\244\311\343?\305\307\224F\227.\341?\000,!\007\035W\276?\310\373\3301\335\232\321?\000\017\247\025k\035\276?\004\374\2648\013\320\317?x\373qd\304\204\334?\006\004Fz\003\233\323?\300\230v \020\317\311?*\033\323\333\022\207\323?\273\332z\014,7\352?\n\330\274\211\t\256\344?\257\217\323S;\333?(\224\034/\264=\343?\035}Ln)Z\354?\234\'\233\013\013\242\357?\024<\246\220#\273\336?\230\327\013\031$\277\357?%\315\225\256\362\376\356?\377S\036\245\306\224\355?\3729<4\323L\357?(\214y\347\261\313\305?\257\037\246\203\'\334\350?\247\355hD\3339\344?\245+\2174\375C\347?\300c\375\010\201\264\275?H\022\243\014B\274\260?\363\007\024\207~\306\344?\335d9RjH\341?\257[h\267\202_\356?\227\343\315\260\367\264\353?\370\\}\265\204|\271?\341\226\356UDY\356?`\306\204\231]\237\225?\262\364,\346(!\353?P\373k\354^-\242?\253s\007\034t\202\347?d*\276\363o\234\330?\034|\312sd\270\310?\352\306\362k\003\307\344?\363^B:\004\277\345?+\241\013\021\027n\340?\034);\347\356k\335?.b*\307-}\341?Y\026a\361\037\317\344?H\220\t\356\366D\273?\344\024Y\3570\351\344?\022x\2035\261k\341?\224\340]\325?dW\203\263\337\007\346?c\324\221xmM\343?S\027\253Sn\"\356?\030\315n\375\226x\274?\323\302\272\325\223\324\355?,\261:\216\245M\352?.\247\024\362\277\363\335?dSKj\030\226\345?\264@\250\356kX\343?\330\353\233r\203I\265?\315\223\005\306q\\\352?\357\212\205\2264\331\353?\247\206e\263\024h\351?\206W\212j5\327\341?|\034\023\356\361\243\345?\002\356\310\377tH\357?\323\255 X\343\353\352?\300rQv3l\234?\351\247PP>?\342? \031\323wq\r\264?\000\253M\347\010Wr?0*\355\022\262b\356?\210\020\271\263\357\222\312?aq@f\214.\344?\316\336>\032\225G\324?\000\307\232\273\277\352\212?\261h\266\330\275\361\354?@ky\313F\344\321?x\235X\330x\244\322?\215\036M\223\340\370\345?V\027\022\305\252D\340?\260\341+\325\217\232\310?\264\377\270\2716\310\344?\275\301Z\256W\317\353?\340.\256\014\311N\241?T\001me\331\236\355?B\366a,\237\202\347?hk\321\336i\350\304?\000qz\237\333\007\327?\361\310\341\014N\211\342?V\335\245\000\361I\353?\0107@w\341%\335?\005{\373\221X\371\351?\263\204\034\324\346\177\345?j\231U\316\254h\347?\201,\033\013\324\"\347?t\036\016\nL(\336?\240\247E\202\237e\246?\320`a-H\270\312?Pb\367\225\260\365\341?\224K\2421\370\003\356?\214\344I\307\305\333\356?\366z\\\254\311\220\342?\014\r\241\\\347\272\334?\320\225\037T\003\236\340?\276\320c\350d\254\327?p\343\001\335\000/\242?\264\3777\177\350\312\321?\002\000\030[\254\212\347?\270\\\305.\2035\323?\250\251\366:w\370\304?\236\317\240L\326]\350?\204j\201\202\254\004\305?\250k\357\326L\364\306?p=\236n\024\311\333?\315\177\026.\345\341\344?7\237\312\345\227e\353?\n\325\315\353\177\227\331?\260\275\213\367\316[\274?&Q%\373\245#\342?\230m\t%\266w\261?\356\243Z\324\322\027\350?\221\253\340]\237Z\354?\356\332w\304Y\020\346?b\021\016\262\363\027\343?\340\014\352\241C\362\267?0\356hcP\313\326?\230\230\270\317\260\334\347?\323\304GGr2\345?\027d\253\023\016\246\353?`V\364S\007f\322?\335\005\320\342\203\232\344?\210\251\\\213\314:\337?\327\272\340U\005\241\345?\340\210J`\273\307\314?\010@s\200sh\270?\200\352\016\372\345\306\263?\246\3605\235\003\253\325?\000s\355\226\370\352\203?\275\024\032x\220\352\344?\310\251\347\013[\216\325?\300x]W\377\312\336?\235K\261\316S\375\342?\020\205f\254\320\026\324?\304\rZ\255\356\360\340?{\252\255&\030@\343?^\327\376\370\036\204\355?2\215hV\234\332\353?b\301\257y\313`\353?n3!\343\253\277\321?4\005\301\267d\216\322?Y\216s\371\027z\355?.\223\363\244\355\202\354?\241\235J\260\263:\354?\000\364Py\342\006n?\346\257\214\t?\304\341?\342\331~\1778\214\350?\236\241I\nY\252\334?{\001\2063Q:\355?\020|A7\031\231\333?%\277|Vdz\341?\332\270\354\202p\035\326?EXj\213\211p\350?\207\350\263n\322\230\354?\351\373[X\016\217\356?\312ux\311\366\343\342?\240j\376dk\256\220?X\364\033L\361%\310?\216\244\275\215\225\364\354?H\217\210\020\235\022\315?\340}\271\256\252\374\267?@\263\252\373\322d\202?\026\351\021>\324_\326?\026\374\204\217\301\357\321?\272#\277\2261\002\333?\260\301!\350\276Z\352?A\334u\363\004\241\351?\277\347\226(]\361\346?\212\273\375\225\004\274\330? \231\232\230e\024\354?H\205@\301\302\303\275?\341\2608C\0250\341?\257\304\3133\335\215\342?\024\373M\221\001\217\334?_\211M\373s\255\357?00\272\202\334\224\322?(*\005A\363b\276?\032\273\224\336\374:\357?\254E}\251\245Z\321?\302\222\270\336\325\351\355?^\242\367\260eC\325?\244v\335\nU\007\334?\334\272\231\272G7\332?\252\3534\\\2404\351?\177\317\312\023\023\251\350?P\236\0376\311\365\314?X\001\343\014\233L\261? \363\235\270~\014\272?F{D\265\241\321\350?\270 \236:\373\017\324?r\330\360\227\264,\346?\274\270\242\365\240\305\343?2\027P\3343\001\326?\220\301\345*\006\003\316?\2321M\270^y\341?\001\324r\273h]\342?)\237\206]\326\361\353?b\217\3064-\352\332?P\023\007\2471\213\341?\367\247s\352\n\222\342?\366M\204\3520\250\353?\350\364\233\001\210\317\274?\200\342O\356\001\001\225??\032\346\007\226#\352?\357\251\313\313\343\225\343?#5\177\2418&\340?\205\002a\326\214\236\350?\320m\325\355\351F\313?\020&8\036G\317\277?\256\275V\177\332\271\337?\341\362!\006v\261\357?\300M\331\201^\353\254?\373\002#\331\240\332\340?\300,\261%\335\251\331?\300\031i6\346\324\324?\226\335\\\020tn\353?\354$\253\'\256,\352?t\'\340\210\374\247\350?\310\034\335\253\337\265\313?\rv\201\035\356\305\340?\253\274s\304\323\232\353?^\210]\244\262\261\333?\010ua\321\221o\313? &\200\322\244:\273?\320\363\013\240\303\201\257?TN\366\020\224\351\305?\214`\256\027\332\034\340?\020|\356\033\"\277\305?\014wG\311\323\314\346?\026\227\025\360\316b\324?>\216\360\277\006\236\325?\264\376\237\221\253\337\300?0\3637\253\353\301\323?\304\261aFWj\306?\320\'\355%\004c\276?\200Q-\025\330\r\223?\374\236or\207\260\306?\270B<3\315\027\266?D\227\344\343\261Y\342?\352.\365\t5\331\331?\264W/\271Z\t\320?\306\326\355\025\360:\341?\256\312dE\270\017\347?\020V\'\035\230\035\303?\000Am\356p\006\201?\210\304}H\023\267\311?\300\372r^t`\211?\231\363\215\307\302\213\353?\346\343Z\034pm\326?\3006\260\027\204S\355?\223\006\313\237~\330\341?D\265\263\333E\001\357?lY\007?\002x\327?(r\236\247\013y\351?D\346_ho\036\310?\215]qMFY\351?t\330\303\r3g\317?\320\343m\250\243\255\276?\271\\7g!\014\340?>\355\207J\030!\347?@\214\331>I}\256?\370\037q\3559j\333?\236\244\240\\\302\234\341?\227d\304Q\316y\347?\375\235\215\362Y\033\340?\301\254\322/\231\274\347?\030U\354jk\275\341?\005=\343jCa\355?\020\353%\331\300\024\243?\253\231\345\007]\257\343?\007n\220B\273\347?d\014=W\231\342\350?<\301\\\340\311\320\320?\334\231\247\240\344;\320?\240L\361\245`u\341?\367u\n\317\370\263\346?\3453\271\035S}\351?\014\375i\t\013\022\356?\372\300-\245Dg\335?\326\343\307Z\344\217\330?\002X\254\362\240\261\344?\304\337\257p\022Y\334?l \023\216\340\373\336?\340\025\305\265\345y\356?\034#\252\324\017\247\352?\264\323\262{G\215\307?pv\0359\001\315\306?\225\3428z\204c\355?^\341\343\017S(\344?\261\343\027\262\327\236\342?3\t\322%:+\344?d\035\232\232rD\347?\034\366\021,dY\315?%\356\244\340\013\340?8v\000JQ\231\305?66\326:\035\276\353?_\246\207\253\377\371\347?\216\352\213\275\216\247\346?Y%N\326\305\332\343?\206/\362W\'\370\327?\272\024\"w\302\035\350?\035\345UF:\256\355?\330\033\230t\254X\356?0K\365\007\"\206\322?\252\"e&\353a\331?QZ/\2703\273\350?\236\300\014\313\016\234\346?\244\001\332\341\023\346\321?\366\016\014\204\364\230\331?t\014\216\207m\320\324?aOD\0273\354\353?\006N*]\303\237\336?\002W\014L|r\323?\036)\204M\345\020\336?`\\\263\213M7\260?\340f\352U\273k\224?\216\235AA\013\"\332?\335\354\037\013\0349\343?0\253\000Wp3\264?\000\260\335~\037\306\257?\214\370\305O\200g\316?\2404\351\022d)\267?\332\326a\006Ox\322?\224;\025\263\267\316\323?\316\276\343`b\342\341?\220\273\032Lx\273\330?\030\236u\362\300\261\327?&\233 \266\375A\341?/\274\374\206>\337\356?\321\020\353\366\2550\350?\301\322 \253\037\200\353?|\270\203\332\341\210\353?\266\310\264\247\026p\330?\\h4w\315l\302?\262m\211$\020\300\342?E\303\227\356?\360l@\202\212\\\353?\375\257e\264X.\355?e\2052\3655X\351?R\251\224t\335M\332?\210\336\013\022\022z\356?Y9`\372\001\230\345?h\364\367\217\004\362\311?\354\330\216\353m\276\327?\334s\274\367\300p\313?y\272\376\231R+\356?\324N\376W\224\255\315?\000(oT\323\315v?\032\023\377\016\006=\354?\220\264\224\327\352\255\262?\357\244\212\226\313\372\353?\3170\306\350,h\353?&\267\242\215\327\357\357?L\315\354\352F\300\356?4\271\325\377\014S\313?@\233\252=G\200\324?\212w-\345\021\377\342?\200\236\350K\360Bv?1({\347\253C\351?\225/\354\226\202\325\346?:H[\0310z\346?\036@\"\\\322\301\350?\202/\266\256\272\255\325?\372(\337km\220\335?\026\253\314\225\321\341\320?8\n\310\252\244\302\330?\255\035\330\223\371g\342?f\357BkT\266\322?\240\224\233\304}\350\244?\340\273!\375\035@\255?\256\356\300Ny\035\321?W\366c$\357\270\350?6I#)\354\214\352?\312\327\007\272\325\315\323?\345\'\250\351\216\254\342?Z\021S@\265\346\325?\344@\316S\034&\344?\317\200\266\210mG\353?\020\202\300\337u3\320?\210\020\360s\215>\324?+\277I\332Ig\354?\2534\301\2733\300\342?\334G\177A\347\301\330?\247o\371\272MY\341?\250\021\0364p\265\353?7tS\331\2523\346?\nj\214\007\332\257\346?\211y\364\026f\022\350?\221\220\234\"h\202\341?\320\242\372\0018\274\340?R\351Ch\245\242\354?\324\203E\344\331\373\326?0\217\201n\346\357\275?L\257\247)\224\310\303?\000\027\026\345\276\003g?L\017\014\2421*\341?\320\362\363^~f\240?\377p\3656\2163\345?\360\025\361\270A\026\332?:\027\313\334\0162\356?>\316\302os\036\326?3VZd\307\345\346?\236\244\nP\212P\326?\320(\000\226&m\332?\254$\203+\311>\305?|wC.fV\317?\200\332\250>k8\245?\\e\025G\003\212\315? $\021$9\356\355?\3644*X\255P\337?tP#\217\036`\357?\236td\360\351?\320B\352\350\237\340\273?\266\354\202\336\307[\344?\200\363|\205\350\331\233?v\231U[Z\001\347?\320C\340kf#\240?\264\235v6r^\311?\314{\301\013\363\350\302?\320\222h\200\231\257\320?=\000\351k8f\346?\212\227s}r\323\346?\354k\202\027\225_\342?@s\327(\031\010\341?C@\020a\256\010\341?`:\260]\033\233\346?:\235\314\014\254\327\331?\314\tWA\310Q\327?N\024[\222\007\255\347?Y\202>\\\306\204\347?p\3701\351\t\300\356?8\331\336\216\201\226\303?\272\3013_\2524\351?\234k\320\243:g\341?*\027\342\236\265\257\341?\310\350)@.>\304?h\362\206Tr\377\310?p$\2670\376\324\246?\216E\274:\327p\336?\030\3327\324h\235\323?\203\363n\347\231\332\340?&7r\021\314\357\332?\256\025\314\022\025\207\327?\216\235G\277>\204\343?RC\230}v\321\354?@E`\311?\034\245?9N\352\240\236g\355?\232\035j\302\203*\353?\256\010D\3205\301\322?\364p\256\350\372\273\313?\366/\271VrT\346?{\0060\266m~\347?\322\233\370Xx\377\346?\200\371\007Hy>\332?\030\263[\360\037\004\260?\310\234\216\374\264\252\304?\010\345\235\'\205>\276?\376\346\361\027\271t\320?8\334\230>\212\363\317?\377\236\347?\020\360\245,\343\260\357?R.#e\0142\347?`\363\3024\326 \310?\'U\256\177P]\350?\366o\354\013}\203\341?O\016\377Z;9\356?|\212\241\347\230\323\352?Y\035\221;\374\247\340? \302\305\346ol\334?\333\372\033Qn\372\346?\372C!\267\003\t\342?\020YGQ\262\265\272?h\217\266\240\254\273\350?\220\232\231u@+\347?\032\200=_\035L\351?\026\310,&\261\203\351?\250e\215q\312\226\322?]\001&\241\014<\355?r\261sw\367\032\325?\324i\215\265\257\031\332?v\005\007\275$^\333?\371 \r4U\363\353?\024\362X\326\272\022\301?\374v\035\\\345;\357?\265\216C\321n\374\343?\321B\230;\r\305\344?\330\016=d\222\376\261?\310Z<\225_\265\323?6X<\005\225\253\336?Z@\320\240\330\344\353?W\236[{E0\357?\027l\266\347y\305\343?\204\343\365\303\337T\332?\307\030\316[\201\211\356?\300m\321\255F\322\317?\207lV\365\"\345\351?\210/\001h^\353\311?\314gy\341(\255\306?x3\"\254&F\311?\023\037\233\023\204\207\352?\300*\221\\\027\020\304?8\266O\364;d\307?\213\235\362\317\264\346\342?\240\364C\205\025a\327?\322\020\216\203\227\023\341?E\270j\362\331\312\342?$j|\247\227G\353?\210\\\213\341_\351\346?\361\222\324\032\216e\352?\010\'8\371\t\340\336?\004m\037\345d\217\310?\363\306\274\020Z4\344?\354\330MM:\207\343?\355\355\215x\253z\352?\210\032\177\307=\000\274?\"\223\0022s:\337?\357\364\377\331%\234\351?\031e\313P\310T\354?\336\000\315sV\335\326?cZ\'w\233f\344?\370\240\2211\025?\322?\240\223\273\275C\343\332?\365u\311\025\335t\345?\335\014\001L\227\306\344?8\370\222\342~#\326?<\202\0101}\023\357?\364nF\264\230s\315?\'v\277\207S\327\352?@H\307\276\220q\305?\210\001\322u_\024\313? \034;T=\233\345?\314\347\312\320\017\253\307?\300G@\365\241\370\217?f\304/6\017)\337?8-\353\322\240~\345?\350\360\240\353\205\351\341?\360K\366\002;F\255?\214\t\310\200j\372\306?X_)\300Mc\261?y\r\273|\3621\355?\200\ty\276\313\036\254?P\t\273\n\222<\314?\232p\325O\3003\355?\332\276[\037&\235\321?\313e4\255b\353\352?\334\307\326\210\034\204\346? \002\'\244\257\222\312?h\365\366\342\274\'\316?\013\223h\236t\206\343?\016FgOG\257\322?\003\037\327\367\255\035\354?\326\2045M\342\246\344?\333\315~T\220\276\352?\204W7\351\371\r\345?\360P\023RC\366\326?`$\237m}6\257?\000\237\220\307\312\304c?_\256\241\344CY\357?]\367\2241\305t\341?O\365\323\311\276\246\342?\360oHX\334\356\334?\204\226%\341.\036\317?\037\213\271\203\331P\353?\304\260\205rG\227\306?\247\315\020\337P2\357?@\301\245\345l\000\301?\310\221.\372\355T\352?GZ\360]\343\354\357?\267K\374\377\274\203\353?\234H\r\323[\t\342? \312M\262\256\034\231?\306\0244lLN\351?\250\242|\322\320>\300?\326\240\323\360mj\330?\\\202*\203*_\315?\311\001\277\0210\212\341?\347&~\3360C\342?\204og\351\017\025\320?\322\311\356\246\270(\337?\234-\316>\t\261\343?\236\"3\202\353\351\343?(G\010x\323\323\336?*\303)j%`\334?!!\220\254\233F\351?\300\333\333r\226\226\335?\031\322\362\362\263S\352?I\227M\251v\021\343?:\210\255\204\376y\341?\242\243[\214\374<\352?\323\272/\200pR\342?\350\223b\032\027\036\331?8ND\377\005\354\320?`n\375\337\341\363\346?\000\010\374?\260c\223?PDl\223\211\336\301?~g9\025wJ\341?\210Y\326\325\273\221\315?\017\271\301L\242H\355?$\247\375~\261U\304?\037\273\307v\177p\341?t\030I\006\235\236\347?\030\234\265\345\0255\301?\254\n\241\246\275\372\324?h&>\211\307\302\352?\204!wJ&\025\336?\310O\312J9\352?\217\306;\333\355\233\344?,\211\223T\242\233\353?\252\355\312\016_\010\323?w6\220\t\027\263\343?y\250\360\245\205\246\344?2\002\342\307\212\037\336?\310\223e\026\344\374\334?\034\351\364\'\305l\311?p\0228\226bj\313?\3364`\221\030\224\353?\201)\244\237\034p\346?P\021h|\272\035\332?\000\362\267p\217[\\?w\262>\255\231\231\357?\316\245>@\026t\324?\342\273\337\037r\273\342?\240~>\\\306s\237?h\370\223\322\261\014\326?\304\351\306\337D(\320?`\325\000\271!\251\251?`\np\353\354\211\316?\034\200\221\037\310b\306?\021E+\204\227u\353?\354\203\177\323>\023\345?z\247\304PQ\020\346?F\210c(\267o\333?\276aM\0166\347\355?dt\031\343\367\331\350?\274\375\331\367\363\213\310?R\362\243Wx\301\357?\342\320\275\341\037Y\341?\034\306\250\300\317b\322?\200E\364\252\233W\343?\232\313\2117\276\313\356?\354Ys\025\261\211\310?\244R\261]\'\256\357? .\317\271\231%\221?9\375\235V\241\203\354?B \016\201\360\275\347?@\217>\r\246\240\346?\002\223O\251\316G\353?\240\223\326\326\374\376\251?\014\240\240!\3334\302?\032\207E=\\\374\351?\353\\q\341-\221\356?r\231:\242}\376\357?\002\341\337^O\316\337?\200\364\036\335\257\376\263?.R\371z\036Z\321?j\347\341\223\357\356\356?t\227\365!\334L\302?\321\2436\343\307v\346?r \354`\223\201\352?\216\305\221\354\220\225\345?\000a\342Lk\202m?\272\267\"\217$,\347?\016\230\322\362\334\352\340?xiN\316\033;\275?\272\207\260\223;\265\350?\207\236Z|\301\027\350?2\204;\257\356H\356?\274`\351`s\340\324?(D\246\301\324\217\261?\240\206Xg\374\017\272?\200\320k\250\316V\337?\312\264\023\251%\206\325?\010\353\204\177G&\327?\270!\226,??\342?\326\2304\321\317\333\342?\232\234\375\317\307\354\322?\325\327\253M\341u\344?\000\302\375\235\213\351\324?pP\310\301\216y\256?\253\017\240\312\305\251\347?:U\273\366\005\310\343?\310\277\234eI\214\315?\001\035\276\257T\324\356?0%\244\031\305\203\316?\352\347Qu$\274\347?\210\354\311nwP\274?B\t\2114\007l\357?,m\000\231\271\202\307?\260)d\236E\305\356?S\375\0063\346M\352?a\212\343G\300\"\344?@\324h\247\354V\344?;z\026i\325\223\342?\300\252z3\267*\322?V3\226\270\324\004\332?\030\005\203I\336\370\320?\270v~{\234z\261?\340JmV\036\305\300?4d\225\361\301R\331?_\306\220\213:\372\347?\260\267\363\331\314\241\302?\020\373LE\322H\317?R\3753XE\255\333?N\353s\235\233\006\332?\377\337ZLm\350\351?\356\275_\215s\352\327?\257\2713\262\003\272\356?;\"\026L\237(\343?\310\036\366\323YK\357?\225\331\246\364\257\341\340?\354\262\201r\303\311\326?0SA\302\302\331\357?_\204\005l\263\010\357?\254\352\362\306\374R\305?\265\000nT\254\215\340?X\311H\004\212\264\355?I\375\257\021\202\373\342?l\"!\354V\036\327?\000\342\220K\017(\235?w\020N\221\253\005\344?\000\301\001\025\206@\334? \203\334<\241\337\332?@\334\255\363\312\030\232?\000@\243S\370E\267?\363/:?-\030\354?\227\370\323\246\254V\347?\202p\320R\336}\320?kn\304Oh\257\346?l\026\331\253\336\323\331?\200\250Z\037\230\010\275?2u\313\346\3251\352?\236\265\007\252G\237\333?\026\026\356\275\223\330\345?t\241\242\237\025\275\343?\002\327\330VV~\336?Wef8\374\003\357?\240\270Pu\241\320\353?\354\302\026!\035\261\302?\360\203\337\014\000\222\255?V\327\205\372\001>\343?\020\256\255\020\254\241\313?\262\215o\214\245\223\346?\034\\\326\324,\213\326?\370uo\347\001g\266?\370\2473\247\374\363\302?\360\330\212\0143\247\315?\\\244\274`\273\305\356?\352\362\236\331 O\333?\356E\270\366\363X\330?F\320\236o\237\212\357?\026\007\323\030\240\272\335?\350(\032\277\037>\264?\022\346\313\211O\360\333?w\340\027\371\220W\347?8s\254\276s\206\305?\270\014\317\231\346\232\312?\263\014\332\307sS\350?\375\317\364\274}\204\353?\266\343E:\341\324\357?\261\326\243\346\214/\342?\226\300\217\310\000\235\331?\210+\217&k\225\265?\"d7R\215\377\356?H0\326\206\315\235\332?\020\343\264\335d\337\243?\324\000\033\202#Z\334?\230\345y\374RE\312?\246\261\363\366\265\271\333?\3166\021\024\211\236\321?\010z\221\017\026\222\277?~\265\312\317\347\276\342?[n\\M\212\024\346?\222\237\344lV\021\323?\353ys\252\220u\344?\264`\310\213[\344\347?\242\227\\l\321S\344?\020&\371\310a\357\250?\324\343_;\245a\331?\n\006\370>H\247\353?P2=W\310\033\304?#8\335$\351C\356?I\205\250\0366\266\353?xY\352\260\352n\335?\324+t%d\370\316?\370\272\244\262\321\232\303?\\\022\331\302\302\035\356?\206\213|\311\210\251\334?A\272E\203|+\352?h/\250\224\207V\353?\321ex\207\236\002\356?@\214\214\351\325\241\252?\264p\331~N\344\331?\374\037\225\213\005<\336?\224\037v\270\303\177\302?xk4|c\273\314?h\003\026\250W\\\313?\230\\\022\037\371\003\310?\230\347\274@n8\355?\330\3063T\376\016\272?\010\371u\3616\022\347?\010RW\347\252\240\346?\035\213t\361(!\341?2\307\222\024\354K\341?\016\243\206i\025\245\346?\247_\002^=a\355?\374\033\336!\362\332\315?\306\201\n\334\000\334\343?\363\260\2117Hq\351?\243`\342\377\013\316\343?\256\352IW\365\t\322?\253\006\234\327\353\315\342?\334\310P4L\330\302?\206C\351\220\363?\341?\300:Sm9A\214?\026qXE\030\037\323?E\016\243D\216\226\351?Z&\253\266\225\037\344?\325\311\034\317z\316\350?C\372\024\014(\360\343?\324\274C\204\265\330\353?\270\213\244\236\277I\317?\315A\332XNI\352?\340\365j\333\311\251\232?U\024\333\031\246:\356?\346s\367\021!\303\346?\200b\022`\256\270\332?\3760n\351\356\204\353?:\340C\276\324\312\327?`$T\255\177\020\232?B\000J\234\256X\322?\212\362\317\362~\310\325?$7)\221\221X\317?\227\277\333\251\310Z\355?\274X\3768\347>\313?\362:\037\340\262\306\340?\324\010&\032\231~\334?\010\230X{\020&\315?\274I\007\017\320\002\352?\201\230r-K\357\340?\nL\210d\3373\331?\354\n\311\032\361t\322?,\211b\232\225\366\357?@}\3443\214\016\354?\350\270\377a\377\030\333?\270g\345\004\014\300\316?\364\307\242{\377p\330?\250\226\320f\302\313\333?\274\344>\"&\373\351?\254K\323\213\364\212\323?\000\322\307\330\342\222\344?\350 Js\016W\353?dx\260;\036\010\303?\334\260\242\\\'l\301?.\\i\226v\004\321?\270\'>\324\335\346\340?\003\031\034\2629\302\354?\372\353\2607W\307\355?\320\020\223!4\274\314?8Ot!\031\"\273?\330a\314\026:\215\351?\002_\321\'\203A\342?p\376\260\0003\331\252?\017\003\247CO\264\354?\232\266\177A\304\177\350?\200\315\214\020e\351r?\317jJrO\354\352?\256\351.\341M\252\356?\ro\025\2510\030\343?\206\177\210\022.J\347?hqQ\'\275\035\342?\310\206nV\362\334\262?8\2072_2g\315?\246\004\350%n\346\351?\350\257\222\244\023m\263?`\022I \377\002\341?pk\262\t%_\342?\344\332\216\257$\315\311?@\204\350r\334\225\231?\317\376\3654\374Q\352??\354\346\216H\340\353?\220\353G\216\217\326\340?\346-\014\360\345\370\345?\\.\206\212\342\201\316?\330\206[x7}\357?\220\322\255\320\305\032\331?\324\303YA\265\t\322?P\351r\337\300\220\343?\334~6V\301\200\300?\234w\272px\016\344?@d\362S2%\234?\014\274O\343m\357\316?\316-\03234o\347?\346\255\320#\312H\340?\323\260\000*_\007\347?L\231\252\263\255\275\337?\021\034v\230\247\t\340?B\254M\177\034\347\336? \223\310\341lm\320?J\365\351\023e\022\334?\014{\'\216\033:\341?\336v\233\363\003X\325?\020\234\262\346\210o\301?\260\005\204\"(\324\267?W\301E\017\006?\354?|_1\207\300\276\302?\276\327\002\027Pk\335?\315G\344\246<\210\342?F \266=\016<\332?,\274cL\220\323\340?\027\266\242\323\213\022\346?\334\275\024G\255\377\345?\200\240N\307)d\226?\302a\302\307-7\353?\204,\326l\2271\350?\302R\021\312\316k\345?\266n\255\363\267c\347?\307-\253qGX\350?\340\260<\300\311\260\313?\330\254\356\312\302{\347?\276\353\032Q\023\352\341?\310\327c\372\024Z\302?`&KO\243M\252?\351\022>\\\321\321\352?\334\205PI:\261\323?\016%\317\262*k\355?Z7\024\346\216\263\325?\340>\370g;I\321?[X\003\022\306^\350?\032--x\205\300\341?Ql\266B\230\333\340?\206\245\351\243\244\026\334?\264\360\263\225\223\351\303?&\3671\316\365,\327?\344P\245_\352\275\315?o\271\n\206\n2\354?\327\3722\361L9\340?\216F#\333\337\360\322?y8F:ud\351?`\030\305\216\335\007\274?T\335\026U\350\274\315?\307\266 K\262\271\340?\252j\002b\325u\337?\370\312B\215-\363\311?\230\275W\336X\343\354?\000:l\212\341\370a?\225_w\014{\032\357?\363Y\326\346zo\356?\340\031\261\002\031\246\250?\250\262\327\317A5\347?\\\374\375\266C\317\314?\014\264\340f\300\304\305?\010]\363(\236\014\311?T*\354AV\010\342?`a\277\"i\r\316?\364\332\266+#\347\327?\262\234\303\014L\217\341?\226\354_~I[\352?\342\022\343\017\021\334\323?\376P\013:\013\242\332?\005\315\303b\335\205\352?n\032@\236\007\353\354?<\230\337em\200\314?z\233\nO\355m\351?\200\260l\230\021u\312?<|\3546\225\'\315?d\t9\326\311,\302?,\230AM\305\004\356?$\247e\037\265\255\304?\376#\317\005\246\240\330?\013/!G\356\225\352?\340\020\365\"m\333\315?d:sxP\017\352?\260f\t0\0052\317?\230mHC\231\004\265?S8Uf_\366\356?9\314f\227\236\037\345?\200-)\313\252\n\231?\250\224g\277v_\261?\230b\350\037\232q\263?\200s\223x\"\230r?\200\\eV\202\211\236?>Z\324\310\246\024\335?\3160\n\355\251L\327?\024\246x#n~\345?\212G\270\2463_\336?ZP\006\215\263M\332?\365\020\316~\342f\341? ]\365K\3467\320?\267t\237\375\243\344\342?(P\001\213?\365\307?f\\\244\250\337\035\352?\006\020\213\r\211\361\321?@\346s8\024\213\210?\335\327o*=x\340?\366\202w\316g\272\341?p~E\257\236`\341?\274Sh\350\032@\337?h\313\204\335W{\301?P=o=\325\321\347?\"r\316\024\230,\343? $\212\273\3039\313?C\2643\232.(\351?\263\335%\t\350[\357?\325\216\350eK\362\342?\210\336}\222\030(\272?\2200+\256 \223\250?#\344\261C5K\350?\356\214d\362g\347\354?`\253\347\307tJ\355?\261`\324?\201\037\350?\2043n\030\300\003\341?H\301\250\006\312\222\344?\317\242\000\324#)\357?8e\312\331\276\362\321?\257(\000\205\220\312\355?@\363\342\300s\251\247?M\265j\373\032\232\351?d\032\021+J\356\322?p\326\027\240\315\227\324?\200%\016\025>\362\324?\037\331\'\370V\035\342?\244E;\212\250r\353?\200\331\233kEW\266?\222?/{6\277\333?\n\234m\207Q\212\327?*\232\2539\027\345\355?\224L\346!`\345\350?\307\233\261\027\273u\340?\254`!\234\034*\302?\272\370_X\212}\343?\021\003SU}\251\340?\240\216\245\201\311<\256?\373\010[/\374\345\342?\373\3559\230\201\371\357?\243F\030\203\306\275\342?\256\022\334\346\317x\332?\343\3531\327\343$\352?T\315w\177\261y\347?\220y\363\"\233I\320?\202\014X)d\231\344?\240\374En\210\264\237?\343\313\316\347v\270\354?!\354\003\030at\344?d3\214\030\016\274\305?\n\217\327\315\333\010\352?\374\240`\300JX\304?\323\342U\324\215\274\342?w\010\364\307\340\244\345?\307\274\277\310\272\275\347?\252N\256\312.\035\356?\026\230\253\366\356\323\322?]\034\351\205w\214\346?\236\307\326!\037\360\351?\272\325\376\220\234C\326?X\346a\227\n}\337?\236\372\211\374\204\342\326?\300?\260>\271,\353?\314\301x\313\202\004\300?\034\351\223\224\323\274\307?\3362;r\260z\346?\221j\002| \204\353?\000\200`\317T@\337?\340P\037\021\204\314\230?\032A\365z\001}\356?\355\3045n?I\351?0\t\341\032\000!\267?\304\337\035\022M5\347?\274\210\230\005\262\352\316?d\367\026\005O\013\303?%\014R\220\337P\346?Pn\260\036\n\224\331?\360\367:\177 [\335?\\Yf\037\354\262\312?\244-{\242\025\202\336?\274\2536&\346\002\323?\222\242M\300\302\226\344?\264\262\247\230\260\033\345?n\212\236\301K\235\337?$\236\335\001\022 \301?`\330\272\035\022\372\247?J\2065\001|F\327?\000\245\00330|\335?\253\311r&-\005\350?\302\246\300\323\225\264\347?Z\376\343%0\265\353?S\217\302\231\007\263\342?\25154\000C\327\351?<\353\220`\376?\331?,\335\252\nQ\261\316? \362\304\004\314\207\263?\365U\264\325C4\343?h\224\211\177\250\274\273?\320|\023\316_\335\305?\232\317\007\245\342\207\345?\0241\314\027Y\205\341?4\204a\327\000\272\336?Tw\240\314\271\252\311?\000J\255\363\217\347\311?\336\271y\276\202\263\337?\212\367o\250\2579\342?+\317\262\304f\232\353?\243l\246\3762\343\340?\273\240\242/\347\016\344?0\\\331s\357\304\302?\023{0\226q\376\345?\220!\256\0017\002\274?uY\305\276@t\343?1\3378\024\306\276\345?\020\333\263)\211I\320?\344\257\360\034\025B\302?\244\256Fw\325\370\312?\214fF\334\016>\330?x\251\316\355\2406\337??P\316\234eF\356?.\301\275$\264\251\322?\030\241\226\276<\276\355?\212\n\021@\3327\354?\000\215\307\0338\334\212?Kx\2025/E\342?\314)\327H\222\004\307?A]\001\255\337R\346?\271\315\'\274\201+\357?\340z\370\211\236\214\267?%\240\333\261w\341\357?\220\363\242\206\241\243\342?q\374]\302\310\264\351?\021\035\275\207\311W\345?\004\325\237\362\277\350\355?7\213\203\224f9\346?\346\260\245\212\205\304\353?\3200\363x\235|\262?V}=fn\343\324?\334\341\316\024\216\264\316?\306iT\016\340\273\336?\360ES\263\220\374\273?Bqv\377C\"\350? \302\276G\316~\243?\006(\233:\202\232\322?\326\265\320\211\003\225\353?\010\307\333\253\032\330\347?\354\270\353+\354\243\310?8o>\245\351B\312?\323\033c\017\366\374\356?\330\254\302\037\207\006\271?\240\032\255\025\300\217\265?\310A\247`\211\340\301?\361\210/\377\033\201\343?0\217\263&n\353\252?\330\001\024\005@\020\355?\t\213\303\006%\027\352?\252,E\0316\034\350?`O\223\321\005T\233?\224\250\351\233x6\326?\372\261\006\363\250\363\344?\352\3310\376\020a\351?D\014gI\030\177\331?`\214\022\211\212\303\332?\342\252b\225\207\373\322?&\237\356\255\'\037\341?NPw\223){\326?\330T\331\010N*\350?\020\255q\2264\212\340?f\020G}\333\213\355?\200\350\366\331\273e\304?\002^\226\336\217\034\333?\300\216\267\231\262\226\320?n\343o\340f,\333?\35754\360B\000\343?S\3741\360\342\316\350?\0264@Q\231\234\322?\256\322\354?J\323\345?\314\034\223\211`\314\344?\230\234\016\n\236 \356?\354\"\233j4&\324?\014\002\243\200k\264\356?{%\004\317\365[\356?\010q\224\234\327L\321?@pJ\330\307\231\303?5\335^\265\222\375\354?\264c\205\251\332\200\310?\244gN\212M:\354?T\354\006\227\233\037\353?\230\311\307\364 \335\335?\254\212\343x\322\315\306?Vz\004\234\262a\337?\236T\352\367\243 \327?\314`\023\2259i\347?\360\014ob\006\251\253?\"s\001\266\2534\331?\364\274\315/,>\340?\027\016\037\267\325\242\340?)\332\302H\311\255\345?\276\021\351\246\317h\354?\354\335_v#7\314?\274\375/\326E\265\334?\\n\335\312\373\353\317?(\363\201\035\307x\261?\214\000\374:o\370\334?2?\342\030F\377\324?\324w\331\245\254\374\305?\250\240\331T\324\241\332?\236f\252\257\237\340\330?\345~+\341\220\260\356?\010)m\3240\276\267?\330\314\322\371\211\314\333?\326j\347R\030<\327?w\332\214o\0234\354?\005\225\310\374\334\016\342?\005\345B>\r\306\356?\366\335\240\372T/\327?h\206\341\223\213\236\315?\217[\273D\254j\354?\237\262\344\226\370|\355?~Y\320\3155\265\347?\034\005\266u\375\354\317?\250a\271\220,\"\321?\004\275i\275C\202\304?P\']\307\003@\241?\224#GLw\'\343?\242\220\252\034\311a\344?\305\004\352\351\254e\346?\2403,\352\304d\256?\266P\376)\342\177\356?\300\235\227\000\365\321\235?\360NB\270\354\023\242?\036b\n\010\220t\324?4\245\205Kn4\300?!\026tD\263q\347?\360pS\303\003\260\337?q\3408}\330\\\355?\314\332Zt\377(\336?\022\354|]r\315\330?\2169\230#:z\350?\004\276\226\307\014\023\313?x\"#h&\222\320?\316\2633p\230t\334?\244J\233g\362\355\322?\324b\206\267FY\337?\244*\023\260\267L\341?\006\246m\326\2064\342?K\260\257[&T\343?\300\306\366<\267\224\300?\210\"\215gD)\326?\263~P\016\031`\356?\3202\250\277\213>\271?J\';\251\366\315\321?*_7,\324\037\337?\370H\266W\245\257\315?\260i\037\240\034\357\246?j\235\033\210\355\221\330?tG\377\300\352Z\327?s\361g\027\210\257\344?|\335\032\273\373\317\333?\300S\254Ibg\235?\262W\357\017\266\276\321?\266\200\030V\265\306\322?G\002m\366\316\350\355?P*\375|\030\367\321?\264/\020\347\327*\342?\246\261\264\365*\265\327?PbS\034t@\252?\310\215\367\263\377M\304?<\313\235\331\247\006\312?\362\307h\235\363P\347?\210\334\230\013\373:\345?\206\270\233EQ\216\347?@\220\206\254f\310\303?\324\2675TG\373\333?\205\240\344\230\246\322\344?\3001\350P\310\244\322?z\223\240\235\264\301\331?$:\333\000U\277\352?\214\331\305\243\275\014\322?\365K\305]\0058\347?\345\245\354\271V\204\346?q:=Z\220\375\356?8\032\352\367\371*\305?\305\204c\270A\247\350?\263\342\267\374\017_\356?\212\261\255\351\356\010\334?l\214|\244\023\027\302?`\365\027C\326\310\320?JR\371+\330\274\346?\246o\211\241\002\351\341?j\263l}w\354\321?\025\r\347\0304o\345?\002\263I\237\031n\330?\324\272\336w$T\341?\240\224\314\366\tQ\236?.9\341jP\200\356?@\265\350b\334\354\267?A\263Z\305\002\353\344?8\\\235.\302L\342?\316\010k\"&)\342?6\367!.,\240\342?\356\010R\215G\331\322?N\257?\256\033\232\334?8[\241\303\033\221\355?\002n\224\255\361\371\324?`*\364\343\003\343\337?y-\304V\367\254\345?\200\351\326iZ\031\257?\202f\3619\332\263\325?d\021\276P\375,\327?\006l\220\317-\250\353?\305\231\273\214\204!\341?\310\303\335:~^\321?\277\2008\310\332\357\344?L\240\331.\253\002\316?Rj\036 O\245\347?\204\327H\253\364\"\330?\\?T\330R\324\312?\235\347y\303\346K\354?4\341\334\347\332V\320?\232C}\213\232\177\327?~>\372(nP\357?\0344\177\371>\371\324?OS,6\000\"\345?\200\017:\006\277\216\305?\376\014N\271\027A\325?\251\252P\236\373\005\353?\270\317\233\264\360k\265?,\250\201\203*\370\321?\336z\311\203\224:\334?\340q\372Y\305\230\244?\002/\336j\'[\347?\312\215\352\225\204\342\352?\354\305}\3600\226\326?\306o\354\350\020 \332?\004\335\003\366MT\310?\\m\375\236\352\326\347?\260\211cH\312\264\261?Q\303\027xP\373\344?\022\260}f\nG\340?\347\244\301\360\204\352\346?\301T\034\014\214v\356?\000\320\024\302TYf?\314\361\371\264\362\304\353?\233\265\367\234\371\312\347?\025PR\325}D\342?_\341\007@\354\031\353?\320y\017\254e\274\303?\007\311\250kf\005\345?\357\2779\370\212}\344?x\256\201\363)\271\270?\330\3410\265n\216\351?\t\253\231_\345\226\352?o\204\360\257w\347\355?\n\030do\2330\327?\326_\\U\006\360\325?\256\0059|ut\356?b\033G#k\002\334?\n\001!:\346\252\323?\315\355\341\034>\t\356?\356\261\376\362\016R\351?\016\341\320\002\273\323\352?\\+L8\240\210\341?\204\222Gi!\247\301?\336\327;F6\234\343?\n\364\330dgX\357?\202\270\245\263\320\267\322?\344\334H\006\215h\344?fqD\034[_\320?\270\037\275\016t\372\264?\340ol\022\322\370\272?@\315~U\0001\326?!v!\305@w\356?\374\367\021\361>;\310?$w\257\376\324\201\327?\341\362\352\177\351r\356?mXG\025R\303\341?d!\306By\030\311?`\017\r\310\264\022\314?\245vB\024\327\000\342?\356\365Y\007(\370\321?\352\344C\363\362\364\342?\312\3714\271\233u\356?q\320\0356Yw\344?\266\031\033\336C\331\340?o{\025\215\207\017\347?\340\351C\264R\324\267?\000\006\"\214\037\030X?\210uv[_\262\315?\367QC\036#$\352?r\315\000\033w\210\353?K\241j\221~y\343?\000\365\261\327\253\231\276?PK\340$}U\265?d\267\374:0\204\354?%D#17\255\351?\274|\332\332i\365\300?V\030^$\0130\342?F-\t\312\237\276\346?\036\356\211\r!\232\330?\346\352%\341[\374\326?\256\270\352\326\354\017\334?\374t;\027\3117\317?ra\236\351^\001\345?\2222\004\340\342E\324?\324[WP)\366\322?\364NM\340\034`\325?\230g\337\0252\021\313?\377\024}\244\321\311\340?\264\246\024\3214\362\330?F\2638\2429\371\333?s\337\r\\\005\272\344?\300\314\356\335`\\\320?\306\302\273}\242\321\352?\035pf\3019\211\347?\300%\237\264\210\024\217?l\313\342~\325\207\343?\256C\333\006_!\353?\000\243\376\220R\305\313?\246\003>rq\350\336?\370\321\365\027\314Z\301?\367\024$\305\020F\343?\342\237\341\350\370~\327?\217P\3756\030\252\357?`UAsM]\305?\272^\250\007\277\037\320?u\302}\307\364o\357?\034\230\311\211c,\351?\230\016$\2325\263\323?\034\202p@\267\226\357?\320\322s\304q7\313?\314\334s\261`\340\337?f\362\217\344A,\345?\006F*\200/\"\331?8\251A\r\023\327\320?x\343\273fz\262\302? \013\306\364\305Z\224?\372R\364\352\276\230\330?Gs\361\2516\270\356?\223[\357Obt\354?\352x\000Go1\324?0\tN\215f\327\266?V\200\213s/\250\346?\240\263M\035\226:\274?N{\030\225\\\346\352?\236\213\3269\326\001\335?\304\331\322fj#\342?0\355^\314p`\257?4\021\361\253\306\321\303?\230wk\033\227\226\270?80\250\214\226\301\345?b\256\222\003\3030\325?@7\217~\320\205\355?_`\3424\357z\352?\004e\226\323F\224\334?\200!s\221s\226w?\363\\\266\234\021\236\340?\273{v\363!\323\350?\310X\212\000>\004\321?\334P\267;s\200\300?IK\034\2509\323\351?\326v|\365\225\256\347?\270\036\000\305\000\375\337?\006\345I\347E\365\330?t\344\375L\221\325\336?t\244\240S\013\332\343?$\360\r\372\251N\341?\340(:\303\243\363\302?@a\024\246\203\r\206?\"$\021u\232^\322?\214\024\317\301\353%\356?p\305\334\337IL\317?\000\'=\020\031t\331?\223\302L\375]j\351?`\271\221\326O\033\263?|\304&i-\223\357?\306/\245+\253\305\352?U\r\021\343\001\222\357?\314\321\225\366\204\326\347?\272\241y\365q\263\324?H\265L\352\220\314\335?X\202<\013\200\001\336?(\004\241\030]\255\346?\032@\375\273\240\024\330?\274jj5\3120\310?\326\214h\232\003\337\330?\240\207a~\264\253\353?.\021k\010\307\231\353?\206%(\020\3106\336?\032\300\323\200C\325\325?\220g\301\275\"\352\323?\340\255*$\225h\352?\314\243=SMT\326?\204\345a\3176\276\320?\375i\"a\303\313\345?\224\347\237\312\3533\314?0)\201\312\023\215\273?@\254\333\362\252Z\356?\306u5\315\237\t\344?g?\013o\233x\347?F\236{\353\344L\326?`\302n\204\226\371\326?h\326t?\371p\356?\2307a\307\365|\274?LF\020\337o\210\347?\300mS\036;$\324?\324\266\323\330\315\231\347?!\307\365X\020 \346?7d/\352z6\347?\254\206\252\236\261\234\354?\336}F1l\240\324?p\322+\n\260Y\304? \316\003(\224\204\345?\245\232\006o\311\371\351?\024\013\363\326\263\350\305?\022\323\251B`\322\357?@C\022\341\320q\272?\371\362\314\300\340\013\345?\034\2464\321\212\272\317?\262\327\277H\204\026\333?\221\317\267&p\270\346?\327\022\341:\204\274\345?\325\314\301\350\226N\344?\010\335u\377\222\314\311?\336q\201l\036\010\327?\030\315z\215\032\331\341?f\256\337P\321k\347?\013\224z1:u\341?\000\307_\035\222\312\340?\304%\203\336\262\207\301?2\372%\241N\375\332?\013\034\024\357\204\275\356?0\027\342\3214\222\347?(n\330\375ns\273?!\003\234\323\021x\346?uO\244\207U\302\355?@\302\330\024\377[\312?x\021x\225\342\304\274?8~\240\271\217\034\350?[\226S&\211P\342?^GI\361\257m\330?;\360Q\013$\031\347?\316\354\366 \372*\337?<\203~\336\267:\332?H\333\354\264,C\316?\205\354]zCi\377\340?x\010x\272m\005\263?e\337Fq{(\355?.\346:\341I@\323?\010\277\235\0365\264\262?sjO\210y\331\346?W\337\311\216\013\375\356?8\366\013D8\351\262?\024ds\2179\035\343?\314\237\322;|\320\307?\374HleJ\375\327?\263&\340\032f8\355?\360\022\221\302\017\233\340?\355\311\263\277\262?\354?\364\347\351\320+%\322?\005l8?k\344\346?\037=\2207\367\026\346?E\327\225\343\326\205\346?\nY\004$\034\345\357?\020\r\272\3621\304\334?i\213\001o\311(\342?\004\356\250\221\235t\305?T\373\331\273&\341\332?`p\324\205\256\256\254?\256\257\rY\006+\350?\222\224\005\213G\334?@\226Ou\242\377\337?\024\034rX/\037\301?\250\006\010\363$\210\350?H\343m~\200\320\334?\300\221+\367\021\277\340?\023\020\304\r\251;\357?X\320\304|d\354\274?\370\030y(\321\253\325?\376F\360\205\307\337\353?x\252\312\307\275\257\353?\201\r6&;\361\341?\250\312\026\021\345\363\333?\227U\310\313\245\327\354?\256\361\246\337 K\347?T&\360\221\364\270\337?\300\203\326\016{L\207?\276\327\346\377\010V\355?\306\nd\006I\026\323?\322\345\264\255\340\250\330?\312P\226}\020\236\336?Q\356\n\220\357\366\340?U\3118H\016u\350?\212\3160z}\251\351?&R3\212\026\336\325?\344\300\250\250\273\301\316?\332\334y\306\270\341\320?\240\213d;\317\014\332?|\223YK|\005\321?\237\277(YB\374\357?\242\250\243T\020\273\320?\3443\323\241\026\300\326?5ue\352\366\376\340?\020m~\263)\215\307?@e=~\225(\242?5\226\307\365\203B\345?\216*\367\004\177\354\352?\236\332G+\266\322\355?(0\315\202`p\325?\362\311\007\243\021_\337?F\212\367\201\207\357\352?\360~\315d\266\014\314?t\031W9k\025\325?M\366\3775\347\263\340?X\356D\222\324\034\351?R\033\237\025\242\353\341?(\323\022/\364I\300?U\3670\353\270\362\346?\224\223{\3032\347\346?F\325\222=K\345\321?\222\204\315\345?(\353\217\363\353$\306?\rG\300\240A[\343?\271\371y\032\276\217\342?\324\372\376\236\355G\346?\2448K\022_\367\322?\024\031\212X\245\354\323?X\300\211\313\'\205\310?h\020;[\334=\272?\210\364`X\241\014\263? q\221>I%\323?\370t\035\324\370\205\340?e\254\364\013H,\351?\373\020L!C\031\342?Nm\304\202\270\250\326?\224=^=r\235\304?\236\327k \255.\337?!\325\321\316\0064\345?h#\321\362:}\321?:\001\220\315\262/\345?s\035\201\353\006I\355?\330\003\233^\032l\321?\203\304\2276\000\327\351? .v\t\270\020\331?\000:\202\225*a\311?\006\232\245t\017I\325?\310\275\370*qc\300?\266\"\212\r\035\024\341?`W[\366D\265\273?\220n\364\243\363\314\304?j\360NY\311^\350?\263|\361\203y\263\345?\004\267\241\272gd\353?\010\341\301c\234p\332?\0200\322}\006\334\336?\250y\227\005\372(\350?\332iC\335=\333\347?VS\"\340\357\004\330?\336\321\255\264b`\351?\262n\334L\227B\354?\360\321\272\020\032\324\334?\264.\000\362\277W\343?\372\274m\346HM\323?:-6\341\016\335\325?*\265\022\340y\273\351?\354\2673\322AQ\311?P\256\213!\024\376\315?JS\212\024b\270\356?Y<\222D\263\034\353?\322(\344?Y\245\337?L!\243\232\321U\312?v9(\223\366\204\345?b\013D\311\237\372\354?\316\363\233\206\244W\340?H\364\025>\267\241\324?\\\216V\335\244,\322?\343.KE\033\373\351?\240\215_XWc\347?R\320T\207\320l\334?@F9\022\306\351\252?,\224%\035\026\311\307?8F!\330\347F\277?6X\372\353\237\013\331?w,35\314v\346?\031n\260\3606<\343?\334\360\033\001\035%\320?\037#\200\237\206\373\352?\366\272\241\247\334l\327?:B\245 \314*\320?d\327\007\352\303\371\317?{l\257:5\325\341?\0166s3\307\325\325?W\262\022%\010,\357?\220\354\357\375>>\333?t\346g\346\223\302\346?;\n\221\202Y<\342?T\363\211\264\216x\325?\013h\302\220TE\347?X\262|\2124\355\340?\330\345\324\026\257\277\260?\010W\031\344_t\275?{\034(\034\030(\355?x\303^\353L\370\347?\030ViK\333F\327?\340\263\211\311\346\247\241?7\004\314\013\"y\344?\210\201\321U\245/\347?\327\376`\315\225\202\342?\360\314\224\274\357\021\272?\365\351\213\035\213\233\354?\'\365\2757\003\365\347?\320\376\353\244\036\267\326?jR\274\375\"\251\321?o\210-\313\345\034\350?\034\202\267\266\204\371\326?\010\267t\2053\200\265?`\320\r\341\005\t\263?C\003#\017\333\271\346?F\006\252\034\257Q\332?\253%\263\'!;\346?G\277R;L\221\356?\370YZ\354\177\355\270?\352\332\271\020M\247\321?A?\252\253}\252\350?\274D {\347W\342?X|\231\241\345\326\333?\247\310!\331\365Z\341?\200\252NH6\357\217?H3\367\245\004\330\266?[\\[\307\355\'\352?(\353\360\203\316=\275?d\376\206\013\236\331\332?n\216\020\330u\250\342?Z\246uS\244\027\340?\355\221{$|\223\353?k?IBu\205\350?\352\367]\232\355\333\327?\016|\260\251\350\336\347?X\013%\274A*\264?\224H??\314\260\332?\274o\211\301\313\354\320?\232L\2374\035\341\333?\240Q\302\210!2\235?\372\302\265\304s.\335?\2600\270\240T\225\305?\320:\335\365\372\016\263?d\244N\253\210\207\310?P\306\207$\244\312\337?\270e\250}\226\210\354? c\307\n,:\301?\212\364\314~>\266\344?\327\253\035\227)\322\353?!L)\016\177\307\350?\356F!h\207b\325?\002\360g%\332\365\353?\240\254\224t\324\230\266?\000\\\3068\352\347K?\253\234R\016q\307\343?t\310\244\233\247\307\334?XS\276\254\320O\302?\200\300\353K\2569\353?\020\021)\206i\206\253?\2507\\wd\303\320?6\241\030o\364\236\325?\034/&gZ\354\336?;kW\216\322N\353?\224C\265;\312L\323?\n\243\333\2722\\\353?\200\274]\200\255\347\202?\344\277\025F\020b\352?\"\334\024DY\267\346?\204\354fya/\303?\360\245\242\3640\225\324?\245&\2155\'O\347?Z\341\r\0348\330\336?^GT\003\033\274\337?v\3763\301Ql\334?t\316\231h7\234\302?\300gJ\210\376p\336?\200:M\332\026\020\227?\302\242\027\214\246\010\323?\2604\325\244L\371\346?L\003\260\327(\302\314?\232\311\357\013gR\320?\342\035\337\316V\305\331?\230|\233\326\0027\350?I=\026\2324\010\352?H\360\210;\247\222\274?\200\213\245\211\262\375\233?\212\035w\207HA\335?\302u\301!W\257\325?~\310Q\002`=\321?\224r??\024/\330?\231\375\305\212h\321\350?\230]E\0327\023\355?PX\261Keb\240?f\006r\361\353\254\335?\200\265\222<\n\t\355?\244\374\301\010\306\010\343?\316\365r\274(\016\322?\0206\025\340\323\013\275?\376\037\327e\312\357\322?Z\302S\376A{\332?\030\025\275\270p\342\317?\202A|c\0173\357?\232\247\333\350^\247\326?&\275\225\343\300\003\347?\301\246\260D\365\342\341?&\202BB\261\373\323?|RX\203bD\316?Q\363\340\302s\233\357?@\006\021\235|R\272?\264\325+\315\204\336\316?0t\024\202\2042\252?M\252\307%`s\350?\214\335q}\2605\321?\367\377\231{.\251\353?\310i\232\"\377\217\341?\365e\224\214Z\254\351?\344\315\264HP\312\356?\000.{Gm\014s?\024%;\345\327z\316?\024\331\026/67\354?\355\206\245i\r`\343?\306\233\350\016\tK\337?\330\023O\362\371\030\304?@.\335\333\353\002\347?\362\264,=\312?\335?\326\276\221\0015$\343?\370\363\333\347`D\321?2\000XkP\300\331?\002Ty\305\021\314\345?\n]7f\334X\341?^\r3\267\332\262\346?(,\335\274\344\227\344?\336\010\263\223\265\203\337?2\312YR\244\016\324?mY\253@\264d\356?D\253\356K\343\345\344?\223\'/2;\326\345?\254\000\214U?\273\302?\004\333\215\006\364|\346?\036\337\326\367X\336\335?\014\244\221\2612\221\342?\000C\204\373\002~\221?\002\375\337\006\374\236\336?\036\342\272\330E>\356?D\204\251q\004\217\306?/\342\025\263\254\022\351?\304h\373\365X\221\352?\342\t\365\315\334\017\347?\274DQc\026\223\352?\313A\337\264\337P\356?\356\353;\202\177\220\353?[\232\262\315{\272\354?\274B}\355\026\224\334?\302C\374\003>\234\342?\321\203]\230F\315\345?\251\212\367\255\221\250\340?MoxoR0\346?\337\354\247\220\320\201\341?\352\027\226\000\216D\355?\320\375Y\306\271i\244?B%\362\272-\251\343?x\367Zj\030\235\353?\2263\352h\331\342\323?\360\025Z\2757P\240?\314kK\304\245\342\351?\'\264H\277 \234\340?Y\330VD\031i\350?\177\364\327\365\361\034\342?\246\006\004\232\215\273\345??cd\377WY\341?T%,\022Y5\337?!\032\265\000\257n\356?0\336\326\370a\020\243?pH?\227dh\323?\270\331\320u-\323\315?\222\316K\222W\\\357?O\023\336Z\\\315\355?\257\315\221]\246\224\353?\363\324X6\207\232\353?\327,\177 \351\036\340?\200\021\210\363V*v?\274\233\356\r\310\316\343?\245jT\035Y\333\354?\032I\\w\254\230\342?-j\311?x\275\345?\3477qu<\221\356?\354_\3171+\231\306?\016-\371\305\230\355\335?7.J#\330\344\351?w\224q\023\357\035\351?jwc\255\350\217\345?x\035\221\212|\363\332?\353\362\350\213\257j\344?B\013\322\357\025.\350?(R\377)\277?\301?$\257\347\350\000U\345?8q\331\006zs\354?\214B\005BS-\343?O\235\276\302c*\354?\250\005\234\270\023\240\345?P\340N\013\377\210\314?V\223[\364\005$\320?r\377\031)Al\330?\374w\230\027\352\244\336?\306.\231\325\267o\354?`~\213~\301{\232?\316\225\000$\206\353\334?h~G;r\277\303?\254\357K\2148\366\344?\034+\357\213T\300\305?@\007!\3730\264\313?\031\333\314f\365\210\354?\364\272\237\2444\276\320?{\305\304eG\332\353?R#\336?\230\277\357?\010x]\332\350\031\277?d\231`Pa\371\357?\266l\265\325uO\342?\350\2503\356iw\303?\270\244Y\n\004\227\317?\324D\364:\226\255\322?kp\327]h\341\357?\322\274\252X\007#\324?\231+\330_z{\353?\\H\004\3159A\313?\304\301\371Xls\357?\207\244\257}\230\305\354?\312l\314\311\352\244\341?\375\242\260\r\327\257\341?\312\245\3453a\330\354?\230\226\247q\025g\315?\342(;\214C\263\350?\365\314\235t\036\236\353?\226Xx*gw\350?\372\3414tf%\340?\323\r\223\324\005x\355?0:\204\003\337\243\317?P\200\'\260hU\274?\234\t46\210\233\352?\226\261tk\221\206\331?\316\341!O\211\017\354?P\201\010\304\211U\273?\014\005C\\\377\000\340?`\267F\350\236\200\307?\266\337\337\206\336\203\325?\214\014\370J\256\033\305?\024@\\D\375\002\341?\226\343\367g\272q\335?8\225\371\372\037\204\350?o\217Y\343\253\014\353?\320\245\362l\"\306\301?x\030\267\376i\t\307?\014\362\376\375\332@\315?\270z\357\314x{\330?\242?[l:9\341?l\332@\326\361\377\307?\210\375J\312\252#\347?\n\311\3646nq\345?\364\345\251\220f\226\352?\270\312\377Fw3\276?\344\354\233\324n\206\306?PQ\200B\026\342\315?\010\251\266o\315k\316?\204A\31076\212\301?NYV\342\226\273\343?X9r/\227f\272?\003\237\265\246\030\277\344?\236+\023\016\321\314\334?\360d\034%\345\263\325?\247\370*\271\233u\342?\374o\023H\244T\317?\263a_\342\375\273\340?N\210\255\222\000\364\356?\252Z%wu$\321?\342\2538\327\340\200\351?(\t\244l=b\344?\244\247\371\267\310\230\327?~\271\376y\236\215\335?\000O\242Zo\242\251?\313*me\346\024\340? (\302\205\207|\240?k\257\357\365\261/\345?\212\237\361BF\370\345?\260k\213\373\211\004\337?\314\022d\246\200\030\347?&\205[_\363\250\345?\265\354\243\307o\232\344?\340\007\030?\3344\225?t0\230\032R^\346?\010\202\241\220\345\262\347?\"\'\177\344\020\035\354?\334\251 \334X\214\306?\334\310\336\206Z]\321?S\366\345\340k~\343?\340\002X\010o\257\263?p\216\271\014y\270\333?)\354WJ\257\277\341?\362\211/\303\326U\320? ]v\000\005\237\304?>Q2\006\016\017\342?\200\377{\nw\362\277?\230\251\341\312\227z\317?\366\2403\302\237z\350?{\300\201)/N\343?>\202\202\220\201\252\325?\356\334\376T.\373\345?|\327\330\024\251\342\336?\212\340\310\315=\227\322?\250\001\213\327\362\212\332?\002\215\337\326\216\327\340?\270\371\277p\234\270\357?\246\345\350\n\2441\322?\334;\305/\241\023\340?\036bd\361\030\245\353?\360nH\200z\315\344?\270\244p@0P\277?H\333\331\234\212\274\357?}\353\214\355!\346\357?\314>\330\324\3776\330?\305G\250_/\335\340?n\353\226a\211W\333?\210*\213\036\205\370\343?p\212B}\355\350\346?\2055\277\374\3269\353?\224$\260\007\030\000\321?\257\3240\314\235\336\340?\000{\364\\\031K\301?\310;\246\231hq\350?\314\373\256\374\2039\347?\361/\023\366\332\374\350?\014W\021\214_\220\314?\370]\0067@\352\272?\260\264\210\356\353v\342?\304\037+\210Bd\303?\350\n\223]9k\340?\324\210$O*\241\322?\000\255\246\243\323\311\211?\360\327\036\372\223\267\324?\240\266@W\254t\251?<\2271\242\204\366\322?20\334\020E!\336?\2070|?\301c\353?\345\353m\006\'\321\354?8O\275\340\221\346\272?\000\342\007]\313m\345?`C\373\265\200h\227? \231P \342q\321?\022\031l\222\037\307\353?\214\177\332\343\240 \311?\266\037\216\233\370\304\326?\022\324\031B\303z\327?\354\302\343R\357?\243D\335\354\303\245\355?&%\234\232\204\276\341?0\035\r\242m\356\274?\004\256\253\025aQ\352?(\307\021\205q+\316?>\251\024>\250q\350?\340\023\010o\261>\233?\332e\351Q-\001\346?\000\t\235\014\310\241\215?\2369\231\307\311\337\334?\027a\0277\236\235\346?\3256\345\260$\037\356?D\360T$\007\233\311?\330\t\004\006\031\301\261?\022\377\2162\360x\352?\320:\355\017\361\323\311?\324\r\266\210\r\222\344?\242GQ%\244\035\336?\225\237\370\256\025?\343?4\370\017\205\324\265\303?\273\276\037E/\001\355?~v/\033\202@\320?\310=#\363\274\321\260?P\204\214\301cZ\323?\255s\201;C\r\340?\206!*\226U\372\343?Dq\035\221\361\214\337?h\325\331]\240i\355?\366\215\367Qpu\350?\314KF\340\272=\321?\'\300\343m17\342?\000rg\034\313;\235?\\\036\316\n\337\201\345?\340\\\203(\030\230\277?`\"3H\372\260\270?\312\211\363|\312\t\336?\213\344aZJH\357?\204c\351\362@\021\342?j\211~\266\000\002\322?x\323\026\326\253\336\322?\3419\342{W\277\341?A>\337\263\365\250\353?\t\255\261D\335\307\347?\255\337S\272TN\353?`\346\010A\236,\332?nV\'I0\016\353?\377S\322\300\034\322\346?\264\304\371\304\344\316\353?M=\330@\t\"\355?\326p\254\215\312t\350?\203d\346\230:\330\352?\202\212\254*\252\222\353?\265\227\373\374\017\260\350?&cX\014;3\334?\300\356\371\010\"V\330?\210\211%\275\357p\330?\303\351\311\227!\344\351?\354\224\300^\333\221\300? \311pE%}\310?Tm\326\262\013\340\301?\254\274G\026C@\326?<\nK\302x\211\331?0W\363\256c\032\254?\204\020\000\013\350\342\336?\365\005o\316\207\247\343?\0203h)\002\265\304?\310\335\032\344\224\311\341?\241\275>\345\233A\355?$\327\215\254\030\314\356?@\021Z* \307\264?x\201\372\342W\347\340?\350X\212k\3324\321?\024MA4:c\300?\264NwQ8\025\316?\333\034\024\337\203\330\352?\276\252\316\360\177e\333?\256:\206\036\310\321\353?\256n\277\203\254\223\340?\220\315a\342\301\254\332?b\001\351\212\226\241\324?\340l\342\034\307\334\331?htf\300.\026\263?\334&\247\021X\315\350?\274)jf\314\344\306?u\031\035\250\262n\342?\225\355\275\007\245\363\341?\245\363\230\tG\271\355?\360\265\324\344u\326\355?\254:u\"\377\201\344?%\026\326\232\024v\355?\224\251X7?C\341?(7\362\214\004\376\273?dG\317\320\315\256\303?\215\311>\013\222\014\356?\n\333\312\352#\255\354?\337O\2728\353@\354?<\030T\373vT\336?\346\307\001\273\022\022\336?\210\224\'\256WA\304?s\271W\300\254\257\347?\350\376\2265\2251\310?\220\304\026\2102\304\254?$\231\366+\304r\312?\265z\230*\373)\353?V\221O\014\263\266\325?\200ew\373\340\360\245?\315\220A9\202\307\353?\240\367\2425\252~\245?\320\204XL\007\336\345?\316Y\r=\266\254\347?\271\006\375L\177\201\347?}5\334\301i\244\347?\3373LI\010[\350?3\242,\330o\326\347?\236\021\306\271\001\005\335?\242k9\351\231U\335?\260\335\223\245\232\235\304?f\"\345\020HM\326?\234\303\035\206}\003\310?\360J\324\364\236\004\247?\222\360\376\001\230 \321?\324B\t\010b\312\341?\270wf\017\311\004\333?%\2521\364\270`\347?\204$P\374\267/\311?\370t1Z\363\023\263?\002\006\"\006\316\351\350?^2\2042\272\177\322?R\306\220n9\237\330?\245\004\366\234p\301\355?PU\341\355\323\213\312?\004h\322p\020\323\326?\000\014k\0213\310\324?:^\360#\235\013\330?u?\234\210m\005\353?\307\266\361\036\364\001\344?(\233rj!f\316?\252\033\247\262\325\202\345?\344J\222x\250\254\346?\345\235\344\001\352\343\356?\250\024\376\261Z-\340?\036\200r\273c\210\342?\270\'3\231\365x\273?\000\306E1\r\247\352?\300\304\032\265\305\203\337?\001\377L\2551e\357?\330d\231\037+3\306?\\\370\013\371\321\025\313?PRxez\013\260?\351\356{\363\3050\343?b2T\007\217\307\356?Dc\355I\260r\310?\372\026\2013x4\331?\300\300ED\247\356\253?\\\007\364J\022\n\311?u\255n\266\2310\352?\216\017>\366g]\333?\350\034\317\321\321\026\301?\030\016\254&\200t\342?\334\315E5\335\271\334?f\363@FM_\323?\010\353\302O+\261\275?\255\215Z|)\306\346?<\211lrE\206\336?\3676I3\033@\343?\252u\233N\325\214\333?\256V\317\307\016\341\352?\017y\340F\332\024\347?\355\235?\311\326\\\353?P>\213^\217\353\314?0\340\353T\273\225\353?\244\312\212I\300\\\343?\377\272\366b\375-\352?7\241YC\037q\344?\360\014\226\353%\t\251?\226D\274\036x\246\341?5\024{\237@\202\346?P-\341\005\262\327\345?\264\360/EG\033\310?\220\352!\3410\270\274?,\222\214\004\275\353\331?t`\241l\271\252\335?8 \2726\260l\334?\330\300\271\322,\354\344?Xf\351h\326*\336?\304\022f\253\342\306\350?\020\215\227\035\235\321\336?\351\331\006\235\023(\354?+\237\221\320M\000\343?\250\300\266J\356\254\337?\250\007\235\177\331(\272?\264\016\211\307\027I\312?r\243\233@\255/\321?K\007\320\315J\322\347?\237\235[!\022B\350?\362\343*(\240\376\354?\014Udz\321?\315?*\316\316\213\215\207\340?P\016\003\346tU\333?y!\246\256g\214\347?r\371\224\305!T\341?De\230\207\'e\301?\230a\245\no\377\357?_}.\236WJ\344?f\366h\200\2376\337?\217\337%\013\261f\356?\014\360\212\274&\222\353?\326\017\n\245%y\332?$\346\034\022\376Y\302?\300T\336\363\351C\247?a\313\035\036\025#\355?\211\014\371:\344j\347?\210\257\2254\0042\324?\372\217\234\177x/\346?\334#\261\271-l\330?\200\014#\267k\256\332?8\270\'\234\371\025\320?N\331q\252Mi\351?T\342&Yk\n\306?Q\027\245\n\233\264\353?\254\\\200\333\342<\300?\231\014\300\274\355+\350?\200\303\355U6&v?\222\227K\311\014\314\341?\262\260\246\016Sg\345?\030A\206\320\205Q\303?\340c\312\375\341^\300?\200\217\350\231\"l\346?\032\364\300Zm\362\321?\000\271U\300\340\007s?\204\231\203\277Va\313?\304G\2324\307\362\357?\004d\276\212\256\306\315?\350d:\021\033e\357?N\277\255\2261C\322?T\221Y\360\257\025\314?\372q\311\352\252\336\345?j\"O{9\230\343?\240\225\035a\341\253\253?\035\247\336\232(\364\340?\300$\277|?%\256?o\0332\234\006U\350?\204=\246L\250\332\304?\245\2609\347\342\304\343?\264,_y\016~\320?\224\n\241[\001\227\306?\310\331\246*\2340\273?\230\031\037]\037\314\356?d\314h\253\335\265\326?J\206,\212\216\377\326?X\025\347\272F\020\274?\010\201\313W\007e\356?\345\201\236\226\266U\350?\210\206\224\230\201\273\317?\201\237\302\207\363J\342?%\037\262\013{\362\355?\225\206\323AD\367\347?\014\370\331@\347\220\355?\007\375z8\260\'\345?\2101\214L5\"\325?\034_.\355\343\307\354? d\234\275\2547\307?\305\322\312-\016`\345?\3015\314\203\241\016\342?7\350Ke\237\263\346?\340\317\372U{\316\342?[\360\253T\214\006\347?\362b\363\350\215\324\352?\260\331?\t\323\345\342?\000\026v\345\\\263^?W\372\372\236\375\250\352?F}\0272\306I\335?T\335\244\021]\347\317?\240D\227\312v\266\226?\322\343\311\324\331\024\326?w,\261]\227\016\345?\000MP\177\220\r\357?\000\2272\343\020_\356?6\306\233\023\231\230\346?\201\257Q\271r\231\342?\225*y.\203\227\353?~\263X\037Dn\325?\223-\213\354\240>\347?%,uj\373\321\357?\310\271\003\224\223\005\324?O\335\261\003\330\351\352?Pe\263\330\335\320\353?#{\276a\365\236\343?\000\205\217i{jx?\020\310\361J[E\247?\02513\262\'h\355?\016\357\n\245v\234\336?\302\264\2660\302\315\344?\224\344\010\340\310R\340?\230\021\264\232\217\007\354?@T\220\265\311O\337?r>C\243ln\327?z]\007\322;\266\342?y\245?\343\2610\354?\247S\033\344\364=\340?\272]\3374\031\357\330?s\300?/\271\350\3719w\356?\203\200\372\354j.\353?;%\316\236\346W\357?\256$\016\022\374\356\350?T>\331\276\325\026\346?\n\007\367P\370\354\336?\020\354\265B\375\005\322?\303\371\202\231\361\366\356?/\241%\227\333\325\345?0C\236\244\204$\274?\236\246`PV\373\327?C\3605\351\214`\346?\304\007\314\354\310V\342?\356^\265\036\301|\355?(\245\017\025\324\323\262?\272\221\335z\002\333\347?\274-iBq\014\353?\270b\336\235\252\370\344?H\001\243*\320\004\323?Z\3511I\315C\322?4W;\347\336T\337?\222\303\005\007\313 \347?|\234i\240Y\267\356?\\\271\034\331\375\262\342?\264\000\237\362\263\253\347?\264\200h\331>t\333?;\266\213\244\274\315\352?\022\244\001\346\356|\326?n\215\000\260\356!\343?\376\307C\241o\254\326?\006i\336\225w\017\320?\364\226)}\217\300\302?r\210t\271?\023\320?(\270\331\260\231&\340?\330\205\204\345\3217\300?4\207\365w\026\237\343?\340\277\2322:W\325?>\304!e\264\224\322?\224\223\367\267\026\316\327?\004|\244\303\251\030\316?*N|+i\371\335?P\217\r\2312&\262?\034\2639\324\030\243\347? \314\251&\033g\310?\004~\221\222\327@\350?\310C\235\266\365\376\266?*t3\000\307f\336?\215\357\027aD\333\353?\216E\007{\235\230\340?8\333\234\264\316C\267?6\355\007\236\203\030\322? ?\370Q\340\233\324?:m\005E\326\014\334?\375\266D-\247:\357?\030\365Y\006\010p\300?\0364\206\330)\025\356?R^J\025\"\237\352?\266!\251\020\"\274\324?\230s\314l\272\300\331?\024\225\312\2668J\333?\223\330\0043\021\242\351?s\263\247\021\351\020\350?>\320\244\377\344\376\322?3\377\232\314h\016\357?\321C\251\241x\212\350?P}\202P\203:\315?F\026{\230\337\345\353?>[\213\013\'8\357?\316\226P\267<\007\352?8F\020\327\213{\317?\250i0\277\224\333\264?\336\261\270\350\375\345\321?X\363\207\320\014\013\350?\253MKW|\310\340?`SQ\276Y\316\305?\343\237^E9\243\340?\024\322\237\035%a\335?0\002\217\276\367\354\254?\270\2749\317\373\236\265?\223P`\314\234\320\353?,u\006\004\026F\327?\350&:\313\237n\305?\211q\360)\236\333\344?\240#\022\303fp\311?w\312b\222\2572\347?\370\276\276\272\t\021\302?\324\276\270E\030\376\343?\362\005\354r=I\350?\222\2741\222\315\253\351?\340\364\014s\274\250\265?\340\362\354k\033\221\302?(:\201\272gl\300?\361\262\335)\324\354\355?\030\364\314\204\336\325\323?)\030\266\207d\212\354?Y*\316\373\375\253\357?\244\232\351\231\327E\307?~\323\373\177S\202\321?\300\226K,\374\334\357?S\246\226\"\342P\341?\376\212\334x\324\207\356?i2\337\322;\334\350?\234lluD\356\325?\200\n\342C\365w\246?\003\251\303_+%\350?\210\370\203\323\252k\330?`e\247,ik\257?\265\342\347\261O_\344?H\007\222\007\263\316\323?\335\257\035LQQ\347?\004z\'8\360\033\324?\020[\307Wl3\317?\364\301b\352bt\315?\314\336;\030hZ\355?\220\234\032\376;\002\327?\3447\301\220\002\244\350?V\316\324\327pE\347?^4\371\300\001~\335?\3109\016\235S\274\331?\260\016\024r\213\264\306?4\210\301\366;h\340?lh\220\2259\273\310?\\\022\345\314\230\373\345?\330\004\234\357\236l\275?n\336\327\231X\267\336?\005{\3113*@\356?\324\007vql\352\304?\252b\237\034\301A\334?#:\227\266\":\345?\256\312$r\251\364\345?\260\306\324&I\235\312?\304\315\014\333K\265\352?\260\240\331\335\005N\254?\270I-\362X&\331?\275\257\023\303^\036\353?\035a\r\365S\364\353?\250\202\272\314\207c\335?D\376n4+6\324?\350G\0217z\375\264?F(\344^a\277\327?q0\255+z\205\351?\314\371\357\262\037\364\335?\200\007\341\222\227\327\222?\376Z\233l\020\273\352?\324\006\245\\\240\005\317?\367u\014\030_l\342?\257\324\314\341\377\312\341?\r\207c2\305\326\357?\203\203\024\030\271\372\355?Hyk5\333\017\267?erf\221fv\354?_30P\306\225\347?\360\231o\235\t\337\332?\234\337\354\255V\006\347?H\354k\315\375\227\265?\034x\226r\005\346\337?r\356\252\240\263\313\354?\374\321\014\230-\275\327?pp\260\365\024\370\341?\000\'\377\230\232/n?\034R\207\024LD\333?\356c\227\360e\022\357?G\361]\201*e\354?\000\'\031\220\347\373\272?XI\323X\004\001\306?\331\261X\032 \362\355?\007\320\365\3052Q\340?nB\303\256jz\344?\246\034)\225k\242\346?\030\340u\306\037\000\322?0\003\326\316\005\260\315?\375gP\303k\243\351?V\375\267\254\256\307\352?\000u\345K\255\355\270?4\202\216\307\365\036\306?\220\356\321\312\3768\301?\201\310~Y\257V\355?\340}`\275\206\177\247?\334\225\316<_\234\346?Hk\270H\224\030\311?\014BY\302I\302\315?\022\274\314\366Y\250\334?\233=\005V\232i\356?\257\307\027w\261\\\356?\030\032^\\\261<\277?D,\321I\333\305\303?\216\016\235\354C\306\331?\256lfz%\205\331?a\315\230\274/\033\346?[4\222\231Oy\343?H8\324\017\254\330\311?\260\257\371L\325\265\313?^U\325!\356e\354?\362NK0\035\001\346?\250\377>Xz\200\300?\000\023goE\264\260?\026\357h\265\013q\353?X<\306\376L\370\306?i\331eI)\036\342?\307\262C\304\006_\341?\200n\260\024\263\233\344?\030~\200\021\212\303\326?\002HX\362k\365\350?\322@\334\004/\366\345?U/\314\t\205\344\357?0@<|\207\243\344?P\265\016\\^\324\322?\340l\366\351\323a\226?\316\372SP\272\000\343?N\257\373\304\255\316\325?\352\251-\006Q\200\351?_\235\362\306!\317\340?\331\374\275\035/\210\356?<\202=\034\007p\303?,\036\027\354}\275\322?@3\241\014\221\334\275?\343\240{F\355\256\350?\260\231(W\031\320\321?Wo\'l\353\021\357?\224P\256\267t\324\301?\202\207\222%\036\001\336?\337\371\224\202\356\206\344?\242\324~M\314\355\325?\264u@\260\326\253\332?P\264\337\305\242\325\242?ZFN\377\205\013\356?\030\221X,\341\005\264?\305\350\360D\312F\342?\177\342P\262\213n\345?\364\002\031\033\200\242\352?\220:\271 g\202\333?#x\027\232\367\242\340?\276\233\352\037\343\302\356? KV\305>w\336?\250C\000\301do\271?\255\202\306[\302\261\340?%\374[dc*\356?\324\327\312\'\037\301\355?p\231\367\324A\314\322?\316\216\271%I\027\351?V$\276\374pW\337?(\275\342\364\230\365\316?3N\335L\016\347\352?z\342\365\363\353\323\336?D\352\032`\016\000\314?\350F\331\276\266|\306?\361\205Y\217\333\331\345?\200ypH*\344\253?\334\031\276/\243\304\312?\223\277\355\212\002\006\350?\000\250\263\337\246A\335?R\n~\316m#\322?PJ\250\227\0007\324?\030\006@\347\246h\320?\022z7\206\314\343\337?\r\374M\327,\224\355?e*\236\341\311\217\354?\270W\366\363\'J\271?&\340\2523\206\245\354?\344~\211\313\014x\306?}{h\313\030\271\350?&\363\344\226\3604\350?\232L7\377G\353\356??\324S\304\035\205\345?! z\333\202\345\347?Dx\332\016W\321\325?\234\264V\004\216N\304?X^l\253^\207\277?p\234\311\305.w\241?\202\312qw\334\237\345?\244\234 \337\n\257\336?\376\311\222\356!d\337?\234Q\036E\221\244\306?,j\373\203\247\261\312?\300it\2006\333\256?0\251\'W\313\300\302?\200\027\025\033\200\370\214?<]{|]\267\325?\371z\227\236\246\017\356?\360e\310\351\to\272?=\325c\347\261\360\340?\214\3258\244\337Q\324?\0334\244/\010\203\354?\301\211W\320\275\010\353?\240\301\010\035Q^\233?\320g\265\360G\022\320?\3140C|\200n\326?\274147\037L\311?\323%\227\"8\240\354?0\267\276\365\375\324\354?\205\367\242m\001\320\350?\212\346W}\000+\324?>\373\021\240\316\231\352?c1\276dQU\351?\300\213k\240B=\337?x\212\222)\234\314\357?\307\202bA\344\203\346?\252d\222 \303\243\343?@b\241\010\326\033\316?\303=#\233W\350\344?#\222\314\275\037\t\352?\374\211F\311&x\314?\006\363\205w\262\216\327?\001^\247\232=\016\354?\315\365OB_(\347?\311\177)\202\377\366\346?.\236\311D\010*\353?\304\026\223\304\007\032\326?\210\nI\361w\236\273?\330\307\264\347\330\217\344?R5\360?\032\203\331?r\307\202^\347\026\333?\274#\'\222\243\036\343?\\\266lG\262\271\333?\261Y\273\017\346q\345?\340\264\315J\td\326?\357\257\357\313\246\246\357?\340\010\003\013\212\246\262?\302\261;xR{\342?\260\311\213\323x\214\350?lM\002\\{\007\326?d\034\360\354\312\353\336?#{\267&\274]\343?\213\247\207/r\026\353?\330W^\311\2365\346?R\271Y\016\267\272\354?\224\023H-\234\013\350?P\305[\357kY\255?\255\216j\177\007g\357?\366\025\006\350\230\300\324?\206\021\373Vf2\355?\2670\254\203H\264\353?\024\351\201\202\301\026\333?\200#a\224D\302\200?\200\243\267\276\033\204\271?\370\234\300P\312k\313?\375\207\276M\212\231\341?;T\300)@\206\353?>]\303\247\260o\331?$\361\263W\001\013\323?\"]7\013l\246\350?\350\367\354Z\340@\302?\017\367\306\007]\352\344?\334\316\343\376.:\336?\260\034>iYa\320?U\236\n\017\215\212\347?X0w\246K{\272?8I:\235k\323\345?\245\007\373\226\205w\341?\\iT\361|U\313?.\366&\243\013\353\351?D\r7\236\nQ\334?\313\205,m\342\236\342?iP\361\253\212=\344?^\030\311\022\330\376\340?\030^X\362\244\233\337?\036\256\364en\224\321?\270s\254\t\010\025\324?\005\245\rPbq\356?\347\325)\010\227\260\350?(\235]9\353\344\356?\360o\217QX\361\277?xUz\370Y\303\264?\300\220I\0036:\201?<6ch\277M\311?\'\267\241*\341\325\356?\"1\326\221\201\351\335?\014\277\374\233|\237\316?gH7\251\022@\345?v\007\362\267u\200\353?\236\266\322+\304\225\332?\254\031\267\222w\305\314?\367Y\353<>)\354?\266\002\204hd\013\326?\202;\006t\302D\341?\276o\177\003Ms\352?\020\205%\3668T\305?\270\276\375\351T+\301?\246\343\353\204]\022\350?\n\332\370\200\261\020\337?-\344\263\372\350v\340?\320n\326\352\356p\321?\266\024k\204w\316\352?\337\333\324\013\016\225\344?4\310\342\332OF\321?L\301\035P\\\361\310?0\311P_\360i\256?l\365\205\006\030\303\304?\370\225LL\213\373\272?`+\027^\004A\240?\330\"\265\025\316\254\344?\240\346q\340kp\345?\021s\314;\260\004\350?\344\343/\026:P\346?\234\204\206\310J,\324?+\2400G+\370\354?\341\013\022\235\346X\342?O\201\036\267\373~\355?ba\377|\010\372\352?\344\235\033y\214\246\342?\372\354Y\037\340\005\342?\272\265\313\261\020q\341?x^\235[\266\023\270?\225\356(f\037V\352?\271\370\3516\374J\347?\343\211\267\234\371\323\350?08\260A5Y\303?@3\217\020\355\250\310?5\235\004\204\202U\340?\000\256+\305\277\256\300?\340\234\266\204S\302\270?\321\340\211\251+S\356?)?W]\355\354\342?\312\212\346;\245\355\320?r>\276\201)\366\356?R.!\242 \002\343?o\033\350\261)\346\353?\251\376\201\230I,\344?\212.\000\343\231\371\344?v2\230\244\275\247\322?\351\205V\307Th\347?\306\004Cg\315G\322?\355\227k1\323S\347?H2\246\366[<\356?`g\023\263\233\355\233?X\375\317\"\343\226\353?\020\256\222(\3356\262?\314\017\333\333\243G\331?\030\263\007\355\023\263\340?\221\210;`%\312\355?\303\301\214\177\376\313\355?>?N\360\020\264\333?\010K\006\340\272\340\325?\n\016\205\t\374\235\330?\340\334\335\242L\304\303?\000\327;\346\234C\231?\230\243\365\030\233\000\327?D\027\214\224}\010\307?8\321\330\315\020\352\317?\013X\313\231\240\027\344?dq[\276\267\017\332?\260c\025dzU\315?\200X\336ml\302\261?\373\014\363i\023E\346?\341\'\276\200\214\022\346?\250\210\360\2130-\312?\300\t\277u\340\340\231?\312\034\256\337p\314\357?v\356\375\002\360.\325?\330Z\231\344^\016\337?\270\260I\274\244\321\301?\3401\025i\177\266\305?\214\324\234\301,\035\315?\233\326\024A\251]\344?@[\212\213\022\255\233?\332O\311\000\374\263\353?\'\204(\037+e\342?\350\312S-\033x\302?P\365|T\240\250\311?pF&/\332}\264?\242\242\232\200\327\254\330?\036\243IL\345\357\320?G.ux\304\277\351?9\0169\270\030\252\343?X\302\035\010\261\033\276?\366\344\241\022\333\346\354?\210\263\226<&\372\272?\317/\035\001\001\215\353?\27692w\205F\343?\262#w\023\262\316\347?YN\035\014\323R\355?\306G\240j\225\005\356?p\351{\034\301\375\242?\2209\267\301s\"\257? \213s\301\222n\221?p\274\314\330\275k\353?<\311\231(\276)\342?8\350\375jv\261\300?\265\327c2\221\255\350?\210\374\315\014\226m\301?\334\331s\246\004\364\343?\024\230\300\363\236\236\350?<\025$i\245m\354?\356\335\333S\275\244\346?5]\002j\310\003\356?\364\'\013[\303\306\346?\246\341\021\225\367\212\322?\330\315\265k \262\272?\314\356\276\365\251\275\325?\224kZ{\246b\355?\212/tV\333\n\330?\204\202\212\\4\237\307?\202\301\202u\234\262\332?\020p\341\251!\217\336?\212\010\0368\354\376\336?\365\270\344\331C$\355?\256\250j\263+R\324?bM\030\215\206y\357?`K\"I\314o\352?\342\340\2719\332\025\355?0\310=C\t\210\336?\234a\301\270[\263\303?9\204\230\222\341)\357?\030\224\360\321\375\207\324?(\365]\201p\257\312?pB\332\312\343d\247?\356T\034z\272\345\321?\234\304\217\303gH\332?\360HI-`\362\240?\030S;E\013Q\326?4\355\333&\340s\307?\024\277\247\274\263A\342?@!\205\036%<\357?\336\362\201\235yH\345?\265\252;p3\216\351?\3607(e8\354\256?\000\323T\2770\211e?@\270\222f\027\024\244?\356\034CC\320\316\330?\302\202\223vo\215\355?,mh\326\206J\331?\013|1\300\001W\347?\225!\312\037\365p\346?\333\005\200\024\265q\346?\335T\036;}\204\340?\344\236\307\r\242d\311?\032F\221\366\327\006\337?\016\272\020n\315\354\326?@\333j\013\337\261\275?\212\265\265,\214\n\342?\310\014\001g\324\211\323?\340\321\267N\256\032\312?\004\030\254^d\177\357?\003\333\373_4\326\357?\345I`\r3\351\352?\360\007\024\016~\363\313?\024\331x\312P\262\340?\200\305\034PR\007v?P\340\340\302\320?\335?\370\006\237^\214\256\355?\330\220q\244\304d\326?\220\003\336\022\252G\275?\352d\342\2041B\326?\005_)\217\267\204\353?r\272\270K#\226\324?\275Q\0264\017\022\351?FM\232!\345k\355?\214\202o\205\212f\325?\000mX\211\246\303\261? \361\325\014GD\342?\270\025<\003S\304\346?p\363\236rY\r\300?\"*A\020\t\200\332?6\2254YV\276\327?\340\243\365\257\250\216\331?\252\206\251\236\330\236\341?\310_{+\024\361\344?\363\326\263\014\360\226\343? K~\351\374#\231?\322\344\\\3770J\352?\210w\306\243e4\264?\260\236TT.\202\256?\004\200d\270\373H\335?JEN.\025\237\341?i\2243\342\\\200\351?\200\324 \216$|\274?\326n\327a+\372\346?\200\351\262|\023ts?Z\"\030\032\3057\323?`U(* \\\337?\343\373%\215V\336\345?\220,\223\237g\217\263?\n\227j&\261P\356?\330&\314\202Z&\347?N}\3323i\354?\306\2625J\346\311\351?\331H|#p\346\341?\204\377Ld\335\000\352?\027[L~\312\202\354?\010z:\242\237\220\264?\373\371\001!/&\341?0\332y\322W)\342?\030P\344\273p\253\324?0\237_\202]t\323?\364\353(\216\331 \311?/\362\313\343\210\345\355?>\250`\035\306\010\327?\237\007\332\312\365\336\353?\t\3731\240\224\034\351?3\343,m\007*\353?@Lb\314\233b\260?\302F\247\031L\246\330?\\\332\362\217\035\366\340?\222\007&`\377\374\352?p\276\"y\335\307\260?\372nw\001\275\345\343?\023\3145i\223 \343?`\177\r\325m\025\306?\215\265\220\220h \355?\360\023\341k\004\324\257?\234U\002@\366\204\330?\350j\257\033\010\264\347?\254+)1\014}\351?\230m\237\367\225\270\345?X\341\360=4\t\307?\324\374A\237\004\214\334?\020X\354\177\200h\313?\023\202\210\345F\245\347?i\303:?\362\t\352?\373_Q\342<\253\344?\237\371d!\330\007\357?\014\234\257~\177{\302?\226\267f;\373B\321?\342\305\214\256\002\350\344?\200:3N\033\243\341?\002)\255\310\016\214\334?\223\0259\313\325\324\341?H\024\353\373\333\263\351?\340\230\213D\3246\337?\247\036\301\375q\234\354?\254[[>\362\030\312?\216\250\014\275\206\221\351?\340\232K\302\337\330\253?\240\364$\302\222\222\344?\350u\374LO\256\346?\347\032\027\204\275\030\345?\2203\335\272%\016\253?@q\022&\020-\351?\340\231r\226\212\025\235?\"\217(n\327B\355?\312\267\264Z!\241\335?\260\356\361 \371\314\241?\350\243XX\031\230\343?\266\332_i|j\356?\324B*;\353\341\356?\324\375N\224!\202\343?x\346z\315\014\360\344?4v\370\361\3425\316?\016\206\242[!\r\351?9\353It\241\315\347?\034\246\002\224\214\024\322?\350\204g\351\036\353\275?;\271\355\'\325|\357?CY \327\031\337\356?\310\235\031\276KM\261?}K\203S\331\252\341?\356\255\216\023s1\356?\216\366\032\r\257&\350?\0323\263\265\353\323\325?\324L\000\177\367>\311?\214]D>sJ\302?\0223\330\265\305\311\344?\266&{\324\252\201\335?%\005\026\263\'\373\343?\312\302\222\235\274S\331?\023KY,\317\261\341?sa+\207)w\353?\254\361\370BL\213\352?E4\346D:_\356?E\027\264\027s\274\345?6\231\254Tf\032\352?8\307\341\014\252\004\336?\203\320\334\334\017\272\345?\220\260\342 \210\371\314?-\331\240\314\r\020\347?\266P\017\364gs\353?\030Zn\260\3363\300?\317r\367\210a\001\340?V3\035?w\\\353?\014O\207\005%\331\342?\300C\341\353\276\304\303?^[\370Z^?\321? *\213|\333\334\261?\300\327\235Y\321E\353?\201!\345\021\036\343\347?\227\020\210\014\n\341\345?\244\220\016\242\375\027\314?\204\312j\215?\217\302?\37018T\033\357\307?\247\0323q@\205\356?\260\022\002;bE\334?\200\276\344\334\246\253\304?\314|N\323\206F\342?\222\237T\3075`\357?dv\307\006\201\255\344?8\305\251\3411\254\327?\303\237\350\277?@\357?\217C\202\2312\256\356?\240?)\336\177\222\232?|)_\373F\315\337?\2175^\303\342r\352?~\246\017a\204\246\334?@\273\313\202@|\355?\020\207\262\3706K\321?\004\2377\211\202\353\346?):\033e4\207\352?\330\230i\030\260\007\344?\302W\242\003(O\323?\030S1\334l\037\335?f\360F\354.p\333?\244\200<\241\350\374\345?\004\316L\273\031\326\320?\360z\354??\224\326?L\207\265\234n\350\312?H\211$\234\341_\314?9\270<\267\374\262\350?p}]AFY\317?n\001U&}\244\327?\276\014\'[U&\353?d\213\201*o\237\312?\000(\321\224\352?\213\2778\314vc\357?\342\027\205;\177/\356?p\217\345bK\343\351?\342\022\225\006\236\037\325?\232p\367\0219D\324?@\3340\007\224\037\336?\360\371\023#A\377\312?\222\227~\363D\262\327?@\255\320\243\314<\230?\244\222\013\032\241\022\346?\330b\361\321\177W\340?\006\305v\206\033\264\355?\301\226&\366\361\020\355?\267\"d\341\303\337\351? \312\004\360\rO\264?1]2\n\200)\355?\222:\322\375\020\261\337?H\221\237\261\2737\263?\025)i\324\001!\354?\256\271\253Rk\375\334?\323?\026\260\2261\346?\243\263\226Z\037u\354?0SO\2726\277\252?J\017\214\320\361Q\350?\354K\232\370u\200\332?\230\265\340\264=\330\273?\220\262\273\232\311\374\241?\000\004a J\211\312?\371l\026\2677\273\340?\331\244\n\303\321\000\356?\226\353\257\334\017A\357?Z\367\207&\330\037\320?\350@\t\'h5\336?\2349\377\010O\334\345?\231\000~(\310\214\353?@\211\354\367\346\372\315?R\364P\273\\\014\351?\340\206x\234\321h\352?S\251\355!\307\366\354?\246\217\010\273j\270\342?\364\254\250\271\\X\343?\340t!{0_\340?\214M\366\271\305\301\351?8.\261\r\307\200\321?\226+U\257O>\345?D\202\000W\036d\343?CH\370\010x\271\343?\324V\247\230\225-\310?}^\235\253\024\373\357?\314=`^\355Y\340?\274Za\"\020\001\317?\032p\303\3053\001\330?\"\201*\003E(\330?\"|\326\rhf\322?\242E~o\031O\321?\313\251\311\271\365\030\347?\356\276\307\375\255\275\346?x2\212\271\272\276\314?\034\2672C\307 \321?vT3\372\325{\321?\350m\217S\223b\270?\337\235\036(\2228\355?\300\355\004\327\322\336\333?T\303\030\222\263\"\321?\310\247\241\020^E\276?,6\020\253\247,\301?\331f\0144\363@\341?\222\337\026\0108\225\323?&-\024u:\301\321?$EKdj\273\342?\2604\242\2234\307\304?\200&\030#\216\237\311?p\216g\233/\373\353?\3319\216\014\257\272\347?\211M\276\231\316&\355?\034\230\346}\202\\\341?\001\270\017\241\2441\356?\274/\311g\354t\312?\3344\210\346\315F\336?\226c\331\317\335&\324?\004s\033\232\030\372\346?\274\250\364\005M}\346?\030d\263\246@\236\357?(t\247\372J\326\342?\0345\220\313\212?\343?\236\251\014QFa\346?^\224\272\277\327\375\342?\255\301\345\252Pc\351?\213\365\345\201\237\022\346?$N\232wI\330\350?\240w\323\241\236=\322?LOU\020\337v\343?e\005(\307\301\341\347?\250P\275\037\032&\337?\330\3121\203=\305\343?\211\230\306\353\343\226\342?\300|\314\215,\210\316?\330\027\001\010O\237\313?\340Z\025:\\\262\317?x\2358g\013\232\337?\222\022 13\355?Va=BH2\327?\262\320\363vN\373\330?*\357z3\206!\337?\210?x\343\376\372\312?\000\242\022\246!\037Y?\206\304\201q#O\334?\321\2041xt\\\354?p\360\0071(\252\323? L\333\222\2363\245?h=\347\274UU\324?\004xz\306\342<\344?\335\037^Z\222\367\353?\272G\260\216n\346\344?\246\202\246a\361y\323?\020;Y\351\212\221\306?(D\227\362\216\335\311?\2403\022\213\223\005\223?6\276\247;v9\352?\314&Y]\357`\305?\360F\245\261\034E\242?x\201\034\226\372\310\260?\252\000\t\2039\314\340?\300\333\225\216\246\225\253?\255\277\343\001\371\371\353?\3605[\211\2542\240?\315t~\017\324\275\351?\177\356\332Gq\304\347?U\001Q\215\226\002\357?zO\201\333\240\376\342?\000\343!&\257\237v?\213HU\221\210\253\342?\232N\'\202\371\334\332?X\306\211=\221\005\343?\220>$\016a\236\252?\273h\217\030{\243\342?L\364\260\024\034\307\323?\364*\200\200\340\213\312?_\261\216E\362\251\355?p\3130\370y \304?S\014\273Xo\346\352?\332\234N\033Ho\352?\327@rV:\211\353?\200Z\027)<\207\241?@\365uY\262\016\222?\000\355+\211c\336\307?\014\361\377-O \305?0\312s(\r\344\331?:M\370V<\006\336?J\272\221\326\242\266\343?\004\200TC^\306\352?\3706j\224\241\010\314?7{\237\244\333\252\347?\232:l\270k}\330?\034\036\031;X\362\303?\374i\334i\317c\300?8C7j\265X\330?\334\260\346\312\327\227\316?\214\032\2228\033\334\346?\320\010O\272\246N\317?`\376\357(\260\227\252?||\333\332\241\006\355?\371&y\257\352b\347?\312o\364a6\243\334?\344\316?\252\224[\335?x\321\216\275\337\213\274?\250\312\251\243\014\345\353?\024\207\235\'a\257\304?\2602\353\0315\202\276?\275\217\324\247\343R\351?\206\231\000xi\271\323?I!\225\327\236\350\341?\000\'A\310`\020\337? s\307\377W\322\301?\316\264\016\364+\2276\231\340\330?\001\347?\272\211t\351?\034NT\372\023z\341?2\341\304M\260\016\332?V\033R/D\206\344?\301\233\305\226\347\315\346?\236\303\340\026#\337\356?G\010D\363B\030\347?\\Z\307\"\340\221\322?\024l[\014\212d\305?\330\225`\372u\244\347?\211\010\002\310\\\302\342?\037\272\325j\261\270\352?>n\245\325\221\267\332?\267\224\221)!Y\350?\340\217\007!\016;\305?\016]b3\237\022\331?\264\\\226\"\266\273\301?\242cb\264\2769\333?\350\205\270\327\264N\326?\310\300\033\"GU\307?\270n\235\2737\014\351?\254\017\270.\345B\301?\002\360\036.\320.\327?%\241J\t\023&\353?s]S\324\"3\351?\033\251SS\370\365\343?k\310]\256\245b\355?T\362\211\332X\334\311?\310\314\244\205\350d\275?\356V\276\366\267>\347?s\234\367\354h\261\344?\200\'\3034U\013\227?\225\r\006p\235_\357?\300\340@\312\010U\261?\250\027\264\222\"\350\263?\272\233\nO\246\376\353?\334\325\177\276\231\310\347?Mc\374\023l\001\355?\303\"hdZ\024\346?\244X\355?\037\254\351?\\\254\223\270\374l\307?)\357\357\247\021x\351?w\030\302\022\353\363\355?\211\023\366\273\345\025\346?s\230wM\033\362\346?x\356g\030\"j\343?Z\244\333\370\232\000\320?dO\350\022\037V\304?\264\273\332\"\364?\304?\362\240\321s\364\362\337?f\257\335)3\322\355?\242di\226di\332?4\205%S\236\214\337?C\372j\332 M\344?\010\312\205\364\006\375\337?\353\362\367\206\354x\345?\254X\022~\265-\343?\241\226k\224<\005\346?\031\303)\273\232\005\343?\370y\0047cq\331?\344x\232Y\222\250\350?\201N\321\013\026\223\341?f/wVx\207\355?Xl\265\nI\372\332?\240\006i\037\216J\230?\330\257\235t3:\315?C\021\322\220XD\352?\353F\363\222]\r\356?Px\225\027,e\267?\263\313\270k\326\313\355?\327\310\214\016\240p\347? \370c\223\373-\264?k\023\306\253@\033\357?\006v\2277t\347\340?\220\245\002P\010\207\257?0\032\020\325\245/\323?V\274d\006kx\351?_e\274\027\226F\352?g|\362\253\346h\356?s\354\311\224\370\342\351?\024\340v\305\267\232\356?\022D}\204\317\267\335?RT?5\213\335\353?\225\336\326\231X1\343?\314;\242\222\314\236\340?c\036\264\232\212\303\340?L\242\222\356\364\307\347?\325%X\267e9\346?\200\321\035\362\300\253\345?\314\245\021M\334\232\317?L3+qV\364\355?ZA*\327c\247\344?\364?\007\273F\263\346?\"\026\252\317gm\350?\361.\315\236\230\357\347?\260~\302\207G\233\276?k\3264\220\033\356\356?\357\212*\346&(\352?Lo\316\262\322\344\333?\000\'Ol\236\201t?p\236?\227Lt\243? ]\272\331\340\361\305?\242\\\236\001Q\344\334?(6\r\316\372\256\334?\300\334\376\307R\035\236?\266\324\350\004\021`\331?|&5\203\372\365\310?\372J;k\250\257\344?\222.\200\352n\321\330?\214\004>F\334/\323?\362\213\216\223\333>\355?\316\322)\021d\027\351?\262\267k%\340\036\343?Z[\023\246M\003\357?\366\351+\361:\205\343?\024=R@\343\214\302?\314*V\033_\030\335?\307=D\344>\237\343?D\351\275\177\007x\307?=\314S\334\276!\353?\266\265\300>\002a\342?\264.\200\256&\360\355?\336\264\275\350\365\261\337?X\363\256> \016\334?nL\311u\020\003\336?\200E\357\237`s\217?\325\206\256\372,O\347?P\340ri95\264?\230\217\016K\323\374\302?D\215\026|\331\361\356?\022!\306\013\264>\347?\274\354 \246\3129\347?\242f\352\336\274O\346?\277\003\377D\336S\345?0\003\035KZ\353\322?h-L\271H\'\274?\322\005f\224t4\352?\334\266\031\214\366\327\322?\310\"\255\360\260\031\352?\037?\306\371[\223\355?$0\322\370\210\236\302?8@\320\356\237\013\305?\351\234\277\344\034\016\350?\033\3411\022\220x\354?\376q\355>\356a\351?X3\224\345\322\355\351?\200\353 uhm\324?(\271\354\303\320a\326?t\355\334\227,\301\342?d.\344n\256g\332?\307\017i\3005`\342?d\211\017\243]\016\322?\241S\230\253A)\356?\332l\377\244\n2\324?\202\274\207E\014s\340?\027D\305\024z\021\356?\203`\247\n\322\262\347?\\\313\207\r\363\201\337?\310\224\265\322\002\022\276?0y\306\362\204\356\270?\300H\263\336\314c\341?\360\004\203q\240_\317?\355\207\206`\271\210\344?M\3768I@\003\353?\"w\2160@\241\353?\300v\271\275\364\022\242?(#O\001\031\252\340?\270C\r\325\330\220\331?\357Co\363\207\215\345?\366\266\277\024\210\335\353?.X\033\264i\314\320?T\224\263\177\3634\315?$\272\375\214\235S\340?\024\327\325o\370a\304? C\213m\037\017\230?\034\r\312SA\317\332?\237\030\337\270\342\010\350?\240\362[\274h\021\235?\350=\"\206\003h\276?4\326\300\005\2471\356?\240X\204>p\301\246?\024\326\356\230i3\334?\267\352\245! _\340?*\360?\"\\\n\346?\250\n\356\3266\005\337?\320 \264\324\037\345\334?\277(\205k~\330\352?\235\345\036\234\234\000\353?`\364\322-\250\314\303?\214~\026\314kS\351?\264\215\267\\\013`\350?P\336cU\037@\325?J\177V\254\030\037\352?\200\242K\234\205\236\242?X_\332xg\356\335?\344\337\303\325\313\330\331?\314-88\330\314\312?]0\'W\'1\356?w\266Y\246h\201\341?\370\305\3753\026@\347?\204\302q4\343P\343?\254\303\033\272`\321\324?&f\261c\244\004\321?3\360\037\214\362^\340?g\312\2764\030\350\343?\3518\334\251\363C\341?\376\320\2002\352#\340?\210\211\232\213\331\351\322?\200\241\371}in\220?wN\222\353\234z\355?p\205/^qY\277?\374\245\016\326\357\313\300?\374\332+6gr\322?\334\363/\370\320w\315?\"\272\274\014v\216\334?\010\230\241g\276\313\320?xY\221\326\311J\346? \202\017!\227\227\263?\364}Cc\300\276\336?\334l\2327\2617\324?\013>\212\363v]\343?&\r\017j\301\275\344?\354Ev\014\331\337\302?k\225\221\242 \t\347?\230`\371\351\'\316\263?W\346\302\232\325\233\341?\360\235\272x\256\300\260?\224\274\006\303w0\331?4\237\216E\252\003\343?`E\353\230}\177\346?z\034\207\335\270\273\343?yeL\334[{\341?=KwWh_\357?8\007\204#C/\343?d\244\037m`\261\321?p\014\257I\202\005\356?h\341T\\0$\326?\224\316K=\177\303\312?:\014\035\356\000T\357?\264\240\361\323\340\201\324?\0179\312\222O\344\351?\336)\307]\235a\333?\220=\"\350\233\224\253?c\257\236\363C1\350?\022\374;\3201\346\351?\344C\361\263\270s\342?\374\275\333\340\327]\307?\0340\243g1\035\323?\360d\360\267\373\317\327?\321\236\266\374\353\376\343? \372\321\202Hq\267?\275\003(?*\220\356?L\r9\217&\215\330?\313G\220\233\320\316\346?&u_|YK\352?P\004b\331\325\243\342?\242\322\336\r\360\250\332?\010\261\026\222D\031\273?_#\226\205!0\354?\220\211\257\367m\026\312?Ow\225\253w\216\342?\335k\3301XI\354?~\2040O&\227\346?\260M\327\356\027T\251?\370j\nX\310\235\265?\336\317m\370:\217\326?U/\216\007\302\354\344?\334Gq\361\337\270\312?\3142\247\351\314v\354?\025\321\030\270\221\022\353?V\344\3402\321X\347?0\336N4\234z\313?\022ZB\241q \325?\373\035\347\\\244\224\342?\000X5\351\353N\333?<\355\300\205 8\350?\216\014\200\023\230\033\324?dY\307\273\030\010\303?%\231\n\271k\351\346?x\220\327R\313\221\346?\2163f\321\341z\351?\332 \303\306\375\332\333?\374U$\270\357\234\333?\341\234\2669\276i\343?\317\003\243$\323\013\355?\033\217\226\3707\225\341?1;\214\0164\220\355?\370\217\210\352f\376\307?\300\374s\207E.\205?\212\317\0146\334d\344?2\3414\rC-\320?nX\256l\013\310\326?Q7\245\031\256\224\341?\220\0236\300\310Y\343?h\300oD\016\204\267?i\210Ux\224E\352?7\307u\252H~\343?\032\323\033)\210+\333?\230\177jy\305\345\333?\347\255h\206Q\033\344?\342`;\313;\247\321?\022\354:j\243\243\354?\234\027\"\307\0310\325? \r\355\034@\332\326?\000#\352y\257\333\264?\000\336\327L\357\253y?\366\030\002\326\253\265\353?\300\030\277\260(\020\243?s\022:\031\025\336\353?\243\336\331(9t\344?\330\014r\216k\270\357?\355G\240\327\371\376\351?\333%\354L\003:\355?\313\357\222\003\265\352\346?\000\353r\203+\301\250?\371\305\253E:\244\353?\260\233\026\306\327h\324?\3504Nm9\310\320?\t*\014\316^\272\357?\232SU\201@\t\352?\356\361\364\273K\343\353?(\213\211Cx\321\266?a@lYh\316\357?\000\010\266\010\273{\335?\372\340&\271\316k\325?\000]\232z\177\353\325?\364\204\033^\246\217\340?\345\211\370\252?\013\346?\350L\360>\270\t\344?\255\355\264\342\327\362\344?e!\003\366\341\222\340?\203\026\307\376\030(\357?v\tD\240\303\327\334?G\026\246\315\035_\342?\351:\363\341H\212\356?\335W\243\033\376\247\357?\357\241\346\250K\347\351?\004\207@~}g\340?8\363W6\352\314\321?,X\2062\335\363\312?\201\262\263.i\251\352?7T\273\241\255\217\357?\034\004\307\254\344\225\301?q\000\3514cP\356?\000\310\215\363\002\n\303?\002\245\326\220\016]\325?\216\374\260\234\225\016\352?\000\024\234&\263\237\342?\337\036\016a?\025\351?\270\036\363\343P\020\327?\351\206\035\322\322O\347?(\007\373\333\3235\350?\365\222C\n\230X\342? \245i\310\217q\331?|\371\313\341\335\264\354?\260\322T~\323\217\241?\004\270s\225\310\350\305?\023@\315\244 \237\356?J_TJ\306\221\330?Z+\351\361u\005\322?\270\214\356\340\377\206\305?\324\321\341\321\014\327\325?\220\246\224\313\352\252\240?;^Y\351\356\272\343?\r\317\303\322e\351\354?)^;2\213f\357?\207\230\350;\325\034\355?\206\266\034F[V\353?\372}\264I]\223\325?\261\270\355~\300\247\355?\210\2532\314\302\\\341?\362\320Y3`\256\336?\010\236\220\t\221V\310?\000\351\204?\247\275o?\362Z\250\217\030\364\350?PP\224\216\r\274\252?\275\003\270g\0066\343?\332\315c\317\235\364\342?p\026\322\272@\257\356?\000\006\240\241\321\257Q?\222s#\020\324\373\345?\364a\334\214\253\034\330?v*\231\270|8\343?P\274* \371l\345?2(\025\r\221\245\356?\355\325\343\245]Y\341?0B\037N\345U\350?\246\253\207\274FT\343?\264\033cm\232\215\303?%\245\333\224\300f\354?h;V\013{\222\300?\205h\023\324\004\332\347?\317V\220\3130\226\353?\360\016w\225i\377\310?\000u\371U0\360\201?1\267g\026BQ\352?\215Z\275\231\255]\343?=vAv\223\224\340?\317\361\377\305\250\305\351?\367\035\021\025\264\203\347?\0356\277\3331\232\353?T\327 \034\232\262\312?.h\r\270\034\202\352?\212\'\260/\004\372\321?\352\332\374\324\260\371\355?\251\272\022_\031|\355?\346\366:\222\014\214\334?f\310P\200u+\334? \360\013o\252\376\264?\024\212r4\201\261\347?\365\241-X\323(\347?\220N\360n\033V\320?H\244\363{\333\212\260?@\345_6*\270\324?\001r\337!\002\231\340?\320\225E\031\370+\301?\240\231\222J\010O\316?DB\251jJT\306?\336\303\213\314\022\035\350?\246\324\232}b-\341?\300\002F\240<-\334?\270\254\214\227\033\226\311?\323qE}\346\005\353?\034\226,\211k\377\352?\233\261\352\000\004\210\350?2xIR\334\037\352?\376\306\232\237\307j\334?\340[\227\264\273)\240?|~\277\016I\027\303?\240#\t\276\364\020\351?0>\215\225\322k\256?@g\231%\367\336\205?*\322STU\331\336?f%p\362U\246\343?$3}O\223*\312?\344|\271%U\266\350?l!\371\020#`\337?x\315L\242>\036\303?I\216\211\036\363k\340?i10\273|Y\350?\212\"wqC\245\333?p\202\303?\240\337]\345\220\267\223?@b:\267\222k\273?\212@\007\265\216\014\356?\360\277owi\360\347?Y\246\357\006E\220\343?\0032r\203\200\377\356?\240tC\000J4\314?6\323\331\211\022m\346?\234\312\366\317h\312\304?\n2\302\357\236Q\330?\032w\250\365\322Y\352? \003Q,\312\005\307?\020\260\t\002X1\333?\226lz*\242\017\332?`\273\204ni^\254?H\367\"Z\314\231\311?\240\244\344{\003#\226?\2619\251\201c\260\343?\302\255\312g\375\265\336?\364\275\231a\302\346\355?-\362\366C\254\247\355?UV\3620:\345\356?m!\n\177H\223\347?z\363\250\004f\246\332?dPv\344qU\335?\010R4\214\327v\327?HTow|\351\353?\032\357\270YZ\271\347?\317\354G\207C\036\356?(\t\302: 9\265?\036\025\366\224Y;\335?J\315+\373\341\r\343?\3046\316d\377H\354?\375\252\314\320\237C\356?\321\327\010\014V\213\340?\250z\377\307\024\021\341?\216\255E\227X\362\325?`z\016\224\374\217\324?\331s\260\230\235\356\357?\3667\274M\323\316\325?\222\333\n+\272L\322?2\371:y\316\r\352?b\346\314kT\315\356?\266M\212\323\304\242\330?\322#\010[\357\253\331?^\230|B\017\026\323?\014\343\023\000a\316\305?#j\0029e\303\352?\213\256\204\336\000W\345?\020\\\374\376\357\267\316?\2047Tb\241\237\354?\300FW9\347(\213?\300\036\261+B\343\223?t\324tc\367M\323?8\276\244\366\343\260\276?\260/p\376\315\271\342?\304\001\326\007\320\'\301?Y?\372^\004\210\352?\334\313\014\206\014\001\336?\000\260.P\221\022@?\322zB\237x\202\344?1i\355\307\265\245\340?`N\201\323\303\345\273?i\367a\033\330\223\347?\340\322\2334[\326\354?\367/\247%\221\323\342?~\t\266\3543\305\326?\241C\022 U\355\355? $\027\360\206D\241?@\321P!\327X\253?\201\332(\2246\304\350?p\026\377\006\017\342\255?\241\264\215\037\274C\344?\010\035}\321\265e\357?\"\224%\2044<\344?B\300Sj\001b\325?O(\212\374M\200\340?6?\252(\244\245\346?},\372\246\367\310\347?FB\310M\030\341\353?\370Z\tX\264\337\354?\236<\304\256\330\270\342?\002\265\246=\037\004\325?\260\214\227e\217Q\267?\205\n\250\r]/\340?P\24086\032\231\350?\024r\205\272\0173\315?\000.\303\206\264\253\257?v\203Z\n\366\001\320?\344+\307\351D\314\310?4\362\260\352\227:\353?\354\014\254\333u\347\321?\200^k\300\350\020y?\007\\\316\325\275\005\354?oZ5/\237\213\357?\025\262u\274\305\245\344?0\n=Y\321\274\355?@\270\023\240\325Q\266?\222jv\206B\035\342?\021\225\020\003\007\363\355?\260jj\341#,\304?\214t\035\t\222\366\300?\020\361\332\252\306\247\252?`\035\t\252O\243\221?\311\021\306\252@.\356?\320\235\272\317\304\351\345?zd\361\261\215\\\330?\264cP}\026\257\322?W`\326\302\233\021\353?\343@K#fl\352?\t\345!\357Q\212\356?\234\024\206{\272\n\322?F\262E6H\003\320?\202\2774N\243p\336?\326\020Z\312;J\327?\250h\022\345\224\234\351?\340\003\345\346\256\370\265?\342\230\001\032\231\307\342?*\222o\354y\264\331?\376\243\254M\243{\347?(\256F \231)\270?\023x\3366\370H\352?\226\347\350\003;\366\356?@\342\372\347\372\333\276?\005\371\364\355d\261\344?\333\361\032M\r\033\353?\022\307\327|\207\233\344?\031\201\023\341\032.\344?\037qx\232\017#\347?$\323\310\266\240\234\304?Q\322\177\'\022\337\357?U\234\024A\035;\344?\000\254\251\246\003~\213?\220\237\362T``\334?k\341\024\035\377\376\352?07+\212\010\020\355?\241c\013\333\214\362\341?\000 M6\250\244k?\333\351\376\nX\252\354?\216\004z\342\370.\326?\177\037v\231\355\324\340?XW\236:\013\332\307?0\036\254a\033F\305?P\205\315\212z,\353?\240\264\355\035\224\261\251?\2102\200\342l9\350?\330!\306\354\025K\275?\360A\242\230|\342\272?\360\235\275\267\236\n\250?a\337\230A\246\002\357?,\2436\301\201\211\335?)Jt8E\000\351?\240\005#&\033\001\323?XCq\266\325`\316?(\356\3261/\241\350?\210T\241\227\372\034\306?/0_\305p\322\345?\246\352\305J\243\272\320?\024~\177\211\254>\303?O\0044\356\203\315\346?\356\337\030u#}\352?\324,\203\027\221\234\341?|7\177\357\235\326\323?\026\331\254\355\350\233\355?\341F\215<\347\304\357?\200\316*f\367S\350?b-C\256\364\215\342?\312H\024_\207K\334?F\3505\315\331\241\342?R8}\214_^\323?\036\320\007M-}\337?KB\200k\237c\346?\033sS@\0339\343?\274\353\336\n\310\325\302?\024\010(\217\276\246\353?\270\266\034\232\337\301\352?\0149B^\271z\316?\020k\020\312x\266\313?\322\336\310D\204s\323?b\211d$\374\346\326?z\312_\342I\265\320?\300Hl\342L\253\356?\314>e\t\233\322\311?>\365@q\376\024\341?\210\010\211<\272E\266?\014\312\342\000\2676\325?\243\334\021\225\005A\346?\240f\345D\216\315\320?\304\367\332x\260\341\310?\246\203\0313\004\210\336?0\tq\313[V\357?7\303\345\3454k\344?\275\246e\233S1\353?\241#\340\301{\230\342?\333[\372\\Og\356?\265+`\343\226\231\356?\221\243\025\016\177\032\351?,LJ~\200\371\306?\330X/\227\377.\323?\234):\254{K\341?\244\273\374\017\324Z\357?\033=\324\201Ep\354?\250\332\2745\306\007\352?\002\037\365\205Y\226\341?\335}\332\376hq\354?\222\344\273\021\263q\340?\006\257\207\317)\266\340?b\327\303\221\234\236\356?\216\232\314\331\017\365\342?\340g\341\301Y\254\327?\266\261(\255\354\307\334?P\313\237\217[.\322?\240% \244\021\016\220?\266\021\023\372\372V\340?\360\317\327\376\024\031\305?\003O\356\276\270\367\354?\030\235\226\346\260b\313?(\212S\306\300_\311?\256\205\276\325(\353\346?\303)\342\026?\177\342?\210M<\333:\007\325?D\274\367\302\001!\301?%%E\320\'\245\352?k?R\347G\346\356?\354\311\203G\245\241\325?~\004\311\300t\351\347?\315\313\374\366\331\325\341?\257\0213\226.\274\346?8K\341nc\227\353?q\366\206\034\377\321\355?\205\000\340\037\310\330\351?\370bs\207hU\267? \313\217\177\263\376\310?pM2\374\247C\305?\351\233\206\020\306\366\350?\000\331\3309\255\361s?P_<\004\344Z\241?H\226Q\200\364\\\321?\251$\363,3)\356?\236\224/\000\312\347\336?\'\321\213g\227\226\340?\321\013\351<\262\n\350?qI\020o\321\247\345?\302upEG\346\333?\016\023t$D\264\352?\260p\004O?0\313?\002\311\323N\212\264\325?\020\352\324GYR\271?\261\206\237\253;\355\340?\3601\264\2771\350\333?B6\033\225%\033\333?4r\272:\335X\306?|:\325\000F\310\340?\371\252]7-\027\343?O\376\2105\007\256\355?@F\210\347e\246\262?\247\341Z\372\264j\343?`%\317\271_\241\255?1\346IT_B\357?\234\014\302\313V\020\342?\023X#\341\324\365\347?\230\374\303\372\276p\356?\330\232d\230V\312\311?\214\037*Rh\177\312?\310XdM\332\233\325?\250\026\206.\216\024\322?\345q\325\304%\032\355?@n<%\226v\217?\314\344\203Z%n\347?\2004\345\017$\032\247?\030\367.\337\277\262\307?\177X93V6\350?\2474\263M\212\314\345?\372G\360QC0\320?\352\201J\230w\351\350?\263D\355RC\026\341?\002H\245-\010\355\340?\030\237\363\037\220\371\275?\240\250|\241\227u\230?.E\360\252V\361\340?\266\003\300\262\230\014\320?E\215\247\367M\350\357?&+\n\351f\363\355?,\235\r\002\032\316\314?w\020\375J2\276\355?w\322\2366\234\376\344?<\233\013\017\254\021\300?\214\356Q\376b\013\335?\'~#\007\315\326\354?\355\314\273+\367\370\344?4\361O\027\234\233\353?\265\267\260\034\247\361\352?\370\304\016\313c7\261?\226\333\247\313y\353\330?\205\022\203\264\030\222\354?\177\306\2626<\036\354?xy\021\235\376\345\274?\021&\226\315\002\t\353?\320\252\000\305\035\252\306?\376t\331\252\236q\357?PN\236\211\330\322\276?>\304\251~\004!\320?\244\2620[Pk\324?:a\014,\220\361\341?\316_\222\024:\377\334?\026\323<\243Z\273\351?\232\315\010\325!I\341?o\023,\035\033U\347?BJ\1771\357\227\333?\242\304\013q\377\210\320?vu^E\216\300\320?#\232e\\\005\003\352?\200=$\276\016\246z?\374\266\254\005\302\013\336?\317aA\003\332\336\343?\300C\3573\022z\336?\274\232@\270T\t\350?\tKT\210=I\354?P\241m_\036\363\336?D\365\375Jh\324\352?\035\301!\356\350\265\346?\273\311\030\2340 \345?\242\244.%\352\201\342?}\222R\377\263}\356?\267\243G\252\205)\341?\317~@\351\330-\354?\2737\250s\327g\353?\200q\336\221\351\\u?P<\230\234\253\342\243?\300\357\n]\200\264\330?JQ\265H\275\'\357?O\t\034\241\275E\356?\025\030\354\265\341\243\343?\214\350\263\033\356n\332?\313;\014\263\370\025\344?hg\307V]f\346?\023\034+\260D\334\355?\264wL\233\227\370\303?\230\371\237\246p<\343?\t\017\241\375\210F\341?k\371\213G\341\346\356?\264\250\352\005i\232\327?\212\\\rj\211I\342?\265\203-\221{\370\346?\320\034^\034\245\013\304?8{>\247z\204\303?\334 \3122\260\377\316?\240\344\273\364\000\222\345?@\270\203\360+\030\324?\270\215]\027\203V\330?\200u\346b\246\204\323?+@\242]\031\351\344?\206.S\307\210.\347?\360;\300\373\241\035\345?\310k\264\001\275\001\277?PW\352\304m\227\311?\356\363\275\211\320\000\342?\353\2754\337\177\363\356?\000\320\377+\331\255s?\\\251\014\236\252\005\335?:\202M\215\372\'\343?\330\035\274\213\360c\353?\320\347e1(\273\255?\240\210\3051\324\313\317? uL\237\357P\270?\350\330.\302\004#\315?\251\261\006<\220\353\342?\002\221`\200\373P\324?\354\217SC\375\246\333?JDbO\352q\320?\350=\021\254\237W\274?\243e+\362$\033\350?L\006\005\232\367<\333?\352\337]\tzC\333?l\016\204h\347d\304?\"\370\207jX?\343?\264u?{U\270\321?\314\265\010\3146@\305?)\315\320\231\2214\347?\255^\256kFE\343?\273o\021\247;u\344?\310ix\370\033\337\270?\300D\345\224\321Z\335?P\357\020s\273<\266?&k\2007~\233\321?M\316\223#\232J\343?X\273/\302\221#\344?\254e\002\177\215h\305?\277\316I{\030\236\352?\312~~\317\363t\353?\020-\351@^\316\327?\345<\302\373\207\005\343?\014\334(\240\365m\311?\030\237\204\025\236\273\331?\025\262\303\221\211\260\357?\214\224\240\300f\326\323?D\251\227S\203\317\314?,\337}\211I\014\354?\337>\242\212\306%\345?>\013P\331\316\272\321?*S\003\311\250S\327?O\375\364\210{t\347?(\365;7\345\341\267?0\326\233\361\247u\240?\302A\350,\006\320\336?-\313j\004\005N\346?L\267\036;\235\204\334?\310\2237\260\202\232\306?\224mC\335Z\323\305?3\010\322\363o8\347?\n\232\275\230%\311\354?!\002\352\327\353\330\346?\355\305\375\023! \357?\370r=)\326\246\303?\032\341b\001\351J\347?\342HRK\377\330\341?\366(\001\"\315\272\345?\270|=\341\211\254\267?\262%\240^KZ\345?\320\037\231\216\347\363\242?\200?\003\340T\361\321?w \3756\376\314\347?t\342\327\337\231\251\310?\020;]f\343\264\336?f\036~\333\344\237\322?\246\277$\234Hm\324?\370#Kh\370<\324?\332\030\003\272\225\351\347?\351J\350A\256\265\342?\210\242\370o\036\335\337?\276\332\254WT\247\331?\014\356.\231\r8\304?~\210\252\260\246\353\356?\320\377\330\373\021R\256?@w*=\244;\220?\312c\024\262sg\346?aN_\310~n\353?\000d\266.\202\274z?\004\203R=K)\346?z*\300\033\004\352\331?X\036\200\265\374-\313?\300\027\001\264\370\237\343?*\205.\272M\300\323?\2178\270w\314\350\350?\034fE ?\240\341?]U\252\212\243\014\340?\230\345}\022\020\276\336?(iw=\013\317\262?[\013\34743\007\347?\024.\267\014m.\312?\244\346\215g.\032\354?\004\3653\272\342\310\315?\322`\263\323\212\006\346?h\216\216\261l\004\317?\235\332\022O\207b\355?\320v/H\tJ\341?2\230\342\356:\274\357?([\000\\\224\\\272?0zU\014$\364\273?\"\327\007\013$\245\320?\207)\260\215\006\352\341?\316L\356Y\034|\322?\340Ty~c\216\250?}\320\343\207\r\311\357?\324\211J\307\345\363\323?HT\270q\375\021\344?\271\177\217iG7\350?\003l\326@\324\333\354?il9\237\023\370\351?\346PQ\305\203\001\352?\236xL+\277\370\341?\310@\356\210\017\202\332?0T\254\303\350\201\265?\234K\375\235_\317\340?\017\234\256]\177g\355?L\034\351e\345\003\354?\364\034\254\327\221\365\344?\247c\3218P)\356?6\2138\345M,\333?\020\322\215\003r\t\343?\004\220O)-\\\355?V\035*@N\277\322?N\254\360\033\357\351\327?\252#[q\003\023\356?\'\356\372~\331u\342?\227u\330$a\027\343?x\240\002\324;C\301?\310\001L\230i\276\332?\010\226\213W\312\004\310?aF\r\205\230\301\344?\226\223i\tZ1\335?\336vt\335J#\352?\364{G\026\223v\344?\357!\254\276\3666\357?_b\350\372\210\021\344?\356~\252z\253\276\323?)\245\201\022\"K\356?\317\342?\250\365\324\372\350\240\306?;\\z\350\364\250\340?\235\233\036\030\037\341\344?\007\022\033\202\306\265\345?\0002m\222)X\344?F\016\213\303.\335\345?.&?J\200\215\325?\244\255w\035!`\343?T\035\210\262$\212\320?\300\277\364\251S3\260?\331E`>\324\323\350?\026MqA\210\377\335?\025m\3147-\340\346?\204p\241Mzy\355?\347\363.\214\2019\357?\006\001\200\222\201]\324?\347Y\250\266\201J\356?\000\304\374*\217.Q?\006\304E\004%\213\343?\312\r\233#\365&\327?\374WRX\304\372\310?\204n\354_\036\215\305?8\216\266\326t\027\303?G\3252\201\325\221\342?K\032\022&\356\276\351?T\353l\345\334\245\317?Vxt#\224\325\342?\365Y\267\020B\327\350?\240qs\n~n\314?2\325k\341\205[\347?\000\261\356}\362q\256?\370g\362\324\210\310\260?\027u\215\3702H\347?~|\334\231\244\311\321?\2016\255\025\271\245\354?\344\2363\255\233\232\315?\022il\217\306\377\347?T\006\toD\212\317?\201\324\354\362\202\324\340?\202\323\237GEc\350?\002P8\307\177~\352?\2276\266\255\376\247\343?B\204;a\021\301\354?\274\r\033\000\324*\347?\254\336\335\001 {\312?L\375\016\323\250s\312?\340G\365\352\371\261\304?\0300_\323\221\\\313?Lh\304pO\366\322?\300\"CI\240\353\276?\265\256\250\034\253\254\351?\253K?9\221\322\350?@\273\276\276\304\340\350?\032\340@\267\214\006\351?\030\274\244\\\257\372\337?\242\246\272a\363\316\351?Y\243\377>\333\346\350?\034?\236\344k;\307?^9\373\300uP\350?\032C\247\340e\220\334?T\266\250\276\002M\331?\234\256Kr\350\017\340?\230\000\374G\371\357\343?p\357s\013\260\n\355?\037{\035\205_\272\346?\177\267[05\202\355?\200\001!H\204\020\355?\240s&\254\272\301\227?6;i\321\354\233\354?\260\326\341\003H\023\262? \237o\2044\250\325?\010]c\242Y\010\307?k\323\376\334K\302\353?,\234\201\337\032]\323?H\257\304\265t\332\340?*\315\324:\345P\331?\233W\271iE\016\350?\330\304\271`\t\275\335?\366-\332nXf\345?p/p\240\037\003\304?y\342\314O_:\340?1\367\252S\303Z\343?\245^C\357\255\250\344?\304\344C\373\336\343\347?V\2221\033\235c\337?\300\005\252J\262\350\343?\230F\305\".s\272?<\312ui\201\'\330?p\341\027\014\210\243\342?\023\330i\002HB\351?H\3009\320M\325\302?\000\302S\252^F\226?\244\205&\301|E\324?\034\t\343\333\211=\337?\032\375Xn\374\017\336?\300\370c\200f,\352?\300\213\354>\336\220\262?T\313\331\214\304\320\353?\274\352 F\021k\347?}\353\r~\244\335\357?\300\367\252\371\014\362\277?@\2343H\303\270\300?\317\377jJ\231\022\344?.\023:\300qP\357?\310\326\214k\327\351\271?\206\223\302\330\350\024\351?\206\033c\324U\254\347?:\323\351~A~\331?\000`\205\352>\372\300?b\275\347L\337s\342?\264Z\3270\213\002\337?\372\342\037C\236\335\336?\360\303k]d2\332?\220\254\240\252M(\322?z\006\324bb\362\322?\265\277\277\213\213\000\345?\327`\206UXB\351?\337\331\276\341.\230\347?\\\374\270Q\016/\320?<\352\300x\014&\305?\336\t\026\231\377\217\325?h\267T\212\265\345\313?\004\213#\342-\337\317?\3521x\224U\353\344?\334/\345\262\312\205\304?\300p\262\242}<\230?\270\366i5\202\260\340?$lV\306\344\265\342?F\337\252\251V\255\333?\032\251\375\3625\375\322?\223\022/\233%\321\343?\365\265\023\336\264~\340?\202\224\030\236\205s\320?\330J\370\373\022h\335??\276\344b\273\260\343?a\3550g\000\321\357?0\250V15t\244? \355\201\007DO\332?\177\007F\261\036\213\344?\210\001x\226|\032\346?\016\273\027\234\253\025\344?\n\016f\341\235\025\344?P}\343\351\t\t\331?\362\276\376\341\302\235\327?\350\ni\257R\357\304?\024\276\233.\035n\310?\314\373\261|\014\351\344?\240p\266\224\001 \271?\357\032\305\"r\210\342?\017\352,\367M\317\352?\255Qv\227\277\265\350?\316~\336\020\375\242\322?\244\2456\177u%\330?\260\344;\031,(\305?h\270\234\226\322-\320?\262\000yJ\332\227\323?\000\243\"\024\222\343\344?6F\202\037\250\373\321?J\201\355\244\266\031\356?\004\274\312A\350\322\322?\314z\245S\020\262\317?\350\3449\2070N\330?\260\343y\034\'\256\357?Cw\254\242+\257\343?\212\371\245\334\216\027\352?\206\335\001\232\353T\325?\342\n\004\261\315n\343?\2765u\207\313\245\326?\342\213YV\306`\347?\334\032=\014f\310\335?\312\316\005@\241\203\323?\016\322\216\372\264\302\355?^-\230)E\303\345?\316\017\344J\025\201\342?\326\322\303\342\"3\337?\213\3705\0076&\346?@\243\232\307\344\200\261?8\313\032\243wQ\346? ]K\231\226\205\235?\304\330q\376H\330\344?Au/I\211\242\354?$\257\323\350_&\312?\325\227\273@\261*\341?\3203}\375\236\241\266?q\264J\341\373z\354?|\234\367Y\250!\336?t\300\016Z\342_\326?\\\301\266\362\237\261\346?\300Dv;\232\334\273?\020\372\262\026\005C\322?C\343\255\004B\230\354?\020\353\273Y\223\370\352?l\274&\013/\235\330?\373\336\303ZP\325\352?%6\364\256\202\217\354?\300\225\231\204\003\215\231?\220\227\003\223\002\372\245?\366,x\210\314J\325?\020\225-{>\035\335?@\3258\003\023\313\324?\343\351\032\210\263`\354?q\347\306j\233\234\352?z\323\222x\276\271\323?\024A6\212\374\020\336?\300\273K\301U#\256?x?ji\327\235\314?\024\317\030fc\230\311?Z\234\363f\315m\333?\000Y\240\304\tb\257?\010\267\035\371\211\311\331?\251\374\306Q\036o\346?\367J9\033y\327\350?\013#\275\203%e\341?!L\3722\177t\357?I\017+\022\277\t\350?\360@\225\373\027\254\311?\3109Q\314\026\r\345? \301\362\324\336D\355?\230\002\267e6\307\325?\\\342x\235\303\224\326?\274\316d\373O\005\340?l\032=\354#\221\347?2c\352\247\037\005\337?\230\027\031\317x\206\325?\367\241\241g\246\203\356?09d\177/Y\343?\255M*o\315X\345?658\"\321\231\332?\364&\014\223\323\316\327?\276K\240\022\326z\331?,\211\211\2152\357\333?\324\261\313\225\010\260\351?m\005\235 \265\243\350?\000,HX\217W\316?$\320\356Gd\025\326?\260\337\024\242\017\326\353?\240\035L\003\322<\250?D\026\322\201\2136\351?^\320:\255\365\307\324?\325\307P\206D4\353?\210\237H\250\357:\270?\200l\231\006\353\364\220?~\337\201f\253~\340?\340\247\346Y\261\350\341?5\306\266\233\250X\353?\017\337?\227\265\254\353?\016z.\021YM\323?\364z\205S\273y\322?\230\233J\re\240\271?\262\t\327\0212\221\320?\357\267\210\246E\"\357?\305q\320\245\270\322\354?>\376\303\205\305\325\354?\342n\251\226\313\002\343?\320o\315\367\204\225\327?\037\205\"\002dP\352?\032\010Z>\370r\345?\213\005\213\336\020\211\353?\332\310^\2768\233\336?\272*G\206\377\'\335?}[\003\2552\310\352?g\363\007b\252\356\353?H\324\227\301RJ\352?=&\327\370}\374\355?L\363\016^\232:\331?|\335\0074\237\006\353?\300k\313\337\370\031\322?zs\263\333\007\256\326?\363\027\370\353\014+\345?\374}n\255)g\330?\226\227\0319\223\317\331?\370\333\221\300\366\350\317?\276~_%\371Y\347?p\177\274\333\345z\356?\3249\036\356\244\255\310?\214Z\010\374HD\335?\270\230\315\004Q\257\332?\265\247d\317\327h\345?\352TV\023J\354\322?L\225Q\357Z\267\347?A\006\255GJh\352?Q\367\266\344\357\273\347?;Q<\227\250}\351?R\025P\031\010r\356?`P\023k\316\243\226?1\030H\231\226\002\354?\257\204\374L\230\n\351?\2304`\364B\242\270?\032\341\267\370\207\366\322?\374\345\306@]I\331?\341~\311\262\323\214\340?\000,\262x[\275P?\240\216x6\237+\251?\225\252g`\311\336\356?)\237\263\322\257\205\341?WK\356\013\014\036\353?\356M\277U\374\262\324?\203\2202\264Fk\350?*\002\325\263\337\331\345?\374\221\250E\314x\333?\344Cf\202\212\373\314?\022D\200W\220\305\353?\340|\330FIW\307?\3008\237\005\206\376\324?\300\001#\315\200\031\224?\237L\037\336L\002\355?|\343}Q\253X\314?\200W\t\354\202\362\231?\220\037\026\370\r\021\323?X\013\005\313\027\241\350?\372\356\371\351\301\253\356?\250\301YF\374\256\274?S\365\366\305(\236\350?J\354?\230h\302\337? \232\231D\030h\257?\005-O\337\013\205\353?h\276,\026~\025\300?\204\206\322|\255\344\313??\213\031:07\344?@+A\3634>\221?\350\277(\346x\306\331?\214m\230\363\277\214\354?\000\303\232|\303\"\357?q\221\303C\323\313\345?\000\333\250\222\024Z\273?D\321FCO\375\337?\355\023\037ufo\345?xU\333\347\351\034\314?\244Q\261\261\224j\344?\304\n\310\362\301\225\305?\276\254\3318\3463\334?\216\347\373\247:\327\333?\211g\313\300\347{\351?p\331\351\316\2175\263?\222\336m\037\327\214\335?9\240\262\030\243L\354?\'4Pt\316\021\341?P2b\251\223\027\334?\375_\263@\317\350\352?\000+\001:\252$\350?\230\376\333z\311p\306?\353\224k^\033|\352?\214$\213\273^z\357?@\323?\326\301\"\337?\rr\211\360\273q\350?\024\317H\345\374\242\322?\3327\362r%\352\327?\263(oa!#\345?\307i\205\327g8\340?\200\272\222\n\215\311w?M9\326\360\276[\340?\320\210\013}\226p\324?\037\003\032\267\252\212\345?J\324\253\017L\251\337?x\322\226G\014i\263?\003K\023\276\250]\342?\304?\320i\327\240\324?|?*\317\216\242\315?\366\021i\n\277\\\321?\324\251\031M\313\027\303?\345\035\221\256\363\027\353?\307\251\362}\203 \347?PIG\232X\245\313?\363\177\223)f\026\356? \213\316;\3339\260?(\022\264\231(\014\350?\340\341(mn\265\251?\326M\315Zzy\322?\26491L\330\311\353?\272\004t\222\272\333\342?\354\214\210\302{x\313?\352\242\000\232Vo\333?`\222\006\246\273d\222?\360\027\235Hw\200\330?\300\311\230\375\334\206\231?[Z\221\026\315\307\340?\370\262\256\\\335\032\316?q\306\224\306+\330\346?\330\312\243\t\021\'\356?\273\363\311 \217\226\341?\226A\273e\210~\346?)Q\214.\201\200\357?\004\201\260\355\323\355\304?\024\310\355{|\336\335?`\313nK\266!\251?QgJ\266\232\013\346?\373\355u\355w\254\341?1\253LNK\213\356?C\213\276\270\271f\351?0\246\275\263\371\201\355?\000\277\030\263\220k\263?\014:u\"\227\257\330?\360\241\264\374M6\334?\214\334\036\010@3\301?)\363%\302\201Y\357?m\233\305\016\237\320?\220t\3607\236\024\260?\370\214\\\372\222\340\324?\244{\0064Q\032\320?6r\001\220\213\035\326?\200\'S\2639j\264?@\351\273\254\331\355\334?\026!e)\301\331\354?\342\211\300\271%+\323?\362/\327\213c\320\322?\314\260z\212\320.\326?\020\252N\"\373\344\251?d9&\322\246\346\304?:\255\217\026\200 \335?p\303\372\251\331\306\267?\022u2\324D\205\322?-\265\356N\252\201\345?\370\016\273\363\276<\312?\334\246\255\214\374\002\313?\022\271\300\315C\377\350?\320\027\177\216\242\035\345?\200.+\221\326\206\212?`\367$\267b\007\353?\243\261\002X\370Y\354?@\004t\254\360|\244?>\332>\352\321\224\337?L\245m\356\000\000\317?V\250\037\253\332\333\321?`\324\211\374\235\362\230?\370,\001\316\264\001\270?\033P\363\313\333\n\354?v*\347\344 F\320?\212\346\r\027\201\224\346?\226\213WGF\024\352?\2121@\376\205\326\330?K_\271\3747\263\341?@!\263\262 \355\202?\356\271\307\310\270Z\355?\000^\222\024E\276\312?\306\311uD\317\206\320?\376!\003\3070~\322?4\314Gd\313a\353?I\260\005#\013<\344?\344\033\334_\221\340\310?Q\241\372[fQ\343?4\331\341\301\332\254\353?A\370\251{a\367\353?\256\313Iv\211\220\351?0\325\270D\304\200\306?\320\235C<\276\307\326?h\332\356]e\343\307?H\241G\312\200\001\332?v\246\373\362\324\330\343?\202\263\024D\343\007\350?\3446zT\315\214\351?\370\002\033e\342\201\346?\226jK\303\210N\351?\240F\006\372\315\212\256?\n\304\322\363\331D\340?n\251ucr\240\321?\360\352\370\204\272\262\347?\320\352\337\023A\016\347?D\342\013h\343\276\303?\320U\303\261*\273\262?\002=\371D\226\344\326?\340\214\3633.\226\266?d-F\225\356\320\346?\220v\242t\365\200\254?\007\335\346^{g\354?\326\266\362xFR\326?\374\tcr\373\362\345?\340/\254\215\301\360\305?\320\2337\306u\270\333?\233\005QC\032 \353?\277l\330\225\034\376\340?V\315\3373\302q\322?\320KqB\\H\324?\t\215\252\347\323\t\346?$\255-\257\336/\326?\234\276\277\305?L\362\347\370_\247\322?\252r\372\203\tW\356?\300ey&\353\234\353?+\036\327.M\300\353?\350\036\302G\213\310\260? y\214\016{\277\270?\332\032/\201\241\003\320?C\022\265\007N\273\347?\026IrX\227\356\325?t\031)?\031*\352?\234\327\267\360\217\207\315?l|\005\257\236G\343?|\243\302\237\234|\302?\202\331\024c\376\311\330?\027G\365\300\204V\350?P\022a\377\025\221\266?x\204\326I\334\307\305?Xb\177\262\263\325\345?(\034\223XI\n\342?\000\005\356\325\342w\323?\244\034\211\221bR\347?\250!|\272\341\324\275?\216+\235x\\\204\325?\262\266T\233\323i\356?\316\005R\270\2516\332?`\356\r\tn%\260?\335mb\320o\363\357?p\235#t\276q\306? U\320\003\344[\332?~O\266\340\341\026\325? \0219\346\311\003\242?\3041\r\201\207\314\342?\300\321\234:\222\341\311?\264\3607uH\277\331?\374\270.\374\200\223\330?\3748\005t\373\235\314?\256\212;\021\262\035\340?\237\\j\253\0174\354?P\345\035\031\376#\356?\340\341\030\317\335B\257?\210\373!\022\377]\337? \203.\371{\343\304?\303\226\023\325\n\315\341?\n2\360\371\352\204\323?\242\037\'\333\362\001\323?Y\246_\326\025h\357?\200\334E%\330\227\201?\020x\024|\221\344\246?\364\021\373\2161\237\331?\3072F7\333\002\357?\274\206\255\343\350\302\317?\034\363\250H\360\270\336?\312k\253\037\005?\325?\210\315\200Y\237\220\266?n\362\273\361X\334\320?w\360\267%\372*\340?\3206\033\035C\t\314?9C\225r\216\230\343?\217\314k\340OW\343?\352Hn\211;\242\346?\313\332\246\253\260(\347?\355\227\266\344\237t\355?\007\"\341\330d&\351?h\331\3565{\375\321?\t\353W`\200\366\352?\356\306\336\236\222\316\353?\203\332\371le\203\355?\3014\270\327b%\356?O\360,\250$\233\354? \201\256\2203\252\355?t\377\357\004\tp\317?\212\207\033k\317\227\327?\266r\226\311\265\362\320?\320o\375\317,\315\267?U\t\236r\016\r\350?\234\\\364k\204\026\310?\374Y\305\212i\005\331?tO&GqK\354?@\203sV\310\264\342?\017\273\366e\224 \354?\350Z\210\272\364\232\310?b\007\225\363\r\335\342?\362\370O\321} \325?\354\231wB\324\246\343?\\\327u\204\\\364\353?R\337 x$\022\343?P\271\2622\265\257\351?S2\315\272SV\340?\324\206\230\341\276\030\314?\262\210f\2638\332\354?\252\364\301\330*w\345?\220\247b\335\351\032\246?\272\211\270\205\255H\347?\310\243G\2215<\274?\244\360\362\353\2117\314?\344dX\311\237\020\352?p\303e\323\344|\315?H\t\314P\026\315\274?<[\200Z/\315\341?\3307\223\2477\221\275?4\276\267\3128?\304?\367\324\023\3514\000\351?b\035H9h\223\320?@\000\036\244K\005\331?\365\033\301\032,R\357?\265\335O?\234\330\351?\326\326\337\037\276\313\346?\270=_Y\335\336\341?\322\013%#]\342\351?0\356+\257+\344\307?\260\314:m\236\341\325?\210D\347j%>\260?\300M;#<\341\232?\001|aL\234\r\347?&\\\227\026\263\020\352?\340[\240L\222\277\257?\376\254\013\263#\275\345?\020H\254s\374\247\255?\370\253\373,\244\224\307?\000\365\026V\313l\206?\334\'\217\230H\266\313?\0233\312I%\345?,\020\303\202\201\016\346?\2448\221\242\300\267\330?\300\333\325Z6\221\351?`\026\265\207\014\251\355?I48(\003\210\351?y\254\362Sd\270\354?Xk\300\202\376\355\277?\010;\262\0163\316\355?\205*B\372\232\343\354?H\020\343XN\010\303?\220\020r\202\000\352\267?\351\\\237Q\373\212\350?\333\376C]V;\345?V_\202\363\377\330\324?@\032\356\335\304\357\254?`\255\330{:\252\304?\206\321@Wd\010\337?\3560\314\3509K\344?\225\177^\312\233\375\351?\347Mf\0146\367\344?y\342\327\352\265\r\357?X\031\327i\355\312\334?8\025\372|\232X\263?\321\374M%\200\274\350?\'\034\rw\020w\347?N\313\3303\370\355\333?\003\236\366\031\314\216\353?\236+\311\247\306L\351?-\017Q\273I\005\357?\342\277\233\002Y\314\357?\277\324xV)8\351?\366Y|KZ\213\322?\317\020\374l\336\003\341?n\356\372\014\300\224\342?M\210_\317S\352\354?\376*m\362\314\355\354?\352\257a<\250\221\333?\021\037V=\266\357\356?\000g/[5d\335? \212\313\256\246\357\276?\020O\301\316gs\301?\220k4/\363\211\355?\032\001|u\260F\341?\254\264C\211!a\333?\220\261I\010C\201\340?\"C\266\244\021q\354?\243\277/\355\022\266\340?\006\220:\264\3779\355?\330\3754\263\204\314\320?\324K\265mE\344\307?m\020\346\"~\325\346?\030\362\262\202q\302\336?\244Z\341@\320\341\354?\323\201\013`\302i\342?J\250Vc\263F\337?\242\347\2139d\330\346?@af\353?T\253?:\014\203S\3474\326?X\200\204\255B\033\266?\340\366\020\206(E\303?\000\335Q~\003\275p?d\014\352\324\215\343\316?\270\006\305\250\242\333\354?\204@1\267\221\325\346?W\367\025Y\273N\341?G\307r\341`\206\342?`x\264\244\tB\242?\272\220\206r\362\246\335?P\237\302\220\336\211\264?\211\334\001\034\363\034\341?pF\225\016\004\355\244?\326\377\374\223\',\335?\222m\326\374\016-\357?P\362\237\316\207\343\311?\260QU\263\307$\274?9\224/K\'\331\357?\214P\034ZLl\353?\324\'$\340\340)\334?\360\211\3775\022v\313?h@t\325\314\237\325?\000L\006\320#\177n?w3p\026w\022\341?\210\363<\'y\243\310?\024\221\177\345\375\255\323?\324D\327\360\022\355\311?@\237{\374r\370\242?\036v\376\260\201Y\336?\020e!^\267\223\262?6\243\3200\242j\322? \212F\373\321\031\256?R\261\007-+<\335?<\360\320u$\273\356?\374\300LP\263J\355?\242\306\207E\300\274\325?\320\214\311\257\037\365\243?h\016\224\222:\323\354?\356\205\3309\306.\327?Xo\"*\te\336?|p.ht\306\304?6\n\"N\320D\322?\023\251\031\004\242l\353?\340>\2345<\325\246?\000\030%!\356\014\331?\365L\210Gl\340\355?\250/\013$\263\013\271?\024,\3741)\000\323?0\367N\026`\333\317?s1\\M\334\005\345?.`\"\256-\257\345?\035\255\027\345D\023\350?\240\325\251s\021r\347?`&u\224\373\260\265?Z\347\257{t\016\335?\362\325\200\031\322l\320?\3004\300\364\351\234\201?N\362|\311N\013\332?Y6\354\240\373W\344?\240i\373ed\340\334?\031\265\363}\306\204\342?\000\027f`\232\213\233?\326\275\321`?\214\331?\346\020Ndo\240\341?\200\324\351T1\233\352?7^p\0141\300\343?S\243\260\3635\356\351?>\211\022\241\240e\355?\240\373;\364\276_\254?\250\321\217*\206\370\326?\202\350o\2352\232\344?V\034\355\317\034$\343?\020\364\212\264K\311\243?#\272\335P\240\257\340?\n\332\236\323\2239\324?>Y\214\260\024\370\347?r\372\241\316\234\341\330?\236e\220\233A\033\354?\327\356L#)\232\357?\360\271FI\257A\253?\000^SP\236\036y?\031\253\017u\325\323\341?\".\031\337\036T\342?\364\317\241\201\341\331\347? \331\363E\017,\327?]MV\201\275W\344?r\335{Pm\377\354?\255\263d\222\360\362\346?B\266\323\212L4\322?\210\335\250\001tg\265?\2002\336\035\253\206r?[\367\303\335|\346\344?\306\320*N$`\330?Q\030\233\300l\231\352?X\024h\215a`\324?\327\236\224\035\030\351\346?x&\272\220e\033\355?\357\271\323\304\231\211\355?\347c\004`(n\347?\266\347\207\210\014\276\324?\206\3473\224,$\343?\360\311)l\236x\265?\230\035\275E\353O\274?l\251\237J\320\245\343?\243z\030\013\272\327\357?,x2\022\202>\335?\340\212z!\213%\245?\266\332P\034\372\350\336?lA3\205\322\024\326?,\352\215\307\265\023\321?\210J\'\3556\313\327?\252\325gt\267\371\354?\022;\211\346\307%\323?V\316\222\211\036\246\325?p!\246\277=\353\270?V\030\036\265oU\335?\200\300z\252/\rz?A\242\021l )\356?\300\373U\355\207\037\273?\205\320\250\265\t+\355?\350\021b\022\236\202\356?\026\262\002/\"\003\352?\004g[\346\247\311\333?\000v\212\356\317\237p?\032\214\325\326Y\005\355?\350\354\235u\227W\305?\324M\005\356w\224\311?Rw\266\323\260\310\343?\237q2bl\246\355?J\025\262D \3220\037\346?\212\014\364\006]\345\357?W\363\206b\237}\343?\244\226}{\200g\320?\320U\241\3264\212\356?\206\357\010I\342N\342?\025\014/\242\2763\351?\310\177\212\230\344\250\351?\320S{\n\375\347\276?;-\377,<\272\346?\260\332\306K\210\333\276?\362a\247\rm8\340?\356\314\315\227\263\377\353?_)L\204\261\'\357?\370\022\035\315\205\224\330?@\253\242Q\354_\344?VV\\@\032\031\353?\005\0043q}\371\345?\342(\327v&\256\345?\017\251\201\314\0357\353?2\037\3101\267\022\346?\320\277\345:\312A\304?\320\254\240\347\276_\241?\302\216(X\005@\321?\005\014\002.\t\244\356?\302\227\266\006\223\356\350?%]\374\301\273\351\340?<\366x\274\021\253\330?2\225 Q(E\344?\325\327\353\275l\021\354?\026ch\366\211F\333?\314\244\252\370\246\316\330?\311\301\005:\314=\341?\262v\245?\352B\331?\260\316\266\367\r\252\264?\350N\352^g\271\317?\206\\\264\340\362f\353?\220\027\243\320\375@\301?\202~\360\316>G\342?&\276p\224\024~\325?\023\255Q\024\355o\354?\224d\2305\221\334\307?`Aq\375\264\031\224?\236\313\030\337\260\355\350?\n\220\243q\221x\327?`\301\2232\003\026\273?\304\230\343\330v\"\330?\260\234\210\337\275j\325?K>\241\337F\265\355?\005\200j\217\370u\353?\330c!\276N|\342?zo\235\"\213#\354?\306\207QJ\036\311\355?@\322\265\211\320\255\241?\320CL\313\244\364\324?\204\315\n\314Y\361\341?.\371q\323\245D\356?\350[Bx\256\272\301?\031\r\362\204\t\034\355?\325\204\355\256\232\004\345?\032\247i\035\300,\352?\3332\274\020oW\355?\374\0335d\356\267\344?\312\205\360\213;R\351?\260}v\363#o\300?\354:\035\2455\364\336?\346>@l]\262\346?(\261\213\273\033\'\276?O\020!K\357\r\351?\"\331\234-I|\354?\341\010\252;\222\265\354?\360\000%Q@e\343?*\034r\365oO\321?{\321\371h\242E\343?\350\021\361\246\314o\352?&\210c\370\237\360\321?\340V\254\344G\355\305?t\322}\2323\030\313?\220!hD\307\367\314?FY\350\330\367\300\341?xF\t\023\006 \313?(z%QHb\322?\000h\372b\364\225\311?\021\3714\265,\307\341?!NT\226\242-\340?d3\216v6_\337?Q\347\263\224o\372\345?,\366\223\023h\215\347?\r\224\237\372}\206\342?(\303\370o;\271\300?\221\261\315\335\211\223\355?*0\237q\"\013\343?\200!\323a~9\275?\273\346\322`\365\306\341?\236Y\026_3P\356?\335\344\351\3509\031\347?\020\023\374!8\177\320?\020y\203un,\267?\020\203,\327S\027\251?p\201\016i\341\375\335?@\237\324\200\351?\220?(\275jc1\271\324?g\357#\250\037(\345?V~Eq\325\024\324?\034YU\210\372\207\304?pO\025\027\332d\340?\214\356\"\263w\217\344?4%i\332\004(\355?Z\230\207\235LK\345?\254\002\355M\300R\327?`\256O,\235\304\265?\300\233K\n\307\323\313?\240C\035\265\"[\272?\246\340\206e\006\225\342?B)d\243\335\224\334?\231E\373;\021u\341?\330\2731\354\300\007\274?\376\t\013\'\260)\355?\316\350uW\277\007\343? \276e\216\231%\330?\366\232\262:PF\342?\334\235\033Iw\360\356?\251\201O\356\214\003\343?;\311Q\233\305\233\350?\206s(#\374\'\327?\234\201\273_\223\263\322?\373\177V\005=\337\350?\204\302\324\034\355\324\347?\036\245J\313#\340\337?@Z\366WS\330\237?\324\365g\253\037=\304?EF\n\203\246C\345?\323\306\224\016F\365\354?\017\263\343R\324\231\341?\000\345\327\371\343\307\257?}+\004t\277.\340?\321\251s\304LV\351?PT\200\326\\U\321?\264\354\222Z\234\222\341?\022r\n\270\305\363\323?\215\000\014n\225\212\354?\2318\246P\033\303\350?\014\225V\371\034\\\305?\240z.=\\\272\274?\344XJ\261\230x\332?k\300yT*0\352?\232\023\207\3538F\351?\210\2108\267f8\304?\330\001\022\212\224x\332?\217\204\252\026\260\367\344?\266\326\263\230\035\344\325?\364+\277\021\022\316\352?.\254\351h\362Q\345?\244:\320_\216:\324?\241O\274\034\024|\341?f9\037w\322\311\342?h\224\243\230\236\346\331?(\000\314\275\211)\353?\332\255N\001\215\356\330?\365k][\3058\347?!\377;\303\223\263\350?\034\332]\214\315\227\307?\236D\3665\202#\350?\220\304\335\271}\232\342?\347zh\266\2400\354?l\342\236\360#\240\344?:\234\331i\360\271\321?\024E\200\330 b\303?PdNV\0141\347?\202\310\261\316\234\005\322?\243\376B>!>\345?\324\334\253.\036\331\320?\366\253\337C\033\026\327?\3008?\362\247K\233?IS\245\200\341\346\340?\310r\343\311`Q\262?\n\272\360fB\257\327?c1\222\374\177\346\347?\262\376[C\354\022\337?@\016WW[\347\223?\336\373\227\275\033Z\322?6\233\345\2744\312\342?0l\241T\237/\316?\334E~\346G\300\307?\215\356b\005\341\327\354?\234\321\030]\351\242\304?\236H\014q=\231\357?\250B\376x\335F\261?h\213\305\257\023\310\322?\321\330C\036Qr\343?\270Px\243z}\267?\334x\177@\251\270\331?\005\376F\331?\\\357?\t=\344\216W\355\356?\352cg\273\020_\345?f\377\360\360L\376\335?.\346\007\034\3361\323?\202\225%\006(n\326?\340\211\223\202\374p\310?\210eS\037@\357\271?\264\232\273\244\216!\327?\004=\tb9\376\333?\216fr2\253\"\346?\264\347\314\217\355\227\352?\314\256q*Sh\351?0<\305\010(\036\322?\226\330\313r\220b\357?c$\355\322\177\"\355?P\342UOx\214\272?\034\361b3_\005\335?\035\224mp\361\234\341?\236\213,M\203\242\332?s\370e\366\t\312\354?I\350r\221\355\362\347?\020e8\273Ca\323?T\036b\354\343^\303?\334\235\363%\375\253\347?Dy,&\301\277\350?\345\327\2124\261\347\341?\260\340/mB\"\352?\374\323\021/\n`\327?20|\032ga\356?8/\001<\200\351\333?T\003\256K\324\246\310?\025\252\202tl3\355?t\235L\325l\305\307?\200\217\243{\217G\267?\344W\037\033\346n\320?&\250\303\341\314\227\342?\022\237\237\010\034u\353?\322_\212tm3\340?\217\267R\005\263\345\345?\200e(\025\252&\266?y\364\227\245,\022\345?\260ke%\346\322\317?|\277\016C\253\035\336?\300:W\201\223l\330?\214\2031\235,\204\353?2\214\314\026\240Z\335?Pv\273\373\371\323\357?\223\270ud\304g\343?\205\270\343>\276<\353?\354\275\r\247#\000\357?\355\263J\215\210\257\347?\250fN\272\200\343\276?\300\313\245n\320\245\344?\345\362\347\212w\353\354?\265.\034\366\311\211\353?H\311Kd\215>\277?\234)\211\261\365\362\312?D\004\201]L\225\310?b\375\306\tI\321\340?Vbx\252\201\213\324?\204\230w>\264\346\324?\376\030R,w\344\326?\022\255\374\220\320\010\330?\004`si\206\353\301?\213\214\363\343N\237\351?\232\320A\226v\'\352?\n\2736\273\254\343\325?\022\016\013\t\226Q\337?\350t\223+G\226\333?T\262\215\271@\324\320?\340\317d\336\240\264\260?\3334\260C\237\252\350?\022\256{g\376\014\324?)g\366\252\313\360\341?t~<=\316k\342?o\377*\255\3501\345?8\034=\254^X\326?\325\271\251;`\367\344?\030|q\214Z\205\311?\212{\302\376\233~\347?\205\004\334\336\212\251\t\274\330?@W\316\336\377\221\273?H4y\304\tM\305?hC\36661\014\350?\205hP\000\275\317\346?\243g\343G(\032\355?\310\311Xd(\235\273?\344\362\265\000\206\347\314?\032\0073\261\270\204\325?a\t\262a\304\256\341?T\335*5\037\'\340?Jon\252\210\352\320?\300\310V\206k8\330?\340|\265\340V\234\302?\340\253\230=\263\327\265?\350@\272\211LB\313? {\237\357\321\370\332?\224f\217\231\247/\303?\200%\035\3137=\267?\354 \244qEA\314?\310\014\034\226d\362\347?\026\376\256]!\222\343?>\351\0251n\001\335?o\312\303]\025\231\347?j\256\267\'\020\r\346?\240\257\336\0228\273\311?)\206\304\262\232\251\343?`j\016&vx\244?\316\314\346\255\223\232\340?\221\304\360\315\237\204\340?\330\347\037V`E\356?\337\307~\215\350~\345?@\324y4}G\247?hca\270i\326\345?\006\034\3474\257\346\335?\274\317\347\374W\340\305?\010|\362\026\267u\325?PO\013\343\315X\323?X!\023\367\001\365\343?\361\0350\007\241Y\357?\250}yk\376\320\270?\313401\014\321\354?\227\010Nz\203n\343?\277\005\220\000\265\024\356?x;\256\217\373D\311?\364\216\000\305\021\017\336?\330\177\324\220LM\322?\270\375\036\213C&\332?`+\254H\2464\355?\264s\036M\r\341\342?\216x\334m\347\250\347?\310\377\364\357\330\363\310?\337\001i]\221/\353?\244t\323\332\220\356?\370\263|e\210\005\262?F\332\031\034\222\275\354?A|\344T2{\351?E9\341\300`\255\355?\210ZF\014\307\344\317?\336\361\332u\354T\332? \362P\223\302\300\333?_\002\251\273\313e\354?%\013\300\205Mm\344?p\340\037\326\037\367\267?\226\367\212\215P\321\323?|\220\351\033#\263\333?Xu\001\332\037\307\357?\306d\020y\354W\322?b\2734\364y&\346?\020\305\225F\332*\265?2\211\335a\306\341\355?\034\276&\312\233\331\327?\340\264\271-l\211\266?\246\033s|\364P\323?\222$\0031s6\345?\314\\#\253-\006\355?\235\267\312[\332\033\346?\204,\000\236-\013\350?0~\030\276\300B\245?M^\325\215A\300\341?\252\016\315\t\263r\326?]P\357\n\347J\351?\310\327\013?S#\301?\305S\246\023\255\273\355?t \032\366h\254\321?\256\361]\300\256\324\324?-\006\2629\336\234\343?0\0060&;\310\343?\367m\225\373\212\277\341?\240\"\337\370\265$\225?\013K\277\343\027\230\355?\310\310\"\003\"\005\263?D\216\213y_\243\324?\3001\225ru\331\226?,\363\350\351\324)\311?\315p\025\345au\351?X\365\276\246\272 \345?<\245)9\241=\301?\006\277\225\0068\242\341?\000\323\211\206\002\374j?\342h\t\301\307\377\320?\226/\030\225\350\203\342?\355\n\224\3262M\345?\360\010f\t\314\001\332?\270\230\302\364;\374\276?\350\311\366\034\303v\271?nwa*f\341\331?\300\270B\267\246*\260?\342{p!\266\014\357?`\370\367by\000\337?\212A^\3202\r\333?\024\001\302\021\006\352\307?d\204?)\247\004\305?\031\346a\2308Y\356?\\u\241?T#\341\370\205Y\316?/\354\324\324\233\347\342?\332\303R#\313\372\334?\260\361>JI\375\354?\230a\222\"Gc\306?\271\010\000\211\316\211\341?L\207l\202\350~\313?\305Yf\211V\330\355?\020\333a\365\210e\341?\306<\225\246\376\341\352?\261\033\337\263\314x\344?{x\233{\022\375\352? !?\326\336/\266?\304\234\026\223y\312\310?\313\211]\316_e\350?z\315*\266\032J\345?\216\021\364\261\0069\333?\030\020\013\333\200@\342?v|\332ao\377\321?\320\203\017U\372`\343?*\330[R@\207\337?\374\327\304r\025\223\347?\271\351n\330=i\357?\362\365\342[\355\232\332?\274\272\355\306<\035\306?A\330\262\007D\226\357?:\313!\001\222\024\353?\226\272\302\242\336\203\334?\225Z\375\275\004\277\350?t\330re\371\266\350?\034,\271\224\000\341\311?d\212\325\322{\r\331?h\260\374\245N\262\357?\036o$\226\007>\321?$\006$\004\216\t\305?9l>mzh\355?\236\326\0250\220\306\337?\327\232\342\232\304\212\347?\002\022Z0\346\277\357?\323\332\036\307C\002\350?\372\230*\336HJ\342?\036e\252\3427\230\342?h\312j\301\265\343\261?xMa\374%w\311?\3303\361\242\211x\354?\226\207N\234\203R\322?\202d\223\025m\261\326?HsM\343\241\177\351?\302v\256\362I\246\327?\220U\230\312\206=\354?\247\3703e\241\236\356?\"\241\327N\354\347\352?\000\255\3556\325\002\253?\014\006T`\034\344\304?p\017\215^\016\020\354?iZ\241\005N\366\345?\344`1 \324,\313?\310p\027\016\224\305\336?Xmzr\305A\351?\362\330\376c\335:\323?\000\237\230\360<^\264?X\327\270\256T\312\351?`\244\341\005\363g\355?\372\344\362C\t\330\\\315?z\n\207\232G\357\357?5\n\315\262+\330\354? \200\027\266}~\226?qr\336\201\033I\357?\207#Zs\357\215\350?\310\234r:p1\277?\300\014\rz\250\276\236?\257FW\331\310\227\357?(\206z\027-\356\262?\036\"\233\350\\\036\352?\250Nu_\353\016\263?\354\024N\304\252S\345?\254\336\372\275\253\266\336?\300\r\354\222J\327\357? \204n|\000W\236?\374x C\257\306\310?\326,\241\310W\353\355?\'\254*J=*\344?\250\347\243\204\206\032\345?\362\220\347\377\220\037\327?\'<\363\223\315\221\345?\340\255\365\264\005&\344?\342#\376i\241\210\344?\244\254 \265mK\335?\014@\027\250\332\366\325?\020F\200\214\324\351\336?\216\013\277k\310\022\344?\356\262X.\257V\330?t\373^\277\314%\301?|\313\353\205\2352\323?S\032\226\355\305\327\342?\320\252A\357B\357\242?29\327\236fp\332?\240jV~\235!\330?:x\200\227\202\372\321?\362\023J\213\304\257\325?If#d%\357\352?J\237\330O[\373\346?\242\350\277|\034e\324?B\345\"x\332\'\330?\000\363\010\212\340\232\340?\230\355$w\232\344\263?\177s\330\031\312H\352?\362\220~\245\271\265\323?\327\'9\332k\000\355?\344\257ILfQ\336?H{e\035&\005\337?JQ\375\026\234\345\353?\267?\316M\007\317\355?\034\310\354\0338-\346?\222x\331}\333\230\352?\230H*\262Xa\271?\337\232\310\273\247\236\343?\316\017Q\3216\320\327?\214\306\362\003B\246\350?j\306\030\264U\024\320?T\036\244\n_N\312?\311#woA\322\344?\312b\322o\026\344\333?\370z\234\357\254$\357?\373\213m\221\021\276\343?\264\327A\361\356\330\352?\300jd\301\322\356\252?\220\016*\020m\026\304?\027D\313\324##\343?\330kn\220\201\325\346?:\245F_\342\002\326?\332\242L\225\371\t\327?dq\335\'\305\320\314?\230\034\304\334\232\351\304?\000\201\001\274\0109\226?\005i\336)#\334\341?\300r\021)b\314\323?\333U%l?Y\344?\275\245\224\360?\367\357?\354+S\220\263\360\350?\024\277\221\273#\364\342?\004=i\315h\200\350?@\305\025\243\210\355\321?\016\375\331%n\270\324?\365m\270,Tl\342?-\004y\265\301\014\354?\256\311*\324\013\264\342?@@L~c\225\223?\346\335\354\372\212s\330?\260{y\010\244\216\341?@\344\t\337hx\274?\211\301\224\r\341\372\352?\370\305hX(\375\271?E;2\244\001\242\344?8@\220\2463\215\304?\013X\364\027n\307\356?\323\247\251\300\261\003\355?p\334\032(s\260\266?\344\244{\322\226\205\353?\371\211\371\360\345\026\343?\270\201\313\240\021\330\270?n\347J\260X\311\330?\374g&\332lT\310?r)k\203\220\340\344?\216\306\340\326\026\272\354?6G\206\3653\007\320?\\0\334\226\264\212\305?\270`\256\2264W\357?x\013\264\236^\351\260?\270\010.\360E\361\306?\244D\026\273\256\243\344?\n\207\231W\345\214\354?Q\353~\251\203\366\346?&\365\344\242\216\007\350?]\312h\244\227|\344?0\320Z\016\370\207\277?\240\256\312\334\001\365\234?\343\305\334\004\014\341\344?H\367\302\372NK\276?\236\243\200\020(\317\341?,,\207\363\376w\353?\310\235\036\247\302\t\320?\004\342\3308t\254\334?\200\242\223AJ\376\274?\002\376\027r\245n\325?\'p\253J\315\211\350?\370\202\241`Gn\277?\353\004_x\034\037\356?U\305\3300\027)\351?R\323]J8A\335?\236\0109\3611\313\332?\226\237\220\037\376E\324?\336\310\361\255\r\270\324?\010\004IW\345C\354?p\256\177\347\340\007\247?X\271d\306\330\223\355?\234\200\365t\242\227\350?H\330\251\250\240\001\264?\320Q\246\315\323\016\254?b~|\247pP\321?@5(\212^\230\223?\304\312\235K1w\352?\250\240@\201.\034\325?X\230\036\227\230\022\305?\200\275\311:<\203\205?\207\264\343\266\246s\342?&\230F\211\263\355\334?T\367i\023\276\265\303?[\335P\3531:\347?\220\033\275\037.\371\315?\035d\317sx\303\353?\0017[\247\255Y\342?HU/\336z.\346?\344\224\003F/\274\301? %+\276\023\036\251?P\020&\211\341\221\253?\302\036\277]B\220\340?~\234w7\035!\355?\024\326\211\207\007\320\347?\032\376N\333;s\324?\250}*I\351W\307?RA!\232\311\326\355?V\020\005\223\003\270\323?}|\346\263\303\347\324?\362jy\031\374]\352?$\252C\274\217\024\335?l\304\1770\253Q\353?y\336\356\301WJ\353?\342\215\227End\330?\246\037\312\026P\323\320?\360\247\374\025\332\347\273?@n\177\017ze\345?\'\351\"\255\326d\342?/\251z\304\355\375\340?\211\325\240\3159\334\347?v\311\320\330a\201\341?P\247\240\267\352c\303?\250\201\017\263\227N\324?\254v\364#`\375\303?\322m\344\327\230\024\355?v\376BE\306\261\332?T\272\013\211\245[\330?\251{\335\252\r\305\352?\373\253\333d\013\340\354?`\202\313\306\267z\274?&F\304_\334\200\335?\252\014`\210\373t\354?\220\244^\313I\351\244? \243\"T\020\355\241?p\272\016\226\027\017\240?1~\377\003-P\351?\362\373\035za\227\331?\035\360}\354 \027\342?N\031\337h\030\030\347?\365\372\213y\205&\357?\272\025\377c\255\224\325?Z\025\216\346\354\r\344?\240\336\026\242\300x\222?\360\306\344A\352\\\272?e\256>}g\370\346?t\316D\nR\005\316?\000\257\276NFy\225?\002\347\033,\001-\322?\260V)\263HW\245?8\273\315nT\222\312?\330{\215O\313\007\351?\240\235\346?\200\324Z\361,\200\223?^\353g\217\261\211\321?\000\204\244\000\271\256\266?\322\261\233\214\262@\336?|\303\272*\275\204\330?6\3631\027\035N\357?\242\325\\\204\366@\356?,\274\356\276\014H\300?\244\214\315\342G\216\356?\021W\245\022\037\317\343?\324\263\326\203\247\343\322?\240Y\021;7)\331?\200!{s!]\340?<\205\367x\307\363\302?\227p\247\336\346b\345?\\\317\255\014\316\r\333?\252>VnH\206\333?@L\301\033ss\315?\312a\341\230S\375\355?\216\030\214\255\035\355\320?\352\266\346\252\354?\251O1\\\3118\353?J\320\032\241@e\330?_\017)\352\375\304\353? \354\202}\312\247\354?\266G\227\200\020\037\334?\230\004\243\253w\362\356?\370\320-aW\"\346?<\210\323\036\215\177\300?\030,\231g\264\213\315?\026\030y\345\246.\340?\010\226>\274\0133\274?g\263BQ\303g\356?O(\207\310y\322\344?\n\250\232jr\354\320?@.\277\317\006\202\242?\205\305J_\232\200\343?k\205,\376Lw\352?\342\204WbW6\325?\000\252u\032\r\356_?8\250\340a\357\311\276?@|\321c@\244\217?\260\256)&\315\237\337?\330\316\253$\371\224\264?\t\204\261B\016\273\341?:0/\002\343\305\346?4\317\221\334\265\002\300?\3701\325\377\022\001\304?\214\313\302\276Z\245\344?\006]\034y\375\224\351?\233@$\237\300\274\341?@4D\251\320\376\347?\316\323,\310\263\356\333?2\333c~\274\264\330?\034\021\350\031\260\316\301?\240s\255$\'\n\252?\355u\030B\035\235\340?\\\233\377\220\353\357\326?\344o\276\321\246\353\315?\230\370\027\246\244\\\354?f^\\q\003\255\334?\231\305P\350P\236\353?\006\331G\225o)\320?\306\214\275\223\014L\345?\363y\256\361h?\347?{S\240\247\305\034\355?Hd]\020\371\023\277?\230d\346g\361Y\310?h|\246\351r\243\277?\230MS\315\243H\305?.\216V\233\215\361\331?\323\276\335F\355\232\356?\350\232\241\360=8\336?\262\371\032We\201\355?`\347\030><\320\352?\333\031\302\0348m\355?\036\n.\032\320\237\322?3\353\200U\000\210\351?@,\266\301H\363\252?\210\2757\252/\204\334?w\245\305\330\256/\350?c\350@\312qh\341?\323[\232\306\016\255\344?&x\374\263\361\'\322?\234N6z]]\344?x\217\237dJ\220\323?n\275\002\311\341\345\323?b\373%\321\233s\320?\026\213tM\020H\334? =\302\271$\r\317?\226\030,\372\270.\344?1\247\204\316\340E\347?\364\250\036\035\320\357\315?\334\312\267cf\257\337?\234\202M1\341\364\341?\232\177YqC,\341?\177\311\177rBT\353?d\233\221\343\206=\336?\310\335C\211\035\316\335?\300\275rM\312\360\323?~J\016\204\003\362\344?\000\016\200l\371\371\337?\026\304r\275\344\016\341?\354H\251\315\220\262\335?\222\217\017\342aw\321?d*IH\264;\333?\322\0316\266\035\302\345?&\301w\342\274\352\350?\265\316\341wSq\357?pN\271\377D\227\341?\332c1\313\220\027\335?Z@\265@a\233\344?\033\245\260Y\002G\352?BM\216\262B\000\347?\252\300\307\223\020\311\325?\260\346\230\377\237\247\253?>\322\307[.\367\343?@\335\333\275\312\233\271?\010\224\251\210\222C\335?\270S\314\002\375\315\261?\254Q\235\000aC\303?\244-\233\2344\231\356?P\"\013Ae\r\334?\252\t2\232\2432\344?iH\254\256V\034\344?\014\325\215*\215\003\313?l!\255r\313\323\345?\210\364-x\346\363\346? \242\026\316$|\271?\222\204\035Hm\031\330?p1\\\351\036g\342?\356\241\017w\301\222\323?\240\201s(D\354\314?\302\261\341\246\036L\323?\344\343s\311\031\365\356?\330\202|\300\252\374\322?\301\342\213\034\344\303\356?\000\013\201[\336<\353?>@\213\313\026\205\337?=C\330\246\231\201\342?\347\335\3434;\361\347?\351|\215~\365\201\354?\246\002\271\3741\026\354?\300\216\230\035\337,\254?\215\017%\001\245\310\342?\214\330\375\t\005\036\317?\371\302H]\367D\355?`U\321\'y>\300?\034\350o\211\001\245\317?F\213\226\016\334$\327?\301\023Q=^\014\354?\3009\364\267\024+\201?\3249k\360zd\344?\350\030lgl:\324?\036\217\356\203\216\027\351?\021\246\355\302\242\371\344?\003\322\233\357x\343\347?\321\347\236o\017\267\346?\230)R\201\3518\335?T\317O.0A\317?\301\"\277\366\255\311\346?\361\343-\315\334\023\350?\010l\017F\364\263\262?2\357\335\003:s\325?\206JO\336\222\017\346?\200\276up\026\355\205?\211\262%\240\346B\343?\326]\224\t\204\275\346?\220\314F\031z\327\331?\240w\002\302K\271\232?\363\370e\r\324\314\351?`A\025+\245\032\351?\\\245\200e/\336\324?^\304\225\260\315r\342?^\356\275\375i\336\335?\026\333{\220\375\300\331?\206t\232hg\001\324?ey\352\3108\305\347?\352\300\323\212\364\336\341?&\306\311\334\336\002\331?p\250\352\341\244\231\304?\366\351Ud\226\222\331?\304\324B\371\266\363\335?Yw\343\032Pp\355?@\373+\221\017\366\313?z\243\313\276\001\215\353?\004\314\2318\203\350\352?\342n\271j\216\017\322?\343\242.\2759r\341?\204\220\322\364k9\337?\211\255\200Fl\333\356?/\362\221\003\366\314\346?\360T\2075xE\330?Xa\003\235:\034\310?\225\251 \344\255\300\346?\372\22306\340\365\344?`\277\215\004\362G\333?&\022a\225\233\303\353?b\353\252}5\223\342?w\251\236\225)\204\350?6\210\260\343\357\231\354?\330eaww\r\271?t\235\202\357\304l\341?\224r\002\343c\207\346?\320\371\ru\275)\345?^>\263\213\312w\327?\200\240\371\310@\254\240?\240\376hn\272\334\271?\310\000.n\236\343\331?\374\023\212\243\036J\320?\327\014\360;\253\321\356?\226f\000\035\276\237\344?\256P\377c\343\327\334?VN]b9\021\341?\227\303(\031\342\271\354?\014S\340\322O\337\344?@\355\275\217\233\271\215?\\\000-\243\035I\315?\234\226\007\r\006\206\347?\340\336%\345\240X\344?D\226$\376\303\004\333?\272\036\322\010\272E\352?|\237\2243B1\347?2\3037gJ\360\331?0(\326\311\0028\254?\032\357\327\233L9\356?\260\254\210q\204|\274?Y\355;\347D:\350?\270-\255\001\311\023\353?\200VzWd\215\246?\346\214\244\344\330\220\353?@\351\321\030\007.\315?\304x\255j\006\026\323?\000\304\036\202\205\025\350?\324\357C>\356b\343?)f\"\361\030\241\347?\332\336\242G2\225\352?X\312\344\003!\t\356?:e\025\371T\342\337?\356m\256\030?8\335?\352\232E\276\301\323\340?\274\262\365Jz3\327?h\304S\321yg\342?\317~\307]\353\360\342?\334\250`B\345\300\314?\360u\337c\241m\337?\000=\027\361`i`?\t\\\0318\360\334\352?\323\035\305\024\352\004\352?\370\263\205\344\220=\337?\200n\t{y\240\255?\2778\272GHa\357?\217\\\322\355\2759\340?\335F\234\346\3703\354?\224=\310\342d\315\353?\020\277\325\360D\340\301?\333\313neI\002\340?%>\202\234\202\020\353?\302\356z\355wk\322?X\3522K:\336\355?J\026\323>\272c\330?\360^(4\302x\335?j\217\336\263L\035\347?\274`ZPb\000\351?\222M\320\254bE\346?\314\216\261\204&\253\303??%5\373\000/\353?\202\204\326\333\273\375\343?\355{\324\216t\350\356?Y\nU\252\025\310\350?p2\236\310\3169\333?4e\363b\241#\356?\256nM\304\264\324\333?\027\244\300\277\037_\357?Y@\363\003\003)\355?\007\331\375\306M\370\353?\214X\005|\315W\307?\241\240>*\324\204\350?\027ct\365k\320\344?d\266\200\301\203\321\330?\372\323b\347!k\354?\320\276\244m!\352\307?T{\307W\317\214\340?0\31136\375\351\324?\230D\036\260\213\317\266?\354|\313_\214\t\317?(\303]\271h<\265?\325\037\022\225G-\344?v\347\225>z\r\336?b1\326\217\272<\355?P{=\257\267m\350?\320\242\216\224\006\314\345?\220\243\206OP\026\242?8d\013\367pW\325?p;\242\"\0020\254?\362\363L\337\247}\337?\340\326\235\321{!\244?<_\212\014\271\305\315?\374\325\332\224\225\326\316?\2109\213L\246Z\307?\340\352p\372\255\300\224\350?\221\303\376\235YW\346?o\335\352^\026\020\350?\202`[S\246\312\356?\020P\035\360\363o\260?\274\3131\372(@\341?\342\303\360\006C\240\352?(\245T\373o\376\266?\264\\\177\321{\314\352?\316l\3115s\355\336?\200c\023[\375f\266?\244M\014\322\211!\330?m\375s\216\010\177\353?U\233\255/,\266\352?\307xu\333\006\017\357?<\266\024?\021\235\305?@}aj\026\230\204?Hl7YE>\354?\016\016+Tn\033\342?\3446\205;>k\345?\010\300v\205\tB\260?\300LQz\2758\350?\353\345dB\216\363\346?B\272\004\320\307\036\334?l\222\356\333\224\200\316?\345\215\331\302}`\347?\352u\010\333\302}\346?\236\362_\360\366\341\334?\336\323\365\267\323\317\354?\232\'q\365\355n\351?\000\271Dk7\300n?\377\331\365\031 &\355?Da\373\357\224q\320?\303\']H\3535\340?\220DI3b\274\313?sNI\037\367H\344?\252\261E\302}G\344?\354\303\371\203\350\367\337?\363\277\027\023Qt\351?\360\371\220\306\351\261\317?%\351\"\310\320_\347?\002\\\3355\010\003\346?\307\341\231!\273\025\343?\235\331\256\335\245\354\355?\014o\371Y\325\357\334?@\340F\237\020\353\215?\030i\345m\222\367\266?\300tdN\337\360\274?\267\263\364u\222\310\343?\257=\004E}\373\357?\257\334N\265\370u\356? \r2\324D0\312?.$C\253\017\346\343?>\246\210\216\325\370\352?)\033|\307\276\222\350?*\220CX7E\322?+\203\213H!\301\345?\035\334\001\177I\237\344?\311JG\204\377\311\352?\232S\271\323\220C\341?\371\250Uu:\207\357?X\376p+\234\367\310?Ha\332e\005\310\276?\354\227\255S$\361\356?N\344\023\347\300\336\325?\214#\364W\3157\323?\343\335\352\207\0362\357? \251\304\326.\363\234?\360#VuP\306\323?\304\347\022GS\002\312?^\031\275zA\014\327?\237L\303\024\347\014\353?\301e[\213\262\324\351?,T\354\366\346\276\336?\216\n\222\221\266l\347?\322KH\232\033\300\343?\254!\303%\241\333\353?\340\025:q\r\217\351?H-9\271\331h\270?\2005^9\360\325\243?\356A{\'\0056\335?x\236\031c\0161\260?<<.Kc\266\320?z\354\235Df\304\327?\220\031\331\267\321\360\326?\224(\242y\270\234\353?S\374y\307!n\347?\035\021\272\206\352,\340?\307R\312\223\327\254\340?\300\265\\\225t\277\335?\350}\330\337\347j\355??\204\360\232\263\364\341?\230\270\027\213\365o\334?\210.\362o |\266??\177\305IAw\344?\032\312$B\364\307\352?D\026d\255\234\340\351?\354\371;\033\3363\321?\206W\264g]\331\325?\030\200}D\3112\310?S?\013G\2748\350?Pa\264\364\311\036\320?\303U;\344\244\342\356?\272\206\3767>K\324?\031\r\372\016\215=\355?D5\245q?\034\326?n\035P\r\3517\352?P>\033,\300,\245?8\201\000\304l#\345?|\341?\3305\235\347?\305?\317vc@\353?\300\223\356\333\377\021\224?0\267D\212\211\302\240?|A\033\036\022g\357?.}\373\235d&\325?N\300\375\277\\\245\335?\214T\223\371\031\267\350?\270\347\210`\"\025\323?i}\214\023n\261\356?,(?\2258c\303?\254W\354~\254\266\317?\335\032\021\020\262v\357?j\0172\315m\330\350?\240\333\244\300}\263\260?1Z\213#\225y\341?\027\301\001\356?:\341?B\244\357\333)\356\324?\2026\266\211\024U\343?ZXjy]V\325?@X\365\006\347\345\311?\032\3731\003\023\034\345?\030i\004\034\314O\304?\332\267\037\313\364\236\324?rDH>\345\300\333?F\322\005\273\2604\326?a\307\316\017\017\032\354?\220\337\3375s\\\332?6FV4\213Z\334?\232\020\240\237\002\223\352?\352\234\3506\317f\346?[\r\331?F\017\364\016\223\204\332?\270w\274K\023+\311?v\033\252\315I\367\356?\265\310\004\000-\372\356?R\034i\204\333[\331?p<\212K\262\367\267?p\220\262\372\242\312\307?\334S&n\244\333\321?\3351\0371Ox\354?\207\200\302\377,I\344?\220\205\020\222\026P\323?\010\305M\244\320<\340?\022\274;&o\370\327?\001\211\371\216n5\357?\340\307\243\242\025w\355?\310aBY\242\313\312?\240\224\236o\364\365\313?X\362^\360\307\203\312?\266\257\267 I\332\353?\200\216\027?r=\341?\262\360m-c\223\350?\200\303%9\3348\307? \025\234\031M\353\342?v\300\253\245\240\366\332?\300\024\2062\036\305\250?\210\033.\r\356\\\316?\256P\034\200I\005\347?N\2425\353\275R\336?\343\315\205\257\023)\357?,9v\311N\020\313?\276\370\376\026\333\377\346?\037e\t\025\351\232\357?\267\310\240\177k\342\347?Fh\207C\033\220\354?\230\272\261\376y\013\306?\373\260\314\037\212\002\347?\352\226\027\244|\210\347?\246\274\0067V\243\320?\230\347\0202\333f\326?\002o\227\261\341\002\345?\320\023\242\212J\342\330?Bg\327\231\327\256\352?\336v\213M\'%\333?\332\302\225\341T\226\340?s\346Y\023#\006\353?\260\320\302=\225\252\241?p\307F[P\356\336?\240\265%\260\231\325\251?]t\234e\343m\353?\300o\377\035\367\364\301?\236k\243f\347d\357?|\347K\0163\304\347?\351\250\301f\3240\355?U\016\213]i\257\345?`_0~Ck\262?\242s\2058\201\314\345?\3542\"d\232\215\342?0\262\'\362#\362\321?\230\251&:G1\323?\216\341\237\242E\031\346?(\330\242\207\341\325\305?\324+w\335\326\\\336?*s\302\323\002\307\353?\343a\356\202Gx\347?\334\037k\324\366\375\304?\207\303E\217|I\341?\022\014\026\033\252\260\332?\360\333ED)\306\270?G\r\020\204\372\200\357?\206\257\207\0211p\345?B\237\032\330Fw\345?\200\250\025\276\361%\334? \205`\031|R\320?0\323\252\256\217\025\272?\235EM\232\244\203\356?\000\240\200\221\210\200W?%\313Dpa\215\340?\245V*M\344\367\354?d\352\306\000\336\373\312?P\\\353At;\340?\"}\243\371\232=\346?$=\311\307sq\334?\016cJ\314K\235\357?y\177v\261%\313\343?\373\336\305\372\223|\356?\"3\256\034MD\336?B\351\366\202)\257\357?\020GK\324&\021\276?\250\355<\313\2239\307?(\235\350y\347>\301?\010SGG\223\306\334?XK-\024\234\276\344?v(\203\263\311I\334?\310;\301\314\303\226\322?\210c\344\302\356L\333?\205\n@:\216G\345?\034\274\230\003\025\302\354?\327 q\214@\006\350?\315\311\255\246\3342\344?\030\364\324\363W\231\355?\304\225\352\236v\270\305?pj\006J\360\005\246?V0p\242(\'\324?\032]k\360\211p\323?>\001\373\242\201\357\325?\255R\257\317\377\260\340?\272s\272\326\353H\344?\244\213\365\253q\242\340?\304%\336_\017\303\315?\320R\034\362\376=\250?e\035\001M\244j\343?\250\017\007\017\347\326\310?\362\307\303\005\231m\347?p\016dA\035\367\266?\364\375_FT|\340?Tgq;?\"\334?\270zV1\220\177\356?\214\026\245X\214\216\320?\250\026\227\215 l\317?f\312\006\345O\342\343?\220\306\032\245r\221\351?\240\207\267\004{|\353?EbJ\367\022\232\352?c\223\346\317\037\'\343?\270\356\240\352\221e\315?\372\367\366\356~\227\324?\341\240\253 \277@\343?\362$B;\006\361\322?H\300\263Y$\200\323?\n\324<\301\006!\325?T\367a\005mD\306?\233\351\031\327\237\276\354?\000\216\000\377\341bs?\nv\250V\246\216\344?$\323\273\377\027m\334?\270\275\317:\315H\333?\004\342\002|?\256\305?\004P%\337\251S\302?y{+\030-\253\351?\256ax\321j\005\342?\340y\367\n\351:\333?\034?\245\006\221!\332?\370\331}q&\007\325?\240vW\316\214g\304?l\032\323\340\315[\304?[+\342\343k\226\356?\200\373\303X\345\010\261?(\361t\312\242S\353?(\236\376\033g\363\336?U\270\324IY\375\343?X#6\260\323\325\270?xy\350\302k9\307?\276\036wi\212\342\347?\214\233O\004\373;\343?f\301\255B\243\311\321?\034\3333\240\027\364\326?\212d\203\367v\006\345?\352\2416c\346\234\340?T\036\016\207\325&\353?(\216\023\016L\031\320?\362\330\001\016\213\236\326?\250\255\350\036c\304\305?\007\225\271\313\373 \355?u\203\326@E\035\343?|\027\343\324t\n\322?hB\265\205\253\226\302?\214\364\032xN\257\340?f\310\320;\255i\330?\3305c4>\026\346?H \330\242\223)\262?\330 m\351%\000\264?fww\352\225p\344?\347\334\336\376\314\221\347?\030\371\265\001\0349\306?G\357\260\351\374\003\353?\312cI\326\241\311\341?PU\321\264\235w\271?\340\215\002\030\301\336\274?\306:N\307G\233\326?\366\330\276/R\324\333?\226\2377YZ\260\347?P\"rf\t\277\267?8\021\364\004\350\341\340?0_T\322hH\251?\\\345\207J\006\271\324?j,PH\024r\342?.\214\371\033\300n\356?J\335\030\351\2741\351?\344\212\314\376j\377\355?\'\003\013\330\035z\343?\324;\356\202\366\240\341?\006P\314\t\210\344\350?\202\340\220m\260\241\346?%\241%{&\243\340?0\206\305- +\317? \260 ~\313Y\253?\236l\223\272\276\017\331?\237\007\262\312\235K\344?\254\260N\233\213\030\300?\343Z;\023\353\201\355?\324#\275\001\377\361\321?^S\034\020\032\272\343?\260\001\204\301\004U\242?\342{\236\035o6\320?\000j%\375\257\310`?\264\032\003\333\017x\344?8Q>\352\366m\327?0KD\273\023 \252?\000\014DE\265\251\274?\030\013\320\210?m\307?\330j\300\307O\243\342?h\306B\321\363d\301?\334\223,&\022\277\357?\260)).wV\352?%m\372\001-\235\347?\353\006\303\323\321C\345?\250\352\373|#A\355?\360r\345\307\356\247\262?\250J\271\350\246,\353?x4u\246\0162\357?\255\022\364b\311\n\355?J\233\371\217\316o\345?\235fZ\003\270\323\353?\365GVR\335\223\354?w0\320\204+T\356?xE~\330\312 \305?[\345#<\2407\353?\270\204\002\225/\230\345?\036\215\317r\320\013\342?U\262\367B\200\210\342?EUF0\031\322\353?\335\225\350`\252\270\356?\256\267>\360\317x\321?@\236\251\273_W\326?\222\210p\262i4\325?\013\353\265\226@\003\340?\340\262\260\350\363\222\272?\250\272XO\0322\343?\035\240\320W\021\236\353?p\036\335\2329\320\356?\200\374\033<^\004\223?\354\360\256*\204\213\330?\"\223\250\271W\312\330?\374\350Cu\377\356\327?\212[hv\313\375\322?\3450\213\366\321\312\357?\376\320\315<\254.\340?\320\332M\030\2357\322?\216Q\304h\234\360\346?\240}p\3433\316\314?%J\262\202\251\363\344?^\232\251\326\372m\322?Z\341\345^w\221\333?P\235e\007\nn\242?\344#\206\333\243\227\345?\213\317\177\366\302\356\357?4\236\305\022\207\276\311?\320\352Vi\346\215\242?\270?\236\002b/\313?\031\272\202\231\366\333\355?\223\346{m\342\261\354?\245\021\322nn\225\345?\260\3557\006\0008\356?\332\362\363\")\320\344?\236O\037\361:Q\350?\260\257\321\276!\260\262?@\020B\216\351c\302?P\233c^k\212\251?h\270t\254\324V\317?\272\\\342\257|\375\321?\351\'W,\323\204\340?B\253\005\305\236\023\340?PC|\264!\242\252?\271\262\336\247]~\351?\036b\350\262\244\353\320?\332\254\244P\315w\345?\260a\032\n.\234\256?H\236tv\340+\305?Fx\t\213\031\233\352?\2637Y\241\303\214\352?fhT!.\360\325?\177C3\345\003\277\350?\253z8\024\226\301\355?\325$\234\002[\274\342?\211\274\005\005\014\354\351?\033/\210\014\300\355\340?i\004\024\206\265\314\355?V\351\335\335Y\000\325?\363=\212\030/\246\350?$\323;\230q]\343?\203\345i\345\220k\346?\332\256\220eF\365\344?\232\312\361\215\372\205\321?b\374\301\235\242\256\333?~\010J\206\006\361\336?\272\354\353\260\311:\340?r<\242B\261\260\356?L\2257\250\223\005\305?\254\322\261\200\244\233\323?\014\307\321\365\014\370\306?\346\316\025\337-\246\353?9E*\"{\304\345?\222,\033I\006\254\320?\r)\225\327FS\350?\035\006M\332:\032\355?\036\223)IV\342\346?\r\232|XA`\350?7\177\212E\307\177\347?\242M\020!\'\220\345?\242\004wu\303\027\345?\037\3232Q,X\355?4\020[g\315[\354?%\251\276\353m\241\343?\242t\227\030\357^\321?\234Tp\374\263\233\351?\010z\216S\320\203\345?\310F\367tG\370\265?\265\020\376\005\n\244\347?\226\017\022\275\032[\330?H\373m\010\332\010\310?`W\263\353\014\001\336?\021\177a\271\024\226\352?\300A\177*\\u\345?RX\240\275Fg\326?\217B\330\003\240\257\357?\202$\261e9\213\320?*\362\034k\374\256\330?\300m\330\004\034\227\225?\020\204A\032\023P\316?\016\331\304\016\3161\335?\034D\020T\335\240\337?\356\312\347E\237\\\343?t\327#K\017l\345?\t\224\035\215}\231\345?@\274\217\377>\377\303?0Sa\014,W\253?\270\302\301N\305\205\354?\210\205f\243\014\343\304?`T>\227\206\247\252?\0207?\333*\231\325?\014\344\302\265\302\304\352?\350:\214\230\330\211\322?\212$\255>W\017\353?>\025z\240\t1\346?\036\234\023\314\273;\331?i\271@\203\234\275\342?\214\364\357Y`M\330?f=,\224a,\346?\020\255\272\020y#\267?\220R\316\340,\326\273?\226\307\"\273p[\351?Z\250\312\'M\004\327?p\311\014\241n\241\250?\'\211\337e\005\377\346?\305Fe\244eq\344?* \256$\333<\347?W\2156=\3050\352?\246F\262\234\003\256\324?h\362\n\351\030\031\341?`\346jwOn\313?\270\257\274\366\220\310\263?\214\361\037\343H*\300?v\'\234\300jX\332?\000\304u!\332\033[?\220\'\353\024\035\377\324?\206\377\010\273>f\322?P\333\322F-\310\276?h2\254b=8\274?\365\345;\365\233\037\341?\376\306\361\tgS\327?\016\224-\263nz\333?\304\035\274\013k\274\337?\334\246:\213\360 \305?\300\345\360\256Q\246\325?\261\322\034\377}\324\345?\350\362J\370W\022\340?\240v:\030AB\314?\261\3553\3017)\344?P\274\226\302\260\334\321?Pkd\014o\"\262?\240;\014sq\022\261?\216+\231\360\214[\321? \276\2562\300O\246?(\014\201\252\367\354\336?\204\261\276\201 \'\352?\311\270\312&N[\351?k,\207}\033F\345?\203\3645\2576\362\345?\341ET\276\220\036\343?\224r\346\322\241\\\337?h8\320R/\204\344?\360a\373H@\205\301?\304-\r\360\320\205\327?9\327<.\205\250\346?\240j~\270\201\236\342?\360\n\257\200\257\027\352?a\305\234y\361\277\355?p\221\324\353\274\304\240?\225\264W_+\003\347?\200C\333\254\0340\272?\323Q\307\342\352(\343?\216\000\230<)\342\323?\236>\335\213\007\204\346?\340\006N\362\231\320\260?(.)\013\364\215\333?\200;U\251f\272\273?`\201\202\353\333t\310?\020(\374\361\350\266\326?\\=\204\3335\234\344?\212\206j\320\030%\351?\024\351k\300\322H\356?\000\264N\271 <\326?v\315\247\354q\352\350? g\372\353Lx\222?\366\211\347IE\031\334?\300\246N\3745\255\307?\272\321%)\354\201\355?<\263\221#\256L\322?\304i*\000B\037\310?\262\037\253\217\027\307\357?ty\240x}\002\351?\210\334:\335\364\034\326?\034\216B\321\346\371\323?\250\347\375\"\032\254\335?\375?\205\027\373\300\342?\220g$\"\243\212\276?\245\204\006m\"\244\340?h&\r{\222\221\353?\253\273\356P\331\364\355?\010\004\001\363\263\026\332?\371\026\026jn\367\345?\000\315@>\365H\271?H\302\323\373/\313\357?\200\352\206|8\330\341?\216+0\253z\244\345?\000$\021`\2633\355?--\204\\7\225\350?\000\346@\'\020.\347?\0301p\004\227\244\327?\032\365\315R\265\316\326?\317\363\265\261\350\263\357?\250dk>\216\255\356?]\373\n\r m\356?\030\261 \230H&\262?\245\317\\x\257\205\340?@0\2447\320q\330?\025\220\237\373\210c\344?H\364\3228\260#\271?\326w\213\305\271\305\342?0\306\r\323\222\223\253?\370\004\n\325\377B\307?\251%\007\247\260\275\357?\004\r\245\273\236y\331?\366\222\021\031\375;\334?|\314\345\313\335\201\323?h\320zg\272\273\313?X\030\276G\224\262\352?\237\177E\356\230\230\353?H,\272\266\211\357\323?\300\006\214\247?\017\342?\354R`\374\315\007\300?(3\010]\277\003\311?,\204\321\320\004X\300?j\376p\367ip\352?8xD/\274\027\327? \036\317\007\374\270\234?^1\262\212\244\207\333?@\256E\035\322t\237?\314G\360z\230\257\304?\256\222$\273\354/\354?f\365^\001\310\033\331?\370K\265$\t\006\341?r\226h\t\314\315\331?\220\245\253\357\244\257\315?`TQ\374\303\'\227?\330/6\244\224S\340?<\357E\252\0338\335?\274\024\353\221f6\336?\334\345\241-[N\314?OEe4\353\002\342?\206\"\304q/\267\356?<|\260\210\213\017\356?P\\\316\001\311\265\320?\340\t\360\313\235\304\302?\277cYN{l\350?Z\302\020\253[\342\357?\314\266w\265\262y\313?\337}2\346@\272\355?l\337\261HLL\312?\3400\312Z\307\367\223?\244\241\373\312\343m\352?\306\216\213\223\031\302\334?\324\037j\036\020\250\331?\256\014\211Yx~\334?L\0273VD[\315?\000\324+\004\267,\333?^\336\215I\177x\341?\260[\361\241\212\\\263? \270\222\245\341\320\356?cB\242\212e\334\343?\014B\3709\367\250\316?\253W \226\036U\343?\315H4\232\rB\344?\024b\307N>\032\356?\211\2230eSu\340?\233#\031/\016K\350?\000\222\311\241N\n\264?\207\350\031\342\r\246\340?\361l{j&3\341?\000\243\317.]\262\335?\266\362By/-\327?\010\214\222&;\355\310?\364\226\352\366&k\321?;\314`+(m\357?m\t\356~\332j\344?\010\223mP\0273\331?@g\253\347)\321\305?h\210=0\247V\340?w>\336~\233o\340?\374j\014\017y\371\356?\035jD\302\2743\353?\034\026\267BP\300\331?\360f3\253{\233\302?\376\347\314RN\302\320?\024\3349ju\276\341?\201\202\013\336\262\200\357?\310\3347-B\362\331?q\263\215\301\372\250\343?\303\256\323}[s\346?f\231 \321n\350\345?h\207G\202\351\023\322?:c\266\331\255k\346?\273\346\351|A\220\344?%51\202\'\221\340?\267\210\331\243\324\302\343?\\<\372.\365\346\340?\232\353K\311^o\345?\313\324\007\375\373\340\353?\303-\353\321\317*\347?\306\370\026\033%\331\336?t\221S\215iA\316?\250&$\236\006\215\317?;\251B\213Y6\357?4\016\025@\357\344\331?\177nY*\243\211\346?\333\274?\332yd\346?\275\227HYt+\342?\214\237\224\264 $\323?X]\2663\001\320\302?\020\324{8!\215\271?6\017?\207\t\241\343?\326\020\234\242\232\271\352?\340F\337\304cY\257?\2008\220a\302\202\211?xE\376cr\346\341?\322S\261E[\273\344?\204\364\326\371\350\334\346?\025\3054\376_\002\357?vk\001\036\230\225\320?~p\377\207Nc\342?\360h\200\320\2450\330?\341L\227\317|\310\351?9\365Z}M\366\351?\320P\rf\237 \325?\266\205q\205\341\333\322?Ur\017\223\265C\355?N\177f\027K\022\340?\\O\336\266\262Y\323?\363\325H|H4\355?\237\275\013\222\304G\341?:\021D\336\3207\341?P(lyM5\262?\\\211f\241\333\330\356?\370-_\\U\266\327?\"\237K-Ax\325?$%\304\013\205>\333?)c)q\350\273\352?>7\331\313Y5\351?`\222\245o\311\322\322?,\034\306F\026\243\322?\307\n\r0\366B\340?\214\223o\275`\301\325?\250\022\000\'\n\214\310?\020\360-Pd\266\251?\00445\205<4\316?T\025\004\317G\010\353?>\352\252?\300\313\336?\262\251\222)\210G\320?XJ\360\376\253\005\327?\345\375\366\272\217\312\343?\317\24292v\340\352?z^\225F\034?\341?\030\261\233C\307:\357?\330%\222\341\340E\344?~\245\027\033`l\325?r2\206\200\221\374\345?l\033F\274\364K\336?\354\013\210\346\334\273\335?r\"\362\302\037\200\350?\030\010\000\233\354\024\302?\262\031v\016\317\010\332?P\345K_\340\276\302?@\375\032\261\250\252\273?\316\241\215\327\247\362\350?\330V\353P7]\272?\345\346\266\352\307y\355?\034\222\003\315\005\030\350?$d\231\014!X\346?$+\327m\244\342\342?R\274\337Q\035\201\346?tY}\334T\335\314?\340\217\367\223\254\373\312?pF9\036\214\271\245?P\321n\305\314u\265?\303\234\026!\275\007\341?\024\201\367\265\373\244\346?D\t\002W\020\213\324?\031\375L\337Q\361\346?\225b\267\312\362\317\342?\362\177(\316\337\357\322?:F\222\234\227\251\320?\027\205\004 \276\366\351?\340x\361r7\'\242?\376\310\002}\360G\350?\352\007\243\341\027\367\345?\374\351\360\311\320G\307?\353\305B\257\324\023\344?\242?e!l\252\320?\223H5\272\205\025\353?\342\365\025S\325\373\342?\340\245!=ag\274?\000\246-\002\246\223\\?\364\253\3625\356\017\335?8WDO\357\204\327?\034\3311\225G\006\327?\210:a\312\345\351\261? \377\320\262\360\024\230? m\270\177aJ\313?\224\330\361\340Ll\302?\2522\251{\327\256\345?,\373\210\317\261^\345?\333\356\313[\0058\356?\244\361fK$\240\303?\362\365t\240b\361\333?\347\\\320\211\216>\350?B\276\033\010v$\325?\001\306\257\254\022\007\356?hO\014\010\036\234\306?\362p\364\370\330\331\337?\022\261s\177&\217\336?\014\376\300\206\226\355\352?\200\264\220\275\366\300y?\2113\235\017\267\361\354?\331\314\036\014\336r\342?\360\353x:\261\327\276?0\206\025\322!\360\340?\335)\356\207\"\334\353?\\\235p\027\340>\350?\020\231\2550Zd\350?\252\264|z\002\214\341?\276\245\325\276\372\017\325?LJc3y+\302?\322\227/\344\253\255\347?\030\300@h\243\253\337?\222\007(\324c\254\332?\300\306l\342\361\371\303?\265\251\361\217\260}\344?\350\357$\t\231\254\317?\372\317\237\006w\374\327?&R\277\244~\327\320?\227\014\206\342pF\347?\3303\201q\334a\344?\213\331\250B\242O\356?\216\017\3744\033\306\327?\006\272\255\373\255\024\340?\300\221\357\202\017\306\204?$\321\344\373\"\267\312?\230\251\3478\332u\347?p\274(\327\353 \321?\314\373\305\177\024\027\327?\032\271b\224\254;\345?p?\314i\220\234\343?\316E\227\313\231?\341?\344\024\222\246\242\341\344?\000I\246s\2747\216?\242\250\371\213\361\376\345?\211&o\316\267=\346?\014\343vJ?:\326? \213\214kN\000\344?\212\031\177cl)\320?b\322\206R\277\037\327?Bv\007\242\215\t\343?a\022q\225\234^\342?\324\234\346\323\026R\333?\324\276:\372\307F\351?r-\230G^8\344?MMa\362#>\340?\322=>\333\375\304\321?\210\206\304&\372\320\330?tI\252\232\032R\337?\363\016K?\216K\356?\003\306J\026\264\302\342?y\254\005\213\301\260\345?\216P\227\211\266\302\352?\255+\025\324\210\034\353?\200Q\236\277\214\361\322?\206\274\343>l\214\330?\002VDY\333N\341?0*\204\031S\232\351?2rr\035q\227\347?+\276\031\317;\215\356?\222O\265\320\014\345\320?\227#\267n\024k\356?5\331l\177\0318\352?\266`\367D\3361\345?\360\216\265\351\206\374\326?P\370kL\344\251\272?\200\245\302\261\324xs?^\335\202\271m\007\324?\206r;\300\036\321\322?h\025Ma\323$\357?\221\376l\270@}\343?\366\222E\024\213[\356?\200M\2155\202\246\234?~\324U\354Zr\333?\264\204\n\336\037\033\355?\235W0+\304\245\357?\353\365\223\202\033\362\355?|S\327a;\271\323?^\024+>\006\257\357?\214\326\321h2B\314?\300\021\253\375\\^\301?\260\002y\r\325\225\240?,~\237\320\007\214\352?\013\326\"\267\0217\351?l\363\000\356\341\264\345?R\300q\223mC\331?1\204\201\2505\032\343?\002n\234|\300\021\331?l\331}\330\253<\347?O)\322=\342\033\342?\315A\324\315B(\350?\206J\361*\257L\342?\024\t\214D\235c\336?$\253\355\223{\'\324?~\n\033\375\246|\345?\003\320?\006\177@\354?Xe\353\215\236\217\354?,\367\301\221\277\312\321?\365\266\310W&\007\346?~\221\000\326\276\320\334?\310\357\003\306x\330\314?\351\332\006\334\030\376\354?\"Q\341Z|`\326?Z\310`\303u=\333?v\314K\027\350L\351?\262\340v\274\201\354\323?O\373C#\232\342\353?\236\213\221\365\234)\346?\320\240A\214s&\354?\312\200M\361\336D\351?@i2^\336\024\344?\2445[1\023n\324?\241\324\206D\2209\351?(h\nm\331\337\341?J\270y\336\031\223\324?\376\257\3714B\326\322?\027\331\236\001\242>\345?\261)\256\270d\266\342?@\212|/\344f\204?\250Z\233\316\276p\307?P\236E\251\026\026\346?@v\266\271K\360\272?\\\n\334\026\311i\353?\304\022\225w\365\002\314?\\e\234.\217\370\302?\264UNQ\005\\\325?\270\014sM\244\370\322?nh\262(\315\210\320?5\233\r\354\036c\355?\276\330$^\364\016\331?\266A\262\254AR\335?\2361\023\312\361\235\353?UB\013\302\351\375\344? \027\260\316\236\314\223?\354ea\007HP\311?@\"\264\004#L\253?\242\373\360X\t>\355?\240\013q\032\'L\354?\245gJ\357\345\213\355?V\021\300\360\346\321\326?\265\254\301\336\323d\350?\025\266f\033\310 \342?c/\035\036C\376\342?\340\200is\274\273\270?[g4\227 (\354?\216n\277\205\035i\343?P\024\264\224\202j\314?\305i\305NM\001\351?f\211\200ol\"\351?JC\334\254\352\247\351?:[\034\374\325\237\345?\030T\200N\013\361\267?8\032$t\tj\316?\236+\"E\2734\351?@-\220\316\350\334\267?\005\201\267\222\345Z\356?\227,\202\223\r\353\356?\3165\200\357:\034\336?@M\253\363<\253\236?$\033a\270|\200\346?\31468\021\365G\301?D\244\211\234\323+\356?\177\036\033\245\232\200\353?Vr:\337\033\021\333?\332\033\234\014\254\027\350?\376\356J\320\004B\324?\334.L\022\263T\325?\270\373\223-1\273\275?(9\263\223\0333\305?\370i\312\276\346\216\321?\352\311X\260\246{\334?lf a\326i\304?\353\246\007\033\"\221\343?0\341\304\001\"E\327?BEx\350n\263\325?Z\036g\365k\355\327?\366\244n\240o\257\334?\234\021wP\345U\312?\350?LV\215\246\271?\333H\030\346\'&\356?\364\365J\302\304\034\316?\224\377~S\242\332\306?\235[\304\231d\036\355?\037S\001\255=\235\343?\270R8\034\275\367\275?\360\301Q\376:[\330?\274\246\237u\370\271\300?%]\036\334\261\263\345?h\363\341U#\213\300?\010\243\255\266\223\344\355?\214\260\'\023\r\004\301?\030Z\237(\322\255\351?\270\216\304o\017\025\263?\t\235\006y\022\203\350?jM\2368&\361\326?\214\223\035\024]9\345?\341d\330\265\276\024\353?\027$\210\314\233\244\355?\006j\3240\326\277\333?\004Y\337\212\251\362\325?\356\272Y\315\363\263\341?\243*\034\223\250\313\356?d\322\237\to\004\325?\222/\201H\362\376\344?\010\226\340>\'\336\331?\250\326\026\000#h\301?\370\204k\322&\314\343?|waB\205\233\322?F\006F7\275\227\350?\236\323\324\005\265\316\357?K\231\001<\340X\350?(\306\204F\246\005\353?\2445s\324^^\306?\200\377\204\343\346w\310?T-\362T\013,\314?\270\3600\205\327d\301?\264q\300\304\032;\345?0mW\263\277\220\260?\004\366\017\014\343\217\322?y\214\2033\355\r\353?T\024\035\327b\021\340?\354\361\354\031\220\237\333? \334\036\343\343\272\355?4g\"D\3047\333?\314U\025\006\016\363\345?o\246\020\255\265!\342?\336\361\314/\300R\332?\023\216\362\002O\211\356?6\242\266K\266<\351?\230_Z\315\323\361\316?U\205&8\267A\351?\346\301\027g#h\326?\2428%\204\331\351\356?\232\227\371\301>\354\337?\240&]\021\276w\235?\354\320w\274\213\237\353?\230\t\020d^\003\352?lE\n#()\337?L\027\r_\356\300\354?\214.\2348g\001\306?\202e\034\013\271\327\325?\033\355\227\014|\373\342??\243\356\252\347\364\356?\226\3073\300\217.\353?|E\330\350\005\205\352?@\032\266\325\036\270\357?\373\332\372Co(\342?\n\025\340\202\315\017\340?>\315\320\343\002\262\342?\200+@q\ty\312?&<\357j\320~\351?\304r&k\025\360\336?`\314\333\207\333\270\331?L-\247 \263\361\355?\262\240\013Ap\211\337?\371@\210&\253\244\354?p\210\346Q\255!\312?b\275\255\332#B\342?\254\2378\252\330B\200\330?,\245\215<\n5\302?\000S\232\240\206\217\234?\3202\220:w\363\354?\276N\236\014\206\023\320?\302\235f\303\304\250\324?\340\273\272\314\3169\320?\020T\037\337\376\375\310?\210\345\233\305I\260\352?\005HFC\264\213\352?\262r\345\234\277;\321?-k&A\027\332\346?\244 L4\365\250\342?u\223\3160\356\004\355?\243\033\272vBr\341?\230F\0068\203\027\326?\200#[\330\233\307\333?\260\233\241\355\374\344\256?\000\224\t\247\271\345F?\000\026\231\200d\231\237?\340\254\236\246\262\023\262?@\213\216\255Q\024\346?\354\004\347/t6\316?\206\263\310%\305\323\356?|(\237\200K\004\342?J\325%\2745Z\323?\352\272m\277\203\t\330?\n\315\332\3045\271\342?\213G[,X\327\342?%t\354B\005\257\356?\244\243+\340S\236\350?\2534)\233\260\035\351?\365G\"\030\202D\342?\200>\310\014g-\230?\340\206\334\353\251\261\231?H\t\000\262\307\271\260?P\370\000@\033\277\301?A\214J0B\271\347?\024Wa\245\027F\346?\200k\350\212\240?\204?\324\312\343,A~\301?\016 q\320\2708\355?\224\2510N\200\254\326?\014\302\306\376E\004\311?W\2642o3\216\344?\200\207|\3054\302\226?\262\221\031\314\002-\354?\030\232\333\237\026\312\351?R\307m\002\2259\320?\200\251\374s=\203\323?\242\347]\036\354E\335?6t\256\235P\256\332?\000|\203A\304\340j?\246\330-\024\364\376\340?\342r\231\307\346\326\350?\262\333\223\351\374\361\353?\212\374\342LF^\337?_\325\267jH\275\351?\274\225f8\211\230\313?\321\330\013~\177[\343?\310\2020\211\033\244\261?K\263\365:\214\307\340?\340\'VOT\\\334?F\312!\200\351 \323?\034\352}\020\026\360\321?\313\300\177\375\336>\342?z\305\210\3712\242\334?P\361\"\334\304\036\267?\3244\372\321.\374\313?\214\230\376\017{\022\300?\3269\2071\334\336\342?ed\215\277\362\336\344?`\310\206F\240\342\235?EQI\'[\177\347?@\021\246\362\235\350\277?\000\021\225\3565\016h?h\354N\017\311q\312?\016\000N\032\241\034\350?4\020*|\322\264\320?\302\267:R\244N\326?\236J\023HK\377\341?\210}\256\257\227Z\305?\360Q\367\177&\235\307?\200I\177h$\320\214?e\014\267\303\220\035\350?|&\377\332\253\337\344?^\314\017\331e\265\354?\320G\201\327\n\363\245?l\200\020\027K\346\307?st(\0060J\354?\200Q\243\332\231o\355?\312\213\263\317\223l\335?R\3378L\347\263\355?\036i\305\334\310~\355?\222\274\273Wy\301\346?\210\217\303b\275\324\304?\365\347\020\371=\306\355?\0201\262\340\356\353\246?\201\3300\350\001\313\340?\316\350dap\024\340?\273\270\037X\263}\342?\030R\"\354u\n\336?\262\365\331\223I\215\341?\234@\226 bf\334?\342\362\031\367\"\232\352?P[\236D&Z\354?\347Cy\340\0347\344?\250\3774/m\335\263?\234\322\232\224\237\321\312?Z\\\034J\372\240\322?\034\306S\217y\346\347?-\251\260\364*\353\352?\273j\304^\214\351\353?\211Lng\277%\344?dH\355}\371&\320?\300\330\321z\363\236\316?\252mK\2049\365\351?`\226\277U\016M\273?5\375WX\301\357\340?\360\030\337*\214\003\255?\3127\202\356\357\212\334?A\361@\360\032\262\344?\301\312\037x\016\323\356?\200\324\317+\342\271\265?-\221\203\306\030\266\353?\324\035]N7&\312?\206Y\316\273\250\247\323?\275n\254\236X\213\350?8\263\326%z\356\266?\353\273\276\374X\026\344?{\000\257-\366\276\352?\337-\257\240\026.\352?\030\177z\273jT\266?\270\0133\256}v\306?:\010\225?\t\206\347?\242\206\260\272\220&\333?\020\234\250\256\016\227\311?\235.\333\003\254\263\342?\330]\\\177\263K\307?\230\003J\247\340\343\355?\007O\033\226\261P\356?\005\314\346=!*\343?,St\002\344\374\301?<\321@?M\347\356?\010\364]O\311C\340?\210Q\353\344\361\300\301?!\017\'\276\303\345\352?\242\016\217=6\272\327?\022\0373A\022\235\345?6O\253\017bj\321?V]\2574\226A\322?\257\027\211o}\336\340?\376\014\n{i$\330?\220r\001\253LJ\307?`\203\247r\237W\267?\216\005\253\366u\327\353?\320\023\204\300O\305\267?\324\347{?$M\337?V;E\256\025N\350?\210\014\300\330\361{\307?\355J\336fmm\344?\201\332V\034c\352\356?\306)\3256\265K\342?\010M\377b\340\372\300?\220\264vH\300\003\341?\\\367Y+A\n\315?J{\232u^\034\322?\375\247g\337\031M\347?x+\337\274;\240\302?\202\202l\3517\225\337?\307i3\361\320\314\355?0\265\2227\264\275\306?\320k\323xNg\340?\241\377 \334\276\335\352?\264\017\274<\266\000\301?`\314\321\001\177|\240?\250\227\225\207\024\r\264?\204 2\272\200V\326?h\364.{\272\027\263?x%K0s\303\266?8$\024\357\264\307\320?\376~\247%\351Q\330?\352:F\263k\262\323?`ok\262\227\316\264?\2579\305\2036\217\343?\034\226\205\303\205c\314?Z+\351U5\016\342?\032\013mUR\302\321?#\226\200\020\365\335\355?Yq\026\331\310V\342?\254\215\026d\'\330\355?\340\300\234\210\250\312\235?\213\202\315\010.\276\353?\212\002\351\363\025\327\324?2.\212\026\375v\335?\254\254\270\364\241:\357?\000\317y\020\033\010\317?E\301vz\360\223\351?^2J\240\203\325\346?g\007\266\207\367]\341?\214\263G\252E?\304?\2022i\274%Z\326?`O\031zy\022\244?\202\331\216\356m^\335?T\337\261\017\305~\324?\030AO\377\255\244\334?\300\303\237s \350\276?\316k\250\275<\224\321?\232\033|c\312\321\346?\265\351\231]\014H\340?\010\250\320\257\361\366\334?\264\0226\032\216k\316?\3109\335\2642W\303?\357p\373[u*\344?\350P\341~<\277\313?\000\005\317\333*\341\225?\332\224&_\322\321\343?\232\376t\345\374\234\326?\232\006\352\21093\335?\353\177\215\206k\342\346?\3600\005\221\302\302\304?B\344\240u\257\367\355?\314*\275()\233\324?\020\024\2560_k\347?Y;\370\263\362\355\351?\034]\271\252Ek\315?\320\322$2\250;\322?\3201\2477\211;\242?\365\016\363\245\362e\344?\311\352`r/I\345?\005WB\340\004Y\356?\370\000\223\354\322\364\305?\322\001\210\nD`\321?lo\222\\=\266\322?\374\372\216\024\343\305\322?(~\247\214\223U\326?\rA\321\222\315;\352?\325Z\235\305\223*\345?t\260G\316\372%\305?H[=\2449b\344? \341\276w4E\240?\226\251oP\376\242\347?\234;\035\333\277\314\341?\215\034b\255\301G\341?:\325\371\340GQ\344?\340\274\035\2558\275\226?\005\275\242\376!\243\354?v%\345\315N)\322?f\232\264niY\330?x\207\222\224\313\231\334?\362j\261\257\025\243\342?e^\214\332\217\266\353?\300\210\006O\027<\343?\202#\203vV\314\336?\260\262\256\274h\205\262?D6\305\032\345\346\344?\000\006yS\302\200l?\252Q\231K\260\211\341?\036\2679\257\336s\334?\260y\242\314\003X\342?\344_\304\005\201Y\303?\202\206\230y\212X\354?G\324*6\310\214\347?Q\276\343-Z\033\352?\300\273\001\022\205\224\210?\030\210 Ud\000\277?\310\223Hl\035x\326?h-\264\224eU\346?p\301\363\273\266\203\333?4\r\037\277s\354\353?\200\223.\003\031\341\351?\025\\\221\n08\350?q\242\3409\343;\342?\271!\366\367\235$\347?\315\227\027I\347\375\341?\316N\030\245`\016\342?\004\000\334\331\376\234\355?\363\"\273\212\232\000\353?\320u\003T\302\356\336?\243\213tM\034n\357?\2405\304p\215\260\260?\201\036\334.\346(\350?\235\223\302\333\346\\\351?x\336\226\256z\234\320?,\240\024\006\006O\314?\367!\254KL\210\350?\010\351\323\227x\026\302?\303\304\361<\240H\352?6\\\323GH2\337?h\227&u:r\272?%\301\346}\230#\354?P3\243\235\251F\321?\355\"\355/\240\342\342?\217\255\373\233\245\366\341?\240X\241\357\032\344\224?\005\373\230yi\257\356?`,\177\210P\320\325?\340\337~\014\252l\347?j\201\307\255\262}\337?O\222i%\267`\352?\200\200\234\361S\305\224?\315\201\304n\362\331\355?D\240\251\023\214`\321?\363\034\335\360\310\025\346?\026\217\346C\361\010\356?D7\263\371!k\302?L\177\267\371\2755\325?\346\014\303C&\242\353?-\273A\343)\306\345?\364\020\221\002\216\002\321?\346\323\223\224\364X\347?\327,\025j\224\202\344?EG\214-\376\334\346?\ti\350\252\273\001\353?4\032J\217S\201\357?\342\244\363To\"\347?;\021\263\312\312\315\356?\244\004\327H\257\031\345?\340\214\002\246\352\324\317?-\215\037^8\000\356?\327&\216\020?\226\352?\351\002.\031Zx\341?\377\345\3641\020\270\354?\217\3230\001+K\346?`\3467\266e\211\222?\240y\017\022\315\370\327?\010J\022\001o\374\353?P\034\264\307G7\327?\037\334\340w\3029\347?\320\334\223\211\363\031\355?\310\177\007\030\007\351\301?\350Yt)*\027\266?5\242\310\370\250\254\341?\272\212j5f\023\327?\300\227\305\321\231\314\240?\215\325\361{\004\t\355?\264\200I\230\n\266\351?\340f\207\200\027\\\353?`\236\276\215\022\320\325?\254v\033H\212\320\321? H\316&1\\\267?9\314\220\201C\246\357?\3407vX]\222\232?\304G\020\241e#\344?\302\227&\237\261f\353?PU0\375\007\343\246?la\237YU\020\356?3\201\337N\200\320\356?\206\224\306gy\334\335?\020R\334\313\233\006\321?\327\006x8X*\347?\375\372\315\205\235N\340?\330\260\006\357\356f\353?sT\265\367\332\010\340?\202\366s\"Z=\326?\322AyH4M\337?\313J\374\271\256\316\346?\374\200az\303\207\322?\231\355n\365\000\303\352?x\\\226\003C\200\310?\353f\005\254u2\341?\006\1772;V\262\341?\214X\221\370\r\010\310?o\254\002\227@\326\343?\257\344`f\021\303\340?b]0\030\331,\352?J\236I\303\376\306\347?\332\006\241\311.\024\330?2\'\317B\221\315\350?\3347X\253\371\300\321?\350\237C\n\305\260\260?8\346,p\230Q\266?\222\007\200)1\276\320?h\356\222ol-\334?`\224u\313M\265\237?X\337P2\227\263\346?\010G\344\222\2037\261?\220\265\357\333X\241\355?\260\266W\343]c\276?\232z\333\236Qi\341?\354\211\005\274\020\321\315?t\211]\006\366e\305?\032\335s\376\322\275\350?\n\336e7X\364\330?\006\245e[i+\323?\364*\312Kuz\350?g\226\035h\216a\345?\330\362\200\302\340\277\347?\200\234/\366\362\016\210?\010\247Jd\034\031\265?\260\036\357\252{Q\260?\262\312\017^\222\236\323?\200{ \311\326\255\255?\204\374<\177\242\326\355?\000\2142\224|\350B?n\241\323\312p\313\351?\220\034\334\311W\343\344?v^\3571\301\230\323?\270\217\261\300z\245\357?\244g\306J;\336\326?\252(\225H\221\023\333?yF\352\334\227\255\347?\002\367\026\313w\203\345?\312\\\331\020\207\255\351?Z\352\351\265\247\223\356?K\202\327(^6\355?\305}\256\237\215\366\347?\267X[~\265\014\357?\310\3634\373!>\306?^mt!\034\246\322?4x\240I\202V\342?d\210\274=\360\230\350?\000\315L-\013+h?\313\367\025.7\271\342?\200F\031e\325\020x?n\375\004o\367\346\334?X9\365P\347\243\274?\376\215\305\222q2\334?\211\021\202\'7\276\346?\274$\250\301\2037\330?!^V\215\031Q\342?Q\350\237wL\253\353?F\205EDD\004\344?$\017\231\r\212(\327?\361\334#\240o\006\352?\374dG\217J7\316?^zP\031Y\355\356?w\177\0144:\366\351?\034\240Z$\372=\332?0\2149C\267\032\312?Xx\325\305Cd\343?\014NR\337\362\035\347?\242=,\201\303\216\326?F\364<\017\014\265\352?u\231\314\3206u\351?\260*\211\276\357\370\203\366\356?d&\276k\251\373\331?\025\223R`\322\375\354?n\027V\006\216\034\353?\3018\354MH\345\354?_\025\027\223\344\321\353?\352:\'\004s\240\326?\210\025\376m\010\t\332?D*\273\246\337\202\352?\200\322\2023\363\232\300?\"\266\364\366WP\333?\257t\230\367\302[\354?Q\315Z0\213*\346?\024\240\334\r\001Q\321?\272\252\224\352\277\021\341?\3006\3438\257\273\255?\352\247{\375\\|\344?x\303\257\266\212J\354?.\022\376%j\270\322?\364\313\233\310\3235\315?H\371\017\232%\213\340?\300\356!lb\337\340?my\221\312\251X\343?\024B%\356I\257\322?t\243\271\036\341\003\316?\263\342oB\374\244\355?\362B\362Kw \331? \035\276\0357p\251?ia\256\246\324\217\356?\260\367\230\365MX\256?\010#H\255\334\245\343?$\303\312\326\032\006\357?k\006\003\362\034/\355?\000P\265I\356\237\347?\356\034\017\276\347\307\344?\220\306#\227\342\032\306?4N\325\304\025\352\300?F\273k\372\033r\337?Z\235\205\013T\357?q\373\252\351\377\360\341?\"\275\206@w\264\325?\376{|x[\353\337? \032\371\250\353\233\257?\037\232\221\224\246\327\341?n?\363\371\257!\353?:\346\250\252\025\'\344?4\3439\177\243*\341?\3247P\2531\004\324?\230\247B\035\250\362\342?P\0223[dG\351?\021\340\300\202L\313\356?d\241O\332l\232\322?R\020)C\351\327\341?\030\266ci\230g\274?\000zL\316bp\305?\377fO \033\361\347?\366\227E\301\tp\342?DoO\222\360L\346?\034\002I\203\320\007\300?\270,\377&}q\274?\346\266\247\251%D\353?\000\300\nj\375i\022?R\214\277\267\246\353\321?\356\257rD\250t\340?\235\220\270c\216_\343?\000.\273\357\337\340\355?\224\010\215r\301c\337?cq\316\217\221j\344?\374\322\367p\245\004\346?\032\220\251YZ\304\332?@i$\277\302P\347?`b\t\300\262\326\240?\216&\032\261\226\332\336?\255\207\265\272\211!\344?\000 \032\334\026u7?H0\034\305SQ\304?\206\242\023\364\307I\341?z\236\342\246R\336\320?\037\r\344\2051N\353?\272L|\255\315\242\353?\376\0169r\314\303\356?\362;W)$\005\353?JCC\035\214\233\326?\230@\327eI(\335?\000\2105\235\241\032O?\000;\367\230\001\221\352?\204af~h\373\347?\374\240\265\332\261\257\332?\244\245O\370\034\312\335?\350\236\027@t?\317?0\374=\335\327*\262?\032\230p\000\215A\325?\346Lz\021\361\374\356?\300n\231\333M\234\224?\346\324(\017\276\246\357?\213/\312\306\363\327\344?PB\376\376\362S\323?\340\367\317\257\220a\313? \261\\]r\344\353?a\267DS\340v\351?\243X\017kK \347?/\337G\032\010<\347?\354\256\317\303<-\300?\342i\235\362\243\315\327?\356\325\327fK\310\320?\330\377\245\336\363\335\344?\364\264)\203\351\253\351?\030p\257j,\024\301?@yW\342\361\235\326?\n\275\302\365\217\270\355?\314\213\201\244\215!\327?\252&\211\213\303N\322?#x\263Tqx\345?Dfy\035\021\336\326?\3165\311\215V\365\323?\024\027\232W\207t\303?\032\017b\0260\231\340?`\356t\330\\\177\356?\206\264_c\312;\326?h\010G\256\310\321\324?\'A\013\304\321\333\355?l\243o_~\001\326?\364\265D\313\335.\335? \315\264.\331I\260?\320\007\224\246\241{\357?h\212\310\315\244\035\302?P\334\256\254\014M\322?P#\013\366~J\304?\370\253\275ey\335\324?l;I\241\332\340\356?\366\010K\260W\026\352?\322\215\251\002\275\210\337?t\036\244xX\253\305?XEO\217\000\034\274?\326\317\355\203i\004\321?$^\035\226\260\241\343?\310\001\246\237\276t\304?\335vP\347\265f\355?\023\007\254g\331i\345?\200\252\257\225](\342?9\276i\340\023\312\340?\212\306H\244A\232\320?\256\220\314:\330\204\334?\233@\351\365\275\304\351?n<\214~]\236\353?\240\352>\363\021\326\327?\036qR\006\260Q\322?I\327,\030\314P\353?;\221\326n\005\253\352?D8\350\251\rN\302?\300\005\241M\326\221\352?\205\232\003d\221\207\351?^E\026\340_\263\340?H\221\233\313qD\267?\370G\224/\215\240\325?\000\265\342\204\307\224\236?\000\235\261h\223\254\233?\010\353\230\014\340\023\266?\017\371\314\273)\024\341?\346\302\331\\\261s\345?\031V\245=T\256\356?\t.\330\004g<\354?\266\030\233\206\017\225\326?q\225\341\310\254\254\343?_\354G\272\034u\351?0\003%\357\220R\304?{\221\351d\253\017\355?V\2451k\316\027\353?\nM\261\345\316\271\324?\035\343\000\355\3700\346?\217\347\216;\207\266\352?_\t\347\250\024&\340?\347\211K\226`Q\353?C\376\372?M\357\347?\016\3506\217\245|\322?\260\032\347\251f\322\331?\322L*\351\253\203\335?\264\314^z\333\365\341?(v01\260\023\265?\312\262\247\rPp\336?\374\344\371\001)\016\303?g\263\331g?\037\346?b%\215\003\254%\323?*\215\256}\201;\350?\217\002\010\365p_\344?\330\350\324\213J[\304?|\337\024\371\226K\315?\314O\371T\360&\335?\024\236T\3408@\312?\264\277I\344\354\271\315?\342\352fPV9\356?d;6\035\221V\350?\237\203\370]Zt\353?\240?R\213\355\271\267?x5\354a\365\313\353?I\306\031Ud\213\351?\256\205\241`\354!\337?MM\300\221\177y\351?\024Y[\014\327\347\347?\340`\301\342|\210\252?\304^.#\266_\330?\\\330\304~v\014\322?\204p\343\361\010 \346?\337\n\"Hg\366\346? o\267\"qX\251?\373\270\317\323,\316\340?\330\023_\"\261\274\273?\363\345\024-\336\001\347?\022\240C\016p\362\357?L#\\u0]\341?\277\231*\244b8\340?/\227\014\213\302\341\354?,\3701]\255\362\315?\260\2615\323PA\346?4\035\240\256/\210\300?\311\243wG\"%\344?\262\001\360\263\377\220\344?\010/4\322\377\267\305?\251\037T\241)?\355?\027\241klD\034\357?\335\216\226\326\t\250\343?\304\000\235\ri\222\336?\325Zc\300in\344?\216\017rt\270\377\344?\252m\002\267\311\021\320?<\243\234\215\301k\301?e\301\277\230\312\214\355?\376\030\255Q\336\230\323?\267\220\027\216*\226\340?\343\225y\022h\"\351?\002#\366w\234.\335?n \t8\345\374\344?H\367e\307\342k\354?\034\231g\221\362O\315?\346\221\206p\312*\357?t\'\005|\371\216\332?J\275\361\200h\206\351?+\243\014\\\201\307\353?\024\201WX\362/\327?xa\213\007\362,\356?\361hx$\2146\347?\230O{\007b\362\331?9\374K\025\212\253\353?t\001\324\027\227\331\350?\320\252\255p\221\377\315?\362u\332T,l\342?26\364\375\227l\336?\200o\356C{P\302?\350\265\006\317k.\346?\274Ws\002\237r\317?\360s\016\"|\245\277?\200\326z\327\217i\237?\350\021\250\003~\022\261?\250\314\250\017\302%\330?\274\254W\025@v\347?Q\177$\236\376I\340?h\271\235\256\317j\330?^\"u\215\327I\325?I\016\313\226\"\323\341?\342\254\347\370\307\212\353?\370\214\203{\340\310\336?\274\226\021\337\250\260\336?\222\216\033\302g{\336?\026\344\227\317\004\332\331?\'H\303\375\261\274\351?\204\204\t\202\210o\314?\\\330\237ko\003\300?\252E\361\005\372n\331?\000\330\304+\2069\177?\021\364\300\017\032\222\351?\365!/\n\032k\351?\304\200\316\247\257\260\322?\316J\301\201F\024\330?\033#\251\360\327/\350?p\200\2724Q\303\246?\007[\311\332\'9\341?H[\330\255\215\317\260?N6\305 \2330\342?\020rVx\302\"\244?`c/\246c\214\276?\360\301\204U\007{\242?\320\036\013\237\013\332\243?@\260\346\355p\301\266?\221V\263\347:\235\343?J\355\r\325\034\\\341?\301+]\311\013j\345?\327<\214\210D\214\355?\030\272=\246\246\030\340?fW\365\017\304\225\346?\2345a\250jA\343?\300\334X\"\3120\253?\2003\307T\370N\351?b\225\264\265\237\250\330?F\271\261qx\227\321?\225%\362\026#<\353?\354\'\332\236U\331\304?R\303\264\363\305\244\351?w\2174+|\200\350?\326\207\031O\311\216\354?\200Pn\311\362\005\254?=)\355k\261\243\357?\250\353\005\317\020\352\264?\217x/\370\235\364\357?\260\014\204kzw\246?\":\370\224\026j\343?\245\355R\3352\225\343?B\306\271W\255\372\325?\335|\373\tZ#\357?\344.\207v\300\337\301?\360\202\020\021=7\254?|{4@;@\312?n\31366\365#\345?0W\317%\320\337\257?\027\231\254y=\277\354?2\241\234nZz\332?\236\222u\361\013J\350?\337\306\"\003k\216\351?\030*e\202*\024\354?\000\261p\215\377\201\307?5&Z{\220\245\352?\300\320\350\002\007t\211?\306\331wG\224\237\345?\000\231\261\372G\'\270?u\263\357b\214\201\343?^\213\020Q\321\367\327?\032q\227\366\246\017\351?\360}\017\344\024\t\354?\356AV\237.\244\343?Q\261\032\212\177I\355?\210D\021\032\371\240\312?\224\256\256Rs\202\341?R\310\'\272\315\t\321?\317\210dI\274P\343?\255\212q\037\230s\357?5\010\0273M\021\341?\306\355\206\252\253.\357?\220\003\344\177\222y\261?\264\356u\023]I\323?\350\336e\355\232\213\342?^\237\257\316\370^\332?H\343\301\264\365\233\342?\200\252\217-g\243\250?nr\334\361\317\205\355?4\337H\217\252\227\301?H\330\322\300}\006\262?$\235\333\267\211\324\315?\314&|b\276\014\307?\220\013\334\274\313\337\300?U\2773\022\240\343\346?\236f\022\201.\367\353?\320^\2075\350Y\306?\000\013|\014\257\255\350?\300}\354\345\301f\260?-\361>F\032\362\347?\216\304\320\027\361\332\336?g\355\275\024\312\253\351?\000i\262HF\321\216?\346\377\231@j\335\332?\020G\037\177^t\250?\340%j\007zt\303?\207\345-(\231\204\346?,\001\347Gv\376\330?\0172\362M\267m\344?@\262I\344\364\261\223?\030hV\360n\200\333?(\271\271m\324\350\316?\3162\372U\272\301\330?\014\237d\356vW\336?\327h\022\303\210\371\346?|\313\006\231%\276\322?\330/\2728Ip\327?\347\373k\217!\230\340?0\313\014R\340\346\273?\346\n\374d6!\340?\260}|\340\302\027\270?`\360\341\202#\365\344?\007\375Q\272\307\032\352?\n\211&\037\214\204\332?dk;\221\345\032\321?\354\326\235W%1\337?)8\370\264 \177\351?\374\233\345\320\256\224\353?A\363z\277n\227\354?\262\227\305\204\265\017\350?v\3703Sv\022\350?`\335\221\253\023\203\262?U\331]\245\234\261\350?\3541*,\247\247\335?\330\261b\376Mq\343?{\336%\237Y\000\354?\300q$\225\006r\312?X\274\312\352&\265\320?\200\230\027\351\223\251\256?x{/\006\230\207\305?\220J\252\232iy\325?0\330\210w\213\217\323? \275\016\3362(\321??f\225$M\341\354?\0331\332~\277M\351?\340\366\273\271\301$\223?@\017\231P+\362\303?\250\021\n\310\253\321\331?\024(\230\206\022M\335?\226o\032\202;T\340?\036cI\n>/\332?JK\271\257\310\014\350?\000\221\343+\'\271\250?\320\230\272\304z\247\334?\363\3172\002bk\354?\340\023\346\274\356\256\241?#E\220\341\021\021\355?K\247\363FA\362\357?\244\014\324AC\021\341?\222\271p\245\207\304\332?@\310X\377p\373\264?\201\274\361I%\340\346?o\304\257\234\\\370\344?\250U\326Yu\216\311?^\211\275\246\261\033\350?\326\373\217\306qh\347?\344\356\254\034\243\332\331?0\231d\320\256\313\320?8QU\356\340\343\314?\234B\326\273d~\351?\200l\rh\336\271\202?\024\367\246j\274\202\355?\373_\361\263\311\204\357?7X\365\233\312\016\342?\330\347\004`\301\242\333?^\035M\202x\241\321?\"kw8\303\215\342?\232\303\313\332(\006\350?.\024\332?g(5\024V<\346?\240tp\367>1\341?g[\035#\241\207\343?\273\021\370\344:I\355?\007\212\261\002\\\323\342?eF\367\205\326\234\341?\360\336\327\236\263Y\354?\036\2408$\345\275\344?ge*ki\213\357?\210\242\305=\207\240\307?\307\341\213\222\251\200\354?kr\226\0374\311\344?\225d\351\226\373\201\355?N\013\361s\"G\333?X\374)\364\211X\273?t\026\310\363N\243\350?\004v&J\267\365\320?\361\314=\255\020\244\353?\327\331JT`\216\347?\000\314\307\231\277z\332?\240\205}\254\360\005\245?\240C)s%h\311?TB\332eT\343\356?8\025\311\'H\210\264?\226\212W\243\201q\346?\234\264`\033`\277\313?e\007\311\305\234\001\341?\016\000\240\0146\371\335?\020\305\213\206\352>\244?BB$\230<\004\356?\204\271\2740\270^\304?\014\025\272k\301\337\330?\010:\3152\3714\261?J,0mc\032\350?7&\307C\212\242\342?Fm\026\377\037a\327?o\366?\005\246\332\342?\215o\275#\270\'\344?0\3231UA\031\354?d\332\222\363\024\241\347?0\004\206 \217\234\306?\2472\260&\264\305\340?\304\002\370\351\031 \356?\207\360\1774U\364\346?\033\201p\222\310\312\340?\302\340|v\272A\334?*\272\261\204\023k\321?\007~\034\255\317&\354?\014\335\315\275\0337\325?\214\344b\372p\017\311?\016+\301\360\230\325\340?\364\254\225\3777\220\315?\274bA\020\2577\311?\340\275(\232\244p\251?\247\2679\317\020\024\355?\320\2662\346\340\224\272?\340\203\342\317\347\227\333?lo\273\375n\006\335?D\253s,#\207\351?@\034\340n\223\276\274?\231\213U\354\301\345\341?\234\201\373\244\276e\357?\3161\0034\370\352\356?\336m5\221\025\234\337?\346\324\233\367\245\202\343?\340%Kb\363\244\263?\003\321\301\020e:\355?\264Q\337\210\372\346\335?\010\333\247\351G+\341?\330\037\014\244\233U\270?\257qo,;\256\347?\212\312\272\201\307\t\321?\246\014a\203\373(\343?0&=\334W\353\251?\303@%\345\301\360\356?1[i\320d\370\347?\030\373d\032\034s\261?`OXL#{\273?N\021p\010\266\333\327?\240\270U\177\254\343\304?\236>\231r\2261\334?\200\354axo#\217?P\247/\026>[\332?\366B{\273\344\237\321?,\330\031\251\305 \312?\002\373\010\316\227\242\326?\272\r1t\177\322\326?\250Q\366m\231/\343?X\300\007\006\246\276\267?\300\200\252\366\362\223\202?\300\200f@Vo\223?\3308:SN\004\315?\000Q\367b\337\244m?\3703\245>\014`\324?\272\203\340B\260\023\322?\275u\016\310\020\302\355?\036f\354\376\273\270\347?\353}j0*s\345?\324\261\3239\367I\343?\206\036\242\371I\221\320?\256\373\266\230\203\377\344?\300\277\365B\007\"\236?\252\216\225\001fI\350?\320\317\207\203\371#\322?\265\2572n\\\307\343?\000\217\034\325\355<\357?\332\004Q\202P(\330?L\357\031Z\334m\345?\304a\315\223[!\352?-\364/\027<0\352?\322>\343\334P]\357?\340@qR8q\227?u\3566\251\307_\347?\300\244\253\3468\204\217?\251\312\320\371\342;\344?\200,\363\306\347\341\252?Z3M\"\001\001\330?\270X\212.]1\321?\221hzr\021\323\351?\300O\006tK\024\230?\372[ _\001\207\347?\274\254gp\244d\331?\325\010\320W\366\026\353?&?Z\343\217\023\340?V>\341\323R\311\343?\226V\362\032\316\372\326?\360\257\333\371\205\235\343?\004\001-q\352\030\342?Jn\244\370\032!\354?\355@{\211\350\264\345?z/\324\321f\202\331?\022kx)\013\036\347?X.\314\325h\004\275? \210\366\363\222\030\254?\344\n\246\255\302\024\303?\376\375f\242\217`\354?\006\271JB\224\223\321?#\266\322\210!\036\351?0\247%<\345\350\262?\345\350\206\346\'l\353?\2172<\257\327\261\346?VP\307S\317\372\354?@t\233u\306|\273?\256\016}o\010O\347?H\307\305)\340\335\262?\324\362\245\327\"\005\352?@\316\245\013?V\345?\357\361\247\274\346\224\340?\000\316v\030,\022r?\3627\360N\254`\343?<\032\315\026\031\267\333?\036X\364\035iF\334?\330\350\356>\370\237\301?t]\242\246\017\037\307?n\261\325\243s\n\321?\340q(d\276/\337?\204W\345\007\357}\352?s\235\"\302b%\353?\300\204\224\037\244!\317?\rMS$\371f\344?X\275\323\303\356Z\351?j\025\023\254\223\235\342?n\241@\203Bh\327?a\243`\020\352\274\340?\013\313O\276\372\"\346?|5\025\200\263\214\314?\"~81No\352?\254^\277\305\3076\340?\204US\252z/\326?\270\314\235\262\305\247\306?\024\344\373\t_Q\333?\034\345\253qH\235\341?\312\257\220\021\rJ\326?0,\211\276\002\367\242?X\323\203d`\315\333?\014\035<\0376\260\347? \t\214M\021D\223?\252\372\343yg\230\335?hPx\364\276\335\301?V\211\322\261\252\270\354?|I?\374P\036\303?\030I{\207j\317\343?\203\377W\216k%\352?k\013\210\361\203\337\352?\262\227%\266\262\021\331?\362\346\\\323o\337\341?\rr\352\262t-\341?\252\273\010\rCk\333?<@\001%^A\306?\253\225\233\242\346\347\350?F*\203P\006\357\355?\246\237\321\237v\026\324?8z\2253\365e\321?ta\246\263\214\206\342?\036\037\365)\214\307\323?]\234\251]z\377\354?\322\232\305\225\225;\326?\3265\003\035=\335\336?\315\256\330\232\323F\346?Wi\207\351\240\247\354?[\262a\'\257\253\355?M\016\362\030#c\345?/-\342\263\275\241\355?_\356\2111\'k\343?$\233\177\r\024\263\341?\232oY\t\267\031\341?\302#4\343cY\344?\220\024[\220dN\341?\"\3778\213\200*\330?\201\002L\324\260X\347?v\351f\325O\'\350?\000\317\371]\331\202j?\330\341\032\243K\376\303?#\023c^\024\234\340?4\217\204_\343*\310?k\230XDU\377\352?\320H\252\302\242\333\345?hBC\364\022\373\341?\n\336\tS\266\244\346?\254\022\2310xF\347?\301a\0036\320\274\341?H\260^\001^~\314?d\2577>\302e\304?\222$\013D*\341\332?\235\301A\253-\236\351?\274h7\t\306L\315?q^zH8H\354?\000\252|\227\007#{?\210$\006\357\234\315\275?\020/\265\037\223\351\325?\327\234.\313\030\260\345?m\213\362l\204o\340?\242\244\241^\207P\354?\016`\3055?\343\341?b\335RE\264\314\352?i\027S\231\265D\342?\024\rcB\200\364\354?\224\202\212!\010\027\316?\340\357\007\276L\326\272?h\227l\003\351\365\303?Qu\242\032`T\353?X\223\263\334o\216\355?Q{\010m\216\013\342?U\244+\320Q\373\342?\020T\331a\321I\326?&\331\t\345\250J\357?\004\201\350\320\260\003\306?\356U\371&5A\352?\210\216\350W\206\203\312?\322\034\211\316h\371\342?8\317\320\326\201O\321?rY\336TL!\333?\267et\300\344\006\343?\0001\312\267\3177\262?\200>/\250X\265\177?\302\376it\010\220\326?\262#\375\220^\257\343?\300SK[\312\014\302?\224,\277 wh\337?1\235\223DQ\247\346?\356\026\212\373e\261\336?\314\200Q\005\031\006\350?\324\211\344*\271\016\326?\2200\241\366\225]\335?\260X\217\356\370F\342?J\337\017\224YM\357?\017\036\276\377\004\226\352?\323C\375\261\274\326\345?\230\272\227\211\363\001\333?\341z\237}m`\355?x\266\034^\246\003\305?\342\313\307\306F#\343?\331\302\222\372\244\346\351?\261\031\370\324\235k\347?\316;\347w\251\323\353?\216R\213\030Cl\351?4}\221_\203\347\312?e>w\3464I\355?\272[<,]\356\350?.\232Z\2466\342\332?\303S\201\360\372\231\340?\022\244\216\326h\033\323? r\206o\332\360\343?\202\"\242\364T6\324?L\356\324\017\0100\352?0 \240\324\313q\313?G\275\026\374\377E\350?h\203\260-O\207\300?\037;t7\256\307\342?.\247{\332\361\224\344?\3107\241L\365\337\275?\320\357\267?\336\277\262?:\370\334\267`\360\352?2n3\013\376d\350?\"\335n\353\360R\334?\2749\304\313gT\303?n\\\211\324O\340\356?\350G\243\010\250n\274?E\207\340Z\030\275\356?Y\300\031\332\265b\355?|\357\177[\032j\353?\020<\345fn)\265?\233\014\306#\312\242\341?&\363\"\331|\314\320?\231-6\002\370\330\351?\374\254\215-Z\372\313?\252\367\350\206\320k\335?\327\224\014|\365\255\345?&u\037q\301\t\340?]\201\001=\307Y\344?`\217\316\374\274\275\330?\210C\320\303a\375\261?\332\2023\273h\335\341?Z\246x\357R\322\324?\007\212\204,\275\316\356?\212L\375\0028:\352?\007gM\274}\027\342?\246,\222\220\235\372\323?\200o\363\205\242\232\321?DGJ~\203\340\307?\313\253\3725\344I\343?\223x\r\253\352(\353?x\271\341\205\2178\274?D\031+\225\253\340\327?rvgK\310\007\342?\361\361\227\271\241\376\350?\006\333\220\354\007F\322?\035\223\264\247kq\343?\216\013\325\272\003\354\342?\3550\216\332\274O\343?\355\023\306\355\252\343\342?\345\264$YN\360\343?\261TC\212p%\353?@\362g!^\265\302?\206\366\027\264\211r\332?\230c\\Wh\273\271?.\262>\244\216s\357?\026y\304\022\377\\\334?\220\225\016\210\037\352\352?\000\200\355QO\255\244?\300\312K\262\tH\240?\010\002\331\215\342\372\304?\340N\313/;P\233?G\350\242\320\364\\\353?%@\244c\251b\351?\204V\236\2479\271\342?\351\310U\254\263\332\347?\362\326\0363\333H\353?\000\211\206\025\244J\330?\302\360q\223\276&\341?\330\323\347\000J\203\270?9>\275(4\033\343?\321\313)\001\270\361\355?\217s\233\211\300\360\347?78\340\235\272\341\346?A\303\004\276\372@\357?\277)\361\034\256\237\342?p;\207\367f\034\267?\036\257,\370\361\220\323?\366\270\250\013a\027\331? \3656L\327C\304?\277\212J\032\235+\344?\230x\220Y\361_\325?\205\2222K\273\330\341?\206\016\265\214\345\305\346?9nC\215\331[\347?\320&\303\262\300\244\243?\237&\325\265\362\240\341?\340\306\360\322\220o\256?\001\321\355\217\002]\353?U\204t\026\023\233\356?>\014p\224\344&\355?\030q\'\223d\375\335?P\271\361\363\213\353?\277Bh\354\245j\344?J\243\034\364\n\322\333?7h\343\244\317\275\340?\330;\2302\351\304\306?@\312\326\362\271s\324?\230%\334Di\207\262?Xc%z*\217\314?p=\275\030XS\250?_\257./FV\345?\265\264R\234F?\341?6\250\237\223\032\242\345?\340 \224V\343\306\317?\236\315\'\016\302f\324?j{\305u\252\276\332? \025\353GS\240\262?\356Y\275\36677\332?\374N\001F\207\327\317?\220;l\2312\261\241?@\243S\265#\321\273?\257\245\267\203\264\374\342?a\347\360-\327c\357?\002\214\254g b\336?\211R\003\003\236\204\347?|\003$(\202,\312?\210\270qj\275\352\307??T~\213N\254\353?\267\020M\271\357-\350?/\307\021\356\231\024\346?\030\376\355\001\226\274\275?j/\345S\277\270\333?\306\036\004\300\212\241\331?\320\213*\351*{\265?\370\253\260}\310\364\301?_\307\266\220\366%\345?@.\014Vt&\343?\020\033\307\305\227$\243?\301w\300\034\3767\357?j\005\305\264w\215\332?\306\303dqN\265\324?T\216[>\307\007\307?\036\026\031\3350\372\336?\316\373\177\200k\243\327?\312\243|\033\200M\334?`I;\003\271\373\305?Z\345\2316:\204\332?VE\365\314B\377\357?t\225\342\324/u\317?\217\317\304\005\350R\355?XF\222\256\342z\331?\000\027h/\236\005f?NS8L\277\231\331?t:\341@#\255\331?{O\255\234t\010\343?\247R\301\327\226\274\343?\026\360\370\336\256\342\330?\002\007\264\2562\034\322?P\3466\271\343]\312?\326`\352-\320e\340?\340K\344R\345l\271?\311\377\310\321\335\034\344?\353HG\276\337\327\350?\032E9\245n\020\354?\246\274\033\023\007\\\357?\226\035k\035\252\213\351?a\320\302C}\026\340?\234ek\001\3018\322?A\221P\341,k\352?\210X\031\025\317\374\346?L\357L\316\376\203\313? \035\314n\247\320\306?\310V\254n\242\361\270?\000AP\355J\345q?\270\225q\326\3314\332?v\303D\242`\250\344?x]Z\371n\206\343?W\372\032\267\353\345\352?$\353B\021\200\"\352?\260\204\364\032\321\177\242?\260\322l\325y\272\326?\001\311 \267X\t\351?pH)e\010\261\334?\240\243\r;\034\377\251?\341\324f\256<\214\350?\213\007\r\367\270_\347?\232\212\300\177\354(\320?f\272M\274\347\207\342?)y\2773\232\373\351?\243:\336T\020t\342?\244W0,\276\247\311?\325V\273.\216\202\342?~\375\002\233\364\200\347?b\240\316.\2358\321?\322\021\352\342Vj\340?\3523\3412\333_\355?\210\nW\264\004X\357?\360\237\330\242\351E\315?]\342{r\341B\347?\352\007r\367\223\363\341?\200\275+[\246\232\231?h#c\031\362<\266?\352C\262\216\030\272\330?!q%\035\275\020\356?\360\244\363\363\254\247\272?\364\0013\310\216\257\304?\326\006A|\325\257\357?>\213=\325\037\354\326?\250N\306:\312\315\345?\260\311\205\303R\003\322?\010\365e6\t\230\326?pd\343<\227\322\264?\250\331V\221&\355\345?5\365F\245\363N\353?0\372\267\373\022\265\324?v\010\275\017\n\326\357? \242<\233{\367\323?YV ~#L\340?\250\371D\304\177+\266?\331\253&1v$\356?\270Q\"L\013\330\302?\317\035t\260\006\337\351?@\262\310{\261\326\212?\233-\260\341Q\372\356?@\252\242\037\215\247\242?\340@\340a\327\315\272?\000 i\263\227\3508?D/g\216\354}\310?\371\370PKD]\357?\202\237/\2605\202\346?\030\021\n\337>\306\300?Ps\350\252P\270\331?\345\204\252Y)\'\350?\200\351\033\263\363\270\273?\300n\226\016\321\240\333?\000\373)\270\230\343|?P\227{7\314H\264?\350\340q\341\256\353\261?\230\002?Y\370\340\312?\330\310)\253\266\024\312?\246\336\237\205@R\354?\224\024\244\313\372\270\345?;\217\004\314\001\323\351?2\212\325\310\032\037\327?\260\370\362\363\356\370\326?.T\276\200\320\333\347? 9\023\202\301\216\353?,S\250\037?+\325?&\244\326\360\276\r\321?D\004\350\332\003J\320?\260\324\264y\013Q\326?Ruay+\033\332?\347\361?X-\333\342?\200F\256(;\263\303?\3007\013\373\352\313\242?\354+\t\354u\365\332?>=Hv\307\345\340?R\344\312(Zc\352?\374LI\360\r\r\310?Lp\214\202a\001\351?h\032\260\013z\243\270?\226\355\323\2129x\341?p/G\360\357d\313?mDT\023\017\247\341?\260=d\364\3472\244?\344L\037*\301\221\334?\200-\367\263t\343~?\363\256\374\205\004\364\356?6\'\240$7Z\357?\270\211\236\313p\245\340?\246\263\242\377\033\340\331?\270\327\357\317\2026\313?$u\331/ l\347?k\r\304\200\"\032\341?\262\346\333\315\267\n\354?\2440s@\235\014\335?\007\273\351\336?\342\353?\323\223\344\234\037\241\343?\226\203\205\223\002\213\324?~\210\002:\331H\354?\220\025\256a\360\272\351?\313\013-\350\307\020\345?\234\312h\311\035F\311?\006\030\030\014\204\230\320?\2247\237<\241\023\302?@6\223\306\350\224\326?\354\021\232\353\362:\320?ZI_\r\255\323\334?\312@\202#A \357?\010f\344!F \334?aM\205>\334\234\355?\276\016\301\337\361&\347?\200\351n\247\372\314\352?ahh\355\356\323\354?X-\266o\221\250\333?\240\332\036\333\317m\224?F\210\277@A\001\325?\204\234W\272\254\200\347?\336\244o\315\327\030\327?P\246\337\271\353\036\277?\364,\215w\214\221\341?~)}\320\334E\332?\240\000)\010\347\n\226?H\250\3631IJ\272?\366\037\246`\330\337\326?T)\240\303\260\345\302?`s\270\022\324<\237?\005l\264W\270&\353?B\366\214a\342G\340?\274\363[|t\276\316?\277\0102\026\323y\347?\274\333\205t\034\372\333?\300\264\374\216\300\360\241?8\200\253\3626j\264?\032\371d\360\246\237\336?\310\216_z\256m\351?\343E8R`[\354?\276so\240?\037\320?\200\017*\325\325\355\335?v\034{\024\016\245\321?L\007\024\276\273\264\340?\006\0376\264\276\366\330?X\177\222\"]w\332?\'\345\t\304\314]\352?\266j\274\245\327\306\346?\324\334i\001\210/\350?\206\347\177\254&\347\320?@W\307\322=\314\247?\300h\010\2364\324\336?\371\335\314\301\355\270\343?l\324\375\247\325\273\321?\024\264OZ\257S\304?d\310L\317\003]\312?mt\263G\t\273\344?\270\325\223\222&\260\300?\234\375\320XH\024\301?\333\022\203\372\343N\352?\362i\017\034\035/\350?L\267r\\\226l\300?\004\r:\273\366-\316?G\267 \252\363\024\343?\3707\242\224\316\361\344?\310ba\322\353\326\316?`\341Ga\245\234\270?\200\032U\037*xu?X\245,t7z\353?\2411u\37133\354?\260\304s\257\324\376\326?\214\363\213\310\324\242\355?\224 \346\372\243\233\340?\267\333:\336\021\307\354?1r\277\"\n\260\343?\256\223\204c\272+\320?\254\214\023l\373\231\302?\000\373\377=\026(\266?/\333\'2d\237\354?\337\303\3629\231\202\346?\256\3066\367\361\241\343?\250\364\251m\205H\314?m\025}\214\037G\350?\274\226Y\243eY\301?fP\271\203\206\252\343?\312\217h\331\317\353\322?B\277\300\370\377\234\327?KcA\302\235\017\352?b\013T\324\310*\342?\334E\235\343UJ\2450p\333?L\265D\356\376;\340?\306\362\2657\0347\327?\301Fx\000!\365\347?@\030u\377f\374\226?LD\254\335c,\357?X\306\177\211[\007\310?\260r\361j\311A\300?\024\326}Y\271\025\356?*\031\\\240\336M\327?\376\356\260\013t\235\323?\232\027\235\256\017L\353?\327\2063\307\211+\357?\357\367\270\023s\304\346?\360\336\250\366\n\237\324?\014\225\363f%\220\354?\367\006 \212\003h\340?@\262t\303\3213\222?T\'j\263\365/\323?\370\314B\355\337{\267?Z\036=F}\231\327?\2148\200\306\\\010\316?\024\004.\234\205f\335?\305\220\032\\\033\350\340?\200E\216z\rQ\311?\346]\270;`\206\322?`\t\242Lb\246\344?j\364\212&\263\304\326?\361\347\273\262\030\330\340?0\213+\037P\034\311?\004\324\352\315\007r\326?\254\313\206g\356\371\313?\020T\234\021\225\206\261?\216\270\005]\267 \353?\373Z\317\202\031\033\345?\351\241\317\006\320\253\342?e\247\224\351k\254\343?\216\204n\034.v\320?\334\364\250\344\201\205\336? \330\036\227\003b\347?tEb\272\020\036\335?q3(\212\302\264\355?d\357\332\252\265C\317?w\243\245;\343{\352?p\365\004\246T\252\256?\350\351\010(J/\264?\300\206!\265?\217\235?\001\337\344\307I\341\353? #\274Wj\363\337?s\316\204$\022\374\342?\024\2572[c\327\355?\200v\246\360\001\225\221?P\245\032\313\210f\352?\226-)\351K\032\342?\364z\325\304\335\220\340?x\201\304(\345|\300?F\202\244,\241j\336?Pv\360 \'\204\327?\364V\307X\354\036\327?\260\301\217\007L\331\260?\356bj\221\330\365\331?>\332\271w\343l\323?\201\214?Y\223\225\354?8\255f\327\247\377\335?\343\3752\245ZH\357?\224\177@\010I\236\317?\261\260O\263Qu\345?9gVY,\364\355?\257\350\014;.\377\354?\302\313\244\3200f\322?\000\032P}\"l\304?v\340j\302\376\224\330?\321\214\374W\363\005\343?\246\210\366R\375\311\330?\300\000\315\210\233\375\323?\372\0136\342.\374\345?;\275\n\235\004N\356? \265\207Im\304\255?d\035\257P\032\261\317?^\343\251C\241\301\341?\nH\232\263\346\314\336?\027G\t\230=w\343?~w\377r\212\244\350?:\307&\314\2544\331?\250\242\254k\300\006\260?Jz\270\025\356\'\352?\344Y\247\211N\232\334?\314\304*UU\333\335?(=1\204\342\247\274?g%\345\"\013}\342?\220\251\321\364`_\270?\000\216.\362CB\225?\2778\203f\230\356\345?\300\002O\231t\340\272?r\003\345\037\241?\353?8\002\347\005y\243\304?\014\224am\240\377\320?\361\345H\004\035\303\352?\300w\027\360R\026\345?X\223\351l\214p\302?\020\006\301s7h\335?\032\275\335\305o\244\347?\257=A{\355\020\345?\337t\203\"\014\215\354?\230S\342\363\020\214\356?@\250\023><\033\344?\\\233\245\2062O\325?hl\277\350\362 \265?8\361\000Z\2639\306?3,\364\017I$\345?\007\003+\2019\026\357?V\021\353\346\037\030\354?e\306.3\017B\341?Txd\026\267`\316?K9TP\356\270\351?.\374\312\356\321\344\353?\\\226\222\330\311\024\350?\310zHH\003\245\332?_\222\361\006\333\t\356?\372]j\250\352\256\346?-x\004:\027\032\341?~\362\271G\335\324\354?Q2/k\311\212\352?\001\244\271rb\361\350?\220]\274zC\231\317?\254\326V.\301%\331?\014\265\023\332\212\203\351?\203\025N\213\223\253\354?\234\206\361\033\312m\303?\24400\322u\223\310?_?\253\035\227/\345?\207\260\372\321\027N\351?0\30356\255a\247?P\256\317d\302\324\325?-\360\343\356\267I\357?\2024e.\316\216\322?\242\250\271\304\236R\347?\031\253\207Y\030\030\342?\343\3557\337\001\304\354?\240\275\376\037\0327\352?\260\'\374\336\357\331\260?\212\376\372~\246\330\356?B,\"\317\247\245\346?\000\201\342\354\365Sf?9\006^]V\254\353?\241\301\212\313:\371\344?\336\357\3409\250Q\335?\355M\262:\353M\340?@z<~\335\355\251?\224E\216\322\220\212\320?5\367j\357\352j\344?5\251\0103\007\336\340?\200\001_\254\362\321\232?dP\342VH\347\344?]\261\202\006\357\334\345?\346\240\336I\022G\321?r+sm\232\340\354?\rl\357\325\312\244\352?\247P\2368\203\250\343?\247\017\257klt\357?\343\367\337\035o\t\346?\000q\246\r\344\271a?pe\342\336?\346\251?2\302\334\301\312\310\352?\334\330\017uPq\304?a5\277\264\215B\353?\275\227\370\303r\030\356?\241\035/D\201\036\347?\2270\327\026\020n\340?\232\216\361A\017\343\324? \006y\315#\021\256?Qe\n7\007\"\353?Z/\033P\310\020\343?\214\311P\\\337\377\354?h\000\222\366]\221\346?qR\376[;\340\344?\234\217M\006\203\371\357?\200K\300?p4\224?\312\307\367\013q|\344?(ha8\005\027\264?\005N?\006G\213\344?@\177N\236\354x\276?\006\022\177\263?\026\355?@\027\335\206d\377\347?\214p_\004\216\037\357?4\332p\230\303\203\340?\2255\355J\023\004\354?\374]V&U\253\307?\272\324\022\035\013\353\347?\304\277\210\206_\277\311?\273\217z\224\231$\351?\236AZH\212\310\342?\272\307\350\247!:\324?@xB-\227\265\350?Ez\202R\001\003\356?T\034\260\033\2258\305?\000\035\255\310\221g\264?\230\346\027\344\366\351\341?\320D\033BX\203\321?`f\340\023\245\201\342? \027\234>r;\247?\tI\033]\302+\350?\3403\274\242\315\312\224?\026|8\010t\321\337?i\314cP\346\245\352?\222\301\371\025\007f\326?\261\nQqN\212\346?\374\207\255\217\216Q\335?\000\344\222$\2054\205?t\367\354\265\263\273\345?\234\234\022\013\004\177\327?\272\224q(\372\242\332?\222\334\333A\252;\351?\237A\310]\246f\346?3\010H\234\261p\341?\020oYK\212\224\336?\200\323\302{\337\341\302?\232a\352p7\331\336?\266\314a\\\234\037\322?\031\210@\210\216\t\344?\220\216\325\235\007\302\310?|\277\214\342i\207\327?\001\356\321M\372\217\345?\"UzZ2\014\320?\2326\311-\0028\351?\340\221[1\305\356\225?\331\204\247\370DS\353?\254\214[z\2325\357?\200\236\031\036\225-\213?\213\241\334\277\240\315\342?\321\0069\036\\\334\352?b\254~\310\214\254\336?\220\330\364\237Y\210\306?\364G\210\237\317\372\326?r\361\207\372\244\220\354?\334\346A04\250\317?A\343\361\332GS\346?\374\033\032\313q \324?\241\025\2739,}\353?\351\302)\341\343}\355?\227\206\20104\200\345?pA&\014;A\254?p\261\246\034;\345\315?\030\006\\z\227\360\354?zwqg\224\214\346?\002\372@\342\374\273\321?\256\003\235\302\360\201\336?H\261\267\256,\350\327?~\260\344\271\214\214\347?\270\346\325g\331j\302?\366\334V,V\331\337?\3605\335X/!\255?bB=\323ku\330?\275\277\304\344e9\351?\214\326\3635\374\223\310?\210\300\002A\332\371\331?\371\023\265x\247T\357?\300K-\214w\265\302?0^\233\251\023\363\302?\354\240\223\352\247\021\343?`|\262ml\317\324?\212j\345\020n\273\344?<\361\3717C&\301?\314m\253|.3\305?\250\007y\343\241\036\342?\354\316\371pm`\300?\363\200\037]~\241\354?1\265;\262\300\312\342?\000\316\0175\254\363\241?\031\0021\037\363:\343?r\235\037\302\365\036\333?J\250\330\256\320r\357?\340l+\031\314u\261?\322(u\3600p\357?\266:\312\324QZ\340?`\267\204\252\274c\223?\222\245\325\341]\234\326?b\016\221J\323e\330?\244\207\262\210\360\027\302?`y\301V\200\272\313?\234\273\'~xz\340?X&%\260O\032\325?\217\305\252dN\"\343?%X\356u\355\200\357?\311&\352\226\375\250\356?\224\253\310q3Q\320?\304]\222\022\201a\347?\214\222:w\270-\330?\234P\"\315`\006\337?K\362\317\027\371\324\342?c\263uWG\322\350?~\nY(\030\335\335?\260\027\232\220\374\323\274?\344\277\014z\310\232\327?\2704\377vE\037\262?hO\224=;\035\300?~Q\353\242O\002\332?p9\001b\222\205\320?\"\037\361M\255S\333?5K\031\377\361:\355?\353|<\242M7\340?\263\235\374\013~\016\343?Z\340^\341\230B\350?\340\261\021\212\304\270\343?\023v\226p\374\001\350?\332!\014\273\351L\335?\217\032/\025%\002\342?\330\345\013\230\336\355\263?\266/\215)\351\231\325?\177F\355\256M\005\341?\372\2070hU>\321?\017\355?Xb\244\347?\322\034\242\203Xp\322?\354d\261\363\020\312\357?\000\016\t\345\344{\246?\302\271\320\376\261\266\337?/\036\327L\343\371\341?4\"Y\246G9\312?\250\000n\013\271#\331?$\322\244\251\033&\322?\252\310\r\310\350\030\322?\362P_\322\2072\341?\336\351\0228@x\321?\240\356\306\014Zh\320?\303\342\217\352\352\207\341?@\0075\252\233\237\271?\367\311\366Y\222\033\345?(\0342>v\017\330?*_\277\001i\204\332?h\036\244\376\367d\335?\256\254\331x:\000\350?\3149\032\317wI\354?\005\037sC\224\025\353?\266\344\270\330\014\010\321?(\025\344\030\370T\315?\334\356\352\323\006\267\316?\370\"\013My\213\265?\322\004,\005\302E\353?\212\207\220\233b\364\334?}M^\235\035\207\351?\314\252\312_\250\217\316?\262 \264\337\241\235\343?D\355J4\251^\343?p?#\275|\034\250?L\257\025\301\354\314\342?\256\330~\340@D\334?\303\353iD\334\212\356?Ru\007\251t\246\336?4\2216Zc\224\320?xm\264\224\202j\323?B#1\314\353<\353?<9\365\300\207\032\334?\234I\362\2544w\302?F\370\275\325\202\377\334?r.\203^\262c\350?\300b\314\352Z7\217?\020N\227\347\252\016\343?`\030\352\344\324?\300?i\t\311\214\246\324\353?\310\353\335\202(\\\352?\204xD\205\364\362\336?[\315H\002#M\355?~l\211[\305\222\337?\272m\"\347\\\265\330?hw!\333\225?\320?-\331\2724{\253\352?\326\210X\365\213\275\333?&\226]\214\241\227\340?9q\247m\275\023\341?\244q\325\255\276\305\334?\"\202R\201\037\233\322?\340\245GB\257\027\270?X\177\254\302\235Z\303?\366\236\\\235(\206\357?6\351\377\342\323S\336??\352\233\313\226\033\344?\210$\263\tKi\320?\243 \206\320\303\212\342?\200\217\224\250L\266\252?4K\247\330#\277\333?\370O\303\351i[\337?\300A\246\023\324w\313?\275\354X\274\210\353\345??\343\020\320\344`\346?R\'\322\326\255\024\334?\340\332\375\031\010\264\326?\347\023;\260\213\032\351?\234\262\223\361_\231\341?R\304\343\3037\347\334?\020\303\321m\006W\252?\300p\235s\231N\277?\335C\237\206\333\244\351?\250\017\253\272\210\254\345?\257\307\224\306\306c\340?.\266\004\204\245\004\342?5\244+~\026]\341?\230\210\203\237\\\343\342?\374\3470\264\2327\305?\210\005\242A)\210\341?\355>{B\204\324\355?\272\2325\217\274\010\323?\252L\216\303sZ\340?:t6\347\037\312\351?H\315\367//\300\320?/.\'\201t~\354?\016\r>\246X\310\331?h\266W\256-\277\267?\214\275\244\237\361|\334?\342\356[Wz\'\347?\352\016\302G\224\234\325?bq\027\304\262\264\347?\210\037VF\016T\353?av\314\246\224\303\342?\"\023\026\254;\374\332?\325C\241\034Xx\343?\247\261?S\010\324\357?P\304\265\247\250\007\321?$\357_\\\201\021\347?\230zO\371}T\313?\320\r\325qcq\243?\370\210\234\362\263\277\343?\230$V\374\266\235\320?\305\262\237(:T\357?G\370\322\225\302\334\346?\364WZ\262h\326\330?0\nC\322\325K\356?Al\303z\272\016\353?\0249\230\224\017P\331?\"\362\377\235\347\221\357?K.\n\\|*\354?\267w{\236]\277\345?\200;\272\302\270\242\241?\274\263\362\274\033v\320?H\203\342\350\267F\330?\372f\276\366\025y\352?u#\001m\"\001\353?\342I\343\365r\227\344?~\033\317\007\272*\352?.&A\262\231a\352?\270\303\237\302t\257\345?\242\313u\3202\\\341?D<\353\267\345\364\317?\376{\372\361o\354\324?\022d\327\n\263\343\332?_\277\243\263S\035\353?\270h\'xs`\351?\300\026\013\365\307N\252?\240R\351\202\240\272\325?\320)\221kZ\205\343?\214\207\362G\331[\334?;\271\356\r\205\022\350?\220C\035\261\326c\271?\220\373\031a\325\273\304?V\031\354w[\357\355?\304\030c#zr\336?\000UO\304H\030\341?H\302\206\t\345U\346?\273\006\357\020d\255\346?\034\327V\357\241\306\343?\320\324\330\256\216\260\347?:\262]x\332\331\321?\210\002x\247\365\025\322?\274\221\375\301\245\240\311?\271\370\223\335 \236\355?\273\341Qn\010\352\347?\344\366\001\035\250\275\355?\3309V\376\031\362\260?.L\005\000Z\271\340?\\\232\330\010M<\317?\031\376\205UW\370\346?\000Y\014\370\356\232\204?b\211\222S&\374\343?\327\010xs\343\n\343?\202J/H\330\310\341?\367\255\362\010\313\037\351?9\332Q#\224(\357?~\266\020\252F\224\335?2\335n\026\021H\357?\370\316\'\235\373S\304?B\304\035$\371\352\335?\260QRG\2245\254?\200U\362\261\221\372\201?\377\275t\031\302J\353?\354V\321\005\017C\354?\326\r\303\336\275\362\341?\263\201\375#\246V\340?\215\023K\036\016\350\345?\271t\217a\356%\355?\343>\327v\263p\355?%,\205[?\345\350?>M\210<\237\251\341?B\222U\376\036v\345?<\275m\230\0008\324?\200\351\321haM\233?\274\356\351rVv\340?\030\257f\332\265\330\267?\034\345\177\034\3765\303?\371\273\010\r0\272\341?\270qn\266\036H\311?n\336\005\250!\002\327?\230\020y\242\371\013\332?P\033f#Q\024\321?4\016\241a\302\"\320?\2607\271\241}%\326?\260^\321D?\234\257?\330\0010\014\242;\356?e~e\267\245>\354?\212/^\025\220/\345?\ny\013\276\217\214\347?\032\n\373g\330\250\321?H\377\202\016\025\204\323?\030\256\374\321U\370\322?\202\3205v\240\036\331?\370\026\351K\231f\314?Pr\006\030\304T\303?\022N/\315c\354\321?\332\275|\247Q\350\330?h\352\225\252\364V\267?>X\260u\313Y\355?\250\036\267\357\3450\351?\'4\210\007\257S\354?p\333\260\342\3646\326?t(\334o\375\371\306?\037,d\0131S\354?N\203\r\257\224\205\340?\204\265\177\255\2248\343?\231\251\017-V\321\356?\204\375\207\363+\033\341?\270\326w\366\234\270\345?\364g=\340\235F\327?\270\351\035E\t\271\320?2U3\210M\372\332?\220M\247\264\307\324\351?\367\333[\001\225#\341?\233\030\236<\267W\354?d\0321>\274;\314?\250\t\213\350\343\033\337?\226\247\014\243C/\327?@OM\330\277\353\205?\230\3702\366^\322\344?\020\352\262\253\260\223\302?\232\\\3403qJ\354?\370\375}\002\243\330\352?\036\0324\332\177+\326?\206\037\256z\2754\353?\342\274D\004k^\350?\230Q\351\354\361I\344?\275\006\031\236W\206\344?t\365\313;\246,\304?(\264\320n|\367\306?\306c\362\354LJ\350?\210c\353\"\247\370\261?`\300R\312\327\361\273?\031\361\355f\303Y\340?\001%\336\363\272N\355?\350Y35/\256\337?\321\177\377x\335/\345?\216p!\2760>\326?~\336\n\343V\355\347?\213\211$\267>\270\343?\320\003`\261\037\202\355?p\357!;\242\262\313?X\035\270D\307b\312? \205ls\367\316\305?) \tx\275p\342?OnN;\026\261\355?\200\354\330\236\244\266t?\314\316E-\351W\301?oc\231\224%\264\352?\022|\356\t\332\307\341?\367\373\\$\302\242\342?\344\"j\311g\373\336?\257NW\314\315`\344?\301,|\031\317\242\345?\306\020qo\003\313\323?9\366\010\335\200w\343?bu\202?\217,\357?\206\210\266h2$\330?\260\'4!$\216\247?\374\036\245\200W\035\334?\327\267\361>Z\267\350?h1\'\006%J\264?\300\326\241\334*{\300?\347n\010]D\217\355?PZ\001\332o\225\351?\320^\361\272\232F\346?\036B\035\241\'\375\345?\260b\200\253\246\255\335?\315F#\032F\364\346?\346.\327!\243)\323?\275\305oh\036}\340?\240 [\036\235\037\250?\260\373\034\352~\014\357?\177\025\007i\305\001\355?\000}D\235\364A\237?\300\261u\035t\276\210?p*\035~\224}\272?,\201F\033\262\034\325?\336^e\237\0161\323?&\376]o6s\356?Id\204\017^\311\341?\210\251\204\200\341\240\337?\220n\201\364P\355\273?\254\233\307\023\207\006\332?\n\225D%\006\014\330?4\376A\023?6\310?\302:\343m\366\225\347?H\334\025-[\240\301?\222\321\344\217!\n\327?\244k\347w{=\337?\343\270Rfe5\341?\3112\314\352k\302\341?0\264*\237\275\274\320?P3\213Qn\237\311?\200\314H\017\246~\257?\276y\363\n\232l\356?\342F\205\265)~\342?\356\332[\373\223\231\325?\354l+I\260\323\356?`\324\243\265\177r\330?0\365\371\245\353\204\241?e\374\316\373\356k\356?d3\310jNd\321?4\261\253\312zU\336?6\207H\232\226@\345?<\213O\221_{\326?\263B\317\021\354\036\345?\nu\213A8K\322?B\354\342\010\231\003\343?\214\034\224\264\016\217\330?\240\205\333\"K\346\234?\337$#\341[\330\342?\034\317\313z\221\200\312?\355ms^\337\004\353?\204\351\014\031\207\365\304?:\356\003Z\021\303\342?\377\000V\333;\343\347?\370yNa\374P\323?\304\013\324-\353 \311?\273z\235\016\224k\346?\252-\216\275e`\320?b\334\220\022&\325\325?\270]\025\365\264d\331?\nf@d\337c\344?\200I\374\203\245\347\252?`\360+\025m1\333?\253\320\373m\356?\220\222YQ\234\223\327?8\206(\036\223\353\320?NG\304C\346|\327?\226\004q\255\3724\330?\224j\030\2032\344\321?\013\324\225\033\260\002\347?2\3571W\314\t\326?\372\272.\230`\\\335? \031.\235\026\241\221?^\201\356\323!\332\352?h\305\343\2133\341\277?X\362h\003\345/\277?\302\036Y\201\022d\320?6\254\000-\351\240\330?1\321\355\363\247\005\357? 3\371\006\314>\344?\255\343^\247\223:\341?\350\366\232\t\307^\350?\000\243\340\354\354\031g?n\"2>\265\367\332?b\346\302\324\377\204\351?\276Si\351\177\250\326?2<\003pRH\326?s\211\303\315\367\213\354?\020M[\230\246\313\340?\350\337\306\332\347\315\341?\214\217`\233\025G\306?)\365=x\2027\342?\375iB(a\322\340?\246\306\327\346\227\267\326?VX\273f\274\216\337?p\345\300T\014P\344?\240\0077\267=\010\333??\2215\310\"g\357?\032\326\3745z\242\334?H\347\343\320\036\345\262?hco\030+\351\306?0\036\255\375\320\031\251?\006\337K:\371\023\332?\242\376p<\256v\325?\246X\021\020\267\217\354?fT\244\225S\204\355?\000\230\252\215\026.\312?\316\262\200\251\013x\320?\032\324l\234Z\002\353?U\311\331\033\2176\340?!\206&T8]\357?\246\205\000\322\304x\344?2\017\347\312\372\242\322?X\334\312S8\335\345?V\214\031\230\033\316\331?\340\274\362\213\213\021\251?\354\313\027n\232L\306?X\336{\222d\276\311?\267\331\257U=\246\340?*\031\371\317\0011\332?\310~\216\212e\205\260?V\326\330\207\034\211\333?\366\033\304\367io\335?\236\264m`\014\007\332?\266]_Gc\350\353?L\373sg\244\230\303?\372\225\307\220\305v\332?\014\364m\017j\313\301?H\313\304Cp\343\326?\321\237d[\215\t\350?\255|\276:\202\204\354?\262V[\246\354\352\332?\371\037~\317u\037\343?x\331\244^e\217\277?\2668\3250\347W\342?<\241#u/\314\354?\333\266\361%y\345\345?>4(\202\364\010\347?\360\223\274\254 \020\271?\354\374\023\216\211h\302?@{\233\264\001\242\341?\325\020\3014\243h\345?\243\025\225\316x\266\341?B\272\312:\224/\320?\274f(\013p\370\317?=]>m\247&\347?\232gwz\367\252\337?\210\225\30133\306\346?\26294\317\347w\347?$\302\331+\030\036\335?\222\213T\312\375\"\330?\301\214\251ns\202\346?\324\207q\221\237\034\332?c\272\020\253>\244\357?\226=G\361\221>\342?\316&\t5SO\356?fh\321\205\333R\334?\200\271Q8\026\242\223?l7\263\2559\200\322?bUp\336Kz\345?\306t\214M\322\036\332?\200(\230\306\251\346\321?\344\3239-\343u\357?\3003\362\341\217)\217?\203\231q\346A\021\356?\350jK\374\307\370\343?FD\240\000L\213\323?jW\271\300\372j\341?l\353\370&\304\347\327?\271}\035x\304\235\343?\253-\001\201\221\372\352?n\035\223\023\261\270\351?\347\360\323\275H\276\345?\372\022;%$\362\343?\031\2466-$\204\346?n:\021\256yl\336?\004j\266\365\301\205\335?\034\3367\215\373\251\300?w \006\327\220?\342?.\377B\365L1\330?\004\336\031T\230@\347?F\370\244\336\216%\353?s\366k\020\317\216\345?\270\325\253\365\3450\341?\214Uv\222\305\267\345?\201\2779M\312K\354?\265\2636\347P\000\343?\000P\334\326\030\252\212?\376=\371u\211\235\335?xP\316\331\201\341\306?9\263\376\017-\002\343?\220\267\002\370\333\016\316?\350\313\250K\006\352\327?\334\017\177\341\004\315\302?\227\244\323\366\353+\343?\327.bD_\232\357?P\353\235\250\201t\277?\243t\243=j2\357?J\205\301\273w\252\333? \235i\2427\027\266?d\312\366\240\030\024\302?\351\211`\316\310S\340?\220\205\021\300\221\253\264?\236\316o\321\365B\332?\351e%$\201\250\342?(\007\333\021\236\251\267?\227\362\247M\247[\350?\324\362\215\361\365\331?\313\036\255DV\022\351?\014\000z\2762\367\326?I\027\240u\252\363\342?\221\344c\303\205\344\343?\314\004n\365\026\307\355?\234\020>\300\314\216\351?(\311\256\344i\017\347?\326\201\210E\0062\327?\227\226\020\\\323u\352?O\334\025\005d\322\352?\020y\311\320#g\330?\267\220d\226!\314\357?\360\260\252\313J\020\335?\340s@\357 \277\336?\030\341.\304D\312\306?\377\241k\326+\254\342?}\r\227\244e\332\340?\224$\373\013#\357\345?\006\326/\326p\031\354?\200\022G\270\"\344\234?\030\210ZDn`\313?\232\'\n\201\001\232\326?\306\201\034\237\255\216\321?\320\3468A\305\026\334?9K\032j\347\033\347?\350(\314\312V\213\340?`P1eA\344\275?\3310\177\2605\225\351?\020-\370\264b?\354?\322Y\032\354\010\270\331?j\312G\226f\364\331?\340\275\006?\017\204\332?\206\344Sj8i\342?x\260\335W\314K\321?f\365\033\232\254\315\330?\300M\327ZF!\341?\030\035&v\336\206\277?\360\274\350\354\261\306\347?\334\\\254\330\004\260\344?\213u\302%KB\346?\332Bq\300U\246\337?X6\032\353;l\351?\340N\214\203\301\301\352?\215\211\222+\020\206\341?\357\227\016H\017\304\355?\\:\2372\006\336\332?\336\231\361D\023\032\333?dB,\221\201=\326?\275\375IZ\274\247\350?t1\310V\212\332\306?\021F._ y\357?e\204\263\372$6\344?\357tE\221\223\033\350?\214\335\032\034\326\247\342?\373-\025\013{\203\351?p\0164a\250g\326?\017O?Aq\320\352?\300\247e\333\026P\262?j\277D\345\000B\325?l\n\206\302\3236\341?\002\253\334{(\222\331?\010f!4\007\341\330?\001\277\264\301\r\217\353?|\006m\006\333_\343?}R\234\347\017j\344?\030\357\177\275f\331\300?D\212\362\334\033\377\341?\007l\256r_\037\346?-\270\177 /\001\340?.\002\253\001\365a\350?r\2251;\213\026\354?\344\336\340u\333v\327?\210\320\361\237\236p\322?:\315\224\314\217J\322?\241\360y\334{}\342?j]\275\251\253\031\340?\260\201\227\341\344\001\241?\304\301+\210~\241\320?\340\212E<\021\207\231?\243\273|\316\336\203\353?\016\000\r\014\322\006\355?^\260\331?,G\360l9\235\345?s\335#\302\210\211\354?\200\n\323\\\\\370\235?\210\033K\337\350\243\313?\206\361He\370\236\354?\206o\232i\255\231\321?\215\326X\tY\031\353?P\314-\366\201(\305?DK\022U\200\230\351?\231\327o/7\254\345?\331e\253@a\"\342?\360`\274{\240\240\351?\344\240\236\201\032B\303?|\003q\364\0330\304?\006\320x\336\257\006\321?P\240\203\276\3065\323?\235\375\275\014\261<\350?\330}\002f\374\301\267?`\273\3008\241\276\356?\344\232/\323,\214\343?2\005\\6L\330\330?\264\302\2019\336\222\323?\017\274\325\276\323\275\357?H\337\273\235/\246\342?\320\236/s\2624\302?`M\204\323z\027\347?O\325\304\250%<\347?\231m\016\001\276\035\346?\254\230\224\310\317\177\322?\027\023~\315y\367\350?X\333=\356\374\017\321?\202\345r\006\307}\357?\372\303\245vd\343\331?\322\025\331\252\013\274\323?\220\224{]\374\023\276?\034\031\272\375\256\324\334?\214\327b]\347\211\321?\322\n\340\367A\317\352?\356\r\323n\325\344\326? =PQU\216\220?aFv1 |\354?\330C\023\000G\336\327?\006\210zs\010(\354?d\346!\237\207m\311?\0240\316\272c\307\315?\2633\231b\025E\340?\304d\364L\246\244\342?\214\332\217\001\037\232\314?\234YG\335{\270\355?\020\312\367\031$.\242?\222\261\"\207\rj\354?]\221`\034(\\\343?,6haG\027\336?\354J\027\327\3707\310?\010\032\250-\347\006\346?l!IBFP\327?\265+&\251F)\350?\010M\263\243\264\035\261?\331\377\354\223{W\351?\000drn\204\213\230?*\232Jr-\232\320?\322\273\007\326\001\334\332?4\027J\034mW\320?\360\245A\225u\327\261?h\001\365\021\024\003\347??\374\205\340\003\242\342?\340\002\210u\266\337\327?\272\250t\352\234\343\337?7c\236\373[b\356?O\036\367Uo\261\346?\000\335i\005\"\035\211?\212\340\322\232\305l\335?\340Z\256 }\007\300?\260\230dD\215\333\261?\030\313\250\251.L\330?\366\020\010\030\234 \346?\3667\010\244\263\234\352?\250]\260\026\035\350\337?\026\367/7df\322?K\274\336-;\352\353?\247\353L\364Y}\351?\200\305\301r\217\205\212?\356\2117$\315\307\342?\000E\377\250\016J\243?\236\225\321^\236\276\333?\024\267\003\300\332\311\306?x\204\340\272\223e\336?\360T\027\177\264\330?F\330\255\251\253)\340?\256*\354Rxp\340?\314\272\362\313F\017\323?\220R\031\263\256\031\314?@\223\305\264\235\371\207?R\333\303\234\003\026\330?\2075#\324\372\273\357?B\274\210\022ob\323?\006\316e\232\317\304\346?\032\216\250\020\371P\357?bl\237\177\366\023\325?\004\252\013c\246F\306?D#\312\363r\365\342?\032\3547\004\n\027\333?4Kyf\227\337\347?\356)\271\021b0\323?r\270pF|\246\357?\026\316\352\375lN\346?\3304p\230(a\305?\\\342QTZ\215\330?\314\367\302D\337\317\305? \010\002\316gx\324?<0S\000\242M\305?\rK\016\347\271.\355?\035\r\261\263\227\310\356?\201\352\234H\002;\350?\0246\346$1\332\313?T\225\265\252\031i\311?}a\276#\246\321\345?0\325\300\242Z\030\335?\330\2360\\\247\312\265?^\\\257Sr\275\332?\222|&\247\310\316\352?^\032\363cS\347\343?\344|K\273\241\213\340?\254\305[\232\377\350\337?\231e\332(\347m\342?\212[\325\214\320i\333?\304\356\311\326/\252\311?\340\263\216+\253\355\316?\370\3531Q3\217\315?\213\331\227\030\276I\352?\013k{\240\340\250\354?\270^{\253\243\003\301?\001\246H`/L\357?x\210\354\344\002\345\313?\036`\271\365\'\330\353?\240\2255@gf\234?\2622\026\341E\230\331?\312\032\021D\356#\345?T\376\027\341\3337\315?\3749*n%U\302?D\373\330\027\270\205\356?ZH\264\005q\351\357?P\013\326\3709\354\357?{\221r \345i\356?8C\237k\017\353\353?\225\025\010o\246q\352?V\274\362\342\014/\334?\270\234\035\245\230j\330?\336\365G~\341C\326?\340\016\215]l1\224?4\277\265W\214P\331?\354B\277,\205\177\334?\300\222V\276\035\236\232?\325p\213bR\033\342?\216s\367\250\272\n\343?W\246\271e\\\272\346?`\234\016\257 \246\343?\354\216E^C\226\323?\224\273V\204\3728\307?\250\376\205\364:K\276?4\224:\236\206\035\302?\")\367\315\323;\332?\351v\327\264\250\035\344?04\316\201\230\270\303?$\0009@\257\317\300? \212\245W\227\206\270?SY\030j\335\021\344?\020\310\374\266s#\354?`\305d\003\231\267\270?\020\021\230\363\341k\347?\222\216@]0\n\324?\241?\313{\222\r\355?\340\347VN6\007\241?\273\371t\216\274_\340?_\222c\303\214\230\354?\010\007]\332\377\244\315?\200\211\257b)\177\347?\30411\375X*\352?\343\002O\032O\t\344?\270\372\027k\377\351\303?\234\003\353E\352W\326?!\304\341\210\326C\351?~\177#\017A\354\351?\270\204W\307\027{\326?\340W*\203\207\302\304?\372\274\253x\327E\323?\026\365U~{^\343?\313y\243^b/\351?hy\274\262e\340\356?o\352\364\304\247w\344?\270S\202)+ \346?(\206\227\005\302L\310?\210\262\371\216\345o\315?\200\323\300\363\265\'\240?\370\244rG\306\251\327?<\026\372\202r\355?\220,\313\000\267R\241?~\362\305\370\2124\331?\205\340\305y]4\343?@Q}\227\365\007\313?\270\004\035\255\270\'\317?\345\023\206%f\347\344?+\023\017\362\232\317\351?Z\244\372\343a,\353?\242\305+\276*z\334?Pf\"\037\302t\344?v\254\2459\251\303\324?\376\273A\n\340\234\353?\270\224?\254F\311\352?:\333\265\312\357\334\347?\270\023\234\343%\342\313?\245\004\373c\215l\357?\230\004\016I]\342\303?0\337\024\372\263#\330?N0\202>r\245\327?Z\2361\252\217(\322?\354\262c#\365\231\316?\322\033U\263\020`\337?\260\376?\273\337V\356?\260\330sd l\335?1\363\333\027\372\365\355?\234Wm\021\333\364\336?\215\222\010\032\024#\343?\363TK\342td\342?\353TZQ\373<\354?\346\230\014\213D\317\341?,\251\213\357\253\340\353?Xz0\225\266\224\354?\237\225-q&\236\346?\213I\367>MY\355?t\341\236\262\240\232\333?\020\004\3626!\235\306?\006Fx\264\355\360\344?\220;\307_7\274\271?\276\257\201E0x\327?\022\254\024\034\371\260\336?\256\302\310M\253\010\352?$D\251\235\317\211\300?\354DX\207\314\000\304?\234QM\304\341*\314?[\365\272\t\265C\356?s\222\006r^\304\341?r\263\363C\326\001\354?\334D\314\213E\256\317?<\253C\017+\267\316?\200tT\024\262\231\341?\233\261\257Z\323\231\353?\324\\\014\336\350c\341?\244\323\243j\340,\303?U\353\024.\237{\356?\332\220@\215d\347\342?3\030\n\356Z\244\340?\363\036>\346?\203\252A)\303\010\355?d\242\257\264\203\251\353?\006RNq\317\332\355?|\326N0\374\251\356?&\"\240\303\305\205\335?\356\352\313\202\370+\324?\240fE:\377\317\336?\310!\236T\355\354\311?\346\016\314R\341f\347?\000f\223\301\230\207\325?\370\246@-\273a\301?\375\017\037\"\373b\347?Y\250(T\031\223\345?C\035\335~E\354\356?\364\310\256\023\244t\334?\035\006EnjM\346?\314\313o\024\276q\342?\333\345B\310\262\237\356?\336\"n\013\311\211\330?\222\221\320\323\212\262\351?B\230)9\321i\352?#\223\305\347\002\303\352?\260\210\336\342O \251?\264\366\036\354O]\327?\306\303\364|\014\\\327?L\345\374\376Dh\330?\022\272,\021\300\030\344?\333\207\021\350E\241\357?l\022Q\351\001\263\353?\256\r\224*\305\253\357?\245\361\376\252\2364\351?\\%\375\277jz\355?X\261#9G}\271?\\\337\225\324\361\374\352?\332\351\275\360\272X\356?\372\354X<\314\213\334?0\r\361\233\377\263\256?\263w\237\177\302\246\353?\336\251\336\214:U\346?\234\373:\013b\320\310?j`AH\221?\326?\340S\307v\347\320\234?\220\204\200\033;\014\272?c\272A\262]\014\347?\212S\241\331\244\024\326?:4\350z>M\345?v\234q\206V\007\347?8\306s\036\276\021\344?\234\237\275\244\234\026\355?@\342o\361\301]\233?)t\326\343\211\251\340?\026\377 `&\326\334?Q\204\214m\327\030\341?\361\303\321\231\233\304\342?\232\001I\005c.\337?\000\230\375`\3251\217?\275\364\344\315=\304\354?\2440\353\346\340\230\336?T%\277 \366\266\337?t\014\353\270\240\017\341?\370\331{N\363\010\262?\310\210\313\212\222=\271?\200\0348\203\230\016\272?jv\276\024\350\215\341?\350:\332\226\004\232\312?^,\177\206\036\376\332?5\247E\376~\365\346?\025\037\341\2045G\346?\202$\331\014{%\351?\370\2775\243-\331\324?\026\204n\306\315S\351?\324\300YZ\250\243\353?4M\355\2641\033\345?\200\370a\362=\244\276?\334J\233\221\021\'\305?\t\272;\204\004\312\343?\300\3701\263\371\243\331?\336\212\353\370\337\260\321?\312E[\320E\312\341?0\314\356\302\2419\311?\004\365\260\204>\024\317?Z\256t\177K\260\336?\250\030\357\342\352\254\272?\300\220\0322\026w\320?`\no0\r\236\265?\237\313X\0315\204\352?\262\342\250\374,+\341?)\026\233JTS\350?88\326\346I\214\311?\3461C;\310\353\347?\347\003\0338\357V\350?B\262\353\344q\371\343?\246\251}\365\337\277\337?{Z\013\265\336\237\355?K\026\026\010\306;\350?|Z\377\321H\261\356?\302^U\027|\303\327?&\261\214\031?\324\347?\247\365\214\017E\t\341?~\316\202\313o.\356?j~V\220b<\354?L.\025k\372\177\325?\232MdS\246\355\352?\301\360\371;\217\026\346?\300\003\252t\265\341\357?\370\303f\226d\245\320?\262\"#\317\320\251\343?;\001\003b9y\355?\300\242\272\027\262\203\316?Z\000e\217=\357\327?\3775\330\266\241\270\340?\322\n\343\367#+\342?\273\344@\000\240\374\344?\000\210\030\213\327!\253?\273\3337t\0245\344?\232\010\371\320\317\003\332?n\202\375\035\222\365\330?\262\252\276\204\033\240\337?\341p\325\347E\024\344?\214N\202?Q\206\334?\306\227\304r\237\370\350?\330\004\330YeE\265?\361\264i;i9\353?\006\253\t\232\302\200\356?\207N{k\241?\340?\336\226E\377\215c\342?\205\034\355?\266a\340?h,\272\240\303\344\352?\300\025)\234w]\274?\235\254\003\013\032J\345?\246\227vR\235\330\344?^-\246\2434\262\333?xi$_\212O\321?Ms\341\200t\036\350?\375\277Y\251\317c\340?\250\256\003\"!\274\354?\261\302yW\367\375\357?\350\035\207\234X\231\352?\340\347\206\202\212\344\354?\tb(r\302>\340?\335\331+D^\325\347?\300\215|\021\t\006\315?x\334\374(|:\325?\034\221+\250:\005\355?R\351\030pdO\335?\032\266\314\350\360a\352?\360o\335\351\016s\245?\323\221\372W\035\242\357?Rh\316\276\216X\345?}X\372\271$\327\350?\231\310\n\022\330\346\357?e>\241\035l\243\356?Xx\260\026`b\266??\205\270\016vq\341?:\331j\2622\214\327?\252\024\033\035\371%\327?\240o+\001\266\247\233?2\200\355\004\325*\352?\273\212\177Q0\356\350?\215\234W\3479\347\345?\272\005\321\",O\336?4\013\315}\017\323\323?\375\264\205\375\215\346\344?\373B\317\324\311,\352?\2309\211Q`?\342?\346\223d\206\350\210\331?q8+*X\300\353?\213S\363\274g\211\347?\262O1\"\026\324\357?\020L?\\s\214\337?$\276\227\340_\001\341?\33087\212\216\240\265?e\264\361^\335\372\354?d\346\270\233\010[\334?\247\222\246(\267\007\345?L\375\210l\001*\306?\234\241\363ld_\324?\030\311\036\363\332\002\317?4\367=\002>\000\331?\214?\002ts\234\336?\256R\330\211>>\352?\236\375j\200\346W\346?`I\032\'\'\346\352?\367ed\372\3404\352?(h\202\231\342N\336?\360\250\303zk\200\354?\252\027\016\277\354\337\345?x\371\256\227\340\333\276?\273v}\226\241\325\352?\302\202\303Y\304\241\353?+\335U+\312J\353?\345\330d\324\204\353\340?\2666c\211\215\016\345?Ot\231\253\345\300\351?>\341\353~\370\037\336?\315M\331\374\355,\347?]G\350\205\244\373\354?\362/\361\223\367\340\352?`\"\203\001\202\354\242?\243e\nS\346R\340?\201G\025I&\346\341?\224_Z\300\232\014\355?\245T\016\331\355\014\352?m\244*\3049\374\352?\262V\341\270\350\276\330?\307\315\373\027\364\333\344?3v\321\353\215b\350?\214\315`#\224b\326?\304+zw\032\372\354?\257\036_B\256q\351?\201]\3102\026\272\353?H_Z%\343\277\261?v[\224\326rp\335?@X\300X\254\320\302?\007\337\207\251m\033\341?\022%^A|S\327?\t2\364@ \014\344?\212>\313 /o\323?\234sK)\352\200\320?r\201\234\320FE\330?(V\327\014\224g\276?\334+\370\377\3476\354?\330\350zi\235+\354?@fB\267\222\000\255?\0348\313\"\335\272\353?\202\325\224*\021\251\355?nq\314\201\237\231\323?\364\371\330\301F\341\350?+\205\022\220\"&\343?\2501\207\264\306\355\320?\026}\305\225\220\361\332?\340\370Q\274\371\356\277?\034\t\370\206\032|\340?\336\335\337\3440\345\323?\031\033ek7c\351?\200\260\237\003\314\322\247?>\262\315EJN\357?\334k\001\255\335\"\312?\320\350{{@\267\327?\260\201\327v\253s\322?\204^Z]C\222\305?vx6\320\004\005\322?\000\322\202\231W=\221?\232\306\370\221#\242\353?\226v\304u\006\027\340?D\017$R\014\275\307?P\370v\017\244-\317?\362\005\275\276\353m\344?\335\337\330\253\262\233\342?\032\361\261\211\322\210\324?\343\252\214t\',\357?b\320y\206\352~\324?d3~\276\316\334\342?8T\210a_\032\277?\330\336\230\263U7\274?r\016\303:\005\225\347?8#)+[\211\350?\356\235\244\223*H\356?\357E\333\251\2329\354?\372zQw\251Z\355?\221:\366^\212\335\343?\211V\327J\233?\341?\3029\323\032\252\004\336?\000\314\244\212\273\322\324?\320\340\235\335\235l\254?H\200\210\210T\017\317?p=\233d@6\250?0d\224\r\303\010\344?hr\350)\016=\347?\362a\026\215\317T\351?\340\317\211 \223\323\310?dM\317\351\"\273\304?\260b\351|m\177\330?z\022\212\360VQ\350?\307\025\203b.\016\352?\023\\\370n\334\245\341?\340\026\201\270\246\016\324?]\2223\233\323\347\346?f~\366\r\223^\324?\254\231xm\017\363\331?_^\217\253\276\235\351?`\220\3758\300\277\220?\nU\325\\\204\335\320?\344F\005\265\364%\316?\3200r(\021\267\260?\200\266\321\366\347\036\340? \352\020F\334\232\260?\330z\023\234\371j\273?\322M\021\242\266\006\334?e\322;;6\210\353?\220Q\211\372t\013\324?>\375\274\373\3074\342?~*\262\003g\310\347?.yx\364>\241\341?\220\311h\214\300\337\265?\306`\177S\261\206\346?\320\304g\242\325\036\324?\024\244\030j\252\266\343?o\265\277\323v\317\347?\037\036J\250h\270\345?\240\025\307\357ay\312?h4T&`u\314?^\325\336yL\335\357?\351H\033\271\310\014\353?\363\004|9h\004\340?\323\201l\211\177\223\340?\342\344\001@\3221\327?\374\221\244\250e\007\351?U\326\236J\014H\353?8\214\371FF\356\311?\\[k\274q\260\330?\250\001Vr\356\367\327?\364\\=\351\362V\307?5s\000\356\212\246\357?\000\006@)e|o?\004{\353+o\013\344? \340)$i\275\261?Lq\326\342\340?x\300Y@\016\220\343?\0204B\"\331\332\335?\032J\235\357X\227\323?$w\007\242\225\351\347?\230y\302\3748|\307?\237k\001\346\274\037\357?\270\032@\325\306;\302?\232\365\246\257@0\355?&\373B\265\313\221\350?>\\&\351\177`\345? q2\265\337\023\326?\356\323\267\211)\035\337?\200\306\232\035\360\331\221?h\370\263~\205\305\316?tTQ\037\371s\311?8\265!7\330\340\273?\334\344*\022P\310\332? \266\336}_;\302?\246\360(\037U\346\357?x3\203~D\232\332?[.\010\235\3647\347?\251L\236\216*U\357?,\257\366\2412\240\310?\374ISmn\306\351?\273Z\201\347/C\340?\000\273\330\362\216\317\333?\220\374\335\374\244\027\254?e\206\022\226i\252\342?K\215\240\271\260P\341?tKb\251+\t\304?\317\302\250F\332f\353?\010\001\334\250\207\244\267?\210\253\314\222r\243\317?\334[\'\002\234\374\345?$\346\266\014\314\003\306?\010\3663\004\026\201\332?~\242G\276E\007\322?x&\221`\276\360\347?\366<\242\352x\247\335?}X\335n\216\347\345?9\025\027)j\273\355?Ck\277\307\277\343\340?\314p\253\007K\340\323?\245\tP\203FG\341?\010\250Pp\020\256\331?\350\035\3520\363Y\342?0\261\021\311#\265\264?\241\210+\266\025\213\357?N!k\274\033\030\341?&:\227\202\330:\343?\237B1aPA\355?\302Q\305\305\014\232\341?\214\314\337@\363\304\324?\032\307`\243ft\342?\274tE;\321\344\352?\355X\261d\0145\356?\231JE\337\010\336\343? \357\014\375\332\027\326?\201A7\033;P\341?\374\245\336\36571\323?\331\363\267\317\323F\344?\320FK\177\304X\344?C\357\3264[\245\357?\340\275\346?\344\372\226E\025C\337?\355\277\0019vc\344?\200_\020r\003\037\265?\251t\323\371\256:\343?\034\226\233 \026J\341?\335\020\375d\266\006\345?\240\262\034\321[9\236?@%\315<\030\246\333?\255\307p\334\205J\343?j\317V\306K\251\353?\200<\232T\221\264\222?\240K\3439\273>\224?\260!\203\216Y\377\317?\257\264\026\027V\202\354?&|\241CI\327\347?\022\365L\234\375\332\333?\202\365\337\216N@\345?B\352\000S\342k\327?Bn\022\305\267\274\343?@\t\253K>\017\325?\033\214\023\253\251\226\350?\220)]h\204u\345?\025DzW\342\217\347?$\032\351\331\245t\353?0aoo8\\\277?\274\257\256\210\320_\324? \370\257\257\341\034\255?\024\001h\324c\037\353?\230\346D\006_\304\355?~\032,\035a\"\342?2\355VU\225A\327?\024\306\253#\346\207\345?\344\020\301T\321\230\337?\256\264\264J\327\000\323?\034\304\227\337[\003\304?O<\362\r%E\343?+\035\206Xe\242\340?\320\271\306\270\277X\271?\036\017 \276T\376\340?\200Tm\276\210p\351?`1:\311!$\343?p\\\n\355\007\360\313?\300f\375/Lc\345?\000\037(L\016\020\240?\312;]$\006\311\355?h*\031\275\271p\275?\002H\036\306C\016\353?\002\223\370\316\233\004\341?\221\377(*\202,\340?\375\211\344\0079$\357?\277\233\320N\033\034\345?.\315I\033T\264\356?p?\315\257\212\372\273?\342\3557\245s\307\340?\316#m8\251\344\327?y\226\273n\035\321\354?XPn\333\336\004\324?\200\037\336\267h%\277?B\253y\336\267\273\347?b\226\237\344j%\321?&\324]8tn\323?\354L\246\252yG\344?\203S\'\342\204v\357?=\227h\360>\320\344?\364-\225\271\254V\344?\334\026\005:\254(\346?\350\301F5\335?\342?&_F\257\216\247\353?\220\354\016.O^\323?$\267\'\354{\214\324?\224\220\225p\036\004\355?(\302~\212\n3\304?xP\225\213\204A\271?F%o*d\277\344?\362(\327z?\207\345?\210\024\"\3528\254\271?\304~\037C%O\340?_\373\3159\314P\346?f\310W+i\017\357?N\356\317\233\247\206\341?Qj7v\026\021\345?(c\301\036z\300\270?\212H?\220s\321\332?\345\335E+\325&\350?L\206\340:\236\177\322?\3248\356\202\347\202\354?R@EN\210h\336?8\236\030\032\232\r\277?\210\332\244H\034\334\302?x+\250\373\021\301\313?\260]Y\250u\330\355?\364f\177\244\232\275\312?HR\002\374\215K\307?\367&?ja\354\352?\\\221\320\371\356\217\335?\352\n\210Q\201@\322?\324\267\372\007v\251\357?\304iV\317\032$\331?V\365\304\033\rK\323?\241\017\023\023o\305\350?\254F\374\302\325h\343?\205AI\210\263\311\340?\203\254W\244\026\241\346?\342\213\357\376cb\353?\244\232L\004I\005\307?\216\322g\350\363\243\330?O\010\244X\241\347\343?S\366\033\t\347\361\353?\027\224\335\244.?\340?\210\252\333A\203L\325?\226\316t\020)\226\341?\300\307\027\261`\n\321?l]%\352\3075\316?Bm\274\\\215^\323?\215\232\203\255\216\344\356?\324Q\254\235\313a\336?\272\217\334\013\347\313\354?\000=\316/\262\377\222?h\215X(<3\357?\252\301\004V\357\355\354?@\245\323L\006/\332?\310*\307E\354\031\275?\004\'\203\241\373\355\305?\232\306gy\346\251\350?\312\276O\226s\260\332?:\276\225\000\251\235\355?|\301\354X\323K\352?D\017\274px\231\346?@\tEs\026f\262?\270\007y\233\204\324\331?Og\236\346b=\344?>\323\201\320b>\352?>\376J\237\020}\341?\246\362\2458\016\020\333?\364s^\"\337\217\343?\010\364\300M\351r\323?\211t\310\3267#\351?\024B,\300\035\375\357? _\233\014&-\301?R\373\\\023X\321\357?\210\221\322j\222,\337?ux\220\245;\\\346?\244\353\273\3038\224\315?\343\013A\037I\263\346?m\013\236\302!\224\351?h\371\\\022\224\276\302?\030\327\2062E.\277?`\0045xg\205\311?\020Y\220g.f\251?\200]GP))\247?h\274\362q\323F\325?\020\254L5/\316\262?\035F\275{\340\274\354?\300\323\341\362\377\236\255?\330x\327\206\331\340\327?\310\327pd\345{\342?\010u\272\361m7\326?\254\273\247\245\245{\300?\300\311?@\016\247\254?h\3355\215\037\274\321?\265\307\003\277%W\343?xR\360c+\274\271?\000\212X\026\361o\222?\\\203\277,\303I\313?\340\325\t\022\361t\221?\300\027\312\330\337\020\256?\261)\356l\036\211\347?\260\021\224\216>\024\323?(\n\337k\317p\327?\304:\361jF\037\326?\260d\247l\034\375\342?y\340S\275\0069\357?f\362\323\235dP\346?\350?\341\347J\301\307?d\332(\211\033\026\357?\234I\363\341\372\220\342?~\333z\371N\334\335?\204\316^9\276\210\330?\307\0241\244#H\341?\225\343\215\007\tq\340?\346\340l\000\035n\344?t\337\366\250\2371\353?8\301\331\025\345\325\333?\306&k\266\027o\340?\251\037\345\010\242\301\356?\036\350\006Rn\361\323?\217\341\274\217\311\\\356?f\3235\376f\240\333?\225\271\t\241k\277\344?d:\271\260\324\251\301?\034\312+\203\223\357\326?n\275\004\321A\303\353?\324\305Z\241\010\242\342?\222\363\350}I\312\347?f\324\247gBm\322?\035nn\027\005\257\346?\260\255G\216\272\003\240?\224W\374\033\260\016\345?\340\024hZ>S\311?z:`S\204g\346?\354%Rh\203H\353?\"\033\365\252\213\272\335?xZ]\n\037\220\276?\270\363\360\034\331\023\347?\250\377\\\321\312\301\347?@\263w\267\311\207\200?@@\317\242\022\201\314?\316\346\263\262\213\224\320?\244\335}\255\241\255\357?\317\'#\376q\003\353?v\235\3069\377\257\333?2\265vQ\336\240\326?e\352\350\221\277Y\344?\322\330FL\260\350\346?\263\270>A\245g\351?\r/\\\000j\264\352?\204\035l\265\261e\330?\272\275\244\205\024\345\330?\200\220\261\017\323\233\244?\202SI>\310 \324?8\3134\037\266\353\306?\260\323\002\353\241\274\261?=\244VG(|\352?Y\345V\317\271a\351?,\004\265\316\304N\351?l\177R\323\nv\317?\230\274\034z\230\304\323?\360\260\255\222\355\236\341?" + } + } + } + } + node { + name: "PartitionedCall" + op: "PartitionedCall" + input: "Const" + attr { + key: "Tin" + value { + list { + type: DT_DOUBLE + } + } + } + attr { + key: "Tout" + value { + list { + type: DT_DOUBLE + } + } + } + attr { + key: "_collective_manager_ids" + value { + list { + } + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: 150 + } + dim { + size: 150 + } + } + } + } + } + attr { + key: "_read_only_resource_inputs" + value { + list { + } + } + } + attr { + key: "config_proto" + value { + s: "\202\001\0008\0012\002J\000\n\007\n\003CPU\020\001\n\007\n\003GPU\020\000" + } + } + attr { + key: "f" + value { + func { + name: "__inference_signature_wrapper_13" + } + } + } + } + node { + name: "NoOp" + op: "NoOp" + } + node { + name: "Const_1" + op: "Const" + device: "/device:CPU:0" + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_STRING + tensor_shape { + } + string_val: "\n\035\n\t\022\005get_c\010\001\n\016\022\nsignatures\010\002*\000\n\017\n\013\022\007trace_0\010\003*\000\n\027\n\023\022\017serving_default\010\004*\000\n\021\n\r\022\tcapture_0\010\005*\000\n\021\n\r\022\tcapture_0\010\005*\000\n\002*\000" + } + } + } + } + node { + name: "saver_filename" + op: "Placeholder" + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "shape" + value { + shape { + } + } + } + } + node { + name: "StatefulPartitionedCall" + op: "StatefulPartitionedCall" + input: "saver_filename" + input: "Const_1" + attr { + key: "Tin" + value { + list { + type: DT_STRING + type: DT_STRING + } + } + } + attr { + key: "Tout" + value { + list { + type: DT_STRING + } + } + } + attr { + key: "_collective_manager_ids" + value { + list { + } + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "_read_only_resource_inputs" + value { + list { + } + } + } + attr { + key: "config_proto" + value { + s: "\202\001\0008\0012\002J\000\n\007\n\003CPU\020\001\n\007\n\003GPU\020\000" + } + } + attr { + key: "f" + value { + func { + name: "__inference__traced_save_40" + } + } + } + } + node { + name: "StatefulPartitionedCall_1" + op: "StatefulPartitionedCall" + input: "saver_filename" + attr { + key: "Tin" + value { + list { + type: DT_STRING + } + } + } + attr { + key: "Tout" + value { + list { + type: DT_STRING + } + } + } + attr { + key: "_collective_manager_ids" + value { + list { + } + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "_read_only_resource_inputs" + value { + list { + } + } + } + attr { + key: "config_proto" + value { + s: "\202\001\0008\0012\002J\000\n\007\n\003CPU\020\001\n\007\n\003GPU\020\000" + } + } + attr { + key: "f" + value { + func { + name: "__inference__traced_restore_49" + } + } + } + } + library { + function { + signature { + name: "__inference_signature_wrapper_13" + input_arg { + name: "unknown" + type: DT_DOUBLE + } + output_arg { + name: "identity" + type: DT_DOUBLE + } + } + node_def { + name: "PartitionedCall" + op: "PartitionedCall" + input: "unknown" + attr { + key: "Tin" + value { + list { + type: DT_DOUBLE + } + } + } + attr { + key: "Tout" + value { + list { + type: DT_DOUBLE + } + } + } + attr { + key: "_collective_manager_ids" + value { + list { + } + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: 150 + } + dim { + size: 150 + } + } + } + } + } + attr { + key: "_read_only_resource_inputs" + value { + list { + } + } + } + attr { + key: "config_proto" + value { + s: "\202\001\0008\0012\002J\000\n\007\n\003CPU\020\001\n\007\n\003GPU\020\000" + } + } + attr { + key: "f" + value { + func { + name: "__inference__6" + } + } + } + } + node_def { + name: "Identity" + op: "Identity" + input: "PartitionedCall:output:0" + attr { + key: "T" + value { + type: DT_DOUBLE + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: 150 + } + dim { + size: 150 + } + } + } + } + } + } + ret { + key: "identity" + value: "Identity:output:0" + } + attr { + key: "_construction_context" + value { + s: "kEagerRuntime" + } + } + attr { + key: "_input_shapes" + value { + list { + shape { + dim { + size: 150 + } + dim { + size: 150 + } + } + } + } + } + arg_attr { + key: 0 + value { + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: 150 + } + dim { + size: 150 + } + } + } + } + } + attr { + key: "_user_specified_name" + value { + s: "9" + } + } + } + } + } + function { + signature { + name: "__inference__traced_save_40" + input_arg { + name: "file_prefix" + type: DT_STRING + } + input_arg { + name: "savev2_const_1" + type: DT_STRING + } + output_arg { + name: "identity_1" + type: DT_STRING + } + is_stateful: true + control_output: "MergeV2Checkpoints" + } + node_def { + name: "StaticRegexFullMatch" + op: "StaticRegexFullMatch" + input: "file_prefix" + device: "/device:CPU:*" + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "pattern" + value { + s: "^s3://.*" + } + } + } + node_def { + name: "Const" + op: "Const" + device: "/device:CPU:*" + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_STRING + tensor_shape { + } + string_val: ".part" + } + } + } + } + node_def { + name: "Const_1" + op: "Const" + device: "/device:CPU:*" + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_STRING + tensor_shape { + } + string_val: "_temp/part" + } + } + } + } + node_def { + name: "Select" + op: "Select" + input: "StaticRegexFullMatch:output:0" + input: "Const:output:0" + input: "Const_1:output:0" + device: "/device:CPU:*" + attr { + key: "T" + value { + type: DT_STRING + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + } + node_def { + name: "StringJoin" + op: "StringJoin" + input: "file_prefix" + input: "Select:output:0" + device: "/device:CPU:*" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + } + node_def { + name: "num_shards" + op: "Const" + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + } + int_val: 1 + } + } + } + } + node_def { + name: "ShardedFilename/shard" + op: "Const" + device: "/device:CPU:0" + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + } + int_val: 0 + } + } + } + } + node_def { + name: "ShardedFilename" + op: "ShardedFilename" + input: "StringJoin:output:0" + input: "ShardedFilename/shard:output:0" + input: "num_shards:output:0" + device: "/device:CPU:0" + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + } + node_def { + name: "SaveV2/tensor_names" + op: "Const" + device: "/device:CPU:0" + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: 1 + } + } + } + } + } + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_STRING + tensor_shape { + dim { + size: 1 + } + } + string_val: "_CHECKPOINTABLE_OBJECT_GRAPH" + } + } + } + } + node_def { + name: "SaveV2/shape_and_slices" + op: "Const" + device: "/device:CPU:0" + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: 1 + } + } + } + } + } + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_STRING + tensor_shape { + dim { + size: 1 + } + } + string_val: "" + } + } + } + } + node_def { + name: "SaveV2" + op: "SaveV2" + input: "ShardedFilename:filename:0" + input: "SaveV2/tensor_names:output:0" + input: "SaveV2/shape_and_slices:output:0" + input: "savev2_const_1" + device: "/device:CPU:0" + attr { + key: "_has_manual_control_dependencies" + value { + b: true + } + } + attr { + key: "_output_shapes" + value { + list { + } + } + } + attr { + key: "dtypes" + value { + list { + type: DT_STRING + } + } + } + } + node_def { + name: "MergeV2Checkpoints/checkpoint_prefixes" + op: "Pack" + input: "ShardedFilename:filename:0" + input: "^SaveV2" + device: "/device:CPU:0" + attr { + key: "N" + value { + i: 1 + } + } + attr { + key: "T" + value { + type: DT_STRING + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: 1 + } + } + } + } + } + } + node_def { + name: "MergeV2Checkpoints" + op: "MergeV2Checkpoints" + input: "MergeV2Checkpoints/checkpoint_prefixes:output:0" + input: "file_prefix" + device: "/device:CPU:0" + attr { + key: "_has_manual_control_dependencies" + value { + b: true + } + } + attr { + key: "_output_shapes" + value { + list { + } + } + } + } + node_def { + name: "Identity" + op: "Identity" + input: "file_prefix" + input: "^MergeV2Checkpoints" + device: "/device:CPU:0" + attr { + key: "T" + value { + type: DT_STRING + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + } + node_def { + name: "Identity_1" + op: "Identity" + input: "Identity:output:0" + input: "^NoOp" + attr { + key: "T" + value { + type: DT_STRING + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + } + node_def { + name: "NoOp" + op: "NoOp" + input: "^MergeV2Checkpoints" + attr { + key: "_output_shapes" + value { + list { + } + } + } + } + ret { + key: "identity_1" + value: "Identity_1:output:0" + } + attr { + key: "_construction_context" + value { + s: "kEagerRuntime" + } + } + attr { + key: "_input_shapes" + value { + list { + shape { + } + shape { + } + } + } + } + control_ret { + key: "MergeV2Checkpoints" + value: "MergeV2Checkpoints" + } + arg_attr { + key: 0 + value { + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "_user_specified_name" + value { + s: "file_prefix" + } + } + } + } + arg_attr { + key: 1 + value { + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "_user_specified_name" + value { + s: "Const_1" + } + } + } + } + } + function { + signature { + name: "__inference__18" + input_arg { + name: "unknown" + type: DT_DOUBLE + } + output_arg { + name: "identity" + type: DT_DOUBLE + } + } + node_def { + name: "Identity" + op: "Identity" + input: "unknown" + attr { + key: "T" + value { + type: DT_DOUBLE + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: 150 + } + dim { + size: 150 + } + } + } + } + } + } + ret { + key: "identity" + value: "Identity:output:0" + } + attr { + key: "_construction_context" + value { + s: "kEagerRuntime" + } + } + attr { + key: "_input_shapes" + value { + list { + shape { + dim { + size: 150 + } + dim { + size: 150 + } + } + } + } + } + arg_attr { + key: 0 + value { + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: 150 + } + dim { + size: 150 + } + } + } + } + } + attr { + key: "_user_specified_name" + value { + s: "15" + } + } + } + } + } + function { + signature { + name: "__inference__traced_restore_49" + input_arg { + name: "file_prefix" + type: DT_STRING + } + output_arg { + name: "identity_1" + type: DT_STRING + } + is_stateful: true + } + node_def { + name: "RestoreV2/tensor_names" + op: "Const" + device: "/device:CPU:0" + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: 1 + } + } + } + } + } + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_STRING + tensor_shape { + dim { + size: 1 + } + } + string_val: "_CHECKPOINTABLE_OBJECT_GRAPH" + } + } + } + } + node_def { + name: "RestoreV2/shape_and_slices" + op: "Const" + device: "/device:CPU:0" + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: 1 + } + } + } + } + } + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_STRING + tensor_shape { + dim { + size: 1 + } + } + string_val: "" + } + } + } + } + node_def { + name: "RestoreV2" + op: "RestoreV2" + input: "file_prefix" + input: "RestoreV2/tensor_names:output:0" + input: "RestoreV2/shape_and_slices:output:0" + device: "/device:CPU:0" + attr { + key: "_output_shapes" + value { + list { + shape { + unknown_rank: true + } + } + } + } + attr { + key: "dtypes" + value { + list { + type: DT_STRING + } + } + } + } + node_def { + name: "NoOp" + op: "NoOp" + device: "/device:CPU:0" + attr { + key: "_has_manual_control_dependencies" + value { + b: true + } + } + attr { + key: "_output_shapes" + value { + list { + } + } + } + } + node_def { + name: "Identity" + op: "Identity" + input: "file_prefix" + input: "^NoOp" + device: "/device:CPU:0" + attr { + key: "T" + value { + type: DT_STRING + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + } + node_def { + name: "Identity_1" + op: "Identity" + input: "Identity:output:0" + attr { + key: "T" + value { + type: DT_STRING + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + } + ret { + key: "identity_1" + value: "Identity_1:output:0" + } + attr { + key: "_construction_context" + value { + s: "kEagerRuntime" + } + } + attr { + key: "_input_shapes" + value { + list { + shape { + } + } + } + } + arg_attr { + key: 0 + value { + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "_user_specified_name" + value { + s: "file_prefix" + } + } + } + } + } + function { + signature { + name: "__inference__6" + input_arg { + name: "unknown" + type: DT_DOUBLE + } + output_arg { + name: "identity" + type: DT_DOUBLE + } + } + node_def { + name: "Identity" + op: "Identity" + input: "unknown" + attr { + key: "T" + value { + type: DT_DOUBLE + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: 150 + } + dim { + size: 150 + } + } + } + } + } + } + ret { + key: "identity" + value: "Identity:output:0" + } + attr { + key: "_construction_context" + value { + s: "kEagerRuntime" + } + } + attr { + key: "_input_shapes" + value { + list { + shape { + dim { + size: 150 + } + dim { + size: 150 + } + } + } + } + } + arg_attr { + key: 0 + value { + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: 150 + } + dim { + size: 150 + } + } + } + } + } + attr { + key: "_user_specified_name" + value { + s: "3" + } + } + } + } + } + } + versions { + producer: 1520 + min_consumer: 12 + } + } + saver_def { + filename_tensor_name: "saver_filename:0" + save_tensor_name: "StatefulPartitionedCall:0" + restore_op_name: "StatefulPartitionedCall_1" + version: V2 + } + collection_def { + key: "saved_model_main_op" + value { + node_list { + value: "NoOp" + } + } + } + signature_def { + key: "__saved_model_init_op" + value { + outputs { + key: "__saved_model_init_op" + value { + name: "NoOp" + tensor_shape { + unknown_rank: true + } + } + } + } + } + signature_def { + key: "serving_default" + value { + outputs { + key: "output_0" + value { + name: "PartitionedCall:0" + dtype: DT_DOUBLE + tensor_shape { + dim { + size: 150 + } + dim { + size: 150 + } + } + } + } + method_name: "tensorflow/serving/predict" + } + } + object_graph_def { + nodes { + children { + node_id: 1 + local_name: "get_c" + } + children { + node_id: 2 + local_name: "signatures" + } + user_object { + identifier: "_generic_user_object" + version { + producer: 1 + min_consumer: 1 + } + } + } + nodes { + children { + node_id: 3 + local_name: "trace_0" + } + function { + concrete_functions: "__inference__18" + function_spec { + fullargspec { + named_tuple_value { + name: "FullArgSpec" + values { + key: "args" + value { + list_value { + } + } + } + values { + key: "varargs" + value { + none_value { + } + } + } + values { + key: "varkw" + value { + none_value { + } + } + } + values { + key: "defaults" + value { + none_value { + } + } + } + values { + key: "kwonlyargs" + value { + list_value { + } + } + } + values { + key: "kwonlydefaults" + value { + none_value { + } + } + } + values { + key: "annotations" + value { + dict_value { + } + } + } + } + } + input_signature { + tuple_value { + } + } + } + } + dependencies { + node_id: 3 + local_name: "trace_0" + } + } + nodes { + children { + node_id: 4 + local_name: "serving_default" + } + user_object { + identifier: "signature_map" + version { + producer: 1 + min_consumer: 1 + } + } + } + nodes { + children { + node_id: 5 + local_name: "capture_0" + } + bare_concrete_function { + concrete_function_name: "__inference__18" + function_spec { + fullargspec { + named_tuple_value { + name: "FullArgSpec" + values { + key: "args" + value { + list_value { + } + } + } + values { + key: "varargs" + value { + none_value { + } + } + } + values { + key: "varkw" + value { + none_value { + } + } + } + values { + key: "defaults" + value { + none_value { + } + } + } + values { + key: "kwonlyargs" + value { + list_value { + } + } + } + values { + key: "kwonlydefaults" + value { + none_value { + } + } + } + values { + key: "annotations" + value { + dict_value { + } + } + } + } + } + input_signature { + tuple_value { + } + } + } + } + dependencies { + node_id: 5 + local_name: "capture_0" + } + } + nodes { + children { + node_id: 5 + local_name: "capture_0" + } + bare_concrete_function { + concrete_function_name: "__inference_signature_wrapper_13" + function_spec { + fullargspec { + named_tuple_value { + name: "FullArgSpec" + values { + key: "args" + value { + list_value { + } + } + } + values { + key: "varargs" + value { + none_value { + } + } + } + values { + key: "varkw" + value { + none_value { + } + } + } + values { + key: "defaults" + value { + none_value { + } + } + } + values { + key: "kwonlyargs" + value { + list_value { + } + } + } + values { + key: "kwonlydefaults" + value { + none_value { + } + } + } + values { + key: "annotations" + value { + dict_value { + } + } + } + } + } + input_signature { + tuple_value { + } + } + } + } + dependencies { + node_id: 5 + local_name: "capture_0" + } + } + nodes { + constant { + operation: "Const" + } + registered_name: "tf.TrackableConstant" + } + concrete_functions { + key: "__inference__18" + value { + bound_inputs: 5 + canonicalized_input_signature { + tuple_value { + values { + tuple_value { + } + } + values { + dict_value { + } + } + } + } + output_signature { + tensor_spec_value { + name: "unknown" + shape { + dim { + size: 150 + } + dim { + size: 150 + } + } + dtype: DT_DOUBLE + } + } + } + } + concrete_functions { + key: "__inference_signature_wrapper_13" + value { + bound_inputs: 5 + canonicalized_input_signature { + tuple_value { + values { + tuple_value { + } + } + values { + dict_value { + } + } + } + } + output_signature { + dict_value { + fields { + key: "output_0" + value { + tensor_spec_value { + name: "output_0" + shape { + dim { + size: 150 + } + dim { + size: 150 + } + } + dtype: DT_DOUBLE + } + } + } + } + } + } + } + } +} diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/chunked_saved_model/non_chunked_model/fingerprint.pb b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/chunked_saved_model/non_chunked_model/fingerprint.pb new file mode 100644 index 0000000000000000000000000000000000000000..a092a1978081114d088407d2a975bc304ec9d2b4 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/chunked_saved_model/non_chunked_model/fingerprint.pb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:17045fc174bce3189659aa820a6504885b5f4050438d4022c6c38a0c107098af +size 57 diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/chunked_saved_model/non_chunked_model/saved_model.pb b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/chunked_saved_model/non_chunked_model/saved_model.pb new file mode 100644 index 0000000000000000000000000000000000000000..23ba0b32a211375fc1f4f144728190b12fbcbf65 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/chunked_saved_model/non_chunked_model/saved_model.pb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f640489ea4ba602db52be9e751637e917a6fd8b0827aedd60ab9102bfcb2bfc3 +size 188206 diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/chunked_saved_model/non_chunked_model/variables/variables.data-00000-of-00001 b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/chunked_saved_model/non_chunked_model/variables/variables.data-00000-of-00001 new file mode 100644 index 0000000000000000000000000000000000000000..3e08df4e8f9ee01eb233b8422191f9b0b714ab44 Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/chunked_saved_model/non_chunked_model/variables/variables.data-00000-of-00001 differ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/chunked_saved_model/non_chunked_model/variables/variables.index b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/chunked_saved_model/non_chunked_model/variables/variables.index new file mode 100644 index 0000000000000000000000000000000000000000..2b377c2506ac3e3b6c86ba7baf83c3acbdc97f68 Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/chunked_saved_model/non_chunked_model/variables/variables.index differ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/bad_node_attr/assets/empty b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/bad_node_attr/assets/empty new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/bad_node_attr/saved_model.pb b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/bad_node_attr/saved_model.pb new file mode 100644 index 0000000000000000000000000000000000000000..cb4e3b60480f53f8db2f3ef806394cf6c52aa218 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/bad_node_attr/saved_model.pb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:66d44631e71b047aae2b0c37da27c751efa62c2ec1524892c6dbbf46413c8408 +size 86308 diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/bad_node_attr/variables/variables.data-00000-of-00001 b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/bad_node_attr/variables/variables.data-00000-of-00001 new file mode 100644 index 0000000000000000000000000000000000000000..f1cf3a00bb2e75d22d5d3b921f54c869b235c1a5 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/bad_node_attr/variables/variables.data-00000-of-00001 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c544793f08e424829d0cc5f168379e946eec762086ca7197c37aecdd92d3e0c9 +size 1225520 diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/bad_node_attr/variables/variables.index b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/bad_node_attr/variables/variables.index new file mode 100644 index 0000000000000000000000000000000000000000..7357e8d57edb5fd403e2821211c790526053ddd7 Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/bad_node_attr/variables/variables.index differ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/const_with_no_value/assets/empty b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/const_with_no_value/assets/empty new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/const_with_no_value/saved_model.pb b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/const_with_no_value/saved_model.pb new file mode 100644 index 0000000000000000000000000000000000000000..898d9a657265d4a159610378a851250c1cee8efc --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/const_with_no_value/saved_model.pb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:971e027aaa2ad73924da532e58309cfdda6f70f77aca65be843e2340b5b3b1dd +size 10768 diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/const_with_no_value/variables/variables.data-00000-of-00001 b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/const_with_no_value/variables/variables.data-00000-of-00001 new file mode 100644 index 0000000000000000000000000000000000000000..f1cf3a00bb2e75d22d5d3b921f54c869b235c1a5 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/const_with_no_value/variables/variables.data-00000-of-00001 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c544793f08e424829d0cc5f168379e946eec762086ca7197c37aecdd92d3e0c9 +size 1225520 diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/const_with_no_value/variables/variables.index b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/const_with_no_value/variables/variables.index new file mode 100644 index 0000000000000000000000000000000000000000..7357e8d57edb5fd403e2821211c790526053ddd7 Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/const_with_no_value/variables/variables.index differ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/negative_shape/assets/empty b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/negative_shape/assets/empty new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/negative_shape/saved_model.pb b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/negative_shape/saved_model.pb new file mode 100644 index 0000000000000000000000000000000000000000..9efb08fa55bcb96cea944fef9cf51e0dcc6c5602 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/negative_shape/saved_model.pb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:971cec4e2fd705717d4969ffe14d5376697ef37fbb62bf903d5060b998a7132c +size 10820 diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/negative_shape/variables/variables.data-00000-of-00001 b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/negative_shape/variables/variables.data-00000-of-00001 new file mode 100644 index 0000000000000000000000000000000000000000..f1cf3a00bb2e75d22d5d3b921f54c869b235c1a5 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/negative_shape/variables/variables.data-00000-of-00001 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c544793f08e424829d0cc5f168379e946eec762086ca7197c37aecdd92d3e0c9 +size 1225520 diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/negative_shape/variables/variables.index b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/negative_shape/variables/variables.index new file mode 100644 index 0000000000000000000000000000000000000000..7357e8d57edb5fd403e2821211c790526053ddd7 Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/negative_shape/variables/variables.index differ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/generate_chunked_models.py b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/generate_chunked_models.py new file mode 100644 index 0000000000000000000000000000000000000000..840f8711b8f68c550a97c9caafc8f52d7091be95 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/generate_chunked_models.py @@ -0,0 +1,76 @@ +# Copyright 2023 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. +# ============================================================================== + +"""Generates GraphDef test data for Merger. + +Constructs chunked protos test data containing GraphDefs with lots of nodes and +large nodes for Merger::Read and Merger::Merge. +""" + +from collections.abc import Sequence + +import os + +from absl import app +from absl import flags +import numpy as np + +from tensorflow.python.compat import v2_compat +from tensorflow.python.eager import def_function +from tensorflow.python.framework import constant_op +from tensorflow.python.lib.io import file_io +from tensorflow.python.module import module +from tensorflow.python.saved_model import loader_impl +from tensorflow.python.saved_model import save +from tensorflow.python.saved_model import save_options +from tensorflow.python.util import compat +from tensorflow.tools.proto_splitter import constants +from tensorflow.tools.proto_splitter.python import saved_model as proto_splitter + +SPLITTER_TESTDATA_PATH = flags.DEFINE_string( + "path", None, help="Path to testdata directory.") + + +def generate_non_chunked_model(non_chunked_dir: str): + root = module.Module() + root.c = constant_op.constant(np.random.random_sample([150, 150])) + constants.debug_set_max_size(80000) + root.get_c = def_function.function(lambda: root.c) + signatures = root.get_c.get_concrete_function() + save.save(root, non_chunked_dir, signatures=signatures, + options=save_options.SaveOptions(experimental_image_format=False)) + + +def generate_chunked_model(non_chunked_dir: str, chunked_dir: str): + saved_model = loader_impl.parse_saved_model(non_chunked_dir) + prefix = file_io.join(compat.as_str(chunked_dir), "saved_model") + file_io.write_string_to_file(f"{prefix}.pbtxt", str(saved_model)) + proto_splitter.SavedModelSplitter(saved_model).write(prefix) + + +def main(argv: Sequence[str]) -> None: + if len(argv) > 1: + raise app.UsageError("Too many command-line arguments.") + + main_dir = os.path.join(SPLITTER_TESTDATA_PATH.value, "chunked_saved_model") + non_chunked_dir = os.path.join(main_dir, "non_chunked_model") + generate_non_chunked_model(non_chunked_dir) + chunked_dir = os.path.join(main_dir, "chunked_model") + generate_chunked_model(non_chunked_dir, chunked_dir) + + +if __name__ == "__main__": + v2_compat.enable_v2_behavior() + app.run(main) diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/generate_saved_models.py b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/generate_saved_models.py new file mode 100644 index 0000000000000000000000000000000000000000..496d792ee069a4f32b1659ee19099a32ba5b4378 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/generate_saved_models.py @@ -0,0 +1,144 @@ +# Copyright 2019 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. +# ============================================================================== +"""Standalone utility to generate some test saved models.""" + +import os + +from absl import app + +from tensorflow.python.client import session as session_lib +from tensorflow.python.compat import v2_compat +from tensorflow.python.eager import def_function +from tensorflow.python.framework import dtypes +from tensorflow.python.framework import ops +from tensorflow.python.framework import tensor_spec +from tensorflow.python.module import module +from tensorflow.python.ops import io_ops +from tensorflow.python.ops import lookup_ops +from tensorflow.python.ops import variables +from tensorflow.python.platform import test +from tensorflow.python.saved_model import save_options +from tensorflow.python.saved_model import saved_model +from tensorflow.python.trackable import asset + + +class VarsAndArithmeticObjectGraph(module.Module): + """Three vars (one in a sub-module) and compute method.""" + + def __init__(self): + self.x = variables.Variable(1.0, name="variable_x") + self.y = variables.Variable(2.0, name="variable_y") + self.child = module.Module() + self.child.z = variables.Variable(3.0, name="child_variable") + self.child.c = ops.convert_to_tensor(5.0) + + @def_function.function(input_signature=[ + tensor_spec.TensorSpec((), dtypes.float32), + tensor_spec.TensorSpec((), dtypes.float32) + ]) + def compute(self, a, b): + return (a + self.x) * (b + self.y) / (self.child.z) + self.child.c + + +class ReferencesParent(module.Module): + + def __init__(self, parent): + super(ReferencesParent, self).__init__() + self.parent = parent + self.my_variable = variables.Variable(3., name="MyVariable") + + +# Creates a cyclic object graph. +class CyclicModule(module.Module): + + def __init__(self): + super(CyclicModule, self).__init__() + self.child = ReferencesParent(self) + + +class AssetModule(module.Module): + + def __init__(self): + self.asset = asset.Asset( + test.test_src_dir_path("cc/saved_model/testdata/test_asset.txt")) + + @def_function.function(input_signature=[]) + def read_file(self): + return io_ops.read_file(self.asset) + + +class StaticHashTableModule(module.Module): + """A module with an Asset, StaticHashTable, and a lookup function.""" + + def __init__(self): + self.asset = asset.Asset( + test.test_src_dir_path( + "cc/saved_model/testdata/static_hashtable_asset.txt")) + self.table = lookup_ops.StaticHashTable( + lookup_ops.TextFileInitializer(self.asset, dtypes.string, + lookup_ops.TextFileIndex.WHOLE_LINE, + dtypes.int64, + lookup_ops.TextFileIndex.LINE_NUMBER), + -1) + + @def_function.function( + input_signature=[tensor_spec.TensorSpec(shape=None, dtype=dtypes.string)]) + def lookup(self, word): + return self.table.lookup(word) + + +def get_simple_session(): + ops.disable_eager_execution() + sess = session_lib.Session() + variables.Variable(1.) + sess.run(variables.global_variables_initializer()) + return sess + + +MODULE_CTORS = { + "VarsAndArithmeticObjectGraph": (VarsAndArithmeticObjectGraph, 2), + "CyclicModule": (CyclicModule, 2), + "AssetModule": (AssetModule, 2), + "StaticHashTableModule": (StaticHashTableModule, 2), + "SimpleV1Model": (get_simple_session, 1), +} + + +def main(args): + if len(args) != 3: + print("Expected: {export_path} {ModuleName}") + print("Allowed ModuleNames:", MODULE_CTORS.keys()) + return 1 + + _, export_path, module_name = args + module_ctor, version = MODULE_CTORS.get(module_name) + if not module_ctor: + print("Expected ModuleName to be one of:", MODULE_CTORS.keys()) + return 2 + os.makedirs(export_path) + + tf_module = module_ctor() + if version == 2: + options = save_options.SaveOptions(save_debug_info=True) + saved_model.save(tf_module, export_path, options=options) + else: + builder = saved_model.builder.SavedModelBuilder(export_path) + builder.add_meta_graph_and_variables(tf_module, ["serve"]) + builder.save() + + +if __name__ == "__main__": + v2_compat.enable_v2_behavior() + app.run(main) diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two/00000123/assets/foo.txt b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two/00000123/assets/foo.txt new file mode 100644 index 0000000000000000000000000000000000000000..f9ff036688007836524129e23f5cf82edd1e8910 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two/00000123/assets/foo.txt @@ -0,0 +1 @@ +asset-file-contents \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two/00000123/saved_model.pb b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two/00000123/saved_model.pb new file mode 100644 index 0000000000000000000000000000000000000000..7a03d9ad10b1d6a429e6d41869b2e6388c7cacce --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two/00000123/saved_model.pb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4e250217d66c0e9b532b9384bad32fafb6a286fec5a67e2e457925b994473daa +size 9935 diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two/00000123/variables/variables.data-00000-of-00001 b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two/00000123/variables/variables.data-00000-of-00001 new file mode 100644 index 0000000000000000000000000000000000000000..15b75d6ef6bffc336d138d923badb3928b8c4c13 Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two/00000123/variables/variables.data-00000-of-00001 differ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two/00000123/variables/variables.index b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two/00000123/variables/variables.index new file mode 100644 index 0000000000000000000000000000000000000000..7ec9fb4fe2dd21d0a6c324aecd7658fc37cf2326 Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two/00000123/variables/variables.index differ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_main_op/00000123/assets/foo.txt b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_main_op/00000123/assets/foo.txt new file mode 100644 index 0000000000000000000000000000000000000000..f9ff036688007836524129e23f5cf82edd1e8910 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_main_op/00000123/assets/foo.txt @@ -0,0 +1 @@ +asset-file-contents \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_main_op/00000123/saved_model.pb b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_main_op/00000123/saved_model.pb new file mode 100644 index 0000000000000000000000000000000000000000..e532f9768598dcc8892e88cd7aa28ba14d2a17ab --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_main_op/00000123/saved_model.pb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:56f053f7ebb4694e9029d38ad48033d305581ce8c53d56f18336cf00c94642d6 +size 10103 diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_main_op/00000123/variables/variables.data-00000-of-00001 b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_main_op/00000123/variables/variables.data-00000-of-00001 new file mode 100644 index 0000000000000000000000000000000000000000..15b75d6ef6bffc336d138d923badb3928b8c4c13 Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_main_op/00000123/variables/variables.data-00000-of-00001 differ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_main_op/00000123/variables/variables.index b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_main_op/00000123/variables/variables.index new file mode 100644 index 0000000000000000000000000000000000000000..7ec9fb4fe2dd21d0a6c324aecd7658fc37cf2326 Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_main_op/00000123/variables/variables.index differ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_pbtxt/00000123/assets/foo.txt b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_pbtxt/00000123/assets/foo.txt new file mode 100644 index 0000000000000000000000000000000000000000..f9ff036688007836524129e23f5cf82edd1e8910 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_pbtxt/00000123/assets/foo.txt @@ -0,0 +1 @@ +asset-file-contents \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_pbtxt/00000123/saved_model.pbtxt b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_pbtxt/00000123/saved_model.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..97edf3f7a9a063255a244c496b910a10e9a9b732 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_pbtxt/00000123/saved_model.pbtxt @@ -0,0 +1,2709 @@ +saved_model_schema_version: 1 +meta_graphs { + meta_info_def { + stripped_op_list { + op { + name: "Add" + input_arg { + name: "x" + type_attr: "T" + } + input_arg { + name: "y" + type_attr: "T" + } + output_arg { + name: "z" + type_attr: "T" + } + attr { + name: "T" + type: "type" + allowed_values { + list { + type: DT_HALF + type: DT_FLOAT + type: DT_DOUBLE + type: DT_UINT8 + type: DT_INT8 + type: DT_INT16 + type: DT_INT32 + type: DT_INT64 + type: DT_COMPLEX64 + type: DT_COMPLEX128 + type: DT_STRING + } + } + } + } + op { + name: "Assign" + input_arg { + name: "ref" + type_attr: "T" + is_ref: true + } + input_arg { + name: "value" + type_attr: "T" + } + output_arg { + name: "output_ref" + type_attr: "T" + is_ref: true + } + attr { + name: "T" + type: "type" + } + attr { + name: "validate_shape" + type: "bool" + default_value { + b: true + } + } + attr { + name: "use_locking" + type: "bool" + default_value { + b: true + } + } + allows_uninitialized_input: true + } + op { + name: "Const" + output_arg { + name: "output" + type_attr: "dtype" + } + attr { + name: "value" + type: "tensor" + } + attr { + name: "dtype" + type: "type" + } + } + op { + name: "Identity" + input_arg { + name: "input" + type_attr: "T" + } + output_arg { + name: "output" + type_attr: "T" + } + attr { + name: "T" + type: "type" + } + } + op { + name: "MergeV2Checkpoints" + input_arg { + name: "checkpoint_prefixes" + type: DT_STRING + } + input_arg { + name: "destination_prefix" + type: DT_STRING + } + attr { + name: "delete_old_dirs" + type: "bool" + default_value { + b: true + } + } + } + op { + name: "Mul" + input_arg { + name: "x" + type_attr: "T" + } + input_arg { + name: "y" + type_attr: "T" + } + output_arg { + name: "z" + type_attr: "T" + } + attr { + name: "T" + type: "type" + allowed_values { + list { + type: DT_HALF + type: DT_FLOAT + type: DT_DOUBLE + type: DT_UINT8 + type: DT_INT8 + type: DT_UINT16 + type: DT_INT16 + type: DT_INT32 + type: DT_INT64 + type: DT_COMPLEX64 + type: DT_COMPLEX128 + } + } + } + is_commutative: true + } + op { + name: "NoOp" + } + op { + name: "Pack" + input_arg { + name: "values" + type_attr: "T" + number_attr: "N" + } + output_arg { + name: "output" + type_attr: "T" + } + attr { + name: "N" + type: "int" + has_minimum: true + minimum: 1 + } + attr { + name: "T" + type: "type" + } + attr { + name: "axis" + type: "int" + default_value { + i: 0 + } + } + } + op { + name: "ParseExample" + input_arg { + name: "serialized" + type: DT_STRING + } + input_arg { + name: "names" + type: DT_STRING + } + input_arg { + name: "sparse_keys" + type: DT_STRING + number_attr: "Nsparse" + } + input_arg { + name: "dense_keys" + type: DT_STRING + number_attr: "Ndense" + } + input_arg { + name: "dense_defaults" + type_list_attr: "Tdense" + } + output_arg { + name: "sparse_indices" + type: DT_INT64 + number_attr: "Nsparse" + } + output_arg { + name: "sparse_values" + type_list_attr: "sparse_types" + } + output_arg { + name: "sparse_shapes" + type: DT_INT64 + number_attr: "Nsparse" + } + output_arg { + name: "dense_values" + type_list_attr: "Tdense" + } + attr { + name: "Nsparse" + type: "int" + has_minimum: true + } + attr { + name: "Ndense" + type: "int" + has_minimum: true + } + attr { + name: "sparse_types" + type: "list(type)" + has_minimum: true + allowed_values { + list { + type: DT_FLOAT + type: DT_INT64 + type: DT_STRING + } + } + } + attr { + name: "Tdense" + type: "list(type)" + has_minimum: true + allowed_values { + list { + type: DT_FLOAT + type: DT_INT64 + type: DT_STRING + } + } + } + attr { + name: "dense_shapes" + type: "list(shape)" + has_minimum: true + } + } + op { + name: "Placeholder" + output_arg { + name: "output" + type_attr: "dtype" + } + attr { + name: "dtype" + type: "type" + } + attr { + name: "shape" + type: "shape" + default_value { + shape { + unknown_rank: true + } + } + } + } + op { + name: "Reshape" + input_arg { + name: "tensor" + type_attr: "T" + } + input_arg { + name: "shape" + type_attr: "Tshape" + } + output_arg { + name: "output" + type_attr: "T" + } + attr { + name: "T" + type: "type" + } + attr { + name: "Tshape" + type: "type" + default_value { + type: DT_INT32 + } + allowed_values { + list { + type: DT_INT32 + type: DT_INT64 + } + } + } + } + op { + name: "RestoreV2" + input_arg { + name: "prefix" + type: DT_STRING + } + input_arg { + name: "tensor_names" + type: DT_STRING + } + input_arg { + name: "shape_and_slices" + type: DT_STRING + } + output_arg { + name: "tensors" + type_list_attr: "dtypes" + } + attr { + name: "dtypes" + type: "list(type)" + has_minimum: true + minimum: 1 + } + } + op { + name: "SaveV2" + input_arg { + name: "prefix" + type: DT_STRING + } + input_arg { + name: "tensor_names" + type: DT_STRING + } + input_arg { + name: "shape_and_slices" + type: DT_STRING + } + input_arg { + name: "tensors" + type_list_attr: "dtypes" + } + attr { + name: "dtypes" + type: "list(type)" + has_minimum: true + minimum: 1 + } + } + op { + name: "ShardedFilename" + input_arg { + name: "basename" + type: DT_STRING + } + input_arg { + name: "shard" + type: DT_INT32 + } + input_arg { + name: "num_shards" + type: DT_INT32 + } + output_arg { + name: "filename" + type: DT_STRING + } + } + op { + name: "StringJoin" + input_arg { + name: "inputs" + type: DT_STRING + number_attr: "N" + } + output_arg { + name: "output" + type: DT_STRING + } + attr { + name: "N" + type: "int" + has_minimum: true + minimum: 1 + } + attr { + name: "separator" + type: "string" + default_value { + s: "" + } + } + } + op { + name: "VariableV2" + output_arg { + name: "ref" + type_attr: "dtype" + is_ref: true + } + attr { + name: "shape" + type: "shape" + } + attr { + name: "dtype" + type: "type" + } + attr { + name: "container" + type: "string" + default_value { + s: "" + } + } + attr { + name: "shared_name" + type: "string" + default_value { + s: "" + } + } + is_stateful: true + } + } + tags: "serve" + tensorflow_version: "1.1.0-rc2" + tensorflow_git_version: "unknown" + } + graph_def { + node { + name: "a/initial_value" + op: "Const" + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.5 + } + } + } + } + node { + name: "a" + op: "VariableV2" + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + } + } + } + attr { + key: "shared_name" + value { + s: "" + } + } + } + node { + name: "a/Assign" + op: "Assign" + input: "a" + input: "a/initial_value" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@a" + } + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "validate_shape" + value { + b: true + } + } + } + node { + name: "a/read" + op: "Identity" + input: "a" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@a" + } + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + } + node { + name: "b/initial_value" + op: "Const" + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 2.0 + } + } + } + } + node { + name: "b" + op: "VariableV2" + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + } + } + } + attr { + key: "shared_name" + value { + s: "" + } + } + } + node { + name: "b/Assign" + op: "Assign" + input: "b" + input: "b/initial_value" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@b" + } + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "validate_shape" + value { + b: true + } + } + } + node { + name: "b/read" + op: "Identity" + input: "b" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@b" + } + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + } + node { + name: "c/initial_value" + op: "Const" + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 3.0 + } + } + } + } + node { + name: "c" + op: "VariableV2" + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + } + } + } + attr { + key: "shared_name" + value { + s: "" + } + } + } + node { + name: "c/Assign" + op: "Assign" + input: "c" + input: "c/initial_value" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@c" + } + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "validate_shape" + value { + b: true + } + } + } + node { + name: "c/read" + op: "Identity" + input: "c" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@c" + } + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + } + node { + name: "tf_example" + op: "Placeholder" + attr { + key: "_output_shapes" + value { + list { + shape { + unknown_rank: true + } + } + } + } + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "shape" + value { + shape { + unknown_rank: true + } + } + } + } + node { + name: "ParseExample/Const" + op: "Const" + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + } + } + } + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + } + } + } + } + } + } + node { + name: "ParseExample/key_x2" + op: "Const" + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: 1 + } + } + } + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + } + float_val: 0.0 + } + } + } + } + node { + name: "ParseExample/Reshape/shape" + op: "Const" + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: 1 + } + } + } + } + } + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 1 + } + } + int_val: 1 + } + } + } + } + node { + name: "ParseExample/Reshape" + op: "Reshape" + input: "ParseExample/key_x2" + input: "ParseExample/Reshape/shape" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tshape" + value { + type: DT_INT32 + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: 1 + } + } + } + } + } + } + node { + name: "ParseExample/ParseExample/names" + op: "Const" + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + } + } + } + } + } + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_STRING + tensor_shape { + dim { + } + } + } + } + } + } + node { + name: "ParseExample/ParseExample/dense_keys_0" + op: "Const" + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_STRING + tensor_shape { + } + string_val: "x" + } + } + } + } + node { + name: "ParseExample/ParseExample/dense_keys_1" + op: "Const" + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_STRING + tensor_shape { + } + string_val: "x2" + } + } + } + } + node { + name: "ParseExample/ParseExample" + op: "ParseExample" + input: "tf_example" + input: "ParseExample/ParseExample/names" + input: "ParseExample/ParseExample/dense_keys_0" + input: "ParseExample/ParseExample/dense_keys_1" + input: "ParseExample/Const" + input: "ParseExample/Reshape" + attr { + key: "Ndense" + value { + i: 2 + } + } + attr { + key: "Nsparse" + value { + i: 0 + } + } + attr { + key: "Tdense" + value { + list { + type: DT_FLOAT + type: DT_FLOAT + } + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: -1 + } + dim { + size: 1 + } + } + shape { + dim { + size: -1 + } + dim { + size: 1 + } + } + } + } + } + attr { + key: "dense_shapes" + value { + list { + shape { + dim { + size: 1 + } + } + shape { + dim { + size: 1 + } + } + } + } + } + attr { + key: "sparse_types" + value { + list { + } + } + } + } + node { + name: "x" + op: "Identity" + input: "ParseExample/ParseExample" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: -1 + } + dim { + size: 1 + } + } + } + } + } + } + node { + name: "Mul" + op: "Mul" + input: "a/read" + input: "x" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: -1 + } + dim { + size: 1 + } + } + } + } + } + } + node { + name: "y" + op: "Add" + input: "Mul" + input: "b/read" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: -1 + } + dim { + size: 1 + } + } + } + } + } + } + node { + name: "Mul_1" + op: "Mul" + input: "a/read" + input: "x" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: -1 + } + dim { + size: 1 + } + } + } + } + } + } + node { + name: "y2" + op: "Add" + input: "Mul_1" + input: "c/read" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: -1 + } + dim { + size: 1 + } + } + } + } + } + } + node { + name: "x2" + op: "Identity" + input: "ParseExample/ParseExample:1" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: -1 + } + dim { + size: 1 + } + } + } + } + } + } + node { + name: "Mul_2" + op: "Mul" + input: "a/read" + input: "x2" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: -1 + } + dim { + size: 1 + } + } + } + } + } + } + node { + name: "y3" + op: "Add" + input: "Mul_2" + input: "c/read" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: -1 + } + dim { + size: 1 + } + } + } + } + } + } + node { + name: "Const" + op: "Const" + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_STRING + tensor_shape { + } + string_val: "/tmp/original/export/assets/foo.txt" + } + } + } + } + node { + name: "filename_tensor/initial_value" + op: "Const" + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_STRING + tensor_shape { + } + string_val: "foo.txt" + } + } + } + } + node { + name: "filename_tensor" + op: "VariableV2" + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "shape" + value { + shape { + } + } + } + attr { + key: "shared_name" + value { + s: "" + } + } + } + node { + name: "filename_tensor/Assign" + op: "Assign" + input: "filename_tensor" + input: "filename_tensor/initial_value" + attr { + key: "T" + value { + type: DT_STRING + } + } + attr { + key: "_class" + value { + list { + s: "loc:@filename_tensor" + } + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "validate_shape" + value { + b: true + } + } + } + node { + name: "filename_tensor/read" + op: "Identity" + input: "filename_tensor" + attr { + key: "T" + value { + type: DT_STRING + } + } + attr { + key: "_class" + value { + list { + s: "loc:@filename_tensor" + } + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + } + node { + name: "Assign/value" + op: "Const" + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_STRING + tensor_shape { + } + string_val: "foo.txt" + } + } + } + } + node { + name: "Assign" + op: "Assign" + input: "filename_tensor" + input: "Assign/value" + attr { + key: "T" + value { + type: DT_STRING + } + } + attr { + key: "_class" + value { + list { + s: "loc:@filename_tensor" + } + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "use_locking" + value { + b: false + } + } + attr { + key: "validate_shape" + value { + b: true + } + } + } + node { + name: "init" + op: "NoOp" + input: "^a/Assign" + input: "^b/Assign" + input: "^c/Assign" + } + node { + name: "group_deps" + op: "NoOp" + input: "^Assign" + } + node { + name: "save/Const" + op: "Const" + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_STRING + tensor_shape { + } + string_val: "model" + } + } + } + } + node { + name: "save/StringJoin/inputs_1" + op: "Const" + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_STRING + tensor_shape { + } + string_val: "_temp_80e928f1e0c844239d136d1ea966099d/part" + } + } + } + } + node { + name: "save/StringJoin" + op: "StringJoin" + input: "save/Const" + input: "save/StringJoin/inputs_1" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "separator" + value { + s: "" + } + } + } + node { + name: "save/num_shards" + op: "Const" + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + } + int_val: 1 + } + } + } + } + node { + name: "save/ShardedFilename/shard" + op: "Const" + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + } + int_val: 0 + } + } + } + } + node { + name: "save/ShardedFilename" + op: "ShardedFilename" + input: "save/StringJoin" + input: "save/ShardedFilename/shard" + input: "save/num_shards" + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + } + node { + name: "save/SaveV2/tensor_names" + op: "Const" + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: 3 + } + } + } + } + } + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_STRING + tensor_shape { + dim { + size: 3 + } + } + string_val: "a" + string_val: "b" + string_val: "c" + } + } + } + } + node { + name: "save/SaveV2/shape_and_slices" + op: "Const" + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: 3 + } + } + } + } + } + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_STRING + tensor_shape { + dim { + size: 3 + } + } + string_val: "" + string_val: "" + string_val: "" + } + } + } + } + node { + name: "save/SaveV2" + op: "SaveV2" + input: "save/ShardedFilename" + input: "save/SaveV2/tensor_names" + input: "save/SaveV2/shape_and_slices" + input: "a" + input: "b" + input: "c" + attr { + key: "dtypes" + value { + list { + type: DT_FLOAT + type: DT_FLOAT + type: DT_FLOAT + } + } + } + } + node { + name: "save/control_dependency" + op: "Identity" + input: "save/ShardedFilename" + input: "^save/SaveV2" + attr { + key: "T" + value { + type: DT_STRING + } + } + attr { + key: "_class" + value { + list { + s: "loc:@save/ShardedFilename" + } + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + } + node { + name: "save/MergeV2Checkpoints/checkpoint_prefixes" + op: "Pack" + input: "save/ShardedFilename" + input: "^save/control_dependency" + attr { + key: "N" + value { + i: 1 + } + } + attr { + key: "T" + value { + type: DT_STRING + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: 1 + } + } + } + } + } + attr { + key: "axis" + value { + i: 0 + } + } + } + node { + name: "save/MergeV2Checkpoints" + op: "MergeV2Checkpoints" + input: "save/MergeV2Checkpoints/checkpoint_prefixes" + input: "save/Const" + attr { + key: "delete_old_dirs" + value { + b: true + } + } + } + node { + name: "save/Identity" + op: "Identity" + input: "save/Const" + input: "^save/control_dependency" + input: "^save/MergeV2Checkpoints" + attr { + key: "T" + value { + type: DT_STRING + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + } + node { + name: "save/RestoreV2/tensor_names" + op: "Const" + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: 1 + } + } + } + } + } + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_STRING + tensor_shape { + dim { + size: 1 + } + } + string_val: "a" + } + } + } + } + node { + name: "save/RestoreV2/shape_and_slices" + op: "Const" + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: 1 + } + } + } + } + } + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_STRING + tensor_shape { + dim { + size: 1 + } + } + string_val: "" + } + } + } + } + node { + name: "save/RestoreV2" + op: "RestoreV2" + input: "save/Const" + input: "save/RestoreV2/tensor_names" + input: "save/RestoreV2/shape_and_slices" + attr { + key: "_output_shapes" + value { + list { + shape { + unknown_rank: true + } + } + } + } + attr { + key: "dtypes" + value { + list { + type: DT_FLOAT + } + } + } + } + node { + name: "save/Assign" + op: "Assign" + input: "a" + input: "save/RestoreV2" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@a" + } + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "validate_shape" + value { + b: true + } + } + } + node { + name: "save/RestoreV2_1/tensor_names" + op: "Const" + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: 1 + } + } + } + } + } + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_STRING + tensor_shape { + dim { + size: 1 + } + } + string_val: "b" + } + } + } + } + node { + name: "save/RestoreV2_1/shape_and_slices" + op: "Const" + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: 1 + } + } + } + } + } + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_STRING + tensor_shape { + dim { + size: 1 + } + } + string_val: "" + } + } + } + } + node { + name: "save/RestoreV2_1" + op: "RestoreV2" + input: "save/Const" + input: "save/RestoreV2_1/tensor_names" + input: "save/RestoreV2_1/shape_and_slices" + attr { + key: "_output_shapes" + value { + list { + shape { + unknown_rank: true + } + } + } + } + attr { + key: "dtypes" + value { + list { + type: DT_FLOAT + } + } + } + } + node { + name: "save/Assign_1" + op: "Assign" + input: "b" + input: "save/RestoreV2_1" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@b" + } + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "validate_shape" + value { + b: true + } + } + } + node { + name: "save/RestoreV2_2/tensor_names" + op: "Const" + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: 1 + } + } + } + } + } + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_STRING + tensor_shape { + dim { + size: 1 + } + } + string_val: "c" + } + } + } + } + node { + name: "save/RestoreV2_2/shape_and_slices" + op: "Const" + attr { + key: "_output_shapes" + value { + list { + shape { + dim { + size: 1 + } + } + } + } + } + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_STRING + tensor_shape { + dim { + size: 1 + } + } + string_val: "" + } + } + } + } + node { + name: "save/RestoreV2_2" + op: "RestoreV2" + input: "save/Const" + input: "save/RestoreV2_2/tensor_names" + input: "save/RestoreV2_2/shape_and_slices" + attr { + key: "_output_shapes" + value { + list { + shape { + unknown_rank: true + } + } + } + } + attr { + key: "dtypes" + value { + list { + type: DT_FLOAT + } + } + } + } + node { + name: "save/Assign_2" + op: "Assign" + input: "c" + input: "save/RestoreV2_2" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@c" + } + } + } + attr { + key: "_output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "validate_shape" + value { + b: true + } + } + } + node { + name: "save/restore_shard" + op: "NoOp" + input: "^save/Assign" + input: "^save/Assign_1" + input: "^save/Assign_2" + } + node { + name: "save/restore_all" + op: "NoOp" + input: "^save/restore_shard" + } + versions { + producer: 23 + } + } + saver_def { + filename_tensor_name: "save/Const:0" + save_tensor_name: "save/Identity:0" + restore_op_name: "save/restore_all" + max_to_keep: 5 + sharded: true + keep_checkpoint_every_n_hours: 10000.0 + version: V2 + } + collection_def { + key: "asset_filepaths" + value { + node_list { + value: "Const:0" + } + } + } + collection_def { + key: "legacy_init_op" + value { + node_list { + value: "group_deps" + } + } + } + collection_def { + key: "saved_model_assets" + value { + any_list { + value { + type_url: "type.googleapis.com/tensorflow.AssetFileDef" + value: "\n\t\n\007Const:0\022\007foo.txt" + } + } + } + } + collection_def { + key: "trainable_variables" + value { + bytes_list { + value: "\n\003a:0\022\010a/Assign\032\010a/read:0" + value: "\n\003b:0\022\010b/Assign\032\010b/read:0" + value: "\n\003c:0\022\010c/Assign\032\010c/read:0" + } + } + } + collection_def { + key: "variables" + value { + bytes_list { + value: "\n\003a:0\022\010a/Assign\032\010a/read:0" + value: "\n\003b:0\022\010b/Assign\032\010b/read:0" + value: "\n\003c:0\022\010c/Assign\032\010c/read:0" + } + } + } + signature_def { + key: "classify_x2_to_y3" + value { + inputs { + key: "inputs" + value { + name: "x2:0" + dtype: DT_FLOAT + tensor_shape { + dim { + size: -1 + } + dim { + size: 1 + } + } + } + } + outputs { + key: "scores" + value { + name: "y3:0" + dtype: DT_FLOAT + tensor_shape { + dim { + size: -1 + } + dim { + size: 1 + } + } + } + } + method_name: "tensorflow/serving/classify" + } + } + signature_def { + key: "classify_x_to_y" + value { + inputs { + key: "inputs" + value { + name: "tf_example:0" + dtype: DT_STRING + tensor_shape { + unknown_rank: true + } + } + } + outputs { + key: "scores" + value { + name: "y:0" + dtype: DT_FLOAT + tensor_shape { + dim { + size: -1 + } + dim { + size: 1 + } + } + } + } + method_name: "tensorflow/serving/classify" + } + } + signature_def { + key: "regress_x2_to_y3" + value { + inputs { + key: "inputs" + value { + name: "x2:0" + dtype: DT_FLOAT + tensor_shape { + dim { + size: -1 + } + dim { + size: 1 + } + } + } + } + outputs { + key: "outputs" + value { + name: "y3:0" + dtype: DT_FLOAT + tensor_shape { + dim { + size: -1 + } + dim { + size: 1 + } + } + } + } + method_name: "tensorflow/serving/regress" + } + } + signature_def { + key: "regress_x_to_y" + value { + inputs { + key: "inputs" + value { + name: "tf_example:0" + dtype: DT_STRING + tensor_shape { + unknown_rank: true + } + } + } + outputs { + key: "outputs" + value { + name: "y:0" + dtype: DT_FLOAT + tensor_shape { + dim { + size: -1 + } + dim { + size: 1 + } + } + } + } + method_name: "tensorflow/serving/regress" + } + } + signature_def { + key: "regress_x_to_y2" + value { + inputs { + key: "inputs" + value { + name: "tf_example:0" + dtype: DT_STRING + tensor_shape { + unknown_rank: true + } + } + } + outputs { + key: "outputs" + value { + name: "y2:0" + dtype: DT_FLOAT + tensor_shape { + dim { + size: -1 + } + dim { + size: 1 + } + } + } + } + method_name: "tensorflow/serving/regress" + } + } + signature_def { + key: "serving_default" + value { + inputs { + key: "x" + value { + name: "x:0" + dtype: DT_FLOAT + tensor_shape { + dim { + size: -1 + } + dim { + size: 1 + } + } + } + } + outputs { + key: "y" + value { + name: "y:0" + dtype: DT_FLOAT + tensor_shape { + dim { + size: -1 + } + dim { + size: 1 + } + } + } + } + method_name: "tensorflow/serving/predict" + } + } +} diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_pbtxt/00000123/variables/variables.data-00000-of-00001 b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_pbtxt/00000123/variables/variables.data-00000-of-00001 new file mode 100644 index 0000000000000000000000000000000000000000..15b75d6ef6bffc336d138d923badb3928b8c4c13 Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_pbtxt/00000123/variables/variables.data-00000-of-00001 differ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_pbtxt/00000123/variables/variables.index b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_pbtxt/00000123/variables/variables.index new file mode 100644 index 0000000000000000000000000000000000000000..7ec9fb4fe2dd21d0a6c324aecd7658fc37cf2326 Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_pbtxt/00000123/variables/variables.index differ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_v2/00000123/assets.extra/validation_result_do_not_edit b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_v2/00000123/assets.extra/validation_result_do_not_edit new file mode 100644 index 0000000000000000000000000000000000000000..99eedbdb9bc6df2f6740760900b035495cf1151a Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_v2/00000123/assets.extra/validation_result_do_not_edit differ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_v2/00000123/assets/foo.txt b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_v2/00000123/assets/foo.txt new file mode 100644 index 0000000000000000000000000000000000000000..f9ff036688007836524129e23f5cf82edd1e8910 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_v2/00000123/assets/foo.txt @@ -0,0 +1 @@ +asset-file-contents \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_v2/00000123/saved_model.pb b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_v2/00000123/saved_model.pb new file mode 100644 index 0000000000000000000000000000000000000000..bca10db635007a3108dbb8c4ef831584fb53ac20 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_v2/00000123/saved_model.pb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e19c21386b5129e130bd4a86940ad83c73e26b2b1f8d52be1bef923ff86bfcf8 +size 10774 diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_v2/00000123/variables/variables.data-00000-of-00001 b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_v2/00000123/variables/variables.data-00000-of-00001 new file mode 100644 index 0000000000000000000000000000000000000000..15b75d6ef6bffc336d138d923badb3928b8c4c13 Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_v2/00000123/variables/variables.data-00000-of-00001 differ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_v2/00000123/variables/variables.index b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_v2/00000123/variables/variables.index new file mode 100644 index 0000000000000000000000000000000000000000..7ec9fb4fe2dd21d0a6c324aecd7658fc37cf2326 Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/half_plus_two_v2/00000123/variables/variables.index differ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/static_hashtable_asset.txt b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/static_hashtable_asset.txt new file mode 100644 index 0000000000000000000000000000000000000000..46416a6918a44aac601c9d054fa6e6973f4dc98a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/static_hashtable_asset.txt @@ -0,0 +1,4 @@ +foo +bar +baz +wombat diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/test_asset.txt b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/test_asset.txt new file mode 100644 index 0000000000000000000000000000000000000000..0f7662808df495ceb05d53f3891879c3f58e1d8e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/test_asset.txt @@ -0,0 +1 @@ +TEST ASSET FILE CONTENTS diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/x_plus_y_v2_debuginfo/debug/saved_model_debug_info.pb b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/x_plus_y_v2_debuginfo/debug/saved_model_debug_info.pb new file mode 100644 index 0000000000000000000000000000000000000000..5b1fc914543bc71e53f02fc76735ec5e49db89a1 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/x_plus_y_v2_debuginfo/debug/saved_model_debug_info.pb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:dc1e4b0c69d4bb467d3657f62b032298dc8409046c734c246eca166ec6ceb48c +size 5551 diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/x_plus_y_v2_debuginfo/saved_model.pb b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/x_plus_y_v2_debuginfo/saved_model.pb new file mode 100644 index 0000000000000000000000000000000000000000..225070aea2879291d57b9fba4802915b97b1f007 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/x_plus_y_v2_debuginfo/saved_model.pb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:36edd4490cdc12b1d7bf1398e755c8876cac439ff6d21f3e9626604b9ba997ce +size 6485 diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/x_plus_y_v2_debuginfo/variables/variables.data-00000-of-00001 b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/x_plus_y_v2_debuginfo/variables/variables.data-00000-of-00001 new file mode 100644 index 0000000000000000000000000000000000000000..1039a8fe6dd60e2e2487d0e55af26530bce9d0e6 Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/x_plus_y_v2_debuginfo/variables/variables.data-00000-of-00001 differ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/x_plus_y_v2_debuginfo/variables/variables.index b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/x_plus_y_v2_debuginfo/variables/variables.index new file mode 100644 index 0000000000000000000000000000000000000000..71e4af3fa42e4b076e8f740a37b03db9e99ccc2e Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/x_plus_y_v2_debuginfo/variables/variables.index differ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/util.cc b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/util.cc new file mode 100644 index 0000000000000000000000000000000000000000..d4178855e110a123b1542f1ca2bc7ca972227e4d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/util.cc @@ -0,0 +1,93 @@ +/* Copyright 2021 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/cc/saved_model/util.h" + +#include +#include +#include +#include + +#include "tensorflow/core/platform/errors.h" +#include "tensorflow/core/protobuf/meta_graph.pb.h" + +namespace tensorflow { +namespace saved_model { + +std::string GetWriteVersion(const SavedModel& saved_model) { + if (saved_model.meta_graphs_size() == 1 && + saved_model.meta_graphs()[0].has_object_graph_def()) { + return "2"; + } + return "1"; +} + +std::set GetMapKeys( + const ::google::protobuf::Map& map) { + std::set keys; + for (const auto& it : map) { + keys.insert(it.first); + } + return keys; +} + +absl::Status GetInputValues( + const SignatureDef& signature, + const ::google::protobuf::Map& request_inputs, + std::vector>& inputs) { + const TensorProto* tensor_proto; + std::set seen_request_inputs; + + for (const auto& [alias, tensor_info] : signature.inputs()) { + const std::string& feed_name = tensor_info.name(); + auto iter = request_inputs.find(alias); + + if (iter == request_inputs.end()) { + auto default_inputs_iter = signature.defaults().find(alias); + if (default_inputs_iter == signature.defaults().end()) { + return absl::InvalidArgumentError(strings::StrCat( + "Signature input alias: ", alias, "(feed name: ", feed_name, + ") not found in request and no default value provided. Inputs " + "expected to be in the request {", + absl::StrJoin(GetMapKeys(request_inputs), ","), + "}, or default input alias set: ", + absl::StrJoin(GetMapKeys(signature.defaults()), ","))); + } + tensor_proto = &default_inputs_iter->second; + } else { + tensor_proto = &iter->second; + seen_request_inputs.insert(alias); + } + + Tensor tensor; + if (!tensor.FromProto(*tensor_proto)) { + return absl::InvalidArgumentError( + absl::StrCat("tensor parsing error: ", alias)); + } + + inputs.emplace_back(feed_name, tensor); + } + + if (!request_inputs.empty() && + seen_request_inputs.size() != request_inputs.size()) { + return absl::InvalidArgumentError(absl::StrCat( + "Inputs contains invalid name. Used request inputs: ", + absl::StrJoin(seen_request_inputs, ","), + ", request input: ", absl::StrJoin(GetMapKeys(request_inputs), ","))); + } + return absl::OkStatus(); +} + +} // namespace saved_model +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/util.h b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/util.h new file mode 100644 index 0000000000000000000000000000000000000000..2f1210a0b5e5ccd97b588b0424c20b6ae10f8169 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/util.h @@ -0,0 +1,56 @@ +/* Copyright 2021 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. +==============================================================================*/ +#ifndef TENSORFLOW_CC_SAVED_MODEL_UTIL_H_ +#define TENSORFLOW_CC_SAVED_MODEL_UTIL_H_ + +#include +#include +#include +#include + +#include "tensorflow/core/framework/tensor.h" +#include "tensorflow/core/framework/tensor.pb.h" +#include "tensorflow/core/platform/status.h" +#include "tensorflow/core/protobuf/meta_graph.pb.h" +#include "tensorflow/core/protobuf/saved_model.pb.h" + +namespace tensorflow { +namespace saved_model { + +// Utility functions for SavedModel reading and writing. + +// Returns "WriteVersion" ("1" or "2") of the SavedModel protobuf. If the +// protobuf has exactly one MetaGraphDef, which contains a SavedObjectGraph, it +// is version 2. Else, the protobuf is version 1. +// +// NOTE: The "WriteVersion" does *not* equal the major version of TF. +std::string GetWriteVersion(const SavedModel& saved_model); + +// Get view of string keys of a map. +std::set GetMapKeys( + const ::google::protobuf::Map& map); + +// Get the default input value from signature if it's missing in the request +// inputs. If `is_alias` is set to true, the keys of the `request_inputs` are +// alias names rather than the feed names in the graph. +absl::Status GetInputValues( + const SignatureDef& signature, + const ::google::protobuf::Map& request_inputs, + std::vector>& inputs); + +} // namespace saved_model +} // namespace tensorflow + +#endif // TENSORFLOW_CC_SAVED_MODEL_UTIL_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/saved_model/util_test.cc b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/util_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..3af89fb3059fc07ae618c7b34a8ed5204bcf3e03 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/saved_model/util_test.cc @@ -0,0 +1,151 @@ +/* Copyright 2021 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/cc/saved_model/util.h" + +#include +#include +#include +#include + +#include "tensorflow/cc/saved_model/test_utils.h" +#include "tensorflow/core/framework/tensor_shape.pb.h" +#include "tensorflow/core/framework/tensor_testutil.h" +#include "tensorflow/core/lib/core/status_test_util.h" +#include "tensorflow/core/lib/io/path.h" +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/platform/test.h" +#include "tensorflow/core/protobuf/meta_graph.pb.h" +#include "tensorflow/core/protobuf/saved_object_graph.pb.h" +#include "tsl/platform/status_matchers.h" + +namespace tensorflow { +namespace saved_model { +namespace { + +using tsl::testing::StatusIs; + +TEST(UtilTest, TestGetWriteVersionV2) { + SavedModel saved_model_proto; + MetaGraphDef* meta_graphdef = saved_model_proto.add_meta_graphs(); + auto* object_graph_def = meta_graphdef->mutable_object_graph_def(); + object_graph_def->add_nodes(); + EXPECT_EQ(GetWriteVersion(saved_model_proto), "2"); +} + +TEST(UtilTest, TestGetWriteVersionV1) { + SavedModel saved_model_proto; + saved_model_proto.add_meta_graphs(); + EXPECT_EQ(GetWriteVersion(saved_model_proto), "1"); + saved_model_proto.add_meta_graphs(); + EXPECT_EQ(GetWriteVersion(saved_model_proto), "1"); +} + +class GetInputValuesTest : public ::testing::Test { + public: + GetInputValuesTest() { + (*sig_.mutable_inputs())["x"].set_name("feed_x"); + (*sig_.mutable_inputs())["y"].set_name("feed_y"); + + (*sig_.mutable_defaults())["x"] = CreateTensorProto(1); + (*sig_.mutable_defaults())["y"] = CreateTensorProto("A"); + + request_["x"] = CreateTensorProto(2); + request_["y"] = CreateTensorProto("B"); + + unaliased_request_["feed_x"] = CreateTensorProto(2); + unaliased_request_["feed_y"] = CreateTensorProto("B"); + + input_x_ = CreateTensorProto(2); + input_y_ = CreateTensorProto("B"); + default_x_ = CreateTensorProto(1); + default_y_ = CreateTensorProto("A"); + } + + template + TensorProto CreateTensorProto(const T& val) { + Tensor tensor(val); + TensorProto tensor_proto; + tensor.AsProtoTensorContent(&tensor_proto); + return tensor_proto; + } + + void ConvertOutputTensorToProto( + std::vector>& inputs, + std::vector>& protos) { + for (const auto& input : inputs) { + TensorProto tensor_proto; + input.second.AsProtoTensorContent(&tensor_proto); + protos.push_back({input.first, std::move(tensor_proto)}); + } + } + + SignatureDef sig_; + google::protobuf::Map request_; + std::map unaliased_request_; + TensorProto input_x_, input_y_, default_x_, default_y_; +}; + +TEST_F(GetInputValuesTest, RequestContainsInvalidInputs) { + google::protobuf::Map local_request = request_; + local_request["xx"] = CreateTensorProto(2); + + std::vector> inputs; + EXPECT_THAT(GetInputValues(sig_, local_request, inputs), + absl_testing::StatusIs(absl::StatusCode::kInvalidArgument)); +} + +TEST_F(GetInputValuesTest, RequestContainsAllTheInputs) { + std::vector> inputs; + TF_EXPECT_OK(GetInputValues(sig_, request_, inputs)); + + std::vector> exp_inputs; + ConvertOutputTensorToProto(inputs, exp_inputs); + + EXPECT_THAT(exp_inputs, + UnorderedElementsAre(Pair("feed_x", EqualsProto(input_x_)), + Pair("feed_y", EqualsProto(input_y_)))); +} + +TEST_F(GetInputValuesTest, RequestContainsNoInputs) { + google::protobuf::Map local_request = request_; + local_request.erase("x"); + local_request.erase("y"); + + std::vector> inputs; + TF_EXPECT_OK(GetInputValues(sig_, local_request, inputs)); + + std::vector> exp_inputs; + ConvertOutputTensorToProto(inputs, exp_inputs); + + EXPECT_THAT(exp_inputs, + UnorderedElementsAre(Pair("feed_x", EqualsProto(default_x_)), + Pair("feed_y", EqualsProto(default_y_)))); +} + +TEST_F(GetInputValuesTest, RequestContainsPartialInputs) { + google::protobuf::Map local_request = request_; + local_request.erase("y"); + + std::vector> inputs; + TF_EXPECT_OK(GetInputValues(sig_, local_request, inputs)); + + std::vector> exp_inputs; + ConvertOutputTensorToProto(inputs, exp_inputs); + + EXPECT_THAT(exp_inputs, + UnorderedElementsAre(Pair("feed_x", EqualsProto(input_x_)), + Pair("feed_y", EqualsProto(default_y_)))); +} + +} // namespace +} // namespace saved_model +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/cc/tools/BUILD b/western_ai/usa/tensorflow/tensorflow/cc/tools/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..13518c47cfb2cba574b61e81dac75f0771ee9c12 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/tools/BUILD @@ -0,0 +1,55 @@ +#include "third_party/absl/strings/str_cat.h" +#Description: +# TensorFlow cc tools. + +load( + "//tensorflow:tensorflow.bzl", + "tf_cc_test", +) +load("//tensorflow/core/platform:rules_cc.bzl", "cc_library") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:license"], + default_visibility = ["//visibility:public"], + licenses = ["notice"], +) + +cc_library( + name = "freeze_saved_model", + srcs = ["freeze_saved_model.cc"], + hdrs = ["freeze_saved_model.h"], + deps = [ + "//tensorflow/cc/saved_model:loader", + "//tensorflow/core:core_cpu", + "//tensorflow/core:framework", + "//tensorflow/core:lib", + "//tensorflow/core:protos_all_cc", + "@com_google_absl//absl/log", + "@com_google_absl//absl/status", + ], +) + +tf_cc_test( + name = "freeze_saved_model_test", + srcs = ["freeze_saved_model_test.cc"], + deps = [ + ":freeze_saved_model", + "//tensorflow/cc:cc_ops", + "//tensorflow/cc:ops", + "//tensorflow/cc:resource_variable_ops", + "//tensorflow/cc:scope", + "//tensorflow/cc/saved_model:loader", + "//tensorflow/core:core_cpu", + "//tensorflow/core:framework", + "//tensorflow/core:framework_internal", + "//tensorflow/core:portable_gif_internal", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core:tensorflow", + "//tensorflow/core:test", + "//tensorflow/core:test_main", + "//tensorflow/core:testlib", + "//tensorflow/core/platform:status", + "@com_google_absl//absl/status", + "@xla//xla/tsl/platform:errors", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/cc/tools/freeze_saved_model.cc b/western_ai/usa/tensorflow/tensorflow/cc/tools/freeze_saved_model.cc new file mode 100644 index 0000000000000000000000000000000000000000..33a8f64e2b3ec21bbb025342aae44c7015322b40 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/tools/freeze_saved_model.cc @@ -0,0 +1,304 @@ +/* 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/cc/tools/freeze_saved_model.h" + +#include +#include +#include +#include +#include +#include + +#include "absl/log/log.h" +#include "absl/status/status.h" +#include "tensorflow/cc/saved_model/loader.h" +#include "xla/tsl/platform/errors.h" +#include "tensorflow/core/framework/attr_value.pb.h" +#include "tensorflow/core/framework/function.pb.h" +#include "tensorflow/core/framework/graph.pb.h" +#include "tensorflow/core/framework/node_def.pb.h" +#include "tensorflow/core/framework/tensor.h" +#include "tensorflow/core/framework/versions.pb.h" +#include "tensorflow/core/lib/strings/str_util.h" +#include "tensorflow/core/platform/status.h" +#include "tensorflow/core/platform/statusor.h" +#include "tensorflow/core/platform/types.h" +#include "tensorflow/core/protobuf/meta_graph.pb.h" +#include "tensorflow/core/public/session.h" + +namespace tensorflow { + +namespace { + +// Gets tensor names from tensor_info and inserts them into the set of tensor +// names. +void GetTensorNamesFromTensorInfo( + const TensorInfo& tensor_info, + std::unordered_set* tensor_names) { + if (tensor_info.has_coo_sparse()) { + // If the tensor is sparse we have to add all three tensors of the sparse + // representations. + const TensorInfo_CooSparse& coo_sparse = tensor_info.coo_sparse(); + tensor_names->insert(coo_sparse.values_tensor_name()); + tensor_names->insert(coo_sparse.indices_tensor_name()); + tensor_names->insert(coo_sparse.dense_shape_tensor_name()); + } else if (tensor_info.has_composite_tensor()) { + for (const auto& component : tensor_info.composite_tensor().components()) { + tensor_names->insert(component.name()); + } + } else { + tensor_names->insert(tensor_info.name()); + } +} + +// Gets the union of all inputs and outputs of all SignatureDefs in the bundle +void GetSignatureDefsInputsAndOutputs( + const SavedModelBundle& saved_model_bundle, + std::unordered_set* inputs, + std::unordered_set* outputs) { + for (auto& sigdef_elem : saved_model_bundle.meta_graph_def.signature_def()) { + const SignatureDef& signature_def = sigdef_elem.second; + for (auto& input_elem : signature_def.inputs()) { + GetTensorNamesFromTensorInfo(input_elem.second, inputs); + } + for (auto& output_elem : signature_def.outputs()) { + GetTensorNamesFromTensorInfo(output_elem.second, outputs); + } + } +} + +// Gets a map from string node name to NodeDef. +void GetNodeNameToNodeDefMap( + GraphDef* graph_def, + std::unordered_map* name_to_node_map) { + for (size_t i = 0; i < graph_def->node_size(); i++) { + NodeDef* node = graph_def->mutable_node(i); + (*name_to_node_map)[node->name()] = node; + } +} + +// Strips off the tensor part of the tensor_name to get the node_name. +const std::string GetNodeNameFromTensorName(std::string tensor_name) { + if (tensor_name[0] == '^') { + tensor_name.erase(0, 1); + } + std::vector tensor_name_parts = + str_util::Split(tensor_name, ':'); + return tensor_name_parts[0]; +} + +// Gets the set of node names needed by `outputs` and the corresponding set of +// variable nodes to convert. +void GetReachableNodesAndVariables( + GraphDef* graph_def, const std::unordered_set& outputs, + const std::unordered_map& name_to_node_map, + std::unordered_set* reachable_node_names, + std::unordered_set* variable_node_names) { + // TODO(suharshs): Add support for ResourceVariables. + static const std::unordered_set* kVariableTypes = + new std::unordered_set( + {"Variable", "VariableV2", "VarHandleOp"}); + + std::queue nodes_to_visit; + for (const std::string& output_tensor_name : outputs) { + nodes_to_visit.push(GetNodeNameFromTensorName(output_tensor_name)); + } + // We do a traversal backwards from the outputs specified in the MetaGraphDef. + while (!nodes_to_visit.empty()) { + const std::string node_name = nodes_to_visit.front(); + nodes_to_visit.pop(); + if (reachable_node_names->find(node_name) != reachable_node_names->end()) { + continue; + } + reachable_node_names->insert(node_name); + NodeDef* node = name_to_node_map.at(node_name); + if (kVariableTypes->find(node->op()) != kVariableTypes->end()) { + variable_node_names->insert(node->name()); + } + for (const std::string& input_tensor_name : node->input()) { + nodes_to_visit.push(GetNodeNameFromTensorName(input_tensor_name)); + } + } +} + +// Gets a map from variable name to variable value. +absl::Status GetVariableNameToTensorMap( + Session* session, + const std::unordered_map& name_to_node_map, + std::unordered_set variable_names_set, + std::unordered_map* variable_name_to_value_map) { + if (variable_names_set.empty()) { + return absl::OkStatus(); + } + std::vector variable_names; + variable_names.reserve(variable_names_set.size()); + std::vector tensor_names; + tensor_names.reserve(variable_names_set.size()); + for (const std::string& node_name : variable_names_set) { + variable_names.push_back(node_name); + NodeDef* node_def = name_to_node_map.at(node_name); + if (node_def->op() == "VarHandleOp") { + // If this is a resource variable, we have to run the corresponding + // ReadVariableOp. + tensor_names.push_back(node_name + "/Read/ReadVariableOp:0"); + } else { + tensor_names.push_back(node_name + ":0"); + } + } + std::vector outputs; + TF_RETURN_IF_ERROR( + session->Run(/* inputs */ {}, tensor_names, /* targets */ {}, &outputs)); + for (size_t i = 0; i < variable_names.size(); i++) { + (*variable_name_to_value_map)[variable_names[i]] = outputs[i]; + } + return absl::OkStatus(); +} + +// Converts a Variable NodeDef into a Constant NodeDef. +void ConvertVariableToConstant(const NodeDef& variable_node, + const Tensor& variable_value, + NodeDef* const_node) { + const_node->set_name(variable_node.name()); + const_node->set_op("Const"); + (*const_node->mutable_attr())["dtype"] = variable_node.attr().at("dtype"); + variable_value.AsProtoTensorContent( + (*const_node->mutable_attr())["value"].mutable_tensor()); +} + +// Converts a ReadVariableOp NodeDef to an Identity NodeDef. +void ConvertReadVariableOpToIdentity(const NodeDef& node, + NodeDef* identity_node) { + identity_node->set_name(node.name()); + identity_node->set_op("Identity"); + (*identity_node->mutable_attr())["T"] = node.attr().at("dtype"); + identity_node->add_input(node.input(0)); +} + +// Returns the name of the VarHandleOp that provides input (possibly indirectly) +// to node with node_name. A typical indirect chain of nodes (that can occur due +// to graph inlining) is the following: VarHandleOp -> Identity -> Identity -> +// ReadVariableOp. Calling the function on any of these nodes would return the +// name of the VarHandleOp. +absl::StatusOr GetVarHandleName( + const std::unordered_map& name_to_node_map, + std::string node_name) { + const NodeDef* node = name_to_node_map.at(node_name); + while (node->input_size() > 0) { + auto parent = name_to_node_map.find(node->input(0)); + if (parent == name_to_node_map.end()) break; + node = parent->second; + if (node->op() != "Identity") { + VLOG(2) << "Stopping at non-identity node " << node->op(); + break; + } + } + if (node->op() == "VarHandleOp") { + return node->name(); + } + return absl::NotFoundError("No VarHandleOp ancestor found"); +} + +// Looks up the variable handle that provides input to node with node_name, +// and returns the handle name if the handle corresponds to a variable that we +// want to freeze (i.e. its name is contained in variable_node_names). If there +// is no such handle in the graph (or we do not want to save that variable) +// then NotFound error is returned. +absl::StatusOr GetHandleNameIfNeedsToFreeze( + const std::unordered_map& name_to_node_map, + std::string node_name, + const std::unordered_set& variable_node_names) { + absl::StatusOr var_handle_name = + GetVarHandleName(name_to_node_map, node_name); + if (var_handle_name.ok() && variable_node_names.count(*var_handle_name)) { + return var_handle_name; + } + return absl::NotFoundError("No VarHandleOp ancestor found"); +} + +// Freezes the subgraph of all nodes needed by `outputs`. +absl::Status FreezeGraphDef(const SavedModelBundle& saved_model_bundle, + const std::unordered_set& outputs, + GraphDef* frozen_graph_def) { + GraphDef graph_def = saved_model_bundle.meta_graph_def.graph_def(); + // Copy versions and library as-is from original graph. + *frozen_graph_def->mutable_versions() = graph_def.versions(); + *frozen_graph_def->mutable_library() = graph_def.library(); + // If the graph is empty there is nothing left to do. + if (graph_def.node_size() == 0) { + return absl::OkStatus(); + } + // name_to_node_map is needed to get the inputs from the NodeDef corresponding + // the a string node name. These inputs are used when doing our backwards + // traversal. + std::unordered_map name_to_node_map; + GetNodeNameToNodeDefMap(&graph_def, &name_to_node_map); + std::unordered_set reachable_node_names; + std::unordered_set variable_node_names; + GetReachableNodesAndVariables(&graph_def, outputs, name_to_node_map, + &reachable_node_names, &variable_node_names); + std::unordered_map variable_to_value_map; + TF_RETURN_IF_ERROR(GetVariableNameToTensorMap( + saved_model_bundle.session.get(), name_to_node_map, variable_node_names, + &variable_to_value_map)); + // We copy the nodes in the same order they were in the original graph_def. + for (const NodeDef& node : graph_def.node()) { + if (reachable_node_names.find(node.name()) == reachable_node_names.end()) { + continue; + } + if (variable_node_names.find(node.name()) != variable_node_names.end()) { + ConvertVariableToConstant(node, variable_to_value_map[node.name()], + frozen_graph_def->add_node()); + continue; + } else if (node.op() == "ReadVariableOp" && + GetHandleNameIfNeedsToFreeze(name_to_node_map, node.name(), + variable_node_names) + .ok()) { + // If the node is a ReadVariableOp, its input VarHandleOp will be + // converted to a Constant, so we will need to convert it to an Identity. + ConvertReadVariableOpToIdentity(node, frozen_graph_def->add_node()); + continue; + } else if (node.op() == "Identity") { + absl::StatusOr handle_name = GetHandleNameIfNeedsToFreeze( + name_to_node_map, node.name(), variable_node_names); + if (handle_name.ok()) { + // Identity node that is forwarding the value of a frozen + // VarhandleOp. We ensure that the dtype matches of the variable dtype. + NodeDef* new_node = frozen_graph_def->add_node(); + *new_node = node; + (*new_node->mutable_attr())["T"] = + name_to_node_map.at(*handle_name)->attr().at("dtype"); + continue; + } + } + // If the node isn't a variable, just copy the node as-is. + *frozen_graph_def->add_node() = node; + } + return absl::OkStatus(); +} + +} // namespace + +absl::Status FreezeSavedModel(const SavedModelBundle& saved_model_bundle, + GraphDef* frozen_graph_def, + std::unordered_set* inputs, + std::unordered_set* outputs) { + GetSignatureDefsInputsAndOutputs(saved_model_bundle, inputs, outputs); + TF_RETURN_IF_ERROR( + FreezeGraphDef(saved_model_bundle, *outputs, frozen_graph_def)); + return absl::OkStatus(); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/cc/tools/freeze_saved_model.h b/western_ai/usa/tensorflow/tensorflow/cc/tools/freeze_saved_model.h new file mode 100644 index 0000000000000000000000000000000000000000..dff94559dc7172c71a406eccf6f4a0b3acc93c2a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/tools/freeze_saved_model.h @@ -0,0 +1,45 @@ +/* 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. +==============================================================================*/ +#ifndef TENSORFLOW_CC_TOOLS_FREEZE_SAVED_MODEL_H_ +#define TENSORFLOW_CC_TOOLS_FREEZE_SAVED_MODEL_H_ + +#include + +#include "absl/status/status.h" +#include "tensorflow/cc/saved_model/loader.h" +#include "tensorflow/core/framework/graph.pb.h" +#include "tensorflow/core/lib/core/status.h" +#include "tensorflow/core/platform/types.h" + +namespace tensorflow { + +// Returns a frozen GraphDef, input tensors, and output tensors from the loaded +// SavedModelBundle. +// `inputs` and `outputs` consist of the union of all inputs and outputs in the +// SignatureDefs in the SavedModelBundle. +// FreezeSavedModel sets `frozen_graph_def` to a GraphDef of all nodes needed by +// `outputs`. All variables in the supplied SavedModelBundle are converted to +// constants, set to the value of the variables, by running the restored Session +// in the SavedModelBundle. +// WARNING: Only the variable checkpoints will be reflected in the frozen +// graph_def. All saved_model assets will be ignored. +absl::Status FreezeSavedModel(const SavedModelBundle& saved_model_bundle, + GraphDef* frozen_graph_def, + std::unordered_set* inputs, + std::unordered_set* outputs); + +} // namespace tensorflow + +#endif // TENSORFLOW_CC_TOOLS_FREEZE_SAVED_MODEL_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/tools/freeze_saved_model_test.cc b/western_ai/usa/tensorflow/tensorflow/cc/tools/freeze_saved_model_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..1cf3cdc994bf522b44fc39ed68fa34055e2bd5c5 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/tools/freeze_saved_model_test.cc @@ -0,0 +1,521 @@ +/* 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/cc/tools/freeze_saved_model.h" + +#include +#include +#include +#include +#include + +#include "absl/status/status.h" +#include "tensorflow/cc/framework/ops.h" +#include "tensorflow/cc/framework/scope.h" +#include "tensorflow/cc/ops/array_ops.h" +#include "tensorflow/cc/ops/const_op.h" +#include "tensorflow/cc/ops/math_ops.h" +#include "tensorflow/cc/ops/resource_variable_ops.h" +#include "tensorflow/cc/ops/state_ops.h" +#include "tensorflow/cc/saved_model/loader.h" +#include "xla/tsl/lib/core/status_test_util.h" +#include "xla/tsl/platform/errors.h" +#include "tensorflow/core/framework/function_testlib.h" +#include "tensorflow/core/framework/graph.pb.h" +#include "tensorflow/core/framework/tensor.h" +#include "tensorflow/core/framework/tensor_testutil.h" +#include "tensorflow/core/framework/types.pb.h" +#include "tensorflow/core/framework/versions.pb.h" +#include "tensorflow/core/platform/status.h" +#include "tensorflow/core/platform/test.h" +#include "tensorflow/core/platform/types.h" +#include "tensorflow/core/protobuf/meta_graph.pb.h" +#include "tensorflow/core/public/session.h" +#include "tensorflow/core/public/session_options.h" + +namespace tensorflow { +namespace { + +class FreezeTest : public ::testing::Test { + protected: + void GraphDefEqual(const GraphDef& actual, const GraphDef& expected) { + EXPECT_EQ(actual.ShortDebugString(), expected.ShortDebugString()); + } + + // Builds a SignatureDef with the provided `inputs` and `outputs`. + SignatureDef BuildSignatureDef( + const std::unordered_set& inputs, + const std::unordered_set& outputs) { + SignatureDef signature_def; + for (const std::string& input : inputs) { + (*signature_def.mutable_inputs())[input].set_name(input); + } + for (const std::string& output : outputs) { + (*signature_def.mutable_outputs())[output].set_name(output); + } + return signature_def; + } + + // Adds `signature_def` to `saved_model_bundle` under `key`. + void AddSignatureDefToSavedModelBundle(const SignatureDef& signature_def, + const std::string& key, + SavedModelBundle* saved_model_bundle) { + MetaGraphDef* meta_graph_def = &saved_model_bundle->meta_graph_def; + (*meta_graph_def->mutable_signature_def())[key] = signature_def; + } + + // Adds an initialized session to `saved_model_bundle` using `graph_def` and + // initializing with `init_node`. + absl::Status InitializeSavedModelBundleSession( + const GraphDef& graph_def, const std::string& init_node, + SavedModelBundle* saved_model_bundle) { + SessionOptions session_options; + saved_model_bundle->session.reset(NewSession(session_options)); + TF_RETURN_IF_ERROR(saved_model_bundle->session->Create(graph_def)); + if (!init_node.empty()) { + std::vector outputs; + return saved_model_bundle->session->Run( + /* inputs */ {}, /* output_tensors */ {}, {init_node}, &outputs); + } + return absl::OkStatus(); + } + + // Adds `graph_def` to `saved_model_bundle` and initializes a session with + // `init_node`. + absl::Status AddGraphDefToSavedModelBundle( + const GraphDef& graph_def, const std::string& init_node, + SavedModelBundle* saved_model_bundle) { + MetaGraphDef* meta_graph_def = &saved_model_bundle->meta_graph_def; + *meta_graph_def->mutable_graph_def() = graph_def; + return InitializeSavedModelBundleSession(graph_def, init_node, + saved_model_bundle); + } + + // Adds `graph_def` and `outputs` as the GraphDef and SignatureDef in + // `saved_model_bundle` and initializes a session with `init_node`. + absl::Status AddGraphDefWithOutputsToSavedModelBundle( + const GraphDef& graph_def, const std::unordered_set& outputs, + const std::string& init_node, SavedModelBundle* saved_model_bundle) { + SignatureDef signature_def = + BuildSignatureDef(std::unordered_set(), outputs); + AddSignatureDefToSavedModelBundle(signature_def, "signature_def", + saved_model_bundle); + return AddGraphDefToSavedModelBundle(graph_def, init_node, + saved_model_bundle); + } + + // Runs and compares the outputs of `tensor_name` on both the + // `unfrozen_session` and the `frozen_graph_def. + void RunAndCompareFrozenAndUnfrozenGraphs(Session* unfrozen_session, + const GraphDef& frozen_graph_def, + const std::string& tensor_name) { + std::vector unfrozen_outputs; + TF_ASSERT_OK(unfrozen_session->Run(/* inputs */ {}, {tensor_name}, + /* targets */ {}, &unfrozen_outputs)); + + SessionOptions session_options; + std::unique_ptr frozen_session(NewSession(session_options)); + TF_ASSERT_OK(frozen_session->Create(frozen_graph_def)); + std::vector frozen_outputs; + TF_ASSERT_OK(frozen_session->Run(/* inputs */ {}, {tensor_name}, + /* targets */ {}, &frozen_outputs)); + + test::ExpectTensorEqual(unfrozen_outputs[0], frozen_outputs[0]); + } + + void TestFreezeGraphWithoutDependentVariables(bool use_resource) { + // Test freezing a graph with variables that are not needed by the outputs + // in the SignatureDef. The resulting graph shouldn't be frozen, but + // non-dependent nodes should be pruned. + SavedModelBundle saved_model_bundle; + GraphDef graph_def; + Scope scope = Scope::NewRootScope(); + Output a = ops::Const(scope.WithOpName("a"), 10.0f, {}); + Output b = ops::Const(scope.WithOpName("b"), 10.0f, {}); + Output c = ops::Mul(scope.WithOpName("c"), a, b); + if (use_resource) { + Output var = + ops::VarHandleOp(scope.WithOpName("var"), DataType::DT_FLOAT, {}); + Output read_var = ops::ReadVariableOp( + scope.WithOpName("var/Read/ReadVariableOp"), var, DataType::DT_FLOAT); + auto assign = ops::AssignVariableOp(scope.WithOpName("assign"), var, a); + } else { + Output var = + ops::Variable(scope.WithOpName("var"), {}, DataType::DT_FLOAT); + Output assign = ops::Assign(scope.WithOpName("assign"), var, a); + } + + TF_ASSERT_OK(scope.ToGraphDef(&graph_def)); + // "c" isn't dependent on the variable, so nothing should be frozen. + TF_ASSERT_OK(AddGraphDefWithOutputsToSavedModelBundle( + graph_def, {"c:0"}, "assign", &saved_model_bundle)); + + GraphDef frozen_graph_def; + std::unordered_set inputs; + std::unordered_set outputs; + TF_ASSERT_OK(FreezeSavedModel(saved_model_bundle, &frozen_graph_def, + &inputs, &outputs)); + + GraphDef expected_graph_def; + Scope expected_scope = Scope::NewRootScope(); + Output expected_a = ops::Const(expected_scope.WithOpName("a"), 10.0f, {}); + Output expected_b = ops::Const(expected_scope.WithOpName("b"), 10.0f, {}); + Output expected_c = + ops::Mul(expected_scope.WithOpName("c"), expected_a, expected_b); + TF_ASSERT_OK(expected_scope.ToGraphDef(&expected_graph_def)); + + GraphDefEqual(frozen_graph_def, expected_graph_def); + + RunAndCompareFrozenAndUnfrozenGraphs(saved_model_bundle.session.get(), + frozen_graph_def, "c:0"); + } + + void TestFreezeGraphWithDependentVariables(bool use_resource, + bool use_identity = false) { + // Test freezing a graph with variables that are needed by outputs in the + // SignatureDef. The variables should be frozen. + SavedModelBundle saved_model_bundle; + GraphDef graph_def; + Scope scope = Scope::NewRootScope(); + Output a = ops::Const(scope.WithOpName("a"), 10.0f, {}); + Output read_var; + if (use_resource) { + Output var = + ops::VarHandleOp(scope.WithOpName("var"), DataType::DT_FLOAT, {}); + if (use_identity) { + Output identity = ops::Identity(scope.WithOpName("identity"), var); + read_var = + ops::ReadVariableOp(scope.WithOpName("var/Read/ReadVariableOp"), + identity, DataType::DT_FLOAT); + } else { + read_var = + ops::ReadVariableOp(scope.WithOpName("var/Read/ReadVariableOp"), + var, DataType::DT_FLOAT); + } + auto assign = ops::AssignVariableOp(scope.WithOpName("assign"), var, a); + } else { + Output read_var = + ops::Variable(scope.WithOpName("var"), {}, DataType::DT_FLOAT); + Output assign = ops::Assign(scope.WithOpName("assign"), read_var, a); + } + Output c = ops::Mul(scope.WithOpName("c"), a, read_var); + TF_ASSERT_OK(scope.ToGraphDef(&graph_def)); + // "c" isn't dependent on the variable, so nothing should be frozen. + TF_ASSERT_OK(AddGraphDefWithOutputsToSavedModelBundle( + graph_def, {"c:0"}, "assign", &saved_model_bundle)); + + GraphDef frozen_graph_def; + std::unordered_set inputs; + std::unordered_set outputs; + TF_ASSERT_OK(FreezeSavedModel(saved_model_bundle, &frozen_graph_def, + &inputs, &outputs)); + + // If using normal variables there should be 3 nodes in the resulting + // graph_def. If using resource variables there should be 4 nodes in the + // resulting graph_def if use_identity == false, otherwise 5 variables. + // In both cases, none should be variables. + size_t expected_nodes = use_resource ? (use_identity ? 5 : 4) : 3; + + EXPECT_EQ(frozen_graph_def.node_size(), expected_nodes); + for (const NodeDef& node : frozen_graph_def.node()) { + EXPECT_NE(node.op(), "Variable") << node.name(); + EXPECT_NE(node.op(), "VariableV2") << node.name(); + EXPECT_NE(node.op(), "VarHandleOp") << node.name(); + EXPECT_NE(node.op(), "ReadVariableOp") << node.name(); + } + + RunAndCompareFrozenAndUnfrozenGraphs(saved_model_bundle.session.get(), + frozen_graph_def, "c:0"); + } + + void TestFreezeGraphWithAndWithoutDependentVariables(bool use_resource) { + // Test freezing a graph with some variables that are needed and not needed + // by + // the outputs in the SignatureDef. The resulting graph should only freeze + // dependent variables. + SavedModelBundle saved_model_bundle; + GraphDef graph_def; + Scope scope = Scope::NewRootScope(); + Output a = ops::Const(scope.WithOpName("a"), 10.0f, {}); + Output read_var; + + if (use_resource) { + Output var = + ops::VarHandleOp(scope.WithOpName("var"), DataType::DT_FLOAT, {}); + read_var = ops::ReadVariableOp( + scope.WithOpName("var/Read/ReadVariableOp"), var, DataType::DT_FLOAT); + auto assign = ops::AssignVariableOp(scope.WithOpName("assign"), var, a); + Output var_1 = + ops::VarHandleOp(scope.WithOpName("var_1"), DataType::DT_FLOAT, {}); + Output read_var_1 = + ops::ReadVariableOp(scope.WithOpName("var_1/Read/ReadVariableOp"), + var, DataType::DT_FLOAT); + auto assign_1 = + ops::AssignVariableOp(scope.WithOpName("assign_1"), var_1, a); + } else { + read_var = ops::Variable(scope.WithOpName("var"), {}, DataType::DT_FLOAT); + Output assign = ops::Assign(scope.WithOpName("assign"), read_var, a); + Output var_1 = + ops::Variable(scope.WithOpName("var_1"), {}, DataType::DT_FLOAT); + Output assign_1 = ops::Assign(scope.WithOpName("assign_1"), var_1, a); + } + + Output c = ops::Mul(scope.WithOpName("c"), a, read_var); + TF_ASSERT_OK(scope.ToGraphDef(&graph_def)); + // "c" isn't dependent on the variable, so nothing should be frozen. + TF_ASSERT_OK(AddGraphDefWithOutputsToSavedModelBundle( + graph_def, {"c:0"}, "assign", &saved_model_bundle)); + + GraphDef frozen_graph_def; + std::unordered_set inputs; + std::unordered_set outputs; + TF_ASSERT_OK(FreezeSavedModel(saved_model_bundle, &frozen_graph_def, + &inputs, &outputs)); + + // There should be 3 nodes in the resulting graph_def, and none should be + // variables. + size_t expected_nodes = use_resource ? 4 : 3; + EXPECT_EQ(frozen_graph_def.node_size(), expected_nodes); + for (const NodeDef& node : frozen_graph_def.node()) { + EXPECT_NE(node.op(), "Variable") << node.name(); + EXPECT_NE(node.op(), "VariableV2") << node.name(); + EXPECT_NE(node.op(), "VarHandleOp") << node.name(); + EXPECT_NE(node.op(), "ReadVariableOp") << node.name(); + } + + RunAndCompareFrozenAndUnfrozenGraphs(saved_model_bundle.session.get(), + frozen_graph_def, "c:0"); + } +}; + +TEST_F(FreezeTest, InputsAndOutputsSingleSignatureDef) { + // Test that inputs and outputs get correctly populated for a single + // SignatureDef. + SavedModelBundle saved_model_bundle; + std::unordered_set expected_inputs = {"input0:0", "input1:0"}; + std::unordered_set expected_outputs = {"output0:0", "output1:0"}; + SignatureDef signature_def = + BuildSignatureDef(expected_inputs, expected_outputs); + AddSignatureDefToSavedModelBundle(signature_def, "signature_def", + &saved_model_bundle); + GraphDef frozen_graph_def; + std::unordered_set inputs; + std::unordered_set outputs; + TF_ASSERT_OK(FreezeSavedModel(saved_model_bundle, &frozen_graph_def, &inputs, + &outputs)); + EXPECT_EQ(expected_inputs, inputs); + EXPECT_EQ(expected_outputs, outputs); +} + +TEST_F(FreezeTest, InputsAndOutputsMultipleSignatureDefs) { + // Test that inputs and outputs get correctly merged and populated when + // multiple SignatureDefs are provided. + SavedModelBundle saved_model_bundle; + SignatureDef signature_def_0 = BuildSignatureDef({"input0:0"}, {"output0:0"}); + SignatureDef signature_def_1 = BuildSignatureDef({"input1:0"}, {"output1:0"}); + AddSignatureDefToSavedModelBundle(signature_def_0, "signature_def_0", + &saved_model_bundle); + AddSignatureDefToSavedModelBundle(signature_def_1, "signature_def_1", + &saved_model_bundle); + GraphDef frozen_graph_def; + std::unordered_set inputs; + std::unordered_set outputs; + TF_ASSERT_OK(FreezeSavedModel(saved_model_bundle, &frozen_graph_def, &inputs, + &outputs)); + std::unordered_set expected_inputs = {"input0:0", "input1:0"}; + std::unordered_set expected_outputs = {"output0:0", "output1:0"}; + EXPECT_EQ(expected_inputs, inputs); + EXPECT_EQ(expected_outputs, outputs); +} + +TEST_F(FreezeTest, GraphDefVersionsAndLibrary) { + // Test that GraphDef versions and library are copied correctly into the + // frozen graph. + SavedModelBundle saved_model_bundle; + GraphDef graph_def; + graph_def.mutable_versions()->set_producer(1234); + graph_def.mutable_versions()->set_min_consumer(1234); + *graph_def.mutable_library()->add_function() = test::function::NonZero(); + TF_ASSERT_OK( + AddGraphDefToSavedModelBundle(graph_def, "", &saved_model_bundle)); + + GraphDef frozen_graph_def; + std::unordered_set inputs; + std::unordered_set outputs; + TF_ASSERT_OK(FreezeSavedModel(saved_model_bundle, &frozen_graph_def, &inputs, + &outputs)); + + GraphDefEqual(frozen_graph_def, graph_def); +} + +TEST_F(FreezeTest, GraphDefWithNoVariables) { + // Test freezing a graph with no variables. + SavedModelBundle saved_model_bundle; + GraphDef graph_def; + Scope scope = Scope::NewRootScope(); + Output a = ops::Const(scope.WithOpName("a"), 10.0f, {}); + Output b = ops::Const(scope.WithOpName("b"), 10.0f, {}); + Output c = ops::Mul(scope.WithOpName("c"), a, b); + TF_ASSERT_OK(scope.ToGraphDef(&graph_def)); + TF_ASSERT_OK(AddGraphDefWithOutputsToSavedModelBundle(graph_def, {"c:0"}, "", + &saved_model_bundle)); + + GraphDef frozen_graph_def; + std::unordered_set inputs; + std::unordered_set outputs; + TF_ASSERT_OK(FreezeSavedModel(saved_model_bundle, &frozen_graph_def, &inputs, + &outputs)); + + GraphDefEqual(frozen_graph_def, graph_def); +} + +TEST_F(FreezeTest, GraphDefWithMultiOutputOperation) { + // Tensors from operations with multiple outputs get tensor suffixes when used + // in input fields of following nodes, i.e. split:0, split:1. + // Test that we traverse those correctly. + SavedModelBundle saved_model_bundle; + GraphDef graph_def; + Scope scope = Scope::NewRootScope(); + Output a = ops::Const(scope.WithOpName("a"), {10.0f, 10.0f}, {2}); + Output axis = ops::Const(scope.WithOpName("axis"), 0, {}); + OutputList split = ops::Split(scope.WithOpName("split"), axis, a, 2).output; + Output b = ops::Const(scope.WithOpName("b"), 10.0f, {}); + Output c = ops::Mul(scope.WithOpName("c"), split[1], b); + TF_ASSERT_OK(scope.ToGraphDef(&graph_def)); + TF_ASSERT_OK(AddGraphDefWithOutputsToSavedModelBundle(graph_def, {"c:0"}, "", + &saved_model_bundle)); + + GraphDef frozen_graph_def; + std::unordered_set inputs; + std::unordered_set outputs; + TF_ASSERT_OK(FreezeSavedModel(saved_model_bundle, &frozen_graph_def, &inputs, + &outputs)); + + GraphDefEqual(frozen_graph_def, graph_def); +} + +TEST_F(FreezeTest, GraphDefWithControlDependency) { + // Inputs that are control dependencies get tensor prefixes, + // i.e. ^control_dependency. + // Test that we traverse those correctly. + SavedModelBundle saved_model_bundle; + GraphDef graph_def; + Scope scope = Scope::NewRootScope(); + Output source = ops::Const(scope.WithOpName("source"), 10.0f, {}); + Output a = ops::Const(scope.WithOpName("a").WithControlDependencies(source), + {10.0f, 10.0f}, {2}); + Output b = ops::Const(scope.WithOpName("b"), 10.0f, {}); + Output c = ops::Mul(scope.WithOpName("c"), a, b); + TF_ASSERT_OK(scope.ToGraphDef(&graph_def)); + TF_ASSERT_OK(AddGraphDefWithOutputsToSavedModelBundle(graph_def, {"c:0"}, "", + &saved_model_bundle)); + + GraphDef frozen_graph_def; + std::unordered_set inputs; + std::unordered_set outputs; + TF_ASSERT_OK(FreezeSavedModel(saved_model_bundle, &frozen_graph_def, &inputs, + &outputs)); + + GraphDefEqual(frozen_graph_def, graph_def); +} + +TEST_F(FreezeTest, GraphDefWithoutDependentVariables) { + TestFreezeGraphWithoutDependentVariables(false); +} + +TEST_F(FreezeTest, GraphDefWithoutDependentResourceVariables) { + TestFreezeGraphWithoutDependentVariables(true); +} + +TEST_F(FreezeTest, GraphDefWithDependentVariables) { + TestFreezeGraphWithDependentVariables(false); +} + +TEST_F(FreezeTest, GraphDefWithDependentResourceVariables) { + TestFreezeGraphWithDependentVariables(true); +} + +TEST_F(FreezeTest, GraphDefWithDependentResourceVariablesAndIdentity) { + TestFreezeGraphWithDependentVariables(true, true); +} + +TEST_F(FreezeTest, GraphDefWithAndWithoutDependentVariables) { + TestFreezeGraphWithAndWithoutDependentVariables(false); +} + +TEST_F(FreezeTest, GraphDefWithAndWithoutDependentResourceVariables) { + TestFreezeGraphWithAndWithoutDependentVariables(true); +} + +TEST_F(FreezeTest, InputsAndOutputsCompositeTensorSignatureDef) { + // Test that inputs and outputs get correctly populated for a + // SignatureDef containing composite tensor inputs and outputs. + SavedModelBundle saved_model_bundle; + SignatureDef signature_def; + + TensorInfo& in = (*signature_def.mutable_inputs())["input_arg"]; + in.mutable_composite_tensor()->add_components()->set_name("input1:0"); + in.mutable_composite_tensor()->add_components()->set_name("input2:0"); + + TensorInfo& out = (*signature_def.mutable_outputs())["output_arg"]; + out.mutable_composite_tensor()->add_components()->set_name("output2:0"); + out.mutable_composite_tensor()->add_components()->set_name("output1:0"); + + AddSignatureDefToSavedModelBundle(signature_def, "signature_def", + &saved_model_bundle); + GraphDef frozen_graph_def; + std::unordered_set inputs; + std::unordered_set outputs; + TF_ASSERT_OK(FreezeSavedModel(saved_model_bundle, &frozen_graph_def, &inputs, + &outputs)); + std::unordered_set expected_inputs = {"input1:0", "input2:0"}; + std::unordered_set expected_outputs = {"output1:0", "output2:0"}; + EXPECT_EQ(expected_inputs, inputs); + EXPECT_EQ(expected_outputs, outputs); +} + +TEST_F(FreezeTest, InputsAndOutputsSparseCooSignatureDef) { + // Test that inputs and outputs get correctly populated for a + // SignatureDef containing composite tensor inputs and outputs. + SavedModelBundle saved_model_bundle; + SignatureDef signature_def; + + TensorInfo& in = (*signature_def.mutable_inputs())["input_arg"]; + in.mutable_coo_sparse()->set_values_tensor_name("input1:0"); + in.mutable_coo_sparse()->set_indices_tensor_name("input2:0"); + in.mutable_coo_sparse()->set_dense_shape_tensor_name("input3:0"); + + TensorInfo& out = (*signature_def.mutable_outputs())["output_arg"]; + out.mutable_coo_sparse()->set_values_tensor_name("output1:0"); + out.mutable_coo_sparse()->set_indices_tensor_name("output2:0"); + out.mutable_coo_sparse()->set_dense_shape_tensor_name("output3:0"); + + AddSignatureDefToSavedModelBundle(signature_def, "signature_def", + &saved_model_bundle); + GraphDef frozen_graph_def; + std::unordered_set inputs; + std::unordered_set outputs; + TF_ASSERT_OK(FreezeSavedModel(saved_model_bundle, &frozen_graph_def, &inputs, + &outputs)); + std::unordered_set expected_inputs = {"input1:0", "input2:0", + "input3:0"}; + std::unordered_set expected_outputs = {"output1:0", "output2:0", + "output3:0"}; + EXPECT_EQ(expected_inputs, inputs); + EXPECT_EQ(expected_outputs, outputs); +} + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/cc/training/coordinator.cc b/western_ai/usa/tensorflow/tensorflow/cc/training/coordinator.cc new file mode 100644 index 0000000000000000000000000000000000000000..cd73ffe590cdf298c92ec3a297f542329271556e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/training/coordinator.cc @@ -0,0 +1,140 @@ +/* Copyright 2016 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/cc/training/coordinator.h" + +#include +#include +#include + +#include "absl/status/status.h" +#include "xla/tsl/protobuf/error_codes.pb.h" +#include "tensorflow/core/framework/cost_graph.pb.h" +#include "tensorflow/core/platform/mutex.h" +#include "tensorflow/core/platform/status.h" + +namespace tensorflow { + +Coordinator::Coordinator() : Coordinator(std::vector()) {} + +Coordinator::Coordinator(const std::vector& clean_stop_errors) + : should_stop_(false) { + if (clean_stop_errors.empty()) { + clean_stop_errors_.insert(error::OUT_OF_RANGE); + } else { + for (const auto& code : clean_stop_errors) { + clean_stop_errors_.insert(static_cast(code)); + } + } +} + +Coordinator::~Coordinator() { + RequestStop().IgnoreError(); + Join().IgnoreError(); +} + +absl::Status Coordinator::RegisterRunner( + std::unique_ptr runner) { + { + mutex_lock l(mu_); + if (should_stop_) { + return absl::Status(absl::StatusCode::kFailedPrecondition, + "The coordinator has been stopped."); + } + } + mutex_lock l(runners_lock_); + runners_.push_back(std::move(runner)); + return absl::OkStatus(); +} + +bool Coordinator::AllRunnersStopped() { + mutex_lock l(runners_lock_); + for (const auto& runner : runners_) { + if (runner->IsRunning()) { + return false; + } + } + return true; +} + +absl::Status Coordinator::RequestStop() { + mutex_lock l(mu_); + if (should_stop_) { + return absl::Status(absl::StatusCode::kFailedPrecondition, + "The Coordinator is not running."); + } + should_stop_ = true; + wait_for_stop_.notify_all(); + return absl::OkStatus(); +} + +bool Coordinator::ShouldStop() { + mutex_lock l(mu_); + return should_stop_; +} + +absl::Status Coordinator::Join() { + // TODO(yuefengz): deal with stragglers. + { + mutex_lock l(mu_); + if (!should_stop_) { + return absl::Status(absl::StatusCode::kFailedPrecondition, + "Joining coordinator without requesting to stop."); + } + } + + { + mutex_lock l(runners_lock_); + for (const auto& t : runners_) { + ReportStatus(t->Join()); + } + runners_.clear(); + } + return GetStatus(); +} + +void Coordinator::ReportStatus(const absl::Status& status) { + mutex_lock l(status_lock_); + if (status.ok() || !status_.ok() || + clean_stop_errors_.count(static_cast(status.code())) > 0) { + return; + } + status_ = status; +} + +absl::Status Coordinator::GetStatus() { + mutex_lock l(status_lock_); + return status_; +} + +void Coordinator::WaitForStop() { + mutex_lock l(mu_); + while (!should_stop_) { + wait_for_stop_.wait(l); + } +} + +absl::Status Coordinator::ExportCostGraph(CostGraphDef* cost_graph) const { + mutex_lock l(runners_lock_); + for (auto& t : runners_) { + absl::Status s = t->ExportCostGraph(cost_graph); + if (!s.ok()) { + return s; + } + } + return absl::OkStatus(); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/cc/training/coordinator.h b/western_ai/usa/tensorflow/tensorflow/cc/training/coordinator.h new file mode 100644 index 0000000000000000000000000000000000000000..f4d54059464e9b62cf35fb4132d7715cb81cac23 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/training/coordinator.h @@ -0,0 +1,136 @@ +/* Copyright 2016 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. +==============================================================================*/ + +#ifndef TENSORFLOW_CC_TRAINING_COORDINATOR_H_ +#define TENSORFLOW_CC_TRAINING_COORDINATOR_H_ + +#include +#include +#include +#include + +#include "absl/status/status.h" +#include "xla/tsl/protobuf/error_codes.pb.h" +#include "tensorflow/core/framework/cost_graph.pb.h" +#include "tensorflow/core/lib/core/status.h" +#include "tensorflow/core/platform/macros.h" +#include "tensorflow/core/platform/mutex.h" +#include "tensorflow/core/protobuf/config.pb.h" +#include "tensorflow/core/protobuf/error_codes.pb.h" +#include "tsl/platform/thread_annotations.h" + +namespace tensorflow { + +/// The abstract interface for runners which must implement the Join and the +/// IsRunning function. +class RunnerInterface { + public: + virtual ~RunnerInterface() = default; + virtual absl::Status Join() = 0; + virtual absl::Status ExportCostGraph(CostGraphDef* cost_graph) const { + return absl::Status(absl::StatusCode::kInvalidArgument, + "No cost model to export."); + } + /// Returns true iff the runner is running, i.e. if it is trying to populate + /// its queue. + virtual bool IsRunning() const = 0; +}; + +/// Coordinator class manages the termination of a collection of QueueRunners. +/// Without a coordinator, QueueRunners have to be joined in a specific order; +/// otherwise the QueueRunner::Join() could sometimes hang. The +/// Coordinator::RequestStop() plays the key role which notifies all running +/// threads under a coordinator to stop. This function could be called by any +/// thread or any client. +/// Usage, in the client: +/// Coordinator coord; +/// std::unique_ptr qr(&coord, ...); +/// qr.Start(session); +/// coord.RegisterRunner(std::move(qr)); +/// /// do some work +/// TF_CHECK_OK(coord.Join()); +/// In each thread of QueueRunner, the coordinator needs to be used as: +/// void Run() { +/// while (!coord->ShouldStop()) { +/// /// do some work +/// if (error) { +/// coord->RequestStop(); +/// coord->ReportStatus(error_status); +/// } +/// } +/// } +class Coordinator { + public: + Coordinator(); + + /// Constructor with a list of error codes which would not be taken as errors + /// in status reporting. + Coordinator(const std::vector& clean_stop_errors); + + /// In the destructor, RequestStop() and Join() would be called. + ~Coordinator(); + + /// Registers a runner, i.e. a unit of running threads which is usually a + /// QueueRunner. It takes the ownership of runner to avoid lifecycle-related + /// problems. Note, the coordinator would not start these threads; they are + /// supposed to be in running state when they are registered here. + absl::Status RegisterRunner(std::unique_ptr runner); + + /// Returns true iff all the registered runners have been stopped. + bool AllRunnersStopped(); + + /// Requests all running threads to stop. + absl::Status RequestStop(); + + /// Returns true if its RequestStop() has been called. + bool ShouldStop(); + + /// Joins all threads, returns OK or the first reported and unexpected status. + absl::Status Join(); + + /// Reports status to the coordinator. This is usually called by threads. + void ReportStatus(const absl::Status& status); + + /// Returns the latest status. + absl::Status GetStatus(); + + /// Returns immediately if the coordinator is stopped or blocks until + /// RequestStop() is called. + void WaitForStop(); + + // Returns the cost graph from stored run metadata in registered runners. + absl::Status ExportCostGraph(CostGraphDef* cost_graph) const; + + private: + std::unordered_set clean_stop_errors_; + condition_variable wait_for_stop_; + + mutex mu_; + bool should_stop_ TF_GUARDED_BY(mu_); + + mutex status_lock_; + absl::Status status_ TF_GUARDED_BY(status_lock_); + + mutable mutex runners_lock_; + std::vector> runners_ + TF_GUARDED_BY(runners_lock_); + + Coordinator(const Coordinator&) = delete; + void operator=(const Coordinator&) = delete; +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_CC_TRAINING_COORDINATOR_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/training/coordinator_test.cc b/western_ai/usa/tensorflow/tensorflow/cc/training/coordinator_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..73e3cf6fd25e9a5a86265c3f7cf9ad570aad3aee --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/training/coordinator_test.cc @@ -0,0 +1,236 @@ +/* Copyright 2016 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/cc/training/coordinator.h" + +#include +#include +#include +#include + +#include "absl/status/status.h" +#include "absl/synchronization/notification.h" +#include "xla/tsl/lib/core/status_test_util.h" +#include "xla/tsl/protobuf/error_codes.pb.h" +#include "tensorflow/core/platform/blocking_counter.h" +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/platform/status.h" +#include "tensorflow/core/platform/test.h" +#include "tensorflow/core/platform/threadpool.h" +#include "tensorflow/core/protobuf/error_codes.pb.h" + +namespace tensorflow { +namespace { + +using error::Code; + +void WaitForStopThread(Coordinator* coord, absl::Notification* about_to_wait, + absl::Notification* done) { + about_to_wait->Notify(); + coord->WaitForStop(); + done->Notify(); +} + +TEST(CoordinatorTest, TestStopAndWaitOnStop) { + Coordinator coord; + EXPECT_EQ(coord.ShouldStop(), false); + + absl::Notification about_to_wait; + absl::Notification done; + Env::Default()->SchedClosure( + std::bind(&WaitForStopThread, &coord, &about_to_wait, &done)); + about_to_wait.WaitForNotification(); + Env::Default()->SleepForMicroseconds(1000 * 1000); + EXPECT_FALSE(done.HasBeenNotified()); + + TF_EXPECT_OK(coord.RequestStop()); + done.WaitForNotification(); + EXPECT_TRUE(coord.ShouldStop()); +} + +class MockQueueRunner : public RunnerInterface { + public: + explicit MockQueueRunner(Coordinator* coord) { + coord_ = coord; + join_counter_ = nullptr; + thread_pool_ = + std::make_unique(Env::Default(), "test-pool", 10); + stopped_ = false; + } + + MockQueueRunner(Coordinator* coord, int* join_counter) + : MockQueueRunner(coord) { + join_counter_ = join_counter; + } + + void StartCounting(std::atomic* counter, int until, + absl::Notification* start = nullptr) { + thread_pool_->Schedule( + std::bind(&MockQueueRunner::CountThread, this, counter, until, start)); + } + + void StartSettingStatus(const absl::Status& status, BlockingCounter* counter, + absl::Notification* start) { + thread_pool_->Schedule(std::bind(&MockQueueRunner::SetStatusThread, this, + status, counter, start)); + } + + absl::Status Join() override { + if (join_counter_ != nullptr) { + (*join_counter_)++; + } + thread_pool_.reset(); + return status_; + } + + absl::Status GetStatus() { return status_; } + + void SetStatus(const absl::Status& status) { status_ = status; } + + bool IsRunning() const override { return !stopped_; }; + + void Stop() { stopped_ = true; } + + private: + void CountThread(std::atomic* counter, int until, + absl::Notification* start) { + if (start != nullptr) start->WaitForNotification(); + while (!coord_->ShouldStop() && counter->load() < until) { + (*counter)++; + Env::Default()->SleepForMicroseconds(10 * 1000); + } + coord_->RequestStop().IgnoreError(); + } + void SetStatusThread(const absl::Status& status, BlockingCounter* counter, + absl::Notification* start) { + start->WaitForNotification(); + SetStatus(status); + counter->DecrementCount(); + } + std::unique_ptr thread_pool_; + absl::Status status_; + Coordinator* coord_; + int* join_counter_; + bool stopped_; +}; + +TEST(CoordinatorTest, TestRealStop) { + std::atomic counter(0); + Coordinator coord; + + std::unique_ptr qr1 = + std::make_unique(&coord); + qr1->StartCounting(&counter, 100); + TF_ASSERT_OK(coord.RegisterRunner(std::move(qr1))); + + std::unique_ptr qr2 = + std::make_unique(&coord); + qr2->StartCounting(&counter, 100); + TF_ASSERT_OK(coord.RegisterRunner(std::move(qr2))); + + // Wait until the counting has started + while (counter.load() == 0) + ; + TF_EXPECT_OK(coord.RequestStop()); + + int temp_counter = counter.load(); + Env::Default()->SleepForMicroseconds(1000 * 1000); + EXPECT_EQ(temp_counter, counter.load()); + TF_EXPECT_OK(coord.Join()); +} + +TEST(CoordinatorTest, TestRequestStop) { + Coordinator coord; + std::atomic counter(0); + absl::Notification start; + std::unique_ptr qr; + for (int i = 0; i < 10; i++) { + qr = std::make_unique(&coord); + qr->StartCounting(&counter, 10, &start); + TF_ASSERT_OK(coord.RegisterRunner(std::move(qr))); + } + start.Notify(); + + coord.WaitForStop(); + EXPECT_EQ(coord.ShouldStop(), true); + EXPECT_EQ(counter.load(), 10); + TF_EXPECT_OK(coord.Join()); +} + +TEST(CoordinatorTest, TestJoin) { + Coordinator coord; + int join_counter = 0; + std::unique_ptr qr1 = + std::make_unique(&coord, &join_counter); + TF_ASSERT_OK(coord.RegisterRunner(std::move(qr1))); + std::unique_ptr qr2 = + std::make_unique(&coord, &join_counter); + TF_ASSERT_OK(coord.RegisterRunner(std::move(qr2))); + + TF_EXPECT_OK(coord.RequestStop()); + TF_EXPECT_OK(coord.Join()); + EXPECT_EQ(join_counter, 2); +} + +TEST(CoordinatorTest, StatusReporting) { + Coordinator coord({Code::CANCELLED, Code::OUT_OF_RANGE}); + absl::Notification start; + BlockingCounter counter(3); + + std::unique_ptr qr1 = + std::make_unique(&coord); + qr1->StartSettingStatus(absl::Status(absl::StatusCode::kCancelled, ""), + &counter, &start); + TF_ASSERT_OK(coord.RegisterRunner(std::move(qr1))); + + std::unique_ptr qr2 = + std::make_unique(&coord); + qr2->StartSettingStatus(absl::Status(absl::StatusCode::kInvalidArgument, ""), + &counter, &start); + TF_ASSERT_OK(coord.RegisterRunner(std::move(qr2))); + + std::unique_ptr qr3 = + std::make_unique(&coord); + qr3->StartSettingStatus(absl::Status(absl::StatusCode::kOutOfRange, ""), + &counter, &start); + TF_ASSERT_OK(coord.RegisterRunner(std::move(qr3))); + + start.Notify(); + counter.Wait(); + TF_EXPECT_OK(coord.RequestStop()); + EXPECT_EQ(coord.Join().code(), absl::StatusCode::kInvalidArgument); +} + +TEST(CoordinatorTest, JoinWithoutStop) { + Coordinator coord; + std::unique_ptr qr = + std::make_unique(&coord); + TF_ASSERT_OK(coord.RegisterRunner(std::move(qr))); + + EXPECT_EQ(coord.Join().code(), Code::FAILED_PRECONDITION); +} + +TEST(CoordinatorTest, AllRunnersStopped) { + Coordinator coord; + MockQueueRunner* qr = new MockQueueRunner(&coord); + TF_ASSERT_OK(coord.RegisterRunner(std::unique_ptr(qr))); + + EXPECT_FALSE(coord.AllRunnersStopped()); + qr->Stop(); + EXPECT_TRUE(coord.AllRunnersStopped()); +} + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/cc/training/queue_runner.cc b/western_ai/usa/tensorflow/tensorflow/cc/training/queue_runner.cc new file mode 100644 index 0000000000000000000000000000000000000000..062c70b19b07289f73726b6a582890d607b22cb4 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/training/queue_runner.cc @@ -0,0 +1,263 @@ +/* Copyright 2016 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/cc/training/queue_runner.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "absl/log/log.h" +#include "absl/status/status.h" +#include "tensorflow/cc/training/coordinator.h" +#include "xla/tsl/protobuf/error_codes.pb.h" +#include "tensorflow/core/framework/cost_graph.pb.h" +#include "tensorflow/core/framework/ops_util.h" +#include "tensorflow/core/platform/blocking_counter.h" +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/platform/mutex.h" +#include "tensorflow/core/platform/status.h" +#include "tensorflow/core/platform/threadpool.h" +#include "tensorflow/core/platform/types.h" +#include "tensorflow/core/protobuf/config.pb.h" +#include "tensorflow/core/protobuf/queue_runner.pb.h" +#include "tensorflow/core/public/session.h" + +namespace tensorflow { + +absl::Status QueueRunner::New(const QueueRunnerDef& queue_runner_def, + std::unique_ptr* result) { + result->reset(new QueueRunner()); + return (*result)->Init(queue_runner_def); +} + +absl::Status QueueRunner::New(const QueueRunnerDef& queue_runner_def, + Coordinator* coord, + std::unique_ptr* result) { + result->reset(new QueueRunner()); + (*result)->coord_ = coord; + return (*result)->Init(queue_runner_def); +} + +void QueueRunner::AddErrorCallback( + const std::function& cb) { + mutex_lock l(cb_mu_); + callbacks_.push_back(cb); +} + +void QueueRunner::ClearErrorCallbacks() { + mutex_lock l(cb_mu_); + callbacks_.clear(); +} + +absl::Status QueueRunner::Init(const QueueRunnerDef& queue_runner_def) { + queue_name_ = queue_runner_def.queue_name(); + enqueue_op_names_.clear(); + enqueue_op_names_.insert(enqueue_op_names_.end(), + queue_runner_def.enqueue_op_name().begin(), + queue_runner_def.enqueue_op_name().end()); + size_t op_names_size = enqueue_op_names_.size(); + if (op_names_size > std::numeric_limits::max()) { + return absl::Status(absl::StatusCode::kInvalidArgument, + "Enqueue ops to run cannot exceed kint32max"); + } + runs_ = static_cast(op_names_size); + if (runs_ == 0) { + return absl::Status(absl::StatusCode::kInvalidArgument, + "Empty enqueue ops to run."); + } + close_op_name_ = queue_runner_def.close_op_name(); + cancel_op_name_ = queue_runner_def.cancel_op_name(); + if (queue_runner_def.queue_closed_exception_types_size() == 0) { + queue_closed_exception_types_.insert(error::OUT_OF_RANGE); + } else { + for (const auto& code : queue_runner_def.queue_closed_exception_types()) { + queue_closed_exception_types_.insert(static_cast(code)); + } + } + + int nthreads = runs_; + if (coord_) { + // One more thread to call Stop() + nthreads++; + } + thread_pool_ = std::make_unique( + Env::Default(), SanitizeThreadSuffix(queue_name_), nthreads); + + return absl::OkStatus(); +} + +QueueRunner::~QueueRunner() { + // Cannot run Stop() here because the session might already be closed or + // destroyed. + Join().IgnoreError(); +} + +absl::Status QueueRunner::Start(Session* sess) { return Start(sess, 0); } + +absl::Status QueueRunner::StartAndCollectCostGraph( + Session* sess, const RunOptions& run_options) { + SetRunArgumentsAndCostGraph(run_options); + return Start(sess, 0); +} + +absl::Status QueueRunner::Start(Session* sess, int wait_for) { + counter_ = std::make_unique(runs_); + for (const std::string& enqueue_op : enqueue_op_names_) { + thread_pool_->Schedule( + std::bind(&QueueRunner::Run, this, sess, enqueue_op)); + } + if (coord_) { + thread_pool_->Schedule(std::bind(&QueueRunner::Stop, this, sess)); + } + // Wait for up to 'wait_for' milliseconds. + if (wait_for > 0) { + if (!counter_->WaitFor(std::chrono::milliseconds(wait_for))) { + return absl::Status(absl::StatusCode::kDeadlineExceeded, + "Queues not fed before the timeout"); + } + // Check the status of the queue runner as well as the result of the enqueue + // operations. + mutex_lock l(mu_); + if (!enqueue_status_.ok()) { + return enqueue_status_; + } else { + return status_; + } + } + return absl::OkStatus(); +} + +absl::Status QueueRunner::StartAndCollectCostGraph( + Session* session, int wait_for_ms, const RunOptions& run_options) { + SetRunArgumentsAndCostGraph(run_options); + return Start(session, wait_for_ms); +} + +void QueueRunner::Stop(Session* sess) { + if (coord_ != nullptr) { + coord_->WaitForStop(); + } + if (!cancel_op_name_.empty()) { + UpdateStatus(RealRun(sess, cancel_op_name_, false)); + } + stopped_ = true; +} + +absl::Status QueueRunner::Join() { + thread_pool_.reset(); + mutex_lock l(mu_); + return status_; +} + +void QueueRunner::UpdateStatus(const absl::Status& status) { + { + mutex_lock l(mu_); + if (!status_.ok() || status.ok() || IsQueueClosed(status)) { + return; + } + status_ = status; + } + if (coord_) { + coord_->ReportStatus(status); + } + mutex_lock l(cb_mu_); + for (auto& cb : callbacks_) { + cb(status); + } +} + +void QueueRunner::Run(Session* sess, const std::string& enqueue_op) { + bool first_iteration = true; + absl::Status status; + while (status.ok()) { + if (coord_ && coord_->ShouldStop()) { + break; + } + status = RealRun(sess, enqueue_op, true); + if (first_iteration) { + if (!status.ok()) { + mutex_lock l(mu_); + enqueue_status_ = status; + } + counter_->DecrementCount(); + first_iteration = false; + } + } + bool last_run = false; + { + mutex_lock l(mu_); + runs_--; + last_run = (runs_ == 0); + } + + // Close the queue unless the coordinator is shutting down since the cancel op + // will be run anyway in this case. + if (IsQueueClosed(status) && (!coord_ || !coord_->ShouldStop())) { + if (last_run && !close_op_name_.empty()) { + UpdateStatus(RealRun(sess, close_op_name_, false)); + } + } else if (!status.ok()) { + LOG(ERROR) << "Queue runner thread got a failure status: " << status; + UpdateStatus(status); + if (coord_) { + coord_->RequestStop().IgnoreError(); + } + } +} + +absl::Status QueueRunner::GetStatus() { + mutex_lock l(mu_); + return status_; +} + +absl::Status QueueRunner::ExportCostGraph(CostGraphDef* cost_graph) const { + if (!cg_mu_) { + return absl::Status(absl::StatusCode::kFailedPrecondition, + "This QueueRunner doesn't collect a cost graph."); + } + mutex_lock l(*cg_mu_); + cost_graph->MergeFrom(*cost_graph_); + return absl::OkStatus(); +} + +void QueueRunner::SetRunArgumentsAndCostGraph(const RunOptions& run_options) { + cg_mu_ = std::make_unique(); + { + mutex_lock l(*cg_mu_); + cost_graph_ = std::make_unique(); + } + run_options_ = run_options; +} + +absl::Status QueueRunner::RealRun(Session* sess, const std::string& op, + bool update_costs) { + absl::Status s; + if (update_costs && cg_mu_) { + RunMetadata metadata; + s = sess->Run(run_options_, {}, {}, {op}, nullptr, &metadata); + mutex_lock l(*cg_mu_); + cost_graph_->Swap(metadata.mutable_cost_graph()); + } else { + s = sess->Run({}, {}, {op}, nullptr); + } + return s; +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/cc/training/queue_runner.h b/western_ai/usa/tensorflow/tensorflow/cc/training/queue_runner.h new file mode 100644 index 0000000000000000000000000000000000000000..9402ab7d6d2c5dac17d8a8ca555591d378f2ff61 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/training/queue_runner.h @@ -0,0 +1,145 @@ +/* Copyright 2016 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. +==============================================================================*/ + +#ifndef TENSORFLOW_CC_TRAINING_QUEUE_RUNNER_H_ +#define TENSORFLOW_CC_TRAINING_QUEUE_RUNNER_H_ + +#include +#include +#include +#include + +#include "absl/status/status.h" +#include "tensorflow/cc/training/coordinator.h" +#include "tensorflow/core/lib/core/status.h" +#include "tensorflow/core/lib/core/threadpool.h" +#include "tensorflow/core/platform/blocking_counter.h" +#include "tensorflow/core/platform/mutex.h" +#include "tensorflow/core/platform/types.h" +#include "tensorflow/core/protobuf/config.pb.h" +#include "tensorflow/core/protobuf/error_codes.pb.h" +#include "tensorflow/core/protobuf/queue_runner.pb.h" +#include "tensorflow/core/public/session.h" +#include "tsl/platform/thread_annotations.h" + +namespace tensorflow { + +/// QueueRunner class imitates the behavior of the python version of QueueRunner +/// which creates a thread for each enqueue op, runs close op on completion. +class QueueRunner : public RunnerInterface { + public: + /// Creates a new QueueRunner from proto. + // TODO(yuefengz): we may want to initialize from queues and ops in the + // future. + static absl::Status New(const QueueRunnerDef& queue_runner_def, + std::unique_ptr* result); + + /// Creates a new QueueRunner with a coordinator, see coordinator.h for usage. + static absl::Status New(const QueueRunnerDef& queue_runner_def, + Coordinator* coord, + std::unique_ptr* result); + + /// Adds a callback that the queue runner will call when it detects an error. + void AddErrorCallback(const std::function& cb); + + /// Delete the previously registered callbacks. + void ClearErrorCallbacks(); + + /// The destructor would join all the threads. + ~QueueRunner() override; + + /// Starts the queue runner with the given session. + absl::Status Start(Session* sess); + + /// Starts the queue runner with the given session and sets the run arguments + /// for sess->Run. It also collects and stores the cost model. + absl::Status StartAndCollectCostGraph( + Session* sess, const RunOptions& run_options = RunOptions()); + + /// Starts the queue runner with the given session, and wait for up to the + /// specified time (in milliseconds) for the queues to start to fill up. + absl::Status Start(Session* sess, int wait_for_ms); + absl::Status StartAndCollectCostGraph( + Session* session, int wait_for_ms, + const RunOptions& run_options = RunOptions()); + + /// Requests to stop and runs the cancel op. It would be called in a separate + /// thread when coordinator is set. If there is no coordinator it should be + /// called before calling Join. + void Stop(Session* sess); + + /// Joins all the threads. Returns okay if all threads run successfully; + /// otherwise returns the first captured failure status. + absl::Status Join() final; + + /// Returns the latest status. + absl::Status GetStatus(); + + // Returns the stored cost model. + absl::Status ExportCostGraph(CostGraphDef* cost_graph) const override; + + private: + QueueRunner() : coord_(nullptr), stopped_(false), cg_mu_(nullptr) {} + + // Initializes the instance with the QueueRunnerDef proto. + absl::Status Init(const QueueRunnerDef& queue_runner_def); + + // The Run function for each thread. + void Run(Session* sess, const std::string& enqueue_op); + + // Updates the internal status; it only keeps OK or the first unexpected error + // status. + void UpdateStatus(const absl::Status& status); + + bool IsQueueClosed(absl::Status status) const { + return queue_closed_exception_types_.count( + static_cast(status.code())) > 0; + } + + bool IsRunning() const override { return !stopped_; } + + void SetRunArgumentsAndCostGraph(const RunOptions& run_options); + + absl::Status RealRun(Session* sess, const std::string& op, bool update_costs); + + std::string queue_name_; + std::vector enqueue_op_names_; + std::string close_op_name_; + std::string cancel_op_name_; + // code::Code casted to int to avoid a hash function. + std::unordered_set queue_closed_exception_types_; + + std::unique_ptr thread_pool_; + mutex mu_; + int runs_ = 0; + absl::Status status_ TF_GUARDED_BY(mu_); + absl::Status enqueue_status_ TF_GUARDED_BY(mu_); + std::unique_ptr counter_; + + Coordinator* coord_; + + std::atomic stopped_; + + mutex cb_mu_; + std::vector> callbacks_; + + mutable std::unique_ptr cg_mu_; + std::unique_ptr cost_graph_ TF_GUARDED_BY(cg_mu_); + RunOptions run_options_; +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_CC_TRAINING_QUEUE_RUNNER_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/cc/training/queue_runner_test.cc b/western_ai/usa/tensorflow/tensorflow/cc/training/queue_runner_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..6223682161289babbe9cb6b8bb0df70669c9308b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/cc/training/queue_runner_test.cc @@ -0,0 +1,426 @@ +/* Copyright 2016 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/cc/training/queue_runner.h" + +#include +#include +#include +#include +#include + +#include "absl/status/status.h" +#include "absl/synchronization/notification.h" +#include "tensorflow/cc/framework/ops.h" +#include "tensorflow/cc/framework/scope.h" +#include "tensorflow/cc/ops/const_op.h" +#include "tensorflow/cc/ops/data_flow_ops.h" +#include "tensorflow/cc/ops/math_ops.h" +#include "tensorflow/cc/ops/random_ops.h" +#include "tensorflow/cc/ops/state_ops.h" +#include "tensorflow/cc/training/coordinator.h" +#include "xla/tsl/lib/core/status_test_util.h" +#include "xla/tsl/platform/status.h" +#include "xla/tsl/protobuf/error_codes.pb.h" +#include "tensorflow/core/framework/cost_graph.pb.h" +#include "tensorflow/core/framework/graph.pb.h" +#include "tensorflow/core/framework/tensor.h" +#include "tensorflow/core/framework/tensor_shape.h" +#include "tensorflow/core/framework/types.pb.h" +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/platform/status.h" +#include "tensorflow/core/platform/test.h" +#include "tensorflow/core/protobuf/config.pb.h" +#include "tensorflow/core/protobuf/error_codes.pb.h" +#include "tensorflow/core/protobuf/queue_runner.pb.h" +#include "tensorflow/core/public/session.h" +#include "tensorflow/core/public/session_options.h" + +namespace tensorflow { +namespace { + +using error::Code; +using ops::Assign; +using ops::Const; +using ops::CountUpTo; +using ops::FIFOQueue; +using ops::QueueClose; +using ops::QueueDequeue; +using ops::QueueEnqueue; +using ops::RandomNormal; +using ops::Square; +using ops::Variable; + +constexpr char kAssignOpName[] = "assign"; +constexpr char kCancelOp0[] = "cancel0"; +constexpr char kCancelOp1[] = "cancel1"; +constexpr char kCloseOp0[] = "close0"; +constexpr char kCloseOp1[] = "close1"; +constexpr char kCountUpToOpName[] = "count"; +constexpr char kDequeueOp0[] = "dequeue0"; +constexpr char kDequeueOp1[] = "dequeue1"; +constexpr char kEnqueueOp0[] = "enqueue0"; +constexpr char kEnqueueOp1[] = "enqueue1"; +constexpr char kIllegalOpName1[] = "would fail"; +constexpr char kIllegalOpName2[] = "fail again"; +constexpr char kQueueName[] = "unit_test"; +constexpr char kQueueName0[] = "q0"; +constexpr char kQueueName1[] = "q1"; +constexpr char kSquareOpName[] = "square"; +constexpr char kVarOpName[] = "var"; + +GraphDef BuildSimpleGraph() { + Scope root = Scope::NewRootScope(); + auto init_value = Const(root, 0); + auto var = Variable(root.WithOpName(kVarOpName), TensorShape({}), + DataType::DT_INT32); + auto assign = Assign(root.WithOpName(kAssignOpName), var, init_value); + auto count = CountUpTo(root.WithOpName(kCountUpToOpName), var, 10); + Square(root.WithOpName(kSquareOpName), var); // NOLINT + + GraphDef graph_def; + TF_EXPECT_OK(root.ToGraphDef(&graph_def)); + return graph_def; +} + +QueueRunnerDef BuildQueueRunnerDef( + const std::string& queue_name, const std::vector& enqueue_ops, + const std::string& close_op, const std::string& cancel_op, + const std::vector& queue_closed_error_codes) { + QueueRunnerDef queue_runner_def; + *queue_runner_def.mutable_queue_name() = queue_name; + for (const std::string& enqueue_op : enqueue_ops) { + *queue_runner_def.mutable_enqueue_op_name()->Add() = enqueue_op; + } + *queue_runner_def.mutable_close_op_name() = close_op; + *queue_runner_def.mutable_cancel_op_name() = cancel_op; + for (const auto& error_code : queue_closed_error_codes) { + *queue_runner_def.mutable_queue_closed_exception_types()->Add() = + error_code; + } + return queue_runner_def; +} + +std::unique_ptr BuildSessionAndInitVariable( + const GraphDef& graph_def) { + SessionOptions options; + std::unique_ptr session(NewSession(options)); + TF_CHECK_OK(session->Create(graph_def)); + + TF_CHECK_OK(session->Run({}, {}, {kAssignOpName}, nullptr)); + return session; +} + +TEST(QueueRunnerTest, BasicTest) { + GraphDef graph_def = BuildSimpleGraph(); + auto session = BuildSessionAndInitVariable(graph_def); + + QueueRunnerDef queue_runner_def = BuildQueueRunnerDef( + kQueueName, {kCountUpToOpName}, kSquareOpName, "", {}); + + std::unique_ptr qr; + TF_EXPECT_OK(QueueRunner::New(queue_runner_def, &qr)); + TF_CHECK_OK(qr->Start(session.get())); + TF_EXPECT_OK(qr->Join()); + + std::vector outputs; + TF_EXPECT_OK(session->Run({}, {kSquareOpName}, {}, &outputs)); + int square_value = *outputs[0].scalar().data(); + EXPECT_EQ(square_value, 100); +} + +TEST(QueueRunnerTest, QueueClosedCode) { + GraphDef graph_def = BuildSimpleGraph(); + auto session = BuildSessionAndInitVariable(graph_def); + + // Start two queues so that multiple threads are in Run. + QueueRunnerDef queue_runner_def = BuildQueueRunnerDef( + kQueueName, {kCountUpToOpName, kCountUpToOpName}, kSquareOpName, "", + {Code::OUT_OF_RANGE, Code::CANCELLED}); + + std::unique_ptr qr; + TF_EXPECT_OK(QueueRunner::New(queue_runner_def, &qr)); + TF_EXPECT_OK(qr->Start(session.get())); + TF_EXPECT_OK(qr->Join()); + + std::vector outputs; + TF_EXPECT_OK(session->Run({}, {kSquareOpName}, {}, &outputs)); + int square_value = *outputs[0].scalar().data(); + EXPECT_EQ(square_value, 100); +} + +TEST(QueueRunnerTest, QueueCloseFails) { + GraphDef graph_def = BuildSimpleGraph(); + auto session = BuildSessionAndInitVariable(graph_def); + + QueueRunnerDef queue_runner_def = + BuildQueueRunnerDef(kQueueName, {kCountUpToOpName}, kIllegalOpName1, "", + {Code::OUT_OF_RANGE}); + + std::unique_ptr qr; + TF_EXPECT_OK(QueueRunner::New(queue_runner_def, &qr)); + TF_EXPECT_OK(qr->Start(session.get())); + auto status = qr->Join(); + EXPECT_EQ(status.code(), Code::NOT_FOUND) << status; +} + +TEST(QueueRunnerTest, CatchErrorInJoin) { + GraphDef graph_def = BuildSimpleGraph(); + auto session = BuildSessionAndInitVariable(graph_def); + + QueueRunnerDef queue_runner_def = BuildQueueRunnerDef( + kQueueName, {kIllegalOpName1, kIllegalOpName2}, kCountUpToOpName, "", {}); + + std::unique_ptr qr; + TF_EXPECT_OK(QueueRunner::New(queue_runner_def, &qr)); + TF_EXPECT_OK(qr->Start(session.get())); + EXPECT_EQ(qr->Join().code(), Code::NOT_FOUND); +} + +GraphDef BuildDoubleQueueGraph() { + Scope root = Scope::NewRootScope(); + auto q0 = FIFOQueue(root.WithOpName(kQueueName0), {DataType::DT_INT32}); + auto ten = Const(root, 10); + auto enqueue0 = QueueEnqueue(root.WithOpName(kEnqueueOp0), q0, {ten}); + auto close0 = QueueClose(root.WithOpName(kCloseOp0), q0); + auto cancel0 = QueueClose(root.WithOpName(kCancelOp0), q0, + QueueClose::CancelPendingEnqueues(true)); + auto q1 = FIFOQueue(root.WithOpName(kQueueName1), {DataType::DT_INT32}, + FIFOQueue::Capacity(3)); + auto dequeue0 = + QueueDequeue(root.WithOpName(kDequeueOp0), q0, {DataType::DT_INT32}); + auto enqueue1 = QueueEnqueue(root.WithOpName(kEnqueueOp1), q1, {dequeue0[0]}); + auto dequeue1 = + QueueDequeue(root.WithOpName(kDequeueOp1), q1, {DataType::DT_INT32}); + auto close1 = QueueClose(root.WithOpName(kCloseOp1), q1); + auto cancel1 = QueueClose(root.WithOpName(kCancelOp1), q1, + QueueClose::CancelPendingEnqueues(true)); + + GraphDef graph_def; + TF_EXPECT_OK(root.ToGraphDef(&graph_def)); + return graph_def; +} + +TEST(QueueRunnerTest, RealEnqueueDequeue) { + auto graph_def = BuildDoubleQueueGraph(); + + SessionOptions options; + std::unique_ptr session(NewSession(options)); + TF_CHECK_OK(session->Create(graph_def)); + + QueueRunnerDef queue_runner_def = + BuildQueueRunnerDef(kQueueName, {kEnqueueOp1}, kCloseOp1, "", {}); + std::unique_ptr qr; + TF_EXPECT_OK(QueueRunner::New(queue_runner_def, &qr)); + TF_CHECK_OK(qr->Start(session.get())); + + TF_EXPECT_OK(session->Run({}, {}, {kEnqueueOp0}, nullptr)); + TF_EXPECT_OK(session->Run({}, {}, {kEnqueueOp0}, nullptr)); + // Closing queue 0 would also close the queue runner. + TF_EXPECT_OK(session->Run({}, {}, {kCloseOp0}, nullptr)); + + TF_EXPECT_OK(qr->Join()); + std::vector dq1; + TF_EXPECT_OK(session->Run({}, {kDequeueOp1}, {}, &dq1)); + EXPECT_EQ(*dq1[0].scalar().data(), 10); + std::vector dq2; + TF_EXPECT_OK(session->Run({}, {kDequeueOp1}, {}, &dq2)); + EXPECT_EQ(*dq2[0].scalar().data(), 10); + + EXPECT_EQ(session->Run({}, {kDequeueOp1}, {}, nullptr).code(), + Code::OUT_OF_RANGE); +} + +void JoinThread(QueueRunner* queue_runner, bool* join_succeeded, + absl::Notification* join_done) { + EXPECT_EQ(queue_runner->Join().code(), Code::CANCELLED); + *join_succeeded = true; + join_done->Notify(); +} + +TEST(QueueRunnerTest, SessionCloseCancelPendingEnqueue) { + auto graph_def = BuildDoubleQueueGraph(); + + SessionOptions options; + std::unique_ptr session(NewSession(options)); + TF_CHECK_OK(session->Create(graph_def)); + + QueueRunnerDef queue_runner_def = BuildQueueRunnerDef( + kQueueName1, {kEnqueueOp1}, kCloseOp1, kCancelOp1, {}); + std::unique_ptr qr; + TF_EXPECT_OK(QueueRunner::New(queue_runner_def, &qr)); + TF_CHECK_OK(qr->Start(session.get())); + + TF_EXPECT_OK(session->Run({}, {}, {kEnqueueOp0}, nullptr)); + + std::vector dq1; + TF_EXPECT_OK(session->Run({}, {kDequeueOp1}, {}, &dq1)); + EXPECT_EQ(*dq1[0].scalar().data(), 10); + + // The expected behavior is the QueueRunner::Join() call is blocked until + // Session::Close() is called. + bool join_succeeded = false; + absl::Notification join_done; + Env::Default()->SchedClosure( + std::bind(&JoinThread, qr.get(), &join_succeeded, &join_done)); + + Env::Default()->SleepForMicroseconds(10000000); + EXPECT_EQ(join_succeeded, false); + + // Closing the session is required to cancel pending enqueue nodes. + TF_EXPECT_OK(session->Close()); + + join_done.WaitForNotification(); + EXPECT_EQ(join_succeeded, true); +} + +TEST(QueueRunnerTest, EmptyEnqueueOps) { + QueueRunnerDef queue_runner_def = + BuildQueueRunnerDef(kQueueName, {}, kCountUpToOpName, "", {}); + + std::unique_ptr qr; + EXPECT_EQ(QueueRunner::New(queue_runner_def, &qr).code(), + Code::INVALID_ARGUMENT); +} + +TEST(QueueRunnerTest, StartTimeout) { + GraphDef graph_def = BuildDoubleQueueGraph(); + SessionOptions options; + std::unique_ptr session(NewSession(options)); + TF_CHECK_OK(session->Create(graph_def)); + + QueueRunnerDef queue_runner_def = BuildQueueRunnerDef( + kQueueName1, {kEnqueueOp1}, kCloseOp1, kCancelOp1, {}); + + std::unique_ptr qr; + TF_EXPECT_OK(QueueRunner::New(queue_runner_def, &qr)); + // This will timeout since queue0 is not fed and queue1 is fetching data from + // queue0. + EXPECT_EQ(qr->Start(session.get(), 1).code(), Code::DEADLINE_EXCEEDED); + TF_EXPECT_OK(session->Close()); +} + +TEST(QueueRunnerTest, TestCoordinatorStop) { + auto graph_def = BuildDoubleQueueGraph(); + SessionOptions options; + std::unique_ptr session(NewSession(options)); + TF_CHECK_OK(session->Create(graph_def)); + + QueueRunnerDef queue_runner0 = + BuildQueueRunnerDef(kQueueName0, {kEnqueueOp0}, kCloseOp0, kCancelOp0, + {Code::OUT_OF_RANGE, Code::CANCELLED}); + QueueRunnerDef queue_runner1 = + BuildQueueRunnerDef(kQueueName1, {kEnqueueOp1}, kCloseOp1, kCancelOp1, + {Code::OUT_OF_RANGE, Code::CANCELLED}); + + Coordinator coord; + std::unique_ptr qr0; + TF_EXPECT_OK(QueueRunner::New(queue_runner0, &coord, &qr0)); + TF_CHECK_OK(qr0->Start(session.get())); + std::unique_ptr qr1; + TF_EXPECT_OK(QueueRunner::New(queue_runner1, &coord, &qr1)); + TF_CHECK_OK(qr1->Start(session.get())); + + TF_EXPECT_OK(coord.RegisterRunner(std::move(qr0))); + TF_EXPECT_OK(coord.RegisterRunner(std::move(qr1))); + + std::vector dq; + TF_EXPECT_OK(session->Run({}, {kDequeueOp1}, {}, &dq)); + EXPECT_EQ(*dq[0].scalar().data(), 10); + + TF_EXPECT_OK(coord.RequestStop()); + TF_EXPECT_OK(coord.Join()); +} + +TEST(QueueRunnerTest, CallbackCalledOnError) { + GraphDef graph_def = BuildSimpleGraph(); + auto session = BuildSessionAndInitVariable(graph_def); + + QueueRunnerDef queue_runner_def = BuildQueueRunnerDef( + kQueueName, {kIllegalOpName1, kIllegalOpName2}, kCountUpToOpName, "", {}); + + std::unique_ptr qr; + TF_EXPECT_OK(QueueRunner::New(queue_runner_def, &qr)); + bool error_caught = false; + qr->AddErrorCallback( + [&error_caught](const absl::Status&) { error_caught = true; }); + TF_EXPECT_OK(qr->Start(session.get())); + EXPECT_FALSE(qr->Join().ok()); + EXPECT_TRUE(error_caught); +} + +TEST(QueueRunnerTest, RunMetaDataTest) { + Scope root = Scope::NewRootScope(); + auto q0 = FIFOQueue(root.WithOpName(kQueueName), {DataType::DT_FLOAT}); + Output rnd = RandomNormal(root.WithOpName("rnd"), {1, 1}, DataType::DT_FLOAT); + Output square = Square(root.WithOpName(kSquareOpName), rnd); + auto enqueue0 = QueueEnqueue(root.WithOpName(kEnqueueOp0), q0, {square}); + auto close0 = QueueClose(root.WithOpName(kCloseOp0), q0); + auto cancel0 = QueueClose(root.WithOpName(kCancelOp0), q0, + QueueClose::CancelPendingEnqueues(true)); + auto dequeue0 = + QueueDequeue(root.WithOpName(kDequeueOp0), q0, {DataType::DT_FLOAT}); + + GraphDef graph_def; + TF_EXPECT_OK(root.ToGraphDef(&graph_def)); + for (auto& node : *graph_def.mutable_node()) { + node.set_device("/cpu:0"); + } + SessionOptions sess_options; + sess_options.config.mutable_graph_options()->set_build_cost_model(1); + std::unique_ptr session(NewSession(sess_options)); + + TF_CHECK_OK(session->Create(graph_def)); + + QueueRunnerDef queue_runner_def = + BuildQueueRunnerDef(kQueueName, {kEnqueueOp0}, kCloseOp0, kCancelOp0, {}); + std::unique_ptr qr; + TF_EXPECT_OK(QueueRunner::New(queue_runner_def, &qr)); + RunOptions run_options; + TF_CHECK_OK(qr->StartAndCollectCostGraph(session.get(), run_options)); + + // Make sure there was at least one element enqueued in q0: this prevents a + // race condition where we close the queue before it was populated. + std::vector dq0; + TF_EXPECT_OK(session->Run({}, {kDequeueOp0}, {}, &dq0)); + // Second call to run dequeue op is to make sure the cost graph has been + // stored. + TF_EXPECT_OK(session->Run({}, {kDequeueOp0}, {}, &dq0)); + + CostGraphDef cost_graph; + TF_CHECK_OK(qr->ExportCostGraph(&cost_graph)); + EXPECT_TRUE(cost_graph.node_size() > 0); + + qr->Stop(session.get()); +} + +TEST(QueueRunnerTest, NoRunMetaDataTest) { + GraphDef graph_def = BuildSimpleGraph(); + auto session = BuildSessionAndInitVariable(graph_def); + + QueueRunnerDef queue_runner_def = BuildQueueRunnerDef( + kQueueName, {kCountUpToOpName}, kSquareOpName, "", {}); + std::unique_ptr qr; + TF_EXPECT_OK(QueueRunner::New(queue_runner_def, &qr)); + TF_CHECK_OK(qr->Start(session.get())); + + TF_EXPECT_OK(qr->Join()); + CostGraphDef cost_graph; + EXPECT_EQ(qr->ExportCostGraph(&cost_graph).code(), + error::FAILED_PRECONDITION); +} + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compat_template.__init__.py b/western_ai/usa/tensorflow/tensorflow/compat_template.__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..425ba61a14b0f7bbd82c0c862e255c023f24c3a0 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compat_template.__init__.py @@ -0,0 +1,64 @@ +# Copyright 2015 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. +# ============================================================================== +"""Bring in all of the public TensorFlow interface into this module.""" + +# pylint: disable=g-bad-import-order,g-import-not-at-top,protected-access + +import os as _os +import sys as _sys + +from tensorflow.python.tools import module_util as _module_util +from tensorflow.python.util.lazy_loader import KerasLazyLoader as _KerasLazyLoader + +# API IMPORTS PLACEHOLDER + +# WRAPPER_PLACEHOLDER + +# Hook external TensorFlow modules. +_current_module = _sys.modules[__name__] + +# Lazy load Keras v2 +_tf_uses_legacy_keras = ( + _os.environ.get("TF_USE_LEGACY_KERAS", None) in ("true", "True", "1")) +setattr(_current_module, "keras", _KerasLazyLoader(globals(), mode="v2")) +if _tf_uses_legacy_keras: + _module_dir = _module_util.get_parent_dir_for_name("tf_keras.api._v2.keras") +else: + _module_dir = _module_util.get_parent_dir_for_name("keras.api._v2.keras") +_current_module.__path__ = [_module_dir] + _current_module.__path__ + + +# We would like the following to work for fully enabling 2.0 in a 1.0 install: +# +# import tensorflow.compat.v2 as tf +# tf.enable_v2_behavior() +# +# This make this one symbol available directly. +from tensorflow.python.compat.v2_compat import enable_v2_behavior # pylint: disable=g-import-not-at-top +setattr(_current_module, "enable_v2_behavior", enable_v2_behavior) + +# Add module aliases +_losses = _KerasLazyLoader( + globals(), submodule="losses", name="losses", mode="v2") +_metrics = _KerasLazyLoader( + globals(), submodule="metrics", name="metrics", mode="v2") +_optimizers = _KerasLazyLoader( + globals(), submodule="optimizers", name="optimizers", mode="v2") +_initializers = _KerasLazyLoader( + globals(), submodule="initializers", name="initializers", mode="v2") +setattr(_current_module, "losses", _losses) +setattr(_current_module, "metrics", _metrics) +setattr(_current_module, "optimizers", _optimizers) +setattr(_current_module, "initializers", _initializers) diff --git a/western_ai/usa/tensorflow/tensorflow/compat_template_v1.__init__.py b/western_ai/usa/tensorflow/tensorflow/compat_template_v1.__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..1c11c2a091e4f1b2f3e81a3278d0769e8bdb9df3 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compat_template_v1.__init__.py @@ -0,0 +1,73 @@ +# Copyright 2015 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. +# ============================================================================== +"""Bring in all of the public TensorFlow interface into this module.""" + +# pylint: disable=g-bad-import-order,g-import-not-at-top,protected-access + +import os as _os +import sys as _sys + +from tensorflow.python.tools import module_util as _module_util +from tensorflow.python.util.lazy_loader import KerasLazyLoader as _KerasLazyLoader + +# API IMPORTS PLACEHOLDER + +# WRAPPER_PLACEHOLDER + +# Hook external TensorFlow modules. +_current_module = _sys.modules[__name__] + +# Lazy load Keras v1 +_tf_uses_legacy_keras = ( + _os.environ.get("TF_USE_LEGACY_KERAS", None) in ("true", "True", "1")) +setattr(_current_module, "keras", _KerasLazyLoader(globals(), mode="v1")) +if _tf_uses_legacy_keras: + _module_dir = _module_util.get_parent_dir_for_name("tf_keras.api._v1.keras") +else: + _module_dir = _module_util.get_parent_dir_for_name("keras.api._v1.keras") +_current_module.__path__ = [_module_dir] + _current_module.__path__ + + +from tensorflow.python.platform import flags # pylint: disable=g-import-not-at-top +_current_module.app.flags = flags # pylint: disable=undefined-variable +setattr(_current_module, "flags", flags) + +# Add module aliases from Keras to TF. +# Some tf endpoints actually lives under Keras. +_current_module.layers = _KerasLazyLoader( + globals(), + submodule="__internal__.legacy.layers", + name="layers", + mode="v1") +if _tf_uses_legacy_keras: + _module_dir = _module_util.get_parent_dir_for_name( + "tf_keras.api._v1.keras.__internal__.legacy.layers") +else: + _module_dir = _module_util.get_parent_dir_for_name( + "keras.api._v1.keras.__internal__.legacy.layers") +_current_module.__path__ = [_module_dir] + _current_module.__path__ + +_current_module.nn.rnn_cell = _KerasLazyLoader( + globals(), + submodule="__internal__.legacy.rnn_cell", + name="rnn_cell", + mode="v1") +if _tf_uses_legacy_keras: + _module_dir = _module_util.get_parent_dir_for_name( + "tf_keras.api._v1.keras.__internal__.legacy.rnn_cell") +else: + _module_dir = _module_util.get_parent_dir_for_name( + "keras.api._v1.keras.__internal__.legacy.rnn_cell") +_current_module.nn.__path__ = [_module_dir] + _current_module.nn.__path__ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/aot/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..7c2233a371e64aafd90a2edd19cf01a6b3cfb693 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/BUILD @@ -0,0 +1,423 @@ +load("//tensorflow:tensorflow.bzl", "if_google", "if_oss", "tf_cc_binary", "tf_cc_test") +load("//tensorflow/compiler/aot:tfcompile.bzl", "tf_library") +load( + "//tensorflow/core/platform:build_config_root.bzl", + "if_llvm_aarch32_available", + "if_llvm_aarch64_available", + "if_llvm_hexagon_available", + "if_llvm_powerpc_available", + "if_llvm_system_z_available", + "if_llvm_x86_available", +) +load("//tensorflow/core/platform:rules_cc.bzl", "cc_library") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:license"], + default_visibility = ["//visibility:private"], + licenses = ["notice"], +) + +# Don't depend on this directly; this is only used for the benchmark test +# generated by tf_library. +cc_library( + name = "tf_library_test_main", + testonly = 1, + visibility = ["//visibility:public"], + deps = if_oss([ + "//tensorflow/core:test_main", + ]) + if_google([ + "//tensorflow/core/platform/default/build_config:test_main", + ]), +) + +filegroup( + name = "quantize_header", + srcs = ["quantize.h"], + visibility = ["//visibility:public"], +) + +cc_library( + name = "thunk_proto_execution_deserializer", + srcs = ["thunk_proto_execution_deserializer.cc"], + hdrs = ["thunk_proto_execution_deserializer.h"], + visibility = ["//visibility:public"], + deps = [ + "@com_google_absl//absl/numeric:int128", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@xla//xla:debug_options_flags", + "@xla//xla:shape_util", + "@xla//xla:status_macros", + "@xla//xla:util", + "@xla//xla:xla_data_proto_cc", + "@xla//xla/backends/cpu/runtime:convolution_dims", + "@xla//xla/backends/cpu/runtime:dot_dims", + "@xla//xla/backends/cpu/runtime:kernel_thunk", + "@xla//xla/backends/cpu/runtime:thunk_proto_cc", + "@xla//xla/service/cpu:executable_proto_cc", + "@xla//xla/tsl/platform:statusor", + ], +) + +cc_library( + name = "tfcompile_lib", + srcs = [ + "codegen.cc", + "compile.cc", + "flags.cc", + ], + hdrs = [ + "codegen.h", + "compile.h", + "flags.h", + "quantize.h", + ], + compatible_with = [], + defines = if_llvm_aarch32_available(["TF_LLVM_AARCH32_AVAILABLE=1"]) + if_llvm_aarch64_available([ + "TF_LLVM_AARCH64_AVAILABLE=1", + ]) + if_llvm_hexagon_available([ + "TF_LLVM_HEXAGON_AVAILABLE=1", + ]) + if_llvm_powerpc_available([ + "TF_LLVM_POWERPC_AVAILABLE=1", + ]) + if_llvm_system_z_available([ + "TF_LLVM_S390X_AVAILABLE=1", + ]) + if_llvm_x86_available([ + "TF_LLVM_X86_AVAILABLE=1", + ]), + visibility = [ + "//tensorflow:__pkg__", + "//tensorflow/python:__pkg__", + ], + deps = [ + ":aot_only_var_handle_op", + ":embedded_protocol_buffers", + ":llvm_targets", # fixdeps: keep + ":thunk_proto_execution_deserializer", + "//tensorflow/compiler/tf2xla", + "//tensorflow/compiler/tf2xla:allocator", + "//tensorflow/compiler/tf2xla:encoded_buffer_allocation_info", + "//tensorflow/compiler/tf2xla:mlir_tf2xla", # fixdeps: keep + "//tensorflow/compiler/tf2xla:tf2xla_proto_cc", + "//tensorflow/compiler/tf2xla:tf2xla_util", + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/compiler/tf2xla/kernels:xla_dummy_ops", + "//tensorflow/compiler/tf2xla/kernels:xla_ops", + "//tensorflow/core:core_cpu_internal", + "//tensorflow/core:framework_internal", + "//tensorflow/core:lib", + "//tensorflow/core:lib_internal", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core/platform:regexp", + "//tensorflow/core/platform:status", + "@com_google_absl//absl/algorithm:container", + "@com_google_absl//absl/base", + "@com_google_absl//absl/log:check", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/strings:str_format", + "@com_google_absl//absl/types:span", + "@llvm-project//llvm:Support", + "@llvm-project//llvm:Target", + "@xla//xla:debug_options_flags", + "@xla//xla:shape_util", + "@xla//xla:status_macros", + "@xla//xla:util", + "@xla//xla:xla_data_proto_cc", + "@xla//xla/backends/cpu:buffer_allocation_info", + "@xla//xla/backends/cpu:buffer_allocation_info_util", + "@xla//xla/backends/cpu/codegen:symbol_name_util", + "@xla//xla/backends/cpu/runtime:thunk_proto_cc", + "@xla//xla/backends/cpu/runtime:thunk_proto_serdes", + "@xla//xla/client:client_library", + "@xla//xla/client:compile_only_client", + "@xla//xla/hlo/builder:xla_computation", + "@xla//xla/service:compiler", + "@xla//xla/service/cpu:cpu_aot_compilation_result", + "@xla//xla/service/cpu:cpu_compiler", + "@xla//xla/service/cpu:cpu_executable", + "@xla//xla/stream_executor:platform", + "@xla//xla/stream_executor:platform_manager", + "@xla//xla/util:embedded_constant_buffers", + ], +) + +tf_cc_test( + name = "codegen_test", + srcs = ["codegen_test.cc"], + defines = if_llvm_x86_available(["TF_LLVM_X86_AVAILABLE=1"]), + deps = [ + ":tfcompile_lib", + "//tensorflow/core:lib", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core:test", + "//tensorflow/core:test_main", + "//tensorflow/core/platform:resource_loader", + "@com_google_absl//absl/memory", + "@com_google_absl//absl/strings", + "@llvm-project//llvm:Support", # fixdeps: keep + "@xla//xla:shape_util", + "@xla//xla/service/cpu:cpu_aot_compilation_result", + ] + if_llvm_x86_available([ + "@llvm-project//llvm:X86CodeGen", # fixdeps: keep + ]), +) + +tf_cc_binary( + name = "tfcompile", + visibility = ["//visibility:public"], + deps = [":tfcompile_main"], +) + +cc_library( + name = "llvm_targets", + visibility = ["//tensorflow/python:__pkg__"], + deps = [ + ] + if_llvm_aarch32_available([ + "@llvm-project//llvm:ARMAsmParser", # fixdeps: keep + "@llvm-project//llvm:ARMCodeGen", # fixdeps: keep + ]) + if_llvm_aarch64_available([ + "@llvm-project//llvm:AArch64AsmParser", # fixdeps: keep + "@llvm-project//llvm:AArch64CodeGen", # fixdeps: keep + ]) + if_llvm_hexagon_available([ + "@llvm-project//llvm:HexagonAsmParser", # fixdeps: keep + "@llvm-project//llvm:HexagonCodeGen", # fixdeps: keep + ]) + if_llvm_powerpc_available([ + "@llvm-project//llvm:PowerPCAsmParser", # fixdeps: keep + "@llvm-project//llvm:PowerPCCodeGen", # fixdeps: keep + ]) + if_llvm_system_z_available([ + "@llvm-project//llvm:SystemZAsmParser", # fixdeps: keep + "@llvm-project//llvm:SystemZCodeGen", # fixdeps: keep + ]) + if_llvm_x86_available([ + "@llvm-project//llvm:X86AsmParser", # fixdeps: keep + "@llvm-project//llvm:X86CodeGen", # fixdeps: keep + ]), +) + +cc_library( + name = "tfcompile_main", + srcs = ["tfcompile_main.cc"], + compatible_with = [], + visibility = ["//visibility:public"], + deps = [ + ":tfcompile_lib", + "//tensorflow/compiler/tf2xla:tf2xla_proto_cc", + "//tensorflow/core:core_cpu_internal", + "//tensorflow/core:framework_internal", + "//tensorflow/core:lib", + "//tensorflow/core:protos_all_cc", + "@com_google_absl//absl/log:check", + "@com_google_absl//absl/status", + "@com_google_absl//absl/strings", + "@xla//xla:debug_options_flags", + "@xla//xla/tsl/platform:status", + ], +) + +# NOTE: Most end-to-end tests are in the "tests" subdirectory, to ensure that +# tfcompile.bzl correctly handles usage from outside of the package that it is +# defined in. + +# A simple test of tf_library from a text protobuf, to enable benchmark_test. +# This test uses an incompleted graph with a node that is not defined. The +# compilation works because the undefined node is a feed node. +tf_library( + name = "test_graph_tfadd", + testonly = 1, + config = "test_graph_tfadd.config.pbtxt", + cpp_class = "AddComp", + graph = "test_graph_tfadd.pbtxt", + mlir_components = "None", + tags = [ + "manual", + ], +) + +tf_library( + name = "test_graph_tfadd_mlir_bridge", + testonly = 1, + config = "test_graph_tfadd.config.pbtxt", + cpp_class = "AddComp", + graph = "test_graph_tfadd.pbtxt", + mlir_components = "Bridge", + tags = [ + "manual", + ], +) + +# A test of tf_library that includes a graph with an unknown op, but where +# the compilation works because the node with the unknown op is not needed +# for the fetches. +tf_library( + name = "test_graph_tfunknownop", + testonly = 1, + config = "test_graph_tfunknownop.config.pbtxt", + cpp_class = "UnknownOpAddComp", + graph = "test_graph_tfunknownop.pbtxt", + mlir_components = "None", + tags = [ + "manual", + ], +) + +tf_library( + name = "test_graph_tfunknownop_mlir_bridge", + testonly = 1, + config = "test_graph_tfunknownop.config.pbtxt", + cpp_class = "UnknownOpAddComp", + graph = "test_graph_tfunknownop.pbtxt", + mlir_components = "Bridge", + tags = [ + "manual", + ], +) + +# A test of tf_library that includes a graph with an unknown op, but where +# the compilation works because the node with the unknown op is only used as +# an input of a feed node. +tf_library( + name = "test_graph_tfunknownop2", + testonly = 1, + config = "test_graph_tfunknownop2.config.pbtxt", + cpp_class = "UnknownOpAddComp", + graph = "test_graph_tfunknownop.pbtxt", + mlir_components = "None", + tags = [ + "manual", + ], +) + +tf_library( + name = "test_graph_tfunknownop2_mlir_bridge", + testonly = 1, + config = "test_graph_tfunknownop2.config.pbtxt", + cpp_class = "UnknownOpAddComp", + graph = "test_graph_tfunknownop.pbtxt", + mlir_components = "Bridge", + tags = [ + "manual", + ], +) + +# A test of tf_library that includes a graph with an unknown op, but where +# the compilation works because the node with the unknown op is a feed node. +tf_library( + name = "test_graph_tfunknownop3", + testonly = 1, + config = "test_graph_tfunknownop3.config.pbtxt", + cpp_class = "UnknownOpAddComp", + graph = "test_graph_tfunknownop.pbtxt", + mlir_components = "None", + tags = [ + "manual", + ], +) + +tf_library( + name = "test_graph_tfunknownop3_mlir_bridge", + testonly = 1, + config = "test_graph_tfunknownop3.config.pbtxt", + cpp_class = "UnknownOpAddComp", + graph = "test_graph_tfunknownop.pbtxt", + mlir_components = "Bridge", + tags = [ + "manual", + ], +) + +# Utility library for benchmark binaries, used by the *_benchmark rules that are +# added by the tfcompile bazel macro. +cc_library( + name = "benchmark", + srcs = ["benchmark.cc"], + hdrs = ["benchmark.h"], + visibility = ["//visibility:public"], + deps = [ + # The purpose of the benchmark library is to support building an aot + # binary with minimal dependencies, to demonstrate small binary sizes. + # + # KEEP THE DEPENDENCIES MINIMAL. + "//tensorflow/core:framework_lite", + ], +) + +cc_library( + name = "benchmark_extra_android", + tags = [ + "manual", + ], + visibility = ["//visibility:public"], +) + +cc_library( + name = "embedded_protocol_buffers", + srcs = ["embedded_protocol_buffers.cc"], + hdrs = ["embedded_protocol_buffers.h"], + visibility = ["//visibility:public"], + deps = [ + "//tensorflow/core:lib", + "@com_google_absl//absl/log:check", + "@com_google_absl//absl/memory", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/types:span", + "@llvm-project//llvm:Core", + "@llvm-project//llvm:MC", + "@llvm-project//llvm:Target", + "@llvm-project//llvm:TargetParser", + "@xla//xla:util", + "@xla//xla/service/llvm_ir:llvm_type_conversion_util", + ], +) + +cc_library( + name = "aot_only_var_handle_op", + srcs = ["aot_only_var_handle_op.cc"], + hdrs = ["aot_only_var_handle_op.h"], + visibility = [ + "//tensorflow/compiler/tf2xla:__pkg__", + ], + deps = [ + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/compiler/tf2xla:xla_context", + "//tensorflow/compiler/tf2xla:xla_op_registry", + "//tensorflow/core:framework", + ], + alwayslink = 1, +) + +tf_cc_test( + name = "benchmark_test", + srcs = ["benchmark_test.cc"], + tags = ["manual"], + deps = [ + ":benchmark", + ":test_graph_tfadd", + "//tensorflow/core:test", + "//tensorflow/core:test_main", + ], +) + +test_suite( + name = "all_tests", + tags = ["manual"], + tests = [ + ":benchmark_test", + ":codegen_test", + ":test_graph_tfadd_mlir_bridge_test", + ":test_graph_tfadd_test", + ":test_graph_tfunknownop2_mlir_bridge_test", + ":test_graph_tfunknownop2_test", + ":test_graph_tfunknownop3_mlir_bridge_test", + ":test_graph_tfunknownop3_test", + ":test_graph_tfunknownop_mlir_bridge_test", + ":test_graph_tfunknownop_test", + "//tensorflow/compiler/aot/tests:all_tests", + ], +) + +exports_files([ + "benchmark_main.template", # used by tf_library(...,gen_benchmark=True) + if_oss("test.cc", "test_google.cc"), # used by tf_library(...,gen_test=True) +]) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/aot_only_var_handle_op.cc b/western_ai/usa/tensorflow/tensorflow/compiler/aot/aot_only_var_handle_op.cc new file mode 100644 index 0000000000000000000000000000000000000000..60481e6077f461e1dd8ac6050212ea7eb8122904 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/aot_only_var_handle_op.cc @@ -0,0 +1,85 @@ +/* Copyright 2019 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/compiler/aot/aot_only_var_handle_op.h" + +#include "tensorflow/compiler/tf2xla/xla_context.h" +#include "tensorflow/compiler/tf2xla/xla_op_kernel.h" +#include "tensorflow/compiler/tf2xla/xla_op_registry.h" +#include "tensorflow/core/framework/shape_inference.h" + +namespace tensorflow { +namespace { + +// Implementation of varhandle that binds a VarHandleOp to an XlaResource of the +// same name. It is not safe to use this op in a JIT context. +class XlaAotOnlyVarHandleOp : public XlaOpKernel { + public: + explicit XlaAotOnlyVarHandleOp(OpKernelConstruction* c); + void Compile(XlaOpKernelContext* context) override; + + private: + std::string name_; +}; + +XlaAotOnlyVarHandleOp::XlaAotOnlyVarHandleOp(OpKernelConstruction* c) + : XlaOpKernel(c) { + OP_REQUIRES_OK(c, c->GetAttr("shared_name", &name_)); +} + +void XlaAotOnlyVarHandleOp::Compile(XlaOpKernelContext* context) { + // Look for a resource of the same name. TF also keys that on the container + // and type attributes, but that doesn't seem necessary. + for (const auto& resource : context->xla_context()->resources()) { + if (resource->kind() == XlaResource::kVariable && + resource->name() == name_) { + context->SetResourceOutput(0, resource.get()); + return; + } + } + context->SetStatus(absl::InvalidArgumentError( + absl::StrCat("Variable: ", name_, " not configured"))); +} +} // namespace + +REGISTER_OP(tfcompile::kXlaAotOnlyVarHandleOp) + .Doc(R"doc( +Internal VarHandleOp registration used for XLA AOT compilation. +)doc") + .Attr("container: string = ''") + .Attr("shared_name: string = ''") + .Attr("debug_name: string = ''") + .Attr("dtype: type") + .Attr("shape: shape") + .Output("resource: resource") + .SetIsStateful() + .SetShapeFn([](shape_inference::InferenceContext* c) { + c->set_output(0, c->Scalar()); + DataType t; + TF_RETURN_IF_ERROR(c->GetAttr("dtype", &t)); + PartialTensorShape p; + TF_RETURN_IF_ERROR(c->GetAttr("shape", &p)); + shape_inference::ShapeHandle s; + TF_RETURN_IF_ERROR(c->MakeShapeFromPartialTensorShape(p, &s)); + c->set_output_handle_shapes_and_types( + 0, std::vector{{s, t}}); + + return absl::OkStatus(); + }); + +REGISTER_XLA_OP(Name(tfcompile::kXlaAotOnlyVarHandleOp).CompilationOnly(), + XlaAotOnlyVarHandleOp); + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/aot_only_var_handle_op.h b/western_ai/usa/tensorflow/tensorflow/compiler/aot/aot_only_var_handle_op.h new file mode 100644 index 0000000000000000000000000000000000000000..10e0b08170f27a210f70360c4ded2b590342c14c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/aot_only_var_handle_op.h @@ -0,0 +1,27 @@ +/* Copyright 2020 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_AOT_AOT_ONLY_VAR_HANDLE_OP_H_ +#define TENSORFLOW_COMPILER_AOT_AOT_ONLY_VAR_HANDLE_OP_H_ + +namespace tensorflow { +namespace tfcompile { + +static constexpr const char* const kXlaAotOnlyVarHandleOp = + "_XlaAotOnlyVarHandleOp"; + +} // namespace tfcompile +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_AOT_AOT_ONLY_VAR_HANDLE_OP_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/benchmark.cc b/western_ai/usa/tensorflow/tensorflow/compiler/aot/benchmark.cc new file mode 100644 index 0000000000000000000000000000000000000000..8ef23d5f77ad2bd1e46a65459e8d7d2d686b58a7 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/benchmark.cc @@ -0,0 +1,138 @@ +/* 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. +==============================================================================*/ + +// The purpose of the benchmark library is to support building an aot binary +// with minimal dependencies, to demonstrate small binary sizes. +// +// KEEP THE DEPENDENCIES MINIMAL. + +#include "tensorflow/compiler/aot/benchmark.h" + +#include + +#include +#include +#include +#include + +#include "tensorflow/core/platform/types.h" + +namespace tensorflow { +namespace tfcompile { +namespace benchmark { + +// Returns current wall time in micros. +// +// TODO(b/33546473): Refactor tensorflow::Env::NowMicros() so that we can re-use +// the implementation without pulling in all of the Env dependencies. +static uint64_t NowMicros() { + struct timeval tv; + gettimeofday(&tv, nullptr); + return static_cast(tv.tv_sec) * 1000000 + tv.tv_usec; +} + +void DumpStatsToStdout(const Stats& stats) { + // Compute stats. + std::vector sorted_us(stats.per_iter_us); + std::sort(sorted_us.begin(), sorted_us.end()); + const size_t count_us = sorted_us.size(); + double sum_us = 0; + size_t count_us_trimmed = 0; + double sum_us_trimmed = 0; + size_t count_us_best = 0; + double sum_us_best = 0; + static constexpr float trim_ratio = 0.25; + static constexpr float best_ratio = 0.1; + const size_t count_trimmed = count_us * trim_ratio; + const size_t count_best = count_us * best_ratio; + for (size_t i = 0; i < sorted_us.size(); ++i) { + const int64_t us = sorted_us[i]; + sum_us += us; + if (i >= count_trimmed && i < count_us - count_trimmed) { + sum_us_trimmed += us; + ++count_us_trimmed; + } + if (i < count_best) { + sum_us_best += us; + ++count_us_best; + } + } + // Prepare nicely-formatted data. + const int kBufSize = 1000; + char buf[kBufSize]; + snprintf(buf, kBufSize, "Mean with %2.0f%% trimmed:", trim_ratio * 100); + std::string label_trimmed(buf); + snprintf(buf, kBufSize, "Mean of %2.0f%% best:", best_ratio * 100); + std::string label_best(buf); + std::vector> groups = { + {"Best:", sorted_us.front()}, + {"Worst:", sorted_us.back()}, + {"Median:", sorted_us[count_us / 2]}, + {"Mean:", sum_us / count_us}, + {std::move(label_trimmed), sum_us_trimmed / count_us_trimmed}, + {std::move(label_best), sum_us_best / count_us_best}, + }; + int max_label_size = 0; + double max_us = 0; + for (const auto& g : groups) { + if (g.first.size() > max_label_size) { + max_label_size = g.first.size(); + } + if (g.second > max_us) { + max_us = g.second; + } + } + int max_digits = 1; + while (max_us >= 10.0) { + max_us /= 10.0; + ++max_digits; + } + // Dump stats out. + printf("Benchmark ran %zu iterations over %lld us\n", count_us, + static_cast(stats.total_us)); // NOLINT + for (const auto& g : groups) { + printf(" %-*s %*.3f us\n", max_label_size, g.first.c_str(), max_digits + 4, + g.second); + } +} + +void Benchmark(const Options& options, const BenchmarkFn& fn, Stats* stats) { + // If neither max_seconds or max_iters is set, stop at kDefaultMicros. + const int64_t max_us = (options.max_micros <= 0 && options.max_iters <= 0) + ? Options::kDefaultMicros + : options.max_micros; + // NOLINTNEXTLINE + printf("Running benchmark for %lld us\n", static_cast(max_us)); + const int64_t start_us = NowMicros(); + int64_t iters = 0; + while (true) { + const int64_t iter_start_us = NowMicros(); + fn(); + const int64_t end_us = NowMicros(); + // Collect stats and decide whether to stop. + stats->per_iter_us.push_back(end_us - iter_start_us); + const int64_t total_us = end_us - start_us; + ++iters; + if ((max_us > 0 && total_us >= max_us) || + (options.max_iters > 0 && iters >= options.max_iters)) { + stats->total_us = total_us; + break; + } + } +} + +} // namespace benchmark +} // namespace tfcompile +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/benchmark.h b/western_ai/usa/tensorflow/tensorflow/compiler/aot/benchmark.h new file mode 100644 index 0000000000000000000000000000000000000000..823f7eeec97604f987a75ab36276fc57aa9789ee --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/benchmark.h @@ -0,0 +1,70 @@ +/* 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. +==============================================================================*/ + +// Contains benchmark functions used with the code-generated benchmarks that can +// be used to test a model on android. See also code generation rules in +// tfcompile.bzl. +// +// This is separate from the built-in micro-benchmarks, because we want to: +// 1. show a binary with minimal dependencies, to show a close-to-lower-bound +// binary size. +// 2. compile on Android. +#ifndef TENSORFLOW_COMPILER_AOT_BENCHMARK_H_ +#define TENSORFLOW_COMPILER_AOT_BENCHMARK_H_ + +#include +#include +#include + +#include "tensorflow/core/platform/types.h" + +namespace tensorflow { +namespace tfcompile { +namespace benchmark { + +// Options specifies options for benchmarks of functions generated by tfcompile. +struct Options { + // kDefaultMicros specifies the default time to run the benchmark, and is used + // if neither max_iters nor max_micros is set. + static constexpr int64_t kDefaultMicros = 3000000; + + int64_t max_iters = 0; // Maximum iterations to run, ignored if <= 0. + int64_t max_micros = 0; // Maximum microseconds to run, ignored if <= 0. +}; + +// Stats holds statistics collected during benchmarking. +struct Stats { + std::vector per_iter_us; // Per-iteration deltas in us. + int64_t total_us; // Total time in us. + + Stats() : total_us(0) { per_iter_us.reserve(5000); } +}; + +// DumpStatsToStdout printfs to stdout stats in a multi-line human-friendly +// form. +void DumpStatsToStdout(const Stats& stats); + +// BenchmarkFn is the signature of the function generated by tfcompile. +typedef std::function BenchmarkFn; + +// Benchmark runs a benchmark of the function `fn`, collecting stats in `stats`. +// Use `options` to configure benchmarking options. +void Benchmark(const Options& options, const BenchmarkFn& fn, Stats* stats); + +} // namespace benchmark +} // namespace tfcompile +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_AOT_BENCHMARK_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/benchmark_main.template b/western_ai/usa/tensorflow/tensorflow/compiler/aot/benchmark_main.template new file mode 100644 index 0000000000000000000000000000000000000000..f3cba3739dd349d3fc9d9a46731fa714d3ad0481 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/benchmark_main.template @@ -0,0 +1,51 @@ +// Generated by the tf_library build rule. DO NOT EDIT! +// +// This file contains the main function and logic for benchmarking code +// generated by tfcompile. All tokens of the form `{{TFCOMPILE_*}}` must be +// rewritten to real values before this file can be compiled. +// +// TFCOMPILE_HEADER : Path to the header file generated by tfcompile. +// TFCOMPILE_CPP_CLASS : Name of the C++ class generated by tfcompile. +// +// The tf_library bazel macro in tfcompile.bzl performs the token rewriting, and +// generates a cc_binary rule for you. + +// These macros must be defined before eigen files are included. +#define EIGEN_USE_THREADS +#define EIGEN_USE_CUSTOM_THREAD_POOL + +// clang-format off +#include "{{TFCOMPILE_HEADER}}" // NOLINT(whitespace/braces) +// clang-format on + +#include "tensorflow/compiler/aot/benchmark.h" +#include "unsupported/Eigen/CXX11/Tensor" + +// Macros that expand to tokens based on the entry point name. +// clang-format off +#define CPP_CLASS {{TFCOMPILE_CPP_CLASS}} // NOLINT(whitespace/braces) +// clang-format on + +namespace tensorflow { +namespace tfcompile { + +int Main(int argc, char** argv) { + Eigen::ThreadPool pool(1 /* num_threads */); + Eigen::ThreadPoolDevice device(&pool, pool.NumThreads()); + + CPP_CLASS computation; + computation.set_thread_pool(&device); + + benchmark::Options options; + benchmark::Stats stats; + benchmark::Benchmark(options, [&] { computation.Run(); }, &stats); + benchmark::DumpStatsToStdout(stats); + return 0; +} + +} // namespace tfcompile +} // namespace tensorflow + +int main(int argc, char** argv) { + return tensorflow::tfcompile::Main(argc, argv); +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/benchmark_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/aot/benchmark_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..4a658c5e149073f2b4cf9883f2b4a074343a1f08 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/benchmark_test.cc @@ -0,0 +1,46 @@ +/* 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/compiler/aot/benchmark.h" + +#include "tensorflow/compiler/aot/test_graph_tfadd.h" +#include "tensorflow/core/platform/test.h" + +namespace tensorflow { +namespace tfcompile { +namespace benchmark { +namespace { + +// There isn't much we can verify in a stable fashion, so we just run the +// benchmark with max_iters, and ensure we end up with that many iter stats. +TEST(Benchmark, Benchmark) { + AddComp add; + + Options options; + options.max_iters = 1; + Stats stats1; + Benchmark(options, [&] { add.Run(); }, &stats1); + EXPECT_EQ(stats1.per_iter_us.size(), 1); + + options.max_iters = 5; + Stats stats5; + Benchmark(options, [&] { add.Run(); }, &stats5); + EXPECT_EQ(stats5.per_iter_us.size(), 5); +} + +} // namespace +} // namespace benchmark +} // namespace tfcompile +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/codegen.cc b/western_ai/usa/tensorflow/tensorflow/compiler/aot/codegen.cc new file mode 100644 index 0000000000000000000000000000000000000000..f0cb617acba2177dd307c2092b04aa48c7e5368b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/codegen.cc @@ -0,0 +1,1355 @@ +/* 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/compiler/aot/codegen.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "absl/algorithm/container.h" +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/ascii.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/str_format.h" +#include "absl/strings/str_join.h" +#include "absl/strings/str_replace.h" +#include "absl/strings/str_split.h" +#include "absl/strings/string_view.h" +#include "absl/strings/substitute.h" +#include "absl/types/span.h" +#include "tensorflow/compiler/aot/compile.h" +#include "tensorflow/compiler/aot/embedded_protocol_buffers.h" +#include "tensorflow/compiler/aot/thunk_proto_execution_deserializer.h" +#include "tensorflow/compiler/tf2xla/allocator.h" +#include "tensorflow/compiler/tf2xla/encoded_buffer_allocation_info.h" +#include "tensorflow/compiler/tf2xla/tf2xla.pb.h" +#include "tensorflow/compiler/tf2xla/tf2xla_util.h" +#include "xla/backends/cpu/buffer_allocation_info.h" +#include "xla/backends/cpu/buffer_allocation_info_util.h" +#include "xla/backends/cpu/runtime/thunk.pb.h" +#include "xla/backends/cpu/runtime/thunk_proto_serdes.h" +#include "xla/debug_options_flags.h" +#include "xla/service/cpu/cpu_aot_compilation_result.h" +#include "xla/service/cpu/cpu_executable.h" +#include "xla/shape.h" +#include "xla/shape_util.h" +#include "xla/tsl/platform/errors.h" +#include "xla/tsl/platform/statusor.h" +#include "xla/util.h" +#include "xla/util/embedded_constant_buffers.h" +#include "xla/xla_data.pb.h" +#include "tensorflow/core/lib/core/errors.h" +#include "tsl/platform/casts.h" + +namespace tensorflow { +namespace tfcompile { + +namespace { + +using xla::cpu::BufferAllocationInfo; + +// Convert an XLA type into a C++ type. +absl::Status XLATypeToCpp(xla::PrimitiveType type, std::string* str) { + switch (type) { + case xla::PRED: + *str = "bool"; + break; + case xla::S8: + *str = "int8_t"; + break; + case xla::S16: + *str = "int16_t"; + break; + case xla::S32: + *str = "int32_t"; + break; + case xla::S64: + *str = "int64_t"; + break; + case xla::U8: + *str = "uint8_t"; + break; + case xla::U16: + *str = "uint16_t"; + break; + case xla::U32: + *str = "uint32_t"; + break; + case xla::U64: + *str = "uint64_t"; + break; + case xla::F32: + *str = "float"; + break; + case xla::F64: + *str = "double"; + break; + default: + return absl::UnimplementedError( + absl::StrCat("XLA type ", xla::PrimitiveType_Name(type), + " has no equivalent in C++")); + } + return absl::OkStatus(); +} + +// Returns the sum of the size of each buffer in `buffer_infos`. +size_t TotalBufferBytes(absl::Span buffer_infos) { + return std::accumulate( + buffer_infos.begin(), buffer_infos.end(), size_t{0}, + [](size_t size, const BufferAllocationInfo& buffer_info) { + return size + buffer_info.size(); + }); +} + +// Returns a vector of BufferAllocationInfo instances in `buffer_infos` that are +// entry parameter buffers. +std::vector ExtractEntryParamBufferAllocationInfos( + absl::Span buffer_infos) { + std::vector result; + std::copy_if(buffer_infos.begin(), buffer_infos.end(), + std::back_inserter(result), + [](const BufferAllocationInfo& buffer_info) { + return buffer_info.is_entry_parameter(); + }); + return result; +} + +// Returns a vector of BufferAllocationInfo instances in `buffer_infos` that are +// temp buffers. +std::vector ExtractTempBufferAllocationInfos( + absl::Span buffer_infos) { + std::vector result; + std::copy_if(buffer_infos.begin(), buffer_infos.end(), + std::back_inserter(result), + [](const BufferAllocationInfo& buffer_info) { + return buffer_info.is_temp(); + }); + return result; +} + +// Add (from,to) rewrite pairs based on the given shape. These rewrite pairs +// are used to generate methods for args and results. +absl::Status AddRewritesForShape( + int i, const xla::Shape& shape, + std::vector>* rewrites) { + std::string type; + TF_RETURN_IF_ERROR(XLATypeToCpp(shape.element_type(), &type)); + std::vector dim_vars; + std::string dim_sizes, indices; + int count = 1; + if (shape.dimensions().size() == 0 || + (shape.dimensions().size() == 1 && shape.dimensions(0) == 1)) { + dim_sizes = "[1]"; + indices = "[0]"; + } else { + for (int dim = 0; dim < shape.dimensions().size(); ++dim) { + dim_vars.push_back(absl::StrCat("size_t dim", dim)); + absl::StrAppend(&dim_sizes, "[", shape.dimensions(dim), "]"); + absl::StrAppend(&indices, "[dim", dim, "]"); + count *= shape.dimensions(dim); + } + } + rewrites->push_back({"{{I}}", absl::StrCat(i)}); + rewrites->push_back({"{{TYPE}}", type}); + rewrites->push_back({"{{DIM_VARS}}", absl::StrJoin(dim_vars, ", ")}); + rewrites->push_back({"{{DIM_SIZES}}", dim_sizes}); + rewrites->push_back({"{{INDICES}}", indices}); + rewrites->push_back({"{{COUNT}}", absl::StrCat(count)}); + return absl::OkStatus(); +} + +// Returns code rewritten by replacing all rewrite pairs, with an extra rewrite +// for the name. Note that the rewriting strategy is roughly O(N*M), where N is +// the size of the code and M is the number of rewrites. It's fine for now +// since N and M are pretty small. +// +// TODO(toddw): If this becomes a problem, we should be able to change the +// algorithm to O(N) by using a state machine, e.g. regexps or a real +// text-templating mechanism. +std::string RewriteWithName( + const std::string& name, std::string code, + const std::vector>& rewrites) { + absl::StrReplaceAll(rewrites, &code); + absl::StrReplaceAll({{"{{NAME}}", name}}, &code); + return code; +} + +// Generate methods for args (inputs). +absl::Status GenArgMethods(const tf2xla::Config& config, + const xla::ProgramShapeProto& ps, + const CompileResult& compile_result, + std::string* methods) { + const int num_args = ps.parameters_size(); + // feed_size() + variable_size() is the maximum number of args as an + // implementation may not create an argument for an unused variable. + if (config.feed_size() + config.variable_size() < num_args) { + return absl::InvalidArgumentError(absl::StrCat( + "mismatch between feed_size(", config.feed_size(), ")+variable_size(", + config.variable_size(), ") and num_args(", num_args, ")")); + } + for (int i = 0; i < config.feed_size(); ++i) { + std::vector> rewrites; + TF_ASSIGN_OR_RETURN(xla::Shape shape, + xla::Shape::FromProto(ps.parameters(i))); + TF_RETURN_IF_ERROR(AddRewritesForShape(i, shape, &rewrites)); + const std::string code = R"( + void set_arg{{NAME}}_data(const void* data) { + set_arg_data({{I}}, data); + } + {{TYPE}}* arg{{NAME}}_data() { + return static_cast<{{TYPE}}*>(arg_data({{I}})); + } + {{TYPE}}& arg{{NAME}}({{DIM_VARS}}) { + return (*static_cast<{{TYPE}}(*){{DIM_SIZES}}>( + arg_data({{I}}))){{INDICES}}; + } + const {{TYPE}}* arg{{NAME}}_data() const { + return static_cast(arg_data({{I}})); + } + const {{TYPE}}& arg{{NAME}}({{DIM_VARS}}) const { + return (*static_cast( + arg_data({{I}}))){{INDICES}}; + } + int arg{{NAME}}_size() const { + return {{COUNT}} * sizeof({{TYPE}}); + } + int arg{{NAME}}_count() const { + return {{COUNT}}; + } +)"; + *methods += RewriteWithName(absl::StrCat(i), code, rewrites); + if (!config.feed(i).name().empty()) { + *methods += RewriteWithName("_" + config.feed(i).name(), code, rewrites); + } + } + return absl::OkStatus(); +} + +// Generate methods for results (outputs). +absl::Status GenResultMethods(const tf2xla::Config& config, + const xla::ProgramShapeProto& ps, + std::string* methods) { + if (ps.result().element_type() != xla::TUPLE) { + // The XlaCompiler we use to build the xla computation always generates a + // tuple result, and we rely on this to simplify code generation. + return xla::Internal("codegen requires the XLA result to be a tuple"); + } + size_t num_results = ps.result().tuple_shapes_size(); + int readonly_variables = absl::c_count_if( + config.variable(), + [](const tf2xla::Variable& var) { return var.readonly(); }); + const int actual_num_results = + config.fetch_size() + config.variable_size() - readonly_variables; + if (actual_num_results != num_results) { + return absl::InvalidArgumentError(absl::StrCat( + "mismatch between fetch_size(", config.fetch_size(), ")+variable_size(", + config.variable_size(), ") and tuple_size(", + ps.result().tuple_shapes_size(), ")")); + } + for (int i = 0; i < config.fetch_size(); ++i) { + std::vector> rewrites; + TF_ASSIGN_OR_RETURN(xla::Shape shape, + xla::Shape::FromProto(ps.result().tuple_shapes(i))); + TF_RETURN_IF_ERROR(AddRewritesForShape(i, shape, &rewrites)); + std::string code = R"( + {{TYPE}}* result{{NAME}}_data() { + return static_cast<{{TYPE}}*>(result_data({{I}})); + } + {{TYPE}}& result{{NAME}}({{DIM_VARS}}) { + return (*static_cast<{{TYPE}}(*){{DIM_SIZES}}>( + result_data({{I}}))){{INDICES}}; + } + const {{TYPE}}* result{{NAME}}_data() const { + return static_cast(result_data({{I}})); + } + const {{TYPE}}& result{{NAME}}({{DIM_VARS}}) const { + return (*static_cast( + result_data({{I}}))){{INDICES}}; + } + int result{{NAME}}_size() const { + return {{COUNT}} * sizeof({{TYPE}}); + } + int result{{NAME}}_count() const { + return {{COUNT}}; + } +)"; + *methods += RewriteWithName(absl::StrCat(i), code, rewrites); + if (!config.fetch(i).name().empty()) { + *methods += RewriteWithName("_" + config.fetch(i).name(), code, rewrites); + } + } + return absl::OkStatus(); +} + +// Generate methods for variables. +absl::Status GenVariableMethods(const tf2xla::Config& config, + const xla::ProgramShapeProto& ps, + std::string* methods) { + const int num_args = ps.parameters_size(); + for (int i = config.feed_size(); i < num_args; ++i) { + std::vector> rewrites; + TF_ASSIGN_OR_RETURN(xla::Shape shape, + xla::Shape::FromProto(ps.parameters(i))); + TF_RETURN_IF_ERROR(AddRewritesForShape(i, shape, &rewrites)); + const std::string code = R"( + void set_var_{{NAME}}_data({{MAYBE_CONST}}{{TYPE}}* data) { + set_arg_data({{I}}, data); + } + {{MAYBE_CONST}}{{TYPE}}* var_{{NAME}}_data() { + return static_cast<{{MAYBE_CONST}}{{TYPE}}*>(arg_data({{I}})); + } + {{MAYBE_CONST}}{{TYPE}}& var_{{NAME}}({{DIM_VARS}}) { + return (*static_cast<{{MAYBE_CONST}}{{TYPE}}(*){{DIM_SIZES}}>( + arg_data({{I}}))){{INDICES}}; + } + const {{TYPE}}* var_{{NAME}}_data() const { + return static_cast(arg_data({{I}})); + } + const {{TYPE}}& var_{{NAME}}({{DIM_VARS}}) const { + return (*static_cast( + arg_data({{I}}))){{INDICES}}; + } + int var_{{NAME}}_size() const { + return {{COUNT}} * sizeof({{TYPE}}); + } + int var_{{NAME}}_count() const { + return {{COUNT}}; + } +)"; + const tf2xla::Variable& var = config.variable(i - config.feed_size()); + rewrites.emplace_back("{{MAYBE_CONST}}", var.readonly() ? "const " : ""); + *methods += RewriteWithName( + var.name().empty() ? var.node_name() : var.name(), code, rewrites); + } + return absl::OkStatus(); +} + +// Generate shape infos for args (inputs). +absl::Status GenArgShapeInfos(const xla::ProgramShapeProto& ps, + std::string* infos) { + for (int i = 0; i < ps.parameters_size(); ++i) { + const xla::ShapeProto& shape = ps.parameters(i); + if (shape.element_type() == xla::TUPLE) { + // ShapeInfo cannot represent tuple args. + return absl::InternalError( + absl::StrCat("parameter ", i, + ": codegen requires XLA parameters to " + "be non-tuples.")); + } + // Please some compilers (e.g. MSVC) by avoiding the initialization of an + // array of unknown size an empty initializer. Use "-1" for this; note that + // this value is never used (the size attribute is set to 0 in ShapeInfo). + *infos += absl::Substitute(R"( static constexpr int32_t kArg$0Shapes[] = { +$1 + }; +)", + i, + shape.dimensions_size() > 0 + ? absl::StrJoin(shape.dimensions(), ", ") + : "-1"); + } + *infos += R"( static const ShapeInfo* ArgShapeInfos() { + static constexpr ShapeInfo kArgShapeInfoTable[kNumArgs] = { +)"; + for (int i = 0; i < ps.parameters_size(); ++i) { + const xla::ShapeProto& shape = ps.parameters(i); + *infos += + absl::Substitute("{ kArg$0Shapes, $1 },\n", i, shape.dimensions_size()); + } + *infos += R"( }; + return kArgShapeInfoTable; + })"; + return absl::OkStatus(); +} + +// Generate shape infos for results. +absl::Status GenResultShapeInfos(const xla::ProgramShapeProto& ps, + std::string* infos) { + if (ps.result().element_type() != xla::TUPLE) { + return absl::InternalError("codegen requires the XLA result to be a tuple"); + } + for (int i = 0; i < ps.result().tuple_shapes_size(); ++i) { + const xla::ShapeProto& shape = ps.result().tuple_shapes(i); + // See above comment about the use here of "-1". + *infos += absl::Substitute( + R"( static constexpr int32_t kResult$0Shapes[] = { +$1 + }; +)", + i, + shape.dimensions_size() > 0 ? absl::StrJoin(shape.dimensions(), ", ") + : "-1"); + } + *infos += R"( static const ShapeInfo* ResultShapeInfos() { + static constexpr ShapeInfo kResultShapeInfoTable[kNumResults] = { +)"; + for (int i = 0; i < ps.result().tuple_shapes_size(); ++i) { + const xla::ShapeProto& shape = ps.result().tuple_shapes(i); + *infos += absl::Substitute("{ kResult$0Shapes, $1 },\n", i, + shape.dimensions_size()); + } + *infos += R"( }; + return kResultShapeInfoTable; + })"; + return absl::OkStatus(); +} + +// Generates code implementing {Arg,Result}Names(), where T is one of +// tf2xla::{Feed,Fetch,Variable}. Each feed or fetch name results in a C-style +// string literal in the array, with nullptr terminating the array. +template +std::string GenNameToIndexCode(const T& entries, bool generate) { + // No need for a static array if we're not supposed to generate the data. + if (!generate) { + return "{\n return nullptr;\n }"; + } + // Determine when to stop. We stop emitting string literals after the last + // non-empty name. + int end = entries.size(); + for (int i = entries.size() - 1; i >= 0; --i) { + if (!entries[i].name().empty()) { + break; + } + end = i; + } + // Emit string literals up to the last non-empty name. + std::string code = "{\n static const char* kNames[] = {"; + for (int i = 0; i < end; ++i) { + if (i > 0) { + code += ", "; + } + code += "\""; + code += entries[i].name(); + code += "\""; + } + if (end > 0) { + code += ", "; + } + code += "nullptr};\n return kNames;\n }"; + return code; +} + +absl::Status ValidateFeedFetchCppNames(const tf2xla::Config& config) { + for (const tf2xla::Feed& feed : config.feed()) { + if (!feed.name().empty()) { + TF_RETURN_IF_ERROR(ValidateCppIdent(feed.name(), "feed name")); + } + } + for (const tf2xla::Fetch& fetch : config.fetch()) { + if (!fetch.name().empty()) { + TF_RETURN_IF_ERROR(ValidateCppIdent(fetch.name(), "fetch name")); + } + } + for (const tf2xla::Variable& variable : config.variable()) { + if (!variable.name().empty()) { + TF_RETURN_IF_ERROR(ValidateCppIdent(variable.name(), "variable name")); + } else { + TF_RETURN_IF_ERROR( + ValidateCppIdent(variable.node_name(), "variable name")); + } + } + return absl::OkStatus(); +} + +// Returns a list of C++ expressions that, when executed, will construct the +// BufferAllocationInfo instances in `buffer_infos`. +std::vector BufferAllocationInfosToCppExpression( + absl::Span buffer_infos) { + std::vector buffer_infos_as_strings; + absl::c_transform( + buffer_infos, std::back_inserter(buffer_infos_as_strings), + [](const BufferAllocationInfo& buffer_info) { + xla::cpu::EncodedBufferAllocationInfo encoded(buffer_info); + auto param_to_str = [](int32_t param) -> std::string { + return param == -1 ? "~0U" : absl::StrCat(param, "U"); + }; + return absl::StrCat( + "static_cast<::xla::cpu::BufferAllocationInfo>(" + "::xla::cpu::EncodedBufferAllocationInfo{", + encoded.packed_kind_and_size, "ULL, ", + param_to_str(encoded.entry_param_number), ", ", + param_to_str(encoded.result_number), "})"); + }); + return buffer_infos_as_strings; +} + +absl::Status CheckEqual(size_t a, size_t b, absl::string_view error_msg) { + if (a != b) { + return absl::InternalError( + absl::StrCat(error_msg, ". Expected ", a, ", got ", b, ".")); + } + return absl::OkStatus(); +} + +absl::StatusOr GenFunctionDeclaration( + const xla::cpu::SymbolProto& symbol) { + std::string function_declaration; + + switch (symbol.function_type_id()) { + case xla::cpu::SymbolProto::COMPARATOR: + absl::StrAppend( + &function_declaration, " void ", symbol.name(), + "(bool* result, const void* run_options, const void** params, " + "const void* buffer_table, const void* status, " + "const void* prof_counters);"); + break; + case xla::cpu::SymbolProto::KERNEL: + absl::StrAppend(&function_declaration, " XLA_CPU_KernelError* ", + symbol.name(), + "(const XLA_CPU_KernelCallFrame* call_frame);"); + break; + default: + return absl::InvalidArgumentError( + absl::StrCat("Unsupported symbol kind: ", symbol.function_type_id())); + } + return function_declaration; +} + +absl::StatusOr GenFunctionDeclarations( + absl::Span compiled_symbols) { + std::string function_declarations = + !compiled_symbols.empty() ? "extern \"C\" {\n" : ""; + for (const auto& symbol : compiled_symbols) { + TF_ASSIGN_OR_RETURN(std::string function_declaration, + GenFunctionDeclaration(symbol)); + absl::StrAppend(&function_declarations, function_declaration, "\n"); + } + if (!compiled_symbols.empty()) { + function_declarations += "}\n"; + } + return function_declarations; +} + +absl::StatusOr GenSymbolMapInitializerString( + absl::Span entry_point_symbols) { + std::string symbol_map_initializer = "{"; + for (const auto& symbol : entry_point_symbols) { + absl::StrAppend(&symbol_map_initializer, + " std::pair{\"", symbol.name(), + "\", reinterpret_cast(", symbol.name(), ")},"); + } + symbol_map_initializer += "}"; + return symbol_map_initializer; +} + +std::vector ExtractEntryPointSymbols( + const xla::cpu::CompilationResultProto& proto) { + std::vector compiled_symbols; + for (const auto& symbol : proto.compiled_symbols()) { + compiled_symbols.push_back(symbol); + } + + std::vector entry_point_symbols; + + auto check_if_compiled_symbol_and_add_entry_point = + [](const std::string& symbol_name, + const std::vector& compiled_symbols, + std::vector& entry_point_symbols) { + auto it = std::find_if(compiled_symbols.begin(), compiled_symbols.end(), + [&symbol_name](const auto& symbol) { + return symbol.name() == symbol_name; + }); + + if (it != compiled_symbols.end()) { + entry_point_symbols.push_back(*it); + } + }; + + xla::cpu::ForEachThunkProto( + proto.thunk_sequence(), [&entry_point_symbols, &compiled_symbols, + &check_if_compiled_symbol_and_add_entry_point]( + const xla::cpu::ThunkProto& thunk) { + if (thunk.has_kernel_thunk()) { + auto symbol_name = thunk.kernel_thunk().kernel_name(); + check_if_compiled_symbol_and_add_entry_point( + symbol_name, compiled_symbols, entry_point_symbols); + } else if (thunk.has_sort_thunk()) { + auto symbol_name = thunk.sort_thunk().comparator_name(); + check_if_compiled_symbol_and_add_entry_point( + symbol_name, compiled_symbols, entry_point_symbols); + } + }); + + return entry_point_symbols; +} + +bool HasThunkKind(const xla::cpu::ThunkSequenceProto& thunk_sequence, + xla::cpu::ThunkProto::ImplCase kind) { + bool has_kind = false; + xla::cpu::ForEachThunkProto( + thunk_sequence, [&kind, &has_kind](const xla::cpu::ThunkProto& thunk) { + if (thunk.impl_case() == kind) { + has_kind = true; + } + }); + + return has_kind; +} + +absl::StatusOr> ExtractThunksOfKind( + const xla::cpu::ThunkSequenceProto& thunk_sequence, + xla::cpu::ThunkProto::ImplCase kind) { + std::vector matching_thunks; + + xla::cpu::ForEachThunkProto( + thunk_sequence, + [&kind, &matching_thunks](const xla::cpu::ThunkProto& thunk) { + if (thunk.impl_case() == kind) { + matching_thunks.push_back(thunk); + } + }); + + return matching_thunks; +} + +absl::StatusOr GetThunkSpecificConstantAllocationsInitializers( + const xla::cpu::CompilationResultProto& proto, + const xla::EmbeddedConstantBuffers& embedded_constant_buffers) { + std::vector constant_buffer_accesses; + constant_buffer_accesses.reserve( + embedded_constant_buffers.variable_decls.size()); + for (const auto& variable_decl : embedded_constant_buffers.variable_decls) { + constant_buffer_accesses.push_back(variable_decl.cpp_access_shim); + } + std::string constant_allocations_initializer = absl::StrCat( + R"( +{ +)", + absl::StrJoin(constant_buffer_accesses, ", "), + R"( +}; +)"); + + return constant_allocations_initializer; +} + +absl::Status ExtendRewrites( + std::vector>& rewrites, + const xla::cpu::CpuAotCompilationResult* aot_thunks, + const MetadataResult& metadata_result, const CodegenOpts& codegen_opts, + const xla::EmbeddedConstantBuffers& embedded_constant_buffers) { + std::vector entry_point_symbols = + ExtractEntryPointSymbols(aot_thunks->proto()); + + TF_ASSIGN_OR_RETURN( + const std::string symbol_map_initializer, + GenSymbolMapInitializerString(absl::MakeSpan(entry_point_symbols))); + + TF_ASSIGN_OR_RETURN( + const std::string function_declarations_from_obj_files, + GenFunctionDeclarations(absl::MakeSpan(entry_point_symbols))); + + int64_t buffer_infos_size = aot_thunks->buffer_allocation_infos().size(); + std::optional temp_allocation_index = + aot_thunks->temp_allocation_index(); + if (temp_allocation_index.has_value() && + (*temp_allocation_index < 0 || + *temp_allocation_index >= buffer_infos_size)) { + return absl::InvalidArgumentError(absl::StrCat( + "temp allocation index: ", *temp_allocation_index, + " is outside the range of temp sizes: [0,", buffer_infos_size, ")")); + } + + std::vector runtime_specific_includes = {R"( +#include "absl/log/check.h" +#include "absl/synchronization/blocking_counter.h" +#include "xla/backends/cpu/runtime/kernel_c_api.h" +#include "xla/types.h")"}; + + if (HasThunkKind(aot_thunks->proto().thunk_sequence(), + xla::cpu::ThunkProto::kDotThunk)) { + runtime_specific_includes.push_back( + R"(#include "xla/backends/cpu/runtime/dot_lib.h")"); + } + + if (HasThunkKind(aot_thunks->proto().thunk_sequence(), + xla::cpu::ThunkProto::kConvolutionThunk)) { + runtime_specific_includes.push_back( + R"(#include "absl/synchronization/notification.h")"); + runtime_specific_includes.push_back( + R"(#include "xla/backends/cpu/runtime/convolution_lib.h")"); + } + + if (HasThunkKind(aot_thunks->proto().thunk_sequence(), + xla::cpu::ThunkProto::kSortThunk)) { + runtime_specific_includes.push_back( + R"(#include "xla/backends/cpu/runtime/sort_lib.h")"); + } + + if (HasThunkKind(aot_thunks->proto().thunk_sequence(), + xla::cpu::ThunkProto::kTopKThunk)) { + runtime_specific_includes.push_back( + R"(#include "xla/backends/cpu/runtime/topk_lib.h")"); + } + + if (HasThunkKind(aot_thunks->proto().thunk_sequence(), + xla::cpu::ThunkProto::kRngSeedThunk)) { + runtime_specific_includes.push_back(R"(#include )"); + } + + TF_ASSIGN_OR_RETURN( + const auto rng_thunks, + ExtractThunksOfKind(aot_thunks->proto().thunk_sequence(), + xla::cpu::ThunkProto::kRngGetAndUpdateStateThunk)); + + std::string executable_proto_getter = ""; + std::string thunk_specific_run_impl = ""; + std::string nanort_specific_static_data_setters = ""; + std::string thunk_specific_static_data_setters = ""; + std::string thunk_run_impl_getter = ""; + std::string embedded_constant_buffers_initializer_getter = ""; + std::string rng_deltas_initializer_getter = ""; + std::string computation_class_base = ""; + + if (codegen_opts.use_xla_nanort_runtime) { + executable_proto_getter = absl::StrReplaceAll( + R"(static const ::xla::cpu::CompilationResultProto* StaticCompilationResultProto() { + static const ::xla::cpu::CompilationResultProto* kCompilationResultProto = {{EXECUTABLE_PROTO_SHIM_EXPRESSION}}; + return kCompilationResultProto; + })", + {{"{{EXECUTABLE_PROTO_SHIM_EXPRESSION}}", + metadata_result.cpu_executable_access_shim}}); + + runtime_specific_includes.push_back( + R"(#include "tensorflow/compiler/tf2xla/xla_compiled_cpu_function_thunks.h")"); + + nanort_specific_static_data_setters = + "set_static_data_compilation_result_proto(data, " + "StaticCompilationResultProto());"; + + computation_class_base = "XlaCompiledCpuFunctionThunks"; + } else { + ThunkProtoExecutionDeserializer thunk_proto_execution_deserializer; + TF_ASSIGN_OR_RETURN(const std::string run_impl, + std::move(thunk_proto_execution_deserializer) + .GetThunkSpecificRunImpl(aot_thunks->proto())); + + nanort_specific_static_data_setters = + "set_static_data_compilation_result_proto(data, nullptr);"; + + computation_class_base = "XlaCompiledCpuFunction"; + + thunk_specific_static_data_setters = + R"(set_static_data_thunk_run_impl(data, ThunkRunImplFunction()); + set_static_data_embedded_constant_buffers(data, EmbeddedConstantBuffers()); + set_static_data_rng_state_deltas(data, RngStateDeltas());)"; + + std::vector rng_deltas; + rng_deltas.reserve(rng_thunks.size()); + for (const auto& rng_thunk : rng_thunks) { + rng_deltas.push_back(rng_thunk.rng_get_and_update_state_thunk().delta()); + } + rng_deltas_initializer_getter = absl::StrReplaceAll( + R"( + static std::vector RngStateDeltas() { + return { + {{RNG_DELTAS}} + }; + } + )", + {{"{{RNG_DELTAS}}", absl::StrJoin(rng_deltas, ", ")}}); + + thunk_run_impl_getter = absl::StrReplaceAll( + R"( + static std::function>&)> + ThunkRunImplFunction() { + return [](void** buffer_table, xla::ExecutableRunOptions* run_options, + std::vector>& rng_states) { + {{THUNK_RUN_IMPL}} + return true; + }; + } + )", + {{"{{THUNK_RUN_IMPL}}", run_impl}}); + } + + auto embedded_constant_buffers_initializer_getter_format = R"( + static std::vector> EmbeddedConstantBuffers() { + return {{EMBEDDED_CONSTANT_BUFFERS}}; + } + )"; + + TF_ASSIGN_OR_RETURN(auto const_buffs_initializer, + GetThunkSpecificConstantAllocationsInitializers( + aot_thunks->proto(), embedded_constant_buffers)); + embedded_constant_buffers_initializer_getter = absl::StrReplaceAll( + embedded_constant_buffers_initializer_getter_format, + {{"{{EMBEDDED_CONSTANT_BUFFERS}}", const_buffs_initializer}}); + + std::vector> rewrites_thunks = { + {"{{SYMBOL_MAP_INITIALIZER}}", symbol_map_initializer}, + {"{{FUNCTION_DECLARATIONS_FROM_OBJ_FILES}}", + function_declarations_from_obj_files}, + {"{{TEMP_ALLOCATION_INDEX}}", temp_allocation_index.has_value() + ? absl::StrCat(*temp_allocation_index) + : "std::nullopt"}, + {"{{RUNTIME_SPECIFIC_INCLUDES}}", + absl::StrJoin(runtime_specific_includes, "\n")}, + {"{{EXECUTABLE_PROTO_GETTER}}", executable_proto_getter}, + {"{{THUNK_SPECIFIC_STATIC_DATA_SETTERS}}", + thunk_specific_static_data_setters}, + {"{{THUNK_RUN_IMPL_GETTER}}", thunk_run_impl_getter}, + {"{{EMBEDDED_CONSTANT_BUFFERS_INITIALIZER_GETTER}}", + embedded_constant_buffers_initializer_getter}, + {"{{RNG_DELTAS_INITIALIZER_GETTER}}", rng_deltas_initializer_getter}, + {"{{NANORT_SPECIFIC_STATIC_DATA_SETTERS}}", + nanort_specific_static_data_setters}, + {"{{IS_THUNK_MODE}}", "true"}, + {"{{COMPUTATION_CLASS_BASE}}", computation_class_base}}; + + rewrites.insert(rewrites.end(), rewrites_thunks.begin(), + rewrites_thunks.end()); + + return absl::OkStatus(); +} + +} // namespace + +absl::Status GenerateHeader( + const CodegenOpts& opts, const tf2xla::Config& config, + const CompileResult& compile_result, const MetadataResult& metadata_result, + const xla::EmbeddedConstantBuffers& embedded_constant_buffers, + std::string* header) { + TF_RETURN_IF_ERROR(ValidateConfig(config)); + TF_RETURN_IF_ERROR(ValidateFeedFetchCppNames(config)); + + absl::Span buffer_infos = + compile_result.aot->buffer_allocation_infos(); + + const std::vector arg_index_table = + ::xla::cpu::CreateArgIndexTable(buffer_infos); + const std::vector result_index_table = + ::xla::cpu::CreateResultIndexTable(buffer_infos); + std::vector buffer_infos_as_strings = + BufferAllocationInfosToCppExpression(buffer_infos); + + // Compute sizes and generate methods. + std::vector buffer_infos_for_args = + ExtractEntryParamBufferAllocationInfos(buffer_infos); + std::vector buffer_infos_for_temps = + ExtractTempBufferAllocationInfos(buffer_infos); + const xla::ProgramShapeProto& ps = compile_result.program_shape; + std::string methods_arg, methods_result, methods_variable; + TF_RETURN_IF_ERROR(GenArgMethods(config, ps, compile_result, &methods_arg)); + TF_RETURN_IF_ERROR(GenResultMethods(config, ps, &methods_result)); + TF_RETURN_IF_ERROR(GenVariableMethods(config, ps, &methods_variable)); + std::string arg_shape_infos, result_shape_infos; + TF_RETURN_IF_ERROR(GenArgShapeInfos(ps, &arg_shape_infos)); + TF_RETURN_IF_ERROR( + CheckEqual(ps.parameters_size(), arg_index_table.size(), + "Arg number mismatch, proto vs. arg_index_table")); + TF_RETURN_IF_ERROR(GenResultShapeInfos(ps, &result_shape_infos)); + TF_RETURN_IF_ERROR( + CheckEqual(ps.result().tuple_shapes_size(), result_index_table.size(), + "Result number mismatch, proto vs. result_index_table")); + TF_ASSIGN_OR_RETURN(auto program_shape, xla::ProgramShape::FromProto(ps)); + const size_t arg_bytes_aligned = + tensorflow::AlignedBufferBytes(buffer_infos_for_args, + /*allocate_entry_params=*/true); + const size_t arg_bytes_total = TotalBufferBytes(buffer_infos_for_args); + const size_t temp_bytes_aligned = + tensorflow::AlignedBufferBytes(buffer_infos_for_temps, + /*allocate_entry_params=*/true); + const size_t temp_bytes_total = TotalBufferBytes(buffer_infos_for_temps); + + // Create rewrite strings for namespace start and end. + std::string ns_start; + for (const std::string& n : opts.namespaces) { + ns_start += absl::StrCat("namespace ", n, " {\n"); + } + ns_start += "\n"; + std::string ns_end("\n"); + for (int i = opts.namespaces.size() - 1; i >= 0; --i) { + const std::string& n = opts.namespaces[i]; + ns_end += absl::StrCat("} // end namespace ", n, "\n"); + } + + // Generate metadata. + const std::string arg_names_code = + GenNameToIndexCode(config.feed(), opts.gen_name_to_index); + + auto variable_copy = config.variable(); + for (auto& var : variable_copy) { + if (var.name().empty()) { + var.set_name(var.node_name()); + } + } + const std::string variable_names_code = + GenNameToIndexCode(variable_copy, opts.gen_name_to_index); + + const std::string result_names_code = + GenNameToIndexCode(config.fetch(), opts.gen_name_to_index); + const std::string include_xla_data_proto = + opts.gen_program_shape + ? R"(#include "xla/xla_data.pb.h")" + : ""; + + // Use a poor-man's text templating mechanism; first populate the full + // header with placeholder tokens, and then rewrite the tokens with real + // values. + *header = + R"(// Generated by tfcompile, the TensorFlow graph compiler. DO NOT EDIT! +// +// This header was generated via ahead-of-time compilation of a TensorFlow +// graph. An object file corresponding to this header was also generated. +// This header gives access to the functionality in that object file. +// +// clang-format off + +#ifndef TFCOMPILE_GENERATED_{{ENTRY}}_H_ // NOLINT(build/header_guard) +#define TFCOMPILE_GENERATED_{{ENTRY}}_H_ // NOLINT(build/header_guard) + +{{INCLUDE_XLA_DATA_PROTO}} +{{RUNTIME_SPECIFIC_INCLUDES}} +#include "tensorflow/compiler/tf2xla/xla_compiled_cpu_function.h" +#include "tensorflow/core/platform/types.h" + +namespace Eigen { struct ThreadPoolDevice; } +namespace xla { class ExecutableRunOptions; } + +{{FUNCTION_DECLARATIONS_FROM_OBJ_FILES}} + +{{DECLS_FROM_OBJ_FILE}} + +{{NS_START}} +// {{CLASS}} represents a computation previously specified in a +// TensorFlow graph, now compiled into executable code. This extends the generic +// XlaCompiledCpuFunction class with statically type-safe arg and result +// methods. Usage example: +// +// {{CLASS}} computation; +// // ...set args using computation.argN methods +// CHECK(computation.Run()); +// // ...inspect results using computation.resultN methods +// +// The Run method invokes the actual computation, with inputs read from arg +// buffers, and outputs written to result buffers. Each Run call may also use +// a set of temporary buffers for the computation. +// +// By default each instance of this class manages its own arg, result and temp +// buffers. The AllocMode constructor parameter may be used to modify the +// buffer allocation strategy. +// +// Under the default allocation strategy, this class is thread-compatible: +// o Calls to non-const methods require exclusive access to the object. +// o Concurrent calls to const methods are OK, if those calls are made while it +// is guaranteed that no thread may call a non-const method. +// +// The logical function signature is: +// {{PROGRAM_SHAPE}} +// +// Memory stats: +// arg bytes total: {{ARG_BYTES_TOTAL}} +// arg bytes aligned: {{ARG_BYTES_ALIGNED}} +// temp bytes total: {{TEMP_BYTES_TOTAL}} +// temp bytes aligned: {{TEMP_BYTES_ALIGNED}} +class {{CLASS}} final : public tensorflow::{{COMPUTATION_CLASS_BASE}} { + public: + // Number of input arguments for the compiled computation. + static constexpr size_t kNumArgs = {{ARG_NUM}}; + + static constexpr size_t kNumResults = {{RESULT_NUM}}; + + // Number of variables for the compiled computation. + static constexpr size_t kNumVariables = {{VARIABLE_NUM}}; + + // Byte size of each argument buffer. There are kNumArgs entries. + static const ::int64_t ArgSize(::tensorflow::int32 index) { + return BufferAllocationInfos()[ArgIndexToBufferIndex()[index]].size(); + } + + // Returns static data used to create an XlaCompiledCpuFunction. + static const tensorflow::XlaCompiledCpuFunction::StaticData& StaticData() { + static XlaCompiledCpuFunction::StaticData* kStaticData = [](){ + XlaCompiledCpuFunction::StaticData* data = + new XlaCompiledCpuFunction::StaticData; + set_static_data_function_library_symbol_map(data, FunctionLibrarySymbolMap()); + set_static_data_buffer_infos(data, BufferAllocationInfos()); + set_static_data_num_buffers(data, kNumBuffers); + set_static_data_result_index_table(data, ResultIndexToBufferIndex()); + set_static_data_num_results(data, kNumResults); + set_static_data_arg_index_table(data, ArgIndexToBufferIndex()); + set_static_data_num_args(data, kNumArgs); + set_static_data_num_variables(data, kNumVariables); + set_static_data_temp_allocation_index(data, kTempAllocationIndex); + set_static_data_arg_shape_infos(data, ArgShapeInfos()); + set_static_data_result_shape_infos(data, ResultShapeInfos()); + set_static_data_arg_names(data, StaticArgNames()); + set_static_data_variable_names(data, StaticVariableNames()); + set_static_data_result_names(data, StaticResultNames()); + set_static_data_program_shape(data, StaticProgramShape()); + {{THUNK_SPECIFIC_STATIC_DATA_SETTERS}} + {{NANORT_SPECIFIC_STATIC_DATA_SETTERS}} + return data; + }(); + return *kStaticData; + } + + {{CLASS}}(AllocMode alloc_mode = + AllocMode::ARGS_VARIABLES_RESULTS_PROFILES_AND_TEMPS) + : {{COMPUTATION_CLASS_BASE}}(StaticData(), alloc_mode) {} + + {{CLASS}}(const {{CLASS}}&) = delete; + {{CLASS}}& operator=(const {{CLASS}}&) = delete; + + // Arg methods for managing input buffers. Buffers are in row-major order. + // There is a set of methods for each positional argument, with the following + // general form: + // + // void set_argN_data(void* data) + // Sets the buffer of type T for positional argument N. May be called in + // any AllocMode. Must be called before Run to have an effect. Must be + // called in AllocMode::RESULTS_PROFILES_AND_TEMPS_ONLY for each positional + // argument, to set the argument buffers. + // + // T* argN_data() + // Returns the buffer of type T for positional argument N. + // + // T& argN(...dim indices...) + // Returns a reference to the value of type T for positional argument N, + // with dim indices specifying which value. No bounds checking is performed + // on dim indices. +{{METHODS_ARG}} + + // Result methods for managing output buffers. Buffers are in row-major order. + // Must only be called after a successful Run call. There is a set of methods + // for each positional result, with the following general form: + // + // T* resultN_data() + // Returns the buffer of type T for positional result N. + // + // T& resultN(...dim indices...) + // Returns a reference to the value of type T for positional result N, + // with dim indices specifying which value. No bounds checking is performed + // on dim indices. + // + // Unlike the arg methods, there is no set_resultN_data method. The result + // buffers are managed internally, and may change after each call to Run. +{{METHODS_RESULT}} + + // Methods for managing variable buffers. Buffers are in row-major order. + // + // For read-write variables we generate the following methods: + // + // void set_var_X_data(T* data) + // Sets the buffer for variable X. Must be called before Run if the + // allocation mode is RESULTS_PROFILES_AND_TEMPS_ONLY. + // + // T* var_X_data() + // Returns the buffer of type T for variable X. If the allocation mode is + // RESULTS_PROFILES_AND_TEMPS_ONLY then this buffer is the same as the + // buffer passed to set_var_X_data. + // + // T& var_X(...dim indices...) + // Returns a reference to the value of type T for variable X, + // with dim indices specifying which value. No bounds checking is performed + // on dim indices. + // + // For readonly variables we generate the same set of methods, except that we + // use `const T` instead of `T`. We use `const T` to avoid erasing the + // constness of the buffer passed to `set_var_X_data` but the underlying + // buffer is not const (and thus the const can be safely const-cast'ed away) + // unless `set_var_X_data` is called with a pointer to constant storage. +{{METHODS_VARIABLE}} + + private: + // Number of buffers for the compiled computation. + static constexpr size_t kNumBuffers = {{NUM_BUFFERS}}; + + static const ::xla::cpu::BufferAllocationInfo* BufferAllocationInfos() { + static const ::xla::cpu::BufferAllocationInfo + kBufferAllocationInfos[kNumBuffers] = { +{{BUFFER_INFOS_AS_STRING}} + }; + return kBufferAllocationInfos; + } + + static const ::tensorflow::int32* ResultIndexToBufferIndex() { + static constexpr ::tensorflow::int32 kResultIndexToBufferIndex[kNumResults] = { +{{RESULT_INDEX_TABLE}} + }; + return kResultIndexToBufferIndex; + } + + static const ::tensorflow::int32* ArgIndexToBufferIndex() { + static constexpr ::tensorflow::int32 kArgIndexToBufferIndex[kNumArgs] = { +{{ARG_INDEX_TABLE}} + }; + return kArgIndexToBufferIndex; + } + + // Temp allocation index.. + static constexpr std::optional kTempAllocationIndex = {{TEMP_ALLOCATION_INDEX}}; + + // Shapes of the input arguments. +{{ARG_SHAPE_INFOS}}; + + // Shapes of the results. +{{RESULT_SHAPE_INFOS}}; + + // Array of names of each positional argument, terminated by nullptr. + static const char** StaticArgNames() {{ARG_NAMES_CODE}} + + // Array of names of each positional variable, terminated by nullptr. + static const char** StaticVariableNames() {{VARIABLE_NAMES_CODE}} + + // Array of names of each positional result, terminated by nullptr. + static const char** StaticResultNames() {{RESULT_NAMES_CODE}} + + // Shape of the args and results. + static const ::xla::ProgramShapeProto* StaticProgramShape() { + static const ::xla::ProgramShapeProto* kShape = {{PROGRAM_SHAPE_SHIM_EXPRESSION}}; + return kShape; + } + + {{EXECUTABLE_PROTO_GETTER}} + {{THUNK_RUN_IMPL_GETTER}} + {{EMBEDDED_CONSTANT_BUFFERS_INITIALIZER_GETTER}} + {{RNG_DELTAS_INITIALIZER_GETTER}} + + static absl::flat_hash_map FunctionLibrarySymbolMap() { + return {{SYMBOL_MAP_INITIALIZER}}; + } + protected: + bool is_thunk_mode() const override { return {{IS_THUNK_MODE}}; } +}; +{{NS_END}} + +#endif // TFCOMPILE_GENERATED_{{ENTRY}}_H_ + +// clang-format on +)"; + + std::vector decls_from_obj_file = + metadata_result.header_variable_decls; + for (const auto& constant_buffer_var_info : + embedded_constant_buffers.variable_decls) { + decls_from_obj_file.push_back(constant_buffer_var_info.variable_decl); + } + + // The replacement strategy is naive, but good enough for our purposes. + std::vector> rewrites = { + {"{{ARG_BYTES_ALIGNED}}", absl::StrCat(arg_bytes_aligned)}, + {"{{ARG_BYTES_TOTAL}}", absl::StrCat(arg_bytes_total)}, + {"{{ARG_NAMES_CODE}}", arg_names_code}, + {"{{ARG_NUM}}", absl::StrCat(arg_index_table.size())}, + {"{{ARG_SHAPE_INFOS}}", arg_shape_infos}, + {"{{VARIABLE_NUM}}", absl::StrCat(config.variable_size())}, + {"{{ARG_INDEX_TABLE}}", absl::StrJoin(arg_index_table, ", ")}, + {"{{RESULT_NUM}}", absl::StrCat(result_index_table.size())}, + {"{{RESULT_INDEX_TABLE}}", absl::StrJoin(result_index_table, ", ")}, + {"{{CLASS}}", opts.class_name}, + {"{{DECLS_FROM_OBJ_FILE}}", absl::StrJoin(decls_from_obj_file, "\n")}, + {"{{ENTRY}}", compile_result.entry_point}, + {"{{INCLUDE_XLA_DATA_PROTO}}", include_xla_data_proto}, + {"{{METHODS_ARG}}\n", methods_arg}, + {"{{METHODS_RESULT}}\n", methods_result}, + {"{{METHODS_VARIABLE}}\n", methods_variable}, + {"{{NS_END}}\n", ns_end}, + {"{{NS_START}}\n", ns_start}, + {"{{PROGRAM_SHAPE}}", xla::ShapeUtil::HumanString(program_shape)}, + {"{{PROGRAM_SHAPE_SHIM_EXPRESSION}}", + metadata_result.program_shape_access_shim}, + {"{{VARIABLE_NAMES_CODE}}", variable_names_code}, + {"{{RESULT_NAMES_CODE}}", result_names_code}, + {"{{RESULT_SHAPE_INFOS}}", result_shape_infos}, + {"{{TEMP_BYTES_ALIGNED}}", absl::StrCat(temp_bytes_aligned)}, + {"{{TEMP_BYTES_TOTAL}}", absl::StrCat(temp_bytes_total)}, + {"{{NUM_BUFFERS}}", absl::StrCat(buffer_infos.size())}, + {"{{BUFFER_INFOS_AS_STRING}}", + absl::StrJoin(buffer_infos_as_strings, ",\n")}, + }; + + TF_RETURN_IF_ERROR(ExtendRewrites(rewrites, compile_result.aot.get(), + metadata_result, opts, + embedded_constant_buffers)); + absl::StrReplaceAll(rewrites, header); + return absl::OkStatus(); +} + +static std::string CreateUniqueIdentifier(const CodegenOpts& opts, + absl::string_view suffix) { + std::string result = "__tfcompile"; + for (const std::string& n : opts.namespaces) { + absl::StrAppend(&result, "_", n); + } + + absl::StrAppend(&result, "_", opts.class_name, "_", suffix); + return result; +} + +absl::StatusOr GenerateConstantBuffersData( + const CodegenOpts& opts, const CompileResult& compile_result) { + auto aot_thunk_result = compile_result.aot.get(); + + // Create a temporary object for aot_thunk_result to be able to call + // LoadExecutable without moving the original object. + TF_ASSIGN_OR_RETURN(auto serialized, aot_thunk_result->SerializeAsString()); + TF_ASSIGN_OR_RETURN( + auto aot_thunk_result_temp, + xla::cpu::CpuAotCompilationResult::FromString(serialized, nullptr)); + + TF_ASSIGN_OR_RETURN(auto executable, + std::move(*aot_thunk_result_temp).LoadExecutable()); + + xla::cpu::CpuExecutable* cpu_executable = + absl::down_cast(executable.get()); + + std::vector constants_to_embed; + + int constant_identifier = 0; + for (const auto& constant : cpu_executable->constants()) { + const uint8_t* constant_data_bytes_ptr = + reinterpret_cast(constant.AsDeviceAddress().opaque()); + const size_t constant_size = constant.AsDeviceAddress().size(); + + // NOTE(basioli): Some constants are empty, we don't need to embed them + if (constant_size == 0) { + continue; + } + + xla::ConstantToEmbed constant_to_embed; + constant_to_embed.SerializeIntoBuffer( + absl::MakeSpan(constant_data_bytes_ptr, constant_size)); + + constant_to_embed.symbol_prefix = CreateUniqueIdentifier( + opts, absl::StrCat("Constant_", std::to_string(constant_identifier++))); + + constants_to_embed.push_back(std::move(constant_to_embed)); + } + + return xla::CreateEmbeddedConstantBuffers(opts.target_triple, + absl::MakeSpan(constants_to_embed)); +} + +absl::Status GenerateMetadata(const CodegenOpts& opts, + const CompileResult& compile_result, + MetadataResult* metadata_result) { + std::unique_ptr program_shape; + + if (opts.gen_program_shape) { + program_shape = + std::make_unique(compile_result.program_shape); + + // The parameter names are currently meaningless, and redundant with the + // rest of our metadata, so clear them out to avoid confusion and save + // space. + program_shape->clear_parameter_names(); + } + + // When asked to serialize a null protobuf, CreateEmbeddedProtocolBuffer + // gives a shim that evaluates to nullptr, which is what we want. + std::vector protobufs_to_embed; + protobufs_to_embed.push_back( + ProtobufToEmbed{CreateUniqueIdentifier(opts, "ProgramShapeProto"), + "::xla::ProgramShapeProto", program_shape.get()}); + + protobufs_to_embed.push_back( + ProtobufToEmbed{CreateUniqueIdentifier(opts, "HloProfilePrinterData"), + "::xla::HloProfilePrinterData", nullptr}); + protobufs_to_embed.push_back(ProtobufToEmbed{ + CreateUniqueIdentifier(opts, "CompilationResultProto"), + "::xla::cpu::CompilationResultProto", &compile_result.aot->proto()}); + + TF_ASSIGN_OR_RETURN( + EmbeddedProtocolBuffers embedded_protobufs, + CreateEmbeddedProtocolBuffers(opts.target_triple, protobufs_to_embed)); + + metadata_result->program_shape_access_shim = + std::move(embedded_protobufs.cpp_shims[0].expression); + metadata_result->header_variable_decls.emplace_back( + std::move(embedded_protobufs.cpp_shims[0].variable_decl)); + + metadata_result->hlo_profile_printer_data_access_shim = + std::move(embedded_protobufs.cpp_shims[1].expression); + metadata_result->header_variable_decls.emplace_back( + std::move(embedded_protobufs.cpp_shims[1].variable_decl)); + + metadata_result->cpu_executable_access_shim = + std::move(embedded_protobufs.cpp_shims[2].expression); + metadata_result->header_variable_decls.emplace_back( + std::move(embedded_protobufs.cpp_shims[2].variable_decl)); + + metadata_result->object_file_data = + std::move(embedded_protobufs.object_file_data); + return absl::OkStatus(); +} + +absl::Status ParseCppClass(const std::string& cpp_class, + std::string* class_name, + std::vector* namespaces) { + class_name->clear(); + namespaces->clear(); + if (cpp_class.empty()) { + return absl::InvalidArgumentError("empty cpp_class: " + cpp_class); + } + std::vector parts = absl::StrSplit(cpp_class, "::"); + if (parts.front().empty()) { + // Allow a fully qualified name that starts with "::". + parts.erase(parts.begin()); + } + for (int i = 0, end = parts.size(); i < end; ++i) { + if (i < end - 1) { + TF_RETURN_IF_ERROR(ValidateCppIdent( + parts[i], "in namespace component of cpp_class: " + cpp_class)); + namespaces->push_back(parts[i]); + } else { + TF_RETURN_IF_ERROR(ValidateCppIdent( + parts[i], "in class name of cpp_class: " + cpp_class)); + *class_name = parts[i]; + } + } + return absl::OkStatus(); +} + +absl::Status ValidateCppIdent(absl::string_view ident, absl::string_view msg) { + if (ident.empty()) { + return absl::InvalidArgumentError(absl::StrCat("empty identifier: ", msg)); + } + // Require that the identifier starts with a nondigit, and is composed of + // nondigits and digits, as specified in section [2.11 Identifiers] of the + // C++11 Standard. Note that nondigit is defined as [_a-zA-Z] and digit is + // defined as [0-9]. + // + // Technically the standard also allows for `universal-character-name`, with + // a table of allowed unicode ranges, as well as `other + // implementation-defined characters`. We disallow those here to give + // better error messages, at the expensive of being more restrictive than + // the standard. + if (ident[0] != '_' && !absl::ascii_isalpha(ident[0])) { + return absl::InvalidArgumentError( + absl::StrCat("illegal leading char: ", msg)); + } + for (size_t pos = 1; pos < ident.size(); ++pos) { + if (ident[pos] != '_' && !absl::ascii_isalnum(ident[pos])) { + return absl::InvalidArgumentError(absl::StrCat("illegal char: ", msg)); + } + } + return absl::OkStatus(); +} + +} // namespace tfcompile +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/codegen.h b/western_ai/usa/tensorflow/tensorflow/compiler/aot/codegen.h new file mode 100644 index 0000000000000000000000000000000000000000..650ccb48f64999de371af44be424117a094db64f --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/codegen.h @@ -0,0 +1,126 @@ +/* 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_AOT_CODEGEN_H_ +#define TENSORFLOW_COMPILER_AOT_CODEGEN_H_ +#include + +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" +#include "tensorflow/compiler/aot/compile.h" +#include "tensorflow/compiler/tf2xla/tf2xla.pb.h" +#include "xla/util/embedded_constant_buffers.h" +#include "tensorflow/core/platform/types.h" + +namespace tensorflow { +namespace tfcompile { + +// CodegenOpts specifies code generation options for the generated header file +// and the generated metadata object file. +struct CodegenOpts { + // The name of the generated C++ class, wrapping the generated function. + std::string class_name; + + // Target triple for the architecture we're targeting. + std::string target_triple; + + // Namespaces specifies a list of C++ namespaces to add to the generated + // header. If empty, all symbols will be in the global namespace. + std::vector namespaces; + + // If true, generate name-to-index data for Lookup{Arg,Result}Index methods. + bool gen_name_to_index = false; + + // If true, generate program shape data for the ProgramShape method. + bool gen_program_shape = false; + + // If true, emit a serialized HloProfilePrinterData protobuf that can be used + // to pretty print HLO profile counters. + bool gen_hlo_profile_printer_data = false; + + // If true, sets this executable as an XLA Runtime one. + bool use_xla_runtime = false; + + // If true, use xla cpu nanort runtime. + bool use_xla_nanort_runtime = false; +}; + +// Describes a generated metadata object file. +struct MetadataResult { + // These are top level "extern C" declarations that are expected to be visible + // wherever program_shape_access_shim is emitted. + std::vector header_variable_decls; + + // program_shape_access_shim is a C++ expression that constructs the + // xla::ProgramShapeProto instance for the CompileResult passed to + // GenerateMetadata. + std::string program_shape_access_shim; + + // hlo_profile_printer_data_access_shim is a C++ expression that constructs + // the xla::HloProfilePrinterData instance for the CompileResult passed to + // GenerateMetadata. If the xla::HloProfilePrinterData is null then this is a + // C++ expression that evaluates to nullptr at runtime. + // This is set only for AOT legacy. + std::string hlo_profile_printer_data_access_shim; + + // cpu_executable_access_shim is a C++ expression that constructs + // a protobuf required to construct a CpuExecutable. + // This is set only for AOT thunks. + std::string cpu_executable_access_shim; + + // The contents of the object (".o") file. + std::string object_file_data; +}; + +// Generates a set of constant buffers embedded into an object file. +absl::StatusOr GenerateConstantBuffersData( + const CodegenOpts& opts, const CompileResult& compile_result); + +// Generates a metadata object file according to `opts` and +// `compile_result`. The generated object file is returned via +// `metadata_result`. +absl::Status GenerateMetadata(const CodegenOpts& opts, + const CompileResult& compile_result, + MetadataResult* metadata_result); + +// GenerateHeader uses the meta-information from compile_result to generate a +// C++ header giving access to the function in the generated object file. The +// header includes API usage documentation. +// +// metadata_result is an instance of MetadataResult obtained by a previous +// invocation to GenerateMetadata. +absl::Status GenerateHeader( + const CodegenOpts& opts, const tf2xla::Config& config, + const CompileResult& compile_result, const MetadataResult& metadata_result, + const xla::EmbeddedConstantBuffers& embedded_constant_buffers, + std::string* header); + +// ParseCppClass parses `cpp_class` into its `class_name` and `namespaces` +// components. The syntax is [[::],...]. This +// mirrors the C++ syntax for referring to a class, where multiple namespaces +// may precede the class name, separated by double-colons. +absl::Status ParseCppClass(const std::string& cpp_class, + std::string* class_name, + std::vector* namespaces); + +// ValidateCppIdent returns OK iff ident is a valid C++ identifier. The msg is +// appended to error messages. +absl::Status ValidateCppIdent(absl::string_view ident, absl::string_view msg); + +} // namespace tfcompile +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_AOT_CODEGEN_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/codegen_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/aot/codegen_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..49709e5309e45bd3f4dfde91733efa9744b29a19 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/codegen_test.cc @@ -0,0 +1,184 @@ +/* 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/compiler/aot/codegen.h" + +#include +#include +#include + +#include "absl/memory/memory.h" +#include "absl/strings/match.h" +#include "absl/strings/string_view.h" +#include "llvm/Support/TargetSelect.h" +#include "tensorflow/compiler/aot/compile.h" +#include "xla/service/cpu/cpu_aot_compilation_result.h" +#include "xla/shape_util.h" +#include "tensorflow/core/framework/tensor_shape.pb.h" +#include "tensorflow/core/framework/types.pb.h" +#include "tensorflow/core/lib/core/status.h" +#include "tensorflow/core/lib/core/status_test_util.h" +#include "tensorflow/core/lib/io/path.h" +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/platform/resource_loader.h" +#include "tensorflow/core/platform/test.h" + +namespace tensorflow { +namespace tfcompile { +namespace { + +using ::xla::cpu::BufferAllocationInfo; + +void ExpectErrorContains(const absl::Status& status, absl::string_view str) { + EXPECT_NE(absl::OkStatus(), status); + EXPECT_TRUE(absl::StrContains(status.message(), str)) + << "expected error: " << status.message() << " to contain: " << str; +} + +TEST(ValidateCppIdent, Simple) { + TF_EXPECT_OK(ValidateCppIdent("a", "")); + TF_EXPECT_OK(ValidateCppIdent("abc", "")); + TF_EXPECT_OK(ValidateCppIdent("_abc", "")); + TF_EXPECT_OK(ValidateCppIdent("_abc123", "")); + // Make sure we didn't skip a valid letter or digit + std::string ident; + for (char c = 'a'; c <= 'z'; c++) { + ident.append(1, c); + } + for (char c = 'A'; c <= 'Z'; c++) { + ident.append(1, c); + } + for (char c = '0'; c <= '9'; c++) { + ident.append(1, c); + } + ident += "_"; + TF_EXPECT_OK(ValidateCppIdent(ident, "")); + + ExpectErrorContains(ValidateCppIdent("", ""), "empty identifier"); + ExpectErrorContains(ValidateCppIdent(" ", ""), "illegal leading char"); + ExpectErrorContains(ValidateCppIdent("0", ""), "illegal leading char"); + ExpectErrorContains(ValidateCppIdent(".", ""), "illegal leading char"); + ExpectErrorContains(ValidateCppIdent(":", ""), "illegal leading char"); + ExpectErrorContains(ValidateCppIdent("a.", ""), "illegal char"); + ExpectErrorContains(ValidateCppIdent("a:", ""), "illegal char"); + ExpectErrorContains(ValidateCppIdent("a:", ""), "illegal char"); +} + +class ParseCppClassTest : public ::testing::Test { + protected: + void ExpectOK(const std::string& cpp_class, + const std::string& want_class_name, + const std::vector& want_namespaces) { + std::string class_name; + std::vector namespaces; + TF_EXPECT_OK(ParseCppClass(cpp_class, &class_name, &namespaces)); + EXPECT_EQ(class_name, want_class_name); + EXPECT_EQ(namespaces, want_namespaces); + } + + void ExpectFail(const std::string& cpp_class) { + std::string class_name; + std::vector namespaces; + EXPECT_NE(ParseCppClass(cpp_class, &class_name, &namespaces), + absl::OkStatus()) + << cpp_class; + } +}; + +TEST_F(ParseCppClassTest, ParseOK) { + ExpectOK("MyClass", "MyClass", {}); + ExpectOK("_MyClass", "_MyClass", {}); + ExpectOK("a::MyClass", "MyClass", {"a"}); + ExpectOK("a::foo::MyClass", "MyClass", {"a", "foo"}); + ExpectOK("a::foo::b::MyClass", "MyClass", {"a", "foo", "b"}); + ExpectOK("a::foo::b::bar::MyClass", "MyClass", {"a", "foo", "b", "bar"}); + ExpectOK("foo::MyClass", "MyClass", {"foo"}); + ExpectOK("_foo::MyClass", "MyClass", {"_foo"}); + ExpectOK("_foo::_MyClass", "_MyClass", {"_foo"}); + ExpectOK("::foo::bar::MyClass", "MyClass", {"foo", "bar"}); + ExpectOK("::_foo::MyClass", "MyClass", {"_foo"}); + ExpectOK("::_foo::_MyClass", "_MyClass", {"_foo"}); + // Make sure we didn't skip a valid letter or digit + std::string ident; + for (char c = 'a'; c <= 'z'; c++) { + ident.append(1, c); + } + for (char c = 'A'; c <= 'Z'; c++) { + ident.append(1, c); + } + for (char c = '0'; c <= '9'; c++) { + ident.append(1, c); + } + ident += "_"; + ExpectOK(ident, ident, {}); + ExpectOK(ident + "::" + ident, ident, {ident}); + ExpectOK(ident + "::" + ident + "::" + ident, ident, {ident, ident}); +} + +TEST_F(ParseCppClassTest, ParseFail) { + ExpectFail(""); + ExpectFail("::"); + ExpectFail("0"); + ExpectFail("a.b"); + ExpectFail("a:b"); + ExpectFail(":foo::bar"); + ExpectFail("good::.bad"); + ExpectFail("good:::bad"); + ExpectFail("good::bad::"); + ExpectFail("good::::bad"); + ExpectFail("::::bad"); + ExpectFail("good:: bad"); + ExpectFail("good::0bad"); +} + +static void CompareWithGoldenFile( + const std::string& tensorflow_relative_golden_file_name, + const std::string& expected_contents, bool ignore_cr) { + // Get rid of all CR characters, we may be running under windows. + std::string sanitized_expected_contents(expected_contents); + if (ignore_cr) { + sanitized_expected_contents.erase( + std::remove(sanitized_expected_contents.begin(), + sanitized_expected_contents.end(), '\r'), + sanitized_expected_contents.end()); + } + + // To update the golden file, flip update_golden to true and run the + // following: + // blaz test --test_strategy=local \ + // "third_party/tensorflow/compiler/aot:codegen_test" + const bool update_golden = false; + std::string golden_file_name = + GetDataDependencyFilepath(tensorflow_relative_golden_file_name); + + if (update_golden) { + TF_EXPECT_OK( + WriteStringToFile(Env::Default(), golden_file_name, expected_contents)); + } + + std::string golden_file_contents; + TF_ASSERT_OK(ReadFileToString(Env::Default(), golden_file_name, + &golden_file_contents)); + if (ignore_cr) { + golden_file_contents.erase(std::remove(golden_file_contents.begin(), + golden_file_contents.end(), '\r'), + golden_file_contents.end()); + } + EXPECT_EQ(golden_file_contents, expected_contents); +} + +} // namespace +} // namespace tfcompile +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/compile.cc b/western_ai/usa/tensorflow/tensorflow/compiler/aot/compile.cc new file mode 100644 index 0000000000000000000000000000000000000000..f1d38aea907694427ca0a74b6531c4fee1710416 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/compile.cc @@ -0,0 +1,381 @@ +/* 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/compiler/aot/compile.h" + +#include +#include +#include +#include +#include +#include + +#include "absl/base/call_once.h" +#include "absl/log/check.h" +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/match.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/str_join.h" +#include "absl/strings/str_split.h" +#include "absl/strings/string_view.h" +#include "llvm-c/Target.h" +#include "llvm/Support/ManagedStatic.h" +#include "tensorflow/compiler/aot/codegen.h" +#include "tensorflow/compiler/aot/flags.h" +#include "tensorflow/compiler/aot/quantize.h" +#include "tensorflow/compiler/tf2xla/tf2xla.h" +#include "tensorflow/compiler/tf2xla/tf2xla_util.h" +#include "xla/backends/cpu/codegen/symbol_name_util.h" +#include "xla/client/client_library.h" +#include "xla/client/compile_only_client.h" +#include "xla/hlo/builder/xla_computation.h" +#include "xla/service/compiler.h" +#include "xla/service/cpu/cpu_aot_compilation_result.h" +#include "xla/shape.h" +#include "xla/status_macros.h" +#include "xla/stream_executor/platform.h" +#include "xla/stream_executor/platform_manager.h" +#include "xla/tsl/platform/errors.h" +#include "xla/tsl/platform/statusor.h" +#include "xla/util.h" +#include "xla/util/embedded_constant_buffers.h" +#include "xla/xla_data.pb.h" +#include "tensorflow/core/framework/graph.pb.h" +#include "tensorflow/core/lib/core/errors.h" +#include "tensorflow/core/lib/strings/proto_serialization.h" +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/platform/logging.h" +#include "tensorflow/core/platform/regexp.h" // IWYU pragma: keep +#include "tensorflow/core/platform/types.h" + +namespace tensorflow { +namespace tfcompile { + +static llvm::ManagedStatic quantize_xla; + +bool RegisterQuantizeFn(const QuantizeXlaFn& fn) { + if (*quantize_xla) return false; + *quantize_xla = fn; + return true; +} + +namespace { + +// Compiles the XLA computation into executable code. +absl::Status CompileXla(xla::CompileOnlyClient* client, + const xla::XlaComputation& computation, + const xla::cpu::CpuAotCompilationOptions& aot_opts, + CompileResult* compile_result) { + // Retrieves arg and result layouts from the computation. + // TODO(toddw): Should we let the user choose the major/minor ordering? + absl::StatusOr> pshape_or = + client->GetComputationShape(computation); + if (!pshape_or.ok()) { + return absl::UnknownError(absl::StrCat("Couldn't get XLA program shape: ", + pshape_or.status().message())); + } + compile_result->program_shape = pshape_or.value()->ToProto(); + xla::ProgramShapeProto* pshape = &compile_result->program_shape; + + // AotXlaComputationInstance::argument_layouts is a vector of Shape + // pointers. Accumulate the Shape objects themselves in a separate vector + // while building the vector of pointers. + std::vector arg_layout_ptrs(pshape->parameters_size()); + std::vector arg_layouts(pshape->parameters_size()); + for (int i = 0; i < pshape->parameters_size(); ++i) { + TF_ASSIGN_OR_RETURN(arg_layouts[i], + xla::Shape::FromProto(pshape->parameters(i))); + arg_layout_ptrs[i] = &arg_layouts[i]; + } + xla::CompileOnlyClient::AotXlaComputationInstance instance; + instance.computation = &computation; + instance.argument_layouts = std::move(arg_layout_ptrs); + TF_ASSIGN_OR_RETURN(xla::Shape result_shape, + xla::Shape::FromProto(pshape->result())); + instance.result_layout = &result_shape; + absl::StatusOr>> aot_or = + client->CompileAheadOfTime(instance, aot_opts); + if (!aot_or.ok()) { + return absl::UnknownError( + absl::StrCat("XLA compilation failed: ", aot_or.status().message())); + } + compile_result->aot = + xla::unique_ptr_down_cast( + std::move(aot_or.value().back())); + compile_result->entry_point = aot_opts.entry_point_name(); + compile_result->pointer_size = + xla::CompileOnlyClient::PointerSizeForTriple(aot_opts.triple()); + return absl::OkStatus(); +} + +// Renames the computation proto to ensure unique symbol names to avoid linking +// errors when linking multiple tf_library targets. +absl::Status ConfigureKernelNamingConvention( + xla::cpu::CpuAotCompilationOptions& aot_opts, + xla::XlaComputation& computation, const std::string& cpp_class) { + aot_opts.mutable_debug_options() + ->set_xla_cpu_generate_unique_c_style_kernel_entry_points(true); + + TF_ASSIGN_OR_RETURN(std::string class_name_as_valid_c_name, + xla::cpu::ConvertToCName(cpp_class)); + + // Prefix the computation name. We use this to blacklist the generated symbols + // from dfsan instrumentation. + constexpr absl::string_view kModuleNameGeneratorPrefix = + "tfcompile_xla_generated"; + // Rename proto to ensure unique symbol names. + *computation.mutable_proto()->mutable_name() = + absl::StrCat(kModuleNameGeneratorPrefix, "_", computation.proto().name(), + "_", class_name_as_valid_c_name); + + return absl::OkStatus(); +} + +} // namespace + +absl::Status CompileGraph(GraphDef graph_def, const tf2xla::Config& config, + const MainFlags& flags, + CompileResult* compile_result) { + // Converts the graph into an XLA computation, and compiles the + // computation. + // TODO(toddw): Should we let the user pick the XLA cpu vs. gpu client? + se::Platform* cpu_platform = + se::PlatformManager::PlatformWithName("Host").value(); + xla::CompileOnlyClient* client = + xla::ClientLibrary::GetOrCreateCompileOnlyClient(cpu_platform).value(); + xla::XlaComputation computation; + + bool use_mlir_bridge = false; + if (!flags.mlir_components.empty() && flags.mlir_components != "None") { + for (auto component : absl::StrSplit(flags.mlir_components, ',')) { + if (component == "Bridge") { + use_mlir_bridge = true; + } else { + return absl::UnknownError( + absl::StrCat("Unknown mlir_component ", component)); + } + } + } + if (use_mlir_bridge) { + TF_RETURN_IF_ERROR(ConvertGraphDefToXlaViaMlir( + graph_def, config, &computation, flags.debug_info, + flags.debug_info_path_begin_marker)); + } else { + TF_RETURN_IF_ERROR(ConvertGraphDefToXla(std::move(graph_def), config, + client, &computation)); + } + + if (flags.experimental_quantize && *quantize_xla) { + TF_RETURN_IF_ERROR((*quantize_xla)(config, &computation)); + } + + if (!flags.out_session_module.empty()) { + TF_ASSIGN_OR_RETURN(std::unique_ptr module, + computation.Snapshot()); + // Serialize the HloSnapshot deterministically so that all the outputs of a + // tf_library genrule are deterministic. + const size_t size = module->ByteSizeLong(); + auto serialized = std::make_unique(size); + TF_RET_CHECK( + SerializeToBufferDeterministic(*module, serialized.get(), size)); + TF_RETURN_IF_ERROR( + WriteStringToFile(Env::Default(), flags.out_session_module, + absl::string_view(serialized.get(), size))); + } + xla::cpu::CpuAotCompilationOptions aot_opts( + flags.target_triple, flags.target_cpu, flags.target_features, + flags.entry_point, + xla::cpu::CpuAotCompilationOptions::RelocationModel::BigPic, + /*compile_copy_as_llvm_kernel=*/true); + + if (flags.sanitize_dataflow) { + aot_opts.set_sanitize_dataflow(flags.sanitize_dataflow); + aot_opts.set_sanitize_abilists_dataflow(absl::StrSplit( + flags.sanitize_abilists_dataflow, ',', absl::SkipEmpty())); + } + + TF_RETURN_IF_ERROR( + ConfigureKernelNamingConvention(aot_opts, computation, flags.cpp_class)); + + return CompileXla(client, computation, aot_opts, compile_result); +} + +static absl::Status ReadProtoFile(const std::string& fname, + protobuf::Message* proto) { + if (absl::EndsWith(fname, ".pbtxt")) { + return ReadTextProto(Env::Default(), fname, proto); + } else { + return ReadBinaryProto(Env::Default(), fname, proto); + } +} + +static absl::once_flag targets_init; + +static void InitializeTargets() { + // Initialize all LLVM targets so we can cross compile. +#if TF_LLVM_AARCH32_AVAILABLE + LLVMInitializeARMTarget(); + LLVMInitializeARMTargetInfo(); + LLVMInitializeARMTargetMC(); + LLVMInitializeARMAsmParser(); + LLVMInitializeARMAsmPrinter(); +#endif +#if TF_LLVM_AARCH64_AVAILABLE + LLVMInitializeAArch64Target(); + LLVMInitializeAArch64TargetInfo(); + LLVMInitializeAArch64TargetMC(); + LLVMInitializeAArch64AsmParser(); + LLVMInitializeAArch64AsmPrinter(); +#endif +#if TF_LLVM_HEXAGON_AVAILABLE + LLVMInitializeHexagonTarget(); + LLVMInitializeHexagonTargetInfo(); + LLVMInitializeHexagonTargetMC(); + LLVMInitializeHexagonAsmParser(); + LLVMInitializeHexagonAsmPrinter(); +#endif +#if TF_LLVM_POWERPC_AVAILABLE + LLVMInitializePowerPCTarget(); + LLVMInitializePowerPCTargetInfo(); + LLVMInitializePowerPCTargetMC(); + LLVMInitializePowerPCAsmParser(); + LLVMInitializePowerPCAsmPrinter(); +#endif +#if TF_LLVM_S390X_AVAILABLE + LLVMInitializeSystemZTarget(); + LLVMInitializeSystemZTargetInfo(); + LLVMInitializeSystemZTargetMC(); + LLVMInitializeSystemZAsmParser(); + LLVMInitializeSystemZAsmPrinter(); +#endif +#if TF_LLVM_X86_AVAILABLE + LLVMInitializeX86Target(); + LLVMInitializeX86TargetInfo(); + LLVMInitializeX86TargetMC(); + LLVMInitializeX86AsmParser(); + LLVMInitializeX86AsmPrinter(); +#endif +} + +// Replaces {{tag.type tag.name}} in the error message with tag_name. +// TODO(bixia): We currently only handlge tag.type == "node". +// +// In the error message, a graph node is represented as {{tag.type, tag.name}}, +// to allow a Python debugger to insert source information about the graph node. +// For example, a Python add expression may be represented as +// {{node, x_y_sum}} = Add(x, y) in the error message. See routine interpolate +// in tensorflow/python/framework/error_interpolation.py for more detail. +static std::string InterpolateErrorMessage(std::string message) { + // See _NAME_REGEX in tensorflow/python/framework/error_interpolation.py + // Change "prefix {{node tag.name}} suffix" to "prefix tag.name suffix". + static LazyRE2 pattern{"(.*){{node (.*)}}(.*)"}; + RE2::GlobalReplace(&message, *pattern, "\\1\\2\\3"); + + return message; +} + +absl::Status Main(const MainFlags& flags) { + absl::call_once(targets_init, &InitializeTargets); + + // Process config. + tf2xla::Config config; + if (flags.config.empty()) { + return absl::InvalidArgumentError("Must specify --config"); + } + TF_RETURN_IF_ERROR(ReadProtoFile(flags.config, &config)); + TF_RETURN_IF_ERROR(ValidateConfig(config)); + if (flags.dump_fetch_nodes) { + std::set nodes; + for (const tf2xla::Fetch& fetch : config.fetch()) { + nodes.insert(fetch.id().node_name()); + } + std::cout << absl::StrJoin(nodes, ","); + return absl::OkStatus(); + } + + // Read and initialize the graph. + if (flags.graph.empty()) { + return absl::InvalidArgumentError("Must specify --graph"); + } + GraphDef graph_def; + TF_RETURN_IF_ERROR(ReadProtoFile(flags.graph, &graph_def)); + CompileResult compile_result; + + absl::Status status = + CompileGraph(std::move(graph_def), config, flags, &compile_result); + if (!status.ok()) { + return errors::CreateWithUpdatedMessage( + status, InterpolateErrorMessage(std::string(status.message()))); + } + + // Write output files. + Env* env = Env::Default(); + + const auto obj_files = compile_result.aot->obj_files(); + DCHECK_EQ(obj_files.size(), 1); + const absl::string_view obj_file = obj_files[0]; + TF_RETURN_IF_ERROR( + WriteStringToFile(env, flags.out_function_object, obj_file)); + + CodegenOpts codegen_opts; + codegen_opts.gen_name_to_index = flags.gen_name_to_index; + codegen_opts.gen_program_shape = flags.gen_program_shape; + codegen_opts.target_triple = flags.target_triple; + codegen_opts.use_xla_nanort_runtime = flags.use_xla_nanort_runtime; + // Set the XLA Runtime bit if this is an HloLowering. + if (!flags.mlir_components.empty() && flags.mlir_components != "None") { + for (auto component : absl::StrSplit(flags.mlir_components, ',')) { + if (component == "HloLowering") { + codegen_opts.use_xla_runtime = true; + } + } + } + + if (flags.cpp_class.empty()) { + return absl::InvalidArgumentError("Must specify --cpp_class"); + } + codegen_opts.gen_hlo_profile_printer_data = + xla::GetDebugOptionsFromFlags().xla_hlo_profile(); + TF_RETURN_IF_ERROR(ParseCppClass(flags.cpp_class, &codegen_opts.class_name, + &codegen_opts.namespaces)); + + xla::EmbeddedConstantBuffers embedded_constant_buffers; + if (flags.out_constant_buffers_object.empty()) { + return absl::InvalidArgumentError( + "Must specify --out_constant_buffers_object when using AOT thunks"); + } + TF_ASSIGN_OR_RETURN( + embedded_constant_buffers, + GenerateConstantBuffersData(codegen_opts, compile_result)); + TF_RETURN_IF_ERROR( + WriteStringToFile(env, flags.out_constant_buffers_object, + embedded_constant_buffers.object_file_data)); + + MetadataResult metadata_result; + TF_RETURN_IF_ERROR( + GenerateMetadata(codegen_opts, compile_result, &metadata_result)); + TF_RETURN_IF_ERROR(WriteStringToFile(env, flags.out_metadata_object, + metadata_result.object_file_data)); + std::string header; + TF_RETURN_IF_ERROR(GenerateHeader(codegen_opts, config, compile_result, + metadata_result, embedded_constant_buffers, + &header)); + TF_RETURN_IF_ERROR(WriteStringToFile(env, flags.out_header, header)); + return absl::OkStatus(); +} + +} // namespace tfcompile +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/compile.h b/western_ai/usa/tensorflow/tensorflow/compiler/aot/compile.h new file mode 100644 index 0000000000000000000000000000000000000000..3f3188c632fde16999b30ca6999a1c5946718d64 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/compile.h @@ -0,0 +1,59 @@ +/* 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_AOT_COMPILE_H_ +#define TENSORFLOW_COMPILER_AOT_COMPILE_H_ + +#include +#include + +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "tensorflow/compiler/aot/flags.h" +#include "tensorflow/compiler/tf2xla/tf2xla.pb.h" +#include "xla/service/cpu/cpu_aot_compilation_result.h" +#include "xla/xla_data.pb.h" +#include "tensorflow/core/framework/graph.pb.h" +#include "tensorflow/core/platform/types.h" +#include "tsl/platform/casts.h" + +namespace tensorflow { +namespace tfcompile { + +// CompileResult describes the output of CompileGraph, where the object file +// data and meta-information is available in aot. +struct CompileResult { + // Contains object file and meta-info. + std::unique_ptr aot; + xla::ProgramShapeProto program_shape; // Static shape of args and results. + std::string entry_point; // Name of generated function. + int pointer_size = 0; // Size of a pointer in bytes. +}; + +// CompileGraph compiles the graph_def into an object file containing a function +// that performs the graph operations. +// +// The XLA compilation options are specified in the flags. +absl::Status CompileGraph(GraphDef graph_def, const tf2xla::Config& config, + const MainFlags& flags, + CompileResult* compile_result); + +// The full compilation method, for reuse in a library setting. +absl::Status Main(const MainFlags& flags); + +} // namespace tfcompile +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_AOT_COMPILE_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/embedded_protocol_buffers.cc b/western_ai/usa/tensorflow/tensorflow/compiler/aot/embedded_protocol_buffers.cc new file mode 100644 index 0000000000000000000000000000000000000000..d7ea56367e4a2f85ab5248b9304a425fdf8348ce --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/embedded_protocol_buffers.cc @@ -0,0 +1,159 @@ +/* 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/compiler/aot/embedded_protocol_buffers.h" + +#include +#include +#include + +#include "absl/log/check.h" +#include "absl/memory/memory.h" +#include "absl/strings/str_replace.h" +#include "llvm/IR/Constants.h" +#include "llvm/IR/GlobalVariable.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/LegacyPassManager.h" +#include "llvm/IR/Module.h" +#include "llvm/MC/TargetRegistry.h" +#include "llvm/Target/TargetMachine.h" +#include "llvm/Target/TargetOptions.h" +#include "llvm/TargetParser/Triple.h" +#include "xla/service/llvm_ir/llvm_type_conversion_util.h" +#include "xla/tsl/platform/statusor.h" +#include "xla/util.h" +#include "tensorflow/core/lib/strings/proto_serialization.h" + +namespace tensorflow { +namespace tfcompile { + +using xla::llvm_ir::AsStringRef; + +static void AddEmbeddedProtocolBufferToLlvmModule( + llvm::Module* module, const ::tensorflow::protobuf::MessageLite& proto, + absl::string_view unique_identifier, + std::string* protobuf_array_symbol_name, int64_t* protobuf_array_size) { + std::string protobuf_array_contents; + CHECK(::tensorflow::SerializeToStringDeterministic(proto, + &protobuf_array_contents)); + *protobuf_array_symbol_name = + absl::StrCat(unique_identifier, "_protobuf_array_contents"); + *protobuf_array_size = protobuf_array_contents.size(); + + llvm::Constant* protobuf_array_initializer = + llvm::ConstantDataArray::getString(module->getContext(), + AsStringRef(protobuf_array_contents), + /*AddNull=*/false); + new llvm::GlobalVariable( + *module, protobuf_array_initializer->getType(), + /*isConstant=*/true, llvm::GlobalValue::ExternalLinkage, + protobuf_array_initializer, AsStringRef(*protobuf_array_symbol_name)); +} + +static std::string CreateCPPShimExpression( + absl::string_view qualified_cpp_protobuf_name, + absl::string_view protobuf_array_symbol_name, int64_t protobuf_array_size) { + std::string code = + "[]() {\n" + " {{PROTOBUF_NAME}}* proto = new {{PROTOBUF_NAME}};\n" + " proto->ParseFromArray(&{{ARRAY_SYMBOL}}[0], {{ARRAY_SIZE}});\n" + " return proto;\n" + " }()"; + + return absl::StrReplaceAll( + code, + { + {"{{ARRAY_SYMBOL}}", absl::StrCat(protobuf_array_symbol_name)}, + {"{{ARRAY_SIZE}}", absl::StrCat(protobuf_array_size)}, + {"{{PROTOBUF_NAME}}", absl::StrCat(qualified_cpp_protobuf_name)}, + }); +} + +static absl::StatusOr CodegenModule( + llvm::TargetMachine* target_machine, std::unique_ptr module) { + llvm::SmallVector stream_buffer; + llvm::raw_svector_ostream ostream(stream_buffer); + llvm::legacy::PassManager codegen_passes; + + if (target_machine->addPassesToEmitFile(codegen_passes, ostream, nullptr, + llvm::CodeGenFileType::ObjectFile)) { + return xla::Internal( + "Could not create pass pipeline to generate object file"); + } + + codegen_passes.run(*module); + + return std::string(stream_buffer.begin(), stream_buffer.end()); +} + +static absl::StatusOr> +GetTargetMachineFromTriple(absl::string_view target_triple) { + std::string error; + llvm::Triple normalized_triple( + llvm::Triple::normalize(AsStringRef(absl::string_view(target_triple)))); + const llvm::Target* target = + llvm::TargetRegistry::lookupTarget(normalized_triple, error); + if (target == nullptr) { + return xla::Internal("TargetRegistry::lookupTarget failed: %s", + error.c_str()); + } + + return absl::WrapUnique(target->createTargetMachine( + normalized_triple, /*CPU=*/"", + /*Features=*/"", llvm::TargetOptions(), std::nullopt)); +} + +absl::StatusOr CreateEmbeddedProtocolBuffers( + absl::string_view target_triple, + absl::Span protobufs_to_embed) { + TF_ASSIGN_OR_RETURN(std::unique_ptr target_machine, + GetTargetMachineFromTriple(target_triple)); + + llvm::LLVMContext llvm_context; + auto module_with_serialized_proto = + std::make_unique("embedded_data_module", llvm_context); + + EmbeddedProtocolBuffers result; + + for (const ProtobufToEmbed& protobuf_to_embed : protobufs_to_embed) { + std::string cpp_shim, cpp_variable_decl; + if (protobuf_to_embed.message) { + std::string protobuf_array_symbol_name; + int64_t protobuf_array_size; + + AddEmbeddedProtocolBufferToLlvmModule( + module_with_serialized_proto.get(), *protobuf_to_embed.message, + protobuf_to_embed.symbol_prefix, &protobuf_array_symbol_name, + &protobuf_array_size); + cpp_shim = CreateCPPShimExpression( + protobuf_to_embed.qualified_cpp_protobuf_name, + protobuf_array_symbol_name, protobuf_array_size); + + cpp_variable_decl = + absl::StrCat("extern \"C\" char ", protobuf_array_symbol_name, "[];"); + } else { + cpp_shim = "nullptr"; + } + result.cpp_shims.push_back({cpp_shim, cpp_variable_decl}); + } + + TF_ASSIGN_OR_RETURN(result.object_file_data, + CodegenModule(target_machine.get(), + std::move(module_with_serialized_proto))); + return result; +} + +} // namespace tfcompile +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/embedded_protocol_buffers.h b/western_ai/usa/tensorflow/tensorflow/compiler/aot/embedded_protocol_buffers.h new file mode 100644 index 0000000000000000000000000000000000000000..449ae49b59a5a50ba842ce5865d6209115e4ee28 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/embedded_protocol_buffers.h @@ -0,0 +1,92 @@ +/* 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. +==============================================================================*/ + +// This file defines utilities to help "embed" protocol buffers into object +// (".o") files. These C++ binaries and shared objects can link in these .o to +// get access to said protocol buffers at runtime. + +#ifndef TENSORFLOW_COMPILER_AOT_EMBEDDED_PROTOCOL_BUFFERS_H_ +#define TENSORFLOW_COMPILER_AOT_EMBEDDED_PROTOCOL_BUFFERS_H_ + +#include "absl/status/statusor.h" +#include "absl/types/span.h" +#include "tensorflow/core/platform/protobuf.h" + +namespace tensorflow { +namespace tfcompile { +using absl::StatusOr; + +// Represents a set of protocol buffers embedded into an object file and +// describes how to access them at runtime. +struct EmbeddedProtocolBuffers { + // Each instance CPPShim describes how to generate C++ code to instantiate a + // protobuf instance from the corresponding static data emitted into the + // object file. + struct CPPShim { + // `expression` is a C++ expression that creates an instance of said + // protocol buffer when executed. + std::string expression; + + // `variable_decl` is an "extern C" array declaration that is used in + // `expression`. It must be visible wherever `expression` is emitted. + std::string variable_decl; + }; + + // Each cpp_shim corresponds to one embedded protocol buffer. + std::vector cpp_shims; + + // The contents of the object (".o") file the protocol buffers are embbed in. + // This needs to be linked in to any program that wants to execute any of the + // expressions in `cpp_shims`. + std::string object_file_data; +}; + +// Describes a protocol buffer to embed into an object file. +struct ProtobufToEmbed { + // `symbol_prefix` is prefix that is guaranteed to be unique across the binary + // or DSO the generated object file will be linked into. + std::string symbol_prefix; + + // `qualified_cpp_protobuf_name` is a qualified ("qualified" as in C++ + // namespace qualified) protocol buffer name. This is only used in + // CPPShim::expression so relatively qualified names are fine as long as + // they're valid wherever CPPShim::expression is emitted. + std::string qualified_cpp_protobuf_name; + + // `message` is the protocol buffer to be embedded. It is allowed to be + // nullptr, in which case the generated C++ shim expression is just `nullptr`, + // and the generated object file does not define any symbols. + const ::tensorflow::protobuf::MessageLite* message; +}; + +// Embeds a sequence of protocol buffers into an object file. +// +// `target_triple` is the target triple for the target architecture for the +// generated object file. +// +// `protobufs_to_embed` describes the protocol buffers to embed into the +// resulting object file. The C++ shim for protobufs_to_embed[i] is +// cpp_shims[i] in the returned EmbeddedProtocolBuffers instance. The contents +// of all the protocol buffers are embedded into a single .o file whose content +// is stored in the object_file_data field in the returned +// EmbeddedProtocolBuffers instance. +absl::StatusOr CreateEmbeddedProtocolBuffers( + absl::string_view target_triple, + absl::Span protobufs_to_embed); + +} // namespace tfcompile +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_AOT_EMBEDDED_PROTOCOL_BUFFERS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/flags.cc b/western_ai/usa/tensorflow/tensorflow/compiler/aot/flags.cc new file mode 100644 index 0000000000000000000000000000000000000000..e2ce0974be5d6bf40ae78825d5df046952fb05b3 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/flags.cc @@ -0,0 +1,101 @@ +/* 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/compiler/aot/flags.h" + +namespace tensorflow { +namespace tfcompile { + +void AppendMainFlags(std::vector* flag_list, MainFlags* flags) { + const std::vector tmp = { + {"graph", &flags->graph, + "Input GraphDef file. If the file ends in '.pbtxt' it is expected to " + "be in the human-readable proto text format, otherwise it is expected " + "to be in the proto binary format."}, + {"debug_info", &flags->debug_info, + "Graph debug info file. If the file ends in '.pbtxt' it is expected to " + "be in the human-readable proto text format, otherwise it is expected " + "to be in the proto binary format."}, + {"debug_info_path_begin_marker", &flags->debug_info_path_begin_marker, + "If not none, only keep the file path in the debug information after the" + " marker. The default value is empty"}, + {"config", &flags->config, + "Input file containing Config proto. If the file ends in '.pbtxt' it " + "is expected to be in the human-readable proto text format, otherwise " + "it is expected to be in the proto binary format."}, + {"dump_fetch_nodes", &flags->dump_fetch_nodes, + "If set, only flags related to fetches are processed, and the resulting " + "fetch nodes will be dumped to stdout in a comma-separated list. " + "Typically used to format arguments for other tools, e.g. " + "freeze_graph."}, + // Flags controlling the XLA ahead-of-time compilation, that correspond to + // the fields of xla::cpu::CpuAotCompilationOptions. + // + // TODO(toddw): The following flags also need to be supported: + // --xla_cpu_llvm_opt_level + // --xla_cpu_llvm_cl_opts + {"target_triple", &flags->target_triple, + "Target platform, similar to the clang -target flag. The general " + "format is ---. " + "http://clang.llvm.org/docs/CrossCompilation.html#target-triple."}, + {"target_cpu", &flags->target_cpu, + "Target cpu, similar to the clang -mcpu flag. " + "http://clang.llvm.org/docs/CrossCompilation.html#cpu-fpu-abi"}, + {"target_features", &flags->target_features, + "Target features, e.g. +avx2, +neon, etc."}, + {"entry_point", &flags->entry_point, + "Name of the generated function. If multiple generated object files " + "will be linked into the same binary, each will need a unique entry " + "point."}, + {"cpp_class", &flags->cpp_class, + "Name of the generated C++ class, wrapping the generated function. The " + "syntax of this flag is [[::],...]. " + "This mirrors the C++ syntax for referring to a class, where multiple " + "namespaces may precede the class name, separated by double-colons. " + "The class will be generated in the given namespace(s), or if no " + "namespaces are given, within the global namespace."}, + {"out_function_object", &flags->out_function_object, + "Output object file containing the generated function for the " + "TensorFlow model."}, + {"out_header", &flags->out_header, "Output header file name."}, + {"out_metadata_object", &flags->out_metadata_object, + "Output object file name containing optional metadata for the generated " + "function."}, + {"out_constant_buffers_object", &flags->out_constant_buffers_object, + "Output object file name containing constant buffers for the runtime."}, + {"out_session_module", &flags->out_session_module, + "Output session module proto."}, + {"mlir_components", &flags->mlir_components, + "The MLIR components to enable. Currently only Bridge is supported."}, + {"experimental_quantize", &flags->experimental_quantize, + "If set, quantization passes will run and dump the result before HLO " + "code generation."}, + {"sanitize_dataflow", &flags->sanitize_dataflow, + "Enable DataFlow Sanitizer pass."}, + {"sanitize_abilists_dataflow", &flags->sanitize_abilists_dataflow, + "Comma separated list of ABIList file paths."}, + {"gen_name_to_index", &flags->gen_name_to_index, + "Generate name-to-index data for Lookup{Arg,Result}Index methods."}, + {"gen_program_shape", &flags->gen_program_shape, + "Generate program shape data for the ProgramShape method."}, + {"use_xla_nanort_runtime", &flags->use_xla_nanort_runtime, + "Use xla cpu nanort runtime, otherwise each thunk execution gets " + "serialized directly into the header."}, + }; + flag_list->insert(flag_list->end(), tmp.begin(), tmp.end()); +} + +} // namespace tfcompile +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/flags.h b/western_ai/usa/tensorflow/tensorflow/compiler/aot/flags.h new file mode 100644 index 0000000000000000000000000000000000000000..af6adc9153993260fec87090db47c61ebb18fc5b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/flags.h @@ -0,0 +1,64 @@ +/* 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_AOT_FLAGS_H_ +#define TENSORFLOW_COMPILER_AOT_FLAGS_H_ + +#include +#include + +#include "tensorflow/core/util/command_line_flags.h" + +namespace tensorflow { +namespace tfcompile { + +// Flags for the tfcompile binary. See *.cc file for descriptions. + +struct MainFlags { + std::string graph; + std::string debug_info; + std::string debug_info_path_begin_marker; + std::string config; + bool dump_fetch_nodes = false; + std::string target_triple; + std::string target_cpu; + std::string target_features; + std::string entry_point; + std::string cpp_class; + std::string out_function_object; + std::string out_metadata_object; + std::string out_header; + std::string out_constant_buffers_object; + std::string out_session_module; + std::string mlir_components; + bool experimental_quantize = false; + + // Sanitizer pass options + bool sanitize_dataflow = false; + std::string sanitize_abilists_dataflow; + + // C++ codegen options + bool gen_name_to_index = false; + bool gen_program_shape = false; + bool use_xla_nanort_runtime = false; +}; + +// Appends to flag_list a tensorflow::Flag for each field in MainFlags. +void AppendMainFlags(std::vector* flag_list, MainFlags* flags); + +} // namespace tfcompile +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_AOT_FLAGS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/quantize.h b/western_ai/usa/tensorflow/tensorflow/compiler/aot/quantize.h new file mode 100644 index 0000000000000000000000000000000000000000..7a87b5f6aebc5f82c4761594a81a2a8b422094d1 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/quantize.h @@ -0,0 +1,41 @@ +/* Copyright 2020 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_AOT_QUANTIZE_H_ +#define TENSORFLOW_COMPILER_AOT_QUANTIZE_H_ + +#include +#include +#include + +#include "tensorflow/compiler/tf2xla/tf2xla.pb.h" +#include "xla/hlo/builder/xla_computation.h" +#include "tensorflow/core/platform/errors.h" +#include "tensorflow/core/platform/status.h" + +namespace tensorflow { +namespace tfcompile { + +using QuantizeXlaFn = std::function; + +// Set the static quantization function to the `fn` if it hasn't been set. +// Return false if the static function has been set. +bool RegisterQuantizeFn(const QuantizeXlaFn& fn); + +} // namespace tfcompile +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_AOT_QUANTIZE_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/aot/test.cc new file mode 100644 index 0000000000000000000000000000000000000000..126835d06ffe251776584a74723f1e4345de2696 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/test.cc @@ -0,0 +1,89 @@ +/* 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. +==============================================================================*/ + +// Generated by the tf_library build rule. DO NOT EDIT! +// +// This file contains a test and benchmark for the function generated by +// tfcompile. All tokens of the form `{{TFCOMPILE_*}}` must be rewritten to +// real values before this file can be compiled. +// +// TFCOMPILE_HEADER : Path to the header file generated by tfcompile. +// TFCOMPILE_CPP_CLASS : Name of the C++ class generated by tfcompile. +// TFCOMPILE_NAME : Name for tests and benchmarks. +// +// The tf_library bazel macro in tfcompile.bzl performs the token rewriting, and +// generates a cc_test rule for you. + +// These macros must be defined before eigen files are included. +#define EIGEN_USE_THREADS +#define EIGEN_USE_CUSTOM_THREAD_POOL + +// clang-format off +#include "{{TFCOMPILE_HEADER}}" // NOLINT(whitespace/braces) +// clang-format on + +#include "unsupported/Eigen/CXX11/Tensor" // from @eigen_archive +#include "tensorflow/core/platform/byte_order.h" +#include "tensorflow/core/platform/cpu_info.h" +#include "tensorflow/core/platform/test.h" +#include "tensorflow/core/platform/test_benchmark.h" + +// Macros that expand to tokens based on the entry point name. +// clang-format off +#define CPP_CLASS {{TFCOMPILE_CPP_CLASS}} // NOLINT(whitespace/braces) +#define TEST_NAME {{TFCOMPILE_NAME}}Test // NOLINT(whitespace/braces) +#define BM_NAME BM_{{TFCOMPILE_NAME}} // NOLINT(whitespace/braces) +// clang-format on + +namespace tensorflow { +namespace tfcompile { +namespace { + +void zero_buffers(XlaCompiledCpuFunction* computation) { + for (int i = 0; i < computation->num_args(); ++i) { + memset(computation->arg_data(i), 0, computation->arg_size(i)); + } +} + +// Trivial test that runs the generated function to ensure it doesn't crash. +TEST(TEST_NAME, NoCrash) { + Eigen::ThreadPool pool(port::MaxParallelism()); + Eigen::ThreadPoolDevice device(&pool, pool.NumThreads()); + + CPP_CLASS computation; + computation.set_thread_pool(&device); + zero_buffers(&computation); + + EXPECT_TRUE(computation.Run()); +} + +// Simple benchmark that repeatedly runs the generated function. +void BM_NAME(benchmark::State& state) { + Eigen::ThreadPool pool(port::MaxParallelism()); + Eigen::ThreadPoolDevice device(&pool, pool.NumThreads()); + + CPP_CLASS computation; + computation.set_thread_pool(&device); + zero_buffers(&computation); + + for (auto s : state) { + computation.Run(); + } +} +BENCHMARK(BM_NAME); + +} // namespace +} // namespace tfcompile +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/test_google.cc b/western_ai/usa/tensorflow/tensorflow/compiler/aot/test_google.cc new file mode 100644 index 0000000000000000000000000000000000000000..97ad9ae518113a7755f3c160e97c8328d7b514fd --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/test_google.cc @@ -0,0 +1,89 @@ +/* Copyright 2023 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. +==============================================================================*/ + +// Generated by the tf_library build rule. DO NOT EDIT! +// +// This file contains a test and benchmark for the function generated by +// tfcompile. All tokens of the form `{{TFCOMPILE_*}}` must be rewritten to +// real values before this file can be compiled. +// +// TFCOMPILE_HEADER : Path to the header file generated by tfcompile. +// TFCOMPILE_CPP_CLASS : Name of the C++ class generated by tfcompile. +// TFCOMPILE_NAME : Name for tests and benchmarks. +// +// The tf_library bazel macro in tfcompile.bzl performs the token rewriting, and +// generates a cc_test rule for you. + +// These macros must be defined before eigen files are included. +#define EIGEN_USE_THREADS +#define EIGEN_USE_CUSTOM_THREAD_POOL + +// clang-format off +#include "{{TFCOMPILE_HEADER}}" // NOLINT(whitespace/braces) +// clang-format on + +#include "testing/base/public/benchmark.h" +#include +#include "unsupported/Eigen/CXX11/Tensor" // from @eigen_archive +#include "tensorflow/core/platform/byte_order.h" +#include "tensorflow/core/platform/cpu_info.h" + +// Macros that expand to tokens based on the entry point name. +// clang-format off +#define CPP_CLASS {{TFCOMPILE_CPP_CLASS}} // NOLINT(whitespace/braces) +#define TEST_NAME {{TFCOMPILE_NAME}}Test // NOLINT(whitespace/braces) +#define BM_NAME BM_{{TFCOMPILE_NAME}} // NOLINT(whitespace/braces) +// clang-format on + +namespace tensorflow { +namespace tfcompile { +namespace { + +void zero_buffers(XlaCompiledCpuFunction* computation) { + for (int i = 0; i < computation->num_args(); ++i) { + memset(computation->arg_data(i), 0, computation->arg_size(i)); + } +} + +// Trivial test that runs the generated function to ensure it doesn't crash. +TEST(TEST_NAME, NoCrash) { + Eigen::ThreadPool pool(port::MaxParallelism()); + Eigen::ThreadPoolDevice device(&pool, pool.NumThreads()); + + CPP_CLASS computation; + computation.set_thread_pool(&device); + zero_buffers(&computation); + + EXPECT_TRUE(computation.Run()); +} + +// Simple benchmark that repeatedly runs the generated function. +void BM_NAME(benchmark::State& state) { + Eigen::ThreadPool pool(port::MaxParallelism()); + Eigen::ThreadPoolDevice device(&pool, pool.NumThreads()); + + CPP_CLASS computation; + computation.set_thread_pool(&device); + zero_buffers(&computation); + + for (auto s : state) { + computation.Run(); + } +} +BENCHMARK(BM_NAME); + +} // namespace +} // namespace tfcompile +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/test_graph_tfadd.config.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/aot/test_graph_tfadd.config.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..e90a43b9d5e5e5bbc8f7ce1884196090a33ef58f --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/test_graph_tfadd.config.pbtxt @@ -0,0 +1,16 @@ +# Text form of tensorflow.tf2xla.Config proto. +feed { + id { node_name: "x_const" } + shape { + dim { size: 1 } + } +} +feed { + id { node_name: "y_reshape" } + shape { + dim { size: 1 } + } +} +fetch { + id { node_name: "x_y_sum" } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/test_graph_tfadd.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/aot/test_graph_tfadd.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..b0b40184fa10685afab875fc5c2560b34882e2e2 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/test_graph_tfadd.pbtxt @@ -0,0 +1,56 @@ +node { + name : "x_const" + op : "Const" + attr { + key: "value" + value { + tensor { dtype: DT_INT32 tensor_shape { dim { size: 1 } } int_val: 1 } + } + } + attr { + key : "dtype" + value { + type : DT_INT32 + } + } +} +node { + name : "y_const" + op : "Const" + attr { + key: "value" + value { + tensor { dtype: DT_INT32 tensor_shape { dim { size: 1 } } int_val: 2 } + } + } + attr { + key: "dtype" + value { + type: DT_INT32 + } + } +} +node { + name : "y_reshape" + op : "Reshape" + input : "y_const" + input : "y_shape" + attr { key: "T" value { type: DT_INT32 } } + # Attribute TShape not specified; needs to be set to its default + # by tfcompile. +} +node { + name : "x_y_sum" + op : "Add" + input : "x_const" + input : "y_reshape" + attr { + key : "T" + value { + type: DT_INT32 + } + } +} +versions { + producer: 15 +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/test_graph_tfunknownop.config.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/aot/test_graph_tfunknownop.config.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..4b2e33215f5313b563d46a87c9f7443ec621dd6d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/test_graph_tfunknownop.config.pbtxt @@ -0,0 +1,16 @@ +# Text form of tensorflow.tf2xla.Config proto. +feed { + id { node_name: "x_const" } + shape { + dim { size: 1 } + } +} +feed { + id { node_name: "y_const" } + shape { + dim { size: 1 } + } +} +fetch { + id { node_name: "x_y_sum" } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/test_graph_tfunknownop.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/aot/test_graph_tfunknownop.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..835e298edc8d4f7c77f69386075f3b6a76683ec2 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/test_graph_tfunknownop.pbtxt @@ -0,0 +1,58 @@ +node { + name : "x_const" + op : "Const" + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { dim { size: 1 } } + int_val: 1 + } + } + } + attr { key : "dtype" value { type: DT_INT32 } } +} +node { + name : "y_const" + op : "Const" + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { dim { size: 1 } } + int_val: 2 + } + } + } + attr { key: "dtype" value { type: DT_INT32 } } +} +node { + name : "x_y_sum" + op : "Add" + input : "x_const" + input : "y_const" + attr { key : "T" value { type: DT_INT32 } } +} +node { + name : "z" + op : "SomeUnknownOp" + input : "x_const" +} +node { + name : "z_identity" + op : "Identity" + input : "z:1" + attr { key : "T" value { type: DT_INT32 } } +} +node { + name : "x_z_sum" + op : "Add" + input : "x_const" + input : "z_identity" + attr { key : "T" value { type: DT_INT32 } } +} +versions { + producer: 15 +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/test_graph_tfunknownop2.config.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/aot/test_graph_tfunknownop2.config.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..870f704ea3dd5e5b4f43886cf811acc3502d8cab --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/test_graph_tfunknownop2.config.pbtxt @@ -0,0 +1,25 @@ +# Text form of tensorflow.tf2xla.Config proto. +feed { + id { node_name: "x_const" } + shape { + dim { size: 1 } + } +} +feed { + id { node_name: "y_const" } + shape { + dim { size: 1 } + } +} +feed { + id { node_name: "z_identity"} + shape { + dim { size: 1 } + } +} +fetch { + id { node_name: "x_y_sum" } +} +fetch { + id { node_name: "x_z_sum" } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/test_graph_tfunknownop3.config.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/aot/test_graph_tfunknownop3.config.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..214f457a7de2db99e90b8bcb90489fff73ef114d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/test_graph_tfunknownop3.config.pbtxt @@ -0,0 +1,26 @@ +# Text form of tensorflow.tf2xla.Config proto. +feed { + id { node_name: "x_const" } + shape { + dim { size: 1 } + } +} +feed { + id { node_name: "y_const" } + shape { + dim { size: 1 } + } +} +feed { + id { node_name: "z" output_index: 1} + shape { + dim { size: 1 } + } + type: DT_INT32 +} +fetch { + id { node_name: "x_y_sum" } +} +fetch { + id { node_name: "x_z_sum" } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..f4012c891d092e849f955bbd5b8365bfd06582cd --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/BUILD @@ -0,0 +1,520 @@ +load("@xla//third_party/rules_python/python:py_binary.bzl", "py_binary") +load("//tensorflow:tensorflow.bzl", "tf_cc_test") +load("//tensorflow:tensorflow.default.bzl", "filegroup", "genrule") +load("//tensorflow/compiler/aot:tfcompile.bzl", "tf_library") +load("//tensorflow/compiler/mlir:glob_lit_test.bzl", "glob_lit_tests") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:license"], + default_visibility = ["//visibility:private"], + licenses = ["notice"], +) + +glob_lit_tests( + name = "all_lit_tests", + data = [":filecheck_test_utilities"], + driver = "@llvm-project//mlir:run_lit.sh", + tags_override = { + "test_error_message.lit.pbtxt": ["no_oss"], # TODO(b/150957738): to be fixed on oss. + }, + test_file_exts = ["lit.pbtxt"], +) + +# Bundle together all of the test utilities that are used by tests. +filegroup( + name = "filecheck_test_utilities", + testonly = True, + srcs = [ + "test_error_message.lit.pbtxt.config.pbtxt", + "test_error_message.lit.pbtxt.debug.pbtxt", + "test_error_message.lit.pbtxt.fake_py.debug", + ], + data = [ + "//tensorflow/compiler/aot:tfcompile", + "@llvm-project//llvm:FileCheck", + "@llvm-project//llvm:not", + ], +) + +# We disable some tfcompile tests in the open source build with the +# "manual" tag to avoid making our OSS users build LLVM twice +# (once for host and once for target). + +test_suite( + name = "all_tests", + tags = ["manual"], + tests = [ + ":test_graph_tfadd_thunks_test", + ":test_graph_tfadd_with_ckpt_saver_thunks_test", + ":test_graph_tfadd_with_ckpt_thunks_test", + ":test_graph_tfassert_eq_thunks_test", + ":test_graph_tfcond_thunks_test", + ":test_graph_tffunction_thunks_test", + ":test_graph_tfgather_thunks_test", + ":test_graph_tfmatmul_thunks_test", + ":test_graph_tfmatmulandadd_thunks_test", + ":test_graph_tfsplits_thunks_test", + ":test_graph_tftop_k_thunks_test", + ":test_graph_tfvariable_readonly_thunks_test", + ":test_graph_tfvariable_sequential_updates_thunks_test", + ":test_graph_tfvariable_thunks_test", + ":tfcompile_test_thunks", + ], + visibility = ["//visibility:public"], +) + +py_binary( + name = "make_test_graphs", + testonly = 1, + srcs = ["make_test_graphs.py"], + strict_deps = True, + deps = [ + "//tensorflow/core:protos_all_py", + "//tensorflow/python/client:session", + "//tensorflow/python/framework:constant_op", + "//tensorflow/python/framework:dtypes", + "//tensorflow/python/framework:function", + "//tensorflow/python/framework:ops", + "//tensorflow/python/ops:array_ops", + "//tensorflow/python/ops:array_ops_stack", + "//tensorflow/python/ops:cond", + "//tensorflow/python/ops:control_flow_assert", + "//tensorflow/python/ops:control_flow_ops", + "//tensorflow/python/ops:control_flow_util", + "//tensorflow/python/ops:math_ops", + "//tensorflow/python/ops:nn_ops", + "//tensorflow/python/ops:random_ops", + "//tensorflow/python/ops:variable_v1", + "//tensorflow/python/ops:variables", + "//tensorflow/python/training:saver", + "@absl_py//absl:app", + "@six_archive//:six", + ], +) + +genrule( + name = "gen_test_graphs", + testonly = 1, + outs = [ + "test_graph_tfadd.pb", + "test_graph_tfadd_with_ckpt.ckpt", + "test_graph_tfadd_with_ckpt.pb", + "test_graph_tfadd_with_ckpt_saver.ckpt", + "test_graph_tfadd_with_ckpt_saver.pb", + "test_graph_tfadd_with_ckpt_saver.saver", + "test_graph_tfassert_eq.pb", + "test_graph_tfcond.pb", + "test_graph_tffunction.pb", + "test_graph_tfgather.pb", + "test_graph_tfmatmul.pb", + "test_graph_tfmatmulandadd.pb", + "test_graph_tfmatmul_with_constant.pb", + "test_graph_tfsplits.pb", + "test_graph_tftop_k.pb", + "test_graph_tfvariable.pb", + "test_graph_tfvariable_readonly.pb", + "test_graph_tfvariable_sequential_updates.pb", + "test_graph_tfrandom_uniform.pb", + "test_graph_tfscatter.pb", + ], + # Set CUDA_VISIBLE_DEVICES='' to prevent the code we launch from using any + # GPUs which might be present. This is important because builds may run + # concurrently with tests, and tests need to be able to assume that they + # have control of the full GPU. + cmd = "CUDA_VISIBLE_DEVICES='' " + + "$(location :make_test_graphs) --out_dir $(@D)", + tags = ["manual"], + tools = [":make_test_graphs"], +) + +# This list is used to run tf_compile on the compiled graphs used by +# tfcompile_test. The test is run for several different configurations of +# tf_compile, so this allows the configurations to be specified in one place. +# suffix, mlir_component, xla_flags, tfcompile_flags +tfcompile_test_dep_configs = [ + ("_thunks", "None", "--xla_cpu_use_thunk_runtime=true", ""), + ("_nanort", "None", "--xla_cpu_use_thunk_runtime=true", "--use_xla_nanort_runtime"), +] + +[ + [ + tf_library( + name = "test_graph_tfadd" + suffix, + testonly = 1, + config = "test_graph_tfadd.config.pbtxt", + cpp_class = "AddComp", + graph = "test_graph_tfadd.pb", + # This serves as a test for the list of minimal deps included even when + # include_standard_runtime_deps is False. If this target fails to + # compile but the others in this directory succeed, you may need to + # expand the "required by all tf_library targets" list in tfcompile.bzl. + include_standard_runtime_deps = False, + mlir_components = mlir_component, + tags = [ + "manual", + "no_mac", # TODO(b/228273415) + ], + tfcompile_flags = tfcompile_flags, + xla_flags = xla_flags, + ), + tf_library( + name = "test_graph_tfadd_with_ckpt" + suffix, + testonly = 1, + config = "test_graph_tfadd_with_ckpt.config.pbtxt", + cpp_class = "AddWithCkptComp", + freeze_checkpoint = "test_graph_tfadd_with_ckpt.ckpt", + graph = "test_graph_tfadd_with_ckpt.pb", + mlir_components = mlir_component, + tags = [ + "manual", + "no_mac", # TODO(b/228273415) + ], + tfcompile_flags = tfcompile_flags, + xla_flags = xla_flags, + ), + tf_library( + name = "test_graph_tfadd_with_ckpt_saver" + suffix, + testonly = 1, + config = "test_graph_tfadd_with_ckpt.config.pbtxt", + cpp_class = "AddWithCkptSaverComp", + freeze_checkpoint = "test_graph_tfadd_with_ckpt_saver.ckpt", + freeze_saver = "test_graph_tfadd_with_ckpt_saver.saver", + graph = "test_graph_tfadd_with_ckpt_saver.pb", + mlir_components = mlir_component, + tags = [ + "manual", + "no_mac", # TODO(b/228273415) + ], + tfcompile_flags = tfcompile_flags, + xla_flags = xla_flags, + ), + tf_library( + name = "test_graph_tfassert_eq" + suffix, + testonly = 1, + config = "test_graph_tfassert_eq.config.pbtxt", + cpp_class = "AssertComp", + graph = "test_graph_tfassert_eq.pb", + mlir_components = mlir_component, + tags = [ + "manual", + "no_mac", # TODO(b/228273415) + ], + tfcompile_flags = tfcompile_flags, + xla_flags = xla_flags, + ), + tf_library( + name = "test_graph_tfcond" + suffix, + testonly = 1, + config = "test_graph_tfcond.config.pbtxt", + cpp_class = "CondComp", + graph = "test_graph_tfcond.pb", + mlir_components = mlir_component, + tags = [ + "manual", + "no_mac", # TODO(b/228273415) + ], + tfcompile_flags = tfcompile_flags, + xla_flags = xla_flags, + ), + tf_library( + name = "test_graph_tffunction" + suffix, + testonly = 1, + config = "test_graph_tffunction.config.pbtxt", + cpp_class = "FunctionComp", + graph = "test_graph_tffunction.pb", + mlir_components = mlir_component, + tags = [ + "manual", + "no_mac", # TODO(b/228273415) + ], + tfcompile_flags = tfcompile_flags, + xla_flags = xla_flags, + ), + tf_library( + name = "test_graph_tfgather" + suffix, + testonly = 1, + config = "test_graph_tfgather.config.pbtxt", + cpp_class = "GatherComp", + graph = "test_graph_tfgather.pb", + mlir_components = mlir_component, + tags = [ + "manual", + "no_mac", # TODO(b/228273415) + ], + tfcompile_flags = tfcompile_flags, + xla_flags = xla_flags, + ), + tf_library( + name = "test_graph_tfmatmul" + suffix, + testonly = 1, + config = "test_graph_tfmatmul.config.pbtxt", + cpp_class = "foo::bar::MatMulComp", + graph = "test_graph_tfmatmul.pb", + mlir_components = mlir_component, + tags = [ + "manual", + "no_mac", # TODO(b/228273415) + ], + tfcompile_flags = tfcompile_flags, + xla_flags = xla_flags, + ), + tf_library( + name = "test_graph_tfmatmulandadd" + suffix, + testonly = 1, + config = "test_graph_tfmatmulandadd.config.pbtxt", + cpp_class = "::foo::bar::MatMulAndAddComp", + graph = "test_graph_tfmatmulandadd.pb", + mlir_components = mlir_component, + tags = [ + "manual", + "no_mac", # TODO(b/228273415) + ], + tfcompile_flags = "--gen_name_to_index --gen_program_shape " + tfcompile_flags, + xla_flags = xla_flags, + ), + tf_library( + name = "test_graph_tfmatmul_with_constant" + suffix, + testonly = 1, + config = "test_graph_tfmatmul_with_constant.config.pbtxt", + cpp_class = "foo::bar::MatMulWithConstantComp", + graph = "test_graph_tfmatmul_with_constant.pb", + include_standard_runtime_deps = True, + mlir_components = mlir_component, + tags = [ + "manual", + "no_mac", # TODO(b/228273415) + ], + tfcompile_flags = tfcompile_flags, + xla_flags = xla_flags, + ), + tf_library( + name = "test_graph_tfsplits" + suffix, + testonly = 1, + config = "test_graph_tfsplits.config.pbtxt", + cpp_class = "SplitsComp", + graph = "test_graph_tfsplits.pb", + mlir_components = mlir_component, + tags = [ + "manual", + "no_mac", # TODO(b/228273415) + ], + tfcompile_flags = tfcompile_flags, + xla_flags = xla_flags, + ), + tf_library( + name = "test_graph_tftop_k" + suffix, + testonly = 1, + config = "test_graph_tftop_k.config.pbtxt", + cpp_class = "TopKComp", + graph = "test_graph_tftop_k.pb", + mlir_components = "None", + tags = [ + "manual", + "no_mac", # TODO(b/228273415) + ], + tfcompile_flags = tfcompile_flags, + xla_flags = xla_flags, + ), + tf_library( + name = "test_graph_tfvariable" + suffix, + testonly = 1, + config = "test_graph_tfvariable.config.pbtxt", + cpp_class = "VariableComp", + graph = "test_graph_tfvariable.pb", + mlir_components = mlir_component, + tags = [ + "manual", + "no_mac", # TODO(b/228273415) + ], + tfcompile_flags = tfcompile_flags, + xla_flags = xla_flags, + ), + tf_library( + name = "test_graph_tfvariable_readonly" + suffix, + testonly = 1, + config = "test_graph_tfvariable_readonly.config.pbtxt", + cpp_class = "VariableReadonlyComp", + graph = "test_graph_tfvariable_readonly.pb", + mlir_components = mlir_component, + tags = [ + "manual", + "no_mac", # TODO(b/228273415) + ], + tfcompile_flags = tfcompile_flags, + xla_flags = xla_flags, + ), + tf_library( + name = "test_graph_tfvariable_sequential_updates" + suffix, + testonly = 1, + config = "test_graph_tfvariable_sequential_updates.config.pbtxt", + cpp_class = "VariableSequentialUpdatesComp", + graph = "test_graph_tfvariable_sequential_updates.pb", + mlir_components = mlir_component, + tags = [ + "manual", + "no_mac", # TODO(b/228273415) + ], + tfcompile_flags = tfcompile_flags, + xla_flags = xla_flags, + ), + tf_library( + name = "test_graph_tfrandom_uniform" + suffix, + testonly = 1, + config = "test_graph_tfrandom_uniform.config.pbtxt", + cpp_class = "RandomUniformComp", + graph = "test_graph_tfrandom_uniform.pb", + include_standard_runtime_deps = True, + mlir_components = mlir_component, + tags = [ + "manual", + "no_mac", # TODO(b/228273415) + ], + tfcompile_flags = tfcompile_flags, + xla_flags = xla_flags, + ), + tf_library( + name = "test_graph_tfscatter" + suffix, + testonly = 1, + config = "test_graph_tfscatter.config.pbtxt", + cpp_class = "ScatterComp", + graph = "test_graph_tfscatter.pb", + mlir_components = mlir_component, + tags = [ + "manual", + "no_mac", # TODO(b/228273415) + ], + tfcompile_flags = tfcompile_flags, + xla_flags = xla_flags, + ), + ] + for suffix, mlir_component, xla_flags, tfcompile_flags in tfcompile_test_dep_configs +] + +tfcompile_bench_tfmatmul_mkn = [ + # Intentionally empty to avoid running unnecessary tests. + # Add here your desired (M, K, N) parameters, e.g. + # (1, 1, 256), + # (1, 2, 256), +] + +tfcompile_bench_tfmatmul = [ + ( + "bench_graph_tfmatmul_%sx%sx%s" % (m, k, n), + "bench_graph_tfmatmul_%sx%sx%s.config.pbtxt" % (m, k, n), + "bench_graph_tfmatmul.template.pbtxt", + "-e \"s||%s|g\" -e \"s||%s|g\" -e \"s||%s|g\"" % (m, k, n), + ) + for (m, k, n) in tfcompile_bench_tfmatmul_mkn +] + +test_suite( + name = "all_tfmatmul_benchmarks", + tags = ["manual"], + tests = [ + (":%s_test" % bench_name) + for (bench_name, _, _, _) in tfcompile_bench_tfmatmul + ], + visibility = ["//visibility:public"], +) + +[[ + genrule( + name = "gen_" + config_file, + testonly = 1, + srcs = [template_file], + outs = [config_file], + cmd = ("sed " + sed_replace + " " + + "$(location " + template_file + ") " + + "> $(OUTS)"), + tags = ["manual"], + ), + tf_library( + name = bench_name, + testonly = 1, + config = config_file, + cpp_class = "foo::bar::MatMulComp", + graph = "test_graph_tfmatmul.pb", + tags = [ + "manual", + "no_mac", # TODO(b/228273415) + ], + ), +] for (bench_name, config_file, template_file, sed_replace) in tfcompile_bench_tfmatmul] + +tf_cc_test( + name = "tfcompile_test_thunks", + srcs = ["tfcompile_test.cc"], + extra_copts = ["-DENABLE_XLA_THUNK_TEST"], + tags = [ + "manual", + "no_mac", # TODO(b/228273415) + "not_run:arm", + ], + deps = [ + ":test_graph_tfadd_thunks", + ":test_graph_tfadd_with_ckpt_saver_thunks", + ":test_graph_tfadd_with_ckpt_thunks", + ":test_graph_tfassert_eq_thunks", + ":test_graph_tfcond_thunks", + ":test_graph_tffunction_thunks", + ":test_graph_tfgather_thunks", + ":test_graph_tfmatmul_thunks", + ":test_graph_tfmatmul_with_constant_thunks", + ":test_graph_tfmatmulandadd_thunks", + ":test_graph_tfrandom_uniform_thunks", + ":test_graph_tfscatter_thunks", + ":test_graph_tfsplits_thunks", + ":test_graph_tftop_k_thunks", + ":test_graph_tfvariable_readonly_thunks", + ":test_graph_tfvariable_sequential_updates_thunks", + ":test_graph_tfvariable_thunks", + "//tensorflow/compiler/tf2xla:xla_compiled_cpu_function", + "//tensorflow/core:portable_gif_internal", + "//tensorflow/core:test", + "//tensorflow/core:test_main", + "@eigen_archive//:eigen3", + "@xla//xla:shape_util", + "@xla//xla/hlo/testlib:test", + "@xla//xla/tsl/platform:env", + "@xla//xla/tsl/platform:statusor", + ], +) + +tf_cc_test( + name = "tfcompile_test_nanort", + srcs = ["tfcompile_test.cc"], + extra_copts = ["-DENABLE_XLA_NANORT_TEST"], + tags = [ + "manual", + "no_mac", # TODO(b/228273415) + "not_run:arm", + ], + deps = [ + ":test_graph_tfadd_nanort", + ":test_graph_tfadd_with_ckpt_nanort", + ":test_graph_tfadd_with_ckpt_saver_nanort", + ":test_graph_tfassert_eq_nanort", + ":test_graph_tfcond_nanort", + ":test_graph_tffunction_nanort", + ":test_graph_tfgather_nanort", + ":test_graph_tfmatmul_nanort", + ":test_graph_tfmatmul_with_constant_nanort", + ":test_graph_tfmatmulandadd_nanort", + ":test_graph_tfrandom_uniform_nanort", + ":test_graph_tfscatter_nanort", + ":test_graph_tfsplits_nanort", + ":test_graph_tftop_k_nanort", + ":test_graph_tfvariable_nanort", + ":test_graph_tfvariable_readonly_nanort", + ":test_graph_tfvariable_sequential_updates_nanort", + "//tensorflow/compiler/tf2xla:xla_compiled_cpu_function", + "//tensorflow/core:portable_gif_internal", + "//tensorflow/core:test", + "//tensorflow/core:test_main", + "@eigen_archive//:eigen3", + "@xla//xla:shape_util", + "@xla//xla/hlo/testlib:test", + "@xla//xla/tsl/platform:env", + "@xla//xla/tsl/platform:statusor", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/bench_graph_tfmatmul.template.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/bench_graph_tfmatmul.template.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..f9cf0c57b25a36672673a33e9fa85e9ac80e7a15 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/bench_graph_tfmatmul.template.pbtxt @@ -0,0 +1,18 @@ +# Text form of tensorflow.tf2xla.Config proto. +feed { + id { node_name: "x_hold" } + shape { + dim { size: } + dim { size: } + } +} +feed { + id { node_name: "y_hold" } + shape { + dim { size: } + dim { size: } + } +} +fetch { + id { node_name: "x_y_prod" } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/make_test_graphs.py b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/make_test_graphs.py new file mode 100644 index 0000000000000000000000000000000000000000..9dc831e5b4300bfd5167ea59970977f1dcd307bd --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/make_test_graphs.py @@ -0,0 +1,259 @@ +# 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. +# ============================================================================== +"""Generate tensorflow graphs for testing tfcompile.""" + +import argparse +import os +import sys + +from absl import app +import six +from six.moves import range + +from tensorflow.core.protobuf import saver_pb2 +from tensorflow.python.client import session +from tensorflow.python.framework import constant_op +from tensorflow.python.framework import dtypes +from tensorflow.python.framework import function +from tensorflow.python.framework import ops +from tensorflow.python.ops import array_ops +from tensorflow.python.ops import array_ops_stack # pylint: disable=g-direct-tensorflow-import +from tensorflow.python.ops import cond +from tensorflow.python.ops import control_flow_assert +from tensorflow.python.ops import control_flow_ops +from tensorflow.python.ops import control_flow_util +from tensorflow.python.ops import math_ops +from tensorflow.python.ops import nn_ops +from tensorflow.python.ops import random_ops +from tensorflow.python.ops import variable_v1 +from tensorflow.python.ops import variables +from tensorflow.python.training import saver as saver_lib + +FLAGS = None + + +def tfadd(_): + x = constant_op.constant([1], name='x_const') + y = constant_op.constant([2], name='y_const') + math_ops.add(x, y, name='x_y_sum') + + +def tfadd_with_ckpt(out_dir): + x = array_ops.placeholder(dtypes.int32, name='x_hold') + y = variable_v1.VariableV1(constant_op.constant([0]), name='y_saved') + math_ops.add(x, y, name='x_y_sum') + + init_op = variables.global_variables_initializer() + saver = saver_lib.Saver(write_version=saver_pb2.SaverDef.V1) + with session.Session() as sess: + sess.run(init_op) + sess.run(y.assign(y + 42)) + # Without the checkpoint, the variable won't be set to 42. + ckpt = os.path.join(out_dir, 'test_graph_tfadd_with_ckpt.ckpt') + saver.save(sess, ckpt) + + +def tfadd_with_ckpt_saver(out_dir): + x = array_ops.placeholder(dtypes.int32, name='x_hold') + y = variable_v1.VariableV1(constant_op.constant([0]), name='y_saved') + math_ops.add(x, y, name='x_y_sum') + + init_op = variables.global_variables_initializer() + saver = saver_lib.Saver(name='abcprefix', write_version=saver_pb2.SaverDef.V1) + with session.Session() as sess: + sess.run(init_op) + sess.run(y.assign(y + 42)) + # Without the checkpoint, the variable won't be set to 42. + ckpt_file = os.path.join(out_dir, 'test_graph_tfadd_with_ckpt_saver.ckpt') + saver.save(sess, ckpt_file) + # Without the SaverDef, the restore op won't be named correctly. + saver_file = os.path.join(out_dir, 'test_graph_tfadd_with_ckpt_saver.saver') + with open(saver_file, 'wb') as f: + f.write(six.ensure_binary(saver.as_saver_def().SerializeToString())) + + +def tfassert_eq(_): + x = array_ops.placeholder(dtypes.int32, name='x_hold') + y = array_ops.placeholder(dtypes.int32, name='y_hold') + control_flow_assert.Assert( + math_ops.equal(x, y), ['Expected x == y.'], name='assert_eq' + ) + math_ops.add(x, math_ops.negative(y), name='x_y_diff') + + +def tfcond(_): + p = array_ops.placeholder(dtypes.bool, name='p_hold') + x = array_ops.placeholder(dtypes.int32, name='x_hold') + y = array_ops.placeholder(dtypes.int32, name='y_hold') + z = cond.cond(p, lambda: x, lambda: y) + array_ops.identity(z, name='result') + + +def tfgather(_): + params = array_ops.placeholder(dtypes.float32, name='params') + indices = array_ops.placeholder(dtypes.int32, name='indices') + array_ops.gather(params, indices, name='gather_output') + + +def tfmatmul(_): + x = array_ops.placeholder(dtypes.float32, name='x_hold') + y = array_ops.placeholder(dtypes.float32, name='y_hold') + math_ops.matmul(x, y, name='x_y_prod') + + +def tfmatmul_with_constant(_): + x = array_ops.placeholder(dtypes.float32, name='x_hold') + constant_y = array_ops.ones( + shape=[1024, 256], dtype=dtypes.float32, name='constant_y_hold' + ) + math_ops.matmul(x, constant_y, name='x_constant_y_prod') + + +def tfmatmulandadd(_): + # This tests multiple outputs. + x = array_ops.placeholder(dtypes.float32, name='x_hold') + y = array_ops.placeholder(dtypes.float32, name='y_hold') + math_ops.matmul(x, y, name='x_y_prod') + math_ops.add(x, y, name='x_y_sum') + + +def tffunction(_): + + @function.Defun(dtypes.int32, dtypes.int32) + def test_func(a, b): + return a + b + + x = constant_op.constant([1], name='x_const') + y = constant_op.constant([2], name='y_const') + test_func(x, y, name='func_call') # pylint: disable=unexpected-keyword-arg + + +def tfsplits(_): + """A more complex graph, including splits.""" + x = array_ops.placeholder(dtypes.float32, shape=[2, 2], name='x') + y = array_ops.placeholder(dtypes.float32, shape=[2, 2], name='y') + for _ in range(3): + x0, x1 = array_ops.split(x, 2, 0) + y0, y1 = array_ops.split(y, 2, 0) + x0 += 1 + y0 += 1 + z = math_ops.matmul(x, y, name='x_y_prod') + a = array_ops.concat([x0, y1], axis=0, name='concat_x0_y1') + b = array_ops.concat([y0, x1], axis=0, name='concat_y0_x1') + x = math_ops.matmul(a, b, name='a_b') + y = math_ops.add(x, z) + array_ops.identity(y, name='result') + + +def tftop_k(_): + x = array_ops.placeholder(dtypes.int32, shape=[5], name='x') + output = nn_ops.top_k(x, 2, name='values') + array_ops.identity(output[1], name='indices') + + +def tfvariable_readonly(_): + x = variables.Variable(1000.0, name='x') + unused_y = variables.Variable(1000.0, name='y') + old_x = x.value() + with ops.control_dependencies([old_x]): + new_value = math_ops.add(old_x, 42.0) + array_ops.identity(new_value, name='result') + + +# TODO(b/147908587): Change x and the two constants back to have a scalar shape +# when the bug is fixed. +def tfvariable(_): + x = variables.Variable([1000.0], name='x', shape=[1]) + old_x = x.value() + with ops.control_dependencies([old_x]): + new_x = x.assign_add([42.0]) + array_ops_stack.stack([old_x, new_x], name='result') + + +def tfvariable_sequential_updates(_): + x = variables.Variable(1.0, name='x') + y = variables.Variable(1.0, name='y') + updates = control_flow_ops.no_op() + for _ in range(3): + with ops.control_dependencies([updates]): + x_val = x.read_value() + y + updates = x.assign_sub(0.1 * x_val) + + array_ops.identity(updates, name='result') + + +def tfrandom_uniform(_): + x = random_ops.random_uniform(shape=[1], minval=0.0, maxval=5.0) + array_ops.identity(x, name='result') + + +def tfscatter(_): + # NOTE(basioli): We run two scatter operations to make sure fusion kernels + # get named correctly in XLA:CPU. + indices0 = array_ops.placeholder(dtypes.int32, name='indices_0') + updates0 = array_ops.placeholder(dtypes.float32, name='updates_0') + indices1 = array_ops.placeholder(dtypes.int32, name='indices_1') + updates1 = array_ops.placeholder(dtypes.float32, name='updates_1') + shape = constant_op.constant([8], name='shape') + array_ops.scatter_nd(indices0, updates0, shape, name='result_0') + array_ops.scatter_nd(indices1, updates1, shape, name='result_1') + + +def write_graph(build_graph, out_dir): + """Build a graph using build_graph and write it out.""" + g = ops.Graph() + with g.as_default(): + build_graph(out_dir) + filename = os.path.join(out_dir, 'test_graph_%s.pb' % build_graph.__name__) + with open(filename, 'wb') as f: + f.write( + six.ensure_binary( + g.as_graph_def().SerializeToString(deterministic=True) + ) + ) + + +def main(_): + control_flow_util.enable_control_flow_v2() + write_graph(tfadd, FLAGS.out_dir) + write_graph(tfadd_with_ckpt, FLAGS.out_dir) + write_graph(tfadd_with_ckpt_saver, FLAGS.out_dir) + write_graph(tfassert_eq, FLAGS.out_dir) + write_graph(tfcond, FLAGS.out_dir) + write_graph(tffunction, FLAGS.out_dir) + write_graph(tfgather, FLAGS.out_dir) + write_graph(tfmatmul, FLAGS.out_dir) + write_graph(tfmatmul_with_constant, FLAGS.out_dir) + write_graph(tfmatmulandadd, FLAGS.out_dir) + write_graph(tfsplits, FLAGS.out_dir) + write_graph(tftop_k, FLAGS.out_dir) + write_graph(tfvariable, FLAGS.out_dir) + write_graph(tfvariable_readonly, FLAGS.out_dir) + write_graph(tfvariable_sequential_updates, FLAGS.out_dir) + write_graph(tfrandom_uniform, FLAGS.out_dir) + write_graph(tfscatter, FLAGS.out_dir) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.register('type', 'bool', lambda v: v.lower() == 'true') + parser.add_argument( + '--out_dir', + type=str, + default='', + help='Output directory for graphs, checkpoints and savers.', + ) + FLAGS, unparsed = parser.parse_known_args() + app.run(main=main, argv=[sys.argv[0]] + unparsed) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_error_message.lit.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_error_message.lit.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..4fc782384f63f42323e0604b63811b2d361c7297 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_error_message.lit.pbtxt @@ -0,0 +1,69 @@ +# RUN: not tfcompile --graph=%s --config=%s.config.pbtxt --mlir_components=Bridge --debug_info=%s.debug.pbtxt 2>&1 | FileCheck %s +# RUN: not tfcompile --graph=%s --config=%s.config.pbtxt --mlir_components=None 2>&1 | FileCheck -check-prefix=OLD %s + +# Checks the error message produced by tfcompile with mlir_component +# Checks that source debug information is used in the output error message and +# the node x_y_sum = Add +# CHECK: INVALID ARGUMENTS: Dimensions must be equal, but are 2 and 3 for 'x_y_sum = Add[T=DT_INT32](aot_feed_0/x, aot_feed_0/y)' +# CHECK: math_ops.add(x, y, name='x_y_sum') +# CHECK: build_graph(out_dir) + +# Checks the error message produced by tfcompile without mlir_component +# OLD: INVALID ARGUMENTS: Incompatible shapes: [2] vs. [3] +# OLD: x_y_sum + +node: { + name: "x" + op: "Placeholder" + attr: { + key: "shape" + value: { + shape: { + dim: { + size: -1 + } + } + } + } + attr: { + key: "dtype" + value: { + type: DT_INT32 + } + } +} +node: { + name: "y" + op: "Placeholder" + attr: { + key: "shape" + value: { + shape: { + dim: { + size: -1 + } + } + } + } + attr: { + key: "dtype" + value: { + type: DT_INT32 + } + } +} +node: { + name: "x_y_sum" + op: "Add" + input: "x" + input: "y" + attr: { + key: "T" + value: { + type: DT_INT32 + } + } +} +versions: { + producer: 321 +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_error_message.lit.pbtxt.config.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_error_message.lit.pbtxt.config.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..6b29197034230490c077fbbc3ed5324a9925f456 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_error_message.lit.pbtxt.config.pbtxt @@ -0,0 +1,16 @@ +# Text form of tensorflow.tf2xla.Config proto. +feed { + id { node_name: "x" } + shape { + dim { size: 2 } + } +} +feed { + id { node_name: "y" } + shape { + dim { size: 3 } + } +} +fetch { + id { node_name: "x_y_sum" } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_error_message.lit.pbtxt.debug.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_error_message.lit.pbtxt.debug.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..f9fa33300bcd639a539aa8a9efd1b9479fe29a24 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_error_message.lit.pbtxt.debug.pbtxt @@ -0,0 +1,32 @@ +files: "org_tensorflow/tensorflow/compiler/aot/tests/test_error_message.lit.pbtxt.fake_py.debug" +traces: { + key: "x@" + value: { + file_line_cols: { + line: 1 + col: 1 + } + } +} +traces: { + key: "x_y_sum@" + value: { + file_line_cols: { + line: 3 + col: 1 + } + file_line_cols: { + line: 4 + col: 1 + } + } +} +traces: { + key: "y@" + value: { + file_line_cols: { + line: 2 + col: 1 + } + } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_error_message.lit.pbtxt.fake_py.debug b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_error_message.lit.pbtxt.fake_py.debug new file mode 100644 index 0000000000000000000000000000000000000000..78b1ace67554c7be6952bbbd3121672e2f7e6e6a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_error_message.lit.pbtxt.fake_py.debug @@ -0,0 +1,4 @@ + x = value + y = value + math_ops.add(x, y, name='x_y_sum') + build_graph(out_dir) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfadd.config.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfadd.config.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..4b2e33215f5313b563d46a87c9f7443ec621dd6d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfadd.config.pbtxt @@ -0,0 +1,16 @@ +# Text form of tensorflow.tf2xla.Config proto. +feed { + id { node_name: "x_const" } + shape { + dim { size: 1 } + } +} +feed { + id { node_name: "y_const" } + shape { + dim { size: 1 } + } +} +fetch { + id { node_name: "x_y_sum" } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfadd_with_ckpt.config.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfadd_with_ckpt.config.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..288b40251d4975a640de3c2cfb934f1b7126ca26 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfadd_with_ckpt.config.pbtxt @@ -0,0 +1,10 @@ +# Text form of tensorflow.tf2xla.Config proto. +feed { + id { node_name: "x_hold" } + shape { + dim { size: 1 } + } +} +fetch { + id { node_name: "x_y_sum" } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfassert_eq.config.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfassert_eq.config.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..1b9164d78a6698472754934daf6dae4104743497 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfassert_eq.config.pbtxt @@ -0,0 +1,16 @@ +# Text form of tensorflow.tf2xla.Config proto. +feed { + id { node_name: "x_hold" } + shape { + dim { size: 1 } + } +} +feed { + id { node_name: "y_hold" } + shape { + dim { size: 1 } + } +} +fetch { + id { node_name: "x_y_diff" } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfcond.config.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfcond.config.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..e903096a942ab55ddb7c43d24ab9444aa1ca288b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfcond.config.pbtxt @@ -0,0 +1,20 @@ +# Text form of tensorflow.tf2xla.Config proto. +feed { + id { node_name: "p_hold" } + shape {} +} +feed { + id { node_name: "x_hold" } + shape { + dim { size: 1 } + } +} +feed { + id { node_name: "y_hold" } + shape { + dim { size: 1 } + } +} +fetch { + id { node_name: "result" } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tffunction.config.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tffunction.config.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..13a4b690e1979cf59e0b84374e84b20c0ee9db2b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tffunction.config.pbtxt @@ -0,0 +1,16 @@ +# Text form of tensorflow.tf2xla.Config proto. +feed { + id { node_name: "x_const" } + shape { + dim { size: 1 } + } +} +feed { + id { node_name: "y_const" } + shape { + dim { size: 1 } + } +} +fetch { + id { node_name: "func_call" } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfgather.config.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfgather.config.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..4dbb31b1eb2b457d92e5809237154ea82323f2c9 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfgather.config.pbtxt @@ -0,0 +1,16 @@ +# Text form of tensorflow.tf2xla.Config proto. +feed { + id { node_name: "params" } + shape { + dim { size: 4 } + } +} +feed { + id { node_name: "indices" } + shape { + dim { size: 2 } + } +} +fetch { + id { node_name: "gather_output" } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfmatmul.config.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfmatmul.config.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..6d74ab56e26cea95746235d8be30dd21007abbf1 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfmatmul.config.pbtxt @@ -0,0 +1,18 @@ +# Text form of tensorflow.tf2xla.Config proto. +feed { + id { node_name: "x_hold" } + shape { + dim { size: 2 } + dim { size: 3 } + } +} +feed { + id { node_name: "y_hold" } + shape { + dim { size: 3 } + dim { size: 2 } + } +} +fetch { + id { node_name: "x_y_prod" } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfmatmul_with_constant.config.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfmatmul_with_constant.config.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..0ed7e9bba94a0d56ac154148d7d24ba37cb546b9 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfmatmul_with_constant.config.pbtxt @@ -0,0 +1,11 @@ +# Text form of tensorflow.tf2xla.Config proto. +feed { + id { node_name: "x_hold" } + shape { + dim { size: 512 } + dim { size: 1024 } + } +} +fetch { + id { node_name: "x_constant_y_prod" } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfmatmulandadd.config.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfmatmulandadd.config.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..960f7022eaa178061e6b3a80fd549f39b1d5a70e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfmatmulandadd.config.pbtxt @@ -0,0 +1,25 @@ +# Text form of tensorflow.tf2xla.Config proto. +feed { + id { node_name: "x_hold" } + shape { + dim { size: 2 } + dim { size: 2 } + } + name: "x" +} +feed { + id { node_name: "y_hold" } + shape { + dim { size: 2 } + dim { size: 2 } + } + name: "y" +} +fetch { + id { node_name: "x_y_prod" } + name: "x_y_prod" +} +fetch { + id { node_name: "x_y_sum" } + name: "x_y_sum" +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfrandom_uniform.config.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfrandom_uniform.config.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..c62289e1e28786bc53e3507c89a189fe30d714cc --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfrandom_uniform.config.pbtxt @@ -0,0 +1,4 @@ +# Text form of tensorflow.tf2xla.Config proto. +fetch { + id { node_name: "result" } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfscatter.config.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfscatter.config.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..2438bd297c04620c32bc4d61ee0432f9857ee8bd --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfscatter.config.pbtxt @@ -0,0 +1,33 @@ +# Text form of tensorflow.tf2xla.Config proto. +feed { + id { node_name: "indices_0" } + shape { + dim { size: 4 } + dim { size: 1 } + } +} +feed { + id { node_name: "updates_0" } + shape { + dim { size: 4 } + } +} +feed { + id { node_name: "indices_1" } + shape { + dim { size: 4 } + dim { size: 1 } + } +} +feed { + id { node_name: "updates_1" } + shape { + dim { size: 4 } + } +} +fetch { + id { node_name: "result_0" } +} +fetch { + id { node_name: "result_1" } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfsplits.config.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfsplits.config.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..5204a6bc6ede8cba4f674e8625c8eed0f1d29046 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfsplits.config.pbtxt @@ -0,0 +1,18 @@ +# Text form of tensorflow.tf2xla.Config proto. +feed { + id { node_name: "x" } + shape { + dim { size: 2 } + dim { size: 2 } + } +} +feed { + id { node_name: "y" } + shape { + dim { size: 2 } + dim { size: 2 } + } +} +fetch { + id { node_name: "result" } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tftop_k.config.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tftop_k.config.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..5aa678ae0abdf00361e13e11faf9059839bad79d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tftop_k.config.pbtxt @@ -0,0 +1,13 @@ +# Text form of tensorflow.tf2xla.Config proto. +feed { + id { node_name: "x" } + shape { + dim { size: 5 } + } +} +fetch { + id { node_name: "values" } +} +fetch { + id { node_name: "indices" } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfvariable.config.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfvariable.config.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..486b12bc37144f6c17a6a36472f1052c968ea3bb --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfvariable.config.pbtxt @@ -0,0 +1,12 @@ +# Text form of tensorflow.tf2xla.Config proto. +fetch { + id { node_name: "result" } +} + +variable { + node_name: "x" + shape { + dim { size: 1 } + } + type: DT_FLOAT +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfvariable_readonly.config.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfvariable_readonly.config.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..e35798cd33b8c6d9194e8e304f1d249e96d84dd4 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfvariable_readonly.config.pbtxt @@ -0,0 +1,20 @@ +# Text form of tensorflow.tf2xla.Config proto. +fetch { + id { node_name: "result" } +} + +variable { + node_name: "x" + shape { + } + type: DT_FLOAT + readonly: true +} + +variable { + node_name: "y" + shape { + } + type: DT_FLOAT + readonly: true +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfvariable_sequential_updates.config.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfvariable_sequential_updates.config.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..873c79e82831f014eb522e9a75a5f89fea6e83a9 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/test_graph_tfvariable_sequential_updates.config.pbtxt @@ -0,0 +1,15 @@ +# Text form of tensorflow.tf2xla.Config proto. +fetch { + id { node_name: "result" } +} + +variable { + node_name: "x" + type: DT_FLOAT +} + +variable { + node_name: "y" + type: DT_FLOAT + readonly: true +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/tfcompile_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/tfcompile_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..99bb5be71b2d86fca08beaa07dc62651f3941cff --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tests/tfcompile_test.cc @@ -0,0 +1,760 @@ +/* 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 + +#if defined(ENABLE_XLA_THUNK_TEST) +#include "tensorflow/compiler/aot/tests/test_graph_tfadd_thunks.h" +#include "tensorflow/compiler/aot/tests/test_graph_tfadd_with_ckpt_saver_thunks.h" +#include "tensorflow/compiler/aot/tests/test_graph_tfadd_with_ckpt_thunks.h" +#include "tensorflow/compiler/aot/tests/test_graph_tfassert_eq_thunks.h" +#include "tensorflow/compiler/aot/tests/test_graph_tfcond_thunks.h" +#include "tensorflow/compiler/aot/tests/test_graph_tffunction_thunks.h" +#include "tensorflow/compiler/aot/tests/test_graph_tfgather_thunks.h" +#include "tensorflow/compiler/aot/tests/test_graph_tfmatmul_thunks.h" +#include "tensorflow/compiler/aot/tests/test_graph_tfmatmul_with_constant_thunks.h" +#include "tensorflow/compiler/aot/tests/test_graph_tfmatmulandadd_thunks.h" +#include "tensorflow/compiler/aot/tests/test_graph_tfrandom_uniform_thunks.h" +#include "tensorflow/compiler/aot/tests/test_graph_tfscatter_thunks.h" +#include "tensorflow/compiler/aot/tests/test_graph_tfsplits_thunks.h" +#include "tensorflow/compiler/aot/tests/test_graph_tftop_k_thunks.h" +#include "tensorflow/compiler/aot/tests/test_graph_tfvariable_readonly_thunks.h" +#include "tensorflow/compiler/aot/tests/test_graph_tfvariable_sequential_updates_thunks.h" +#include "tensorflow/compiler/aot/tests/test_graph_tfvariable_thunks.h" + +#elif defined(ENABLE_XLA_NANORT_TEST) + +#include "tensorflow/compiler/aot/tests/test_graph_tfadd_nanort.h" +#include "tensorflow/compiler/aot/tests/test_graph_tfadd_with_ckpt_nanort.h" +#include "tensorflow/compiler/aot/tests/test_graph_tfadd_with_ckpt_saver_nanort.h" +#include "tensorflow/compiler/aot/tests/test_graph_tfassert_eq_nanort.h" +#include "tensorflow/compiler/aot/tests/test_graph_tfcond_nanort.h" +#include "tensorflow/compiler/aot/tests/test_graph_tffunction_nanort.h" +#include "tensorflow/compiler/aot/tests/test_graph_tfgather_nanort.h" +#include "tensorflow/compiler/aot/tests/test_graph_tfmatmul_nanort.h" +#include "tensorflow/compiler/aot/tests/test_graph_tfmatmul_with_constant_nanort.h" +#include "tensorflow/compiler/aot/tests/test_graph_tfmatmulandadd_nanort.h" +#include "tensorflow/compiler/aot/tests/test_graph_tfrandom_uniform_nanort.h" +#include "tensorflow/compiler/aot/tests/test_graph_tfscatter_nanort.h" +#include "tensorflow/compiler/aot/tests/test_graph_tfsplits_nanort.h" +#include "tensorflow/compiler/aot/tests/test_graph_tftop_k_nanort.h" +#include "tensorflow/compiler/aot/tests/test_graph_tfvariable_nanort.h" +#include "tensorflow/compiler/aot/tests/test_graph_tfvariable_readonly_nanort.h" +#include "tensorflow/compiler/aot/tests/test_graph_tfvariable_sequential_updates_nanort.h" + +#endif + +#include "tensorflow/compiler/tf2xla/xla_compiled_cpu_function.h" +#include "xla/hlo/testlib/test.h" +#include "xla/shape.h" +#include "xla/shape_util.h" +#include "xla/tsl/platform/statusor.h" +#include "xla/tsl/platform/threadpool.h" +#include "tensorflow/core/platform/test.h" +#include "tensorflow/core/platform/types.h" + +#define EIGEN_USE_THREADS +#define EIGEN_USE_CUSTOM_THREAD_POOL + +#include "unsupported/Eigen/CXX11/Tensor" // from @eigen_archive + +namespace tensorflow { +namespace tfcompile { +namespace { + +TEST(TFCompileTest, Add) { + AddComp add; + EXPECT_EQ(add.arg0_data(), add.arg_data(0)); + EXPECT_EQ(add.arg1_data(), add.arg_data(1)); + add.arg0() = 1; + add.arg1() = 2; + EXPECT_TRUE(add.Run()); + EXPECT_EQ(add.error_msg(), ""); + EXPECT_EQ(add.result0(), 3); + EXPECT_EQ(add.result0_data()[0], 3); + EXPECT_EQ(add.result0_data(), add.result_data(0)); + + add.arg0_data()[0] = 123; + add.arg1_data()[0] = 456; + EXPECT_TRUE(add.Run()); + EXPECT_EQ(add.error_msg(), ""); + EXPECT_EQ(add.result0(), 579); + EXPECT_EQ(add.result0_data()[0], 579); + EXPECT_EQ(add.result0_data(), add.result_data(0)); + + const AddComp& add_const = add; + EXPECT_EQ(add_const.error_msg(), ""); + EXPECT_EQ(add_const.arg0(), 123); + EXPECT_EQ(add_const.arg0_data()[0], 123); + EXPECT_EQ(add_const.arg0_data(), add.arg_data(0)); + EXPECT_EQ(add_const.arg1(), 456); + EXPECT_EQ(add_const.arg1_data()[0], 456); + EXPECT_EQ(add_const.arg1_data(), add.arg_data(1)); + EXPECT_EQ(add_const.result0(), 579); + EXPECT_EQ(add_const.result0_data()[0], 579); + EXPECT_EQ(add_const.result0_data(), add_const.result_data(0)); +} + +// Run tests that use set_argN_data separately, to avoid accidentally re-using +// non-existent buffers. +TEST(TFCompileTest, Add_SetArg) { + AddComp add( + XlaCompiledCpuFunction::AllocMode::RESULTS_PROFILES_AND_TEMPS_ONLY); + + alignas(32) int32_t arg_x = 10; + alignas(32) int32_t arg_y = 32; + add.set_arg0_data(&arg_x); + add.set_arg1_data(&arg_y); + EXPECT_EQ(add.arg0_data(), add.arg_data(0)); + EXPECT_EQ(add.arg1_data(), add.arg_data(1)); + + EXPECT_TRUE(add.Run()); + EXPECT_EQ(add.error_msg(), ""); + EXPECT_EQ(add.result0(), 42); + EXPECT_EQ(add.result0_data()[0], 42); + EXPECT_EQ(add.result0_data(), add.result_data(0)); +} + +TEST(TFCompileTest, AddWithCkpt) { + AddWithCkptComp add; + EXPECT_EQ(add.arg0_data(), add.arg_data(0)); + + add.arg0() = 1; + EXPECT_TRUE(add.Run()); + EXPECT_EQ(add.error_msg(), ""); + EXPECT_EQ(add.result0(), 43); + EXPECT_EQ(add.result0_data()[0], 43); + EXPECT_EQ(add.result0_data(), add.result_data(0)); + + add.arg0_data()[0] = 111; + EXPECT_TRUE(add.Run()); + EXPECT_EQ(add.error_msg(), ""); + EXPECT_EQ(add.result0(), 153); + EXPECT_EQ(add.result0_data()[0], 153); + EXPECT_EQ(add.result0_data(), add.result_data(0)); + + const AddWithCkptComp& add_const = add; + EXPECT_EQ(add_const.error_msg(), ""); + EXPECT_EQ(add_const.arg0(), 111); + EXPECT_EQ(add_const.arg0_data()[0], 111); + EXPECT_EQ(add_const.arg0_data(), add_const.arg_data(0)); + EXPECT_EQ(add_const.result0(), 153); + EXPECT_EQ(add_const.result0_data()[0], 153); + EXPECT_EQ(add_const.result0_data(), add_const.result_data(0)); +} + +TEST(TFCompileTest, AddWithCkptSaver) { + AddWithCkptSaverComp add; + EXPECT_EQ(add.arg0_data(), add.arg_data(0)); + + add.arg0() = 1; + EXPECT_TRUE(add.Run()); + EXPECT_EQ(add.error_msg(), ""); + EXPECT_EQ(add.result0(), 43); + EXPECT_EQ(add.result0_data()[0], 43); + EXPECT_EQ(add.result0_data(), add.result_data(0)); + + add.arg0_data()[0] = 111; + EXPECT_TRUE(add.Run()); + EXPECT_EQ(add.error_msg(), ""); + EXPECT_EQ(add.result0(), 153); + EXPECT_EQ(add.result0_data()[0], 153); + EXPECT_EQ(add.result0_data(), add.result_data(0)); + + const AddWithCkptSaverComp& add_const = add; + EXPECT_EQ(add_const.error_msg(), ""); + EXPECT_EQ(add_const.arg0(), 111); + EXPECT_EQ(add_const.arg0_data()[0], 111); + EXPECT_EQ(add_const.arg0_data(), add_const.arg_data(0)); + EXPECT_EQ(add_const.result0(), 153); + EXPECT_EQ(add_const.result0_data()[0], 153); + EXPECT_EQ(add_const.result0_data(), add_const.result_data(0)); +} + +TEST(TFCompileTest, Cond) { + CondComp cond; + EXPECT_EQ(cond.arg0_data(), cond.arg_data(0)); + EXPECT_EQ(cond.arg1_data(), cond.arg_data(1)); + EXPECT_EQ(cond.arg2_data(), cond.arg_data(2)); + cond.arg1() = 10; + cond.arg2() = 20; + { + cond.arg0() = true; + const int32_t expected_result = cond.arg1(); + EXPECT_TRUE(cond.Run()); + EXPECT_EQ(cond.error_msg(), ""); + EXPECT_EQ(cond.result0(), expected_result); + EXPECT_EQ(cond.result0_data()[0], expected_result); + EXPECT_EQ(cond.result0_data(), cond.result_data(0)); + } + { + cond.arg0() = false; + const int32_t expected_result = cond.arg2(); + EXPECT_TRUE(cond.Run()); + EXPECT_EQ(cond.error_msg(), ""); + EXPECT_EQ(cond.result0(), expected_result); + EXPECT_EQ(cond.result0_data()[0], expected_result); + EXPECT_EQ(cond.result0_data(), cond.result_data(0)); + } +} + +TEST(TFCompileTest, Gather) { + GatherComp gather; + EXPECT_EQ(gather.arg0_data(), gather.arg_data(0)); + EXPECT_EQ(gather.arg1_data(), gather.arg_data(1)); + + // Successful gather. + { + const float params[4] = {1, 2, 3, 4}; + std::copy(params + 0, params + 4, gather.arg0_data()); + const int32_t indices[2] = {1, 3}; + std::copy(indices + 0, indices + 2, gather.arg1_data()); + EXPECT_TRUE(gather.Run()); + EXPECT_EQ(gather.error_msg(), ""); + const float results[2] = {2, 4}; + for (int i = 0; i < 2; ++i) { + EXPECT_EQ(gather.result0(i), results[i]); + EXPECT_EQ(gather.result0_data()[i], results[i]); + } + EXPECT_EQ(gather.result0_data(), gather.result_data(0)); + + const GatherComp& gather_const = gather; + EXPECT_EQ(gather_const.error_msg(), ""); + for (int i = 0; i < 4; ++i) { + EXPECT_EQ(gather_const.arg0(i), params[i]); + EXPECT_EQ(gather_const.arg0_data()[i], params[i]); + } + EXPECT_EQ(gather_const.arg0_data(), gather_const.arg_data(0)); + for (int i = 0; i < 2; ++i) { + EXPECT_EQ(gather_const.arg1(i), indices[i]); + EXPECT_EQ(gather_const.arg1_data()[i], indices[i]); + } + EXPECT_EQ(gather_const.arg1_data(), gather_const.arg_data(1)); + for (int i = 0; i < 2; ++i) { + EXPECT_EQ(gather_const.result0(i), results[i]); + EXPECT_EQ(gather_const.result0_data()[i], results[i]); + } + EXPECT_EQ(gather_const.result0_data(), gather.result_data(0)); + } +} + +TEST(TFCompileTest, MatMul2) { + Eigen::ThreadPool tp(2); + Eigen::ThreadPoolDevice device(&tp, tp.NumThreads()); + + foo::bar::MatMulComp matmul; + matmul.set_thread_pool(&device); + EXPECT_EQ(matmul.arg0_data(), matmul.arg_data(0)); + EXPECT_EQ(matmul.arg1_data(), matmul.arg_data(1)); + + // Test using the argN() methods. + { + matmul.arg0(0, 0) = 1; + matmul.arg0(0, 1) = 2; + matmul.arg0(0, 2) = 3; + matmul.arg0(1, 0) = 4; + matmul.arg0(1, 1) = 5; + matmul.arg0(1, 2) = 6; + + matmul.arg1(0, 0) = 7; + matmul.arg1(0, 1) = 8; + matmul.arg1(1, 0) = 9; + matmul.arg1(1, 1) = 10; + matmul.arg1(2, 0) = 11; + matmul.arg1(2, 1) = 12; + + EXPECT_TRUE(matmul.Run()); + EXPECT_EQ(matmul.error_msg(), ""); + const float results[4] = {58, 64, 139, 154}; + for (int i = 0; i < 4; ++i) { + EXPECT_EQ(matmul.result0(i / 2, i % 2), results[i]); + EXPECT_EQ(matmul.result0_data()[i], results[i]); + } + EXPECT_EQ(matmul.result0_data(), matmul.result_data(0)); + } + + // Test using the argN_data() methods. + { + const float args[12] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120}; + std::copy(args + 0, args + 6, matmul.arg0_data()); + std::copy(args + 6, args + 12, matmul.arg1_data()); + EXPECT_TRUE(matmul.Run()); + EXPECT_EQ(matmul.error_msg(), ""); + const float results[4] = {5800, 6400, 13900, 15400}; + for (int i = 0; i < 4; ++i) { + EXPECT_EQ(matmul.result0(i / 2, i % 2), results[i]); + EXPECT_EQ(matmul.result0_data()[i], results[i]); + } + EXPECT_EQ(matmul.result0_data(), matmul.result_data(0)); + + const foo::bar::MatMulComp& matmul_const = matmul; + EXPECT_EQ(matmul_const.error_msg(), ""); + for (int i = 0; i < 6; ++i) { + EXPECT_EQ(matmul_const.arg0(i / 3, i % 3), args[i]); + EXPECT_EQ(matmul_const.arg0_data()[i], args[i]); + } + EXPECT_EQ(matmul_const.arg0_data(), matmul.arg_data(0)); + for (int i = 0; i < 6; ++i) { + EXPECT_EQ(matmul_const.arg1(i / 2, i % 2), args[i + 6]); + EXPECT_EQ(matmul_const.arg1_data()[i], args[i + 6]); + } + EXPECT_EQ(matmul_const.arg1_data(), matmul.arg_data(1)); + for (int i = 0; i < 4; ++i) { + EXPECT_EQ(matmul_const.result0(i / 2, i % 2), results[i]); + EXPECT_EQ(matmul_const.result0_data()[i], results[i]); + } + EXPECT_EQ(matmul_const.result0_data(), matmul.result_data(0)); + } +} + +// Run tests that use set_argN_data separately, to avoid accidentally re-using +// non-existent buffers. +TEST(TFCompileTest, MatMul2_SetArg) { + Eigen::ThreadPool tp(2); + Eigen::ThreadPoolDevice device(&tp, tp.NumThreads()); + + foo::bar::MatMulComp matmul( + XlaCompiledCpuFunction::AllocMode::RESULTS_PROFILES_AND_TEMPS_ONLY); + matmul.set_thread_pool(&device); + + // Test using the set_argN_data() methods. + + alignas(32) float arg0[2][3] = {{1, 2, 3}, {4, 5, 6}}; + alignas(32) float arg1[3][2] = {{7, 8}, {9, 10}, {11, 12}}; + matmul.set_arg0_data(&arg0); + matmul.set_arg1_data(&arg1); + EXPECT_EQ(matmul.arg0_data(), matmul.arg_data(0)); + EXPECT_EQ(matmul.arg1_data(), matmul.arg_data(1)); + + EXPECT_TRUE(matmul.Run()); + EXPECT_EQ(matmul.error_msg(), ""); + const float results[4] = {58, 64, 139, 154}; + for (int i = 0; i < 4; ++i) { + EXPECT_EQ(matmul.result0(i / 2, i % 2), results[i]); + EXPECT_EQ(matmul.result0_data()[i], results[i]); + } + EXPECT_EQ(matmul.result0_data(), matmul.result_data(0)); +} + +TEST(TFCompileTest, MatMulAndAdd1) { + Eigen::ThreadPool tp(1); + Eigen::ThreadPoolDevice device(&tp, tp.NumThreads()); + + ::foo::bar::MatMulAndAddComp muladd; + muladd.set_thread_pool(&device); + EXPECT_EQ(muladd.arg0_data(), muladd.arg_data(0)); + EXPECT_EQ(muladd.arg1_data(), muladd.arg_data(1)); + + // Test methods with positional args and results. + { + const float args[8] = {1, 2, 3, 4, 5, 6, 7, 8}; + std::copy(args + 0, args + 4, muladd.arg0_data()); + std::copy(args + 4, args + 8, muladd.arg1_data()); + EXPECT_TRUE(muladd.Run()); + EXPECT_EQ(muladd.error_msg(), ""); + const float results0[4] = {19, 22, 43, 50}; + const float results1[4] = {6, 8, 10, 12}; + for (int i = 0; i < 4; ++i) { + EXPECT_EQ(muladd.result0(i / 2, i % 2), results0[i]); + EXPECT_EQ(muladd.result0_data()[i], results0[i]); + EXPECT_EQ(muladd.result1(i / 2, i % 2), results1[i]); + EXPECT_EQ(muladd.result1_data()[i], results1[i]); + } + EXPECT_EQ(muladd.result0_data(), muladd.result_data(0)); + EXPECT_EQ(muladd.result1_data(), muladd.result_data(1)); + + const ::foo::bar::MatMulAndAddComp& muladd_const = muladd; + EXPECT_EQ(muladd_const.error_msg(), ""); + for (int i = 0; i < 4; ++i) { + EXPECT_EQ(muladd_const.arg0(i / 2, i % 2), args[i]); + EXPECT_EQ(muladd_const.arg0_data()[i], args[i]); + } + EXPECT_EQ(muladd_const.arg0_data(), muladd.arg_data(0)); + for (int i = 0; i < 4; ++i) { + EXPECT_EQ(muladd_const.arg1(i / 2, i % 2), args[i + 4]); + EXPECT_EQ(muladd_const.arg1_data()[i], args[i + 4]); + } + EXPECT_EQ(muladd_const.arg1_data(), muladd.arg_data(1)); + for (int i = 0; i < 4; ++i) { + EXPECT_EQ(muladd_const.result0(i / 2, i % 2), results0[i]); + EXPECT_EQ(muladd_const.result0_data()[i], results0[i]); + EXPECT_EQ(muladd_const.result1(i / 2, i % 2), results1[i]); + EXPECT_EQ(muladd_const.result1_data()[i], results1[i]); + } + EXPECT_EQ(muladd_const.result0_data(), muladd.result_data(0)); + EXPECT_EQ(muladd_const.result1_data(), muladd.result_data(1)); + } + + // Test methods with named args and results. + { + const float args[8] = {10, 20, 30, 40, 50, 60, 70, 80}; + std::copy(args + 0, args + 4, muladd.arg_x_data()); + std::copy(args + 4, args + 8, muladd.arg_y_data()); + EXPECT_TRUE(muladd.Run()); + EXPECT_EQ(muladd.error_msg(), ""); + const float results0[4] = {1900, 2200, 4300, 5000}; + const float results1[4] = {60, 80, 100, 120}; + for (int i = 0; i < 4; ++i) { + EXPECT_EQ(muladd.result_x_y_prod(i / 2, i % 2), results0[i]); + EXPECT_EQ(muladd.result_x_y_prod_data()[i], results0[i]); + EXPECT_EQ(muladd.result_x_y_sum(i / 2, i % 2), results1[i]); + EXPECT_EQ(muladd.result_x_y_sum_data()[i], results1[i]); + } + EXPECT_EQ(muladd.result_x_y_prod_data(), muladd.result_data(0)); + EXPECT_EQ(muladd.result_x_y_sum_data(), muladd.result_data(1)); + + // Test const methods. + const ::foo::bar::MatMulAndAddComp& muladd_const = muladd; + EXPECT_EQ(muladd_const.error_msg(), ""); + for (int i = 0; i < 4; ++i) { + EXPECT_EQ(muladd_const.arg_x(i / 2, i % 2), args[i]); + EXPECT_EQ(muladd_const.arg_x_data()[i], args[i]); + } + EXPECT_EQ(muladd_const.arg_x_data(), muladd.arg_data(0)); + for (int i = 0; i < 4; ++i) { + EXPECT_EQ(muladd_const.arg_y(i / 2, i % 2), args[i + 4]); + EXPECT_EQ(muladd_const.arg_y_data()[i], args[i + 4]); + } + EXPECT_EQ(muladd_const.arg_y_data(), muladd.arg_data(1)); + for (int i = 0; i < 4; ++i) { + EXPECT_EQ(muladd_const.result_x_y_prod(i / 2, i % 2), results0[i]); + EXPECT_EQ(muladd_const.result_x_y_prod_data()[i], results0[i]); + EXPECT_EQ(muladd_const.result_x_y_sum(i / 2, i % 2), results1[i]); + EXPECT_EQ(muladd_const.result_x_y_sum_data()[i], results1[i]); + } + EXPECT_EQ(muladd_const.result_x_y_prod_data(), muladd.result_data(0)); + EXPECT_EQ(muladd_const.result_x_y_sum_data(), muladd.result_data(1)); + } +} + +TEST(TFCompileTest, Function) { + // The function is equivalent to an addition + FunctionComp add_fn; + EXPECT_EQ(add_fn.arg0_data(), add_fn.arg_data(0)); + EXPECT_EQ(add_fn.arg1_data(), add_fn.arg_data(1)); + + add_fn.arg0() = 1; + add_fn.arg1() = 2; + EXPECT_TRUE(add_fn.Run()); + EXPECT_EQ(add_fn.error_msg(), ""); + EXPECT_EQ(add_fn.result0(), 3); + EXPECT_EQ(add_fn.result0_data()[0], 3); + EXPECT_EQ(add_fn.result0_data(), add_fn.result_data(0)); +} + +TEST(TFCompileTest, Splits) { + Eigen::ThreadPool tp(1); + Eigen::ThreadPoolDevice device(&tp, tp.NumThreads()); + + SplitsComp fn; + + fn.set_thread_pool(&device); + // x = [[1, 2], [3, 4]] + fn.arg0(0, 0) = 1; + fn.arg0(0, 1) = 2; + fn.arg0(1, 0) = 3; + fn.arg0(1, 1) = 4; + + // y = [[10, 20], [30, 40]] + fn.arg1(0, 0) = 10; + fn.arg1(0, 1) = 20; + fn.arg1(1, 0) = 30; + fn.arg1(1, 1) = 40; + EXPECT_TRUE(fn.Run()); + EXPECT_EQ(fn.error_msg(), ""); + const float expected[] = {7.86375557e+10, 1.34274679e+11, 1.92741717e+12, + 3.29964742e+12}; + EXPECT_NEAR(expected[0], fn.result0(0, 0), 1e4); + EXPECT_NEAR(expected[1], fn.result0(0, 1), 1e4); + EXPECT_NEAR(expected[2], fn.result0(1, 0), 1e4); + EXPECT_NEAR(expected[3], fn.result0(1, 1), 1e4); +} + +TEST(TFCompileTest, TopK) { + Eigen::ThreadPool tp(1); + Eigen::ThreadPoolDevice device(&tp, tp.NumThreads()); + + TopKComp fn; + + fn.set_thread_pool(&device); + // x = [4, 1, 4, 4, 3] + fn.arg0(0) = 4; + fn.arg0(1) = 1; + fn.arg0(2) = 4; + fn.arg0(3) = 4; + fn.arg0(4) = 3; + + EXPECT_TRUE(fn.Run()); + EXPECT_EQ(fn.error_msg(), ""); + const int32_t expected_values[] = {4, 4}; + const int32_t expected_indices[] = {0, 2}; + EXPECT_EQ(expected_values[0], fn.result0(0)); + EXPECT_EQ(expected_values[1], fn.result0(1)); + EXPECT_EQ(expected_indices[0], fn.result1(0)); + EXPECT_EQ(expected_indices[1], fn.result1(1)); +} + +TEST(TFCompileTest, VariableReadonly) { + Eigen::ThreadPool tp(1); + Eigen::ThreadPoolDevice device(&tp, tp.NumThreads()); + + VariableReadonlyComp fn; + + alignas(32) float x = 23; + fn.set_var_x_data(&x); + + fn.set_thread_pool(&device); + EXPECT_TRUE(fn.Run()); + EXPECT_EQ(fn.error_msg(), ""); + + EXPECT_EQ(fn.result0(), 65); + EXPECT_EQ(fn.var_x(), 23); +} + +TEST(TFCompileTest, Variable) { + Eigen::ThreadPool tp(1); + Eigen::ThreadPoolDevice device(&tp, tp.NumThreads()); + + VariableComp fn; + + alignas(32) float x = 23; + fn.set_var_x_data(&x); + + fn.set_thread_pool(&device); + EXPECT_TRUE(fn.Run()); + EXPECT_EQ(fn.error_msg(), ""); + EXPECT_EQ(fn.result0(0, 0), 23); + EXPECT_EQ(fn.result0(1, 0), 65); + EXPECT_EQ(fn.var_x(), 65); + + EXPECT_EQ(fn.var_x_data(), &x); + EXPECT_EQ(x, 65); + EXPECT_TRUE(fn.Run()); + EXPECT_EQ(fn.error_msg(), ""); + EXPECT_EQ(fn.result0(0, 0), 65); + EXPECT_EQ(fn.result0(1, 0), 107); + EXPECT_EQ(fn.var_x(), 107); +} + +TEST(TFCompileTest, VariableSequentialUpdates) { + Eigen::ThreadPool tp(1); + Eigen::ThreadPoolDevice device(&tp, tp.NumThreads()); + + // This implements the recursion: + // x[0] = 2.0 + // x[n+1] = x[n] - 0.1*(x[n-1] + y) + VariableSequentialUpdatesComp fn; + fn.var_x() = 2; + *const_cast(fn.var_y_data()) = 1; + + fn.set_thread_pool(&device); + // First calculate x[3] + EXPECT_TRUE(fn.Run()); + EXPECT_EQ(fn.error_msg(), ""); + EXPECT_NEAR(fn.var_x(), 1.187f, 1e-6); + + alignas(32) const float y = 1; + fn.set_var_y_data(&y); + + // Now const_cast(fn.var_y_data()) is not longer legal since we've + // set the buffer to point to a constant location. + + // Then calculate x[6] + EXPECT_TRUE(fn.Run()); + EXPECT_EQ(fn.error_msg(), ""); + EXPECT_NEAR(fn.var_x(), 0.594322f, 1e-6); +} + +TEST(TFCompileTest, VariableSequentialUpdatesNoAlloc) { + Eigen::ThreadPool tp(1); + Eigen::ThreadPoolDevice device(&tp, tp.NumThreads()); + + // This implements the recursion: + // x[0] = 2.0 + // x[n+1] = x[n] - 0.1*(x[n-1] + 1.0) + VariableSequentialUpdatesComp fn( + XlaCompiledCpuFunction::AllocMode::RESULTS_PROFILES_AND_TEMPS_ONLY); + + alignas(32) float x = 2; + alignas(32) float y = 1; + fn.set_var_x_data(&x); + fn.set_var_y_data(&y); + + fn.set_thread_pool(&device); + // First calculate x[3] + EXPECT_TRUE(fn.Run()); + EXPECT_EQ(fn.error_msg(), ""); + EXPECT_NEAR(x, 1.187f, 1e-6); + + // Then calculate x[6] + EXPECT_TRUE(fn.Run()); + EXPECT_EQ(fn.error_msg(), ""); + EXPECT_NEAR(x, 0.594322f, 1e-6); +} + +TEST(TFCompileTest, MatMulWithConstants) { + Eigen::ThreadPool tp(2); + Eigen::ThreadPoolDevice device(&tp, tp.NumThreads()); + + foo::bar::MatMulWithConstantComp matmul; + matmul.set_thread_pool(&device); + EXPECT_EQ(matmul.arg0_data(), matmul.arg_data(0)); + + // Test using the argN() methods. + { + for (int i = 0; i < 512; ++i) { + for (int j = 0; j < 1024; ++j) { + matmul.arg0(i, j) = 1; + } + } + + EXPECT_TRUE(matmul.Run()); + EXPECT_EQ(matmul.error_msg(), ""); + std::vector results(512 * 256, 1024); + for (int i = 0; i < results.size(); ++i) { + ASSERT_EQ(matmul.result0(i / 512, i % 256), results[i]); + ASSERT_EQ(matmul.result0_data()[i], results[i]); + } + EXPECT_EQ(matmul.result0_data(), matmul.result_data(0)); + } +} + +TEST(TFCompileTest, RandomUniform) { + RandomUniformComp random_uniform; + EXPECT_TRUE(random_uniform.Run()); + EXPECT_EQ(random_uniform.error_msg(), ""); + EXPECT_LE(random_uniform.result0(), 5.0); + EXPECT_GE(random_uniform.result0(), 0.0); +} +TEST(TFCompileTest, Scatter) { + ScatterComp scatter; + + const std::vector indices0 = {4, 3, 1, 7}; + const std::vector updates0 = {9.0, 10.0, 11.0, 12.0}; + + const std::vector indices1 = {2, 5, 3, 6}; + const std::vector updates1 = {17.0, 2.0, 5.0, -1.0}; + + std::memcpy(scatter.arg0_data(), indices0.data(), + indices0.size() * sizeof(int32_t)); + std::memcpy(scatter.arg1_data(), updates0.data(), + updates0.size() * sizeof(float)); + + std::memcpy(scatter.arg2_data(), indices1.data(), + indices1.size() * sizeof(int32_t)); + std::memcpy(scatter.arg3_data(), updates1.data(), + updates1.size() * sizeof(float)); + + EXPECT_TRUE(scatter.Run()); + EXPECT_EQ(scatter.error_msg(), ""); + + const std::vector expected0 = {0, 11, 0, 10, 9, 0, 0, 12}; + const std::vector expected1 = {0, 0, 17, 5, 0, 2, -1, 0}; + + // NOTE(basioli): Shape is hardcoded to 8 in tensorflow config. + EXPECT_EQ(scatter.result0_count(), expected0.size()); + + for (int i = 0; i < scatter.result0_count(); ++i) { + EXPECT_EQ(scatter.result0(i), expected0[i]); + } + + // NOTE(basioli): Shape is hardcoded to 8 in tensorflow config. + EXPECT_EQ(scatter.result1_count(), expected1.size()); + + for (int i = 0; i < scatter.result1_count(); ++i) { + EXPECT_EQ(scatter.result1(i), expected1[i]) << "i: " << i; + } +} + +TEST(TFCompileTest, AssertEqAndReturnDiff) { + // Assert is converted into a no-op in XLA, so there is no failure even if + // the two args are different. + AssertComp assert; + EXPECT_EQ(assert.arg0_data(), assert.arg_data(0)); + EXPECT_EQ(assert.arg1_data(), assert.arg_data(1)); + + assert.arg0() = 2; + assert.arg1() = 1; + const int32_t expected_result = assert.arg0() - assert.arg1(); + EXPECT_TRUE(assert.Run()); + EXPECT_EQ(assert.error_msg(), ""); + EXPECT_EQ(assert.result0(), expected_result); + EXPECT_EQ(assert.result0_data()[0], expected_result); + EXPECT_EQ(assert.result0_data(), assert.result_data(0)); +} + +TEST(TFCompileTest, LookupNameIndex) { + // add doesn't have any names defined in its config. + AddComp add; + EXPECT_FALSE(add.HasNameIndices()); + + // muladd has names defined for all feeds and fetches. + ::foo::bar::MatMulAndAddComp muladd; + EXPECT_TRUE(muladd.HasNameIndices()); + + EXPECT_EQ(muladd.LookupArgIndex("x"), 0); + EXPECT_EQ(muladd.LookupArgIndex("y"), 1); + EXPECT_EQ(muladd.LookupArgIndex(""), -1); + EXPECT_EQ(muladd.LookupArgIndex("x_hold"), -1); + EXPECT_EQ(muladd.LookupArgIndex("y_hold"), -1); + EXPECT_EQ(muladd.LookupArgIndex("x_y_prod"), -1); + EXPECT_EQ(muladd.LookupArgIndex("x_y_sum"), -1); + + EXPECT_EQ(muladd.LookupResultIndex("x_y_prod"), 0); + EXPECT_EQ(muladd.LookupResultIndex("x_y_sum"), 1); + EXPECT_EQ(muladd.LookupResultIndex(""), -1); + EXPECT_EQ(muladd.LookupResultIndex("x"), -1); + EXPECT_EQ(muladd.LookupResultIndex("y"), -1); + EXPECT_EQ(muladd.LookupResultIndex("x_hold"), -1); + EXPECT_EQ(muladd.LookupResultIndex("y_hold"), -1); +} + +TEST(TFCompileTest, ProgramShape) { + using xla::ShapeUtil; + const xla::Shape f32_2x2 = ShapeUtil::MakeShape(xla::F32, {2, 2}); + + // add doesn't have the program shape defined. + AddComp add; + ASSERT_TRUE(add.ProgramShape() == nullptr); + + // muladd has the program shape defined. + ::foo::bar::MatMulAndAddComp muladd; + const xla::ProgramShapeProto* muladd_shape = muladd.ProgramShape(); + ASSERT_TRUE(muladd_shape != nullptr); + ASSERT_EQ(muladd_shape->parameters_size(), 2); + TF_ASSERT_OK_AND_ASSIGN(xla::Shape muladd_arg0, + xla::Shape::FromProto(muladd_shape->parameters(0))); + TF_ASSERT_OK_AND_ASSIGN(xla::Shape muladd_arg1, + xla::Shape::FromProto(muladd_shape->parameters(1))); + EXPECT_TRUE(ShapeUtil::Compatible(muladd_arg0, f32_2x2)); + EXPECT_TRUE(ShapeUtil::Compatible(muladd_arg1, f32_2x2)); + + TF_ASSERT_OK_AND_ASSIGN(xla::Shape muladd_result, + xla::Shape::FromProto(muladd_shape->result())); + ASSERT_EQ(muladd_result.element_type(), xla::TUPLE); + ASSERT_EQ(ShapeUtil::TupleElementCount(muladd_result), 2); + const xla::Shape& muladd_result0 = + ShapeUtil::GetTupleElementShape(muladd_result, 0); + EXPECT_TRUE(ShapeUtil::Compatible(muladd_result0, f32_2x2)); + const xla::Shape& muladd_result1 = + ShapeUtil::GetTupleElementShape(muladd_result, 1); + EXPECT_TRUE(ShapeUtil::Compatible(muladd_result1, f32_2x2)); +} + +} // namespace +} // namespace tfcompile +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/tfcompile.bzl b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tfcompile.bzl new file mode 100644 index 0000000000000000000000000000000000000000..bbe32b44f5674a4fda6e23d37a21efd25dc4f8da --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tfcompile.bzl @@ -0,0 +1,591 @@ +"""Build macro that compiles a TensorFlow graph into a cc_library. + +To use from your BUILD file, add the following line to load the macro: + +load("//tensorflow/compiler/aot:tfcompile.bzl", "tf_library") + +Then call the macro like this: + +tf_library( + name = "test_graph_tfmatmul", + config = "test_graph_tfmatmul.config.pbtxt", + cpp_class = "MatMulComp", + graph = ":test_graph_tfmatmul.pb", +) +""" + +load("@rules_cc//cc:cc_binary.bzl", "cc_binary") +load("@rules_cc//cc:cc_library.bzl", "cc_library") +load( + "//tensorflow:tensorflow.bzl", + "if_android", + "if_google", + "if_oss", + "tf_cc_test", + "tf_copts", +) +load("//tensorflow:tensorflow.default.bzl", "tfcompile_dfsan_abilists", "tfcompile_dfsan_enabled", "tfcompile_friends", "tfcompile_target_cpu") + +visibility(tfcompile_friends()) + +def _tfcompile_model_library_rule_impl(ctx): + header_file = ctx.outputs.header_out + metadata_object_file = ctx.actions.declare_file("%s_tfcompile_metadata.o" % ctx.attr.model_name) + function_object_file = ctx.actions.declare_file("%s_tfcompile_function.o" % ctx.attr.model_name) + constant_buffers_object_file = ctx.actions.declare_file("%s_tfcompile_constant_buffers.o" % ctx.attr.model_name) + session_module_pb = ctx.actions.declare_file("%s_session_module.pb" % ctx.attr.model_name) + out_files = [header_file, metadata_object_file, function_object_file, constant_buffers_object_file, session_module_pb] + compiler_log_file = None + if ctx.attr.gen_compiler_log: + compiler_log_file = ctx.actions.declare_file("%s_compiler.log" % ctx.attr.model_name) + out_files.append(compiler_log_file) + + output_dict = {} + output_dict["header_files"] = [header_file] + output_dict["object_files"] = [metadata_object_file, function_object_file, constant_buffers_object_file] + if compiler_log_file: + output_dict["log_files"] = [compiler_log_file] + + output_flags = [ + "--out_header=" + header_file.path, + "--out_metadata_object=" + metadata_object_file.path, + "--out_function_object=" + function_object_file.path, + "--out_constant_buffers_object=" + constant_buffers_object_file.path, + "--out_session_module=" + session_module_pb.path, + ] + + additional_xla_flags = ctx.attr.xla_flags + + tfcompile_env = { + "XLA_FLAGS": ("--xla_cpu_enable_fast_math=true " + + "--xla_cpu_fast_math_honor_nans=false " + + "--xla_cpu_fast_math_honor_infs=false " + + "--xla_cpu_fast_math_honor_functions=false " + + "--xla_cpu_fast_math_honor_division=false " + + "--xla_cpu_enable_fast_min_max=true " + + "--xla_cpu_experimental_ynn_fusion_type= " + + additional_xla_flags + " " + + "$${XLA_FLAGS:-}' "), + "CUDA_VISIBLE_DEVICES": "", + } + + dfsan_flags = [] + dfsan_deps = [] + + # DFSan is only supported on linux. + if ctx.attr.is_linux and ctx.attr.dfsan: + dfsan_flags = [ + "--sanitize_dataflow", + "--sanitize_abilists_dataflow=" + ",".join([f.path for f in ctx.files.dfsan_abilists]), + ] + dfsan_deps = ctx.files.dfsan_abilists + + cpu_flags = ["--target_cpu=" + ctx.attr.target_cpu] if ctx.attr.target_cpu else [] + + flags = [ + "--graph=" + ctx.file.tfcompile_graph.path, + "--config=" + ctx.file.tfcompile_config.path, + "--entry_point=" + ctx.attr.entry_point, + "--cpp_class=" + ctx.attr.cpp_class, + "--target_triple=" + ctx.attr.target_triple, + ] + cpu_flags + output_flags + ctx.attr.extra_flags + dfsan_flags + + post_command = "" + if ctx.attr.gen_compiler_log: + post_command += " --vmodule=cpu_compiler=5 2> >(tee -a " + compiler_log_file.path + " >&2) " + + full_cmd = ( + ctx.executable.tfcompile_tool.path + " " + " ".join(flags) + " " + ctx.attr.flags + post_command + ) + ctx.actions.run_shell( + inputs = ctx.files.srcs, + outputs = out_files, + tools = [ctx.executable.tfcompile_tool] + dfsan_deps, + env = tfcompile_env, + command = full_cmd, + progress_message = "tfcompile for model %s (%s)" % (ctx.attr.model_name, ctx.file.tfcompile_graph.path), + mnemonic = "TensorflowCompile", + ) + return [ + DefaultInfo( + files = depset(out_files), + ), + OutputGroupInfo(**output_dict), + ] + +# Use tf_library macro instead of using this rule directly. +_tfcompile_model_library = rule( + implementation = _tfcompile_model_library_rule_impl, + attrs = { + "model_name": attr.string(), + "srcs": attr.label_list(mandatory = True, allow_files = True), + "header_out": attr.output(), + "cmd": attr.string(), + "tfcompile_tool": attr.label(cfg = "exec", executable = True, allow_files = True), + "tfcompile_graph": attr.label(allow_single_file = True), + "tfcompile_config": attr.label(allow_single_file = True), + "entry_point": attr.string(), + "cpp_class": attr.string(), + "target_triple": attr.string(), + "target_cpu": attr.string(), + # The tfcompile_flags passed into tf_library macro may be a string + # containing multiple flags (and there are cases that do this). + "flags": attr.string(), + # Extra flags are built in the tf_library macro as a list. + "extra_flags": attr.string_list(), + "dfsan": attr.bool(default = False), + "dfsan_abilists": attr.label_list(default = [], allow_files = True), + "is_linux": attr.bool(), + "gen_compiler_log": attr.bool(), + "xla_flags": attr.string(), + }, +) + +def _tf_library( + name, + graph, + config, + debug_info = None, + freeze_checkpoint = None, + freeze_saver = None, + cpp_class = None, + gen_test = True, + gen_benchmark = True, + gen_compiler_log = False, + visibility = None, + testonly = None, + tfcompile_flags = None, + tfcompile_tool = "//tensorflow/compiler/aot:tfcompile", + include_standard_runtime_deps = True, + enable_xla_hlo_profiling = False, + enable_tracemes = False, + mlir_components = "None", + deps = None, + tags = [], + copts = [], + xla_flags = None): + if not cpp_class: + fail("cpp_class must be specified") + + tfcompile_graph = graph + if freeze_checkpoint or freeze_saver: + if not freeze_checkpoint: + fail("freeze_checkpoint must be specified when freeze_saver is " + + "specified") + + freeze_name = "freeze_" + name + freeze_file = freeze_name + ".pb" + + # First run tfcompile to generate the list of out_nodes. + # + # Here and below, we set CUDA_VISIBLE_DEVICES='' to prevent the code we + # launch from using any GPUs which might be present. This is important + # because builds may run concurrently with tests, and tests need to be + # able to assume that they have control of the full GPU. + out_nodes_file = "out_nodes_" + freeze_name + native.genrule( + name = ("gen_" + out_nodes_file), + srcs = [config], + outs = [out_nodes_file], + cmd = ("CUDA_VISIBLE_DEVICES='' " + + "$(location " + tfcompile_tool + ")" + + " --config=$(location " + config + ")" + + " --dump_fetch_nodes > $@"), + tools = [tfcompile_tool], + # Run tfcompile on the build host, rather than forge, since it's + # typically way faster on the local machine. + local = 1, + tags = tags, + ) + + # Now run freeze_graph to convert variables into constants. + freeze_args = ( + " --input_graph=$(location " + graph + ")" + + " --checkpoint_version=1" + + " --input_binary=" + str(not graph.endswith(".pbtxt")) + + " --input_checkpoint=$(location " + freeze_checkpoint + ")" + + " --output_graph=$(location " + freeze_file + ")" + + " --output_node_names=$$(<$(location " + out_nodes_file + + "))" + ) + freeze_saver_srcs = [] + if freeze_saver: + freeze_args += " --input_saver=$(location " + freeze_saver + ")" + freeze_saver_srcs.append(freeze_saver) + native.genrule( + name = freeze_name, + srcs = [ + graph, + freeze_checkpoint, + out_nodes_file, + ] + freeze_saver_srcs, + outs = [freeze_file], + cmd = ( + "CUDA_VISIBLE_DEVICES='' " + + "$(location " + + "//tensorflow/python/tools:freeze_graph)" + + freeze_args + ), + tools = ["//tensorflow/python/tools:freeze_graph"], + tags = tags, + ) + tfcompile_graph = freeze_file + + # Rule that runs tfcompile to produce the header and object file. + header_file = name + ".h" + + # The XLA backends morph kernel name prefix __ that is not in the form of + # __xla_. + ep = ("__xla_" + native.package_name() + "__" + name).replace("/", "_") + if type(tfcompile_flags) == type(""): + flags = tfcompile_flags + else: + flags = " ".join([ + "'" + arg.replace("'", "'\\''") + "'" + for arg in (tfcompile_flags or []) + ]) + + # Do this before we append the `select` into `flags`, because doing so + # transforms `flags` into a variable of type `select`, and we can't call + # `find` on such an object. + need_xla_data_proto = flags and flags.find("--gen_program_shape") != -1 + + if enable_xla_hlo_profiling: + profiling_flags = ["--xla_hlo_profile"] + else: + profiling_flags = [] + + if enable_tracemes: + traceme_flags = ["--xla_cpu_enable_xprof_traceme=true"] + else: + traceme_flags = ["--xla_cpu_enable_xprof_traceme=false"] + + mlir_flags = ["--mlir_components=" + mlir_components] + + srcs = [tfcompile_graph, config] + debug_info_flags = [] + if debug_info: + srcs.append(debug_info) + debug_info_flags = ["--debug_info=$(location " + debug_info + ")"] + + tfcompile_gen = "gen_" + name + _tfcompile_model_library( + name = tfcompile_gen, + model_name = name, + srcs = srcs, + gen_compiler_log = gen_compiler_log, + header_out = header_file, + tfcompile_tool = tfcompile_tool, + tfcompile_graph = tfcompile_graph, + tfcompile_config = config, + entry_point = ep, + cpp_class = cpp_class, + target_cpu = tfcompile_target_cpu(name), + target_triple = target_llvm_triple(), + flags = flags, + extra_flags = debug_info_flags + profiling_flags + mlir_flags + traceme_flags, + dfsan = tfcompile_dfsan_enabled(), + dfsan_abilists = tfcompile_dfsan_abilists(), + is_linux = select({ + "//tensorflow:linux_x86_64": True, + "//conditions:default": False, + }), + visibility = visibility, + testonly = testonly, + tags = tags, + xla_flags = xla_flags, + ) + + tfcompile_gen_object_files = tfcompile_gen + "_object_files" + native.filegroup( + name = tfcompile_gen_object_files, + srcs = [tfcompile_gen], + output_group = "object_files", + visibility = visibility, + testonly = testonly, + ) + + use_xla_nanort_runtime = False + if tfcompile_flags and "--use_xla_nanort_runtime" in tfcompile_flags: + use_xla_nanort_runtime = True + + # The cc_library rule packaging up the header and object file, and needed + # kernel implementations. + cc_library( + name = name, + srcs = [tfcompile_gen_object_files], + hdrs = [header_file], + visibility = visibility, + testonly = testonly, + deps = [ + # These deps are required by all tf_library targets even if + # include_standard_runtime_deps is False. Without them, the + # generated code will fail to compile. + "//third_party/absl/log:check", + "//third_party/absl/synchronization", + "//tensorflow/core:framework_lite", + "//tensorflow/compiler/tf2xla:xla_compiled_cpu_function", + "@xla//xla:types", + "@xla//xla/backends/cpu/runtime:kernel_c_api", + "@xla//xla/backends/cpu/runtime:rng_state_lib", + ] + (need_xla_data_proto and [ + # If we're generating the program shape, we must depend on the + # proto. + "@xla//xla:xla_data_proto_cc", + ] or []) + (enable_xla_hlo_profiling and [ + "@xla//xla/service:hlo_profile_printer_data_cc", + ] or []) + (include_standard_runtime_deps and [ + # TODO(cwhipkey): only depend on kernel code that the model actually + # needed. + "@xla//xla/backends/cpu/runtime:dot_lib", + "@xla//xla/backends/cpu/runtime:sort_lib", + "@xla//xla/backends/cpu/runtime:topk_lib", + "@xla//xla/backends/cpu/runtime:convolution_lib", + "@xla//xla/service/cpu:runtime_matmul", + "@xla//xla/service/cpu:runtime_single_threaded_matmul", + "@eigen_archive//:eigen3", + ] or []) + (use_xla_nanort_runtime and [ + "//tensorflow/compiler/tf2xla:xla_compiled_cpu_function_thunks", + ] or []) + (deps or []), + tags = tags, + copts = copts, + ) + + # Variables used for gen_test and gen_benchmark. + cpp_class_split = cpp_class.rsplit("::", 2) + if len(cpp_class_split) == 1: + no_ns_name = cpp_class_split[0] + else: + no_ns_name = cpp_class_split[1] + sed_replace = ( + "-e \"s|{{TFCOMPILE_HEADER}}|$(location " + header_file + ")|g\" " + + "-e \"s|{{TFCOMPILE_CPP_CLASS}}|" + cpp_class + "|g\" " + + "-e \"s|{{TFCOMPILE_NAME}}|" + no_ns_name + "|g\" " + ) + + if gen_test: + test_name = name + "_test" + test_file = test_name + ".cc" + + template_file = "//tensorflow/compiler/aot:test" + template_file += if_oss("", "_google") + ".cc" + + # Rule to rewrite the template_file to produce the test_file. + native.genrule( + name = ("gen_" + test_name), + testonly = 1, + srcs = [ + template_file, + header_file, + ], + outs = [test_file], + cmd = ( + "sed " + sed_replace + + " $(location " + template_file + ") " + + "> $(OUTS)" + ), + tags = tags, + ) + + # The cc_test rule for the generated code. To ensure that this works + # reliably across build configurations, we must use tf_cc_test instead + # of native.cc_test. This is related to how we build + # //tensorflow/core:lib -- see the note in + # tensorflow/core/BUILD for more details. + tf_cc_test( + name = test_name, + srcs = [test_file], + deps = [ + ":" + name, + "//tensorflow/compiler/aot:tf_library_test_main", + "@xla//xla:executable_run_options", + "@eigen_archive//:eigen3", + ] + if_oss([ + "//tensorflow/core:lib", + "//tensorflow/core:test", + ]) + if_google([ + "@com_google_googletest//:gtest", + "//tensorflow/core/platform:byte_order", + "//tensorflow/core/platform:platform_port", + ]), + tags = tags, + extra_copts = copts, + visibility = visibility, + ) + + if gen_benchmark: + benchmark_name = name + "_benchmark" + benchmark_file = benchmark_name + ".cc" + benchmark_main = ("//tensorflow/compiler/aot:" + + "benchmark_main.template") + + # Rule to rewrite benchmark.cc to produce the benchmark_file. + native.genrule( + name = ("gen_" + benchmark_name), + srcs = [ + benchmark_main, + header_file, + ], + testonly = testonly, + outs = [benchmark_file], + cmd = ("sed " + sed_replace + + " $(location " + benchmark_main + ") " + + "> $(OUTS)"), + tags = tags, + ) + + # The cc_benchmark rule for the generated code. This does not need the + # tf_cc_binary since we (by deliberate design) do not depend on + # //tensorflow/core:lib. + # + # Note: to get smaller size on android for comparison, compile with: + # --copt=-fvisibility=hidden + # --copt=-D_LIBCPP_TYPE_VIS=_LIBCPP_HIDDEN + # --copt=-D_LIBCPP_EXCEPTION_ABI=_LIBCPP_HIDDEN + cc_binary( + name = benchmark_name, + srcs = [benchmark_file], + testonly = testonly, + copts = copts + tf_copts(), + linkopts = if_android(["-pie", "-s"]), + deps = [ + ":" + name, + "//tensorflow/compiler/aot:benchmark", + "@xla//xla:executable_run_options", + "@eigen_archive//:eigen3", + ] + if_android([ + "//tensorflow/compiler/aot:benchmark_extra_android", + ]), + tags = tags, + visibility = visibility, + ) + +def tf_library( + name, + graph, + config, + debug_info = None, + freeze_checkpoint = None, + freeze_saver = None, + cpp_class = None, + gen_test = True, + gen_benchmark = True, + gen_compiler_log = False, + visibility = None, + testonly = None, + tfcompile_flags = None, + tfcompile_tool = "//tensorflow/compiler/aot:tfcompile", + include_standard_runtime_deps = True, + enable_xla_hlo_profiling = False, + enable_tracemes = False, + mlir_components = "None", + deps = None, + tags = [], + copts = [], + xla_flags = None): + """Compiles a TensorFlow graph into an executable with fast math enabled. + + Given an invocation of tf_library(name="foo", ...), generates the following + build targets: + foo: A cc_library containing the generated header and + computation. + foo_test: A cc_test with simple tests and benchmarks. Only created if + gen_test=True. + foo_benchmark: A cc_binary that runs a minimal-dependency benchmark, + useful for mobile devices or other platforms that can't + compile the full test libraries. Only created if + gen_benchmark=True. + The output header is called .h. + + Args: + name: The name of the build rule. + graph: The TensorFlow GraphDef to compile. If the file ends in '.pbtxt' + it is expected to be in the human-readable proto text format, otherwise + it is expected to be in the proto binary format. + config: File containing tensorflow.tf2xla.Config proto. If the file ends + in '.pbtxt' it is expected to be in the human-readable proto text + format, otherwise it is expected to be in the proto binary format. + debug_info: Debug info to include in the output. + freeze_checkpoint: If provided, run freeze_graph with this checkpoint to + convert variables into constants. + freeze_saver: If provided, run freeze_graph with this saver, in SaverDef + binary form, to convert variables into constants. + cpp_class: The name of the generated C++ class, wrapping the generated + function. The syntax of this flag is + [[::],...]. This mirrors the C++ syntax + for referring to a class, where multiple namespaces may precede the + class name, separated by double-colons. The class will be generated in + the given namespace(s), or if no namespaces are given, within the global + namespace. + gen_test: If True, also generate a cc_test rule that builds a simple + test and benchmark. + gen_benchmark: If True, also generate a binary with a simple benchmark. + Unlike the output of gen_test, this benchmark can be run on android. + gen_compiler_log: If True, dumps XLA:CPU debug output to a log file. + visibility: Bazel build visibility. + testonly: Bazel testonly attribute. + tfcompile_flags: Extra flags to pass to tfcompile to control compilation. + tfcompile_tool: The tfcompile binary. A non-default can be passed to + use a tfcompile built with extra dependencies. + include_standard_runtime_deps: If True, the standard list of + kernel/runtime deps is added to deps. If False, deps must contain the + full set of deps needed by the generated library. + enable_xla_hlo_profiling: Enable XLA HLO profiling in the generated + program, and emit metadata that lets us pretty-print the gathered + profile counters. + enable_tracemes: Tell tfcompile to generate calls to + TraceMe::Activity{Start|End} around HLO instructions that can be used by + Xprof to construct profiler timelines. + mlir_components: When the value is "None", no components use MLIR. When + the value is "Bridge", use MLIR to translate GraphDef to HLO. + deps: a list of deps to include on the build rules for the generated + library, added to the standard deps if standard_runtime_deps is True. + tags: tags to apply to subsidiary build rules. + copts: list of copts to pass to cc rules. + """ + _tf_library( + name, + graph, + config, + debug_info, + freeze_checkpoint, + freeze_saver, + cpp_class, + gen_test, + gen_benchmark, + gen_compiler_log, + visibility, + testonly, + tfcompile_flags, + tfcompile_tool, + include_standard_runtime_deps, + enable_xla_hlo_profiling, + enable_tracemes, + mlir_components, + deps, + tags, + copts, + xla_flags, + ) + +def target_llvm_triple(): + """Returns the target LLVM triple to be used for compiling the target.""" + + # TODO(toddw): Add target_triple for other targets. For details see: + # http://llvm.org/docs/doxygen/html/Triple_8h_source.html + return select({ + "//tensorflow:android_armeabi": "armv5-none-android", + "//tensorflow:android_arm": "armv7-none-android", + "//tensorflow:android_arm64": "aarch64-none-android", + "//tensorflow:android_x86": "i686-none-android", + "//tensorflow:ios": "arm64-none-ios", + "//tensorflow:ios_x86_64": "x86_64-apple-ios", + "//tensorflow:linux_ppc64le": "ppc64le-ibm-linux-gnu", + "//tensorflow:linux_aarch64": "aarch64-none-linux-gnu", + "//tensorflow:macos_x86_64": "x86_64-none-darwin", + "//tensorflow:macos_arm64": "aarch64-none-darwin", + "//tensorflow:windows": "x86_64-none-windows", + "//tensorflow:linux_s390x": "systemz-none-linux-gnu", + # internal placeholder, + "//conditions:default": "x86_64-pc-linux", + }) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/tfcompile_main.cc b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tfcompile_main.cc new file mode 100644 index 0000000000000000000000000000000000000000..e22aa3e65feb515018e6e6a5fac4394ced4dc5f3 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/tfcompile_main.cc @@ -0,0 +1,84 @@ +/* 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 "absl/log/check.h" +#include "absl/status/status.h" +#include "absl/strings/string_view.h" +#include "tensorflow/compiler/aot/compile.h" +#include "tensorflow/compiler/aot/flags.h" +#include "tensorflow/compiler/tf2xla/tf2xla.pb.h" +#include "xla/debug_options_flags.h" +#include "xla/tsl/platform/status.h" +#include "tensorflow/core/framework/graph.pb.h" +#include "tensorflow/core/platform/init_main.h" +#include "tensorflow/core/platform/logging.h" +#include "tensorflow/core/util/command_line_flags.h" + +namespace tensorflow { +namespace tfcompile { + +const char kUsageHeader[] = + "tfcompile performs ahead-of-time compilation of a TensorFlow graph,\n" + "resulting in an object file compiled for your target architecture, and a\n" + "header file that gives access to the functionality in the object file.\n" + "A typical invocation looks like this:\n" + "\n" + " $ tfcompile --graph=mygraph.pb --config=myfile.pbtxt " + "--cpp_class=\"mynamespace::MyComputation\"\n" + "\n"; + +} // end namespace tfcompile +} // end namespace tensorflow + +int main(int argc, char** argv) { + tensorflow::tfcompile::MainFlags flags; +#ifndef __s390x__ + flags.target_triple = "x86_64-pc-linux"; +#endif + flags.out_function_object = "out_model.o"; + flags.out_metadata_object = "out_helper.o"; + flags.out_header = "out.h"; + flags.entry_point = "entry"; + flags.debug_info_path_begin_marker = ""; + + // Note that tfcompile.bzl's tf_library macro sets fast math flags as that is + // generally the preferred case. + std::vector flag_list; + AppendMainFlags(&flag_list, &flags); + xla::AppendDebugOptionsFlags(&flag_list); + + std::string usage = tensorflow::tfcompile::kUsageHeader; + usage += tensorflow::Flags::Usage(argv[0], flag_list); + if (argc > 1 && absl::string_view(argv[1]) == "--help") { + std::cerr << usage << "\n"; + return 0; + } + bool parsed_flags_ok = tensorflow::Flags::Parse(&argc, argv, flag_list); + QCHECK(parsed_flags_ok) << "\n" << usage; + + tensorflow::port::InitMain(usage.c_str(), &argc, &argv); + QCHECK(argc == 1) << "\nERROR: This command does not take any arguments " + "other than flags. See --help.\n\n"; + absl::Status status = tensorflow::tfcompile::Main(flags); + if (status.code() == absl::StatusCode::kInvalidArgument) { + std::cerr << "INVALID ARGUMENTS: " << status.message() << "\n\n"; + return 1; + } else { + TF_QCHECK_OK(status); + } + return 0; +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/thunk_proto_execution_deserializer.cc b/western_ai/usa/tensorflow/tensorflow/compiler/aot/thunk_proto_execution_deserializer.cc new file mode 100644 index 0000000000000000000000000000000000000000..a250abfdee0b1f93af10c579d346d77d3943e97e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/thunk_proto_execution_deserializer.cc @@ -0,0 +1,918 @@ +/* Copyright 2025 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/compiler/aot/thunk_proto_execution_deserializer.h" + +#include +#include +#include +#include +#include + +#include "absl/numeric/int128.h" +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/str_join.h" +#include "absl/strings/str_replace.h" +#include "absl/strings/string_view.h" +#include "xla/backends/cpu/runtime/convolution_dims.h" +#include "xla/backends/cpu/runtime/dot_dims.h" +#include "xla/backends/cpu/runtime/kernel_thunk.h" +#include "xla/backends/cpu/runtime/thunk.pb.h" +#include "xla/layout_util.h" +#include "xla/service/cpu/executable.pb.h" +#include "xla/shape.h" +#include "xla/shape_util.h" +#include "xla/status_macros.h" +#include "xla/tsl/platform/statusor.h" +#include "xla/util.h" +#include "xla/xla_data.pb.h" + +namespace tensorflow { +namespace tfcompile { + +namespace { + +std::string GetBufferAllocationString( + const xla::buffer_assignment::BufferAllocationSliceProto& slice) { + return absl::StrCat("reinterpret_cast(buffer_table[", + slice.buffer_allocation_index(), "]) + ", slice.offset()); +} + +} // namespace + +absl::StatusOr +ThunkProtoExecutionDeserializer::GetThunkSpecificRunImpl( + const xla::cpu::CompilationResultProto& proto) && { + return ThunkSpecificRunImplFromThunkSequence(proto.thunk_sequence()); +} + +absl::StatusOr +ThunkProtoExecutionDeserializer::ThunkSpecificRunImplFromThunkSequence( + const xla::cpu::ThunkSequenceProto& thunk_sequence_proto) { + std::vector thunk_run_impls; + thunk_run_impls.reserve(thunk_sequence_proto.thunks_size()); + + for (const auto& thunk : thunk_sequence_proto.thunks()) { + switch (thunk.impl_case()) { + case xla::cpu::ThunkProto::kKernelThunk: { + TF_ASSIGN_OR_RETURN(thunk_run_impls.emplace_back(), + GetKernelThunkRunImpl(thunk)); + break; + } + case xla::cpu::ThunkProto::kDotThunk: { + TF_ASSIGN_OR_RETURN(thunk_run_impls.emplace_back(), + GetDotThunkRunImpl(thunk)); + break; + } + case xla::cpu::ThunkProto::kConditionalThunk: { + TF_ASSIGN_OR_RETURN(thunk_run_impls.emplace_back(), + GetConditionalThunkRunImpl(thunk)); + break; + } + case xla::cpu::ThunkProto::kWhileThunk: { + TF_ASSIGN_OR_RETURN(thunk_run_impls.emplace_back(), + GetWhileThunkRunImpl(thunk)); + break; + } + case xla::cpu::ThunkProto::kConvolutionThunk: { + TF_ASSIGN_OR_RETURN(thunk_run_impls.emplace_back(), + GetConvolutionFusionThunkRunImpl(thunk)); + break; + } + case xla::cpu::ThunkProto::kRngGetAndUpdateStateThunk: { + TF_ASSIGN_OR_RETURN(thunk_run_impls.emplace_back(), + GetRngGetAndUpdateStateThunkRunImpl(thunk)); + break; + } + case xla::cpu::ThunkProto::kCallThunk: { + TF_ASSIGN_OR_RETURN(thunk_run_impls.emplace_back(), + GetCallThunkRunImpl(thunk)); + break; + } + case xla::cpu::ThunkProto::kCopyThunk: { + TF_ASSIGN_OR_RETURN(thunk_run_impls.emplace_back(), + GetCopyThunkRunImpl(thunk)); + break; + } + case xla::cpu::ThunkProto::kSortThunk: { + TF_ASSIGN_OR_RETURN(thunk_run_impls.emplace_back(), + GetSortThunkRunImpl(thunk)); + break; + } + case xla::cpu::ThunkProto::kTopKThunk: { + TF_ASSIGN_OR_RETURN(thunk_run_impls.emplace_back(), + GetTopKThunkRunImpl(thunk)); + break; + } + case xla::cpu::ThunkProto::kRngSeedThunk: { + TF_ASSIGN_OR_RETURN(thunk_run_impls.emplace_back(), + GetRngSeedThunkRunImpl(thunk)); + break; + } + default: { + return xla::Internal("Unsupported thunk type: %s.", thunk.kind()); + } + } + } + + return absl::StrJoin(thunk_run_impls, "\n"); +} + +absl::StatusOr ThunkProtoExecutionDeserializer::GetMatmulFunction( + xla::PrimitiveType xla_type) { + switch (xla_type) { + case xla::F16: + return "::xla::cpu::internal::TypedMatMul"; + case xla::F32: + return "::xla::cpu::internal::TypedMatMul"; + case xla::F64: + return "::xla::cpu::internal::TypedMatMul"; + case xla::C64: + return "::xla::cpu::internal::TypedMatMul, " + "std::complex, std::complex"; + case xla::C128: + return "::xla::cpu::internal::TypedMatMul, " + "std::complex, std::complex"; + case xla::S32: + return "::xla::cpu::internal::TypedMatMul"; + default: + return xla::Internal("Unsupported xla type: %d", xla_type); + } +} + +absl::StatusOr ThunkProtoExecutionDeserializer::GetDotThunkRunImpl( + const xla::cpu::ThunkProto& thunk) { + if (!thunk.has_dot_thunk()) { + return xla::Internal( + "Dot thunk was expected when getting thunk run implementation."); + } + const xla::cpu::DotThunkProto& dot_thunk = thunk.dot_thunk(); + + absl::string_view dot_thunk_invocation_format = R"( + // Dot Thunk + { + absl::BlockingCounter done({{BATCH_SIZE}}); + for (int64_t i = 0; i < {{BATCH_SIZE}}; ++i) { + {{MATMUL_FUNCTION}}( + run_options->intra_op_thread_pool(), + {{OUTPUT_PTR}} + {{OUTPUT_STRIDE}} * i, + {{LHS_PTR}} + {{LHS_STRIDE}} * i, + {{RHS_PTR}} + {{RHS_STRIDE}} * i, + {{M}}, {{N}}, {{K}}, {{TRANSPOSE_LHS}}, {{TRANSPOSE_RHS}}, + [&done] { done.DecrementCount(); } + ); + } + done.Wait(); + } + )"; + + if (!(dot_thunk.lhs_buffer_shape().shape().element_type() == + dot_thunk.rhs_buffer_shape().shape().element_type() && + dot_thunk.rhs_buffer_shape().shape().element_type() == + dot_thunk.out_buffer_shape().shape().element_type())) { + return xla::Internal( + "Dot thunk has mismatched types between lhs, rhs, and out buffers."); + } + + TF_ASSIGN_OR_RETURN( + std::string matmul_function, + GetMatmulFunction(dot_thunk.lhs_buffer_shape().shape().element_type())); + + TF_ASSIGN_OR_RETURN(std::string data_type, + CppDataTypeFromXlaType( + dot_thunk.lhs_buffer_shape().shape().element_type())); + + std::string output_ptr = absl::StrCat( + "reinterpret_cast<", data_type, "*>(", + GetBufferAllocationString(dot_thunk.out_buffer_shape().slice()), ")"); + std::string lhs_ptr = absl::StrCat( + "reinterpret_cast<", data_type, "*>(", + GetBufferAllocationString(dot_thunk.lhs_buffer_shape().slice()), ")"); + std::string rhs_ptr = absl::StrCat( + "reinterpret_cast<", data_type, "*>(", + GetBufferAllocationString(dot_thunk.rhs_buffer_shape().slice()), ")"); + + TF_ASSIGN_OR_RETURN( + auto lhs_shape, + xla::Shape::FromProto(dot_thunk.lhs_buffer_shape().shape())); + TF_ASSIGN_OR_RETURN( + auto rhs_shape, + xla::Shape::FromProto(dot_thunk.rhs_buffer_shape().shape())); + TF_ASSIGN_OR_RETURN( + auto out_shape, + xla::Shape::FromProto(dot_thunk.out_buffer_shape().shape())); + + TF_ASSIGN_OR_RETURN(xla::cpu::DotShape dot_shape, + xla::cpu::GetDotShape(dot_thunk.dot_dimensions(), + lhs_shape, rhs_shape, out_shape)); + + TF_ASSIGN_OR_RETURN( + xla::cpu::DotCanonicalDims dot_canonical_dims, + GetDotCanonicalDims(dot_thunk.dot_dimensions(), dot_shape)); + + size_t m = dot_canonical_dims.m; + size_t k = dot_canonical_dims.k; + size_t n = dot_canonical_dims.n; + + // Decide if a transpose is required based on an XOR of the canonical and + // column major flags. + bool transpose_lhs = + (dot_canonical_dims.lhs_canonical != dot_canonical_dims.lhs_column_major); + bool transpose_rhs = + (dot_canonical_dims.rhs_canonical != dot_canonical_dims.rhs_column_major); + + if (!dot_canonical_dims.output_column_major) { + std::swap(m, n); + std::swap(lhs_ptr, rhs_ptr); + std::swap(transpose_lhs, transpose_rhs); + transpose_lhs = !transpose_lhs; + transpose_rhs = !transpose_rhs; + } + + // NOTE: Don't use byte width because the buffer pointers will already be cast + // to the correct type. Reference xla/backends/cpu/runtime/dot_thunk.cc. + int64_t lhs_stride = m * k; + int64_t rhs_stride = k * n; + int64_t out_stride = m * n; + + std::vector> rewrites = { + {"{{MATMUL_FUNCTION}}", matmul_function}, + {"{{OUTPUT_PTR}}", output_ptr}, + {"{{OUTPUT_STRIDE}}", absl::StrCat(out_stride)}, + {"{{LHS_PTR}}", lhs_ptr}, + {"{{LHS_STRIDE}}", absl::StrCat(lhs_stride)}, + {"{{RHS_PTR}}", rhs_ptr}, + {"{{RHS_STRIDE}}", absl::StrCat(rhs_stride)}, + {"{{M}}", absl::StrCat(m)}, + {"{{N}}", absl::StrCat(n)}, + {"{{K}}", absl::StrCat(k)}, + {"{{TRANSPOSE_LHS}}", transpose_lhs ? "true" : "false"}, + {"{{TRANSPOSE_RHS}}", transpose_rhs ? "true" : "false"}, + {"{{BATCH_SIZE}}", absl::StrCat(dot_shape.batch_size)}}; + + return absl::StrReplaceAll(dot_thunk_invocation_format, rewrites); +}; + +absl::StatusOr +ThunkProtoExecutionDeserializer::GetConvolutionFunction( + xla::PrimitiveType xla_type) { + switch (xla_type) { + case xla::F16: + return "xla::cpu::internal::EigenConv2D"; + case xla::F32: + return "xla::cpu::internal::EigenConv2D"; + default: + return xla::Internal("Unsupported xla type: %d", xla_type); + } +} + +absl::StatusOr +ThunkProtoExecutionDeserializer::GetConvolution2DRunImpl( + const xla::cpu::ConvolutionThunkProto& convolution_thunk, + const xla::cpu::ConvolutionCanonicalDims& canonical_dims) { + TF_ASSIGN_OR_RETURN( + std::string data_type, + CppDataTypeFromXlaType( + convolution_thunk.input_buffer_shape().shape().element_type())); + + std::string output_ptr = + absl::StrCat("reinterpret_cast<", data_type, "*>(", + GetBufferAllocationString( + convolution_thunk.output_buffer_shape().slice()), + ")"); + std::string lhs_ptr = absl::StrCat( + "reinterpret_cast<", data_type, "*>(", + GetBufferAllocationString(convolution_thunk.input_buffer_shape().slice()), + ")"); + std::string rhs_ptr = + absl::StrCat("reinterpret_cast<", data_type, "*>(", + GetBufferAllocationString( + convolution_thunk.kernel_buffer_shape().slice()), + ")"); + + TF_ASSIGN_OR_RETURN( + std::string convolution_function, + GetConvolutionFunction( + convolution_thunk.input_buffer_shape().shape().element_type())); + + absl::string_view convolution_thunk_invocation_format = R"( + // Convolution Thunk + { + absl::Notification done; + {{CONVOLUTION_FUNCTION}}( + run_options->intra_op_thread_pool(), + {{OUTPUT_PTR}}, {{LHS_PTR}}, {{RHS_PTR}}, {{INPUT_BATCH}}, + {{INPUT_ROWS}}, {{INPUT_COLS}}, {{INPUT_CHANNELS}}, {{KERNEL_ROWS}}, + {{KERNEL_COLS}}, {{KERNEL_CHANNELS}}, {{KERNEL_FILTERS}}, + {{OUTPUT_ROWS}}, {{OUTPUT_COLS}}, {{ROW_STRIDE}}, {{COL_STRIDE}}, + {{PADDING_TOP}}, {{PADDING_BOTTOM}}, {{PADDING_LEFT}}, + {{PADDING_RIGHT}}, {{LHS_ROW_DILATION}}, {{LHS_COL_DILATION}}, + {{RHS_ROW_DILATION}}, {{RHS_COL_DILATION}}, {{FEATURE_GROUP_COUNT}}, + [&done] { done.Notify(); } + ); + done.WaitForNotification(); + })"; + + std::vector> rewrites = { + {"{{CONVOLUTION_FUNCTION}}", convolution_function}, + {"{{OUTPUT_PTR}}", output_ptr}, + {"{{LHS_PTR}}", lhs_ptr}, + {"{{RHS_PTR}}", rhs_ptr}, + {"{{INPUT_BATCH}}", absl::StrCat(canonical_dims.input_batch)}, + {"{{INPUT_ROWS}}", absl::StrCat(canonical_dims.input_dims.x)}, + {"{{INPUT_COLS}}", absl::StrCat(canonical_dims.input_dims.y)}, + {"{{INPUT_CHANNELS}}", absl::StrCat(canonical_dims.input_channels)}, + {"{{KERNEL_ROWS}}", absl::StrCat(canonical_dims.kernel_dims.x)}, + {"{{KERNEL_COLS}}", absl::StrCat(canonical_dims.kernel_dims.y)}, + {"{{KERNEL_CHANNELS}}", absl::StrCat(canonical_dims.kernel_channels)}, + {"{{KERNEL_FILTERS}}", absl::StrCat(canonical_dims.kernel_filters)}, + {"{{OUTPUT_ROWS}}", absl::StrCat(canonical_dims.output_dims.x)}, + {"{{OUTPUT_COLS}}", absl::StrCat(canonical_dims.output_dims.y)}, + {"{{ROW_STRIDE}}", absl::StrCat(canonical_dims.strides.x)}, + {"{{COL_STRIDE}}", absl::StrCat(canonical_dims.strides.y)}, + {"{{PADDING_TOP}}", absl::StrCat(canonical_dims.padding_before.x)}, + {"{{PADDING_BOTTOM}}", absl::StrCat(canonical_dims.padding_after.x)}, + {"{{PADDING_LEFT}}", absl::StrCat(canonical_dims.padding_before.y)}, + {"{{PADDING_RIGHT}}", absl::StrCat(canonical_dims.padding_after.y)}, + {"{{LHS_ROW_DILATION}}", absl::StrCat(canonical_dims.base_dilation.x)}, + {"{{LHS_COL_DILATION}}", absl::StrCat(canonical_dims.base_dilation.y)}, + {"{{RHS_ROW_DILATION}}", absl::StrCat(canonical_dims.window_dilation.x)}, + {"{{RHS_COL_DILATION}}", absl::StrCat(canonical_dims.window_dilation.y)}, + {"{{FEATURE_GROUP_COUNT}}", + absl::StrCat(canonical_dims.feature_group_count)}}; + + return absl::StrReplaceAll(convolution_thunk_invocation_format, rewrites); +} + +absl::StatusOr +ThunkProtoExecutionDeserializer::GetConvolutionFusionThunkRunImpl( + const xla::cpu::ThunkProto& thunk) { + if (!thunk.has_convolution_thunk()) { + return xla::Internal( + "Convolution thunk was expected when getting thunk run " + "implementation."); + } + const xla::cpu::ConvolutionThunkProto& convolution_thunk = + thunk.convolution_thunk(); + + TF_ASSIGN_OR_RETURN( + xla::Shape input_buffer_shape, + xla::Shape::FromProto(convolution_thunk.input_buffer_shape().shape())); + TF_ASSIGN_OR_RETURN( + xla::Shape kernel_buffer_shape, + xla::Shape::FromProto(convolution_thunk.kernel_buffer_shape().shape())); + TF_ASSIGN_OR_RETURN( + xla::Shape output_buffer_shape, + xla::Shape::FromProto(convolution_thunk.output_buffer_shape().shape())); + + // NOTE(basioli): Slices are not needed here, we only use this class to + // invoke GetConvolutionCanonicalDims. + xla::cpu::ConvolutionSlices slices{ + /*input_buffer=*/{}, + /*input_shape=*/input_buffer_shape, + /*kernel_buffer=*/{}, + /*kernel_shape=*/kernel_buffer_shape, + /*output_buffer=*/{}, + /*output_shape=*/output_buffer_shape, + }; + + TF_ASSIGN_OR_RETURN( + xla::cpu::ConvolutionCanonicalDims canonical_dims, + xla::cpu::GetConvolutionCanonicalDims( + slices, convolution_thunk.dimension_numbers(), + convolution_thunk.window(), convolution_thunk.feature_group_count())); + + if (canonical_dims.convolution_rank() == 2) { + return GetConvolution2DRunImpl(convolution_thunk, canonical_dims); + } else { + return xla::Internal("3D convolution is not implemented."); + } +} + +absl::StatusOr +ThunkProtoExecutionDeserializer::GetRngGetAndUpdateStateThunkRunImpl( + const xla::cpu::ThunkProto& thunk) { + if (!thunk.has_rng_get_and_update_state_thunk()) { + return xla::Internal( + "RngGetAndUpdateState thunk was expected when getting thunk run " + "implementation."); + } + const xla::cpu::RngGetAndUpdateStateThunkProto& rng_thunk = + thunk.rng_get_and_update_state_thunk(); + absl::string_view rng_thunk_invocation_format = R"( + // Rng Thunk + { + rng_states[{{RNG_STATE_INDEX}}]->GetAndUpdateState({{RNG_STATE_PTR}}); + })"; + + if (rng_thunk.state_buffer().size() != sizeof(absl::int128)) { + return absl::InvalidArgumentError( + absl::StrCat("Rng state buffer size: ", rng_thunk.state_buffer().size(), + " is not equal to the size of an absl::int128: ", + sizeof(absl::int128))); + } + + return absl::StrReplaceAll( + rng_thunk_invocation_format, + {{"{{RNG_STATE_INDEX}}", absl::StrCat(rng_state_index_++)}, + {"{{RNG_STATE_PTR}}", + absl::StrCat("reinterpret_cast(", + GetBufferAllocationString(rng_thunk.state_buffer()), + ")")}}); +} + +absl::StatusOr +ThunkProtoExecutionDeserializer::GetRngSeedThunkRunImpl( + const xla::cpu::ThunkProto& thunk) { + if (!thunk.has_rng_seed_thunk()) { + return xla::Internal( + "RngSeed thunk was expected when getting thunk run implementation."); + } + const xla::cpu::RngSeedThunkProto& rng_seed_thunk = thunk.rng_seed_thunk(); + + absl::string_view rng_seed_thunk_invocation_format = R"( + // Rng Seed Thunk + { + uint64_t seed = static_cast(run_options->rng_seed()); + if (seed == 0) { + static thread_local std::random_device rd; + static thread_local std::mt19937_64 gen(rd()); + std::uniform_int_distribution distrib(1, std::numeric_limits::max()); + seed = distrib(gen); + } + *reinterpret_cast({{RNG_SEED_PTR}}) = seed; + })"; + + return absl::StrReplaceAll( + rng_seed_thunk_invocation_format, + {{"{{RNG_SEED_PTR}}", + absl::StrCat("reinterpret_cast(", + GetBufferAllocationString(rng_seed_thunk.dest_buffer()), + ")")}}); +} + +absl::StatusOr +ThunkProtoExecutionDeserializer::GetCallThunkRunImpl( + const xla::cpu::ThunkProto& thunk) { + if (!thunk.has_call_thunk()) { + return xla::Internal( + "Calls thunk was expected when getting thunk run implementation."); + } + const xla::cpu::CallThunkProto& call_thunk = thunk.call_thunk(); + absl::string_view call_thunk_invocation_format = R"( + // Call Thunk + { + {{CALL_THUNK_IMPL}} + })"; + + TF_ASSIGN_OR_RETURN( + std::string call_thunk_impl, + ThunkSpecificRunImplFromThunkSequence(call_thunk.called_sequence())); + + return absl::StrReplaceAll(call_thunk_invocation_format, + {{"{{CALL_THUNK_IMPL}}", call_thunk_impl}}); +} + +absl::StatusOr +ThunkProtoExecutionDeserializer::GetCopyThunkRunImpl( + const xla::cpu::ThunkProto& thunk) { + // IMPORTANT(basioli): tfcompiled models should always emit llvm kernels for + // copy thunks. Here we emit just a memcpy. This is done exclusively for copy + // thunks that get created by the sort thunk. + if (!thunk.has_copy_thunk()) { + return xla::Internal( + "Copy thunk was expected when getting thunk run implementation."); + } + + const xla::cpu::CopyThunkProto& copy_thunk = thunk.copy_thunk(); + TF_ASSIGN_OR_RETURN( + auto input_shape, + xla::Shape::FromProto(copy_thunk.src_buffer_shape().shape())); + TF_ASSIGN_OR_RETURN( + auto output_shape, + xla::Shape::FromProto(copy_thunk.dst_buffer_shape().shape())); + + if (input_shape != output_shape) { + return xla::Internal( + "Copy thunk has input shape %s and output shape %s that are not the " + "same.", + input_shape.ToString(true), output_shape.ToString(true)); + } + + absl::string_view copy_thunk_invocation_format = R"( + // Copy Thunk + { + std::memcpy({{OUTPUT_PTR}}, {{INPUT_PTR}}, {{SIZE}}); + })"; + + return absl::StrReplaceAll( + copy_thunk_invocation_format, + {{"{{OUTPUT_PTR}}", + absl::StrCat( + "reinterpret_cast(", + GetBufferAllocationString(copy_thunk.dst_buffer_shape().slice()), + ")")}, + {"{{INPUT_PTR}}", + absl::StrCat( + "reinterpret_cast(", + GetBufferAllocationString(copy_thunk.src_buffer_shape().slice()), + ")")}, + {"{{SIZE}}", + absl::StrCat(copy_thunk.src_buffer_shape().slice().size())}}); +} + +absl::StatusOr +ThunkProtoExecutionDeserializer::GetSortThunkRunImpl( + const xla::cpu::ThunkProto& thunk) { + if (!thunk.has_sort_thunk()) { + return xla::Internal( + "Sort thunk was expected when getting thunk run implementation."); + } + const xla::cpu::SortThunkProto& sort_thunk = thunk.sort_thunk(); + + std::vector buffers_to_sort; + buffers_to_sort.reserve(sort_thunk.inputs_shapes_size()); + + std::vector primitive_sizes; + primitive_sizes.reserve(sort_thunk.inputs_shapes_size()); + for (const auto& buffer_proto : sort_thunk.inputs_shapes()) { + buffers_to_sort.push_back( + absl::StrCat("reinterpret_cast(", + GetBufferAllocationString(buffer_proto.slice()), ")")); + primitive_sizes.push_back(xla::ShapeUtil::ByteSizeOfPrimitiveType( + buffer_proto.shape().element_type())); + } + absl::string_view sort_thunk_invocation_format = R"( + // Sort Thunk + { + std::vector values = { + {{BUFFERS_TO_SORT}} + }; + std::vector primitive_sizes = { + {{VALUES_PRIMITIVE_TYPE_SIZE_IN_BYTES}} + }; + + // Type alias compatible with `FunctionLibrary::Comparator`. + using Comparator = void(bool* result, const void* run_options, + const void** params, const void* buffer_table, + const void* status, const void* prof_counters); + Comparator* comparator = reinterpret_cast( + {{SORT_FUNCTION_NAME}}); + + absl::AnyInvocable less_than = + [comparator](const void** data) { + bool result; + (*comparator)(&result, nullptr, data, nullptr, nullptr, nullptr); + ABSL_ANNOTATE_MEMORY_IS_INITIALIZED(&result, sizeof(result)); + return result; + }; + + xla::cpu::internal::SortInplace( + { + {{HIGHER_DIMENSIONS}}, + {{SORT_DIMENSION_ELEMENTS}}, + {{LOWER_DIMENSIONS}} + }, + values, primitive_sizes, {{IS_STABLE}}, &less_than); + })"; + + TF_ASSIGN_OR_RETURN( + auto keys_shape, + xla::Shape::FromProto(sort_thunk.inputs_shapes(0).shape())); + + // Normalize the shape and the dimension to sort. + xla::Shape normalized_keys_shape = + xla::ShapeUtil::MakeShapeWithDescendingLayoutAndSamePhysicalLayout( + keys_shape); + auto logical_to_physical = + xla::LayoutUtil::MakeLogicalToPhysical(keys_shape.layout()); + TF_RET_CHECK(sort_thunk.dimension() < logical_to_physical.size()); + int64_t physical_dimension_to_sort = + logical_to_physical[sort_thunk.dimension()]; + + int64_t sort_dimension_elements = + normalized_keys_shape.dimensions(physical_dimension_to_sort); + int64_t higher_dimensions = 1; + for (int64_t i = 0; i < physical_dimension_to_sort; ++i) { + higher_dimensions *= normalized_keys_shape.dimensions(i); + } + int64_t lower_dimensions = 1; + for (int64_t i = normalized_keys_shape.dimensions().size() - 1; + i > physical_dimension_to_sort; --i) { + lower_dimensions *= normalized_keys_shape.dimensions(i); + } + return absl::StrReplaceAll( + sort_thunk_invocation_format, + { + {"{{HIGHER_DIMENSIONS}}", absl::StrCat(higher_dimensions)}, + {"{{SORT_DIMENSION_ELEMENTS}}", + absl::StrCat(sort_dimension_elements)}, + {"{{LOWER_DIMENSIONS}}", absl::StrCat(lower_dimensions)}, + {"{{SORT_FUNCTION_NAME}}", sort_thunk.comparator_name()}, + {"{{BUFFERS_TO_SORT}}", absl::StrJoin(buffers_to_sort, ", ")}, + {"{{VALUES_PRIMITIVE_TYPE_SIZE_IN_BYTES}}", + absl::StrJoin(primitive_sizes, ", ")}, + {"{{IS_STABLE}}", sort_thunk.is_stable() ? "true" : "false"}, + }); +} + +absl::StatusOr +ThunkProtoExecutionDeserializer::GetTopKThunkRunImpl( + const xla::cpu::ThunkProto& thunk) { + if (!thunk.has_top_k_thunk()) { + return xla::Internal( + "TopK thunk was expected when getting thunk run implementation."); + } + const xla::cpu::TopKThunkProto& topk_thunk_proto = thunk.top_k_thunk(); + + absl::string_view topk_thunk_invocation_format = R"( + // TopK Thunk + { + ::xla::cpu::internal::TopK({{BATCH_SIZE}}, {{INPUT_SIZE}}, {{K}}, + reinterpret_cast({{VALUES_PTR}}), + reinterpret_cast({{OUTPUT_PTR}}), + reinterpret_cast({{INDICES_PTR}})); + })"; + + return absl::StrReplaceAll( + topk_thunk_invocation_format, + { + {"{{BATCH_SIZE}}", absl::StrCat(topk_thunk_proto.batch_size())}, + {"{{INPUT_SIZE}}", absl::StrCat(topk_thunk_proto.input_size())}, + {"{{K}}", absl::StrCat(topk_thunk_proto.k())}, + {"{{VALUES_PTR}}", + GetBufferAllocationString(topk_thunk_proto.values_buffer())}, + {"{{OUTPUT_PTR}}", + GetBufferAllocationString(topk_thunk_proto.output_buffer())}, + {"{{INDICES_PTR}}", + GetBufferAllocationString(topk_thunk_proto.indices_buffer())}, + }); +} + +absl::StatusOr +ThunkProtoExecutionDeserializer::GetKernelThunkRunImpl( + const xla::cpu::ThunkProto& thunk) { + if (!thunk.has_kernel_thunk()) { + return xla::Internal( + "Kernel thunk was expected when getting thunk run implementation."); + } + const xla::cpu::KernelThunkProto& kernel_thunk = thunk.kernel_thunk(); + + auto get_args_initializer_as_string = + [](const xla::cpu::KernelThunkProto& kernel_thunk) -> std::string { + std::vector args_initializer; + for (const auto& buffer_proto : kernel_thunk.arguments_buffers()) { + args_initializer.push_back(absl::StrCat( + "XLA_CPU_KernelArg{", GetBufferAllocationString(buffer_proto.slice()), + ", ", buffer_proto.slice().size(), "}")); + } + for (const auto& buffer_proto : kernel_thunk.results_buffers()) { + args_initializer.push_back(absl::StrCat( + "XLA_CPU_KernelArg{", GetBufferAllocationString(buffer_proto.slice()), + ", ", buffer_proto.slice().size(), "}")); + } + return absl::StrCat("{", absl::StrJoin(args_initializer, ", "), "}"); + }; + + // Execute in block so we don't have to worry about naming for now + absl::string_view kernel_invocation_format = R"( + // Kernel Thunk + { + std::array args = {{ARGS_INITIALIZER}}; + XLA_CPU_NumWorkGroups kernel_thread_dims = { + {{THREAD_DIM_X}}, + {{THREAD_DIM_Y}}, + {{THREAD_DIM_Z}}, + }; + + for (uint64_t z = 0; z < {{THREAD_DIM_Z}}; ++z) { + for (uint64_t y = 0; y < {{THREAD_DIM_Y}}; ++y) { + for (uint64_t x = 0; x < {{THREAD_DIM_X}}; ++x) { + XLA_CPU_WorkGroupId kernel_thread = {x, y, z}; + + XLA_CPU_KernelCallFrame call_frame = { + &kernel_thread_dims, &kernel_thread, args.size(), args.data()}; + + XLA_CPU_KernelError* error = (*{{KERNEL_NAME}})(&call_frame); + + if (ABSL_PREDICT_FALSE(error != nullptr)) { + return false; + } + } + } + } + })"; + + return absl::StrReplaceAll( + kernel_invocation_format, + { + {"{{NUM_ARGS}}", + absl::StrCat(kernel_thunk.arguments_buffers().size() + + kernel_thunk.results_buffers().size())}, + {"{{ARGS_INITIALIZER}}", + get_args_initializer_as_string(kernel_thunk)}, + {"{{THREAD_DIM_X}}", absl::StrCat(kernel_thunk.num_workgroups().x())}, + {"{{THREAD_DIM_Y}}", absl::StrCat(kernel_thunk.num_workgroups().y())}, + {"{{THREAD_DIM_Z}}", absl::StrCat(kernel_thunk.num_workgroups().z())}, + {"{{KERNEL_NAME}}", kernel_thunk.kernel_name()}, + }); +} + +absl::StatusOr +ThunkProtoExecutionDeserializer::GetConditionalThunkRunImpl( + const xla::cpu::ThunkProto& thunk) { + if (!thunk.has_conditional_thunk()) { + return xla::Internal( + "Conditional thunk was expected when getting thunk run " + "implementation."); + } + const xla::cpu::ConditionalThunkProto& conditional_thunk = + thunk.conditional_thunk(); + + std::vector conditional_thunk_branches; + conditional_thunk_branches.reserve(conditional_thunk.branch_sequences_size()); + for (const auto& branch_sequence : conditional_thunk.branch_sequences()) { + TF_ASSIGN_OR_RETURN(conditional_thunk_branches.emplace_back(), + ThunkSpecificRunImplFromThunkSequence(branch_sequence)); + } + + absl::string_view branch_execution_format = R"( + case {{CASE_INDEX}}: { + {{BRANCH_EXECUTION}} + break; + } + )"; + + std::vector branch_execution_impls; + branch_execution_impls.reserve(conditional_thunk_branches.size()); + + for (size_t i = 0; i < conditional_thunk_branches.size(); ++i) { + branch_execution_impls.push_back(absl::StrReplaceAll( + branch_execution_format, + { + {"{{CASE_INDEX}}", absl::StrCat(i)}, + {"{{BRANCH_EXECUTION}}", conditional_thunk_branches[i]}, + })); + } + + absl::string_view conditional_thunk_invocation_format = R"( + // Conditional Thunk + { + size_t branch_index = {{BRANCH_INDEX}}; + CHECK(branch_index < {{NUM_BRANCHES}}) << "branch_index is out of bounds"; + switch (branch_index) { + {{BRANCH_EXECUTIONS}} + } + })"; + + auto get_branch_index = + [](const xla::buffer_assignment::BufferAllocationSliceProto& + branch_index_buffer) -> absl::StatusOr { + if (branch_index_buffer.size() == sizeof(bool)) { + return absl::StrCat("*reinterpret_cast(", + GetBufferAllocationString(branch_index_buffer), + ") ? 0 : 1"); + } + if (branch_index_buffer.size() == sizeof(int32_t)) { + return absl::StrCat("*reinterpret_cast(", + GetBufferAllocationString(branch_index_buffer), ")"); + } + + return xla::Internal("Unsupported branch index buffer size %d", + branch_index_buffer.size()); + }; + + TF_ASSIGN_OR_RETURN( + std::string branch_index, + get_branch_index(conditional_thunk.branch_index_buffer())); + + return absl::StrReplaceAll( + conditional_thunk_invocation_format, + { + {"{{BRANCH_INDEX}}", branch_index}, + {"{{NUM_BRANCHES}}", absl::StrCat(branch_execution_impls.size())}, + {"{{BRANCH_EXECUTIONS}}", + absl::StrJoin(branch_execution_impls, "\n")}, + }); +} + +absl::StatusOr +ThunkProtoExecutionDeserializer::GetForLoopThunkRunImpl( + const xla::cpu::WhileThunkProto& while_thunk) { + if (!while_thunk.trip_count().contains_value()) { + return xla::Internal("While thunk is missing trip count."); + } + int64_t trip_count = while_thunk.trip_count().value(); + + absl::string_view for_loop_thunk_invocation_format = R"( + // For Loop Thunk + { + for (int64_t loop_counter = 0; loop_counter < {{TRIP_COUNT}}; ++loop_counter) { + {{BODY_EXECUTION}}; + } + } + )"; + + TF_ASSIGN_OR_RETURN( + std::string body_execution, + ThunkSpecificRunImplFromThunkSequence(while_thunk.body_sequence())); + + return absl::StrReplaceAll(for_loop_thunk_invocation_format, + { + {"{{TRIP_COUNT}}", absl::StrCat(trip_count)}, + {"{{BODY_EXECUTION}}", body_execution}, + }); +} + +absl::StatusOr +ThunkProtoExecutionDeserializer::GetWhileLoopThunkRunImpl( + const xla::cpu::WhileThunkProto& while_thunk) { + if (while_thunk.trip_count().contains_value()) { + return xla::Internal("While loop path should not have trip count."); + } + + absl::string_view while_loop_thunk_invocation_format = R"( + // While Loop Thunk + { + {{CONDITION_EXECUTION}}; + while ({{CONDITION}}) { + {{BODY_EXECUTION}}; + {{CONDITION_EXECUTION}}; + } + } + )"; + + TF_ASSIGN_OR_RETURN( + std::string body_execution, + ThunkSpecificRunImplFromThunkSequence(while_thunk.body_sequence())); + + TF_ASSIGN_OR_RETURN( + std::string condition_execution, + ThunkSpecificRunImplFromThunkSequence(while_thunk.cond_sequence())); + + return absl::StrReplaceAll( + while_loop_thunk_invocation_format, + { + {"{{CONDITION}}", + absl::StrCat("*reinterpret_cast(", + GetBufferAllocationString(while_thunk.cond_buffer()), + ")")}, + {"{{BODY_EXECUTION}}", body_execution}, + {"{{CONDITION_EXECUTION}}", condition_execution}, + }); +} + +absl::StatusOr +ThunkProtoExecutionDeserializer::GetWhileThunkRunImpl( + const xla::cpu::ThunkProto& thunk) { + if (!thunk.has_while_thunk()) { + return xla::Internal( + "While thunk was expected when getting thunk run implementation."); + } + const xla::cpu::WhileThunkProto& while_thunk = thunk.while_thunk(); + + if (!while_thunk.trip_count().contains_value()) { + return GetWhileLoopThunkRunImpl(while_thunk); + } + + return GetForLoopThunkRunImpl(while_thunk); +} + +absl::StatusOr +ThunkProtoExecutionDeserializer::CppDataTypeFromXlaType( + xla::PrimitiveType xla_type) { + switch (xla_type) { + case xla::F16: + return "Eigen::half"; + case xla::F32: + return "float"; + case xla::F64: + return "double"; + case xla::C64: + return "std::complex"; + case xla::C128: + return "std::complex"; + case xla::S32: + return "int32_t"; + default: + return xla::Internal("Unsupported xla type: %d", xla_type); + } +} + +} // namespace tfcompile +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/aot/thunk_proto_execution_deserializer.h b/western_ai/usa/tensorflow/tensorflow/compiler/aot/thunk_proto_execution_deserializer.h new file mode 100644 index 0000000000000000000000000000000000000000..b82f8639c5ac68117cf9f95360538c37499c5409 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/aot/thunk_proto_execution_deserializer.h @@ -0,0 +1,109 @@ +/* Copyright 2025 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_AOT_THUNK_PROTO_EXECUTION_DESERIALIZER_H_ +#define TENSORFLOW_COMPILER_AOT_THUNK_PROTO_EXECUTION_DESERIALIZER_H_ + +#include +#include + +#include "absl/status/statusor.h" +#include "xla/backends/cpu/runtime/convolution_dims.h" +#include "xla/backends/cpu/runtime/thunk.pb.h" +#include "xla/debug_options_flags.h" +#include "xla/service/cpu/executable.pb.h" +#include "xla/xla_data.pb.h" + +namespace tensorflow { +namespace tfcompile { + +// Helper class for deserializing the contents of specific thunks into C++ code +// that is used to codegen the `Run` method of the tfcompiled models. +class ThunkProtoExecutionDeserializer { + public: + explicit ThunkProtoExecutionDeserializer() + : xla_cpu_multi_thread_eigen_( + xla::GetDebugOptionsFromFlags().xla_cpu_multi_thread_eigen()) {} + + absl::StatusOr GetThunkSpecificRunImpl( + const xla::cpu::CompilationResultProto& proto) &&; + + absl::StatusOr ThunkSpecificRunImplFromThunkSequence( + const xla::cpu::ThunkSequenceProto& thunk_sequence_proto); + + protected: + absl::StatusOr GetMatmulFunction(xla::PrimitiveType xla_type); + + absl::StatusOr GetDotThunkRunImpl( + const xla::cpu::ThunkProto& thunk); + + absl::StatusOr GetConvolutionFunction( + xla::PrimitiveType xla_type); + + absl::StatusOr GetConvolution2DRunImpl( + const xla::cpu::ConvolutionThunkProto& convolution_thunk, + const xla::cpu::ConvolutionCanonicalDims& canonical_dims); + + absl::StatusOr GetConvolutionFusionThunkRunImpl( + const xla::cpu::ThunkProto& thunk); + + absl::StatusOr GetRngGetAndUpdateStateThunkRunImpl( + const xla::cpu::ThunkProto& thunk); + + absl::StatusOr GetRngSeedThunkRunImpl( + const xla::cpu::ThunkProto& thunk); + + absl::StatusOr GetCallThunkRunImpl( + const xla::cpu::ThunkProto& thunk); + + absl::StatusOr GetKernelThunkRunImpl( + const xla::cpu::ThunkProto& thunk); + + absl::StatusOr GetCopyThunkRunImpl( + const xla::cpu::ThunkProto& thunk); + + absl::StatusOr GetConditionalThunkRunImpl( + const xla::cpu::ThunkProto& thunk); + + absl::StatusOr GetForLoopThunkRunImpl( + const xla::cpu::WhileThunkProto& while_thunk); + + absl::StatusOr GetWhileLoopThunkRunImpl( + const xla::cpu::WhileThunkProto& while_thunk); + + absl::StatusOr GetWhileThunkRunImpl( + const xla::cpu::ThunkProto& thunk); + + absl::StatusOr GetSortThunkRunImpl( + const xla::cpu::ThunkProto& thunk); + + absl::StatusOr GetTopKThunkRunImpl( + const xla::cpu::ThunkProto& thunk); + + absl::StatusOr CppDataTypeFromXlaType( + xla::PrimitiveType xla_type); + + private: + // The index of the next rng state to use when deserializing the rng state + // from the ThunkProto. + int64_t rng_state_index_ = 0; + + bool xla_cpu_multi_thread_eigen_; +}; + +} // namespace tfcompile +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_AOT_THUNK_PROTO_EXECUTION_DESERIALIZER_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/jit/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..33fe654ef1fe552ab20acf66f803857f3a3870d1 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/BUILD @@ -0,0 +1,2115 @@ +load("@local_config_cuda//cuda:build_defs.bzl", "if_cuda") +load("@local_config_rocm//rocm:build_defs.bzl", "if_rocm") +load("@xla//xla/stream_executor:build_defs.bzl", "if_cuda_or_rocm") +load( + "@xla//xla/tsl:tsl.bzl", + "check_deps", + "internal_visibility", +) +load("//tensorflow:tensorflow.bzl", "if_libtpu", "if_with_tpu_support", "tf_cc_test", "tf_copts", "tf_cuda_cc_test", "tf_cuda_only_cc_test") +load("//tensorflow:tensorflow.default.bzl", "cc_header_only_library", "filegroup", "tf_custom_op_py_strict_library") +load("//tensorflow/compiler/jit:package_groups.bzl", "legacy_jit_users_package_group") +load("//tensorflow/core/platform:build_config.bzl", "tf_additional_all_protos", "tf_proto_library") +load( + "//tensorflow/core/platform:build_config_root.bzl", + "tf_cuda_tests_tags", +) +load("//tensorflow/core/platform:rules_cc.bzl", "cc_library") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:license"], + default_visibility = [ + ":internal", + "//third_party/cloud_tpu/inference_converter:__pkg__", + "//waymo/ml/deploy/system/aot_compilation:__pkg__", + ], + licenses = ["notice"], +) + +legacy_jit_users_package_group(name = "legacy_jit_users") + +package_group( + name = "internal", + includes = [ + "//tensorflow/compiler/tf2xla:internal", + ], + packages = [ + "//tensorflow/c/...", + "//tensorflow/compiler/tests/...", + "//tensorflow/core/tfrt/saved_model/...", + "//tensorflow/python/...", + ], +) + +package_group( + name = "friends", + includes = [ + "//tensorflow/compiler/tf2xla:friends", + ], +) + +# defs.cc/h only contains string constants, and can be included in mobile +# builds. +filegroup( + name = "mobile_srcs_no_runtime", + srcs = [ + "defs.cc", + ], + visibility = [":friends"], +) + +filegroup( + name = "mobile_hdrs_no_runtime", + srcs = [ + "defs.h", + ], + visibility = [":friends"], +) + +# Target that bundles up the XLA CPU and GPU JIT devices. +# This target is deprecated as it pulls in all the XLA devices and bloats the dependencies. +# Please use the individual targets in the deps list as needed. See b/336889334. +cc_library( + name = "jit", + visibility = internal_visibility([ + ":legacy_jit_users", + "//tensorflow/python/profiler:__pkg__", + "//tensorflow/python/kernel_tests/nn_ops:__pkg__", + ]), + deps = [ + ":xla_cpu_device", + ":xla_cpu_jit", + "//tensorflow/compiler/plugin", + ] + if_cuda_or_rocm([ + ":xla_gpu_device", + ":xla_gpu_jit", + ]) + if_with_tpu_support([ + ":xla_tpu_device", + ":xla_tpu_jit", + ]), + alwayslink = 1, +) + +cc_library( + name = "xla_cpu_jit", + visibility = ["//visibility:public"], + deps = [ + ":jit_compilation_passes", + ":xla_kernel_creator", # buildcleaner: keep + "//tensorflow/compiler/jit/kernels:xla_ops", + "//tensorflow/compiler/tf2xla/kernels:xla_dummy_ops", + "//tensorflow/compiler/tf2xla/kernels:xla_ops", + "//tensorflow/core/tfrt/common:pjrt_cpu_client_registration", + ] + if_with_tpu_support( + if_false = ["@xla//xla/service:cpu_plugin"], + if_true = [], + ), + alwayslink = 1, +) + +cc_library( + name = "xla_gpu_jit", + visibility = ["//visibility:public"], + deps = if_cuda_or_rocm([ + ":jit_compilation_passes", + ":xla_kernel_creator", # buildcleaner: keep + "//tensorflow/compiler/jit/kernels:xla_ops", + "//tensorflow/compiler/tf2xla/kernels:xla_ops", + "//tensorflow/compiler/tf2xla/kernels:xla_dummy_ops", + "@xla//xla/pjrt/plugin/xla_gpu:gpu_static_registration", + "@xla//xla/service:gpu_plugin", + "//tensorflow/core/tfrt/common:pjrt_gpu_client_registration", + ]) + if_cuda([ + "@xla//xla/stream_executor/cuda:all_runtime", # buildcleaner: keep + ]) + if_rocm([ + "@xla//xla/stream_executor/rocm:all_runtime", # buildcleaner: keep + ]), + alwayslink = 1, +) + +cc_library( + name = "xla_tpu_jit", + visibility = ["//visibility:public"], + deps = if_libtpu([ + "//tensorflow/core/tpu/graph_rewrite:tpu_rewrite_pass_registration", + "//tensorflow/core/tpu/graph_rewrite:configure_tpu_embedding_rewrite_registration", + ]), + alwayslink = 1, +) + +cc_library( + name = "xla_cpu_device", + srcs = ["xla_cpu_device.cc"], + visibility = [":friends"], + deps = [ + ":common", + ":flags", + ":jit_compilation_passes", + ":xla_device", + ":xla_kernel_creator", # buildcleaner: keep + "//tensorflow/compiler/jit/kernels:xla_ops", + "//tensorflow/compiler/tf2xla:layout_util", + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/compiler/tf2xla:xla_op_registry", + "//tensorflow/compiler/tf2xla/kernels:xla_ops", + "//tensorflow/core:core_cpu_internal", + "//tensorflow/core:lib", + "@com_google_absl//absl/memory", + "@xla//xla/stream_executor:platform_manager", + ] + if_libtpu( + if_false = [ + "@xla//xla/service:cpu_plugin", # buildcleaner: keep + ], + if_true = [], + ), + alwayslink = 1, +) + +cc_library( + name = "xla_gpu_device", + srcs = ["xla_gpu_device.cc"], + visibility = [":friends"], + deps = [ + ":common", + ":flags", + ":jit_compilation_passes", + ":xla_device", + ":xla_device_no_jit_rewrite_registration", + ":xla_kernel_creator", # buildcleaner: keep + "//tensorflow/compiler/jit/kernels:xla_ops", + "//tensorflow/compiler/tf2xla:layout_util", + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/compiler/tf2xla:xla_op_registry", + "//tensorflow/compiler/tf2xla/kernels:xla_ops", + "//tensorflow/core:core_cpu_internal", + "//tensorflow/core:lib", + "//tensorflow/core:protos_all_cc", + "@com_google_absl//absl/memory", + "@com_google_absl//absl/strings", + "@xla//xla/stream_executor:platform_manager", + "@xla//xla/stream_executor/gpu:gpu_init", + ] + if_libtpu( + if_false = [ + "@xla//xla/service:gpu_plugin", # buildcleaner: keep + ], + if_true = [], + ), + alwayslink = 1, +) + +cc_library( + name = "xla_tpu_device", + srcs = ["xla_tpu_device.cc"], + visibility = [":friends"], + deps = [ + ":xla_device", + "//tensorflow/compiler/jit/kernels:xla_ops_no_jit_rewrite_registration", + "//tensorflow/core:core_cpu_base", + "//tensorflow/core:framework_internal", + "//tensorflow/core/framework:kernel_def_proto_cc", + "//tensorflow/core/tpu:tpu_defs", + ], + alwayslink = 1, +) + +cc_library( + name = "xla_tensor", + srcs = ["xla_tensor.cc"], + hdrs = ["xla_tensor.h"], + visibility = [":friends"], + deps = [ + "//tensorflow/compiler/tf2xla:common", + "//tensorflow/core:core_cpu_internal", + "//tensorflow/core:framework", + "//tensorflow/core:lib", + "//tensorflow/core:lib_internal", + "@com_google_absl//absl/memory", + "@xla//xla:shape_util", + "@xla//xla/client:local_client", + "@xla//xla/service:shaped_buffer", + ], +) + +cc_library( + name = "xla_device_context", + srcs = ["xla_device_context.cc"], + hdrs = ["xla_device_context.h"], + visibility = ["//visibility:public"], + deps = [ + ":xla_tensor", + "//tensorflow/compiler/tf2xla:common", + "//tensorflow/compiler/tf2xla:layout_util", + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/compiler/tf2xla:xla_helpers", + "//tensorflow/core:framework", + "//tensorflow/core:framework_internal", + "//tensorflow/core:lib", + "//tensorflow/core:lib_proto_parsing", + "//tensorflow/core/common_runtime:dma_helper", + "//tensorflow/core/framework:allocator", + "//tensorflow/core/platform:errors", + "//tensorflow/core/platform:status", + "@com_google_absl//absl/log", + "@com_google_absl//absl/log:check", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings:string_view", + "@com_google_absl//absl/synchronization", + "@xla//xla:literal", + "@xla//xla:shape_util", + "@xla//xla:status_macros", + "@xla//xla/client:local_client", + "@xla//xla/service:stream_pool", + "@xla//xla/stream_executor:allocator_stats", + "@xla//xla/stream_executor:event", + "@xla//xla/stream_executor:stream", + "@xla//xla/stream_executor:stream_executor_h", + "@xla//xla/tsl/platform:errors", + "@xla//xla/tsl/platform:statusor", + ], +) + +cc_library( + name = "xla_device_no_jit_rewrite_registration", + srcs = [ + "xla_compile_on_demand_op.cc", + "xla_compiler_options_util.cc", + "xla_device.cc", + "xla_device_ops.cc", + "xla_ops_on_regular_devices.cc", + "xla_platform_info.cc", + ], + hdrs = [ + "xla_compile_on_demand_op.h", + "xla_compiler_options_util.h", + "xla_device.h", + "xla_device_ops.h", + "xla_platform_info.h", + ], + # Public visibility is needed for external TF/XLA backends. + visibility = ["//visibility:public"], + deps = [ + ":common", + ":device_compilation_cluster_signature", + ":device_compilation_profiler", + ":device_compiler", + ":device_executable_persistor", + ":flags_headers", + ":pjrt_base_device", + ":pjrt_device_compiler_client", + ":pjrt_device_context", + ":variable_info", + ":variable_info_util", + ":xla_compile_util", + ":xla_device_compiler_client", + ":xla_device_context", + ":xla_launch_util", + ":xla_tensor", + "//tensorflow/compiler/jit/ops:xla_ops", + "//tensorflow/compiler/tf2xla:common", + "//tensorflow/compiler/tf2xla:layout_util", + "//tensorflow/compiler/tf2xla:tf2xla_util", + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/compiler/tf2xla:xla_op_registry", + "//tensorflow/compiler/tf2xla/kernels:xla_dummy_ops", + "//tensorflow/compiler/tf2xla/kernels:xla_ops", + "//tensorflow/core:array_ops_op_lib", + "//tensorflow/core:control_flow_ops_op_lib", + "//tensorflow/core:core_cpu", + "//tensorflow/core:core_cpu_internal", + "//tensorflow/core:dataset_ops_op_lib", + "//tensorflow/core:framework", + "//tensorflow/core:framework_internal", + "//tensorflow/core:functional_ops_op_lib", + "//tensorflow/core:lib", + "//tensorflow/core:lib_internal", + "//tensorflow/core:math_ops_op_lib", + "//tensorflow/core:nn_ops_op_lib", + "//tensorflow/core:no_op_op_lib", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core:resource_variable_ops_op_lib", + "//tensorflow/core:sendrecv_ops_op_lib", + "//tensorflow/core:state_ops_op_lib", + "//tensorflow/core/kernels:constant_op", + "//tensorflow/core/kernels:fifo_queue", + "//tensorflow/core/kernels:function_ops", + "//tensorflow/core/kernels:identity_op", + "//tensorflow/core/kernels:resource_variable_ops", + "//tensorflow/core/kernels:shape_ops", + "//tensorflow/core/kernels:variable_ops", + "//tensorflow/core/kernels/data:finalize_dataset_op", + "//tensorflow/core/kernels/data:generator_dataset_op", + "//tensorflow/core/kernels/data:iterator_ops", + "//tensorflow/core/kernels/data:optional_ops", + "//tensorflow/core/kernels/data:options_dataset_op", + "//tensorflow/core/kernels/data:prefetch_dataset_op", + "//tensorflow/core/platform:stream_executor_no_cuda", + "//tensorflow/core/profiler/lib:traceme", + "//tensorflow/core/tfrt/common:async_value_tensor", + "//tensorflow/core/tfrt/common:create_pjrt_client_util", + "//tensorflow/core/tfrt/common:global_state", + "//tensorflow/core/tfrt/common:pjrt_util", + "//tensorflow/core/tpu:tpu_defs", + "@com_google_absl//absl/algorithm:container", + "@com_google_absl//absl/base", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/log", + "@com_google_absl//absl/log:check", + "@com_google_absl//absl/memory", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/types:optional", + "@com_google_absl//absl/types:span", + "@xla//xla:executable_run_options", + "@xla//xla/client:client_library", + "@xla//xla/client:local_client", + "@xla//xla/hlo/ir:hlo", + "@xla//xla/pjrt:pjrt_client", + "@xla//xla/pjrt:tf_pjrt_client", + "@xla//xla/service:compiler", + "@xla//xla/service:executable", + "@xla//xla/service:stream_pool", + "@xla//xla/service/gpu:gpu_executable_run_options", + "@xla//xla/stream_executor:platform_manager", + "@xla//xla/stream_executor:stream", + "@xla//xla/stream_executor/host:host_platform_id", + "@xla//xla/stream_executor/integrations:tf_allocator_adapter", + "@xla//xla/tsl/framework:device_id_utils", + ], + alwayslink = 1, +) + +check_deps( + name = "xla_device_check_deps", + disallowed_deps = [ + "@xla//xla/service/gpu:gpu_compiler", + "@xla//xla/service/cpu:cpu_compiler", + ], + deps = [":xla_device"], +) + +cc_library( + name = "xla_device", + hdrs = [ + "xla_compile_on_demand_op.h", + "xla_device.h", + "xla_device_ops.h", + ], + # Public visibility is needed for external TF/XLA backends. + visibility = ["//visibility:public"], + deps = [ + ":device_compilation_cluster_signature", + ":device_compilation_profiler", + ":jit_compilation_passes", + ":variable_info", + ":variable_info_util", + ":xla_device_no_jit_rewrite_registration", + ":xla_launch_util", + ":xla_tensor", + "//tensorflow/compiler/jit/ops:xla_ops", + "//tensorflow/compiler/tf2xla:layout_util", + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/compiler/tf2xla:xla_op_registry", + "//tensorflow/compiler/tf2xla/kernels:xla_dummy_ops", + "//tensorflow/compiler/tf2xla/kernels:xla_ops", + "//tensorflow/core:array_ops_op_lib", + "//tensorflow/core:control_flow_ops_op_lib", + "//tensorflow/core:core_cpu", + "//tensorflow/core:core_cpu_internal", + "//tensorflow/core:dataset_ops_op_lib", + "//tensorflow/core:framework", + "//tensorflow/core:framework_internal", + "//tensorflow/core:functional_ops_op_lib", + "//tensorflow/core:lib", + "//tensorflow/core:lib_internal", + "//tensorflow/core:math_ops_op_lib", + "//tensorflow/core:nn_ops_op_lib", + "//tensorflow/core:no_op_op_lib", + "//tensorflow/core:resource_variable_ops_op_lib", + "//tensorflow/core:sendrecv_ops_op_lib", + "//tensorflow/core:state_ops_op_lib", + "//tensorflow/core/kernels:constant_op", + "//tensorflow/core/kernels:fifo_queue", + "//tensorflow/core/kernels:function_ops", + "//tensorflow/core/kernels:identity_op", + "//tensorflow/core/kernels:resource_variable_ops", + "//tensorflow/core/kernels:shape_ops", + "//tensorflow/core/kernels:variable_ops", + "//tensorflow/core/kernels/data:finalize_dataset_op", + "//tensorflow/core/kernels/data:generator_dataset_op", + "//tensorflow/core/kernels/data:iterator_ops", + "//tensorflow/core/kernels/data:optional_ops", + "//tensorflow/core/kernels/data:options_dataset_op", + "//tensorflow/core/kernels/data:prefetch_dataset_op", + "//tensorflow/core/platform:stream_executor_no_cuda", + "//tensorflow/core/tfrt/common:async_value_tensor", + "@com_google_absl//absl/types:optional", + "@xla//xla/client:local_client", + "@xla//xla/pjrt:pjrt_client", + ], +) + +cc_library( + name = "shape_inference_helpers", + srcs = ["shape_inference_helpers.cc"], + hdrs = ["shape_inference_helpers.h"], + visibility = [":friends"], + deps = select({ + "//tensorflow:android": [ + "//tensorflow/core:portable_tensorflow_lib", + ], + "//conditions:default": [ + "//tensorflow/core:graph", + ], + }), +) + +cc_library( + name = "flags", + srcs = ["flags.cc"], + hdrs = ["flags.h"], + visibility = [":friends"], + deps = [ + "//tensorflow/compiler/mlir/tensorflow:dump_graph", + "//tensorflow/core:framework_internal", + "//tensorflow/core:framework_types_hdr", + "//tensorflow/core:lib", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core/tpu/kernels:sparse_core_xla_flags_defaults", + "@com_google_absl//absl/base", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/types:optional", + "@xla//xla:parse_flags_from_env", + ], +) + +# Header-only version of "flags" library, for linking from the shared object +# without ODR violations. +cc_library( + name = "flags_headers", + hdrs = ["flags.h"], + visibility = [":friends"], + deps = [ + "//tensorflow/core:framework_internal", + "//tensorflow/core:framework_types_hdr", + "//tensorflow/core:lib", + "//tensorflow/core/protobuf:for_core_protos_cc", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/types:optional", + ], +) + +cc_header_only_library( + name = "flags_headers_only", + features = [ + "-parse_headers", # buildifier: disable=no-parse-headers + ], + deps = [":flags_headers"], +) + +cc_library( + name = "common", + srcs = [ + "defs.cc", + ], + hdrs = [ + "defs.h", + ], + visibility = [":friends"], +) + +# Internal targets below this point. + +cc_library( + name = "variable_info", + srcs = ["variable_info.cc"], + hdrs = ["variable_info.h"], + visibility = [ + ":internal", + # We reuse VariableInfo in TFRT's implementation of TpuExecuteOp. + "//learning/brain/tfrt/tf_tpu:__pkg__", + "//learning/brain/tfrt/tpu_common:__pkg__", + "//tensorflow/core/common_runtime/next_pluggable_device:__pkg__", + ], + deps = [ + "//tensorflow/core:core_cpu_internal", + "//tensorflow/core:framework", + "//tensorflow/core:framework_internal", + "//tensorflow/core:lib", + "//tensorflow/core:lib_internal", + ], +) + +cc_library( + name = "variable_info_util", + srcs = ["variable_info_util.cc"], + hdrs = ["variable_info_util.h"], + visibility = [ + ":internal", + # We reuse VariableInfo in TFRT's implementation of TpuExecuteOp. + "//learning/brain/tfrt/tf_tpu:__pkg__", + ], + deps = [ + ":variable_info", + "//tensorflow/core:core_cpu_internal", + "//tensorflow/core:framework", + "//tensorflow/core:framework_internal", + "//tensorflow/core:lib", + "//tensorflow/core:lib_internal", + "@com_google_absl//absl/algorithm:container", + ], +) + +cc_library( + name = "pjrt_compile_util", + srcs = ["pjrt_compile_util.cc"], + hdrs = ["pjrt_compile_util.h"], + visibility = [ + ":internal", + "//tensorflow/core/tfrt/gpu/kernel:__pkg__", + ], + deps = [ + ":device_compilation_profiler", + ":device_compiler", + ":xla_compile_util", + ":xla_device_no_jit_rewrite_registration", + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/core:framework", + "//tensorflow/core:lib", + "@tsl//tsl/platform:statusor", + "@xla//xla/pjrt:pjrt_client", + ], +) + +tf_cuda_cc_test( + name = "pjrt_compile_util_test", + srcs = ["pjrt_compile_util_test.cc"], + tags = [ + "config-cuda-only", + "no_oss", # This test only runs with GPU. + "requires-gpu-nvidia", + "xla", + ], + deps = [ + ":pjrt_compile_util", + ":test_util", + ":xla_gpu_jit", + "//tensorflow/cc:function_ops", + "//tensorflow/cc:math_ops", + "//tensorflow/cc:scope", + "//tensorflow/core:framework", + "//tensorflow/core:test", + "//tensorflow/core/kernels:ops_testutil", + "//tensorflow/core/platform:errors", + "//tensorflow/core/platform:status", + "//tensorflow/core/platform:status_matchers", + "//tensorflow/core/platform:statusor", + "//tensorflow/core/tfrt/common:pjrt_util", + "@com_google_googletest//:gtest_main", + ], +) + +cc_library( + name = "xla_launch_util", + srcs = ["xla_launch_util.cc"], + hdrs = ["xla_launch_util.h"], + visibility = [ + ":internal", + # We reuse VariableInfo in TFRT's implementation of TpuExecuteOp. + "//learning/brain/tfrt/tf_tpu:__pkg__", + "//learning/brain/tfrt/tpu_plugin:__pkg__", + "//tensorflow/core/tfrt/gpu/kernel:__pkg__", + ], + deps = [ + ":pjrt_tensor_buffer", + ":pjrt_tensor_buffer_util", + ":variable_info", + ":variable_info_util", + ":xla_tensor", + "//tensorflow/compiler/tf2xla:common", + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/compiler/tf2xla:xla_resource", + "//tensorflow/core:core_cpu_internal", + "//tensorflow/core:framework", + "//tensorflow/core:framework_internal", + "//tensorflow/core:lib", + "//tensorflow/core:lib_internal", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core/common_runtime:dma_helper", + "//tensorflow/core/common_runtime:gpu_device_context", + "//tensorflow/core/common_runtime/gpu:gpu_serving_device_selector", + "//tensorflow/core/tfrt/common:async_value_tensor", + "@com_google_absl//absl/algorithm:container", + "@com_google_absl//absl/cleanup", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/log", + "@com_google_absl//absl/log:check", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/types:span", + "@xla//xla:future", + "@xla//xla:shape_util", + "@xla//xla:status_macros", + "@xla//xla:util", + "@xla//xla/client:local_client", + "@xla//xla/hlo/ir:hlo", + "@xla//xla/pjrt:pjrt_client", + "@xla//xla/pjrt:pjrt_common", + "@xla//xla/pjrt:pjrt_executable", + "@xla//xla/service:executable", + "@xla//xla/service:maybe_owning_device_memory", + "@xla//xla/service:shaped_buffer", + "@xla//xla/service:transfer_manager", + "@xla//xla/stream_executor:device_memory", + "@xla//xla/stream_executor:device_memory_allocator", + "@xla//xla/stream_executor:event", + "@xla//xla/stream_executor:platform", + "@xla//xla/stream_executor:platform_manager", + "@xla//xla/stream_executor/host:host_platform_id", + "@xla//xla/tsl/framework:device_id_utils", + "@xla//xla/tsl/framework:serving_device_selector_policies", + ], +) + +tf_cc_test( + name = "xla_launch_util_test", + srcs = ["xla_launch_util_test.cc"], + deps = [ + ":device_compilation_profiler", + ":device_compiler", + ":device_executable_persistor", + ":flags_headers", + ":pjrt_device_compiler_client", + ":variable_info", + ":variable_info_util", + ":xla_cpu_device", + ":xla_cpu_jit", + ":xla_device_no_jit_rewrite_registration", + ":xla_launch_util", + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/compiler/tf2xla:xla_op_registry", + "//tensorflow/core:framework", + "//tensorflow/core:lib", + "//tensorflow/core:portable_gif_internal", + "//tensorflow/core:session_options", + "//tensorflow/core/framework:fake_input", + "//tensorflow/core/framework:tensor_testutil", + "//tensorflow/core/kernels:ops_testutil", + "//tensorflow/core/platform:refcount", + "//tensorflow/core/tfrt/common:create_pjrt_client_util", + "//tensorflow/core/tfrt/common:pjrt_util", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/log:check", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/types:span", + "@com_google_googletest//:gtest_main", + "@xla//xla:literal", + "@xla//xla:literal_util", + "@xla//xla/pjrt:pjrt_client", + "@xla//xla/pjrt:pjrt_common", + "@xla//xla/pjrt:pjrt_executable", + "@xla//xla/pjrt/plugin/xla_cpu:cpu_client_options", + "@xla//xla/pjrt/plugin/xla_cpu:xla_cpu_pjrt_client", + "@xla//xla/tests:literal_test_util", + "@xla//xla/tsl/framework:device_id_utils", + "@xla//xla/tsl/lib/core:status_test_util", + "@xla//xla/tsl/platform:statusor", + ], +) + +tf_cuda_only_cc_test( + name = "xla_launch_util_gpu_test", + srcs = ["xla_launch_util_gpu_test.cc"], + deps = [ + ":device_compiler", + ":flags_headers", + ":pjrt_device_compiler_client", + ":pjrt_device_context", + ":variable_info", + ":variable_info_util", + ":xla_device_no_jit_rewrite_registration", + ":xla_gpu_jit", + ":xla_launch_util", + "//tensorflow/compiler/tf2xla:xla_op_registry", + "//tensorflow/core:framework", + "//tensorflow/core/framework:fake_input", + "//tensorflow/core/framework:tensor_testutil", + "//tensorflow/core/kernels:ops_testutil", + "//tensorflow/core/platform:refcount", + "//tensorflow/core/tfrt/common:create_pjrt_client_util", + "//tensorflow/core/tfrt/common:pjrt_util", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_googletest//:gtest_main", + "@tsl//tsl/platform:status", + "@tsl//tsl/platform:statusor", + "@xla//xla/pjrt:pjrt_client", + "@xla//xla/tests:literal_test_util", + "@xla//xla/tsl/lib/core:status_test_util", + ], +) + +cc_library( + name = "xla_compile_util", + srcs = ["xla_compile_util.cc"], + hdrs = ["xla_compile_util.h"], + visibility = [ + ":internal", + "//tensorflow/core/tfrt/gpu/kernel:__pkg__", + ], + deps = [ + ":flags_headers", + "//tensorflow/compiler/tf2xla:xla_argument", + "//tensorflow/core:core_cpu_base", + "//tensorflow/core:framework", + "//tensorflow/core/tfrt/common:global_state", + "//tensorflow/core/util:determinism", + "@com_google_absl//absl/status", + ], +) + +tf_cc_test( + name = "xla_compile_util_test", + srcs = [ + "xla_compile_util_test.cc", + ], + deps = [ + ":flags_headers", + ":xla_compile_util", + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/core:test_main", + "//tensorflow/core/framework:fake_input", + "//tensorflow/core/kernels:identity_op", + "//tensorflow/core/kernels:ops_testutil", + "//tensorflow/core/tpu:tpu_defs", + "@com_google_googletest//:gtest", + ], +) + +tf_proto_library( + name = "xla_compilation_cache_proto", + srcs = ["xla_compilation_cache.proto"], + protodeps = tf_additional_all_protos() + ["@xla//xla/service:hlo_proto"], + visibility = ["//visibility:public"], +) + +tf_proto_library( + name = "tf_graph_to_hlo_compiler_proto", + srcs = ["tf_graph_to_hlo_compiler.proto"], + protodeps = tf_additional_all_protos(), + visibility = ["//visibility:public"], + deps = ["//tensorflow/compiler/tf2xla:xla_argument_proto"], +) + +cc_library( + name = "device_compiler", + hdrs = ["device_compiler.h"], + copts = tf_copts(), + visibility = [":internal"], + deps = [ + ":device_compilation_cache", + ":device_compilation_cluster_signature", + ":device_compilation_profiler", + ":device_compiler_client", + ":device_executable_persistor", + ":flags_headers", + ":tf_graph_to_hlo_compiler", + ":xla_compile_util", + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/core:framework", + "//tensorflow/core:framework_lite", + "//tensorflow/core:lib", + "//tensorflow/core/framework:resource_base", + "//tensorflow/core/platform:thread_annotations", + "@com_google_absl//absl/algorithm:container", + "@com_google_absl//absl/base", + "@com_google_absl//absl/base:nullability", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/status", + "@com_google_absl//absl/types:span", + ], +) + +tf_cc_test( + name = "device_compiler_disable_test", + srcs = [ + "device_compiler_disable_test.cc", + ], + tags = ["no_cuda_on_cpu_tap"], + deps = [ + ":device_compilation_profiler", + ":device_compiler", + ":flags", + ":xla_cpu_jit", + ":xla_device_compiler_client", + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/core:test", + "//tensorflow/core:test_main", + "@xla//xla/client:client_library", + ], +) + +cc_library( + name = "jit_compilation_passes", + srcs = ["jit_compilation_pass_registration.cc"], + deps = [ + ":compilation_passes", + ":xla_activity_logging_listener", + "//tensorflow/compiler/tf2xla:functionalize_control_flow_pass_registration", + "//tensorflow/compiler/tf2xla:mlir_bridge_pass_registration", + "//tensorflow/core:core_cpu_internal", + ], + alwayslink = 1, +) + +cc_library( + name = "get_compiler_ir", + srcs = ["get_compiler_ir.cc"], + hdrs = ["get_compiler_ir.h"], + visibility = [":internal"], + deps = [ + ":compilability_check_util", + ":device_compiler", + ":variable_info", + ":variable_info_util", + ":xla_device_no_jit_rewrite_registration", + ":xla_launch_util", + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/core:framework", + "//tensorflow/core:lib", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core/common_runtime:core_cpu_internal", + "//tensorflow/core/common_runtime/eager:tensor_handle", + "//tensorflow/core/platform:refcount", + "@com_google_absl//absl/algorithm:container", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/types:span", + "@xla//xla:status_macros", + "@xla//xla/client:executable_build_options", + "@xla//xla/client:local_client", + "@xla//xla/hlo/translate:portable_api", + "@xla//xla/service:hlo_graph_dumper", + "@xla//xla/stream_executor:platform", + "@xla//xla/stream_executor/host:host_platform_id", + ], + alwayslink = 1, +) + +# Header-only version of "flags" library, for linking from the shared object +# without ODR violations. +cc_library( + name = "get_compiler_ir_hdrs", + textual_hdrs = ["get_compiler_ir.h"], + visibility = [":internal"], + deps = [ + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/core:framework", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core/platform:statusor", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/types:span", + ], +) + +cc_header_only_library( + name = "get_compiler_ir_hdrs_only", + features = [ + "-parse_headers", # buildifier: disable=no-parse-headers + ], + deps = [":get_compiler_ir_hdrs"], +) + +# This target can be used by XLA device plugins to prevent circular dependencies, and provides access to all of the required headers for building a device library. +cc_header_only_library( + name = "xla_jit_headers_lib", + visibility = ["//visibility:public"], + deps = [ + ":xla_cpu_device", + ":xla_cpu_jit", + ":xla_gpu_device", + ":xla_gpu_jit", + ], +) + +cc_library( + name = "xla_kernel_creator", + srcs = [ + "xla_kernel_creator.cc", + ], + hdrs = [ + "xla_kernel_creator.h", + ], + visibility = [ + ":internal", + "//tensorflow/core/common_runtime/eager:__pkg__", + ], + deps = [ + ":common", + ":compilability_check_util", + ":compilation_passes", + ":flags", + ":jit_compilation_passes", + "//tensorflow/compiler/jit/kernels:xla_ops_no_jit_rewrite_registration", + "//tensorflow/compiler/tf2xla:mlir_bridge_pass", + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/compiler/tf2xla:xla_op_registry", + "//tensorflow/core:core_cpu_internal", + "//tensorflow/core:framework", + "//tensorflow/core:lib", + "//tensorflow/core:protos_all_cc", + "@com_google_absl//absl/memory", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/strings:str_format", + ], + alwayslink = 1, +) + +tf_cc_test( + name = "xla_kernel_creator_test", + srcs = [ + "xla_kernel_creator.h", + "xla_kernel_creator_test.cc", + ], + deps = [ + ":xla_kernel_creator", + "//tensorflow/core:core_cpu_internal", + "//tensorflow/core:framework", + "//tensorflow/core:lib", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core:session_options", + "//tensorflow/core:test", + "//tensorflow/core:test_main", + "@com_google_absl//absl/log:check", + "@com_google_absl//absl/status", + ], +) + +cc_library( + name = "resource_operation_safety_analysis", + srcs = ["resource_operation_safety_analysis.cc"], + hdrs = ["resource_operation_safety_analysis.h"], + deps = [ + ":xla_cluster_util", + "//tensorflow/compiler/tf2xla:resource_operation_table", + "//tensorflow/core:framework", + "//tensorflow/core:graph", + "//tensorflow/core:lib", + "//tensorflow/core:protos_all_cc", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/memory", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/types:optional", + "@xla//xla/service/graphcycles", + ], +) + +tf_cc_test( + name = "resource_operation_safety_analysis_test", + srcs = ["resource_operation_safety_analysis_test.cc"], + deps = [ + ":common", + ":resource_operation_safety_analysis", + "//tensorflow/cc:cc_ops", + "//tensorflow/cc:cc_ops_internal", + "//tensorflow/cc:function_ops", + "//tensorflow/cc:functional_ops", + "//tensorflow/cc:ops", + "//tensorflow/cc:resource_variable_ops", + "//tensorflow/cc:sendrecv_ops", + "//tensorflow/compiler/jit/kernels:xla_ops", + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/compiler/tf2xla/kernels:xla_ops", + "//tensorflow/core:core_cpu", + "//tensorflow/core:framework", + "//tensorflow/core:framework_internal", + "//tensorflow/core:graph", + "//tensorflow/core:lib", + "//tensorflow/core:test", + "//tensorflow/core:test_main", + "//tensorflow/core:testlib", + "@com_google_absl//absl/strings", + ], +) + +cc_library( + name = "shape_inference", + srcs = ["shape_inference.cc"], + hdrs = ["shape_inference.h"], + visibility = [":friends"], + deps = [ + ":shape_inference_helpers", + "//tensorflow/core:core_cpu_internal", + "//tensorflow/core:framework", + "//tensorflow/core:lib", + "//tensorflow/core:protos_all_cc", + "@com_google_absl//absl/log", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + ], +) + +cc_library( + name = "test_util", + testonly = 1, + srcs = ["test_util.cc"], + hdrs = ["test_util.h"], + deps = [ + ":shape_inference", + "//tensorflow/core:core_cpu", + "//tensorflow/core:core_cpu_base", + "//tensorflow/core:framework", + "//tensorflow/core:lib", + "@com_google_absl//absl/log:check", + "@com_google_absl//absl/status", + "@com_google_absl//absl/strings", + "@xla//xla:status_macros", + ], +) + +tf_cc_test( + name = "shape_inference_test", + srcs = ["shape_inference_test.cc"], + deps = [ + ":shape_inference", + ":test_util", + "//tensorflow/cc:cc_ops", + "//tensorflow/cc:cc_ops_internal", + "//tensorflow/cc:ops", + "//tensorflow/cc:resource_variable_ops", + "//tensorflow/cc:scope", + "//tensorflow/core:core_cpu_base", + "//tensorflow/core:framework", + "//tensorflow/core:ops", + "//tensorflow/core:test", + "//tensorflow/core:test_main", + "//tensorflow/core/kernels:constant_op", + "@com_google_absl//absl/log:check", + ], +) + +cc_library( + name = "encapsulate_util", + srcs = ["encapsulate_util.cc"], + hdrs = ["encapsulate_util.h"], + deps = [ + ":shape_inference", + "//tensorflow/compiler/tf2xla:tf2xla_util", + "//tensorflow/core:framework", + "//tensorflow/core:graph", + "//tensorflow/core:protos_all_cc", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/types:optional", + ], +) + +tf_cc_test( + name = "encapsulate_util_test", + srcs = ["encapsulate_util_test.cc"], + deps = [ + ":encapsulate_util", + "//tensorflow/cc:cc_ops", + "//tensorflow/cc:ops", + "//tensorflow/cc:scope", + "//tensorflow/core:core_cpu_base", + "//tensorflow/core:framework", + "//tensorflow/core:ops", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core:test", + "//tensorflow/core:test_main", + "@com_google_absl//absl/log:check", + ], +) + +cc_library( + name = "compilation_passes", + srcs = [ + "build_xla_ops_pass.cc", + "clone_constants_for_better_clustering.cc", + "cluster_scoping_pass.cc", + "deadness_analysis.cc", + "deadness_analysis_internal.h", + "encapsulate_subgraphs_pass.cc", + "encapsulate_xla_computations_pass.cc", + "extract_outside_compilation_pass.cc", + "force_xla_constants_on_host_pass.cc", + "increase_dynamism_for_auto_jit_pass.cc", + "mark_for_compilation_pass.cc", + "mark_for_compilation_pass_test_helper.cc", + "partially_decluster_pass.cc", + "report_clustering_info_pass.cc", + ], + hdrs = [ + "build_xla_ops_pass.h", + "clone_constants_for_better_clustering.h", + "cluster_scoping_pass.h", + "deadness_analysis.h", + "encapsulate_subgraphs_pass.h", + "encapsulate_xla_computations_pass.h", + "extract_outside_compilation_pass.h", + "force_xla_constants_on_host_pass.h", + "increase_dynamism_for_auto_jit_pass.h", + "mark_for_compilation_pass.h", + "mark_for_compilation_pass_test_helper.h", + "partially_decluster_pass.h", + "report_clustering_info_pass.h", + ], + visibility = [ + ":internal", + "//third_party/cloud_tpu/inference_converter:__pkg__", + "//waymo/onboard/ml/chauffeur_net:__pkg__", + ], + deps = [ + "compilability_check_util", + ":common", + ":device_util", + ":encapsulate_util", + ":flags", + ":resource_operation_safety_analysis", + ":shape_inference_helpers", + ":xla_activity_listener", + ":xla_cluster_util", + "//tensorflow/cc:cc_ops", + "//tensorflow/cc:functional_ops", + "//tensorflow/cc:ops", + "//tensorflow/cc:scope", + "//tensorflow/cc:scope_internal", + "//tensorflow/compiler/jit/ops:xla_ops", + "//tensorflow/compiler/tf2xla:resource_operation_table", + "//tensorflow/compiler/tf2xla:side_effect_util", + "//tensorflow/compiler/tf2xla:tf2xla_util", + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/compiler/tf2xla:xla_op_registry", + "//tensorflow/compiler/tf2xla/cc:xla_jit_ops", + "//tensorflow/compiler/tf2xla/cc:xla_ops", + "//tensorflow/core:core_cpu", + "//tensorflow/core:core_cpu_internal", + "//tensorflow/core:framework", + "//tensorflow/core:lib", + "//tensorflow/core:lib_internal", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core/framework:bounds_check", + "@com_google_absl//absl/algorithm:container", + "@com_google_absl//absl/base", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/container:inlined_vector", + "@com_google_absl//absl/log", + "@com_google_absl//absl/log:check", + "@com_google_absl//absl/memory", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/types:optional", + "@xla//xla:status_macros", + "@xla//xla:union_find", + "@xla//xla:util", + "@xla//xla:xla_data_proto_cc", + "@xla//xla/service/graphcycles", + ], +) + +cc_library( + name = "xla_cluster_util", + srcs = ["xla_cluster_util.cc"], + hdrs = ["xla_cluster_util.h"], + deps = [ + ":flags", + ":xla_activity_proto_cc", + "//tensorflow/core:core_cpu", + "//tensorflow/core:framework", + "//tensorflow/core:framework_internal", + "//tensorflow/core:graph", + "//tensorflow/core:lib", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core/common_runtime:function_body", + "//tensorflow/core/framework:bounds_check", + "//tensorflow/core/platform:hash", + "@com_google_absl//absl/algorithm:container", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/container:inlined_vector", + "@com_google_absl//absl/log", + "@com_google_absl//absl/log:check", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/types:optional", + "@xla//xla:status_macros", + "@xla//xla/service/graphcycles", + ], +) + +cc_library( + name = "device_util", + srcs = ["device_util.cc"], + hdrs = ["device_util.h"], + deps = [ + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/compiler/tf2xla:xla_op_registry", + "//tensorflow/core:framework", + "@com_google_absl//absl/algorithm:container", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/numeric:bits", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/types:span", + "@xla//xla:status_macros", + ], +) + +tf_cc_test( + name = "device_util_test", + srcs = ["device_util_test.cc"], + deps = [ + ":device_util", + "//tensorflow/core:test", + "//tensorflow/core:test_main", + "//tensorflow/core:testlib", + ], +) + +tf_cc_test( + name = "deadness_analysis_test", + size = "small", + srcs = [ + "deadness_analysis_internal.h", + "deadness_analysis_test.cc", + ], + deps = [ + ":common", + ":compilation_passes", + "//tensorflow/cc:cc_ops", + "//tensorflow/cc:cc_ops_internal", + "//tensorflow/cc:function_ops", + "//tensorflow/cc:ops", + "//tensorflow/cc:sendrecv_ops", + "//tensorflow/compiler/jit/kernels:xla_ops", + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/compiler/tf2xla/kernels:xla_ops", + "//tensorflow/core:core_cpu", + "//tensorflow/core:framework", + "//tensorflow/core:framework_internal", + "//tensorflow/core:graph", + "//tensorflow/core:lib", + "//tensorflow/core:test", + "//tensorflow/core:test_main", + "//tensorflow/core:testlib", + "@com_google_absl//absl/container:flat_hash_map", + ], +) + +cc_library( + name = "compilation_passes_test_main", + testonly = True, + srcs = ["compilation_passes_test_main.cc"], + visibility = ["//visibility:public"], + deps = [ + ":flags", + "//tensorflow/core:lib", + "//tensorflow/core:test", + "@com_google_absl//absl/strings", + ], +) + +tf_cc_test( + name = "compilation_passes_test", + size = "small", + srcs = [ + "build_xla_ops_pass_test.cc", + "clone_constants_for_better_clustering_test.cc", + "cluster_scoping_pass_test.cc", + "encapsulate_subgraphs_pass_test.cc", + "encapsulate_xla_computations_pass_test.cc", + "extract_outside_compilation_pass_test.cc", + "force_xla_constants_on_host_pass_test.cc", + "increase_dynamism_for_auto_jit_pass_test.cc", + "mark_for_compilation_pass_test.cc", + "partially_decluster_pass_test.cc", + "rearrange_function_argument_pass_test.cc", + ], + tags = [ + # TODO(b/141643254) Re-enable msan after fixing + # use-of-uninitialized-value error. + "nomsan", + ] + tf_cuda_tests_tags(), + deps = [ + ":common", + ":compilability_check_util", + ":compilation_passes", + ":compilation_passes_test_main", + ":encapsulate_util", + ":flags", + ":node_matchers", + ":test_util", + ":xla_cluster_util", + ":xla_cpu_device", + ":xla_gpu_device", + "//tensorflow/cc:cc_ops", + "//tensorflow/cc:cc_ops_internal", + "//tensorflow/cc:function_ops", + "//tensorflow/cc:functional_ops", + "//tensorflow/cc:ops", + "//tensorflow/cc:resource_variable_ops", + "//tensorflow/cc:scope", + "//tensorflow/cc:sendrecv_ops", + "//tensorflow/compiler/jit/kernels:xla_ops", + "//tensorflow/compiler/tf2xla:rearrange_function_argument", + "//tensorflow/compiler/tf2xla:side_effect_util", + "//tensorflow/compiler/tf2xla:test_util", + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/compiler/tf2xla/cc:xla_jit_ops", + "//tensorflow/compiler/tf2xla/cc:xla_ops", + "//tensorflow/compiler/tf2xla/kernels:xla_dummy_ops", + "//tensorflow/compiler/tf2xla/kernels:xla_ops", + "//tensorflow/core:all_kernels", + "//tensorflow/core:core_cpu", + "//tensorflow/core:framework", + "//tensorflow/core:framework_internal", + "//tensorflow/core:lib", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core:session_options", + "//tensorflow/core:test", + "//tensorflow/core:testlib", + "//tensorflow/core/common_runtime:device_set", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/log", + "@com_google_absl//absl/log:check", + "@com_google_absl//absl/memory", + "@com_google_absl//absl/status", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/types:span", + "@com_google_googletest//:gtest", + "@xla//xla/hlo/testlib:test", + ], +) + +tf_cc_test( + name = "xla_cluster_util_test", + size = "small", + srcs = [ + "xla_cluster_util_test.cc", + ], + deps = [ + ":common", + ":xla_cluster_util", + "//tensorflow/cc:cc_ops", + "//tensorflow/cc:cc_ops_internal", + "//tensorflow/cc:function_ops", + "//tensorflow/cc:functional_ops", + "//tensorflow/cc:ops", + "//tensorflow/compiler/jit/kernels:xla_ops", + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/compiler/tf2xla/kernels:xla_ops", + "//tensorflow/core:core_cpu", + "//tensorflow/core:core_cpu_lib", + "//tensorflow/core:framework", + "//tensorflow/core:framework_internal", + "//tensorflow/core:lib", + "//tensorflow/core:test", + "//tensorflow/core:test_main", + "//tensorflow/core:testlib", + "@com_google_absl//absl/algorithm:container", + "@com_google_absl//absl/strings", + "@xla//xla:status_macros", + ], +) + +cc_library( + name = "node_matchers", + testonly = True, + srcs = ["node_matchers.cc"], + hdrs = ["node_matchers.h"], + deps = [ + "//tensorflow/cc:ops", + "//tensorflow/core:framework", + "//tensorflow/core:graph", + "//tensorflow/core:protos_all_cc", + "@com_google_absl//absl/algorithm:container", + "@com_google_absl//absl/log", + "@com_google_absl//absl/log:check", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/types:optional", + "@com_google_absl//absl/types:span", + "@xla//xla/hlo/testlib:test", + ], +) + +tf_cc_test( + name = "node_matchers_test", + srcs = ["node_matchers_test.cc"], + deps = [ + ":node_matchers", + "//tensorflow/cc:cc_ops", + "//tensorflow/cc:cc_ops_internal", + "//tensorflow/cc:ops", + "//tensorflow/core:ops", + "//tensorflow/core:test_main", + ], +) + +cc_library( + name = "compilability_check_util", + srcs = ["compilability_check_util.cc"], + hdrs = ["compilability_check_util.h"], + visibility = [ + ":friends", + ], + deps = [ + ":common", + ":device_util", + ":flags", + ":resource_operation_safety_analysis", + ":xla_activity_listener", + ":xla_activity_proto_cc", + ":xla_cluster_util", + "//tensorflow/compiler/tf2xla:resource_operation_table", + "//tensorflow/compiler/tf2xla:tf2xla_defs", + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/compiler/tf2xla:xla_op_registry", + "//tensorflow/core:core_cpu", + "//tensorflow/core:framework", + "//tensorflow/core:graph", + "//tensorflow/core:lib", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core/common_runtime:function_body", + "//tensorflow/core/common_runtime:function_utils", + "@com_google_absl//absl/algorithm:container", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/log", + "@com_google_absl//absl/log:check", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/types:optional", + "@com_google_absl//absl/types:span", + "@xla//xla:union_find", + "@xla//xla:util", + "@xla//xla/service/graphcycles", + ], +) + +tf_cc_test( + name = "compilability_check_util_test", + srcs = ["compilability_check_util_test.cc"], + deps = [ + ":common", + ":compilability_check_util", + ":xla_cpu_device", + ":xla_cpu_jit", + "//tensorflow/cc:cc_ops", + "//tensorflow/cc:function_ops", + "//tensorflow/cc:functional_ops", + "//tensorflow/cc:ops", + "//tensorflow/cc:scope", + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/core:core_cpu", + "//tensorflow/core:framework", + "//tensorflow/core:lib", + "//tensorflow/core:ops", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core:test", + "//tensorflow/core:test_main", + "@com_google_absl//absl/log:check", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/types:span", + ], +) + +tf_cc_test( + name = "xla_activity_listener_test", + srcs = ["xla_activity_listener_test.cc"], + deps = [ + ":flags", + ":xla_activity_listener", + ":xla_cpu_device", + ":xla_cpu_jit", + "//tensorflow/cc:cc_ops", + "//tensorflow/cc:ops", + "//tensorflow/core:all_kernels", + "//tensorflow/core:core_cpu", + "//tensorflow/core:framework", + "//tensorflow/core:ops", + "//tensorflow/core:test", + "//tensorflow/core/common_runtime:direct_session_internal", + "//tensorflow/core/kernels:cwise_op", + "//tensorflow/core/kernels:matmul_op", + "//tensorflow/core/kernels:partitioned_function_ops", + ], +) + +tf_custom_op_py_strict_library( + name = "xla_ops_py", + kernels = ["//tensorflow/compiler/jit/ops:xla_ops"], + visibility = [ + ":friends", + ], + deps = [ + "//tensorflow/compiler/jit/ops:xla_ops_grad", + "//tensorflow/compiler/jit/ops:xla_ops_wrapper_py", + ], +) + +cc_library( + name = "xla_activity_listener", + srcs = ["xla_activity_listener.cc"], + hdrs = ["xla_activity_listener.h"], + visibility = ["//visibility:public"], + deps = [ + ":xla_activity_proto_cc", + "//tensorflow/core:lib", + "@com_google_absl//absl/synchronization", + ], +) + +tf_proto_library( + name = "xla_activity_proto", + srcs = ["xla_activity.proto"], + protodeps = tf_additional_all_protos(), +) + +cc_library( + name = "xla_activity_logging_listener", + srcs = ["xla_activity_logging_listener.cc"], + deps = [ + ":xla_activity_listener", + ":xla_activity_proto_cc", + "//tensorflow/core:lib", + "@com_google_absl//absl/memory", + ], + alwayslink = 1, +) + +cc_library( + name = "tf_to_hlo_compiler", + hdrs = ["tf_to_hlo_compiler.h"], + deps = [ + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/core:framework", + ], +) + +cc_library( + name = "tf_graph_to_hlo_compiler", + srcs = ["tf_graph_to_hlo_compiler.cc"], + hdrs = ["tf_graph_to_hlo_compiler.h"], + deps = [ + ":tf_graph_to_hlo_compiler_proto_cc", + ":tf_to_hlo_compiler", + "//tensorflow/compiler/tf2xla:xla_argument", + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/core:framework", + "//tensorflow/core:lib", + "@com_google_absl//absl/log", + "@com_google_absl//absl/status", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/types:span", + "@tsl//tsl/platform:path", + "@xla//xla/tsl/lib/strings:proto_serialization", + ], +) + +cc_library( + name = "device_compilation_profiler", + srcs = ["device_compilation_profiler.cc"], + hdrs = ["device_compilation_profiler.h"], + deps = [ + ":xla_activity_listener", + ":xla_activity_proto_cc", + ":xla_compile_util", + "//tensorflow/core:framework", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core/platform:errors", + "//tensorflow/core/platform:status", + "@com_google_absl//absl/strings", + "@tsl//tsl/platform:mutex", + ], +) + +cc_library( + name = "device_compiler_client", + srcs = ["device_compiler_client.cc"], + hdrs = ["device_compiler_client.h"], + visibility = [":internal"], + deps = [ + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/core/util:determinism", + "@xla//xla/client:executable_build_options", + ], +) + +tf_cc_test( + name = "device_compiler_client_test", + srcs = ["device_compiler_client_test.cc"], + deps = [ + ":device_compiler_client", + "@com_google_googletest//:gtest_main", + ], +) + +cc_library( + name = "xla_device_compiler_client", + srcs = ["xla_device_compiler_client.cc"], + hdrs = ["xla_device_compiler_client.h"], + deps = [ + ":device_compiler_client", + "@xla//xla/client:local_client", + ], +) + +cc_library( + name = "device_executable_persistor", + srcs = ["device_executable_persistor.cc"], + hdrs = ["device_executable_persistor.h"], + deps = [ + ":device_compiler_client", + ":xla_compilation_cache_proto_cc", + ":xla_device_compiler_client", + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/core:core_cpu_base", + "//tensorflow/core:framework", + "//tensorflow/core:lib_headers_for_pybind", + "//tensorflow/core:portable_gif_internal", + "//tensorflow/core/platform:path", + "//tensorflow/core/platform:status", + "//tensorflow/core/platform:statusor", + "@com_google_absl//absl/log", + "@com_google_absl//absl/status", + "@com_google_absl//absl/strings", + "@tsl//tsl/platform:errors", + "@tsl//tsl/platform:statusor", + "@xla//xla:util", + "@xla//xla/pjrt:pjrt_client", + "@xla//xla/service:hlo_proto_cc", + ], +) + +cc_library( + name = "device_compilation_cache", + hdrs = ["device_compilation_cache.h"], + deps = [ + ":device_compilation_cluster_signature", + ":xla_compile_util", + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/core:framework_lite", + "@com_google_absl//absl/base:nullability", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/strings", + "@xla//xla/client:local_client", + "@xla//xla/pjrt:pjrt_client", + ], +) + +cc_library( + name = "device_compilation_cluster_signature", + srcs = ["device_compilation_cluster_signature.cc"], + hdrs = ["device_compilation_cluster_signature.h"], + deps = [ + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/core:framework", + "//tensorflow/core/platform:types", + "@com_google_absl//absl/container:inlined_vector", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/types:span", + ], +) + +cc_library( + name = "pjrt_device_compiler_client", + srcs = ["pjrt_device_compiler_client.cc"], + hdrs = ["pjrt_device_compiler_client.h"], + deps = [ + ":device_compiler_client", + "@xla//xla/pjrt:pjrt_client", + ], +) + +cc_library( + name = "pjrt_base_device", + srcs = ["pjrt_base_device.cc"], + hdrs = ["pjrt_base_device.h"], + # Public visibility is needed for external TF/XLA backends. + visibility = ["//visibility:public"], + deps = [ + "//tensorflow/compiler/tf2xla:layout_util", + "//tensorflow/core:framework", + "//tensorflow/core/common_runtime:local_device", + ], +) + +cc_library( + name = "pjrt_tensor_buffer", + hdrs = ["pjrt_tensor_buffer.h"], + visibility = ["//visibility:public"], + deps = [ + "//tensorflow/core:framework", + "//tensorflow/core:protos_all_cc", + "@xla//xla/pjrt:pjrt_client", + ], +) + +cc_library( + name = "pjrt_tensor_buffer_util", + srcs = ["pjrt_tensor_buffer_util.cc"], + hdrs = ["pjrt_tensor_buffer_util.h"], + visibility = ["//visibility:public"], + deps = [ + ":pjrt_tensor_buffer", + "//tensorflow/core:framework", + "//tensorflow/core/common_runtime:dma_helper", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@tsl//tsl/platform:statusor", + "@xla//xla/pjrt:pjrt_client", + ], +) + +tf_cuda_only_cc_test( + name = "pjrt_tensor_buffer_util_test", + srcs = ["pjrt_tensor_buffer_util_test.cc"], + tags = tf_cuda_tests_tags() + [ + "config-cuda-only", + "no_oss", + ], + deps = [ + ":pjrt_tensor_buffer_util", + ":test_util", + ":xla_gpu_jit", + "//tensorflow/core:framework", + "//tensorflow/core:gpu_runtime", # Needed to register DEVICE_GPU + "//tensorflow/core/tfrt/common:pjrt_util", + "@com_google_googletest//:gtest_main", + "@tsl//tsl/platform:statusor", + "@xla//xla:shape_util", + "@xla//xla/pjrt:pjrt_client", + ], +) + +cc_library( + name = "pjrt_device_context", + srcs = [ + "pjrt_device_context.cc", + ], + hdrs = [ + "pjrt_device_context.h", + ], + visibility = ["//visibility:public"], + deps = [ + ":pjrt_tensor_buffer", + ":pjrt_tensor_buffer_util", + "//tensorflow/c/experimental/next_pluggable_device:tensor_pjrt_buffer_util", + "//tensorflow/compiler/tf2xla:common", + "//tensorflow/compiler/tf2xla:layout_util", + "//tensorflow/core:framework", + "//tensorflow/core/common_runtime:dma_helper", + "//tensorflow/core/common_runtime/next_pluggable_device:next_pluggable_device_api", + "//tensorflow/core/platform:status", + "//tensorflow/core/profiler/lib:traceme", + "//tensorflow/core/tfrt/common:async_value_tensor", + "//tensorflow/core/tfrt/common:create_pjrt_client_util", + "@com_google_absl//absl/status", + "@xla//xla/pjrt:pjrt_client", + "@xla//xla/pjrt:pjrt_common", + "@xla//xla/tsl/c:tsl_status_internal", + "@xla//xla/tsl/framework:device_id_utils", + ], +) + +cc_library( + name = "xla_host_recv_device_context", + srcs = [ + "xla_host_recv_device_context.cc", + ], + hdrs = [ + "xla_host_recv_device_context.h", + ], + visibility = ["//visibility:public"], + deps = [ + "//tensorflow/compiler/tf2xla:common", + "//tensorflow/core:framework", + "@tf_runtime//:async_value", + "@xla//xla:shape_util", + "@xla//xla/stream_executor:device_memory", + "@xla//xla/stream_executor:stream", + ], +) + +cc_library( + name = "xla_host_send_device_context", + srcs = [ + "xla_host_send_device_context.cc", + ], + hdrs = [ + "xla_host_send_device_context.h", + ], + visibility = ["//visibility:public"], + deps = [ + "//tensorflow/compiler/tf2xla:common", + "//tensorflow/core:framework", + "@tf_runtime//:async_value", + "@xla//xla:shape_util", + "@xla//xla/stream_executor:device_memory", + "@xla//xla/stream_executor:stream", + ], +) + +tf_cuda_only_cc_test( + name = "xla_host_send_recv_device_context_test", + srcs = ["xla_host_send_recv_device_context_test.cc"], + tags = tf_cuda_tests_tags() + [ + "config-cuda-only", + "no_oss", # Temporarily disable OSS. + ], + deps = [ + ":flags", + ":xla_device", + ":xla_gpu_device", + ":xla_host_recv_device_context", + ":xla_host_send_device_context", + "//tensorflow/compiler/tf2xla:common", + "//tensorflow/compiler/tf2xla:xla_op_registry", + "//tensorflow/core:framework_internal", + "//tensorflow/core:gpu_runtime", # Needed to register DEVICE_GPU + "//tensorflow/core:test", + "//tensorflow/core/framework:tensor_testutil", + "@com_google_googletest//:gtest_main", + "@xla//xla:shape_util", + "@xla//xla/stream_executor:device_memory", + "@xla//xla/stream_executor:platform_manager", + "@xla//xla/stream_executor:stream", + "@xla//xla/stream_executor:stream_executor_h", + ], +) + +tf_cc_test( + name = "device_compilation_cluster_signature_test", + srcs = [ + "device_compilation_cluster_signature_test.cc", + ], + deps = [ + ":device_compilation_cluster_signature", + ":flags", + "//tensorflow/compiler/tf2xla:common", + "//tensorflow/core:test", + "//tensorflow/core:test_main", + "@xla//xla/client:client_library", + ], +) + +tf_cc_test( + name = "device_compilation_profiler_test", + srcs = ["device_compilation_profiler_test.cc"], + tags = [ + "nomsan", # TODO(b/284492454) + ], + deps = [ + ":device_compilation_profiler", + ":xla_activity_proto_cc", + "//tensorflow/compiler/jit/tests:device_compiler_test_helper", + "//tensorflow/core:protos_all_cc", + "@com_google_googletest//:gtest_main", + ], +) + +tf_cc_test( + name = "device_executable_persistor_test", + srcs = ["device_executable_persistor_test.cc"], + tags = ["no_cuda_on_cpu_tap"], + deps = [ + ":device_compiler_client", + ":device_executable_persistor", + ":pjrt_device_compiler_client", + ":xla_compilation_cache_proto_cc", + ":xla_cpu_device", + ":xla_cpu_jit", + ":xla_device_compiler_client", + "//tensorflow/cc:function_ops", + "//tensorflow/cc:math_ops", + "//tensorflow/cc:scope", + "//tensorflow/core:test", + "//tensorflow/core/platform:errors", + "//tensorflow/core/platform:status_matchers", + "//tensorflow/core/platform:statusor", + "//tensorflow/core/tfrt/common:create_pjrt_client_util", + "//tensorflow/core/tfrt/common:pjrt_util", + "@com_google_googletest//:gtest_main", + "@xla//xla/client:client_library", + "@xla//xla/client:executable_build_options", + "@xla//xla/client:local_client", + "@xla//xla/pjrt:pjrt_client", + "@xla//xla/pjrt/plugin/xla_cpu:cpu_client_options", + "@xla//xla/pjrt/plugin/xla_cpu:xla_cpu_pjrt_client", + ], +) + +tf_cc_test( + name = "device_compilation_cache_test", + srcs = ["device_compilation_cache_test.cc"], + deps = [ + ":device_compilation_cache", + ":xla_compile_util", + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/core:test", + "//tensorflow/core/platform:errors", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_googletest//:gtest_main", + "@xla//xla/hlo/builder:xla_computation", + "@xla//xla/tsl/protobuf:error_codes_proto_impl_cc", + ], +) + +# TODO(b/261212343): Support running this test on CPU and OSS. +tf_cuda_cc_test( + name = "device_compiler_test", + srcs = ["device_compiler_test.cc"], + tags = [ + "config-cuda-only", + "no_oss", # This test only runs with GPU. + "requires-gpu-nvidia", + "xla", + ], + deps = [ + ":device_compilation_cluster_signature", + ":device_compiler", + ":device_compiler_client", + ":tf_graph_to_hlo_compiler_proto_cc", + ":xla_compile_util", + ":xla_device_compiler_client", + ":xla_gpu_device", + ":xla_gpu_jit", + "//tensorflow/cc:function_ops", + "//tensorflow/cc:math_ops", + "//tensorflow/cc:scope", + "//tensorflow/compiler/jit/tests:device_compiler_test_helper", + "//tensorflow/compiler/tf2xla:xla_argument", + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/core:framework", + "//tensorflow/core:framework_internal", + "//tensorflow/core:test", + "//tensorflow/core/framework:fake_input", + "//tensorflow/core/kernels:ops_testutil", + "//tensorflow/core/platform:env", + "//tensorflow/core/platform:errors", + "//tensorflow/core/platform:refcount", + "//tensorflow/core/platform:status", + "//tensorflow/core/platform:status_matchers", + "//tensorflow/core/platform:statusor", + "//tensorflow/core/platform:test", + "@com_google_absl//absl/algorithm:container", + "@com_google_absl//absl/functional:any_invocable", + "@com_google_absl//absl/log:check", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/synchronization", + "@com_google_absl//absl/time", + "@com_google_googletest//:gtest_main", + "@tsl//tsl/platform:path", + "@xla//xla/client:client_library", + "@xla//xla/client:local_client", + "@xla//xla/hlo/builder:xla_computation", + "@xla//xla/stream_executor:platform_manager", + "@xla//xla/tsl/lib/strings:proto_serialization", + "@xla//xla/tsl/platform:statusor", + ], +) + +tf_cuda_cc_test( + name = "device_context_test", + srcs = ["device_context_test.cc"], + tags = tf_cuda_tests_tags() + [ + "config-cuda-only", + "no_oss", # Temporarily disable OSS. + ], + deps = [ + ":flags", + ":xla_device", + ":xla_gpu_device", + ":xla_gpu_jit", + "//tensorflow/compiler/tf2xla:xla_op_registry", + "//tensorflow/core:framework_internal", + "//tensorflow/core:test", + "//tensorflow/core/framework:tensor_testutil", + "@com_google_googletest//:gtest_main", + ], +) + +tf_cuda_cc_test( + name = "xla_compiler_options_util_test", + srcs = ["xla_compiler_options_util_test.cc"], + tags = tf_cuda_tests_tags(), + deps = [ + ":device_compiler", + ":device_executable_persistor", + ":flags", + ":pjrt_base_device", + ":pjrt_device_compiler_client", + ":test_util", + ":xla_device_compiler_client", + ":xla_device_no_jit_rewrite_registration", + ":xla_gpu_device", + ":xla_gpu_jit", + "//tensorflow/compiler/tf2xla:layout_util", + "//tensorflow/compiler/tf2xla:xla_argument", + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/compiler/tf2xla:xla_op_registry", + "//tensorflow/core:framework", + "//tensorflow/core:framework_types_hdr", + "//tensorflow/core/platform:refcount", + "//tensorflow/core/tpu:tpu_defs", + "@com_google_googletest//:gtest_main", + "@xla//xla:shape_util", + "@xla//xla:xla_data_proto_cc", + "@xla//xla/client:client_library", + "@xla//xla/client:local_client", + "@xla//xla/pjrt:pjrt_client", + "@xla//xla/stream_executor:device_memory_allocator", + "@xla//xla/stream_executor:platform", + "@xla//xla/stream_executor/host:host_platform_id", + "@xla//xla/tsl/platform:statusor", + ], +) + +tf_cuda_cc_test( + name = "xla_platform_info_test", + srcs = ["xla_platform_info_test.cc"], + tags = tf_cuda_tests_tags() + ["config-cuda-only"], + deps = [ + ":device_compilation_profiler", + ":device_compiler", + ":flags_headers", + ":test_util", + ":xla_device_no_jit_rewrite_registration", + ":xla_gpu_device", + ":xla_gpu_jit", + "//tensorflow/compiler/tf2xla:layout_util", + "//tensorflow/compiler/tf2xla:xla_op_registry", + "//tensorflow/core:framework", + "//tensorflow/core:framework_types_hdr", + "//tensorflow/core:lib_proto_parsing", + "//tensorflow/core:test", + "//tensorflow/core/platform:refcount", + "//tensorflow/core/platform:status_matchers", + "//tensorflow/core/platform:statusor", + "//tensorflow/core/protobuf:error_codes_proto_impl_cc", + "//tensorflow/core/tfrt/common:create_pjrt_client_util", + "//tensorflow/core/tfrt/common:pjrt_util", + "//tensorflow/core/tpu:tpu_defs", + "@com_google_absl//absl/log:check", + "@com_google_googletest//:gtest_main", + "@xla//xla/client:local_client", + "@xla//xla/pjrt:pjrt_client", + "@xla//xla/pjrt/plugin/xla_cpu:cpu_client_options", + "@xla//xla/pjrt/plugin/xla_cpu:xla_cpu_pjrt_client", + "@xla//xla/tsl/platform:statusor", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/build_xla_ops_pass.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/build_xla_ops_pass.cc new file mode 100644 index 0000000000000000000000000000000000000000..06ca13d313fe5cfcd5ce7ad95a5f2657fc1b161c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/build_xla_ops_pass.cc @@ -0,0 +1,624 @@ +/* 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/compiler/jit/build_xla_ops_pass.h" + +#include "absl/algorithm/container.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/str_join.h" +#include "tensorflow/cc/framework/ops.h" +#include "tensorflow/cc/framework/scope_internal.h" +#include "tensorflow/cc/ops/array_ops.h" +#include "tensorflow/cc/ops/const_op.h" +#include "tensorflow/cc/ops/control_flow_ops.h" +#include "tensorflow/cc/ops/functional_ops.h" +#include "tensorflow/cc/ops/logging_ops.h" +#include "tensorflow/compiler/jit/defs.h" +#include "tensorflow/compiler/jit/device_util.h" +#include "tensorflow/compiler/jit/encapsulate_subgraphs_pass.h" +#include "tensorflow/compiler/jit/flags.h" +#include "tensorflow/compiler/jit/xla_cluster_util.h" +#include "tensorflow/compiler/tf2xla/cc/ops/xla_jit_ops.h" +#include "tensorflow/compiler/tf2xla/xla_op_registry.h" +#include "xla/status_macros.h" +#include "tensorflow/core/common_runtime/function.h" +#include "tensorflow/core/common_runtime/graph_constructor.h" +#include "tensorflow/core/common_runtime/optimization_registry.h" +#include "tensorflow/core/framework/graph_def_util.h" +#include "tensorflow/core/framework/memory_types.h" +#include "tensorflow/core/framework/node_def_builder.h" +#include "tensorflow/core/framework/node_def_util.h" +#include "tensorflow/core/graph/algorithm.h" +#include "tensorflow/core/graph/graph.h" +#include "tensorflow/core/lib/core/status.h" +#include "tensorflow/core/lib/hash/hash.h" +#include "tensorflow/core/public/version.h" +#include "tensorflow/core/util/dump_graph.h" + +namespace tensorflow { +namespace { +struct DebuggingOpts { + // If true, insert Print nodes to print every output from an XLA cluster. + bool print_outputs; + + // If true, insert CheckNumerics nodes for every floating point typed input to + // an XLA cluster. + bool check_input_numerics; + + // If true, insert CheckNumerics nodes for every floating point typed output + // from an XLA cluster. + bool check_output_numerics; +}; + +void MoveOutgoingEdges(Graph* g, Node* old_node, Node* new_node) { + std::vector out_edges(old_node->out_edges().begin(), + old_node->out_edges().end()); + for (const Edge* edge : out_edges) { + // TODO(sanjoy): This does not update NodeDef inputs. To be able to update + // NodeDef inputs we first need to fix encapsulate_subgraphs_pass to fix up + // the NodeDef inputs to the function call nodes. + g->AddEdge(new_node, edge->src_output(), edge->dst(), edge->dst_input()); + g->RemoveEdge(edge); + } +} + +// Returns a data value that is dead iff `control` is dead. +Output ControlToData(const Scope& scope, Node* control) { + // The choice of data type here is important. + // + // We implement a "control merge", which is a control edge that is alive if + // either of two nodes (denoted as A and B below) are alive, in the following + // manner: + // + // A --ctrl--> Const0 --data--> Merge --data--> Identity + // ^ | + // | ctrl + // B --ctrl--> Const1 --data-----+ | + // v + // *** + // + // where *** denotes the merged control output. + // + // We want everything starting from Const{0/1} to Identity to either wholly + // live on the host or wholly live on device so we need to pick a data type + // that is either consistently assigned to the device (e.g. float) or + // consistently assigned to the host (e.g. int32). We should *not* pick a + // data type that partly placed on the host and partly on the device + // (e.g. bool constants are placed on the device but bool Identity is placed + // on the host). + Output data = ops::Const(scope.WithOpName("ctrl_as_data"), + Tensor(DT_INT32, TensorShape({0}))); + scope.graph()->AddControlEdge(control, data.node()); + return Output(data.node()); +} + +// Returns an operation that can be control-depended on that is dead iff `data` +// is dead. +Operation DataToControl(const Scope& scope, Output data) { + return Operation( + ops::Identity(scope.WithOpName("data_as_ctrl"), data).node()); +} + +// Replaces each outgoing edge from `old_node` with a merge node that merges in +// the corresponding output from `new_node`. +void MergeOutgoingDataEdges(const Scope& s, Node* old_node, Node* new_node, + absl::string_view cluster_name, + const DebuggingOpts& debugging_opts) { + if (!s.status().ok()) { + return; + } + + std::vector merged_outputs(old_node->num_outputs(), Output(nullptr)); + + std::vector data_edges; + absl::c_copy_if(old_node->out_edges(), std::back_inserter(data_edges), + [](const Edge* e) { return !e->IsControlEdge(); }); + + for (const Edge* e : data_edges) { + int oidx = e->src_output(); + Output merged_output = merged_outputs[oidx]; + if (merged_output.node() == nullptr) { + Output new_output(new_node, oidx); + if (debugging_opts.print_outputs) { + std::string cpu_device = "/job:localhost/replica:0/task:0/device:CPU:0"; + ops::Print print_op(s.WithOpName("print_", oidx) + .WithDevice(cpu_device) + .WithAssignedDevice(cpu_device), + new_output, {new_output}, + ops::Print::Attrs{} + .Message(absl::StrCat("output ", oidx, " from ", + old_node->name(), " is ")) + .FirstN(1000) + .Summarize(-1)); + new_output = print_op; + } + + if (debugging_opts.check_output_numerics && + DataTypeIsFloating(new_output.type())) { + ops::CheckNumerics check_numerics_op( + s.WithOpName("check_output_", oidx) + .WithDevice(new_node->requested_device()) + .WithAssignedDevice(new_node->assigned_device_name()), + new_output, + absl::StrCat("CheckNumerics failed for output ", oidx, "(", + new_output.name(), ") from cluster ", cluster_name)); + new_output = check_numerics_op; + } + + ops::_XlaMerge xla_merge_op(s.WithOpName("merge_oidx_", oidx), + Output(old_node, oidx), new_output); + merged_output = merged_outputs[oidx] = xla_merge_op.output; + } + + Node* dst = e->dst(); + int dst_idx = e->dst_input(); + + s.graph()->RemoveEdge(e); + s.graph()->AddEdge(merged_output.node(), merged_output.index(), dst, + dst_idx); + } +} + +// Replaces each control successor of `old_node` to execute whenever either +// `old_node` or `new_node` is executed. +void MergeOutgoingControlEdges(const Scope& s, Node* old_node, Node* new_node) { + if (!s.status().ok()) { + return; + } + + std::vector ctrl_edges; + absl::c_copy_if(old_node->out_edges(), std::back_inserter(ctrl_edges), + [](const Edge* e) { return e->IsControlEdge(); }); + + if (ctrl_edges.empty()) { + return; + } + + if (ctrl_edges.size() == 1 && ctrl_edges.front()->dst()->IsSink()) { + // Avoid creating a Merge node if we can just add an edge to _SINK + // instead. + s.graph()->AddControlEdge(new_node, s.graph()->sink_node()); + return; + } + + // We can't merge control edges directly so we instead first "convert" them to + // normal values that can be merged, merge the values and then "convert" the + // merged value back into control. + // + // NB! We need to copy out the outgoing control edges before constructing + // old_ctrl_as_data otherwise the control edge from old_node to the constant + // in ControlToData will be present in ctrl_edges. + + Output old_ctrl_as_data = ControlToData(s, old_node); + Output new_ctrl_as_data = ControlToData(s, new_node); + + ops::Merge ctrl_merge_as_data(s.WithOpName("ctrl_merge"), + {old_ctrl_as_data, new_ctrl_as_data}); + Operation ctrl_merge = DataToControl(s, ctrl_merge_as_data.output); + + for (const Edge* e : ctrl_edges) { + s.graph()->AddControlEdge(ctrl_merge.node(), e->dst()); + s.graph()->RemoveControlEdge(e); + } +} + +struct XlaClusterInfo { + std::vector constant_inputs; + std::vector non_constant_inputs; + std::vector resource_inputs; + NameAttrList function; +}; + +Output IncomingEdgeAsOutput(const Edge* e) { + return Output(e->src(), e->src_output()); +} + +absl::Status GetXlaClusterInfo(Node* n, XlaClusterInfo* result) { + int num_constant_inputs, num_resource_inputs; + TF_RETURN_IF_ERROR( + GetNodeAttr(n->attrs(), kXlaNumConstantArgsAttr, &num_constant_inputs)); + TF_RETURN_IF_ERROR( + GetNodeAttr(n->attrs(), kXlaNumResourceArgsAttr, &num_resource_inputs)); + + if (num_constant_inputs < 0 || num_resource_inputs < 0 || + num_constant_inputs + num_resource_inputs > n->num_inputs()) { + return absl::InvalidArgumentError( + "Invalid number of constant/resource arguments to XLA kernel."); + } + + int num_non_constant_inputs = + n->num_inputs() - num_constant_inputs - num_resource_inputs; + + std::vector input_edges_vector; + TF_RETURN_IF_ERROR(n->input_edges(&input_edges_vector)); + absl::Span input_edges(input_edges_vector); + + absl::c_transform(input_edges.subspan(0, num_constant_inputs), + std::back_inserter(result->constant_inputs), + IncomingEdgeAsOutput); + + absl::c_transform( + input_edges.subspan(num_constant_inputs, num_non_constant_inputs), + std::back_inserter(result->non_constant_inputs), IncomingEdgeAsOutput); + + absl::c_transform( + input_edges.subspan(num_constant_inputs + num_non_constant_inputs, + num_resource_inputs), + std::back_inserter(result->resource_inputs), IncomingEdgeAsOutput); + + result->function.set_name(n->type_string()); + *result->function.mutable_attr() = n->def().attr(); + return absl::OkStatus(); +} + +absl::Status CopyIncomingControlEdges(Graph* g, Node* from, Node* to) { + for (const Edge* e : from->in_edges()) { + if (e->IsControlEdge()) { + g->AddControlEdge(e->src(), to); + } + } + + return absl::OkStatus(); +} + +void RemoveAllIncomingControlEdges(Graph* g, Node* n) { + std::vector incoming_ctrl_edges; + absl::c_copy_if(n->in_edges(), std::back_inserter(incoming_ctrl_edges), + [](const Edge* e) { return e->IsControlEdge(); }); + for (const Edge* e : incoming_ctrl_edges) { + g->RemoveControlEdge(e); + } +} + +// Returns true (into `result`) if a node placed on `device` must be compiled. +absl::Status DeviceRequiresCompilation( + const jit::DeviceInfoCache& device_info_cache, jit::DeviceId device, + bool* result) { + const XlaOpRegistry::DeviceRegistration* registration = + device_info_cache.GetCompilationDevice(device); + *result = registration->autoclustering_policy == + XlaOpRegistry::AutoclusteringPolicy::kAlways; + return absl::OkStatus(); +} + +// Replaces `n` with a `PartitionedCall` op that calls the same function. +absl::StatusOr ReplaceFunctionCallWithPartitionedCall( + const GraphOptimizationPassOptions& options, + const FunctionLibraryDefinition& flib_def, Node* n, Graph* g, + const NameAttrList& func, const Scope& root) { + std::string config_string = + options.session_options->config.SerializeAsString(); + + int input_count = absl::c_count_if( + n->in_edges(), [](const Edge* e) { return !e->IsControlEdge(); }); + + std::vector args(input_count); + for (const Edge* e : n->in_edges()) { + if (!e->IsControlEdge()) { + args[e->dst_input()] = Output(e->src(), e->src_output()); + } + } + + // In theory we can use PartitionedCall if the XLA cluster does not have any + // stateful operations. However, for now we choose to be conservative since + // we don't have any evidence that choosing a stateless partitioned call helps + // for performance. + ops::StatefulPartitionedCall call( + root.WithOpName("stateful_partitioned_call"), args, n->output_types(), + func, ops::StatefulPartitionedCall::Attrs{}.ConfigProto(config_string)); + + for (const Edge* e : n->in_edges()) { + if (e->IsControlEdge()) { + g->AddControlEdge(e->src(), call.operation.node()); + } + } + + std::vector edges_to_delete; + + for (const Edge* e : n->out_edges()) { + edges_to_delete.push_back(e); + if (e->IsControlEdge()) { + g->AddControlEdge(call.operation.node(), e->dst()); + } else { + g->AddEdge(call.operation.node(), e->src_output(), e->dst(), + e->dst_input()); + } + } + + for (const Edge* e : edges_to_delete) { + g->RemoveEdge(e); + } + + g->RemoveNode(n); + return call.operation.node(); +} + +absl::StatusOr InferDeviceForCluster( + jit::DeviceInfoCache* device_info_cache, Node* n, + const std::string& function_name, + const FunctionLibraryDefinition& flib_def) { + const FunctionDef* func_def = flib_def.Find(function_name); + TF_RET_CHECK(func_def) << "Could not find " << function_name; + + jit::DeviceSet device_set; + + for (const NodeDef& ndef : func_def->node_def()) { + VLOG(3) << ndef.DebugString(); + if (!ndef.device().empty()) { + TF_ASSIGN_OR_RETURN(jit::DeviceId device_id, + device_info_cache->GetIdFor(ndef.device())); + device_set.Insert(device_id); + } + } + + if (!n->assigned_device_name().empty()) { + // TODO(sanjoy): We need this because EncapsulateSubgraphsPass drops device + // assignment when constant folding. We should fix EncapsulateSubgraphsPass + // instead. + TF_ASSIGN_OR_RETURN(jit::DeviceId device_id, + device_info_cache->GetIdFor(n->assigned_device_name())); + device_set.Insert(device_id); + } + + TF_ASSIGN_OR_RETURN(jit::DeviceId result, + PickDeviceForXla(*device_info_cache, device_set, + /*allow_mixing_unknown_and_cpu=*/true)); + VLOG(2) << "For " << function_name << " PickDeviceForXla(" + << device_info_cache->DebugString(device_set) << ") -> " + << device_info_cache->GetNameFor(result); + return result; +} + +std::vector GetXlaRunArgs(const Scope& s, + const XlaClusterInfo& cluster_info, + const DebuggingOpts& debugging_opts) { + std::vector xla_run_args; + xla_run_args.reserve(cluster_info.non_constant_inputs.size() + + cluster_info.resource_inputs.size()); + int input_idx = 0; + for (const Output& o : cluster_info.non_constant_inputs) { + if (debugging_opts.check_input_numerics && DataTypeIsFloating(o.type())) { + ops::CheckNumerics check_numerics_op( + s.WithOpName("check_input_", input_idx), o, + absl::StrCat("CheckNumerics failed for input ", input_idx, "(", + o.name(), ") into ", cluster_info.function.name())); + xla_run_args.push_back(check_numerics_op); + } else { + xla_run_args.push_back(o); + } + input_idx++; + } + absl::c_copy(cluster_info.resource_inputs, std::back_inserter(xla_run_args)); + return xla_run_args; +} + +absl::StatusOr GetOutputMemoryTypes(const Scope& root, + Node* n) { + MemoryTypeVector input_mtypes, output_mtypes; + DeviceType device_type(""); + TF_RETURN_IF_ERROR( + DeviceNameToDeviceType(n->assigned_device_name(), &device_type)); + TF_RETURN_IF_ERROR(MemoryTypesForNode(root.graph()->op_registry(), + device_type, n->def(), &input_mtypes, + &output_mtypes)); + return output_mtypes; +} + +// Predicate INT32 typed inputs to `n` on the deadness of +// `predicate_as_control`. +// +// This is a performance optimization. Since INT32 arguments to a +// PartitionedCall are placed on the host, a producer that produces them on the +// device will incur a D2H copy, even if the PartitionedCall is not executed +// (i.e. even if we choose to execute the XLA compiled computation via _XlaRun). +// To prevent this, we add control dependencies to make the int32 input edges +// into the PartitionedCall dead. With this change the D2H copy only happens if +// the PartitionedCall is actually executed. +absl::Status PredicateInt32Inputs(const Scope& root, Node* n, + Operation predicate_as_control) { + std::vector int32_inputs; + std::vector int32_inputs_input_idxs; + for (const Edge* e : n->in_edges()) { + if (e->IsControlEdge()) { + continue; + } + + if (e->src()->output_type(e->src_output()) == DT_INT32) { + TF_ASSIGN_OR_RETURN(MemoryTypeVector source_output_mem_types, + GetOutputMemoryTypes(root, e->src())); + if (source_output_mem_types[e->src_output()] == DEVICE_MEMORY) { + int32_inputs.push_back(Output(e->src(), e->src_output())); + int32_inputs_input_idxs.push_back(e->dst_input()); + } + } + } + + if (int32_inputs.empty()) { + return absl::OkStatus(); + } + + // Create a single IdentityN that is dead if and only if + // `predicate_as_control` is dead. + // + // IdentityN is also special in that, unlike `Identity`, it does not place + // int32 inputs in host memory. Placing int32 inputs in host memory would + // defeat the purpose of adding this indirection. + ops::IdentityN identity_n(root.WithOpName("int32_id_n"), int32_inputs); + root.graph()->AddControlEdge(predicate_as_control.node(), + identity_n.operation.node()); + + for (int i = 0, end = int32_inputs.size(); i < end; i++) { + TF_RETURN_IF_ERROR(root.graph()->UpdateEdge(identity_n[i].node(), i, n, + int32_inputs_input_idxs[i])); + } + + return absl::OkStatus(); +} + +absl::Status ReplaceNodeWithXlaCompileAndXlaRun( + jit::DeviceInfoCache* device_info_cache, + const GraphOptimizationPassOptions& options, + const FunctionLibraryDefinition& flib_def, bool lazy_compilation_enabled, + const DebuggingOpts& debugging_opts, Graph* g, Node* n) { + XlaClusterInfo cluster_info; + TF_RETURN_IF_ERROR(GetXlaClusterInfo(n, &cluster_info)); + + TF_ASSIGN_OR_RETURN( + jit::DeviceId device, + InferDeviceForCluster(device_info_cache, n, cluster_info.function.name(), + flib_def)); + + bool requires_compilation; + TF_RETURN_IF_ERROR(DeviceRequiresCompilation(*device_info_cache, device, + &requires_compilation)); + if (!lazy_compilation_enabled) { + requires_compilation = true; + } + + std::string device_name_str = + std::string(device_info_cache->GetNameFor(device)); + + absl::Status status; + Scope root = NewInternalScope(g, &status, /*refiner=*/nullptr) + .NewSubScope(n->name()) + .WithDevice(n->requested_device()) + .WithAssignedDevice(device_name_str); + + ops::_XlaCompile xla_compile(root.WithOpName("xla_compile"), + /*constants=*/cluster_info.constant_inputs, + /*args=*/cluster_info.non_constant_inputs, + /*resources=*/cluster_info.resource_inputs, + /*must_compile=*/requires_compilation, + cluster_info.function); + + bool has_ref_attr; + TF_RETURN_IF_ERROR( + GetNodeAttr(n->attrs(), kXlaHasReferenceVarsAttr, &has_ref_attr)); + xla_compile.operation.node()->AddAttr(kXlaHasReferenceVarsAttr, has_ref_attr); + TF_RETURN_IF_ERROR( + CopyIncomingControlEdges(g, /*from=*/n, /*to=*/xla_compile.key.node())); + + std::vector xla_run_args = + GetXlaRunArgs(root, cluster_info, debugging_opts); + + if (requires_compilation) { + // "Strict" compilation: every _XlaCompile invocation must compile the + // cluster. + ops::_XlaRun xla_run(root.WithOpName("xla_run"), xla_run_args, + xla_compile.key, n->output_types()); + + MoveOutgoingEdges(g, /*old_node=*/n, + /*new_node=*/xla_run.operation.node()); + g->RemoveNode(n); + } else { + // "Lazy" compilation: an _XlaCompile invocation may decide not to compile + // the cluster based on profitability heuristics. + + // We generate the following graph: + // + // (use_tf_call, use_xla_run) = + // Switch(pred=xla_compile.compilation_successful, + // value=xla_compile.key) + // + // tf_call_outputs = cluster_N(..., ^use_tf_call) + // xla_run_outputs = _XlaRun(..., key=use_xla_run) + // outputs = Merge(tf_call_outputs, xla_run_outputs). + ops::Switch s(root.WithOpName("predicated_compilation_key"), + xla_compile.key, xla_compile.compilation_successful); + Output predicated_compilation_key = s.output_true; + Output inverse_predicated_compilation_key = s.output_false; + + ops::_XlaRun xla_run(root.WithOpName("xla_run"), xla_run_args, + predicated_compilation_key, n->output_types()); + + MergeOutgoingControlEdges(root, /*old_node=*/n, + /*new_node=*/xla_run.operation.node()); + + MergeOutgoingDataEdges(root, /*old_node=*/n, + /*new_node=*/xla_run.operation.node(), + cluster_info.function.name(), debugging_opts); + + TF_RETURN_IF_ERROR(root.status()); + + // We already have a TensorFlow function call into the cluster -- the + // original node we set out to rewrite. We just wire in the correct control + // deps and we're done. + RemoveAllIncomingControlEdges(g, n); + Operation inverse_predicate_as_control = + DataToControl(root, inverse_predicated_compilation_key); + g->AddControlEdge(inverse_predicate_as_control.node(), n); + n->ClearAttr(kXlaCompiledKernelAttr); + + TF_ASSIGN_OR_RETURN(Node* const pco, ReplaceFunctionCallWithPartitionedCall( + options, flib_def, n, g, + cluster_info.function, root)); + + TF_RETURN_IF_ERROR( + PredicateInt32Inputs(root, pco, inverse_predicate_as_control)); + } + + return absl::OkStatus(); +} +} // namespace + +absl::Status BuildXlaOpsPass::Run(const GraphOptimizationPassOptions& options) { + Graph* graph = options.graph->get(); + + // Copy out the nodes we want to rewrite to avoid modifying the graph while we + // iterate on graph->op_nodes(). + std::vector xla_compiled_kernels; + absl::c_copy_if(graph->op_nodes(), std::back_inserter(xla_compiled_kernels), + [](const Node* n) { + if (n->IsSend() || n->IsRecv() || n->IsControlFlow()) { + return false; + } + + // Only compile nodes that are marked for compilation by the + // compilation-marking pass (via 'attr_name'). + return IsXlaCompiledKernel(*n); + }); + + bool lazy_compilation_enabled = + enable_lazy_compilation_ + ? *enable_lazy_compilation_ + : GetBuildXlaOpsPassFlags()->tf_xla_enable_lazy_compilation; + + jit::DeviceInfoCache device_info_cache; + const BuildXlaOpsPassFlags& flags = *GetBuildXlaOpsPassFlags(); + + DebuggingOpts debugging_opts; + debugging_opts.print_outputs = flags.tf_xla_print_cluster_outputs; + debugging_opts.check_input_numerics = + flags.tf_xla_check_cluster_input_numerics; + debugging_opts.check_output_numerics = + flags.tf_xla_check_cluster_output_numerics; + + VLOG(1) << "print_outputs = " << debugging_opts.print_outputs; + VLOG(1) << "check_input_numerics = " << debugging_opts.check_input_numerics; + VLOG(1) << "check_output_numerics = " << debugging_opts.check_output_numerics; + + for (Node* n : xla_compiled_kernels) { + TF_RETURN_IF_ERROR(ReplaceNodeWithXlaCompileAndXlaRun( + &device_info_cache, options, *options.flib_def, + lazy_compilation_enabled, debugging_opts, graph, n)); + } + + if (VLOG_IS_ON(1)) { + DumpGraphToFile("build_xla_ops", *graph, options.flib_def); + } + + return absl::OkStatus(); +} +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/build_xla_ops_pass.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/build_xla_ops_pass.h new file mode 100644 index 0000000000000000000000000000000000000000..e8b3b56eb9a9af0714c5e7bb3876452a0f0481f7 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/build_xla_ops_pass.h @@ -0,0 +1,45 @@ +/* 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_BUILD_XLA_OPS_PASS_H_ +#define TENSORFLOW_COMPILER_JIT_BUILD_XLA_OPS_PASS_H_ + +#include "absl/types/optional.h" +#include "tensorflow/core/common_runtime/optimization_registry.h" +#include "tensorflow/core/lib/core/status.h" + +namespace tensorflow { + +// Replaces TF function calls marked with `_XlaCompiledKernel` with _XlaCompile +// and _XlaRun nodes (which compile and launch, respectively, the corresponding +// HLO module). +class BuildXlaOpsPass : public GraphOptimizationPass { + public: + // If enable_lazy_compilation is not nullopt then *enable_lazy_compilation + // overrides --tf_xla_enable_lazy_compilation flag in deciding whether lazy + // compilation is enabled. + explicit BuildXlaOpsPass( + std::optional enable_lazy_compilation = std::nullopt) + : enable_lazy_compilation_(enable_lazy_compilation) {} + + absl::Status Run(const GraphOptimizationPassOptions& options) override; + + private: + std::optional enable_lazy_compilation_; +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_BUILD_XLA_OPS_PASS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/build_xla_ops_pass_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/build_xla_ops_pass_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..0a59a2b7effa82fe2cb590d17cd12099515c37fb --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/build_xla_ops_pass_test.cc @@ -0,0 +1,328 @@ +/* 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 "tensorflow/compiler/jit/build_xla_ops_pass.h" + +#include "tensorflow/cc/framework/ops.h" +#include "tensorflow/cc/ops/array_ops.h" +#include "tensorflow/cc/ops/resource_variable_ops.h" +#include "tensorflow/cc/ops/standard_ops.h" +#include "tensorflow/compiler/jit/defs.h" +#include "tensorflow/compiler/jit/encapsulate_subgraphs_pass.h" +#include "tensorflow/compiler/jit/node_matchers.h" +#include "tensorflow/compiler/jit/test_util.h" +#include "tensorflow/core/common_runtime/device_factory.h" +#include "tensorflow/core/graph/algorithm.h" +#include "tensorflow/core/lib/core/status_test_util.h" +#include "tensorflow/core/platform/logging.h" +#include "tensorflow/core/platform/test.h" +#include "tensorflow/core/public/session_options.h" + +namespace tensorflow { +namespace { + +class BuildXlaOpsTest : public ::testing::Test { + protected: + void SetUp() override { + // This is needed to register the XLA_* devices. + CHECK(DeviceFactory::AddDevices( + SessionOptions(), "/job:localhost/replica:0/task:0", &devices_) + .ok()); + } + + private: + std::vector> devices_; +}; + +using ::tensorflow::testing::FindNodeByName; +using ::tensorflow::testing::matchers::Attr; +using ::tensorflow::testing::matchers::CtrlDeps; +using ::tensorflow::testing::matchers::Inputs; +using ::tensorflow::testing::matchers::NodeWith; +using ::tensorflow::testing::matchers::Op; +using ::tensorflow::testing::matchers::Out; +using ::testing::_; + +absl::Status BuildXlaOps(const Scope& s, const FunctionDefLibrary& fdef_lib, + std::unique_ptr* result) { + auto graph = std::make_unique(OpRegistry::Global()); + TF_RETURN_IF_ERROR(s.ToGraph(graph.get())); + FunctionLibraryDefinition flib_def(graph->op_registry(), fdef_lib); + + // Assign all nodes to the CPU device. + static const char* kCpuDevice = "/job:localhost/replica:0/task:0/cpu:0"; + for (Node* n : graph->nodes()) { + if (n->requested_device().empty()) { + n->set_assigned_device_name(kCpuDevice); + } else { + n->set_assigned_device_name(n->requested_device()); + } + } + + FixupSourceAndSinkEdges(graph.get()); + + GraphOptimizationPassWrapper wrapper; + GraphOptimizationPassOptions opt_options = + wrapper.CreateGraphOptimizationPassOptions(&graph); + opt_options.flib_def = &flib_def; + + BuildXlaOpsPass pass(/*enable_lazy_compilation=*/true); + TF_RETURN_IF_ERROR(pass.Run(opt_options)); + VLOG(3) << graph->ToGraphDefDebug().DebugString(); + *result = std::move(graph); + return absl::OkStatus(); +} + +absl::Status MakeXlaCompiledKernel(Graph* graph, const std::string& callee_name, + const std::string& node_name, + int num_constant_args, int num_resource_args, + Node** result) { + NodeDef call_node; + call_node.set_name(node_name); + call_node.set_op(callee_name); + AddNodeAttr(kXlaCompiledKernelAttr, true, &call_node); + AddNodeAttr(kXlaNumConstantArgsAttr, num_constant_args, &call_node); + AddNodeAttr(kXlaNumResourceArgsAttr, num_resource_args, &call_node); + TF_ASSIGN_OR_RETURN(*result, graph->AddNode(call_node)); + return absl::OkStatus(); +} + +absl::Status MakeXlaCompiledKernel(Graph* graph, const std::string& callee_name, + const std::string& node_name, + Node** result) { + return MakeXlaCompiledKernel(graph, callee_name, node_name, + /*num_constant_args=*/0, /*num_resource_args=*/0, + result); +} + +Node* MakeWrite(const Scope& scope, Output value_to_write, + const std::string& id) { + Output var_handle = ops::VarHandleOp(scope.WithOpName("Var_" + id), DT_FLOAT, + TensorShape({})); + ops::AssignVariableOp assign_op(scope.WithOpName("Assignee_" + id), + var_handle, value_to_write); + return assign_op.operation.node(); +} + +Node* MakeWrite(const Scope& scope, const std::string& id) { + return MakeWrite( + scope, ops::Const(scope.WithOpName("ValueToAssign" + id), 1.0f), id); +} + +FunctionDefLibrary CreateFunctionDefLibWithConstFunction( + const std::string& name) { + FunctionDefLibrary fdef_lib; + FunctionDef func = FunctionDefHelper::Create( + /*function_name=*/name, /*in_def=*/{}, /*out_def=*/{"out: float"}, + /*attr_def*/ + {}, /*node_def=*/{FunctionDefHelper::Const("one", 1.0f)}, + /*ret_def=*/{{"out", "out:output:0"}}); + *fdef_lib.add_function() = std::move(func); + return fdef_lib; +} + +TEST_F(BuildXlaOpsTest, ControlDepsPreserved) { + const char* kXlaDeviceName = "/job:worker/replica:0/task:0/device:XLA_CPU:0"; + Scope root = Scope::NewRootScope().WithDevice(kXlaDeviceName).ExitOnError(); + + FunctionDefLibrary fdef_lib = + CreateFunctionDefLibWithConstFunction("cluster_0"); + TF_ASSERT_OK(root.graph()->AddFunctionLibrary(fdef_lib)); + Node* call; + TF_ASSERT_OK(MakeXlaCompiledKernel(root.graph(), "cluster_0", "C", &call)); + call->AddAttr(kXlaHasReferenceVarsAttr, false); + call->set_requested_device(kXlaDeviceName); + Node* write_op = MakeWrite(root, "write"); + write_op->AddAttr(kXlaHasReferenceVarsAttr, false); + root.graph()->AddControlEdge(call, write_op); + + std::unique_ptr graph; + TF_ASSERT_OK(BuildXlaOps(root, fdef_lib, &graph)); + + Node* write_op_new = FindNodeByName(graph.get(), write_op->name()); + ASSERT_NE(write_op_new, nullptr); + EXPECT_THAT(write_op_new, NodeWith(CtrlDeps(NodeWith(Op("_XlaRun"))))); +} + +TEST_F(BuildXlaOpsTest, CleanFailureOnBogusAttr) { + Scope root = Scope::NewRootScope().ExitOnError(); + + FunctionDefLibrary fdef_lib = + CreateFunctionDefLibWithConstFunction("cluster_0"); + TF_ASSERT_OK(root.graph()->AddFunctionLibrary(fdef_lib)); + + Node* call; + TF_ASSERT_OK( + MakeXlaCompiledKernel(root.graph(), "cluster_0", "C", 100, 100, &call)); + + Node* write_op = MakeWrite(root, "write"); + root.graph()->AddControlEdge(call, write_op); + + std::unique_ptr graph; + absl::Status failure_status = BuildXlaOps(root, fdef_lib, &graph); + ASSERT_FALSE(failure_status.ok()); + EXPECT_EQ(failure_status.code(), error::INVALID_ARGUMENT); +} + +TEST_F(BuildXlaOpsTest, OnNonXlaDevice) { + Scope root = Scope::NewRootScope().ExitOnError(); + + FunctionDefLibrary fdef_lib = + CreateFunctionDefLibWithConstFunction("cluster_0"); + TF_ASSERT_OK(root.graph()->AddFunctionLibrary(fdef_lib)); + + Node* call; + TF_ASSERT_OK(MakeXlaCompiledKernel(root.graph(), "cluster_0", "C", &call)); + TF_ASSERT_OK(root.DoShapeInference(call)); + call->AddAttr(kXlaHasReferenceVarsAttr, false); + + Node* write_op = MakeWrite(root, Output(call), "write_result"); + write_op->AddAttr(kXlaHasReferenceVarsAttr, false); + + auto xla_compile = NodeWith(Op("_XlaCompile"), Attr("must_compile", false)); + auto predicated_compilation_key = + NodeWith(Op("Switch"), Inputs(Out(0, xla_compile), Out(1, xla_compile))); + auto xla_run = + NodeWith(Op("_XlaRun"), Inputs(Out(1, predicated_compilation_key))); + auto tf_call = + NodeWith(Op("StatefulPartitionedCall"), + CtrlDeps(NodeWith(Op("Identity"), + Inputs(Out(0, predicated_compilation_key))))); + auto merge = NodeWith(Op("_XlaMerge"), Inputs(Out(tf_call), Out(xla_run))); + auto assign_var = NodeWith(Op("AssignVariableOp"), Inputs(_, Out(merge))); + + std::unique_ptr graph; + TF_ASSERT_OK(BuildXlaOps(root, fdef_lib, &graph)); + + Node* write_op_new = FindNodeByName(graph.get(), write_op->name()); + ASSERT_NE(write_op_new, nullptr); + EXPECT_THAT(write_op_new, assign_var); +} + +TEST_F(BuildXlaOpsTest, OnXlaDevice) { + const char* kXlaDeviceName = "/job:worker/replica:0/task:0/device:XLA_CPU:0"; + Scope root = Scope::NewRootScope().WithDevice(kXlaDeviceName).ExitOnError(); + + FunctionDefLibrary fdef_lib = + CreateFunctionDefLibWithConstFunction("cluster_0"); + TF_ASSERT_OK(root.graph()->AddFunctionLibrary(fdef_lib)); + + Node* call; + TF_ASSERT_OK(MakeXlaCompiledKernel(root.graph(), "cluster_0", "C", &call)); + call->set_requested_device(kXlaDeviceName); + TF_ASSERT_OK(root.DoShapeInference(call)); + call->AddAttr(kXlaHasReferenceVarsAttr, false); + + Node* write_op = MakeWrite(root, Output(call), "write_result"); + write_op->AddAttr(kXlaHasReferenceVarsAttr, false); + + std::unique_ptr graph; + TF_ASSERT_OK(BuildXlaOps(root, fdef_lib, &graph)); + + auto xla_op = + NodeWith(Op("_XlaRun"), Inputs(Out(NodeWith(Op("_XlaCompile"))))); + auto assign_var = + NodeWith(Op("AssignVariableOp"), Inputs(Out(NodeWith()), Out(xla_op))); + + Node* write_op_new = FindNodeByName(graph.get(), write_op->name()); + ASSERT_NE(write_op_new, nullptr); + EXPECT_THAT(write_op_new, assign_var); +} + +TEST_F(BuildXlaOpsTest, NoExtraMergeForEdgeToSink) { + Scope root = Scope::NewRootScope().ExitOnError(); + + FunctionDefLibrary fdef_lib = + CreateFunctionDefLibWithConstFunction("cluster_0"); + TF_ASSERT_OK(root.graph()->AddFunctionLibrary(fdef_lib)); + Node* call; + TF_ASSERT_OK(MakeXlaCompiledKernel(root.graph(), "cluster_0", "C", &call)); + call->AddAttr(kXlaHasReferenceVarsAttr, false); + + std::unique_ptr graph; + TF_ASSERT_OK(BuildXlaOps(root, fdef_lib, &graph)); + + Node* sink_node = graph->sink_node(); + EXPECT_THAT(sink_node, + NodeWith(CtrlDeps(NodeWith(Op("_XlaRun")), + NodeWith(Op("StatefulPartitionedCall")), + NodeWith(Op("NoOp"))))); +} + +#ifdef GOOGLE_CUDA +FunctionDefLibrary CreateFunctionDefLibWithInt32Input(const std::string& name) { + FunctionDefLibrary fdef_lib; + FunctionDef func = FunctionDefHelper::Create( + /*function_name=*/name, /*in_def=*/{"in: int32"}, + /*out_def=*/{"out: int32"}, + /*attr_def=*/{}, /*node_def=*/{{{"out"}, "Identity", {"in"}}}, + /*ret_def=*/{{"out", "out:output:0"}}); + *fdef_lib.add_function() = std::move(func); + return fdef_lib; +} + +// This tests a rewrite that only makes sense and is active in a CUDA-enabled +// build. Specifically we check that we insert an IdentityN op to avoid extra +// device-to-host copies. +TEST_F(BuildXlaOpsTest, NoDeviceToHostCopiesForClustersWithInt32Inputs) { + const char* kXlaDeviceName = "/job:worker/replica:0/task:0/device:GPU:0"; + Scope root = Scope::NewRootScope() + .WithDevice(kXlaDeviceName) + .WithAssignedDevice(kXlaDeviceName) + .ExitOnError(); + + FunctionDefLibrary fdef_lib = + CreateFunctionDefLibWithInt32Input("cluster_int32"); + TF_ASSERT_OK(root.graph()->AddFunctionLibrary(fdef_lib)); + Node* call; + TF_ASSERT_OK( + MakeXlaCompiledKernel(root.graph(), "cluster_int32", "C", &call)); + call->set_requested_device(kXlaDeviceName); + call->AddAttr(kXlaHasReferenceVarsAttr, false); + + auto var = + ops::VarHandleOp(root.WithOpName("var"), DT_INT32, TensorShape({})); + auto int32_on_device = + ops::ReadVariableOp(root.WithOpName("int32_on_device"), var, DT_INT32); + + root.graph()->AddEdge(int32_on_device.node(), 0, call, 0); + + std::unique_ptr graph; + TF_ASSERT_OK(BuildXlaOps(root, fdef_lib, &graph)); + + Node* stateful_partitioned_call_op = nullptr; + for (Node* n : graph->op_nodes()) { + if (n->type_string() == "StatefulPartitionedCall") { + ASSERT_EQ(stateful_partitioned_call_op, nullptr); + stateful_partitioned_call_op = n; + } + } + + ASSERT_NE(stateful_partitioned_call_op, nullptr); + auto xla_compile = NodeWith(Op("_XlaCompile")); + auto switch_on_compilation_pred = + NodeWith(Op("Switch"), Inputs(Out(0, xla_compile), Out(1, xla_compile))); + auto ctrl_dep = + NodeWith(Op("Identity"), Inputs(Out(0, switch_on_compilation_pred))); + // Check that we pipe int32 inputs through an IdentityN to avoid extra D2H + // copies. + EXPECT_THAT( + stateful_partitioned_call_op, + NodeWith(Inputs(Out(NodeWith(Op("IdentityN"), CtrlDeps(ctrl_dep)))))); +} +#endif + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/clone_constants_for_better_clustering.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/clone_constants_for_better_clustering.cc new file mode 100644 index 0000000000000000000000000000000000000000..27054f047a1df69d5469572c948875d08a06139e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/clone_constants_for_better_clustering.cc @@ -0,0 +1,225 @@ +/* Copyright 2019 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/compiler/jit/clone_constants_for_better_clustering.h" + +#include + +#include "absl/algorithm/container.h" +#include "tensorflow/compiler/jit/xla_cluster_util.h" +#include "xla/status_macros.h" +#include "tensorflow/core/framework/node_def.pb.h" +#include "tensorflow/core/framework/tensor.pb.h" +#include "tensorflow/core/util/dump_graph.h" + +namespace tensorflow { + +using tsl::StatusOr; + +class CloneConstantsForBetterClusteringPassImpl { + public: + explicit CloneConstantsForBetterClusteringPassImpl(Graph* graph) + : graph_(graph), unique_name_counter_(0) {} + absl::Status Run(); + + private: + absl::Status CloneSmallConstantInputs( + const absl::flat_hash_set& name_set, Node* n); + std::string GenerateUniqueName( + const absl::flat_hash_set& name_set, + absl::string_view prefix); + absl::StatusOr CloneNode( + const absl::flat_hash_set& name_set, Node* n); + + Graph* graph_; + int unique_name_counter_; +}; + +std::string CloneConstantsForBetterClusteringPassImpl::GenerateUniqueName( + const absl::flat_hash_set& name_set, + absl::string_view prefix) { + std::string candidate; + do { + candidate = absl::StrCat(prefix, "/clone_", unique_name_counter_++); + } while (name_set.contains(candidate)); + return candidate; +} + +absl::StatusOr CloneConstantsForBetterClusteringPassImpl::CloneNode( + const absl::flat_hash_set& name_set, Node* n) { + NodeDef new_in_def = n->def(); + new_in_def.clear_input(); + new_in_def.set_name(GenerateUniqueName(name_set, new_in_def.name())); + TF_ASSIGN_OR_RETURN(Node * new_in, graph_->AddNode(new_in_def)); + + for (const Edge* e : n->in_edges()) { + if (e->IsControlEdge()) { + graph_->AddControlEdge(e->src(), new_in); + } else { + graph_->AddEdge(e->src(), e->src_output(), new_in, e->dst_input()); + } + } + + new_in->set_assigned_device_name(n->assigned_device_name()); + return new_in; +} + +namespace { +absl::StatusOr IsConstantSmall(Node* n) { + const TensorProto* proto = nullptr; + TF_RETURN_IF_ERROR(GetNodeAttr(n->def(), "value", &proto)); + + int64_t total_elements = 1; + for (const auto& dim : proto->tensor_shape().dim()) { + if (dim.size() < 0) { + return absl::InternalError(absl::StrCat( + "Unknown dimension size in constant tensor ", n->name())); + } + total_elements *= dim.size(); + } + + // TODO(sanjoy): It may make sense to combine this threshold with XLA's "large + // constant" threshold, if there is one. + const int kSmallTensorThreshold = 16; + return total_elements < kSmallTensorThreshold; +} + +// We only clone small constants since we want to avoid increasing memory +// pressure on GPUs. +absl::StatusOr IsSmallConstant(Node* n) { + if (!n->IsConstant()) { + return false; + } + + return IsConstantSmall(n); +} + +bool IsInPlaceOp(absl::string_view op_name) { + return op_name == "InplaceUpdate" || op_name == "InplaceAdd" || + op_name == "InplaceSub"; +} +} // namespace + +absl::Status +CloneConstantsForBetterClusteringPassImpl::CloneSmallConstantInputs( + const absl::flat_hash_set& name_set, Node* n) { + std::vector in_edges; + // Get the edges and sort them so we clone in a deterministic order. + absl::c_copy(n->in_edges(), std::back_inserter(in_edges)); + absl::c_stable_sort(in_edges, [](const Edge* e1, const Edge* e2) { + return e1->id() < e2->id(); + }); + for (const Edge* e : in_edges) { + Node* input = e->src(); + TF_ASSIGN_OR_RETURN(bool is_small_constant, IsSmallConstant(input)); + if (is_small_constant && input->out_edges().size() != 1) { + VLOG(2) << "Cloning small constant " << input->name(); + TF_ASSIGN_OR_RETURN(Node* const input_cloned, CloneNode(name_set, input)); + if (e->IsControlEdge()) { + graph_->AddControlEdge(input_cloned, e->dst()); + } else { + int dst_input = e->dst_input(); + TF_RET_CHECK(e->src_output() == 0) + << "expected constant to have exactly one non-control output, but " + "found output index = " + << e->src_output(); + graph_->RemoveEdge(e); + graph_->AddEdge(input_cloned, 0, n, dst_input); + } + } + } + return absl::OkStatus(); +} + +absl::Status CloneConstantsForBetterClusteringPassImpl::Run() { + absl::flat_hash_set name_set; + absl::c_transform(graph_->nodes(), std::inserter(name_set, name_set.begin()), + [](Node* n) { return n->name(); }); + std::vector nodes; + for (Node* n : graph_->nodes()) { + // We rely on the immutability of Tensors to safely clone Const operations. + // However, "in place" ops do not respect the immutability of Tensors so we + // avoid this transformation when such ops are present in the graph. + // + // In-place operations are problematic because they break the semantic + // illusion that tensorflow::Tensor instances are immutable. For instance + // if we have the following graph: + // + // digraph { + // SRC -> Const + // SRC -> I + // SRC -> V + // Const -> Identity + // Const -> InplaceAdd [label="x"] + // I -> InplaceAdd [label="i"] + // V -> InplaceAdd [label="v"] + // InplaceAdd -> Identity [style=dotted] + // } + // + // then the value produced by `Identity` is Const+I*V since InplaceAdd + // modifies the tensor in place. However, if we clone `Const` and turn the + // graph into: + // + // digraph { + // SRC -> "Const/clone_1" + // SRC -> "Const/clone_2" + // SRC -> I + // SRC -> V + // "Const/clone_1" -> Identity + // "Const/clone_2" -> InplaceAdd [label="x"] + // I -> InplaceAdd [label="i"] + // V -> InplaceAdd [label="v"] + // InplaceAdd -> Identity [style=dotted] + // } + // + // then `Identity` no longer produces Const+I*V because the InplaceAdd + // operation only modifies Const/clone_2 in place. + + if (IsInPlaceOp(n->type_string())) { + return absl::OkStatus(); + } + nodes.push_back(n); + } + + // Iterate over a copy of the nodes to avoid iterating over g->nodes() while + // creating more nodes. + for (Node* n : nodes) { + TF_RETURN_IF_ERROR(CloneSmallConstantInputs(name_set, n)); + } + return absl::OkStatus(); +} + +absl::Status CloneConstantsForBetterClusteringPass::Run( + const GraphOptimizationPassOptions& options) { + if (GetGlobalJitLevelForGraph(options) == OptimizerOptions::OFF) { + return absl::OkStatus(); + } + + Graph* g = options.graph->get(); + + if (VLOG_IS_ON(1)) { + DumpGraphToFile("before_clone_constants_for_better_clustering", *g); + } + + TF_RETURN_IF_ERROR(CloneConstantsForBetterClusteringPassImpl{g}.Run()); + + if (VLOG_IS_ON(1)) { + DumpGraphToFile("after_clone_constants_for_better_clustering", *g); + } + + return absl::OkStatus(); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/clone_constants_for_better_clustering.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/clone_constants_for_better_clustering.h new file mode 100644 index 0000000000000000000000000000000000000000..af6e708b9bf7d0e2f56379e52ee44c397a857534 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/clone_constants_for_better_clustering.h @@ -0,0 +1,62 @@ +/* Copyright 2019 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_CLONE_CONSTANTS_FOR_BETTER_CLUSTERING_H_ +#define TENSORFLOW_COMPILER_JIT_CLONE_CONSTANTS_FOR_BETTER_CLUSTERING_H_ + +#include "absl/container/flat_hash_set.h" +#include "tensorflow/core/common_runtime/optimization_registry.h" + +namespace tensorflow { +// Clones small host constants in the graph to make it easier to form larger +// clusters. +// +// This helps us in two ways: +// +// - It reduces dependencies between clusters. Let's say a constant C is used +// by nodes X and Y. If X and Y are put in different clusters (for whatever +// reason) Y's cluster now has to wait for all the operations in X's cluster +// to finish before it starts running. +// +// - It lets us create bigger clusters in multi-GPU benchmarks. Consider the +// following graph: +// +// digraph { +// Const -> GPU_1 +// Const -> GPU_0_Y +// GPU_0_X -> GPU_0_Y +// } +// +// We'd cluster Const and GPU_1 together (and place it on GPU_1), and this +// will block us from clustering GPU_0_X and GPU_0_Y together since that +// would increase the amount of work on GPU 0 waiting on work on GPU 1. +// However, cloning Const into two copies, one for GPU_0_Y and one for GPU_1 +// will let us create one cluster containing {Const/copy_0, GPU_1} and +// another containing {Const/copy_1, GPU_0_X, GPU_0_Y}. +// +// We only clone small host constants now to avoid increasing memory consumption +// too much. Moreover, in practice the constants we have to duplicate are +// things like the `perm` input to `Transpose` and the `size` input to `Slice` +// which tend to be small anyway. + +class CloneConstantsForBetterClusteringPass : public GraphOptimizationPass { + public: + CloneConstantsForBetterClusteringPass() = default; + + absl::Status Run(const GraphOptimizationPassOptions& options) override; +}; +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_CLONE_CONSTANTS_FOR_BETTER_CLUSTERING_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/clone_constants_for_better_clustering_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/clone_constants_for_better_clustering_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..1cb6eff226ba0974a4760b0b6a0bf3bf7b46a8c1 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/clone_constants_for_better_clustering_test.cc @@ -0,0 +1,232 @@ +/* Copyright 2019 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/compiler/jit/clone_constants_for_better_clustering.h" + +#include "tensorflow/cc/framework/ops.h" +#include "tensorflow/cc/ops/array_ops.h" +#include "tensorflow/cc/ops/const_op.h" +#include "tensorflow/cc/ops/math_ops.h" +#include "tensorflow/compiler/jit/node_matchers.h" +#include "tensorflow/core/lib/core/status_test_util.h" +#include "tensorflow/core/platform/test.h" +#include "tensorflow/core/public/session_options.h" + +namespace tensorflow { +namespace { +using ::tensorflow::testing::FindNodeByName; + +absl::Status CloneConstantsForBetterClustering(const Scope& s, + std::unique_ptr* result) { + auto graph = std::make_unique(OpRegistry::Global()); + SessionOptions session_options; + session_options.config.mutable_graph_options() + ->mutable_optimizer_options() + ->set_global_jit_level(OptimizerOptions::ON_2); + GraphOptimizationPassOptions options; + options.graph = &graph; + options.session_options = &session_options; + + // Scope::ToGraph seems to drop assigned devices, probably because it goes + // through a GraphDef. So explicitly maintain the device assignment. + // std::unordered_map assigned_device_names; + // for (Node* n : s.graph()->nodes()) { + // assigned_device_names[n->name()] = n->assigned_device_name(); + // } + GraphConstructorOptions opts; + opts.expect_device_spec = true; + TF_RETURN_IF_ERROR(s.ToGraph(graph.get(), opts)); + // for (Node* n : graph->nodes()) { + // n->set_assigned_device_name(assigned_device_names[n->name()]); + // } + + CloneConstantsForBetterClusteringPass rewriter; + TF_RETURN_IF_ERROR(rewriter.Run(options)); + *result = std::move(graph); + return absl::OkStatus(); +} + +const char* kCPU = "/job:localhost/replica:0/task:0/device:CPU:0"; +const char* kGPU = "/job:localhost/replica:0/task:0/device:GPU:0"; + +TEST(CloneConstantsForBetterClusteringTest, ScalarConstantPlacedOnGpu) { + Scope root = Scope::NewRootScope().ExitOnError(); + Scope on_gpu = root.WithAssignedDevice(kGPU).WithDevice(kGPU); + + Output in = ops::Placeholder(on_gpu.WithOpName("in"), DT_FLOAT); + Output c = ops::Const(on_gpu.WithOpName("const"), 1.0f, {}); + Output add1 = ops::AddV2(on_gpu.WithOpName("add1"), in, c); + Output add2 = ops::AddV2(on_gpu.WithOpName("add2"), add1, c); + + std::unique_ptr result; + TF_ASSERT_OK(CloneConstantsForBetterClustering(root, &result)); + + OutputTensor add1_operand; + TF_ASSERT_OK( + FindNodeByName(result.get(), "add1")->input_tensor(1, &add1_operand)); + + OutputTensor add2_operand; + TF_ASSERT_OK( + FindNodeByName(result.get(), "add2")->input_tensor(1, &add2_operand)); + + EXPECT_NE(add1_operand.node, add2_operand.node); +} + +TEST(CloneConstantsForBetterClusteringTest, HostConstantPlacedOnCpu) { + Scope root = Scope::NewRootScope().ExitOnError(); + Scope on_gpu = root.WithAssignedDevice(kGPU).WithDevice(kGPU); + Scope on_cpu = root.WithAssignedDevice(kCPU).WithDevice(kCPU); + + Output in0 = ops::Placeholder(on_gpu.WithOpName("in0"), DT_FLOAT); + Output in1 = ops::Placeholder(on_gpu.WithOpName("in1"), DT_FLOAT); + + Output perm = ops::Const(on_cpu.WithOpName("perm"), {3, 1, 2, 0}); + + { + Output tr0 = ops::Transpose(on_gpu.WithOpName("tr0"), in0, perm); + Output tr1 = ops::Transpose(on_gpu.WithOpName("tr1"), in1, perm); + } + + std::unique_ptr result; + TF_ASSERT_OK(CloneConstantsForBetterClustering(root, &result)); + + OutputTensor tr0_perm; + TF_ASSERT_OK(FindNodeByName(result.get(), "tr0")->input_tensor(1, &tr0_perm)); + + OutputTensor tr1_perm; + TF_ASSERT_OK(FindNodeByName(result.get(), "tr1")->input_tensor(1, &tr1_perm)); + + EXPECT_NE(tr0_perm.node, tr1_perm.node); +} + +TEST(CloneConstantsForBetterClusteringTest, HostConstantPlacedOnGpu) { + Scope root = Scope::NewRootScope().ExitOnError(); + Scope on_gpu = root.WithAssignedDevice(kGPU).WithDevice(kGPU); + + Output in0 = ops::Placeholder(on_gpu.WithOpName("in0"), DT_FLOAT); + Output in1 = ops::Placeholder(on_gpu.WithOpName("in1"), DT_FLOAT); + + Output perm = ops::Const(on_gpu.WithOpName("perm"), {3, 1, 2, 0}); + + { + Output tr0 = ops::Transpose(on_gpu.WithOpName("tr0"), in0, perm); + Output tr1 = ops::Transpose(on_gpu.WithOpName("tr1"), in1, perm); + } + + std::unique_ptr result; + TF_ASSERT_OK(CloneConstantsForBetterClustering(root, &result)); + + OutputTensor tr0_perm; + TF_ASSERT_OK(FindNodeByName(result.get(), "tr0")->input_tensor(1, &tr0_perm)); + + OutputTensor tr1_perm; + TF_ASSERT_OK(FindNodeByName(result.get(), "tr1")->input_tensor(1, &tr1_perm)); + + EXPECT_NE(tr0_perm.node, tr1_perm.node); +} + +TEST(CloneConstantsForBetterClusteringTest, CloneSmallDeviceConstants) { + Scope root = Scope::NewRootScope().ExitOnError(); + Scope on_gpu = root.WithAssignedDevice(kGPU).WithDevice(kGPU); + + Output in0 = ops::Placeholder(on_gpu.WithOpName("in0"), DT_FLOAT); + Output in1 = ops::Placeholder(on_gpu.WithOpName("in1"), DT_FLOAT); + + Output perm_f32 = ops::Const(on_gpu.WithOpName("perm"), {3.0, 1.0, 2.0, 0.0}); + Output perm_int0 = + ops::Cast(on_gpu.WithOpName("perm_cast_0"), perm_f32, DT_INT32); + Output perm_int1 = + ops::Cast(on_gpu.WithOpName("perm_cast_1"), perm_f32, DT_INT32); + + { + Output tr0 = ops::Transpose(on_gpu.WithOpName("tr0"), in0, perm_int0); + Output tr1 = ops::Transpose(on_gpu.WithOpName("tr1"), in1, perm_int1); + } + + std::unique_ptr result; + TF_ASSERT_OK(CloneConstantsForBetterClustering(root, &result)); + + OutputTensor tr0_perm; + TF_ASSERT_OK( + FindNodeByName(result.get(), "perm_cast_0")->input_tensor(0, &tr0_perm)); + + OutputTensor tr1_perm; + TF_ASSERT_OK( + FindNodeByName(result.get(), "perm_cast_1")->input_tensor(0, &tr1_perm)); + + EXPECT_NE(tr0_perm.node, tr1_perm.node); +} + +TEST(CloneConstantsForBetterClusteringTest, DontCloneLargeConstants) { + Scope root = Scope::NewRootScope().ExitOnError(); + Scope on_gpu = root.WithAssignedDevice(kGPU).WithDevice(kGPU); + Scope on_cpu = root.WithAssignedDevice(kCPU).WithDevice(kCPU); + + Output in0 = ops::Placeholder(on_gpu.WithOpName("in0"), DT_FLOAT); + Output in1 = ops::Placeholder(on_gpu.WithOpName("in1"), DT_FLOAT); + + Output perm = ops::Const( + on_cpu.WithOpName("perm"), + {17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}); + + { + Output tr0 = ops::Transpose(on_gpu.WithOpName("tr0"), in0, perm); + Output tr1 = ops::Transpose(on_gpu.WithOpName("tr1"), in1, perm); + } + + std::unique_ptr result; + TF_ASSERT_OK(CloneConstantsForBetterClustering(root, &result)); + + OutputTensor tr0_perm; + TF_ASSERT_OK(FindNodeByName(result.get(), "tr0")->input_tensor(1, &tr0_perm)); + + OutputTensor tr1_perm; + TF_ASSERT_OK(FindNodeByName(result.get(), "tr1")->input_tensor(1, &tr1_perm)); + + EXPECT_EQ(tr0_perm.node, tr1_perm.node); +} + +TEST(CloneConstantsForBetterClusteringTest, InplaceOps) { + Scope root = Scope::NewRootScope().ExitOnError(); + Scope on_gpu = root.WithAssignedDevice(kGPU).WithDevice(kGPU); + Scope on_cpu = root.WithAssignedDevice(kCPU).WithDevice(kCPU); + + Output in0 = ops::Placeholder(on_gpu.WithOpName("in0"), DT_FLOAT); + Output in1 = ops::Placeholder(on_gpu.WithOpName("in1"), DT_FLOAT); + + Output perm = ops::Const(on_cpu.WithOpName("perm"), {3, 1, 2, 0}); + + { + Output tr0 = ops::Transpose(on_gpu.WithOpName("tr0"), in0, perm); + Output tr1 = ops::Transpose(on_gpu.WithOpName("tr1"), in1, perm); + } + + Output in_place_add = + ops::InplaceAdd(on_cpu.WithOpName("tr0"), perm, + ops::Placeholder(on_cpu.WithOpName("i"), DT_INT32), perm); + + std::unique_ptr result; + TF_ASSERT_OK(CloneConstantsForBetterClustering(root, &result)); + + OutputTensor tr0_perm; + TF_ASSERT_OK(FindNodeByName(result.get(), "tr0")->input_tensor(1, &tr0_perm)); + + OutputTensor tr1_perm; + TF_ASSERT_OK(FindNodeByName(result.get(), "tr1")->input_tensor(1, &tr1_perm)); + + EXPECT_EQ(tr0_perm.node, tr1_perm.node); +} +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/cluster_scoping_pass.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/cluster_scoping_pass.cc new file mode 100644 index 0000000000000000000000000000000000000000..aff21e05ae7fcf5719aa454bbd0a95428be4fa55 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/cluster_scoping_pass.cc @@ -0,0 +1,164 @@ +/* Copyright 2019 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/compiler/jit/cluster_scoping_pass.h" + +#include "absl/algorithm/container.h" +#include "absl/container/flat_hash_set.h" +#include "absl/strings/str_cat.h" +#include "tensorflow/compiler/jit/defs.h" +#include "tensorflow/compiler/jit/xla_cluster_util.h" +#include "tensorflow/core/framework/node_def_util.h" +#include "tensorflow/core/graph/algorithm.h" + +namespace tensorflow { +namespace { + +class ClusterScopingPassImpl { + public: + ClusterScopingPassImpl(Graph* graph, + OptimizerOptions::GlobalJitLevel global_jit_level) + : graph_(graph), + global_jit_level_(global_jit_level), + unique_scope_id_(0) {} + + absl::Status Run(); + + private: + absl::Status ScopingForPipelineStages(); + + size_t GetUniqueScopeId() { return unique_scope_id_++; } + + void AddScopeToAllTransitivePredecessors(Node* start); + + void AddScopeToAllTransitiveSuccessors(Node* start); + + private: + Graph* graph_; + OptimizerOptions::GlobalJitLevel global_jit_level_; + size_t unique_scope_id_; +}; + +std::optional GetXlaInternalScope(Node* node) { + std::string scope; + if (GetNodeAttr(node->attrs(), kXlaInternalScopeAttr, &scope).ok()) { + return scope; + } + + return std::nullopt; +} + +void SetXlaInternalScope(Node* node, absl::string_view scope) { + node->AddAttr(kXlaInternalScopeAttr, scope); +} + +// NB! We append a new scope as suffix to the _XlaInternalScope attribute +// instead of overriding the old value. In other words, appending scope B to +// scope A creates the conjunction of the scopes A and B (i.e, A & B) and, +// in effect, the node gets both the old and new scopes. As a unique scope +// disallows a node being merged with nodes in other scopes, the scope +// conjunction preserves the semantic of the old scope (i.e., the node still +// cannot be merged with the previously incompatible nodes.) +// +// For example, the below case should be rare in practice but can serve for the +// purpose of discussion. After adding scopes for both Stage and Unstage, +// Node_Y will receive both scopes "unstage" and "stage", while Node_X receives +// only scope "stage". The semantic of scope "unstage" is preserved although +// scope "stage" is later appended. As a result, Node_X and Node_Y will be put +// into different clusters. +// +// Unstage -> Node_Y (scope "unstage & stage") +// | +// V +// Node_X (scope "stage") -> Stage +// +void AddOrAppendXlaInternalScope(Node* node, absl::string_view suffix) { + std::string updated_scope; + std::optional cur_scope = GetXlaInternalScope(node); + if (cur_scope == std::nullopt) { + updated_scope = std::string(suffix); + } else { + updated_scope = absl::StrCat(cur_scope.value(), "&", suffix); + } + SetXlaInternalScope(node, updated_scope); +} + +void ClusterScopingPassImpl::AddScopeToAllTransitivePredecessors(Node* start) { + const std::string unique_suffix = absl::StrCat("_", GetUniqueScopeId()); + + std::vector starts; + starts.push_back(start); + auto enter = [&](Node* n) { AddOrAppendXlaInternalScope(n, unique_suffix); }; + ReverseDFSFrom(*graph_, starts, enter, /*leave=*/nullptr, + /*stable_comparator=*/NodeComparatorName()); +} + +void ClusterScopingPassImpl::AddScopeToAllTransitiveSuccessors(Node* start) { + const std::string unique_suffix = absl::StrCat("_", GetUniqueScopeId()); + + std::vector starts; + starts.push_back(start); + auto enter = [&](Node* n) { AddOrAppendXlaInternalScope(n, unique_suffix); }; + DFSFrom(*graph_, starts, enter, /*leave=*/nullptr, + /*stable_comparator=*/NodeComparatorName(), + // Do not filter any edges to better capture the semantics of + // transitive closure of successors. We may revisit this when + // we see more cases needing cluster scoping in the future. + /*edge_filter=*/nullptr); +} + +// This preserves the parallelism between pipeline stages. For example, below +// is a typical pattern of input pipelining in Tensorflow and this heuristic +// ensures Node_X and Node_Y are put into different clusters. Without the +// heuristic, they may be put into the same cluster and it can introduce +// artificial dependencies and incur great performance loss. In this example, +// Node_Y becomes dependent on IteratorGetNext and the latencies add up if +// Node_X and Node_Y are in the same cluster. +// +// IteratorGetNext -> Node_X -> Stage +// +// Unstage -> Node_Y +// +absl::Status ClusterScopingPassImpl::ScopingForPipelineStages() { + for (Node* n : graph_->nodes()) { + DCHECK(n); + if (n->type_string() == "Unstage") { + AddScopeToAllTransitiveSuccessors(n); + } + if (n->type_string() == "Stage") { + AddScopeToAllTransitivePredecessors(n); + } + } + + return absl::OkStatus(); +} + +absl::Status ClusterScopingPassImpl::Run() { + if (global_jit_level_ == OptimizerOptions::OFF) { + return absl::OkStatus(); + } + + return ScopingForPipelineStages(); +} +} // namespace + +absl::Status ClusterScopingPass::Run( + const GraphOptimizationPassOptions& options) { + Graph* graph = options.graph->get(); + + return ClusterScopingPassImpl{graph, GetGlobalJitLevelForGraph(options)} + .Run(); +} +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/cluster_scoping_pass.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/cluster_scoping_pass.h new file mode 100644 index 0000000000000000000000000000000000000000..47894fdfc9b87dbcd4fb4ee1e719b057352cbbd7 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/cluster_scoping_pass.h @@ -0,0 +1,38 @@ +/* Copyright 2019 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_CLUSTER_SCOPING_PASS_H_ +#define TENSORFLOW_COMPILER_JIT_CLUSTER_SCOPING_PASS_H_ + +#include "tensorflow/core/common_runtime/optimization_registry.h" + +namespace tensorflow { + +// This pass adds scopes to nodes in the _XlaInternalScope attribute to guide +// the later clustering passes. A major reason to do this is to prevent the +// clustering from losing critical parallelism in the Tensorflow graph, which +// can incur great performance degradation. +// +// This pass must be run before MarkForCompilationPass, as it stores the +// scoping information that MarkForCompilationPass will need to respect for +// clustering decision. +class ClusterScopingPass : public GraphOptimizationPass { + public: + absl::Status Run(const GraphOptimizationPassOptions& options) override; +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_CLUSTER_SCOPING_PASS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/cluster_scoping_pass_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/cluster_scoping_pass_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..ccc1dbaa012db23b7c98e64697058ecdd354543e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/cluster_scoping_pass_test.cc @@ -0,0 +1,180 @@ +/* Copyright 2019 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/compiler/jit/cluster_scoping_pass.h" + +#include "absl/container/flat_hash_map.h" +#include "tensorflow/compiler/jit/defs.h" +#include "tensorflow/compiler/jit/test_util.h" +#include "tensorflow/core/common_runtime/graph_constructor.h" +#include "tensorflow/core/common_runtime/graph_def_builder_util.h" +#include "tensorflow/core/framework/node_def_util.h" +#include "tensorflow/core/framework/op.h" +#include "tensorflow/core/graph/algorithm.h" +#include "tensorflow/core/graph/graph_def_builder.h" +#include "tensorflow/core/lib/core/status_test_util.h" +#include "tensorflow/core/platform/test.h" +#include "tensorflow/core/public/session_options.h" + +namespace tensorflow { +namespace { + +absl::Status ClusterScoping(std::unique_ptr* graph) { + FixupSourceAndSinkEdges(graph->get()); + + GraphOptimizationPassWrapper wrapper; + wrapper.session_options.config.mutable_graph_options() + ->mutable_optimizer_options() + ->set_global_jit_level(OptimizerOptions::ON_2); + GraphOptimizationPassOptions opt_options = + wrapper.CreateGraphOptimizationPassOptions(graph); + + ClusterScopingPass pass; + return pass.Run(opt_options); +} + +absl::flat_hash_map GetXlaInternalScopes( + const Graph& graph) { + absl::flat_hash_map scopes; + for (Node* node : graph.nodes()) { + std::string scope; + if (GetNodeAttr(node->attrs(), kXlaInternalScopeAttr, &scope).ok()) { + scopes[node->name()] = scope; + } + } + + if (VLOG_IS_ON(2)) { + VLOG(2) << "_XlaInternalScopes:"; + for (const auto& p : scopes) { + VLOG(2) << " " << p.first << " -> " << p.second; + } + } + return scopes; +} + +Node* BuildStageNode(GraphDefBuilder& builder, std::string name, + std::initializer_list dtypes, + absl::Span values) { + auto opts = builder.opts() + .WithName(std::move(name)) + .WithAttr("dtypes", std::move(dtypes)); + if (opts.HaveError()) { + return nullptr; + } + + NodeBuilder node_builder(name, "Stage", opts.op_registry()); + node_builder.Input(values); + return opts.FinalizeBuilder(&node_builder); +} + +TEST(XlaCompilationTest, StagePipelinePreserved) { + std::unique_ptr graph(new Graph(OpRegistry::Global())); + { + // Graph: + // b + // | + // v + // a -> add0 (ClusterX) -> relu0 (ClusterX) -> stage + // + // b + // | + // v + // unstage -> add1 (ClusterY) -> relu1 (ClusterY) + GraphDefBuilder builder(GraphDefBuilder::kFailImmediately); + Node* a = ops::SourceOp("Const", builder.opts() + .WithName("a") + .WithAttr("dtype", DT_FLOAT) + .WithAttr("value", Tensor())); + Node* b = ops::SourceOp("Const", builder.opts() + .WithName("b") + .WithAttr("dtype", DT_FLOAT) + .WithAttr("value", Tensor())); + Node* unstage = ops::SourceOp( + "Unstage", + builder.opts().WithName("unstage").WithAttr("dtypes", {DT_FLOAT})); + + Node* add0 = ops::BinaryOp("Add", a, b, builder.opts().WithName("add0")); + Node* add1 = + ops::BinaryOp("Add", unstage, b, builder.opts().WithName("add1")); + Node* relu0 = ops::UnaryOp("Relu", add0, builder.opts().WithName("relu0")); + ops::UnaryOp("Relu", add1, builder.opts().WithName("relu1")); + BuildStageNode(builder, "stage", {DT_FLOAT}, {relu0}); + + TF_EXPECT_OK(GraphDefBuilderToGraph(builder, graph.get())); + } + + TF_ASSERT_OK(ClusterScoping(&graph)); + + auto scopes = GetXlaInternalScopes(*graph); + EXPECT_NE(scopes["add0"], scopes["add1"]); + EXPECT_EQ(scopes["add0"], scopes["relu0"]); + EXPECT_EQ(scopes["add1"], scopes["relu1"]); +} + +TEST(XlaCompilationTest, StagePipelinePreservedAndInitialScopesRespected) { + std::unique_ptr graph(new Graph(OpRegistry::Global())); + { + // Graph: + // b + // | + // v + // a -> add0 (ClusterA) -> relu0 (ClusterB) -> stage + // + // b + // | + // v + // unstage -> add1 (ClusterC) -> relu1 (ClusterD) + GraphDefBuilder builder(GraphDefBuilder::kFailImmediately); + Node* a = ops::SourceOp("Const", builder.opts() + .WithName("a") + .WithAttr("dtype", DT_FLOAT) + .WithAttr("value", Tensor())); + Node* b = ops::SourceOp("Const", builder.opts() + .WithName("b") + .WithAttr("dtype", DT_FLOAT) + .WithAttr("value", Tensor())); + Node* unstage = ops::SourceOp( + "Unstage", + builder.opts().WithName("unstage").WithAttr("dtypes", {DT_FLOAT})); + + // Intentionally give add0 and add1 the same initial scope but they should + // be separated by the ClusterScopingPass. + Node* add0 = ops::BinaryOp("Add", a, b, + builder.opts().WithName("add0").WithAttr( + kXlaInternalScopeAttr, "ClusterA")); + Node* add1 = ops::BinaryOp("Add", unstage, b, + builder.opts().WithName("add1").WithAttr( + kXlaInternalScopeAttr, "ClusterA")); + Node* relu0 = ops::UnaryOp("Relu", add0, + builder.opts().WithName("relu0").WithAttr( + kXlaInternalScopeAttr, "ClusterB")); + ops::UnaryOp("Relu", add1, + builder.opts().WithName("relu1").WithAttr( + kXlaInternalScopeAttr, "ClusterD")); + BuildStageNode(builder, "stage", {DT_FLOAT}, {relu0}); + + TF_EXPECT_OK(GraphDefBuilderToGraph(builder, graph.get())); + } + + TF_ASSERT_OK(ClusterScoping(&graph)); + + auto scopes = GetXlaInternalScopes(*graph); + EXPECT_NE(scopes["add0"], scopes["add1"]); + EXPECT_NE(scopes["add0"], scopes["relu0"]); + EXPECT_NE(scopes["add1"], scopes["relu1"]); +} + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/compilability_check_util.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/compilability_check_util.cc new file mode 100644 index 0000000000000000000000000000000000000000..6cd98eaffb35e45e9188839c38d90a89eee9c888 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/compilability_check_util.cc @@ -0,0 +1,758 @@ +/* Copyright 2019 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/compiler/jit/compilability_check_util.h" + +#include +#include +#include +#include +#include + +#include "absl/algorithm/container.h" +#include "absl/container/flat_hash_set.h" +#include "absl/log/check.h" +#include "absl/log/log.h" +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" +#include "absl/types/span.h" +#include "tensorflow/compiler/jit/defs.h" +#include "tensorflow/compiler/jit/xla_activity.pb.h" +#include "tensorflow/compiler/jit/xla_activity_listener.h" +#include "tensorflow/compiler/jit/xla_cluster_util.h" +#include "tensorflow/compiler/tf2xla/const_analysis.h" +#include "tensorflow/compiler/tf2xla/tf2xla_defs.h" +#include "tensorflow/compiler/tf2xla/xla_op_registry.h" +#include "xla/tsl/platform/errors.h" +#include "tensorflow/core/common_runtime/function_body.h" +#include "tensorflow/core/common_runtime/function_utils.h" +#include "tensorflow/core/framework/attr_value.pb.h" +#include "tensorflow/core/framework/graph_def_util.h" +#include "tensorflow/core/framework/memory_types.h" +#include "tensorflow/core/framework/node_def.pb.h" +#include "tensorflow/core/framework/node_def_util.h" +#include "tensorflow/core/framework/op_kernel.h" +#include "tensorflow/core/framework/types.h" +#include "tensorflow/core/graph/graph.h" +#include "tensorflow/core/lib/gtl/cleanup.h" + +namespace tensorflow { + +namespace { + +bool HasResourceInput(const Node& node) { + return absl::c_count(node.input_types(), DT_RESOURCE) != 0; +} + +void LogNotCompilable(const Node& node, absl::string_view reason = "") { + VLOG(3) << "Found uncompilable node " << node.name() << " (op " + << node.type_string() << ")" << (reason.empty() ? "" : ": ") + << reason; +} + +bool IsInOutsideCompilationCluster(const Node& n) { + return n.attrs().Find(kXlaOutsideCompilationAttr) != nullptr; +} + +absl::Status MakeCallNodeFromAttribute(const Node& node, + const std::string& attr_name, + NodeDef* node_def) { + const NameAttrList* name_attr; + TF_RETURN_IF_ERROR(GetNodeAttr(node.attrs(), attr_name, &name_attr)); + node_def->set_op(name_attr->name()); + *(node_def->mutable_attr()) = name_attr->attr(); + return absl::OkStatus(); +} + +absl::StatusOr> MakeCallNodesFromAttribute( + const Node& node, absl::string_view attr_name, + absl::string_view call_name) { + std::vector attr_lists; + TF_RETURN_IF_ERROR(GetNodeAttr(node.attrs(), attr_name, &attr_lists)); + + std::vector out; + out.reserve(attr_lists.size()); + for (int i = 0; i < attr_lists.size(); i++) { + out.emplace_back(); + NodeDef& inserted = out.back(); + inserted.set_name(absl::StrCat(call_name, "_", i)); + inserted.set_op(attr_lists[i].name()); + *inserted.mutable_attr() = attr_lists[i].attr(); + } + return out; +} + +// Utility which searches for values in a sorted list by scanning over it once. +// No matter how many times ScanForValue is called, the list is scanned at most +// once. However, if a call to ScanForValue skips over a value, that value is +// not revisited in future calls to ScanForValue, so callers must take +// care to order their calls. +// +// Useful for merging multiple sorted lists in O(n) time. +class SinglePassSearch { + public: + // Creates a SinglePassSearch object that can be used to search in `values`. + // Does not take ownership of `values`. `values` must outlive this. + // `values` must be sorted. + explicit SinglePassSearch(absl::Span values) + : current_index_(0), values_(values) {} + + // Scans forward in the vector looking for "value", updating the internal + // position in to the vector. + // Returns true iff the vector contains the given value at or after current + // position. + // Not thread-safe. + bool ScanForValue(int value) { + while (current_index_ < values_.size() && + values_[current_index_] <= value) { + if (values_[current_index_] == value) { + current_index_++; + return true; + } + current_index_++; + } + return false; + } + + private: + int current_index_; + const absl::Span values_; +}; + +} // anonymous namespace + +RecursiveCompilabilityChecker::UncompilableNodesMap +RecursiveCompilabilityChecker::FindUncompilableNodes( + const Node& node, FunctionLibraryRuntime* lib_runtime, + const std::vector* + node_stack_trace) const { + std::vector stack_trace; + // If `node_stack_trace` is provided, that means `node` is inside + // a function body, and therefore, arg nodes and retval nodes are + // not considered uncompilable. + if (node_stack_trace != nullptr) { + for (const auto& frame : *node_stack_trace) { + stack_trace.emplace_back( + StackFrameView{frame.name, frame.function_name, frame.stack_trace}); + } + } + stack_trace.emplace_back( + StackFrameView{node.name(), "", node.GetStackTrace()}); + + RecursiveCompilabilityChecker::UncompilableNodesMap uncompilable_nodes; + IsCompilableNode(node, lib_runtime, &stack_trace, + /*encapsulating_function=*/nullptr, &uncompilable_nodes); + return uncompilable_nodes; +} + +bool RecursiveCompilabilityChecker::HasXLAKernel( + const Node& node, std::string* uncompilable_reason) const { + // There is a SymbolicGradient kernel on the XLA_JIT device, but the gradient + // is really a kind of function call and will be handled by + // IsCompilableCall(). + if (node.type_string() == "SymbolicGradient") { + *uncompilable_reason = + "SymbolicGradient should be handled by IsCompilableCall()."; + return false; + } + + if (node.type_string() == "Const") { + const AttrValue* attr = node.attrs().Find("dtype"); + if (!op_filter_.allow_string_consts && attr != nullptr && + attr->type() == DT_STRING) { + *uncompilable_reason = + "Const op with type DT_STRING is not supported by XLA."; + return false; + } + } + + // XLA does not offer guaranteed aliasing between the input and output of the + // XLA cluster so it can't implement the forward-tensor-ref semantic. Leave + // such nodes out of XLA clusters. + if (HasForwardedRefInput(node)) { + VLOG(2) << "Rejecting " << node.name() << ": Identity with unsafe cast."; + *uncompilable_reason = "Identity with unsafe cast."; + return false; + } + + absl::Status s = + FindKernelDef(jit_device_type_, node.def(), nullptr, nullptr); + if (!s.ok()) { + *uncompilable_reason = s.message(); + return false; + } + return true; +} + +// Tests whether 'if_node' is compilable. Every operator in the then_branch and +// else_branch functions must be compilable for 'if_node' to be compilable. +bool RecursiveCompilabilityChecker::IsCompilableIf( + const Node& if_node, FunctionLibraryRuntime* lib_runtime, + std::vector* stack_trace, + NameAttrList* encapsulating_function, + RecursiveCompilabilityChecker::UncompilableNodesMap* uncompilable_nodes) + const { + bool is_compilable = true; + is_compilable &= ExtractNodeDefAndCheckCompilability( + if_node, "then_branch", "if_then", encapsulating_function, lib_runtime, + stack_trace, uncompilable_nodes); + if (!uncompilable_nodes && !is_compilable) return is_compilable; + + is_compilable &= ExtractNodeDefAndCheckCompilability( + if_node, "else_branch", "if_else", encapsulating_function, lib_runtime, + stack_trace, uncompilable_nodes); + + return is_compilable; +} + +bool RecursiveCompilabilityChecker::IsCompilableCase( + const Node& case_node, FunctionLibraryRuntime* lib_runtime, + std::vector* stack_trace, + NameAttrList* encapsulating_function, + RecursiveCompilabilityChecker::UncompilableNodesMap* uncompilable_nodes) + const { + absl::StatusOr> calls = + MakeCallNodesFromAttribute(case_node, "branches", "branch"); + if (!calls.ok()) { + VLOG(2) << "Rejecting node " << case_node.name() << ": " + << "missing attribute 'branches'"; + return false; + } + + bool is_compilable = true; + + for (const NodeDef& call : *calls) { + is_compilable &= + IsCompilableCall(call, lib_runtime, stack_trace, encapsulating_function, + uncompilable_nodes); + } + return is_compilable; +} + +// Tests whether 'while_node' is a completely compilable loop. +// Every operator in the condition and body functions must be compilable for a +// while loop to be compilable. +bool RecursiveCompilabilityChecker::IsCompilableWhile( + const Node& while_node, FunctionLibraryRuntime* lib_runtime, + std::vector* stack_trace, + NameAttrList* encapsulating_function, + RecursiveCompilabilityChecker::UncompilableNodesMap* uncompilable_nodes) + const { + bool is_compilable = true; + is_compilable &= ExtractNodeDefAndCheckCompilability( + while_node, "cond", "while_cond", encapsulating_function, lib_runtime, + stack_trace, uncompilable_nodes); + + if (!uncompilable_nodes && !is_compilable) return is_compilable; + + is_compilable &= ExtractNodeDefAndCheckCompilability( + while_node, "body", "while_body", encapsulating_function, lib_runtime, + stack_trace, uncompilable_nodes); + + return is_compilable; +} + +bool RecursiveCompilabilityChecker::ExtractNodeDefAndCheckCompilability( + const Node& node, const std::string& attr_name, + const std::string& call_name, NameAttrList* encapsulating_function, + FunctionLibraryRuntime* lib_runtime, + std::vector* stack_trace, + RecursiveCompilabilityChecker::UncompilableNodesMap* uncompilable_nodes) + const { + NodeDef call; + call.set_name(call_name); + if (!MakeCallNodeFromAttribute(node, attr_name, &call).ok()) { + const auto uncompilable_reason = absl::StrCat( + "missing '", attr_name, "' attribute from node", node.name()); + MaybeMarkUncompilableNode(uncompilable_reason, *stack_trace, + encapsulating_function, uncompilable_nodes); + VLOG(2) << "Rejecting node " << node.name() << ": " << uncompilable_reason + << "."; + return false; + } + if (!IsCompilableCall(call, lib_runtime, stack_trace, encapsulating_function, + uncompilable_nodes)) { + VLOG(2) << "Rejecting node " << node.name() + << ": can't compile : " << call.op(); + return false; + } + return true; +} + +// Tests whether 'call_def' is a call to a completely compilable function. +// Every operator in the function must be compilable for a function to be +// compilable. +bool RecursiveCompilabilityChecker::IsCompilableCall( + const NodeDef& call_def, FunctionLibraryRuntime* lib_runtime, + std::vector* stack_trace, + NameAttrList* encapsulating_function, + RecursiveCompilabilityChecker::UncompilableNodesMap* uncompilable_nodes) + const { + if (stack_trace->size() > kMaxRecursionDepth) { + std::string uncompilable_reason = "function depth limit exceeded"; + MaybeMarkUncompilableNode(uncompilable_reason, *stack_trace, + encapsulating_function, uncompilable_nodes); + VLOG(2) << "Rejecting " << call_def.op() << ": " << uncompilable_reason + << "."; + return false; + } + + FunctionLibraryRuntime::Handle handle; + absl::Status s; + NameAttrList function; + s = NameAndAttrsFromFunctionCall(call_def, &function); + if (s.ok()) { + s = lib_runtime->Instantiate(function.name(), AttrSlice(&function.attr()), + &handle); + } + if (!s.ok()) { + std::string uncompilable_reason = + absl::StrCat("could not instantiate call: '", function.name(), "'"); + MaybeMarkUncompilableNode(uncompilable_reason, *stack_trace, + encapsulating_function, uncompilable_nodes); + VLOG(2) << "Rejecting " << call_def.DebugString() << ": " + << uncompilable_reason << " : " << s; + return false; + } + + auto release_handle_on_return = + gtl::MakeCleanup([&] { CHECK_OK(lib_runtime->ReleaseHandle(handle)); }); + const FunctionBody* fbody = lib_runtime->GetFunctionBody(handle); + bool is_compilable = true; + for (const Node* node : fbody->graph->op_nodes()) { + stack_trace->emplace_back( + StackFrameView{node->name(), function.name(), node->GetStackTrace()}); + is_compilable &= IsCompilableNode(*node, lib_runtime, stack_trace, + &function, uncompilable_nodes); + stack_trace->pop_back(); + if (!uncompilable_nodes && !is_compilable) return is_compilable; + } + + return is_compilable; +} + +bool RecursiveCompilabilityChecker::OpIsInaccurate(const Node& node) const { + // b/127344411: SelfAdjointEigV2 and Svd precision issues. + return node.type_string() == "SelfAdjointEigV2" || + node.type_string() == "Svd"; +} + +bool RecursiveCompilabilityChecker::OpIsSlow(const Node& node) const { + // b/128001705: SelfAdjointEigV2 and Svd performance issues. + // b/135640736: MatrixInverse performance issues. + // b/111271662: MatrixSolve performance issues. + // https://github.com/tensorflow/tensorflow/pull/31012: + // ResizeNearestNeighbor, ResizeBilinear, and ResizeBilinearGrad sometimes + // create convolutions too large for CuDNN to handle. + // NonMaxSuppressionV3/V4 in XLA runs significantly slower than TF kernel in + // object detection models, specially when there are a lot of proposed + // bounding boxes. + return node.type_string() == "SelfAdjointEigV2" || + node.type_string() == "Svd" || node.type_string() == "Qr" || + node.type_string() == "MatrixInverse" || + node.type_string() == "MatrixSolve" || + node.type_string() == "ResizeBilinearGrad" || + node.type_string() == "NonMaxSuppressionV3" || + node.type_string() == "NonMaxSuppressionV4"; +} + +bool RecursiveCompilabilityChecker::IsCompilableNode( + const Node& node, FunctionLibraryRuntime* lib_runtime, + std::vector* stack_trace, + NameAttrList* encapsulating_function, + RecursiveCompilabilityChecker::UncompilableNodesMap* uncompilable_nodes) + const { + auto stack_depth = stack_trace->size(); + + if (op_filter_.allow_outside_compiled && IsInOutsideCompilationCluster(node)) + return true; + + if (node.IsSource() || node.IsSink()) { + absl::string_view uncompilable_reason = "source or sink node"; + MaybeMarkUncompilableNode(uncompilable_reason, *stack_trace, + encapsulating_function, uncompilable_nodes); + LogNotCompilable(node, uncompilable_reason); + return false; + } + + // _Arg nodes in a top-level function represent feeds and _Retval nodes in a + // top-level function represent fetches. + if (stack_depth == 1 && + (node.type_string() == "_Arg" || node.type_string() == "_Retval")) { + absl::string_view uncompilable_reason = "top level _Arg or _Retval"; + MaybeMarkUncompilableNode(uncompilable_reason, *stack_trace, + encapsulating_function, uncompilable_nodes); + LogNotCompilable(node, uncompilable_reason); + return false; + } + + if (node.attrs().Find("_scoped_allocator") || + node.attrs().Find("_forward_from")) { + // TODO(b/128858118): XLA does not support _scoped_allocator and + // _forward_from. + absl::string_view uncompilable_reason = + "_scoped_allocator or _forward_from attribute"; + MaybeMarkUncompilableNode(uncompilable_reason, *stack_trace, + encapsulating_function, uncompilable_nodes); + LogNotCompilable(node, uncompilable_reason); + return false; + } + + std::string uncompilable_reason; + if (IsFunctionCall(*lib_runtime->GetFunctionLibraryDefinition(), node)) { + if (!IsCompilableCall(node.def(), lib_runtime, stack_trace, + encapsulating_function, uncompilable_nodes)) { + LogNotCompilable(node, "unsupported function"); + return false; + } + } else if (!HasXLAKernel(node, &uncompilable_reason)) { + MaybeMarkUncompilableNode( + absl::StrCat("unsupported op: ", uncompilable_reason), *stack_trace, + encapsulating_function, uncompilable_nodes); + LogNotCompilable(node, uncompilable_reason); + return false; + } + + if (node.IsWhileNode() && + !IsCompilableWhile(node, lib_runtime, stack_trace, encapsulating_function, + uncompilable_nodes)) { + LogNotCompilable(node, "unsupported while"); + return false; + } + + if (node.IsIfNode() && + !IsCompilableIf(node, lib_runtime, stack_trace, encapsulating_function, + uncompilable_nodes)) { + LogNotCompilable(node, "unsupported if"); + return false; + } + + if (op_filter_.require_always_compilable && node.IsCaseNode() && + !IsCompilableCase(node, lib_runtime, stack_trace, encapsulating_function, + uncompilable_nodes)) { + LogNotCompilable(node, "unsupported case"); + return false; + } + + if (!op_filter_.allow_stateful_rng_ops && + IsStatefulRandomOp(node.type_string())) { + absl::string_view uncompilable_reason = "stateful random op"; + MaybeMarkUncompilableNode(uncompilable_reason, *stack_trace, + encapsulating_function, uncompilable_nodes); + LogNotCompilable(node, uncompilable_reason); + return false; + } + + if (!op_filter_.allow_control_trigger && node.IsControlTrigger()) { + absl::string_view uncompilable_reason = "not allowed control trigger"; + MaybeMarkUncompilableNode(uncompilable_reason, *stack_trace, + encapsulating_function, uncompilable_nodes); + LogNotCompilable(node, uncompilable_reason); + return false; + } + + if (!op_filter_.allow_eliding_assert_and_checknumerics_ops && + IsAssertOrCheckNumerics(node.type_string())) { + absl::string_view uncompilable_reason = "Assert or CheckNumerics"; + MaybeMarkUncompilableNode(uncompilable_reason, *stack_trace, + encapsulating_function, uncompilable_nodes); + LogNotCompilable(node, uncompilable_reason); + return false; + } + + if (!op_filter_.allow_collective_reduce_v2 && + node.type_string() == "CollectiveReduceV2") { + absl::string_view uncompilable_reason = "Collective op"; + MaybeMarkUncompilableNode(uncompilable_reason, *stack_trace, + encapsulating_function, uncompilable_nodes); + LogNotCompilable(node, uncompilable_reason); + return false; + } + + if (!op_filter_.allow_where_op && node.type_string() == "Where") { + absl::string_view uncompilable_reason = "Where op"; + MaybeMarkUncompilableNode(uncompilable_reason, *stack_trace, + encapsulating_function, uncompilable_nodes); + LogNotCompilable(node, uncompilable_reason); + return false; + } + + if (!op_filter_.allow_unique_op && node.type_string() == "Unique") { + absl::string_view uncompilable_reason = "Unique op"; + MaybeMarkUncompilableNode(uncompilable_reason, *stack_trace, + encapsulating_function, uncompilable_nodes); + LogNotCompilable(node, uncompilable_reason); + return false; + } + + if (!op_filter_.allow_ops_producing_or_consuming_variant && + OpProducesOrConsumesVariant(node)) { + absl::string_view uncompilable_reason = "DT_VARIANT producer/consumer"; + MaybeMarkUncompilableNode(uncompilable_reason, *stack_trace, + encapsulating_function, uncompilable_nodes); + LogNotCompilable(node, uncompilable_reason); + return false; + } + + if (!op_filter_.allow_stack_ops && IsStackOp(node)) { + absl::string_view uncompilable_reason = "Stack op"; + MaybeMarkUncompilableNode(uncompilable_reason, *stack_trace, + encapsulating_function, uncompilable_nodes); + LogNotCompilable(node, uncompilable_reason); + return false; + } + + if (!op_filter_.allow_tensor_array_ops && IsTensorArrayOp(node)) { + absl::string_view uncompilable_reason = "TensorArray op"; + MaybeMarkUncompilableNode(uncompilable_reason, *stack_trace, + encapsulating_function, uncompilable_nodes); + LogNotCompilable(node, uncompilable_reason); + return false; + } + + if (!op_filter_.allow_resource_ops_in_called_functions && stack_depth > 1 && + HasResourceInput(node)) { + absl::string_view uncompilable_reason = + "resource variable op in called function"; + MaybeMarkUncompilableNode(uncompilable_reason, *stack_trace, + encapsulating_function, uncompilable_nodes); + LogNotCompilable(node, uncompilable_reason); + return false; + } + + if (!op_filter_.allow_inaccurate_ops && OpIsInaccurate(node)) { + absl::string_view uncompilable_reason = + "operation with numerical accuracy issues"; + BroadcastOptimizationRemark(XlaOptimizationRemark::INACCURATE_OPERATION, + node.DebugString()) + .IgnoreError(); + MaybeMarkUncompilableNode(uncompilable_reason, *stack_trace, + encapsulating_function, uncompilable_nodes); + LogNotCompilable(node, uncompilable_reason); + return false; + } + + if (!op_filter_.allow_slow_ops && OpIsSlow(node)) { + absl::string_view uncompilable_reason = "slow operation"; + BroadcastOptimizationRemark(XlaOptimizationRemark::SLOW_OPERATION, + node.DebugString()) + .IgnoreError(); + MaybeMarkUncompilableNode(uncompilable_reason, *stack_trace, + encapsulating_function, uncompilable_nodes); + LogNotCompilable(node, uncompilable_reason); + return false; + } + + return true; +} + +RecursiveCompilabilityChecker::OperationFilter CreateOperationFilter( + const XlaOpRegistry::DeviceRegistration& registration) { + RecursiveCompilabilityChecker::OperationFilter op_filter; + op_filter.allow_resource_ops_in_called_functions = + registration.cluster_resource_variable_ops_unsafely; + op_filter.allow_stack_ops = registration.cluster_stack_ops; + op_filter.allow_tensor_array_ops = registration.cluster_tensor_array_ops; + op_filter.allow_stateful_rng_ops = registration.cluster_stateful_rng_ops; + op_filter.allow_control_trigger = registration.cluster_control_trigger; + op_filter.allow_eliding_assert_and_checknumerics_ops = + registration.elide_assert_and_checknumerics; + op_filter.allow_ops_producing_or_consuming_variant = + registration.cluster_variant_ops; + op_filter.allow_slow_ops = registration.cluster_slow_ops; + op_filter.allow_inaccurate_ops = registration.cluster_inaccurate_ops; + return op_filter; +} + +/*static*/ void RecursiveCompilabilityChecker::MaybeMarkUncompilableNode( + const absl::string_view reason, + const std::vector& stack_trace, + NameAttrList* encapsulating_function, + RecursiveCompilabilityChecker::UncompilableNodesMap* uncompilable_nodes) { + if (!uncompilable_nodes) return; + + UncompilableNodeInfo node_info; + node_info.uncompilable_reason = std::string(reason); + absl::c_transform(stack_trace, std::back_inserter(node_info.stack_trace), + [](const StackFrameView& stack_element) { + return StackFrame{ + std::string(stack_element.name), + std::string(stack_element.function_name), + stack_element.stack_trace}; + }); + + node_info.name = std::string(stack_trace.back().name); + auto function = + encapsulating_function ? *encapsulating_function : NameAttrList(); + auto function_identifier = function.ShortDebugString(); + + auto it = uncompilable_nodes->find(function_identifier); + if (it == uncompilable_nodes->end()) { + std::vector + uncompilable_node_info{std::move(node_info)}; + uncompilable_nodes->emplace( + std::move(function_identifier), + std::make_pair(function, std::move(uncompilable_node_info))); + } else { + it->second.second.emplace_back(std::move(node_info)); + } +} + +// Returns `true` iff node has a given `attr` set to `true`. Returns `false` +// both for the missing attr, and the attr set to `false`. +static bool HasBoolAttr(const NodeDef& node, const char* attr) { + const auto& it = node.attr().find(attr); + return it != node.attr().end() && it->second.b(); +} + +bool CanCreateXlaKernel(const NodeDef& node_def) { + return HasBoolAttr(node_def, kXlaMustCompileAttr); +} + +absl::Status GetBodyAndConstantsAndResources( + FunctionLibraryRuntime* flr, const NameAttrList& function, + const FunctionBody** fbody, std::vector* constant_arg_indices, + std::vector* resource_arg_indices) { + FunctionLibraryRuntime::Handle handle; + TF_RETURN_IF_ERROR( + flr->Instantiate(function.name(), AttrSlice(&function.attr()), &handle)); + *fbody = flr->GetFunctionBody(handle); + CHECK(*fbody); // Can't be nullptr since we just instantiated it. + const DataTypeVector& arg_types = (*fbody)->arg_types; + std::vector const_args(arg_types.size()); + // If we can't analyze the const args. Bail out. + TF_RETURN_IF_ERROR( + BackwardsConstAnalysis(*((*fbody)->graph), &const_args, + /*compile_time_const_nodes=*/nullptr, flr)); + + for (size_t i = 0; i < const_args.size(); ++i) { + if (const_args[i]) { + constant_arg_indices->push_back(i); + } + } + + // There can be hundreds of resource variables. Reserve the space for them. + // We don't reserve for constants above as they are usually few. + resource_arg_indices->reserve(arg_types.size()); + for (size_t i = 0; i < arg_types.size(); ++i) { + if (arg_types[i] == DT_RESOURCE) { + resource_arg_indices->push_back(i); + } + } + + return absl::OkStatus(); +} + +tensorflow::MemoryTypeVector GetInputMemoryTypes( + const tensorflow::FunctionBody* fbody, + absl::Span constant_arg_indices, + absl::Span resource_arg_indices) { + // Set input and output memory types. + tensorflow::MemoryTypeVector input_memory_types(fbody->arg_types.size(), + tensorflow::DEVICE_MEMORY); + // These indices are used only for optimization purposes. They allow us + // to loop over constant_arg_indices and resource_arg_indices only once + // while iterating over all the function arguments checking if it is a + // resource or a constant. + // The reason we optimized this code is because functions can have a lot of + // captured arguments. For example, the backward pass of ResNet50 takes in all + // 214 variables and a similar number of activations. + SinglePassSearch constants_search(constant_arg_indices); + SinglePassSearch resources_search(resource_arg_indices); + for (size_t i = 0; i < fbody->arg_types.size(); ++i) { + if (resources_search.ScanForValue(i) || constants_search.ScanForValue(i)) { + // Compile-time constants and resource handles are expected to be in + // host memory. + input_memory_types[i] = tensorflow::HOST_MEMORY; + } + } + return input_memory_types; +} + +tensorflow::MemoryTypeVector GetOutputMemoryTypes( + const tensorflow::FunctionBody* fbody) { + tensorflow::MemoryTypeVector output_memory_types(fbody->ret_types.size(), + tensorflow::DEVICE_MEMORY); + for (size_t i = 0; i < fbody->ret_types.size(); ++i) { + if (fbody->ret_types[i] == tensorflow::DT_RESOURCE) { + output_memory_types[i] = tensorflow::HOST_MEMORY; + } + } + return output_memory_types; +} + +static auto const ops_triggering_xla_compilation = + new absl::flat_hash_set{"XlaBroadcastHelper", + "XlaCallModule", + "XlaConv", + "XlaConvV2", + "XlaDequantize", + "XlaDot", + "XlaDotV2", + "XlaDynamicSlice", + "XlaDynamicUpdateSlice", + "XlaEinsum", + "XlaGather", + "XlaIf", + "XlaKeyValueSort", + "XlaPad", + "XlaRecv", + "XlaReduce", + "XlaReduceWindow", + "XlaReplicaId", + "XlaRngBitGenerator", + "XlaScatter", + "XlaSelectAndScatter", + "XlaSelfAdjointEig", + "XlaSend", + "XlaSharding", + "XlaSort", + "XlaSpmdFullToShardShape", + "XlaSpmdShardToFullShape", + "XlaSvd", + "XlaVariadicReduceV2", + "XlaVariadicSort", + "XlaWhile"}; + +bool NodeCanTriggerXlaCompilation(const NodeDef& node) { + return node.attr().find(kXlaClusterIdAttr) != node.attr().end() || + HasBoolAttr(node, kXlaMustCompileAttr) || + HasBoolAttr(node, kXlaCompileAttr) || + HasBoolAttr(node, kXlaScopeAttr) || + HasBoolAttr(node, kXlaInternalScopeAttr) || + ops_triggering_xla_compilation->count(node.op()); +} + +bool CanTriggerXlaCompilation(const GraphDef& graph) { + for (const FunctionDef& function : graph.library().function()) { + for (const NodeDef& node : function.node_def()) { + if (NodeCanTriggerXlaCompilation(node)) { + return true; + } + } + } + + for (const NodeDef& node : graph.node()) { + if (NodeCanTriggerXlaCompilation(node)) { + return true; + } + } + + return false; +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/compilability_check_util.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/compilability_check_util.h new file mode 100644 index 0000000000000000000000000000000000000000..307f74c223bc1ac9121d3d57a5cbd7c605fcd5cd --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/compilability_check_util.h @@ -0,0 +1,340 @@ +/* Copyright 2019 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_COMPILABILITY_CHECK_UTIL_H_ +#define TENSORFLOW_COMPILER_JIT_COMPILABILITY_CHECK_UTIL_H_ + +#include + +#include "absl/algorithm/container.h" +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" +#include "absl/types/optional.h" +#include "tensorflow/compiler/jit/defs.h" +#include "tensorflow/compiler/jit/device_util.h" +#include "tensorflow/compiler/jit/flags.h" +#include "tensorflow/compiler/jit/resource_operation_safety_analysis.h" +#include "tensorflow/compiler/tf2xla/const_analysis.h" +#include "tensorflow/compiler/tf2xla/resource_operation_table.h" +#include "tensorflow/compiler/tf2xla/xla_op_registry.h" +#include "xla/service/graphcycles/graphcycles.h" +#include "xla/union_find.h" +#include "xla/util.h" +#include "tensorflow/core/common_runtime/function.h" +#include "tensorflow/core/common_runtime/graph_constructor.h" +#include "tensorflow/core/framework/attr_value.pb.h" +#include "tensorflow/core/framework/bounds_check.h" +#include "tensorflow/core/framework/function.h" +#include "tensorflow/core/framework/graph_def_util.h" +#include "tensorflow/core/framework/memory_types.h" +#include "tensorflow/core/framework/node_def.pb.h" +#include "tensorflow/core/framework/op_kernel.h" +#include "tensorflow/core/framework/types.h" +#include "tensorflow/core/framework/types.pb.h" +#include "tensorflow/core/graph/algorithm.h" +#include "tensorflow/core/graph/control_flow.h" +#include "tensorflow/core/graph/graph.h" +#include "tensorflow/core/lib/gtl/cleanup.h" +#include "tensorflow/core/lib/strings/stringprintf.h" +#include "tensorflow/core/public/version.h" +#include "tensorflow/core/util/dump_graph.h" + +namespace tensorflow { +// Checks whether a TF node can be compiled or not. "Recursive" as in for call +// and functional while nodes it recursively checks whether the callee functions +// can be compiled. +class RecursiveCompilabilityChecker { + public: + // Contains node name and function name. If the node is not inside a function + // body, function name is an empty string. + struct StackFrame { + std::string name; + std::string function_name; + std::shared_ptr stack_trace; + }; + + // Contains information about uncompilable node inside a function body. + struct UncompilableNodeInfo { + std::string name; + // A list representing a stacktrace from the highest level node in + // increasing call depth to immediate node that fails the + // compilability checker. + std::vector stack_trace; + std::string uncompilable_reason; + }; + + // Aggregates information about what kinds of ops are allowed. + struct OperationFilter { // TODO(lzr): Add AllowEverything() helper. + // Whether resource variable ops are allowed are allowed in callees. We do + // not allow resource variable ops in called functions (either as direct TF + // calls or as higher order control flow ops) because we do not yet model + // their memory effects in jit/resource_operation_safety_analysis. + bool allow_resource_ops_in_called_functions = false; + + // Whether Stack operations are allowed. We avoid auto-clustering Stack + // operations in general because we do not support snapshotting them. + // + // TODO(b/112837194): This restriction can be lifted with some work. + bool allow_stack_ops = false; + + // Whether TensorArray operations are allowed. We avoid auto-clustering + // TensorArray operations in general because we do not support snapshotting + // them. + // + // TODO(b/112837194): This restriction can be lifted with some work. + bool allow_tensor_array_ops = false; + + // Whether stateful RNG ops are allowed. XLA's RNG does not have the same + // seeding behavior as TensorFlow's RNG (b/34749654). So we avoid + // auto-clustering stateful RNG ops. + bool allow_stateful_rng_ops = false; + + // TODO(b/118970344): Whether ControlTrigger ops are allowed. It is unsound + // to cluster ControlTrigger because of how we use deadness analysis. + bool allow_control_trigger = false; + + // Whether it is okay to "cluster" Assert and CheckNumerics by simply + // removing them (they're not removed during clustering, but their + // XlaOpKernel is a no-op kernel). We avoid auto-clustering these ops so + // that the user is not surprised when XLA is implicitly enabled. If the + // user explicitly specifies to use XLA, it is fine to resort to a dummy + // implementation. Currently Assert and CheckNumerics ops have dummy XLA + // implementations. + bool allow_eliding_assert_and_checknumerics_ops = false; + + // Whether ops that produce or consume DT_VARIANT values are allowed. We + // don't auto-cluster these ops because we don't yet support live-in or + // live-out DT_VARIANT values. + bool allow_ops_producing_or_consuming_variant = false; + + // Whether ops known to be slow on XLA-GPU should be considered compilable. + bool allow_slow_ops = false; + + // Whether ops known to have numerical accuracy issues should be considered + // compilable.. + bool allow_inaccurate_ops = false; + + // Require the function to be always compilable, regardless whether some + // control flow branches might be dead for a given input. + bool require_always_compilable = false; + + // Whether string constants are compilable. + bool allow_string_consts = true; + + // Whether to allow the compilation of CollectiveReduceV2Op. + bool allow_collective_reduce_v2 = true; + + // Whether to allow the compilation of WhereOp. + bool allow_where_op = true; + + // Whether to allow the compilation of UniqueOp. Compilation of the UniqueOp + // generates output with bounded dynamic shape that may cause failures with + // auto clustering. + // TODO(b/209813421): Enable tf.unique during + // autoclustering once all failures are rfixed. + bool allow_unique_op = true; + + // Whether ops that are marked as outside compiled are always considered + // compilable. + // TODO(b/191502757): Make this behavior true by default and remove this + // option once inference converter supports outside compilation. + bool allow_outside_compiled = false; + }; + + RecursiveCompilabilityChecker(OperationFilter op_filter, + DeviceType jit_device_type) + : op_filter_(std::move(op_filter)), + jit_device_type_(std::move(jit_device_type)) {} + + using UncompilableNodesMap = + std::map>>; + + // Returns a map where the key is the function identifier(short debug + // string) of the function encapsulating the uncompilable nodes, and the + // value is a pair of NameAttrList of the function and a vector of + // uncompilable node info. When uncompilable node is not inside any + // function call nodes, then key is a ShortDebugString() of an empty + // NameAttrList. + // + // Also, when `node` is inside a function body, users can set + // `node_stack_trace` to provide an additional context for `node`'s + // placement within the outer most graph. + UncompilableNodesMap FindUncompilableNodes( + const Node& node, FunctionLibraryRuntime* lib_runtime, + const std::vector* node_stack_trace = nullptr) const; + + // Returns true if `node` can be compiled by XLA. + bool IsCompilableNode(const Node& node, + FunctionLibraryRuntime* lib_runtime) const { + std::vector stack_trace; + stack_trace.emplace_back(StackFrameView{node.name(), ""}); + return IsCompilableNode(node, lib_runtime, &stack_trace); + } + + // Returns true if XLA supports this Op, but we don't want to cluster it (ie: + // due to performance or correctness concerns). + bool OpIsInaccurate(const Node& node) const; + bool OpIsSlow(const Node& node) const; + + private: + struct StackFrameView { + absl::string_view name; + absl::string_view function_name; + std::shared_ptr stack_trace; + }; + + bool IsCompilableNode( + const Node& node, FunctionLibraryRuntime* lib_runtime, + std::vector* stack_trace, + NameAttrList* encapsulating_function = nullptr, + UncompilableNodesMap* uncompilable_nodes = nullptr) const; + bool IsCompilableCall( + const NodeDef& call_def, FunctionLibraryRuntime* lib_runtime, + std::vector* stack_trace, + NameAttrList* encapsulating_function = nullptr, + UncompilableNodesMap* uncompilable_nodes = nullptr) const; + bool IsCompilableIf(const Node& if_node, FunctionLibraryRuntime* lib_runtime, + std::vector* stack_trace, + NameAttrList* encapsulating_function, + UncompilableNodesMap* uncompilable_nodes) const; + bool IsCompilableWhile(const Node& while_node, + FunctionLibraryRuntime* lib_runtime, + std::vector* stack_trace, + NameAttrList* encapsulating_function, + UncompilableNodesMap* uncompilable_nodes) const; + + // Tests whether 'case_node' is compilable. Every operator in all branches + // must be compilable. + bool IsCompilableCase(const Node& case_node, + FunctionLibraryRuntime* lib_runtime, + std::vector* stack_trace, + NameAttrList* encapsulating_function, + UncompilableNodesMap* uncompilable_nodes) const; + + // Returns compilability of node def retrieved from `node`'s attribute with + // name `attr_name`. + bool ExtractNodeDefAndCheckCompilability( + const Node& node, const std::string& attr_name, + const std::string& call_name, NameAttrList* encapsulating_function, + FunctionLibraryRuntime* lib_runtime, + std::vector* stack_trace, + UncompilableNodesMap* uncompilable_nodes) const; + + bool IsStackOp(const Node& node) const { + const XlaResourceOpInfo* op_info = + GetResourceOpInfoForOp(node.type_string()); + return op_info && op_info->resource_kind() == XlaResourceKind::kStack; + } + + bool IsTensorArrayOp(const Node& node) const { + const XlaResourceOpInfo* op_info = + GetResourceOpInfoForOp(node.type_string()); + return op_info && op_info->resource_kind() == XlaResourceKind::kTensorArray; + } + + bool IsAssertOrCheckNumerics(absl::string_view op_name) const { + return op_name == "Assert" || op_name == "CheckNumerics"; + } + + bool IsStatefulRandomOp(absl::string_view op_name) const { + return op_name == "RandomUniform" || op_name == "RandomShuffle" || + op_name == "RandomUniformInt" || op_name == "RandomStandardNormal" || + op_name == "TruncatedNormal" || op_name == "Multinomial"; + } + + bool OpProducesOrConsumesVariant(const Node& node) const { + auto is_variant = [](DataType dtype) { return dtype == DT_VARIANT; }; + return absl::c_any_of(node.input_types(), is_variant) || + absl::c_any_of(node.output_types(), is_variant); + } + + bool HasXLAKernel(const Node& node, + std::string* uncompilable_reason = nullptr) const; + + static void MaybeMarkUncompilableNode( + const absl::string_view reason, + const std::vector& stack_trace, + NameAttrList* encapsulating_function, + UncompilableNodesMap* uncompilable_nodes_map); + + // Make sure we don't recurse infinitely on recursive functions. + const size_t kMaxRecursionDepth = 50; + + const OperationFilter op_filter_; + const DeviceType jit_device_type_; +}; + +RecursiveCompilabilityChecker::OperationFilter CreateOperationFilter( + const XlaOpRegistry::DeviceRegistration& registration); + +// Given a FunctionLibraryRuntime and a `function`, returns this function's body +// in `fbody` as well as the indices of its constant and resource arguments. +// `fbody` is owned by `flr`. +// `constant_arg_indices` and `resource_arg_indices` should be empty vector. +// They are sorted in ascending order on this function's return. +absl::Status GetBodyAndConstantsAndResources( + FunctionLibraryRuntime* flr, const NameAttrList& function, + const FunctionBody** fbody, std::vector* constant_arg_indices, + std::vector* resource_arg_indices); + +// Given a NodeDef `node_def` returns true iff `node_def` has kXlaCompileAttr +// set. +bool CanCreateXlaKernel(const NodeDef& node_def); + +// Returns memory types for the input. +// `constant_arg_indices` and `resource_arg_indices` are sorted arrays of +// indices corresponding to constant and resource arguments respectively. +// +// One might wonder, about the case where a compile-time constant argument +// (which must be in host memory) is also used as an input into an op, +// e.g. `Add`, that expects its inputs in device memory. Here is how it +// works now. +// First, what do we mean by "op expects an input in XYZ memory"? +// There are two types of "ops" here: the tf2xla kernel and the HLO +// computation it builds. The tf2xla kernel needs to retrieve the actual +// numeric value of the compile-time constant tensors, so it really expects +// them to be on in host memory. However, for other inputs, it refers to them +// using xla::ComputationDataHandle, which is just a symbolic handle that +// xla::ComputationBuilder assigns. How does this handle gets assigned for +// constant arguments? Even constant arguments get an _Arg node in the graph +// instantiated for Function compilation. The tf2xla kernel for constant _Arg +// nodes takes the constant value, converts it to XlaLiteral, and feeds it +// to xla::ComputationBuilder.ConstantLiteral, which returns the handle. This +// constant XlaLiteral is included in the HLO graph, and subsequently, in +// the actual executable, which is copied to the device before being +// executed. Thus, when this executable runs, the constant is available in +// device memory. +tensorflow::MemoryTypeVector GetInputMemoryTypes( + const tensorflow::FunctionBody* fbody, + absl::Span constant_arg_indices, + absl::Span resource_arg_indices); + +// Returns output memory types. +// +// XlaLaunch kernel keeps all outputs (including constants, which it copies), +// in device memory except for resources. +tensorflow::MemoryTypeVector GetOutputMemoryTypes( + const tensorflow::FunctionBody* fbody); + +// Check whether graph can trigger XLA compilation. +bool CanTriggerXlaCompilation(const GraphDef& graph); + +// Returns true iff the node can trigger XLA compilation. +bool NodeCanTriggerXlaCompilation(const NodeDef& node); + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_COMPILABILITY_CHECK_UTIL_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/compilability_check_util_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/compilability_check_util_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..0ab275ae87ddbaa81c292c4c089e13804de3f7d9 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/compilability_check_util_test.cc @@ -0,0 +1,656 @@ +/* Copyright 2019 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/compiler/jit/compilability_check_util.h" + +#include +#include +#include + +#include "absl/log/check.h" +#include "absl/strings/match.h" +#include "absl/types/span.h" +#include "tensorflow/cc/framework/ops.h" +#include "tensorflow/cc/framework/scope.h" +#include "tensorflow/cc/ops/array_ops.h" +#include "tensorflow/cc/ops/functional_ops.h" +#include "tensorflow/compiler/jit/defs.h" +#include "tensorflow/compiler/tf2xla/xla_op_kernel.h" +#include "tensorflow/compiler/tf2xla/xla_op_registry.h" +#include "xla/tsl/lib/core/status_test_util.h" +#include "tensorflow/core/common_runtime/graph_def_builder_util.h" +#include "tensorflow/core/framework/attr_value.pb.h" +#include "tensorflow/core/framework/function.h" +#include "tensorflow/core/framework/node_def_util.h" +#include "tensorflow/core/framework/op.h" +#include "tensorflow/core/framework/op_kernel.h" +#include "tensorflow/core/framework/types.h" +#include "tensorflow/core/graph/graph_def_builder.h" +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/platform/test.h" +#include "tensorflow/core/public/version.h" + +namespace tensorflow { +namespace { + +AttrValue FuncListAttr(const absl::Span names) { + AttrValue attr; + for (const char* name : names) { + attr.mutable_list()->add_func()->set_name(name); + } + return attr; +} + +constexpr char kFunctionalIfNodeName[] = "If"; +constexpr char kFunctionalCaseNodeName[] = "Case"; +constexpr char kFunctionalWhileNodeName[] = "While"; +constexpr char kCompilableFunctionName[] = "CompilableFn"; +constexpr char kCompilableFunctionNodeName[] = "n_c"; +constexpr char kUncompilableFunctionName[] = "UncompilableFn"; +constexpr char kUncompilableFunctionNodeName[] = "n_c_uncompilable"; +constexpr char kUncompilableFunctionTwoName[] = "UncompilableFnTwo"; +constexpr char kUncompilableFunctionNodeTwoName[] = "n_d_uncompilable"; +constexpr char kNonMaxSuppressionNodeName[] = "NonMaxSuppression"; + +// A dummy OpKernel for testing. +class DummyCompilableOp : public XlaOpKernel { + public: + explicit DummyCompilableOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {} + void Compile(XlaOpKernelContext* ctx) override { + ctx->SetOutput(0, ctx->Input(0)); + } +}; + +// Register the DummyCompilableOp kernel for CPU. +REGISTER_OP("InputFloatOp").Output("o: float"); +REGISTER_OP("InputInt32Op").Output("o: int32"); +REGISTER_OP("CompilableOp").Input("i: float").Output("o: float"); +REGISTER_XLA_OP(Name("CompilableOp").Device(DEVICE_CPU_XLA_JIT), + DummyCompilableOp); + +// Dummy op that is uncompilable in CPU. +REGISTER_OP("MissingKernel").Input("i: float").Output("o: float"); + +class CompilabilityCheckUtilTest : public ::testing::Test { + protected: + void SetUp() override { + XlaOpRegistry::RegisterCompilationKernels(); + + op_filter_.allow_resource_ops_in_called_functions = false; + op_filter_.allow_stack_ops = false; + op_filter_.allow_tensor_array_ops = false; + op_filter_.allow_stateful_rng_ops = false; + op_filter_.allow_control_trigger = false; + op_filter_.allow_eliding_assert_and_checknumerics_ops = false; + op_filter_.allow_ops_producing_or_consuming_variant = false; + op_filter_.allow_inaccurate_ops = false; + op_filter_.allow_slow_ops = false; + op_filter_.allow_outside_compiled = false; + + checker_ = CreateCompilabilityChecker(); + } + + std::unique_ptr CreateCompilabilityChecker() { + return std::make_unique(op_filter_, + device_type_); + } + + FunctionLibraryRuntime* GetFunctionLibraryRuntime() { + OptimizerOptions opts; + pflr_ = std::make_unique( + nullptr, Env::Default(), /*config=*/nullptr, TF_GRAPH_DEF_VERSION, + flib_def_.get(), opts); + + return pflr_->GetFLR(ProcessFunctionLibraryRuntime::kDefaultFLRDevice); + } + + RecursiveCompilabilityChecker::OperationFilter op_filter_; + DeviceType device_type_ = DeviceType(DEVICE_CPU_XLA_JIT); + std::unique_ptr func_library_ = + std::make_unique(); + std::unique_ptr flib_def_ = + std::make_unique(OpRegistry::Global(), + *func_library_); + std::unique_ptr checker_; + std::unique_ptr pflr_; +}; + +TEST_F(CompilabilityCheckUtilTest, CheckNonFunctionalNodes) { + GraphDefBuilder builder(GraphDefBuilder::kFailImmediately); + auto opts = builder.opts(); + Node* const0 = ops::SourceOp("InputFloatOp", opts); + Node* compilable_op = ops::UnaryOp("CompilableOp", const0, opts); + Node* uncompilable_op = ops::UnaryOp("MissingKernel", compilable_op, opts); + GraphDef graph_def; + TF_EXPECT_OK(builder.ToGraphDef(&graph_def)); + + auto* flib_runtime = GetFunctionLibraryRuntime(); + // Source node is not compilable. + EXPECT_FALSE(checker_->IsCompilableNode(*const0, flib_runtime)); + + EXPECT_TRUE(checker_->IsCompilableNode(*compilable_op, flib_runtime)); + + // Uncompilable as we are only checking compilability in CPU device type. + EXPECT_FALSE(checker_->IsCompilableNode(*uncompilable_op, flib_runtime)); + + const auto uncompilable_nodes = + checker_->FindUncompilableNodes(*uncompilable_op, flib_runtime); + ASSERT_EQ(1, uncompilable_nodes.size()); + auto node_info_it = + uncompilable_nodes.find(NameAttrList().ShortDebugString()); + ASSERT_NE(uncompilable_nodes.end(), node_info_it); + const auto& uncompilable_nodes_inside_function = node_info_it->second.second; + ASSERT_EQ(1, uncompilable_nodes_inside_function.size()); + const auto& uncompilable_node_info = uncompilable_nodes_inside_function.at(0); + EXPECT_TRUE(absl::StrContains(uncompilable_node_info.uncompilable_reason, + "unsupported op")); + ASSERT_EQ(1, uncompilable_node_info.stack_trace.size()); + ASSERT_EQ("", uncompilable_node_info.stack_trace.at(0).function_name); +} + +TEST_F(CompilabilityCheckUtilTest, CheckOutsideCompiledNode) { + GraphDefBuilder builder(GraphDefBuilder::kFailImmediately); + auto opts = builder.opts(); + Node* const0 = ops::SourceOp("InputFloatOp", opts); + Node* uncompilable_op = ops::UnaryOp("MissingKernel", const0, opts); + uncompilable_op->AddAttr("_xla_outside_compilation", "0"); + GraphDef graph_def; + TF_EXPECT_OK(builder.ToGraphDef(&graph_def)); + + auto* flib_runtime = GetFunctionLibraryRuntime(); + + // Outside compiled ops are considered by default.. + EXPECT_FALSE(checker_->IsCompilableNode(*uncompilable_op, flib_runtime)); + + const auto uncompilable_nodes = + checker_->FindUncompilableNodes(*uncompilable_op, flib_runtime); + ASSERT_EQ(1, uncompilable_nodes.size()); + + op_filter_.allow_outside_compiled = true; + checker_ = CreateCompilabilityChecker(); + // With filter option outside compiled ops are ignored and considered + // compilable. + EXPECT_TRUE(checker_->IsCompilableNode(*uncompilable_op, flib_runtime)); + + const auto uncompilable_nodes2 = + checker_->FindUncompilableNodes(*uncompilable_op, flib_runtime); + ASSERT_EQ(0, uncompilable_nodes2.size()); +} + +TEST_F(CompilabilityCheckUtilTest, CheckSimpleFunctionNode) { + FunctionDefLibrary flib; + *flib.add_function() = FunctionDefHelper::Define( + /*Function*/ kUncompilableFunctionName, + /*Inputs*/ {"n_a:float"}, + /*Outputs*/ {"n_c_uncompilable:float"}, + /*Attributes*/ {}, + // Node info + {{{kUncompilableFunctionNodeName}, "MissingKernel", {"n_a"}}}); + flib_def_.reset(new FunctionLibraryDefinition(OpRegistry::Global(), flib)); + + GraphDefBuilder builder(GraphDefBuilder::kFailImmediately, flib_def_.get()); + std::unique_ptr graph(new Graph(flib_def_.get())); + Node* const0 = ops::SourceOp("InputFloatOp", builder.opts()); + Node* functional_node = ops::UnaryOp(kUncompilableFunctionName, const0, + builder.opts().WithName("D")); + TF_EXPECT_OK(GraphDefBuilderToGraph(builder, graph.get())); + + auto* flib_runtime = GetFunctionLibraryRuntime(); + EXPECT_FALSE(checker_->IsCompilableNode(*functional_node, flib_runtime)); + const auto uncompilable_nodes = + checker_->FindUncompilableNodes(*functional_node, flib_runtime); + + EXPECT_EQ(1, uncompilable_nodes.size()); + NameAttrList function; + function.set_name(kUncompilableFunctionName); + const auto node_info_it = + uncompilable_nodes.find(function.ShortDebugString()); + ASSERT_NE(uncompilable_nodes.end(), node_info_it); + const auto& uncompilable_node_list = node_info_it->second.second; + ASSERT_EQ(1, uncompilable_node_list.size()); + const auto& node_info = uncompilable_node_list.at(0); + const auto& node_stack = node_info.stack_trace; + ASSERT_EQ(2, node_stack.size()); + EXPECT_EQ("D", node_stack.at(0).name); + EXPECT_EQ(kUncompilableFunctionNodeName, node_stack.at(1).name); + EXPECT_EQ(kUncompilableFunctionNodeName, node_info.name); + EXPECT_TRUE( + absl::StrContains(node_info.uncompilable_reason, "unsupported op")); +} + +TEST_F(CompilabilityCheckUtilTest, CheckFunctionalWhileNode) { + FunctionDefLibrary flib; + *flib.add_function() = FunctionDefHelper::Define( + /*Function*/ kCompilableFunctionName, + /*Inputs*/ {"n_a:float", "n_b:float"}, + /*Outputs*/ {"n_c:float"}, + /*Attribute*/ {}, + // Node info + {{{kCompilableFunctionNodeName}, + "Add", + {"n_a", "n_b"}, + {{"T", DT_FLOAT}}}}); + *flib.add_function() = FunctionDefHelper::Define( + /*Function*/ kUncompilableFunctionName, + /*Inputs*/ {"n_a:float"}, + /*Outputs*/ {"n_c_uncompilable:float"}, + /*Attributes*/ {}, + // Node info + {{{kUncompilableFunctionNodeName}, "MissingKernel", {"n_a"}}}); + + flib_def_.reset(new FunctionLibraryDefinition(OpRegistry::Global(), flib)); + GraphDefBuilder builder(GraphDefBuilder::kFailImmediately, flib_def_.get()); + + Node* const0 = ops::SourceOp("InputFloatOp", builder.opts()); + Node* input_node = ops::UnaryOp("CompilableOp", const0, builder.opts()); + + NameAttrList compilable; + compilable.set_name(kCompilableFunctionName); + NameAttrList uncompilable; + uncompilable.set_name(kUncompilableFunctionName); + + NodeBuilder while_builder(kFunctionalWhileNodeName, "While", + builder.opts().op_registry()); + while_builder.Input({input_node, input_node}) + .Attr("cond", compilable) + .Attr("body", uncompilable); + builder.opts().FinalizeBuilder(&while_builder); + + GraphDef graph_def; + TF_EXPECT_OK(builder.ToGraphDef(&graph_def)); + std::unique_ptr graph(new Graph(flib_def_.get())); + CHECK_OK(GraphDefBuilderToGraph(builder, graph.get())); + + auto while_node_it = std::find_if( + graph->nodes().begin(), graph->nodes().end(), + [&](const Node* n) { return n->name() == kFunctionalWhileNodeName; }); + EXPECT_NE(while_node_it, graph->nodes().end()); + + auto* flib_runtime = GetFunctionLibraryRuntime(); + + EXPECT_FALSE(checker_->IsCompilableNode(**while_node_it, flib_runtime)); + const auto uncompilable_nodes = + checker_->FindUncompilableNodes(**while_node_it, flib_runtime); + ASSERT_EQ(1, uncompilable_nodes.size()); + + NameAttrList function; + function.set_name(kUncompilableFunctionName); + const auto node_info_it = + uncompilable_nodes.find(function.ShortDebugString()); + ASSERT_NE(uncompilable_nodes.end(), node_info_it); + const auto& uncompilable_node_list = node_info_it->second.second; + ASSERT_EQ(1, uncompilable_node_list.size()); + const auto& node_info = uncompilable_node_list.at(0); + + const auto& node_stack = node_info.stack_trace; + ASSERT_EQ(2, node_stack.size()); + const auto& stacktrace_first_node_info = node_stack.at(0); + EXPECT_EQ(kFunctionalWhileNodeName, stacktrace_first_node_info.name); + EXPECT_EQ("", stacktrace_first_node_info.function_name); + + const auto& stacktrace_second_node_info = node_stack.at(1); + EXPECT_EQ(kUncompilableFunctionNodeName, stacktrace_second_node_info.name); + EXPECT_EQ(kUncompilableFunctionName, + stacktrace_second_node_info.function_name); + + EXPECT_EQ(kUncompilableFunctionNodeName, node_info.name); + EXPECT_TRUE( + absl::StrContains(node_info.uncompilable_reason, "unsupported op")); +} + +TEST_F(CompilabilityCheckUtilTest, CheckFunctionalIfNode) { + FunctionDefLibrary flib; + *flib.add_function() = FunctionDefHelper::Define( + /*Function*/ kUncompilableFunctionName, + /*Inputs*/ {"n_a:float"}, + /*Outputs*/ {"n_c_uncompilable:float"}, + /*Attributes*/ {}, + // Node info + {{{kUncompilableFunctionNodeName}, "MissingKernel", {"n_a"}}}); + *flib.add_function() = FunctionDefHelper::Define( + /*Function*/ kUncompilableFunctionTwoName, + /*Inputs*/ {"n_a:float"}, + /*Outputs*/ {"n_d_uncompilable:float"}, + /*Attribute*/ {}, + // Node info + {{{kUncompilableFunctionNodeTwoName}, "MissingKernel", {"n_a"}}}); + NameAttrList uncompilable_fn1_attr; + uncompilable_fn1_attr.set_name(kUncompilableFunctionName); + NameAttrList uncompilable_fn2_attr; + uncompilable_fn2_attr.set_name(kUncompilableFunctionTwoName); + + Scope root = Scope::NewRootScope().ExitOnError(); + TF_ASSERT_OK(root.graph()->AddFunctionLibrary(flib)); + auto predicate = ops::Placeholder(root.WithOpName("pred"), DT_BOOL); + auto placeholder = ops::Placeholder(root.WithOpName("A"), DT_INT32); + std::vector if_inputs( + {NodeBuilder::NodeOut(placeholder.node())}); + Node* if_node; + TF_ASSERT_OK( + NodeBuilder(kFunctionalIfNodeName, "If", &root.graph()->flib_def()) + .Input(predicate.node()) + .Input(if_inputs) + .Attr("then_branch", uncompilable_fn1_attr) + .Attr("else_branch", uncompilable_fn2_attr) + .Attr("Tout", {DT_INT32}) + .Finalize(root.graph(), &if_node)); + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_ASSERT_OK(root.ToGraph(graph.get())); + + flib_def_.reset(new FunctionLibraryDefinition(OpRegistry::Global(), flib)); + + auto if_node_it = std::find_if( + graph->nodes().begin(), graph->nodes().end(), + [&](const Node* n) { return n->name() == kFunctionalIfNodeName; }); + EXPECT_NE(if_node_it, graph->nodes().end()); + auto* flib_runtime = GetFunctionLibraryRuntime(); + + EXPECT_FALSE(checker_->IsCompilableNode(**if_node_it, flib_runtime)); + const auto uncompilable_nodes = + checker_->FindUncompilableNodes(**if_node_it, flib_runtime); + ASSERT_EQ(2, uncompilable_nodes.size()); + + NameAttrList function_one; + function_one.set_name(kUncompilableFunctionName); + auto it = uncompilable_nodes.find(function_one.ShortDebugString()); + ASSERT_NE(uncompilable_nodes.end(), it); + + const auto& uncompilable_node_list = it->second.second; + ASSERT_EQ(1, uncompilable_node_list.size()); + const auto& uncompilable_node_one = uncompilable_node_list.at(0); + const auto& node_one_stack = uncompilable_node_one.stack_trace; + + ASSERT_EQ(2, node_one_stack.size()); + const auto& node_one_stacktrace_first_node = node_one_stack.at(0); + EXPECT_EQ(kFunctionalIfNodeName, node_one_stacktrace_first_node.name); + EXPECT_EQ("", node_one_stacktrace_first_node.function_name); + + const auto& stacktrace_second_node_info = node_one_stack.at(1); + EXPECT_EQ(kUncompilableFunctionNodeName, stacktrace_second_node_info.name); + EXPECT_EQ(kUncompilableFunctionName, + stacktrace_second_node_info.function_name); + + EXPECT_EQ(kUncompilableFunctionNodeName, uncompilable_node_one.name); + EXPECT_TRUE(absl::StrContains(uncompilable_node_one.uncompilable_reason, + "unsupported op")); + + NameAttrList function_two; + function_two.set_name(kUncompilableFunctionTwoName); + it = uncompilable_nodes.find(function_two.ShortDebugString()); + ASSERT_NE(uncompilable_nodes.end(), it); + + const auto& uncompilable_node_two_list = it->second.second; + ASSERT_EQ(1, uncompilable_node_two_list.size()); + const auto& uncompilable_node_two = uncompilable_node_two_list.at(0); + const auto& node_two_stack = uncompilable_node_two.stack_trace; + ASSERT_EQ(2, node_two_stack.size()); + const auto& node_two_stacktrace_first_node = node_two_stack.at(0); + EXPECT_EQ(kFunctionalIfNodeName, node_two_stacktrace_first_node.name); + EXPECT_EQ("", node_two_stacktrace_first_node.function_name); + + const auto& node_two_stacktrace_second_node = node_two_stack.at(1); + EXPECT_EQ(kUncompilableFunctionNodeTwoName, + node_two_stacktrace_second_node.name); + EXPECT_EQ(kUncompilableFunctionTwoName, + node_two_stacktrace_second_node.function_name); + + EXPECT_EQ(kUncompilableFunctionNodeTwoName, uncompilable_node_two.name); + EXPECT_TRUE(absl::StrContains(uncompilable_node_one.uncompilable_reason, + "unsupported op")); +} + +TEST_F(CompilabilityCheckUtilTest, CheckFunctionalCaseNode) { + FunctionDefLibrary flib; + *flib.add_function() = FunctionDefHelper::Define( + /*Function*/ kUncompilableFunctionName, + /*Inputs*/ {"n_a:float"}, + /*Outputs*/ {"n_c_uncompilable:float"}, + /*Attributes*/ {}, + // Node info + {{{kUncompilableFunctionNodeName}, "MissingKernel", {"n_a"}}}); + *flib.add_function() = FunctionDefHelper::Define( + /*Function*/ kUncompilableFunctionTwoName, + /*Inputs*/ {"n_a:float"}, + /*Outputs*/ {"n_d_uncompilable:float"}, + /*Attribute*/ {}, + // Node info + {{{kUncompilableFunctionNodeTwoName}, "MissingKernel", {"n_a"}}}); + + Scope root = Scope::NewRootScope().ExitOnError(); + TF_ASSERT_OK(root.graph()->AddFunctionLibrary(flib)); + auto branch_index = ops::Placeholder(root.WithOpName("pred"), DT_INT32); + auto placeholder = ops::Placeholder(root.WithOpName("A"), DT_INT32); + std::vector inputes( + {NodeBuilder::NodeOut(placeholder.node())}); + Node* case_node; + TF_ASSERT_OK( + NodeBuilder(kFunctionalCaseNodeName, "Case", &root.graph()->flib_def()) + .Input(branch_index.node()) + .Input(inputes) + .Attr("branches", FuncListAttr({kUncompilableFunctionName, + kUncompilableFunctionTwoName})) + .Attr("Tout", {DT_INT32}) + .Finalize(root.graph(), &case_node)); + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_ASSERT_OK(root.ToGraph(graph.get())); + + flib_def_.reset(new FunctionLibraryDefinition(OpRegistry::Global(), flib)); + + auto case_node_it = std::find_if( + graph->nodes().begin(), graph->nodes().end(), + [&](const Node* n) { return n->name() == kFunctionalCaseNodeName; }); + EXPECT_NE(case_node_it, graph->nodes().end()); + auto* flib_runtime = GetFunctionLibraryRuntime(); + + op_filter_.require_always_compilable = false; + checker_ = CreateCompilabilityChecker(); + EXPECT_TRUE(checker_->IsCompilableNode(**case_node_it, flib_runtime)); + op_filter_.require_always_compilable = true; + checker_ = CreateCompilabilityChecker(); + EXPECT_FALSE(checker_->IsCompilableNode(**case_node_it, flib_runtime)); +} + +TEST_F(CompilabilityCheckUtilTest, TestCanNotTriggerXlaCompilation) { + GraphDefBuilder b(GraphDefBuilder::kFailImmediately); + Scope root = Scope::NewRootScope().ExitOnError(); + FunctionDefLibrary library; + + FunctionDef identity_func = FunctionDefHelper::Create( + "IdentityFunc", + /*in_def=*/{"x:float"}, + /*out_def=*/{"res:float"}, + /*attr_def=*/{}, + /*node_def=*/{{{"t0"}, "Identity", {"x"}, {{"T", DT_FLOAT}}}}, + /*ret_def*/ {{"res", "t0:output"}}); + + *library.add_function() = identity_func; + + Output in = ops::Placeholder(root, DT_FLOAT); + NameAttrList b_name_attr; + b_name_attr.set_name("IdentityFunc"); + ops::PartitionedCall call(root.WithOpName("call"), {in}, {DT_FLOAT}, + b_name_attr); + + GraphDef graph_def; + TF_ASSERT_OK(root.graph()->AddFunctionLibrary(library)); + TF_ASSERT_OK(root.ToGraphDef(&graph_def)); + + EXPECT_FALSE(CanTriggerXlaCompilation(graph_def)); +} + +TEST_F(CompilabilityCheckUtilTest, TestXlaOpsCanTriggerXlaCompilation) { + GraphDefBuilder b(GraphDefBuilder::kFailImmediately); + Scope root = Scope::NewRootScope().ExitOnError(); + FunctionDefLibrary library; + + FunctionDef sort_func = FunctionDefHelper::Create( + "SortFunc", + /*in_def=*/{"x:float"}, + /*out_def=*/{"res:float"}, + /*attr_def=*/{}, + /*node_def=*/{{{"t0"}, "XlaSort", {"x"}, {{"T", DT_FLOAT}}}}, + /*ret_def*/ {{"res", "t0:output"}}); + + *library.add_function() = sort_func; + + Output in = ops::Placeholder(root, DT_FLOAT); + NameAttrList b_name_attr; + b_name_attr.set_name("SortFunc"); + ops::PartitionedCall call(root.WithOpName("call"), {in}, {DT_FLOAT}, + b_name_attr); + + GraphDef graph_def; + TF_ASSERT_OK(root.graph()->AddFunctionLibrary(library)); + TF_ASSERT_OK(root.ToGraphDef(&graph_def)); + + EXPECT_TRUE(CanTriggerXlaCompilation(graph_def)); +} + +TEST_F(CompilabilityCheckUtilTest, TestCanTriggerXlaCompilation) { + GraphDefBuilder b(GraphDefBuilder::kFailImmediately); + Scope root = Scope::NewRootScope().ExitOnError(); + FunctionDefLibrary library; + + AttrValue true_attribute; + true_attribute.set_b(true); + + FunctionDef identity_func = FunctionDefHelper::Create( + "IdentityFunc", + /*in_def=*/{"x:float"}, + /*out_def=*/{"res:float"}, + /*attr_def=*/{}, + /*node_def=*/{{{"t0"}, "Identity", {"x"}, {{"T", DT_FLOAT}}}}, + /*ret_def*/ {{"res", "t0:output"}}); + + (*identity_func.mutable_attr())[kXlaMustCompileAttr] = true_attribute; + + FunctionDef call_identity = FunctionDefHelper::Create( + "CallIdentity", + /*in_def=*/{"x:float"}, + /*out_def=*/{"z:float"}, /*attr_def=*/{}, + /*node_def=*/ + {{{"func_call"}, + "PartitionedCall", + {"x"}, + {{"Tin", DataTypeSlice({DT_FLOAT})}, + {"Tout", DataTypeSlice({DT_FLOAT})}, + {"f", + FunctionDefHelper::FunctionRef("IdentityRef", {{"T", DT_FLOAT}})}, + {kXlaMustCompileAttr, true}}}}, + /*ret_def=*/{{"z", "func_call:output:0"}}); + + *library.add_function() = identity_func; + *library.add_function() = call_identity; + + Output in = ops::Placeholder(root, DT_FLOAT); + NameAttrList b_name_attr; + b_name_attr.set_name("CallIdentity"); + ops::PartitionedCall call(root.WithOpName("call"), {in}, {DT_FLOAT}, + b_name_attr); + + GraphDef graph_def; + TF_ASSERT_OK(root.graph()->AddFunctionLibrary(library)); + TF_ASSERT_OK(root.ToGraphDef(&graph_def)); + + EXPECT_TRUE(CanTriggerXlaCompilation(graph_def)); +} + +TEST_F(CompilabilityCheckUtilTest, CheckNonMaxSuppressionV3UncompilableSlowOp) { + GraphDefBuilder builder(GraphDefBuilder::kFailImmediately); + auto opts = builder.opts(); + + Node* boxes = ops::SourceOp("InputFloatOp", opts); + Node* scores = ops::SourceOp("InputFloatOp", opts); + Node* max_output_size = ops::SourceOp("InputInt32Op", opts); + Node* iou_threshold = ops::SourceOp("InputFloatOp", opts); + Node* score_threshold = ops::SourceOp("InputFloatOp", opts); + + NodeBuilder non_max_suppression_builder( + kNonMaxSuppressionNodeName, "NonMaxSuppressionV3", opts.op_registry()); + non_max_suppression_builder.Input(boxes) + .Input(scores) + .Input(max_output_size) + .Input(iou_threshold) + .Input(score_threshold) + .Attr("T", DT_FLOAT); + Node* non_max_suppression; + non_max_suppression = + builder.opts().FinalizeBuilder(&non_max_suppression_builder); + + GraphDef graph_def; + TF_EXPECT_OK(builder.ToGraphDef(&graph_def)); + auto* flib_runtime = GetFunctionLibraryRuntime(); + + EXPECT_FALSE(checker_->IsCompilableNode(*non_max_suppression, flib_runtime)); + + const auto uncompilable_nodes = + checker_->FindUncompilableNodes(*non_max_suppression, flib_runtime); + ASSERT_EQ(1, uncompilable_nodes.size()); + auto node_info_it = + uncompilable_nodes.find(NameAttrList().ShortDebugString()); + ASSERT_NE(uncompilable_nodes.end(), node_info_it); + + const auto& uncompilable_nodes_inside_function = node_info_it->second.second; + ASSERT_EQ(1, uncompilable_nodes_inside_function.size()); + const auto& uncompilable_node_info = uncompilable_nodes_inside_function.at(0); + EXPECT_TRUE(absl::StrContains(uncompilable_node_info.uncompilable_reason, + "slow operation")); +} + +TEST_F(CompilabilityCheckUtilTest, CheckNonMaxSuppressionV4UncompilableSlowOp) { + GraphDefBuilder builder(GraphDefBuilder::kFailImmediately); + auto opts = builder.opts(); + + Node* boxes = ops::SourceOp("InputFloatOp", opts); + Node* scores = ops::SourceOp("InputFloatOp", opts); + Node* max_output_size = ops::SourceOp("InputInt32Op", opts); + Node* iou_threshold = ops::SourceOp("InputFloatOp", opts); + Node* score_threshold = ops::SourceOp("InputFloatOp", opts); + + NodeBuilder non_max_suppression_v4_builder( + kNonMaxSuppressionNodeName, "NonMaxSuppressionV4", opts.op_registry()); + non_max_suppression_v4_builder.Input(boxes) + .Input(scores) + .Input(max_output_size) + .Input(iou_threshold) + .Input(score_threshold) + .Attr("T", DT_FLOAT); + Node* non_max_suppression_v4; + non_max_suppression_v4 = + builder.opts().FinalizeBuilder(&non_max_suppression_v4_builder); + + GraphDef graph_def; + TF_EXPECT_OK(builder.ToGraphDef(&graph_def)); + auto* flib_runtime = GetFunctionLibraryRuntime(); + + EXPECT_FALSE( + checker_->IsCompilableNode(*non_max_suppression_v4, flib_runtime)); + + const auto uncompilable_nodes = + checker_->FindUncompilableNodes(*non_max_suppression_v4, flib_runtime); + ASSERT_EQ(1, uncompilable_nodes.size()); + auto node_info_it = + uncompilable_nodes.find(NameAttrList().ShortDebugString()); + ASSERT_NE(uncompilable_nodes.end(), node_info_it); + + const auto& uncompilable_nodes_inside_function = node_info_it->second.second; + ASSERT_EQ(1, uncompilable_nodes_inside_function.size()); + const auto& uncompilable_node_info = uncompilable_nodes_inside_function.at(0); + EXPECT_TRUE(absl::StrContains(uncompilable_node_info.uncompilable_reason, + "slow operation")); +} + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/compilation_passes_test_main.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/compilation_passes_test_main.cc new file mode 100644 index 0000000000000000000000000000000000000000..5023e5ca82f8ced913db9bf4bcab9663db1be692 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/compilation_passes_test_main.cc @@ -0,0 +1,58 @@ +/* Copyright 2019 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 "absl/strings/match.h" +#include "absl/strings/string_view.h" +#include "tensorflow/compiler/jit/flags.h" +#include "tensorflow/core/platform/logging.h" +#include "tensorflow/core/platform/test.h" +#include "tensorflow/core/platform/test_benchmark.h" + +GTEST_API_ int main(int real_argc, char** real_argv) { + std::vector flag_list; + tensorflow::AppendMarkForCompilationPassFlags(&flag_list); + auto usage = tensorflow::Flags::Usage(real_argv[0], flag_list); + + std::vector args; + + args.reserve(real_argc + 1); + for (int i = 0; i < real_argc; i++) { + args.push_back(real_argv[i]); + } + + struct FreeDeleter { + void operator()(char* ptr) { free(ptr); } + }; + + std::unique_ptr enable_global_jit_arg( + strdup("--tf_xla_cpu_global_jit=true")); + args.push_back(enable_global_jit_arg.get()); + + std::unique_ptr reduce_min_cluster_size_arg( + strdup("--tf_xla_min_cluster_size=2")); + args.push_back(reduce_min_cluster_size_arg.get()); + + int argc = args.size(); + + if (!tensorflow::Flags::Parse(&argc, &args.front(), flag_list)) { + LOG(ERROR) << "\n" << usage; + return 2; + } + + testing::InitGoogleTest(&argc, &args.front()); + return RUN_ALL_TESTS(); +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/deadness_analysis.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/deadness_analysis.cc new file mode 100644 index 0000000000000000000000000000000000000000..a200514e0b8da6fe0ea956eb0ce2be4500e911a6 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/deadness_analysis.cc @@ -0,0 +1,1610 @@ +/* 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 "tensorflow/compiler/jit/deadness_analysis.h" + +#include "absl/algorithm/container.h" +#include "absl/container/flat_hash_map.h" +#include "absl/container/flat_hash_set.h" +#include "absl/strings/str_join.h" +#include "absl/strings/string_view.h" +#include "tensorflow/compiler/jit/deadness_analysis_internal.h" +#include "tensorflow/compiler/jit/xla_cluster_util.h" +#include "xla/status_macros.h" +#include "tensorflow/core/framework/tensor.pb.h" +#include "tensorflow/core/graph/algorithm.h" +#include "tensorflow/core/graph/control_flow.h" +#include "tensorflow/core/graph/graph_node_util.h" +#include "tensorflow/core/graph/tensor_id.h" +#include "tensorflow/core/lib/hash/hash.h" + +// ALGORITHM OVERVIEW +// ================== +// +// We map every output produced by each node in the TensorFlow graph (including +// control dependence) into an instance of the Predicate class. Instances of +// Predicate denote logical formulas and mapping a node `n` to a predicate +// `pred` implies that `n` is live whenever `pred` is true. Then we can deduce +// mismatching liveness in the inputs to node by comparing the predicate those +// inputs are mapped to. The core logic of this pass resides in creating the +// map from TensorFlow nodes to predicates. +// +// +// MAPPING NODES TO PREDICATES, MODULO CYCLES +// ------------------------------------------ +// +// If we ignore cycles for a moment, computing predicates is fairly +// straightforward. We traverse the graph in a topological order, mapping each +// node to a predicate based on the predicates its inputs are mapped to. For +// instance a Merge(X, Y) node will be mapped to OR(PredicateFor(X), +// PredicateFor(Y)). Roughtly speaking, we abstractly interpret each node on +// the "liveness" domain, where values in the domain represent if a tensor +// carries a dead signal or not. +// +// +// DEALING WITH CYCLES +// ------------------- +// +// We map Merge nodes that are the target of a backedge to AndRecurrence +// instances. An AndRecurrence with start() = S and step() = X, printed as +// {S,&,X}, *roughly* represents the infinite list of predicates +// [S,S&X,S&X&X,S&X&X, ...]. So {S,&,X} can be used to represent the predicate +// for Merge in a graph like: +// +// Init +// | +// v +// Merge <-----------+ +// | | +// v | +// Incr | +// | | +// v | +// Switch <- Cond | +// | | +// v (oidx: 1) | +// | | +// +---------------+ +// +// Where S is the predicate for Init and X is the predicate that asserts that +// Cond is true. {S,&,X} states that Merge is live on the first "iteration" iff +// S is true, live on the second iteration iff "S&X" is true, live on the third +// iteration iff "S&X&X" is true etc. There is a subtlety here, S&X&X would +// normally be equivalent to S&X which isn't quite what we want to represent. +// Instead we want {S,&,X} to denote the infinite list [S, S&X, +// S&X&X',S&X&X'&X'', ...] where X, X', X'' are predicates that assert Cond is +// true on iteration 0, 1, 2 respectively. This is made more precise in the +// comment on the AndRecurrence class. +// +// The general algorithm that deals with cycles does two topological-order +// iterations over the graph. On the first iteration it assigns a symbolic +// predicate to merge nodes with backedges. On the second iteration it tries +// to pattern match the predicates for the backedges of these merges and infer +// an AndRecurrence for the merge. In other words, we do a data flow analysis +// where the data-flow lattice has two elements, Symbolic and NonSymbolic with +// Symbolic > NonSymbolic. The lattice has height = 2 so two iterations are +// sufficient to converge. +// +// We first do an optimistic analysis and, if it does not converge, we then fall +// back to a pessimistic analysis. The optimistic analysis assigns the same +// symbolic predicate to all the merge nodes whose preceding enter nodes have +// the same frame name on the first iteration. On the second iteration, if all +// the merge nodes are pattern matched into the same AndRecurrence predicate +// instance, the optimistic assignment of the same symbolic predicate is correct +// and the analyzed result is taken. +// +// Otherwise, if the optimistic analysis fails to converge, we then obtain the +// result by falling back to the pessimistic analysis which assigns a unique +// symbolic predicate to each merge on the first iteration. We still use +// symbolic predicates for merges for which we can't pattern match on the +// backedge predicate. This is conservatively correct. + +namespace tensorflow { + +namespace { + +using tsl::StatusOr; + +// Represents a logical predicate, used as described in the algorithm overview +// above. +class Predicate { + public: + enum class Kind { kAnd, kOr, kNot, kAndRecurrence, kSymbol, kIntSymbol }; + + virtual std::string ToString() const = 0; + + // An ID assigned to the Predicate at construction time. Conceptually like a + // pointer, except that it is stable across runs. + int64_t id() const { return id_; } + + virtual absl::Span GetOperands() const = 0; + + virtual Kind kind() const = 0; + virtual ~Predicate() {} + + // Invokes func on p and on all of its operands recursively. Does not invoke + // `func` on the same Predicate instance twice. Aborts the search if `func` + // returns true. + template + static void Visit(Predicate* p, const FunctionTy& func); + + protected: + explicit Predicate(int64_t id) : id_(id) {} + + private: + const int64_t id_; + + Predicate(const Predicate&) = delete; + void operator=(const Predicate&) = delete; +}; + +// Represents a logical conjunction of a set of predicates. +class AndPredicate : public Predicate { + public: + explicit AndPredicate(int64_t id, std::vector operands) + : Predicate(id), operands_(std::move(operands)) {} + + std::string ToString() const override { + if (operands().empty()) { + return "#true"; + } + + std::vector operands_str; + std::transform(operands().begin(), operands().end(), + std::back_inserter(operands_str), + [](Predicate* pred) { return pred->ToString(); }); + + return absl::StrCat("(", absl::StrJoin(operands_str, " & "), ")"); + } + + Kind kind() const override { return Kind::kAnd; } + + absl::Span GetOperands() const override { + return operands_; + } + absl::Span operands() const { return operands_; } + + private: + std::vector operands_; +}; + +// Represents a logical disjunction of a set of predicates. +class OrPredicate : public Predicate { + public: + explicit OrPredicate(int64_t id, std::vector operands) + : Predicate(id), operands_(std::move(operands)) {} + + std::string ToString() const override { + if (operands().empty()) { + return "#false"; + } + + std::vector operands_str; + std::transform(operands().begin(), operands().end(), + std::back_inserter(operands_str), + [](Predicate* pred) { return pred->ToString(); }); + + return absl::StrCat("(", absl::StrJoin(operands_str, " | "), ")"); + } + + Kind kind() const override { return Kind::kOr; } + absl::Span GetOperands() const override { + return operands_; + } + absl::Span operands() const { return operands_; } + + private: + std::vector operands_; +}; + +// Represents a logical negation of a set of predicates. +class NotPredicate : public Predicate { + public: + explicit NotPredicate(int64_t id, Predicate* operand) + : Predicate(id), operands_({operand}) {} + + std::string ToString() const override { + return absl::StrCat("~", operand()->ToString()); + } + + Kind kind() const override { return Kind::kNot; } + Predicate* operand() const { return operands_[0]; } + absl::Span GetOperands() const override { + return operands_; + } + + private: + std::array operands_; +}; + +// Represents the liveness of an induction variable. For users inside the loop +// this represents the "current" liveness of the induction variable. For users +// outside the loop it represents the "last" liveness of the induction variable. +// +// More concretely, an and recurrence {S,&,X} represents the liveness of V +// in the following graph: +// +// V = Merge(S', V_NextIt) +// V = Op(V, X') +// V_NextIt = NextIteration(V) +// +// where Predicate(S') = S and Predicate(X') = X. +// +// `X` may contain symbolic predicates and the operations corresponding to these +// symbolic predicates are either in frame `loop` or outside it. The symbols +// that are inside frame `loop` are loop variant (i.e. can have different +// liveness in each loop iteration) and the symbols that are outside frame +// `loop` are loop invariant (i.e. have the same liveness across all +// iterations). +class AndRecurrencePredicate : public Predicate { + public: + explicit AndRecurrencePredicate(int64_t id, Predicate* start, Predicate* step, + std::vector frame) + : Predicate(id), operands_({start, step}), frame_(std::move(frame)) {} + + Predicate* start() const { return operands_[0]; } + Predicate* step() const { return operands_[1]; } + absl::Span frame() const { return frame_; } + + std::string ToString() const override { + return absl::StrCat("{", start()->ToString(), ",&,", step()->ToString(), + "}<", absl::StrJoin(frame(), ";"), ">"); + } + + Kind kind() const override { return Kind::kAndRecurrence; } + + absl::Span GetOperands() const override { + return operands_; + } + + private: + std::array operands_; + std::vector frame_; +}; + +// Represents an uninterpreted symbol in a logical predicate. +// +// Two predicates are equivalent iff they are equivalent for all assignments to +// the symbols contained in them, i.e. predicates are forall qualified over +// symbols. +class SymbolPredicate : public Predicate { + public: + explicit SymbolPredicate(int64_t id, TensorId tensor_id, bool must_be_true) + : Predicate(id), + tensor_id_(std::move(tensor_id)), + must_be_true_(must_be_true) {} + + std::string ToString() const override { + return must_be_true() ? absl::StrCat("*", tensor_id_.ToString()) + : tensor_id_.ToString(); + } + + Kind kind() const override { return Kind::kSymbol; } + absl::Span GetOperands() const override { return {}; } + + // If `must_be_true()` is true this SymbolPredicate represents the proposition + // "tensor_id() is live and evaluates to true". + // + // If `must_be_true()` is false then this SymbolPredicate represents the + // proposition "tensor_id() is live (and may evaluate to any value)" + TensorId tensor_id() const { return tensor_id_; } + bool must_be_true() const { return must_be_true_; } + + private: + TensorId tensor_id_; + bool must_be_true_; +}; + +// Represents an uninterpreted symbol in a logical predicate. +// +// Two predicates are equivalent iff they are equivalent for all assignments to +// the symbols contained in them, i.e. predicates are forall qualified over +// symbols. +class IntSymbolPredicate : public Predicate { + public: + explicit IntSymbolPredicate(int64_t id, TensorId tensor_id, + std::optional must_have_value) + : Predicate(id), + tensor_id_(std::move(tensor_id)), + must_have_value_(must_have_value) {} + + std::string ToString() const override { + return must_have_value().has_value() + ? absl::StrCat(tensor_id_.ToString(), "=", *must_have_value_) + : tensor_id_.ToString(); + } + + Kind kind() const override { return Kind::kIntSymbol; } + absl::Span GetOperands() const override { return {}; } + + // If `must_have_value().has_value()` is true, then this IntSymbolPredicate + // represents the proposition "tensor_id() is live and evaluates to + // `*must_have_value()`". + // + // If `must_have_value().has_value()` is false, then this IntSymbolPredicate + // represents the proposition "tensor_id() is live (and may evaluate to any + // value)". + TensorId tensor_id() const { return tensor_id_; } + const std::optional& must_have_value() const { return must_have_value_; } + + private: + TensorId tensor_id_; + std::optional must_have_value_; +}; + +template +/*static*/ void Predicate::Visit(Predicate* p, const FunctionTy& func) { + absl::flat_hash_set visited; + std::vector stack; + + stack.push_back(p); + visited.insert(p); + + while (!stack.empty()) { + Predicate* current = stack.back(); + stack.pop_back(); + bool done = func(current); + if (done) { + return; + } + for (Predicate* op : current->GetOperands()) { + if (visited.insert(op).second) { + stack.push_back(op); + } + } + } +} + +// Creates and owns Predicate instances. Simplifies predicates as it creates +// them. +class PredicateFactory { + public: + Predicate* MakeAndPredicate(absl::Span operands) { + return MakeAndOrImpl(operands, /*is_and=*/true); + } + + Predicate* MakeOrPredicate(absl::Span operands) { + return MakeAndOrImpl(operands, /*is_and=*/false); + } + + Predicate* MakeNotPredicate(Predicate* pred) { + auto it = make_not_predicate_cache_.find(pred); + if (it != make_not_predicate_cache_.end()) { + return it->second; + } + + Predicate* result = MakeNotPredicateImpl(pred); + + bool insert_successful = + make_not_predicate_cache_.insert({pred, result}).second; + (void)insert_successful; + DCHECK(insert_successful); + + return result; + } + + Predicate* MakeAndRecurrencePredicate(Predicate* start, Predicate* step, + std::vector frame) { + SignatureForAndRec signature(start, step, std::move(frame)); + auto it = interned_and_rec_instances_.find(signature); + if (it != interned_and_rec_instances_.end()) { + return it->second.get(); + } + + std::unique_ptr new_pred = Make( + std::get<0>(signature), std::get<1>(signature), std::get<2>(signature)); + Predicate* new_pred_ptr = new_pred.get(); + bool inserted = + interned_and_rec_instances_.emplace(signature, std::move(new_pred)) + .second; + (void)inserted; + DCHECK(inserted); + return new_pred_ptr; + } + + absl::Status MakeSymbolPredicate(Node* node, int output_idx, + bool must_be_true, Predicate** predicate) { + TensorId tensor_id(node->name(), output_idx); + + bool is_boolean_tensor = + BaseType(node->output_type(tensor_id.index())) == DT_BOOL; + TF_RET_CHECK(!must_be_true || is_boolean_tensor); + + if (node->type_string() == "Const" && must_be_true) { + const TensorProto* proto = nullptr; + TF_RETURN_IF_ERROR(GetNodeAttr(node->def(), "value", &proto)); + + Tensor tensor(proto->dtype()); + TF_RET_CHECK(tensor.FromProto(*proto)); + + *predicate = tensor.scalar()() ? MakeTrue() : MakeFalse(); + return absl::OkStatus(); + } + + SignatureForSymbol signature = {tensor_id, must_be_true}; + auto it = interned_symbol_instances_.find(signature); + if (it == interned_symbol_instances_.end()) { + std::unique_ptr new_pred = + Make(tensor_id, must_be_true); + Predicate* new_pred_ptr = new_pred.get(); + interned_symbol_instances_.emplace(std::move(signature), + std::move(new_pred)); + *predicate = new_pred_ptr; + } else { + *predicate = it->second.get(); + } + + return absl::OkStatus(); + } + + absl::Status MakeSymbolPredicate(Node* node, int output_idx, + std::optional must_have_value, + Predicate** predicate) { + TensorId tensor_id(node->name(), output_idx); + + TF_RET_CHECK(BaseType(node->output_type(tensor_id.index())) == DT_INT32); + + if (must_have_value.has_value() && node->type_string() == "Const") { + const TensorProto* proto = nullptr; + TF_RETURN_IF_ERROR(GetNodeAttr(node->def(), "value", &proto)); + + Tensor tensor(proto->dtype()); + TF_RET_CHECK(tensor.FromProto(*proto)); + + *predicate = tensor.scalar()() == *must_have_value ? MakeTrue() + : MakeFalse(); + return absl::OkStatus(); + } + SignatureForIntSymbol signature = {tensor_id, must_have_value}; + auto it = interned_int_symbol_instances_.find(signature); + if (it == interned_int_symbol_instances_.end()) { + std::unique_ptr new_pred = + Make(tensor_id, must_have_value); + Predicate* new_pred_ptr = new_pred.get(); + interned_int_symbol_instances_.emplace(std::move(signature), + std::move(new_pred)); + *predicate = new_pred_ptr; + } else { + *predicate = it->second.get(); + } + + return absl::OkStatus(); + } + + Predicate* MakeTrue() { return MakeAndPredicate({}); } + Predicate* MakeFalse() { return MakeOrPredicate({}); } + + ~PredicateFactory() { + DCHECK_EQ(stack_depth_, 0) << "Unnested IncrementStackDepth?"; + } + + private: + Predicate* MakeNotPredicateImpl(Predicate* pred) { + IncrementStackDepth stack_frame(this); + if (!stack_frame.HasOverflowed()) { + if (Predicate* simplified = SimplifyUsingDeMorgan(pred)) { + return simplified; + } + + // ~~A => A + if (auto* not_pred = dynamic_cast(pred)) { + return not_pred->operand(); + } + } + + SignatureForNot signature = pred; + auto it = interned_not_instances_.find(signature); + if (it == interned_not_instances_.end()) { + std::unique_ptr new_pred = Make(pred); + Predicate* new_pred_ptr = new_pred.get(); + interned_not_instances_.emplace(signature, std::move(new_pred)); + return new_pred_ptr; + } else { + return it->second.get(); + } + } + + Predicate* SimplifyUsingDeMorgan(Predicate* pred) { + // ~(A & B & C & ...) => ~A | ~B | ~C | ~... + // ~(A | B | C | ...) -> ~A & ~B & ~C & ~... + Predicate::Kind kind = pred->kind(); + + if (kind == Predicate::Kind::kAnd || kind == Predicate::Kind::kOr) { + std::vector new_operands; + absl::c_transform(pred->GetOperands(), std::back_inserter(new_operands), + [&](Predicate* p) { return MakeNotPredicate(p); }); + return kind == Predicate::Kind::kOr ? MakeAndPredicate(new_operands) + : MakeOrPredicate(new_operands); + } + + return nullptr; + } + + template + std::unique_ptr Make(Args&&... args) { + // If we ever expose the Predicate class outside this .cc file then we may + // want to make this hard to misuse (by accidentally passing in an arbitrary + // integer to the Predicate constructor for instance). + return std::unique_ptr( + new PredicateT(id_counter_++, std::forward(args)...)); + } + + Predicate* MakeAndOrImpl(absl::Span operands, bool is_and); + Predicate* MakeInternedAndOr(std::vector simplified_ops, + Predicate::Kind pred_kind); + + // Predicate instances are interned, meaning that there is only a single + // instance of a Predicate object with a given content. This makes checking + // for structural equality super-cheap -- we can just compare pointers. + // + // We intern predicates by maintaining a map from the content of a Predicate + // to the only instance of said predicate we allow to exist in the + // interned_and_or_instances_, interned_not_instances_ and + // interned_symbol_instances_ fields. These maps also double up as storage + // for the owning pointers to predicate instances. + + using SignatureForAndOr = + std::pair>; + using SignatureForNot = Predicate*; + using SignatureForAndRec = + std::tuple>; + using SignatureForSymbol = std::pair; + using SignatureForIntSymbol = std::pair>; + + struct HashSignatureForAndOr { + size_t operator()(const SignatureForAndOr& signature) const { + size_t hash = ::tensorflow::hash()(signature.first); + for (Predicate* p : signature.second) { + hash = Hash64Combine(hash, ::tensorflow::hash()(p)); + } + return hash; + } + }; + + struct HashSignatureForSymbol { + size_t operator()(const SignatureForSymbol& signature) const { + return Hash64Combine(SafeTensorId::Hasher()(signature.first), + ::tensorflow::hash()(signature.second)); + } + }; + + struct HashSignatureForIntSymbol { + size_t operator()(const SignatureForIntSymbol& signature) const { + return Hash64Combine( + SafeTensorId::Hasher()(signature.first), + Hash64Combine( + ::tensorflow::hash()(signature.second.has_value()), + ::tensorflow::hash()( + signature.second.has_value() ? *signature.second : 0))); + } + }; + + // Used to limit recursion to avoid blowing up the stack and cap compile time. + class IncrementStackDepth { + public: + explicit IncrementStackDepth(PredicateFactory* parent) : parent_(parent) { + parent_->stack_depth_++; + } + + bool HasOverflowed() const { + const int kMaxStackDepth = 8; + return parent_->stack_depth_ >= kMaxStackDepth; + } + + ~IncrementStackDepth() { parent_->stack_depth_--; } + + private: + PredicateFactory* parent_; + }; + + // A cache for the MakeNotPredicate function. + // + // NB! This is *not* the same as `interned_not_instances_`. + // `interned_not_instances_` maps ensures pointer identity for `NotPredicate` + // instances, i.e., it ensures there at most one instance of Not(predicate) + // for any given predicate whereas `make_not_predicate_cache_` simply caches + // the result of the `MakeNotPredicate` function. The values in + // `interned_not_instances_` are always instance of `NotPredicate` whereas the + // values in `make_not_predicate_cache_` may not be (for instance it will map + // Not(Not(A)) to A). + absl::flat_hash_map make_not_predicate_cache_; + + absl::flat_hash_map, + HashSignatureForAndOr> + interned_and_or_instances_; + absl::flat_hash_map> + interned_not_instances_; + absl::flat_hash_map> + interned_and_rec_instances_; + absl::flat_hash_map, + HashSignatureForSymbol> + interned_symbol_instances_; + absl::flat_hash_map, + HashSignatureForIntSymbol> + interned_int_symbol_instances_; + int64_t id_counter_ = 0; + int stack_depth_ = 0; +}; + +Predicate* PredicateFactory::MakeInternedAndOr( + std::vector simplified_ops, Predicate::Kind pred_kind) { + std::stable_sort( + simplified_ops.begin(), simplified_ops.end(), + [](Predicate* a, Predicate* b) { return a->id() < b->id(); }); + + auto it = interned_and_or_instances_.find({pred_kind, simplified_ops}); + if (it != interned_and_or_instances_.end()) { + return it->second.get(); + } + + simplified_ops.shrink_to_fit(); + // NB! Because we'll use a non-owning reference to simplified_ops in the + // key for interned_and_or_instances_ we need to be careful to std::move() + // it all the way through. + absl::Span operands_slice = simplified_ops; + std::unique_ptr new_pred = + pred_kind == Predicate::Kind::kAnd + ? Make(std::move(simplified_ops)) + : Make(std::move(simplified_ops)); + + Predicate* new_pred_ptr = new_pred.get(); + interned_and_or_instances_.emplace( + SignatureForAndOr(pred_kind, operands_slice), std::move(new_pred)); + return new_pred_ptr; +} + +// Common code to create AndPredicate or OrPredicate instances. +Predicate* PredicateFactory::MakeAndOrImpl( + absl::Span operands, bool is_and) { + Predicate::Kind pred_kind = + is_and ? Predicate::Kind::kAnd : Predicate::Kind::kOr; + + IncrementStackDepth stack_frame(this); + if (stack_frame.HasOverflowed()) { + return MakeInternedAndOr( + std::vector(operands.begin(), operands.end()), pred_kind); + } + + Predicate::Kind other_pred_kind = + is_and ? Predicate::Kind::kOr : Predicate::Kind::kAnd; + absl::flat_hash_set simplified_ops_set; + std::vector simplified_ops; + for (Predicate* op : operands) { + // Simplify A&A => A and A|A => A. + if (!simplified_ops_set.insert(op).second) { + continue; + } + + if (op->kind() == pred_kind) { + // "Inline" the operands of an inner And/Or into the parent And/Or. + for (Predicate* subop : op->GetOperands()) { + if (simplified_ops_set.insert(subop).second) { + simplified_ops.push_back(subop); + } + } + } else { + simplified_ops.push_back(op); + } + } + + if (simplified_ops.size() == 1) { + return simplified_ops[0]; + } + + // Simplify "A&~A=>False" and "A|~A=>True". + absl::flat_hash_set negated_ops; + for (Predicate* op : simplified_ops) { + if (negated_ops.count(op)) { + // Simple case: + // + // A & ~A & ... == False + // A | ~A | ... == True + return is_and ? MakeFalse() : MakeTrue(); + } + + Predicate* negated_op = MakeNotPredicate(op); + if (negated_op->kind() == pred_kind) { + // Slightly more complicated case: + // + // (~A | ~B | ~C) & A & B & C & ... == + // ~(A & B & C) & (A & B & C) & ... == False + // + // (~A & ~B & ~C) | A | B | C | ... == + // ~(A | B | C) | (A | B | C) | ... == True + if (absl::c_all_of(negated_op->GetOperands(), [&](Predicate* p) { + return simplified_ops_set.contains(p); + })) { + return is_and ? MakeFalse() : MakeTrue(); + } + } + negated_ops.insert(negated_op); + } + + // Simplify {S,&,X} & ~X & ... => S & ... + if (is_and) { + absl::flat_hash_set to_remove; + std::vector to_add; + for (Predicate* op : simplified_ops) { + if (op->kind() == Predicate::Kind::kAndRecurrence) { + auto* and_rec = static_cast(op); + if (negated_ops.contains(and_rec->step())) { + // Remove and_rec and ~X and insert S. Note that checking the + // existence of ~X through negated_ops is sufficient since it makes + // sure the predicate is in the input operands. It does not need to + // be in simplified_ops if it was already cancelled out. + to_remove.insert(and_rec); + to_remove.insert(MakeNotPredicate(and_rec->step())); + to_add.push_back(and_rec->start()); + } + } + } + auto it = simplified_ops.begin(); + while (it != simplified_ops.end()) { + if (to_remove.contains(*it)) { + it = simplified_ops.erase(it); + } else { + ++it; + } + } + simplified_ops.insert(simplified_ops.end(), to_add.begin(), to_add.end()); + } + + // If all ops contain the same subop, then factor it out thanks to the + // distributive property. Such as: + // - (A & B) | (A & C) | (A & D) => A & (B | C | D) + // - (A | B) & (A | C) & (A | D) => A | (B & C & D) + // + // First find any predicates contained in all subops. + std::vector common_inner_operands; + absl::flat_hash_set common_inner_operands_set; + for (Predicate* op : simplified_ops) { + if (op->kind() != other_pred_kind) { + common_inner_operands.clear(); + break; + } + + if (common_inner_operands.empty()) { + common_inner_operands.insert(common_inner_operands.end(), + op->GetOperands().begin(), + op->GetOperands().end()); + } else { + common_inner_operands.clear(); + absl::c_copy_if(op->GetOperands(), + std::back_inserter(common_inner_operands), + [&](Predicate* sub_op) { + return common_inner_operands_set.count(sub_op) == 1; + }); + } + if (common_inner_operands.empty()) break; + common_inner_operands_set.clear(); + common_inner_operands_set.insert(common_inner_operands.begin(), + common_inner_operands.end()); + } + + if (common_inner_operands.empty()) { + return MakeInternedAndOr(std::move(simplified_ops), pred_kind); + } + + // For all predicates that can be factored out, remove them and recreate the + // subops. + std::vector factored_ops; + for (Predicate* op : simplified_ops) { + std::vector new_sub_op_ops; + absl::c_copy_if(op->GetOperands(), std::back_inserter(new_sub_op_ops), + [&](Predicate* sub_op) { + return std::find(common_inner_operands.begin(), + common_inner_operands.end(), + sub_op) == common_inner_operands.end(); + }); + factored_ops.push_back(MakeAndOrImpl(new_sub_op_ops, !is_and)); + } + + Predicate* new_inner_op = MakeAndOrImpl(factored_ops, is_and); + std::vector outer_ops; + outer_ops.push_back(new_inner_op); + outer_ops.insert(outer_ops.end(), common_inner_operands.begin(), + common_inner_operands.end()); + return MakeAndOrImpl(outer_ops, !is_and); +} + +class DeadnessAnalysisImpl : public DeadnessAnalysis { + public: + explicit DeadnessAnalysisImpl(const Graph* graph) + : graph_(*graph), vlog_(VLOG_IS_ON(2)) {} + + absl::Status Populate(bool enable_optimistic); + absl::Status PopulateFrame(absl::Span topo, + bool use_optimistic_mode, bool* success); + absl::StatusOr GetPredicateFor( + Node* n, int oidx) const override; + void Print() const override; + absl::flat_hash_map + PredicateMapAsString() const; + + private: + enum class EdgeKind { kDataAndControl, kDataOnly, kControlOnly }; + + absl::Status GetInputPreds(Node* n, EdgeKind edge_kind, + std::vector* result); + + // Sets the predicate for output `output_idx` of `n` to `pred`. Sets the i'th + // bit of `should_revisit` if `pred` is different from the current predicate + // for the `output_idx` output of `n`. + void SetPredicate(Node* n, int output_idx, Predicate* pred, + std::vector* should_revisit) { + auto insert_result = + predicate_map_.insert({TensorId(n->name(), output_idx), pred}); + if (!insert_result.second && insert_result.first->second != pred) { + VLOG(4) << "For " << n->name() << ":" << output_idx << " from " + << insert_result.first->second->ToString() << " " + << insert_result.first->second << " to " << pred->ToString() + << " " << pred; + insert_result.first->second = pred; + if (should_revisit != nullptr) { + for (const Edge* e : n->out_edges()) { + (*should_revisit)[e->dst()->id()] = true; + } + } + } + } + + void SetPredicate(Node* n, absl::Span output_idxs, Predicate* pred, + std::vector* should_revisit) { + for (int output_idx : output_idxs) { + SetPredicate(n, output_idx, pred, should_revisit); + } + } + + absl::Status HandleSwitch(Node* n, std::vector* should_revisit); + absl::Status HandleMerge(Node* n, std::vector* should_revisit, + bool use_optimistic_mode); + absl::Status HandleRecv(Node* n, std::vector* should_revisit); + absl::Status HandleGeneric(Node* n, std::vector* should_revisit); + absl::Status HandleNode(Node* n, std::vector* should_revisit, + bool use_optimistic_mode = false); + + absl::Status GetFrameBasedTopologicalOrder(std::vector* order); + + bool IsRootEnter(const Node* n) const { + return IsEnter(n) && control_flow_info_[n->id()].parent_frame->IsSource(); + } + + bool IsRootExit(const Node* n) const { + return IsExit(n) && control_flow_info_[n->id()].parent_frame->IsSource(); + } + + const Graph& graph_; + absl::flat_hash_map predicate_map_; + PredicateFactory predicate_factory_; + std::vector control_flow_info_; + bool vlog_; + absl::flat_hash_map frame_to_merge_node_; +}; + +TensorId InputEdgeToTensorId(const Edge* e) { + return TensorId(e->src()->name(), e->src_output()); +} + +absl::Status DeadnessAnalysisImpl::GetInputPreds( + Node* n, DeadnessAnalysisImpl::EdgeKind edge_kind, + std::vector* result) { + result->clear(); + for (const Edge* in_edge : n->in_edges()) { + bool should_process = + edge_kind == EdgeKind::kDataAndControl || + (in_edge->IsControlEdge() && edge_kind == EdgeKind::kControlOnly) || + (!in_edge->IsControlEdge() && edge_kind == EdgeKind::kDataOnly); + + if (should_process) { + auto it = predicate_map_.find(InputEdgeToTensorId(in_edge)); + if (it == predicate_map_.end()) { + xla::GraphCycles graph_cycles; + TF_RETURN_IF_ERROR( + CreateCycleDetectionGraph(&graph_, &graph_cycles).status()); + + // If we didn't return with an error above then the graph is probably + // fine and we have a bug in deadness analysis. + return absl::InternalError(absl::StrCat( + "Could not find input ", in_edge->DebugString(), " to ", n->name(), + " when visiting the graph in post-order. Most " + "likely indicates a bug in deadness analysis.")); + } + result->push_back(it->second); + } + } + return absl::OkStatus(); +} + +absl::Status DeadnessAnalysisImpl::HandleSwitch( + Node* n, std::vector* should_revisit) { + std::vector input_preds; + TF_RETURN_IF_ERROR(GetInputPreds(n, EdgeKind::kDataAndControl, &input_preds)); + const Edge* pred_edge; + TF_RETURN_IF_ERROR(n->input_edge(1, &pred_edge)); + + if (n->type_string() != "_SwitchN") { // bool pred branch selector. + Predicate* true_switch; + TF_RETURN_IF_ERROR(predicate_factory_.MakeSymbolPredicate( + pred_edge->src(), pred_edge->src_output(), + /*must_be_true=*/true, &true_switch)); + + Predicate* false_switch = predicate_factory_.MakeNotPredicate(true_switch); + + // Output 0 is alive iff all inputs are alive and the condition is false. + input_preds.push_back(false_switch); + SetPredicate(n, 0, predicate_factory_.MakeAndPredicate(input_preds), + should_revisit); + input_preds.pop_back(); + + // Output 1 is alive iff all inputs are alive and the condition is true. + input_preds.push_back(true_switch); + SetPredicate(n, 1, predicate_factory_.MakeAndPredicate(input_preds), + should_revisit); + input_preds.pop_back(); + } else { // N-way switch case. Exactly one of N branches is alive. + Predicate* branch_pred; + for (int i = 0; i < n->num_outputs() - 1; i++) { + TF_RETURN_IF_ERROR(predicate_factory_.MakeSymbolPredicate( + pred_edge->src(), pred_edge->src_output(), + /*must_have_value=*/std::optional(i), &branch_pred)); + input_preds.push_back(branch_pred); + SetPredicate(n, i, predicate_factory_.MakeAndPredicate(input_preds), + should_revisit); + input_preds.pop_back(); + input_preds.push_back(predicate_factory_.MakeNotPredicate(branch_pred)); + } + // The default (last) branch does not need its own symbol, is simply the + // nor of all other branches. + SetPredicate(n, n->num_outputs() - 1, + predicate_factory_.MakeAndPredicate(input_preds), + should_revisit); + } + + // Control is alive iff all inputs are alive. + SetPredicate(n, Graph::kControlSlot, + predicate_factory_.MakeAndPredicate(input_preds), + should_revisit); + + return absl::OkStatus(); +} + +namespace { +absl::Status CreateMultipleNextIterationInputsError(Node* merge) { + std::vector backedges; + for (const Edge* backedge : merge->in_edges()) { + if (backedge->src()->IsNextIteration()) { + backedges.push_back(absl::StrCat(" ", SummarizeNode(*backedge->src()))); + } + } + return absl::InvalidArgumentError(absl::StrCat( + "Multiple NextIteration inputs to merge node ", + FormatNodeForError(*merge), ": \n", absl::StrJoin(backedges, "\n"), + "\nMerge nodes can have at most one incoming NextIteration edge.")); +} + +absl::Status FindUniqueBackedge(Node* merge, const Edge** result) { + *result = nullptr; + CHECK(merge->IsMerge()); + for (const Edge* e : merge->in_edges()) { + if (e->src()->IsNextIteration()) { + if (*result != nullptr) { + return CreateMultipleNextIterationInputsError(merge); + } + *result = e; + } + } + return absl::OkStatus(); +} + +// If `backedge_predicate` is equal to `symbolic_predicate` & Step where Step +// does not contain `symbolic_predicate` as an inner (not top-level) operand +// then returns `Step`. Otherwise returns nullptr. +Predicate* DeduceStepPredicate(PredicateFactory* predicate_factory, + Predicate* symbolic_predicate, + Predicate* backedge_predicate) { + CHECK(dynamic_cast(symbolic_predicate)); + if (backedge_predicate->kind() != Predicate::Kind::kAnd) { + return nullptr; + } + + std::vector and_ops; + absl::Span recurrent_pred_ops = + backedge_predicate->GetOperands(); + + bool found_sym = false; + for (Predicate* and_op : recurrent_pred_ops) { + // We want the `symbol_predicate` to be the one of the operands of + // `backedge_predicate`, + if (and_op == symbolic_predicate) { + found_sym = true; + continue; + } + + // but we don't want it to be present anywhere else in the formula. E.g. we + // don't want the recurrent predicate to be + // symbol_predicate&(X|symbol_predicate). + bool found_sym_as_inner_operand = false; + auto has_self_as_inner_operand = [&](Predicate* p) { + if (p == symbolic_predicate) { + found_sym_as_inner_operand = true; + return true; // Stop searching, we're done. + } + + // Continue searching. + return false; + }; + + Predicate::Visit(and_op, has_self_as_inner_operand); + if (found_sym_as_inner_operand) { + return nullptr; + } + and_ops.push_back(and_op); + } + + return found_sym ? predicate_factory->MakeAndPredicate(and_ops) : nullptr; +} + +absl::Status GetFullFrame(const Node* n, + absl::Span cfi_infos, + std::vector* frame) { + int depth = 0; + for (const ControlFlowInfo* cfi_iter = &cfi_infos[n->id()]; !n->IsSource(); + n = cfi_iter->parent_frame, cfi_iter = &cfi_infos[n->id()]) { + frame->push_back(cfi_iter->frame_name); + + if (depth++ > 5000) { + return absl::InternalError( + "Frame of depth > 5000: Probably malformed graph or a bug in " + "BuildControlFlowInfo"); + } + } + + return absl::OkStatus(); +} + +// If the node is inside some frames, get the name of the outermost non-empty +// frame. Otherwise, get an empty frame name. +absl::Status GetRootFrame(const Node* n, + absl::Span cfi_infos, + absl::string_view* frame) { + int depth = 0; + const ControlFlowInfo* cfi_iter = &cfi_infos[n->id()]; + while (!cfi_iter->parent_frame->IsSource()) { + n = cfi_iter->parent_frame; + cfi_iter = &cfi_infos[n->id()]; + + if (depth++ > 5000) { + return absl::InternalError( + "Frame of depth > 5000: Probably malformed graph or a bug in " + "BuildControlFlowInfo"); + } + } + + *frame = cfi_iter->frame_name; + return absl::OkStatus(); +} +} // namespace + +absl::Status DeadnessAnalysisImpl::HandleMerge( + Node* n, std::vector* should_revisit, bool use_optimistic_mode) { + // Merge ignores deadness of its control inputs. A merge that isn't the + // target of a backedge has is alive iff any of its data inputs are. The + // liveness of a merge that is the target of a backedge can sometimes be + // represented using a AndRecurrencePredicate. If neither apply, we represent + // the liveness of the merge symbolically. + + bool has_unvisited_backedge = false; + for (const Edge* e : n->in_edges()) { + if (!e->IsControlEdge() && e->src()->IsNextIteration()) { + has_unvisited_backedge |= !predicate_map_.count(InputEdgeToTensorId(e)); + } + } + + auto it = predicate_map_.find(TensorId(n->name(), 0)); + if (it == predicate_map_.end()) { + if (has_unvisited_backedge) { + // We're visiting this merge for the first time and it has an unvisited + // backedge. + Predicate* input_data_pred; + if (use_optimistic_mode) { + // In the optimistic mode, we use the first-seen Merge node per + // frame as the representative Merge node. It is just convenient and + // does not affect the result after pattern-matching into the + // AndRecurrence form. + absl::string_view frame_name = control_flow_info_[n->id()].frame_name; + auto insert_result = frame_to_merge_node_.insert({frame_name, n}); + Node* representative = insert_result.first->second; + TF_RETURN_IF_ERROR(predicate_factory_.MakeSymbolPredicate( + representative, /*output_idx=*/0, /*must_be_true=*/false, + &input_data_pred)); + } else { + TF_RETURN_IF_ERROR(predicate_factory_.MakeSymbolPredicate( + n, /*output_idx=*/0, /*must_be_true=*/false, &input_data_pred)); + } + + SetPredicate(n, {0, 1, Graph::kControlSlot}, input_data_pred, + should_revisit); + return absl::OkStatus(); + } + + std::vector input_preds; + TF_RETURN_IF_ERROR(GetInputPreds(n, EdgeKind::kDataOnly, &input_preds)); + + // We're visiting this merge for the first time and it is an acyclic merge. + Predicate* input_data_pred = + predicate_factory_.MakeOrPredicate(input_preds); + SetPredicate(n, {0, 1, Graph::kControlSlot}, input_data_pred, + should_revisit); + return absl::OkStatus(); + } + + if (it->second->kind() == Predicate::Kind::kSymbol) { + // Last time we visited this merge we only got a symbolic predicate because + // of an unvisited backedge. Try to pattern match the predicate expression + // for that backedge (which should be visited now) into an and recurrence + // for the merge node. + const Edge* unique_backedge; + TF_RETURN_IF_ERROR(FindUniqueBackedge(n, &unique_backedge)); + if (unique_backedge) { + if (Predicate* step = DeduceStepPredicate( + &predicate_factory_, it->second, + predicate_map_[InputEdgeToTensorId(unique_backedge)])) { + // If the predicate for the backedge is "Sym&X" where "Sym" is the + // predicate for the merge then the merge has predicate {S,&,X} where S + // is the predicate for the merge ignoring the backedge. + std::vector non_recurrent_inputs; + for (const Edge* e : n->in_edges()) { + if (e != unique_backedge) { + non_recurrent_inputs.push_back( + predicate_map_[InputEdgeToTensorId(e)]); + } + } + + Predicate* start = + predicate_factory_.MakeOrPredicate(non_recurrent_inputs); + std::vector frame; + TF_RETURN_IF_ERROR(GetFullFrame(n, control_flow_info_, &frame)); + Predicate* and_rec = predicate_factory_.MakeAndRecurrencePredicate( + start, step, std::move(frame)); + SetPredicate(n, {0, 1, Graph::kControlSlot}, and_rec, should_revisit); + return absl::OkStatus(); + } + } + } + return absl::OkStatus(); +} + +absl::Status DeadnessAnalysisImpl::HandleRecv( + Node* n, std::vector* should_revisit) { + // In addition to being alive or dead based on the inputs, a _Recv can also + // acquire a dead signal from a _Send. + std::vector input_preds; + TF_RETURN_IF_ERROR(GetInputPreds(n, EdgeKind::kDataAndControl, &input_preds)); + Predicate* signal_is_alive; + TF_RETURN_IF_ERROR(predicate_factory_.MakeSymbolPredicate( + n, /*output_idx=*/0, /*must_be_true=*/false, &signal_is_alive)); + input_preds.push_back(signal_is_alive); + SetPredicate(n, {0, Graph::kControlSlot}, + predicate_factory_.MakeAndPredicate(input_preds), + should_revisit); + return absl::OkStatus(); +} + +absl::Status DeadnessAnalysisImpl::HandleGeneric( + Node* n, std::vector* should_revisit) { + // Generally nodes are alive iff all their inputs are alive. + std::vector input_preds; + TF_RETURN_IF_ERROR(GetInputPreds(n, EdgeKind::kDataAndControl, &input_preds)); + Predicate* pred = predicate_factory_.MakeAndPredicate(input_preds); + for (int output_idx = 0; output_idx < n->num_outputs(); output_idx++) { + SetPredicate(n, output_idx, pred, should_revisit); + } + SetPredicate(n, Graph::kControlSlot, pred, should_revisit); + return absl::OkStatus(); +} + +absl::Status DeadnessAnalysisImpl::HandleNode(Node* n, + std::vector* should_revisit, + bool use_optimistic_mode) { + if (n->IsSwitch()) { + TF_RETURN_IF_ERROR(HandleSwitch(n, should_revisit)); + } else if (n->IsMerge()) { + TF_RETURN_IF_ERROR(HandleMerge(n, should_revisit, use_optimistic_mode)); + } else if (n->IsControlTrigger()) { + SetPredicate(n, Graph::kControlSlot, predicate_factory_.MakeTrue(), + nullptr); + } else if (n->IsRecv() || n->IsHostRecv()) { + TF_RETURN_IF_ERROR(HandleRecv(n, should_revisit)); + } else if (n->IsNextIteration()) { + TF_RETURN_IF_ERROR(HandleGeneric(n, should_revisit)); + } else { + TF_RETURN_IF_ERROR(HandleGeneric(n, should_revisit)); + } + return absl::OkStatus(); +} + +// Compute a special topological order for the Graph, where nodes having the +// same root frame are placed adjacent to each other. The traversal uses a +// variant of Kahn's algorithm. num_ready_inputs is used to keep track of how +// many inputs of each node are ready; a node is ready to be scheduled if all +// of its inputs are ready. +// Ref. to https://en.wikipedia.org/wiki/Topological_sorting for details. +absl::Status DeadnessAnalysisImpl::GetFrameBasedTopologicalOrder( + std::vector* order) { + absl::flat_hash_map num_enters_for_frame; + absl::flat_hash_map num_exits_for_frame; + std::vector num_ready_inputs(graph_.num_node_ids(), 0); + Node* src_node = graph_.source_node(); + for (const auto* node : graph_.op_nodes()) { + const ControlFlowInfo& cf = control_flow_info_[node->id()]; + if (IsRootEnter(node)) { + // Since we care only the root-level frame, full frame names are the same + // as frame names. + ++num_enters_for_frame[cf.frame_name]; + } else if (IsRootExit(node)) { + ++num_exits_for_frame[cf.frame_name]; + } + // Edge NextIteration->Merge is counted before starting the traversal to + // break the backedges. + if (IsMerge(node)) { + for (const Edge* e : node->in_edges()) { + if (IsNextIteration(e->src())) { + ++num_ready_inputs[node->id()]; + } + } + } + } + + // dequeue is used to ensure that the nodes are first-in-first-out. This + // order guarantees that the exits in the ready queue are visited before + // nodes that will become ready in the future. + std::deque ready; + ready.push_back(src_node); + // ready_enters_per_frame and ready_exits serve as a staging area to buffer + // the ready enters/exits before they are moved to the `ready` queue for + // controlling the start and end of a processing frame. + absl::flat_hash_map> + ready_enters_per_frame; + // Exit nodes shall all be from the same frame, as we process a frame at a + // time. So, one vector is enough. + std::vector ready_exits; + while (!ready.empty()) { + Node* curr_node = ready.front(); + ready.pop_front(); + + VLOG(4) << "Visiting " << curr_node->name(); + order->push_back(curr_node); + + for (const Edge* out_edge : curr_node->out_edges()) { + Node* out = out_edge->dst(); + int out_id = out->id(); + if (IsNextIteration(curr_node) && IsMerge(out)) { + // Edge NextIteration->Merge has been counted. + continue; + } + ++num_ready_inputs[out->id()]; + if (!out->IsOp()) continue; // Skip Sink/Source nodes. + if (num_ready_inputs[out->id()] != out->in_edges().size()) continue; + + absl::string_view frame_name = control_flow_info_[out_id].frame_name; + if (IsRootEnter(out)) { + ready_enters_per_frame[frame_name].push_back(out); + } else if (IsRootExit(out)) { + ready_exits.push_back(out); + } else { + ready.push_back(out); + } + } + + if (ready.empty()) { + // Try moving nodes from ready_enters_per_frame and ready_exits to + // `ready`. + if (!ready_exits.empty()) { + // If there are nodes in ready_exits we must process them before + // processing ready_enters_per_frame to make sure all nodes in the + // currently processing frame are visited before starting processing + // other frames. + absl::string_view frame_name = + control_flow_info_[ready_exits.front()->id()].frame_name; + CHECK_EQ(ready_exits.size(), num_exits_for_frame[frame_name]); + ready.insert(ready.end(), ready_exits.begin(), ready_exits.end()); + ready_exits.clear(); + } else { + // Otherwise, try moving nodes from ready_enters to `ready`. + for (auto iter = ready_enters_per_frame.begin(); + iter != ready_enters_per_frame.end(); ++iter) { + absl::string_view frame_name = iter->first; + const std::vector& ready_enters = iter->second; + if (ready_enters.size() == num_enters_for_frame[frame_name]) { + ready.insert(ready.end(), ready_enters.begin(), ready_enters.end()); + ready_enters_per_frame.erase(iter); + break; + } + } + } + } + } + + if (!ready_enters_per_frame.empty() || !ready_exits.empty()) { + return absl::InvalidArgumentError( + "Some enters/exits have never been visited in the traversal." + " Most probably the input graph is malformed."); + } + return absl::OkStatus(); +} + +// We populate the nodes along a special topological order where nodes having +// the same root frame are placed adjacent to each other. This grouping enables +// processing the graph per root frame at a time and guarantees that when a root +// frame is being processed, nodes in the downstream frames have not yet been +// processed. This property is important because we need to process an entire +// frame to know whether the optimistic mode converges or not. In other words, +// nodes in the downstream frames shall not be populated until all of its +// upstream frames are populated. In effect, this order enables processing each +// (nested) tf.while one-by-one, as each (nested) tf.while creates a unique +// (root) frame. Note that we don't separate while loops belonging to the same +// nested while, as there is no clean cut for separating them in the topological +// order. +absl::Status DeadnessAnalysisImpl::Populate(bool enable_optimistic) { + std::vector unreachable_nodes; + // Compute the loop structure of the graph. + TF_RETURN_IF_ERROR( + BuildControlFlowInfo(&graph_, &control_flow_info_, &unreachable_nodes)); + + // Do some opportunistic error checking: + if (!unreachable_nodes.empty()) { + if (unreachable_nodes.size() > 5) { + unreachable_nodes.erase(unreachable_nodes.begin() + 5, + unreachable_nodes.end()); + } + + return absl::InvalidArgumentError(absl::StrCat( + "Found unreachable nodes, most likely source and sink nodes not " + "connected: ", + absl::StrJoin(unreachable_nodes, ", "))); + } + + std::vector topo; + TF_RETURN_IF_ERROR(GetFrameBasedTopologicalOrder(&topo)); + + size_t frame_start = 0; + while (frame_start < topo.size()) { + // Batching nodes who have the same root frame. + absl::string_view cur_frame_name; + TF_RETURN_IF_ERROR( + GetRootFrame(topo[frame_start], control_flow_info_, &cur_frame_name)); + size_t frame_end = frame_start; + for (size_t i = frame_start + 1; i < topo.size(); ++i) { + absl::string_view i_frame_name; + TF_RETURN_IF_ERROR( + GetRootFrame(topo[i], control_flow_info_, &i_frame_name)); + if (i_frame_name == cur_frame_name) { + frame_end = i; + } else { + break; + } + } + absl::Span sub_topo(topo.data() + frame_start, + /*length=*/frame_end - frame_start + 1); + frame_start = frame_end + 1; + + // First, try the optimistic mode. + bool success = false; + if (enable_optimistic && !cur_frame_name.empty()) { + TF_RETURN_IF_ERROR( + PopulateFrame(sub_topo, /*use_optimistic_mode=*/true, &success)); + } + if (!success) { + // The optimistic mode does not converge. Let's fall back to the + // pessimistic mode. + TF_RETURN_IF_ERROR( + PopulateFrame(sub_topo, /*use_optimistic_mode=*/false, nullptr)); + } + VLOG(2) << "Done populating frame " << cur_frame_name << " using the " + << (success ? "optimistic" : "pessimistic") << " mode."; + } + + return absl::OkStatus(); +} + +absl::Status DeadnessAnalysisImpl::PopulateFrame(absl::Span topo, + bool use_optimistic_mode, + bool* success) { + CHECK(use_optimistic_mode && success != nullptr || + !use_optimistic_mode && success == nullptr); + + // This an abstract interpretation over the deadness propagation semantics of + // the graph executor. + // + // We iterate over the graph twice, each time in a topological order. On the + // first iteration merge nodes with backedges are mapped to symbolic + // predicates. On the second iteration we use the predicates assigned to the + // backedges in the previous iteration to infer a more precise predicate for + // the backedge merge nodes and all the nodes that transitively use it. + // + // We don't track the output indices for should_revisit. Instead, putting a + // node in `should_revisit` denotes that the deadness flowing out from any + // output from said node may have changed. This is fine; only switches + // propagate different deadness along different output edges, and since the + // delta is solely due to the input *values* (and not input deadness), the + // delta should not change in the second iteration. + std::vector should_revisit; + should_revisit.resize(graph_.num_node_ids()); + for (Node* n : topo) { + VLOG(4) << "Visiting " << n->name(); + TF_RETURN_IF_ERROR( + HandleNode(n, /*should_revisit=*/nullptr, use_optimistic_mode)); + if (n->IsNextIteration()) { + // If this is a backedge for a merge node then remember to reprocess the + // merge the next time we run. + for (const Edge* e : n->out_edges()) { + if (e->dst()->IsMerge()) { + should_revisit[e->dst()->id()] = true; + } + } + } + } + + for (Node* n : topo) { + // The nodes added to should_revisit in the previous loop need to be + // revisited now. Reprocessing these initial nodes may add *their* + // consumers to should_revisit, and these newly added nodes will also be + // processed by this very same loop. Since we're traversing the graph in + // topological order (producers before consumers) and HandleNode(n) can only + // ever add n's consumers to should_revisit, we won't "miss" an addition to + // should_revisit. + if (should_revisit[n->id()]) { + VLOG(4) << "Revisiting " << n->name(); + TF_RETURN_IF_ERROR(HandleNode(n, &should_revisit)); + } + } + + // Check if the optimistic analysis converges. Specifically, check whether + // all the predicates of the merge nodes in the same frame are the same. If + // yes, report success. If not, report failure and clear the assigned + // predicates. + if (use_optimistic_mode) { + bool is_converged = true; + absl::flat_hash_map frame_to_pred; + for (Node* n : topo) { + if (!n->IsMerge()) { + continue; + } + const Edge* e; + TF_RETURN_IF_ERROR(FindUniqueBackedge(n, &e)); + if (e == nullptr) { + // Skip acyclic merge nodes. + continue; + } + Node* merge = n; + // Note that here uses frame names instead of root frame names. In the + // case of a nested while loop, each level of while loops can have merges + // with different predicate instances, while the merge nodes on the same + // level must have the same predicate instances. + absl::string_view frame_name = control_flow_info_[merge->id()].frame_name; + auto it = predicate_map_.find(TensorId(merge->name(), 0)); + Predicate* merge_pred = it->second; + if (merge_pred->kind() != Predicate::Kind::kAndRecurrence) { + is_converged = false; + VLOG(2) << "Running the optimistic mode on frame " << frame_name + << " does not converge because node " << merge->name() + << " cannot be mapped into the AndRecurrence form."; + break; + } + + auto insert_result = frame_to_pred.insert({frame_name, merge_pred}); + if (!insert_result.second) { + // If we have already seen this frame name, verify the predicate is the + // same as the previously seen one's. + Predicate* curr_andrec = merge_pred; + Predicate* prev_andrec = insert_result.first->second; + if (curr_andrec != prev_andrec) { + is_converged = false; + VLOG(2) << "Running the optimistic mode on frame " << frame_name + << " does not converge. Seeing different Merge predicates: \n" + << curr_andrec->ToString() << " and \n" + << prev_andrec->ToString(); + break; + } + } + } + + // Clear the assigned predicates if the optimistic mode does not converge. + if (!is_converged) { + for (Node* n : topo) { + for (int oid = 0; oid < n->num_outputs(); ++oid) { + predicate_map_.erase(TensorId(n->name(), oid)); + } + predicate_map_.erase(TensorId(n->name(), Graph::kControlSlot)); + } + } + + if (success != nullptr) { + *success = is_converged; + } + } + + return absl::OkStatus(); +} + +absl::StatusOr +DeadnessAnalysisImpl::GetPredicateFor(Node* n, int oidx) const { + auto it = predicate_map_.find(TensorId(n->name(), oidx)); + TF_RET_CHECK(it != predicate_map_.end()) + << "could not find " << TensorId(n->name(), oidx).ToString() + << " in predicate map"; + return MakeDeadnessPredicate(it->second); +} + +void DeadnessAnalysisImpl::Print() const { + std::vector tensor_ids; + tensor_ids.reserve(predicate_map_.size()); + for (const auto& kv_pair : predicate_map_) { + tensor_ids.push_back(kv_pair.first); + } + + std::sort(tensor_ids.begin(), tensor_ids.end()); + + for (TensorId tensor_id : tensor_ids) { + auto it = predicate_map_.find(tensor_id); + CHECK(it != predicate_map_.end()) << tensor_id.ToString(); + VLOG(2) << tensor_id.ToString() << " -> " << it->second->ToString(); + } +} + +} // namespace + +DeadnessAnalysis::~DeadnessAnalysis() {} + +/*static*/ absl::Status DeadnessAnalysis::Run( + const Graph& graph, std::unique_ptr* result) { + std::unique_ptr analysis( + new DeadnessAnalysisImpl(&graph)); + TF_RETURN_IF_ERROR(analysis->Populate(/*enable_optimistic=*/true)); + + if (VLOG_IS_ON(2)) { + analysis->Print(); + } + + *result = std::move(analysis); + return absl::OkStatus(); +} + +absl::flat_hash_map +DeadnessAnalysisImpl::PredicateMapAsString() const { + absl::flat_hash_map result; + for (const auto& kv_pair : predicate_map_) { + CHECK(result.insert({kv_pair.first, kv_pair.second->ToString()}).second); + } + return result; +} + +namespace deadness_analysis_internal { +absl::Status ComputePredicates(const Graph& graph, + PredicateMapTy* out_predicate_map, + bool enable_optimistic) { + DeadnessAnalysisImpl impl(&graph); + TF_RETURN_IF_ERROR(impl.Populate(enable_optimistic)); + *out_predicate_map = impl.PredicateMapAsString(); + return absl::OkStatus(); +} + +} // namespace deadness_analysis_internal + +std::string DeadnessAnalysis::DebugString(DeadnessPredicate predicate) const { + return static_cast(predicate.pred_)->ToString(); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/deadness_analysis.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/deadness_analysis.h new file mode 100644 index 0000000000000000000000000000000000000000..218de70bec2251ab94dc1b6debf975512dc10b5e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/deadness_analysis.h @@ -0,0 +1,99 @@ +/* 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_DEADNESS_ANALYSIS_H_ +#define TENSORFLOW_COMPILER_JIT_DEADNESS_ANALYSIS_H_ + +#include "tensorflow/core/graph/graph.h" + +namespace tensorflow { + +// This analyzes a TensorFlow graph to identify nodes which may have partially +// dead inputs (i.e. these nodes may have some dead inputs and some alive +// inputs). +// +// For example, the ADD node in the following graph +// +// V0 PRED0 V1 PRED1 +// | | | | +// v v v v +// SWITCH SWITCH +// | | +// +---+ + ---+ +// | | +// v v +// ADD +// +// can have its inputs independently dead or alive based on the runtime values +// of PRED0 and PRED1. +// +// It is tempting to call this a liveness analysis but I avoided that because +// "liveness" already has other connotations. +class DeadnessAnalysis { + public: + // An opaque representation of a predicate. DeadnessPredicate + // instances that compare equal via operator== represent predicates + // that always evaluate to the same value. + struct DeadnessPredicate { + public: + DeadnessPredicate(const DeadnessPredicate&) = default; + DeadnessPredicate(DeadnessPredicate&&) = default; + + DeadnessPredicate& operator=(const DeadnessPredicate&) = default; + DeadnessPredicate& operator=(DeadnessPredicate&&) = default; + + bool operator==(const DeadnessPredicate& other) const { + return other.pred_ == pred_; + } + + bool operator!=(const DeadnessPredicate& other) const { + return other.pred_ != pred_; + } + + private: + explicit DeadnessPredicate(void* pred) : pred_(pred) {} + + // This is really a Predicate*, but we don't want to expose that + // implementation detail to our clients. `pred_` has pointer equality so we + // can just compare the pointer in operator== and operator!=. + void* pred_; + + friend class DeadnessAnalysis; + }; + + virtual absl::StatusOr GetPredicateFor(Node* n, + int oidx) const = 0; + + // Prints out the internal state of this instance. For debugging purposes + // only. + virtual void Print() const = 0; + virtual ~DeadnessAnalysis(); + + std::string DebugString(DeadnessPredicate predicate) const; + + // Run the deadness analysis over `graph` and returns an error or a populated + // instance of DeadnessAnalysis in `result`. + static absl::Status Run(const Graph& graph, + std::unique_ptr* result); + + protected: + static DeadnessPredicate MakeDeadnessPredicate(void* pred) { + return DeadnessPredicate(pred); + } +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_DEADNESS_ANALYSIS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/deadness_analysis_internal.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/deadness_analysis_internal.h new file mode 100644 index 0000000000000000000000000000000000000000..7050cbfad96178ece9e27e51cf5964b78c4645d9 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/deadness_analysis_internal.h @@ -0,0 +1,36 @@ +/* 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_DEADNESS_ANALYSIS_INTERNAL_H_ +#define TENSORFLOW_COMPILER_JIT_DEADNESS_ANALYSIS_INTERNAL_H_ + +#include "absl/container/flat_hash_map.h" +#include "tensorflow/core/graph/tensor_id.h" + +namespace tensorflow { +namespace deadness_analysis_internal { + +// Returns a map describing the predicate each Tensor was mapped to. For +// testing purposes only. +using PredicateMapTy = + absl::flat_hash_map; +absl::Status ComputePredicates(const Graph& graph, + PredicateMapTy* out_predicate_map, + bool enable_optimistic = true); + +} // namespace deadness_analysis_internal +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_DEADNESS_ANALYSIS_INTERNAL_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/deadness_analysis_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/deadness_analysis_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..742ed5ec2a2fce4d0df00c9b522820801a155412 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/deadness_analysis_test.cc @@ -0,0 +1,1313 @@ +/* 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 "tensorflow/compiler/jit/deadness_analysis.h" + +#include "tensorflow/cc/framework/ops.h" +#include "tensorflow/cc/ops/array_ops.h" +#include "tensorflow/cc/ops/control_flow_ops_internal.h" +#include "tensorflow/cc/ops/function_ops.h" +#include "tensorflow/cc/ops/sendrecv_ops.h" +#include "tensorflow/cc/ops/standard_ops.h" +#include "tensorflow/compiler/jit/deadness_analysis_internal.h" +#include "tensorflow/compiler/jit/defs.h" +#include "tensorflow/compiler/tf2xla/xla_op_kernel.h" +#include "tensorflow/compiler/tf2xla/xla_op_registry.h" +#include "tensorflow/core/common_runtime/graph_constructor.h" +#include "tensorflow/core/framework/node_def_util.h" +#include "tensorflow/core/framework/op.h" +#include "tensorflow/core/graph/algorithm.h" +#include "tensorflow/core/graph/graph_def_builder.h" +#include "tensorflow/core/lib/core/status_test_util.h" +#include "tensorflow/core/platform/test.h" + +namespace tensorflow { +namespace { + +absl::StatusOr HasInputsWithMismatchingDeadness( + const DeadnessAnalysis& deadness_analysis, const Node& n) { + std::optional pred; + for (const Edge* edge : n.in_edges()) { + TF_ASSIGN_OR_RETURN( + DeadnessAnalysis::DeadnessPredicate this_pred, + deadness_analysis.GetPredicateFor(edge->src(), edge->src_output())); + if (pred && *pred != this_pred) { + return true; + } + pred = this_pred; + } + + return false; +} + +using deadness_analysis_internal::ComputePredicates; +using deadness_analysis_internal::PredicateMapTy; + +absl::Status AnalyzeDeadness(Graph* graph, + std::unique_ptr* result) { + FixupSourceAndSinkEdges(graph); + return DeadnessAnalysis::Run(*graph, result); +} + +ops::Switch CreateSwitch(const Scope& root, const std::string& prefix) { + Output value = ops::Placeholder(root.WithOpName(prefix + "/value"), DT_FLOAT); + Output predicate = + ops::Placeholder(root.WithOpName(prefix + "/pred"), DT_BOOL); + return ops::Switch(root.WithOpName(prefix + "/switch"), value, predicate); +} + +TensorId ControlOutputFor(const Output& o) { + return {o.node()->name(), Graph::kControlSlot}; +} + +void VLogGraphIfAsked(const Graph& graph) { + if (VLOG_IS_ON(3)) { + GraphDef graph_def; + graph.ToGraphDef(&graph_def); + std::string serialized; + ::tensorflow::protobuf::TextFormat::PrintToString(graph_def, &serialized); + LOG(INFO) << serialized; + } +} + +struct InductionVarInfo { + Output induction_var; + Output loop_cond; +}; + +// Creates an induction variable with the following structure (simplified for +// brevity): +// +// +---------------+ +// | initial_value | +// +---------------+ +// | +// | +// v +// +---------------+ +// | Enter | +// +---------------+ +// | +// | +// v +// +---------------+ +// +> | Merge | -+ +// | +---------------+ | +// | | | +// | | | +// | v | +// | +---------------+ | +// | | LessThan10 | | +// | +---------------+ | +// | | | +// | | | +// | v | +// | +---------------+ | +// +----+- | Switch | <+ +// | | +---------------+ +// | | | +// | | | +// | | v +// | | +---------------+ +// | +- | AddOne | +// | +---------------+ +// | +---------------+ +// +-----> | Exit | +// +---------------+ +InductionVarInfo CreateInductionVariable(const Scope& root, + const std::string& prefix, + const std::string& frame_name, + const Output& initial_value) { + Output enter_initial_value = ops::internal::Enter( + root.WithOpName(prefix + "/enter"), initial_value, frame_name); + + ops::Merge iv(root.WithOpName(prefix + "/iv"), + {enter_initial_value, enter_initial_value}); + Output increment_by = ops::Const(root.WithOpName(prefix + "/incr"), 1); + Output final_value = ops::Const(root.WithOpName(prefix + "/final"), 10); + Output loop_cond_expr = + ops::Less(root.WithOpName(prefix + "/cond"), iv.output, final_value); + ops::Switch latch(root.WithOpName(prefix + "/latch"), iv.output, + loop_cond_expr); + ops::internal::Exit exit(root.WithOpName(prefix + "/exit"), + latch.output_false); + Output iv_next = ops::Add(root.WithOpName(prefix + "/ivnext"), + latch.output_true, increment_by); + Output next_iteration = + ops::NextIteration(root.WithOpName(prefix + "/next_iteration"), iv_next); + + CHECK(root.graph() + ->UpdateEdge(next_iteration.node(), 0, iv.output.node(), 1) + .ok()); + root.graph()->AddControlEdge(iv.output.node(), increment_by.node()); + root.graph()->AddControlEdge(iv.output.node(), final_value.node()); + + return {iv.output, loop_cond_expr}; +} + +InductionVarInfo CreateInductionVariable(const Scope& root, + const std::string& prefix, + const std::string& frame_name, + int32_t init) { + return CreateInductionVariable( + root, prefix, frame_name, + ops::Const(root.WithOpName(prefix + "/init"), init)); +} + +// Creates an induction variable with the following structure: +// +// +---------------+ +// | initial_value | +// +---------------+ +// | +// | +// v +// +---------------+ +// | Enter | +// +---------------+ +// | +// | +// v +// +---------------+ +// | Merge | <+ +// +---------------+ | +// | | +// | | +// v | +// +-----------+ +---------------+ | +// | loop_cond | --> | Switch | -+ +// +-----------+ +---------------+ +// | +// | +// v +// +---------------+ +// | Exit | +// +---------------+ +struct DependentInductionVar { + Output induction_var; + ops::Switch latch; +}; + +DependentInductionVar CreateDependentLoopInvariantValue( + const Scope& root, const std::string& prefix, const std::string& frame_name, + const Output& loop_cond, const Output& value) { + Output enter_value = ops::internal::Enter(root.WithOpName(prefix + "/enter"), + value, frame_name); + ops::Merge iv(root.WithOpName(prefix + "/iv"), {enter_value, enter_value}); + ops::Switch latch(root.WithOpName(prefix + "/latch"), iv.output, loop_cond); + ops::internal::Exit exit(root.WithOpName(prefix + "/exit"), + latch.output_false); + Output next_iteration = ops::NextIteration( + root.WithOpName(prefix + "/next_iteration"), latch.output_true); + CHECK(root.graph() + ->UpdateEdge(next_iteration.node(), 0, iv.output.node(), 1) + .ok()); + return {iv.output, latch}; +} + +DependentInductionVar CreateDependentLoopInvariantValue( + const Scope& root, const std::string& prefix, const std::string& frame_name, + const Output& loop_cond, int32_t value) { + return CreateDependentLoopInvariantValue( + root, prefix, frame_name, loop_cond, + ops::Const(root.WithOpName(prefix + "/init"), value)); +} + +TEST(DeadnessAnalysisTest, BasicPositive) { + Scope root = Scope::NewRootScope().ExitOnError(); + + ops::Switch sw = CreateSwitch(root, "0"); + Output add = + ops::Add(root.WithOpName("add"), sw.output_true, sw.output_false); + + std::unique_ptr result; + TF_ASSERT_OK(AnalyzeDeadness(root.graph(), &result)); + + TF_ASSERT_OK_AND_ASSIGN( + bool has_inputs_with_mismatching_deadness, + HasInputsWithMismatchingDeadness(*result, *add.node())); + EXPECT_TRUE(has_inputs_with_mismatching_deadness); +} + +TEST(DeadnessAnalysisTest, BasicNegative) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Output a = ops::Placeholder(root.WithOpName("a"), DT_FLOAT); + Output b = ops::Placeholder(root.WithOpName("b"), DT_FLOAT); + Output add = ops::Add(root.WithOpName("add"), a, b); + + std::unique_ptr result; + TF_ASSERT_OK(AnalyzeDeadness(root.graph(), &result)); + + TF_ASSERT_OK_AND_ASSIGN( + bool has_inputs_with_mismatching_deadness, + HasInputsWithMismatchingDeadness(*result, *add.node())); + EXPECT_FALSE(has_inputs_with_mismatching_deadness); +} + +TEST(DeadnessAnalysisTest, AndIsCommutative) { + Scope root = Scope::NewRootScope().ExitOnError(); + + ops::Switch sw_0 = CreateSwitch(root, "0"); + ops::Switch sw_1 = CreateSwitch(root, "1"); + + Output a0 = + ops::Add(root.WithOpName("a0"), sw_0.output_false, sw_1.output_false); + Output a1 = + ops::Add(root.WithOpName("a1"), sw_1.output_false, sw_0.output_false); + + Output b0 = + ops::Add(root.WithOpName("b0"), sw_0.output_false, sw_1.output_true); + Output b1 = + ops::Add(root.WithOpName("b1"), sw_1.output_true, sw_0.output_false); + + Output live0 = ops::Add(root.WithOpName("live0"), a0, a1); + Output live1 = ops::Add(root.WithOpName("live1"), b0, b1); + + Output halfdead0 = ops::Add(root.WithOpName("halfdead0"), a0, b0); + Output halfdead1 = ops::Add(root.WithOpName("halfdead1"), a1, b1); + + std::unique_ptr result; + TF_ASSERT_OK(AnalyzeDeadness(root.graph(), &result)); + + bool has_inputs_with_mismatching_deadness; + + TF_ASSERT_OK_AND_ASSIGN( + has_inputs_with_mismatching_deadness, + HasInputsWithMismatchingDeadness(*result, *live0.node())); + EXPECT_FALSE(has_inputs_with_mismatching_deadness); + + TF_ASSERT_OK_AND_ASSIGN( + has_inputs_with_mismatching_deadness, + HasInputsWithMismatchingDeadness(*result, *live1.node())); + EXPECT_FALSE(has_inputs_with_mismatching_deadness); + + TF_ASSERT_OK_AND_ASSIGN( + has_inputs_with_mismatching_deadness, + HasInputsWithMismatchingDeadness(*result, *halfdead0.node())); + EXPECT_TRUE(has_inputs_with_mismatching_deadness); + + TF_ASSERT_OK_AND_ASSIGN( + has_inputs_with_mismatching_deadness, + HasInputsWithMismatchingDeadness(*result, *halfdead1.node())); + EXPECT_TRUE(has_inputs_with_mismatching_deadness); +} + +TEST(DeadnessAnalysisTest, AndIsAssociative) { + Scope root = Scope::NewRootScope().ExitOnError(); + + ops::Switch sw_0 = CreateSwitch(root, "0"); + ops::Switch sw_1 = CreateSwitch(root, "1"); + ops::Switch sw_2 = CreateSwitch(root, "2"); + + Output a0 = + ops::Add(root.WithOpName("a0"), sw_0.output_false, sw_1.output_false); + Output a1 = ops::Add(root.WithOpName("a1"), a0, sw_2.output_false); + + Output b0 = + ops::Add(root.WithOpName("b0"), sw_1.output_false, sw_2.output_false); + Output b1 = ops::Add(root.WithOpName("b1"), sw_0.output_false, b0); + + Output add = ops::Add(root.WithOpName("add"), a1, b1); + + std::unique_ptr result; + TF_ASSERT_OK(AnalyzeDeadness(root.graph(), &result)); + + TF_ASSERT_OK_AND_ASSIGN( + bool has_inputs_with_mismatching_deadness, + HasInputsWithMismatchingDeadness(*result, *add.node())); + EXPECT_FALSE(has_inputs_with_mismatching_deadness); +} + +TEST(DeadnessAnalysisTest, OrIsCommutative) { + Scope root = Scope::NewRootScope().ExitOnError(); + + ops::Switch sw_0 = CreateSwitch(root, "0"); + ops::Switch sw_1 = CreateSwitch(root, "1"); + + ops::Merge m0(root.WithOpName("m0"), {sw_0.output_false, sw_1.output_false}); + ops::Merge m1(root.WithOpName("m1"), {sw_1.output_false, sw_0.output_false}); + ops::Merge m2(root.WithOpName("m2"), {sw_0.output_false, sw_1.output_true}); + ops::Merge m3(root.WithOpName("m3"), {sw_1.output_true, sw_0.output_false}); + + Output live0 = ops::Add(root.WithOpName("live0"), m0.output, m1.output); + Output live1 = ops::Add(root.WithOpName("live1"), m2.output, m3.output); + + Output halfdead0 = + ops::Add(root.WithOpName("halfdead0"), m0.output, m2.output); + Output halfdead1 = + ops::Add(root.WithOpName("halfdead1"), m1.output, m3.output); + + std::unique_ptr result; + TF_ASSERT_OK(AnalyzeDeadness(root.graph(), &result)); + + bool has_inputs_with_mismatching_deadness; + + TF_ASSERT_OK_AND_ASSIGN( + has_inputs_with_mismatching_deadness, + HasInputsWithMismatchingDeadness(*result, *live0.node())); + EXPECT_FALSE(has_inputs_with_mismatching_deadness); + + TF_ASSERT_OK_AND_ASSIGN( + has_inputs_with_mismatching_deadness, + HasInputsWithMismatchingDeadness(*result, *live1.node())); + EXPECT_FALSE(has_inputs_with_mismatching_deadness); + + TF_ASSERT_OK_AND_ASSIGN( + has_inputs_with_mismatching_deadness, + HasInputsWithMismatchingDeadness(*result, *halfdead0.node())); + EXPECT_TRUE(has_inputs_with_mismatching_deadness); + + TF_ASSERT_OK_AND_ASSIGN( + has_inputs_with_mismatching_deadness, + HasInputsWithMismatchingDeadness(*result, *halfdead1.node())); + EXPECT_TRUE(has_inputs_with_mismatching_deadness); +} + +TEST(DeadnessAnalysisTest, OrIsAssociative) { + Scope root = Scope::NewRootScope().ExitOnError(); + + ops::Switch sw_0 = CreateSwitch(root, "0"); + ops::Switch sw_1 = CreateSwitch(root, "1"); + ops::Switch sw_2 = CreateSwitch(root, "2"); + + ops::Merge m0(root.WithOpName("m0"), {sw_0.output_false, sw_1.output_false}); + ops::Merge m1(root.WithOpName("m1"), {m0.output, sw_2.output_false}); + ops::Merge m2(root.WithOpName("m2"), {sw_1.output_false, sw_2.output_false}); + ops::Merge m3(root.WithOpName("m3"), {sw_0.output_false, m2.output}); + + Output add = ops::Add(root.WithOpName("add"), m1.output, m3.output); + + std::unique_ptr result; + TF_ASSERT_OK(AnalyzeDeadness(root.graph(), &result)); + + TF_ASSERT_OK_AND_ASSIGN( + bool has_inputs_with_mismatching_deadness, + HasInputsWithMismatchingDeadness(*result, *add.node())); + EXPECT_FALSE(has_inputs_with_mismatching_deadness); +} + +TEST(DeadnessAnalysisTest, AndOfOr) { + Scope root = Scope::NewRootScope().ExitOnError(); + + ops::Switch sw_0 = CreateSwitch(root, "0"); + ops::Switch sw_1 = CreateSwitch(root, "1"); + ops::Switch sw_2 = CreateSwitch(root, "2"); + ops::Switch sw_3 = CreateSwitch(root, "3"); + + ops::Merge m0(root.WithOpName("m0"), {sw_0.output_false, sw_1.output_false}); + ops::Merge m1(root.WithOpName("m1"), {sw_2.output_false, sw_3.output_false}); + + Output add0 = ops::Add(root.WithOpName("add0"), m0.output, m1.output); + Output add1 = ops::Add(root.WithOpName("add1"), m0.output, m1.output); + + Output add2 = ops::Add(root.WithOpName("add2"), add0, add1); + + std::unique_ptr result; + TF_ASSERT_OK(AnalyzeDeadness(root.graph(), &result)); + + TF_ASSERT_OK_AND_ASSIGN( + bool has_inputs_with_mismatching_deadness, + HasInputsWithMismatchingDeadness(*result, *add2.node())); + EXPECT_FALSE(has_inputs_with_mismatching_deadness); +} + +TEST(DeadnessAnalysisTest, OrOfAnd) { + Scope root = Scope::NewRootScope().ExitOnError(); + + ops::Switch sw_0 = CreateSwitch(root, "0"); + ops::Switch sw_1 = CreateSwitch(root, "1"); + ops::Switch sw_2 = CreateSwitch(root, "2"); + ops::Switch sw_3 = CreateSwitch(root, "3"); + + Output add0 = + ops::Add(root.WithOpName("add0"), sw_0.output_false, sw_1.output_false); + Output add1 = + ops::Add(root.WithOpName("add1"), sw_2.output_false, sw_3.output_false); + + ops::Merge m0(root.WithOpName("m0"), {add0, add1}); + ops::Merge m1(root.WithOpName("m1"), {add0, add1}); + + Output add2 = ops::Add(root.WithOpName("add2"), m0.output, m1.output); + + std::unique_ptr result; + TF_ASSERT_OK(AnalyzeDeadness(root.graph(), &result)); + + TF_ASSERT_OK_AND_ASSIGN( + bool has_inputs_with_mismatching_deadness, + HasInputsWithMismatchingDeadness(*result, *add2.node())); + EXPECT_FALSE(has_inputs_with_mismatching_deadness); +} + +TEST(DeadnessAnalysisTest, AndOrDistributiveSimplified) { + // (*A | (~*A & ((~*B & ~*A) | (~*A & *B)))) == #true + Scope root = Scope::NewRootScope().ExitOnError(); + + ops::Switch sw_0 = CreateSwitch(root, "A"); + ops::Switch sw_1 = CreateSwitch(root, "B"); + Output add0 = + ops::Add(root.WithOpName("and0"), sw_0.output_false, sw_1.output_true); + Output add1 = + ops::Add(root.WithOpName("and1"), sw_0.output_false, sw_1.output_false); + ops::Merge or2(root.WithOpName("or2"), {add0, add1}); + Output add3 = + ops::Add(root.WithOpName("and3"), or2.output, sw_0.output_false); + ops::Merge or4(root.WithOpName("or4"), {add3, sw_0.output_true}); + + std::unique_ptr result; + TF_ASSERT_OK(AnalyzeDeadness(root.graph(), &result)); + + PredicateMapTy predicate_map; + TF_ASSERT_OK(ComputePredicates(*root.graph(), &predicate_map)); + EXPECT_EQ(predicate_map[ControlOutputFor(or4.output)], "#true"); +} + +TEST(DeadnessAnalysisTest, AndOrDistributive) { + // (A|B)&C == (A&C)|(B&C) + Scope root = Scope::NewRootScope().ExitOnError(); + + ops::Switch sw_0 = CreateSwitch(root, "0"); + ops::Switch sw_1 = CreateSwitch(root, "1"); + ops::Switch sw_2 = CreateSwitch(root, "2"); + + ops::Merge m0(root.WithOpName("m0"), {sw_0.output_false, sw_1.output_false}); + Output add0 = ops::Add(root.WithOpName("add0"), m0.output, sw_2.output_false); + + Output add1 = + ops::Add(root.WithOpName("add1"), sw_0.output_false, sw_2.output_false); + Output add2 = + ops::Add(root.WithOpName("add2"), sw_1.output_false, sw_2.output_false); + ops::Merge m1(root.WithOpName("m1"), {add1, add2}); + + Output add3 = ops::Add(root.WithOpName("add3"), add0, m1.output); + + std::unique_ptr result; + TF_ASSERT_OK(AnalyzeDeadness(root.graph(), &result)); + + TF_ASSERT_OK_AND_ASSIGN( + bool has_inputs_with_mismatching_deadness, + HasInputsWithMismatchingDeadness(*result, *add3.node())); + EXPECT_FALSE(has_inputs_with_mismatching_deadness); +} + +TEST(DeadnessAnalysisTest, Ternary) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Output predicate = ops::Placeholder(root.WithOpName("predicate"), DT_BOOL); + Output true_value = ops::Placeholder(root.WithOpName("true_value"), DT_FLOAT); + Output false_value = + ops::Placeholder(root.WithOpName("false_value"), DT_FLOAT); + + ops::Switch predicated_true(root.WithOpName("predicated_true"), true_value, + predicate); + + ops::Switch predicated_false(root.WithOpName("predicated_false"), true_value, + predicate); + ops::Merge merge(root.WithOpName("ternary"), {predicated_true.output_true, + predicated_false.output_false}); + Output addend = ops::Placeholder(root.WithOpName("addend"), DT_FLOAT); + Output add = ops::Add(root.WithOpName("add"), merge.output, addend); + + std::unique_ptr result; + TF_ASSERT_OK(AnalyzeDeadness(root.graph(), &result)); + + TF_ASSERT_OK_AND_ASSIGN( + bool has_inputs_with_mismatching_deadness, + HasInputsWithMismatchingDeadness(*result, *add.node())); + EXPECT_FALSE(has_inputs_with_mismatching_deadness); +} + +TEST(DeadnessAnalysisTest, Recv) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Output recv_a = ops::_Recv(root.WithOpName("recv_a"), DT_FLOAT, "tensor_a", + "sender", 0, "receiver"); + Output recv_b = ops::_Recv(root.WithOpName("recv_b"), DT_FLOAT, "tensor_b", + "sender", 0, "receiver"); + Output add = ops::Add(root.WithOpName("add"), recv_a, recv_b); + + std::unique_ptr result; + TF_ASSERT_OK(AnalyzeDeadness(root.graph(), &result)); + + TF_ASSERT_OK_AND_ASSIGN( + bool has_inputs_with_mismatching_deadness, + HasInputsWithMismatchingDeadness(*result, *add.node())); + EXPECT_TRUE(has_inputs_with_mismatching_deadness); +} + +TEST(DeadnessAnalysisTest, HostRecv) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Output recv_a = ops::_HostRecv(root.WithOpName("recv_a"), DT_FLOAT, + "tensor_a", "sender", 0, "receiver"); + Output recv_b = ops::_HostRecv(root.WithOpName("recv_b"), DT_FLOAT, + "tensor_b", "sender", 0, "receiver"); + Output add = ops::Add(root.WithOpName("add"), recv_a, recv_b); + + std::unique_ptr result; + TF_ASSERT_OK(AnalyzeDeadness(root.graph(), &result)); + + TF_ASSERT_OK_AND_ASSIGN( + bool has_inputs_with_mismatching_deadness, + HasInputsWithMismatchingDeadness(*result, *add.node())); + EXPECT_TRUE(has_inputs_with_mismatching_deadness); +} + +TEST(DeadnessAnalysisTest, Loop) { + Scope root = Scope::NewRootScope().ExitOnError(); + Output iv0 = CreateInductionVariable(root, "iv0", "fr0", 0).induction_var; + Output iv1 = CreateInductionVariable(root, "iv1", "fr0", 0).induction_var; + Output iv2 = CreateInductionVariable(root, "iv2", "fr0", 1).induction_var; + Output add0 = ops::Add(root.WithOpName("add0"), iv0, iv1); + Output add1 = ops::Add(root.WithOpName("add1"), iv1, iv2); + + // NB! iv0 and iv1 are equivalent and a smarter deadness analysis would have + // noticed that. Today we are pessimistic here because we assign an + // uninterpreted symbol to merges with backedges. + + VLogGraphIfAsked(*root.graph()); + + { + std::unique_ptr result; + TF_ASSERT_OK(AnalyzeDeadness(root.graph(), &result)); + + bool has_inputs_with_mismatching_deadness; + + TF_ASSERT_OK_AND_ASSIGN( + has_inputs_with_mismatching_deadness, + HasInputsWithMismatchingDeadness(*result, *add0.node())); + EXPECT_TRUE(has_inputs_with_mismatching_deadness); + + TF_ASSERT_OK_AND_ASSIGN( + has_inputs_with_mismatching_deadness, + HasInputsWithMismatchingDeadness(*result, *add1.node())); + EXPECT_TRUE(has_inputs_with_mismatching_deadness); + } + { + PredicateMapTy predicate_map; + TF_ASSERT_OK(ComputePredicates(*root.graph(), &predicate_map)); + + // In theory we should be able to tell that iv0/cond:0 and iv1/cond:0 + // produce the same deadness. But we're not that smart today. + EXPECT_EQ(predicate_map[ControlOutputFor(iv0)], + "{#true,&,*iv0/cond:0}"); + EXPECT_EQ(predicate_map[ControlOutputFor(iv1)], + "{#true,&,*iv1/cond:0}"); + EXPECT_EQ(predicate_map[ControlOutputFor(iv2)], + "{#true,&,*iv2/cond:0}"); + EXPECT_EQ(predicate_map[ControlOutputFor(add0)], + "({#true,&,*iv0/cond:0} & {#true,&,*iv1/cond:0})"); + EXPECT_EQ(predicate_map[ControlOutputFor(add1)], + "({#true,&,*iv1/cond:0} & {#true,&,*iv2/cond:0})"); + } +} + +TEST(DeadnessAnalysisTest, ControlEquivalentLoopBodies) { + Scope root = Scope::NewRootScope().ExitOnError(); + InductionVarInfo iv = CreateInductionVariable(root, "iv0", "loop", 0); + Output dependent_iv0 = + CreateDependentLoopInvariantValue(root, "div0", "loop", iv.loop_cond, 0) + .induction_var; + Output dependent_iv1 = + CreateDependentLoopInvariantValue(root, "div1", "loop", iv.loop_cond, 0) + .induction_var; + Output add0 = ops::Add(root.WithOpName("add0"), dependent_iv0, dependent_iv1); + + VLogGraphIfAsked(*root.graph()); + + { + std::unique_ptr result; + TF_ASSERT_OK(AnalyzeDeadness(root.graph(), &result)); + + TF_ASSERT_OK_AND_ASSIGN( + bool has_inputs_with_mismatching_deadness, + HasInputsWithMismatchingDeadness(*result, *add0.node())); + EXPECT_FALSE(has_inputs_with_mismatching_deadness); + } + { + PredicateMapTy predicate_map; + TF_ASSERT_OK(ComputePredicates(*root.graph(), &predicate_map, + /*enable_optimistic=*/true)); + + EXPECT_EQ(predicate_map[ControlOutputFor(iv.induction_var)], + "{#true,&,*iv0/cond:0}"); + EXPECT_EQ(predicate_map[ControlOutputFor(dependent_iv0)], + predicate_map[ControlOutputFor(iv.induction_var)]); + EXPECT_EQ(predicate_map[ControlOutputFor(dependent_iv1)], + predicate_map[ControlOutputFor(iv.induction_var)]); + EXPECT_EQ(predicate_map[ControlOutputFor(add0)], + predicate_map[ControlOutputFor(iv.induction_var)]); + } + { + PredicateMapTy predicate_map; + TF_ASSERT_OK(ComputePredicates(*root.graph(), &predicate_map, + /*enable_optimistic=*/false)); + + EXPECT_EQ(predicate_map[ControlOutputFor(iv.induction_var)], + "{#true,&,*iv0/cond:0}"); + EXPECT_EQ(predicate_map[ControlOutputFor(dependent_iv0)], + "{#true,&,(iv0/iv:0 & *iv0/cond:0)}"); + EXPECT_EQ(predicate_map[ControlOutputFor(dependent_iv1)], + "{#true,&,(iv0/iv:0 & *iv0/cond:0)}"); + EXPECT_EQ(predicate_map[ControlOutputFor(add0)], + "{#true,&,(iv0/iv:0 & *iv0/cond:0)}"); + } +} + +TEST(DeadnessAnalysisTest, LoopInvariantPredicateOnBackedge) { + // Create a merge that "looks like" a loop but isn't really. It has a value + // that does not depend on the merge on its backedge. + Scope root = Scope::NewRootScope().ExitOnError(); + InductionVarInfo iv = CreateInductionVariable(root, "iv0", "frame", 0); + DependentInductionVar dependent_iv = + CreateDependentLoopInvariantValue(root, "div0", "frame", iv.loop_cond, 0); + FixupSourceAndSinkEdges(root.graph()); + + TF_ASSERT_OK(root.graph()->UpdateEdge( + iv.induction_var.node(), 0, dependent_iv.latch.output_true.node(), 0)); + + VLogGraphIfAsked(*root.graph()); + + { + PredicateMapTy predicate_map; + TF_ASSERT_OK(ComputePredicates(*root.graph(), &predicate_map, + /*enable_optimistic=*/true)); + + EXPECT_EQ(predicate_map[ControlOutputFor(dependent_iv.induction_var)], + "{#true,&,*iv0/cond:0}"); + } + { + PredicateMapTy predicate_map; + TF_ASSERT_OK(ComputePredicates(*root.graph(), &predicate_map, + /*enable_optimistic=*/false)); + + EXPECT_EQ(predicate_map[ControlOutputFor(dependent_iv.induction_var)], + "div0/iv:0"); + } +} + +TEST(DeadnessAnalysisTest, ControlEquivalentNestedLoopBodies) { + Scope root = Scope::NewRootScope().ExitOnError(); + InductionVarInfo iv_outer = + CreateInductionVariable(root, "iv_outer", "outer_loop", 0); + Output enter_constant_outer_loop = ops::internal::Enter( + root.WithOpName("constant_enter_outer_loop"), + ops::Const(root.WithOpName("constant"), 5), "outer_loop", + ops::internal::Enter::Attrs().IsConstant(true)); + ops::Switch inner_value(root.WithOpName("outer_is_live"), + enter_constant_outer_loop, iv_outer.loop_cond); + InductionVarInfo iv_inner = CreateInductionVariable( + root, "iv_inner", "inner_loop", inner_value.output_true); + + Output dependent_outer_iv0 = + CreateDependentLoopInvariantValue(root, "dependent_outer_iv0", + "outer_loop", iv_outer.loop_cond, 0) + .induction_var; + Output dependent_outer_iv1 = + CreateDependentLoopInvariantValue(root, "dependent_outer_iv1", + "outer_loop", iv_outer.loop_cond, 0) + .induction_var; + + Output dependent_inner_iv0 = CreateDependentLoopInvariantValue( + root, "dependent_inner_iv0", "inner_loop", + iv_inner.loop_cond, dependent_outer_iv0) + .induction_var; + Output dependent_inner_iv1 = CreateDependentLoopInvariantValue( + root, "dependent_inner_iv1", "inner_loop", + iv_inner.loop_cond, dependent_outer_iv1) + .induction_var; + + Output add0 = ops::Add(root.WithOpName("add0"), dependent_inner_iv0, + dependent_inner_iv1); + + VLogGraphIfAsked(*root.graph()); + + { + std::unique_ptr result; + TF_ASSERT_OK(AnalyzeDeadness(root.graph(), &result)); + + TF_ASSERT_OK_AND_ASSIGN( + bool has_inputs_with_mismatching_deadness, + HasInputsWithMismatchingDeadness(*result, *add0.node())); + EXPECT_FALSE(has_inputs_with_mismatching_deadness); + } + { + PredicateMapTy predicate_map; + TF_ASSERT_OK(ComputePredicates(*root.graph(), &predicate_map, + /*enable_optimistic=*/true)); + + EXPECT_EQ(predicate_map[ControlOutputFor(iv_outer.induction_var)], + "{#true,&,*iv_outer/cond:0}"); + EXPECT_EQ(predicate_map[ControlOutputFor(iv_inner.induction_var)], + "{(*iv_outer/cond:0 & " + "{#true,&,*iv_outer/cond:0}),&,*iv_inner/" + "cond:0}"); + + // enable_optimistic = true or not should produce the same results because + // of fallback. However, note that the order of iv_inner/cond:0 and + // iv_inner/iv:0 is different because the optimistic approach does not + // create predicates for all merges and it can change the predicate id and + // hence the symbol order. + EXPECT_EQ(predicate_map[ControlOutputFor(dependent_inner_iv0)], + "{{#true,&,(iv_outer/iv:0 & " + "*iv_outer/cond:0)},&,(*iv_inner/cond:0 & " + "iv_inner/iv:0)}"); + EXPECT_EQ(predicate_map[ControlOutputFor(dependent_inner_iv1)], + predicate_map[ControlOutputFor(dependent_inner_iv0)]); + EXPECT_EQ(predicate_map[ControlOutputFor(add0)], + predicate_map[ControlOutputFor(dependent_inner_iv0)]); + } + { + PredicateMapTy predicate_map; + TF_ASSERT_OK(ComputePredicates(*root.graph(), &predicate_map, + /*enable_optimistic=*/false)); + + EXPECT_EQ(predicate_map[ControlOutputFor(iv_outer.induction_var)], + "{#true,&,*iv_outer/cond:0}"); + EXPECT_EQ(predicate_map[ControlOutputFor(iv_inner.induction_var)], + "{(*iv_outer/cond:0 & " + "{#true,&,*iv_outer/cond:0}),&,*iv_inner/" + "cond:0}"); + + EXPECT_EQ(predicate_map[ControlOutputFor(dependent_inner_iv0)], + "{{#true,&,(iv_outer/iv:0 & " + "*iv_outer/cond:0)},&,(iv_inner/iv:0 & " + "*iv_inner/cond:0)}"); + EXPECT_EQ(predicate_map[ControlOutputFor(dependent_inner_iv1)], + predicate_map[ControlOutputFor(dependent_inner_iv0)]); + EXPECT_EQ(predicate_map[ControlOutputFor(add0)], + predicate_map[ControlOutputFor(dependent_inner_iv0)]); + } +} + +TEST(DeadnessAnalysisTest, ControlNonEquivalentNestedLoopBodies) { + Scope root = Scope::NewRootScope().ExitOnError(); + + std::array outer_iv; + std::array inner_iv; + + for (int i : {0, 1}) { + InductionVarInfo iv_outer = + CreateInductionVariable(root, "iv_outer", "outer_loop", 0); + Output enter_constant_outer_loop = ops::internal::Enter( + root.WithOpName("constant_enter_outer_loop"), + ops::Const(root.WithOpName("constant"), 5), "outer_loop", + ops::internal::Enter::Attrs().IsConstant(true)); + ops::Switch inner_value(root.WithOpName("outer_is_live"), + enter_constant_outer_loop, iv_outer.loop_cond); + InductionVarInfo iv_inner = CreateInductionVariable( + root, "iv_inner", "inner_loop", inner_value.output_true); + + outer_iv[i] = iv_outer.induction_var; + inner_iv[i] = iv_inner.induction_var; + } + + Output add0 = ops::Add(root.WithOpName("add0"), inner_iv[0], inner_iv[1]); + + VLogGraphIfAsked(*root.graph()); + + { + std::unique_ptr result; + TF_ASSERT_OK(AnalyzeDeadness(root.graph(), &result)); + + TF_ASSERT_OK_AND_ASSIGN( + bool has_inputs_with_mismatching_deadness, + HasInputsWithMismatchingDeadness(*result, *add0.node())); + EXPECT_TRUE(has_inputs_with_mismatching_deadness); + } + + { + PredicateMapTy predicate_map; + TF_ASSERT_OK(ComputePredicates(*root.graph(), &predicate_map)); + + EXPECT_EQ(predicate_map[ControlOutputFor(outer_iv[0])], + "{#true,&,*iv_outer/cond:0}"); + EXPECT_EQ(predicate_map[ControlOutputFor(inner_iv[0])], + "{(*iv_outer/cond:0 & " + "{#true,&,*iv_outer/cond:0}),&,*iv_inner/" + "cond:0}"); + EXPECT_EQ(predicate_map[ControlOutputFor(outer_iv[1])], + "{#true,&,*iv_outer/cond_1:0}"); + EXPECT_EQ(predicate_map[ControlOutputFor(inner_iv[1])], + "{(*iv_outer/cond_1:0 & " + "{#true,&,*iv_outer/cond_1:0}),&,*iv_inner/" + "cond_1:0}"); + EXPECT_EQ(predicate_map[ControlOutputFor(add0)], + "({(*iv_outer/cond:0 & " + "{#true,&,*iv_outer/cond:0}),&,*iv_inner/" + "cond:0} & {(*iv_outer/cond_1:0 & " + "{#true,&,*iv_outer/cond_1:0}),&,*iv_inner/" + "cond_1:0})"); + } +} + +TEST(DeadnessAnalysisTest, NestedLoopBodiesWithACapture) { + Scope root = Scope::NewRootScope().ExitOnError(); + InductionVarInfo iv_outer = + CreateInductionVariable(root, "iv_outer", "outer_loop", 0); + Output enter_constant_outer_loop = ops::internal::Enter( + root.WithOpName("constant_enter_outer_loop"), + ops::Const(root.WithOpName("constant"), 5), "outer_loop", + ops::internal::Enter::Attrs().IsConstant(true)); + ops::Switch inner_value(root.WithOpName("outer_is_live"), + enter_constant_outer_loop, iv_outer.loop_cond); + InductionVarInfo iv_inner = CreateInductionVariable( + root, "iv_inner", "inner_loop", inner_value.output_true); + + DependentInductionVar div0_outer = CreateDependentLoopInvariantValue( + root, "div0_outer", "outer_loop", iv_outer.loop_cond, 0); + DependentInductionVar div1_outer = CreateDependentLoopInvariantValue( + root, "div1_outer", "outer_loop", iv_outer.loop_cond, 0); + + DependentInductionVar div0_inner = CreateDependentLoopInvariantValue( + root, "div0_inner", "inner_loop", iv_inner.loop_cond, + div0_outer.induction_var); + DependentInductionVar div1_inner = CreateDependentLoopInvariantValue( + root, "div1_inner", "inner_loop", iv_inner.loop_cond, + div1_outer.induction_var); + + Output captured = ops::_Recv(root.WithOpName("captured"), DT_INT32, + "tensor_a", "sender", 0, "receiver"); + Output capture_enter_outer = ops::internal::Enter( + root.WithOpName("capture_enter_outer"), captured, "outer_loop", + ops::internal::Enter::Attrs().IsConstant(true)); + Output capture_enter_inner = ops::internal::Enter( + root.WithOpName("capture_enter_inner"), capture_enter_outer, "inner_loop", + ops::internal::Enter::Attrs().IsConstant(true)); + Output mul0 = ops::Mul(root.WithOpName("mul0"), div1_inner.induction_var, + capture_enter_inner); + TF_ASSERT_OK(root.graph()->UpdateEdge( + mul0.node(), 0, div1_inner.latch.output_true.node(), 0)); + + Output add0 = ops::Add(root.WithOpName("add0"), div0_inner.induction_var, + div1_inner.induction_var); + + VLogGraphIfAsked(*root.graph()); + + { + std::unique_ptr result; + TF_ASSERT_OK(AnalyzeDeadness(root.graph(), &result)); + + TF_ASSERT_OK_AND_ASSIGN( + bool has_inputs_with_mismatching_deadness, + HasInputsWithMismatchingDeadness(*result, *add0.node())); + EXPECT_TRUE(has_inputs_with_mismatching_deadness); + } +} + +TEST(DeadnessAnalysisTest, CyclicRecurrence) { + Scope root = Scope::NewRootScope().ExitOnError(); + InductionVarInfo iv = CreateInductionVariable(root, "iv0", "loop", 0); + DependentInductionVar div0 = + CreateDependentLoopInvariantValue(root, "div0", "loop", iv.loop_cond, 0); + DependentInductionVar div1 = + CreateDependentLoopInvariantValue(root, "div1", "loop", iv.loop_cond, 0); + FixupSourceAndSinkEdges(root.graph()); + TF_ASSERT_OK(root.graph()->UpdateEdge(div1.induction_var.node(), 0, + div0.latch.output_true.node(), 0)); + TF_ASSERT_OK(root.graph()->UpdateEdge(div0.induction_var.node(), 0, + div1.latch.output_true.node(), 0)); + + VLogGraphIfAsked(*root.graph()); + + { + PredicateMapTy predicate_map; + TF_ASSERT_OK(ComputePredicates(*root.graph(), &predicate_map, + /*enable_optimistic=*/true)); + + EXPECT_EQ(predicate_map[ControlOutputFor(iv.induction_var)], + "{#true,&,*iv0/cond:0}"); + EXPECT_EQ(predicate_map[ControlOutputFor(div0.induction_var)], + "{#true,&,*iv0/cond:0}"); + EXPECT_EQ(predicate_map[ControlOutputFor(div1.induction_var)], + "{#true,&,*iv0/cond:0}"); + + // This tests the rule {S,&,X} & ~X => S. + TensorId switch_false_out = {div1.latch.output_false.node()->name(), + div1.latch.output_false.index()}; + EXPECT_EQ(predicate_map[switch_false_out], "(#true)"); + } + { + PredicateMapTy predicate_map; + TF_ASSERT_OK(ComputePredicates(*root.graph(), &predicate_map, + /*enable_optimistic=*/false)); + + EXPECT_EQ(predicate_map[ControlOutputFor(iv.induction_var)], + "{#true,&,*iv0/cond:0}"); + EXPECT_EQ(predicate_map[ControlOutputFor(div0.induction_var)], "div0/iv:0"); + EXPECT_EQ(predicate_map[ControlOutputFor(div1.induction_var)], "div1/iv:0"); + } +} + +TEST(DeadnessAnalysisTest, AndRecurrenceNeedsFrameName) { + Scope root = Scope::NewRootScope().ExitOnError(); + InductionVarInfo iv_0 = CreateInductionVariable(root, "iv_0", "frame_0", 10); + InductionVarInfo iv_1 = CreateInductionVariable(root, "iv_1", "frame_1", 9); + + Output init = CreateSwitch(root, "init").output_true; + Output step = CreateSwitch(root, "step").output_true; + + std::array exits; + std::array next_iterations; + + for (int i : {0, 1}) { + Output init_enter = ops::internal::Enter( + root.WithOpName(absl::StrCat("init_enter_frame_", i)), init, + absl::StrCat("frame_", i), + ops::internal::Enter::Attrs().IsConstant(true)); + Output step_enter = ops::internal::Enter( + root.WithOpName(absl::StrCat("step_enter_frame_", i)), step, + absl::StrCat("frame_", i), + ops::internal::Enter::Attrs().IsConstant(true)); + + ops::Merge iv(root.WithOpName(absl::StrCat("expr_", i)), + {init_enter, init_enter}); + Output add = ops::Add(root.WithOpName(absl::StrCat("add_", i)), iv.output, + step_enter); + next_iterations[i] = ops::NextIteration( + root.WithOpName(absl::StrCat("expr_", i, "_next_iteration")), add); + EXPECT_TRUE( + root.graph() + ->UpdateEdge(next_iterations[i].node(), 0, iv.output.node(), 1) + .ok()); + exits[i] = ops::internal::Exit(root.WithOpName(absl::StrCat("exit_", i)), + iv.output); + } + + FixupSourceAndSinkEdges(root.graph()); + + { + PredicateMapTy predicate_map; + TF_ASSERT_OK(ComputePredicates(*root.graph(), &predicate_map)); + + EXPECT_NE(predicate_map[ControlOutputFor(exits[0])], + predicate_map[ControlOutputFor(exits[1])]); + EXPECT_NE(predicate_map[ControlOutputFor(exits[0])], ""); + EXPECT_NE(predicate_map[ControlOutputFor(exits[1])], ""); + + EXPECT_NE(predicate_map[ControlOutputFor(next_iterations[0])], + predicate_map[ControlOutputFor(next_iterations[1])]); + EXPECT_NE(predicate_map[ControlOutputFor(next_iterations[0])], ""); + EXPECT_NE(predicate_map[ControlOutputFor(next_iterations[1])], ""); + } +} + +TEST(DeadnessAnalysisTest, ControlInputs) { + Scope root = Scope::NewRootScope().ExitOnError(); + ops::Switch sw = CreateSwitch(root, "0"); + + Output id0 = ops::Identity(root.WithOpName("id0"), sw.output_false); + Output id1 = ops::Identity(root.WithOpName("id1"), sw.output_true); + + Output const0 = ops::Const(root.WithOpName("const0"), 1); + Output const1 = ops::Const(root.WithOpName("const1"), 2); + + Output add = ops::Add(root.WithOpName("add"), const0, const1); + + root.graph()->AddControlEdge(id0.node(), const0.node()); + root.graph()->AddControlEdge(id1.node(), const1.node()); + + std::unique_ptr result; + TF_ASSERT_OK(AnalyzeDeadness(root.graph(), &result)); + + TF_ASSERT_OK_AND_ASSIGN( + bool has_inputs_with_mismatching_deadness, + HasInputsWithMismatchingDeadness(*result, *add.node())); + EXPECT_TRUE(has_inputs_with_mismatching_deadness); +} + +TEST(DeadnessAnalysisTest, ControlTrigger) { + Scope root = Scope::NewRootScope().ExitOnError(); + ops::Switch sw = CreateSwitch(root, "0"); + + Output id0 = ops::Identity(root.WithOpName("id0"), sw.output_false); + Output id1 = ops::Identity(root.WithOpName("id1"), sw.output_true); + + ops::ControlTrigger ctrl_trigger0(root.WithOpName("ctrl_trigger0")); + ops::ControlTrigger ctrl_trigger1(root.WithOpName("ctrl_trigger1")); + + Output const0 = ops::Const(root.WithOpName("const0"), 1); + Output const1 = ops::Const(root.WithOpName("const1"), 2); + + Output add = ops::Add(root.WithOpName("add"), const0, const1); + + root.graph()->AddControlEdge(id0.node(), ctrl_trigger0.operation.node()); + root.graph()->AddControlEdge(ctrl_trigger0.operation.node(), const0.node()); + + root.graph()->AddControlEdge(id1.node(), ctrl_trigger1.operation.node()); + root.graph()->AddControlEdge(ctrl_trigger1.operation.node(), const1.node()); + + std::unique_ptr result; + TF_ASSERT_OK(AnalyzeDeadness(root.graph(), &result)); + + TF_ASSERT_OK_AND_ASSIGN( + bool has_inputs_with_mismatching_deadness, + HasInputsWithMismatchingDeadness(*result, *add.node())); + EXPECT_FALSE(has_inputs_with_mismatching_deadness); +} + +TEST(DeadnessAnalysisTest, ControlInputsToMerge) { + Scope root = Scope::NewRootScope().ExitOnError(); + ops::Switch sw = CreateSwitch(root, "0"); + + Output id0 = ops::Identity(root.WithOpName("id0"), sw.output_false); + Output id1 = ops::Identity(root.WithOpName("id1"), sw.output_true); + + Output constant = ops::Const(root.WithOpName("constant"), 5); + ops::Merge m0(root.WithOpName("m0"), {constant}); + ops::Merge m1(root.WithOpName("m0"), {constant}); + Output add = ops::Add(root.WithOpName("add"), m0.output, m1.output); + + root.graph()->AddControlEdge(id0.node(), m0.output.node()); + root.graph()->AddControlEdge(id1.node(), m1.output.node()); + + std::unique_ptr result; + TF_ASSERT_OK(AnalyzeDeadness(root.graph(), &result)); + + TF_ASSERT_OK_AND_ASSIGN( + bool has_inputs_with_mismatching_deadness, + HasInputsWithMismatchingDeadness(*result, *add.node())); + EXPECT_FALSE(has_inputs_with_mismatching_deadness); +} + +TEST(DeadnessAnalysisTest, RecvVsSwitch) { + // Demonstrates why we need the must_be_true bit on SymbolP. + Scope root = Scope::NewRootScope().ExitOnError(); + + Output recv = ops::_Recv(root.WithOpName("recv"), DT_BOOL, "tensor", "sender", + 0, "receiver"); + Output value = ops::Placeholder(root.WithOpName("value"), DT_BOOL); + ops::Switch sw(root.WithOpName("switch"), value, recv); + Output logical_and = + ops::LogicalAnd(root.WithOpName("and"), recv, sw.output_true); + + std::unique_ptr result; + TF_ASSERT_OK(AnalyzeDeadness(root.graph(), &result)); + + TF_ASSERT_OK_AND_ASSIGN( + bool has_inputs_with_mismatching_deadness, + HasInputsWithMismatchingDeadness(*result, *logical_and.node())); + EXPECT_TRUE(has_inputs_with_mismatching_deadness); +} + +TEST(DeadnessAnalysisTest, RecvVsSwitchText) { + // Demonstrates why we need the must_be_true bit on SymbolP. + Scope root = Scope::NewRootScope().ExitOnError(); + + Output recv = ops::_Recv(root.WithOpName("recv"), DT_BOOL, "tensor", "sender", + 0, "receiver"); + Output value = ops::Placeholder(root.WithOpName("value"), DT_BOOL); + ops::Switch sw(root.WithOpName("switch"), value, recv); + Output logical_and = + ops::LogicalAnd(root.WithOpName("and"), recv, sw.output_true); + + std::unique_ptr result; + TF_ASSERT_OK(AnalyzeDeadness(root.graph(), &result)); + + PredicateMapTy predicate_map; + TF_ASSERT_OK(ComputePredicates(*root.graph(), &predicate_map)); + + TensorId logical_and_output_0 = {logical_and.node()->name(), + Graph::kControlSlot}; + EXPECT_EQ(predicate_map[logical_and_output_0], "(recv:0 & *recv:0)"); +} + +TEST(DeadnessAnalysisTest, DeMorgan) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Output cond_0 = ops::Placeholder(root.WithOpName("cond_0"), DT_BOOL); + Output cond_1 = ops::Placeholder(root.WithOpName("cond_1"), DT_BOOL); + Output value = ops::Placeholder(root.WithOpName("value"), DT_FLOAT); + + ops::Switch sw_0(root.WithOpName("switch_0"), value, cond_0); + ops::Switch sw_1(root.WithOpName("switch_1"), value, cond_1); + + Output and_0_1 = + ops::Add(root.WithOpName("and_0_1"), sw_0.output_true, sw_1.output_true); + + Output or_not0_not1 = ops::Merge(root.WithOpName("or_not0_not1"), + {sw_0.output_false, sw_1.output_false}) + .output; + + // Predicate(should_always_be_dead) = + // (A & B) & (~A | ~B) = (A & B) & ~(A & B) = False + Output should_always_be_dead = + ops::Add(root.WithOpName("should_always_be_dead"), and_0_1, or_not0_not1); + + // Predicate(should_always_be_dead) = + // (A & B) | (~A | ~B) = (A & B) | ~(A & B) = True + Output should_always_be_alive = + ops::Merge(root.WithOpName("should_always_be_alive"), + {and_0_1, or_not0_not1}) + .output; + + std::unique_ptr result; + TF_ASSERT_OK(AnalyzeDeadness(root.graph(), &result)); + + PredicateMapTy predicate_map; + TF_ASSERT_OK(ComputePredicates(*root.graph(), &predicate_map)); + + EXPECT_EQ(predicate_map[ControlOutputFor(should_always_be_dead)], "#false"); + EXPECT_EQ(predicate_map[ControlOutputFor(should_always_be_alive)], "#true"); +} + +TEST(DeadnessAnalysisTest, ConstantTrueSwitchCondition) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Output constant_true = ops::Const(root.WithOpName("const_true"), true); + Output value = ops::Placeholder(root.WithOpName("value"), DT_FLOAT); + ops::Switch sw(root.WithOpName("switch"), value, constant_true); + + Output id_false = ops::Identity(root.WithOpName("id_false"), sw.output_false); + Output id_true = ops::Identity(root.WithOpName("id_true"), sw.output_true); + + FixupSourceAndSinkEdges(root.graph()); + + PredicateMapTy predicate_map; + TF_ASSERT_OK(ComputePredicates(*root.graph(), &predicate_map)); + + EXPECT_EQ(predicate_map[ControlOutputFor(id_false)], "#false"); + EXPECT_EQ(predicate_map[ControlOutputFor(id_true)], "#true"); +} + +TEST(DeadnessAnalysisTest, ConstantFalseSwitchCondition) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Output constant_false = ops::Const(root.WithOpName("const_false"), false); + Output value = ops::Placeholder(root.WithOpName("value"), DT_FLOAT); + ops::Switch sw(root.WithOpName("switch"), value, constant_false); + + Output id_false = ops::Identity(root.WithOpName("id_false"), sw.output_false); + Output id_true = ops::Identity(root.WithOpName("id_true"), sw.output_true); + + FixupSourceAndSinkEdges(root.graph()); + + PredicateMapTy predicate_map; + TF_ASSERT_OK(ComputePredicates(*root.graph(), &predicate_map)); + + EXPECT_EQ(predicate_map[ControlOutputFor(id_false)], "#true"); + EXPECT_EQ(predicate_map[ControlOutputFor(id_true)], "#false"); +} + +TEST(DeadnessAnalysisTest, RefBoolSwitchCondition) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Output condition_ref_var = + ops::Variable(root.WithOpName("cond_ref"), TensorShape({}), DT_BOOL); + Output value = ops::Placeholder(root.WithOpName("value"), DT_FLOAT); + ops::Switch sw(root.WithOpName("switch"), value, condition_ref_var); + + Output id_false = ops::Identity(root.WithOpName("id_false"), sw.output_false); + Output id_true = ops::Identity(root.WithOpName("id_true"), sw.output_true); + + FixupSourceAndSinkEdges(root.graph()); + + PredicateMapTy predicate_map; + TF_ASSERT_OK(ComputePredicates(*root.graph(), &predicate_map)); + + EXPECT_EQ(predicate_map[ControlOutputFor(id_false)], "~*cond_ref:0"); + EXPECT_EQ(predicate_map[ControlOutputFor(id_true)], "*cond_ref:0"); +} + +void CreateSwitchN(const Scope& scope, Input data, Input output_index, + int64_t num_outs, OutputList* outputs) { + if (!scope.ok()) return; + auto _data = ops::AsNodeOut(scope, data); + if (!scope.ok()) return; + auto _output_index = ops::AsNodeOut(scope, output_index); + if (!scope.ok()) return; + Node* ret; + const auto unique_name = scope.GetUniqueNameForOp("_SwitchN"); + auto builder = NodeBuilder(unique_name, "_SwitchN") + .Input(_data) + .Input(_output_index) + .Attr("num_outs", num_outs); + scope.UpdateBuilder(&builder); + scope.UpdateStatus(builder.Finalize(scope.graph(), &ret)); + if (!scope.ok()) return; + scope.UpdateStatus(scope.DoShapeInference(ret)); + for (int32_t i = 0; i < ret->num_outputs(); ++i) { + outputs->push_back(Output(ret, i)); + } +} + +TEST(DeadnessAnalysisTest, Constant1_SwitchN_2Branches_DoesNotFail) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Output constant_1 = ops::Const(root.WithOpName("const_1"), 1); + Output value = ops::Placeholder(root.WithOpName("value"), DT_FLOAT); + OutputList outputs; + CreateSwitchN(root.WithOpName("switchn"), value, constant_1, 2, &outputs); + + Output id_0 = ops::Identity(root.WithOpName("id_0"), outputs[0]); + Output id_1 = ops::Identity(root.WithOpName("id_1"), outputs[1]); + + FixupSourceAndSinkEdges(root.graph()); + + PredicateMapTy predicate_map; + TF_ASSERT_OK(ComputePredicates(*root.graph(), &predicate_map)); + + EXPECT_EQ(predicate_map[ControlOutputFor(id_0)], "#false"); + EXPECT_EQ(predicate_map[ControlOutputFor(id_1)], "#true"); +} + +TEST(DeadnessAnalysisTest, Constant7_SwitchN_3Branches) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Output constant_7 = ops::Const(root.WithOpName("const_7"), 7); + Output value = ops::Placeholder(root.WithOpName("value"), DT_FLOAT); + OutputList outputs; + CreateSwitchN(root.WithOpName("switchn"), value, constant_7, 3, &outputs); + + Output id_0 = ops::Identity(root.WithOpName("id_0"), outputs[0]); + Output id_1 = ops::Identity(root.WithOpName("id_1"), outputs[1]); + Output id_2 = ops::Identity(root.WithOpName("id_2"), outputs[2]); + + FixupSourceAndSinkEdges(root.graph()); + + PredicateMapTy predicate_map; + TF_ASSERT_OK(ComputePredicates(*root.graph(), &predicate_map)); + + EXPECT_EQ(predicate_map[ControlOutputFor(id_0)], "#false"); + EXPECT_EQ(predicate_map[ControlOutputFor(id_1)], "#false"); + EXPECT_EQ(predicate_map[ControlOutputFor(id_2)], "#true"); +} + +TEST(DeadnessAnalysisTest, RefInt_SwitchN_3Branches) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Output condition_ref_var = + ops::Variable(root.WithOpName("bidx"), TensorShape({}), DT_INT32); + Output value = ops::Placeholder(root.WithOpName("value"), DT_FLOAT); + OutputList outputs; + CreateSwitchN(root.WithOpName("switchn"), value, condition_ref_var, 3, + &outputs); + + Output id_0 = ops::Identity(root.WithOpName("id_0"), outputs[0]); + Output id_1 = ops::Identity(root.WithOpName("id_1"), outputs[1]); + Output id_2 = ops::Identity(root.WithOpName("id_2"), outputs[2]); + + FixupSourceAndSinkEdges(root.graph()); + + PredicateMapTy predicate_map; + TF_ASSERT_OK(ComputePredicates(*root.graph(), &predicate_map)); + + EXPECT_EQ(predicate_map[ControlOutputFor(id_0)], "bidx:0=0"); + EXPECT_EQ(predicate_map[ControlOutputFor(id_1)], "(~bidx:0=0 & bidx:0=1)"); + EXPECT_EQ(predicate_map[ControlOutputFor(id_2)], "(~bidx:0=0 & ~bidx:0=1)"); +} + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/defs.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/defs.cc new file mode 100644 index 0000000000000000000000000000000000000000..31cae73882447eae8bdd81bc292939400527f6e5 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/defs.cc @@ -0,0 +1,45 @@ +/* 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/compiler/jit/defs.h" + +#include + +namespace tensorflow { + +const char* const kXlaMustCompileAttr = "_XlaMustCompile"; + +const char* const kXlaCompileAttr = "_XlaCompile"; + +// User-provided through jit_scope APIs. Effective only when auto_jit is OFF. +const char* const kXlaScopeAttr = "_XlaScope"; + +// Automatically inserted by auto_jit to guide clustering results. Effective +// only when auto_jit is ON. +const char* const kXlaInternalScopeAttr = "_XlaInternalScope"; + +const char* const kXlaClusterIdAttr = "_xla_compile_id"; + +static std::atomic xla_devices_creation_required(false); + +// Request XLA:GPU and XLA:CPU device creation. Deprecated, only used by XRT +// backend. +void RequestXlaDevicesCreation() { xla_devices_creation_required = true; } + +// Check whether XLA:GPU and XLA:CPU device creation was requested. Deprecated, +// only used by XRT backend. +bool XlaDevicesCreationRequired() { return xla_devices_creation_required; } + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/defs.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/defs.h new file mode 100644 index 0000000000000000000000000000000000000000..5c8a7d906f92791dcb167bfbd05910aa013f7e1e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/defs.h @@ -0,0 +1,49 @@ +/* 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. +==============================================================================*/ + +// Provides definitions needed for use of the TensorFlow XLA +// device. + +#ifndef TENSORFLOW_COMPILER_JIT_DEFS_H_ +#define TENSORFLOW_COMPILER_JIT_DEFS_H_ + +namespace tensorflow { + +// Name of attribute used to tag operators for compilation with XLA + +// Implies must-compile semantics: either it will be compiled +// with XLA, or an error will be thrown. +extern const char* const kXlaMustCompileAttr; // "_XlaMustCompile" + +// Implies auto-clustering: tagged nodes will be clustered and compiled with XLA +// on a best-effort basis. +extern const char* const kXlaCompileAttr; // "_XlaCompile" + +// Implies auto-clustering within the given scope. +extern const char* const kXlaScopeAttr; // "_XlaScope" +extern const char* const kXlaInternalScopeAttr; // "_XlaInternalScope" + +// The id of the compiled cluster. +extern const char* const kXlaClusterIdAttr; // "_xla_compile_id" + +[[deprecated("XLA:CPU/GPU devices are deprecated")]] void +RequestXlaDevicesCreation(); + +[[deprecated("XLA:CPU/GPU devices are deprecated")]] bool +XlaDevicesCreationRequired(); + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_DEFS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compilation_cache.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compilation_cache.h new file mode 100644 index 0000000000000000000000000000000000000000..96df5e9a0d567a3481e2a20dd1f59d04c2b978c6 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compilation_cache.h @@ -0,0 +1,280 @@ +/* Copyright 2022 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_DEVICE_COMPILATION_CACHE_H_ +#define TENSORFLOW_COMPILER_JIT_DEVICE_COMPILATION_CACHE_H_ + +#include +#include +#include +#include +#include + +#include "absl/base/nullability.h" +#include "absl/container/flat_hash_map.h" +#include "absl/strings/str_cat.h" +#include "tensorflow/compiler/jit/device_compilation_cluster_signature.h" +#include "tensorflow/compiler/jit/xla_compile_util.h" +#include "tensorflow/compiler/tf2xla/xla_compiler.h" +#include "xla/client/local_client.h" +#include "xla/pjrt/pjrt_client.h" +#include "tensorflow/core/platform/mutex.h" + +namespace tensorflow { +namespace device_compilation_cache_internal { +template +int64_t ExecutableSize(const ExecutableType* executable) { + return 0; +} + +template <> +inline int64_t ExecutableSize( + const xla::LocalExecutable* executable) { + if (executable != nullptr && executable->executable() != nullptr) { + return executable->executable()->SizeOfGeneratedCodeInBytes(); + } + + return 0; +} + +template <> +inline int64_t ExecutableSize( + const xla::PjRtLoadedExecutable* executable) { + if (executable != nullptr) { + return executable->SizeOfGeneratedCodeInBytes(); + } + + return 0; +} +} // namespace device_compilation_cache_internal + +// Cache to store compiled HLO, executables and related metadata keyed by +// `DeviceCompilationClusterSignature`. The cache owns the stored +// CompilationResults and Executables. +// Currently no cache eviction policy is implemented and the cache grows without +// bound. +template +class DeviceCompilationCache { + public: + DeviceCompilationCache() = default; + ~DeviceCompilationCache() = default; + + using Key = DeviceCompilationClusterSignature; + struct Value { + DeviceCompileState compile_state = DeviceCompileState::kUncompiled; + absl::Status compilation_status; + int64_t request_count = 0; + const XlaCompiler::CompilationResult* compilation_result = nullptr; + ExecutableType* executable = nullptr; + }; + + // Returns std::nullopt if value for the supplied key is not found. If a value + // is found, `request_count` is incremented before returning the value. + std::optional Lookup(const Key& key) const; + + // Inserts an empty value if value is not found and returns it. If a value is + // found, `request_count` is incremented before returning the value. + Value LookupOrCreate(const Key& key); + + // Caches `compile_state`, `compilation_status`, `compilation_result` and + // `executable` and associates them with the provided `key`. Takes ownership + // of `compilation_result` and `executable`. Does not increment the + // corresponding `request_count`. Only arguments that are not std::nullopt are + // updated in the cache. + void Store(const Key& key, std::optional compile_state, + std::optional compilation_status, + std::optional> + compilation_result, + std::optional> executable); + + std::string DebugString() const; + + // Erase any cache entries that have a null entry and releases all references + // to `xla::XlaComputation` in the non-null cache entries. + void Finalize() { + const mutex_lock lock(compile_cache_mu_); + absl::erase_if( + cache_, + [&](std::pair>& kv) { + Entry* absl_nullable const entry = kv.second.get(); + if (entry == nullptr) { + return true; + } + + const mutex_lock entry_lock(entry->mu); + if (entry->compilation_result != nullptr) { + entry->compilation_result->computation.reset(); + } + + return false; + }); + }; + + private: + // The value associated with a cache entry. + struct Entry { + mutable mutex mu; + + // The current compilation state for this entry. + DeviceCompileState compile_state TF_GUARDED_BY(mu) = + DeviceCompileState::kUncompiled; + + // The number of times a compilation with this signature has been requested. + int64_t request_count TF_GUARDED_BY(mu) = 0; + + // Did compilation succeed? + absl::Status compilation_status TF_GUARDED_BY(mu); + + // Output of the XlaCompiler. + std::unique_ptr compilation_result + TF_GUARDED_BY(mu); + + // The XLA executable compiled from . May be null if no + // executable has been built. + std::unique_ptr executable TF_GUARDED_BY(mu); + + std::string DebugString() const { + mutex_lock lock(mu); + + int64_t executable_size = + device_compilation_cache_internal::ExecutableSize( + executable.get()); + + int64_t hlo_module_size = 0; + if (compilation_result != nullptr && + compilation_result->computation != nullptr) { + hlo_module_size = + compilation_result->computation->proto().ByteSizeLong(); + } + + return absl::StrCat( + "{compile_state: ", compile_state, ", request_count: ", request_count, + ", compilation_status: ", compilation_status.ToString(), + ", compilation_result?: ", compilation_result != nullptr, + ", hlo_module_size: ", hlo_module_size, " bytes", + ", executable?: ", executable != nullptr, + ", executable_size: ", executable_size, " bytes}"); + } + }; + + mutable mutex compile_cache_mu_; + absl::flat_hash_map, Key::Hash> cache_ + TF_GUARDED_BY(compile_cache_mu_); + + DeviceCompilationCache(const DeviceCompilationCache&) = delete; + void operator=(const DeviceCompilationCache&) = delete; +}; + +template +std::optional::Value> +DeviceCompilationCache::Lookup(const Key& key) const { + // The outer lock protects the existence of the cache entry. It does not + // protect the contents of the cache entry. + Entry* entry; + { + mutex_lock lock(compile_cache_mu_); + // Find cache entry. + auto it = cache_.find(key); + if (it == cache_.cend()) { + return std::nullopt; + } + + entry = it->second.get(); + } + + mutex_lock lock(entry->mu); + Value value = {/*compile_state=*/entry->compile_state, + /*compilation_status=*/entry->compilation_status, + /*request_count=*/++entry->request_count, + /*compilation_result=*/entry->compilation_result.get(), + /*executable=*/entry->executable.get()}; + return value; +} + +template +typename DeviceCompilationCache::Value +DeviceCompilationCache::LookupOrCreate(const Key& key) { + // The outer lock protects the existence of the cache entry. It does not + // protect the contents of the cache entry. + Entry* entry; + { + mutex_lock lock(compile_cache_mu_); + // Emplace empty cache entry if not found. + auto it = cache_.emplace(key, std::make_unique()).first; + entry = it->second.get(); + } + + mutex_lock lock(entry->mu); + Value value = {/*compile_state=*/entry->compile_state, + /*compilation_status=*/entry->compilation_status, + /*request_count=*/++entry->request_count, + /*compilation_result=*/entry->compilation_result.get(), + /*executable=*/entry->executable.get()}; + return value; +} + +template +void DeviceCompilationCache::Store( + const Key& key, std::optional compile_state, + std::optional compilation_status, + std::optional> + compilation_result, + std::optional> executable) { + Entry* entry; + { + mutex_lock lock(compile_cache_mu_); + // Emplace empty cache entry if not found. + auto it = cache_.emplace(key, std::make_unique()).first; + entry = it->second.get(); + } + + { + mutex_lock lock(entry->mu); + if (compile_state.has_value()) { + entry->compile_state = *compile_state; + } + if (compilation_status.has_value()) { + entry->compilation_status = *compilation_status; + } + if (compilation_result.has_value()) { + entry->compilation_result = std::move(*compilation_result); + } + if (executable.has_value()) { + entry->executable = std::move(*executable); + } + } + + VLOG(4) << "Added/updated cache entry: key=" << key.HumanString() + << ", entry=" << entry->DebugString(); +} + +template +std::string DeviceCompilationCache::DebugString() const { + std::string s = "DeviceCompilationCache {\n"; + { + mutex_lock lock(compile_cache_mu_); + for (const auto& [key, entry] : cache_) { + absl::StrAppend(&s, key.HumanString(), " : ", entry->DebugString(), + ",\n"); + } + } + absl::StrAppend(&s, "}"); + + return s; +} + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_DEVICE_COMPILATION_CACHE_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compilation_cache_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compilation_cache_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..c64f389ce9e6dc7988525c5ab53b4f094dd99ab9 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compilation_cache_test.cc @@ -0,0 +1,277 @@ +/* Copyright 2022 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/compiler/jit/device_compilation_cache.h" + +#include +#include +#include +#include +#include + +#include +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "tensorflow/compiler/jit/xla_compile_util.h" +#include "tensorflow/compiler/tf2xla/xla_compiler.h" +#include "xla/hlo/builder/xla_computation.h" +#include "xla/tsl/protobuf/error_codes.pb.h" +#include "tensorflow/core/platform/errors.h" +#include "tensorflow/core/platform/test.h" + +namespace tensorflow { +namespace { + +struct FakeExecutable { + std::string data; + explicit FakeExecutable(const std::string& s) : data(s) {} +}; + +using Cache = DeviceCompilationCache; +using Signature = DeviceCompilationClusterSignature; + +absl::StatusOr BuildSampleSignature(const std::string& fn_name) { + NameAttrList fn; + fn.set_name(fn_name); + std::vector args(1); + args[0].kind = XlaCompiler::Argument::kConstant; + args[0].type = DT_INT32; + args[0].shape = TensorShape({4, 0}); + args[0].constant_value = Tensor(DT_INT32, {4, 0}); + return Signature::Build(fn, args); +} + +TEST(DeviceCompilationCacheTest, LookupEntryDoesntExist) { + auto cache = std::make_unique(); + + TF_ASSERT_OK_AND_ASSIGN(auto key, BuildSampleSignature("foo")); + auto cache_value = cache->Lookup(key); + + EXPECT_FALSE(cache_value.has_value()); +} + +TEST(DeviceCompilationCacheTest, LookupOrCreateEntryDoesntExist) { + auto cache = std::make_unique(); + + TF_ASSERT_OK_AND_ASSIGN(auto key, BuildSampleSignature("foo")); + Cache::Value cache_value = cache->LookupOrCreate(key); + + EXPECT_EQ(cache_value.compile_state, DeviceCompileState::kUncompiled); + EXPECT_EQ(cache_value.request_count, 1); + EXPECT_EQ(cache_value.compilation_result, nullptr); + EXPECT_EQ(cache_value.executable, nullptr); +} + +TEST(DeviceCompilationCacheTest, IncrementRequestCountOnLookup) { + auto cache = std::make_unique(); + + TF_ASSERT_OK_AND_ASSIGN(auto key, BuildSampleSignature("foo")); + Cache::Value cache_value = cache->LookupOrCreate(key); + EXPECT_EQ(cache_value.request_count, 1); + + cache_value = cache->LookupOrCreate(key); + EXPECT_EQ(cache_value.request_count, 2); + + cache_value = cache->LookupOrCreate(key); + EXPECT_EQ(cache_value.request_count, 3); +} + +TEST(DeviceCompilationCacheTest, RequestCountUnchangedOnStore) { + auto cache = std::make_unique(); + + TF_ASSERT_OK_AND_ASSIGN(auto key, BuildSampleSignature("foo")); + Cache::Value cache_value = cache->LookupOrCreate(key); + EXPECT_EQ(cache_value.request_count, 1); + + cache_value = cache->LookupOrCreate(key); + EXPECT_EQ(cache_value.request_count, 2); + + cache_value = cache->LookupOrCreate(key); + EXPECT_EQ(cache_value.request_count, 3); + + auto compilation_result = std::make_unique(); + cache->Store(key, DeviceCompileState::kCompiled, absl::OkStatus(), + std::move(compilation_result), std::nullopt); + cache_value = cache->LookupOrCreate(key); + + EXPECT_EQ(cache_value.request_count, 4); +} + +TEST(DeviceCompilationCacheTest, StoreLookup) { + auto cache = std::make_unique(); + + TF_ASSERT_OK_AND_ASSIGN(auto key, BuildSampleSignature("foo")); + auto compilation_result = std::make_unique(); + auto executable = std::make_unique("foo_exe"); + cache->Store(key, DeviceCompileState::kCompiled, absl::OkStatus(), + std::move(compilation_result), std::move(executable)); + auto cache_value = cache->Lookup(key); + + EXPECT_EQ(cache_value->compile_state, DeviceCompileState::kCompiled); + EXPECT_EQ(cache_value->request_count, 1); + EXPECT_TRUE(cache_value->compilation_status.ok()); + EXPECT_TRUE(cache_value->compilation_result != nullptr); + EXPECT_TRUE(cache_value->executable != nullptr); + EXPECT_EQ(cache_value->executable->data, "foo_exe"); +} + +TEST(DeviceCompilationCacheTest, StoreLookupOrCreate) { + auto cache = std::make_unique(); + + TF_ASSERT_OK_AND_ASSIGN(auto key, BuildSampleSignature("foo")); + auto compilation_result = std::make_unique(); + auto executable = std::make_unique("foo_exe"); + cache->Store(key, DeviceCompileState::kCompiled, absl::OkStatus(), + std::move(compilation_result), std::move(executable)); + auto cache_value = cache->LookupOrCreate(key); + + EXPECT_EQ(cache_value.compile_state, DeviceCompileState::kCompiled); + EXPECT_EQ(cache_value.request_count, 1); + EXPECT_TRUE(cache_value.compilation_status.ok()); + EXPECT_TRUE(cache_value.compilation_result != nullptr); + EXPECT_TRUE(cache_value.executable != nullptr); + EXPECT_EQ(cache_value.executable->data, "foo_exe"); +} + +TEST(DeviceCompilationCacheTest, StoreOptionalArgs) { + auto cache = std::make_unique(); + + TF_ASSERT_OK_AND_ASSIGN(auto key, BuildSampleSignature("foo")); + + auto compilation_result = std::make_unique(); + auto executable = std::make_unique("foo_exe"); + + cache->Store(key, DeviceCompileState::kCompiled, std::nullopt, std::nullopt, + std::nullopt); + auto cache_value = cache->Lookup(key); + + EXPECT_EQ(cache_value->compile_state, DeviceCompileState::kCompiled); + EXPECT_TRUE(cache_value->compilation_status.ok()); + EXPECT_TRUE(cache_value->compilation_result == nullptr); + EXPECT_TRUE(cache_value->executable == nullptr); + + cache->Store(key, std::nullopt, + absl::InvalidArgumentError("Couldn't compile."), std::nullopt, + std::nullopt); + cache_value = cache->Lookup(key); + + EXPECT_EQ(cache_value->compile_state, DeviceCompileState::kCompiled); + EXPECT_EQ(cache_value->compilation_status.code(), error::INVALID_ARGUMENT); + EXPECT_TRUE(cache_value->compilation_result == nullptr); + EXPECT_TRUE(cache_value->executable == nullptr); + + cache->Store(key, std::nullopt, std::nullopt, std::move(compilation_result), + std::nullopt); + cache_value = cache->Lookup(key); + + EXPECT_EQ(cache_value->compile_state, DeviceCompileState::kCompiled); + EXPECT_EQ(cache_value->compilation_status.code(), error::INVALID_ARGUMENT); + EXPECT_TRUE(cache_value->compilation_result != nullptr); + EXPECT_TRUE(cache_value->executable == nullptr); + + cache->Store(key, std::nullopt, std::nullopt, std::nullopt, + std::move(executable)); + cache_value = cache->Lookup(key); + + EXPECT_EQ(cache_value->compile_state, DeviceCompileState::kCompiled); + EXPECT_EQ(cache_value->compilation_status.code(), error::INVALID_ARGUMENT); + EXPECT_TRUE(cache_value->compilation_result != nullptr); + EXPECT_TRUE(cache_value->executable != nullptr); + EXPECT_EQ(cache_value->executable->data, "foo_exe"); +} + +TEST(DeviceCompilationCacheTest, StoreMultipleEntries) { + auto cache = std::make_unique(); + + TF_ASSERT_OK_AND_ASSIGN(auto key1, BuildSampleSignature("foo")); + TF_ASSERT_OK_AND_ASSIGN(auto key2, BuildSampleSignature("bar")); + + auto compilation_result1 = std::make_unique(); + auto compilation_result2 = std::make_unique(); + auto executable1 = std::make_unique("foo_exe"); + auto executable2 = std::make_unique("bar_exe"); + cache->Store(key1, DeviceCompileState::kCompiled, + absl::InvalidArgumentError("Invalid argument."), + std::move(compilation_result1), std::move(executable1)); + cache->Store(key2, DeviceCompileState::kCompiling, absl::OkStatus(), + std::move(compilation_result2), std::move(executable2)); + auto cache_value_1 = cache->Lookup(key1); + auto cache_value_2 = cache->Lookup(key2); + + EXPECT_EQ(cache_value_1->compile_state, DeviceCompileState::kCompiled); + EXPECT_EQ(cache_value_1->compilation_status.code(), error::INVALID_ARGUMENT); + EXPECT_TRUE(cache_value_1->compilation_result != nullptr); + EXPECT_TRUE(cache_value_1->executable != nullptr); + EXPECT_EQ(cache_value_1->executable->data, "foo_exe"); + + EXPECT_EQ(cache_value_2->compile_state, DeviceCompileState::kCompiling); + EXPECT_TRUE(cache_value_2->compilation_status.ok()); + EXPECT_TRUE(cache_value_2->compilation_result != nullptr); + EXPECT_TRUE(cache_value_2->executable != nullptr); + EXPECT_EQ(cache_value_2->executable->data, "bar_exe"); +} + +TEST(DeviceCompilationCacheTest, Finalize) { + auto cache = std::make_unique(); + + TF_ASSERT_OK_AND_ASSIGN(auto key, BuildSampleSignature("foo")); + auto computation = std::make_shared(); + auto compilation_result = std::make_unique(); + compilation_result->computation = computation; + + cache->Store(key, /*compile_state=*/DeviceCompileState::kCompiled, + /*compilation_status=*/absl::OkStatus(), + std::move(compilation_result), + /*executable=*/std::make_unique("foo_exe")); + + std::optional cache_value = cache->Lookup(key); + ASSERT_TRUE(cache_value.has_value()); + ASSERT_TRUE(cache_value->compilation_result != nullptr); + EXPECT_EQ(cache_value->compilation_result->computation, computation); + + cache->Finalize(); + + cache_value = cache->Lookup(key); + ASSERT_TRUE(cache_value.has_value()); + ASSERT_TRUE(cache_value->compilation_result != nullptr); + EXPECT_TRUE(cache_value->compilation_result->computation == nullptr); +} + +TEST(DeviceCompilationCache, FinalizeWithNullComputation) { + auto cache = std::make_unique(); + + TF_ASSERT_OK_AND_ASSIGN(auto key, BuildSampleSignature("foo")); + + cache->Store( + key, /*compile_state=*/DeviceCompileState::kCompiled, + /*compilation_status=*/absl::OkStatus(), + /*compilation_result=*/std::make_unique(), + /*executable=*/std::make_unique("foo_exe")); + + std::optional cache_value = cache->Lookup(key); + ASSERT_TRUE(cache_value.has_value()); + ASSERT_TRUE(cache_value->compilation_result != nullptr); + EXPECT_TRUE(cache_value->compilation_result->computation == nullptr); + + cache->Finalize(); + + cache_value = cache->Lookup(key); + ASSERT_TRUE(cache_value.has_value()); + ASSERT_TRUE(cache_value->compilation_result != nullptr); + EXPECT_TRUE(cache_value->compilation_result->computation == nullptr); +} + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compilation_cluster_signature.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compilation_cluster_signature.cc new file mode 100644 index 0000000000000000000000000000000000000000..40e63c42d89cae4437896f2c99f1531c63e01242 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compilation_cluster_signature.cc @@ -0,0 +1,158 @@ +/* Copyright 2022 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/compiler/jit/device_compilation_cluster_signature.h" + +#include +#include +#include + +#include "absl/status/statusor.h" +#include "absl/types/span.h" +#include "tensorflow/compiler/tf2xla/xla_compiler.h" +#include "tensorflow/core/framework/function.h" +#include "tensorflow/core/framework/node_def_util.h" + +namespace tensorflow { +namespace { +using Signature = DeviceCompilationClusterSignature; +using TensorTypeAndShape = Signature::TensorTypeAndShape; + +// Functor that converts a Signature's arg to a human readable string. +struct SignatureHumanStringAppender { + explicit SignatureHumanStringAppender(std::string* dest) : dest(dest) {} + std::string* dest; + void operator()(const Tensor& arg) { + absl::StrAppend(dest, "; ", arg.DebugString()); + } + void operator()(const TensorTypeAndShape& arg) { + absl::StrAppend(dest, ",", DataTypeString(arg.first)); + absl::StrAppend(dest, " [", absl::StrJoin(arg.second, ","), "]"); + } +}; + +// Functor that compares the arg values of two different signatures. Returns +// true when the args are not equal. +struct SignatureNotEqual { + bool operator()(const Tensor& arg, const Tensor& other) { + return arg.dtype() != other.dtype() || arg.shape() != other.shape() || + arg.tensor_data() != other.tensor_data(); + } + bool operator()(const TensorTypeAndShape& arg, + const TensorTypeAndShape& other) { + return arg.first != other.first || arg.second != other.second; + } + bool operator()(const Tensor& arg, const TensorTypeAndShape& other) { + return true; + } + bool operator()(const TensorTypeAndShape& arg, const Tensor& other) { + return true; + } +}; + +// Functor that incrementally computes a Signature's hash given its current hash +// and one of its args. +struct SignatureHashCombiner { + explicit SignatureHashCombiner(const uint64_t h) : h(h) {} + uint64_t h; + uint64_t operator()(const Tensor& arg) { + h = Hash64Combine(h, std::hash()(static_cast(arg.dtype()))); + h = Hash64Combine( + h, Hash64(arg.tensor_data().data(), arg.tensor_data().size())); + for (int dim = 0; dim < arg.dims(); ++dim) { + h = Hash64Combine(h, std::hash()(arg.dim_size(dim))); + } + return h; + } + uint64_t operator()(const TensorTypeAndShape& arg) { + h = Hash64Combine(h, std::hash()(static_cast(arg.first))); + h = Hash64Combine(h, std::hash()(arg.second.size())); + for (int dim : arg.second) { + h = Hash64Combine(h, std::hash()(dim)); + } + return h; + } +}; +} // namespace + +// Compute a string signature which encodes the shapes of the +// arguments in the supplied list. +std::string Signature::HumanString() const { + std::string result = name; + for (const auto& arg : args) { + std::visit(SignatureHumanStringAppender(&result), arg); + } + return result; +} + +bool Signature::operator==(const Signature& other) const { + if (name != other.name) return false; + if (args.size() != other.args.size()) return false; + for (int i = 0, end = args.size(); i < end; ++i) { + if (std::visit(SignatureNotEqual(), args[i], other.args[i])) { + return false; + } + } + return true; +} + +uint64_t Signature::Hash::operator()(const Signature& signature) const { + uint64_t h = std::hash()(signature.name); + for (const auto& arg : signature.args) { + h = std::visit(SignatureHashCombiner(h), arg); + } + return h; +} + +static absl::StatusOr AppendArguments( + Signature signature, absl::Span args) { + signature.args.reserve(args.size()); + for (const XlaCompiler::Argument& arg : args) { + switch (arg.kind) { + case XlaCompiler::Argument::kConstant: + case XlaCompiler::Argument::kConstantResource: + signature.args.push_back(arg.constant_value); + break; + case XlaCompiler::Argument::kParameter: + case XlaCompiler::Argument::kResource: + signature.args.push_back( + TensorTypeAndShape(arg.type, arg.DimensionSizesAsInlinedVector())); + break; + default: + return absl::InvalidArgumentError( + absl::StrCat("Unhandled argument kind in XlaCompilationCache: ", + arg.HumanString())); + } + } + return signature; +} + +absl::StatusOr Signature::Build( + const NameAttrList& function, + absl::Span args) { + Signature signature; + signature.name = Canonicalize(function.name(), AttrSlice(&function.attr())); + return AppendArguments(std::move(signature), args); +} + +absl::StatusOr Signature::Build( + const DeviceCompilationCanonicalFunction& canonical_function, + absl::Span args) { + Signature signature; + signature.name = canonical_function.canonical; + return AppendArguments(std::move(signature), args); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compilation_cluster_signature.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compilation_cluster_signature.h new file mode 100644 index 0000000000000000000000000000000000000000..d4b2431d44a70b56127d212005d904c8824cd0de --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compilation_cluster_signature.h @@ -0,0 +1,80 @@ +/* Copyright 2022 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_DEVICE_COMPILATION_CLUSTER_SIGNATURE_H_ +#define TENSORFLOW_COMPILER_JIT_DEVICE_COMPILATION_CLUSTER_SIGNATURE_H_ + +#include +#include +#include +#include + +#include "absl/container/inlined_vector.h" +#include "absl/status/statusor.h" +#include "absl/types/span.h" +#include "tensorflow/compiler/tf2xla/xla_compiler.h" +#include "tensorflow/core/framework/function.h" +#include "tensorflow/core/framework/node_def_util.h" +#include "tensorflow/core/framework/tensor.h" +#include "tensorflow/core/platform/types.h" + +namespace tensorflow { + +// Pre-computed device compilation canonical function. +struct DeviceCompilationCanonicalFunction { + std::string canonical; +}; + +inline DeviceCompilationCanonicalFunction Canonicalize( + const NameAttrList& function) { + return {Canonicalize(function.name(), AttrSlice(&function.attr()))}; +} + +// Describes the types, shapes and any compile-time constant arguments +// to a kernel. Key that uniquely identifies a compilation output. +struct DeviceCompilationClusterSignature { + // Name of the cluster, built from the function name and it's attributes. + std::string name; + + // List of args (either as a TensorTypeAndShape or as a Tensor value) + // for compile-time constant arguments to the compilation, ordered by + // argument number. Tensors must be in host memory. + using TensorTypeAndShape = + std::pair>; + absl::InlinedVector, 8> args; + + bool operator==(const DeviceCompilationClusterSignature& other) const; + + struct Hash { + uint64_t operator()( + const DeviceCompilationClusterSignature& signature) const; + }; + + // Returns a human-readable description of the signature. + std::string HumanString() const; + + // Builds the signature for a compilation. + static absl::StatusOr Build( + const NameAttrList& function, + absl::Span args); + + static absl::StatusOr Build( + const DeviceCompilationCanonicalFunction& canonical_function, + absl::Span args); +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_DEVICE_COMPILATION_CLUSTER_SIGNATURE_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compilation_cluster_signature_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compilation_cluster_signature_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..6c518438fef5ace6e2d178898ca84905d1eb270d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compilation_cluster_signature_test.cc @@ -0,0 +1,123 @@ +/* Copyright 2022 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/compiler/jit/device_compilation_cluster_signature.h" + +#include +#include + +#include "tensorflow/compiler/jit/flags.h" +#include "tensorflow/compiler/tf2xla/shape_util.h" +#include "xla/client/client_library.h" +#include "tensorflow/core/platform/test.h" +#include "tensorflow/core/platform/test_benchmark.h" + +namespace tensorflow { +namespace { +using SignatureHash = DeviceCompilationClusterSignature::Hash; + +TEST(DeviceCompilationClusterSignatureTest, SignatureEquality) { + NameAttrList fn; + fn.set_name("afunction"); + std::vector args(1); + args[0].kind = XlaCompiler::Argument::kConstant; + args[0].type = DT_INT32; + args[0].shape = TensorShape({4, 0}); + args[0].constant_value = Tensor(DT_INT32, {4, 0}); + TF_ASSERT_OK_AND_ASSIGN(DeviceCompilationClusterSignature s1, + DeviceCompilationClusterSignature::Build(fn, args)); + + args[0].type = DT_FLOAT; + args[0].constant_value = Tensor(DT_FLOAT, {4, 0}); + TF_ASSERT_OK_AND_ASSIGN(DeviceCompilationClusterSignature s2, + DeviceCompilationClusterSignature::Build(fn, args)); + + args[0].shape = TensorShape({0, 4}); + args[0].constant_value = Tensor(DT_FLOAT, {0, 4}); + TF_ASSERT_OK_AND_ASSIGN(DeviceCompilationClusterSignature s3, + DeviceCompilationClusterSignature::Build(fn, args)); + + std::vector signatures = {s1, s2, s3}; + for (int i = 0; i < signatures.size(); ++i) { + for (int j = 0; j < signatures.size(); ++j) { + EXPECT_EQ(i == j, signatures[i] == signatures[j]) + << "s1: " << signatures[i].HumanString() << "\n" + << "s2: " << signatures[j].HumanString(); + EXPECT_EQ(i == j, + signatures[i].HumanString() == signatures[j].HumanString()) + << "s1: " << signatures[i].HumanString() << "\n" + << "s2: " << signatures[j].HumanString(); + EXPECT_EQ(i == j, SignatureHash()(signatures[i]) == + SignatureHash()(signatures[j])) + << "s1: " << signatures[i].HumanString() << "\n" + << "s1_hash: " << SignatureHash()(signatures[i]) << "\n" + << "s2: " << signatures[j].HumanString() << "\n" + << "s2_hash: " << SignatureHash()(signatures[j]); + } + } +} + +TEST(DeviceCompilationClusterSignatureTest, SignatureUniqueness) { + NameAttrList fn; + fn.set_name("afunction"); + std::vector args(2); + args[0].kind = XlaCompiler::Argument::kConstant; + args[0].type = DT_INT32; + args[0].constant_value = Tensor(DT_INT32, {4, 0}); + + args[1].kind = XlaCompiler::Argument::kParameter; + args[1].type = DT_INT32; + args[1].shape = TensorShape({4, 0}); + + TF_ASSERT_OK_AND_ASSIGN(DeviceCompilationClusterSignature s1, + DeviceCompilationClusterSignature::Build(fn, args)); + + using std::swap; // go/using-std-swap + swap(args[0], args[1]); + TF_ASSERT_OK_AND_ASSIGN(DeviceCompilationClusterSignature s2, + DeviceCompilationClusterSignature::Build(fn, args)); + + EXPECT_NE(s1.HumanString(), s2.HumanString()); + EXPECT_NE(SignatureHash()(s1), SignatureHash()(s2)); + EXPECT_FALSE(s1 == s2); +} + +void BM_BuildSignature(::testing::benchmark::State& state) { + const int n_args = state.range(0); + + NameAttrList fn; + fn.set_name("afunction"); + for (int i = 0; i < n_args; i++) { + (*fn.mutable_attr())[absl::StrCat("T", i)].set_type(DT_FLOAT); + } + std::vector args(n_args); + for (int i = 0; i < n_args; i++) { + args[i].kind = (((i % 3) == 0) ? XlaCompiler::Argument::kConstant + : XlaCompiler::Argument::kParameter); + args[i].type = DT_INT32; + args[i].shape = TensorShape({4, 0}); + args[i].constant_value = Tensor(DT_INT32, {4, 0}); + } + + for (auto i : state) { + auto s = DeviceCompilationClusterSignature::Build(fn, args); + CHECK(s.ok()); + DeviceCompilationClusterSignature sig = std::move(s.value()); + } +} +BENCHMARK(BM_BuildSignature)->Arg(0)->Arg(1)->Arg(2)->Arg(5)->Arg(10); + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compilation_profiler.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compilation_profiler.cc new file mode 100644 index 0000000000000000000000000000000000000000..b58ede9d7c2b30a190112f451d2799e8acf07e65 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compilation_profiler.cc @@ -0,0 +1,233 @@ +/* Copyright 2022 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/compiler/jit/device_compilation_profiler.h" + +#include +#include +#include +#include + +#include "absl/strings/str_cat.h" +#include "tensorflow/compiler/jit/xla_activity.pb.h" +#include "tensorflow/compiler/jit/xla_activity_listener.h" +#include "tensorflow/core/framework/attr_value.pb.h" +#include "tensorflow/core/framework/metrics.h" +#include "tensorflow/core/platform/errors.h" +#include "tensorflow/core/platform/status.h" +#include "tsl/platform/mutex.h" + +namespace tensorflow { +namespace { +bool ShouldBeMegamorphic(int64_t compile_count, int64_t execution_count) { + const int64_t kCompileThreshold = 10; + const int64_t kMinExecutionsPerCompile = 50; + + // This heuristic is trying to capture the following property: have we sunk a + // certain minimum amount of compile time into the cluster that didn't quite + // "pay off"? + return compile_count > kCompileThreshold && + execution_count < kMinExecutionsPerCompile * compile_count; +} + +void RegisterExecutionForCluster( + const NameAttrList& function, + DeviceCompilationProfiler::ClusterCompileStats* stats) { + ++stats->execution_count; + + // The is_megamorphic bit is "sticky". We assume clusters that have been + // observed to be megamorphic once stay megamorphic forever. + if (!stats->is_megamorphic && + ShouldBeMegamorphic(stats->compile_count, stats->execution_count)) { + VLOG(1) << "Marking " << function.name() + << " as megamorphic, compile_count=" << stats->compile_count + << " execution_count=" << stats->execution_count; + stats->is_megamorphic = true; + } +} + +// The number of times a lazy compilation must be requested for a specific +// signature before we attempt to compile it. +constexpr int64_t kDefaultCompilationThreshold = 2; + +// Maximum number of ongoing compilations. +constexpr int64_t kMaxNumOngoingCompilations = kNumAsyncDeviceCompilerThreads; + +} // namespace + +DeviceCompilationProfiler::~DeviceCompilationProfiler() { + mutex_lock lock(mu_); + cluster_compile_stats_.clear(); +} + +absl::StatusOr +DeviceCompilationProfiler::GetCompileStats(const NameAttrList& function) const { + mutex_lock lock(mu_); + + if (auto it = cluster_compile_stats_.find(function.name()); + it != cluster_compile_stats_.end()) { + return it->second; + } + + return absl::NotFoundError(absl::StrCat( + "Couldn't find compilation stats for cluster: ", function.name())); +} + +void DeviceCompilationProfiler::RegisterExecution( + const NameAttrList& function) { + mutex_lock lock(mu_); + auto it = + cluster_compile_stats_.emplace(function.name(), ClusterCompileStats{}) + .first; + RegisterExecutionForCluster(function, &it->second); +} + +absl::Status DeviceCompilationProfiler::RegisterCompilation( + const NameAttrList& function, int64_t compile_time_us, + bool used_persistent_cache) { + metrics::UpdateXlaCompilationTime(compile_time_us); + + const std::string& function_name = function.name(); + + mutex_lock lock(mu_); + // Create a stats entry if it doesn't already exist. + auto it = + cluster_compile_stats_.emplace(function.name(), ClusterCompileStats{}) + .first; + + const uint64_t compile_time_s = compile_time_us / 1.0e6; + it->second.compile_count++; + it->second.cumulative_compile_time_us += compile_time_us; + VLOG(1) << "Compiled " << function_name << " " << it->second.compile_count + << " times, compile time: " << compile_time_us + << " us, cumulative: " << it->second.cumulative_compile_time_us + << " us (" + << tensorflow::strings::HumanReadableElapsedTime(compile_time_s) + << " / " + << tensorflow::strings::HumanReadableElapsedTime( + it->second.cumulative_compile_time_us / 1.0e6) + << ")"; + + XlaJitCompilationActivity jit_compilation_activity; + jit_compilation_activity.set_cluster_name(function_name); + jit_compilation_activity.set_compile_count(it->second.compile_count); + jit_compilation_activity.set_compile_time_us(compile_time_us); + jit_compilation_activity.set_cumulative_compile_time_us( + it->second.cumulative_compile_time_us); + jit_compilation_activity.set_used_persistent_cache(used_persistent_cache); + return BroadcastXlaActivity(std::move(jit_compilation_activity)); +} + +bool DeviceCompilationProfiler::ShouldCompileCluster( + const NameAttrList& function, DeviceCompileMode compile_mode, + int64_t current_request_count) { + std::optional compile_threshold; + if (compile_mode == DeviceCompileMode::kLazy) { + compile_threshold = kDefaultCompilationThreshold; + } else if (compile_mode == DeviceCompileMode::kAsync) { + compile_threshold = 0; // for now, always compile right away. + } + + if (compile_mode == DeviceCompileMode::kStrict) { + // Lazy compilation is disabled. + return true; + } + + mutex_lock lock(mu_); + // Create a stats entry if one isn't found and register an execution. + // Determine eligibility assuming this is the first execution of the cluster + // and this cluster has never been compiled before. + auto [it, cluster_not_found] = + cluster_compile_stats_.emplace(function.name(), ClusterCompileStats{}); + if (cluster_not_found) { + RegisterExecutionForCluster(function, &it->second); + } + + // We avoid compiling clusters that have "gone megamorphic" i.e. have an + // excessive amount of shape dynamism. + if (it->second.is_megamorphic) { + BroadcastOptimizationRemark(XlaOptimizationRemark::MEGAMORPHIC_FUNCTION, + function.name()) + .IgnoreError(); + VLOG(2) << "Not compiling cluster " << function.name() + << " because it is megamorphic."; + return false; + } + + // TODO(b/255826209): Figure out if Lazy compilation is still needed given + // that we always compile a cluster the first time it is executed (explained + // below) regardless of compilation mode. If it is not, clean up the related + // logic. + // We always compile a cluster the very first time it is executed. This is an + // optimistic guess that pays off for statically shaped TensorFlow graphs + // (since they get the benefit of XLA right away without waiting for warmup) + // and doesn't hurt much for dynamically shaped TensorFlow graphs (we "pay" at + // most one cluster-compilation's worth of compile time). + if (it->second.execution_count == 1) { + return true; + } + + if (compile_mode == DeviceCompileMode::kAsync) { + // Asynchronous compilation is enabled. + if (num_ongoing_compilations_ >= kMaxNumOngoingCompilations) { + VLOG(2) << "Not asynchronously compiling cluster " << function.name() + << " because of too many ongoing compilations."; + return false; + } + } + + bool reached_compile_threshold = current_request_count >= *compile_threshold; + if (!reached_compile_threshold) { + VLOG(2) << "Not compiling cluster " << function.name() + << " because it has not reached compile threshold; threshold is " + << *compile_threshold << " execution count " + << current_request_count << "."; + } + return reached_compile_threshold; +} + +void DeviceCompilationProfiler::IncrementOngoingAsyncCompilations() { + mutex_lock lock(mu_); + num_ongoing_compilations_++; +} + +void DeviceCompilationProfiler::DecrementOngoingAsyncCompilations() { + mutex_lock lock(mu_); + num_ongoing_compilations_--; +} + +int64_t DeviceCompilationProfiler::GetNumOngoingAsyncCompilations() const { + mutex_lock lock(mu_); + return num_ongoing_compilations_; +} + +std::string DeviceCompilationProfiler::DebugString() const { + std::string debug_string = + "DeviceCompilationProfiler {\ncluster_compile_stats: {\n"; + { + mutex_lock lock(mu_); + + for (const auto& [key, stats] : cluster_compile_stats_) { + absl::StrAppend(&debug_string, key, ": ", stats.DebugString(), "\n"); + } + } + + absl::StrAppend(&debug_string, "}\nnum_ongoing_compilations=", + GetNumOngoingAsyncCompilations(), "\n}\n"); + + return debug_string; +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compilation_profiler.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compilation_profiler.h new file mode 100644 index 0000000000000000000000000000000000000000..ec970baf58af9657ed1c2dd2f6095bc9e9caf220 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compilation_profiler.h @@ -0,0 +1,101 @@ +/* Copyright 2022 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_DEVICE_COMPILATION_PROFILER_H_ +#define TENSORFLOW_COMPILER_JIT_DEVICE_COMPILATION_PROFILER_H_ + +#include +#include + +#include "tensorflow/compiler/jit/xla_compile_util.h" +#include "tensorflow/core/framework/attr_value.pb.h" + +namespace tensorflow { + +// Tracks statistics for device compilation and uses these to determine whether +// the given cluster should be compiled or not. +class DeviceCompilationProfiler : public ResourceBase { + public: + DeviceCompilationProfiler() = default; + ~DeviceCompilationProfiler() override; + + struct ClusterCompileStats { + // Number of times the cluster has been (re-)compiled. + int64_t compile_count = 0; + + // The number of times this cluster has been executed. + int64_t execution_count = 0; + + // Cumulative time spent compiling the cluster. + int64_t cumulative_compile_time_us = 0; + + // True if we have decided that this cluster is too dynamic (i.e. its shapes + // change too frequently) to profitably JIT compile. Once a cluster is + // tagged megamorphic, it stays megamorphic forever. + bool is_megamorphic = false; + + std::string DebugString() const { + return absl::StrCat( + "DeviceCompilationProfiler::ClusterCompileStats {compile_count=", + compile_count, ", execution_count=", execution_count, + ", cumulative_compile_time_us=", cumulative_compile_time_us, + ", is_megamorphic=", is_megamorphic, "}"); + } + }; + + // Returns the compilation statistics for the given cluster. + absl::StatusOr GetCompileStats( + const NameAttrList& function) const; + + // Determines whether the cluster should be compiled. Creates and inserts an + // entry into stats (also calls `RegisterExecution`) for `function` if it + // doesn't already exist. + virtual bool ShouldCompileCluster(const NameAttrList& function, + DeviceCompileMode compile_mode, + int64_t current_request_count); + + // Registers a cluster execution. Increments the execution count for the given + // cluster and also determines whether the cluster has gone megamorphic (and + // sets the megamorphic bit accordingly). + void RegisterExecution(const NameAttrList& function); + + // Registers a cluster compilation. Increments the compilation count and + // accumulates the compile time for the given cluster. Also broadcasts an + // XlaJitCompilationActivity. + virtual absl::Status RegisterCompilation(const NameAttrList& function, + int64_t compile_time_us, + bool used_persistent_cache); + + void IncrementOngoingAsyncCompilations(); + void DecrementOngoingAsyncCompilations(); + int64_t GetNumOngoingAsyncCompilations() const; + std::string DebugString() const override; + + private: + mutable mutex mu_; + + // Maps cluster names to compilation statistics for said cluster. + absl::flat_hash_map cluster_compile_stats_ + TF_GUARDED_BY(mu_); + + int64_t num_ongoing_compilations_ TF_GUARDED_BY(mu_) = 0; + + DeviceCompilationProfiler(const DeviceCompilationProfiler&) = delete; + void operator=(const DeviceCompilationProfiler&) = delete; +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_DEVICE_COMPILATION_PROFILER_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compilation_profiler_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compilation_profiler_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..a73ac9bd87d6d2e1c5bf16c5f0564a082cf5ab22 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compilation_profiler_test.cc @@ -0,0 +1,243 @@ +/* Copyright 2022 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/compiler/jit/device_compilation_profiler.h" + +#include +#include +#include + +#include +#include +#include "tensorflow/compiler/jit/tests/device_compiler_test_helper.h" +#include "tensorflow/compiler/jit/xla_activity.pb.h" +#include "tensorflow/core/framework/attr_value.pb.h" + +namespace tensorflow { +namespace { + +TEST(DeviceCompilationProfilerTest, RegisterExecution) { + DeviceCompilationProfiler* profiler = new DeviceCompilationProfiler(); + core::ScopedUnref profiler_ref(profiler); + + NameAttrList function; + function.set_name("TestFunc"); + + for (int i = 0; i < 5; ++i) { + profiler->RegisterExecution(function); + } + TF_ASSERT_OK_AND_ASSIGN(auto stats, profiler->GetCompileStats(function)); + EXPECT_EQ(stats.execution_count, 5); +} + +TEST(DeviceCompilationProfilerTest, RegisterCompilation) { + DeviceCompilationProfiler* profiler = new DeviceCompilationProfiler(); + core::ScopedUnref profiler_ref(profiler); + + auto listener = std::make_unique(); + auto listener_ptr = listener.get(); + RegisterXlaActivityListener(std::move(listener)); + + NameAttrList function; + function.set_name("TestFunc"); + + std::vector expected_activities; + for (int i = 0; i < 5; ++i) { + EXPECT_TRUE(profiler->RegisterCompilation(function, 4, false).ok()); + + TF_ASSERT_OK_AND_ASSIGN(auto stats, profiler->GetCompileStats(function)); + XlaJitCompilationActivity expected_activity; + expected_activity.set_cluster_name(function.name()); + expected_activity.set_compile_count(stats.compile_count); + expected_activity.set_compile_time_us(4); + expected_activity.set_cumulative_compile_time_us( + stats.cumulative_compile_time_us); + expected_activity.set_used_persistent_cache(false); + expected_activities.push_back(expected_activity); + } + + TF_ASSERT_OK_AND_ASSIGN(auto stats, profiler->GetCompileStats(function)); + EXPECT_EQ(stats.compile_count, 5); + EXPECT_EQ(stats.cumulative_compile_time_us, 5 * 4); + + // TODO(b/255826209): Use ::testing::EqualsProto once b/135192747 is fixed. + const auto& actual_activities = listener_ptr->GetListenerHistory(); + EXPECT_EQ(actual_activities.size(), expected_activities.size()); + for (size_t i = 0; i < actual_activities.size(); ++i) { + EXPECT_EQ(actual_activities[i].SerializeAsString(), + expected_activities[i].SerializeAsString()); + } +} + +TEST(DeviceCompilationProfilerTest, OngoingAsyncCompilations) { + DeviceCompilationProfiler* profiler = new DeviceCompilationProfiler(); + core::ScopedUnref profiler_ref(profiler); + + for (int i = 0; i < 5; ++i) { + profiler->IncrementOngoingAsyncCompilations(); + } + + EXPECT_EQ(profiler->GetNumOngoingAsyncCompilations(), 5); + + for (int i = 0; i < 5; ++i) { + profiler->DecrementOngoingAsyncCompilations(); + } + + EXPECT_EQ(profiler->GetNumOngoingAsyncCompilations(), 0); + + for (int i = 0; i < 5; ++i) { + profiler->IncrementOngoingAsyncCompilations(); + profiler->DecrementOngoingAsyncCompilations(); + } + + EXPECT_EQ(profiler->GetNumOngoingAsyncCompilations(), 0); +} + +TEST(DeviceCompilationProfilerTest, ShouldCompileClusterNotFound) { + DeviceCompilationProfiler* profiler = new DeviceCompilationProfiler(); + core::ScopedUnref profiler_ref(profiler); + + NameAttrList function; + function.set_name("TestFunc"); + + EXPECT_TRUE( + profiler->ShouldCompileCluster(function, DeviceCompileMode::kAsync, 0)); + EXPECT_TRUE( + profiler->ShouldCompileCluster(function, DeviceCompileMode::kLazy, 0)); + EXPECT_TRUE( + profiler->ShouldCompileCluster(function, DeviceCompileMode::kStrict, 0)); +} + +TEST(DeviceCompilationProfilerTest, ShouldCompileClusterFirstExecution) { + DeviceCompilationProfiler* profiler = new DeviceCompilationProfiler(); + core::ScopedUnref profiler_ref(profiler); + + NameAttrList function; + function.set_name("TestFunc"); + + profiler->RegisterExecution(function); + + EXPECT_TRUE( + profiler->ShouldCompileCluster(function, DeviceCompileMode::kAsync, 0)); + EXPECT_TRUE( + profiler->ShouldCompileCluster(function, DeviceCompileMode::kLazy, 0)); +} + +TEST(DeviceCompilationProfilerTest, ShouldCompileClusterMegamorphic) { + DeviceCompilationProfiler* profiler = new DeviceCompilationProfiler(); + core::ScopedUnref profiler_ref(profiler); + + NameAttrList function; + function.set_name("TestFunc"); + + const int64_t kCompileThreshold = 10; + const int64_t kMinExecutionsPerCompile = 50; + + // Register compilation enough times (without registering executions enough + // times) so that the function is marked megamorphic. + for (int i = 0; i < kCompileThreshold + 1; ++i) { + EXPECT_TRUE(profiler->RegisterCompilation(function, 1, false).ok()); + } + profiler->RegisterExecution(function); + + // Shouldn't compile cluster since it has gone megamorphic. + EXPECT_FALSE( + profiler->ShouldCompileCluster(function, DeviceCompileMode::kAsync, 0)); + EXPECT_FALSE( + profiler->ShouldCompileCluster(function, DeviceCompileMode::kLazy, 0)); + TF_ASSERT_OK_AND_ASSIGN(auto stats, profiler->GetCompileStats(function)); + EXPECT_TRUE(stats.is_megamorphic); + + // Always compile for strict compile mode. + EXPECT_TRUE( + profiler->ShouldCompileCluster(function, DeviceCompileMode::kStrict, 0)); + + // Once a cluster has gone megamorphic, it remains megamorphic (even though + // it's being executed more frequently now) and shouldn't be compiled again. + for (int i = 0; i < kCompileThreshold * kMinExecutionsPerCompile + 1; ++i) { + profiler->RegisterExecution(function); + } + + EXPECT_FALSE( + profiler->ShouldCompileCluster(function, DeviceCompileMode::kAsync, 0)); + EXPECT_FALSE( + profiler->ShouldCompileCluster(function, DeviceCompileMode::kLazy, 0)); + TF_ASSERT_OK_AND_ASSIGN(stats, profiler->GetCompileStats(function)); + EXPECT_TRUE(stats.is_megamorphic); + + // Always compile for strict compile mode. + EXPECT_TRUE( + profiler->ShouldCompileCluster(function, DeviceCompileMode::kStrict, 0)); +} + +TEST(DeviceCompilationProfilerTest, ShouldCompileClusterAsync) { + DeviceCompilationProfiler* profiler = new DeviceCompilationProfiler(); + core::ScopedUnref profiler_ref(profiler); + + NameAttrList function; + function.set_name("TestFunc"); + + const int64_t kMaxNumOngoingCompilations = 10; + for (int i = 0; i < kMaxNumOngoingCompilations; ++i) { + profiler->IncrementOngoingAsyncCompilations(); + } + + // Should allow compilation since this is the first execution. + profiler->RegisterExecution(function); + EXPECT_TRUE( + profiler->ShouldCompileCluster(function, DeviceCompileMode::kAsync, 0)); + + // Should not allow compilation since this is not the first execution and + // we've already reached the maximum number of ongoing compilations allowed. + profiler->RegisterExecution(function); + EXPECT_FALSE( + profiler->ShouldCompileCluster(function, DeviceCompileMode::kAsync, 0)); + + profiler->DecrementOngoingAsyncCompilations(); + // Should allow compilation since we've decremented the number of ongoing + // compilations. + EXPECT_TRUE( + profiler->ShouldCompileCluster(function, DeviceCompileMode::kAsync, 0)); +} + +TEST(DeviceCompilationProfilerTest, ShouldCompileClusterLazy) { + DeviceCompilationProfiler* profiler = new DeviceCompilationProfiler(); + core::ScopedUnref profiler_ref(profiler); + + NameAttrList function; + function.set_name("TestFunc"); + + constexpr int64_t kDefaultCompilationThreshold = 2; + + // Should allow compilation since this is the first execution. + profiler->RegisterExecution(function); + EXPECT_TRUE( + profiler->ShouldCompileCluster(function, DeviceCompileMode::kLazy, 0)); + + // Shouldn't allow compilation until compilation has been requested at least + // kDefaultCompilationThreshold times. + profiler->RegisterExecution(function); + for (int current_request_count = 0; + current_request_count < kDefaultCompilationThreshold; + ++current_request_count) { + EXPECT_FALSE(profiler->ShouldCompileCluster( + function, DeviceCompileMode::kLazy, current_request_count)); + } + EXPECT_TRUE(profiler->ShouldCompileCluster(function, DeviceCompileMode::kLazy, + kDefaultCompilationThreshold)); +} + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compiler.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compiler.h new file mode 100644 index 0000000000000000000000000000000000000000..dab515415374cd56873840afc3d889b181a50c2c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compiler.h @@ -0,0 +1,573 @@ +/* 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_DEVICE_COMPILER_H_ +#define TENSORFLOW_COMPILER_JIT_DEVICE_COMPILER_H_ + +#include +#include +#include +#include +#include + +#include "absl/algorithm/container.h" +#include "absl/base/call_once.h" +#include "absl/base/nullability.h" +#include "absl/container/flat_hash_map.h" +#include "absl/status/status.h" +#include "absl/types/span.h" +#include "tensorflow/compiler/jit/device_compilation_cache.h" +#include "tensorflow/compiler/jit/device_compilation_cluster_signature.h" +#include "tensorflow/compiler/jit/device_compilation_profiler.h" +#include "tensorflow/compiler/jit/device_compiler_client.h" +#include "tensorflow/compiler/jit/device_executable_persistor.h" +#include "tensorflow/compiler/jit/flags.h" +#include "tensorflow/compiler/jit/tf_graph_to_hlo_compiler.h" +#include "tensorflow/compiler/jit/xla_compile_util.h" +#include "tensorflow/compiler/tf2xla/xla_compiler.h" +#include "xla/tsl/platform/statusor.h" +#include "tensorflow/core/framework/metrics.h" +#include "tensorflow/core/framework/op_kernel.h" +#include "tensorflow/core/framework/resource_base.h" +#include "tensorflow/core/lib/core/threadpool.h" +#include "tensorflow/core/platform/mutex.h" +#include "tensorflow/core/platform/thread_annotations.h" + +namespace tensorflow { + +// Compiles/lowers a given Tensorflow graph/function/cluster into a compiled XLA +// compilation (HLO) using the XlaCompiler and compiles the resulting +// XlaCompilationResult into an `ExecutableType` (eg. xla::LocalExecutable) by +// calling `ClientType` (eg. xla::LocalClient). +// +// Caches the compiled XlaCompilationResult and Executable using a +// DeviceCompilationCache. Compilation is done only when there's a cache miss. +// +// Uses the DeviceExecutablePersistor class for persistence and tries to load a +// serialized executable from disk upon a request for compilation. If the +// appropriate executable isn't found on disk, compiles the given Tensorflow +// function/graph/cluster into an XlaCompilationResult (HLO) and +// `ExecutableType` and tries saving/persisting the compiled HLO and executable +// to disk. +// +// Since XLA computations must have static shapes, DeviceCompiler generates a +// new XLA computation for each new set of input shapes. +// TODO(b/255826209): De-templatize once we've moved to Device API completely. +template +class DeviceCompiler : public ResourceBase { + public: + DeviceCompiler( + std::unique_ptr> + persistor, + std::unique_ptr> + compiler_client); + ~DeviceCompiler() override; + + enum class CompileScope { + kOp, + kFunction, + }; + + // Compiles a function into a XlaCompiler::CompilationResult that can be used + // to execute an XLA Computation. Compilation results are cached. Compilation + // is skipped if there is a cache hit. `function` is the name of a Tensorflow + // function to compile. `args` is a description of the arguments to the + // computation. + // + // `compile_mode` controls the behavior of the compilation cache on a cache + // miss. If `compile_mode` is `kLazy` then, based on some profitability + // heuristics, the compilation cache may decide not to compile the cluster at + // this time. In this case it returns null into both `out_compilation_result` + // and `out_executable`. If `compile_mode` is `kStrict` then the compilation + // cache always attempts the compilation on a cache miss. If compilation mode + // is 'kAsync' compilation of the cluster happens in the background while the + // fallback path executes. + // + // The result of compilation is written to `*out_compilation_result`, which + // must be non-null. If `out_executable` is non-null, also builds an + // `ExecutableType` and sets `out_executable` to point to it. The + // resulting executable pointer may be null if the computation has no + // non-constant outputs. + absl::Status CompileIfNeeded( + const XlaCompiler::Options& options, const NameAttrList& function, + const std::vector& args, + const XlaCompiler::CompileOptions& compile_options, + DeviceCompileMode compile_mode, DeviceCompilationProfiler* profiler, + const XlaCompiler::CompilationResult** out_compilation_result, + ExecutableType** out_executable); + + // As above, but for a single op. + absl::Status CompileSingleOpIfNeeded( + const XlaCompiler::Options& options, + const std::vector& args, + const XlaCompiler::CompileOptions& compile_options, OpKernelContext* ctx, + DeviceCompilationProfiler* profiler, + const XlaCompiler::CompilationResult** out_compilation_result, + ExecutableType** out_executable); + + // An override that allows the caller to specify the function explicitly. + absl::Status CompileSingleOpIfNeeded( + const XlaCompiler::Options& options, const NameAttrList& function, + const DeviceCompilationCanonicalFunction& canonical_function, + const std::vector& args, + const XlaCompiler::CompileOptions& compile_options, OpKernelContext* ctx, + DeviceCompilationProfiler* profiler, + const XlaCompiler::CompilationResult** out_compilation_result, + ExecutableType** out_executable); + + ClientType* client() const { return compiler_client_->client(); } + const DeviceType& device_type() const { return persistor_->device_type(); } + DeviceCompilationCache* cache() { return cache_.get(); } + DeviceExecutablePersistor* persistor() { + return persistor_.get(); + } + DeviceCompilerClient* compiler_client() { + return compiler_client_.get(); + } + + std::string DebugString() const override; + + private: + // Common implementation of Compile and CompileSingleOp. The `OpKernelContext` + // parameter is always null for the former. + absl::Status CompileImpl( + const XlaCompiler::CompileOptions& compile_options, + const XlaCompiler::Options& options, const NameAttrList& function, + const DeviceCompilationCanonicalFunction& canonical_function, + const std::vector& args, CompileScope scope, + DeviceCompileMode compile_mode, OpKernelContext* ctx, + DeviceCompilationProfiler* profiler, + const XlaCompiler::CompilationResult** out_compilation_result, + ExecutableType** out_executable); + + StatusOr::Value> + CompileStrict( + const DeviceCompilationClusterSignature& sig, + const XlaCompiler::CompileOptions& compile_options, + const XlaCompiler::Options& options, + const std::vector& args, + const NameAttrList& function, + typename DeviceCompilationCache::Value cache_value, + CompileScope scope, OpKernelContext* ctx, + DeviceCompilationProfiler* profiler, mutex* mu) + TF_EXCLUSIVE_LOCKS_REQUIRED(*mu); + + absl::Status CompileAsynchronous( + const DeviceCompilationClusterSignature& sig, + const XlaCompiler::CompileOptions& compile_options, + const XlaCompiler::Options& options, + const std::vector& args, + const NameAttrList& function, CompileScope scope, OpKernelContext* ctx, + DeviceCompilationProfiler* profiler); + + // Releases all references held to `std::shared_ptr` + // held by the cache. + // + // This is to be called during session finalization, after all compilation + // has completed and computations no longer need to be accessed through the + // cache. + void Finalize() override; + + std::unique_ptr> + persistor_; + std::unique_ptr> + compiler_client_; + std::unique_ptr> cache_; + + // Pool of threads for asynchronous compilations. + std::unique_ptr async_compiler_threads_; + + mutex cluster_mutexes_mu_; + absl::flat_hash_map, + DeviceCompilationClusterSignature::Hash> + cluster_mutexes_ TF_GUARDED_BY(cluster_mutexes_mu_); + + DeviceCompiler(const DeviceCompiler&) = delete; + void operator=(const DeviceCompiler&) = delete; +}; + +namespace device_compiler_internal { +// Print something that users can search for to definitively ascertain that XLA +// was used for their TF model. +// Prints only once to avoid spamming LOG(INFO). +inline void LogOnceXlaCompiledFirstCluster() { + static absl::once_flag log_once; + absl::call_once(log_once, [] { + LOG(INFO) << "Compiled cluster using XLA! This line is logged at most " + "once for the lifetime of the process."; + }); +} + +template +inline absl::Status EligibleToPersist(DeviceCompileState compile_state, + const ExecutableType* executable) { + if (compile_state != DeviceCompileState::kCompiled) { + return absl::FailedPreconditionError( + "Cache entry to serialize is not compiled."); + } + if (executable == nullptr) { + return absl::FailedPreconditionError( + "LocalExecutable not found for cache entry to serialize."); + } + return absl::OkStatus(); +} +} // namespace device_compiler_internal + +template +DeviceCompiler::DeviceCompiler( + std::unique_ptr> + persistor, + std::unique_ptr> + compiler_client) + : persistor_(std::move(persistor)), + compiler_client_(std::move(compiler_client)) { + cache_ = std::make_unique>(); + async_compiler_threads_ = std::make_unique( + tensorflow::Env::Default(), "async_compiler_threads", + kNumAsyncDeviceCompilerThreads); +} + +template +DeviceCompiler::~DeviceCompiler() { + // Since programs are owned by the cache, ensure any use of our programs have + // completed by waiting for all stream executors to complete. + compiler_client_->WaitForProgramsToFinish(); + // Wait for all outstanding compilations to finish. + // Resetting the pointer explicitly in the top level destructor. + // Without this, the pointer would be reset when the AsyncCompilationState + // is destructed, which is dependent on the order of the members in the + // DeviceCompiler class, which is error prone if the order changes. + async_compiler_threads_.reset(); + // TODO(b/110813685): Think about the program ownership model. Programs are + // currently owned by the compilation cache which means we must wait for + // program completion in the destructor. There are multiple compilation caches + // around, which complicates things a little. Perhaps having programs be + // shared_ptrs (an invasive change) would make the model easier to reason + // about? +} + +template +std::string DeviceCompiler::DebugString() const { + return "DeviceCompiler"; +} + +template +absl::Status DeviceCompiler::CompileIfNeeded( + const XlaCompiler::Options& options, const NameAttrList& function, + const std::vector& args, + const XlaCompiler::CompileOptions& compile_options, + DeviceCompileMode compile_mode, DeviceCompilationProfiler* profiler, + const XlaCompiler::CompilationResult** out_compilation_result, + ExecutableType** out_executable) { + return CompileImpl(compile_options, options, function, Canonicalize(function), + args, CompileScope::kFunction, compile_mode, + /*ctx=*/nullptr, profiler, out_compilation_result, + out_executable); +} + +inline NameAttrList GetDeviceCompilerFunction(const NodeDef& def) { + NameAttrList function; + function.set_name(def.op()); + *function.mutable_attr() = def.attr(); + // Remove the "_class" attribute from the attribute set used to create the + // compilation cache key. This attribute is information for the colocator + // and causes false uniqueness between nodes. + function.mutable_attr()->erase("_class"); + return function; +} + +template +absl::Status +DeviceCompiler::CompileSingleOpIfNeeded( + const XlaCompiler::Options& options, + const std::vector& args, + const XlaCompiler::CompileOptions& compile_options, OpKernelContext* ctx, + DeviceCompilationProfiler* profiler, + const XlaCompiler::CompilationResult** out_compilation_result, + ExecutableType** out_executable) { + const NodeDef& def = ctx->op_kernel().def(); + const NameAttrList function = GetDeviceCompilerFunction(def); + return CompileSingleOpIfNeeded(options, function, Canonicalize(function), + args, compile_options, ctx, profiler, + out_compilation_result, out_executable); +} + +template +absl::Status +DeviceCompiler::CompileSingleOpIfNeeded( + const XlaCompiler::Options& options, const NameAttrList& function, + const DeviceCompilationCanonicalFunction& canonical_function, + const std::vector& args, + const XlaCompiler::CompileOptions& compile_options, OpKernelContext* ctx, + DeviceCompilationProfiler* profiler, + const XlaCompiler::CompilationResult** out_compilation_result, + ExecutableType** out_executable) { + return CompileImpl(compile_options, options, function, canonical_function, + args, CompileScope::kOp, DeviceCompileMode::kStrict, ctx, + profiler, out_compilation_result, out_executable); +} + +template +StatusOr::Value> +DeviceCompiler::CompileStrict( + const DeviceCompilationClusterSignature& sig, + const XlaCompiler::CompileOptions& compile_options, + const XlaCompiler::Options& options, + const std::vector& args, + const NameAttrList& function, + typename DeviceCompilationCache::Value cache_value, + CompileScope scope, OpKernelContext* ctx, + DeviceCompilationProfiler* profiler, mutex* mu) { + tensorflow::Env* env = tensorflow::Env::Default(); + const uint64_t compile_start_us = env->NowMicros(); + + TfGraphToHloCompiler compiler(options); + cache_value.compile_state = DeviceCompileState::kCompiled; + + std::unique_ptr out_executable; + auto out_compilation_result = + std::make_unique(); + + if (scope == CompileScope::kOp) { + cache_value.compilation_status = compiler.CompileSingleOp( + compile_options, ctx, args, out_compilation_result.get()); + } else { + CHECK(scope == CompileScope::kFunction); // Crash OK + cache_value.compilation_status = compiler.Compile( + compile_options, function, args, out_compilation_result.get()); + } + TF_RETURN_IF_ERROR(cache_value.compilation_status); + TF_RET_CHECK(cache_value.executable == nullptr); + TF_RET_CHECK(out_compilation_result->computation != nullptr); + + auto loaded_executable = persistor_->TryToLoadExecutable( + DeviceCompilationClusterSignature::Hash()(sig), sig.HumanString(), + options, *out_compilation_result, compiler_client_.get()); + + if (loaded_executable.has_value()) { + cache_value.compilation_status = loaded_executable->status(); + if (loaded_executable->ok()) { + out_executable = *std::move(*loaded_executable); + metrics::UpdatePersistentCacheLoadCount(); + } + } else { + auto built_executable = + compiler_client_->BuildExecutable(options, *out_compilation_result); + TF_RETURN_IF_ERROR(built_executable.status()); + out_executable = *std::move(built_executable); + + TF_RETURN_IF_ERROR( + device_compiler_internal::EligibleToPersist( + cache_value.compile_state, out_executable.get())); + TF_RETURN_IF_ERROR(persistor_->TryToPersistExecutable( + DeviceCompilationClusterSignature::Hash()(sig), sig.HumanString(), + options, *out_compilation_result, *out_executable, + compiler_client_.get())); + } + + cache_value.compilation_result = out_compilation_result.get(); + cache_value.executable = out_executable.get(); + cache_->Store(sig, cache_value.compile_state, cache_value.compilation_status, + std::move(out_compilation_result), std::move(out_executable)); + + // Finalize the cache to release the XlaComputation after it was compiled. + cache_->Finalize(); + + const uint64_t compile_end_us = env->NowMicros(); + const uint64_t compile_time_us = compile_end_us - compile_start_us; + + device_compiler_internal::LogOnceXlaCompiledFirstCluster(); + TF_RETURN_IF_ERROR(profiler->RegisterCompilation( + function, compile_time_us, loaded_executable.has_value())); + return cache_value; +} + +template +absl::Status DeviceCompiler::CompileAsynchronous( + const DeviceCompilationClusterSignature& signature, + const XlaCompiler::CompileOptions& compile_options, + const XlaCompiler::Options& options, + const std::vector& args, + const NameAttrList& function, CompileScope scope, OpKernelContext* ctx, + DeviceCompilationProfiler* profiler) { + // Explicitly capture all required data by value for async compilation. + // Update compilation state in cache. + cache_->Store(signature, DeviceCompileState::kCompiling, std::nullopt, + std::nullopt, std::nullopt); + profiler->IncrementOngoingAsyncCompilations(); + // Don't move the above code into the thread function as it synchronously + // updates the async compilation state! + + // When the ThreadPool for the compilation cache is destroyed, it waits for + // compilations to have finished. This means that both 'entry' and 'this' will + // be alive for the duration of the compilation. + // !!Pay attention when additional variables must be captured by this lambda!! + // All values are captured by value. Make sure that all pointer values (like + // entry) do not get freed until the lambda has finished. + const std::string& function_name = function.name(); + async_compiler_threads_->Schedule([=] { + VLOG(2) << "Starting asynchronous compilation of cluster " << function_name + << '.'; + // We don't need to lock mu, but do it anyway to satisfy thread safety + // analysis. + mutex mu; + mutex_lock lock(mu); + auto cache_value = typename DeviceCompilationCache::Value(); + auto s = CompileStrict(signature, compile_options, options, args, function, + cache_value, scope, ctx, profiler, &mu); + VLOG(2) << "Finished asynchronous compililation of cluster " + << function_name << '.'; + profiler->DecrementOngoingAsyncCompilations(); + // Update compilation status in cache. + if (!s.ok()) { + cache_->Store(signature, std::nullopt, s.status(), std::nullopt, + std::nullopt); + } + }); + return absl::OkStatus(); +} + +template +void DeviceCompiler::Finalize() { + const mutex_lock lock(cluster_mutexes_mu_); + std::vector cluster_mutexes; + cluster_mutexes.reserve(cluster_mutexes_.size()); + for (auto& [_, mutex] : cluster_mutexes_) { + if (mutex != nullptr) { + cluster_mutexes.push_back(mutex.get()); + } + } + + // Sort the mutexes before locking to ensure that this happens in a + // deterministic order, consistent between resizes of the `cluster_mutexes_` + // map. + absl::c_sort(cluster_mutexes); + std::vector cluster_mutex_locks; + cluster_mutex_locks.reserve(cluster_mutexes.size()); + for (mutex* absl_nonnull const mutex : cluster_mutexes) { + cluster_mutex_locks.emplace_back(*mutex); + } + + cache_->Finalize(); +} + +template +absl::Status DeviceCompiler::CompileImpl( + const XlaCompiler::CompileOptions& compile_options, + const XlaCompiler::Options& options, const NameAttrList& function, + const DeviceCompilationCanonicalFunction& canonical_function, + const std::vector& args, CompileScope scope, + DeviceCompileMode compile_mode, OpKernelContext* ctx, + DeviceCompilationProfiler* profiler, + const XlaCompiler::CompilationResult** out_compilation_result, + ExecutableType** out_executable) { + DCHECK_NE(out_executable, nullptr); + VLOG(2) << "DeviceCompiler::Compile " << DebugString(); + + if (VLOG_IS_ON(2)) { + VLOG(2) << "num_inputs=" << args.size(); + for (int i = 0, end = args.size(); i < end; i++) { + VLOG(3) << i << ": " << args[i].HumanString(); + } + } + TF_ASSIGN_OR_RETURN(auto signature, DeviceCompilationClusterSignature::Build( + canonical_function, args)); + + // The outer lock protects the existence of the mutex in the map. + mutex* cluster_mutex; + { + mutex_lock lock(cluster_mutexes_mu_); + auto it = + cluster_mutexes_.emplace(signature, std::make_unique()).first; + cluster_mutex = it->second.get(); + } + + profiler->RegisterExecution(function); + + std::string human_signature; + if (VLOG_IS_ON(2)) { + human_signature = VLOG_IS_ON(3) ? signature.HumanString() : function.name(); + VLOG(2) << "DeviceCompilationClusterSignature: " << human_signature; + } + + // Acquire the cache entry lock and compile, if necessary. + // TODO(phawkins): this locking will need to be restructured when we implement + // cache eviction. + mutex_lock cluster_compile_lock(*cluster_mutex); + auto cache_value = cache_->LookupOrCreate(signature); + + int64_t current_request_count = cache_value.request_count; + VLOG(2) << "Compilation cache entry hit: " + << static_cast(cache_value.compile_state) + << " signature: " << human_signature << " with request count " + << current_request_count; + + DeviceCompileState state = cache_value.compile_state; + *out_compilation_result = nullptr; + *out_executable = nullptr; + + // Check if the requested entry is uncompiled and return an error if + // compilation is disabled. This will raise an error for kLazy even if we have + // not yet hit the compilation threshold and no compilation happens this + // round. This is to avoid non-determanism of when compilation is disallowed, + // for example by changing the threshold. + if (state == DeviceCompileState::kUncompiled && FailOnXlaCompilation()) { + VLOG(1) << "XLA compilation disabled: " << function.name() << "\n" + << absl::StrJoin( + args, "\n", + [](std::string* out, const XlaCompiler::Argument& arg) { + absl::StrAppend(out, " arg: ", arg.HumanString()); + }); + + return absl::InternalError("XLA compilation disabled"); + } + + if (state == DeviceCompileState::kUncompiled) { + XLA_SCOPED_LOGGING_TIMER("Compilation of XLA executable"); + if (!profiler->ShouldCompileCluster(function, compile_mode, + current_request_count)) { + VLOG(2) << "Not compiling for signature: " << human_signature; + return absl::OkStatus(); + } else if (compile_mode == DeviceCompileMode::kAsync) { + VLOG(2) << "Queueing asynchronous compilation for signature: " + << human_signature; + TF_RETURN_IF_ERROR(CompileAsynchronous(signature, compile_options, + options, args, function, scope, + ctx, profiler)); + return absl::OkStatus(); + } else { + VLOG(2) << "Instantly compiling for signature: " << human_signature; + TF_ASSIGN_OR_RETURN( + cache_value, + CompileStrict(signature, compile_options, options, args, function, + cache_value, scope, ctx, profiler, cluster_mutex)); + } + } else if (state == DeviceCompileState::kCompiling) { + VLOG(2) << "Ongoing asynchronous compilation for signature: " + << human_signature; + return absl::OkStatus(); + } else if (state == DeviceCompileState::kCompiled) { + VLOG(2) << "Already Compiled for signature: " << human_signature; + } + + TF_RETURN_IF_ERROR(cache_value.compilation_status); + *out_compilation_result = cache_value.compilation_result; + *out_executable = cache_value.executable; + return absl::OkStatus(); +} + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_DEVICE_COMPILER_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compiler_client.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compiler_client.cc new file mode 100644 index 0000000000000000000000000000000000000000..d845a4e83e00115a92393596d41fde782bd969bb --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compiler_client.cc @@ -0,0 +1,46 @@ +/* Copyright 2022 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/compiler/jit/device_compiler_client.h" + +#include "tensorflow/compiler/tf2xla/xla_compiler.h" +#include "tensorflow/core/util/determinism.h" + +namespace tensorflow { + +xla::ExecutableBuildOptions GetExecutableBuildOptions( + const XlaCompiler::Options& options, + const XlaCompiler::CompilationResult& result, int default_device_ordinal) { + xla::ExecutableBuildOptions build_options; + if (result.collective_info) { + build_options.set_num_replicas(result.collective_info->group_size); + } + if (options.device_ordinal != -1) { + build_options.set_device_ordinal(options.device_ordinal); + } else if (default_device_ordinal != -1) { + build_options.set_device_ordinal(default_device_ordinal); + } + build_options.set_result_layout(result.xla_output_shape); + build_options.set_device_allocator(options.device_allocator.get()); + build_options.set_alias_passthrough_params(options.alias_passthrough_params); + build_options.mutable_debug_options()->set_xla_detailed_logging( + options.detailed_logging); + if (tensorflow::OpDeterminismRequired()) { + build_options.mutable_debug_options()->set_xla_gpu_deterministic_ops(true); + } + return build_options; +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compiler_client.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compiler_client.h new file mode 100644 index 0000000000000000000000000000000000000000..0ef68a9ae995264b1060e3a80cd713d500355f65 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compiler_client.h @@ -0,0 +1,76 @@ +/* Copyright 2022 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_DEVICE_COMPILER_CLIENT_H_ +#define TENSORFLOW_COMPILER_JIT_DEVICE_COMPILER_CLIENT_H_ + +#include +#include +#include + +#include "tensorflow/compiler/tf2xla/xla_compiler.h" +#include "xla/client/executable_build_options.h" + +namespace tensorflow { + +template +class DeviceCompilerClient { + public: + DeviceCompilerClient() = default; + virtual ~DeviceCompilerClient() = default; + + // Compiles `result` (HLO) to an `ExecutableType` using `ClientType` and + // returns it. + virtual StatusOr> BuildExecutable( + const XlaCompiler::Options& options, + const XlaCompiler::CompilationResult& result) = 0; + + // Serializes an available `executable` to string using `ClientType` and + // returns it. + virtual absl::StatusOr SerializeExecutable( + const ExecutableType& executable) = 0; + + // Compiles `result` (HLO) to a serializable executable (eg. + // xla::AotCompilationResult) using `ClientType`, serializes it to string and + // returns it. + virtual absl::StatusOr BuildSerializedExecutable( + const XlaCompiler::Options& options, + const XlaCompiler::CompilationResult& result) = 0; + + // Loads `serialized_executable` into an `ExecutableType` using `ClientType`. + virtual StatusOr> LoadExecutable( + const XlaCompiler::Options& options, + const XlaCompiler::CompilationResult& result, + const std::string& serialized_executable) = 0; + + // Waits for the underlying `ClientType` backend's programs to finish + // executing before returning. + virtual void WaitForProgramsToFinish() = 0; + + virtual ClientType* client() const = 0; + + private: + DeviceCompilerClient(const DeviceCompilerClient&) = delete; + void operator=(const DeviceCompilerClient&) = delete; +}; + +// Generates the ExecutableBuildOptions for compilation from HLO to +// executable. +xla::ExecutableBuildOptions GetExecutableBuildOptions( + const XlaCompiler::Options& options, + const XlaCompiler::CompilationResult& result, int default_device_ordinal); +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_DEVICE_COMPILER_CLIENT_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compiler_client_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compiler_client_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..b46725f5794c8d1b2ea6f3f0912977a1a33cfb6d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compiler_client_test.cc @@ -0,0 +1,76 @@ +/* Copyright 2022 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/compiler/jit/device_compiler_client.h" + +#include + +namespace tensorflow { +namespace { + +TEST(GetExecutableOptionTest, Basic) { + XlaCompiler::Options options; + options.device_ordinal = 0; + options.alias_passthrough_params = true; + options.detailed_logging = true; + XlaCompiler::CompilationResult result; + xla::Shape xla_output_shape; + result.xla_output_shape = xla_output_shape; + + auto build_option = + GetExecutableBuildOptions(options, result, /*default_device_ordinal=*/-1); + + EXPECT_EQ(build_option.device_ordinal(), 0); + EXPECT_EQ(build_option.result_layout()->ToString(), + xla_output_shape.ToString()); + EXPECT_EQ(build_option.alias_passthrough_params(), true); + EXPECT_EQ(build_option.debug_options().xla_detailed_logging(), true); + EXPECT_EQ(build_option.debug_options().xla_enable_dumping(), true); +} + +TEST(GetExecutableOptionTest, DefaultDeviceOrdinal) { + XlaCompiler::Options options; + XlaCompiler::CompilationResult result; + + auto build_option = + GetExecutableBuildOptions(options, result, /*default_device_ordinal=*/0); + + EXPECT_EQ(build_option.device_ordinal(), 0); +} + +TEST(GetExecutableOptionTest, DeviceOrdinalNotSet) { + XlaCompiler::Options options; + XlaCompiler::CompilationResult result; + + auto build_option = + GetExecutableBuildOptions(options, result, /*default_device_ordinal=*/-1); + + EXPECT_EQ(build_option.device_ordinal(), -1); +} + +TEST(GetExecutableOptionTest, DumpingWithoutDetailedLogging) { + XlaCompiler::Options options; + options.detailed_logging = false; + XlaCompiler::CompilationResult result; + + auto build_option = + GetExecutableBuildOptions(options, result, /*default_device_ordinal=*/-1); + + EXPECT_FALSE(build_option.debug_options().xla_detailed_logging()); + EXPECT_TRUE(build_option.debug_options().xla_enable_dumping()); +} + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compiler_disable_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compiler_disable_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..9a4dc1733157253fba421bf86399efba0c454b23 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compiler_disable_test.cc @@ -0,0 +1,89 @@ +/* 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 +#include +#include + +#include "tensorflow/compiler/jit/device_compilation_profiler.h" +#include "tensorflow/compiler/jit/flags.h" +#include "tensorflow/compiler/jit/device_compiler.h" +#include "tensorflow/compiler/jit/xla_device_compiler_client.h" +#include "tensorflow/compiler/tf2xla/xla_compiler.h" +#include "xla/client/client_library.h" +#include "tensorflow/core/platform/test.h" + +namespace tensorflow { +namespace { + +// This test is kept separate because it disables XLA compilation globally. +TEST(DeviceCompilerTest, TestDisabledXlaCompilation) { + NameAttrList fn; + fn.set_name("afunction"); + + // Create mock arguments so we see them in the VLOG when compilation fails. + std::vector args(2); + for (int i = 0; i < 2; ++i) { + args[i].kind = XlaCompiler::Argument::kParameter; + args[i].type = DT_INT32; + args[i].shape = TensorShape({2, i + 1}); + args[i].name = absl::StrCat("arg", i); + } + + DisableXlaCompilation(); + + xla::LocalClient* client = xla::ClientLibrary::LocalClientOrDie(); + DeviceType device_type = DeviceType(DEVICE_CPU_XLA_JIT); + + const XlaCompiler::CompilationResult* compilation_result; + xla::LocalExecutable* executable; + + using XlaDeviceExecutablePersistor = + DeviceExecutablePersistor; + auto persistor = std::make_unique( + XlaDeviceExecutablePersistor::Config(), device_type); + auto compiler_client = std::make_unique(client); + auto xla_device_compiler = + new DeviceCompiler( + std::move(persistor), std::move(compiler_client)); + core::ScopedUnref xla_device_compiler_ref(xla_device_compiler); + + auto profiler = new DeviceCompilationProfiler(); + core::ScopedUnref profiler_ref(profiler); + + // Check that strict compilation is disallowed. + absl::Status status = xla_device_compiler->CompileIfNeeded( + XlaCompiler::Options{}, fn, args, XlaCompiler::CompileOptions{}, + DeviceCompileMode::kStrict, profiler, &compilation_result, &executable); + EXPECT_FALSE(status.ok()); + EXPECT_TRUE(absl::StrContains(status.message(), "XLA compilation disabled")); + + // Check that async compilation is disallowed. + status = xla_device_compiler->CompileIfNeeded( + XlaCompiler::Options{}, fn, args, XlaCompiler::CompileOptions{}, + DeviceCompileMode::kAsync, profiler, &compilation_result, &executable); + EXPECT_FALSE(status.ok()); + EXPECT_TRUE(absl::StrContains(status.message(), "XLA compilation disabled")); + + // Check that lazy compilation is disallowed. + status = xla_device_compiler->CompileIfNeeded( + XlaCompiler::Options{}, fn, args, XlaCompiler::CompileOptions{}, + DeviceCompileMode::kLazy, profiler, &compilation_result, &executable); + EXPECT_FALSE(status.ok()); + EXPECT_TRUE(absl::StrContains(status.message(), "XLA compilation disabled")); +} + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compiler_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compiler_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..5f02022188fe2425d40a09be7cf703ddbd8d6b10 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_compiler_test.cc @@ -0,0 +1,673 @@ +/* Copyright 2022 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/compiler/jit/device_compiler.h" + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include "absl/algorithm/container.h" +#include "absl/log/check.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" +#include "absl/synchronization/notification.h" +#include "absl/time/clock.h" +#include "absl/time/time.h" +#include "tensorflow/cc/framework/scope.h" +#include "tensorflow/cc/ops/function_ops.h" +#include "tensorflow/cc/ops/math_ops.h" +#include "tensorflow/compiler/jit/device_compilation_cluster_signature.h" +#include "tensorflow/compiler/jit/device_compiler_client.h" +#include "tensorflow/compiler/jit/tests/device_compiler_test_helper.h" +#include "tensorflow/compiler/jit/tf_graph_to_hlo_compiler.pb.h" +#include "tensorflow/compiler/jit/xla_compile_util.h" +#include "tensorflow/compiler/jit/xla_device_compiler_client.h" +#include "tensorflow/compiler/tf2xla/xla_argument.h" +#include "tensorflow/compiler/tf2xla/xla_compiler.h" +#include "xla/client/client_library.h" +#include "xla/client/local_client.h" +#include "xla/hlo/builder/xla_computation.h" +#include "xla/stream_executor/platform_manager.h" +#include "xla/tsl/lib/core/status_test_util.h" +#include "xla/tsl/lib/strings/proto_serialization.h" +#include "xla/tsl/platform/statusor.h" +#include "tensorflow/core/framework/fake_input.h" +#include "tensorflow/core/framework/function.h" +#include "tensorflow/core/framework/graph_to_functiondef.h" +#include "tensorflow/core/framework/node_def_builder.h" +#include "tensorflow/core/framework/resource_base.h" +#include "tensorflow/core/kernels/ops_testutil.h" +#include "tensorflow/core/lib/core/status_test_util.h" +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/platform/errors.h" +#include "tensorflow/core/platform/status.h" +#include "tensorflow/core/platform/status_matchers.h" +#include "tensorflow/core/platform/statusor.h" +#include "tensorflow/core/platform/test.h" +#include "tsl/platform/path.h" + +namespace tensorflow { +namespace { +using ::testing::_; +using ::testing::ElementsAreArray; +using ::testing::EndsWith; +using ::testing::Eq; +using ::testing::EqualsProto; +using ::testing::IsTrue; +using ::testing::NotNull; +using ::testing::Return; +using ::testing::SizeIs; +using ::testing::StrEq; +using ::testing::UnorderedElementsAre; +using ::testing::status::IsOk; + +using XlaDeviceCompiler = + DeviceCompiler; +using XlaDeviceExecutablePersistor = + DeviceExecutablePersistor; +using Signature = DeviceCompilationClusterSignature; + +xla::LocalClient* GetLocalClient() { + // TODO(b/255826209): Figure out how to run this test with the CPU client as + // well. + auto platform = se::PlatformManager::PlatformWithName("cuda").value(); + return xla::ClientLibrary::GetOrCreateLocalClient(platform).value(); +} + +XlaDeviceCompiler* CreateXlaDeviceCompiler(bool enable_persistence = false) { + auto xla_compiler_client = + std::make_unique(GetLocalClient()); + auto xla_persistor = std::make_unique( + XlaDeviceExecutablePersistor::Config{ + enable_persistence ? testing::TmpDir() : "", false, "xla"}, + DeviceType(DEVICE_GPU_XLA_JIT)); + return new XlaDeviceCompiler(std::move(xla_persistor), + std::move(xla_compiler_client)); +} + +absl::StatusOr> SampleGraphAddXY() { + std::unique_ptr graph(new Graph(OpRegistry::Global())); + Scope scope = Scope::NewRootScope().ExitOnError(); + auto a = ops::_Arg(scope.WithOpName("A"), DT_INT32, 0); + auto b = ops::_Arg(scope.WithOpName("B"), DT_INT32, 1); + auto c = ops::Add(scope.WithOpName("C"), a, b); + auto d = ops::_Retval(scope.WithOpName("D"), c, 0); + TF_RETURN_IF_ERROR(scope.ToGraph(graph.get())); + return graph; +} + +absl::StatusOr SampleFuntionAddXY(const std::string& name) { + TF_ASSIGN_OR_RETURN(auto graph, SampleGraphAddXY()); + FunctionDef fdef; + TF_RETURN_IF_ERROR(GraphToFunctionDef(*graph, name, &fdef)); + return fdef; +} + +std::vector SampleArgsForAddXY() { + std::vector args(2); + args[0].kind = XlaCompiler::Argument::kParameter; + args[0].type = DT_INT32; + args[0].shape = TensorShape({2}); + args[1].kind = XlaCompiler::Argument::kParameter; + args[1].type = DT_INT32; + args[1].shape = TensorShape({2}); + return args; +} + +class MockXlaDeviceExecutablePersistor + : public DeviceExecutablePersistor { + public: + MockXlaDeviceExecutablePersistor() + : DeviceExecutablePersistor( + Config{testing::TmpDir(), false, "xla"}, + DeviceType(DEVICE_CPU_XLA_JIT)) {} + MOCK_METHOD(absl::Status, TryToPersistExecutable, + (uint64_t, const std::string&, const XlaCompiler::Options&, + const XlaCompiler::CompilationResult&, + const xla::LocalExecutable&, + (DeviceCompilerClient*)), + (const, override)); +}; + +class MockDeviceCompilationProfiler : public DeviceCompilationProfiler { + public: + MOCK_METHOD(bool, ShouldCompileCluster, + (const NameAttrList& function, DeviceCompileMode compile_mode, + int64_t current_request_count), + (override)); + MOCK_METHOD(absl::Status, RegisterCompilation, + (const NameAttrList& function, int64_t compile_time_us, + bool used_persistent_cache), + (override)); +}; + +class DeviceCompilerTest : public ::testing::Test { + protected: + void SetUp() override { + flib_def_ = std::make_unique( + OpRegistry::Global(), FunctionDefLibrary()); + TF_ASSERT_OK_AND_ASSIGN(auto fdef, SampleFuntionAddXY("foo")); + TF_ASSERT_OK(flib_def_->AddFunctionDef(fdef)); + + profiler_ = new DeviceCompilationProfiler(); + profiler_ref_ = std::make_unique(profiler_); + + mock_profiler_ = new MockDeviceCompilationProfiler(); + mock_profiler_ref_ = std::make_unique(mock_profiler_); + + xla_device_compiler_ = CreateXlaDeviceCompiler(); + xla_device_compiler_ref_ = + std::make_unique(xla_device_compiler_); + + auto listener = std::make_unique(); + listener_ = listener.get(); + RegisterXlaActivityListener(std::move(listener)); + } + + XlaCompiler::Options GetDefaultXlaOptions() { + XlaCompiler::Options options; + options.device_type = DeviceType(DEVICE_GPU_XLA_JIT); + options.client = xla_device_compiler_->client(); + options.flib_def = flib_def_.get(); + return options; + } + + absl::StatusOr> + BuildSampleXlaExecutable() { + TF_ASSIGN_OR_RETURN(auto graph, SampleGraphAddXY()); + auto args = SampleArgsForAddXY(); + + // Compiles the graph. + XlaCompiler compiler(GetDefaultXlaOptions()); + + XlaCompiler::CompilationResult compilation_result; + TF_RETURN_IF_ERROR(compiler.CompileGraph(XlaCompiler::CompileOptions(), + "graph", std::move(graph), args, + &compilation_result)); + return xla_device_compiler_->compiler_client()->BuildExecutable( + GetDefaultXlaOptions(), compilation_result); + } + + std::unique_ptr flib_def_; + JitCompilationListener* listener_; + + DeviceCompilationProfiler* profiler_; + std::unique_ptr profiler_ref_; + + MockDeviceCompilationProfiler* mock_profiler_; + std::unique_ptr mock_profiler_ref_; + + XlaDeviceCompiler* xla_device_compiler_; + std::unique_ptr xla_device_compiler_ref_; +}; + +TEST_F(DeviceCompilerTest, CompileStrictSuccess) { + const XlaCompiler::CompilationResult* compilation_result = nullptr; + xla::LocalExecutable* xla_executable = nullptr; + + XlaCompiler::Options options = GetDefaultXlaOptions(); + + NameAttrList fn; + fn.set_name("foo"); + + TF_EXPECT_OK(xla_device_compiler_->CompileIfNeeded( + options, fn, SampleArgsForAddXY(), XlaCompiler::CompileOptions{}, + DeviceCompileMode::kStrict, profiler_, &compilation_result, + &xla_executable)); + + EXPECT_TRUE(compilation_result != nullptr); + EXPECT_TRUE(xla_executable != nullptr); +} + +TEST_F(DeviceCompilerTest, CompileShouldCompileClusterFalse) { + const XlaCompiler::CompilationResult* compilation_result = nullptr; + xla::LocalExecutable* xla_executable = nullptr; + + XlaCompiler::Options options = GetDefaultXlaOptions(); + + NameAttrList fn; + fn.set_name("foo"); + + // Using a mock here since it's difficult to have ShouldCompileCluster() + // return false. + EXPECT_CALL(*mock_profiler_, + ShouldCompileCluster(_, DeviceCompileMode::kLazy, 1)) + .WillOnce(Return(false)); + + TF_EXPECT_OK(xla_device_compiler_->CompileIfNeeded( + options, fn, SampleArgsForAddXY(), XlaCompiler::CompileOptions{}, + DeviceCompileMode::kLazy, mock_profiler_, &compilation_result, + &xla_executable)); + + EXPECT_TRUE(compilation_result == nullptr); + EXPECT_TRUE(xla_executable == nullptr); +} + +TEST_F(DeviceCompilerTest, CompileCacheHit) { + const XlaCompiler::CompilationResult* compilation_result = nullptr; + xla::LocalExecutable* xla_executable = nullptr; + + XlaCompiler::Options options = GetDefaultXlaOptions(); + + NameAttrList fn; + fn.set_name("foo"); + + TF_EXPECT_OK(xla_device_compiler_->CompileIfNeeded( + options, fn, SampleArgsForAddXY(), XlaCompiler::CompileOptions{}, + DeviceCompileMode::kStrict, profiler_, &compilation_result, + &xla_executable)); + + EXPECT_TRUE(compilation_result != nullptr); + EXPECT_TRUE(xla_executable != nullptr); + + const XlaCompiler::CompilationResult* new_compilation_result = nullptr; + xla::LocalExecutable* new_xla_executable = nullptr; + + // Request compiling the same function again. + TF_EXPECT_OK(xla_device_compiler_->CompileIfNeeded( + options, fn, SampleArgsForAddXY(), XlaCompiler::CompileOptions{}, + DeviceCompileMode::kStrict, profiler_, &new_compilation_result, + &new_xla_executable)); + + // new_compilation_result and new_xla_executable should point to the + // compilation_result and executable returned after the first compilation + // request. + EXPECT_EQ(compilation_result, new_compilation_result); + EXPECT_EQ(xla_executable, new_xla_executable); +} + +TEST_F(DeviceCompilerTest, CompileAsyncSuccess) { + const XlaCompiler::CompilationResult* compilation_result = nullptr; + xla::LocalExecutable* xla_executable = nullptr; + + XlaCompiler::Options options = GetDefaultXlaOptions(); + + NameAttrList fn; + fn.set_name("foo"); + + // Using a mock here to determine when the async compilation finishes. This is + // to avoid using absl::SleepFor(). + // `RegisterCompilation` is the last call that happens just before the async + // compilation completes. We use the completion of this call to determine when + // the compilation finshes to verify expected behavior. + absl::Notification done; + EXPECT_CALL(*mock_profiler_, + ShouldCompileCluster(_, DeviceCompileMode::kAsync, 1)) + .WillOnce(Return(true)); + EXPECT_CALL(*mock_profiler_, RegisterCompilation(_, _, false)) + .WillOnce([&done] { + done.Notify(); + return absl::OkStatus(); + }); + + auto args = SampleArgsForAddXY(); + TF_EXPECT_OK(xla_device_compiler_->CompileIfNeeded( + options, fn, args, XlaCompiler::CompileOptions{}, + DeviceCompileMode::kAsync, mock_profiler_, &compilation_result, + &xla_executable)); + + // compilation_result and xla_executable aren't available immediately after + // requesting compilation in asynchronous mode. + EXPECT_TRUE(compilation_result == nullptr); + EXPECT_TRUE(xla_executable == nullptr); + + // Check if an appropriate entry is made in xla_cache. + auto xla_cache = xla_device_compiler_->cache(); + TF_ASSERT_OK_AND_ASSIGN(auto signature, Signature::Build(fn, args)); + + auto cache_value = xla_cache->Lookup(signature); + EXPECT_TRUE(cache_value); + EXPECT_TRUE(cache_value->compile_state != DeviceCompileState::kUncompiled); + + // Wait for async compilation to complete. + done.WaitForNotification(); + cache_value = xla_cache->Lookup(signature); + EXPECT_TRUE(cache_value); + EXPECT_TRUE(cache_value->compile_state == DeviceCompileState::kCompiled); + EXPECT_TRUE(cache_value->compilation_result != nullptr); + EXPECT_TRUE(cache_value->executable != nullptr); + EXPECT_TRUE(cache_value->compilation_status.ok()); +} + +TEST_F(DeviceCompilerTest, CompilePersistentCacheEnabled) { + auto xla_device_compiler = + CreateXlaDeviceCompiler(/*enable_persistence=*/true); + core::ScopedUnref xla_device_compiler_ref(xla_device_compiler); + + NameAttrList fn; + fn.set_name("foo"); + auto args = SampleArgsForAddXY(); + XlaCompiler::Options options = GetDefaultXlaOptions(); + + const XlaCompiler::CompilationResult* compilation_result = nullptr; + xla::LocalExecutable* xla_executable = nullptr; + + TF_EXPECT_OK(xla_device_compiler->CompileIfNeeded( + options, fn, args, XlaCompiler::CompileOptions{}, + DeviceCompileMode::kStrict, profiler_, &compilation_result, + &xla_executable)); + + EXPECT_TRUE(compilation_result != nullptr); + EXPECT_TRUE(xla_executable != nullptr); + + // Check if device_compiler was able to load the executable from the + // persistent cache. + std::vector activity_history = + listener_->GetListenerHistory(); + EXPECT_EQ(activity_history.size(), 1); + EXPECT_EQ(activity_history[0].cluster_name(), fn.name()); + EXPECT_EQ(activity_history[0].compile_count(), 1); + EXPECT_FALSE(activity_history[0].used_persistent_cache()); + + listener_->ClearListenerHistory(); + + // Create another DeviceCompiler object pointing to the same persistent cache + // directory. It should load the executable instead of building it. + auto xla_device_compiler_2 = + CreateXlaDeviceCompiler(/*enable_persistence=*/true); + core::ScopedUnref xla_device_compiler_ref_2(xla_device_compiler_2); + + auto profiler = new DeviceCompilationProfiler(); + core::ScopedUnref profiler_ref(profiler); + + const XlaCompiler::CompilationResult* compilation_result_2 = nullptr; + xla::LocalExecutable* xla_executable_2 = nullptr; + TF_EXPECT_OK(xla_device_compiler_2->CompileIfNeeded( + options, fn, args, XlaCompiler::CompileOptions{}, + DeviceCompileMode::kStrict, profiler, &compilation_result_2, + &xla_executable_2)); + + EXPECT_TRUE(compilation_result_2 != nullptr); + EXPECT_TRUE(xla_executable_2 != nullptr); + + activity_history = listener_->GetListenerHistory(); + EXPECT_EQ(activity_history.size(), 1); + EXPECT_EQ(activity_history[0].cluster_name(), fn.name()); + EXPECT_EQ(activity_history[0].compile_count(), 1); + // Verify that the executable was loaded instead of built. + EXPECT_TRUE(activity_history[0].used_persistent_cache()); +} + +TEST_F(DeviceCompilerTest, CompileFailedToLoadFromPersistentCache) { + auto xla_device_compiler = + CreateXlaDeviceCompiler(/*enable_persistence=*/true); + core::ScopedUnref xla_device_compiler_ref(xla_device_compiler); + + NameAttrList fn; + fn.set_name("foo"); + auto args = SampleArgsForAddXY(); + XlaCompiler::Options options = GetDefaultXlaOptions(); + + const XlaCompiler::CompilationResult* compilation_result = nullptr; + xla::LocalExecutable* xla_executable = nullptr; + + // Persist an executable. + TF_EXPECT_OK(xla_device_compiler->CompileIfNeeded( + options, fn, args, XlaCompiler::CompileOptions{}, + DeviceCompileMode::kStrict, profiler_, &compilation_result, + &xla_executable)); + + // Corrupt the file which contains the serialized executable. + std::vector files; + TF_ASSERT_OK(Env::Default()->GetChildren(testing::TmpDir(), &files)); + std::string const* serialized_executable_filename = nullptr; + for (const auto& file : files) { + if (absl::StartsWith(file, "xla__")) { + serialized_executable_filename = &file; + break; + } + } + EXPECT_TRUE(serialized_executable_filename != nullptr); + std::string serialized_executable_filepath = + io::JoinPath(testing::TmpDir(), *serialized_executable_filename); + std::unique_ptr serialized_executable_file; + TF_ASSERT_OK(Env::Default()->NewWritableFile(serialized_executable_filepath, + &serialized_executable_file)); + TF_ASSERT_OK(serialized_executable_file->Append("Garbage.")); + TF_ASSERT_OK(serialized_executable_file->Close()); + + // Create another DeviceCompiler object pointing to the same persistent cache + // directory. It should error out while loading the executable from the + // corrupt file. + auto xla_device_compiler_2 = + CreateXlaDeviceCompiler(/*enable_persistence=*/true); + core::ScopedUnref xla_device_compiler_ref_2(xla_device_compiler_2); + + const XlaCompiler::CompilationResult* compilation_result_2 = nullptr; + xla::LocalExecutable* xla_executable_2 = nullptr; + + EXPECT_FALSE(xla_device_compiler_2 + ->CompileIfNeeded(options, fn, args, + XlaCompiler::CompileOptions{}, + DeviceCompileMode::kStrict, profiler_, + &compilation_result_2, &xla_executable_2) + .ok()); + + EXPECT_TRUE(compilation_result_2 == nullptr); + EXPECT_TRUE(xla_executable_2 == nullptr); +} + +TEST_F(DeviceCompilerTest, CompileStrictPersistentCacheFailedToPersist) { + auto xla_compiler_client = + std::make_unique(GetLocalClient()); + auto xla_persistor = std::make_unique(); + auto xla_device_compiler = new XlaDeviceCompiler( + std::move(xla_persistor), std::move(xla_compiler_client)); + core::ScopedUnref xla_device_compiler_ref(xla_device_compiler); + + NameAttrList fn; + fn.set_name("foo"); + auto args = SampleArgsForAddXY(); + XlaCompiler::Options options = GetDefaultXlaOptions(); + + const XlaCompiler::CompilationResult* compilation_result = nullptr; + xla::LocalExecutable* xla_executable = nullptr; + + auto persistor = absl::down_cast( + xla_device_compiler->persistor()); + TF_ASSERT_OK_AND_ASSIGN(auto signature, Signature::Build(fn, args)); + EXPECT_CALL(*persistor, + TryToPersistExecutable(Signature::Hash()(signature), + signature.HumanString(), _, _, _, _)) + .WillOnce(Return(absl::FailedPreconditionError("Random error."))); + + EXPECT_THAT(xla_device_compiler->CompileIfNeeded( + options, fn, args, XlaCompiler::CompileOptions{}, + DeviceCompileMode::kStrict, profiler_, &compilation_result, + &xla_executable), + absl_testing::StatusIs(error::FAILED_PRECONDITION, + ::testing::HasSubstr("Random error."))); + + EXPECT_TRUE(compilation_result == nullptr); + EXPECT_TRUE(xla_executable == nullptr); +} + +class DeviceCompilerTestWithDump : public DeviceCompilerTest { + protected: + explicit DeviceCompilerTestWithDump() { + dump_dir_ = tsl::io::JoinPath( + testing::TmpDir(), + absl::StrCat("dump_test_", absl::ToUnixNanos(absl::Now()))); + CHECK_OK(Env::Default()->RecursivelyCreateDir(dump_dir_)); + setenv("TF_GRAPH_TO_HLO_COMPILER_DUMP_DIR", dump_dir_.c_str(), 1); + } + ~DeviceCompilerTestWithDump() override { + unsetenv("TF_GRAPH_TO_HLO_COMPILER_DUMP_DIR"); + } + + std::string dump_dir_; +}; + +TEST_F(DeviceCompilerTestWithDump, CompileStrictDebugInformationDumpWorks) { + // We create a new XlaDeviceCompiler here so that we ensure that the cached + // result is not used and `CompileStrict` is called. + auto xla_device_compiler = + CreateXlaDeviceCompiler(/*enable_persistence=*/false); + ASSERT_THAT(xla_device_compiler, NotNull()); + core::ScopedUnref xla_device_compiler_ref(xla_device_compiler); + + // Now we run the compilation. We only care that `CompileStrict` has been + // actually called, and we don't care about whether it succeeded. + const XlaCompiler::CompilationResult* compilation_result = nullptr; + xla::LocalExecutable* xla_executable = nullptr; + XlaCompiler::Options options = GetDefaultXlaOptions(); + XlaCompiler::CompileOptions compile_options; + NameAttrList fn; + fn.set_name("foo"); + xla_device_compiler + ->CompileIfNeeded(options, fn, SampleArgsForAddXY(), compile_options, + DeviceCompileMode::kStrict, profiler_, + &compilation_result, &xla_executable) + .IgnoreError(); + + // Check the directory structure. + std::vector dump_dir_files; + EXPECT_THAT(Env::Default()->GetChildren(dump_dir_, &dump_dir_files), IsOk()); + EXPECT_THAT(dump_dir_files, SizeIs(1)); + absl::string_view dump_subdir = dump_dir_files[0]; + std::vector dump_subdir_files; + EXPECT_THAT( + Env::Default()->GetChildren(tsl::io::JoinPath(dump_dir_, dump_subdir), + &dump_subdir_files), + IsOk()); + // `options.pb` and one pb for the compile call arguments. + EXPECT_THAT(dump_subdir_files, + UnorderedElementsAre("options.pb", EndsWith(".pb"))); + + // First check the options file. + std::string options_file = + tsl::io::JoinPath(dump_dir_, dump_subdir, "options.pb"); + std::string options_contents; + EXPECT_THAT(ReadFileToString(Env::Default(), options_file, &options_contents), + IsOk()); + TfGraphToHloCompilerOptions options_proto; + EXPECT_THAT(options_proto.ParseFromString(options_contents), IsTrue()); + EXPECT_THAT(absl::StrCat(tsl::DeterministicProtoHash64(options_proto)), + StrEq(dump_subdir)); + EXPECT_THAT(options_proto.device_type(), + StrEq(options.device_type.type_string())); + EXPECT_THAT(options_proto.flib_def(), + EqualsProto(options.flib_def->ToProto())); + EXPECT_THAT(options_proto.graph_def_version(), Eq(options.graph_def_version)); + + // Now check the compile call arguments file. + absl::string_view compile_call_args_file_name = + (dump_subdir_files[0] == "options.pb" ? dump_subdir_files[1] + : dump_subdir_files[0]); + std::string compile_call_args_file = + tsl::io::JoinPath(dump_dir_, dump_subdir, compile_call_args_file_name); + std::string compile_call_args_contents; + EXPECT_THAT(ReadFileToString(Env::Default(), compile_call_args_file, + &compile_call_args_contents), + IsOk()); + TfGraphToHloCompilerCompileCallArgs compile_call_args_proto; + EXPECT_THAT( + compile_call_args_proto.ParseFromString(compile_call_args_contents), + IsTrue()); + EXPECT_THAT(compile_call_args_proto.compile_options(), + StrEq(compile_options.DebugString())); + EXPECT_THAT(compile_call_args_proto.function(), EqualsProto(fn)); + std::vector xla_arguments_from_proto; + absl::c_transform(compile_call_args_proto.xla_args(), + std::back_inserter(xla_arguments_from_proto), + [](const tf2xla::XlaArgumentProto& proto) -> XlaArgument { + auto arg = XlaArgument::FromProto(proto); + CHECK_OK(arg); // Crash OK + return arg.value(); + }); + EXPECT_THAT(xla_arguments_from_proto, ElementsAreArray(SampleArgsForAddXY())); +} + +TEST_F(OpsTestBase, CompileSingleOpSuccess) { + TF_EXPECT_OK(NodeDefBuilder("identity_op", "Identity") + .Input(FakeInput(DT_FLOAT)) + .Attr("T", DT_FLOAT) + .Finalize(node_def())); + TF_EXPECT_OK(InitOp()); + AddInputFromArray(TensorShape({1, 2}), {6.9, 4.2}); + TF_EXPECT_OK(RunOpKernel()); + + auto xla_device_compiler = CreateXlaDeviceCompiler(); + core::ScopedUnref xla_device_compiler_ref(xla_device_compiler); + + auto profiler = new DeviceCompilationProfiler(); + core::ScopedUnref profiler_ref(profiler); + + const XlaCompiler::CompilationResult* compilation_result = nullptr; + xla::LocalExecutable* xla_executable = nullptr; + + XlaOpRegistry::RegisterCompilationKernels(); + auto flib_def = std::make_unique( + OpRegistry::Global(), FunctionDefLibrary()); + + XlaCompiler::Options options; + options.device_type = DeviceType(DEVICE_GPU_XLA_JIT); + options.client = GetLocalClient(); + options.flib_def = flib_def.get(); + + std::vector args(1); + args[0].kind = XlaCompiler::Argument::kConstant; + args[0].type = DT_FLOAT; + args[0].shape = TensorShape({1, 2}); + args[0].constant_value = GetInput(0); + args[0].initialized = true; + + NameAttrList fn; + fn.set_name("foo"); + + TF_EXPECT_OK(xla_device_compiler->CompileSingleOpIfNeeded( + options, args, XlaCompiler::CompileOptions{}, context_.get(), profiler, + &compilation_result, &xla_executable)); + + EXPECT_TRUE(compilation_result != nullptr); + EXPECT_TRUE(xla_executable != nullptr); +} + +TEST_F(DeviceCompilerTest, Finalize) { + XlaCompiler::Options options = GetDefaultXlaOptions(); + + NameAttrList fn; + fn.set_name("foo"); + + const XlaCompiler::CompilationResult* compilation_result = nullptr; + xla::LocalExecutable* xla_executable = nullptr; + TF_EXPECT_OK(xla_device_compiler_->CompileIfNeeded( + options, fn, SampleArgsForAddXY(), XlaCompiler::CompileOptions{}, + DeviceCompileMode::kStrict, profiler_, &compilation_result, + &xla_executable)); + + ASSERT_TRUE(compilation_result != nullptr); + + // Cast to `ResourceBase` to verify that the `Finalize` implementation + // overrides the base class's. + static_cast(xla_device_compiler_)->Finalize(); + + TF_EXPECT_OK(xla_device_compiler_->CompileIfNeeded( + options, fn, SampleArgsForAddXY(), XlaCompiler::CompileOptions{}, + DeviceCompileMode::kStrict, profiler_, &compilation_result, + &xla_executable)); + + ASSERT_TRUE(compilation_result != nullptr); +} + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_context_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_context_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..662b5e9560be9c5cf81b2274139670d2884bfb5a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_context_test.cc @@ -0,0 +1,96 @@ +/* Copyright 2023 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/compiler/jit/flags.h" +#include "tensorflow/compiler/tf2xla/xla_op_registry.h" +#include "xla/tsl/lib/core/status_test_util.h" +#include "tensorflow/core/framework/tensor_testutil.h" + +namespace tensorflow { +namespace { + +static bool Initialized = [] { + auto& rollout_config = GetXlaOpsCommonFlags()->tf_xla_use_device_api; + rollout_config.enabled_for_xla_launch_ = true; + rollout_config.enabled_for_compile_on_demand_ = true; + + tensorflow::GetXlaDeviceFlags()->tf_xla_enable_xla_devices = true; + return true; +}(); + +class DeviceContextTest : public ::testing::Test { + public: + void SetDevice(const std::string& device_type) { + auto& rollout_config = GetXlaOpsCommonFlags()->tf_xla_use_device_api; + rollout_config.AllowForDeviceInXlaLaunch(DeviceType(device_type)); + rollout_config.AllowForDeviceInXlaCompileOnDemand(DeviceType(device_type)); + + auto device_factory = DeviceFactory::GetFactory(device_type); + SessionOptions options; + std::vector> devices; + absl::Status s = device_factory->CreateDevices( + options, "/job:worker/replica:0/task:0", &devices); + device_ = std::move(devices[0]); + + tensorflow::AllocatorAttributes host_alloc_attr; + host_alloc_attr.set_on_host(true); + host_allocator_ = device_->GetAllocator(host_alloc_attr); + + tensorflow::AllocatorAttributes device_alloc_attr; + device_alloc_attr.set_on_host(false); + device_allocator_ = device_->GetAllocator(device_alloc_attr); + + tensorflow::DeviceContext* device_context; + auto status = device_->TryGetDeviceContext(&device_context); + TF_EXPECT_OK(status); + device_context_.reset(device_context); + } + + std::unique_ptr device_; + tensorflow::core::RefCountPtr device_context_; + tensorflow::Allocator* host_allocator_; + tensorflow::Allocator* device_allocator_; +}; + +#if GOOGLE_CUDA || TENSORFLOW_USE_ROCM +TEST_F(DeviceContextTest, TestXlaGpuRoundTripTransferWithDeviceApi) { + SetDevice(DEVICE_XLA_GPU); + tensorflow::Tensor origin_cpu_tensor(host_allocator_, tensorflow::DT_FLOAT, + tensorflow::TensorShape({2, 2})); + tensorflow::test::FillValues(&origin_cpu_tensor, {1.2, 2.3, 3.4, 4.5}); + tensorflow::Tensor device_tensor(device_allocator_, tensorflow::DT_FLOAT, + tensorflow::TensorShape({2, 2})); + tensorflow::Tensor dest_cpu_tensor(host_allocator_, tensorflow::DT_FLOAT, + tensorflow::TensorShape({2, 2})); + + TF_ASSERT_OK(device_context_->CopyCPUTensorToDeviceSync( + &origin_cpu_tensor, device_.get(), &device_tensor)); + TF_ASSERT_OK(device_context_->CopyDeviceTensorToCPUSync( + &device_tensor, "", device_.get(), &dest_cpu_tensor)); + LOG(INFO) << "H2D - D2H roundtrip completes. tensor: " + << dest_cpu_tensor.DebugString(4); + + tensorflow::test::ExpectClose(origin_cpu_tensor, dest_cpu_tensor); +} +#endif + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_executable_persistor.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_executable_persistor.cc new file mode 100644 index 0000000000000000000000000000000000000000..87ec3065d740128aff22e9da13a548a76130244a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_executable_persistor.cc @@ -0,0 +1,37 @@ +/* Copyright 2024 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/compiler/jit/device_executable_persistor.h" + +#include + +#include "absl/strings/str_cat.h" + +namespace tensorflow { + +std::string XlaSerializedCacheKeyToFileName(const XlaSerializedCacheKey& key) { + static constexpr char kXlaSerializedCacheKeySeparator[] = "__"; + return absl::StrCat( + key.prefix(), key.prefix().empty() ? "" : kXlaSerializedCacheKeySeparator, + key.signature_fingerprint(), kXlaSerializedCacheKeySeparator, + key.cluster_fingerprint(), kXlaSerializedCacheKeySeparator, + key.device_type(), + key.compiled_using_pjrt() + ? absl::StrCat(kXlaSerializedCacheKeySeparator, "pjrt") + : "", + ".pb"); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_executable_persistor.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_executable_persistor.h new file mode 100644 index 0000000000000000000000000000000000000000..729eedc6ac62ef5e519f21459eba578abc9ee0ea --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_executable_persistor.h @@ -0,0 +1,404 @@ +/* Copyright 2022 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_DEVICE_EXECUTABLE_PERSISTOR_H_ +#define TENSORFLOW_COMPILER_JIT_DEVICE_EXECUTABLE_PERSISTOR_H_ + +#include +#include +#include + +#include "absl/log/log.h" +#include "absl/status/status.h" +#include "absl/strings/str_cat.h" +#include "tensorflow/compiler/jit/device_compiler_client.h" +#include "tensorflow/compiler/jit/xla_compilation_cache.pb.h" +#include "tensorflow/compiler/jit/xla_device_compiler_client.h" +#include "tensorflow/compiler/tf2xla/xla_compiler.h" +#include "xla/pjrt/pjrt_client.h" +#include "xla/service/hlo.pb.h" +#include "xla/util.h" +#include "tensorflow/core/framework/device.h" +#include "tensorflow/core/lib/strings/proto_serialization.h" +#include "tensorflow/core/platform/path.h" +#include "tensorflow/core/platform/status.h" +#include "tensorflow/core/platform/statusor.h" +#include "tensorflow/core/platform/types.h" +#include "tsl/platform/errors.h" +#include "tsl/platform/statusor.h" + +namespace tensorflow { + +// Returns the persisted compilation cache file name for the given key. +std::string XlaSerializedCacheKeyToFileName(const XlaSerializedCacheKey& key); + +// Offers a way to persist and/or load compiled `ExecutableType`s along with the +// corresponding HLO (`CompilationResult`) to/from `persistent_cache_directory` +// (if one was provided during construction) on disk using `ClientType`. +template +class DeviceExecutablePersistor { + public: + // Configuration for setting up persistence (directory, filename prefix, etc). + struct Config { + Config() = default; + explicit Config(absl::string_view persistent_cache_directory, + bool disable_strict_signature_checks, + absl::string_view persistence_prefix, + bool persistent_cache_directory_read_only) + : persistent_cache_directory(persistent_cache_directory), + disable_strict_signature_checks(disable_strict_signature_checks), + persistence_prefix(persistence_prefix), + persistent_cache_directory_read_only( + persistent_cache_directory_read_only) {} + + explicit Config(absl::string_view persistent_cache_directory, + bool disable_strict_signature_checks, + absl::string_view persistence_prefix) + : persistent_cache_directory(persistent_cache_directory), + disable_strict_signature_checks(disable_strict_signature_checks), + persistence_prefix(persistence_prefix) {} + + // If non-empty, JIT-compiled executables are saved to and loaded from the + // specified file system directory path. + std::string persistent_cache_directory; + + // Disable strict signature checks for entries loaded into the cache from + // external sources. + bool disable_strict_signature_checks = false; + + // The cache persistence prefix to use if serializing/deserialzing entries. + std::string persistence_prefix; + + // Cache is read-only if set to true. + bool persistent_cache_directory_read_only = false; + }; + + DeviceExecutablePersistor(const Config& config, + const DeviceType& device_type); + virtual ~DeviceExecutablePersistor() = default; + + // Returns std::nullopt if persistence is not enabled (i.e. + // `persistent_cache_directory_` is empty) or if the serialized entry is not + // found on disk. Otherwise, loads and returns the serialized executable + // (or returns a status). + // TODO(b/255826209): Take in Signature instead of hash and string once cache + // is refactored. + std::optional>> TryToLoadExecutable( + uint64_t signature_hash, const std::string& signature_str, + const XlaCompiler::Options& options, + const XlaCompiler::CompilationResult& compilation_result, + DeviceCompilerClient* client) const; + + // Tries to serialize an already built `executable` and persist it on disk. If + // unable to do so, tries to build a serialized executable using the AOT + // pipeline and persists that to disk. + // TODO(b/255826209): Take in Signature instead hash and string once cache + // is refactored. + virtual absl::Status TryToPersistExecutable( + uint64_t signature_hash, const std::string& signature_str, + const XlaCompiler::Options& options, + const XlaCompiler::CompilationResult& compilation_result, + const ExecutableType& executable, + DeviceCompilerClient* client) const; + + const DeviceType& device_type() const { return device_type_; } + const std::string& persistence_prefix() const { return persistence_prefix_; } + const std::string& persistent_cache_directory() const { + return persistent_cache_directory_; + } + + private: + // Returns a cache key proto that identifies an entry in the compilation + // cache. + XlaSerializedCacheKey BuildSerializedCacheKey( + uint64_t signature_hash, const xla::HloModuleProto& hlo_module) const; + + XlaSerializedCacheKey BuildSerializedCacheKey( + uint64_t signature_hash, const xla::HloModuleProto& hlo_module, + bool compiled_using_pjrt) const; + + // Serializes the signature and its corresponding entry to a proto message. + absl::StatusOr SerializeEntry( + uint64_t signature_hash, const XlaCompiler::Options& options, + const XlaCompiler::CompilationResult& compilation_result, + const ExecutableType& executable, + DeviceCompilerClient* compiler_client) const; + + // Saves the cache entry in the file directory supplied during the + // construction of this class. Overwrites existing entries. + absl::Status SaveSerializedEntry(const XlaSerializedCacheEntry& entry) const; + + // Tries to read a cache entry given a `key` by searching the file directory + // supplied during the construction of this class. Returns std::nullopt if no + // cache entry is found. + absl::StatusOr> + TryToReadSerializedEntry(const XlaSerializedCacheKey& key) const; + + // Checks if the loaded `entry` matches the expected `key` and `hlo_module`. + absl::Status VerifyLoadedCacheEntry( + const XlaSerializedCacheKey& key, const xla::HloModuleProto& hlo_module, + const XlaSerializedCacheEntry& entry) const; + + std::string GetFilePath(const XlaSerializedCacheKey& key) const; + + const DeviceType device_type_; + const bool disable_strict_signature_checks_; + const std::string persistence_prefix_; + + // If non-empty, JIT-compiled executables are saved to and loaded from the + // specified file system directory path. + const std::string persistent_cache_directory_; + + // Cache is read-only if set to true. + const bool persistent_cache_directory_read_only_; + + DeviceExecutablePersistor(const DeviceExecutablePersistor&) = delete; + void operator=(const DeviceExecutablePersistor&) = delete; +}; + +template +DeviceExecutablePersistor:: + DeviceExecutablePersistor(const Config& config, + const DeviceType& device_type) + : device_type_(device_type), + disable_strict_signature_checks_(config.disable_strict_signature_checks), + persistence_prefix_(config.persistence_prefix), + persistent_cache_directory_(config.persistent_cache_directory), + persistent_cache_directory_read_only_( + config.persistent_cache_directory_read_only) {} + +template +std::string DeviceExecutablePersistor::GetFilePath( + const XlaSerializedCacheKey& key) const { + const std::string file_name = XlaSerializedCacheKeyToFileName(key); + return io::JoinPath(persistent_cache_directory_, file_name); +} + +template +XlaSerializedCacheKey +DeviceExecutablePersistor::BuildSerializedCacheKey( + uint64_t signature_hash, const xla::HloModuleProto& hlo_module, + bool compiled_using_pjrt) const { + XlaSerializedCacheKey key; + key.set_signature_fingerprint(signature_hash); + key.set_cluster_fingerprint(DeterministicProtoHash64(hlo_module)); + key.set_device_type(device_type().type_string()); + key.set_prefix(persistence_prefix()); + key.set_compiled_using_pjrt(compiled_using_pjrt); + return key; +} + +template +XlaSerializedCacheKey +DeviceExecutablePersistor::BuildSerializedCacheKey( + uint64_t signature_hash, const xla::HloModuleProto& hlo_module) const { + return BuildSerializedCacheKey(signature_hash, hlo_module, false); +} + +// This template specialization sets compiled_using_prjt to true in the cache +// key when the template arguments are PjRtLoadedExecutable and PjRtClient. +template <> +inline XlaSerializedCacheKey +DeviceExecutablePersistor:: + BuildSerializedCacheKey(uint64_t signature_hash, + const xla::HloModuleProto& hlo_module) const { + return BuildSerializedCacheKey(signature_hash, hlo_module, true); +} + +template +absl::StatusOr> +DeviceExecutablePersistor::TryToReadSerializedEntry( + const XlaSerializedCacheKey& key) const { + Env* env = Env::Default(); + const std::string file_path = GetFilePath(key); + if (!env->FileExists(file_path).ok()) { + return absl::StatusOr>(std::nullopt); + } + + XlaSerializedCacheEntry entry; + TF_RETURN_IF_ERROR(ReadTextOrBinaryProto(env, file_path, &entry)); + return std::optional(entry); +} + +template +absl::Status +DeviceExecutablePersistor::VerifyLoadedCacheEntry( + const XlaSerializedCacheKey& key, const xla::HloModuleProto& hlo_module, + const XlaSerializedCacheEntry& entry) const { + XLA_SCOPED_LOGGING_TIMER(absl::StrCat("Verifying loaded cache entry: ", + hlo_module.entry_computation_name())); + + if (!AreSerializedProtosEqual(key, entry.key())) { + VLOG(2) << "Serialized cache key does not match:\n" + << "got:\n" + << entry.key().DebugString() << "\nexpected:\n" + << key.DebugString() << "\n"; + return absl::InvalidArgumentError("Serialized cache key does not match."); + } + + // Perform a stricter (slower) check of the snapshot to verify that they + // match exactly. + if (!disable_strict_signature_checks_) { + if (!AreSerializedProtosEqual(hlo_module, entry.hlo_module())) { + VLOG(2) << "HLOs do not match:\n" + << "got:\n" + << hlo_module.DebugString() << "\nexpected:\n" + << entry.hlo_module().DebugString() << "\n"; + return absl::InvalidArgumentError("Serialized HLO does not match."); + } + } + + if (entry.executable().empty()) { + return absl::InvalidArgumentError("No binary found in serialized entry."); + } + return absl::OkStatus(); +} + +template +absl::Status +DeviceExecutablePersistor::SaveSerializedEntry( + const XlaSerializedCacheEntry& entry) const { + Env* env = Env::Default(); + TF_RETURN_IF_ERROR(env->RecursivelyCreateDir(persistent_cache_directory_)); + + // The cache on the filesystem can be read while we're writing out the proto. + // To prevent reads of partially-written files, we write the proto to a temp + // file, then move it into place once we're done writing. And we warn the + // user if these moves are not known to be atomic. + bool has_atomic_move = false; + env->HasAtomicMove(persistent_cache_directory_, &has_atomic_move) + .IgnoreError(); + if (!has_atomic_move) { + LOG_EVERY_POW_2(WARNING) + << "Filesystem for XLA persistent cache at " + << persistent_cache_directory_ + << " does not support atomic moves. Therefore the persistent cache is " + "racy if you have multiple XLA compilations occurring " + "simultaneously! You have been warned. :)"; + } + + // Write to temp location, then when that completes, atomically move into the + // final location. + std::string temp_path = + io::JoinPath(persistent_cache_directory_, + XlaSerializedCacheKeyToFileName(entry.key())); + if (!env->CreateUniqueFileName(&temp_path, ".tmp")) { + return absl::UnavailableError(absl::StrCat( + "Could not create a unique file inside ", persistent_cache_directory_)); + } + TF_RETURN_IF_ERROR(WriteBinaryProto(env, temp_path, entry)); + return env->RenameFile(temp_path, GetFilePath(entry.key())); +} + +template +absl::StatusOr +DeviceExecutablePersistor::SerializeEntry( + uint64_t signature_hash, const XlaCompiler::Options& options, + const XlaCompiler::CompilationResult& compilation_result, + const ExecutableType& executable, + DeviceCompilerClient* compiler_client) const { + XlaSerializedCacheEntry serialized_entry; + const xla::HloModuleProto& hlo_module = + compilation_result.computation->proto(); + *serialized_entry.mutable_key() = + BuildSerializedCacheKey(signature_hash, hlo_module); + *serialized_entry.mutable_hlo_module() = hlo_module; + + // XLA compiler supports exporting executables as an AOT compilation result + // to avoid running potentially expensive compilation pipeline twice. + // Check if XLA compiler can export available executable. + if (auto serialized_executable = + compiler_client->SerializeExecutable(executable); + serialized_executable.ok()) { + serialized_entry.set_executable(std::move(*serialized_executable)); + return serialized_entry; + } else if (serialized_executable.status().code() == error::UNIMPLEMENTED) { + VLOG(1) << "Executable export is not implemented"; + } else { + return serialized_executable.status(); + } + + TF_ASSIGN_OR_RETURN( + auto serialized_executable, + compiler_client->BuildSerializedExecutable(options, compilation_result)); + serialized_entry.set_executable(std::move(serialized_executable)); + return serialized_entry; +} + +template +std::optional>> +DeviceExecutablePersistor::TryToLoadExecutable( + uint64_t signature_hash, const std::string& signature_str, + const XlaCompiler::Options& options, + const XlaCompiler::CompilationResult& compilation_result, + DeviceCompilerClient* compiler_client) const { + if (persistent_cache_directory_.empty()) { + return std::nullopt; + } + + const xla::HloModuleProto& hlo_module = + compilation_result.computation->proto(); + + XlaSerializedCacheKey cache_key = + BuildSerializedCacheKey(signature_hash, hlo_module); + + std::optional serialized_entry; + { + XLA_SCOPED_LOGGING_TIMER( + absl::StrCat("Try loading serialized cache entry:", signature_str)); + TF_ASSIGN_OR_RETURN(serialized_entry, TryToReadSerializedEntry(cache_key)); + } + + if (!serialized_entry.has_value()) { + return std::nullopt; + } + + TF_RETURN_IF_ERROR( + VerifyLoadedCacheEntry(cache_key, hlo_module, *serialized_entry)); + + VLOG(1) << "Loading cached entry for: " << signature_str; + return compiler_client->LoadExecutable(options, compilation_result, + serialized_entry->executable()); +} + +template +absl::Status +DeviceExecutablePersistor::TryToPersistExecutable( + uint64_t signature_hash, const std::string& signature_str, + const XlaCompiler::Options& options, + const XlaCompiler::CompilationResult& compilation_result, + const ExecutableType& executable, + DeviceCompilerClient* client) const { + if (persistent_cache_directory_.empty() || + persistent_cache_directory_read_only_) { + VLOG(1) << "Not persisting executable. No `persistent_cache_directory` " + "provided or cache is read-only."; + return absl::OkStatus(); + } + + XLA_SCOPED_LOGGING_TIMER( + absl::StrCat("Serializing and saving cache entry: ", signature_str)); + TF_ASSIGN_OR_RETURN(XlaSerializedCacheEntry serialized_entry, + SerializeEntry(signature_hash, options, + compilation_result, executable, client)); + TF_RETURN_IF_ERROR(SaveSerializedEntry(std::move(serialized_entry))); + VLOG(2) << "XlaSerializedCacheEntry saved for signature: [" << signature_str + << "] with signature hash: " << signature_hash; + return absl::OkStatus(); +} + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_DEVICE_EXECUTABLE_PERSISTOR_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_executable_persistor_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_executable_persistor_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..1646377f481318691903ada9fd067e48547560d6 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_executable_persistor_test.cc @@ -0,0 +1,637 @@ +/* Copyright 2022 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/compiler/jit/device_executable_persistor.h" + +#include + +#include +#include +#include +#include + +#include +#include +#include "tensorflow/cc/framework/scope.h" +#include "tensorflow/cc/ops/function_ops.h" +#include "tensorflow/cc/ops/math_ops.h" +#include "tensorflow/compiler/jit/device_compiler_client.h" +#include "tensorflow/compiler/jit/pjrt_device_compiler_client.h" +#include "tensorflow/compiler/jit/xla_compilation_cache.pb.h" +#include "tensorflow/compiler/jit/xla_device_compiler_client.h" +#include "xla/client/client_library.h" +#include "xla/client/executable_build_options.h" +#include "xla/client/local_client.h" +#include "xla/pjrt/pjrt_client.h" +#include "xla/pjrt/plugin/xla_cpu/cpu_client_options.h" +#include "xla/pjrt/plugin/xla_cpu/xla_cpu_pjrt_client.h" +#include "tensorflow/core/lib/core/status_test_util.h" +#include "tensorflow/core/platform/errors.h" +#include "tensorflow/core/platform/status_matchers.h" +#include "tensorflow/core/platform/statusor.h" +#include "tensorflow/core/tfrt/common/create_pjrt_client_util.h" +#include "tensorflow/core/tfrt/common/pjrt_util.h" + +namespace tensorflow { +namespace { + +using ::testing::_; +using ::testing::ByMove; +using ::testing::Return; +using XlaDeviceExecutablePersistor = + DeviceExecutablePersistor; +using PjRtDeviceExecutablePersistor = + DeviceExecutablePersistor; + +class DeviceExecutionPersistorTest : public ::testing::Test { + protected: + void SetUp() override { + xla_compiler_client_ = std::make_unique( + xla::ClientLibrary::LocalClientOrDie()); + TF_ASSERT_OK(CreatePjRtCompilerClient()); + + XlaOpRegistry::RegisterCompilationKernels(); + + flib_def_ = std::make_unique( + OpRegistry::Global(), FunctionDefLibrary()); + + cache_dir_ = testing::TmpDir(); + TF_ASSERT_OK_AND_ASSIGN(compilation_result_add_, + BuildSampleCompilationResult()); + } + + absl::StatusOr> + BuildSampleExecutable() { + return xla_compiler_client_->BuildExecutable(DefaultXlaOptions(), + compilation_result_add_); + } + + absl::StatusOr> + BuildSamplePjRtExecutable() { + return pjrt_compiler_client_->BuildExecutable(DefaultPjRtOptions(), + compilation_result_add_); + } + + absl::StatusOr BuildSampleCompilationResult( + bool mul = false) { + std::unique_ptr graph(new Graph(OpRegistry::Global())); + Scope scope = Scope::NewRootScope().ExitOnError(); + auto a = ops::_Arg(scope.WithOpName("A"), DT_INT32, 0); + auto b = ops::_Arg(scope.WithOpName("B"), DT_INT32, 1); + if (mul) { + auto c = ops::Mul(scope.WithOpName("C"), a, b); + auto d = ops::_Retval(scope.WithOpName("D"), c, 0); + TF_RETURN_IF_ERROR(scope.ToGraph(graph.get())); + } else { + auto c = ops::Add(scope.WithOpName("C"), a, b); + auto d = ops::_Retval(scope.WithOpName("D"), c, 0); + TF_RETURN_IF_ERROR(scope.ToGraph(graph.get())); + } + + // Builds a description of the arguments. + std::vector args(2); + args[0].kind = XlaCompiler::Argument::kParameter; + args[0].type = DT_INT32; + args[0].shape = TensorShape({2}); + args[1].kind = XlaCompiler::Argument::kParameter; + args[1].type = DT_INT32; + args[1].shape = TensorShape({2}); + + // Compiles the graph. + XlaCompiler compiler(DefaultXlaOptions()); + + XlaCompiler::CompilationResult compilation_result; + TF_RETURN_IF_ERROR(compiler.CompileGraph(XlaCompiler::CompileOptions(), + "graph", std::move(graph), args, + &compilation_result)); + return compilation_result; + } + + XlaCompiler::Options DefaultXlaOptions() { + XlaCompiler::Options options; + options.device_type = DeviceType(DEVICE_CPU_XLA_JIT); + options.client = xla_compiler_client_->client(); + options.flib_def = flib_def_.get(); + return options; + } + + XlaCompiler::Options DefaultPjRtOptions() { + XlaCompiler::Options options; + options.device_type = DeviceType(DEVICE_CPU_XLA_JIT); + options.client = nullptr; + options.flib_def = flib_def_.get(); + return options; + } + + absl::Status CreatePjRtCompilerClient() { + // Create PjRtClient manually while GetOrCreatePjRtClient() is WIP. + xla::CpuClientOptions options; + options.asynchronous = true; + options.cpu_device_count = 1; + TF_RETURN_IF_ERROR(SetPjRtClientInTFGlobalResourceManager( + DEVICE_CPU_XLA_JIT, xla::GetXlaPjrtCpuClient(options).value())); + TF_ASSIGN_OR_RETURN(auto pjrt_client, + GetOrCreatePjRtClient(DeviceType(DEVICE_CPU_XLA_JIT))); + pjrt_compiler_client_ = + std::make_unique(pjrt_client); + return absl::OkStatus(); + } + + std::unique_ptr flib_def_; + std::unique_ptr xla_compiler_client_; + std::unique_ptr pjrt_compiler_client_; + XlaCompiler::CompilationResult compilation_result_add_; + std::string serialized_xla_executable_ = "serialized_xla_executable"; + std::string serialized_pjrt_executable_ = "serialized_pjrt_executable"; + std::string cache_dir_; +}; + +// Using a mock to make testing different branches and triggering errors easier. +// Currently the `XlaDeviceCompilerClient`'s load/serialize functions don't work +// with the current test setup. +// TODO(b/255826209): Look into using a real object for most tests. +class MockXlaCompilerClient : public XlaDeviceCompilerClient { + public: + MockXlaCompilerClient() : XlaDeviceCompilerClient(nullptr) {} + MOCK_METHOD(absl::StatusOr, SerializeExecutable, + (const xla::LocalExecutable& executable), (override)); + MOCK_METHOD(absl::StatusOr, BuildSerializedExecutable, + (const XlaCompiler::Options& options, + const XlaCompiler::CompilationResult& result), + (override)); + MOCK_METHOD(absl::StatusOr>, + LoadExecutable, + (const XlaCompiler::Options& options, + const XlaCompiler::CompilationResult& result, + const std::string& serialized_executable), + (override)); +}; + +class MockPjRtCompilerClient : public PjRtDeviceCompilerClient { + public: + MockPjRtCompilerClient() : PjRtDeviceCompilerClient(nullptr) {} + MOCK_METHOD(absl::StatusOr, SerializeExecutable, + (const xla::PjRtLoadedExecutable& executable), (override)); + MOCK_METHOD(absl::StatusOr, BuildSerializedExecutable, + (const XlaCompiler::Options& options, + const XlaCompiler::CompilationResult& result), + (override)); + MOCK_METHOD(absl::StatusOr>, + LoadExecutable, + (const XlaCompiler::Options& options, + const XlaCompiler::CompilationResult& result, + const std::string& serialized_executable), + (override)); +}; + +std::string GetFilePath(XlaSerializedCacheKey key, + const std::string& persistent_cache_dir) { + static constexpr char kXlaSerializedCacheKeySeparator[] = "__"; + + std::string file_name = absl::StrCat( + key.prefix(), key.prefix().empty() ? "" : kXlaSerializedCacheKeySeparator, + key.signature_fingerprint(), kXlaSerializedCacheKeySeparator, + key.cluster_fingerprint(), kXlaSerializedCacheKeySeparator, + key.device_type(), + key.compiled_using_pjrt() + ? absl::StrCat(kXlaSerializedCacheKeySeparator, "pjrt") + : "", + ".pb"); + + return io::JoinPath(persistent_cache_dir, file_name); +} + +absl::StatusOr ReadCacheEntryFromFile( + XlaSerializedCacheKey key, const std::string& persistent_cache_dir) { + std::string file_path = GetFilePath(key, persistent_cache_dir); + XlaSerializedCacheEntry entry; + TF_RETURN_IF_ERROR(ReadTextOrBinaryProto(Env::Default(), file_path, &entry)); + return entry; +} + +XlaSerializedCacheKey CreateCacheKey( + uint64_t signature_hash, + const XlaCompiler::CompilationResult& compilation_result, + const DeviceType& device_type, const std::string& persistence_prefix, + bool compiled_using_pjrt = false) { + XlaSerializedCacheKey key; + key.set_signature_fingerprint(signature_hash); + key.set_cluster_fingerprint( + DeterministicProtoHash64(compilation_result.computation->proto())); + key.set_device_type(device_type.type_string()); + key.set_prefix(persistence_prefix); + key.set_compiled_using_pjrt(compiled_using_pjrt); + return key; +} + +TEST_F(DeviceExecutionPersistorTest, PersistCacheDirNotSet) { + XlaDeviceExecutablePersistor::Config config( + /*persistent_cache_directory=*/"", + /*disable_strict_signature_checks=*/false, + /*persistence_prefix=*/"xla"); + XlaDeviceExecutablePersistor persistor(config, + DefaultXlaOptions().device_type); + + MockXlaCompilerClient mock_client; + TF_ASSERT_OK_AND_ASSIGN(auto executable, BuildSampleExecutable()); + TF_EXPECT_OK(persistor.TryToPersistExecutable( + /*signature_hash=*/123, "signature_string", DefaultXlaOptions(), + compilation_result_add_, *executable, &mock_client)); + + auto key = + CreateCacheKey(/*signature_hash=*/123, compilation_result_add_, + persistor.device_type(), persistor.persistence_prefix()); + auto entry = ReadCacheEntryFromFile(key, ""); + EXPECT_FALSE(entry.ok()); +} + +TEST_F(DeviceExecutionPersistorTest, PersistCacheDirReadOnly) { + XlaDeviceExecutablePersistor::Config config( + /*persistent_cache_directory=*/"cache_dir_", + /*disable_strict_signature_checks=*/false, + /*persistence_prefix=*/"xla", + /*persistent_cache_directory_read_only=*/true); + XlaDeviceExecutablePersistor persistor(config, + DefaultXlaOptions().device_type); + + MockXlaCompilerClient mock_client; + TF_ASSERT_OK_AND_ASSIGN(auto executable, BuildSampleExecutable()); + TF_EXPECT_OK(persistor.TryToPersistExecutable( + /*signature_hash=*/123, "signature_string", DefaultXlaOptions(), + compilation_result_add_, *executable, &mock_client)); + + auto key = + CreateCacheKey(/*signature_hash=*/123, compilation_result_add_, + persistor.device_type(), persistor.persistence_prefix()); + auto entry = ReadCacheEntryFromFile(key, ""); + EXPECT_FALSE(entry.ok()); +} + +TEST_F(DeviceExecutionPersistorTest, PersistSerializeAlreadyBuiltExecutable) { + XlaDeviceExecutablePersistor::Config config( + /*persistent_cache_directory=*/cache_dir_, + /*disable_strict_signature_checks=*/false, + /*persistence_prefix=*/"xla"); + XlaDeviceExecutablePersistor persistor(config, + DefaultXlaOptions().device_type); + + MockXlaCompilerClient mock_client; + EXPECT_CALL(mock_client, SerializeExecutable(_)) + .WillOnce( + Return(absl::StatusOr(serialized_xla_executable_))); + + TF_ASSERT_OK_AND_ASSIGN(auto executable, BuildSampleExecutable()); + TF_EXPECT_OK(persistor.TryToPersistExecutable( + /*signature_hash=*/123, "signature_string", DefaultXlaOptions(), + compilation_result_add_, *executable, &mock_client)); + + auto key = + CreateCacheKey(/*signature_hash=*/123, compilation_result_add_, + persistor.device_type(), persistor.persistence_prefix()); + TF_ASSERT_OK_AND_ASSIGN(auto entry, ReadCacheEntryFromFile(key, cache_dir_)); + + EXPECT_EQ(entry.executable(), serialized_xla_executable_); +} + +TEST_F(DeviceExecutionPersistorTest, PersistBuildSerializedExecutable) { + XlaDeviceExecutablePersistor::Config config( + /*persistent_cache_directory=*/cache_dir_, + /*disable_strict_signature_checks=*/false, + /*persistence_prefix=*/"xla"); + XlaDeviceExecutablePersistor persistor(config, + DefaultXlaOptions().device_type); + + MockXlaCompilerClient mock_client; + EXPECT_CALL(mock_client, SerializeExecutable(_)) + .WillOnce(Return(absl::UnimplementedError("Unimplemented."))); + EXPECT_CALL(mock_client, BuildSerializedExecutable(_, _)) + .WillOnce(Return(serialized_xla_executable_)); + + TF_ASSERT_OK_AND_ASSIGN(auto executable, BuildSampleExecutable()); + TF_EXPECT_OK(persistor.TryToPersistExecutable( + /*signature_hash=*/123, "signature_string", DefaultXlaOptions(), + compilation_result_add_, *executable, &mock_client)); + + auto key = + CreateCacheKey(/*signature_hash=*/123, compilation_result_add_, + persistor.device_type(), persistor.persistence_prefix()); + TF_ASSERT_OK_AND_ASSIGN(auto entry, ReadCacheEntryFromFile(key, cache_dir_)); + + EXPECT_EQ(entry.executable(), serialized_xla_executable_); +} + +TEST_F(DeviceExecutionPersistorTest, PersistSerializeExecutableError) { + XlaDeviceExecutablePersistor::Config config( + /*persistent_cache_directory=*/cache_dir_, + /*disable_strict_signature_checks=*/false, + /*persistence_prefix=*/"xla"); + XlaDeviceExecutablePersistor persistor(config, + DefaultXlaOptions().device_type); + + MockXlaCompilerClient mock_client; + EXPECT_CALL(mock_client, SerializeExecutable(_)) + .WillOnce(Return(absl::InvalidArgumentError("InvalidArgument."))); + + TF_ASSERT_OK_AND_ASSIGN(auto executable, BuildSampleExecutable()); + EXPECT_THAT( + persistor.TryToPersistExecutable( + /*signature_hash=*/123, "signature_string", DefaultXlaOptions(), + compilation_result_add_, *executable, &mock_client), + absl_testing::StatusIs(error::INVALID_ARGUMENT)); +} + +TEST_F(DeviceExecutionPersistorTest, PersistExecutableEmpty) { + XlaDeviceExecutablePersistor::Config config( + /*persistent_cache_directory=*/cache_dir_, + /*disable_strict_signature_checks=*/false, + /*persistence_prefix=*/"xla"); + XlaDeviceExecutablePersistor persistor(config, + DefaultXlaOptions().device_type); + + MockXlaCompilerClient mock_client; + xla::LocalExecutable empty_executable( + nullptr, nullptr, + GetExecutableBuildOptions(DefaultXlaOptions(), compilation_result_add_, + 0)); + EXPECT_CALL(mock_client, SerializeExecutable(_)) + .WillOnce(Return(absl::FailedPreconditionError("Failed precondition."))); + + EXPECT_THAT( + persistor.TryToPersistExecutable( + /*signature_hash=*/123, "signature_string", DefaultXlaOptions(), + compilation_result_add_, empty_executable, &mock_client), + absl_testing::StatusIs(error::FAILED_PRECONDITION)); +} + +TEST_F(DeviceExecutionPersistorTest, LoadCacheDirNotSet) { + XlaDeviceExecutablePersistor::Config config( + /*persistent_cache_directory=*/"", + /*disable_strict_signature_checks=*/false, + /*persistence_prefix=*/"xla"); + XlaDeviceExecutablePersistor persistor(config, + DefaultXlaOptions().device_type); + + MockXlaCompilerClient mock_client; + auto executable = persistor.TryToLoadExecutable( + 123, "signature_string", DefaultXlaOptions(), compilation_result_add_, + &mock_client); + EXPECT_FALSE(executable.has_value()); +} + +TEST_F(DeviceExecutionPersistorTest, LoadSuccess) { + XlaDeviceExecutablePersistor::Config config( + /*persistent_cache_directory=*/cache_dir_, + /*disable_strict_signature_checks=*/false, + /*persistence_prefix=*/"xla"); + XlaDeviceExecutablePersistor persistor(config, + DefaultXlaOptions().device_type); + + MockXlaCompilerClient mock_client; + TF_ASSERT_OK_AND_ASSIGN(auto executable, BuildSampleExecutable()); + EXPECT_CALL(mock_client, LoadExecutable(_, _, serialized_xla_executable_)) + .WillOnce(Return(ByMove(std::move(executable)))); + + auto loaded_executable = persistor.TryToLoadExecutable( + /*signature_hash=*/123, "signature_string", DefaultXlaOptions(), + compilation_result_add_, &mock_client); + + EXPECT_TRUE(loaded_executable.has_value()); + EXPECT_TRUE(loaded_executable.value().ok()); + EXPECT_TRUE((*loaded_executable.value())->executable() != nullptr); +} + +TEST_F(DeviceExecutionPersistorTest, LoadFileDoesntExist) { + XlaDeviceExecutablePersistor::Config config( + /*persistent_cache_directory=*/cache_dir_, + /*disable_strict_signature_checks=*/false, + /*persistence_prefix=*/"xla"); + XlaDeviceExecutablePersistor persistor(config, + DefaultXlaOptions().device_type); + + MockXlaCompilerClient mock_client; + // Try to load an executable for a different signature hash (which hasn't been + // persisted). + auto loaded_executable = persistor.TryToLoadExecutable( + /*signature_hash=*/12345, "different_signature", DefaultXlaOptions(), + compilation_result_add_, &mock_client); + + EXPECT_FALSE(loaded_executable.has_value()); +} + +TEST_F(DeviceExecutionPersistorTest, LoadSerializedKeyMismatch) { + XlaDeviceExecutablePersistor::Config config( + /*persistent_cache_directory=*/cache_dir_, + /*disable_strict_signature_checks=*/false, + /*persistence_prefix=*/"xla"); + XlaDeviceExecutablePersistor persistor(config, + DefaultXlaOptions().device_type); + + auto key1 = + CreateCacheKey(/*signature_hash=*/123, compilation_result_add_, + persistor.device_type(), persistor.persistence_prefix()); + auto key2 = + CreateCacheKey(/*signature_hash=*/456, compilation_result_add_, + persistor.device_type(), persistor.persistence_prefix()); + // File for key2 contains the same content as key1. + TF_ASSERT_OK(Env::Default()->CopyFile( + GetFilePath(key1, persistor.persistent_cache_directory()), + GetFilePath(key2, persistor.persistent_cache_directory()))); + + MockXlaCompilerClient mock_client; + // Try to load an executable from file corresponding to key2 (whose file + // content corresponds to key1). + auto loaded_executable = persistor.TryToLoadExecutable( + /*signature_hash=*/456, "different_signature", DefaultXlaOptions(), + compilation_result_add_, &mock_client); + + EXPECT_TRUE(loaded_executable.has_value()); + EXPECT_FALSE(loaded_executable->ok()); + EXPECT_THAT(loaded_executable.value(), + absl_testing::StatusIs(error::INVALID_ARGUMENT)); +} + +TEST_F(DeviceExecutionPersistorTest, LoadSerializedHloMismatch) { + XlaDeviceExecutablePersistor::Config config( + /*persistent_cache_directory=*/cache_dir_, + /*disable_strict_signature_checks=*/false, + /*persistence_prefix=*/"xla"); + XlaDeviceExecutablePersistor persistor(config, + DefaultXlaOptions().device_type); + + TF_ASSERT_OK_AND_ASSIGN(auto compilation_result_mul, + BuildSampleCompilationResult(true)); + + auto key1 = + CreateCacheKey(/*signature_hash=*/123, compilation_result_add_, + persistor.device_type(), persistor.persistence_prefix()); + auto key2 = + CreateCacheKey(/*signature_hash=*/123, compilation_result_mul, + persistor.device_type(), persistor.persistence_prefix()); + + // Read serialized entry corresponding to key1. + XlaSerializedCacheEntry entry; + TF_ASSERT_OK(ReadTextOrBinaryProto( + Env::Default(), GetFilePath(key1, persistor.persistent_cache_directory()), + &entry)); + // Change the entry's key to key2. + *entry.mutable_key() = key2; + // Write the modified entry to file corresponding to key2. + TF_ASSERT_OK(WriteBinaryProto( + Env::Default(), GetFilePath(key2, persistor.persistent_cache_directory()), + entry)); + + MockXlaCompilerClient mock_client; + // Try to load executable corresponding to key2 (whose file contains HLO + // corresponding to key1). + auto loaded_executable = persistor.TryToLoadExecutable( + /*signature_hash=*/123, "signature", DefaultXlaOptions(), + compilation_result_mul, &mock_client); + + EXPECT_TRUE(loaded_executable.has_value()); + EXPECT_FALSE(loaded_executable->ok()); + EXPECT_THAT(loaded_executable.value(), + absl_testing::StatusIs(error::INVALID_ARGUMENT)); +} + +TEST_F(DeviceExecutionPersistorTest, LoadStrictChecksDisabled) { + XlaDeviceExecutablePersistor::Config config( + /*persistent_cache_directory=*/cache_dir_, + /*disable_strict_signature_checks=*/true, + /*persistence_prefix=*/"xla"); + XlaDeviceExecutablePersistor persistor(config, + DefaultXlaOptions().device_type); + + TF_ASSERT_OK_AND_ASSIGN(auto compilation_result_mul, + BuildSampleCompilationResult(true)); + + auto key1 = + CreateCacheKey(/*signature_hash=*/123, compilation_result_add_, + persistor.device_type(), persistor.persistence_prefix()); + auto key2 = + CreateCacheKey(/*signature_hash=*/123, compilation_result_mul, + persistor.device_type(), persistor.persistence_prefix()); + + // Read serialized entry corresponding to key1. + XlaSerializedCacheEntry entry; + TF_ASSERT_OK(ReadTextOrBinaryProto( + Env::Default(), GetFilePath(key1, persistor.persistent_cache_directory()), + &entry)); + // Change the entry's key to key2. + *entry.mutable_key() = key2; + // Write the modified entry to file corresponding to key2. + TF_ASSERT_OK(WriteBinaryProto( + Env::Default(), GetFilePath(key2, persistor.persistent_cache_directory()), + entry)); + + MockXlaCompilerClient mock_client; + TF_ASSERT_OK_AND_ASSIGN(auto executable, BuildSampleExecutable()); + EXPECT_CALL(mock_client, LoadExecutable(_, _, serialized_xla_executable_)) + .WillOnce(Return(ByMove(std::move(executable)))); + + auto loaded_executable = + persistor.TryToLoadExecutable(123, "signature", DefaultXlaOptions(), + compilation_result_mul, &mock_client); + + EXPECT_TRUE(loaded_executable.has_value()); + EXPECT_TRUE(loaded_executable->ok()); +} + +TEST_F(DeviceExecutionPersistorTest, LoadSerializedExecutableEmpty) { + XlaDeviceExecutablePersistor::Config config( + /*persistent_cache_directory=*/cache_dir_, + /*disable_strict_signature_checks=*/false, + /*persistence_prefix=*/"xla"); + XlaDeviceExecutablePersistor persistor(config, + DefaultXlaOptions().device_type); + + auto key = + CreateCacheKey(/*signature_hash=*/123, compilation_result_add_, + persistor.device_type(), persistor.persistence_prefix()); + + // Read serialized entry. + XlaSerializedCacheEntry entry; + TF_ASSERT_OK(ReadTextOrBinaryProto( + Env::Default(), GetFilePath(key, persistor.persistent_cache_directory()), + &entry)); + entry.clear_executable(); + // Write entry to another file. + TF_ASSERT_OK(WriteBinaryProto( + Env::Default(), GetFilePath(key, persistor.persistent_cache_directory()), + entry)); + + MockXlaCompilerClient mock_client; + auto loaded_executable = persistor.TryToLoadExecutable( + /*signature_hash=*/123, "signature", DefaultXlaOptions(), + compilation_result_add_, &mock_client); + + EXPECT_TRUE(loaded_executable.has_value()); + EXPECT_FALSE(loaded_executable->ok()); + EXPECT_THAT(loaded_executable.value(), + absl_testing::StatusIs(error::INVALID_ARGUMENT)); +} + +TEST_F(DeviceExecutionPersistorTest, PersistPjRtAndXlaExecutables) { + // Persist PJRT executable. + PjRtDeviceExecutablePersistor::Config pjrt_config( + /*persistent_cache_directory=*/cache_dir_, + /*disable_strict_signature_checks=*/false, + /*persistence_prefix=*/"xla"); + PjRtDeviceExecutablePersistor pjrt_persistor( + pjrt_config, DefaultPjRtOptions().device_type); + + MockPjRtCompilerClient mock_pjrt_client; + EXPECT_CALL(mock_pjrt_client, SerializeExecutable(_)) + .WillOnce(Return(serialized_pjrt_executable_)); + + TF_ASSERT_OK_AND_ASSIGN(auto pjrt_executable, BuildSamplePjRtExecutable()); + TF_EXPECT_OK(pjrt_persistor.TryToPersistExecutable( + /*signature_hash=*/123, "signature_string", DefaultPjRtOptions(), + compilation_result_add_, *pjrt_executable, &mock_pjrt_client)); + + // Persist XLA executable. + XlaDeviceExecutablePersistor::Config xla_config( + /*persistent_cache_directory=*/cache_dir_, + /*disable_strict_signature_checks=*/false, + /*persistence_prefix=*/"xla"); + XlaDeviceExecutablePersistor xla_persistor(xla_config, + DefaultXlaOptions().device_type); + + MockXlaCompilerClient mock_xla_client; + EXPECT_CALL(mock_xla_client, SerializeExecutable(_)) + .WillOnce(Return(serialized_xla_executable_)); + + TF_ASSERT_OK_AND_ASSIGN(auto xla_executable, BuildSampleExecutable()); + TF_EXPECT_OK(xla_persistor.TryToPersistExecutable( + /*signature_hash=*/123, "signature_string", DefaultXlaOptions(), + compilation_result_add_, *xla_executable, &mock_xla_client)); + + // Read and verify serialized PJRT executable. + auto pjrt_key = CreateCacheKey( + /*signature_hash=*/123, compilation_result_add_, + pjrt_persistor.device_type(), pjrt_persistor.persistence_prefix(), true); + TF_ASSERT_OK_AND_ASSIGN(auto entry, + ReadCacheEntryFromFile(pjrt_key, cache_dir_)); + EXPECT_EQ(entry.executable(), serialized_pjrt_executable_); + + // Read and verify serialized XLA executable. + auto xla_key = CreateCacheKey(/*signature_hash=*/123, compilation_result_add_, + pjrt_persistor.device_type(), + pjrt_persistor.persistence_prefix()); + TF_ASSERT_OK_AND_ASSIGN(entry, ReadCacheEntryFromFile(xla_key, cache_dir_)); + EXPECT_EQ(entry.executable(), serialized_xla_executable_); +} + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_util.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_util.cc new file mode 100644 index 0000000000000000000000000000000000000000..535c6a61622a8117d2943d817cd99f07b27dbf88 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_util.cc @@ -0,0 +1,236 @@ +/* Copyright 2019 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/compiler/jit/device_util.h" + +#include "absl/algorithm/container.h" +#include "absl/container/flat_hash_set.h" +#include "xla/status_macros.h" + +namespace tensorflow { +namespace jit { + +void DeviceSet::Insert(DeviceId device_id) { + int word_index = device_id.id() / kWordSize; + int bit_index = device_id.id() % kWordSize; + const int storage_size = storage_.size(); + if (word_index >= storage_size) { + storage_.resize(word_index + 1, 0); + } + + storage_[word_index] |= (1ull << bit_index); +} + +void DeviceSet::UnionWith(const DeviceSet& other) { + if (other.storage_.size() > storage_.size()) { + storage_.resize(other.storage_.size(), 0); + } + + for (int i = 0, end = other.storage_.size(); i < end; i++) { + storage_[i] |= other.storage_[i]; + } +} + +bool DeviceSet::IsEmpty() const { + return absl::c_all_of(storage_, [&](uint64_t val) { return val == 0; }); +} + +absl::StatusOr DeviceInfoCache::GetIdFor(absl::string_view name) { + TF_RET_CHECK(!name.empty()); + + auto it = name_to_id_.find(name); + if (it != name_to_id_.end()) { + return it->second; + } + + int new_id = names_.size(); + names_.push_back(std::string(name)); + id_to_device_type_.push_back(std::make_unique("")); + DeviceType* device_type = id_to_device_type_.back().get(); + TF_RETURN_IF_ERROR(DeviceNameToDeviceType(names_.back(), device_type)); + + is_cpu_.push_back(device_type->type_string() == DEVICE_CPU); + is_gpu_.push_back(device_type->type_string() == DEVICE_GPU); + + name_to_id_.emplace(std::string(name), DeviceId(new_id)); + + const XlaOpRegistry::DeviceRegistration* compilation_device; + if (!XlaOpRegistry::GetCompilationDevice(device_type->type(), + &compilation_device)) { + compilation_device = nullptr; + } + id_to_compilation_device_.push_back(compilation_device); + + return DeviceId(new_id); +} + +std::string DeviceInfoCache::DebugString(const DeviceSet& device_set) const { + std::vector names; + device_set.ForEach([&](DeviceId device_id) { + names.push_back(std::string(GetNameFor(device_id))); + return true; + }); + + return absl::StrCat("[", absl::StrJoin(names, ","), "]"); +} +} // namespace jit + +absl::Status DeviceNameToDeviceType(const std::string& device, + DeviceType* device_type) { + DeviceNameUtils::ParsedName parsed; + if (!DeviceNameUtils::ParseFullName(device, &parsed)) { + return absl::InternalError( + absl::StrCat("Malformed assigned device '", device, "'")); + } + *device_type = DeviceType(parsed.type); + return absl::OkStatus(); +} + +absl::StatusOr> PickDeviceForXlaImpl( + const jit::DeviceInfoCache& device_info_cache, + const jit::DeviceSet& devices, bool allow_mixing_unknown_and_cpu, + bool failure_to_pick_is_error) { +#define FAILED_TO_PICK_DEVICE(failing_status) \ + do { \ + if (failure_to_pick_is_error) { \ + return failing_status; \ + } else { \ + return {std::nullopt}; \ + } \ + } while (false) + + std::optional maybe_gpu_device; + std::optional maybe_cpu_device; + std::optional maybe_unknown_device; + + bool multiple_cpu_devices = false; + bool multiple_gpu_devices = false; + bool multiple_unknown_devices = false; + + // Returns 'true' if d0 and d1 are conflicting devices. If they are + // compatible, update d1 with a more specific one. + // TODO(sanjoy): Cache DeviceNameUtils::ParsedName inside device_info_cache. + const auto is_multiple_devices = + [&](const jit::DeviceId& d0, std::optional* d1) -> bool { + const absl::string_view name0 = device_info_cache.GetNameFor(d0); + const absl::string_view name1 = device_info_cache.GetNameFor(d1->value()); + + DeviceNameUtils::ParsedName parsed0, parsed1; + if (!DeviceNameUtils::ParseFullName(name0, &parsed0) || + !DeviceNameUtils::ParseFullName(name1, &parsed1) || + !DeviceNameUtils::AreCompatibleDevNames(parsed0, parsed1)) { + return true; + } + + if (DeviceNameUtils::IsSpecification(parsed0, parsed1)) { + return false; + } + + if (DeviceNameUtils::IsSpecification(parsed1, parsed0)) { + *d1 = d0; + return false; + } + + return true; + }; + + devices.ForEach([&](jit::DeviceId device) { + if (device_info_cache.IsGpu(device)) { + if (maybe_gpu_device) { + multiple_gpu_devices = is_multiple_devices(device, &maybe_gpu_device); + if (multiple_gpu_devices) return false; + } else { + maybe_gpu_device = device; + } + } else if (device_info_cache.IsCpu(device)) { + if (maybe_cpu_device) { + multiple_cpu_devices = is_multiple_devices(device, &maybe_cpu_device); + if (multiple_cpu_devices) return false; + } else { + maybe_cpu_device = device; + } + } else { + if (maybe_unknown_device) { + multiple_unknown_devices = true; + return false; + } + maybe_unknown_device = device; + } + + return true; + }); + + if (multiple_cpu_devices) { + FAILED_TO_PICK_DEVICE(absl::InternalError(absl::StrCat( + "Multiple CPU devices ", device_info_cache.DebugString(devices)))); + } + + if (multiple_gpu_devices) { + FAILED_TO_PICK_DEVICE(absl::InternalError(absl::StrCat( + "Multiple GPU devices ", device_info_cache.DebugString(devices)))); + } + + if (multiple_unknown_devices) { + FAILED_TO_PICK_DEVICE(absl::InternalError(absl::StrCat( + "Multiple unknown devices ", device_info_cache.DebugString(devices)))); + } + + if (maybe_unknown_device && maybe_gpu_device) { + FAILED_TO_PICK_DEVICE(absl::InternalError( + absl::StrCat("Found both unknown and GPU devices: ", + device_info_cache.GetNameFor(*maybe_unknown_device), ", ", + device_info_cache.GetNameFor(*maybe_gpu_device)))); + } + + if (!allow_mixing_unknown_and_cpu) { + if (maybe_unknown_device && maybe_cpu_device) { + FAILED_TO_PICK_DEVICE(absl::InternalError( + absl::StrCat("Found both unknown and CPU devices: ", + device_info_cache.GetNameFor(*maybe_unknown_device), + ", ", device_info_cache.GetNameFor(*maybe_cpu_device)))); + } + } + + if (maybe_gpu_device) { + return {*maybe_gpu_device}; + } else if (maybe_unknown_device) { + return {*maybe_unknown_device}; + } else if (maybe_cpu_device) { + return {*maybe_cpu_device}; + } + + FAILED_TO_PICK_DEVICE(absl::InternalError("Empty device set!")); + +#undef FAILED_TO_PICK_DEVICE +} + +absl::StatusOr PickDeviceForXla( + const jit::DeviceInfoCache& device_info_cache, + const jit::DeviceSet& devices, bool allow_mixing_unknown_and_cpu) { + TF_ASSIGN_OR_RETURN(std::optional device_id, + PickDeviceForXlaImpl(device_info_cache, devices, + allow_mixing_unknown_and_cpu, + /*failure_to_pick_is_error=*/true)); + return *device_id; +} + +absl::StatusOr> MaybePickDeviceForXla( + const jit::DeviceInfoCache& device_info_cache, + const jit::DeviceSet& devices, bool allow_mixing_unknown_and_cpu) { + return PickDeviceForXlaImpl(device_info_cache, devices, + allow_mixing_unknown_and_cpu, + /*failure_to_pick_is_error=*/false); +} +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_util.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_util.h new file mode 100644 index 0000000000000000000000000000000000000000..6b0190cbd7d9243214f03c4e7ba9745007c06dd0 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_util.h @@ -0,0 +1,203 @@ +/* Copyright 2019 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_DEVICE_UTIL_H_ +#define TENSORFLOW_COMPILER_JIT_DEVICE_UTIL_H_ + +#include +#include + +#include "absl/container/flat_hash_map.h" +#include "absl/numeric/bits.h" +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" +#include "absl/types/span.h" +#include "tensorflow/compiler/tf2xla/xla_op_registry.h" +#include "xla/status_macros.h" +#include "tensorflow/core/framework/types.h" + +namespace tensorflow { +namespace jit { +class DeviceInfoCache; +class DeviceSet; + +// Instances of DeviceId represent TensorFlow devices as integers. +// +// This helps avoid having to manipulate device names as strings when +// auto-clustering. +class DeviceId { + public: + DeviceId(DeviceId&&) = default; + DeviceId(const DeviceId&) = default; + DeviceId& operator=(const DeviceId&) = default; + + bool operator==(const DeviceId& other) const { return id() == other.id(); } + bool operator!=(const DeviceId& other) const { return !(*this == other); } + + private: + int id_; + + explicit DeviceId(int id) : id_(id) {} + + int id() const { return id_; } + + friend class DeviceInfoCache; + friend class DeviceSet; +}; + +// A set of DeviceIds, represented as a bitmap. +class DeviceSet { + public: + void Insert(DeviceId device_id); + void UnionWith(const DeviceSet& other); + bool IsEmpty() const; + + // Calls `func` on each DeviceId in the set. Stops iterating early if `func` + // return false. + // + // TODO(sanjoy): Change this to take a typed std::function if that's + // performance neutral. + template + void ForEach(FnTy func) const { + // This is really a poor man's iterator, we should consider writing a proper + // iterator if this ends up being used widely. + for (int word_index = 0, end = storage_.size(); word_index < end; + word_index++) { + uint64_t word = storage_[word_index]; + while (word != 0) { + uint64_t only_lowest_bit_set = word & -word; + // The number of trailing zeros in a non-zero word is the index of the + // least significant 1. + int bit_index = absl::countr_zero(word); + if (!func(DeviceId(word_index * kWordSize + bit_index))) { + return; + } + word ^= only_lowest_bit_set; + } + } + } + + private: + absl::InlinedVector storage_; + + const int kWordSize = 64; +}; + +// Caches some miscellaneous information about TF devices. Thread compatible. +class DeviceInfoCache { + public: + bool IsGpu(DeviceId device) const { return is_gpu_[device.id()]; } + bool IsCpu(DeviceId device) const { return is_cpu_[device.id()]; } + + absl::string_view GetNameFor(DeviceId device) const { + return names_[device.id()]; + } + + absl::StatusOr GetIdFor(absl::string_view name); + + using DeviceRegistration = const XlaOpRegistry::DeviceRegistration; + + DeviceRegistration* GetCompilationDevice(DeviceId device) const { + return id_to_compilation_device_[device.id()]; + } + + absl::StatusOr GetCompilationDevice( + absl::string_view name) { + TF_ASSIGN_OR_RETURN(DeviceId device_id, GetIdFor(name)); + return GetCompilationDevice(device_id); + } + + const DeviceType& GetDeviceTypeFor(DeviceId device) const { + return *id_to_device_type_[device.id()]; + } + + using DeviceTypeConstRef = std::reference_wrapper; + + absl::StatusOr GetDeviceTypeFor( + absl::string_view device_name) { + TF_ASSIGN_OR_RETURN(DeviceId device_id, GetIdFor(device_name)); + return std::cref(*id_to_device_type_[device_id.id()]); + } + + std::string DebugString(const DeviceSet& device_set) const; + + private: + absl::flat_hash_map name_to_id_; + + // These fields are populated for a device in GetIdFor, *before* we give out a + // DeviceId. + std::vector + id_to_compilation_device_; + std::vector> id_to_device_type_; + std::vector names_; + std::vector is_cpu_; + std::vector is_gpu_; +}; + +} // namespace jit + +// Returns the DeviceType corresponding to 'device'. +absl::Status DeviceNameToDeviceType(const std::string& device, + DeviceType* device_type); + +// Picks the device for which XLA should compile a cluster that contains +// operations placed in devices in `devices`. For instance a cluster that +// contains operations solely placed on the CPU will be compiled into a CPU +// executable by XLA, whereas a cluster that contains operations placed on the +// CPU and also operations placed on the GPU will be compiled into a GPU +// executable. +// +// Returns a non-OK Status if no unambiguous choice of device exists. +// +// We choose the device using the following rules: +// +// - It is an error for `device_names` to contain more than one device of the +// same type. +// - GPU is preferred over CPU. +// - If `allow_mixing_unknown_and_cpu` is true then unknown devices are +// preferred over CPU. +// - XLA devices count as "unrecognized devices". +// +// This set of rules above implicitly assume that XLA:GPU can compile all +// operations in the cluster that XLA:CPU can compile, and if +// `allow_mixing_unknown_and_cpu` then the unrecognized device can also compile +// all operations in the cluster that XLA:CPU can compile. +// +// We provide the `allow_mixing_unknown_and_cpu` knob so that we can do both of +// the following things: +// +// - Let MarkForCompilationPass not inject CPU-placed operations into clusters +// that will run on unknown devices (because the unknown XLA backend may not +// support every operation supported by CPU). +// - Let BuildXlaOpsPass successfully infer a compilation device for a cluster +// that contains nodes placed on both the CPU and on unknown devices. In this +// case it is the responsibility of the optimization pass that injected the +// CPU nodes into the cluster to ensure that these nodes can be compiled by +// the unknown XLA backend. +absl::StatusOr PickDeviceForXla( + const jit::DeviceInfoCache& device_info_cache, + const jit::DeviceSet& devices, bool allow_mixing_unknown_and_cpu); + +// This is like `PickDeviceForXla` except that it returns nullopt (instead of a +// non-OK Status) if no unambiguous choice of device exists. +// +// We return a failing Status for errors unrelated to the device choice +// algorithm itself. +absl::StatusOr> MaybePickDeviceForXla( + const jit::DeviceInfoCache& device_info_cache, + const jit::DeviceSet& devices, bool allow_mixing_unknown_and_cpu); +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_DEVICE_UTIL_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_util_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_util_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..0e7ae7d5b540f0942c6187b143ec110ac7f2fe49 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/device_util_test.cc @@ -0,0 +1,144 @@ +/* Copyright 2019 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/compiler/jit/device_util.h" + +#include "tensorflow/core/lib/core/status_test_util.h" +#include "tensorflow/core/platform/test.h" + +namespace tensorflow { +namespace { + +absl::Status PickDeviceHelper(bool allow_mixing_unknown_and_cpu, + absl::Span device_names, + std::string* result) { + jit::DeviceInfoCache cache; + jit::DeviceSet device_set; + for (absl::string_view name : device_names) { + TF_ASSIGN_OR_RETURN(jit::DeviceId device_id, cache.GetIdFor(name)); + device_set.Insert(device_id); + } + + TF_ASSIGN_OR_RETURN( + jit::DeviceId result_id, + PickDeviceForXla(cache, device_set, allow_mixing_unknown_and_cpu)); + *result = std::string(cache.GetNameFor(result_id)); + return absl::OkStatus(); +} + +void CheckPickDeviceResult(absl::string_view expected_result, + bool allow_mixing_unknown_and_cpu, + absl::Span inputs) { + std::string result; + TF_ASSERT_OK(PickDeviceHelper(allow_mixing_unknown_and_cpu, inputs, &result)) + << "inputs = [" << absl::StrJoin(inputs, ", ") + << "], allow_mixing_unknown_and_cpu=" << allow_mixing_unknown_and_cpu + << ", expected_result=" << expected_result; + EXPECT_EQ(result, expected_result); +} + +void CheckPickDeviceHasError(bool allow_mixing_unknown_and_cpu, + absl::Span inputs) { + std::string result; + EXPECT_FALSE( + PickDeviceHelper(allow_mixing_unknown_and_cpu, inputs, &result).ok()); +} + +const char* kCPU0 = "/job:localhost/replica:0/task:0/device:CPU:0"; +const char* kGPU0 = "/job:localhost/replica:0/task:0/device:GPU:0"; +const char* kXPU0 = "/job:localhost/replica:0/task:0/device:XPU:0"; +const char* kYPU0 = "/job:localhost/replica:0/task:0/device:YPU:0"; + +const char* kCPU1 = "/job:localhost/replica:0/task:0/device:CPU:1"; +const char* kGPU1 = "/job:localhost/replica:0/task:0/device:GPU:1"; +const char* kXPU1 = "/job:localhost/replica:0/task:0/device:XPU:1"; + +const char* kCPU0Partial = "/device:CPU:0"; +const char* kGPU0Partial = "/device:GPU:0"; +const char* kXPU0Partial = "/device:XPU:0"; + +TEST(PickDeviceForXla, UniqueDevice) { + CheckPickDeviceResult(kGPU0, false, {kGPU0, kGPU0}); +} + +TEST(PickDeviceForXla, MoreSpecificDevice) { + CheckPickDeviceResult(kCPU0, false, {kCPU0, kCPU0Partial}); + CheckPickDeviceResult(kGPU0, false, {kGPU0, kGPU0Partial}); + // Unknown devices do not support merging of full and partial specifications. + CheckPickDeviceHasError(false, {kXPU1, kXPU0Partial}); +} + +TEST(PickDeviceForXla, DeviceOrder) { + CheckPickDeviceResult(kGPU0, false, {kGPU0, kCPU0}); + CheckPickDeviceResult(kGPU0, false, {kCPU0, kGPU0}); + CheckPickDeviceResult(kXPU0, true, {kXPU0, kCPU0}); +} + +TEST(PickDeviceForXla, MultipleUnknownDevices) { + CheckPickDeviceHasError(false, {kXPU0, kYPU0}); +} + +TEST(PickDeviceForXla, GpuAndUnknown) { + CheckPickDeviceHasError(false, {kGPU0, kXPU1}); +} + +TEST(PickDeviceForXla, UnknownAndCpu) { + CheckPickDeviceHasError(false, {kXPU0, kCPU1}); +} + +TEST(PickDeviceForXla, MultipleDevicesOfSameType) { + CheckPickDeviceHasError(true, {kCPU0, kCPU1}); + CheckPickDeviceHasError(false, {kCPU0, kCPU1}); + CheckPickDeviceHasError(false, {kGPU0, kGPU1}); + CheckPickDeviceHasError(false, {kXPU0, kXPU1}); + CheckPickDeviceHasError(false, {kCPU0, kCPU1, kGPU0}); +} + +void SimpleRoundTripTestForDeviceSet(int num_devices) { + jit::DeviceSet device_set; + jit::DeviceInfoCache device_info_cache; + + std::vector expected_devices, actual_devices; + + for (int i = 0; i < num_devices; i++) { + std::string device_name = + absl::StrCat("/job:localhost/replica:0/task:0/device:XPU:", i); + TF_ASSERT_OK_AND_ASSIGN(jit::DeviceId device_id, + device_info_cache.GetIdFor(device_name)); + device_set.Insert(device_id); + expected_devices.push_back(device_name); + } + + device_set.ForEach([&](jit::DeviceId device_id) { + actual_devices.push_back( + std::string(device_info_cache.GetNameFor(device_id))); + return true; + }); + + EXPECT_EQ(expected_devices, actual_devices); +} + +TEST(DeviceSetTest, SimpleRoundTrip_One) { SimpleRoundTripTestForDeviceSet(1); } + +TEST(DeviceSetTest, SimpleRoundTrip_Small) { + SimpleRoundTripTestForDeviceSet(8); +} + +TEST(DeviceSetTest, SimpleRoundTrip_Large) { + SimpleRoundTripTestForDeviceSet(800); +} + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/encapsulate_subgraphs_pass.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/encapsulate_subgraphs_pass.cc new file mode 100644 index 0000000000000000000000000000000000000000..cbca6416f3f0a845a84759b5ee3caeca0bc035fb --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/encapsulate_subgraphs_pass.cc @@ -0,0 +1,1334 @@ +/* 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/compiler/jit/encapsulate_subgraphs_pass.h" + +#include +#include +#include +#include +#include + +#include "absl/container/flat_hash_map.h" +#include "absl/container/flat_hash_set.h" +#include "absl/strings/match.h" +#include "absl/strings/str_cat.h" +#include "absl/types/optional.h" +#include "tensorflow/compiler/jit/flags.h" +#include "tensorflow/compiler/jit/mark_for_compilation_pass.h" +#include "tensorflow/compiler/jit/shape_inference_helpers.h" +#include "tensorflow/compiler/jit/xla_cluster_util.h" +#include "tensorflow/compiler/tf2xla/const_analysis.h" +#include "xla/service/graphcycles/graphcycles.h" +#include "xla/status_macros.h" +#include "tensorflow/core/common_runtime/device_factory.h" +#include "tensorflow/core/common_runtime/function.h" +#include "tensorflow/core/common_runtime/optimization_registry.h" +#include "tensorflow/core/common_runtime/shape_refiner.h" +#include "tensorflow/core/framework/function.h" +#include "tensorflow/core/framework/graph_def_util.h" +#include "tensorflow/core/framework/graph_to_functiondef.h" +#include "tensorflow/core/framework/node_def_builder.h" +#include "tensorflow/core/framework/node_def_util.h" +#include "tensorflow/core/framework/tensor.pb.h" +#include "tensorflow/core/graph/algorithm.h" +#include "tensorflow/core/graph/control_flow.h" +#include "tensorflow/core/graph/graph.h" +#include "tensorflow/core/graph/graph_def_builder.h" +#include "tensorflow/core/graph/tensor_id.h" +#include "tensorflow/core/lib/gtl/map_util.h" +#include "tensorflow/core/lib/hash/hash.h" +#include "tensorflow/core/public/session_options.h" +#include "tensorflow/core/public/version.h" +#include "tensorflow/core/util/device_name_utils.h" +#include "tensorflow/core/util/dump_graph.h" + +namespace tensorflow { + +const char* const kXlaCompiledKernelAttr = "_XlaCompiledKernel"; +const char* const kXlaNumConstantArgsAttr = "_XlaNumConstantArgs"; +const char* const kXlaNumResourceArgsAttr = "_XlaNumResourceArgs"; +const char* const kXlaHostTransferSequencerAttr = + "_xla_host_transfer_sequencer"; +const char* const kXlaHasReferenceVarsAttr = "_XlaHasReferenceVars"; + +namespace { + +bool AreAllParentsGuaranteedConst( + const Node& n, + const absl::flat_hash_set& runtime_const_nodes) { + if (n.type_string() == "GuaranteeConst") { + // If the current node is itself a cast-to-const, no need + // to look at the incoming edges. + return true; + } + + bool all_parents_const = true; + bool atleast_one_non_control_edge = false; + for (const Edge* in : n.in_edges()) { + atleast_one_non_control_edge = + atleast_one_non_control_edge || !in->IsControlEdge(); + if (!in->IsControlEdge() && runtime_const_nodes.count(in->src()) == 0) { + all_parents_const = false; + break; + } + } + return all_parents_const && atleast_one_non_control_edge; +} + +void MarkGuaranteedConstants( + const Graph& graph, + const std::vector>& src_arg_pairs) { + absl::flat_hash_set guaranteed_const_nodes; + std::vector srcs; + srcs.reserve(src_arg_pairs.size()); + for (const auto& src_arg : src_arg_pairs) { + srcs.push_back(src_arg.first); + } + ReverseDFSFrom( + graph, srcs, /*enter=*/nullptr, + /*leave=*/[&guaranteed_const_nodes](const Node* n) { + // TODO(vinuraja): Doesn't work in the presence of loops. + if (AreAllParentsGuaranteedConst(*n, guaranteed_const_nodes)) { + guaranteed_const_nodes.insert(n); + } + }); + + for (auto& src_arg : src_arg_pairs) { + if (guaranteed_const_nodes.count(src_arg.first) != 0) { + VLOG(1) << "Guaranteed const found: " << src_arg.first->DebugString(); + src_arg.second->AddAttr("_is_guaranteed_constant", true); + } + } +} + +struct OutputInputTensorPairHasher { + uint64_t operator()(std::pair const& s) const { + return Hash64Combine(OutputTensor::Hash()(s.first), + InputTensor::Hash()(s.second)); + } +}; + +// TODO(phawkins) add a canonical copy of these operator names and refactor +// everything to use it. +static const char* const kArgOp = "_Arg"; +static const char* const kRetValOp = "_Retval"; + +class Encapsulator { + public: + Encapsulator(std::string group_attribute, Graph const* graph_in) + : group_attribute_(std::move(group_attribute)), graph_in_(graph_in) {} + + // Find subgraphs marked with 'group_attribute', and build a new + // subgraph, one for each value of 'group_attribute'. + absl::Status SplitIntoSubgraphs(FunctionLibraryDefinition* library); + + // Build a FunctionDef for each subgraph, and add it 'library'. The values of + // the 'group_attribute' annotations become the function names. + // If 'reuse_existing_functions' is set, use an existing function with the + // same name, if any. + // If 'rewrite_subgraph_fn' is set, it is applied to each subgraph before + // function conversion. + absl::Status BuildFunctionDefs(const RewriteSubgraphFn& rewrite_subgraph_fn, + bool reuse_existing_functions, + FunctionLibraryDefinition* library); + + // Write a copy of the input graph to 'graph_out', where the subgraphs are + // replaced with calls to the new functions. + absl::Status BuildOutputGraph(Graph* graph_out, + FunctionLibraryDefinition* library); + + private: + // A subgraph of the input, all marked with a common 'group_attribute' + // value. + // + // In the following simple example, A, B, ..., E are nodes in the original + // graph. The group attributes g are each shown as either 0 or empty. + // + // A --> B --> C --> D --> E + // g: g:0 g:0 g:0 g: + // + // The example is rewritten to two graphs; one on the host and one to be + // compiled. The host graph is as follows. + // + // A --> Call --> E + // + // The compiled cluster is as follows. + // + // Arg --> B --> C --> D --> Retval + class Subgraph { + public: + // Creates a graph to build the subgraph in, if it doesn't already exist, + // using the same op registry and versions as graph_in. + Node* MakeNodeImage(const Graph* graph_in, Node* node); + + // Returns the graph the subgraph is being built in. + Graph* GetGraph() const; + + // Builds a FunctionDef, and adds it to 'library'. The value of the + // 'group_attribute' annotations becomes the function name. If + // 'reuse_existing_functions' is set, use an existing function with the same + // name, if any. If 'rewrite_subgraph_fn' is set, it is applied to the + // subgraph before function conversion. + absl::Status BuildFunctionDef(const std::string& name_in, + const RewriteSubgraphFn& rewrite_subgraph_fn, + bool reuse_existing_functions, + FunctionLibraryDefinition* library); + + // Adds the function call node to graph_out. + absl::Status AddFunctionCallNode( + const absl::flat_hash_map& node_images, + Graph* graph_out); + + // Returns the Node that the inputs and outputs of the function should be + // wired up to. + Node* GetCallNode() const; + + // Returns the index of the arg that the dst of edge should connect to. + int GetArgIndexForEdge(const Edge* edge) const; + + // Returns the index of the result that the src of edge should connect to. + int GetResultIndexForEdge(const Edge* edge) const; + + // Creates an _Arg node for the src node of edge, and add its index to + // args_by_src_, if none exists yet. Also adds its index to args_by_dst_, + // and adds the edge within the subgraph from the _Arg node to the image of + // the dst node. + absl::Status RecordArg( + const Edge* edge, + const absl::flat_hash_map& node_images, + std::vector>* src_arg_pairs); + + // Records the src of the given edge as a control result of the graph. + // Used during graph to function conversion to tie control results to + // the function signature. + absl::Status RecordControlResult( + const Edge* edge, + const absl::flat_hash_map& node_images); + + // Creates a _Retval node for the src node of edge, and add it to results_, + // if none exists yet. If a new _Retval node is created, also adds the edge + // within the subgraph from the src to the _Retval node. + absl::Status RecordResult( + const Edge* edge, + const absl::flat_hash_map& node_images); + + // Creates the sequencer node if it doesn't exist, adding it to graph_out. + absl::Status MakeSequencingNode(const std::string& subgraph_name, + Graph* graph_out); + + // If there is a sequencer node, adds a control edge from the sequencer to + // the call node. + void ConnectSequencerToCallNode(Graph* graph_out); + + absl::Status ReplaceFunctionDef(FunctionLibraryDefinition* library); + + private: + // The subgraph extracted from the input graph, suitable for being turned + // into a FunctionDef. Inputs are fed by _Arg nodes, and outputs are + // returned by _Retval nodes. + std::unique_ptr graph_; + + // Which device are these nodes on? Used to assign a device to the call + // node. + std::string device_; + + // NodeDef for the function call node. + NodeDef call_node_def_; + + // Name that is used for the call node. This may not be + // call_node_def_.name() if the client supplies a rewrite lambda. + std::string function_def_name_; + + // Placeholder node simulating the host compute key in the output graph. + // Not owned. + Node* host_compute_key_placeholder_ = nullptr; + + // Function call node in the output graph. Not owned. + Node* call_node_; + + // Maps from source (producer node/slot) and destination + // (consumer node/slot) tensors in the input graph to _Arg numbers in + // the subgraph. The source map is one-to-one, whereas the dest map may be + // many-to-one. + absl::flat_hash_map args_by_src_; + absl::flat_hash_map args_by_dst_; + + // The arguments to the subgraph, in order. + std::vector args_; + + // Map from source tensor in the input graph to result #. + absl::flat_hash_map results_; + + // Set of node names that are the source of a control output of the + // subgraph. We store strings here so that we can tolerate nodes being + // removed from the graph. + absl::flat_hash_set control_output_nodes_; + + // NoOp node in the output graph that is sequenced after the call node. + Node* sequencer_ = nullptr; + }; + + // Returns the key attribute associated with a node in attr. Sets either + // result to the empty string if the respective attribute is not found. + absl::Status GetFunctionNameAttr(Node const* node, std::string* attr) const; + + // Copies edges local to a subgraph. Adds _Arg and _Retval nodes to + // subgraphs for data edges that cross subgraph boundaries. + absl::Status CopySubgraphEdges( + const absl::flat_hash_map& node_images, + std::vector>* src_arg_pairs); + + // Copies all marked nodes to a subgraph. Does nothing for unmarked nodes. + absl::Status CopySubgraphNodes( + absl::flat_hash_map* node_images); + + // Copies all nodes that aren't in a compiled subgraph to the output graph. + absl::Status CopyNodesToOutputGraph( + Graph* graph_out, absl::flat_hash_map* node_images); + + // Adds function call nodes for each compiled subgraph. + absl::Status AddFunctionCallNodes( + const absl::flat_hash_map& node_images, + Graph* graph_out); + + // Finds the image of an edge source in the output graph. If the edge crosses + // a subgraph boundary it is the output of a call node, otherwise it is a node + // in the output graph. + absl::Status FindOutputImageOfEdgeSrc( + const std::string& src_func_id, const std::string& dst_func_id, + const absl::flat_hash_map& node_images, + const Node* original_src_node, Node** src_image); + + // Finds an edge source slot in the output graph. If the edge crosses a + // subgraph boundary it is a slot on the output of a call node, otherwise it + // is a slot on a node in the output graph. + int FindOutputSlotOfEdgeSrc(const std::string& src_func_id, + const std::string& dst_func_id, const Edge* edge); + + // Finds the image of an edge destination in the output graph. If the edge + // crosses a subgraph boundary it is the input of a call node, otherwise it is + // a node in the output graph. + absl::Status FindOutputImageOfEdgeDst( + const std::string& src_func_id, const std::string& dst_func_id, + const absl::flat_hash_map& node_images, + const Node* original_dst_node, Node** dst_image); + + // Finds an edge destination slot in the output graph. If the edge crosses a + // subgraph boundary it is a slot on the input of a call node, otherwise it is + // a slot on a node in the output graph. + int FindOutputSlotOfEdgeDst(const std::string& src_func_id, + const std::string& dst_func_id, const Edge* edge); + + // Copies a single edge to the output graph. The edge is either entirely + // within the output graph, or crosses into or out of a compiled subgraph. + absl::Status CopyEdgeToOutputGraph( + const Edge* edge, const std::string& src_func_id, + const std::string& dst_func_id, + const absl::flat_hash_map& node_images, + Graph* graph_out, + absl::flat_hash_set, + OutputInputTensorPairHasher>* edges_added); + + // Adds all edges to the output graph. + absl::Status AddEdgesToOutputGraph( + const absl::flat_hash_map& node_images, + Graph* graph_out); + + // Makes a copy of graph containing only nodes that are ancestors of at least + // one node in send_from_host_nodes and store it in pruned_graph. On exit + // nodes_images contains a mapping from nodes in graph to nodes in + // pruned_graph. All functions in the copied graph are inlined. + absl::Status MakePrunedGraphCopyAndInline( + const Graph& graph, const std::vector& sink_nodes, + std::unique_ptr* pruned_graph, + absl::flat_hash_map* node_images, + FunctionLibraryDefinition* library); + + const std::string group_attribute_; + const Graph* graph_in_; + + absl::flat_hash_map subgraphs_; + + Encapsulator(const Encapsulator&) = delete; + void operator=(const Encapsulator&) = delete; +}; + +namespace { + +// Return in 'sorted' a topological sort of clusters according to the +// dependencies encoded in ancestors. clusters is the list of all clusters +// including clusters that are not present in the ancestors map. has_successors +// is the set of clusters that are ancestors of some other cluster. +void TopologicalClusterSort( + const absl::flat_hash_set& clusters, + const absl::flat_hash_set& has_successors, + const absl::flat_hash_map>& + ancestors, + std::vector* sorted) { + // The nodes are placed in 'sorted' in topological order. + sorted->clear(); + // We don't use the standard DFS because we are not operating on Node* + // objects. + struct Work { + std::string cluster; + bool leave; + }; + std::set visited; + std::vector stack; + // Seed the processing list with clusters that have no successors. + for (const auto& cluster : clusters) { + if (has_successors.find(cluster) == has_successors.end()) { + stack.push_back({cluster, false}); + } + } + while (!stack.empty()) { + const Work item = stack.back(); + stack.pop_back(); + if (item.leave) { + sorted->push_back(item.cluster); + continue; + } + + if (visited.find(item.cluster) != visited.end()) continue; + visited.insert(item.cluster); + + stack.push_back({item.cluster, true}); + const auto& iter = ancestors.find(item.cluster); + if (iter != ancestors.end()) { + for (const auto& ancestor : iter->second) { + stack.push_back({ancestor, false}); + } + } + } + CHECK(sorted->size() == clusters.size()); +} + +} // namespace + +Node* Encapsulator::Subgraph::GetCallNode() const { return call_node_; } + +int Encapsulator::Subgraph::GetArgIndexForEdge(const Edge* edge) const { + return args_by_dst_.at(InputTensor(edge->dst(), edge->dst_input())); +} + +int Encapsulator::Subgraph::GetResultIndexForEdge(const Edge* edge) const { + return results_.at(OutputTensor(edge->src(), edge->src_output())); +} + +Node* Encapsulator::Subgraph::MakeNodeImage(const Graph* graph_in, Node* node) { + if (!graph_) { + graph_.reset(new Graph(graph_in->op_registry())); + graph_->set_versions(graph_in->versions()); + } + + // TODO(b/116981129): Enhance how the device for the encapsulated subgraph is + // determined. In case of hard placement, ensure all the encapsulated nodes + // have the same requested device, which in turn will be the requested device + // for the entire encapsulated subgraph. In case of soft placement, use a + // deterministic approach to fill in the requested device. Handle co-location + // constraints similarly if they exist. + if (device_.empty()) { + device_ = node->assigned_device_name().empty() + ? node->requested_device() + : node->assigned_device_name(); + } + + return graph_->CopyNode(node); +} + +Graph* Encapsulator::Subgraph::GetGraph() const { return graph_.get(); } + +absl::Status Encapsulator::Subgraph::RecordArg( + const Edge* edge, + const absl::flat_hash_map& node_images, + std::vector>* src_arg_pairs) { + Node* src_node = edge->src(); + int src_slot = edge->src_output(); + absl::flat_hash_map::iterator iter; + bool inserted; + std::tie(iter, inserted) = args_by_src_.emplace( + OutputTensor(src_node, src_slot), args_by_src_.size()); + int arg_index = iter->second; + if (inserted) { + NodeDef arg_def; + NodeDefBuilder builder( + absl::StrCat(src_node->name(), "_", src_slot, "_arg"), kArgOp, + NodeDebugInfo(src_node->def())); + DataType dtype = edge->dst()->input_type(edge->dst_input()); + builder.Attr("T", dtype); + builder.Attr("index", arg_index); + absl::Status s = builder.Finalize(&arg_def); + if (!s.ok()) return s; + + TF_ASSIGN_OR_RETURN(Node * arg, graph_->AddNode(arg_def)); + src_arg_pairs->push_back({src_node, arg}); + args_.push_back(arg); + } + Node* dst_node = edge->dst(); + Node* dst_image = node_images.at(dst_node); + int dst_slot = edge->dst_input(); + args_by_dst_[InputTensor(dst_node, dst_slot)] = arg_index; + graph_->AddEdge(args_[arg_index], 0, dst_image, dst_slot); + return absl::OkStatus(); +} + +absl::Status Encapsulator::Subgraph::RecordControlResult( + const Edge* edge, + const absl::flat_hash_map& node_images) { + Node* src_node = edge->src(); + Node* src_image = node_images.at(src_node); + control_output_nodes_.insert(src_image->name()); + return absl::OkStatus(); +} + +absl::Status Encapsulator::Subgraph::RecordResult( + const Edge* edge, + const absl::flat_hash_map& node_images) { + Node* src_node = edge->src(); + Node* src_image = node_images.at(src_node); + int src_slot = edge->src_output(); + absl::flat_hash_map::iterator iter; + bool inserted; + std::tie(iter, inserted) = + results_.emplace(OutputTensor(src_node, src_slot), results_.size()); + int ret_index = iter->second; + if (inserted) { + NodeDef ret_def; + NodeDefBuilder builder( + absl::StrCat(src_node->name(), "_", src_slot, "_retval"), kRetValOp, + NodeDebugInfo(src_node->def())); + DataType dtype = src_node->output_type(src_slot); + builder.Attr("T", dtype); + builder.Attr("index", ret_index); + builder.Input(src_image->name(), src_slot, dtype); + absl::Status s = builder.Finalize(&ret_def); + if (!s.ok()) return s; + TF_ASSIGN_OR_RETURN(Node * ret, graph_->AddNode(ret_def)); + graph_->AddEdge(src_image, src_slot, ret, 0); + } + return absl::OkStatus(); +} + +absl::Status Encapsulator::Subgraph::MakeSequencingNode( + const std::string& subgraph_name, Graph* graph_out) { + if (sequencer_ == nullptr) { + NodeDef seq_def; + // TODO(shikharagarwal): What source node should we use for errors? + NodeDefBuilder builder(absl::StrCat(subgraph_name, "_sequencer"), "NoOp"); + builder.Attr(kXlaHostTransferSequencerAttr, subgraph_name); + builder.Device(device_); + absl::Status s = builder.Finalize(&seq_def); + if (!s.ok()) return s; + + TF_ASSIGN_OR_RETURN(sequencer_, graph_out->AddNode(seq_def)); + } + return absl::OkStatus(); +} + +void Encapsulator::Subgraph::ConnectSequencerToCallNode(Graph* graph_out) { + if (sequencer_ != nullptr) { + VLOG(2) << "ConnectSequencerToCallNode"; + graph_out->AddControlEdge(sequencer_, call_node_, + /* allow_duplicates= */ true); + } +} + +absl::Status Encapsulator::Subgraph::BuildFunctionDef( + const std::string& name_in, const RewriteSubgraphFn& rewrite_subgraph_fn, + bool reuse_existing_functions, FunctionLibraryDefinition* library) { + // name_in is copied here because name may be modified below if + // rewrite_subgraph_fn is true. + std::string name = name_in; + call_node_def_.set_op(name); + call_node_def_.set_name(name); + call_node_def_.set_device(device_); + + if (rewrite_subgraph_fn) { + std::vector arg_source_tensors(args_by_src_.size()); + for (const auto& arg : args_by_src_) { + arg_source_tensors.at(arg.second) = arg.first; + } + // Initialize the input and output permutations to the identity. + std::vector input_permutation(args_by_src_.size()); + std::iota(input_permutation.begin(), input_permutation.end(), 0); + std::vector output_permutation(results_.size()); + std::iota(output_permutation.begin(), output_permutation.end(), 0); + + TF_RETURN_IF_ERROR( + rewrite_subgraph_fn(arg_source_tensors, &graph_, &input_permutation, + &output_permutation, &call_node_def_)); + + // Apply the input/output permutations to the 'args_by_...' and 'results_' + // mappings, so when we build edges in BuildOutputGraph() we + // connect them to the right input/output positions. + if (input_permutation.size() != args_by_src_.size()) { + return absl::InvalidArgumentError( + "Input permutation has incorrect size."); + } + if (output_permutation.size() != results_.size()) { + return absl::InvalidArgumentError( + "Output permutation has incorrect size."); + } + for (auto& arg : args_by_src_) { + arg.second = input_permutation[arg.second]; + } + for (auto& arg : args_by_dst_) { + arg.second = input_permutation[arg.second]; + } + for (auto& result : results_) { + result.second = output_permutation[result.second]; + } + + name = call_node_def_.op(); + } + + function_def_name_ = name; + + FunctionDef fdef; + auto lookup = [this](const Node* node) -> std::optional { + if (control_output_nodes_.contains(node->name())) { + return std::make_optional(node->name()); + } + return std::nullopt; + }; + // Verify that the graph has well-formed control flow structure. + std::vector dummy; + TF_RETURN_IF_ERROR(BuildControlFlowInfo(graph_.get(), &dummy)); + TF_RETURN_IF_ERROR(GraphToFunctionDef(*graph_, name, lookup, &fdef)); + + if (VLOG_IS_ON(1)) { + VLOG(2) << "Build function def " << name; + DumpGraphToFile(absl::StrCat("encapsulate_fdef_graph_", name), *graph_, + library); + DumpFunctionDefToFile(absl::StrCat("encapsulate_fdef_", name), fdef); + } + + const FunctionDef* original_fdef = library->Find(name); + if (!reuse_existing_functions || original_fdef == nullptr) { + TF_RETURN_IF_ERROR(library->AddFunctionDef(fdef)); + } else if (!FunctionDefsEqual(*original_fdef, fdef)) { + TF_RETURN_IF_ERROR(library->ReplaceFunction(name, fdef)); + } + return absl::OkStatus(); +} + +absl::Status Encapsulator::Subgraph::ReplaceFunctionDef( + FunctionLibraryDefinition* library) { + const std::string& name = function_def_name_; + + FunctionDef fdef; + TF_RETURN_IF_ERROR(GraphToFunctionDef(*graph_, name, &fdef)); + + if (VLOG_IS_ON(1)) { + VLOG(2) << "Replace function def " << name; + DumpGraphToFile(absl::StrCat("replace_encapsulate_fdef_graph_", name), + *graph_, library); + DumpFunctionDefToFile(absl::StrCat("replace_encapsulate_fdef_", name), + fdef); + } + + TF_RETURN_IF_ERROR(library->ReplaceFunction(name, fdef)); + return absl::OkStatus(); +} + +absl::Status Encapsulator::Subgraph::AddFunctionCallNode( + const absl::flat_hash_map& node_images, + Graph* graph_out) { + TF_ASSIGN_OR_RETURN(call_node_, graph_out->AddNode(call_node_def_)); + + // Copy the assigned device and the key_annotation over. + call_node_->set_assigned_device_name(device_); + + return absl::OkStatus(); +} + +absl::Status Encapsulator::GetFunctionNameAttr(Node const* node, + std::string* attr) const { + AttrSlice attrs = node->attrs(); + attr->clear(); + for (const auto& node_attr : attrs) { + if (node_attr.first == group_attribute_) { + TF_RETURN_IF_ERROR(AttrValueHasType(node_attr.second, "string")); + *attr = node_attr.second.s(); + break; + } + } + return absl::OkStatus(); +} + +bool IsInSubgraph(const std::string& func_id) { return !func_id.empty(); } + +absl::Status Encapsulator::CopySubgraphNodes( + absl::flat_hash_map* node_images) { + for (Node* node : graph_in_->op_nodes()) { + std::string func_id; + TF_RETURN_IF_ERROR(GetFunctionNameAttr(node, &func_id)); + if (!IsInSubgraph(func_id)) continue; + + Subgraph& subgraph = subgraphs_[func_id]; + Node* image = subgraph.MakeNodeImage(graph_in_, node); + image->ClearAttr(group_attribute_); + (*node_images)[node] = image; + } + return absl::OkStatus(); +} + +absl::Status Encapsulator::CopySubgraphEdges( + const absl::flat_hash_map& node_images, + std::vector>* src_arg_pairs) { + for (const Edge* edge : graph_in_->edges()) { + std::string src_func_id; + TF_RETURN_IF_ERROR(GetFunctionNameAttr(edge->src(), &src_func_id)); + std::string dst_func_id; + TF_RETURN_IF_ERROR(GetFunctionNameAttr(edge->dst(), &dst_func_id)); + Node* src_image = gtl::FindWithDefault(node_images, edge->src(), nullptr); + Node* dst_image = gtl::FindWithDefault(node_images, edge->dst(), nullptr); + + // Copy edges that are local to a subgraph. + if (IsInSubgraph(src_func_id) && IsInSubgraph(dst_func_id) && + src_func_id == dst_func_id) { + Graph* g = subgraphs_[src_func_id].GetGraph(); + if (edge->IsControlEdge()) { + g->AddControlEdge(src_image, dst_image, + /* allow_duplicates= */ true); + } else { + g->AddEdge(src_image, edge->src_output(), dst_image, edge->dst_input()); + } + continue; + } + + // Record 'src' as an output of its subgraph, if applicable. + if (IsInSubgraph(src_func_id)) { + if (!edge->IsControlEdge()) { + DataType dtype = edge->src()->output_type(edge->src_output()); + if (IsRefType(dtype)) { + return absl::InvalidArgumentError(absl::StrCat( + "Ref Tensors (e.g., Variables) are not supported as results: " + "tensor ", + edge->src()->name(), ":", edge->src_output())); + } + } + + Subgraph& src_subgraph = subgraphs_[src_func_id]; + if (edge->IsControlEdge()) { + TF_RETURN_IF_ERROR(src_subgraph.RecordControlResult(edge, node_images)); + } else { + TF_RETURN_IF_ERROR(src_subgraph.RecordResult(edge, node_images)); + } + } + + // Record 'dst' as an input of its subgraph, if applicable. + if (IsInSubgraph(dst_func_id)) { + // Look at the type of the destination not the source, since Ref output + // Tensors can be automatically cast to non-Ref Tensors at the + // destination. + if (!edge->IsControlEdge()) { + DataType dtype = edge->dst()->input_type(edge->dst_input()); + if (IsRefType(dtype)) { + return absl::InvalidArgumentError(absl::StrCat( + "Ref Tensors (e.g., Variables) are not supported as args: " + "tensor ", + edge->src()->name(), ":", edge->src_output())); + } + } + + Subgraph& dst_subgraph = subgraphs_[dst_func_id]; + // Ignore control edges entering the subgraph. We will lift them onto + // the enclosing call operators in BuildOutputGraph(). + if (!edge->IsControlEdge()) { + TF_RETURN_IF_ERROR( + dst_subgraph.RecordArg(edge, node_images, src_arg_pairs)); + } + } + } + return absl::OkStatus(); +} + +absl::Status Encapsulator::SplitIntoSubgraphs( + FunctionLibraryDefinition* library) { + absl::Status s; + + // Map from input graph nodes to subgraph nodes. + absl::flat_hash_map node_images; + + // Each entry of src_arg_pairs is a pair whose first element is a node in the + // original graph that has an output edge in the subgraph, and whose second + // element is the arg node in the subgraph that it sends to. The vector will + // be filled in below in AddArgs. + std::vector> src_arg_pairs; + + TF_RETURN_IF_ERROR(CopySubgraphNodes(&node_images)); + TF_RETURN_IF_ERROR(CopySubgraphEdges(node_images, &src_arg_pairs)); + MarkGuaranteedConstants(*graph_in_, src_arg_pairs); + + for (auto& entry : subgraphs_) { + Subgraph& subgraph = entry.second; + FixupSourceAndSinkEdges(subgraph.GetGraph()); + } + + if (VLOG_IS_ON(1)) { + // Dump subgraphs. + for (auto& entry : subgraphs_) { + DumpGraphToFile( + absl::StrCat("encapsulate_subgraphs_subgraph_", entry.first), + *entry.second.GetGraph(), library); + } + } + + return s; +} + +absl::Status Encapsulator::BuildFunctionDefs( + const RewriteSubgraphFn& rewrite_subgraph_fn, bool reuse_existing_functions, + FunctionLibraryDefinition* library) { + for (auto& subgraph_entry : subgraphs_) { + std::string name = subgraph_entry.first; + Subgraph& subgraph = subgraph_entry.second; + TF_RETURN_IF_ERROR(subgraph.BuildFunctionDef( + name, rewrite_subgraph_fn, reuse_existing_functions, library)); + } + return absl::OkStatus(); +} + +absl::Status Encapsulator::CopyNodesToOutputGraph( + Graph* graph_out, absl::flat_hash_map* node_images) { + for (Node* node : graph_in_->op_nodes()) { + std::string func_id; + TF_RETURN_IF_ERROR(GetFunctionNameAttr(node, &func_id)); + + // Don't copy nodes that are going to be encapsulated. + if (IsInSubgraph(func_id)) continue; + + Node* image = graph_out->CopyNode(node); + (*node_images)[node] = image; + } + (*node_images)[graph_in_->source_node()] = graph_out->source_node(); + (*node_images)[graph_in_->sink_node()] = graph_out->sink_node(); + return absl::OkStatus(); +} + +absl::Status Encapsulator::AddFunctionCallNodes( + const absl::flat_hash_map& node_images, + Graph* graph_out) { + for (auto& subgraph_entry : subgraphs_) { + TF_RETURN_IF_ERROR( + subgraph_entry.second.AddFunctionCallNode(node_images, graph_out)); + } + return absl::OkStatus(); +} + +absl::Status Encapsulator::FindOutputImageOfEdgeSrc( + const std::string& src_func_id, const std::string& dst_func_id, + const absl::flat_hash_map& node_images, + const Node* original_src_node, Node** src_image) { + if (IsInSubgraph(src_func_id)) { + // The edge is from a subgraph to a regular node in the output graph so + // use the subgraph's call node output. + *src_image = subgraphs_.at(src_func_id).GetCallNode(); + } else { + // The source of the edge is in the output graph so use the node image in + // the output graph. + *src_image = node_images.at(original_src_node); + } + return absl::OkStatus(); +} + +int Encapsulator::FindOutputSlotOfEdgeSrc(const std::string& src_func_id, + const std::string& dst_func_id, + const Edge* edge) { + if (IsInSubgraph(src_func_id)) { + const Subgraph& src_subgraph = subgraphs_.at(src_func_id); + // 'src' is in a subgraph and 'dst' is a regular node in the output + // graph. Use the corresponding call output instead. + return src_subgraph.GetResultIndexForEdge(edge); + } else { + // The source of the edge is in the output graph so use the regular edge + // slot. + return edge->src_output(); + } +} + +absl::Status Encapsulator::FindOutputImageOfEdgeDst( + const std::string& src_func_id, const std::string& dst_func_id, + const absl::flat_hash_map& node_images, + const Node* original_dst_node, Node** dst_image) { + if (IsInSubgraph(dst_func_id)) { + // The edge is to a subgraph from a regular node in the output graph so + // use the subgraph's call node input. + *dst_image = subgraphs_.at(dst_func_id).GetCallNode(); + } else { + // The destination of the edge is in the output graph so use the node image + // in the output graph. + *dst_image = node_images.at(original_dst_node); + } + return absl::OkStatus(); +} + +int Encapsulator::FindOutputSlotOfEdgeDst(const std::string& src_func_id, + const std::string& dst_func_id, + const Edge* edge) { + if (IsInSubgraph(dst_func_id)) { + const Subgraph& dst_subgraph = subgraphs_.at(dst_func_id); + // 'dst' is in a subgraph and 'src' is a regular node in the output + // graph. Use the corresponding call input instead. + return dst_subgraph.GetArgIndexForEdge(edge); + } else { + // The destination of the edge is in the output graph so use the regular + // edge slot. + return edge->dst_input(); + } +} + +absl::Status Encapsulator::CopyEdgeToOutputGraph( + const Edge* edge, const std::string& src_func_id, + const std::string& dst_func_id, + const absl::flat_hash_map& node_images, + Graph* graph_out, + absl::flat_hash_set, + OutputInputTensorPairHasher>* edges_added) { + Node* src_image; + TF_RETURN_IF_ERROR(FindOutputImageOfEdgeSrc( + src_func_id, dst_func_id, node_images, edge->src(), &src_image)); + Node* dst_image; + TF_RETURN_IF_ERROR(FindOutputImageOfEdgeDst( + src_func_id, dst_func_id, node_images, edge->dst(), &dst_image)); + + // If this is a control edge then copy it and return. Lift control edges onto + // the enclosing call operator. + if (edge->IsControlEdge()) { + // Add the control edge, if we have not already added it, using the images + // determined above (potentially call operators or RecvAtHost/SendFromHost). + if (edges_added + ->emplace(OutputTensor(src_image, -1), InputTensor(dst_image, -1)) + .second) { + graph_out->AddControlEdge(src_image, dst_image, + /* allow_duplicates= */ true); + } + + return absl::OkStatus(); + } + + int src_output = FindOutputSlotOfEdgeSrc(src_func_id, dst_func_id, edge); + + int dst_input = FindOutputSlotOfEdgeDst(src_func_id, dst_func_id, edge); + + // Add the edge, if we have not already added it. + if (edges_added + ->emplace(OutputTensor(src_image, src_output), + InputTensor(dst_image, dst_input)) + .second) { + graph_out->AddEdge(src_image, src_output, dst_image, dst_input); + } + return absl::OkStatus(); +} + +absl::Status Encapsulator::AddEdgesToOutputGraph( + const absl::flat_hash_map& node_images, + Graph* graph_out) { + // Set of edges already added to the output graph, represented as (src, dst) + // pairs. We use the set to deduplicate edges; multiple edges in the input + // graph may map to one edge in the output graph. + absl::flat_hash_set, + OutputInputTensorPairHasher> + edges_added; + + for (const Edge* edge : graph_in_->edges()) { + std::string src_func_id; + TF_RETURN_IF_ERROR(GetFunctionNameAttr(edge->src(), &src_func_id)); + std::string dst_func_id; + TF_RETURN_IF_ERROR(GetFunctionNameAttr(edge->dst(), &dst_func_id)); + + // Ignore edges that are strictly contained within one subgraph, unless + // we are constructing parallel check graphs. + if (IsInSubgraph(src_func_id) && IsInSubgraph(dst_func_id) && + src_func_id == dst_func_id) { + continue; + } + + // We have an edge that crosses a cluster boundary or is entirely within the + // unclustered graph. + TF_RETURN_IF_ERROR(CopyEdgeToOutputGraph( + edge, src_func_id, dst_func_id, node_images, graph_out, &edges_added)); + } + + for (auto& subgraph_entry : subgraphs_) { + Subgraph& subgraph = subgraph_entry.second; + subgraph.ConnectSequencerToCallNode(graph_out); + } + + return absl::OkStatus(); +} + +namespace { + +// Adds a dummy Const node to graph_out. The "constant" has the type of +// data_type and the shape indicated in 'shape'. The dummy node is not a valid +// Const node because it does not have any value defined, but this doesn't +// matter because it will only be used subsequently for shape inference. (It +// would be possible to add a switch statement over data_type to create a value +// for the constant, but that would entail maintaining the logic as new types +// are added, and is not necessary.) If the node being replaced was within a +// control flow frame, adds appropriate Enter nodes so that the use of the Const +// is well-formed. +Node* AddDummyShapedNode(const Node* src_node, int src_port, + const std::vector& control_flow_info, + const TensorShapeProto& shape, Graph* graph_out) { + DataType data_type = src_node->output_type(src_port); + TensorProto dummy_proto; + dummy_proto.set_dtype(data_type); + *dummy_proto.mutable_tensor_shape() = shape; + // Don't set any value field in the proto, since it is only going to be used + // for shape inference. + + GraphDefBuilder::Options options(graph_out, /*status=*/nullptr); + NodeBuilder node_builder(options.GetNameForOp("KnownShape"), "Const", + options.op_registry()); + node_builder.Attr("dtype", data_type).Attr("value", dummy_proto); + Node* node = options.FinalizeBuilder(&node_builder); + // Add any Enter nodes required to bring the constant to the correct control + // flow frame. + while (!control_flow_info[src_node->id()].frame_name.empty()) { + NodeDebugInfo debug_info(*src_node); + NodeBuilder enter_builder(options.GetNameForOp("Enter"), "Enter", + options.op_registry(), &debug_info); + enter_builder.Attr("frame_name", + control_flow_info[src_node->id()].frame_name); + enter_builder.Attr("is_constant", true); + enter_builder.Input(node, 0); + Node* enter_node = options.FinalizeBuilder(&enter_builder); + // Adopt the new Enter node as the value in the current frame. + node = enter_node; + // Recurse to the parent frame to see if more Enter nodes need to be added. + src_node = control_flow_info[src_node->id()].parent_frame; + } + return node; +} + +} // namespace + +absl::Status Encapsulator::MakePrunedGraphCopyAndInline( + const Graph& graph, const std::vector& sink_nodes, + std::unique_ptr* pruned_graph, + absl::flat_hash_map* node_images, + FunctionLibraryDefinition* library) { + // First copy all ancestor nodes of sink_nodes into a new graph. + pruned_graph->reset(new Graph(library)); + (*pruned_graph)->set_versions(graph.versions()); + ReverseDFSFrom(graph, sink_nodes, + /*enter=*/nullptr, + /*leave=*/[&](Node* n) { + if (!n->IsSource()) { + Node* copied = (*pruned_graph)->CopyNode(n); + node_images->emplace(n, copied); + } + }); + + // Add all the edges between copied nodes. + for (auto entry : *node_images) { + const Node* orig = entry.first; + Node* image = entry.second; + for (const Edge* out_edge : orig->out_edges()) { + auto iter = node_images->find(out_edge->dst()); + if (iter != node_images->end()) { + // The source and destination are both in the copied graph. + (*pruned_graph) + ->AddEdge(image, out_edge->src_output(), iter->second, + out_edge->dst_input()); + } + } + } + + // Find all the function call nodes, and inline them. + std::vector function_nodes; + for (auto node : (*pruned_graph)->nodes()) { + const OpRegistrationData* op_reg_data; + TF_RETURN_IF_ERROR(library->LookUp(node->type_string(), &op_reg_data)); + if (op_reg_data->is_function_op) { + function_nodes.push_back(node); + } + } + for (auto node : function_nodes) { + VLOG(2) << "Inlining function " << node->name(); + const FunctionDef* fdef = library->Find(node->type_string()); + if (fdef == nullptr) { + return absl::InternalError(absl::StrCat("Failed to find function ", + node->type_string(), + " in function library.")); + } + std::unique_ptr fbody; + TF_RETURN_IF_ERROR( + FunctionDefToBodyHelper(*fdef, node->attrs(), library, &fbody)); + + InlineFunctionBodyOptions inline_opts; + TF_RETURN_IF_ERROR(InlineFunctionBody(*library, pruned_graph->get(), node, + fbody.get(), inline_opts)); + } + + return absl::OkStatus(); +} + +absl::Status Encapsulator::BuildOutputGraph( + Graph* graph_out, FunctionLibraryDefinition* library) { + // Map from nodes in the input graph to nodes in the output graph. + absl::flat_hash_map node_images; + + TF_RETURN_IF_ERROR(CopyNodesToOutputGraph(graph_out, &node_images)); + TF_RETURN_IF_ERROR(AddFunctionCallNodes(node_images, graph_out)); + TF_RETURN_IF_ERROR(AddEdgesToOutputGraph(node_images, graph_out)); + + return absl::OkStatus(); +} + +} // anonymous namespace + +absl::Status EncapsulateSubgraphsInFunctions( + std::string group_attribute, const Graph& graph_in, + const RewriteSubgraphFn& rewrite_subgraph_fn, bool reuse_existing_functions, + std::unique_ptr* graph_out, FunctionLibraryDefinition* library) { + Encapsulator encapsulator(std::move(group_attribute), + &graph_in); + TF_RETURN_IF_ERROR(encapsulator.SplitIntoSubgraphs(library)); + + TF_RETURN_IF_ERROR(encapsulator.BuildFunctionDefs( + rewrite_subgraph_fn, reuse_existing_functions, library)); + + std::unique_ptr out(new Graph(library)); + out->set_versions(graph_in.versions()); + TF_RETURN_IF_ERROR(encapsulator.BuildOutputGraph(out.get(), library)); + + *graph_out = std::move(out); + return absl::OkStatus(); +} + +// Finds the types of the _Arg nodes, indexed by position. +static absl::Status GetArgTypes(const Graph& graph, DataTypeVector* types) { + for (Node* n : graph.op_nodes()) { + if (n->type_string() == kArgOp) { + int index; + TF_RETURN_IF_ERROR(GetNodeAttr(n->attrs(), "index", &index)); + const int num_types = types->size(); + if (index < 0 || index >= num_types) { + return absl::InvalidArgumentError("Invalid argument number"); + } + (*types)[index] = n->output_type(0); + } + } + return absl::OkStatus(); +} + +// Renumber the indices of _Arg nodes in a graph, according to +// 'permutation' that maps old indices to new indices. +static absl::Status RenumberArguments(Graph* graph, + const std::vector& permutation) { + for (Node* n : graph->op_nodes()) { + if (n->type_string() == kArgOp) { + int index; + TF_RETURN_IF_ERROR(GetNodeAttr(n->attrs(), "index", &index)); + const int permutation_size = permutation.size(); + if (index < 0 || index >= permutation_size) { + return absl::InvalidArgumentError("Invalid argument number"); + } + n->AddAttr("index", permutation[index]); + } + } + return absl::OkStatus(); +} + +absl::Status EncapsulateSubgraphsPass::Run( + const GraphOptimizationPassOptions& options) { + VLOG(1) << "EncapsulateSubgraphsPass::Run"; + if (VLOG_IS_ON(1)) { + DumpGraphToFile("encapsulate_subgraphs_before", **options.graph, + options.flib_def); + } + + // TODO(b/195757077): Remove this once there is a better way to disable + // GraphOptimizationPasses that are not needed due to MLIR bridge. + for (Node* n : (*options.graph)->nodes()) { + // Skip the pass if we found TPUExecute or TPUExecuteAndUpdateVariables ops + // in the graph, which indicates the graph is produced by TPU TF-XLA bridge + // and doesn't require auto clustering. + if (n->type_string() == "TPUExecute" || + n->type_string() == "TPUExecuteAndUpdateVariables") { + return absl::OkStatus(); + } + } + + std::unique_ptr graph_out; + FunctionLibraryDefinition* const library = options.flib_def; + + // Constant folding below might need to run part of the function to compute + // constants. Create an FunctionLibraryRuntime with a single CPU device + // that can run the part of the function. + // NOTE: If this turns out to be slow, we can cache the FLRs keyed by + // `options`. + SessionOptions session_options; + auto* device_count = session_options.config.mutable_device_count(); + device_count->insert({"CPU", 1}); + std::vector> devices; + + DeviceFactory* cpu_factory = DeviceFactory::GetFactory("CPU"); + if (!cpu_factory) { + return absl::NotFoundError( + "CPU Factory not registered. Can't run EncapsulateSubgraphsPass"); + } + TF_RETURN_IF_ERROR(cpu_factory->CreateDevices( + session_options, "/job:localhost/replica:0/task:0", &devices)); + if (devices.empty()) { + return absl::NotFoundError( + "Failed to create a CPU device for EncapsulateSubgraphsPass"); + } + + std::unique_ptr device_mgr = + std::make_unique(std::move(devices)); + const auto* config = &options.session_options->config; + std::unique_ptr pflr( + new ProcessFunctionLibraryRuntime( + device_mgr.get(), options.session_options->env, + /*config=*/config, TF_GRAPH_DEF_VERSION, library, + config->graph_options().optimizer_options())); + FunctionLibraryRuntime* flr = + pflr->GetFLR("/job:localhost/replica:0/task:0/device:CPU:0"); + if (flr == nullptr) { + return absl::InternalError( + "Failed to create and retrieve function library runtime to run " + "constant folding"); + } + + auto rewrite_subgraph = + [flr](const std::vector& arg_source_tensors, + std::unique_ptr* subgraph, + std::vector* input_permutation, + std::vector* output_permutation, NodeDef* node) { + // Optimize the subgraph. + // Do not constant fold nodes that output DT_VARIANT type tensors. + // XLA does not support Const nodes of Variant type since it needs + // to know the original ops to be able to compile them to the relevant + // XLA form. + // TODO(srbs): This filter is a little conservative. E.g. a subgraph of + // the form: + // Const + // | + // EmptyTensorList -> TensorListPushBack -> TensorListPopBack -> Op + // | + // (Discard popped list) + // + // Would have been reduced to "Const -> Op" without this filter. + // However since we are only allowed to specify the filter at the "Node" + // level there is no good way to allow the above behavior. So we + // disallow any sort of constant folding on Variant nodes for now. + bool disable_constant_folding = + GetBuildXlaOpsPassFlags()->tf_xla_disable_constant_folding; + auto cf_consider_fn = [disable_constant_folding](const Node* n) { + if (disable_constant_folding) return false; + for (const auto& output_arg : n->op_def().output_arg()) { + if (output_arg.type() == DT_VARIANT) { + return false; + } + } + return true; + }; + GraphOptimizer::Options graph_optimizer_options; + graph_optimizer_options.cf_consider_fn = cf_consider_fn; + OptimizeGraph(flr, subgraph, graph_optimizer_options); + + const int num_args = input_permutation->size(); + std::vector const_args(num_args); + TF_RETURN_IF_ERROR( + BackwardsConstAnalysis(**subgraph, &const_args, + /*compile_time_const_nodes=*/nullptr, flr)); + + DataTypeVector arg_types(num_args); + TF_RETURN_IF_ERROR(GetArgTypes(**subgraph, &arg_types)); + + // Compute a permutation of the arguments such that the constant + // arguments are first. + const int num_consts = + std::count(const_args.begin(), const_args.end(), true); + + const int num_resources = + std::count(arg_types.begin(), arg_types.end(), DT_RESOURCE); + const int num_nonconsts = num_args - num_resources - num_consts; + if (num_nonconsts < 0) { + return absl::InternalError(absl::StrCat( + "num_nonconsts should be >= 0, was ", num_nonconsts)); + } + + int const_pos = 0; + int arg_pos = num_consts; + int resource_pos = num_consts + num_nonconsts; + for (int i = 0; i < num_args; ++i) { + if (const_args[i]) { + if (arg_types[i] == DT_RESOURCE) { + return absl::InternalError(absl::StrCat( + "Resource arguments cannot be constant (argument ", i, ")")); + } + (*input_permutation)[i] = const_pos; + ++const_pos; + } else if (arg_types[i] == DT_RESOURCE) { + (*input_permutation)[i] = resource_pos; + ++resource_pos; + } else { + (*input_permutation)[i] = arg_pos; + ++arg_pos; + } + } + + // Renumber argument nodes in the graph. + TF_RETURN_IF_ERROR( + RenumberArguments(subgraph->get(), *input_permutation)); + + // TODO(phawkins): add a forward is-constant analysis, similarly split + // outputs into host-memory constants and device-memory non-constants. + + AddNodeAttr(kXlaCompiledKernelAttr, true, node); + AddNodeAttr(kXlaNumConstantArgsAttr, num_consts, node); + AddNodeAttr(kXlaNumResourceArgsAttr, num_resources, node); + return absl::OkStatus(); + }; + + TF_RETURN_WITH_CONTEXT_IF_ERROR( + EncapsulateSubgraphsInFunctions( + kXlaClusterAttr, **options.graph, rewrite_subgraph, + /*reuse_existing_functions=*/false, &graph_out, library), + "EncapsulateSubgraphsPass failed"); + if (VLOG_IS_ON(1)) { + DumpGraphToFile("encapsulate_subgraphs_after", *graph_out, + options.flib_def); + } + + *options.graph = std::move(graph_out); + + TF_ASSIGN_OR_RETURN(absl::flat_hash_set ref_related_nodes, + GetNodesRelatedToRefVariables(**options.graph, flr)); + for (Node* node : (*options.graph)->nodes()) { + bool has_ref_vars = ref_related_nodes.contains(node); + node->AddAttr(kXlaHasReferenceVarsAttr, has_ref_vars); + VLOG(3) << "Has ref vars = " << has_ref_vars + << ", node: " << node->def().DebugString(); + } + return absl::OkStatus(); +} + +bool IsXlaCompiledKernel(const Node& node) { + bool is_compiled = false; + bool has_compilation_attr = + TryGetNodeAttr(node.attrs(), kXlaCompiledKernelAttr, &is_compiled) && + is_compiled; + return has_compilation_attr ? is_compiled : false; +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/encapsulate_subgraphs_pass.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/encapsulate_subgraphs_pass.h new file mode 100644 index 0000000000000000000000000000000000000000..ca8940b0098904dd5813e7e13b24e930f09f5726 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/encapsulate_subgraphs_pass.h @@ -0,0 +1,108 @@ +/* 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. +==============================================================================*/ + +// An optimization pass that groups nodes marked with a common +// kXlaClusterAttr into functions, and replaces the original nodes by +// calls. The calls are annotated with kXlaCompiledKernelAttr. + +#ifndef TENSORFLOW_COMPILER_JIT_ENCAPSULATE_SUBGRAPHS_PASS_H_ +#define TENSORFLOW_COMPILER_JIT_ENCAPSULATE_SUBGRAPHS_PASS_H_ + +#include "tensorflow/core/common_runtime/optimization_registry.h" +#include "tensorflow/core/framework/function.h" +#include "tensorflow/core/graph/graph.h" +#include "tensorflow/core/lib/core/status.h" + +namespace tensorflow { + +// EncapsulateSubgraphs pass takes all the nodes with the same cluster ID +// (derived from kXlaClusterAttr=ID (kXlaClusterAttr) attribute), puts them into +// a TF function, and replaces the subgraph in the main graph with a call to +// that TF function annotated with kXlaCompiledKernelAttr (_XlaCompiledKernel). +class EncapsulateSubgraphsPass : public GraphOptimizationPass { + public: + absl::Status Run(const GraphOptimizationPassOptions& options) override; +}; + +// A rewriting function to apply to each subgraph during encapsulation. +// 'arg_source_tensors' are the tensors corresponding to the arguments in the +// original source graph (*not* 'graph'). +// +// 'graph' is the subgraph. The rewriting may renumber the inputs and outputs; +// 'input_permutation' is a mapping from old argument numbers to new argument +// numbers, whereas 'output_permutation' is the same for outputs. Both +// 'input_permutation' and 'output_permutation' are initialized to the identity +// permutation. 'nodedef' is the NodeDef for the call to the function under +// construction, provided to allow additional attributes to be set. +// The rewrite may also change the NodeDef's operator name, and that +// name will be used as the name of the generated function. +typedef std::function& arg_source_tensors, + std::unique_ptr* graph, std::vector* input_permutation, + std::vector* output_permutation, NodeDef* node_def)> + RewriteSubgraphFn; + +// Transformation that finds subgraphs whose nodes are marked with +// 'group_attribute', splits those subgraphs into functions, and replaces +// the originals with function calls. +// +// 'group_attribute' must be a string valued-attribute that names the new +// functions to introduce. +// +// If 'rewrite_subgraph_fn' is set, it is applied to each subgraph before +// function conversion. +// +// If 'reuse_existing_functions' is set, use an existing function with the +// same name, if any. +// +// TODO(phawkins): currently, some information in control edges +// is not preserved. Suppose you have A and B in the main +// graph, C and D in a subgraph. B and C have control deps from A, D has control +// dep from B. Originally D must run after C, post-transformation this +// dependency is lost. +absl::Status EncapsulateSubgraphsInFunctions( + std::string group_attribute, const Graph& graph_in, + const RewriteSubgraphFn& rewrite_subgraph_fn, bool reuse_existing_functions, + std::unique_ptr* graph_out, FunctionLibraryDefinition* library); + +// The attribute that marks function calls produced by the encapsulate +// subgraphs pass and that should in turn be compiled via XlaLaunch operators. +extern const char* const kXlaCompiledKernelAttr; + +// Does `node` have the kXlaCompiledKernelAttr attribute? +bool IsXlaCompiledKernel(const Node& node); + +// Functions produced by the EncapsulateSubgraphs pass have their arguments in +// the order: +// 1) compile-time constant arguments, in host memory, +// 2) other arguments, in device memory. +// 3) resource variable arguments, in host memory. Note that only the resource +// Tensor itself is in host memory; the underlying value may be in device +// memory. +// The functions are annotated with the following attributes that describe how +// many constant and resource arguments there are: + +// Name of the attribute containing the number of constant arguments. +extern const char* const kXlaNumConstantArgsAttr; + +// Name of the attribute containing the number of resource variable arguments. +extern const char* const kXlaNumResourceArgsAttr; + +// Name of the attribute defining whether the cluster has reference variables. +extern const char* const kXlaHasReferenceVarsAttr; + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_ENCAPSULATE_SUBGRAPHS_PASS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/encapsulate_subgraphs_pass_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/encapsulate_subgraphs_pass_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..26c26eec1866821a68d2f88dd1c87952af822698 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/encapsulate_subgraphs_pass_test.cc @@ -0,0 +1,2788 @@ +/* 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/compiler/jit/encapsulate_subgraphs_pass.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "absl/log/check.h" +#include "absl/status/status.h" +#include "absl/strings/match.h" +#include "absl/strings/str_cat.h" +#include "absl/types/span.h" +#include "tensorflow/cc/framework/ops.h" +#include "tensorflow/cc/framework/scope.h" +#include "tensorflow/cc/ops/array_ops.h" +#include "tensorflow/cc/ops/const_op.h" +#include "tensorflow/cc/ops/math_ops.h" +#include "tensorflow/cc/ops/state_ops.h" +#include "tensorflow/compiler/jit/encapsulate_util.h" +#include "tensorflow/compiler/jit/extract_outside_compilation_pass.h" +#include "tensorflow/compiler/jit/test_util.h" +#include "tensorflow/compiler/tf2xla/side_effect_util.h" +#include "xla/tsl/lib/core/status_test_util.h" +#include "xla/tsl/platform/errors.h" +#include "tensorflow/core/common_runtime/device_mgr.h" +#include "tensorflow/core/common_runtime/graph_constructor.h" +#include "tensorflow/core/common_runtime/optimization_registry.h" +#include "tensorflow/core/framework/common_shape_fns.h" +#include "tensorflow/core/framework/device.h" +#include "tensorflow/core/framework/device_factory.h" +#include "tensorflow/core/framework/function.h" +#include "tensorflow/core/framework/function_testlib.h" +#include "tensorflow/core/framework/graph_to_functiondef.h" +#include "tensorflow/core/framework/node_def_util.h" +#include "tensorflow/core/framework/op.h" +#include "tensorflow/core/framework/shape_inference.h" +#include "tensorflow/core/framework/tensor_shape.h" +#include "tensorflow/core/graph/graph.h" +#include "tensorflow/core/graph/graph_def_builder.h" +#include "tensorflow/core/graph/node_builder.h" +#include "tensorflow/core/lib/core/errors.h" +#include "tensorflow/core/platform/test.h" +#include "tensorflow/core/platform/types.h" +#include "tensorflow/core/public/session_options.h" +#include "tensorflow/core/public/version.h" +#include "tensorflow/core/util/equal_graph_def.h" + +namespace tensorflow { +namespace { + +const char* const kXlaHostTransferSequencerAttr = + "_xla_host_transfer_sequencer"; + +absl::Status AddGraphDefToFunctionLibrary( + const GraphDefBuilder& graphdef_builder, const std::string& name_suffix, + FunctionDefLibrary* library) { + GraphDef graphdef; + TF_RETURN_IF_ERROR(graphdef_builder.ToGraphDef(&graphdef)); + std::unique_ptr graph = + std::unique_ptr(new Graph(OpRegistry::Global())); + GraphConstructorOptions opts; + opts.allow_internal_ops = true; + TF_RETURN_IF_ERROR(ConvertGraphDefToGraph(opts, graphdef, graph.get())); + FunctionDef* fdef = library->add_function(); + TF_RETURN_IF_ERROR(GraphToFunctionDef( + *graph, + absl::StrCat("_outside_compilation_shape_inference_", name_suffix), + fdef)); + return absl::OkStatus(); +} + +template +bool EqualProtoMap( + const ::tensorflow::protobuf::Map& a, + const ::tensorflow::protobuf::Map& b, + const std::function& key_to_string, + const std::function& value_to_string, + const std::function& + compare, + const std::string& map_name, std::string* diff) { + for (const auto& elt_a : a) { + const auto iter = b.find(elt_a.first); + if (iter == b.end()) { + if (diff) { + *diff = absl::StrCat(map_name, " expected: contains element with key '", + key_to_string(elt_a.first), + "' got: map has no such element"); + } + return false; + } + if (!compare(elt_a.first, elt_a.second, iter->second)) { + if (diff) { + *diff = absl::StrCat(map_name, " expected: element with key '", + key_to_string(elt_a.first), "' has value '", + value_to_string(elt_a.second), "' got: '", + value_to_string(iter->second), "'"); + } + return false; + } + } + for (const auto& elt_b : b) { + const auto iter = a.find(elt_b.first); + if (iter == a.end()) { + if (diff) { + *diff = absl::StrCat(map_name, " got: contains element with key '", + key_to_string(elt_b.first), + "' expected: map has no such element"); + } + return false; + } + } + return true; +} + +bool EqualFunctionNodeDef(const NodeDef& a, const NodeDef& b, + const std::string& diff_preamble, std::string* diff) { + if (a.op() != b.op()) { + if (diff) { + *diff = absl::StrCat(diff_preamble, " mismatch for node ", a.name(), + ", expected op '", a.op(), "' got '", b.op()); + } + return false; + } + if (a.device() != b.device()) { + if (diff) { + *diff = absl::StrCat(diff_preamble, " mismatch for node ", a.name(), + ", expected device '", a.device(), "' got '", + b.device()); + } + return false; + } + if (a.input_size() != b.input_size()) { + if (diff) { + *diff = absl::StrCat(diff_preamble, " mismatch for node ", a.name(), + ", expected ", a.input_size(), " inputs got ", + b.input_size(), " expected:\n", a.DebugString(), + "\ngot:\n", b.DebugString()); + } + return false; + } + std::unordered_set control_input_a; + std::unordered_set control_input_b; + for (int i = 0; i < a.input_size(); ++i) { + if (absl::StartsWith(a.input(i), "^")) { + if (!absl::StartsWith(b.input(i), "^")) { + if (diff) { + *diff = absl::StrCat(diff_preamble, " mismatch for node ", a.name(), + " input ", i, ", expected control input ", + a.input(i), " got ", b.input(i), " expected:\n", + a.DebugString(), "\ngot:\n", b.DebugString()); + } + return false; + } + control_input_a.insert(a.input(i)); + control_input_b.insert(b.input(i)); + } else if (a.input(i) != b.input(i)) { + if (diff) { + *diff = absl::StrCat(diff_preamble, " mismatch for node ", a.name(), + " input ", i, ", expected ", a.input(i), " got ", + b.input(i), " expected:\n", a.DebugString(), + "\ngot:\n", b.DebugString()); + } + return false; + } + } + if (control_input_a != control_input_b) { + if (diff) { + *diff = absl::StrCat(diff_preamble, " mismatch for node ", a.name(), + " control inputs differ expected:\n", + a.DebugString(), "\ngot:\n", b.DebugString()); + } + return false; + } + return EqualProtoMap( + a.attr(), b.attr(), [](const std::string& s) { return s; }, + [](const AttrValue& v) { return v.DebugString(); }, + [](const std::string& key, const AttrValue& av, const AttrValue& bv) { + if (key == "ancestors") { + // The ancestors are added from a set so the order is unpredictable; + // just compare set equality not list equality. + std::unordered_set a_set(av.list().s().begin(), + av.list().s().end()); + std::unordered_set b_set(bv.list().s().begin(), + bv.list().s().end()); + return a_set == b_set; + } else { + return av.DebugString() == bv.DebugString(); + } + }, + absl::StrCat(diff_preamble, " attr mismatch for node ", a.name()), diff); +} + +bool EqualFunctionDef(const FunctionDef& a, const FunctionDef& b, + std::string* diff) { + if (a.signature().DebugString() != b.signature().DebugString()) { + if (diff) { + *diff = + absl::StrCat("Signature mismatch for function ", a.signature().name(), + ", expected:\n", a.signature().DebugString(), "\ngot:\n", + b.signature().DebugString()); + } + return false; + } + if (!EqualProtoMap( + a.attr(), b.attr(), [](const std::string& s) { return s; }, + [](const AttrValue& v) { return v.DebugString(); }, + [](const std::string& key, const AttrValue& av, const AttrValue& bv) { + return av.DebugString() == bv.DebugString(); + }, + absl::StrCat("attr mismatch for function ", a.signature().name()), + diff)) { + return false; + } + if (!EqualProtoMap( + a.ret(), b.ret(), [](const std::string& s) { return s; }, + [](const std::string& s) { return s; }, + [](const std::string& key, const std::string& av, + const std::string& bv) { return av == bv; }, + absl::StrCat("ret mismatch for function ", a.signature().name()), + diff)) { + return false; + } + for (int i = 0; i < a.node_def_size(); ++i) { + bool found = false; + for (int j = 0; j < b.node_def_size(); ++j) { + if (a.node_def(i).name() == b.node_def(j).name()) { + if (!EqualFunctionNodeDef( + a.node_def(i), b.node_def(j), + absl::StrCat("Function ", a.signature().name()), diff)) { + return false; + } + found = true; + break; + } + } + if (!found) { + if (diff) { + *diff = absl::StrCat("Function ", a.signature().name(), + ", expected: has node '", a.node_def(i).name(), + "' got: no node of that name"); + } + return false; + } + } + for (int i = 0; i < b.node_def_size(); ++i) { + bool found = false; + for (int j = 0; j < a.node_def_size(); ++j) { + if (b.node_def(i).name() == a.node_def(j).name()) { + found = true; + break; + } + } + if (!found) { + if (diff) { + *diff = absl::StrCat("Function ", a.signature().name(), + ", got: has node '", b.node_def(i).name(), + "' expected: no node of that name"); + } + return false; + } + } + return true; +} + +bool EqualFunctionDefLibrary(const FunctionDefLibrary& expected, + const FunctionDefLibrary& actual, + std::string* diff) { + std::unordered_map actual_index; + for (const FunctionDef& function : actual.function()) { + actual_index[function.signature().name()] = &function; + } + + for (const FunctionDef& expected_function : expected.function()) { + auto it = actual_index.find(expected_function.signature().name()); + if (it == actual_index.end()) { + if (diff) { + *diff = absl::StrCat("Did not find expected function '", + expected_function.signature().name(), "'"); + } + return false; + } + if (!EqualFunctionDef(expected_function, *it->second, diff)) return false; + actual_index.erase(it); + } + + if (!actual_index.empty()) { + if (diff != nullptr) { + *diff = + absl::StrCat("Found unexpected function '", + actual_index.begin()->second->signature().name(), "'"); + } + return false; + } + + return true; +} + +#define TF_EXPECT_FUNCTIONDEFLIBRARY_EQ(expected, actual) \ + do { \ + string diff; \ + EXPECT_TRUE(EqualFunctionDefLibrary(expected, actual, &diff)) \ + << diff << "\nActual: " << actual.DebugString(); \ + } while (false) + +REGISTER_OP("InputTest") + .Output("o: float") + .SetShapeFn([](::tensorflow::shape_inference::InferenceContext* c) { + c->set_output(0, c->UnknownShape()); + return absl::OkStatus(); + }); + +REGISTER_OP("InputTestShaped") + .Output("o: float") + .SetShapeFn([](::tensorflow::shape_inference::InferenceContext* c) { + c->set_output(0, c->Vector(2)); + return absl::OkStatus(); + }); + +REGISTER_OP("UnaryTest") + .Input("a: float") + .Output("o: float") + .SetShapeFn([](::tensorflow::shape_inference::InferenceContext* c) { + ::tensorflow::shape_inference::ShapeHandle o; + TF_RETURN_IF_ERROR(c->Merge(c->UnknownShape(), c->input(0), &o)); + c->set_output(0, o); + return absl::OkStatus(); + }); +REGISTER_OP("BinaryTest") + .Input("a: float") + .Input("b: float") + .Output("o: float") + .SetShapeFn([](::tensorflow::shape_inference::InferenceContext* c) { + ::tensorflow::shape_inference::ShapeHandle o; + TF_RETURN_IF_ERROR(c->Merge(c->UnknownShape(), c->input(0), &o)); + c->set_output(0, o); + return absl::OkStatus(); + }); +REGISTER_OP("BinaryTest2") + .Input("a: float") + .Input("b: float") + .Output("o: float") + .SetShapeFn(::tensorflow::shape_inference::UnknownShape); + +REGISTER_OP("AddNLikeTest") + .Input("inputs: N * T") + .Output("sum: T") + .Attr("N: int >= 1") + .Attr("T: numbertype") + .SetIsCommutative() + .SetIsAggregate(); + +Node* Sequencer(const GraphDefBuilder::Options& opts, + const std::string& call_node_name) { + if (opts.HaveError()) return nullptr; + NodeBuilder node_builder(opts.GetNameForOp("NoOp"), "NoOp", + opts.op_registry()); + return opts.WithAttr(kXlaHostTransferSequencerAttr, call_node_name) + .FinalizeBuilder(&node_builder); +} + +Node* Input(const GraphDefBuilder::Options& opts) { + return ops::SourceOp("InputTest", opts); +} + +Node* InputShaped(const GraphDefBuilder::Options& opts) { + return ops::SourceOp("InputTestShaped", opts); +} + +Node* KnownShapeBase(DataType dtype, absl::Span shape, + const GraphDefBuilder::Options& opts) { + if (opts.HaveError()) return nullptr; + NodeBuilder node_builder(opts.GetNameForOp("Const"), "Const", + opts.op_registry()); + TensorProto value; + value.set_dtype(dtype); + for (int dim : shape) { + value.mutable_tensor_shape()->add_dim()->set_size(dim); + } + return opts.WithAttr("value", value) + .WithAttr("dtype", dtype) + .FinalizeBuilder(&node_builder); +} + +Node* KnownShape(absl::Span shape, + const GraphDefBuilder::Options& opts) { + return KnownShapeBase(DT_FLOAT, shape, opts); +} + +Node* KeyPlaceholderShape(const GraphDefBuilder::Options& opts) { + return KnownShapeBase(DT_STRING, {2}, opts); +} + +Node* KeyPlaceholder(const std::string& call_node, + const GraphDefBuilder::Options& opts) { + if (opts.HaveError()) return nullptr; + NodeBuilder node_builder(absl::StrCat(call_node, "_key_placeholder"), + "Placeholder", opts.op_registry()); + TensorShapeProto shape; + shape.add_dim()->set_size(2); + return opts.WithAttr("shape", shape) + .WithAttr("dtype", DT_STRING) + .WithAttr("_host_compute_call_node", call_node) + .FinalizeBuilder(&node_builder); +} + +Node* RecvAtHost(ops::NodeOut key_input, const std::string& cluster, + const std::string& new_func_name, + const std::string& oc_cluster, + absl::Span dtypes, + const GraphDefBuilder::Options& opts) { + if (opts.HaveError()) return nullptr; + std::string key = absl::StrCat("host_compute_channel_", cluster, "_", + new_func_name, "_", oc_cluster); + std::string name = absl::StrCat("outside_compilation_", cluster, "_", + new_func_name, "_", oc_cluster, "_recv"); + NodeBuilder node_builder(opts.WithName(name).GetNameForOp("_XlaRecvAtHost"), + "_XlaRecvAtHost", opts.op_registry()); + node_builder.Input(std::move(key_input)); + return opts.WithAttr("Toutputs", dtypes) + .WithAttr("key", key) + .WithAttr("device_ordinal", 0) + .WithAttr("_encapsulate", cluster) + .WithAttr("_outside", oc_cluster) + .FinalizeBuilder(&node_builder); +} + +Node* SendFromHost(ops::NodeOut key_input, const std::string& cluster, + const std::string& new_func_name, + const std::string& oc_cluster, + const std::vector& inputs, + const GraphDefBuilder::Options& opts) { + if (opts.HaveError()) return nullptr; + std::string key = absl::StrCat("host_compute_channel_", cluster, "_", + new_func_name, "_", oc_cluster); + std::string name = absl::StrCat("outside_compilation_", cluster, "_", + new_func_name, "_", oc_cluster, "_send"); + NodeBuilder node_builder(opts.WithName(name).GetNameForOp("_XlaSendFromHost"), + "_XlaSendFromHost", opts.op_registry()); + node_builder.Input(inputs); + node_builder.Input(std::move(key_input)); + std::vector dtypes; + for (const auto& node : inputs) { + dtypes.push_back(node.dt); + } + return opts.WithAttr("Tinputs", dtypes) + .WithAttr("key", key) + .WithAttr("device_ordinal", 0) + .WithAttr("_encapsulate", cluster) + .WithAttr("_outside", oc_cluster) + .FinalizeBuilder(&node_builder); +} + +Node* Unary(ops::NodeOut a, const GraphDefBuilder::Options& opts) { + return ops::UnaryOp("UnaryTest", std::move(a), opts); +} + +Node* Binary(ops::NodeOut a, ops::NodeOut b, + const GraphDefBuilder::Options& opts) { + return ops::BinaryOp("BinaryTest", std::move(a), std::move(b), opts); +} + +Node* BinaryUnknownShape(ops::NodeOut a, ops::NodeOut b, + const GraphDefBuilder::Options& opts) { + return ops::BinaryOp("BinaryTest2", std::move(a), std::move(b), opts); +} + +Node* AddNLike(const std::vector& inputs, + const GraphDefBuilder::Options& opts) { + if (opts.HaveError()) return nullptr; + NodeBuilder node_builder(opts.GetNameForOp("AddN"), "AddNLikeTest", + opts.op_registry()); + node_builder.Input(inputs); + return opts.FinalizeBuilder(&node_builder); +} + +Node* ArgOp(int index, DataType type, const GraphDefBuilder::Options& opts) { + return ops::SourceOp("_Arg", + opts.WithAttr("T", type).WithAttr("index", index)); +} + +Node* RetOp(int index, ops::NodeOut a, const GraphDefBuilder::Options& opts) { + if (opts.HaveError()) return nullptr; + NodeBuilder node_builder(opts.GetNameForOp("Retval"), "_Retval", + opts.op_registry()); + node_builder.Input(std::move(a)).Attr("index", index); + return opts.FinalizeBuilder(&node_builder); +} + +absl::Status Encapsulate( + GraphDef* graphdef, FunctionDefLibrary* library, + const std::vector& encapsulated_functions) { + absl::Status s; + // Convert the GraphDef to a Graph + std::unique_ptr lib_def( + new FunctionLibraryDefinition(OpRegistry::Global(), *library)); + GraphConstructorOptions options; + options.allow_internal_ops = true; + std::unique_ptr graph(new Graph(lib_def.get())); + s = ConvertGraphDefToGraph(options, *graphdef, graph.get()); + if (!s.ok()) return s; + + s = PerformStaticShapeInferenceBeforeEncapsulation(graph.get()); + if (!s.ok()) return s; + + // Create FunctionLibraryRuntime. + SessionOptions session_options; + std::vector> devices; + CHECK_OK(DeviceFactory::AddDevices( + session_options, "/job:localhost/replica:0/task:0", &devices)); + OptimizerOptions opts; + auto device_mgr = std::make_unique(std::move(devices)); + auto pflr = std::make_unique( + device_mgr.get(), Env::Default(), /*config=*/nullptr, + TF_GRAPH_DEF_VERSION, lib_def.get(), opts, + /*default_thread_pool=*/nullptr, /*cluster_flr=*/nullptr); + auto flr = pflr->GetFLR("/job:localhost/replica:0/task:0/cpu:0"); + + std::unique_ptr graph_out; + s = EncapsulateSubgraphsInFunctions("_encapsulate", *graph, + /*rewrite_subgraph_fn=*/{}, + /*reuse_existing_functions=*/false, + &graph_out, lib_def.get()); + if (!s.ok()) return s; + + std::unordered_map clusters; + for (const auto& func : encapsulated_functions) { + Node* xla_computation_node; + for (Node* n : graph_out->nodes()) { + if (n->name() == func) { + xla_computation_node = n; + } + } + if (!xla_computation_node) { + return absl::InternalError(absl::StrCat("Cannot find node ", func)); + } + NameAttrList func_name_attrs; + func_name_attrs.set_name(func); + clusters.emplace(func, + XlaClusterInfo{func, func_name_attrs, xla_computation_node, + std::map{}}); + } + bool modified; + s = ExtractOutsideCompilation("_encapsulate", "_outside", clusters, + graph_out.get(), flr, lib_def.get(), &modified); + if (!s.ok()) return s; + + GraphDef graphdef_out; + graph_out->ToGraphDef(&graphdef_out); + graphdef->Swap(&graphdef_out); + + *library = lib_def->ToProto(); + // Remove "_xla_inferred_shapes" attr. They are added by + // `PerformStaticShapeInferenceBeforeEncapsulation`. + for (FunctionDef& fdef : *library->mutable_function()) { + for (NodeDef& node_def : *fdef.mutable_node_def()) { + node_def.mutable_attr()->erase("_xla_inferred_shapes"); + } + } + + return s; +} + +absl::Status Encapsulate(GraphDef* graphdef, FunctionDefLibrary* library) { + std::vector encapsulated_functions; + return Encapsulate(graphdef, library, encapsulated_functions); +} + +// If there are no marked nodes, funcification should be a no-op. +TEST(EncapsulateSubgraphsTest, NoFunctions) { + GraphDefBuilder builder(GraphDefBuilder::kFailImmediately); + + Node* a = Input(builder.opts().WithName("A")); + Node* b = Input(builder.opts().WithName("B")); + Node* c = Unary(a, builder.opts().WithName("C")); + Binary(b, c, builder.opts().WithName("D")); + + GraphDef graphdef_in; + FunctionDefLibrary library_in; + TF_EXPECT_OK(builder.ToGraphDef(&graphdef_in)); + *library_in.add_function() = test::function::XTimesTwo(); + + GraphDef graphdef_out = graphdef_in; + FunctionDefLibrary library_out = library_in; + TF_EXPECT_OK(Encapsulate(&graphdef_out, &library_out)); + + // If there are no marked nodes, funcification should be a no-op. + TF_EXPECT_GRAPH_EQ(graphdef_in, graphdef_out); + TF_EXPECT_FUNCTIONDEFLIBRARY_EQ(library_in, library_out); +} + +// Test with one function to transform. +TEST(EncapsulateSubgraphsTest, OneFunction) { + FunctionDefLibrary library; + GraphDef graphdef; + + { + *library.add_function() = test::function::XTimesTwo(); + + GraphDefBuilder b1(GraphDefBuilder::kFailImmediately); + Node* a = Input(b1.opts().WithName("A")); + Node* b = Input(b1.opts().WithName("B")); + // Give nodes 'c' and 'd' names that collide after lowercasing. + Node* c = Unary(a, b1.opts().WithName("C").WithAttr("_encapsulate", "F1")); + Node* d = Binary(b, c, + b1.opts().WithName("c").WithControlInput(c).WithAttr( + "_encapsulate", "F1")); + Binary(a, d, b1.opts().WithName("E")); + TF_EXPECT_OK(b1.ToGraphDef(&graphdef)); + } + + TF_EXPECT_OK(Encapsulate(&graphdef, &library)); + + FunctionDefLibrary library_expected; + GraphDef graphdef_expected; + + *library_expected.add_function() = test::function::XTimesTwo(); + *library_expected.add_function() = FunctionDefHelper::Create( + "F1", {"a_0_arg:float", "b_0_arg:float"}, {"c_0_retval:float"}, {}, + { + {{"C"}, "UnaryTest", {"a_0_arg"}}, + {{"c"}, "BinaryTest", {"b_0_arg", "C:o:0"}, {}, {"C"}}, + }, + {{"c_0_retval", "c:o:0"}}); + + { + std::unique_ptr lib_def( + new FunctionLibraryDefinition(OpRegistry::Global(), library_expected)); + GraphDefBuilder b2(GraphDefBuilder::kFailImmediately, lib_def.get()); + Node* a = Input(b2.opts().WithName("A")); + Node* b = Input(b2.opts().WithName("B")); + + NodeBuilder node_builder("F1", "F1", lib_def.get()); + node_builder.Input(a).Input(b); + Node* call = b2.opts().FinalizeBuilder(&node_builder); + + Binary(a, call, b2.opts().WithName("E")); + TF_EXPECT_OK(b2.ToGraphDef(&graphdef_expected)); + } + + TF_EXPECT_GRAPH_EQ(graphdef_expected, graphdef); + TF_EXPECT_FUNCTIONDEFLIBRARY_EQ(library_expected, library); +} + +// Test with two functions to transform. +TEST(EncapsulateSubgraphsTest, TwoFunctions) { + FunctionDefLibrary library; + GraphDef graphdef; + + { + *library.add_function() = test::function::XTimesTwo(); + + GraphDefBuilder b1(GraphDefBuilder::kFailImmediately); + Node* a = Input(b1.opts().WithName("A")); + Node* b = Input(b1.opts().WithName("B")); + Node* control = Input(b1.opts().WithName("Control")); + Node* c = + Unary(a, b1.opts().WithName("C").WithControlInput(control).WithAttr( + "_encapsulate", "F1")); + Node* d = Binary(b, c, + b1.opts().WithName("D").WithControlInput(control).WithAttr( + "_encapsulate", "F2")); + Binary(a, d, b1.opts().WithName("E")); + TF_EXPECT_OK(b1.ToGraphDef(&graphdef)); + } + + TF_EXPECT_OK(Encapsulate(&graphdef, &library)); + + FunctionDefLibrary library_expected; + GraphDef graphdef_expected; + + *library_expected.add_function() = test::function::XTimesTwo(); + *library_expected.add_function() = FunctionDefHelper::Create( + "F1", {"a_0_arg:float"}, {"c_0_retval:float"}, {}, + { + {{"C"}, "UnaryTest", {"a_0_arg"}}, + }, + {{"c_0_retval", "C:o:0"}}); + *library_expected.add_function() = FunctionDefHelper::Create( + "F2", {"b_0_arg:float", "c_0_arg:float"}, {"d_0_retval:float"}, {}, + { + {{"D"}, "BinaryTest", {"b_0_arg", "c_0_arg"}}, + }, + {{"d_0_retval", "D:o:0"}}); + + { + std::unique_ptr lib_def( + new FunctionLibraryDefinition(OpRegistry::Global(), library_expected)); + GraphDefBuilder b2(GraphDefBuilder::kFailImmediately, lib_def.get()); + Node* a = Input(b2.opts().WithName("A")); + Node* b = Input(b2.opts().WithName("B")); + Node* control = Input(b2.opts().WithName("Control")); + + NodeBuilder nb("F1", "F1", lib_def.get()); + nb.Input(a).ControlInput(control); + Node* call1 = b2.opts().FinalizeBuilder(&nb); + + NodeBuilder nb2("F2", "F2", lib_def.get()); + nb2.Input(b).Input(call1).ControlInput(control); + Node* call2 = b2.opts().FinalizeBuilder(&nb2); + + Binary(a, call2, b2.opts().WithName("E")); + TF_EXPECT_OK(b2.ToGraphDef(&graphdef_expected)); + } + + // If there are no marked nodes, funcification should be a no-op. + TF_EXPECT_GRAPH_EQ(graphdef_expected, graphdef); + TF_EXPECT_FUNCTIONDEFLIBRARY_EQ(library_expected, library); +} + +// Returns a vector of node names in 'graph', sorted by name. +std::vector GraphNodes(const Graph& graph) { + std::vector nodes; + for (const auto& node : graph.nodes()) { + if (!node->IsSource() && !node->IsSink()) { + nodes.push_back(node->name()); + } + } + std::sort(nodes.begin(), nodes.end()); + return nodes; +} + +// Returns a sorted vector of (src, dst) edges in 'graph'. +std::vector> GraphEdges( + const Graph& graph) { + std::vector> edges; + for (const Edge* edge : graph.edges()) { + if (edge->src()->IsSource() || edge->dst()->IsSink()) continue; + edges.emplace_back( + absl::StrCat(edge->src()->name(), ":", edge->src_output()), + absl::StrCat(edge->dst()->name(), ":", edge->dst_input())); + } + std::sort(edges.begin(), edges.end()); + return edges; +} + +TEST(EncapsulateSubgraphsTest, InputDeduplication) { + Scope root = Scope::NewRootScope().ExitOnError().WithDevice( + "/job:localhost/replica:0/task:0/cpu:0"); + auto x = ops::Placeholder(root.WithOpName("x"), DT_FLOAT); + auto add1 = ops::Add(root.WithOpName("add1"), x, x); + add1.node()->AddAttr("_cluster", "cluster1"); + auto add2 = ops::Add(root.WithOpName("add2"), add1, add1); + add2.node()->AddAttr("_cluster", "cluster2"); + auto out = ops::Mul(root.WithOpName("mul"), add1, add2); + + Graph graph_before_encapsulation(OpRegistry::Global()); + TF_ASSERT_OK(root.ToGraph(&graph_before_encapsulation)); + + FunctionLibraryDefinition library(OpRegistry::Global(), FunctionDefLibrary()); + std::unique_ptr graph; + TF_ASSERT_OK(EncapsulateSubgraphsInFunctions( + "_cluster", graph_before_encapsulation, + /*rewrite_subgraph_fn=*/{}, + /*reuse_existing_functions=*/false, &graph, &library)); + + std::vector expected_nodes = {"cluster1", "cluster2", "mul", + "x"}; + EXPECT_EQ(expected_nodes, GraphNodes(*graph)); + + std::vector> expected_edges = { + {"cluster1:0", "cluster2:0"}, + {"cluster1:0", "mul:0"}, + {"cluster2:0", "mul:1"}, + {"x:0", "cluster1:0"}}; + EXPECT_EQ(expected_edges, GraphEdges(*graph)); +} + +const Node* FindNodeByName(const Graph& graph, const std::string& name) { + for (const Node* node : graph.nodes()) { + if (node->name() == name) return node; + } + return nullptr; +} + +bool HasGuaranteeConstAttr(const Node& n) { + bool is_guaranteed_constant = false; + if (!GetNodeAttr(n.attrs(), "_is_guaranteed_constant", + &is_guaranteed_constant) + .ok()) { + return false; + } + return is_guaranteed_constant; +} + +TEST(EncapsulateSubgraphsWithGuaranteeConstOpTest, Simple) { + Scope root = Scope::NewRootScope().ExitOnError().WithDevice( + "/job:localhost/replica:0/task:0/cpu:0"); + auto x1 = ops::Placeholder(root.WithOpName("x1"), DT_FLOAT); + auto x2 = ops::Placeholder(root.WithOpName("x2"), DT_FLOAT); + auto const_guarantee_x2 = + ops::GuaranteeConst(root.WithOpName("const_guarantee_x2"), x2); + auto const_guarantee_x1 = + ops::GuaranteeConst(root.WithOpName("const_guarantee_x1"), x1); + auto add1 = + ops::Add(root.WithOpName("add1"), const_guarantee_x1, const_guarantee_x2); + add1.node()->AddAttr("_encapsulate", "encapsulate1"); + + Graph graph_before(OpRegistry::Global()); + TF_ASSERT_OK(root.ToGraph(&graph_before)); + + std::unique_ptr graph_after; + FunctionLibraryDefinition library(OpRegistry::Global(), FunctionDefLibrary()); + int guaranteed_consts = 0; + TF_ASSERT_OK(EncapsulateSubgraphsInFunctions( + "_encapsulate", graph_before, + /*rewrite_subgraph_fn=*/ + [&guaranteed_consts](const std::vector& arg_source_tensors, + std::unique_ptr* graph_ptr, + std::vector* input_permutation, + std::vector* output_permutation, + NodeDef* call_def) { + Graph* graph = graph_ptr->get(); + for (const Node* n : graph->nodes()) { + if (n->type_string() == "_Arg" && + absl::StartsWith(n->name(), "const")) { + ++guaranteed_consts; + EXPECT_TRUE(HasGuaranteeConstAttr(*n)); + } else { + EXPECT_FALSE(HasGuaranteeConstAttr(*n)); + } + } + return absl::OkStatus(); + }, + /*reuse_existing_functions=*/false, &graph_after, &library)); + EXPECT_EQ(2, guaranteed_consts); +} + +TEST(EncapsulateSubgraphsWithGuaranteeConstOpTest, Add) { + Scope root = Scope::NewRootScope().ExitOnError().WithDevice( + "/job:localhost/replica:0/task:0/cpu:0"); + auto x1 = ops::Placeholder(root.WithOpName("x1"), DT_FLOAT); + auto x2 = ops::Placeholder(root.WithOpName("x2"), DT_FLOAT); + auto const_guarantee_x1 = + ops::GuaranteeConst(root.WithOpName("const_guarantee_x1"), x1); + auto const_guarantee_x2 = + ops::GuaranteeConst(root.WithOpName("const_guarantee_x2"), x2); + auto const_guarantee_add1 = ops::Add(root.WithOpName("const_guarantee_add1"), + const_guarantee_x1, const_guarantee_x2); + auto add2 = ops::Add(root.WithOpName("add2"), const_guarantee_x1, x2); + auto mul1 = ops::Mul(root.WithOpName("mul1"), const_guarantee_add1, add2); + mul1.node()->AddAttr("_encapsulate", "encapsulate1"); + + Graph graph_before(OpRegistry::Global()); + TF_ASSERT_OK(root.ToGraph(&graph_before)); + + std::unique_ptr graph_after; + FunctionLibraryDefinition library(OpRegistry::Global(), FunctionDefLibrary()); + int guaranteed_consts = 0; + TF_ASSERT_OK(EncapsulateSubgraphsInFunctions( + "_encapsulate", graph_before, + /*rewrite_subgraph_fn=*/ + [&guaranteed_consts](const std::vector& arg_source_tensors, + std::unique_ptr* graph_ptr, + std::vector* input_permutation, + std::vector* output_permutation, + NodeDef* call_def) { + Graph* graph = graph_ptr->get(); + for (const Node* n : graph->nodes()) { + if (n->type_string() == "_Arg" && + absl::StartsWith(n->name(), "const")) { + ++guaranteed_consts; + EXPECT_TRUE(HasGuaranteeConstAttr(*n)); + } else { + EXPECT_FALSE(HasGuaranteeConstAttr(*n)); + } + } + return absl::OkStatus(); + }, + /*reuse_existing_functions=*/false, &graph_after, &library)); + // Only 1 runtime const, which is const_guarantee_add1. Add2 has one const + // and another non-const, so overall non-const. + EXPECT_EQ(1, guaranteed_consts); +} + +// Test with one function to transform and one outside_compilation cluster. +TEST(EncapsulateSubgraphsTest, OneFunctionOneOutside) { + FunctionDefLibrary library; + GraphDef graphdef; + + { + *library.add_function() = test::function::XTimesTwo(); + + GraphDefBuilder b1(GraphDefBuilder::kFailImmediately); + Node* a = Input(b1.opts().WithName("A")); + Node* b = Input(b1.opts().WithName("B")); + // Give nodes 'c' and 'd' names that collide after lowercasing. + Node* c = Unary(a, b1.opts().WithName("C").WithAttr("_encapsulate", "F1")); + Node* d = Binary(b, c, + b1.opts().WithName("c").WithControlInput(c).WithAttr( + "_encapsulate", "F1")); + Node* e = Binary(c, d, + b1.opts() + .WithName("E") + .WithControlInputs({b, d}) + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + Node* f = Binary(c, e, + b1.opts().WithName("F").WithControlInput(e).WithAttr( + "_encapsulate", "F1")); + Binary(a, f, b1.opts().WithName("G").WithControlInput(e)); + TF_EXPECT_OK(b1.ToGraphDef(&graphdef)); + } + + std::vector encapsulated_functions{"F1"}; + TF_EXPECT_OK(Encapsulate(&graphdef, &library, encapsulated_functions)); + + FunctionDefLibrary library_expected; + GraphDef graphdef_expected; + + { + GraphDefBuilder shape(GraphDefBuilder::kFailImmediately); + Node* key_constant = KeyPlaceholder("F1", shape.opts()); + Node* recv = RecvAtHost( + ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {DT_FLOAT, DT_FLOAT}, + shape.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + Node* e = Binary(ops::NodeOut(recv, 0), ops::NodeOut(recv, 1), + shape.opts() + .WithName("E") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + SendFromHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {e}, + shape.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + TF_EXPECT_OK( + AddGraphDefToFunctionLibrary(shape, "F1_F1_O1", &library_expected)); + } + + NameAttrList shape_inference_graph; + shape_inference_graph.set_name( + "_outside_compilation_shape_inference_F1_F1_O1"); + *library_expected.add_function() = test::function::XTimesTwo(); + *library_expected.add_function() = FunctionDefHelper::Create( + "F1", {"a_0_arg:float", "b_0_arg:float"}, {"f_0_retval_retval:float"}, {}, + { + {{"C"}, "UnaryTest", {"a_0_arg"}}, + {{"c"}, "BinaryTest", {"b_0_arg", "C:o:0"}, {}, {"C"}}, + {{"F"}, + "BinaryTest", + {"C:o:0", "outside_compilation_O1_host_compute:outputs:0"}, + {}, + {"outside_compilation_O1_host_compute"}}, + {{"outside_compilation_O1_host_compute"}, + "XlaHostCompute", + {"C:o:0", "c:o:0"}, + {{"Tinputs", absl::Span({DT_FLOAT, DT_FLOAT})}, + {"Toutputs", absl::Span({DT_FLOAT})}, + {"ancestors", absl::Span({})}, + {"key", "host_compute_channel_F1_F1_O1"}, + {"send_key", ""}, + {"recv_key", ""}, + {"shape_inference_graph", shape_inference_graph}, + {"tpu_core", 0}, + {"cost_estimate_ns", 1000000}, + {"shapes", absl::Span({})}, + {"_outside_compilation_subgraph", "O1"}, + {"_xla_token_input_nodes", + absl::Span({"_xla_token_arg_node"})}, + {"_xla_original_oc_node_name", + "outside_compilation_O1_host_compute"}}, + {"c"}}, + }, + {{"f_0_retval_retval", "F:o:0"}}); + + { + std::unique_ptr lib_def( + new FunctionLibraryDefinition(OpRegistry::Global(), library_expected)); + GraphDefBuilder b2(GraphDefBuilder::kFailImmediately, lib_def.get()); + Node* a = Input(b2.opts().WithName("A")); + Node* b = Input(b2.opts().WithName("B")); + + Node* key_constant = + KeyPlaceholder("F1", b2.opts().WithName("F1_key_placeholder")); + Node* recv = RecvAtHost( + ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {DT_FLOAT, DT_FLOAT}, + b2.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + Node* e = Binary(ops::NodeOut(recv, 0), ops::NodeOut(recv, 1), + b2.opts() + .WithName("E") + .WithControlInputs({recv}) + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + Node* send = + SendFromHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {e}, + b2.opts().WithControlInput(e).WithAttr( + kXlaHasHostTransferAttrName, true)); + + Node* s = Sequencer( + b2.opts().WithName("F1_sequencer").WithControlInputs({recv, send}), + "F1"); + + NodeBuilder node_builder("F1", "F1", lib_def.get()); + node_builder.Input(a).Input(b); + Node* call = + b2.opts().WithControlInputs({s, b}).FinalizeBuilder(&node_builder); + + Binary(a, call, b2.opts().WithName("G").WithControlInputs({call})); + TF_EXPECT_OK(b2.ToGraphDef(&graphdef_expected)); + } + + TF_EXPECT_GRAPH_EQ(graphdef_expected, graphdef); + TF_EXPECT_FUNCTIONDEFLIBRARY_EQ(library_expected, library); +} + +// Test with one function to transform and two outside_compilation clusters. +TEST(EncapsulateSubgraphsTest, OneFunctionTwoOutside) { + FunctionDefLibrary library; + GraphDef graphdef; + + { + GraphDefBuilder b1(GraphDefBuilder::kFailImmediately); + Node* a = Input(b1.opts().WithName("A")); + Node* b = Input(b1.opts().WithName("B")); + Node* c = Unary(a, b1.opts().WithName("C").WithAttr("_encapsulate", "F1")); + Node* d = + Binary(b, c, b1.opts().WithName("D").WithAttr("_encapsulate", "F1")); + Node* e = Binary(c, d, + b1.opts() + .WithName("E") + .WithControlInputs({b, d}) + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + Node* f = Binary(c, e, + b1.opts().WithName("F").WithControlInput(e).WithAttr( + "_encapsulate", "F1")); + Node* g = Binary(e, f, + b1.opts() + .WithName("G") + .WithControlInputs({e, f}) + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O2")); + Node* h = Binary(d, e, + b1.opts() + .WithName("H") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O2")); + Node* i = Unary(h, b1.opts().WithName("I").WithAttr("_encapsulate", "F1")); + Binary(g, i, b1.opts().WithName("J")); + TF_EXPECT_OK(b1.ToGraphDef(&graphdef)); + } + + std::vector encapsulated_functions{"F1"}; + TF_EXPECT_OK(Encapsulate(&graphdef, &library, encapsulated_functions)); + + FunctionDefLibrary library_expected; + GraphDef graphdef_expected; + + { + GraphDefBuilder shape1(GraphDefBuilder::kFailImmediately); + Node* key_constant = KeyPlaceholder("F1", shape1.opts()); + Node* recv = RecvAtHost( + ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {DT_FLOAT, DT_FLOAT}, + shape1.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + Node* e = Binary(ops::NodeOut(recv, 0), ops::NodeOut(recv, 1), + shape1.opts() + .WithName("E") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + SendFromHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {e}, + shape1.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + TF_EXPECT_OK( + AddGraphDefToFunctionLibrary(shape1, "F1_F1_O1", &library_expected)); + } + + { + GraphDefBuilder shape2(GraphDefBuilder::kFailImmediately); + Node* key_constant = KeyPlaceholder("F1", shape2.opts()); + Node* recv1 = RecvAtHost( + ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {DT_FLOAT, DT_FLOAT}, + shape2.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + Node* e = Binary(ops::NodeOut(recv1, 0), ops::NodeOut(recv1, 1), + shape2.opts() + .WithName("E") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + Node* recv2 = RecvAtHost( + ops::NodeOut(key_constant, 0), "F1", "F1", "O2", {DT_FLOAT, DT_FLOAT}, + shape2.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + Node* g = Binary(e, ops::NodeOut(recv2, 0), + shape2.opts() + .WithName("G") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O2")); + Node* h = Binary(ops::NodeOut(recv2, 1), e, + shape2.opts() + .WithName("H") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O2")); + SendFromHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O2", {g, h}, + shape2.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + TF_EXPECT_OK( + AddGraphDefToFunctionLibrary(shape2, "F1_F1_O2", &library_expected)); + } + + NameAttrList shape_inference_graph1, shape_inference_graph2; + shape_inference_graph1.set_name( + "_outside_compilation_shape_inference_F1_F1_O1"); + shape_inference_graph2.set_name( + "_outside_compilation_shape_inference_F1_F1_O2"); + *library_expected.add_function() = FunctionDefHelper::Create( + "F1", {"a_0_arg:float", "b_0_arg:float"}, + {"g_0_retval_retval:float", "i_0_retval_retval:float"}, {}, + { + {{"C"}, "UnaryTest", {"a_0_arg"}}, + {{"D"}, "BinaryTest", {"b_0_arg", "C:o:0"}, {}}, + {{"I"}, + "UnaryTest", + {"outside_compilation_O2_host_compute:outputs:1"}}, + {{"F"}, + "BinaryTest", + {"C:o:0", "outside_compilation_O1_host_compute:outputs:0"}, + {}, + {"outside_compilation_O1_host_compute"}}, + {{"outside_compilation_O2_host_compute"}, + "XlaHostCompute", + {"F:o:0", "D:o:0"}, + {{"Tinputs", absl::Span({DT_FLOAT, DT_FLOAT})}, + {"Toutputs", absl::Span({DT_FLOAT, DT_FLOAT})}, + {"ancestors", absl::Span({})}, + {"key", "host_compute_channel_F1_F1_O2"}, + {"send_key", ""}, + {"recv_key", ""}, + {"shape_inference_graph", shape_inference_graph2}, + {"tpu_core", 0}, + {"cost_estimate_ns", 1000000}, + {"shapes", absl::Span({})}, + {"_outside_compilation_subgraph", "O2"}, + {"_xla_token_input_nodes", + absl::Span( + {"_xla_token_arg_node", + "outside_compilation_O1_host_compute"})}, + {"_xla_original_oc_node_name", + "outside_compilation_O2_host_compute"}}, + {"F", "outside_compilation_O1_host_compute"}}, + {{"outside_compilation_O1_host_compute"}, + "XlaHostCompute", + {"C:o:0", "D:o:0"}, + {{"Tinputs", absl::Span({DT_FLOAT, DT_FLOAT})}, + {"Toutputs", absl::Span({DT_FLOAT})}, + {"ancestors", absl::Span({})}, + {"key", "host_compute_channel_F1_F1_O1"}, + {"send_key", ""}, + {"recv_key", ""}, + {"shape_inference_graph", shape_inference_graph1}, + {"tpu_core", 0}, + {"cost_estimate_ns", 1000000}, + {"shapes", absl::Span({})}, + {"_outside_compilation_subgraph", "O1"}, + {"_xla_token_input_nodes", + absl::Span({"_xla_token_arg_node"})}, + {"_xla_original_oc_node_name", + "outside_compilation_O1_host_compute"}}, + {"D"}}, + }, + {{"g_0_retval_retval", "outside_compilation_O2_host_compute:outputs:0"}, + {"i_0_retval_retval", "I:o:0"}}); + + { + std::unique_ptr lib_def( + new FunctionLibraryDefinition(OpRegistry::Global(), library_expected)); + GraphDefBuilder b2(GraphDefBuilder::kFailImmediately, lib_def.get()); + Node* a = Input(b2.opts().WithName("A")); + Node* b = Input(b2.opts().WithName("B")); + + Node* key_constant = + KeyPlaceholder("F1", b2.opts().WithName("F1_key_placeholder")); + Node* recv1 = RecvAtHost( + ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {DT_FLOAT, DT_FLOAT}, + b2.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + Node* e = Binary(ops::NodeOut(recv1, 0), ops::NodeOut(recv1, 1), + b2.opts() + .WithName("E") + .WithControlInputs({recv1}) + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + Node* send1 = + SendFromHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {e}, + b2.opts().WithControlInput(e).WithAttr( + kXlaHasHostTransferAttrName, true)); + + Node* recv2 = RecvAtHost( + ops::NodeOut(key_constant, 0), "F1", "F1", "O2", {DT_FLOAT, DT_FLOAT}, + b2.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + Node* g = Binary(e, ops::NodeOut(recv2, 0), + b2.opts() + .WithName("G") + .WithControlInputs({recv2, e}) + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O2")); + Node* h = Binary(ops::NodeOut(recv2, 1), e, + b2.opts() + .WithName("H") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O2")); + Node* send2 = + SendFromHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O2", {g, h}, + b2.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + + Node* s = Sequencer(b2.opts() + .WithName("F1_sequencer") + .WithControlInputs({recv1, send1, recv2, send2}), + "F1"); + + NodeBuilder node_builder("F1", "F1", lib_def.get()); + node_builder.Input(a).Input(b); + Node* call = + b2.opts().WithControlInputs({s, b}).FinalizeBuilder(&node_builder); + + Binary(ops::NodeOut(call, 0), ops::NodeOut(call, 1), + b2.opts().WithName("J")); + TF_EXPECT_OK(b2.ToGraphDef(&graphdef_expected)); + } + TF_EXPECT_GRAPH_EQ(graphdef_expected, graphdef); + TF_EXPECT_FUNCTIONDEFLIBRARY_EQ(library_expected, library); +} + +// Test with two functions to transform, each with one outside_compilation +// cluster. +TEST(EncapsulateSubgraphsTest, TwoFunctionsTwoOutside) { + FunctionDefLibrary library; + GraphDef graphdef; + + { + GraphDefBuilder b1(GraphDefBuilder::kFailImmediately); + Node* a = InputShaped(b1.opts().WithName("A")); + Node* b = InputShaped(b1.opts().WithName("B")); + Node* c = Unary(a, b1.opts().WithName("C").WithAttr("_encapsulate", "F1")); + Node* d = + Binary(b, c, b1.opts().WithName("D").WithAttr("_encapsulate", "F1")); + Node* e = Binary(c, d, + b1.opts() + .WithName("E") + .WithControlInputs({b, d}) + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + Node* f = Binary(c, e, + b1.opts().WithName("F").WithControlInput(e).WithAttr( + "_encapsulate", "F1")); + Node* g = Binary(e, f, + b1.opts().WithName("G").WithControlInputs({e, f}).WithAttr( + "_encapsulate", "F2")); + Node* h = Binary(d, g, + b1.opts() + .WithName("H") + .WithAttr("_encapsulate", "F2") + .WithAttr("_outside", "O1")); + Node* i = + Binary(f, h, b1.opts().WithName("I").WithAttr("_encapsulate", "F2")); + Binary(g, i, b1.opts().WithName("J")); + TF_EXPECT_OK(b1.ToGraphDef(&graphdef)); + } + + std::vector encapsulated_functions{"F1", "F2"}; + TF_EXPECT_OK(Encapsulate(&graphdef, &library, encapsulated_functions)); + + FunctionDefLibrary library_expected; + GraphDef graphdef_expected; + + TensorShapeProto shape_proto_expected; + shape_proto_expected.add_dim()->set_size(2); + + *library_expected.add_function() = FunctionDefHelper::Create( + "F1", {"a_0_arg:float", "b_0_arg:float"}, + {"e_0_retval_retval:float", "f_0_retval_retval:float", + "d_0_retval_retval:float"}, + {}, + { + {{"C"}, "UnaryTest", {"a_0_arg"}}, + {{"D"}, "BinaryTest", {"b_0_arg", "C:o:0"}}, + {{"F"}, + "BinaryTest", + {"C:o:0", "outside_compilation_O1_host_compute:outputs:0"}, + {}, + {"outside_compilation_O1_host_compute"}}, + {{"outside_compilation_O1_host_compute"}, + "XlaHostCompute", + {"C:o:0", "D:o:0"}, + {{"Tinputs", absl::Span({DT_FLOAT, DT_FLOAT})}, + {"Toutputs", absl::Span({DT_FLOAT})}, + {"ancestors", absl::Span({})}, + {"key", "host_compute_channel_F1_F1_O1"}, + {"send_key", ""}, + {"recv_key", ""}, + {"shape_inference_graph", NameAttrList()}, + {"tpu_core", 0}, + {"cost_estimate_ns", 1000000}, + {"shapes", + absl::Span({shape_proto_expected})}, + {"_outside_compilation_subgraph", "O1"}, + {"_xla_token_input_nodes", + absl::Span({"_xla_token_arg_node"})}, + {"_xla_original_oc_node_name", + "outside_compilation_O1_host_compute"}}, + {"D"}}, + }, + {{"e_0_retval_retval", "outside_compilation_O1_host_compute:outputs:0"}, + {"d_0_retval_retval", "D:o:0"}, + {"f_0_retval_retval", "F:o:0"}}); + + *library_expected.add_function() = FunctionDefHelper::Create( + "F2", {"e_0_arg:float", "f_0_arg:float", "d_0_arg:float"}, + {"g_0_retval_retval:float", "i_0_retval_retval:float"}, {}, + { + {{"G"}, "BinaryTest", {"e_0_arg", "f_0_arg"}}, + {{"I"}, + "BinaryTest", + {"f_0_arg", "outside_compilation_O1_host_compute:outputs:0"}}, + {{"outside_compilation_O1_host_compute"}, + "XlaHostCompute", + {"d_0_arg", "G:o:0"}, + {{"Tinputs", absl::Span({DT_FLOAT, DT_FLOAT})}, + {"Toutputs", absl::Span({DT_FLOAT})}, + {"ancestors", absl::Span({})}, + {"key", "host_compute_channel_F2_F2_O1"}, + {"send_key", ""}, + {"recv_key", ""}, + {"shape_inference_graph", NameAttrList()}, + {"tpu_core", 0}, + {"cost_estimate_ns", 1000000}, + {"shapes", + absl::Span({shape_proto_expected})}, + {"_outside_compilation_subgraph", "O1"}, + {"_xla_token_input_nodes", + absl::Span({"_xla_token_arg_node"})}, + {"_xla_original_oc_node_name", + "outside_compilation_O1_host_compute"}}}, + }, + {{"g_0_retval_retval", "G:o:0"}, {"i_0_retval_retval", "I:o:0"}}); + + { + std::unique_ptr lib_def( + new FunctionLibraryDefinition(OpRegistry::Global(), library_expected)); + GraphDefBuilder b2(GraphDefBuilder::kFailImmediately, lib_def.get()); + Node* a = InputShaped(b2.opts().WithName("A")); + Node* b = InputShaped(b2.opts().WithName("B")); + + Node* key_constant1 = + KeyPlaceholder("F1", b2.opts().WithName("F1_key_placeholder")); + Node* recv1 = RecvAtHost( + ops::NodeOut(key_constant1, 0), "F1", "F1", "O1", {DT_FLOAT, DT_FLOAT}, + b2.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + Node* e = Binary(ops::NodeOut(recv1, 0), ops::NodeOut(recv1, 1), + b2.opts() + .WithName("E") + .WithControlInputs({recv1}) + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + Node* send1 = + SendFromHost(ops::NodeOut(key_constant1, 0), "F1", "F1", "O1", {e}, + b2.opts().WithControlInput(e).WithAttr( + kXlaHasHostTransferAttrName, true)); + Node* s1 = Sequencer( + b2.opts().WithName("F1_sequencer").WithControlInputs({recv1, send1}), + "F1"); + + NodeBuilder node_builder1("F1", "F1", lib_def.get()); + node_builder1.Input(a).Input(b); + Node* call1 = + b2.opts().WithControlInputs({s1, b}).FinalizeBuilder(&node_builder1); + + Node* key_constant2 = + KeyPlaceholder("F2", b2.opts().WithName("F2_key_placeholder")); + Node* recv2 = RecvAtHost( + ops::NodeOut(key_constant2, 0), "F2", "F2", "O1", {DT_FLOAT, DT_FLOAT}, + b2.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + Node* h = Binary(recv2, ops::NodeOut(recv2, 1), + b2.opts() + .WithName("H") + .WithAttr("_encapsulate", "F2") + .WithAttr("_outside", "O1")); + Node* send2 = + SendFromHost(ops::NodeOut(key_constant2, 0), "F2", "F2", "O1", {h}, + b2.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + + Node* s2 = Sequencer( + b2.opts().WithName("F2_sequencer").WithControlInputs({recv2, send2}), + "F2"); + NodeBuilder node_builder2("F2", "F2", lib_def.get()); + node_builder2.Input(call1) + .Input(ops::NodeOut(call1, 1)) + .Input(ops::NodeOut(call1, 2)); + Node* call2 = b2.opts() + .WithControlInputs({s2, call1}) + .FinalizeBuilder(&node_builder2); + Binary(call2, ops::NodeOut(call2, 1), b2.opts().WithName("J")); + TF_EXPECT_OK(b2.ToGraphDef(&graphdef_expected)); + } + + TF_EXPECT_GRAPH_EQ(graphdef_expected, graphdef); + TF_EXPECT_FUNCTIONDEFLIBRARY_EQ(library_expected, library); +} + +// Test with two functions to transform, each with one outside_compilation +// cluster, with the dependency between them purely from an outside_compilation +// edge. +TEST(EncapsulateSubgraphsTest, TwoFunctionsTwoOutsideDependencyFromOutside) { + FunctionDefLibrary library; + GraphDef graphdef; + + { + GraphDefBuilder b1(GraphDefBuilder::kFailImmediately); + Node* a = InputShaped(b1.opts().WithName("A")); + Node* b = InputShaped(b1.opts().WithName("B")); + Node* c = Unary(a, b1.opts().WithName("C").WithAttr("_encapsulate", "F1")); + Node* d = + Binary(b, c, b1.opts().WithName("D").WithAttr("_encapsulate", "F1")); + Node* e = Binary(c, d, + b1.opts() + .WithName("E") + .WithControlInputs({b, d}) + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + Node* f = Binary(c, e, + b1.opts().WithName("F").WithControlInput(e).WithAttr( + "_encapsulate", "F1")); + Node* g = + Binary(a, b, b1.opts().WithName("G").WithAttr("_encapsulate", "F2")); + Node* h = Unary(g, b1.opts() + .WithName("H") + .WithAttr("_encapsulate", "F2") + .WithAttr("_outside", "O1")); + Node* i = Unary(h, b1.opts().WithName("I").WithAttr("_encapsulate", "F2")); + Binary(f, i, b1.opts().WithName("J")); + TF_EXPECT_OK(b1.ToGraphDef(&graphdef)); + } + + std::vector encapsulated_functions{"F1", "F2"}; + TF_EXPECT_OK(Encapsulate(&graphdef, &library, encapsulated_functions)); + + FunctionDefLibrary library_expected; + GraphDef graphdef_expected; + TensorShapeProto shape_proto_expected; + shape_proto_expected.add_dim()->set_size(2); + + *library_expected.add_function() = FunctionDefHelper::Create( + "F1", {"a_0_arg:float", "b_0_arg:float"}, {"f_0_retval_retval:float"}, {}, + { + {{"C"}, "UnaryTest", {"a_0_arg"}}, + {{"D"}, "BinaryTest", {"b_0_arg", "C:o:0"}}, + {{"F"}, + "BinaryTest", + {"C:o:0", "outside_compilation_O1_host_compute:outputs:0"}, + {}, + {"outside_compilation_O1_host_compute"}}, + {{"outside_compilation_O1_host_compute"}, + "XlaHostCompute", + {"C:o:0", "D:o:0"}, + {{"Tinputs", absl::Span({DT_FLOAT, DT_FLOAT})}, + {"Toutputs", absl::Span({DT_FLOAT})}, + {"ancestors", absl::Span({})}, + {"key", "host_compute_channel_F1_F1_O1"}, + {"send_key", ""}, + {"recv_key", ""}, + {"shape_inference_graph", NameAttrList()}, + {"tpu_core", 0}, + {"cost_estimate_ns", 1000000}, + {"shapes", + absl::Span({shape_proto_expected})}, + {"_outside_compilation_subgraph", "O1"}, + {"_xla_token_input_nodes", + absl::Span({"_xla_token_arg_node"})}, + {"_xla_original_oc_node_name", + "outside_compilation_O1_host_compute"}}, + {"D"}}, + }, + {{"f_0_retval_retval", "F:o:0"}}); + + *library_expected.add_function() = FunctionDefHelper::Create( + "F2", {"a_0_arg:float", "b_0_arg:float"}, {"i_0_retval_retval:float"}, {}, + { + {{"G"}, "BinaryTest", {"a_0_arg", "b_0_arg"}}, + {{"I"}, + "UnaryTest", + {"outside_compilation_O1_host_compute:outputs:0"}}, + {{"outside_compilation_O1_host_compute"}, + "XlaHostCompute", + {"G:o:0"}, + {{"Tinputs", absl::Span({DT_FLOAT})}, + {"Toutputs", absl::Span({DT_FLOAT})}, + {"ancestors", absl::Span({})}, + {"key", "host_compute_channel_F2_F2_O1"}, + {"send_key", ""}, + {"recv_key", ""}, + {"shape_inference_graph", NameAttrList()}, + {"tpu_core", 0}, + {"cost_estimate_ns", 1000000}, + {"shapes", + absl::Span({shape_proto_expected})}, + {"_outside_compilation_subgraph", "O1"}, + {"_xla_token_input_nodes", + absl::Span({"_xla_token_arg_node"})}, + {"_xla_original_oc_node_name", + "outside_compilation_O1_host_compute"}}}, + }, + {{"i_0_retval_retval", "I:o:0"}}); + + { + std::unique_ptr lib_def( + new FunctionLibraryDefinition(OpRegistry::Global(), library_expected)); + GraphDefBuilder b2(GraphDefBuilder::kFailImmediately, lib_def.get()); + Node* a = InputShaped(b2.opts().WithName("A")); + Node* b = InputShaped(b2.opts().WithName("B")); + + Node* key_constant1 = + KeyPlaceholder("F1", b2.opts().WithName("F1_key_placeholder")); + Node* recv1 = RecvAtHost(ops::NodeOut(key_constant1, 0), "F1", "F1", "O1", + {DT_FLOAT, DT_FLOAT}, b2.opts()); + Node* e = Binary(ops::NodeOut(recv1, 0), ops::NodeOut(recv1, 1), + b2.opts() + .WithName("E") + .WithControlInputs({recv1}) + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + Node* send1 = SendFromHost(ops::NodeOut(key_constant1, 0), "F1", "F1", "O1", + {e}, b2.opts().WithControlInput(e)); + Node* s1 = Sequencer( + b2.opts().WithName("F1_sequencer").WithControlInputs({recv1, send1}), + "F1"); + + NodeBuilder node_builder1("F1", "F1", lib_def.get()); + node_builder1.Input(a).Input(b); + Node* call1 = + b2.opts().WithControlInputs({s1, b}).FinalizeBuilder(&node_builder1); + + Node* key_constant2 = + KeyPlaceholder("F2", b2.opts().WithName("F2_key_placeholder")); + Node* recv2 = RecvAtHost(ops::NodeOut(key_constant2, 0), "F2", "F2", "O1", + {DT_FLOAT}, b2.opts()); + Node* h = Unary(recv2, b2.opts() + .WithName("H") + .WithAttr("_encapsulate", "F2") + .WithAttr("_outside", "O1")); + Node* send2 = SendFromHost(ops::NodeOut(key_constant2, 0), "F2", "F2", "O1", + {h}, b2.opts()); + + Node* s2 = Sequencer( + b2.opts().WithName("F2_sequencer").WithControlInputs({recv2, send2}), + "F2"); + NodeBuilder node_builder2("F2", "F2", lib_def.get()); + node_builder2.Input(a).Input(b); + Node* call2 = + b2.opts().WithControlInputs({s2}).FinalizeBuilder(&node_builder2); + Binary(call1, call2, b2.opts().WithName("J")); + TF_EXPECT_OK(b2.ToGraphDef(&graphdef_expected)); + } + + TF_EXPECT_GRAPH_EQ(graphdef_expected, graphdef); + TF_EXPECT_FUNCTIONDEFLIBRARY_EQ(library_expected, library); +} + +// Test with one outside_compilation cluster that has no inputs from the +// compiled subgraph. +TEST(EncapsulateSubgraphsTest, OutsideCompilationNoInputs) { + FunctionDefLibrary library; + GraphDef graphdef; + + { + GraphDefBuilder b1(GraphDefBuilder::kFailImmediately); + Node* a = InputShaped(b1.opts().WithName("A")); + Node* b = Input(b1.opts().WithName("B")); + Node* c = Unary(a, b1.opts().WithName("C").WithAttr("_encapsulate", "F1")); + Node* d = + Binary(b, c, b1.opts().WithName("D").WithAttr("_encapsulate", "F1")); + Node* e = Unary(a, b1.opts() + .WithName("E") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + Node* f = + Binary(d, e, b1.opts().WithName("F").WithAttr("_encapsulate", "F1")); + Unary(f, b1.opts().WithName("G")); + TF_EXPECT_OK(b1.ToGraphDef(&graphdef)); + } + + std::vector encapsulated_functions{"F1"}; + TF_EXPECT_OK(Encapsulate(&graphdef, &library, encapsulated_functions)); + + FunctionDefLibrary library_expected; + GraphDef graphdef_expected; + + TensorShapeProto shape_proto_expected; + shape_proto_expected.add_dim()->set_size(2); + + *library_expected.add_function() = FunctionDefHelper::Create( + "F1", {"a_0_arg:float", "b_0_arg:float"}, {"f_0_retval_retval:float"}, {}, + { + {{"C"}, "UnaryTest", {"a_0_arg"}}, + {{"D"}, "BinaryTest", {"b_0_arg", "C:o:0"}}, + {{"F"}, + "BinaryTest", + {"D:o:0", "outside_compilation_O1_host_compute:outputs:0"}}, + {{"outside_compilation_O1_host_compute"}, + "XlaHostCompute", + {"a_0_arg"}, + {{"Tinputs", absl::Span({DT_FLOAT})}, + {"Toutputs", absl::Span({DT_FLOAT})}, + {"ancestors", absl::Span({})}, + {"key", "host_compute_channel_F1_F1_O1"}, + {"send_key", ""}, + {"recv_key", ""}, + {"shape_inference_graph", NameAttrList()}, + {"tpu_core", 0}, + {"cost_estimate_ns", 1000000}, + {"shapes", + absl::Span({shape_proto_expected})}, + {"_outside_compilation_subgraph", "O1"}, + {"_xla_token_input_nodes", + absl::Span({"_xla_token_arg_node"})}, + {"_xla_original_oc_node_name", + "outside_compilation_O1_host_compute"}}}, + }, + {{"f_0_retval_retval", "F:o:0"}}); + + { + std::unique_ptr lib_def( + new FunctionLibraryDefinition(OpRegistry::Global(), library_expected)); + GraphDefBuilder b2(GraphDefBuilder::kFailImmediately, lib_def.get()); + Node* a = InputShaped(b2.opts().WithName("A")); + Node* b = Input(b2.opts().WithName("B")); + + Node* key_constant = + KeyPlaceholder("F1", b2.opts().WithName("F1_key_placeholder")); + Node* recv1 = RecvAtHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", + {DT_FLOAT}, b2.opts()); + Node* e = Unary(recv1, b2.opts() + .WithName("E") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + Node* send1 = SendFromHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", + {e}, b2.opts()); + Node* s1 = Sequencer( + b2.opts().WithName("F1_sequencer").WithControlInputs({send1, recv1}), + "F1"); + NodeBuilder node_builder1("F1", "F1", lib_def.get()); + node_builder1.Input(a).Input(b); + Node* call1 = + b2.opts().WithControlInput(s1).FinalizeBuilder(&node_builder1); + + Unary(call1, b2.opts().WithName("G")); + TF_EXPECT_OK(b2.ToGraphDef(&graphdef_expected)); + } + + TF_EXPECT_GRAPH_EQ(graphdef_expected, graphdef); + TF_EXPECT_FUNCTIONDEFLIBRARY_EQ(library_expected, library); +} + +// Test with one outside_compilation cluster that has no data inputs but has a +// control input from the compiled subgraph. +TEST(EncapsulateSubgraphsTest, OutsideCompilationControlInput) { + FunctionDefLibrary library; + GraphDef graphdef; + + { + GraphDefBuilder b1(GraphDefBuilder::kFailImmediately); + Node* a = InputShaped(b1.opts().WithName("A")); + Node* b = Input(b1.opts().WithName("B")); + Node* c = Unary(a, b1.opts().WithName("C").WithAttr("_encapsulate", "F1")); + Node* d = + Binary(b, c, b1.opts().WithName("D").WithAttr("_encapsulate", "F1")); + Node* e = Unary(a, b1.opts() + .WithName("E") + .WithControlInput(d) + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + Node* f = + Binary(d, e, b1.opts().WithName("F").WithAttr("_encapsulate", "F1")); + Unary(f, b1.opts().WithName("G")); + TF_EXPECT_OK(b1.ToGraphDef(&graphdef)); + } + + std::vector encapsulated_functions{"F1"}; + TF_EXPECT_OK(Encapsulate(&graphdef, &library, encapsulated_functions)); + + FunctionDefLibrary library_expected; + GraphDef graphdef_expected; + + TensorShapeProto shape_proto_expected; + shape_proto_expected.add_dim()->set_size(2); + + *library_expected.add_function() = FunctionDefHelper::Create( + "F1", {"a_0_arg:float", "b_0_arg:float"}, {"f_0_retval_retval:float"}, {}, + { + {{"C"}, "UnaryTest", {"a_0_arg"}}, + {{"D"}, "BinaryTest", {"b_0_arg", "C:o:0"}}, + {{"F"}, + "BinaryTest", + {"D:o:0", "outside_compilation_O1_host_compute:outputs:0"}}, + {{"outside_compilation_O1_host_compute"}, + "XlaHostCompute", + {"a_0_arg"}, + {{"Tinputs", absl::Span({DT_FLOAT})}, + {"Toutputs", absl::Span({DT_FLOAT})}, + {"ancestors", absl::Span({})}, + {"key", "host_compute_channel_F1_F1_O1"}, + {"send_key", ""}, + {"recv_key", ""}, + {"shape_inference_graph", NameAttrList()}, + {"tpu_core", 0}, + {"cost_estimate_ns", 1000000}, + {"shapes", + absl::Span({shape_proto_expected})}, + {"_outside_compilation_subgraph", "O1"}, + {"_xla_token_input_nodes", + absl::Span({"_xla_token_arg_node"})}, + {"_xla_original_oc_node_name", + "outside_compilation_O1_host_compute"}}, + {"D"}}, + }, + {{"f_0_retval_retval", "F:o:0"}}); + + { + std::unique_ptr lib_def( + new FunctionLibraryDefinition(OpRegistry::Global(), library_expected)); + GraphDefBuilder b2(GraphDefBuilder::kFailImmediately, lib_def.get()); + Node* a = InputShaped(b2.opts().WithName("A")); + Node* b = Input(b2.opts().WithName("B")); + + Node* key_constant = + KeyPlaceholder("F1", b2.opts().WithName("F1_key_placeholder")); + Node* recv1 = RecvAtHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", + {DT_FLOAT}, b2.opts()); + Node* e = Unary(recv1, b2.opts() + .WithName("E") + .WithControlInput(recv1) + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + Node* send1 = SendFromHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", + {e}, b2.opts()); + Node* s1 = Sequencer( + b2.opts().WithName("F1_sequencer").WithControlInputs({recv1, send1}), + "F1"); + NodeBuilder node_builder1("F1", "F1", lib_def.get()); + node_builder1.Input(a).Input(b); + Node* call1 = + b2.opts().WithControlInput(s1).FinalizeBuilder(&node_builder1); + + Unary(call1, b2.opts().WithName("G")); + TF_EXPECT_OK(b2.ToGraphDef(&graphdef_expected)); + } + + TF_EXPECT_GRAPH_EQ(graphdef_expected, graphdef); + TF_EXPECT_FUNCTIONDEFLIBRARY_EQ(library_expected, library); +} + +// Test with one outside_compilation cluster that has no outputs from the +// compiled subgraph. +TEST(EncapsulateSubgraphsTest, OutsideCompilationNoOutputs) { + FunctionDefLibrary library; + GraphDef graphdef; + + { + GraphDefBuilder b1(GraphDefBuilder::kFailImmediately); + Node* a = Input(b1.opts().WithName("A")); + Node* b = Input(b1.opts().WithName("B")); + Node* c = Unary(a, b1.opts().WithName("C").WithAttr("_encapsulate", "F1")); + Node* d = + Binary(b, c, b1.opts().WithName("D").WithAttr("_encapsulate", "F1")); + Node* e = Unary(d, b1.opts() + .WithName("E") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + Node* f = Unary(d, b1.opts().WithName("F").WithAttr("_encapsulate", "F1")); + Binary(e, f, b1.opts().WithName("G")); + TF_EXPECT_OK(b1.ToGraphDef(&graphdef)); + } + + std::vector encapsulated_functions{"F1"}; + TF_EXPECT_OK(Encapsulate(&graphdef, &library, encapsulated_functions)); + + FunctionDefLibrary library_expected; + GraphDef graphdef_expected; + + { + GraphDefBuilder shape1(GraphDefBuilder::kFailImmediately); + Node* key_constant = KeyPlaceholder("F1", shape1.opts()); + Node* recv1 = + RecvAtHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {DT_FLOAT}, + shape1.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + Node* e = Unary(ops::NodeOut(recv1, 0), shape1.opts() + .WithName("E") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + SendFromHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {e}, + shape1.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + TF_EXPECT_OK( + AddGraphDefToFunctionLibrary(shape1, "F1_F1_O1", &library_expected)); + } + + NameAttrList shape_inference_graph; + shape_inference_graph.set_name( + "_outside_compilation_shape_inference_F1_F1_O1"); + *library_expected.add_function() = FunctionDefHelper::Create( + "F1", {"a_0_arg:float", "b_0_arg:float"}, + {"e_0_retval_retval:float", "f_0_retval_retval:float"}, {}, + { + {{"C"}, "UnaryTest", {"a_0_arg"}}, + {{"D"}, "BinaryTest", {"b_0_arg", "C:o:0"}}, + {{"F"}, "UnaryTest", {"D:o:0"}}, + {{"outside_compilation_O1_host_compute"}, + "XlaHostCompute", + {"D:o:0"}, + {{"Tinputs", absl::Span({DT_FLOAT})}, + {"Toutputs", absl::Span({DT_FLOAT})}, + {"ancestors", absl::Span({})}, + {"key", "host_compute_channel_F1_F1_O1"}, + {"send_key", ""}, + {"recv_key", ""}, + {"shape_inference_graph", shape_inference_graph}, + {"tpu_core", 0}, + {"cost_estimate_ns", 1000000}, + {"shapes", absl::Span({})}, + {"_outside_compilation_subgraph", "O1"}, + {"_xla_token_input_nodes", + absl::Span({"_xla_token_arg_node"})}, + {"_xla_original_oc_node_name", + "outside_compilation_O1_host_compute"}}}, + }, + {{"e_0_retval_retval", "outside_compilation_O1_host_compute:outputs:0"}, + {"f_0_retval_retval", "F:o:0"}}); + + { + std::unique_ptr lib_def( + new FunctionLibraryDefinition(OpRegistry::Global(), library_expected)); + GraphDefBuilder b2(GraphDefBuilder::kFailImmediately, lib_def.get()); + Node* a = Input(b2.opts().WithName("A")); + Node* b = Input(b2.opts().WithName("B")); + + Node* key_constant = + KeyPlaceholder("F1", b2.opts().WithName("F1_key_placeholder")); + Node* recv1 = RecvAtHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", + {DT_FLOAT}, b2.opts()); + Node* e = Unary(recv1, b2.opts() + .WithName("E") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + Node* send1 = SendFromHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", + {e}, b2.opts()); + Node* s1 = Sequencer( + b2.opts().WithName("F1_sequencer").WithControlInputs({recv1, send1}), + "F1"); + NodeBuilder node_builder1("F1", "F1", lib_def.get()); + node_builder1.Input(a).Input(b); + Node* call1 = + b2.opts().WithControlInput(s1).FinalizeBuilder(&node_builder1); + + Binary(call1, ops::NodeOut(call1, 1), b2.opts().WithName("G")); + TF_EXPECT_OK(b2.ToGraphDef(&graphdef_expected)); + } + + TF_EXPECT_GRAPH_EQ(graphdef_expected, graphdef); + TF_EXPECT_FUNCTIONDEFLIBRARY_EQ(library_expected, library); +} + +// Test with one outside_compilation cluster that has no data outputs but has a +// control output to the compiled subgraph. +TEST(EncapsulateSubgraphsTest, OutsideCompilationControlOutput) { + FunctionDefLibrary library; + GraphDef graphdef; + + { + GraphDefBuilder b1(GraphDefBuilder::kFailImmediately); + Node* a = Input(b1.opts().WithName("A")); + Node* b = Input(b1.opts().WithName("B")); + Node* c = Unary(a, b1.opts().WithName("C").WithAttr("_encapsulate", "F1")); + Node* d = + Binary(b, c, b1.opts().WithName("D").WithAttr("_encapsulate", "F1")); + Node* e = Unary(d, b1.opts() + .WithName("E") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + Node* f = Unary(d, b1.opts().WithName("F").WithControlInput(e).WithAttr( + "_encapsulate", "F1")); + Binary(e, f, b1.opts().WithName("G")); + TF_EXPECT_OK(b1.ToGraphDef(&graphdef)); + } + + std::vector encapsulated_functions{"F1"}; + TF_EXPECT_OK(Encapsulate(&graphdef, &library, encapsulated_functions)); + + FunctionDefLibrary library_expected; + GraphDef graphdef_expected; + + { + GraphDefBuilder shape1(GraphDefBuilder::kFailImmediately); + Node* key_constant = KeyPlaceholder("F1", shape1.opts()); + Node* recv1 = + RecvAtHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {DT_FLOAT}, + shape1.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + Node* e = Unary(ops::NodeOut(recv1, 0), shape1.opts() + .WithName("E") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + SendFromHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {e}, + shape1.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + TF_EXPECT_OK( + AddGraphDefToFunctionLibrary(shape1, "F1_F1_O1", &library_expected)); + } + + NameAttrList shape_inference_graph; + shape_inference_graph.set_name( + "_outside_compilation_shape_inference_F1_F1_O1"); + *library_expected.add_function() = FunctionDefHelper::Create( + "F1", {"a_0_arg:float", "b_0_arg:float"}, + {"e_0_retval_retval:float", "f_0_retval_retval:float"}, {}, + { + {{"C"}, "UnaryTest", {"a_0_arg"}}, + {{"D"}, "BinaryTest", {"b_0_arg", "C:o:0"}}, + {{"F"}, + "UnaryTest", + {"D:o:0"}, + {}, + {"outside_compilation_O1_host_compute"}}, + {{"outside_compilation_O1_host_compute"}, + "XlaHostCompute", + {"D:o:0"}, + {{"Tinputs", absl::Span({DT_FLOAT})}, + {"Toutputs", absl::Span({DT_FLOAT})}, + {"ancestors", absl::Span({})}, + {"key", "host_compute_channel_F1_F1_O1"}, + {"send_key", ""}, + {"recv_key", ""}, + {"shape_inference_graph", shape_inference_graph}, + {"tpu_core", 0}, + {"cost_estimate_ns", 1000000}, + {"shapes", absl::Span({})}, + {"_outside_compilation_subgraph", "O1"}, + {"_xla_token_input_nodes", + absl::Span({"_xla_token_arg_node"})}, + {"_xla_original_oc_node_name", + "outside_compilation_O1_host_compute"}}}, + }, + {{"e_0_retval_retval", "outside_compilation_O1_host_compute:outputs:0"}, + {"f_0_retval_retval", "F:o:0"}}); + + { + std::unique_ptr lib_def( + new FunctionLibraryDefinition(OpRegistry::Global(), library_expected)); + GraphDefBuilder b2(GraphDefBuilder::kFailImmediately, lib_def.get()); + Node* a = Input(b2.opts().WithName("A")); + Node* b = Input(b2.opts().WithName("B")); + + Node* key_constant = + KeyPlaceholder("F1", b2.opts().WithName("F1_key_placeholder")); + Node* recv1 = RecvAtHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", + {DT_FLOAT}, b2.opts()); + Node* e = Unary(recv1, b2.opts() + .WithName("E") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + Node* send1 = SendFromHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", + {e}, b2.opts().WithControlInput(e)); + Node* s1 = Sequencer( + b2.opts().WithName("F1_sequencer").WithControlInputs({recv1, send1}), + "F1"); + NodeBuilder node_builder1("F1", "F1", lib_def.get()); + node_builder1.Input(a).Input(b); + Node* call1 = + b2.opts().WithControlInput(s1).FinalizeBuilder(&node_builder1); + + Binary(call1, ops::NodeOut(call1, 1), b2.opts().WithName("G")); + TF_EXPECT_OK(b2.ToGraphDef(&graphdef_expected)); + } + + TF_EXPECT_GRAPH_EQ(graphdef_expected, graphdef); + TF_EXPECT_FUNCTIONDEFLIBRARY_EQ(library_expected, library); +} + +// Test with two outside_compilation clusters that interact outside the compiled +// subgraph, where the ancestor has no HostCompute Op. +TEST(EncapsulateSubgraphsTest, + OutsideCompilationClusterDependencyNoSrcCluster) { + FunctionDefLibrary library; + GraphDef graphdef; + + { + GraphDefBuilder b1(GraphDefBuilder::kFailImmediately); + Node* a = Input(b1.opts().WithName("A")); + Node* b = Input(b1.opts().WithName("B")); + Node* c = Unary(a, b1.opts().WithName("C").WithAttr("_encapsulate", "F1")); + Node* d = + Binary(b, c, b1.opts().WithName("D").WithAttr("_encapsulate", "F1")); + Node* e = Unary(a, b1.opts() + .WithName("E") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + Node* f = Unary(d, b1.opts().WithName("F").WithAttr("_encapsulate", "F1")); + Node* g = Unary(f, b1.opts() + .WithName("G") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O2") + .WithControlInput(e)); + Node* h = Unary(g, b1.opts().WithName("H").WithAttr("_encapsulate", "F1")); + Binary(e, h, b1.opts().WithName("I")); + TF_EXPECT_OK(b1.ToGraphDef(&graphdef)); + } + + std::vector encapsulated_functions{"F1"}; + TF_EXPECT_OK(Encapsulate(&graphdef, &library, encapsulated_functions)); + + FunctionDefLibrary library_expected; + GraphDef graphdef_expected; + + { + GraphDefBuilder shape1(GraphDefBuilder::kFailImmediately); + Node* key_constant = KeyPlaceholder("F1", shape1.opts()); + Node* recv1 = + RecvAtHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {DT_FLOAT}, + shape1.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + Node* e = Unary(ops::NodeOut(recv1, 0), shape1.opts() + .WithName("E") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + SendFromHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {e}, + shape1.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + TF_EXPECT_OK( + AddGraphDefToFunctionLibrary(shape1, "F1_F1_O1", &library_expected)); + } + + { + GraphDefBuilder shape2(GraphDefBuilder::kFailImmediately); + Node* key_constant = KeyPlaceholder("F1", shape2.opts()); + Node* recv2 = + RecvAtHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O2", {DT_FLOAT}, + shape2.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + Node* g = Unary(ops::NodeOut(recv2, 0), shape2.opts() + .WithName("G") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O2")); + SendFromHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O2", {g}, + shape2.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + TF_EXPECT_OK( + AddGraphDefToFunctionLibrary(shape2, "F1_F1_O2", &library_expected)); + } + + NameAttrList shape_inference_graph1; + shape_inference_graph1.set_name( + "_outside_compilation_shape_inference_F1_F1_O1"); + NameAttrList shape_inference_graph2; + shape_inference_graph2.set_name( + "_outside_compilation_shape_inference_F1_F1_O2"); + *library_expected.add_function() = FunctionDefHelper::Create( + "F1", {"a_0_arg:float", "b_0_arg:float"}, + {"e_0_retval_retval:float", "h_0_retval_retval:float"}, {}, + { + {{"C"}, "UnaryTest", {"a_0_arg"}}, + {{"D"}, "BinaryTest", {"b_0_arg", "C:o:0"}}, + {{"F"}, "UnaryTest", {"D:o:0"}}, + {{"H"}, + "UnaryTest", + {"outside_compilation_O2_host_compute:outputs:0"}}, + {{"outside_compilation_O1_host_compute"}, + "XlaHostCompute", + {"a_0_arg"}, + {{"Tinputs", absl::Span({DT_FLOAT})}, + {"Toutputs", absl::Span({DT_FLOAT})}, + {"ancestors", absl::Span({})}, + {"key", "host_compute_channel_F1_F1_O1"}, + {"send_key", ""}, + {"recv_key", ""}, + {"shape_inference_graph", shape_inference_graph1}, + {"tpu_core", 0}, + {"cost_estimate_ns", 1000000}, + {"shapes", absl::Span({})}, + {"_outside_compilation_subgraph", "O1"}, + {"_xla_token_input_nodes", + absl::Span({"_xla_token_arg_node"})}, + {"_xla_original_oc_node_name", + "outside_compilation_O1_host_compute"}}}, + {{"outside_compilation_O2_host_compute"}, + "XlaHostCompute", + {"F:o:0"}, + {{"Tinputs", absl::Span({DT_FLOAT})}, + {"Toutputs", absl::Span({DT_FLOAT})}, + {"ancestors", absl::Span({})}, + {"key", "host_compute_channel_F1_F1_O2"}, + {"send_key", ""}, + {"recv_key", ""}, + {"shape_inference_graph", shape_inference_graph2}, + {"tpu_core", 0}, + {"cost_estimate_ns", 1000000}, + {"shapes", absl::Span({})}, + {"_outside_compilation_subgraph", "O2"}, + {"_xla_token_input_nodes", + absl::Span( + {"_xla_token_arg_node", + "outside_compilation_O1_host_compute"})}, + {"_xla_original_oc_node_name", + "outside_compilation_O2_host_compute"}}, + {"outside_compilation_O1_host_compute"}}, + }, + {{"e_0_retval_retval", "outside_compilation_O1_host_compute:outputs:0"}, + {"h_0_retval_retval", "H:o:0"}}); + + { + std::unique_ptr lib_def( + new FunctionLibraryDefinition(OpRegistry::Global(), library_expected)); + GraphDefBuilder b2(GraphDefBuilder::kFailImmediately, lib_def.get()); + Node* a = Input(b2.opts().WithName("A")); + Node* b = Input(b2.opts().WithName("B")); + Node* key_constant = + KeyPlaceholder("F1", b2.opts().WithName("F1_key_placeholder")); + Node* recv1 = + RecvAtHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {DT_FLOAT}, + b2.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + + Node* e = Unary(recv1, b2.opts() + .WithName("E") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + Node* send1 = + SendFromHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {e}, + b2.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + Node* recv2 = + RecvAtHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O2", {DT_FLOAT}, + b2.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + Node* g = Unary(recv2, b2.opts() + .WithName("G") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O2") + .WithControlInput(e)); + Node* send2 = + SendFromHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O2", {g}, + b2.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + Node* s1 = Sequencer(b2.opts() + .WithName("F1_sequencer") + .WithControlInputs({recv1, send1, recv2, send2}), + "F1"); + NodeBuilder node_builder1("F1", "F1", lib_def.get()); + node_builder1.Input(a).Input(b).ControlInput(s1); + Node* call1 = b2.opts().FinalizeBuilder(&node_builder1); + + Binary(call1, ops::NodeOut(call1, 1), b2.opts().WithName("I")); + TF_EXPECT_OK(b2.ToGraphDef(&graphdef_expected)); + } + + TF_EXPECT_GRAPH_EQ(graphdef_expected, graphdef); + TF_EXPECT_FUNCTIONDEFLIBRARY_EQ(library_expected, library); +} + +// Test with two outside_compilation clusters that interact outside the compiled +// subgraph, where the successor has no HostCompute Op. +TEST(EncapsulateSubgraphsTest, + OutsideCompilationClusterDependencyNoDstCluster) { + FunctionDefLibrary library; + GraphDef graphdef; + + { + GraphDefBuilder b1(GraphDefBuilder::kFailImmediately); + Node* a = Input(b1.opts().WithName("A")); + Node* b = Input(b1.opts().WithName("B")); + Node* c = Unary(a, b1.opts().WithName("C").WithAttr("_encapsulate", "F1")); + Node* d = + Binary(b, c, b1.opts().WithName("D").WithAttr("_encapsulate", "F1")); + Node* e = Unary(d, b1.opts() + .WithName("E") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + Node* f = Unary(e, b1.opts().WithName("F").WithAttr("_encapsulate", "F1")); + /*Node* g =*/Unary(a, b1.opts() + .WithName("G") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O2") + .WithControlInput(e)); + Node* h = Unary(f, b1.opts().WithName("H").WithAttr("_encapsulate", "F1")); + Binary(e, h, b1.opts().WithName("I")); + TF_EXPECT_OK(b1.ToGraphDef(&graphdef)); + } + + std::vector encapsulated_functions{"F1"}; + TF_EXPECT_OK(Encapsulate(&graphdef, &library, encapsulated_functions)); + + FunctionDefLibrary library_expected; + GraphDef graphdef_expected; + + { + GraphDefBuilder shape1(GraphDefBuilder::kFailImmediately); + Node* key_constant = KeyPlaceholder("F1", shape1.opts()); + Node* recv2 = + RecvAtHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {DT_FLOAT}, + shape1.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + Node* e = Unary(ops::NodeOut(recv2, 0), shape1.opts() + .WithName("E") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + SendFromHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {e}, + shape1.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + TF_EXPECT_OK( + AddGraphDefToFunctionLibrary(shape1, "F1_F1_O1", &library_expected)); + } + + NameAttrList shape_inference_graph; + shape_inference_graph.set_name( + "_outside_compilation_shape_inference_F1_F1_O1"); + *library_expected.add_function() = FunctionDefHelper::Create( + "F1", {"a_0_arg:float", "b_0_arg:float"}, + {"e_0_retval_retval:float", "h_0_retval_retval:float"}, {}, + { + {{"C"}, "UnaryTest", {"a_0_arg"}}, + {{"D"}, "BinaryTest", {"b_0_arg", "C:o:0"}}, + {{"F"}, + "UnaryTest", + {"outside_compilation_O1_host_compute:outputs:0"}}, + {{"H"}, "UnaryTest", {"F:o:0"}}, + {{"outside_compilation_O2_host_compute"}, + "XlaHostCompute", + {"a_0_arg"}, + {{"Tinputs", absl::Span({DT_FLOAT})}, + {"Toutputs", absl::Span({})}, + {"ancestors", absl::Span({})}, + {"key", "host_compute_channel_F1_F1_O2"}, + {"send_key", ""}, + {"recv_key", ""}, + {"shape_inference_graph", NameAttrList()}, + {"tpu_core", 0}, + {"cost_estimate_ns", 1000000}, + {"shapes", absl::Span({})}, + {"_outside_compilation_subgraph", "O2"}, + {"_xla_token_input_nodes", + absl::Span( + {"_xla_token_arg_node", + "outside_compilation_O1_host_compute"})}, + {"_xla_original_oc_node_name", + "outside_compilation_O2_host_compute"}}, + {"outside_compilation_O1_host_compute"}}, + {{"outside_compilation_O1_host_compute"}, + "XlaHostCompute", + {"D:o:0"}, + {{"Tinputs", absl::Span({DT_FLOAT})}, + {"Toutputs", absl::Span({DT_FLOAT})}, + {"ancestors", absl::Span({})}, + {"key", "host_compute_channel_F1_F1_O1"}, + {"send_key", ""}, + {"recv_key", ""}, + {"shape_inference_graph", shape_inference_graph}, + {"tpu_core", 0}, + {"cost_estimate_ns", 1000000}, + {"shapes", absl::Span({})}, + {"_outside_compilation_subgraph", "O1"}, + {"_xla_token_input_nodes", + absl::Span({"_xla_token_arg_node"})}, + {"_xla_original_oc_node_name", + "outside_compilation_O1_host_compute"}}}, + }, + {{"e_0_retval_retval", "outside_compilation_O1_host_compute:outputs:0"}, + {"h_0_retval_retval", "H:o:0"}}); + + { + std::unique_ptr lib_def( + new FunctionLibraryDefinition(OpRegistry::Global(), library_expected)); + GraphDefBuilder b2(GraphDefBuilder::kFailImmediately, lib_def.get()); + Node* a = Input(b2.opts().WithName("A")); + Node* b = Input(b2.opts().WithName("B")); + + Node* key_constant = + KeyPlaceholder("F1", b2.opts().WithName("F1_key_placeholder")); + Node* recv1 = + RecvAtHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {DT_FLOAT}, + b2.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + Node* e = Unary(recv1, b2.opts() + .WithName("E") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + Node* send = + SendFromHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {e}, + b2.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + Node* recv2 = + RecvAtHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O2", {DT_FLOAT}, + b2.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + /*Node* g =*/Unary(recv2, b2.opts() + .WithName("G") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O2") + .WithControlInput(e)); + Node* s1 = Sequencer(b2.opts() + .WithName("F1_sequencer") + .WithControlInputs({recv1, recv2, send}), + "F1"); + NodeBuilder node_builder1("F1", "F1", lib_def.get()); + node_builder1.Input(a).Input(b).ControlInput(s1); + Node* call1 = b2.opts().FinalizeBuilder(&node_builder1); + + Binary(call1, ops::NodeOut(call1, 1), b2.opts().WithName("I")); + TF_EXPECT_OK(b2.ToGraphDef(&graphdef_expected)); + } + + TF_EXPECT_GRAPH_EQ(graphdef_expected, graphdef); + TF_EXPECT_FUNCTIONDEFLIBRARY_EQ(library_expected, library); +} + +// Test with two outside_compilation clusters that interact outside the compiled +// subgraph. +TEST(EncapsulateSubgraphsTest, OutsideCompilationClusterDependency) { + FunctionDefLibrary library; + GraphDef graphdef; + + { + GraphDefBuilder b1(GraphDefBuilder::kFailImmediately); + Node* a = Input(b1.opts().WithName("A")); + Node* b = Input(b1.opts().WithName("B")); + Node* c = Unary(a, b1.opts().WithName("C").WithAttr("_encapsulate", "F1")); + Node* d = + Binary(b, c, b1.opts().WithName("D").WithAttr("_encapsulate", "F1")); + Node* e = Unary(d, b1.opts() + .WithName("E") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + Node* f = Unary(e, b1.opts().WithName("F").WithAttr("_encapsulate", "F1")); + Node* g = Unary(d, b1.opts() + .WithName("G") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O2") + .WithControlInput(e)); + Node* h = Unary(f, b1.opts().WithName("H").WithAttr("_encapsulate", "F1")); + /*Node* i =*/Binary(d, e, + b1.opts() + .WithName("I") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O3") + .WithControlInput(g)); + Binary(e, h, b1.opts().WithName("J")); + TF_EXPECT_OK(b1.ToGraphDef(&graphdef)); + } + + std::vector encapsulated_functions{"F1"}; + TF_EXPECT_OK(Encapsulate(&graphdef, &library, encapsulated_functions)); + + FunctionDefLibrary library_expected; + GraphDef graphdef_expected; + + { + GraphDefBuilder shape1(GraphDefBuilder::kFailImmediately); + Node* key_constant = KeyPlaceholder("F1", shape1.opts()); + Node* recv2 = + RecvAtHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {DT_FLOAT}, + shape1.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + Node* e = Unary(ops::NodeOut(recv2, 0), shape1.opts() + .WithName("E") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + SendFromHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {e}, + shape1.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + TF_EXPECT_OK( + AddGraphDefToFunctionLibrary(shape1, "F1_F1_O1", &library_expected)); + } + + NameAttrList shape_inference_graph; + shape_inference_graph.set_name( + "_outside_compilation_shape_inference_F1_F1_O1"); + *library_expected.add_function() = FunctionDefHelper::Create( + "F1", {"a_0_arg:float", "b_0_arg:float"}, + {"e_0_retval_retval:float", "h_0_retval_retval:float"}, {}, + {{{"C"}, "UnaryTest", {"a_0_arg"}}, + {{"D"}, "BinaryTest", {"b_0_arg", "C:o:0"}}, + {{"F"}, "UnaryTest", {"outside_compilation_O1_host_compute:outputs:0"}}, + {{"H"}, "UnaryTest", {"F:o:0"}}, + {{"outside_compilation_O1_host_compute"}, + "XlaHostCompute", + {"D:o:0"}, + {{"Tinputs", absl::Span({DT_FLOAT})}, + {"Toutputs", absl::Span({DT_FLOAT})}, + {"ancestors", absl::Span({})}, + {"key", "host_compute_channel_F1_F1_O1"}, + {"send_key", ""}, + {"recv_key", ""}, + {"shape_inference_graph", shape_inference_graph}, + {"tpu_core", 0}, + {"cost_estimate_ns", 1000000}, + {"shapes", absl::Span({})}, + {"_outside_compilation_subgraph", "O1"}, + {"_xla_token_input_nodes", + absl::Span({"_xla_token_arg_node"})}, + {"_xla_original_oc_node_name", + "outside_compilation_O1_host_compute"}}}, + {{"outside_compilation_O2_host_compute"}, + "XlaHostCompute", + {"D:o:0"}, + {{"Tinputs", absl::Span({DT_FLOAT})}, + {"Toutputs", absl::Span({})}, + {"ancestors", absl::Span({})}, + {"key", "host_compute_channel_F1_F1_O2"}, + {"send_key", ""}, + {"recv_key", ""}, + {"shape_inference_graph", NameAttrList()}, + {"tpu_core", 0}, + {"cost_estimate_ns", 1000000}, + {"shapes", absl::Span({})}, + {"_outside_compilation_subgraph", "O2"}, + {"_xla_token_input_nodes", + absl::Span( + {"_xla_token_arg_node", "outside_compilation_O1_host_compute"})}, + {"_xla_original_oc_node_name", "outside_compilation_O2_host_compute"}}, + {"outside_compilation_O1_host_compute"}}, + {{"outside_compilation_O3_host_compute"}, + "XlaHostCompute", + {"D:o:0"}, + {{"Tinputs", absl::Span({DT_FLOAT})}, + {"Toutputs", absl::Span({})}, + {"ancestors", absl::Span({})}, + {"key", "host_compute_channel_F1_F1_O3"}, + {"send_key", ""}, + {"recv_key", ""}, + {"shape_inference_graph", NameAttrList()}, + {"tpu_core", 0}, + {"cost_estimate_ns", 1000000}, + {"shapes", absl::Span({})}, + {"_outside_compilation_subgraph", "O3"}, + {"_xla_token_input_nodes", + absl::Span( + {"_xla_token_arg_node", "outside_compilation_O1_host_compute", + "outside_compilation_O2_host_compute"})}, + {"_xla_original_oc_node_name", "outside_compilation_O3_host_compute"}}, + {"outside_compilation_O1_host_compute", + "outside_compilation_O2_host_compute"}}}, + {{"e_0_retval_retval", "outside_compilation_O1_host_compute:outputs:0"}, + {"h_0_retval_retval", "H:o:0"}}); + + { + std::unique_ptr lib_def( + new FunctionLibraryDefinition(OpRegistry::Global(), library_expected)); + GraphDefBuilder b2(GraphDefBuilder::kFailImmediately, lib_def.get()); + Node* a = Input(b2.opts().WithName("A")); + Node* b = Input(b2.opts().WithName("B")); + + Node* key_constant = + KeyPlaceholder("F1", b2.opts().WithName("F1_key_placeholder")); + Node* recv1 = + RecvAtHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {DT_FLOAT}, + b2.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + Node* e = Unary(recv1, b2.opts() + .WithName("E") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + Node* send = + SendFromHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {e}, + b2.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + Node* recv2 = + RecvAtHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O2", {DT_FLOAT}, + b2.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + Node* g = Unary(recv2, b2.opts() + .WithName("G") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O2") + .WithControlInput(e)); + Node* recv3 = + RecvAtHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O3", {DT_FLOAT}, + b2.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + /*Node* i =*/Binary(recv3, e, + b2.opts() + .WithName("I") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O3") + .WithControlInput(g)); + Node* s1 = Sequencer(b2.opts() + .WithName("F1_sequencer") + .WithControlInputs({recv1, send, recv2, recv3}), + "F1"); + NodeBuilder node_builder1("F1", "F1", lib_def.get()); + node_builder1.Input(a).Input(b).ControlInput(s1); + Node* call1 = b2.opts().FinalizeBuilder(&node_builder1); + + Binary(call1, ops::NodeOut(call1, 1), b2.opts().WithName("J")); + TF_EXPECT_OK(b2.ToGraphDef(&graphdef_expected)); + } + + TF_EXPECT_GRAPH_EQ(graphdef_expected, graphdef); + TF_EXPECT_FUNCTIONDEFLIBRARY_EQ(library_expected, library); +} + +// Test with one outside_compilation cluster that has no outputs from the +// compiled subgraph. +TEST(EncapsulateSubgraphsTest, OutsideCompilationNoInputsOrOutputs) { + FunctionDefLibrary library; + GraphDef graphdef; + + { + GraphDefBuilder b1(GraphDefBuilder::kFailImmediately); + Node* a = Input(b1.opts().WithName("A")); + Node* b = Input(b1.opts().WithName("B")); + Node* c = Unary(a, b1.opts().WithName("C").WithAttr("_encapsulate", "F1")); + Node* d = + Binary(b, c, b1.opts().WithName("D").WithAttr("_encapsulate", "F1")); + Node* e = Unary(a, b1.opts() + .WithName("E") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + Node* f = Unary(d, b1.opts().WithName("F").WithAttr("_encapsulate", "F1")); + Binary(e, f, b1.opts().WithName("G")); + TF_EXPECT_OK(b1.ToGraphDef(&graphdef)); + } + + std::vector encapsulated_functions{"F1"}; + TF_EXPECT_OK(Encapsulate(&graphdef, &library, encapsulated_functions)); + + FunctionDefLibrary library_expected; + GraphDef graphdef_expected; + + { + GraphDefBuilder shape1(GraphDefBuilder::kFailImmediately); + Node* key_constant = KeyPlaceholder("F1", shape1.opts()); + Node* recv2 = + RecvAtHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {DT_FLOAT}, + shape1.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + Node* e = Unary(ops::NodeOut(recv2, 0), shape1.opts() + .WithName("E") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + SendFromHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {e}, + shape1.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + TF_EXPECT_OK( + AddGraphDefToFunctionLibrary(shape1, "F1_F1_O1", &library_expected)); + } + + NameAttrList shape_inference_graph; + shape_inference_graph.set_name( + "_outside_compilation_shape_inference_F1_F1_O1"); + *library_expected.add_function() = FunctionDefHelper::Create( + "F1", {"a_0_arg:float", "b_0_arg:float"}, + {"e_0_retval_retval:float", "f_0_retval_retval:float"}, {}, + { + {{"C"}, "UnaryTest", {"a_0_arg"}}, + {{"D"}, "BinaryTest", {"b_0_arg", "C:o:0"}}, + {{"F"}, "UnaryTest", {"D:o:0"}}, + {{"outside_compilation_O1_host_compute"}, + "XlaHostCompute", + {"a_0_arg"}, + {{"Tinputs", absl::Span({DT_FLOAT})}, + {"Toutputs", absl::Span({DT_FLOAT})}, + {"ancestors", absl::Span({})}, + {"key", "host_compute_channel_F1_F1_O1"}, + {"send_key", ""}, + {"recv_key", ""}, + {"shape_inference_graph", shape_inference_graph}, + {"tpu_core", 0}, + {"cost_estimate_ns", 1000000}, + {"shapes", absl::Span({})}, + {"_outside_compilation_subgraph", "O1"}, + {"_xla_token_input_nodes", + absl::Span({"_xla_token_arg_node"})}, + {"_xla_original_oc_node_name", + "outside_compilation_O1_host_compute"}}}, + }, + {{"e_0_retval_retval", "outside_compilation_O1_host_compute:outputs:0"}, + {"f_0_retval_retval", "F:o:0"}}); + + { + std::unique_ptr lib_def( + new FunctionLibraryDefinition(OpRegistry::Global(), library_expected)); + GraphDefBuilder b2(GraphDefBuilder::kFailImmediately, lib_def.get()); + Node* a = Input(b2.opts().WithName("A")); + Node* b = Input(b2.opts().WithName("B")); + + Node* key_constant = + KeyPlaceholder("F1", b2.opts().WithName("F1_key_placeholder")); + Node* recv = + RecvAtHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {DT_FLOAT}, + b2.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + Node* e = Unary(recv, b2.opts() + .WithName("E") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + Node* send = + SendFromHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {e}, + b2.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + Node* s = Sequencer( + b2.opts().WithName("F1_sequencer").WithControlInputs({recv, send}), + "F1"); + NodeBuilder node_builder1("F1", "F1", lib_def.get()); + node_builder1.Input(a).Input(b).ControlInput(s); + Node* call1 = b2.opts().FinalizeBuilder(&node_builder1); + + Binary(call1, ops::NodeOut(call1, 1), b2.opts().WithName("G")); + TF_EXPECT_OK(b2.ToGraphDef(&graphdef_expected)); + } + + TF_EXPECT_GRAPH_EQ(graphdef_expected, graphdef); + TF_EXPECT_FUNCTIONDEFLIBRARY_EQ(library_expected, library); +} + +// Test for shape inference of outside compilation. +TEST(EncapsulateSubgraphsTest, OutsideCompilationShapeInference) { + FunctionDefLibrary library; + GraphDef graphdef; + + { + *library.add_function() = test::function::XTimesTwo(); + + GraphDefBuilder b1(GraphDefBuilder::kFailImmediately); + Node* a = InputShaped(b1.opts().WithName("A")); + Node* b = Input(b1.opts().WithName("B")); + // Give nodes 'c' and 'd' names that collide after lowercasing. + Node* c = Unary(a, b1.opts().WithName("C")); + Node* d = Unary(b, b1.opts().WithName("c").WithControlInput(c).WithAttr( + "_encapsulate", "F1")); + Node* e = BinaryUnknownShape(c, d, + b1.opts() + .WithName("E") + .WithControlInputs({b, d}) + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + Node* f = Binary(c, e, + b1.opts().WithName("F").WithControlInput(e).WithAttr( + "_encapsulate", "F1")); + Binary(a, f, b1.opts().WithName("G").WithControlInput(e)); + TF_EXPECT_OK(b1.ToGraphDef(&graphdef)); + } + + std::vector encapsulated_functions{"F1"}; + TF_EXPECT_OK(Encapsulate(&graphdef, &library, encapsulated_functions)); + + FunctionDefLibrary library_expected; + GraphDef graphdef_expected; + + { + GraphDefBuilder shape(GraphDefBuilder::kFailImmediately); + Node* key_constant = KeyPlaceholder("F1", shape.opts()); + Node* recv = RecvAtHost( + ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {DT_FLOAT, DT_FLOAT}, + shape.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + Node* e = BinaryUnknownShape(recv, ops::NodeOut(recv, 1), + shape.opts() + .WithName("E") + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + SendFromHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {e}, + shape.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + TF_EXPECT_OK( + AddGraphDefToFunctionLibrary(shape, "F1_F1_O1", &library_expected)); + } + + NameAttrList shape_inference_graph; + shape_inference_graph.set_name( + "_outside_compilation_shape_inference_F1_F1_O1"); + *library_expected.add_function() = test::function::XTimesTwo(); + *library_expected.add_function() = FunctionDefHelper::Create( + "F1", {"b_0_arg:float", "c_0_arg:float"}, {"f_0_retval_retval:float"}, {}, + { + {{"c"}, "UnaryTest", {"b_0_arg"}, {}, {}}, + {{"F"}, + "BinaryTest", + {"c_0_arg", "outside_compilation_O1_host_compute:outputs:0"}, + {}, + {"outside_compilation_O1_host_compute"}}, + {{"outside_compilation_O1_host_compute"}, + "XlaHostCompute", + {"c_0_arg", "c:o:0"}, + {{"Tinputs", absl::Span({DT_FLOAT, DT_FLOAT})}, + {"Toutputs", absl::Span({DT_FLOAT})}, + {"ancestors", absl::Span({})}, + {"key", "host_compute_channel_F1_F1_O1"}, + {"send_key", ""}, + {"recv_key", ""}, + {"shape_inference_graph", shape_inference_graph}, + {"tpu_core", 0}, + {"cost_estimate_ns", 1000000}, + {"shapes", absl::Span({})}, + {"_outside_compilation_subgraph", "O1"}, + {"_xla_token_input_nodes", + absl::Span({"_xla_token_arg_node"})}, + {"_xla_original_oc_node_name", + "outside_compilation_O1_host_compute"}}, + {"c"}}, + }, + {{"f_0_retval_retval", "F:o:0"}}); + + { + std::unique_ptr lib_def( + new FunctionLibraryDefinition(OpRegistry::Global(), library_expected)); + GraphDefBuilder b2(GraphDefBuilder::kFailImmediately, lib_def.get()); + Node* a = InputShaped(b2.opts().WithName("A")); + Node* b = Input(b2.opts().WithName("B")); + Node* c = Unary(a, b2.opts().WithName("C")); + + Node* key_constant = + KeyPlaceholder("F1", b2.opts().WithName("F1_key_placeholder")); + Node* recv = RecvAtHost( + ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {DT_FLOAT, DT_FLOAT}, + b2.opts().WithAttr(kXlaHasHostTransferAttrName, true)); + Node* e = BinaryUnknownShape(recv, ops::NodeOut(recv, 1), + b2.opts() + .WithName("E") + .WithControlInputs({recv}) + .WithAttr("_encapsulate", "F1") + .WithAttr("_outside", "O1")); + Node* send = + SendFromHost(ops::NodeOut(key_constant, 0), "F1", "F1", "O1", {e}, + b2.opts().WithControlInput(e).WithAttr( + kXlaHasHostTransferAttrName, true)); + + Node* s = Sequencer( + b2.opts().WithName("F1_sequencer").WithControlInputs({recv, send}), + "F1"); + + NodeBuilder node_builder("F1", "F1", lib_def.get()); + node_builder.Input(b).Input(c); + Node* call = + b2.opts().WithControlInputs({s, b, c}).FinalizeBuilder(&node_builder); + + Binary(a, call, b2.opts().WithName("G").WithControlInputs({call})); + TF_EXPECT_OK(b2.ToGraphDef(&graphdef_expected)); + } + + TF_EXPECT_GRAPH_EQ(graphdef_expected, graphdef); + TF_EXPECT_FUNCTIONDEFLIBRARY_EQ(library_expected, library); +} + +void CreateSubgraphTouchingRefVar(const Scope& s) { + Output variable = + ops::Variable(s.WithOpName("variable"), PartialTensorShape{}, DT_FLOAT); + Output read = ops::Identity(s.WithOpName("read_ref_var"), variable); + Output neg = ops::Negate(s.WithOpName("negate_ref"), read); + Output add = ops::Add(s.WithOpName("add_ref"), neg, neg); + + Output constant = + ops::Const(s.WithOpName("constant_ref"), Input::Initializer(0.0)); + s.graph()->AddControlEdge(constant.node(), variable.node()); +} + +TEST(EncapsulateSubgraphsTest, RefVariablesMarked) { + Scope root = Scope::NewRootScope().ExitOnError(); + CreateSubgraphTouchingRefVar(root); + + auto graph = std::make_unique(OpRegistry::Global()); + TF_ASSERT_OK(root.ToGraph(graph.get())); + + GraphOptimizationPassWrapper wrapper; + GraphOptimizationPassOptions options = + wrapper.CreateGraphOptimizationPassOptions(&graph); + + EncapsulateSubgraphsPass pass; + TF_ASSERT_OK(pass.Run(options)); + + for (const Node* node : graph->nodes()) { + bool has_ref_var; + TF_ASSERT_OK( + GetNodeAttr(node->attrs(), kXlaHasReferenceVarsAttr, &has_ref_var)); + EXPECT_TRUE(node->IsSink() || node->IsSource() || has_ref_var) + << "All nodes apart from source and sink can access reference variable"; + } +} + +void CreateSubgraphNotTouchingRefVar(const Scope& s) { + Output constant = + ops::Const(s.WithOpName("constant_normal"), Input::Initializer(0.0)); + Output neg = ops::Negate(s.WithOpName("negate_normal"), constant); + Output add = ops::Add(s.WithOpName("add_normal"), neg, neg); +} + +TEST(EncapsulateSubgraphsTest, NoRefVarsNoAttr) { + Scope root = Scope::NewRootScope().ExitOnError(); + CreateSubgraphNotTouchingRefVar(root); + + auto graph = std::make_unique(OpRegistry::Global()); + TF_ASSERT_OK(root.ToGraph(graph.get())); + + GraphOptimizationPassWrapper wrapper; + GraphOptimizationPassOptions options = + wrapper.CreateGraphOptimizationPassOptions(&graph); + + EncapsulateSubgraphsPass pass; + TF_ASSERT_OK(pass.Run(options)); + + for (const Node* node : graph->nodes()) { + bool has_ref_var; + TF_ASSERT_OK( + GetNodeAttr(node->attrs(), kXlaHasReferenceVarsAttr, &has_ref_var)); + EXPECT_FALSE(has_ref_var) << "The graph does not have reference variables"; + } +} + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/encapsulate_util.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/encapsulate_util.cc new file mode 100644 index 0000000000000000000000000000000000000000..0c5e18f0fd4c82607611dca8c69e52f3cf4bb439 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/encapsulate_util.cc @@ -0,0 +1,418 @@ +/* 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 "tensorflow/compiler/jit/encapsulate_util.h" + +#include +#include + +#include "absl/container/flat_hash_map.h" +#include "absl/container/flat_hash_set.h" +#include "absl/strings/str_cat.h" +#include "absl/types/optional.h" +#include "tensorflow/compiler/jit/shape_inference.h" +#include "tensorflow/compiler/tf2xla/tf2xla_util.h" +#include "tensorflow/core/framework/node_def_util.h" +#include "tensorflow/core/graph/node_builder.h" +#include "tensorflow/core/protobuf/error_codes.pb.h" + +using tsl::StatusOr; + +namespace tensorflow { + +namespace { + +// Returns string attribute value for the node if the attribute is present, +// otherwise returns empty optional value. +std::optional GetStringAttr(const Node& n, + const std::string& attr_name) { + auto attr = n.attrs().Find(attr_name); + if (!attr) { + return std::nullopt; + } else { + return attr->s(); + } +} + +// Adds a value to the node's list attribute. +template +absl::Status AppendToListAttr(Node* n, const std::string& attr_name, + const std::string& value) { + std::vector attr_value; + absl::Status s = GetNodeAttr(n->attrs(), attr_name, &attr_value); + if (!s.ok() && s.code() != error::NOT_FOUND) { + return s; + } + + n->ClearAttr(attr_name); + attr_value.push_back(value); + n->AddAttr(attr_name, attr_value); + return absl::OkStatus(); +} + +// Replaces attribute value. +template +void ReplaceAttr(Node* n, const std::string& attr_name, const T& value) { + n->ClearAttr(attr_name); + n->AddAttr(attr_name, value); +} + +// Step 1 for `PreprocessEdgesBetweenOutsideCompilations`. See comments of +// `PreprocessEdgesBetweenOutsideCompilations` for details. +absl::Status PreprocessControlEdgesBetweenOutsideCompilations( + Graph* g, const std::string& outside_compilation_attr_name) { + // Gather edges to remove. We should not remove the edge while iterating. + std::vector edges_to_remove; + for (const Edge* e : g->edges()) { + if (!e->IsControlEdge()) { + continue; + } + + auto src_outside_compilation = + GetStringAttr(*e->src(), outside_compilation_attr_name); + auto dst_outside_compilation = + GetStringAttr(*e->dst(), outside_compilation_attr_name); + + if (src_outside_compilation && dst_outside_compilation) { + if (*src_outside_compilation != *dst_outside_compilation) { + // Case 1a: outside compilation to outside compilation control edge. + edges_to_remove.push_back(e); + + TF_RETURN_IF_ERROR(AppendToListAttr( + e->dst(), kXlaControlDependenciesWithinXlaClusterAttrName, + e->src()->name())); + } + } else if (src_outside_compilation && !dst_outside_compilation) { + // Case 1b: outside compilation to its XLA computation control edge. + ReplaceAttr(e->src(), kXlaConnectedToXlaComputationAttrName, true); + } else if (!src_outside_compilation && dst_outside_compilation) { + // Case 1b: XLA computation to outside compilation in it control edge. + ReplaceAttr(e->dst(), kXlaConnectedFromXlaComputationAttrName, true); + } + } + + for (auto e : edges_to_remove) { + g->RemoveEdge(e); + } + return absl::OkStatus(); +} + +// Step 2 for `PreprocessEdgesBetweenOutsideCompilations`. See comments of +// `PreprocessEdgesBetweenOutsideCompilations` for details. +absl::Status PreprocessDataEdgesBetweenOutsideCompilations( + Graph* g, const std::string& outside_compilation_attr_name) { + // Gather edges between outside compilation and host computation. Notice that + // we do not store `Edge*` directly because we remove some nodes while adding + // Identity nodes, and those Edge pointers might be invalidated. + struct EdgeInfo { + int dst_input, dst_node_id; + }; + std::vector edges; + for (const Edge* e : g->edges()) { + if (e->IsControlEdge()) { + continue; + } + + auto src_outside_compilation = + GetStringAttr(*e->src(), outside_compilation_attr_name); + auto dst_outside_compilation = + GetStringAttr(*e->dst(), outside_compilation_attr_name); + + if (src_outside_compilation && dst_outside_compilation && + *src_outside_compilation != *dst_outside_compilation) { + edges.push_back(EdgeInfo{e->dst_input(), e->dst()->id()}); + VLOG(4) << "Oc -> oc edge: " << e->DebugString(); + } + } + + // Remove the edge from host to outside compilation. Add a placeholder as + // outside compilation node input. + std::map, Node*> placeholders; + for (int i = 0, end = edges.size(); i < end; i++) { + Node* dst = g->FindNodeId(edges[i].dst_node_id); + const Edge* e; + TF_RETURN_IF_ERROR(dst->input_edge(edges[i].dst_input, &e)); + Node* src = e->src(); + int src_output = e->src_output(), dst_input = e->dst_input(); + g->RemoveEdge(e); + + // Find or create placeholder node. + std::string new_name = + absl::StrCat(src->name(), "_oc_to_oc_placeholder_", src_output); + auto placeholder_index = std::make_pair(src->name(), src_output); + auto iter = placeholders.find(placeholder_index); + Node* placeholder_node; + if (iter == placeholders.end()) { + NodeDefBuilder placeholder_builder(new_name, "Placeholder"); + placeholder_builder.Attr("dtype", src->output_type(src_output)); + std::string outside_compilation_attr; + TF_RETURN_IF_ERROR(GetNodeAttr(dst->attrs(), + outside_compilation_attr_name, + &outside_compilation_attr)); + placeholder_builder.Attr(outside_compilation_attr_name, + outside_compilation_attr); + placeholder_builder.Attr(kOutsideCompilationOriginalNodeAttrName, + src->name()); + placeholder_builder.Attr(kOutsideCompilationSrcOutputAttrName, + src_output); + NodeDef placeholder_def; + TF_RETURN_IF_ERROR(placeholder_builder.Finalize(&placeholder_def)); + TF_ASSIGN_OR_RETURN(placeholder_node, g->AddNode(placeholder_def)); + placeholders[placeholder_index] = placeholder_node; + } else { + placeholder_node = iter->second; + } + g->AddEdge(placeholder_node, 0, dst, dst_input); + + // Replace `e->dst()` because its input node changed. + NodeDef new_def = dst->def(); + *new_def.mutable_input(dst_input) = placeholder_node->name(); + TF_ASSIGN_OR_RETURN(Node * dst_replace_node, ReplaceNode(g, dst, new_def)); + + // Other edge in `edges` might have `e->dst()` as src or dst + // node. Before removing `e->dst()`, replace those edges with + // corresponding edges for `dst_replace_node`. + for (int j = i + 1, end = edges.size(); j < end; j++) { + if (edges[j].dst_node_id == edges[i].dst_node_id) { + edges[j].dst_node_id = dst_replace_node->id(); + } + } + } + return absl::OkStatus(); +} + +// Step 1 for `PostprocessEdgesBetweenOutsideCompilations`. See comments of +// `PostprocessEdgesBetweenOutsideCompilations` for details. +absl::Status PostprocessDataEdgesBetweenOutsideCompilations( + Graph* g, const std::string& outside_compilation_attr_name) { + // Gather all outside compilation to outside compilation nodes. + std::vector placeholder_nodes; + for (Node* n : g->nodes()) { + if (n->type_string() == "Placeholder" && + HasNodeAttr(n->def(), kOutsideCompilationOriginalNodeAttrName)) { + placeholder_nodes.push_back(n); + } + } + + // Remove the placeholder nodes, and reconnect original edge. + auto node_name_index = g->BuildNodeNameIndex(); + for (auto n : placeholder_nodes) { + std::string node_name; + int node_src_output; + TF_RETURN_IF_ERROR(GetNodeAttr( + n->attrs(), kOutsideCompilationOriginalNodeAttrName, &node_name)); + TF_RETURN_IF_ERROR(GetNodeAttr( + n->attrs(), kOutsideCompilationSrcOutputAttrName, &node_src_output)); + auto iter = node_name_index.find(node_name); + if (iter == node_name_index.end()) { + return absl::InternalError(absl::StrCat( + "Cannot find original node for oc -> host placeholder node ", + node_name)); + } + + // Change all usage node to use the original node instead. + Node* original_node = iter->second; + std::vector control_edges; + std::vector data_edges; + for (auto e : n->out_edges()) { + if (e->IsControlEdge()) { + control_edges.push_back(e); + } else { + data_edges.push_back({e->dst(), e->src_output(), e->dst_input()}); + } + } + for (const Edge* e : control_edges) { + g->AddControlEdge(original_node, e->dst()); + g->RemoveEdge(e); + } + for (int i = 0, end = data_edges.size(); i < end; i++) { + Node* dst = data_edges[i].dst; + NodeDef new_def = dst->def(); + int dst_input = data_edges[i].dst_input; + *new_def.mutable_input(dst_input) = + absl::StrCat(original_node->name(), ":", node_src_output); + TF_ASSIGN_OR_RETURN(Node * replace_node, ReplaceNode(g, dst, new_def)); + + const Edge* edge_to_replace = nullptr; + TF_RETURN_IF_ERROR(replace_node->input_edge(dst_input, &edge_to_replace)); + g->RemoveEdge(edge_to_replace); + g->AddEdge(original_node, node_src_output, replace_node, dst_input); + + // Other edges might have `dst` as dst node. Update those edges with + // `replace_node`. + for (int j = i + 1, end = data_edges.size(); j < end; j++) { + if (data_edges[j].dst == dst) { + data_edges[j].dst = replace_node; + } + } + + // Other placeholder node might have `dst` as original node. Update + // `node_name_index` with `replace_node`. + node_name_index[replace_node->name()] = replace_node; + } + + // Remove placeholder node. + g->RemoveNode(n); + } + return absl::OkStatus(); +} + +// Step 2 for `PostprocessEdgesBetweenOutsideCompilations`. See comments of +// `PostprocessEdgesBetweenOutsideCompilations` for details. +absl::Status PostprocessControlEdgesBetweenOutsideCompilations( + Graph* g, const std::string& outside_compilation_attr_name) { + auto node_name_index = g->BuildNodeNameIndex(); + + // Reconnect outside compilation to outside compilation control edge. + for (Node* n : g->nodes()) { + std::vector control_deps; + absl::Status s = + GetNodeAttr(n->attrs(), kXlaControlDependenciesWithinXlaClusterAttrName, + &control_deps); + if (!s.ok()) { + if (s.code() != error::NOT_FOUND) { + return s; + } else { + continue; + } + } else { + n->ClearAttr(kXlaControlDependenciesWithinXlaClusterAttrName); + for (const std::string& control_input : control_deps) { + auto iter = node_name_index.find(control_input); + if (iter == node_name_index.end()) { + return absl::InternalError( + absl::StrCat("Cannot find original node for ", control_input)); + } + g->AddControlEdge(iter->second, n); + } + } + } + return absl::OkStatus(); +} +} // namespace + +const char kXlaInferredShapesAttrName[] = "_xla_inferred_shapes"; + +const char kXlaConnectedToXlaComputationAttrName[] = + "_xla_connected_to_xla_computation"; +const char kXlaConnectedFromXlaComputationAttrName[] = + "_xla_connected_from_xla_computation"; +const char kOutsideCompilationOriginalNodeAttrName[] = + "_xla_oc_to_oc_node_name"; +const char kOutsideCompilationSrcOutputAttrName[] = "_xla_oc_to_oc_src_output"; +const char kXlaControlDependenciesWithinXlaClusterAttrName[] = + "_xla_control_dependencies_within_xla_cluster"; +const char kXlaIsLiftedArgAttrName[] = "_xla_is_lifted_arg"; +const char kXlaLiftedArgOutsideCompilationAttrName[] = "_xla_lifted_arg_oc"; +const char kXlaOutsideCompilationInputsAttrName[] = "_xla_oc_inputs"; +const char kXlaIsPlaceholderForArg[] = "_xla_is_placeholder_for_arg"; + +absl::Status PerformStaticShapeInferenceBeforeEncapsulation(Graph* g) { + // Perform shape inference. + std::map arg_shapes; + GraphShapeInfo shape_info; + TF_RETURN_IF_ERROR( + InferShapes(g, arg_shapes, /*fnlib_def=*/nullptr, &shape_info)); + + // Add attribute for output shapes. + auto node_name_index = g->BuildNodeNameIndex(); + for (auto iter : shape_info) { + std::vector output_shapes; + std::transform(iter.second.begin(), iter.second.end(), + std::back_inserter(output_shapes), + [](const InferredShape& inferred_shape) { + return inferred_shape.shape; + }); + Node* n = node_name_index[iter.first]; + n->AddAttr(kXlaInferredShapesAttrName, output_shapes); + } + + return absl::OkStatus(); +} + +absl::StatusOr< + std::unique_ptr>>> +OutsideCompilationClusterDependencies( + const Graph* g, const std::string& outside_compilation_attr_name) { + auto cluster_deps = std::make_unique< + absl::flat_hash_map>>(); + + for (const Edge* e : g->edges()) { + auto src_outside_compilation = + GetStringAttr(*e->src(), outside_compilation_attr_name); + auto dst_outside_compilation = + GetStringAttr(*e->dst(), outside_compilation_attr_name); + + if (src_outside_compilation && dst_outside_compilation && + *src_outside_compilation != *dst_outside_compilation) { + auto dst_deps_it = cluster_deps->find(*dst_outside_compilation); + if (dst_deps_it == cluster_deps->end()) { + cluster_deps->insert(std::make_pair( + *dst_outside_compilation, + absl::flat_hash_set({*src_outside_compilation}))); + } else { + dst_deps_it->second.insert(*src_outside_compilation); + } + } + } + + auto cluster_deps_ordered = std::make_unique< + absl::flat_hash_map>>(); + + for (auto it = cluster_deps->begin(); it != cluster_deps->end(); it++) { + std::vector ordered_deps(it->second.begin(), it->second.end()); + std::sort(ordered_deps.begin(), ordered_deps.end()); + cluster_deps_ordered->insert(std::make_pair(it->first, ordered_deps)); + } + + return std::move(cluster_deps_ordered); +} + +absl::Status PreprocessEdgesBetweenOutsideCompilations( + Graph* g, const std::string& outside_compilation_attr_name) { + // Remove edges from source node to outside compilation nodes, and edges + // from outside compilation nodes to sink node. + std::vector edges_to_remove; + for (const Edge* e : g->source_node()->out_edges()) { + if (HasNodeAttr(e->dst()->def(), outside_compilation_attr_name)) { + edges_to_remove.push_back(e); + } + } + for (const Edge* e : g->sink_node()->in_edges()) { + if (HasNodeAttr(e->src()->def(), outside_compilation_attr_name)) { + edges_to_remove.push_back(e); + } + } + for (auto e : edges_to_remove) { + g->RemoveEdge(e); + } + + TF_RETURN_IF_ERROR(PreprocessControlEdgesBetweenOutsideCompilations( + g, outside_compilation_attr_name)); + TF_RETURN_IF_ERROR(PreprocessDataEdgesBetweenOutsideCompilations( + g, outside_compilation_attr_name)); + return absl::OkStatus(); +} + +absl::Status PostprocessEdgesBetweenOutsideCompilations( + Graph* g, const std::string& outside_compilation_attr_name) { + TF_RETURN_IF_ERROR(PostprocessDataEdgesBetweenOutsideCompilations( + g, outside_compilation_attr_name)); + TF_RETURN_IF_ERROR(PostprocessControlEdgesBetweenOutsideCompilations( + g, outside_compilation_attr_name)); + return absl::OkStatus(); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/encapsulate_util.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/encapsulate_util.h new file mode 100644 index 0000000000000000000000000000000000000000..e72ecc159d6e119dbcf0dd7e7f8a61eba2264a26 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/encapsulate_util.h @@ -0,0 +1,155 @@ +/* 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. +==============================================================================*/ + +// This file contains some utility functions for encapsulating XLA computation +// in host graph and encapsulating outside compilation in XLA computation. + +#ifndef TENSORFLOW_COMPILER_JIT_ENCAPSULATE_UTIL_H_ +#define TENSORFLOW_COMPILER_JIT_ENCAPSULATE_UTIL_H_ + +#include "absl/container/flat_hash_map.h" +#include "tensorflow/core/graph/graph.h" + +namespace tensorflow { + +// Attribute marking output tensor shapes inferred by XLA. Attribute value is +// a list of PartialTensorShape objects. +extern const char kXlaInferredShapesAttrName[]; + +// Infers output shapes for all nodes in graph `g`. The output shapes will be +// stored in node attribute `kXlaInferredShapesAttrName`. +// +// We have to perform shape inference before encapsulation because after +// encapsulation, some nodes will be encapsulated into function call, and shape +// inference does not handle function call at the moment. +absl::Status PerformStaticShapeInferenceBeforeEncapsulation(Graph* g); + +// Attribute indicating that some ops in this node's XLA computation has control +// dependency on this node. Attribute value will always be "true". +extern const char kXlaConnectedToXlaComputationAttrName[]; + +// Attribute indicating that this node has control dependency on some ops in +// this node's XLA computation. Attribute value will always be "true". +extern const char kXlaConnectedFromXlaComputationAttrName[]; + +// Attribute indicating that this is an Placeholder node added to act as a +// temporary input node for an outside compilation node. Attribute value will be +// string (original input node name). +extern const char kOutsideCompilationOriginalNodeAttrName[]; + +// Attribute indicating that this is an Placeholder node added to act as a +// temporary input node for an outside compilation node. Attribute value will be +// int (src_output for original edge). +extern const char kOutsideCompilationSrcOutputAttrName[]; + +// Attribute indicating that this node has control dependencies on some other +// nodes within the same XLA cluster. Attribute value will be a list of string +// (node names). +extern const char kXlaControlDependenciesWithinXlaClusterAttrName[]; + +// Attribute indicating that this node is an outside compilation node which is +// lifted out of If/While/function node. Attribute value will always be boolean +// value "true". +extern const char kXlaIsLiftedArgAttrName[]; + +// Attribute indicating that this node is a Placeholder node for an _Arg node +// lifted out of If/While/function node. Attribute value will be a string, which +// is the outside compilation cluster name sending the lifted arg node to host. +extern const char kXlaLiftedArgOutsideCompilationAttrName[]; + +// Attribute indicating that this is an IdentityN node receiving inputs for a +// outside compilation Placeholder node (the original outside compilation node +// is moved out of TPU computation, and we left a Placeholder node there). +// Attribute value will be a string, which is the outside compilation cluster +// name for the outside compilation Placeholder node. +extern const char kXlaOutsideCompilationInputsAttrName[]; + +// Attribute indicating that this is a Placeholder node for an _Arg node used in +// outside compilation. We should not move this node out of XLA computation. +// Attribute value will always be boolean value "true". +extern const char kXlaIsPlaceholderForArg[]; + +// Information for XLA computation. +struct XlaClusterInfo { + // Add an explicitly-defined default constructor for this class. + // + // The compiler may delete the default constructor here because + // host_compute_core is a const member whose type (std::map) doesn't + // necessarily have a user provided constructor -- while libc++ and + // libstdc++ 4.8 provide a user defined default constructor, libstdc++ at + // least >= 7.3 does not. See also c++11 [class.ctor] p5. + // + // TODO(klimek): In c++17 we'll be able to initialize host_compute_core + // without losing aggregate initialization, which allows us to get rid of + // the constructor definitions again. + XlaClusterInfo() {} + XlaClusterInfo(const std::string& cluster_name, + const NameAttrList& func_name_attrs, Node* node, + const std::map& host_compute_core) + : cluster_name(cluster_name), + func_name_attrs(func_name_attrs), + node(node), + host_compute_core(host_compute_core) {} + // XLA cluster name. It might be different from `func_name`. + const std::string cluster_name; + // Name and attributes of XLA computation function. + const NameAttrList func_name_attrs; + // The XLA computation node in the graph. + Node* node; + // A mapping from outside compilation cluster name to its device assignment. + const std::map host_compute_core; +}; + +// Finds dependencies between outside compilation clusters, including both data +// dependencies and control dependencies. cluster_deps maps the name name of an +// outside compilation cluster to a set of names of outside compilation clusters +// that it depends on. +absl::StatusOr< + std::unique_ptr>>> +OutsideCompilationClusterDependencies( + const Graph* g, const std::string& outside_compilation_attr_name); + +// Preprocesses edges within the same XLA cluster. It will perform the following +// operations in order: +// +// 0. Remove edges from source node to outside compilation nodes, and edges +// from outside compilation nodes to sink node. +// 1a. For edges between different outside compilation clusters, remove the edge +// and add attr "kXlaControlDependenciesWithinXlaClusterAttrName = src node +// name" to dst node. +// 1b. For control edges between outside compilation and its XLA computation, +// add attr "kXlaConnected{From, To}XlaComputationAttrName = true" to the +// outside compilation node. +// 2. For data edges between different outside compilations, remove the edge +// and create a Placeholder node as dst node's input. +absl::Status PreprocessEdgesBetweenOutsideCompilations( + Graph* g, const std::string& outside_compilation_attr_name); + +// Postprocesses edges within the same XLA cluster. This function reverts what +// `PreprocessEdgesBetweenOutsideCompilations` did. It will perform the +// following operations in order: +// +// 1. Remove Placeholder nodes between different outside compilations (created +// in `PreprocessEdgesBetweenOutsideCompilations` step 2). +// 2a. Reconnect control edges between different outside compilations (marked by +// `PreprocessEdgesBetweenOutsideCompilations` step 1a). +// Notice that control edges marked by +// `PreprocessEdgesBetweenOutsideCompilations` step 1b are not handled here. +// They are handled in `RewriteOutsideCompilationSubgraphFn`. +absl::Status PostprocessEdgesBetweenOutsideCompilations( + Graph* g, const std::string& outside_compilation_attr_name); +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_ENCAPSULATE_UTIL_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/encapsulate_util_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/encapsulate_util_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..2eaf9c690519e07c608407600423fcf2db55f9da --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/encapsulate_util_test.cc @@ -0,0 +1,62 @@ +/* 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 "tensorflow/compiler/jit/encapsulate_util.h" + +#include + +#include "absl/log/check.h" +#include "tensorflow/cc/framework/ops.h" +#include "tensorflow/cc/framework/scope.h" +#include "tensorflow/cc/ops/array_ops.h" +#include "tensorflow/cc/ops/const_op.h" +#include "tensorflow/cc/ops/math_ops.h" +#include "tensorflow/core/framework/node_def_util.h" +#include "tensorflow/core/framework/op.h" +#include "tensorflow/core/framework/tensor_shape.h" +#include "tensorflow/core/framework/tensor_shape.pb.h" +#include "tensorflow/core/graph/graph.h" +#include "tensorflow/core/platform/test.h" + +namespace tensorflow { + +TEST(PerformStaticShapeInferenceBeforeEncapsulationTest, Basic) { + // Build the graph: + // "add" = "const_0" + "const_1" + // "identity" = "add" + tensorflow::Scope s = tensorflow::Scope::NewRootScope(); + Output const_0 = ops::Const(s.WithOpName("const_0"), 1, {2}); + Output const_1 = ops::Const(s.WithOpName("const_1"), 2, {2}); + Output add = ops::Add(s.WithOpName("add"), const_0, const_1); + Output identity = ops::Identity(s.WithOpName("identity"), add); + Graph g(OpRegistry::Global()); + CHECK_OK(s.ToGraph(&g)); + + CHECK_OK(PerformStaticShapeInferenceBeforeEncapsulation(&g)); + + // Check that "add" node now has _xla_inferred_shapes attr. + auto node_index = g.BuildNodeNameIndex(); + Node *add_node = node_index["add"]; + std::vector output_shapes; + CHECK_OK(GetNodeAttr(add_node->attrs(), kXlaInferredShapesAttrName, + &output_shapes)); + EXPECT_EQ(output_shapes.size(), 1); + TensorShapeProto shape_proto; + output_shapes[0].AsProto(&shape_proto); + EXPECT_EQ(shape_proto.dim_size(), 1); + EXPECT_EQ(shape_proto.dim(0).size(), 2); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/encapsulate_xla_computations_pass.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/encapsulate_xla_computations_pass.cc new file mode 100644 index 0000000000000000000000000000000000000000..64b1a10e7b1ed26c845f36170372e8ed3aaaa671 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/encapsulate_xla_computations_pass.cc @@ -0,0 +1,405 @@ +/* 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 "tensorflow/compiler/jit/encapsulate_xla_computations_pass.h" + +#include +#include + +#include "absl/algorithm/container.h" +#include "absl/container/flat_hash_set.h" +#include "absl/memory/memory.h" +#include "absl/strings/ascii.h" +#include "absl/strings/str_cat.h" +#include "tensorflow/compiler/jit/defs.h" +#include "tensorflow/compiler/jit/encapsulate_subgraphs_pass.h" +#include "tensorflow/compiler/jit/xla_cluster_util.h" +#include "xla/status_macros.h" +#include "tensorflow/core/framework/node_def.pb.h" +#include "tensorflow/core/framework/types.h" +#include "tensorflow/core/graph/graph_node_util.h" +#include "tensorflow/core/lib/core/stringpiece.h" +#include "tensorflow/core/lib/hash/hash.h" +#include "tensorflow/core/lib/strings/proto_serialization.h" +#include "tensorflow/core/lib/strings/str_util.h" +#include "tensorflow/core/platform/fingerprint.h" +#include "tensorflow/core/platform/statusor.h" +#include "tensorflow/core/util/dump_graph.h" + +namespace tensorflow { + +namespace { + +const char* const kXlaClusterOutput = "XlaClusterOutput"; + +bool IsCpuGpuCompile(const Graph* graph) { + for (Node* n : graph->nodes()) { + std::string name; + // Only consider nodes being compiled. + if (!TryGetNodeAttr(n->attrs(), kXlaClusterIdAttr, &name)) continue; + // Early return for any node with a device that is not a CPU or GPU. + DeviceNameUtils::ParsedName parsed; + if (DeviceNameUtils::ParseFullName(n->requested_device(), &parsed)) { + if (parsed.type != DEVICE_CPU && parsed.type != DEVICE_GPU) { + return false; + } + } + } + return true; +} + +// Checks if a graph node is marked to be a guaranteed constant. +bool is_guaranteed_constant(const Node& n) { + bool guaranteed_constant = false; + if (!TryGetNodeAttr(n.attrs(), "_is_guaranteed_constant", + &guaranteed_constant)) { + return false; + } + return guaranteed_constant; +} + +// Finds the `index` of an _Arg or _Retval node. +absl::Status GetIndexAttr(const Node& n, int num_args, int* index) { + TF_RETURN_IF_ERROR(GetNodeAttr(n.attrs(), "index", index)); + if (*index < 0 || *index >= num_args) { + return absl::InvalidArgumentError( + absl::StrCat("Invalid ", n.type_string(), " number ", *index)); + } + return absl::OkStatus(); +} + +// Returns the data type of the destination of an edge. +DataType EdgeType(const Edge* edge) { + return edge->dst()->input_type(edge->dst_input()); +} + +// Adds the control inputs of `node` to `*deps`. +void AddControlInputs(const Node& node, absl::flat_hash_set* deps) { + for (const Edge* edge : node.in_edges()) { + if (edge->IsControlEdge()) { + deps->insert(edge->src()); + } + } +} + +// Adds the control outputs of `node` to `*deps`. +void AddControlOutputs(const Node& node, absl::flat_hash_set* deps) { + for (const Edge* edge : node.out_edges()) { + if (edge->IsControlEdge()) { + deps->insert(edge->dst()); + } + } +} + +// Rewrite function to be passed to EncapsulateSubgraphsInFunctions that sorts +// the arguments into the order expected by XlaLaunch computations: +// 1) arguments +// 2) resource variable arguments +// See the documentation of EncapsulateSubgraphsInFunctions for the meaning +// of the arguments. +// +// TODO(b/113166435): Ordering constraints on XlaLaunch op can be relaxed. +absl::Status RewriteSubgraph( + const std::vector& arg_source_tensors, + std::unique_ptr* graph_ptr, std::vector* input_permutation, + std::vector* output_permutation, NodeDef* call_def) { + Graph* graph = graph_ptr->get(); + const int num_args = input_permutation->size(); + const int num_retvals = output_permutation->size(); + + std::vector args; + std::vector retvals; + args.reserve(num_args); + retvals.reserve(num_retvals); + for (Node* n : graph->nodes()) { + if (n->type_string() == "_Arg") { + // Check if this is a guaranteed constant. + if (is_guaranteed_constant(*n)) { + return absl::InvalidArgumentError(absl::StrCat( + "Guaranteed constants are not supported (", n->name(), ")")); + } + args.push_back(n); + } else if (n->type_string() == "_Retval") { + retvals.push_back(n); + } + } + + if (std::find(args.begin(), args.end(), nullptr) != args.end()) { + return absl::InvalidArgumentError("Missing or non-consecutive arguments"); + } + + // Reorders the arguments. + std::sort(args.begin(), args.end(), [&](Node* a, Node* b) { + // Non-resources appear before resources + bool a_is_resource = (a->output_type(0) == DT_RESOURCE); + bool b_is_resource = (b->output_type(0) == DT_RESOURCE); + // Uses the name as a tiebreaker so the output is deterministic. + absl::string_view a_name(a->name()); + absl::string_view b_name(b->name()); + return std::tie(a_is_resource, a_name) < std::tie(b_is_resource, b_name); + }); + + // Sorts the retvals by name so the order is deterministic. + std::sort(retvals.begin(), retvals.end(), + [](Node* a, Node* b) { return a->name() < b->name(); }); + + // Computes the permutation to produce the correct argument order, and update + // the argument indices. + int variable_start_index = num_args; + for (int i = 0; i < num_args; ++i) { + int index; + TF_RETURN_IF_ERROR(GetIndexAttr(*args[i], num_args, &index)); + if (args[i]->output_type(0) == DT_RESOURCE && + variable_start_index == num_args) { + variable_start_index = i; + } + (*input_permutation)[index] = i; + args[i]->AddAttr("index", i); + } + VLOG(4) << "variable_start_index: " << variable_start_index; + + // Computes the permutation to produce the correct retval order, and update + // the argument indices. + for (int i = 0; i < num_retvals; ++i) { + int index; + TF_RETURN_IF_ERROR(GetIndexAttr(*retvals[i], num_retvals, &index)); + (*output_permutation)[index] = i; + retvals[i]->AddAttr("index", i); + } + + AddNodeAttr(kXlaClusterIdAttr, call_def->name(), call_def); + AddNodeAttr("_variable_start_index", variable_start_index, call_def); + + // Uniquify the function name by computing a fingerprint of the function. + // Nondeterminism in serialization would not lead to incorrect results, but + // may cause spurious cache misses. + TF_ASSIGN_OR_RETURN(uint64_t fingerprint, FingerprintGraph(*graph)); + VLOG(1) << "Subgraph fingerprint:" << fingerprint; + call_def->set_op(absl::StrCat(call_def->op(), "_", fingerprint)); + return absl::OkStatus(); +} + +} // namespace + +/*static*/ absl::Status EncapsulateXlaComputationsPass::Encapsulate( + std::unique_ptr* graph, FunctionLibraryDefinition* flib_def) { + // Check for undeclared outputs before Encapsulation, so we can give a better + // error message. + // TODO(phawkins): merge this with the encapsulation code to avoid the extra + // O(n) pass over the edges. + for (const Edge* e : (*graph)->edges()) { + if (!e->IsControlEdge() && + e->src()->attrs().Find(kXlaClusterIdAttr) != nullptr && + e->dst()->attrs().Find(kXlaClusterIdAttr) == nullptr && + e->dst()->type_string() != kXlaClusterOutput) { + return absl::InvalidArgumentError(absl::StrCat( + "Undeclared output of XLA computation. Some common causes of this " + "error are: 1) variable initializers that depend on the XLA " + "computation; 2) gradient computations that depend on the XLA " + "computation, which can be mitigated by moving gradient computations " + "inside XLA computation. Offending edge: ", + e->src()->name(), ":", e->src_output(), " -> ", e->dst()->name(), ":", + e->dst_input())); + } + } + + auto output = std::make_unique((*graph)->op_registry()); + TF_RETURN_WITH_CONTEXT_IF_ERROR( + EncapsulateSubgraphsInFunctions( + kXlaClusterIdAttr, **graph, RewriteSubgraph, + /*reuse_existing_functions=*/true, &output, flib_def), + "EncapsulateXlaComputationsPass failed"); + graph->swap(output); + return absl::OkStatus(); +} + +/*static*/ absl::Status EncapsulateXlaComputationsPass::BuildXlaLaunchOps( + Graph* graph, + const std::function(const Node&)>& is_xla_launch_node, + const std::function(const Node&)>& + get_xla_function_info, + const bool add_edges_to_output_of_downstream_nodes) { + // Finds all of the XlaLaunch function calls, to avoid mutating the graph + // while iterating. + std::vector launch_nodes; + for (Node* n : graph->nodes()) { + TF_ASSIGN_OR_RETURN(const bool is_xla_launch_node, is_xla_launch_node(*n)); + if (is_xla_launch_node) launch_nodes.push_back(n); + } + + // Replaces each launch function call together with its neighboring + // XlaClusterOutput nodes with a XlaLaunch node. + for (Node* launch : launch_nodes) { + TF_ASSIGN_OR_RETURN(const XlaFunctionInfo xla_function_info, + get_xla_function_info(*launch)); + + std::vector in_edges; + TF_RETURN_IF_ERROR(launch->input_edges(&in_edges)); + + const int num_inputs = in_edges.size(); + const int variable_start_index = xla_function_info.variable_start_index; + const int num_variables = num_inputs - variable_start_index; + const int num_args = variable_start_index; + + VLOG(4) << "Launch node '" << launch->name() << "'" + << " input edges: " << in_edges.size() << " num_args: " << num_args + << " num_variables: " << num_variables; + + std::vector nodes_to_remove = {launch}; + + // Data and control inputs to the new XlaLaunch node. + std::vector> data_inputs(num_inputs); + absl::flat_hash_set control_inputs; + DataTypeVector arg_types(num_args); + + AddControlInputs(*launch, &control_inputs); + + for (int i = 0; i < num_args; ++i) { + const Edge* edge = in_edges[i]; + data_inputs[i] = {edge->src(), edge->src_output()}; + arg_types[i] = EdgeType(edge); + } + + // Appends the variable inputs. + for (int i = 0; i < num_variables; ++i) { + int pos = variable_start_index + i; + const Edge* edge = in_edges[pos]; + data_inputs[pos] = {edge->src(), edge->src_output()}; + } + + // Outputs. + const int num_outputs = launch->output_types().size(); + absl::flat_hash_set control_outputs; + std::vector>> data_outputs(num_outputs); + const DataTypeVector& output_types(launch->output_types()); + + for (const Edge* le : launch->out_edges()) { + if (le->IsControlEdge()) { + control_outputs.insert(le->dst()); + } else { + TF_RET_CHECK(le->src_output() < num_outputs); + Node* output_node = le->dst(); + + if (add_edges_to_output_of_downstream_nodes) { + TF_RET_CHECK(output_node->type_string() == kXlaClusterOutput) + << le->DebugString(); + nodes_to_remove.push_back(output_node); + + for (const Edge* oe : output_node->out_edges()) { + TF_RET_CHECK(!oe->IsControlEdge()); + data_outputs[le->src_output()].push_back( + {oe->dst(), oe->dst_input()}); + } + + AddControlOutputs(*output_node, &control_outputs); + } else { + data_outputs[le->src_output()].push_back( + {le->dst(), le->dst_input()}); + } + } + } + + NodeDef def; + def.set_name(launch->name()); + MergeDebugInfo(NodeDebugInfo(launch->def()), &def); + + // Target the XLA CPU/GPU backends. + VLOG(2) << "Replacing with XlaLaunch"; + VLOG(2) << "Device is " << launch->requested_device(); + def.set_op("XlaLaunch"); + def.set_device(launch->requested_device()); + AddNodeAttr("Tconstants", DataTypeVector{}, &def); + AddNodeAttr("Targs", arg_types, &def); + AddNodeAttr("Nresources", num_variables, &def); + AddNodeAttr("Tresults", output_types, &def); + NameAttrList function; + function.set_name(xla_function_info.function_name); + AddNodeAttr("function", function, &def); + + for (Node* node : nodes_to_remove) { + VLOG(2) << "Deleting node " << node->DebugString(); + // Ensure that we do not attempt to add control edges to nodes that are + // deleted. + control_inputs.erase(node); + control_outputs.erase(node); + graph->RemoveNode(node); + } + + TF_ASSIGN_OR_RETURN(Node * xla_launch, graph->AddNode(def)); + for (int i = 0, end = data_inputs.size(); i < end; ++i) { + graph->AddEdge(data_inputs[i].first, data_inputs[i].second, xla_launch, + i); + } + for (Node* n : control_inputs) { + graph->AddControlEdge(n, xla_launch); + } + for (int i = 0, end = data_outputs.size(); i < end; ++i) { + for (const auto& successor : data_outputs[i]) { + graph->AddEdge(xla_launch, i, successor.first, successor.second); + } + } + for (Node* n : control_outputs) { + graph->AddControlEdge(xla_launch, n); + } + } + return absl::OkStatus(); +} + +/*static*/ absl::Status EncapsulateXlaComputationsPass::BuildXlaLaunchOps( + Graph* graph) { + const auto is_xla_launch_node = [](const Node& node) -> absl::StatusOr { + const std::string& name = + GetNodeAttrString(node.attrs(), kXlaClusterIdAttr); + return !name.empty(); + }; + + const auto get_xla_function_info = + [](const Node& node) -> absl::StatusOr { + XlaFunctionInfo result; + TF_RETURN_IF_ERROR(GetNodeAttr(node.attrs(), "_variable_start_index", + &result.variable_start_index)); + result.function_name = node.type_string(); + return result; + }; + return BuildXlaLaunchOps(graph, is_xla_launch_node, get_xla_function_info, + /*add_edges_to_output_of_downstream_nodes=*/true); +} + +absl::Status EncapsulateXlaComputationsPass::Run( + const GraphOptimizationPassOptions& options) { + VLOG(1) << "EncapsulateXlaComputations(): " + << DumpGraphToFile("encapsulate_xla_computations_before", + **options.graph, options.flib_def); + + const char* additional_help = + IsCpuGpuCompile(options.graph->get()) + ? xla::status_macros::kPossibleAutoJitAlternative + : ""; + + TF_RETURN_WITH_CONTEXT_IF_ERROR(Encapsulate(options.graph, options.flib_def), + additional_help); + VLOG(1) << "EncapsulateXlaComputations() half-way: " + << DumpGraphToFile("encapsulate_xla_computations_halfway", + **options.graph, options.flib_def); + + TF_RETURN_WITH_CONTEXT_IF_ERROR(BuildXlaLaunchOps(options.graph->get()), + additional_help); + VLOG(1) << "EncapsulateXlaComputations() finished: " + << DumpGraphToFile("encapsulate_xla_computations_after", + **options.graph, options.flib_def); + return absl::OkStatus(); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/encapsulate_xla_computations_pass.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/encapsulate_xla_computations_pass.h new file mode 100644 index 0000000000000000000000000000000000000000..91d0b1787aab621cd8045ac153a9dae18c869393 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/encapsulate_xla_computations_pass.h @@ -0,0 +1,85 @@ +/* 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. + ==============================================================================*/ +// Rewrites computations generated by the xla.compile() Python code into +// XlaLaunch nodes. +// +// xla.compile() does two main things: +// a) marks operators that make up an XLA computation with the attribute +// _xla_compile_id=XYZ, where XYZ is a unique key. +// b) adds XlaClusterOutput nodes to represent outputs of the computation. +// These nodes are not marked with the _xla_compile_id attribute. + +#ifndef TENSORFLOW_COMPILER_JIT_ENCAPSULATE_XLA_COMPUTATIONS_PASS_H_ +#define TENSORFLOW_COMPILER_JIT_ENCAPSULATE_XLA_COMPUTATIONS_PASS_H_ + +#include +#include + +#include "tensorflow/core/common_runtime/optimization_registry.h" +#include "tensorflow/core/graph/graph.h" +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/platform/statusor.h" + +namespace tensorflow { + +// Encapsulates nodes marked with the _xla_compile_id attribute into +// XlaLaunch operators. +class EncapsulateXlaComputationsPass : public GraphOptimizationPass { + public: + absl::Status Run(const GraphOptimizationPassOptions& options) override; + + // The following methods are public only for unit tests. + + // This pass has two stages: + // a) first, we call EncapsulateSubgraphsPass to encapsulate all nodes + // marked with the same _xla_compile_id attribute into functions. These + // functions contain the computations to be passed to XlaLaunch. During + // encapsulation, we sort the arguments into the order expected by + // XlaLaunch. + static absl::Status Encapsulate(std::unique_ptr* graph, + FunctionLibraryDefinition* flib_def); + + // b) we rewrite the function calls generated in phase (a) into XlaLaunch + // operators. We also convert the XlaClusterOutput output nodes of the + // function call into the outputs of the XlaLaunch operator. + static absl::Status BuildXlaLaunchOps(Graph* graph); + + struct XlaFunctionInfo { + int variable_start_index = -1; + std::string function_name; + }; + + // We need to introduce this version to adapt to the output of gpu inference + // converter. The single argument overload version calls this function. + // + // When add_edges_to_output_of_downstream_nodes is true, the output edges of + // the xla_launch_node's immediate downstream nodes would be attached to the + // generated xla node. For example, if the original graph is + // StatefulPartitionedCall{_xla_compile_id=1} -> XlaClusterOutput -> NodeA + // The output graph of this function would look like the following when + // add_edges_to_output_of_downstream_nodes is true: + // XlaLaunch -> NodeA + static absl::Status BuildXlaLaunchOps( + Graph* graph, + const std::function(const Node&)>& + is_xla_launch_node, + const std::function(const Node&)>& + get_xla_function_info, + bool add_edges_to_output_of_downstream_nodes); +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_ENCAPSULATE_XLA_COMPUTATIONS_PASS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/encapsulate_xla_computations_pass_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/encapsulate_xla_computations_pass_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..09e81df9fa56a8b3f808819575f9f75794e22ed4 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/encapsulate_xla_computations_pass_test.cc @@ -0,0 +1,356 @@ +/* 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 "tensorflow/compiler/jit/encapsulate_xla_computations_pass.h" + +#include +#include +#include +#include + +#include "absl/log/check.h" +#include "absl/status/status.h" +#include "tensorflow/cc/framework/ops.h" +#include "tensorflow/cc/framework/scope.h" +#include "tensorflow/cc/ops/array_ops.h" +#include "tensorflow/cc/ops/function_ops.h" +#include "tensorflow/cc/ops/math_ops.h" +#include "tensorflow/cc/ops/resource_variable_ops.h" +#include "tensorflow/compiler/jit/defs.h" +#include "tensorflow/compiler/jit/xla_cluster_util.h" +#include "tensorflow/compiler/tf2xla/cc/ops/xla_jit_ops.h" +#include "tensorflow/compiler/tf2xla/test_util.h" +#include "xla/tsl/lib/core/status_test_util.h" +#include "tensorflow/core/common_runtime/graph_constructor.h" +#include "tensorflow/core/framework/function.h" +#include "tensorflow/core/framework/graph_to_functiondef.h" +#include "tensorflow/core/framework/node_def_builder.h" +#include "tensorflow/core/framework/op.h" +#include "tensorflow/core/framework/types.h" +#include "tensorflow/core/graph/graph.h" +#include "tensorflow/core/platform/test.h" +#include "tensorflow/core/util/equal_graph_def.h" + +namespace tensorflow { + +static std::unique_ptr MakeOuterGraph( + const FunctionLibraryDefinition& flib_def, const std::string& function) { + Scope scope = Scope::NewRootScope().ExitOnError(); + TF_EXPECT_OK(scope.graph()->AddFunctionLibrary(flib_def.ToProto())); + + auto a = ops::Placeholder(scope.WithOpName("A"), DT_INT32); + auto b = ops::Placeholder(scope.WithOpName("B"), DT_FLOAT); + auto c = ops::Placeholder(scope.WithOpName("C"), DT_INT32); + auto d = ops::Placeholder(scope.WithOpName("D"), DT_FLOAT); + auto u = ops::Placeholder(scope.WithOpName("U"), DT_RESOURCE); + auto v = ops::Placeholder(scope.WithOpName("V"), DT_RESOURCE); + auto w = ops::Placeholder(scope.WithOpName("W"), DT_RESOURCE); + + NodeDef def; + CHECK_OK(NodeDefBuilder("launch0", function, &flib_def) + .Input(a.node()->name(), 0, DT_INT32) + .Input(b.node()->name(), 0, DT_FLOAT) + .Input(c.node()->name(), 0, DT_INT32) + .Input(d.node()->name(), 0, DT_FLOAT) + .Input(u.node()->name(), 0, DT_RESOURCE) + .Input(v.node()->name(), 0, DT_RESOURCE) + .Input(w.node()->name(), 0, DT_RESOURCE) + .Device("/gpu:0") + .Attr(kXlaClusterIdAttr, "launch0") + .Attr("_variable_start_index", 4) + .Finalize(&def)); + + absl::Status status; + Node* launch = scope.graph()->AddNode(def, &status); + CHECK_OK(status); + CHECK_OK(scope.DoShapeInference(launch)); + scope.graph()->AddEdge(a.node(), 0, launch, 0); + scope.graph()->AddEdge(b.node(), 0, launch, 1); + scope.graph()->AddEdge(c.node(), 0, launch, 2); + scope.graph()->AddEdge(d.node(), 0, launch, 3); + scope.graph()->AddEdge(u.node(), 0, launch, 4); + scope.graph()->AddEdge(v.node(), 0, launch, 5); + scope.graph()->AddEdge(w.node(), 0, launch, 6); + + auto out0 = + ops::XlaClusterOutput(scope.WithOpName("Out0"), Output(launch, 0)); + auto out1 = + ops::XlaClusterOutput(scope.WithOpName("Out1"), Output(launch, 1)); + auto out2 = + ops::XlaClusterOutput(scope.WithOpName("Out2"), Output(launch, 2)); + auto out3 = + ops::XlaClusterOutput(scope.WithOpName("Out3"), Output(launch, 3)); + + auto consumer0_a = ops::Identity(scope.WithOpName("consumer0_a"), out0); + auto consumer0_b = ops::Identity(scope.WithOpName("consumer0_b"), out0); + auto consumer0_c = ops::Identity(scope.WithOpName("consumer0_c"), out0); + auto consumer1 = ops::Identity(scope.WithOpName("consumer1"), out1); + auto consumer2 = ops::Identity(scope.WithOpName("consumer2"), out2); + auto consumer3 = ops::Identity(scope.WithOpName("consumer3"), out3); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + CHECK_OK(scope.ToGraph(graph.get())); + return graph; +} + +// Makes an encapsulate body graph for use in tests. +static std::unique_ptr MakeBodyGraph() { + Scope scope = Scope::NewRootScope().ExitOnError(); + + auto arg0 = ops::_Arg(scope.WithOpName("a_0_arg"), DT_INT32, 0); + auto arg1 = ops::_Arg(scope.WithOpName("b_0_arg"), DT_FLOAT, 1); + auto arg2 = ops::_Arg(scope.WithOpName("c_0_arg"), DT_INT32, 2); + auto arg3 = ops::_Arg(scope.WithOpName("d_0_arg"), DT_FLOAT, 3); + + auto arg4 = ops::_Arg(scope.WithOpName("u_0_arg"), DT_RESOURCE, 4); + auto arg5 = ops::_Arg(scope.WithOpName("v_0_arg"), DT_RESOURCE, 5); + auto arg6 = ops::_Arg(scope.WithOpName("w_0_arg"), DT_RESOURCE, 6); + + auto add_attrs = [](Node* node) { + node->AddAttr(kXlaClusterIdAttr, "launch0"); + node->set_requested_device("/gpu:0"); + }; + + auto b_identity = ops::Identity(scope.WithOpName("B_identity"), arg1); + add_attrs(b_identity.node()); + auto read_u = ops::ReadVariableOp(scope.WithOpName("ReadU"), arg4, DT_FLOAT); + add_attrs(read_u.node()); + auto read_v = ops::ReadVariableOp(scope.WithOpName("ReadV"), arg5, DT_FLOAT); + add_attrs(read_v.node()); + auto read_w = ops::ReadVariableOp(scope.WithOpName("ReadW"), arg6, DT_FLOAT); + add_attrs(read_w.node()); + + auto e = ops::Add(scope.WithOpName("E"), arg0, arg2); + add_attrs(e.node()); + auto f = ops::Add(scope.WithOpName("F"), read_v, read_w); + add_attrs(f.node()); + auto g = ops::Add(scope.WithOpName("G"), f, arg3); + add_attrs(g.node()); + + auto out0 = ops::_Retval(scope.WithOpName("b_identity_0_retval_RetVal"), + b_identity, 0); + auto out1 = ops::_Retval(scope.WithOpName("e_0_retval_RetVal"), e, 1); + auto out2 = ops::_Retval(scope.WithOpName("g_0_retval_RetVal"), g, 2); + auto out3 = + ops::_Retval(scope.WithOpName("readu_0_retval_RetVal"), read_u, 3); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + CHECK_OK(scope.ToGraph(graph.get())); + return graph; +} + +TEST(EncapsulateXlaComputations, DeterministicEncapsulate) { + // Test that control edge insertion order doesn't affect the cache key + // (cluster name) generated by TPU encapsulate pass. + auto get_serialized_graph = [](bool control_input_reversed, + bool operand_reversed) -> std::string { + FunctionLibraryDefinition flib_def(OpRegistry::Global(), + FunctionDefLibrary()); + std::unique_ptr graph(new Graph(&flib_def)); + { + Scope scope = Scope::NewRootScope().ExitOnError(); + auto a0 = ops::Placeholder(scope.WithOpName("A0"), DT_INT32); + auto a1 = ops::Placeholder(scope.WithOpName("A1"), DT_INT32); + + ops::Add e = operand_reversed ? ops::Add(scope.WithOpName("E"), a0, a1) + : ops::Add(scope.WithOpName("E"), a1, a0); + + auto add_attrs = [](Node* node) { + node->AddAttr(kXlaClusterIdAttr, "launch0"); + }; + add_attrs(e.node()); + + CHECK_OK(scope.ToGraph(graph.get())); + auto get_node_in_graph = [&graph](Node* node) { + return graph->FindNodeId(node->id()); + }; + // Insert control edge in different order. The order should not affect + // the encapsulated or serialized graph. + if (!control_input_reversed) { + graph->AddControlEdge(get_node_in_graph(a0.node()), + get_node_in_graph(e.node()), true); + graph->AddControlEdge(get_node_in_graph(a1.node()), + get_node_in_graph(e.node()), true); + } else { + graph->AddControlEdge(get_node_in_graph(a1.node()), + get_node_in_graph(e.node()), true); + graph->AddControlEdge(get_node_in_graph(a0.node()), + get_node_in_graph(e.node()), true); + } + } + CHECK_OK(EncapsulateXlaComputationsPass::Encapsulate(&graph, &flib_def)); + return SerializeGraphDeterministic(*graph).value(); + }; + + // Changing the order of control input shouldn't affect the graph generated. + EXPECT_EQ(get_serialized_graph(/*control_input_reversed=*/true, + /*operand_reversed=*/false), + get_serialized_graph(/*control_input_reversed=*/false, + /*operand_reversed=*/false)); + + // Changing the order of data input should affect the graph generated. + EXPECT_NE(get_serialized_graph(/*control_input_reversed=*/false, + /*operand_reversed=*/true), + get_serialized_graph(/*control_input_reversed=*/false, + /*operand_reversed=*/false)); +} + +TEST(EncapsulateXlaComputations, Encapsulate) { + FunctionLibraryDefinition flib_def(OpRegistry::Global(), + FunctionDefLibrary()); + std::unique_ptr graph(new Graph(&flib_def)); + { + Scope scope = Scope::NewRootScope().ExitOnError(); + auto a = ops::Placeholder(scope.WithOpName("A"), DT_INT32); + auto b = ops::Placeholder(scope.WithOpName("B"), DT_FLOAT); + auto c = ops::Placeholder(scope.WithOpName("C"), DT_INT32); + auto d = ops::Placeholder(scope.WithOpName("D"), DT_FLOAT); + auto u = ops::Placeholder(scope.WithOpName("U"), DT_RESOURCE); + auto v = ops::Placeholder(scope.WithOpName("V"), DT_RESOURCE); + auto w = ops::Placeholder(scope.WithOpName("W"), DT_RESOURCE); + + auto add_attrs = [](Node* node) { + node->AddAttr(kXlaClusterIdAttr, "launch0"); + node->set_requested_device("/gpu:0"); + }; + + auto b_identity = ops::Identity(scope.WithOpName("B_identity"), b); + add_attrs(b_identity.node()); + + auto read_u = ops::ReadVariableOp(scope.WithOpName("ReadU"), u, DT_FLOAT); + add_attrs(read_u.node()); + auto read_v = ops::ReadVariableOp(scope.WithOpName("ReadV"), v, DT_FLOAT); + add_attrs(read_v.node()); + auto read_w = ops::ReadVariableOp(scope.WithOpName("ReadW"), w, DT_FLOAT); + add_attrs(read_w.node()); + + auto e = ops::Add(scope.WithOpName("E"), a, c); + add_attrs(e.node()); + auto f = ops::Add(scope.WithOpName("F"), read_v, read_w); + add_attrs(f.node()); + auto g = ops::Add(scope.WithOpName("G"), f, d); + add_attrs(g.node()); + + auto out0 = ops::XlaClusterOutput(scope.WithOpName("Out0"), b_identity); + auto out1 = ops::XlaClusterOutput(scope.WithOpName("Out1"), e); + auto out2 = ops::XlaClusterOutput(scope.WithOpName("Out2"), g); + auto out3 = ops::XlaClusterOutput(scope.WithOpName("Out3"), read_u); + + auto consumer0_a = ops::Identity(scope.WithOpName("consumer0_a"), out0); + auto consumer0_b = ops::Identity(scope.WithOpName("consumer0_b"), out0); + auto consumer0_c = ops::Identity(scope.WithOpName("consumer0_c"), out0); + auto consumer1 = ops::Identity(scope.WithOpName("consumer1"), out1); + auto consumer2 = ops::Identity(scope.WithOpName("consumer2"), out2); + auto consumer3 = ops::Identity(scope.WithOpName("consumer3"), out3); + TF_ASSERT_OK(scope.ToGraph(graph.get())); + } + + std::unique_ptr graph_copy(new Graph(&flib_def)); + CopyGraph(*graph, graph_copy.get()); + + TF_ASSERT_OK(EncapsulateXlaComputationsPass::Encapsulate(&graph, &flib_def)); + + std::unordered_map index = graph->BuildNodeNameIndex(); + std::string function = index.at("launch0")->type_string(); + + // Tests the outer graph is as expected. + { + std::unique_ptr outer = MakeOuterGraph(flib_def, function); + GraphDef expected_def; + outer->ToGraphDef(&expected_def); + + GraphDef actual_def; + graph->ToGraphDef(&actual_def); + TF_EXPECT_GRAPH_EQ_INTERNAL(expected_def, actual_def); + } + + // Tests the encapsulated body graph is as expected. + { + std::unique_ptr body = MakeBodyGraph(); + GraphDef expected_body_def; + body->ToGraphDef(&expected_body_def); + + InstantiationResultForTest result; + TF_EXPECT_OK(InstantiateFunctionForTest(function, flib_def, &result)); + + EXPECT_EQ((DataTypeVector{DT_INT32, DT_FLOAT, DT_INT32, DT_FLOAT, + DT_RESOURCE, DT_RESOURCE, DT_RESOURCE}), + result.arg_types); + EXPECT_EQ((DataTypeVector{DT_FLOAT, DT_INT32, DT_FLOAT, DT_FLOAT}), + result.ret_types); + TF_EXPECT_GRAPH_EQ(expected_body_def, result.gdef); + } + + // Encapsulates the same computation again, verifies we reuse the same + // function. Encapsulation should be deterministic to avoid recompilation. + TF_ASSERT_OK( + EncapsulateXlaComputationsPass::Encapsulate(&graph_copy, &flib_def)); + std::unordered_map index_copy = + graph_copy->BuildNodeNameIndex(); + std::string function_copy = index_copy.at("launch0")->type_string(); + EXPECT_EQ(function, function_copy); +} + +TEST(EncapsulateXlaComputations, BuildXlaLaunchOp) { + std::unique_ptr body_graph = MakeBodyGraph(); + FunctionDefLibrary flib; + TF_ASSERT_OK(GraphToFunctionDef(*body_graph, "launch0", flib.add_function())); + + FunctionLibraryDefinition flib_def(OpRegistry::Global(), flib); + + std::unique_ptr graph = MakeOuterGraph(flib_def, "launch0"); + TF_ASSERT_OK(EncapsulateXlaComputationsPass::BuildXlaLaunchOps(graph.get())); + + Scope scope = Scope::DisabledShapeInferenceScope().ExitOnError(); + TF_EXPECT_OK(scope.graph()->AddFunctionLibrary(flib)); + + auto a = ops::Placeholder(scope.WithOpName("A"), DT_INT32); + auto b = ops::Placeholder(scope.WithOpName("B"), DT_FLOAT); + auto c = ops::Placeholder(scope.WithOpName("C"), DT_INT32); + auto d = ops::Placeholder(scope.WithOpName("D"), DT_FLOAT); + auto u = ops::Placeholder(scope.WithOpName("U"), DT_RESOURCE); + auto v = ops::Placeholder(scope.WithOpName("V"), DT_RESOURCE); + auto w = ops::Placeholder(scope.WithOpName("W"), DT_RESOURCE); + + NameAttrList function; + function.set_name("launch0"); + auto launch = ops::XlaLaunch( + scope.WithOpName("launch0").WithDevice("/gpu:0"), + std::initializer_list{}, std::initializer_list{a, b, c, d}, + std::initializer_list{u, v, w}, + DataTypeVector{DT_FLOAT, DT_INT32, DT_FLOAT, DT_FLOAT}, function); + + auto consumer0_a = + ops::Identity(scope.WithOpName("consumer0_a"), launch.results[0]); + auto consumer0_b = + ops::Identity(scope.WithOpName("consumer0_b"), launch.results[0]); + auto consumer0_c = + ops::Identity(scope.WithOpName("consumer0_c"), launch.results[0]); + auto consumer1 = + ops::Identity(scope.WithOpName("consumer1"), launch.results[1]); + auto consumer2 = + ops::Identity(scope.WithOpName("consumer2"), launch.results[2]); + auto consumer3 = + ops::Identity(scope.WithOpName("consumer3"), launch.results[3]); + + GraphDef expected_def; + TF_ASSERT_OK(scope.ToGraphDef(&expected_def)); + + GraphDef actual_def; + graph->ToGraphDef(&actual_def); + TF_EXPECT_GRAPH_EQ(expected_def, actual_def); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/extract_outside_compilation_pass.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/extract_outside_compilation_pass.cc new file mode 100644 index 0000000000000000000000000000000000000000..cc5ea15a0160ddf08ef576e8cda5233938ab511b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/extract_outside_compilation_pass.cc @@ -0,0 +1,2588 @@ +/* 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 "tensorflow/compiler/jit/extract_outside_compilation_pass.h" + +#include "absl/container/flat_hash_map.h" +#include "absl/strings/match.h" +#include "absl/strings/str_cat.h" +#include "tensorflow/compiler/jit/encapsulate_subgraphs_pass.h" +#include "tensorflow/compiler/jit/encapsulate_util.h" +#include "tensorflow/compiler/tf2xla/side_effect_util.h" +#include "tensorflow/compiler/tf2xla/tf2xla_util.h" +#include "xla/status_macros.h" +#include "xla/xla_data.pb.h" +#include "tensorflow/core/common_runtime/function.h" +#include "tensorflow/core/framework/function.h" +#include "tensorflow/core/framework/graph_to_functiondef.h" +#include "tensorflow/core/framework/node_def_builder.h" +#include "tensorflow/core/framework/node_def_util.h" +#include "tensorflow/core/framework/tensor_shape.pb.h" +#include "tensorflow/core/graph/algorithm.h" +#include "tensorflow/core/lib/core/errors.h" +#include "tensorflow/core/lib/gtl/cleanup.h" +#include "tensorflow/core/platform/macros.h" +#include "tensorflow/core/util/dump_graph.h" + +namespace tensorflow { + +namespace { + +// Control return mapping function for outside compilation host graphs. +// All nodes with kXlaHasHostTransfer attribute are control outputs. +std::optional HostGraphControlRetMapping(const Node* n) { + if (HasNodeAttr(n->def(), kXlaHasHostTransferAttrName)) { + return n->name(); + } + return std::nullopt; +} + +// Add a key placeholder node to the graph. The key placeholder node will be +// used as input for XlaRecvAtHost/XlaSendFromHost nodes. +absl::StatusOr AddHostComputeKeyPlaceholder( + const std::string& xla_cluster_name, Graph* g) { + NodeDef key_def; + NodeDefBuilder builder(absl::StrCat(xla_cluster_name, "_key_placeholder"), + "Placeholder"); + builder.Attr("dtype", DT_STRING); + builder.Attr("shape", PartialTensorShape({2})); + builder.Attr("_host_compute_call_node", xla_cluster_name); + absl::Status s = builder.Finalize(&key_def); + if (!s.ok()) return s; + + Node* n = g->AddNode(key_def, &s); + if (!s.ok()) return s; + return n; +} + +// Returns if the node is a XLA computation key placeholder. +bool IsKeyPlaceholderNode(const Node& n) { + return n.type_string() == "Placeholder" && + absl::EndsWith(n.name(), "_key_placeholder"); +} + +// Returns nodes with given type. +std::vector GatherNodesWithType(const Graph& g, + const std::string& type) { + std::vector result; + for (Node* n : g.nodes()) { + if (n->type_string() == type) { + result.push_back(n); + } + } + return result; +} + +// Gets data types from `arg_nodes` and fills them into `recv_at_host_dtypes`. +absl::Status GetArgDataTypes(const std::vector& arg_nodes, + std::vector* recv_at_host_dtypes) { + recv_at_host_dtypes->resize(arg_nodes.size(), DT_INVALID); + for (auto* n : arg_nodes) { + int index; + TF_RETURN_IF_ERROR(GetNodeAttr(n->attrs(), "index", &index)); + DataType dtype; + TF_RETURN_IF_ERROR(GetNodeAttr(n->attrs(), "T", &dtype)); + (*recv_at_host_dtypes)[index] = dtype; + } + for (int i = 0, end = recv_at_host_dtypes->size(); i < end; i++) { + if ((*recv_at_host_dtypes)[i] == DT_INVALID) { + return absl::InternalError( + absl::StrCat("Cannot get datatype for input ", i)); + } + } + return absl::OkStatus(); +} + +// Builds XlaRecvAtHost node. +absl::StatusOr BuildRecvAtHostNode( + Graph* g, const std::string& oc_cluster_name, + const std::vector& recv_at_host_dtypes, Node* key_placeholder) { + NodeDefBuilder recv_at_host_builder( + absl::StrCat("outside_compilation_", oc_cluster_name, "_recv"), + "_XlaRecvAtHost"); + NodeDef recv_at_host_def; + recv_at_host_builder.Attr("Toutputs", recv_at_host_dtypes); + // The correct device_ordinal will be inserted during replication in a + // subsequent rewrite. + AttrValue device_ordinal_value; + device_ordinal_value.set_placeholder("_device_ordinal"); + recv_at_host_builder.Attr("device_ordinal", device_ordinal_value); + recv_at_host_builder.Attr( + "key", absl::StrCat("host_compute_channel_", oc_cluster_name)); + recv_at_host_builder.Attr(kXlaHasHostTransferAttrName, true); + recv_at_host_builder.Input(key_placeholder->name(), 0, DT_STRING); + TF_RETURN_IF_ERROR(recv_at_host_builder.Finalize(&recv_at_host_def)); + TF_ASSIGN_OR_RETURN(Node * recv_at_host_node, g->AddNode(recv_at_host_def)); + return recv_at_host_node; +} + +// Builds XlaRecvAtHost node, and replaces all _Arg nodes with it. +absl::StatusOr ReplaceArgNodesWithRecvAtHostNode( + Graph* g, const std::string& oc_cluster_name, + std::vector* recv_at_host_dtypes, Node* key_placeholder) { + // TODO(b/77601805): use out nodes for source node, instead of traversing all + // nodes. + std::vector arg_nodes = GatherNodesWithType(*g, "_Arg"); + TF_RETURN_IF_ERROR(GetArgDataTypes(arg_nodes, recv_at_host_dtypes)); + TF_ASSIGN_OR_RETURN( + Node * recv_at_host_node, + BuildRecvAtHostNode(g, oc_cluster_name, *recv_at_host_dtypes, + key_placeholder)); + for (auto* n : arg_nodes) { + int index; + TF_RETURN_IF_ERROR(GetNodeAttr(n->attrs(), "index", &index)); + // Record out edges and remove `n` before adding those edges to RecvAtHost. + // This is to avoid multiple producers. + std::vector out_edge_info; + out_edge_info.reserve(n->out_edges().size()); + for (auto edge : n->out_edges()) { + out_edge_info.push_back( + {edge->dst(), edge->src_output(), edge->dst_input()}); + } + g->RemoveNode(n); + for (const OutEdgeInfo& edge : out_edge_info) { + if (edge.dst_input == Graph::kControlSlot) { + g->AddControlEdge(recv_at_host_node, edge.dst); + } else { + g->AddEdge(recv_at_host_node, index, edge.dst, edge.dst_input); + } + } + + // Rewrite dst nodes because their input changed. + for (int i = 0, end = out_edge_info.size(); i < end; i++) { + const OutEdgeInfo edge = out_edge_info[i]; + if (edge.dst_input == Graph::kControlSlot) { + continue; + } + + Node* dst = edge.dst; + NodeDef new_def = dst->def(); + *new_def.mutable_input(edge.dst_input) = + absl::StrCat(recv_at_host_node->name(), ":", index); + TF_ASSIGN_OR_RETURN(Node * dst_replace, ReplaceNode(g, dst, new_def)); + + // Other edges might have `dst` as dst node as well. Update those edges + // with `dst_replace`. + for (int j = i + 1, end = out_edge_info.size(); j < end; j++) { + if (out_edge_info[j].dst == dst) { + out_edge_info[j].dst = dst_replace; + } + } + } + } + g->AddEdge(key_placeholder, 0, recv_at_host_node, 0); + return recv_at_host_node; +} + +// Gets data types from `ret_nodes` and fills them into `send_from_host_dtypes`. +absl::Status GetRetDataTypes(const std::vector& ret_nodes, + std::vector* send_from_host_dtypes) { + send_from_host_dtypes->resize(ret_nodes.size(), DT_INVALID); + for (auto* n : ret_nodes) { + int index; + TF_RETURN_IF_ERROR(GetNodeAttr(n->attrs(), "index", &index)); + DataType dtype; + TF_RETURN_IF_ERROR(GetNodeAttr(n->attrs(), "T", &dtype)); + (*send_from_host_dtypes)[index] = dtype; + } + for (int i = 0, end = send_from_host_dtypes->size(); i < end; i++) { + if ((*send_from_host_dtypes)[i] == DT_INVALID) { + return absl::InternalError( + absl::StrCat("Cannot get datatype for output ", i)); + } + } + return absl::OkStatus(); +} + +// Builds XlaSendFromHost node. +absl::StatusOr BuildSendFromHostNode( + Graph* g, const std::string& oc_cluster_name, + const std::vector& ret_nodes, + const std::vector& send_from_host_dtypes, Node* key_placeholder) { + NodeDefBuilder send_from_host_builder( + absl::StrCat("outside_compilation_", oc_cluster_name, "_send"), + "_XlaSendFromHost"); + NodeDef send_from_host_def; + send_from_host_builder.Attr("Tinputs", send_from_host_dtypes); + // The correct device_ordinal will be inserted during replication in a + // subsequent rewrite. + AttrValue device_ordinal_value; + device_ordinal_value.set_placeholder("_device_ordinal"); + send_from_host_builder.Attr("device_ordinal", device_ordinal_value); + send_from_host_builder.Attr( + "key", absl::StrCat("host_compute_channel_", oc_cluster_name)); + send_from_host_builder.Attr(kXlaHasHostTransferAttrName, true); + std::vector inputs(send_from_host_dtypes.size()); + for (auto* n : ret_nodes) { + int index; + TF_RETURN_IF_ERROR(GetNodeAttr(n->attrs(), "index", &index)); + const int num_dtypes = send_from_host_dtypes.size(); + if (index < 0 || index >= num_dtypes) { + return absl::InternalError( + absl::StrCat("Invalid _Retval index: ", index)); + } + for (auto edge : n->in_edges()) { + inputs[index] = + NodeDefBuilder::NodeOut{edge->src()->name(), edge->src_output(), + edge->src()->output_type(edge->src_output())}; + } + } + send_from_host_builder.Input(inputs); + send_from_host_builder.Input(key_placeholder->name(), 0, DT_STRING); + TF_RETURN_IF_ERROR(send_from_host_builder.Finalize(&send_from_host_def)); + TF_ASSIGN_OR_RETURN(Node * send_from_host_node, + g->AddNode(send_from_host_def)); + return send_from_host_node; +} + +// Builds XlaSendFromHost node, and replaces all _Retval nodes with it. +absl::StatusOr ReplaceRetNodesWithSendFromHostNode( + Graph* g, const std::string& oc_cluster_name, + std::vector* send_from_host_dtypes, Node* key_placeholder) { + // TODO(b/77601805): use in nodes for sink node, instead of traversing all + // nodes. + std::vector ret_nodes = GatherNodesWithType(*g, "_Retval"); + TF_RETURN_IF_ERROR(GetRetDataTypes(ret_nodes, send_from_host_dtypes)); + TF_ASSIGN_OR_RETURN( + Node * send_from_host_node, + BuildSendFromHostNode(g, oc_cluster_name, ret_nodes, + *send_from_host_dtypes, key_placeholder)); + for (auto* n : ret_nodes) { + int index; + TF_RETURN_IF_ERROR(GetNodeAttr(n->attrs(), "index", &index)); + for (auto edge : n->in_edges()) { + if (edge->src_output() == Graph::kControlSlot) { + g->AddControlEdge(edge->src(), send_from_host_node); + } else { + g->AddEdge(edge->src(), edge->src_output(), send_from_host_node, index); + } + } + g->RemoveNode(n); + } + g->AddEdge(key_placeholder, 0, send_from_host_node, + send_from_host_dtypes->size()); + return send_from_host_node; +} + +// Returns input shapes (excluding key placeholder) for `send_from_host_node` +// if they are all fully defined; std::nullopt otherwise. +std::optional> GetInferredInputShapes( + int num_inputs, Node* send_from_host_node) { + std::vector results(num_inputs); + for (int i = 0; i < num_inputs; i++) { + const Edge* e; + if (!send_from_host_node->input_edge(i, &e).ok()) { + return std::nullopt; + } + + std::vector shapes; + if (!GetNodeAttr(e->src()->attrs(), kXlaInferredShapesAttrName, &shapes) + .ok()) { + return std::nullopt; + } + + const PartialTensorShape shape = shapes[e->src_output()]; + if (!shape.IsFullyDefined()) { + return std::nullopt; + } + + results[e->dst_input()] = shape; + } + return results; +} + +std::string host_compute_node_name(const std::string& original_oc_name) { + return absl::StrCat("outside_compilation_", original_oc_name, + "_host_compute"); +} + +// Builds XlaHostCompute NodeDef from the outside compilation call node. +absl::StatusOr BuildXlaHostComputeNodeDef( + const Node* call_node, const std::map& host_compute_core, + const absl::flat_hash_map>& + cluster_deps) { + std::string original_oc_name; + TF_RETURN_IF_ERROR(GetNodeAttr( + call_node->attrs(), "_outside_compilation_subgraph", &original_oc_name)); + NodeDefBuilder host_compute_builder(host_compute_node_name(original_oc_name), + "XlaHostCompute"); + // In XlaCompiler, if XlaHostCompute node is in a function call node and that + // function is inlined, name of the XlaHostCompute node will be changed. So + // we cannot rely on node name; use an attribute instead. + host_compute_builder.Attr(kXlaOriginalOutsideCompilationNodeName, + host_compute_builder.node_name()); + + // Copy all attributes. + for (const auto& attr : call_node->attrs()) { + host_compute_builder.Attr(attr.first, attr.second); + } + + // Populate tpu_core assignment. + const auto iter = host_compute_core.find(original_oc_name); + if (iter != host_compute_core.end()) { + int core = iter->second; + host_compute_builder.Attr("tpu_core", core); + } + + // Set input tokens and other outside compilation clusters that current + // cluster depends in `kXlaTokenArgNodeName`. This is needed because when + // outside compilation subgraphs are encapsulated and moved to host graph, + // control/data edges between them will only be reflected in host graph. + // From XLA's perspective, two originally dependent clusters are no longer + // connected, which makes them look like they can be scheduled for execution + // in arbitrary order even though in fact they must be executed in order + // according to their host-side graph dependency. This can cause deadlock. + // Therefore, we hint XLA what the correct ordering of these clusters should + // be to avoid deadlocks. + std::vector xla_token_input_nodes; + xla_token_input_nodes.emplace_back(kXlaTokenArgNodeName); + auto cluster_deps_it = cluster_deps.find(original_oc_name); + if (cluster_deps_it != cluster_deps.end()) { + for (const auto& dep : cluster_deps_it->second) { + xla_token_input_nodes.emplace_back(host_compute_node_name(dep)); + } + } + host_compute_builder.Attr(kXlaTokenInputNodesAttrName, xla_token_input_nodes); + + // Populate inputs. + std::vector input_dtypes; + TF_RETURN_IF_ERROR(GetNodeAttr(call_node->attrs(), "Tinputs", &input_dtypes)); + std::vector inputs(input_dtypes.size()); + for (auto e : call_node->in_edges()) { + if (e->IsControlEdge()) { + continue; + } + + const int input_dtypes_size = input_dtypes.size(); + if (e->dst_input() < 0 || e->dst_input() >= input_dtypes_size) { + return absl::InternalError( + absl::StrCat("Invalid dst_input: ", e->dst_input())); + } + inputs[e->dst_input()] = NodeDefBuilder::NodeOut{ + e->src()->name(), e->src_output(), input_dtypes[e->dst_input()]}; + } + host_compute_builder.Input(inputs); + + NodeDef new_def; + TF_RETURN_IF_ERROR(host_compute_builder.Finalize(&new_def)); + return new_def; +} + +// Replace outside compilation function call node with XlaHostCompute node. +TF_ATTRIBUTE_NOINLINE absl::StatusOr ReplaceOutsideCompilationCallNode( + Graph* g, Node* call_node, + const std::map& host_compute_core, + const absl::flat_hash_map>& + cluster_deps) { + // Build XlaHostCompute NodeDef. + TF_ASSIGN_OR_RETURN( + NodeDef node_def, + BuildXlaHostComputeNodeDef(call_node, host_compute_core, cluster_deps)); + TF_ASSIGN_OR_RETURN(Node * host_compute_node, + ReplaceNode(g, call_node, node_def)); + VLOG(4) << "Added HostCompute node: " << host_compute_node->DebugString(); + + return host_compute_node; +} + +// Resets "_device_ordinal" attr to placeholder value for related nodes +// (XlaRecvAtHost nodes; XlaSendFromHost nodes; If/While/FuncCall nodes +// containing XlaRecvAtHost/XlaSendFromHost). +absl::Status ResetDeviceOrdinalToPlaceholderValue(Graph* g) { + AttrValue device_ordinal_value; + device_ordinal_value.set_placeholder("_device_ordinal"); + for (Node* n : g->nodes()) { + if (!HasNodeAttr(n->def(), kXlaHasHostTransferAttrName)) { + continue; + } + + if (n->type_string() == "_XlaRecvAtHost" || + n->type_string() == "_XlaSendFromHost") { + n->ClearAttr("device_ordinal"); + n->AddAttr("device_ordinal", device_ordinal_value); + } else if (n->IsIfNode()) { + for (const std::string& attr_name : + std::vector{"then_branch", "else_branch"}) { + NameAttrList branch_func; + TF_RETURN_IF_ERROR(GetNodeAttr(n->attrs(), attr_name, &branch_func)); + (*branch_func.mutable_attr())["_device_ordinal"] = device_ordinal_value; + n->ClearAttr(attr_name); + n->AddAttr(attr_name, branch_func); + } + } else if (n->IsWhileNode()) { + for (const std::string& attr_name : + std::vector{"cond", "body"}) { + NameAttrList branch_func; + TF_RETURN_IF_ERROR(GetNodeAttr(n->attrs(), attr_name, &branch_func)); + (*branch_func.mutable_attr())["_device_ordinal"] = device_ordinal_value; + n->ClearAttr(attr_name); + n->AddAttr(attr_name, branch_func); + } + } else if (HasNodeAttr(n->def(), "_device_ordinal")) { + // Function call node containing outside compilation. + n->ClearAttr("_device_ordinal"); + n->AddAttr("_device_ordinal", device_ordinal_value); + } else { + return absl::InternalError(absl::StrCat("Unknown node marked with ", + kXlaHasHostTransferAttrName, ": ", + n->DebugString())); + } + } + return absl::OkStatus(); +} + +// Cheap check to tell whether FunctionDef contains a lifted argument. +bool HasLiftedArgs(const FunctionDef& function_def) { + return absl::c_any_of(function_def.node_def(), [](const NodeDef& node_def) { + return (node_def.op() == "Placeholder" && + node_def.attr().find(kXlaLiftedArgOutsideCompilationAttrName) != + node_def.attr().end()); + }); +} + +// Find lifted arguments in a function body and their corresponding outside +// compilation nodes. +absl::StatusOr>> +LiftedArgsAndOutsideCompilationNodesInFunctionBody( + const FunctionBody& function_body, + const std::unordered_map& + outside_compilation_attr_to_node) { + std::vector> + lifted_arg_nodes_and_outside_compilation_nodes; + for (Node* n : function_body.graph->op_nodes()) { + std::string oc_cluster; + if (n->type_string() == "Placeholder" && + GetNodeAttr(n->def(), kXlaLiftedArgOutsideCompilationAttrName, + &oc_cluster) + .ok()) { + TF_RET_CHECK(outside_compilation_attr_to_node.find(oc_cluster) != + outside_compilation_attr_to_node.end()); + lifted_arg_nodes_and_outside_compilation_nodes.emplace_back( + n, outside_compilation_attr_to_node.at(oc_cluster)); + } + } + return lifted_arg_nodes_and_outside_compilation_nodes; +} + +// Append lifted args' types to functional control flow node's `type_attr_name` +// attribute. +absl::StatusOr> UpdateTypesAttribute( + const std::vector>& + lifted_arg_nodes_and_outside_compilation_nodes, + const std::string& type_attr_name, Node* n) { + std::vector data_types; + data_types.reserve(lifted_arg_nodes_and_outside_compilation_nodes.size()); + TF_RETURN_IF_ERROR(GetNodeAttr(n->def(), type_attr_name, &data_types)); + for (auto pair : lifted_arg_nodes_and_outside_compilation_nodes) { + Node* outside_compilation_node = pair.second; + DataType data_type; + TF_RET_CHECK(outside_compilation_node->IsIdentity() || + outside_compilation_node->type_string() == "Placeholder"); + if (outside_compilation_node->IsIdentity()) { + TF_RETURN_IF_ERROR( + GetNodeAttr(outside_compilation_node->def(), "T", &data_type)); + } else { + TF_RETURN_IF_ERROR( + GetNodeAttr(outside_compilation_node->def(), "dtype", &data_type)); + } + data_types.push_back(data_type); + } + n->ClearAttr(type_attr_name); + n->AddAttr(type_attr_name, data_types); + + return data_types; +} + +// Add edges from lifted outside compilation argument nodes to `n` in Graph `g`. +void AddEdgesFromOutsideCompilationNodes( + const int original_arg_count, const int arg_to_input_edge_offset, + const std::vector& data_types, + const std::vector& outside_compilation_nodes, Graph* g, Node* n) { + // Add edges from outside compilation nodes to While node. + for (int i = original_arg_count, end = data_types.size(); i < end; i++) { + Node* outside_compilation_node = + outside_compilation_nodes[i - original_arg_count]; + g->AddEdge(outside_compilation_node, 0, n, i + arg_to_input_edge_offset); + } +} + +// Construct _Arg that maps to lifted outside compilation argument node input. +absl::StatusOr AddOutsideCompilationInputArgToFunctionBody( + const FunctionBody& function_body, const int arg_idx, + const DataType& data_type) { + NodeDefBuilder arg_builder(absl::StrCat("arg_", arg_idx), "_Arg"); + arg_builder.Attr("T", data_type); + arg_builder.Attr("index", arg_idx); + NodeDef arg_def; + TF_RETURN_IF_ERROR(arg_builder.Finalize(&arg_def)); + + TF_ASSIGN_OR_RETURN(Node * arg_node, function_body.graph->AddNode(arg_def)); + return arg_node; +} + +// Add _Retval node that matches newly added `arg_node` and connect `arg_node` +// to it. +absl::Status AddMatchingRetvalNode(const FunctionBody& function_body, + const int arg_idx, const DataType& data_type, + Node* arg_node) { + NodeDefBuilder ret_builder(absl::StrCat("ret_", arg_idx), "_Retval"); + ret_builder.Attr("T", data_type); + ret_builder.Attr("index", arg_idx); + ret_builder.Input(arg_node->name(), 0, data_type); + NodeDef ret_def; + TF_RETURN_IF_ERROR(ret_builder.Finalize(&ret_def)); + TF_ASSIGN_OR_RETURN(Node * ret_node, function_body.graph->AddNode(ret_def)); + function_body.graph->AddEdge(arg_node, 0, ret_node, 0); + + return absl::OkStatus(); +} + +void ReplaceLiftedArgNodePlaceholderWithArg( + const FunctionBody& function_body, const int original_arg_count, + const int arg_idx, const std::vector& lifted_arg_nodes, + Node* arg_node) { + Node* lifted_arg_node = lifted_arg_nodes[arg_idx - original_arg_count]; + // This might happen because lifted_arg_node only exists in one branch of an + // If node, and we are handling the other branch. + if (!lifted_arg_node) { + return; + } + + for (const Edge* e : lifted_arg_node->out_edges()) { + if (e->IsControlEdge()) { + function_body.graph->AddControlEdge(arg_node, e->dst()); + } else { + function_body.graph->AddEdge(arg_node, 0, e->dst(), e->dst_input()); + } + } + function_body.graph->RemoveNode(lifted_arg_node); +} + +// Adds function def to function definition library and update the function +// callsite operation `callsite_node` to invoke new function instead. +absl::Status AddFunctionWithNewName(const std::string& new_name, + const std::string& func_attr_name, + const FunctionDef& function_def, + NameAttrList* func_attr, + Node* callsite_node, + FunctionLibraryDefinition* fld) { + TF_RETURN_IF_ERROR(fld->AddFunctionDef(function_def)); + func_attr->set_name(new_name); + callsite_node->ClearAttr(func_attr_name); + callsite_node->AddAttr(func_attr_name, *func_attr); + return absl::OkStatus(); +} + +// Reconnect outside compilation lifted arguments in a functional While node to +// its outside compilation tensor sources. +absl::Status PostprocessLiftedArgsForWhile( + const std::unordered_map& + outside_compilation_attr_to_node, + Graph* g, Node* n, FunctionLibraryDefinition* fld) { + TF_RET_CHECK(n->IsWhileNode()); + + // Check if there is any lifted args in body function. + NameAttrList body_func; + TF_RETURN_IF_ERROR(GetNodeAttr(n->def(), "body", &body_func)); + const FunctionDef* body_function_def = fld->Find(body_func.name()); + TF_RET_CHECK(body_function_def); + + if (!HasLiftedArgs(*body_function_def)) { + return absl::OkStatus(); + } + + // Gather all lifted args. + std::unique_ptr body_function_body; + TF_RETURN_IF_ERROR(FunctionDefToBodyHelper(*body_function_def, + AttrSlice(&body_func.attr()), fld, + &body_function_body)); + + int original_arg_count = body_function_body->arg_nodes.size(); + + TF_ASSIGN_OR_RETURN( + auto lifted_arg_nodes_and_outside_compilation_nodes, + LiftedArgsAndOutsideCompilationNodesInFunctionBody( + *body_function_body, outside_compilation_attr_to_node)); + + // Append lifted args' types to While node's T attribute. + TF_ASSIGN_OR_RETURN( + std::vector data_types, + UpdateTypesAttribute(lifted_arg_nodes_and_outside_compilation_nodes, "T", + n)); + + // Add edges from outside compilation nodes to While node. + std::vector outside_compilation_nodes; + outside_compilation_nodes.reserve( + lifted_arg_nodes_and_outside_compilation_nodes.size()); + std::transform( + lifted_arg_nodes_and_outside_compilation_nodes.begin(), + lifted_arg_nodes_and_outside_compilation_nodes.end(), + std::back_inserter(outside_compilation_nodes), + [](const std::pair& pair) { return pair.second; }); + AddEdgesFromOutsideCompilationNodes(original_arg_count, + /*arg_to_input_edge_offset=*/0, + data_types, outside_compilation_nodes, g, + n); + + // In body_graph, create new _Arg/_Retval nodes, and replace lifted arg + // nodes with the new _Arg nodes. + std::vector lifted_arg_nodes; + lifted_arg_nodes.reserve( + lifted_arg_nodes_and_outside_compilation_nodes.size()); + std::transform( + lifted_arg_nodes_and_outside_compilation_nodes.begin(), + lifted_arg_nodes_and_outside_compilation_nodes.end(), + std::back_inserter(lifted_arg_nodes), + [](const std::pair& pair) { return pair.first; }); + for (int i = original_arg_count, end = data_types.size(); i < end; i++) { + TF_ASSIGN_OR_RETURN(Node * arg_node, + AddOutsideCompilationInputArgToFunctionBody( + *body_function_body, i, data_types[i])); + + TF_RETURN_IF_ERROR( + AddMatchingRetvalNode(*body_function_body, i, data_types[i], arg_node)); + + ReplaceLiftedArgNodePlaceholderWithArg( + *body_function_body, original_arg_count, i, lifted_arg_nodes, arg_node); + } + + const auto new_body_function_name = + fld->UniqueFunctionName(absl::StrCat(body_func.name(), "_lifted_arg_")); + FunctionDef rewritten_body_function_def; + TF_RETURN_IF_ERROR(GraphToFunctionDef( + *body_function_body->graph, new_body_function_name, + HostGraphControlRetMapping, &rewritten_body_function_def)); + TF_RETURN_IF_ERROR(AddFunctionWithNewName(new_body_function_name, "body", + rewritten_body_function_def, + &body_func, n, fld)); + + // In cond_graph, just add new _Arg nodes. + NameAttrList cond_func; + TF_RETURN_IF_ERROR(GetNodeAttr(n->def(), "cond", &cond_func)); + const FunctionDef* cond_function_def = fld->Find(cond_func.name()); + TF_RET_CHECK(cond_function_def); + std::unique_ptr cond_function_body; + TF_RETURN_IF_ERROR(FunctionDefToBodyHelper(*cond_function_def, + AttrSlice(&cond_func.attr()), fld, + &cond_function_body)); + + for (int i = original_arg_count, end = data_types.size(); i < end; i++) { + absl::StatusOr arg_node_or = + AddOutsideCompilationInputArgToFunctionBody(*cond_function_body, i, + data_types[i]); + TF_RETURN_IF_ERROR(arg_node_or.status()); + } + + const auto new_cond_function_name = + fld->UniqueFunctionName(absl::StrCat(cond_func.name(), "_lifted_arg_")); + FunctionDef rewritten_cond_function_def; + TF_RETURN_IF_ERROR(GraphToFunctionDef( + *cond_function_body->graph, new_cond_function_name, + HostGraphControlRetMapping, &rewritten_cond_function_def)); + TF_RETURN_IF_ERROR(AddFunctionWithNewName(new_cond_function_name, "cond", + rewritten_cond_function_def, + &cond_func, n, fld)); + return absl::OkStatus(); +} + +absl::Status PostprocessLiftedArgsForIf( + const std::unordered_map& + outside_compilation_attr_to_node, + Graph* g, Node* n, FunctionLibraryDefinition* fld) { + TF_RET_CHECK(n->IsIfNode()); + + NameAttrList then_branch_func; + TF_RETURN_IF_ERROR(GetNodeAttr(n->def(), "then_branch", &then_branch_func)); + const FunctionDef* then_branch_function_def = + fld->Find(then_branch_func.name()); + TF_RET_CHECK(then_branch_function_def); + + NameAttrList else_branch_func; + TF_RETURN_IF_ERROR(GetNodeAttr(n->def(), "else_branch", &else_branch_func)); + const FunctionDef* else_branch_function_def = + fld->Find(else_branch_func.name()); + TF_RET_CHECK(else_branch_function_def); + + // Nothing to do if neither branch contains any lifted arguments. + if (!HasLiftedArgs(*then_branch_function_def) && + !HasLiftedArgs(*else_branch_function_def)) { + return absl::OkStatus(); + } + + std::unique_ptr then_branch_function_body; + TF_RETURN_IF_ERROR(FunctionDefToBodyHelper( + *then_branch_function_def, AttrSlice(&then_branch_func.attr()), fld, + &then_branch_function_body)); + + std::unique_ptr else_branch_function_body; + TF_RETURN_IF_ERROR(FunctionDefToBodyHelper( + *else_branch_function_def, AttrSlice(&else_branch_func.attr()), fld, + &else_branch_function_body)); + + // Then and else branches have same argument count and argument data types. + int original_arg_count = then_branch_function_body->arg_nodes.size(); + + TF_ASSIGN_OR_RETURN( + auto then_branch_lifted_arg_nodes_and_outside_compilation_nodes, + LiftedArgsAndOutsideCompilationNodesInFunctionBody( + *then_branch_function_body, outside_compilation_attr_to_node)); + + TF_ASSIGN_OR_RETURN( + auto else_branch_lifted_arg_nodes_and_outside_compilation_nodes, + LiftedArgsAndOutsideCompilationNodesInFunctionBody( + *else_branch_function_body, outside_compilation_attr_to_node)); + + // Merge lifted args from then and else branches. + std::vector outside_compilation_nodes; + std::vector then_branch_lifted_arg_nodes; + outside_compilation_nodes.reserve( + then_branch_lifted_arg_nodes_and_outside_compilation_nodes.size()); + then_branch_lifted_arg_nodes.reserve( + then_branch_lifted_arg_nodes_and_outside_compilation_nodes.size()); + for (const auto& pair : + then_branch_lifted_arg_nodes_and_outside_compilation_nodes) { + outside_compilation_nodes.push_back(pair.second); + then_branch_lifted_arg_nodes.push_back(pair.first); + } + for (const auto& pair : + else_branch_lifted_arg_nodes_and_outside_compilation_nodes) { + if (std::find(outside_compilation_nodes.begin(), + outside_compilation_nodes.end(), + pair.second) == outside_compilation_nodes.end()) { + outside_compilation_nodes.push_back(pair.second); + // Then branch does not contain this lifted arg. Add an empty item to + // then_branch_lifted_arg_nodes. + then_branch_lifted_arg_nodes.push_back(nullptr); + } + } + // Reorder else_branch_lifted_arg_nodes_and_outside_compilation_nodes. + std::vector else_branch_lifted_arg_nodes( + outside_compilation_nodes.size()); + for (const auto& pair : + else_branch_lifted_arg_nodes_and_outside_compilation_nodes) { + auto iter = std::find(outside_compilation_nodes.begin(), + outside_compilation_nodes.end(), pair.second); + TF_RET_CHECK(iter != outside_compilation_nodes.end()); + int index = iter - outside_compilation_nodes.begin(); + else_branch_lifted_arg_nodes[index] = pair.first; + } + + // Append lifted args' types to If node's Tin attribute. + std::vector data_types; + data_types.reserve(outside_compilation_nodes.size()); + TF_RETURN_IF_ERROR(GetNodeAttr(n->def(), "Tin", &data_types)); + for (Node* n : outside_compilation_nodes) { + data_types.push_back(n->output_type(0)); + } + n->ClearAttr("Tin"); + n->AddAttr("Tin", data_types); + + // Add edges from outside compilation nodes to If node. If node's input #0 + // is predicate input, input #1 maps to _Arg #0 of branch functions, thus + // arg_to_input_edge_offset is set to 1. + AddEdgesFromOutsideCompilationNodes(original_arg_count, + /*arg_to_input_edge_offset=*/1, + data_types, outside_compilation_nodes, g, + n); + + for (int i = original_arg_count, end = data_types.size(); i < end; ++i) { + TF_ASSIGN_OR_RETURN(Node * then_branch_arg_node, + AddOutsideCompilationInputArgToFunctionBody( + *then_branch_function_body, i, data_types[i])); + + ReplaceLiftedArgNodePlaceholderWithArg( + *then_branch_function_body, original_arg_count, i, + then_branch_lifted_arg_nodes, then_branch_arg_node); + + TF_ASSIGN_OR_RETURN(Node * else_branch_arg_node, + AddOutsideCompilationInputArgToFunctionBody( + *else_branch_function_body, i, data_types[i])); + + ReplaceLiftedArgNodePlaceholderWithArg( + *else_branch_function_body, original_arg_count, i, + else_branch_lifted_arg_nodes, else_branch_arg_node); + } + + const auto new_then_function_name = fld->UniqueFunctionName( + absl::StrCat(then_branch_func.name(), "_lifted_arg_")); + FunctionDef rewritten_then_branch_function_def; + TF_RETURN_IF_ERROR(GraphToFunctionDef( + *then_branch_function_body->graph, new_then_function_name, + HostGraphControlRetMapping, &rewritten_then_branch_function_def)); + TF_RETURN_IF_ERROR(AddFunctionWithNewName( + new_then_function_name, "then_branch", rewritten_then_branch_function_def, + &then_branch_func, n, fld)); + + const auto new_else_function_name = fld->UniqueFunctionName( + absl::StrCat(else_branch_func.name(), "_lifted_arg_")); + FunctionDef rewritten_else_branch_function_def; + TF_RETURN_IF_ERROR(GraphToFunctionDef( + *else_branch_function_body->graph, new_else_function_name, + HostGraphControlRetMapping, &rewritten_else_branch_function_def)); + TF_RETURN_IF_ERROR(AddFunctionWithNewName( + new_else_function_name, "else_branch", rewritten_else_branch_function_def, + &else_branch_func, n, fld)); + return absl::OkStatus(); +} + +absl::Status PostprocessLiftedArgsForCall( + const std::unordered_map& + outside_compilation_attr_to_node, + Graph* g, Node* n, FunctionLibraryDefinition* fld) { + const FunctionDef* fdef = fld->Find(n->type_string()); + TF_RET_CHECK(fdef); + + // Nothing to do if the function does not contain any lifted arguments. + if (!HasLiftedArgs(*fdef)) { + return absl::OkStatus(); + } + + std::unique_ptr fbody; + TF_RETURN_IF_ERROR(FunctionDefToBodyHelper(*fdef, n->attrs(), fld, &fbody)); + + int original_arg_count = fbody->arg_nodes.size(); + + TF_ASSIGN_OR_RETURN(auto lifted_arg_nodes_and_outside_compilation_nodes, + LiftedArgsAndOutsideCompilationNodesInFunctionBody( + *fbody, outside_compilation_attr_to_node)); + + // Append lifted args' types to call node's input data types. + std::vector data_types(n->input_types().begin(), + n->input_types().end()); + for (auto pair : lifted_arg_nodes_and_outside_compilation_nodes) { + Node* outside_compilation_node = pair.second; + DataType data_type; + TF_RET_CHECK(outside_compilation_node->IsIdentity() || + outside_compilation_node->type_string() == "Placeholder"); + if (outside_compilation_node->IsIdentity()) { + TF_RETURN_IF_ERROR( + GetNodeAttr(outside_compilation_node->def(), "T", &data_type)); + } else { + TF_RETURN_IF_ERROR( + GetNodeAttr(outside_compilation_node->def(), "dtype", &data_type)); + } + data_types.push_back(data_type); + } + + std::vector lifted_arg_nodes; + lifted_arg_nodes.reserve( + lifted_arg_nodes_and_outside_compilation_nodes.size()); + std::transform( + lifted_arg_nodes_and_outside_compilation_nodes.begin(), + lifted_arg_nodes_and_outside_compilation_nodes.end(), + std::back_inserter(lifted_arg_nodes), + [](const std::pair& pair) { return pair.first; }); + for (int i = original_arg_count, end = data_types.size(); i < end; ++i) { + TF_ASSIGN_OR_RETURN( + Node * arg_node, + AddOutsideCompilationInputArgToFunctionBody(*fbody, i, data_types[i])); + + ReplaceLiftedArgNodePlaceholderWithArg(*fbody, original_arg_count, i, + lifted_arg_nodes, arg_node); + } + + FunctionDef rewritten_fdef; + TF_RETURN_IF_ERROR(GraphToFunctionDef(*fbody->graph, n->type_string(), + HostGraphControlRetMapping, + &rewritten_fdef)); + const auto new_function_name = + fld->UniqueFunctionName(absl::StrCat(n->type_string(), "_lifted_arg_")); + rewritten_fdef.mutable_signature()->set_name(new_function_name); + TF_RETURN_IF_ERROR(fld->AddFunctionDef(rewritten_fdef)); + + // We need to recreate the node. Otherwise TF will not know n->num_inputs() + // has increased. + NodeDef node_def = n->def(); + + // Function name is represented via the Op's type. Reset the op type to new + // function def name; + *node_def.mutable_op() = new_function_name; + + for (int i = original_arg_count, end = data_types.size(); i < end; i++) { + Node* outside_compilation_node = + lifted_arg_nodes_and_outside_compilation_nodes[i - original_arg_count] + .second; + node_def.add_input(absl::StrCat(outside_compilation_node->name(), ":", 0)); + } + TF_ASSIGN_OR_RETURN(n, ReplaceNode(g, n, node_def)); + + // Add edges from outside compilation nodes to call node. + std::vector outside_compilation_nodes; + outside_compilation_nodes.reserve( + lifted_arg_nodes_and_outside_compilation_nodes.size()); + std::transform( + lifted_arg_nodes_and_outside_compilation_nodes.begin(), + lifted_arg_nodes_and_outside_compilation_nodes.end(), + std::back_inserter(outside_compilation_nodes), + [](const std::pair& pair) { return pair.second; }); + AddEdgesFromOutsideCompilationNodes(original_arg_count, + /*arg_to_input_edge_offset=*/0, + data_types, outside_compilation_nodes, g, + n); + + return absl::OkStatus(); +} + +// Creates a mapping from outside compilation cluster name to lifted argument +// placeholder. +absl::StatusOr> +OutsideCompilationAttrToNode(const Graph& g) { + std::unordered_map outside_compilation_attr_to_node; + for (Node* n : g.op_nodes()) { + bool is_lifted_arg; + std::string outside_compilation_attr; + if (TryGetNodeAttr(n->def(), kXlaIsLiftedArgAttrName, &is_lifted_arg) && + TryGetNodeAttr(n->def(), "_xla_outside_compilation", + &outside_compilation_attr)) { + TF_RET_CHECK(is_lifted_arg); + TF_RET_CHECK(n->IsIdentity() || n->type_string() == "Placeholder"); + outside_compilation_attr_to_node[outside_compilation_attr] = n; + } + } + + return outside_compilation_attr_to_node; +} + +absl::Status PostprocessLiftedArgs(Graph* g, FunctionLibraryDefinition* fld) { + TF_ASSIGN_OR_RETURN(auto outside_compilation_attr_to_node, + OutsideCompilationAttrToNode(*g)); + + std::vector call_nodes; + for (Node* n : g->op_nodes()) { + if (!HasNodeAttr(n->def(), kXlaHasHostTransferAttrName)) { + continue; + } + + if (n->IsWhileNode()) { + TF_RETURN_IF_ERROR(PostprocessLiftedArgsForWhile( + outside_compilation_attr_to_node, g, n, fld)); + } + + if (n->IsIfNode()) { + TF_RETURN_IF_ERROR(PostprocessLiftedArgsForIf( + outside_compilation_attr_to_node, g, n, fld)); + } + + // Outside compilation host side function call will always be direct + // function call nodes. + // Function call nodes need to be handled separately because we rewrite + // nodes in `PostprocessLiftedArgsForCall`. + if (fld->Contains(n->type_string())) { + call_nodes.push_back(n); + } + } + + for (Node* n : call_nodes) { + TF_RETURN_IF_ERROR(PostprocessLiftedArgsForCall( + outside_compilation_attr_to_node, g, n, fld)); + } + + return absl::OkStatus(); +} + +// For an XLA computation, builds host side graph given all outside compilation +// graphs inside it. The host side graph contains: +// 1) a "sequencer" node (we will add control edge between XlaRecvAtHost and +// XlaSendFromHost to this sequencer node, so all outside compilation nodes +// will be executed *before* this sequencer). +// 2) a "key placeholder" node. Later in ExpandHostGraphIntoMainGraph(), we will +// replace this node with compilation result node. +// 3) all outside compilation graphs. +absl::Status ConstructHostGraph( + const std::string& xla_cluster_name, + const std::string& outside_compilation_attr_name, + const std::vector& outside_compilation_host_graphs, + FunctionLibraryDefinition* fld, std::unique_ptr* host_graph) { + host_graph->reset(new Graph(fld)); + + // Create sequencer node in host graph. + NodeDefBuilder sequencer_builder(absl::StrCat(xla_cluster_name, "_sequencer"), + "NoOp"); + sequencer_builder.Attr("_xla_host_transfer_sequencer", xla_cluster_name); + NodeDef sequencer_def; + TF_RETURN_IF_ERROR(sequencer_builder.Finalize(&sequencer_def)); + TF_ASSIGN_OR_RETURN(Node * sequencer, (*host_graph)->AddNode(sequencer_def)); + + // Create key placeholder in host graph. + TF_ASSIGN_OR_RETURN( + Node * key_placeholder, + AddHostComputeKeyPlaceholder(xla_cluster_name, host_graph->get())); + + // For each outside compilation graph, copy them to host graph with the + // following changes: + // a) Use key_placeholder in host graph instead of its own. + // b) Add control edge from host transfer nodes (XlaRecvAtHost, + // XlaSendFromHost, If/While nodes containing + // XlaRecvAtHost/XlaSendFromHost) to sequencer node. + // c) Clear node_def.device(), so device placer won't get confused. + for (const std::string& host_func : outside_compilation_host_graphs) { + VLOG(4) << "Expanding host graph " << host_func; + // Temporarily use "0" as "_device_ordinal". It will be reset to placeholder + // value after we expanded all host graphs. We cannot just use placeholder + // value here because FunctionDef instantiation does not allow placeholder + // value for attributes. + AttrValue device_ordinal_attr; + device_ordinal_attr.set_i(0); + protobuf::Map attrs; + attrs["_device_ordinal"] = device_ordinal_attr; + std::unique_ptr host_fbody; + const FunctionDef* host_fdef = fld->Find(host_func); + TF_RET_CHECK(host_fdef); + TF_RETURN_IF_ERROR(FunctionDefToBodyHelper(*host_fdef, AttrSlice(&attrs), + fld, &host_fbody)); + + // We use ReverseDFS() to copy nodes. Make sure all nodes are reverse + // reachable from sink node so all nodes will be copied. + // TODO(b/77601805): consolidate copy graph functions. + FixupSourceAndSinkEdges(host_fbody->graph); + + std::map node_map; + node_map[host_fbody->graph->source_node()] = (*host_graph)->source_node(); + node_map[host_fbody->graph->sink_node()] = (*host_graph)->sink_node(); + absl::Status s; + ReverseDFS( + *host_fbody->graph, /*enter=*/nullptr, + [&](const Node* n) { + if (!s.ok()) { + return; + } + + Node* copy; + if (node_map.find(n) != node_map.end()) { + // Already copied this node. + copy = node_map.at(n); + } else if (IsKeyPlaceholderNode(*n)) { + // Change a). + copy = key_placeholder; + node_map[n] = copy; + } else { + // Copy the node. + NodeDef copy_def = n->def(); + // Change c). + copy_def.clear_device(); + copy = (*host_graph)->AddNode(copy_def, &s); + if (!s.ok()) { + return; + } + node_map[n] = copy; + } + + // Only handle input edges. Output edges will be added later as + // its output nodes' input edges. + for (auto e : n->in_edges()) { + if (node_map.find(e->src()) == node_map.end()) { + s = absl::InternalError(absl::StrCat( + "Cannot find node image for ", e->src()->DebugString())); + return; + } + (*host_graph) + ->AddEdge(node_map[e->src()], e->src_output(), copy, + e->dst_input()); + } + + // Change b). + if (HasNodeAttr(copy->def(), kXlaHasHostTransferAttrName)) { + (*host_graph)->AddControlEdge(copy, sequencer); + } + }, + NodeComparatorID()); + + if (!s.ok()) { + return s; + } + } + // Reset "_device_ordinal" to placeholder value. + TF_RETURN_IF_ERROR(ResetDeviceOrdinalToPlaceholderValue(host_graph->get())); + + // sequencer and key_placeholder might be dead nodes. Prune them if necessary. + // - sequencer should be pruned iff it has no input control edges from + // RecvAtHost/SendFromHost. If it has input control edge, we connect it to + // sink node so it won't be pruned. + // - key_placeholder should be pruned iff there's no RecvAtHost/SendFromHost. + // We don't need to do anything special. + if (!sequencer->in_edges().empty()) { + (*host_graph)->AddControlEdge(sequencer, (*host_graph)->sink_node()); + } + PruneForReverseReachability( + host_graph->get(), + std::unordered_set{(*host_graph)->sink_node()}); + + // Postprocess edges between different outside compilations. + TF_RETURN_IF_ERROR(PostprocessEdgesBetweenOutsideCompilations( + host_graph->get(), outside_compilation_attr_name)); + + // Postprocess lifted arg nodes. + TF_RETURN_IF_ERROR(PostprocessLiftedArgs(host_graph->get(), fld)); + + if (VLOG_IS_ON(4)) { + DumpGraphToFile(absl::StrCat("extract_outside_compilation_host_graph_for_", + xla_cluster_name), + **host_graph, fld); + } + + return absl::OkStatus(); +} + +// Expand XLA computation's outside compilation host side graph into main graph. +// Add a control edge between sequencer node and the XLA computation node. +absl::Status ExpandHostGraphIntoMainGraph( + Graph* main_graph, FunctionLibraryDefinition* fld, + const std::string& host_graph_func_name, Node* xla_computation_node, + Node* pivot_node) { + // Temporarily use "0" as "_device_ordinal". It will be rewritten with the + // correct value in a later pass. We cannot just use placeholder value here + // because FunctionDef instantiation does not allow placeholder value for + // attributes. + AttrValue device_ordinal_attr; + device_ordinal_attr.set_i(0); + protobuf::Map attrs; + attrs["_device_ordinal"] = device_ordinal_attr; + std::unique_ptr fbody; + const FunctionDef* host_graph_func = fld->Find(host_graph_func_name); + TF_RET_CHECK(host_graph_func); + TF_RETURN_IF_ERROR(FunctionDefToBodyHelper(*host_graph_func, + AttrSlice(&attrs), fld, &fbody)); + Graph* host_graph = fbody->graph; + + // We use ReverseDFS() to copy nodes. Make sure all nodes are reverse + // reachable from sink node so all nodes will be copied. + // TODO(b/77601805): consolidate copy graph functions. + FixupSourceAndSinkEdges(host_graph); + + // Copy all nodes. + std::map node_map; + if (pivot_node) { + node_map[host_graph->source_node()] = pivot_node; + } else { + node_map[host_graph->source_node()] = main_graph->source_node(); + } + node_map[host_graph->sink_node()] = main_graph->sink_node(); + absl::Status s = absl::OkStatus(); + auto copy_node_fn = [&](const Node* n) { + if (!s.ok()) { + return; + } + + Node* copy; + if (node_map.find(n) != node_map.end()) { + // Already copied this node. + copy = node_map.at(n); + } else { + // Copy the node. + NodeDef copy_def = n->def(); + copy = main_graph->AddNode(copy_def, &s); + if (!s.ok()) { + return; + } + node_map[n] = copy; + } + + // Only handle input edges. Output edges will be added later as its output + // nodes' input edges. + for (auto e : n->in_edges()) { + if (node_map.find(e->src()) == node_map.end()) { + s = absl::InternalError(absl::StrCat("Cannot find node image for ", + e->src()->DebugString())); + return; + } + main_graph->AddEdge(node_map[e->src()], e->src_output(), copy, + e->dst_input()); + } + + // Add control edge from sequencer to XLA computation node. + if (copy->type_string() == "NoOp" && + HasNodeAttr(copy->def(), "_xla_host_transfer_sequencer")) { + main_graph->AddControlEdge(copy, xla_computation_node); + } + }; + ReverseDFS(*host_graph, /*enter=*/nullptr, copy_node_fn, NodeComparatorID()); + return s; +} + +// Rewrites shape inference graph for outside compilation: +// 1) If XlaSendFromHost also exists in `host_graph`, copy nodes from +// `host_graph`. Because we might still have outside compilation to outside +// compilation placeholder nodes in shape inference graph, which will prevent +// us from inferring XlaSendFromHost shape. But in `host_graph`, we already +// removed those placeholder nodes. +// 2) Remove control edges. +// 3) Prune nodes that are not useful for shape inference. +absl::Status RewriteShapeInferenceGraph( + const std::string& shape_inference_graph_name, Graph* host_graph, + Node* pivot_node, FunctionLibraryDefinition* fld) { + // Use "0" as "_device_ordinal". It does not matter for shape inference. + AttrValue device_ordinal_attr; + device_ordinal_attr.set_i(0); + protobuf::Map attrs; + attrs["_device_ordinal"] = device_ordinal_attr; + std::unique_ptr fbody; + const FunctionDef* shape_inference_graph = + fld->Find(shape_inference_graph_name); + TF_RET_CHECK(shape_inference_graph); + TF_RETURN_IF_ERROR(FunctionDefToBodyHelper(*shape_inference_graph, + AttrSlice(&attrs), fld, &fbody)); + Graph* g = fbody->graph; + + // Find SendFromHost node. + Node* send_from_host = nullptr; + for (Node* n : g->nodes()) { + if (n->type_string() == "_XlaSendFromHost") { + send_from_host = n; + break; + } + } + if (!send_from_host) { + return absl::InternalError( + absl::StrCat("Shape inference graph ", shape_inference_graph_name, + " does not have _XlaSendFromHost node.")); + } + + // See if the SendFromHost node exists in `host_graph`. + Node* send_node_in_host_graph = nullptr; + for (Node* n : host_graph->nodes()) { + if (n->name() == send_from_host->name()) { + send_node_in_host_graph = n; + break; + } + } + if (send_node_in_host_graph) { + // This is an "top-level" outside compilation. Clear the graph, and copy + // SendFromHost and all its predecessors from `host_graph`. + std::vector nodes; + nodes.reserve(g->num_op_nodes()); + for (Node* n : g->op_nodes()) { + nodes.push_back(n); + } + for (Node* n : nodes) { + g->RemoveNode(n); + } + Node* start_node = pivot_node ? pivot_node : host_graph->source_node(); + // Reverse DFS from send_from_host_main_graph, and stop at start_node. + struct Visit { + Node* n; + bool is_exiting; + }; + std::vector stack{{send_node_in_host_graph, false}}; + std::map node_map; + node_map[host_graph->source_node()] = g->source_node(); + while (!stack.empty()) { + Visit& curr = stack.back(); + if (curr.is_exiting) { + if (node_map.find(curr.n) == node_map.end()) { + Node* copy = g->CopyNode(curr.n); + if (curr.n != start_node) { + for (const Edge* e : curr.n->in_edges()) { + auto node_iter = node_map.find(e->src()); + if (node_iter == node_map.end()) { + return absl::InternalError(absl::StrCat( + "Cannot find node image for ", e->src()->DebugString())); + } + g->AddEdge(node_iter->second, e->src_output(), copy, + e->dst_input()); + } + } + node_map[curr.n] = copy; + } + stack.pop_back(); + } else { + curr.is_exiting = true; + if (curr.n != start_node) { + for (const Edge* e : curr.n->in_edges()) { + if (node_map.find(e->src()) != node_map.end()) { + continue; + } + stack.push_back({e->src(), false}); + } + } + } + } + + send_from_host = node_map[send_node_in_host_graph]; + } else { + // This is an outside compilation generated for If/While/gradient/etc. + // It will be enough for shape inference. Leave `g` unchanged. + } + + // Control edges are not useful for shape inference. Remove them. + for (auto e : g->edges()) { + if (e->IsControlEdge()) { + g->RemoveEdge(e); + } + } + + // Nodes that are not reverse reachable from SendFromHost are not useful for + // shape inference. Prune them. + PruneForReverseReachability(g, + std::unordered_set{send_from_host}); + + if (VLOG_IS_ON(4)) { + DumpGraphToFile(shape_inference_graph_name, *g, fld); + } + + // Replace original shape inference graph. + FunctionDef fdef_replace; + TF_RETURN_IF_ERROR( + GraphToFunctionDef(*g, shape_inference_graph_name, &fdef_replace)); + TF_RETURN_IF_ERROR( + fld->ReplaceFunction(shape_inference_graph_name, fdef_replace)); + + return absl::OkStatus(); +} + +void SetMaximalSharding(NodeDefBuilder& node_builder) { + xla::OpSharding sharding; + sharding.set_type(xla::OpSharding::MAXIMAL); + sharding.add_tile_assignment_dimensions(1); + sharding.add_tile_assignment_devices(0); + node_builder.Attr("_XlaSharding", sharding.SerializeAsString()); +} + +// Builds XlaSendToHost node which sends cond predicate to host. +TF_ATTRIBUTE_NOINLINE absl::StatusOr BuildSendIfPredNode( + const std::string& name, const std::string& host_transfer_key, + Node* pred_node, Graph* g) { + NodeDefBuilder send_pred_builder(name, "XlaSendToHost"); + send_pred_builder.Attr("Tinput", DT_BOOL); + send_pred_builder.Attr("key", absl::StrCat(host_transfer_key, "_dtoh_0")); + send_pred_builder.Attr(kXlaTokenInputNodesAttrName, + std::vector{kXlaTokenArgNodeName}); + send_pred_builder.Attr(kXlaOriginalOutsideCompilationNodeName, name); + SetMaximalSharding(send_pred_builder); + send_pred_builder.Input(pred_node->name(), 0, DT_BOOL); + NodeDef send_pred_def; + TF_RETURN_IF_ERROR(send_pred_builder.Finalize(&send_pred_def)); + TF_ASSIGN_OR_RETURN(Node * send_pred_node, g->AddNode(send_pred_def)); + g->AddEdge(pred_node, 0, send_pred_node, 0); + return send_pred_node; +} + +// Replaces key placeholder node with an _Arg node. +absl::Status ReplaceKeyPlaceholderWithArgNode( + const std::string& xla_cluster_name, const std::string& func_name, + FunctionLibraryDefinition* fld) { + // Temporarily use "0" as "_device_ordinal". It will be reset to placeholder + // value after rewriting. + AttrValue device_ordinal_attr; + device_ordinal_attr.set_i(0); + protobuf::Map attrs; + attrs["_device_ordinal"] = device_ordinal_attr; + std::unique_ptr fbody; + const FunctionDef* func = fld->Find(func_name); + TF_RETURN_IF_ERROR( + FunctionDefToBodyHelper(*func, AttrSlice(&attrs), fld, &fbody)); + Graph* g = fbody->graph; + + // Find or create the key placeholder node. + Node* key_placeholder = nullptr; + for (Node* n : g->nodes()) { + if (IsKeyPlaceholderNode(*n)) { + key_placeholder = n; + break; + } + } + if (!key_placeholder) { + TF_ASSIGN_OR_RETURN(key_placeholder, + AddHostComputeKeyPlaceholder(xla_cluster_name, g)); + } + + // Build the _Arg node, and replace key placeholder node with it. + NodeDefBuilder arg_builder("key_arg", FunctionLibraryDefinition::kArgOp); + arg_builder.Attr("T", DT_STRING); + arg_builder.Attr("index", 0); + NodeDef arg_def; + TF_RETURN_IF_ERROR(arg_builder.Finalize(&arg_def)); + TF_RETURN_IF_ERROR(ReplaceNode(g, key_placeholder, arg_def).status()); + + // Reset "_device_ordinal" to placeholder value. + TF_RETURN_IF_ERROR(ResetDeviceOrdinalToPlaceholderValue(g)); + + FunctionDef replace_fdef; + TF_RETURN_IF_ERROR(GraphToFunctionDef( + *g, func_name, HostGraphControlRetMapping, &replace_fdef)); + TF_RETURN_IF_ERROR(fld->ReplaceFunction(func_name, replace_fdef)); + return absl::OkStatus(); +} + +// Builds host side graph for If node. +TF_ATTRIBUTE_NOINLINE absl::Status BuildHostGraphForIfNode( + const std::string& xla_cluster_attr_name, + const std::string& outside_compilation_attr_name, + const std::string& xla_cluster_name, const std::string& if_node_name, + const std::string& host_transfer_key, + const std::string& host_graph_func_name, FunctionLibraryDefinition* fld, + const std::string& then_branch_host_func_name, + const std::string& else_branch_host_func_name) { + Graph host_graph(fld); + std::string outside_compilation_name = absl::StrCat("oc_if_", if_node_name); + AttrValue device_ordinal_value; + device_ordinal_value.set_placeholder("_device_ordinal"); + + // Step 1: add key placeholder node. + TF_ASSIGN_OR_RETURN( + Node * key_placeholder, + AddHostComputeKeyPlaceholder(xla_cluster_name, &host_graph)); + + // Step 2: build XlaRecvAtHost node to recv predicate. + NodeDefBuilder recv_pred_builder( + absl::StrCat("recv_oc_if_pred_", if_node_name), "_XlaRecvAtHost"); + recv_pred_builder.Attr("Toutputs", std::vector{DT_BOOL}); + recv_pred_builder.Attr("key", host_transfer_key); + recv_pred_builder.Attr("device_ordinal", device_ordinal_value); + recv_pred_builder.Attr(xla_cluster_attr_name, xla_cluster_name); + recv_pred_builder.Attr(outside_compilation_attr_name, + outside_compilation_name); + recv_pred_builder.Attr(kXlaHasHostTransferAttrName, true); + recv_pred_builder.Input(key_placeholder->name(), 0, DT_STRING); + NodeDef recv_pred_def; + TF_RETURN_IF_ERROR(recv_pred_builder.Finalize(&recv_pred_def)); + TF_ASSIGN_OR_RETURN(Node * recv_pred_node, host_graph.AddNode(recv_pred_def)); + host_graph.AddEdge(key_placeholder, 0, recv_pred_node, 0); + + // Step 3: rewrite `{then, else}_branch_host_func_name`, replace key + // placeholder with an _Arg node. + TF_RETURN_IF_ERROR(ReplaceKeyPlaceholderWithArgNode( + xla_cluster_name, then_branch_host_func_name, fld)); + TF_RETURN_IF_ERROR(ReplaceKeyPlaceholderWithArgNode( + xla_cluster_name, else_branch_host_func_name, fld)); + + // Step 4: build If node to choose between `{then, else}_branch_host_graph`. + NodeDefBuilder if_builder(absl::StrCat("oc_if_", if_node_name), "If"); + if_builder.Attr("Tcond", DT_BOOL); + if_builder.Attr("Tin", std::vector{DT_STRING}); + if_builder.Attr("Tout", std::vector{}); + NameAttrList host_then_branch, host_else_branch; + host_then_branch.set_name(then_branch_host_func_name); + (*host_then_branch.mutable_attr())["_device_ordinal"] = device_ordinal_value; + host_else_branch.set_name(else_branch_host_func_name); + (*host_else_branch.mutable_attr())["_device_ordinal"] = device_ordinal_value; + if_builder.Attr("then_branch", host_then_branch); + if_builder.Attr("else_branch", host_else_branch); + if_builder.Attr(kXlaHasHostTransferAttrName, true); + if_builder.Attr(xla_cluster_attr_name, xla_cluster_name); + if_builder.Attr(outside_compilation_attr_name, outside_compilation_name); + if_builder.Input(recv_pred_node->name(), 0, DT_BOOL); + std::vector if_inputs{ + {key_placeholder->name(), 0, DT_STRING}}; + if_builder.Input(if_inputs); + NodeDef if_def; + TF_RETURN_IF_ERROR(if_builder.Finalize(&if_def)); + TF_ASSIGN_OR_RETURN(Node * if_node, host_graph.AddNode(if_def)); + host_graph.AddEdge(recv_pred_node, 0, if_node, 0); + host_graph.AddEdge(key_placeholder, 0, if_node, 1); + + // Convert `host_graph` to function. + FunctionDef oc_host_graph_fdef; + TF_RETURN_IF_ERROR(GraphToFunctionDef(host_graph, host_graph_func_name, + &oc_host_graph_fdef)); + if (fld->Find(host_graph_func_name)) { + TF_RETURN_IF_ERROR( + fld->ReplaceFunction(host_graph_func_name, oc_host_graph_fdef)); + } else { + TF_RETURN_IF_ERROR(fld->AddFunctionDef(oc_host_graph_fdef)); + } + + return absl::OkStatus(); +} + +// Rewrites loop cond to add a node which sends loop cond to host. +TF_ATTRIBUTE_NOINLINE absl::Status AddSendLoopPredToLoopCond( + const std::string& cond_xla_func_name, const std::string& host_transfer_key, + NameAttrList* loop_cond_func, FunctionLibraryDefinition* fld, + Node* while_node) { + // Instantiate the loop cond function. + std::unique_ptr fbody; + const FunctionDef* loop_cond_fdef = fld->Find(loop_cond_func->name()); + TF_RET_CHECK(loop_cond_fdef); + TF_RETURN_IF_ERROR(FunctionDefToBodyHelper( + *loop_cond_fdef, AttrSlice(&loop_cond_func->attr()), fld, &fbody)); + Graph* g = fbody->graph; + + // Find the _Retval node and the loop cond node. + Node* ret_node = nullptr; + for (Node* n : g->nodes()) { + if (n->type_string() == "_Retval") { + if (ret_node) { + return absl::InternalError( + absl::StrCat("Multiple return node for loop cond function ", + loop_cond_func->name(), ": ", ret_node->DebugString(), + " and ", n->DebugString())); + } else { + ret_node = n; + } + } + } + if (!ret_node) { + return absl::InternalError(absl::StrCat( + "No _Retval node for loop cond function ", loop_cond_func->name())); + } + Node* loop_cond; + TF_RETURN_IF_ERROR(ret_node->input_node(0, &loop_cond)); + + // Build the XlaSendToHost node. + NodeDefBuilder send_loop_cond_builder( + absl::StrCat("send_oc_while_cond_", while_node->name()), "XlaSendToHost"); + send_loop_cond_builder.Attr("Tinput", DT_BOOL); + send_loop_cond_builder.Attr("key", + absl::StrCat(host_transfer_key, "_dtoh_0")); + send_loop_cond_builder.Attr(kXlaTokenInputNodesAttrName, + std::vector{kXlaTokenArgNodeName}); + send_loop_cond_builder.Attr(kXlaOriginalOutsideCompilationNodeName, + send_loop_cond_builder.node_name()); + SetMaximalSharding(send_loop_cond_builder); + send_loop_cond_builder.Input(loop_cond->name(), 0, DT_BOOL); + NodeDef send_loop_cond_def; + TF_RETURN_IF_ERROR(send_loop_cond_builder.Finalize(&send_loop_cond_def)); + TF_ASSIGN_OR_RETURN(Node * send_loop_cond_node, + g->AddNode(send_loop_cond_def)); + g->AddEdge(loop_cond, 0, send_loop_cond_node, 0); + + // Replace original function if loop_cond_func already has been re-written + // for outside compilation. + FunctionDef replace_fdef; + if (loop_cond_func->name() == cond_xla_func_name) { + TF_RETURN_IF_ERROR( + GraphToFunctionDef(*g, loop_cond_func->name(), &replace_fdef)); + TF_RETURN_IF_ERROR( + fld->ReplaceFunction(loop_cond_func->name(), replace_fdef)); + } else { + // If original while cond function has not been modified, add a new function + // with send loop predicated added and update the while node callsite + // operation. + const auto new_name = fld->UniqueFunctionName( + absl::StrCat(loop_cond_func->name(), "_send_pred_added_")); + TF_RETURN_IF_ERROR(GraphToFunctionDef(*g, new_name, &replace_fdef)); + TF_RETURN_IF_ERROR(fld->AddFunctionDef(replace_fdef)); + loop_cond_func->set_name(new_name); + while_node->ClearAttr("cond"); + while_node->AddAttr("cond", *loop_cond_func); + } + + return absl::OkStatus(); +} + +// Rewrites while loop cond function for host. +absl::Status RewriteHostWhileLoopCond( + const std::string& cond_host_func_name, const std::string& while_node_name, + const std::string& host_transfer_key, + const std::string& xla_cluster_attr_name, + const std::string& xla_cluster_name, + const std::string& outside_compilation_attr_name, + const std::string& outside_compilation_name, + FunctionLibraryDefinition* fld) { + // Replace key placeholder node with _Arg node. + TF_RETURN_IF_ERROR(ReplaceKeyPlaceholderWithArgNode( + xla_cluster_name, cond_host_func_name, fld)); + + // Instantiate cond function. + AttrValue device_ordinal_temp_value; + device_ordinal_temp_value.set_i(0); + protobuf::Map attrs; + attrs["_device_ordinal"] = device_ordinal_temp_value; + std::unique_ptr cond_fbody; + const FunctionDef* cond_host_func = fld->Find(cond_host_func_name); + TF_RETURN_IF_ERROR(FunctionDefToBodyHelper(*cond_host_func, AttrSlice(&attrs), + fld, &cond_fbody)); + Graph* cond_graph = cond_fbody->graph; + Node* key_arg = nullptr; + for (Node* n : cond_graph->nodes()) { + if (n->type_string() == "_Arg") { + key_arg = n; + } + } + if (!key_arg) { + return absl::InternalError( + absl::StrCat("No _Arg node found for host compute key in function ", + cond_host_func_name)); + } + + // Add an XlaRecvAtHost node to use as cond function return value. + NodeDefBuilder recv_pred_builder( + absl::StrCat("recv_oc_while_cond_", while_node_name), "_XlaRecvAtHost"); + recv_pred_builder.Attr("Toutputs", std::vector{DT_BOOL}); + recv_pred_builder.Attr("key", host_transfer_key); + AttrValue device_ordinal_value; + device_ordinal_value.set_placeholder("_device_ordinal"); + recv_pred_builder.Attr("device_ordinal", device_ordinal_value); + recv_pred_builder.Attr(xla_cluster_attr_name, xla_cluster_name); + recv_pred_builder.Attr(outside_compilation_attr_name, + outside_compilation_name); + recv_pred_builder.Attr(kXlaHasHostTransferAttrName, true); + recv_pred_builder.Input(key_arg->name(), 0, DT_STRING); + NodeDef recv_pred_def; + TF_RETURN_IF_ERROR(recv_pred_builder.Finalize(&recv_pred_def)); + TF_ASSIGN_OR_RETURN(Node * recv_pred_node, + cond_graph->AddNode(recv_pred_def)); + cond_graph->AddEdge(key_arg, 0, recv_pred_node, 0); + NodeDefBuilder ret_builder( + absl::StrCat("recv_oc_while_cond_ret_", while_node_name), "_Retval"); + ret_builder.Attr("T", DT_BOOL); + ret_builder.Attr("index", 0); + ret_builder.Input(recv_pred_node->name(), 0, DT_BOOL); + NodeDef ret_def; + TF_RETURN_IF_ERROR(ret_builder.Finalize(&ret_def)); + TF_ASSIGN_OR_RETURN(Node * ret_node, cond_graph->AddNode(ret_def)); + cond_graph->AddEdge(recv_pred_node, 0, ret_node, 0); + + // Reset device_ordinal to placeholder value. + TF_RETURN_IF_ERROR(ResetDeviceOrdinalToPlaceholderValue(cond_graph)); + + // Replace original function. + FunctionDef cond_replace_fdef; + TF_RETURN_IF_ERROR(GraphToFunctionDef(*cond_graph, cond_host_func_name, + HostGraphControlRetMapping, + &cond_replace_fdef)); + TF_RETURN_IF_ERROR( + fld->ReplaceFunction(cond_host_func_name, cond_replace_fdef)); + + return absl::OkStatus(); +} + +// Rewrites while loop body function for host. +absl::Status RewriteHostWhileLoopBody( + const std::string& body_host_func_name, const std::string& while_node_name, + const std::string& host_transfer_key, + const std::string& xla_cluster_attr_name, + const std::string& xla_cluster_name, + const std::string& outside_compilation_attr_name, + const std::string& outside_compilation_name, + FunctionLibraryDefinition* fld) { + // Replace key placeholder node with _Arg node. + TF_RETURN_IF_ERROR(ReplaceKeyPlaceholderWithArgNode( + xla_cluster_name, body_host_func_name, fld)); + + // Instantiate body function. + AttrValue device_ordinal_temp_value; + device_ordinal_temp_value.set_i(0); + protobuf::Map attrs; + attrs["_device_ordinal"] = device_ordinal_temp_value; + std::unique_ptr body_fbody; + const FunctionDef* body_host_func = fld->Find(body_host_func_name); + TF_RET_CHECK(body_host_func); + TF_RETURN_IF_ERROR(FunctionDefToBodyHelper(*body_host_func, AttrSlice(&attrs), + fld, &body_fbody)); + Graph* body_graph = body_fbody->graph; + Node* key_arg = nullptr; + for (Node* n : body_graph->nodes()) { + if (n->type_string() == "_Arg") { + key_arg = n; + } + } + if (!key_arg) { + return absl::InternalError( + absl::StrCat("No _Arg node found for host compute key in function ", + body_host_func_name)); + } + + // Add a _Retval node to loop body. + NodeDefBuilder ret_builder( + absl::StrCat("recv_oc_while_body_ret_", while_node_name), "_Retval"); + ret_builder.Attr("T", DT_STRING); + ret_builder.Attr("index", 0); + ret_builder.Input(key_arg->name(), 0, DT_STRING); + NodeDef ret_def; + TF_RETURN_IF_ERROR(ret_builder.Finalize(&ret_def)); + TF_ASSIGN_OR_RETURN(Node * ret_node, body_graph->AddNode(ret_def)); + body_graph->AddEdge(key_arg, 0, ret_node, 0); + + // Reset device_ordinal to placeholder value. + TF_RETURN_IF_ERROR(ResetDeviceOrdinalToPlaceholderValue(body_graph)); + + // Replace original function. + FunctionDef body_replace_fdef; + TF_RETURN_IF_ERROR(GraphToFunctionDef(*body_graph, body_host_func_name, + HostGraphControlRetMapping, + &body_replace_fdef)); + TF_RETURN_IF_ERROR( + fld->ReplaceFunction(body_host_func_name, body_replace_fdef)); + + return absl::OkStatus(); +} + +// Builds host side graph for while node. +TF_ATTRIBUTE_NOINLINE absl::Status BuildHostGraphForWhileNode( + const std::string& xla_cluster_attr_name, + const std::string& outside_compilation_attr_name, + const std::string& xla_cluster_name, const std::string& while_node_name, + const std::string& host_transfer_key, + const std::string& host_graph_func_name, FunctionLibraryDefinition* fld, + const std::string& cond_host_func_name, + const std::string& body_host_func_name) { + Graph host_graph(fld); + std::string outside_compilation_name = + absl::StrCat("oc_while_", while_node_name); + + // Step 1: add key placeholder node. + TF_ASSIGN_OR_RETURN( + Node * key_placeholder, + AddHostComputeKeyPlaceholder(xla_cluster_name, &host_graph)); + + // Step 2: rewrite cond function. + TF_RETURN_IF_ERROR(RewriteHostWhileLoopCond( + cond_host_func_name, while_node_name, host_transfer_key, + xla_cluster_attr_name, xla_cluster_name, outside_compilation_attr_name, + outside_compilation_name, fld)); + + // Step 3: rewrite body function. + TF_RETURN_IF_ERROR(RewriteHostWhileLoopBody( + body_host_func_name, while_node_name, host_transfer_key, + xla_cluster_attr_name, xla_cluster_name, outside_compilation_attr_name, + outside_compilation_name, fld)); + + // Step 4: build While node. + NodeDefBuilder while_builder(absl::StrCat("oc_while_", while_node_name), + "While"); + while_builder.Attr("T", std::vector{DT_STRING}); + NameAttrList func; + AttrValue device_ordinal_value; + device_ordinal_value.set_placeholder("_device_ordinal"); + (*func.mutable_attr())["_device_ordinal"] = device_ordinal_value; + func.set_name(cond_host_func_name); + while_builder.Attr("cond", func); + func.set_name(body_host_func_name); + while_builder.Attr("body", func); + while_builder.Attr(kXlaHasHostTransferAttrName, true); + while_builder.Attr(xla_cluster_attr_name, xla_cluster_name); + while_builder.Attr(outside_compilation_attr_name, outside_compilation_name); + // Make sure loop body of i-th iteration happens before loop cond of (i+1)-th + // iteration. + while_builder.Attr("parallel_iterations", 1); + std::vector while_inputs{ + {key_placeholder->name(), 0, DT_STRING}}; + while_builder.Input(while_inputs); + NodeDef while_def; + TF_RETURN_IF_ERROR(while_builder.Finalize(&while_def)); + TF_ASSIGN_OR_RETURN(Node * while_node, host_graph.AddNode(while_def)); + host_graph.AddEdge(key_placeholder, 0, while_node, 0); + + // Convert `host_graph` to function. + FunctionDef oc_host_graph_fdef; + TF_RETURN_IF_ERROR(GraphToFunctionDef(host_graph, host_graph_func_name, + &oc_host_graph_fdef)); + if (fld->Find(host_graph_func_name)) { + TF_RETURN_IF_ERROR( + fld->ReplaceFunction(host_graph_func_name, oc_host_graph_fdef)); + } else { + TF_RETURN_IF_ERROR(fld->AddFunctionDef(oc_host_graph_fdef)); + } + + return absl::OkStatus(); +} + +// Builds host graph for func call nodes. +absl::Status BuildHostGraphForFuncCallNode( + const std::string& xla_cluster_attr_name, + const std::string& xla_cluster_name, + const std::string& outside_compilation_attr_name, + const std::string& func_call_node_name, + const std::string& func_call_host_func_name, + const std::string& host_graph_func_name, FunctionLibraryDefinition* fld) { + Graph host_graph(fld); + AttrValue device_ordinal_value; + device_ordinal_value.set_placeholder("_device_ordinal"); + + // Step 1: add key placeholder node. + TF_ASSIGN_OR_RETURN( + Node * key_placeholder, + AddHostComputeKeyPlaceholder(xla_cluster_name, &host_graph)); + + // Step 2: rewrite `host_func_name`, replace key placeholder with an _Arg + // node. + TF_RETURN_IF_ERROR(ReplaceKeyPlaceholderWithArgNode( + xla_cluster_name, func_call_host_func_name, fld)); + + // Step 3: build a function call node with `host_func_name`, with + // `key_placeholder` as input. + NodeDefBuilder call_builder(absl::StrCat("oc_call_", func_call_node_name), + func_call_host_func_name, fld); + call_builder.Input(key_placeholder->name(), 0, DT_STRING); + call_builder.Attr("_device_ordinal", device_ordinal_value); + call_builder.Attr(kXlaHasHostTransferAttrName, true); + call_builder.Attr(xla_cluster_attr_name, xla_cluster_name); + call_builder.Attr(outside_compilation_attr_name, call_builder.node_name()); + NodeDef call_def; + TF_RETURN_IF_ERROR(call_builder.Finalize(&call_def)); + TF_ASSIGN_OR_RETURN(Node * call_node, host_graph.AddNode(call_def)); + host_graph.AddEdge(key_placeholder, 0, call_node, 0); + + // Convert `host_graph` to function. + FunctionDef oc_host_graph_fdef; + TF_RETURN_IF_ERROR(GraphToFunctionDef(host_graph, host_graph_func_name, + HostGraphControlRetMapping, + &oc_host_graph_fdef)); + if (fld->Find(host_graph_func_name)) { + TF_RETURN_IF_ERROR( + fld->ReplaceFunction(host_graph_func_name, oc_host_graph_fdef)); + } else { + TF_RETURN_IF_ERROR(fld->AddFunctionDef(oc_host_graph_fdef)); + } + + return absl::OkStatus(); +} + +TF_ATTRIBUTE_NOINLINE absl::Status ExtractOutsideCompilationForFuncCallNode( + const std::string& xla_cluster_attr_name, + const std::string& outside_compilation_attr_name, + const std::string& xla_cluster_name, + const std::map& host_compute_core, Graph* g, Node* n, + FunctionLibraryRuntime* flr, FunctionLibraryDefinition* fld, + std::vector* host_graphs, + std::vector* shape_inference_graphs, + bool* has_outside_compilation) { + bool func_has_outside_compilation = false; + NameAttrList func; + if (fld->Contains(n->type_string())) { + func.set_name(n->type_string()); + typedef protobuf::Map AttrMap; + *func.mutable_attr() = AttrMap(n->attrs().begin(), n->attrs().end()); + } else if (n->IsPartitionedCall()) { + TF_RETURN_IF_ERROR(GetNodeAttr(n->def(), "f", &func)); + } else { + TF_RET_CHECK(n->type_string() == FunctionLibraryDefinition::kGradientOp); + func.set_name(FunctionLibraryDefinition::kGradientOp); + *func.mutable_attr() = n->def().attr(); + } + std::string canonical_func_name; + if (func.name() == FunctionLibraryDefinition::kGradientOp) { + NameAttrList forward_func; + TF_RETURN_IF_ERROR(GetNodeAttr(n->def(), "f", &forward_func)); + canonical_func_name = absl::StrCat("gradient_", forward_func.name()); + } else { + canonical_func_name = func.name(); + } + std::string new_func_name = absl::StrCat(canonical_func_name, "_oc"); + std::string host_func_name = + absl::StrCat("oc_func_call_host_", canonical_func_name); + TF_RETURN_IF_ERROR(ExtractOutsideCompilationForFunction( + xla_cluster_attr_name, outside_compilation_attr_name, xla_cluster_name, + func, new_func_name, host_func_name, host_compute_core, flr, fld, + shape_inference_graphs, &func_has_outside_compilation)); + + // If the function call does not have outside compilation, nothing to do. + if (!func_has_outside_compilation) { + return absl::OkStatus(); + } + + *has_outside_compilation = true; + + // Change `n` to call the new function directly. + auto replace_builder = + std::make_unique(n->name(), new_func_name, fld); + std::vector inputs(n->num_inputs()); + for (const Edge* e : n->in_edges()) { + if (e->IsControlEdge()) { + continue; + } + + const bool input_size_check = + e->dst_input() < static_cast(inputs.size()); + TF_RET_CHECK(e->dst_input() >= 0 && input_size_check); + inputs[e->dst_input()] = + NodeDefBuilder::NodeOut{e->src()->name(), e->src_output(), + e->src()->output_type(e->src_output())}; + } + for (const auto& input : inputs) { + replace_builder->Input(input); + } + for (const auto& attr : n->attrs()) { + replace_builder->Attr(attr.first, attr.second); + } + auto replace_def = std::make_unique(); + TF_RETURN_IF_ERROR(replace_builder->Finalize(replace_def.get())); + TF_ASSIGN_OR_RETURN(Node * replace, ReplaceNode(g, n, *replace_def)); + replace->AddAttr(kXlaTokenInputNodesAttrName, + std::vector{kXlaTokenArgNodeName}); + replace->AddAttr(kXlaOriginalOutsideCompilationNodeName, replace->name()); + + // Build host side graph for the function call. + std::string oc_host_graph_name = + absl::StrCat("oc_func_host_graph_", replace->name()); + TF_RETURN_IF_ERROR(BuildHostGraphForFuncCallNode( + xla_cluster_attr_name, xla_cluster_name, outside_compilation_attr_name, + replace->name(), host_func_name, oc_host_graph_name, fld)); + + // Record the host graph. + host_graphs->push_back(oc_host_graph_name); + + return absl::OkStatus(); +} + +absl::Status ExtractOutsideCompilationForIfNode( + const std::string& xla_cluster_attr_name, + const std::string& outside_compilation_attr_name, + const std::string& xla_cluster_name, + const std::map& host_compute_core, Graph* g, Node* n, + FunctionLibraryRuntime* flr, FunctionLibraryDefinition* fld, + std::vector* host_graphs, + std::vector* shape_inference_graphs, + bool* has_outside_compilation) { + // Instantiate "then_branch" and "else_branch". + NameAttrList then_branch, else_branch; + TF_RETURN_IF_ERROR(GetNodeAttr(n->attrs(), "then_branch", &then_branch)); + TF_RETURN_IF_ERROR(GetNodeAttr(n->attrs(), "else_branch", &else_branch)); + + // Extract outside compilation for then_branch and else_branch. + bool then_branch_has_outside_compilation = false; + bool else_branch_has_outside_compilation = false; + std::string then_branch_host_func_name = + absl::StrCat("oc_then_branch_host_if_", then_branch.name()), + else_branch_host_func_name = + absl::StrCat("oc_else_branch_host_if_", else_branch.name()); + std::string then_branch_xla_func_name = + absl::StrCat(then_branch.name(), "_oc"), + else_branch_xla_func_name = + absl::StrCat(else_branch.name(), "_oc"); + TF_RETURN_IF_ERROR(ExtractOutsideCompilationForFunction( + xla_cluster_attr_name, outside_compilation_attr_name, xla_cluster_name, + then_branch, then_branch_xla_func_name, then_branch_host_func_name, + host_compute_core, flr, fld, shape_inference_graphs, + &then_branch_has_outside_compilation)); + TF_RETURN_IF_ERROR(ExtractOutsideCompilationForFunction( + xla_cluster_attr_name, outside_compilation_attr_name, xla_cluster_name, + else_branch, else_branch_xla_func_name, else_branch_host_func_name, + host_compute_core, flr, fld, shape_inference_graphs, + &else_branch_has_outside_compilation)); + + // If then/else branch do not have outside compilation, nothing to do. + if (!then_branch_has_outside_compilation && + !else_branch_has_outside_compilation) { + return absl::OkStatus(); + } + + *has_outside_compilation = true; + + // Change If node to call the new functions. + if (then_branch_has_outside_compilation) { + then_branch.set_name(then_branch_xla_func_name); + n->ClearAttr("then_branch"); + n->AddAttr("then_branch", then_branch); + } + if (else_branch_has_outside_compilation) { + else_branch.set_name(else_branch_xla_func_name); + n->ClearAttr("else_branch"); + n->AddAttr("else_branch", else_branch); + } + n->AddAttr(kXlaOriginalOutsideCompilationNodeName, n->name()); + + std::string host_transfer_key = absl::StrCat("oc_if_pred_", n->name()); + + // XLA computation: add a SendToHost node to send cond predicate. + Node* pred_node; + TF_RETURN_IF_ERROR(n->input_node(0, &pred_node)); + TF_ASSIGN_OR_RETURN( + Node * send_pred_node, + BuildSendIfPredNode(absl::StrCat("send_oc_if_pred_", n->name()), + host_transfer_key, pred_node, g)); + n->AddAttr(kXlaTokenInputNodesAttrName, + std::vector{send_pred_node->name()}); + + // Add a control edge from `send_pred_node` to If node, so XlaCompiler will + // visit If node after `send_pred_node`, thus the token output for + // `send_pred_node` has been generated. + g->AddControlEdge(send_pred_node, n); + + // Build host side graph for the "If" node. + // If then/else branch does not have outside compilation, we won't build host + // graph for the branch. But here we need a host graph for both branches, so + // we need to create a no-op host graph. + if (!then_branch_has_outside_compilation) { + std::unique_ptr then_branch_host_graph(new Graph(fld)); + std::vector then_branch_host_graphs; + TF_RETURN_IF_ERROR(ConstructHostGraph( + xla_cluster_name, outside_compilation_attr_name, + then_branch_host_graphs, fld, &then_branch_host_graph)); + FunctionDef then_branch_host_fdef; + TF_RETURN_IF_ERROR(GraphToFunctionDef(*then_branch_host_graph, + then_branch_host_func_name, + &then_branch_host_fdef)); + if (fld->Find(then_branch_host_func_name)) { + TF_RETURN_IF_ERROR(fld->ReplaceFunction(then_branch_host_func_name, + then_branch_host_fdef)); + } else { + TF_RETURN_IF_ERROR(fld->AddFunctionDef(then_branch_host_fdef)); + } + } + if (!else_branch_has_outside_compilation) { + std::unique_ptr else_branch_host_graph(new Graph(fld)); + std::vector else_branch_host_graphs; + TF_RETURN_IF_ERROR(ConstructHostGraph( + xla_cluster_name, outside_compilation_attr_name, + else_branch_host_graphs, fld, &else_branch_host_graph)); + FunctionDef else_branch_host_fdef; + TF_RETURN_IF_ERROR(GraphToFunctionDef(*else_branch_host_graph, + else_branch_host_func_name, + &else_branch_host_fdef)); + if (fld->Find(else_branch_host_func_name)) { + TF_RETURN_IF_ERROR(fld->ReplaceFunction(else_branch_host_func_name, + else_branch_host_fdef)); + } else { + TF_RETURN_IF_ERROR(fld->AddFunctionDef(else_branch_host_fdef)); + } + } + std::string oc_host_graph_name = absl::StrCat("oc_if_host_graph_", n->name()); + TF_RETURN_IF_ERROR(BuildHostGraphForIfNode( + xla_cluster_attr_name, outside_compilation_attr_name, xla_cluster_name, + n->name(), host_transfer_key, oc_host_graph_name, fld, + then_branch_host_func_name, else_branch_host_func_name)); + host_graphs->push_back(oc_host_graph_name); + + return absl::OkStatus(); +} + +absl::Status ExtractOutsideCompilationForWhileNode( + const std::string& xla_cluster_attr_name, + const std::string& outside_compilation_attr_name, + const std::string& xla_cluster_name, + const std::map& host_compute_core, Graph* g, Node* n, + FunctionLibraryRuntime* flr, FunctionLibraryDefinition* fld, + std::vector* host_graphs, + std::vector* shape_inference_graphs, + bool* has_outside_compilation) { + // Instantiate "cond" and "body". + NameAttrList cond, body; + TF_RETURN_IF_ERROR(GetNodeAttr(n->attrs(), "cond", &cond)); + TF_RETURN_IF_ERROR(GetNodeAttr(n->attrs(), "body", &body)); + + // Extract outside compilation for cond and body. + bool cond_has_outside_compilation = false; + bool body_has_outside_compilation = false; + std::string cond_host_func_name = + absl::StrCat("oc_cond_host_while_", cond.name()), + body_host_func_name = + absl::StrCat("oc_body_host_while_", body.name()); + std::string cond_xla_func_name = absl::StrCat(cond.name(), "_oc"), + body_xla_func_name = absl::StrCat(body.name(), "_oc"); + TF_RETURN_IF_ERROR(ExtractOutsideCompilationForFunction( + xla_cluster_attr_name, outside_compilation_attr_name, xla_cluster_name, + cond, cond_xla_func_name, cond_host_func_name, host_compute_core, flr, + fld, shape_inference_graphs, &cond_has_outside_compilation)); + TF_RETURN_IF_ERROR(ExtractOutsideCompilationForFunction( + xla_cluster_attr_name, outside_compilation_attr_name, xla_cluster_name, + body, body_xla_func_name, body_host_func_name, host_compute_core, flr, + fld, shape_inference_graphs, &body_has_outside_compilation)); + + // If cond/body do not have outside compilation, nothing to do. + if (!cond_has_outside_compilation && !body_has_outside_compilation) { + return absl::OkStatus(); + } + + *has_outside_compilation = true; + + // Change While node to call the new functions. + if (cond_has_outside_compilation) { + cond.set_name(cond_xla_func_name); + n->ClearAttr("cond"); + n->AddAttr("cond", cond); + } + if (body_has_outside_compilation) { + body.set_name(body_xla_func_name); + n->ClearAttr("body"); + n->AddAttr("body", body); + } + n->AddAttr(kXlaOriginalOutsideCompilationNodeName, n->name()); + + std::string host_transfer_key = absl::StrCat("oc_while_pred_", n->name()); + + // XLA computation: rewrite cond function to add a SendToHost node to send + // loop predicate. + TF_RETURN_IF_ERROR(AddSendLoopPredToLoopCond( + cond_xla_func_name, host_transfer_key, &cond, fld, n)); + n->AddAttr(kXlaTokenInputNodesAttrName, + std::vector{kXlaTokenArgNodeName}); + + // Build host side graph for the "While" node. + if (!cond_has_outside_compilation) { + std::unique_ptr cond_host_graph(new Graph(fld)); + std::vector host_graphs; + TF_RETURN_IF_ERROR(ConstructHostGraph(xla_cluster_name, + outside_compilation_attr_name, + host_graphs, fld, &cond_host_graph)); + FunctionDef cond_host_fdef; + TF_RETURN_IF_ERROR(GraphToFunctionDef(*cond_host_graph, cond_host_func_name, + &cond_host_fdef)); + if (fld->Find(cond_host_func_name)) { + TF_RETURN_IF_ERROR( + fld->ReplaceFunction(cond_host_func_name, cond_host_fdef)); + } else { + TF_RETURN_IF_ERROR(fld->AddFunctionDef(cond_host_fdef)); + } + } + if (!body_has_outside_compilation) { + std::unique_ptr body_host_graph(new Graph(fld)); + std::vector host_graphs; + TF_RETURN_IF_ERROR(ConstructHostGraph(xla_cluster_name, + outside_compilation_attr_name, + host_graphs, fld, &body_host_graph)); + FunctionDef body_host_fdef; + TF_RETURN_IF_ERROR(GraphToFunctionDef(*body_host_graph, body_host_func_name, + &body_host_fdef)); + if (fld->Find(body_host_func_name)) { + TF_RETURN_IF_ERROR( + fld->ReplaceFunction(body_host_func_name, body_host_fdef)); + } else { + TF_RETURN_IF_ERROR(fld->AddFunctionDef(body_host_fdef)); + } + } + std::string oc_host_graph_name = + absl::StrCat("oc_while_host_graph_", n->name()); + TF_RETURN_IF_ERROR(BuildHostGraphForWhileNode( + xla_cluster_attr_name, outside_compilation_attr_name, xla_cluster_name, + n->name(), host_transfer_key, oc_host_graph_name, fld, + cond_host_func_name, body_host_func_name)); + host_graphs->push_back(oc_host_graph_name); + + return absl::OkStatus(); +} + +absl::Status ExtractOutsideCompilationForNodesWithAssociatedFunctions( + Graph* g, const std::string& xla_cluster_attr_name, + const std::string& outside_compilation_attr_name, + const std::string& xla_cluster_name, + const std::map& host_compute_core, + FunctionLibraryRuntime* flr, FunctionLibraryDefinition* fld, + std::vector* host_graphs, + std::vector* shape_inference_graphs, + bool* has_outside_compilation) { + std::vector if_nodes, while_nodes, func_call_nodes; + for (Node* n : g->nodes()) { + if (n->IsIfNode()) { + if_nodes.push_back(n); + } else if (n->IsWhileNode()) { + while_nodes.push_back(n); + } else if (IsFunctionCall(*fld, *n)) { + func_call_nodes.push_back(n); + } + } + + for (Node* n : func_call_nodes) { + TF_RETURN_IF_ERROR(ExtractOutsideCompilationForFuncCallNode( + xla_cluster_attr_name, outside_compilation_attr_name, xla_cluster_name, + host_compute_core, g, n, flr, fld, host_graphs, shape_inference_graphs, + has_outside_compilation)); + } + + for (Node* n : if_nodes) { + TF_RETURN_IF_ERROR(ExtractOutsideCompilationForIfNode( + xla_cluster_attr_name, outside_compilation_attr_name, xla_cluster_name, + host_compute_core, g, n, flr, fld, host_graphs, shape_inference_graphs, + has_outside_compilation)); + } + + for (Node* n : while_nodes) { + TF_RETURN_IF_ERROR(ExtractOutsideCompilationForWhileNode( + xla_cluster_attr_name, outside_compilation_attr_name, xla_cluster_name, + host_compute_core, g, n, flr, fld, host_graphs, shape_inference_graphs, + has_outside_compilation)); + } + + return absl::OkStatus(); +} + +absl::Status CopyOutsideCompilationConstNodes( + Graph* g, const std::string& outside_compilation_attr_name) { + for (Node* n : g->op_nodes()) { + if (!n->IsConstant() || + !HasNodeAttr(n->def(), outside_compilation_attr_name)) { + continue; + } + + std::vector out_edges(n->out_edges().begin(), + n->out_edges().end()); + bool has_non_oc_output = false; + for (const Edge* e : out_edges) { + if (!e->IsControlEdge() && + !HasNodeAttr(e->dst()->def(), outside_compilation_attr_name)) { + has_non_oc_output = true; + break; + } + } + if (!has_non_oc_output) { + continue; + } + + NodeDef copy_def = n->def(); + copy_def.set_name(g->NewName(n->name())); + copy_def.mutable_attr()->erase(outside_compilation_attr_name); + TF_ASSIGN_OR_RETURN(Node * copy_node, g->AddNode(copy_def)); + for (const Edge* e : n->in_edges()) { + if (e->IsControlEdge()) { + g->AddControlEdge(e->src(), copy_node); + } + } + for (const Edge* e : out_edges) { + if (!e->IsControlEdge() && + !HasNodeAttr(e->dst()->def(), outside_compilation_attr_name)) { + Node* dst = e->dst(); + int dst_input = e->dst_input(); + g->RemoveEdge(e); + g->AddEdge(copy_node, 0, dst, dst_input); + } + } + } + + return absl::OkStatus(); +} + +} // namespace + +absl::Status RewriteOutsideCompilationSubgraphFn::operator()( + const std::vector& arg_source_tensors, + std::unique_ptr* graph, std::vector* input_permutation, + std::vector* output_permutation, NodeDef* node_def) { + std::string old_name = node_def->op(); + std::string new_name = + absl::StrCat(xla_cluster_name_, "_", new_function_name_, "_", old_name); + node_def->set_op(new_name); + node_def->set_name(new_name); + + // Later we will run PruneForReverseReachability(), so make sure all original + // nodes are reachable from sink node and won't be removed. + FixupSourceAndSinkEdges(graph->get()); + + // Step 1: create a key placeholder node. + TF_ASSIGN_OR_RETURN( + Node * key_placeholder, + AddHostComputeKeyPlaceholder(xla_cluster_name_, graph->get())); + + // Step 2: build RecvAtHost node, and replace all _Arg nodes with it. + std::vector recv_at_host_dtypes; + TF_ASSIGN_OR_RETURN( + Node * recv_at_host_node, + ReplaceArgNodesWithRecvAtHostNode(graph->get(), new_name, + &recv_at_host_dtypes, key_placeholder)); + + // Step 3: build SendFromHost node, and replace all _Retval nodes with it. + std::vector send_from_host_dtypes; + TF_ASSIGN_OR_RETURN( + Node * send_from_host_node, + ReplaceRetNodesWithSendFromHostNode( + graph->get(), new_name, &send_from_host_dtypes, key_placeholder)); + + // Step 4: add XLA cluster and outside compilation attr. + for (Node* n : (*graph)->nodes()) { + if (IsKeyPlaceholderNode(*n)) { + continue; + } + + n->AddAttr(xla_cluster_attr_name_, xla_cluster_name_); + n->AddAttr(outside_compilation_attr_name_, old_name); + } + + // Check whether we have all input shapes for XlaSendFromHost. If we do, we + // will set `shapes` attr for the call node; otherwise we will save the + // shape inference graph and set `shape_inference_graph` for the call node. + std::optional> shapes = + GetInferredInputShapes(send_from_host_dtypes.size(), send_from_host_node); + for (Node* n : (*graph)->nodes()) { + n->ClearAttr(kXlaInferredShapesAttrName); + } + + // Step 5: add control edges for originally XLA <-> outside compilation + // control edges. + for (Node* n : (*graph)->nodes()) { + if (HasNodeAttr(n->def(), kXlaConnectedToXlaComputationAttrName)) { + (*graph)->AddControlEdge(n, send_from_host_node); + n->ClearAttr(kXlaConnectedToXlaComputationAttrName); + } + if (HasNodeAttr(n->def(), kXlaConnectedFromXlaComputationAttrName)) { + (*graph)->AddControlEdge(recv_at_host_node, n); + n->ClearAttr(kXlaConnectedFromXlaComputationAttrName); + } + } + + // Step 6: RecvAtHost/SendFromHost/key_placeholder might be dead nodes. Prune + // them if necessary. + // - RecvAtHost should be pruned iff it has no output data/control edges. If + // it has any output edge, it will be reverse reachable from sink node. We + // don't need to do anything special. + // - SendFromHost should be pruned iff it has no input data/control edges. If + // it has input edges other than key_placeholder, we connect it to sink + // node so it won't be pruned. + // - key_placeholder should be pruned iff RecvAtHost/SendFromHost are pruned. + // We don't need to do anything special. + if (send_from_host_node->in_edges().size() > 1) { + (*graph)->AddControlEdge(send_from_host_node, (*graph)->sink_node()); + } + PruneForReverseReachability( + graph->get(), std::unordered_set{(*graph)->sink_node()}); + + // Step 7: add necessary attributes to function call node, so we can replace + // it with HostCompute node later. + AddNodeAttr("_outside_compilation_subgraph", old_name, node_def); + if (shapes) { + NameAttrList shape_inference_graph; + AddNodeAttr("shape_inference_graph", shape_inference_graph, node_def); + AddNodeAttr("shapes", *shapes, node_def); + } else { + std::string shape_inference_func_name = + absl::StrCat("_outside_compilation_shape_inference_", new_name); + NameAttrList shape_inference_graph; + shape_inference_graph.set_name(shape_inference_func_name); + AddNodeAttr("shape_inference_graph", shape_inference_graph, node_def); + AddNodeAttr("shapes", std::vector{}, node_def); + } + AddNodeAttr("ancestors", std::vector{}, node_def); + AddNodeAttr("Tinputs", recv_at_host_dtypes, node_def); + AddNodeAttr("Toutputs", send_from_host_dtypes, node_def); + AddNodeAttr("key", absl::StrCat("host_compute_channel_", new_name), node_def); + + return absl::OkStatus(); +} + +absl::Status ExtractOutsideCompilationForFunction( + const std::string& xla_cluster_attr_name, + const std::string& outside_compilation_attr_name, + const std::string& xla_cluster_name, const NameAttrList& func_name_attrs, + const std::string& new_func_name, const std::string& host_graph_func_name, + const std::map& host_compute_core, + FunctionLibraryRuntime* flr, FunctionLibraryDefinition* fld, + std::vector* shape_inference_graphs, + bool* has_outside_compilation) { + // Convert the function to graph. + const std::string& func_name = func_name_attrs.name(); + FunctionLibraryRuntime::Handle handle; + TF_RETURN_IF_ERROR( + flr->Instantiate(func_name, AttrSlice(&func_name_attrs.attr()), &handle)); + absl::Status ret_status = absl::OkStatus(); + auto cleanup_handle = gtl::MakeCleanup([&]() { + auto s = flr->ReleaseHandle(handle); + if (!s.ok()) { + ret_status.Update(s); + } + }); + const FunctionBody* fbody = flr->GetFunctionBody(handle); + + // Check if we have outside compilation nodes. + *has_outside_compilation = false; + for (Node* n : fbody->graph->nodes()) { + if (HasNodeAttr(n->def(), outside_compilation_attr_name)) { + *has_outside_compilation = true; + break; + } + } + // We cannot early return here, because we might have outside compilation in + // If/While function body. + + if (VLOG_IS_ON(4)) { + DumpGraphToFile( + absl::StrCat("extract_outside_compilation_for_func_before_", func_name), + *fbody->graph, fld); + } + + std::unique_ptr graph_out; + std::vector outside_compilation_host_graphs; + std::vector shape_inference_graphs_to_rewrite; + if (*has_outside_compilation) { + // Copy outside compilation Const nodes with non outside compilation users. + TF_RETURN_IF_ERROR(CopyOutsideCompilationConstNodes( + fbody->graph, outside_compilation_attr_name)); + + // Find dependencies between outside compilation clusters. + TF_ASSIGN_OR_RETURN(auto cluster_deps, + OutsideCompilationClusterDependencies( + fbody->graph, outside_compilation_attr_name)); + + // Preprocess edges between different outside compilations. They will be + // restored in `ConstructHostGraph()`. + TF_RETURN_IF_ERROR(PreprocessEdgesBetweenOutsideCompilations( + fbody->graph, outside_compilation_attr_name)); + + // Encapsulate outside_compilation cluster into function call node. + auto rewrite_fn = std::make_unique( + xla_cluster_attr_name, outside_compilation_attr_name, xla_cluster_name, + new_func_name); + TF_RETURN_IF_ERROR(EncapsulateSubgraphsInFunctions( + outside_compilation_attr_name, *fbody->graph, *rewrite_fn, + /*reuse_existing_functions=*/true, &graph_out, fld)); + + // Replace outside_compilation function nodes with HostCompute ops. + std::vector outside_compilation_nodes; + for (Node* n : graph_out->nodes()) { + if (HasNodeAttr(n->def(), "_outside_compilation_subgraph")) { + outside_compilation_nodes.push_back(n); + outside_compilation_host_graphs.push_back(n->name()); + + // If we could not infer shapes for XlaSendFromHost inputs statically, + // we will set the "shape_inference_graph" attribute. In that case, copy + // outside compilation subgraph as shape inference graph in `fld`. + auto shape_inference_graph = std::make_unique(); + TF_RETURN_IF_ERROR(GetNodeAttr(n->attrs(), "shape_inference_graph", + shape_inference_graph.get())); + if (!shape_inference_graph->name().empty()) { + shape_inference_graphs->push_back(shape_inference_graph->name()); + shape_inference_graphs_to_rewrite.push_back( + shape_inference_graph->name()); + + const FunctionDef* xla_fdef = fld->Find(n->name()); + if (!xla_fdef) { + return absl::InternalError( + absl::StrCat("Cannot find XLA function ", n->name())); + } + auto shape_inference_fdef = std::make_unique(*xla_fdef); + shape_inference_fdef->mutable_signature()->set_name( + shape_inference_graph->name()); + if (fld->Find(shape_inference_graph->name())) { + TF_RETURN_IF_ERROR(fld->ReplaceFunction( + shape_inference_graph->name(), *shape_inference_fdef)); + } else { + TF_RETURN_IF_ERROR(fld->AddFunctionDef(*shape_inference_fdef)); + } + } + } + } + std::map host_compute_nodes; + for (Node* n : outside_compilation_nodes) { + auto host_compute_node_or = ReplaceOutsideCompilationCallNode( + graph_out.get(), n, host_compute_core, *cluster_deps); + TF_RETURN_IF_ERROR(host_compute_node_or.status()); + Node* host_compute_node = host_compute_node_or.value(); + host_compute_nodes[host_compute_node->name()] = host_compute_node; + } + // For XlaHostCompute nodes with dependencies, add control edges between + // them so XlaCompiler can handle them in correct order. + for (const auto& iter : host_compute_nodes) { + Node* host_compute_node = iter.second; + std::vector token_input_node_names; + TF_RETURN_IF_ERROR(GetNodeAttr(host_compute_node->def(), + kXlaTokenInputNodesAttrName, + &token_input_node_names)); + for (const std::string& node_name : token_input_node_names) { + if (node_name == kXlaTokenArgNodeName) { + continue; + } + + auto iter = host_compute_nodes.find(node_name); + TF_RET_CHECK(iter != host_compute_nodes.end()); + graph_out->AddControlEdge(iter->second, host_compute_node); + } + } + } + + // Handle nodes with associated functions. + Graph* g = (*has_outside_compilation) ? graph_out.get() : fbody->graph; + TF_RETURN_IF_ERROR(ExtractOutsideCompilationForNodesWithAssociatedFunctions( + g, xla_cluster_attr_name, outside_compilation_attr_name, xla_cluster_name, + host_compute_core, flr, fld, &outside_compilation_host_graphs, + shape_inference_graphs, has_outside_compilation)); + + if (*has_outside_compilation) { + // Construct host graph. + std::unique_ptr host_graph; + TF_RETURN_IF_ERROR( + ConstructHostGraph(xla_cluster_name, outside_compilation_attr_name, + outside_compilation_host_graphs, fld, &host_graph)); + auto host_graph_fdef = std::make_unique(); + TF_RETURN_IF_ERROR(GraphToFunctionDef(*host_graph, host_graph_func_name, + HostGraphControlRetMapping, + host_graph_fdef.get())); + if (fld->Find(host_graph_func_name)) { + TF_RETURN_IF_ERROR( + fld->ReplaceFunction(host_graph_func_name, *host_graph_fdef)); + } else { + TF_RETURN_IF_ERROR(fld->AddFunctionDef(*host_graph_fdef)); + } + + // Shape inference graphs might contain Placeholder nodes for outside + // compilation to outside compilation edges. Rewrite shape inference graphs + // to remove such nodes. + for (const std::string& shape_inference_graph : + shape_inference_graphs_to_rewrite) { + TF_RETURN_IF_ERROR( + RewriteShapeInferenceGraph(shape_inference_graph, host_graph.get(), + /*pivot_node=*/nullptr, fld)); + } + + // Remove the outside compilation graphs from function library. + for (const std::string& func : outside_compilation_host_graphs) { + TF_RETURN_IF_ERROR(fld->RemoveFunction(func)); + } + + // Replace original function. + auto updated_fdef = std::make_unique(); + TF_RETURN_IF_ERROR( + GraphToFunctionDef(*g, new_func_name, updated_fdef.get())); + updated_fdef->mutable_signature()->set_is_stateful(true); + const FunctionDef* original_fdef = fld->Find(func_name); + if (original_fdef) { + for (const auto& attr : original_fdef->attr()) { + (*updated_fdef->mutable_attr())[attr.first] = attr.second; + } + } + if (fld->Find(new_func_name)) { + TF_RETURN_IF_ERROR(fld->ReplaceFunction(new_func_name, *updated_fdef)); + } else { + TF_RETURN_IF_ERROR(fld->AddFunctionDef(*updated_fdef)); + } + if (VLOG_IS_ON(4)) { + DumpGraphToFile( + absl::StrCat("extract_outside_compilation_for_func_after_", + func_name), + *g, fld); + } + } + + return ret_status; +} + +absl::Status ExtractOutsideCompilation( + const std::string& xla_cluster_attr_name, + const std::string& outside_compilation_attr_name, + const std::unordered_map& clusters, Graph* g, + FunctionLibraryRuntime* flr, FunctionLibraryDefinition* fld, + bool* modified) { + if (VLOG_IS_ON(4)) { + DumpGraphToFile("extract_outside_compilation_before", *g, fld); + } + + *modified = false; + auto node_name_index = g->BuildNodeNameIndex(); + for (auto& iter : clusters) { + std::string xla_cluster_name = iter.first; + Node* n = iter.second.node; + auto const& func_name_attrs = iter.second.func_name_attrs; + auto const& host_compute_core = iter.second.host_compute_core; + + std::vector shape_inference_graphs; + bool has_outside_compilation; + std::string host_graph_func_name = + absl::StrCat("oc_host_graph_", xla_cluster_name); + TF_RETURN_IF_ERROR(ExtractOutsideCompilationForFunction( + xla_cluster_attr_name, outside_compilation_attr_name, xla_cluster_name, + func_name_attrs, func_name_attrs.name(), host_graph_func_name, + host_compute_core, flr, fld, &shape_inference_graphs, + &has_outside_compilation)); + *modified |= has_outside_compilation; + + if (has_outside_compilation) { + std::string pivot_name = absl::StrCat(xla_cluster_name, "/pivot"); + Node* pivot_node = node_name_index[pivot_name]; + TF_RETURN_IF_ERROR(ExpandHostGraphIntoMainGraph( + g, fld, host_graph_func_name, n, pivot_node)); + + TF_RETURN_IF_ERROR(fld->RemoveFunction(host_graph_func_name)); + + for (const auto& shape_inference_graph_name : shape_inference_graphs) { + TF_RETURN_IF_ERROR(RewriteShapeInferenceGraph( + shape_inference_graph_name, g, pivot_node, fld)); + } + } + } + + if (VLOG_IS_ON(4)) { + DumpGraphToFile("extract_outside_compilation_after", *g, fld); + } + return absl::OkStatus(); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/extract_outside_compilation_pass.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/extract_outside_compilation_pass.h new file mode 100644 index 0000000000000000000000000000000000000000..92cda7449b49d3616149e447341441ebec4f637b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/extract_outside_compilation_pass.h @@ -0,0 +1,113 @@ +/* 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_EXTRACT_OUTSIDE_COMPILATION_PASS_H_ +#define TENSORFLOW_COMPILER_JIT_EXTRACT_OUTSIDE_COMPILATION_PASS_H_ + +#include "absl/types/optional.h" +#include "tensorflow/compiler/jit/encapsulate_util.h" +#include "xla/status_macros.h" +#include "tensorflow/core/graph/graph.h" + +namespace tensorflow { + +// Rewrite function for outside compilation subgraphs. It will perform the +// following steps: +// +// 1. Add a XLA computation key placeholder node (it will be used as input for +// XlaRecvAtHost and XlaSendFromHost); +// 2. Replace all _Arg nodes with one single XlaRecvAtHost node; +// 3. Replace all _Retval nodes with one single XlaSendFromHost node; +// 4. Mark all nodes except key placeholder with attr `xla_cluster_attr_name` +// and `outside_compilation_attr_name`; +// 5. For nodes marked with attr kXlaConnectedToXlaComputationAttrName, add a +// control edge from the node to XlaSendFromHost; for nodes marked with attr +// kXlaConnectedFromXlaComputationAttrName, add a control edge from +// XlaRecvAtHost node to the node; +// 6. Try pruning XlaRecvAtHost/XlaSendFromHost/key placeholder node. +// 7. Add necessary attributes to `node_def`, so we can replace it with a +// XlaHostCompute node later. If all input shapes for XlaSendFromHost are +// known, "shapes" attr will be set to the list of input shapes; otherwise +// "shape_inference_graph" attr will be set to shape inference function name. +class RewriteOutsideCompilationSubgraphFn { + public: + RewriteOutsideCompilationSubgraphFn( + const std::string& xla_cluster_attr_name, + const std::string& outside_compilation_attr_name, + const std::string& xla_cluster_name, const std::string& new_function_name) + : xla_cluster_attr_name_(xla_cluster_attr_name), + outside_compilation_attr_name_(outside_compilation_attr_name), + xla_cluster_name_(xla_cluster_name), + new_function_name_(new_function_name) {} + + absl::Status operator()(const std::vector&, + std::unique_ptr* graph, + std::vector* input_permutation, + std::vector* output_permutation, + NodeDef* node_def); + + private: + std::string xla_cluster_attr_name_; + std::string outside_compilation_attr_name_; + std::string xla_cluster_name_; + std::string new_function_name_; +}; + +// For an XLA computation function, replace all outside compilations with +// XlaHostCompute nodes. Each outside compilation subgraph will be rewritten by +// `RewriteOutsideCompilationSubgraphFn`, and they will be merged into one +// single host side graph (`host_graph`). +// +// xla_cluster_attr_name and outside_compilation_attr_name: attr name for XLA +// computation and outside compilation. Required for +// `RewriteOutsideCompilationSubgraphFn`. +// xla_cluster_name: XLA cluster name for this XLA computation. We need it +// because XLA cluster name might be different from `func_name`. +// func_name_attrs: they will be used to instantiate the XLA computation func. +// new_func_name: new function name for rewritten XLA computation func. +// host_compute_core: mapping from outside compilation cluster name to XLA +// device assignment. +// fld: FunctionLibraryDefinition object. +// host_graph: Graph object to store host side graph for all outside +// compilations within this XLA computation func. If there is no outside +// compilation, it will be empty. +// shape_inference_graphs: a list of outside compilation shape inference +// function names. These functions need to be rewritten later. +// has_outside_compilation: a bool indicating whether this function has any +// outside compilation nodes. +absl::Status ExtractOutsideCompilationForFunction( + const std::string& xla_cluster_attr_name, + const std::string& outside_compilation_attr_name, + const std::string& xla_cluster_name, const NameAttrList& func_name_attrs, + const std::string& new_func_name, const std::string& host_graph_func_name, + const std::map& host_compute_core, + FunctionLibraryRuntime* flr, FunctionLibraryDefinition* fld, + std::vector* shape_inference_graphs, + bool* has_outside_compilation); + +// Rewrites XLA computation in `clusters` to replace outside compilation nodes +// with XlaHostCompute, and moves those outside compilations into `g`. If shapes +// of outside compilation outputs cannot be determined now, we will store shape +// inference graph into `fld`. +absl::Status ExtractOutsideCompilation( + const std::string& xla_cluster_attr_name, + const std::string& outside_compilation_attr_name, + const std::unordered_map& clusters, Graph* g, + FunctionLibraryRuntime* flr, FunctionLibraryDefinition* fld, + bool* modified); + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_EXTRACT_OUTSIDE_COMPILATION_PASS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/extract_outside_compilation_pass_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/extract_outside_compilation_pass_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..2f9ff6c9dc2b866a1a57f911d2ff120eb9ef0067 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/extract_outside_compilation_pass_test.cc @@ -0,0 +1,1025 @@ +/* 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 "tensorflow/compiler/jit/extract_outside_compilation_pass.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "absl/log/check.h" +#include "absl/status/status.h" +#include "absl/strings/match.h" +#include "tensorflow/cc/framework/ops.h" +#include "tensorflow/cc/framework/scope.h" +#include "tensorflow/cc/ops/array_ops.h" +#include "tensorflow/cc/ops/const_op.h" +#include "tensorflow/cc/ops/function_ops.h" +#include "tensorflow/cc/ops/functional_ops.h" +#include "tensorflow/cc/ops/math_ops.h" +#include "tensorflow/compiler/jit/encapsulate_util.h" +#include "xla/hlo/testlib/test.h" +#include "tensorflow/core/common_runtime/device_mgr.h" +#include "tensorflow/core/common_runtime/function.h" +#include "tensorflow/core/framework/device_factory.h" +#include "tensorflow/core/framework/function.h" +#include "tensorflow/core/framework/graph_to_functiondef.h" +#include "tensorflow/core/framework/node_def_builder.h" +#include "tensorflow/core/framework/node_def_util.h" +#include "tensorflow/core/framework/op.h" +#include "tensorflow/core/framework/tensor_shape.h" +#include "tensorflow/core/framework/tensor_shape.pb.h" +#include "tensorflow/core/graph/graph.h" +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/platform/test.h" +#include "tensorflow/core/public/session_options.h" +#include "tensorflow/core/public/version.h" + +namespace tensorflow { + +TEST(RewriteOutsideCompilationSubgraphFnTest, Basic) { + // Build the graph: + // "add" = "arg0" + "arg1" + // "ret0" = "add" + // "ret1" = "arg1" + tensorflow::Scope s = tensorflow::Scope::NewRootScope(); + Output arg0 = ops::_Arg(s.WithOpName("arg0"), DT_INT32, 0); + Output arg1 = ops::_Arg(s.WithOpName("arg1"), DT_FLOAT, 1); + Output arg2 = ops::_Arg(s.WithOpName("arg2"), DT_INT32, 2); + Output add = ops::Add(s.WithOpName("add"), arg0, arg0); + auto ret0 = ops::_Retval(s.WithOpName("ret0"), add, 0); + auto ret1 = ops::_Retval(s.WithOpName("ret1"), arg1, 1); + std::unique_ptr g(new Graph(OpRegistry::Global())); + CHECK_OK(s.ToGraph(g.get())); + auto node_name_image = g->BuildNodeNameIndex(); + Node *add_node = node_name_image["add"]; + EXPECT_NE(add_node, nullptr); + add_node->AddAttr(kXlaConnectedToXlaComputationAttrName, "cluster"); + add_node->AddAttr(kXlaConnectedFromXlaComputationAttrName, "cluster"); + + RewriteOutsideCompilationSubgraphFn rewrite_fn("_xla", "_oc", "cluster", ""); + std::vector arg_source_tensors; + NodeDef call_node_def; + call_node_def.set_op("0"); + CHECK_OK( + rewrite_fn(arg_source_tensors, &g, nullptr, nullptr, &call_node_def)); + node_name_image = g->BuildNodeNameIndex(); + + // Verify step 1: add key placeholder node. + Node *key_placeholder = node_name_image["cluster_key_placeholder"]; + EXPECT_NE(key_placeholder, nullptr); + // Verify step 2: replace _Arg nodes with XlaRecvAtHost. + for (Node *n : g->nodes()) { + EXPECT_NE(n->type_string(), "_Arg"); + } + Node *recv_at_host = node_name_image["outside_compilation_cluster__0_recv"]; + EXPECT_NE(recv_at_host, nullptr); + std::vector recv_at_host_dtypes; + CHECK_OK( + GetNodeAttr(recv_at_host->attrs(), "Toutputs", &recv_at_host_dtypes)); + EXPECT_EQ(recv_at_host_dtypes.size(), 3); + EXPECT_EQ(recv_at_host_dtypes[0], DT_INT32); + EXPECT_EQ(recv_at_host_dtypes[1], DT_FLOAT); + EXPECT_EQ(recv_at_host_dtypes[2], DT_INT32); + // Verify step 3: replace _Retval nodes with XlaSendFromHost. + for (Node *n : g->nodes()) { + EXPECT_NE(n->type_string(), "_Retval"); + } + Node *send_from_host = node_name_image["outside_compilation_cluster__0_send"]; + EXPECT_NE(send_from_host, nullptr); + std::vector send_from_host_dtypes; + CHECK_OK( + GetNodeAttr(send_from_host->attrs(), "Tinputs", &send_from_host_dtypes)); + EXPECT_EQ(send_from_host_dtypes.size(), 2); + EXPECT_EQ(send_from_host_dtypes[0], DT_INT32); + EXPECT_EQ(send_from_host_dtypes[1], DT_FLOAT); + // Verify step 4: nodes marked with XLA cluster and outside compilation attr. + add_node = node_name_image["add"]; + EXPECT_NE(add_node, nullptr); + EXPECT_TRUE(HasNodeAttr(add_node->def(), "_xla")); + EXPECT_TRUE(HasNodeAttr(add_node->def(), "_oc")); + // Verify step 5: control edges added. + bool has_control_edge_from_recv_at_host = false; + for (auto e : add_node->in_edges()) { + if (e->IsControlEdge() && e->src() == recv_at_host) { + has_control_edge_from_recv_at_host = true; + } + } + EXPECT_TRUE(has_control_edge_from_recv_at_host); + bool has_control_edge_to_send_from_host = false; + for (auto e : add_node->out_edges()) { + if (e->IsControlEdge() && e->dst() == send_from_host) { + has_control_edge_to_send_from_host = true; + } + } + EXPECT_TRUE(has_control_edge_to_send_from_host); + // Verify step 7: necessary attrs added to call_node_def. + NameAttrList shape_inference_graph; + CHECK_OK(GetNodeAttr(AttrSlice(&call_node_def.attr()), + "shape_inference_graph", &shape_inference_graph)); + EXPECT_EQ(shape_inference_graph.name(), + "_outside_compilation_shape_inference_cluster__0"); +} + +TEST(RewriteOutsideCompilationSubgraphFnTest, NoSendFromHost) { + // Build the graph: only 1 node: "arg0" + tensorflow::Scope s = tensorflow::Scope::NewRootScope(); + Output arg0 = ops::_Arg(s.WithOpName("arg0"), DT_INT32, 0); + std::unique_ptr g(new Graph(OpRegistry::Global())); + CHECK_OK(s.ToGraph(g.get())); + + RewriteOutsideCompilationSubgraphFn rewrite_fn("_xla", "_oc", "cluster", ""); + std::vector arg_source_tensors; + NodeDef call_node_def; + call_node_def.set_op("0"); + CHECK_OK( + rewrite_fn(arg_source_tensors, &g, nullptr, nullptr, &call_node_def)); + auto node_name_image = g->BuildNodeNameIndex(); + + // Check key placeholder and RecvAtHost is present, but SendFromHost is not. + Node *key_placeholder = node_name_image["cluster_key_placeholder"]; + EXPECT_NE(key_placeholder, nullptr); + Node *recv_at_host = node_name_image["outside_compilation_cluster__0_recv"]; + EXPECT_NE(recv_at_host, nullptr); + Node *send_from_host = node_name_image["outside_compilation_cluster__0_send"]; + EXPECT_EQ(send_from_host, nullptr); +} + +TEST(RewriteOutsideCompilationSubgraphFnTest, NoRecvAtHost) { + // Build the graph: + // "ret" = "const0" + tensorflow::Scope s = tensorflow::Scope::NewRootScope(); + Output const0 = ops::Const(s.WithOpName("const0"), 1, {2}); + auto ret = ops::_Retval(s.WithOpName("ret"), const0, 0); + std::unique_ptr g(new Graph(OpRegistry::Global())); + CHECK_OK(s.ToGraph(g.get())); + + RewriteOutsideCompilationSubgraphFn rewrite_fn("_xla", "_oc", "cluster", ""); + std::vector arg_source_tensors; + NodeDef call_node_def; + call_node_def.set_op("0"); + CHECK_OK( + rewrite_fn(arg_source_tensors, &g, nullptr, nullptr, &call_node_def)); + auto node_name_image = g->BuildNodeNameIndex(); + + // Check key placeholder and SendFromHost is present, but RecvAtHost is not. + Node *key_placeholder = node_name_image["cluster_key_placeholder"]; + EXPECT_NE(key_placeholder, nullptr); + Node *recv_at_host = node_name_image["outside_compilation_cluster__0_recv"]; + EXPECT_EQ(recv_at_host, nullptr); + Node *send_from_host = node_name_image["outside_compilation_cluster__0_send"]; + EXPECT_NE(send_from_host, nullptr); +} + +TEST(RewriteOutsideCompilationSubgraphFnTest, NoKeyPlaceholder) { + // Build the graph: only 1 node: "const0" + tensorflow::Scope s = tensorflow::Scope::NewRootScope(); + Output const0 = ops::Const(s.WithOpName("const0"), 1, {2}); + std::unique_ptr g(new Graph(OpRegistry::Global())); + CHECK_OK(s.ToGraph(g.get())); + + RewriteOutsideCompilationSubgraphFn rewrite_fn("_xla", "_oc", "cluster", ""); + std::vector arg_source_tensors; + NodeDef call_node_def; + call_node_def.set_op("0"); + CHECK_OK( + rewrite_fn(arg_source_tensors, &g, nullptr, nullptr, &call_node_def)); + auto node_name_image = g->BuildNodeNameIndex(); + + // Check key placeholder/RecvAtHost/SendFromHost are not present. + Node *key_placeholder = node_name_image["cluster_key_placeholder"]; + EXPECT_EQ(key_placeholder, nullptr); + Node *recv_at_host = node_name_image["outside_compilation_cluster__0_recv"]; + EXPECT_EQ(recv_at_host, nullptr); + Node *send_from_host = node_name_image["outside_compilation_cluster__0_send"]; + EXPECT_EQ(send_from_host, nullptr); +} + +TEST(RewriteOutsideCompilationSubgraphFnTest, ShapesInferred) { + // Build the graph: + // "ret" = "const0" + tensorflow::Scope s = tensorflow::Scope::NewRootScope(); + Output const0 = ops::Const(s.WithOpName("const0"), 1, {2}); + auto ret = ops::_Retval(s.WithOpName("ret"), const0, 0); + std::unique_ptr g(new Graph(OpRegistry::Global())); + CHECK_OK(s.ToGraph(g.get())); + auto node_name_image = g->BuildNodeNameIndex(); + Node *const0_node = node_name_image["const0"]; + EXPECT_NE(const0_node, nullptr); + PartialTensorShape shape({2}); + const0_node->AddAttr(kXlaInferredShapesAttrName, + std::vector{shape}); + + RewriteOutsideCompilationSubgraphFn rewrite_fn("_xla", "_oc", "cluster", ""); + std::vector arg_source_tensors; + NodeDef call_node_def; + call_node_def.set_op("0"); + CHECK_OK( + rewrite_fn(arg_source_tensors, &g, nullptr, nullptr, &call_node_def)); + node_name_image = g->BuildNodeNameIndex(); + + // Check "shape" attr is available in call_node_def. + std::vector shapes; + CHECK_OK(GetNodeAttr(AttrSlice(&call_node_def.attr()), "shapes", &shapes)); + EXPECT_EQ(shapes.size(), 1); + EXPECT_EQ(shapes[0].dim_size(), 1); +} + +class ExtractOutsideCompilationForFunctionTest : public ::testing::Test { + public: + void SetUp() override { + SessionOptions session_options; + std::vector> devices; + CHECK_OK(DeviceFactory::AddDevices( + session_options, "/job:localhost/replica:0/task:0", &devices)); + device_mgr_ = std::make_unique(std::move(devices)); + } + + absl::Status ExtractOutsideCompilationTest( + const std::string& xla_cluster_attr_name, + const std::string& outside_compilation_attr_name, + const std::string& xla_cluster_name, const NameAttrList& func_name_attrs, + const std::string& new_func_name, const std::string& host_graph_func_name, + const std::map& host_compute_core, + FunctionLibraryDefinition* fld, + std::vector* shape_inference_graphs, + bool* has_outside_compilation) { + OptimizerOptions opts; + pflr_ = std::make_unique( + device_mgr_.get(), Env::Default(), /*config=*/nullptr, + TF_GRAPH_DEF_VERSION, fld, opts, + /*default_thread_pool=*/nullptr); + auto flr = pflr_->GetFLR("/job:localhost/replica:0/task:0/cpu:0"); + return ExtractOutsideCompilationForFunction( + xla_cluster_attr_name, outside_compilation_attr_name, xla_cluster_name, + func_name_attrs, new_func_name, host_graph_func_name, host_compute_core, + flr, fld, shape_inference_graphs, has_outside_compilation); + } + + private: + std::unique_ptr device_mgr_; + std::unique_ptr pflr_; +}; + +TEST_F(ExtractOutsideCompilationForFunctionTest, Basic) { + // Build the XLA computation func. + // "const0" + // "identity0" = "const0" (outside compilation cluster "0") + // "identity1" = "identity0" (outside compilation cluster "1") + // "identity2" = "identity1" + FunctionDefLibrary fdl; + { + tensorflow::Scope s = tensorflow::Scope::NewRootScope(); + Output const0 = ops::Const(s.WithOpName("const0"), 1, {2}); + Output identity0 = ops::Identity(s.WithOpName("identity0"), const0); + Output identity1 = ops::Identity(s.WithOpName("identity1"), identity0); + Output identity2 = ops::Identity(s.WithOpName("identity2"), identity1); + std::unique_ptr g(new Graph(OpRegistry::Global())); + CHECK_OK(s.ToGraph(g.get())); + auto node_name_image = g->BuildNodeNameIndex(); + node_name_image["identity0"]->AddAttr("_oc", "0"); + node_name_image["identity1"]->AddAttr("_oc", "1"); + PartialTensorShape shape({2}); + node_name_image["identity1"]->AddAttr( + kXlaInferredShapesAttrName, std::vector{shape}); + + FunctionDef *xla_fdef = fdl.add_function(); + CHECK_OK(GraphToFunctionDef(*g, "cluster", xla_fdef)); + } + FunctionLibraryDefinition fld(OpRegistry::Global(), fdl); + + protobuf::Map attrs; + std::map host_compute_core = {{"0", 1}, {"1", 0}}; + std::vector shape_inference_graphs; + bool has_outside_compilation; + NameAttrList name_attrs; + name_attrs.set_name("cluster"); + *name_attrs.mutable_attr() = attrs; + CHECK_OK(ExtractOutsideCompilationTest( + "_xla", "_oc", "cluster", name_attrs, "cluster_rewritten", "host_graph", + host_compute_core, &fld, &shape_inference_graphs, + &has_outside_compilation)); + + // Get rewritten XLA computation function. + std::unique_ptr xla_fbody; + CHECK_OK(FunctionDefToBodyHelper(*fld.Find("cluster_rewritten"), AttrSlice(), + &fld, &xla_fbody)); + auto node_name_index = xla_fbody->graph->BuildNodeNameIndex(); + + // Check XlaHostCompute nodes. + Node *host_compute_0 = node_name_index["outside_compilation_0_host_compute"]; + EXPECT_NE(host_compute_0, nullptr); + Node *host_compute_1 = node_name_index["outside_compilation_1_host_compute"]; + EXPECT_NE(host_compute_1, nullptr); + // Check XlaHostCompute nodes' "tpu_core" attr. + int tpu_core; + CHECK_OK(GetNodeAttr(host_compute_0->attrs(), "tpu_core", &tpu_core)); + EXPECT_EQ(tpu_core, 1); + CHECK_OK(GetNodeAttr(host_compute_1->attrs(), "tpu_core", &tpu_core)); + EXPECT_EQ(tpu_core, 0); + // Check XlaHostCompute nodes' "shapes" attr. "0" should not have shapes, and + // "1" should have shapes. + std::vector shapes; + CHECK_OK(GetNodeAttr(host_compute_0->attrs(), "shapes", &shapes)); + EXPECT_EQ(shapes.size(), 0); + CHECK_OK(GetNodeAttr(host_compute_1->attrs(), "shapes", &shapes)); + EXPECT_EQ(shapes.size(), 1); + EXPECT_EQ(shapes[0].dim_size(), 1); + // Check XlaHostCompute nodes' "shape_inference_graph" attr. Both should have + // empty values. + NameAttrList shape_inference_graph; + CHECK_OK(GetNodeAttr(host_compute_0->attrs(), "shape_inference_graph", + &shape_inference_graph)); + EXPECT_EQ(shape_inference_graph.name(), ""); + CHECK_OK(GetNodeAttr(host_compute_1->attrs(), "shape_inference_graph", + &shape_inference_graph)); + EXPECT_EQ(shape_inference_graph.name(), ""); + + // Check `shape_inference_graphs`. + EXPECT_EQ(shape_inference_graphs.size(), 0); + + // Check host graph: verify we have key placeholder and sequencer. + std::unique_ptr host_fbody; + AttrValue device_ordinal_temp_value; + device_ordinal_temp_value.set_i(0); + protobuf::Map host_func_attrs; + host_func_attrs["_device_ordinal"] = device_ordinal_temp_value; + CHECK_OK(FunctionDefToBodyHelper( + *fld.Find("host_graph"), AttrSlice(&host_func_attrs), &fld, &host_fbody)); + Graph *host_graph = host_fbody->graph; + Node *key_placeholder = nullptr, *sequencer = nullptr; + for (Node *n : host_graph->nodes()) { + if (n->type_string() == "Placeholder" && + absl::EndsWith(n->name(), "_key_placeholder")) { + EXPECT_EQ(key_placeholder, nullptr); + key_placeholder = n; + } else if (HasNodeAttr(n->def(), "_xla_host_transfer_sequencer")) { + EXPECT_EQ(sequencer, nullptr); + sequencer = n; + } + } + EXPECT_NE(key_placeholder, nullptr); + EXPECT_NE(sequencer, nullptr); + // Check SendFromHost and RecvAtHost has key placeholder as input, and have + // control edge to sequencer. + int num_send_from_host = 0, num_recv_at_host = 0; + std::vector send_recv_nodes; + for (Node *n : host_graph->nodes()) { + if (n->type_string() == "_XlaSendFromHost") { + num_send_from_host++; + send_recv_nodes.push_back(n); + } else if (n->type_string() == "_XlaRecvAtHost") { + num_recv_at_host++; + send_recv_nodes.push_back(n); + } + } + EXPECT_EQ(num_send_from_host, 1); + EXPECT_EQ(num_recv_at_host, 1); + for (Node *n : send_recv_nodes) { + Node *input_node; + CHECK_OK(n->input_node(n->num_inputs() - 1, &input_node)); + EXPECT_EQ(input_node, key_placeholder); + + bool has_control_edge_to_sequencer = false; + for (const Edge *e : n->out_edges()) { + if (e->IsControlEdge() && e->dst() == sequencer) { + has_control_edge_to_sequencer = true; + break; + } + } + EXPECT_TRUE(has_control_edge_to_sequencer); + } +} + +TEST_F(ExtractOutsideCompilationForFunctionTest, NoHostGraph) { + // Build the XLA computation func. + // "const0" + FunctionDefLibrary fdl; + { + tensorflow::Scope s = tensorflow::Scope::NewRootScope(); + Output const0 = ops::Const(s.WithOpName("const0"), 1, {2}); + std::unique_ptr g(new Graph(OpRegistry::Global())); + CHECK_OK(s.ToGraph(g.get())); + + FunctionDef *xla_fdef = fdl.add_function(); + CHECK_OK(GraphToFunctionDef(*g, "cluster", xla_fdef)); + } + FunctionLibraryDefinition fld(OpRegistry::Global(), fdl); + + protobuf::Map attrs; + std::map host_compute_core = {{"0", 1}, {"1", 0}}; + std::vector shape_inference_graphs; + bool has_outside_compilation; + NameAttrList name_attrs; + name_attrs.set_name("cluster"); + *name_attrs.mutable_attr() = attrs; + CHECK_OK(ExtractOutsideCompilationTest( + "_xla", "_oc", "cluster", name_attrs, "cluster_rewritten", "host_graph", + host_compute_core, &fld, &shape_inference_graphs, + &has_outside_compilation)); + + // Check host graph is not created. + EXPECT_EQ(fld.Find("host_graph"), nullptr); +} + +TEST_F(ExtractOutsideCompilationForFunctionTest, OutsideCompilationInIf) { + // Build the XLA computation func. + // "const0" (bool) + // "const1" (int32) + // "if0" (pred = "const0", input = "const1", then_branch = "true_fn", + // else_branch = "false_fn") + FunctionDefLibrary fdl; + { + tensorflow::Scope s = tensorflow::Scope::NewRootScope(); + Output arg = ops::_Arg(s.WithOpName("arg"), DT_INT32, 0); + Output identity = ops::Identity(s.WithOpName("identity_true_fn"), arg); + ops::_Retval retval(s.WithOpName("retval"), identity, 0); + std::unique_ptr g(new Graph(OpRegistry::Global())); + CHECK_OK(s.ToGraph(g.get())); + auto node_name_image = g->BuildNodeNameIndex(); + node_name_image["identity_true_fn"]->AddAttr("_oc", "0"); + PartialTensorShape shape({2}); + node_name_image["identity_true_fn"]->AddAttr( + kXlaInferredShapesAttrName, std::vector{shape}); + + FunctionDef *true_fn_fdef = fdl.add_function(); + CHECK_OK(GraphToFunctionDef(*g, "true_fn", true_fn_fdef)); + } + { + tensorflow::Scope s = tensorflow::Scope::NewRootScope(); + Output arg = ops::_Arg(s.WithOpName("arg"), DT_INT32, 0); + Output identity = ops::Identity(s.WithOpName("identity_false_fn"), arg); + ops::_Retval retval(s.WithOpName("retval"), identity, 0); + std::unique_ptr g(new Graph(OpRegistry::Global())); + CHECK_OK(s.ToGraph(g.get())); + auto node_name_image = g->BuildNodeNameIndex(); + node_name_image["identity_false_fn"]->AddAttr("_oc", "0"); + PartialTensorShape shape({2}); + node_name_image["identity_false_fn"]->AddAttr( + kXlaInferredShapesAttrName, std::vector{shape}); + + FunctionDef *false_fn_fdef = fdl.add_function(); + CHECK_OK(GraphToFunctionDef(*g, "false_fn", false_fn_fdef)); + } + { + tensorflow::Scope s = tensorflow::Scope::NewRootScope(); + Output cond = ops::Const(s.WithOpName("const0"), true, {2}); + Output input = ops::Const(s.WithOpName("const1"), 1, {2}); + NameAttrList true_fn; + true_fn.set_name("true_fn"); + NameAttrList false_fn; + false_fn.set_name("false_fn"); + auto if_op = ops::If(s.WithOpName("if"), cond, + std::initializer_list{cond, input}, {DT_INT32}, + true_fn, false_fn); + ops::_Retval retval(s.WithOpName("retval"), if_op.output[0], 0); + std::unique_ptr g(new Graph(OpRegistry::Global())); + CHECK_OK(s.ToGraph(g.get())); + + FunctionDef *xla_fdef = fdl.add_function(); + CHECK_OK(GraphToFunctionDef(*g, "cluster", xla_fdef)); + } + FunctionLibraryDefinition fld(OpRegistry::Global(), fdl); + + protobuf::Map attrs; + std::map host_compute_core; + std::vector shape_inference_graphs; + bool has_outside_compilation; + NameAttrList name_attrs; + name_attrs.set_name("cluster"); + *name_attrs.mutable_attr() = attrs; + CHECK_OK(ExtractOutsideCompilationTest( + "_xla", "_oc", "cluster", name_attrs, "cluster_rewritten", "host_graph", + host_compute_core, &fld, &shape_inference_graphs, + &has_outside_compilation)); + + // Check host graph. + { + std::unique_ptr host_fbody; + AttrValue device_ordinal_temp_value; + device_ordinal_temp_value.set_i(0); + protobuf::Map host_func_attrs; + host_func_attrs["_device_ordinal"] = device_ordinal_temp_value; + CHECK_OK(FunctionDefToBodyHelper(*fld.Find("host_graph"), + AttrSlice(&host_func_attrs), &fld, + &host_fbody)); + Graph *host_graph = host_fbody->graph; + auto node_name_index = host_graph->BuildNodeNameIndex(); + + // Verify we have XlaRecvAtHost to receive "If" predicate. + Node *recv_if_pred_node = node_name_index["recv_oc_if_pred_if"]; + EXPECT_NE(recv_if_pred_node, nullptr); + + // Verify we have an "If" to choose outside compilation between then_branch + // and else_branch, and it has `recv_if_pred_node` as cond input. + Node *if_oc_node = node_name_index["oc_if_if"]; + EXPECT_NE(if_oc_node, nullptr); + Node *if_oc_node_cond_input; + CHECK_OK(if_oc_node->input_node(0, &if_oc_node_cond_input)); + EXPECT_EQ(if_oc_node_cond_input, recv_if_pred_node); + + // Check that then_branch outside compilation has node "identity_true_fn". + const FunctionDef *true_def = fld.Find("oc_then_branch_host_if_true_fn"); + EXPECT_NE(true_def, nullptr); + bool has_identity_true_fn_node = false; + for (const auto &node_def : true_def->node_def()) { + if (node_def.name() == "identity_true_fn") { + has_identity_true_fn_node = true; + break; + } + } + EXPECT_TRUE(has_identity_true_fn_node); + + // Check that else_branch outside compilation has node "identity_false_fn". + const FunctionDef *false_def = fld.Find("oc_else_branch_host_if_false_fn"); + EXPECT_NE(false_def, nullptr); + bool has_identity_false_fn_node = false; + for (const auto &node_def : false_def->node_def()) { + if (node_def.name() == "identity_false_fn") { + has_identity_false_fn_node = true; + break; + } + } + EXPECT_TRUE(has_identity_false_fn_node); + } + + // Check XLA graph. + { + std::unique_ptr xla_fbody; + CHECK_OK(FunctionDefToBodyHelper(*fld.Find("cluster_rewritten"), + AttrSlice(), &fld, &xla_fbody)); + Graph *xla_graph = xla_fbody->graph; + auto node_name_index = xla_graph->BuildNodeNameIndex(); + + // Check that we have XlaSendToHost to send cond predicate to host, and + // there is a control edge to If node. + Node *send_if_pred_node = node_name_index["send_oc_if_pred_if"]; + EXPECT_NE(send_if_pred_node, nullptr); + bool has_control_edge_to_if = false; + for (const Edge *e : send_if_pred_node->out_edges()) { + if (e->IsControlEdge() && e->dst()->name() == "if") { + has_control_edge_to_if = true; + break; + } + } + EXPECT_TRUE(has_control_edge_to_if); + + // Check that the "If" node now has `send_if_pred_node` as attribute + // _xla_token_input_nodes. + Node *if_node = node_name_index["if"]; + EXPECT_NE(if_node, nullptr); + std::vector token_inputs; + CHECK_OK( + GetNodeAttr(if_node->def(), "_xla_token_input_nodes", &token_inputs)); + EXPECT_THAT(token_inputs, ::testing::ElementsAre("send_oc_if_pred_if")); + } +} + +TEST_F(ExtractOutsideCompilationForFunctionTest, OutsideCompilationInWhile) { + // Build the XLA computation func. + // "const0" (bool) + // "while0" (input = "const0", cond = "cond_fn", body = "body_fn") + FunctionDefLibrary fdl; + { + tensorflow::Scope s = tensorflow::Scope::NewRootScope(); + Output arg = ops::_Arg(s.WithOpName("arg"), DT_BOOL, 0); + Output identity = ops::Identity(s.WithOpName("identity_cond_fn"), arg); + ops::_Retval retval(s.WithOpName("retval"), identity, 0); + std::unique_ptr g(new Graph(OpRegistry::Global())); + CHECK_OK(s.ToGraph(g.get())); + auto node_name_image = g->BuildNodeNameIndex(); + node_name_image["identity_cond_fn"]->AddAttr("_oc", "0"); + PartialTensorShape shape({2}); + node_name_image["identity_cond_fn"]->AddAttr( + kXlaInferredShapesAttrName, std::vector{shape}); + + FunctionDef *cond_fn_fdef = fdl.add_function(); + CHECK_OK(GraphToFunctionDef(*g, "cond_fn", cond_fn_fdef)); + } + { + tensorflow::Scope s = tensorflow::Scope::NewRootScope(); + Output arg = ops::_Arg(s.WithOpName("arg"), DT_BOOL, 0); + Output identity = ops::Identity(s.WithOpName("identity_body_fn"), arg); + ops::_Retval retval(s.WithOpName("retval"), identity, 0); + std::unique_ptr g(new Graph(OpRegistry::Global())); + CHECK_OK(s.ToGraph(g.get())); + auto node_name_image = g->BuildNodeNameIndex(); + node_name_image["identity_body_fn"]->AddAttr("_oc", "0"); + PartialTensorShape shape({2}); + node_name_image["identity_body_fn"]->AddAttr( + kXlaInferredShapesAttrName, std::vector{shape}); + + FunctionDef *body_fn_fdef = fdl.add_function(); + CHECK_OK(GraphToFunctionDef(*g, "body_fn", body_fn_fdef)); + } + { + tensorflow::Scope s = tensorflow::Scope::NewRootScope(); + Output input = ops::Const(s.WithOpName("const0"), true, {2}); + NameAttrList cond_fn; + cond_fn.set_name("cond_fn"); + NameAttrList body_fn; + body_fn.set_name("body_fn"); + auto while_op = + ops::While(s.WithOpName("while"), std::initializer_list{input}, + cond_fn, body_fn); + ops::_Retval retval(s.WithOpName("retval"), while_op.output[0], 0); + std::unique_ptr g(new Graph(OpRegistry::Global())); + CHECK_OK(s.ToGraph(g.get())); + + FunctionDef *xla_fdef = fdl.add_function(); + CHECK_OK(GraphToFunctionDef(*g, "cluster", xla_fdef)); + } + FunctionLibraryDefinition fld(OpRegistry::Global(), fdl); + + protobuf::Map attrs; + std::map host_compute_core; + std::vector shape_inference_graphs; + bool has_outside_compilation; + NameAttrList name_attrs; + name_attrs.set_name("cluster"); + *name_attrs.mutable_attr() = attrs; + CHECK_OK(ExtractOutsideCompilationTest( + "_xla", "_oc", "cluster", name_attrs, "cluster_rewritten", "host_graph", + host_compute_core, &fld, &shape_inference_graphs, + &has_outside_compilation)); + + // Check host graph. + { + std::unique_ptr host_fbody; + AttrValue device_ordinal_temp_value; + device_ordinal_temp_value.set_i(0); + protobuf::Map host_func_attrs; + host_func_attrs["_device_ordinal"] = device_ordinal_temp_value; + CHECK_OK(FunctionDefToBodyHelper(*fld.Find("host_graph"), + AttrSlice(&host_func_attrs), &fld, + &host_fbody)); + Graph *host_graph = host_fbody->graph; + auto node_name_index = host_graph->BuildNodeNameIndex(); + + // Verify we have an "While" to execute outside compilation. + Node *while_oc_node = node_name_index["oc_while_while"]; + EXPECT_NE(while_oc_node, nullptr); + + // Check that cond outside compilation has node "identity_cond_fn". + const FunctionDef *cond_def = fld.Find("oc_cond_host_while_cond_fn"); + EXPECT_NE(cond_def, nullptr); + bool has_identity_cond_fn_node = false; + for (const auto &node_def : cond_def->node_def()) { + if (node_def.name() == "identity_cond_fn") { + has_identity_cond_fn_node = true; + break; + } + } + EXPECT_TRUE(has_identity_cond_fn_node); + + // Check that body outside compilation has node "identity_body_fn". + const FunctionDef *body_def = fld.Find("oc_body_host_while_body_fn"); + EXPECT_NE(body_def, nullptr); + bool has_identity_body_fn_node = false; + for (const auto &node_def : body_def->node_def()) { + if (node_def.name() == "identity_body_fn") { + has_identity_body_fn_node = true; + break; + } + } + EXPECT_TRUE(has_identity_body_fn_node); + } + + // Check XLA graph. + { + // Verify that rewritten cond fn has XlaSendToHost to send loop predicate to + // host. + const FunctionDef *cond_def = fld.Find("cond_fn_oc"); + EXPECT_NE(cond_def, nullptr); + bool has_send_oc_while_cond_node = false; + for (const auto &node_def : cond_def->node_def()) { + if (node_def.name() == "send_oc_while_cond_while") { + has_send_oc_while_cond_node = true; + break; + } + } + EXPECT_TRUE(has_send_oc_while_cond_node); + } +} + +TEST_F(ExtractOutsideCompilationForFunctionTest, OutsideCompilationInFunction) { + // Build the XLA computation func. + // "const0" (int32) + // "fn" (input = "const0") + FunctionDefLibrary fdl; + { + tensorflow::Scope s = tensorflow::Scope::NewRootScope(); + Output arg = ops::_Arg(s.WithOpName("arg"), DT_INT32, 0); + Output identity = ops::Identity(s.WithOpName("identity"), arg); + ops::_Retval retval(s.WithOpName("retval"), identity, 0); + std::unique_ptr g(new Graph(OpRegistry::Global())); + CHECK_OK(s.ToGraph(g.get())); + auto node_name_image = g->BuildNodeNameIndex(); + node_name_image["identity"]->AddAttr("_oc", "0"); + PartialTensorShape shape({2}); + node_name_image["identity"]->AddAttr( + kXlaInferredShapesAttrName, std::vector{shape}); + + FunctionDef *true_fn_fdef = fdl.add_function(); + CHECK_OK(GraphToFunctionDef(*g, "fn", true_fn_fdef)); + } + FunctionLibraryDefinition fld(OpRegistry::Global(), fdl); + { + std::unique_ptr g(new Graph(&fld)); + + tensorflow::TensorProto tensor_proto; + tensor_proto.set_dtype(tensorflow::DT_INT32); + tensorflow::TensorShapeProto shape; + shape.add_dim()->set_size(2); + *tensor_proto.mutable_tensor_shape() = shape; + for (int i = 0; i < 2; ++i) { + tensor_proto.add_int_val(1); + } + NodeDef const_def; + CHECK_OK(NodeDefBuilder("const", "Const") + .Attr("dtype", DT_INT32) + .Attr("value", tensor_proto) + .Finalize(&const_def)); + absl::Status s; + Node *const_node = g->AddNode(const_def, &s); + CHECK_OK(s); + + NodeDef fn_def; + CHECK_OK(NodeDefBuilder("fn", "fn", &fld) + .Input("const", 0, DT_INT32) + .Finalize(&fn_def)); + Node *fn_node = g->AddNode(fn_def, &s); + CHECK_OK(s); + g->AddEdge(const_node, 0, fn_node, 0); + + NodeDef ret_def; + CHECK_OK(NodeDefBuilder("ret", "_Retval") + .Attr("index", 0) + .Attr("T", DT_INT32) + .Input("fn", 0, DT_INT32) + .Finalize(&ret_def)); + Node *ret_node = g->AddNode(ret_def, &s); + CHECK_OK(s); + g->AddEdge(fn_node, 0, ret_node, 0); + + FunctionDef *xla_fdef = fdl.add_function(); + CHECK_OK(GraphToFunctionDef(*g, "cluster", xla_fdef)); + CHECK_OK(fld.AddFunctionDef(*xla_fdef)); + } + + protobuf::Map attrs; + std::map host_compute_core; + std::vector shape_inference_graphs; + bool has_outside_compilation; + NameAttrList name_attrs; + name_attrs.set_name("cluster"); + *name_attrs.mutable_attr() = attrs; + CHECK_OK(ExtractOutsideCompilationTest( + "_xla", "_oc", "cluster", name_attrs, "cluster_rewritten", "host_graph", + host_compute_core, &fld, &shape_inference_graphs, + &has_outside_compilation)); + + // Check host graph. + { + std::unique_ptr host_fbody; + AttrValue device_ordinal_temp_value; + device_ordinal_temp_value.set_i(0); + protobuf::Map host_func_attrs; + host_func_attrs["_device_ordinal"] = device_ordinal_temp_value; + CHECK_OK(FunctionDefToBodyHelper(*fld.Find("host_graph"), + AttrSlice(&host_func_attrs), &fld, + &host_fbody)); + Graph *host_graph = host_fbody->graph; + auto node_name_index = host_graph->BuildNodeNameIndex(); + + // Verify we have call node for outside compilation in `fn`. + Node *call_node = node_name_index["oc_call_fn"]; + EXPECT_NE(call_node, nullptr); + + std::unique_ptr call_fbody; + CHECK_OK(FunctionDefToBodyHelper(*fld.Find("oc_func_call_host_fn"), + AttrSlice(&host_func_attrs), &fld, + &call_fbody)); + + // Verify we have _XlaRecvAtHost and _XlaSendFromHost nodes. + bool has_recv = false, has_send = false; + for (Node *n : call_fbody->graph->nodes()) { + if (n->type_string() == "_XlaRecvAtHost") { + has_recv = true; + } else if (n->type_string() == "_XlaSendFromHost") { + has_send = true; + } + } + EXPECT_TRUE(has_recv); + EXPECT_TRUE(has_send); + } + + // Check XLA graph. + { + std::unique_ptr xla_fbody; + CHECK_OK(FunctionDefToBodyHelper(*fld.Find("cluster_rewritten"), + AttrSlice(), &fld, &xla_fbody)); + Graph *xla_graph = xla_fbody->graph; + auto node_name_index = xla_graph->BuildNodeNameIndex(); + + // Check that we have call node. + Node *fn_node = node_name_index["fn"]; + EXPECT_NE(fn_node, nullptr); + EXPECT_EQ(fn_node->type_string(), "fn_oc"); + + std::unique_ptr call_fbody; + CHECK_OK(FunctionDefToBodyHelper(*fld.Find("fn_oc"), AttrSlice(), &fld, + &call_fbody)); + + // Verify we have XlaHostCompute nodes. + bool has_hc = false; + for (Node *n : call_fbody->graph->nodes()) { + if (n->type_string() == "XlaHostCompute") { + has_hc = true; + } + } + EXPECT_TRUE(has_hc); + } +} + +TEST_F(ExtractOutsideCompilationForFunctionTest, + OutsideCompilationClusterDataDependency) { + // Build the XLA computation func. + // "const0" + // "identity0" = "const0" (outside compilation cluster "0") + // "identity1" = "identity0" (outside compilation cluster "1") + // "identity2" = "identity1" + FunctionDefLibrary fdl; + { + tensorflow::Scope s = tensorflow::Scope::NewRootScope(); + Output const0 = ops::Const(s.WithOpName("const0"), 1, {2}); + Output identity0 = ops::Identity(s.WithOpName("identity0"), const0); + Output identity1 = ops::Identity(s.WithOpName("identity1"), identity0); + Output identity2 = ops::Identity(s.WithOpName("identity2"), identity1); + std::unique_ptr g(new Graph(OpRegistry::Global())); + CHECK_OK(s.ToGraph(g.get())); + std::cout << "Graph is " << (*g).ToGraphDefDebug().DebugString() + << std::endl; + auto node_name_image = g->BuildNodeNameIndex(); + node_name_image["identity0"]->AddAttr("_oc", "0"); + node_name_image["identity1"]->AddAttr("_oc", "1"); + + PartialTensorShape shape({2}); + node_name_image["identity1"]->AddAttr( + kXlaInferredShapesAttrName, std::vector{shape}); + + FunctionDef *xla_fdef = fdl.add_function(); + CHECK_OK(GraphToFunctionDef(*g, "cluster", xla_fdef)); + } + FunctionLibraryDefinition fld(OpRegistry::Global(), fdl); + + protobuf::Map attrs; + std::map host_compute_core = {{"0", 1}, {"1", 0}}; + std::vector shape_inference_graphs; + bool has_outside_compilation; + NameAttrList name_attrs; + name_attrs.set_name("cluster"); + *name_attrs.mutable_attr() = attrs; + CHECK_OK(ExtractOutsideCompilationTest( + "_xla", "_oc", "cluster", name_attrs, "cluster_rewritten", "host_graph", + host_compute_core, &fld, &shape_inference_graphs, + &has_outside_compilation)); + + // Get rewritten XLA computation function. + std::unique_ptr xla_fbody; + CHECK_OK(FunctionDefToBodyHelper(*fld.Find("cluster_rewritten"), AttrSlice(), + &fld, &xla_fbody)); + auto node_name_index = xla_fbody->graph->BuildNodeNameIndex(); + + // Check XlaHostCompute nodes. + Node *host_compute_0 = node_name_index["outside_compilation_0_host_compute"]; + EXPECT_NE(host_compute_0, nullptr); + Node *host_compute_1 = node_name_index["outside_compilation_1_host_compute"]; + EXPECT_NE(host_compute_1, nullptr); + + // Check XlaHostCompute nodes' "_xla_token_input_nodes" attr. + std::vector token_input_nodes; + CHECK_OK(GetNodeAttr(AttrSlice(host_compute_0->attrs()), + "_xla_token_input_nodes", &token_input_nodes)); + + std::vector expected_token_input_nodes_0( + {"_xla_token_arg_node"}); + EXPECT_EQ(token_input_nodes, expected_token_input_nodes_0); + token_input_nodes.clear(); + std::vector expected_token_input_nodes_1( + {"_xla_token_arg_node", "outside_compilation_0_host_compute"}); + CHECK_OK(GetNodeAttr(AttrSlice(host_compute_1->attrs()), + "_xla_token_input_nodes", &token_input_nodes)); + EXPECT_EQ(token_input_nodes, expected_token_input_nodes_1); + + // Check there is a control edge from host_compute_0 to host_compute_1. + bool has_control_edge = false; + for (const Edge *e : host_compute_1->in_edges()) { + if (e->IsControlEdge() && e->src() == host_compute_0) { + has_control_edge = true; + break; + } + } + EXPECT_TRUE(has_control_edge); +} + +TEST_F(ExtractOutsideCompilationForFunctionTest, + OutsideCompilationClusterControlDependency) { + // Build the XLA computation func. + // "const0" + // "identity0" = "const0" (outside compilation cluster "0") + // "identity1" = "const0" "^identity0" (outside compilation cluster "1", + // control dependent on cluster "0") + // "identity2" = "identity1" + FunctionDefLibrary fdl; + { + tensorflow::Scope s = tensorflow::Scope::NewRootScope(); + Output const0 = ops::Const(s.WithOpName("const0"), 1, {2}); + Output identity0 = ops::Identity(s.WithOpName("identity0"), const0); + Output identity1 = ops::Identity( + s.WithOpName("identity1").WithControlDependencies(identity0), const0); + Output identity2 = ops::Identity(s.WithOpName("identity2"), identity1); + std::unique_ptr g(new Graph(OpRegistry::Global())); + CHECK_OK(s.ToGraph(g.get())); + std::cout << "Graph is " << (*g).ToGraphDefDebug().DebugString() + << std::endl; + auto node_name_image = g->BuildNodeNameIndex(); + node_name_image["identity0"]->AddAttr("_oc", "0"); + node_name_image["identity1"]->AddAttr("_oc", "1"); + + PartialTensorShape shape({2}); + node_name_image["identity1"]->AddAttr( + kXlaInferredShapesAttrName, std::vector{shape}); + + FunctionDef *xla_fdef = fdl.add_function(); + CHECK_OK(GraphToFunctionDef(*g, "cluster", xla_fdef)); + } + FunctionLibraryDefinition fld(OpRegistry::Global(), fdl); + + protobuf::Map attrs; + std::map host_compute_core = {{"0", 1}, {"1", 0}}; + std::vector shape_inference_graphs; + bool has_outside_compilation; + NameAttrList name_attrs; + name_attrs.set_name("cluster"); + *name_attrs.mutable_attr() = attrs; + CHECK_OK(ExtractOutsideCompilationTest( + "_xla", "_oc", "cluster", name_attrs, "cluster_rewritten", "host_graph", + host_compute_core, &fld, &shape_inference_graphs, + &has_outside_compilation)); + + // Get rewritten XLA computation function. + std::unique_ptr xla_fbody; + CHECK_OK(FunctionDefToBodyHelper(*fld.Find("cluster_rewritten"), AttrSlice(), + &fld, &xla_fbody)); + auto node_name_index = xla_fbody->graph->BuildNodeNameIndex(); + + // Check XlaHostCompute nodes. + Node *host_compute_0 = node_name_index["outside_compilation_0_host_compute"]; + EXPECT_NE(host_compute_0, nullptr); + Node *host_compute_1 = node_name_index["outside_compilation_1_host_compute"]; + EXPECT_NE(host_compute_1, nullptr); + + // Check XlaHostCompute nodes' "_xla_token_input_nodes" attr. + std::vector token_input_nodes; + CHECK_OK(GetNodeAttr(AttrSlice(host_compute_0->attrs()), + "_xla_token_input_nodes", &token_input_nodes)); + + std::vector expected_token_input_nodes_0( + {"_xla_token_arg_node"}); + EXPECT_EQ(token_input_nodes, expected_token_input_nodes_0); + token_input_nodes.clear(); + std::vector expected_token_input_nodes_1( + {"_xla_token_arg_node", "outside_compilation_0_host_compute"}); + CHECK_OK(GetNodeAttr(AttrSlice(host_compute_1->attrs()), + "_xla_token_input_nodes", &token_input_nodes)); + EXPECT_EQ(token_input_nodes, expected_token_input_nodes_1); + + // Check there is a control edge from host_compute_0 to host_compute_1. + bool has_control_edge = false; + for (const Edge *e : host_compute_1->in_edges()) { + if (e->IsControlEdge() && e->src() == host_compute_0) { + has_control_edge = true; + break; + } + } + EXPECT_TRUE(has_control_edge); +} +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/flags.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/flags.cc new file mode 100644 index 0000000000000000000000000000000000000000..cfa4aabec0f158396180b5bc0e31ea25a1e34519 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/flags.cc @@ -0,0 +1,557 @@ +/* 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 "tensorflow/compiler/jit/flags.h" + +#include +#include // NOLINT +#include +#include + +#include "absl/base/call_once.h" +#include "absl/strings/numbers.h" +#include "absl/strings/str_split.h" +#include "absl/strings/strip.h" +#include "tensorflow/compiler/mlir/tensorflow/utils/dump_graph.h" +#include "xla/parse_flags_from_env.h" +#include "tensorflow/core/platform/macros.h" +#include "tensorflow/core/tpu/kernels/sparse_core_xla_flags_defaults.h" +#include "tensorflow/core/util/command_line_flags.h" + +namespace tensorflow { +namespace { + +BuildXlaOpsPassFlags* build_ops_flags; +MarkForCompilationPassFlags* mark_for_compilation_flags; +XlaDeviceFlags* device_flags; +XlaSparseCoreFlags* sparse_core_flags; +XlaOpsCommonFlags* ops_flags; +XlaCallModuleFlags* call_module_flags; +MlirCommonFlags* mlir_flags; +JitRtFlags* jitrt_flags; +std::vector* jitrt_flag_list; + +std::vector* flag_list; +absl::once_flag flags_init; + +bool SetterForXlaAutoJitFlag(const std::string& value) { + int32_t opt_level; + // We need to use the mark_for_compilation_flags directly here instead of + // going via GetMarkForCompilationPassFlags() to avoid infinite recursion. The + // latter will try to setup and parse flags, which would bring us back to this + // setter. + if (absl::SimpleAtoi(value, &opt_level)) { + mark_for_compilation_flags->xla_auto_jit_flag + .optimization_level_single_gpu = opt_level; + mark_for_compilation_flags->xla_auto_jit_flag.optimization_level_general = + opt_level; + return true; + } + + if (value == "fusible") { + mark_for_compilation_flags->xla_auto_jit_flag + .optimization_level_single_gpu = 1; + mark_for_compilation_flags->xla_auto_jit_flag.optimization_level_general = + 1; + mark_for_compilation_flags->tf_xla_ops_to_cluster = "FUSIBLE"; + return true; + } + + absl::string_view value_sv(value); + if (!absl::ConsumePrefix(&value_sv, "single-gpu(") || + !absl::ConsumeSuffix(&value_sv, ")") || + !absl::SimpleAtoi(value_sv, &opt_level)) { + return false; + } + + mark_for_compilation_flags->xla_auto_jit_flag.optimization_level_single_gpu = + opt_level; + return true; +} + +bool SetterForXlaCallModuleDisabledChecks(const std::string& value) { + auto directives = absl::StrSplit(value, ',', absl::SkipEmpty()); + call_module_flags->disabled_checks.insert(directives.begin(), + directives.end()); + return true; +} + +void AppendMarkForCompilationPassFlagsInternal(std::vector* flag_list) { + std::vector new_flags = { + Flag("tf_xla_auto_jit", SetterForXlaAutoJitFlag, "0", + "Control compilation of operators into XLA computations on CPU and " + "GPU devices. 0 = use ConfigProto setting; -1 = off; 1 = on for " + "things very likely to be improved; 2 = on for everything; " + "(experimental) fusible = only for Tensorflow operations that XLA " + "knows how to fuse. " + "If set to single-gpu() then this resolves to for single-GPU " + "graphs (graphs that have at least one node placed on a GPU and no " + "more than one GPU is in use through the entire graph) and 0 " + "otherwise. Experimental."), + Flag("tf_xla_min_cluster_size", + &mark_for_compilation_flags->tf_xla_min_cluster_size, + "Minimum number of operators in an XLA compilation. Ignored for " + "operators placed on an XLA device or operators explicitly marked " + "for compilation."), + Flag("tf_xla_max_cluster_size", + &mark_for_compilation_flags->tf_xla_max_cluster_size, + "Maximum number of operators in an XLA compilation."), + Flag( + "tf_xla_ops_to_cluster", + &mark_for_compilation_flags->tf_xla_ops_to_cluster, + "(experimental) " + "Limit the operations clustered by XLA to these operations. " + "If multiple, separate them with commas. Shortcuts: " + " PW: All point-wise operations." + " RED: All reduction operations." + " MISC: Mixed operations." + " PWRED: TF operations that get converted to PW+RED operation in XLA." + " REDUCEWINDOW: TF operations like MaxPool/AvgPool that get " + "converted to ReduceWindow in XLA." + " REDUCEWINDOWPW: Operation that get converted to ReduceWindow + PW " + "(LRN, LRNGrad)." + " BN: TF FusedBatchNorm* operations." + " FUSIBLE: All TF operations that XLA can fuse (All the above). " + "You can also put any TF operation name, e.g. 'FUSIBLE,MatMul'."), + Flag("tf_xla_cluster_exclude_ops", + &mark_for_compilation_flags->tf_xla_cluster_exclude_ops, + "(experimental) " + "Exclude the operations from auto-clustering. " + "If multiple, separate them with commas." + " Where, Some_other_ops"), + Flag("tf_xla_clustering_debug", + &mark_for_compilation_flags->tf_xla_clustering_debug, + "Dump graphs during XLA compilation."), + Flag("tf_xla_cpu_global_jit", + &mark_for_compilation_flags->tf_xla_cpu_global_jit, + "Enables global JIT compilation for CPU via SessionOptions."), + Flag("tf_xla_clustering_fuel", + &mark_for_compilation_flags->tf_xla_clustering_fuel, + "Places an artificial limit on the number of ops marked as " + "eligible for clustering."), + Flag("tf_xla_disable_deadness_safety_checks_for_debugging", + &mark_for_compilation_flags + ->tf_xla_disable_deadness_safety_checks_for_debugging, + "Disable deadness related safety checks when clustering (this is " + "unsound)."), + Flag("tf_xla_disable_resource_variable_safety_checks_for_debugging", + &mark_for_compilation_flags + ->tf_xla_disable_resource_variable_safety_checks_for_debugging, + "Disable resource variables related safety checks when clustering " + "(this is unsound)."), + Flag("tf_xla_deterministic_cluster_names", + &mark_for_compilation_flags->tf_xla_deterministic_cluster_names, + "Causes the function names assigned by auto clustering to be " + "deterministic from run to run."), + Flag("tf_xla_persistent_cache_directory", + &mark_for_compilation_flags->tf_xla_persistent_cache_directory, + "If non-empty, JIT-compiled executables are saved to and loaded " + "from the specified file system directory path. Empty by default."), + Flag("tf_xla_persistent_cache_device_types", + &mark_for_compilation_flags->tf_xla_persistent_cache_device_types, + "If non-empty, the persistent cache will only be used for the " + "specified devices (comma separated). Each device type should be " + "able to be converted to `DeviceType`."), + Flag("tf_xla_persistent_cache_read_only", + &mark_for_compilation_flags->tf_xla_persistent_cache_read_only, + "If true, the persistent cache will be read-only."), + Flag("tf_xla_disable_strict_signature_checks", + &mark_for_compilation_flags->tf_xla_disable_strict_signature_checks, + "If true, entires loaded into the XLA compile cache will not have " + "their signatures checked strictly. Defaults to false."), + Flag("tf_xla_persistent_cache_prefix", + &mark_for_compilation_flags->tf_xla_persistent_cache_prefix, + "Specifies the persistance cache prefix. Default is " + "\"xla_compile_cache\""), + Flag("tf_xla_sparse_core_disable_table_stacking", + &sparse_core_flags->tf_xla_sparse_core_disable_table_stacking, + "Disable table stacking for all the tables passed to the SparseCore" + "mid level API."), + Flag("tf_xla_sparse_core_minibatch_max_division_level", + &sparse_core_flags->tf_xla_sparse_core_minibatch_max_division_level, + "Max level of division to split input data into minibatches."), + Flag("tf_xla_sparse_core_stacking_mem_limit_bytes", + &sparse_core_flags->tf_xla_sparse_core_stacking_mem_limit_bytes, + "If non-zero, limits the size of the activations for a given table" + "to be below these many bytes."), + Flag("tf_xla_sparse_core_stacking_table_shard_limit_bytes", + &sparse_core_flags + ->tf_xla_sparse_core_stacking_table_shard_limit_bytes, + "If non-zero, limits the size of any table shard to be below these" + "many bytes.")}; + flag_list->insert(flag_list->end(), new_flags.begin(), new_flags.end()); +} + +void AllocateAndParseJitRtFlags() { + jitrt_flags = new JitRtFlags; + jitrt_flags->always_specialize = false; + jitrt_flags->cost_driven_async_parallel_for = false; + jitrt_flags->enable_crash_reproducer = false; + jitrt_flags->log_query_of_death = false; + jitrt_flags->vectorize = false; + jitrt_flag_list = new std::vector({ + Flag("always_specialize", &jitrt_flags->always_specialize, ""), + Flag("cost_driven_async_parallel_for", + &jitrt_flags->cost_driven_async_parallel_for, ""), + Flag("enable_crash_reproducer", &jitrt_flags->enable_crash_reproducer, + ""), + Flag("log_query_of_death", &jitrt_flags->log_query_of_death, ""), + Flag("vectorize", &jitrt_flags->vectorize, ""), + }); + xla::ParseFlagsFromEnvAndDieIfUnknown("TF_JITRT_FLAGS", *jitrt_flag_list); +} + +void AllocateAndParseFlags() { + build_ops_flags = new BuildXlaOpsPassFlags; + build_ops_flags->tf_xla_enable_lazy_compilation = true; + build_ops_flags->tf_xla_print_cluster_outputs = false; + build_ops_flags->tf_xla_check_cluster_input_numerics = false; + build_ops_flags->tf_xla_check_cluster_output_numerics = false; + build_ops_flags->tf_xla_disable_constant_folding = false; + build_ops_flags->tf_xla_disable_full_embedding_pipelining = false; + build_ops_flags->tf_xla_disable_full_embedding_pipelining_with_summaries = + true; + build_ops_flags->tf_xla_embedding_parallel_iterations = 0; + + mark_for_compilation_flags = new MarkForCompilationPassFlags; + mark_for_compilation_flags->xla_auto_jit_flag.optimization_level_single_gpu = + 0; + mark_for_compilation_flags->xla_auto_jit_flag.optimization_level_general = 0; + mark_for_compilation_flags->tf_xla_min_cluster_size = 4; + mark_for_compilation_flags->tf_xla_max_cluster_size = + std::numeric_limits::max(); + mark_for_compilation_flags->tf_xla_clustering_debug = false; + mark_for_compilation_flags->tf_xla_cpu_global_jit = false; + mark_for_compilation_flags->tf_xla_clustering_fuel = + std::numeric_limits::max(); + mark_for_compilation_flags + ->tf_xla_disable_deadness_safety_checks_for_debugging = false; + mark_for_compilation_flags + ->tf_xla_disable_resource_variable_safety_checks_for_debugging = false; + mark_for_compilation_flags->tf_xla_deterministic_cluster_names = false; + mark_for_compilation_flags->tf_xla_persistent_cache_directory = ""; + mark_for_compilation_flags->tf_xla_persistent_cache_device_types = ""; + mark_for_compilation_flags->tf_xla_persistent_cache_read_only = false; + mark_for_compilation_flags->tf_xla_disable_strict_signature_checks = false; + mark_for_compilation_flags->tf_xla_persistent_cache_prefix = + "xla_compile_cache"; + + device_flags = new XlaDeviceFlags; + device_flags->tf_xla_compile_on_demand = false; + device_flags->tf_xla_enable_xla_devices = false; + + sparse_core_flags = new XlaSparseCoreFlags; + sparse_core_flags->tf_xla_sparse_core_minibatch_max_division_level = + kDefaultSparseCoreMinibatchMaxDivisionLevel; + sparse_core_flags->tf_xla_sparse_core_disable_table_stacking = + kDefaultDisableTableStacking; + sparse_core_flags->tf_xla_sparse_core_stacking_mem_limit_bytes = + kDefaultXlaSparseCoreStackingMemLimit; + sparse_core_flags->tf_xla_sparse_core_stacking_table_shard_limit_bytes = + kDefaultXlaSparseCoreStackingTableShardLimit; + + ops_flags = new XlaOpsCommonFlags; + ops_flags->tf_xla_always_defer_compilation = false; + ops_flags->tf_xla_async_compilation = false; + ops_flags->tf_xla_use_device_api.enabled_for_xla_launch_ = true; + ops_flags->tf_xla_use_device_api.enabled_for_compile_on_demand_ = true; + ops_flags->tf_xla_use_device_api.enabled_for_compile_and_run_ = true; + ops_flags->tf_xla_use_device_api.enabled_for_all_ = false; + ops_flags->tf_xla_use_device_api.enabled_for_gpu_ = true; + + call_module_flags = new XlaCallModuleFlags; + // The `enable_mlir_bridge` flag allows the user to explicitly request that + // their program is (or isn't) compiled using the MLIR-based TF-to-XLA bridge. + // + // The `enable_mlir_bridge_is_explicit` variable tracks whether or not the + // user has made an explicit request. That is, if this variable is set to + // true, the program honors the user's request as per `enable_mlir_bridge`; if + // it's set to false, the default behavior is used (which may run either + // bridge, on a per-graph basis). + bool enable_mlir_bridge = false; + bool enable_mlir_bridge_is_explicit = false; + bool enable_mlir_merge_control_flow_pass = true; + bool enable_mlir_convert_control_to_data_outputs_pass = false; + bool enable_mlir_composite_tpuexecute_side_effects = false; + bool enable_mlir_strict_clusters = false; + bool enable_mlir_multiple_local_cpu_devices = false; + bool enable_mlir_debug_info_serialization = true; + // Dump graphs in TFG dialect. + bool use_tfg_graph_dumper = false; + bool enable_tpu_variable_runtime_reformatting_pass = true; + bool enable_serialize_mlir_to_compressed_bytecode = false; + + flag_list = new std::vector( + {Flag("tf_xla_enable_lazy_compilation", + &build_ops_flags->tf_xla_enable_lazy_compilation, ""), + Flag("tf_xla_print_cluster_outputs", + &build_ops_flags->tf_xla_print_cluster_outputs, + "If true then insert Print nodes to print out values produced by " + "XLA clusters."), + Flag("tf_xla_check_cluster_input_numerics", + &build_ops_flags->tf_xla_check_cluster_input_numerics, + "If true then insert CheckNumerics nodes to check all cluster " + "inputs."), + Flag("tf_xla_check_cluster_output_numerics", + &build_ops_flags->tf_xla_check_cluster_output_numerics, + "If true then insert CheckNumerics nodes to check all cluster " + "outputs."), + Flag("tf_xla_disable_constant_folding", + &build_ops_flags->tf_xla_disable_constant_folding, + "If true then disables constant folding on TF graph before XLA " + "compilation."), + Flag("tf_xla_disable_full_embedding_pipelining", + &build_ops_flags->tf_xla_disable_full_embedding_pipelining, + "If true then disables full embedding pipelining and instead use " + "strict SparseCore / TensorCore sequencing."), + Flag("tf_xla_disable_full_embedding_pipelining_with_summaries", + &build_ops_flags + ->tf_xla_disable_full_embedding_pipelining_with_summaries, + "If true then disables full embedding pipelining when summary ops " + "are detected."), + Flag("tf_xla_embedding_parallel_iterations", + &build_ops_flags->tf_xla_embedding_parallel_iterations, + "If >0 then use this many parallel iterations in " + "embedding_pipelining and embedding_sequency. By default, use the " + "parallel_iterations on the original model WhileOp."), + + Flag("tf_xla_compile_on_demand", &device_flags->tf_xla_compile_on_demand, + "Switch a device into 'on-demand' mode, where instead of " + "autoclustering ops are compiled one by one just-in-time."), + + Flag("tf_xla_enable_xla_devices", + &device_flags->tf_xla_enable_xla_devices, + "Generate XLA_* devices, where placing a computation on such a " + "device" + "forces compilation by XLA. Deprecated."), + + Flag("tf_xla_always_defer_compilation", + &ops_flags->tf_xla_always_defer_compilation, ""), + Flag("tf_xla_async_compilation", &ops_flags->tf_xla_async_compilation, + "When lazy compilation is enabled, asynchronous compilation starts " + "the cluster compilation in the background, and the fallback path " + "is executed until the compilation has finished."), + Flag("tf_xla_use_device_api_for_xla_launch", + &ops_flags->tf_xla_use_device_api.enabled_for_xla_launch_, + "If true, uses Device API (PjRt) for single device compilation and " + "execution of functions marked for JIT compilation i.e. " + "jit_compile=True. Defaults to false."), + Flag("tf_xla_use_device_api_for_compile_on_demand", + &ops_flags->tf_xla_use_device_api.enabled_for_compile_on_demand_, + "If true, uses Device API (PjRt) for compiling and executing ops " + "one by one in 'on-demand' mode. Defaults to false."), + Flag("tf_xla_use_device_api_for_auto_jit", + &ops_flags->tf_xla_use_device_api.enabled_for_compile_and_run_, + "If true, uses Device API (PjRt) for compilation and execution " + "when auto-clustering is enabled. Defaults to false."), + Flag("tf_xla_use_device_api", + &ops_flags->tf_xla_use_device_api.enabled_for_all_, + "If true, uses Device API (PjRt) for compilation and execution " + "of ops one-by-one in 'on-demand' mode, for functions marked for " + "JIT compilation, or when auto-clustering is enabled. Defaults to " + "false."), + Flag("tf_xla_enable_device_api_for_gpu", + &ops_flags->tf_xla_use_device_api.enabled_for_gpu_, + "If true, uses Device API (PjRt) for TF GPU device. This is a " + "helper flag so that individual tests can turn on PjRt for GPU " + "specifically."), + + Flag("tf_xla_call_module_disabled_checks", + SetterForXlaCallModuleDisabledChecks, "", + "A comma-sepated list of directives specifying the safety checks " + "to be skipped when compiling XlaCallModuleOp. See the op " + "documentation for the recognized values."), + + Flag("tf_mlir_enable_mlir_bridge", &enable_mlir_bridge, + "Enables experimental MLIR-Based TensorFlow Compiler Bridge.", + &enable_mlir_bridge_is_explicit), + Flag("tf_mlir_enable_merge_control_flow_pass", + &enable_mlir_merge_control_flow_pass, + "Enables MergeControlFlow pass for MLIR-Based TensorFlow Compiler " + "Bridge."), + Flag("tf_mlir_enable_convert_control_to_data_outputs_pass", + &enable_mlir_convert_control_to_data_outputs_pass, + "Enables `tf-executor-convert-control-to-data-outputs` pass for " + "MLIR-Based TensorFlow Compiler Bridge."), + Flag("tf_mlir_composite_tpuexecute_side_effects", + &enable_mlir_composite_tpuexecute_side_effects, + "Enables certain TPUExecute ops to run in parallel if they only " + "operate on resources that live on composite devices."), + Flag("tf_mlir_enable_strict_clusters", &enable_mlir_strict_clusters, + "Do not allow clusters that have cyclic control dependencies."), + Flag("tf_mlir_enable_multiple_local_cpu_devices", + &enable_mlir_multiple_local_cpu_devices, + "Enable multiple local CPU devices. CPU ops which are outside " + "compiled inside the tpu cluster will also be replicated across " + "multiple cpu devices."), + Flag("tf_mlir_enable_debug_info_serialization", + &enable_mlir_debug_info_serialization, + "Enable debug info serialization in MLIR."), + Flag("tf_dump_graphs_in_tfg", &use_tfg_graph_dumper, + "When tf_dump_graphs_in_tfg is true, graphs after transformations " + "are dumped in MLIR TFG dialect and not in GraphDef"), + Flag("tf_mlir_enable_tpu_variable_runtime_reformatting_pass", + &enable_tpu_variable_runtime_reformatting_pass, + "Enables TPUVariableRuntimeReformatting pass for MLIR-Based " + "TensorFlow Compiler Bridge. This enables weight update sharding " + "and creates TPUReshardVariables ops."), + Flag("tf_serialize_mlir_to_compressed_bytecode", + &enable_serialize_mlir_to_compressed_bytecode, + "If true, serialize MLIR to compressed bytecode.")}); + + AppendMarkForCompilationPassFlagsInternal(flag_list); + xla::ParseFlagsFromEnvAndDieIfUnknown("TF_XLA_FLAGS", *flag_list); + + mlir_flags = new MlirCommonFlags; + if (!enable_mlir_bridge_is_explicit) { + mlir_flags->tf_mlir_enable_mlir_bridge = + ConfigProto::Experimental::MLIR_BRIDGE_ROLLOUT_UNSPECIFIED; + } else if (enable_mlir_bridge) { + mlir_flags->tf_mlir_enable_mlir_bridge = + ConfigProto::Experimental::MLIR_BRIDGE_ROLLOUT_ENABLED; + } else { + mlir_flags->tf_mlir_enable_mlir_bridge = + ConfigProto::Experimental::MLIR_BRIDGE_ROLLOUT_DISABLED; + } + mlir_flags->tf_mlir_enable_merge_control_flow_pass = + enable_mlir_merge_control_flow_pass; + mlir_flags->tf_mlir_enable_convert_control_to_data_outputs_pass = + enable_mlir_convert_control_to_data_outputs_pass; + mlir_flags->tf_mlir_enable_composite_tpuexecute_side_effects = + enable_mlir_composite_tpuexecute_side_effects; + mlir_flags->tf_mlir_enable_strict_clusters = enable_mlir_strict_clusters; + mlir_flags->tf_mlir_enable_tpu_variable_runtime_reformatting_pass = + enable_tpu_variable_runtime_reformatting_pass; + mlir_flags->tf_mlir_enable_multiple_local_cpu_devices = + enable_mlir_multiple_local_cpu_devices; + mlir_flags->tf_mlir_enable_debug_info_serialization = + enable_mlir_debug_info_serialization; + mlir_flags->tf_serialize_mlir_to_compressed_bytecode = + enable_serialize_mlir_to_compressed_bytecode; + + if (use_tfg_graph_dumper) { + UseMlirForGraphDump(MlirDumpConfig{}.elide_large_attributes().emit_dialect( + MlirDumpConfig::Dialect::kTFG)); + } + + AllocateAndParseJitRtFlags(); +} + +void ResetFlags() { + delete build_ops_flags; + delete mark_for_compilation_flags; + delete device_flags; + delete ops_flags; + delete mlir_flags; + delete flag_list; + delete jitrt_flags; + delete jitrt_flag_list; + AllocateAndParseFlags(); +} + +} // namespace + +bool SetXlaAutoJitFlagFromFlagString(const std::string& value) { + absl::call_once(flags_init, &AllocateAndParseFlags); + return SetterForXlaAutoJitFlag(value); +} + +BuildXlaOpsPassFlags* GetBuildXlaOpsPassFlags() { + absl::call_once(flags_init, &AllocateAndParseFlags); + return build_ops_flags; +} + +MarkForCompilationPassFlags* GetMarkForCompilationPassFlags() { + absl::call_once(flags_init, &AllocateAndParseFlags); + return mark_for_compilation_flags; +} + +XlaSparseCoreFlags* GetXlaSparseCoreFlags() { + absl::call_once(flags_init, &AllocateAndParseFlags); + return sparse_core_flags; +} + +XlaDeviceFlags* GetXlaDeviceFlags() { + absl::call_once(flags_init, &AllocateAndParseFlags); + return device_flags; +} + +XlaOpsCommonFlags* GetXlaOpsCommonFlags() { + absl::call_once(flags_init, &AllocateAndParseFlags); + return ops_flags; +} + +XlaCallModuleFlags* GetXlaCallModuleFlags() { + absl::call_once(flags_init, &AllocateAndParseFlags); + return call_module_flags; +} + +MlirCommonFlags* GetMlirCommonFlags() { + absl::call_once(flags_init, &AllocateAndParseFlags); + return mlir_flags; +} + +void ResetJitCompilerFlags() { ResetFlags(); } + +const JitRtFlags& GetJitRtFlags() { + absl::call_once(flags_init, &AllocateAndParseFlags); + return *jitrt_flags; +} + +ConfigProto::Experimental::MlirBridgeRollout GetMlirBridgeRolloutState( + std::optional config_proto) { + // TF1 graphs that do not override Sessions's ConfigProto and TF2 graphs + // can enable/disable the graph via tf_mlir_enable_mlir_bridge. + auto tf_mlir_enable_mlir_bridge = + GetMlirCommonFlags()->tf_mlir_enable_mlir_bridge; + if (tf_mlir_enable_mlir_bridge != + ConfigProto::Experimental::MLIR_BRIDGE_ROLLOUT_UNSPECIFIED) { + return tf_mlir_enable_mlir_bridge; + } + + // If a ConfigProto was not passed in, we can assume the caller is + // checking if TF2 graph should have the bridge enabled / disabled. In that + // case, we have already checked tf_mlir_enable_mlir_bridge so it is safe to + // return UNSPECIFIED here. + if (!config_proto.has_value()) { + return ConfigProto::Experimental::MLIR_BRIDGE_ROLLOUT_UNSPECIFIED; + } + + // TF1 graphs that do override Session's ConfigProto and set + // ConfigProto's enable_mlir_bridge or mlir_bridge_rollout fields will not + // update tf_mlir_enable_mlir_bridge so check their values. + + // ConfigProto's enable_mlir_bridge defaults to false so only respect it + // when it is true. + if (config_proto.value().experimental().enable_mlir_bridge()) { + return ConfigProto::Experimental::MLIR_BRIDGE_ROLLOUT_ENABLED; + } + return config_proto.value().experimental().mlir_bridge_rollout(); +} + +void AppendMarkForCompilationPassFlags(std::vector* flag_list) { + absl::call_once(flags_init, &AllocateAndParseFlags); + AppendMarkForCompilationPassFlagsInternal(flag_list); +} + +static std::atomic xla_compilation_disabled(false); + +void DisableXlaCompilation() { xla_compilation_disabled = true; } + +void EnableXlaCompilation() { xla_compilation_disabled = false; } + +bool FailOnXlaCompilation() { return xla_compilation_disabled; } + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/flags.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/flags.h new file mode 100644 index 0000000000000000000000000000000000000000..4e815c649dc876418d8b303de99fb007769e34ff --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/flags.h @@ -0,0 +1,366 @@ +/* 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_FLAGS_H_ +#define TENSORFLOW_COMPILER_JIT_FLAGS_H_ + +#include +#include +#include +#include + +#include "absl/container/flat_hash_set.h" +#include "absl/types/optional.h" +#include "tensorflow/core/framework/types.h" +#include "tensorflow/core/platform/types.h" +#include "tensorflow/core/protobuf/config.pb.h" +#include "tensorflow/core/util/command_line_flags.h" + +namespace tensorflow { + +struct XlaAutoJitFlag { + // Control compilation of operators into XLA computations on CPU and GPU + // devices. 0 = use ConfigProto setting; -1 = off; 1 = on for things very + // likely to be improved; 2 = on for everything. + // + // If all non-CPU ops in the graph being optimized are placed on a single GPU + // and there is at least one node placed on that GPU then + // `optimization_level_single_gpu` applies. Otherwise + // `optimization_level_general` applies. + // + // Experimental. + int32_t optimization_level_single_gpu; + int32_t optimization_level_general; +}; + +// Sets the xla_auto_jit_flag based on the given flag string. Supported syntax +// is: +// : sets general and single_gpu setting to the provided number. +// single-gpu(): sets the single_gpu setting to the provided number. +bool SetXlaAutoJitFlagFromFlagString(const std::string& value); + +// Flags associated with the XLA bridge's mark_for_compilation_pass module. +struct MarkForCompilationPassFlags { + XlaAutoJitFlag xla_auto_jit_flag; + + // Minimum number of operators in an XLA compilation. Ignored for operators + // placed on an XLA device or operators explicitly marked for compilation. + int32_t tf_xla_min_cluster_size; + + // Maximum number of operators in an XLA compilation. + int32_t tf_xla_max_cluster_size; + + // If non-empty, limit XLA clustering to the following TF operations. + std::string tf_xla_ops_to_cluster; + + // If non-empty, remove following operations from XLA clustering excludelist. + std::string tf_xla_cluster_exclude_ops; + + // Dump graphs during XLA compilation. + bool tf_xla_clustering_debug; + + // Enables global JIT compilation for CPU via SessionOptions. + bool tf_xla_cpu_global_jit; + + // "Compiler fuel" for clustering. Only this many ops will be marked as + // eligible for clustering. + int64_t tf_xla_clustering_fuel; + + // If tf_xla_disable_deadness_safety_checks_for_debugging is set to true then + // we do not do deadness related safety checks. This is unsound in general, + // but can be used as a debugging aid. + bool tf_xla_disable_deadness_safety_checks_for_debugging; + + // If tf_xla_disable_resource_variable_safety_checks_for_debugging is set to + // true then we do not do safety checks to preserve TensorFlow's resource + // variable concurrency semantics. This is unsound in general, but can be + // used as a debugging aid. + bool tf_xla_disable_resource_variable_safety_checks_for_debugging; + + // If true names of clustered operations will be computed deterministically + // so that they remain stable from run to run of auto clusteing. + bool tf_xla_deterministic_cluster_names; + + // If non-empty, JIT-compiled executables are saved to and loaded from the + // specified file system directory path. + std::string tf_xla_persistent_cache_directory; + + // If non-empty, the persistent cache will only be used for the specified + // devices (comma separated). Each device type should be able to be converted + // to `DeviceType`. + std::string tf_xla_persistent_cache_device_types; + + bool tf_xla_persistent_cache_read_only; + + // If true, entries loaded into the XLA compile cache will not have their + // signatures checked strictly. This should generally not be disabled except + // for debugging. Defaults to false. + bool tf_xla_disable_strict_signature_checks; + + // Specifies the persistance cache prefix. Default is "xla_compile_cache" + std::string tf_xla_persistent_cache_prefix; +}; + +// Flags associated with XLA Sparse Core. +struct XlaSparseCoreFlags { + // Max level of division to split input data into minibatches. + int tf_xla_sparse_core_minibatch_max_division_level; + + // Disable table stacking for all the tables passed to the SparseCore + // mid level API. + bool tf_xla_sparse_core_disable_table_stacking; + + // If non-zero, limits the size of the activations for a given table to + // be below these many bytes. + int64_t tf_xla_sparse_core_stacking_mem_limit_bytes; + + // If non-zero, limits the size of any table shard to be below these + // many bytes. + int64_t tf_xla_sparse_core_stacking_table_shard_limit_bytes; +}; + +// Flags associated with the XLA bridge's xla_device module. +struct XlaDeviceFlags { + // Switch the CPU device into "on-demand" mode, where instead of + // auto-clustering ops are compiled one by one just-in-time. + // Enabling this mode by a legacy flag is a temporary mechanism. When this + // feature is battle-tested, we will switch this to be a session option. + bool tf_xla_compile_on_demand; + + // Enables "XLA" devices if this flag is set. + bool tf_xla_enable_xla_devices; +}; + +// Flags common to the _Xla* ops and their kernels. +struct XlaOpsCommonFlags { + // If true, _XlaCompile always refuses to compile the cluster, which means the + // XLA clusters always run in the TF executor. Defaults to false. + bool tf_xla_always_defer_compilation; + // If true, _XlaCompile compiles the cluster asynchronously with respect to + // the main execution. The fallback path is taken while compilation happens. + bool tf_xla_async_compilation; + + class PjRtForSingleDeviceCompilationRollout { + public: + // Allow using Device API (PjRt) for `device_type` in the XlaLaunch op. + // Please note that `enabled_for_xla_launch_` needs to be true in addition + // to the `device_type` being allowed in order to use the Device API for + // single device compilation and execution in the XlaLaunch op. + void AllowForDeviceInXlaLaunch(const DeviceType& device_type) { + xla_launch_allowed_devices_.insert(device_type.type_string()); + } + + bool IsEnabledInXlaLaunchForDevice(const DeviceType& device_type) const { + if (!enabled_for_gpu_ && device_type.type_string() == "GPU") return false; + return enabled_for_all_ || + (enabled_for_xla_launch_ && + xla_launch_allowed_devices_.contains(device_type.type_string())); + } + + // Allow using Device API (PjRt) for `device_type` in the XlaCompileOnDemand + // op. Please note that `enabled_for_compile_on_demand_` needs to be true in + // addition to the `device_type` being allowed in order to use the Device + // API for single device compilation and execution in the XlaCompileOnDemand + // op. + void AllowForDeviceInXlaCompileOnDemand(const DeviceType& device_type) { + xla_compile_on_demand_allowed_devices_.insert(device_type.type_string()); + } + + bool IsEnabledInXlaCompileOnDemandForDevice( + const DeviceType& device_type) const { + if (!enabled_for_gpu_ && device_type.type_string() == "GPU") return false; + return enabled_for_all_ || + (enabled_for_compile_on_demand_ && + xla_compile_on_demand_allowed_devices_.contains( + device_type.type_string())); + } + + // Allow using Device API (PjRt) for `device_type` in the XlaCompile and + // XlaRun ops. Please note that `enabled_for_compile_and_run_` needs to be + // true in addition to the `device_type` being allowed in order to use the + // Device API for single device compilation and execution in the XlaCompile + // and XlaRun ops. + void AllowForDeviceInXlaCompileAndRun(const DeviceType& device_type) { + xla_compile_and_run_allowed_devices_.insert(device_type.type_string()); + } + + bool IsEnabledInXlaCompileAndRunForDevice( + const DeviceType& device_type) const { + if (!enabled_for_gpu_ && device_type.type_string() == "GPU") return false; + return enabled_for_all_ || (enabled_for_compile_and_run_ && + xla_compile_and_run_allowed_devices_.contains( + device_type.type_string())); + } + + bool IsEnabledForGpu() const { return enabled_for_gpu_; } + + // If true, uses Device API (PjRt) for single device compilation and + // execution of functions marked for JIT compilation i.e. jit_compile=True. + // Defaults to false. + bool enabled_for_xla_launch_; + + // If true, uses Device API (PjRt) for compiling and executing ops one by + // one in "on-demand" mode. Defaults to false. + bool enabled_for_compile_on_demand_; + + // If true, uses Device API (PjRt) for compilation and execution when + // auto-clustering is enabled. Defaults to false. + bool enabled_for_compile_and_run_; + + // If true, uses Device API (PjRt) for compilation and execution everywhere + // i.e. for functions marked for JIT compilation, for ops in "on-demand" + // mode and auto-clustering. Defaults to false. + // + // Note that this flag can be overridden by device flag like + // `enabled_for_gpu_` below. + bool enabled_for_all_; + + // If true, enable Device API (PjRt) for TF GPU device. This is a helper + // flag so that individual tests can turn on PjRt for GPU specifically. + // Once the rollout to GPU is complete, this flag can be deprecated. + bool enabled_for_gpu_; + + private: + // Devices for which using Device API (PjRt) is allowed in the XlaLaunch op. + // This can only be modified programmatically. + absl::flat_hash_set xla_launch_allowed_devices_; + // Devices for which using Device API (PjRt) is allowed in the + // XlaCompileOnDemand op. This can only be modified programmatically. + absl::flat_hash_set xla_compile_on_demand_allowed_devices_; + // Devices for which using Device API (PjRt) is allowed in the + // XlaCompile and XlaRun ops. This can only be modified programmatically. + absl::flat_hash_set xla_compile_and_run_allowed_devices_; + } tf_xla_use_device_api; +}; + +// Flags for the XlaCallModule kernel. +struct XlaCallModuleFlags { + // Used by XlaCallModuleOp to specify safety checks to disable. + absl::flat_hash_set disabled_checks; +}; + +// Flags for the build_xla_ops pass. +struct BuildXlaOpsPassFlags { + // Enables lazy compilation for TF/XLA (only when auto-clustering) if true. + // Defaults to true. + bool tf_xla_enable_lazy_compilation; + + // If true then insert Print nodes to print out values produced by XLA + // clusters. Useful for debugging. + bool tf_xla_print_cluster_outputs; + + // If true, insert CheckNumerics nodes for every floating point typed input to + // an XLA cluster. + bool tf_xla_check_cluster_input_numerics; + + // If true, insert CheckNumerics nodes for every floating point typed output + // from an XLA cluster. + bool tf_xla_check_cluster_output_numerics; + + // Disables all constant folding. The primary use for this is for testing to + // guarantee that tests are run on XLA and not on TF's CPU implementation. + bool tf_xla_disable_constant_folding; + + // Disables full embedding pipelining when true. Instead, strict SparseCore + // TensorCore sequencing will be used. + bool tf_xla_disable_full_embedding_pipelining; + + // Whether to enable automatical embedding pipelining when summary ops are + // detected in the graph. + bool tf_xla_disable_full_embedding_pipelining_with_summaries; + + // Force the WhileOps in embedding_pipelining and embedding_sequencing to use + // this many parallel_iterations + int tf_xla_embedding_parallel_iterations; +}; + +// Flags for common MLIR configurations. +struct MlirCommonFlags { + ConfigProto::Experimental::MlirBridgeRollout tf_mlir_enable_mlir_bridge; + + bool tf_mlir_enable_merge_control_flow_pass; + bool tf_mlir_enable_convert_control_to_data_outputs_pass; + bool tf_mlir_enable_composite_tpuexecute_side_effects; + bool tf_mlir_enable_strict_clusters; + bool tf_mlir_enable_tpu_variable_runtime_reformatting_pass; + // TODO(pineapplejuice233): Revisit this flag once the performance impact is verified + // with different local CPU devices settings. + bool tf_mlir_enable_multiple_local_cpu_devices; + bool tf_mlir_enable_debug_info_serialization; + bool tf_serialize_mlir_to_compressed_bytecode; +}; + +// Flags for the JitRt pipeline -- see tf_jitrt_pipeline.h for details. +struct JitRtFlags { + bool always_specialize; + bool cost_driven_async_parallel_for; + + // Enables tracking of the "live" JitRt queries to, on a crash, identify the + // "query of death". See TfJitRtQueryOfDeathLogger. + bool log_query_of_death; + + // Enable vectorization, which requires tiling and peeling on different ops. + bool vectorize; + + // Enables crash reproducer for JitRt MLIR pass manager. + bool enable_crash_reproducer; +}; + +// Return a pointer to the DumpGraphFlags struct; +// repeated calls return the same pointer. +// This should be called only after Flags::Parse() has returned. + +// Getters for flags structs defined above. The first call to any of these +// parses TF_XLA_FLAGS for all of them. Those functions which return a pointer +// always return the same pointer. +MarkForCompilationPassFlags* GetMarkForCompilationPassFlags(); +BuildXlaOpsPassFlags* GetBuildXlaOpsPassFlags(); +XlaSparseCoreFlags* GetXlaSparseCoreFlags(); +XlaDeviceFlags* GetXlaDeviceFlags(); +XlaOpsCommonFlags* GetXlaOpsCommonFlags(); +XlaCallModuleFlags* GetXlaCallModuleFlags(); + +MlirCommonFlags* GetMlirCommonFlags(); + +void ResetJitCompilerFlags(); + +const JitRtFlags& GetJitRtFlags(); + +// Returns the effective MLIR bridge rollout state based on the flags and the +// optional configuration. +ConfigProto::Experimental::MlirBridgeRollout GetMlirBridgeRolloutState( + std::optional config_proto); + +// Appends the flag definitions associated with +// MarkForCompilationPassFlags/DumpGraphFlags to `flag_list`. +// +// Has the side-effect of parsing TF_XLA_FLAGS if that hasn't happened yet. +void AppendMarkForCompilationPassFlags( + std::vector* flag_list); + +// Disables XLA compilation, forces it to return an error message instead. Can +// be used by a server to ensure that JIT compilation is opt-in. +void DisableXlaCompilation(); + +// Enables XLA compilation. Can be used with `DisableXlaCompilation` to +// enable/disable JIT compilation at different stages. +void EnableXlaCompilation(); + +// Returns `false` unless `DisableXlaCompilation` was called. +bool FailOnXlaCompilation(); + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_FLAGS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/force_xla_constants_on_host_pass.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/force_xla_constants_on_host_pass.cc new file mode 100644 index 0000000000000000000000000000000000000000..21015d621c88c0570e1adea893865b60de2dba2e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/force_xla_constants_on_host_pass.cc @@ -0,0 +1,56 @@ +/* Copyright 2020 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/compiler/jit/force_xla_constants_on_host_pass.h" + +#include "tensorflow/compiler/jit/compilability_check_util.h" +#include "tensorflow/compiler/jit/defs.h" +#include "tensorflow/core/common_runtime/optimization_registry.h" + +namespace tensorflow { + +absl::Status ForceXlaConstantsOnHostPass::Run( + const GraphOptimizationPassOptions& options) { + Graph* graph = options.graph->get(); + + OptimizerOptions opts; + auto pflr = std::make_unique( + nullptr, options.session_options->env, /*config=*/nullptr, + TF_GRAPH_DEF_VERSION, options.flib_def, opts); + FunctionLibraryRuntime* flr = + pflr->GetFLR(ProcessFunctionLibraryRuntime::kDefaultFLRDevice); + + for (Node* node : graph->nodes()) { + if (CanCreateXlaKernel(node->def())) { + const FunctionBody* fbody = nullptr; + std::vector constant_arg_indices; + std::vector resource_arg_indices; + + NameAttrList function; + TF_RETURN_IF_ERROR(NameAndAttrsFromFunctionCall(node->def(), &function)); + + // Force all constants to be on the host memory. + TF_RETURN_IF_ERROR(GetBodyAndConstantsAndResources( + flr, function, &fbody, &constant_arg_indices, &resource_arg_indices)); + VLOG(3) << "Found constant arg indices: " + << absl::StrJoin(constant_arg_indices, ", "); + + node->AddAttr("_input_hostmem", constant_arg_indices); + } + } + return absl::OkStatus(); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/force_xla_constants_on_host_pass.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/force_xla_constants_on_host_pass.h new file mode 100644 index 0000000000000000000000000000000000000000..2570263dc00754339718590c106ab6353a4ba5c6 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/force_xla_constants_on_host_pass.h @@ -0,0 +1,36 @@ +/* Copyright 2020 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_FORCE_XLA_CONSTANTS_ON_HOST_PASS_H_ +#define TENSORFLOW_COMPILER_JIT_FORCE_XLA_CONSTANTS_ON_HOST_PASS_H_ + +#include "absl/container/flat_hash_set.h" +#include "tensorflow/compiler/jit/compilability_check_util.h" +#include "tensorflow/core/common_runtime/optimization_registry.h" + +namespace tensorflow { + +// An optimization pass which marks the constants which have to be resolved for +// XLA compilation with `_input_hostmem`. +class ForceXlaConstantsOnHostPass : public GraphOptimizationPass { + public: + ForceXlaConstantsOnHostPass() = default; + + absl::Status Run(const GraphOptimizationPassOptions& options) override; +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_FORCE_XLA_CONSTANTS_ON_HOST_PASS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/force_xla_constants_on_host_pass_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/force_xla_constants_on_host_pass_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..213c43dd7729165e696b0d0003ea96cc8e4132e6 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/force_xla_constants_on_host_pass_test.cc @@ -0,0 +1,108 @@ +/* Copyright 2020 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/compiler/jit/force_xla_constants_on_host_pass.h" + +#include "absl/strings/match.h" +#include "absl/strings/str_cat.h" +#include "tensorflow/cc/framework/ops.h" +#include "tensorflow/cc/ops/functional_ops.h" +#include "tensorflow/cc/ops/standard_ops.h" +#include "tensorflow/compiler/jit/compilability_check_util.h" +#include "tensorflow/compiler/jit/defs.h" +#include "tensorflow/compiler/jit/test_util.h" +#include "tensorflow/core/common_runtime/function.h" +#include "tensorflow/core/common_runtime/graph_constructor.h" +#include "tensorflow/core/framework/function_testlib.h" +#include "tensorflow/core/framework/node_def_util.h" +#include "tensorflow/core/graph/graph_def_builder.h" +#include "tensorflow/core/lib/core/errors.h" +#include "tensorflow/core/lib/core/status_test_util.h" +#include "tensorflow/core/platform/test.h" +#include "tensorflow/core/public/session_options.h" +#include "tensorflow/core/public/version.h" + +namespace tensorflow { +namespace { + +absl::Status ForceXlaConstantsOnHost(const Scope& s, + FunctionLibraryDefinition* flib_def, + std::unique_ptr* result) { + auto graph = std::make_unique(OpRegistry::Global()); + GraphOptimizationPassOptions options; + SessionOptions session_options; + session_options.env = Env::Default(); + options.graph = &graph; + options.session_options = &session_options; + options.flib_def = flib_def; + TF_RETURN_IF_ERROR(s.ToGraph(graph.get())); + ForceXlaConstantsOnHostPass rewriter; + TF_RETURN_IF_ERROR(rewriter.Run(options)); + *result = std::move(graph); + return absl::OkStatus(); +} + +TEST(ForceXlaConstantsOnHostPassTest, Simple) { + GraphDefBuilder b(GraphDefBuilder::kFailImmediately); + Scope root = Scope::NewRootScope().ExitOnError(); + FunctionDefLibrary library; + + FunctionDef called_func = + FunctionDefHelper::Create("TransposeCall", + /*in_def=*/{"a:float", "b:int32"}, + /*out_def=*/{"c:float"}, {}, + {{{"t0"}, + "Transpose", + {"a", "b"}, + { + {"T", DT_FLOAT}, + {"Tperm", DT_INT32}, + }}}, + {{"c", "t0:y:0"}}); + + AttrValue true_attribute; + true_attribute.set_b(true); + (*called_func.mutable_attr())[kXlaMustCompileAttr] = true_attribute; + *library.add_function() = called_func; + TF_ASSERT_OK(root.graph()->AddFunctionLibrary(library)); + FunctionLibraryDefinition flib_def(OpRegistry::Global(), library); + Output in = ops::Placeholder(root, DT_FLOAT); + Output perm = ops::Const(root, {3, 1, 2, 0}); + + NameAttrList b_name_attr; + b_name_attr.set_name("TransposeCall"); + ops::PartitionedCall call(root.WithOpName("call"), {in, perm}, {DT_FLOAT}, + b_name_attr); + call.output.front().node()->AddAttr(kXlaMustCompileAttr, true); + + std::unique_ptr graph; + TF_ASSERT_OK(ForceXlaConstantsOnHost(root, &flib_def, &graph)); + + bool found = false; + for (Node* node : graph->nodes()) { + if (CanCreateXlaKernel(node->def())) { + EXPECT_FALSE(found); + found = true; + std::vector hostmem_attr; + EXPECT_TRUE(TryGetNodeAttr(node->def(), "_input_hostmem", &hostmem_attr)); + EXPECT_EQ(hostmem_attr.size(), 1); + EXPECT_EQ(hostmem_attr[0], 1); + } + } + EXPECT_TRUE(found); +} + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/get_compiler_ir.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/get_compiler_ir.cc new file mode 100644 index 0000000000000000000000000000000000000000..062d82005ab5fbefa0edebaa48b5247d50ec602d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/get_compiler_ir.cc @@ -0,0 +1,482 @@ +/* Copyright 2020 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/compiler/jit/get_compiler_ir.h" + +#include +#include +#include +#include +#include +#include + +#include "absl/algorithm/container.h" +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/ascii.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" +#include "absl/types/span.h" +#include "tensorflow/compiler/jit/compilability_check_util.h" +#include "tensorflow/compiler/jit/device_compiler.h" +#include "tensorflow/compiler/jit/variable_info.h" +#include "tensorflow/compiler/jit/variable_info_util.h" +#include "tensorflow/compiler/jit/xla_compiler_options_util.h" +#include "tensorflow/compiler/jit/xla_launch_util.h" +#include "tensorflow/compiler/jit/xla_platform_info.h" +#include "tensorflow/compiler/tf2xla/xla_compiler.h" +#include "xla/client/executable_build_options.h" +#include "xla/client/local_client.h" +#include "xla/hlo/translate/portable_api.h" +#include "xla/service/hlo_graph_dumper.h" +#include "xla/status_macros.h" +#include "xla/stream_executor/host/host_platform_id.h" +#include "xla/stream_executor/platform.h" +#include "tensorflow/core/common_runtime/eager/tensor_handle.h" +#include "tensorflow/core/framework/device_base.h" +#include "tensorflow/core/framework/function.h" +#include "tensorflow/core/framework/op_def.pb.h" +#include "tensorflow/core/framework/resource_handle.pb.h" +#include "tensorflow/core/framework/tensor.h" +#include "tensorflow/core/framework/tensor_shape.h" +#include "tensorflow/core/framework/types.h" +#include "tensorflow/core/framework/types.pb.h" +#include "tensorflow/core/platform/refcount.h" +#include "tensorflow/core/platform/statusor.h" +#include "tsl/platform/errors.h" +#include "tsl/platform/statusor.h" + +namespace tensorflow { + +static absl::StatusOr> BuildExecutable( + xla::LocalClient* local_client, + const XlaCompiler::CompilationResult& result, + const XlaCompiler::Options& options, + const bool xla_embed_ir_in_executable = false) { + std::vector argument_layouts( + result.xla_input_shapes.size()); + for (int i = 0, end = result.xla_input_shapes.size(); i < end; ++i) { + argument_layouts[i] = &result.xla_input_shapes[i]; + } + xla::ExecutableBuildOptions build_options; + if (result.collective_info) { + build_options.set_num_replicas(result.collective_info->group_size); + } + build_options.set_device_ordinal( + options.device_ordinal != -1 ? options.device_ordinal + : local_client->default_device_ordinal()); + build_options.set_result_layout(result.xla_output_shape); + build_options.set_device_allocator(options.device_allocator.get()); + build_options.set_alias_passthrough_params(options.alias_passthrough_params); + build_options.mutable_debug_options()->set_xla_detailed_logging( + options.detailed_logging); + // If the embed_ir_in_executable is set, hlo_proto will be dumped in + // executable. The hlo_proto contains HLO modules and buffer assignment. + build_options.mutable_debug_options()->set_xla_embed_ir_in_executable( + xla_embed_ir_in_executable); + TF_ASSIGN_OR_RETURN( + std::vector> executables, + local_client->Compile(*result.computation, argument_layouts, + build_options)); + TF_RET_CHECK(executables.size() == 1); + return std::move(executables[0]); +} + +static absl::StatusOr BuildHLOString( + IrExportStage stage, const XlaCompiler::CompilationResult& result, + xla::LocalClient* local_client, const XlaCompiler::Options& options) { + switch (stage) { + case IrExportStage::STABLEHLO: + case IrExportStage::STABLEHLO_SERIALIZED: + case IrExportStage::HLO: + case IrExportStage::HLO_NO_METADATA: + case IrExportStage::HLO_SERIALIZED: { + TF_ASSIGN_OR_RETURN(xla::ProgramShape program_shape, + result.computation->GetProgramShape()); + xla::HloModuleConfig config(program_shape); + TF_ASSIGN_OR_RETURN( + std::unique_ptr new_module, + xla::HloModule::CreateFromProto(result.computation->proto(), config)); + + if (stage == IrExportStage::STABLEHLO_SERIALIZED) { + TF_ASSIGN_OR_RETURN( + std::string stablehlo, + xla::ConvertHloToStablehlo(*new_module, /*emit_bytecode=*/true)); + return stablehlo; + } + if (stage == IrExportStage::STABLEHLO) { + TF_ASSIGN_OR_RETURN( + std::string stablehlo, + xla::ConvertHloToStablehlo(*new_module, /*emit_bytecode=*/false)); + return stablehlo; + } + + xla::HloPrintOptions opts; + opts.set_print_large_constants(false); + opts.set_print_operand_shape(true); + if (stage == IrExportStage::HLO_NO_METADATA) { + opts.set_print_metadata(false); + } + if (stage == IrExportStage::HLO_SERIALIZED) { + return new_module->ToProto().SerializeAsString(); + } + return new_module->ToString(opts); + } + case IrExportStage::OPTIMIZED_HLO: + case IrExportStage::OPTIMIZED_HLO_SERIALIZED: { + TF_ASSIGN_OR_RETURN(std::unique_ptr executable, + BuildExecutable(local_client, result, options)); + xla::Executable* new_executable = executable->executable(); + if (stage == IrExportStage::OPTIMIZED_HLO_SERIALIZED) { + return new_executable->module().ToProto().SerializeAsString(); + } else { + return new_executable->module().ToString(); + } + } + case IrExportStage::OPTIMIZED_HLO_PROTO_SERIALIZED: { + TF_ASSIGN_OR_RETURN(std::unique_ptr executable, + BuildExecutable(local_client, result, options, + /*xla_embed_ir_in_executable=*/true)); + return executable->executable()->hlo_proto()->SerializeAsString(); + } + case IrExportStage::OPTIMIZED_HLO_DOT: { + TF_ASSIGN_OR_RETURN(std::unique_ptr executable, + BuildExecutable(local_client, result, options)); + absl::StatusOr graph = xla::RenderGraph( + *executable->executable()->module().entry_computation(), + "Visualization", + /*debug_options=*/{}, xla::RenderedGraphFormat::kDot, + /*hlo_render_options=*/{}); + TF_RETURN_IF_ERROR(graph.status()); + return *graph; + } + } +} + +static absl::StatusOr> +BuildXlaCompilerArgumentFromTensorSpec( + const FunctionBody* fbody, absl::Span must_be_constant_idxs, + absl::Span inputs, + absl::Span variable_args, + absl::Span flat_arg_shape_and_dtype) { + TF_RET_CHECK(fbody != nullptr); + auto& input_args = fbody->record->fdef().signature().input_arg(); + int input_arg_size = input_args.size(); + std::vector args; + args.reserve(input_arg_size); + + for (auto& arg_info : flat_arg_shape_and_dtype) { + XlaCompiler::Argument arg; + arg.kind = XlaCompiler::Argument::kParameter; + arg.type = arg_info.dtype; + arg.shape = arg_info.shape; + args.push_back(arg); + } + + // Build Xla Compiler Arguments from concrete_fn.captured_inputs + absl::flat_hash_map variable_info_lookup; + TF_RETURN_IF_ERROR( + CreateVariableInfoLookup(variable_args, variable_info_lookup)); + + for (const VariableInfo& info : variable_args) { + TF_RET_CHECK(!info.var() || info.lock_held() || info.shared_lock_held()) + << "Need to hold the lock on resource variables " + "before calling BuildXlaCompilerArguments"; + variable_info_lookup.emplace(info.index(), &info); + } + + int offset = flat_arg_shape_and_dtype.size(); + // Here it takes in the concrete_fn.captured_inputs and builds the appropriate + // XLA compiler arguments. + for (int64_t input_num = offset; input_num < input_arg_size; ++input_num) { + const Tensor* input = inputs[input_num]; + + XlaCompiler::Argument arg; + if (variable_info_lookup.count(input_num)) { + // Handles tf.resource variables. + TF_RET_CHECK(input->dtype() == DT_RESOURCE); + const VariableInfo& variable = *variable_info_lookup[input_num]; + arg.kind = XlaCompiler::Argument::kResource; + arg.resource_kind = XlaResource::kVariable; + arg.definition_stack_trace = variable.definition_stack_trace(); + TF_RET_CHECK(variable.var() && variable.var()->is_initialized); + const Tensor* value = variable.var()->tensor(); + arg.type = value->dtype(); + arg.shape = value->shape(); + arg.initialized = true; + } else { + // Instead of embedding constant into HLO, + // we handle tf.constant as parameter to reduce size. + arg.kind = XlaCompiler::Argument::kParameter; + arg.type = input->dtype(); + arg.shape = input->shape(); + } + args.push_back(arg); + } + + for (int64_t i = 0; i < input_arg_size; ++i) { + args[i].name = input_args[i].name(); + } + + return args; +} + +absl::Status ValidateGetCompilerIrTfrtTpu(absl::string_view device_type, + stream_executor::Stream* stream, + IrExportStage stage) { + auto is_tfrt_tpu_supported_stage = [](IrExportStage stage) { + return stage == IrExportStage::HLO || + stage == IrExportStage::HLO_NO_METADATA || + stage == IrExportStage::HLO_SERIALIZED || + stage == IrExportStage::STABLEHLO || + stage == IrExportStage::STABLEHLO_SERIALIZED; + }; + // TODO(b/238830423): support GetCompilerIr on TFRT TPU device for stages + // that requires compilation from HLO to executable. + if (device_type != DEVICE_CPU && stream == nullptr && + !is_tfrt_tpu_supported_stage(stage)) { + return absl::InternalError( + "GetCompilerIr with requested stage is not supported on this device."); + } + return absl::OkStatus(); +} + +absl::StatusOr> PrepareXlaCompilerArgs( + FunctionLibraryRuntime* flr, const NameAttrList& function, + EagerContext* context, Device* dev, + absl::Span input_arg_shape_and_dtype, + absl::Span input_handles, + CompilerArgSource compiler_arg_source) { + const FunctionBody* fbody = nullptr; + std::vector constant_arg_indices; + std::vector resource_arg_indices; + TF_RETURN_IF_ERROR(GetBodyAndConstantsAndResources( + flr, function, &fbody, &constant_arg_indices, &resource_arg_indices)); + if (dev == nullptr && !resource_arg_indices.empty()) { + return absl::InternalError( + "GetCompilerIr does not support resource arguments when device is " + "null."); + } + + // `input_args` includes both concrete_fn input args and captured_input here. + auto& input_args = fbody->record->fdef().signature().input_arg(); + // Here input_arg_size = len(flat_args) + len(captured_input) + int input_arg_size = input_args.size(); + + std::vector inputs(input_arg_size); + std::deque inputs_storage; + std::vector variable_infos; + int offset = input_arg_shape_and_dtype.size(); + + for (int i = 0; i < input_handles.size(); i++) { + const TensorHandle* th = input_handles[i]; + const Tensor* t; + // Handle owns the tensor. + TF_RETURN_IF_ERROR(th->Tensor(&t)); + if (absl::c_binary_search(constant_arg_indices, i)) { + // Need to make sure it's on the host. + inputs_storage.emplace_back(t->dtype(), t->shape()); + TF_RETURN_IF_ERROR( + th->CopyToDevice(*context, /*d=*/nullptr, &inputs_storage.back())); + inputs[i + offset] = &inputs_storage.back(); + } else { + inputs[i + offset] = t; + } + } + + if (dev != nullptr) { + TF_RETURN_IF_ERROR(GetVariableInfosFromInputs(dev->resource_manager(), dev, + inputs, resource_arg_indices, + &variable_infos)); + TF_RETURN_IF_ERROR(LockVariables(absl::MakeSpan(variable_infos))); + } + + absl::StatusOr> args; + + if (compiler_arg_source == CompilerArgSource::TENSOR_SPEC) { + args = BuildXlaCompilerArgumentFromTensorSpec(fbody, constant_arg_indices, + inputs, variable_infos, + input_arg_shape_and_dtype); + } else if (compiler_arg_source == CompilerArgSource::CONCRETE_INPUT) { + args = XlaComputationLaunchContext::BuildXlaCompilerArguments( + constant_arg_indices, inputs, variable_infos, dev); + } + return args; +} + +absl::StatusOr CompileAndBuildHLOString( + IrExportStage stage, const XlaCompiler::Options& options, + xla::LocalClient* local_client, const NameAttrList& function, + const std::vector& args) { + XlaCompiler::CompileOptions compile_options; + compile_options.always_return_tuple = false; + compile_options.alias_resource_update = true; + + XlaCompiler compiler(options); + XlaCompiler::CompilationResult result; + TF_RETURN_IF_ERROR( + compiler.CompileFunction(compile_options, function, args, &result)); + + return BuildHLOString(stage, result, local_client, options); +} + +/** + * Clarifies the different meanings of 'input_arg_shape_and_dtype' and + * 'input_handles' in different cases. + * + * For TENSOR_SPEC case: + * - `input_arg_shape_and_dtype`: Contains the shape and dtype of + * concrete_fn input args. + * - `input_handles`: Contains the concrete_fn.captured_input tensors. + * + * For CONCRETE_INPUT case: + * - `input_arg_shape_and_dtype`: it is empty. + * - `input_handles`: Contains all concrete_fn inputs tensors, including + * captured inputs. + */ +absl::StatusOr GetCompilerIr( + IrExportStage stage, ProcessFunctionLibraryRuntime* pflr, + absl::string_view func_name, Device* dev, EagerContext* context, + absl::Span input_arg_shape_and_dtype, + absl::Span input_handles, + CompilerArgSource compiler_arg_source) { + using XlaDeviceCompiler = + DeviceCompiler; + + se::Stream* stream = nullptr; + if (const DeviceBase::AcceleratorDeviceInfo* accelerator_device_info = + dev->tensorflow_accelerator_device_info()) { + stream = accelerator_device_info->stream; + } + TF_RETURN_IF_ERROR( + ValidateGetCompilerIrTfrtTpu(dev->device_type(), stream, stage)); + + NameAttrList function; + function.set_name(std::string{func_name}); + + FunctionLibraryRuntime* flr = pflr->GetFLR(dev->name()); + + TF_ASSIGN_OR_RETURN( + std::vector args, + PrepareXlaCompilerArgs(flr, function, context, dev, + input_arg_shape_and_dtype, input_handles, + compiler_arg_source)); + + XlaPlatformInfo platform_info = XlaPlatformInfoFromDevice(dev); + auto compilation_device_type = platform_info.device_type(); + if (platform_info.device_type() != DEVICE_TPU) { + TF_ASSIGN_OR_RETURN(compilation_device_type, + GetCompilationDeviceType(platform_info.device_type())); + } + + XlaDeviceCompiler* xla_device_compiler; + TF_RETURN_IF_ERROR(dev->resource_manager()->LookupOrCreate( + dev->resource_manager()->default_container(), "xla_device_compiler", + &xla_device_compiler, [&](XlaDeviceCompiler** xla_device_compiler) { + return BuildXlaDeviceCompiler(dev, flr, platform_info, + compilation_device_type, + xla_device_compiler); + })); + core::ScopedUnref xla_device_compiler_ref(xla_device_compiler); + + XlaCompiler::Options options; + if (platform_info.device_type() == DEVICE_TPU && stream == nullptr) { + options = GenerateCompilerOptionsForTfrtTpu(*xla_device_compiler, *flr); + } else { + options = GenerateCompilerOptions(*xla_device_compiler, *flr, dev, stream, + platform_info, + /*has_ref_vars=*/false); + } + + return CompileAndBuildHLOString(stage, options, xla_device_compiler->client(), + function, args); +} + +absl::StatusOr GetCompilerIr( + IrExportStage stage, ProcessFunctionLibraryRuntime* pflr, + absl::string_view func_name, absl::string_view platform_name, + EagerContext* context, + absl::Span input_arg_shape_and_dtype, + absl::Span input_handles, + CompilerArgSource compiler_arg_source) { + using XlaDeviceCompiler = + DeviceCompiler; + + TF_RETURN_IF_ERROR( + ValidateGetCompilerIrTfrtTpu(platform_name, /*stream=*/nullptr, stage)); + + NameAttrList function; + function.set_name(std::string{func_name}); + + std::string device_name; + if (!platform_name.empty()) { + device_name = absl::StrCat("/device:", platform_name, ":0"); + } + FunctionLibraryRuntime* flr = pflr->GetFLR(device_name); + if (flr == nullptr) { + // Use CPU as the fallback to get the `FunctionLibraryRuntime`. + flr = pflr->GetFLR("/device:CPU:0"); + } + + TF_ASSIGN_OR_RETURN( + std::vector args, + PrepareXlaCompilerArgs(flr, function, context, /*dev=*/nullptr, + input_arg_shape_and_dtype, input_handles, + compiler_arg_source)); + + se::Platform::Id platform_id = nullptr; + if (platform_name == DEVICE_CPU) { + platform_id = se::host::kHostPlatformId; + } + XlaPlatformInfo platform_info(DeviceType(platform_name), platform_id, + /*xla_device_metadata=*/nullptr, + /*pjrt_device_metadata=*/nullptr, + /*device_allocator=*/nullptr); + DeviceType compilation_device_type = platform_info.device_type(); + if (platform_info.device_type() != DEVICE_TPU) { + TF_ASSIGN_OR_RETURN(compilation_device_type, + GetCompilationDeviceType(platform_info.device_type())); + } + XlaDeviceCompiler* xla_device_compiler; + const std::string xla_device_compiler_name = absl::StrCat( + absl::AsciiStrToLower(platform_name), "_xla_device_compiler"); + TF_RETURN_IF_ERROR( + context->HostCPU()->resource_manager()->LookupOrCreate( + context->HostCPU()->resource_manager()->default_container(), + xla_device_compiler_name, &xla_device_compiler, + [&](XlaDeviceCompiler** xla_device_compiler) { + return BuildXlaDeviceCompiler(/*dev=*/nullptr, flr, platform_info, + compilation_device_type, + xla_device_compiler); + })); + core::ScopedUnref xla_device_compiler_ref(xla_device_compiler); + + XlaCompiler::Options options; + if (platform_info.device_type() == DEVICE_TPU) { + options = GenerateCompilerOptionsForTfrtTpu(*xla_device_compiler, *flr); + } else { + options.device_type = compilation_device_type; + options.flib_def = flr->GetFunctionLibraryDefinition(); + options.graph_def_version = flr->graph_def_version(); + options.allow_cpu_custom_calls = + (platform_info.platform_id() == se::host::kHostPlatformId); + options.alias_passthrough_params = !platform_info.is_on_xla_device(); + } + + return CompileAndBuildHLOString(stage, options, /*local_client=*/nullptr, + function, args); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/get_compiler_ir.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/get_compiler_ir.h new file mode 100644 index 0000000000000000000000000000000000000000..92479a8fa100f58a71d70a30c2e11e30d64b8553 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/get_compiler_ir.h @@ -0,0 +1,82 @@ +/* Copyright 2020 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_JIT_GET_COMPILER_IR_H_ +#define TENSORFLOW_COMPILER_JIT_GET_COMPILER_IR_H_ + +#include +#include + +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" +#include "absl/types/span.h" +#include "tensorflow/compiler/tf2xla/xla_compiler.h" +#include "tensorflow/core/framework/tensor_shape.h" +#include "tensorflow/core/framework/types.pb.h" +#include "tensorflow/core/platform/statusor.h" + +namespace tensorflow { + +class ProcessFunctionLibraryRuntime; +class Device; +class Tensor; +class TensorHandle; +class EagerContext; + +enum class IrExportStage { + STABLEHLO, + STABLEHLO_SERIALIZED, + HLO, + HLO_NO_METADATA, + HLO_SERIALIZED, + OPTIMIZED_HLO, + OPTIMIZED_HLO_SERIALIZED, + OPTIMIZED_HLO_PROTO_SERIALIZED, + OPTIMIZED_HLO_DOT +}; + +struct ArgShapeAndDType { + TensorShape shape; + DataType dtype; +}; + +enum class CompilerArgSource { + TENSOR_SPEC, + CONCRETE_INPUT, +}; + +// Returns the IR format of the selected stage for a given function `func_name` +// using library runtime `runtime` on a device `dev` with given +// `inputs_arg_shape_and_dtype` and `input_handles`. +absl::StatusOr GetCompilerIr( + IrExportStage stage, ProcessFunctionLibraryRuntime* pflr, + absl::string_view func_name, Device* dev, EagerContext* context, + absl::Span input_arg_shape_and_dtype, + absl::Span input_handles, + CompilerArgSource compiler_arg_source); + +// Returns the IR format of the selected stage for a given function `func_name` +// using library runtime `runtime` on a platform `platform_name` with given +// `inputs_arg_shape_and_dtype` and `input_handles`. +absl::StatusOr GetCompilerIr( + IrExportStage stage, ProcessFunctionLibraryRuntime* pflr, + absl::string_view func_name, absl::string_view platform_name, + EagerContext* context, + absl::Span input_arg_shape_and_dtype, + absl::Span input_handles, + CompilerArgSource compiler_arg_source); + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_GET_COMPILER_IR_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/increase_dynamism_for_auto_jit_pass.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/increase_dynamism_for_auto_jit_pass.cc new file mode 100644 index 0000000000000000000000000000000000000000..89d501515aae7b0ca7c5130c1fba2efc923c9e44 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/increase_dynamism_for_auto_jit_pass.cc @@ -0,0 +1,408 @@ +/* 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 "tensorflow/compiler/jit/increase_dynamism_for_auto_jit_pass.h" + +#include + +#include "absl/algorithm/container.h" +#include "absl/log/check.h" +#include "absl/log/log.h" +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" +#include "absl/types/optional.h" +#include "tensorflow/cc/framework/ops.h" +#include "tensorflow/cc/framework/scope.h" +#include "tensorflow/cc/framework/scope_internal.h" +#include "tensorflow/cc/ops/array_ops.h" +#include "tensorflow/cc/ops/const_op.h" +#include "tensorflow/cc/ops/math_ops.h" +#include "tensorflow/compiler/jit/flags.h" +#include "tensorflow/compiler/jit/xla_cluster_util.h" +#include "xla/status_macros.h" +#include "tensorflow/core/common_runtime/optimization_registry.h" +#include "tensorflow/core/framework/node_def_util.h" +#include "tensorflow/core/framework/tensor.h" +#include "tensorflow/core/framework/tensor.pb.h" +#include "tensorflow/core/framework/types.pb.h" +#include "tensorflow/core/graph/algorithm.h" +#include "tensorflow/core/graph/graph.h" +#include "tensorflow/core/platform/statusor.h" +#include "tensorflow/core/platform/types.h" +#include "tensorflow/core/util/device_name_utils.h" +#include "tensorflow/core/util/dump_graph.h" +#include "tsl/platform/errors.h" +#include "tsl/platform/statusor.h" + +namespace tensorflow { +namespace { + +// StatusOrOptional instances hold +// +// - A non-OK Status to indicate an error that needs to be propagated out of +// this pass (e.g. the Graph is malformed). +// +// - A nullopt to indicate the function that created the instance failed to do +// what it set out to do but this is not actually an error +// (e.g. TryToGetTensorFromConstOp was passed a non-Const node). +// +// - A T to indicate a successful operation. +template +using StatusOrOptional = StatusOr>; + +StatusOrOptional TryToGetTensorFromConstOp(Node* n) { + if (n->type_string() != "Const") { + return {std::nullopt}; + } + + const TensorProto* proto = nullptr; + TF_RETURN_IF_ERROR(GetNodeAttr(n->def(), "value", &proto)); + Tensor tensor(proto->dtype()); + TF_RET_CHECK(tensor.FromProto(*proto)); + return {tensor}; +} + +struct SliceInputs { + Output slice_op; + Output input; + Output begin; + Output size; + + // The size of the TF slice operation as a std::vector. We can always compute + // this because we only manipulate slices with a Const size. + std::vector size_as_vector; +}; + +std::vector IntTensorAsVector(const Tensor& t) { + DCHECK(t.dtype() == DT_INT32 || t.dtype() == DT_INT64); + std::vector result; + result.reserve(t.NumElements()); + for (int i = 0; i < t.NumElements(); i++) { + int64_t element = t.dtype() == DT_INT32 + ? static_cast(t.flat()(i)) + : t.flat()(i); + result.push_back(element); + } + return result; +} + +// Packages up the inputs to a Slice operation into an instance of +// `SliceInputs`. +StatusOrOptional GetSliceInputs(Node* slice) { + const int kSliceInputIndex = 0; + const int kSliceBeginIndex = 1; + const int kSliceSizeIndex = 2; + + const Edge* slice_input_edge; + TF_RETURN_IF_ERROR(slice->input_edge(kSliceInputIndex, &slice_input_edge)); + const Edge* slice_size_edge; + TF_RETURN_IF_ERROR(slice->input_edge(kSliceSizeIndex, &slice_size_edge)); + const Edge* slice_begin_edge; + TF_RETURN_IF_ERROR(slice->input_edge(kSliceBeginIndex, &slice_begin_edge)); + + SliceInputs slice_inputs; + slice_inputs.input = + Output(slice_input_edge->src(), slice_input_edge->src_output()); + slice_inputs.begin = + Output(slice_begin_edge->src(), slice_begin_edge->src_output()); + slice_inputs.size = + Output(slice_size_edge->src(), slice_size_edge->src_output()); + + TF_ASSIGN_OR_RETURN(std::optional tf_slice_size, + TryToGetTensorFromConstOp(slice_inputs.size.node())); + if (!tf_slice_size.has_value()) { + return {std::nullopt}; + } + + if (tf_slice_size->dims() != 1) { + return {std::nullopt}; + } + + slice_inputs.size_as_vector = IntTensorAsVector(*tf_slice_size); + return {slice_inputs}; +} + +// Casts `x` to a DT_INT64 if it isn't one already. +Output MakeInt64(const Scope& host_scope, absl::string_view name, + const Output& x) { + return x.type() == DT_INT64 + ? x + : ops::Cast(host_scope.WithOpName(name, "_s64"), x, DT_INT64); +} + +// Returns `slice_inputs` with the index and size inputs cast to DT_INT64. +SliceInputs MakeSliceIndexAndSizeInt64(const Scope& host_scope, + const SliceInputs& slice_inputs) { + SliceInputs result; + result.input = slice_inputs.input; + result.begin = MakeInt64(host_scope, "begin", slice_inputs.begin); + result.size = MakeInt64(host_scope, "size", slice_inputs.size); + result.size_as_vector = slice_inputs.size_as_vector; + return result; +} + +// This class caches emitted constants to avoid creating multiple nodes for the +// same constant value. This helps make the generated GraphDef more readable. +class ConstantCache { + public: + explicit ConstantCache(const Scope& s, + const std::vector& control_deps) + : scope_(s), control_deps_(control_deps) {} + + Output Get1DHostConstant(int64_t constant) { + auto it = cache_.find(constant); + if (it == cache_.end()) { + Output new_const = + ops::Const(scope_.WithOpName("const_", constant), {constant}); + it = cache_.insert({constant, new_const}).first; + for (const Edge* e : control_deps_) { + scope_.graph()->AddControlEdge(e->src(), new_const.node()); + } + } + return it->second; + } + + private: + Scope scope_; + std::unordered_map cache_; + std::vector control_deps_; +}; + +// Returns a node computing the size of the Slice op with inputs `slice_inputs`. +absl::Status ComputeSliceSize(const Scope& host_scope, + const SliceInputs& slice_inputs, + std::vector control_deps, + Output* size) { + // If slice_size[i] >= 0 then slice_size[i] = slice_size[i]. + // + // If slice_size[i] == -1 then slice_size[i] = input_size[i] - + // begin[i]. + // + // If slice_size[i] < -1 then executing the slice will throw an error, and we + // don't do anything here. We've already filtered these cases out in + // IsRewritableSlice. + + if (absl::c_all_of(slice_inputs.size_as_vector, + [](int64_t i) { return i >= 0; })) { + *size = slice_inputs.size; + return absl::OkStatus(); + } + + Output input_shape = + ops::Shape(host_scope.WithOpName("input_shape"), slice_inputs.input, + ops::Shape::OutType(DT_INT64)); + + ConstantCache constant_pool(host_scope, control_deps); + + std::vector slice_size; + for (int i = 0, end = slice_inputs.size_as_vector.size(); i < end; i++) { + if (slice_inputs.size_as_vector[i] >= 0) { + slice_size.push_back( + constant_pool.Get1DHostConstant(slice_inputs.size_as_vector[i])); + continue; + } + + DCHECK_EQ(slice_inputs.size_as_vector[i], -1); + + Output begin_i = ops::Slice( + host_scope.WithOpName("begin_", i), slice_inputs.begin, + constant_pool.Get1DHostConstant(i), constant_pool.Get1DHostConstant(1)); + + Output input_shape_i = ops::Slice( + host_scope.WithOpName("input_shape_", i), input_shape, + constant_pool.Get1DHostConstant(i), constant_pool.Get1DHostConstant(1)); + + slice_size.push_back(ops::Sub(host_scope.WithOpName("slice_size_", i), + input_shape_i, begin_i)); + DCHECK_EQ(slice_size.back().type(), DT_INT64); + } + + // Trivial ConcatV2 nodes (with exactly one input) are disallowed. + if (slice_size.size() == 1) { + *size = slice_size[0]; + } else { + auto concat_axis = ops::Const(host_scope.WithOpName("concat_axis"), 0); + for (const Edge* e : control_deps) { + host_scope.graph()->AddControlEdge(e->src(), concat_axis.node()); + } + *size = ops::Concat(host_scope.WithOpName("slice_size"), slice_size, + concat_axis); + } + return absl::OkStatus(); +} + +// Terminology: "static sized" slice is a slice with the +// _XlaCompileTimeConstantInputs attribute set to {2}. The output shape of +// these slices can be solely determined by their "size" input. +absl::Status ConvertTensorFlowSliceToStaticShapedSlice( + Graph* g, Node* slice, const SliceInputs& slice_inputs, + absl::string_view cluster_name, Node** result) { + std::string host_name; + TF_RETURN_IF_ERROR(DeviceNameUtils::DeviceNameToCpuDeviceName( + slice->assigned_device_name(), &host_name)); + + absl::Status status; + Scope main_scope = + NewInternalScope(g, &status, /*refiner=*/nullptr) + .WithXlaCluster(std::string(cluster_name)) + .NewSubScope(absl::StrCat(slice->name(), "/static_shaped_slice")); + Scope host_scope = main_scope.WithAssignedDevice(host_name); + + // In the future we may want to be clever here and avoid the extra Cast ops. + SliceInputs slice_inputs_int64 = + MakeSliceIndexAndSizeInt64(host_scope, slice_inputs); + + // Create a list of all control dependencies to be copied when possibly + // replacing nodes related to slice_size. + Node* old_size; + std::vector old_size_ctrl_deps; + TF_RETURN_IF_ERROR(slice->input_node(2, &old_size)); + absl::c_copy_if(old_size->in_edges(), std::back_inserter(old_size_ctrl_deps), + [](const Edge* e) { return e->IsControlEdge(); }); + + Output slice_size; + TF_RETURN_IF_ERROR(ComputeSliceSize(host_scope, slice_inputs_int64, + old_size_ctrl_deps, &slice_size)); + + *result = + ops::Slice(main_scope.WithAssignedDevice(slice->assigned_device_name()) + .WithOpName("static_shaped_slice"), + slice_inputs_int64.input, slice_inputs_int64.begin, slice_size) + .node(); + + TF_RETURN_IF_ERROR(main_scope.status()); + + std::vector compile_time_const_inputs; + compile_time_const_inputs.push_back("size"); + (*result)->AddAttr(kXlaCompileTimeConstantInputsAttr, + compile_time_const_inputs); + return status; +} + +void ReplaceTensorFlowSliceWithStaticShapedSlice(Graph* g, Node* slice, + Node* static_shaped_slice) { + std::vector slice_out_edges; + absl::c_copy(slice->out_edges(), std::back_inserter(slice_out_edges)); + for (const Edge* e : slice_out_edges) { + DCHECK(e->src_output() == 0 || e->src_output() == Graph::kControlSlot); + + int src_output = e->src_output(); + int dst_input = e->dst_input(); + Node* dst = e->dst(); + g->RemoveEdge(e); + g->AddEdge(static_shaped_slice, src_output, dst, dst_input); + } + + for (const Edge* e : slice->in_edges()) { + if (e->IsControlEdge()) { + g->AddControlEdge(e->src(), static_shaped_slice); + } + } + + g->RemoveNode(slice); +} + +absl::Status RewriteSlice(Graph* g, Node* slice, + const SliceInputs& slice_inputs, + absl::string_view cluster_name) { + VLOG(3) << "Rewriting slice " << slice->name() + << " to a \"static shaped\" Slice"; + Node* static_shaped_slice; + TF_RETURN_IF_ERROR(ConvertTensorFlowSliceToStaticShapedSlice( + g, slice, slice_inputs, cluster_name, &static_shaped_slice)); + ReplaceTensorFlowSliceWithStaticShapedSlice(g, slice, static_shaped_slice); + return absl::OkStatus(); +} + +// Return true if `n` is a slice we should rewrite to have a static shape +// (i.e. have the output shape only depend on the "size" input). +absl::StatusOr ShouldRewriteSlice(Node* n) { + if (n->type_string() != "Slice") { + return false; + } + + if (!GetXlaClusterForNode(*n).has_value()) { + // There is no need to change slice ops outside XLA clusters. + return false; + } + + TF_ASSIGN_OR_RETURN(std::optional slice_inputs, + GetSliceInputs(n)); + if (!slice_inputs.has_value()) { + return false; + } + + // If slice_size[i] < -1 for any i then executing the slice will throw an + // error, and we don't do anything here. + bool slice_size_has_error = + absl::c_all_of(slice_inputs->size_as_vector, + [](int64_t size_i) { return size_i >= -1; }); + if (!slice_size_has_error) { + return false; + } + + // No point in rewriting slices that have both size and begin as constants. + return !slice_inputs->begin.node()->IsConstant(); +} + +absl::Status FindAndRewriteSlices(Graph* g, bool* changed) { + std::vector slices_to_rewrite; + for (Node* n : g->nodes()) { + TF_ASSIGN_OR_RETURN(bool is_rewritable, ShouldRewriteSlice(n)); + if (is_rewritable) { + slices_to_rewrite.push_back(n); + } + } + + for (Node* n : slices_to_rewrite) { + TF_ASSIGN_OR_RETURN(std::optional slice_inputs, + GetSliceInputs(n)); + TF_RET_CHECK(slice_inputs.has_value()); + TF_RETURN_IF_ERROR( + RewriteSlice(g, n, *slice_inputs, *GetXlaClusterForNode(*n))); + } + + if (!slices_to_rewrite.empty()) { + // We've added constants to the graph; hook them up to _SOURCE. + FixupSourceAndSinkEdges(g); + } + + *changed = !slices_to_rewrite.empty(); + + return absl::OkStatus(); +} +} // namespace + +absl::Status IncreaseDynamismForAutoJitPass::Run( + const GraphOptimizationPassOptions& options) { + MarkForCompilationPassFlags* flags = GetMarkForCompilationPassFlags(); + if (flags->tf_xla_clustering_debug) { + DumpGraphToFile("before_increase_dynamism_for_auto_jit_pass", + **options.graph, options.flib_def); + } + + bool changed; + TF_RETURN_IF_ERROR(FindAndRewriteSlices(options.graph->get(), &changed)); + if (changed && flags->tf_xla_clustering_debug) { + DumpGraphToFile("increase_dynamism_for_auto_jit_pass", **options.graph, + options.flib_def); + } + + return absl::OkStatus(); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/increase_dynamism_for_auto_jit_pass.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/increase_dynamism_for_auto_jit_pass.h new file mode 100644 index 0000000000000000000000000000000000000000..0aafed8c2c3ecf52a4718832296e755389b8faff --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/increase_dynamism_for_auto_jit_pass.h @@ -0,0 +1,59 @@ +/* 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_INCREASE_DYNAMISM_FOR_AUTO_JIT_PASS_H_ +#define TENSORFLOW_COMPILER_JIT_INCREASE_DYNAMISM_FOR_AUTO_JIT_PASS_H_ + +#include "absl/status/status.h" +#include "tensorflow/core/common_runtime/optimization_registry.h" +#include "tensorflow/core/platform/status.h" + +namespace tensorflow { + +// Increases the amount of "dynamism" representable by XLA clusters by rewriting +// the TensorFlow graph. This pass does the following rewrites: +// +// Slice +// ----- +// +// Slice(op, begin, size ) => +// Slice(op, begin, actual_size(op.shape(), size, begin)); +// _XlaCompileTimeConstantInputs={2} +// +// where +// +// actual_size(op_shape, size, begin)[i] = +// size[i] == -1 ? (op_shape[i] - size[i]) +// : size[i] +// +// This pass, combined with jit/partially_decluster_pass, reduces the number of +// unnecessary cluster recompilations in some common cases. After the rewrite +// shown above jit/partially_decluster_pass extracts the actual_size(...) +// computation to outside the XLA cluster, causing the cluster to be versioned +// only on the actual size of the XlaDynamicSlice. This avoids recompilation +// due to superficial changes that don't affect tensor shapes. +// +// Future Work TODO(b/111210515) +// ----------------------------- +// +// In the future we will also translate StridedSlice and Pad a similar way. +class IncreaseDynamismForAutoJitPass : public GraphOptimizationPass { + public: + absl::Status Run(const GraphOptimizationPassOptions& options) override; +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_INCREASE_DYNAMISM_FOR_AUTO_JIT_PASS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/increase_dynamism_for_auto_jit_pass_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/increase_dynamism_for_auto_jit_pass_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..2dc282913b89c25aa9b4342c60db6c4ec41a1dcf --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/increase_dynamism_for_auto_jit_pass_test.cc @@ -0,0 +1,477 @@ +/* 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 "tensorflow/compiler/jit/increase_dynamism_for_auto_jit_pass.h" + +#include +#include "absl/status/status.h" +#include "tensorflow/cc/framework/ops.h" +#include "tensorflow/cc/framework/scope.h" +#include "tensorflow/cc/ops/array_ops.h" +#include "tensorflow/cc/ops/const_op.h" +#include "tensorflow/compiler/jit/node_matchers.h" +#include "tensorflow/compiler/jit/xla_cluster_util.h" +#include "xla/tsl/lib/core/status_test_util.h" +#include "tensorflow/core/common_runtime/device_set.h" +#include "tensorflow/core/common_runtime/optimization_registry.h" +#include "tensorflow/core/framework/allocator.h" +#include "tensorflow/core/framework/device.h" +#include "tensorflow/core/framework/device_attributes.pb.h" +#include "tensorflow/core/framework/op.h" +#include "tensorflow/core/framework/types.h" +#include "tensorflow/core/framework/types.pb.h" +#include "tensorflow/core/graph/graph.h" +#include "tensorflow/core/platform/errors.h" +#include "tensorflow/core/platform/test.h" +#include "tensorflow/core/platform/types.h" +#include "tensorflow/core/protobuf/config.pb.h" +#include "tensorflow/core/public/session_options.h" +#include "tsl/platform/errors.h" + +namespace tensorflow { +namespace { + +using ::testing::_; +using testing::matchers::AssignedDevice; +using testing::matchers::Attr; +using testing::matchers::Const; +using testing::matchers::CtrlDeps; +using testing::matchers::Inputs; +using testing::matchers::Name; +using testing::matchers::NodeWith; +using testing::matchers::Op; +using testing::matchers::Out; + +// A fake device used to populate a DeviceSet. +class FakeDevice : public Device { + public: + explicit FakeDevice(const DeviceAttributes& device_attributes) + : Device(nullptr, device_attributes) {} + + absl::Status Sync() override { + return absl::UnimplementedError("FakeDevice::Sync()"); + } + + Allocator* GetAllocator(AllocatorAttributes attr) override { return nullptr; } + + static std::unique_ptr Make(const std::string& name, + const std::string& type) { + DeviceAttributes device_attributes; + device_attributes.set_name(name); + device_attributes.set_device_type(DeviceType(type).type()); + return std::make_unique(device_attributes); + } +}; + +const char* kHostName = "/job:worker/replica:0/task:0/device:CPU:0"; +const char* kDeviceName = "/job:worker/replica:0/task:0/device:GPU:0"; + +absl::Status IncreaseDynamismForAutoJit(const Scope& s, + std::unique_ptr* result) { + std::vector> devices; + devices.push_back(FakeDevice::Make(kDeviceName, DEVICE_GPU)); + devices.push_back(FakeDevice::Make(kHostName, DEVICE_CPU)); + + std::unique_ptr device_set(new DeviceSet()); + for (auto& device : devices) { + device_set->AddDevice(device.get()); + } + + auto graph = std::make_unique(OpRegistry::Global()); + SessionOptions session_options; + session_options.config.mutable_graph_options() + ->mutable_optimizer_options() + ->set_global_jit_level(OptimizerOptions::ON_2); + GraphOptimizationPassOptions options; + options.graph = &graph; + options.device_set = device_set.get(); + options.session_options = &session_options; + + // Scope::ToGraph seems to drop assigned devices, probably because it goes + // through a GraphDef. So explicitly maintain the device assignment. + std::unordered_map assigned_device_names; + for (Node* n : s.graph()->nodes()) { + assigned_device_names[n->name()] = n->assigned_device_name(); + } + TF_RETURN_IF_ERROR(s.ToGraph(graph.get())); + for (Node* n : graph->nodes()) { + n->set_assigned_device_name(assigned_device_names[n->name()]); + } + + IncreaseDynamismForAutoJitPass rewriter; + TF_RETURN_IF_ERROR(rewriter.Run(options)); + *result = std::move(graph); + return absl::OkStatus(); +} + +TEST(SliceToDynamicSliceRewriteTest, Basic) { + Scope root = Scope::NewRootScope() + .ExitOnError() + .WithAssignedDevice(kDeviceName) + .WithXlaCluster("cluster_0"); + + Output input = ops::Placeholder(root.WithOpName("input"), DT_FLOAT); + Output begin = ops::Placeholder(root.WithOpName("begin"), DT_INT32); + Output size = ops::Const(root.WithOpName("size"), {-1, 500}); + Output slice = ops::Slice(root.WithOpName("slice"), input, begin, size); + + std::unique_ptr result; + TF_ASSERT_OK(IncreaseDynamismForAutoJit(root, &result)); + + const int64_t zero_64 = 0; + const int32_t zero_32 = 0; + const int64_t one_64 = 1; + + auto m_input = Out(NodeWith(Op("Placeholder"), Name("input"))); + auto m_begin_s64 = Out(NodeWith( + Op("Cast"), Inputs(Out(NodeWith(Op("Placeholder"), Name("begin")))))); + auto m_input_shape = Out(NodeWith(Op("Shape"), Inputs(m_input))); + auto m_slice_size_0 = Out(NodeWith( + Op("Sub"), AssignedDevice(kHostName), + Inputs( + Out(NodeWith(Op("Slice"), AssignedDevice(kHostName), + Inputs(m_input_shape, Const(zero_64), Const(one_64)))), + Out(NodeWith(Op("Slice"), AssignedDevice(kHostName), + Inputs(m_begin_s64, Const(zero_64), Const(one_64))))))); + auto m_dynamic_slice_size = + Out(NodeWith(Op("ConcatV2"), AssignedDevice(kHostName), + Inputs(m_slice_size_0, Const(static_cast(500)), + Const(zero_32)))); + + std::vector compile_time_constant_inputs; + compile_time_constant_inputs.push_back("size"); + auto m_dynamic_slice = NodeWith( + Op("Slice"), AssignedDevice(kDeviceName), + Attr(kXlaCompileTimeConstantInputsAttr, compile_time_constant_inputs), + Inputs(m_input, m_begin_s64, m_dynamic_slice_size)); + + Node* static_shaped_slice = testing::FindNodeByName( + result.get(), "slice/static_shaped_slice/static_shaped_slice"); + ASSERT_NE(static_shaped_slice, nullptr); + EXPECT_THAT(static_shaped_slice, m_dynamic_slice); +} + +TEST(SliceToDynamicSliceRewriteTest, SliceFromVector) { + Scope root = Scope::NewRootScope() + .ExitOnError() + .WithAssignedDevice(kDeviceName) + .WithXlaCluster("cluster_0"); + + Output input = ops::Placeholder(root.WithOpName("input"), DT_FLOAT); + Output begin = ops::Placeholder(root.WithOpName("begin"), DT_INT32); + Output size = ops::Const(root.WithOpName("size"), {-1}); + Output slice = ops::Slice(root.WithOpName("slice"), input, begin, size); + + std::unique_ptr result; + TF_ASSERT_OK(IncreaseDynamismForAutoJit(root, &result)); + + Node* static_shaped_slice = testing::FindNodeByName( + result.get(), "slice/static_shaped_slice/static_shaped_slice"); + EXPECT_NE(static_shaped_slice, nullptr); + EXPECT_THAT(result->nodes(), Not(Contains(NodeWith(Op("ConcatV2"))))); +} + +TEST(SliceToDynamicSliceRewriteTest, ControlDependencePreserved) { + Scope root = Scope::NewRootScope() + .ExitOnError() + .WithAssignedDevice(kDeviceName) + .WithXlaCluster("cluster_0"); + + Output input = ops::Placeholder(root.WithOpName("input"), DT_FLOAT); + Output begin = ops::Placeholder(root.WithOpName("begin"), DT_INT32); + Output size = ops::Const(root.WithOpName("size"), {-1, 500}); + Output control_pred = ops::Placeholder(root.WithOpName("control"), DT_BOOL); + Output slice = ops::Slice(root.WithOpName("slice"), input, begin, size); + root.graph()->AddControlEdge(control_pred.node(), slice.node()); + + std::unique_ptr result; + TF_ASSERT_OK(IncreaseDynamismForAutoJit(root, &result)); + + Node* static_shaped_slice = testing::FindNodeByName( + result.get(), "slice/static_shaped_slice/static_shaped_slice"); + ASSERT_NE(static_shaped_slice, nullptr); + EXPECT_THAT(static_shaped_slice, + NodeWith(Op("Slice"), + CtrlDeps(NodeWith(Op("Placeholder"), Name("control"))))); +} + +int64_t ToInt64(int v) { return static_cast(v); } + +TEST(SliceToDynamicSliceRewriteTest, Int64Indices) { + Scope root = Scope::NewRootScope() + .ExitOnError() + .WithAssignedDevice(kDeviceName) + .WithXlaCluster("cluster_0"); + + Output input = ops::Placeholder(root.WithOpName("input"), DT_FLOAT); + Output begin = ops::Placeholder(root.WithOpName("begin"), DT_INT64); + Output size = + ops::Const(root.WithOpName("size"), {ToInt64(-1), ToInt64(500)}); + Output slice = ops::Slice(root.WithOpName("slice"), input, begin, size); + + std::unique_ptr result; + TF_ASSERT_OK(IncreaseDynamismForAutoJit(root, &result)); + + EXPECT_THAT(result->nodes(), Not(Contains(NodeWith(Op("Cast"))))); +} + +TEST(SliceToDynamicSliceRewriteTest, DontRewriteInvalidSlice) { + Scope root = Scope::NewRootScope() + .ExitOnError() + .WithAssignedDevice(kDeviceName) + .WithXlaCluster("cluster_0"); + + Output input = ops::Placeholder(root.WithOpName("input"), DT_FLOAT); + Output begin = ops::Placeholder(root.WithOpName("begin"), DT_INT32); + + // The shape refiner throws an error if we use a bogus constant value for + // size. So we first use a Placeholder to placate the shape refiner, and + // later replace it with a bogus constant. + Output size_placeholder = + ops::Placeholder(root.WithOpName("size_placeholder"), DT_INT32); + Output slice = + ops::Slice(root.WithOpName("slice"), input, begin, size_placeholder); + + Output size = ops::Const(root.WithOpName("size"), {-8, 500}); + TF_ASSERT_OK(root.graph()->UpdateEdge(/*new_src=*/size.node(), + /*new_src_index=*/0, + /*dst=*/slice.node(), /*dst_index=*/2)); + + std::unique_ptr result; + TF_ASSERT_OK(IncreaseDynamismForAutoJit(root, &result)); + + EXPECT_THAT(result->nodes(), + Not(Contains(NodeWith(Op("Slice"), + Attr(kXlaCompileTimeConstantInputsAttr))))); +} + +TEST(SliceToDynamicSliceRewriteTest, DontRewriteUnclusteredSlice) { + Scope root = + Scope::NewRootScope().ExitOnError().WithAssignedDevice(kDeviceName); + + Output input = ops::Placeholder(root.WithOpName("input"), DT_FLOAT); + Output begin = ops::Placeholder(root.WithOpName("begin"), DT_INT32); + Output size = ops::Const(root.WithOpName("size"), {-1, 500}); + Output slice = ops::Slice(root.WithOpName("slice"), input, begin, size); + + std::unique_ptr result; + TF_ASSERT_OK(IncreaseDynamismForAutoJit(root, &result)); + + EXPECT_THAT(result->nodes(), + Not(Contains(NodeWith(Op("Slice"), + Attr(kXlaCompileTimeConstantInputsAttr))))); +} + +TEST(SliceToDynamicSliceRewriteTest, DontRewriteSliceWithNonConstSize) { + Scope root = Scope::NewRootScope() + .ExitOnError() + .WithAssignedDevice(kDeviceName) + .WithXlaCluster("cluster_0"); + + Output input = ops::Placeholder(root.WithOpName("input"), DT_FLOAT); + Output begin = ops::Placeholder(root.WithOpName("begin"), DT_INT64); + Output size = ops::Placeholder(root.WithOpName("size"), DT_INT64); + Output slice = ops::Slice(root.WithOpName("slice"), input, begin, size); + + std::unique_ptr result; + TF_ASSERT_OK(IncreaseDynamismForAutoJit(root, &result)); + + EXPECT_THAT(result->nodes(), + Not(Contains(NodeWith(Op("Slice"), + Attr(kXlaCompileTimeConstantInputsAttr))))); +} + +TEST(SliceToDynamicSliceRewriteTest, ScalarSlice) { + Scope root = Scope::NewRootScope() + .ExitOnError() + .WithAssignedDevice(kDeviceName) + .WithXlaCluster("cluster_0"); + + Output input = ops::Placeholder(root.WithOpName("input"), DT_FLOAT); + Output begin = ops::Placeholder(root.WithOpName("begin"), DT_INT64); + Output size = ops::Const(root.WithOpName("size"), {}); + Output slice = ops::Slice(root.WithOpName("slice"), input, begin, size); + + std::unique_ptr result; + TF_ASSERT_OK(IncreaseDynamismForAutoJit(root, &result)); + + Node* static_shaped_slice = testing::FindNodeByName( + result.get(), "slice/static_shaped_slice/static_shaped_slice"); + ASSERT_NE(static_shaped_slice, nullptr); + EXPECT_THAT(static_shaped_slice, + NodeWith(Op("Slice"), Attr(kXlaCompileTimeConstantInputsAttr), + Inputs(_, _, Out(NodeWith(Name(size.node()->name())))))); +} + +TEST(SliceToDynamicSliceRewriteTest, IndicesNotVector) { + Scope root = Scope::NewRootScope() + .ExitOnError() + .WithAssignedDevice(kDeviceName) + .WithXlaCluster("cluster_0"); + + auto ToInt64 = [](int v) { return static_cast(v); }; + + Output input = ops::Placeholder(root.WithOpName("input"), DT_FLOAT); + Output begin = ops::Placeholder(root.WithOpName("begin"), DT_INT64); + + // The C++ node bindings immediately error out when we try construct a bogus + // slice so we first use a placeholder to construct the Slice and then replace + // the input. + Output size_placeholder = ops::Placeholder(root.WithOpName("size"), DT_INT64); + Output slice = + ops::Slice(root.WithOpName("slice"), input, begin, size_placeholder); + + Output size = + ops::Const(root.WithOpName("size"), {{ToInt64(-1)}, {ToInt64(500)}}); + TF_ASSERT_OK(root.graph()->UpdateEdge(size.node(), 0, slice.node(), 2)); + + std::unique_ptr result; + TF_ASSERT_OK(IncreaseDynamismForAutoJit(root, &result)); + + EXPECT_THAT(result->nodes(), + Not(Contains(NodeWith(Op("Slice"), + Attr(kXlaCompileTimeConstantInputsAttr))))); +} + +TEST(SliceToDynamicSliceRewriteTest, SliceWithSliceInput) { + Scope root = Scope::NewRootScope() + .ExitOnError() + .WithAssignedDevice(kDeviceName) + .WithXlaCluster("cluster_0"); + + Output input = ops::Placeholder(root.WithOpName("input"), DT_FLOAT); + Output begin = ops::Placeholder(root.WithOpName("begin"), DT_INT32); + Output size_a = ops::Const(root.WithOpName("size_a"), {-1, 500}); + Output slice = ops::Slice(root.WithOpName("slice"), input, begin, size_a); + + Output size_b = ops::Const(root.WithOpName("size_a"), {-1, 200}); + Output slice_with_slice_input = ops::Slice( + root.WithOpName("slice_with_slice_input"), slice, begin, size_b); + + std::unique_ptr result; + TF_ASSERT_OK(IncreaseDynamismForAutoJit(root, &result)); + + Node* static_shaped_slice = testing::FindNodeByName( + result.get(), + "slice_with_slice_input/static_shaped_slice/static_shaped_slice"); + ASSERT_NE(static_shaped_slice, nullptr); + EXPECT_EQ(static_shaped_slice->output_type(0), DT_FLOAT) + << "Expected DT_FLOAT, was " + << DataType_Name(static_shaped_slice->output_type(0)); + EXPECT_THAT( + static_shaped_slice, + NodeWith( + Op("Slice"), + Inputs(Out(NodeWith( + Op("Slice"), + Name("slice/static_shaped_slice/static_shaped_slice"))), + _, _))); +} + +TEST(SliceToDynamicSliceRewriteTest, SliceWithSliceBegin) { + Scope root = Scope::NewRootScope() + .ExitOnError() + .WithAssignedDevice(kDeviceName) + .WithXlaCluster("cluster_0"); + + Output input_float = + ops::Placeholder(root.WithOpName("input_float"), DT_FLOAT); + Output input_i64 = ops::Placeholder(root.WithOpName("input_i64"), DT_INT64); + + Output begin_begin = + ops::Placeholder(root.WithOpName("begin_begin"), DT_INT32); + Output begin_size = ops::Const(root.WithOpName("begin_size"), {-1}); + Output begin = + ops::Slice(root.WithOpName("begin"), input_i64, begin_begin, begin_size); + + Output size = + ops::Const(root.WithOpName("size"), {ToInt64(-1), ToInt64(200)}); + Output slice_with_slice_begin = ops::Slice( + root.WithOpName("slice_with_slice_begin"), input_float, begin, size); + + std::unique_ptr result; + TF_ASSERT_OK(IncreaseDynamismForAutoJit(root, &result)); + + Node* static_shaped_slice = testing::FindNodeByName( + result.get(), + "slice_with_slice_begin/static_shaped_slice/static_shaped_slice"); + ASSERT_NE(static_shaped_slice, nullptr); + EXPECT_EQ(static_shaped_slice->output_type(0), DT_FLOAT) + << "Expected DT_FLOAT, was " + << DataType_Name(static_shaped_slice->output_type(0)); + EXPECT_THAT( + static_shaped_slice, + NodeWith( + Op("Slice"), + Inputs(_, + Out(NodeWith( + Op("Slice"), + Name("begin/static_shaped_slice/static_shaped_slice"))), + _))); +} + +// New constants being created need to have control dependencies copied to +// ensure correct control flow analysis in TF V2. +TEST(SliceToDynamicSliceRewriteTest, WithControlDepsToConstant) { + Scope root = Scope::NewRootScope() + .ExitOnError() + .WithAssignedDevice(kDeviceName) + .WithXlaCluster("cluster_0"); + + Output input = ops::Placeholder(root.WithOpName("input"), DT_FLOAT); + Output begin = ops::Placeholder(root.WithOpName("begin"), DT_INT32); + Output size = ops::Const(root.WithOpName("size"), {-1}); + Output slice = ops::Slice(root.WithOpName("slice"), input, begin, size); + + // Add an additional dependency that should still exist in with the new size + // variables. + Output dependency = ops::Placeholder(root.WithOpName("dependency"), DT_BOOL); + root.graph()->AddControlEdge(dependency.node(), size.node()); + + std::unique_ptr result; + TF_ASSERT_OK(IncreaseDynamismForAutoJit(root, &result)); + + // Check that the new constants have control dependencies. + Node* const_0 = testing::FindNodeByName(result.get(), + "slice/static_shaped_slice/const_0"); + EXPECT_NE(const_0, nullptr); + EXPECT_THAT(const_0, + NodeWith(Op("Const"), CtrlDeps(NodeWith(Op("Placeholder"), + Name("dependency"))))); +} + +TEST(SliceToDynamicSliceRewriteTest, DontRewriteSliceWithConstBegin) { + Scope root = Scope::NewRootScope() + .ExitOnError() + .WithAssignedDevice(kDeviceName) + .WithXlaCluster("cluster_0"); + + Output input = ops::Placeholder(root.WithOpName("input"), DT_FLOAT); + Output begin = ops::Const(root.WithOpName("begin"), {10, 10}); + Output size = ops::Const(root.WithOpName("size"), {-1, 500}); + Output slice = ops::Slice(root.WithOpName("slice"), input, begin, size); + + std::unique_ptr result; + TF_ASSERT_OK(IncreaseDynamismForAutoJit(root, &result)); + + Node* slice_node = testing::FindNodeByName(result.get(), "slice"); + EXPECT_THAT(slice_node, + NodeWith(Op("Slice"), Inputs(Out(NodeWith(Op("Placeholder"))), + Out(NodeWith(Op("Const"))), + Out(NodeWith(Op("Const")))))); +} + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/jit_compilation_pass_registration.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/jit_compilation_pass_registration.cc new file mode 100644 index 0000000000000000000000000000000000000000..44209d9e1e191c4af0abcc7e1d1fdae4a62b3332 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/jit_compilation_pass_registration.cc @@ -0,0 +1,84 @@ +/* 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/compiler/jit/build_xla_ops_pass.h" +#include "tensorflow/compiler/jit/clone_constants_for_better_clustering.h" +#include "tensorflow/compiler/jit/cluster_scoping_pass.h" +#include "tensorflow/compiler/jit/encapsulate_subgraphs_pass.h" +#include "tensorflow/compiler/jit/encapsulate_xla_computations_pass.h" +#include "tensorflow/compiler/jit/force_xla_constants_on_host_pass.h" +#include "tensorflow/compiler/jit/increase_dynamism_for_auto_jit_pass.h" +#include "tensorflow/compiler/jit/mark_for_compilation_pass.h" +#include "tensorflow/compiler/jit/partially_decluster_pass.h" +#include "tensorflow/compiler/jit/report_clustering_info_pass.h" +#include "tensorflow/core/common_runtime/optimization_registry.h" + +namespace tensorflow { + +// PRE_PLACEMENT passes: + +// EncapsulateXlaComputationsPass rewrites computations generated by the +// xla.compile() Python code into XlaLaunch nodes. +REGISTER_OPTIMIZATION(OptimizationPassRegistry::PRE_PLACEMENT, 36, + EncapsulateXlaComputationsPass); + +// from +// tensorflow/compiler/tf2xla/functionalize_control_flow_pass_registration.cc +// FunctionalizeControlFlowPass: 27 +// +// This pass looks at the graph and all associated FunctionDefs, and turns +// traditional control flow structure (Switch/Merge/etc.) into functional +// control flow structure (XlaIf/XlaWhile). Following passes must +// handle those FunctionDef correctly. + +// POST_REWRITE_FOR_EXEC passes that support auto-clustering to enable XLA: + +REGISTER_OPTIMIZATION(OptimizationPassRegistry::POST_REWRITE_FOR_EXEC, 5, + CloneConstantsForBetterClusteringPass); + +REGISTER_OPTIMIZATION(OptimizationPassRegistry::POST_REWRITE_FOR_EXEC, 9, + ClusterScopingPass); + +REGISTER_OPTIMIZATION(OptimizationPassRegistry::POST_REWRITE_FOR_EXEC, 10, + MarkForCompilationPass); + +REGISTER_OPTIMIZATION(OptimizationPassRegistry::POST_REWRITE_FOR_EXEC, 12, + ForceXlaConstantsOnHostPass); + +REGISTER_OPTIMIZATION(OptimizationPassRegistry::POST_REWRITE_FOR_EXEC, 20, + IncreaseDynamismForAutoJitPass); + +REGISTER_OPTIMIZATION(OptimizationPassRegistry::POST_REWRITE_FOR_EXEC, 30, + PartiallyDeclusterPass); + +// ReportClusteringInfoPass pass needs to run after all of the auto-clustering +// passes have run but before encapsulation has run. This way it can easily +// compute a summary of the clustering decisions we made and broadcast it via +// xla_activity_listener. +REGISTER_OPTIMIZATION(OptimizationPassRegistry::POST_REWRITE_FOR_EXEC, 40, + ReportClusteringInfoPass); + +// The EncapsulateSubgraphs pass must run after the MarkForCompilationPass. We +// also need to run it after the graph been rewritten to have _Send nodes added +// for fetches. Before the _Send nodes are added, fetch nodes are identified by +// name, and encapsulation might remove that node from the graph. +REGISTER_OPTIMIZATION(OptimizationPassRegistry::POST_REWRITE_FOR_EXEC, 50, + EncapsulateSubgraphsPass); + +// Must run after EncapsulateSubgraphsPass. +REGISTER_OPTIMIZATION(OptimizationPassRegistry::POST_REWRITE_FOR_EXEC, 60, + BuildXlaOpsPass); + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/kernels/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/jit/kernels/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..fb414df3e4f595483ae1f8d6d83c48a9878e45c6 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/kernels/BUILD @@ -0,0 +1,91 @@ +load("//tensorflow/core/platform:rules_cc.bzl", "cc_library") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:license"], + default_visibility = ["//tensorflow/compiler/tf2xla:internal"], + licenses = ["notice"], +) + +XLA_OPS_DEPS = [ + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/memory", + "@com_google_absl//absl/synchronization", + "//tensorflow/compiler/jit:common", + "//tensorflow/compiler/jit:compilation_passes", + "//tensorflow/compiler/jit:flags", + "//tensorflow/compiler/jit:xla_activity_listener", + "//tensorflow/compiler/jit:xla_activity_proto_cc", + "//tensorflow/compiler/jit:device_compiler", + "//tensorflow/compiler/jit:variable_info", + "//tensorflow/compiler/jit:variable_info_util", + "//tensorflow/compiler/jit:xla_device_no_jit_rewrite_registration", + "//tensorflow/compiler/jit:xla_cluster_util", + "//tensorflow/compiler/jit:xla_host_recv_device_context", + "//tensorflow/compiler/jit:xla_host_send_device_context", + "//tensorflow/compiler/jit:xla_launch_util", + "//tensorflow/compiler/tf2xla:common", + "//tensorflow/compiler/tf2xla:tf2xla_util", + "//tensorflow/compiler/tf2xla:xla_compiler", + "//tensorflow/compiler/tf2xla:xla_helpers", + "//tensorflow/compiler/tf2xla:xla_op_registry", + "@xla//xla:executable_run_options", + "@xla//xla:status_macros", + "@xla//xla/client:client_library", + "@xla//xla/client:local_client", + "@xla//xla/service:compiler", + "@xla//xla/service/gpu:gpu_executable_run_options", + "//tensorflow/core:core_cpu_internal", + "//tensorflow/core:framework", + "//tensorflow/core:lib", + "//tensorflow/core:state_ops_op_lib", + "//tensorflow/core/platform:stream_executor_no_cuda", + "//tensorflow/core/profiler/lib:traceme", + "@xla//xla/stream_executor/integrations:tf_allocator_adapter", + "@com_google_absl//absl/types:optional", +] + +# Linked by tensorflow core, without registration of jit compilation passes. +cc_library( + name = "xla_ops_no_jit_rewrite_registration", + srcs = ["xla_ops.cc"], + hdrs = ["xla_ops.h"], + deps = XLA_OPS_DEPS + [ + "//tensorflow/compiler/jit:device_compilation_cache", + "//tensorflow/compiler/jit:device_compilation_profiler", + "//tensorflow/compiler/jit:pjrt_compile_util", + "//tensorflow/compiler/jit:tf_graph_to_hlo_compiler", + "//tensorflow/compiler/jit:tf_to_hlo_compiler", + "//tensorflow/compiler/jit:xla_compile_util", + "//tensorflow/core:framework_internal", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core/platform:refcount", + "@com_google_absl//absl/base:core_headers", + "@com_google_absl//absl/container:node_hash_map", + "@com_google_absl//absl/log", + "@com_google_absl//absl/log:check", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/strings:string_view", + "@com_google_absl//absl/types:span", + "@tsl//tsl/profiler/lib:traceme", + "@xla//xla/hlo/ir:hlo", + "@xla//xla/pjrt:pjrt_client", + "@xla//xla/service:executable", + "@xla//xla/stream_executor/host:host_platform_id", + "@xla//xla/tsl/concurrency:async_value", + "@xla//xla/tsl/protobuf:error_codes_proto_impl_cc", + ], + alwayslink = 1, +) + +cc_library( + name = "xla_ops", + hdrs = ["xla_ops.h"], + deps = XLA_OPS_DEPS + [ + ":xla_ops_no_jit_rewrite_registration", + "//tensorflow/compiler/jit:jit_compilation_passes", + "//tensorflow/core:protos_all_cc", + ], + alwayslink = 1, +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/kernels/xla_ops.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/kernels/xla_ops.cc new file mode 100644 index 0000000000000000000000000000000000000000..6ceaaf46165038b716e0214db20a6a257a8362ff --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/kernels/xla_ops.cc @@ -0,0 +1,1046 @@ +/* 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/compiler/jit/kernels/xla_ops.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "absl/base/const_init.h" +#include "absl/base/thread_annotations.h" +#include "absl/container/flat_hash_map.h" +#include "absl/container/node_hash_map.h" +#include "absl/log/check.h" +#include "absl/log/log.h" +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" +#include "absl/synchronization/mutex.h" +#include "absl/types/span.h" +#include "tensorflow/compiler/jit/device_compilation_profiler.h" +#include "tensorflow/compiler/jit/device_compiler.h" +#include "tensorflow/compiler/jit/encapsulate_subgraphs_pass.h" +#include "tensorflow/compiler/jit/flags.h" +#include "tensorflow/compiler/jit/pjrt_compile_util.h" +#include "tensorflow/compiler/jit/variable_info.h" +#include "tensorflow/compiler/jit/variable_info_util.h" +#include "tensorflow/compiler/jit/xla_activity.pb.h" +#include "tensorflow/compiler/jit/xla_activity_listener.h" +#include "tensorflow/compiler/jit/xla_compile_util.h" +#include "tensorflow/compiler/jit/xla_compiler_options_util.h" +#include "tensorflow/compiler/jit/xla_host_recv_device_context.h" +#include "tensorflow/compiler/jit/xla_host_send_device_context.h" +#include "tensorflow/compiler/jit/xla_launch_util.h" +#include "tensorflow/compiler/jit/xla_platform_info.h" +#include "tensorflow/compiler/tf2xla/tf2xla_util.h" +#include "tensorflow/compiler/tf2xla/xla_compiler.h" +#include "tensorflow/compiler/tf2xla/xla_helpers.h" +#include "tensorflow/compiler/tf2xla/xla_op_registry.h" +#include "xla/client/local_client.h" +#include "xla/executable_run_options.h" +#include "xla/hlo/ir/hlo_input_output_alias_config.h" +#include "xla/pjrt/pjrt_client.h" +#include "xla/service/executable.h" +#include "xla/service/gpu/gpu_executable_run_options.h" +#include "xla/stream_executor/host/host_platform_id.h" +#include "xla/tsl/concurrency/async_value_ref.h" +#include "xla/tsl/protobuf/error_codes.pb.h" +#include "tensorflow/core/framework/allocator.h" +#include "tensorflow/core/framework/control_flow.h" +#include "tensorflow/core/framework/device.h" +#include "tensorflow/core/framework/function.h" +#include "tensorflow/core/framework/node_def_util.h" +#include "tensorflow/core/framework/op_kernel.h" +#include "tensorflow/core/framework/op_requires.h" +#include "tensorflow/core/framework/rendezvous.h" +#include "tensorflow/core/framework/tensor.h" +#include "tensorflow/core/framework/tensor_shape.h" +#include "tensorflow/core/framework/types.h" +#include "tensorflow/core/framework/types.pb.h" +#include "tensorflow/core/lib/monitoring/counter.h" +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/platform/errors.h" +#include "tensorflow/core/platform/mutex.h" +#include "tensorflow/core/platform/refcount.h" +#include "tensorflow/core/platform/stream_executor_no_cuda.h" +#include "tensorflow/core/platform/threadpool.h" +#include "tensorflow/core/platform/tstring.h" +#include "tensorflow/core/platform/types.h" +#include "tensorflow/core/util/device_name_utils.h" +#include "tensorflow/core/util/stream_executor_util.h" +#include "tsl/platform/thread_annotations.h" +#include "tsl/profiler/lib/traceme.h" + +// OP_REQUIRES_OK_RETURN is the same as OP_REQUIRES_OK except that +// in error case, it returns RET instead of void. +#define OP_REQUIRES_OK_RETURN(CTX, RET, ...) \ + do { \ + ::tensorflow::Status _s(__VA_ARGS__); \ + if (!TF_PREDICT_TRUE(_s.ok())) { \ + (CTX)->CtxFailureWithWarning(__FILE__, __LINE__, _s); \ + return RET; \ + } \ + } while (0) + +namespace tensorflow { + +namespace { +using XlaDeviceCompiler = + DeviceCompiler; +using PjRtDeviceCompiler = + DeviceCompiler; + +auto* xla_launch_counter = monitoring::Counter<1>::New( + "/tensorflow/core/xla_launch_counter", + "The number of times a XlaLaunch is called.", "device"); + +// A closure describing how to run a compiled version of a TensorFlow function. +// +// It may seem unusual to stick the resource variable snapshots in this class. +// This is necessary: we need to use the snapshots observed by the compiler as +// the initial values for the resource variables (and cannot snapshot them again +// during execution) because otherwise we risk observing a different snapshot +// with shapes different from what we compiled for. +template +class ExecutableClosure { + public: + explicit ExecutableClosure( + ClientType* client, ExecutableType* executable, + const XlaCompiler::CompilationResult* compilation_result, + ResourceVarsSnapshot resource_var_snapshots, int num_constant_args) + : client_(client), + executable_(executable), + compilation_result_(compilation_result), + resource_var_snapshots_(std::move(resource_var_snapshots)), + num_constant_args_(num_constant_args) {} + + ExecutableClosure(ExecutableClosure&&) = default; + ExecutableClosure& operator=(ExecutableClosure&&) = default; + + ClientType* client() const { return client_; } + ExecutableType* executable() const { return executable_; } + const XlaCompiler::CompilationResult* compilation_result() const { + return compilation_result_; + } + const ResourceVarsSnapshot& resource_var_snapshots() const { + return resource_var_snapshots_; + } + int num_constant_args() const { return num_constant_args_; } + + private: + ClientType* client_; + ExecutableType* executable_; + const XlaCompiler::CompilationResult* compilation_result_; + ResourceVarsSnapshot resource_var_snapshots_; + int num_constant_args_; + + ExecutableClosure(const ExecutableClosure&) = delete; + void operator=(const ExecutableClosure&) = delete; +}; + +// This maintains a mapping from a globally unique ID to ExecutableClosure +// instances. +template +class ExecutableClosureStore { + public: + ExecutableClosureStore() : key_counter_(0) {} + + using KeyT = std::string; + + KeyT Produce(ExecutableClosure result) { + mutex_lock l(mutex_); + KeyT key = absl::StrCat(key_counter_++); + bool insert_successful = closures_.emplace(key, std::move(result)).second; + DCHECK(insert_successful); + (void)insert_successful; + return key; + } + + ExecutableClosure Consume(const KeyT& key) { + mutex_lock l(mutex_); + auto it = closures_.find(key); + DCHECK(it != closures_.end()); + ExecutableClosure value = std::move(it->second); + closures_.erase(it); + return value; + } + + static ExecutableClosureStore* Global() { + static ExecutableClosureStore* instance = new ExecutableClosureStore; + return instance; + } + + private: + mutex mutex_; + int64_t key_counter_ TF_GUARDED_BY(mutex_); + absl::flat_hash_map> + closures_ TF_GUARDED_BY(mutex_); + + ExecutableClosureStore(const ExecutableClosureStore&) = delete; + void operator=(const ExecutableClosureStore&) = delete; +}; + +using XlaExecutableClosure = + ExecutableClosure; +using XlaExecutableClosureStore = + ExecutableClosureStore; +using PjRtExecutableClosure = + ExecutableClosure; +using PjRtExecutableClosureStore = + ExecutableClosureStore; + +se::Stream* GetStream(OpKernelContext* ctx) { + return ctx->op_device_context() ? ctx->op_device_context()->stream() + : nullptr; +} + +XlaComputationLaunchContext GetLaunchContext( + const XlaPlatformInfo& platform_info, OpKernelContext* ctx, + xla::LocalClient* client, + stream_executor::DeviceAddressAllocator* allocator) { + se::Stream* stream = GetStream(ctx); + int device_ordinal = stream ? stream->parent()->device_ordinal() + : client->default_device_ordinal(); + XlaComputationLaunchContext launch_context( + client, allocator, device_ordinal, + /*allocate_xla_tensors=*/platform_info.is_on_xla_device(), + /*use_multiple_streams=*/platform_info.UseMultipleStreams()); + return launch_context; +} + +absl::Status GetTaskName(const absl::string_view device_name, + std::string* task_name) { + std::string ignored; + if (!DeviceNameUtils::SplitDeviceName(device_name, task_name, &ignored)) { + return absl::InvalidArgumentError( + absl::StrCat("Unable to parse device name: ", device_name)); + } + + return absl::OkStatus(); +} + +// Provide SendDeviceMemoryFunction for XLA host callbacks. This callback +// handles transferring from device to host. +xla::SendDeviceMemoryFunction GetSendDeviceMemoryFunction( + OpKernelContext* ctx, const std::string& program_key) { + return + [ctx, program_key]( + int64_t channel_id, se::Stream* stream, const xla::Shape& shape, + const stream_executor::DeviceAddressBase& device_memory_base, + const absl::flat_hash_map& frontend_attrs) + -> absl::StatusOr>> { + auto iter = frontend_attrs.find("_xla_host_transfer_rendezvous"); + + // Generate the Rendezvous key. + const std::string& rendezvous_key_base = + absl::StrCat(program_key, iter->second); + + const std::string& src_device = ctx->device()->name(); + std::string task_prefix; + TF_RETURN_IF_ERROR(GetTaskName(src_device, &task_prefix)); + const std::string dst_device = + absl::StrCat(task_prefix, "/device:CPU:0"); + const std::string& rendezvous_key = + Rendezvous::CreateKey(src_device, /*src_incarnation=*/1, dst_device, + rendezvous_key_base, FrameAndIter(0, 0)); + VLOG(2) << "Rendezvous Key for receiving at host: " << rendezvous_key; + + RendezvousInterface::ParsedKey parsed_key; + TF_RETURN_IF_ERROR(Rendezvous::ParseKey(rendezvous_key, &parsed_key)); + + TF_ASSIGN_OR_RETURN(auto event, stream->parent()->CreateEvent()); + tsl::AsyncValueRef> done_event = + tsl::MakeConstructedAsyncValueRef>( + std::move(event)); + + Rendezvous::Args args; + // Rendezvous::Args owns the device context pointer. + args.device_context = new XlaHostRecvDeviceContext( + stream, device_memory_base, shape, done_event); + + Tensor host_tensor; + TF_RETURN_IF_ERROR( + ctx->rendezvous()->Send(parsed_key, args, host_tensor, false)); + + return std::move(done_event); + }; +} + +// Provide RecvDeviceMemoryFunction for XLA host callbacks. This callback +// handles transferring from host to device. +xla::RecvDeviceMemoryFunction GetRecvDeviceMemoryFunction( + OpKernelContext* ctx, const std::string& program_key) { + return + [ctx, program_key]( + int64_t channel_id, se::Stream* stream, const xla::Shape& shape, + stream_executor::DeviceAddressBase* device_memory_base, + const absl::flat_hash_map& frontend_attrs) + -> absl::StatusOr>> { + auto iter = frontend_attrs.find("_xla_host_transfer_rendezvous"); + + // Generate the Rendezvous key. + const std::string& rendezvous_key_base = + absl::StrCat(program_key, iter->second); + + const std::string& dst_device = ctx->device()->name(); + std::string task_prefix; + TF_RETURN_IF_ERROR(GetTaskName(dst_device, &task_prefix)); + const std::string src_device = + absl::StrCat(task_prefix, "/device:CPU:0"); + const std::string& rendezvous_key = + Rendezvous::CreateKey(src_device, /*src_incarnation=*/1, dst_device, + rendezvous_key_base, FrameAndIter(0, 0)); + VLOG(2) << "Rendezvous Key for sending from host: " << rendezvous_key; + + RendezvousInterface::ParsedKey parsed_key; + TF_RETURN_IF_ERROR(Rendezvous::ParseKey(rendezvous_key, &parsed_key)); + + TF_ASSIGN_OR_RETURN(auto event, stream->parent()->CreateEvent()); + tsl::AsyncValueRef> done_event = + tsl::MakeConstructedAsyncValueRef>( + std::move(event)); + + Rendezvous::Args args; + // Rendezvous::Args owns the device context pointer. + args.device_context = new XlaHostSendDeviceContext( + stream, device_memory_base, shape, done_event); + + Tensor device_tensor; + bool is_dead; + TF_RETURN_IF_ERROR(ctx->rendezvous()->Recv( + parsed_key, args, &device_tensor, /*is_dead=*/&is_dead)); + + return std::move(done_event); + }; +} + +absl::StatusOr RunExecutable( + const XlaPlatformInfo& platform_info, + const XlaComputationLaunchContext& launch_context, + std::vector execution_inputs, + xla::ExecutableRunOptions run_options, xla::LocalExecutable* executable, + OpKernelContext* ctx, stream_executor::DeviceAddressAllocator* allocator) { + VLOG(2) << "Executing Xla Computation."; + Env* env = Env::Default(); + auto start_time = env->NowMicros(); + + se::Stream* stream = GetStream(ctx); + run_options.set_stream(GetStream(ctx)); + run_options.set_allocator(allocator); + run_options.set_intra_op_thread_pool(&ctx->eigen_cpu_device()); + run_options.set_rng_seed(GetXLARandomSeed()); + + absl::StatusOr execution_output; + bool run_synchronous = + !stream || platform_info.platform_id() == se::host::kHostPlatformId; + if (run_synchronous) { + execution_output = + executable->Run(std::move(execution_inputs), run_options); + } else { + execution_output = + executable->RunAsync(std::move(execution_inputs), run_options); + } + + auto elapsed = env->NowMicros() - start_time; + VLOG(2) << "Elapsed time for Xla Executable Run: " << elapsed << "us"; + return execution_output; +} + +absl::StatusOr< + std::pair, ResourceVarsSnapshot>> +GetXlaCompilerArgsAndSnapshotVariables( + absl::Span variable_indices, + absl::Span must_be_constant_idxs, + absl::Span inputs, OpKernelContext* ctx) { + std::pair, ResourceVarsSnapshot> result; + + std::vector variable_infos; + TF_RETURN_IF_ERROR( + GetVariableInfosFromInputs(ctx->resource_manager(), ctx->device(), inputs, + variable_indices, &variable_infos)); + TF_RETURN_IF_ERROR(LockVariables(absl::MakeSpan(variable_infos))); + + TF_RETURN_IF_ERROR(SnapshotResourceVariables(ctx, variable_indices, + variable_infos, &result.second)); + + TF_ASSIGN_OR_RETURN(result.first, + XlaComputationLaunchContext::BuildXlaCompilerArguments( + must_be_constant_idxs, inputs, variable_infos, + static_cast(ctx->device()))); + return result; +} + +absl::Status CompileToLocalExecutable( + OpKernelContext* ctx, const NameAttrList& function, bool has_ref_vars, + const XlaPlatformInfo& platform_info, + const std::vector& args, + DeviceCompileMode compile_mode, bool may_alias_resource_update, + xla::LocalClient** client, + const XlaCompiler::CompilationResult** compilation_result, + xla::LocalExecutable** executable) { + // We store information about the JIT-compiled XLA computation + // in the ResourceMgr. + ResourceMgr* rm = ctx->resource_manager(); + if (!rm) { + return absl::InternalError("No resource manager."); + } + + TF_ASSIGN_OR_RETURN(DeviceType compilation_device_type, + GetCompilationDeviceType(platform_info.device_type())); + + XlaDeviceCompiler* xla_device_compiler; + TF_RETURN_IF_ERROR(rm->LookupOrCreate( + rm->default_container(), "xla_device_compiler", &xla_device_compiler, + [&](XlaDeviceCompiler** xla_device_compiler) { + return BuildXlaDeviceCompiler(ctx->device(), ctx->function_library(), + platform_info, compilation_device_type, + xla_device_compiler); + })); + DeviceCompilationProfiler* profiler; + TF_RETURN_IF_ERROR(rm->LookupOrCreate( + rm->default_container(), "device_compilation_profiler", &profiler, + [](DeviceCompilationProfiler** profiler) { + *profiler = new DeviceCompilationProfiler(); + return absl::OkStatus(); + })); + // Hold the reference to the XLA device compiler and profiler during + // evaluation. (We could probably free them sooner because the ResourceMgr + // will retain references, but this is more obviously correct.) + core::ScopedUnref xla_device_compiler_ref(xla_device_compiler); + core::ScopedUnref profiler_ref(profiler); + + *client = static_cast(xla_device_compiler->client()); + + XlaCompiler::Options options = GenerateCompilerOptions( + *xla_device_compiler, *ctx->function_library(), ctx->device(), + GetStream(ctx), platform_info, has_ref_vars); + + XlaCompiler::CompileOptions compile_options = + GenerateCompileOptions(has_ref_vars, may_alias_resource_update); + + return xla_device_compiler->CompileIfNeeded( + options, function, args, compile_options, compile_mode, profiler, + compilation_result, executable); +} + +absl::Status GetUpdatedVariables( + const OpKernelContext* ctx, absl::Span inputs, + absl::Span variable_indices, + const XlaCompiler::CompilationResult& compilation_result, + std::vector* variable_infos) { + std::set variables_updated; + for (const auto& resource_update : compilation_result.resource_updates) { + if (resource_update.modified) { + variables_updated.insert(resource_update.input_index); + } + } + return GetVariableInfosFromInputs(ctx->resource_manager(), ctx->device(), + inputs, variable_indices, + &variables_updated, variable_infos); +} + +// Get-or-create thread pool for a given collective. +static thread::ThreadPool* GetOrCreateThreadPoolForCollective( + const XlaCompilationResult::CollectiveInfo& collective_info) { + static absl::Mutex m(absl::kConstInit); + static auto& thread_pool_cache ABSL_GUARDED_BY(m) = + *new absl::node_hash_map(); + absl::MutexLock l(m); + auto it = thread_pool_cache.find(collective_info); + if (it == thread_pool_cache.end()) { + // Create & cache thread pool. + auto inserted_it = thread_pool_cache.emplace( + std::piecewise_construct, std::forward_as_tuple(collective_info), + std::forward_as_tuple(Env::Default(), "xla_collective_thread_pool", + collective_info.group_size)); + return &inserted_it.first->second; + } + return &it->second; +} + +void RunInThreadPoolIfCollectivesPresent( + const XlaCompiler::CompilationResult& compilation_result, + std::function execution_fn) { + // If we are using collectives, we need to run in a separate threadpool. + if (compilation_result.collective_info.has_value()) { + GetOrCreateThreadPoolForCollective(*compilation_result.collective_info) + ->Schedule(execution_fn); + } else { + // Otherwise, just run normally: we merely "pretend" to be asynchronous. + execution_fn(); + } +} + +} // namespace + +XlaLocalLaunchBase::XlaLocalLaunchBase(OpKernelConstruction* ctx, + const std::vector& constants, + const std::vector& resources, + const NameAttrList& function, + bool has_ref_vars) + : AsyncOpKernel(ctx), + constants_(constants), + resources_(resources), + function_(function), + platform_info_(XlaPlatformInfoFromDevice(ctx->device())), + has_ref_vars_(has_ref_vars) {} + +void XlaLocalLaunchBase::ComputeAsync(OpKernelContext* ctx, DoneCallback done) { + VLOG(1) << "XlaLocalLaunchOpBase::Compute " + << Canonicalize(function_.name(), AttrSlice(&function_.attr())); + xla_launch_counter->GetCell(platform_info_.device_type().type_string()) + ->IncrementBy(1); + + std::vector inputs = InputsFromContext(ctx); + std::vector xla_compiler_args; + const XlaCompiler::CompilationResult* compilation_result; + + xla::LocalClient* client; // Not owned. + xla::LocalExecutable* executable; // Not owned. + + xla::PjRtClient* pjrt_client; // Not owned. + xla::PjRtLoadedExecutable* pjrt_executable; // Not owned. + + // Note that here we assume the shape of the variables don't change between + // compilation and execution. The locks on the variables are released before + // compilation so that we can achieve parallel compilation of different batch + // sizes during warm-up. + { + // Creating a scope so that the locks on the variables are released when + // variable_infos goes out of scope. + std::vector variable_infos; + std::set variables_updated; + // Here we only need to reader-lock the variables, so we pass an empty + // variables_updated set here. + absl::Status status = GetVariableInfosFromInputs( + ctx->resource_manager(), ctx->device(), inputs, resources_, + &variables_updated, &variable_infos); + OP_REQUIRES_OK_ASYNC(ctx, status, done); + status = LockVariables(absl::MakeSpan(variable_infos)); + OP_REQUIRES_OK_ASYNC(ctx, status, done); + auto status_or_xla_compiler_args = + XlaComputationLaunchContext::BuildXlaCompilerArguments( + constants_, inputs, variable_infos, + static_cast(ctx->device())); + OP_REQUIRES_OK_ASYNC(ctx, status_or_xla_compiler_args.status(), done); + xla_compiler_args = std::move(status_or_xla_compiler_args.value()); + } + + bool use_pjrt = GetXlaOpsCommonFlags() + ->tf_xla_use_device_api.IsEnabledInXlaLaunchForDevice( + platform_info_.device_type()); + if (use_pjrt) { + VLOG(2) << "Compiling using PJRT"; + absl::Status status = CompileToPjRtLoadedExecutable( + *ctx, platform_info_, function_, xla_compiler_args, + DeviceCompileMode::kStrict, has_ref_vars_, + /*may_alias_resource_update=*/true, &compilation_result, &pjrt_client, + &pjrt_executable); + OP_REQUIRES_OK_ASYNC(ctx, status, done); + + VLOG(2) << "Compiled using PJRT: " << status; + VLOG(2) << "pjrt_executable != nullptr: " << (pjrt_executable != nullptr); + VLOG(2) << "compilation_result != nullptr: " + << (compilation_result != nullptr); + VLOG(2) << "Executing using PJRT."; + + auto run_pjrt_cluster = [ctx, pjrt_client, pjrt_executable, + compilation_result, done, inputs, + resources = resources_]() { + // Separate scope so that VariableInfo locks are released before done() is + // called. + { + std::vector variable_infos; + OP_REQUIRES_OK_ASYNC( + ctx, + GetUpdatedVariables(ctx, inputs, resources, *compilation_result, + &variable_infos), + done); + OP_REQUIRES_OK_ASYNC(ctx, LockVariables(absl::MakeSpan(variable_infos)), + done); + OP_REQUIRES_OK_ASYNC( + ctx, + RunPjRtExecutable(inputs, variable_infos, *compilation_result, + pjrt_client, pjrt_executable, ctx), + done); + } + VLOG(2) << "Done executing with PJRT."; + done(); + }; + + RunInThreadPoolIfCollectivesPresent(*compilation_result, run_pjrt_cluster); + return; + } + + absl::Status status = CompileToLocalExecutable( + ctx, function_, /*has_ref_vars=*/has_ref_vars_, platform_info_, + xla_compiler_args, DeviceCompileMode::kStrict, + /*may_alias_resource_update=*/true, &client, &compilation_result, + &executable); + OP_REQUIRES_OK_ASYNC(ctx, status, done); + + // Continuation of the execution, may be run in a different thread. + auto run_xla_cluster = [ctx, client, executable, compilation_result, done, + inputs, resources = resources_]() { + // Separate scope so that VariableInfo locks are released before done is + // called. + { + auto platform_info = XlaPlatformInfoFromDevice(ctx->device()); + std::vector variable_infos; + OP_REQUIRES_OK_ASYNC( + ctx, + GetUpdatedVariables(ctx, inputs, resources, *compilation_result, + &variable_infos), + done); + OP_REQUIRES_OK_ASYNC(ctx, LockVariables(absl::MakeSpan(variable_infos)), + done); + absl::flat_hash_map resource_var_ptrs; + for (int i = 0; i < resources.size(); i++) { + resource_var_ptrs[resources[i]] = variable_infos[i].var()->tensor(); + } + + std::shared_ptr allocator = + GetAllocator(ctx->device(), GetStream(ctx), platform_info); + XlaComputationLaunchContext launch_context = + GetLaunchContext(platform_info, ctx, client, allocator.get()); + + const xla::HloInputOutputAliasConfig& input_output_alias = + executable->executable()->module().input_output_alias_config(); + absl::StatusOr> execution_inputs = + launch_context.PopulateInputs( + ctx, compilation_result, resource_var_ptrs, + /*missing_ctx_input_prefix=*/0, input_output_alias); + OP_REQUIRES_OK_ASYNC(ctx, execution_inputs.status(), done); + + xla::gpu::GpuExecutableRunOptions gpu_options; + xla::DeviceAssignment device_assignment; + xla::ExecutableRunOptions run_options; + if (compilation_result->collective_info.has_value()) { + OP_REQUIRES_OK_ASYNC(ctx, + ResolveDeviceAssignment( + ctx, *compilation_result->collective_info, + run_options, device_assignment, gpu_options), + done); + } + + // Hardcode run id to always be zero: TF distributed strategy + // differentiates between subsequent runs using dependency edges. This + // is safe, as only TF dist-strat can produce distributed ops, and we + // can rely on TF dist-strat invariants. + xla::RunId run_id(0); + run_options.set_run_id(run_id); + + absl::StatusOr execution_output = RunExecutable( + platform_info, launch_context, std::move(*execution_inputs), + run_options, executable, ctx, allocator.get()); + OP_REQUIRES_ASYNC(ctx, execution_output.ok(), execution_output.status(), + done); + + OP_REQUIRES_OK_ASYNC( + ctx, + launch_context.PopulateOutputs( + ctx, compilation_result, execution_output->ConsumeResult(), + /*missing_ctx_input_prefix=*/0, absl::MakeSpan(variable_infos), + input_output_alias, resource_var_ptrs), + done); + VLOG(1) << "Done"; + } + done(); + }; + + RunInThreadPoolIfCollectivesPresent(*compilation_result, run_xla_cluster); +} + +namespace { +// Helper static functions to construct parameters for +// XlaLocalLaunchBase constructor from OpKernelConstruction. +std::vector ConstantsVector(OpKernelConstruction* ctx) { + DataTypeVector constant_types; + OP_REQUIRES_OK_RETURN(ctx, std::vector(), + ctx->GetAttr("Tconstants", &constant_types)); + std::vector constants(constant_types.size()); + std::iota(constants.begin(), constants.end(), 0); + return constants; +} + +std::vector ResourcesVector(OpKernelConstruction* ctx) { + DataTypeVector constant_types; + OP_REQUIRES_OK_RETURN(ctx, std::vector(), + ctx->GetAttr("Tconstants", &constant_types)); + + DataTypeVector arg_types; + OP_REQUIRES_OK_RETURN(ctx, std::vector(), + ctx->GetAttr("Targs", &arg_types)); + + int num_resources; + OP_REQUIRES_OK_RETURN(ctx, std::vector(), + ctx->GetAttr("Nresources", &num_resources)); + + std::vector resources(num_resources); + std::iota(resources.begin(), resources.end(), + constant_types.size() + arg_types.size()); + return resources; +} + +NameAttrList FunctionAttr(OpKernelConstruction* ctx) { + const NameAttrList* func; + OP_REQUIRES_OK_RETURN(ctx, NameAttrList(), ctx->GetAttr("function", &func)); + return *func; +} + +std::vector VectorAttr(OpKernelConstruction* ctx, + absl::string_view attr_name) { + std::vector vec; + OP_REQUIRES_OK_RETURN(ctx, std::vector(), ctx->GetAttr(attr_name, &vec)); + return vec; +} + +bool MustCompileAttr(OpKernelConstruction* ctx) { + bool must_compile; + OP_REQUIRES_OK_RETURN(ctx, false, + ctx->GetAttr("must_compile", &must_compile)); + return must_compile; +} + +bool HasRefVars(OpKernelConstruction* ctx) { + bool has_ref_vars; + OP_REQUIRES_OK_RETURN(ctx, false, + ctx->GetAttr(kXlaHasReferenceVarsAttr, &has_ref_vars)); + return has_ref_vars; +} + +class XlaLaunchV2Op : public XlaLocalLaunchBase { + public: + explicit XlaLaunchV2Op(OpKernelConstruction* ctx) + : XlaLocalLaunchBase(ctx, VectorAttr(ctx, "constants"), + VectorAttr(ctx, "resources"), FunctionAttr(ctx), + /*has_ref_vars=*/true) {} +}; + +} // namespace + +XlaLocalLaunchOp::XlaLocalLaunchOp(OpKernelConstruction* ctx) + : XlaLocalLaunchBase(ctx, ConstantsVector(ctx), ResourcesVector(ctx), + FunctionAttr(ctx), /*has_ref_vars=*/true) {} + +XlaLocalLaunchOp::~XlaLocalLaunchOp() { + VLOG(1) << "XlaLocalLaunchOp destroyed"; +} + +XlaCompileOp::XlaCompileOp(OpKernelConstruction* ctx) + : OpKernel(ctx), + constants_(ConstantsVector(ctx)), + resources_(ResourcesVector(ctx)), + function_(FunctionAttr(ctx)), + platform_info_(XlaPlatformInfoFromDevice(ctx->device())), + must_compile_(MustCompileAttr(ctx)), + has_ref_vars_(HasRefVars(ctx)) {} + +void XlaCompileOp::Compute(OpKernelContext* ctx) { + VLOG(3) << "XlaCompileOp " << def().name() + << (must_compile_ ? "(must-compile)" : ""); + const XlaCompiler::CompilationResult* kernel = nullptr; + xla::LocalClient* client = nullptr; + xla::LocalExecutable* executable = nullptr; + xla::PjRtClient* pjrt_client = nullptr; + xla::PjRtLoadedExecutable* pjrt_executable = nullptr; + ResourceVarsSnapshot variables_snapshot; + + std::vector inputs = InputsFromContext(ctx); + bool cannot_compile_cluster; + { + mutex_lock guard(cannot_compile_cluster_mu_); + cannot_compile_cluster = cannot_compile_cluster_; + } + DeviceCompileMode compile_mode = [&] { + if (must_compile_) { + return DeviceCompileMode::kStrict; + } + return GetXlaOpsCommonFlags()->tf_xla_async_compilation + ? DeviceCompileMode::kAsync + : DeviceCompileMode::kLazy; + }(); + + bool use_pjrt = + GetXlaOpsCommonFlags() + ->tf_xla_use_device_api.IsEnabledInXlaCompileAndRunForDevice( + platform_info_.device_type()); + + if (GetXlaOpsCommonFlags()->tf_xla_always_defer_compilation || + cannot_compile_cluster) { + executable = nullptr; + } else { + auto args_and_variables_snapshot = GetXlaCompilerArgsAndSnapshotVariables( + resources_, constants_, inputs, ctx); + OP_REQUIRES_OK(ctx, args_and_variables_snapshot.status()); + const std::vector& args = + args_and_variables_snapshot->first; + variables_snapshot = std::move(args_and_variables_snapshot->second); + + // Do not alias resource updates as locking variables in XlaCompile and + // unlocking them in XlaRun may lead to deadlocks. + absl::Status status; + if (use_pjrt) { + VLOG(2) << "Using PJRT for compilation. Function name: " + << function_.name(); + status = CompileToPjRtLoadedExecutable( + *ctx, platform_info_, function_, args, compile_mode, has_ref_vars_, + /*may_alias_resource_update=*/false, &kernel, &pjrt_client, + &pjrt_executable); + } else { + status = CompileToLocalExecutable( + ctx, function_, has_ref_vars_, platform_info_, args, compile_mode, + /*may_alias_resource_update=*/false, &client, &kernel, &executable); + } + if (compile_mode != DeviceCompileMode::kLazy || + status.code() != error::UNIMPLEMENTED) { + OP_REQUIRES_OK(ctx, status); + } + + if (status.code() == error::UNIMPLEMENTED) { + LOG(WARNING) << "Compilation failed:" << status + << ". Falling back to TF function call."; + + BroadcastOptimizationRemark( + XlaOptimizationRemark::UNIMPLEMENTED_OPERATION, status.ToString()) + .IgnoreError(); + executable = nullptr; + pjrt_executable = nullptr; + mutex_lock guard(cannot_compile_cluster_mu_); + cannot_compile_cluster_ = true; + } + } + + AllocatorAttributes host_alloc_attrs; + host_alloc_attrs.set_gpu_compatible(true); + host_alloc_attrs.set_on_host(true); + Allocator* cpu_allocator = ctx->device()->GetAllocator(host_alloc_attrs); + + // Async compilation returns nullptr executable without an error. + if (!executable && !pjrt_executable) { + DCHECK(!must_compile_); + Tensor compilation_key(cpu_allocator, DT_STRING, TensorShape({})); + Tensor compilation_successful(cpu_allocator, DT_BOOL, TensorShape({})); + compilation_successful.scalar()() = false; + ctx->set_output(0, compilation_key); + ctx->set_output(1, compilation_successful); + return; + } + + // Each execution of an XlaCompile op creates a new ExecutableClosure, even + // if it didn't have to compile the cluster because of a compilation-cache + // hit. This is because we at least need new snapshots of the resource + // variables. + Tensor compilation_key(cpu_allocator, DT_STRING, TensorShape({})); + if (use_pjrt) { + PjRtExecutableClosureStore::KeyT key = + PjRtExecutableClosureStore::Global()->Produce(PjRtExecutableClosure( + pjrt_client, pjrt_executable, kernel, std::move(variables_snapshot), + constants_.size())); + compilation_key.flat()(0) = key; + VLOG(2) << "Compiled with PJRT. compilation_key: " << key; + } else { + XlaExecutableClosureStore::KeyT key = + XlaExecutableClosureStore::Global()->Produce(XlaExecutableClosure( + client, executable, kernel, std::move(variables_snapshot), + constants_.size())); + compilation_key.flat()(0) = key; + VLOG(2) << "Compiled with XLA. compilation_key: " << key; + } + + Tensor compilation_successful(cpu_allocator, DT_BOOL, TensorShape({})); + compilation_successful.flat()(0) = true; + + ctx->set_output(0, compilation_key); + ctx->set_output(1, compilation_successful); +} + +XlaRunOp::XlaRunOp(OpKernelConstruction* ctx) + : OpKernel(ctx), platform_info_(XlaPlatformInfoFromDevice(ctx->device())) {} + +void XlaRunOp::Compute(OpKernelContext* ctx) { + VLOG(3) << "XlaRunOp " << def().name(); + Tensor key_tensor = ctx->input(ctx->num_inputs() - 1); + + bool use_pjrt = + GetXlaOpsCommonFlags() + ->tf_xla_use_device_api.IsEnabledInXlaCompileAndRunForDevice( + platform_info_.device_type()); + + if (use_pjrt) { + const PjRtExecutableClosureStore::KeyT& key = key_tensor.flat()(0); + PjRtExecutableClosure closure = + PjRtExecutableClosureStore::Global()->Consume(key); + + // Fetch inputs from the OpKernelContext. Inputs are the same as the ones + // for XlaCompile, except that the must-be-constant inputs that appear in + // the beginning are stripped off and the closure key is appended as the + // last input. So the inputs look like: input tensors, resource variables, + // closure key tensor. + std::vector inputs = InputsFromContext(ctx); + absl::flat_hash_map variable_snapshots; + for (const auto& [variable_index, variable_tensor] : + closure.resource_var_snapshots()) { + variable_snapshots.emplace(variable_index, variable_tensor.has_value() + ? &variable_tensor.value() + : nullptr); + } + + { + absl::StatusOr> updated_variables = + GatherVariableInfo(ctx, *closure.compilation_result(), + closure.num_constant_args()); + OP_REQUIRES_OK(ctx, updated_variables.status()); + OP_REQUIRES_OK(ctx, LockVariables(absl::MakeSpan(*updated_variables))); + OP_REQUIRES_OK( + ctx, RunPjRtExecutable(closure.num_constant_args(), inputs, + variable_snapshots, *updated_variables, + *closure.compilation_result(), + closure.client(), closure.executable(), ctx)); + } + + OP_REQUIRES_OK(ctx, absl::OkStatus()); + return; + } + + const XlaExecutableClosureStore::KeyT& key = key_tensor.flat()(0); + + XlaExecutableClosure closure = + XlaExecutableClosureStore::Global()->Consume(key); + std::shared_ptr allocator = + GetAllocator(ctx->device(), GetStream(ctx), platform_info_); + XlaComputationLaunchContext launch_context = + GetLaunchContext(platform_info_, ctx, closure.client(), allocator.get()); + + // We're missing the must-be-constant inputs, tell `PopulateInputs` + // about this. We don't actually need these inputs because they've + // already been baked into the compiled kernel. + const xla::HloInputOutputAliasConfig& input_output_alias = + closure.executable()->executable()->module().input_output_alias_config(); + absl::StatusOr> execution_inputs; + absl::flat_hash_map snapshot_ptrs; + { + tsl::profiler::TraceMe hlo_module_activity( + [&] { + return absl::StrCat( + "Populate Inputs (", + closure.compilation_result()->xla_input_shapes.size(), ")"); + }, + tsl::profiler::TraceMeLevel::kInfo); + + for (const auto& [variable_index, variable_tensor] : + closure.resource_var_snapshots()) { + snapshot_ptrs.emplace(variable_index, variable_tensor.has_value() + ? &variable_tensor.value() + : nullptr); + } + execution_inputs = launch_context.PopulateInputs( + ctx, closure.compilation_result(), snapshot_ptrs, + /*missing_ctx_input_prefix=*/closure.num_constant_args(), + input_output_alias); + OP_REQUIRES_OK(ctx, execution_inputs.status()); + } + + xla::ExecutableRunOptions run_options; + + // Host callbacks used for HLO send/recv. + xla::SendDeviceMemoryFunction send_function = + GetSendDeviceMemoryFunction(ctx, key); + run_options.set_send_device_memory_function(&send_function); + xla::RecvDeviceMemoryFunction recv_function = + GetRecvDeviceMemoryFunction(ctx, key); + run_options.set_recv_device_memory_function(&recv_function); + + absl::StatusOr execution_output = RunExecutable( + platform_info_, launch_context, std::move(*execution_inputs), run_options, + closure.executable(), ctx, allocator.get()); + OP_REQUIRES(ctx, execution_output.ok(), execution_output.status()); + + tsl::profiler::TraceMe hlo_module_activity( + [&] { + return absl::StrCat("Populate Outputs (", ctx->num_outputs(), ")"); + }, + tsl::profiler::TraceMeLevel::kInfo); + + absl::StatusOr> variable_infos = GatherVariableInfo( + ctx, *closure.compilation_result(), closure.num_constant_args()); + OP_REQUIRES_OK(ctx, variable_infos.status()); + OP_REQUIRES_OK(ctx, LockVariables(absl::MakeSpan(*variable_infos))); + OP_REQUIRES_OK( + ctx, + launch_context.PopulateOutputs( + ctx, closure.compilation_result(), execution_output->ConsumeResult(), + /*missing_ctx_input_prefix=*/closure.num_constant_args(), + absl::MakeSpan(*variable_infos), input_output_alias, snapshot_ptrs)); +} + +XlaMergeOp::XlaMergeOp(OpKernelConstruction* ctx) : OpKernel(ctx) {} + +void XlaMergeOp::Compute(OpKernelContext* ctx) { + VLOG(3) << "XlaMergeOp " << def().name(); + int i = 0; + if (ctx->has_input(i) || ctx->has_input(++i)) { + ctx->set_output(0, ctx->input(i)); + } +} + +REGISTER_KERNEL_BUILDER(Name("XlaLaunch").Device(DEVICE_CPU), XlaLocalLaunchOp); + +REGISTER_KERNEL_BUILDER(Name("XlaLaunchV2").Device(DEVICE_CPU), XlaLaunchV2Op); + +REGISTER_KERNEL_BUILDER(Name("XlaLaunch") + .Device(DEVICE_GPU) + .HostMemory("constants") + .HostMemory("resources"), + XlaLocalLaunchOp); + +REGISTER_KERNEL_BUILDER(Name("_XlaCompile").Device(DEVICE_CPU), XlaCompileOp); +REGISTER_KERNEL_BUILDER(Name("_XlaCompile") + .Device(DEVICE_GPU) + .HostMemory("constants") + .HostMemory("key") + .HostMemory("compilation_successful") + .HostMemory("resources"), + XlaCompileOp); + +REGISTER_KERNEL_BUILDER(Name("_XlaCompile") + .Device(DEVICE_DEFAULT) + .HostMemory("constants") + .HostMemory("key") + .HostMemory("compilation_successful") + .HostMemory("resources"), + XlaCompileOp); + +REGISTER_KERNEL_BUILDER(Name("_XlaRun").Device(DEVICE_CPU), XlaRunOp); +REGISTER_KERNEL_BUILDER(Name("_XlaRun").Device(DEVICE_GPU).HostMemory("key"), + XlaRunOp); +REGISTER_KERNEL_BUILDER( + Name("_XlaRun").Device(DEVICE_DEFAULT).HostMemory("key"), XlaRunOp); + +REGISTER_KERNEL_BUILDER(Name("_XlaMerge").Device(DEVICE_CPU), XlaMergeOp); +REGISTER_KERNEL_BUILDER(Name("_XlaMerge").Device(DEVICE_GPU), XlaMergeOp); +REGISTER_KERNEL_BUILDER(Name("_XlaMerge").Device(DEVICE_DEFAULT), XlaMergeOp); + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/kernels/xla_ops.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/kernels/xla_ops.h new file mode 100644 index 0000000000000000000000000000000000000000..7e97a7ebaa325429de3abe2c9d616650df1e9055 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/kernels/xla_ops.h @@ -0,0 +1,142 @@ +/* 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_KERNELS_XLA_OPS_H_ +#define TENSORFLOW_COMPILER_JIT_KERNELS_XLA_OPS_H_ + +#include +#include + +#include "tensorflow/compiler/jit/device_compiler.h" +#include "tensorflow/compiler/jit/xla_device.h" +#include "tensorflow/compiler/jit/xla_launch_util.h" +#include "tensorflow/compiler/jit/xla_platform_info.h" +#include "xla/stream_executor/integrations/tf_allocator_adapter.h" +#include "tensorflow/core/framework/allocator.h" +#include "tensorflow/core/framework/attr_value.pb.h" +#include "tensorflow/core/framework/op.h" +#include "tensorflow/core/framework/op_kernel.h" +#include "tensorflow/core/framework/tensor.h" +#include "tensorflow/core/platform/macros.h" +#include "tensorflow/core/util/stream_executor_util.h" + +namespace tensorflow { + + +// XlaLocalLaunchBase is almost the same as XlaLocalLaunchOp. +// The only difference is that it does not require arguments to follow +// the "constants, then regular args, then resources" order. +// It takes vectors of constant and resource arguments explicitly. +// It does not have corresponding OpDef because it is never present +// in the GraphDef. +// Currently, it is used by eager runtime. FunctionLibraryRuntime creates +// this kernel when asked to create a kernel for an XLA-compiled function. +// +// `has_ref_vars`: whether the input computation can have reference variables. +// TODO(cheshire): instead derive this information from the input graph. +class XlaLocalLaunchBase : public AsyncOpKernel { + public: + XlaLocalLaunchBase(OpKernelConstruction* ctx, + const std::vector& constants, + const std::vector& resources, + const NameAttrList& function, bool has_ref_vars); + XlaLocalLaunchBase(const XlaLocalLaunchBase&) = delete; + XlaLocalLaunchBase& operator=(const XlaLocalLaunchBase&) = delete; + ~XlaLocalLaunchBase() override = default; + + void ComputeAsync(OpKernelContext* ctx, DoneCallback done) override; + + protected: + // Indexes of compile-time constant inputs + const std::vector constants_; + // Indexes of resource inputs + const std::vector resources_; + + const NameAttrList function_; + const XlaPlatformInfo platform_info_; + + bool has_ref_vars_; +}; + +// XlaLocalLaunchOp is used to replace a region of the TensorFlow graph +// which will be compiled and executed using XLA. The XlaLocalLaunchOp is +// responsible for handling interactions with the TensorFlow executor. +// Once all inputs are present, and their shapes are known, the op can +// use a 'DeviceCompiler' to compile and execute code which is specific +// to the shapes of input Tensors. +// XlaLocalLaunchOp uses xla::LocalClient::Compile() and +// xla::LocalExecutable::Run(), and passes arguments into/out of XLA in device +// memory. +class XlaLocalLaunchOp : public XlaLocalLaunchBase { + public: + explicit XlaLocalLaunchOp(OpKernelConstruction* ctx); + ~XlaLocalLaunchOp() override; + + private: + XlaLocalLaunchOp(const XlaLocalLaunchOp&) = delete; + void operator=(const XlaLocalLaunchOp&) = delete; +}; + +class XlaCompileOp : public OpKernel { + public: + explicit XlaCompileOp(OpKernelConstruction* ctx); + + void Compute(OpKernelContext* ctx) override; + + private: + // Indexes of compile-time constant inputs + const std::vector constants_; + // Indexes of resource inputs + const std::vector resources_; + + const NameAttrList function_; + + XlaPlatformInfo platform_info_; + + const bool must_compile_; + + // Whether the graph has TF reference variables. + const bool has_ref_vars_; + + // cannot_compile_cluster_ is set to true if XLA returns an Unimplemented + // error when compiling the cluster this _XlaCompile is supposed to compile. + // If `cannot_compile_cluster_` is true then we avoid compiling this cluster + // on any future calls to _XlaCompile. + bool cannot_compile_cluster_ TF_GUARDED_BY(cannot_compile_cluster_mu_) = + false; + + mutex cannot_compile_cluster_mu_; +}; + +class XlaRunOp : public OpKernel { + public: + explicit XlaRunOp(OpKernelConstruction* ctx); + + void Compute(OpKernelContext* ctx) override; + + private: + const XlaPlatformInfo platform_info_; +}; + +class XlaMergeOp : public OpKernel { + public: + explicit XlaMergeOp(OpKernelConstruction* ctx); + + void Compute(OpKernelContext* ctx) override; +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_KERNELS_XLA_OPS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/mark_for_compilation_pass.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/mark_for_compilation_pass.cc new file mode 100644 index 0000000000000000000000000000000000000000..9747cb6f3df08fd8150c75538f361396bbfd3d81 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/mark_for_compilation_pass.cc @@ -0,0 +1,2377 @@ +/* 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/compiler/jit/mark_for_compilation_pass.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "absl/base/call_once.h" +#include "absl/container/flat_hash_map.h" +#include "absl/container/flat_hash_set.h" +#include "absl/status/statusor.h" +#include "absl/strings/str_join.h" +#include "tensorflow/compiler/jit/compilability_check_util.h" +#include "tensorflow/compiler/jit/deadness_analysis.h" +#include "tensorflow/compiler/jit/defs.h" +#include "tensorflow/compiler/jit/device_util.h" +#include "tensorflow/compiler/jit/flags.h" +#include "tensorflow/compiler/jit/resource_operation_safety_analysis.h" +#include "tensorflow/compiler/jit/xla_cluster_util.h" +#include "tensorflow/compiler/tf2xla/const_analysis.h" +#include "tensorflow/compiler/tf2xla/resource_operation_table.h" +#include "tensorflow/compiler/tf2xla/xla_op_registry.h" +#include "xla/service/graphcycles/graphcycles.h" +#include "xla/union_find.h" +#include "xla/util.h" +#include "tensorflow/core/common_runtime/function.h" +#include "tensorflow/core/common_runtime/graph_constructor.h" +#include "tensorflow/core/framework/bounds_check.h" +#include "tensorflow/core/framework/graph_def_util.h" +#include "tensorflow/core/framework/memory_types.h" +#include "tensorflow/core/framework/node_def.pb.h" +#include "tensorflow/core/framework/op_kernel.h" +#include "tensorflow/core/framework/tensor.pb.h" +#include "tensorflow/core/framework/types.h" +#include "tensorflow/core/graph/algorithm.h" +#include "tensorflow/core/graph/control_flow.h" +#include "tensorflow/core/lib/gtl/cleanup.h" +#include "tensorflow/core/lib/gtl/flatmap.h" +#include "tensorflow/core/lib/strings/stringprintf.h" +#include "tensorflow/core/platform/errors.h" +#include "tensorflow/core/platform/mutex.h" +#include "tensorflow/core/platform/statusor.h" +#include "tensorflow/core/platform/types.h" +#include "tensorflow/core/public/version.h" +#include "tensorflow/core/util/dump_graph.h" + +namespace tensorflow { + +namespace { +using DeadnessPredicate = DeadnessAnalysis::DeadnessPredicate; +using jit::DeviceId; +using jit::DeviceSet; + +// The clusters we create here are eventually lowered into an +// _XlaCompile/_XlaRun pair with a TF executor "fallback" that uses the +// PartitionedCall op to execute the cluster in the regular graph executor if +// need be. PartitionedCall, however, reruns the entire TF graph optimization +// pipeline over the cluster which includes this mark for compilation pass. To +// avoid endlessly recursing we tag nodes that we've already visited with this +// attribute so that we can bail out if we see them a second time. +// +// TODO(sanjoy): This method is not robust since it is possible that the +// optimizations run by PartitionedCall can mutate the cluster arbitrarily, +// dropping the kXlaAlreadyClustered attributes from all nodes in the process. +// The correct fix is to use the ConfigProto to pass in some sort of flag into +// the PartitionedCall kernel that tells it to not rerun auto-clustering on the +// cluster. +const char* kXlaAlreadyClustered = "_XlaAlreadyClustered"; + +class MarkForCompilationPassImpl { + public: + struct DebugOptions { + // If true, do not respect the results of deadness analysis. + bool ignore_deadness_checks; + + // If true, do not do safety checks to preserve TensorFlow's resource + // variable concurrency semantics. + bool ignore_resource_variable_checks; + + // If true, do not respect the _XlaCompile=false attribute. + bool ignore_xla_compile_attr; + + // If true, compute the cluster name in a deterministic way so that its + // stable from run to rum. + bool deterministic_cluster_names; + + int max_cluster_size; + int min_cluster_size; + + // Compiler fuel for the auto-clustering algorithm. + // + // We decrement this value by one on every time we choose a compilation + // candidate and we stop clustering when it hits zero. This means the + // initial value for this variable (via --tf_xla_clustering_fuel=N) + // effectively acts as a "cap" for how much we cluster and we can bisect + // over this initial value to discover clustering decisions that cause a + // miscompile or a performance regression. + std::atomic* fuel; + + bool dump_graphs; + }; + + MarkForCompilationPassImpl(DebugOptions debug_options, Graph* graph, + FunctionLibraryDefinition* flib_def, Env* env, + OptimizerOptions::GlobalJitLevel global_jit_level, + bool cpu_global_jit, + std::string cluster_name_prefix) + : debug_options_(debug_options), + graph_(graph), + graph_fingerprint_(0), + flib_def_(flib_def), + env_(env), + global_jit_level_(global_jit_level), + cpu_global_jit_(cpu_global_jit), + cluster_name_prefix_(cluster_name_prefix) {} + + absl::Status Run(); + + private: + // Represents a "cluster" or a connected subgraph of a TensorFlow graph. + class Cluster { + public: + // Constructs a trivial cluster representing a single TF node. + Cluster(int tf_graph_node_id, int effective_cluster_size, + bool has_functional_control_flow, DeviceSet devices, + std::optional resource_op_device, + std::optional resource_var_operation_node_id, + std::optional deadness_predicate, + bool is_xla_compile_attr_true, std::optional xla_scope) + : cycles_graph_node_id_(tf_graph_node_id), + effective_cluster_size_(effective_cluster_size), + has_functional_control_flow_(has_functional_control_flow), + devices_(std::move(devices)), + resource_op_device_(resource_op_device), + deadness_predicate_(deadness_predicate), + is_xla_compile_attr_true_(is_xla_compile_attr_true), + xla_scope_(std::move(xla_scope)) { + if (resource_var_operation_node_id.has_value()) { + resource_var_operation_node_ids_.push_back( + *resource_var_operation_node_id); + } + } + + // Merges `other` into this cluster, and clears `other`. This method is + // closely tied with the implementation of `MarkForCompilationPassImpl`. + void Merge(Cluster* other); + + // If this is a trivial cluster containing only one node then return the ID + // of that node. May not be called otherwise. + int GetIdOfOnlyNode() const { + DCHECK_EQ(cluster_size(), 1); + return cycles_graph_node_id(); + } + + // The number of TF nodes in this cluster. + int cluster_size() const { return cluster_size_; } + + // The ID of the cluster as represented in `cycles_graph_`. + int cycles_graph_node_id() const { return cycles_graph_node_id_; } + + // Sets the ID of the cluster as represented in `cycles_graph_`. + void set_cycles_graph_node_id(int cycles_graph_node_id) { + cycles_graph_node_id_ = cycles_graph_node_id; + } + + // The size of the cluster excluding constant and identity nodes. + int effective_cluster_size() const { return effective_cluster_size_; } + + // True if the cluster has functional control flow like `If` and `While`. + bool has_functional_control_flow() const { + return has_functional_control_flow_; + } + + // The set of devices nodes in the cluster are placed on. + const DeviceSet& devices() const { return devices_; } + + // If the cluster has a resource operation then the device the resource + // operation is placed on. A cluster may have resource ops placed only on a + // single device. + const std::optional& resource_op_device() const { + return resource_op_device_; + } + + // If not nullopt the a predicate that is true iff the cluster is alive. + // Otherwise the user has (unsafely) disabled deadness analysis. If this is + // unset on a single Cluster instance then it is unset on all Cluster + // instances. + const std::optional& deadness_predicate() const { + return deadness_predicate_; + } + + // If true then the cluster has a XlaCompile=true attribute on one of its + // nodes. + bool is_xla_compile_attr_true() const { return is_xla_compile_attr_true_; } + + // If not nullopt then the all nodes in the cluster either do not have the + // XlaScope attribute set or have it set to the value returned. + const std::optional& xla_scope() const { return xla_scope_; } + + // Returns the TF graph node IDs for the resource variable operations in + // this cluster. + absl::Span resource_var_operation_node_ids() const { + return resource_var_operation_node_ids_; + } + + std::string DebugString(const Graph& graph) const { + Node* node = graph.FindNodeId(cycles_graph_node_id()); + if (!node) { + // This should never happen but we try to be resilient because this is a + // debugging aid. + return absl::StrCat("NULL NODE IN #", cycles_graph_node_id()); + } + + if (cluster_size() == 1) { + return absl::StrCat("<", node->name(), " #", cycles_graph_node_id(), + ">"); + } + + return absl::StrCat("<", node->name(), " + ", cluster_size() - 1, + " others #", cycles_graph_node_id(), ">"); + } + + private: + int cluster_size_ = 1; + int cycles_graph_node_id_; + int effective_cluster_size_; + bool has_functional_control_flow_; + DeviceSet devices_; + std::optional resource_op_device_; + std::optional deadness_predicate_; + bool is_xla_compile_attr_true_; + std::optional xla_scope_; + std::vector resource_var_operation_node_ids_; + + Cluster(const Cluster&) = delete; + void operator=(const Cluster&) = delete; + }; + + // If `cluster` has only a single node then returns that, otherwise returns + // nullptr. + Node* GetOnlyNodeIn(const Cluster& cluster); + + // Returns true if `cluster` is a trivial cluster containing a "sink like" + // node -- a NoOp node that only the Sink node control depends on. + bool IsSinkLike(const Cluster& cluster); + + // Returns true if `cluster` looks like an "i++" operation on an integer + // scalar resource variable. + bool IsScalarIntegerResourceOperation(const Cluster& cluster); + + // --------------------------------------------------------------------------- + // The pass proceeds in five steps, out of which `RunEdgeContractionLoop` and + // `CreateClusters` do most of the heavy lifting. + + // Initializes some internal data structures. + // + // If this returns false then Initialize exited early (either because there is + // nothing to do or we saw a graph that we can't handle) and not all the + // fields in this MarkForCompilationPassImpl instance are set up. + absl::StatusOr Initialize(); + + // Runs through the entire cluster graph in post-order and calls `fn(from, + // to)` on each edge. `fn(from, to)` is expected to return true if it was + // able to contract `from`->`to`. + // + // Returns true if `fn` returned true for any edge. + template + absl::StatusOr ForEachEdgeInPostOrder(FnTy fn); + + // Contracts as many edges as possible to create XLA clusters. After this + // finishes the clustering decisions made are implicitly stored in + // `clusters_`. + absl::Status RunEdgeContractionLoop(); + + // "Fixes up" clusters by removing some modes. + // + // Autoclustering can sometimes be overeager. For example, clustering large + // constants (or large broadcasts of constants) can increase the live range + // of those constants, and increase overall memory usage. + // + // This function removes "obviously bad" cases like these. + absl::Status DeclusterNodes(); + + // Manifests the clustering decisions into the TF graph by tagging nodes with + // an `_XlaCluster` attribute. Also some basic filter logic, like + // tf_xla_min_cluster_size, are applied here. + absl::Status CreateClusters(); + + absl::Status DumpDebugInfo(); + + bool IsCompilationCandidate(Node* n) const { + return compilation_candidates_.find(n) != compilation_candidates_.end(); + } + + // Tries to contract the edge from cluster `from` to cluster `to`. Returns + // true if successful. + absl::StatusOr TryToContractEdge(Cluster* from, Cluster* to); + + // Nodes that XLA can compile are put in `compilation_candidates_`. + absl::Status FindCompilationCandidates(); + + bool CompilationDisallowedByXlaCompileAttr(Node* node); + + // Populates `clusters_`. + absl::Status BuildInitialClusterSet(); + + absl::StatusOr ShouldCompileClusterImpl(const Cluster& cluster); + + absl::StatusOr ShouldCompileCluster(const Cluster& cluster); + + absl::StatusOr ClusteringWillIntroduceInterDeviceDependency( + const Cluster& from, const Cluster& to); + + bool ShouldCompile(bool is_xla_compile_attr_true, + const DeviceType& device_type, + XlaOpRegistry::AutoclusteringPolicy policy) { + return is_xla_compile_attr_true || + policy == XlaOpRegistry::AutoclusteringPolicy::kAlways || + (policy == XlaOpRegistry::AutoclusteringPolicy::kIfEnabledGlobally && + global_jit_level_ != OptimizerOptions::OFF) || + (device_type.type_string() == DEVICE_CPU && + policy == + XlaOpRegistry::AutoclusteringPolicy::kIfExplicitlyRequested && + cpu_global_jit_); + } + + // Returns true if the devices in `cluster_a` and `cluster_b` are compatible + // and therefore not a hindrance for combining the two clusters into a larger + // cluster. + absl::StatusOr AreDevicesCompatible(const Cluster& cluster_a, + const Cluster& cluster_b); + + void DumpPostClusteringGraphs(); + void VLogClusteringSummary(); + + Cluster* MakeNewCluster(int cycles_graph_node_id, int effective_cluster_size, + bool has_functional_control_flow, + const DeviceSet& device_set, + std::optional resource_op_device, + std::optional resource_var_operation_node_id, + std::optional deadness_predicate, + bool is_xla_compile_attr_true, + std::optional xla_scope) { + cluster_storage_.push_back(std::make_unique( + cycles_graph_node_id, effective_cluster_size, + has_functional_control_flow, device_set, resource_op_device, + resource_var_operation_node_id, deadness_predicate, + is_xla_compile_attr_true, xla_scope)); + return cluster_storage_.back().get(); + } + + std::optional GetXlaScope(Node* n); + + // Returns the cluster for node `n`. If two nodes, N1 and N2, are placed in + // the same cluster by the clustering algorithm then this function will return + // the same Cluster instance for N1 and N2. + // + // Returns nullptr if `n` is not a compilation candidate. + Cluster* GetClusterForNode(Node* n) { + return cluster_for_node_[n->id()].Get(); + } + + // Returns the cluster for a node in `cycles_graph_`. This uses the same + // underlying map because of how we set things up, but we can do an additional + // CHECK in this accessor. + // + // Returns nullptr if `node_id` is not a compilation candidate. + Cluster* GetClusterForCyclesGraphNode(int node_id) { + // We have to check `graph_->FindNodeId(node) == nullptr` because we add all + // nodes in [0, graph_->num_node_ids()) to the cycle detection graph but the + // TF graph may be missing some node ids. + if (node_id >= graph_->num_node_ids() || + graph_->FindNodeId(node_id) == nullptr) { + return nullptr; + } + Cluster* cluster = cluster_for_node_[node_id].Get(); + if (cluster) { + DCHECK_EQ(cluster->cycles_graph_node_id(), node_id); + } + return cluster; + } + + bool LogNotContractableAndReturnFalse(Cluster* from, Cluster* to, + absl::string_view reason); + + // Finds a path in `cycles_graph_` from `from` to `to` that is not a direct + // edge from `from` to `to`. + // + // Tries to find a path that contains at least one unclusterable node. + std::vector FindAlternatePathForDebugging(int from, int to); + + // Returns a string representing `cycles_graph_node_id`. If the node is + // unclusterable (either it is a phatom "frame" node or is not a compilation + // candidate) then set `*found_unclustered` to true. + std::string DebugStringForCyclesGraphNode(int node_id, + bool* found_unclustered); + + // We could not contract the edge from `from` to `to`. Return a string + // describing an alternate path from `from` to `to` (besides the direct edge + // from `from` to `to`) which would have created a cycle had we contracted the + // edge. + // + // Tries (if possible) to find a path that contains at least one unclusterable + // node as it is surprising to the user if we print "A->B could not be + // contracted because of the path [P,Q,R]" where P, Q and R are all clusters + // since in that case a natural question is why we could not form a {A, P, Q, + // R, B} cluster. + std::string DescribePotentialCycle(int from, int to); + + // Merge the clusters `cluster_from` and `cluster_to`. After this step the + // larger combined cluster is represented by `cluster_from`, but can have + // `cycles_graph_`'s ID of either `cluster_from` or `cluster_to` depending on + // which way will require less operations. + bool MergeClusters(Cluster* cluster_from, Cluster* cluster_to) { + int from = cluster_from->cycles_graph_node_id(); + int to = cluster_to->cycles_graph_node_id(); + + auto optional_merged_node = cycles_graph_.ContractEdge(from, to); + if (!optional_merged_node.has_value()) { + VLOG(3) << "Could not contract " << cluster_from->DebugString(*graph_) + << " -> " << cluster_to->DebugString(*graph_) + << " because contracting the edge would create a cycle via " + << DescribePotentialCycle(from, to) << "."; + return false; + } + + // Merge the clusters. + cluster_from->Merge(cluster_to); + // Update `cycle_graph_`'s ID. + cluster_from->set_cycles_graph_node_id(optional_merged_node.value()); + + // Merge the UnionFind. + cluster_for_node_[from].Merge(&cluster_for_node_[to]); + + return true; + } + + std::string EdgeContractionFailureMsg(Cluster* from, Cluster* to, + absl::string_view reason) { + return absl::StrCat("Could not contract ", from->DebugString(*graph_), + " -> ", to->DebugString(*graph_), " because ", reason, + "."); + } + + DebugOptions debug_options_; + Graph* graph_; + uint64_t graph_fingerprint_; + FunctionLibraryDefinition* flib_def_; + Env* env_; + OptimizerOptions::GlobalJitLevel global_jit_level_; + bool cpu_global_jit_; + const std::string cluster_name_prefix_; + absl::flat_hash_map should_compile_cluster_cache_; + jit::DeviceInfoCache device_info_cache_; + + bool initialized_ = false; + bool edges_contracted_ = false; + bool clusters_created_ = false; + + std::vector> cluster_storage_; + std::vector> cluster_for_node_; + absl::flat_hash_set declustered_nodes_; + xla::GraphCycles cycles_graph_; + OrderedNodeSet compilation_candidates_; + std::unique_ptr deadness_analysis_; + int64_t iteration_count_ = 0; + absl::flat_hash_set> unsafe_resource_deps_; +}; + +std::vector MarkForCompilationPassImpl::FindAlternatePathForDebugging( + int from, int to) { + std::vector rpo = cycles_graph_.AllNodesInPostOrder(); + absl::c_reverse(rpo); + + // best_pred_for_node[n] contains a predecessor of `n` that has an + // unclusterable node in some path from `from` to itself. + // best_pred_for_node[n] is unpopulated for nodes that are not reachable from + // `from`. We build this table up inductively by traversing the cycles graph + // in RPO. + absl::flat_hash_map best_pred_for_node; + best_pred_for_node[from] = -1; + + int rpo_index = 0, current_rpo_node; + do { + current_rpo_node = rpo[rpo_index++]; + std::optional some_pred, preferred_pred; + for (int pred : cycles_graph_.Predecessors(current_rpo_node)) { + if (!best_pred_for_node.contains(pred)) { + continue; + } + + // Ignore the from->to edge since we're trying to find an alternate path. + if (current_rpo_node == to && pred == from) { + continue; + } + + some_pred = pred; + if (GetClusterForCyclesGraphNode(pred) == nullptr) { + preferred_pred = pred; + } + } + + if (some_pred || preferred_pred) { + best_pred_for_node[current_rpo_node] = + preferred_pred.has_value() ? *preferred_pred : *some_pred; + } + } while (current_rpo_node != to); + + auto get_best_pred = [&](int n) { + auto it = best_pred_for_node.find(n); + CHECK(it != best_pred_for_node.end()); + return it->second; + }; + + std::vector path; + int current_path_node = get_best_pred(to); + while (current_path_node != from) { + path.push_back(current_path_node); + current_path_node = get_best_pred(current_path_node); + } + + absl::c_reverse(path); + return path; +} + +std::string MarkForCompilationPassImpl::DebugStringForCyclesGraphNode( + int cycles_graph_node_id, bool* found_unclustered) { + Cluster* cluster = GetClusterForCyclesGraphNode(cycles_graph_node_id); + if (cluster) { + return cluster->DebugString(*graph_); + } + + *found_unclustered = true; + if (cycles_graph_node_id >= graph_->num_node_ids()) { + return absl::StrCat(""); + } + + Node* node = graph_->FindNodeId(cycles_graph_node_id); + if (!node) { + return absl::StrCat(""); + } + + return node->name(); +} + +std::string MarkForCompilationPassImpl::DescribePotentialCycle(int from, + int to) { + std::vector path_str; + bool found_unclustered = false; + absl::c_transform(FindAlternatePathForDebugging(from, to), + std::back_inserter(path_str), [&](int node_id) { + return DebugStringForCyclesGraphNode(node_id, + &found_unclustered); + }); + return absl::StrCat(!found_unclustered ? "(all clusters) " : "", "[", + absl::StrJoin(path_str, ","), "]"); +} + +void MarkForCompilationPassImpl::Cluster::Merge(Cluster* other) { + // We keep our own cycles_graph_node_id_ to mirror what GraphCycles does. + + // Clearing out data structures in `other` is just a memory saving + // optimization and not needed for correctness. + + cluster_size_ += other->cluster_size_; + effective_cluster_size_ += other->effective_cluster_size_; + has_functional_control_flow_ |= other->has_functional_control_flow_; + + devices_.UnionWith(other->devices_); + + DCHECK(!(resource_op_device_.has_value() && + other->resource_op_device_.has_value()) || + *resource_op_device_ == *other->resource_op_device_) + << "AreDevicesCompatible should have returned false otherwise!"; + + if (!resource_op_device_.has_value()) { + resource_op_device_ = other->resource_op_device_; + } + + is_xla_compile_attr_true_ |= other->is_xla_compile_attr_true_; + + if (!xla_scope_.has_value()) { + xla_scope_ = std::move(other->xla_scope_); + } + + resource_var_operation_node_ids_.reserve( + resource_var_operation_node_ids_.size() + + other->resource_var_operation_node_ids_.size()); + absl::c_copy(other->resource_var_operation_node_ids_, + std::back_inserter(resource_var_operation_node_ids_)); + other->resource_var_operation_node_ids_.clear(); +} + +absl::Status IgnoreResourceOpForSafetyAnalysis( + jit::DeviceInfoCache* device_info_cache, const Node& n, bool* ignore) { + // If a resource operation is assigned to XLA_CPU or XLA_GPU explicitly then + // ignore it during resource operation safety analysis. We need this hack + // because of two reasons: + // + // 1. Operations assigned to XLA_CPU and XLA_GPU have to always be compiled. + // 2. We don't support live-out values of type DT_RESOURCE and live-in values + // of type DT_RESOURCE that are not resource variables. + // + // Together these imply we cannot let resource variable safety analysis + // constrain e.g. a TensorArrayV3->TensorArrayAssignV3 edge to be in different + // clusters: both of them will have to be clustered because of (1) and we + // won't be able to keep the edge between the two as neither the input to the + // second XLA cluster nor the output from the first XLA cluster are supported + // because of (2). + // + // TODO(b/113100872): This can be fixed if the TensorFlow representation for + // TensorArray and Stack on the XLA_{C|G}PU devices were the same in XLA; then + // (2) would no longer hold. + + if (n.assigned_device_name().empty()) { + *ignore = false; + return absl::OkStatus(); + } + + TF_ASSIGN_OR_RETURN( + const XlaOpRegistry::DeviceRegistration* registration, + device_info_cache->GetCompilationDevice(n.assigned_device_name())); + + if (!registration) { + *ignore = true; + } else { + *ignore = registration->cluster_resource_variable_ops_unsafely; + } + return absl::OkStatus(); +} + +absl::StatusOr MarkForCompilationPassImpl::Initialize() { + TF_RET_CHECK(!initialized_ && !edges_contracted_ && !clusters_created_); + initialized_ = true; + + TF_RETURN_IF_ERROR(FindCompilationCandidates()); + + if (compilation_candidates_.empty()) { + VLOG(2) << "No compilable candidates"; + return false; + } + + TF_ASSIGN_OR_RETURN(bool cycle_detection_graph_ok, + CreateCycleDetectionGraph(graph_, &cycles_graph_)); + if (!cycle_detection_graph_ok) { + // TODO(sanjoy): This should be logged via the XLA activity listener. + VLOG(2) << "Could not form cycle detection graph"; + return false; + } + + if (!debug_options_.ignore_deadness_checks) { + XLA_SCOPED_LOGGING_TIMER_LEVEL("DeadnessAnalysis", 1); + TF_RETURN_IF_ERROR(DeadnessAnalysis::Run(*graph_, &deadness_analysis_)); + } + + // If the user is requesting deterministic cluster names compute a hash of the + // input graph to provide a stable but unique prefix for the name. + if (debug_options_.deterministic_cluster_names) { + TF_ASSIGN_OR_RETURN(graph_fingerprint_, FingerprintGraph(*graph_)); + } + + // Each compilation candidate belongs to a cluster. The cluster's + // representative names the node in the 'cycles' graph that represents the + // cluster. + TF_RETURN_IF_ERROR(BuildInitialClusterSet()); + return true; +} + +template +absl::StatusOr MarkForCompilationPassImpl::ForEachEdgeInPostOrder( + FnTy fn) { + bool changed = false; + for (int32_t node : cycles_graph_.AllNodesInPostOrder()) { + Cluster* cluster_from = GetClusterForCyclesGraphNode(node); + if (!cluster_from) { + continue; + } + + // Make a copy of the set of successors because we may modify the graph in + // TryToContractEdge. + std::vector successors_copy = + cycles_graph_.SuccessorsCopy(cluster_from->cycles_graph_node_id()); + + for (int to : successors_copy) { + iteration_count_++; + + Cluster* cluster_to = GetClusterForCyclesGraphNode(to); + if (!cluster_to) { + continue; + } + + TF_ASSIGN_OR_RETURN(bool contracted_edge, fn(cluster_from, cluster_to)); + changed |= contracted_edge; + } + } + + return changed; +} + +Node* MarkForCompilationPassImpl::GetOnlyNodeIn(const Cluster& cluster) { + return cluster.cluster_size() == 1 + ? graph_->FindNodeId(cluster.GetIdOfOnlyNode()) + : nullptr; +} + +bool MarkForCompilationPassImpl::IsSinkLike(const Cluster& cluster) { + if (Node* n = GetOnlyNodeIn(cluster)) { + return n->type_string() == "NoOp" && n->out_edges().size() == 1 && + (*n->out_edges().begin())->dst()->IsSink(); + } + + return false; +} + +bool MarkForCompilationPassImpl::IsScalarIntegerResourceOperation( + const Cluster& cluster) { + Node* n = GetOnlyNodeIn(cluster); + if (!n) { + return false; + } + + if (n->type_string() != "AssignAddVariableOp" && + n->type_string() != "AssignSubVariableOp") { + return false; + } + + DataType dtype; + if (!TryGetNodeAttr(n->def(), "dtype", &dtype) || !DataTypeIsInteger(dtype)) { + return false; + } + + Node* const_input = nullptr; + for (const Edge* e : n->in_edges()) { + if (!e->IsControlEdge() && e->src()->IsConstant()) { + const_input = e->src(); + break; + } + } + + if (!const_input) { + return false; + } + + const TensorProto* proto = nullptr; + if (!TryGetNodeAttr(const_input->def(), "value", &proto)) { + return false; + } + + return TensorShapeUtils::IsScalar(proto->tensor_shape()); +} + +absl::Status MarkForCompilationPassImpl::RunEdgeContractionLoop() { + TF_RET_CHECK(initialized_ && !edges_contracted_ && !clusters_created_); + edges_contracted_ = true; + + // TODO(hpucha): Handle the case where kXlaClusterAttr is already set (for + // example, from the Grappler fusion pass). + + // In general there are multiple maximal clusterings, but they are not all + // equally performant. Some clustering decision are likely to improve + // performance much more than others, and we cannot order contractions on this + // cost function, nor can we look at global information while deciding on + // individual edges to contract. Instead, we will make decisions on these + // important edges then make decisions on all other edges, causing the highest + // chance of all most important edges to be contracted. + // + // An example of where this might occur is with a digraph: + // {A -> B, B -> C, A -> X, X -> C} where B is a Size operation and X is + // not-compilable. In this case, the valid clusterings are {A,B} or {B,C}. B + // should be clustered with A because it will prevent a potentially large + // tensor from A being computed and copied. + // + // To choose better maximal clusterings we make multiple iterations over the + // graph in post-order, where each such iteration is called a "phase". + + // Phase 0: contract metadata operations with their producer. + + VLOG(4) << "Running phase 0"; + TF_RETURN_IF_ERROR( + ForEachEdgeInPostOrder([&](Cluster* from, + Cluster* to) -> absl::StatusOr { + // Shape consuming operations are desirable to cluster with their + // operands because they return a small set of scalar values after + // consuming a large amount of data. For example, given a graph X -> Y + // -> Size -> Z, where the possible clustering is [{X, Y, Size}, {Z}] or + // [{X, Y}, {Size, Z}], the better clustering is Size with Y because the + // output of size will be a small tensor while Y is a potentially large + // tensor that must be computed and possible transposed/copied before + // the second cluster executes. + Node* n = GetOnlyNodeIn(*to); + bool is_shape_consumer_op = n && IsShapeConsumerOp(*n); + if (!is_shape_consumer_op) { + return false; + } + + return TryToContractEdge(from, to); + }).status()); + + // Phase 1: apply a heuristic to ensure that we don't mess up clustering due + // to "group_deps". After this phase most edges should have been contracted. + + VLOG(4) << "Running phase 1"; + TF_RETURN_IF_ERROR( + ForEachEdgeInPostOrder([&](Cluster* from, + Cluster* to) -> absl::StatusOr { + // We split out this phase to get good clustering in the presence of a + // specific pattern seen in some graphs: + // + // digraph { + // ApplyWeightUpdates_0 -> "iteration++" + // ApplyWeightUpdates_1 -> "iteration++" + // ApplyWeightUpdates_2 -> "iteration++" + // ApplyWeightUpdates_0 -> Computation_A + // ApplyWeightUpdates_1 -> Computation_B + // ApplyWeightUpdates_2 -> Computation_C + // Computation_A -> NoOp + // Computation_B -> NoOp + // Computation_C -> NoOp + // "iteration++" -> NoOp + // } + // + // In the graph above we can't cluster iteration++ with any of the + // gradient update operations since that will break the TF resource + // variable memory model. Given that constraint the ideal clustering + // would be to put all the gradient updates and all of the Computation_* + // nodes in one cluster, and leave iteration++ and NoOp unclustered. + // + // A naive post-order traversal would not create this good clustering, + // however. Instead it will first create a cluster that puts + // Computation_* nodes, the NoOp and iteration++ node in a single + // cluster, after which it will fail to put any of the + // ApplyWeightUpdates_* nodes into this cluster. To avoid this fate we + // instead run a pass that avoids contracting edges _into_ NoOps like + // the above, and avoid clustering edges _from_ "iteration++" like the + // above. Then we run a second pass that contracts the edges we could + // not contract the first time around. + + if (IsSinkLike(*to)) { + return false; + } + + if (IsScalarIntegerResourceOperation(*from)) { + return false; + } + + return TryToContractEdge(from, to); + }).status()); + + // Phase 2: contract any remaining edges. After this phase we should have a + // maximal clustering: + // + // A. We visit a cluster only after maximally clustering all its children. + // B. By the time we're done with a node all of its children that could have + // been absorbed into the node have been absorbed. + // C. We have an invariant that making a cluster larger does not make edges + // leaving it more contractable. That is, if we have + // digraph { X->Y; Y->Z; } then collapsing X->Y does not make it possible + // to contract Y->Z if Y->Z was not contractible originally. + VLOG(4) << "Running phase 2"; + TF_RETURN_IF_ERROR(ForEachEdgeInPostOrder([&](Cluster* from, Cluster* to) { + return TryToContractEdge(from, to); + }).status()); + + // Check that the conclusion made above (that iterating over the graph once in + // post order gives a maximal clustering) holds. Once the linear time + // post-order scheme has been battle tested we can move this to happen only in + // debug builds. + VLOG(2) << "Checking idempotence"; + TF_ASSIGN_OR_RETURN(bool changed, + ForEachEdgeInPostOrder([&](Cluster* from, Cluster* to) { + return TryToContractEdge(from, to); + })); + TF_RET_CHECK(!changed); + + return absl::OkStatus(); +} + +int64_t GetConstantTensorSize(Node* n) { + if (n->op_def().name() != "Const") return -1; + + const TensorProto* proto = nullptr; + absl::Status s = GetNodeAttr(n->def(), "value", &proto); + if (!s.ok()) return -1; + + if (!proto->has_tensor_shape()) { + return -1; + } + const auto& tensor_shape_proto = proto->tensor_shape(); + if (tensor_shape_proto.unknown_rank()) { + return -1; + } + int64_t num_elements = 1; + for (const auto& dim : tensor_shape_proto.dim()) { + // Note that in some cases, dim.size() can be zero (e.g., empty vector). + num_elements *= dim.size(); + } + return num_elements; +} + +absl::Status MarkForCompilationPassImpl::DeclusterNodes() { + for (Node* n : compilation_candidates_) { + Cluster* cluster = GetClusterForNode(n); + if (cluster == nullptr) { + continue; + } + + // Large constants (bigger than L1 cache size) shared across multiple + // clusters typically get copied, resulting in performance penalty. Below + // code will avoid copying large constant to multiple clusters. + const int64_t kLargeConstantThreshold = 16384; + if (n->op_def().name() == "Const") { + int64_t tensor_size = GetConstantTensorSize(n); + if (tensor_size > kLargeConstantThreshold) { + // Check if the constant is used outside its cluster + if (absl::c_any_of(n->out_nodes(), [&](Node* user) { + return GetClusterForNode(user) != cluster; + })) { + declustered_nodes_.insert(n); + } + } + } + + // De-cluster Fill ops that are + // - used at least once outside the cluster, and + // - not used inside the cluster. + // + // In this case, using XLA for the op can only make peak memory usage worse. + // If we don't cluster the Fill, it can be materialized right before it's + // used in the TF graph. Whereas if we do cluster it, the Fill must be live + // starting at the end of the XLA cluster, potentially significantly + // increasing its live range. + // + // See b/221997940 for a real-world example of this. + if (n->op_def().name() == "Fill" && + n->out_nodes().begin() != n->out_nodes().end() && + absl::c_all_of(n->out_nodes(), [&](Node* user) { + return GetClusterForNode(user) != cluster; + })) { + declustered_nodes_.insert(n); + } + } + + return absl::OkStatus(); +} + +// Tracks monotonic sequence numbers for graphs. +class ClusterSequenceNumberGenerator { + public: + void Reset() { + mutex_lock lock(mu_); + sequence_numbers_.clear(); + } + + int64_t GetNext(uint64_t key) { + mutex_lock lock(mu_); + return sequence_numbers_[key]++; + } + + static ClusterSequenceNumberGenerator& Global() { + static ClusterSequenceNumberGenerator* gen = + new ClusterSequenceNumberGenerator; + return *gen; + } + + private: + mutex mu_; + absl::flat_hash_map sequence_numbers_; +}; + +// Get a monotonic sequence numbers for a graph identified by its `fingerprint`. +// The sequence number is necessary to disambiguate clusters extracted from the +// same graph and when duplicate graphs exist within the same process. +int64_t GetNextClusterSequenceNumber(uint64_t fingerprint) { + return ClusterSequenceNumberGenerator::Global().GetNext(fingerprint); +} + +absl::Status MarkForCompilationPassImpl::CreateClusters() { + TF_RET_CHECK(initialized_ && edges_contracted_ && !clusters_created_); + clusters_created_ = true; + + // Names for each cluster. + std::unordered_map cluster_names; + + if (debug_options_.dump_graphs) { + DumpGraphToFile("before_mark_for_compilation", *graph_, flib_def_); + } + + // Mark clusters for compilation that: + // * are placed on a device that requires compilation (an XlaDevice), + // * are explicitly marked for compilation (_XlaCompile=true), or + // * have more than debug_options_.xla_min_cluster_size elements (applicable + // only if compilation is enabled, otherwise there will be no such + // candidates). + for (Node* n : compilation_candidates_) { + Cluster* cluster = GetClusterForNode(n); + TF_ASSIGN_OR_RETURN(bool should_compile_cluster, + ShouldCompileCluster(*cluster)); + if (!should_compile_cluster || declustered_nodes_.contains(n)) { + continue; + } + + // We assume that functional If and While nodes have at least + // min_cluster_size non-trivial nodes in them. It would be more principled + // to (recursively) verify this fact, but that's probably not worth the + // trouble. + + if (cluster->effective_cluster_size() >= debug_options_.min_cluster_size || + cluster->has_functional_control_flow() || + cluster->is_xla_compile_attr_true()) { + std::string& name = cluster_names[cluster->cycles_graph_node_id()]; + + if (name.empty()) { + if (!cluster_name_prefix_.empty()) { + name = absl::StrCat(cluster_name_prefix_, "_"); + } else { + name = "cluster_"; + } + if (debug_options_.deterministic_cluster_names) { + absl::StrAppend(&name, graph_fingerprint_, "_"); + } + absl::StrAppend(&name, + GetNextClusterSequenceNumber(graph_fingerprint_)); + } + + n->AddAttr(kXlaClusterAttr, name); + n->AddAttr(kXlaAlreadyClustered, true); + VLOG(3) << "Assigning node " << n->name() << " to cluster " << name; + } + } + + return absl::OkStatus(); +} + +absl::Status MarkForCompilationPassImpl::DumpDebugInfo() { + TF_RET_CHECK(initialized_ && edges_contracted_ && clusters_created_); + + if (debug_options_.dump_graphs) { + DumpPostClusteringGraphs(); + } + + VLogClusteringSummary(); + + return absl::OkStatus(); +} + +absl::StatusOr +MarkForCompilationPassImpl::ClusteringWillIntroduceInterDeviceDependency( + const Cluster& cluster_from, const Cluster& cluster_to) { + // If any of the consumer's producers are on a different device, do not + // cluster these nodes. This prevents other work on this device from being + // delayed by work on other devices. We consider predecessors of the entire + // cluster rather than just the inputs to the node to prevent the cluster + // still being combined in cases where the 'to' cluster has multiple + // dependencies on the 'from' cluster and another dependency leads to a + // merging of the clusters. + // + // TODO(b/117085735): We probably want to handle the reciprocal of this case + // where a cluster is producing data for multiple devices. + for (const auto& in_id : + cycles_graph_.Predecessors(cluster_to.cycles_graph_node_id())) { + const Cluster* cluster_in = GetClusterForCyclesGraphNode(in_id); + if (cluster_in) { + TF_ASSIGN_OR_RETURN(bool devices_compatible, + AreDevicesCompatible(cluster_to, *cluster_in)); + if (!devices_compatible) { + return true; + } + TF_ASSIGN_OR_RETURN(devices_compatible, + AreDevicesCompatible(cluster_from, *cluster_in)); + if (!devices_compatible) { + return true; + } + } + } + + return false; +} + +std::optional MarkForCompilationPassImpl::GetXlaScope(Node* node) { + // Look for either _XlaScope or _XlaInternalScope on both nodes to guide + // clustering. If both nodes have a scope and the scopes do not match, do + // not cluster along this edge. If even one of the nodes lacks a scope + // attribute, then it is treated as a "bridge" and a cluster may be created + // along it. + // + // The difference between _XlaScope and _XlaInternalScope is that _XlaScope is + // provided by users through jit_scope APIs, while _XlaInternalScope is + // automatically generated by the ClusterScopingPass when auto_jit is on. As + // such, we respect _XlaScope only when auto_jit is off, while respecting + // _XlaInternalScope only when auto_jit is on. + // + // We may want to restrict the _XlaScope behavior to require all nodes marked + // with _XlaCompile=true to also have a _XlaScope property set (and raise an + // error otherwise); but for now we don't do this. + + if (global_jit_level_ != OptimizerOptions::OFF) { + // If global_jit_level_ is ON, respect only _XlaInternalScope. + const std::string& scope = + GetNodeAttrString(node->attrs(), kXlaInternalScopeAttr); + if (!scope.empty()) { + return scope; + } + } else { + // If global_jit_level_ is OFF, respect only _XlaScope. + const std::string& scope = GetNodeAttrString(node->attrs(), kXlaScopeAttr); + if (!scope.empty()) { + return scope; + } + } + + return std::nullopt; +} + +// Returns true iff the attribute `attr_name` is attached to either the node or +// to it's callee. +static bool GetNodeOrFuncAttr(Node* node, FunctionLibraryDefinition* flib_def, + const char* attr_name) { + bool out = false; + bool attr_value; + if (TryGetNodeAttr(node->attrs(), attr_name, &attr_value)) { + out |= attr_value; + } + + if (flib_def->GetAttr(*node, attr_name, &attr_value).ok()) { + out |= attr_value; + } + return out; +} + +absl::Status MarkForCompilationPassImpl::BuildInitialClusterSet() { + auto ignore_resource_ops = [&](const Node& n, bool* ignore) { + return IgnoreResourceOpForSafetyAnalysis(&device_info_cache_, n, ignore); + }; + + std::vector> unsafe_resource_deps_vect; + TF_RETURN_IF_ERROR(ComputeIncompatibleResourceOperationPairs( + *graph_, flib_def_, ignore_resource_ops, &unsafe_resource_deps_vect)); + absl::c_copy( + unsafe_resource_deps_vect, + std::inserter(unsafe_resource_deps_, unsafe_resource_deps_.begin())); + + cluster_for_node_.resize(graph_->num_node_ids()); + for (Node* node : graph_->nodes()) { + if (!IsCompilationCandidate(node)) { + cluster_for_node_[node->id()].Get() = nullptr; + continue; + } + + // We want clusters to be big enough that the benefit from XLA's + // optimizations offsets XLA related overhead (for instance we add some + // Switch/Merge nodes into the graph to implement lazy compilation). To + // this end, we don't count Identity and Constant nodes because they do not + // enable interesting optimizations by themselves. + int effective_cluster_size = + (node->IsIdentity() || node->IsConstant()) ? 0 : 1; + + bool has_functional_control_flow = node->IsWhileNode() || node->IsIfNode(); + + std::optional deadness_predicate; + if (deadness_analysis_) { + TF_ASSIGN_OR_RETURN( + deadness_predicate, + deadness_analysis_->GetPredicateFor(node, Graph::kControlSlot)); + } + + const std::string& device_name_str = !node->assigned_device_name().empty() + ? node->assigned_device_name() + : node->requested_device(); + TF_ASSIGN_OR_RETURN(DeviceId device, + device_info_cache_.GetIdFor(device_name_str)); + + bool is_resource_op = HasResourceInputOrOutput(*node); + std::optional resource_op_device; + if (is_resource_op) { + resource_op_device = device; + } + + std::optional resource_var_operation_node_id; + if (is_resource_op || MayCallFunction(*node, flib_def_)) { + resource_var_operation_node_id = node->id(); + } + + bool is_xla_compile_attr_true = + GetNodeOrFuncAttr(node, flib_def_, kXlaCompileAttr) || + (global_jit_level_ != OptimizerOptions::OFF && + GetNodeOrFuncAttr(node, flib_def_, kXlaMustCompileAttr)); + + DeviceSet devices; + devices.Insert(device); + + Cluster* new_cluster = MakeNewCluster( + /*cycles_graph_node_id=*/node->id(), + /*effective_cluster_size=*/effective_cluster_size, + /*has_functional_control_flow=*/has_functional_control_flow, devices, + resource_op_device, resource_var_operation_node_id, deadness_predicate, + /*is_xla_compile_attr_true=*/is_xla_compile_attr_true, + GetXlaScope(node)); + + cluster_for_node_[node->id()].Get() = new_cluster; + } + + return absl::OkStatus(); +} + +absl::StatusOr IsIdentityDrivingConstsInLoop(Node* node) { + if (!node->IsIdentity()) { + return false; + } + + // Check if the Identity is driven by a Switch on its true path. + auto it = absl::c_find_if(node->in_edges(), [](const Edge* e) { + return e->src()->IsSwitch() && e->src_output() == 1; + }); + if (it == node->in_edges().end()) { + return false; + } + const Node* switch_node = (*it)->src(); + + // Check if the Switch is driven by LoopCond. + const Node* maybe_loop_cond; + TF_RETURN_IF_ERROR(switch_node->input_node(1, &maybe_loop_cond)); + if (!maybe_loop_cond->IsLoopCond()) { + return false; + } + + // Check if the Identity is driving any const nodes through a control edge. + bool driving_any_consts = + absl::c_any_of(node->out_edges(), [](const Edge* e) { + return e->dst()->IsConstant() && e->IsControlEdge(); + }); + if (!driving_any_consts) { + return false; + } + + return true; +} + +absl::flat_hash_set CreateClusterExcludeList() { + MarkForCompilationPassFlags* flags = GetMarkForCompilationPassFlags(); + absl::flat_hash_set excludelist; + for (auto s : absl::StrSplit(flags->tf_xla_cluster_exclude_ops, ',')) { + if (!s.empty()) { + excludelist.insert(std::string(s)); + } + } + if (VLOG_IS_ON(2) && !excludelist.empty()) { + std::vector vexcludelist(excludelist.begin(), + excludelist.end()); + absl::c_sort(vexcludelist); + VLOG(2) << "XLA clustering will exclude following TF operations from auto " + "clustering: " + << absl::StrJoin(vexcludelist, " "); + } + return excludelist; +} + +absl::flat_hash_set GetOrCreateAllowlist() { + absl::flat_hash_map>* allowlist_table = + tensorflow::GetAllowlistTable(); + MarkForCompilationPassFlags* flags = GetMarkForCompilationPassFlags(); + absl::flat_hash_set allowlist; + + for (auto s : absl::StrSplit(flags->tf_xla_ops_to_cluster, ',')) { + if (s == "FUSIBLE") { + for (auto pair : *allowlist_table) { + allowlist.insert(pair.second.begin(), pair.second.end()); + } + } else if (allowlist_table->contains(s)) { + auto v = allowlist_table->at(s); + allowlist.insert(v.begin(), v.end()); + } else if (!s.empty()) { + // Should be a user provided TF operation. + allowlist.insert(std::string(s)); + } + } + + if (VLOG_IS_ON(2) && !allowlist.empty()) { + std::vector vallowlist(allowlist.begin(), allowlist.end()); + absl::c_sort(vallowlist); + VLOG(2) << "XLA clustering will only consider the following TF operations: " + << absl::StrJoin(vallowlist, " "); + } + return allowlist; +} + +absl::Status MarkForCompilationPassImpl::FindCompilationCandidates() { + OptimizerOptions opts; + std::unique_ptr pflr( + new ProcessFunctionLibraryRuntime(nullptr, env_, /*config=*/nullptr, + TF_GRAPH_DEF_VERSION, flib_def_, opts)); + FunctionLibraryRuntime* lib_runtime = + pflr->GetFLR(ProcessFunctionLibraryRuntime::kDefaultFLRDevice); + std::vector compile_time_const_nodes(graph_->num_node_ids(), false); + TF_RETURN_IF_ERROR(BackwardsConstAnalysis( + *graph_, /*compile_time_const_arg_indices=*/nullptr, + &compile_time_const_nodes, lib_runtime)); + // Iterate over nodes in sorted order so that compiler fuel is deterministic. + // We can't simply pass op_nodes().begin() and op_nodes().end() to the + // std::vector constructor because they're not proper iterators, with + // iterator_traits defined and so on. + std::vector sorted_nodes; + for (Node* node : graph_->op_nodes()) { + sorted_nodes.push_back(node); + } + std::sort(sorted_nodes.begin(), sorted_nodes.end(), NodeComparatorID()); + + if (*debug_options_.fuel >= std::numeric_limits::max() / 2) { + // The assumption is that if fuel started out as INT64_MAX, it will forever + // stay greater than INT64_MAX / 2. + VLOG(2) << "Starting fuel: infinity"; + } else { + VLOG(2) << "Starting fuel: " << *debug_options_.fuel; + } + + VLOG(2) << "sorted_nodes.size() = " << sorted_nodes.size(); + + auto allowlist = GetOrCreateAllowlist(); + + std::vector vall_ops = XlaOpRegistry::GetAllRegisteredOps(); + absl::flat_hash_set all_ops(vall_ops.begin(), vall_ops.end()); + // Check that user's provided TF operation really exists. + for (const auto& s : allowlist) { + if (!all_ops.contains(s)) { + return absl::InvalidArgumentError(absl::StrCat( + "The operation '", s, + "' passed to --tf_xla_ops_to_cluster is not supported by XLA.")); + } + } + + auto cluster_exclude_op_list = CreateClusterExcludeList(); + bool allow_where_op = true; + for (const auto& s : cluster_exclude_op_list) { + if (s == "Where") { + allow_where_op = false; + } else { + return absl::InvalidArgumentError(absl::StrCat( + "The operation '", s, + "' passed to --tf_xla_cluster_exclude_ops is not supported by " + "XLA.")); + } + } + + for (Node* node : sorted_nodes) { + if (*debug_options_.fuel <= 0) { + VLOG(1) + << "Hit fuel limit; not marking any remaining ops as clusterable."; + break; + } + + TF_ASSIGN_OR_RETURN( + const DeviceType& device_type, + device_info_cache_.GetDeviceTypeFor(node->assigned_device_name())); + VLOG(4) << "Device type for " << node->name() << ": " + << device_type.type_string(); + + if (CompilationDisallowedByXlaCompileAttr(node)) { + VLOG(2) << "Not clustering " << node->name() + << ": disallowed by _XlaCompile attribute"; + continue; + } + + const XlaOpRegistry::DeviceRegistration* registration; + if (!XlaOpRegistry::GetCompilationDevice(device_type.type(), + ®istration)) { + VLOG(2) << "Rejecting " << node->name() + << ": could not find JIT device for " << device_type.type(); + continue; + } + + // Skip nodes early that won't be compilable for efficiency. + bool is_xla_compile_attr_true = + GetNodeOrFuncAttr(node, flib_def_, kXlaCompileAttr) || + (global_jit_level_ != OptimizerOptions::OFF && + GetNodeOrFuncAttr(node, flib_def_, kXlaMustCompileAttr)); + auto policy = registration->autoclustering_policy; + if (!ShouldCompile(is_xla_compile_attr_true, device_type, policy)) { + continue; + } + + RecursiveCompilabilityChecker::OperationFilter filter = + CreateOperationFilter(*registration); + filter.require_always_compilable = true; + filter.allow_string_consts = false; + filter.allow_collective_reduce_v2 = false; + filter.allow_unique_op = false; + filter.allow_where_op = allow_where_op; + + RecursiveCompilabilityChecker checker( + filter, DeviceType{registration->compilation_device_name}); + + if (!checker.IsCompilableNode(*node, lib_runtime)) { + continue; + } + + if (node->type_string() == "Const") { + // Skip Const op with type DT_STRING, since XLA autoclustering doesn't + // support it. + const AttrValue* attr = node->attrs().Find("dtype"); + if (attr != nullptr && attr->type() == DT_STRING) { + continue; + } + } + + if (!allowlist.empty() && !allowlist.contains(node->def().op())) { + VLOG(1) << "Rejecting TF operation " << node->def().op() + << " as it is not listed in --tf_xla_ops_to_cluster."; + continue; + } + + if (compile_time_const_nodes[node->id()]) { + const OpDef* op_def; + TF_RETURN_IF_ERROR( + graph_->op_registry()->LookUpOpDef(node->type_string(), &op_def)); + if (op_def->is_stateful()) { + // It is easiest to demonstrate the problem we're trying to solve with + // an example. Say we have this graph: + // + // shape = RandomUniformInt(); + // reshape = Reshape(input, shape) + // + // Both RandomUniformInt and Reshape are compilable by XLA so, absent + // any other reason, we will try to put both shape and reshape in the + // same cluster. However, since XLA only supports statically shaped + // values, it will expect to be able to constant fold `shape` to get a + // static shape for `reshape`. This is a problem because side-effecting + // ops like RandomUniformInt() cannot be constant folded. We fix this + // by putting `shape` and `reshape` in different clusters, which results + // in us recompiling `reshape`'s cluster for every new value of `shape`, + // making `reshape` statically sized within each compilation. We + // simplify the solution even further by disallowing operations like + // `shape` from being part of *any* non-trivial cluster. They're either + // not compiled by XLA altogether or, if assigned to an XLA_* device + // with "must compile" semantics, compiled into a trivial single-op + // cluster. This approach leaves some room for improvement, and we can + // consider implementing a more aggressive data-flow-analysis based + // solution in the future if needed. + // + // One ugly problem we have to contend with: certain sets of ops *have* + // to be in the same cluster because values flowing between them have + // types that can't be live-in or live-out of a cluster. These ops are: + // + // - TensorArray ops operating on the same TensorArray instance. + // - Stack ops operating on the same Stack instance. + // + // To work around this we avoid isolating these specific ops. Because + // of this concession it is unsound to auto-cluster them because then + // we'd create clusters we could not compile (because we can't constant + // fold, say, a TensorArrayRead or a StackPopV2). But we don't + // auto-cluster these operations today so we're good for now. + const XlaResourceOpInfo* op_info = + GetResourceOpInfoForOp(node->type_string()); + bool is_tensor_array_or_stack_op = + op_info && op_info->resource_kind() != XlaResourceKind::kVariable; + if (!is_tensor_array_or_stack_op) { + VLOG(2) << "Isolating " << node->name() + << ": must-be-constant stateful op"; + continue; + } + } + } + + // This is a heuristic to avoid creating dependency between while loop + // condition and body computations. Dependency between them can be created + // if a special Identity node in the following pattern is clustered in. + // That is, an Identity node in the loop cond computation is used to drive + // const nodes consumed by the loop body. If this Identity node goes into + // the same cluster with nodes from the loop body, extra dependency is + // created between the loop cond and body computations and it hinders the + // progression of the loop cond computation at runtime with significant + // overhead. Specifically, we look for the below pattern and do not cluster + // in this Identity to avoid the described issue. Since Identity has low + // execution cost in native TF, the fact that this heuristic gives up these + // special Identity nodes as candidates should not harm any performance. If + // other considerations emerge in the future, we can revisit the heuristic + // and only disallow these Identities to go into the cluster with nodes from + // the loop body but still consider them candidates. + // + // LoopCond -> + // Merge -> Switch -> Identity -> i++ -> ... -> NextIteration + // ..> Const -> LoopBody + // (control edge) + TF_ASSIGN_OR_RETURN(bool is_identity_driving_consts_in_loop, + IsIdentityDrivingConstsInLoop(node)); + if (is_identity_driving_consts_in_loop) { + VLOG(2) << "Rejecting " << node->name() + << ": including it can create dependencies between while loop " + "condition and body computations with runtime overhead."; + continue; + } + + compilation_candidates_.insert(node); + --(*debug_options_.fuel); + } + + VLOG(2) << "compilation_candidates_.size() = " + << compilation_candidates_.size(); + return absl::OkStatus(); +} + +bool MarkForCompilationPassImpl::CompilationDisallowedByXlaCompileAttr( + Node* node) { + if (debug_options_.ignore_xla_compile_attr) { + return false; + } + + // If there is a _XlaCompile annotation, use its value. + bool compile = false; + absl::Status status = GetNodeAttr(node->attrs(), kXlaCompileAttr, &compile); + if (status.ok()) { + if (!compile) { + VLOG(2) << "Rejecting " << node->name() << ": kXlaCompileAttr(" + << kXlaCompileAttr << ") is false."; + } + return !compile; + } + + status = flib_def_->GetAttr(*node, kXlaCompileAttr, &compile); + if (status.ok()) { + if (!compile) { + VLOG(2) << "Rejecting " << node->name() << ": kXlaCompileAttr(" + << kXlaCompileAttr << ") on callee is false."; + } + return !compile; + } + + return false; +} + +bool MarkForCompilationPassImpl::LogNotContractableAndReturnFalse( + Cluster* from, Cluster* to, absl::string_view reason) { + VLOG(3) << EdgeContractionFailureMsg(from, to, reason); + return false; +} + +absl::StatusOr MarkForCompilationPassImpl::TryToContractEdge( + Cluster* from, Cluster* to) { + DCHECK(from->deadness_predicate().has_value() == + to->deadness_predicate().has_value()); + if (from->deadness_predicate() != to->deadness_predicate()) { + VLOG(3) << EdgeContractionFailureMsg( + from, to, + absl::StrCat( + "the two nodes have mismatching deadness: ", + deadness_analysis_->DebugString(*from->deadness_predicate()), + " and ", + deadness_analysis_->DebugString(*to->deadness_predicate()))); + return false; + } + + TF_ASSIGN_OR_RETURN(bool devices_compatible, + AreDevicesCompatible(*from, *to)); + if (!devices_compatible) { + return LogNotContractableAndReturnFalse( + from, to, "the two nodes have incompatible devices"); + } + + if (from->xla_scope().has_value() && to->xla_scope().has_value() && + *from->xla_scope() != *to->xla_scope()) { + return LogNotContractableAndReturnFalse( + from, to, "the two nodes have mismatching XLA scopes"); + } + + // Don't exceed the maximum cluster size. + if (from->cluster_size() + to->cluster_size() > + debug_options_.max_cluster_size) { + return LogNotContractableAndReturnFalse( + from, to, "the new cluster will be larger than the max cluster size"); + } + + TF_ASSIGN_OR_RETURN(bool will_introduce_cross_device_dependency, + ClusteringWillIntroduceInterDeviceDependency(*from, *to)); + + if (will_introduce_cross_device_dependency) { + return LogNotContractableAndReturnFalse( + from, to, "the new cluster will introduce a cross device dependency"); + } + + // Check if contracting this edge will break the resource variable concurrency + // semantics. In theory this is quadratic in the number of nodes, but seems + // to not be a problem in practice so far. + if (!debug_options_.ignore_resource_variable_checks) { + for (int resource_var_from : from->resource_var_operation_node_ids()) { + for (int resource_var_to : to->resource_var_operation_node_ids()) { + // If unsafe_resource_deps_ contains {A, B} then + // + // a. A and B are resource operations. + // b. A and B cannot be placed in the same cluster. + // c. There is no path from B to A in the cycles graph (but there may + // be a path from A to B). + // + // So check the legality of the edge contraction by checking if any of + // the n^2 pairs of resource variable operations are forbidden. + if (unsafe_resource_deps_.contains( + {resource_var_from, resource_var_to})) { + return LogNotContractableAndReturnFalse( + from, to, + "the new cluster would break resource variable semantics"); + } + } + } + } + + return MergeClusters(from, to); +} + +absl::Status MarkForCompilationPassImpl::Run() { + // Make sure that kernels have been registered on the JIT device. + XlaOpRegistry::RegisterCompilationKernels(); + + // Start the timer after XlaOpRegistry::RegisterCompilationKernels which does + // some one-time work. + XLA_SCOPED_LOGGING_TIMER_LEVEL("MarkForCompilationPassImpl::Run", 1); + + TF_ASSIGN_OR_RETURN(bool initialized, Initialize()); + if (!initialized) { + // Initialization exited early which means this instance of + // MarkForCompilationPassImpl is not set up to run the subsequent phases. + return absl::OkStatus(); + } + + TF_RETURN_IF_ERROR(RunEdgeContractionLoop()); + TF_RETURN_IF_ERROR(DeclusterNodes()); + TF_RETURN_IF_ERROR(CreateClusters()); + TF_RETURN_IF_ERROR(DumpDebugInfo()); + + return absl::OkStatus(); +} + +void MarkForCompilationPassImpl::DumpPostClusteringGraphs() { + DumpGraphToFile("mark_for_compilation", *graph_, flib_def_); + + // We also dump out an annotated version of the TF graph where the nodes + // names are prefixed with the cluster names. This can help visualizing the + // clustering decisions on TensorBoard. + Graph new_graph(graph_->op_registry()); + CopyGraph(*graph_, &new_graph); + + for (Node* n : new_graph.nodes()) { + if (std::optional cluster_name = + GetXlaClusterForNode(*n)) { + n->set_name(absl::StrCat(*cluster_name, "/", n->name())); + } else if (n->type_string() == "VarHandleOp") { + n->set_name(absl::StrCat("varhandle/", n->name())); + } else { + // There is room for improvement here. In particular, it may help to + // split these unclustered nodes into classes where every node in a + // specific class has edges to and from the same set of clusters. + n->set_name(absl::StrCat("unclustered/", n->name())); + } + } + + DumpGraphToFile("mark_for_compilation_annotated", new_graph, flib_def_); +} + +std::string RatioToString(int numerator, int denominator) { + return absl::StrFormat("%d / %d (%.2f%%)", numerator, denominator, + (100.0 * numerator) / denominator); +} + +void MarkForCompilationPassImpl::VLogClusteringSummary() { + if (!VLOG_IS_ON(2)) { + return; + } + + XlaAutoClusteringSummary auto_clustering_info = + GetXlaAutoClusteringSummary(*graph_); + + VLOG(2) << "*** Clustering info for graph of size " << graph_->num_nodes(); + VLOG(2) << " Built " << auto_clustering_info.clusters_size() + << " clusters, size " + << RatioToString(auto_clustering_info.clustered_node_count(), + graph_->num_nodes()); + + for (const XlaAutoClusteringSummary::Cluster& cluster : + auto_clustering_info.clusters()) { + absl::string_view cluster_name = cluster.name(); + int size = cluster.size(); + VLOG(2) << " " << cluster_name << " " + << RatioToString(size, graph_->num_nodes()); + for (const XlaAutoClusteringSummary::OpAndCount& op_count : + cluster.op_histogram()) { + VLOG(3) << " " << op_count.op() << ": " << op_count.count() + << " instances"; + } + } + + if (!auto_clustering_info.unclustered_op_histogram().empty()) { + VLOG(2) << " Unclustered nodes: " + << RatioToString(auto_clustering_info.unclustered_node_count(), + graph_->num_nodes()); + for (const XlaAutoClusteringSummary::OpAndCount& op_count : + auto_clustering_info.unclustered_op_histogram()) { + VLOG(3) << " " << op_count.op() << ": " << op_count.count() + << " instances"; + } + } + + struct EdgeInfo { + absl::string_view node_name; + std::optional cluster_name; + + absl::string_view GetClusterName() const { + return cluster_name ? *cluster_name : "[none]"; + } + + std::pair> AsPair() + const { + return {node_name, cluster_name}; + } + + bool operator<(const EdgeInfo& other) const { + return AsPair() < other.AsPair(); + } + }; + + using EdgeInfoMap = std::map>; + + EdgeInfoMap incoming_edge_infos; + EdgeInfoMap outgoing_edge_infos; + + std::set cluster_names_to_print; + + for (const Edge* e : graph_->edges()) { + const Node* from = e->src(); + std::optional from_cluster_name = + GetXlaClusterForNode(*from); + + const Node* to = e->dst(); + std::optional to_cluster_name = + GetXlaClusterForNode(*to); + + if (to_cluster_name == from_cluster_name) { + continue; + } + + if (to_cluster_name) { + incoming_edge_infos[*to_cluster_name] + [EdgeInfo{from->name(), from_cluster_name}]++; + cluster_names_to_print.insert(*to_cluster_name); + } + + if (from_cluster_name) { + outgoing_edge_infos[*from_cluster_name][{to->name(), to_cluster_name}]++; + cluster_names_to_print.insert(*from_cluster_name); + } + } + + VLOG(4) << "*** Inter-Cluster edges:"; + if (cluster_names_to_print.empty()) { + VLOG(4) << " [none]"; + } + + auto print_edge_info_set_for_cluster = [&](absl::string_view cluster_name, + const EdgeInfoMap& edge_info_map, + absl::string_view desc) { + auto it = edge_info_map.find(cluster_name); + if (it != edge_info_map.end()) { + VLOG(4) << " " << it->second.size() << " " << desc << " edges"; + for (const auto& edge_info_count_pair : it->second) { + VLOG(4) << " " << edge_info_count_pair.first.GetClusterName() << " " + << edge_info_count_pair.first.node_name << " # " + << edge_info_count_pair.second; + } + } else { + VLOG(4) << " No " << desc << " edges."; + } + }; + + for (absl::string_view cluster_name : cluster_names_to_print) { + VLOG(4) << " ** Cluster " << cluster_name; + print_edge_info_set_for_cluster(cluster_name, incoming_edge_infos, + "incoming"); + print_edge_info_set_for_cluster(cluster_name, outgoing_edge_infos, + "outgoing"); + } +} + +absl::StatusOr MarkForCompilationPassImpl::AreDevicesCompatible( + const Cluster& cluster_a, const Cluster& cluster_b) { + DeviceSet devices = cluster_a.devices(); + devices.UnionWith(cluster_b.devices()); + + TF_ASSIGN_OR_RETURN( + std::optional maybe_chosen_device, + MaybePickDeviceForXla(device_info_cache_, devices, + /*allow_mixing_unknown_and_cpu=*/false)); + if (!maybe_chosen_device.has_value()) { + return false; + } + + jit::DeviceId chosen_device = *maybe_chosen_device; + + // If we are able to pick a device `chosen_device` for the larger cluster, the + // resource operations in `cluster_a` and `cluster_b` must be placed on the + // same device as `chosen_device`. This is because the _XlaCompile and + // _XlaRun kernels are going to run on and therefore try to access the + // resource variables from `chosen_device`, which will be an error if the + // resource variables are placed on some other device. + auto resource_op_device_ok = [&](std::optional resource_op_device) { + return !resource_op_device.has_value() || + *resource_op_device == chosen_device; + }; + + return resource_op_device_ok(cluster_a.resource_op_device()) && + resource_op_device_ok(cluster_b.resource_op_device()); +} + +// Returns `true` iff we should compile `cluster`. +absl::StatusOr MarkForCompilationPassImpl::ShouldCompileClusterImpl( + const Cluster& cluster) { + TF_ASSIGN_OR_RETURN(DeviceId chosen_device, + PickDeviceForXla(device_info_cache_, cluster.devices(), + /*allow_mixing_unknown_and_cpu=*/false)); + + const DeviceType& device_type = + device_info_cache_.GetDeviceTypeFor(chosen_device); + const XlaOpRegistry::DeviceRegistration* registration = + device_info_cache_.GetCompilationDevice(chosen_device); + TF_RET_CHECK(registration) + << "chosen device = " << device_info_cache_.GetNameFor(chosen_device) + << "; device type = " << device_type.type() << "; devices (" + << device_info_cache_.DebugString(cluster.devices()); + + auto policy = registration->autoclustering_policy; + bool should_compile = + ShouldCompile(cluster.is_xla_compile_attr_true(), device_type, policy); + + if (!should_compile && device_type.type_string() == DEVICE_CPU && + global_jit_level_ > OptimizerOptions::OFF) { + static absl::once_flag once; + absl::call_once(once, [] { + LOG(WARNING) << R"((One-time warning): Not using XLA:CPU for cluster. + +If you want XLA:CPU, do one of the following: + + - set the TF_XLA_FLAGS to include "--tf_xla_cpu_global_jit", or + - set cpu_global_jit to true on this session's OptimizerOptions, or + - use experimental_jit_scope, or + - use tf.function(jit_compile=True). + +To confirm that XLA is active, pass --vmodule=xla_compilation_cache=1 (as a +proper command-line flag, not via TF_XLA_FLAGS).)"; + + MarkForCompilationPassFlags* flags = GetMarkForCompilationPassFlags(); + if (flags->tf_xla_cpu_global_jit) { + LOG(WARNING) + << "(Although the tf_xla_cpu_global_jit flag is currently enabled, " + "perhaps it wasn't enabled at process startup?)"; + } + }); + } + + VLOG(3) << (should_compile ? "Compiling" : "Not compiling") + << " cluster with device " + << device_info_cache_.GetNameFor(chosen_device); + + return should_compile; +} + +absl::StatusOr MarkForCompilationPassImpl::ShouldCompileCluster( + const Cluster& cluster) { + auto it = should_compile_cluster_cache_.find(&cluster); + if (it != should_compile_cluster_cache_.end()) { + return it->second; + } + + TF_ASSIGN_OR_RETURN(bool should_compile, ShouldCompileClusterImpl(cluster)); + should_compile_cluster_cache_.insert({&cluster, should_compile}); + return should_compile; +} + +absl::Status MarkForCompilation( + const GraphOptimizationPassOptions& options, + const MarkForCompilationPassImpl::DebugOptions& debug_options) { + Graph* graph = options.graph->get(); + FunctionLibraryDefinition* flib_def = options.flib_def; + + // Deadness analysis expects a graph with source and sink edges properly + // connected but sometimes the incoming graph does not follow this invariant. + // So fix up the source and sink edges before calling into deadness analysis. + FixupSourceAndSinkEdges(graph); + + for (Node* n : graph->nodes()) { + // See explanation on `kXlaAlreadyClustered`. + if (n->attrs().Find(kXlaAlreadyClustered)) { + return absl::OkStatus(); + } + // Skip the pass if we found TPUExecute or TPUExecuteAndUpdateVariables ops + // in the graph, which indicates the graph is produced by TPU TF-XLA bridge + // and doesn't require auto clustering. + if (n->type_string() == "TPUExecute" || + n->type_string() == "TPUExecuteAndUpdateVariables") { + return absl::OkStatus(); + } + } + + return MarkForCompilationPassImpl{ + debug_options, + graph, + flib_def, + options.session_options != nullptr ? options.session_options->env + : Env::Default(), + GetGlobalJitLevelForGraph(options), + options.session_options->config.graph_options() + .optimizer_options() + .cpu_global_jit(), + /*cluster_name_prefix=*/options.session_options != nullptr + ? options.session_options->config.experimental() + .session_metadata() + .name() + : ""} + .Run(); +} + +std::atomic* GetPointerToFuel(int64_t initial_value) { + static std::atomic* fuel = [&]() { + std::atomic* fuel = new std::atomic; + *fuel = initial_value; + return fuel; + }(); + + return fuel; +} + +} // anonymous namespace + +absl::Status MarkForCompilationPass::Run( + const GraphOptimizationPassOptions& options) { + MarkForCompilationPassFlags* flags = GetMarkForCompilationPassFlags(); + + MarkForCompilationPassImpl::DebugOptions debug_options; + debug_options.ignore_deadness_checks = + flags->tf_xla_disable_deadness_safety_checks_for_debugging; + debug_options.ignore_resource_variable_checks = + flags->tf_xla_disable_resource_variable_safety_checks_for_debugging; + debug_options.ignore_xla_compile_attr = false; + debug_options.deterministic_cluster_names = + flags->tf_xla_deterministic_cluster_names; + debug_options.max_cluster_size = flags->tf_xla_max_cluster_size; + debug_options.min_cluster_size = flags->tf_xla_min_cluster_size; + debug_options.fuel = GetPointerToFuel(flags->tf_xla_clustering_fuel); + debug_options.dump_graphs = flags->tf_xla_clustering_debug; + + return MarkForCompilation(options, debug_options); +} + +absl::Status MarkForCompilationPass::RunForTest( + const GraphOptimizationPassOptions& options, bool disable_deadness_analysis, + bool deterministic_cluster_names) { + MarkForCompilationPassFlags* flags = GetMarkForCompilationPassFlags(); + + MarkForCompilationPassImpl::DebugOptions debug_options; + debug_options.ignore_deadness_checks = disable_deadness_analysis; + debug_options.ignore_resource_variable_checks = + flags->tf_xla_disable_resource_variable_safety_checks_for_debugging; + debug_options.ignore_xla_compile_attr = true; + debug_options.deterministic_cluster_names = deterministic_cluster_names; + debug_options.max_cluster_size = flags->tf_xla_max_cluster_size; + debug_options.min_cluster_size = flags->tf_xla_min_cluster_size; + debug_options.fuel = GetPointerToFuel(flags->tf_xla_clustering_fuel); + debug_options.dump_graphs = flags->tf_xla_clustering_debug; + + return MarkForCompilation(options, debug_options); +} + +absl::flat_hash_map>* +GetAllowlistTable() { + // Table format: category name: {list of TF operations in that category} + static absl::flat_hash_map>* result = + new absl::flat_hash_map>{ + // Unary + {"PW", + {"ComplexAbs", "Angle", "Conj", "Abs", "Acos", "Acosh", "Asin", + "Atan", "Atanh", "Ceil", "Cos", "Cosh", "Sin", "Exp", "Expm1", + "Floor", "IsFinite", "IsInf", "IsNan", "Inv", "Reciprocal", "Log", + "Log1p", "Invert", "LogicalNot", "Ndtri", "Neg", "Rint", "Round", + "Rsqrt", "Sigmoid", "Sign", "Sinh", "Softplus", "Softsign", "Sqrt", + "Square", "Tan", "Tanh", "Real", "Imag", "Erf", "Erfc", "Erfinv", + "Lgamma", "Digamma", + // Binary + "Add", "AddV2", "Sub", "Mul", "Div", "Atan2", "Complex", "DivNoNan", + "MulNoNan", "FloorDiv", "Xlogy", "Xlog1py", "Xdivy", "FloorMod", + "BitwiseAnd", "BitwiseOr", "BitwiseXor", "LeftShift", "RightShift", + "LogicalAnd", "LogicalOr", "Mod", "Maximum", "Minimum", "RealDiv", + "ReciprocalGrad", "RsqrtGrad", "SqrtGrad", "TruncateDiv", + "TruncateMod", "Equal", "NotEqual", "Greater", "GreaterEqual", + "Less", "LessEqual", "SigmoidGrad", "SoftplusGrad", "SoftsignGrad", + "TanhGrad", "Pow", "SquaredDifference", "ApproximateEqual", + // Others + "AddN", "Bitcast", "Cast", "ClipByValue", "Const", "Empty", + "Identity", "IdentityN", "Relu", "Relu6", "ReluGrad", "Relu6Grad", + "LeakyReluGrad", "Elu", "EluGrad", "Selu", "SeluGrad", "Select", + "SelectV2", "Transpose", "ConjugateTranspose", + "_UnaryOpsComposition", "CollectiveReduceV2", + "CollectiveAssignGroupV2", + // The following 5 operations are converted to identity + "PlaceholderWithDefault", "PreventGradient", "StopGradient", + "Snapshot", "_EagerConst"}}, + // clang-format off + {"RED", + {"All", "Any", "Min", "Max", "Mean", "Prod", "Sum"}}, + // clang-format on + {"PWRED", + {"ArgMax", "ArgMin", "DiagPart", "Softmax", + "SparseSoftmaxCrossEntropyWithLogits", "LogSoftmax"}}, + {"REDUCEWINDOW", + {"ArgMax", "ArgMin", "DiagPart", "Softmax", + "SparseSoftmaxCrossEntropyWithLogits", "LogSoftmax"}}, + {"REDUCEWINDOWPW", {"BiasAddGrad", "LRN", "LRNGrad"}}, + {"BN", + {"FusedBatchNorm", "FusedBatchNormV2", "FusedBatchNormV3", + "_FusedBatchNormEx", "FusedBatchNormGrad", "FusedBatchNormGradV2", + "FusedBatchNormGradV3"}}, + {"Conv", {"_FusedConv2D"}}, + {"SORT", {"TopKV2"}}, // XLA version much faster then TF version. + {"MISC", + // clang-format off + {"ApproxTopK", "BroadcastTo", "ExpandDims", "Fill", "NoOp", + "Range", "Rank", "Reshape", "Shape", "ShapeN", "Size", "Squeeze", + "Transpose", "ZerosLike", "OnesLike", "BiasAdd" /*PW + Broadcast*/, + "BroadcastArgs", "BroadcastGradientArgs", "OneHot", "Concat", "ConcatV2", + "ConcatOffset", "Const", "MirrorPad", "MirrorPadGrad", "Pack", "Pad", + "PadV2", "Reverse", "ReverseV2", "ReverseSequence", "Slice", "Split", + "SplitV", "StridedSlice", "StridedSliceGrad", + "ResourceStridedSliceAssign", "Tile", "Transpose", "InvertPermutation", + "Unpack", "DeviceIndex", "TensorStridedSliceUpdate", "XlaConcatND", + "XlaSplitND", + }}}; + // clang-format on + return result; +} + +namespace testing { +void ResetClusterSequenceNumber() { + ClusterSequenceNumberGenerator::Global().Reset(); +} + +absl::flat_hash_set GetKnownXLAAllowlistOp() { + absl::flat_hash_set result{ + "AdjustContrastv2", + "AdjustHue", + "AdjustSaturation", + "Asinh", + "Assert", + "AssignAddVariableOp", + "AssignSubVariableOp", + "AssignVariableOp", + "AssignVariableXlaConcatND", + "AvgPool", + "AvgPool3D", + "AvgPool3DGrad", + "AvgPoolGrad", + "BatchMatMul", + "BatchMatMulV2", + "BatchMatMulV3", + "BatchToSpace", + "BatchToSpaceND", + "BesselI0e", + "BesselI1e", + "Betainc", + "BiasAddV1", + "Bincount", + "Bucketize", + "Case", + "CheckNumerics", + "Cholesky", + "ControlTrigger", + "Conv", + "Conv2D", + "Conv2DBackpropFilter", + "Conv2DBackpropInput", + "Conv3D", + "Conv3DBackpropFilterV2", + "Conv3DBackpropInputV2", + "Cross", + "Cumprod", + "Cumsum", + "CumulativeLogsumexp", + "DenseBincount", + "DataFormatDimMap", + "DataFormatVecPermute", + "DepthToSpace", + "DepthwiseConv2dNative", + "DepthwiseConv2dNativeBackpropFilter", + "DepthwiseConv2dNativeBackpropInput", + "Dequantize", + "Diag", + "DynamicInfeedEnqueueTupleOp", + "DynamicInfeedDequeueTupleOp", + "DynamicStitch", + "DynamicPartition", + "Einsum", + "EmptyTensorList", + "EnsureShape", + "ExtractImagePatches", + "Igamma", + "IgammaGradA", + "RandomGammaGrad", + "Igammac", + "FFT", + "FFT2D", + "FFT3D", + "FakeParam", + "FakeQuantWithMinMaxArgs", + "FakeQuantWithMinMaxArgsGradient", + "FakeQuantWithMinMaxVars", + "FakeQuantWithMinMaxVarsGradient", + "FakeQuantWithMinMaxVarsPerChannel", + "FakeQuantWithMinMaxVarsPerChannelGradient", + "Gather", + "GatherNd", + "GatherV2", + "HSVToRGB", + "IFFT", + "IFFT2D", + "IFFT3D", + "IRFFT", + "IRFFT2D", + "IRFFT3D", + "If", + "InTopKV2", + "L2Loss", + "LeakyRelu", + "LinSpace", + "ListDiff", + "LogMatrixDeterminant", + "LowerBound", + "MatMul", + "MatrixBandPart", + "MatrixDiag", + "MatrixDiagPart", + "MatrixDiagPartV2", + "MatrixDiagPartV3", + "MatrixDiagV2", + "MatrixDiagV3", + "MatrixInverse", + "MatrixSetDiag", + "MatrixSetDiagV2", + "MatrixSetDiagV3", + "MatrixSolve", + "MatrixTriangularSolve", + "MaxPool", + "MaxPool3D", + "MaxPool3DGrad", + "MaxPool3DGradGrad", + "MaxPoolGrad", + "MaxPoolGradGrad", + "MaxPoolGradGradV2", + "MaxPoolGradV2", + "MaxPoolV2", + "Multinomial", + "NextAfter", + "NonMaxSuppressionV3", + "NonMaxSuppressionV4", + "ParallelDynamicStitch", + "ParameterizedTruncatedNormal", + "PartitionedCall", + "Polygamma", + "PopulationCount", + "Qr", + "QuantizeAndDequantizeV2", + "QuantizeAndDequantizeV3", + "QuantizeAndDequantizeV4", + "RFFT", + "RFFT2D", + "RFFT3D", + "RGBToHSV", + "RandomShuffle", + "RandomStandardNormal", + "RandomUniform", + "RandomUniformInt", + "ReadVariableOp", + "ReadVariableXlaSplitND", + "ResizeBilinear", + "ResizeBilinearGrad", + "ResizeNearestNeighbor", + "ResourceApplyAdaMax", + "ResourceApplyAdadelta", + "ResourceApplyAdagrad", + "ResourceApplyAdagradDA", + "ResourceApplyAdagradV2", + "ResourceApplyAdam", + "ResourceApplyAddSign", + "ResourceApplyCenteredRMSProp", + "ResourceApplyFtrl", + "ResourceApplyFtrlV2", + "ResourceApplyGradientDescent", + "ResourceApplyKerasMomentum", + "ResourceApplyMomentum", + "ResourceApplyPowerSign", + "ResourceApplyProximalAdagrad", + "ResourceApplyProximalGradientDescent", + "ResourceApplyRMSProp", + "ResourceGather", + "ResourceScatterAdd", + "ResourceScatterDiv", + "ResourceScatterMax", + "ResourceScatterMin", + "ResourceScatterMul", + "ResourceScatterNdAdd", + "ResourceScatterNdSub", + "ResourceScatterNdUpdate", + "ResourceScatterSub", + "ResourceScatterUpdate", + "RngReadAndSkip", + "RngSkip", + "Roll", + "ScatterNd", + "SegmentSumV2", + "SegmentProdV2", + "SegmentMinV2", + "SegmentMaxV2", + "SelfAdjointEigV2", + "SoftmaxCrossEntropyWithLogits", + "SpaceToBatch", + "SpaceToBatchND", + "SpaceToDepth", + "SparseMatMul", + "SparseToDense", + "StackCloseV2", + "StackPopV2", + "StackPushV2", + "StackV2", + "StatefulPartitionedCall", + "StatefulStandardNormalV2", + "StatefulTruncatedNormal", + "StatefulUniform", + "StatefulUniformFullInt", + "StatefulUniformInt", + "StatelessCase", + "StatelessIf", + "StatelessMultinomial", + "StatelessParameterizedTruncatedNormal", + "StatelessRandomGetAlg", + "StatelessRandomGetKeyCounter", + "StatelessRandomGetKeyCounterAlg", + "StatelessRandomNormal", + "StatelessRandomNormalV2", + "StatelessRandomUniform", + "StatelessRandomUniformV2", + "StatelessRandomUniformInt", + "StatelessRandomUniformIntV2", + "StatelessRandomUniformFullInt", + "StatelessRandomUniformFullIntV2", + "StatelessTruncatedNormal", + "StatelessTruncatedNormalV2", + "StatelessWhile", + "StochasticCastToInt", + "Svd", + "SymbolicGradient", + "TensorArrayCloseV3", + "TensorArrayConcatV3", + "TensorArrayGatherV3", + "TensorArrayGradV3", + "TensorArrayReadV3", + "TensorArrayScatterV3", + "TensorArraySizeV3", + "TensorArraySplitV3", + "TensorArrayV3", + "TensorArrayWriteV3", + "TensorListConcatV2", + "TensorListElementShape", + "TensorListFromTensor", + "TensorListGather", + "TensorListGetItem", + "TensorListLength", + "TensorListPopBack", + "TensorListPushBack", + "TensorListReserve", + "TensorListSetItem", + "TensorListSplit", + "TensorListStack", + "TensorScatterAdd", + "TensorScatterMax", + "TensorScatterMin", + "TensorScatterSub", + "TensorScatterUpdate", + "ToBool", + "TridiagonalSolve", + "TridiagonalMatMul", + "TruncatedNormal", + "UniformDequantize", + "UniformQuantize", + "UniformQuantizedAdd", + "UniformQuantizedClipByValue", + "UniformQuantizedConvolution", + "UniformQuantizedDot", + "UniformRequantize", + "Unique", + "UniqueV2", + "UpperBound", + "UnsortedSegmentMax", + "UnsortedSegmentMin", + "UnsortedSegmentProd", + "UnsortedSegmentSum", + "VarIsInitializedOp", + "VariableShape", + "Where", + "While", + "XlaAllReduce", + "XlaBroadcastHelper", + "XlaCallModule", + "XlaConcatND", + "XlaConv", + "XlaConvV2", + "XlaCustomCall", + "XlaCustomCallV2", + "XlaDequantize", + "XlaDot", + "XlaDotV2", + "XlaDynamicSlice", + "XlaDynamicUpdateSlice", + "XlaEinsum", + "XlaGather", + "XlaIf", + "XlaKeyValueSort", + "XlaOptimizationBarrier", + "XlaPad", + "XlaRecv", + "XlaReduce", + "XlaReducePrecision", + "XlaReduceScatter", + "XlaReduceWindow", + "XlaRemoveDynamicDimensionSize", + "XlaReplicaId", + "XlaRngBitGenerator", + "XlaScatter", + "XlaSelectAndScatter", + "XlaSelfAdjointEig", + "XlaSend", + "XlaSetBound", + "XlaSetDynamicDimensionSize", + "XlaSharding", + "XlaSort", + "XlaSplitND", + "XlaSpmdFullToShardShape", + "XlaSpmdShardToFullShape", + "XlaSvd", + "XlaVariadicReduce", + "XlaVariadicReduceV2", + "XlaVariadicSort", + "XlaWhile", + "Zeta", + "_Arg", + "_ArrayToList", + "_ListToArray", + "_Retval"}; + return result; +} + +} // namespace testing +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/mark_for_compilation_pass.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/mark_for_compilation_pass.h new file mode 100644 index 0000000000000000000000000000000000000000..d9b01cef9f2b847e627ca30a3bed70f57cf4e85a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/mark_for_compilation_pass.h @@ -0,0 +1,63 @@ +/* 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. +==============================================================================*/ + +// An optimization passes that marks nodes that are to be compiled with +// attribute kXlaClusterAttr. Nodes with the same cluster ID will be compiled +// together. + +#ifndef TENSORFLOW_COMPILER_JIT_MARK_FOR_COMPILATION_PASS_H_ +#define TENSORFLOW_COMPILER_JIT_MARK_FOR_COMPILATION_PASS_H_ + +#include "absl/container/flat_hash_set.h" +#include "tensorflow/compiler/jit/compilability_check_util.h" +#include "tensorflow/core/common_runtime/optimization_registry.h" + +namespace tensorflow { + +// The attribute that marks nodes to be grouped into functions by the +// encapsulate subgraphs pass. +extern const char* const kXlaClusterAttr; + +// Marks a subset of nodes in the graph which are to be clustered +// with an attribute _XlaCluster= so they are picked up by the +// EncapsulateSubgraphsPass. +class MarkForCompilationPass : public GraphOptimizationPass { + public: + MarkForCompilationPass() = default; + + absl::Status Run(const GraphOptimizationPassOptions& options) override; + + private: + absl::Status RunForTest(const GraphOptimizationPassOptions& options, + bool disable_deadness_analysis, + bool deterministic_cluster_names); + + friend class MarkForCompilationPassTestHelper; +}; + +absl::flat_hash_map>* GetAllowlistTable(); + +namespace testing { +// DO NOT USE IN PRODUCTION. +// +// Resets some internal state to let us write reliable unit tests. +void ResetClusterSequenceNumber(); + +// Return a list of operation that we choose not to put into the allowlist. +absl::flat_hash_set GetKnownXLAAllowlistOp(); +} // namespace testing +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_MARK_FOR_COMPILATION_PASS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/mark_for_compilation_pass_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/mark_for_compilation_pass_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..17c3762ba258e35cd0edb3c70da91929435ba9bf --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/mark_for_compilation_pass_test.cc @@ -0,0 +1,2024 @@ +/* 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/compiler/jit/mark_for_compilation_pass.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "absl/container/flat_hash_map.h" +#include "absl/container/flat_hash_set.h" +#include "absl/log/check.h" +#include "absl/log/log.h" +#include "absl/status/status.h" +#include "absl/strings/match.h" +#include "absl/strings/str_join.h" +#include "absl/strings/str_split.h" +#include "absl/strings/string_view.h" +#include "absl/types/span.h" +#include "tensorflow/cc/framework/ops.h" +#include "tensorflow/cc/framework/scope.h" +#include "tensorflow/cc/ops/array_ops.h" +#include "tensorflow/cc/ops/const_op.h" +#include "tensorflow/cc/ops/control_flow_ops.h" +#include "tensorflow/cc/ops/control_flow_ops_internal.h" +#include "tensorflow/cc/ops/data_flow_ops.h" +#include "tensorflow/cc/ops/function_ops.h" +#include "tensorflow/cc/ops/functional_ops.h" +#include "tensorflow/cc/ops/list_ops.h" +#include "tensorflow/cc/ops/logging_ops.h" +#include "tensorflow/cc/ops/math_ops.h" +#include "tensorflow/cc/ops/no_op.h" +#include "tensorflow/cc/ops/random_ops.h" +#include "tensorflow/cc/ops/resource_variable_ops.h" +#include "tensorflow/cc/ops/sendrecv_ops.h" +#include "tensorflow/cc/ops/state_ops.h" +#include "tensorflow/compiler/jit/defs.h" +#include "tensorflow/compiler/jit/flags.h" +#include "tensorflow/compiler/jit/mark_for_compilation_pass_test_helper.h" +#include "tensorflow/compiler/jit/node_matchers.h" +#include "tensorflow/compiler/jit/xla_cluster_util.h" +#include "tensorflow/compiler/tf2xla/xla_op_registry.h" +#include "xla/tsl/lib/core/status_test_util.h" +#include "tensorflow/core/common_runtime/graph_def_builder_util.h" +#include "tensorflow/core/framework/function.h" +#include "tensorflow/core/framework/node_def_builder.h" +#include "tensorflow/core/framework/node_def_util.h" +#include "tensorflow/core/framework/op.h" +#include "tensorflow/core/framework/tensor.h" +#include "tensorflow/core/framework/tensor_shape.h" +#include "tensorflow/core/graph/algorithm.h" +#include "tensorflow/core/graph/graph.h" +#include "tensorflow/core/graph/graph_def_builder.h" +#include "tensorflow/core/graph/node_builder.h" +#include "tensorflow/core/platform/errors.h" +#include "tensorflow/core/platform/test.h" + +using ::tensorflow::testing::FindNodeByName; + +namespace tensorflow { +namespace { + +static bool Initialized = [] { + tensorflow::GetXlaDeviceFlags()->tf_xla_enable_xla_devices = true; + return true; +}(); + +REGISTER_OP("UncompilableNullary").Output("o: float"); +REGISTER_OP("UncompilableUnary").Input("a: float").Output("o: float"); + +std::unordered_map GetClusters(const Graph& graph) { + std::unordered_map ids; + for (Node* node : graph.nodes()) { + std::string cluster; + if (TryGetNodeAttr(node->attrs(), kXlaClusterAttr, &cluster)) { + CHECK(!cluster.empty()); + ids[node->name()] = cluster; + } + } + + if (VLOG_IS_ON(2)) { + VLOG(2) << "Clusters:"; + for (const auto& p : ids) { + VLOG(2) << " " << p.first << " -> " << p.second; + } + } + return ids; +} + +std::set GetClusterNames(const Graph& graph) { + std::set names; + for (Node* node : graph.nodes()) { + std::string cluster; + if (TryGetNodeAttr(node->attrs(), kXlaClusterAttr, &cluster)) { + CHECK(!cluster.empty()); + names.insert(cluster); + } + } + return names; +} + +absl::flat_hash_map> GetClusterSets( + const Graph& g, std::vector* cluster_names = nullptr) { + CHECK(cluster_names == nullptr || cluster_names->empty()); + absl::flat_hash_map> cluster_sets; + for (const auto& p : GetClusters(g)) { + cluster_sets[p.second].push_back(p.first); + } + for (auto& p : cluster_sets) { + if (cluster_names != nullptr) { + cluster_names->push_back(p.first); + } + std::sort(p.second.begin(), p.second.end()); + } + if (cluster_names != nullptr) { + std::sort(cluster_names->begin(), cluster_names->end()); + } + return cluster_sets; +} + +TEST(XlaCompilationTest, Chains) { + std::unique_ptr graph(new Graph(OpRegistry::Global())); + { + GraphDefBuilder builder(GraphDefBuilder::kFailImmediately); + Node* a = + ops::SourceOp("UncompilableNullary", builder.opts().WithName("A")); + Node* b = ops::UnaryOp("Relu", a, builder.opts().WithName("B")); + Node* c = ops::UnaryOp("Relu", b, builder.opts().WithName("C")); + Node* d = + ops::UnaryOp("UncompilableUnary", c, builder.opts().WithName("D")); + Node* e = ops::UnaryOp("Relu", d, builder.opts().WithName("E")); + ops::UnaryOp("Relu", e, builder.opts().WithName("F")); + TF_EXPECT_OK(GraphDefBuilderToGraph(builder, graph.get())); + } + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + auto clusters = GetClusters(*graph); + EXPECT_EQ(4, clusters.size()); + EXPECT_EQ(clusters["B"], clusters["C"]); + EXPECT_EQ(clusters["E"], clusters["F"]); + EXPECT_NE(clusters["B"], clusters["E"]); + EXPECT_TRUE(clusters.find("A") == clusters.cend()); + EXPECT_TRUE(clusters.find("D") == clusters.cend()); +} + +TEST(XlaCompilationTest, UncompilableCycles) { + std::unique_ptr graph(new Graph(OpRegistry::Global())); + { + GraphDefBuilder builder(GraphDefBuilder::kFailImmediately); + Node* a = ops::SourceOp("Const", builder.opts() + .WithName("A") + .WithAttr("dtype", DT_FLOAT) + .WithAttr("value", Tensor())); + Node* b = + ops::UnaryOp("UncompilableUnary", a, builder.opts().WithName("B")); + ops::BinaryOp("MatMul", a, b, builder.opts().WithName("C")); + TF_EXPECT_OK(GraphDefBuilderToGraph(builder, graph.get())); + } + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + auto clusters = GetClusters(*graph); + + EXPECT_TRUE(clusters.empty()); +} + +TEST(XlaCompilationTest, CompilableCycles) { + std::unique_ptr graph(new Graph(OpRegistry::Global())); + { + GraphDefBuilder builder(GraphDefBuilder::kFailImmediately); + Node* a = ops::SourceOp("Const", builder.opts() + .WithName("A") + .WithAttr("dtype", DT_FLOAT) + .WithAttr("value", Tensor())); + Node* b = ops::UnaryOp("Relu", a, builder.opts().WithName("B")); + ops::BinaryOp("MatMul", a, b, builder.opts().WithName("C")); + TF_EXPECT_OK(GraphDefBuilderToGraph(builder, graph.get())); + } + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + auto clusters = GetClusters(*graph); + + EXPECT_EQ(3, clusters.size()); + EXPECT_EQ(clusters["A"], clusters["B"]); + EXPECT_EQ(clusters["A"], clusters["C"]); +} + +TEST(XlaCompilationTest, StringUnsupported) { + std::unique_ptr graph(new Graph(OpRegistry::Global())); + { + GraphDefBuilder builder(GraphDefBuilder::kFailImmediately); + Node* a = ops::SourceOp( + "Const", builder.opts() + .WithName("A") + .WithAttr("dtype", DT_STRING) + .WithAttr("value", Tensor(DT_STRING, TensorShape()))); + Node* b = ops::UnaryOp("EncodeBase64", a, builder.opts().WithName("B")); + ops::BinaryOp("StringSplit", a, b, builder.opts().WithName("C")); + TF_EXPECT_OK(GraphDefBuilderToGraph(builder, graph.get())); + } + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + auto clusters = GetClusters(*graph); + EXPECT_TRUE(clusters.empty()); +} + +TEST(XlaCompilationTest, WhereUnsupported) { + std::unique_ptr graph(new Graph(OpRegistry::Global())); + { + GraphDefBuilder builder(GraphDefBuilder::kFailImmediately); + Node* a = ops::SourceOp("Const", builder.opts() + .WithName("A") + .WithAttr("dtype", DT_INT32) + .WithAttr("value", Tensor())); + Node* b = ops::UnaryOp("Where", a, builder.opts().WithName("B")); + ops::BinaryOp("Gather", b, a, builder.opts().WithName("C")); + TF_EXPECT_OK(GraphDefBuilderToGraph(builder, graph.get())); + } + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + auto clusters = GetClusters(*graph); + EXPECT_TRUE(!clusters.empty()); +} + +TEST(XlaCompilationTest, HalfSupported) { + std::unique_ptr graph(new Graph(OpRegistry::Global())); + { + GraphDefBuilder builder(GraphDefBuilder::kFailImmediately); + Tensor t(DT_HALF, TensorShape()); + t.scalar()() = static_cast(0.0f); + Node* a = ops::SourceOp("Const", builder.opts() + .WithName("A") + .WithAttr("dtype", DT_HALF) + .WithAttr("value", t)); + Node* b = ops::UnaryOp("Neg", a, builder.opts().WithName("B")); + ops::BinaryOp("MatMul", a, b, builder.opts().WithName("C")); + TF_EXPECT_OK(GraphDefBuilderToGraph(builder, graph.get())); + } + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + auto clusters = GetClusters(*graph); + EXPECT_FALSE(clusters.empty()); +} + +// Tests that PartitionedCalls are only marked for compilation if every node +// inside the function can be compiled. +TEST(XlaCompilationTest, PartitionedCallUnsupported) { + FunctionDef compilable = FunctionDefHelper::Define( + "CompilableFn", {"n_a:float", "n_b:float"}, {"n_c:float"}, {}, + {{{"n_c"}, "Add", {"n_a", "n_b"}, {{"T", DT_FLOAT}}}}); + FunctionDef uncompilable = + FunctionDefHelper::Define("UncompilableFn", {"n_a:float"}, {"n_c:float"}, + {}, {{{"n_c"}, "UncompilableUnary", {"n_a"}}}); + + FunctionDefLibrary flib; + *flib.add_function() = compilable; + *flib.add_function() = uncompilable; + FunctionLibraryDefinition flib_def(OpRegistry::Global(), flib); + + std::unique_ptr graph(new Graph(&flib_def)); + Scope root = Scope::NewRootScope().ExitOnError(); + Output a = ops::Placeholder(root.WithOpName("A"), DT_FLOAT); + + NameAttrList b_name_attr; + b_name_attr.set_name("CompilableFn"); + ops::PartitionedCall b(root.WithOpName("B"), {a, a}, {DT_FLOAT}, b_name_attr); + NameAttrList c_name_attr; + c_name_attr.set_name("UncompilableFn"); + + ops::PartitionedCall c(root.WithOpName("C"), {a}, {DT_FLOAT}, c_name_attr); + Output d = ops::Add(root.WithOpName("D"), b.output.front(), c.output.front()); + + TF_ASSERT_OK(root.ToGraph(graph.get())); + TF_ASSERT_OK( + MarkForCompilationPassTestHelper::MarkForCompilation(&graph, &flib_def)); + auto clusters = GetClusters(*graph); + + EXPECT_EQ(2, clusters.size()); + EXPECT_FALSE(clusters["B"].empty()); + EXPECT_TRUE(clusters["C"].empty()); + EXPECT_EQ(clusters["B"], clusters["D"]); +} + +TEST(XlaCompilationTest, FunctionCalls) { + FunctionDef compilable = FunctionDefHelper::Define( + "CompilableFn", {"n_a:float", "n_b:float"}, {"n_c:float"}, {}, + {{{"n_c"}, "Add", {"n_a", "n_b"}, {{"T", DT_FLOAT}}}}); + FunctionDef uncompilable = + FunctionDefHelper::Define("UncompilableFn", {"n_a:float"}, {"n_c:float"}, + {}, {{{"n_c"}, "UncompilableUnary", {"n_a"}}}); + FunctionDef noinline = compilable; + noinline.mutable_signature()->set_name("NoInlineFn"); + AddAttr("_noinline", static_cast(true), noinline.mutable_attr()); + + FunctionDefLibrary flib; + *flib.add_function() = compilable; + *flib.add_function() = uncompilable; + *flib.add_function() = noinline; + FunctionLibraryDefinition flib_def(OpRegistry::Global(), flib); + + std::unique_ptr graph(new Graph(&flib_def)); + { + GraphDefBuilder builder(GraphDefBuilder::kFailImmediately, &flib_def); + Node* a = + ops::SourceOp("UncompilableNullary", builder.opts().WithName("A")); + Node* b = ops::BinaryOp("CompilableFn", a, a, builder.opts().WithName("B")); + Node* c = ops::UnaryOp("Relu", b, builder.opts().WithName("C")); + ops::UnaryOp("UncompilableFn", c, builder.opts().WithName("D")); + ops::BinaryOp("NoInlineFn", c, c, builder.opts().WithName("E")); + TF_EXPECT_OK(GraphDefBuilderToGraph(builder, graph.get())); + } + + TF_ASSERT_OK( + MarkForCompilationPassTestHelper::MarkForCompilation(&graph, &flib_def)); + auto clusters = GetClusters(*graph); + + EXPECT_EQ(2, clusters.size()); + EXPECT_FALSE(clusters["C"].empty()); + EXPECT_EQ(clusters["C"], clusters["E"]); + EXPECT_TRUE(clusters.find("A") == clusters.cend()); + EXPECT_TRUE(clusters.find("B") == clusters.cend()); + EXPECT_TRUE(clusters.find("D") == clusters.cend()); +} + +TEST(XlaCompilationTest, CallXlaDeviceFuncWithResourceOp) { + FunctionDef compilable = FunctionDefHelper::Define( + "FnWithResourceOp", {"var:resource", "val:float"}, {"retval:float"}, {}, + {{{"assign_op"}, + "AssignVariableOp", + {"var", "val"}, + {{"dtype", DT_FLOAT}}}, + {{"retval"}, "Identity", {"val"}, {{"T", DT_FLOAT}}, {"assign_op"}}}); + + FunctionDefLibrary flib; + *flib.add_function() = compilable; + FunctionLibraryDefinition flib_def(OpRegistry::Global(), flib); + + std::unique_ptr graph(new Graph(&flib_def)); + { + GraphDefBuilder builder(GraphDefBuilder::kFailImmediately, &flib_def); + Node* resource = + ops::SourceOp("VarHandleOp", builder.opts() + .WithName("varhandle") + .WithAttr("dtype", DT_FLOAT) + .WithAttr("shape", TensorShape({}))); + + Tensor const_tensor(DT_FLOAT, TensorShape({})); + const_tensor.scalar()() = 42.0f; + Node* value = ops::SourceOp("Const", builder.opts() + .WithName("const") + .WithAttr("value", const_tensor) + .WithAttr("dtype", DT_FLOAT)); + + Node* call = ops::BinaryOp("FnWithResourceOp", resource, value, + builder.opts().WithName("A")); + Node* tanh0 = ops::UnaryOp("Tanh", call, builder.opts().WithName("tanh0")); + Node* tanh1 = ops::UnaryOp("Tanh", tanh0, builder.opts().WithName("tanh1")); + ops::UnaryOp("Tanh", tanh1, builder.opts().WithName("tanh2")); + TF_EXPECT_OK(GraphDefBuilderToGraph(builder, graph.get())); + } + + std::string xla_cpu_device = "/job:worker/replica:0/task:0/device:XLA_CPU:0"; + testing::FindNodeByName(graph.get(), "A") + ->set_assigned_device_name(xla_cpu_device); + testing::FindNodeByName(graph.get(), "tanh0") + ->set_assigned_device_name(xla_cpu_device); + testing::FindNodeByName(graph.get(), "tanh1") + ->set_assigned_device_name(xla_cpu_device); + testing::FindNodeByName(graph.get(), "tanh2") + ->set_assigned_device_name(xla_cpu_device); + + TF_ASSERT_OK( + MarkForCompilationPassTestHelper::MarkForCompilation(&graph, &flib_def)); + auto clusters = GetClusters(*graph); + + EXPECT_NE(clusters["A"], ""); +} + +static absl::Status GradForUnaryCwise( + FunctionDef* g, std::vector nodes) { + for (auto& n : nodes) { + if (n.attr.empty()) { + n.attr = {{"T", DT_FLOAT}}; + } + } + *g = FunctionDefHelper::Define( + // Arg defs + {"x: float", "dy: float"}, + // Ret val defs + {"dx: float"}, + // Attr defs + {}, + // Nodes + nodes); + return absl::OkStatus(); +} + +// A gradient containing only supported operators +absl::Status SupportedGrad(const AttrSlice& attrs, FunctionDef* g) { + // clang-format off + return GradForUnaryCwise(g, { + {{"y"}, "Tanh", {"x"}}, + {{"y2"}, "Square", {"y"}, {}, {"dy"}}, + FunctionDefHelper::Const("one", 1.0f), + {{"a"}, "Sub", {"one", "y2"}}, + {{"dx"}, "Mul", {"dy", "a"}}, + }); + // clang-format on +} +REGISTER_OP_GRADIENT("Supported", SupportedGrad); + +// A gradient containing an unsupported operator. +absl::Status UnsupportedGrad(const AttrSlice& attrs, FunctionDef* g) { + // clang-format off + return GradForUnaryCwise(g, { + {{"y"}, "Tanh", {"x"}}, + {{"y2"}, "UncompilableUnary", {"y"}, {}, {"dy"}}, + FunctionDefHelper::Const("one", 1.0f), + {{"a"}, "Sub", {"one", "y2"}}, + {{"dx"}, "Mul", {"dy", "a"}}, + }); + // clang-format on +} +REGISTER_OP_GRADIENT("Unsupported", UnsupportedGrad); + +TEST(XlaCompilationTest, SymbolicGradients) { + std::unique_ptr graph(new Graph(OpRegistry::Global())); + { + GraphDefBuilder builder(GraphDefBuilder::kFailImmediately); + Node* a = + ops::SourceOp("UncompilableNullary", builder.opts().WithName("A")); + + // Builds a Symbolic gradient for Supported + NodeBuilder b_builder("B", "SymbolicGradient", + builder.opts().op_registry()); + NameAttrList b_name_attr; + b_name_attr.set_name("Supported"); + b_builder.Attr("f", b_name_attr); + b_builder.Attr("Tin", {DT_FLOAT, DT_FLOAT}); + b_builder.Attr("Tout", {DT_FLOAT}); + b_builder.Input({a, a}); + Node* b = builder.opts().FinalizeBuilder(&b_builder); + + Node* c = ops::UnaryOp("Relu", b, builder.opts().WithName("C")); + + // Builds a Symbolic gradient for Unsupported + NodeBuilder d_builder("D", "SymbolicGradient", + builder.opts().op_registry()); + NameAttrList d_name_attr; + d_name_attr.set_name("Unsupported"); + d_builder.Attr("f", d_name_attr); + d_builder.Attr("Tin", {DT_FLOAT, DT_FLOAT}); + d_builder.Attr("Tout", {DT_FLOAT}); + d_builder.Input({c, c}); + builder.opts().FinalizeBuilder(&d_builder); + + TF_EXPECT_OK(GraphDefBuilderToGraph(builder, graph.get())); + } + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + auto clusters = GetClusters(*graph); + + EXPECT_EQ(2, clusters.size()); + EXPECT_FALSE(clusters["B"].empty()); + EXPECT_EQ(clusters["B"], clusters["C"]); + EXPECT_TRUE(clusters.find("A") == clusters.cend()); + EXPECT_TRUE(clusters.find("D") == clusters.cend()); +} + +TEST(XlaCompilationTest, Loops) { + // Regression test for b/32350199, where the autoclustering code introduced a + // deadlock in a graph containing a while loop. + Scope root = Scope::NewRootScope().ExitOnError(); + auto a = ops::Placeholder(root.WithOpName("A"), DT_FLOAT); + auto b = ops::Placeholder(root.WithOpName("B"), DT_FLOAT); + auto c = ops::Add(root.WithOpName("C"), a, b); + auto enter = ops::internal::Enter(root, c, "aframe"); + auto next_iter = ops::NextIteration(root, enter); + auto exit = ops::internal::Exit(root, next_iter); + auto d = ops::Add(root.WithOpName("D"), c, exit); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_EXPECT_OK(root.ToGraph(graph.get())); + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + auto clusters = GetClusters(*graph); + + // Nothing should be compiled. In particular, 'd' and 'c' must not be + // compiled. + EXPECT_EQ(0, clusters.size()); +} + +TEST(XlaCompilationTest, CyclesWithAllDifferentScopesGlobalJitOverridden) { + std::unique_ptr graph(new Graph(OpRegistry::Global())); + { + GraphDefBuilder builder(GraphDefBuilder::kFailImmediately); + Node* a = ops::SourceOp("Const", builder.opts() + .WithName("A") + .WithAttr("dtype", DT_FLOAT) + .WithAttr("value", Tensor()) + .WithAttr(kXlaScopeAttr, "ScopeA")); + Node* b = ops::UnaryOp( + "Relu", a, + builder.opts().WithName("B").WithAttr(kXlaScopeAttr, "ScopeB")); + ops::BinaryOp( + "MatMul", a, b, + builder.opts().WithName("C").WithAttr(kXlaScopeAttr, "ScopeC")); + CHECK_OK(GraphDefBuilderToGraph(builder, graph.get())); + } + + FunctionDefLibrary flib; + FunctionLibraryDefinition flib_def(graph->op_registry(), flib); + TF_ASSERT_OK( + MarkForCompilationPassTestHelper::MarkForCompilation(&graph, &flib_def)); + auto clusters = GetClusters(*graph); + + // The computation is: C = A + relu(A) + // where A sits in ScopeA, relu(A) sits in ScopeB, and C sits in ScopeC. + // In this case, the GlobalJitLevel overrides the scopes to cluster while + // ignoring scopes. + EXPECT_EQ(3, clusters.size()); + EXPECT_EQ(clusters["A"], clusters["B"]); + EXPECT_EQ(clusters["A"], clusters["C"]); +} + +TEST(XlaCompilationTest, CyclesWithAllDifferentScopes) { + std::unique_ptr graph(new Graph(OpRegistry::Global())); + { + GraphDefBuilder builder(GraphDefBuilder::kFailImmediately); + Node* a = ops::SourceOp("Const", builder.opts() + .WithName("A") + .WithAttr("dtype", DT_FLOAT) + .WithAttr("value", Tensor()) + .WithAttr(kXlaScopeAttr, "ScopeA")); + Node* b = ops::UnaryOp( + "Relu", a, + builder.opts().WithName("B").WithAttr(kXlaScopeAttr, "ScopeB")); + ops::BinaryOp( + "MatMul", a, b, + builder.opts().WithName("C").WithAttr(kXlaScopeAttr, "ScopeC")); + CHECK_OK(GraphDefBuilderToGraph(builder, graph.get())); + } + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation( + &graph, MarkForCompilationPassTestHelper::Options().WithNoGlobalJit())); + auto clusters = GetClusters(*graph); + + // The computation is: C = A + relu(A) + // where A sits in ScopeA, relu(A) sits in ScopeB, and C sits in ScopeC. + // In this case, we cannot fuse anything, and there are no clusters. + EXPECT_EQ(0, clusters.size()); +} + +TEST(XlaCompilationTest, CyclesWithSplittingScopes) { + std::unique_ptr graph(new Graph(OpRegistry::Global())); + { + GraphDefBuilder builder(GraphDefBuilder::kFailImmediately); + Node* a = ops::SourceOp("Const", builder.opts() + .WithName("A") + .WithAttr("dtype", DT_FLOAT) + .WithAttr("value", Tensor()) + .WithAttr(kXlaCompileAttr, true) + .WithAttr(kXlaScopeAttr, "Scope1")); + Node* b = ops::UnaryOp("Relu", a, + builder.opts() + .WithName("B") + .WithAttr(kXlaCompileAttr, true) + .WithAttr(kXlaScopeAttr, "Scope1")); + Node* c = ops::BinaryOp("MatMul", a, b, + builder.opts() + .WithName("C") + .WithAttr(kXlaCompileAttr, true) + .WithAttr(kXlaScopeAttr, "Scope2")); + ops::BinaryOp("Add", b, c, + builder.opts() + .WithName("D") + .WithAttr(kXlaCompileAttr, true) + .WithAttr(kXlaScopeAttr, "Scope2")); + CHECK_OK(GraphDefBuilderToGraph(builder, graph.get())); + } + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation( + &graph, MarkForCompilationPassTestHelper::Options().WithNoGlobalJit())); + auto clusters = GetClusters(*graph); + + // The computation is: D = relu(A) + (A @ relu(A)) + // where A and relu(A) are in Scope1, and the @, + ops are in Scope2. + // In this case, we can fuse the A and relu(A), and we can fuse the + // second half of the operations; there are two clusters. + EXPECT_EQ(4, clusters.size()); + EXPECT_EQ(clusters["A"], clusters["B"]); + EXPECT_NE(clusters["A"], clusters["C"]); + EXPECT_EQ(clusters["C"], clusters["D"]); +} + +TEST(XlaCompilationTest, CyclesWithDifferentScopesAndBridge) { + std::unique_ptr graph(new Graph(OpRegistry::Global())); + { + GraphDefBuilder builder(GraphDefBuilder::kFailImmediately); + Node* a = ops::SourceOp("Const", builder.opts() + .WithName("A") + .WithAttr("dtype", DT_FLOAT) + .WithAttr("value", Tensor()) + .WithAttr(kXlaCompileAttr, true) + .WithAttr(kXlaScopeAttr, "ScopeA")); + Node* b = ops::UnaryOp("Relu", a, + builder.opts() + .WithName("B") + .WithAttr(kXlaCompileAttr, true) + .WithAttr(kXlaScopeAttr, "ScopeB")); + ops::BinaryOp("MatMul", a, b, builder.opts().WithName("C")); + CHECK_OK(GraphDefBuilderToGraph(builder, graph.get())); + } + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation( + &graph, MarkForCompilationPassTestHelper::Options().WithNoGlobalJit())); + auto clusters = GetClusters(*graph); + + // The computation is: C = A @ relu(A) + // where A sits in ScopeA, relu(A) sits in ScopeB, and C sits in ScopeC. + // In this case, we cannot fuse anything. + EXPECT_EQ(2, clusters.size()); + EXPECT_NE(clusters["A"], clusters["B"]); + EXPECT_NE(clusters["B"], clusters["C"]); +} + +TEST(XlaCompilationTest, DontClusterNodesWithMismatchingDeadness) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Output cond_a = ops::Placeholder(root.WithOpName("cond_a"), DT_BOOL); + Output cond_b = ops::Placeholder(root.WithOpName("cond_b"), DT_BOOL); + + Output value = ops::Placeholder(root.WithOpName("value"), DT_FLOAT); + + ops::Switch switch_a(root.WithOpName("switch_a"), value, cond_a); + ops::Switch switch_b(root.WithOpName("switch_b"), value, cond_b); + + Output tanh_a0 = ops::Tanh(root.WithOpName("tan_a0"), switch_a.output_true); + Output tanh_a1 = ops::Tanh(root.WithOpName("tan_a1"), tanh_a0); + + Output tanh_b0 = ops::Tanh(root.WithOpName("tan_b0"), switch_b.output_true); + Output tanh_b1 = ops::Tanh(root.WithOpName("tan_b1"), tanh_b0); + + Output add = ops::Add(root.WithOpName("add"), tanh_a1, tanh_b1); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_EXPECT_OK(root.ToGraph(graph.get())); + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation( + &graph, + MarkForCompilationPassTestHelper::Options().WithDeadnessAnalysis())); + auto clusters = GetClusters(*graph); + + EXPECT_NE(clusters["tan_a0"], ""); + EXPECT_NE(clusters["tan_a1"], ""); + EXPECT_NE(clusters["tan_b0"], ""); + EXPECT_NE(clusters["tan_b1"], ""); + + EXPECT_EQ(clusters["tan_a0"], clusters["tan_a1"]); + EXPECT_EQ(clusters["tan_b0"], clusters["tan_b1"]); + + EXPECT_NE(clusters["tan_a0"], clusters["tan_b0"]); +} + +TEST(XlaCompilationTest, ClusterNodesWithMismatchingInputDeadness) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Output cond_a = ops::Placeholder(root.WithOpName("cond_a"), DT_BOOL); + Output cond_b = ops::Placeholder(root.WithOpName("cond_b"), DT_BOOL); + + Output value = ops::Placeholder(root.WithOpName("value"), DT_FLOAT); + + ops::Switch switch_a(root.WithOpName("switch_a"), value, cond_a); + ops::Switch switch_b(root.WithOpName("switch_b"), value, cond_b); + + Output add_a = ops::Add(root.WithOpName("add_a"), switch_a.output_true, + switch_b.output_true); + Output add_b = ops::Add(root.WithOpName("add_b"), switch_a.output_true, + switch_b.output_true); + Output add = ops::Add(root.WithOpName("add_c"), add_a, add_b); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_EXPECT_OK(root.ToGraph(graph.get())); + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation( + &graph, + MarkForCompilationPassTestHelper::Options().WithDeadnessAnalysis())); + auto clusters = GetClusters(*graph); + + EXPECT_NE(clusters["add_a"], ""); + EXPECT_NE(clusters["add_b"], ""); + EXPECT_NE(clusters["add_c"], ""); + + EXPECT_EQ(clusters["add_a"], clusters["add_b"]); + EXPECT_EQ(clusters["add_b"], clusters["add_c"]); +} + +namespace { +Node* MakeRead(const Scope& scope, const std::string& id, + Node** var_handle_op = nullptr) { + Output var_handle = + ops::VarHandleOp(scope.WithOpName("Var" + id), DT_FLOAT, TensorShape({})); + Output read = + ops::ReadVariableOp(scope.WithOpName("Read" + id), var_handle, DT_FLOAT); + if (var_handle_op) { + *var_handle_op = var_handle.node(); + } + return read.node(); +} + +Node* MakeWrite(const Scope& scope, const std::string& id) { + Output var_handle = + ops::VarHandleOp(scope.WithOpName("Var" + id), DT_FLOAT, TensorShape({})); + Output value_to_write = + ops::Const(scope.WithOpName("ValueToAssign" + id), 1.0f); + ops::AssignVariableOp assign_op(scope.WithOpName("Assignment" + id), + var_handle, value_to_write); + return assign_op.operation.node(); +} + +Node* MakeNeutral(const Scope& scope, const std::string& id) { + return ops::Const(scope.WithOpName("Const" + id), 42.0f).node(); +} +} // namespace + +TEST(XlaCompilationTest, ResourcesClusteringAllowed) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Node* read = MakeRead(root, "R"); + Node* write = MakeWrite(root, "W"); + + root.graph()->AddControlEdge(read, write); + + FixupSourceAndSinkEdges(root.graph()); + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_EXPECT_OK(root.ToGraph(graph.get())); + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + absl::flat_hash_map> cluster_sets = + GetClusterSets(*graph); + ASSERT_EQ(cluster_sets.size(), 1); + std::vector expected_clustered_nodes = {"AssignmentW", "ReadR", + "ValueToAssignW"}; + ASSERT_EQ(cluster_sets.begin()->second, expected_clustered_nodes); +} + +TEST(XlaCompilationTest, ResourcesClusteringDisallowed) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Node* read = MakeRead(root, "R"); + Node* write = MakeWrite(root, "W"); + + root.graph()->AddControlEdge(write, read); + + FixupSourceAndSinkEdges(root.graph()); + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_EXPECT_OK(root.ToGraph(graph.get())); + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + absl::flat_hash_map> cluster_sets = + GetClusterSets(*graph); + ASSERT_EQ(cluster_sets.size(), 0); +} + +TEST(XlaCompilationTest, ChainOfOps) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Node* write_0 = MakeWrite(root, "W0"); + Node* neutral_0 = MakeNeutral(root, "N0"); + Node* read_0 = MakeRead(root, "R0"); + Node* write_1 = MakeWrite(root, "W1"); + Node* neutral_1 = MakeNeutral(root, "N1"); + Node* read_1 = MakeRead(root, "R1"); + + root.graph()->AddControlEdge(write_0, neutral_0); + root.graph()->AddControlEdge(neutral_0, read_0); + root.graph()->AddControlEdge(read_0, write_1); + root.graph()->AddControlEdge(write_1, neutral_1); + root.graph()->AddControlEdge(neutral_1, read_1); + + FixupSourceAndSinkEdges(root.graph()); + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_EXPECT_OK(root.ToGraph(graph.get())); + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + + std::vector cluster_names; + absl::flat_hash_map> cluster_sets = + GetClusterSets(*graph, &cluster_names); + + ASSERT_EQ(cluster_sets.size(), 1); + + std::vector expected_clustered_nodes_a = { + "AssignmentW1", "ConstN0", "ReadR0", "ValueToAssignW1"}; + ASSERT_EQ(cluster_sets[cluster_names[0]], expected_clustered_nodes_a); +} + +TEST(XlaCompilationTest, IllegalCycle_UsefulErrorMessage) { + std::unique_ptr graph(new Graph(OpRegistry::Global())); + Scope root = Scope::NewRootScope().ExitOnError(); + { + auto BuildNoopNode = [](absl::string_view name, Graph* graph) { + NodeDefBuilder builder(name, "NoOp"); + NodeDef def; + CHECK_OK(builder.Finalize(&def)); + + absl::Status status; + Node* node = graph->AddNode(def, &status); + CHECK_OK(status); + return node; + }; + + Node* a = BuildNoopNode("a", graph.get()); + Node* b = BuildNoopNode("b", graph.get()); + Node* c = BuildNoopNode("c", graph.get()); + graph->AddControlEdge(a, b); + graph->AddControlEdge(b, c); + graph->AddControlEdge(c, a); + } + + TF_EXPECT_OK(root.ToGraph(graph.get())); + + absl::Status status = + MarkForCompilationPassTestHelper::MarkForCompilation(&graph); + EXPECT_FALSE(status.ok()); + EXPECT_TRUE(absl::StrContains(status.ToString(), + "Edge from c to a would create a cycle.\n" + "+-> a\n" + "| b\n" + "+-- c\n")); +} + +TEST(XlaCompilationTest, Retval) { + std::unique_ptr graph(new Graph(OpRegistry::Global())); + { + GraphDefBuilder builder(GraphDefBuilder::kFailImmediately); + Node* a = ops::SourceOp("Const", builder.opts() + .WithName("A") + .WithAttr("dtype", DT_FLOAT) + .WithAttr("value", Tensor())); + Node* b = ops::UnaryOp("Relu", a, builder.opts().WithName("B")); + ops::UnaryOp("_Retval", b, + builder.opts() + .WithName("R") + .WithAttr("T", DT_FLOAT) + .WithAttr("index", 0)); + + TF_EXPECT_OK(GraphDefBuilderToGraph(builder, graph.get())); + } + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + auto clusters = GetClusters(*graph); + + EXPECT_TRUE(clusters.empty()); +} + +TEST(XlaCompilationTest, DontCountIdentityOps) { + std::unique_ptr graph(new Graph(OpRegistry::Global())); + Scope root = Scope::NewRootScope().ExitOnError(); + { + auto a = ops::_Arg(root.WithOpName("A"), DT_INT32, 0); + auto b = ops::Identity(root.WithOpName("B"), a); + auto c = ops::Identity(root.WithOpName("C"), b); + auto r = ops::_Retval(root.WithOpName("R"), c, 0); + } + TF_ASSERT_OK(root.ToGraph(graph.get())); + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + auto clusters = GetClusters(*graph); + + EXPECT_TRUE(clusters.empty()); +} + +TEST(XlaCompilationTest, ConstOp) { + // valid data type + { + std::unique_ptr graph(new Graph(OpRegistry::Global())); + Scope root = Scope::NewRootScope().ExitOnError(); + auto c = ops::Const(root.WithOpName("const"), 0.5f); + c.node()->AddAttr(kXlaCompileAttr, true); + TF_ASSERT_OK(root.ToGraph(graph.get())); + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + EXPECT_EQ(1, GetClusters(*graph).size()); + } + + // invalid data type + { + std::unique_ptr graph(new Graph(OpRegistry::Global())); + Scope root = Scope::NewRootScope().ExitOnError(); + auto c = ops::Const(root.WithOpName("const"), std::string("string")); + c.node()->AddAttr(kXlaCompileAttr, true); + TF_ASSERT_OK(root.ToGraph(graph.get())); + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + EXPECT_TRUE(GetClusters(*graph).empty()); + } +} + +TEST(XlaCompilationTest, DontClusterIdentityWithRefInput) { + Scope root = Scope::NewRootScope().ExitOnError(); + Output variable = ops::Variable(root.WithOpName("variable"), + PartialTensorShape{}, DT_FLOAT); + Output read = ops::Identity(root.WithOpName("read"), variable); + Output neg = ops::Negate(root.WithOpName("negate"), read); + Output add = ops::Add(root.WithOpName("add"), neg, neg); + std::unique_ptr graph(new Graph(OpRegistry::Global())); + + TF_ASSERT_OK(root.ToGraph(graph.get())); + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + + std::unordered_map clusters = GetClusters(*graph); + + ASSERT_FALSE(clusters.empty()); + std::string cluster_name = clusters.begin()->second; + + std::unordered_map expected_clusters( + {{"negate", cluster_name}, {"add", cluster_name}}); + EXPECT_EQ(clusters, expected_clusters); +} + +TEST(XlaCompilationTest, ClusterIdentityWithNonRefInput) { + Scope root = Scope::NewRootScope().ExitOnError(); + Output variable = ops::Variable(root.WithOpName("variable"), + PartialTensorShape{}, DT_FLOAT); + Output read = ops::Identity(root.WithOpName("read"), variable); + Output neg = ops::Negate(root.WithOpName("negate"), read); + Output identity = ops::Negate(root.WithOpName("identity"), neg); + Output add = ops::Add(root.WithOpName("add"), identity, neg); + std::unique_ptr graph(new Graph(OpRegistry::Global())); + + TF_ASSERT_OK(root.ToGraph(graph.get())); + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + + std::unordered_map clusters = GetClusters(*graph); + + ASSERT_FALSE(clusters.empty()); + std::string cluster_name = clusters.begin()->second; + + std::unordered_map expected_clusters( + {{"negate", cluster_name}, + {"identity", cluster_name}, + {"add", cluster_name}}); + EXPECT_EQ(clusters, expected_clusters); +} + +TEST(XlaCompilationTest, ClusterControlTrigger) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Output recv_a = ops::_Recv(root.WithOpName("recv_a"), DT_BOOL, "tensor_a", + "sender", 0, "receiver"); + Output recv_b = ops::_Recv(root.WithOpName("recv_b"), DT_BOOL, "tensor_b", + "sender", 0, "receiver"); + Output const_a = ops::Const(root.WithOpName("const_a"), 42); + + ops::ControlTrigger ctrl_trigger_a(root.WithOpName("ctrl_trigger_a")); + ops::ControlTrigger ctrl_trigger_b(root.WithOpName("ctrl_trigger_b")); + root.graph()->AddControlEdge(recv_a.node(), ctrl_trigger_a.operation.node()); + root.graph()->AddControlEdge(recv_b.node(), ctrl_trigger_a.operation.node()); + root.graph()->AddControlEdge(ctrl_trigger_b.operation.node(), const_a.node()); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + + TF_ASSERT_OK(root.ToGraph(graph.get())); + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + + std::unordered_map clusters = GetClusters(*graph); + + // TODO(b/118970344): ctrl_trigger_a has inputs with mismatching deadness so + // it won't be clustered. ctrl_trigger_b is okay to cluster but we don't + // cluster it because of b/118970344. + EXPECT_TRUE(clusters.empty()); +} + +TEST(XlaCompilationTest, RandomShape) { + Scope root = Scope::NewRootScope().ExitOnError(); + Output shape_shape = ops::Const(root.WithOpName("shape_shape"), {2}, {1}); + Output shape = + ops::RandomUniformInt(root.WithOpName("shape"), shape_shape, + ops::Const(root.WithOpName("minval"), 1), + ops::Const(root.WithOpName("maxval"), 20)); + Output reshape_input = + ops::Placeholder(root.WithOpName("reshape_input"), DT_FLOAT, + ops::Placeholder::Shape(TensorShape({500, 500}))); + Output reshape = + ops::Reshape(root.WithOpName("reshape"), reshape_input, shape); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + + TF_ASSERT_OK(root.ToGraph(graph.get())); + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + + std::unordered_map clusters = GetClusters(*graph); + EXPECT_EQ(clusters["shape"], ""); +} + +TEST(XlaCompilationTest, RandomShapeWithFunc) { + Scope root = Scope::DisabledShapeInferenceScope().ExitOnError(); + + FunctionDefLibrary flib_def; + FunctionDef func = FunctionDefHelper::Create( + /*function_name=*/"Stateful_func", /*in_def=*/{}, + /*out_def=*/{"out: int32"}, + /*attr_def*/ + {}, /*node_def=*/ + {FunctionDefHelper::Const("shape_shape", 2), + FunctionDefHelper::Const("minval", 1), + FunctionDefHelper::Const("maxval", 20), + {{"shape"}, + "RandomUniformInt", + {"shape_shape:output:0", "minval:output:0", "maxval:output:0"}, + {{"Tout", DataType::DT_INT32}, {"T", DataType::DT_INT32}}}}, + /*ret_def=*/{{"out", "shape:output:0"}}); + + func.mutable_signature()->set_is_stateful(true); + *flib_def.add_function() = std::move(func); + TF_ASSERT_OK(root.graph()->AddFunctionLibrary(flib_def)); + NodeDef call_node; + call_node.set_name("fn_call"); + call_node.set_op("Stateful_func"); + absl::Status status; + Node* call = root.graph()->AddNode(call_node, &status); + TF_ASSERT_OK(status); + + Output shape = Output(call, 0); + Output reshape_input = + ops::Placeholder(root.WithOpName("reshape_input"), DT_FLOAT, + ops::Placeholder::Shape(TensorShape({500, 500}))); + Output reshape = + ops::Reshape(root.WithOpName("reshape"), reshape_input, shape); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_ASSERT_OK(root.ToGraph(graph.get())); + auto fld = std::make_unique(OpRegistry::Global(), + flib_def); + TF_ASSERT_OK( + MarkForCompilationPassTestHelper::MarkForCompilation(&graph, fld.get())); + + std::unordered_map clusters = GetClusters(*graph); + EXPECT_EQ(clusters["fn_call"], ""); +} + +TEST(XlaCompilationTest, RandomShapeOnXlaDevice) { + absl::string_view xla_gpu_device = + "/job:worker/replica:0/task:0/device:XLA_GPU:0"; + + Scope root = Scope::NewRootScope().ExitOnError(); + Output shape_shape = + ops::Const(root.WithOpName("test/shape_shape"), {2}, {1}); + Output shape = + ops::RandomUniformInt(root.WithOpName("test/shape_rng"), shape_shape, + ops::Const(root.WithOpName("test/minval"), 1), + ops::Const(root.WithOpName("test/maxval"), 20)); + Output reshape_input = + ops::Placeholder(root.WithOpName("test/reshape_input"), DT_FLOAT, + ops::Placeholder::Shape(TensorShape({500, 500}))); + Output reshape = + ops::Reshape(root.WithOpName("test/reshape"), reshape_input, shape); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_ASSERT_OK(root.ToGraph(graph.get())); + + for (Node* n : graph->nodes()) { + if (absl::StartsWith(n->name(), /*prefix=*/"test/")) { + n->set_assigned_device_name(std::string(xla_gpu_device)); + } + } + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + + std::unordered_map clusters = GetClusters(*graph); + EXPECT_EQ(clusters["test/shape_rng"], ""); + EXPECT_EQ(clusters["test/reshape"], ""); +} + +TEST(XlaCompilationTest, TensorArrayShapeOnXlaDevice) { + absl::string_view xla_gpu_device = + "/job:worker/replica:0/task:0/device:XLA_GPU:0"; + Scope root = Scope::NewRootScope().ExitOnError(); + ops::TensorArray tensor_array(root.WithOpName("test/tensor_array"), 1, + DT_INT32); + Output zero = ops::Const(root.WithOpName("test/zero"), 0); + ops::TensorArrayWrite tensor_array_write( + root.WithOpName("test/write"), tensor_array.handle, zero, + ops::Const(root.WithOpName("test/forty_two"), 42.0f), tensor_array.flow); + Output tensor_array_read = + ops::TensorArrayRead(root.WithOpName("test/read"), tensor_array.handle, + zero, tensor_array_write.flow_out, DT_INT32); + Output reshape = + ops::Reshape(root.WithOpName("test/reshape"), + ops::Placeholder(root.WithOpName("placeholder"), DT_FLOAT), + tensor_array_read); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_ASSERT_OK(root.ToGraph(graph.get())); + + for (Node* n : graph->nodes()) { + if (absl::StartsWith(n->name(), /*prefix=*/"test/")) { + n->set_assigned_device_name(std::string(xla_gpu_device)); + } + } + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + + std::unordered_map clusters = GetClusters(*graph); + EXPECT_NE(clusters["test/read"], ""); + EXPECT_EQ(clusters["test/read"], clusters["test/reshape"]); +} + +TEST(XlaCompilationTest, DontClusterMergingNodes) { + // MatMulCombined below takes data from nodes on GPU0 and GPU1 and is placed + // on GPU1. However, it should not be clustered with the previous node on + // GPU1, because that will serialize production of its inputs that should be + // done in parallel. + // + // This graph is: + // (Const0, Const0) -> MatMul0 + // (Const1, Const1) -> MatMul1 + // (MatMul0, MatMul1) -> MatMulCombined + // + // Device0: [Const0, Const0, MatMul0] + // Device1: [Const1, Const1, MatMul1, MatMulCombined] + // + // Cluster0: [Const0, Const0, MatMul0] + // Cluster1: [Const1, Const1, MatMul1] + // Cluster2: [MatMulCombined] + Scope root = Scope::NewRootScope().ExitOnError(); + absl::string_view xla_gpu_dev0 = + "/job:worker/replica:0/task:0/device:XLA_GPU:0"; + absl::string_view xla_gpu_dev1 = + "/job:worker/replica:0/task:0/device:XLA_GPU:1"; + std::unique_ptr graph(new Graph(OpRegistry::Global())); + Output a = ops::Tanh(root.WithOpName("tanh_A_dev0"), + ops::Const(root.WithOpName("A_dev0"), 1.0f, {2, 2})); + Output b = ops::Tanh(root.WithOpName("tanh_B_dev1"), + ops::Const(root.WithOpName("B_dev1"), 1.0f, {2, 2})); + Output matmul0 = ops::MatMul(root.WithOpName("MatMul0_dev0"), a, a); + Output matmul1 = ops::MatMul(root.WithOpName("MatMul1_dev1"), b, b); + + Output combined = + ops::MatMul(root.WithOpName("MatMulCombined_dev1"), matmul0, matmul1); + TF_ASSERT_OK(root.ToGraph(graph.get())); + + for (Node* n : graph->nodes()) { + if (absl::EndsWith(n->name(), /*suffix=*/"dev0")) { + n->set_assigned_device_name(std::string(xla_gpu_dev0)); + } else if (absl::EndsWith(n->name(), /*suffix=*/"dev1")) { + n->set_assigned_device_name(std::string(xla_gpu_dev1)); + } + } + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + + // Each of the MatMuls should be in a separate cluster. + std::unordered_map clusters = GetClusters(*graph); + EXPECT_NE(clusters["MatMul0_dev0"], clusters["MatMul1_dev1"]); + EXPECT_NE(clusters["MatMulCombined_dev1"], clusters["MatMul0_dev0"]); + EXPECT_NE(clusters["MatMulCombined_dev1"], clusters["MatMul1_dev1"]); + EXPECT_EQ(clusters["A_dev0"], clusters["MatMul0_dev0"]); + EXPECT_EQ(clusters["B_dev1"], clusters["MatMul1_dev1"]); +} + +TEST(XlaCompilationTest, DontClusterMergingNodesOnCPU) { + // This is similar to the 'DontClusterMergingNodes' above, except + // MatMulCombined is placed on the CPU. + Scope root = Scope::NewRootScope().ExitOnError(); + absl::string_view xla_gpu_dev0 = "/job:worker/replica:0/task:0/device:GPU:0"; + absl::string_view xla_gpu_dev1 = "/job:worker/replica:0/task:0/device:GPU:1"; + absl::string_view xla_cpu_dev0 = "/job:worker/replica:0/task:0/device:CPU:0"; + std::unique_ptr graph(new Graph(OpRegistry::Global())); + Output a = ops::Tanh(root.WithOpName("tanh_A_dev0"), + ops::Const(root.WithOpName("A_dev0"), 1.0f, {2, 2})); + Output b = ops::Tanh(root.WithOpName("tanh_B_dev1"), + ops::Const(root.WithOpName("B_dev1"), 1.0f, {2, 2})); + Output matmul0 = ops::MatMul(root.WithOpName("MatMul0_dev0"), a, a); + Output matmul1 = ops::MatMul(root.WithOpName("MatMul1_dev1"), b, b); + + Output combined = + ops::MatMul(root.WithOpName("MatMulCombined_cpu"), matmul0, matmul1); + TF_ASSERT_OK(root.ToGraph(graph.get())); + + for (Node* n : graph->nodes()) { + if (absl::EndsWith(n->name(), /*suffix=*/"cpu")) { + n->set_assigned_device_name(std::string(xla_cpu_dev0)); + } else if (absl::EndsWith(n->name(), /*suffix=*/"dev0")) { + n->set_assigned_device_name(std::string(xla_gpu_dev0)); + } else if (absl::EndsWith(n->name(), /*suffix=*/"dev1")) { + n->set_assigned_device_name(std::string(xla_gpu_dev1)); + } + } + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + + // Each of the MatMuls should be in a separate cluster. + std::unordered_map clusters = GetClusters(*graph); + EXPECT_NE(clusters["MatMul0_dev0"], clusters["MatMul1_dev1"]); + EXPECT_NE(clusters["MatMulCombined_cpu"], clusters["MatMul0_dev0"]); + EXPECT_NE(clusters["MatMulCombined_cpu"], clusters["MatMul1_dev1"]); + EXPECT_EQ(clusters["A_dev0"], clusters["MatMul0_dev0"]); + EXPECT_EQ(clusters["B_dev1"], clusters["MatMul1_dev1"]); +} + +// TODO(b/117085735): This form of clustering should be prevented. +TEST(XlaCompilationTest, NOT_DontClusterSpreadingNodes) { + // MatMulSource below creates data for nodes on GPU0 and GPU1 and is placed + // on GPU0. However, it should not be clustered with the next node on + // GPU0, because that will prevent the node on GPU1 from beginning its work as + // soon as the data has been produced. + // + // This graph is: + // (Const0, Const0) -> MatMulSource + // MatMulSource -> (MatMul0, MatMul1) + // + // Device0: [Const0, Const1, MatMulSource, MatMul0] + // Device1: [MatMul1] + // + // Cluster0: [Const0, Const1, MatMulSource] + // Cluster1: [MatMul0] + // Cluster2: [MatMul1] + Scope root = Scope::NewRootScope().ExitOnError(); + absl::string_view xla_gpu_dev0 = + "/job:worker/replica:0/task:0/device:XLA_GPU:0"; + absl::string_view xla_gpu_dev1 = + "/job:worker/replica:0/task:0/device:XLA_GPU:1"; + std::unique_ptr graph(new Graph(OpRegistry::Global())); + Output a = ops::Const(root.WithOpName("A_dev0"), 1.0f, {2, 2}); + Output matmul_source = + ops::MatMul(root.WithOpName("MatMulSource_dev0"), a, a); + + Output matmul0 = ops::MatMul(root.WithOpName("MatMul0_dev0"), matmul_source, + matmul_source); + Output matmul1 = ops::MatMul(root.WithOpName("MatMul1_dev1"), matmul_source, + matmul_source); + + TF_ASSERT_OK(root.ToGraph(graph.get())); + for (Node* n : graph->nodes()) { + if (absl::EndsWith(n->name(), /*suffix=*/"dev0")) { + n->set_assigned_device_name(std::string(xla_gpu_dev0)); + } else if (absl::EndsWith(n->name(), /*suffix=*/"dev1")) { + n->set_assigned_device_name(std::string(xla_gpu_dev1)); + } + } + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + + std::unordered_map clusters = GetClusters(*graph); + EXPECT_EQ(clusters["A_dev0"], clusters["MatMulSource_dev0"]); + EXPECT_NE(clusters["MatMul0_dev0"], clusters["MatMul1_dev1"]); + EXPECT_NE(clusters["MatMulSource_dev0"], clusters["MatMul1_dev1"]); + + // Improved Heuristics should prevent this probably. + EXPECT_EQ(clusters["MatMulSource_dev0"], clusters["MatMul0_dev0"]); +} + +TEST(XlaCompilationTest, ClusterStatefulRandomOpOnXlaDevice) { + absl::string_view xla_cpu_device = + "/job:worker/replica:0/task:0/device:XLA_CPU:0"; + + Scope root = Scope::NewRootScope().ExitOnError(); + Output shape = ops::Const(root.WithOpName("test/shape_shape"), {200, 200}); + Output a = ops::RandomUniform(root.WithOpName("test/a"), shape, DT_FLOAT); + Output b = ops::RandomUniform(root.WithOpName("test/b"), shape, DT_FLOAT); + Output c = ops::Add(root.WithOpName("test/c"), a, b); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_ASSERT_OK(root.ToGraph(graph.get())); + + for (Node* n : graph->nodes()) { + if (absl::StartsWith(n->name(), /*prefix=*/"test/")) { + n->set_assigned_device_name(std::string(xla_cpu_device)); + } + } + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + + std::unordered_map clusters = GetClusters(*graph); + EXPECT_NE(clusters["test/a"], ""); + EXPECT_NE(clusters["test/b"], ""); + EXPECT_NE(clusters["test/c"], ""); +} + +TEST(XlaCompilationTest, DontAutoClusterStatefulRandomOp) { + Scope root = Scope::NewRootScope().ExitOnError(); + Output shape = ops::Const(root.WithOpName("test/shape_shape"), {200, 200}); + Output a = ops::RandomUniform(root.WithOpName("test/a"), shape, DT_FLOAT); + Output b = ops::RandomUniform(root.WithOpName("test/b"), shape, DT_FLOAT); + Output c = ops::Add(root.WithOpName("test/c"), a, b); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_ASSERT_OK(root.ToGraph(graph.get())); + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + + std::unordered_map clusters = GetClusters(*graph); + EXPECT_EQ(clusters["test/a"], ""); + EXPECT_EQ(clusters["test/b"], ""); +} + +TEST(XlaCompilationTest, ClusterDummyOpsOnXlaDevice) { + absl::string_view xla_cpu_device = + "/job:worker/replica:0/task:0/device:XLA_CPU:0"; + + Scope root = Scope::NewRootScope().ExitOnError(); + Output a = ops::Placeholder(root.WithOpName("test/a"), DT_FLOAT); + Output b = ops::Placeholder(root.WithOpName("test/b"), DT_FLOAT); + Output check = + ops::CheckNumerics(root.WithOpName("test/check"), a, "test/check"); + Output ge = ops::GreaterEqual(root.WithOpName("test/greaterequal"), check, b); + Operation assert = ops::Assert(root.WithOpName("test/assert"), ge, {a, b}); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_ASSERT_OK(root.ToGraph(graph.get())); + + for (Node* n : graph->nodes()) { + if (absl::StartsWith(n->name(), /*prefix=*/"test/")) { + n->set_assigned_device_name(std::string(xla_cpu_device)); + } + } + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + + std::unordered_map clusters = GetClusters(*graph); + EXPECT_NE(clusters["test/check"], ""); + EXPECT_NE(clusters["test/greaterequal"], ""); + EXPECT_NE(clusters["test/assert"], ""); +} + +TEST(XlaCompilationTest, DontAutoClusterDummyOps) { + Scope root = Scope::NewRootScope().ExitOnError(); + Output a = ops::Placeholder(root.WithOpName("test/a"), DT_FLOAT); + Output b = ops::Placeholder(root.WithOpName("test/b"), DT_FLOAT); + Output check = + ops::CheckNumerics(root.WithOpName("test/check"), a, "test/check"); + Output ge = ops::GreaterEqual(root.WithOpName("test/greaterequal"), check, b); + Operation assert = ops::Assert(root.WithOpName("test/assert"), ge, {a, b}); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_ASSERT_OK(root.ToGraph(graph.get())); + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + + std::unordered_map clusters = GetClusters(*graph); + EXPECT_EQ(clusters["test/assert"], ""); + EXPECT_EQ(clusters["test/check"], ""); +} + +TEST(XlaCompilationTest, DontAutoClusterOpsProducingVariant) { + Scope root = Scope::NewRootScope().ExitOnError(); + Output a = ops::Placeholder(root.WithOpName("test/a"), DT_INT64); + Output b = ops::Placeholder(root.WithOpName("test/b"), DT_INT64); + + Output cast_a = ops::Cast(root.WithOpName("test/cast_a"), a, DT_INT32); + Output cast_b = ops::Cast(root.WithOpName("test/cast_b"), b, DT_INT32); + + Output tensor_list_reserve = ops::TensorListReserve( + root.WithOpName("test/tensor_list_reserve"), cast_a, cast_b, DT_FLOAT); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_ASSERT_OK(root.ToGraph(graph.get())); + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + + std::unordered_map clusters = GetClusters(*graph); + EXPECT_EQ(clusters["test/tensor_list_reserve"], ""); +} + +TEST(XlaCompilationTest, DontAutoClusterOpsConsumingVariant) { + Scope root = Scope::NewRootScope().ExitOnError(); + Output dummy_input = + ops::Placeholder(root.WithOpName("test/dummy_input"), DT_INT64); + Output variant_input = + ops::Placeholder(root.WithOpName("test/variant_input"), DT_VARIANT); + + // Create one more node so that we don't avoid creating a cluster solely + // because it would be trivial. + Output dummy_cast = + ops::Cast(root.WithOpName("test/dummy_cast"), dummy_input, DT_INT32); + + Output tensor_list_element_shape = ops::TensorListElementShape( + root.WithOpName("test/tensor_list_element_shape"), variant_input, + DT_INT32); + + root.graph()->AddControlEdge(dummy_cast.node(), + tensor_list_element_shape.node()); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_ASSERT_OK(root.ToGraph(graph.get())); + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + + std::unordered_map clusters = GetClusters(*graph); + EXPECT_EQ(clusters["test/tensor_list_element_shape"], ""); +} + +TEST(XlaCompilationTest, ClusterOpsProducingVariantIfOnXlaDevice) { + Scope root = Scope::NewRootScope().ExitOnError(); + Output a = ops::Placeholder(root.WithOpName("test/a"), DT_INT64); + Output b = ops::Placeholder(root.WithOpName("test/b"), DT_INT64); + + Output cast_a = ops::Cast(root.WithOpName("test/cast_a"), a, DT_INT32); + Output cast_b = ops::Cast(root.WithOpName("test/cast_b"), b, DT_INT32); + + Output tensor_list_reserve = ops::TensorListReserve( + root.WithOpName("test/tensor_list_reserve"), cast_a, cast_b, DT_FLOAT); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_ASSERT_OK(root.ToGraph(graph.get())); + + std::string xla_cpu_device = "/job:worker/replica:0/task:0/device:XLA_CPU:0"; + for (Node* n : graph->nodes()) { + if (absl::StartsWith(n->name(), /*prefix=*/"test/")) { + n->set_assigned_device_name(xla_cpu_device); + } + } + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + + std::unordered_map clusters = GetClusters(*graph); + EXPECT_NE(clusters["test/tensor_list_reserve"], ""); +} + +const char* kCPU0 = "/job:worker/replica:0/task:0/device:CPU:0"; +const char* kGPU0 = "/job:worker/replica:0/task:0/device:GPU:0"; +const char* kXLA_GPU0 = "/job:worker/replica:0/task:0/device:XLA_GPU:0"; +const char* kGPU1 = "/job:worker/replica:0/task:0/device:GPU:1"; + +TEST(XlaCompilationTest, CreateCombinedCpuGpuClusters) { + Scope root = Scope::NewRootScope().ExitOnError(); + Output a = ops::Placeholder(root.WithOpName("test/a"), DT_FLOAT); + Output b = ops::Placeholder(root.WithOpName("test/b"), DT_FLOAT); + + Output x = ops::Add(root.WithOpName("test/x"), a, b); + Output y = ops::MatMul(root.WithOpName("test/y"), a, b); + Output z = ops::Add(root.WithOpName("test/z"), x, y); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_ASSERT_OK(root.ToGraph(graph.get())); + + FindNodeByName(graph.get(), "test/x")->set_assigned_device_name(kGPU0); + FindNodeByName(graph.get(), "test/y")->set_assigned_device_name(kCPU0); + FindNodeByName(graph.get(), "test/z")->set_assigned_device_name(kGPU0); + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + + std::unordered_map clusters = GetClusters(*graph); + + EXPECT_NE(clusters["test/x"], ""); + + EXPECT_EQ(clusters["test/x"], clusters["test/y"]); + EXPECT_EQ(clusters["test/y"], clusters["test/z"]); +} + +TEST(XlaCompilationTest, DontCreateGpu0AndGpu1Clusters) { + Scope root = Scope::NewRootScope().ExitOnError(); + Output a = ops::Placeholder(root.WithOpName("test/a"), DT_FLOAT); + Output b = ops::Placeholder(root.WithOpName("test/b"), DT_FLOAT); + + Output x = ops::Add(root.WithOpName("test/x"), a, b); + Output y = ops::Add(root.WithOpName("test/y"), x, x); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_ASSERT_OK(root.ToGraph(graph.get())); + + FindNodeByName(graph.get(), "test/x")->set_assigned_device_name(kGPU0); + FindNodeByName(graph.get(), "test/y")->set_assigned_device_name(kGPU1); + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + + std::unordered_map clusters = GetClusters(*graph); + + EXPECT_EQ(clusters["test/x"], ""); + EXPECT_EQ(clusters["test/y"], ""); +} + +TEST(XlaCompilationTest, DontCreateCombinedCpuUnknownClusters) { + Scope root = Scope::NewRootScope().ExitOnError(); + Output a = ops::Placeholder(root.WithOpName("test/a"), DT_FLOAT); + Output b = ops::Placeholder(root.WithOpName("test/b"), DT_FLOAT); + + Output x = ops::Add(root.WithOpName("test/x"), a, b); + Output y = ops::Add(root.WithOpName("test/y"), x, x); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_ASSERT_OK(root.ToGraph(graph.get())); + + FindNodeByName(graph.get(), "test/x")->set_assigned_device_name(kCPU0); + FindNodeByName(graph.get(), "test/y")->set_assigned_device_name(kXLA_GPU0); + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + + std::unordered_map clusters = GetClusters(*graph); + + EXPECT_EQ(clusters["test/x"], ""); + EXPECT_EQ(clusters["test/y"], ""); +} + +TEST(XlaCompilationTest, ClusterResourceOpsWhenSafe) { + Scope root = Scope::NewRootScope().ExitOnError(); + Output a = ops::Placeholder(root.WithOpName("test/a"), DT_FLOAT); + Node* var_handle; + Node* resource_read = MakeRead(root, "read", &var_handle); + Output b = ops::Add(root.WithOpName("test/b"), Output(resource_read, 0), a); + + std::string resource_read_name = resource_read->name(); + std::string var_handle_name = var_handle->name(); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_ASSERT_OK(root.ToGraph(graph.get())); + + FindNodeByName(graph.get(), "test/b")->set_assigned_device_name(kCPU0); + FindNodeByName(graph.get(), resource_read_name) + ->set_assigned_device_name(kGPU0); + FindNodeByName(graph.get(), var_handle_name)->set_assigned_device_name(kGPU0); + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + + std::unordered_map clusters = GetClusters(*graph); + + EXPECT_NE(clusters["test/b"], ""); + EXPECT_EQ(clusters["test/b"], clusters[resource_read_name]); +} + +TEST(XlaCompilationTest, DontClusterResourceOpsWhenUnsafe) { + Scope root = Scope::NewRootScope().ExitOnError(); + Output a = ops::Placeholder(root.WithOpName("test/a"), DT_FLOAT); + Node* var_handle; + Node* resource_read = MakeRead(root, "read", &var_handle); + Output b = ops::Add(root.WithOpName("test/b"), Output(resource_read, 0), a); + + std::string resource_read_name = resource_read->name(); + std::string var_handle_name = var_handle->name(); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_ASSERT_OK(root.ToGraph(graph.get())); + + FindNodeByName(graph.get(), "test/b")->set_assigned_device_name(kGPU0); + FindNodeByName(graph.get(), resource_read_name) + ->set_assigned_device_name(kCPU0); + FindNodeByName(graph.get(), var_handle_name)->set_assigned_device_name(kCPU0); + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + + std::unordered_map clusters = GetClusters(*graph); + + EXPECT_EQ(clusters["test/b"], ""); + EXPECT_EQ(clusters[resource_read_name], ""); +} + +TEST(XlaCompilationTest, DontClusterNodesWithScopedAllocatorAttr) { + Scope root = Scope::NewRootScope().ExitOnError(); + Output a = ops::Placeholder(root.WithOpName("test/a"), DT_FLOAT); + Output b = ops::Placeholder(root.WithOpName("test/b"), DT_FLOAT); + + Output x = ops::Add(root.WithOpName("test/x"), a, b); + Output y = ops::MatMul(root.WithOpName("test/y"), a, b); + Output z = ops::Add(root.WithOpName("test/z"), x, y); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_ASSERT_OK(root.ToGraph(graph.get())); + + FindNodeByName(graph.get(), "test/x")->set_assigned_device_name(kGPU0); + FindNodeByName(graph.get(), "test/y")->set_assigned_device_name(kGPU0); + FindNodeByName(graph.get(), "test/z")->set_assigned_device_name(kGPU0); + + std::vector scoped_allocator_value; + scoped_allocator_value.push_back(0); + scoped_allocator_value.push_back(155); + FindNodeByName(graph.get(), "test/z") + ->AddAttr("_scoped_allocator", scoped_allocator_value); + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + + std::unordered_map clusters = GetClusters(*graph); + + EXPECT_EQ(clusters["test/z"], ""); +} + +TEST(XlaCompilationTest, DontClusterNodesWithForwardFromAttr) { + Scope root = Scope::NewRootScope().ExitOnError(); + Output a = ops::Placeholder(root.WithOpName("test/a"), DT_FLOAT); + Output b = ops::Placeholder(root.WithOpName("test/b"), DT_FLOAT); + + Output x = ops::Add(root.WithOpName("test/x"), a, b); + Output y = ops::MatMul(root.WithOpName("test/y"), a, b); + Output z = ops::Add(root.WithOpName("test/z"), x, y); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_ASSERT_OK(root.ToGraph(graph.get())); + + FindNodeByName(graph.get(), "test/x")->set_assigned_device_name(kGPU0); + FindNodeByName(graph.get(), "test/y")->set_assigned_device_name(kGPU0); + FindNodeByName(graph.get(), "test/z")->set_assigned_device_name(kGPU0); + + FindNodeByName(graph.get(), "test/z")->AddAttr("_forward_from", 0); + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + + std::unordered_map clusters = GetClusters(*graph); + + EXPECT_EQ(clusters["test/z"], ""); +} + +// Note, this relies on other implementation details to test the +// specific heuristic we care about here, so other changes might be at fault if +// this CL breaks. What we care about is that if a ShapeConsumingOp can be +// connected with a producer or consumer and cannot be clustered with both, it +// should be clustered with the producer. +TEST(XlaCompilationTest, ClusterShapeConsumerWithProducer) { + Scope root = Scope::NewRootScope().ExitOnError(); + Output a = ops::Placeholder(root.WithOpName("test/a"), DT_FLOAT); + Output b = ops::Placeholder(root.WithOpName("test/b"), DT_FLOAT); + + Output x = ops::MatMul(root.WithOpName("test/x"), a, b); + Output y = ops::Size(root.WithOpName("test/y"), x); + Output z = ops::Add(root.WithOpName("test/z"), y, y); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_ASSERT_OK(root.ToGraph(graph.get())); + + // Ensure that the "Size" op can only be clustered with either the producer or + // consumer by putting them on different devices. + FindNodeByName(graph.get(), "test/x")->set_assigned_device_name(kGPU0); + FindNodeByName(graph.get(), "test/y")->set_assigned_device_name(kCPU0); + FindNodeByName(graph.get(), "test/z")->set_assigned_device_name(kGPU1); + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + + std::unordered_map clusters = GetClusters(*graph); + + EXPECT_NE(clusters["test/y"], ""); + EXPECT_EQ(clusters["test/x"], clusters["test/y"]); + EXPECT_NE(clusters["test/z"], clusters["test/y"]); +} + +// Test that ShapeConsuming ops are still fully clustered whenever possible. +TEST(XlaCompilationTest, ClusterShapeConsumerWithProducerAndConsumer) { + Scope root = Scope::NewRootScope().ExitOnError(); + Output a = ops::Placeholder(root.WithOpName("test/a"), DT_FLOAT); + Output b = ops::Placeholder(root.WithOpName("test/b"), DT_FLOAT); + + Output x = ops::MatMul(root.WithOpName("test/x"), a, b); + Output y = ops::Size(root.WithOpName("test/y"), x); + Output z = ops::Add(root.WithOpName("test/z"), y, y); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_ASSERT_OK(root.ToGraph(graph.get())); + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + + std::unordered_map clusters = GetClusters(*graph); + + EXPECT_NE(clusters["test/y"], ""); + EXPECT_EQ(clusters["test/y"], clusters["test/x"]); + EXPECT_EQ(clusters["test/y"], clusters["test/z"]); +} + +void AddCtrlEdge(const Scope& scope, Operation a, Operation b) { + scope.graph()->AddControlEdge(a.node(), b.node()); +} + +void AddCtrlEdge(const Scope& scope, Output a, Operation b) { + AddCtrlEdge(scope, a.op(), b); +} + +void AddCtrlEdge(const Scope& scope, Operation a, Output b) { + AddCtrlEdge(scope, a, b.op()); +} + +// Tests that we pick a good clustering for graphs that have an integer +// increment operation control dependent on gradient update operations. +TEST(XlaCompilationTest, IterationIncrementAndGroupDeps) { + Scope scope = Scope::NewRootScope().ExitOnError(); + + Output iter = + ops::VarHandleOp(scope.WithOpName("iter"), DT_INT64, TensorShape({})); + Output weights_0 = ops::VarHandleOp(scope.WithOpName("weights_0"), DT_FLOAT, + TensorShape({1000})); + Output weights_1 = ops::VarHandleOp(scope.WithOpName("weights_1"), DT_FLOAT, + TensorShape({1000})); + + // We update the weights by adding delta to them (to "simulate" a + // ResourceApplyGradientDescent and similar things). + Output delta = ops::Placeholder(scope.WithOpName("delta"), DT_FLOAT); + + ops::AssignAddVariableOp increment_op( + scope.WithOpName("IncrementIteration"), iter, + ops::Const(scope.WithOpName("one"), static_cast(1))); + + ops::AssignAddVariableOp weights_0_update_op( + scope.WithOpName("weights_0_update"), weights_0, delta); + ops::AssignAddVariableOp weights_1_update_op( + scope.WithOpName("weights_1_update"), weights_1, delta); + + ops::NoOp group_deps(scope.WithOpName("group_deps")); + + ops::NoOp some_ctrl_input(scope.WithOpName("some_ctrl_input")); + + Output matmul_input = + ops::Placeholder(scope.WithOpName("matmul_input"), DT_FLOAT); + Output matmul_0 = + ops::MatMul(scope.WithOpName("matmul_0"), matmul_input, matmul_input); + Output matmul_1 = + ops::MatMul(scope.WithOpName("matmul_1"), matmul_input, matmul_input); + + AddCtrlEdge(scope, increment_op, group_deps); + AddCtrlEdge(scope, weights_0_update_op, increment_op); + AddCtrlEdge(scope, weights_1_update_op, increment_op); + + AddCtrlEdge(scope, some_ctrl_input, weights_0_update_op); + AddCtrlEdge(scope, some_ctrl_input, weights_1_update_op); + + AddCtrlEdge(scope, matmul_0, group_deps); + AddCtrlEdge(scope, matmul_1, group_deps); + + AddCtrlEdge(scope, weights_0_update_op, matmul_0); + AddCtrlEdge(scope, weights_1_update_op, matmul_1); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_ASSERT_OK(scope.ToGraph(graph.get())); + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + + std::unordered_map clusters = GetClusters(*graph); + + EXPECT_NE(clusters["some_ctrl_input"], ""); + EXPECT_EQ(clusters["some_ctrl_input"], clusters["weights_0_update"]); + EXPECT_EQ(clusters["some_ctrl_input"], clusters["weights_1_update"]); + EXPECT_EQ(clusters["some_ctrl_input"], clusters["matmul_0"]); + EXPECT_EQ(clusters["some_ctrl_input"], clusters["matmul_0"]); +} + +// Test a pattern where a special Identity node is driving consts in a loop. +// Expect that the Identity node will not go into any clusters. Note that we +// create an incomplete graph here (e.g., lacking Enter/Exit/NextIteration, +// etc.) just enough to test the pattern, as a complete graph may be too +// cumbersome and unnecessary. +TEST(XlaCompilationTest, DontClusterTheSpecialIdentityDrivingConstsInLoop) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Output cond = ops::Placeholder(root.WithOpName("cond"), DT_BOOL); + Output value = ops::Placeholder(root.WithOpName("value"), DT_FLOAT); + Output loop_cond = ops::LoopCond(root.WithOpName("loop_cond"), cond); + ops::Switch switch_node(root.WithOpName("switch"), value, loop_cond); + + Output identity = + ops::Identity(root.WithOpName("identity"), switch_node.output_true); + Output const_node = ops::Const(root.WithOpName("const"), 1.0f); + root.graph()->AddControlEdge(identity.node(), const_node.node()); + Output tanh0 = ops::Tanh(root.WithOpName("tanh0"), const_node); + Output tanh1 = ops::Tanh(root.WithOpName("tanh1"), tanh0); + Output add = ops::Add(root.WithOpName("add"), const_node, tanh1); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_EXPECT_OK(root.ToGraph(graph.get())); + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation( + &graph, + MarkForCompilationPassTestHelper::Options().WithDeadnessAnalysis())); + auto clusters = GetClusters(*graph); + + EXPECT_EQ(clusters["identity"], ""); +} + +TEST(XlaCompilationTest, UnsupportedEnterExitPattern) { + // Regression test for b/32350199, where the autoclustering code introduced a + // deadlock in a graph containing a while loop. + Scope root = Scope::NewRootScope().ExitOnError(); + auto a = ops::Placeholder(root.WithOpName("A"), DT_FLOAT); + auto enter_0 = ops::internal::Enter(root.WithOpName("enter_a"), a, "frame"); + auto exit_0 = ops::internal::Exit(root.WithOpName("exit_a"), enter_0); + auto tanh = ops::Tanh(root.WithOpName("tanh"), exit_0); + auto enter_1 = + ops::internal::Enter(root.WithOpName("enter_1"), tanh, "frame"); + auto exit_1 = ops::internal::Exit(root.WithOpName("exit_1"), enter_1); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_EXPECT_OK(root.ToGraph(graph.get())); + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + auto clusters = GetClusters(*graph); + + // Nothing should be compiled. + EXPECT_EQ(0, clusters.size()); +} + +TEST(XlaCompilationTest, DeterministicClusterNames) { + auto create_graph = + [](absl::string_view output_name) -> std::unique_ptr { + std::unique_ptr graph(new Graph(OpRegistry::Global())); + GraphDefBuilder builder(GraphDefBuilder::kFailImmediately); + Tensor t(DT_FLOAT, TensorShape()); + t.scalar()() = 0.0f; + Node* a = ops::SourceOp("Const", builder.opts() + .WithName("A") + .WithAttr("dtype", DT_FLOAT) + .WithAttr("value", t)); + Node* b = ops::UnaryOp("Neg", a, builder.opts().WithName("B")); + ops::BinaryOp("MatMul", a, b, builder.opts().WithName(output_name)); + TF_EXPECT_OK(GraphDefBuilderToGraph(builder, graph.get())); + return graph; + }; + + // Checks if two cluster names match for all parts except their sequence + // number. Names are expected as: cluster_fp_seq# + auto cluster_names_match = [](absl::string_view lhs_cluster_name, + absl::string_view rhs_cluster_name) { + std::vector lhs_cluster_name_parts = + absl::StrSplit(lhs_cluster_name, '_'); + std::vector rhs_cluster_name_parts = + absl::StrSplit(rhs_cluster_name, '_'); + + if (lhs_cluster_name_parts.size() != 3) { + return absl::FailedPreconditionError( + absl::StrCat("unexpected lhs cluster name: ", lhs_cluster_name)); + } + + if (rhs_cluster_name_parts.size() != 3) { + return absl::FailedPreconditionError( + absl::StrCat("unexpected rhs cluster name: ", rhs_cluster_name)); + } + + if (lhs_cluster_name_parts[0] != rhs_cluster_name_parts[0] || + lhs_cluster_name_parts[1] != rhs_cluster_name_parts[1]) { + return absl::FailedPreconditionError( + absl::StrCat("Cluster names mismatch: lhs: ", lhs_cluster_name, + " rhs: ", rhs_cluster_name)); + } + + if (lhs_cluster_name_parts[2] == rhs_cluster_name_parts[2]) { + return absl::FailedPreconditionError(absl::StrCat( + "cluster sequence numbers are the same: lhs: ", lhs_cluster_name, + " rhs: ", rhs_cluster_name)); + } + + return absl::OkStatus(); + }; + + testing::ResetClusterSequenceNumber(); + auto options = MarkForCompilationPassTestHelper::Options() + .WithDeterministicClusterNames(); + + // Cluster the same graphs twice so we can observe that the prefix contains + // the stable fingerprint. + auto graph0 = create_graph("out"); + auto graph1 = create_graph("differs"); + auto graph2 = create_graph("out"); // same as graph0 + auto graph3 = create_graph("differs"); // same as graph1 + + TF_ASSERT_OK( + MarkForCompilationPassTestHelper::MarkForCompilation(&graph0, options)); + auto clusters0 = GetClusterNames(*graph0); + ASSERT_EQ(clusters0.size(), 1); + + TF_ASSERT_OK( + MarkForCompilationPassTestHelper::MarkForCompilation(&graph1, options)); + auto clusters1 = GetClusterNames(*graph1); + ASSERT_EQ(clusters1.size(), 1); + + TF_ASSERT_OK( + MarkForCompilationPassTestHelper::MarkForCompilation(&graph2, options)); + auto clusters2 = GetClusterNames(*graph2); + ASSERT_EQ(clusters2.size(), 1); + + TF_ASSERT_OK( + MarkForCompilationPassTestHelper::MarkForCompilation(&graph3, options)); + auto clusters3 = GetClusterNames(*graph3); + ASSERT_EQ(clusters3.size(), 1); + + // clusters0 and clusters2 should be the same + TF_EXPECT_OK(cluster_names_match(*clusters0.begin(), *clusters2.begin())); + + // clusters1 and clusters3 should also be the same + TF_EXPECT_OK(cluster_names_match(*clusters1.begin(), *clusters3.begin())); + + // clusters0/2 should differ from clusters1/3 +} + +TEST(XlaCompilationTest, ClusterSessionName) { + Scope root = Scope::NewRootScope().ExitOnError(); + Output variable = ops::Variable(root.WithOpName("variable"), + PartialTensorShape{}, DT_FLOAT); + Output read = ops::Identity(root.WithOpName("read"), variable); + Output neg = ops::Negate(root.WithOpName("negate"), read); + Output add = ops::Add(root.WithOpName("add"), neg, neg); + std::unique_ptr graph(new Graph(OpRegistry::Global())); + + TF_ASSERT_OK(root.ToGraph(graph.get())); + auto options = MarkForCompilationPassTestHelper::Options().WithSessionName( + "test_session_name"); + TF_ASSERT_OK( + MarkForCompilationPassTestHelper::MarkForCompilation(&graph, options)); + + std::unordered_map clusters = GetClusters(*graph); + + ASSERT_FALSE(clusters.empty()); + std::string cluster_name = clusters.begin()->second; + + std::unordered_map expected_clusters( + {{"negate", cluster_name}, {"add", cluster_name}}); + EXPECT_EQ(clusters, expected_clusters); + EXPECT_THAT(cluster_name, ::testing::StartsWith("test_session_name")); +} + +namespace { +Node* MakeStageNode(GraphDefBuilder& builder, std::string name, + std::initializer_list dtypes, + absl::Span values) { + auto opts = builder.opts() + .WithName(std::move(name)) + .WithAttr("dtypes", std::move(dtypes)); + if (opts.HaveError()) { + return nullptr; + } + + NodeBuilder node_builder(name, "Stage", opts.op_registry()); + node_builder.Input(values); + return opts.FinalizeBuilder(&node_builder); +} +} // namespace + +TEST(XlaCompilationTest, StagePipelinePreservedByClusterScopingPass) { + auto build_staged_graph = [](std::unique_ptr* graph) -> absl::Status { + // Construct a graph as below with two pipeline stages and test that nodes + // in different stages will not be merged if ClusterScopingPass is on. + // + // b + // | + // v + // a -> add0 -> relu0 -> stage + // + // b + // | + // v + // unstage -> add1 -> relu1 + GraphDefBuilder builder(GraphDefBuilder::kFailImmediately); + Node* a = ops::SourceOp("Const", builder.opts() + .WithName("a") + .WithAttr("dtype", DT_FLOAT) + .WithAttr("value", Tensor())); + Node* b = ops::SourceOp("Const", builder.opts() + .WithName("b") + .WithAttr("dtype", DT_FLOAT) + .WithAttr("value", Tensor())); + Node* unstage = ops::SourceOp( + "Unstage", + builder.opts().WithName("unstage").WithAttr("dtypes", {DT_FLOAT})); + + Node* add0 = ops::BinaryOp("Add", a, b, builder.opts().WithName("add0")); + Node* add1 = + ops::BinaryOp("Add", unstage, b, builder.opts().WithName("add1")); + Node* relu0 = ops::UnaryOp("Relu", add0, builder.opts().WithName("relu0")); + ops::UnaryOp("Relu", add1, builder.opts().WithName("relu1")); + MakeStageNode(builder, "stage", {DT_FLOAT}, {relu0}); + + return GraphDefBuilderToGraph(builder, graph->get()); + }; + + // All nodes go into the same cluster if ClusterScopingPass is off. + { + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_ASSERT_OK(build_staged_graph(&graph)); + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation( + &graph, + MarkForCompilationPassTestHelper::Options().WithNoClusterScoping())); + + std::unordered_map clusters = GetClusters(*graph); + EXPECT_EQ(clusters["add0"], clusters["add1"]); + EXPECT_EQ(clusters["add0"], clusters["relu1"]); + EXPECT_EQ(clusters["relu0"], clusters["add1"]); + EXPECT_EQ(clusters["relu0"], clusters["relu1"]); + } + + // By default, ClusterScopingPass is on and different pipeline stages should + // not be merged. + { + std::unique_ptr graph(new Graph(OpRegistry::Global())); + TF_ASSERT_OK(build_staged_graph(&graph)); + + TF_ASSERT_OK(MarkForCompilationPassTestHelper::MarkForCompilation(&graph)); + + std::unordered_map clusters = GetClusters(*graph); + EXPECT_NE(clusters["add0"], clusters["add1"]); + EXPECT_NE(clusters["add0"], clusters["relu1"]); + EXPECT_NE(clusters["relu0"], clusters["add1"]); + EXPECT_NE(clusters["relu0"], clusters["relu1"]); + } +} +TEST(XlaCompilationTest, XLALiteAllowlist) { + auto* allowlist_table = tensorflow::GetAllowlistTable(); + absl::flat_hash_set hallowlist; + std::vector vall_ops = XlaOpRegistry::GetAllRegisteredOps(); + absl::flat_hash_set all_ops(vall_ops.begin(), vall_ops.end()); + + // Check that all the operations in the table are existing TF operations + for (auto pair : *allowlist_table) { + hallowlist.insert(pair.second.begin(), pair.second.end()); + for (auto op : pair.second) { + ASSERT_TRUE(all_ops.contains(op)); + } + } + + // Check that all registered XLA operation are in the allowlist + // table or are known to not be in it. + + absl::flat_hash_set known_not_in_list = + tensorflow::testing::GetKnownXLAAllowlistOp(); + std::vector unknow_op; + for (std::string op : vall_ops) { + if (!hallowlist.contains(op) && !known_not_in_list.contains(op)) { + unknow_op.push_back(op); + } + } + EXPECT_TRUE(unknow_op.empty()) + << "Someone added support for a new TF operations inside XLA. They must " + "be included in the XLALite allowlist or denylist:\n" + << absl::StrJoin(unknow_op, "\n"); +} +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/mark_for_compilation_pass_test_helper.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/mark_for_compilation_pass_test_helper.cc new file mode 100644 index 0000000000000000000000000000000000000000..4710062f9c41d09d3d460027832dfe3b7b7cce22 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/mark_for_compilation_pass_test_helper.cc @@ -0,0 +1,81 @@ +/* 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 "tensorflow/compiler/jit/mark_for_compilation_pass_test_helper.h" + +#include +#include + +#include "tensorflow/compiler/jit/cluster_scoping_pass.h" +#include "tensorflow/core/common_runtime/device_factory.h" +#include "tensorflow/core/lib/gtl/cleanup.h" +#include "tensorflow/core/public/session_options.h" + +namespace tensorflow { +/*static*/ absl::Status MarkForCompilationPassTestHelper::MarkForCompilation( + std::unique_ptr* graph, FunctionLibraryDefinition* flib_def, + MarkForCompilationPassTestHelper::Options options) { + // Assign all unassigned nodes to the CPU device. + static const char* kCpuDevice = "/job:localhost/replica:0/task:0/cpu:0"; + for (Node* n : (*graph)->nodes()) { + if (n->assigned_device_name().empty()) { + n->set_assigned_device_name(kCpuDevice); + } + } + + SessionOptions session_options; + if (options.enable_global_jit) { + session_options.config.mutable_graph_options() + ->mutable_optimizer_options() + ->set_global_jit_level(OptimizerOptions::ON_2); + } + if (!options.session_name.empty()) { + session_options.config.mutable_experimental() + ->mutable_session_metadata() + ->set_name(options.session_name); + } + + // Call AddDevices to register the XLA devices. + // + // It may be worth refactoring out XlaOpRegistry::RegisterCompilationDevice to + // make this more direct, but probably not worth it solely for this test. + std::vector> devices; + TF_RETURN_IF_ERROR(DeviceFactory::AddDevices(session_options, "", &devices)); + + GraphOptimizationPassOptions opt_options; + opt_options.graph = graph; + opt_options.session_options = &session_options; + opt_options.flib_def = flib_def; + + if (options.enable_cluster_scoping) { + ClusterScopingPass cluster_scoping_pass; + TF_RETURN_IF_ERROR(cluster_scoping_pass.Run(opt_options)); + } + + MarkForCompilationPass mark_for_compilation_pass; + return mark_for_compilation_pass.RunForTest( + opt_options, + /*disable_deadness_analysis=*/options.disable_deadness_analysis, + /*deterministic_cluster_names=*/options.deterministic_cluster_names); +} + +/*static*/ absl::Status MarkForCompilationPassTestHelper::MarkForCompilation( + std::unique_ptr* graph, + MarkForCompilationPassTestHelper::Options options) { + FunctionDefLibrary flib; + FunctionLibraryDefinition flib_def((*graph)->op_registry(), flib); + return MarkForCompilation(graph, &flib_def, options); +} +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/mark_for_compilation_pass_test_helper.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/mark_for_compilation_pass_test_helper.h new file mode 100644 index 0000000000000000000000000000000000000000..eeeae5ba5c232de4076c3cc8b46280ef1da9bd21 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/mark_for_compilation_pass_test_helper.h @@ -0,0 +1,85 @@ +/* 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_MARK_FOR_COMPILATION_PASS_TEST_HELPER_H_ +#define TENSORFLOW_COMPILER_JIT_MARK_FOR_COMPILATION_PASS_TEST_HELPER_H_ + +#include +#include +#include + +#include "tensorflow/compiler/jit/mark_for_compilation_pass.h" + +namespace tensorflow { +class MarkForCompilationPassTestHelper { + public: + struct Options { + bool enable_global_jit; + bool disable_deadness_analysis; + bool enable_cluster_scoping; + bool deterministic_cluster_names; + std::string session_name; // ConfigProto.Experimental.SessionMetadata.name + + Options() + : enable_global_jit(true), + disable_deadness_analysis(true), + enable_cluster_scoping(true), + deterministic_cluster_names(false) {} + + Options WithNoGlobalJit() { + Options copy = *this; + copy.enable_global_jit = false; + return copy; + } + + Options WithDeadnessAnalysis() { + Options copy = *this; + copy.disable_deadness_analysis = false; + return copy; + } + + Options WithNoClusterScoping() { + Options copy = *this; + copy.enable_cluster_scoping = false; + return copy; + } + + Options WithDeterministicClusterNames() { + Options copy = *this; + copy.deterministic_cluster_names = true; + return copy; + } + + Options WithSessionName(std::string name) { + Options copy = *this; + copy.session_name = std::move(name); + return copy; + } + }; + + // Runs the MarkForCompilation pass on `graph` after assigning all nodes in + // `graph` to the CPU device. To make testing easier, ignores device + // registration and _XlaCompile attributes. + static absl::Status MarkForCompilation(std::unique_ptr* graph, + FunctionLibraryDefinition* flib_def, + Options options = Options()); + + // Like `MarkForCompilation` but creates `flib_def` from the op registry. + static absl::Status MarkForCompilation(std::unique_ptr* graph, + Options options = Options()); +}; +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_MARK_FOR_COMPILATION_PASS_TEST_HELPER_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/node_matchers.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/node_matchers.cc new file mode 100644 index 0000000000000000000000000000000000000000..4d80b9910a73ef563852ffb8955edff981f40511 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/node_matchers.cc @@ -0,0 +1,562 @@ +/* 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 "tensorflow/compiler/jit/node_matchers.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "absl/algorithm/container.h" +#include "absl/log/check.h" +#include "absl/log/log.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/str_join.h" +#include "absl/strings/str_split.h" +#include "absl/strings/string_view.h" +#include "absl/types/span.h" +#include "tensorflow/cc/framework/ops.h" +#include "tensorflow/core/framework/attr_value_util.h" +#include "tensorflow/core/framework/node_def.pb.h" +#include "tensorflow/core/framework/node_def_util.h" +#include "tensorflow/core/framework/tensor.h" +#include "tensorflow/core/framework/tensor.pb.h" +#include "tensorflow/core/graph/graph.h" +#include "tensorflow/core/graph/graph_node_util.h" + +namespace tensorflow { +namespace testing { +namespace matchers { +namespace { + +using impl::NodeMatcherProperties; +using impl::OutEdge; + +std::string IndentAllButFirstLine(absl::string_view text) { + std::vector lines = absl::StrSplit(text, '\n'); + for (int i = 1; i < lines.size(); i++) { + lines[i].insert(0, " "); + } + return absl::StrJoin(lines, "\n"); +} + +template +bool CompareTensor(const Tensor& actual, const Tensor& expected, + ::testing::MatchResultListener* listener) { + if (actual.NumElements() != expected.NumElements()) { + if (listener->IsInterested()) { + *listener << "\nwas looking for tensor with " << expected.NumElements() + << " elements, found tensor with " << actual.NumElements() + << " elements"; + return false; + } + } + + for (int64_t i = 0, e = actual.NumElements(); i < e; i++) { + if (actual.flat()(i) != expected.flat()(i)) { + *listener << "\nmismatch in constant tensor at index " << i + << " expected = " << expected.flat()(i) + << " actual = " << actual.flat()(i); + return false; + } + } + + return true; +} + +bool MatchAndExplainTensor(const Tensor& tensor, const Tensor& expected_tensor, + ::testing::MatchResultListener* listener) { + if (tensor.dtype() != expected_tensor.dtype()) { + if (listener->IsInterested()) { + *listener << "\nexpected tensor of type " + << DataType_Name(expected_tensor.dtype()) + << " but found one of type " << DataType_Name(tensor.dtype()); + return false; + } + } + + switch (tensor.dtype()) { + case DT_HALF: + return CompareTensor(tensor, expected_tensor, listener); + case DT_FLOAT: + return CompareTensor(tensor, expected_tensor, listener); + case DT_DOUBLE: + return CompareTensor(tensor, expected_tensor, listener); + case DT_INT8: + return CompareTensor(tensor, expected_tensor, listener); + case DT_INT16: + return CompareTensor(tensor, expected_tensor, listener); + case DT_INT32: + return CompareTensor(tensor, expected_tensor, listener); + case DT_INT64: + return CompareTensor(tensor, expected_tensor, listener); + case DT_UINT8: + return CompareTensor(tensor, expected_tensor, listener); + case DT_UINT16: + return CompareTensor(tensor, expected_tensor, listener); + case DT_UINT32: + return CompareTensor(tensor, expected_tensor, listener); + case DT_UINT64: + return CompareTensor(tensor, expected_tensor, listener); + default: + LOG(FATAL) << "Unsupported dtype " // Crash ok: testonly. + << DataType_Name(tensor.dtype()); + } +} + +struct NodeMatcher : public ::testing::MatcherInterface { + bool MatchAndExplain( + const Node* node, + ::testing::MatchResultListener* listener) const override { + if (op && node->type_string() != *op) { + if (listener->IsInterested()) { + *listener << "\nexpected op " << *op << " but found " + << node->type_string(); + } + return false; + } + + if (assigned_device && node->assigned_device_name() != *assigned_device) { + if (listener->IsInterested()) { + *listener << "\nexpected assigned_device " << *assigned_device + << " but found \"" << node->assigned_device_name() << "\""; + } + return false; + } + + if (name && node->name() != *name) { + if (listener->IsInterested()) { + *listener << "\nexpected name " << *name << " but found " + << node->name(); + } + return false; + } + + if (constant_value) { + const TensorProto* proto = nullptr; + if (!TryGetNodeAttr(node->def(), "value", &proto)) { + if (listener->IsInterested()) { + *listener << "\ncould not find \"value\" attribute in node"; + } + return false; + } + + Tensor tensor(proto->dtype()); + if (!tensor.FromProto(*proto)) { + if (listener->IsInterested()) { + *listener << "\ncould not convert TensorProto in \"value\" attribute " + "to Tensor"; + } + return false; + } + + if (!MatchAndExplainTensor(/*tensor=*/tensor, + /*expected_tensor=*/*constant_value, + listener)) { + return false; + } + } + + if (input_matchers) { + if (input_matchers->size() != node->num_inputs()) { + if (listener->IsInterested()) { + *listener << "\nexpected " << input_matchers->size() + << " inputs but node has " << node->num_inputs(); + } + return false; + } + + for (int input_idx = 0, e = input_matchers->size(); input_idx < e; + input_idx++) { + if (!MatchAndExplainInput(node, input_idx, listener)) { + return false; + } + } + } + + std::vector control_deps; + for (const Edge* e : node->in_edges()) { + if (e->IsControlEdge()) { + control_deps.push_back(e->src()); + } + } + + ::testing::StringMatchResultListener inner_listener; + if (control_dep_set && + !control_dep_set->MatchAndExplain(control_deps, &inner_listener)) { + if (listener->IsInterested()) { + std::string explanation = inner_listener.str(); + if (!explanation.empty()) { + explanation = absl::StrCat(", ", explanation, ","); + } + *listener << "ctrl_deps" << explanation << " does not match expected: "; + control_dep_set->DescribeTo(listener->stream()); + } + return false; + } + + const AttrValueMap attr_value_map = node->def().attr(); + for (const auto& attr_kv_pair : attrs) { + auto it = attr_value_map.find(attr_kv_pair.first); + if (it == attr_value_map.end()) { + if (listener->IsInterested()) { + *listener << "did not find attribute named \"" << attr_kv_pair.first + << "\" in node"; + } + return false; + } + if (attr_kv_pair.second && + !AreAttrValuesEqual(it->second, *attr_kv_pair.second)) { + if (listener->IsInterested()) { + *listener << "attribute named " << attr_kv_pair.first + << " does not match value; expected: \"" + << SummarizeAttrValue(*attr_kv_pair.second) + << "\", found: \"" << SummarizeAttrValue(it->second) + << "\""; + } + return false; + } + } + + return true; + } + + void DescribeTo(::std::ostream* os) const override { + std::vector predicates; + + if (name) { + predicates.push_back(absl::StrCat("name: ", *name)); + } + + if (op) { + predicates.push_back(absl::StrCat("op: ", *op)); + } + + if (assigned_device) { + predicates.push_back(absl::StrCat("assigned device: ", *assigned_device)); + } + + bool printed_something = !predicates.empty(); + + *os << absl::StrJoin(predicates, ", "); + + if (constant_value) { + printed_something = true; + *os << "constant value: " << constant_value->DebugString(); + } + + if (input_matchers) { + if (!input_matchers->empty()) { + printed_something = true; + *os << " with " << (input_matchers->size() == 1 ? "only " : "") + << "input" << (input_matchers->size() == 1 ? "" : "s") << " "; + } + + if (input_matchers->size() == 1) { + ::std::stringstream ss; + input_matchers->front().DescribeTo(&ss); + printed_something = true; + *os << "matching " << ss.str(); + } else { + int edge_idx = 0; + for (const ::testing::Matcher& matcher : (*input_matchers)) { + *os << "\n [" << edge_idx << "] matching ("; + ::std::stringstream ss; + matcher.DescribeTo(&ss); + printed_something = true; + *os << IndentAllButFirstLine(ss.str()); + *os << ")"; + edge_idx++; + } + } + } + + if (control_dep_set) { + printed_something = true; + *os << " and control deps "; + control_dep_set->DescribeTo(os); + } + + if (!attrs.empty()) { + printed_something = true; + std::vector attrs_str; + absl::c_transform( + attrs, std::back_inserter(attrs_str), + [](const std::pair>& + attr_kv_pair) { + return absl::StrCat(attr_kv_pair.first, "->", + attr_kv_pair.second + ? SummarizeAttrValue(*attr_kv_pair.second) + : "*"); + }); + *os << " and attr values matching [" << absl::StrJoin(attrs_str, ", ") + << "]"; + } + + if (!printed_something) { + *os << "is any node"; + } + } + + bool MatchAndExplainInput(const Node* node, int input_idx, + ::testing::MatchResultListener* listener) const { + const Edge* edge; + if (!node->input_edge(input_idx, &edge).ok()) { + if (listener->IsInterested()) { + *listener << "\ncould not find incoming edge for input " << input_idx; + } + return false; + } + + ::testing::StringMatchResultListener inner_listener; + OutEdge input = {edge->src(), edge->src_output()}; + if ((*input_matchers)[input_idx].MatchAndExplain(input, &inner_listener)) { + return true; + } + + if (listener->IsInterested()) { + *listener << "\ninput " << input_idx << " does not match expected:\n"; + (*input_matchers)[input_idx].DescribeTo(listener->stream()); + std::string explanation = inner_listener.str(); + if (!explanation.empty()) { + *listener << ", " << explanation; + } + } + return false; + } + + std::optional op; + std::optional name; + std::optional assigned_device; + std::optional constant_value; + std::optional>> input_matchers; + std::optional<::testing::Matcher>> + control_dep_set; + std::map> attrs; +}; + +// Matches a dst and dst_output on an input edge. Today we only use this with +// dst_output=0 but we will eventually need to support multi-output operations. +class OutEdgeMatcher : public ::testing::MatcherInterface { + public: + OutEdgeMatcher(::testing::Matcher src_matcher, int src_oidx) + : src_matcher_(std::move(src_matcher)), src_oidx_(src_oidx) {} + + bool MatchAndExplain( + OutEdge out_edge, + ::testing::MatchResultListener* listener) const override { + ::testing::StringMatchResultListener inner_listener; + if (!src_matcher_.MatchAndExplain(out_edge.first, &inner_listener)) { + if (listener->IsInterested()) { + *listener << "\nsource does not match expected "; + src_matcher_.DescribeTo(listener->stream()); + std::string explanation = inner_listener.str(); + if (!explanation.empty()) { + *listener << "\n\t" << explanation; + } + } + return false; + } + if (out_edge.second != src_oidx_) { + if (listener->IsInterested()) { + *listener << "\nexpected output slot to be " << src_oidx_ + << " but found " << out_edge.second; + } + return false; + } + + return true; + } + + void DescribeTo(::std::ostream* os) const override { + if (src_oidx_) { + *os << "output slot: " << src_oidx_ << ", source: ("; + } + + src_matcher_.DescribeTo(os); + + if (src_oidx_) { + *os << ")"; + } + } + + private: + ::testing::Matcher src_matcher_; + int src_oidx_; +}; +} // namespace + +::testing::Matcher impl::NodeWith( + absl::Span props) { + NodeMatcher* matcher = new NodeMatcher(); + for (const NodeMatcherProperties& prop : props) { + if (prop.name()) { + DCHECK(!matcher->name); + matcher->name = prop.name(); + } + + if (prop.op()) { + DCHECK(!matcher->op); + matcher->op = prop.op(); + } + + if (prop.constant_value()) { + DCHECK(!matcher->constant_value); + matcher->constant_value = prop.constant_value(); + } + + if (prop.assigned_device()) { + DCHECK(!matcher->assigned_device); + matcher->assigned_device = prop.assigned_device(); + } + + if (prop.inputs()) { + DCHECK(!matcher->input_matchers); + matcher->input_matchers = *prop.inputs(); + } + + if (prop.control_deps()) { + DCHECK(!matcher->control_dep_set); + matcher->control_dep_set = + ::testing::UnorderedElementsAreArray(*prop.control_deps()); + } + + if (prop.attr()) { + auto insert_result = matcher->attrs.insert(*prop.attr()); + DCHECK(insert_result.second); + } + } + + return ::testing::MakeMatcher(matcher); +} + +impl::NodeMatcherProperties Name(std::string name) { + impl::NodeMatcherProperties props; + props.set_name(std::move(name)); + return props; +} + +// Matches a node with op `op`. +impl::NodeMatcherProperties Op(std::string op) { + impl::NodeMatcherProperties props; + props.set_op(std::move(op)); + return props; +} + +// Matches a node with assigned device `assigned_device`. +impl::NodeMatcherProperties AssignedDevice(std::string assigned_device) { + impl::NodeMatcherProperties props; + props.set_assigned_device(std::move(assigned_device)); + return props; +} + +impl::NodeMatcherProperties impl::Inputs( + absl::Span> inputs) { + std::vector<::testing::Matcher> inputs_vector; + absl::c_copy(inputs, std::back_inserter(inputs_vector)); + + impl::NodeMatcherProperties props; + props.set_inputs(std::move(inputs_vector)); + return props; +} + +impl::NodeMatcherProperties impl::CtrlDeps( + absl::Span> control_deps) { + std::vector<::testing::Matcher> control_deps_vector; + absl::c_copy(control_deps, std::back_inserter(control_deps_vector)); + + impl::NodeMatcherProperties props; + props.set_control_deps(std::move(control_deps_vector)); + return props; +} + +std::pair impl::AttrLiteralHelper( + const std::pair& bool_attr) { + AttrValue attr_value; + attr_value.set_b(bool_attr.second); + return {bool_attr.first, attr_value}; +} + +std::pair impl::AttrLiteralHelper( + const std::pair>& int_list_attr) { + AttrValue attr_value; + AttrValue::ListValue* list = attr_value.mutable_list(); + for (int i : int_list_attr.second) { + list->add_i(i); + } + return {int_list_attr.first, attr_value}; +} + +std::pair impl::AttrLiteralHelper( + const std::pair>& + string_list_attr) { + AttrValue attr_value; + AttrValue::ListValue* list = attr_value.mutable_list(); + for (const std::string& s : string_list_attr.second) { + list->add_s(s); + } + return {string_list_attr.first, attr_value}; +} + +impl::NodeMatcherProperties impl::Attr(std::pair attr) { + impl::NodeMatcherProperties props; + props.set_attr(std::move(attr)); + return props; +} + +impl::NodeMatcherProperties impl::Attr(std::string name) { + impl::NodeMatcherProperties props; + props.set_attr({std::move(name), std::nullopt}); + return props; +} + +NodeMatcherProperties ConstantValue( + const ::tensorflow::Input::Initializer& val) { + CHECK_OK(val.status); + NodeMatcherProperties props; + props.set_constant_value(val.tensor); + return props; +} + +::testing::Matcher Const( + const ::tensorflow::Input::Initializer& val) { + return Out(NodeWith(ConstantValue(val))); +} +::testing::Matcher Out( + int oidx, ::testing::Matcher node_matcher) { + return ::testing::MakeMatcher(new OutEdgeMatcher(node_matcher, oidx)); +} +} // namespace matchers + +Node* FindNodeByName(Graph* g, absl::string_view name) { + for (Node* n : g->nodes()) { + if (n->name() == name) { + return n; + } + } + + return nullptr; +} +} // namespace testing + +void PrintTo(const Node* n, ::std::ostream* os) { *os << SummarizeNode(*n); } +void PrintTo(Node* n, ::std::ostream* os) { *os << SummarizeNode(*n); } +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/node_matchers.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/node_matchers.h new file mode 100644 index 0000000000000000000000000000000000000000..39e134410b1d1c2fc213d6445d1137c291aed33e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/node_matchers.h @@ -0,0 +1,252 @@ +/* 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. +==============================================================================*/ + +// Provides a set of matchers for tensorflow nodes. +// +// Example usage: +// +// tensorflow::Node* node = ...; +// EXPECT_THAT(node, NodeWith(Name("name"), Op("op"), +// Inputs(Out(3, NodeWith(Name("input")))))) +// +// Matchable node properties (the expressions that go inside NodeWith(...)) +// are: +// +// - Name(string): matches the node name exactly. We will probably need to +// have this take a string matcher soon in the future. +// +// - Op(string): matches the op exactly. +// +// - AssignedDevice(string): matches the assigned device exactly. +// +// - Inputs(): matches the list of non-control inputs to the node +// exactly (i.e. does not match a suffix or a prefix) where each element +// matches an output of a node (see Out(idx, node) below). +// +// - CtrlDeps(): matches the list of control dependences on the +// node exactly but in any order. +// +// - ConstantValue(tensorflow::Input::Initializer init): matches a Const node +// with the constant value `init`. Implies Op("Const"). +// +// - Attr(name, value): Matches a single attribute with name `name` and value +// `value`. Right now only boolean values are supported. +// +// Overlapping node properties may not be repeated in a single NodeWith(...) +// matcher. E.g. NodeWith(Op("Foo"), Op("Bar")) will CHECK-fail. Since +// ConstantValue implies Op("Const"), a single NodeWith matcher can't have both +// ConstantValue(...) and Op(...). Multiple Attr() values can be combined as +// long as the attribute names are different. +// +// Out(idx, node) matches the `idx`'th output of a node that matches `node`. + +#ifndef TENSORFLOW_COMPILER_JIT_NODE_MATCHERS_H_ +#define TENSORFLOW_COMPILER_JIT_NODE_MATCHERS_H_ + +#include +#include +#include + +#include "absl/algorithm/container.h" +#include "absl/strings/string_view.h" +#include "absl/types/optional.h" +#include "absl/types/span.h" +#include "tensorflow/cc/framework/ops.h" +#include "xla/hlo/testlib/test.h" +#include "tensorflow/core/graph/graph.h" + +namespace tensorflow { +namespace testing { +namespace matchers { + +namespace impl { + +using OutEdge = std::pair; + +// ----------------------------------------------------------------------------- +// Implementation details. + +// Properties that we match on for a particular Node. If a particular property +// is nullopt then any value for it is allowed. +class NodeMatcherProperties { + public: + using NodeSeqMatcher = std::vector<::testing::Matcher>; + using InputSeqMatcher = std::vector<::testing::Matcher>; + using AttrKeyValuePair = std::pair>; + + const std::optional& name() const { return name_; } + const std::optional& op() const { return op_; } + const std::optional& assigned_device() const { + return assigned_device_; + } + const std::optional& constant_value() const { + return constant_value_; + } + const std::optional& inputs() const { + return input_matchers_; + } + const std::optional& control_deps() const { + return control_deps_; + } + const std::optional& attr() const { return attr_; } + + void set_name(std::string name) { + DCHECK(IsEmpty()); + name_ = std::move(name); + } + + void set_op(std::string op) { + DCHECK(IsEmpty()); + op_ = std::move(op); + } + + void set_assigned_device(std::string assigned_device) { + DCHECK(IsEmpty()); + assigned_device_ = std::move(assigned_device); + } + + void set_constant_value(Tensor constant_value) { + DCHECK(IsEmpty()); + constant_value_ = std::move(constant_value); + op_ = "Const"; + } + + void set_inputs(InputSeqMatcher inputs) { + DCHECK(IsEmpty()); + input_matchers_ = std::move(inputs); + } + + void set_control_deps(NodeSeqMatcher control_deps) { + DCHECK(IsEmpty()); + control_deps_ = std::move(control_deps); + } + + void set_attr(AttrKeyValuePair attr) { + DCHECK(IsEmpty()); + attr_ = std::move(attr); + } + + bool IsEmpty() const { + return !name().has_value() && !op().has_value() && !inputs().has_value() && + !control_deps().has_value() && !attr().has_value(); + } + + private: + std::optional name_; + std::optional op_; + std::optional assigned_device_; + std::optional constant_value_; + std::optional input_matchers_; + std::optional control_deps_; + std::optional attr_; +}; + +::testing::Matcher NodeWith( + absl::Span props); + +impl::NodeMatcherProperties Inputs( + absl::Span> inputs); + +impl::NodeMatcherProperties CtrlDeps( + absl::Span> control_deps); + +impl::NodeMatcherProperties Attr(std::pair attrs); +impl::NodeMatcherProperties Attr(std::string name); + +std::pair AttrLiteralHelper( + const std::pair& bool_attr); + +std::pair AttrLiteralHelper( + const std::pair>& int_list_attr); + +std::pair AttrLiteralHelper( + const std::pair>& + string_list_attr); +} // namespace impl + +// ----------------------------------------------------------------------------- +// Public interface. + +// Matches a node with name `name`. +impl::NodeMatcherProperties Name(std::string name); + +// Matches a node with op `op`. +impl::NodeMatcherProperties Op(std::string op); + +// Matches a node with assigned device `assigned_device`. +impl::NodeMatcherProperties AssignedDevice(std::string assigned_device); + +// Matches a node with a boolean typed attribute named `name` and with value +// `value`. +template +impl::NodeMatcherProperties Attr(const std::string& name, ValueTy value) { + return impl::Attr({impl::AttrLiteralHelper({name, value})}); +} + +inline impl::NodeMatcherProperties Attr(const std::string& name) { + return impl::Attr(name); +} + +// Matches a node with inputs `inputs`. +// +// `inputs` are ordered; `inputs`[i] must match input i. +template +impl::NodeMatcherProperties Inputs(Ts... inputs) { + return impl::Inputs({inputs...}); +} + +// Matches the `idx`'th output of a node that matches `node`. +::testing::Matcher Out(int oidx, + ::testing::Matcher node); + +// Matches the first output of a node that matches `node`. +inline ::testing::Matcher Out( + ::testing::Matcher node) { + return Out(0, node); +} + +// Matches a node with control dependences `control_deps`. +// +// `control_deps` are unordered and will match the control deps of a node in any +// order. +template +impl::NodeMatcherProperties CtrlDeps(Ts... control_deps) { + return impl::CtrlDeps({control_deps...}); +} + +// Matches a constant node with value `val`. +impl::NodeMatcherProperties ConstantValue( + const ::tensorflow::Input::Initializer& val); + +// The main gmock matcher. See file comment for example usage. +template +::testing::Matcher NodeWith(Ts... args) { + std::array array = {args...}; + return impl::NodeWith(array); +} + +::testing::Matcher Const( + const ::tensorflow::Input::Initializer& val); +} // namespace matchers + +// If `g` has a node named `name` returns it, otherwise returns null. +Node* FindNodeByName(Graph* g, absl::string_view name); +} // namespace testing + +void PrintTo(const Node* n, ::std::ostream* os); +void PrintTo(Node* n, ::std::ostream* os); +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_NODE_MATCHERS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/node_matchers_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/node_matchers_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..8359accd815e1f801aecf074af17145c8766840a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/node_matchers_test.cc @@ -0,0 +1,230 @@ +/* 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 "tensorflow/compiler/jit/node_matchers.h" + +#include + +#include "tensorflow/cc/framework/ops.h" +#include "tensorflow/cc/ops/array_ops.h" +#include "tensorflow/cc/ops/const_op.h" +#include "tensorflow/cc/ops/control_flow_ops.h" +#include "tensorflow/cc/ops/control_flow_ops_internal.h" +#include "tensorflow/cc/ops/math_ops.h" + +namespace tensorflow { +namespace testing { +namespace { + +using ::testing::_; + +using testing::matchers::AssignedDevice; +using testing::matchers::Attr; +using testing::matchers::ConstantValue; +using testing::matchers::CtrlDeps; +using testing::matchers::Inputs; +using testing::matchers::Name; +using testing::matchers::NodeWith; +using testing::matchers::Op; +using testing::matchers::Out; + +template +std::string Explain(const T& t, const M& m) { + ::testing::StringMatchResultListener listener; + EXPECT_THAT(t, ::testing::Not(m)); // For the error message. + EXPECT_FALSE(m.MatchAndExplain(t, &listener)); + return listener.str(); +} + +TEST(NodeMatchers, CheckAgainstConstant) { + Scope root = Scope::NewRootScope().ExitOnError(); + Output placeholder = + ops::Placeholder(root.WithOpName("placeholder"), DT_FLOAT); + + EXPECT_THAT(placeholder.node(), NodeWith(Op("Placeholder"))); + EXPECT_THAT(placeholder.node(), NodeWith(Name("placeholder"))); + EXPECT_THAT(placeholder.node(), + NodeWith(Op("Placeholder"), Name("placeholder"))); + EXPECT_THAT(placeholder.node(), + NodeWith(Name("placeholder"), Op("Placeholder"))); + EXPECT_THAT(placeholder.node(), NodeWith(Inputs())); + EXPECT_THAT(placeholder.node(), + NodeWith(Op("Placeholder"), Name("placeholder"), Inputs())); + + EXPECT_EQ(Explain(placeholder.node(), NodeWith(Op("Add"))), + "\nexpected op Add but found Placeholder"); + EXPECT_EQ(Explain(placeholder.node(), NodeWith(Name("add"))), + "\nexpected name add but found placeholder"); + EXPECT_EQ(Explain(placeholder.node(), NodeWith(Inputs(Out(NodeWith())))), + "\nexpected 1 inputs but node has 0"); +} + +TEST(NodeMatchers, CheckAgainstBinary) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Output placeholder_a = + ops::Placeholder(root.WithOpName("placeholder_a"), DT_FLOAT); + Output placeholder_b = + ops::Placeholder(root.WithOpName("placeholder_b"), DT_FLOAT); + Output add = ops::Add(root.WithOpName("add"), placeholder_a, placeholder_b); + + EXPECT_THAT(add.node(), + NodeWith(Op("Add"), Name("add"), + Inputs(Out(NodeWith(Name("placeholder_a"))), + Out(NodeWith(Name("placeholder_b")))))); + + EXPECT_EQ(Explain(add.node(), NodeWith(Inputs())), + "\nexpected 0 inputs but node has 2"); + EXPECT_EQ( + Explain(add.node(), NodeWith(Inputs(Out(NodeWith(Name("blah"))), _))), + "\ninput 0 does not match expected:\nname: blah, \nsource does not match " + "expected name: blah\n\t\nexpected name blah but found placeholder_a"); + EXPECT_EQ( + Explain(add.node(), NodeWith(Inputs(_, Out(NodeWith(Name("blah")))))), + "\ninput 1 does not match expected:\nname: blah, \nsource does not match " + "expected name: blah\n\t\nexpected name blah but found placeholder_b"); +} + +TEST(NodeMatchers, CheckControlDependence) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Output placeholder_a = + ops::Placeholder(root.WithOpName("placeholder_a"), DT_FLOAT); + Output placeholder_b = + ops::Placeholder(root.WithOpName("placeholder_b"), DT_FLOAT); + Output placeholder_c = + ops::Placeholder(root.WithOpName("placeholder_c"), DT_FLOAT); + Output placeholder_d = + ops::Placeholder(root.WithOpName("placeholder_d"), DT_FLOAT); + + root.graph()->AddControlEdge(placeholder_a.node(), placeholder_c.node()); + root.graph()->AddControlEdge(placeholder_b.node(), placeholder_c.node()); + + EXPECT_THAT(placeholder_c.node(), + NodeWith(Name("placeholder_c"), + CtrlDeps(NodeWith(Name("placeholder_a")), + NodeWith(Name("placeholder_b"))))); + EXPECT_THAT(placeholder_d.node(), + NodeWith(Name("placeholder_d"), CtrlDeps())); + + // TODO(griffithjames): Exactly match these explanations. + // + // When the OSS build has been updated to include the new error messages, the + // Explain() expectations can be exact strings again. + { + const std::string explanation = + Explain(placeholder_c.node(), NodeWith(CtrlDeps())); + EXPECT_NE(explanation.find("ctrl_deps, which has 2 elements"), + std::string::npos); + EXPECT_NE(explanation.find("does not match expected: is empty"), + std::string::npos); + } + { + const std::string explanation = + Explain(placeholder_d.node(), NodeWith(CtrlDeps(NodeWith()))); + EXPECT_NE(explanation.find("ctrl_deps"), std::string::npos); + EXPECT_NE(explanation.find("does not match expected: has 1 element and " + "that element is any node"), + std::string::npos); + } +} + +TEST(NodeMatchers, ConstValue) { + Scope root = Scope::NewRootScope().ExitOnError(); + Output placeholder = + ops::Placeholder(root.WithOpName("placeholder"), DT_FLOAT); + Output const_0d = ops::Const(root.WithOpName("const_0d"), 42); + + Output const_2d = ops::Const(root.WithOpName("const_2d"), {{1, 2}, {4, 3}}); + + EXPECT_THAT(const_0d.node(), NodeWith(ConstantValue(42))); + EXPECT_THAT(const_0d.node(), NodeWith(ConstantValue(42), Name("const_0d"))); + + EXPECT_THAT(const_2d.node(), NodeWith(ConstantValue({{1, 2}, {4, 3}}))); + + EXPECT_EQ(Explain(placeholder.node(), NodeWith(ConstantValue(42))), + "\nexpected op Const but found Placeholder"); + EXPECT_EQ( + Explain(const_0d.node(), NodeWith(ConstantValue(43))), + "\nmismatch in constant tensor at index 0 expected = 43 actual = 42"); + EXPECT_EQ( + Explain(const_0d.node(), NodeWith(ConstantValue({{1, 2}, {4, 3}}))), + "\nwas looking for tensor with 4 elements, found tensor with 1 elements"); + EXPECT_EQ( + Explain(const_2d.node(), NodeWith(ConstantValue(42))), + "\nwas looking for tensor with 1 elements, found tensor with 4 elements"); +} + +TEST(NodeMatchers, AssignedDevice) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Output placeholder_a = + ops::Placeholder(root.WithOpName("placeholder_a"), DT_FLOAT); + Output placeholder_b = + ops::Placeholder(root.WithOpName("placeholder_b"), DT_FLOAT); + + Output assigned_add = + ops::Add(root.WithOpName("assigned_add"), placeholder_a, placeholder_b); + assigned_add.node()->set_assigned_device_name( + "/job:localhost/replica:0/task:0/device:CPU:0"); + + Output unassigned_add = + ops::Add(root.WithOpName("unassigned_add"), placeholder_a, placeholder_b); + + EXPECT_THAT( + assigned_add.node(), + NodeWith(AssignedDevice("/job:localhost/replica:0/task:0/device:CPU:0"))); + EXPECT_THAT(unassigned_add.node(), NodeWith(AssignedDevice(""))); + + EXPECT_EQ(Explain(unassigned_add.node(), + NodeWith(AssignedDevice( + "/job:localhost/replica:0/task:0/device:CPU:0"))), + "\nexpected assigned_device " + "/job:localhost/replica:0/task:0/device:CPU:0 but found \"\""); +} + +TEST(NodeMatchers, OutputIndices) { + Scope root = Scope::NewRootScope().ExitOnError(); + Output pred = ops::Placeholder(root.WithOpName("pred"), DT_BOOL); + + Output data = ops::Placeholder(root.WithOpName("data"), DT_FLOAT); + ops::Switch sw(root.WithOpName("switch"), data, pred); + Output add = ops::Add(root.WithOpName("add"), sw.output_true, + ops::Placeholder(root.WithOpName("addend"), DT_FLOAT)); + + EXPECT_THAT(add.node(), NodeWith(Inputs(Out(1, NodeWith(Op("Switch"))), _))); + EXPECT_EQ( + Explain(add.node(), NodeWith(Inputs(Out(0, NodeWith(Op("Switch"))), _))), + "\ninput 0 does not match expected:\nop: Switch, \nexpected output slot " + "to be 0 but found 1"); +} + +TEST(NodeMatchers, Attrs) { + Scope root = Scope::NewRootScope().ExitOnError(); + Output enter = ops::internal::Enter( + root.WithOpName("enter"), + ops::Placeholder(root.WithOpName("data"), DT_FLOAT), "frame_name", + ops::internal::Enter::Attrs{}.IsConstant(true)); + EXPECT_THAT(enter.node(), NodeWith(Attr("is_constant", true))); + EXPECT_EQ(Explain(enter.node(), NodeWith(Attr("is_constant", false))), + "attribute named is_constant does not match value; expected: " + "\"false\", found: \"true\""); + EXPECT_EQ(Explain(enter.node(), NodeWith(Attr("missing_attr", false))), + "did not find attribute named \"missing_attr\" in node"); +} + +} // namespace +} // namespace testing +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/ops/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/jit/ops/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..0026fa1fde6dbb66af7e13e2a38c48c25f7e2867 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/ops/BUILD @@ -0,0 +1,43 @@ +load("@xla//third_party/rules_python/python:py_library.bzl", "py_library") +load("//tensorflow:tensorflow.bzl", "tf_gen_op_wrapper_py") +load("//tensorflow/core/platform:rules_cc.bzl", "cc_library") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:license"], + default_visibility = ["//tensorflow/compiler/tf2xla:internal"], + licenses = ["notice"], +) + +cc_library( + name = "xla_ops", + srcs = ["xla_ops.cc"], + deps = [ + "//tensorflow/core:framework", + "@com_google_absl//absl/status", + ], + alwayslink = 1, +) + +tf_gen_op_wrapper_py( + name = "xla_ops_wrapper_py", + out = "xla_ops.py", + extra_py_deps = [ + "//tensorflow/python:pywrap_tfe", + "//tensorflow/python/util:dispatch", + "//tensorflow/python/util:deprecation", + "//tensorflow/python/util:tf_export", + ], + py_lib_rule = py_library, + deps = ["//tensorflow/compiler/jit/ops:xla_ops"], +) + +py_library( + name = "xla_ops_grad", + srcs = ["xla_ops_grad.py"], + strict_deps = True, + visibility = [ + "//tensorflow/compiler/tf2xla:internal", + "//tensorflow/python/ops:__pkg__", + ], + deps = ["//tensorflow/python/framework:ops"], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/ops/xla_ops.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/ops/xla_ops.cc new file mode 100644 index 0000000000000000000000000000000000000000..dbcf9e553114bcb1052a80d771b10dffbdc99be5 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/ops/xla_ops.cc @@ -0,0 +1,127 @@ +/* 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 "absl/status/status.h" +#include "tensorflow/core/framework/op.h" +#include "tensorflow/core/framework/shape_inference.h" + +namespace tensorflow { + +using shape_inference::InferenceContext; + +REGISTER_OP("XlaLaunch") + .Input("constants: Tconstants") + .Attr("Tconstants: list(type) >= 0") + .Input("args: Targs") + .Attr("Targs: list(type) >= 0") + .Input("resources: Nresources * resource") + .Attr("Nresources: int >= 0") + .Output("results: Tresults") + .Attr("Tresults: list(type) >= 0") + .Attr("function: func") + // XLA random-number generation ops are stateful. + // TODO(phawkins): create stateful and non-stateful variants of XlaLaunch. + .SetIsStateful() + .Doc("XLA Launch Op. For use by the XLA JIT only."); + +REGISTER_OP("XlaLaunchV2") + .Input("args: Targs") + .Output("results: Tresults") + .Attr("Targs: list(type) >= 0") + .Attr("Tresults: list(type) >= 0") + .Attr("constants: list(int) >= 0") + .Attr("resources: list(int) >= 0") + .Attr("function: func") + .Doc("XLA Launch Op. For use by the XLA JIT only."); + +REGISTER_OP("XlaClusterOutput") + .Input("input: T") + // Note: when replication is supported, this op will have N outputs. + .Output("outputs: T") + .Attr("T: type") + .SetShapeFn([](InferenceContext* c) { + for (int i = 0; i < c->num_outputs(); ++i) { + c->set_output(i, c->input(0)); + } + return absl::OkStatus(); + }) + .Doc( + "Operator that connects the output of an XLA computation to other " + "consumer graph nodes."); + +REGISTER_OP("_XlaCompile") + .Input("constants: Tconstants") + .Attr("Tconstants: list(type) >= 0") + .Attr("must_compile: bool") + .Input("args: Targs") + .Attr("Targs: list(type) >= 0") + .Input("resources: Nresources * resource") + .Attr("Nresources: int >= 0") + .Output("key: string") + .Output("compilation_successful: bool") + .Attr("function: func") + // The compilation cache is stateful. + .SetIsStateful() + .Doc(R"(XLA Compile Op. For use by the XLA JIT only. + +Compiles a TensorFlow function into an XLA LocalExecutable and returns a key +that _XlaRun can use to look up the LocalExecutable and execute it. + +key: A key that can be used to look up the local executable compiled by the + node and associated metadata. + +compilation_successful: If the `must_compile` attr is false the _XlaCompile op + can decide not to compile the clusters based on some profitability + heuristics. In that case `compilation_successful` is false if _XlaCompile + chose not to compile the cluster. If the `must_compile` attr is true then + _XlaCompile always attempts to compile the cluster and + `compilation_successful` is always true. +)"); + +REGISTER_OP("_XlaRun") + .Input("args: Targs") + .Attr("Targs: list(type) >= 0") + .Output("results: Tresults") + .Attr("Tresults: list(type) >= 0") + .Input("key: string") + // XLA random-number generation ops are stateful. + // TODO(phawkins): create stateful and non-stateful variants of _XlaRun. + .SetIsStateful() + .Doc(R"(XLA Run Op. For use by the XLA JIT only. + +Executes a TensorFlow function previously compiled into a LocalExecutable by an +_XlaCompile op. +)"); + +REGISTER_OP("_XlaMerge") + .Input("partitioned_call: T") + .Input("xla_run: T") + .Output("output: T") + .Attr("T: type") + .SetShapeFn([](InferenceContext* c) { + c->set_output(0, c->input(0)); + return absl::OkStatus(); + }) + .Doc(R"(XLA Merge Op. For use by the XLA JIT only. + +Merges the outputs from the PartitionedCall node and the _XlaRun node. +Unlike the TensorFlow Merge op, which requires inputs of some types to be +placed on the host, the _XlaMerge op can merge inputs of all types when +placed on the device. This prevents the need for copy operations, in +particular when an XLA cluster has int32 outputs. The _XlaMerge up does not +have a value_index output that identifies the chosen input. +)"); + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/ops/xla_ops_grad.py b/western_ai/usa/tensorflow/tensorflow/compiler/jit/ops/xla_ops_grad.py new file mode 100644 index 0000000000000000000000000000000000000000..d9937f413c8367dd90a7fd94055329e81d5cb68e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/ops/xla_ops_grad.py @@ -0,0 +1,25 @@ +"""Gradients for XLA ops.""" +# 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. +# ============================================================================== + +from tensorflow.python.framework import ops + + +@ops.RegisterGradient("XlaClusterOutput") +def _XlaClusterOutputGrad(_, grad): + del grad # unused + raise RuntimeError("Gradient computation of graph in xla.compile() is " + "prohibited because it can cause performance degradation." + "Please move gradient computation inside xla.compile().") diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/package_groups.bzl b/western_ai/usa/tensorflow/tensorflow/compiler/jit/package_groups.bzl new file mode 100644 index 0000000000000000000000000000000000000000..54031a4e5767e2eea6018011b9dc25a4fd8b7933 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/package_groups.bzl @@ -0,0 +1,13 @@ +"""Tensorflow JIT package_group definitions""" + +def legacy_jit_users_package_group(name): + """Defines visibility group for //third_party/tensorflow/compiler/jit. + + Args: + name: package group name + """ + + native.package_group( + name = name, + packages = ["//..."], + ) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/partially_decluster_pass.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/partially_decluster_pass.cc new file mode 100644 index 0000000000000000000000000000000000000000..7b41c6f295eb32564c81484fa8f92a5cfef02bf7 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/partially_decluster_pass.cc @@ -0,0 +1,435 @@ +/* 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 "tensorflow/compiler/jit/partially_decluster_pass.h" + +#include "absl/algorithm/container.h" +#include "absl/container/flat_hash_set.h" +#include "absl/strings/str_cat.h" +#include "tensorflow/compiler/jit/device_util.h" +#include "tensorflow/compiler/jit/xla_cluster_util.h" +#include "tensorflow/compiler/tf2xla/const_analysis.h" +#include "tensorflow/compiler/tf2xla/xla_op_registry.h" +#include "tensorflow/core/common_runtime/function.h" +#include "tensorflow/core/framework/function.h" +#include "tensorflow/core/framework/memory_types.h" +#include "tensorflow/core/framework/node_def.pb.h" +#include "tensorflow/core/framework/op_kernel.h" +#include "tensorflow/core/graph/graph_node_util.h" +#include "tensorflow/core/lib/core/errors.h" +#include "tensorflow/core/public/version.h" + +namespace tensorflow { +namespace { + +bool NotBackedge(const Edge& edge) { return !edge.src()->IsNextIteration(); } + +namespace reduce_device_to_host_copies { +absl::Status FindNodesToDecluster(const Graph& graph, + absl::flat_hash_set* result, + absl::Span post_order) { + // Find nodes that have at least one user outside their cluster that expects + // hostmem output. These nodes should be cloned to outside the cluster to + // avoid the device-host copy we'd otherwise need. + + MemoryTypeVector input_mtypes, output_mtypes; + + for (Node* n : post_order) { + std::optional from_cluster = GetXlaClusterForNode(*n); + if (!from_cluster) { + continue; + } + + // Assume the benefit of not outputting a larger tensor outweighs the + // benefit of this check. + // TODO(tpopp): Only apply this if the value being consumed is not output + // from the cluster to another consumer. + // TODO(tpopp): See if XlaRun can be modified to avoid this issue + // completely. + if (IsShapeConsumerOp(*n)) { + continue; + } + // We assume the only XLA-auto-clusterable operations with side effects are + // resource variable updates. We can't execute these twice. + if (HasResourceInputOrOutput(*n)) { + continue; + } + + DeviceType device_type(""); + TF_RETURN_IF_ERROR( + DeviceNameToDeviceType(n->assigned_device_name(), &device_type)); + TF_RETURN_IF_ERROR(MemoryTypesForNode(graph.op_registry(), device_type, + n->def(), &input_mtypes, + &output_mtypes)); + for (const Edge* e : n->out_edges()) { + Node* dst = e->dst(); + + if (e->IsControlEdge()) { + continue; + } + + bool edge_incurs_extra_device_to_host_copy; + if (output_mtypes[e->src_output()] == DEVICE_MEMORY) { + // If the output of the *TensorFlow* operation is in DEVICE_MEMORY then + // keep the node clustered -- XLA will also produce the output in device + // memory and we will get some benefit from clustering. + edge_incurs_extra_device_to_host_copy = false; + } else { + MemoryTypeVector dst_input_mtypes, dst_output_mtypes; + DeviceType dst_device_type(""); + TF_RETURN_IF_ERROR(DeviceNameToDeviceType(dst->assigned_device_name(), + &dst_device_type)); + TF_RETURN_IF_ERROR(MemoryTypesForNode(graph.op_registry(), device_type, + dst->def(), &dst_input_mtypes, + &dst_output_mtypes)); + edge_incurs_extra_device_to_host_copy = + dst_input_mtypes[e->dst_input()] == HOST_MEMORY; + } + + if (!edge_incurs_extra_device_to_host_copy) { + continue; + } + + // Check if `dst` is in a different cluster, unclustered, or about to be + // partially declustered (here we rely on the post-order traversal order). + // If yes, decluster `n` to avoid the device-to-host memcpy. + std::optional dst_cluster = + result->count(dst) ? std::nullopt : GetXlaClusterForNode(*dst); + if (from_cluster != dst_cluster) { + CHECK(result->insert(n).second); + break; + } + } + } + return absl::OkStatus(); +} + +absl::Status PartiallyDeclusterNode(Graph* graph, Node* n) { + absl::string_view cluster_name = *GetXlaClusterForNode(*n); + absl::InlinedVector out_edges_to_clone; + for (const Edge* out_edge : n->out_edges()) { + if (out_edge->IsControlEdge()) { + continue; + } + + Node* dst = out_edge->dst(); + std::optional dst_cluster_name = + GetXlaClusterForNode(*dst); + if (dst_cluster_name != cluster_name) { + out_edges_to_clone.push_back(out_edge); + } + } + + CHECK(!out_edges_to_clone.empty()) << n->DebugString(); + + NodeDef ndef = n->def(); + ndef.set_name(absl::StrCat(n->name(), "/declustered")); + MergeDebugInfo(NodeDebugInfo(n->def()), &ndef); + RemoveFromXlaCluster(&ndef); + TF_ASSIGN_OR_RETURN(Node * cloned_node, graph->AddNode(ndef)); + cloned_node->set_assigned_device_name(n->assigned_device_name()); + + for (const Edge* in_edge : n->in_edges()) { + graph->AddEdge(in_edge->src(), in_edge->src_output(), cloned_node, + in_edge->dst_input()); + } + + for (const Edge* out_edge_to_clone : out_edges_to_clone) { + graph->AddEdge(cloned_node, out_edge_to_clone->src_output(), + out_edge_to_clone->dst(), out_edge_to_clone->dst_input()); + graph->RemoveEdge(out_edge_to_clone); + } + + if (n->out_edges().empty()) { + graph->RemoveNode(n); + } + + return absl::OkStatus(); +} + +// Clones nodes to outside their cluster to avoid device-to-host copies. For +// instance, converts this: +// +// ..... +// | +// v +// A_Clustered ====> C_Unclustered +// | +// v +// B_Clustered +// +// to: +// +// ..... +// | | +// | +-------------+ +// | | +// v v +// A_Clustered A_Unclustered ====> C_Unclustered +// | +// v +// B_Clustered +// +// where the ===> arrow has a hostmem source and destination and would entail a +// device to host copy if the source and destination were not in the same XLA +// cluster. +absl::Status PartiallyDeclusterGraph(Graph* graph) { + // When deciding whether to decluster a particular node, we base our decision + // on if we've decided that some of its consumers have to be declustered too. + // Iterating the graph in post-order guarantees that consumers have been + // visited before producers. + std::vector post_order; + GetPostOrder(*graph, &post_order, /*stable_comparator=*/NodeComparatorName(), + /*edge_filter=*/NotBackedge); + + absl::flat_hash_set nodes_to_partially_decluster; + TF_RETURN_IF_ERROR( + FindNodesToDecluster(*graph, &nodes_to_partially_decluster, post_order)); + + if (VLOG_IS_ON(3)) { + for (Node* n : post_order) { + if (nodes_to_partially_decluster.count(n)) { + VLOG(3) << n->DebugString(); + } + } + } + + for (Node* n : post_order) { + if (nodes_to_partially_decluster.count(n)) { + TF_RETURN_IF_ERROR(PartiallyDeclusterNode(graph, n)); + } + } + + // Recompute post order since PartiallyDeclusterNode may have deleted nodes. + post_order.clear(); + GetPostOrder(*graph, &post_order, /*stable_comparator=*/NodeComparatorName(), + /*edge_filter=*/NotBackedge); + nodes_to_partially_decluster.clear(); + TF_RETURN_IF_ERROR( + FindNodesToDecluster(*graph, &nodes_to_partially_decluster, post_order)); + CHECK(nodes_to_partially_decluster.empty()); + + return absl::OkStatus(); +} +} // namespace reduce_device_to_host_copies + +namespace reduce_recompilation { +bool IsIntraClusterEdge(const Edge& edge) { + std::optional src_cluster_name = + GetXlaClusterForNode(*edge.src()); + std::optional dst_cluster_name = + GetXlaClusterForNode(*edge.dst()); + return src_cluster_name.has_value() && src_cluster_name == dst_cluster_name; +} + +bool IsMustCompileDevice(const DeviceType& device_type) { + const XlaOpRegistry::DeviceRegistration* registration; + if (XlaOpRegistry::GetCompilationDevice(device_type.type(), ®istration)) { + return registration->autoclustering_policy == + XlaOpRegistry::AutoclusteringPolicy::kAlways; + } + + return false; +} + +absl::Status MustCompileNode(const Node* n, bool* must_compile) { + DeviceType device_type(""); + TF_RETURN_IF_ERROR( + DeviceNameToDeviceType(n->assigned_device_name(), &device_type)); + + if (IsMustCompileDevice(device_type)) { + *must_compile = true; + return absl::OkStatus(); + } + + // We must compile `n` if it does not have a TensorFlow kernel. + *must_compile = !FindKernelDef(device_type, n->def(), nullptr, nullptr).ok(); + return absl::OkStatus(); +} + +// Declusters nodes to reduce the number of times we think we need to recompile +// a TensorFlow graph. +// +// Abstractly, if we have a cluster of this form: +// +// x0 = arg0 +// x1 = arg1 +// ... +// shape = f(x0, x1, ...) +// result = Reshape(input=, new_shape=shape) +// +// then pulling `f` out of the cluster may reduce the number of compilations and +// will never increase the number of compilations. +// +// We may reduce the number of compilations if f is many to one. For instance +// if f(x,y) = x-y then x=3,y=1 and x=4,y=2 will generate two different +// compilations if f is in the cluster but only one compilation if f is outside +// the cluster. +// +// Declustering f will increase the number of compilations only if f is a +// one-to-many "function" i.e. isn't a function at all. RNG is one possible +// example, depending on how we look at it. But we never create clusters where +// such f's would be marked as must-be-constant. +// +// We assume here that the extra repeated (repeated compared to a clustered f +// where it will always be constant folded) host-side computation of f does not +// regress performance in any significant manner. We will have to revisit this +// algorithm with a more complex cost model if this assumption turns out to be +// incorrect. +absl::Status PartiallyDeclusterGraph(Graph* graph, + const FunctionLibraryDefinition* flib_def, + Env* env) { + std::vector compile_time_const_nodes(graph->num_node_ids()); + OptimizerOptions opts; + auto pflr = std::make_unique( + nullptr, env, /*config=*/nullptr, TF_GRAPH_DEF_VERSION, flib_def, opts); + FunctionLibraryRuntime* lib_runtime = + pflr->GetFLR(ProcessFunctionLibraryRuntime::kDefaultFLRDevice); + TF_RETURN_IF_ERROR(BackwardsConstAnalysis(*graph, nullptr, + &compile_time_const_nodes, + lib_runtime, IsIntraClusterEdge)); + + std::vector rpo; + GetReversePostOrder(*graph, &rpo, /*stable_comparator=*/NodeComparatorName(), + /*edge_filter=*/NotBackedge); + for (Node* n : rpo) { + if (!compile_time_const_nodes[n->id()]) { + continue; + } + + absl::string_view cluster_name = *GetXlaClusterForNode(*n); + bool node_on_cluster_edge = + absl::c_all_of(n->in_edges(), [&](const Edge* e) { + std::optional incoming_cluster = + GetXlaClusterForNode(*e->src()); + return !incoming_cluster || *incoming_cluster != cluster_name; + }); + + // We don't want to decluster F in a graph like + // + // Input -> OP -> Shape -> F -> Reshape + // + // Doing so will break up the cluster. Even if we were okay with breaking + // up the cluster we will at least have to relabel the two clusters to have + // different cluster names. + // + // We may want to revisit this in the future: we may have cases where OP is + // a small computation that does not benefit from XLA while XLA can optimize + // everything that follows the Reshape. In these cases it may be wise to + // remove Input, OP, Shape and F from the cluster, if F is a many-to-one + // function. + // + // Note that we do the right thing for graphs like: + // + // Input -> F0 -> F1 -> Reshape + // + // Since we iterate in RPO, we'll first encounter F0, decluster it, then + // encounter F1, decluster it and so on. + if (node_on_cluster_edge) { + bool must_compile_node; + TF_RETURN_IF_ERROR(MustCompileNode(n, &must_compile_node)); + if (!must_compile_node) { + if (n->IsConstant()) { + // We must decluster Const nodes that have an input control edge from + // a different device, because this node may be part of the + // co-ordination of while loops between devices. + for (auto it : n->in_edges()) { + if (!it->src()->assigned_device_name().empty() && + it->src()->assigned_device_name() != + n->assigned_device_name()) { + VLOG(3) << "Declustering Const with cross-device control input " + << n->name(); + RemoveFromXlaCluster(n); + break; + } + } + } else { + VLOG(3) << "Declustering must-be-constant node " << n->name(); + RemoveFromXlaCluster(n); + } + } + } + } + + return absl::OkStatus(); +} +} // namespace reduce_recompilation + +namespace decluster_root_shape_consumers { + +absl::Status PartiallyDeclusterGraph(Graph* graph) { + std::vector reverse_post_order; + GetReversePostOrder(*graph, &reverse_post_order, + /*stable_comparator=*/NodeComparatorName(), + /*edge_filter=*/NotBackedge); + + for (Node* n : reverse_post_order) { + if (!IsShapeConsumerOp(*n)) { + continue; + } + + std::optional cluster = GetXlaClusterForNode(*n); + if (!cluster.has_value()) { + continue; + } + + auto input_belongs_to_same_cluster = [&](const Edge* e) { + return cluster == GetXlaClusterForNode(*e->src()); + }; + + if (absl::c_any_of(n->in_edges(), input_belongs_to_same_cluster)) { + continue; + } + + VLOG(2) << "Declustering " << n->name() + << " because it is a root shape consumer"; + RemoveFromXlaCluster(n); + } + return absl::OkStatus(); +} +} // namespace decluster_root_shape_consumers +} // namespace + +absl::Status PartiallyDeclusterPass::Run( + const GraphOptimizationPassOptions& options) { + // NB! In this pass we assume the only XLA-auto-clusterable operations that + // may have side effects are resource variable operations so we don't cluster + // those. The pass will have to be updated if this assumption becomes + // invalid. + + Graph* graph = options.graph->get(); + + TF_RETURN_IF_ERROR( + reduce_device_to_host_copies::PartiallyDeclusterGraph(graph)); + if (options.flib_def == nullptr) { + return absl::InvalidArgumentError( + "GraphOptimizationPassOptions::flib_def must be set for " + "PartiallyDeclusterPass."); + } + if (options.session_options == nullptr || + options.session_options->env == nullptr) { + return absl::InvalidArgumentError( + "GraphOptimizationPassOptions::session_options::env must be set for " + "PartiallyDeclusterPass."); + } + TF_RETURN_IF_ERROR(reduce_recompilation::PartiallyDeclusterGraph( + graph, options.flib_def, options.session_options->env)); + + TF_RETURN_IF_ERROR( + decluster_root_shape_consumers::PartiallyDeclusterGraph(graph)); + + return absl::OkStatus(); +} +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/partially_decluster_pass.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/partially_decluster_pass.h new file mode 100644 index 0000000000000000000000000000000000000000..7da56cdec863a90b63338c0f213e9278ee9ca262 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/partially_decluster_pass.h @@ -0,0 +1,35 @@ +/* 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_PARTIALLY_DECLUSTER_PASS_H_ +#define TENSORFLOW_COMPILER_JIT_PARTIALLY_DECLUSTER_PASS_H_ + +#include "tensorflow/core/common_runtime/optimization_registry.h" + +namespace tensorflow { + +// Clones or moves nodes from within a cluster to outside the cluster if +// profitable. There are two reasons why we do this: +// +// - Reducing device-to-host copies. +// - Reducing the number of XLA recompilations. +class PartiallyDeclusterPass : public GraphOptimizationPass { + public: + absl::Status Run(const GraphOptimizationPassOptions& options) override; +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_PARTIALLY_DECLUSTER_PASS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/partially_decluster_pass_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/partially_decluster_pass_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..3ac6a4d24df7f069ce4146925f745776d960fa8b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/partially_decluster_pass_test.cc @@ -0,0 +1,561 @@ +/* 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 "tensorflow/compiler/jit/partially_decluster_pass.h" + +#include "absl/memory/memory.h" +#include "tensorflow/cc/framework/ops.h" +#include "tensorflow/cc/ops/array_ops.h" +#include "tensorflow/cc/ops/const_op.h" +#include "tensorflow/cc/ops/control_flow_ops_internal.h" +#include "tensorflow/cc/ops/function_ops.h" +#include "tensorflow/cc/ops/sendrecv_ops.h" +#include "tensorflow/cc/ops/standard_ops.h" +#include "tensorflow/compiler/jit/defs.h" +#include "tensorflow/compiler/jit/test_util.h" +#include "tensorflow/compiler/jit/xla_cluster_util.h" +#include "tensorflow/compiler/tf2xla/cc/ops/xla_ops.h" +#include "tensorflow/compiler/tf2xla/xla_op_kernel.h" +#include "tensorflow/compiler/tf2xla/xla_op_registry.h" +#include "tensorflow/core/common_runtime/graph_constructor.h" +#include "tensorflow/core/common_runtime/graph_def_builder_util.h" +#include "tensorflow/core/framework/function.h" +#include "tensorflow/core/framework/function.pb.h" +#include "tensorflow/core/framework/node_def_util.h" +#include "tensorflow/core/framework/op.h" +#include "tensorflow/core/graph/algorithm.h" +#include "tensorflow/core/graph/graph_def_builder.h" +#include "tensorflow/core/lib/core/status_test_util.h" +#include "tensorflow/core/platform/test.h" + +namespace tensorflow { +namespace { +REGISTER_OP("FakeNullary").Output("out: int32"); + +REGISTER_OP("FakeBinary") + .Input("host_in: int32") + .Input("device_in: int32") + .Output("host_out: int32") + .Output("device_out: int32"); + +REGISTER_OP("FakeResourceVar").Output("out: resource"); + +REGISTER_OP("FakeResourceUpdate") + .Input("in: resource") + .Output("out: resource") + .Output("something_else: int32"); + +class FakeBinaryOp : public OpKernel { + public: + explicit FakeBinaryOp(OpKernelConstruction* context) : OpKernel(context) {} + + void Compute(OpKernelContext* ctx) override { CHECK(false); } +}; + +class FakeResourceUpdateOp : public OpKernel { + public: + explicit FakeResourceUpdateOp(OpKernelConstruction* context) + : OpKernel(context) {} + + void Compute(OpKernelContext* ctx) override { CHECK(false); } +}; + +REGISTER_KERNEL_BUILDER(Name("FakeBinary") + .Device(DEVICE_CPU) + .HostMemory("host_in") + .HostMemory("host_out"), + FakeBinaryOp); + +REGISTER_KERNEL_BUILDER( + Name("FakeResourceUpdate").Device(DEVICE_CPU).HostMemory("something_else"), + FakeResourceUpdateOp); + +absl::Status PartiallyDecluster(std::unique_ptr* graph) { + FixupSourceAndSinkEdges(graph->get()); + // Assign all nodes to the CPU device. + static const char* kCpuDevice = "/job:localhost/replica:0/task:0/cpu:0"; + for (Node* n : (*graph)->nodes()) { + if (n->assigned_device_name().empty()) { + n->set_assigned_device_name(kCpuDevice); + } + } + + GraphOptimizationPassWrapper wrapper; + GraphOptimizationPassOptions opt_options = + wrapper.CreateGraphOptimizationPassOptions(graph); + + PartiallyDeclusterPass pass; + return pass.Run(opt_options); +} + +Node* FindNodeByName(const Graph& graph, const std::string& name) { + for (Node* node : graph.nodes()) { + if (node->name() == name) { + return node; + } + } + return nullptr; +} + +bool GetInputsForNode(const Graph& graph, const std::string& node_name, + std::vector* inputs) { + const Node* node = FindNodeByName(graph, node_name); + if (node == nullptr) { + return false; + } + for (const Edge* e : node->in_edges()) { + inputs->push_back(e->src()); + } + std::sort(inputs->begin(), inputs->end(), NodeComparatorName()); + return true; +} + +TEST(PartiallyDeclusterPassTest, ClusteredAndUnclustered) { + std::unique_ptr graph(new Graph(OpRegistry::Global())); + { + GraphDefBuilder builder(GraphDefBuilder::kFailImmediately); + Node* input = + ops::SourceOp("FakeNullary", builder.opts().WithName("Input")); + Node* clustered_producer = + ops::BinaryOp("FakeBinary", input, input, + builder.opts().WithName("ClusteredProducer")); + ops::BinaryOp("FakeBinary", clustered_producer, input, + builder.opts().WithName("UnclusteredConsumer")); + Node* clustered_consumer = + ops::BinaryOp("FakeBinary", {clustered_producer, 1}, input, + builder.opts().WithName("ClusteredConsumer")); + clustered_producer->AddAttr(kXlaClusterAttr, "cluster_0"); + clustered_consumer->AddAttr(kXlaClusterAttr, "cluster_0"); + TF_EXPECT_OK(GraphDefBuilderToGraph(builder, graph.get())); + } + + TF_ASSERT_OK(PartiallyDecluster(&graph)); + std::vector unclustered_consumer_inputs; + ASSERT_TRUE(GetInputsForNode(*graph, "UnclusteredConsumer", + &unclustered_consumer_inputs)); + ASSERT_EQ(unclustered_consumer_inputs.size(), 2); + EXPECT_EQ(unclustered_consumer_inputs[0]->name(), + "ClusteredProducer/declustered"); + EXPECT_EQ(unclustered_consumer_inputs[1]->name(), "Input"); + + std::vector clustered_consumer_inputs; + ASSERT_TRUE(GetInputsForNode(*graph, "ClusteredConsumer", + &clustered_consumer_inputs)); + ASSERT_EQ(clustered_consumer_inputs.size(), 2); + EXPECT_EQ(clustered_consumer_inputs[0]->name(), "ClusteredProducer"); + EXPECT_EQ(clustered_consumer_inputs[1]->name(), "Input"); +} + +TEST(PartiallyDeclusterPassTest, DifferentClusters) { + std::unique_ptr graph(new Graph(OpRegistry::Global())); + { + GraphDefBuilder builder(GraphDefBuilder::kFailImmediately); + Node* input = + ops::SourceOp("FakeNullary", builder.opts().WithName("Input")); + Node* clustered_producer = + ops::BinaryOp("FakeBinary", input, input, + builder.opts().WithName("ClusteredProducer")); + Node* consumer_in_different_cluster = + ops::BinaryOp("FakeBinary", clustered_producer, input, + builder.opts().WithName("ConsumerInDifferentCluster")); + Node* clustered_consumer = + ops::BinaryOp("FakeBinary", input, {clustered_producer, 1}, + builder.opts().WithName("ClusteredConsumer")); + clustered_producer->AddAttr(kXlaClusterAttr, "cluster_0"); + clustered_consumer->AddAttr(kXlaClusterAttr, "cluster_0"); + consumer_in_different_cluster->AddAttr(kXlaClusterAttr, "cluster_1"); + TF_EXPECT_OK(GraphDefBuilderToGraph(builder, graph.get())); + } + + TF_ASSERT_OK(PartiallyDecluster(&graph)); + std::vector inputs; + ASSERT_TRUE(GetInputsForNode(*graph, "ConsumerInDifferentCluster", &inputs)); + ASSERT_EQ(inputs.size(), 2); + EXPECT_EQ(inputs[0]->name(), "ClusteredProducer/declustered"); + EXPECT_EQ(inputs[1]->name(), "Input"); +} + +TEST(PartiallyDeclusterPassTest, DontDeclusterIfUserIsDeviceMem) { + std::unique_ptr graph(new Graph(OpRegistry::Global())); + { + GraphDefBuilder builder(GraphDefBuilder::kFailImmediately); + Node* input = + ops::SourceOp("FakeNullary", builder.opts().WithName("Input")); + Node* clustered_producer = + ops::BinaryOp("FakeBinary", input, input, + builder.opts().WithName("ClusteredProducer")); + // The first input is hostmem and the second input is devicemem. + Node* consumer_in_different_cluster = + ops::BinaryOp("FakeBinary", input, clustered_producer, + builder.opts().WithName("ConsumerInDifferentCluster")); + Node* clustered_consumer = + ops::BinaryOp("FakeBinary", input, {clustered_producer, 1}, + builder.opts().WithName("ClusteredConsumer")); + clustered_producer->AddAttr(kXlaClusterAttr, "cluster_0"); + clustered_consumer->AddAttr(kXlaClusterAttr, "cluster_0"); + consumer_in_different_cluster->AddAttr(kXlaClusterAttr, "cluster_1"); + TF_EXPECT_OK(GraphDefBuilderToGraph(builder, graph.get())); + } + + TF_ASSERT_OK(PartiallyDecluster(&graph)); + std::vector inputs; + ASSERT_TRUE(GetInputsForNode(*graph, "ConsumerInDifferentCluster", &inputs)); + ASSERT_EQ(inputs.size(), 2); + EXPECT_EQ(inputs[0]->name(), "ClusteredProducer"); + EXPECT_EQ(inputs[1]->name(), "Input"); +} + +TEST(PartiallyDeclusterPassTest, DontDuplicateResourceVarOps) { + std::unique_ptr graph(new Graph(OpRegistry::Global())); + { + GraphDefBuilder builder(GraphDefBuilder::kFailImmediately); + Node* input = + ops::SourceOp("FakeNullary", builder.opts().WithName("Input")); + Node* resource_var = ops::SourceOp("FakeResourceVar", + builder.opts().WithName("ResourceVar")); + Node* clustered_producer = + ops::UnaryOp("FakeResourceUpdate", resource_var, + builder.opts().WithName("ClusteredProducer")); + Node* consumer_in_different_cluster = + ops::BinaryOp("FakeBinary", {clustered_producer, 1}, input, + builder.opts().WithName("ConsumerInDifferentCluster")); + Node* clustered_consumer = + ops::BinaryOp("FakeBinary", input, {clustered_producer, 1}, + builder.opts().WithName("ClusteredConsumer")); + clustered_producer->AddAttr(kXlaClusterAttr, "cluster_0"); + clustered_consumer->AddAttr(kXlaClusterAttr, "cluster_0"); + consumer_in_different_cluster->AddAttr(kXlaClusterAttr, "cluster_1"); + TF_EXPECT_OK(GraphDefBuilderToGraph(builder, graph.get())); + } + + TF_ASSERT_OK(PartiallyDecluster(&graph)); + std::vector inputs; + ASSERT_TRUE(GetInputsForNode(*graph, "ConsumerInDifferentCluster", &inputs)); + ASSERT_EQ(inputs.size(), 2); + EXPECT_EQ(inputs[0]->name(), "ClusteredProducer"); + EXPECT_EQ(inputs[1]->name(), "Input"); +} + +TEST(PartiallyDeclusterPassTest, DeclusterDependentNodes) { + std::unique_ptr graph(new Graph(OpRegistry::Global())); + { + GraphDefBuilder builder(GraphDefBuilder::kFailImmediately); + Node* input = + ops::SourceOp("FakeNullary", builder.opts().WithName("Input")); + Node* clustered_producer_0 = + ops::BinaryOp("FakeBinary", input, input, + builder.opts().WithName("ClusteredProducer0")); + Node* clustered_producer_1 = + ops::BinaryOp("FakeBinary", clustered_producer_0, input, + builder.opts().WithName("ClusteredProducer1")); + ops::BinaryOp("FakeBinary", clustered_producer_1, input, + builder.opts().WithName("UnclusteredConsumer")); + Node* clustered_consumer = + ops::BinaryOp("FakeBinary", {clustered_producer_1, 1}, input, + builder.opts().WithName("ClusteredConsumer")); + clustered_producer_0->AddAttr(kXlaClusterAttr, "cluster_0"); + clustered_producer_1->AddAttr(kXlaClusterAttr, "cluster_0"); + clustered_consumer->AddAttr(kXlaClusterAttr, "cluster_0"); + TF_EXPECT_OK(GraphDefBuilderToGraph(builder, graph.get())); + } + + TF_ASSERT_OK(PartiallyDecluster(&graph)); + std::vector unclustered_consumer_inputs, declustered_producer_1_inputs; + + ASSERT_TRUE(GetInputsForNode(*graph, "UnclusteredConsumer", + &unclustered_consumer_inputs)); + ASSERT_EQ(unclustered_consumer_inputs.size(), 2); + EXPECT_EQ(unclustered_consumer_inputs[0]->name(), + "ClusteredProducer1/declustered"); + EXPECT_EQ(unclustered_consumer_inputs[1]->name(), "Input"); + + ASSERT_TRUE(GetInputsForNode(*graph, "ClusteredProducer1/declustered", + &declustered_producer_1_inputs)); + ASSERT_EQ(declustered_producer_1_inputs.size(), 2); + EXPECT_EQ(declustered_producer_1_inputs[0]->name(), + "ClusteredProducer0/declustered"); + EXPECT_EQ(declustered_producer_1_inputs[1]->name(), "Input"); +} + +void AddToCluster(absl::Span nodes, + absl::string_view cluster_name) { + for (Node* n : nodes) { + n->AddAttr(kXlaClusterAttr, std::string(cluster_name)); + } +} + +TEST(PartiallyDeclusterPassTest, DeclusterMustBeConstantNodes) { + tensorflow::Scope s = tensorflow::Scope::NewRootScope(); + Output shape_a = ops::Placeholder(s.WithOpName("shape_a"), DT_INT32, + ops::Placeholder::Attrs{}); + Output shape_b = ops::Placeholder(s.WithOpName("shape_b"), DT_INT32, + ops::Placeholder::Attrs{}); + Output shape = ops::Add(s.WithOpName("shape"), shape_a, shape_b); + + Output reshape_input = ops::Placeholder(s.WithOpName("reshape_input"), + DT_FLOAT, ops::Placeholder::Attrs{}); + Output reshape = ops::Reshape(s.WithOpName("reshape"), reshape_input, shape); + + AddToCluster({shape.node(), reshape.node()}, "cluster_0"); + + auto graph = std::make_unique(OpRegistry::Global()); + TF_ASSERT_OK(s.ToGraph(graph.get())); + TF_ASSERT_OK(PartiallyDecluster(&graph)); + + const Node* n = FindNodeByName(*graph, "shape"); + ASSERT_NE(n, nullptr); + + EXPECT_EQ(GetXlaClusterForNode(*n), std::nullopt); +} + +TEST(PartiallyDeclusterPassTest, DeclusteringStopsAtMetadataOps) { + tensorflow::Scope s = tensorflow::Scope::NewRootScope(); + Output input_a = ops::Placeholder(s.WithOpName("input_a"), DT_INT32, + ops::Placeholder::Attrs{}); + Output input_b = ops::Placeholder(s.WithOpName("shape_b"), DT_FLOAT, + ops::Placeholder::Attrs{}); + Output mul = ops::Mul(s.WithOpName("mul"), input_b, input_b); + Output shape_of_mul = ops::Shape(s.WithOpName("shape_of_mul"), mul); + + Output shape = ops::Add(s.WithOpName("shape"), shape_of_mul, input_a); + + Output reshape_input = ops::Placeholder(s.WithOpName("reshape_input"), + DT_FLOAT, ops::Placeholder::Attrs{}); + Output reshape = ops::Reshape(s.WithOpName("reshape"), reshape_input, shape); + + AddToCluster({mul.node(), shape_of_mul.node(), shape.node(), reshape.node()}, + "cluster_0"); + + auto graph = std::make_unique(OpRegistry::Global()); + TF_ASSERT_OK(s.ToGraph(graph.get())); + TF_ASSERT_OK(PartiallyDecluster(&graph)); + + const Node* n = FindNodeByName(*graph, "shape"); + ASSERT_NE(n, nullptr); + + EXPECT_EQ(GetXlaClusterForNode(*n), "cluster_0"); +} + +TEST(PartiallyDeclusterPassTest, EdgeAcrossDifferentClusters) { + tensorflow::Scope s = tensorflow::Scope::NewRootScope(); + Output shape_a = ops::Placeholder(s.WithOpName("shape_a"), DT_INT32, + ops::Placeholder::Attrs{}); + Output shape_b = ops::Placeholder(s.WithOpName("shape_b"), DT_INT32, + ops::Placeholder::Attrs{}); + Output shape = ops::Add(s.WithOpName("shape"), shape_a, shape_b); + + Output reshape_input = ops::Placeholder(s.WithOpName("reshape_input"), + DT_FLOAT, ops::Placeholder::Attrs{}); + Output reshape = ops::Reshape(s.WithOpName("reshape"), reshape_input, shape); + + AddToCluster({reshape.node()}, "cluster_0"); + AddToCluster({shape.node()}, "cluster_1"); + + auto graph = std::make_unique(OpRegistry::Global()); + TF_ASSERT_OK(s.ToGraph(graph.get())); + TF_ASSERT_OK(PartiallyDecluster(&graph)); + + const Node* n = FindNodeByName(*graph, "shape"); + ASSERT_NE(n, nullptr); + + EXPECT_EQ(GetXlaClusterForNode(*n), "cluster_1"); +} + +TEST(PartiallyDeclusterPassTest, DontDeclusterXlaDeviceOps) { + tensorflow::Scope s = tensorflow::Scope::NewRootScope(); + Output shape_a = ops::Placeholder(s.WithOpName("shape_a"), DT_INT32, + ops::Placeholder::Attrs{}); + Output shape_b = ops::Placeholder(s.WithOpName("shape_b"), DT_INT32, + ops::Placeholder::Attrs{}); + Output shape = ops::Add(s.WithOpName("shape"), shape_a, shape_b); + + Output reshape_input = ops::Placeholder(s.WithOpName("reshape_input"), + DT_FLOAT, ops::Placeholder::Attrs{}); + Output reshape = ops::Reshape(s.WithOpName("reshape"), reshape_input, shape); + + AddToCluster({shape.node(), reshape.node()}, "cluster_0"); + + auto graph = std::make_unique(OpRegistry::Global()); + TF_ASSERT_OK(s.ToGraph(graph.get())); + + // This is needed to register the XLA_GPU device. + std::vector> devices; + TF_ASSERT_OK(DeviceFactory::AddDevices( + SessionOptions(), "/job:localhost/replica:0/task:0", &devices)); + + // Scope::ToGraph loses the assigned device name since it goes through + // GraphDef/NodeDef which does not have a field for the assigned device name. + Node* n = FindNodeByName(*graph, "shape"); + ASSERT_NE(n, nullptr); + n->set_assigned_device_name( + "/job:localhost/replica:0/task:0/device:XLA_GPU:0"); + + TF_ASSERT_OK(PartiallyDecluster(&graph)); + + EXPECT_EQ(GetXlaClusterForNode(*n), "cluster_0"); +} + +TEST(PartiallyDeclusterPassTest, EliminatedUnusedNodes) { + const char* const kClusteredProducer0Name = "ClusteredProducer0"; + const char* const kClusteredProducer1Name = "ClusteredProducer1"; + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + { + GraphDefBuilder builder(GraphDefBuilder::kFailImmediately); + Node* input = + ops::SourceOp("FakeNullary", builder.opts().WithName("Input")); + Node* clustered_producer_0 = + ops::BinaryOp("FakeBinary", input, input, + builder.opts().WithName(kClusteredProducer0Name)); + Node* clustered_producer_1 = + ops::BinaryOp("FakeBinary", clustered_producer_0, input, + builder.opts().WithName(kClusteredProducer1Name)); + ops::BinaryOp("FakeBinary", clustered_producer_1, input, + builder.opts().WithName("UnclusteredConsumer")); + clustered_producer_0->AddAttr(kXlaClusterAttr, "cluster_0"); + clustered_producer_1->AddAttr(kXlaClusterAttr, "cluster_0"); + TF_EXPECT_OK(GraphDefBuilderToGraph(builder, graph.get())); + } + + TF_ASSERT_OK(PartiallyDecluster(&graph)); + EXPECT_EQ(FindNodeByName(*graph, kClusteredProducer0Name), nullptr); + EXPECT_EQ(FindNodeByName(*graph, kClusteredProducer1Name), nullptr); +} + +TEST(PartiallyDeclusterPassTest, MetadataOpsDontStartClusters) { + tensorflow::Scope root = tensorflow::Scope::NewRootScope(); + tensorflow::Scope in_cluster_and = root.WithXlaCluster("cluster_0"); + + Output a = ops::Placeholder(root.WithOpName("a"), DT_FLOAT); + Output b = ops::Shape(in_cluster_and.WithOpName("b"), a); + Output c = ops::Rank(in_cluster_and.WithOpName("c"), b); + Output d = ops::Size(in_cluster_and.WithOpName("d"), c); + (void)ops::Shape(in_cluster_and.WithOpName("e"), d); + + auto graph = std::make_unique(OpRegistry::Global()); + TF_ASSERT_OK(root.ToGraph(graph.get())); + + TF_ASSERT_OK(PartiallyDecluster(&graph)); + + Node* n_b = FindNodeByName(*graph, "b"); + ASSERT_NE(n_b, nullptr); + EXPECT_EQ(GetXlaClusterForNode(*n_b), std::nullopt); + + Node* n_c = FindNodeByName(*graph, "c"); + ASSERT_NE(n_c, nullptr); + EXPECT_EQ(GetXlaClusterForNode(*n_c), std::nullopt); + + Node* n_d = FindNodeByName(*graph, "d"); + ASSERT_NE(n_d, nullptr); + EXPECT_EQ(GetXlaClusterForNode(*n_d), std::nullopt); + + Node* n_e = FindNodeByName(*graph, "e"); + ASSERT_NE(n_e, nullptr); + EXPECT_EQ(GetXlaClusterForNode(*n_e), std::nullopt); +} + +TEST(PartiallyDeclusterPassTest, MetaConsumersArentDeclustered) { + tensorflow::Scope root = tensorflow::Scope::NewRootScope(); + tensorflow::Scope in_cluster_and = root.WithXlaCluster("cluster_0"); + auto graph = std::make_unique(OpRegistry::Global()); + Output a = ops::Placeholder(root.WithOpName("a"), DT_FLOAT); + Output b = ops::Add(in_cluster_and.WithOpName("b"), a, a); + Output c = ops::Rank(in_cluster_and.WithOpName("c"), b); + + Output e; + TF_ASSERT_OK( + CreateOutputWithScope("FakeBinary", {c, c}, root.WithOpName("e"), &e)); + + TF_ASSERT_OK(root.ToGraph(graph.get())); + TF_ASSERT_OK(PartiallyDecluster(&graph)); + + Node* n_b = FindNodeByName(*graph, "b"); + ASSERT_NE(n_b, nullptr); + EXPECT_EQ(GetXlaClusterForNode(*n_b), "cluster_0"); + + Node* n_c = FindNodeByName(*graph, "c"); + ASSERT_NE(n_c, nullptr); + EXPECT_EQ(GetXlaClusterForNode(*n_c), "cluster_0"); +} + +TEST(PartiallyDeclusterPassTest, ConstInputsToSliceArentDeclustered) { + tensorflow::Scope root = tensorflow::Scope::NewRootScope(); + tensorflow::Scope in_cluster_and = root.WithXlaCluster("cluster_0"); + auto graph = std::make_unique(OpRegistry::Global()); + Output a = ops::Placeholder(root.WithOpName("a"), DT_FLOAT, + ops::Placeholder::Attrs{{4}}); + Output b = ops::Const(in_cluster_and.WithOpName("b"), {1}); + Output c = ops::Const(in_cluster_and.WithOpName("c"), {2}); + Output d = ops::Slice(in_cluster_and.WithOpName("d"), a, b, c); + + TF_ASSERT_OK(root.ToGraph(graph.get())); + TF_ASSERT_OK(PartiallyDecluster(&graph)); + + Node* n_b = FindNodeByName(*graph, "b"); + ASSERT_NE(n_b, nullptr); + EXPECT_EQ(GetXlaClusterForNode(*n_b), "cluster_0"); + + Node* n_c = FindNodeByName(*graph, "c"); + ASSERT_NE(n_c, nullptr); + EXPECT_EQ(GetXlaClusterForNode(*n_c), "cluster_0"); +} + +TEST(PartiallyDeclusterPassTest, + ConstInLoopWithCrossDeviceControlInputsAreDeclustered) { + // Based on DontClusterTheSpecialIdentityDrivingConstsInLoop in + // mark_for_compilation_pass_test.cc + tensorflow::Scope root = tensorflow::Scope::NewRootScope(); + tensorflow::Scope in_cluster_and = root.WithXlaCluster("cluster_0"); + auto graph = std::make_unique(OpRegistry::Global()); + Output a = ops::Placeholder(root.WithOpName("a"), DT_FLOAT, + ops::Placeholder::Attrs{{4}}); + Output b = ops::Const(in_cluster_and.WithOpName("b"), {1}); + Output c = ops::Const(in_cluster_and.WithOpName("c"), {2}); + Output slice = ops::Slice(in_cluster_and.WithOpName("slice"), a, b, c); + Output cond = ops::Placeholder(root.WithOpName("cond"), DT_BOOL); + Output value = ops::Placeholder(root.WithOpName("value"), DT_FLOAT); + Output loop_cond = ops::LoopCond(root.WithOpName("loop_cond"), cond); + ops::Switch switch_node(root.WithOpName("switch"), value, loop_cond); + Output identity = + ops::Identity(root.WithOpName("identity"), switch_node.output_true); + root.graph()->AddControlEdge(identity.node(), b.node()); + + TF_ASSERT_OK(root.ToGraph(graph.get())); + + // This is needed to register the XLA_GPU device. + std::vector> devices; + TF_ASSERT_OK(DeviceFactory::AddDevices( + SessionOptions(), "/job:localhost/replica:0/task:0", &devices)); + + // Scope::ToGraph loses the assigned device name since it goes through + // GraphDef/NodeDef which does not have a field for the assigned device name. + Node* identity_node = FindNodeByName(*graph, "identity"); + ASSERT_NE(identity_node, nullptr); + identity_node->set_assigned_device_name( + "/job:localhost/replica:0/task:0/device:XLA_GPU:0"); + + TF_ASSERT_OK(PartiallyDecluster(&graph)); + + Node* n_b = FindNodeByName(*graph, "b"); + ASSERT_NE(n_b, nullptr); + EXPECT_EQ(GetXlaClusterForNode(*n_b), std::nullopt); + + Node* n_c = FindNodeByName(*graph, "c"); + ASSERT_NE(n_c, nullptr); + EXPECT_EQ(GetXlaClusterForNode(*n_c), "cluster_0"); +} + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_base_device.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_base_device.cc new file mode 100644 index 0000000000000000000000000000000000000000..e1e3bdee26cbf1fc24e3fa1060836a474031beb4 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_base_device.cc @@ -0,0 +1,60 @@ +/* Copyright 2023 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/compiler/jit/pjrt_base_device.h" + +namespace tensorflow { +namespace { + +DeviceAttributes BuildPjRtBaseDeviceAttributes(const std::string& name_prefix, + const std::string& device_name, + int device_ordinal) { + return Device::BuildDeviceAttributes( + absl::StrCat(name_prefix, "/device:", device_name, ":", device_ordinal), + DeviceType(device_name), Bytes(16ULL << 30), DeviceLocality(), + absl::StrCat("device: ", device_name, " device")); +} + +} // namespace + +PjRtBaseDevice::PjRtBaseDevice(const SessionOptions& session_options, + const Options& options) + : LocalDevice(session_options, + BuildPjRtBaseDeviceAttributes(options.device_name_prefix, + options.device_name, + options.device_ordinal)), + metadata_(DeviceType(options.compilation_device_name), + options.shape_determination_fns) { + if (options.shape_determination_fns.empty()) { + LOG(ERROR) << "shape_representation_fns must be non-empty."; + } + VLOG(1) << "Created PJRT base device " << options.compilation_device_name + << " device_name: " << name(); +} + +/*static*/ absl::StatusOr +PjRtBaseDevice::GetMetadataFromDevice(DeviceBase* device) { + PjRtBaseDevice* pjrt_device = + dynamic_cast(device->UnderlyingDevice()); + if (pjrt_device == nullptr) { + return absl::InternalError(absl::StrCat( + "Cannot get device metadata from non-PJRT device \"", device->name(), + "\". GetMetadata must only be called on a device derived from " + "PjRtBaseDevice. Either an internal bug has been triggered, or an " + "XLA-specific op has been placed on the wrong device.")); + } + return &pjrt_device->metadata_; +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_base_device.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_base_device.h new file mode 100644 index 0000000000000000000000000000000000000000..30b506a445d4573745c49423dcf05316e04e14df --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_base_device.h @@ -0,0 +1,112 @@ +/* Copyright 2023 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_PJRT_BASE_DEVICE_H_ +#define TENSORFLOW_COMPILER_JIT_PJRT_BASE_DEVICE_H_ + +#include +#include +#include + +#include "tensorflow/compiler/tf2xla/layout_util.h" +#include "tensorflow/core/common_runtime/local_device.h" +#include "tensorflow/core/framework/device_base.h" + +namespace tensorflow { + +// tensorflow::PjRtBaseDevice replaces the deprecated tensorflow::XlaDevice. +// This accelerator agnostic device is mainly used to store metadata. +class PjRtBaseDevice : public LocalDevice { + public: + // Stores metadata about the PjRtBaseDevice. + class Metadata { + public: + Metadata(const DeviceType& jit_device_type, + std::vector + shape_determination_fns) + : jit_device_type_(jit_device_type), + shape_determination_fns_(std::move(shape_determination_fns)) {} + + // The index of the device on this host. + int device_ordinal() const; + + const DeviceType& jit_device_type() const { return jit_device_type_; } + const XlaShapeLayoutHelpers::ShapeDeterminationFns& + default_shape_determination_fns() const { + return shape_determination_fns_.at(0); + } + + const XlaShapeLayoutHelpers::ShapeDeterminationFns& + shape_determination_fns_at(int i) const { + return shape_determination_fns_[i]; + } + + private: + const DeviceType jit_device_type_; + std::vector + shape_determination_fns_; + + Metadata(const Metadata&) = delete; + void operator=(const Metadata&) = delete; + }; + + struct Options { + // The device name's prefix (e.g., "/task:7") + std::string device_name_prefix; + + // The name of the device (e.g., "TPU") + std::string device_name; + + // The index of the device. + int device_ordinal = -1; + + // The name of the compilation device, also referred to as jit_device_type. + // (e.g., "XLA_CPU_JIT"); + std::string compilation_device_name; + + // A vector of ShapeDeterminationFn (i.e., a bundle of LayoutSelectionFn, + // ShapeRepresentationFn). Each bundle describes how the on-host shapes of + // a) argument and return value, for entry computations b) variables, for + // all computations, should be represented in XLA. Parameters/return values + // will be shaped according to the function pair, and reshaped back to/from + // their declared shapes for computations. Must be non-empty. + std::vector + shape_determination_fns; + + Options(std::string device_name_prefix, std::string device_name, + int device_ordinal, std::string compilation_device_name, + std::vector + shape_determination_fns) + : device_name_prefix(device_name_prefix), + device_name(device_name), + device_ordinal(device_ordinal), + compilation_device_name(compilation_device_name), + shape_determination_fns(shape_determination_fns) {} + }; + + // Creates a new PJRT base device. + PjRtBaseDevice(const SessionOptions& session_options, const Options& options); + + static absl::StatusOr GetMetadataFromDevice( + DeviceBase* device); + + private: + // The metadata of this PjRtBaseDevice. + const Metadata metadata_; +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_PJRT_BASE_DEVICE_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_compile_util.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_compile_util.cc new file mode 100644 index 0000000000000000000000000000000000000000..937c89649381fe19e280a39bf66677ff23d70e35 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_compile_util.cc @@ -0,0 +1,89 @@ +/* Copyright 2023 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/compiler/jit/pjrt_compile_util.h" + +#include + +#include "tensorflow/compiler/jit/device_compilation_profiler.h" +#include "tensorflow/compiler/jit/device_compiler.h" +#include "tensorflow/compiler/jit/xla_compile_util.h" +#include "tensorflow/compiler/jit/xla_compiler_options_util.h" +#include "tensorflow/compiler/jit/xla_platform_info.h" +#include "tensorflow/compiler/tf2xla/xla_compiler.h" +#include "xla/pjrt/pjrt_client.h" +#include "tensorflow/core/framework/device_base.h" +#include "tensorflow/core/framework/function.h" +#include "tensorflow/core/framework/op_kernel.h" +#include "tensorflow/core/framework/resource_mgr.h" +#include "tensorflow/core/lib/core/refcount.h" +#include "tensorflow/core/platform/status.h" +#include "tsl/platform/errors.h" +#include "tsl/platform/statusor.h" + +namespace tensorflow { + +using PjRtDeviceCompiler = + DeviceCompiler; + +absl::Status CompileToPjRtLoadedExecutable( + const DeviceBase* device, const XlaPlatformInfo& platform_info, + const NameAttrList& function, + const std::vector& args, + DeviceCompileMode compile_mode, bool has_ref_vars, + bool may_alias_resource_update, FunctionLibraryRuntime* flr, + ResourceMgr* rm, const XlaCompiler::CompilationResult** compilation_result, + xla::PjRtClient** client, xla::PjRtLoadedExecutable** executable) { + PjRtDeviceCompiler* pjrt_device_compiler; + DeviceCompilationProfiler* profiler; + TF_RETURN_IF_ERROR(GetOrCreatePjRtDeviceCompilerAndProfiler( + platform_info, rm, flr, &pjrt_device_compiler, &profiler)); + + // Hold the reference to the PJRT device compiler and profiler during + // evaluation. (We could probably free them sooner because the ResourceMgr + // will retain references, but this is more obviously correct.) + core::ScopedUnref pjrt_device_compiler_ref(pjrt_device_compiler); + core::ScopedUnref profiler_ref(profiler); + + *client = pjrt_device_compiler->client(); + + XlaCompiler::Options options = GenerateCompilerOptionsForPjRt( + *flr, device, platform_info, pjrt_device_compiler); + + XlaCompiler::CompileOptions compile_options = + GenerateCompileOptions(has_ref_vars, may_alias_resource_update); + + return pjrt_device_compiler->CompileIfNeeded( + options, function, args, compile_options, compile_mode, profiler, + compilation_result, executable); +} + +absl::Status CompileToPjRtLoadedExecutable( + const OpKernelContext& ctx, const XlaPlatformInfo& platform_info, + const NameAttrList& function, + const std::vector& args, + DeviceCompileMode compile_mode, bool has_ref_vars, + bool may_alias_resource_update, + const XlaCompiler::CompilationResult** compilation_result, + xla::PjRtClient** client, xla::PjRtLoadedExecutable** executable) { + TF_ASSIGN_OR_RETURN(ResourceMgr * rm, GetResourceMgrForDeviceCompiler( + ctx, platform_info.device_type())); + return CompileToPjRtLoadedExecutable( + ctx.device(), platform_info, function, args, compile_mode, has_ref_vars, + may_alias_resource_update, ctx.function_library(), rm, compilation_result, + client, executable); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_compile_util.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_compile_util.h new file mode 100644 index 0000000000000000000000000000000000000000..8b63d3ab4c41100f67fae2da5015002a7e2185cd --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_compile_util.h @@ -0,0 +1,60 @@ +/* Copyright 2023 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_JIT_PJRT_COMPILE_UTIL_H_ +#define TENSORFLOW_COMPILER_JIT_PJRT_COMPILE_UTIL_H_ + +#include "tensorflow/compiler/jit/xla_compile_util.h" +#include "tensorflow/compiler/jit/xla_platform_info.h" +#include "tensorflow/compiler/tf2xla/xla_compiler.h" +#include "xla/pjrt/pjrt_client.h" +#include "tensorflow/core/framework/device_base.h" +#include "tensorflow/core/framework/op_kernel.h" +#include "tensorflow/core/framework/resource_mgr.h" +#include "tensorflow/core/platform/status.h" + +namespace tensorflow { + +// Compiles a `function` to PjRtLoadedExecutable `executable` with `ctx`. +// The compilation result is output in `compilation_result`. The PJRT client +// used for compilation is output in `client`. The PJRT executable is output in +// `executable`. +absl::Status CompileToPjRtLoadedExecutable( + const OpKernelContext& ctx, const XlaPlatformInfo& platform_info, + const NameAttrList& function, + const std::vector& args, + DeviceCompileMode compile_mode, bool has_ref_vars, + bool may_alias_resource_update, + const XlaCompiler::CompilationResult** compilation_result, + xla::PjRtClient** client, xla::PjRtLoadedExecutable** executable); + +// Similar to the above function but it does not take a OpKernelContext. +// Instead, it takes the following arguments that are obtained from +// OpKernelContext in the above function. +// - `device`: the device used to compile the function. +// - `rm`: the resource manager for DeviceCompiler to store JIT-compiled XLA +// computation. +// - `flr`: the FunctionLibraryRuntime for the `function`. +absl::Status CompileToPjRtLoadedExecutable( + const DeviceBase* device, const XlaPlatformInfo& platform_info, + const NameAttrList& function, + const std::vector& args, + DeviceCompileMode compile_mode, bool has_ref_vars, + bool may_alias_resource_update, FunctionLibraryRuntime* flr, + ResourceMgr* rm, const XlaCompiler::CompilationResult** compilation_result, + xla::PjRtClient** client, xla::PjRtLoadedExecutable** executable); + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_PJRT_COMPILE_UTIL_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_compile_util_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_compile_util_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..64171cb9c24e496fbc2c591d45c8afc81ea9d2f7 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_compile_util_test.cc @@ -0,0 +1,129 @@ +/* Copyright 2022 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. +==============================================================================*/ +#if GOOGLE_CUDA || TENSORFLOW_USE_ROCM +#include "tensorflow/compiler/jit/pjrt_compile_util.h" + +#include +#include +#include "tensorflow/cc/framework/scope.h" +#include "tensorflow/cc/ops/function_ops.h" +#include "tensorflow/cc/ops/math_ops.h" +#include "tensorflow/compiler/jit/test_util.h" +#include "tensorflow/core/framework/device_base.h" +#include "tensorflow/core/framework/function.h" +#include "tensorflow/core/framework/graph_to_functiondef.h" +#include "tensorflow/core/framework/node_def_builder.h" +#include "tensorflow/core/framework/types.h" +#include "tensorflow/core/lib/core/status_test_util.h" +#include "tensorflow/core/platform/errors.h" +#include "tensorflow/core/platform/status.h" +#include "tensorflow/core/platform/statusor.h" + +namespace tensorflow { +namespace { + +absl::StatusOr> SampleGraphAddXY() { + std::unique_ptr graph(new Graph(OpRegistry::Global())); + Scope scope = Scope::NewRootScope().ExitOnError(); + auto a = ops::_Arg(scope.WithOpName("A"), DT_INT32, 0); + auto b = ops::_Arg(scope.WithOpName("B"), DT_INT32, 1); + auto c = ops::Add(scope.WithOpName("C"), a, b); + auto d = ops::_Retval(scope.WithOpName("D"), c, 0); + TF_RETURN_IF_ERROR(scope.ToGraph(graph.get())); + return graph; +} + +absl::StatusOr SampleFuntionAddXY(const std::string& name) { + TF_ASSIGN_OR_RETURN(auto graph, SampleGraphAddXY()); + FunctionDef fdef; + TF_RETURN_IF_ERROR(GraphToFunctionDef(*graph, name, &fdef)); + return fdef; +} + +std::vector SampleArgsForAddXY() { + std::vector args(2); + args[0].kind = XlaCompiler::Argument::kParameter; + args[0].type = DT_INT32; + args[0].shape = TensorShape({2}); + args[1].kind = XlaCompiler::Argument::kParameter; + args[1].type = DT_INT32; + args[1].shape = TensorShape({2}); + return args; +} + +TEST(PjrtCompileUtilTest, CompileToPjRtLoadedExecutable) { + DeviceSetup device_setup; + TF_ASSERT_OK_AND_ASSIGN(auto fdef, SampleFuntionAddXY("foo")); + device_setup.AddDevicesAndSetUp({DEVICE_GPU}, fdef); + + Device* device = device_setup.GetDevice(DEVICE_GPU); + const XlaPlatformInfo platform_info = XlaPlatformInfoFromDevice(device); + + NameAttrList function; + function.set_name("foo"); + + ResourceMgr resource_mgr(""); + + const XlaCompiler::CompilationResult* compilation_result = nullptr; + xla::PjRtLoadedExecutable* pjrt_executable = nullptr; + xla::PjRtClient* pjrt_client = nullptr; + + TF_EXPECT_OK(CompileToPjRtLoadedExecutable( + device, platform_info, function, SampleArgsForAddXY(), + DeviceCompileMode::kStrict, /*has_ref_vars=*/true, + /*may_alias_resource_update=*/true, device_setup.flr(), &resource_mgr, + &compilation_result, &pjrt_client, &pjrt_executable)); + + EXPECT_TRUE(compilation_result != nullptr); + EXPECT_TRUE(pjrt_executable != nullptr); + EXPECT_TRUE(pjrt_client != nullptr); +} + +TEST(PjrtCompileUtilTest, CompileToPjRtLoadedExecutableWithOpKernelContext) { + DeviceSetup device_setup; + TF_ASSERT_OK_AND_ASSIGN(auto fdef, SampleFuntionAddXY("foo")); + device_setup.AddDevicesAndSetUp({DEVICE_GPU}, fdef); + + Device* device = device_setup.GetDevice(DEVICE_GPU); + const XlaPlatformInfo platform_info = XlaPlatformInfoFromDevice(device); + + NameAttrList function; + function.set_name("foo"); + + ResourceMgr resource_mgr(""); + OpKernelContext::Params params; + params.resource_manager = &resource_mgr; + params.device = device; + params.function_library = device_setup.flr(); + OpKernelContext ctx(¶ms, 1); + + const XlaCompiler::CompilationResult* compilation_result = nullptr; + xla::PjRtLoadedExecutable* pjrt_executable = nullptr; + xla::PjRtClient* pjrt_client = nullptr; + + TF_EXPECT_OK(CompileToPjRtLoadedExecutable( + ctx, platform_info, function, SampleArgsForAddXY(), + DeviceCompileMode::kStrict, /*has_ref_vars=*/true, + /*may_alias_resource_update=*/true, &compilation_result, &pjrt_client, + &pjrt_executable)); + + EXPECT_TRUE(compilation_result != nullptr); + EXPECT_TRUE(pjrt_executable != nullptr); + EXPECT_TRUE(pjrt_client != nullptr); +} + +} // namespace +} // namespace tensorflow +#endif diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_device_compiler_client.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_device_compiler_client.cc new file mode 100644 index 0000000000000000000000000000000000000000..e8addcb5567f113ac5b63388453f3f79d8bbfb97 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_device_compiler_client.cc @@ -0,0 +1,93 @@ +/* Copyright 2022 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/compiler/jit/pjrt_device_compiler_client.h" + +#include +#include +#include + +namespace tensorflow { + +xla::CompileOptions GetPjRtCompileOptions( + const XlaCompiler::Options& options, + const XlaCompiler::CompilationResult& result) { + xla::CompileOptions pjrt_compile_options; + pjrt_compile_options.argument_layouts = result.xla_input_shapes; + pjrt_compile_options.executable_build_options = + GetExecutableBuildOptions(options, result, /*default_device_ordinal=*/-1); + if (pjrt_compile_options.executable_build_options.num_replicas() > 1 || + pjrt_compile_options.executable_build_options.num_partitions() > 1) { + // Compile executable for sharded program + pjrt_compile_options.compile_portable_executable = false; + } else { + // Compile portable executable for single device compilation. + pjrt_compile_options.compile_portable_executable = true; + } + return pjrt_compile_options; +} + +absl::StatusOr> +PjRtDeviceCompilerClient::BuildExecutable( + const XlaCompiler::Options& options, + const XlaCompiler::CompilationResult& result) { + VLOG(2) << "Compiling to xla::PjRtLoadedExecutable."; + + TF_ASSIGN_OR_RETURN( + auto executable, + client_->CompileAndLoad(*result.computation, + GetPjRtCompileOptions(options, result))); + + VLOG(2) << "Compiled PJRT executable " << executable->name() + << " num_replicas " << executable->num_replicas() + << " num_partitions " << executable->num_partitions(); + + return std::move(executable); +} + +absl::StatusOr PjRtDeviceCompilerClient::SerializeExecutable( + const xla::PjRtLoadedExecutable& executable) { + VLOG(1) << "Serializing xla::PjRtLoadedExecutable to string."; + return executable.SerializeExecutable(); +} + +absl::StatusOr PjRtDeviceCompilerClient::BuildSerializedExecutable( + const XlaCompiler::Options& options, + const XlaCompiler::CompilationResult& result) { + VLOG(1) << "PJRT currently doesn't support AOT compilation. Compiling to " + "xla::PjRtLoadedExecutable and serializing it"; + TF_ASSIGN_OR_RETURN(auto executable, BuildExecutable(options, result)); + return executable->SerializeExecutable(); +} + +absl::StatusOr> +PjRtDeviceCompilerClient::LoadExecutable( + const XlaCompiler::Options& options, + const XlaCompiler::CompilationResult& result, + const std::string& serialized_executable) { + VLOG(1) << "Deserializing from string to xla::PjRtLoadedExecutable."; + return client_->LoadSerializedExecutable( + serialized_executable, GetPjRtCompileOptions(options, result), + xla::LoadOptions()); +} + +void PjRtDeviceCompilerClient::WaitForProgramsToFinish() { + // TODO(b/255826209): Modify this if PjRtClient exposes a function to wait for + // programs to finish. + LOG(INFO) << "Unimplemented: PJRT uses futures and waiting for programs to " + "finish isn't necessary."; +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_device_compiler_client.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_device_compiler_client.h new file mode 100644 index 0000000000000000000000000000000000000000..7002babbd25134536c212d6f0e31437099ae93c3 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_device_compiler_client.h @@ -0,0 +1,85 @@ +/* Copyright 2022 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_PJRT_DEVICE_COMPILER_CLIENT_H_ +#define TENSORFLOW_COMPILER_JIT_PJRT_DEVICE_COMPILER_CLIENT_H_ + +#include +#include +#include + +#include "tensorflow/compiler/jit/device_compiler_client.h" +#include "xla/pjrt/pjrt_client.h" + +namespace tensorflow { + +// Calls into PjRtClient to provide functionality for building, serializing and +// loading PjRtLoadedExecutables. +class PjRtDeviceCompilerClient + : public DeviceCompilerClient { + public: + explicit PjRtDeviceCompilerClient(xla::PjRtClient* client) + : client_(client) {} + + absl::StatusOr> BuildExecutable( + const XlaCompiler::Options& options, + const XlaCompiler::CompilationResult& result) override; + + // Returns a platform-specific serialization of `executable`. The + // serialization is not guaranteed to be stable over time. `executable` must + // have been produced by this client. + absl::StatusOr SerializeExecutable( + const xla::PjRtLoadedExecutable& executable) override; + + // PjRt doesn't support AOT compilation yet. Builds a PjRtLoadedExecutable and + // serializes it to string. + absl::StatusOr BuildSerializedExecutable( + const XlaCompiler::Options& options, + const XlaCompiler::CompilationResult& result) override; + + // Deserializes a serialized executable as produced by + // PjRtExecutable::SerializeExecutable(). `serialized_executable` must have + // been produced by a compiler of the same platform and version as this one. + // + // PjRt doesn't support AOT compilation yet. Loading a serialized executable + // is currently only implemented for TfrtTpuPjrtClient and hence, this + // function doesn't use PjRtClient::LoadSerializedExecutable() and uses + // PjRtClient::DeserializeExecutable() instead. + absl::StatusOr> LoadExecutable( + const XlaCompiler::Options& options, + const XlaCompiler::CompilationResult& result, + const std::string& serialized_executable) override; + + // No-op. PJRT uses futures and waiting for programs to finish isn't + // necessary. + void WaitForProgramsToFinish() override; + + xla::PjRtClient* client() const override { return client_; } + + private: + xla::PjRtClient* const client_; + + PjRtDeviceCompilerClient(const PjRtDeviceCompilerClient&) = delete; + void operator=(const PjRtDeviceCompilerClient&) = delete; +}; + +// Generates CompileOptions for PJRT compilation. +xla::CompileOptions GetPjRtCompileOptions( + const XlaCompiler::Options& options, + const XlaCompiler::CompilationResult& result); + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_PJRT_DEVICE_COMPILER_CLIENT_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_device_context.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_device_context.cc new file mode 100644 index 0000000000000000000000000000000000000000..ee6c0a3a7387124acc1996a451824b636395c601 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_device_context.cc @@ -0,0 +1,305 @@ +/* Copyright 2022 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/compiler/jit/pjrt_device_context.h" + +#include +#include +#include + +#include "absl/status/status.h" +#include "tensorflow/c/experimental/next_pluggable_device/tensor_pjrt_buffer_util.h" +#include "tensorflow/compiler/jit/pjrt_tensor_buffer.h" +#include "tensorflow/compiler/jit/pjrt_tensor_buffer_util.h" +#include "tensorflow/compiler/tf2xla/literal_util.h" +#include "xla/pjrt/pjrt_client.h" +#include "xla/pjrt/pjrt_common.h" +#include "xla/tsl/c/tsl_status_internal.h" +#include "xla/tsl/framework/device_id_utils.h" +#include "tensorflow/core/common_runtime/dma_helper.h" +#include "tensorflow/core/common_runtime/next_pluggable_device/next_pluggable_device_api.h" +#include "tensorflow/core/framework/device.h" +#include "tensorflow/core/framework/device_factory.h" +#include "tensorflow/core/profiler/lib/traceme.h" +#include "tensorflow/core/tfrt/common/async_value_tensor.h" +#include "tensorflow/core/tfrt/common/create_pjrt_client_util.h" + +namespace tensorflow { +namespace { + +absl::StatusOr> HostTensorToPjRtBuffer( + const tensorflow::Tensor* cpu_tensor, tensorflow::Device* device, + xla::PjRtClient* pjrt_client, + const XlaShapeLayoutHelpers::ShapeDeterminationFns + shape_determination_fns) { + XlaLayoutPreference layout_preference = + shape_determination_fns.layout_preference_fn( + cpu_tensor->shape(), cpu_tensor->dtype(), std::nullopt); + TF_ASSIGN_OR_RETURN(xla::Shape shape, + shape_determination_fns.shape_representation_fn( + cpu_tensor->shape(), cpu_tensor->dtype(), + /*fast_mem=*/false, layout_preference)); + const xla::Layout* device_layout = &(shape.layout()); + // The device id should match the local_device_id in + // tensorflow/compiler/xla/pjrt/pjrt_client.h. + const int pjrt_device_id = + tsl::GetDeviceIdFromDeviceParsedName(device->parsed_name()); + TF_ASSIGN_OR_RETURN( + xla::PjRtDevice * pjrt_device, + pjrt_client->LookupAddressableDevice(xla::LocalDeviceId(pjrt_device_id))); + TF_ASSIGN_OR_RETURN(xla::PjRtMemorySpace * pjrt_memory, + pjrt_device->default_memory_space()); + auto first_try_buffer = pjrt_client->BufferFromHostBuffer( + cpu_tensor->data(), shape.element_type(), shape.dimensions(), + /*byte_strides=*/std::nullopt, + xla::PjRtClient::HostBufferSemantics::kImmutableZeroCopy, + /*on_done_with_host_buffer=*/ + [cpu_tensor = *cpu_tensor]() { /* frees tensor */ }, pjrt_memory, + device_layout); + if (first_try_buffer.ok()) { + return std::move(*first_try_buffer); + } + if (first_try_buffer.status().code() == absl::StatusCode::kUnimplemented) { + LOG_FIRST_N(WARNING, 1) + << first_try_buffer.status() + << "; fallback to BufferFromHostBuffer without device layout."; + TF_ASSIGN_OR_RETURN( + std::unique_ptr second_try_buffer, + pjrt_client->BufferFromHostBuffer( + cpu_tensor->data(), shape.element_type(), shape.dimensions(), + /*byte_strides=*/std::nullopt, + xla::PjRtClient::HostBufferSemantics::kImmutableZeroCopy, + /*on_done_with_host_buffer=*/ + [cpu_tensor = *cpu_tensor]() { /* frees tensor */ }, pjrt_memory, + /*device_layout=*/nullptr)); + return second_try_buffer; + } else { + return first_try_buffer.status(); + } +} +} // namespace + +void PjRtDeviceContext::CopyDeviceTensorToCPU(const Tensor* device_tensor, + absl::string_view tensor_name, + Device* device, + Tensor* cpu_tensor, + StatusCallback done) { + tsl::profiler::TraceMe traceme("PjRtDeviceContext::CopyDeviceTensorToCPU"); + if (device_tensor->NumElements() == 0) { + VLOG(2) << "CopyDeviceTensorToCPU empty tensor"; + done(absl::OkStatus()); + return; + } + auto literal = std::make_unique(); + auto status = tensorflow::HostTensorToMutableBorrowingLiteral(cpu_tensor, + literal.get()); + if (!status.ok()) { + done(status); + return; + } + + xla::PjRtBuffer* device_buffer; + AsyncValueTensor* device_tensor_av = + tensorflow::AsyncValueTensor::FromTensor(device_tensor); + if (use_pjrt_tensor_buffer_) { + if (device_tensor_av) { + done(absl::InvalidArgumentError( + "If use_pjrt_tensor_buffer is set, the device tensor should not " + "contain an AsyncValueTensor.")); + return; + } + const PjRtTensorBuffer* pjrt_tensor_buffer = + dynamic_cast(DMAHelper::buffer(device_tensor)); + if (pjrt_tensor_buffer == nullptr) { + done(absl::UnimplementedError( + "use_pjrt_tensor_buffer is set to true. Transferring a tensor " + "without pjrt_tensor_buffer in this case is not supported.")); + return; + } + device_buffer = pjrt_tensor_buffer->pjrt_buffer(); + } else { + device_buffer = device_tensor_av->GetBuffer().get(); + } + + if (device_buffer == nullptr) { + done(absl::InvalidArgumentError( + "The device tensor has no associated device buffer.")); + return; + } + + tsl::Future future = device_buffer->ToLiteral(literal.get()); + future.OnReady([literal = std::move(literal), done = std::move(done)]( + const absl::Status& status) { done(status); }); +} + +void PjRtDeviceContext::CopyCPUTensorToDevice(const Tensor* cpu_tensor, + Device* device, + Tensor* device_tensor, + StatusCallback done, + bool sync_dst_compute) const { + tsl::profiler::TraceMe traceme("PjRtDeviceContext::CopyCPUTensorToDevice"); + if (cpu_tensor->NumElements() == 0) { + VLOG(2) << "CopyCPUTensorToDevice empty tensor"; + done(absl::OkStatus()); + return; + } + + absl::StatusOr pjrt_client = + GetOrCreatePjRtClient(DeviceType(device->device_type())); + if (!pjrt_client.ok()) { + done(pjrt_client.status()); + return; + } + absl::StatusOr> buffer_or = + HostTensorToPjRtBuffer(cpu_tensor, device, *pjrt_client, + shape_determination_fns_); + if (!buffer_or.ok()) { + done(buffer_or.status()); + return; + } + + xla::PjRtBuffer* pjrt_buffer = (*buffer_or).get(); + if (use_pjrt_tensor_buffer_) { + // Copy the newly created tensor with PjRtTensorBuffer to output device + // tensor. + absl::StatusOr t = MakeTensorFromPjRtBuffer( + device_tensor->dtype(), device_tensor->shape(), std::move(*buffer_or)); + if (!t.ok()) { + done(t.status()); + return; + } + *device_tensor = *t; + } else { + AsyncValueTensor* result_tensor = + tensorflow::AsyncValueTensor::FromTensor(device_tensor); + // The result tensor should be newly allocated, which does not point to a + // valid buffer yet. + CHECK(!result_tensor->GetBuffer()); // Crash OK + result_tensor->SetBuffer(std::move(*buffer_or)); + } + pjrt_buffer->GetReadyFuture().OnReady(std::move(done)); +} + +void PjRtDeviceContext::CopyTensorInSameDevice(const Tensor* input_tensor, + Device* device, + Tensor* output_tensor, + StatusCallback done) const { + if (!DeviceFactory::IsPluggableDevice(device->device_type())) { + done(absl::UnimplementedError( + "Same-device copies in PjRtDeviceContext is only implemented when " + "is_pluggable_device is true.")); + return; + } + // TODO(b/288585098): consider whether to support same device copy in PJRT + // API. + absl::StatusOr c_src_buffer = + GetPjRtCBufferFromTensor(input_tensor); + if (!c_src_buffer.ok()) { + done(c_src_buffer.status()); + return; + } + absl::StatusOr c_api_client = + tensorflow::GetPjRtCApiClient( + tensorflow::DeviceType(device->device_type())); + if (!c_api_client.ok()) { + done(c_api_client.status()); + return; + } + + TSL_Status c_status; + PJRT_Buffer* dst_buffer = TfnpdApi()->TFNPD_SameDevicePjRtBufferCopy( + *c_src_buffer, (*c_api_client)->pjrt_c_client(), &c_status); + if (!c_status.status.ok()) { + done(c_status.status); + return; + } + + auto set_c_buffer_status = + SetPjRtCBufferToTensor(dst_buffer, *c_api_client, output_tensor); + if (!set_c_buffer_status.ok()) { + done(set_c_buffer_status); + return; + } + AsyncValueTensor* result_tensor = + tensorflow::AsyncValueTensor::FromTensor(output_tensor); + result_tensor->GetBuffer()->GetReadyFuture().OnReady(std::move(done)); +} + +void PjRtDeviceToDeviceCopy(DeviceContext* send_dev_context, + DeviceContext* recv_dev_context, Device* src, + Device* dst, AllocatorAttributes src_alloc_attr, + AllocatorAttributes dst_alloc_attr, + const Tensor* input, Tensor* output, + int dev_to_dev_stream_index, StatusCallback done) { + tsl::profiler::TraceMe traceme("PjRtDevice_DeviceToDeviceCopy"); + if (input->NumElements() == 0) { + VLOG(2) << "PjRtDevice_DeviceToDeviceCopy empty tensor"; + done(absl::OkStatus()); + return; + } + + absl::StatusOr pjrt_dst_client = + GetOrCreatePjRtClient(DeviceType(dst->device_type())); + + if (!pjrt_dst_client.ok()) { + done(pjrt_dst_client.status()); + return; + } + + xla::PjRtBuffer* src_device_buffer = + tensorflow::AsyncValueTensor::FromTensor(input)->GetBuffer().get(); + + // The device id should match the local_device_id in + // tensorflow/compiler/xla/pjrt/pjrt_client.h. + const int pjrt_dst_device_id = + tsl::GetDeviceIdFromDeviceParsedName(dst->parsed_name()); + xla::PjRtDevice* pjrt_dst_device = + (*pjrt_dst_client) + ->LookupAddressableDevice(xla::LocalDeviceId(pjrt_dst_device_id)) + .value(); + + absl::StatusOr> buffer_or = + src_device_buffer->CopyToMemorySpace( + *pjrt_dst_device->default_memory_space()); + if (!buffer_or.ok()) { + done(buffer_or.status()); + return; + } + + xla::PjRtBuffer* pjrt_buffer = (*buffer_or).get(); + + if (static_cast(recv_dev_context) + ->use_pjrt_tensor_buffer()) { + // Copy the newly created tensor with PjRtTensorBuffer to output device + // tensor. + absl::StatusOr t = MakeTensorFromPjRtBuffer( + output->dtype(), output->shape(), std::move(*buffer_or)); + if (!t.ok()) { + done(t.status()); + return; + } + *output = *t; + } else { + AsyncValueTensor* output_tensor = + tensorflow::AsyncValueTensor::FromTensor(output); + // The result tensor should be newly allocated, which does not point to a + // valid buffer yet. + CHECK(!output_tensor->GetBuffer()); // Crash OK + output_tensor->SetBuffer(std::move(*buffer_or)); + } + pjrt_buffer->GetReadyFuture().OnReady(std::move(done)); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_device_context.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_device_context.h new file mode 100644 index 0000000000000000000000000000000000000000..095092aa8dfeab5f3b48609aff4d78f52d5ccd3d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_device_context.h @@ -0,0 +1,64 @@ +/* Copyright 2022 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_PJRT_DEVICE_CONTEXT_H_ +#define TENSORFLOW_COMPILER_JIT_PJRT_DEVICE_CONTEXT_H_ + +#include + +#include "tensorflow/compiler/tf2xla/layout_util.h" +#include "tensorflow/core/framework/device_base.h" +#include "tensorflow/core/platform/status.h" + +namespace tensorflow { + +// Helper class for managing data transfers between host and accelerator +// devices using PjRt. +class PjRtDeviceContext : public DeviceContext { + public: + explicit PjRtDeviceContext( + XlaShapeLayoutHelpers::ShapeDeterminationFns shape_determination_fns, + bool use_pjrt_tensor_buffer = false) + : shape_determination_fns_(std::move(shape_determination_fns)), + use_pjrt_tensor_buffer_(use_pjrt_tensor_buffer) {} + + void CopyCPUTensorToDevice(const Tensor* cpu_tensor, Device* device, + Tensor* device_tensor, StatusCallback done, + bool sync_dst_compute) const override; + void CopyDeviceTensorToCPU(const Tensor* device_tensor, + absl::string_view tensor_name, Device* device, + Tensor* cpu_tensor, StatusCallback done) override; + void CopyTensorInSameDevice(const Tensor* input_tensor, Device* device, + Tensor* output_tensor, + StatusCallback done) const override; + + bool use_pjrt_tensor_buffer() const { return use_pjrt_tensor_buffer_; } + + private: + XlaShapeLayoutHelpers::ShapeDeterminationFns shape_determination_fns_; + // Note: we currently assume the PjRtBuffer is a PjRtStreamExecutorBuffer. + bool use_pjrt_tensor_buffer_; +}; + +void PjRtDeviceToDeviceCopy(DeviceContext* send_dev_context, + DeviceContext* recv_dev_context, Device* src, + Device* dst, AllocatorAttributes src_alloc_attr, + AllocatorAttributes dst_alloc_attr, + const Tensor* input, Tensor* output, + int dev_to_dev_stream_index, StatusCallback done); + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_PJRT_DEVICE_CONTEXT_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_tensor_buffer.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_tensor_buffer.h new file mode 100644 index 0000000000000000000000000000000000000000..fab7cff5bc62d46caa9ff2681a7a1b3f0f9d8948 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_tensor_buffer.h @@ -0,0 +1,57 @@ +/* Copyright 2023 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_PJRT_TENSOR_BUFFER_H_ +#define TENSORFLOW_COMPILER_JIT_PJRT_TENSOR_BUFFER_H_ + +#include +#include + +#include "xla/pjrt/pjrt_client.h" +#include "tensorflow/core/framework/allocation_description.pb.h" +#include "tensorflow/core/framework/tensor.h" + +namespace tensorflow { + +// PjRtTensorBuffer is derived from TensorBuffer, which holds a device memory +// pointer so that legacy TF kernel can access it directly. PjRtTensorBuffer +// also owns a PjRtBuffer for XLA kernel's usage. +class PjRtTensorBuffer : public TensorBuffer { + public: + PjRtTensorBuffer(const void* ptr, size_t expected_size, + std::unique_ptr pjrt_buffer) + : TensorBuffer(const_cast(ptr)), + expected_size_(expected_size), + pjrt_buffer_(std::move(pjrt_buffer)) {} + + size_t size() const override { return expected_size_; } + + TensorBuffer* root_buffer() override { return this; } + + xla::PjRtBuffer* pjrt_buffer() const { return pjrt_buffer_.get(); } + + // TODO(b/288965065): Implement this. + void FillAllocationDescription(AllocationDescription* proto) const override { + proto->set_requested_bytes(static_cast(expected_size_)); + } + + private: + size_t expected_size_; + std::unique_ptr pjrt_buffer_; +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_PJRT_TENSOR_BUFFER_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_tensor_buffer_util.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_tensor_buffer_util.cc new file mode 100644 index 0000000000000000000000000000000000000000..e2d22259650036df80c7bfe437c00b737d355844 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_tensor_buffer_util.cc @@ -0,0 +1,89 @@ +/* Copyright 2023 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/compiler/jit/pjrt_tensor_buffer_util.h" + +#include +#include +#include + +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "tensorflow/compiler/jit/pjrt_tensor_buffer.h" +#include "xla/pjrt/pjrt_client.h" +#include "tensorflow/core/common_runtime/dma_helper.h" +#include "tensorflow/core/framework/tensor.h" +#include "tensorflow/core/framework/tensor_shape.h" +#include "tensorflow/core/framework/types.h" +#include "tsl/platform/statusor.h" + +namespace tensorflow { + +static size_t GetTensorSize(const TensorShape& shape, const DataType dtype) { + return shape.num_elements() * DataTypeSize(dtype); +} + +absl::StatusOr MakeTensorFromPjRtBuffer( + const DataType dtype, const TensorShape& shape, + std::unique_ptr pjrt_buffer) { + TF_ASSIGN_OR_RETURN(std::unique_ptr ref, + pjrt_buffer->AcquireExternalReference()); + auto* tensor_buffer = + new PjRtTensorBuffer(ref->OpaqueDeviceMemoryDataPointer(), + GetTensorSize(shape, dtype), std::move(pjrt_buffer)); + Tensor result(dtype, shape, tensor_buffer); + tensor_buffer->Unref(); + return result; +} + +// If existing_tensor does not use PjRtTensorBuffer and the opaque device memory +// is the same, the tensor should be reused so that the same device memory will +// not be double-freed. +static bool ShouldReuseTensor(void* opaque_device_memory, + const size_t expected_size, + const Tensor* existing_tensor) { + const PjRtTensorBuffer* input_pjrt_tensor_buffer = + dynamic_cast(DMAHelper::buffer(existing_tensor)); + if (input_pjrt_tensor_buffer != nullptr) { + return false; + } + + const size_t current_size = + GetTensorSize(existing_tensor->shape(), existing_tensor->dtype()); + return existing_tensor->tensor_data().data() == opaque_device_memory && + current_size == expected_size; +} + +absl::Status PjRtTensorBufferUtil::UpdateOrMakeTensorWithPjRtBuffer( + const DataType dtype, const TensorShape& shape, + std::unique_ptr pjrt_buffer, Tensor* output_tensor) { + TF_ASSIGN_OR_RETURN(std::unique_ptr ref, + pjrt_buffer->AcquireExternalReference()); + const size_t expected_size = GetTensorSize(shape, dtype); + void* opaque_device_memory = ref->OpaqueDeviceMemoryDataPointer(); + auto* tensor_buffer = new PjRtTensorBuffer( + opaque_device_memory, expected_size, std::move(pjrt_buffer)); + if (ShouldReuseTensor(opaque_device_memory, expected_size, output_tensor)) { + output_tensor->buf_ = tensor_buffer; + return absl::OkStatus(); + } + + Tensor result(dtype, shape, tensor_buffer); + tensor_buffer->Unref(); + *output_tensor = result; + return absl::OkStatus(); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_tensor_buffer_util.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_tensor_buffer_util.h new file mode 100644 index 0000000000000000000000000000000000000000..69d723f51c8ef13465b4d16221e9d875c6523e18 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_tensor_buffer_util.h @@ -0,0 +1,56 @@ +/* Copyright 2023 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_PJRT_TENSOR_BUFFER_UTIL_H_ +#define TENSORFLOW_COMPILER_JIT_PJRT_TENSOR_BUFFER_UTIL_H_ + +#include + +#include "absl/status/statusor.h" +#include "tensorflow/compiler/jit/pjrt_tensor_buffer.h" +#include "xla/pjrt/pjrt_client.h" +#include "tensorflow/core/framework/tensor.h" +#include "tensorflow/core/framework/tensor_shape.h" + +namespace tensorflow { + +// Takes the device memory pointer from the PjRtBuffer and create a Tensor that +// contains a PjRtTensorBuffer. The PjRtTensorBuffer holds the pointer to the +// device memory. It also owns the PjRtBuffer. +// +// TODO(b/289001822): Create a unit test to cover this function. +absl::StatusOr MakeTensorFromPjRtBuffer( + DataType dtype, const TensorShape& shape, + std::unique_ptr pjrt_buffer); + +// For TensorFlow internal use only. +class PjRtTensorBufferUtil { + public: + // Takes the device memory pointer from the PjRtBuffer and create a + // PjRtTensorBuffer. The PjRtTensorBuffer holds the pointer to the device + // memory. It also owns the PjRtBuffer. If output_tensor does not use + // PjRtTensorBuffer and the opaque device memory is the same, update the + // output_tensor->buf_ so that the same device memory will not be double-free. + // Otherwise a new Tensor will be created with the PjRtTensorBuffer. + // + // TODO(b/289001822): Create a unit test to cover this function. + static absl::Status UpdateOrMakeTensorWithPjRtBuffer( + DataType dtype, const TensorShape& shape, + std::unique_ptr pjrt_buffer, Tensor* output_tensor); +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_PJRT_TENSOR_BUFFER_UTIL_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_tensor_buffer_util_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_tensor_buffer_util_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..5d70ffb3ac7de159c74ddf3fb14dc010466f40cb --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/pjrt_tensor_buffer_util_test.cc @@ -0,0 +1,72 @@ +/* Copyright 2022 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/compiler/jit/pjrt_tensor_buffer_util.h" + +#include +#include +#include +#include + +#include +#include "tensorflow/compiler/jit/test_util.h" +#include "xla/pjrt/pjrt_client.h" +#include "xla/shape.h" +#include "xla/shape_util.h" +#include "tensorflow/core/framework/allocator.h" +#include "tensorflow/core/framework/device.h" +#include "tensorflow/core/framework/tensor.h" +#include "tensorflow/core/framework/tensor_shape.h" +#include "tensorflow/core/framework/types.h" +#include "tensorflow/core/tfrt/common/pjrt_util.h" +#include "tsl/platform/statusor.h" + +namespace tensorflow { +namespace { + +TEST(PjRtTensorBufferUtilTest, MakeTensorFromPjRtBuffer) { + DeviceSetup device_setup; + device_setup.AddDevicesAndSetUp({DEVICE_GPU}); + Device* device = device_setup.GetDevice(DEVICE_GPU); + std::vector dimensions = {2, 3}; + Tensor dest_cpu_tensor(cpu_allocator(), tensorflow::DT_INT32, + tensorflow::TensorShape(dimensions)); + TF_ASSERT_OK_AND_ASSIGN(auto pjrt_client, GetPjRtClient(DEVICE_GPU)); + std::vector data{1, 2, 3, 4, 5, 6}; + xla::Shape xla_shape = xla::ShapeUtil::MakeShape(xla::S32, dimensions); + xla::PjRtDevice* pjrt_device = pjrt_client->addressable_devices()[0]; + TF_ASSERT_OK_AND_ASSIGN(xla::PjRtMemorySpace * pjrt_memory, + pjrt_device->default_memory_space()); + TF_ASSERT_OK_AND_ASSIGN( + auto pjrt_buffer, + pjrt_client->BufferFromHostBuffer( + data.data(), xla_shape.element_type(), xla_shape.dimensions(), + /*byte_strides=*/std::nullopt, + xla::PjRtClient::HostBufferSemantics::kImmutableOnlyDuringCall, + nullptr, pjrt_memory, /*device_layout=*/nullptr)); + + TF_ASSERT_OK_AND_ASSIGN( + Tensor tensor, MakeTensorFromPjRtBuffer(DT_INT32, TensorShape(dimensions), + std::move(pjrt_buffer))); + + auto s = device->tensorflow_accelerator_device_info() + ->pjrt_context->CopyDeviceTensorToCPUSync(&tensor, "", device, + &dest_cpu_tensor); + for (int i = 0; i < tensor.NumElements(); ++i) { + EXPECT_EQ(dest_cpu_tensor.flat().data()[i], data[i]); + } +} + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/rearrange_function_argument_pass_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/rearrange_function_argument_pass_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..55ec6c072c7b504d8c6f6e14c4979686b7fc5025 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/rearrange_function_argument_pass_test.cc @@ -0,0 +1,240 @@ +/* Copyright 2019 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 "absl/log/check.h" +#include "absl/status/status.h" +#include "tensorflow/cc/framework/ops.h" +#include "tensorflow/cc/framework/scope.h" +#include "tensorflow/cc/ops/const_op.h" +#include "tensorflow/cc/ops/function_ops.h" +#include "tensorflow/cc/ops/functional_ops.h" +#include "tensorflow/compiler/tf2xla/rearrange_function_argument.h" +#include "xla/tsl/platform/errors.h" +#include "tensorflow/core/common_runtime/function.h" +#include "tensorflow/core/framework/function.h" +#include "tensorflow/core/framework/graph_to_functiondef.h" +#include "tensorflow/core/framework/node_def_util.h" +#include "tensorflow/core/framework/op.h" +#include "tensorflow/core/framework/tensor_shape.h" +#include "tensorflow/core/platform/test.h" +#include "tensorflow/core/protobuf/error_codes.pb.h" + +namespace tensorflow { + +TEST(RearrangeFunctionArgumentForFunctionTest, Basic) { + FunctionDefLibrary fdl; + { + // Function for StatefulPartitionedCall's "f", If's + // "then_branch"/"else_branch". + // "arg0" (T=DT_RESOURCE), "arg1" (T=DT_BOOL) + // "ret0" = "arg1" + // "ret1" = "arg0" + tensorflow::Scope s = tensorflow::Scope::NewRootScope(); + Output arg0 = ops::_Arg(s.WithOpName("arg0"), DT_RESOURCE, 0); + Output arg1 = ops::_Arg(s.WithOpName("arg1"), DT_BOOL, 1); + auto ret0 = ops::_Retval(s.WithOpName("ret0"), arg1, 0); + auto ret1 = ops::_Retval(s.WithOpName("ret1"), arg0, 1); + std::unique_ptr g(new Graph(OpRegistry::Global())); + CHECK_OK(s.ToGraph(g.get())); + FunctionDef *xla_fdef = fdl.add_function(); + CHECK_OK(GraphToFunctionDef(*g, "f1", xla_fdef)); + } + { + // Function for While's "body". + // "arg0" (T=DT_RESOURCE), "arg1" (T=DT_BOOL) + // "ret0" = "arg0" + // "ret1" = "arg1" + tensorflow::Scope s = tensorflow::Scope::NewRootScope(); + Output arg0 = ops::_Arg(s.WithOpName("arg0"), DT_RESOURCE, 0); + Output arg1 = ops::_Arg(s.WithOpName("arg1"), DT_BOOL, 1); + auto ret0 = ops::_Retval(s.WithOpName("ret0"), arg0, 0); + auto ret1 = ops::_Retval(s.WithOpName("ret1"), arg1, 1); + std::unique_ptr g(new Graph(OpRegistry::Global())); + CHECK_OK(s.ToGraph(g.get())); + FunctionDef *xla_fdef = fdl.add_function(); + CHECK_OK(GraphToFunctionDef(*g, "f2", xla_fdef)); + } + { + // Function for While's "cond". + // "arg0" (T=DT_RESOURCE), "arg1" (T=DT_BOOL) + // "ret0" = "arg1" + tensorflow::Scope s = tensorflow::Scope::NewRootScope(); + Output arg0 = ops::_Arg(s.WithOpName("arg0"), DT_RESOURCE, 0); + Output arg1 = ops::_Arg(s.WithOpName("arg1"), DT_BOOL, 1); + auto ret0 = ops::_Retval(s.WithOpName("ret0"), arg1, 0); + std::unique_ptr g(new Graph(OpRegistry::Global())); + CHECK_OK(s.ToGraph(g.get())); + FunctionDef *xla_fdef = fdl.add_function(); + CHECK_OK(GraphToFunctionDef(*g, "f3", xla_fdef)); + } + FunctionLibraryDefinition fld(OpRegistry::Global(), fdl); + + // Build the XLA computation graph. + // "arg0" (T=DT_RESOURCE), "arg1" (T=DT_INT32) + // "arg0", "arg1" -> "if" (If) -> "ret0", "ret1" + // "arg0", "arg1" -> "while" (While) -> "ret2", "ret3" + tensorflow::Scope s = tensorflow::Scope::NewRootScope(); + Output arg0 = ops::_Arg(s.WithOpName("arg0"), DT_RESOURCE, 0); + Output arg1 = ops::_Arg(s.WithOpName("arg1"), DT_BOOL, 1); + NameAttrList f; + f.set_name("f1"); + auto if_op = ops::If(s.WithOpName("if"), arg1, + std::initializer_list{arg0, arg1}, + {DT_BOOL, DT_RESOURCE}, f, f); + auto ret0 = ops::_Retval(s.WithOpName("ret0"), if_op.output[0], 0); + auto ret1 = ops::_Retval(s.WithOpName("ret1"), if_op.output[1], 1); + NameAttrList cond_fn, body_fn; + cond_fn.set_name("f3"); + body_fn.set_name("f2"); + auto while_op = + ops::While(s.WithOpName("while"), + std::initializer_list{arg0, arg1}, cond_fn, body_fn); + auto ret2 = ops::_Retval(s.WithOpName("ret2"), while_op.output[0], 2); + auto ret3 = ops::_Retval(s.WithOpName("ret3"), while_op.output[1], 3); + std::unique_ptr g(new Graph(OpRegistry::Global())); + CHECK_OK(s.ToGraph(g.get())); + + std::vector> fbodies; + CHECK_OK(RearrangeFunctionArguments( + [&](const NameAttrList& function, const FunctionBody** fbody) { + std::unique_ptr new_fbody; + TF_RETURN_IF_ERROR(FunctionDefToBodyHelper(*fld.Find(function.name()), + AttrSlice(&function.attr()), + &fld, &new_fbody)); + *fbody = new_fbody.get(); + fbodies.push_back(std::move(new_fbody)); + return absl::OkStatus(); + }, + g.get(), &fld)); + + // Check function f1_rearrange_0, input types should be {DT_BOOL, DT_RESOURCE} + // and output types should be {DT_BOOL}. + const FunctionDef *f1_rewritten = fld.Find("f1_rearrange_0"); + CHECK_NE(f1_rewritten, nullptr); + ASSERT_EQ(f1_rewritten->signature().input_arg_size(), 2); + EXPECT_EQ(f1_rewritten->signature().input_arg(0).type(), DT_BOOL); + EXPECT_EQ(f1_rewritten->signature().input_arg(1).type(), DT_RESOURCE); + ASSERT_EQ(f1_rewritten->signature().output_arg_size(), 1); + EXPECT_EQ(f1_rewritten->signature().output_arg(0).type(), DT_BOOL); + + // Check node "if" input and output edges. + auto node_name_index = g->BuildNodeNameIndex(); + const Node *if_node = node_name_index.at("if"); + ASSERT_NE(if_node, nullptr); + const Node *input_node; + CHECK_OK(if_node->input_node(1, &input_node)); + EXPECT_EQ(input_node->name(), "arg1"); + CHECK_OK(if_node->input_node(2, &input_node)); + EXPECT_EQ(input_node->name(), "arg0"); + const Node *ret0_node = node_name_index.at("ret0"); + ASSERT_NE(ret0_node, nullptr); + CHECK_OK(ret0_node->input_node(0, &input_node)); + EXPECT_EQ(input_node->name(), "if"); + const Node *ret1_node = node_name_index.at("ret1"); + ASSERT_NE(ret1_node, nullptr); + CHECK_OK(ret1_node->input_node(0, &input_node)); + EXPECT_EQ(input_node->name(), "arg0"); + + // Check node "while" input and output edges. + const Node *while_node = node_name_index.at("while"); + ASSERT_NE(while_node, nullptr); + CHECK_OK(while_node->input_node(0, &input_node)); + EXPECT_EQ(input_node->name(), "arg1"); + CHECK_OK(while_node->input_node(1, &input_node)); + EXPECT_EQ(input_node->name(), "arg0"); + const Node *ret2_node = node_name_index.at("ret2"); + ASSERT_NE(ret2_node, nullptr); + CHECK_OK(ret2_node->input_node(0, &input_node)); + EXPECT_EQ(input_node->name(), "arg0"); + const Node *ret3_node = node_name_index.at("ret3"); + ASSERT_NE(ret3_node, nullptr); + CHECK_OK(ret3_node->input_node(0, &input_node)); + EXPECT_EQ(input_node->name(), "while"); +} + +TEST(RearrangeFunctionArgumentForFunctionTest, + WhileResourceRetvalFromDifferentArgUnimplemented) { + FunctionDefLibrary fdl; + { + // Function for While's "body". + // "arg0" (T=DT_RESOURCE), "arg1" (T=DT_RESOURCE), "arg2" (T=DT_INT32) + // "ret0" = "arg1" + // "ret1" = "arg0" + tensorflow::Scope s = tensorflow::Scope::NewRootScope(); + Output arg0 = ops::_Arg(s.WithOpName("arg0"), DT_RESOURCE, 0); + Output arg1 = ops::_Arg(s.WithOpName("arg1"), DT_RESOURCE, 1); + Output arg2 = ops::_Arg(s.WithOpName("arg2"), DT_INT32, 2); + auto ret0 = ops::_Retval(s.WithOpName("ret0"), arg1, 0); + auto ret1 = ops::_Retval(s.WithOpName("ret1"), arg0, 1); + auto ret2 = ops::_Retval(s.WithOpName("ret2"), arg2, 2); + std::unique_ptr g(new Graph(OpRegistry::Global())); + CHECK_OK(s.ToGraph(g.get())); + FunctionDef *xla_fdef = fdl.add_function(); + CHECK_OK(GraphToFunctionDef(*g, "f2", xla_fdef)); + } + { + // Function for While's "cond". + // "arg0" (T=DT_RESOURCE), "arg1" (T=DT_RESOURCE), "arg2" (T=DT_INT32) + // "ret0" = true + tensorflow::Scope s = tensorflow::Scope::NewRootScope(); + Output arg0 = ops::_Arg(s.WithOpName("arg0"), DT_RESOURCE, 0); + Output arg1 = ops::_Arg(s.WithOpName("arg1"), DT_RESOURCE, 1); + Output arg2 = ops::_Arg(s.WithOpName("arg2"), DT_INT32, 2); + Output cond = ops::Const(s.WithOpName("const"), true, TensorShape({})); + auto ret0 = ops::_Retval(s.WithOpName("ret0"), cond, 0); + std::unique_ptr g(new Graph(OpRegistry::Global())); + CHECK_OK(s.ToGraph(g.get())); + FunctionDef *xla_fdef = fdl.add_function(); + CHECK_OK(GraphToFunctionDef(*g, "f1", xla_fdef)); + } + FunctionLibraryDefinition fld(OpRegistry::Global(), fdl); + + // Build the XLA computation graph. + // "arg0" (T=DT_RESOURCE), "arg1" (T=DT_RESOURCE), "arg2" (T=DT_INT32) + // "arg0", "arg1" -> "while" (While) + tensorflow::Scope s = tensorflow::Scope::NewRootScope(); + Output arg0 = ops::_Arg(s.WithOpName("arg0"), DT_RESOURCE, 0); + Output arg1 = ops::_Arg(s.WithOpName("arg1"), DT_RESOURCE, 1); + Output arg2 = ops::_Arg(s.WithOpName("arg2"), DT_INT32, 2); + NameAttrList cond_fn, body_fn; + cond_fn.set_name("f1"); + body_fn.set_name("f2"); + auto while_op = ops::While(s.WithOpName("while"), + std::initializer_list{arg0, arg1, arg2}, + cond_fn, body_fn); + std::unique_ptr g(new Graph(OpRegistry::Global())); + CHECK_OK(s.ToGraph(g.get())); + + std::vector> fbodies; + absl::Status status = RearrangeFunctionArguments( + [&](const NameAttrList &function, const FunctionBody **fbody) { + std::unique_ptr new_fbody; + TF_RETURN_IF_ERROR(FunctionDefToBodyHelper(*fld.Find(function.name()), + AttrSlice(&function.attr()), + &fld, &new_fbody)); + *fbody = new_fbody.get(); + fbodies.push_back(std::move(new_fbody)); + return absl::OkStatus(); + }, + g.get(), &fld); + EXPECT_EQ(status.code(), error::UNIMPLEMENTED); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/report_clustering_info_pass.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/report_clustering_info_pass.cc new file mode 100644 index 0000000000000000000000000000000000000000..63d24306fc0ff0e650941c0bce05245d7476b05c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/report_clustering_info_pass.cc @@ -0,0 +1,32 @@ +/* Copyright 2019 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/compiler/jit/report_clustering_info_pass.h" + +#include "tensorflow/compiler/jit/flags.h" +#include "tensorflow/compiler/jit/xla_activity_listener.h" +#include "tensorflow/compiler/jit/xla_cluster_util.h" + +namespace tensorflow { +absl::Status ReportClusteringInfoPass::Run( + const GraphOptimizationPassOptions& options) { + XlaAutoClusteringActivity activity; + *activity.mutable_summary() = GetXlaAutoClusteringSummary(**options.graph); + activity.set_global_jit_level(GetGlobalJitLevelForGraph(options)); + activity.set_cpu_global_jit_enabled( + GetMarkForCompilationPassFlags()->tf_xla_cpu_global_jit); + return BroadcastXlaActivity(std::move(activity)); +} +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/report_clustering_info_pass.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/report_clustering_info_pass.h new file mode 100644 index 0000000000000000000000000000000000000000..3bb38ce99239084b766b5e6973c6c43923cc3d8c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/report_clustering_info_pass.h @@ -0,0 +1,32 @@ +/* Copyright 2019 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_REPORT_CLUSTERING_INFO_PASS_H_ +#define TENSORFLOW_COMPILER_JIT_REPORT_CLUSTERING_INFO_PASS_H_ + +#include "tensorflow/core/common_runtime/optimization_registry.h" + +namespace tensorflow { + +// This is not really an optimization pass. It does not change the graph in any +// way; instead it computes a summary of the XLA clusters in the graph and +// broadcasts it via xla_activity_listener. +class ReportClusteringInfoPass : public GraphOptimizationPass { + public: + absl::Status Run(const GraphOptimizationPassOptions& options) override; +}; +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_REPORT_CLUSTERING_INFO_PASS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/resource_operation_safety_analysis.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/resource_operation_safety_analysis.cc new file mode 100644 index 0000000000000000000000000000000000000000..f4b669f6bfc59e01c732df64f2310d27b7c68b7c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/resource_operation_safety_analysis.cc @@ -0,0 +1,321 @@ +/* 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. +==============================================================================*/ + +// ALGORITHM OVERVIEW +// ================== +// +// An XLA cluster hoists all resource reads to be beginning of the cluster +// execution and all the resource writes to the end. This means it cannot +// enforce arbitrary ordering dependencies (via control or data edges) between +// resource operations. Since all resource reads happen before all resource +// writes, edges constraining resource reads to happen before resource writes +// are fine, but all other kinds of edges are problematic. This analysis +// computes the set of pairs of resource operations that cannot be put in the +// same cluster because XLA cannot respect the dependencies between them in the +// TensorFlow program. +// +// TODO(b/112856632): We can, in theory, support Read->Read and Write->Write +// dependencies. +// +// Specifically the result computed by this analysis contains the edge {W, R} +// iff all of these hold true: +// +// - In the graph (g - {edges from NextIteration to Merge}) there is a path +// from W to R. +// - IsEdgeSafe(W, R) == False [defined below] +// - W != R (note: some resource operations both read from and write to +// resource variables). +// +// The result is incorrect around loops because we ignore edges from +// NextIteration to Merge. For instance, in: +// +// Init -----> Merge <-------+ +// | | +// v | +// Read | +// | | +// v | +// Write | +// | | +// v | +// NextIteration --+ +// +// we won't put (Read, Write) in the returned set. This is fine if +// auto-clustering can only cluster the Read->Write edge, but it is a problem if +// it clusters the Write->NextIteration->Merge->Read edges instead. So we rely +// on auto-clustering to not cluster NextIteration->Merge edges. The same +// problem is present for the functional version of the loop above and we also +// rely on auto-clustering not clustering functional while loops containing +// resource operations. +// +// One way to think about this is that we only care about cases where two nodes, +// A and B, would normally have been put in the same cluster but cannot legally +// be in the same cluster because of resourcevar-dependencies. If A and B would +// normally have been put in the same cluster then all paths between A and B +// would have to be clusterable (otherwise we'd have introduced a cycle). Ergo +// there could not have been a NextIteration->Merge edge between A and B since +// we don't cluster these edges. +// +// IMPLEMENTATION +// -------------- +// +// We traverse the graph minus backedges in reverse post order, mapping each +// node to the set of resource operation reaching that node. Since we visit +// producers before consumers, we can construct the set of reaching operations +// by taking the union of the operations reaching the input nodes. These +// "reaching resource operations" can then be used to create the pairs of +// incompatible nodes using `IsEdgeSafe`. + +#include "tensorflow/compiler/jit/resource_operation_safety_analysis.h" + +#include "absl/container/flat_hash_set.h" +#include "absl/memory/memory.h" +#include "absl/strings/str_join.h" +#include "absl/types/optional.h" +#include "tensorflow/compiler/jit/xla_cluster_util.h" +#include "tensorflow/compiler/tf2xla/resource_operation_table.h" +#include "tensorflow/core/framework/node_def.pb.h" +#include "tensorflow/core/graph/algorithm.h" +#include "tensorflow/core/graph/tensor_id.h" +#include "tensorflow/core/lib/hash/hash.h" + +namespace tensorflow { +namespace { +// Maps `n` to the XlaResourceOpKind corresponding to its operation. If `n` is +// not a resource operation recognized by XLA then sets `out_resource_op_kind` +// to nullopt. +absl::Status XlaResourceOpKindForNode( + const Node& n, const FunctionLibraryDefinition* flib_def, + const std::function& + resource_ops_to_ignore, + std::optional* out_resource_op_kind) { + bool should_ignore = false; + if (resource_ops_to_ignore) { + TF_RETURN_IF_ERROR(resource_ops_to_ignore(n, &should_ignore)); + } + if (should_ignore) { + *out_resource_op_kind = std::nullopt; + return absl::OkStatus(); + } + + const XlaResourceOpInfo* op_info = GetResourceOpInfoForOp(n.type_string()); + if (op_info) { + *out_resource_op_kind = op_info->kind(); + return absl::OkStatus(); + } + + // We conservatively assume that functions will both read and write resource + // variables. In the future we may consider doing some form of + // inter-procedural analysis. + if (MayCallFunction(n, flib_def)) { + *out_resource_op_kind = XlaResourceOpKind::kReadWrite; + } else { + *out_resource_op_kind = std::nullopt; + } + + return absl::OkStatus(); +} + +// Returns true if a control or data dependence from a TensorFlow operation of +// resource op kind `from` to a TensorFlow operation of resource op kind `to` +// can be represented by an XLA cluster and needs no special handling around +// auto-jit. +bool IsEdgeSafe(XlaResourceOpKind from, XlaResourceOpKind to) { + // XLA clusters force all reads to happen before all writes. Moreover the set + // of reads are executed as one atomic operation, and the set of writes are as + // another atomic operation. This means we can faithfully represent the + // following edges: Read->*, *->Write. + + return from == XlaResourceOpKind::kRead || to == XlaResourceOpKind::kWrite; +} + +using ResourceOp = std::pair; + +std::string ResourceOpToString(const ResourceOp& resource_op) { + return absl::StrCat( + resource_op.first, ": ", + XlaResourceOpInfo::XlaResourceOpKindToString(resource_op.second)); +} + +// A copy-on-write set used to store the set of ResourceOps reaching a node in a +// TensorFlow graph. +// +// TODO(sanjoy): It may be useful to pull this out into its own header at some +// point. +class ResourceOpSet { + private: + using Impl = absl::flat_hash_set; + + public: + ResourceOpSet() = default; + + // Adds all ResourceOp s in `other` to this set. + void Add(const ResourceOpSet& other) { + CHECK(!frozen_); + if (other.impl_ == impl_) { + other.frozen_ = true; + return; + } + + if (!impl_) { + other.frozen_ = true; + impl_ = other.impl_; + return; + } + + for (ResourceOp resource_op : other) { + Add(resource_op); + } + } + + void Add(const ResourceOp& resource_op) { + CHECK(!frozen_); + if (!IsCopy() && Contains(resource_op)) { + // We can avoid the copy if the item we want to insert already exists. + return; + } + + EnsureIsCopied(); + impl_->insert(resource_op); + } + + Impl::const_iterator begin() const { + return impl_ ? impl_->begin() : GetEmptyImpl()->begin(); + } + + Impl::const_iterator end() const { + return impl_ ? impl_->end() : GetEmptyImpl()->end(); + } + + bool Contains(const ResourceOp& resource_op) const { + return impl_ != nullptr && impl_->count(resource_op); + } + + private: + bool IsCopy() const { return storage_ != nullptr; } + + void EnsureIsCopied() { + if (storage_ == nullptr) { + storage_ = std::make_unique(); + for (ResourceOp op : *this) { + storage_->insert(op); + } + impl_ = storage_.get(); + } + } + + static Impl* GetEmptyImpl() { + static Impl* empty_impl = new Impl; + return empty_impl; + } + + Impl* impl_ = nullptr; + std::unique_ptr storage_; + + // frozen_ is true if there is another set pointing to this set's impl_. We + // can no longer add elements to this set in that case since the sets pointing + // to this set expect the contents of this set to be stable. + mutable bool frozen_ = false; + + ResourceOpSet(const ResourceOpSet&) = delete; + void operator=(const ResourceOpSet&) = delete; +}; + +std::string ResourceOpSetToString(const ResourceOpSet& resource_op_set) { + std::vector elements_debug_string; + std::transform(resource_op_set.begin(), resource_op_set.end(), + std::back_inserter(elements_debug_string), ResourceOpToString); + return absl::StrCat("{", absl::StrJoin(elements_debug_string, ","), "}"); +} + +std::string NodeToString(const Node& n, XlaResourceOpKind resource_op_kind) { + return absl::StrCat( + "[", n.name(), ": ", n.type_string(), "(", + XlaResourceOpInfo::XlaResourceOpKindToString(resource_op_kind), ")", "]"); +} +} // namespace + +absl::Status ComputeIncompatibleResourceOperationPairs( + const Graph& g, const FunctionLibraryDefinition* flib_def, + const std::function& + resource_ops_to_ignore, + std::vector>* result) { + CHECK(result->empty()); + + std::vector rpo; + GetReversePostOrder(g, &rpo, /*stable_comparator=*/NodeComparatorName(), + /*edge_filter=*/[](const Edge& edge) { + return !edge.src()->IsNextIteration(); + }); + + auto resource_op_set_for_node = + std::make_unique(g.num_node_ids()); + + const bool vlog = VLOG_IS_ON(2); + + for (Node* n : rpo) { + std::optional op_kind; + TF_RETURN_IF_ERROR(XlaResourceOpKindForNode( + *n, flib_def, resource_ops_to_ignore, &op_kind)); + + ResourceOpSet* resource_op_set = &resource_op_set_for_node[n->id()]; + + // Merge the reaching resource operations for all the incoming edges to + // create the set of all possible resource ops reaching `n`. + for (const Edge* e : n->in_edges()) { + if (n->IsMerge() && e->src()->IsNextIteration()) { + // Ignore back-edges (see file comment). + continue; + } + + const ResourceOpSet& incoming_op_set = + resource_op_set_for_node[e->src()->id()]; + resource_op_set->Add(incoming_op_set); + } + + // Add to the "incompatible resource ops" set if necessary. + if (op_kind) { + for (ResourceOp incoming_op : *resource_op_set) { + if (IsEdgeSafe(incoming_op.second, *op_kind)) { + continue; + } + + if (vlog) { + VLOG(2) << "Unsafe edge: " + << NodeToString(*g.FindNodeId(incoming_op.first), + incoming_op.second) + << " -> " << NodeToString(*n, *op_kind); + } + result->push_back({incoming_op.first, n->id()}); + } + + // Some graphs might have a lot of 'kRead' kinds, but they are always safe + // for incoming ops, so not storing them might save a lot of memory. + if (op_kind != XlaResourceOpKind::kRead) { + resource_op_set->Add({n->id(), *op_kind}); + } + } + + if (vlog) { + VLOG(3) << n->name() << " -> " << ResourceOpSetToString(*resource_op_set); + } + } + + std::sort(result->begin(), result->end()); + CHECK(std::unique(result->begin(), result->end()) == result->end()); + + return absl::OkStatus(); +} +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/resource_operation_safety_analysis.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/resource_operation_safety_analysis.h new file mode 100644 index 0000000000000000000000000000000000000000..0c00f8c27a56a609b45a676e25ff3cb3b1168f70 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/resource_operation_safety_analysis.h @@ -0,0 +1,69 @@ +/* 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_RESOURCE_OPERATION_SAFETY_ANALYSIS_H_ +#define TENSORFLOW_COMPILER_JIT_RESOURCE_OPERATION_SAFETY_ANALYSIS_H_ + +#include "xla/service/graphcycles/graphcycles.h" +#include "tensorflow/core/framework/function.h" +#include "tensorflow/core/graph/graph.h" + +namespace tensorflow { +// An XLA cluster hoists all resource reads to be beginning of the cluster +// execution and all the resource writes to the end. This means it cannot +// enforce arbitrary ordering dependencies (via control or data edges) between +// resource operations. Since all resource reads happen before all resource +// writes, edges constraining resource writes to happen before resource reads +// are problematic. This analysis returns the set of pairs of resource +// operations that cannot be put in the same cluster because XLA cannot respect +// the dependencies between them in the TensorFlow program. +// +// The restrictions are not transitive: it is fine to put A and C in the same +// cluster even if the returned set contains (A,B) and (B,C). +// +// In other words, if these pairs are seen as edges in an undirected graph of +// the nodes in `g` then auto-clustering is at least as constrained as the graph +// coloring problem on this graph. +// +// +// For instance if we auto-cluster all operations in this TensorFlow graph: +// +// AssignVariablepOp0 -> AssignVariableOp1 +// | +// v +// ReadVariableOp0 -> ReadVariableOp1 +// +// we will lose the AssignVariablepOp1 -> ReadVariableOp0. The ReadVariableOp0 +// -> ReadVariableOp1 and AssignVariableOp0 -> AssignVariableOp1 edges will be +// respected by XlaLaunchOp though because all reads happen before all writes +// with that limited clustering.. +// +// +// NB! The result computed by this analysis assumes that we don't auto-cluster +// back-edges (i.e. the edges from NextIteration to Merge). +// +// NB! The result computed by this analysis assumes that we don't auto-cluster +// functional control flow nodes containing resource operations. +// +// If `resource_ops_to_ignore` is set then nodes for which it returns true are +// ignored (we pretend these nodes are not resource operations). +absl::Status ComputeIncompatibleResourceOperationPairs( + const Graph& g, const FunctionLibraryDefinition* flib_def, + const std::function& + resource_ops_to_ignore, + std::vector>* result); +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_RESOURCE_OPERATION_SAFETY_ANALYSIS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/resource_operation_safety_analysis_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/resource_operation_safety_analysis_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..93be354956c442fda9e472d7a642d985be288f46 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/resource_operation_safety_analysis_test.cc @@ -0,0 +1,522 @@ +/* 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 "tensorflow/compiler/jit/resource_operation_safety_analysis.h" + +#include "tensorflow/cc/framework/ops.h" +#include "tensorflow/cc/ops/array_ops.h" +#include "tensorflow/cc/ops/control_flow_ops_internal.h" +#include "tensorflow/cc/ops/function_ops.h" +#include "tensorflow/cc/ops/functional_ops.h" +#include "tensorflow/cc/ops/resource_variable_ops.h" +#include "tensorflow/cc/ops/sendrecv_ops.h" +#include "tensorflow/cc/ops/standard_ops.h" +#include "tensorflow/compiler/jit/defs.h" +#include "tensorflow/compiler/tf2xla/xla_op_kernel.h" +#include "tensorflow/compiler/tf2xla/xla_op_registry.h" +#include "tensorflow/core/common_runtime/graph_constructor.h" +#include "tensorflow/core/framework/node_def_util.h" +#include "tensorflow/core/framework/op.h" +#include "tensorflow/core/graph/algorithm.h" +#include "tensorflow/core/graph/graph_def_builder.h" +#include "tensorflow/core/lib/core/status_test_util.h" +#include "tensorflow/core/lib/strings/str_util.h" +#include "tensorflow/core/platform/test.h" + +namespace tensorflow { +namespace { + +Node* MakeRead(const Scope& scope, const std::string& id) { + Output var_handle = + ops::VarHandleOp(scope.WithOpName("Var" + id), DT_FLOAT, TensorShape({})); + Output read = + ops::ReadVariableOp(scope.WithOpName("Read" + id), var_handle, DT_FLOAT); + return read.node(); +} + +Node* MakeWrite(const Scope& scope, const std::string& id) { + Output var_handle = + ops::VarHandleOp(scope.WithOpName("Var" + id), DT_FLOAT, TensorShape({})); + Output value_to_write = + ops::Const(scope.WithOpName("ValueToAssign" + id), 1.0f); + ops::AssignVariableOp assign_op(scope.WithOpName("Assignee" + id), var_handle, + value_to_write); + return assign_op.operation.node(); +} + +Node* MakeModify(const Scope& scope, const std::string& id) { + Output var_handle = + ops::VarHandleOp(scope.WithOpName("Var" + id), DT_FLOAT, TensorShape({})); + Output value_to_write = ops::Const(scope.WithOpName("Increment" + id), 1.0f); + ops::AssignAddVariableOp assign_add_op(scope.WithOpName("Increment" + id), + var_handle, value_to_write); + return assign_add_op.operation.node(); +} + +Node* MakeNeutral(const Scope& scope, const std::string& id) { + return ops::Const(scope.WithOpName("Const" + id), 42.0f).node(); +} + +absl::Status ComputeIncompatiblePairs( + Graph* g, std::vector>* result) { + FixupSourceAndSinkEdges(g); + return ComputeIncompatibleResourceOperationPairs(*g, &g->flib_def(), {}, + result); +} + +TEST(ResourceOperationSafetyAnalysisTest, WriteRead) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Node* read = MakeRead(root, "R"); + Node* write = MakeWrite(root, "W"); + + root.graph()->AddControlEdge(write, read); + + std::vector> incompatible_pairs; + TF_ASSERT_OK(ComputeIncompatiblePairs(root.graph(), &incompatible_pairs)); + + ASSERT_EQ(incompatible_pairs.size(), 1); + std::pair write_read_pair = {write->id(), read->id()}; + EXPECT_EQ(incompatible_pairs[0], write_read_pair); +} + +TEST(ResourceOperationSafetyAnalysisTest, ReadWrite) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Node* read = MakeRead(root, "R"); + Node* write = MakeWrite(root, "W"); + + root.graph()->AddControlEdge(read, write); + + std::vector> incompatible_pairs; + TF_ASSERT_OK(ComputeIncompatiblePairs(root.graph(), &incompatible_pairs)); + + EXPECT_EQ(incompatible_pairs.size(), 0); +} + +TEST(ResourceOperationSafetyAnalysisTest, ReadWriteNoEdges) { + Scope root = Scope::NewRootScope().ExitOnError(); + + MakeRead(root, "R"); + MakeWrite(root, "W"); + + std::vector> incompatible_pairs; + TF_ASSERT_OK(ComputeIncompatiblePairs(root.graph(), &incompatible_pairs)); + + EXPECT_EQ(incompatible_pairs.size(), 0); +} + +TEST(ResourceOperationSafetyAnalysisTest, ReadModify) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Node* read = MakeRead(root, "R"); + Node* modify = MakeModify(root, "M"); + + root.graph()->AddControlEdge(read, modify); + + std::vector> incompatible_pairs; + TF_ASSERT_OK(ComputeIncompatiblePairs(root.graph(), &incompatible_pairs)); + + EXPECT_EQ(incompatible_pairs.size(), 0); +} + +TEST(ResourceOperationSafetyAnalysisTest, ModifyRead) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Node* read = MakeRead(root, "R"); + Node* modify = MakeModify(root, "M"); + + root.graph()->AddControlEdge(modify, read); + + std::vector> incompatible_pairs; + TF_ASSERT_OK(ComputeIncompatiblePairs(root.graph(), &incompatible_pairs)); + + ASSERT_EQ(incompatible_pairs.size(), 1); + std::pair modify_read_pair = {modify->id(), read->id()}; + EXPECT_EQ(incompatible_pairs[0], modify_read_pair); +} + +TEST(ResourceOperationSafetyAnalysisTest, ModifyWrite) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Node* modify = MakeModify(root, "M"); + Node* write = MakeWrite(root, "W"); + + root.graph()->AddControlEdge(modify, write); + + std::vector> incompatible_pairs; + TF_ASSERT_OK(ComputeIncompatiblePairs(root.graph(), &incompatible_pairs)); + + EXPECT_EQ(incompatible_pairs.size(), 0); +} + +TEST(ResourceOperationSafetyAnalysisTest, WriteModify) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Node* modify = MakeModify(root, "M"); + Node* write = MakeWrite(root, "W"); + + root.graph()->AddControlEdge(write, modify); + + std::vector> incompatible_pairs; + TF_ASSERT_OK(ComputeIncompatiblePairs(root.graph(), &incompatible_pairs)); + + ASSERT_EQ(incompatible_pairs.size(), 1); + std::pair write_modify_pair = {write->id(), modify->id()}; + EXPECT_EQ(incompatible_pairs[0], write_modify_pair); +} + +TEST(ResourceOperationSafetyAnalysisTest, ReadModifyWrite) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Node* read = MakeRead(root, "R"); + Node* modify = MakeModify(root, "M"); + Node* write = MakeWrite(root, "W"); + + root.graph()->AddControlEdge(read, modify); + root.graph()->AddControlEdge(modify, write); + + std::vector> incompatible_pairs; + TF_ASSERT_OK(ComputeIncompatiblePairs(root.graph(), &incompatible_pairs)); + + EXPECT_EQ(incompatible_pairs.size(), 0); +} + +TEST(ResourceOperationSafetyAnalysisTest, WriteModifyRead) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Node* read = MakeRead(root, "R"); + Node* modify = MakeModify(root, "M"); + Node* write = MakeWrite(root, "W"); + + root.graph()->AddControlEdge(write, modify); + root.graph()->AddControlEdge(modify, read); + + std::vector> incompatible_pairs; + TF_ASSERT_OK(ComputeIncompatiblePairs(root.graph(), &incompatible_pairs)); + + ASSERT_EQ(incompatible_pairs.size(), 3); + + std::pair write_modify_pair = {write->id(), modify->id()}; + std::pair modify_read_pair = {modify->id(), read->id()}; + std::pair write_read_pair = {write->id(), read->id()}; + EXPECT_EQ(incompatible_pairs[0], modify_read_pair); + EXPECT_EQ(incompatible_pairs[1], write_read_pair); + EXPECT_EQ(incompatible_pairs[2], write_modify_pair); +} + +TEST(ResourceOperationSafetyAnalysisTest, WriteReadModify) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Node* read = MakeRead(root, "R"); + Node* modify = MakeModify(root, "M"); + Node* write = MakeWrite(root, "W"); + + root.graph()->AddControlEdge(write, read); + root.graph()->AddControlEdge(read, modify); + + std::vector> incompatible_pairs; + TF_ASSERT_OK(ComputeIncompatiblePairs(root.graph(), &incompatible_pairs)); + + ASSERT_EQ(incompatible_pairs.size(), 2); + + std::pair write_modify_pair = {write->id(), modify->id()}; + std::pair write_read_pair = {write->id(), read->id()}; + EXPECT_EQ(incompatible_pairs[0], write_read_pair); + EXPECT_EQ(incompatible_pairs[1], write_modify_pair); +} + +FunctionDefLibrary CreateFunctionDefLibWithConstFunction( + const std::string& name) { + FunctionDefLibrary flib_def; + FunctionDef func = FunctionDefHelper::Create( + /*function_name=*/name, /*in_def=*/{}, /*out_def=*/{"out: float"}, + /*attr_def*/ + {}, /*node_def=*/{FunctionDefHelper::Const("one", 1.0f)}, + /*ret_def=*/{{"out", "out:output:0"}}); + *flib_def.add_function() = std::move(func); + return flib_def; +} + +Node* MakeCall(Graph* graph, const std::string& callee_name, + const std::string& node_name, absl::Status* status) { + NodeDef call_node; + call_node.set_name(node_name); + call_node.set_op(callee_name); + return graph->AddNode(call_node, status); +} + +TEST(ResourceOperationSafetyAnalysisTest, CallRead) { + Scope root = Scope::NewRootScope().ExitOnError(); + + FunctionDefLibrary flib_def = + CreateFunctionDefLibWithConstFunction("Const_func"); + TF_ASSERT_OK(root.graph()->AddFunctionLibrary(flib_def)); + + Node* read = MakeRead(root, "R"); + absl::Status status; + Node* call = MakeCall(root.graph(), "Const_func", "C", &status); + TF_ASSERT_OK(status); + + root.graph()->AddControlEdge(call, read); + + std::vector> incompatible_pairs; + TF_ASSERT_OK(ComputeIncompatiblePairs(root.graph(), &incompatible_pairs)); + + ASSERT_EQ(incompatible_pairs.size(), 1); + std::pair call_read_edge = {call->id(), read->id()}; + EXPECT_EQ(incompatible_pairs[0], call_read_edge); +} + +TEST(ResourceOperationSafetyAnalysisTest, ReadCall) { + Scope root = Scope::NewRootScope().ExitOnError(); + + FunctionDefLibrary flib_def = + CreateFunctionDefLibWithConstFunction("Const_func"); + TF_ASSERT_OK(root.graph()->AddFunctionLibrary(flib_def)); + + Node* read = MakeRead(root, "R"); + absl::Status status; + Node* call = MakeCall(root.graph(), "Const_func", "C", &status); + TF_ASSERT_OK(status); + + root.graph()->AddControlEdge(read, call); + + std::vector> incompatible_pairs; + TF_ASSERT_OK(ComputeIncompatiblePairs(root.graph(), &incompatible_pairs)); + + EXPECT_EQ(incompatible_pairs.size(), 0); +} + +TEST(ResourceOperationSafetyAnalysisTest, CallWrite) { + Scope root = Scope::NewRootScope().ExitOnError(); + + FunctionDefLibrary flib_def = + CreateFunctionDefLibWithConstFunction("Const_func"); + TF_ASSERT_OK(root.graph()->AddFunctionLibrary(flib_def)); + + Node* write = MakeWrite(root, "W"); + absl::Status status; + Node* call = MakeCall(root.graph(), "Const_func", "C", &status); + TF_ASSERT_OK(status); + + root.graph()->AddControlEdge(call, write); + + std::vector> incompatible_pairs; + TF_ASSERT_OK(ComputeIncompatiblePairs(root.graph(), &incompatible_pairs)); + + EXPECT_EQ(incompatible_pairs.size(), 0); +} + +TEST(ResourceOperationSafetyAnalysisTest, WriteCall) { + Scope root = Scope::NewRootScope().ExitOnError(); + + FunctionDefLibrary flib_def = + CreateFunctionDefLibWithConstFunction("Const_func"); + TF_ASSERT_OK(root.graph()->AddFunctionLibrary(flib_def)); + + Node* write = MakeWrite(root, "W"); + absl::Status status; + Node* call = MakeCall(root.graph(), "Const_func", "C", &status); + TF_ASSERT_OK(status); + + root.graph()->AddControlEdge(write, call); + + std::vector> incompatible_pairs; + TF_ASSERT_OK(ComputeIncompatiblePairs(root.graph(), &incompatible_pairs)); + + ASSERT_EQ(incompatible_pairs.size(), 1); + std::pair write_call_edge = {write->id(), call->id()}; + EXPECT_EQ(incompatible_pairs[0], write_call_edge); +} + +TEST(ResourceOperationSafetyAnalysisTest, SymbolicGradientRead) { + Scope root = Scope::NewRootScope().ExitOnError(); + + FunctionDefLibrary flib_def = + CreateFunctionDefLibWithConstFunction("Const_func"); + TF_ASSERT_OK(root.graph()->AddFunctionLibrary(flib_def)); + + Node* read = MakeRead(root, "R"); + NameAttrList fn; + fn.set_name("Const_func"); + Node* symbolic_gradient = + ops::SymbolicGradient(root, /*input=*/{ops::Const(root, 1.0f)}, + /*Tout=*/{DT_FLOAT}, fn) + .output[0] + .node(); + + root.graph()->AddControlEdge(symbolic_gradient, read); + + std::vector> incompatible_pairs; + TF_ASSERT_OK(ComputeIncompatiblePairs(root.graph(), &incompatible_pairs)); + + ASSERT_EQ(incompatible_pairs.size(), 1); + std::pair symbolic_gradient_read_edge = {symbolic_gradient->id(), + read->id()}; + EXPECT_EQ(incompatible_pairs[0], symbolic_gradient_read_edge); +} + +TEST(ResourceOperationSafetyAnalysisTest, WriteSymbolicGradient) { + Scope root = Scope::NewRootScope().ExitOnError(); + + FunctionDefLibrary flib_def = + CreateFunctionDefLibWithConstFunction("Const_func"); + TF_ASSERT_OK(root.graph()->AddFunctionLibrary(flib_def)); + + Node* write = MakeWrite(root, "W"); + NameAttrList fn; + fn.set_name("Const_func"); + Node* symbolic_gradient = + ops::SymbolicGradient(root, /*input=*/{ops::Const(root, 1.0f)}, + /*Tout=*/{DT_FLOAT}, fn) + .output[0] + .node(); + + root.graph()->AddControlEdge(write, symbolic_gradient); + + std::vector> incompatible_pairs; + TF_ASSERT_OK(ComputeIncompatiblePairs(root.graph(), &incompatible_pairs)); + + ASSERT_EQ(incompatible_pairs.size(), 1); + std::pair write_symbolic_gradient_edge = {write->id(), + symbolic_gradient->id()}; + EXPECT_EQ(incompatible_pairs[0], write_symbolic_gradient_edge); +} + +TEST(ResourceOperationSafetyAnalysisTest, ChainOfOps) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Node* write_0 = MakeWrite(root, "W0"); + Node* neutral_0 = MakeNeutral(root, "N0"); + Node* read_0 = MakeRead(root, "R0"); + Node* write_1 = MakeWrite(root, "W1"); + Node* neutral_1 = MakeNeutral(root, "N1"); + Node* read_1 = MakeRead(root, "R1"); + + root.graph()->AddControlEdge(write_0, neutral_0); + root.graph()->AddControlEdge(neutral_0, read_0); + root.graph()->AddControlEdge(read_0, write_1); + root.graph()->AddControlEdge(write_1, neutral_1); + root.graph()->AddControlEdge(neutral_1, read_1); + + std::vector> incompatible_pairs; + TF_ASSERT_OK(ComputeIncompatiblePairs(root.graph(), &incompatible_pairs)); + + ASSERT_EQ(incompatible_pairs.size(), 3); + std::pair write_0_read_0_pair = {write_0->id(), read_0->id()}; + std::pair write_0_read_1_pair = {write_0->id(), read_1->id()}; + std::pair write_1_read_1_pair = {write_1->id(), read_1->id()}; + + EXPECT_EQ(incompatible_pairs[0], write_0_read_0_pair); + EXPECT_EQ(incompatible_pairs[1], write_0_read_1_pair); + EXPECT_EQ(incompatible_pairs[2], write_1_read_1_pair); +} + +TEST(ResourceOperationSafetyAnalysisTest, DagOfOps) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Node* write_0 = MakeWrite(root, "W0"); + Node* write_1 = MakeWrite(root, "W1"); + Node* neutral = MakeNeutral(root, "N"); + Node* read_0 = MakeRead(root, "R0"); + Node* read_1 = MakeRead(root, "R1"); + + root.graph()->AddControlEdge(write_0, neutral); + root.graph()->AddControlEdge(write_1, neutral); + root.graph()->AddControlEdge(neutral, read_0); + root.graph()->AddControlEdge(neutral, read_1); + + std::vector> incompatible_pairs; + TF_ASSERT_OK(ComputeIncompatiblePairs(root.graph(), &incompatible_pairs)); + + ASSERT_EQ(incompatible_pairs.size(), 4); + std::pair write_0_read_0_pair = {write_0->id(), read_0->id()}; + std::pair write_0_read_1_pair = {write_0->id(), read_1->id()}; + std::pair write_1_read_0_pair = {write_1->id(), read_0->id()}; + std::pair write_1_read_1_pair = {write_1->id(), read_1->id()}; + + EXPECT_EQ(incompatible_pairs[0], write_0_read_0_pair); + EXPECT_EQ(incompatible_pairs[1], write_0_read_1_pair); + EXPECT_EQ(incompatible_pairs[2], write_1_read_0_pair); + EXPECT_EQ(incompatible_pairs[3], write_1_read_1_pair); +} + +TEST(ResourceOperationSafetyAnalysisTest, DagOfOpsWithRepeatedPaths) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Node* write_0 = MakeWrite(root, "W0"); + Node* write_1 = MakeWrite(root, "W1"); + Node* neutral = MakeNeutral(root, "N"); + Node* read_0 = MakeRead(root, "R0"); + Node* read_1 = MakeRead(root, "R1"); + + root.graph()->AddControlEdge(write_0, neutral); + root.graph()->AddControlEdge(write_1, neutral); + root.graph()->AddControlEdge(neutral, read_0); + root.graph()->AddControlEdge(neutral, read_1); + root.graph()->AddControlEdge(write_1, read_1); + + std::vector> incompatible_pairs; + TF_ASSERT_OK(ComputeIncompatiblePairs(root.graph(), &incompatible_pairs)); + + ASSERT_EQ(incompatible_pairs.size(), 4); + std::pair write_0_read_0_pair = {write_0->id(), read_0->id()}; + std::pair write_0_read_1_pair = {write_0->id(), read_1->id()}; + std::pair write_1_read_0_pair = {write_1->id(), read_0->id()}; + std::pair write_1_read_1_pair = {write_1->id(), read_1->id()}; + + EXPECT_EQ(incompatible_pairs[0], write_0_read_0_pair); + EXPECT_EQ(incompatible_pairs[1], write_0_read_1_pair); + EXPECT_EQ(incompatible_pairs[2], write_1_read_0_pair); + EXPECT_EQ(incompatible_pairs[3], write_1_read_1_pair); +} + +TEST(ResourceOperationSafetyAnalysisTest, Loop) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Output init_value = ops::Placeholder(root.WithOpName("init"), DT_FLOAT); + Output loop_cond = ops::Placeholder(root.WithOpName("init"), DT_BOOL); + Output enter_value = + ops::internal::Enter(root.WithOpName("enter"), init_value, "fr"); + ops::Merge iv(root.WithOpName("iv"), {enter_value, enter_value}); + ops::Switch latch(root.WithOpName("latch"), iv.output, loop_cond); + ops::internal::Exit exit(root.WithOpName("exit"), iv.output); + Output next_iteration = + ops::NextIteration(root.WithOpName("next_iteration"), latch.output_true); + TF_ASSERT_OK( + root.graph()->UpdateEdge(next_iteration.node(), 0, iv.output.node(), 1)); + + Node* write = MakeWrite(root, "W"); + Node* read = MakeRead(root, "R"); + + root.graph()->AddControlEdge(iv.output.node(), write); + root.graph()->AddControlEdge(write, read); + root.graph()->AddControlEdge(read, next_iteration.node()); + + std::vector> incompatible_pairs; + TF_ASSERT_OK(ComputeIncompatiblePairs(root.graph(), &incompatible_pairs)); + + ASSERT_EQ(incompatible_pairs.size(), 1); + + std::pair write_read_pair = {write->id(), read->id()}; + EXPECT_EQ(incompatible_pairs[0], write_read_pair); +} + +bool IsResourceArgDef(const OpDef::ArgDef& arg_def) { + return arg_def.type() == DT_RESOURCE; +} +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/shape_inference.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/shape_inference.cc new file mode 100644 index 0000000000000000000000000000000000000000..753602d95644c0a1139a7c3695aab27e130399ba --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/shape_inference.cc @@ -0,0 +1,317 @@ +/* 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 "tensorflow/compiler/jit/shape_inference.h" + +#include +#include +#include + +#include "absl/log/log.h" +#include "absl/strings/str_cat.h" +#include "tensorflow/compiler/jit/shape_inference_helpers.h" +#include "tensorflow/core/common_runtime/shape_refiner.h" +#include "tensorflow/core/framework/function.h" +#include "tensorflow/core/framework/node_def_util.h" +#include "tensorflow/core/framework/shape_inference.h" +#include "tensorflow/core/framework/tensor.pb.h" +#include "tensorflow/core/framework/tensor_shape.h" +#include "tensorflow/core/framework/tensor_shape.pb.h" +#include "tensorflow/core/framework/types.h" +#include "tensorflow/core/graph/algorithm.h" +#include "tensorflow/core/graph/graph.h" +#include "tensorflow/core/platform/errors.h" +#include "tensorflow/core/platform/status.h" +#include "tsl/platform/errors.h" +#include "tsl/platform/statusor.h" + +namespace tensorflow { + +namespace { + +// Converts a shape inference handle to a PartialTensorShape. +absl::Status ShapeHandleToTensorShape( + shape_inference::InferenceContext* context, + const shape_inference::ShapeHandle& handle, PartialTensorShape* shape) { + // The default is already unknown + if (!context->RankKnown(handle)) return absl::OkStatus(); + + std::vector dims(context->Rank(handle)); + for (int32_t i = 0, end = dims.size(); i < end; ++i) { + dims[i] = context->Value(context->Dim(handle, i)); + } + return PartialTensorShape::MakePartialShape(dims.data(), dims.size(), shape); +} + +absl::Status PropagateShapes( + Graph* graph, const std::map& arg_shapes, + const std::vector& back_edges, + ShapeRefiner* shape_refiner) { + std::map merge_to_next_iteration; + for (const auto& e : back_edges) { + if (e.src->IsNextIteration() && e.dst->IsMerge()) { + merge_to_next_iteration[e.dst] = e.src; + } + } + + // Visits the nodes in topological order (reverse post-order), inferring + // shapes. + // TODO(phawkins): handle cyclic graphs. + std::vector order; + GetReversePostOrder(*graph, &order); + + for (Node* n : order) { + VLOG(4) << "Propagating shape for node " << n->name() + << ", type: " << n->type_string(); + // Ignore the status returned by the shape_refiner. We want the best effort + // shapes, even if no shape function is registered for a node. + absl::Status status = shape_refiner->AddNode(n); + if (!status.ok()) { + VLOG(1) << "Shape inference failed for node " << n->name() << ": " + << status; + } else { + shape_inference::InferenceContext* context = shape_refiner->GetContext(n); + for (int i = 0; i < n->num_outputs(); i++) { + shape_inference::ShapeHandle handle = context->output(i); + VLOG(4) << "Output " << i << " for node " << n->name() << ": " + << context->DebugString(handle); + } + } + + int index = -1; + if (n->type_string() == "_Arg") { + // NOTE: during runtime, Placeholder ops will be replaced as `_Arg` ops. + // And Args must have `index` attribute. + TF_RETURN_IF_ERROR(GetNodeAttr(n->attrs(), "index", &index)); + } else if (n->type_string() == "Placeholder") { + // Use custom attribute (prefixed with `_`) `_index` for placeholders as + // they come from user specifications. + if (const auto s = GetNodeAttr(n->attrs(), "_index", &index); !s.ok()) { + VLOG(1) << "Failed to get node index for node " << n->name(); + } + } + if (index >= 0) { + if (auto it = arg_shapes.find(index); it != arg_shapes.end()) { + const InferredShape& arg_shape = it->second; + shape_inference::InferenceContext* context = + shape_refiner->GetContext(n); + + if (arg_shape.handle_type != DT_INVALID) { + shape_inference::ShapeHandle handle; + TF_RETURN_IF_ERROR(context->MakeShapeFromPartialTensorShape( + arg_shape.handle_shape, &handle)); + + // Sets the shape and type of the variable's value. + context->set_output_handle_shapes_and_types( + 0, std::vector{ + {handle, arg_shape.handle_type}}); + } + + shape_inference::ShapeHandle handle; + TF_RETURN_IF_ERROR( + context->MakeShapeFromPartialTensorShape(arg_shape.shape, &handle)); + TF_RETURN_IF_ERROR(shape_refiner->SetShape(n, 0, handle)); + } + } + + // Sometimes we have VariableShape nodes in while loop (after Enter nodes). + // They won't be constant-folded because TensorFlow constant folding does + // not handle Enter nodes (and thus does not handle any nodes after Enter + // nodes). We try to replace such VariableShape nodes with Const nodes here. + if (n->type_string() == "VariableShape") { + shape_inference::InferenceContext* context = shape_refiner->GetContext(n); + auto handle_shapes_and_types = context->input_handle_shapes_and_types(0); + if (handle_shapes_and_types && !handle_shapes_and_types->empty()) { + shape_inference::ShapeHandle handle = + handle_shapes_and_types->at(0).shape; + TensorShapeProto shape_proto; + context->ShapeHandleToProto(handle, &shape_proto); + if (!shape_proto.unknown_rank()) { + NodeDef const_def; + const_def.set_op("Const"); + Node* var_node; + TF_RETURN_IF_ERROR(n->input_node(0, &var_node)); + const_def.set_name( + graph->NewName(absl::StrCat("var_shape_", var_node->name()))); + DataType dtype = n->output_type(0); + AddNodeAttr("dtype", dtype, &const_def); + TensorProto value; + value.set_dtype(dtype); + value.mutable_tensor_shape()->add_dim()->set_size( + shape_proto.dim_size()); + for (const auto& dim : shape_proto.dim()) { + if (dtype == DT_INT32) { + value.add_int_val(dim.size()); + } else { + value.add_int64_val(dim.size()); + } + } + AddNodeAttr("value", value, &const_def); + for (auto const& attr : n->attrs()) { + if (*attr.first.begin() == '_') { + AddNodeAttr(attr.first, attr.second, &const_def); + } + } + + TF_ASSIGN_OR_RETURN(Node * const_node, graph->AddNode(const_def)); + graph->AddControlEdge(var_node, const_node); + std::vector out_edges(n->out_edges().begin(), + n->out_edges().end()); + for (const Edge* e : out_edges) { + if (e->IsControlEdge()) { + graph->AddControlEdge(const_node, e->dst()); + graph->RemoveEdge(e); + } else { + Node* dst = e->dst(); + int dst_input = e->dst_input(); + graph->RemoveEdge(e); + graph->AddEdge(const_node, 0, dst, dst_input); + } + } + } + } + } + + // Merge node causes a loop so we remove NextIteration->Merge edge before + // performing shape inference. But removing those edges also prevents us + // from inferring output shape for Merge node (we need shapes for all its + // inputs). + // For loop invariant resource input's Merge node, we set output resource + // shape as Enter node's resource shape. + // TODO(b/129367850): clean this up. + if (n->IsMerge() && n->output_type(0) == DT_RESOURCE) { + // Check if this is a loop invariant input's Merge node. We do it by + // checking if corresponding NextIteration node comes from Switch node + // directly. + auto iter = merge_to_next_iteration.find(n); + if (iter != merge_to_next_iteration.end()) { + const Node *next_iter = iter->second, *node = next_iter; + do { + TF_RETURN_IF_ERROR(node->input_node(0, &node)); + } while (node->IsIdentity()); + const Node* switch_input; + bool is_loop_invariant = node->IsSwitch() && + node->input_node(0, &switch_input).ok() && + switch_input == n; + if (is_loop_invariant) { + shape_inference::InferenceContext* context = + shape_refiner->GetContext(n); + for (int i = 0; i < n->num_inputs(); i++) { + const Node* input_node; + if (n->input_node(i, &input_node).ok()) { + auto shapes_and_types = context->input_handle_shapes_and_types(i); + if (shapes_and_types) { + context->set_output_handle_shapes_and_types(0, + *shapes_and_types); + } + break; + } + } + } + } + } + } + return absl::OkStatus(); +} + +// Store the shapes of the output tensors in a map +absl::Status StoreOutputShapes(const Graph& graph, + const ShapeRefiner& shape_refiner, + GraphShapeInfo* shape_info) { + for (const Node* node : graph.nodes()) { + shape_inference::InferenceContext* context = shape_refiner.GetContext(node); + if (!context) continue; + + auto& outputs = (*shape_info)[node->name()]; + outputs.resize(context->num_outputs()); + for (int i = 0; i < context->num_outputs(); ++i) { + auto& output = outputs[i]; + TF_RETURN_IF_ERROR( + ShapeHandleToTensorShape(context, context->output(i), &output.shape)); + + const auto* handle_shapes_and_types = + context->output_handle_shapes_and_types(i); + if (handle_shapes_and_types != nullptr) { + if (handle_shapes_and_types->size() == 1) { + TF_RETURN_IF_ERROR(ShapeHandleToTensorShape( + context, (*handle_shapes_and_types)[0].shape, + &output.handle_shape)); + output.handle_type = (*handle_shapes_and_types)[0].dtype; + } else { + // otherwise, it may be resource like a Queue, which can have + // multiple shapes and types represented by a single handle. + } + } + VLOG(4) << node->name() << " output " << i << " shape" + << output.shape.DebugString() << " handle_type " + << DataTypeString(output.handle_type) << " handle_shape " + << output.handle_shape.DebugString(); + } + } + return absl::OkStatus(); +} + +} // namespace + +absl::Status InferShapes(Graph* graph, + const std::map& arg_shapes, + const tensorflow::FunctionLibraryDefinition* fnlib_def, + GraphShapeInfo* shape_info) { + ShapeRefiner shape_refiner(graph->versions(), graph->op_registry()); + shape_refiner.set_require_shape_inference_fns(false); + // TODO(dlibenzi): Verify if it is worth trying to infer shaped within + // functions. Some functions can be called at multiple locations with + // difference shapes, which will trigger a shape inference based on the + // arguments passed at the first call. + // shape_refiner.set_function_library_for_shape_inference(fnlib_def); + + // ShapeRefiner requires that all inputs of a node are present when + // ShapeRefiner::AddNode is called. To get at least some shape information in + // loops, we temporarily remove loop backedges and add them back again after + // the shape inference is complete. + BackEdgeHelper back_edge; + TF_RETURN_IF_ERROR(back_edge.Remove(graph)); + TF_RETURN_IF_ERROR(PropagateShapes(graph, arg_shapes, + back_edge.RemovedEdges(), &shape_refiner)); + TF_RETURN_IF_ERROR(back_edge.Replace()); + + // Currently information does not flow "backward" from consumers to producers + // in the shape inference, but we consume the shapes in a second pass in case + // backward information flow is added in the future. + return StoreOutputShapes(*graph, shape_refiner, shape_info); +} + +absl::StatusOr MergeInferredShapes(const InferredShape& a, + const InferredShape& b) { + InferredShape result; + TF_RETURN_IF_ERROR(a.shape.MergeWith(b.shape, &result.shape)); + + if (a.handle_type == DT_INVALID) { + result.handle_type = b.handle_type; + } else if (b.handle_type == DT_INVALID) { + result.handle_type = a.handle_type; + } else if (a.handle_type == b.handle_type) { + result.handle_type = a.handle_type; + } else { + return absl::InvalidArgumentError(absl::StrCat( + "Mismatched resource types: ", DataTypeString(a.handle_type), " vs. ", + DataTypeString(b.handle_type))); + } + TF_RETURN_IF_ERROR( + a.handle_shape.MergeWith(b.handle_shape, &result.handle_shape)); + return result; +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/shape_inference.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/shape_inference.h new file mode 100644 index 0000000000000000000000000000000000000000..5342ad4b708d5f9e607e4b608d241ca68c5b8c62 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/shape_inference.h @@ -0,0 +1,59 @@ +/* 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_SHAPE_INFERENCE_H_ +#define TENSORFLOW_COMPILER_JIT_SHAPE_INFERENCE_H_ + +#include +#include + +#include "absl/status/statusor.h" +#include "tensorflow/core/common_runtime/optimization_registry.h" +#include "tensorflow/core/common_runtime/shape_refiner.h" +#include "tensorflow/core/lib/core/status.h" + +namespace tensorflow { + +struct InferredShape { + // Shape of the argument tensor. + PartialTensorShape shape; + + // If the argument is a resource variable, the type and shape of the + // variable's value. + DataType handle_type = DT_INVALID; + PartialTensorShape handle_shape; +}; +typedef std::unordered_map> + GraphShapeInfo; + +// Infer shapes for all Tensors in a graph, and save them in a map. The vector +// for a Node contains the information about each of its outputs. +// TODO(phawkins): this code does not infer accurate shapes for cyclic graphs. +// `arg_shapes`: user given map from the `index` to shapes of this +// node, where `index` is the `index` attribute of `_Arg` op or `_index` +// attribute of `Placeholder` op. +absl::Status InferShapes(Graph* graph, + const std::map& arg_shapes, + const tensorflow::FunctionLibraryDefinition* fnlib_def, + GraphShapeInfo* shape_info); + +// Merges two InferredShapes. Return an error if the two shapes cannot be +// merged. +absl::StatusOr MergeInferredShapes(const InferredShape& a, + const InferredShape& b); + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_SHAPE_INFERENCE_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/shape_inference_helpers.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/shape_inference_helpers.cc new file mode 100644 index 0000000000000000000000000000000000000000..37bf9c30d993864c3428927f4d20fcced4465a3b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/shape_inference_helpers.cc @@ -0,0 +1,66 @@ +/* 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. +==============================================================================*/ + +// Contains helpers for use in shape inference. + +#include "tensorflow/compiler/jit/shape_inference_helpers.h" + +#include + +#include "tensorflow/core/graph/graph.h" + +namespace tensorflow { + +absl::Status BackEdgeHelper::Remove(Graph* graph) { + if (graph_ != nullptr) { + return absl::InternalError("BackEdgeHelper duplicate call to Remove."); + } + graph_ = graph; + for (Node* n : graph_->nodes()) { + if (n->IsMerge()) { + for (const Edge* e : n->in_edges()) { + if (e->src()->IsNextIteration()) { + back_edges_.push_back( + BackEdge{e, e->src(), e->src_output(), e->dst(), e->dst_input()}); + } + } + } + } + for (const BackEdge& be : back_edges_) { + graph_->RemoveEdge(be.edge); + } + return absl::OkStatus(); +} + +const std::vector& BackEdgeHelper::RemovedEdges() + const { + return back_edges_; +} + +absl::Status BackEdgeHelper::Replace() { + if (graph_ == nullptr) { + return absl::InternalError("BackEdgeHelper Replace called before Remove."); + } + if (replaced_) { + return absl::InternalError("BackEdgeHelper Replace called more than once."); + } + replaced_ = true; + for (const BackEdge& be : back_edges_) { + graph_->AddEdge(be.src, be.src_output, be.dst, be.dst_input); + } + return absl::OkStatus(); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/shape_inference_helpers.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/shape_inference_helpers.h new file mode 100644 index 0000000000000000000000000000000000000000..48acc345085e9d1b8a1fb74be233c49f89047b46 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/shape_inference_helpers.h @@ -0,0 +1,65 @@ +/* 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_SHAPE_INFERENCE_HELPERS_H_ +#define TENSORFLOW_COMPILER_JIT_SHAPE_INFERENCE_HELPERS_H_ + +#include + +#include "tensorflow/core/graph/graph.h" + +namespace tensorflow { + +// Helper class to temporarily remove, then replace, the back edges in a +// graph. Simple algorithms for shape inference don't work with cycles, and this +// class can be used to remove cycles before running inference and replace them +// after. Correct usage requires exactly one call to Remove(), followed by any +// number of calls to RemovedEdges() and at most one call to Replace(). The call +// to Replace() is optional if the graph will be discarded without being +// executed, e.g., if it is being used purely for a shape inference pass. +class BackEdgeHelper { + public: + struct BackEdge { + const Edge* edge; + Node* src; + int src_output; + Node* dst; + int dst_input; + }; + + BackEdgeHelper() = default; + // Disallows copy and assign. + BackEdgeHelper(const BackEdgeHelper& other) = delete; + BackEdgeHelper& operator=(const BackEdgeHelper& other) = delete; + + // Temporarily removes all the back edges in graph. + absl::Status Remove(Graph* graph); + + // Gets the list of removed edges. + const std::vector& RemovedEdges() const; + + // Replaces the back edges removed by a prior call to Remove. + absl::Status Replace(); + + private: + Graph* graph_ = nullptr; // not owned + std::vector back_edges_; + // Set once Replace has been called. + bool replaced_ = false; +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_SHAPE_INFERENCE_HELPERS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/shape_inference_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/shape_inference_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..b25ecd980142198c48750d6b170452d70c20cd9a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/shape_inference_test.cc @@ -0,0 +1,250 @@ +/* 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. +==============================================================================*/ + +// Tests for ShapeInference. + +#include "tensorflow/compiler/jit/shape_inference.h" + +#include +#include +#include +#include +#include +#include + +#include "absl/log/check.h" +#include "tensorflow/cc/framework/ops.h" +#include "tensorflow/cc/framework/scope.h" +#include "tensorflow/cc/ops/array_ops.h" +#include "tensorflow/cc/ops/const_op.h" +#include "tensorflow/cc/ops/control_flow_ops.h" +#include "tensorflow/cc/ops/control_flow_ops_internal.h" +#include "tensorflow/cc/ops/math_ops.h" +#include "tensorflow/cc/ops/resource_variable_ops.h" +#include "tensorflow/compiler/jit/test_util.h" +#include "xla/tsl/lib/core/status_test_util.h" +#include "tensorflow/core/framework/op.h" +#include "tensorflow/core/framework/tensor_shape.h" +#include "tensorflow/core/graph/graph.h" +#include "tensorflow/core/platform/test.h" + +namespace tensorflow { +namespace { + +TEST(ShapeInferenceTest, Basics) { + Scope root = Scope::NewRootScope().ExitOnError(); + auto a = ops::Placeholder(root.WithOpName("A"), DT_FLOAT, + ops::Placeholder::Shape({2, 3})); + auto b = ops::Placeholder(root.WithOpName("B"), DT_FLOAT, + ops::Placeholder::Shape({3})); + auto c = ops::Placeholder(root.WithOpName("C"), DT_FLOAT); + auto d = ops::Add(root.WithOpName("D"), a, b); + auto e = ops::Add(root.WithOpName("E"), d, c); + auto f = ops::Neg(root.WithOpName("F"), e); + auto g = ops::AddN(root.WithOpName("G"), std::initializer_list{e, f}); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + CHECK_OK(root.ToGraph(graph.get())); + + GraphShapeInfo shape_info; + TF_ASSERT_OK(InferShapes(graph.get(), /*arg_shapes=*/{}, + /*fnlib_def=*/nullptr, &shape_info)); + + std::map> expected = { + {"A", {PartialTensorShape({2, 3})}}, {"B", {PartialTensorShape({3})}}, + {"C", {PartialTensorShape()}}, {"D", {PartialTensorShape({2, 3})}}, + {"E", {PartialTensorShape()}}, {"F", {PartialTensorShape()}}, + {"G", {PartialTensorShape()}}, + }; + TF_EXPECT_OK(ShapeAnnotationsMatch(*graph, shape_info, expected)); +} + +// Test that shape inference uses user-given `arg_shapes` to propagate shapes. +TEST(ShapeInferenceTest, UseArgShapesForVariableBatchSize) { + Scope root = Scope::NewRootScope().ExitOnError(); + auto a = ops::Placeholder(root.WithOpName("A"), DT_FLOAT, + ops::Placeholder::Shape({-1, 3})); + auto b = ops::Placeholder(root.WithOpName("B"), DT_FLOAT, + ops::Placeholder::Shape({-1, 3})); + auto c = ops::Add(root.WithOpName("C"), a, b); + auto d = ops::Neg(root.WithOpName("D"), c); + + a.node()->AddAttr("_index", 0); + b.node()->AddAttr("_index", 1); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + CHECK_OK(root.ToGraph(graph.get())); + + std::map arg_shapes; + arg_shapes[0].shape = TensorShape({2, 3}); + arg_shapes[1].shape = TensorShape({2, 3}); + + GraphShapeInfo shape_info; + TF_ASSERT_OK(InferShapes(graph.get(), arg_shapes, + /*fnlib_def=*/nullptr, &shape_info)); + + std::map> expected = { + {"A", {PartialTensorShape({2, 3})}}, + {"B", {PartialTensorShape({2, 3})}}, + {"C", {PartialTensorShape({2, 3})}}, + {"D", {PartialTensorShape({2, 3})}}, + }; + TF_EXPECT_OK(ShapeAnnotationsMatch(*graph, shape_info, expected)); +} + +// Test that when user-given `arg_shapes` is incomplete (to cover all +// Placeholders), shape inference still succeeds. +TEST(ShapeInferenceTest, UseArgShapesForVariableBatchSizeIncompleteUserArgs) { + Scope root = Scope::NewRootScope().ExitOnError(); + auto a = ops::Placeholder(root.WithOpName("A"), DT_FLOAT, + ops::Placeholder::Shape({-1, 3})); + auto b = ops::Placeholder(root.WithOpName("B"), DT_FLOAT, + ops::Placeholder::Shape({-1, 3})); + auto c = ops::Add(root.WithOpName("C"), a, b); + auto d = ops::Neg(root.WithOpName("D"), c); + + a.node()->AddAttr("_index", 0); + b.node()->AddAttr("_index", 0); + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + CHECK_OK(root.ToGraph(graph.get())); + + std::map arg_shapes; + arg_shapes[0].shape = TensorShape({2, 3}); + + GraphShapeInfo shape_info; + TF_ASSERT_OK(InferShapes(graph.get(), arg_shapes, + /*fnlib_def=*/nullptr, &shape_info)); + + std::map> expected = { + {"A", {PartialTensorShape({2, 3})}}, + {"B", {PartialTensorShape({2, 3})}}, + {"C", {PartialTensorShape({2, 3})}}, + {"D", {PartialTensorShape({2, 3})}}, + }; + TF_EXPECT_OK(ShapeAnnotationsMatch(*graph, shape_info, expected)); +} + +TEST(ShapeInferenceTest, WhileLoop) { + // Graph: + // x = array_ops.placeholder(dtypes.int32) + // y = control_flow_ops.while_loop(lambda i: i < 10, lambda i: i + 1, [x]) + Graph graph(OpRegistry::Global()); + { + Scope scope = Scope::NewRootScope().ExitOnError(); + + auto dummy = ops::Placeholder(scope.WithOpName("Dummy"), DT_INT32, + ops::Placeholder::Shape({})); + + auto source = ops::Placeholder(scope.WithOpName("source"), DT_INT32, + ops::Placeholder::Shape({})); + auto enter = + ops::internal::Enter(scope.WithOpName("while/Enter"), source, "aloop"); + // Add an unused Enter node. These should be ignored. + auto enter2 = + ops::internal::Enter(scope.WithOpName("while/Enter2"), source, "aloop"); + auto merge = ops::Merge(scope.WithOpName("while/Merge"), + std::initializer_list{enter, dummy}); + auto ten = ops::Const( + scope.WithOpName("while/Less/y").WithControlDependencies(merge.output), + 10); + auto less = ops::Less(scope.WithOpName("while/Less"), merge.output, ten); + auto loop_cond = ops::LoopCond(scope.WithOpName("while/LoopCond"), less); + auto switch_node = + ops::Switch(scope.WithOpName("while/Switch"), merge.output, loop_cond); + auto exit = ops::internal::Exit(scope.WithOpName("while/Exit"), + switch_node.output_false); + auto identity = ops::Identity(scope.WithOpName("while/Identity"), + switch_node.output_true); + auto identity_shape = + ops::Const(scope.WithOpName("while/Identity/shape"), {}); + auto identity_reshaped = ops::Reshape( + scope.WithOpName("while/Identity/reshaped"), identity, identity_shape); + + auto one = ops::Const( + scope.WithOpName("while/add/y").WithControlDependencies(identity), 1); + auto add = ops::Add(scope.WithOpName("while/add"), identity_reshaped, one); + auto next_iteration = + ops::NextIteration(scope.WithOpName("while/NextIteration"), add); + + auto sink = ops::Identity(scope.WithOpName("sink"), exit); + + // Remove the dummy node and add the loop backedge. + scope.graph()->RemoveNode(dummy.node()); + scope.graph()->AddEdge(next_iteration.node(), 0, merge.output.node(), 1); + + TF_EXPECT_OK(scope.ToGraph(&graph)); + } + + GraphShapeInfo shape_info; + TF_ASSERT_OK(InferShapes(&graph, /*arg_shapes=*/{}, /*fnlib_def=*/nullptr, + &shape_info)); + std::map> expected = { + {"while/Identity", {PartialTensorShape()}}, + {"while/add", {PartialTensorShape({})}}, + }; + TF_EXPECT_OK(ShapeAnnotationsMatch(graph, shape_info, expected)); +} + +TEST(ShapeInferenceTest, WhileLoopWithResource) { + // Graph: + // x = resource_variable_ops.var_handle_op(dtype=dtypes.float32, shape=[2, 3]) + // y = control_flow_ops.while_loop(lambda _: true, lambda x: x, [x]) + Graph graph(OpRegistry::Global()); + { + Scope scope = Scope::NewRootScope().ExitOnError(); + + auto x = + ops::VarHandleOp(scope.WithOpName("x"), DT_FLOAT, TensorShape({2, 3})); + auto enter = + ops::internal::Enter(scope.WithOpName("while/Enter"), x, "aloop"); + auto dummy = ops::Placeholder(scope.WithOpName("dummy"), DT_RESOURCE); + auto merge = ops::Merge(scope.WithOpName("while/Merge"), + std::initializer_list{enter, dummy}); + auto false_value = ops::Const(scope.WithOpName("false"), false); + auto loop_cond = + ops::LoopCond(scope.WithOpName("while/LoopCond"), false_value); + auto switch_node = + ops::Switch(scope.WithOpName("while/Switch"), merge.output, loop_cond); + auto exit = ops::internal::Exit(scope.WithOpName("while/Exit"), + switch_node.output_false); + auto identity = ops::Identity(scope.WithOpName("while/Identity"), + switch_node.output_true); + auto next_iteration = + ops::NextIteration(scope.WithOpName("while/NextIteration"), identity); + auto sink = ops::Identity(scope.WithOpName("sink"), exit); + + // Remove the dummy node and add the loop backedge. + scope.graph()->RemoveNode(dummy.node()); + scope.graph()->AddEdge(next_iteration.node(), 0, merge.output.node(), 1); + + TF_EXPECT_OK(scope.ToGraph(&graph)); + } + + // Check that we can infer shape for "sink" node (Merge node output). + GraphShapeInfo shape_info; + TF_ASSERT_OK(InferShapes(&graph, /*arg_shapes=*/{}, /*fnlib_def=*/nullptr, + &shape_info)); + auto iter = shape_info.find("sink"); + EXPECT_NE(iter, shape_info.end()); + EXPECT_EQ(iter->second.size(), 1); + EXPECT_EQ(iter->second.at(0).handle_type, DT_FLOAT); + TensorShape resource_shape; + EXPECT_TRUE(iter->second.at(0).handle_shape.AsTensorShape(&resource_shape)); + EXPECT_EQ(resource_shape, TensorShape({2, 3})); +} + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/test_util.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/test_util.cc new file mode 100644 index 0000000000000000000000000000000000000000..65f4f3d192df3d439c5bced324c78d5698ce995e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/test_util.cc @@ -0,0 +1,117 @@ +/* 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 "tensorflow/compiler/jit/test_util.h" + +#include +#include +#include +#include +#include +#include + +#include "absl/log/check.h" +#include "absl/status/status.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/str_join.h" +#include "tensorflow/compiler/jit/shape_inference.h" +#include "xla/status_macros.h" +#include "tensorflow/core/common_runtime/device_mgr.h" +#include "tensorflow/core/common_runtime/graph_runner.h" +#include "tensorflow/core/common_runtime/process_function_library_runtime.h" +#include "tensorflow/core/framework/device_factory.h" +#include "tensorflow/core/framework/function.h" +#include "tensorflow/core/framework/op.h" +#include "tensorflow/core/framework/tensor_shape.h" +#include "tensorflow/core/graph/graph.h" +#include "tensorflow/core/platform/errors.h" +#include "tensorflow/core/public/version.h" + +namespace tensorflow { + +absl::Status ShapeAnnotationsMatch( + const Graph& graph, const GraphShapeInfo& shape_info, + std::map> expected_shapes) { + for (Node* node : graph.op_nodes()) { + auto sit = shape_info.find(node->name()); + TF_RET_CHECK(sit != shape_info.end()) + << "Missing shape information for node " << node->name(); + std::vector shapes; + for (const auto& output : sit->second) shapes.push_back(output.shape); + + auto it = expected_shapes.find(node->name()); + if (it != expected_shapes.end()) { + if (!PartialTensorShapeUtils::AreIdentical(shapes, it->second)) { + return absl::InvalidArgumentError(absl::StrCat( + "Shape mismatch for ", node->name(), ". Expected: ", + PartialTensorShapeUtils::PartialShapeListString(it->second), + ", actual: ", + PartialTensorShapeUtils::PartialShapeListString(shapes))); + } + expected_shapes.erase(it); + } + } + if (!expected_shapes.empty()) { + std::vector missing; + missing.reserve(expected_shapes.size()); + for (const auto& entry : expected_shapes) { + missing.push_back(entry.first); + } + return absl::InvalidArgumentError(absl::StrCat( + "Missing shapes for nodes: ", absl::StrJoin(missing, ","))); + } + return absl::OkStatus(); +} + +void DeviceSetup::AddDevicesAndSetUp( + const std::vector& device_names, + const std::optional& fdef) { + SessionOptions options; + auto* device_count = options.config.mutable_device_count(); + for (const auto& device_name : device_names) { + device_count->insert({device_name, 1}); + } + + std::vector> devices; + CHECK_OK(DeviceFactory::AddDevices(options, "/job:localhost/replica:0/task:0", + &devices)); + device_mgr_ = std::make_unique(std::move(devices)); + + OptimizerOptions opts; + lib_def_ = std::make_unique(OpRegistry::Global(), + FunctionDefLibrary()); + if (fdef.has_value()) { + CHECK_OK(lib_def_->AddFunctionDef(*fdef)); + } + pflr_ = std::make_unique( + device_mgr_.get(), Env::Default(), /*config=*/nullptr, + TF_GRAPH_DEF_VERSION, lib_def_.get(), opts, + /*default_thread_pool=*/nullptr, /*cluster_flr=*/nullptr); + flr_ = pflr_->GetFLR("/job:localhost/replica:0/task:0/cpu:0"); +} + +Device* DeviceSetup::GetDevice(const std::string& device_name) { + if (device_mgr_ == nullptr) { + return nullptr; + } + + std::string full_device_name = absl::StrCat( + "/job:localhost/replica:0/task:0/device:", device_name, ":0"); + Device* device; + CHECK_OK(device_mgr_->LookupDevice(full_device_name, &device)); + return device; +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/test_util.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/test_util.h new file mode 100644 index 0000000000000000000000000000000000000000..f3c4c67e67a3d4cb59ac2391c3f628925ee031c4 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/test_util.h @@ -0,0 +1,89 @@ +/* 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. +==============================================================================*/ + +// Helper functions for tests. + +#ifndef TENSORFLOW_COMPILER_JIT_TEST_UTIL_H_ +#define TENSORFLOW_COMPILER_JIT_TEST_UTIL_H_ + +#include +#include +#include +#include +#include +#include + +#include "tensorflow/compiler/jit/shape_inference.h" +#include "tensorflow/core/common_runtime/device_mgr.h" +#include "tensorflow/core/common_runtime/optimization_registry.h" +#include "tensorflow/core/common_runtime/process_function_library_runtime.h" +#include "tensorflow/core/framework/function.h" +#include "tensorflow/core/framework/op.h" +#include "tensorflow/core/framework/partial_tensor_shape.h" +#include "tensorflow/core/graph/graph.h" +#include "tensorflow/core/lib/core/status.h" +#include "tensorflow/core/public/session_options.h" + +namespace tensorflow { + +// Tests that the shapes in 'shape_info' for the nodes in `graph` match +// `expected_shapes`. Returns an error if there are nodes in `expected_shapes` +// that do not have shape information. Ignores nodes in `graph` that do not have +// `expected_shapes` entries. +absl::Status ShapeAnnotationsMatch( + const Graph& graph, const GraphShapeInfo& shape_info, + std::map> expected_shapes); + +// A helper object to create GraphOptimizationPassOptions. +struct GraphOptimizationPassWrapper { + explicit GraphOptimizationPassWrapper() + : library(OpRegistry::Global(), FunctionDefLibrary()) { + session_options.env = Env::Default(); + } + + // Create GraphOptimizationPassOptions with a graph passed in constructor and + // sensible options. + GraphOptimizationPassOptions CreateGraphOptimizationPassOptions( + std::unique_ptr* graph) { + GraphOptimizationPassOptions options; + options.session_options = &session_options; + options.flib_def = &library; + options.graph = graph; + return options; + } + + FunctionLibraryDefinition library; + SessionOptions session_options; +}; + +// Helps set up devices for unit tests. +class DeviceSetup { + public: + void AddDevicesAndSetUp( + const std::vector& device_names, + const std::optional& fdef = std::nullopt); + Device* GetDevice(const std::string& device_name); + FunctionLibraryRuntime* flr() { return flr_; } + + private: + FunctionLibraryRuntime* flr_; + std::unique_ptr device_mgr_; + std::unique_ptr lib_def_; + std::unique_ptr pflr_; +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_TEST_UTIL_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..72ca38233018c2dcdbeccef3ea72e147735d5345 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/BUILD @@ -0,0 +1,126 @@ +load("//tensorflow:tensorflow.bzl", "tf_cc_test") +load("//tensorflow/core/platform:rules_cc.bzl", "cc_library") + +licenses(["notice"]) + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:license"], + default_visibility = ["//visibility:private"], +) + +cc_library( + name = "auto_clustering_test_helper", + testonly = True, + srcs = ["auto_clustering_test_helper.cc"], + hdrs = ["auto_clustering_test_helper.h"], + visibility = ["//visibility:public"], + deps = [ + "//tensorflow/compiler/jit:compilation_passes", + "//tensorflow/compiler/jit:jit_compilation_passes", + "//tensorflow/compiler/jit:xla_cluster_util", + "//tensorflow/compiler/jit:xla_cpu_jit", + "//tensorflow/compiler/jit:xla_gpu_jit", + "//tensorflow/core:core_cpu", + "//tensorflow/core:framework", + "//tensorflow/core:lib", + "//tensorflow/core:lib_internal", + "//tensorflow/core:test", + "//tensorflow/core:test_main", + "//tensorflow/tools/optimization:optimization_pass_runner_lib", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@xla//xla:status_macros", + ], +) + +tf_cc_test( + name = "auto_clustering_test", + srcs = ["auto_clustering_test.cc"], + data = [ + "keras_imagenet_main.golden_summary", + "keras_imagenet_main.pbtxt", + "keras_imagenet_main_graph_mode.golden_summary", + "keras_imagenet_main_graph_mode.pbtxt", + "opens2s_gnmt_mixed_precision.golden_summary", + "opens2s_gnmt_mixed_precision.pbtxt.gz", + ], + tags = ["config-cuda-only"], + deps = [ + ":auto_clustering_test_helper", + "//tensorflow/core:test", + "@com_google_absl//absl/log:check", + "@com_google_absl//absl/status", + "@com_google_absl//absl/strings", + ], +) + +cc_library( + name = "device_compiler_test_helper", + testonly = True, + srcs = ["device_compiler_test_helper.cc"], + hdrs = ["device_compiler_test_helper.h"], + visibility = [ + "//tensorflow/compiler/jit:__pkg__", + ], + deps = [ + "//tensorflow/compiler/jit:xla_activity_listener", + "//tensorflow/compiler/jit:xla_compilation_cache_proto_cc", + "//tensorflow/compiler/jit:xla_cpu_jit", + "//tensorflow/compiler/jit:xla_gpu_device", + "//tensorflow/compiler/jit:xla_gpu_jit", + "//tensorflow/core:all_kernels", + "//tensorflow/core:core_cpu", + "//tensorflow/core:core_cpu_internal", + "//tensorflow/core:direct_session", + "//tensorflow/core:framework_internal", + "//tensorflow/core:lib", + "//tensorflow/core:lib_internal", + "//tensorflow/core:ops", + "//tensorflow/core:test", + "//tensorflow/core/platform:path", + "@com_google_absl//absl/status", + "@com_google_absl//absl/strings", + "@xla//xla/service:hlo_proto_cc", + ], +) + +tf_cc_test( + name = "device_compiler_serialize_test", + srcs = [ + "device_compiler_serialize_test.cc", + ], + tags = [ + "config-cuda-only", + "no_oss", # This test only runs with GPU. + "requires-gpu-nvidia", + "xla", + ], + deps = [ + ":device_compiler_test_helper", + "//tensorflow/compiler/jit:compilation_passes", + "//tensorflow/compiler/jit:flags", + "//tensorflow/core:test", + ], +) + +tf_cc_test( + name = "device_compiler_serialize_options_test", + srcs = [ + "device_compiler_serialize_options_test.cc", + ], + tags = [ + "config-cuda-only", + "no_oss", # This test only runs with GPU. + "requires-gpu-nvidia", + "xla", + ], + deps = [ + ":device_compiler_test_helper", + "//tensorflow/compiler/jit:compilation_passes", + "//tensorflow/compiler/jit:flags", + "//tensorflow/core:test", + "//tensorflow/core/framework:graph_proto_cc", + "@com_google_absl//absl/strings", + "@com_google_googletest//:gtest", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/auto_clustering_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/auto_clustering_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..52e1230b9bff3d98f0e6447a07bdd6e307646a05 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/auto_clustering_test.cc @@ -0,0 +1,101 @@ +/* Copyright 2019 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 "absl/log/check.h" +#include "absl/status/status.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" +#include "tensorflow/compiler/jit/tests/auto_clustering_test_helper.h" +#include "xla/tsl/lib/core/status_test_util.h" +#include "tensorflow/core/platform/test.h" + +namespace tensorflow { +namespace { +class AutoClusteringTestImpl : public AutoClusteringTest { + protected: + // Test auto-clustering with a proto text file ${key}.pbtxt. + absl::Status RunAutoClusteringTestWithPbtxt(absl::string_view key) { + std::string file_name_without_extension = + absl::StrCat(testing::TensorFlowSrcRoot(), "/compiler/jit/tests/", key); + + return AutoClusteringTest::RunAutoClusteringTestWithPbtxt( + absl::StrCat(file_name_without_extension, ".pbtxt"), + absl::StrCat(file_name_without_extension, ".golden_summary")); + } + + // Test auto-clustering with a gzipped proto text file ${key}.pbtxt.gz. + absl::Status RunAutoClusteringTestWithGzippedPbtxt(absl::string_view key) { + std::string file_name_without_extension = + absl::StrCat(testing::TensorFlowSrcRoot(), "/compiler/jit/tests/", key); + + return AutoClusteringTest::RunAutoClusteringTestWithGzippedPbtxt( + absl::StrCat(file_name_without_extension, ".pbtxt.gz"), + absl::StrCat(file_name_without_extension, ".golden_summary")); + } +}; + +TEST_F(AutoClusteringTestImpl, KerasImagenetMain) { + // Generated from + // + // TARGET_PATH=tensorflow_models/official/legacy/image_classification \ + // bazel run -c opt --config=cuda ${TARGET_PATH}:resnet_imagenet_main \ + // -- --skip_eval --num_gpus=1 --dtype=fp16 --batch_size=192 \ + // --train_steps=210 --enable_xla --enable_eager=true + // + // At CL 245846452 + TF_ASSERT_OK(RunAutoClusteringTestWithPbtxt("keras_imagenet_main")); +} + +TEST_F(AutoClusteringTestImpl, KerasImagenetMainGraphMode) { + // Generated from + // + // TARGET_PATH=tensorflow_models/official/legacy/image_classification \ + // bazel run -c opt --config=cuda ${TARGET_PATH}:resnet_imagenet_main \ + // -- --use_synthetic_data --num_gpus=1 --batch_size=117 --train_steps=600 \ + // --skip_eval=True --logtostderr --enable_xla + TF_ASSERT_OK( + RunAutoClusteringTestWithPbtxt("keras_imagenet_main_graph_mode")); +} + +TEST_F(AutoClusteringTestImpl, OpenSeq2SeqGNMT) { + // Model is from https://github.com/NVIDIA/OpenSeq2Seq. + // Generated from + // + // python run.py \ + // --config_file=example_configs/text2text/en-de/en-de-gnmt-like-4GPUs.py \ + // --use_xla_jit + TF_ASSERT_OK( + RunAutoClusteringTestWithGzippedPbtxt("opens2s_gnmt_mixed_precision")); +} + +#if defined(PLATFORM_GOOGLE) +absl::Status BenchmarkHelper(absl::string_view key, benchmark::State& state) { + return BenchmarkMarkForCompilation( + absl::StrCat(testing::TensorFlowSrcRoot(), "/compiler/jit/tests/", key, + ".pbtxt"), + state); +} + +void BM_MarkForCompilationPass_KerasImagenetMain(benchmark::State& state) { + CHECK_OK(BenchmarkHelper("keras_imagenet_main", state)); +} + +BENCHMARK(BM_MarkForCompilationPass_KerasImagenetMain); +#endif // PLATFORM_GOOGLE + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/auto_clustering_test_helper.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/auto_clustering_test_helper.cc new file mode 100644 index 0000000000000000000000000000000000000000..3237d47d13fb3fd5beb24bdca6b28a14b236a58c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/auto_clustering_test_helper.cc @@ -0,0 +1,234 @@ +/* Copyright 2019 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/compiler/jit/tests/auto_clustering_test_helper.h" + +#include + +#include "absl/status/statusor.h" +#include "absl/strings/numbers.h" +#include "tensorflow/compiler/jit/mark_for_compilation_pass.h" +#include "tensorflow/compiler/jit/xla_cluster_util.h" +#include "xla/status_macros.h" +#include "tensorflow/core/common_runtime/graph_constructor.h" +#include "tensorflow/core/lib/core/status_test_util.h" +#include "tensorflow/core/lib/io/random_inputstream.h" +#include "tensorflow/core/lib/io/zlib_compression_options.h" +#include "tensorflow/core/lib/io/zlib_inputstream.h" +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/platform/test.h" +#include "tensorflow/core/platform/test_benchmark.h" +#include "tensorflow/core/util/port.h" +#include "tensorflow/tools/optimization/optimization_pass_runner.h" + +namespace tensorflow { +namespace { +absl::StatusOr SummarizeClustering( + const GraphDef& auto_clustered_graph_def) { + testing::ResetClusterSequenceNumber(); + Graph graph(OpRegistry::Global()); + GraphConstructorOptions graph_opts; + graph_opts.expect_device_spec = true; + graph_opts.allow_internal_ops = true; + TF_RETURN_IF_ERROR( + ConvertGraphDefToGraph(graph_opts, auto_clustered_graph_def, &graph)); + + // cluster_id -> (operation name -> # of operations) + const int kNoCluster = -1; + std::map> clusters; + std::map cluster_size; + int clustered_nodes = 0; + for (Node* n : graph.op_nodes()) { + int cluster = kNoCluster; + if (std::optional maybe_cluster = + GetXlaClusterForNode(*n)) { + maybe_cluster->remove_prefix(absl::string_view("cluster_").size()); + TF_RET_CHECK(absl::SimpleAtoi(*maybe_cluster, &cluster)); + clustered_nodes++; + } + clusters[cluster][n->type_string()]++; + cluster_size[cluster]++; + } + + std::string result = + absl::StrCat("Clustered nodes: ", clustered_nodes, + "\nUnclustered nodes: ", cluster_size[kNoCluster], + "\nNumber of clusters: ", clusters.size() - 1, "\n\n"); + for (const auto& pair : clusters) { + if (pair.first == kNoCluster) { + absl::StrAppend(&result, "unclustered"); + } else { + absl::StrAppend(&result, "cluster ", pair.first); + } + + absl::StrAppend(&result, " size ", cluster_size[pair.first], "\n"); + + for (const auto& ops_and_counts : pair.second) { + absl::StrAppend(&result, " ", ops_and_counts.first, " ", + ops_and_counts.second, "\n"); + } + } + + return result; +} + +absl::Status AssertGraphDefIsUnclustered(const GraphDef& graphdef) { + const char* kXlaClusterAttr = "_XlaCluster"; + const char* kXlaAlreadyClusteredAttr = "_XlaAlreadyClustered"; + + for (const NodeDef& node : graphdef.node()) { + if (node.attr().count(kXlaClusterAttr) || + node.attr().count(kXlaAlreadyClusteredAttr)) { + return absl::InvalidArgumentError( + "Input files are already clustered, you probably copied in " + "mark_for_compilation_.pbtxt when you should have copied in " + "before_mark_for_compilation_.pbtxt"); + } + } + + return absl::OkStatus(); +} + +absl::Status ReadTextProtoFromString(Env* env, const std::string& data, + ::tensorflow::protobuf::Message* proto) { + if (!::tensorflow::protobuf::TextFormat::ParseFromString(data, proto)) { + return absl::DataLossError("Can't parse input data as text proto"); + } + return absl::OkStatus(); +} +} // namespace + +absl::Status AutoClusteringTest::RunAutoClusteringTestImpl( + GraphDef graphdef, absl::string_view golden_summary_file_path) { + if (!IsGoogleCudaEnabled()) { + // There is some slight change in the clustering decisions under + // --config=cuda. I have not looked closely at why that is happening, but + // most likely some of the partial declustering passes behave differently + // with --config=cuda because of different HostMemory. So for now only test + // the non-CUDA config, under the assumption that regressions with + // --config=cuda would also be detected as regressions without + // --config=cuda. + + LOG(INFO) << "Not running " + << ::testing::UnitTest::GetInstance()->current_test_info()->name() + << " since test was not built with --config=cuda"; + return absl::OkStatus(); + } + + TF_RETURN_IF_ERROR(AssertGraphDefIsUnclustered(graphdef)); + + OptimizationPassRunner runner; + TF_RETURN_IF_ERROR(runner.SetJitLevel(tensorflow::OptimizerOptions::ON_2)); + TF_RETURN_IF_ERROR(runner.AddCpus(32)); + TF_RETURN_IF_ERROR(runner.AddGpus(8)); + + for (absl::string_view auto_clustering_pass : + {"CloneConstantsForBetterClusteringPass", "MarkForCompilationPass", + "IncreaseDynamismForAutoJitPass", "PartiallyDeclusterPass"}) { + GraphDef next; + TF_RETURN_IF_ERROR( + runner.Run(auto_clustering_pass, std::move(graphdef), &next)); + graphdef = std::move(next); + } + + TF_ASSIGN_OR_RETURN(std::string clustering_summary, + SummarizeClustering(graphdef)); + + // To update golden files flip this to true and run + // + // bazel test --test_strategy=local \ + // tensorflow/compiler/jit/tests:auto_clustering_test + bool update_golden = false; + if (update_golden) { + TF_RETURN_IF_ERROR(WriteStringToFile(Env::Default(), + std::string(golden_summary_file_path), + clustering_summary)); + } + + std::string golden_file_contents; + TF_RETURN_IF_ERROR(ReadFileToString(Env::Default(), + std::string(golden_summary_file_path), + &golden_file_contents)); + + EXPECT_EQ(golden_file_contents, clustering_summary); + + return absl::OkStatus(); +} + +absl::Status AutoClusteringTest::RunAutoClusteringTestWithPbtxt( + absl::string_view pbtxt_file_path, + absl::string_view golden_summary_file_path) { + GraphDef graphdef; + TF_RETURN_IF_ERROR( + ReadTextProto(Env::Default(), std::string(pbtxt_file_path), &graphdef)); + return RunAutoClusteringTestImpl(std::move(graphdef), + golden_summary_file_path); +} + +absl::Status AutoClusteringTest::RunAutoClusteringTestWithGzippedPbtxt( + absl::string_view gzipped_pbtxt_file_path, + absl::string_view golden_summary_file_path) { + Env* env = Env::Default(); + std::unique_ptr file_reader; + TF_RETURN_IF_ERROR(env->NewRandomAccessFile( + std::string(gzipped_pbtxt_file_path), &file_reader)); + std::unique_ptr input_stream = + std::make_unique(file_reader.get()); + constexpr int k_buffer_size = 256 << 10; // 256kb + io::ZlibInputStream in(input_stream.get(), + /*input_buffer_bytes=*/k_buffer_size, + /*output_buffer_bytes=*/k_buffer_size, + io::ZlibCompressionOptions::GZIP()); + tstring decompressed_pbtxt_string; + absl::Status s = in.ReadNBytes(INT_MAX, &decompressed_pbtxt_string); + if (!s.ok() && !absl::IsOutOfRange(s)) { + // OutOfRange is fine since we set the number of read bytes to INT_MAX. + // Only return other kinds of errors. + return s; + } + + GraphDef graphdef; + TF_RETURN_IF_ERROR(ReadTextProtoFromString( + Env::Default(), decompressed_pbtxt_string, &graphdef)); + return RunAutoClusteringTestImpl(std::move(graphdef), + golden_summary_file_path); +} + +#if defined(PLATFORM_GOOGLE) +absl::Status BenchmarkMarkForCompilation(absl::string_view graph_def_path, + benchmark::State& state) { + GraphDef graph_def; + TF_RETURN_IF_ERROR( + ReadTextProto(Env::Default(), std::string(graph_def_path), &graph_def)); + + OptimizationPassRunner runner; + TF_RETURN_IF_ERROR(runner.SetJitLevel(tensorflow::OptimizerOptions::ON_2)); + TF_RETURN_IF_ERROR(runner.AddCpus(32)); + TF_RETURN_IF_ERROR(runner.AddGpus(8)); + + for (auto _ : state) { + state.PauseTiming(); + GraphDef result; + GraphDef graph_def_copy = graph_def; + state.ResumeTiming(); + TF_RETURN_IF_ERROR(runner.Run("MarkForCompilationPass", + std::move(graph_def_copy), &result)); + } + + return absl::OkStatus(); +} +#endif // PLATFORM_GOOGLE + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/auto_clustering_test_helper.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/auto_clustering_test_helper.h new file mode 100644 index 0000000000000000000000000000000000000000..2d0bc83d7b223a0d84dcfc0eb94badabffa40190 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/auto_clustering_test_helper.h @@ -0,0 +1,71 @@ +/* Copyright 2019 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_JIT_TESTS_AUTO_CLUSTERING_TEST_HELPER_H_ +#define TENSORFLOW_COMPILER_JIT_TESTS_AUTO_CLUSTERING_TEST_HELPER_H_ + +#include "absl/status/statusor.h" +#include "tensorflow/core/common_runtime/graph_constructor.h" +#include "tensorflow/core/platform/test.h" +#include "tensorflow/core/platform/test_benchmark.h" + +namespace tensorflow { +// Helper to write integration tests and benchmarks for the auto-clustering pass +// pipeline. These tests run auto-clustering on a graphdef and compare a +// summary of the auto-clustering decisions with a "golden" summary. +// +// To create a new test from an TF workload first run the workload with the +// following environment variables set: +// +// TF_DUMP_GRAPH_PREFIX= +// TF_XLA_FLAGS="--tf_xla_clustering_debug" +// +// If auto-clustering is enabled this should produce files named +// before_mark_for_compilation_.pbtxt in the temporary directory. As the +// file name suggests, these are graphdefs that have been dumped right before +// the mark_for_compilation pass. There should be one +// before_mark_for_compilation_.pbtxt for every TF graph that was +// auto-clustered, out of which usually only one is the "main" graph that's +// running training/inference. +// +// Copy the pbtxt for that "main" graph to tensorflow/compiler/jit/tests/ +// (i.e. this directory) and create a corresponding empty .golden_summary file. +// Add the .pbtxt and .golden_summary files to the "data" section of the cc_test +// rule for :auto_clustering_test and then see the comment on update_golden on +// how to auto-generate the .golden_summary file. + +class AutoClusteringTest : public ::testing::Test { + protected: + absl::Status RunAutoClusteringTestWithPbtxt( + absl::string_view pbtxt_file_path, + absl::string_view golden_summary_file_path); + absl::Status RunAutoClusteringTestWithGzippedPbtxt( + absl::string_view gzipped_pbtxt_file_path, + absl::string_view golden_summary_file_path); + + private: + absl::Status RunAutoClusteringTestImpl( + GraphDef graphdef, absl::string_view golden_summary_file_path); +}; + +#if defined(PLATFORM_GOOGLE) +// Reads the GraphDef stored in graph_def_path (which must be a pbtxt file) and +// benchmarks MarkForCompilationPass on this graphdef. +absl::Status BenchmarkMarkForCompilation(absl::string_view graph_def_path, + benchmark::State& state); +#endif // PLATFORM_GOOGLE + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_TESTS_AUTO_CLUSTERING_TEST_HELPER_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/device_compiler_serialize_options_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/device_compiler_serialize_options_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..f75eaf9546c6dacdea7d55843dddda5517e5fe78 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/device_compiler_serialize_options_test.cc @@ -0,0 +1,78 @@ +/* Copyright 2022 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 "absl/strings/match.h" +#include "tensorflow/compiler/jit/flags.h" +#include "tensorflow/compiler/jit/mark_for_compilation_pass.h" +#include "tensorflow/compiler/jit/tests/device_compiler_test_helper.h" +#include "tensorflow/core/framework/graph.pb.h" +#include "tensorflow/core/lib/core/status_test_util.h" + +namespace tensorflow { +namespace { + +TEST_F(DeviceCompilerSerializeTest, PersistentCacheOptionsTest) { + GraphDef graph = GetTestGraph({-1, 4}); + + // Warmup the persistent cache(s) with multiple runs. 4 is a magic number to + // detect non-determinism in TF when running the test. + listener()->ClearListenerHistory(); + for (int b = 1; b < 4; ++b) { + TF_ASSERT_OK(ExecuteWithBatch(graph, b)); + } + TF_ASSERT_OK(listener()->VerifyPersistentCacheUseListenerHistory( + /*expect_persistent_cache_use=*/false)); + + // Reset the cluster numbering between sessions so we can get the same + // cluster numbering. + testing::ResetClusterSequenceNumber(); + + auto status = + AlterPersistentCacheEntryHloModuleNames(tensorflow::testing::TmpDir()); + EXPECT_FALSE(status.ok()); + EXPECT_TRUE(absl::StrContains( + status.message(), + "Did not find any persistent XLA compilation cache entries to alter.")); + + TF_ASSERT_OK(AlterPersistentCacheEntryHloModuleNames( + tensorflow::testing::TmpDir(), "my_test_prefix")); + + // Run again and these should all hit in the persistent cache despite having + // altered the persistent cache entries' HLO modules (disabled strict + // signature checks). + listener()->ClearListenerHistory(); + for (int b = 1; b < 4; ++b) { + TF_ASSERT_OK(ExecuteWithBatch(graph, b)); + } + TF_ASSERT_OK(listener()->VerifyPersistentCacheUseListenerHistory( + /*expect_persistent_cache_use=*/true)); +} + +} // namespace +} // namespace tensorflow + +int main(int argc, char** argv) { + tensorflow::GetMarkForCompilationPassFlags() + ->tf_xla_deterministic_cluster_names = true; + tensorflow::GetMarkForCompilationPassFlags() + ->tf_xla_persistent_cache_directory = tensorflow::testing::TmpDir(); + tensorflow::GetMarkForCompilationPassFlags() + ->tf_xla_disable_strict_signature_checks = true; + tensorflow::GetMarkForCompilationPassFlags()->tf_xla_persistent_cache_prefix = + "my_test_prefix"; + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/device_compiler_serialize_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/device_compiler_serialize_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..1cc53e670614e9b02e3646d3c3ccc3b89ceacd85 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/device_compiler_serialize_test.cc @@ -0,0 +1,75 @@ +/* Copyright 2022 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/compiler/jit/flags.h" +#include "tensorflow/compiler/jit/mark_for_compilation_pass.h" +#include "tensorflow/compiler/jit/tests/device_compiler_test_helper.h" +#include "tensorflow/core/lib/core/status_test_util.h" + +namespace tensorflow { +namespace { + +TEST_F(DeviceCompilerSerializeTest, PersistentCacheTest) { + GraphDef graph = GetTestGraph({-1, 4}); + + // Warmup the persistent cache(s) with multiple runs. 4 is a magic number to + // detect non-determinism in TF when running the test. + listener()->ClearListenerHistory(); + for (int b = 1; b < 4; ++b) { + TF_ASSERT_OK(ExecuteWithBatch(graph, b)); + } + TF_ASSERT_OK(listener()->VerifyPersistentCacheUseListenerHistory( + /*expect_persistent_cache_use=*/false)); + + // Reset the cluster numbering between sessions so we can get the same + // cluster numbering. + testing::ResetClusterSequenceNumber(); + + // Run again but these should all hit in the persistent cache. + listener()->ClearListenerHistory(); + for (int b = 1; b < 4; ++b) { + TF_ASSERT_OK(ExecuteWithBatch(graph, b)); + } + TF_ASSERT_OK(listener()->VerifyPersistentCacheUseListenerHistory( + /*expect_persistent_cache_use=*/true)); + + // Reset the cluster numbering between sessions so we can get the same + // cluster numbering. + testing::ResetClusterSequenceNumber(); + + TF_ASSERT_OK( + AlterPersistentCacheEntryHloModuleNames(tensorflow::testing::TmpDir())); + + // Run again but these should all fail, because the persistent cache entries' + // HLO modules have been altered. + for (int b = 1; b < 4; ++b) { + auto status = ExecuteWithBatch(graph, b); + EXPECT_FALSE(status.ok()); + EXPECT_TRUE( + absl::StrContains(status.message(), "Serialized HLO does not match.")); + } +} + +} // namespace +} // namespace tensorflow + +int main(int argc, char** argv) { + tensorflow::GetMarkForCompilationPassFlags() + ->tf_xla_deterministic_cluster_names = true; + tensorflow::GetMarkForCompilationPassFlags() + ->tf_xla_persistent_cache_directory = tensorflow::testing::TmpDir(); + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/device_compiler_test_helper.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/device_compiler_test_helper.cc new file mode 100644 index 0000000000000000000000000000000000000000..f490ac911260ef9d69df5a5614873d1f93cba9d1 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/device_compiler_test_helper.cc @@ -0,0 +1,167 @@ +/* Copyright 2022 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/compiler/jit/tests/device_compiler_test_helper.h" + +#include + +#include "absl/strings/match.h" +#include "tensorflow/compiler/jit/xla_compilation_cache.pb.h" +#include "xla/service/hlo.pb.h" +#include "tensorflow/core/platform/path.h" +#include "tensorflow/core/public/session.h" + +namespace tensorflow { +namespace { + +// Creates a float tensor of linearly increasing values, starting from offset. +Tensor CreateInputTensor(const TensorShape& shape, float offset) { + Tensor tensor(DT_FLOAT, shape); + for (int64_t i = 0; i < tensor.flat().size(); ++i) { + tensor.flat()(i) = offset + i; + } + return tensor; +} + +NodeDef MakeNode( + absl::string_view name, absl::string_view op, + absl::Span inputs, + absl::Span< + const std::pair> + attrs) { + NodeDef node; + node.set_name(std::string(name)); + node.set_op(std::string(op)); + for (const auto& input : inputs) node.add_input(input); + for (const auto& attr : attrs) + node.mutable_attr()->insert({attr.first, attr.second.proto}); + return node; +} + +} // namespace + +GraphDef DeviceCompilerSerializeTest::GetTestGraph( + const PartialTensorShape& input_shape) { + FunctionDef make_test_fn = FunctionDefHelper::Define( + "TestFn", {"a:float", "b:float", "c:float"}, {"m:float"}, {}, + {{{"d"}, "Add", {"a", "b"}, {{"T", DT_FLOAT}}}, + {{"e"}, "Mul", {"d", "c"}, {{"T", DT_FLOAT}}}, + {{"f"}, "Add", {"e", "a"}, {{"T", DT_FLOAT}}}, + {{"g"}, "Mul", {"f", "b"}, {{"T", DT_FLOAT}}}, + // Force two clusters by excluding this node explicitly. + {{"h"}, "Add", {"g", "f"}, {{"T", DT_FLOAT}, {"_XlaCompile", false}}}, + {{"i"}, "Add", {"h", "e"}, {{"T", DT_FLOAT}}}, + {{"j"}, "Add", {"i", "h"}, {{"T", DT_FLOAT}}}, + {{"k"}, "Add", {"j", "h"}, {{"T", DT_FLOAT}}}, + {{"l"}, "Add", {"k", "h"}, {{"T", DT_FLOAT}}}, + {{"m"}, "Identity", {"l"}, {{"T", DT_FLOAT}}}}); + + GraphDef graph; + *graph.mutable_library()->add_function() = make_test_fn; + *graph.add_node() = MakeNode("a", "Placeholder", {}, + {{"dtype", DT_FLOAT}, {"shape", input_shape}}); + *graph.add_node() = MakeNode("b", "Placeholder", {}, + {{"dtype", DT_FLOAT}, {"shape", input_shape}}); + *graph.add_node() = MakeNode("c", "Placeholder", {}, + {{"dtype", DT_FLOAT}, {"shape", input_shape}}); + *graph.add_node() = MakeNode("m", "TestFn", {"a", "b", "c"}, {}); + return graph; +} + +absl::Status DeviceCompilerSerializeTest::ExecuteWithBatch( + const GraphDef& graph, int batch) { + const TensorShape shape({batch, 4}); + + // Compute the golden output tensor + std::vector golden_output_tensors; + { + SessionOptions options; + std::unique_ptr session(NewSession(options)); + TF_RETURN_IF_ERROR(session->Create(graph)); + RunOptions run_options; + + Tensor input_a = CreateInputTensor(shape, 0); + Tensor input_b = CreateInputTensor(shape, shape.num_elements()); + Tensor input_c = CreateInputTensor(shape, 2 * shape.num_elements()); + TF_RETURN_IF_ERROR(session->Run( + run_options, + {std::make_pair("a", input_a), std::make_pair("b", input_b), + std::make_pair("c", input_c)}, + {"m"}, {}, &golden_output_tensors, nullptr)); + TF_RETURN_IF_ERROR(session->Close()); + } + + // Compute the XLA compiled output + std::vector output_tensors; + { + SessionOptions options; + auto& opts = + *options.config.mutable_graph_options()->mutable_optimizer_options(); + opts.set_global_jit_level(OptimizerOptions::ON_1); + opts.set_cpu_global_jit(true); + + std::unique_ptr session(NewSession(options)); + TF_RETURN_IF_ERROR(session->Create(graph)); + RunOptions run_options; + Tensor input_a = CreateInputTensor(shape, 0); + Tensor input_b = CreateInputTensor(shape, shape.num_elements()); + Tensor input_c = CreateInputTensor(shape, 2 * shape.num_elements()); + TF_RETURN_IF_ERROR(session->Run( + run_options, + {std::make_pair("a", input_a), std::make_pair("b", input_b), + std::make_pair("c", input_c)}, + {"m"}, {}, &output_tensors, nullptr)); + TF_RETURN_IF_ERROR(session->Close()); + } + + Tensor f32_input(DT_FLOAT, shape); + for (int64_t i = 0; i < f32_input.NumElements(); ++i) { + EXPECT_NEAR(golden_output_tensors[0].flat()(i), + output_tensors[0].flat()(i), 1e-3); + } + return absl::OkStatus(); +} + +absl::Status +DeviceCompilerSerializeTest::AlterPersistentCacheEntryHloModuleNames( + absl::string_view persistent_cache_dir_path, + absl::string_view file_prefix) { + Env* env = Env::Default(); + std::vector file_names; + TF_RETURN_IF_ERROR( + env->GetChildren(tensorflow::testing::TmpDir(), &file_names)); + + bool altered = false; + for (const auto& file_name : file_names) { + if (absl::EndsWith(file_name, ".pb") && + absl::StartsWith(file_name, file_prefix)) { + XlaSerializedCacheEntry entry; + auto file_path = io::JoinPath(persistent_cache_dir_path, file_name); + TF_RETURN_IF_ERROR(ReadTextOrBinaryProto(env, file_path, &entry)); + entry.mutable_hlo_module()->set_name( + absl::StrCat(entry.hlo_module().name(), "_altered")); + TF_RETURN_IF_ERROR(WriteBinaryProto(env, file_path, entry)); + altered = true; + } + } + + if (!altered) { + return absl::NotFoundError( + "Did not find any persistent XLA compilation cache entries to alter."); + } + return absl::OkStatus(); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/device_compiler_test_helper.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/device_compiler_test_helper.h new file mode 100644 index 0000000000000000000000000000000000000000..15b7342a24fadb0db1ee140557d524d8adfdde56 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/device_compiler_test_helper.h @@ -0,0 +1,104 @@ +/* Copyright 2022 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_JIT_TESTS_DEVICE_COMPILER_TEST_HELPER_H_ +#define TENSORFLOW_COMPILER_JIT_TESTS_DEVICE_COMPILER_TEST_HELPER_H_ + +#include +#include +#include + +#include "absl/status/status.h" +#include "absl/strings/string_view.h" +#include "tensorflow/compiler/jit/xla_activity_listener.h" +#include "tensorflow/core/graph/graph_def_builder.h" +#include "tensorflow/core/platform/errors.h" +#include "tensorflow/core/platform/test.h" + +namespace tensorflow { + +// A listener to inspect the use of XLA's persistent compilation cache entries. +class JitCompilationListener : public XlaActivityListener { + public: + absl::Status Listen( + const XlaAutoClusteringActivity& auto_clustering_activity) override { + return absl::OkStatus(); + } + + absl::Status Listen( + const XlaJitCompilationActivity& jit_compilation_activity) override { + activity_history_.push_back(jit_compilation_activity); + return absl::OkStatus(); + } + + absl::Status Listen( + const XlaOptimizationRemark& optimization_remark) override { + return absl::OkStatus(); + } + + ~JitCompilationListener() override = default; + + absl::Status VerifyPersistentCacheUseListenerHistory( + bool expect_persistent_cache_use) { + for (const auto& activity : activity_history_) { + if (activity.used_persistent_cache() != expect_persistent_cache_use) { + return absl::FailedPreconditionError("Unexpected listener history."); + } + } + return absl::OkStatus(); + } + + std::vector GetListenerHistory() { + return activity_history_; + } + + void ClearListenerHistory() { activity_history_.clear(); } + + private: + std::vector activity_history_; +}; + +// Fixture for testing XLA compilation cache serialization. +class DeviceCompilerSerializeTest : public ::testing::Test { + protected: + DeviceCompilerSerializeTest() { + auto listener = std::make_unique(); + listener_ = listener.get(); + RegisterXlaActivityListener(std::move(listener)); + } + + JitCompilationListener* listener() const { return listener_; } + + // Returns a test graph that will split into two XLA clusters (due to a node + // with _XlaCompile = false). + GraphDef GetTestGraph(const PartialTensorShape& input_shape); + + // Runs the graph using specified batch size both with and without XLA JIT + // compilation. Returns an error if the results between the two do not match. + absl::Status ExecuteWithBatch(const GraphDef& graph, int batch); + + // Adds the suffix "_altered" to the HLO module names of all of the persistent + // XLA compilation cache entries found at the specified directory. If none are + // found, returns NOT_FOUND error. + absl::Status AlterPersistentCacheEntryHloModuleNames( + absl::string_view persistent_cache_dir_path, + absl::string_view file_prefix = "xla_compile_cache"); + + private: + JitCompilationListener* listener_; +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_TESTS_DEVICE_COMPILER_TEST_HELPER_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/keras_imagenet_main.golden_summary b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/keras_imagenet_main.golden_summary new file mode 100644 index 0000000000000000000000000000000000000000..8c9e557de43b03fd65227376ef8e0e9a9c674d98 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/keras_imagenet_main.golden_summary @@ -0,0 +1,60 @@ +Clustered nodes: 2725 +Unclustered nodes: 606 +Number of clusters: 2 + +unclustered size 606 + AssignAddVariableOp 1 + Const 108 + DivNoNan 1 + Identity 2 + Merge 53 + PlaceholderWithDefault 1 + ReadVariableOp 2 + Switch 1 + _Arg 435 + _Retval 2 +cluster 0 size 1910 + Add 16 + AddN 71 + ArgMax 1 + AssignAddVariableOp 1 + BiasAdd 1 + BiasAddGrad 1 + Cast 115 + Const 407 + Conv2D 53 + Conv2DBackpropFilter 53 + Conv2DBackpropInput 52 + Equal 1 + FusedBatchNormGradV2 53 + FusedBatchNormV2 53 + MatMul 3 + MaxPool 1 + MaxPoolGrad 1 + Mean 1 + Mul 218 + Pad 2 + ReadVariableOp 538 + Relu 49 + ReluGrad 49 + Reshape 2 + ResourceApplyKerasMomentum 161 + Slice 1 + Softmax 1 + SparseSoftmaxCrossEntropyWithLogits 1 + Squeeze 1 + Sum 1 + Tile 1 + Transpose 1 +cluster 1 size 815 + AddN 1 + AssignAddVariableOp 1 + AssignSubVariableOp 106 + Const 220 + DivNoNan 1 + Identity 1 + Mul 161 + ReadVariableOp 106 + Square 55 + Sub 106 + Sum 57 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/keras_imagenet_main.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/keras_imagenet_main.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..53982f02bcac7ca93eddee038673154d9922241b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/keras_imagenet_main.pbtxt @@ -0,0 +1,71133 @@ +node { + name: "metrics_2/sparse_categorical_accuracy/Const" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 1 + } + } + int_val: 0 + } + } + } +} +node { + name: "metrics_2/sparse_categorical_accuracy/ArgMax/dimension" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + } + int_val: -1 + } + } + } +} +node { + name: "fc1000_biasadd_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1001 + } + } + } + } + } + attr { + key: "index" + value { + i: 268 + } + } +} +node { + name: "fc1000_matmul_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 2048 + } + dim { + size: 1001 + } + } + } + } + } + attr { + key: "index" + value { + i: 267 + } + } +} +node { + name: "avg_pool_1/Mean/reduction_indices" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 2 + } + } + tensor_content: "\002\000\000\000\003\000\000\000" + } + } + } +} +node { + name: "bn5a_branch1_1/Const_1" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + } + } + } + } + } +} +node { + name: "bn5a_branch1_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 234 + } + } +} +node { + name: "bn5a_branch1_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 233 + } + } +} +node { + name: "res5a_branch1_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 232 + } + } +} +node { + name: "bn4a_branch1_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 139 + } + } +} +node { + name: "bn4a_branch1_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 138 + } + } +} +node { + name: "res4a_branch1_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 137 + } + } +} +node { + name: "bn3a_branch1_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 74 + } + } +} +node { + name: "bn3a_branch1_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 73 + } + } +} +node { + name: "res3a_branch1_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 72 + } + } +} +node { + name: "bn2a_branch1_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 24 + } + } +} +node { + name: "bn2a_branch1_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 23 + } + } +} +node { + name: "res2a_branch1_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 64 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 22 + } + } +} +node { + name: "pool1_pad_1/Pad/paddings" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + dim { + size: 2 + } + } + tensor_content: "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\001\000\000\000\001\000\000\000\001\000\000\000\001\000\000\000" + } + } + } +} +node { + name: "bn_conv1_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 4 + } + } +} +node { + name: "bn_conv1_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 3 + } + } +} +node { + name: "conv1_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 7 + } + dim { + size: 7 + } + dim { + size: 3 + } + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 2 + } + } +} +node { + name: "conv1_pad_1/Pad/paddings" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + dim { + size: 2 + } + } + tensor_content: "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\003\000\000\000\003\000\000\000\003\000\000\000\003\000\000\000" + } + } + } +} +node { + name: "transpose_1/transpose/perm" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\000\000\000\000\003\000\000\000\001\000\000\000\002\000\000\000" + } + } + } +} +node { + name: "input_1_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "index" + value { + i: 0 + } + } +} +node { + name: "bn2a_branch2c_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 19 + } + } +} +node { + name: "bn2a_branch2c_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 18 + } + } +} +node { + name: "res2a_branch2c_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 64 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 17 + } + } +} +node { + name: "bn2a_branch2b_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 14 + } + } +} +node { + name: "bn2a_branch2b_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 13 + } + } +} +node { + name: "res2a_branch2b_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 64 + } + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 12 + } + } +} +node { + name: "bn2a_branch2a_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 9 + } + } +} +node { + name: "bn2a_branch2a_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 8 + } + } +} +node { + name: "res2a_branch2a_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 64 + } + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 7 + } + } +} +node { + name: "bn2b_branch2c_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 39 + } + } +} +node { + name: "bn2b_branch2c_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 38 + } + } +} +node { + name: "res2b_branch2c_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 64 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 37 + } + } +} +node { + name: "bn2b_branch2b_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 34 + } + } +} +node { + name: "bn2b_branch2b_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 33 + } + } +} +node { + name: "res2b_branch2b_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 64 + } + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 32 + } + } +} +node { + name: "bn2b_branch2a_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 29 + } + } +} +node { + name: "bn2b_branch2a_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 28 + } + } +} +node { + name: "res2b_branch2a_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 27 + } + } +} +node { + name: "bn2c_branch2c_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 54 + } + } +} +node { + name: "bn2c_branch2c_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 53 + } + } +} +node { + name: "res2c_branch2c_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 64 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 52 + } + } +} +node { + name: "bn2c_branch2b_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 49 + } + } +} +node { + name: "bn2c_branch2b_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 48 + } + } +} +node { + name: "res2c_branch2b_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 64 + } + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 47 + } + } +} +node { + name: "bn2c_branch2a_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 44 + } + } +} +node { + name: "bn2c_branch2a_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 43 + } + } +} +node { + name: "res2c_branch2a_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 42 + } + } +} +node { + name: "bn3a_branch2c_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 69 + } + } +} +node { + name: "bn3a_branch2c_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 68 + } + } +} +node { + name: "res3a_branch2c_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 128 + } + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 67 + } + } +} +node { + name: "bn3a_branch2b_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 64 + } + } +} +node { + name: "bn3a_branch2b_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 63 + } + } +} +node { + name: "res3a_branch2b_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 128 + } + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 62 + } + } +} +node { + name: "bn3a_branch2a_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 59 + } + } +} +node { + name: "bn3a_branch2a_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 58 + } + } +} +node { + name: "res3a_branch2a_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 57 + } + } +} +node { + name: "bn3b_branch2c_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 89 + } + } +} +node { + name: "bn3b_branch2c_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 88 + } + } +} +node { + name: "res3b_branch2c_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 128 + } + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 87 + } + } +} +node { + name: "bn3b_branch2b_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 84 + } + } +} +node { + name: "bn3b_branch2b_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 83 + } + } +} +node { + name: "res3b_branch2b_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 128 + } + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 82 + } + } +} +node { + name: "bn3b_branch2a_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 79 + } + } +} +node { + name: "bn3b_branch2a_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 78 + } + } +} +node { + name: "res3b_branch2a_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 77 + } + } +} +node { + name: "bn3c_branch2c_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 104 + } + } +} +node { + name: "bn3c_branch2c_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 103 + } + } +} +node { + name: "res3c_branch2c_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 128 + } + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 102 + } + } +} +node { + name: "bn3c_branch2b_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 99 + } + } +} +node { + name: "bn3c_branch2b_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 98 + } + } +} +node { + name: "res3c_branch2b_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 128 + } + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 97 + } + } +} +node { + name: "bn3c_branch2a_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 94 + } + } +} +node { + name: "bn3c_branch2a_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 93 + } + } +} +node { + name: "res3c_branch2a_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 92 + } + } +} +node { + name: "bn3d_branch2c_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 119 + } + } +} +node { + name: "bn3d_branch2c_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 118 + } + } +} +node { + name: "res3d_branch2c_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 128 + } + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 117 + } + } +} +node { + name: "bn3d_branch2b_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 114 + } + } +} +node { + name: "bn3d_branch2b_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 113 + } + } +} +node { + name: "res3d_branch2b_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 128 + } + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 112 + } + } +} +node { + name: "bn3d_branch2a_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 109 + } + } +} +node { + name: "bn3d_branch2a_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 108 + } + } +} +node { + name: "res3d_branch2a_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 107 + } + } +} +node { + name: "bn4a_branch2c_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 134 + } + } +} +node { + name: "bn4a_branch2c_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 133 + } + } +} +node { + name: "res4a_branch2c_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 132 + } + } +} +node { + name: "bn4a_branch2b_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 129 + } + } +} +node { + name: "bn4a_branch2b_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 128 + } + } +} +node { + name: "res4a_branch2b_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 256 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 127 + } + } +} +node { + name: "bn4a_branch2a_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 124 + } + } +} +node { + name: "bn4a_branch2a_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 123 + } + } +} +node { + name: "res4a_branch2a_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 122 + } + } +} +node { + name: "bn4b_branch2c_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 154 + } + } +} +node { + name: "bn4b_branch2c_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 153 + } + } +} +node { + name: "res4b_branch2c_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 152 + } + } +} +node { + name: "bn4b_branch2b_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 149 + } + } +} +node { + name: "bn4b_branch2b_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 148 + } + } +} +node { + name: "res4b_branch2b_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 256 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 147 + } + } +} +node { + name: "bn4b_branch2a_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 144 + } + } +} +node { + name: "bn4b_branch2a_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 143 + } + } +} +node { + name: "res4b_branch2a_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 142 + } + } +} +node { + name: "bn4c_branch2c_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 169 + } + } +} +node { + name: "bn4c_branch2c_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 168 + } + } +} +node { + name: "res4c_branch2c_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 167 + } + } +} +node { + name: "bn4c_branch2b_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 164 + } + } +} +node { + name: "bn4c_branch2b_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 163 + } + } +} +node { + name: "res4c_branch2b_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 256 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 162 + } + } +} +node { + name: "bn4c_branch2a_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 159 + } + } +} +node { + name: "bn4c_branch2a_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 158 + } + } +} +node { + name: "res4c_branch2a_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 157 + } + } +} +node { + name: "bn4d_branch2c_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 184 + } + } +} +node { + name: "bn4d_branch2c_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 183 + } + } +} +node { + name: "res4d_branch2c_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 182 + } + } +} +node { + name: "bn4d_branch2b_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 179 + } + } +} +node { + name: "bn4d_branch2b_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 178 + } + } +} +node { + name: "res4d_branch2b_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 256 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 177 + } + } +} +node { + name: "bn4d_branch2a_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 174 + } + } +} +node { + name: "bn4d_branch2a_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 173 + } + } +} +node { + name: "res4d_branch2a_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 172 + } + } +} +node { + name: "bn4e_branch2c_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 199 + } + } +} +node { + name: "bn4e_branch2c_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 198 + } + } +} +node { + name: "res4e_branch2c_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 197 + } + } +} +node { + name: "bn4e_branch2b_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 194 + } + } +} +node { + name: "bn4e_branch2b_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 193 + } + } +} +node { + name: "res4e_branch2b_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 256 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 192 + } + } +} +node { + name: "bn4e_branch2a_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 189 + } + } +} +node { + name: "bn4e_branch2a_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 188 + } + } +} +node { + name: "res4e_branch2a_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 187 + } + } +} +node { + name: "bn4f_branch2c_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 214 + } + } +} +node { + name: "bn4f_branch2c_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 213 + } + } +} +node { + name: "res4f_branch2c_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 212 + } + } +} +node { + name: "bn4f_branch2b_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 209 + } + } +} +node { + name: "bn4f_branch2b_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 208 + } + } +} +node { + name: "res4f_branch2b_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 256 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 207 + } + } +} +node { + name: "bn4f_branch2a_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 204 + } + } +} +node { + name: "bn4f_branch2a_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 203 + } + } +} +node { + name: "res4f_branch2a_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 202 + } + } +} +node { + name: "bn5a_branch2c_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 229 + } + } +} +node { + name: "bn5a_branch2c_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 228 + } + } +} +node { + name: "res5a_branch2c_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 227 + } + } +} +node { + name: "bn5a_branch2b_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 224 + } + } +} +node { + name: "bn5a_branch2b_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 223 + } + } +} +node { + name: "res5a_branch2b_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 512 + } + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 222 + } + } +} +node { + name: "bn5a_branch2a_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 219 + } + } +} +node { + name: "bn5a_branch2a_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 218 + } + } +} +node { + name: "res5a_branch2a_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 217 + } + } +} +node { + name: "bn5b_branch2c_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 249 + } + } +} +node { + name: "bn5b_branch2c_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 248 + } + } +} +node { + name: "res5b_branch2c_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 247 + } + } +} +node { + name: "bn5b_branch2b_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 244 + } + } +} +node { + name: "bn5b_branch2b_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 243 + } + } +} +node { + name: "res5b_branch2b_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 512 + } + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 242 + } + } +} +node { + name: "bn5b_branch2a_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 239 + } + } +} +node { + name: "bn5b_branch2a_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 238 + } + } +} +node { + name: "res5b_branch2a_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 2048 + } + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 237 + } + } +} +node { + name: "bn5c_branch2c_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 264 + } + } +} +node { + name: "bn5c_branch2c_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 263 + } + } +} +node { + name: "res5c_branch2c_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 262 + } + } +} +node { + name: "bn5c_branch2b_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 259 + } + } +} +node { + name: "bn5c_branch2b_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 258 + } + } +} +node { + name: "res5c_branch2b_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 512 + } + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 257 + } + } +} +node { + name: "bn5c_branch2a_cond_input_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 254 + } + } +} +node { + name: "bn5c_branch2a_cond_input_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 253 + } + } +} +node { + name: "res5c_branch2a_conv2d_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 2048 + } + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 252 + } + } +} +node { + name: "activation_49_target_1" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "index" + value { + i: 1 + } + } +} +node { + name: "metrics_2_sparse_categorical_accuracy_assignaddvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "index" + value { + i: 269 + } + } +} +node { + name: "metrics_2_sparse_categorical_accuracy_assignaddvariableop_1_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "index" + value { + i: 270 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "index" + value { + i: 273 + } + } +} +node { + name: "training/LossScaleOptimizer/truediv" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + tensor_content: "\000\000\000<" + } + } + } +} +node { + name: "loss_1/activation_49_loss/Reshape/shape" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 1 + } + } + int_val: -1 + } + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res4c_branch2c/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 1024 + } + } + float_val: 0.0256 + } + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "index" + value { + i: 272 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_99_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 372 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_98_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 371 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_97_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 370 + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res4c_branch2b/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 256 + } + dim { + size: 256 + } + } + float_val: 0.0256 + } + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_96_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 256 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 369 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_95_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 368 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_94_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 367 + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res4c_branch2a/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 256 + } + } + float_val: 0.0256 + } + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_93_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 366 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_92_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 365 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_91_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 364 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_90_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 363 + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res2a_branch2c/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 64 + } + dim { + size: 256 + } + } + float_val: 0.0256 + } + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_9_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 64 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 282 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_89_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 362 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_88_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 361 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_87_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 256 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 360 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_86_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 359 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_85_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 358 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_84_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 357 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_83_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 356 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_82_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 355 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_81_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 354 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_80_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 353 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_8_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 281 + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res4a_branch1/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 1024 + } + } + float_val: 0.0256 + } + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_79_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 352 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_78_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 351 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_77_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 350 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_76_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 349 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_75_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 256 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 348 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_74_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 347 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_73_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 346 + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res4a_branch2a/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 256 + } + } + float_val: 0.0256 + } + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_72_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 345 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_71_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 344 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_70_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 343 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_7_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 280 + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res3d_branch2c/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 128 + } + dim { + size: 512 + } + } + float_val: 0.0256 + } + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_69_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 128 + } + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 342 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_68_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 341 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_67_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 340 + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res3d_branch2b/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 128 + } + dim { + size: 128 + } + } + float_val: 0.0256 + } + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_66_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 128 + } + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 339 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_65_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 338 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_64_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 337 + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res3d_branch2a/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 128 + } + } + float_val: 0.0256 + } + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_63_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 336 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_62_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 335 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_61_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 334 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_60_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 128 + } + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 333 + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res2a_branch2b/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 64 + } + dim { + size: 64 + } + } + float_val: 0.0256 + } + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_6_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 64 + } + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 279 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_59_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 332 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_58_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 331 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_57_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 128 + } + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 330 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_56_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 329 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_55_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 328 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_54_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 327 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_53_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 326 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_52_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 325 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_51_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 128 + } + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 324 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_50_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 323 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_5_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 278 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_49_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 322 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_48_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 128 + } + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 321 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_47_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 320 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_46_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 319 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_45_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 318 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_44_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 317 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_43_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 316 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_42_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 315 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_41_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 314 + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res3a_branch1/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 512 + } + } + float_val: 0.0256 + } + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_40_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 313 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_4_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 277 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_39_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 128 + } + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 312 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_38_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 311 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_37_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 310 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_36_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 128 + } + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 309 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_35_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 308 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_34_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 307 + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res3a_branch2a/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 128 + } + } + float_val: 0.0256 + } + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_33_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 306 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_32_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 305 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_31_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 304 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_30_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 64 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 303 + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res2a_branch2a/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 64 + } + dim { + size: 64 + } + } + float_val: 0.0256 + } + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_3_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 64 + } + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 276 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_29_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 302 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_28_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 301 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_27_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 64 + } + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 300 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_26_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 299 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_25_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 298 + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res2c_branch2a/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 64 + } + } + float_val: 0.0256 + } + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_24_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 297 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_23_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 296 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_22_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 295 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_21_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 64 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 294 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_20_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 293 + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/pool1_pad_1/Pad_grad/Reshape" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\000\000\000\000\000\000\000\000\001\000\000\000\001\000\000\000" + } + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_2_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 275 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_19_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 292 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_18_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 64 + } + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 291 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_17_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 290 + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/fc1000/bias/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1001 + } + } + float_val: 0.0256 + } + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_160_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1001 + } + } + } + } + } + attr { + key: "index" + value { + i: 433 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_16_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 289 + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/fc1000/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 2048 + } + dim { + size: 1001 + } + } + float_val: 0.0256 + } + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_159_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 2048 + } + dim { + size: 1001 + } + } + } + } + } + attr { + key: "index" + value { + i: 432 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_158_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 431 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_157_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 430 + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res5c_branch2c/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 2048 + } + } + float_val: 0.0256 + } + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_156_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 429 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_155_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 428 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_154_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 427 + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res5c_branch2b/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 512 + } + dim { + size: 512 + } + } + float_val: 0.0256 + } + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_153_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 512 + } + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 426 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_152_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 425 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_151_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 424 + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res5c_branch2a/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 2048 + } + dim { + size: 512 + } + } + float_val: 0.0256 + } + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_150_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 2048 + } + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 423 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_15_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 288 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_149_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 422 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_148_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 421 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_147_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 420 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_146_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 419 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_145_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 418 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_144_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 512 + } + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 417 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_143_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 416 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_142_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 415 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_141_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 2048 + } + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 414 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_140_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 413 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_14_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 287 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_139_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 412 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_138_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 411 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_137_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 410 + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res5a_branch1/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 2048 + } + } + float_val: 0.0256 + } + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_136_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 409 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_135_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 408 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_134_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 407 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_133_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 406 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_132_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 512 + } + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 405 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_131_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 404 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_130_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 403 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_13_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 286 + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res5a_branch2a/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 512 + } + } + float_val: 0.0256 + } + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_129_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 402 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_128_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 401 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_127_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 400 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_126_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 399 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_125_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 398 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_124_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 397 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_123_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 256 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 396 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_122_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 395 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_121_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 394 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_120_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 393 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_12_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 285 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_119_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 392 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_118_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 391 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_117_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 390 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_116_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 389 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_115_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 388 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_114_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 256 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 387 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_113_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 386 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_112_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 385 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_111_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 384 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_110_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 383 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_11_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 284 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_109_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 382 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_108_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 381 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_107_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 380 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_106_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 379 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_105_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 256 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 378 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_104_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 377 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_103_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 376 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_102_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 375 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_101_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 374 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_100_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 373 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_10_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 64 + } + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 283 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_1_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 274 + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/conv1/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 7 + } + dim { + size: 7 + } + dim { + size: 3 + } + dim { + size: 64 + } + } + float_val: 0.0256 + } + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_accum" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 7 + } + dim { + size: 7 + } + dim { + size: 3 + } + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 271 + } + } +} +node { + name: "training_lossscaleoptimizer_sgd_assignaddvariableop_resource" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_INT64 + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "index" + value { + i: 434 + } + } +} +node { + name: "bn_conv1_1/AssignMovingAvg_1/sub" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + tensor_content: "\320\314\314=" + } + } + } +} +node { + name: "bn_conv1_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 6 + } + } +} +node { + name: "bn_conv1_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 5 + } + } +} +node { + name: "keras_learning_phase/input" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + bool_val: false + } + } + } +} +node { + name: "bn5c_branch2c_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 265 + } + } +} +node { + name: "bn5c_branch2c_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 266 + } + } +} +node { + name: "bn5c_branch2b_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 260 + } + } +} +node { + name: "bn5c_branch2b_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 261 + } + } +} +node { + name: "bn5c_branch2a_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 256 + } + } +} +node { + name: "bn5c_branch2a_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 255 + } + } +} +node { + name: "bn5b_branch2c_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 251 + } + } +} +node { + name: "bn5b_branch2c_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 250 + } + } +} +node { + name: "bn5b_branch2b_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 245 + } + } +} +node { + name: "bn5b_branch2b_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 246 + } + } +} +node { + name: "bn5b_branch2a_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 241 + } + } +} +node { + name: "bn5b_branch2a_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 240 + } + } +} +node { + name: "bn5a_branch2c_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 230 + } + } +} +node { + name: "bn5a_branch2c_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 231 + } + } +} +node { + name: "bn5a_branch2b_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 225 + } + } +} +node { + name: "bn5a_branch2b_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 226 + } + } +} +node { + name: "bn5a_branch2a_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 220 + } + } +} +node { + name: "bn5a_branch2a_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 221 + } + } +} +node { + name: "bn5a_branch1_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 235 + } + } +} +node { + name: "bn5a_branch1_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 2048 + } + } + } + } + } + attr { + key: "index" + value { + i: 236 + } + } +} +node { + name: "bn4f_branch2c_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 216 + } + } +} +node { + name: "bn4f_branch2c_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 215 + } + } +} +node { + name: "bn4f_branch2b_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 211 + } + } +} +node { + name: "bn4f_branch2b_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 210 + } + } +} +node { + name: "bn4f_branch2a_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 205 + } + } +} +node { + name: "bn4f_branch2a_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 206 + } + } +} +node { + name: "bn4e_branch2c_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 201 + } + } +} +node { + name: "bn4e_branch2c_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 200 + } + } +} +node { + name: "bn4e_branch2b_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 196 + } + } +} +node { + name: "bn4e_branch2b_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 195 + } + } +} +node { + name: "bn4e_branch2a_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 190 + } + } +} +node { + name: "bn4e_branch2a_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 191 + } + } +} +node { + name: "bn4d_branch2c_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 186 + } + } +} +node { + name: "bn4d_branch2c_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 185 + } + } +} +node { + name: "bn4d_branch2b_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 180 + } + } +} +node { + name: "bn4d_branch2b_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 181 + } + } +} +node { + name: "bn4d_branch2a_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 175 + } + } +} +node { + name: "bn4d_branch2a_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 176 + } + } +} +node { + name: "bn4c_branch2c_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 171 + } + } +} +node { + name: "bn4c_branch2c_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 170 + } + } +} +node { + name: "bn4c_branch2b_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 165 + } + } +} +node { + name: "bn4c_branch2b_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 166 + } + } +} +node { + name: "bn4c_branch2a_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 160 + } + } +} +node { + name: "bn4c_branch2a_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 161 + } + } +} +node { + name: "bn4b_branch2c_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 156 + } + } +} +node { + name: "bn4b_branch2c_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 155 + } + } +} +node { + name: "bn4b_branch2b_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 150 + } + } +} +node { + name: "bn4b_branch2b_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 151 + } + } +} +node { + name: "bn4b_branch2a_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 145 + } + } +} +node { + name: "bn4b_branch2a_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 146 + } + } +} +node { + name: "bn4a_branch2c_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 136 + } + } +} +node { + name: "bn4a_branch2c_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 135 + } + } +} +node { + name: "bn4a_branch2b_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 131 + } + } +} +node { + name: "bn4a_branch2b_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 130 + } + } +} +node { + name: "bn4a_branch2a_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 126 + } + } +} +node { + name: "bn4a_branch2a_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 125 + } + } +} +node { + name: "bn4a_branch1_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 141 + } + } +} +node { + name: "bn4a_branch1_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 1024 + } + } + } + } + } + attr { + key: "index" + value { + i: 140 + } + } +} +node { + name: "bn3d_branch2c_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 120 + } + } +} +node { + name: "bn3d_branch2c_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 121 + } + } +} +node { + name: "bn3d_branch2b_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 116 + } + } +} +node { + name: "bn3d_branch2b_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 115 + } + } +} +node { + name: "bn3d_branch2a_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 111 + } + } +} +node { + name: "bn3d_branch2a_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 110 + } + } +} +node { + name: "bn3c_branch2c_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 105 + } + } +} +node { + name: "bn3c_branch2c_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 106 + } + } +} +node { + name: "bn3c_branch2b_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 101 + } + } +} +node { + name: "bn3c_branch2b_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 100 + } + } +} +node { + name: "bn3c_branch2a_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 95 + } + } +} +node { + name: "bn3c_branch2a_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 96 + } + } +} +node { + name: "bn3b_branch2c_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 90 + } + } +} +node { + name: "bn3b_branch2c_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 91 + } + } +} +node { + name: "bn3b_branch2b_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 86 + } + } +} +node { + name: "bn3b_branch2b_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 85 + } + } +} +node { + name: "bn3b_branch2a_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 80 + } + } +} +node { + name: "bn3b_branch2a_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 81 + } + } +} +node { + name: "bn3a_branch2c_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 70 + } + } +} +node { + name: "bn3a_branch2c_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 71 + } + } +} +node { + name: "bn3a_branch2b_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 66 + } + } +} +node { + name: "bn3a_branch2b_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 65 + } + } +} +node { + name: "bn3a_branch2a_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 61 + } + } +} +node { + name: "bn3a_branch2a_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 128 + } + } + } + } + } + attr { + key: "index" + value { + i: 60 + } + } +} +node { + name: "bn3a_branch1_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 75 + } + } +} +node { + name: "bn3a_branch1_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 512 + } + } + } + } + } + attr { + key: "index" + value { + i: 76 + } + } +} +node { + name: "bn2c_branch2c_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 55 + } + } +} +node { + name: "bn2c_branch2c_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 56 + } + } +} +node { + name: "bn2c_branch2b_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 51 + } + } +} +node { + name: "bn2c_branch2b_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 50 + } + } +} +node { + name: "bn2c_branch2a_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 45 + } + } +} +node { + name: "bn2c_branch2a_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 46 + } + } +} +node { + name: "bn2b_branch2c_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 40 + } + } +} +node { + name: "bn2b_branch2c_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 41 + } + } +} +node { + name: "bn2b_branch2b_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 36 + } + } +} +node { + name: "bn2b_branch2b_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 35 + } + } +} +node { + name: "bn2b_branch2a_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 31 + } + } +} +node { + name: "bn2b_branch2a_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 30 + } + } +} +node { + name: "bn2a_branch2c_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 21 + } + } +} +node { + name: "bn2a_branch2c_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 20 + } + } +} +node { + name: "bn2a_branch2b_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 15 + } + } +} +node { + name: "bn2a_branch2b_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 16 + } + } +} +node { + name: "bn2a_branch2a_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 10 + } + } +} +node { + name: "bn2a_branch2a_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 64 + } + } + } + } + } + attr { + key: "index" + value { + i: 11 + } + } +} +node { + name: "bn2a_branch1_cond_input_3" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 25 + } + } +} +node { + name: "bn2a_branch1_cond_input_4" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "_handle_dtypes" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_handle_shapes" + value { + list { + shape { + dim { + size: 256 + } + } + } + } + } + attr { + key: "index" + value { + i: 26 + } + } +} +node { + name: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.0001 + } + } + } +} +node { + name: "loss_1/fc1000/kernel/Regularizer/Const" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 2 + } + } + tensor_content: "\000\000\000\000\001\000\000\000" + } + } + } +} +node { + name: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\000\000\000\000\001\000\000\000\002\000\000\000\003\000\000\000" + } + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/Shape" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + } + } + } + } + } +} +node { + name: "fc1000_1/BiasAdd/ReadVariableOp" + op: "ReadVariableOp" + input: "fc1000_biasadd_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/fc1000/bias/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "fc1000_biasadd_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "fc1000_1/MatMul/ReadVariableOp" + op: "ReadVariableOp" + input: "fc1000_matmul_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/fc1000/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "fc1000_matmul_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch1_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn5a_branch1_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch1_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5a_branch1_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5a_branch1_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res5a_branch1_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5a_branch1/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res5a_branch1_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch1_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4a_branch1_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch1_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4a_branch1_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4a_branch1_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4a_branch1_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4a_branch1/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4a_branch1_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch1_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn3a_branch1_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch1_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3a_branch1_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3a_branch1_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res3a_branch1_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3a_branch1/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res3a_branch1_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch1_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn2a_branch1_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch1_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2a_branch1_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2a_branch1_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res2a_branch1_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2a_branch1/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res2a_branch1_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn_conv1_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn_conv1_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn_conv1_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn_conv1_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "conv1_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "conv1_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/conv1/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "conv1_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "transpose_1/transpose" + op: "Transpose" + input: "input_1_1" + input: "transpose_1/transpose/perm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "Tperm" + value { + type: DT_INT32 + } + } +} +node { + name: "bn2a_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn2a_branch2c_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2a_branch2c_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2a_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res2a_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2a_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res2a_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn2a_branch2b_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2a_branch2b_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2a_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res2a_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2a_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res2a_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn2a_branch2a_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2a_branch2a_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2a_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res2a_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2a_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res2a_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn2b_branch2c_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2b_branch2c_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2b_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res2b_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2b_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res2b_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn2b_branch2b_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2b_branch2b_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2b_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res2b_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2b_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res2b_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn2b_branch2a_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2b_branch2a_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2b_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res2b_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2b_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res2b_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn2c_branch2c_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2c_branch2c_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2c_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res2c_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2c_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res2c_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn2c_branch2b_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2c_branch2b_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2c_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res2c_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2c_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res2c_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn2c_branch2a_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2c_branch2a_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2c_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res2c_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2c_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res2c_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn3a_branch2c_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3a_branch2c_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3a_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res3a_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3a_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res3a_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn3a_branch2b_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3a_branch2b_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3a_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res3a_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3a_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res3a_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn3a_branch2a_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3a_branch2a_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3a_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res3a_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3a_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res3a_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn3b_branch2c_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3b_branch2c_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3b_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res3b_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3b_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res3b_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn3b_branch2b_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3b_branch2b_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3b_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res3b_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3b_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res3b_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn3b_branch2a_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3b_branch2a_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3b_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res3b_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3b_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res3b_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn3c_branch2c_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3c_branch2c_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3c_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res3c_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3c_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res3c_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn3c_branch2b_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3c_branch2b_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3c_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res3c_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3c_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res3c_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn3c_branch2a_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3c_branch2a_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3c_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res3c_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3c_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res3c_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn3d_branch2c_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3d_branch2c_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3d_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res3d_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3d_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res3d_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn3d_branch2b_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3d_branch2b_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3d_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res3d_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3d_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res3d_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn3d_branch2a_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3d_branch2a_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3d_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res3d_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3d_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res3d_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4a_branch2c_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4a_branch2c_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4a_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4a_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4a_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4a_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4a_branch2b_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4a_branch2b_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4a_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4a_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4a_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4a_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4a_branch2a_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4a_branch2a_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4a_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4a_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4a_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4a_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4b_branch2c_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4b_branch2c_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4b_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4b_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4b_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4b_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4b_branch2b_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4b_branch2b_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4b_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4b_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4b_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4b_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4b_branch2a_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4b_branch2a_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4b_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4b_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4b_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4b_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4c_branch2c_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4c_branch2c_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4c_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4c_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4c_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4c_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4c_branch2b_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4c_branch2b_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4c_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4c_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4c_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4c_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4c_branch2a_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4c_branch2a_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4c_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4c_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4c_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4c_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4d_branch2c_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4d_branch2c_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4d_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4d_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4d_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4d_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4d_branch2b_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4d_branch2b_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4d_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4d_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4d_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4d_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4d_branch2a_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4d_branch2a_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4d_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4d_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4d_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4d_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4e_branch2c_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4e_branch2c_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4e_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4e_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4e_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4e_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4e_branch2b_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4e_branch2b_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4e_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4e_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4e_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4e_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4e_branch2a_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4e_branch2a_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4e_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4e_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4e_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4e_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4f_branch2c_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4f_branch2c_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4f_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4f_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4f_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4f_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4f_branch2b_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4f_branch2b_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4f_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4f_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4f_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4f_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4f_branch2a_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4f_branch2a_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4f_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4f_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4f_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4f_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn5a_branch2c_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5a_branch2c_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5a_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res5a_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5a_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res5a_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn5a_branch2b_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5a_branch2b_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5a_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res5a_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5a_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res5a_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn5a_branch2a_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5a_branch2a_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5a_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res5a_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5a_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res5a_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn5b_branch2c_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5b_branch2c_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5b_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res5b_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5b_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res5b_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn5b_branch2b_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5b_branch2b_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5b_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res5b_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5b_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res5b_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn5b_branch2a_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5b_branch2a_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5b_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res5b_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5b_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res5b_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn5c_branch2c_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5c_branch2c_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5c_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res5c_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5c_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res5c_branch2c_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn5c_branch2b_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5c_branch2b_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5c_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res5c_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5c_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res5c_branch2b_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn5c_branch2a_cond_input_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5c_branch2a_cond_input_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5c_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res5c_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5c_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res5c_branch2a_conv2d_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "metrics_2/sparse_categorical_accuracy/Squeeze" + op: "Squeeze" + input: "activation_49_target_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "squeeze_dims" + value { + list { + i: -1 + } + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_99/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_98/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_97/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_96/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_95/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_94/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_93/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_92/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_91/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_90/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_9/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_89/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_88/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_87/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_86/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_85/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_84/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_83/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_82/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_81/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_80/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_8/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_79/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_78/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_77/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_76/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_75/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_74/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_73/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_72/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_71/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_70/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_7/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_69/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_68/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_67/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_66/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_65/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_64/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_63/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_62/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_61/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_60/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_6/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_59/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_58/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_57/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_56/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_55/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_54/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_53/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_52/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_51/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_50/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_5/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_49/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_48/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_47/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_46/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_45/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_44/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_43/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_42/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_41/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_40/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_4/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_39/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_38/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_37/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_36/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_35/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_34/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_33/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_32/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_31/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_30/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_3/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_29/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_28/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_27/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_26/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_25/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_24/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_23/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_22/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_21/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_20/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_2/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_19/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_18/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_17/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_160/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_16/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_159/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_158/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_157/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_156/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_155/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_154/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_153/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_152/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_151/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_150/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_15/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_149/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_148/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_147/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_146/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_145/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_144/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_143/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_142/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_141/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_140/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_14/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_139/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_138/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_137/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_136/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_135/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_134/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_133/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_132/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_131/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_130/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_13/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_129/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_128/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_127/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_126/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_125/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_124/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_123/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_122/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_121/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_120/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_12/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_119/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_118/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_117/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_116/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_115/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_114/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_113/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_112/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_111/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_110/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_11/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_109/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_108/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_107/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_106/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_105/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_104/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_103/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_102/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_101/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_100/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_10/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_1/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_1_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/activation_49_loss/Reshape" + op: "Reshape" + input: "activation_49_target_1" + input: "loss_1/activation_49_loss/Reshape/shape" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tshape" + value { + type: DT_INT32 + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_99/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_98/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_97/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_96/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_95/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_94/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_93/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_92/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_91/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_90/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_9/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_89/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_88/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_87/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_86/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_85/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_84/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_83/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_82/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_81/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_80/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_8/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_79/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_78/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_77/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_76/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_75/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_74/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_73/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_72/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_71/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_70/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_7/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_69/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_68/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_67/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_66/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_65/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_64/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_63/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_62/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_61/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_60/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_6/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_59/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_58/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_57/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_56/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_55/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_54/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_53/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_52/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_51/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_50/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_5/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_49/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_48/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_47/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_46/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_45/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_44/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_43/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_42/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_41/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_40/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_4/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_39/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_38/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_37/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_36/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_35/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_34/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_33/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_32/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_31/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_30/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_3/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_29/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_28/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_27/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_26/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_25/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_24/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_23/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_22/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_21/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_20/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_2/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_19/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_18/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_17/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_160/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_16/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_159/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_158/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_157/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_156/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_155/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_154/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_153/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_152/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_151/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_150/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_15/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_149/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_148/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_147/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_146/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_145/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_144/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_143/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_142/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_141/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_140/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_14/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_139/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_138/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_137/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_136/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_135/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_134/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_133/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_132/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_131/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_130/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_13/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_129/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_128/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_127/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_126/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_125/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_124/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_123/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_122/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_121/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_120/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_12/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_119/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_118/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_117/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_116/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_115/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_114/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_113/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_112/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_111/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_110/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_11/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_109/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_108/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_107/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_106/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_105/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_104/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_103/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_102/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_101/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_100/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_10/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_1/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_readvariableop_resource" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn_conv1_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn_conv1_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn_conv1_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn_conv1_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "keras_learning_phase" + op: "PlaceholderWithDefault" + input: "keras_learning_phase/input" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "shape" + value { + shape { + } + } + } +} +node { + name: "bn5c_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5c_branch2c_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5c_branch2c_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5c_branch2b_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5c_branch2b_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5c_branch2a_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5c_branch2a_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5b_branch2c_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5b_branch2c_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5b_branch2b_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5b_branch2b_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5b_branch2a_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5b_branch2a_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5a_branch2c_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5a_branch2c_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5a_branch2b_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5a_branch2b_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5a_branch2a_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5a_branch2a_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch1_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5a_branch1_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch1_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5a_branch1_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4f_branch2c_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4f_branch2c_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4f_branch2b_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4f_branch2b_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4f_branch2a_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4f_branch2a_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4e_branch2c_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4e_branch2c_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4e_branch2b_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4e_branch2b_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4e_branch2a_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4e_branch2a_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4d_branch2c_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4d_branch2c_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4d_branch2b_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4d_branch2b_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4d_branch2a_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4d_branch2a_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4c_branch2c_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4c_branch2c_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4c_branch2b_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4c_branch2b_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4c_branch2a_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4c_branch2a_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4b_branch2c_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4b_branch2c_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4b_branch2b_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4b_branch2b_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4b_branch2a_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4b_branch2a_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4a_branch2c_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4a_branch2c_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4a_branch2b_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4a_branch2b_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4a_branch2a_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4a_branch2a_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch1_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4a_branch1_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch1_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4a_branch1_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3d_branch2c_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3d_branch2c_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3d_branch2b_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3d_branch2b_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3d_branch2a_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3d_branch2a_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3c_branch2c_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3c_branch2c_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3c_branch2b_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3c_branch2b_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3c_branch2a_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3c_branch2a_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3b_branch2c_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3b_branch2c_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3b_branch2b_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3b_branch2b_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3b_branch2a_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3b_branch2a_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3a_branch2c_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3a_branch2c_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3a_branch2b_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3a_branch2b_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3a_branch2a_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3a_branch2a_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch1_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3a_branch1_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch1_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3a_branch1_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2c_branch2c_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2c_branch2c_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2c_branch2b_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2c_branch2b_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2c_branch2a_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2c_branch2a_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2b_branch2c_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2b_branch2c_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2b_branch2b_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2b_branch2b_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2b_branch2a_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2b_branch2a_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2a_branch2c_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2a_branch2c_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2a_branch2b_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2a_branch2b_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2a_branch2a_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2a_branch2a_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch1_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2a_branch1_cond_input_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch1_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2a_branch1_cond_input_4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "fc1000_1/BiasAdd/Cast" + op: "Cast" + input: "fc1000_1/BiasAdd/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/fc1000/bias/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/fc1000/bias/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/fc1000/bias/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/fc1000/bias/Regularizer/Square" + op: "Square" + input: "loss_1/fc1000/bias/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "fc1000_1/MatMul/Cast" + op: "Cast" + input: "fc1000_1/MatMul/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/fc1000/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/fc1000/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/fc1000/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/fc1000/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/fc1000/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5a_branch1_1/Conv2D/Cast" + op: "Cast" + input: "res5a_branch1_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res5a_branch1/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res5a_branch1/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res5a_branch1/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5a_branch1/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res5a_branch1/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4a_branch1_1/Conv2D/Cast" + op: "Cast" + input: "res4a_branch1_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res4a_branch1/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4a_branch1/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res4a_branch1/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4a_branch1/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4a_branch1/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3a_branch1_1/Conv2D/Cast" + op: "Cast" + input: "res3a_branch1_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res3a_branch1/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res3a_branch1/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res3a_branch1/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3a_branch1/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res3a_branch1/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2a_branch1_1/Conv2D/Cast" + op: "Cast" + input: "res2a_branch1_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res2a_branch1/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res2a_branch1/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res2a_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2a_branch1/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res2a_branch1/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "conv1_1/Conv2D/Cast" + op: "Cast" + input: "conv1_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/conv1/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/conv1/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/conv1/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/conv1/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/conv1/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "conv1_pad_1/Pad" + op: "Pad" + input: "transpose_1/transpose" + input: "conv1_pad_1/Pad/paddings" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "Tpaddings" + value { + type: DT_INT32 + } + } +} +node { + name: "res2a_branch2c_1/Conv2D/Cast" + op: "Cast" + input: "res2a_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res2a_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res2a_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res2a_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2a_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res2a_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2a_branch2b_1/Conv2D/Cast" + op: "Cast" + input: "res2a_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res2a_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res2a_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res2a_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2a_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res2a_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2a_branch2a_1/Conv2D/Cast" + op: "Cast" + input: "res2a_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res2a_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res2a_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res2a_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2a_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res2a_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2b_branch2c_1/Conv2D/Cast" + op: "Cast" + input: "res2b_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res2b_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res2b_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res2a_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2b_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res2b_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2b_branch2b_1/Conv2D/Cast" + op: "Cast" + input: "res2b_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res2b_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res2b_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res2a_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2b_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res2b_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2b_branch2a_1/Conv2D/Cast" + op: "Cast" + input: "res2b_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res2b_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res2b_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res2c_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2b_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res2b_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2c_branch2c_1/Conv2D/Cast" + op: "Cast" + input: "res2c_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res2c_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res2c_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res2a_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2c_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res2c_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2c_branch2b_1/Conv2D/Cast" + op: "Cast" + input: "res2c_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res2c_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res2c_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res2a_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2c_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res2c_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2c_branch2a_1/Conv2D/Cast" + op: "Cast" + input: "res2c_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res2c_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res2c_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res2c_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2c_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res2c_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3a_branch2c_1/Conv2D/Cast" + op: "Cast" + input: "res3a_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res3a_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res3a_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res3d_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3a_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res3a_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3a_branch2b_1/Conv2D/Cast" + op: "Cast" + input: "res3a_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res3a_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res3a_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res3d_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3a_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res3a_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3a_branch2a_1/Conv2D/Cast" + op: "Cast" + input: "res3a_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res3a_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res3a_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res3a_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3a_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res3a_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3b_branch2c_1/Conv2D/Cast" + op: "Cast" + input: "res3b_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res3b_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res3b_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res3d_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3b_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res3b_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3b_branch2b_1/Conv2D/Cast" + op: "Cast" + input: "res3b_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res3b_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res3b_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res3d_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3b_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res3b_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3b_branch2a_1/Conv2D/Cast" + op: "Cast" + input: "res3b_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res3b_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res3b_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res3d_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3b_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res3b_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3c_branch2c_1/Conv2D/Cast" + op: "Cast" + input: "res3c_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res3c_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res3c_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res3d_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3c_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res3c_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3c_branch2b_1/Conv2D/Cast" + op: "Cast" + input: "res3c_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res3c_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res3c_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res3d_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3c_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res3c_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3c_branch2a_1/Conv2D/Cast" + op: "Cast" + input: "res3c_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res3c_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res3c_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res3d_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3c_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res3c_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3d_branch2c_1/Conv2D/Cast" + op: "Cast" + input: "res3d_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res3d_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res3d_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res3d_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3d_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res3d_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3d_branch2b_1/Conv2D/Cast" + op: "Cast" + input: "res3d_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res3d_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res3d_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res3d_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3d_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res3d_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3d_branch2a_1/Conv2D/Cast" + op: "Cast" + input: "res3d_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res3d_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res3d_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res3d_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3d_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res3d_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4a_branch2c_1/Conv2D/Cast" + op: "Cast" + input: "res4a_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res4a_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4a_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res4c_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4a_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4a_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4a_branch2b_1/Conv2D/Cast" + op: "Cast" + input: "res4a_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res4a_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4a_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res4c_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4a_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4a_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4a_branch2a_1/Conv2D/Cast" + op: "Cast" + input: "res4a_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res4a_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4a_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res4a_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4a_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4a_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4b_branch2c_1/Conv2D/Cast" + op: "Cast" + input: "res4b_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res4b_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4b_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res4c_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4b_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4b_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4b_branch2b_1/Conv2D/Cast" + op: "Cast" + input: "res4b_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res4b_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4b_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res4c_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4b_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4b_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4b_branch2a_1/Conv2D/Cast" + op: "Cast" + input: "res4b_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res4b_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4b_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res4c_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4b_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4b_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4c_branch2c_1/Conv2D/Cast" + op: "Cast" + input: "res4c_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res4c_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res4c_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4c_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4c_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4c_branch2b_1/Conv2D/Cast" + op: "Cast" + input: "res4c_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res4c_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4c_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res4c_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4c_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4c_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4c_branch2a_1/Conv2D/Cast" + op: "Cast" + input: "res4c_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res4c_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4c_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res4c_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4c_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4c_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4d_branch2c_1/Conv2D/Cast" + op: "Cast" + input: "res4d_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res4d_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4d_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res4c_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4d_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4d_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4d_branch2b_1/Conv2D/Cast" + op: "Cast" + input: "res4d_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res4d_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4d_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res4c_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4d_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4d_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4d_branch2a_1/Conv2D/Cast" + op: "Cast" + input: "res4d_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res4d_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4d_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res4c_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4d_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4d_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4e_branch2c_1/Conv2D/Cast" + op: "Cast" + input: "res4e_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res4e_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4e_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res4c_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4e_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4e_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4e_branch2b_1/Conv2D/Cast" + op: "Cast" + input: "res4e_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res4e_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4e_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res4c_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4e_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4e_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4e_branch2a_1/Conv2D/Cast" + op: "Cast" + input: "res4e_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res4e_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4e_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res4c_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4e_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4e_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4f_branch2c_1/Conv2D/Cast" + op: "Cast" + input: "res4f_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res4f_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4f_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res4c_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4f_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4f_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4f_branch2b_1/Conv2D/Cast" + op: "Cast" + input: "res4f_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res4f_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4f_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res4c_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4f_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4f_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4f_branch2a_1/Conv2D/Cast" + op: "Cast" + input: "res4f_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res4f_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4f_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res4c_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4f_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4f_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5a_branch2c_1/Conv2D/Cast" + op: "Cast" + input: "res5a_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res5a_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res5a_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res5c_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5a_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res5a_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5a_branch2b_1/Conv2D/Cast" + op: "Cast" + input: "res5a_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res5a_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res5a_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res5c_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5a_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res5a_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5a_branch2a_1/Conv2D/Cast" + op: "Cast" + input: "res5a_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res5a_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res5a_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res5a_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5a_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res5a_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5b_branch2c_1/Conv2D/Cast" + op: "Cast" + input: "res5b_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res5b_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res5b_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res5c_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5b_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res5b_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5b_branch2b_1/Conv2D/Cast" + op: "Cast" + input: "res5b_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res5b_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res5b_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res5c_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5b_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res5b_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5b_branch2a_1/Conv2D/Cast" + op: "Cast" + input: "res5b_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res5b_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res5b_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res5c_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5b_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res5b_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5c_branch2c_1/Conv2D/Cast" + op: "Cast" + input: "res5c_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res5c_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res5c_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5c_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5c_branch2b_1/Conv2D/Cast" + op: "Cast" + input: "res5c_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res5c_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res5c_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res5c_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5c_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res5c_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5c_branch2a_1/Conv2D/Cast" + op: "Cast" + input: "res5c_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/res5c_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res5c_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/LossScaleOptimizer/gradients/loss_1/res5c_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5c_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res5c_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/activation_49_loss/Cast" + op: "Cast" + input: "loss_1/activation_49_loss/Reshape" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_INT64 + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "bn_conv1/cond_1/switch_pred/_163" + op: "Switch" + input: "keras_learning_phase" + input: "keras_learning_phase" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn_conv1/cond_1" + } +} +node { + name: "loss_1/fc1000/bias/Regularizer/Sum" + op: "Sum" + input: "loss_1/fc1000/bias/Regularizer/Square" + input: "metrics_2/sparse_categorical_accuracy/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/fc1000/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/fc1000/kernel/Regularizer/Square" + input: "loss_1/fc1000/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res5a_branch1/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res5a_branch1/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4a_branch1/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4a_branch1/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res3a_branch1/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res3a_branch1/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res2a_branch1/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res2a_branch1/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/conv1/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/conv1/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "conv1_1/Conv2D" + op: "Conv2D" + input: "conv1_pad_1/Pad" + input: "conv1_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/conv1_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^conv1_1/Conv2D/Cast" + input: "^conv1_pad_1/Pad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\007\000\000\000\007\000\000\000\003\000\000\000@\000\000\000" + } + } + } +} +node { + name: "loss_1/res2a_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res2a_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res2a_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res2a_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res2a_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res2a_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res2b_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res2b_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res2b_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res2b_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res2b_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res2b_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res2c_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res2c_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res2c_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res2c_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res2c_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res2c_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res3a_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res3a_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res3a_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res3a_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res3a_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res3a_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res3b_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res3b_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res3b_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res3b_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res3b_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res3b_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res3c_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res3c_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res3c_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res3c_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res3c_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res3c_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res3d_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res3d_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res3d_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res3d_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res3d_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res3d_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4a_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4a_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4a_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4a_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4a_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4a_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4b_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4b_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4b_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4b_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4b_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4b_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4c_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4c_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4c_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4c_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4c_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4c_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4d_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4d_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4d_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4d_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4d_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4d_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4e_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4e_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4e_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4e_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4e_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4e_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4f_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4f_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4f_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4f_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4f_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4f_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res5a_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res5a_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res5a_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res5a_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res5a_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res5a_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res5b_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res5b_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res5b_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res5b_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res5b_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res5b_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res5c_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res5c_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res5c_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res5c_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res5c_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + op: "Identity" + input: "bn_conv1/cond_1/switch_pred/_163" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_BOOL + } + } +} +node { + name: "ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + op: "Identity" + input: "bn_conv1/cond_1/switch_pred/_163:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_BOOL + } + } +} +node { + name: "loss_1/fc1000/bias/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/fc1000/bias/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/fc1000/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/fc1000/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5a_branch1/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res5a_branch1/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4a_branch1/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res4a_branch1/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3a_branch1/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res3a_branch1/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2a_branch1/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res2a_branch1/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/conv1/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/conv1/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn_conv1_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "conv1_1/Conv2D" + input: "bn_conv1_1/ReadVariableOp" + input: "bn_conv1_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "loss_1/res2a_branch2c/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res2a_branch2c/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2a_branch2b/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res2a_branch2b/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2a_branch2a/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res2a_branch2a/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2b_branch2c/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res2b_branch2c/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2b_branch2b/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res2b_branch2b/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2b_branch2a/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res2b_branch2a/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2c_branch2c/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res2c_branch2c/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2c_branch2b/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res2c_branch2b/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2c_branch2a/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res2c_branch2a/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3a_branch2c/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res3a_branch2c/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3a_branch2b/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res3a_branch2b/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3a_branch2a/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res3a_branch2a/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3b_branch2c/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res3b_branch2c/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3b_branch2b/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res3b_branch2b/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3b_branch2a/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res3b_branch2a/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3c_branch2c/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res3c_branch2c/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3c_branch2b/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res3c_branch2b/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3c_branch2a/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res3c_branch2a/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3d_branch2c/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res3d_branch2c/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3d_branch2b/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res3d_branch2b/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3d_branch2a/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res3d_branch2a/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4a_branch2c/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res4a_branch2c/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4a_branch2b/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res4a_branch2b/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4a_branch2a/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res4a_branch2a/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4b_branch2c/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res4b_branch2c/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4b_branch2b/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res4b_branch2b/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4b_branch2a/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res4b_branch2a/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4c_branch2c/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res4c_branch2c/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4c_branch2b/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res4c_branch2b/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4c_branch2a/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res4c_branch2a/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4d_branch2c/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res4d_branch2c/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4d_branch2b/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res4d_branch2b/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4d_branch2a/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res4d_branch2a/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4e_branch2c/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res4e_branch2c/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4e_branch2b/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res4e_branch2b/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4e_branch2a/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res4e_branch2a/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4f_branch2c/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res4f_branch2c/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4f_branch2b/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res4f_branch2b/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4f_branch2a/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res4f_branch2a/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5a_branch2c/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res5a_branch2c/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5a_branch2b/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res5a_branch2b/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5a_branch2a/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res5a_branch2a/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5b_branch2c/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res5b_branch2c/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5b_branch2b/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res5b_branch2b/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5b_branch2a/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res5b_branch2a/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5c_branch2c/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5c_branch2b/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res5c_branch2b/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5c_branch2a/kernel/Regularizer/mul" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul/x" + input: "loss_1/res5c_branch2a/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn_conv1/cond_1/pivot_f/_164" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn_conv1/cond_1" + } +} +node { + name: "bn5c_branch2c/cond_1/pivot_f/_269" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn5c_branch2c/cond_1" + } +} +node { + name: "bn5c_branch2b/cond_1/pivot_f/_213" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn5c_branch2b/cond_1" + } +} +node { + name: "bn5c_branch2a/cond_1/pivot_f/_346" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn5c_branch2a/cond_1" + } +} +node { + name: "bn5b_branch2c/cond_1/pivot_f/_311" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn5b_branch2c/cond_1" + } +} +node { + name: "bn5b_branch2b/cond_1/pivot_f/_262" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn5b_branch2b/cond_1" + } +} +node { + name: "bn5b_branch2a/cond_1/pivot_f/_157" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn5b_branch2a/cond_1" + } +} +node { + name: "bn5a_branch2c/cond_1/pivot_f/_115" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn5a_branch2c/cond_1" + } +} +node { + name: "bn5a_branch2b/cond_1/pivot_f/_80" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn5a_branch2b/cond_1" + } +} +node { + name: "bn5a_branch2a/cond_1/pivot_f/_59" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn5a_branch2a/cond_1" + } +} +node { + name: "bn5a_branch1/cond_1/pivot_f/_206" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn5a_branch1/cond_1" + } +} +node { + name: "bn4f_branch2c/cond_1/pivot_f/_304" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn4f_branch2c/cond_1" + } +} +node { + name: "bn4f_branch2b/cond_1/pivot_f/_150" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn4f_branch2b/cond_1" + } +} +node { + name: "bn4f_branch2a/cond_1/pivot_f/_255" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn4f_branch2a/cond_1" + } +} +node { + name: "bn4e_branch2c/cond_1/pivot_f/_199" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn4e_branch2c/cond_1" + } +} +node { + name: "bn4e_branch2b/cond_1/pivot_f/_108" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn4e_branch2b/cond_1" + } +} +node { + name: "bn4e_branch2a/cond_1/pivot_f/_73" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn4e_branch2a/cond_1" + } +} +node { + name: "bn4d_branch2c/cond_1/pivot_f/_52" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn4d_branch2c/cond_1" + } +} +node { + name: "bn4d_branch2b/cond_1/pivot_f/_339" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn4d_branch2b/cond_1" + } +} +node { + name: "bn4d_branch2a/cond_1/pivot_f/_143" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn4d_branch2a/cond_1" + } +} +node { + name: "bn4c_branch2c/cond_1/pivot_f/_248" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn4c_branch2c/cond_1" + } +} +node { + name: "bn4c_branch2b/cond_1/pivot_f/_192" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn4c_branch2b/cond_1" + } +} +node { + name: "bn4c_branch2a/cond_1/pivot_f/_101" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn4c_branch2a/cond_1" + } +} +node { + name: "bn4b_branch2c/cond_1/pivot_f/_66" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn4b_branch2c/cond_1" + } +} +node { + name: "bn4b_branch2b/cond_1/pivot_f/_45" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn4b_branch2b/cond_1" + } +} +node { + name: "bn4b_branch2a/cond_1/pivot_f/_10" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn4b_branch2a/cond_1" + } +} +node { + name: "bn4a_branch2c/cond_1/pivot_f/_185" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn4a_branch2c/cond_1" + } +} +node { + name: "bn4a_branch2b/cond_1/pivot_f/_136" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn4a_branch2b/cond_1" + } +} +node { + name: "bn4a_branch2a/cond_1/pivot_f/_94" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn4a_branch2a/cond_1" + } +} +node { + name: "bn4a_branch1/cond_1/pivot_f/_367" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn4a_branch1/cond_1" + } +} +node { + name: "bn3d_branch2c/cond_1/pivot_f/_38" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn3d_branch2c/cond_1" + } +} +node { + name: "bn3d_branch2b/cond_1/pivot_f/_241" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn3d_branch2b/cond_1" + } +} +node { + name: "bn3d_branch2a/cond_1/pivot_f/_360" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn3d_branch2a/cond_1" + } +} +node { + name: "bn3c_branch2c/cond_1/pivot_f/_332" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn3c_branch2c/cond_1" + } +} +node { + name: "bn3c_branch2b/cond_1/pivot_f/_297" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn3c_branch2b/cond_1" + } +} +node { + name: "bn3c_branch2a/cond_1/pivot_f/_325" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn3c_branch2a/cond_1" + } +} +node { + name: "bn3b_branch2c/cond_1/pivot_f/_234" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn3b_branch2c/cond_1" + } +} +node { + name: "bn3b_branch2b/cond_1/pivot_f/_353" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn3b_branch2b/cond_1" + } +} +node { + name: "bn3b_branch2a/cond_1/pivot_f/_178" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn3b_branch2a/cond_1" + } +} +node { + name: "bn3a_branch2c/cond_1/pivot_f/_87" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn3a_branch2c/cond_1" + } +} +node { + name: "bn3a_branch2b/cond_1/pivot_f/_31" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn3a_branch2b/cond_1" + } +} +node { + name: "bn3a_branch2a/cond_1/pivot_f/_3" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn3a_branch2a/cond_1" + } +} +node { + name: "bn3a_branch1/cond_1/pivot_f/_129" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn3a_branch1/cond_1" + } +} +node { + name: "bn2c_branch2c/cond_1/pivot_f/_24" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn2c_branch2c/cond_1" + } +} +node { + name: "bn2c_branch2b/cond_1/pivot_f/_318" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn2c_branch2b/cond_1" + } +} +node { + name: "bn2c_branch2a/cond_1/pivot_f/_227" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn2c_branch2a/cond_1" + } +} +node { + name: "bn2b_branch2c/cond_1/pivot_f/_171" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn2b_branch2c/cond_1" + } +} +node { + name: "bn2b_branch2b/cond_1/pivot_f/_122" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn2b_branch2b/cond_1" + } +} +node { + name: "bn2b_branch2a/cond_1/pivot_f/_290" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn2b_branch2a/cond_1" + } +} +node { + name: "bn2a_branch2c/cond_1/pivot_f/_276" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn2a_branch2c/cond_1" + } +} +node { + name: "bn2a_branch2b/cond_1/pivot_f/_17" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn2a_branch2b/cond_1" + } +} +node { + name: "bn2a_branch2a/cond_1/pivot_f/_220" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn2a_branch2a/cond_1" + } +} +node { + name: "bn2a_branch1/cond_1/pivot_f/_283" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\000" + } + } + } + experimental_debug_info { + original_node_names: "bn2a_branch1/cond_1" + } +} +node { + name: "bn_conv1/cond_1/pivot_t/_165" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn_conv1/cond_1" + } +} +node { + name: "bn5c_branch2c/cond_1/pivot_t/_270" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn5c_branch2c/cond_1" + } +} +node { + name: "bn5c_branch2b/cond_1/pivot_t/_214" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn5c_branch2b/cond_1" + } +} +node { + name: "bn5c_branch2a/cond_1/pivot_t/_347" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn5c_branch2a/cond_1" + } +} +node { + name: "bn5b_branch2c/cond_1/pivot_t/_312" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn5b_branch2c/cond_1" + } +} +node { + name: "bn5b_branch2b/cond_1/pivot_t/_263" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn5b_branch2b/cond_1" + } +} +node { + name: "bn5b_branch2a/cond_1/pivot_t/_158" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn5b_branch2a/cond_1" + } +} +node { + name: "bn5a_branch2c/cond_1/pivot_t/_116" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn5a_branch2c/cond_1" + } +} +node { + name: "bn5a_branch2b/cond_1/pivot_t/_81" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn5a_branch2b/cond_1" + } +} +node { + name: "bn5a_branch2a/cond_1/pivot_t/_60" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn5a_branch2a/cond_1" + } +} +node { + name: "bn5a_branch1/cond_1/pivot_t/_207" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn5a_branch1/cond_1" + } +} +node { + name: "bn4f_branch2c/cond_1/pivot_t/_305" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn4f_branch2c/cond_1" + } +} +node { + name: "bn4f_branch2b/cond_1/pivot_t/_151" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn4f_branch2b/cond_1" + } +} +node { + name: "bn4f_branch2a/cond_1/pivot_t/_256" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn4f_branch2a/cond_1" + } +} +node { + name: "bn4e_branch2c/cond_1/pivot_t/_200" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn4e_branch2c/cond_1" + } +} +node { + name: "bn4e_branch2b/cond_1/pivot_t/_109" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn4e_branch2b/cond_1" + } +} +node { + name: "bn4e_branch2a/cond_1/pivot_t/_74" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn4e_branch2a/cond_1" + } +} +node { + name: "bn4d_branch2c/cond_1/pivot_t/_53" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn4d_branch2c/cond_1" + } +} +node { + name: "bn4d_branch2b/cond_1/pivot_t/_340" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn4d_branch2b/cond_1" + } +} +node { + name: "bn4d_branch2a/cond_1/pivot_t/_144" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn4d_branch2a/cond_1" + } +} +node { + name: "bn4c_branch2c/cond_1/pivot_t/_249" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn4c_branch2c/cond_1" + } +} +node { + name: "bn4c_branch2b/cond_1/pivot_t/_193" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn4c_branch2b/cond_1" + } +} +node { + name: "bn4c_branch2a/cond_1/pivot_t/_102" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn4c_branch2a/cond_1" + } +} +node { + name: "bn4b_branch2c/cond_1/pivot_t/_67" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn4b_branch2c/cond_1" + } +} +node { + name: "bn4b_branch2b/cond_1/pivot_t/_46" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn4b_branch2b/cond_1" + } +} +node { + name: "bn4b_branch2a/cond_1/pivot_t/_11" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn4b_branch2a/cond_1" + } +} +node { + name: "bn4a_branch2c/cond_1/pivot_t/_186" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn4a_branch2c/cond_1" + } +} +node { + name: "bn4a_branch2b/cond_1/pivot_t/_137" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn4a_branch2b/cond_1" + } +} +node { + name: "bn4a_branch2a/cond_1/pivot_t/_95" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn4a_branch2a/cond_1" + } +} +node { + name: "bn4a_branch1/cond_1/pivot_t/_368" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn4a_branch1/cond_1" + } +} +node { + name: "bn3d_branch2c/cond_1/pivot_t/_39" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn3d_branch2c/cond_1" + } +} +node { + name: "bn3d_branch2b/cond_1/pivot_t/_242" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn3d_branch2b/cond_1" + } +} +node { + name: "bn3d_branch2a/cond_1/pivot_t/_361" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn3d_branch2a/cond_1" + } +} +node { + name: "bn3c_branch2c/cond_1/pivot_t/_333" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn3c_branch2c/cond_1" + } +} +node { + name: "bn3c_branch2b/cond_1/pivot_t/_298" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn3c_branch2b/cond_1" + } +} +node { + name: "bn3c_branch2a/cond_1/pivot_t/_326" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn3c_branch2a/cond_1" + } +} +node { + name: "bn3b_branch2c/cond_1/pivot_t/_235" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn3b_branch2c/cond_1" + } +} +node { + name: "bn3b_branch2b/cond_1/pivot_t/_354" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn3b_branch2b/cond_1" + } +} +node { + name: "bn3b_branch2a/cond_1/pivot_t/_179" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn3b_branch2a/cond_1" + } +} +node { + name: "bn3a_branch2c/cond_1/pivot_t/_88" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn3a_branch2c/cond_1" + } +} +node { + name: "bn3a_branch2b/cond_1/pivot_t/_32" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn3a_branch2b/cond_1" + } +} +node { + name: "bn3a_branch2a/cond_1/pivot_t/_4" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn3a_branch2a/cond_1" + } +} +node { + name: "bn3a_branch1/cond_1/pivot_t/_130" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn3a_branch1/cond_1" + } +} +node { + name: "bn2c_branch2c/cond_1/pivot_t/_25" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn2c_branch2c/cond_1" + } +} +node { + name: "bn2c_branch2b/cond_1/pivot_t/_319" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn2c_branch2b/cond_1" + } +} +node { + name: "bn2c_branch2a/cond_1/pivot_t/_228" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn2c_branch2a/cond_1" + } +} +node { + name: "bn2b_branch2c/cond_1/pivot_t/_172" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn2b_branch2c/cond_1" + } +} +node { + name: "bn2b_branch2b/cond_1/pivot_t/_123" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn2b_branch2b/cond_1" + } +} +node { + name: "bn2b_branch2a/cond_1/pivot_t/_291" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn2b_branch2a/cond_1" + } +} +node { + name: "bn2a_branch2c/cond_1/pivot_t/_277" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn2a_branch2c/cond_1" + } +} +node { + name: "bn2a_branch2b/cond_1/pivot_t/_18" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn2a_branch2b/cond_1" + } +} +node { + name: "bn2a_branch2a/cond_1/pivot_t/_221" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn2a_branch2a/cond_1" + } +} +node { + name: "bn2a_branch1/cond_1/pivot_t/_284" + op: "Const" + input: "^ConstantFoldingCtrl/bn_conv1/cond_1/switch_pred/_163_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + tensor_content: "\001" + } + } + } + experimental_debug_info { + original_node_names: "bn2a_branch1/cond_1" + } +} +node { + name: "activation_50/Relu" + op: "Relu" + input: "bn_conv1_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn_conv1_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn_conv1_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn_conv1_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn_conv1_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn_conv1_1/AssignMovingAvg/ReadVariableOp" + input: "bn_conv1_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn_conv1/cond_1/branch_executed/_167" + op: "Merge" + input: "bn_conv1/cond_1/pivot_t/_165" + input: "bn_conv1/cond_1/pivot_f/_164" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn_conv1/cond_1" + } +} +node { + name: "bn5c_branch2c/cond_1/branch_executed/_272" + op: "Merge" + input: "bn5c_branch2c/cond_1/pivot_t/_270" + input: "bn5c_branch2c/cond_1/pivot_f/_269" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn5c_branch2c/cond_1" + } +} +node { + name: "bn5c_branch2b/cond_1/branch_executed/_216" + op: "Merge" + input: "bn5c_branch2b/cond_1/pivot_t/_214" + input: "bn5c_branch2b/cond_1/pivot_f/_213" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn5c_branch2b/cond_1" + } +} +node { + name: "bn5c_branch2a/cond_1/branch_executed/_349" + op: "Merge" + input: "bn5c_branch2a/cond_1/pivot_t/_347" + input: "bn5c_branch2a/cond_1/pivot_f/_346" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn5c_branch2a/cond_1" + } +} +node { + name: "bn5b_branch2c/cond_1/branch_executed/_314" + op: "Merge" + input: "bn5b_branch2c/cond_1/pivot_t/_312" + input: "bn5b_branch2c/cond_1/pivot_f/_311" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn5b_branch2c/cond_1" + } +} +node { + name: "bn5b_branch2b/cond_1/branch_executed/_265" + op: "Merge" + input: "bn5b_branch2b/cond_1/pivot_t/_263" + input: "bn5b_branch2b/cond_1/pivot_f/_262" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn5b_branch2b/cond_1" + } +} +node { + name: "bn5b_branch2a/cond_1/branch_executed/_160" + op: "Merge" + input: "bn5b_branch2a/cond_1/pivot_t/_158" + input: "bn5b_branch2a/cond_1/pivot_f/_157" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn5b_branch2a/cond_1" + } +} +node { + name: "bn5a_branch2c/cond_1/branch_executed/_118" + op: "Merge" + input: "bn5a_branch2c/cond_1/pivot_t/_116" + input: "bn5a_branch2c/cond_1/pivot_f/_115" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn5a_branch2c/cond_1" + } +} +node { + name: "bn5a_branch2b/cond_1/branch_executed/_83" + op: "Merge" + input: "bn5a_branch2b/cond_1/pivot_t/_81" + input: "bn5a_branch2b/cond_1/pivot_f/_80" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn5a_branch2b/cond_1" + } +} +node { + name: "bn5a_branch2a/cond_1/branch_executed/_62" + op: "Merge" + input: "bn5a_branch2a/cond_1/pivot_t/_60" + input: "bn5a_branch2a/cond_1/pivot_f/_59" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn5a_branch2a/cond_1" + } +} +node { + name: "bn5a_branch1/cond_1/branch_executed/_209" + op: "Merge" + input: "bn5a_branch1/cond_1/pivot_t/_207" + input: "bn5a_branch1/cond_1/pivot_f/_206" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn5a_branch1/cond_1" + } +} +node { + name: "bn4f_branch2c/cond_1/branch_executed/_307" + op: "Merge" + input: "bn4f_branch2c/cond_1/pivot_t/_305" + input: "bn4f_branch2c/cond_1/pivot_f/_304" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn4f_branch2c/cond_1" + } +} +node { + name: "bn4f_branch2b/cond_1/branch_executed/_153" + op: "Merge" + input: "bn4f_branch2b/cond_1/pivot_t/_151" + input: "bn4f_branch2b/cond_1/pivot_f/_150" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn4f_branch2b/cond_1" + } +} +node { + name: "bn4f_branch2a/cond_1/branch_executed/_258" + op: "Merge" + input: "bn4f_branch2a/cond_1/pivot_t/_256" + input: "bn4f_branch2a/cond_1/pivot_f/_255" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn4f_branch2a/cond_1" + } +} +node { + name: "bn4e_branch2c/cond_1/branch_executed/_202" + op: "Merge" + input: "bn4e_branch2c/cond_1/pivot_t/_200" + input: "bn4e_branch2c/cond_1/pivot_f/_199" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn4e_branch2c/cond_1" + } +} +node { + name: "bn4e_branch2b/cond_1/branch_executed/_111" + op: "Merge" + input: "bn4e_branch2b/cond_1/pivot_t/_109" + input: "bn4e_branch2b/cond_1/pivot_f/_108" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn4e_branch2b/cond_1" + } +} +node { + name: "bn4e_branch2a/cond_1/branch_executed/_76" + op: "Merge" + input: "bn4e_branch2a/cond_1/pivot_t/_74" + input: "bn4e_branch2a/cond_1/pivot_f/_73" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn4e_branch2a/cond_1" + } +} +node { + name: "bn4d_branch2c/cond_1/branch_executed/_55" + op: "Merge" + input: "bn4d_branch2c/cond_1/pivot_t/_53" + input: "bn4d_branch2c/cond_1/pivot_f/_52" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn4d_branch2c/cond_1" + } +} +node { + name: "bn4d_branch2b/cond_1/branch_executed/_342" + op: "Merge" + input: "bn4d_branch2b/cond_1/pivot_t/_340" + input: "bn4d_branch2b/cond_1/pivot_f/_339" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn4d_branch2b/cond_1" + } +} +node { + name: "bn4d_branch2a/cond_1/branch_executed/_146" + op: "Merge" + input: "bn4d_branch2a/cond_1/pivot_t/_144" + input: "bn4d_branch2a/cond_1/pivot_f/_143" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn4d_branch2a/cond_1" + } +} +node { + name: "bn4c_branch2c/cond_1/branch_executed/_251" + op: "Merge" + input: "bn4c_branch2c/cond_1/pivot_t/_249" + input: "bn4c_branch2c/cond_1/pivot_f/_248" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn4c_branch2c/cond_1" + } +} +node { + name: "bn4c_branch2b/cond_1/branch_executed/_195" + op: "Merge" + input: "bn4c_branch2b/cond_1/pivot_t/_193" + input: "bn4c_branch2b/cond_1/pivot_f/_192" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn4c_branch2b/cond_1" + } +} +node { + name: "bn4c_branch2a/cond_1/branch_executed/_104" + op: "Merge" + input: "bn4c_branch2a/cond_1/pivot_t/_102" + input: "bn4c_branch2a/cond_1/pivot_f/_101" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn4c_branch2a/cond_1" + } +} +node { + name: "bn4b_branch2c/cond_1/branch_executed/_69" + op: "Merge" + input: "bn4b_branch2c/cond_1/pivot_t/_67" + input: "bn4b_branch2c/cond_1/pivot_f/_66" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn4b_branch2c/cond_1" + } +} +node { + name: "bn4b_branch2b/cond_1/branch_executed/_48" + op: "Merge" + input: "bn4b_branch2b/cond_1/pivot_t/_46" + input: "bn4b_branch2b/cond_1/pivot_f/_45" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn4b_branch2b/cond_1" + } +} +node { + name: "bn4b_branch2a/cond_1/branch_executed/_13" + op: "Merge" + input: "bn4b_branch2a/cond_1/pivot_t/_11" + input: "bn4b_branch2a/cond_1/pivot_f/_10" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn4b_branch2a/cond_1" + } +} +node { + name: "bn4a_branch2c/cond_1/branch_executed/_188" + op: "Merge" + input: "bn4a_branch2c/cond_1/pivot_t/_186" + input: "bn4a_branch2c/cond_1/pivot_f/_185" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn4a_branch2c/cond_1" + } +} +node { + name: "bn4a_branch2b/cond_1/branch_executed/_139" + op: "Merge" + input: "bn4a_branch2b/cond_1/pivot_t/_137" + input: "bn4a_branch2b/cond_1/pivot_f/_136" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn4a_branch2b/cond_1" + } +} +node { + name: "bn4a_branch2a/cond_1/branch_executed/_97" + op: "Merge" + input: "bn4a_branch2a/cond_1/pivot_t/_95" + input: "bn4a_branch2a/cond_1/pivot_f/_94" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn4a_branch2a/cond_1" + } +} +node { + name: "bn4a_branch1/cond_1/branch_executed/_370" + op: "Merge" + input: "bn4a_branch1/cond_1/pivot_t/_368" + input: "bn4a_branch1/cond_1/pivot_f/_367" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn4a_branch1/cond_1" + } +} +node { + name: "bn3d_branch2c/cond_1/branch_executed/_41" + op: "Merge" + input: "bn3d_branch2c/cond_1/pivot_t/_39" + input: "bn3d_branch2c/cond_1/pivot_f/_38" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn3d_branch2c/cond_1" + } +} +node { + name: "bn3d_branch2b/cond_1/branch_executed/_244" + op: "Merge" + input: "bn3d_branch2b/cond_1/pivot_t/_242" + input: "bn3d_branch2b/cond_1/pivot_f/_241" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn3d_branch2b/cond_1" + } +} +node { + name: "bn3d_branch2a/cond_1/branch_executed/_363" + op: "Merge" + input: "bn3d_branch2a/cond_1/pivot_t/_361" + input: "bn3d_branch2a/cond_1/pivot_f/_360" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn3d_branch2a/cond_1" + } +} +node { + name: "bn3c_branch2c/cond_1/branch_executed/_335" + op: "Merge" + input: "bn3c_branch2c/cond_1/pivot_t/_333" + input: "bn3c_branch2c/cond_1/pivot_f/_332" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn3c_branch2c/cond_1" + } +} +node { + name: "bn3c_branch2b/cond_1/branch_executed/_300" + op: "Merge" + input: "bn3c_branch2b/cond_1/pivot_t/_298" + input: "bn3c_branch2b/cond_1/pivot_f/_297" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn3c_branch2b/cond_1" + } +} +node { + name: "bn3c_branch2a/cond_1/branch_executed/_328" + op: "Merge" + input: "bn3c_branch2a/cond_1/pivot_t/_326" + input: "bn3c_branch2a/cond_1/pivot_f/_325" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn3c_branch2a/cond_1" + } +} +node { + name: "bn3b_branch2c/cond_1/branch_executed/_237" + op: "Merge" + input: "bn3b_branch2c/cond_1/pivot_t/_235" + input: "bn3b_branch2c/cond_1/pivot_f/_234" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn3b_branch2c/cond_1" + } +} +node { + name: "bn3b_branch2b/cond_1/branch_executed/_356" + op: "Merge" + input: "bn3b_branch2b/cond_1/pivot_t/_354" + input: "bn3b_branch2b/cond_1/pivot_f/_353" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn3b_branch2b/cond_1" + } +} +node { + name: "bn3b_branch2a/cond_1/branch_executed/_181" + op: "Merge" + input: "bn3b_branch2a/cond_1/pivot_t/_179" + input: "bn3b_branch2a/cond_1/pivot_f/_178" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn3b_branch2a/cond_1" + } +} +node { + name: "bn3a_branch2c/cond_1/branch_executed/_90" + op: "Merge" + input: "bn3a_branch2c/cond_1/pivot_t/_88" + input: "bn3a_branch2c/cond_1/pivot_f/_87" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn3a_branch2c/cond_1" + } +} +node { + name: "bn3a_branch2b/cond_1/branch_executed/_34" + op: "Merge" + input: "bn3a_branch2b/cond_1/pivot_t/_32" + input: "bn3a_branch2b/cond_1/pivot_f/_31" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn3a_branch2b/cond_1" + } +} +node { + name: "bn3a_branch2a/cond_1/branch_executed/_6" + op: "Merge" + input: "bn3a_branch2a/cond_1/pivot_t/_4" + input: "bn3a_branch2a/cond_1/pivot_f/_3" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn3a_branch2a/cond_1" + } +} +node { + name: "bn3a_branch1/cond_1/branch_executed/_132" + op: "Merge" + input: "bn3a_branch1/cond_1/pivot_t/_130" + input: "bn3a_branch1/cond_1/pivot_f/_129" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn3a_branch1/cond_1" + } +} +node { + name: "bn2c_branch2c/cond_1/branch_executed/_27" + op: "Merge" + input: "bn2c_branch2c/cond_1/pivot_t/_25" + input: "bn2c_branch2c/cond_1/pivot_f/_24" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn2c_branch2c/cond_1" + } +} +node { + name: "bn2c_branch2b/cond_1/branch_executed/_321" + op: "Merge" + input: "bn2c_branch2b/cond_1/pivot_t/_319" + input: "bn2c_branch2b/cond_1/pivot_f/_318" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn2c_branch2b/cond_1" + } +} +node { + name: "bn2c_branch2a/cond_1/branch_executed/_230" + op: "Merge" + input: "bn2c_branch2a/cond_1/pivot_t/_228" + input: "bn2c_branch2a/cond_1/pivot_f/_227" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn2c_branch2a/cond_1" + } +} +node { + name: "bn2b_branch2c/cond_1/branch_executed/_174" + op: "Merge" + input: "bn2b_branch2c/cond_1/pivot_t/_172" + input: "bn2b_branch2c/cond_1/pivot_f/_171" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn2b_branch2c/cond_1" + } +} +node { + name: "bn2b_branch2b/cond_1/branch_executed/_125" + op: "Merge" + input: "bn2b_branch2b/cond_1/pivot_t/_123" + input: "bn2b_branch2b/cond_1/pivot_f/_122" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn2b_branch2b/cond_1" + } +} +node { + name: "bn2b_branch2a/cond_1/branch_executed/_293" + op: "Merge" + input: "bn2b_branch2a/cond_1/pivot_t/_291" + input: "bn2b_branch2a/cond_1/pivot_f/_290" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn2b_branch2a/cond_1" + } +} +node { + name: "bn2a_branch2c/cond_1/branch_executed/_279" + op: "Merge" + input: "bn2a_branch2c/cond_1/pivot_t/_277" + input: "bn2a_branch2c/cond_1/pivot_f/_276" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn2a_branch2c/cond_1" + } +} +node { + name: "bn2a_branch2b/cond_1/branch_executed/_20" + op: "Merge" + input: "bn2a_branch2b/cond_1/pivot_t/_18" + input: "bn2a_branch2b/cond_1/pivot_f/_17" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn2a_branch2b/cond_1" + } +} +node { + name: "bn2a_branch2a/cond_1/branch_executed/_223" + op: "Merge" + input: "bn2a_branch2a/cond_1/pivot_t/_221" + input: "bn2a_branch2a/cond_1/pivot_f/_220" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn2a_branch2a/cond_1" + } +} +node { + name: "bn2a_branch1/cond_1/branch_executed/_286" + op: "Merge" + input: "bn2a_branch1/cond_1/pivot_t/_284" + input: "bn2a_branch1/cond_1/pivot_f/_283" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_BOOL + } + } + experimental_debug_info { + original_node_names: "bn2a_branch1/cond_1" + } +} +node { + name: "pool1_pad_1/Pad" + op: "Pad" + input: "activation_50/Relu" + input: "pool1_pad_1/Pad/paddings" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "Tpaddings" + value { + type: DT_INT32 + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/pool1_pad_1/Pad_grad/Shape" + op: "Const" + input: "^activation_50/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000@\000\000\000p\000\000\000p\000\000\000" + } + } + } +} +node { + name: "bn_conv1_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn_conv1_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn_conv1_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn_conv1_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "max_pooling2d_1/MaxPool" + op: "MaxPool" + input: "pool1_pad_1/Pad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "ksize" + value { + list { + i: 1 + i: 1 + i: 3 + i: 3 + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } +} +node { + name: "bn_conv1_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn_conv1_cond_input_4" + input: "bn_conv1_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn_conv1_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn_conv1_cond_input_3" + input: "bn_conv1_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2a_branch1_1/Conv2D" + op: "Conv2D" + input: "max_pooling2d_1/MaxPool" + input: "res2a_branch1_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "res2a_branch2a_1/Conv2D" + op: "Conv2D" + input: "max_pooling2d_1/MaxPool" + input: "res2a_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res2a_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^max_pooling2d_1/MaxPool" + input: "^res2a_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000@\000\000\000@\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res2a_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^max_pooling2d_1/MaxPool" + input: "^res2a_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000@\000\000\0008\000\000\0008\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res2a_branch1_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^max_pooling2d_1/MaxPool" + input: "^res2a_branch1_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000@\000\000\0008\000\000\0008\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res2a_branch1_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^max_pooling2d_1/MaxPool" + input: "^res2a_branch1_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000@\000\000\000\000\001\000\000" + } + } + } +} +node { + name: "bn2a_branch1_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res2a_branch1_1/Conv2D" + input: "bn2a_branch1_1/ReadVariableOp" + input: "bn2a_branch1_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn2a_branch2a_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res2a_branch2a_1/Conv2D" + input: "bn2a_branch2a_1/ReadVariableOp" + input: "bn2a_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn2a_branch1_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn2a_branch1_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn2a_branch1_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch1_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn2a_branch1_1/AssignMovingAvg/ReadVariableOp" + input: "bn2a_branch1_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_1_1/Relu" + op: "Relu" + input: "bn2a_branch2a_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn2a_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn2a_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn2a_branch2a_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn2a_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn2a_branch2a_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch1_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn2a_branch1_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch1_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn2a_branch1_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2a_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_1_1/Relu" + input: "res2a_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res2a_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_1_1/Relu" + input: "^res2a_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000@\000\000\000@\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res2a_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_1_1/Relu" + input: "^res2a_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000@\000\000\0008\000\000\0008\000\000\000" + } + } + } +} +node { + name: "bn2a_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn2a_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn2a_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch1_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2a_branch1_cond_input_3" + input: "bn2a_branch1_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch1_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2a_branch1_cond_input_4" + input: "bn2a_branch1_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2b_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res2a_branch2b_1/Conv2D" + input: "bn2a_branch2b_1/ReadVariableOp" + input: "bn2a_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn2a_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2a_branch2a_cond_input_3" + input: "bn2a_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2a_branch2a_cond_input_4" + input: "bn2a_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_2_1/Relu" + op: "Relu" + input: "bn2a_branch2b_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn2a_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn2a_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn2a_branch2b_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn2a_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn2a_branch2b_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2a_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_2_1/Relu" + input: "res2a_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res2a_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_2_1/Relu" + input: "^res2a_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000@\000\000\000\000\001\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res2a_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_2_1/Relu" + input: "^res2a_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000@\000\000\0008\000\000\0008\000\000\000" + } + } + } +} +node { + name: "bn2a_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn2a_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn2a_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2c_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res2a_branch2c_1/Conv2D" + input: "bn2a_branch2c_1/ReadVariableOp" + input: "bn2a_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn2a_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2a_branch2b_cond_input_3" + input: "bn2a_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2a_branch2b_cond_input_4" + input: "bn2a_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "add_16/add" + op: "Add" + input: "bn2a_branch2c_1/FusedBatchNormV2" + input: "bn2a_branch1_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn2a_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn2a_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn2a_branch2c_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn2a_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn2a_branch2c_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_3_1/Relu" + op: "Relu" + input: "add_16/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn2a_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn2a_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn2a_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2b_branch2a_1/Conv2D" + op: "Conv2D" + input: "activation_3_1/Relu" + input: "res2b_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res2b_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_3_1/Relu" + input: "^res2b_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\001\000\0008\000\000\0008\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res2b_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_3_1/Relu" + input: "^res2b_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\001\000\000@\000\000\000" + } + } + } +} +node { + name: "bn2a_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2a_branch2c_cond_input_4" + input: "bn2a_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2a_branch2c_cond_input_3" + input: "bn2a_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2a_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res2b_branch2a_1/Conv2D" + input: "bn2b_branch2a_1/ReadVariableOp" + input: "bn2b_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "activation_4_1/Relu" + op: "Relu" + input: "bn2b_branch2a_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn2b_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn2b_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn2b_branch2a_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn2b_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn2b_branch2a_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2b_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_4_1/Relu" + input: "res2b_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res2b_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_4_1/Relu" + input: "^res2b_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000@\000\000\0008\000\000\0008\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res2b_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_4_1/Relu" + input: "^res2b_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000@\000\000\000@\000\000\000" + } + } + } +} +node { + name: "bn2b_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn2b_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn2b_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2b_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res2b_branch2b_1/Conv2D" + input: "bn2b_branch2b_1/ReadVariableOp" + input: "bn2b_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn2b_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2b_branch2a_cond_input_4" + input: "bn2b_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2b_branch2a_cond_input_3" + input: "bn2b_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_5_1/Relu" + op: "Relu" + input: "bn2b_branch2b_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn2b_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn2b_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn2b_branch2b_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn2b_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn2b_branch2b_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2b_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_5_1/Relu" + input: "res2b_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res2b_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_5_1/Relu" + input: "^res2b_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000@\000\000\0008\000\000\0008\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res2b_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_5_1/Relu" + input: "^res2b_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000@\000\000\000\000\001\000\000" + } + } + } +} +node { + name: "bn2b_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn2b_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn2b_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2c_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res2b_branch2c_1/Conv2D" + input: "bn2b_branch2c_1/ReadVariableOp" + input: "bn2b_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn2b_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2b_branch2b_cond_input_4" + input: "bn2b_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2b_branch2b_cond_input_3" + input: "bn2b_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "add_1_1/add" + op: "Add" + input: "bn2b_branch2c_1/FusedBatchNormV2" + input: "activation_3_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn2b_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn2b_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn2b_branch2c_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn2b_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn2b_branch2c_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_6_1/Relu" + op: "Relu" + input: "add_1_1/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn2b_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn2b_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn2b_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2c_branch2a_1/Conv2D" + op: "Conv2D" + input: "activation_6_1/Relu" + input: "res2c_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res2c_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_6_1/Relu" + input: "^res2c_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\001\000\0008\000\000\0008\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res2c_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_6_1/Relu" + input: "^res2c_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\001\000\000@\000\000\000" + } + } + } +} +node { + name: "bn2b_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2b_branch2c_cond_input_3" + input: "bn2b_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2b_branch2c_cond_input_4" + input: "bn2b_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2a_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res2c_branch2a_1/Conv2D" + input: "bn2c_branch2a_1/ReadVariableOp" + input: "bn2c_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "activation_7_1/Relu" + op: "Relu" + input: "bn2c_branch2a_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn2c_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn2c_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn2c_branch2a_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn2c_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn2c_branch2a_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2c_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_7_1/Relu" + input: "res2c_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res2c_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_7_1/Relu" + input: "^res2c_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000@\000\000\0008\000\000\0008\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res2c_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_7_1/Relu" + input: "^res2c_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000@\000\000\000@\000\000\000" + } + } + } +} +node { + name: "bn2c_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn2c_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn2c_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2b_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res2c_branch2b_1/Conv2D" + input: "bn2c_branch2b_1/ReadVariableOp" + input: "bn2c_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn2c_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2c_branch2a_cond_input_3" + input: "bn2c_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2c_branch2a_cond_input_4" + input: "bn2c_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_8_1/Relu" + op: "Relu" + input: "bn2c_branch2b_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn2c_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn2c_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn2c_branch2b_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn2c_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn2c_branch2b_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2c_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_8_1/Relu" + input: "res2c_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res2c_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_8_1/Relu" + input: "^res2c_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000@\000\000\0008\000\000\0008\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res2c_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_8_1/Relu" + input: "^res2c_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000@\000\000\000\000\001\000\000" + } + } + } +} +node { + name: "bn2c_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn2c_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn2c_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2c_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res2c_branch2c_1/Conv2D" + input: "bn2c_branch2c_1/ReadVariableOp" + input: "bn2c_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn2c_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2c_branch2b_cond_input_4" + input: "bn2c_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2c_branch2b_cond_input_3" + input: "bn2c_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "add_2_1/add" + op: "Add" + input: "bn2c_branch2c_1/FusedBatchNormV2" + input: "activation_6_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn2c_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn2c_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn2c_branch2c_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn2c_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn2c_branch2c_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_9_1/Relu" + op: "Relu" + input: "add_2_1/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn2c_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn2c_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn2c_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3a_branch1_1/Conv2D" + op: "Conv2D" + input: "activation_9_1/Relu" + input: "res3a_branch1_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "res3a_branch2a_1/Conv2D" + op: "Conv2D" + input: "activation_9_1/Relu" + input: "res3a_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res3a_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_9_1/Relu" + input: "^res3a_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\001\000\0008\000\000\0008\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res3a_branch1_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_9_1/Relu" + input: "^res3a_branch1_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\001\000\0008\000\000\0008\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res3a_branch1_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_9_1/Relu" + input: "^res3a_branch1_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\001\000\000\000\002\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res3a_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_9_1/Relu" + input: "^res3a_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\001\000\000\200\000\000\000" + } + } + } +} +node { + name: "bn2c_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2c_branch2c_cond_input_3" + input: "bn2c_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2c_branch2c_cond_input_4" + input: "bn2c_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch1_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res3a_branch1_1/Conv2D" + input: "bn3a_branch1_1/ReadVariableOp" + input: "bn3a_branch1_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn3a_branch2a_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res3a_branch2a_1/Conv2D" + input: "bn3a_branch2a_1/ReadVariableOp" + input: "bn3a_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn3a_branch1_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn3a_branch1_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn3a_branch1_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch1_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn3a_branch1_1/AssignMovingAvg/ReadVariableOp" + input: "bn3a_branch1_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_10_1/Relu" + op: "Relu" + input: "bn3a_branch2a_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn3a_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn3a_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn3a_branch2a_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn3a_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn3a_branch2a_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch1_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn3a_branch1_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch1_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn3a_branch1_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3a_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_10_1/Relu" + input: "res3a_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res3a_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_10_1/Relu" + input: "^res3a_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\200\000\000\0008\000\000\0008\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res3a_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_10_1/Relu" + input: "^res3a_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000\200\000\000\000\200\000\000\000" + } + } + } +} +node { + name: "bn3a_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn3a_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn3a_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch1_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3a_branch1_cond_input_3" + input: "bn3a_branch1_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch1_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3a_branch1_cond_input_4" + input: "bn3a_branch1_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2b_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res3a_branch2b_1/Conv2D" + input: "bn3a_branch2b_1/ReadVariableOp" + input: "bn3a_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn3a_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3a_branch2a_cond_input_4" + input: "bn3a_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3a_branch2a_cond_input_3" + input: "bn3a_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_11_1/Relu" + op: "Relu" + input: "bn3a_branch2b_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn3a_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn3a_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn3a_branch2b_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn3a_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn3a_branch2b_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3a_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_11_1/Relu" + input: "res3a_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res3a_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_11_1/Relu" + input: "^res3a_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\200\000\000\000\034\000\000\000\034\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res3a_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_11_1/Relu" + input: "^res3a_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\200\000\000\000\000\002\000\000" + } + } + } +} +node { + name: "bn3a_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn3a_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn3a_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2c_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res3a_branch2c_1/Conv2D" + input: "bn3a_branch2c_1/ReadVariableOp" + input: "bn3a_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn3a_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3a_branch2b_cond_input_4" + input: "bn3a_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3a_branch2b_cond_input_3" + input: "bn3a_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "add_3_1/add" + op: "Add" + input: "bn3a_branch2c_1/FusedBatchNormV2" + input: "bn3a_branch1_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn3a_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn3a_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn3a_branch2c_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn3a_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn3a_branch2c_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_12_1/Relu" + op: "Relu" + input: "add_3_1/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn3a_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn3a_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn3a_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3b_branch2a_1/Conv2D" + op: "Conv2D" + input: "activation_12_1/Relu" + input: "res3b_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res3b_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_12_1/Relu" + input: "^res3b_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\002\000\000\034\000\000\000\034\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res3b_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_12_1/Relu" + input: "^res3b_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\002\000\000\200\000\000\000" + } + } + } +} +node { + name: "bn3a_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3a_branch2c_cond_input_3" + input: "bn3a_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3a_branch2c_cond_input_4" + input: "bn3a_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2a_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res3b_branch2a_1/Conv2D" + input: "bn3b_branch2a_1/ReadVariableOp" + input: "bn3b_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "activation_13_1/Relu" + op: "Relu" + input: "bn3b_branch2a_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn3b_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn3b_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn3b_branch2a_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn3b_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn3b_branch2a_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3b_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_13_1/Relu" + input: "res3b_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res3b_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_13_1/Relu" + input: "^res3b_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\200\000\000\000\034\000\000\000\034\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res3b_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_13_1/Relu" + input: "^res3b_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000\200\000\000\000\200\000\000\000" + } + } + } +} +node { + name: "bn3b_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn3b_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn3b_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2b_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res3b_branch2b_1/Conv2D" + input: "bn3b_branch2b_1/ReadVariableOp" + input: "bn3b_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn3b_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3b_branch2a_cond_input_3" + input: "bn3b_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3b_branch2a_cond_input_4" + input: "bn3b_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_14_1/Relu" + op: "Relu" + input: "bn3b_branch2b_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn3b_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn3b_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn3b_branch2b_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn3b_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn3b_branch2b_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3b_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_14_1/Relu" + input: "res3b_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res3b_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_14_1/Relu" + input: "^res3b_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\200\000\000\000\034\000\000\000\034\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res3b_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_14_1/Relu" + input: "^res3b_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\200\000\000\000\000\002\000\000" + } + } + } +} +node { + name: "bn3b_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn3b_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn3b_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2c_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res3b_branch2c_1/Conv2D" + input: "bn3b_branch2c_1/ReadVariableOp" + input: "bn3b_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn3b_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3b_branch2b_cond_input_4" + input: "bn3b_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3b_branch2b_cond_input_3" + input: "bn3b_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "add_4_1/add" + op: "Add" + input: "bn3b_branch2c_1/FusedBatchNormV2" + input: "activation_12_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn3b_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn3b_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn3b_branch2c_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn3b_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn3b_branch2c_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_15_1/Relu" + op: "Relu" + input: "add_4_1/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn3b_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn3b_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn3b_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3c_branch2a_1/Conv2D" + op: "Conv2D" + input: "activation_15_1/Relu" + input: "res3c_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res3c_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_15_1/Relu" + input: "^res3c_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\002\000\000\034\000\000\000\034\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res3c_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_15_1/Relu" + input: "^res3c_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\002\000\000\200\000\000\000" + } + } + } +} +node { + name: "bn3b_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3b_branch2c_cond_input_3" + input: "bn3b_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3b_branch2c_cond_input_4" + input: "bn3b_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2a_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res3c_branch2a_1/Conv2D" + input: "bn3c_branch2a_1/ReadVariableOp" + input: "bn3c_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "activation_16_1/Relu" + op: "Relu" + input: "bn3c_branch2a_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn3c_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn3c_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn3c_branch2a_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn3c_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn3c_branch2a_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3c_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_16_1/Relu" + input: "res3c_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res3c_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_16_1/Relu" + input: "^res3c_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\200\000\000\000\034\000\000\000\034\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res3c_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_16_1/Relu" + input: "^res3c_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000\200\000\000\000\200\000\000\000" + } + } + } +} +node { + name: "bn3c_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn3c_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn3c_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2b_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res3c_branch2b_1/Conv2D" + input: "bn3c_branch2b_1/ReadVariableOp" + input: "bn3c_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn3c_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3c_branch2a_cond_input_3" + input: "bn3c_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3c_branch2a_cond_input_4" + input: "bn3c_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_17_1/Relu" + op: "Relu" + input: "bn3c_branch2b_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn3c_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn3c_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn3c_branch2b_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn3c_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn3c_branch2b_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3c_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_17_1/Relu" + input: "res3c_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res3c_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_17_1/Relu" + input: "^res3c_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\200\000\000\000\034\000\000\000\034\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res3c_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_17_1/Relu" + input: "^res3c_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\200\000\000\000\000\002\000\000" + } + } + } +} +node { + name: "bn3c_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn3c_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn3c_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2c_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res3c_branch2c_1/Conv2D" + input: "bn3c_branch2c_1/ReadVariableOp" + input: "bn3c_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn3c_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3c_branch2b_cond_input_4" + input: "bn3c_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3c_branch2b_cond_input_3" + input: "bn3c_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "add_5_1/add" + op: "Add" + input: "bn3c_branch2c_1/FusedBatchNormV2" + input: "activation_15_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn3c_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn3c_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn3c_branch2c_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn3c_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn3c_branch2c_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_18_1/Relu" + op: "Relu" + input: "add_5_1/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn3c_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn3c_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn3c_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3d_branch2a_1/Conv2D" + op: "Conv2D" + input: "activation_18_1/Relu" + input: "res3d_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res3d_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_18_1/Relu" + input: "^res3d_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\002\000\000\034\000\000\000\034\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res3d_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_18_1/Relu" + input: "^res3d_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\002\000\000\200\000\000\000" + } + } + } +} +node { + name: "bn3c_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3c_branch2c_cond_input_3" + input: "bn3c_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3c_branch2c_cond_input_4" + input: "bn3c_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2a_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res3d_branch2a_1/Conv2D" + input: "bn3d_branch2a_1/ReadVariableOp" + input: "bn3d_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "activation_19_1/Relu" + op: "Relu" + input: "bn3d_branch2a_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn3d_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn3d_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn3d_branch2a_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn3d_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn3d_branch2a_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3d_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_19_1/Relu" + input: "res3d_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res3d_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_19_1/Relu" + input: "^res3d_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\200\000\000\000\034\000\000\000\034\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res3d_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_19_1/Relu" + input: "^res3d_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000\200\000\000\000\200\000\000\000" + } + } + } +} +node { + name: "bn3d_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn3d_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn3d_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2b_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res3d_branch2b_1/Conv2D" + input: "bn3d_branch2b_1/ReadVariableOp" + input: "bn3d_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn3d_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3d_branch2a_cond_input_4" + input: "bn3d_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3d_branch2a_cond_input_3" + input: "bn3d_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_20_1/Relu" + op: "Relu" + input: "bn3d_branch2b_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn3d_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn3d_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn3d_branch2b_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn3d_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn3d_branch2b_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3d_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_20_1/Relu" + input: "res3d_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res3d_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_20_1/Relu" + input: "^res3d_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\200\000\000\000\034\000\000\000\034\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res3d_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_20_1/Relu" + input: "^res3d_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\200\000\000\000\000\002\000\000" + } + } + } +} +node { + name: "bn3d_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn3d_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn3d_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2c_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res3d_branch2c_1/Conv2D" + input: "bn3d_branch2c_1/ReadVariableOp" + input: "bn3d_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn3d_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3d_branch2b_cond_input_4" + input: "bn3d_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3d_branch2b_cond_input_3" + input: "bn3d_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "add_6_1/add" + op: "Add" + input: "bn3d_branch2c_1/FusedBatchNormV2" + input: "activation_18_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn3d_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn3d_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn3d_branch2c_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn3d_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn3d_branch2c_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_21_1/Relu" + op: "Relu" + input: "add_6_1/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn3d_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn3d_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn3d_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4a_branch1_1/Conv2D" + op: "Conv2D" + input: "activation_21_1/Relu" + input: "res4a_branch1_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "res4a_branch2a_1/Conv2D" + op: "Conv2D" + input: "activation_21_1/Relu" + input: "res4a_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4a_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_21_1/Relu" + input: "^res4a_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\002\000\000\034\000\000\000\034\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4a_branch1_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_21_1/Relu" + input: "^res4a_branch1_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\002\000\000\034\000\000\000\034\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4a_branch1_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_21_1/Relu" + input: "^res4a_branch1_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\002\000\000\000\004\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4a_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_21_1/Relu" + input: "^res4a_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\002\000\000\000\001\000\000" + } + } + } +} +node { + name: "bn3d_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3d_branch2c_cond_input_3" + input: "bn3d_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3d_branch2c_cond_input_4" + input: "bn3d_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch1_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res4a_branch1_1/Conv2D" + input: "bn4a_branch1_1/ReadVariableOp" + input: "bn4a_branch1_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4a_branch2a_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res4a_branch2a_1/Conv2D" + input: "bn4a_branch2a_1/ReadVariableOp" + input: "bn4a_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4a_branch1_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4a_branch1_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4a_branch1_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch1_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4a_branch1_1/AssignMovingAvg/ReadVariableOp" + input: "bn4a_branch1_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_22_1/Relu" + op: "Relu" + input: "bn4a_branch2a_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn4a_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4a_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4a_branch2a_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4a_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn4a_branch2a_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch1_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4a_branch1_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch1_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4a_branch1_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4a_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_22_1/Relu" + input: "res4a_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4a_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_22_1/Relu" + input: "^res4a_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\001\000\000\034\000\000\000\034\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4a_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_22_1/Relu" + input: "^res4a_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000\000\001\000\000\000\001\000\000" + } + } + } +} +node { + name: "bn4a_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4a_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4a_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch1_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4a_branch1_cond_input_4" + input: "bn4a_branch1_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch1_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4a_branch1_cond_input_3" + input: "bn4a_branch1_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2b_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res4a_branch2b_1/Conv2D" + input: "bn4a_branch2b_1/ReadVariableOp" + input: "bn4a_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4a_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4a_branch2a_cond_input_4" + input: "bn4a_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4a_branch2a_cond_input_3" + input: "bn4a_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_23_1/Relu" + op: "Relu" + input: "bn4a_branch2b_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn4a_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4a_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4a_branch2b_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4a_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn4a_branch2b_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4a_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_23_1/Relu" + input: "res4a_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4a_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_23_1/Relu" + input: "^res4a_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\001\000\000\016\000\000\000\016\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4a_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_23_1/Relu" + input: "^res4a_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\001\000\000\000\004\000\000" + } + } + } +} +node { + name: "bn4a_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4a_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4a_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2c_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res4a_branch2c_1/Conv2D" + input: "bn4a_branch2c_1/ReadVariableOp" + input: "bn4a_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4a_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4a_branch2b_cond_input_4" + input: "bn4a_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4a_branch2b_cond_input_3" + input: "bn4a_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "add_7_1/add" + op: "Add" + input: "bn4a_branch2c_1/FusedBatchNormV2" + input: "bn4a_branch1_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn4a_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4a_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4a_branch2c_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4a_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn4a_branch2c_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_24_1/Relu" + op: "Relu" + input: "add_7_1/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn4a_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4a_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4a_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4b_branch2a_1/Conv2D" + op: "Conv2D" + input: "activation_24_1/Relu" + input: "res4b_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4b_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_24_1/Relu" + input: "^res4b_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\004\000\000\016\000\000\000\016\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4b_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_24_1/Relu" + input: "^res4b_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\004\000\000\000\001\000\000" + } + } + } +} +node { + name: "bn4a_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4a_branch2c_cond_input_4" + input: "bn4a_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4a_branch2c_cond_input_3" + input: "bn4a_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2a_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res4b_branch2a_1/Conv2D" + input: "bn4b_branch2a_1/ReadVariableOp" + input: "bn4b_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "activation_25_1/Relu" + op: "Relu" + input: "bn4b_branch2a_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn4b_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4b_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4b_branch2a_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4b_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn4b_branch2a_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4b_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_25_1/Relu" + input: "res4b_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4b_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_25_1/Relu" + input: "^res4b_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\001\000\000\016\000\000\000\016\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4b_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_25_1/Relu" + input: "^res4b_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000\000\001\000\000\000\001\000\000" + } + } + } +} +node { + name: "bn4b_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4b_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4b_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2b_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res4b_branch2b_1/Conv2D" + input: "bn4b_branch2b_1/ReadVariableOp" + input: "bn4b_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4b_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4b_branch2a_cond_input_3" + input: "bn4b_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4b_branch2a_cond_input_4" + input: "bn4b_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_26_1/Relu" + op: "Relu" + input: "bn4b_branch2b_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn4b_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4b_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4b_branch2b_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4b_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn4b_branch2b_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4b_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_26_1/Relu" + input: "res4b_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4b_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_26_1/Relu" + input: "^res4b_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\001\000\000\000\004\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4b_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_26_1/Relu" + input: "^res4b_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\001\000\000\016\000\000\000\016\000\000\000" + } + } + } +} +node { + name: "bn4b_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4b_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4b_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2c_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res4b_branch2c_1/Conv2D" + input: "bn4b_branch2c_1/ReadVariableOp" + input: "bn4b_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4b_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4b_branch2b_cond_input_3" + input: "bn4b_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4b_branch2b_cond_input_4" + input: "bn4b_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "add_8_1/add" + op: "Add" + input: "bn4b_branch2c_1/FusedBatchNormV2" + input: "activation_24_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn4b_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4b_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4b_branch2c_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4b_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn4b_branch2c_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_27_1/Relu" + op: "Relu" + input: "add_8_1/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn4b_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4b_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4b_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4c_branch2a_1/Conv2D" + op: "Conv2D" + input: "activation_27_1/Relu" + input: "res4c_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4c_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_27_1/Relu" + input: "^res4c_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\004\000\000\000\001\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4c_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_27_1/Relu" + input: "^res4c_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\004\000\000\016\000\000\000\016\000\000\000" + } + } + } +} +node { + name: "bn4b_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4b_branch2c_cond_input_4" + input: "bn4b_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4b_branch2c_cond_input_3" + input: "bn4b_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2a_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res4c_branch2a_1/Conv2D" + input: "bn4c_branch2a_1/ReadVariableOp" + input: "bn4c_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "activation_28_1/Relu" + op: "Relu" + input: "bn4c_branch2a_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn4c_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4c_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4c_branch2a_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4c_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn4c_branch2a_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4c_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_28_1/Relu" + input: "res4c_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4c_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_28_1/Relu" + input: "^res4c_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000\000\001\000\000\000\001\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4c_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_28_1/Relu" + input: "^res4c_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\001\000\000\016\000\000\000\016\000\000\000" + } + } + } +} +node { + name: "bn4c_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4c_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4c_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2b_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res4c_branch2b_1/Conv2D" + input: "bn4c_branch2b_1/ReadVariableOp" + input: "bn4c_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4c_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4c_branch2a_cond_input_3" + input: "bn4c_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4c_branch2a_cond_input_4" + input: "bn4c_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_29_1/Relu" + op: "Relu" + input: "bn4c_branch2b_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn4c_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4c_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4c_branch2b_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4c_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn4c_branch2b_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4c_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_29_1/Relu" + input: "res4c_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4c_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_29_1/Relu" + input: "^res4c_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\001\000\000\000\004\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4c_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_29_1/Relu" + input: "^res4c_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\001\000\000\016\000\000\000\016\000\000\000" + } + } + } +} +node { + name: "bn4c_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4c_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4c_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2c_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res4c_branch2c_1/Conv2D" + input: "bn4c_branch2c_1/ReadVariableOp" + input: "bn4c_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4c_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4c_branch2b_cond_input_3" + input: "bn4c_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4c_branch2b_cond_input_4" + input: "bn4c_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "add_9_1/add" + op: "Add" + input: "bn4c_branch2c_1/FusedBatchNormV2" + input: "activation_27_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn4c_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4c_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4c_branch2c_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4c_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn4c_branch2c_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_30_1/Relu" + op: "Relu" + input: "add_9_1/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn4c_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4c_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4c_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4d_branch2a_1/Conv2D" + op: "Conv2D" + input: "activation_30_1/Relu" + input: "res4d_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4d_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_30_1/Relu" + input: "^res4d_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\004\000\000\016\000\000\000\016\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4d_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_30_1/Relu" + input: "^res4d_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\004\000\000\000\001\000\000" + } + } + } +} +node { + name: "bn4c_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4c_branch2c_cond_input_4" + input: "bn4c_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4c_branch2c_cond_input_3" + input: "bn4c_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2a_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res4d_branch2a_1/Conv2D" + input: "bn4d_branch2a_1/ReadVariableOp" + input: "bn4d_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "activation_31_1/Relu" + op: "Relu" + input: "bn4d_branch2a_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn4d_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4d_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4d_branch2a_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4d_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn4d_branch2a_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4d_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_31_1/Relu" + input: "res4d_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4d_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_31_1/Relu" + input: "^res4d_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\001\000\000\016\000\000\000\016\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4d_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_31_1/Relu" + input: "^res4d_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000\000\001\000\000\000\001\000\000" + } + } + } +} +node { + name: "bn4d_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4d_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4d_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2b_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res4d_branch2b_1/Conv2D" + input: "bn4d_branch2b_1/ReadVariableOp" + input: "bn4d_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4d_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4d_branch2a_cond_input_3" + input: "bn4d_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4d_branch2a_cond_input_4" + input: "bn4d_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_32_1/Relu" + op: "Relu" + input: "bn4d_branch2b_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn4d_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4d_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4d_branch2b_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4d_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn4d_branch2b_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4d_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_32_1/Relu" + input: "res4d_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4d_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_32_1/Relu" + input: "^res4d_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\001\000\000\016\000\000\000\016\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4d_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_32_1/Relu" + input: "^res4d_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\001\000\000\000\004\000\000" + } + } + } +} +node { + name: "bn4d_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4d_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4d_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2c_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res4d_branch2c_1/Conv2D" + input: "bn4d_branch2c_1/ReadVariableOp" + input: "bn4d_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4d_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4d_branch2b_cond_input_3" + input: "bn4d_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4d_branch2b_cond_input_4" + input: "bn4d_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "add_10_1/add" + op: "Add" + input: "bn4d_branch2c_1/FusedBatchNormV2" + input: "activation_30_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn4d_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4d_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4d_branch2c_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4d_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn4d_branch2c_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_33_1/Relu" + op: "Relu" + input: "add_10_1/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn4d_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4d_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4d_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4e_branch2a_1/Conv2D" + op: "Conv2D" + input: "activation_33_1/Relu" + input: "res4e_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4e_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_33_1/Relu" + input: "^res4e_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\004\000\000\016\000\000\000\016\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4e_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_33_1/Relu" + input: "^res4e_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\004\000\000\000\001\000\000" + } + } + } +} +node { + name: "bn4d_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4d_branch2c_cond_input_4" + input: "bn4d_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4d_branch2c_cond_input_3" + input: "bn4d_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2a_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res4e_branch2a_1/Conv2D" + input: "bn4e_branch2a_1/ReadVariableOp" + input: "bn4e_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "activation_34_1/Relu" + op: "Relu" + input: "bn4e_branch2a_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn4e_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4e_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4e_branch2a_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4e_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn4e_branch2a_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4e_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_34_1/Relu" + input: "res4e_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4e_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_34_1/Relu" + input: "^res4e_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\001\000\000\016\000\000\000\016\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4e_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_34_1/Relu" + input: "^res4e_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000\000\001\000\000\000\001\000\000" + } + } + } +} +node { + name: "bn4e_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4e_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4e_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2b_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res4e_branch2b_1/Conv2D" + input: "bn4e_branch2b_1/ReadVariableOp" + input: "bn4e_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4e_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4e_branch2a_cond_input_3" + input: "bn4e_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4e_branch2a_cond_input_4" + input: "bn4e_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_35_1/Relu" + op: "Relu" + input: "bn4e_branch2b_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn4e_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4e_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4e_branch2b_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4e_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn4e_branch2b_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4e_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_35_1/Relu" + input: "res4e_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4e_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_35_1/Relu" + input: "^res4e_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\001\000\000\016\000\000\000\016\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4e_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_35_1/Relu" + input: "^res4e_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\001\000\000\000\004\000\000" + } + } + } +} +node { + name: "bn4e_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4e_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4e_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2c_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res4e_branch2c_1/Conv2D" + input: "bn4e_branch2c_1/ReadVariableOp" + input: "bn4e_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4e_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4e_branch2b_cond_input_4" + input: "bn4e_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4e_branch2b_cond_input_3" + input: "bn4e_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "add_11_1/add" + op: "Add" + input: "bn4e_branch2c_1/FusedBatchNormV2" + input: "activation_33_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn4e_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4e_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4e_branch2c_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4e_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn4e_branch2c_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_36_1/Relu" + op: "Relu" + input: "add_11_1/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn4e_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4e_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4e_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4f_branch2a_1/Conv2D" + op: "Conv2D" + input: "activation_36_1/Relu" + input: "res4f_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4f_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_36_1/Relu" + input: "^res4f_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\004\000\000\016\000\000\000\016\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4f_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_36_1/Relu" + input: "^res4f_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\004\000\000\000\001\000\000" + } + } + } +} +node { + name: "bn4e_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4e_branch2c_cond_input_4" + input: "bn4e_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4e_branch2c_cond_input_3" + input: "bn4e_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2a_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res4f_branch2a_1/Conv2D" + input: "bn4f_branch2a_1/ReadVariableOp" + input: "bn4f_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "activation_37_1/Relu" + op: "Relu" + input: "bn4f_branch2a_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn4f_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4f_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4f_branch2a_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4f_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn4f_branch2a_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4f_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_37_1/Relu" + input: "res4f_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4f_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_37_1/Relu" + input: "^res4f_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\001\000\000\016\000\000\000\016\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4f_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_37_1/Relu" + input: "^res4f_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000\000\001\000\000\000\001\000\000" + } + } + } +} +node { + name: "bn4f_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4f_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4f_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2b_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res4f_branch2b_1/Conv2D" + input: "bn4f_branch2b_1/ReadVariableOp" + input: "bn4f_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4f_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4f_branch2a_cond_input_3" + input: "bn4f_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4f_branch2a_cond_input_4" + input: "bn4f_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_38_1/Relu" + op: "Relu" + input: "bn4f_branch2b_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn4f_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4f_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4f_branch2b_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4f_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn4f_branch2b_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4f_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_38_1/Relu" + input: "res4f_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4f_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_38_1/Relu" + input: "^res4f_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\001\000\000\016\000\000\000\016\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res4f_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_38_1/Relu" + input: "^res4f_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\001\000\000\000\004\000\000" + } + } + } +} +node { + name: "bn4f_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4f_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4f_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2c_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res4f_branch2c_1/Conv2D" + input: "bn4f_branch2c_1/ReadVariableOp" + input: "bn4f_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4f_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4f_branch2b_cond_input_4" + input: "bn4f_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4f_branch2b_cond_input_3" + input: "bn4f_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "add_12_1/add" + op: "Add" + input: "bn4f_branch2c_1/FusedBatchNormV2" + input: "activation_36_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn4f_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4f_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4f_branch2c_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4f_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn4f_branch2c_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_39_1/Relu" + op: "Relu" + input: "add_12_1/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn4f_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4f_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4f_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5a_branch1_1/Conv2D" + op: "Conv2D" + input: "activation_39_1/Relu" + input: "res5a_branch1_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "res5a_branch2a_1/Conv2D" + op: "Conv2D" + input: "activation_39_1/Relu" + input: "res5a_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res5a_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_39_1/Relu" + input: "^res5a_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\004\000\000\016\000\000\000\016\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res5a_branch1_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_39_1/Relu" + input: "^res5a_branch1_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\004\000\000\016\000\000\000\016\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res5a_branch1_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_39_1/Relu" + input: "^res5a_branch1_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\004\000\000\000\010\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res5a_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_39_1/Relu" + input: "^res5a_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\004\000\000\000\002\000\000" + } + } + } +} +node { + name: "bn4f_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4f_branch2c_cond_input_4" + input: "bn4f_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4f_branch2c_cond_input_3" + input: "bn4f_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch1_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res5a_branch1_1/Conv2D" + input: "bn5a_branch1_1/ReadVariableOp" + input: "bn5a_branch1_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn5a_branch2a_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res5a_branch2a_1/Conv2D" + input: "bn5a_branch2a_1/ReadVariableOp" + input: "bn5a_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn5a_branch1_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn5a_branch1_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn5a_branch1_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch1_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn5a_branch1_1/AssignMovingAvg/ReadVariableOp" + input: "bn5a_branch1_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_40_1/Relu" + op: "Relu" + input: "bn5a_branch2a_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn5a_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn5a_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn5a_branch2a_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn5a_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn5a_branch2a_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch1_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn5a_branch1_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch1_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn5a_branch1_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5a_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_40_1/Relu" + input: "res5a_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res5a_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_40_1/Relu" + input: "^res5a_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\002\000\000\016\000\000\000\016\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res5a_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_40_1/Relu" + input: "^res5a_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000\000\002\000\000\000\002\000\000" + } + } + } +} +node { + name: "bn5a_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn5a_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn5a_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch1_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5a_branch1_cond_input_3" + input: "bn5a_branch1_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch1_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5a_branch1_cond_input_4" + input: "bn5a_branch1_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2b_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res5a_branch2b_1/Conv2D" + input: "bn5a_branch2b_1/ReadVariableOp" + input: "bn5a_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn5a_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5a_branch2a_cond_input_3" + input: "bn5a_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5a_branch2a_cond_input_4" + input: "bn5a_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_41_1/Relu" + op: "Relu" + input: "bn5a_branch2b_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn5a_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn5a_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn5a_branch2b_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn5a_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn5a_branch2b_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5a_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_41_1/Relu" + input: "res5a_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res5a_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_41_1/Relu" + input: "^res5a_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\002\000\000\007\000\000\000\007\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res5a_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_41_1/Relu" + input: "^res5a_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\002\000\000\000\010\000\000" + } + } + } +} +node { + name: "bn5a_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn5a_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn5a_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2c_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res5a_branch2c_1/Conv2D" + input: "bn5a_branch2c_1/ReadVariableOp" + input: "bn5a_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn5a_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5a_branch2b_cond_input_3" + input: "bn5a_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5a_branch2b_cond_input_4" + input: "bn5a_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "add_13_1/add" + op: "Add" + input: "bn5a_branch2c_1/FusedBatchNormV2" + input: "bn5a_branch1_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn5a_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn5a_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn5a_branch2c_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn5a_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn5a_branch2c_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_42_1/Relu" + op: "Relu" + input: "add_13_1/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn5a_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn5a_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn5a_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5b_branch2a_1/Conv2D" + op: "Conv2D" + input: "activation_42_1/Relu" + input: "res5b_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res5b_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_42_1/Relu" + input: "^res5b_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\010\000\000\007\000\000\000\007\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res5b_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_42_1/Relu" + input: "^res5b_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\010\000\000\000\002\000\000" + } + } + } +} +node { + name: "bn5a_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5a_branch2c_cond_input_3" + input: "bn5a_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5a_branch2c_cond_input_4" + input: "bn5a_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2a_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res5b_branch2a_1/Conv2D" + input: "bn5b_branch2a_1/ReadVariableOp" + input: "bn5b_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "activation_43_1/Relu" + op: "Relu" + input: "bn5b_branch2a_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn5b_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn5b_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn5b_branch2a_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn5b_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn5b_branch2a_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5b_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_43_1/Relu" + input: "res5b_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res5b_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_43_1/Relu" + input: "^res5b_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\002\000\000\007\000\000\000\007\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res5b_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_43_1/Relu" + input: "^res5b_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000\000\002\000\000\000\002\000\000" + } + } + } +} +node { + name: "bn5b_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn5b_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn5b_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2b_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res5b_branch2b_1/Conv2D" + input: "bn5b_branch2b_1/ReadVariableOp" + input: "bn5b_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn5b_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5b_branch2a_cond_input_4" + input: "bn5b_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5b_branch2a_cond_input_3" + input: "bn5b_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_44_1/Relu" + op: "Relu" + input: "bn5b_branch2b_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn5b_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn5b_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn5b_branch2b_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn5b_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn5b_branch2b_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5b_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_44_1/Relu" + input: "res5b_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res5b_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_44_1/Relu" + input: "^res5b_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\002\000\000\007\000\000\000\007\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res5b_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_44_1/Relu" + input: "^res5b_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\002\000\000\000\010\000\000" + } + } + } +} +node { + name: "bn5b_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn5b_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn5b_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2c_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res5b_branch2c_1/Conv2D" + input: "bn5b_branch2c_1/ReadVariableOp" + input: "bn5b_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn5b_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5b_branch2b_cond_input_3" + input: "bn5b_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5b_branch2b_cond_input_4" + input: "bn5b_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "add_14_1/add" + op: "Add" + input: "bn5b_branch2c_1/FusedBatchNormV2" + input: "activation_42_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn5b_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn5b_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn5b_branch2c_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn5b_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn5b_branch2c_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_45_1/Relu" + op: "Relu" + input: "add_14_1/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn5b_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn5b_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn5b_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5c_branch2a_1/Conv2D" + op: "Conv2D" + input: "activation_45_1/Relu" + input: "res5c_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res5c_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_45_1/Relu" + input: "^res5c_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\010\000\000\007\000\000\000\007\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res5c_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_45_1/Relu" + input: "^res5c_branch2a_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\010\000\000\000\002\000\000" + } + } + } +} +node { + name: "bn5b_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5b_branch2c_cond_input_4" + input: "bn5b_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5b_branch2c_cond_input_3" + input: "bn5b_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2a_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res5c_branch2a_1/Conv2D" + input: "bn5c_branch2a_1/ReadVariableOp" + input: "bn5c_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "activation_46_1/Relu" + op: "Relu" + input: "bn5c_branch2a_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn5c_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn5c_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn5c_branch2a_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn5c_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn5c_branch2a_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5c_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_46_1/Relu" + input: "res5c_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res5c_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_46_1/Relu" + input: "^res5c_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\002\000\000\007\000\000\000\007\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res5c_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_46_1/Relu" + input: "^res5c_branch2b_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000\000\002\000\000\000\002\000\000" + } + } + } +} +node { + name: "bn5c_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn5c_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn5c_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2b_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res5c_branch2b_1/Conv2D" + input: "bn5c_branch2b_1/ReadVariableOp" + input: "bn5c_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn5c_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5c_branch2a_cond_input_4" + input: "bn5c_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5c_branch2a_cond_input_3" + input: "bn5c_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_47_1/Relu" + op: "Relu" + input: "bn5c_branch2b_1/FusedBatchNormV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn5c_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn5c_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn5c_branch2b_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn5c_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn5c_branch2b_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5c_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_47_1/Relu" + input: "res5c_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res5c_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_47_1/Relu" + input: "^res5c_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\002\000\000\007\000\000\000\007\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/res5c_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_47_1/Relu" + input: "^res5c_branch2c_1/Conv2D/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\002\000\000\000\010\000\000" + } + } + } +} +node { + name: "bn5c_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn5c_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn5c_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2c_1/FusedBatchNormV2" + op: "FusedBatchNormV2" + input: "res5c_branch2c_1/Conv2D" + input: "bn5c_branch2c_1/ReadVariableOp" + input: "bn5c_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn5c_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5c_branch2b_cond_input_3" + input: "bn5c_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5c_branch2b_cond_input_4" + input: "bn5c_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "add_15_1/add" + op: "Add" + input: "bn5c_branch2c_1/FusedBatchNormV2" + input: "activation_45_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn5c_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn5c_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn5c_branch2c_1/FusedBatchNormV2:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn5c_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn5c_branch2c_1/FusedBatchNormV2:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "activation_48_1/Relu" + op: "Relu" + input: "add_15_1/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "bn5c_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn5c_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn5c_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "avg_pool_1/Mean" + op: "Mean" + input: "activation_48_1/Relu" + input: "avg_pool_1/Mean/reduction_indices" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/avg_pool_1/Mean_grad/floordiv" + op: "Const" + input: "^activation_48_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\007\000\000\000\007\000\000\000" + } + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/avg_pool_1/Mean_grad/DynamicStitch" + op: "Const" + input: "^activation_48_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\300\000\000\000\000\010\000\000\001\000\000\000\001\000\000\000" + } + } + } +} +node { + name: "bn5c_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5c_branch2c_cond_input_3" + input: "bn5c_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5c_branch2c_cond_input_4" + input: "bn5c_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "fc1000_1/MatMul" + op: "MatMul" + input: "avg_pool_1/Mean" + input: "fc1000_1/MatMul/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "transpose_a" + value { + b: false + } + } + attr { + key: "transpose_b" + value { + b: false + } + } +} +node { + name: "ConstantFolding/training/LossScaleOptimizer/gradients/avg_pool_1/Mean_grad/truediv_recip" + op: "Const" + input: "^avg_pool_1/Mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_HALF + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_HALF + tensor_shape { + } + tensor_content: "9%" + } + } + } +} +node { + name: "fc1000_1/BiasAdd" + op: "BiasAdd" + input: "fc1000_1/MatMul" + input: "fc1000_1/BiasAdd/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NHWC" + } + } +} +node { + name: "tf_op_layer_Cast/Cast" + op: "Cast" + input: "fc1000_1/BiasAdd" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "activation_49_1/Softmax" + op: "Softmax" + input: "tf_op_layer_Cast/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/activation_49_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits" + op: "SparseSoftmaxCrossEntropyWithLogits" + input: "tf_op_layer_Cast/Cast" + input: "loss_1/activation_49_loss/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tlabels" + value { + type: DT_INT64 + } + } +} +node { + name: "metrics_2/sparse_categorical_accuracy/ArgMax" + op: "ArgMax" + input: "activation_49_1/Softmax" + input: "metrics_2/sparse_categorical_accuracy/ArgMax/dimension" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "output_type" + value { + type: DT_INT64 + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/activation_49_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits_grad/ExpandDims" + op: "Const" + input: "^loss_1/activation_49_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 192 + } + dim { + size: 1 + } + } + float_val: 0.6666667 + } + } + } +} +node { + name: "loss_1/activation_49_loss/num_elements/Cast" + op: "Const" + input: "^loss_1/activation_49_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + tensor_content: "\000\000@C" + } + } + } +} +node { + name: "loss_1/activation_49_loss/Sum" + op: "Sum" + input: "loss_1/activation_49_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits" + input: "metrics_2/sparse_categorical_accuracy/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "metrics_2/sparse_categorical_accuracy/Cast" + op: "Cast" + input: "metrics_2/sparse_categorical_accuracy/ArgMax" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_INT64 + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/loss_1/activation_49_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits_grad/mul" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/loss_1/activation_49_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits_grad/ExpandDims" + input: "loss_1/activation_49_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/activation_49_loss/Sum_1" + op: "Sum" + input: "loss_1/activation_49_loss/Sum" + input: "training/LossScaleOptimizer/gradients/Shape" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "metrics_2/sparse_categorical_accuracy/Equal" + op: "Equal" + input: "metrics_2/sparse_categorical_accuracy/Squeeze" + input: "metrics_2/sparse_categorical_accuracy/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/tf_op_layer_Cast/Cast_grad/ArithmeticOptimizer/ReorderCastLikeAndValuePreserving_float_Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/loss_1/activation_49_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits_grad/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "loss_1/activation_49_loss/value" + op: "DivNoNan" + input: "loss_1/activation_49_loss/Sum_1" + input: "loss_1/activation_49_loss/num_elements/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "metrics_2/sparse_categorical_accuracy/Cast_1" + op: "Cast" + input: "metrics_2/sparse_categorical_accuracy/Equal" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_BOOL + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/fc1000_1/MatMul_grad/MatMul" + op: "MatMul" + input: "training/LossScaleOptimizer/gradients/tf_op_layer_Cast/Cast_grad/ArithmeticOptimizer/ReorderCastLikeAndValuePreserving_float_Cast" + input: "fc1000_1/MatMul/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "transpose_a" + value { + b: false + } + } + attr { + key: "transpose_b" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/fc1000_1/BiasAdd_grad/BiasAddGrad" + op: "BiasAddGrad" + input: "training/LossScaleOptimizer/gradients/tf_op_layer_Cast/Cast_grad/ArithmeticOptimizer/ReorderCastLikeAndValuePreserving_float_Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NHWC" + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/fc1000_1/MatMul_grad/MatMul_1" + op: "MatMul" + input: "avg_pool_1/Mean" + input: "training/LossScaleOptimizer/gradients/tf_op_layer_Cast/Cast_grad/ArithmeticOptimizer/ReorderCastLikeAndValuePreserving_float_Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "transpose_a" + value { + b: true + } + } + attr { + key: "transpose_b" + value { + b: false + } + } +} +node { + name: "loss_1/ArithmeticOptimizer/AddOpsRewrite_add" + op: "AddN" + input: "loss_1/activation_49_loss/value" + input: "loss_1/conv1/kernel/Regularizer/mul" + input: "loss_1/res2a_branch2a/kernel/Regularizer/mul" + input: "loss_1/res2a_branch2b/kernel/Regularizer/mul" + input: "loss_1/res2a_branch2c/kernel/Regularizer/mul" + input: "loss_1/res2a_branch1/kernel/Regularizer/mul" + input: "loss_1/res2b_branch2a/kernel/Regularizer/mul" + input: "loss_1/res2b_branch2b/kernel/Regularizer/mul" + input: "loss_1/res2b_branch2c/kernel/Regularizer/mul" + input: "loss_1/res2c_branch2a/kernel/Regularizer/mul" + input: "loss_1/res2c_branch2b/kernel/Regularizer/mul" + input: "loss_1/res2c_branch2c/kernel/Regularizer/mul" + input: "loss_1/res3a_branch2a/kernel/Regularizer/mul" + input: "loss_1/res3a_branch2b/kernel/Regularizer/mul" + input: "loss_1/res3a_branch2c/kernel/Regularizer/mul" + input: "loss_1/res3a_branch1/kernel/Regularizer/mul" + input: "loss_1/res3b_branch2a/kernel/Regularizer/mul" + input: "loss_1/res3b_branch2b/kernel/Regularizer/mul" + input: "loss_1/res3b_branch2c/kernel/Regularizer/mul" + input: "loss_1/res3c_branch2a/kernel/Regularizer/mul" + input: "loss_1/res3c_branch2b/kernel/Regularizer/mul" + input: "loss_1/res3c_branch2c/kernel/Regularizer/mul" + input: "loss_1/res3d_branch2a/kernel/Regularizer/mul" + input: "loss_1/res3d_branch2b/kernel/Regularizer/mul" + input: "loss_1/res3d_branch2c/kernel/Regularizer/mul" + input: "loss_1/res4a_branch2a/kernel/Regularizer/mul" + input: "loss_1/res4a_branch2b/kernel/Regularizer/mul" + input: "loss_1/res4a_branch2c/kernel/Regularizer/mul" + input: "loss_1/res4a_branch1/kernel/Regularizer/mul" + input: "loss_1/res4b_branch2a/kernel/Regularizer/mul" + input: "loss_1/res4b_branch2b/kernel/Regularizer/mul" + input: "loss_1/res4b_branch2c/kernel/Regularizer/mul" + input: "loss_1/res4c_branch2a/kernel/Regularizer/mul" + input: "loss_1/res4c_branch2b/kernel/Regularizer/mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/mul" + input: "loss_1/res4d_branch2a/kernel/Regularizer/mul" + input: "loss_1/res4d_branch2b/kernel/Regularizer/mul" + input: "loss_1/res4d_branch2c/kernel/Regularizer/mul" + input: "loss_1/res4e_branch2a/kernel/Regularizer/mul" + input: "loss_1/res4e_branch2b/kernel/Regularizer/mul" + input: "loss_1/res4e_branch2c/kernel/Regularizer/mul" + input: "loss_1/res4f_branch2a/kernel/Regularizer/mul" + input: "loss_1/res4f_branch2b/kernel/Regularizer/mul" + input: "loss_1/res4f_branch2c/kernel/Regularizer/mul" + input: "loss_1/res5a_branch2a/kernel/Regularizer/mul" + input: "loss_1/res5a_branch2b/kernel/Regularizer/mul" + input: "loss_1/res5a_branch2c/kernel/Regularizer/mul" + input: "loss_1/res5a_branch1/kernel/Regularizer/mul" + input: "loss_1/res5b_branch2a/kernel/Regularizer/mul" + input: "loss_1/res5b_branch2b/kernel/Regularizer/mul" + input: "loss_1/res5b_branch2c/kernel/Regularizer/mul" + input: "loss_1/res5c_branch2a/kernel/Regularizer/mul" + input: "loss_1/res5c_branch2b/kernel/Regularizer/mul" + input: "loss_1/res5c_branch2c/kernel/Regularizer/mul" + input: "loss_1/fc1000/kernel/Regularizer/mul" + input: "loss_1/fc1000/bias/Regularizer/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 56 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_grappler:ArithmeticOptimizer:AddOpsRewriteStage" + value { + b: true + } + } +} +node { + name: "metrics_2/sparse_categorical_accuracy/Sum" + op: "Sum" + input: "metrics_2/sparse_categorical_accuracy/Cast_1" + input: "metrics_2/sparse_categorical_accuracy/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "metrics_2/sparse_categorical_accuracy/Cast_2" + op: "Const" + input: "^metrics_2/sparse_categorical_accuracy/Cast_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + tensor_content: "\000\000@C" + } + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/avg_pool_1/Mean_grad/Reshape" + op: "Reshape" + input: "training/LossScaleOptimizer/gradients/fc1000_1/MatMul_grad/MatMul" + input: "training/LossScaleOptimizer/gradients/avg_pool_1/Mean_grad/DynamicStitch" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "Tshape" + value { + type: DT_INT32 + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/fc1000_1/BiasAdd/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/fc1000_1/BiasAdd_grad/BiasAddGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/fc1000_1/MatMul/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/fc1000_1/MatMul_grad/MatMul_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "metrics_2/sparse_categorical_accuracy/AssignAddVariableOp" + op: "AssignAddVariableOp" + input: "metrics_2_sparse_categorical_accuracy_assignaddvariableop_resource" + input: "metrics_2/sparse_categorical_accuracy/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/avg_pool_1/Mean_grad/Tile" + op: "Tile" + input: "training/LossScaleOptimizer/gradients/avg_pool_1/Mean_grad/Reshape" + input: "training/LossScaleOptimizer/gradients/avg_pool_1/Mean_grad/floordiv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "Tmultiples" + value { + type: DT_INT32 + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/fc1000/bias/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/fc1000_1/BiasAdd/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_1" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/fc1000/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/fc1000_1/MatMul/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "metrics_2/sparse_categorical_accuracy/AssignAddVariableOp_1" + op: "AssignAddVariableOp" + input: "metrics_2_sparse_categorical_accuracy_assignaddvariableop_1_resource" + input: "metrics_2/sparse_categorical_accuracy/Cast_2" + input: "^metrics_2/sparse_categorical_accuracy/AssignAddVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/avg_pool_1/Mean_grad/truediv" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/avg_pool_1/Mean_grad/Tile" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/avg_pool_1/Mean_grad/truediv_recip" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/mul_161" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_160" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "metrics_2/sparse_categorical_accuracy/div_no_nan/ReadVariableOp_1" + op: "ReadVariableOp" + input: "metrics_2_sparse_categorical_accuracy_assignaddvariableop_1_resource" + input: "^metrics_2/sparse_categorical_accuracy/AssignAddVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "metrics_2/sparse_categorical_accuracy/div_no_nan/ReadVariableOp" + op: "ReadVariableOp" + input: "metrics_2_sparse_categorical_accuracy_assignaddvariableop_resource" + input: "^metrics_2/sparse_categorical_accuracy/AssignAddVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_48_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/avg_pool_1/Mean_grad/truediv" + input: "activation_48_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_160/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "fc1000_biasadd_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_160_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_160/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_161" + input: "training/LossScaleOptimizer/SGD/update_160/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_159/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "fc1000_matmul_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_159_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_159/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_160" + input: "training/LossScaleOptimizer/SGD/update_159/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "metrics_2/sparse_categorical_accuracy/div_no_nan" + op: "DivNoNan" + input: "metrics_2/sparse_categorical_accuracy/div_no_nan/ReadVariableOp" + input: "metrics_2/sparse_categorical_accuracy/div_no_nan/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn5c_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_48_1/Relu_grad/ReluGrad" + input: "res5c_branch2c_1/Conv2D" + input: "bn5c_branch2c_1/ReadVariableOp" + input: "bn5c_branch2c_1/FusedBatchNormV2:3" + input: "bn5c_branch2c_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "metrics_2_sparse_categorical_accuracy_identity_RetVal" + op: "_Retval" + input: "metrics_2/sparse_categorical_accuracy/div_no_nan" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "index" + value { + i: 1 + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res5c_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res5c_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res5c_branch2c_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn5c_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_159" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn5c_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_158" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn5c_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res5c_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_47_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res5c_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn5c_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_47_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res5c_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_47_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_158/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5c_branch2c_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_158_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_158/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_159" + input: "training/LossScaleOptimizer/SGD/update_158/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_157/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5c_branch2c_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_157_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_157/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_158" + input: "training/LossScaleOptimizer/SGD/update_157/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res5c_branch2c_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res5c_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn5c_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_47_1/Relu_grad/ReluGrad" + input: "res5c_branch2b_1/Conv2D" + input: "bn5c_branch2b_1/ReadVariableOp" + input: "bn5c_branch2b_1/FusedBatchNormV2:3" + input: "bn5c_branch2b_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_2" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res5c_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res5c_branch2c_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res5c_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res5c_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res5c_branch2b_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn5c_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_156" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn5c_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_155" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn5c_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res5c_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_46_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res5c_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn5c_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_157" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_46_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res5c_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_46_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_155/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5c_branch2b_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_155_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_155/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_156" + input: "training/LossScaleOptimizer/SGD/update_155/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_154/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5c_branch2b_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_154_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_154/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_155" + input: "training/LossScaleOptimizer/SGD/update_154/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res5c_branch2b_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res5c_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_156/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res5c_branch2c_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_156_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_156/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_157" + input: "training/LossScaleOptimizer/SGD/update_156/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn5c_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_46_1/Relu_grad/ReluGrad" + input: "res5c_branch2a_1/Conv2D" + input: "bn5c_branch2a_1/ReadVariableOp" + input: "bn5c_branch2a_1/FusedBatchNormV2:3" + input: "bn5c_branch2a_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_3" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res5c_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res5c_branch2b_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res5c_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res5c_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res5c_branch2a_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn5c_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_153" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn5c_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_152" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn5c_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res5c_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_45_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res5c_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn5c_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_154" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_3" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_4" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/activation_48_1/Relu_grad/ReluGrad" + input: "training/LossScaleOptimizer/gradients/res5c_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_152/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5c_branch2a_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_152_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_152/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_153" + input: "training/LossScaleOptimizer/SGD/update_152/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_151/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5c_branch2a_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_151_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_151/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_152" + input: "training/LossScaleOptimizer/SGD/update_151/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res5c_branch2a_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res5c_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_153/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res5c_branch2b_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_153_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_153/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_154" + input: "training/LossScaleOptimizer/SGD/update_153/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_45_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/AddN_4" + input: "activation_45_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_5" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res5c_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res5c_branch2a_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn5b_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_45_1/Relu_grad/ReluGrad" + input: "res5b_branch2c_1/Conv2D" + input: "bn5b_branch2c_1/ReadVariableOp" + input: "bn5b_branch2c_1/FusedBatchNormV2:3" + input: "bn5b_branch2c_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_151" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_5" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res5b_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res5b_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res5b_branch2c_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn5b_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_150" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn5b_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_149" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn5b_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res5b_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_44_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res5b_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn5b_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_150/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res5c_branch2a_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_150_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_150/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_151" + input: "training/LossScaleOptimizer/SGD/update_150/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_44_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res5b_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_44_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_149/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5b_branch2c_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_149_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_149/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_150" + input: "training/LossScaleOptimizer/SGD/update_149/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_148/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5b_branch2c_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_148_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_148/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_149" + input: "training/LossScaleOptimizer/SGD/update_148/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res5b_branch2c_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res5b_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn5b_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_44_1/Relu_grad/ReluGrad" + input: "res5b_branch2b_1/Conv2D" + input: "bn5b_branch2b_1/ReadVariableOp" + input: "bn5b_branch2b_1/FusedBatchNormV2:3" + input: "bn5b_branch2b_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_6" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res5b_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res5b_branch2c_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res5b_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res5b_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res5b_branch2b_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn5b_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_147" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn5b_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_146" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn5b_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res5b_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_43_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res5b_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn5b_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_148" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_6" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_43_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res5b_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_43_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_146/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5b_branch2b_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_146_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_146/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_147" + input: "training/LossScaleOptimizer/SGD/update_146/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_145/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5b_branch2b_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_145_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_145/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_146" + input: "training/LossScaleOptimizer/SGD/update_145/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res5b_branch2b_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res5b_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_147/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res5b_branch2c_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_147_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_147/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_148" + input: "training/LossScaleOptimizer/SGD/update_147/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn5b_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_43_1/Relu_grad/ReluGrad" + input: "res5b_branch2a_1/Conv2D" + input: "bn5b_branch2a_1/ReadVariableOp" + input: "bn5b_branch2a_1/FusedBatchNormV2:3" + input: "bn5b_branch2a_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_7" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res5b_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res5b_branch2b_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res5b_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res5b_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res5b_branch2a_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn5b_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_144" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn5b_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_143" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn5b_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res5b_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_42_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res5b_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn5b_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_145" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_7" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_8" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/activation_45_1/Relu_grad/ReluGrad" + input: "training/LossScaleOptimizer/gradients/res5b_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_143/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5b_branch2a_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_143_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_143/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_144" + input: "training/LossScaleOptimizer/SGD/update_143/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_142/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5b_branch2a_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_142_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_142/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_143" + input: "training/LossScaleOptimizer/SGD/update_142/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res5b_branch2a_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res5b_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_144/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res5b_branch2b_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_144_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_144/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_145" + input: "training/LossScaleOptimizer/SGD/update_144/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_42_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/AddN_8" + input: "activation_42_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_9" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res5b_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res5b_branch2a_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn5a_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_42_1/Relu_grad/ReluGrad" + input: "res5a_branch2c_1/Conv2D" + input: "bn5a_branch2c_1/ReadVariableOp" + input: "bn5a_branch2c_1/FusedBatchNormV2:3" + input: "bn5a_branch2c_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn5a_branch1_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_42_1/Relu_grad/ReluGrad" + input: "res5a_branch1_1/Conv2D" + input: "bn5a_branch1_1/ReadVariableOp" + input: "bn5a_branch1_1/FusedBatchNormV2:3" + input: "bn5a_branch1_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_142" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_9" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res5a_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res5a_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res5a_branch2c_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn5a_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_139" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn5a_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_138" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn5a_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res5a_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_41_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res5a_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn5a_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res5a_branch1_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res5a_branch1_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res5a_branch1_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn5a_branch1_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_141" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn5a_branch1_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_140" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn5a_branch1_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res5a_branch1_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_39_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res5a_branch1_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn5a_branch1_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_141/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res5b_branch2a_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_141_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_141/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_142" + input: "training/LossScaleOptimizer/SGD/update_141/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_41_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res5a_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_41_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_138/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5a_branch2c_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_138_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_138/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_139" + input: "training/LossScaleOptimizer/SGD/update_138/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_137/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5a_branch2c_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_137_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_137/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_138" + input: "training/LossScaleOptimizer/SGD/update_137/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res5a_branch2c_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res5a_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_140/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5a_branch1_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_140_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_140/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_141" + input: "training/LossScaleOptimizer/SGD/update_140/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_139/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5a_branch1_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_139_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_139/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_140" + input: "training/LossScaleOptimizer/SGD/update_139/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res5a_branch1_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res5a_branch1_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn5a_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_41_1/Relu_grad/ReluGrad" + input: "res5a_branch2b_1/Conv2D" + input: "bn5a_branch2b_1/ReadVariableOp" + input: "bn5a_branch2b_1/FusedBatchNormV2:3" + input: "bn5a_branch2b_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_10" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res5a_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res5a_branch2c_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_11" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res5a_branch1/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res5a_branch1_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res5a_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res5a_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res5a_branch2b_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn5a_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_135" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn5a_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_134" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn5a_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res5a_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_40_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res5a_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn5a_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_136" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_10" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_137" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_11" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_40_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res5a_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_40_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_134/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5a_branch2b_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_134_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_134/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_135" + input: "training/LossScaleOptimizer/SGD/update_134/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_133/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5a_branch2b_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_133_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_133/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_134" + input: "training/LossScaleOptimizer/SGD/update_133/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res5a_branch2b_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res5a_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_135/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res5a_branch2c_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_135_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_135/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_136" + input: "training/LossScaleOptimizer/SGD/update_135/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_136/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res5a_branch1_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_136_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_136/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_137" + input: "training/LossScaleOptimizer/SGD/update_136/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn5a_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_40_1/Relu_grad/ReluGrad" + input: "res5a_branch2a_1/Conv2D" + input: "bn5a_branch2a_1/ReadVariableOp" + input: "bn5a_branch2a_1/FusedBatchNormV2:3" + input: "bn5a_branch2a_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_12" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res5a_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res5a_branch2b_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res5a_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res5a_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res5a_branch2a_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn5a_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_132" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn5a_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_131" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn5a_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res5a_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_39_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res5a_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn5a_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_133" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_12" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_13" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/res5a_branch1_1/Conv2D_grad/Conv2DBackpropInput" + input: "training/LossScaleOptimizer/gradients/res5a_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_131/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5a_branch2a_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_131_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_131/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_132" + input: "training/LossScaleOptimizer/SGD/update_131/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_130/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5a_branch2a_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_130_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_130/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_131" + input: "training/LossScaleOptimizer/SGD/update_130/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res5a_branch2a_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res5a_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_132/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res5a_branch2b_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_132_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_132/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_133" + input: "training/LossScaleOptimizer/SGD/update_132/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_39_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/AddN_13" + input: "activation_39_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_14" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res5a_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res5a_branch2a_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn4f_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_39_1/Relu_grad/ReluGrad" + input: "res4f_branch2c_1/Conv2D" + input: "bn4f_branch2c_1/ReadVariableOp" + input: "bn4f_branch2c_1/FusedBatchNormV2:3" + input: "bn4f_branch2c_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_130" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_14" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4f_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4f_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res4f_branch2c_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn4f_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_129" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4f_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_128" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4f_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4f_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_38_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4f_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn4f_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_129/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res5a_branch2a_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_129_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_129/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_130" + input: "training/LossScaleOptimizer/SGD/update_129/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_38_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res4f_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_38_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_128/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4f_branch2c_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_128_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_128/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_129" + input: "training/LossScaleOptimizer/SGD/update_128/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_127/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4f_branch2c_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_127_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_127/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_128" + input: "training/LossScaleOptimizer/SGD/update_127/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4f_branch2c_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res4f_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn4f_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_38_1/Relu_grad/ReluGrad" + input: "res4f_branch2b_1/Conv2D" + input: "bn4f_branch2b_1/ReadVariableOp" + input: "bn4f_branch2b_1/FusedBatchNormV2:3" + input: "bn4f_branch2b_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_15" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res4f_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res4f_branch2c_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4f_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4f_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res4f_branch2b_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn4f_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_126" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4f_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_125" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4f_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4f_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_37_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4f_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn4f_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_127" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_15" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_37_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res4f_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_37_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_125/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4f_branch2b_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_125_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_125/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_126" + input: "training/LossScaleOptimizer/SGD/update_125/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_124/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4f_branch2b_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_124_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_124/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_125" + input: "training/LossScaleOptimizer/SGD/update_124/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4f_branch2b_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res4f_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_126/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4f_branch2c_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_126_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_126/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_127" + input: "training/LossScaleOptimizer/SGD/update_126/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn4f_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_37_1/Relu_grad/ReluGrad" + input: "res4f_branch2a_1/Conv2D" + input: "bn4f_branch2a_1/ReadVariableOp" + input: "bn4f_branch2a_1/FusedBatchNormV2:3" + input: "bn4f_branch2a_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_16" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res4f_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res4f_branch2b_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4f_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4f_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res4f_branch2a_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn4f_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_123" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4f_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_122" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4f_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4f_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_36_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4f_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn4f_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_124" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_16" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_17" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/activation_39_1/Relu_grad/ReluGrad" + input: "training/LossScaleOptimizer/gradients/res4f_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_122/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4f_branch2a_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_122_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_122/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_123" + input: "training/LossScaleOptimizer/SGD/update_122/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_121/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4f_branch2a_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_121_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_121/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_122" + input: "training/LossScaleOptimizer/SGD/update_121/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4f_branch2a_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res4f_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_123/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4f_branch2b_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_123_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_123/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_124" + input: "training/LossScaleOptimizer/SGD/update_123/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_36_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/AddN_17" + input: "activation_36_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_18" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res4f_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res4f_branch2a_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn4e_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_36_1/Relu_grad/ReluGrad" + input: "res4e_branch2c_1/Conv2D" + input: "bn4e_branch2c_1/ReadVariableOp" + input: "bn4e_branch2c_1/FusedBatchNormV2:3" + input: "bn4e_branch2c_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_121" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_18" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4e_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4e_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res4e_branch2c_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn4e_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_120" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4e_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_119" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4e_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4e_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_35_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4e_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn4e_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_120/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4f_branch2a_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_120_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_120/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_121" + input: "training/LossScaleOptimizer/SGD/update_120/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_35_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res4e_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_35_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_119/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4e_branch2c_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_119_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_119/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_120" + input: "training/LossScaleOptimizer/SGD/update_119/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_118/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4e_branch2c_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_118_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_118/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_119" + input: "training/LossScaleOptimizer/SGD/update_118/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4e_branch2c_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res4e_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn4e_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_35_1/Relu_grad/ReluGrad" + input: "res4e_branch2b_1/Conv2D" + input: "bn4e_branch2b_1/ReadVariableOp" + input: "bn4e_branch2b_1/FusedBatchNormV2:3" + input: "bn4e_branch2b_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_19" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res4e_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res4e_branch2c_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4e_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4e_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res4e_branch2b_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn4e_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_117" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4e_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_116" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4e_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4e_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_34_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4e_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn4e_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_118" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_19" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_34_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res4e_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_34_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_116/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4e_branch2b_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_116_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_116/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_117" + input: "training/LossScaleOptimizer/SGD/update_116/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_115/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4e_branch2b_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_115_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_115/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_116" + input: "training/LossScaleOptimizer/SGD/update_115/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4e_branch2b_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res4e_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_117/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4e_branch2c_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_117_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_117/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_118" + input: "training/LossScaleOptimizer/SGD/update_117/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn4e_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_34_1/Relu_grad/ReluGrad" + input: "res4e_branch2a_1/Conv2D" + input: "bn4e_branch2a_1/ReadVariableOp" + input: "bn4e_branch2a_1/FusedBatchNormV2:3" + input: "bn4e_branch2a_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_20" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res4e_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res4e_branch2b_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4e_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4e_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res4e_branch2a_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn4e_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_114" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4e_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_113" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4e_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4e_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_33_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4e_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn4e_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_115" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_20" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_21" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/activation_36_1/Relu_grad/ReluGrad" + input: "training/LossScaleOptimizer/gradients/res4e_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_113/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4e_branch2a_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_113_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_113/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_114" + input: "training/LossScaleOptimizer/SGD/update_113/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_112/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4e_branch2a_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_112_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_112/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_113" + input: "training/LossScaleOptimizer/SGD/update_112/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4e_branch2a_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res4e_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_114/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4e_branch2b_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_114_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_114/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_115" + input: "training/LossScaleOptimizer/SGD/update_114/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_33_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/AddN_21" + input: "activation_33_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_22" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res4e_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res4e_branch2a_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn4d_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_33_1/Relu_grad/ReluGrad" + input: "res4d_branch2c_1/Conv2D" + input: "bn4d_branch2c_1/ReadVariableOp" + input: "bn4d_branch2c_1/FusedBatchNormV2:3" + input: "bn4d_branch2c_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_112" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_22" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4d_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4d_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res4d_branch2c_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn4d_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_111" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4d_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_110" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4d_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4d_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_32_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4d_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn4d_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_111/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4e_branch2a_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_111_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_111/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_112" + input: "training/LossScaleOptimizer/SGD/update_111/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_32_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res4d_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_32_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_110/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4d_branch2c_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_110_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_110/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_111" + input: "training/LossScaleOptimizer/SGD/update_110/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_109/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4d_branch2c_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_109_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_109/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_110" + input: "training/LossScaleOptimizer/SGD/update_109/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4d_branch2c_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res4d_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn4d_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_32_1/Relu_grad/ReluGrad" + input: "res4d_branch2b_1/Conv2D" + input: "bn4d_branch2b_1/ReadVariableOp" + input: "bn4d_branch2b_1/FusedBatchNormV2:3" + input: "bn4d_branch2b_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_23" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res4d_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res4d_branch2c_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4d_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4d_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res4d_branch2b_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn4d_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_108" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4d_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_107" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4d_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4d_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_31_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4d_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn4d_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_109" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_23" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_31_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res4d_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_31_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_107/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4d_branch2b_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_107_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_107/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_108" + input: "training/LossScaleOptimizer/SGD/update_107/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_106/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4d_branch2b_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_106_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_106/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_107" + input: "training/LossScaleOptimizer/SGD/update_106/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4d_branch2b_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res4d_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_108/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4d_branch2c_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_108_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_108/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_109" + input: "training/LossScaleOptimizer/SGD/update_108/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn4d_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_31_1/Relu_grad/ReluGrad" + input: "res4d_branch2a_1/Conv2D" + input: "bn4d_branch2a_1/ReadVariableOp" + input: "bn4d_branch2a_1/FusedBatchNormV2:3" + input: "bn4d_branch2a_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_24" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res4d_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res4d_branch2b_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4d_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4d_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res4d_branch2a_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn4d_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_105" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4d_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_104" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4d_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4d_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_30_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4d_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn4d_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_106" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_24" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_25" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/activation_33_1/Relu_grad/ReluGrad" + input: "training/LossScaleOptimizer/gradients/res4d_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_104/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4d_branch2a_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_104_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_104/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_105" + input: "training/LossScaleOptimizer/SGD/update_104/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_103/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4d_branch2a_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_103_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_103/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_104" + input: "training/LossScaleOptimizer/SGD/update_103/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4d_branch2a_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res4d_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_105/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4d_branch2b_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_105_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_105/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_106" + input: "training/LossScaleOptimizer/SGD/update_105/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_30_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/AddN_25" + input: "activation_30_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_26" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res4d_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res4d_branch2a_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn4c_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_30_1/Relu_grad/ReluGrad" + input: "res4c_branch2c_1/Conv2D" + input: "bn4c_branch2c_1/ReadVariableOp" + input: "bn4c_branch2c_1/FusedBatchNormV2:3" + input: "bn4c_branch2c_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_103" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_26" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4c_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_29_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4c_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn4c_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4c_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4c_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res4c_branch2c_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn4c_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_102" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4c_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_101" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4c_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_102/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4d_branch2a_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_102_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_102/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_103" + input: "training/LossScaleOptimizer/SGD/update_102/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4c_branch2c_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res4c_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_29_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res4c_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_29_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_101/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4c_branch2c_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_101_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_101/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_102" + input: "training/LossScaleOptimizer/SGD/update_101/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_100/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4c_branch2c_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_100_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_100/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_101" + input: "training/LossScaleOptimizer/SGD/update_100/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_27" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res4c_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res4c_branch2c_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn4c_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_29_1/Relu_grad/ReluGrad" + input: "res4c_branch2b_1/Conv2D" + input: "bn4c_branch2b_1/ReadVariableOp" + input: "bn4c_branch2b_1/FusedBatchNormV2:3" + input: "bn4c_branch2b_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_100" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_27" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_99" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4c_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_98" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4c_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4c_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_28_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4c_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn4c_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4c_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4c_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res4c_branch2b_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn4c_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_99/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4c_branch2c_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_99_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_99/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_100" + input: "training/LossScaleOptimizer/SGD/update_99/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_98/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4c_branch2b_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_98_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_98/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_99" + input: "training/LossScaleOptimizer/SGD/update_98/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_97/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4c_branch2b_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_97_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_97/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_98" + input: "training/LossScaleOptimizer/SGD/update_97/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4c_branch2b_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res4c_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_28_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res4c_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_28_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_28" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res4c_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res4c_branch2b_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn4c_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_28_1/Relu_grad/ReluGrad" + input: "res4c_branch2a_1/Conv2D" + input: "bn4c_branch2a_1/ReadVariableOp" + input: "bn4c_branch2a_1/FusedBatchNormV2:3" + input: "bn4c_branch2a_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_97" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_28" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_96" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4c_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_95" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4c_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4c_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_27_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4c_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn4c_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4c_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4c_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res4c_branch2a_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn4c_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_96/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4c_branch2b_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_96_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_96/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_97" + input: "training/LossScaleOptimizer/SGD/update_96/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_95/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4c_branch2a_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_95_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_95/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_96" + input: "training/LossScaleOptimizer/SGD/update_95/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_94/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4c_branch2a_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_94_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_94/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_95" + input: "training/LossScaleOptimizer/SGD/update_94/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4c_branch2a_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res4c_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_29" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/activation_30_1/Relu_grad/ReluGrad" + input: "training/LossScaleOptimizer/gradients/res4c_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_30" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res4c_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res4c_branch2a_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_27_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/AddN_29" + input: "activation_27_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/mul_94" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_30" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn4b_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_27_1/Relu_grad/ReluGrad" + input: "res4b_branch2c_1/Conv2D" + input: "bn4b_branch2c_1/ReadVariableOp" + input: "bn4b_branch2c_1/FusedBatchNormV2:3" + input: "bn4b_branch2c_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_93/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4c_branch2a_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_93_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_93/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_94" + input: "training/LossScaleOptimizer/SGD/update_93/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/mul_93" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4b_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_92" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4b_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4b_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_26_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4b_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn4b_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4b_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4b_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res4b_branch2c_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn4b_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_92/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4b_branch2c_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_92_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_92/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_93" + input: "training/LossScaleOptimizer/SGD/update_92/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_91/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4b_branch2c_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_91_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_91/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_92" + input: "training/LossScaleOptimizer/SGD/update_91/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4b_branch2c_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res4b_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_26_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res4b_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_26_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_31" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res4b_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res4b_branch2c_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn4b_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_26_1/Relu_grad/ReluGrad" + input: "res4b_branch2b_1/Conv2D" + input: "bn4b_branch2b_1/ReadVariableOp" + input: "bn4b_branch2b_1/FusedBatchNormV2:3" + input: "bn4b_branch2b_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_91" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_31" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4b_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4b_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res4b_branch2b_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn4b_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_90" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4b_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_89" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4b_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4b_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_25_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4b_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn4b_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_90/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4b_branch2c_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_90_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_90/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_91" + input: "training/LossScaleOptimizer/SGD/update_90/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_25_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res4b_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_25_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_89/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4b_branch2b_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_89_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_89/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_90" + input: "training/LossScaleOptimizer/SGD/update_89/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_88/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4b_branch2b_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_88_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_88/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_89" + input: "training/LossScaleOptimizer/SGD/update_88/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4b_branch2b_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res4b_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn4b_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_25_1/Relu_grad/ReluGrad" + input: "res4b_branch2a_1/Conv2D" + input: "bn4b_branch2a_1/ReadVariableOp" + input: "bn4b_branch2a_1/FusedBatchNormV2:3" + input: "bn4b_branch2a_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_32" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res4b_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res4b_branch2b_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4b_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4b_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res4b_branch2a_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn4b_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_87" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4b_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_86" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4b_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4b_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_24_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4b_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn4b_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_88" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_32" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_33" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/activation_27_1/Relu_grad/ReluGrad" + input: "training/LossScaleOptimizer/gradients/res4b_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_86/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4b_branch2a_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_86_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_86/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_87" + input: "training/LossScaleOptimizer/SGD/update_86/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_85/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4b_branch2a_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_85_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_85/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_86" + input: "training/LossScaleOptimizer/SGD/update_85/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4b_branch2a_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res4b_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_87/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4b_branch2b_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_87_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_87/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_88" + input: "training/LossScaleOptimizer/SGD/update_87/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_24_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/AddN_33" + input: "activation_24_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_34" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res4b_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res4b_branch2a_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn4a_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_24_1/Relu_grad/ReluGrad" + input: "res4a_branch2c_1/Conv2D" + input: "bn4a_branch2c_1/ReadVariableOp" + input: "bn4a_branch2c_1/FusedBatchNormV2:3" + input: "bn4a_branch2c_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn4a_branch1_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_24_1/Relu_grad/ReluGrad" + input: "res4a_branch1_1/Conv2D" + input: "bn4a_branch1_1/ReadVariableOp" + input: "bn4a_branch1_1/FusedBatchNormV2:3" + input: "bn4a_branch1_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_85" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_34" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4a_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4a_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res4a_branch2c_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn4a_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_82" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4a_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_81" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4a_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4a_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_23_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4a_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn4a_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4a_branch1_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4a_branch1_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res4a_branch1_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn4a_branch1_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_84" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4a_branch1_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_83" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4a_branch1_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4a_branch1_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_21_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4a_branch1_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn4a_branch1_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_84/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4b_branch2a_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_84_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_84/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_85" + input: "training/LossScaleOptimizer/SGD/update_84/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_23_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res4a_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_23_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_81/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4a_branch2c_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_81_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_81/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_82" + input: "training/LossScaleOptimizer/SGD/update_81/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_80/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4a_branch2c_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_80_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_80/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_81" + input: "training/LossScaleOptimizer/SGD/update_80/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4a_branch2c_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res4a_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_83/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4a_branch1_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_83_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_83/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_84" + input: "training/LossScaleOptimizer/SGD/update_83/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_82/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4a_branch1_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_82_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_82/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_83" + input: "training/LossScaleOptimizer/SGD/update_82/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4a_branch1_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res4a_branch1_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn4a_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_23_1/Relu_grad/ReluGrad" + input: "res4a_branch2b_1/Conv2D" + input: "bn4a_branch2b_1/ReadVariableOp" + input: "bn4a_branch2b_1/FusedBatchNormV2:3" + input: "bn4a_branch2b_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_35" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res4a_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res4a_branch2c_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_36" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res4a_branch1/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res4a_branch1_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4a_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4a_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res4a_branch2b_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn4a_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_78" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4a_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_77" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4a_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4a_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_22_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4a_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn4a_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_79" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_35" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_80" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_36" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_22_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res4a_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_22_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_77/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4a_branch2b_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_77_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_77/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_78" + input: "training/LossScaleOptimizer/SGD/update_77/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_76/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4a_branch2b_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_76_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_76/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_77" + input: "training/LossScaleOptimizer/SGD/update_76/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4a_branch2b_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res4a_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_78/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4a_branch2c_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_78_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_78/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_79" + input: "training/LossScaleOptimizer/SGD/update_78/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_79/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4a_branch1_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_79_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_79/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_80" + input: "training/LossScaleOptimizer/SGD/update_79/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn4a_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_22_1/Relu_grad/ReluGrad" + input: "res4a_branch2a_1/Conv2D" + input: "bn4a_branch2a_1/ReadVariableOp" + input: "bn4a_branch2a_1/FusedBatchNormV2:3" + input: "bn4a_branch2a_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_37" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res4a_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res4a_branch2b_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4a_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4a_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res4a_branch2a_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn4a_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_75" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4a_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_74" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn4a_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4a_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_21_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res4a_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn4a_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_76" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_37" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_38" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/res4a_branch1_1/Conv2D_grad/Conv2DBackpropInput" + input: "training/LossScaleOptimizer/gradients/res4a_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_74/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4a_branch2a_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_74_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_74/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_75" + input: "training/LossScaleOptimizer/SGD/update_74/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_73/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4a_branch2a_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_73_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_73/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_74" + input: "training/LossScaleOptimizer/SGD/update_73/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res4a_branch2a_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res4a_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_75/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4a_branch2b_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_75_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_75/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_76" + input: "training/LossScaleOptimizer/SGD/update_75/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_21_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/AddN_38" + input: "activation_21_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_39" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res4a_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res4a_branch2a_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn3d_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_21_1/Relu_grad/ReluGrad" + input: "res3d_branch2c_1/Conv2D" + input: "bn3d_branch2c_1/ReadVariableOp" + input: "bn3d_branch2c_1/FusedBatchNormV2:3" + input: "bn3d_branch2c_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_73" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_39" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3d_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res3d_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res3d_branch2c_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn3d_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_72" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn3d_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_71" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn3d_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3d_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_20_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res3d_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn3d_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_72/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4a_branch2a_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_72_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_72/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_73" + input: "training/LossScaleOptimizer/SGD/update_72/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_20_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res3d_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_20_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_71/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3d_branch2c_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_71_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_71/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_72" + input: "training/LossScaleOptimizer/SGD/update_71/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_70/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3d_branch2c_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_70_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_70/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_71" + input: "training/LossScaleOptimizer/SGD/update_70/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3d_branch2c_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res3d_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn3d_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_20_1/Relu_grad/ReluGrad" + input: "res3d_branch2b_1/Conv2D" + input: "bn3d_branch2b_1/ReadVariableOp" + input: "bn3d_branch2b_1/FusedBatchNormV2:3" + input: "bn3d_branch2b_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_40" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res3d_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res3d_branch2c_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3d_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res3d_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res3d_branch2b_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn3d_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_69" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn3d_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_68" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn3d_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3d_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_19_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res3d_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn3d_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_70" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_40" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_19_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res3d_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_19_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_68/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3d_branch2b_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_68_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_68/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_69" + input: "training/LossScaleOptimizer/SGD/update_68/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_67/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3d_branch2b_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_67_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_67/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_68" + input: "training/LossScaleOptimizer/SGD/update_67/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3d_branch2b_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res3d_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_69/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res3d_branch2c_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_69_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_69/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_70" + input: "training/LossScaleOptimizer/SGD/update_69/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn3d_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_19_1/Relu_grad/ReluGrad" + input: "res3d_branch2a_1/Conv2D" + input: "bn3d_branch2a_1/ReadVariableOp" + input: "bn3d_branch2a_1/FusedBatchNormV2:3" + input: "bn3d_branch2a_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_41" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res3d_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res3d_branch2b_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3d_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res3d_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res3d_branch2a_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn3d_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_66" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn3d_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_65" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn3d_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3d_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_18_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res3d_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn3d_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_67" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_41" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_42" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/activation_21_1/Relu_grad/ReluGrad" + input: "training/LossScaleOptimizer/gradients/res3d_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_65/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3d_branch2a_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_65_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_65/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_66" + input: "training/LossScaleOptimizer/SGD/update_65/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_64/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3d_branch2a_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_64_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_64/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_65" + input: "training/LossScaleOptimizer/SGD/update_64/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3d_branch2a_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res3d_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_66/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res3d_branch2b_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_66_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_66/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_67" + input: "training/LossScaleOptimizer/SGD/update_66/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_18_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/AddN_42" + input: "activation_18_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_43" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res3d_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res3d_branch2a_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn3c_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_18_1/Relu_grad/ReluGrad" + input: "res3c_branch2c_1/Conv2D" + input: "bn3c_branch2c_1/ReadVariableOp" + input: "bn3c_branch2c_1/FusedBatchNormV2:3" + input: "bn3c_branch2c_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_64" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_43" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3c_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res3c_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res3c_branch2c_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn3c_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_63" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn3c_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_62" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn3c_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3c_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_17_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res3c_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn3c_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_63/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res3d_branch2a_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_63_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_63/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_64" + input: "training/LossScaleOptimizer/SGD/update_63/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_17_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res3c_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_17_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_62/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3c_branch2c_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_62_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_62/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_63" + input: "training/LossScaleOptimizer/SGD/update_62/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_61/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3c_branch2c_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_61_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_61/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_62" + input: "training/LossScaleOptimizer/SGD/update_61/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3c_branch2c_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res3c_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn3c_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_17_1/Relu_grad/ReluGrad" + input: "res3c_branch2b_1/Conv2D" + input: "bn3c_branch2b_1/ReadVariableOp" + input: "bn3c_branch2b_1/FusedBatchNormV2:3" + input: "bn3c_branch2b_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_44" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res3c_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res3c_branch2c_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3c_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res3c_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res3c_branch2b_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn3c_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_60" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn3c_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_59" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn3c_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3c_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_16_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res3c_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn3c_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_61" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_44" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_16_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res3c_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_16_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_59/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3c_branch2b_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_59_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_59/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_60" + input: "training/LossScaleOptimizer/SGD/update_59/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_58/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3c_branch2b_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_58_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_58/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_59" + input: "training/LossScaleOptimizer/SGD/update_58/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3c_branch2b_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res3c_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_60/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res3c_branch2c_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_60_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_60/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_61" + input: "training/LossScaleOptimizer/SGD/update_60/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn3c_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_16_1/Relu_grad/ReluGrad" + input: "res3c_branch2a_1/Conv2D" + input: "bn3c_branch2a_1/ReadVariableOp" + input: "bn3c_branch2a_1/FusedBatchNormV2:3" + input: "bn3c_branch2a_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_45" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res3c_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res3c_branch2b_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3c_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res3c_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res3c_branch2a_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn3c_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_57" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn3c_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_56" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn3c_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3c_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_15_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res3c_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn3c_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_58" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_45" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_46" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/activation_18_1/Relu_grad/ReluGrad" + input: "training/LossScaleOptimizer/gradients/res3c_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_56/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3c_branch2a_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_56_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_56/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_57" + input: "training/LossScaleOptimizer/SGD/update_56/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_55/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3c_branch2a_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_55_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_55/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_56" + input: "training/LossScaleOptimizer/SGD/update_55/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3c_branch2a_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res3c_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_57/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res3c_branch2b_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_57_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_57/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_58" + input: "training/LossScaleOptimizer/SGD/update_57/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_15_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/AddN_46" + input: "activation_15_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_47" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res3c_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res3c_branch2a_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn3b_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_15_1/Relu_grad/ReluGrad" + input: "res3b_branch2c_1/Conv2D" + input: "bn3b_branch2c_1/ReadVariableOp" + input: "bn3b_branch2c_1/FusedBatchNormV2:3" + input: "bn3b_branch2c_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_55" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_47" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3b_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res3b_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res3b_branch2c_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn3b_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_54" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn3b_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_53" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn3b_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3b_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_14_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res3b_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn3b_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_54/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res3c_branch2a_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_54_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_54/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_55" + input: "training/LossScaleOptimizer/SGD/update_54/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_14_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res3b_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_14_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_53/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3b_branch2c_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_53_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_53/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_54" + input: "training/LossScaleOptimizer/SGD/update_53/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_52/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3b_branch2c_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_52_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_52/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_53" + input: "training/LossScaleOptimizer/SGD/update_52/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3b_branch2c_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res3b_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn3b_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_14_1/Relu_grad/ReluGrad" + input: "res3b_branch2b_1/Conv2D" + input: "bn3b_branch2b_1/ReadVariableOp" + input: "bn3b_branch2b_1/FusedBatchNormV2:3" + input: "bn3b_branch2b_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_48" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res3b_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res3b_branch2c_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3b_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res3b_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res3b_branch2b_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn3b_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_51" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn3b_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_50" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn3b_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3b_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_13_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res3b_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn3b_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_52" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_48" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_13_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res3b_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_13_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_50/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3b_branch2b_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_50_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_50/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_51" + input: "training/LossScaleOptimizer/SGD/update_50/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_49/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3b_branch2b_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_49_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_49/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_50" + input: "training/LossScaleOptimizer/SGD/update_49/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3b_branch2b_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res3b_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_51/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res3b_branch2c_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_51_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_51/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_52" + input: "training/LossScaleOptimizer/SGD/update_51/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn3b_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_13_1/Relu_grad/ReluGrad" + input: "res3b_branch2a_1/Conv2D" + input: "bn3b_branch2a_1/ReadVariableOp" + input: "bn3b_branch2a_1/FusedBatchNormV2:3" + input: "bn3b_branch2a_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_49" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res3b_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res3b_branch2b_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3b_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res3b_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res3b_branch2a_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn3b_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_48" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn3b_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_47" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn3b_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3b_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_12_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res3b_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn3b_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_49" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_49" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_50" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/activation_15_1/Relu_grad/ReluGrad" + input: "training/LossScaleOptimizer/gradients/res3b_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_47/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3b_branch2a_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_47_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_47/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_48" + input: "training/LossScaleOptimizer/SGD/update_47/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_46/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3b_branch2a_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_46_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_46/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_47" + input: "training/LossScaleOptimizer/SGD/update_46/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3b_branch2a_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res3b_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_48/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res3b_branch2b_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_48_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_48/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_49" + input: "training/LossScaleOptimizer/SGD/update_48/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_12_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/AddN_50" + input: "activation_12_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_51" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res3b_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res3b_branch2a_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn3a_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_12_1/Relu_grad/ReluGrad" + input: "res3a_branch2c_1/Conv2D" + input: "bn3a_branch2c_1/ReadVariableOp" + input: "bn3a_branch2c_1/FusedBatchNormV2:3" + input: "bn3a_branch2c_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn3a_branch1_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_12_1/Relu_grad/ReluGrad" + input: "res3a_branch1_1/Conv2D" + input: "bn3a_branch1_1/ReadVariableOp" + input: "bn3a_branch1_1/FusedBatchNormV2:3" + input: "bn3a_branch1_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_46" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_51" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3a_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res3a_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res3a_branch2c_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn3a_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_43" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn3a_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_42" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn3a_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3a_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_11_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res3a_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn3a_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3a_branch1_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res3a_branch1_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res3a_branch1_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn3a_branch1_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_45" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn3a_branch1_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_44" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn3a_branch1_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3a_branch1_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_9_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res3a_branch1_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn3a_branch1_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_45/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res3b_branch2a_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_45_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_45/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_46" + input: "training/LossScaleOptimizer/SGD/update_45/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_11_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res3a_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_11_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_42/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3a_branch2c_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_42_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_42/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_43" + input: "training/LossScaleOptimizer/SGD/update_42/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_41/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3a_branch2c_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_41_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_41/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_42" + input: "training/LossScaleOptimizer/SGD/update_41/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3a_branch2c_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res3a_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_44/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3a_branch1_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_44_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_44/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_45" + input: "training/LossScaleOptimizer/SGD/update_44/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_43/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3a_branch1_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_43_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_43/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_44" + input: "training/LossScaleOptimizer/SGD/update_43/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3a_branch1_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res3a_branch1_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn3a_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_11_1/Relu_grad/ReluGrad" + input: "res3a_branch2b_1/Conv2D" + input: "bn3a_branch2b_1/ReadVariableOp" + input: "bn3a_branch2b_1/FusedBatchNormV2:3" + input: "bn3a_branch2b_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_52" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res3a_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res3a_branch2c_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_53" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res3a_branch1/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res3a_branch1_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3a_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res3a_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res3a_branch2b_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn3a_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_39" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn3a_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_38" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn3a_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3a_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_10_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res3a_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn3a_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_40" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_52" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_41" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_53" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_10_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res3a_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_10_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_38/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3a_branch2b_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_38_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_38/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_39" + input: "training/LossScaleOptimizer/SGD/update_38/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_37/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3a_branch2b_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_37_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_37/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_38" + input: "training/LossScaleOptimizer/SGD/update_37/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3a_branch2b_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res3a_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_39/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res3a_branch2c_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_39_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_39/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_40" + input: "training/LossScaleOptimizer/SGD/update_39/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_40/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res3a_branch1_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_40_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_40/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_41" + input: "training/LossScaleOptimizer/SGD/update_40/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn3a_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_10_1/Relu_grad/ReluGrad" + input: "res3a_branch2a_1/Conv2D" + input: "bn3a_branch2a_1/ReadVariableOp" + input: "bn3a_branch2a_1/FusedBatchNormV2:3" + input: "bn3a_branch2a_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_54" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res3a_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res3a_branch2b_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3a_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res3a_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res3a_branch2a_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn3a_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_36" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn3a_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_35" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn3a_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3a_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_9_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res3a_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn3a_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_37" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_54" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_55" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/res3a_branch1_1/Conv2D_grad/Conv2DBackpropInput" + input: "training/LossScaleOptimizer/gradients/res3a_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_35/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3a_branch2a_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_35_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_35/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_36" + input: "training/LossScaleOptimizer/SGD/update_35/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_34/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3a_branch2a_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_34_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_34/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_35" + input: "training/LossScaleOptimizer/SGD/update_34/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res3a_branch2a_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res3a_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_36/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res3a_branch2b_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_36_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_36/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_37" + input: "training/LossScaleOptimizer/SGD/update_36/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_9_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/AddN_55" + input: "activation_9_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_56" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res3a_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res3a_branch2a_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn2c_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_9_1/Relu_grad/ReluGrad" + input: "res2c_branch2c_1/Conv2D" + input: "bn2c_branch2c_1/ReadVariableOp" + input: "bn2c_branch2c_1/FusedBatchNormV2:3" + input: "bn2c_branch2c_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_34" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_56" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res2c_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res2c_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res2c_branch2c_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn2c_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_33" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn2c_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_32" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn2c_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res2c_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_8_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res2c_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn2c_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_33/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res3a_branch2a_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_33_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_33/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_34" + input: "training/LossScaleOptimizer/SGD/update_33/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_8_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res2c_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_8_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_32/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2c_branch2c_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_32_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_32/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_33" + input: "training/LossScaleOptimizer/SGD/update_32/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_31/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2c_branch2c_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_31_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_31/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_32" + input: "training/LossScaleOptimizer/SGD/update_31/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res2c_branch2c_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res2c_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn2c_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_8_1/Relu_grad/ReluGrad" + input: "res2c_branch2b_1/Conv2D" + input: "bn2c_branch2b_1/ReadVariableOp" + input: "bn2c_branch2b_1/FusedBatchNormV2:3" + input: "bn2c_branch2b_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_57" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res2c_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res2c_branch2c_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res2c_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res2c_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res2c_branch2b_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn2c_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_30" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn2c_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_29" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn2c_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res2c_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_7_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res2c_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn2c_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_31" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_57" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_7_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res2c_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_7_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_29/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2c_branch2b_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_29_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_29/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_30" + input: "training/LossScaleOptimizer/SGD/update_29/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_28/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2c_branch2b_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_28_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_28/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_29" + input: "training/LossScaleOptimizer/SGD/update_28/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res2c_branch2b_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res2c_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_30/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res2c_branch2c_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_30_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_30/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_31" + input: "training/LossScaleOptimizer/SGD/update_30/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn2c_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_7_1/Relu_grad/ReluGrad" + input: "res2c_branch2a_1/Conv2D" + input: "bn2c_branch2a_1/ReadVariableOp" + input: "bn2c_branch2a_1/FusedBatchNormV2:3" + input: "bn2c_branch2a_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_58" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res2c_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res2c_branch2b_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res2c_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res2c_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res2c_branch2a_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn2c_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_27" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn2c_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_26" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn2c_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res2c_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_6_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res2c_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn2c_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_28" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_58" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_59" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/activation_9_1/Relu_grad/ReluGrad" + input: "training/LossScaleOptimizer/gradients/res2c_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_26/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2c_branch2a_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_26_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_26/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_27" + input: "training/LossScaleOptimizer/SGD/update_26/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_25/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2c_branch2a_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_25_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_25/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_26" + input: "training/LossScaleOptimizer/SGD/update_25/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res2c_branch2a_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res2c_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_27/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res2c_branch2b_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_27_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_27/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_28" + input: "training/LossScaleOptimizer/SGD/update_27/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_6_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/AddN_59" + input: "activation_6_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_60" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res2c_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res2c_branch2a_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn2b_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_6_1/Relu_grad/ReluGrad" + input: "res2b_branch2c_1/Conv2D" + input: "bn2b_branch2c_1/ReadVariableOp" + input: "bn2b_branch2c_1/FusedBatchNormV2:3" + input: "bn2b_branch2c_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_25" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_60" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res2b_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res2b_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res2b_branch2c_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn2b_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_24" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn2b_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_23" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn2b_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res2b_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_5_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res2b_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn2b_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_24/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res2c_branch2a_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_24_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_24/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_25" + input: "training/LossScaleOptimizer/SGD/update_24/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_5_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res2b_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_5_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_23/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2b_branch2c_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_23_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_23/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_24" + input: "training/LossScaleOptimizer/SGD/update_23/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_22/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2b_branch2c_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_22_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_22/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_23" + input: "training/LossScaleOptimizer/SGD/update_22/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res2b_branch2c_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res2b_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn2b_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_5_1/Relu_grad/ReluGrad" + input: "res2b_branch2b_1/Conv2D" + input: "bn2b_branch2b_1/ReadVariableOp" + input: "bn2b_branch2b_1/FusedBatchNormV2:3" + input: "bn2b_branch2b_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_61" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res2b_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res2b_branch2c_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res2b_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res2b_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res2b_branch2b_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn2b_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_21" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn2b_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_20" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn2b_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res2b_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_4_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res2b_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn2b_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_22" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_61" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_4_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res2b_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_4_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_20/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2b_branch2b_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_20_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_20/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_21" + input: "training/LossScaleOptimizer/SGD/update_20/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_19/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2b_branch2b_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_19_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_19/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_20" + input: "training/LossScaleOptimizer/SGD/update_19/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res2b_branch2b_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res2b_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_21/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res2b_branch2c_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_21_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_21/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_22" + input: "training/LossScaleOptimizer/SGD/update_21/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn2b_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_4_1/Relu_grad/ReluGrad" + input: "res2b_branch2a_1/Conv2D" + input: "bn2b_branch2a_1/ReadVariableOp" + input: "bn2b_branch2a_1/FusedBatchNormV2:3" + input: "bn2b_branch2a_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_62" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res2b_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res2b_branch2b_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res2b_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res2b_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res2b_branch2a_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn2b_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_18" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn2b_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_17" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn2b_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res2b_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_3_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res2b_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn2b_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_19" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_62" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_63" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/activation_6_1/Relu_grad/ReluGrad" + input: "training/LossScaleOptimizer/gradients/res2b_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_17/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2b_branch2a_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_17_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_17/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_18" + input: "training/LossScaleOptimizer/SGD/update_17/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_16/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2b_branch2a_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_16_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_16/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_17" + input: "training/LossScaleOptimizer/SGD/update_16/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res2b_branch2a_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res2b_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_18/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res2b_branch2b_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_18_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_18/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_19" + input: "training/LossScaleOptimizer/SGD/update_18/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_3_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/AddN_63" + input: "activation_3_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_64" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res2b_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res2b_branch2a_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn2a_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_3_1/Relu_grad/ReluGrad" + input: "res2a_branch2c_1/Conv2D" + input: "bn2a_branch2c_1/ReadVariableOp" + input: "bn2a_branch2c_1/FusedBatchNormV2:3" + input: "bn2a_branch2c_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn2a_branch1_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_3_1/Relu_grad/ReluGrad" + input: "res2a_branch1_1/Conv2D" + input: "bn2a_branch1_1/ReadVariableOp" + input: "bn2a_branch1_1/FusedBatchNormV2:3" + input: "bn2a_branch1_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_16" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_64" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res2a_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_2_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res2a_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn2a_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res2a_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res2a_branch2c_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res2a_branch2c_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn2a_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_13" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn2a_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_12" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn2a_branch2c_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res2a_branch1_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res2a_branch1_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res2a_branch1_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn2a_branch1_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_15" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn2a_branch1_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_14" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn2a_branch1_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res2a_branch1_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "max_pooling2d_1/MaxPool" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res2a_branch1_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn2a_branch1_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_15/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res2b_branch2a_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_15_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_15/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_16" + input: "training/LossScaleOptimizer/SGD/update_15/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res2a_branch2c_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res2a_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_2_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res2a_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_2_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_12/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2a_branch2c_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_12_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_12/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_13" + input: "training/LossScaleOptimizer/SGD/update_12/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_11/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2a_branch2c_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_11_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_11/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_12" + input: "training/LossScaleOptimizer/SGD/update_11/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_14/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2a_branch1_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_14_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_14/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_15" + input: "training/LossScaleOptimizer/SGD/update_14/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_13/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2a_branch1_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_13_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_13/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_14" + input: "training/LossScaleOptimizer/SGD/update_13/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res2a_branch1_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res2a_branch1_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_65" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res2a_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res2a_branch2c_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn2a_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_2_1/Relu_grad/ReluGrad" + input: "res2a_branch2b_1/Conv2D" + input: "bn2a_branch2b_1/ReadVariableOp" + input: "bn2a_branch2b_1/FusedBatchNormV2:3" + input: "bn2a_branch2b_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_66" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res2a_branch1/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res2a_branch1_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_10" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_65" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_9" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn2a_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_8" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn2a_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res2a_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_1_1/Relu" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res2a_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn2a_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res2a_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res2a_branch2b_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res2a_branch2b_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn2a_branch2b_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_11" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_66" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_9/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res2a_branch2c_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_9_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_9/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_10" + input: "training/LossScaleOptimizer/SGD/update_9/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_8/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2a_branch2b_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_8_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_8/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_9" + input: "training/LossScaleOptimizer/SGD/update_8/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_7/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2a_branch2b_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_7_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_7/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_8" + input: "training/LossScaleOptimizer/SGD/update_7/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res2a_branch2b_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res2a_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_1_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/res2a_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_1_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_10/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res2a_branch1_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_10_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_10/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_11" + input: "training/LossScaleOptimizer/SGD/update_10/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_67" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res2a_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res2a_branch2b_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn2a_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_1_1/Relu_grad/ReluGrad" + input: "res2a_branch2a_1/Conv2D" + input: "bn2a_branch2a_1/ReadVariableOp" + input: "bn2a_branch2a_1/FusedBatchNormV2:3" + input: "bn2a_branch2a_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_7" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_67" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_6" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn2a_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_5" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn2a_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res2a_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "max_pooling2d_1/MaxPool" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res2a_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn2a_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res2a_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/res2a_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res2a_branch2a_1/Conv2D/Cast" + input: "training/LossScaleOptimizer/gradients/bn2a_branch2a_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_6/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res2a_branch2b_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_6_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_6/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_7" + input: "training/LossScaleOptimizer/SGD/update_6/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_5/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2a_branch2a_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_5_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_5/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_6" + input: "training/LossScaleOptimizer/SGD/update_5/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_4/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2a_branch2a_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_4_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_4/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_5" + input: "training/LossScaleOptimizer/SGD/update_4/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/res2a_branch2a_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/res2a_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_68" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/res2a_branch1_1/Conv2D_grad/Conv2DBackpropInput" + input: "training/LossScaleOptimizer/gradients/res2a_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_69" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/res2a_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/res2a_branch2a_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/max_pooling2d_1/MaxPool_grad/MaxPoolGrad" + op: "MaxPoolGrad" + input: "pool1_pad_1/Pad" + input: "max_pooling2d_1/MaxPool" + input: "training/LossScaleOptimizer/gradients/AddN_68" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "ksize" + value { + list { + i: 1 + i: 1 + i: 3 + i: 3 + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } +} +node { + name: "training/LossScaleOptimizer/mul_4" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_69" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/pool1_pad_1/Pad_grad/Slice_1" + op: "Slice" + input: "training/LossScaleOptimizer/gradients/max_pooling2d_1/MaxPool_grad/MaxPoolGrad" + input: "training/LossScaleOptimizer/gradients/pool1_pad_1/Pad_grad/Reshape" + input: "training/LossScaleOptimizer/gradients/pool1_pad_1/Pad_grad/Shape" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "Index" + value { + type: DT_INT32 + } + } + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_3/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res2a_branch2a_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_3_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_3/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_4" + input: "training/LossScaleOptimizer/SGD/update_3/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/activation_50/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/LossScaleOptimizer/gradients/pool1_pad_1/Pad_grad/Slice_1" + input: "activation_50/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/bn_conv1_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + op: "FusedBatchNormGradV2" + input: "training/LossScaleOptimizer/gradients/activation_50/Relu_grad/ReluGrad" + input: "conv1_1/Conv2D" + input: "bn_conv1_1/ReadVariableOp" + input: "bn_conv1_1/FusedBatchNormV2:3" + input: "bn_conv1_1/FusedBatchNormV2:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "U" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/mul_3" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn_conv1_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:2" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_2" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/bn_conv1_1/FusedBatchNormV2_grad/FusedBatchNormGradV2:1" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/conv1_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "conv1_pad_1/Pad" + input: "ConstantFolding/training/LossScaleOptimizer/gradients/conv1_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/LossScaleOptimizer/gradients/bn_conv1_1/FusedBatchNormV2_grad/FusedBatchNormGradV2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_HALF + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_2/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn_conv1_cond_input_1" + input: "training_lossscaleoptimizer_sgd_update_2_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_2/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_3" + input: "training/LossScaleOptimizer/SGD/update_2/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update_1/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn_conv1_cond_input_0" + input: "training_lossscaleoptimizer_sgd_update_1_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update_1/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_2" + input: "training/LossScaleOptimizer/SGD/update_1/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/conv1_1/Conv2D/Cast_grad/Cast" + op: "Cast" + input: "training/LossScaleOptimizer/gradients/conv1_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_HALF + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/gradients/AddN_70" + op: "AddN" + input: "training/LossScaleOptimizer/gradients/loss_1/conv1/kernel/Regularizer/Square_grad/Mul_1" + input: "training/LossScaleOptimizer/gradients/conv1_1/Conv2D/Cast_grad/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/mul_1" + op: "Mul" + input: "training/LossScaleOptimizer/gradients/AddN_70" + input: "training/LossScaleOptimizer/truediv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/update/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "conv1_conv2d_readvariableop_resource" + input: "training_lossscaleoptimizer_sgd_update_resourceapplykerasmomentum_accum" + input: "training/LossScaleOptimizer/SGD/update/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/LossScaleOptimizer/mul_1" + input: "training/LossScaleOptimizer/SGD/update/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/Const" + op: "Const" + input: "^training/LossScaleOptimizer/SGD/update/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_1/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_10/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_100/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_101/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_102/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_103/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_104/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_105/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_106/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_107/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_108/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_109/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_11/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_110/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_111/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_112/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_113/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_114/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_115/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_116/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_117/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_118/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_119/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_12/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_120/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_121/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_122/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_123/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_124/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_125/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_126/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_127/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_128/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_129/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_13/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_130/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_131/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_132/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_133/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_134/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_135/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_136/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_137/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_138/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_139/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_14/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_140/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_141/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_142/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_143/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_144/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_145/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_146/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_147/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_148/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_149/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_15/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_150/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_151/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_152/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_153/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_154/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_155/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_156/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_157/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_158/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_159/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_16/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_160/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_17/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_18/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_19/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_2/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_20/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_21/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_22/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_23/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_24/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_25/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_26/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_27/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_28/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_29/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_3/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_30/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_31/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_32/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_33/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_34/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_35/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_36/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_37/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_38/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_39/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_4/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_40/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_41/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_42/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_43/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_44/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_45/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_46/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_47/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_48/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_49/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_5/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_50/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_51/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_52/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_53/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_54/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_55/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_56/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_57/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_58/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_59/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_6/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_60/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_61/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_62/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_63/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_64/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_65/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_66/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_67/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_68/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_69/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_7/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_70/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_71/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_72/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_73/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_74/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_75/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_76/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_77/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_78/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_79/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_8/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_80/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_81/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_82/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_83/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_84/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_85/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_86/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_87/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_88/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_89/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_9/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_90/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_91/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_92/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_93/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_94/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_95/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_96/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_97/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_98/ResourceApplyKerasMomentum" + input: "^training/LossScaleOptimizer/SGD/update_99/ResourceApplyKerasMomentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT64 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT64 + tensor_shape { + } + int64_val: 1 + } + } + } +} +node { + name: "training/LossScaleOptimizer/SGD/AssignAddVariableOp" + op: "AssignAddVariableOp" + input: "training_lossscaleoptimizer_sgd_assignaddvariableop_resource" + input: "training/LossScaleOptimizer/SGD/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT64 + } + } +} +node { + name: "training_1/Identity" + op: "Identity" + input: "loss_1/ArithmeticOptimizer/AddOpsRewrite_add" + input: "^bn2a_branch1/cond_1/branch_executed/_286" + input: "^bn2a_branch1_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn2a_branch1_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn2a_branch2a/cond_1/branch_executed/_223" + input: "^bn2a_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn2a_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn2a_branch2b/cond_1/branch_executed/_20" + input: "^bn2a_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn2a_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn2a_branch2c/cond_1/branch_executed/_279" + input: "^bn2a_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn2a_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn2b_branch2a/cond_1/branch_executed/_293" + input: "^bn2b_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn2b_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn2b_branch2b/cond_1/branch_executed/_125" + input: "^bn2b_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn2b_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn2b_branch2c/cond_1/branch_executed/_174" + input: "^bn2b_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn2b_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn2c_branch2a/cond_1/branch_executed/_230" + input: "^bn2c_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn2c_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn2c_branch2b/cond_1/branch_executed/_321" + input: "^bn2c_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn2c_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn2c_branch2c/cond_1/branch_executed/_27" + input: "^bn2c_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn2c_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn3a_branch1/cond_1/branch_executed/_132" + input: "^bn3a_branch1_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn3a_branch1_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn3a_branch2a/cond_1/branch_executed/_6" + input: "^bn3a_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn3a_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn3a_branch2b/cond_1/branch_executed/_34" + input: "^bn3a_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn3a_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn3a_branch2c/cond_1/branch_executed/_90" + input: "^bn3a_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn3a_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn3b_branch2a/cond_1/branch_executed/_181" + input: "^bn3b_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn3b_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn3b_branch2b/cond_1/branch_executed/_356" + input: "^bn3b_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn3b_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn3b_branch2c/cond_1/branch_executed/_237" + input: "^bn3b_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn3b_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn3c_branch2a/cond_1/branch_executed/_328" + input: "^bn3c_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn3c_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn3c_branch2b/cond_1/branch_executed/_300" + input: "^bn3c_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn3c_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn3c_branch2c/cond_1/branch_executed/_335" + input: "^bn3c_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn3c_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn3d_branch2a/cond_1/branch_executed/_363" + input: "^bn3d_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn3d_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn3d_branch2b/cond_1/branch_executed/_244" + input: "^bn3d_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn3d_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn3d_branch2c/cond_1/branch_executed/_41" + input: "^bn3d_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn3d_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4a_branch1/cond_1/branch_executed/_370" + input: "^bn4a_branch1_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4a_branch1_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4a_branch2a/cond_1/branch_executed/_97" + input: "^bn4a_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4a_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4a_branch2b/cond_1/branch_executed/_139" + input: "^bn4a_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4a_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4a_branch2c/cond_1/branch_executed/_188" + input: "^bn4a_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4a_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4b_branch2a/cond_1/branch_executed/_13" + input: "^bn4b_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4b_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4b_branch2b/cond_1/branch_executed/_48" + input: "^bn4b_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4b_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4b_branch2c/cond_1/branch_executed/_69" + input: "^bn4b_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4b_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4c_branch2a/cond_1/branch_executed/_104" + input: "^bn4c_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4c_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4c_branch2b/cond_1/branch_executed/_195" + input: "^bn4c_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4c_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4c_branch2c/cond_1/branch_executed/_251" + input: "^bn4c_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4c_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4d_branch2a/cond_1/branch_executed/_146" + input: "^bn4d_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4d_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4d_branch2b/cond_1/branch_executed/_342" + input: "^bn4d_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4d_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4d_branch2c/cond_1/branch_executed/_55" + input: "^bn4d_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4d_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4e_branch2a/cond_1/branch_executed/_76" + input: "^bn4e_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4e_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4e_branch2b/cond_1/branch_executed/_111" + input: "^bn4e_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4e_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4e_branch2c/cond_1/branch_executed/_202" + input: "^bn4e_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4e_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4f_branch2a/cond_1/branch_executed/_258" + input: "^bn4f_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4f_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4f_branch2b/cond_1/branch_executed/_153" + input: "^bn4f_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4f_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4f_branch2c/cond_1/branch_executed/_307" + input: "^bn4f_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4f_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn5a_branch1/cond_1/branch_executed/_209" + input: "^bn5a_branch1_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn5a_branch1_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn5a_branch2a/cond_1/branch_executed/_62" + input: "^bn5a_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn5a_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn5a_branch2b/cond_1/branch_executed/_83" + input: "^bn5a_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn5a_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn5a_branch2c/cond_1/branch_executed/_118" + input: "^bn5a_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn5a_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn5b_branch2a/cond_1/branch_executed/_160" + input: "^bn5b_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn5b_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn5b_branch2b/cond_1/branch_executed/_265" + input: "^bn5b_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn5b_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn5b_branch2c/cond_1/branch_executed/_314" + input: "^bn5b_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn5b_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn5c_branch2a/cond_1/branch_executed/_349" + input: "^bn5c_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn5c_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn5c_branch2b/cond_1/branch_executed/_216" + input: "^bn5c_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn5c_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn5c_branch2c/cond_1/branch_executed/_272" + input: "^bn5c_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn5c_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn_conv1/cond_1/branch_executed/_167" + input: "^bn_conv1_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn_conv1_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^training/LossScaleOptimizer/SGD/AssignAddVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "identity_RetVal" + op: "_Retval" + input: "training_1/Identity" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "index" + value { + i: 0 + } + } +} +library { + function { + signature { + name: "bn2c_branch2a_cond_1_false_1067" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn3c_branch2a_cond_1_false_2239" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn2b_branch2c_cond_1_true_948" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn2a_branch2a_cond_1_false_247" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4b_branch2c_cond_1_false_3645" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn3b_branch2a_cond_1_true_1886" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn5a_branch2b_cond_1_true_5287" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn5a_branch2a_cond_1_false_5171" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn3b_branch2b_cond_1_true_2003" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4a_branch2a_cond_1_true_2942" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn3b_branch2c_cond_1_false_2121" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4a_branch1_cond_1_false_3293" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn5b_branch2c_cond_1_true_5872" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4b_branch2a_cond_1_false_3411" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn3d_branch2a_cond_1_true_2590" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn5c_branch2b_cond_1_false_6108" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn5c_branch2c_cond_1_true_6224" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn5c_branch2c_cond_1_false_6225" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn2c_branch2b_cond_1_true_1183" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4a_branch1_cond_1_true_3292" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4f_branch2b_cond_1_true_4935" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn3a_branch2b_cond_1_true_1535" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn5b_branch2a_cond_1_false_5639" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn2c_branch2c_cond_1_false_1301" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn2a_branch2c_cond_1_true_480" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn3a_branch2a_cond_1_false_1419" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4e_branch2c_cond_1_true_4700" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4f_branch2b_cond_1_false_4936" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4c_branch2c_cond_1_false_3997" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4f_branch2a_cond_1_false_4819" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn2a_branch2a_cond_1_true_246" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn5a_branch2c_cond_1_false_5405" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4c_branch2a_cond_1_false_3763" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn3d_branch2c_cond_1_false_2825" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn3b_branch2a_cond_1_false_1887" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4c_branch2b_cond_1_true_3879" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn2a_branch1_cond_1_false_597" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn2c_branch2a_cond_1_true_1066" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn3a_branch2c_cond_1_false_1653" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn3a_branch1_cond_1_false_1769" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4a_branch2a_cond_1_false_2943" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn5c_branch2a_cond_1_true_5990" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn5a_branch2c_cond_1_true_5404" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn3a_branch2b_cond_1_false_1536" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4b_branch2c_cond_1_true_3644" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn2a_branch2c_cond_1_false_481" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn5a_branch2a_cond_1_true_5170" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn3a_branch2a_cond_1_true_1418" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4e_branch2a_cond_1_true_4466" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn3c_branch2c_cond_1_false_2473" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4a_branch2c_cond_1_true_3176" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn5c_branch2a_cond_1_false_5991" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn3c_branch2b_cond_1_false_2356" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn3c_branch2a_cond_1_true_2238" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn5b_branch2a_cond_1_true_5638" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4c_branch2a_cond_1_true_3762" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4a_branch2b_cond_1_false_3060" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4c_branch2b_cond_1_false_3880" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn2c_branch2c_cond_1_true_1300" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4b_branch2b_cond_1_true_3527" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4f_branch2a_cond_1_true_4818" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn2b_branch2a_cond_1_true_714" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4e_branch2b_cond_1_true_4583" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn3c_branch2c_cond_1_true_2472" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn5a_branch1_cond_1_false_5521" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn5b_branch2c_cond_1_false_5873" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4c_branch2c_cond_1_true_3996" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn2b_branch2b_cond_1_true_831" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn3a_branch1_cond_1_true_1768" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn2a_branch2b_cond_1_true_363" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4e_branch2c_cond_1_false_4701" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn_conv1_cond_1_true_126" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn3c_branch2b_cond_1_true_2355" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4d_branch2c_cond_1_true_4348" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn2c_branch2b_cond_1_false_1184" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4d_branch2a_cond_1_false_4115" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4a_branch2c_cond_1_false_3177" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn3d_branch2b_cond_1_false_2708" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn3a_branch2c_cond_1_true_1652" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4b_branch2a_cond_1_true_3410" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn2a_branch1_cond_1_true_596" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn5c_branch2b_cond_1_true_6107" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn3d_branch2b_cond_1_true_2707" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4e_branch2b_cond_1_false_4584" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn_conv1_cond_1_false_127" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn5a_branch2b_cond_1_false_5288" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn3d_branch2a_cond_1_false_2591" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn2a_branch2b_cond_1_false_364" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4d_branch2b_cond_1_false_4232" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4e_branch2a_cond_1_false_4467" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4a_branch2b_cond_1_true_3059" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4b_branch2b_cond_1_false_3528" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4f_branch2c_cond_1_true_5052" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn5a_branch1_cond_1_true_5520" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn3b_branch2c_cond_1_true_2120" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4d_branch2a_cond_1_true_4114" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn3b_branch2b_cond_1_false_2004" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4d_branch2c_cond_1_false_4349" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn2b_branch2c_cond_1_false_949" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn5b_branch2b_cond_1_false_5756" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn5b_branch2b_cond_1_true_5755" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn2b_branch2b_cond_1_false_832" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn2b_branch2a_cond_1_false_715" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4f_branch2c_cond_1_false_5053" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 1 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn4d_branch2b_cond_1_true_4231" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } + function { + signature { + name: "bn3d_branch2c_cond_1_true_2824" + output_arg { + name: "const" + type: DT_FLOAT + } + } + node_def { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } + } + ret { + key: "const" + value: "Const:output:0" + } + } +} +versions { + producer: 29 +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/keras_imagenet_main_graph_mode.golden_summary b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/keras_imagenet_main_graph_mode.golden_summary new file mode 100644 index 0000000000000000000000000000000000000000..d99b22a4f9d88b36216926f6b036e7e0abc3982f --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/keras_imagenet_main_graph_mode.golden_summary @@ -0,0 +1,51 @@ +Clustered nodes: 2178 +Unclustered nodes: 446 +Number of clusters: 1 + +unclustered size 446 + AssignAddVariableOp 2 + Const 2 + DivNoNan 1 + Identity 1 + NoOp 1 + ReadVariableOp 2 + VarHandleOp 435 + _Retval 2 +cluster 0 size 2178 + Add 17 + AddN 72 + ArgMax 1 + AssignAddVariableOp 1 + AssignSubVariableOp 106 + BiasAdd 1 + BiasAddGrad 1 + Cast 3 + Const 357 + Conv2D 53 + Conv2DBackpropFilter 53 + Conv2DBackpropInput 52 + DivNoNan 1 + Equal 1 + FusedBatchNorm 53 + FusedBatchNormGrad 53 + Identity 2 + MatMul 3 + MaxPool 1 + MaxPoolGrad 1 + Mean 1 + Mul 164 + Pad 1 + ReadVariableOp 646 + Relu 49 + ReluGrad 49 + Reshape 2 + ResourceApplyKerasMomentum 161 + ShapeN 50 + Softmax 1 + SparseSoftmaxCrossEntropyWithLogits 1 + Square 55 + Squeeze 1 + Sub 106 + Sum 57 + Tile 1 + Transpose 1 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/keras_imagenet_main_graph_mode.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/keras_imagenet_main_graph_mode.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..574f83ddb13d77a5aed4df61a58a5fadfe2590a0 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/keras_imagenet_main_graph_mode.pbtxt @@ -0,0 +1,59776 @@ +node { + name: "G4SM2HGMB5" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@G4SM2HGMB5/Read/ReadVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 117 + } + dim { + size: 224 + } + dim { + size: 224 + } + dim { + size: 3 + } + } + } + } + attr { + key: "shared_name" + value { + s: "G4SM2HGMB5" + } + } +} +node { + name: "HK0SX0WHBZ" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@HK0SX0WHBZ/Read/ReadVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 117 + } + dim { + size: 1 + } + } + } + } + attr { + key: "shared_name" + value { + s: "HK0SX0WHBZ" + } + } +} +node { + name: "bn2a_branch1/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn2a_branch1_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2a_branch1/moving_mean" + } + } +} +node { + name: "bn2a_branch1/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn2a_branch1_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2a_branch1/moving_variance" + } + } +} +node { + name: "bn2a_branch2a/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn2a_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2a_branch2a/moving_mean" + } + } +} +node { + name: "bn2a_branch2a/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn2a_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2a_branch2a/moving_variance" + } + } +} +node { + name: "bn2a_branch2b/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn2a_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2a_branch2b/moving_mean" + } + } +} +node { + name: "bn2a_branch2b/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn2a_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2a_branch2b/moving_variance" + } + } +} +node { + name: "bn2a_branch2c/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn2a_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2a_branch2c/moving_mean" + } + } +} +node { + name: "bn2a_branch2c/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn2a_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2a_branch2c/moving_variance" + } + } +} +node { + name: "bn2b_branch2a/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn2b_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2b_branch2a/moving_mean" + } + } +} +node { + name: "bn2b_branch2a/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn2b_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2b_branch2a/moving_variance" + } + } +} +node { + name: "bn2b_branch2b/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn2b_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2b_branch2b/moving_mean" + } + } +} +node { + name: "bn2b_branch2b/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn2b_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2b_branch2b/moving_variance" + } + } +} +node { + name: "bn2b_branch2c/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn2b_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2b_branch2c/moving_mean" + } + } +} +node { + name: "bn2b_branch2c/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn2b_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2b_branch2c/moving_variance" + } + } +} +node { + name: "bn2c_branch2a/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn2c_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2c_branch2a/moving_mean" + } + } +} +node { + name: "bn2c_branch2a/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn2c_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2c_branch2a/moving_variance" + } + } +} +node { + name: "bn2c_branch2b/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn2c_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2c_branch2b/moving_mean" + } + } +} +node { + name: "bn2c_branch2b/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn2c_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2c_branch2b/moving_variance" + } + } +} +node { + name: "bn2c_branch2c/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn2c_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2c_branch2c/moving_mean" + } + } +} +node { + name: "bn2c_branch2c/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn2c_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2c_branch2c/moving_variance" + } + } +} +node { + name: "bn3a_branch1/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn3a_branch1_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3a_branch1/moving_mean" + } + } +} +node { + name: "bn3a_branch1/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn3a_branch1_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3a_branch1/moving_variance" + } + } +} +node { + name: "bn3a_branch2a/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn3a_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3a_branch2a/moving_mean" + } + } +} +node { + name: "bn3a_branch2a/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn3a_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3a_branch2a/moving_variance" + } + } +} +node { + name: "bn3a_branch2b/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn3a_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3a_branch2b/moving_mean" + } + } +} +node { + name: "bn3a_branch2b/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn3a_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3a_branch2b/moving_variance" + } + } +} +node { + name: "bn3a_branch2c/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn3a_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3a_branch2c/moving_mean" + } + } +} +node { + name: "bn3a_branch2c/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn3a_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3a_branch2c/moving_variance" + } + } +} +node { + name: "bn3b_branch2a/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn3b_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3b_branch2a/moving_mean" + } + } +} +node { + name: "bn3b_branch2a/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn3b_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3b_branch2a/moving_variance" + } + } +} +node { + name: "bn3b_branch2b/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn3b_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3b_branch2b/moving_mean" + } + } +} +node { + name: "bn3b_branch2b/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn3b_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3b_branch2b/moving_variance" + } + } +} +node { + name: "bn3b_branch2c/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn3b_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3b_branch2c/moving_mean" + } + } +} +node { + name: "bn3b_branch2c/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn3b_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3b_branch2c/moving_variance" + } + } +} +node { + name: "bn3c_branch2a/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn3c_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3c_branch2a/moving_mean" + } + } +} +node { + name: "bn3c_branch2a/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn3c_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3c_branch2a/moving_variance" + } + } +} +node { + name: "bn3c_branch2b/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn3c_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3c_branch2b/moving_mean" + } + } +} +node { + name: "bn3c_branch2b/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn3c_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3c_branch2b/moving_variance" + } + } +} +node { + name: "bn3c_branch2c/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn3c_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3c_branch2c/moving_mean" + } + } +} +node { + name: "bn3c_branch2c/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn3c_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3c_branch2c/moving_variance" + } + } +} +node { + name: "bn3d_branch2a/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn3d_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3d_branch2a/moving_mean" + } + } +} +node { + name: "bn3d_branch2a/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn3d_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3d_branch2a/moving_variance" + } + } +} +node { + name: "bn3d_branch2b/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn3d_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3d_branch2b/moving_mean" + } + } +} +node { + name: "bn3d_branch2b/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn3d_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3d_branch2b/moving_variance" + } + } +} +node { + name: "bn3d_branch2c/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn3d_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3d_branch2c/moving_mean" + } + } +} +node { + name: "bn3d_branch2c/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn3d_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3d_branch2c/moving_variance" + } + } +} +node { + name: "bn4a_branch1/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4a_branch1_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4a_branch1/moving_mean" + } + } +} +node { + name: "bn4a_branch1/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4a_branch1_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4a_branch1/moving_variance" + } + } +} +node { + name: "bn4a_branch2a/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4a_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4a_branch2a/moving_mean" + } + } +} +node { + name: "bn4a_branch2a/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4a_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4a_branch2a/moving_variance" + } + } +} +node { + name: "bn4a_branch2b/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4a_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4a_branch2b/moving_mean" + } + } +} +node { + name: "bn4a_branch2b/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4a_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4a_branch2b/moving_variance" + } + } +} +node { + name: "bn4a_branch2c/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4a_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4a_branch2c/moving_mean" + } + } +} +node { + name: "bn4a_branch2c/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4a_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4a_branch2c/moving_variance" + } + } +} +node { + name: "bn4b_branch2a/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4b_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4b_branch2a/moving_mean" + } + } +} +node { + name: "bn4b_branch2a/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4b_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4b_branch2a/moving_variance" + } + } +} +node { + name: "bn4b_branch2b/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4b_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4b_branch2b/moving_mean" + } + } +} +node { + name: "bn4b_branch2b/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4b_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4b_branch2b/moving_variance" + } + } +} +node { + name: "bn4b_branch2c/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4b_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4b_branch2c/moving_mean" + } + } +} +node { + name: "bn4b_branch2c/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4b_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4b_branch2c/moving_variance" + } + } +} +node { + name: "bn4c_branch2a/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4c_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4c_branch2a/moving_mean" + } + } +} +node { + name: "bn4c_branch2a/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4c_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4c_branch2a/moving_variance" + } + } +} +node { + name: "bn4c_branch2b/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4c_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4c_branch2b/moving_mean" + } + } +} +node { + name: "bn4c_branch2b/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4c_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4c_branch2b/moving_variance" + } + } +} +node { + name: "bn4c_branch2c/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4c_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4c_branch2c/moving_mean" + } + } +} +node { + name: "bn4c_branch2c/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4c_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4c_branch2c/moving_variance" + } + } +} +node { + name: "bn4d_branch2a/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4d_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4d_branch2a/moving_mean" + } + } +} +node { + name: "bn4d_branch2a/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4d_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4d_branch2a/moving_variance" + } + } +} +node { + name: "bn4d_branch2b/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4d_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4d_branch2b/moving_mean" + } + } +} +node { + name: "bn4d_branch2b/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4d_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4d_branch2b/moving_variance" + } + } +} +node { + name: "bn4d_branch2c/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4d_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4d_branch2c/moving_mean" + } + } +} +node { + name: "bn4d_branch2c/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4d_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4d_branch2c/moving_variance" + } + } +} +node { + name: "bn4e_branch2a/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4e_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4e_branch2a/moving_mean" + } + } +} +node { + name: "bn4e_branch2a/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4e_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4e_branch2a/moving_variance" + } + } +} +node { + name: "bn4e_branch2b/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4e_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4e_branch2b/moving_mean" + } + } +} +node { + name: "bn4e_branch2b/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4e_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4e_branch2b/moving_variance" + } + } +} +node { + name: "bn4e_branch2c/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4e_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4e_branch2c/moving_mean" + } + } +} +node { + name: "bn4e_branch2c/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4e_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4e_branch2c/moving_variance" + } + } +} +node { + name: "bn4f_branch2a/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4f_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4f_branch2a/moving_mean" + } + } +} +node { + name: "bn4f_branch2a/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4f_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4f_branch2a/moving_variance" + } + } +} +node { + name: "bn4f_branch2b/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4f_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4f_branch2b/moving_mean" + } + } +} +node { + name: "bn4f_branch2b/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4f_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4f_branch2b/moving_variance" + } + } +} +node { + name: "bn4f_branch2c/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4f_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4f_branch2c/moving_mean" + } + } +} +node { + name: "bn4f_branch2c/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn4f_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4f_branch2c/moving_variance" + } + } +} +node { + name: "bn5a_branch1/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn5a_branch1_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5a_branch1/moving_mean" + } + } +} +node { + name: "bn5a_branch1/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn5a_branch1_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5a_branch1/moving_variance" + } + } +} +node { + name: "bn5a_branch2a/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn5a_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5a_branch2a/moving_mean" + } + } +} +node { + name: "bn5a_branch2a/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn5a_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5a_branch2a/moving_variance" + } + } +} +node { + name: "bn5a_branch2b/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn5a_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5a_branch2b/moving_mean" + } + } +} +node { + name: "bn5a_branch2b/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn5a_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5a_branch2b/moving_variance" + } + } +} +node { + name: "bn5a_branch2c/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn5a_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5a_branch2c/moving_mean" + } + } +} +node { + name: "bn5a_branch2c/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn5a_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5a_branch2c/moving_variance" + } + } +} +node { + name: "bn5b_branch2a/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn5b_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5b_branch2a/moving_mean" + } + } +} +node { + name: "bn5b_branch2a/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn5b_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5b_branch2a/moving_variance" + } + } +} +node { + name: "bn5b_branch2b/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn5b_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5b_branch2b/moving_mean" + } + } +} +node { + name: "bn5b_branch2b/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn5b_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5b_branch2b/moving_variance" + } + } +} +node { + name: "bn5b_branch2c/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn5b_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5b_branch2c/moving_mean" + } + } +} +node { + name: "bn5b_branch2c/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn5b_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5b_branch2c/moving_variance" + } + } +} +node { + name: "bn5c_branch2a/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn5c_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5c_branch2a/moving_mean" + } + } +} +node { + name: "bn5c_branch2a/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn5c_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5c_branch2a/moving_variance" + } + } +} +node { + name: "bn5c_branch2b/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn5c_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5c_branch2b/moving_mean" + } + } +} +node { + name: "bn5c_branch2b/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn5c_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5c_branch2b/moving_variance" + } + } +} +node { + name: "bn5c_branch2c/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn5c_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5c_branch2c/moving_mean" + } + } +} +node { + name: "bn5c_branch2c/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn5c_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5c_branch2c/moving_variance" + } + } +} +node { + name: "bn_conv1/moving_mean" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn_conv1_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn_conv1/moving_mean" + } + } +} +node { + name: "bn_conv1_1/AssignMovingAvg_1/sub" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + tensor_content: "\320\314\314=" + } + } + } +} +node { + name: "bn_conv1/moving_variance" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bn_conv1_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn_conv1/moving_variance" + } + } +} +node { + name: "training/SGD/iter" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_INT64 + } + } + attr { + key: "shape" + value { + shape { + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/iter" + } + } +} +node { + name: "training/SGD/bn2a_branch1/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn2a_branch1/beta/momentum" + } + } +} +node { + name: "training/SGD/bn2a_branch1/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn2a_branch1/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn2a_branch2a/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn2a_branch2a/beta/momentum" + } + } +} +node { + name: "training/SGD/bn2a_branch2a/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn2a_branch2a/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn2a_branch2b/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn2a_branch2b/beta/momentum" + } + } +} +node { + name: "training/SGD/bn2a_branch2b/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn2a_branch2b/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn2a_branch2c/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn2a_branch2c/beta/momentum" + } + } +} +node { + name: "training/SGD/bn2a_branch2c/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn2a_branch2c/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn2b_branch2a/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn2b_branch2a/beta/momentum" + } + } +} +node { + name: "training/SGD/bn2b_branch2a/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn2b_branch2a/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn2b_branch2b/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn2b_branch2b/beta/momentum" + } + } +} +node { + name: "training/SGD/bn2b_branch2b/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn2b_branch2b/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn2b_branch2c/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn2b_branch2c/beta/momentum" + } + } +} +node { + name: "training/SGD/bn2b_branch2c/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn2b_branch2c/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn2c_branch2a/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn2c_branch2a/beta/momentum" + } + } +} +node { + name: "training/SGD/bn2c_branch2a/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn2c_branch2a/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn2c_branch2b/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn2c_branch2b/beta/momentum" + } + } +} +node { + name: "training/SGD/bn2c_branch2b/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn2c_branch2b/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn2c_branch2c/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn2c_branch2c/beta/momentum" + } + } +} +node { + name: "training/SGD/bn2c_branch2c/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn2c_branch2c/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn3a_branch1/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn3a_branch1/beta/momentum" + } + } +} +node { + name: "training/SGD/bn3a_branch1/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn3a_branch1/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn3a_branch2a/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn3a_branch2a/beta/momentum" + } + } +} +node { + name: "training/SGD/bn3a_branch2a/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn3a_branch2a/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn3a_branch2b/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn3a_branch2b/beta/momentum" + } + } +} +node { + name: "training/SGD/bn3a_branch2b/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn3a_branch2b/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn3a_branch2c/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn3a_branch2c/beta/momentum" + } + } +} +node { + name: "training/SGD/bn3a_branch2c/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn3a_branch2c/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn3b_branch2a/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn3b_branch2a/beta/momentum" + } + } +} +node { + name: "training/SGD/bn3b_branch2a/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn3b_branch2a/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn3b_branch2b/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn3b_branch2b/beta/momentum" + } + } +} +node { + name: "training/SGD/bn3b_branch2b/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn3b_branch2b/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn3b_branch2c/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn3b_branch2c/beta/momentum" + } + } +} +node { + name: "training/SGD/bn3b_branch2c/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn3b_branch2c/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn3c_branch2a/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn3c_branch2a/beta/momentum" + } + } +} +node { + name: "training/SGD/bn3c_branch2a/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn3c_branch2a/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn3c_branch2b/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn3c_branch2b/beta/momentum" + } + } +} +node { + name: "training/SGD/bn3c_branch2b/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn3c_branch2b/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn3c_branch2c/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn3c_branch2c/beta/momentum" + } + } +} +node { + name: "training/SGD/bn3c_branch2c/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn3c_branch2c/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn3d_branch2a/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn3d_branch2a/beta/momentum" + } + } +} +node { + name: "training/SGD/bn3d_branch2a/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn3d_branch2a/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn3d_branch2b/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn3d_branch2b/beta/momentum" + } + } +} +node { + name: "training/SGD/bn3d_branch2b/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn3d_branch2b/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn3d_branch2c/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn3d_branch2c/beta/momentum" + } + } +} +node { + name: "training/SGD/bn3d_branch2c/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn3d_branch2c/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn4a_branch1/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4a_branch1/beta/momentum" + } + } +} +node { + name: "training/SGD/bn4a_branch1/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4a_branch1/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn4a_branch2a/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4a_branch2a/beta/momentum" + } + } +} +node { + name: "training/SGD/bn4a_branch2a/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4a_branch2a/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn4a_branch2b/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4a_branch2b/beta/momentum" + } + } +} +node { + name: "training/SGD/bn4a_branch2b/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4a_branch2b/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn4a_branch2c/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4a_branch2c/beta/momentum" + } + } +} +node { + name: "training/SGD/bn4a_branch2c/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4a_branch2c/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn4b_branch2a/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4b_branch2a/beta/momentum" + } + } +} +node { + name: "training/SGD/bn4b_branch2a/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4b_branch2a/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn4b_branch2b/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4b_branch2b/beta/momentum" + } + } +} +node { + name: "training/SGD/bn4b_branch2b/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4b_branch2b/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn4b_branch2c/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4b_branch2c/beta/momentum" + } + } +} +node { + name: "training/SGD/bn4b_branch2c/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4b_branch2c/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn4c_branch2a/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4c_branch2a/beta/momentum" + } + } +} +node { + name: "training/SGD/bn4c_branch2a/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4c_branch2a/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn4c_branch2b/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4c_branch2b/beta/momentum" + } + } +} +node { + name: "training/SGD/bn4c_branch2b/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4c_branch2b/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn4c_branch2c/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4c_branch2c/beta/momentum" + } + } +} +node { + name: "training/SGD/bn4c_branch2c/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4c_branch2c/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn4d_branch2a/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4d_branch2a/beta/momentum" + } + } +} +node { + name: "training/SGD/bn4d_branch2a/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4d_branch2a/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn4d_branch2b/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4d_branch2b/beta/momentum" + } + } +} +node { + name: "training/SGD/bn4d_branch2b/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4d_branch2b/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn4d_branch2c/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4d_branch2c/beta/momentum" + } + } +} +node { + name: "training/SGD/bn4d_branch2c/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4d_branch2c/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn4e_branch2a/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4e_branch2a/beta/momentum" + } + } +} +node { + name: "training/SGD/bn4e_branch2a/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4e_branch2a/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn4e_branch2b/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4e_branch2b/beta/momentum" + } + } +} +node { + name: "training/SGD/bn4e_branch2b/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4e_branch2b/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn4e_branch2c/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4e_branch2c/beta/momentum" + } + } +} +node { + name: "training/SGD/bn4e_branch2c/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4e_branch2c/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn4f_branch2a/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4f_branch2a/beta/momentum" + } + } +} +node { + name: "training/SGD/bn4f_branch2a/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4f_branch2a/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn4f_branch2b/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4f_branch2b/beta/momentum" + } + } +} +node { + name: "training/SGD/bn4f_branch2b/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4f_branch2b/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn4f_branch2c/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4f_branch2c/beta/momentum" + } + } +} +node { + name: "training/SGD/bn4f_branch2c/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn4f_branch2c/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn5a_branch1/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn5a_branch1/beta/momentum" + } + } +} +node { + name: "training/SGD/bn5a_branch1/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn5a_branch1/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn5a_branch2a/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn5a_branch2a/beta/momentum" + } + } +} +node { + name: "training/SGD/bn5a_branch2a/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn5a_branch2a/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn5a_branch2b/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn5a_branch2b/beta/momentum" + } + } +} +node { + name: "training/SGD/bn5a_branch2b/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn5a_branch2b/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn5a_branch2c/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn5a_branch2c/beta/momentum" + } + } +} +node { + name: "training/SGD/bn5a_branch2c/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn5a_branch2c/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn5b_branch2a/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn5b_branch2a/beta/momentum" + } + } +} +node { + name: "training/SGD/bn5b_branch2a/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn5b_branch2a/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn5b_branch2b/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn5b_branch2b/beta/momentum" + } + } +} +node { + name: "training/SGD/bn5b_branch2b/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn5b_branch2b/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn5b_branch2c/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn5b_branch2c/beta/momentum" + } + } +} +node { + name: "training/SGD/bn5b_branch2c/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn5b_branch2c/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn5c_branch2a/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn5c_branch2a/beta/momentum" + } + } +} +node { + name: "training/SGD/bn5c_branch2a/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn5c_branch2a/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn5c_branch2b/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn5c_branch2b/beta/momentum" + } + } +} +node { + name: "training/SGD/bn5c_branch2b/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn5c_branch2b/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn5c_branch2c/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn5c_branch2c/beta/momentum" + } + } +} +node { + name: "training/SGD/bn5c_branch2c/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn5c_branch2c/gamma/momentum" + } + } +} +node { + name: "training/SGD/bn_conv1/beta/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn_conv1/beta/momentum" + } + } +} +node { + name: "training/SGD/bn_conv1/gamma/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/bn_conv1/gamma/momentum" + } + } +} +node { + name: "training/SGD/conv1/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 7 + } + dim { + size: 7 + } + dim { + size: 3 + } + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/conv1/kernel/momentum" + } + } +} +node { + name: "training/SGD/gradients/loss_1/conv1/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 7 + } + dim { + size: 7 + } + dim { + size: 3 + } + dim { + size: 64 + } + } + float_val: 0.0002 + } + } + } +} +node { + name: "training/SGD/fc1000/bias/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1001 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/fc1000/bias/momentum" + } + } +} +node { + name: "training/SGD/gradients/loss_1/fc1000/bias/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1001 + } + } + float_val: 0.0002 + } + } + } +} +node { + name: "training/SGD/fc1000/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2048 + } + dim { + size: 1001 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/fc1000/kernel/momentum" + } + } +} +node { + name: "training/SGD/gradients/loss_1/fc1000/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 2048 + } + dim { + size: 1001 + } + } + float_val: 0.0002 + } + } + } +} +node { + name: "training/SGD/res2a_branch1/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 64 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res2a_branch1/kernel/momentum" + } + } +} +node { + name: "training/SGD/res2a_branch2a/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 64 + } + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res2a_branch2a/kernel/momentum" + } + } +} +node { + name: "training/SGD/gradients/loss_1/res2a_branch2a/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 64 + } + dim { + size: 64 + } + } + float_val: 0.0002 + } + } + } +} +node { + name: "training/SGD/res2a_branch2b/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 64 + } + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res2a_branch2b/kernel/momentum" + } + } +} +node { + name: "training/SGD/res2a_branch2c/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 64 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res2a_branch2c/kernel/momentum" + } + } +} +node { + name: "training/SGD/res2b_branch2a/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res2b_branch2a/kernel/momentum" + } + } +} +node { + name: "training/SGD/res2b_branch2b/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 64 + } + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res2b_branch2b/kernel/momentum" + } + } +} +node { + name: "training/SGD/res2b_branch2c/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 64 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res2b_branch2c/kernel/momentum" + } + } +} +node { + name: "training/SGD/res2c_branch2a/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res2c_branch2a/kernel/momentum" + } + } +} +node { + name: "training/SGD/gradients/loss_1/res2c_branch2a/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 64 + } + } + float_val: 0.0002 + } + } + } +} +node { + name: "training/SGD/res2c_branch2b/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 64 + } + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res2c_branch2b/kernel/momentum" + } + } +} +node { + name: "training/SGD/gradients/loss_1/res2c_branch2b/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 64 + } + dim { + size: 64 + } + } + float_val: 0.0002 + } + } + } +} +node { + name: "training/SGD/res2c_branch2c/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 64 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res2c_branch2c/kernel/momentum" + } + } +} +node { + name: "training/SGD/gradients/loss_1/res2c_branch2c/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 64 + } + dim { + size: 256 + } + } + float_val: 0.0002 + } + } + } +} +node { + name: "training/SGD/res3a_branch1/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res3a_branch1/kernel/momentum" + } + } +} +node { + name: "training/SGD/gradients/loss_1/res3a_branch1/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 512 + } + } + float_val: 0.0002 + } + } + } +} +node { + name: "training/SGD/res3a_branch2a/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res3a_branch2a/kernel/momentum" + } + } +} +node { + name: "training/SGD/gradients/loss_1/res3a_branch2a/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 128 + } + } + float_val: 0.0002 + } + } + } +} +node { + name: "training/SGD/res3a_branch2b/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 128 + } + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res3a_branch2b/kernel/momentum" + } + } +} +node { + name: "training/SGD/res3a_branch2c/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 128 + } + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res3a_branch2c/kernel/momentum" + } + } +} +node { + name: "training/SGD/res3b_branch2a/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res3b_branch2a/kernel/momentum" + } + } +} +node { + name: "training/SGD/res3b_branch2b/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 128 + } + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res3b_branch2b/kernel/momentum" + } + } +} +node { + name: "training/SGD/res3b_branch2c/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 128 + } + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res3b_branch2c/kernel/momentum" + } + } +} +node { + name: "training/SGD/res3c_branch2a/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res3c_branch2a/kernel/momentum" + } + } +} +node { + name: "training/SGD/res3c_branch2b/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 128 + } + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res3c_branch2b/kernel/momentum" + } + } +} +node { + name: "training/SGD/res3c_branch2c/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 128 + } + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res3c_branch2c/kernel/momentum" + } + } +} +node { + name: "training/SGD/res3d_branch2a/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res3d_branch2a/kernel/momentum" + } + } +} +node { + name: "training/SGD/gradients/loss_1/res3d_branch2a/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 128 + } + } + float_val: 0.0002 + } + } + } +} +node { + name: "training/SGD/res3d_branch2b/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 128 + } + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res3d_branch2b/kernel/momentum" + } + } +} +node { + name: "training/SGD/gradients/loss_1/res3d_branch2b/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 128 + } + dim { + size: 128 + } + } + float_val: 0.0002 + } + } + } +} +node { + name: "training/SGD/res3d_branch2c/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 128 + } + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res3d_branch2c/kernel/momentum" + } + } +} +node { + name: "training/SGD/gradients/loss_1/res3d_branch2c/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 128 + } + dim { + size: 512 + } + } + float_val: 0.0002 + } + } + } +} +node { + name: "training/SGD/res4a_branch1/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res4a_branch1/kernel/momentum" + } + } +} +node { + name: "training/SGD/gradients/loss_1/res4a_branch1/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 1024 + } + } + float_val: 0.0002 + } + } + } +} +node { + name: "training/SGD/res4a_branch2a/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res4a_branch2a/kernel/momentum" + } + } +} +node { + name: "training/SGD/gradients/loss_1/res4a_branch2a/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 256 + } + } + float_val: 0.0002 + } + } + } +} +node { + name: "training/SGD/res4a_branch2b/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 256 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res4a_branch2b/kernel/momentum" + } + } +} +node { + name: "training/SGD/res4a_branch2c/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res4a_branch2c/kernel/momentum" + } + } +} +node { + name: "training/SGD/res4b_branch2a/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res4b_branch2a/kernel/momentum" + } + } +} +node { + name: "training/SGD/res4b_branch2b/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 256 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res4b_branch2b/kernel/momentum" + } + } +} +node { + name: "training/SGD/res4b_branch2c/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res4b_branch2c/kernel/momentum" + } + } +} +node { + name: "training/SGD/res4c_branch2a/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res4c_branch2a/kernel/momentum" + } + } +} +node { + name: "training/SGD/res4c_branch2b/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 256 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res4c_branch2b/kernel/momentum" + } + } +} +node { + name: "training/SGD/res4c_branch2c/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res4c_branch2c/kernel/momentum" + } + } +} +node { + name: "training/SGD/res4d_branch2a/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res4d_branch2a/kernel/momentum" + } + } +} +node { + name: "training/SGD/res4d_branch2b/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 256 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res4d_branch2b/kernel/momentum" + } + } +} +node { + name: "training/SGD/res4d_branch2c/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res4d_branch2c/kernel/momentum" + } + } +} +node { + name: "training/SGD/res4e_branch2a/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res4e_branch2a/kernel/momentum" + } + } +} +node { + name: "training/SGD/res4e_branch2b/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 256 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res4e_branch2b/kernel/momentum" + } + } +} +node { + name: "training/SGD/res4e_branch2c/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res4e_branch2c/kernel/momentum" + } + } +} +node { + name: "training/SGD/res4f_branch2a/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res4f_branch2a/kernel/momentum" + } + } +} +node { + name: "training/SGD/gradients/loss_1/res4f_branch2a/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 256 + } + } + float_val: 0.0002 + } + } + } +} +node { + name: "training/SGD/res4f_branch2b/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 256 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res4f_branch2b/kernel/momentum" + } + } +} +node { + name: "training/SGD/gradients/loss_1/res4f_branch2b/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 256 + } + dim { + size: 256 + } + } + float_val: 0.0002 + } + } + } +} +node { + name: "training/SGD/res4f_branch2c/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res4f_branch2c/kernel/momentum" + } + } +} +node { + name: "training/SGD/gradients/loss_1/res4f_branch2c/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 1024 + } + } + float_val: 0.0002 + } + } + } +} +node { + name: "training/SGD/res5a_branch1/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res5a_branch1/kernel/momentum" + } + } +} +node { + name: "training/SGD/gradients/loss_1/res5a_branch1/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 2048 + } + } + float_val: 0.0002 + } + } + } +} +node { + name: "training/SGD/res5a_branch2a/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res5a_branch2a/kernel/momentum" + } + } +} +node { + name: "training/SGD/gradients/loss_1/res5a_branch2a/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 512 + } + } + float_val: 0.0002 + } + } + } +} +node { + name: "training/SGD/res5a_branch2b/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 512 + } + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res5a_branch2b/kernel/momentum" + } + } +} +node { + name: "training/SGD/res5a_branch2c/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res5a_branch2c/kernel/momentum" + } + } +} +node { + name: "training/SGD/res5b_branch2a/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 2048 + } + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res5b_branch2a/kernel/momentum" + } + } +} +node { + name: "training/SGD/res5b_branch2b/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 512 + } + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res5b_branch2b/kernel/momentum" + } + } +} +node { + name: "training/SGD/res5b_branch2c/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res5b_branch2c/kernel/momentum" + } + } +} +node { + name: "training/SGD/res5c_branch2a/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 2048 + } + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res5c_branch2a/kernel/momentum" + } + } +} +node { + name: "training/SGD/gradients/loss_1/res5c_branch2a/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 2048 + } + dim { + size: 512 + } + } + float_val: 0.0002 + } + } + } +} +node { + name: "training/SGD/res5c_branch2b/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 512 + } + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res5c_branch2b/kernel/momentum" + } + } +} +node { + name: "training/SGD/gradients/loss_1/res5c_branch2b/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 512 + } + dim { + size: 512 + } + } + float_val: 0.0002 + } + } + } +} +node { + name: "training/SGD/res5c_branch2c/kernel/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/res5c_branch2c/kernel/momentum" + } + } +} +node { + name: "training/SGD/learning_rate" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/learning_rate" + } + } +} +node { + name: "training/SGD/gradients/loss_1/res5c_branch2c/kernel/Regularizer/Square_grad/Mul" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 2048 + } + } + float_val: 0.0002 + } + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/avg_pool_1/Mean_grad/truediv_recip" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + tensor_content: "\005/\247<" + } + } + } +} +node { + name: "training/SGD/gradients/loss_1/activation_49_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits_grad/ExpandDims" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 117 + } + dim { + size: 1 + } + } + float_val: 0.008547009 + } + } + } +} +node { + name: "training/SGD/gradients/avg_pool_1/Mean_grad/DynamicStitch" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "u\000\000\000\000\010\000\000\001\000\000\000\001\000\000\000" + } + } + } +} +node { + name: "training/SGD/gradients/avg_pool_1/Mean_grad/floordiv" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\007\000\000\000\007\000\000\000" + } + } + } +} +node { + name: "training/SGD/momentum" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + } + } + } + attr { + key: "shared_name" + value { + s: "training/SGD/momentum" + } + } +} +node { + name: "loss_1/fc1000/bias/Regularizer/mul/x" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.0001 + } + } + } +} +node { + name: "loss_1/fc1000/kernel/Regularizer/Const" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 2 + } + } + tensor_content: "\000\000\000\000\001\000\000\000" + } + } + } +} +node { + name: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\000\000\000\000\001\000\000\000\002\000\000\000\003\000\000\000" + } + } + } +} +node { + name: "loss_1/activation_49_loss/Reshape/shape" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 1 + } + } + int_val: -1 + } + } + } +} +node { + name: "count_3" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + } + } + } + attr { + key: "shared_name" + value { + s: "count_3" + } + } +} +node { + name: "metrics_2/sparse_categorical_accuracy/Cast_2" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + tensor_content: "\000\000\352B" + } + } + } +} +node { + name: "total_1" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + } + } + } + attr { + key: "shared_name" + value { + s: "total_1" + } + } +} +node { + name: "res5a_branch1/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res5a_branch1/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res5a_branch1/kernel" + } + } +} +node { + name: "bn5a_branch1/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn5a_branch1/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5a_branch1/gamma" + } + } +} +node { + name: "bn5a_branch1/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn5a_branch1/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5a_branch1/beta" + } + } +} +node { + name: "res4a_branch1/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res4a_branch1/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res4a_branch1/kernel" + } + } +} +node { + name: "bn4a_branch1/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4a_branch1/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4a_branch1/gamma" + } + } +} +node { + name: "bn4a_branch1/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4a_branch1/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4a_branch1/beta" + } + } +} +node { + name: "res3a_branch1/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res3a_branch1/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res3a_branch1/kernel" + } + } +} +node { + name: "bn3a_branch1/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn3a_branch1/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3a_branch1/gamma" + } + } +} +node { + name: "bn3a_branch1/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn3a_branch1/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3a_branch1/beta" + } + } +} +node { + name: "res2a_branch1/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res2a_branch1/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 64 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res2a_branch1/kernel" + } + } +} +node { + name: "bn2a_branch1/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn2a_branch1/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2a_branch1/gamma" + } + } +} +node { + name: "bn2a_branch1/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn2a_branch1/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2a_branch1/beta" + } + } +} +node { + name: "transpose_1/transpose/perm" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\000\000\000\000\003\000\000\000\001\000\000\000\002\000\000\000" + } + } + } +} +node { + name: "conv1_pad_1/Pad/paddings" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + dim { + size: 2 + } + } + tensor_content: "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\003\000\000\000\003\000\000\000\003\000\000\000\003\000\000\000" + } + } + } +} +node { + name: "conv1/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/conv1/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 7 + } + dim { + size: 7 + } + dim { + size: 3 + } + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "conv1/kernel" + } + } +} +node { + name: "bn_conv1/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn_conv1/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn_conv1/gamma" + } + } +} +node { + name: "bn_conv1/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn_conv1/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn_conv1/beta" + } + } +} +node { + name: "res2a_branch2a/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res2a_branch2a/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 64 + } + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res2a_branch2a/kernel" + } + } +} +node { + name: "bn2a_branch2a/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn2a_branch2a/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2a_branch2a/gamma" + } + } +} +node { + name: "bn2a_branch2a/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn2a_branch2a/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2a_branch2a/beta" + } + } +} +node { + name: "res2a_branch2b/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res2a_branch2b/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 64 + } + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res2a_branch2b/kernel" + } + } +} +node { + name: "bn2a_branch2b/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn2a_branch2b/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2a_branch2b/gamma" + } + } +} +node { + name: "bn2a_branch2b/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn2a_branch2b/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2a_branch2b/beta" + } + } +} +node { + name: "res2a_branch2c/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res2a_branch2c/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 64 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res2a_branch2c/kernel" + } + } +} +node { + name: "bn2a_branch2c/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn2a_branch2c/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2a_branch2c/gamma" + } + } +} +node { + name: "bn2a_branch2c/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn2a_branch2c/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2a_branch2c/beta" + } + } +} +node { + name: "res2b_branch2a/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res2b_branch2a/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res2b_branch2a/kernel" + } + } +} +node { + name: "bn2b_branch2a/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn2b_branch2a/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2b_branch2a/gamma" + } + } +} +node { + name: "bn2b_branch2a/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn2b_branch2a/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2b_branch2a/beta" + } + } +} +node { + name: "res2b_branch2b/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res2b_branch2b/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 64 + } + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res2b_branch2b/kernel" + } + } +} +node { + name: "bn2b_branch2b/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn2b_branch2b/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2b_branch2b/gamma" + } + } +} +node { + name: "bn2b_branch2b/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn2b_branch2b/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2b_branch2b/beta" + } + } +} +node { + name: "res2b_branch2c/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res2b_branch2c/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 64 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res2b_branch2c/kernel" + } + } +} +node { + name: "bn2b_branch2c/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn2b_branch2c/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2b_branch2c/gamma" + } + } +} +node { + name: "bn2b_branch2c/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn2b_branch2c/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2b_branch2c/beta" + } + } +} +node { + name: "res2c_branch2a/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res2c_branch2a/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res2c_branch2a/kernel" + } + } +} +node { + name: "bn2c_branch2a/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn2c_branch2a/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2c_branch2a/gamma" + } + } +} +node { + name: "bn2c_branch2a/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn2c_branch2a/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2c_branch2a/beta" + } + } +} +node { + name: "res2c_branch2b/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res2c_branch2b/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 64 + } + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res2c_branch2b/kernel" + } + } +} +node { + name: "bn2c_branch2b/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn2c_branch2b/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2c_branch2b/gamma" + } + } +} +node { + name: "bn2c_branch2b/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn2c_branch2b/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 64 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2c_branch2b/beta" + } + } +} +node { + name: "res2c_branch2c/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res2c_branch2c/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 64 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res2c_branch2c/kernel" + } + } +} +node { + name: "bn2c_branch2c/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn2c_branch2c/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2c_branch2c/gamma" + } + } +} +node { + name: "bn2c_branch2c/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn2c_branch2c/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn2c_branch2c/beta" + } + } +} +node { + name: "res3a_branch2a/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res3a_branch2a/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res3a_branch2a/kernel" + } + } +} +node { + name: "bn3a_branch2a/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn3a_branch2a/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3a_branch2a/gamma" + } + } +} +node { + name: "bn3a_branch2a/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn3a_branch2a/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3a_branch2a/beta" + } + } +} +node { + name: "res3a_branch2b/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res3a_branch2b/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 128 + } + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res3a_branch2b/kernel" + } + } +} +node { + name: "bn3a_branch2b/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn3a_branch2b/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3a_branch2b/gamma" + } + } +} +node { + name: "bn3a_branch2b/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn3a_branch2b/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3a_branch2b/beta" + } + } +} +node { + name: "res3a_branch2c/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res3a_branch2c/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 128 + } + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res3a_branch2c/kernel" + } + } +} +node { + name: "bn3a_branch2c/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn3a_branch2c/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3a_branch2c/gamma" + } + } +} +node { + name: "bn3a_branch2c/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn3a_branch2c/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3a_branch2c/beta" + } + } +} +node { + name: "res3b_branch2a/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res3b_branch2a/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res3b_branch2a/kernel" + } + } +} +node { + name: "bn3b_branch2a/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn3b_branch2a/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3b_branch2a/gamma" + } + } +} +node { + name: "bn3b_branch2a/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn3b_branch2a/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3b_branch2a/beta" + } + } +} +node { + name: "res3b_branch2b/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res3b_branch2b/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 128 + } + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res3b_branch2b/kernel" + } + } +} +node { + name: "bn3b_branch2b/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn3b_branch2b/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3b_branch2b/gamma" + } + } +} +node { + name: "bn3b_branch2b/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn3b_branch2b/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3b_branch2b/beta" + } + } +} +node { + name: "res3b_branch2c/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res3b_branch2c/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 128 + } + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res3b_branch2c/kernel" + } + } +} +node { + name: "bn3b_branch2c/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn3b_branch2c/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3b_branch2c/gamma" + } + } +} +node { + name: "bn3b_branch2c/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn3b_branch2c/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3b_branch2c/beta" + } + } +} +node { + name: "res3c_branch2a/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res3c_branch2a/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res3c_branch2a/kernel" + } + } +} +node { + name: "bn3c_branch2a/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn3c_branch2a/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3c_branch2a/gamma" + } + } +} +node { + name: "bn3c_branch2a/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn3c_branch2a/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3c_branch2a/beta" + } + } +} +node { + name: "res3c_branch2b/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res3c_branch2b/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 128 + } + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res3c_branch2b/kernel" + } + } +} +node { + name: "bn3c_branch2b/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn3c_branch2b/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3c_branch2b/gamma" + } + } +} +node { + name: "bn3c_branch2b/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn3c_branch2b/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3c_branch2b/beta" + } + } +} +node { + name: "res3c_branch2c/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res3c_branch2c/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 128 + } + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res3c_branch2c/kernel" + } + } +} +node { + name: "bn3c_branch2c/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn3c_branch2c/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3c_branch2c/gamma" + } + } +} +node { + name: "bn3c_branch2c/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn3c_branch2c/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3c_branch2c/beta" + } + } +} +node { + name: "res3d_branch2a/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res3d_branch2a/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res3d_branch2a/kernel" + } + } +} +node { + name: "bn3d_branch2a/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn3d_branch2a/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3d_branch2a/gamma" + } + } +} +node { + name: "bn3d_branch2a/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn3d_branch2a/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3d_branch2a/beta" + } + } +} +node { + name: "res3d_branch2b/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res3d_branch2b/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 128 + } + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res3d_branch2b/kernel" + } + } +} +node { + name: "bn3d_branch2b/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn3d_branch2b/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3d_branch2b/gamma" + } + } +} +node { + name: "bn3d_branch2b/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn3d_branch2b/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 128 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3d_branch2b/beta" + } + } +} +node { + name: "res3d_branch2c/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res3d_branch2c/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 128 + } + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res3d_branch2c/kernel" + } + } +} +node { + name: "bn3d_branch2c/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn3d_branch2c/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3d_branch2c/gamma" + } + } +} +node { + name: "bn3d_branch2c/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn3d_branch2c/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn3d_branch2c/beta" + } + } +} +node { + name: "res4a_branch2a/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res4a_branch2a/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res4a_branch2a/kernel" + } + } +} +node { + name: "bn4a_branch2a/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4a_branch2a/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4a_branch2a/gamma" + } + } +} +node { + name: "bn4a_branch2a/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4a_branch2a/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4a_branch2a/beta" + } + } +} +node { + name: "res4a_branch2b/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res4a_branch2b/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 256 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res4a_branch2b/kernel" + } + } +} +node { + name: "bn4a_branch2b/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4a_branch2b/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4a_branch2b/gamma" + } + } +} +node { + name: "bn4a_branch2b/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4a_branch2b/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4a_branch2b/beta" + } + } +} +node { + name: "res4a_branch2c/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res4a_branch2c/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res4a_branch2c/kernel" + } + } +} +node { + name: "bn4a_branch2c/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4a_branch2c/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4a_branch2c/gamma" + } + } +} +node { + name: "bn4a_branch2c/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4a_branch2c/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4a_branch2c/beta" + } + } +} +node { + name: "res4b_branch2a/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res4b_branch2a/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res4b_branch2a/kernel" + } + } +} +node { + name: "bn4b_branch2a/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4b_branch2a/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4b_branch2a/gamma" + } + } +} +node { + name: "bn4b_branch2a/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4b_branch2a/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4b_branch2a/beta" + } + } +} +node { + name: "res4b_branch2b/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res4b_branch2b/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 256 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res4b_branch2b/kernel" + } + } +} +node { + name: "bn4b_branch2b/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4b_branch2b/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4b_branch2b/gamma" + } + } +} +node { + name: "bn4b_branch2b/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4b_branch2b/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4b_branch2b/beta" + } + } +} +node { + name: "res4b_branch2c/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res4b_branch2c/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res4b_branch2c/kernel" + } + } +} +node { + name: "bn4b_branch2c/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4b_branch2c/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4b_branch2c/gamma" + } + } +} +node { + name: "bn4b_branch2c/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4b_branch2c/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4b_branch2c/beta" + } + } +} +node { + name: "res4c_branch2a/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res4c_branch2a/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res4c_branch2a/kernel" + } + } +} +node { + name: "bn4c_branch2a/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4c_branch2a/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4c_branch2a/gamma" + } + } +} +node { + name: "bn4c_branch2a/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4c_branch2a/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4c_branch2a/beta" + } + } +} +node { + name: "res4c_branch2b/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res4c_branch2b/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 256 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res4c_branch2b/kernel" + } + } +} +node { + name: "bn4c_branch2b/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4c_branch2b/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4c_branch2b/gamma" + } + } +} +node { + name: "bn4c_branch2b/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4c_branch2b/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4c_branch2b/beta" + } + } +} +node { + name: "res4c_branch2c/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res4c_branch2c/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res4c_branch2c/kernel" + } + } +} +node { + name: "bn4c_branch2c/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4c_branch2c/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4c_branch2c/gamma" + } + } +} +node { + name: "bn4c_branch2c/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4c_branch2c/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4c_branch2c/beta" + } + } +} +node { + name: "res4d_branch2a/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res4d_branch2a/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res4d_branch2a/kernel" + } + } +} +node { + name: "bn4d_branch2a/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4d_branch2a/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4d_branch2a/gamma" + } + } +} +node { + name: "bn4d_branch2a/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4d_branch2a/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4d_branch2a/beta" + } + } +} +node { + name: "res4d_branch2b/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res4d_branch2b/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 256 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res4d_branch2b/kernel" + } + } +} +node { + name: "bn4d_branch2b/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4d_branch2b/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4d_branch2b/gamma" + } + } +} +node { + name: "bn4d_branch2b/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4d_branch2b/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4d_branch2b/beta" + } + } +} +node { + name: "res4d_branch2c/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res4d_branch2c/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res4d_branch2c/kernel" + } + } +} +node { + name: "bn4d_branch2c/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4d_branch2c/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4d_branch2c/gamma" + } + } +} +node { + name: "bn4d_branch2c/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4d_branch2c/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4d_branch2c/beta" + } + } +} +node { + name: "res4e_branch2a/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res4e_branch2a/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res4e_branch2a/kernel" + } + } +} +node { + name: "bn4e_branch2a/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4e_branch2a/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4e_branch2a/gamma" + } + } +} +node { + name: "bn4e_branch2a/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4e_branch2a/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4e_branch2a/beta" + } + } +} +node { + name: "res4e_branch2b/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res4e_branch2b/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 256 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res4e_branch2b/kernel" + } + } +} +node { + name: "bn4e_branch2b/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4e_branch2b/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4e_branch2b/gamma" + } + } +} +node { + name: "bn4e_branch2b/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4e_branch2b/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4e_branch2b/beta" + } + } +} +node { + name: "res4e_branch2c/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res4e_branch2c/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res4e_branch2c/kernel" + } + } +} +node { + name: "bn4e_branch2c/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4e_branch2c/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4e_branch2c/gamma" + } + } +} +node { + name: "bn4e_branch2c/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4e_branch2c/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4e_branch2c/beta" + } + } +} +node { + name: "res4f_branch2a/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res4f_branch2a/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res4f_branch2a/kernel" + } + } +} +node { + name: "bn4f_branch2a/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4f_branch2a/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4f_branch2a/gamma" + } + } +} +node { + name: "bn4f_branch2a/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4f_branch2a/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4f_branch2a/beta" + } + } +} +node { + name: "res4f_branch2b/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res4f_branch2b/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 256 + } + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res4f_branch2b/kernel" + } + } +} +node { + name: "bn4f_branch2b/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4f_branch2b/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4f_branch2b/gamma" + } + } +} +node { + name: "bn4f_branch2b/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4f_branch2b/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 256 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4f_branch2b/beta" + } + } +} +node { + name: "res4f_branch2c/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res4f_branch2c/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res4f_branch2c/kernel" + } + } +} +node { + name: "bn4f_branch2c/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4f_branch2c/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4f_branch2c/gamma" + } + } +} +node { + name: "bn4f_branch2c/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn4f_branch2c/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1024 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn4f_branch2c/beta" + } + } +} +node { + name: "res5a_branch2a/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res5a_branch2a/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1024 + } + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res5a_branch2a/kernel" + } + } +} +node { + name: "bn5a_branch2a/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn5a_branch2a/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5a_branch2a/gamma" + } + } +} +node { + name: "bn5a_branch2a/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn5a_branch2a/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5a_branch2a/beta" + } + } +} +node { + name: "res5a_branch2b/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res5a_branch2b/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 512 + } + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res5a_branch2b/kernel" + } + } +} +node { + name: "bn5a_branch2b/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn5a_branch2b/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5a_branch2b/gamma" + } + } +} +node { + name: "bn5a_branch2b/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn5a_branch2b/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5a_branch2b/beta" + } + } +} +node { + name: "res5a_branch2c/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res5a_branch2c/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res5a_branch2c/kernel" + } + } +} +node { + name: "bn5a_branch2c/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn5a_branch2c/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5a_branch2c/gamma" + } + } +} +node { + name: "bn5a_branch2c/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn5a_branch2c/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5a_branch2c/beta" + } + } +} +node { + name: "res5b_branch2a/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res5b_branch2a/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 2048 + } + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res5b_branch2a/kernel" + } + } +} +node { + name: "bn5b_branch2a/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn5b_branch2a/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5b_branch2a/gamma" + } + } +} +node { + name: "bn5b_branch2a/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn5b_branch2a/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5b_branch2a/beta" + } + } +} +node { + name: "res5b_branch2b/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res5b_branch2b/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 512 + } + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res5b_branch2b/kernel" + } + } +} +node { + name: "bn5b_branch2b/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn5b_branch2b/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5b_branch2b/gamma" + } + } +} +node { + name: "bn5b_branch2b/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn5b_branch2b/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5b_branch2b/beta" + } + } +} +node { + name: "res5b_branch2c/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res5b_branch2c/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res5b_branch2c/kernel" + } + } +} +node { + name: "bn5b_branch2c/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn5b_branch2c/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5b_branch2c/gamma" + } + } +} +node { + name: "bn5b_branch2c/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn5b_branch2c/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5b_branch2c/beta" + } + } +} +node { + name: "res5c_branch2a/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res5c_branch2a/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 2048 + } + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res5c_branch2a/kernel" + } + } +} +node { + name: "bn5c_branch2a/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn5c_branch2a/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5c_branch2a/gamma" + } + } +} +node { + name: "bn5c_branch2a/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn5c_branch2a/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5c_branch2a/beta" + } + } +} +node { + name: "res5c_branch2b/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res5c_branch2b/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 512 + } + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res5c_branch2b/kernel" + } + } +} +node { + name: "bn5c_branch2b/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn5c_branch2b/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5c_branch2b/gamma" + } + } +} +node { + name: "bn5c_branch2b/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn5c_branch2b/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 512 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5c_branch2b/beta" + } + } +} +node { + name: "res5c_branch2c/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/res5c_branch2c/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 512 + } + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "res5c_branch2c/kernel" + } + } +} +node { + name: "bn5c_branch2c/gamma" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn5c_branch2c/gamma/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5c_branch2c/gamma" + } + } +} +node { + name: "bn5c_branch2c/beta" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/bn5c_branch2c/beta/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2048 + } + } + } + } + attr { + key: "shared_name" + value { + s: "bn5c_branch2c/beta" + } + } +} +node { + name: "bn5a_branch1_1/Const_1" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + } + } + } + } + } +} +node { + name: "avg_pool_1/Mean/reduction_indices" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 2 + } + } + tensor_content: "\002\000\000\000\003\000\000\000" + } + } + } +} +node { + name: "fc1000/kernel" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/fc1000/kernel/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2048 + } + dim { + size: 1001 + } + } + } + } + attr { + key: "shared_name" + value { + s: "fc1000/kernel" + } + } +} +node { + name: "fc1000/bias" + op: "VarHandleOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/fc1000/bias/momentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1001 + } + } + } + } + attr { + key: "shared_name" + value { + s: "fc1000/bias" + } + } +} +node { + name: "metrics_2/sparse_categorical_accuracy/ArgMax/dimension" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + } + int_val: -1 + } + } + } +} +node { + name: "metrics_2/sparse_categorical_accuracy/Const" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 1 + } + } + int_val: 0 + } + } + } +} +node { + name: "G4SM2HGMB5/Read/ReadVariableOp" + op: "ReadVariableOp" + input: "G4SM2HGMB5" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "HK0SX0WHBZ/Read/ReadVariableOp" + op: "ReadVariableOp" + input: "HK0SX0WHBZ" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch1_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2a_branch1/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch1_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2a_branch1/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2a_branch2a/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2a_branch2a/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2a_branch2b/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2a_branch2b/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2a_branch2c/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2a_branch2c/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2b_branch2a/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2b_branch2a/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2b_branch2b/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2b_branch2b/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2b_branch2c/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2b_branch2c/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2c_branch2a/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2c_branch2a/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2c_branch2b/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2c_branch2b/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2c_branch2c/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2c_branch2c/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch1_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3a_branch1/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch1_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3a_branch1/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3a_branch2a/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3a_branch2a/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3a_branch2b/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3a_branch2b/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3a_branch2c/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3a_branch2c/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3b_branch2a/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3b_branch2a/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3b_branch2b/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3b_branch2b/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3b_branch2c/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3b_branch2c/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3c_branch2a/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3c_branch2a/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3c_branch2b/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3c_branch2b/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3c_branch2c/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3c_branch2c/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3d_branch2a/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3d_branch2a/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3d_branch2b/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3d_branch2b/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3d_branch2c/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3d_branch2c/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch1_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4a_branch1/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch1_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4a_branch1/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4a_branch2a/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4a_branch2a/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4a_branch2b/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4a_branch2b/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4a_branch2c/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4a_branch2c/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4b_branch2a/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4b_branch2a/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4b_branch2b/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4b_branch2b/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4b_branch2c/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4b_branch2c/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4c_branch2a/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4c_branch2a/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4c_branch2b/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4c_branch2b/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4c_branch2c/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4c_branch2c/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4d_branch2a/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4d_branch2a/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4d_branch2b/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4d_branch2b/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4d_branch2c/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4d_branch2c/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4e_branch2a/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4e_branch2a/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4e_branch2b/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4e_branch2b/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4e_branch2c/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4e_branch2c/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4f_branch2a/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4f_branch2a/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4f_branch2b/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4f_branch2b/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4f_branch2c/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4f_branch2c/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch1_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5a_branch1/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch1_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5a_branch1/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5a_branch2a/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5a_branch2a/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5a_branch2b/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5a_branch2b/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5a_branch2c/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5a_branch2c/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5b_branch2a/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5b_branch2a/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5b_branch2b/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5b_branch2b/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5b_branch2c/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5b_branch2c/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2a_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5c_branch2a/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5c_branch2a/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2b_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5c_branch2b/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5c_branch2b/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2c_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5c_branch2c/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5c_branch2c/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn_conv1_1/AssignMovingAvg/ReadVariableOp" + op: "ReadVariableOp" + input: "bn_conv1/moving_mean" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn_conv1_1/AssignMovingAvg_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn_conv1/moving_variance" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2a_branch1/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2a_branch1/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2a_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2a_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2a_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2a_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2a_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2a_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2b_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2b_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2b_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2b_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2b_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2b_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2c_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2c_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2c_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2c_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2c_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2c_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3a_branch1/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3a_branch1/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3a_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3a_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3a_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3a_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3a_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3a_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3b_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3b_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3b_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3b_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3b_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3b_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3c_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3c_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3c_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3c_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3c_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3c_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3d_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3d_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3d_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3d_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3d_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3d_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4a_branch1/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4a_branch1/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4a_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4a_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4a_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4a_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4a_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4a_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4b_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4b_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4b_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4b_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4b_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4b_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4c_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4c_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4c_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4c_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4c_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4c_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4d_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4d_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4d_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4d_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4d_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4d_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4e_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4e_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4e_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4e_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4e_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4e_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4f_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4f_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4f_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4f_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4f_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4f_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5a_branch1/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5a_branch1/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5a_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5a_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5a_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5a_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5a_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5a_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5b_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5b_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5b_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5b_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5b_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5b_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5c_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5c_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5c_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5c_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5c_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5c_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn_conv1/beta/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn_conv1/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_conv1/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_fc1000/bias/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_fc1000/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res2a_branch1/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res2a_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res2a_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res2a_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res2b_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res2b_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res2b_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res2c_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res2c_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res2c_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res3a_branch1/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res3a_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res3a_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res3a_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res3b_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res3b_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res3b_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res3c_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res3c_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res3c_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res3d_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res3d_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res3d_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4a_branch1/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4a_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4a_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4a_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4b_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4b_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4b_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4c_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4c_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4c_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4d_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4d_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4d_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4e_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4e_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4e_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4f_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4f_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4f_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res5a_branch1/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res5a_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res5a_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res5a_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res5b_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res5b_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res5b_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res5c_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res5c_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res5c_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + op: "ReadVariableOp" + input: "training/SGD/learning_rate" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2a_branch1/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2a_branch1/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2a_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2a_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2a_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2a_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2a_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2a_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2b_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2b_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2b_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2b_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2b_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2b_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2c_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2c_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2c_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2c_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2c_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn2c_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3a_branch1/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3a_branch1/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3a_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3a_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3a_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3a_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3a_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3a_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3b_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3b_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3b_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3b_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3b_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3b_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3c_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3c_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3c_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3c_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3c_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3c_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3d_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3d_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3d_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3d_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3d_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn3d_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4a_branch1/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4a_branch1/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4a_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4a_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4a_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4a_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4a_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4a_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4b_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4b_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4b_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4b_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4b_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4b_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4c_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4c_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4c_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4c_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4c_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4c_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4d_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4d_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4d_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4d_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4d_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4d_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4e_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4e_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4e_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4e_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4e_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4e_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4f_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4f_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4f_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4f_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4f_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn4f_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5a_branch1/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5a_branch1/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5a_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5a_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5a_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5a_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5a_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5a_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5b_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5b_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5b_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5b_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5b_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5b_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5c_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5c_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5c_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5c_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5c_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5c_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn_conv1/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn_conv1/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_conv1/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_fc1000/bias/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_fc1000/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res2a_branch1/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res2a_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res2a_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res2a_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res2b_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res2b_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res2b_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res2c_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res2c_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res2c_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res3a_branch1/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res3a_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res3a_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res3a_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res3b_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res3b_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res3b_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res3c_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res3c_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res3c_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res3d_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res3d_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res3d_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4a_branch1/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4a_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4a_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4a_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4b_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4b_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4b_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4c_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4c_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4c_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4d_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4d_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4d_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4e_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4e_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4e_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4f_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4f_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4f_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res5a_branch1/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res5a_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res5a_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res5a_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res5b_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res5b_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res5b_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res5c_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res5c_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res5c_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + op: "ReadVariableOp" + input: "training/SGD/momentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5a_branch1/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res5a_branch1/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5a_branch1_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res5a_branch1/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch1_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5a_branch1/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch1_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn5a_branch1/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4a_branch1/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4a_branch1/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4a_branch1_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4a_branch1/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch1_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4a_branch1/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch1_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4a_branch1/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3a_branch1/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res3a_branch1/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3a_branch1_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res3a_branch1/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch1_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3a_branch1/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch1_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn3a_branch1/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2a_branch1/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res2a_branch1/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2a_branch1_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res2a_branch1/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch1_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2a_branch1/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch1_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn2a_branch1/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "transpose_1/transpose" + op: "Transpose" + input: "_identity_input_1_1_0" + input: "transpose_1/transpose/perm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tperm" + value { + type: DT_INT32 + } + } +} +node { + name: "loss_1/conv1/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "conv1/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "conv1_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "conv1/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn_conv1_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn_conv1/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn_conv1_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn_conv1/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2a_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res2a_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2a_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res2a_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2a_branch2a/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn2a_branch2a/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2a_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res2a_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2a_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res2a_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2a_branch2b/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn2a_branch2b/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2a_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res2a_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2a_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res2a_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2a_branch2c/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn2a_branch2c/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2b_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res2b_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2b_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res2b_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2b_branch2a/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn2b_branch2a/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2b_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res2b_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2b_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res2b_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2b_branch2b/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn2b_branch2b/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2b_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res2b_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2b_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res2b_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2b_branch2c/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn2b_branch2c/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2c_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res2c_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2c_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res2c_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2c_branch2a/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn2c_branch2a/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2c_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res2c_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2c_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res2c_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2c_branch2b/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn2c_branch2b/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res2c_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res2c_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res2c_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res2c_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn2c_branch2c/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn2c_branch2c/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3a_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res3a_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3a_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res3a_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3a_branch2a/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn3a_branch2a/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3a_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res3a_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3a_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res3a_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3a_branch2b/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn3a_branch2b/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3a_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res3a_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3a_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res3a_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3a_branch2c/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn3a_branch2c/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3b_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res3b_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3b_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res3b_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3b_branch2a/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn3b_branch2a/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3b_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res3b_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3b_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res3b_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3b_branch2b/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn3b_branch2b/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3b_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res3b_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3b_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res3b_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3b_branch2c/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn3b_branch2c/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3c_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res3c_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3c_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res3c_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3c_branch2a/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn3c_branch2a/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3c_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res3c_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3c_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res3c_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3c_branch2b/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn3c_branch2b/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3c_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res3c_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3c_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res3c_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3c_branch2c/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn3c_branch2c/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3d_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res3d_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3d_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res3d_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3d_branch2a/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn3d_branch2a/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3d_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res3d_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3d_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res3d_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3d_branch2b/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn3d_branch2b/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res3d_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res3d_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res3d_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res3d_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn3d_branch2c/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn3d_branch2c/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4a_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4a_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4a_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4a_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4a_branch2a/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4a_branch2a/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4a_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4a_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4a_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4a_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4a_branch2b/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4a_branch2b/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4a_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4a_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4a_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4a_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4a_branch2c/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4a_branch2c/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4b_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4b_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4b_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4b_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4b_branch2a/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4b_branch2a/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4b_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4b_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4b_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4b_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4b_branch2b/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4b_branch2b/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4b_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4b_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4b_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4b_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4b_branch2c/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4b_branch2c/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4c_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4c_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4c_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4c_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4c_branch2a/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4c_branch2a/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4c_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4c_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4c_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4c_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4c_branch2b/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4c_branch2b/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4c_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4c_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4c_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4c_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4c_branch2c/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4c_branch2c/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4d_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4d_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4d_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4d_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4d_branch2a/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4d_branch2a/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4d_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4d_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4d_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4d_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4d_branch2b/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4d_branch2b/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4d_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4d_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4d_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4d_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4d_branch2c/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4d_branch2c/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4e_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4e_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4e_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4e_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4e_branch2a/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4e_branch2a/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4e_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4e_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4e_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4e_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4e_branch2b/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4e_branch2b/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4e_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4e_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4e_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4e_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4e_branch2c/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4e_branch2c/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4f_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4f_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4f_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4f_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4f_branch2a/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4f_branch2a/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4f_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4f_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4f_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4f_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4f_branch2b/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4f_branch2b/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res4f_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res4f_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res4f_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res4f_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn4f_branch2c/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn4f_branch2c/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5a_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res5a_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5a_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res5a_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5a_branch2a/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn5a_branch2a/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5a_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res5a_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5a_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res5a_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5a_branch2b/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn5a_branch2b/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5a_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res5a_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5a_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res5a_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5a_branch2c/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn5a_branch2c/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5b_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res5b_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5b_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res5b_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5b_branch2a/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn5b_branch2a/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5b_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res5b_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5b_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res5b_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5b_branch2b/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn5b_branch2b/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5b_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res5b_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5b_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res5b_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5b_branch2c/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn5b_branch2c/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5c_branch2a/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res5c_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5c_branch2a_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res5c_branch2a/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2a_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5c_branch2a/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2a_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn5c_branch2a/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5c_branch2b/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res5c_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5c_branch2b_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res5c_branch2b/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2b_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5c_branch2b/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2b_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn5c_branch2b/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/res5c_branch2c/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "res5c_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "res5c_branch2c_1/Conv2D/ReadVariableOp" + op: "ReadVariableOp" + input: "res5c_branch2c/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2c_1/ReadVariableOp" + op: "ReadVariableOp" + input: "bn5c_branch2c/gamma" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2c_1/ReadVariableOp_1" + op: "ReadVariableOp" + input: "bn5c_branch2c/beta" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/fc1000/kernel/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "fc1000/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "fc1000_1/MatMul/ReadVariableOp" + op: "ReadVariableOp" + input: "fc1000/kernel" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/fc1000/bias/Regularizer/Square/ReadVariableOp" + op: "ReadVariableOp" + input: "fc1000/bias" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "fc1000_1/BiasAdd/ReadVariableOp" + op: "ReadVariableOp" + input: "fc1000/bias" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/activation_49_loss/Reshape" + op: "Reshape" + input: "_identity_activation_49_target_1_0" + input: "loss_1/activation_49_loss/Reshape/shape" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tshape" + value { + type: DT_INT32 + } + } +} +node { + name: "metrics_2/sparse_categorical_accuracy/Squeeze" + op: "Squeeze" + input: "_identity_activation_49_target_1_0" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "squeeze_dims" + value { + list { + i: -1 + } + } + } +} +node { + name: "training/SGD/gradients/loss_1/res5a_branch1/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res5a_branch1/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res5a_branch1/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_11" + } + } + } +} +node { + name: "loss_1/res5a_branch1/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res5a_branch1/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res4a_branch1/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4a_branch1/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res4a_branch1/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_36" + } + } + } +} +node { + name: "loss_1/res4a_branch1/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4a_branch1/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res3a_branch1/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res3a_branch1/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res3a_branch1/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_53" + } + } + } +} +node { + name: "loss_1/res3a_branch1/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res3a_branch1/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res2a_branch1/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res2a_branch1/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res2c_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_66" + } + } + } +} +node { + name: "loss_1/res2a_branch1/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res2a_branch1/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "conv1_pad_1/Pad" + op: "Pad" + input: "transpose_1/transpose" + input: "conv1_pad_1/Pad/paddings" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tpaddings" + value { + type: DT_INT32 + } + } +} +node { + name: "training/SGD/gradients/loss_1/conv1/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/conv1/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/conv1/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_70" + } + } + } +} +node { + name: "loss_1/conv1/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/conv1/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res2a_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res2a_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res2a_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_69" + } + } + } +} +node { + name: "loss_1/res2a_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res2a_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res2a_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res2a_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res2c_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_67" + } + } + } +} +node { + name: "loss_1/res2a_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res2a_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res2a_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res2a_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res2c_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_65" + } + } + } +} +node { + name: "loss_1/res2a_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res2a_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res2b_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res2b_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res2c_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_64" + } + } + } +} +node { + name: "loss_1/res2b_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res2b_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res2b_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res2b_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res2c_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_62" + } + } + } +} +node { + name: "loss_1/res2b_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res2b_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res2b_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res2b_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res2c_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_61" + } + } + } +} +node { + name: "loss_1/res2b_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res2b_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res2c_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res2c_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res2c_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_60" + } + } + } +} +node { + name: "loss_1/res2c_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res2c_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res2c_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res2c_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res2c_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_58" + } + } + } +} +node { + name: "loss_1/res2c_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res2c_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res2c_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res2c_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res2c_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_57" + } + } + } +} +node { + name: "loss_1/res2c_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res2c_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res3a_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res3a_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res3a_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_56" + } + } + } +} +node { + name: "loss_1/res3a_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res3a_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res3a_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res3a_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res3d_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_54" + } + } + } +} +node { + name: "loss_1/res3a_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res3a_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res3a_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res3a_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res3d_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_52" + } + } + } +} +node { + name: "loss_1/res3a_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res3a_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res3b_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res3b_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res3d_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_51" + } + } + } +} +node { + name: "loss_1/res3b_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res3b_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res3b_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res3b_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res3d_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_49" + } + } + } +} +node { + name: "loss_1/res3b_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res3b_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res3b_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res3b_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res3d_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_48" + } + } + } +} +node { + name: "loss_1/res3b_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res3b_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res3c_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res3c_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res3d_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_47" + } + } + } +} +node { + name: "loss_1/res3c_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res3c_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res3c_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res3c_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res3d_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_45" + } + } + } +} +node { + name: "loss_1/res3c_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res3c_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res3c_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res3c_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res3d_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_44" + } + } + } +} +node { + name: "loss_1/res3c_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res3c_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res3d_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res3d_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res3d_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_43" + } + } + } +} +node { + name: "loss_1/res3d_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res3d_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res3d_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res3d_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res3d_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_41" + } + } + } +} +node { + name: "loss_1/res3d_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res3d_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res3d_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res3d_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res3d_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_40" + } + } + } +} +node { + name: "loss_1/res3d_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res3d_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res4a_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4a_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res4a_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_39" + } + } + } +} +node { + name: "loss_1/res4a_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4a_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res4a_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4a_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res4f_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_37" + } + } + } +} +node { + name: "loss_1/res4a_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4a_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res4a_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4a_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res4f_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_35" + } + } + } +} +node { + name: "loss_1/res4a_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4a_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res4b_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4b_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res4f_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_34" + } + } + } +} +node { + name: "loss_1/res4b_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4b_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res4b_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4b_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res4f_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_32" + } + } + } +} +node { + name: "loss_1/res4b_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4b_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res4b_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4b_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res4f_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_31" + } + } + } +} +node { + name: "loss_1/res4b_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4b_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res4c_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4c_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res4f_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_30" + } + } + } +} +node { + name: "loss_1/res4c_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4c_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res4c_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4c_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res4f_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_28" + } + } + } +} +node { + name: "loss_1/res4c_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4c_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res4c_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4c_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res4f_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_27" + } + } + } +} +node { + name: "loss_1/res4c_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4c_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res4d_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4d_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res4f_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_26" + } + } + } +} +node { + name: "loss_1/res4d_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4d_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res4d_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4d_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res4f_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_24" + } + } + } +} +node { + name: "loss_1/res4d_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4d_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res4d_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4d_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res4f_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_23" + } + } + } +} +node { + name: "loss_1/res4d_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4d_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res4e_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4e_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res4f_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_22" + } + } + } +} +node { + name: "loss_1/res4e_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4e_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res4e_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4e_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res4f_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_20" + } + } + } +} +node { + name: "loss_1/res4e_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4e_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res4e_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4e_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res4f_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_19" + } + } + } +} +node { + name: "loss_1/res4e_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4e_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res4f_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4f_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res4f_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_18" + } + } + } +} +node { + name: "loss_1/res4f_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4f_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res4f_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4f_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res4f_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_16" + } + } + } +} +node { + name: "loss_1/res4f_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4f_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res4f_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res4f_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res4f_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_15" + } + } + } +} +node { + name: "loss_1/res4f_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res4f_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res5a_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res5a_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res5a_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_14" + } + } + } +} +node { + name: "loss_1/res5a_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res5a_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res5a_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res5a_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res5c_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_12" + } + } + } +} +node { + name: "loss_1/res5a_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res5a_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res5a_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res5a_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res5c_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_10" + } + } + } +} +node { + name: "loss_1/res5a_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res5a_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res5b_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res5b_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res5c_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_9" + } + } + } +} +node { + name: "loss_1/res5b_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res5b_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res5b_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res5b_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res5c_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_7" + } + } + } +} +node { + name: "loss_1/res5b_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res5b_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res5b_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res5b_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res5c_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_6" + } + } + } +} +node { + name: "loss_1/res5b_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res5b_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res5c_branch2a/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res5c_branch2a/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res5c_branch2a/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_5" + } + } + } +} +node { + name: "loss_1/res5c_branch2a/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res5c_branch2a/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res5c_branch2b/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res5c_branch2b/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res5c_branch2b/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_3" + } + } + } +} +node { + name: "loss_1/res5c_branch2b/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res5c_branch2b/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/res5c_branch2c/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/res5c_branch2c/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_2" + } + } + } +} +node { + name: "loss_1/res5c_branch2c/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/fc1000/kernel/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/fc1000/kernel/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/fc1000/kernel/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_1" + } + } + } +} +node { + name: "loss_1/fc1000/kernel/Regularizer/Square" + op: "Square" + input: "loss_1/fc1000/kernel/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/fc1000/bias/Regularizer/Square_grad/Mul_1" + op: "Mul" + input: "loss_1/fc1000/bias/Regularizer/Square/ReadVariableOp" + input: "training/SGD/gradients/loss_1/fc1000/bias/Regularizer/Square_grad/Mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN" + } + } + } +} +node { + name: "loss_1/fc1000/bias/Regularizer/Square" + op: "Square" + input: "loss_1/fc1000/bias/Regularizer/Square/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/activation_49_loss/Cast" + op: "Cast" + input: "loss_1/activation_49_loss/Reshape" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_INT64 + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "loss_1/res5a_branch1/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res5a_branch1/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4a_branch1/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4a_branch1/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res3a_branch1/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res3a_branch1/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res2a_branch1/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res2a_branch1/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/conv1_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^conv1_1/Conv2D/ReadVariableOp" + input: "^conv1_pad_1/Pad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\007\000\000\000\007\000\000\000\003\000\000\000@\000\000\000" + } + } + } +} +node { + name: "conv1_1/Conv2D" + op: "Conv2D" + input: "conv1_pad_1/Pad" + input: "conv1_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "loss_1/conv1/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/conv1/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res2a_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res2a_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res2a_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res2a_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res2a_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res2a_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res2b_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res2b_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res2b_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res2b_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res2b_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res2b_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res2c_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res2c_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res2c_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res2c_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res2c_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res2c_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res3a_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res3a_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res3a_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res3a_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res3a_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res3a_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res3b_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res3b_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res3b_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res3b_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res3b_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res3b_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res3c_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res3c_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res3c_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res3c_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res3c_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res3c_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res3d_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res3d_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res3d_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res3d_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res3d_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res3d_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4a_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4a_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4a_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4a_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4a_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4a_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4b_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4b_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4b_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4b_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4b_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4b_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4c_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4c_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4c_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4c_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4c_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4c_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4d_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4d_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4d_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4d_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4d_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4d_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4e_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4e_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4e_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4e_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4e_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4e_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4f_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4f_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4f_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4f_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res4f_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res4f_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res5a_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res5a_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res5a_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res5a_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res5a_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res5a_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res5b_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res5b_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res5b_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res5b_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res5b_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res5b_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res5c_branch2a/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res5c_branch2a/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res5c_branch2b/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res5c_branch2b/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/res5c_branch2c/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Square" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/fc1000/kernel/Regularizer/Sum" + op: "Sum" + input: "loss_1/fc1000/kernel/Regularizer/Square" + input: "loss_1/fc1000/kernel/Regularizer/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "loss_1/fc1000/bias/Regularizer/Sum" + op: "Sum" + input: "loss_1/fc1000/bias/Regularizer/Square" + input: "metrics_2/sparse_categorical_accuracy/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "bn_conv1_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "conv1_1/Conv2D" + input: "bn_conv1_1/ReadVariableOp" + input: "bn_conv1_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "loss_1/ArithmeticOptimizer/HoistCommonFactor_Add_AddN" + op: "AddN" + input: "loss_1/conv1/kernel/Regularizer/Sum" + input: "loss_1/fc1000/bias/Regularizer/Sum" + input: "loss_1/fc1000/kernel/Regularizer/Sum" + input: "loss_1/res2a_branch1/kernel/Regularizer/Sum" + input: "loss_1/res2a_branch2a/kernel/Regularizer/Sum" + input: "loss_1/res2a_branch2b/kernel/Regularizer/Sum" + input: "loss_1/res2a_branch2c/kernel/Regularizer/Sum" + input: "loss_1/res2b_branch2a/kernel/Regularizer/Sum" + input: "loss_1/res2b_branch2b/kernel/Regularizer/Sum" + input: "loss_1/res2b_branch2c/kernel/Regularizer/Sum" + input: "loss_1/res2c_branch2a/kernel/Regularizer/Sum" + input: "loss_1/res2c_branch2b/kernel/Regularizer/Sum" + input: "loss_1/res2c_branch2c/kernel/Regularizer/Sum" + input: "loss_1/res3a_branch1/kernel/Regularizer/Sum" + input: "loss_1/res3a_branch2a/kernel/Regularizer/Sum" + input: "loss_1/res3a_branch2b/kernel/Regularizer/Sum" + input: "loss_1/res3a_branch2c/kernel/Regularizer/Sum" + input: "loss_1/res3b_branch2a/kernel/Regularizer/Sum" + input: "loss_1/res3b_branch2b/kernel/Regularizer/Sum" + input: "loss_1/res3b_branch2c/kernel/Regularizer/Sum" + input: "loss_1/res3c_branch2a/kernel/Regularizer/Sum" + input: "loss_1/res3c_branch2b/kernel/Regularizer/Sum" + input: "loss_1/res3c_branch2c/kernel/Regularizer/Sum" + input: "loss_1/res3d_branch2a/kernel/Regularizer/Sum" + input: "loss_1/res3d_branch2b/kernel/Regularizer/Sum" + input: "loss_1/res3d_branch2c/kernel/Regularizer/Sum" + input: "loss_1/res4a_branch1/kernel/Regularizer/Sum" + input: "loss_1/res4a_branch2a/kernel/Regularizer/Sum" + input: "loss_1/res4a_branch2b/kernel/Regularizer/Sum" + input: "loss_1/res4a_branch2c/kernel/Regularizer/Sum" + input: "loss_1/res4b_branch2a/kernel/Regularizer/Sum" + input: "loss_1/res4b_branch2b/kernel/Regularizer/Sum" + input: "loss_1/res4b_branch2c/kernel/Regularizer/Sum" + input: "loss_1/res4c_branch2a/kernel/Regularizer/Sum" + input: "loss_1/res4c_branch2b/kernel/Regularizer/Sum" + input: "loss_1/res4c_branch2c/kernel/Regularizer/Sum" + input: "loss_1/res4d_branch2a/kernel/Regularizer/Sum" + input: "loss_1/res4d_branch2b/kernel/Regularizer/Sum" + input: "loss_1/res4d_branch2c/kernel/Regularizer/Sum" + input: "loss_1/res4e_branch2a/kernel/Regularizer/Sum" + input: "loss_1/res4e_branch2b/kernel/Regularizer/Sum" + input: "loss_1/res4e_branch2c/kernel/Regularizer/Sum" + input: "loss_1/res4f_branch2a/kernel/Regularizer/Sum" + input: "loss_1/res4f_branch2b/kernel/Regularizer/Sum" + input: "loss_1/res4f_branch2c/kernel/Regularizer/Sum" + input: "loss_1/res5a_branch1/kernel/Regularizer/Sum" + input: "loss_1/res5a_branch2a/kernel/Regularizer/Sum" + input: "loss_1/res5a_branch2b/kernel/Regularizer/Sum" + input: "loss_1/res5a_branch2c/kernel/Regularizer/Sum" + input: "loss_1/res5b_branch2a/kernel/Regularizer/Sum" + input: "loss_1/res5b_branch2b/kernel/Regularizer/Sum" + input: "loss_1/res5b_branch2c/kernel/Regularizer/Sum" + input: "loss_1/res5c_branch2a/kernel/Regularizer/Sum" + input: "loss_1/res5c_branch2b/kernel/Regularizer/Sum" + input: "loss_1/res5c_branch2c/kernel/Regularizer/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 55 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn_conv1_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn_conv1_1/AssignMovingAvg/ReadVariableOp" + input: "bn_conv1_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn_conv1_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn_conv1_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn_conv1_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn_conv1_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn_conv1_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_50/Relu" + op: "Relu" + input: "bn_conv1_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/ArithmeticOptimizer/HoistCommonFactor_Mul_AddN" + op: "Mul" + input: "loss_1/fc1000/bias/Regularizer/mul/x" + input: "loss_1/ArithmeticOptimizer/HoistCommonFactor_Add_AddN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn_conv1_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn_conv1_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn_conv1_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn_conv1_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + input: "bn_conv1_1/AssignMovingAvg_1/sub_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn_conv1_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "max_pooling2d_1/MaxPool" + op: "MaxPool" + input: "activation_50/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "ksize" + value { + list { + i: 1 + i: 1 + i: 3 + i: 3 + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } +} +node { + name: "bn_conv1_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn_conv1/moving_mean" + input: "bn_conv1_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn_conv1_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn_conv1/moving_variance" + input: "bn_conv1_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/res2a_branch1_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "max_pooling2d_1/MaxPool" + input: "res2a_branch1_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "training/SGD/gradients/res2a_branch2a_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "max_pooling2d_1/MaxPool" + input: "res2a_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res2a_branch1_1/Conv2D" + op: "Conv2D" + input: "max_pooling2d_1/MaxPool" + input: "res2a_branch1_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "res2a_branch2a_1/Conv2D" + op: "Conv2D" + input: "max_pooling2d_1/MaxPool" + input: "res2a_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res2a_branch1_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res2a_branch1_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000@\000\000\000\000\001\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res2a_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res2a_branch2a_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000@\000\000\000@\000\000\000" + } + } + } +} +node { + name: "bn2a_branch1_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res2a_branch1_1/Conv2D" + input: "bn2a_branch1_1/ReadVariableOp" + input: "bn2a_branch1_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn2a_branch2a_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res2a_branch2a_1/Conv2D" + input: "bn2a_branch2a_1/ReadVariableOp" + input: "bn2a_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn2a_branch1_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn2a_branch1_1/AssignMovingAvg/ReadVariableOp" + input: "bn2a_branch1_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2a_branch1_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn2a_branch1_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn2a_branch1_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn2a_branch1_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2a_branch1_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "bn2a_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn2a_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn2a_branch2a_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2a_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn2a_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn2a_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn2a_branch2a_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2a_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_1_1/Relu" + op: "Relu" + input: "bn2a_branch2a_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch1_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn2a_branch1_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2a_branch1_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn2a_branch1_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn2a_branch1_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2a_branch1_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "bn2a_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn2a_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2a_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn2a_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn2a_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2a_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res2a_branch2b_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_1_1/Relu" + input: "res2a_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res2a_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_1_1/Relu" + input: "res2a_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn2a_branch1_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2a_branch1/moving_mean" + input: "bn2a_branch1_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch1_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2a_branch1/moving_variance" + input: "bn2a_branch1_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2a_branch2a/moving_mean" + input: "bn2a_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2a_branch2a/moving_variance" + input: "bn2a_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res2a_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res2a_branch2b_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000@\000\000\000@\000\000\000" + } + } + } +} +node { + name: "bn2a_branch2b_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res2a_branch2b_1/Conv2D" + input: "bn2a_branch2b_1/ReadVariableOp" + input: "bn2a_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn2a_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn2a_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn2a_branch2b_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2a_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn2a_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn2a_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn2a_branch2b_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2a_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_2_1/Relu" + op: "Relu" + input: "bn2a_branch2b_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn2a_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2a_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn2a_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn2a_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2a_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res2a_branch2c_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_2_1/Relu" + input: "res2a_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res2a_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_2_1/Relu" + input: "res2a_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn2a_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2a_branch2b/moving_mean" + input: "bn2a_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2a_branch2b/moving_variance" + input: "bn2a_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res2a_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res2a_branch2c_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000@\000\000\000\000\001\000\000" + } + } + } +} +node { + name: "bn2a_branch2c_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res2a_branch2c_1/Conv2D" + input: "bn2a_branch2c_1/ReadVariableOp" + input: "bn2a_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn2a_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn2a_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn2a_branch2c_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2a_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn2a_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn2a_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn2a_branch2c_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2a_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "add_16/add" + op: "Add" + input: "bn2a_branch1_1/FusedBatchNorm" + input: "bn2a_branch2c_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn2a_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2a_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn2a_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn2a_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2a_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_3_1/Relu" + op: "Relu" + input: "add_16/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2a_branch2c/moving_mean" + input: "bn2a_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2a_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2a_branch2c/moving_variance" + input: "bn2a_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/res2b_branch2a_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_3_1/Relu" + input: "res2b_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res2b_branch2a_1/Conv2D" + op: "Conv2D" + input: "activation_3_1/Relu" + input: "res2b_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res2b_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res2b_branch2a_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\001\000\000@\000\000\000" + } + } + } +} +node { + name: "bn2b_branch2a_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res2b_branch2a_1/Conv2D" + input: "bn2b_branch2a_1/ReadVariableOp" + input: "bn2b_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn2b_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn2b_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn2b_branch2a_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2b_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn2b_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn2b_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn2b_branch2a_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2b_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_4_1/Relu" + op: "Relu" + input: "bn2b_branch2a_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn2b_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2b_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn2b_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn2b_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2b_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res2b_branch2b_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_4_1/Relu" + input: "res2b_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res2b_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_4_1/Relu" + input: "res2b_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn2b_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2b_branch2a/moving_mean" + input: "bn2b_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2b_branch2a/moving_variance" + input: "bn2b_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res2b_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res2b_branch2b_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000@\000\000\000@\000\000\000" + } + } + } +} +node { + name: "bn2b_branch2b_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res2b_branch2b_1/Conv2D" + input: "bn2b_branch2b_1/ReadVariableOp" + input: "bn2b_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn2b_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn2b_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn2b_branch2b_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2b_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn2b_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn2b_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn2b_branch2b_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2b_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_5_1/Relu" + op: "Relu" + input: "bn2b_branch2b_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn2b_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2b_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn2b_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn2b_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2b_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res2b_branch2c_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_5_1/Relu" + input: "res2b_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res2b_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_5_1/Relu" + input: "res2b_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn2b_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2b_branch2b/moving_mean" + input: "bn2b_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2b_branch2b/moving_variance" + input: "bn2b_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res2b_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res2b_branch2c_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000@\000\000\000\000\001\000\000" + } + } + } +} +node { + name: "bn2b_branch2c_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res2b_branch2c_1/Conv2D" + input: "bn2b_branch2c_1/ReadVariableOp" + input: "bn2b_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn2b_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn2b_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn2b_branch2c_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2b_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn2b_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn2b_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn2b_branch2c_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2b_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "add_1_1/add" + op: "Add" + input: "activation_3_1/Relu" + input: "bn2b_branch2c_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn2b_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2b_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn2b_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn2b_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2b_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_6_1/Relu" + op: "Relu" + input: "add_1_1/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2b_branch2c/moving_mean" + input: "bn2b_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2b_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2b_branch2c/moving_variance" + input: "bn2b_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/res2c_branch2a_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_6_1/Relu" + input: "res2c_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res2c_branch2a_1/Conv2D" + op: "Conv2D" + input: "activation_6_1/Relu" + input: "res2c_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res2c_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res2c_branch2a_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\001\000\000@\000\000\000" + } + } + } +} +node { + name: "bn2c_branch2a_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res2c_branch2a_1/Conv2D" + input: "bn2c_branch2a_1/ReadVariableOp" + input: "bn2c_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn2c_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn2c_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn2c_branch2a_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2c_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn2c_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn2c_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn2c_branch2a_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2c_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_7_1/Relu" + op: "Relu" + input: "bn2c_branch2a_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn2c_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2c_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn2c_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn2c_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2c_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res2c_branch2b_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_7_1/Relu" + input: "res2c_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res2c_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_7_1/Relu" + input: "res2c_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn2c_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2c_branch2a/moving_mean" + input: "bn2c_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2c_branch2a/moving_variance" + input: "bn2c_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res2c_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res2c_branch2b_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000@\000\000\000@\000\000\000" + } + } + } +} +node { + name: "bn2c_branch2b_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res2c_branch2b_1/Conv2D" + input: "bn2c_branch2b_1/ReadVariableOp" + input: "bn2c_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn2c_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn2c_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn2c_branch2b_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2c_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn2c_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn2c_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn2c_branch2b_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2c_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_8_1/Relu" + op: "Relu" + input: "bn2c_branch2b_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn2c_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2c_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn2c_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn2c_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2c_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res2c_branch2c_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_8_1/Relu" + input: "res2c_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res2c_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_8_1/Relu" + input: "res2c_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn2c_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2c_branch2b/moving_mean" + input: "bn2c_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2c_branch2b/moving_variance" + input: "bn2c_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res2c_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res2c_branch2c_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000@\000\000\000\000\001\000\000" + } + } + } +} +node { + name: "bn2c_branch2c_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res2c_branch2c_1/Conv2D" + input: "bn2c_branch2c_1/ReadVariableOp" + input: "bn2c_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn2c_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn2c_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn2c_branch2c_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2c_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn2c_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn2c_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn2c_branch2c_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2c_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "add_2_1/add" + op: "Add" + input: "activation_6_1/Relu" + input: "bn2c_branch2c_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn2c_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2c_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn2c_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn2c_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn2c_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_9_1/Relu" + op: "Relu" + input: "add_2_1/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2c_branch2c/moving_mean" + input: "bn2c_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn2c_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn2c_branch2c/moving_variance" + input: "bn2c_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/res3a_branch1_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_9_1/Relu" + input: "res3a_branch1_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "training/SGD/gradients/res3a_branch2a_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_9_1/Relu" + input: "res3a_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res3a_branch1_1/Conv2D" + op: "Conv2D" + input: "activation_9_1/Relu" + input: "res3a_branch1_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "res3a_branch2a_1/Conv2D" + op: "Conv2D" + input: "activation_9_1/Relu" + input: "res3a_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res3a_branch1_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res3a_branch1_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\001\000\000\000\002\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res3a_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res3a_branch2a_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\001\000\000\200\000\000\000" + } + } + } +} +node { + name: "bn3a_branch1_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res3a_branch1_1/Conv2D" + input: "bn3a_branch1_1/ReadVariableOp" + input: "bn3a_branch1_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn3a_branch2a_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res3a_branch2a_1/Conv2D" + input: "bn3a_branch2a_1/ReadVariableOp" + input: "bn3a_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn3a_branch1_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn3a_branch1_1/AssignMovingAvg/ReadVariableOp" + input: "bn3a_branch1_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3a_branch1_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn3a_branch1_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn3a_branch1_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn3a_branch1_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3a_branch1_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "bn3a_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn3a_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn3a_branch2a_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3a_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn3a_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn3a_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn3a_branch2a_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3a_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_10_1/Relu" + op: "Relu" + input: "bn3a_branch2a_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch1_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn3a_branch1_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3a_branch1_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn3a_branch1_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn3a_branch1_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3a_branch1_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "bn3a_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn3a_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3a_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn3a_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn3a_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3a_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res3a_branch2b_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_10_1/Relu" + input: "res3a_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res3a_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_10_1/Relu" + input: "res3a_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn3a_branch1_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3a_branch1/moving_mean" + input: "bn3a_branch1_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch1_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3a_branch1/moving_variance" + input: "bn3a_branch1_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3a_branch2a/moving_mean" + input: "bn3a_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3a_branch2a/moving_variance" + input: "bn3a_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res3a_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res3a_branch2b_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000\200\000\000\000\200\000\000\000" + } + } + } +} +node { + name: "bn3a_branch2b_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res3a_branch2b_1/Conv2D" + input: "bn3a_branch2b_1/ReadVariableOp" + input: "bn3a_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn3a_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn3a_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn3a_branch2b_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3a_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn3a_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn3a_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn3a_branch2b_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3a_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_11_1/Relu" + op: "Relu" + input: "bn3a_branch2b_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn3a_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3a_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn3a_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn3a_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3a_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res3a_branch2c_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_11_1/Relu" + input: "res3a_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res3a_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_11_1/Relu" + input: "res3a_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn3a_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3a_branch2b/moving_mean" + input: "bn3a_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3a_branch2b/moving_variance" + input: "bn3a_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res3a_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res3a_branch2c_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\200\000\000\000\000\002\000\000" + } + } + } +} +node { + name: "bn3a_branch2c_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res3a_branch2c_1/Conv2D" + input: "bn3a_branch2c_1/ReadVariableOp" + input: "bn3a_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn3a_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn3a_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn3a_branch2c_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3a_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn3a_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn3a_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn3a_branch2c_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3a_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "add_3_1/add" + op: "Add" + input: "bn3a_branch1_1/FusedBatchNorm" + input: "bn3a_branch2c_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn3a_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3a_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn3a_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn3a_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3a_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_12_1/Relu" + op: "Relu" + input: "add_3_1/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3a_branch2c/moving_mean" + input: "bn3a_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3a_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3a_branch2c/moving_variance" + input: "bn3a_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/res3b_branch2a_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_12_1/Relu" + input: "res3b_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res3b_branch2a_1/Conv2D" + op: "Conv2D" + input: "activation_12_1/Relu" + input: "res3b_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res3b_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res3b_branch2a_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\002\000\000\200\000\000\000" + } + } + } +} +node { + name: "bn3b_branch2a_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res3b_branch2a_1/Conv2D" + input: "bn3b_branch2a_1/ReadVariableOp" + input: "bn3b_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn3b_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn3b_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn3b_branch2a_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3b_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn3b_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn3b_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn3b_branch2a_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3b_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_13_1/Relu" + op: "Relu" + input: "bn3b_branch2a_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn3b_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3b_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn3b_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn3b_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3b_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res3b_branch2b_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_13_1/Relu" + input: "res3b_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res3b_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_13_1/Relu" + input: "res3b_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn3b_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3b_branch2a/moving_mean" + input: "bn3b_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3b_branch2a/moving_variance" + input: "bn3b_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res3b_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res3b_branch2b_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000\200\000\000\000\200\000\000\000" + } + } + } +} +node { + name: "bn3b_branch2b_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res3b_branch2b_1/Conv2D" + input: "bn3b_branch2b_1/ReadVariableOp" + input: "bn3b_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn3b_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn3b_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn3b_branch2b_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3b_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn3b_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn3b_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn3b_branch2b_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3b_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_14_1/Relu" + op: "Relu" + input: "bn3b_branch2b_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn3b_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3b_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn3b_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn3b_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3b_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res3b_branch2c_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_14_1/Relu" + input: "res3b_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res3b_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_14_1/Relu" + input: "res3b_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn3b_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3b_branch2b/moving_mean" + input: "bn3b_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3b_branch2b/moving_variance" + input: "bn3b_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res3b_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res3b_branch2c_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\200\000\000\000\000\002\000\000" + } + } + } +} +node { + name: "bn3b_branch2c_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res3b_branch2c_1/Conv2D" + input: "bn3b_branch2c_1/ReadVariableOp" + input: "bn3b_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn3b_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn3b_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn3b_branch2c_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3b_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn3b_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn3b_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn3b_branch2c_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3b_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "add_4_1/add" + op: "Add" + input: "activation_12_1/Relu" + input: "bn3b_branch2c_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn3b_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3b_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn3b_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn3b_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3b_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_15_1/Relu" + op: "Relu" + input: "add_4_1/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3b_branch2c/moving_mean" + input: "bn3b_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3b_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3b_branch2c/moving_variance" + input: "bn3b_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/res3c_branch2a_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_15_1/Relu" + input: "res3c_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res3c_branch2a_1/Conv2D" + op: "Conv2D" + input: "activation_15_1/Relu" + input: "res3c_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res3c_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res3c_branch2a_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\002\000\000\200\000\000\000" + } + } + } +} +node { + name: "bn3c_branch2a_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res3c_branch2a_1/Conv2D" + input: "bn3c_branch2a_1/ReadVariableOp" + input: "bn3c_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn3c_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn3c_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn3c_branch2a_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3c_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn3c_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn3c_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn3c_branch2a_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3c_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_16_1/Relu" + op: "Relu" + input: "bn3c_branch2a_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn3c_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3c_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn3c_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn3c_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3c_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res3c_branch2b_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_16_1/Relu" + input: "res3c_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res3c_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_16_1/Relu" + input: "res3c_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn3c_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3c_branch2a/moving_mean" + input: "bn3c_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3c_branch2a/moving_variance" + input: "bn3c_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res3c_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res3c_branch2b_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000\200\000\000\000\200\000\000\000" + } + } + } +} +node { + name: "bn3c_branch2b_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res3c_branch2b_1/Conv2D" + input: "bn3c_branch2b_1/ReadVariableOp" + input: "bn3c_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn3c_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn3c_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn3c_branch2b_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3c_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn3c_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn3c_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn3c_branch2b_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3c_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_17_1/Relu" + op: "Relu" + input: "bn3c_branch2b_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn3c_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3c_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn3c_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn3c_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3c_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res3c_branch2c_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_17_1/Relu" + input: "res3c_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res3c_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_17_1/Relu" + input: "res3c_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn3c_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3c_branch2b/moving_mean" + input: "bn3c_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3c_branch2b/moving_variance" + input: "bn3c_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res3c_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res3c_branch2c_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\200\000\000\000\000\002\000\000" + } + } + } +} +node { + name: "bn3c_branch2c_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res3c_branch2c_1/Conv2D" + input: "bn3c_branch2c_1/ReadVariableOp" + input: "bn3c_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn3c_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn3c_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn3c_branch2c_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3c_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn3c_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn3c_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn3c_branch2c_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3c_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "add_5_1/add" + op: "Add" + input: "activation_15_1/Relu" + input: "bn3c_branch2c_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn3c_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3c_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn3c_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn3c_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3c_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_18_1/Relu" + op: "Relu" + input: "add_5_1/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3c_branch2c/moving_mean" + input: "bn3c_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3c_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3c_branch2c/moving_variance" + input: "bn3c_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/res3d_branch2a_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_18_1/Relu" + input: "res3d_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res3d_branch2a_1/Conv2D" + op: "Conv2D" + input: "activation_18_1/Relu" + input: "res3d_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res3d_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res3d_branch2a_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\002\000\000\200\000\000\000" + } + } + } +} +node { + name: "bn3d_branch2a_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res3d_branch2a_1/Conv2D" + input: "bn3d_branch2a_1/ReadVariableOp" + input: "bn3d_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn3d_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn3d_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn3d_branch2a_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3d_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn3d_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn3d_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn3d_branch2a_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3d_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_19_1/Relu" + op: "Relu" + input: "bn3d_branch2a_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn3d_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3d_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn3d_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn3d_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3d_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res3d_branch2b_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_19_1/Relu" + input: "res3d_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res3d_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_19_1/Relu" + input: "res3d_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn3d_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3d_branch2a/moving_mean" + input: "bn3d_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3d_branch2a/moving_variance" + input: "bn3d_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res3d_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res3d_branch2b_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000\200\000\000\000\200\000\000\000" + } + } + } +} +node { + name: "bn3d_branch2b_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res3d_branch2b_1/Conv2D" + input: "bn3d_branch2b_1/ReadVariableOp" + input: "bn3d_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn3d_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn3d_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn3d_branch2b_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3d_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn3d_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn3d_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn3d_branch2b_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3d_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_20_1/Relu" + op: "Relu" + input: "bn3d_branch2b_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn3d_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3d_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn3d_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn3d_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3d_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res3d_branch2c_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_20_1/Relu" + input: "res3d_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res3d_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_20_1/Relu" + input: "res3d_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn3d_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3d_branch2b/moving_mean" + input: "bn3d_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3d_branch2b/moving_variance" + input: "bn3d_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res3d_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res3d_branch2c_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\200\000\000\000\000\002\000\000" + } + } + } +} +node { + name: "bn3d_branch2c_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res3d_branch2c_1/Conv2D" + input: "bn3d_branch2c_1/ReadVariableOp" + input: "bn3d_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn3d_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn3d_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn3d_branch2c_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3d_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn3d_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn3d_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn3d_branch2c_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3d_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "add_6_1/add" + op: "Add" + input: "activation_18_1/Relu" + input: "bn3d_branch2c_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn3d_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3d_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn3d_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn3d_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn3d_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_21_1/Relu" + op: "Relu" + input: "add_6_1/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3d_branch2c/moving_mean" + input: "bn3d_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn3d_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn3d_branch2c/moving_variance" + input: "bn3d_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/res4a_branch1_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_21_1/Relu" + input: "res4a_branch1_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "training/SGD/gradients/res4a_branch2a_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_21_1/Relu" + input: "res4a_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res4a_branch1_1/Conv2D" + op: "Conv2D" + input: "activation_21_1/Relu" + input: "res4a_branch1_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "res4a_branch2a_1/Conv2D" + op: "Conv2D" + input: "activation_21_1/Relu" + input: "res4a_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res4a_branch1_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res4a_branch1_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\002\000\000\000\004\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res4a_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res4a_branch2a_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\002\000\000\000\001\000\000" + } + } + } +} +node { + name: "bn4a_branch1_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res4a_branch1_1/Conv2D" + input: "bn4a_branch1_1/ReadVariableOp" + input: "bn4a_branch1_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4a_branch2a_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res4a_branch2a_1/Conv2D" + input: "bn4a_branch2a_1/ReadVariableOp" + input: "bn4a_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4a_branch1_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4a_branch1_1/AssignMovingAvg/ReadVariableOp" + input: "bn4a_branch1_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4a_branch1_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4a_branch1_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4a_branch1_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4a_branch1_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4a_branch1_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4a_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4a_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn4a_branch2a_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4a_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4a_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4a_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4a_branch2a_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4a_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_22_1/Relu" + op: "Relu" + input: "bn4a_branch2a_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch1_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4a_branch1_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4a_branch1_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4a_branch1_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4a_branch1_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4a_branch1_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4a_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4a_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4a_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4a_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4a_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4a_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res4a_branch2b_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_22_1/Relu" + input: "res4a_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res4a_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_22_1/Relu" + input: "res4a_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn4a_branch1_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4a_branch1/moving_mean" + input: "bn4a_branch1_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch1_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4a_branch1/moving_variance" + input: "bn4a_branch1_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4a_branch2a/moving_mean" + input: "bn4a_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4a_branch2a/moving_variance" + input: "bn4a_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res4a_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res4a_branch2b_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000\000\001\000\000\000\001\000\000" + } + } + } +} +node { + name: "bn4a_branch2b_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res4a_branch2b_1/Conv2D" + input: "bn4a_branch2b_1/ReadVariableOp" + input: "bn4a_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4a_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4a_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn4a_branch2b_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4a_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4a_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4a_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4a_branch2b_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4a_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_23_1/Relu" + op: "Relu" + input: "bn4a_branch2b_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4a_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4a_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4a_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4a_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4a_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res4a_branch2c_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_23_1/Relu" + input: "res4a_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res4a_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_23_1/Relu" + input: "res4a_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn4a_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4a_branch2b/moving_mean" + input: "bn4a_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4a_branch2b/moving_variance" + input: "bn4a_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res4a_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res4a_branch2c_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\001\000\000\000\004\000\000" + } + } + } +} +node { + name: "bn4a_branch2c_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res4a_branch2c_1/Conv2D" + input: "bn4a_branch2c_1/ReadVariableOp" + input: "bn4a_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4a_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4a_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn4a_branch2c_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4a_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4a_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4a_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4a_branch2c_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4a_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "add_7_1/add" + op: "Add" + input: "bn4a_branch1_1/FusedBatchNorm" + input: "bn4a_branch2c_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4a_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4a_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4a_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4a_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4a_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_24_1/Relu" + op: "Relu" + input: "add_7_1/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4a_branch2c/moving_mean" + input: "bn4a_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4a_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4a_branch2c/moving_variance" + input: "bn4a_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/res4b_branch2a_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_24_1/Relu" + input: "res4b_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res4b_branch2a_1/Conv2D" + op: "Conv2D" + input: "activation_24_1/Relu" + input: "res4b_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res4b_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res4b_branch2a_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\004\000\000\000\001\000\000" + } + } + } +} +node { + name: "bn4b_branch2a_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res4b_branch2a_1/Conv2D" + input: "bn4b_branch2a_1/ReadVariableOp" + input: "bn4b_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4b_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4b_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn4b_branch2a_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4b_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4b_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4b_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4b_branch2a_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4b_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_25_1/Relu" + op: "Relu" + input: "bn4b_branch2a_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4b_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4b_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4b_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4b_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4b_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res4b_branch2b_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_25_1/Relu" + input: "res4b_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res4b_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_25_1/Relu" + input: "res4b_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn4b_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4b_branch2a/moving_mean" + input: "bn4b_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4b_branch2a/moving_variance" + input: "bn4b_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res4b_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res4b_branch2b_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000\000\001\000\000\000\001\000\000" + } + } + } +} +node { + name: "bn4b_branch2b_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res4b_branch2b_1/Conv2D" + input: "bn4b_branch2b_1/ReadVariableOp" + input: "bn4b_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4b_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4b_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn4b_branch2b_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4b_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4b_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4b_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4b_branch2b_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4b_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_26_1/Relu" + op: "Relu" + input: "bn4b_branch2b_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4b_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4b_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4b_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4b_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4b_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res4b_branch2c_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_26_1/Relu" + input: "res4b_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res4b_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_26_1/Relu" + input: "res4b_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn4b_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4b_branch2b/moving_mean" + input: "bn4b_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4b_branch2b/moving_variance" + input: "bn4b_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res4b_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res4b_branch2c_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\001\000\000\000\004\000\000" + } + } + } +} +node { + name: "bn4b_branch2c_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res4b_branch2c_1/Conv2D" + input: "bn4b_branch2c_1/ReadVariableOp" + input: "bn4b_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4b_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4b_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn4b_branch2c_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4b_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4b_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4b_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4b_branch2c_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4b_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "add_8_1/add" + op: "Add" + input: "activation_24_1/Relu" + input: "bn4b_branch2c_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4b_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4b_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4b_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4b_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4b_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_27_1/Relu" + op: "Relu" + input: "add_8_1/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4b_branch2c/moving_mean" + input: "bn4b_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4b_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4b_branch2c/moving_variance" + input: "bn4b_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/res4c_branch2a_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_27_1/Relu" + input: "res4c_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res4c_branch2a_1/Conv2D" + op: "Conv2D" + input: "activation_27_1/Relu" + input: "res4c_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res4c_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res4c_branch2a_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\004\000\000\000\001\000\000" + } + } + } +} +node { + name: "bn4c_branch2a_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res4c_branch2a_1/Conv2D" + input: "bn4c_branch2a_1/ReadVariableOp" + input: "bn4c_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4c_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4c_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn4c_branch2a_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4c_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4c_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4c_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4c_branch2a_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4c_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_28_1/Relu" + op: "Relu" + input: "bn4c_branch2a_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4c_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4c_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4c_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4c_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4c_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res4c_branch2b_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_28_1/Relu" + input: "res4c_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res4c_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_28_1/Relu" + input: "res4c_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn4c_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4c_branch2a/moving_mean" + input: "bn4c_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4c_branch2a/moving_variance" + input: "bn4c_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res4c_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res4c_branch2b_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000\000\001\000\000\000\001\000\000" + } + } + } +} +node { + name: "bn4c_branch2b_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res4c_branch2b_1/Conv2D" + input: "bn4c_branch2b_1/ReadVariableOp" + input: "bn4c_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4c_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4c_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn4c_branch2b_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4c_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4c_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4c_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4c_branch2b_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4c_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_29_1/Relu" + op: "Relu" + input: "bn4c_branch2b_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4c_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4c_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4c_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4c_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4c_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res4c_branch2c_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_29_1/Relu" + input: "res4c_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res4c_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_29_1/Relu" + input: "res4c_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn4c_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4c_branch2b/moving_mean" + input: "bn4c_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4c_branch2b/moving_variance" + input: "bn4c_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res4c_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res4c_branch2c_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\001\000\000\000\004\000\000" + } + } + } +} +node { + name: "bn4c_branch2c_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res4c_branch2c_1/Conv2D" + input: "bn4c_branch2c_1/ReadVariableOp" + input: "bn4c_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4c_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4c_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn4c_branch2c_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4c_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4c_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4c_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4c_branch2c_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4c_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "add_9_1/add" + op: "Add" + input: "activation_27_1/Relu" + input: "bn4c_branch2c_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4c_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4c_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4c_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4c_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4c_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_30_1/Relu" + op: "Relu" + input: "add_9_1/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4c_branch2c/moving_mean" + input: "bn4c_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4c_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4c_branch2c/moving_variance" + input: "bn4c_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/res4d_branch2a_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_30_1/Relu" + input: "res4d_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res4d_branch2a_1/Conv2D" + op: "Conv2D" + input: "activation_30_1/Relu" + input: "res4d_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res4d_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res4d_branch2a_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\004\000\000\000\001\000\000" + } + } + } +} +node { + name: "bn4d_branch2a_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res4d_branch2a_1/Conv2D" + input: "bn4d_branch2a_1/ReadVariableOp" + input: "bn4d_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4d_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4d_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn4d_branch2a_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4d_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4d_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4d_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4d_branch2a_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4d_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_31_1/Relu" + op: "Relu" + input: "bn4d_branch2a_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4d_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4d_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4d_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4d_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4d_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res4d_branch2b_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_31_1/Relu" + input: "res4d_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res4d_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_31_1/Relu" + input: "res4d_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn4d_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4d_branch2a/moving_mean" + input: "bn4d_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4d_branch2a/moving_variance" + input: "bn4d_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res4d_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res4d_branch2b_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000\000\001\000\000\000\001\000\000" + } + } + } +} +node { + name: "bn4d_branch2b_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res4d_branch2b_1/Conv2D" + input: "bn4d_branch2b_1/ReadVariableOp" + input: "bn4d_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4d_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4d_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn4d_branch2b_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4d_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4d_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4d_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4d_branch2b_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4d_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_32_1/Relu" + op: "Relu" + input: "bn4d_branch2b_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4d_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4d_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4d_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4d_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4d_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res4d_branch2c_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_32_1/Relu" + input: "res4d_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res4d_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_32_1/Relu" + input: "res4d_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn4d_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4d_branch2b/moving_mean" + input: "bn4d_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4d_branch2b/moving_variance" + input: "bn4d_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res4d_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res4d_branch2c_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\001\000\000\000\004\000\000" + } + } + } +} +node { + name: "bn4d_branch2c_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res4d_branch2c_1/Conv2D" + input: "bn4d_branch2c_1/ReadVariableOp" + input: "bn4d_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4d_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4d_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn4d_branch2c_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4d_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4d_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4d_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4d_branch2c_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4d_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "add_10_1/add" + op: "Add" + input: "activation_30_1/Relu" + input: "bn4d_branch2c_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4d_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4d_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4d_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4d_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4d_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_33_1/Relu" + op: "Relu" + input: "add_10_1/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4d_branch2c/moving_mean" + input: "bn4d_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4d_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4d_branch2c/moving_variance" + input: "bn4d_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/res4e_branch2a_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_33_1/Relu" + input: "res4e_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res4e_branch2a_1/Conv2D" + op: "Conv2D" + input: "activation_33_1/Relu" + input: "res4e_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res4e_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res4e_branch2a_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\004\000\000\000\001\000\000" + } + } + } +} +node { + name: "bn4e_branch2a_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res4e_branch2a_1/Conv2D" + input: "bn4e_branch2a_1/ReadVariableOp" + input: "bn4e_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4e_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4e_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn4e_branch2a_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4e_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4e_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4e_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4e_branch2a_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4e_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_34_1/Relu" + op: "Relu" + input: "bn4e_branch2a_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4e_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4e_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4e_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4e_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4e_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res4e_branch2b_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_34_1/Relu" + input: "res4e_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res4e_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_34_1/Relu" + input: "res4e_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn4e_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4e_branch2a/moving_mean" + input: "bn4e_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4e_branch2a/moving_variance" + input: "bn4e_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res4e_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res4e_branch2b_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000\000\001\000\000\000\001\000\000" + } + } + } +} +node { + name: "bn4e_branch2b_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res4e_branch2b_1/Conv2D" + input: "bn4e_branch2b_1/ReadVariableOp" + input: "bn4e_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4e_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4e_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn4e_branch2b_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4e_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4e_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4e_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4e_branch2b_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4e_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_35_1/Relu" + op: "Relu" + input: "bn4e_branch2b_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4e_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4e_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4e_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4e_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4e_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res4e_branch2c_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_35_1/Relu" + input: "res4e_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res4e_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_35_1/Relu" + input: "res4e_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn4e_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4e_branch2b/moving_mean" + input: "bn4e_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4e_branch2b/moving_variance" + input: "bn4e_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res4e_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res4e_branch2c_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\001\000\000\000\004\000\000" + } + } + } +} +node { + name: "bn4e_branch2c_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res4e_branch2c_1/Conv2D" + input: "bn4e_branch2c_1/ReadVariableOp" + input: "bn4e_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4e_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4e_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn4e_branch2c_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4e_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4e_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4e_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4e_branch2c_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4e_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "add_11_1/add" + op: "Add" + input: "activation_33_1/Relu" + input: "bn4e_branch2c_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4e_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4e_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4e_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4e_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4e_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_36_1/Relu" + op: "Relu" + input: "add_11_1/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4e_branch2c/moving_mean" + input: "bn4e_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4e_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4e_branch2c/moving_variance" + input: "bn4e_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/res4f_branch2a_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_36_1/Relu" + input: "res4f_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res4f_branch2a_1/Conv2D" + op: "Conv2D" + input: "activation_36_1/Relu" + input: "res4f_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res4f_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res4f_branch2a_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\004\000\000\000\001\000\000" + } + } + } +} +node { + name: "bn4f_branch2a_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res4f_branch2a_1/Conv2D" + input: "bn4f_branch2a_1/ReadVariableOp" + input: "bn4f_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4f_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4f_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn4f_branch2a_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4f_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4f_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4f_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4f_branch2a_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4f_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_37_1/Relu" + op: "Relu" + input: "bn4f_branch2a_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4f_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4f_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4f_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4f_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4f_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res4f_branch2b_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_37_1/Relu" + input: "res4f_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res4f_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_37_1/Relu" + input: "res4f_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn4f_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4f_branch2a/moving_mean" + input: "bn4f_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4f_branch2a/moving_variance" + input: "bn4f_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res4f_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res4f_branch2b_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000\000\001\000\000\000\001\000\000" + } + } + } +} +node { + name: "bn4f_branch2b_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res4f_branch2b_1/Conv2D" + input: "bn4f_branch2b_1/ReadVariableOp" + input: "bn4f_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4f_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4f_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn4f_branch2b_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4f_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4f_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4f_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4f_branch2b_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4f_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_38_1/Relu" + op: "Relu" + input: "bn4f_branch2b_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4f_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4f_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4f_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4f_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4f_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res4f_branch2c_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_38_1/Relu" + input: "res4f_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res4f_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_38_1/Relu" + input: "res4f_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn4f_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4f_branch2b/moving_mean" + input: "bn4f_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4f_branch2b/moving_variance" + input: "bn4f_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res4f_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res4f_branch2c_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\001\000\000\000\004\000\000" + } + } + } +} +node { + name: "bn4f_branch2c_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res4f_branch2c_1/Conv2D" + input: "bn4f_branch2c_1/ReadVariableOp" + input: "bn4f_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn4f_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn4f_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn4f_branch2c_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4f_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4f_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn4f_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn4f_branch2c_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4f_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "add_12_1/add" + op: "Add" + input: "activation_36_1/Relu" + input: "bn4f_branch2c_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn4f_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4f_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn4f_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn4f_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn4f_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_39_1/Relu" + op: "Relu" + input: "add_12_1/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4f_branch2c/moving_mean" + input: "bn4f_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn4f_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn4f_branch2c/moving_variance" + input: "bn4f_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/res5a_branch1_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_39_1/Relu" + input: "res5a_branch1_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "training/SGD/gradients/res5a_branch2a_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_39_1/Relu" + input: "res5a_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res5a_branch1_1/Conv2D" + op: "Conv2D" + input: "activation_39_1/Relu" + input: "res5a_branch1_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "res5a_branch2a_1/Conv2D" + op: "Conv2D" + input: "activation_39_1/Relu" + input: "res5a_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res5a_branch1_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res5a_branch1_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\004\000\000\000\010\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res5a_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res5a_branch2a_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\004\000\000\000\002\000\000" + } + } + } +} +node { + name: "bn5a_branch1_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res5a_branch1_1/Conv2D" + input: "bn5a_branch1_1/ReadVariableOp" + input: "bn5a_branch1_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn5a_branch2a_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res5a_branch2a_1/Conv2D" + input: "bn5a_branch2a_1/ReadVariableOp" + input: "bn5a_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn5a_branch1_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn5a_branch1_1/AssignMovingAvg/ReadVariableOp" + input: "bn5a_branch1_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5a_branch1_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn5a_branch1_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn5a_branch1_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn5a_branch1_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5a_branch1_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "bn5a_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn5a_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn5a_branch2a_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5a_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn5a_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn5a_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn5a_branch2a_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5a_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_40_1/Relu" + op: "Relu" + input: "bn5a_branch2a_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch1_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn5a_branch1_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5a_branch1_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn5a_branch1_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn5a_branch1_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5a_branch1_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "bn5a_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn5a_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5a_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn5a_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn5a_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5a_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res5a_branch2b_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_40_1/Relu" + input: "res5a_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res5a_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_40_1/Relu" + input: "res5a_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn5a_branch1_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5a_branch1/moving_mean" + input: "bn5a_branch1_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch1_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5a_branch1/moving_variance" + input: "bn5a_branch1_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5a_branch2a/moving_mean" + input: "bn5a_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5a_branch2a/moving_variance" + input: "bn5a_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res5a_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res5a_branch2b_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000\000\002\000\000\000\002\000\000" + } + } + } +} +node { + name: "bn5a_branch2b_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res5a_branch2b_1/Conv2D" + input: "bn5a_branch2b_1/ReadVariableOp" + input: "bn5a_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn5a_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn5a_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn5a_branch2b_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5a_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn5a_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn5a_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn5a_branch2b_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5a_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_41_1/Relu" + op: "Relu" + input: "bn5a_branch2b_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn5a_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5a_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn5a_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn5a_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5a_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res5a_branch2c_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_41_1/Relu" + input: "res5a_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res5a_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_41_1/Relu" + input: "res5a_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn5a_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5a_branch2b/moving_mean" + input: "bn5a_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5a_branch2b/moving_variance" + input: "bn5a_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res5a_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res5a_branch2c_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\002\000\000\000\010\000\000" + } + } + } +} +node { + name: "bn5a_branch2c_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res5a_branch2c_1/Conv2D" + input: "bn5a_branch2c_1/ReadVariableOp" + input: "bn5a_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn5a_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn5a_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn5a_branch2c_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5a_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn5a_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn5a_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn5a_branch2c_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5a_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "add_13_1/add" + op: "Add" + input: "bn5a_branch1_1/FusedBatchNorm" + input: "bn5a_branch2c_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn5a_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5a_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn5a_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn5a_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5a_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_42_1/Relu" + op: "Relu" + input: "add_13_1/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5a_branch2c/moving_mean" + input: "bn5a_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5a_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5a_branch2c/moving_variance" + input: "bn5a_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res5b_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_42_1/Relu" + input: "^res5b_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "u\000\000\000\000\010\000\000\007\000\000\000\007\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res5b_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_42_1/Relu" + input: "^res5b_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\010\000\000\000\002\000\000" + } + } + } +} +node { + name: "res5b_branch2a_1/Conv2D" + op: "Conv2D" + input: "activation_42_1/Relu" + input: "res5b_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn5b_branch2a_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res5b_branch2a_1/Conv2D" + input: "bn5b_branch2a_1/ReadVariableOp" + input: "bn5b_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn5b_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn5b_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn5b_branch2a_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5b_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn5b_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn5b_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn5b_branch2a_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5b_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_43_1/Relu" + op: "Relu" + input: "bn5b_branch2a_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn5b_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5b_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn5b_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn5b_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5b_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res5b_branch2b_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_43_1/Relu" + input: "res5b_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res5b_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_43_1/Relu" + input: "res5b_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn5b_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5b_branch2a/moving_mean" + input: "bn5b_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5b_branch2a/moving_variance" + input: "bn5b_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res5b_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res5b_branch2b_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000\000\002\000\000\000\002\000\000" + } + } + } +} +node { + name: "bn5b_branch2b_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res5b_branch2b_1/Conv2D" + input: "bn5b_branch2b_1/ReadVariableOp" + input: "bn5b_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn5b_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn5b_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn5b_branch2b_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5b_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn5b_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn5b_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn5b_branch2b_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5b_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_44_1/Relu" + op: "Relu" + input: "bn5b_branch2b_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn5b_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5b_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn5b_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn5b_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5b_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res5b_branch2c_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_44_1/Relu" + input: "res5b_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res5b_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_44_1/Relu" + input: "res5b_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn5b_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5b_branch2b/moving_mean" + input: "bn5b_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5b_branch2b/moving_variance" + input: "bn5b_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res5b_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res5b_branch2c_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\002\000\000\000\010\000\000" + } + } + } +} +node { + name: "bn5b_branch2c_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res5b_branch2c_1/Conv2D" + input: "bn5b_branch2c_1/ReadVariableOp" + input: "bn5b_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn5b_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn5b_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn5b_branch2c_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5b_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn5b_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn5b_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn5b_branch2c_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5b_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "add_14_1/add" + op: "Add" + input: "activation_42_1/Relu" + input: "bn5b_branch2c_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn5b_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5b_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn5b_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn5b_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5b_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_45_1/Relu" + op: "Relu" + input: "add_14_1/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5b_branch2c/moving_mean" + input: "bn5b_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5b_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5b_branch2c/moving_variance" + input: "bn5b_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res5c_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + op: "Const" + input: "^activation_45_1/Relu" + input: "^res5c_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "u\000\000\000\000\010\000\000\007\000\000\000\007\000\000\000" + } + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res5c_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^activation_45_1/Relu" + input: "^res5c_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\010\000\000\000\002\000\000" + } + } + } +} +node { + name: "res5c_branch2a_1/Conv2D" + op: "Conv2D" + input: "activation_45_1/Relu" + input: "res5c_branch2a_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn5c_branch2a_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res5c_branch2a_1/Conv2D" + input: "bn5c_branch2a_1/ReadVariableOp" + input: "bn5c_branch2a_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn5c_branch2a_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn5c_branch2a_1/AssignMovingAvg/ReadVariableOp" + input: "bn5c_branch2a_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5c_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn5c_branch2a_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn5c_branch2a_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn5c_branch2a_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5c_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_46_1/Relu" + op: "Relu" + input: "bn5c_branch2a_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2a_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn5c_branch2a_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5c_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn5c_branch2a_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn5c_branch2a_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5c_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res5c_branch2b_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_46_1/Relu" + input: "res5c_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res5c_branch2b_1/Conv2D" + op: "Conv2D" + input: "activation_46_1/Relu" + input: "res5c_branch2b_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn5c_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5c_branch2a/moving_mean" + input: "bn5c_branch2a_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5c_branch2a/moving_variance" + input: "bn5c_branch2a_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res5c_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res5c_branch2b_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\003\000\000\000\003\000\000\000\000\002\000\000\000\002\000\000" + } + } + } +} +node { + name: "bn5c_branch2b_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res5c_branch2b_1/Conv2D" + input: "bn5c_branch2b_1/ReadVariableOp" + input: "bn5c_branch2b_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn5c_branch2b_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn5c_branch2b_1/AssignMovingAvg/ReadVariableOp" + input: "bn5c_branch2b_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5c_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn5c_branch2b_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn5c_branch2b_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn5c_branch2b_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5c_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_47_1/Relu" + op: "Relu" + input: "bn5c_branch2b_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2b_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn5c_branch2b_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5c_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn5c_branch2b_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn5c_branch2b_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5c_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "training/SGD/gradients/res5c_branch2c_1/Conv2D_grad/ShapeN" + op: "ShapeN" + input: "activation_47_1/Relu" + input: "res5c_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "out_type" + value { + type: DT_INT32 + } + } +} +node { + name: "res5c_branch2c_1/Conv2D" + op: "Conv2D" + input: "activation_47_1/Relu" + input: "res5c_branch2c_1/Conv2D/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "bn5c_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5c_branch2b/moving_mean" + input: "bn5c_branch2b_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5c_branch2b/moving_variance" + input: "bn5c_branch2b_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "ConstantFolding/training/SGD/gradients/res5c_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + op: "Const" + input: "^training/SGD/gradients/res5c_branch2c_1/Conv2D_grad/ShapeN" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\001\000\000\000\001\000\000\000\000\002\000\000\000\010\000\000" + } + } + } +} +node { + name: "bn5c_branch2c_1/FusedBatchNorm" + op: "FusedBatchNorm" + input: "res5c_branch2c_1/Conv2D" + input: "bn5c_branch2c_1/ReadVariableOp" + input: "bn5c_branch2c_1/ReadVariableOp_1" + input: "bn5a_branch1_1/Const_1" + input: "bn5a_branch1_1/Const_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "bn5c_branch2c_1/AssignMovingAvg/sub_1" + op: "Sub" + input: "bn5c_branch2c_1/AssignMovingAvg/ReadVariableOp" + input: "bn5c_branch2c_1/FusedBatchNorm:1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5c_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn5c_branch2c_1/AssignMovingAvg_1/sub_1" + op: "Sub" + input: "bn5c_branch2c_1/AssignMovingAvg_1/ReadVariableOp" + input: "bn5c_branch2c_1/FusedBatchNorm:2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5c_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "add_15_1/add" + op: "Add" + input: "activation_45_1/Relu" + input: "bn5c_branch2c_1/FusedBatchNorm" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2c_1/AssignMovingAvg/mul" + op: "Mul" + input: "bn5c_branch2c_1/AssignMovingAvg/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5c_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + } + } + } +} +node { + name: "bn5c_branch2c_1/AssignMovingAvg_1/mul" + op: "Mul" + input: "bn5c_branch2c_1/AssignMovingAvg_1/sub_1" + input: "bn_conv1_1/AssignMovingAvg_1/sub" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@bn5c_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + } + } + } +} +node { + name: "activation_48_1/Relu" + op: "Relu" + input: "add_15_1/add" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5c_branch2c/moving_mean" + input: "bn5c_branch2c_1/AssignMovingAvg/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "bn5c_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + op: "AssignSubVariableOp" + input: "bn5c_branch2c/moving_variance" + input: "bn5c_branch2c_1/AssignMovingAvg_1/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "avg_pool_1/Mean" + op: "Mean" + input: "activation_48_1/Relu" + input: "avg_pool_1/Mean/reduction_indices" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "fc1000_1/MatMul" + op: "MatMul" + input: "avg_pool_1/Mean" + input: "fc1000_1/MatMul/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "transpose_a" + value { + b: false + } + } + attr { + key: "transpose_b" + value { + b: false + } + } +} +node { + name: "fc1000_1/BiasAdd" + op: "BiasAdd" + input: "fc1000_1/MatMul" + input: "fc1000_1/BiasAdd/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NHWC" + } + } +} +node { + name: "loss_1/activation_49_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits" + op: "SparseSoftmaxCrossEntropyWithLogits" + input: "fc1000_1/BiasAdd" + input: "loss_1/activation_49_loss/Cast" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tlabels" + value { + type: DT_INT64 + } + } +} +node { + name: "activation_49_1/Softmax" + op: "Softmax" + input: "fc1000_1/BiasAdd" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/loss_1/activation_49_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits_grad/mul" + op: "Mul" + input: "loss_1/activation_49_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits:1" + input: "training/SGD/gradients/loss_1/activation_49_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits_grad/ExpandDims" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "loss_1/activation_49_loss/Sum" + op: "Sum" + input: "loss_1/activation_49_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits" + input: "metrics_2/sparse_categorical_accuracy/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "metrics_2/sparse_categorical_accuracy/ArgMax" + op: "ArgMax" + input: "activation_49_1/Softmax" + input: "metrics_2/sparse_categorical_accuracy/ArgMax/dimension" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "output_type" + value { + type: DT_INT64 + } + } +} +node { + name: "training/SGD/gradients/fc1000_1/BiasAdd_grad/BiasAddGrad" + op: "BiasAddGrad" + input: "training/SGD/gradients/loss_1/activation_49_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits_grad/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NHWC" + } + } +} +node { + name: "training/SGD/gradients/fc1000_1/MatMul_grad/MatMul_1" + op: "MatMul" + input: "avg_pool_1/Mean" + input: "training/SGD/gradients/loss_1/activation_49_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits_grad/mul" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "transpose_a" + value { + b: true + } + } + attr { + key: "transpose_b" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/fc1000_1/MatMul_grad/MatMul" + op: "MatMul" + input: "training/SGD/gradients/loss_1/activation_49_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits_grad/mul" + input: "fc1000_1/MatMul/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "transpose_a" + value { + b: false + } + } + attr { + key: "transpose_b" + value { + b: true + } + } +} +node { + name: "loss_1/activation_49_loss/value" + op: "DivNoNan" + input: "loss_1/activation_49_loss/Sum" + input: "metrics_2/sparse_categorical_accuracy/Cast_2" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "metrics_2/sparse_categorical_accuracy/Cast" + op: "Cast" + input: "metrics_2/sparse_categorical_accuracy/ArgMax" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_INT64 + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/AddN" + op: "AddN" + input: "training/SGD/gradients/fc1000_1/BiasAdd_grad/BiasAddGrad" + input: "training/SGD/gradients/loss_1/fc1000/bias/Regularizer/Square_grad/Mul_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_1" + op: "AddN" + input: "training/SGD/gradients/fc1000_1/MatMul_grad/MatMul_1" + input: "training/SGD/gradients/loss_1/fc1000/kernel/Regularizer/Square_grad/Mul_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/avg_pool_1/Mean_grad/Reshape" + op: "Reshape" + input: "training/SGD/gradients/fc1000_1/MatMul_grad/MatMul" + input: "training/SGD/gradients/avg_pool_1/Mean_grad/DynamicStitch" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tshape" + value { + type: DT_INT32 + } + } +} +node { + name: "loss_1/add" + op: "Add" + input: "loss_1/ArithmeticOptimizer/HoistCommonFactor_Mul_AddN" + input: "loss_1/activation_49_loss/value" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "metrics_2/sparse_categorical_accuracy/Equal" + op: "Equal" + input: "metrics_2/sparse_categorical_accuracy/Cast" + input: "metrics_2/sparse_categorical_accuracy/Squeeze" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_fc1000/bias/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "fc1000/bias" + input: "training/SGD/fc1000/bias/momentum" + input: "training/SGD/SGD/update_fc1000/bias/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN" + input: "training/SGD/SGD/update_fc1000/bias/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_fc1000/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "fc1000/kernel" + input: "training/SGD/fc1000/kernel/momentum" + input: "training/SGD/SGD/update_fc1000/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_1" + input: "training/SGD/SGD/update_fc1000/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/avg_pool_1/Mean_grad/Tile" + op: "Tile" + input: "training/SGD/gradients/avg_pool_1/Mean_grad/Reshape" + input: "training/SGD/gradients/avg_pool_1/Mean_grad/floordiv" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tmultiples" + value { + type: DT_INT32 + } + } +} +node { + name: "metrics_2/sparse_categorical_accuracy/Cast_1" + op: "Cast" + input: "metrics_2/sparse_categorical_accuracy/Equal" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "DstT" + value { + type: DT_FLOAT + } + } + attr { + key: "SrcT" + value { + type: DT_BOOL + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/avg_pool_1/Mean_grad/truediv" + op: "Mul" + input: "ConstantFolding/training/SGD/gradients/avg_pool_1/Mean_grad/truediv_recip" + input: "training/SGD/gradients/avg_pool_1/Mean_grad/Tile" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "metrics_2/sparse_categorical_accuracy/Sum" + op: "Sum" + input: "metrics_2/sparse_categorical_accuracy/Cast_1" + input: "metrics_2/sparse_categorical_accuracy/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/activation_48_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/avg_pool_1/Mean_grad/truediv" + input: "activation_48_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_4" + } + } + } +} +node { + name: "metrics_2/sparse_categorical_accuracy/AssignAddVariableOp" + op: "AssignAddVariableOp" + input: "total_1" + input: "metrics_2/sparse_categorical_accuracy/Sum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/bn5c_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_48_1/Relu_grad/ReluGrad" + input: "res5c_branch2c_1/Conv2D" + input: "bn5c_branch2c_1/ReadVariableOp" + input: "bn5c_branch2c_1/FusedBatchNorm:3" + input: "bn5c_branch2c_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "metrics_2/sparse_categorical_accuracy/AssignAddVariableOp_1" + op: "AssignAddVariableOp" + input: "count_3" + input: "metrics_2/sparse_categorical_accuracy/Cast_2" + input: "^metrics_2/sparse_categorical_accuracy/AssignAddVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5c_branch2c/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5c_branch2c/beta" + input: "training/SGD/bn5c_branch2c/beta/momentum" + input: "training/SGD/SGD/update_bn5c_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn5c_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn5c_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn5c_branch2c/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5c_branch2c/gamma" + input: "training/SGD/bn5c_branch2c/gamma/momentum" + input: "training/SGD/SGD/update_bn5c_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn5c_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn5c_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res5c_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res5c_branch2c_1/Conv2D_grad/ShapeN" + input: "res5c_branch2c_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn5c_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res5c_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_47_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res5c_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn5c_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "metrics_2/sparse_categorical_accuracy/div_no_nan/ReadVariableOp" + op: "ReadVariableOp" + input: "total_1" + input: "^metrics_2/sparse_categorical_accuracy/AssignAddVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "metrics_2/sparse_categorical_accuracy/div_no_nan/ReadVariableOp_1" + op: "ReadVariableOp" + input: "count_3" + input: "^metrics_2/sparse_categorical_accuracy/AssignAddVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/activation_47_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res5c_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_47_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_2" + op: "AddN" + input: "training/SGD/gradients/loss_1/res5c_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res5c_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "metrics_2/sparse_categorical_accuracy/div_no_nan" + op: "DivNoNan" + input: "metrics_2/sparse_categorical_accuracy/div_no_nan/ReadVariableOp" + input: "metrics_2/sparse_categorical_accuracy/div_no_nan/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/bn5c_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_47_1/Relu_grad/ReluGrad" + input: "res5c_branch2b_1/Conv2D" + input: "bn5c_branch2b_1/ReadVariableOp" + input: "bn5c_branch2b_1/FusedBatchNorm:3" + input: "bn5c_branch2b_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res5c_branch2c/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res5c_branch2c/kernel" + input: "training/SGD/res5c_branch2c/kernel/momentum" + input: "training/SGD/SGD/update_res5c_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_2" + input: "training/SGD/SGD/update_res5c_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "metrics_2/sparse_categorical_accuracy/Identity" + op: "Identity" + input: "metrics_2/sparse_categorical_accuracy/div_no_nan" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_bn5c_branch2b/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5c_branch2b/beta" + input: "training/SGD/bn5c_branch2b/beta/momentum" + input: "training/SGD/SGD/update_bn5c_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn5c_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn5c_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn5c_branch2b/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5c_branch2b/gamma" + input: "training/SGD/bn5c_branch2b/gamma/momentum" + input: "training/SGD/SGD/update_bn5c_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn5c_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn5c_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res5c_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res5c_branch2b_1/Conv2D_grad/ShapeN" + input: "res5c_branch2b_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn5c_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res5c_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_46_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res5c_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn5c_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/activation_46_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res5c_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_46_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_3" + op: "AddN" + input: "training/SGD/gradients/loss_1/res5c_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res5c_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/bn5c_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_46_1/Relu_grad/ReluGrad" + input: "res5c_branch2a_1/Conv2D" + input: "bn5c_branch2a_1/ReadVariableOp" + input: "bn5c_branch2a_1/FusedBatchNorm:3" + input: "bn5c_branch2a_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res5c_branch2b/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res5c_branch2b/kernel" + input: "training/SGD/res5c_branch2b/kernel/momentum" + input: "training/SGD/SGD/update_res5c_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_3" + input: "training/SGD/SGD/update_res5c_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn5c_branch2a/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5c_branch2a/beta" + input: "training/SGD/bn5c_branch2a/beta/momentum" + input: "training/SGD/SGD/update_bn5c_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn5c_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn5c_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn5c_branch2a/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5c_branch2a/gamma" + input: "training/SGD/bn5c_branch2a/gamma/momentum" + input: "training/SGD/SGD/update_bn5c_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn5c_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn5c_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res5c_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/SGD/gradients/res5c_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res5c_branch2a_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn5c_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res5c_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_45_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res5c_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn5c_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/AddN_4" + op: "AddN" + input: "training/SGD/gradients/activation_48_1/Relu_grad/ReluGrad" + input: "training/SGD/gradients/res5c_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_5" + op: "AddN" + input: "training/SGD/gradients/loss_1/res5c_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res5c_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/activation_45_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/AddN_4" + input: "activation_45_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_8" + } + } + } +} +node { + name: "training/SGD/SGD/update_res5c_branch2a/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res5c_branch2a/kernel" + input: "training/SGD/res5c_branch2a/kernel/momentum" + input: "training/SGD/SGD/update_res5c_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_5" + input: "training/SGD/SGD/update_res5c_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/bn5b_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_45_1/Relu_grad/ReluGrad" + input: "res5b_branch2c_1/Conv2D" + input: "bn5b_branch2c_1/ReadVariableOp" + input: "bn5b_branch2c_1/FusedBatchNorm:3" + input: "bn5b_branch2c_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_bn5b_branch2c/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5b_branch2c/beta" + input: "training/SGD/bn5b_branch2c/beta/momentum" + input: "training/SGD/SGD/update_bn5b_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn5b_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn5b_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn5b_branch2c/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5b_branch2c/gamma" + input: "training/SGD/bn5b_branch2c/gamma/momentum" + input: "training/SGD/SGD/update_bn5b_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn5b_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn5b_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res5b_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res5b_branch2c_1/Conv2D_grad/ShapeN" + input: "res5b_branch2c_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn5b_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res5b_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_44_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res5b_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn5b_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/activation_44_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res5b_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_44_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_6" + op: "AddN" + input: "training/SGD/gradients/loss_1/res5b_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res5b_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/bn5b_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_44_1/Relu_grad/ReluGrad" + input: "res5b_branch2b_1/Conv2D" + input: "bn5b_branch2b_1/ReadVariableOp" + input: "bn5b_branch2b_1/FusedBatchNorm:3" + input: "bn5b_branch2b_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res5b_branch2c/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res5b_branch2c/kernel" + input: "training/SGD/res5b_branch2c/kernel/momentum" + input: "training/SGD/SGD/update_res5b_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_6" + input: "training/SGD/SGD/update_res5b_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn5b_branch2b/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5b_branch2b/beta" + input: "training/SGD/bn5b_branch2b/beta/momentum" + input: "training/SGD/SGD/update_bn5b_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn5b_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn5b_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn5b_branch2b/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5b_branch2b/gamma" + input: "training/SGD/bn5b_branch2b/gamma/momentum" + input: "training/SGD/SGD/update_bn5b_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn5b_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn5b_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res5b_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res5b_branch2b_1/Conv2D_grad/ShapeN" + input: "res5b_branch2b_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn5b_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res5b_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_43_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res5b_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn5b_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/activation_43_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res5b_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_43_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_7" + op: "AddN" + input: "training/SGD/gradients/loss_1/res5b_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res5b_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/bn5b_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_43_1/Relu_grad/ReluGrad" + input: "res5b_branch2a_1/Conv2D" + input: "bn5b_branch2a_1/ReadVariableOp" + input: "bn5b_branch2a_1/FusedBatchNorm:3" + input: "bn5b_branch2a_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res5b_branch2b/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res5b_branch2b/kernel" + input: "training/SGD/res5b_branch2b/kernel/momentum" + input: "training/SGD/SGD/update_res5b_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_7" + input: "training/SGD/SGD/update_res5b_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn5b_branch2a/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5b_branch2a/beta" + input: "training/SGD/bn5b_branch2a/beta/momentum" + input: "training/SGD/SGD/update_bn5b_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn5b_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn5b_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn5b_branch2a/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5b_branch2a/gamma" + input: "training/SGD/bn5b_branch2a/gamma/momentum" + input: "training/SGD/SGD/update_bn5b_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn5b_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn5b_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res5b_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "ConstantFolding/training/SGD/gradients/res5b_branch2a_1/Conv2D_grad/ShapeN-matshapes-0" + input: "res5b_branch2a_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn5b_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res5b_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_42_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res5b_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn5b_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/AddN_8" + op: "AddN" + input: "training/SGD/gradients/activation_45_1/Relu_grad/ReluGrad" + input: "training/SGD/gradients/res5b_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_9" + op: "AddN" + input: "training/SGD/gradients/loss_1/res5b_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res5b_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/activation_42_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/AddN_8" + input: "activation_42_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res5b_branch2a/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res5b_branch2a/kernel" + input: "training/SGD/res5b_branch2a/kernel/momentum" + input: "training/SGD/SGD/update_res5b_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_9" + input: "training/SGD/SGD/update_res5b_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/bn5a_branch1_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_42_1/Relu_grad/ReluGrad" + input: "res5a_branch1_1/Conv2D" + input: "bn5a_branch1_1/ReadVariableOp" + input: "bn5a_branch1_1/FusedBatchNorm:3" + input: "bn5a_branch1_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/bn5a_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_42_1/Relu_grad/ReluGrad" + input: "res5a_branch2c_1/Conv2D" + input: "bn5a_branch2c_1/ReadVariableOp" + input: "bn5a_branch2c_1/FusedBatchNorm:3" + input: "bn5a_branch2c_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_bn5a_branch1/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5a_branch1/beta" + input: "training/SGD/bn5a_branch1/beta/momentum" + input: "training/SGD/SGD/update_bn5a_branch1/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn5a_branch1_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn5a_branch1/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn5a_branch1/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5a_branch1/gamma" + input: "training/SGD/bn5a_branch1/gamma/momentum" + input: "training/SGD/SGD/update_bn5a_branch1/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn5a_branch1_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn5a_branch1/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res5a_branch1_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res5a_branch1_1/Conv2D_grad/ShapeN" + input: "res5a_branch1_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn5a_branch1_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_13" + } + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res5a_branch1_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_39_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res5a_branch1_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn5a_branch1_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_bn5a_branch2c/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5a_branch2c/beta" + input: "training/SGD/bn5a_branch2c/beta/momentum" + input: "training/SGD/SGD/update_bn5a_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn5a_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn5a_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn5a_branch2c/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5a_branch2c/gamma" + input: "training/SGD/bn5a_branch2c/gamma/momentum" + input: "training/SGD/SGD/update_bn5a_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn5a_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn5a_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res5a_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res5a_branch2c_1/Conv2D_grad/ShapeN" + input: "res5a_branch2c_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn5a_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res5a_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_41_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res5a_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn5a_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/AddN_11" + op: "AddN" + input: "training/SGD/gradients/loss_1/res5a_branch1/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res5a_branch1_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/activation_41_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res5a_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_41_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_10" + op: "AddN" + input: "training/SGD/gradients/loss_1/res5a_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res5a_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res5a_branch1/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res5a_branch1/kernel" + input: "training/SGD/res5a_branch1/kernel/momentum" + input: "training/SGD/SGD/update_res5a_branch1/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_11" + input: "training/SGD/SGD/update_res5a_branch1/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/bn5a_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_41_1/Relu_grad/ReluGrad" + input: "res5a_branch2b_1/Conv2D" + input: "bn5a_branch2b_1/ReadVariableOp" + input: "bn5a_branch2b_1/FusedBatchNorm:3" + input: "bn5a_branch2b_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res5a_branch2c/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res5a_branch2c/kernel" + input: "training/SGD/res5a_branch2c/kernel/momentum" + input: "training/SGD/SGD/update_res5a_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_10" + input: "training/SGD/SGD/update_res5a_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn5a_branch2b/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5a_branch2b/beta" + input: "training/SGD/bn5a_branch2b/beta/momentum" + input: "training/SGD/SGD/update_bn5a_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn5a_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn5a_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn5a_branch2b/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5a_branch2b/gamma" + input: "training/SGD/bn5a_branch2b/gamma/momentum" + input: "training/SGD/SGD/update_bn5a_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn5a_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn5a_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res5a_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res5a_branch2b_1/Conv2D_grad/ShapeN" + input: "res5a_branch2b_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn5a_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res5a_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_40_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res5a_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn5a_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/activation_40_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res5a_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_40_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_12" + op: "AddN" + input: "training/SGD/gradients/loss_1/res5a_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res5a_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/bn5a_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_40_1/Relu_grad/ReluGrad" + input: "res5a_branch2a_1/Conv2D" + input: "bn5a_branch2a_1/ReadVariableOp" + input: "bn5a_branch2a_1/FusedBatchNorm:3" + input: "bn5a_branch2a_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res5a_branch2b/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res5a_branch2b/kernel" + input: "training/SGD/res5a_branch2b/kernel/momentum" + input: "training/SGD/SGD/update_res5a_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_12" + input: "training/SGD/SGD/update_res5a_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn5a_branch2a/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5a_branch2a/beta" + input: "training/SGD/bn5a_branch2a/beta/momentum" + input: "training/SGD/SGD/update_bn5a_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn5a_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn5a_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn5a_branch2a/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn5a_branch2a/gamma" + input: "training/SGD/bn5a_branch2a/gamma/momentum" + input: "training/SGD/SGD/update_bn5a_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn5a_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn5a_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res5a_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res5a_branch2a_1/Conv2D_grad/ShapeN" + input: "res5a_branch2a_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn5a_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res5a_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_39_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res5a_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn5a_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/AddN_13" + op: "AddN" + input: "training/SGD/gradients/res5a_branch1_1/Conv2D_grad/Conv2DBackpropInput" + input: "training/SGD/gradients/res5a_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_14" + op: "AddN" + input: "training/SGD/gradients/loss_1/res5a_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res5a_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/activation_39_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/AddN_13" + input: "activation_39_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_17" + } + } + } +} +node { + name: "training/SGD/SGD/update_res5a_branch2a/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res5a_branch2a/kernel" + input: "training/SGD/res5a_branch2a/kernel/momentum" + input: "training/SGD/SGD/update_res5a_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_14" + input: "training/SGD/SGD/update_res5a_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/bn4f_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_39_1/Relu_grad/ReluGrad" + input: "res4f_branch2c_1/Conv2D" + input: "bn4f_branch2c_1/ReadVariableOp" + input: "bn4f_branch2c_1/FusedBatchNorm:3" + input: "bn4f_branch2c_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_bn4f_branch2c/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4f_branch2c/beta" + input: "training/SGD/bn4f_branch2c/beta/momentum" + input: "training/SGD/SGD/update_bn4f_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4f_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn4f_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn4f_branch2c/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4f_branch2c/gamma" + input: "training/SGD/bn4f_branch2c/gamma/momentum" + input: "training/SGD/SGD/update_bn4f_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4f_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn4f_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res4f_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res4f_branch2c_1/Conv2D_grad/ShapeN" + input: "res4f_branch2c_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn4f_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res4f_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_38_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res4f_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn4f_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/activation_38_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res4f_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_38_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_15" + op: "AddN" + input: "training/SGD/gradients/loss_1/res4f_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res4f_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/bn4f_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_38_1/Relu_grad/ReluGrad" + input: "res4f_branch2b_1/Conv2D" + input: "bn4f_branch2b_1/ReadVariableOp" + input: "bn4f_branch2b_1/FusedBatchNorm:3" + input: "bn4f_branch2b_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res4f_branch2c/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4f_branch2c/kernel" + input: "training/SGD/res4f_branch2c/kernel/momentum" + input: "training/SGD/SGD/update_res4f_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_15" + input: "training/SGD/SGD/update_res4f_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn4f_branch2b/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4f_branch2b/beta" + input: "training/SGD/bn4f_branch2b/beta/momentum" + input: "training/SGD/SGD/update_bn4f_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4f_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn4f_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn4f_branch2b/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4f_branch2b/gamma" + input: "training/SGD/bn4f_branch2b/gamma/momentum" + input: "training/SGD/SGD/update_bn4f_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4f_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn4f_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res4f_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res4f_branch2b_1/Conv2D_grad/ShapeN" + input: "res4f_branch2b_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn4f_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res4f_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_37_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res4f_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn4f_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/activation_37_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res4f_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_37_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_16" + op: "AddN" + input: "training/SGD/gradients/loss_1/res4f_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res4f_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/bn4f_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_37_1/Relu_grad/ReluGrad" + input: "res4f_branch2a_1/Conv2D" + input: "bn4f_branch2a_1/ReadVariableOp" + input: "bn4f_branch2a_1/FusedBatchNorm:3" + input: "bn4f_branch2a_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res4f_branch2b/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4f_branch2b/kernel" + input: "training/SGD/res4f_branch2b/kernel/momentum" + input: "training/SGD/SGD/update_res4f_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_16" + input: "training/SGD/SGD/update_res4f_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn4f_branch2a/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4f_branch2a/beta" + input: "training/SGD/bn4f_branch2a/beta/momentum" + input: "training/SGD/SGD/update_bn4f_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4f_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn4f_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn4f_branch2a/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4f_branch2a/gamma" + input: "training/SGD/bn4f_branch2a/gamma/momentum" + input: "training/SGD/SGD/update_bn4f_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4f_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn4f_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res4f_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res4f_branch2a_1/Conv2D_grad/ShapeN" + input: "res4f_branch2a_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn4f_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res4f_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_36_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res4f_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn4f_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/AddN_17" + op: "AddN" + input: "training/SGD/gradients/activation_39_1/Relu_grad/ReluGrad" + input: "training/SGD/gradients/res4f_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_18" + op: "AddN" + input: "training/SGD/gradients/loss_1/res4f_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res4f_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/activation_36_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/AddN_17" + input: "activation_36_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_21" + } + } + } +} +node { + name: "training/SGD/SGD/update_res4f_branch2a/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4f_branch2a/kernel" + input: "training/SGD/res4f_branch2a/kernel/momentum" + input: "training/SGD/SGD/update_res4f_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_18" + input: "training/SGD/SGD/update_res4f_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/bn4e_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_36_1/Relu_grad/ReluGrad" + input: "res4e_branch2c_1/Conv2D" + input: "bn4e_branch2c_1/ReadVariableOp" + input: "bn4e_branch2c_1/FusedBatchNorm:3" + input: "bn4e_branch2c_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_bn4e_branch2c/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4e_branch2c/beta" + input: "training/SGD/bn4e_branch2c/beta/momentum" + input: "training/SGD/SGD/update_bn4e_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4e_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn4e_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn4e_branch2c/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4e_branch2c/gamma" + input: "training/SGD/bn4e_branch2c/gamma/momentum" + input: "training/SGD/SGD/update_bn4e_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4e_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn4e_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res4e_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res4e_branch2c_1/Conv2D_grad/ShapeN" + input: "res4e_branch2c_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn4e_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res4e_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_35_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res4e_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn4e_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/activation_35_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res4e_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_35_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_19" + op: "AddN" + input: "training/SGD/gradients/loss_1/res4e_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res4e_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/bn4e_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_35_1/Relu_grad/ReluGrad" + input: "res4e_branch2b_1/Conv2D" + input: "bn4e_branch2b_1/ReadVariableOp" + input: "bn4e_branch2b_1/FusedBatchNorm:3" + input: "bn4e_branch2b_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res4e_branch2c/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4e_branch2c/kernel" + input: "training/SGD/res4e_branch2c/kernel/momentum" + input: "training/SGD/SGD/update_res4e_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_19" + input: "training/SGD/SGD/update_res4e_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn4e_branch2b/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4e_branch2b/beta" + input: "training/SGD/bn4e_branch2b/beta/momentum" + input: "training/SGD/SGD/update_bn4e_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4e_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn4e_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn4e_branch2b/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4e_branch2b/gamma" + input: "training/SGD/bn4e_branch2b/gamma/momentum" + input: "training/SGD/SGD/update_bn4e_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4e_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn4e_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res4e_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res4e_branch2b_1/Conv2D_grad/ShapeN" + input: "res4e_branch2b_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn4e_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res4e_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_34_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res4e_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn4e_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/activation_34_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res4e_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_34_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_20" + op: "AddN" + input: "training/SGD/gradients/loss_1/res4e_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res4e_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/bn4e_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_34_1/Relu_grad/ReluGrad" + input: "res4e_branch2a_1/Conv2D" + input: "bn4e_branch2a_1/ReadVariableOp" + input: "bn4e_branch2a_1/FusedBatchNorm:3" + input: "bn4e_branch2a_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res4e_branch2b/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4e_branch2b/kernel" + input: "training/SGD/res4e_branch2b/kernel/momentum" + input: "training/SGD/SGD/update_res4e_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_20" + input: "training/SGD/SGD/update_res4e_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn4e_branch2a/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4e_branch2a/beta" + input: "training/SGD/bn4e_branch2a/beta/momentum" + input: "training/SGD/SGD/update_bn4e_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4e_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn4e_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn4e_branch2a/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4e_branch2a/gamma" + input: "training/SGD/bn4e_branch2a/gamma/momentum" + input: "training/SGD/SGD/update_bn4e_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4e_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn4e_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res4e_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res4e_branch2a_1/Conv2D_grad/ShapeN" + input: "res4e_branch2a_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn4e_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res4e_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_33_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res4e_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn4e_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/AddN_21" + op: "AddN" + input: "training/SGD/gradients/activation_36_1/Relu_grad/ReluGrad" + input: "training/SGD/gradients/res4e_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_22" + op: "AddN" + input: "training/SGD/gradients/loss_1/res4e_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res4e_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/activation_33_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/AddN_21" + input: "activation_33_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_25" + } + } + } +} +node { + name: "training/SGD/SGD/update_res4e_branch2a/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4e_branch2a/kernel" + input: "training/SGD/res4e_branch2a/kernel/momentum" + input: "training/SGD/SGD/update_res4e_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_22" + input: "training/SGD/SGD/update_res4e_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/bn4d_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_33_1/Relu_grad/ReluGrad" + input: "res4d_branch2c_1/Conv2D" + input: "bn4d_branch2c_1/ReadVariableOp" + input: "bn4d_branch2c_1/FusedBatchNorm:3" + input: "bn4d_branch2c_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_bn4d_branch2c/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4d_branch2c/beta" + input: "training/SGD/bn4d_branch2c/beta/momentum" + input: "training/SGD/SGD/update_bn4d_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4d_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn4d_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn4d_branch2c/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4d_branch2c/gamma" + input: "training/SGD/bn4d_branch2c/gamma/momentum" + input: "training/SGD/SGD/update_bn4d_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4d_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn4d_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res4d_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res4d_branch2c_1/Conv2D_grad/ShapeN" + input: "res4d_branch2c_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn4d_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res4d_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_32_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res4d_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn4d_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/activation_32_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res4d_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_32_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_23" + op: "AddN" + input: "training/SGD/gradients/loss_1/res4d_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res4d_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/bn4d_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_32_1/Relu_grad/ReluGrad" + input: "res4d_branch2b_1/Conv2D" + input: "bn4d_branch2b_1/ReadVariableOp" + input: "bn4d_branch2b_1/FusedBatchNorm:3" + input: "bn4d_branch2b_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res4d_branch2c/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4d_branch2c/kernel" + input: "training/SGD/res4d_branch2c/kernel/momentum" + input: "training/SGD/SGD/update_res4d_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_23" + input: "training/SGD/SGD/update_res4d_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn4d_branch2b/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4d_branch2b/beta" + input: "training/SGD/bn4d_branch2b/beta/momentum" + input: "training/SGD/SGD/update_bn4d_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4d_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn4d_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn4d_branch2b/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4d_branch2b/gamma" + input: "training/SGD/bn4d_branch2b/gamma/momentum" + input: "training/SGD/SGD/update_bn4d_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4d_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn4d_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res4d_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res4d_branch2b_1/Conv2D_grad/ShapeN" + input: "res4d_branch2b_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn4d_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res4d_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_31_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res4d_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn4d_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/activation_31_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res4d_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_31_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_24" + op: "AddN" + input: "training/SGD/gradients/loss_1/res4d_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res4d_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/bn4d_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_31_1/Relu_grad/ReluGrad" + input: "res4d_branch2a_1/Conv2D" + input: "bn4d_branch2a_1/ReadVariableOp" + input: "bn4d_branch2a_1/FusedBatchNorm:3" + input: "bn4d_branch2a_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res4d_branch2b/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4d_branch2b/kernel" + input: "training/SGD/res4d_branch2b/kernel/momentum" + input: "training/SGD/SGD/update_res4d_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_24" + input: "training/SGD/SGD/update_res4d_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn4d_branch2a/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4d_branch2a/beta" + input: "training/SGD/bn4d_branch2a/beta/momentum" + input: "training/SGD/SGD/update_bn4d_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4d_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn4d_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn4d_branch2a/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4d_branch2a/gamma" + input: "training/SGD/bn4d_branch2a/gamma/momentum" + input: "training/SGD/SGD/update_bn4d_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4d_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn4d_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res4d_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res4d_branch2a_1/Conv2D_grad/ShapeN" + input: "res4d_branch2a_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn4d_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res4d_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_30_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res4d_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn4d_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/AddN_25" + op: "AddN" + input: "training/SGD/gradients/activation_33_1/Relu_grad/ReluGrad" + input: "training/SGD/gradients/res4d_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_26" + op: "AddN" + input: "training/SGD/gradients/loss_1/res4d_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res4d_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/activation_30_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/AddN_25" + input: "activation_30_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_29" + } + } + } +} +node { + name: "training/SGD/SGD/update_res4d_branch2a/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4d_branch2a/kernel" + input: "training/SGD/res4d_branch2a/kernel/momentum" + input: "training/SGD/SGD/update_res4d_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_26" + input: "training/SGD/SGD/update_res4d_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/bn4c_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_30_1/Relu_grad/ReluGrad" + input: "res4c_branch2c_1/Conv2D" + input: "bn4c_branch2c_1/ReadVariableOp" + input: "bn4c_branch2c_1/FusedBatchNorm:3" + input: "bn4c_branch2c_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_bn4c_branch2c/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4c_branch2c/beta" + input: "training/SGD/bn4c_branch2c/beta/momentum" + input: "training/SGD/SGD/update_bn4c_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4c_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn4c_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn4c_branch2c/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4c_branch2c/gamma" + input: "training/SGD/bn4c_branch2c/gamma/momentum" + input: "training/SGD/SGD/update_bn4c_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4c_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn4c_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res4c_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res4c_branch2c_1/Conv2D_grad/ShapeN" + input: "res4c_branch2c_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn4c_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res4c_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_29_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res4c_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn4c_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/activation_29_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res4c_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_29_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_27" + op: "AddN" + input: "training/SGD/gradients/loss_1/res4c_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res4c_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/bn4c_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_29_1/Relu_grad/ReluGrad" + input: "res4c_branch2b_1/Conv2D" + input: "bn4c_branch2b_1/ReadVariableOp" + input: "bn4c_branch2b_1/FusedBatchNorm:3" + input: "bn4c_branch2b_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res4c_branch2c/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4c_branch2c/kernel" + input: "training/SGD/res4c_branch2c/kernel/momentum" + input: "training/SGD/SGD/update_res4c_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_27" + input: "training/SGD/SGD/update_res4c_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn4c_branch2b/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4c_branch2b/beta" + input: "training/SGD/bn4c_branch2b/beta/momentum" + input: "training/SGD/SGD/update_bn4c_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4c_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn4c_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn4c_branch2b/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4c_branch2b/gamma" + input: "training/SGD/bn4c_branch2b/gamma/momentum" + input: "training/SGD/SGD/update_bn4c_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4c_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn4c_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res4c_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res4c_branch2b_1/Conv2D_grad/ShapeN" + input: "res4c_branch2b_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn4c_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res4c_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_28_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res4c_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn4c_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/activation_28_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res4c_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_28_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_28" + op: "AddN" + input: "training/SGD/gradients/loss_1/res4c_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res4c_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/bn4c_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_28_1/Relu_grad/ReluGrad" + input: "res4c_branch2a_1/Conv2D" + input: "bn4c_branch2a_1/ReadVariableOp" + input: "bn4c_branch2a_1/FusedBatchNorm:3" + input: "bn4c_branch2a_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res4c_branch2b/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4c_branch2b/kernel" + input: "training/SGD/res4c_branch2b/kernel/momentum" + input: "training/SGD/SGD/update_res4c_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_28" + input: "training/SGD/SGD/update_res4c_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn4c_branch2a/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4c_branch2a/beta" + input: "training/SGD/bn4c_branch2a/beta/momentum" + input: "training/SGD/SGD/update_bn4c_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4c_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn4c_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn4c_branch2a/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4c_branch2a/gamma" + input: "training/SGD/bn4c_branch2a/gamma/momentum" + input: "training/SGD/SGD/update_bn4c_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4c_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn4c_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res4c_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res4c_branch2a_1/Conv2D_grad/ShapeN" + input: "res4c_branch2a_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn4c_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res4c_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_27_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res4c_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn4c_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/AddN_29" + op: "AddN" + input: "training/SGD/gradients/activation_30_1/Relu_grad/ReluGrad" + input: "training/SGD/gradients/res4c_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_30" + op: "AddN" + input: "training/SGD/gradients/loss_1/res4c_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res4c_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/activation_27_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/AddN_29" + input: "activation_27_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_33" + } + } + } +} +node { + name: "training/SGD/SGD/update_res4c_branch2a/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4c_branch2a/kernel" + input: "training/SGD/res4c_branch2a/kernel/momentum" + input: "training/SGD/SGD/update_res4c_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_30" + input: "training/SGD/SGD/update_res4c_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/bn4b_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_27_1/Relu_grad/ReluGrad" + input: "res4b_branch2c_1/Conv2D" + input: "bn4b_branch2c_1/ReadVariableOp" + input: "bn4b_branch2c_1/FusedBatchNorm:3" + input: "bn4b_branch2c_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_bn4b_branch2c/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4b_branch2c/beta" + input: "training/SGD/bn4b_branch2c/beta/momentum" + input: "training/SGD/SGD/update_bn4b_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4b_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn4b_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn4b_branch2c/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4b_branch2c/gamma" + input: "training/SGD/bn4b_branch2c/gamma/momentum" + input: "training/SGD/SGD/update_bn4b_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4b_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn4b_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res4b_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res4b_branch2c_1/Conv2D_grad/ShapeN" + input: "res4b_branch2c_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn4b_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res4b_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_26_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res4b_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn4b_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/activation_26_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res4b_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_26_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_31" + op: "AddN" + input: "training/SGD/gradients/loss_1/res4b_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res4b_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/bn4b_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_26_1/Relu_grad/ReluGrad" + input: "res4b_branch2b_1/Conv2D" + input: "bn4b_branch2b_1/ReadVariableOp" + input: "bn4b_branch2b_1/FusedBatchNorm:3" + input: "bn4b_branch2b_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res4b_branch2c/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4b_branch2c/kernel" + input: "training/SGD/res4b_branch2c/kernel/momentum" + input: "training/SGD/SGD/update_res4b_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_31" + input: "training/SGD/SGD/update_res4b_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn4b_branch2b/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4b_branch2b/beta" + input: "training/SGD/bn4b_branch2b/beta/momentum" + input: "training/SGD/SGD/update_bn4b_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4b_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn4b_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn4b_branch2b/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4b_branch2b/gamma" + input: "training/SGD/bn4b_branch2b/gamma/momentum" + input: "training/SGD/SGD/update_bn4b_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4b_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn4b_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res4b_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res4b_branch2b_1/Conv2D_grad/ShapeN" + input: "res4b_branch2b_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn4b_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res4b_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_25_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res4b_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn4b_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/activation_25_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res4b_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_25_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_32" + op: "AddN" + input: "training/SGD/gradients/loss_1/res4b_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res4b_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/bn4b_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_25_1/Relu_grad/ReluGrad" + input: "res4b_branch2a_1/Conv2D" + input: "bn4b_branch2a_1/ReadVariableOp" + input: "bn4b_branch2a_1/FusedBatchNorm:3" + input: "bn4b_branch2a_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res4b_branch2b/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4b_branch2b/kernel" + input: "training/SGD/res4b_branch2b/kernel/momentum" + input: "training/SGD/SGD/update_res4b_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_32" + input: "training/SGD/SGD/update_res4b_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn4b_branch2a/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4b_branch2a/beta" + input: "training/SGD/bn4b_branch2a/beta/momentum" + input: "training/SGD/SGD/update_bn4b_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4b_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn4b_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn4b_branch2a/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4b_branch2a/gamma" + input: "training/SGD/bn4b_branch2a/gamma/momentum" + input: "training/SGD/SGD/update_bn4b_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4b_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn4b_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res4b_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res4b_branch2a_1/Conv2D_grad/ShapeN" + input: "res4b_branch2a_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn4b_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res4b_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_24_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res4b_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn4b_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/AddN_33" + op: "AddN" + input: "training/SGD/gradients/activation_27_1/Relu_grad/ReluGrad" + input: "training/SGD/gradients/res4b_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_34" + op: "AddN" + input: "training/SGD/gradients/loss_1/res4b_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res4b_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/activation_24_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/AddN_33" + input: "activation_24_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4b_branch2a/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4b_branch2a/kernel" + input: "training/SGD/res4b_branch2a/kernel/momentum" + input: "training/SGD/SGD/update_res4b_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_34" + input: "training/SGD/SGD/update_res4b_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/bn4a_branch1_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_24_1/Relu_grad/ReluGrad" + input: "res4a_branch1_1/Conv2D" + input: "bn4a_branch1_1/ReadVariableOp" + input: "bn4a_branch1_1/FusedBatchNorm:3" + input: "bn4a_branch1_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/bn4a_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_24_1/Relu_grad/ReluGrad" + input: "res4a_branch2c_1/Conv2D" + input: "bn4a_branch2c_1/ReadVariableOp" + input: "bn4a_branch2c_1/FusedBatchNorm:3" + input: "bn4a_branch2c_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_bn4a_branch1/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4a_branch1/beta" + input: "training/SGD/bn4a_branch1/beta/momentum" + input: "training/SGD/SGD/update_bn4a_branch1/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4a_branch1_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn4a_branch1/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn4a_branch1/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4a_branch1/gamma" + input: "training/SGD/bn4a_branch1/gamma/momentum" + input: "training/SGD/SGD/update_bn4a_branch1/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4a_branch1_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn4a_branch1/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res4a_branch1_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res4a_branch1_1/Conv2D_grad/ShapeN" + input: "res4a_branch1_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn4a_branch1_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_38" + } + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res4a_branch1_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_21_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res4a_branch1_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn4a_branch1_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_bn4a_branch2c/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4a_branch2c/beta" + input: "training/SGD/bn4a_branch2c/beta/momentum" + input: "training/SGD/SGD/update_bn4a_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4a_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn4a_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn4a_branch2c/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4a_branch2c/gamma" + input: "training/SGD/bn4a_branch2c/gamma/momentum" + input: "training/SGD/SGD/update_bn4a_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4a_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn4a_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res4a_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res4a_branch2c_1/Conv2D_grad/ShapeN" + input: "res4a_branch2c_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn4a_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res4a_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_23_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res4a_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn4a_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/AddN_36" + op: "AddN" + input: "training/SGD/gradients/loss_1/res4a_branch1/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res4a_branch1_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/activation_23_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res4a_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_23_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_35" + op: "AddN" + input: "training/SGD/gradients/loss_1/res4a_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res4a_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res4a_branch1/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4a_branch1/kernel" + input: "training/SGD/res4a_branch1/kernel/momentum" + input: "training/SGD/SGD/update_res4a_branch1/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_36" + input: "training/SGD/SGD/update_res4a_branch1/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/bn4a_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_23_1/Relu_grad/ReluGrad" + input: "res4a_branch2b_1/Conv2D" + input: "bn4a_branch2b_1/ReadVariableOp" + input: "bn4a_branch2b_1/FusedBatchNorm:3" + input: "bn4a_branch2b_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res4a_branch2c/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4a_branch2c/kernel" + input: "training/SGD/res4a_branch2c/kernel/momentum" + input: "training/SGD/SGD/update_res4a_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_35" + input: "training/SGD/SGD/update_res4a_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn4a_branch2b/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4a_branch2b/beta" + input: "training/SGD/bn4a_branch2b/beta/momentum" + input: "training/SGD/SGD/update_bn4a_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4a_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn4a_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn4a_branch2b/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4a_branch2b/gamma" + input: "training/SGD/bn4a_branch2b/gamma/momentum" + input: "training/SGD/SGD/update_bn4a_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4a_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn4a_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res4a_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res4a_branch2b_1/Conv2D_grad/ShapeN" + input: "res4a_branch2b_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn4a_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res4a_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_22_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res4a_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn4a_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/activation_22_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res4a_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_22_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_37" + op: "AddN" + input: "training/SGD/gradients/loss_1/res4a_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res4a_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/bn4a_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_22_1/Relu_grad/ReluGrad" + input: "res4a_branch2a_1/Conv2D" + input: "bn4a_branch2a_1/ReadVariableOp" + input: "bn4a_branch2a_1/FusedBatchNorm:3" + input: "bn4a_branch2a_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res4a_branch2b/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4a_branch2b/kernel" + input: "training/SGD/res4a_branch2b/kernel/momentum" + input: "training/SGD/SGD/update_res4a_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_37" + input: "training/SGD/SGD/update_res4a_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn4a_branch2a/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4a_branch2a/beta" + input: "training/SGD/bn4a_branch2a/beta/momentum" + input: "training/SGD/SGD/update_bn4a_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4a_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn4a_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn4a_branch2a/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn4a_branch2a/gamma" + input: "training/SGD/bn4a_branch2a/gamma/momentum" + input: "training/SGD/SGD/update_bn4a_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn4a_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn4a_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res4a_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res4a_branch2a_1/Conv2D_grad/ShapeN" + input: "res4a_branch2a_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn4a_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res4a_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_21_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res4a_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn4a_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/AddN_38" + op: "AddN" + input: "training/SGD/gradients/res4a_branch1_1/Conv2D_grad/Conv2DBackpropInput" + input: "training/SGD/gradients/res4a_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_39" + op: "AddN" + input: "training/SGD/gradients/loss_1/res4a_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res4a_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/activation_21_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/AddN_38" + input: "activation_21_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_42" + } + } + } +} +node { + name: "training/SGD/SGD/update_res4a_branch2a/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res4a_branch2a/kernel" + input: "training/SGD/res4a_branch2a/kernel/momentum" + input: "training/SGD/SGD/update_res4a_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_39" + input: "training/SGD/SGD/update_res4a_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/bn3d_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_21_1/Relu_grad/ReluGrad" + input: "res3d_branch2c_1/Conv2D" + input: "bn3d_branch2c_1/ReadVariableOp" + input: "bn3d_branch2c_1/FusedBatchNorm:3" + input: "bn3d_branch2c_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_bn3d_branch2c/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3d_branch2c/beta" + input: "training/SGD/bn3d_branch2c/beta/momentum" + input: "training/SGD/SGD/update_bn3d_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn3d_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn3d_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn3d_branch2c/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3d_branch2c/gamma" + input: "training/SGD/bn3d_branch2c/gamma/momentum" + input: "training/SGD/SGD/update_bn3d_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn3d_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn3d_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res3d_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res3d_branch2c_1/Conv2D_grad/ShapeN" + input: "res3d_branch2c_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn3d_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res3d_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_20_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res3d_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn3d_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/activation_20_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res3d_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_20_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_40" + op: "AddN" + input: "training/SGD/gradients/loss_1/res3d_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res3d_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/bn3d_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_20_1/Relu_grad/ReluGrad" + input: "res3d_branch2b_1/Conv2D" + input: "bn3d_branch2b_1/ReadVariableOp" + input: "bn3d_branch2b_1/FusedBatchNorm:3" + input: "bn3d_branch2b_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res3d_branch2c/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res3d_branch2c/kernel" + input: "training/SGD/res3d_branch2c/kernel/momentum" + input: "training/SGD/SGD/update_res3d_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_40" + input: "training/SGD/SGD/update_res3d_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn3d_branch2b/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3d_branch2b/beta" + input: "training/SGD/bn3d_branch2b/beta/momentum" + input: "training/SGD/SGD/update_bn3d_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn3d_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn3d_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn3d_branch2b/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3d_branch2b/gamma" + input: "training/SGD/bn3d_branch2b/gamma/momentum" + input: "training/SGD/SGD/update_bn3d_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn3d_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn3d_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res3d_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res3d_branch2b_1/Conv2D_grad/ShapeN" + input: "res3d_branch2b_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn3d_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res3d_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_19_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res3d_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn3d_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/activation_19_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res3d_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_19_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_41" + op: "AddN" + input: "training/SGD/gradients/loss_1/res3d_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res3d_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/bn3d_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_19_1/Relu_grad/ReluGrad" + input: "res3d_branch2a_1/Conv2D" + input: "bn3d_branch2a_1/ReadVariableOp" + input: "bn3d_branch2a_1/FusedBatchNorm:3" + input: "bn3d_branch2a_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res3d_branch2b/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res3d_branch2b/kernel" + input: "training/SGD/res3d_branch2b/kernel/momentum" + input: "training/SGD/SGD/update_res3d_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_41" + input: "training/SGD/SGD/update_res3d_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn3d_branch2a/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3d_branch2a/beta" + input: "training/SGD/bn3d_branch2a/beta/momentum" + input: "training/SGD/SGD/update_bn3d_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn3d_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn3d_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn3d_branch2a/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3d_branch2a/gamma" + input: "training/SGD/bn3d_branch2a/gamma/momentum" + input: "training/SGD/SGD/update_bn3d_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn3d_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn3d_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res3d_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res3d_branch2a_1/Conv2D_grad/ShapeN" + input: "res3d_branch2a_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn3d_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res3d_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_18_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res3d_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn3d_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/AddN_42" + op: "AddN" + input: "training/SGD/gradients/activation_21_1/Relu_grad/ReluGrad" + input: "training/SGD/gradients/res3d_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_43" + op: "AddN" + input: "training/SGD/gradients/loss_1/res3d_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res3d_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/activation_18_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/AddN_42" + input: "activation_18_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_46" + } + } + } +} +node { + name: "training/SGD/SGD/update_res3d_branch2a/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res3d_branch2a/kernel" + input: "training/SGD/res3d_branch2a/kernel/momentum" + input: "training/SGD/SGD/update_res3d_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_43" + input: "training/SGD/SGD/update_res3d_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/bn3c_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_18_1/Relu_grad/ReluGrad" + input: "res3c_branch2c_1/Conv2D" + input: "bn3c_branch2c_1/ReadVariableOp" + input: "bn3c_branch2c_1/FusedBatchNorm:3" + input: "bn3c_branch2c_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_bn3c_branch2c/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3c_branch2c/beta" + input: "training/SGD/bn3c_branch2c/beta/momentum" + input: "training/SGD/SGD/update_bn3c_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn3c_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn3c_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn3c_branch2c/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3c_branch2c/gamma" + input: "training/SGD/bn3c_branch2c/gamma/momentum" + input: "training/SGD/SGD/update_bn3c_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn3c_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn3c_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res3c_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res3c_branch2c_1/Conv2D_grad/ShapeN" + input: "res3c_branch2c_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn3c_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res3c_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_17_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res3c_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn3c_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/activation_17_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res3c_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_17_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_44" + op: "AddN" + input: "training/SGD/gradients/loss_1/res3c_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res3c_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/bn3c_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_17_1/Relu_grad/ReluGrad" + input: "res3c_branch2b_1/Conv2D" + input: "bn3c_branch2b_1/ReadVariableOp" + input: "bn3c_branch2b_1/FusedBatchNorm:3" + input: "bn3c_branch2b_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res3c_branch2c/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res3c_branch2c/kernel" + input: "training/SGD/res3c_branch2c/kernel/momentum" + input: "training/SGD/SGD/update_res3c_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_44" + input: "training/SGD/SGD/update_res3c_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn3c_branch2b/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3c_branch2b/beta" + input: "training/SGD/bn3c_branch2b/beta/momentum" + input: "training/SGD/SGD/update_bn3c_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn3c_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn3c_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn3c_branch2b/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3c_branch2b/gamma" + input: "training/SGD/bn3c_branch2b/gamma/momentum" + input: "training/SGD/SGD/update_bn3c_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn3c_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn3c_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res3c_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res3c_branch2b_1/Conv2D_grad/ShapeN" + input: "res3c_branch2b_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn3c_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res3c_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_16_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res3c_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn3c_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/activation_16_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res3c_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_16_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_45" + op: "AddN" + input: "training/SGD/gradients/loss_1/res3c_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res3c_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/bn3c_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_16_1/Relu_grad/ReluGrad" + input: "res3c_branch2a_1/Conv2D" + input: "bn3c_branch2a_1/ReadVariableOp" + input: "bn3c_branch2a_1/FusedBatchNorm:3" + input: "bn3c_branch2a_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res3c_branch2b/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res3c_branch2b/kernel" + input: "training/SGD/res3c_branch2b/kernel/momentum" + input: "training/SGD/SGD/update_res3c_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_45" + input: "training/SGD/SGD/update_res3c_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn3c_branch2a/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3c_branch2a/beta" + input: "training/SGD/bn3c_branch2a/beta/momentum" + input: "training/SGD/SGD/update_bn3c_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn3c_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn3c_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn3c_branch2a/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3c_branch2a/gamma" + input: "training/SGD/bn3c_branch2a/gamma/momentum" + input: "training/SGD/SGD/update_bn3c_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn3c_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn3c_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res3c_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res3c_branch2a_1/Conv2D_grad/ShapeN" + input: "res3c_branch2a_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn3c_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res3c_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_15_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res3c_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn3c_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/AddN_46" + op: "AddN" + input: "training/SGD/gradients/activation_18_1/Relu_grad/ReluGrad" + input: "training/SGD/gradients/res3c_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_47" + op: "AddN" + input: "training/SGD/gradients/loss_1/res3c_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res3c_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/activation_15_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/AddN_46" + input: "activation_15_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_50" + } + } + } +} +node { + name: "training/SGD/SGD/update_res3c_branch2a/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res3c_branch2a/kernel" + input: "training/SGD/res3c_branch2a/kernel/momentum" + input: "training/SGD/SGD/update_res3c_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_47" + input: "training/SGD/SGD/update_res3c_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/bn3b_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_15_1/Relu_grad/ReluGrad" + input: "res3b_branch2c_1/Conv2D" + input: "bn3b_branch2c_1/ReadVariableOp" + input: "bn3b_branch2c_1/FusedBatchNorm:3" + input: "bn3b_branch2c_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_bn3b_branch2c/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3b_branch2c/beta" + input: "training/SGD/bn3b_branch2c/beta/momentum" + input: "training/SGD/SGD/update_bn3b_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn3b_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn3b_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn3b_branch2c/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3b_branch2c/gamma" + input: "training/SGD/bn3b_branch2c/gamma/momentum" + input: "training/SGD/SGD/update_bn3b_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn3b_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn3b_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res3b_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res3b_branch2c_1/Conv2D_grad/ShapeN" + input: "res3b_branch2c_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn3b_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res3b_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_14_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res3b_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn3b_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/activation_14_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res3b_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_14_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_48" + op: "AddN" + input: "training/SGD/gradients/loss_1/res3b_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res3b_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/bn3b_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_14_1/Relu_grad/ReluGrad" + input: "res3b_branch2b_1/Conv2D" + input: "bn3b_branch2b_1/ReadVariableOp" + input: "bn3b_branch2b_1/FusedBatchNorm:3" + input: "bn3b_branch2b_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res3b_branch2c/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res3b_branch2c/kernel" + input: "training/SGD/res3b_branch2c/kernel/momentum" + input: "training/SGD/SGD/update_res3b_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_48" + input: "training/SGD/SGD/update_res3b_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn3b_branch2b/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3b_branch2b/beta" + input: "training/SGD/bn3b_branch2b/beta/momentum" + input: "training/SGD/SGD/update_bn3b_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn3b_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn3b_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn3b_branch2b/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3b_branch2b/gamma" + input: "training/SGD/bn3b_branch2b/gamma/momentum" + input: "training/SGD/SGD/update_bn3b_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn3b_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn3b_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res3b_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res3b_branch2b_1/Conv2D_grad/ShapeN" + input: "res3b_branch2b_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn3b_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res3b_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_13_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res3b_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn3b_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/activation_13_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res3b_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_13_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_49" + op: "AddN" + input: "training/SGD/gradients/loss_1/res3b_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res3b_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/bn3b_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_13_1/Relu_grad/ReluGrad" + input: "res3b_branch2a_1/Conv2D" + input: "bn3b_branch2a_1/ReadVariableOp" + input: "bn3b_branch2a_1/FusedBatchNorm:3" + input: "bn3b_branch2a_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res3b_branch2b/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res3b_branch2b/kernel" + input: "training/SGD/res3b_branch2b/kernel/momentum" + input: "training/SGD/SGD/update_res3b_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_49" + input: "training/SGD/SGD/update_res3b_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn3b_branch2a/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3b_branch2a/beta" + input: "training/SGD/bn3b_branch2a/beta/momentum" + input: "training/SGD/SGD/update_bn3b_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn3b_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn3b_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn3b_branch2a/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3b_branch2a/gamma" + input: "training/SGD/bn3b_branch2a/gamma/momentum" + input: "training/SGD/SGD/update_bn3b_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn3b_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn3b_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res3b_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res3b_branch2a_1/Conv2D_grad/ShapeN" + input: "res3b_branch2a_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn3b_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res3b_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_12_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res3b_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn3b_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/AddN_50" + op: "AddN" + input: "training/SGD/gradients/activation_15_1/Relu_grad/ReluGrad" + input: "training/SGD/gradients/res3b_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_51" + op: "AddN" + input: "training/SGD/gradients/loss_1/res3b_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res3b_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/activation_12_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/AddN_50" + input: "activation_12_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res3b_branch2a/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res3b_branch2a/kernel" + input: "training/SGD/res3b_branch2a/kernel/momentum" + input: "training/SGD/SGD/update_res3b_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_51" + input: "training/SGD/SGD/update_res3b_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/bn3a_branch1_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_12_1/Relu_grad/ReluGrad" + input: "res3a_branch1_1/Conv2D" + input: "bn3a_branch1_1/ReadVariableOp" + input: "bn3a_branch1_1/FusedBatchNorm:3" + input: "bn3a_branch1_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/bn3a_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_12_1/Relu_grad/ReluGrad" + input: "res3a_branch2c_1/Conv2D" + input: "bn3a_branch2c_1/ReadVariableOp" + input: "bn3a_branch2c_1/FusedBatchNorm:3" + input: "bn3a_branch2c_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_bn3a_branch1/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3a_branch1/beta" + input: "training/SGD/bn3a_branch1/beta/momentum" + input: "training/SGD/SGD/update_bn3a_branch1/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn3a_branch1_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn3a_branch1/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn3a_branch1/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3a_branch1/gamma" + input: "training/SGD/bn3a_branch1/gamma/momentum" + input: "training/SGD/SGD/update_bn3a_branch1/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn3a_branch1_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn3a_branch1/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res3a_branch1_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res3a_branch1_1/Conv2D_grad/ShapeN" + input: "res3a_branch1_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn3a_branch1_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_55" + } + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res3a_branch1_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_9_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res3a_branch1_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn3a_branch1_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_bn3a_branch2c/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3a_branch2c/beta" + input: "training/SGD/bn3a_branch2c/beta/momentum" + input: "training/SGD/SGD/update_bn3a_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn3a_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn3a_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn3a_branch2c/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3a_branch2c/gamma" + input: "training/SGD/bn3a_branch2c/gamma/momentum" + input: "training/SGD/SGD/update_bn3a_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn3a_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn3a_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res3a_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res3a_branch2c_1/Conv2D_grad/ShapeN" + input: "res3a_branch2c_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn3a_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res3a_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_11_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res3a_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn3a_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/AddN_53" + op: "AddN" + input: "training/SGD/gradients/loss_1/res3a_branch1/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res3a_branch1_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/activation_11_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res3a_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_11_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_52" + op: "AddN" + input: "training/SGD/gradients/loss_1/res3a_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res3a_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res3a_branch1/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res3a_branch1/kernel" + input: "training/SGD/res3a_branch1/kernel/momentum" + input: "training/SGD/SGD/update_res3a_branch1/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_53" + input: "training/SGD/SGD/update_res3a_branch1/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/bn3a_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_11_1/Relu_grad/ReluGrad" + input: "res3a_branch2b_1/Conv2D" + input: "bn3a_branch2b_1/ReadVariableOp" + input: "bn3a_branch2b_1/FusedBatchNorm:3" + input: "bn3a_branch2b_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res3a_branch2c/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res3a_branch2c/kernel" + input: "training/SGD/res3a_branch2c/kernel/momentum" + input: "training/SGD/SGD/update_res3a_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_52" + input: "training/SGD/SGD/update_res3a_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn3a_branch2b/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3a_branch2b/beta" + input: "training/SGD/bn3a_branch2b/beta/momentum" + input: "training/SGD/SGD/update_bn3a_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn3a_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn3a_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn3a_branch2b/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3a_branch2b/gamma" + input: "training/SGD/bn3a_branch2b/gamma/momentum" + input: "training/SGD/SGD/update_bn3a_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn3a_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn3a_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res3a_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res3a_branch2b_1/Conv2D_grad/ShapeN" + input: "res3a_branch2b_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn3a_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res3a_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_10_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res3a_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn3a_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/activation_10_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res3a_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_10_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_54" + op: "AddN" + input: "training/SGD/gradients/loss_1/res3a_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res3a_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/bn3a_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_10_1/Relu_grad/ReluGrad" + input: "res3a_branch2a_1/Conv2D" + input: "bn3a_branch2a_1/ReadVariableOp" + input: "bn3a_branch2a_1/FusedBatchNorm:3" + input: "bn3a_branch2a_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res3a_branch2b/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res3a_branch2b/kernel" + input: "training/SGD/res3a_branch2b/kernel/momentum" + input: "training/SGD/SGD/update_res3a_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_54" + input: "training/SGD/SGD/update_res3a_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn3a_branch2a/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3a_branch2a/beta" + input: "training/SGD/bn3a_branch2a/beta/momentum" + input: "training/SGD/SGD/update_bn3a_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn3a_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn3a_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn3a_branch2a/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn3a_branch2a/gamma" + input: "training/SGD/bn3a_branch2a/gamma/momentum" + input: "training/SGD/SGD/update_bn3a_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn3a_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn3a_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res3a_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res3a_branch2a_1/Conv2D_grad/ShapeN" + input: "res3a_branch2a_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn3a_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res3a_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_9_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res3a_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn3a_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/AddN_55" + op: "AddN" + input: "training/SGD/gradients/res3a_branch1_1/Conv2D_grad/Conv2DBackpropInput" + input: "training/SGD/gradients/res3a_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_56" + op: "AddN" + input: "training/SGD/gradients/loss_1/res3a_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res3a_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/activation_9_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/AddN_55" + input: "activation_9_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_59" + } + } + } +} +node { + name: "training/SGD/SGD/update_res3a_branch2a/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res3a_branch2a/kernel" + input: "training/SGD/res3a_branch2a/kernel/momentum" + input: "training/SGD/SGD/update_res3a_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_56" + input: "training/SGD/SGD/update_res3a_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/bn2c_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_9_1/Relu_grad/ReluGrad" + input: "res2c_branch2c_1/Conv2D" + input: "bn2c_branch2c_1/ReadVariableOp" + input: "bn2c_branch2c_1/FusedBatchNorm:3" + input: "bn2c_branch2c_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_bn2c_branch2c/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2c_branch2c/beta" + input: "training/SGD/bn2c_branch2c/beta/momentum" + input: "training/SGD/SGD/update_bn2c_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn2c_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn2c_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn2c_branch2c/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2c_branch2c/gamma" + input: "training/SGD/bn2c_branch2c/gamma/momentum" + input: "training/SGD/SGD/update_bn2c_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn2c_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn2c_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res2c_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res2c_branch2c_1/Conv2D_grad/ShapeN" + input: "res2c_branch2c_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn2c_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res2c_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_8_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res2c_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn2c_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/activation_8_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res2c_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_8_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_57" + op: "AddN" + input: "training/SGD/gradients/loss_1/res2c_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res2c_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/bn2c_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_8_1/Relu_grad/ReluGrad" + input: "res2c_branch2b_1/Conv2D" + input: "bn2c_branch2b_1/ReadVariableOp" + input: "bn2c_branch2b_1/FusedBatchNorm:3" + input: "bn2c_branch2b_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res2c_branch2c/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res2c_branch2c/kernel" + input: "training/SGD/res2c_branch2c/kernel/momentum" + input: "training/SGD/SGD/update_res2c_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_57" + input: "training/SGD/SGD/update_res2c_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn2c_branch2b/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2c_branch2b/beta" + input: "training/SGD/bn2c_branch2b/beta/momentum" + input: "training/SGD/SGD/update_bn2c_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn2c_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn2c_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn2c_branch2b/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2c_branch2b/gamma" + input: "training/SGD/bn2c_branch2b/gamma/momentum" + input: "training/SGD/SGD/update_bn2c_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn2c_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn2c_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res2c_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res2c_branch2b_1/Conv2D_grad/ShapeN" + input: "res2c_branch2b_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn2c_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res2c_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_7_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res2c_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn2c_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/activation_7_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res2c_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_7_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_58" + op: "AddN" + input: "training/SGD/gradients/loss_1/res2c_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res2c_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/bn2c_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_7_1/Relu_grad/ReluGrad" + input: "res2c_branch2a_1/Conv2D" + input: "bn2c_branch2a_1/ReadVariableOp" + input: "bn2c_branch2a_1/FusedBatchNorm:3" + input: "bn2c_branch2a_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res2c_branch2b/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res2c_branch2b/kernel" + input: "training/SGD/res2c_branch2b/kernel/momentum" + input: "training/SGD/SGD/update_res2c_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_58" + input: "training/SGD/SGD/update_res2c_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn2c_branch2a/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2c_branch2a/beta" + input: "training/SGD/bn2c_branch2a/beta/momentum" + input: "training/SGD/SGD/update_bn2c_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn2c_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn2c_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn2c_branch2a/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2c_branch2a/gamma" + input: "training/SGD/bn2c_branch2a/gamma/momentum" + input: "training/SGD/SGD/update_bn2c_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn2c_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn2c_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res2c_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res2c_branch2a_1/Conv2D_grad/ShapeN" + input: "res2c_branch2a_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn2c_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res2c_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_6_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res2c_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn2c_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/AddN_59" + op: "AddN" + input: "training/SGD/gradients/activation_9_1/Relu_grad/ReluGrad" + input: "training/SGD/gradients/res2c_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_60" + op: "AddN" + input: "training/SGD/gradients/loss_1/res2c_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res2c_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/activation_6_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/AddN_59" + input: "activation_6_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_63" + } + } + } +} +node { + name: "training/SGD/SGD/update_res2c_branch2a/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res2c_branch2a/kernel" + input: "training/SGD/res2c_branch2a/kernel/momentum" + input: "training/SGD/SGD/update_res2c_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_60" + input: "training/SGD/SGD/update_res2c_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/bn2b_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_6_1/Relu_grad/ReluGrad" + input: "res2b_branch2c_1/Conv2D" + input: "bn2b_branch2c_1/ReadVariableOp" + input: "bn2b_branch2c_1/FusedBatchNorm:3" + input: "bn2b_branch2c_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_bn2b_branch2c/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2b_branch2c/beta" + input: "training/SGD/bn2b_branch2c/beta/momentum" + input: "training/SGD/SGD/update_bn2b_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn2b_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn2b_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn2b_branch2c/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2b_branch2c/gamma" + input: "training/SGD/bn2b_branch2c/gamma/momentum" + input: "training/SGD/SGD/update_bn2b_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn2b_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn2b_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res2b_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res2b_branch2c_1/Conv2D_grad/ShapeN" + input: "res2b_branch2c_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn2b_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res2b_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_5_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res2b_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn2b_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/activation_5_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res2b_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_5_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_61" + op: "AddN" + input: "training/SGD/gradients/loss_1/res2b_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res2b_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/bn2b_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_5_1/Relu_grad/ReluGrad" + input: "res2b_branch2b_1/Conv2D" + input: "bn2b_branch2b_1/ReadVariableOp" + input: "bn2b_branch2b_1/FusedBatchNorm:3" + input: "bn2b_branch2b_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res2b_branch2c/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res2b_branch2c/kernel" + input: "training/SGD/res2b_branch2c/kernel/momentum" + input: "training/SGD/SGD/update_res2b_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_61" + input: "training/SGD/SGD/update_res2b_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn2b_branch2b/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2b_branch2b/beta" + input: "training/SGD/bn2b_branch2b/beta/momentum" + input: "training/SGD/SGD/update_bn2b_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn2b_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn2b_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn2b_branch2b/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2b_branch2b/gamma" + input: "training/SGD/bn2b_branch2b/gamma/momentum" + input: "training/SGD/SGD/update_bn2b_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn2b_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn2b_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res2b_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res2b_branch2b_1/Conv2D_grad/ShapeN" + input: "res2b_branch2b_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn2b_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res2b_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_4_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res2b_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn2b_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/activation_4_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res2b_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_4_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_62" + op: "AddN" + input: "training/SGD/gradients/loss_1/res2b_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res2b_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/bn2b_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_4_1/Relu_grad/ReluGrad" + input: "res2b_branch2a_1/Conv2D" + input: "bn2b_branch2a_1/ReadVariableOp" + input: "bn2b_branch2a_1/FusedBatchNorm:3" + input: "bn2b_branch2a_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res2b_branch2b/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res2b_branch2b/kernel" + input: "training/SGD/res2b_branch2b/kernel/momentum" + input: "training/SGD/SGD/update_res2b_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_62" + input: "training/SGD/SGD/update_res2b_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn2b_branch2a/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2b_branch2a/beta" + input: "training/SGD/bn2b_branch2a/beta/momentum" + input: "training/SGD/SGD/update_bn2b_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn2b_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn2b_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn2b_branch2a/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2b_branch2a/gamma" + input: "training/SGD/bn2b_branch2a/gamma/momentum" + input: "training/SGD/SGD/update_bn2b_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn2b_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn2b_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res2b_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res2b_branch2a_1/Conv2D_grad/ShapeN" + input: "res2b_branch2a_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn2b_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res2b_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_3_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res2b_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn2b_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/AddN_63" + op: "AddN" + input: "training/SGD/gradients/activation_6_1/Relu_grad/ReluGrad" + input: "training/SGD/gradients/res2b_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_64" + op: "AddN" + input: "training/SGD/gradients/loss_1/res2b_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res2b_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/activation_3_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/AddN_63" + input: "activation_3_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res2b_branch2a/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res2b_branch2a/kernel" + input: "training/SGD/res2b_branch2a/kernel/momentum" + input: "training/SGD/SGD/update_res2b_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_64" + input: "training/SGD/SGD/update_res2b_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/bn2a_branch1_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_3_1/Relu_grad/ReluGrad" + input: "res2a_branch1_1/Conv2D" + input: "bn2a_branch1_1/ReadVariableOp" + input: "bn2a_branch1_1/FusedBatchNorm:3" + input: "bn2a_branch1_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/bn2a_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_3_1/Relu_grad/ReluGrad" + input: "res2a_branch2c_1/Conv2D" + input: "bn2a_branch2c_1/ReadVariableOp" + input: "bn2a_branch2c_1/FusedBatchNorm:3" + input: "bn2a_branch2c_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_bn2a_branch1/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2a_branch1/beta" + input: "training/SGD/bn2a_branch1/beta/momentum" + input: "training/SGD/SGD/update_bn2a_branch1/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn2a_branch1_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn2a_branch1/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn2a_branch1/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2a_branch1/gamma" + input: "training/SGD/bn2a_branch1/gamma/momentum" + input: "training/SGD/SGD/update_bn2a_branch1/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn2a_branch1_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn2a_branch1/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res2a_branch1_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res2a_branch1_1/Conv2D_grad/ShapeN" + input: "res2a_branch1_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn2a_branch1_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@training/SGD/gradients/AddN_68" + } + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res2a_branch1_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "max_pooling2d_1/MaxPool" + input: "ConstantFolding/training/SGD/gradients/res2a_branch1_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn2a_branch1_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_bn2a_branch2c/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2a_branch2c/beta" + input: "training/SGD/bn2a_branch2c/beta/momentum" + input: "training/SGD/SGD/update_bn2a_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn2a_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn2a_branch2c/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn2a_branch2c/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2a_branch2c/gamma" + input: "training/SGD/bn2a_branch2c/gamma/momentum" + input: "training/SGD/SGD/update_bn2a_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn2a_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn2a_branch2c/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res2a_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res2a_branch2c_1/Conv2D_grad/ShapeN" + input: "res2a_branch2c_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn2a_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res2a_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_2_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res2a_branch2c_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn2a_branch2c_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/AddN_66" + op: "AddN" + input: "training/SGD/gradients/loss_1/res2a_branch1/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res2a_branch1_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/activation_2_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res2a_branch2c_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_2_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_65" + op: "AddN" + input: "training/SGD/gradients/loss_1/res2a_branch2c/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res2a_branch2c_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_res2a_branch1/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res2a_branch1/kernel" + input: "training/SGD/res2a_branch1/kernel/momentum" + input: "training/SGD/SGD/update_res2a_branch1/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_66" + input: "training/SGD/SGD/update_res2a_branch1/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/bn2a_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_2_1/Relu_grad/ReluGrad" + input: "res2a_branch2b_1/Conv2D" + input: "bn2a_branch2b_1/ReadVariableOp" + input: "bn2a_branch2b_1/FusedBatchNorm:3" + input: "bn2a_branch2b_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res2a_branch2c/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res2a_branch2c/kernel" + input: "training/SGD/res2a_branch2c/kernel/momentum" + input: "training/SGD/SGD/update_res2a_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_65" + input: "training/SGD/SGD/update_res2a_branch2c/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn2a_branch2b/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2a_branch2b/beta" + input: "training/SGD/bn2a_branch2b/beta/momentum" + input: "training/SGD/SGD/update_bn2a_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn2a_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn2a_branch2b/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn2a_branch2b/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2a_branch2b/gamma" + input: "training/SGD/bn2a_branch2b/gamma/momentum" + input: "training/SGD/SGD/update_bn2a_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn2a_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn2a_branch2b/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res2a_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res2a_branch2b_1/Conv2D_grad/ShapeN" + input: "res2a_branch2b_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn2a_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res2a_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "activation_1_1/Relu" + input: "ConstantFolding/training/SGD/gradients/res2a_branch2b_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn2a_branch2b_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/activation_1_1/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/res2a_branch2b_1/Conv2D_grad/Conv2DBackpropInput" + input: "activation_1_1/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_67" + op: "AddN" + input: "training/SGD/gradients/loss_1/res2a_branch2b/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res2a_branch2b_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/bn2a_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_1_1/Relu_grad/ReluGrad" + input: "res2a_branch2a_1/Conv2D" + input: "bn2a_branch2a_1/ReadVariableOp" + input: "bn2a_branch2a_1/FusedBatchNorm:3" + input: "bn2a_branch2a_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_res2a_branch2b/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res2a_branch2b/kernel" + input: "training/SGD/res2a_branch2b/kernel/momentum" + input: "training/SGD/SGD/update_res2a_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_67" + input: "training/SGD/SGD/update_res2a_branch2b/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn2a_branch2a/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2a_branch2a/beta" + input: "training/SGD/bn2a_branch2a/beta/momentum" + input: "training/SGD/SGD/update_bn2a_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn2a_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn2a_branch2a/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn2a_branch2a/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn2a_branch2a/gamma" + input: "training/SGD/bn2a_branch2a/gamma/momentum" + input: "training/SGD/SGD/update_bn2a_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn2a_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn2a_branch2a/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/res2a_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + op: "Conv2DBackpropInput" + input: "training/SGD/gradients/res2a_branch2a_1/Conv2D_grad/ShapeN" + input: "res2a_branch2a_1/Conv2D/ReadVariableOp" + input: "training/SGD/gradients/bn2a_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/res2a_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "max_pooling2d_1/MaxPool" + input: "ConstantFolding/training/SGD/gradients/res2a_branch2a_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn2a_branch2a_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/AddN_68" + op: "AddN" + input: "training/SGD/gradients/res2a_branch1_1/Conv2D_grad/Conv2DBackpropInput" + input: "training/SGD/gradients/res2a_branch2a_1/Conv2D_grad/Conv2DBackpropInput" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/AddN_69" + op: "AddN" + input: "training/SGD/gradients/loss_1/res2a_branch2a/kernel/Regularizer/Square_grad/Mul_1" + input: "training/SGD/gradients/res2a_branch2a_1/Conv2D_grad/Conv2DBackpropFilter" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/max_pooling2d_1/MaxPool_grad/MaxPoolGrad" + op: "MaxPoolGrad" + input: "activation_50/Relu" + input: "max_pooling2d_1/MaxPool" + input: "training/SGD/gradients/AddN_68" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "ksize" + value { + list { + i: 1 + i: 1 + i: 3 + i: 3 + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } +} +node { + name: "training/SGD/SGD/update_res2a_branch2a/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "res2a_branch2a/kernel" + input: "training/SGD/res2a_branch2a/kernel/momentum" + input: "training/SGD/SGD/update_res2a_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_69" + input: "training/SGD/SGD/update_res2a_branch2a/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/activation_50/Relu_grad/ReluGrad" + op: "ReluGrad" + input: "training/SGD/gradients/max_pooling2d_1/MaxPool_grad/MaxPoolGrad" + input: "activation_50/Relu" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/gradients/bn_conv1_1/FusedBatchNorm_grad/FusedBatchNormGrad" + op: "FusedBatchNormGrad" + input: "training/SGD/gradients/activation_50/Relu_grad/ReluGrad" + input: "conv1_1/Conv2D" + input: "bn_conv1_1/ReadVariableOp" + input: "bn_conv1_1/FusedBatchNorm:3" + input: "bn_conv1_1/FusedBatchNorm:4" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "epsilon" + value { + f: 1.001e-05 + } + } + attr { + key: "is_training" + value { + b: true + } + } +} +node { + name: "training/SGD/SGD/update_bn_conv1/beta/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn_conv1/beta" + input: "training/SGD/bn_conv1/beta/momentum" + input: "training/SGD/SGD/update_bn_conv1/beta/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn_conv1_1/FusedBatchNorm_grad/FusedBatchNormGrad:2" + input: "training/SGD/SGD/update_bn_conv1/beta/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/update_bn_conv1/gamma/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "bn_conv1/gamma" + input: "training/SGD/bn_conv1/gamma/momentum" + input: "training/SGD/SGD/update_bn_conv1/gamma/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/bn_conv1_1/FusedBatchNorm_grad/FusedBatchNormGrad:1" + input: "training/SGD/SGD/update_bn_conv1/gamma/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/gradients/conv1_1/Conv2D_grad/Conv2DBackpropFilter" + op: "Conv2DBackpropFilter" + input: "conv1_pad_1/Pad" + input: "ConstantFolding/training/SGD/gradients/conv1_1/Conv2D_grad/ShapeN-matshapes-1" + input: "training/SGD/gradients/bn_conv1_1/FusedBatchNorm_grad/FusedBatchNormGrad" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NCHW" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "VALID" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 2 + i: 2 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "training/SGD/gradients/AddN_70" + op: "AddN" + input: "training/SGD/gradients/conv1_1/Conv2D_grad/Conv2DBackpropFilter" + input: "training/SGD/gradients/loss_1/conv1/kernel/Regularizer/Square_grad/Mul_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "training/SGD/SGD/update_conv1/kernel/ResourceApplyKerasMomentum" + op: "ResourceApplyKerasMomentum" + input: "conv1/kernel" + input: "training/SGD/conv1/kernel/momentum" + input: "training/SGD/SGD/update_conv1/kernel/ResourceApplyKerasMomentum/ReadVariableOp" + input: "training/SGD/gradients/AddN_70" + input: "training/SGD/SGD/update_conv1/kernel/ResourceApplyKerasMomentum/ReadVariableOp_1" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "training/SGD/SGD/Const" + op: "Const" + input: "^training/SGD/SGD/update_bn2a_branch1/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn2a_branch1/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn2a_branch2a/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn2a_branch2a/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn2a_branch2b/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn2a_branch2b/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn2a_branch2c/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn2a_branch2c/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn2b_branch2a/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn2b_branch2a/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn2b_branch2b/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn2b_branch2b/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn2b_branch2c/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn2b_branch2c/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn2c_branch2a/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn2c_branch2a/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn2c_branch2b/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn2c_branch2b/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn2c_branch2c/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn2c_branch2c/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn3a_branch1/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn3a_branch1/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn3a_branch2a/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn3a_branch2a/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn3a_branch2b/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn3a_branch2b/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn3a_branch2c/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn3a_branch2c/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn3b_branch2a/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn3b_branch2a/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn3b_branch2b/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn3b_branch2b/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn3b_branch2c/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn3b_branch2c/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn3c_branch2a/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn3c_branch2a/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn3c_branch2b/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn3c_branch2b/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn3c_branch2c/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn3c_branch2c/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn3d_branch2a/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn3d_branch2a/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn3d_branch2b/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn3d_branch2b/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn3d_branch2c/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn3d_branch2c/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4a_branch1/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4a_branch1/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4a_branch2a/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4a_branch2a/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4a_branch2b/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4a_branch2b/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4a_branch2c/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4a_branch2c/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4b_branch2a/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4b_branch2a/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4b_branch2b/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4b_branch2b/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4b_branch2c/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4b_branch2c/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4c_branch2a/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4c_branch2a/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4c_branch2b/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4c_branch2b/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4c_branch2c/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4c_branch2c/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4d_branch2a/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4d_branch2a/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4d_branch2b/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4d_branch2b/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4d_branch2c/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4d_branch2c/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4e_branch2a/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4e_branch2a/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4e_branch2b/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4e_branch2b/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4e_branch2c/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4e_branch2c/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4f_branch2a/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4f_branch2a/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4f_branch2b/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4f_branch2b/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4f_branch2c/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn4f_branch2c/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn5a_branch1/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn5a_branch1/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn5a_branch2a/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn5a_branch2a/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn5a_branch2b/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn5a_branch2b/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn5a_branch2c/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn5a_branch2c/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn5b_branch2a/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn5b_branch2a/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn5b_branch2b/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn5b_branch2b/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn5b_branch2c/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn5b_branch2c/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn5c_branch2a/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn5c_branch2a/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn5c_branch2b/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn5c_branch2b/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn5c_branch2c/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn5c_branch2c/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn_conv1/beta/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_bn_conv1/gamma/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_conv1/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_fc1000/bias/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_fc1000/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res2a_branch1/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res2a_branch2a/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res2a_branch2b/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res2a_branch2c/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res2b_branch2a/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res2b_branch2b/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res2b_branch2c/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res2c_branch2a/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res2c_branch2b/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res2c_branch2c/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res3a_branch1/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res3a_branch2a/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res3a_branch2b/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res3a_branch2c/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res3b_branch2a/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res3b_branch2b/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res3b_branch2c/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res3c_branch2a/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res3c_branch2b/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res3c_branch2c/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res3d_branch2a/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res3d_branch2b/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res3d_branch2c/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res4a_branch1/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res4a_branch2a/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res4a_branch2b/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res4a_branch2c/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res4b_branch2a/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res4b_branch2b/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res4b_branch2c/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res4c_branch2a/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res4c_branch2b/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res4c_branch2c/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res4d_branch2a/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res4d_branch2b/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res4d_branch2c/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res4e_branch2a/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res4e_branch2b/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res4e_branch2c/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res4f_branch2a/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res4f_branch2b/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res4f_branch2c/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res5a_branch1/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res5a_branch2a/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res5a_branch2b/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res5a_branch2c/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res5b_branch2a/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res5b_branch2b/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res5b_branch2c/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res5c_branch2a/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res5c_branch2b/kernel/ResourceApplyKerasMomentum" + input: "^training/SGD/SGD/update_res5c_branch2c/kernel/ResourceApplyKerasMomentum" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT64 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT64 + tensor_shape { + } + int64_val: 1 + } + } + } +} +node { + name: "training/SGD/SGD/AssignAddVariableOp" + op: "AssignAddVariableOp" + input: "training/SGD/iter" + input: "training/SGD/SGD/Const" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "dtype" + value { + type: DT_INT64 + } + } +} +node { + name: "group_deps_1" + op: "NoOp" + input: "^bn2a_branch1_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn2a_branch1_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn2a_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn2a_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn2a_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn2a_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn2a_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn2a_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn2b_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn2b_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn2b_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn2b_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn2b_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn2b_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn2c_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn2c_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn2c_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn2c_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn2c_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn2c_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn3a_branch1_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn3a_branch1_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn3a_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn3a_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn3a_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn3a_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn3a_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn3a_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn3b_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn3b_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn3b_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn3b_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn3b_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn3b_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn3c_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn3c_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn3c_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn3c_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn3c_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn3c_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn3d_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn3d_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn3d_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn3d_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn3d_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn3d_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4a_branch1_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4a_branch1_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4a_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4a_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4a_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4a_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4a_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4a_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4b_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4b_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4b_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4b_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4b_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4b_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4c_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4c_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4c_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4c_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4c_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4c_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4d_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4d_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4d_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4d_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4d_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4d_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4e_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4e_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4e_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4e_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4e_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4e_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4f_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4f_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4f_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4f_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn4f_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn4f_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn5a_branch1_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn5a_branch1_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn5a_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn5a_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn5a_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn5a_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn5a_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn5a_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn5b_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn5b_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn5b_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn5b_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn5b_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn5b_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn5c_branch2a_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn5c_branch2a_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn5c_branch2b_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn5c_branch2b_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn5c_branch2c_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn5c_branch2c_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^bn_conv1_1/AssignMovingAvg/AssignSubVariableOp" + input: "^bn_conv1_1/AssignMovingAvg_1/AssignSubVariableOp" + input: "^loss_1/add" + input: "^training/SGD/SGD/AssignAddVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" +} +node { + name: "_identity_input_1_1_0" + op: "Identity" + input: "G4SM2HGMB5/Read/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "_identity_activation_49_target_1_0" + op: "Identity" + input: "HK0SX0WHBZ/Read/ReadVariableOp" + device: "/job:localhost/replica:0/task:0/device:GPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "_retval_loss_1/add_0_0" + op: "_Retval" + input: "loss_1/add" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "index" + value { + i: 0 + } + } +} +node { + name: "_retval_metrics_2/sparse_categorical_accuracy/Identity_0_1" + op: "_Retval" + input: "metrics_2/sparse_categorical_accuracy/Identity" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "index" + value { + i: 1 + } + } +} +library { +} +versions { + producer: 41 +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/opens2s_gnmt_mixed_precision.golden_summary b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/opens2s_gnmt_mixed_precision.golden_summary new file mode 100644 index 0000000000000000000000000000000000000000..12bc4c46041673611b2e03ed6914be0a0eefa4de --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/opens2s_gnmt_mixed_precision.golden_summary @@ -0,0 +1,438 @@ +Clustered nodes: 2385 +Unclustered nodes: 4221 +Number of clusters: 30 + +unclustered size 4221 + Add 17 + AddN 1 + All 1 + ApplyAdam 38 + Assert 7 + Assign 47 + AssignAdd 2 + AssignSub 2 + BroadcastGradientArgs 44 + Cast 38 + ConcatV2 3 + Const 875 + ControlTrigger 5 + Enter 874 + Equal 4 + Exit 69 + ExpandDims 9 + Fill 5 + FloorMod 1 + GreaterEqual 7 + Identity 113 + IsVariableInitialized 1 + IteratorGetNext 1 + IteratorV2 1 + Less 9 + LogicalAnd 3 + LoopCond 8 + Max 4 + Maximum 44 + Merge 145 + Minimum 43 + Mul 8 + NextIteration 136 + RandomUniform 14 + Range 9 + RefSwitch 166 + Reshape 2 + ScatterAdd 4 + Shape 6 + ShapeN 10 + Size 2 + Snapshot 1 + StackPopV2 314 + StackPushV2 314 + StackV2 314 + StridedSlice 12 + Sub 5 + Sum 2 + Switch 247 + TensorArrayGatherV3 8 + TensorArrayGradV3 17 + TensorArrayReadV3 9 + TensorArrayScatterV3 8 + TensorArraySizeV3 4 + TensorArrayV3 8 + TensorArrayWriteV3 9 + Unique 2 + VariableV2 164 + _Retval 5 +cluster 0 size 440 + Abs 40 + AddN 1 + Any 41 + Cast 40 + ConcatV2 2 + Const 95 + ExpandDims 2 + IsInf 1 + IsNan 40 + L2Loss 40 + LogicalOr 1 + Max 41 + Minimum 1 + Mul 82 + Pack 3 + Reciprocal 2 + Reshape 2 + ReverseSequence 1 + Sqrt 1 + Sum 1 + Transpose 3 +cluster 1 size 86 + BroadcastGradientArgs 1 + Cast 5 + ConcatV2 1 + Const 30 + ExpandDims 3 + Less 1 + Mean 2 + Minimum 1 + Mul 3 + Pack 2 + Pad 1 + Range 1 + RealDiv 1 + Reshape 8 + Shape 7 + Size 1 + Slice 2 + Snapshot 5 + SparseSoftmaxCrossEntropyWithLogits 1 + StridedSlice 2 + Sub 3 + Sum 1 + Tile 1 + Transpose 2 + Unpack 1 +cluster 2 size 44 + Cast 2 + ConcatV2 2 + Const 18 + ExpandDims 1 + GatherV2 2 + Less 1 + MatMul 1 + Mul 1 + Pack 1 + Prod 2 + Range 1 + Reshape 3 + Shape 8 + StridedSlice 1 +cluster 3 size 10 + AddN 1 + Const 1 + MatMul 2 + Mul 1 + Reshape 3 + Sum 1 + Transpose 1 +cluster 4 size 11 + ConcatOffset 1 + Const 4 + ReverseSequence 1 + Slice 2 + Transpose 3 +cluster 5 size 21 + All 1 + ConcatV2 1 + Const 11 + Equal 1 + ExpandDims 1 + ReverseSequence 1 + Shape 1 + StridedSlice 1 + Transpose 3 +cluster 6 size 11 + Cast 1 + Const 5 + GatherV2 1 + Shape 1 + StridedSlice 1 + Transpose 1 + ZerosLike 1 +cluster 7 size 33 + All 2 + Cast 1 + Const 17 + Equal 2 + ExpandDims 2 + GatherV2 1 + Identity 1 + ReverseSequence 1 + Shape 2 + StridedSlice 2 + Transpose 2 +cluster 8 size 11 + Cast 1 + Const 4 + Floor 1 + Identity 1 + Mul 2 + Pow 1 + Sub 1 +cluster 9 size 5 + All 1 + Const 1 + Less 1 + LogicalAnd 1 + LogicalNot 1 +cluster 10 size 9 + All 1 + Const 4 + Equal 1 + LessEqual 1 + LogicalOr 1 + Max 1 +cluster 11 size 302 + Add 24 + BatchMatMulV2 1 + BiasAdd 8 + Cast 8 + ConcatV2 16 + Const 81 + ExpandDims 3 + Fill 1 + GreaterEqual 8 + Identity 1 + Less 1 + MatMul 10 + Mul 44 + Range 1 + Rsqrt 1 + Select 19 + Shape 6 + Sigmoid 24 + Snapshot 8 + Softmax 1 + Split 8 + SplitV 6 + Square 1 + Squeeze 1 + StridedSlice 1 + Sum 2 + Tanh 17 +cluster 12 size 6 + Add 1 + All 1 + Const 2 + GreaterEqual 1 + LogicalOr 1 +cluster 15 size 614 + Add 22 + AddN 41 + BatchMatMulV2 2 + BiasAddGrad 8 + BroadcastGradientArgs 4 + ConcatOffset 1 + ConcatV2 14 + Const 199 + DynamicStitch 1 + FloorDiv 1 + MatMul 20 + Maximum 1 + Mul 74 + NoOp 13 + Reshape 86 + RsqrtGrad 1 + Select 36 + SigmoidGrad 24 + Slice 32 + StridedSlice 2 + Sub 1 + Sum 11 + TanhGrad 17 + Tile 2 + ZerosLike 1 +cluster 16 size 22 + Add 2 + BiasAdd 1 + ConcatV2 1 + Const 3 + GreaterEqual 1 + MatMul 1 + Mul 3 + Select 3 + Shape 1 + Sigmoid 3 + Split 1 + Tanh 2 +cluster 17 size 60 + Add 2 + AddN 4 + BiasAddGrad 1 + BroadcastGradientArgs 1 + Cast 2 + ConcatOffset 1 + ConcatV2 1 + Const 8 + MatMul 2 + Mul 6 + NoOp 2 + Reshape 9 + Select 5 + SigmoidGrad 3 + Slice 2 + Sum 9 + TanhGrad 2 +cluster 18 size 22 + Add 2 + BiasAdd 1 + ConcatV2 1 + Const 3 + GreaterEqual 1 + MatMul 1 + Mul 3 + Select 3 + Shape 1 + Sigmoid 3 + Split 1 + Tanh 2 +cluster 19 size 60 + Add 2 + AddN 4 + BiasAddGrad 1 + BroadcastGradientArgs 1 + Cast 2 + ConcatOffset 1 + ConcatV2 1 + Const 8 + MatMul 2 + Mul 6 + NoOp 2 + Reshape 9 + Select 5 + SigmoidGrad 3 + Slice 2 + Sum 9 + TanhGrad 2 +cluster 21 size 29 + Add 2 + BiasAdd 1 + Cast 1 + ConcatV2 1 + Const 7 + GreaterEqual 2 + MatMul 1 + Mul 5 + Select 2 + Sigmoid 3 + Snapshot 1 + Split 1 + Tanh 2 +cluster 22 size 28 + Add 3 + BiasAdd 1 + Cast 1 + ConcatV2 1 + Const 5 + GreaterEqual 1 + MatMul 1 + Mul 5 + Select 3 + Sigmoid 3 + Snapshot 1 + Split 1 + Tanh 2 +cluster 23 size 423 + Add 12 + AddN 28 + BiasAddGrad 6 + BroadcastGradientArgs 12 + Cast 12 + ConcatOffset 6 + ConcatV2 6 + Const 49 + MatMul 12 + Mul 48 + NoOp 13 + Reshape 76 + Select 25 + SigmoidGrad 18 + Slice 12 + Sum 76 + TanhGrad 12 +cluster 24 size 4 + Const 1 + Shape 2 + Transpose 1 +cluster 25 size 27 + Add 3 + BiasAdd 1 + Cast 1 + ConcatV2 1 + Const 5 + GreaterEqual 1 + MatMul 1 + Mul 5 + Select 2 + Sigmoid 3 + Snapshot 1 + Split 1 + Tanh 2 +cluster 26 size 27 + Add 3 + BiasAdd 1 + Cast 1 + ConcatV2 1 + Const 5 + GreaterEqual 1 + MatMul 1 + Mul 5 + Select 2 + Sigmoid 3 + Snapshot 1 + Split 1 + Tanh 2 +cluster 27 size 27 + Add 3 + BiasAdd 1 + Cast 1 + ConcatV2 1 + Const 5 + GreaterEqual 1 + MatMul 1 + Mul 5 + Select 2 + Sigmoid 3 + Snapshot 1 + Split 1 + Tanh 2 +cluster 28 size 27 + Add 3 + BiasAdd 1 + Cast 1 + ConcatV2 1 + Const 5 + GreaterEqual 1 + MatMul 1 + Mul 5 + Select 2 + Sigmoid 3 + Snapshot 1 + Split 1 + Tanh 2 +cluster 29 size 9 + Add 1 + Mul 2 + RealDiv 2 + Sqrt 2 + Sub 2 +cluster 30 size 9 + Add 1 + Mul 2 + RealDiv 2 + Sqrt 2 + Sub 2 +cluster 31 size 4 + Mul 3 + UnsortedSegmentSum 1 +cluster 32 size 4 + Mul 3 + UnsortedSegmentSum 1 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/opens2s_gnmt_mixed_precision.pbtxt.gz b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/opens2s_gnmt_mixed_precision.pbtxt.gz new file mode 100644 index 0000000000000000000000000000000000000000..a8406b019b6ce3b7bb025fcc210f4be5af018e18 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tests/opens2s_gnmt_mixed_precision.pbtxt.gz @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:86666e236c31e4ecc80e445b806babaa6531587a1f2de99b5a66a1d73356d030 +size 200228 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/tf_graph_to_hlo_compiler.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tf_graph_to_hlo_compiler.cc new file mode 100644 index 0000000000000000000000000000000000000000..1139e717d3699520f814418aae672f2ea55e5d80 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tf_graph_to_hlo_compiler.cc @@ -0,0 +1,102 @@ +/* Copyright 2022 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/compiler/jit/tf_graph_to_hlo_compiler.h" + +#include +#include + +#include "absl/log/log.h" +#include "absl/status/status.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" +#include "absl/types/span.h" +#include "tensorflow/compiler/jit/tf_graph_to_hlo_compiler.pb.h" +#include "tensorflow/compiler/tf2xla/xla_argument.h" +#include "tensorflow/compiler/tf2xla/xla_compiler.h" +#include "tensorflow/compiler/tf2xla/xla_helpers.h" +#include "xla/tsl/lib/strings/proto_serialization.h" +#include "xla/tsl/platform/env.h" +#include "tensorflow/core/framework/op_kernel.h" +#include "tensorflow/core/platform/env.h" +#include "tsl/platform/path.h" + +namespace tensorflow { + +TfGraphToHloCompiler::TfGraphToHloCompiler(const XlaCompiler::Options& options) + : xla_compiler_(options) { + absl::string_view dump_base_dir = + absl::NullSafeStringView(getenv("TF_GRAPH_TO_HLO_COMPILER_DUMP_DIR")); + if (dump_base_dir.empty()) { + return; + } + + TfGraphToHloCompilerOptions options_proto; + options_proto.set_device_type(options.device_type.type_string()); + options_proto.set_graph_def_version(options.graph_def_version); + *options_proto.mutable_flib_def() = options.flib_def->ToProto(); + + std::string dump_dir = tsl::io::JoinPath( + dump_base_dir, + absl::StrCat(tsl::DeterministicProtoHash64(options_proto))); + if (!Env::Default()->RecursivelyCreateDir(dump_dir).ok()) { + LOG(WARNING) << "[TfGraphToHloCompiler] Failed to create dump directory: " + << dump_dir; + return; + } + dump_dir_ = dump_dir; + + std::string options_path = tsl::io::JoinPath(dump_dir, "options.pb"); + if (!tsl::WriteBinaryProto(Env::Default(), options_path, options_proto) + .ok()) { + LOG(WARNING) << "[TfGraphToHloCompiler] Failed to dump XLA compiler " + "options proto to " + << options_path; + } +} + +absl::Status TfGraphToHloCompiler::Compile( + const XlaCompiler::CompileOptions& options, const NameAttrList& function, + absl::Span args, XlaCompilationResult* result) { + if (!dump_dir_.empty()) { + TfGraphToHloCompilerCompileCallArgs call_args; + call_args.set_compile_options(options.DebugString()); + *call_args.mutable_function() = function; + for (const XlaArgument& arg : args) { + *call_args.add_xla_args() = arg.ToProto(); + } + + std::string dump_path = tsl::io::JoinPath( + dump_dir_, + absl::StrCat(tsl::DeterministicProtoHash64(call_args), ".pb")); + if (!tsl::WriteBinaryProto(Env::Default(), dump_path, call_args).ok()) { + LOG(WARNING) << "[TfGraphToHloCompiler] Failed to dump " + "TfGraphToHloCompilerCompileCallArgs proto to " + << dump_path; + } + } + + return ADD_SOURCE_LOCATION( + xla_compiler_.CompileFunction(options, function, args, result)); +} + +absl::Status TfGraphToHloCompiler::CompileSingleOp( + const XlaCompiler::CompileOptions& options, const OpKernelContext* ctx, + absl::Span args, XlaCompilationResult* result) { + return ADD_SOURCE_LOCATION(xla_compiler_.CompileSingleOp( + options, XlaCompiler::SingleOpCompileArgument(*ctx), args, result)); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/tf_graph_to_hlo_compiler.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tf_graph_to_hlo_compiler.h new file mode 100644 index 0000000000000000000000000000000000000000..7ea854525859e39a8e177b99dc6ee98672cb499a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tf_graph_to_hlo_compiler.h @@ -0,0 +1,61 @@ +/* Copyright 2022 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_TF_GRAPH_TO_HLO_COMPILER_H_ +#define TENSORFLOW_COMPILER_JIT_TF_GRAPH_TO_HLO_COMPILER_H_ + +#include "absl/status/status.h" +#include "absl/types/span.h" +#include "tensorflow/compiler/jit/tf_to_hlo_compiler.h" +#include "tensorflow/compiler/tf2xla/xla_argument.h" +#include "tensorflow/compiler/tf2xla/xla_compiler.h" +#include "tensorflow/compiler/tf2xla/xla_helpers.h" +#include "tensorflow/core/framework/op_kernel.h" + +namespace tensorflow { + +class TfGraphToHloCompiler : public TfToHloCompiler { + public: + TfGraphToHloCompiler() = delete; + + explicit TfGraphToHloCompiler(const XlaCompiler::Options& options); + + // Compiles a Tensorflow `function` into an HloModuleProto stored in the + // XlaCompilationResult pointed to by `result` by calling + // XlaCompiler::CompileFunction. + absl::Status Compile(const XlaCompiler::CompileOptions& options, + const NameAttrList& function, + absl::Span args, + XlaCompilationResult* result) override; + + // Compiles a Tensorflow single op into an HloModuleProto stored in the + // XlaCompilationResult pointed to by `result` by calling + // XlaCompiler::CompileSingleOp. + absl::Status CompileSingleOp(const XlaCompiler::CompileOptions& options, + const OpKernelContext* ctx, + absl::Span args, + XlaCompilationResult* result) override; + + private: + XlaCompiler xla_compiler_; + std::string dump_dir_; + + TfGraphToHloCompiler(const TfGraphToHloCompiler&) = delete; + void operator=(const TfGraphToHloCompiler&) = delete; +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_TF_GRAPH_TO_HLO_COMPILER_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/tf_graph_to_hlo_compiler.proto b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tf_graph_to_hlo_compiler.proto new file mode 100644 index 0000000000000000000000000000000000000000..f0cbf935a248bfa859c6fdf02f037f31f0f12978 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tf_graph_to_hlo_compiler.proto @@ -0,0 +1,40 @@ +/* Copyright 2025 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. +==============================================================================*/ + +syntax = "proto3"; + +package tensorflow; + +import "tensorflow/compiler/tf2xla/xla_argument.proto"; +import "tensorflow/core/framework/attr_value.proto"; +import "tensorflow/core/framework/function.proto"; + +// XLA compiler options used for constructing a TfGraphToHloCompiler instance. +// Next ID: 4 +message TfGraphToHloCompilerOptions { + string device_type = 1; + FunctionDefLibrary flib_def = 2; + int32 graph_def_version = 3; +} + +// Arguments used for a TfGraphToHloCompiler::Compile() call. +// Next ID: 5 +message TfGraphToHloCompilerCompileCallArgs { + string compile_options = 1; + NameAttrList function = 2; + // TODO: b/489869472 - Remove this field once it is no longer used. + repeated string xla_arguments = 3 [deprecated = true]; + repeated tf2xla.XlaArgumentProto xla_args = 4; +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/tf_to_hlo_compiler.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tf_to_hlo_compiler.h new file mode 100644 index 0000000000000000000000000000000000000000..ee869810ccb6f94bbadf7be4aed95410c304e708 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/tf_to_hlo_compiler.h @@ -0,0 +1,52 @@ +/* Copyright 2022 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_TF_TO_HLO_COMPILER_H_ +#define TENSORFLOW_COMPILER_JIT_TF_TO_HLO_COMPILER_H_ + +#include +#include + +#include "tensorflow/compiler/tf2xla/xla_compiler.h" +#include "tensorflow/core/framework/op_kernel.h" + +namespace tensorflow { + +class TfToHloCompiler { + public: + TfToHloCompiler() = default; + virtual ~TfToHloCompiler() = default; + + // Compiles a Tensorflow `function` to an HloModuleProto stored in the + // XlaCompilationResult pointed to by `result`. + virtual absl::Status Compile(const XlaCompiler::CompileOptions& options, + const NameAttrList& function, + absl::Span args, + XlaCompilationResult* result) = 0; + + // Compiles a Tensorflow single op to an HloModuleProto stored in the + // XlaCompilationResult pointed to by `result`. + virtual absl::Status CompileSingleOp( + const XlaCompiler::CompileOptions& options, const OpKernelContext* ctx, + absl::Span args, XlaCompilationResult* result) = 0; + + private: + TfToHloCompiler(const TfToHloCompiler&) = delete; + void operator=(const TfToHloCompiler&) = delete; +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_TF_TO_HLO_COMPILER_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/variable_info.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/variable_info.cc new file mode 100644 index 0000000000000000000000000000000000000000..1ae0d3f9f6789283ea79c5aed869779f08576c2b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/variable_info.cc @@ -0,0 +1,78 @@ +/* Copyright 2023 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/compiler/jit/variable_info.h" + +#include +#include +#include +#include +#include + +#include "tensorflow/core/framework/op.h" +#include "tensorflow/core/framework/tensor.h" +#include "tensorflow/core/lib/core/errors.h" +#include "tensorflow/core/lib/core/refcount.h" +#include "tsl/platform/status.h" + +namespace tensorflow { + +VariableInfo::VariableInfo( + int index, absl::string_view name, Var* var, + const std::optional& definition_stack_trace) + : index_(index), + name_(name), + var_(var), + definition_stack_trace_(definition_stack_trace) {} + +VariableInfo::VariableInfo(VariableInfo&& other) + : index_(other.index_), + var_(other.var_), + definition_stack_trace_(other.definition_stack_trace_), + lock_held_(other.lock_held_) { + other.index_ = -1; + other.var_ = nullptr; +} + +VariableInfo& VariableInfo::operator=(VariableInfo&& other) { + index_ = other.index_; + var_ = other.var_; + lock_held_ = other.lock_held_; + definition_stack_trace_ = other.definition_stack_trace_; + + other.index_ = -1; + other.var_ = nullptr; + + return *this; +} + +VariableInfo::~VariableInfo() { + // Release the variable's lock if we hold it. Ensures that the lock is + // released even on error. It does not matter in what order we release the + // locks. + if (var()) { + if (lock_held()) { + var()->mu()->unlock(); + } + if (shared_lock_held()) { + var()->mu()->unlock_shared(); + } + + // Unref the variable so it can be released by ResourceManager. + var()->Unref(); + } +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/variable_info.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/variable_info.h new file mode 100644 index 0000000000000000000000000000000000000000..6f5bebd547f52e8ef1fcf9b696653243eec6a121 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/variable_info.h @@ -0,0 +1,95 @@ +/* Copyright 2023 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_VARIABLE_INFO_H_ +#define TENSORFLOW_COMPILER_JIT_VARIABLE_INFO_H_ + +#include +#include +#include +#include + +#include "tensorflow/core/framework/resource_var.h" +#include "tensorflow/core/framework/tensor.h" +#include "tensorflow/core/lib/core/status.h" +#include "tensorflow/core/platform/thread_annotations.h" + +namespace tensorflow { + +// Information about the state of a variable passed as input to the _XlaCompile +// and _XlaRun operators. Unlocks the resource variable and decrements its +// refcount on destruction. +class VariableInfo { + public: + explicit VariableInfo(int index, absl::string_view name, Var* var, + const std::optional& + definition_stack_trace = std::nullopt); + VariableInfo(VariableInfo&& other); + + VariableInfo& operator=(VariableInfo&& other); + + VariableInfo(const VariableInfo&) = delete; + VariableInfo& operator=(const VariableInfo&) = delete; + + // The index of the DT_RESOURCE input to the _XlaCompile/_XlaRun operator. + // Note that the indices can be different between _XlaCompile and _XlaRun. + int index() const { return index_; } + + // A pointer to the resource variable. May be null if this VariableInfo is + // "empty", i.e. it does not track a resource variable. + Var* var() const { return var_; } + + // Returns the variable name. + absl::string_view name() const { return name_; } + + // Returns true if the resource variable lock was successfully acquired by + // this thread. + bool lock_held() const { return lock_held_; } + void set_lock_held() { lock_held_ = true; } + + // Returns true if the resource variable reader lock was successfully acquired + // by this thread. + bool shared_lock_held() const { return shared_lock_held_; } + void set_shared_lock_held() { shared_lock_held_ = true; } + + bool read_only() const { return read_only_; } + void set_read_only() { read_only_ = true; } + + const std::optional& definition_stack_trace() const { + return definition_stack_trace_; + } + + ~VariableInfo(); + + private: + int index_; + std::string name_; + Var* var_; + std::optional definition_stack_trace_; + + // We can't use a optional here because it confuses the compiler's + // thread safety analysis. Instead we use a boolean flag and release the lock + // in the VariableInfo destructor. + bool lock_held_ = false; + bool shared_lock_held_ = false; + + // Whether this variable is going to be mutated. Left false if the caller + // doesn't provide this information. + bool read_only_ = false; +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_VARIABLE_INFO_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/variable_info_util.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/variable_info_util.cc new file mode 100644 index 0000000000000000000000000000000000000000..4a74f4643d4aff65487f4c60c0dfc5f16bfd0fed --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/variable_info_util.cc @@ -0,0 +1,187 @@ +/* Copyright 2023 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/compiler/jit/variable_info_util.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "absl/algorithm/container.h" +#include "tensorflow/core/framework/op.h" +#include "tensorflow/core/framework/op_kernel.h" +#include "tensorflow/core/framework/resource_handle.h" +#include "tensorflow/core/framework/resource_mgr.h" +#include "tensorflow/core/framework/tensor.h" +#include "tensorflow/core/lib/core/errors.h" +#include "tensorflow/core/lib/core/refcount.h" +#include "tensorflow/core/platform/errors.h" +#include "tsl/platform/status.h" + +namespace tensorflow { + +absl::Status GetVariableInfosFromInputs(ResourceMgr* rm, DeviceBase* dev, + absl::Span inputs, + absl::Span variable_indices, + std::vector* result) { + return GetVariableInfosFromInputs(rm, dev, inputs, variable_indices, nullptr, + result); +} + +absl::Status GetVariableInfosFromInputs(ResourceMgr* rm, DeviceBase* dev, + absl::Span inputs, + absl::Span variable_indices, + const std::set* variables_updated, + std::vector* result) { + result->clear(); + result->reserve(variable_indices.size()); + for (int var_idx : variable_indices) { + Var* variable = nullptr; + if (inputs[var_idx]->NumElements() == 0) { + return absl::InvalidArgumentError( + absl::StrCat("Empty resource tensor passed at index ", var_idx, + " to GetVariableInfosFromInputs.")); + } + const ResourceHandle& handle = inputs[var_idx]->flat()(0); + if (handle.device() != dev->attributes().name()) { + std::string definition_location = + DefinitionLocationMsg(handle.definition_stack_trace()); + return absl::InvalidArgumentError(absl::StrCat( + "Trying to access resource ", handle.name(), definition_location, + " located in device ", handle.device(), " from device ", + dev->attributes().name(), + "\n Cf. " + "https://www.tensorflow.org/xla/" + "known_issues#tfvariable_on_a_different_device")); + } + TF_RETURN_IF_ERROR(rm->LookupOrCreate( + handle.container(), handle.name(), &variable, [](Var** ptr) { + // This var is uninitialized for now. + *ptr = new Var(DT_INVALID); + return absl::OkStatus(); + })); + VariableInfo& variable_info = result->emplace_back( + var_idx, handle.name(), variable, handle.definition_stack_trace()); + if (variables_updated != nullptr && + variables_updated->find(var_idx) == variables_updated->end()) { + variable_info.set_read_only(); + } + } + return absl::OkStatus(); +} + +absl::Status LockVariables(absl::Span variables) { + std::vector lock_order(variables.size()); + std::iota(lock_order.begin(), lock_order.end(), 0); + + // VariableInfoComparator orders all empty VariableInfo instances as + // equivalent so it looks like we may want to stable sort these to maintain a + // deterministic order between the empty VariableInfo instances. However + // since we're sorting by pointer value the sort is pretty non-deterministic + // anyway so we don't bother using std::stable_sort for now. + absl::c_sort(lock_order, [&](int a, int b) { + if (variables[a]->var() && variables[b]->var()) { + return variables[a]->var()->mu() < variables[b]->var()->mu(); + } + + // Move all the empty VariableInfo instances to the end. + return variables[a]->var() != nullptr; + }); + + mutex* prev = nullptr; + for (int i : lock_order) { + Var* variable = variables[i]->var(); + if (variable == nullptr) { + // All empty VariableInfo instances are at the end of the order + // so we're done. + break; + } + mutex* mu = variable->mu(); + if (prev == mu) { + // It is an error to pass the same variable handle twice to the same XLA + // cluster because we would not handle variable updates correctly. Any + // locks we have already acquired will be released when the VariableInfo + // objects are destroyed. + // TODO(b/128495870) Add support for passing aliased resource variables. + return absl::UnimplementedError( + "Duplicate variable passed to XLA cluster"); + } + if (variables[i]->read_only()) { + VLOG(4) << "Acquiring reader lock for variable " + << reinterpret_cast(variable); + mu->lock_shared(); + variables[i]->set_shared_lock_held(); + } else { + VLOG(4) << "Acquiring lock for variable " + << reinterpret_cast(variable); + mu->lock(); + variables[i]->set_lock_held(); + } + prev = mu; + } + VLOG(4) << "Finished acquiring variable locks."; + return absl::OkStatus(); +} + +absl::Status LockVariables(absl::Span variables) { + std::vector variable_ptrs; + variable_ptrs.reserve(variables.size()); + for (auto& var : variables) { + variable_ptrs.push_back(&var); + } + return LockVariables(absl::MakeSpan(variable_ptrs)); +} + +absl::Status SnapshotResourceVariables( + OpKernelContext* ctx, absl::Span variable_indices, + absl::Span variable_infos, + ResourceVarsSnapshot* result) { + for (int i = 0, end = variable_indices.size(); i < end; i++) { + Var* var = variable_infos[i].var(); + (*result)[variable_indices[i]] = + var ? std::make_optional(*var->tensor()) : std::nullopt; + } + return absl::OkStatus(); +} + +std::vector GetResourceVariableIndicesFromContext(OpKernelContext* ctx) { + std::vector out; + for (int64_t i = 0; i < ctx->num_inputs(); i++) { + if (ctx->input(i).dtype() == DT_RESOURCE) { + out.push_back(i); + } + } + return out; +} + +absl::Status CreateVariableInfoLookup( + absl::Span variable_args, + absl::flat_hash_map& variable_info_lookup) { + for (const VariableInfo& info : variable_args) { + if (!(!info.var() || info.lock_held() || info.shared_lock_held())) { + return absl::InternalError( + "Need to hold the lock on resource variables " + "before calling BuildXlaCompilerArguments"); + } + variable_info_lookup.emplace(info.index(), &info); + } + return absl::OkStatus(); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/variable_info_util.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/variable_info_util.h new file mode 100644 index 0000000000000000000000000000000000000000..4be1e40a7d58758f41aed801bc67381d2471a255 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/variable_info_util.h @@ -0,0 +1,93 @@ +/* Copyright 2023 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_VARIABLE_INFO_UTIL_H_ +#define TENSORFLOW_COMPILER_JIT_VARIABLE_INFO_UTIL_H_ + +#include +#include +#include +#include + +#include "tensorflow/compiler/jit/variable_info.h" +#include "tensorflow/core/framework/resource_mgr.h" +#include "tensorflow/core/framework/tensor.h" +#include "tensorflow/core/lib/core/status.h" +#include "tensorflow/core/platform/thread_annotations.h" + +namespace tensorflow { + +// Snapshot of resource variables for a TF kernel invocation, mapping from +// parameter number to values at execution time. If the resource variable is not +// initialized, the value will not be present. +using ResourceVarsSnapshot = absl::flat_hash_map>; + +// Takes a snapshot of the values of resource variable arguments, whose indices +// are specified in `variable_indices` argument. We snapshot tensors that back +// resource variables since concurrent updates may modify the shape, and it is +// important that the shapes used for compilation match the true shapes of the +// buffers. +// +// We snapshot the entire set of resource variables as one atomic operation. +// This models Read->* dependencies between resource variable operations. See +// jit/resource_operation_safety_analysis for details. +absl::Status SnapshotResourceVariables( + OpKernelContext* ctx, absl::Span variable_indices, + absl::Span variable_infos, + ResourceVarsSnapshot* result); + +// Acquires the mutexes for all the variables in `variables` using a +// deadlock-safe protocol (acquire the mutexes in increasing-address order). +// +// `variables` is allowed to contain instances that don't track a resource +// variable (i.e. variables[i].var() can be null for some i). +// +// If the variable is read_only(), only acquires reader locks. +absl::Status LockVariables(absl::Span variables) + TF_EXCLUSIVE_LOCK_FUNCTION(); +absl::Status LockVariables(absl::Span variables) + TF_EXCLUSIVE_LOCK_FUNCTION(); + +// Returns a vector of VariableInfo instances for the resource variable inputs, +// given that *all* inputs are in `inputs`. The input indices for the resource +// variable inputs are in `variable_indices`. +// +// When using the VariableInfos generated by this version, all variables would +// be writer-locked. +absl::Status GetVariableInfosFromInputs(ResourceMgr* rm, DeviceBase* dev, + absl::Span inputs, + absl::Span variable_indices, + std::vector* result); + +// variables_updated is a set containing the indices of the variables that are +// going to be mutated. If variables_updated is empty, then in LockVariables all +// variables would only be reader-locked. If variables_updated is null, then we +// consider this information unknown and will acquire writer-lock for all +// variables. +absl::Status GetVariableInfosFromInputs(ResourceMgr* rm, DeviceBase* dev, + absl::Span inputs, + absl::Span variable_indices, + const std::set* variables_updated, + std::vector* result); + +std::vector GetResourceVariableIndicesFromContext(OpKernelContext* ctx); + +absl::Status CreateVariableInfoLookup( + absl::Span variable_args, + absl::flat_hash_map& variable_info_lookup); + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_VARIABLE_INFO_UTIL_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_activity.proto b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_activity.proto new file mode 100644 index 0000000000000000000000000000000000000000..f31f1031cbac8942f69a699807b46f8ae33b8a67 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_activity.proto @@ -0,0 +1,123 @@ +/* Copyright 2019 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. +==============================================================================*/ + +syntax = "proto3"; + +package tensorflow; + +import "tensorflow/core/protobuf/config.proto"; + +// Summarizes the results of auto-clustering a TensorFlow graph. +// +// Next ID: 5 +message XlaAutoClusteringSummary { + // Represents a single element in a histogram of ops ("op" as in "TensorFlow + // operation"). + // + // Next ID: 3 + message OpAndCount { + // The TensorFlow operation (like MatMult, Add etc.) + string op = 1; + + // The number of times this occurs. + int32 count = 2; + } + + // Describes a single XLA cluster. + // + // Next ID: 4 + message Cluster { + string name = 1; + + // The number of nodes in the cluster. + int32 size = 2; + + // A histogram of the TF operations in this cluster. + repeated OpAndCount op_histogram = 3; + } + + // The number of nodes in the graph that are not inside an XLA cluster. + int32 unclustered_node_count = 1; + + // The number of nodes in the graph that are in an XLA cluster. + int32 clustered_node_count = 2; + + // All of the XLA clusters in the TF graph. + repeated Cluster clusters = 3; + + // A histogram of the TF operations that were not clustered. + repeated OpAndCount unclustered_op_histogram = 4; +} + +// Listeners listening for auto clustering events get messages of this type. +// +// Next ID: 4 +message XlaAutoClusteringActivity { + // The value of GlobalJitLevel, as determined by `GetGlobalJitLevelForGraph`. + // This determines if global auto-clustering is enabled. + OptimizerOptions.GlobalJitLevel global_jit_level = 1; + + // Whether --tf_xla_cpu_global_jit is enabled in TF_XLA_FLAGS. + bool cpu_global_jit_enabled = 2; + + XlaAutoClusteringSummary summary = 3; +} + +// Listeners listening for JIT compilation events get messages of this type. +// Each instance of XlaJitCompilationActivity corresponds to a single +// compilation of a single XLA cluster. E.g. if a graph has two clusters, A and +// B, and A is compiled 5 times and B is compiled 2 times then we will generate +// 7 instances of XlaJitCompilationActivity. +// +// Next ID: 6 +message XlaJitCompilationActivity { + string cluster_name = 1; + + // The number of time this cluster has been compiled. + int32 compile_count = 2; + + // Microseconds spent in the individual compilation being reported. + int64 compile_time_us = 3; + + // Total microseconds spent in (re-)compiling this cluster so far. + int64 cumulative_compile_time_us = 4; + + // Whether a persistent compilation cache entry was used. + bool used_persistent_cache = 5; +} + +// LINT.IfChange +// +// Used for logging situations seen in Tensorflow models being optimized that +// are known to not perform well with XLA. +// +// Next ID: 3 +message XlaOptimizationRemark { + // Next ID: 6 + enum Warning { + NONE = 0; + INACCURATE_OPERATION = 1; + SLOW_OPERATION = 2; + UNIMPLEMENTED_OPERATION = 3; + SLOW_IMAGE_RESIZE_DIMENSIONS = 4; + MEGAMORPHIC_FUNCTION = 5; + } + + Warning warning = 1; + + // Information such as which node was the problem. + string debug_information = 2; +} +// LINT.ThenChange(//tensorflow/compiler/jit/xla_activity_listener.h) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_activity_listener.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_activity_listener.cc new file mode 100644 index 0000000000000000000000000000000000000000..f55641e16be3d89d761c800ce432adbf3312195d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_activity_listener.cc @@ -0,0 +1,105 @@ +/* Copyright 2019 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/compiler/jit/xla_activity_listener.h" + +#include "absl/synchronization/mutex.h" +#include "tensorflow/compiler/jit/xla_activity.pb.h" +#include "tensorflow/core/lib/core/errors.h" +#include "tensorflow/core/platform/thread_annotations.h" + +namespace tensorflow { +namespace { +// The list of all registered `XlaActivityListener`s. +struct XlaActivityListenerList { + absl::Mutex mutex; + std::vector> listeners + TF_GUARDED_BY(mutex); +}; + +void FlushAllListeners(); + +XlaActivityListenerList* GetXlaActivityListenerList() { + static XlaActivityListenerList* listener_list = new XlaActivityListenerList; + static int unused = std::atexit(FlushAllListeners); + (void)unused; + return listener_list; +} + +template +absl::Status ForEachListener(FnTy fn) { + XlaActivityListenerList* listener_list = GetXlaActivityListenerList(); + absl::ReaderMutexLock reader_lock(listener_list->mutex); + + for (const std::unique_ptr& listener : + listener_list->listeners) { + TF_RETURN_IF_ERROR(fn(listener.get())); + } + + return absl::OkStatus(); +} + +void FlushAllListeners() { + absl::Status s = ForEachListener([](XlaActivityListener* listener) { + listener->Flush(); + return absl::OkStatus(); + }); + CHECK(s.ok()); +} +} // namespace + +absl::Status BroadcastXlaActivity( + XlaAutoClusteringActivity auto_clustering_activity) { + return ForEachListener([&](XlaActivityListener* listener) { + return listener->Listen(auto_clustering_activity); + }); +} + +absl::Status BroadcastXlaActivity( + XlaJitCompilationActivity jit_compilation_activity) { + return ForEachListener([&](XlaActivityListener* listener) { + return listener->Listen(jit_compilation_activity); + }); +} + +absl::Status BroadcastOptimizationRemark( + XlaOptimizationRemark optimization_remark) { + VLOG(2) << "OptimizationRemark: " << optimization_remark.DebugString(); + return ForEachListener([&](XlaActivityListener* listener) { + return listener->Listen(optimization_remark); + }); +} + +absl::Status BroadcastOptimizationRemark( + XlaOptimizationRemark::Warning optimization_warning, + std::string debug_information) { + XlaOptimizationRemark remark; + remark.set_warning(optimization_warning); + remark.set_debug_information(std::move(debug_information)); + return BroadcastOptimizationRemark(std::move(remark)); +} +void RegisterXlaActivityListener( + std::unique_ptr listener) { + XlaActivityListenerList* listener_list = GetXlaActivityListenerList(); + absl::WriterMutexLock writer_lock(listener_list->mutex); + + listener_list->listeners.push_back(std::move(listener)); +} + +void XlaActivityListener::Flush() {} + +XlaActivityListener::~XlaActivityListener() {} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_activity_listener.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_activity_listener.h new file mode 100644 index 0000000000000000000000000000000000000000..4d07f8c4bc9fc2f775245fe976bb4cff6341a18b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_activity_listener.h @@ -0,0 +1,77 @@ +/* Copyright 2019 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_JIT_XLA_ACTIVITY_LISTENER_H_ +#define TENSORFLOW_COMPILER_JIT_XLA_ACTIVITY_LISTENER_H_ + +#include + +#include "tensorflow/compiler/jit/xla_activity.pb.h" +#include "tensorflow/core/lib/core/status.h" + +namespace tensorflow { +// Broadcast `auto_clustering_activity` to all the registered listeners. +absl::Status BroadcastXlaActivity( + XlaAutoClusteringActivity auto_clustering_activity); + +// Broadcast `jit_compilation_activity` to all the registered listeners. +absl::Status BroadcastXlaActivity( + XlaJitCompilationActivity jit_compilation_activity); + +// Broadcast `jit_compilation_activity` to all the registered listeners. +absl::Status BroadcastOptimizationRemark( + XlaOptimizationRemark optimization_remark); + +// LINT.IfChange +// Called after TensorFlow realizes possible lost performance. The parameters in +// this should match all of the values in the XlaOptimizationRemark proto. +absl::Status BroadcastOptimizationRemark( + XlaOptimizationRemark::Warning optimization_warning, + std::string debug_information); + +// LINT.ThenChange(//tensorflow/compiler/jit/xla_activity.proto) + +// Various components of the system can subclass XlaActivityListener to +// notifications on auto-clustering and JIT compilation events. +// +// Subclasses of XlaActivityListener must be thread safe. +class XlaActivityListener { + public: + // Called after TensorFlow auto-clusters a graph. + virtual absl::Status Listen( + const XlaAutoClusteringActivity& auto_clustering_activity) = 0; + + // Called after TensorFlow JIT compiles an XLA cluster. + virtual absl::Status Listen( + const XlaJitCompilationActivity& jit_compilation_activity) = 0; + + // Called after TensorFlow realizes possible lost performance. + virtual absl::Status Listen( + const XlaOptimizationRemark& optimization_remark) = 0; + + // Called at program exit in best-effort manner to give listeners a chance to + // flush their state. + // + // Default implementation is a no-op. + virtual void Flush(); + + virtual ~XlaActivityListener(); +}; + +// Registers an `XlaActivityListener`, which will be invoked on all subsequent +// `BroadcastXlaActivity` calls. +void RegisterXlaActivityListener(std::unique_ptr listener); +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_XLA_ACTIVITY_LISTENER_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_activity_listener_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_activity_listener_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..2142fa04e454448332cfcea7010a7760a5b4cc1c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_activity_listener_test.cc @@ -0,0 +1,199 @@ +/* Copyright 2019 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/compiler/jit/xla_activity_listener.h" + +#include + +#include "tensorflow/cc/framework/ops.h" +#include "tensorflow/cc/ops/array_ops.h" +#include "tensorflow/cc/ops/list_ops.h" +#include "tensorflow/cc/ops/standard_ops.h" +#include "tensorflow/compiler/jit/flags.h" +#include "tensorflow/core/common_runtime/direct_session.h" +#include "tensorflow/core/lib/core/status_test_util.h" +#include "tensorflow/core/platform/test.h" + +namespace tensorflow { +namespace { + +class TestListener : public XlaActivityListener { + public: + absl::Status Listen( + const XlaAutoClusteringActivity& auto_clustering_activity) override { + auto_clustering_activity_ = auto_clustering_activity; + return absl::OkStatus(); + } + + absl::Status Listen( + const XlaJitCompilationActivity& jit_compilation_activity) override { + jit_compilation_activity_ = jit_compilation_activity; + return absl::OkStatus(); + } + + absl::Status Listen( + const XlaOptimizationRemark& optimization_remark) override { + return absl::OkStatus(); + } + + ~TestListener() override {} + + const XlaAutoClusteringActivity& auto_clustering_activity() const { + return auto_clustering_activity_; + } + const XlaJitCompilationActivity& jit_compilation_activity() const { + return jit_compilation_activity_; + } + + private: + XlaAutoClusteringActivity auto_clustering_activity_; + XlaJitCompilationActivity jit_compilation_activity_; +}; + +class XlaActivityListenerTest : public ::testing::Test { + protected: + XlaActivityListenerTest() { + auto listener = std::make_unique(); + listener_ = listener.get(); + RegisterXlaActivityListener(std::move(listener)); + } + + TestListener* listener() const { return listener_; } + + private: + TestListener* listener_; +}; + +GraphDef CreateGraphDef() { + Scope root = Scope::NewRootScope().ExitOnError().WithAssignedDevice( + "/job:localhost/replica:0/task:0/device:CPU:0"); + Output a = ops::Placeholder(root.WithOpName("A"), DT_FLOAT); + for (int i = 0; i < 5; i++) { + a = ops::MatMul(root.WithOpName(absl::StrCat("matmul_", i)), a, a); + a = ops::Add(root.WithOpName(absl::StrCat("add_", i)), a, a); + } + + GraphDef graph_def; + root.graph()->ToGraphDef(&graph_def); + return graph_def; +} + +TEST_F(XlaActivityListenerTest, Test) { + GraphDef graph_def = CreateGraphDef(); + SessionOptions options; + options.config.mutable_graph_options() + ->mutable_optimizer_options() + ->set_global_jit_level(OptimizerOptions::ON_2); + std::unique_ptr session(NewSession(options)); + + TF_ASSERT_OK(session->Create(graph_def)); + + std::vector output_names = {std::string("add_4:0")}; + + Tensor tensor_2x2(DT_FLOAT, TensorShape({2, 2})); + for (int i = 0; i < 4; i++) { + tensor_2x2.matrix()(i / 2, i % 2) = 5 * i; + } + + Tensor tensor_3x3(DT_FLOAT, TensorShape({3, 3})); + for (int i = 0; i < 9; i++) { + tensor_3x3.matrix()(i / 3, i % 3) = 5 * i; + } + + std::vector> inputs_2x2 = {{"A", tensor_2x2}}; + + std::vector outputs; + TF_ASSERT_OK(session->Run(inputs_2x2, output_names, + /*target_tensor_names=*/{}, &outputs)); + + XlaAutoClusteringActivity expected_auto_clustering_activity; + protobuf::TextFormat::ParseFromString( + R"(global_jit_level: ON_2 +cpu_global_jit_enabled: true +summary { + unclustered_node_count: 4 + clustered_node_count: 14 + clusters { + name: "cluster_0" + size: 14 + op_histogram { + op: "Add" + count: 1 + } + op_histogram { + op: "Const" + count: 4 + } + op_histogram { + op: "MatMul" + count: 5 + } + op_histogram { + op: "Mul" + count: 4 + } + } + unclustered_op_histogram { + op: "NoOp" + count: 2 + } + unclustered_op_histogram { + op: "_Arg" + count: 1 + } + unclustered_op_histogram { + op: "_Retval" + count: 1 + } +} +)", + &expected_auto_clustering_activity); + EXPECT_EQ(listener()->auto_clustering_activity().DebugString(), + expected_auto_clustering_activity.DebugString()); + + EXPECT_EQ(listener()->jit_compilation_activity().cluster_name(), "cluster_0"); + EXPECT_EQ(listener()->jit_compilation_activity().compile_count(), 1); + + int64_t first_compile_time = + listener()->jit_compilation_activity().compile_time_us(); + EXPECT_GT(first_compile_time, 0); + EXPECT_EQ(listener()->jit_compilation_activity().cumulative_compile_time_us(), + first_compile_time); + + std::vector> inputs_3x3 = {{"A", tensor_3x3}}; + + outputs.clear(); + for (int i = 0; i < 3; i++) { + TF_ASSERT_OK(session->Run(inputs_3x3, output_names, + /*target_tensor_names=*/{}, &outputs)); + } + + EXPECT_EQ(listener()->jit_compilation_activity().cluster_name(), "cluster_0"); + EXPECT_EQ(listener()->jit_compilation_activity().compile_count(), 2); + + EXPECT_GT(listener()->jit_compilation_activity().compile_time_us(), 0); + EXPECT_EQ(listener()->jit_compilation_activity().cumulative_compile_time_us(), + first_compile_time + + listener()->jit_compilation_activity().compile_time_us()); +} + +} // namespace +} // namespace tensorflow + +int main(int argc, char** argv) { + tensorflow::GetMarkForCompilationPassFlags()->tf_xla_cpu_global_jit = true; + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_activity_logging_listener.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_activity_logging_listener.cc new file mode 100644 index 0000000000000000000000000000000000000000..0a58b8c33111ce4897da2da7e7d37a71a3519f7e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_activity_logging_listener.cc @@ -0,0 +1,80 @@ +/* Copyright 2019 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 "absl/memory/memory.h" +#include "tensorflow/compiler/jit/xla_activity.pb.h" +#include "tensorflow/compiler/jit/xla_activity_listener.h" + +namespace tensorflow { +namespace { + +// Listens to XLA activity and logs them using tensorflow::Logger. +class XlaActivityLoggingListener final : public XlaActivityListener { + public: + absl::Status Listen( + const XlaAutoClusteringActivity& auto_clustering_activity) override { + if (!IsEnabled()) { + VLOG(3) << "Logging XlaAutoClusteringActivity disabled"; + return absl::OkStatus(); + } + + return absl::OkStatus(); + } + + absl::Status Listen( + const XlaJitCompilationActivity& jit_compilation_activity) override { + if (!IsEnabled()) { + VLOG(3) << "Logging XlaJitCompilationActivity disabled"; + return absl::OkStatus(); + } + + return absl::OkStatus(); + } + + absl::Status Listen( + const XlaOptimizationRemark& optimization_remark) override { + if (!IsEnabled()) { + VLOG(3) << "Logging XlaJitCompilationActivity disabled"; + return absl::OkStatus(); + } + + return absl::OkStatus(); + } + + private: + bool IsEnabled() { + static bool result = ComputeIsEnabled(); + return result; + } + + bool ComputeIsEnabled() { + char* log_xla_activity = getenv("TF_LOG_XLA_ACTIVITY"); + if (log_xla_activity == nullptr) { + bool enabled_by_default = true; + return enabled_by_default; + } + + return absl::string_view(log_xla_activity) == "1"; + } +}; + +bool Register() { + RegisterXlaActivityListener(std::make_unique()); + return false; +} + +bool unused = Register(); +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_cluster_util.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_cluster_util.cc new file mode 100644 index 0000000000000000000000000000000000000000..5da667e97f5ea680b0ec6013ce480ab151648c12 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_cluster_util.cc @@ -0,0 +1,648 @@ +/* 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 "tensorflow/compiler/jit/xla_cluster_util.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "absl/algorithm/container.h" +#include "absl/container/flat_hash_map.h" +#include "absl/container/flat_hash_set.h" +#include "absl/container/inlined_vector.h" +#include "absl/log/check.h" +#include "absl/log/log.h" +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/match.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" +#include "tensorflow/compiler/jit/flags.h" +#include "xla/service/graphcycles/graphcycles.h" +#include "xla/status_macros.h" +#include "xla/tsl/platform/errors.h" +#include "xla/tsl/platform/statusor.h" +#include "tensorflow/core/common_runtime/function_body.h" +#include "tensorflow/core/common_runtime/optimization_registry.h" +#include "tensorflow/core/framework/attr_value_util.h" +#include "tensorflow/core/framework/bounds_check.h" +#include "tensorflow/core/framework/function.h" +#include "tensorflow/core/framework/node_def.pb.h" +#include "tensorflow/core/framework/node_def_util.h" +#include "tensorflow/core/framework/op.h" +#include "tensorflow/core/framework/op_def_builder.h" +#include "tensorflow/core/framework/types.h" +#include "tensorflow/core/graph/algorithm.h" +#include "tensorflow/core/graph/control_flow.h" +#include "tensorflow/core/graph/edgeset.h" +#include "tensorflow/core/graph/graph.h" +#include "tensorflow/core/lib/gtl/cleanup.h" +#include "tensorflow/core/lib/strings/proto_serialization.h" +#include "tensorflow/core/platform/errors.h" +#include "tensorflow/core/platform/hash.h" +#include "tensorflow/core/platform/types.h" +#include "tensorflow/core/public/session_options.h" +#include "tensorflow/core/util/device_name_utils.h" +#include "tensorflow/core/util/xla_config_registry.h" + +namespace tensorflow { + +const char* const kXlaClusterAttr = "_XlaCluster"; +const char* const kXlaCompileTimeConstantInputsAttr = + "_XlaCompileTimeConstantInputs"; + +namespace { +// Returns a string describing how an edge from src to dst would +// create a cycle. +std::string DescribeCycle(const xla::GraphCycles* cycles, const Graph& graph, + int src, int dst) { + int32_t max_path_size = graph.num_node_ids() + 1; + std::vector path(max_path_size); + int32_t path_size = cycles->FindPath(dst, src, max_path_size, path.data()); + if (path_size == 0) { + return ""; + } + + auto node_name = [&graph](int node_id) { + if (!FastBoundsCheck(node_id, graph.num_node_ids())) { + return std::string("(null)"); + } + auto* node = graph.FindNodeId(node_id); + if (node == nullptr) { + return std::string("(null)"); + } + return node->name(); + }; + + std::string description; + absl::StrAppend(&description, "Edge from ", node_name(src), " to ", + node_name(dst), " would create a cycle.\n"); + path.resize(path_size); + for (int32_t node_id : path) { + std::string ascii_art; + if (node_id == dst) { + ascii_art = "+-> "; + } else if (node_id != src) { + ascii_art = "| "; + } else { + ascii_art = "+-- "; + } + absl::StrAppend(&description, ascii_art, node_name(node_id), "\n"); + } + return description; +} + +bool AlwaysForwardsRefInput(const Node& node) { return node.IsIdentity(); } + +} // namespace + +bool HasForwardedRefInput(const Node& node) { + if (AlwaysForwardsRefInput(node)) { + for (const Edge* incoming_edge : node.in_edges()) { + if (incoming_edge->IsControlEdge()) { + continue; + } + + Node* incoming_node = incoming_edge->src(); + if (IsRefType(incoming_node->output_type(incoming_edge->src_output()))) { + VLOG(2) << "Node " << node.def().ShortDebugString() << " has ref input " + << incoming_node->name() << " " << incoming_node->type_string(); + return true; + } + } + } + return false; +} + +absl::StatusOr CreateCycleDetectionGraph(const Graph* graph, + xla::GraphCycles* cycles) { + for (int i = 0; i < graph->num_node_ids(); ++i) { + // We rely on the node IDs in the cycle detection graph being consecutive + // integers starting from 0. + CHECK_EQ(i, cycles->NewNode()); + } + + // Compute the loop structure of the graph. + std::vector control_flow_info; + TF_RETURN_IF_ERROR(BuildControlFlowInfo(graph, &control_flow_info)); + + // The clustering code must avoid adding cycles to the graph to prevent + // deadlock. However, the graph may contain loops, which would trigger the + // cycle detection code. To handle loops, we alter the structure of the cycle + // detection graph, disconnecting each loop from the enclosing graph. + // Specifically, we: + // * add a new "frame" node for each loop. + // * replace edges to "Enter" nodes, and edges from "Exit" nodes with edges + // to/from the corresponding frame node. In essence, we collapse the loop + // into a single node for the purpose of cycle detection in the enclosing + // graph. + // * the body of the loop should now be disconnected from the rest of the + // graph; we make it acyclic by breaking loop backedges (edges outgoing from + // "NextIteration" nodes. + + // Map from frame name strings to node IDs in the cycle detection graph. + std::unordered_map frame_nodes; + + // Get the cycle graph node ID for frame 'frame_name', or add one if none + // exists. + auto GetOrAddFrameNodeId = [&frame_nodes, + cycles](const std::string& frame_name) { + int& frame_id = frame_nodes.emplace(frame_name, -1).first->second; + if (frame_id < 0) { + // The emplace succeeded; we have not allocated a frame node yet. + frame_id = cycles->NewNode(); + } + return frame_id; + }; + + for (Edge const* edge : graph->edges()) { + if (edge->dst()->IsEnter() || edge->src()->IsExit()) { + const char* src_type = "pre-enter"; + const char* dst_type = "post-exit"; + int src = edge->src()->id(); + int dst = edge->dst()->id(); + + if (edge->dst()->IsEnter()) { + // Lift edges to an "Enter" node to the corresponding frame node. + const std::string& frame_name = + control_flow_info[edge->dst()->id()].frame_name; + dst = GetOrAddFrameNodeId(frame_name); + dst_type = "frame"; + } + + if (edge->src()->IsExit()) { + // Lift edges from an "Exit" node to the corresponding frame node. + const std::string& frame_name = + control_flow_info[edge->src()->id()].frame_name; + src = GetOrAddFrameNodeId(frame_name); + src_type = "frame"; + } + + if (!cycles->InsertEdge(src, dst)) { + // TODO(b/127521408): We can probably handle this situation with a more + // sophisticated SCC based algorithm, but for now we bail out. + VLOG(1) << "Cycle detected when adding " << src_type << "->" << dst_type + << " edge: " << DescribeCycle(cycles, *graph, src, dst); + return false; + } + // Drop the original edge. + continue; + } + if (edge->src()->IsNextIteration()) { + // Break loop back-edges. + continue; + } + if (!cycles->InsertEdge(edge->src()->id(), edge->dst()->id())) { + // This should never happen. All cycles in the graph should contain + // a control flow operator. + return absl::InternalError(absl::StrCat( + "Found cycle in graph without control flow operator during XLA " + "compilation: ", + DescribeCycle(cycles, *graph, edge->src()->id(), edge->dst()->id()))); + } + } + + return true; +} + +std::optional GetXlaClusterForNode(const Node& node) { + const AttrValue* attr_value = node.attrs().Find(kXlaClusterAttr); + if (attr_value == nullptr) { + return std::nullopt; + } + absl::Status s = AttrValueHasType(*attr_value, "string"); + if (!s.ok()) { + return std::nullopt; + } + return attr_value->s(); +} + +bool HasResourceInputOrOutput(const Node& node) { + return std::find(node.input_types().begin(), node.input_types().end(), + DT_RESOURCE) != node.input_types().end() || + std::find(node.output_types().begin(), node.output_types().end(), + DT_RESOURCE) != node.output_types().end(); +} + +void RemoveFromXlaCluster(NodeDef* node_def) { + node_def->mutable_attr()->erase(kXlaClusterAttr); +} + +void RemoveFromXlaCluster(Node* node) { node->ClearAttr(kXlaClusterAttr); } + +namespace { +typedef xla_config_registry::XlaGlobalJitLevel XlaGlobalJitLevel; + +XlaGlobalJitLevel GetXlaGlobalJitLevel( + const OptimizerOptions::GlobalJitLevel& jit_level_in_session_opts) { + XlaGlobalJitLevel result; + + if (jit_level_in_session_opts == OptimizerOptions::DEFAULT) { + // To set compilation to be on by default, change the following line. + result.single_gpu = result.general = OptimizerOptions::OFF; + } else { + result.single_gpu = result.general = jit_level_in_session_opts; + } + + // If the flag tf_xla_auto_jit is a valid, non-DEFAULT setting, it overrides + // the setting in ConfigProto. + MarkForCompilationPassFlags* flags = GetMarkForCompilationPassFlags(); + if (flags->xla_auto_jit_flag.optimization_level_single_gpu != + OptimizerOptions::DEFAULT) { + result.single_gpu = static_cast( + flags->xla_auto_jit_flag.optimization_level_single_gpu); + } + if (flags->xla_auto_jit_flag.optimization_level_general != + OptimizerOptions::DEFAULT) { + result.general = static_cast( + flags->xla_auto_jit_flag.optimization_level_general); + } + + return result; +} + +int GetGpuNumber(const std::string& device_name) { + DeviceNameUtils::ParsedName parsed_name; + if (!DeviceNameUtils::ParseFullName(device_name, &parsed_name)) { + return -1; + } + + return parsed_name.type == DEVICE_GPU ? parsed_name.id : -1; +} +} // namespace + +bool IsSingleGpuGraph(const Graph& g) { + int gpus_seen = 0; + absl::flat_hash_set devices_seen; + + for (Node* n : g.op_nodes()) { + if (devices_seen.contains(n->assigned_device_name())) { + continue; + } + + int gpu_number = GetGpuNumber(n->assigned_device_name()); + if (gpu_number != -1) { + if (++gpus_seen > 1) { + return false; + } + } + + devices_seen.insert(n->assigned_device_name()); + } + + return gpus_seen == 1; +} + +OptimizerOptions::GlobalJitLevel GetGlobalJitLevelForGraph( + const GraphOptimizationPassOptions& options) { + OptimizerOptions::GlobalJitLevel jit_level_in_session_opts = + options.session_options->config.graph_options() + .optimizer_options() + .global_jit_level(); + XlaGlobalJitLevel xla_global_jit_level = + GetXlaGlobalJitLevel(jit_level_in_session_opts); + if (xla_global_jit_level.single_gpu == xla_global_jit_level.general) { + VLOG(4) << "GetGlobalJitLevelForGraph returning " + << xla_global_jit_level.single_gpu; + return xla_global_jit_level.single_gpu; + } + OptimizerOptions::GlobalJitLevel result = + IsSingleGpuGraph(**options.graph) ? xla_global_jit_level.single_gpu + : xla_global_jit_level.general; + VLOG(4) << "GetGlobalJitLevelForGraph returning " << result; + return result; +} + +bool MayCallFunction(const Node& n, const FunctionLibraryDefinition* flib_def) { + if (flib_def->Contains(n.type_string())) { + return true; + } + + // This is a conservative check: there may be nodes with a `func` + // attribute that do not make function calls. + return absl::c_any_of( + n.def().attr(), + [](const std::pair& name_attr_pair) { + return name_attr_pair.second.has_func(); + }); +} +bool IsShapeConsumerOp(const Node& node) { + return node.type_string() == "Shape" || node.type_string() == "Rank" || + node.type_string() == "Size"; +} + +namespace { +struct ClusterInfo { + int size; + + // Maps op names to the number of times they appear in the cluster. + absl::flat_hash_map op_histogram; +}; + +void HistogramMapToRepeatedOpAndCount( + protobuf::RepeatedPtrField* result, + const absl::flat_hash_map& histogram) { + for (const auto& pair : histogram) { + XlaAutoClusteringSummary::OpAndCount* new_entry = result->Add(); + new_entry->set_op(std::string(pair.first)); + new_entry->set_count(pair.second); + } + + absl::c_sort(*result, [](const XlaAutoClusteringSummary::OpAndCount& a, + const XlaAutoClusteringSummary::OpAndCount& b) { + return a.op() < b.op(); + }); +} + +void ClusterInfoToProtobuf(XlaAutoClusteringSummary::Cluster* result, + absl::string_view name, const ClusterInfo& info) { + result->set_name(std::string(name)); + result->set_size(info.size); + HistogramMapToRepeatedOpAndCount(result->mutable_op_histogram(), + info.op_histogram); +} +} // namespace + +XlaAutoClusteringSummary GetXlaAutoClusteringSummary(const Graph& graph) { + absl::flat_hash_map cluster_name_to_info; + XlaAutoClusteringSummary result; + + absl::flat_hash_map unclustered_op_histogram; + + for (Node* n : graph.nodes()) { + std::optional cluster_name = GetXlaClusterForNode(*n); + if (cluster_name) { + result.set_clustered_node_count(result.clustered_node_count() + 1); + ClusterInfo* info = &cluster_name_to_info[*cluster_name]; + info->size++; + info->op_histogram[n->type_string()]++; + } else { + result.set_unclustered_node_count(result.unclustered_node_count() + 1); + unclustered_op_histogram[n->type_string()]++; + } + } + + for (const auto& pair : cluster_name_to_info) { + XlaAutoClusteringSummary::Cluster* new_cluster = result.add_clusters(); + ClusterInfoToProtobuf(new_cluster, pair.first, pair.second); + } + + absl::c_sort(*result.mutable_clusters(), + [&](const XlaAutoClusteringSummary::Cluster& a, + const XlaAutoClusteringSummary::Cluster& b) { + return a.name() < b.name(); + }); + + HistogramMapToRepeatedOpAndCount(result.mutable_unclustered_op_histogram(), + unclustered_op_histogram); + + return result; +} + +namespace { +using CallTargetListTy = absl::InlinedVector; + +CallTargetListTy GetCallTargetListFromNode( + const Node& n, FunctionLibraryRuntime* lib_runtime) { + const FunctionLibraryDefinition& flib_def = + *lib_runtime->GetFunctionLibraryDefinition(); + if (flib_def.Find(n.type_string())) { + NameAttrList callee; + callee.set_name(n.type_string()); + *callee.mutable_attr() = n.def().attr(); + return {callee}; + } + + CallTargetListTy result; + for (const auto& name_attr_pair : n.attrs()) { + const AttrValue& attr_value = name_attr_pair.second; + if (attr_value.value_case() == AttrValue::kFunc) { + result.push_back(attr_value.func()); + } else if (attr_value.value_case() == AttrValue::kList) { + result.insert(result.end(), attr_value.list().func().begin(), + attr_value.list().func().end()); + } + } + + return result; +} + +enum class Direction { kForward, kBackward }; + +absl::Status GetNodesRelatedToRefVariablesInDirection( + const Graph& graph, FunctionLibraryRuntime* lib_runtime, + Direction direction, int depth, absl::flat_hash_set* result); + +absl::StatusOr DoesAnyCalleeHaveRefNodes( + const CallTargetListTy& call_target_list, + FunctionLibraryRuntime* lib_runtime, Direction direction, int depth) { + const int kMaxDepth = 10; + + if (depth == kMaxDepth && !call_target_list.empty()) { + // Conservative answer to avoid recursing too much. + return true; + } + + absl::flat_hash_set callee_ref_nodes; + for (const NameAttrList& call_target : call_target_list) { + const OpRegistrationData* op_reg; + if (OpRegistry::Global()->LookUp(call_target.name(), &op_reg).ok()) { + const OpDef& op = op_reg->op_def; + if (absl::c_any_of(op.output_arg(), [](const OpDef::ArgDef arg) { + return arg.is_ref(); + })) { + return true; + } + continue; + } + + callee_ref_nodes.clear(); + FunctionLibraryRuntime::Handle handle; + if (!lib_runtime + ->Instantiate(call_target.name(), AttrSlice(&call_target.attr()), + &handle) + .ok()) { + VLOG(2) << "Could not find " << call_target.name() + << " in the function library."; + // Since we don't know the semantic of `n` we don't know if this is an + // error. We return true to signal a conservative answer. + return true; + } + + auto release_handle_on_return = + gtl::MakeCleanup([&] { CHECK_OK(lib_runtime->ReleaseHandle(handle)); }); + + const FunctionBody* fbody = lib_runtime->GetFunctionBody(handle); + TF_RETURN_IF_ERROR(GetNodesRelatedToRefVariablesInDirection( + *fbody->graph, lib_runtime, direction, depth + 1, &callee_ref_nodes)); + + // We could possibly use something cheaper than + // GetNodesRelatedToRefVariablesInDirection since we only care about the + // size of `callee_ref_nodes` but for now we don't ceare. + if (!callee_ref_nodes.empty()) { + return true; + } + } + + return false; +} + +// Helper for GetNodesRelatedToRefVariables that traverses the graph in one +// direction. +absl::Status GetNodesRelatedToRefVariablesInDirection( + const Graph& graph, FunctionLibraryRuntime* lib_runtime, + Direction direction, int depth, absl::flat_hash_set* result) { + std::vector nodes_in_order; + if (direction == Direction::kForward) { + GetReversePostOrder(graph, &nodes_in_order, + /*stable_comparator=*/NodeComparatorName()); + } else { + GetPostOrder(graph, &nodes_in_order, + /*stable_comparator=*/NodeComparatorName()); + } + + size_t old_result_size; + int iterations = 0; + + const int kMaxIterations = 10 * 1000; + + std::vector callee_has_ref_nodes_cache; + callee_has_ref_nodes_cache.resize(graph.num_node_ids()); + + auto does_callee_have_ref_nodes = [&](Node* n) -> absl::StatusOr { + if (iterations == 1) { + TF_ASSIGN_OR_RETURN( + bool callee_has_ref_nodes, + DoesAnyCalleeHaveRefNodes(GetCallTargetListFromNode(*n, lib_runtime), + lib_runtime, direction, depth)); + callee_has_ref_nodes_cache[n->id()] = callee_has_ref_nodes; + return callee_has_ref_nodes; + } else { + return {callee_has_ref_nodes_cache[n->id()]}; + } + }; + + do { + TF_RET_CHECK(iterations++ < kMaxIterations) << "infinite loop?"; + + old_result_size = result->size(); + for (Node* n : nodes_in_order) { + if (n->IsSource() || n->IsSink()) { + continue; + } + + bool inserted_n = false; + const EdgeSet& edges = + direction == Direction::kForward ? n->in_edges() : n->out_edges(); + for (const Edge* e : edges) { + if (result->contains(direction == Direction::kForward ? e->src() + : e->dst())) { + result->insert(n); + inserted_n = true; + break; + } + } + + if (inserted_n) { + continue; + } + + if (direction == Direction::kForward && + absl::c_any_of(n->output_types(), IsRefType)) { + result->insert(n); + continue; + } + + TF_ASSIGN_OR_RETURN(bool callee_has_ref_nodes, + does_callee_have_ref_nodes(n)); + if (callee_has_ref_nodes) { + result->insert(n); + continue; + } + } + + // Loop until convergence. + } while (result->size() != old_result_size); + + VLOG(2) << "# iterations = " << iterations; + + return absl::OkStatus(); +} + +// Sorts control inputs of a graphdef so that they are deterministically +// ordered. +void SortControlInputs(GraphDef* gdef) { + int64_t num_nodes = gdef->node_size(); + for (int64_t i = 0; i < num_nodes; ++i) { + NodeDef* node = gdef->mutable_node(i); + // Stable sort control inputs and leave the order of data inputs unchanged. + std::stable_sort(node->mutable_input()->begin(), + node->mutable_input()->end(), + [](const std::string& a, const std::string& b) { + bool a_is_control = absl::StartsWith(a, "^"); + bool b_is_control = absl::StartsWith(b, "^"); + return (!a_is_control && b_is_control) || + (a_is_control && b_is_control && a < b); + }); + } +} +} // namespace + +absl::StatusOr> GetNodesRelatedToRefVariables( + const Graph& graph, FunctionLibraryRuntime* lib_runtime) { + absl::flat_hash_set result; + TF_RETURN_IF_ERROR(GetNodesRelatedToRefVariablesInDirection( + graph, lib_runtime, Direction::kForward, 0, &result)); + TF_RETURN_IF_ERROR(GetNodesRelatedToRefVariablesInDirection( + graph, lib_runtime, Direction::kBackward, 0, &result)); + + VLOG(1) << "GetNodesRelatedToRefVariables() found " << result.size() + << " nodes"; + return result; +} + +absl::StatusOr SerializeGraphDeterministic(const Graph& graph) { + GraphDef def; + graph.ToGraphDef(&def); + + // Before serialization, sort each node's control inputs to achieve + // determinism. Sorting control inputs could help (but not necessarily) create + // a deterministic serialization and fingerprint. Other sources of + // nondeterminism include unstable node ordering. + SortControlInputs(&def); + + std::string s; + if (!SerializeToStringDeterministic(def, &s)) { + return absl::InternalError("Failed to serialize graphdef."); + } + return s; +} + +absl::StatusOr FingerprintGraph(const Graph& graph) { + TF_ASSIGN_OR_RETURN(std::string serialized, + SerializeGraphDeterministic(graph)); + return Hash64(serialized.data(), serialized.size()); +} + +// Register a callback for querying XlaGlobalJitLevel. +REGISTER_XLA_CONFIG_GETTER(GetXlaGlobalJitLevel); + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_cluster_util.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_cluster_util.h new file mode 100644 index 0000000000000000000000000000000000000000..1e40fa18d267d1485d1bc5ffe1fcd029ce2a3d76 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_cluster_util.h @@ -0,0 +1,113 @@ +/* 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. +==============================================================================*/ + +// Contains utilities for clustering compilable graph nodes via XLA. + +#ifndef TENSORFLOW_COMPILER_JIT_XLA_CLUSTER_UTIL_H_ +#define TENSORFLOW_COMPILER_JIT_XLA_CLUSTER_UTIL_H_ + +#include + +#include "absl/container/flat_hash_map.h" +#include "absl/container/flat_hash_set.h" +#include "absl/status/statusor.h" +#include "absl/types/optional.h" +#include "tensorflow/compiler/jit/xla_activity.pb.h" +#include "xla/service/graphcycles/graphcycles.h" +#include "tensorflow/core/common_runtime/optimization_registry.h" +#include "tensorflow/core/graph/algorithm.h" + +namespace tensorflow { + +// The attribute that marks nodes to be grouped into functions by the +// encapsulate subgraphs pass. +extern const char* const kXlaClusterAttr; + +// The attribute that marks certain inputs to a Node as required to be a +// constant at compile time. If this attribute is present then the +// CompileTimeConstantInput information in the corresponding XlaOpKernel is +// ignored. +// +// The value for this attribute, if present, has to be a list of strings naming +// the inputs to the node that must be constant. +extern const char* const kXlaCompileTimeConstantInputsAttr; + +using OrderedNodeSet = std::set; + +// Returns true if `node` has a ref tensor input that it forwards to its output. +bool HasForwardedRefInput(const Node& node); + +// Creates a graph representation to enable cycle detection when clustering. +// This representation handles loops in graph by disconnecting each loop from +// the enclosing graph. +// +// Returns true for success and false for valid graphs that we can't handle yet +// (b/127521408). +absl::StatusOr CreateCycleDetectionGraph(const Graph* graph, + xla::GraphCycles* cycles); + +// Returns the XLA cluster in which `node` is placed if it is in an XLA cluster, +// otherwise returns nullopt. +std::optional GetXlaClusterForNode(const Node& node); + +// Removes `node_def` its XLA cluster (by clearing its _XlaCluster attribute). +void RemoveFromXlaCluster(NodeDef* node_def); + +// Removes `node` its XLA cluster (by clearing its _XlaCluster attribute). +void RemoveFromXlaCluster(Node* node); + +// Returns true if `node` has a DT_RESOURCE typed input or output. +bool HasResourceInputOrOutput(const Node& node); + +// Determines the global jit level based on GraphOptimizationPassOptions, +// --tf_xla_auto_jit and whether the graph is a single GPU graph. +OptimizerOptions::GlobalJitLevel GetGlobalJitLevelForGraph( + const GraphOptimizationPassOptions& options); + +// Returns true if `g` is a single-GPU graph. A single-GPU graph uses exactly +// one GPU (and any number of CPUs). +bool IsSingleGpuGraph(const Graph& g); + +// Returns true if it is possible (but not guaranteed) that `n` calls a +// function. +bool MayCallFunction(const Node& n, const FunctionLibraryDefinition* flib_def); + +// Returns true if `node` an operator that consumes only the shape of its input, +// not the data itself. +bool IsShapeConsumerOp(const Node& node); + +// Computes a clustering summary for `graph`. See documentation on +// `XlaAutoClusteringSummary` for details. +XlaAutoClusteringSummary GetXlaAutoClusteringSummary(const Graph& graph); + +// Returns the set of nodes that have a path to or from nodes that may have ref +// variables as input or output. +// +// We assume each node has a trivial path to itself so the returned set includes +// all of the nodes that have ref variables as input or output. +absl::StatusOr> GetNodesRelatedToRefVariables( + const Graph& graph, FunctionLibraryRuntime* lib_runtime); + +// Deterministically serialized the graph to a byte string. +absl::StatusOr SerializeGraphDeterministic(const Graph& graph); + +// Computes a fingerprint of the given `graph`. The fingerprint can use used to +// check if two graphs are likely the same but should not be relied on +// determining if the graphs are identical. +absl::StatusOr FingerprintGraph(const Graph& graph); + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_XLA_CLUSTER_UTIL_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_cluster_util_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_cluster_util_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..1a736a64acf1ccc14b4d481fbca7fc3fd4e4e3d7 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_cluster_util_test.cc @@ -0,0 +1,288 @@ +/* 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 "tensorflow/compiler/jit/xla_cluster_util.h" + +#include "absl/algorithm/container.h" +#include "absl/strings/str_join.h" +#include "tensorflow/cc/framework/ops.h" +#include "tensorflow/cc/ops/control_flow_ops_internal.h" +#include "tensorflow/cc/ops/function_ops.h" +#include "tensorflow/cc/ops/functional_ops.h" +#include "tensorflow/cc/ops/standard_ops.h" +#include "xla/status_macros.h" +#include "tensorflow/core/common_runtime/graph_constructor.h" +#include "tensorflow/core/common_runtime/process_function_library_runtime.h" +#include "tensorflow/core/framework/function_testlib.h" +#include "tensorflow/core/framework/graph_to_functiondef.h" +#include "tensorflow/core/graph/algorithm.h" +#include "tensorflow/core/graph/graph_def_builder.h" +#include "tensorflow/core/graph/testlib.h" +#include "tensorflow/core/lib/core/status_test_util.h" +#include "tensorflow/core/platform/test.h" +#include "tensorflow/core/public/version.h" + +namespace tensorflow { +namespace { + +TEST(CreateCycleDetectionGraph, ConnectivityThroughEnterExitRegion) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Output a = ops::Const(root.WithOpName("a"), Input::Initializer(0.0)); + Output enter = + ops::internal::Enter(root.WithOpName("enter"), a, "only_frame"); + Output exit = ops::internal::Exit(root.WithOpName("exit"), enter); + Output b = ops::Add(root.WithOpName("b"), a, exit); + + FixupSourceAndSinkEdges(root.graph()); + + xla::GraphCycles cycles; + TF_ASSERT_OK(CreateCycleDetectionGraph(root.graph(), &cycles).status()); + EXPECT_FALSE(cycles.CanContractEdge(a.node()->id(), b.node()->id())); +} + +TEST(CreateCycleDetectionGraph, ConnectivityThroughMultipleEnterExitRegions) { + Scope root = Scope::NewRootScope().ExitOnError(); + + Output a = ops::Const(root.WithOpName("a"), Input::Initializer(0.0)); + Output enter_0 = + ops::internal::Enter(root.WithOpName("enter_0"), a, "frame_0"); + Output exit_0 = ops::internal::Exit(root.WithOpName("exit_0"), enter_0); + Output enter_1 = + ops::internal::Enter(root.WithOpName("enter_1"), a, "frame_1"); + Output exit_1 = ops::internal::Exit(root.WithOpName("exit_1"), enter_1); + Output b = ops::Add(root.WithOpName("b"), a, exit_1); + + FixupSourceAndSinkEdges(root.graph()); + + xla::GraphCycles cycles; + TF_ASSERT_OK(CreateCycleDetectionGraph(root.graph(), &cycles).status()); + EXPECT_FALSE(cycles.CanContractEdge(a.node()->id(), b.node()->id())); +} + +TEST(CreateCycleDetectionGraph, ReachingEnterExit) { + // TODO(b/127521408): We can lift this limitation with some work. + Scope root = Scope::NewRootScope().ExitOnError(); + + Output a = ops::Const(root.WithOpName("a"), Input::Initializer(0.0)); + Output enter_0 = + ops::internal::Enter(root.WithOpName("enter_0"), a, "frame_0"); + Output exit_0 = ops::internal::Exit(root.WithOpName("exit_0"), enter_0); + + Output add = ops::Add(root.WithOpName("add"), exit_0, exit_0); + + Output enter_1 = + ops::internal::Enter(root.WithOpName("enter_1"), add, "frame_0"); + Output exit_1 = ops::internal::Exit(root.WithOpName("exit_1"), enter_1); + + FixupSourceAndSinkEdges(root.graph()); + + xla::GraphCycles cycles; + TF_ASSERT_OK_AND_ASSIGN(bool ok, + CreateCycleDetectionGraph(root.graph(), &cycles)); + EXPECT_FALSE(ok); +} + +const char* kCPU0 = "/job:localhost/replica:0/task:0/device:CPU:0"; +const char* kGPU0 = "/job:localhost/replica:0/task:0/device:GPU:0"; +const char* kGPU1 = "/job:localhost/replica:0/task:0/device:GPU:1"; + +TEST(IsSingleGpuGraph, ReturnsTrue) { + Scope root = Scope::NewRootScope().WithAssignedDevice(kGPU0).ExitOnError(); + + Output a = ops::Const(root.WithOpName("a"), Input::Initializer(0.0)); + Output b = ops::Add(root.WithOpName("b"), a, a); + Output c = ops::Add(root.WithOpName("c"), b, b); + + FixupSourceAndSinkEdges(root.graph()); + + EXPECT_TRUE(IsSingleGpuGraph(*root.graph())); +} + +TEST(IsSingleGpuGraph, ReturnsFalseForCpuGraph) { + Scope root = Scope::NewRootScope().WithAssignedDevice(kCPU0).ExitOnError(); + + Output a = ops::Const(root.WithOpName("a"), Input::Initializer(0.0)); + Output b = ops::Add(root.WithOpName("b"), a, a); + Output c = ops::Add(root.WithOpName("c"), b, b); + + FixupSourceAndSinkEdges(root.graph()); + + EXPECT_FALSE(IsSingleGpuGraph(*root.graph())); +} + +TEST(IsSingleGpuGraph, ReturnsFalseForMultiGpuGraph) { + Scope root = Scope::NewRootScope().WithAssignedDevice(kGPU0).ExitOnError(); + + Output a = ops::Const(root.WithOpName("a"), Input::Initializer(0.0)); + Output b = ops::Add(root.WithOpName("b").WithAssignedDevice(kGPU1), a, a); + Output c = ops::Add(root.WithOpName("c"), b, b); + + FixupSourceAndSinkEdges(root.graph()); + + EXPECT_FALSE(IsSingleGpuGraph(*root.graph())); +} + +absl::StatusOr> GetNodesRelatedToRefVarsSorted( + const Scope& scope, FunctionLibraryDefinition* flib_def = nullptr) { + FunctionDefLibrary flib; + FunctionLibraryDefinition flib_def_local(OpRegistry::Global(), flib); + if (flib_def == nullptr) { + flib_def = &flib_def_local; + } + + std::unique_ptr graph(new Graph(OpRegistry::Global())); + + TF_RETURN_IF_ERROR(scope.ToGraph(graph.get())); + + std::unique_ptr pflr( + new ProcessFunctionLibraryRuntime( + nullptr, Env::Default(), /*config=*/nullptr, TF_GRAPH_DEF_VERSION, + flib_def, OptimizerOptions{})); + FunctionLibraryRuntime* lib_runtime = + pflr->GetFLR(ProcessFunctionLibraryRuntime::kDefaultFLRDevice); + + TF_ASSIGN_OR_RETURN(absl::flat_hash_set nodes_related_to_ref_vars, + GetNodesRelatedToRefVariables(*graph, lib_runtime)); + + std::vector names; + absl::c_transform(nodes_related_to_ref_vars, std::back_inserter(names), + [](Node* n) { return n->name(); }); + absl::c_sort(names); + return names; +} + +void CreateSubgraphTouchingRefVar(const Scope& s) { + Output variable = + ops::Variable(s.WithOpName("variable"), PartialTensorShape{}, DT_FLOAT); + Output read = ops::Identity(s.WithOpName("read_ref_var"), variable); + Output neg = ops::Negate(s.WithOpName("negate_ref"), read); + Output add = ops::Add(s.WithOpName("add_ref"), neg, neg); + + Output constant = + ops::Const(s.WithOpName("constant_ref"), Input::Initializer(0.0)); + s.graph()->AddControlEdge(constant.node(), variable.node()); +} + +void CreateSubgraphNotTouchingRefVar(const Scope& s) { + Output constant = + ops::Const(s.WithOpName("constant_normal"), Input::Initializer(0.0)); + Output neg = ops::Negate(s.WithOpName("negate_normal"), constant); + Output add = ops::Add(s.WithOpName("add_normal"), neg, neg); +} + +void CreateSubgraphCallingFunctionWithRefVar(const Scope& s) { + NameAttrList ref_float_function; + ref_float_function.set_name("RefFloatFn"); + ops::PartitionedCall call(s.WithOpName("RefFloat"), {absl::Span{}}, + {DT_FLOAT}, ref_float_function); + Output constant = + ops::Const(s.WithOpName("constant_ref_pco"), Input::Initializer(0.0)); + s.graph()->AddControlEdge(call.operation.node(), constant.node()); +} + +void CreateSubgraphCallingFunctionWithoutRefVar(const Scope& s) { + NameAttrList regular_float_function; + regular_float_function.set_name("RegularFloatFn"); + ops::PartitionedCall call(s.WithOpName("RegularFloat"), {absl::Span{}}, + {DT_FLOAT}, regular_float_function); + Output constant = + ops::Const(s.WithOpName("constant_normal_pco"), Input::Initializer(0.0)); + s.graph()->AddControlEdge(call.operation.node(), constant.node()); +} + +void AddRefFunctionFunctionDef(FunctionDefLibrary* fdef_lib) { + FunctionDef make_ref_float = FunctionDefHelper::Define( + "RefFloatFn", {}, {"r:float"}, {}, + {{{"var"}, + "VariableV2", + {}, + {{"dtype", DT_FLOAT}, {"shape", TensorShape({})}}}, + {{"r"}, "Identity", {"var"}, {{"T", DT_FLOAT}}}}); + *fdef_lib->add_function() = make_ref_float; +} + +void AddRegularFunctionFunctionDef(FunctionDefLibrary* fdef_lib) { + Tensor seven(DT_FLOAT, {}); + seven.scalar()() = 7; + FunctionDef make_regular_float = FunctionDefHelper::Define( + "RegularFloatFn", {}, {"r:float"}, {}, + {{{"r"}, "Const", {}, {{"dtype", DT_FLOAT}, {"value", seven}}}}); + *fdef_lib->add_function() = make_regular_float; +} + +TEST(NodesRelatedToRefVariables, Basic) { + Scope root = Scope::NewRootScope().ExitOnError(); + + FunctionDefLibrary fdef_lib; + + CreateSubgraphTouchingRefVar(root); + CreateSubgraphNotTouchingRefVar(root); + + AddRefFunctionFunctionDef(&fdef_lib); + CreateSubgraphCallingFunctionWithRefVar(root); + + AddRegularFunctionFunctionDef(&fdef_lib); + CreateSubgraphCallingFunctionWithoutRefVar(root); + + FunctionLibraryDefinition flib_def(OpRegistry::Global(), fdef_lib); + + TF_ASSERT_OK_AND_ASSIGN(std::vector names, + GetNodesRelatedToRefVarsSorted(root, &flib_def)); + + std::vector expected({ + "RefFloat", + "add_ref", + "constant_ref", + "constant_ref_pco", + "negate_ref", + "read_ref_var", + "variable", + }); + + EXPECT_EQ(names, expected); +} + +absl::Status MakeLoop(Scope s, Output init_value, absl::string_view loop_name) { + s = s.NewSubScope(std::string(loop_name)); + ops::internal::Enter enter(s.WithOpName("init_value"), init_value, loop_name); + ops::Merge merge(s.WithOpName("merge"), {init_value, init_value}); + Output next_iteration = + ops::NextIteration(s.WithOpName("next_itr"), merge.output); + return s.graph()->UpdateEdge(next_iteration.node(), 0, merge.output.node(), + 1); +} + +TEST(NodesRelatedToRefVariables, Cycles) { + Scope root = Scope::NewRootScope().ExitOnError(); + Output variable = ops::Variable(root.WithOpName("variable"), + PartialTensorShape{}, DT_FLOAT); + TF_ASSERT_OK( + MakeLoop(root, ops::Identity(root.WithOpName("read_ref_var"), variable), + "ref_loop")); + TF_ASSERT_OK(MakeLoop( + root, ops::Const(root.WithOpName("constant"), Input::Initializer(0.0)), + "normal_loop")); + + TF_ASSERT_OK_AND_ASSIGN(std::vector names, + GetNodesRelatedToRefVarsSorted(root)); + std::vector expected({"read_ref_var", "ref_loop/init_value", + "ref_loop/merge", "ref_loop/next_itr", + "variable"}); + + EXPECT_EQ(names, expected); +} +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_compilation_cache.proto b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_compilation_cache.proto new file mode 100644 index 0000000000000000000000000000000000000000..51e9a79b5bf7f6b5e80e30a43dbdd1e998c59b98 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_compilation_cache.proto @@ -0,0 +1,45 @@ +/* Copyright 2022 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. +==============================================================================*/ + +syntax = "proto3"; + +package tensorflow; + +import "xla/service/hlo.proto"; + +// Represents the cache key used for persistence. +message XlaSerializedCacheKey { + uint64 signature_fingerprint = 1; + uint64 cluster_fingerprint = 2; + string device_type = 3; + string prefix = 4; + bool compiled_using_pjrt = 5; +} + +// Represents an entry in the XLA compile cache. +message XlaSerializedCacheEntry { + // Used to uniqely identify this entry in its persisted representation. + XlaSerializedCacheKey key = 1; + + // The computation (HLO) that compilation was done for. It is correlated to + // the input TF graph so we can use it to fingerprint the compiled binary. We + // serialize this rather than the input graphdef because it provides a + // stronger guarantee over what bindings are needed between the HLO and + // calling TF graph. + xla.HloModuleProto hlo_module = 2; + + // The raw bytes of the executable. + bytes executable = 3; +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_compile_on_demand_op.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_compile_on_demand_op.cc new file mode 100644 index 0000000000000000000000000000000000000000..92fa4903adde9068a20bd03713c3629c84e2a4db --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_compile_on_demand_op.cc @@ -0,0 +1,324 @@ +/* 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. +==============================================================================*/ + +// Defines the XlaCompileOnDemandOp. + +#include "tensorflow/compiler/jit/xla_compile_on_demand_op.h" + +#include +#include +#include +#include +#include + +#include "absl/container/flat_hash_map.h" +#include "absl/log/check.h" +#include "absl/log/log.h" +#include "absl/memory/memory.h" +#include "absl/types/span.h" +#include "tensorflow/compiler/jit/device_compilation_cluster_signature.h" +#include "tensorflow/compiler/jit/device_compilation_profiler.h" +#include "tensorflow/compiler/jit/device_compiler.h" +#include "tensorflow/compiler/jit/flags.h" +#include "tensorflow/compiler/jit/variable_info.h" +#include "tensorflow/compiler/jit/variable_info_util.h" +#include "tensorflow/compiler/jit/xla_compile_util.h" +#include "tensorflow/compiler/jit/xla_compiler_options_util.h" +#include "tensorflow/compiler/jit/xla_launch_util.h" +#include "tensorflow/compiler/jit/xla_platform_info.h" +#include "tensorflow/compiler/tf2xla/const_analysis.h" +#include "tensorflow/compiler/tf2xla/tf2xla_util.h" +#include "tensorflow/compiler/tf2xla/xla_compiler.h" +#include "tensorflow/compiler/tf2xla/xla_helpers.h" +#include "xla/client/local_client.h" +#include "xla/executable_run_options.h" +#include "xla/hlo/ir/hlo_input_output_alias_config.h" +#include "xla/pjrt/pjrt_client.h" +#include "xla/pjrt/tf_pjrt_client.h" +#include "xla/service/executable.h" +#include "xla/service/gpu/gpu_executable_run_options.h" +#include "tensorflow/core/framework/function.h" +#include "tensorflow/core/framework/op_kernel.h" +#include "tensorflow/core/framework/op_requires.h" +#include "tensorflow/core/framework/tensor.h" +#include "tensorflow/core/framework/types.h" +#include "tensorflow/core/lib/core/errors.h" +#include "tensorflow/core/lib/core/refcount.h" +#include "tensorflow/core/lib/core/status.h" +#include "tensorflow/core/platform/statusor.h" +#include "tensorflow/core/tfrt/common/pjrt_util.h" +#include "tsl/platform/errors.h" + +namespace tensorflow { +namespace { +using XlaDeviceCompiler = + DeviceCompiler; +using PjRtDeviceCompiler = + DeviceCompiler; + +XlaCompiler::CompileOptions GetCompileOptions(bool for_pjrt = false) { + XlaCompiler::CompileOptions compile_options; + compile_options.is_entry_computation = true; + // Optimization: where possible, have the computation return a naked array + // rather than a one-element tuple. + compile_options.always_return_tuple = false; + if (for_pjrt) { + compile_options.use_tuple_arg = false; + compile_options.always_return_tuple = true; + } + + return compile_options; +} + +// Gets `variables` from `ctx`, locks them and builds XlaCompiler::Arguments +// using them. Stores the arguments in `args`. `variables` and `args` passed in +// will be cleared before populating them. +absl::Status GetAndLockVariablesAndBuildXlaCompilerArguments( + const OpKernelContext& ctx, const std::vector& inputs, + const std::vector& constant_indices, + const std::vector& variable_indices, + std::vector* variables, + std::vector* args) { + variables->clear(); + args->clear(); + TF_RETURN_IF_ERROR(GetVariableInfosFromInputs(ctx.resource_manager(), + ctx.device(), inputs, + variable_indices, variables)); + TF_RETURN_IF_ERROR(LockVariables(absl::MakeSpan(*variables))); + TF_ASSIGN_OR_RETURN(*args, + XlaComputationLaunchContext::BuildXlaCompilerArguments( + constant_indices, inputs, *variables, + static_cast(ctx.device()))); + return absl::OkStatus(); +} +} // namespace + +XlaCompileOnDemandOp::XlaCompileOnDemandOp(OpKernelConstruction* ctx) + : OpKernel(ctx), + platform_info_(XlaPlatformInfoFromDevice(ctx->device())), + function_(GetDeviceCompilerFunction(ctx->def())), + canonical_function_(Canonicalize(function_)) {} + +absl::Status XlaCompileOnDemandOp::Run( + const ResourceVarsSnapshot& variable_args, + const XlaCompiler::CompilationResult* result, + const XlaDeviceCompiler* xla_device_compiler, + xla::LocalExecutable* executable, OpKernelContext* ctx) { + xla::LocalClient* client = + static_cast(xla_device_compiler->client()); + + se::Stream* stream = + ctx->op_device_context() ? ctx->op_device_context()->stream() : nullptr; + std::shared_ptr allocator_ptr = + GetAllocator(ctx->device(), stream, platform_info_); + stream_executor::DeviceAddressAllocator* allocator = allocator_ptr.get(); + XlaComputationLaunchContext launch_context( + client, allocator, client->default_device_ordinal(), + /*allocate_xla_tensors=*/platform_info_.xla_device_metadata() != nullptr, + platform_info_.xla_device_metadata() + ? platform_info_.xla_device_metadata()->UseMultipleStreams() + : false); + + absl::flat_hash_map snapshot_ptrs; + for (auto& p : variable_args) { + snapshot_ptrs.emplace(p.first, + p.second.has_value() ? &p.second.value() : nullptr); + } + + const xla::HloInputOutputAliasConfig& input_output_alias = + executable->executable()->module().input_output_alias_config(); + absl::StatusOr> execution_inputs = + launch_context.PopulateInputs(ctx, result, snapshot_ptrs, + /*missing_ctx_input_prefix=*/0, + input_output_alias); + TF_RETURN_IF_ERROR(execution_inputs.status()); + + VLOG(2) << "Executing computation: " << name(); + xla::ExecutableRunOptions run_options; + xla::gpu::GpuExecutableRunOptions gpu_options; + xla::DeviceAssignment device_assignment; + if (result->collective_info) { + TF_RETURN_IF_ERROR(ResolveDeviceAssignment(ctx, *result->collective_info, + run_options, device_assignment, + gpu_options)); + } + + run_options.set_stream(stream); + run_options.set_allocator(allocator); + run_options.set_intra_op_thread_pool(&ctx->eigen_cpu_device()); + run_options.set_rng_seed(GetXLARandomSeed()); + + absl::StatusOr run_result = + executable->Run(std::move(execution_inputs).value(), run_options); + TF_RETURN_IF_ERROR(run_result.status()); + xla::ExecutionOutput execution_output = std::move(run_result).value(); + absl::StatusOr> variable_infos = + GatherVariableInfo(ctx, *result, 0); + TF_RETURN_IF_ERROR(variable_infos.status()); + TF_RETURN_IF_ERROR(LockVariables(absl::MakeSpan(*variable_infos))); + TF_RETURN_IF_ERROR(launch_context.PopulateOutputs( + ctx, result, execution_output.ConsumeResult(), + /*missing_ctx_input_prefix=*/0, absl::MakeSpan(*variable_infos), + input_output_alias, snapshot_ptrs)); + return absl::OkStatus(); +} + +absl::Status XlaCompileOnDemandOp::Compile( + const std::vector& args, OpKernelContext* ctx, + PjRtDeviceCompiler** pjrt_device_compiler, + DeviceCompilationProfiler** profiler, + const XlaCompiler::CompilationResult** result, + xla::PjRtLoadedExecutable** executable) { + TF_RETURN_IF_ERROR(GetOrCreatePjRtDeviceCompilerAndProfiler( + *ctx, platform_info_, ctx->function_library(), pjrt_device_compiler, + profiler)); + + XlaCompiler::Options options = + GenerateCompilerOptionsForPjRt(*(ctx->function_library()), ctx->device(), + platform_info_, *pjrt_device_compiler); + // No detailed logging for on demand op. + options.detailed_logging = false; + XlaCompiler::CompileOptions compile_options = GetCompileOptions(true); + + return (*pjrt_device_compiler) + ->CompileSingleOpIfNeeded(options, function_, canonical_function_, args, + compile_options, ctx, *profiler, result, + executable); +} + +absl::Status XlaCompileOnDemandOp::Compile( + const std::vector& args, OpKernelContext* ctx, + XlaDeviceCompiler** xla_device_compiler, + DeviceCompilationProfiler** profiler, + const XlaCompiler::CompilationResult** result, + xla::LocalExecutable** executable) { + // We store information about the JIT-compiled XLA computation + // in the ResourceMgr. + ResourceMgr* rm = ctx->resource_manager(); + CHECK(rm); + + TF_ASSIGN_OR_RETURN(DeviceType compilation_device_type, + GetCompilationDeviceType(platform_info_.device_type())); + + TF_RETURN_IF_ERROR(rm->LookupOrCreate( + rm->default_container(), "xla_device_compiler", xla_device_compiler, + [&](XlaDeviceCompiler** xla_device_compiler) { + return BuildXlaDeviceCompiler(ctx->device(), ctx->function_library(), + platform_info_, compilation_device_type, + xla_device_compiler); + })); + + TF_RETURN_IF_ERROR(rm->LookupOrCreate( + rm->default_container(), "device_compilation_profiler", profiler, + [](DeviceCompilationProfiler** profiler) { + *profiler = new DeviceCompilationProfiler(); + return absl::OkStatus(); + })); + + XlaCompiler::Options options = GenerateCompilerOptions( + **xla_device_compiler, *ctx->function_library(), ctx->device(), + ctx->op_device_context() ? ctx->op_device_context()->stream() : nullptr, + platform_info_, /*has_ref_vars=*/true); + // No detailed logging from on demand op. + options.detailed_logging = false; + XlaCompiler::CompileOptions compile_options = GetCompileOptions(); + + return (*xla_device_compiler) + ->CompileSingleOpIfNeeded(options, function_, canonical_function_, args, + compile_options, ctx, *profiler, result, + executable); +} + +void XlaCompileOnDemandOp::Compute(OpKernelContext* ctx) { + const XlaCompiler::CompilationResult* result; + DeviceCompilationProfiler* profiler; + + OP_REQUIRES(ctx, ctx->function_library(), + absl::InternalError("Function library missing")); + + // Get constants, inputs and variables from the OpKernelContext. + auto constant_indices_or = GetConstantInputIndicesFromContext(ctx); + OP_REQUIRES_OK(ctx, constant_indices_or.status()); + std::vector inputs = InputsFromContext(ctx); + std::vector variable_indices = + GetResourceVariableIndicesFromContext(ctx); + + bool use_pjrt = + GetXlaOpsCommonFlags() + ->tf_xla_use_device_api.IsEnabledInXlaCompileOnDemandForDevice( + platform_info_.device_type()); + if (use_pjrt) { + std::vector variables; + std::vector args; + // Lock variables for the whole duration of compile + execute. + OP_REQUIRES_OK(ctx, GetAndLockVariablesAndBuildXlaCompilerArguments( + *ctx, inputs, *constant_indices_or, + variable_indices, &variables, &args)); + + PjRtDeviceCompiler* pjrt_device_compiler = nullptr; + xla::PjRtLoadedExecutable* pjrt_executable = nullptr; + absl::Status status = Compile(args, ctx, &pjrt_device_compiler, &profiler, + &result, &pjrt_executable); + // Hold the reference to the XLA device compiler and profiler during + // evaluation. (We could probably free them sooner because the ResourceMgr + // will retain references, but this is more obviously correct.) + // We must also ensure these pointers are freed even if compilation fails. + core::ScopedUnref pjrt_device_compiler_ref(pjrt_device_compiler); + core::ScopedUnref profiler_ref(profiler); + OP_REQUIRES_OK(ctx, status); + + VLOG(2) << "Compiled op with PJRT: " << ctx->status(); + VLOG(2) << "result != nullptr: " << (result != nullptr); + VLOG(2) << "pjrt_executable != nullptr: " << (pjrt_executable != nullptr); + VLOG(2) << "Executing with PJRT ..."; + + OP_REQUIRES_OK(ctx, RunPjRtExecutable(inputs, variables, *result, + pjrt_device_compiler->client(), + pjrt_executable, ctx)); + + VLOG(2) << "Completed executing with PJRT!"; + } else { + ResourceVarsSnapshot variable_args; + std::vector args; + // Lock variables only for generating XlaCompiler::Arguments and then + // release them. + { + std::vector variables; + OP_REQUIRES_OK(ctx, GetAndLockVariablesAndBuildXlaCompilerArguments( + *ctx, inputs, *constant_indices_or, + variable_indices, &variables, &args)); + OP_REQUIRES_OK(ctx, SnapshotResourceVariables(ctx, variable_indices, + variables, &variable_args)); + } + + XlaDeviceCompiler* xla_device_compiler = nullptr; + xla::LocalExecutable* executable = nullptr; + absl::Status status = Compile(args, ctx, &xla_device_compiler, &profiler, + &result, &executable); + // Hold the reference to the XLA device compiler and profiler during + // evaluation. (We could probably free them sooner because the ResourceMgr + // will retain references, but this is more obviously correct.) + // We must also ensure these pointers are freed even if compilation fails. + core::ScopedUnref xla_device_compiler_ref(xla_device_compiler); + core::ScopedUnref profiler_ref(profiler); + OP_REQUIRES_OK(ctx, status); + + // Locks are acquired again when populating the `ctx` outputs. + OP_REQUIRES_OK( + ctx, Run(variable_args, result, xla_device_compiler, executable, ctx)); + } +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_compile_on_demand_op.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_compile_on_demand_op.h new file mode 100644 index 0000000000000000000000000000000000000000..529655cf5764c7ef3145c5051a4fa758ea50154c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_compile_on_demand_op.h @@ -0,0 +1,81 @@ +/* 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. +==============================================================================*/ + +// The XlaCompileOnDemandOp is an OpKernel that, when its Compute method is +// called, will generate an xla::Computation and run it asynchronously. + +#ifndef TENSORFLOW_COMPILER_JIT_XLA_COMPILE_ON_DEMAND_OP_H_ +#define TENSORFLOW_COMPILER_JIT_XLA_COMPILE_ON_DEMAND_OP_H_ + +#include + +#include "tensorflow/compiler/jit/device_compilation_cluster_signature.h" +#include "tensorflow/compiler/jit/device_compilation_profiler.h" +#include "tensorflow/compiler/jit/variable_info.h" +#include "tensorflow/compiler/jit/variable_info_util.h" +#include "tensorflow/compiler/jit/xla_launch_util.h" +#include "tensorflow/compiler/jit/xla_platform_info.h" +#include "tensorflow/compiler/tf2xla/xla_compiler.h" +#include "xla/client/local_client.h" +#include "xla/pjrt/pjrt_client.h" +#include "tensorflow/core/framework/function.h" +#include "tensorflow/core/framework/op_kernel.h" +#include "tensorflow/core/framework/tensor.h" +#include "tensorflow/core/framework/types.h" +#include "tensorflow/core/lib/core/status.h" + +namespace tensorflow { + +// An OpKernel that compiles an op to an XLA computation and runs it. Unlike +// XlaLaunch this doesn't rely on any rewrites of the graphdef - it will run a +// vanilla TensorFlow op as long as the bridge supports it. +class XlaCompileOnDemandOp : public OpKernel { + public: + explicit XlaCompileOnDemandOp(OpKernelConstruction* ctx); + void Compute(OpKernelContext* ctx) override; + + private: + absl::Status Compile(const std::vector& args, + OpKernelContext* ctx, + DeviceCompiler** + xla_device_compiler, + DeviceCompilationProfiler** profiler, + const XlaCompiler::CompilationResult** result, + xla::LocalExecutable** executable); + + absl::Status Compile(const std::vector& args, + OpKernelContext* ctx, + DeviceCompiler** pjrt_device_compiler, + DeviceCompilationProfiler** profiler, + const XlaCompiler::CompilationResult** result, + xla::PjRtLoadedExecutable** executable); + + absl::Status Run(const ResourceVarsSnapshot& variable_args, + const XlaCompiler::CompilationResult* result, + const DeviceCompiler* + xla_device_compiler, + xla::LocalExecutable* executable, OpKernelContext* ctx); + + const XlaPlatformInfo platform_info_; + + // Canonicalized function to compile derived from the Op attributes. + NameAttrList function_; + DeviceCompilationCanonicalFunction canonical_function_; +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_XLA_COMPILE_ON_DEMAND_OP_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_compile_util.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_compile_util.cc new file mode 100644 index 0000000000000000000000000000000000000000..5e3afab0c4934ef67e76cd3b4b11f31e5acc8faa --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_compile_util.cc @@ -0,0 +1,119 @@ +/* Copyright 2022 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/compiler/jit/xla_compile_util.h" + +#include +#include +#include + +#include "absl/status/status.h" +#include "tensorflow/compiler/jit/flags.h" +#include "tensorflow/core/framework/op_kernel.h" +#include "tensorflow/core/framework/resource_mgr.h" +#include "tensorflow/core/framework/types.h" +#include "tensorflow/core/graph/algorithm.h" +#include "tensorflow/core/graph/node_builder.h" +#include "tensorflow/core/tfrt/common/global_state.h" +#include "tensorflow/core/util/determinism.h" + +namespace tensorflow { +namespace { +constexpr const char* kPjRtDeviceCompilerResourceName = "pjrt_device_compiler"; +constexpr const char* kPjRtDeviceCompilationProfilerResourceName = + "pjrt_device_compilation_profiler"; +} // namespace + +absl::StatusOr> CreateSingleOpGraph( + const NodeDef& node_def, absl::Span args, + absl::Span result_types) { + // TODO(b/74182462): We implement this by creating a new dummy Graph including + // _Arg nodes, and let CompileGraph walk it. This could be optimized. + std::unique_ptr graph(new Graph(OpRegistry::Global())); + + // First create the actual node we care about computing. + TF_ASSIGN_OR_RETURN(Node * main_node, graph->AddNode(node_def)); + + // Create dummy _Arg nodes. Link these to `node` and also via a control + // dependency edge to the _SOURCE node. + for (int64_t i = 0, end = args.size(); i < end; ++i) { + Node* node; + std::string arg_name = absl::StrCat("_arg", i); + absl::Status status = + NodeBuilder(arg_name, FunctionLibraryDefinition::kArgOp) + .ControlInput(graph->source_node()) + .Attr("T", args[i].kind == XlaArgument::kResource ? DT_RESOURCE + : args[i].type) + .Attr("index", i) + .Finalize(graph.get(), &node); + TF_RETURN_IF_ERROR(status); + graph->AddEdge(node, 0, main_node, i); + } + + // Similarly with return values, create dummy _Retval nodes fed by `node`. + for (int64_t i = 0, end = result_types.size(); i < end; ++i) { + Node* node; + std::string retval_name = absl::StrCat("_retval", i); + absl::Status status = + NodeBuilder(retval_name, FunctionLibraryDefinition::kRetOp) + .Input(main_node, i) + .Attr("T", result_types[i]) + .Attr("index", i) + .Finalize(graph.get(), &node); + TF_RETURN_IF_ERROR(status); + } + FixupSourceAndSinkEdges(graph.get()); + return graph; +} + +bool UsePjRtForSingleDeviceCompilation(const DeviceType& device_type) { + const auto& rollout_config = GetXlaOpsCommonFlags()->tf_xla_use_device_api; + return rollout_config.IsEnabledInXlaLaunchForDevice(device_type) || + rollout_config.IsEnabledInXlaCompileOnDemandForDevice(device_type) || + rollout_config.IsEnabledInXlaCompileAndRunForDevice(device_type); +} + +std::string GetPjRtDeviceCompilerResourceName(const DeviceType& device_type) { + return absl::StrCat(kPjRtDeviceCompilerResourceName, "_", + device_type.type_string()); +} + +std::string GetPjRtDeviceCompilationProfilerResourceName( + const DeviceType& device_type) { + return absl::StrCat(kPjRtDeviceCompilationProfilerResourceName, "_", + device_type.type_string()); +} + +absl::StatusOr GetResourceMgrForDeviceCompiler( + const OpKernelContext& ctx, const DeviceType& device_type) { + // We store information about the JIT-compiled XLA computation in the + // ResourceMgr. The DeviceCompiler (which contains the DeviceCompilationCache) + // is stored in the tfrt_global ResourceMgr for TPU and the Device ResourceMgr + // for CPU/GPU. This is to make sure the DeviceCompiler's lifecycle is + // maintained appropriately. + ResourceMgr* rm = nullptr; + if (device_type == DEVICE_TPU) { + rm = tfrt_global::GetTFGlobalResourceMgr(); + } else { + rm = ctx.resource_manager(); + } + + if (!rm) { + return absl::InternalError("No resource manager found."); + } + return rm; +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_compile_util.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_compile_util.h new file mode 100644 index 0000000000000000000000000000000000000000..db8995b12b96be6384ecfc01292df4a1cc69c701 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_compile_util.h @@ -0,0 +1,67 @@ +/* Copyright 2022 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_XLA_COMPILE_UTIL_H_ +#define TENSORFLOW_COMPILER_JIT_XLA_COMPILE_UTIL_H_ + +#include +#include + +#include "tensorflow/compiler/tf2xla/xla_argument.h" +#include "tensorflow/core/graph/graph.h" + +namespace tensorflow { +// The number of compiler threads to use for asynchronous device compilation. +inline constexpr int64_t kNumAsyncDeviceCompilerThreads = 10; + +enum class DeviceCompileMode { + kLazy, + kStrict, + kAsync, +}; + +enum class DeviceCompileState { + kUncompiled, + kCompiling, + kCompiled, +}; + +// Creates a single-node graph using the specified `node_def` as the only op +// apart from the arg and retval nodes corresponding to `args` and +// `result_types` respectively. +absl::StatusOr> CreateSingleOpGraph( + const NodeDef& node_def, absl::Span args, + absl::Span result_types); + +// Checks if single device compilation and execution with PJRT is enabled for +// `device_type` in either the XlaLaunch op or the XlaCompileOnDemand op. +bool UsePjRtForSingleDeviceCompilation(const DeviceType& device_type); + +// Gets the resource name of the PjRt DeviceCompiler for `device_type`. +std::string GetPjRtDeviceCompilerResourceName(const DeviceType& device_type); + +// Gets the resource name of the DeviceCompilationProfiler for `device_type` +// when PjRt is used for compilation and execution. +std::string GetPjRtDeviceCompilationProfilerResourceName( + const DeviceType& device_type); + +// Gets the ResourceMgr where the DeviceCompiler is/should be stored for the +// given `device_type`. +absl::StatusOr GetResourceMgrForDeviceCompiler( + const OpKernelContext& ctx, const DeviceType& device_type); + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_XLA_COMPILE_UTIL_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_compile_util_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_compile_util_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..6f81c5bdbfdaf510608ad8be9bb1d7c5539eb86d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_compile_util_test.cc @@ -0,0 +1,149 @@ +/* Copyright 2022 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/compiler/jit/xla_compile_util.h" + +#include +#include + +#include +#include "tensorflow/compiler/jit/flags.h" +#include "tensorflow/compiler/tf2xla/xla_compiler.h" +#include "tensorflow/core/framework/fake_input.h" +#include "tensorflow/core/kernels/ops_testutil.h" +#include "tensorflow/core/tpu/tpu_defs.h" + +namespace tensorflow { +namespace { + +TEST_F(OpsTestBase, CreateSingleOpGraph) { + TF_EXPECT_OK(NodeDefBuilder("identity_op", "Identity") + .Input(FakeInput(DT_FLOAT)) + .Attr("T", DT_FLOAT) + .Finalize(node_def())); + TF_EXPECT_OK(InitOp()); + AddInputFromArray(TensorShape({1, 2}), {6.9, 4.2}); + TF_EXPECT_OK(RunOpKernel()); + + XlaCompiler::SingleOpCompileArgument single_op_arg(*context_); + + std::vector args(1); + args[0].kind = XlaArgument::kConstant; + args[0].type = DT_FLOAT; + args[0].shape = TensorShape({1, 2}); + args[0].constant_value = GetInput(0); + args[0].initialized = true; + + TF_ASSERT_OK_AND_ASSIGN( + auto graph, + CreateSingleOpGraph(*node_def(), args, single_op_arg.output_dtypes)); + + const auto& node_name_index = graph->BuildNodeNameIndex(); + + const Node* identity_node = node_name_index.at("identity_op"); + EXPECT_EQ(identity_node->op_def().name(), "Identity"); + EXPECT_EQ(identity_node->attrs().FindByString("T")->type(), DT_FLOAT); + + EXPECT_EQ(identity_node->num_inputs(), 1); + const Node* identity_input_node = nullptr; + TF_EXPECT_OK(identity_node->input_node(0, &identity_input_node)); + EXPECT_EQ(identity_input_node->name(), "_arg0"); + + const Node* arg_node = node_name_index.at("_arg0"); + EXPECT_EQ(arg_node->op_def().name(), "_Arg"); + EXPECT_EQ(arg_node->attrs().FindByString("T")->type(), DT_FLOAT); + + const Node* retval_node = node_name_index.at("_retval0"); + EXPECT_EQ(retval_node->op_def().name(), "_Retval"); + EXPECT_EQ(retval_node->attrs().FindByString("T")->type(), DT_FLOAT); + + EXPECT_EQ(identity_node->num_outputs(), 1); + EXPECT_EQ(retval_node->num_inputs(), 1); + const Node* retval_input_node = nullptr; + TF_EXPECT_OK(retval_node->input_node(0, &retval_input_node)); + EXPECT_EQ(retval_input_node->name(), "identity_op"); +} + +TEST(XlaCompileUtilTest, PjRtXlaLaunchFlagTest) { + EXPECT_FALSE(UsePjRtForSingleDeviceCompilation(DeviceType(DEVICE_CPU))); + + // Flag is turned on, but no device is allowlisted. + auto& rollout_config = GetXlaOpsCommonFlags()->tf_xla_use_device_api; + rollout_config.enabled_for_xla_launch_ = true; + + EXPECT_FALSE(UsePjRtForSingleDeviceCompilation(DeviceType(DEVICE_CPU))); + + // Flag is turned on, some device is allowlisted, but the requested one isn't. + rollout_config.AllowForDeviceInXlaLaunch(DeviceType(DEVICE_GPU)); + EXPECT_FALSE(UsePjRtForSingleDeviceCompilation(DeviceType(DEVICE_CPU))); + + // Flag is turned on and the requested device is allowlisted. + rollout_config.AllowForDeviceInXlaLaunch(DeviceType(DEVICE_CPU)); + EXPECT_TRUE(UsePjRtForSingleDeviceCompilation(DeviceType(DEVICE_CPU))); + + // The requested device is allowlisted, but the flag is turned off. + rollout_config.enabled_for_xla_launch_ = false; + EXPECT_FALSE(UsePjRtForSingleDeviceCompilation(DeviceType(DEVICE_CPU))); +} + +TEST(XlaCompileUtilTest, PjRtXlaCompileOnDemandFlagTest) { + EXPECT_FALSE(UsePjRtForSingleDeviceCompilation(DeviceType(DEVICE_CPU))); + + // Flag is turned on, but no device is allowlisted. + auto& rollout_config = GetXlaOpsCommonFlags()->tf_xla_use_device_api; + rollout_config.enabled_for_compile_on_demand_ = true; + + EXPECT_FALSE(UsePjRtForSingleDeviceCompilation(DeviceType(DEVICE_CPU))); + + // Flag is turned on, some device is allowlisted, but the requested one isn't. + rollout_config.AllowForDeviceInXlaCompileOnDemand(DeviceType(DEVICE_GPU)); + EXPECT_FALSE(UsePjRtForSingleDeviceCompilation(DeviceType(DEVICE_CPU))); + + // Flag is turned on and the requested device is allowlisted. + rollout_config.AllowForDeviceInXlaCompileOnDemand(DeviceType(DEVICE_CPU)); + EXPECT_TRUE(UsePjRtForSingleDeviceCompilation(DeviceType(DEVICE_CPU))); + + // The requested device is allowlisted, but the flag is turned off. + rollout_config.enabled_for_compile_on_demand_ = false; + EXPECT_FALSE(UsePjRtForSingleDeviceCompilation(DeviceType(DEVICE_CPU))); +} + +TEST(XlaCompileUtilTest, PjRtDeviceCompilerResourceName) { + EXPECT_EQ(GetPjRtDeviceCompilerResourceName(DeviceType(DEVICE_TPU)), + "pjrt_device_compiler_TPU"); + EXPECT_EQ(GetPjRtDeviceCompilerResourceName(DeviceType(DEVICE_TPU_NODE)), + "pjrt_device_compiler_TPU"); + EXPECT_EQ(GetPjRtDeviceCompilerResourceName(DeviceType(DEVICE_CPU)), + "pjrt_device_compiler_CPU"); + EXPECT_EQ(GetPjRtDeviceCompilerResourceName(DeviceType(DEVICE_GPU)), + "pjrt_device_compiler_GPU"); +} + +TEST(XlaCompileUtilTest, PjRtDeviceCompilationProfilerResourceName) { + EXPECT_EQ( + GetPjRtDeviceCompilationProfilerResourceName(DeviceType(DEVICE_TPU)), + "pjrt_device_compilation_profiler_TPU"); + EXPECT_EQ( + GetPjRtDeviceCompilationProfilerResourceName(DeviceType(DEVICE_TPU_NODE)), + "pjrt_device_compilation_profiler_TPU"); + EXPECT_EQ( + GetPjRtDeviceCompilationProfilerResourceName(DeviceType(DEVICE_CPU)), + "pjrt_device_compilation_profiler_CPU"); + EXPECT_EQ( + GetPjRtDeviceCompilationProfilerResourceName(DeviceType(DEVICE_GPU)), + "pjrt_device_compilation_profiler_GPU"); +} + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_compiler_options_util.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_compiler_options_util.cc new file mode 100644 index 0000000000000000000000000000000000000000..cc6fd696572d3442af2b4074282f329d4face7e0 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_compiler_options_util.cc @@ -0,0 +1,160 @@ +/* Copyright 2023 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/compiler/jit/xla_compiler_options_util.h" + +#include "absl/log/log.h" +#include "absl/status/statusor.h" +#include "tensorflow/compiler/jit/device_compiler.h" +#include "tensorflow/compiler/jit/xla_platform_info.h" +#include "tensorflow/compiler/tf2xla/xla_compiler.h" +#include "xla/client/local_client.h" +#include "xla/pjrt/pjrt_client.h" +#include "xla/stream_executor/host/host_platform_id.h" +#include "xla/stream_executor/stream.h" +#include "xla/tsl/framework/device_id_utils.h" +#include "tensorflow/core/framework/device_base.h" +#include "tensorflow/core/framework/function.h" +#include "tensorflow/core/framework/types.h" + +namespace tensorflow { +namespace { +using XlaDeviceCompiler = + DeviceCompiler; +using PjRtDeviceCompiler = + DeviceCompiler; + +inline void LogOptions(const XlaCompiler::Options& options) { + VLOG(2) << "XlaCompiler::Options[device_type=" << options.device_type + << ",device_ordinal=" << options.device_ordinal + << ",client=" << options.client << ",flib_def=" << options.flib_def + << ",graph_def_version=" << options.graph_def_version + << ",options.shape_determination_fns.layout_preference_fn?=" + << (options.shape_determination_fns.layout_preference_fn != nullptr) + << ",options.shape_determination_fns.shape_representation_fn?=" + << (options.shape_determination_fns.shape_representation_fn != + nullptr) + << ",allow_cpu_custom_calls=" << options.allow_cpu_custom_calls + << ",populate_resource_manager=" << options.populate_resource_manager + << ",alias_passthrough_params=" << options.alias_passthrough_params + << ",detailed_logging=" << options.detailed_logging << "]"; +} +} // namespace + +XlaCompiler::Options GenerateCompilerOptions( + const XlaDeviceCompiler& xla_device_compiler, + const FunctionLibraryRuntime& function_library, DeviceBase* device, + se::Stream* stream, const XlaPlatformInfo& platform_info, + bool has_ref_vars) { + XlaCompiler::Options options; + options.client = static_cast(xla_device_compiler.client()); + if (stream != nullptr) { + options.device_ordinal = stream->parent()->device_ordinal(); + } + options.device_type = xla_device_compiler.device_type(); + options.flib_def = function_library.GetFunctionLibraryDefinition(); + options.graph_def_version = function_library.graph_def_version(); + options.allow_cpu_custom_calls = + (platform_info.platform_id() == se::host::kHostPlatformId); + options.device_allocator = GetAllocator(device, stream, platform_info); + if (platform_info.xla_device_metadata()) { + options.shape_determination_fns = + platform_info.xla_device_metadata()->default_shape_determination_fns(); + } + // If reference variables are not present in the graph, we can safely alias + // passthrough parameters without performing a copy. + options.alias_passthrough_params = + !has_ref_vars && !platform_info.is_on_xla_device(); + + LogOptions(options); + return options; +} + +XlaCompiler::Options GenerateCompilerOptionsForTfrtTpu( + const XlaDeviceCompiler& xla_device_compiler, + const FunctionLibraryRuntime& function_library) { + XlaCompiler::Options options; + // TODO(b/238830423): consider device_ordinal and shape_determination_fns. + options.device_type = xla_device_compiler.device_type(); + options.flib_def = function_library.GetFunctionLibraryDefinition(); + options.graph_def_version = function_library.graph_def_version(); + options.allow_cpu_custom_calls = false; + options.alias_passthrough_params = false; + return options; +} + +XlaCompiler::Options GenerateCompilerOptionsForPjRt( + const FunctionLibraryRuntime& function_library, + const DeviceBase* device_base, const XlaPlatformInfo& platform_info, + const DeviceCompiler* + pjrt_device_compiler) { + return GenerateCompilerOptionsForPjRt( + function_library.GetFunctionLibraryDefinition(), + function_library.graph_def_version(), device_base, platform_info, + pjrt_device_compiler); +} + +XlaCompiler::Options GenerateCompilerOptionsForPjRt( + const FunctionLibraryDefinition* function_library_def, + int graph_def_version, const DeviceBase* device_base, + const XlaPlatformInfo& platform_info, + const PjRtDeviceCompiler* pjrt_device_compiler) { + XlaCompiler::Options options; + absl::StatusOr platform_device_id = + tsl::GetPlatformDeviceIdFromDeviceParsedName( + device_base->parsed_name(), + DeviceType( + absl::down_cast(device_base)->device_type())); + if (platform_device_id.ok()) { + options.device_ordinal = *platform_device_id; + } else { + options.device_ordinal = device_base->parsed_name().id; + } + options.flib_def = function_library_def; + options.graph_def_version = graph_def_version; + if (const auto* metadata = platform_info.xla_device_metadata(); + metadata != nullptr) { + options.device_type = metadata->jit_device_type(); + options.shape_determination_fns = + metadata->default_shape_determination_fns(); + } else if (const auto* metadata = platform_info.pjrt_device_metadata(); + metadata != nullptr) { + options.device_type = metadata->jit_device_type(); + options.shape_determination_fns = + metadata->default_shape_determination_fns(); + } else if (pjrt_device_compiler != nullptr) { + options.device_type = pjrt_device_compiler->device_type(); + } + // TODO(b/255826209): Confirm below options are correctly set after testing. + options.allow_cpu_custom_calls = false; + options.alias_passthrough_params = false; + + LogOptions(options); + return options; +} + +XlaCompiler::CompileOptions GenerateCompileOptions( + bool has_ref_vars, bool may_alias_resource_update) { + XlaCompiler::CompileOptions compile_options; + compile_options.is_entry_computation = true; + // Optimization: where possible, have the computation return a naked array + // rather than a one-element tuple. + compile_options.always_return_tuple = false; + compile_options.alias_resource_update = + !has_ref_vars && may_alias_resource_update; + return compile_options; +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_compiler_options_util.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_compiler_options_util.h new file mode 100644 index 0000000000000000000000000000000000000000..11213c43256f3793ad3a0cd4c380c0076820e34f --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_compiler_options_util.h @@ -0,0 +1,67 @@ +/* Copyright 2023 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_XLA_COMPILER_OPTIONS_UTIL_H_ +#define TENSORFLOW_COMPILER_JIT_XLA_COMPILER_OPTIONS_UTIL_H_ + +#include "tensorflow/compiler/jit/device_compiler.h" +#include "tensorflow/compiler/jit/xla_platform_info.h" +#include "tensorflow/compiler/tf2xla/xla_compiler.h" +#include "xla/client/local_client.h" +#include "xla/pjrt/pjrt_client.h" +#include "xla/stream_executor/stream.h" +#include "tensorflow/core/framework/device_base.h" +#include "tensorflow/core/framework/function.h" + +namespace tensorflow { + +// Returns created options for the XLA compiler. +XlaCompiler::Options GenerateCompilerOptions( + const DeviceCompiler& + xla_device_compiler, + const FunctionLibraryRuntime& function_library, DeviceBase* device, + se::Stream* stream, const XlaPlatformInfo& platform_info, + bool has_ref_vars); + +// Returns created options for XLA compiler when TFRT-TPU is used. +XlaCompiler::Options GenerateCompilerOptionsForTfrtTpu( + const DeviceCompiler& + xla_device_compiler, + const FunctionLibraryRuntime& function_library); + +// Returns created options for XLA compiler when PjRt (Device API) is used for +// compilation and execution. +XlaCompiler::Options GenerateCompilerOptionsForPjRt( + const FunctionLibraryRuntime& function_library, + const DeviceBase* device_base, const XlaPlatformInfo& platform_info, + const DeviceCompiler* + pjrt_device_compiler); + +// Returns created options for XLA compiler when PjRt (Device API) is used for +// compilation and execution. +XlaCompiler::Options GenerateCompilerOptionsForPjRt( + const FunctionLibraryDefinition* function_library_def, + int graph_def_version, const DeviceBase* device_base, + const XlaPlatformInfo& platform_info, + const DeviceCompiler* + pjrt_device_compiler); + +// Returns created CompileOptions for XLA compiler. +XlaCompiler::CompileOptions GenerateCompileOptions( + bool has_ref_vars, bool may_alias_resource_update); + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_XLA_COMPILER_OPTIONS_UTIL_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_compiler_options_util_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_compiler_options_util_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..e970c4883160305eda753597a77157e53ad3ebac --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_compiler_options_util_test.cc @@ -0,0 +1,358 @@ +/* Copyright 2023 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/compiler/jit/xla_compiler_options_util.h" + +#include +#include +#include +#include + +#include +#include "tensorflow/compiler/jit/device_compiler.h" +#include "tensorflow/compiler/jit/device_executable_persistor.h" +#include "tensorflow/compiler/jit/flags.h" +#include "tensorflow/compiler/jit/pjrt_base_device.h" +#include "tensorflow/compiler/jit/pjrt_device_compiler_client.h" +#include "tensorflow/compiler/jit/test_util.h" +#include "tensorflow/compiler/jit/xla_device.h" +#include "tensorflow/compiler/jit/xla_device_compiler_client.h" +#include "tensorflow/compiler/jit/xla_platform_info.h" +#include "tensorflow/compiler/tf2xla/layout_util.h" +#include "tensorflow/compiler/tf2xla/xla_argument.h" +#include "tensorflow/compiler/tf2xla/xla_compiler.h" +#include "tensorflow/compiler/tf2xla/xla_helpers.h" +#include "tensorflow/compiler/tf2xla/xla_op_registry.h" +#include "xla/client/client_library.h" +#include "xla/client/local_client.h" +#include "xla/pjrt/pjrt_client.h" +#include "xla/shape.h" +#include "xla/stream_executor/device_memory_allocator.h" +#include "xla/stream_executor/host/host_platform_id.h" +#include "xla/stream_executor/platform.h" +#include "xla/tsl/platform/statusor.h" +#include "xla/xla_data.pb.h" +#include "tensorflow/core/framework/device.h" +#include "tensorflow/core/framework/device_base.h" +#include "tensorflow/core/framework/tensor_shape.h" +#include "tensorflow/core/framework/types.h" +#include "tensorflow/core/platform/refcount.h" +#include "tensorflow/core/public/version.h" +#include "tensorflow/core/tpu/tpu_defs.h" + +namespace tensorflow { +namespace { +using XlaDeviceCompiler = + DeviceCompiler; +using XlaDeviceExecutablePersistor = + DeviceExecutablePersistor; +using PjRtDeviceCompiler = + DeviceCompiler; +using PjRtDeviceExecutablePersistor = + DeviceExecutablePersistor; + +XlaDeviceCompiler* CreateXlaDeviceCompiler(DeviceType device_type, + xla::LocalClient* local_client) { + auto persistor = std::make_unique( + XlaDeviceExecutablePersistor::Config(), device_type); + auto compiler_client = + std::make_unique(local_client); + return new XlaDeviceCompiler(std::move(persistor), + std::move(compiler_client)); +} + +PjRtDeviceCompiler* CreatePjRtDeviceCompiler(DeviceType device_type, + xla::PjRtClient* pjrt_client) { + auto persistor = std::make_unique( + PjRtDeviceExecutablePersistor::Config(), device_type); + auto compiler_client = + std::make_unique(pjrt_client); + return new PjRtDeviceCompiler(std::move(persistor), + std::move(compiler_client)); +} + +std::vector +GetShapeDeterminationFns() { + XlaHelpers::ShapeRepresentationFn shape_representation_fn = + [](const TensorShape&, DataType, bool, XlaLayoutPreference) { + return xla::Shape(); + }; + XlaShapeLayoutHelpers::LayoutPreferenceFn layout_preference_fn = + [](const TensorShape&, DataType, std::optional) { + return tensorflow::XlaLayoutPreference::kTpuPreferLinearLayout; + }; + return {XlaShapeLayoutHelpers::ShapeDeterminationFns{ + layout_preference_fn, shape_representation_fn}}; +} + +std::unique_ptr CreateXlaDeviceMetadata( + DeviceType compilation_device_type) { + return std::make_unique( + /*device_ordinal=*/0, /*platform=*/nullptr, compilation_device_type, + GetShapeDeterminationFns(), XlaDevice::PaddedShapeFn(), + /*use_multiple_streams=*/false); +} + +std::unique_ptr CreatePjRtDeviceMetadata( + DeviceType compilation_device_type) { + return std::make_unique(compilation_device_type, + GetShapeDeterminationFns()); +} + +class XlaCompilerOptionsTest : public ::testing::Test { + protected: + void SetUp() override { + tensorflow::GetXlaDeviceFlags()->tf_xla_enable_xla_devices = true; + } + + DeviceSetup device_setup_; +}; + +TEST_F(XlaCompilerOptionsTest, PjRtOptionsXlaDevice) { + device_setup_.AddDevicesAndSetUp({DEVICE_XLA_GPU}); + Device* device = device_setup_.GetDevice(DEVICE_XLA_GPU); + DeviceType compilation_device_type = DeviceType(DEVICE_GPU_XLA_JIT); + + se::Platform::Id platform_id = nullptr; + auto xla_device_metadata = CreateXlaDeviceMetadata(compilation_device_type); + std::shared_ptr custom_allocator; + XlaPlatformInfo platform_info( + compilation_device_type, platform_id, xla_device_metadata.get(), + /*pjrt_device_metadata=*/nullptr, custom_allocator); + + XlaCompiler::Options options = GenerateCompilerOptionsForPjRt( + *device_setup_.flr(), device, platform_info, + /*pjrt_device_compiler=*/nullptr); + + EXPECT_EQ(options.device_type, compilation_device_type); + EXPECT_EQ(options.device_ordinal, 0); + EXPECT_NE(options.flib_def, nullptr); + EXPECT_EQ(options.graph_def_version, TF_GRAPH_DEF_VERSION); + EXPECT_FALSE(options.allow_cpu_custom_calls); + EXPECT_FALSE(options.alias_passthrough_params); + // Check if options have the supplied shape determination functions set. + TF_ASSERT_OK_AND_ASSIGN( + auto shape, options.shape_determination_fns.shape_representation_fn( + TensorShape(), DT_FLOAT, false, + tensorflow::XlaLayoutPreference::kTpuPreferLinearLayout)); + EXPECT_EQ(shape, xla::Shape()); + EXPECT_EQ(options.shape_determination_fns.layout_preference_fn( + TensorShape(), DT_FLOAT, std::nullopt), + tensorflow::XlaLayoutPreference::kTpuPreferLinearLayout); +} + +TEST_F(XlaCompilerOptionsTest, PjRtOptionsPjRtBaseDevice) { + // Although DEVICE_CPU isn't a PjRtBaseDevice, we use it here just for testing + // purposes and to keep things simple. Creating a TpuDevice or + // NextPluggableDevice in the context of this unit test is non-trivial. + device_setup_.AddDevicesAndSetUp({DEVICE_CPU}); + Device* device = device_setup_.GetDevice(DEVICE_CPU); + DeviceType compilation_device_type = DeviceType(DEVICE_CPU_XLA_JIT); + + auto pjrt_device_metadata = CreatePjRtDeviceMetadata(compilation_device_type); + XlaPlatformInfo platform_info( + compilation_device_type, /*platform_id=*/nullptr, + /*xla_device_metadata=*/nullptr, + /*pjrt_device_metadata=*/pjrt_device_metadata.get(), + /*device_allocator=*/nullptr); + + XlaCompiler::Options options = GenerateCompilerOptionsForPjRt( + *device_setup_.flr(), device, platform_info, + /*pjrt_device_compiler=*/nullptr); + + EXPECT_EQ(options.device_type, compilation_device_type); + EXPECT_EQ(options.device_ordinal, 0); + EXPECT_NE(options.flib_def, nullptr); + EXPECT_EQ(options.graph_def_version, TF_GRAPH_DEF_VERSION); + EXPECT_FALSE(options.allow_cpu_custom_calls); + EXPECT_FALSE(options.alias_passthrough_params); + // Check if options have the supplied shape determination functions set. + TF_ASSERT_OK_AND_ASSIGN( + auto shape, options.shape_determination_fns.shape_representation_fn( + TensorShape(), DT_FLOAT, false, + tensorflow::XlaLayoutPreference::kTpuPreferLinearLayout)); + EXPECT_EQ(shape, xla::Shape()); + EXPECT_EQ(options.shape_determination_fns.layout_preference_fn( + TensorShape(), DT_FLOAT, std::nullopt), + tensorflow::XlaLayoutPreference::kTpuPreferLinearLayout); +} + +TEST_F(XlaCompilerOptionsTest, PjRtOptionsNonXlaDevice) { + device_setup_.AddDevicesAndSetUp({DEVICE_CPU}); + Device* device = device_setup_.GetDevice(DEVICE_CPU); + DeviceType compilation_device_type = DeviceType(DEVICE_CPU_XLA_JIT); + + XlaPlatformInfo platform_info(compilation_device_type, + /*platform_id=*/nullptr, + /*xla_device_metadata=*/nullptr, + /*pjrt_device_metadata=*/nullptr, + /*device_allocator=*/nullptr); + + auto pjrt_device_compiler = + CreatePjRtDeviceCompiler(compilation_device_type, nullptr); + core::ScopedUnref pjrt_device_compiler_ref(pjrt_device_compiler); + + XlaCompiler::Options options = GenerateCompilerOptionsForPjRt( + *device_setup_.flr(), device, platform_info, pjrt_device_compiler); + + EXPECT_EQ(options.device_type, compilation_device_type); + EXPECT_EQ(options.device_ordinal, 0); + EXPECT_NE(options.flib_def, nullptr); + EXPECT_EQ(options.graph_def_version, TF_GRAPH_DEF_VERSION); + EXPECT_FALSE(options.allow_cpu_custom_calls); + EXPECT_FALSE(options.alias_passthrough_params); + // Check whether options have default shape determination functions set. + TF_ASSERT_OK_AND_ASSIGN( + auto shape, options.shape_determination_fns.shape_representation_fn( + TensorShape(), DT_FLOAT, false, + tensorflow::XlaLayoutPreference::kNoPreference)); + xla::ShapeProto shape_proto; + shape_proto.set_element_type(xla::PrimitiveType::F32); + shape_proto.mutable_layout(); + TF_ASSERT_OK_AND_ASSIGN(auto expected_shape, + xla::Shape::FromProto(shape_proto)); + EXPECT_EQ(shape, expected_shape); + EXPECT_EQ(options.shape_determination_fns.layout_preference_fn( + TensorShape(), DT_FLOAT, std::nullopt), + tensorflow::XlaLayoutPreference::kNoPreference); +} + +TEST_F(XlaCompilerOptionsTest, XlaOptions) { + device_setup_.AddDevicesAndSetUp({DEVICE_XLA_GPU}); + Device* device = device_setup_.GetDevice(DEVICE_XLA_GPU); + + xla::LocalClient* client = xla::ClientLibrary::LocalClientOrDie(); + DeviceType device_type = DeviceType(DEVICE_XLA_GPU); + DeviceType compilation_device_type = DeviceType(DEVICE_GPU_XLA_JIT); + + auto xla_device_compiler = + CreateXlaDeviceCompiler(compilation_device_type, client); + core::ScopedUnref xla_device_compiler_ref(xla_device_compiler); + + se::Platform::Id platform_id = se::host::kHostPlatformId; + auto xla_device_metadata = CreateXlaDeviceMetadata(compilation_device_type); + std::shared_ptr custom_allocator; + XlaPlatformInfo platform_info( + device_type, platform_id, xla_device_metadata.get(), + /*pjrt_device_metadata=*/nullptr, custom_allocator); + + XlaCompiler::Options options = + GenerateCompilerOptions(*xla_device_compiler, *device_setup_.flr(), + device, nullptr, platform_info, false); + + EXPECT_EQ(options.device_type, compilation_device_type); + EXPECT_NE(options.flib_def, nullptr); + EXPECT_EQ(options.graph_def_version, TF_GRAPH_DEF_VERSION); + EXPECT_TRUE(options.allow_cpu_custom_calls); + EXPECT_NE(options.device_allocator, nullptr); + EXPECT_FALSE(options.alias_passthrough_params); + // Check if options have the supplied shape determination functions set. + TF_ASSERT_OK_AND_ASSIGN( + auto shape, options.shape_determination_fns.shape_representation_fn( + TensorShape(), DT_FLOAT, false, + tensorflow::XlaLayoutPreference::kTpuPreferLinearLayout)); + EXPECT_EQ(shape, xla::Shape()); + EXPECT_EQ(options.shape_determination_fns.layout_preference_fn( + TensorShape(), DT_FLOAT, std::nullopt), + tensorflow::XlaLayoutPreference::kTpuPreferLinearLayout); +} + +TEST_F(XlaCompilerOptionsTest, XlaOptionsHasRefVarsNoXlaDeviceMetadata) { + device_setup_.AddDevicesAndSetUp({DEVICE_CPU}); + Device* device = device_setup_.GetDevice(DEVICE_CPU); + + xla::LocalClient* client = xla::ClientLibrary::LocalClientOrDie(); + DeviceType device_type = DeviceType(DEVICE_CPU); + DeviceType compilation_device_type = DeviceType(DEVICE_CPU_XLA_JIT); + + auto xla_device_compiler = + CreateXlaDeviceCompiler(compilation_device_type, client); + core::ScopedUnref xla_device_compiler_ref(xla_device_compiler); + + se::Platform::Id platform_id = se::host::kHostPlatformId; + std::shared_ptr custom_allocator; + XlaPlatformInfo platform_info( + device_type, platform_id, /*xla_device_metadata=*/nullptr, + /*pjrt_device_metadata=*/nullptr, custom_allocator); + + XlaCompiler::Options options = + GenerateCompilerOptions(*xla_device_compiler, *device_setup_.flr(), + device, nullptr, platform_info, false); + + EXPECT_EQ(options.device_type, compilation_device_type); + EXPECT_NE(options.flib_def, nullptr); + EXPECT_EQ(options.graph_def_version, TF_GRAPH_DEF_VERSION); + EXPECT_TRUE(options.allow_cpu_custom_calls); + EXPECT_NE(options.device_allocator, nullptr); + EXPECT_TRUE(options.alias_passthrough_params); + // Check whether options have default shape determination functions set. + TF_ASSERT_OK_AND_ASSIGN( + auto shape, options.shape_determination_fns.shape_representation_fn( + TensorShape(), DT_FLOAT, false, + tensorflow::XlaLayoutPreference::kNoPreference)); + xla::ShapeProto shape_proto; + shape_proto.set_element_type(xla::PrimitiveType::F32); + shape_proto.mutable_layout(); + TF_ASSERT_OK_AND_ASSIGN(auto expected_shape, + xla::Shape::FromProto(shape_proto)); + EXPECT_EQ(shape, expected_shape); + EXPECT_EQ(options.shape_determination_fns.layout_preference_fn( + TensorShape(), DT_FLOAT, std::nullopt), + tensorflow::XlaLayoutPreference::kNoPreference); +} + +TEST_F(XlaCompilerOptionsTest, TfRtTpuOptions) { + device_setup_.AddDevicesAndSetUp({DEVICE_TPU_NODE}); + + // Just use the default local client for testing purposes. + xla::LocalClient* client = xla::ClientLibrary::LocalClientOrDie(); + DeviceType compilation_device_type = DeviceType(DEVICE_TPU_XLA_JIT); + + auto xla_device_compiler = + CreateXlaDeviceCompiler(compilation_device_type, client); + core::ScopedUnref xla_device_compiler_ref(xla_device_compiler); + + XlaCompiler::Options options = GenerateCompilerOptionsForTfrtTpu( + *xla_device_compiler, *device_setup_.flr()); + + EXPECT_EQ(options.device_type, compilation_device_type); + EXPECT_NE(options.flib_def, nullptr); + EXPECT_EQ(options.graph_def_version, TF_GRAPH_DEF_VERSION); + EXPECT_FALSE(options.allow_cpu_custom_calls); + EXPECT_FALSE(options.alias_passthrough_params); +} + +TEST_F(XlaCompilerOptionsTest, GenerateCompileOptions) { + XlaCompiler::CompileOptions option1 = GenerateCompileOptions( + /*has_ref_vars=*/false, /*may_alias_resource_update=*/false); + EXPECT_TRUE(option1.is_entry_computation); + EXPECT_FALSE(option1.always_return_tuple); + EXPECT_FALSE(option1.alias_resource_update); + + XlaCompiler::CompileOptions option2 = GenerateCompileOptions( + /*has_ref_vars=*/false, /*may_alias_resource_update=*/true); + EXPECT_TRUE(option2.alias_resource_update); + + XlaCompiler::CompileOptions option3 = GenerateCompileOptions( + /*has_ref_vars=*/true, /*may_alias_resource_update=*/false); + EXPECT_FALSE(option3.alias_resource_update); + + XlaCompiler::CompileOptions option4 = GenerateCompileOptions( + /*has_ref_vars=*/true, /*may_alias_resource_update=*/true); + EXPECT_FALSE(option4.alias_resource_update); +} + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_cpu_device.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_cpu_device.cc new file mode 100644 index 0000000000000000000000000000000000000000..e417e7709b6a7be10a73d151a80fd513319b6295 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_cpu_device.cc @@ -0,0 +1,133 @@ +/* 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. +==============================================================================*/ + +// Registers the XLA_CPU device, which is an XlaDevice instantiation that runs +// operators using XLA via the XLA "Host" (CPU) backend. + +#include + +#include "absl/memory/memory.h" +#include "tensorflow/compiler/jit/defs.h" +#include "tensorflow/compiler/jit/flags.h" +#include "tensorflow/compiler/jit/kernels/xla_ops.h" +#include "tensorflow/compiler/jit/xla_compile_on_demand_op.h" +#include "tensorflow/compiler/jit/xla_device.h" +#include "tensorflow/compiler/jit/xla_device_ops.h" +#include "tensorflow/compiler/tf2xla/layout_util.h" +#include "tensorflow/compiler/tf2xla/xla_op_registry.h" +#include "xla/stream_executor/platform_manager.h" +#include "tensorflow/core/common_runtime/device_factory.h" +#include "tensorflow/core/lib/core/status.h" + +namespace tensorflow { +using tensorflow::IdentityShapeRepresentationFn; + +class XlaCpuDeviceFactory : public DeviceFactory { + public: + absl::Status ListPhysicalDevices(std::vector* devices) override; + absl::Status CreateDevices( + const SessionOptions& options, const std::string& name_prefix, + std::vector>* devices) override; +}; + +absl::Status XlaCpuDeviceFactory::ListPhysicalDevices( + std::vector* devices) { + XlaDeviceFlags* flags = GetXlaDeviceFlags(); + if (!flags->tf_xla_enable_xla_devices && !XlaDevicesCreationRequired()) { + VLOG(1) << "Not creating XLA devices, tf_xla_enable_xla_devices not set " + "and XLA device creation not requested"; + return absl::OkStatus(); + } + + devices->push_back(absl::StrCat("/physical_device:", DEVICE_XLA_CPU, ":0")); + return absl::OkStatus(); +} + +absl::Status XlaCpuDeviceFactory::CreateDevices( + const SessionOptions& session_options, const std::string& name_prefix, + std::vector>* devices) { + XlaDeviceFlags* flags = GetXlaDeviceFlags(); + if (!flags->tf_xla_enable_xla_devices && !XlaDevicesCreationRequired()) { + VLOG(1) << "Not creating XLA devices, tf_xla_enable_xla_devices not set"; + return absl::OkStatus(); + } + bool compile_on_demand = flags->tf_xla_compile_on_demand; + + XlaOpRegistry::DeviceRegistration registration; + registration.compilation_device_name = DEVICE_CPU_XLA_JIT; + registration.autoclustering_policy = + compile_on_demand + ? XlaOpRegistry::AutoclusteringPolicy::kIfExplicitlyRequested + : XlaOpRegistry::AutoclusteringPolicy::kAlways; + registration.cluster_resource_variable_ops_unsafely = true; + registration.cluster_stack_ops = false; + registration.cluster_tensor_array_ops = true; + registration.cluster_stateful_rng_ops = true; + registration.cluster_control_trigger = true; + registration.elide_assert_and_checknumerics = true; + registration.cluster_variant_ops = true; + registration.cluster_slow_ops = true; + registration.cluster_inaccurate_ops = true; + XlaOpRegistry::RegisterCompilationDevice(DEVICE_XLA_CPU, registration); + + static XlaDeviceOpRegistrations* registrations = + RegisterXlaDeviceKernels(DEVICE_XLA_CPU, DEVICE_CPU_XLA_JIT); + (void)registrations; + + TF_ASSIGN_OR_RETURN(auto platform, + se::PlatformManager::PlatformWithName("Host")); + + XlaDevice::Options options; + options.platform = platform; + options.device_name_prefix = name_prefix; + options.device_name = DEVICE_XLA_CPU; + options.device_ordinal = 0; + options.compilation_device_name = DEVICE_CPU_XLA_JIT; + options.use_multiple_streams = false; + XlaShapeLayoutHelpers::ShapeDeterminationFns shape_representation_fns{ + UseNoPreferenceLayoutFn(), IdentityShapeRepresentationFn()}; + options.shape_determination_fns = {shape_representation_fns}; + auto device = std::make_unique(session_options, options); + + // Setting AcceleratorDeviceInfo because eager runtime relies on the device + // context in tensorflow_accelerator_device_info(). Also, + // tensorflow_accelerator_device_info() == nullptr is used as an IsCPU test. + // We need XlaCpuDevice to be treated not as CPU because it allocates + // XlaTensors, not regular Tensors. + absl::Status status = device->UseAcceleratorDeviceInfo(); + if (!status.ok()) { + errors::AppendToMessage(&status, "while setting up ", DEVICE_GPU_XLA_JIT); + return status; + } + devices->push_back(std::move(device)); + return absl::OkStatus(); +} + +REGISTER_LOCAL_DEVICE_FACTORY(DEVICE_XLA_CPU, XlaCpuDeviceFactory); + +// Kernel registrations + +constexpr std::array kAllXlaCpuTypes = { + {DT_UINT8, DT_QUINT8, DT_UINT16, DT_INT8, DT_QINT8, DT_INT16, DT_INT32, + DT_QINT32, DT_INT64, DT_HALF, DT_FLOAT, DT_DOUBLE, DT_COMPLEX64, + DT_COMPLEX128, DT_BOOL, DT_BFLOAT16, DT_INT4, DT_UINT4}}; + +REGISTER_XLA_LAUNCH_KERNEL(DEVICE_XLA_CPU, XlaLocalLaunchOp, kAllXlaCpuTypes); +REGISTER_XLA_COMPILE_KERNEL(DEVICE_XLA_CPU, XlaCompileOp, kAllXlaCpuTypes); +REGISTER_XLA_RUN_KERNEL(DEVICE_XLA_CPU, XlaRunOp, kAllXlaCpuTypes); + +REGISTER_XLA_DEVICE_KERNELS(DEVICE_XLA_CPU, kAllXlaCpuTypes); + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_device.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_device.cc new file mode 100644 index 0000000000000000000000000000000000000000..e691f306d5524d6c00d247093568879ae71cdd3b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_device.cc @@ -0,0 +1,623 @@ +/* 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/compiler/jit/xla_device.h" + +#include + +#include +#include +#include + +#include "absl/base/call_once.h" +#include "absl/memory/memory.h" +#include "absl/strings/match.h" +#include "tensorflow/compiler/jit/defs.h" +#include "tensorflow/compiler/jit/pjrt_device_context.h" +#include "tensorflow/compiler/jit/xla_compile_on_demand_op.h" +#include "tensorflow/compiler/jit/xla_compile_util.h" +#include "tensorflow/compiler/jit/xla_device_context.h" +#include "tensorflow/compiler/tf2xla/shape_util.h" +#include "tensorflow/compiler/tf2xla/xla_op_registry.h" +#include "xla/client/client_library.h" +#include "xla/service/stream_pool.h" +#include "tensorflow/core/common_runtime/device.h" +#include "tensorflow/core/common_runtime/device_factory.h" +#include "tensorflow/core/common_runtime/dma_helper.h" +#include "tensorflow/core/common_runtime/function.h" +#include "tensorflow/core/common_runtime/graph_constructor.h" +#include "tensorflow/core/common_runtime/renamed_device.h" +#include "tensorflow/core/framework/allocator.h" +#include "tensorflow/core/framework/device_base.h" +#include "tensorflow/core/framework/function.h" +#include "tensorflow/core/framework/kernel_def.pb.h" +#include "tensorflow/core/framework/node_def_builder.h" +#include "tensorflow/core/framework/op_kernel.h" +#include "tensorflow/core/framework/tensor.h" +#include "tensorflow/core/framework/tensor.pb.h" +#include "tensorflow/core/framework/types.h" +#include "tensorflow/core/lib/core/notification.h" +#include "tensorflow/core/lib/core/status.h" +#include "tensorflow/core/platform/logging.h" +#include "tensorflow/core/platform/stream_executor_no_cuda.h" +#include "tensorflow/core/profiler/lib/traceme.h" +#include "tensorflow/core/public/session_options.h" +#include "tensorflow/core/public/version.h" +#include "tensorflow/core/tfrt/common/async_value_tensor.h" +#include "tensorflow/core/util/device_name_utils.h" +#include "tensorflow/core/util/dump_graph.h" +#include "tensorflow/core/util/stream_executor_util.h" + +namespace tensorflow { + +// Default PaddedShapeFn implementation that simply returns the unpadded +// on-device shape. This is accurate for CPU and GPU devices that neither +// transpose nor pad tensors. +absl::Status DefaultPaddedShapeFn(const Tensor& tensor, xla::Shape* shape) { + const tensorflow::XlaTensor* xla_tensor = + tensorflow::XlaTensor::FromTensor(&tensor); + if (xla_tensor == nullptr) { + return TensorShapeToXLAShape(tensor.dtype(), tensor.shape(), shape); + } + + const xla::ShapedBuffer& shaped_buffer = xla_tensor->shaped_buffer(); + *shape = shaped_buffer.on_device_shape(); + return absl::OkStatus(); +} + +// Caches a XlaDeviceAllocator per pair. A +// XlaDeviceAllocator is created on demand and is associated with a +// XlaDevice. It outlives the device itself (for instance, the buffer +// backing a tensor holds a pointer to the allocator for book-keeping, +// and this buffer can outlast the device). +class XlaDeviceAllocatorState { + public: + // Creates or returns a cached XlaDeviceAllocator for a given + // backend and device_ordinal. + static XlaDeviceAllocator* GetOrCreateXlaDeviceAllocator( + const xla::Backend* backend, int device_ordinal); + + private: + // Returns the singleton instance of XlaDeviceAllocatorState. + static XlaDeviceAllocatorState& Singleton(); + XlaDeviceAllocatorState(); + ~XlaDeviceAllocatorState(); + + mutex allocator_mutex_; // Guards the singleton allocator state. + std::unordered_map, + std::unique_ptr, + hash>> + allocators_ TF_GUARDED_BY(allocator_mutex_); + + XlaDeviceAllocatorState(const XlaDeviceAllocatorState&) = delete; + void operator=(const XlaDeviceAllocatorState&) = delete; +}; + +/* static */ XlaDeviceAllocatorState& XlaDeviceAllocatorState::Singleton() { + static auto a = new XlaDeviceAllocatorState; + return *a; +} + +XlaDeviceAllocatorState::XlaDeviceAllocatorState() = default; +XlaDeviceAllocatorState::~XlaDeviceAllocatorState() = default; + +XlaDeviceAllocator* XlaDeviceAllocatorState::GetOrCreateXlaDeviceAllocator( + const xla::Backend* backend, int device_ordinal) { + XlaDeviceAllocatorState& state = Singleton(); + mutex_lock lock(state.allocator_mutex_); + + auto it = state.allocators_.find({backend, device_ordinal}); + if (it != state.allocators_.end()) { + return it->second.get(); + } + + std::unique_ptr alloc = + std::make_unique( + backend->stream_executors()[device_ordinal]); + XlaDeviceAllocator* alloc_ptr = alloc.get(); + state.allocators_[{backend, device_ordinal}] = std::move(alloc); + return alloc_ptr; +} + +namespace { + +static DeviceAttributes BuildXlaDeviceAttributes(const std::string& name_prefix, + const std::string& device_name, + int device_ordinal) { + return Device::BuildDeviceAttributes( + absl::StrCat(name_prefix, "/device:", device_name, ":", device_ordinal), + DeviceType(device_name), Bytes(16ULL << 30), DeviceLocality(), + absl::StrCat("device: ", device_name, " device")); +} + +} // namespace + +XlaDevice::Metadata::Metadata( + int device_ordinal, se::Platform* platform, const DeviceType& device_type, + std::vector + shape_determination_fns, + PaddedShapeFn padded_shape_fn, bool use_multiple_streams) + : device_ordinal_(device_ordinal), + device_type_(device_type), + platform_(platform), + shape_determination_fns_(std::move(shape_determination_fns)), + padded_shape_fn_(std::move(padded_shape_fn)), + use_multiple_streams_(use_multiple_streams) {} + +int XlaDevice::Metadata::device_ordinal() const { return device_ordinal_; } + +se::Platform* XlaDevice::Metadata::platform() const { return platform_; } + +xla::LocalClient* XlaDevice::Metadata::client() const { + auto client = xla::ClientLibrary::GetOrCreateLocalClient(platform_); + return client.value(); +} + +const DeviceType& XlaDevice::Metadata::jit_device_type() const { + return device_type_; +} + +/*static*/ absl::Status XlaDevice::GetMetadataFromDevice( + DeviceBase* device, const XlaDevice::Metadata** metadata) { + *metadata = nullptr; + XlaDevice* xla_device = dynamic_cast(device->UnderlyingDevice()); + if (xla_device == nullptr) { + return absl::InternalError(absl::StrCat( + "Cannot get XLA metadata from non-XLA device \"", device->name(), + "\". GetMetadata must only be called on an XLA device. Either an " + "internal bug has been triggered, or an XLA-specific op has been " + "placed on the wrong device.")); + } + *metadata = &(xla_device->xla_metadata_); + return absl::OkStatus(); +} + +/* static */ absl::Status XlaDevice::GetMetadata(OpKernelContext* ctx, + const Metadata** metadata) { + return GetMetadataFromDevice(ctx->device(), metadata); +} + +/* static */ absl::Status XlaDevice::GetMetadata(OpKernelConstruction* ctx, + const Metadata** metadata) { + return GetMetadataFromDevice(ctx->device(), metadata); +} + +/* static */ mutex XlaDevice::global_mu_(LINKER_INITIALIZED); +/* static */ std::vector>* + XlaDevice::global_compute_streams_ = + new std::vector>; + +XlaDevice::XlaDevice(const SessionOptions& session_options, + const Options& options) + : LocalDevice(session_options, + BuildXlaDeviceAttributes(options.device_name_prefix, + options.device_name, + options.device_ordinal)), + xla_metadata_(options.device_ordinal, options.platform, + DeviceType(options.compilation_device_name), + options.shape_determination_fns, + options.padded_shape_fn ? options.padded_shape_fn + : DefaultPaddedShapeFn, + options.use_multiple_streams), + device_ordinal_(options.device_ordinal), + device_name_(options.device_name), + jit_device_name_(options.compilation_device_name), + platform_(options.platform), + intra_op_parallelism_threads_( + session_options.config.intra_op_parallelism_threads()), + use_multiple_streams_(options.use_multiple_streams), + shape_determination_fns_(options.shape_determination_fns), + allowed_devices_(options.allowed_devices), + use_global_compute_stream_(options.use_global_compute_stream) { + if (options.shape_determination_fns.empty()) { + LOG(ERROR) << "shape_representation_fns must be non-empty."; + } + VLOG(1) << "Created XLA device " << options.compilation_device_name << " " + << options.device_ordinal << " " << this; + VLOG(1) << "XlaDevice options: use_multiple_streams: " + << options.use_multiple_streams << " use_global_compute_stream: " + << options.use_global_compute_stream; + thread_pool_.reset(new thread::ThreadPool(session_options.env, "xla_device", + /*num_threads=*/1)); + + // We have multiple device to device streams to allow for some concurrency + // between transfers. The particular value of '4' is chosen fairly + // arbitrarily. It may be necessary to make this tunable via + // XlaDevice::Options. + static constexpr int kNumDeviceToDeviceStreams = 4; + device_to_device_streams_.resize(kNumDeviceToDeviceStreams); +} + +XlaDevice::~XlaDevice() { + VLOG(1) << "Destroying XLA device " << jit_device_name_ << " " << this; + mutex_lock lock(mu_); + for (const auto& iter : device_contexts_) { + iter->Unref(); + } +} + +absl::StatusOr XlaDevice::GetOrCreateClient() const { + // We lazily create the client because the platform commits to the + // details of the host hardware when the client is created, so we + // don't want to do it until we get a chance to hook the platform up + // to a simulator. + + xla::LocalClientOptions options; + options.set_platform(platform_) + .set_allowed_devices(allowed_devices_) + .set_intra_op_parallelism_threads(intra_op_parallelism_threads_); + return xla::ClientLibrary::GetOrCreateLocalClient(options); +} + +Allocator* XlaDevice::GetAllocator(AllocatorAttributes attr) { + mutex_lock lock(mu_); + return GetAllocatorLocked(attr); +} + +Allocator* XlaDevice::GetAllocatorLocked(AllocatorAttributes attr) { + if (attr.on_host()) { + return cpu_allocator(); + } + + if (xla_allocator_ == nullptr) { + if (UsePjRtForSingleDeviceCompilation(device_name_)) { + VLOG(1) << "XlaDevice " << this << " uses AsyncValueAllocator"; + pjrt_allocator_ = std::make_unique(); + xla_allocator_ = pjrt_allocator_.get(); + } else { + // TODO(b/78468222): This can fail, at least when the backend is GPU and + // there is no GPU on the host. + xla::Backend* backend = GetOrCreateClient().value()->mutable_backend(); + xla_allocator_ = XlaDeviceAllocatorState::GetOrCreateXlaDeviceAllocator( + backend, device_ordinal_); + } + } + return xla_allocator_; +} + +absl::Status XlaDevice::EnsureDeviceContextOk() { + mutex_lock lock(mu_); + return GetDeviceContextLocked().status(); +} + +absl::Status XlaDevice::EnsureStreamOkLocked( + xla::Backend* backend, const std::string& name, + std::shared_ptr* stream, bool* stream_was_changed) { + if (!(*stream) || !(*stream)->ok()) { + xla::StreamPool::Ptr ptr; + TF_ASSIGN_OR_RETURN(ptr, backend->BorrowStream(device_ordinal_)); + *stream = std::shared_ptr(std::move(ptr)); + VLOG(1) << "XlaDevice " << this << " new " << name + << " stream=" << (*stream); + *stream_was_changed = true; + } + return absl::OkStatus(); +} + +absl::StatusOr> +XlaDevice::GetDeviceContextLocked() { + if (UsePjRtForSingleDeviceCompilation(device_name_)) { + if (device_contexts_.empty()) { + for (const auto& iter : shape_determination_fns_) { + auto device_context = new PjRtDeviceContext(iter); + VLOG(1) << "XlaDevice " << this << " new PjRtDeviceContext " + << device_context; + device_contexts_.emplace_back(device_context); + } + if (use_accelerator_device_info_) { + auto accelerator_device_info = + std::make_unique(); + accelerator_device_info->default_context = device_contexts_.at(0); + set_tensorflow_accelerator_device_info(accelerator_device_info.get()); + accelerator_device_info_ = std::move(accelerator_device_info); + VLOG(1) << "XlaDevice " << this << " new AcceleratorDeviceInfo " + << accelerator_device_info_.get(); + } + } + + return device_contexts_; + } + + TF_ASSIGN_OR_RETURN(xla::LocalClient * client, GetOrCreateClient()); + xla::Backend* backend = client->mutable_backend(); + + // Ensure all our streams are valid, borrowing new streams if necessary. + bool need_new_device_context = device_contexts_.empty(); + if (use_global_compute_stream_) { + mutex_lock lock(global_mu_); + if (global_compute_streams_->size() <= device_ordinal_) { + global_compute_streams_->resize(device_ordinal_ + 1, nullptr); + } + + auto& global_stream = global_compute_streams_->at(device_ordinal_); + if (global_stream != nullptr && global_stream->ok()) { + stream_ = global_stream; + } else { + // Directly create the stream here instead of borrowing from the stream + // pool to avoid potential lifetime issues. + TF_ASSIGN_OR_RETURN( + stream_, + backend->stream_executors()[device_ordinal_]->CreateStream()); + TF_RETURN_IF_ERROR(EnsureStreamOkLocked(backend, "stream", &stream_, + &need_new_device_context)); + (*global_compute_streams_)[device_ordinal_] = stream_; + } + } else { + TF_RETURN_IF_ERROR(EnsureStreamOkLocked(backend, "stream", &stream_, + &need_new_device_context)); + } + + std::shared_ptr host_to_device_stream; + std::shared_ptr device_to_host_stream; + std::vector> device_to_device_streams; + if (use_multiple_streams_) { + TF_RETURN_IF_ERROR(EnsureStreamOkLocked(backend, "host_to_device_stream", + &host_to_device_stream_, + &need_new_device_context)); + for (std::shared_ptr& stream : device_to_device_streams_) { + TF_RETURN_IF_ERROR( + EnsureStreamOkLocked(backend, "device_to_device_stream", &stream, + &need_new_device_context)); + } + host_to_device_stream = host_to_device_stream_; + device_to_device_streams = device_to_device_streams_; + // The data transfer requests from device to host could arrive out of order, + // so a single stream would cause deadlock. For this case, + // xla_device_context would borrow a stream for each transfer request. + device_to_host_stream = nullptr; + } else { + host_to_device_stream = stream_; + device_to_host_stream = stream_; + device_to_device_streams = {stream_}; + } + + if (!need_new_device_context) { + return device_contexts_; + } + + // At this point we know we need a new device context. + // Call GetAllocator for the side-effect of ensuring the allocator is created. + GetAllocatorLocked({}); + for (const auto& iter : device_contexts_) { + iter->Unref(); + } + // The XlaDeviceContext keeps a reference count to the streams, and the + // XlaDeviceContext remains live for the duration of a Executor run. This + // ensures that the streams remain live for the duration of a run, even if + // an error is encountered and the streams are replaced with new ones. + for (const auto& iter : shape_determination_fns_) { + auto device_context = new XlaDeviceContext( + stream_, host_to_device_stream, device_to_host_stream, + device_to_device_streams, client, iter, thread_pool_.get()); + VLOG(1) << "XlaDevice " << this << " new XlaDeviceContext " + << device_context; + device_contexts_.emplace_back(device_context); + } + + // Create and set a new AcceleratorDeviceInfo, if necessary. + // + // TODO(b/78232898): This isn't thread-safe; there is a race between the call + // to set_tensorflow_accelerator_device_info() with ops that call the getter + // tensorflow_accelerator_device_info(). This isn't trivially fixed by adding + // locking to those methods; see the bug for details. Our only saving grace at + // the moment is that this race doesn't seem to occur in practice. + if (use_accelerator_device_info_) { + auto accelerator_device_info = + std::make_unique(); + accelerator_device_info->stream = stream_.get(); + accelerator_device_info->default_context = device_contexts_.at(0); + set_tensorflow_accelerator_device_info(accelerator_device_info.get()); + accelerator_device_info_ = std::move(accelerator_device_info); + VLOG(1) << "XlaDevice " << this << " new AcceleratorDeviceInfo " + << accelerator_device_info_.get(); + } + + return device_contexts_; +} + +absl::StatusOr XlaDevice::GetDeviceContextWithIndex(int index) { + mutex_lock lock(mu_); + TF_ASSIGN_OR_RETURN(auto device_contexts, GetDeviceContextLocked()); + return device_contexts.at(index); +} + +absl::StatusOr XlaDevice::GetDeviceContextDefault() { + return GetDeviceContextWithIndex(0); +} + +absl::Status XlaDevice::UseAcceleratorDeviceInfo() { + mutex_lock lock(mu_); + use_accelerator_device_info_ = true; + return GetDeviceContextLocked().status(); +} + +absl::Status XlaDevice::TryGetDeviceContext(DeviceContext** out_context) { + TF_ASSIGN_OR_RETURN(auto device_context, GetDeviceContextDefault()); + device_context->Ref(); + *out_context = device_context; + return absl::OkStatus(); +} + +// Warn about XLA_CPU/XLA_GPU exactly once. +static void ShowXlaDeviceDeprecationWarning( + absl::string_view compilation_device_name) { + static absl::once_flag once; + if (absl::StrContains(compilation_device_name, "CPU") || + absl::StrContains(compilation_device_name, "GPU")) { + absl::call_once(once, [] { + LOG(INFO) << "XLA_GPU and XLA_CPU devices are deprecated and will be " + "removed in subsequent releases. Instead, use either " + "@tf.function(jit_compile=True) for must-compile " + "semantics, or run with TF_XLA_FLAGS=--tf_xla_auto_jit=2 " + "for auto-clustering best-effort compilation."; + }); + } +} + +void XlaDevice::Compute(OpKernel* op_kernel, OpKernelContext* context) { + VLOG(2) << "XlaDevice::Compute " << op_kernel->name() << ":" + << op_kernel->type_string(); + ShowXlaDeviceDeprecationWarning(jit_device_name_.type_string()); + op_kernel->Compute(context); +} + +void XlaDevice::ComputeAsync(AsyncOpKernel* op_kernel, OpKernelContext* context, + AsyncOpKernel::DoneCallback done) { + ShowXlaDeviceDeprecationWarning(jit_device_name_.type_string()); + VLOG(2) << "XlaDevice::ComputeAsync " << op_kernel->name() << ":" + << op_kernel->type_string(); + op_kernel->ComputeAsync(context, done); +} + +absl::Status XlaDevice::Sync() { + VLOG(1) << "XlaDevice::Sync"; + tsl::profiler::TraceMe activity("XlaDevice::Sync", + tsl::profiler::TraceMeLevel::kInfo); + std::shared_ptr stream; + { + mutex_lock lock(mu_); + stream = stream_; + } + if (!stream) return absl::OkStatus(); + + absl::Status status = stream->BlockHostUntilDone(); + TF_RETURN_IF_ERROR(status); + if (!stream->ok()) { + return absl::InternalError("XlaDevice::Sync() failed."); + } + VLOG(1) << "XlaDevice::Sync completed"; + return absl::OkStatus(); +} + +absl::Status XlaDevice::MakeTensorFromProto( + DeviceContext* device_context, const TensorProto& tensor_proto, + const AllocatorAttributes alloc_attrs, Tensor* tensor) { + Tensor parsed(tensor_proto.dtype()); + if (!parsed.FromProto(cpu_allocator(), tensor_proto)) { + return absl::InvalidArgumentError(absl::StrCat( + "Cannot parse tensor from proto: ", tensor_proto.DebugString())); + } + + absl::Status status; + if (alloc_attrs.on_host()) { + *tensor = parsed; + } else { + Allocator* allocator; + { + mutex_lock lock(mu_); + allocator = GetAllocatorLocked(alloc_attrs); + } + Tensor copy(allocator, parsed.dtype(), parsed.shape()); + TF_RETURN_IF_ERROR( + device_context->CopyCPUTensorToDeviceSync(&parsed, this, ©)); + *tensor = copy; + } + VLOG(2) << "Allocated tensor at " << DMAHelper::base(tensor); + return status; +} + +absl::Status XlaDevice::MakeTensorFromProto( + const TensorProto& tensor_proto, const AllocatorAttributes alloc_attrs, + Tensor* tensor) { + VLOG(1) << "XlaDevice::MakeTensorFromProto"; + DeviceContext* device_context; + TF_ASSIGN_OR_RETURN(device_context, GetDeviceContextDefault()); + return MakeTensorFromProto(device_context, tensor_proto, alloc_attrs, tensor); +} + +void XlaDevice::SetAllowsSyncOnCompletion(bool sync_on_completion) { + mutex_lock lock(mu_); + sync_on_completion_ = sync_on_completion; +} + +bool XlaDevice::AllowsSyncOnCompletion() const { + mutex_lock lock(mu_); + return sync_on_completion_; +} + +void XlaDevice::SetHandleDeviceErrorCallback( + std::function callback) { + mutex_lock lock(mu_); + device_error_callback_ = callback; +} + +absl::Status XlaDevice::HandleDeviceError() { + std::function local_device_error_callback; + { + mutex_lock lock(mu_); + local_device_error_callback = device_error_callback_; + } + if (local_device_error_callback != nullptr) { + return local_device_error_callback(); + } + return absl::OkStatus(); +} + +absl::Status XlaDevice::RefreshStatus() { + std::shared_ptr stream; + { + mutex_lock lock(mu_); + stream = stream_; + } + if (!stream) { + return absl::OkStatus(); + } + absl::Status status = stream->RefreshStatus(); + if (!status.ok()) { + // Ignore errors from HandleDeviceError, since by definition the status is + // already non-ok, so there's nothing extra to report if HandleDeviceError + // itself returns an error. + HandleDeviceError().IgnoreError(); + } + return status; +} + +XlaDeviceOpRegistrations* RegisterXlaDeviceKernels( + const char* device, const char* jit_device, + OpKernel* (*factory)(OpKernelConstruction*), + absl::string_view kernel_class_name) { + XlaOpRegistry::RegisterCompilationKernels(); + XlaDeviceOpRegistrations* registrations = new XlaDeviceOpRegistrations; + for (const KernelDef* jit_def : XlaOpRegistry::DeviceKernels( + jit_device, + /*include_compilation_only_kernels=*/false)) { + KernelDef* def = new KernelDef(*jit_def); + const std::unordered_set* constant_inputs = + XlaOpRegistry::CompileTimeConstantInputArgNames(def->op()); + + for (const std::string& arg_name : *constant_inputs) { + def->add_host_memory_arg(arg_name); + } + + def->set_device_type(device); + registrations->op_kernel_registrars.emplace_back( + new kernel_factory::OpKernelRegistrar(def, kernel_class_name, factory)); + } + return registrations; +} + +XlaDeviceOpRegistrations* RegisterXlaDeviceKernels(const char* device, + const char* jit_device) { + // Any op assigned to the device that isn't rewritten by the graph rewriter + // gets executed by an XlaCompileOnDemandOp, which compiles it and executes + // it just-in-time. + auto factory = [](OpKernelConstruction* context) -> OpKernel* { + return new XlaCompileOnDemandOp(context); + }; + return RegisterXlaDeviceKernels(device, jit_device, factory, + /*kernel_class_name=*/"XlaCompileOnDemandOp"); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_device.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_device.h new file mode 100644 index 0000000000000000000000000000000000000000..91843c363690bfa0391bb660c1bad9baa753604a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_device.h @@ -0,0 +1,322 @@ +/* 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. +==============================================================================*/ + +// The XlaDevice executes a TensorFlow graph using the XLA linear algebra +// runtime. +// +// Operators assigned to an XlaDevice are compiled into XLA computations. +// Tensors on an XlaDevice are thin wrappers around XLA ScopedShapedBuffers. +// +// XlaDevice is instantiated separately for each XLA backend (e.g., CPU or GPU), +// under different names (e.g., XLA_CPU or XLA_GPU). + +#ifndef TENSORFLOW_COMPILER_JIT_XLA_DEVICE_H_ +#define TENSORFLOW_COMPILER_JIT_XLA_DEVICE_H_ +#include + +#include "absl/types/optional.h" +#include "tensorflow/compiler/jit/xla_tensor.h" +#include "tensorflow/compiler/tf2xla/layout_util.h" +#include "tensorflow/compiler/tf2xla/xla_compiler.h" +#include "tensorflow/compiler/tf2xla/xla_op_registry.h" +#include "xla/client/local_client.h" +#include "tensorflow/core/common_runtime/device_factory.h" +#include "tensorflow/core/common_runtime/local_device.h" +#include "tensorflow/core/framework/allocator.h" +#include "tensorflow/core/framework/device_base.h" +#include "tensorflow/core/framework/node_def_builder.h" +#include "tensorflow/core/framework/op_kernel.h" +#include "tensorflow/core/framework/resource_mgr.h" +#include "tensorflow/core/framework/tensor.h" +#include "tensorflow/core/framework/types.h" +#include "tensorflow/core/lib/core/status.h" +#include "tensorflow/core/platform/mutex.h" +#include "tensorflow/core/platform/stream_executor_no_cuda.h" +#include "tensorflow/core/tfrt/common/async_value_tensor.h" + +namespace tensorflow { + +class XlaDevice : public LocalDevice { + public: + // Given a tensor, sets `xla::Shape*` the shape of tensor's representation + // on device, fully padded. On error, the contents of `xla::Shape*` + // are undefined. + typedef std::function PaddedShapeFn; + + // Wrapper class to store metadata about the XlaDevice, where it can be + // retrieved e.g., when lazily creating the XlaCompilationCache device. + class Metadata { + public: + Metadata(int device_ordinal, se::Platform* platform, + const DeviceType& device_type, + std::vector + shape_determination_fns, + PaddedShapeFn padded_shape_fn, bool use_multiple_streams); + + // The index of the device on this host. + int device_ordinal() const; + + se::Platform* platform() const; + xla::LocalClient* client() const; + const DeviceType& jit_device_type() const; + const XlaShapeLayoutHelpers::ShapeDeterminationFns& + default_shape_determination_fns() const { + return shape_determination_fns_.at(0); + } + const PaddedShapeFn& padded_shape_fn() const { return padded_shape_fn_; } + + bool UseMultipleStreams() const { return use_multiple_streams_; } + + private: + const int device_ordinal_; + const DeviceType device_type_; + se::Platform* platform_; // Not owned. + std::vector + shape_determination_fns_; + PaddedShapeFn padded_shape_fn_; + const bool use_multiple_streams_; + + Metadata(const Metadata&) = delete; + void operator=(const Metadata&) = delete; + }; + + // Sets `*metadata` to the XlaDevice Metadata in the XLA device used by `ctx`. + static absl::Status GetMetadata(OpKernelContext* ctx, + const Metadata** metadata); + + // Sets `*metadata` to the XlaDevice Metadata in the XLA device used by `ctx`. + static absl::Status GetMetadata(OpKernelConstruction* ctx, + const Metadata** metadata); + + // Sets `*metadata` to the XlaDevice Metadata in the XLA device used by + // `device`. + static absl::Status GetMetadataFromDevice( + DeviceBase* device, const XlaDevice::Metadata** metadata); + + struct Options { + // The StreamExecutor platform. Not owned. Must be non-null. + se::Platform* platform = nullptr; + + // The device name's prefix (e.g., "/task:7") + std::string device_name_prefix; + + // The name of the XLA device (e.g., "XLA_CPU") + std::string device_name; + + // The number of the device. + int device_ordinal = -1; + + // The name of the compilation device (e.g., "XLA_CPU_JIT"); + std::string compilation_device_name; + + // If 'use_multiple_streams' is true, we create separate streams for + // compute, host-to-device, and device-to-host communication. + bool use_multiple_streams = false; + + // If true, the XLA devices with the same device ordinal will share the same + // compute stream. Otherwise each XLA device will having their own compute + // streams. + bool use_global_compute_stream = false; + + // A vector of ShapeDeterminationFn (i.e., a bundle of LayoutSelectionFn, + // ShapeRepresentationFn). Each bundle describes how the on-host shapes of + // a) argument and return value, for entry computations b) variables, for + // all computations, should be represented in XLA. Parameters/return values + // will be shaped according to the function pair, and reshaped back to/from + // their declared shapes for computations. Must be non-empty. + std::vector + shape_determination_fns; + + // If padded_shape_fn is empty, a default implementation that returns + // the logical on-device shape without padding is used. + PaddedShapeFn padded_shape_fn; + + // Set of devices to use. This controls which of the devices on the given + // platform will have resources allocated. For GPUs this will be + // filled from visible_gpu_devices list from session configuration. + std::optional> allowed_devices; + }; + + // Creates a new XLA Device. + XlaDevice(const SessionOptions& session_options, const Options& options); + ~XlaDevice() override; + + Allocator* GetAllocator(AllocatorAttributes attr) override + TF_LOCKS_EXCLUDED(mu_); + void Compute(OpKernel* op_kernel, OpKernelContext* context) override; + void ComputeAsync(AsyncOpKernel* op_kernel, OpKernelContext* context, + AsyncOpKernel::DoneCallback done) override; + absl::Status Sync() override; + + absl::Status TryGetDeviceContext(DeviceContext** out_context) override + TF_LOCKS_EXCLUDED(mu_); + + absl::Status MakeTensorFromProto(const TensorProto& tensor_proto, + const AllocatorAttributes alloc_attrs, + Tensor* tensor) override + TF_LOCKS_EXCLUDED(mu_); + + absl::Status MakeTensorFromProto(DeviceContext* device_context, + const TensorProto& tensor_proto, + const AllocatorAttributes alloc_attrs, + Tensor* tensor); + + const Metadata& metadata() { return xla_metadata_; } + + // Ensures the DeviceContext associated with this XlaDevice is created and + // valid (i.e. all streams are ok). If any state is not valid, a new + // DeviceContext will be created. + // + // TODO(b/111859745): The Eager context needs to call this method to recover + // from failures. + absl::Status EnsureDeviceContextOk() TF_LOCKS_EXCLUDED(mu_); + + // Two convenient methods to get the underlying device context. + // Get the default device context, created by the first + // shape_representation_fn. + absl::StatusOr GetDeviceContextDefault(); + // Get the device context given the index. + absl::StatusOr GetDeviceContextWithIndex(int index); + + // Instructs this XlaDevice to set a AcceleratorDeviceInfo, which holds extra + // information for GPU and TPU devices. + absl::Status UseAcceleratorDeviceInfo() TF_LOCKS_EXCLUDED(mu_); + + // Instructs this XlaDevice to return 'sync_on_completion' for + // AllowsSyncOnCompletion(). + void SetAllowsSyncOnCompletion(bool sync_on_completion) + TF_LOCKS_EXCLUDED(mu_); + bool AllowsSyncOnCompletion() const override TF_LOCKS_EXCLUDED(mu_); + + // Installs an error handling callback when RefreshStatus sees !status.ok(). + void SetHandleDeviceErrorCallback(std::function callback); + + absl::Status RefreshStatus() override TF_LOCKS_EXCLUDED(mu_); + + private: + absl::StatusOr GetOrCreateClient() const; + Allocator* GetAllocatorLocked(AllocatorAttributes attr) + TF_EXCLUSIVE_LOCKS_REQUIRED(mu_); + absl::Status EnsureStreamOkLocked(xla::Backend* backend, + const std::string& name, + std::shared_ptr* stream, + bool* stream_was_changed) + TF_EXCLUSIVE_LOCKS_REQUIRED(mu_); + + // Return a vector of device context, ordered by the sequence in the given + // shape_representation_fns. + absl::StatusOr> GetDeviceContextLocked() + TF_EXCLUSIVE_LOCKS_REQUIRED(mu_); + + // Handles error when RefreshStatus sees !status.ok(). + absl::Status HandleDeviceError(); + + mutable mutex mu_; + // The metadata of this XlaDevice. + const Metadata xla_metadata_; + // Which hardware device in the client's platform this XlaDevice controls. + const int device_ordinal_; + // The name/type of this XlaDevice. eg. "XLA_GPU". + const DeviceType device_name_; + // The name of the device that is used to compile Ops for this XlaDevice. + const DeviceType jit_device_name_; + // The platform for this device. + se::Platform* const platform_; // Not owned. + // Intra-op threads to spawn (from SessionOptions). + const int intra_op_parallelism_threads_; + // Memory allocator associated with this device. + Allocator* xla_allocator_ TF_GUARDED_BY(mu_) = nullptr; // Not owned. + std::unique_ptr pjrt_allocator_ TF_GUARDED_BY(mu_); + + // Stream associated with this device. Operations enqueued on this + // stream are executed on the device. Operations include data + // copying back and forth between CPU and the device, and + // computations enqueued by XLA. + std::shared_ptr stream_ TF_GUARDED_BY(mu_); + // If false, only stream_ is valid and all computation and transfers use + // stream_. If true, computation is performed by stream_ and transfers are + // performed by host_to_device/device_to_device stream or borrowing a stream + // for each device to host transfer. + const bool use_multiple_streams_; + // If use_multiple_streams_, host to device transfers are performed using this + // stream. + std::shared_ptr host_to_device_stream_ TF_GUARDED_BY(mu_); + // If use_multiple_streams_, transfers between different devices are performed + // using these streams. + std::vector> device_to_device_streams_ + TF_GUARDED_BY(mu_); + + // See comments in options. + std::vector + shape_determination_fns_; + + // A list of the device context accessed by all users of the XlaDevice, set by + // calls to EnsureDeviceContextOk. The number of device conetexts is based on + // the number of shape representation functions in XlaDevice::Options. If + // accelerator_device_info_ is non-null, this pointer is also filled in to + // that struct. DeviceContext is a ref-counted object. + std::vector device_contexts_ TF_GUARDED_BY(mu_); + + // Holds extra information for GPU and TPU devices, e.g. the device context. + bool use_accelerator_device_info_ TF_GUARDED_BY(mu_) = false; + std::unique_ptr accelerator_device_info_ + TF_GUARDED_BY(mu_); + + // Thread pool used for running closures + std::unique_ptr thread_pool_; + + // True if the device allows XlaDevice::Sync to be called on completion + // regardless of status. + bool sync_on_completion_ TF_GUARDED_BY(mu_) = true; + + // A callback that will be invoked when RefreshStatus sees a status error. + std::function device_error_callback_ TF_GUARDED_BY(mu_); + + // Set of devices to use. This controls which of the devices on the given + // platform will have resources allocated. For GPUs this will be + // filled from visible_gpu_devices list from session configuration. + std::optional> allowed_devices_; + + const bool use_global_compute_stream_; + + // A static vector with device_ordinal as its index, describing the global + // compute streams used in each XLA device. It is only used if + // `use_global_compute_stream` in `XlaDevice::Options` is set to true. + static mutex global_mu_; + static std::vector>* global_compute_streams_ + TF_GUARDED_BY(global_mu_); +}; + +// Builds OpKernel registrations on 'device' for the JIT operators +// registered on 'jit_device'. Returns ownership of a XlaDeviceOpRegistrations +// object that encapsulates the kernel registrations. +struct XlaDeviceOpRegistrations { + std::vector> + op_kernel_registrars; +}; + +XlaDeviceOpRegistrations* RegisterXlaDeviceKernels( + const char* device, const char* jit_device, + OpKernel* (*factory)(OpKernelConstruction*), + absl::string_view kernel_class_name); + +XlaDeviceOpRegistrations* RegisterXlaDeviceKernels(const char* device, + const char* jit_device); + +absl::Status DefaultPaddedShapeFn(const Tensor& tensor, xla::Shape* shape); + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_XLA_DEVICE_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_device_compiler_client.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_device_compiler_client.cc new file mode 100644 index 0000000000000000000000000000000000000000..923cce0e1ae9de7fbe27e899e5358a911a535f55 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_device_compiler_client.cc @@ -0,0 +1,116 @@ +/* Copyright 2022 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/compiler/jit/xla_device_compiler_client.h" + +#include +#include +#include +#include + +#include "xla/client/local_client.h" + +namespace tensorflow { +namespace { +std::vector GetShapePointers( + absl::Span shapes) { + std::vector shape_ptrs; + shape_ptrs.reserve(shapes.size()); + for (const auto& shape : shapes) { + shape_ptrs.push_back(&shape); + } + return shape_ptrs; +} +} // namespace + +absl::StatusOr> +XlaDeviceCompilerClient::BuildExecutable( + const XlaCompiler::Options& options, + const XlaCompiler::CompilationResult& result) { + VLOG(2) << "Compiling to xla::LocalExecutable."; + + std::vector argument_layouts = + GetShapePointers(result.xla_input_shapes); + xla::ExecutableBuildOptions build_options = GetExecutableBuildOptions( + options, result, client_->default_device_ordinal()); + TF_ASSIGN_OR_RETURN( + auto executables, + client_->Compile(*result.computation, argument_layouts, build_options)); + TF_RET_CHECK(executables.size() == 1); + return std::move(executables[0]); +} + +absl::StatusOr XlaDeviceCompilerClient::SerializeExecutable( + const xla::LocalExecutable& executable) { + if (executable.executable() == nullptr) { + return absl::FailedPreconditionError( + "Executable not found for serialization."); + } + + VLOG(1) + << "Exporting xla::LocalExecutable as an xla::AotCompilationResult and " + "serializing it to string."; + xla::Compiler* compiler = client_->backend().compiler(); + auto exported = compiler->Export(executable.executable()); + if (exported.ok()) { + return (*exported)->SerializeAsString(); + } + + return exported.status(); +} + +absl::StatusOr XlaDeviceCompilerClient::BuildSerializedExecutable( + const XlaCompiler::Options& options, + const XlaCompiler::CompilationResult& result) { + VLOG(2) << "Compiling to xla::AotCompilationResult and serializing it"; + + std::vector argument_layouts = + GetShapePointers(result.xla_input_shapes); + xla::ExecutableBuildOptions build_options = GetExecutableBuildOptions( + options, result, client_->default_device_ordinal()); + TF_ASSIGN_OR_RETURN( + std::vector> aot_results, + client_->CompileAheadOfTime(*result.computation, argument_layouts, + build_options)); + TF_RET_CHECK(aot_results.size() == 1); + return aot_results[0]->SerializeAsString(); +} + +absl::StatusOr> +XlaDeviceCompilerClient::LoadExecutable( + const XlaCompiler::Options& options, + const XlaCompiler::CompilationResult& result, + const std::string& serialized_executable) { + VLOG(2) << "Loading xla::LocalExecutable from a serialized " + "xla::AotCompilationResult"; + + xla::ExecutableBuildOptions build_options = GetExecutableBuildOptions( + options, result, client_->default_device_ordinal()); + return client_->Load(serialized_executable, build_options); +} + +void XlaDeviceCompilerClient::WaitForProgramsToFinish() { + if (client_ == nullptr) return; + + for (auto* executor : client_->backend().stream_executors()) { + bool ok = executor->SynchronizeAllActivity(); + if (!ok) { + LOG(ERROR) << "Error synchronizing activity while waiting for all " + "programs to complete"; + } + } +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_device_compiler_client.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_device_compiler_client.h new file mode 100644 index 0000000000000000000000000000000000000000..36e5a022b7b4ae79d2cf16911eb0557bcb77f932 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_device_compiler_client.h @@ -0,0 +1,69 @@ +/* Copyright 2022 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_XLA_DEVICE_COMPILER_CLIENT_H_ +#define TENSORFLOW_COMPILER_JIT_XLA_DEVICE_COMPILER_CLIENT_H_ + +#include +#include +#include + +#include "tensorflow/compiler/jit/device_compiler_client.h" +#include "xla/client/local_client.h" + +namespace tensorflow { + +class XlaDeviceCompilerClient + : public DeviceCompilerClient { + public: + explicit XlaDeviceCompilerClient(xla::LocalClient* client) + : client_(client) {} + + absl::StatusOr> BuildExecutable( + const XlaCompiler::Options& options, + const XlaCompiler::CompilationResult& result) override; + + // Returns a serialized AOT result obtained by exporting the available + // `executable` using the XlaCompiler. + absl::StatusOr SerializeExecutable( + const xla::LocalExecutable& executable) override; + + // Returns a serialized AOT result obtained by compiling `result` into an AOT + // result. + absl::StatusOr BuildSerializedExecutable( + const XlaCompiler::Options& options, + const XlaCompiler::CompilationResult& result) override; + + // Loads a serialized AOT result (`serialized_executable`) into an + // xla::LocalExecutable and returns it. + absl::StatusOr> LoadExecutable( + const XlaCompiler::Options& options, + const XlaCompiler::CompilationResult& result, + const std::string& serialized_executable) override; + + void WaitForProgramsToFinish() override; + + xla::LocalClient* client() const override { return client_; } + + private: + xla::LocalClient* const client_; + + XlaDeviceCompilerClient(const XlaDeviceCompilerClient&) = delete; + void operator=(const XlaDeviceCompilerClient&) = delete; +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_XLA_DEVICE_COMPILER_CLIENT_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_device_context.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_device_context.cc new file mode 100644 index 0000000000000000000000000000000000000000..a2fb969261da8923cbd368826ff4d5c45248b8f2 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_device_context.cc @@ -0,0 +1,334 @@ +/* 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/compiler/jit/xla_device_context.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "absl/log/check.h" +#include "absl/log/log.h" +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" +#include "absl/synchronization/mutex.h" +#include "tensorflow/compiler/jit/xla_tensor.h" +#include "tensorflow/compiler/tf2xla/layout_util.h" +#include "tensorflow/compiler/tf2xla/literal_util.h" +#include "tensorflow/compiler/tf2xla/xla_helpers.h" +#include "xla/client/local_client.h" +#include "xla/layout_util.h" +#include "xla/literal.h" +#include "xla/service/stream_pool.h" +#include "xla/shape.h" +#include "xla/shape_util.h" +#include "xla/status_macros.h" +#include "xla/stream_executor/allocator_stats.h" +#include "xla/stream_executor/event.h" +#include "xla/stream_executor/stream.h" +#include "xla/stream_executor/stream_executor.h" +#include "xla/tsl/platform/errors.h" +#include "xla/tsl/platform/statusor.h" +#include "tensorflow/core/common_runtime/dma_helper.h" +#include "tensorflow/core/framework/allocator.h" +#include "tensorflow/core/framework/device.h" +#include "tensorflow/core/framework/tensor.h" +#include "tensorflow/core/framework/tensor_reference.h" +#include "tensorflow/core/platform/errors.h" +#include "tensorflow/core/platform/status.h" +#include "tensorflow/core/platform/threadpool.h" + +namespace tensorflow { + +// The allocator used for Tensors assigned to the XLA device. +XlaDeviceAllocator::XlaDeviceAllocator( + stream_executor::StreamExecutor* stream_executor) + : stream_executor_(stream_executor) {} + +XlaDeviceAllocator::~XlaDeviceAllocator() = default; + +std::string XlaDeviceAllocator::Name() { return "xla"; } + +void* XlaDeviceAllocator::AllocateRaw(size_t alignment, size_t num_bytes) { + // We always return an empty XlaTensor object, encoded as an opaque tagged + // pointer. We can return an empty object and ignore num_bytes here because we + // have control over all of the uses of this device tensor, and can lazily + // allocate memory when used. This allows us to also know the shape of the + // allocated Tensor, which is useful if the device's tensor representation + // differs from the host. + return XlaTensor::ToOpaquePointer(new XlaTensor()); +} + +void XlaDeviceAllocator::DeallocateRaw(void* ptr) { + delete XlaTensor::FromOpaquePointer(ptr); +} + +std::optional XlaDeviceAllocator::GetStats() { + std::optional se_stats = + stream_executor_->GetAllocatorStats(); + if (!se_stats) { + return std::nullopt; + } + + tensorflow::AllocatorStats tf_stats; + tf_stats.num_allocs = se_stats->num_allocs; + tf_stats.bytes_in_use = se_stats->bytes_in_use; + tf_stats.peak_bytes_in_use = se_stats->peak_bytes_in_use; + tf_stats.largest_alloc_size = se_stats->largest_alloc_size; + tf_stats.bytes_limit = se_stats->bytes_limit; + tf_stats.bytes_reserved = se_stats->bytes_reserved; + tf_stats.peak_bytes_reserved = se_stats->peak_bytes_reserved; + tf_stats.bytes_reservable_limit = se_stats->bytes_reservable_limit; + tf_stats.largest_free_block_bytes = se_stats->largest_free_block_bytes; + return tf_stats; +} + +bool XlaDeviceAllocator::ClearStats() { + if (!stream_executor_->SynchronizeAllActivity()) { + return false; + } + return stream_executor_->ClearAllocatorStats(); +} + +XlaDeviceContext::XlaDeviceContext( + std::shared_ptr compute_stream, + std::shared_ptr host_to_device_stream, + std::shared_ptr device_to_host_stream, + std::vector> device_to_device_streams, + xla::LocalClient* client, + XlaShapeLayoutHelpers::ShapeDeterminationFns shape_determination_fns, + thread::ThreadPool* thread_pool) + : stream_(std::move(compute_stream)), + host_to_device_stream_(std::move(host_to_device_stream)), + device_to_host_stream_(std::move(device_to_host_stream)), + device_to_device_streams_(std::move(device_to_device_streams)), + client_(client), + transfer_manager_(client->backend().transfer_manager()), + shape_determination_fns_(std::move(shape_determination_fns)), + thread_pool_(thread_pool) { + CHECK(host_to_device_stream_ != nullptr); + CHECK(stream_ != nullptr); +} + +void XlaDeviceContext::CopyTensorInSameDevice(const Tensor* input_tensor, + Device* device, + Tensor* output_tensor, + StatusCallback done) const { + done( + absl::UnimplementedError("XLA->XLA same-device copies not implemented.")); +} + +void XlaDeviceContext::CopyCPUTensorToDevice(const Tensor* cpu_tensor, + Device* device, + Tensor* device_tensor, + StatusCallback done, + bool sync_dst_compute) const { + if (cpu_tensor->NumElements() == 0) { + VLOG(2) << "CopyCPUTensorToDevice empty tensor"; + done(absl::OkStatus()); + return; + } + + VLOG(2) << "CopyCPUTensorToDevice " << this << " " + << reinterpret_cast(cpu_tensor->tensor_data().data()) + << " " + << reinterpret_cast(device_tensor->tensor_data().data()) + << " " << cpu_tensor->NumElements() << " " + << cpu_tensor->shape().DebugString() << " " + << device_tensor->shape().DebugString(); + + XlaTensor* xla_tensor = XlaTensor::FromTensor(device_tensor); + CHECK(xla_tensor); + + XlaLayoutPreference layout_preference = + shape_determination_fns_.layout_preference_fn( + device_tensor->shape(), device_tensor->dtype(), std::nullopt); + absl::Status status = [&]() -> absl::Status { + TF_ASSIGN_OR_RETURN(xla::Shape shape, + shape_determination_fns_.shape_representation_fn( + device_tensor->shape(), device_tensor->dtype(), + /*fast_mem=*/false, layout_preference)); + + // The device tensor should always be fresh. + TF_RET_CHECK(!xla_tensor->has_shaped_buffer()); + + TF_RETURN_IF_ERROR( + xla_tensor->AllocateShapedBuffer(device_tensor->dtype(), shape, client_, + stream_->parent()->device_ordinal())); + + // The cpu_tensor and literal that we created here hold the data of host + // tensor in descending layout. The layout could be different from layout in + // device_tensor (but the logical shape has to be the same). The + // transfer_manager is responsible to do corresponding transposing when + // transferring the data to device. + xla::BorrowingLiteral literal( + static_cast(DMAHelper::base(cpu_tensor)), + xla::ShapeUtil::MakeShape(shape.element_type(), shape.dimensions())); + + VLOG(2) << "Transfer to device as literal: " << literal.ToString() << " " + << xla_tensor->shaped_buffer().ToString(); + if (UseMultipleStreams() && + !transfer_manager_->CanShapedBufferBeAccessedNow( + stream_->parent(), xla_tensor->shaped_buffer())) { + // Initially wait for the compute stream so that memory allocations are + // synchronized. + TF_RETURN_IF_ERROR(host_to_device_stream_->WaitFor(stream_.get())); + } + + TF_RETURN_IF_ERROR(transfer_manager_->TransferLiteralToDeviceAsync( + host_to_device_stream_.get(), literal, xla_tensor->shaped_buffer())); + + if (UseMultipleStreams()) { + TF_ASSIGN_OR_RETURN(std::shared_ptr event, + stream_->parent()->CreateEvent()); + TF_RETURN_IF_ERROR(host_to_device_stream_->RecordEvent(event.get())); + xla_tensor->ResetDefinitionEvent(std::move(event), + host_to_device_stream_.get()); + } + + return absl::OkStatus(); + }(); + if (!status.ok()) { + done(status); + return; + } + + // Create a reference to hold onto cpu_tensor until after the literal has + // been transferred + TensorReference ref(*cpu_tensor); + if (UseMultipleStreams()) { + // Unref the host tensor when the transfer completes. + // We don't defer the call to done() onto the stream here, and the reasons + // why this is correct are subtle. We assume that: + // a) all consumers of the device tensor will wait for its definition event. + // b) if the tensor is destroyed, then the memory allocator will not hand + // out the same buffers until the transfer has completed. + status = host_to_device_stream_->DoHostCallback([ref]() { ref.Unref(); }); + done(status); + } else { + status = host_to_device_stream_->DoHostCallback([ref, done]() { + ref.Unref(); + done(absl::OkStatus()); + }); + if (!status.ok()) { + done(status); + } + } +} + +void XlaDeviceContext::CopyDeviceTensorToCPU(const Tensor* device_tensor, + absl::string_view tensor_name, + Device* device, Tensor* cpu_tensor, + StatusCallback done) { + if (device_tensor->NumElements() == 0) { + VLOG(2) << "CopyDeviceTensorToCPU empty tensor"; + done(absl::OkStatus()); + return; + } + VLOG(2) << "CopyDeviceTensorToCPU " + << reinterpret_cast(device_tensor->tensor_data().data()) + << " " + << reinterpret_cast(cpu_tensor->tensor_data().data()) + << " " << device_tensor->NumElements() << " " + << cpu_tensor->shape().DebugString() << " " + << device_tensor->shape().DebugString(); + + std::shared_ptr device_to_host_stream; + if (device_to_host_stream_) { + device_to_host_stream = device_to_host_stream_; + } else { + absl::StatusOr ptr_or_status = + client_->mutable_backend()->BorrowStream( + stream_->parent()->device_ordinal()); + if (!ptr_or_status.status().ok()) { + done(ptr_or_status.status()); + return; + } + device_to_host_stream = + std::shared_ptr(std::move(ptr_or_status.value())); + } + + XlaTensor* xla_tensor = XlaTensor::FromTensor(device_tensor); + xla_tensor->WaitForDefinitionEventOnStream(device_to_host_stream.get()); + + // Transfer manager requires the shape of the shaped buffer to be the same as + // literal shape except for the layout. Set the literal to use xla_tensor's + // shape as it is derived from the cpu_tensor's shape using + // shape_representation_fn_. + xla::MutableBorrowingLiteral literal; + CHECK_OK(HostTensorToMutableBorrowingLiteral( + xla::LayoutUtil::GetWithDefaultLayout( + xla_tensor->shaped_buffer().on_host_shape()), + cpu_tensor, &literal)); + + TensorReference ref(*device_tensor); + const bool device_allows_sync_on_completion = + device->AllowsSyncOnCompletion(); + // Explicitly capture device_to_host_stream to make sure the stream is alive + // before the transfer finishes. + transfer_manager_->TransferLiteralFromDevice( + device_to_host_stream.get(), xla_tensor->shaped_buffer(), literal, + [this, ref, xla_tensor, done, device_to_host_stream, + device_allows_sync_on_completion](absl::Status status) { + absl::Status done_status = status; + VLOG(2) << "Transfer from device as literal: " + << xla_tensor->shaped_buffer().ToString(); + // For devices don't allow sync on completion, the device execution is + // deferred. We check the execution stream status here to avoid wrong + // results from a failed stream being propagated to following + // host-side ops. + if (!device_allows_sync_on_completion) { + done_status.Update(xla_tensor->RefreshStatusOfStreams()); + } + done(done_status); + ref.Unref(); + // If a stream is in a bad state, it gets deleted when it's returned to + // the stream pool, i.e. when it leaves this scope. However, a stream + // deleting itself in a host callback on itself can cause bad behaviors + // on some platforms. Releasing it in another stream to avoid that. + if (!device_allows_sync_on_completion && + !device_to_host_stream->RefreshStatus().ok()) { + auto status_or_new_stream = client_->mutable_backend()->BorrowStream( + stream_->parent()->device_ordinal()); + if (status_or_new_stream.ok()) { + status_or_new_stream.value() + ->DoHostCallback([device_to_host_stream] {}) + .IgnoreError(); + } + } + }); +} + +se::Stream* XlaDeviceContext::GetDeviceToDeviceStream() { + DCHECK_GT(device_to_device_streams_.size(), 0); + absl::MutexLock lock(mu_); + int stream = next_stream_; + next_stream_ = (next_stream_ + 1) % device_to_device_streams_.size(); + return device_to_device_stream(stream); +} + +absl::Status XlaDeviceContext::ThenExecute(Device* device, + stream_executor::Stream* stream, + std::function func) { + VLOG(2) << "XlaDeviceContext::ThenExecute"; + return stream->DoHostCallback(std::move(func)); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_device_context.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_device_context.h new file mode 100644 index 0000000000000000000000000000000000000000..cbd01a83456fdc2cfadb0d8f16814acd1033c244 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_device_context.h @@ -0,0 +1,127 @@ +/* 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_XLA_DEVICE_CONTEXT_H_ +#define TENSORFLOW_COMPILER_JIT_XLA_DEVICE_CONTEXT_H_ + +#include + +#include "absl/synchronization/mutex.h" +#include "tensorflow/compiler/jit/xla_tensor.h" +#include "tensorflow/compiler/tf2xla/layout_util.h" +#include "tensorflow/compiler/tf2xla/xla_compiler.h" +#include "xla/client/local_client.h" +#include "tensorflow/core/framework/allocator.h" +#include "tensorflow/core/framework/device_base.h" +#include "tensorflow/core/lib/core/status.h" + +namespace tensorflow { + +// The allocator used for Tensors assigned to the XLA device. The allocator +// ignores the alignment and size of the request and always returns a new, +// empty, XlaTensor. +class XlaDeviceAllocator : public Allocator { + public: + XlaDeviceAllocator(se::StreamExecutor* stream_executor); + ~XlaDeviceAllocator() override; + + std::string Name() override; + + void* AllocateRaw(size_t alignment, size_t num_bytes) override; + void DeallocateRaw(void* ptr) override; + std::optional GetStats() override; + bool ClearStats() override; + + private: + // The stream executor of the device. + se::StreamExecutor* stream_executor_; +}; + +// Helper class for managing data transfers between host and XLA devices. +class XlaDeviceContext : public DeviceContext { + public: + explicit XlaDeviceContext( + std::shared_ptr compute_stream, + std::shared_ptr host_to_device_stream, + std::shared_ptr device_to_host_stream, + std::vector> device_to_device_streams, + xla::LocalClient* client, + XlaShapeLayoutHelpers::ShapeDeterminationFns shape_determination_fns, + thread::ThreadPool* thread_pool); + + void CopyCPUTensorToDevice(const Tensor* cpu_tensor, Device* device, + Tensor* device_tensor, StatusCallback done, + bool sync_dst_compute) const override; + void CopyDeviceTensorToCPU(const Tensor* device_tensor, + absl::string_view tensor_name, Device* device, + Tensor* cpu_tensor, StatusCallback done) override; + void CopyTensorInSameDevice(const Tensor* input_tensor, Device* device, + Tensor* output_tensor, + StatusCallback done) const override; + + xla::LocalClient* client() const { return client_; } + se::Stream* stream() const override { return stream_.get(); } + se::Stream* host_to_device_stream() const { + return host_to_device_stream_.get(); + } + se::Stream* device_to_device_stream(int index) const { + return device_to_device_streams_.at(index).get(); + } + xla::TransferManager* transfer_manager() const { return transfer_manager_; } + const XlaShapeLayoutHelpers::ShapeDeterminationFns& shape_determination_fns() + const { + return shape_determination_fns_; + } + + // Returns a device-to-device stream, in round-robin fashion. + se::Stream* GetDeviceToDeviceStream(); + + absl::Status ThenExecute(Device* device, stream_executor::Stream* stream, + std::function func) override; + + private: + bool UseMultipleStreams() const { return stream_ != host_to_device_stream_; } + + // The main compute stream of the device, used to synchronize the transfer + // streams if they are set. + std::shared_ptr stream_; + // The stream to use for transferring data from host to device. Can be + // idential to stream_, but must not be nullptr. + std::shared_ptr host_to_device_stream_; + // The stream to use for transferring data from device to host. Can be + // idential to stream_. If nullptr, borrow a stream from backend for each + // transfer request to support out-of-order requests. + std::shared_ptr device_to_host_stream_; + // Streams to use for transferring data directly between different devices, + // e.g., over NVLINK. + std::vector> device_to_device_streams_; + + // For the underlying memory allocator and XLA's TransferManager. + xla::LocalClient* client_; + // Transfer manager, for marshalling data to and from the device. + xla::TransferManager* transfer_manager_; + + XlaShapeLayoutHelpers::ShapeDeterminationFns shape_determination_fns_; + + // Thread pool used for running closures + thread::ThreadPool* thread_pool_; + + absl::Mutex mu_; + int next_stream_ TF_GUARDED_BY(mu_) = 0; +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_XLA_DEVICE_CONTEXT_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_device_ops.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_device_ops.cc new file mode 100644 index 0000000000000000000000000000000000000000..54763abd1fc37ef157b65f9e07149ea8a39b9e9d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_device_ops.cc @@ -0,0 +1,72 @@ +/* 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/compiler/jit/xla_device_ops.h" + +#include + +#include "tensorflow/compiler/jit/xla_tensor.h" + +namespace tensorflow { + +XlaDeviceDummyOp::XlaDeviceDummyOp(OpKernelConstruction* ctx) : OpKernel(ctx) {} + +void XlaDeviceDummyOp::Compute(OpKernelContext* ctx) { + LOG(FATAL) << "Attempted to execute Op " << name() << " type " + << type_string() << " on an XLA device. This should never happen."; +} + +XlaAssignVariableOp::XlaAssignVariableOp(OpKernelConstruction* c) + : OpKernel(c) { + OP_REQUIRES_OK(c, c->GetAttr("dtype", &dtype_)); +} + +void XlaAssignVariableOp::Compute(OpKernelContext* context) { + OP_REQUIRES(context, dtype_ == context->input(1).dtype(), + absl::InvalidArgumentError(absl::StrCat( + "Variable and value dtypes don't match; respectively, ", + DataTypeString(dtype_), " and ", + DataTypeString(context->input(1).dtype())))); + core::RefCountPtr variable; + const Tensor& value = context->input(1); + // Note: every resource-variable-manipulating op assumes copy-on-write + // semantics, and creates a copy of the variable's Tensor if its refcount is + // bigger than 1 when we try to modify it. This means we never need to copy + // the original tensor for AssignVariableOp; even if there are other live + // users of it we know none can modify it so this is always safe (even in + // esoteric cases where the same tensor is used to initialize multiple + // variables or the tensor is a constant this is safe, as future writes will + // trigger copies). + OP_REQUIRES_OK(context, LookupOrCreateResource( + context, HandleFromInput(context, 0), &variable, + [this, &value](Var** ptr) { + *ptr = new Var(dtype_); + *(*ptr)->tensor() = value; + (*ptr)->is_initialized = true; + return absl::OkStatus(); + })); + mutex_lock ml(*variable->mu()); + OP_REQUIRES( + context, + !variable->is_initialized || variable->tensor()->dtype() == dtype_, + absl::InvalidArgumentError( + absl::StrCat("Trying to assign variable with wrong dtype. Expected ", + DataTypeString(variable->tensor()->dtype()), " got ", + DataTypeString(dtype_)))); + variable->is_initialized = true; + *variable->tensor() = value; +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_device_ops.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_device_ops.h new file mode 100644 index 0000000000000000000000000000000000000000..e643bf913aea8fa71eb832d6ae7cd8b1880a01be --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_device_ops.h @@ -0,0 +1,256 @@ +/* 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. +==============================================================================*/ + +// Common kernel registrations for XLA devices. + +#ifndef TENSORFLOW_COMPILER_JIT_XLA_DEVICE_OPS_H_ +#define TENSORFLOW_COMPILER_JIT_XLA_DEVICE_OPS_H_ + +#include "tensorflow/core/framework/op_kernel.h" +#include "tensorflow/core/framework/resource_mgr.h" +#include "tensorflow/core/kernels/constant_op.h" +#include "tensorflow/core/kernels/data/finalize_dataset_op.h" +#include "tensorflow/core/kernels/data/generator_dataset_op.h" +#include "tensorflow/core/kernels/data/iterator_ops.h" +#include "tensorflow/core/kernels/data/optional_ops.h" +#include "tensorflow/core/kernels/data/options_dataset_op.h" +#include "tensorflow/core/kernels/data/prefetch_dataset_op.h" +#include "tensorflow/core/kernels/fifo_queue.h" +#include "tensorflow/core/kernels/function_ops.h" +#include "tensorflow/core/kernels/identity_op.h" +#include "tensorflow/core/kernels/resource_variable_ops.h" +#include "tensorflow/core/kernels/shape_ops.h" +#include "tensorflow/core/kernels/variable_ops.h" + +namespace tensorflow { + +// Dummy OpKernel, used for kernels assigned to an XLA device that should be +// compiled. Should never be called at runtime since such ops should be +// rewritten to a XlaLaunch op. If it is called, it means the placer placed an +// operator on an XLA device but the compiler did not compile it. +class XlaDeviceDummyOp : public OpKernel { + public: + explicit XlaDeviceDummyOp(OpKernelConstruction* ctx); + void Compute(OpKernelContext* ctx) override; +}; + +class XlaAssignVariableOp : public OpKernel { + public: + explicit XlaAssignVariableOp(OpKernelConstruction* c); + void Compute(OpKernelContext* context) override; + + private: + DataType dtype_; +}; + +#define REGISTER_XLA_LAUNCH_KERNEL(DEVICE, KERNEL, TYPES) \ + REGISTER_KERNEL_BUILDER(Name("XlaLaunch") \ + .Device(DEVICE) \ + .HostMemory("constants") \ + .HostMemory("resources"), \ + KERNEL); + +#define REGISTER_XLA_COMPILE_KERNEL(DEVICE, KERNEL, TYPES) \ + REGISTER_KERNEL_BUILDER(Name("_XlaCompile") \ + .Device(DEVICE) \ + .HostMemory("constants") \ + .HostMemory("key") \ + .HostMemory("compilation_successful") \ + .HostMemory("resources"), \ + KERNEL); + +#define REGISTER_XLA_RUN_KERNEL(DEVICE, KERNEL, TYPES) \ + REGISTER_KERNEL_BUILDER(Name("_XlaRun").Device(DEVICE), KERNEL); + +#define REGISTER_XLA_DEVICE_KERNELS(DEVICE, TYPES) \ + REGISTER_KERNEL_BUILDER( \ + Name("Const").Device(DEVICE).TypeConstraint("dtype", TYPES), \ + ConstantOp); \ + REGISTER_KERNEL_BUILDER( \ + Name("Identity").Device(DEVICE).TypeConstraint("T", TYPES), IdentityOp); \ + \ + REGISTER_KERNEL_BUILDER( \ + Name("VarHandleOp").Device(DEVICE).HostMemory("resource"), VarHandleOp); \ + REGISTER_KERNEL_BUILDER( \ + Name("_VarHandlesOp").Device(DEVICE).HostMemory("resources"), \ + ResourceHandlesOp); \ + REGISTER_KERNEL_BUILDER( \ + Name("ReadVariableOp").Device(DEVICE).HostMemory("resource"), \ + ReadVariableOp); \ + REGISTER_KERNEL_BUILDER( \ + Name("_ReadVariablesOp").Device(DEVICE).HostMemory("resources"), \ + ReadVariablesOp); \ + REGISTER_KERNEL_BUILDER( \ + Name("DestroyResourceOp").Device(DEVICE).HostMemory("resource"), \ + DestroyResourceOp); \ + REGISTER_KERNEL_BUILDER(Name("Shape") \ + .Device(DEVICE) \ + .HostMemory("output") \ + .TypeConstraint("out_type") \ + .TypeConstraint("T", TYPES), \ + ShapeOp); \ + REGISTER_KERNEL_BUILDER(Name("Shape") \ + .Device(DEVICE) \ + .HostMemory("output") \ + .TypeConstraint("out_type") \ + .TypeConstraint("T", TYPES), \ + ShapeOp); \ + REGISTER_KERNEL_BUILDER(Name("ShapeN") \ + .Device(DEVICE) \ + .HostMemory("output") \ + .TypeConstraint("out_type") \ + .TypeConstraint("T", TYPES), \ + ShapeNOp); \ + REGISTER_KERNEL_BUILDER(Name("ShapeN") \ + .Device(DEVICE) \ + .HostMemory("output") \ + .TypeConstraint("out_type") \ + .TypeConstraint("T", TYPES), \ + ShapeNOp); \ + REGISTER_KERNEL_BUILDER(Name("VariableShape") \ + .Device(DEVICE) \ + .TypeConstraint("out_type") \ + .HostMemory("output") \ + .HostMemory("input"), \ + VariableShapeOp); \ + REGISTER_KERNEL_BUILDER(Name("VariableShape") \ + .Device(DEVICE) \ + .TypeConstraint("out_type") \ + .HostMemory("output") \ + .HostMemory("input"), \ + VariableShapeOp); \ + REGISTER_KERNEL_BUILDER(Name("Size") \ + .Device(DEVICE) \ + .HostMemory("output") \ + .TypeConstraint("out_type") \ + .TypeConstraint("T", TYPES), \ + SizeOp); \ + REGISTER_KERNEL_BUILDER(Name("Size") \ + .Device(DEVICE) \ + .HostMemory("output") \ + .TypeConstraint("out_type") \ + .TypeConstraint("T", TYPES), \ + SizeOp); \ + REGISTER_KERNEL_BUILDER( \ + Name("Rank").Device(DEVICE).HostMemory("output").TypeConstraint("T", \ + TYPES), \ + RankOp); \ + REGISTER_KERNEL_BUILDER( \ + Name("AssignVariableOp").Device(DEVICE).HostMemory("resource"), \ + XlaAssignVariableOp); \ + \ + REGISTER_KERNEL_BUILDER( \ + Name("FIFOQueueV2").Device(DEVICE).HostMemory("handle"), FIFOQueueOp); \ + \ + REGISTER_KERNEL_BUILDER( \ + Name(kArgOp).Device(DEVICE).TypeConstraint("T", TYPES), ArgOp); \ + REGISTER_KERNEL_BUILDER(Name(kArgOp) \ + .Device(DEVICE) \ + .HostMemory("output") \ + .TypeConstraint("T"), \ + ArgOp); \ + REGISTER_KERNEL_BUILDER( \ + Name(kArgOp).Device(DEVICE).TypeConstraint("T"), ArgOp); \ + \ + REGISTER_KERNEL_BUILDER( \ + Name(kRetOp).Device(DEVICE).TypeConstraint("T", TYPES), RetvalOp); \ + REGISTER_KERNEL_BUILDER(Name(kRetOp) \ + .Device(DEVICE) \ + .TypeConstraint("T") \ + .HostMemory("input"), \ + RetvalOp); \ + REGISTER_KERNEL_BUILDER( \ + Name(kDeviceRetOp).Device(DEVICE).TypeConstraint("T"), RetvalOp); \ + \ + REGISTER_KERNEL_BUILDER( \ + Name("RemoteCall").Device(DEVICE).HostMemory("target"), RemoteCallOp); \ + \ + REGISTER_KERNEL_BUILDER( \ + Name("GeneratorDataset").Device(DEVICE).HostMemory("handle"), \ + data::GeneratorDatasetOp); \ + REGISTER_KERNEL_BUILDER(Name("PrefetchDataset") \ + .Device(DEVICE) \ + .HostMemory("buffer_size") \ + .HostMemory("input_dataset") \ + .HostMemory("handle"), \ + data::PrefetchDatasetOp); \ + REGISTER_KERNEL_BUILDER(Name("OptionsDataset") \ + .Device(DEVICE) \ + .HostMemory("input_dataset") \ + .HostMemory("handle"), \ + data::OptionsDatasetOp); \ + REGISTER_KERNEL_BUILDER(Name("FinalizeDataset") \ + .Device(DEVICE) \ + .HostMemory("input_dataset") \ + .HostMemory("handle"), \ + data::FinalizeDatasetOp); \ + \ + REGISTER_KERNEL_BUILDER(Name("IteratorV2").Device(DEVICE), \ + data::IteratorHandleOp); \ + REGISTER_KERNEL_BUILDER( \ + Name("MakeIterator").Device(DEVICE).HostMemory("dataset"), \ + data::MakeIteratorOp); \ + REGISTER_KERNEL_BUILDER(Name("AnonymousIterator").Device(DEVICE), \ + data::AnonymousIteratorHandleOp); \ + REGISTER_KERNEL_BUILDER(Name("AnonymousIteratorV2").Device(DEVICE), \ + data::AnonymousIteratorHandleOp); \ + REGISTER_KERNEL_BUILDER(Name("AnonymousIteratorV3").Device(DEVICE), \ + data::AnonymousIteratorHandleOp); \ + REGISTER_KERNEL_BUILDER(Name("DeleteIterator").Device(DEVICE), \ + data::DeleteIteratorOp); \ + REGISTER_KERNEL_BUILDER(Name("IteratorGetNext").Device(DEVICE), \ + data::IteratorGetNextOp); \ + REGISTER_KERNEL_BUILDER(Name("IteratorGetNextAsOptional").Device(DEVICE), \ + data::IteratorGetNextAsOptionalOp); \ + REGISTER_KERNEL_BUILDER(Name("IteratorGetNextSync").Device(DEVICE), \ + data::IteratorGetNextOp); \ + REGISTER_KERNEL_BUILDER(Name("IteratorToStringHandle") \ + .Device(DEVICE) \ + .HostMemory("string_handle"), \ + data::IteratorToStringHandleOp); \ + REGISTER_KERNEL_BUILDER(Name("IteratorFromStringHandleV2") \ + .Device(DEVICE) \ + .HostMemory("string_handle"), \ + data::IteratorFromStringHandleOp); \ + REGISTER_KERNEL_BUILDER(Name("OptionalNone").Device(DEVICE), \ + data::OptionalNoneOp); \ + REGISTER_KERNEL_BUILDER(Name("OptionalFromValue").Device(DEVICE), \ + data::OptionalFromValueOp); \ + REGISTER_KERNEL_BUILDER( \ + Name("OptionalHasValue").Device(DEVICE).HostMemory("has_value"), \ + data::OptionalHasValueOp); \ + REGISTER_KERNEL_BUILDER(Name("OptionalGetValue").Device(DEVICE), \ + data::OptionalGetValueOp); \ + REGISTER_KERNEL_BUILDER(Name(FunctionLibraryDefinition::kArgOp) \ + .Device(DEVICE) \ + .HostMemory("output") \ + .TypeConstraint("T"), \ + ArgOp); \ + REGISTER_KERNEL_BUILDER(Name(FunctionLibraryDefinition::kRetOp) \ + .Device(DEVICE) \ + .TypeConstraint("T") \ + .HostMemory("input"), \ + RetvalOp); + +// TODO(b/118881356): currently we do not register the QueueEnqueueMany, +// QueueDequeueMany, or QueueDequeueUpTo kernels because they attempt to read +// and write the tensors they access in order to concatenate them into a batch. +// We would need either to call out to an XLA computation to perform the +// concatenation, or we would need to refactor those kernels so the splitting +// or merging is done in a separate operator that can be compiled. + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_XLA_DEVICE_OPS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_gpu_device.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_gpu_device.cc new file mode 100644 index 0000000000000000000000000000000000000000..01c2dafe5f0263f4419ca5ed421233cac880fd71 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_gpu_device.cc @@ -0,0 +1,191 @@ +/* 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. +==============================================================================*/ + +// Registers the XLA_GPU device, which is an XlaDevice instantiation that runs +// operators using XLA via the XLA "CUDA" or "ROCM" (GPU) backend. + +#include +#include + +#include "absl/memory/memory.h" +#include "absl/strings/numbers.h" +#include "absl/strings/str_split.h" +#include "tensorflow/compiler/jit/defs.h" +#include "tensorflow/compiler/jit/flags.h" +#include "tensorflow/compiler/jit/kernels/xla_ops.h" +#include "tensorflow/compiler/jit/xla_device.h" +#include "tensorflow/compiler/jit/xla_device_ops.h" +#include "tensorflow/compiler/jit/xla_platform_info.h" +#include "tensorflow/compiler/tf2xla/layout_util.h" +#include "tensorflow/compiler/tf2xla/xla_op_registry.h" +#include "xla/stream_executor/gpu/gpu_init.h" +#include "xla/stream_executor/platform_manager.h" +#include "tensorflow/core/common_runtime/device_factory.h" +#include "tensorflow/core/framework/types.pb.h" +#include "tensorflow/core/lib/core/status.h" + +namespace tensorflow { + +class XlaGpuDeviceFactory : public DeviceFactory { + public: + absl::Status ListPhysicalDevices(std::vector* devices) override; + absl::Status CreateDevices( + const SessionOptions& options, const std::string& name_prefix, + std::vector>* devices) override; +}; + +absl::Status XlaGpuDeviceFactory::ListPhysicalDevices( + std::vector* devices) { + XlaDeviceFlags* flags = GetXlaDeviceFlags(); + if (!flags->tf_xla_enable_xla_devices && !XlaDevicesCreationRequired()) { + VLOG(1) << "Not creating XLA devices, tf_xla_enable_xla_devices not set " + "and XLA devices creation not required"; + return absl::OkStatus(); + } + + auto platform = se::PlatformManager::PlatformWithName(se::GpuPlatformName()); + if (!platform.ok()) { + // Treat failures as non-fatal; there might not be a GPU in the machine. + VLOG(1) << "Failed to create XLA_GPU device: " << platform.status(); + return absl::OkStatus(); + } + + int device_count = platform.value()->VisibleDeviceCount(); + if (device_count <= 0) { + return absl::OkStatus(); + } + + for (int i = 0; i < device_count; ++i) { + devices->push_back( + absl::StrCat("/physical_device:", DEVICE_XLA_GPU, ":", i)); + } + + return absl::OkStatus(); +} + +absl::Status XlaGpuDeviceFactory::CreateDevices( + const SessionOptions& session_options, const std::string& name_prefix, + std::vector>* devices) { + XlaDeviceFlags* flags = GetXlaDeviceFlags(); + if (!flags->tf_xla_enable_xla_devices && !XlaDevicesCreationRequired()) { + VLOG(1) << "Not creating XLA devices, tf_xla_enable_xla_devices not set"; + return absl::OkStatus(); + } + + XlaOpRegistry::DeviceRegistration registration; + registration.compilation_device_name = DEVICE_GPU_XLA_JIT; + registration.autoclustering_policy = + XlaOpRegistry::AutoclusteringPolicy::kAlways; + registration.cluster_resource_variable_ops_unsafely = true; + registration.cluster_stack_ops = false; + registration.cluster_tensor_array_ops = true; + registration.cluster_stateful_rng_ops = true; + registration.cluster_control_trigger = true; + registration.elide_assert_and_checknumerics = true; + registration.cluster_variant_ops = true; + registration.cluster_slow_ops = true; + registration.cluster_inaccurate_ops = true; + XlaOpRegistry::RegisterCompilationDevice(DEVICE_XLA_GPU, registration); + + static XlaDeviceOpRegistrations* registrations = + RegisterXlaDeviceKernels(DEVICE_XLA_GPU, DEVICE_GPU_XLA_JIT); + (void)registrations; + + auto platform = se::PlatformManager::PlatformWithName(se::GpuPlatformName()); + if (!platform.ok()) { + // Treat failures as non-fatal; there might not be a GPU in the machine. + VLOG(1) << "Failed to create XLA_GPU device: " << platform.status(); + return absl::OkStatus(); + } + + auto iter = session_options.config.device_count().find("GPU"); + if (iter != session_options.config.device_count().end() && + iter->second == 0) { + // Device count for GPU is 0. + return absl::OkStatus(); + } + + std::string allowed_gpus = + session_options.config.gpu_options().visible_device_list(); + std::optional> gpu_ids = + ParseVisibleDeviceList(allowed_gpus).value(); + if (!gpu_ids) { + gpu_ids.emplace(); + // Fill the gpu_ids set with all devices if config string is empty. + for (int i = 0; i < platform.value()->VisibleDeviceCount(); ++i) { + gpu_ids->insert(i); + } + } + for (int i : *gpu_ids) { + XlaDevice::Options options; + options.platform = platform.value(); + options.device_name_prefix = name_prefix; + options.device_name = DEVICE_XLA_GPU; + options.device_ordinal = i; + options.compilation_device_name = DEVICE_GPU_XLA_JIT; + options.use_multiple_streams = true; + options.allowed_devices = gpu_ids; + XlaShapeLayoutHelpers::ShapeDeterminationFns shape_representation_fns{ + UseNoPreferenceLayoutFn(), IdentityShapeRepresentationFn()}; + options.shape_determination_fns = {shape_representation_fns}; + auto device = std::make_unique(session_options, options); + + absl::Status status = device->UseAcceleratorDeviceInfo(); + if (!status.ok()) { + LOG(INFO) << "Ignoring visible " << DEVICE_GPU_XLA_JIT + << " device. Device number is " << i << ", reason: " << status; + continue; + } + + devices->push_back(std::move(device)); + } + return absl::OkStatus(); +} + +REGISTER_LOCAL_DEVICE_FACTORY(DEVICE_XLA_GPU, XlaGpuDeviceFactory); + +// Kernel registrations + +constexpr std::array kAllXlaGpuTypes = {{DT_UINT8, + DT_QUINT8, + DT_UINT16, + DT_INT8, + DT_QINT8, + DT_INT16, + DT_INT32, + DT_QINT32, + DT_INT64, + DT_HALF, + DT_FLOAT, + DT_DOUBLE, + DT_COMPLEX64, + DT_COMPLEX128, + DT_BOOL, + DT_BFLOAT16, + DT_FLOAT8_E5M2, + DT_FLOAT8_E4M3FN, + DT_FLOAT8_E4M3FNUZ, + DT_FLOAT8_E4M3B11FNUZ, + DT_FLOAT8_E5M2FNUZ, + DT_INT4, + DT_UINT4}}; + +REGISTER_XLA_LAUNCH_KERNEL(DEVICE_XLA_GPU, XlaLocalLaunchOp, kAllXlaGpuTypes); +REGISTER_XLA_COMPILE_KERNEL(DEVICE_XLA_GPU, XlaCompileOp, kAllXlaGpuTypes); +REGISTER_XLA_RUN_KERNEL(DEVICE_XLA_GPU, XlaRunOp, kAllXlaGpuTypes); + +REGISTER_XLA_DEVICE_KERNELS(DEVICE_XLA_GPU, kAllXlaGpuTypes); + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_host_recv_device_context.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_host_recv_device_context.cc new file mode 100644 index 0000000000000000000000000000000000000000..568f4a18745987e7bdf66ae58b85886ec8ec3577 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_host_recv_device_context.cc @@ -0,0 +1,57 @@ +/* Copyright 2023 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/compiler/jit/xla_host_recv_device_context.h" + +#include "tensorflow/compiler/tf2xla/shape_util.h" +#include "tensorflow/compiler/tf2xla/type_util.h" + +namespace tensorflow { + +void XlaHostRecvDeviceContext::CopyDeviceTensorToCPU( + const Tensor* device_tensor, absl::string_view tensor_name, Device* device, + Tensor* cpu_tensor, StatusCallback done) { + DataType dtype = EncodePrimitiveTypeAsDataType(shape_.element_type()).value(); + TensorShape tensor_shape; + absl::Status status = XLAShapeToTensorShape(shape_, &tensor_shape); + if (!status.ok()) { + done(status); + return; + } + + *cpu_tensor = Tensor(dtype, tensor_shape); + + status = stream_->Memcpy(cpu_tensor->data(), device_memory_base_, + device_memory_base_.size()); + if (!status.ok()) { + done(status); + return; + } + status = stream_->RecordEvent(done_event_.get().get()); + if (!status.ok()) { + done(status); + return; + } + if (auto st = stream_->BlockHostUntilDone(); !st.ok()) { + done_event_.SetError(absl::InternalError(absl::StrFormat( + "failed to synchronize send operation with a stream: %s", + st.ToString()))); + return; + } + + done_event_.SetStateConcrete(); + done(absl::OkStatus()); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_host_recv_device_context.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_host_recv_device_context.h new file mode 100644 index 0000000000000000000000000000000000000000..e7a51fb39607dcf539fed6571999275b979f510b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_host_recv_device_context.h @@ -0,0 +1,94 @@ +/* Copyright 2023 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_JIT_XLA_HOST_RECV_DEVICE_CONTEXT_H_ +#define TENSORFLOW_COMPILER_JIT_XLA_HOST_RECV_DEVICE_CONTEXT_H_ + +#include "xla/shape.h" +#include "xla/stream_executor/device_memory.h" +#include "xla/stream_executor/stream.h" +#include "tensorflow/core/framework/device_base.h" +#include "tfrt/concurrency/async_value_ref.h" // from @tf_runtime + +namespace tensorflow { + +// XlaHostRecvDeviceContext is a DeviceContext that is intended to be +// used to transfer from device->host using Rendezvous. It transfers the +// content of `device_memory_base` with `shape` using `stream`. Only +// `CopyDeviceTensorToCPU` method is implemented. The `done_event` is marked as +// Concrete once transfer is completed. +// +// Example usage: +// +// Device device; +// stream_executor::Stream stream(executor); +// Tensor device_tensor(device_allocator, DT_FLOAT, TensorShape({2, 2})); +// se::DeviceMemoryBase gpu_dst{device_tensor.data(), 4 * sizeof(float)}; +// xla::Shape shape(xla::F32, {2, 2}, {}, {}) +// tsl::AsyncValueRef> done_event = +// tsl::MakeConstructedAsyncValueRef>(stream.parent()); +// done_event->Init(); +// Tensor dest_cpu_tensor; +// +// XlaHostRecvDeviceContext device_context(&stream, gpu_dst, +// shape, done_event); +// device_context.CopyDeviceTensorToCPUSync( +// &device_tensor, "", &device, &dest_cpu_tensor); + +class XlaHostRecvDeviceContext : public DeviceContext { + public: + XlaHostRecvDeviceContext( + se::Stream* stream, + const stream_executor::DeviceAddressBase& device_memory_base, + const xla::Shape& shape, + tsl::AsyncValueRef>& done_event) + : stream_(stream), + device_memory_base_(device_memory_base), + shape_(shape), + done_event_(done_event) {} + + void CopyCPUTensorToDevice(const Tensor* cpu_tensor, Device* device, + Tensor* device_tensor, StatusCallback done, + bool sync_dst_compute) const override { + done(absl::InternalError("host->device copy not implemented.")); + } + + // Copies `device_memory_base_` with `shape_` into `cpu_tensor`. + // `device_tensor` is unused. + void CopyDeviceTensorToCPU(const Tensor* device_tensor, + absl::string_view tensor_name, Device* device, + Tensor* cpu_tensor, StatusCallback done) override; + + void CopyTensorInSameDevice(const Tensor* input_tensor, Device* device, + Tensor* output_tensor, + StatusCallback done) const override { + done(absl::InternalError("device->device copy not implemented.")); + } + + private: + se::Stream* stream_; // Not owned. + // This is copied rather than a reference or pointer since its lifetime + // is not guaranteed to outlast the original object. Object slicing is + // not an issue here since only DeviceMemoryBase methods/members are used. + const stream_executor::DeviceAddressBase device_memory_base_; + const xla::Shape shape_; + tsl::AsyncValueRef> done_event_; + + XlaHostRecvDeviceContext(const XlaHostRecvDeviceContext&) = delete; + void operator=(const XlaHostRecvDeviceContext&) = delete; +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_XLA_HOST_RECV_DEVICE_CONTEXT_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_host_send_device_context.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_host_send_device_context.cc new file mode 100644 index 0000000000000000000000000000000000000000..755b0698f04a8fc62b992f51b0798edee6177ba6 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_host_send_device_context.cc @@ -0,0 +1,47 @@ +/* Copyright 2023 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/compiler/jit/xla_host_send_device_context.h" + +#include "tensorflow/compiler/tf2xla/shape_util.h" +#include "tensorflow/compiler/tf2xla/type_util.h" + +namespace tensorflow { + +void XlaHostSendDeviceContext::CopyCPUTensorToDevice( + const Tensor* cpu_tensor, Device* device, Tensor* device_tensor, + StatusCallback done, bool sync_dst_compute) const { + auto status = stream_->Memcpy(device_memory_base_, cpu_tensor->data(), + device_memory_base_->size()); + if (!status.ok()) { + done(status); + return; + } + status = stream_->RecordEvent(done_event_.get().get()); + if (!status.ok()) { + done(status); + return; + } + if (auto st = stream_->BlockHostUntilDone(); !st.ok()) { + done_event_.SetError(absl::InternalError(absl::StrFormat( + "failed to synchronize send operation with a stream: %s", + st.ToString()))); + return; + } + + done_event_.SetStateConcrete(); + done(absl::OkStatus()); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_host_send_device_context.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_host_send_device_context.h new file mode 100644 index 0000000000000000000000000000000000000000..2125b6baee584d9fdcbc1d221c43d6079b18d9b2 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_host_send_device_context.h @@ -0,0 +1,91 @@ +/* Copyright 2023 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_JIT_XLA_HOST_SEND_DEVICE_CONTEXT_H_ +#define TENSORFLOW_COMPILER_JIT_XLA_HOST_SEND_DEVICE_CONTEXT_H_ + +#include "xla/shape.h" +#include "xla/stream_executor/device_memory.h" +#include "xla/stream_executor/stream.h" +#include "tensorflow/core/framework/device_base.h" +#include "tfrt/concurrency/async_value_ref.h" // from @tf_runtime + +namespace tensorflow { + +// XlaHostSendDeviceContext is a DeviceContext that is intended to be +// used to transfer from host->device using Rendezvous. It transfers the +// content of `device_memory_base` with `shape` using `stream`. Only +// `CopyCPUTensorToDevice` method is implemented. The `done_event` is marked as +// Concrete once transfer is completed. +// +// Example usage: +// +// Device device; +// stream_executor::Stream stream(executor); +// Tensor cpu_tensor(host_allocator, DT_FLOAT, TensorShape({2, 2})); +// Tensor device_tensor(device_allocator, DT_FLOAT, TensorShape({2, 2})); +// se::DeviceMemoryBase gpu_dst{device_tensor.data(), 4 * sizeof(float)}; +// xla::Shape shape(xla::F32, {2, 2}, {}, {}) +// tsl::AsyncValueRef> done_event = +// tsl::MakeConstructedAsyncValueRef>(stream.parent()); +// done_event->Init(); +// +// XlaHostSendDeviceContext device_context(&stream, &gpu_dst, +// shape, done_event); +// device_context.CopyCPUTensorToDeviceSync( +// &cpu_tensor, &device, &device_tensor); + +class XlaHostSendDeviceContext : public DeviceContext { + public: + XlaHostSendDeviceContext( + se::Stream* stream, + stream_executor::DeviceAddressBase* device_memory_base, + const xla::Shape& shape, + tsl::AsyncValueRef>& done_event) + : stream_(stream), + device_memory_base_(device_memory_base), + shape_(shape), + done_event_(done_event) {} + + // Copies 'cpu_tensor' to `device_memory_base_` with `shape_`. + // `device_tensor` is unused. + void CopyCPUTensorToDevice(const Tensor* cpu_tensor, Device* device, + Tensor* device_tensor, StatusCallback done, + bool sync_dst_compute) const override; + + void CopyDeviceTensorToCPU(const Tensor* device_tensor, + absl::string_view tensor_name, Device* device, + Tensor* cpu_tensor, StatusCallback done) override { + done(absl::InternalError("host->device copy not implemented.")); + } + + void CopyTensorInSameDevice(const Tensor* input_tensor, Device* device, + Tensor* output_tensor, + StatusCallback done) const override { + done(absl::InternalError("device->device copy not implemented.")); + } + + private: + se::Stream* stream_; // Not owned. + stream_executor::DeviceAddressBase* device_memory_base_; // Not owned. + const xla::Shape shape_; + tsl::AsyncValueRef> done_event_; + + XlaHostSendDeviceContext(const XlaHostSendDeviceContext&) = delete; + void operator=(const XlaHostSendDeviceContext&) = delete; +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_XLA_HOST_SEND_DEVICE_CONTEXT_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_host_send_recv_device_context_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_host_send_recv_device_context_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..a42fb9083b5387b110f64415fc871676a0a7ac7e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_host_send_recv_device_context_test.cc @@ -0,0 +1,178 @@ +/* Copyright 2023 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/compiler/jit/xla_host_recv_device_context.h" +#include "tensorflow/compiler/jit/xla_host_send_device_context.h" +#include "tensorflow/compiler/tf2xla/shape_util.h" +#include "tensorflow/compiler/tf2xla/xla_op_registry.h" +#include "xla/stream_executor/device_memory.h" +#include "xla/stream_executor/platform_manager.h" +#include "xla/stream_executor/stream.h" +#include "xla/stream_executor/stream_executor.h" +#include "xla/tsl/lib/core/status_test_util.h" +#include "tensorflow/core/framework/tensor_testutil.h" + +namespace tensorflow { +namespace { + +class XlaHostSendRecvDeviceContextTest : public ::testing::Test { + public: + absl::Status SetDevice(const std::string& device_type) { + auto device_factory = DeviceFactory::GetFactory(device_type); + if (device_factory == nullptr) { + return absl::NotFoundError( + "Failed to get DeviceFactory for device_type: " + device_type); + } + SessionOptions options; + std::vector> devices; + absl::Status s = device_factory->CreateDevices( + options, "/job:worker/replica:0/task:0", &devices); + device_ = std::move(devices[0]); + + AllocatorAttributes host_alloc_attr; + host_alloc_attr.set_on_host(true); + host_allocator_ = device_->GetAllocator(host_alloc_attr); + + AllocatorAttributes device_alloc_attr; + device_alloc_attr.set_on_host(false); + device_allocator_ = device_->GetAllocator(device_alloc_attr); + return absl::OkStatus(); + } + + protected: + std::unique_ptr device_; + Allocator* host_allocator_; + Allocator* device_allocator_; +}; + +TEST_F(XlaHostSendRecvDeviceContextTest, CopyDeviceTensorToCPU) { + TF_ASSERT_OK(SetDevice("GPU")); + Tensor origin_cpu_tensor(host_allocator_, DT_FLOAT, TensorShape({2, 2})); + test::FillValues(&origin_cpu_tensor, {1.2, 2.3, 3.4, 4.5}); + Tensor device_tensor(device_allocator_, DT_FLOAT, TensorShape({2, 2})); + Tensor dest_cpu_tensor(host_allocator_, DT_FLOAT, TensorShape({2, 2})); + + stream_executor::Platform* platform = + stream_executor::PlatformManager::PlatformWithName("CUDA").value(); + stream_executor::StreamExecutor* executor = + platform->ExecutorForDevice(0).value(); + TF_ASSERT_OK_AND_ASSIGN(auto stream, executor->CreateStream()); + + stream_executor::DeviceAddressBase gpu_dst{device_tensor.data(), + 4 * sizeof(float)}; + xla::Shape shape; + TF_ASSERT_OK(TensorShapeToXLAShape(DT_FLOAT, TensorShape({2, 2}), &shape)); + + // Copy the cpu_tensor to the GPU first before trying to copy it back. + TF_ASSERT_OK( + stream->Memcpy(&gpu_dst, origin_cpu_tensor.data(), gpu_dst.size())); + TF_ASSERT_OK(stream->BlockHostUntilDone()); + + TF_ASSERT_OK_AND_ASSIGN(auto se_event, executor->CreateEvent()); + tsl::AsyncValueRef> done_event = + tsl::MakeConstructedAsyncValueRef>( + std::move(se_event)); + XlaHostRecvDeviceContext* device_context = + new XlaHostRecvDeviceContext(stream.get(), gpu_dst, shape, done_event); + TF_ASSERT_OK(device_context->CopyDeviceTensorToCPUSync( + &device_tensor, "", device_.get(), &dest_cpu_tensor)); + + tensorflow::test::ExpectClose(origin_cpu_tensor, dest_cpu_tensor); + device_context->Unref(); +} + +TEST_F(XlaHostSendRecvDeviceContextTest, CopyCPUTensorToDevice) { + TF_ASSERT_OK(SetDevice("GPU")); + Tensor origin_cpu_tensor(host_allocator_, DT_FLOAT, TensorShape({2, 2})); + test::FillValues(&origin_cpu_tensor, {1.2, 2.3, 3.4, 4.5}); + Tensor device_tensor(device_allocator_, DT_FLOAT, TensorShape({2, 2})); + Tensor dest_cpu_tensor(host_allocator_, DT_FLOAT, TensorShape({2, 2})); + + stream_executor::Platform* platform = + stream_executor::PlatformManager::PlatformWithName("CUDA").value(); + stream_executor::StreamExecutor* executor = + platform->ExecutorForDevice(0).value(); + TF_ASSERT_OK_AND_ASSIGN(auto stream, executor->CreateStream()); + + stream_executor::DeviceAddressBase gpu_dst{device_tensor.data(), + 4 * sizeof(float)}; + xla::Shape shape; + TF_ASSERT_OK(TensorShapeToXLAShape(DT_FLOAT, TensorShape({2, 2}), &shape)); + + TF_ASSERT_OK_AND_ASSIGN(auto se_event, executor->CreateEvent()); + tsl::AsyncValueRef> done_event = + tsl::MakeConstructedAsyncValueRef>( + std::move(se_event)); + XlaHostSendDeviceContext* device_context = + new XlaHostSendDeviceContext(stream.get(), &gpu_dst, shape, done_event); + TF_ASSERT_OK(device_context->CopyCPUTensorToDeviceSync( + &origin_cpu_tensor, device_.get(), &device_tensor)); + + // Copy the GPU tensor back to CPU to check that copy worked. + TF_ASSERT_OK(stream->Memcpy(dest_cpu_tensor.data(), gpu_dst, gpu_dst.size())); + TF_ASSERT_OK(stream->BlockHostUntilDone()); + + tensorflow::test::ExpectClose(origin_cpu_tensor, dest_cpu_tensor); + device_context->Unref(); +} + +TEST_F(XlaHostSendRecvDeviceContextTest, RoundTrip) { + TF_ASSERT_OK(SetDevice("GPU")); + Tensor origin_cpu_tensor(host_allocator_, DT_FLOAT, TensorShape({2, 2})); + test::FillValues(&origin_cpu_tensor, {1.2, 2.3, 3.4, 4.5}); + Tensor device_tensor(device_allocator_, DT_FLOAT, TensorShape({2, 2})); + Tensor dest_cpu_tensor(host_allocator_, DT_FLOAT, TensorShape({2, 2})); + + stream_executor::Platform* platform = + stream_executor::PlatformManager::PlatformWithName("CUDA").value(); + stream_executor::StreamExecutor* executor = + platform->ExecutorForDevice(0).value(); + TF_ASSERT_OK_AND_ASSIGN(auto stream, executor->CreateStream()); + + stream_executor::DeviceAddressBase gpu_dst{device_tensor.data(), + 4 * sizeof(float)}; + xla::Shape shape; + TF_ASSERT_OK(TensorShapeToXLAShape(DT_FLOAT, TensorShape({2, 2}), &shape)); + + TF_ASSERT_OK_AND_ASSIGN(auto se_event, executor->CreateEvent()); + tsl::AsyncValueRef> send_done_event = + tsl::MakeConstructedAsyncValueRef>( + std::move(se_event)); + XlaHostSendDeviceContext* send_device_context = new XlaHostSendDeviceContext( + stream.get(), &gpu_dst, shape, send_done_event); + TF_ASSERT_OK(send_device_context->CopyCPUTensorToDeviceSync( + &origin_cpu_tensor, device_.get(), &device_tensor)); + + TF_ASSERT_OK_AND_ASSIGN(auto recv_se_event, executor->CreateEvent()); + tsl::AsyncValueRef> recv_done_event = + tsl::MakeConstructedAsyncValueRef>( + std::move(recv_se_event)); + XlaHostRecvDeviceContext* recv_device_context = new XlaHostRecvDeviceContext( + stream.get(), gpu_dst, shape, recv_done_event); + TF_ASSERT_OK(recv_device_context->CopyDeviceTensorToCPUSync( + &device_tensor, "", device_.get(), &dest_cpu_tensor)); + + tensorflow::test::ExpectClose(origin_cpu_tensor, dest_cpu_tensor); + send_device_context->Unref(); + recv_device_context->Unref(); +} + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_kernel_creator.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_kernel_creator.cc new file mode 100644 index 0000000000000000000000000000000000000000..111322e5ce5ea5967f81f471e0487e92efb9c887 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_kernel_creator.cc @@ -0,0 +1,112 @@ +/* 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/compiler/jit/xla_kernel_creator.h" + +#include +#include + +#include "absl/memory/memory.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/str_format.h" +#include "tensorflow/compiler/jit/compilability_check_util.h" +#include "tensorflow/compiler/jit/defs.h" +#include "tensorflow/compiler/jit/flags.h" +#include "tensorflow/compiler/jit/kernels/xla_ops.h" +#include "tensorflow/compiler/tf2xla/const_analysis.h" +#include "tensorflow/compiler/tf2xla/mlir_bridge_pass.h" +#include "tensorflow/compiler/tf2xla/xla_op_registry.h" +#include "tensorflow/core/common_runtime/function.h" +#include "tensorflow/core/common_runtime/function_body.h" +#include "tensorflow/core/common_runtime/function_utils.h" +#include "tensorflow/core/framework/allocator.h" +#include "tensorflow/core/framework/device.h" +#include "tensorflow/core/framework/node_def_builder.h" +#include "tensorflow/core/framework/node_def_util.h" +#include "tensorflow/core/framework/node_properties.h" +#include "tensorflow/core/framework/op_kernel.h" +#include "tensorflow/core/framework/types.h" +#include "tensorflow/core/lib/core/errors.h" +#include "tensorflow/core/lib/core/status.h" +#include "tsl/platform/errors.h" + +namespace tensorflow { + +bool XlaKernelCreator::CanCreateKernel( + const FunctionLibraryRuntime& flr, + const std::shared_ptr& props) const { + return CanCreateXlaKernel(props->node_def) && + !XlaOpRegistry::IsCompilationDevice(flr.device()->device_type()); +} + +static absl::Status CreateXlaKernel(FunctionLibraryRuntime* flr, + const NodeDef& node_def, + std::unique_ptr* kernel) { + if (!CanCreateXlaKernel(node_def)) { + return absl::InternalError( + absl::StrCat("Invalid node: ", node_def.ShortDebugString())); + } + + VLOG(3) << "Attempting to create XlaLaunchOp for " << node_def.DebugString(); + + // Make sure that kernels have been registered on the JIT device. + XlaOpRegistry::RegisterCompilationKernels(); + + // Get function body, constant args, and resource args. + NameAttrList function; + TF_RETURN_IF_ERROR(NameAndAttrsFromFunctionCall(node_def, &function)); + const FunctionBody* fbody = nullptr; + std::vector constant_arg_indices; + std::vector resource_arg_indices; + TF_RETURN_IF_ERROR(GetBodyAndConstantsAndResources( + flr, function, &fbody, &constant_arg_indices, &resource_arg_indices)); + + MemoryTypeVector input_memory_types = + GetInputMemoryTypes(fbody, constant_arg_indices, resource_arg_indices); + MemoryTypeVector output_memory_types = GetOutputMemoryTypes(fbody); + + // Create the kernel. + Device* dev = flr->device(); + absl::Status s; + auto props = std::make_shared( + &fbody->record->fdef().signature(), node_def, fbody->arg_types, + fbody->ret_types); + OpKernelConstruction construction(DeviceType(dev->device_type()), dev, + dev->GetAllocator(AllocatorAttributes()), + flr, dev->resource_manager(), props, + input_memory_types, output_memory_types, + flr->graph_def_version(), &s); + + *kernel = std::make_unique( + &construction, constant_arg_indices, resource_arg_indices, function, + /*has_ref_vars=*/false); + return s; +} + +absl::Status XlaKernelCreator::CreateKernel( + FunctionLibraryRuntime* flr, + const std::shared_ptr& props, + std::unique_ptr* kernel) const { + return CreateXlaKernel(flr, props->node_def, kernel); +} + +bool RegisterLaunchOpCreator() { + XlaKernelCreator* xla_kernel_creator = new XlaKernelCreator(); + RegisterDefaultCustomKernelCreator(xla_kernel_creator); + return true; +} + +static bool register_me = RegisterLaunchOpCreator(); + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_kernel_creator.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_kernel_creator.h new file mode 100644 index 0000000000000000000000000000000000000000..a66c4c8f72012115002d0318480ece253db155de --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_kernel_creator.h @@ -0,0 +1,49 @@ +/* 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_JIT_XLA_KERNEL_CREATOR_H_ +#define TENSORFLOW_COMPILER_JIT_XLA_KERNEL_CREATOR_H_ + +#include + +#include "tensorflow/core/framework/function.h" +#include "tensorflow/core/framework/node_def.pb.h" +#include "tensorflow/core/framework/node_properties.h" +#include "tensorflow/core/lib/core/status.h" + +namespace tensorflow { + +class FunctionLibraryRuntime; +class OpKernel; + +class XlaKernelCreator : public CustomKernelCreator { + public: + // Given a NodeDef 'node_def' and the function library runtime 'flr', returns + // true if 'node_def' is a call to a compilable function defined in 'flr', + // with the kXlaCompileAttr set. + bool CanCreateKernel( + const FunctionLibraryRuntime& flr, + const std::shared_ptr& props) const override; + + // Given a supported NodeDef, returns a XlaLaunchOp that computes the node. + absl::Status CreateKernel(FunctionLibraryRuntime* flr, + const std::shared_ptr& props, + std::unique_ptr* kernel) const override; +}; + +bool RegisterLaunchOpCreator(); + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_XLA_KERNEL_CREATOR_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_kernel_creator_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_kernel_creator_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..73808af65c0ff8b6e95a1c4f7134c568024471a2 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_kernel_creator_test.cc @@ -0,0 +1,164 @@ +/* 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 "tensorflow/compiler/jit/xla_kernel_creator.h" + +#include +#include +#include + +#include "absl/log/check.h" +#include "absl/status/status.h" +#include "tensorflow/core/common_runtime/device_mgr.h" +#include "tensorflow/core/common_runtime/process_function_library_runtime.h" +#include "tensorflow/core/framework/device.h" +#include "tensorflow/core/framework/device_factory.h" +#include "tensorflow/core/framework/function.h" +#include "tensorflow/core/framework/node_properties.h" +#include "tensorflow/core/framework/op.h" +#include "tensorflow/core/framework/types.h" +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/platform/test.h" +#include "tensorflow/core/platform/types.h" +#include "tensorflow/core/public/session_options.h" +#include "tensorflow/core/public/version.h" + +namespace tensorflow { + +std::shared_ptr ToNodeProperties(const std::string& text) { + NodeDef node_def; + DataTypeVector dummy; + EXPECT_TRUE(protobuf::TextFormat::MergeFromString(text, &node_def)); + return std::make_shared(nullptr, std::move(node_def), dummy, + dummy); +} + +// Create a FunctionDef that takes one resource and one regular param +FunctionDef XTimesY() { + return FunctionDefHelper::Define( + // Name + "XTimesY", + // Args + {"x: float", "y: resource"}, + // Return values + {"z: float"}, + // Attr def + {}, + // Nodes + { + {{"y0"}, "ReadVariableOp", {"y"}, {{"dtype", DT_FLOAT}}}, + {{"z"}, "Mul", {"x", "y0"}, {{"T", DT_FLOAT}}}, + }); +} + +class XlaKernelCreatorTest : public ::testing::Test { + protected: + void Init(const std::vector& flib) { + SessionOptions options; + auto* device_count = options.config.mutable_device_count(); + device_count->insert({"CPU", 1}); + std::vector> devices; + CHECK_OK(DeviceFactory::AddDevices( + options, "/job:localhost/replica:0/task:0", &devices)); + + FunctionDefLibrary proto; + for (const auto& fdef : flib) { + *(proto.add_function()) = fdef; + } + lib_def_ = std::make_unique( + OpRegistry::Global(), proto); + OptimizerOptions opts; + device_mgr_ = std::make_unique(std::move(devices)); + pflr_ = std::make_unique( + device_mgr_.get(), Env::Default(), /*config=*/nullptr, + TF_GRAPH_DEF_VERSION, lib_def_.get(), opts, + /*default_thread_pool=*/nullptr, /*cluster_flr=*/nullptr); + flr_ = pflr_->GetFLR("/job:localhost/replica:0/task:0/cpu:0"); + } + + FunctionLibraryRuntime* flr_; + std::unique_ptr device_mgr_; + std::unique_ptr lib_def_; + std::unique_ptr pflr_; + + std::unique_ptr kernel_; +}; + +AttrValue BoolAttr(bool b) { + AttrValue v; + v.set_b(b); + return v; +} + +TEST_F(XlaKernelCreatorTest, OneFloatOneResourceArgument) { + FunctionDef fdef = XTimesY(); + (*fdef.mutable_attr())["_XlaMustCompile"] = BoolAttr(true); + Init({fdef}); + XlaKernelCreator xla_kernel_creator; + auto callsite = + ToNodeProperties(R"pb( + name: 'XTimesY' op: 'XTimesY' input: 'a' input: 'b' + )pb"); + (*(callsite->node_def.mutable_attr()))["_XlaMustCompile"] = BoolAttr(true); + + // Note: need to set attribute on the created node. + absl::Status status = + xla_kernel_creator.CreateKernel(flr_, callsite, &kernel_); + ASSERT_TRUE(status.ok()) << status.ToString(); + + EXPECT_EQ("XTimesY", kernel_->name()); + EXPECT_EQ("XTimesY", kernel_->type_string()); + + EXPECT_EQ(2, kernel_->num_inputs()); + EXPECT_EQ(DT_FLOAT, kernel_->input_type(0)); + EXPECT_EQ(DT_RESOURCE, kernel_->input_type(1)); + EXPECT_EQ(DEVICE_MEMORY, kernel_->input_memory_types()[0]); + EXPECT_EQ(HOST_MEMORY, kernel_->input_memory_types()[1]); + + EXPECT_EQ(1, kernel_->num_outputs()); + EXPECT_EQ(DT_FLOAT, kernel_->output_type(0)); + EXPECT_EQ(DEVICE_MEMORY, kernel_->output_memory_types()[0]); +} + +TEST_F(XlaKernelCreatorTest, FailsIfXlaCompileAttrNotSet) { + FunctionDef fdef = XTimesY(); + Init({fdef}); + XlaKernelCreator xla_kernel_creator; + + absl::Status status = xla_kernel_creator.CreateKernel( + flr_, + ToNodeProperties(R"pb( + name: 'XTimesY' op: 'XTimesY' input: 'a' input: 'b' + )pb"), + &kernel_); + EXPECT_TRUE(absl::IsInternal(status)) << status; +} + +TEST_F(XlaKernelCreatorTest, FailsIfXlaCompileAttrIsSetToFalse) { + FunctionDef fdef = XTimesY(); + (*fdef.mutable_attr())["_XlaMustCompile"] = BoolAttr(false); + Init({fdef}); + XlaKernelCreator xla_kernel_creator; + + absl::Status status = xla_kernel_creator.CreateKernel( + flr_, + ToNodeProperties(R"pb( + name: 'XTimesY' op: 'XTimesY' input: 'a' input: 'b' + )pb"), + &kernel_); + EXPECT_TRUE(absl::IsInternal(status)) << status; +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_launch_util.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_launch_util.cc new file mode 100644 index 0000000000000000000000000000000000000000..471a25d19d65636e642c8099ed663ae6a86afa8f --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_launch_util.cc @@ -0,0 +1,969 @@ +/* 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 "tensorflow/compiler/jit/xla_launch_util.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "absl/algorithm/container.h" +#include "absl/cleanup/cleanup.h" +#include "absl/container/flat_hash_map.h" +#include "absl/container/flat_hash_set.h" +#include "absl/log/check.h" +#include "absl/log/log.h" +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/str_join.h" +#include "absl/strings/string_view.h" +#include "absl/types/span.h" +#include "tensorflow/compiler/jit/pjrt_tensor_buffer.h" +#include "tensorflow/compiler/jit/pjrt_tensor_buffer_util.h" +#include "tensorflow/compiler/jit/variable_info.h" +#include "tensorflow/compiler/jit/variable_info_util.h" +#include "tensorflow/compiler/jit/xla_tensor.h" +#include "tensorflow/compiler/tf2xla/const_analysis.h" +#include "tensorflow/compiler/tf2xla/shape_util.h" +#include "tensorflow/compiler/tf2xla/xla_compiler.h" +#include "tensorflow/compiler/tf2xla/xla_helpers.h" +#include "tensorflow/compiler/tf2xla/xla_resource.h" +#include "xla/client/local_client.h" +#include "xla/future.h" +#include "xla/hlo/ir/hlo_input_output_alias_config.h" +#include "xla/pjrt/pjrt_client.h" +#include "xla/pjrt/pjrt_common.h" +#include "xla/pjrt/pjrt_executable.h" +#include "xla/service/executable.h" +#include "xla/service/maybe_owning_device_memory.h" +#include "xla/service/shaped_buffer.h" +#include "xla/service/transfer_manager.h" +#include "xla/shape.h" +#include "xla/shape_util.h" +#include "xla/status_macros.h" +#include "xla/stream_executor/device_memory.h" +#include "xla/stream_executor/device_memory_allocator.h" +#include "xla/stream_executor/event.h" +#include "xla/stream_executor/host/host_platform_id.h" +#include "xla/stream_executor/platform.h" +#include "xla/stream_executor/platform_manager.h" +#include "xla/tsl/framework/device_id_utils.h" +#include "xla/tsl/framework/serving_device_selector_policies.h" +#include "xla/tsl/platform/errors.h" +#include "xla/tsl/platform/statusor.h" +#include "xla/util.h" +#include "tensorflow/core/common_runtime/dma_helper.h" +#include "tensorflow/core/common_runtime/gpu/gpu_serving_device_selector.h" +#include "tensorflow/core/common_runtime/gpu_device_context.h" +#include "tensorflow/core/framework/allocator.h" +#include "tensorflow/core/framework/device_base.h" +#include "tensorflow/core/framework/op_kernel.h" +#include "tensorflow/core/framework/resource_handle.h" +#include "tensorflow/core/framework/resource_mgr.h" +#include "tensorflow/core/framework/tensor.h" +#include "tensorflow/core/framework/tensor_shape.h" +#include "tensorflow/core/framework/types.h" +#include "tensorflow/core/lib/core/errors.h" +#include "tensorflow/core/lib/core/refcount.h" +#include "tensorflow/core/platform/errors.h" +#include "tensorflow/core/platform/status.h" +#include "tensorflow/core/tfrt/common/async_value_tensor.h" +#include "tsl/platform/casts.h" + +namespace tensorflow { +namespace { +using xla::ScopedShapedBuffer; +using xla::ShapedBuffer; + +// Fetch the platform Id from device. +se::Platform::Id XlaPlatformInfoFromDevice(DeviceBase* device_base) { + auto device = static_cast(device_base); + se::Platform::Id platform_id = nullptr; + if (device->device_type() == DEVICE_CPU) { + platform_id = se::host::kHostPlatformId; + } + + return platform_id; +} + +absl::flat_hash_map CreateVariableLookup( + const std::vector& variables) { + absl::flat_hash_map variable_lookup; + for (int i = 0; i < variables.size(); i++) { + variable_lookup[variables[i].index()] = i; + } + return variable_lookup; +} + +} // anonymous namespace + +std::vector InputsFromContext(OpKernelContext* ctx) { + std::vector inputs; + inputs.reserve(ctx->num_inputs()); + for (int input_idx = 0; input_idx < ctx->num_inputs(); input_idx++) { + inputs.push_back(&ctx->input(input_idx)); + } + return inputs; +} + +absl::StatusOr> GetConstantInputIndicesFromContext( + OpKernelContext* ctx) { + std::vector constant_input_indices; + TF_RETURN_IF_ERROR(GetCompileTimeConstInputs( + &ctx->op_kernel(), &constant_input_indices, ctx->function_library())); + if (!absl::c_all_of(constant_input_indices, [&](int idx) { + return ctx->input_memory_type(idx) == HOST_MEMORY; + })) { + return absl::InternalError( + "Unexpected device placement for a constant input"); + } + return constant_input_indices; +} + +XlaComputationLaunchContext::XlaComputationLaunchContext( + xla::LocalClient* client, + stream_executor::DeviceAddressAllocator* xla_allocator, int device_ordinal, + bool allocate_xla_tensors, bool use_multiple_streams) + : client_(client), + xla_allocator_(xla_allocator), + allocate_xla_tensors_(allocate_xla_tensors), + use_multiple_streams_(use_multiple_streams), + device_ordinal_(device_ordinal) { + if (use_multiple_streams_) { + CHECK(allocate_xla_tensors_) << "To use multiple streams correctly we must " + "be allocating XLA tensors!"; + } +} + +// Fills in `execution_input` with `buffer` for `index`. +static void PopulateExecutionInputBuffer( + xla::ExecutionInput& execution_input, const xla::ShapeIndex& index, + stream_executor::DeviceAddressBase buffer, bool donate_buffer, + int device_ordinal, stream_executor::DeviceAddressAllocator* allocator) { + xla::MaybeOwningDeviceAddress* in_buffer = + execution_input.MutableBuffer(index); + if (donate_buffer) { + // Here we pass ownership of the buffer to execution_input without releasing + // ownership from the caller of PopulateExecutionInputBuffer. If execution + // succeeds, we'll take back that duplicate ownership in + // GetOrCreateTensorForOutput. If execution fails, the ExecutionInput will + // release that duplicate ownership automatically. + *in_buffer = stream_executor::ScopedDeviceAddress( + buffer, device_ordinal, allocator); + } else { + *in_buffer = buffer; + } +} + +absl::StatusOr> +XlaComputationLaunchContext::PopulateInputs( + OpKernelContext* ctx, + const XlaCompiler::CompilationResult* compilation_result, + const absl::flat_hash_map& resource_vars, + int missing_ctx_input_prefix, + const xla::HloInputOutputAliasConfig& input_output_alias) { + std::vector arguments; + arguments.reserve(compilation_result->xla_input_shapes.size()); + + xla::ShapeIndex root_index = {}; + + for (int i = 0; i < compilation_result->xla_input_shapes.size(); ++i) { + int arg_num = compilation_result->input_mapping[i]; + CHECK_GE(arg_num, missing_ctx_input_prefix); + const xla::Shape& device_shape = compilation_result->xla_input_shapes[i]; + + auto resource_var_it = resource_vars.find(arg_num); + bool is_resource_variable = resource_var_it != resource_vars.end(); + bool is_updated_resource_variable = + is_resource_variable && + absl::c_any_of(compilation_result->resource_updates, + [&](const XlaCompiler::ResourceUpdate& update) { + // XlaCompiler records `arg_num` (instead of kernel + // parameters) in `resource_updates`. + return update.input_index == arg_num && + update.modified; + }); + + const Tensor* t = is_resource_variable + ? resource_var_it->second + : &(ctx->input(arg_num - missing_ctx_input_prefix)); + CHECK(t); + bool donate_buffer = t->RefCountIsOne() && is_updated_resource_variable && + input_output_alias.ParameterHasAlias(i, root_index); + VLOG(3) << "Processing input: " << i + << "; is_resource_variable=" << is_resource_variable + << "; is_updated_resource_variable=" << is_updated_resource_variable + << "; donate_buffer=" << donate_buffer; + + if (use_multiple_streams_) { + CHECK(ctx->op_device_context() && ctx->op_device_context()->stream()) + << "Must have a stream available when using XLA tensors!"; + XlaTensor* xla_tensor = XlaTensor::FromTensor(t); + CHECK(xla_tensor); + xla_tensor->WaitForDefinitionEventOnStream( + ctx->op_device_context()->stream()); + } + + arguments.emplace_back(&device_shape); + xla::ExecutionInput& execution_input = arguments.back(); + stream_executor::DeviceAddressBase dmem = + XlaTensor::DeviceMemoryFromTensor(*t); + PopulateExecutionInputBuffer(execution_input, root_index, dmem, + donate_buffer, device_ordinal_, + xla_allocator_); + } + return std::move(arguments); +} + +// Construct the tensor for the given type and buffer. +static Tensor MakeTensor(DataType dtype, const TensorShape& shape, + stream_executor::DeviceAddressBase buffer, + Allocator* allocator) { + size_t expected_size = shape.num_elements() * DataTypeSize(dtype); + auto* tensor_buffer = new XlaTensorBuffer(buffer.opaque(), expected_size, + buffer.size(), allocator); + Tensor t(dtype, shape, tensor_buffer); + tensor_buffer->Unref(); + return t; +} + +// Get aliased tensor from output, or make a new one for the corresponding +// output operation. Transfers ownership of the buffer from output to the +// returned tensor. +static absl::StatusOr GetOrCreateTensorForOutput( + xla::ScopedShapedBuffer& output, int output_num, OpKernelContext* ctx, + int missing_ctx_input_prefix, + const xla::HloInputOutputAliasConfig& input_output_alias, + absl::Span input_mapping, + const absl::flat_hash_map& resource_vars_snapshots, + DataType output_dtype, const TensorShape& output_shape, + Allocator* output_allocator, bool allocate_xla_tensors, se::Stream* stream, + bool use_multiple_streams, std::shared_ptr definition_event) { + xla::ShapeIndex output_index = input_output_alias.shape().IsTuple() + ? xla::ShapeIndex({output_num}) + : xla::ShapeIndex({}); + CHECK(input_output_alias.shape().IsTuple() || output_num == 0); + if (std::optional alias = + input_output_alias.GetAliasedParameter(output_index)) { + VLOG(3) << "Found alias: " << alias->ToString(); + int tf_param = + input_mapping[alias->parameter_number] - missing_ctx_input_prefix; + const Tensor input_tensor = + ctx->input(tf_param).dtype() != DT_RESOURCE + ? ctx->input(tf_param) + : *resource_vars_snapshots.at(missing_ctx_input_prefix + tf_param); + stream_executor::DeviceAddressBase input_buffer = + XlaTensor::DeviceMemoryFromTensor(input_tensor); + stream_executor::DeviceAddressBase output_buffer = + output.buffer({output_num}); + if (input_buffer.opaque() == output_buffer.opaque()) { + // In the case of a donated buffer, both input_tensor and output think + // they have ownership of the buffer (see comment in + // PopulateExecutionInputBuffer). Release ownership from output to avoid + // double free. + output.set_buffer(stream_executor::ScopedDeviceAddress(), + {output_num}); + return input_tensor; + } + } + + if (allocate_xla_tensors) { + Tensor output_tensor; + TF_RETURN_IF_ERROR( + ctx->allocate_temp(output_dtype, output_shape, &output_tensor)); + if (output_tensor.TotalBytes() > 0) { + XlaTensor* xla_tensor = XlaTensor::FromTensor(&output_tensor); + TF_RET_CHECK(xla_tensor); + xla_tensor->set_shaped_buffer(output.TakeSubTree({output_num})); + if (use_multiple_streams) { + xla_tensor->ResetDefinitionEvent(definition_event, stream); + } + } + return output_tensor; + } + + stream_executor::DeviceAddressBase output_buffer = + output.buffer({output_num}); + Tensor output_tensor = + MakeTensor(output_dtype, output_shape, output_buffer, output_allocator); + output.set_buffer(stream_executor::ScopedDeviceAddress(), + {output_num}); + return output_tensor; +} + +// Sets output `output_num` for `ctx` provided it is known at a compile time. +absl::Status SetOutputForConstant( + OpKernelContext* ctx, bool requires_copy_to_device, + const XlaCompiler::CompilationResult* compilation_result, int output_num) { + CHECK(compilation_result->outputs[output_num].is_constant); + const Tensor& const_tensor = + compilation_result->outputs[output_num].constant_value; + Tensor* output_tensor; + if (requires_copy_to_device && const_tensor.TotalBytes() > 0) { + // Copy host -> device. (Empty tensors don't have backing buffers.) + // Manually allocate memory so we can allocate as much memory as the device + // requires (as given by GetByteSizeRequirement). This avoids + // XlaTransferManager having to reallocate the device buffer later if + // XlaTransferManager is used. + VLOG(1) << "Constant output tensor on device"; + + TF_RETURN_IF_ERROR( + ctx->allocate_output(output_num, const_tensor.shape(), &output_tensor)); + Device* device = dynamic_cast(ctx->device()); + if (device == nullptr) { + return absl::InternalError("DeviceBase was not a Device."); + } + ctx->op_device_context()->CopyCPUTensorToDevice( + &const_tensor, device, output_tensor, + [&](absl::Status status) { CHECK_OK(status); }); + + if (device->device_type() == DEVICE_GPU) { + // The GPUDeviceContext enqueues the host->device transfer in a + // separate stream from the main compute stream. We must ensure the + // compute stream is synchronized with the host->device transfer + // stream now otherwise we will create a race condition. + auto* gpu_device_context = + static_cast(ctx->op_device_context()); + TF_RETURN_IF_ERROR(gpu_device_context->stream()->WaitFor( + gpu_device_context->host_to_device_stream())); + } + } else { + // No copy required. + ctx->set_output(output_num, const_tensor); + output_tensor = ctx->mutable_output(output_num); + } + return absl::OkStatus(); +} + +static absl::StatusOr GetOrCreateResourceVar( + OpKernelContext* ctx, const ResourceHandle& handle, + const XlaCompiler::ResourceUpdate& write) { + Var* variable = nullptr; + TF_RETURN_IF_ERROR( + LookupOrCreateResource(ctx, handle, &variable, [&write](Var** ptr) { + *ptr = new Var(write.type); + return absl::OkStatus(); + })); + return variable; +} + +absl::StatusOr> GatherVariableInfo( + OpKernelContext* ctx, + const XlaCompiler::CompilationResult& compilation_result, + int missing_ctx_input_prefix) { + std::vector out; + out.reserve(compilation_result.resource_updates.size()); + for (int i = 0; i < compilation_result.resource_updates.size(); ++i) { + const XlaCompiler::ResourceUpdate& write = + compilation_result.resource_updates[i]; + int actual_input_index = write.input_index - missing_ctx_input_prefix; + if (actual_input_index < 0 || actual_input_index >= ctx->num_inputs()) { + return xla::Internal("Invalid input index for variable write."); + } + + const ResourceHandle handle = HandleFromInput(ctx, actual_input_index); + TF_ASSIGN_OR_RETURN(Var * variable, + GetOrCreateResourceVar(ctx, handle, write)); + out.emplace_back(actual_input_index, handle.name(), variable, + handle.definition_stack_trace()); + } + return std::move(out); +} + +absl::Status XlaComputationLaunchContext::PopulateOutputs( + OpKernelContext* ctx, + const XlaCompiler::CompilationResult* compilation_result, + ScopedShapedBuffer output, int missing_ctx_input_prefix, + absl::Span variable_infos, + const xla::HloInputOutputAliasConfig& input_output_alias, + const absl::flat_hash_map& resource_vars) { + se::Stream* stream = + ctx->op_device_context() ? ctx->op_device_context()->stream() : nullptr; + Allocator* allocator = ctx->device()->GetAllocator({}); + + // Computation output should always be a tuple. + VLOG(2) << "Result tuple shape: " << output.on_host_shape().ToString(); + VLOG(2) << "Result tuple shape (on device): " + << output.on_device_shape().ToString(); + CHECK_EQ(ctx->num_outputs(), compilation_result->outputs.size()); + + // If the on-host-shape isn't a tuple, create a new single-element tuple + // buffer with a nullptr root index table. This allows the code below to treat + // output as a tuple unconditionally. + if (!output.on_host_shape().IsTuple()) { + ShapedBuffer nontuple_buffer = output.release(); + ShapedBuffer buffer( + xla::ShapeUtil::MakeTupleShape({nontuple_buffer.on_device_shape()}), + output.device_ordinal()); + buffer.buffers().CopySubtreeFrom(nontuple_buffer.buffers(), + /*src_index=*/{}, + /*dst_index=*/{0}); + output = ScopedShapedBuffer(std::move(buffer), output.memory_allocator()); + } + + std::shared_ptr definition_event; + if (use_multiple_streams_ && stream) { + TF_ASSIGN_OR_RETURN(definition_event, stream->parent()->CreateEvent()); + TF_RETURN_IF_ERROR(stream->RecordEvent(definition_event.get())); + } + + for (const XlaOutputDescription& descr : compilation_result->outputs) { + if (descr.type == DT_VARIANT) { + return xla::Unimplemented( + "Support for TensorList crossing the XLA/TF boundary " + "is not implemented"); + } + } + + std::vector output_tensor_shapes; + output_tensor_shapes.reserve(ctx->num_outputs()); + if (output.on_host_shape().is_dynamic()) { + const se::Platform* platform = nullptr; + if (stream != nullptr) { + platform = stream->parent()->GetPlatform(); + } else { + // Stream is not set for the host platform. + TF_ASSIGN_OR_RETURN(platform, + se::PlatformManager::PlatformWithId( + XlaPlatformInfoFromDevice(ctx->device()))); + } + TF_ASSIGN_OR_RETURN(auto transfer_manager, + xla::TransferManager::GetForPlatform(platform)); + + xla::Shape output_device_shape = output.on_device_shape(); + TF_RETURN_IF_ERROR(transfer_manager->ReadDynamicShapes( + stream, &output, &output_device_shape)); + + output.set_shapes(output_device_shape, output_device_shape); + for (int i = 0; i < ctx->num_outputs(); ++i) { + const xla::Shape& subshape = + xla::ShapeUtil::GetSubshape(output_device_shape, {i}); + TensorShape shape; + TF_RETURN_IF_ERROR(XLAShapeToTensorShape(subshape, &shape)); + output_tensor_shapes.push_back(shape); + } + } else { + for (int i = 0; i < ctx->num_outputs(); ++i) { + output_tensor_shapes.push_back(compilation_result->outputs[i].shape); + } + } + + // Copy XLA results to the OpOutputList. + int output_num = 0; + for (int i = 0, end = ctx->num_outputs(); i < end; ++i) { + const TensorShape& shape = output_tensor_shapes[i]; + const DataType& type = compilation_result->outputs[i].type; + VLOG(2) << "Populating output for retval " << i << " shape " + << shape.DebugString() << " type " << DataTypeString(type); + + if (compilation_result->outputs[i].is_constant) { + TF_RETURN_IF_ERROR(SetOutputForConstant( + ctx, /*requires_copy_to_device=*/stream != nullptr, + compilation_result, i)); + } else if (type == DT_RESOURCE) { + int input_index = + compilation_result->outputs[i].input_index - missing_ctx_input_prefix; + TF_RET_CHECK(input_index >= 0 && input_index < ctx->num_inputs()) + << "Invalid input for outputs " << i << ": " << input_index; + ctx->set_output(i, ctx->input(input_index)); + } else { + TF_ASSIGN_OR_RETURN( + Tensor output_tensor, + GetOrCreateTensorForOutput( + output, output_num, ctx, missing_ctx_input_prefix, + input_output_alias, compilation_result->input_mapping, + resource_vars, ctx->expected_output_dtype(i), shape, allocator, + allocate_xla_tensors_, stream, use_multiple_streams_, + definition_event)); + ctx->set_output(i, output_tensor); + ++output_num; + } + } + + // input_index -> index into variable_infos. + absl::flat_hash_map variable_info_lookup; + for (int i = 0; i < variable_infos.size(); i++) { + variable_info_lookup.emplace(variable_infos[i].index(), i); + } + + // Apply variable updates, if any. + for (int i = 0, end = compilation_result->resource_updates.size(); i < end; + ++i) { + const XlaCompiler::ResourceUpdate& write = + compilation_result->resource_updates[i]; + int actual_input_index = write.input_index - missing_ctx_input_prefix; + CHECK_GE(actual_input_index, 0); + CHECK_LT(actual_input_index, ctx->num_inputs()); + Var* var = variable_infos[variable_info_lookup[actual_input_index]].var(); + CHECK(var); + + VLOG(2) << "Updating variable #" << i + << " at input index: " << actual_input_index << " with shape " + << write.shape.DebugString() << "; variable tensor has shape: " + << var->tensor()->shape().DebugString(); + + if (var->is_initialized && var->tensor()->dtype() != write.type) { + return absl::InternalError("Mismatched type in variable write"); + } + + TF_ASSIGN_OR_RETURN( + Tensor output_tensor, + GetOrCreateTensorForOutput(output, output_num, ctx, + missing_ctx_input_prefix, input_output_alias, + compilation_result->input_mapping, + resource_vars, write.type, write.shape, + allocator, allocate_xla_tensors_, stream, + use_multiple_streams_, definition_event)); + var->is_initialized |= write.modified; + *var->tensor() = output_tensor; + ++output_num; + } + return absl::OkStatus(); +} + +absl::StatusOr> +XlaComputationLaunchContext::BuildXlaCompilerArguments( + absl::Span must_be_constant_idxs, + absl::Span inputs, + absl::Span variable_args, Device* device) { + if (!must_be_constant_idxs.empty() && + !absl::c_is_sorted(must_be_constant_idxs)) { + return absl::InvalidArgumentError("must_be_constant_idxs is not sorted"); + } + VLOG(2) << "Must be const args: {" + << absl::StrJoin(must_be_constant_idxs, ",") << "} out of " + << inputs.size() << " args"; + std::vector out; + out.reserve(inputs.size()); + + // TODO(cheshire): Avoid duplication with framework/op_kernel.h + DeviceContext* device_context = nullptr; + if (device != nullptr) { + TF_RETURN_IF_ERROR(device->TryGetDeviceContext(&device_context)); + bool using_default_context = false; + auto cleanup = absl::MakeCleanup([&] { + if (device_context != nullptr && !using_default_context) { + device_context->Unref(); + } + }); + if (device_context == nullptr) { + using_default_context = true; + auto* dev_info = device->tensorflow_accelerator_device_info(); + if (dev_info) device_context = dev_info->default_context; + } + } + + absl::flat_hash_map variable_info_lookup; + CHECK_OK(CreateVariableInfoLookup(variable_args, variable_info_lookup)); + for (int64_t input_num = 0; input_num < inputs.size(); ++input_num) { + const Tensor* input = inputs[input_num]; + XlaCompiler::Argument& arg = out.emplace_back(); + + if (variable_info_lookup.count(input_num) && device != nullptr) { + // Handles resource variables. + TF_RET_CHECK(input->dtype() == DT_RESOURCE); + const VariableInfo& variable = *variable_info_lookup[input_num]; + arg.name = std::string(variable.name()); + arg.kind = XlaCompiler::Argument::kResource; + arg.resource_kind = XlaResource::kVariable; + arg.definition_stack_trace = variable.definition_stack_trace(); + if (variable.var() && variable.var()->is_initialized) { + const Tensor* value = variable.var()->tensor(); + arg.type = value->dtype(); + arg.shape = value->shape(); + arg.initialized = true; + } else { + // The values of uninitialized variables are not passed as inputs, since + // they are meaningless. However, it is legal to assign to a resource + // variable for the first time inside the XLA computation, so we do + // permit uninitialized variables. + arg.initialized = false; + arg.type = DT_INVALID; + arg.shape = TensorShape(); + } + + if (absl::c_binary_search(must_be_constant_idxs, input_num)) { + TF_RET_CHECK(variable.var() && variable.var()->is_initialized); + const Tensor* value = variable.var()->tensor(); + Tensor value_on_host(value->dtype(), value->shape()); + if (!device_context) { + value_on_host = *value; + } else { + TF_RETURN_IF_ERROR(device_context->CopyDeviceTensorToCPUSync( + value, "", device, &value_on_host)); + } + arg.kind = XlaCompiler::Argument::kConstantResource; + arg.constant_value = value_on_host; + } + } else if (absl::c_binary_search(must_be_constant_idxs, input_num)) { + arg.kind = XlaCompiler::Argument::kConstant; + arg.type = input->dtype(); + arg.shape = input->shape(); + arg.constant_value = *input; + } else { + // Normal inputs. + TF_RET_CHECK(input->dtype() != DT_RESOURCE); + if (input->NumElements() > 0) { + arg.kind = XlaCompiler::Argument::kParameter; + } else { + arg.kind = XlaCompiler::Argument::kConstant; + arg.constant_value = *input; + } + arg.type = input->dtype(); + arg.shape = input->shape(); + } + } + + return out; +} + +// TODO(b/289002708) Create a unit test to cover use_pjrt_tensor_buffer=true. +absl::Status PreparePjRtExecutableArguments( + int num_missing_prefix_ctx_inputs, const std::vector& input_mapping, + const std::vector& inputs, + const absl::flat_hash_map& variable_snapshots, + xla::PjRtClient* pjrt_client, xla::PjRtDevice* pjrt_device, + bool use_pjrt_tensor_buffer, std::vector* args, + std::vector>* owned_args, + absl::flat_hash_set* non_donatable_input_indices) { + for (auto arg_num : input_mapping) { + const Tensor* tensor; + if (auto it = variable_snapshots.find(arg_num); + it != variable_snapshots.end()) { + tensor = it->second; + } else { + tensor = inputs[arg_num - num_missing_prefix_ctx_inputs]; + } + + // The input tensor can have the following cases. + // 1. Tensor with PjRtTensorBuffer, containing a PjRtBuffer. This case + // occurs when the producer is a XLA kernel (e.g.XlaLocalLaunch), or if this + // tensor is produced by host-to-device transfer via PjRtDeviceContext. + // + // 2. Old fashion Tensor with raw device memory pointer. This case occurs + // when the producer is a non-XLA TF GPU kernel or function (e.g. + // tf.matmul). + // + // 3. AsyncValueTensor, containing a PjRtBuffer. This is the legacy mode + // and certain device type (e.g. TPU) still uses this path. + AsyncValueTensor* av_tensor = AsyncValueTensor::FromTensor(tensor); + if (use_pjrt_tensor_buffer) { + if (av_tensor != nullptr) { + return absl::InvalidArgumentError( + "If use_pjrt_tensor_buffer is set, the input tensor should not " + "contain an AsyncValueTensor."); + } + const PjRtTensorBuffer* pjrt_tensor_buffer = + dynamic_cast(DMAHelper::buffer(tensor)); + if (pjrt_tensor_buffer != nullptr) { + args->push_back(pjrt_tensor_buffer->pjrt_buffer()); + } else { + // Creates a PjRtBuffer from DeviceMemoryBase. The newly created + // PjRtBuffer needs to be persisted till XLA execution is completed. + xla::Shape device_shape; + TF_RETURN_IF_ERROR(TensorShapeToXLAShape( + tensor->dtype(), tensor->shape(), &device_shape)); + TF_ASSIGN_OR_RETURN( + std::unique_ptr pjrt_buffer, + pjrt_client->CreateViewOfDeviceBuffer( + const_cast(tensor->tensor_data().data()), device_shape, + pjrt_device->default_memory_space().value_or(nullptr), + [tensor = *tensor]() {})); + owned_args->push_back(std::move(pjrt_buffer)); + args->push_back(owned_args->back().get()); + } + } else { + if (av_tensor->GetBuffer() == nullptr) { + CHECK_EQ(tensor->NumElements(), 0); // Crash OK + continue; + } + args->push_back(av_tensor->GetBuffer().get()); + } + + if (!tensor->RefCountIsOne()) { + non_donatable_input_indices->insert(args->size() - 1); + } + } + return absl::OkStatus(); +} + +// TODO(b/289002708) Create a unit test to cover use_pjrt_tensor_buffer=true. +absl::Status PopulateCtxOutputsFromPjRtExecutableOutputs( + int num_missing_prefix_ctx_inputs, const std::vector& inputs, + const std::vector& variables, + const XlaCompiler::CompilationResult& compilation_result, + const bool use_pjrt_tensor_buffer, + std::vector>& executable_outputs, + OpKernelContext* ctx) { + // Copy XLA results to the OpOutputList. + int output_num = 0; + for (int i = 0, end = ctx->num_outputs(); i < end; ++i) { + const DataType& type = compilation_result.outputs[i].type; + VLOG(2) << "Populating output for retval " << i << " type " + << DataTypeString(type); + if (type == DT_VARIANT) { + return absl::UnimplementedError( + "Support for TensorList crossing the XLA/TF boundary " + "is not implemented"); + } + + if (compilation_result.outputs[i].is_constant) { + bool requires_copy_to_device = GetDeviceType(ctx) != DEVICE_CPU; + TF_RETURN_IF_ERROR(SetOutputForConstant(ctx, requires_copy_to_device, + &compilation_result, i)); + } else if (type == DT_RESOURCE) { + int input_index = compilation_result.outputs[i].input_index - + num_missing_prefix_ctx_inputs; + TF_RET_CHECK(input_index >= 0 && input_index < ctx->num_inputs()) + << "Invalid input for outputs " << i << ": " << input_index; + ctx->set_output(i, *inputs[input_index]); + } else { + xla::PjRtBuffer* output_buffer = executable_outputs[output_num].get(); + if (output_buffer->IsTuple()) { + return absl::InvalidArgumentError( + "Tuple PJRT buffer output is not supported."); + } + absl::Span dims; + std::optional> logical_dims_storage; + if (output_buffer->has_dynamic_dimensions()) { + TF_ASSIGN_OR_RETURN(std::vector logical_dims, + output_buffer->logical_dimensions()); + logical_dims_storage.emplace(std::move(logical_dims)); + dims = *logical_dims_storage; + } else { + dims = output_buffer->dimensions(); + } + TensorShape tensor_shape; + for (int i = 0; i < dims.size(); ++i) { + TF_RETURN_IF_ERROR(tensor_shape.AddDimWithStatus(dims[i])); + } + if (use_pjrt_tensor_buffer) { + TF_ASSIGN_OR_RETURN( + Tensor output_tensor, + MakeTensorFromPjRtBuffer( + type, tensor_shape, std::move(executable_outputs[output_num]))); + ctx->set_output(i, output_tensor); + } else { + // Uses AsyncValueTensor. This path currently used by TPU but is going + // to be deprecated. + Tensor* output_tensor; + TF_RETURN_IF_ERROR( + ctx->allocate_output(i, tensor_shape, &output_tensor)); + auto output_avt = AsyncValueTensor::FromTensor(output_tensor); + output_avt->SetBuffer(std::move(executable_outputs[output_num])); + } + ++output_num; + } + } + + // Apply variable updates, if any. + const auto& variable_lookup = CreateVariableLookup(variables); + for (int i = 0; i < compilation_result.resource_updates.size(); ++i) { + const XlaCompiler::ResourceUpdate& write = + compilation_result.resource_updates[i]; + int actual_input_index = write.input_index - num_missing_prefix_ctx_inputs; + CHECK_GE(actual_input_index, 0); // Crash OK + CHECK_LT(actual_input_index, ctx->num_inputs()); // Crash OK + auto it = variable_lookup.find(actual_input_index); + if (it == variable_lookup.end()) { + continue; + } + Var* var = variables[it->second].var(); + CHECK(var); // Crash OK + + VLOG(2) << "Updating variable #" << i + << " at input index: " << actual_input_index << " with shape " + << write.shape.DebugString() << "; variable tensor has shape: " + << var->tensor()->shape().DebugString(); + + if (var->is_initialized && var->tensor()->dtype() != write.type) { + return absl::InternalError("Mismatched type in variable write"); + } + + if (use_pjrt_tensor_buffer) { + TF_RETURN_IF_ERROR(PjRtTensorBufferUtil::UpdateOrMakeTensorWithPjRtBuffer( + write.type, write.shape, std::move(executable_outputs[output_num]), + var->tensor())); + } else { + TF_RETURN_IF_ERROR( + ctx->allocate_temp(write.type, write.shape, var->tensor())); + AsyncValueTensor::FromTensor(var->tensor()) + ->SetBuffer(std::move(executable_outputs[output_num])); + } + + var->is_initialized |= write.modified; + ++output_num; + } + return absl::OkStatus(); +} + +xla::ExecuteOptions GetPjRtExecuteOptions( + const DeviceType& device_type, + absl::flat_hash_set non_donatable_input_indices) { + xla::ExecuteOptions options; + // Hardcode run id to always be one: TF distributed strategy + // differentiates between subsequent runs using dependency edges. This + // is safe, as only TF dist-strat can produce distributed ops, and we + // can rely on TF dist-strat invariants. + options.launch_id = 1; + // TODO(b/293186653): investigate we should turn on strict shape checking for + // GPU. + if (device_type == DEVICE_GPU) { + options.strict_shape_checking = false; + } + // Note: TF does not use PJRT host callbacks as of today. Setting this option + // to true to workaround an ExecuteOptions check: [1]. + // + // [1]: + // tensorflow/compiler/xla/pjrt/pjrt_c_api_client.cc;l=923-927;rcl=519286815 + options.use_major_to_minor_data_layout_for_callbacks = true; + options.non_donatable_input_indices = std::move(non_donatable_input_indices); + return options; +} + +DeviceType GetDeviceType(OpKernelContext* ctx) { + auto* device = absl::down_cast(ctx->device()->UnderlyingDevice()); + return DeviceType(device->device_type()); +} + +absl::Status RunPjRtExecutable( + const std::vector& inputs, + const std::vector& variables, + const XlaCompiler::CompilationResult& compilation_result, + xla::PjRtClient* pjrt_client, xla::PjRtLoadedExecutable* executable, + OpKernelContext* ctx) { + absl::flat_hash_map variable_snapshots; + for (int i = 0; i < variables.size(); i++) { + variable_snapshots[variables[i].index()] = variables[i].var()->tensor(); + } + return RunPjRtExecutable(/*num_missing_prefix_ctx_inputs=*/0, inputs, + variable_snapshots, variables, compilation_result, + pjrt_client, executable, ctx); +} + +// TODO(b/289421064): Add unit test for this. +absl::Status RunPjRtExecutable( + int num_missing_prefix_ctx_inputs, const std::vector& inputs, + const absl::flat_hash_map& variable_snapshots, + const std::vector& updated_variables, + const XlaCompiler::CompilationResult& compilation_result, + xla::PjRtClient* pjrt_client, xla::PjRtLoadedExecutable* executable, + OpKernelContext* ctx) { + const CompositeDevice::AcceleratorDeviceInfo* accelerator_device_info = + ctx->device()->tensorflow_accelerator_device_info(); + const bool use_pjrt_tensor_buffer = + (accelerator_device_info == nullptr) + ? true + : accelerator_device_info->use_pjrt_tensor_buffer; + + const DeviceType& device_type = GetDeviceType(ctx); + const int pjrt_device_id = + tsl::GetDeviceIdFromDeviceParsedName(ctx->device()->parsed_name()); + TF_ASSIGN_OR_RETURN( + xla::PjRtDevice * device, + pjrt_client->LookupAddressableDevice(xla::LocalDeviceId(pjrt_device_id))); + + gpu::GpuServingDeviceSelectorResource* device_selector_resource = nullptr; + if (device_type == DEVICE_GPU) { + auto rm = ctx->resource_manager(); + TF_RETURN_IF_ERROR(rm->LookupOrCreate< + gpu::GpuServingDeviceSelectorResource>( + rm->default_container(), gpu::kGpuServingDeviceSelectorResourceName, + &device_selector_resource, + [&](gpu::GpuServingDeviceSelectorResource** device_selector_resource) { + *device_selector_resource = new gpu::GpuServingDeviceSelectorResource( + pjrt_client->addressable_device_count(), + std::make_unique()); + return absl::OkStatus(); + })); + core::ScopedUnref device_selector_resource_ref(device_selector_resource); + + TF_ASSIGN_OR_RETURN(absl::string_view fingerprint, + executable->FingerprintExecutable()); + device_selector_resource->selector()->Enqueue(pjrt_device_id, fingerprint); + } + TF_ASSIGN_OR_RETURN( + std::vector> execute_outputs, + RunPjRtExecutable(num_missing_prefix_ctx_inputs, inputs, + variable_snapshots, updated_variables, device_type, + use_pjrt_tensor_buffer, compilation_result, device, + pjrt_client, executable)); + if (device_selector_resource != nullptr) { + device_selector_resource->selector()->Completed(pjrt_device_id, + /*had_error=*/false); + } + + TF_RETURN_IF_ERROR(PopulateCtxOutputsFromPjRtExecutableOutputs( + num_missing_prefix_ctx_inputs, inputs, updated_variables, + compilation_result, use_pjrt_tensor_buffer, execute_outputs, ctx)); + return absl::OkStatus(); +} + +absl::StatusOr>> RunPjRtExecutable( + int num_missing_prefix_ctx_inputs, const std::vector& inputs, + const absl::flat_hash_map& variable_snapshots, + const std::vector& updated_variables, + const DeviceType& device_type, bool use_pjrt_tensor_buffer, + const XlaCompiler::CompilationResult& compilation_result, + xla::PjRtDevice* device, xla::PjRtClient* pjrt_client, + xla::PjRtLoadedExecutable* executable) { + std::vector executable_args; + executable_args.reserve(compilation_result.input_mapping.size()); + std::vector> owned_executable_args; + absl::flat_hash_set non_donatable_input_indices; + + TF_RETURN_IF_ERROR(PreparePjRtExecutableArguments( + num_missing_prefix_ctx_inputs, compilation_result.input_mapping, inputs, + variable_snapshots, pjrt_client, device, use_pjrt_tensor_buffer, + &executable_args, &owned_executable_args, &non_donatable_input_indices)); + + std::vector> execute_outputs; + std::optional> future; + if (executable->num_replicas() != 1 || executable->num_partitions() != 1) { + TF_ASSIGN_OR_RETURN( + execute_outputs, + executable->ExecuteSharded( + executable_args, device, + GetPjRtExecuteOptions(device_type, + std::move(non_donatable_input_indices)), + future)); + } else { + TF_ASSIGN_OR_RETURN( + execute_outputs, + executable->ExecutePortable( + executable_args, device, + GetPjRtExecuteOptions(device_type, + std::move(non_donatable_input_indices)), + future)); + } + + // We need to ensure the PjRtBuffers owned by `owned_executable_args` live + // until execution is complete. We do this by capturing + // `owned_executable_args` by move, so it is owned by the lambda + // that is executed when the future returned by ExecutePortable/ExecuteSharded + // is ready i.e. when the execution is complete. + if (!owned_executable_args.empty() && future.has_value()) { + future->OnReady([owned_executable_args = + std::move(owned_executable_args)](absl::Status s) {}); + } + + return execute_outputs; +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_launch_util.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_launch_util.h new file mode 100644 index 0000000000000000000000000000000000000000..8cf73f0111df640814ed91ca6d680a16a2c33de5 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_launch_util.h @@ -0,0 +1,269 @@ +/* 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. +==============================================================================*/ + +// Contains utilities for launching compiled XLA kernels for a KernelContext. + +#ifndef TENSORFLOW_COMPILER_JIT_XLA_LAUNCH_UTIL_H_ +#define TENSORFLOW_COMPILER_JIT_XLA_LAUNCH_UTIL_H_ + +#include +#include +#include +#include + +#include "absl/container/flat_hash_map.h" +#include "tensorflow/compiler/jit/variable_info.h" +#include "tensorflow/compiler/jit/xla_tensor.h" +#include "tensorflow/compiler/tf2xla/xla_compiler.h" +#include "xla/client/local_client.h" +#include "xla/pjrt/pjrt_client.h" +#include "xla/service/maybe_owning_device_memory.h" +#include "xla/service/shaped_buffer.h" +#include "xla/stream_executor/device_memory_allocator.h" +#include "tensorflow/core/framework/allocation_description.pb.h" +#include "tensorflow/core/framework/tensor.h" +#include "tensorflow/core/lib/core/status.h" +#include "tensorflow/core/platform/thread_annotations.h" + +namespace tensorflow { + +// Creates a list of updated resource variables. +absl::StatusOr> GatherVariableInfo( + OpKernelContext* ctx, + const XlaCompiler::CompilationResult& compilation_result, + int missing_ctx_input_prefix); + +// Returns pointers to inputs stored in `ctx`. +std::vector InputsFromContext(OpKernelContext* ctx); + +absl::StatusOr> GetConstantInputIndicesFromContext( + OpKernelContext* ctx); + +absl::Status SetOutputForConstant( + OpKernelContext* ctx, bool requires_copy_to_device, + const XlaCompiler::CompilationResult* compilation_result, int output_num); + +// Converts input tensors and variables which are parameters of the +// XlaComputation into PjRtBuffers to be fed as input to the +// PjRtLoadedExecutable. +// +// Assumes that the first `num_missing_prefix_ctx_inputs` inputs to the +// compilation_result are missing in `inputs` and adjusts indexing into `inputs` +// accordingly. +// `input_mapping` is a vector that maps from the parameters of the +// XlaComputation to their original argument positions. This can be sourced from +// `XlaCompiler::CompilationResult::input_mapping`. +// `variable_snapshots` is a map of {index of the input to the +// compilation_result -> underlying Tensor the variable is/was pointing to (i.e. +// the value of the variable at the time of lowering/compilation)}. +// +// The obtained PjRtBuffers are populated to `args` vector. +// `non_donatable_input_indices` will also be set, which contains the indices of +// the input that should not be donated to output. +// +// There can be three types of input: 1. Tensor with PjRtTensorBuffer; 2. +// Tensor with AsyncValueTensor; 3. Tensor with raw device mem pointer. +// For case 3, we need to create a PjRtBuffer from the raw device mem pointer, +// and we need to ensure the PjRtBuffer persists till XLA computation is +// complete. Therefore we put the newly created PjRtBuffer into `owned_args`. +// Caller is responsible to ensure `owned_args` lives till the end of XLA +// computation. +absl::Status PreparePjRtExecutableArguments( + int num_missing_prefix_ctx_inputs, const std::vector& input_mapping, + const std::vector& inputs, + const absl::flat_hash_map& variable_snapshots, + xla::PjRtClient* pjrt_client, xla::PjRtDevice* pjrt_device, + bool use_pjrt_tensor_buffer, std::vector* args, + std::vector>* owned_args, + absl::flat_hash_set* non_donatable_input_indices); + +// Populates the OpKernelContext outputs with the outputs of the +// PjRtLoadedExecutable. Requires the `compilation_result` used to build the +// PjRtLoadedExecutable. +// This function only looks at variables that were updated, so `variables` can +// either be all the variables or only the ones that were updated. +// Assumes that the first `num_missing_prefix_ctx_inputs` inputs to the +// compilation_result are missing in `inputs` and adjusts indexing into `inputs` +// accordingly. +absl::Status PopulateCtxOutputsFromPjRtExecutableOutputs( + int num_missing_prefix_ctx_inputs, const std::vector& inputs, + const std::vector& variables, + const XlaCompiler::CompilationResult& compilation_result, + bool use_pjrt_tensor_buffer, + std::vector>& executable_outputs, + OpKernelContext* ctx); + +// Returns the options used for executing a PjRtLoadedExecutable. +xla::ExecuteOptions GetPjRtExecuteOptions( + const DeviceType& device_type, + absl::flat_hash_set non_donatable_input_indices); + +// Returns the device ordinal from the parsed name of the device. +int GetDeviceOrdinal(const DeviceBase* device); + +// Returns the device type from the OpKernelContext. +DeviceType GetDeviceType(OpKernelContext* ctx); + +// Runs `executable` and populates the outputs in `ctx`. `inputs` and +// `variables` are the input arguments to the computation, usually read from the +// OpKernelContext, `ctx`. Requires the device-appropriate `pjrt_client` and the +// `compilation_result` used to build the `executable`. +absl::Status RunPjRtExecutable( + const std::vector& inputs, + const std::vector& variables, + const XlaCompiler::CompilationResult& compilation_result, + xla::PjRtClient* pjrt_client, xla::PjRtLoadedExecutable* executable, + OpKernelContext* ctx); + +// Same as the above function but takes in `updated_variables` and +// `variable_snapshots` which is a map of {index of the input to the +// compilation_result -> underlying Tensor the variable is/was pointing to +// (i.e. the value of the variable at the time of lowering/compilation)}. +// Assumes that the first `num_missing_prefix_ctx_inputs` inputs to the +// compilation_result are missing in `inputs` and adjusts indexing into `inputs` +// accordingly. +absl::Status RunPjRtExecutable( + int num_missing_prefix_ctx_inputs, const std::vector& inputs, + const absl::flat_hash_map& variable_snapshots, + const std::vector& updated_variables, + const XlaCompiler::CompilationResult& compilation_result, + xla::PjRtClient* pjrt_client, xla::PjRtLoadedExecutable* executable, + OpKernelContext* ctx); + +// Similar to the above function but it does not take an OpKernelContext, and +// it returns the output in PjRtBuffers, instead of populating results into +// OpKernelContext. +absl::StatusOr>> RunPjRtExecutable( + int num_missing_prefix_ctx_inputs, const std::vector& inputs, + const absl::flat_hash_map& variable_snapshots, + const std::vector& updated_variables, + const DeviceType& device_type, bool use_pjrt_tensor_buffer, + const XlaCompiler::CompilationResult& compilation_result, + xla::PjRtDevice* device, xla::PjRtClient* pjrt_client, + xla::PjRtLoadedExecutable* executable); + +// Helper class to perform the marshalling of TensorFlow inputs and outputs to +// ShapedBuffers suitable for passing to an XLA computation. +class XlaComputationLaunchContext { + public: + // Create a new launch context. 'allocate_xla_tensors' is true if allocated + // output tensors and variables are always XlaTensors. If false they are + // assumed to be "normal" device pointers. + // If 'use_multiple_streams' is true, tensors may be defined and used on + // multiple streams and so se::Events must be defined and waited for. If + // 'use_multiple_streams' is true, 'allocate_xla_tensors' must also be true + // because we track inter-stream dependencies through events inside XlaTensor + // objects. + XlaComputationLaunchContext( + xla::LocalClient* client, + stream_executor::DeviceAddressAllocator* xla_allocator, + int device_ordinal, bool allocate_xla_tensors, bool use_multiple_streams); + + // Builds a XlaCompiler::Argument vector from the arguments to an XlaLaunch + // op. + // Precondition: variables in `variable_args` are locked. + static absl::StatusOr> + BuildXlaCompilerArguments(absl::Span must_be_constant_idxs, + absl::Span inputs, + absl::Span variable_args, + Device* device); + + // Add all inputs within `ctx` as XLA arguments (returned by arguments()). + // `variables` is a map from TensorFlow argument number to resource variable. + // + // Assumes that the first `missing_ctx_input_prefix` inputs to the kernel are + // missing and adjusts input indices accordingly. All elements in kernel's + // input_mapping must be greater than or equal to `missing_ctx_input_prefix` + // (in other words, no inputs actually required by the kernel can be missing). + absl::StatusOr> PopulateInputs( + OpKernelContext* ctx, + const XlaCompiler::CompilationResult* compilation_result, + const absl::flat_hash_map& resource_vars, + int missing_ctx_input_prefix, + const xla::HloInputOutputAliasConfig& input_output_alias); + + // Given the XLA output in `output`, populate all outputs of `ctx`. Also + // writes out the resource variable updates. + // + // Updates to all resource variables are written in a single atomic operation. + // This models *->Write dependencies between resource variable operations. + // See jit/resource_operation_safety_analysis for details. + // + // + // Assumes that the first `missing_ctx_input_prefix` inputs to the + // compilation_result are missing and adjusts input indices accordingly. + absl::Status PopulateOutputs( + OpKernelContext* ctx, + const XlaCompiler::CompilationResult* compilation_result, + xla::ScopedShapedBuffer output, int missing_ctx_input_prefix, + absl::Span variable_infos, + const xla::HloInputOutputAliasConfig& input_output_alias, + const absl::flat_hash_map& resource_vars); + + private: + xla::LocalClient* client_; + stream_executor::DeviceAddressAllocator* xla_allocator_; + bool allocate_xla_tensors_; + bool use_multiple_streams_; + int device_ordinal_; +}; + +// A simple TensorBuffer implementation that allows us to create Tensors that +// take ownership of pre-allocated memory. +class XlaTensorBuffer : public TensorBuffer { + public: + XlaTensorBuffer(const void* ptr, size_t expected_size, size_t actual_size, + Allocator* allocator) + : TensorBuffer(const_cast(ptr)), + expected_size_(expected_size), + actual_size_(actual_size), + allocator_(allocator) {} + + ~XlaTensorBuffer() override { + if (data()) { + allocator_->DeallocateRaw(data()); + } + } + + size_t size() const override { return expected_size_; } + + TensorBuffer* root_buffer() override { return this; } + + void FillAllocationDescription(AllocationDescription* proto) const override { + proto->set_requested_bytes(static_cast(expected_size_)); + proto->set_allocator_name(allocator_->Name()); + proto->set_ptr(reinterpret_cast(data())); + if (allocator_->TracksAllocationSizes()) { + auto ab = static_cast(allocator_->AllocatedSize(data())); + proto->set_allocated_bytes(ab); + int64_t id = allocator_->AllocationId(data()); + if (id > 0) { + proto->set_allocation_id(id); + } + if (RefCountIsOne()) { + proto->set_has_single_reference(true); + } + } + } + + private: + size_t expected_size_; + size_t actual_size_; + Allocator* allocator_; +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_XLA_LAUNCH_UTIL_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_launch_util_gpu_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_launch_util_gpu_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..868c267073d64ad7ca4df8e1b879c43c14c9d2b5 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_launch_util_gpu_test.cc @@ -0,0 +1,256 @@ +/* Copyright 2023 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. +==============================================================================*/ + +#if (defined(GOOGLE_CUDA) && GOOGLE_CUDA) || \ + (defined(TENSORFLOW_USE_ROCM) && TENSORFLOW_USE_ROCM) + +// TODO(b/282059652): This test currently only runs inside Google because PJRT +// in GPU device is not buildable in open-source environment. Resolve the issue +// and enable the test in open-source. +#if defined(PLATFORM_GOOGLE) + +#include +#include +#include +#include + +#include +#include "absl/container/flat_hash_set.h" +#include "tensorflow/compiler/jit/device_compiler.h" +#include "tensorflow/compiler/jit/flags.h" +#include "tensorflow/compiler/jit/pjrt_device_compiler_client.h" +#include "tensorflow/compiler/jit/pjrt_device_context.h" +#include "tensorflow/compiler/jit/variable_info.h" +#include "tensorflow/compiler/jit/variable_info_util.h" +#include "tensorflow/compiler/jit/xla_launch_util.h" +#include "tensorflow/compiler/tf2xla/xla_op_registry.h" +#include "xla/pjrt/pjrt_client.h" +#include "xla/tests/literal_test_util.h" +#include "xla/tsl/framework/allocator.h" +#include "xla/tsl/lib/core/status_test_util.h" +#include "tensorflow/core/framework/allocator.h" +#include "tensorflow/core/framework/device.h" +#include "tensorflow/core/framework/fake_input.h" +#include "tensorflow/core/framework/node_def_builder.h" +#include "tensorflow/core/framework/tensor_shape.h" +#include "tensorflow/core/framework/tensor_testutil.h" +#include "tensorflow/core/framework/types.h" +#include "tensorflow/core/kernels/ops_testutil.h" +#include "tensorflow/core/platform/refcount.h" +#include "tensorflow/core/tfrt/common/create_pjrt_client_util.h" +#include "tensorflow/core/tfrt/common/pjrt_util.h" +#include "tsl/platform/status.h" +#include "tsl/platform/statusor.h" + +namespace tensorflow { +namespace { + +using PjRtDeviceCompiler = + DeviceCompiler; +using PjRtDeviceExecutablePersistor = + DeviceExecutablePersistor; + +absl::flat_hash_map GetVariableSnapshots( + const std::vector& variables) { + absl::flat_hash_map variable_snapshots; + for (int i = 0; i < variables.size(); i++) { + variable_snapshots[variables[i].index()] = variables[i].var()->tensor(); + } + return variable_snapshots; +} + +class PjRtExecutionUtilGpuTest : public OpsTestBase { + public: + PjRtExecutionUtilGpuTest() { + // Set flag to use PJRT for device compilation and execution. + auto& rollout_config = GetXlaOpsCommonFlags()->tf_xla_use_device_api; + rollout_config.enabled_for_xla_launch_ = true; + rollout_config.enabled_for_compile_on_demand_ = true; + rollout_config.enabled_for_gpu_ = true; + + // Set flag to enable using XLA devices. PJRT currently is only supported + // for XLA devices. + GetXlaDeviceFlags()->tf_xla_enable_xla_devices = true; + + // Add and setup the GPU device. + auto device_type = DeviceType(DEVICE_GPU); + auto jit_device_type = DeviceType(DEVICE_GPU); + rollout_config.AllowForDeviceInXlaLaunch(device_type); + rollout_config.AllowForDeviceInXlaCompileOnDemand(device_type); + + auto device = + DeviceFactory::NewDevice(device_type.type_string(), SessionOptions(), + "/job:localhost/replica:0/task:0"); + device_ = device.get(); + SetDevice(device_type, std::move(device)); + + XlaShapeLayoutHelpers::ShapeDeterminationFns shape_fns{ + UseNoPreferenceLayoutFn(), IdentityShapeRepresentationFn()}; + device_context_ = core::RefCountPtr( + new PjRtDeviceContext(shape_fns, /*use_pjrt_tensor_buffer=*/true)); + + // Get the host allocator. + AllocatorAttributes host_alloc_attr; + host_alloc_attr.set_on_host(true); + host_allocator_ = device_->GetAllocator(host_alloc_attr); + + AllocatorAttributes device_alloc_attr; + device_alloc_attr.set_on_host(false); + device_allocator_ = device_->GetAllocator(device_alloc_attr); + + // Create the DeviceCompiler to help with compiling executables. + auto pjrt_client_or = GetOrCreatePjRtClient(device_type_); + CHECK_OK(pjrt_client_or.status()); + pjrt_client_ = pjrt_client_or.value(); + device_compiler_ = new PjRtDeviceCompiler( + std::make_unique( + PjRtDeviceExecutablePersistor::Config(), jit_device_type), + std::make_unique(pjrt_client_)); + profiler_ = new DeviceCompilationProfiler(); + + compiler_options_.device_type = jit_device_type; + compiler_options_.client = nullptr; + compiler_options_.flib_def = flib_def_.get(); + } + + ~PjRtExecutionUtilGpuTest() override { + for (const auto& tensor : tensors_) { + delete tensor; + } + tensors_.clear(); + device_context_->Unref(); + core::ScopedUnref device_compiler_ref(device_compiler_); + core::ScopedUnref profiler_ref(profiler_); + } + + // Creates a Tensor on host using the host_allocator_ + template + Tensor* CreateHostTensor(const TensorShape& shape, + const gtl::ArraySlice data) { + Tensor* host_tensor = + new Tensor(host_allocator_, DataTypeToEnum::v(), shape); + test::FillValues(host_tensor, data); + tensors_.push_back(host_tensor); + return host_tensor; + } + + // Creates a Tensor on device using the device_allocator_ + template + Tensor* CreateDeviceTensor(const TensorShape& shape, + const gtl::ArraySlice data) { + Tensor* host_tensor = CreateHostTensor(shape, data); + Tensor* device_tensor = + new Tensor(device_allocator_, DataTypeToEnum::v(), shape); + TF_EXPECT_OK(device_context_->CopyCPUTensorToDeviceSync( + host_tensor, device_, device_tensor)); + + tensors_.push_back(device_tensor); + return device_tensor; + } + + // Compiles the op set in the context_ to a PjRtLoadedExecutable + void CompileToExecutable(const std::vector& args, + const XlaCompiler::CompilationResult** result, + xla::PjRtLoadedExecutable** executable, + XlaCompiler::CompileOptions compile_options = {}) { + TF_EXPECT_OK(device_compiler_->CompileSingleOpIfNeeded( + compiler_options_, args, compile_options, context_.get(), profiler_, + result, executable)); + } + // Creates a Variable. Doesn't add it to the resource manager. + template + Var* CreateVariable(const string& name, const TensorShape& shape, + const gtl::ArraySlice data) { + Tensor* init_var_value = CreateDeviceTensor(shape, data); + Var* var = new Var(DataTypeToEnum::v()); + *var->tensor() = *init_var_value; + var->is_initialized = true; + + return var; + } + + // Creates a Variable, adds it to the resource manager and also adds it as one + // of the inputs in the context_ + template + void AddVariableInput(const string& name, const TensorShape& shape, + const gtl::ArraySlice data) { + Var* var = CreateVariable(name, shape, data); + ResourceMgr* rm = device_->resource_manager(); + TF_ASSERT_OK(rm->Create(rm->default_container(), name, var)); + + ResourceHandle handle; + handle.set_device(device_->name()); + handle.set_container(rm->default_container()); + handle.set_name(name); + TypeIndex type_index = TypeIndex::Make(); + handle.set_hash_code(type_index.hash_code()); + handle.set_maybe_type_name(type_index.name()); + + Tensor* input = new Tensor(host_allocator_, DT_RESOURCE, TensorShape({})); + input->scalar()() = handle; + tensors_.push_back(input); + inputs_.push_back({nullptr, input}); + } + + protected: + core::RefCountPtr device_context_; + Allocator* host_allocator_; + Allocator* device_allocator_; + + XlaCompiler::Options compiler_options_; + xla::PjRtClient* pjrt_client_; + PjRtDeviceCompiler* device_compiler_; + DeviceCompilationProfiler* profiler_; +}; + +TEST_F(PjRtExecutionUtilGpuTest, PreparePjRtExecutableArguments) { + std::vector inputs; + inputs.push_back(CreateDeviceTensor(TensorShape({1, 3}), {0, 0, 0})); + inputs.push_back(CreateDeviceTensor(TensorShape({1, 3}), {1, 2, 3})); + inputs.push_back(CreateDeviceTensor(TensorShape({1, 3}), {4, 5, 6})); + int num_missing_prefix_ctx_inputs = 2; + std::vector input_mapping{3, 4}; + std::vector variables; + + auto pjrt_device = + pjrt_client_->LookupAddressableDevice(device_->parsed_name().id); + TF_EXPECT_OK(pjrt_device.status()); + + std::vector exec_args; + exec_args.reserve(input_mapping.size()); + absl::flat_hash_set non_donatable_input_indices; + TF_EXPECT_OK(PreparePjRtExecutableArguments( + num_missing_prefix_ctx_inputs, input_mapping, inputs, + GetVariableSnapshots(variables), + /*pjrt_client=*/pjrt_client_, /*pjrt_device=*/*pjrt_device, + /*use_pjrt_tensor_buffer=*/true, &exec_args, + /*owned_args=*/{}, &non_donatable_input_indices)); + + EXPECT_EQ(exec_args.size(), 2); + + std::shared_ptr literal1 = *exec_args[0]->ToLiteralSync(); + EXPECT_TRUE(xla::LiteralTestUtil::Equal( + *literal1, xla::LiteralUtil::CreateR2({{1, 2, 3}}))); + + std::shared_ptr literal2 = *exec_args[1]->ToLiteralSync(); + EXPECT_TRUE(xla::LiteralTestUtil::Equal( + *literal2, xla::LiteralUtil::CreateR2({{4, 5, 6}}))); +} + +} // namespace +} // namespace tensorflow + +#endif // PLATFORM_GOOGLE +#endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_launch_util_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_launch_util_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..0274490ae317f5f5835b096759690804639a1567 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_launch_util_test.cc @@ -0,0 +1,722 @@ +/* Copyright 2023 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/compiler/jit/xla_launch_util.h" + +#include +#include +#include +#include + +#include +#include "absl/container/flat_hash_map.h" +#include "absl/container/flat_hash_set.h" +#include "absl/log/check.h" +#include "absl/status/statusor.h" +#include "absl/types/span.h" +#include "tensorflow/compiler/jit/device_compilation_profiler.h" +#include "tensorflow/compiler/jit/device_compiler.h" +#include "tensorflow/compiler/jit/device_executable_persistor.h" +#include "tensorflow/compiler/jit/flags.h" +#include "tensorflow/compiler/jit/pjrt_device_compiler_client.h" +#include "tensorflow/compiler/jit/variable_info.h" +#include "tensorflow/compiler/jit/variable_info_util.h" +#include "tensorflow/compiler/tf2xla/xla_compiler.h" +#include "tensorflow/compiler/tf2xla/xla_op_registry.h" +#include "xla/literal.h" +#include "xla/literal_util.h" +#include "xla/pjrt/pjrt_client.h" +#include "xla/pjrt/pjrt_common.h" +#include "xla/pjrt/pjrt_executable.h" +#include "xla/pjrt/plugin/xla_cpu/cpu_client_options.h" +#include "xla/pjrt/plugin/xla_cpu/xla_cpu_pjrt_client.h" +#include "xla/tests/literal_test_util.h" +#include "xla/tsl/framework/device_id_utils.h" +#include "xla/tsl/lib/core/status_test_util.h" +#include "xla/tsl/platform/statusor.h" +#include "tensorflow/core/framework/allocator.h" +#include "tensorflow/core/framework/device.h" +#include "tensorflow/core/framework/device_base.h" +#include "tensorflow/core/framework/device_factory.h" +#include "tensorflow/core/framework/fake_input.h" +#include "tensorflow/core/framework/node_def_builder.h" +#include "tensorflow/core/framework/resource_handle.h" +#include "tensorflow/core/framework/resource_mgr.h" +#include "tensorflow/core/framework/tensor_shape.h" +#include "tensorflow/core/framework/tensor_testutil.h" +#include "tensorflow/core/framework/type_index.h" +#include "tensorflow/core/framework/types.h" +#include "tensorflow/core/kernels/ops_testutil.h" +#include "tensorflow/core/lib/gtl/array_slice.h" +#include "tensorflow/core/platform/refcount.h" +#include "tensorflow/core/platform/types.h" +#include "tensorflow/core/public/session_options.h" +#include "tensorflow/core/tfrt/common/create_pjrt_client_util.h" +#include "tensorflow/core/tfrt/common/pjrt_util.h" + +namespace tensorflow { +namespace { +using PjRtDeviceCompiler = + DeviceCompiler; +using PjRtDeviceExecutablePersistor = + DeviceExecutablePersistor; + +absl::flat_hash_map GetVariableSnapshots( + const std::vector& variables) { + absl::flat_hash_map variable_snapshots; + for (int i = 0; i < variables.size(); i++) { + variable_snapshots[variables[i].index()] = variables[i].var()->tensor(); + } + return variable_snapshots; +} + +class PjRtExecutionUtilTest : public OpsTestBase { + public: + PjRtExecutionUtilTest() { + // Set flag to use PJRT for device compilation and execution. + auto& rollout_config = GetXlaOpsCommonFlags()->tf_xla_use_device_api; + rollout_config.enabled_for_xla_launch_ = true; + rollout_config.enabled_for_compile_on_demand_ = true; + + // Set flag to enable using XLA devices. PJRT currently is only supported + // for XLA devices. + GetXlaDeviceFlags()->tf_xla_enable_xla_devices = true; + + // Add and setup the XLA_CPU device. + auto device_type = DeviceType(DEVICE_XLA_CPU); + rollout_config.AllowForDeviceInXlaLaunch(device_type); + rollout_config.AllowForDeviceInXlaCompileOnDemand(device_type); + + auto jit_device_type = DeviceType(DEVICE_CPU_XLA_JIT); + auto device = + DeviceFactory::NewDevice(device_type.type_string(), SessionOptions(), + "/job:localhost/replica:0/task:0"); + device_ = device.get(); + SetDevice(device_type, std::move(device)); + + // Create PjRtClient for XLA_CPU. + xla::CpuClientOptions options; + options.asynchronous = true; + options.cpu_device_count = 1; + CHECK_OK(SetPjRtClientInTFGlobalResourceManager( + device_type, xla::GetXlaPjrtCpuClient(options).value())); + + // device_context_ should be a PjRtDeviceContext. + CHECK_OK(device_->TryGetDeviceContext(&device_context_)); + + // Get the host allocator. + AllocatorAttributes host_alloc_attr; + host_alloc_attr.set_on_host(true); + host_allocator_ = device_->GetAllocator(host_alloc_attr); + + // Get the device allocator. This should give us an AsyncValueAllocator. + AllocatorAttributes device_alloc_attr; + device_alloc_attr.set_on_host(false); + device_allocator_ = device_->GetAllocator(device_alloc_attr); + + // Create the DeviceCompiler to help with compiling executables. + auto pjrt_client_or = GetOrCreatePjRtClient(device_type_); + CHECK_OK(pjrt_client_or.status()); + pjrt_client_ = pjrt_client_or.value(); + device_compiler_ = new PjRtDeviceCompiler( + std::make_unique( + PjRtDeviceExecutablePersistor::Config(), jit_device_type), + std::make_unique(pjrt_client_)); + profiler_ = new DeviceCompilationProfiler(); + + compiler_options_.device_type = jit_device_type; + compiler_options_.client = nullptr; + compiler_options_.flib_def = flib_def_.get(); + } + + ~PjRtExecutionUtilTest() override { + for (const auto& tensor : tensors_) { + delete tensor; + } + tensors_.clear(); + device_context_->Unref(); + core::ScopedUnref device_compiler_ref(device_compiler_); + core::ScopedUnref profiler_ref(profiler_); + } + + // Creates a Tensor on host using the host_allocator_ + template + Tensor* CreateHostTensor(const TensorShape& shape, + const gtl::ArraySlice data) { + Tensor* host_tensor = + new Tensor(host_allocator_, DataTypeToEnum::v(), shape); + test::FillValues(host_tensor, data); + tensors_.push_back(host_tensor); + return host_tensor; + } + + // Creates a Tensor on device using the device_allocator_ + template + Tensor* CreateDeviceTensor(const TensorShape& shape, + const gtl::ArraySlice data) { + Tensor* host_tensor = CreateHostTensor(shape, data); + Tensor* device_tensor = + new Tensor(device_allocator_, DataTypeToEnum::v(), shape); + TF_EXPECT_OK(device_context_->CopyCPUTensorToDeviceSync( + host_tensor, device_, device_tensor)); + + tensors_.push_back(device_tensor); + return device_tensor; + } + + // Gets the `output_index`-th output set in the context_ + Tensor* GetOutput(int output_index) { + CHECK_LT(output_index, context_->num_outputs()); + Tensor* device_tensor = context_->mutable_output(output_index); + managed_outputs_.resize(context_->num_outputs()); + if (managed_outputs_[output_index]) { + return managed_outputs_[output_index]; + } + + Tensor* host_tensor = new Tensor(host_allocator_, device_tensor->dtype(), + device_tensor->shape()); + TF_EXPECT_OK(device_context_->CopyDeviceTensorToCPUSync( + device_tensor, "", device_, host_tensor)); + managed_outputs_[output_index] = host_tensor; + return host_tensor; + } + + // Compiles the op set in the context_ to a PjRtLoadedExecutable + void CompileToExecutable(const std::vector& args, + const XlaCompiler::CompilationResult** result, + xla::PjRtLoadedExecutable** executable, + XlaCompiler::CompileOptions compile_options = {}) { + TF_EXPECT_OK(device_compiler_->CompileSingleOpIfNeeded( + compiler_options_, args, compile_options, context_.get(), profiler_, + result, executable)); + } + + // Runs a PjRtLoadedExecutable with the given inputs, variables. Requires the + // XlaCompiler::CompilationResult that was used to build the executable. + absl::StatusOr>> RunExecutable( + const std::vector& inputs, + const std::vector& variables, + const XlaCompiler::CompilationResult* result, + xla::PjRtLoadedExecutable* executable) { + TF_ASSIGN_OR_RETURN(auto pjrt_device, + pjrt_client_->LookupAddressableDevice( + xla::LocalDeviceId(device_->parsed_name().id))); + + std::vector executable_args; + executable_args.reserve(result->input_mapping.size()); + absl::flat_hash_set non_donatable_input_indices; + TF_EXPECT_OK(PreparePjRtExecutableArguments( + /*num_missing_prefix_ctx_inputs=*/0, result->input_mapping, inputs, + GetVariableSnapshots(variables), /*pjrt_client=*/nullptr, + /*pjrt_device=*/nullptr, /*use_pjrt_tensor_buffer=*/false, + &executable_args, /*owned_args=*/{}, &non_donatable_input_indices)); + + xla::ExecuteOptions exe_options; + + // TODO(b/257548614): currently PJRT is compiled as portable (num_replica = + // 1 and num_partition = 1). Support multiple partitions case. + return executable->ExecutePortable(executable_args, pjrt_device, + exe_options); + } + + // Creates a Variable. Doesn't add it to the resource manager. + template + Var* CreateVariable(const std::string& name, const TensorShape& shape, + const gtl::ArraySlice data) { + Tensor* init_var_value = CreateDeviceTensor(shape, data); + Var* var = new Var(DataTypeToEnum::v()); + *var->tensor() = *init_var_value; + var->is_initialized = true; + + return var; + } + + // Creates a Variable, adds it to the resource manager and also adds it as one + // of the inputs in the context_ + template + void AddVariableInput(const std::string& name, const TensorShape& shape, + const gtl::ArraySlice data) { + Var* var = CreateVariable(name, shape, data); + ResourceMgr* rm = device_->resource_manager(); + TF_ASSERT_OK(rm->Create(rm->default_container(), name, var)); + + ResourceHandle handle; + handle.set_device(device_->name()); + handle.set_container(rm->default_container()); + handle.set_name(name); + TypeIndex type_index = TypeIndex::Make(); + handle.set_hash_code(type_index.hash_code()); + handle.set_maybe_type_name(type_index.name()); + + Tensor* input = new Tensor(host_allocator_, DT_RESOURCE, TensorShape({})); + input->scalar()() = handle; + tensors_.push_back(input); + inputs_.push_back({nullptr, input}); + } + + protected: + DeviceContext* device_context_; + Allocator* host_allocator_; + Allocator* device_allocator_; + + XlaCompiler::Options compiler_options_; + xla::PjRtClient* pjrt_client_; + PjRtDeviceCompiler* device_compiler_; + DeviceCompilationProfiler* profiler_; +}; + +TEST_F(PjRtExecutionUtilTest, PreparePjRtExecutableArguments) { + std::vector inputs; + inputs.push_back(CreateDeviceTensor(TensorShape({1, 3}), {0, 0, 0})); + inputs.push_back(CreateDeviceTensor(TensorShape({1, 3}), {1, 2, 3})); + inputs.push_back(CreateDeviceTensor(TensorShape({1, 3}), {4, 5, 6})); + int num_missing_prefix_ctx_inputs = 2; + std::vector input_mapping{3, 4}; + std::vector variables; + + std::vector exec_args; + exec_args.reserve(input_mapping.size()); + absl::flat_hash_set non_donatable_input_indices; + TF_EXPECT_OK(PreparePjRtExecutableArguments( + num_missing_prefix_ctx_inputs, input_mapping, inputs, + GetVariableSnapshots(variables), + /*pjrt_client=*/nullptr, /*pjrt_device=*/nullptr, + /*use_pjrt_tensor_buffer=*/false, &exec_args, + /*owned_args=*/{}, &non_donatable_input_indices)); + + EXPECT_EQ(exec_args.size(), 2); + + std::shared_ptr literal1 = *exec_args[0]->ToLiteral().Await(); + EXPECT_TRUE(xla::LiteralTestUtil::Equal( + *literal1, xla::LiteralUtil::CreateR2({{1, 2, 3}}))); + + std::shared_ptr literal2 = *exec_args[1]->ToLiteral().Await(); + EXPECT_TRUE(xla::LiteralTestUtil::Equal( + *literal2, xla::LiteralUtil::CreateR2({{4, 5, 6}}))); +} + +TEST_F(PjRtExecutionUtilTest, PreparePjRtExecutableArgumentsVariableInputs) { + std::vector variables; + Var* var1 = CreateVariable("v1", TensorShape({1, 2}), {1, 2}); + variables.emplace_back(3, "v1", var1); + Var* var2 = CreateVariable("v2", TensorShape({1, 2}), {3, 4}); + variables.emplace_back(4, "v2", var2); + + std::vector inputs; + inputs.push_back(CreateDeviceTensor(TensorShape({1, 3}), {0, 0, 0})); + int num_missing_prefix_ctx_inputs = 2; + std::vector input_mapping{3, 4}; + + std::vector exec_args; + exec_args.reserve(input_mapping.size()); + absl::flat_hash_set non_donatable_input_indices; + TF_EXPECT_OK(PreparePjRtExecutableArguments( + num_missing_prefix_ctx_inputs, input_mapping, inputs, + GetVariableSnapshots(variables), + /*pjrt_client=*/nullptr, /*pjrt_device=*/nullptr, + /*use_pjrt_tensor_buffer=*/false, &exec_args, + /*owned_args=*/{}, &non_donatable_input_indices)); + + EXPECT_EQ(exec_args.size(), 2); + + std::shared_ptr literal1 = *exec_args[0]->ToLiteral().Await(); + EXPECT_TRUE(xla::LiteralTestUtil::Equal( + *literal1, xla::LiteralUtil::CreateR2({{1, 2}}))); + + std::shared_ptr literal2 = *exec_args[1]->ToLiteral().Await(); + EXPECT_TRUE(xla::LiteralTestUtil::Equal( + *literal2, xla::LiteralUtil::CreateR2({{3, 4}}))); +} + +TEST_F(PjRtExecutionUtilTest, PopulateCtxOutputs) { + XlaOpRegistry::RegisterCompilationKernels(); + TF_EXPECT_OK(NodeDefBuilder("AddV2", "AddV2") + .Input(FakeInput(DT_INT32)) + .Input(FakeInput(DT_INT32)) + .Attr("T", DT_INT32) + .Device("/job:localhost/replica:0/task:0/device:XLA_CPU:0") + .Finalize(node_def())); + TF_EXPECT_OK(InitOp()); + + // Add inputs. + Tensor* a = CreateDeviceTensor(TensorShape({1, 3}), {1, 2, 3}); + Tensor* b = CreateDeviceTensor(TensorShape({1, 3}), {4, 5, 6}); + inputs_.push_back({nullptr, a}); + inputs_.push_back({nullptr, b}); + + CreateContext(); + + std::vector args(2); + args[0].kind = XlaCompiler::Argument::kParameter; + args[0].type = DT_INT32; + args[0].shape = TensorShape({1, 3}); + args[1].kind = XlaCompiler::Argument::kParameter; + args[1].type = DT_INT32; + args[1].shape = TensorShape({1, 3}); + + const XlaCompiler::CompilationResult* result; + xla::PjRtLoadedExecutable* executable; + CompileToExecutable(args, &result, &executable); + + std::vector inputs; + inputs.push_back(a); + inputs.push_back(b); + TF_ASSERT_OK_AND_ASSIGN(auto execute_outputs, + RunExecutable(inputs, {}, result, executable)); + + TF_EXPECT_OK(PopulateCtxOutputsFromPjRtExecutableOutputs( + /*num_missing_prefix_ctx_inputs=*/0, inputs, {}, *result, + /*use_pjrt_tensor_buffer=*/false, execute_outputs, context_.get())); + + Tensor* expected = CreateHostTensor(TensorShape({1, 3}), {5, 7, 9}); + test::ExpectTensorEqual(*expected, *GetOutput(0)); +} + +TEST_F(PjRtExecutionUtilTest, PopulateCtxOutputsDynamicShape) { + XlaOpRegistry::RegisterCompilationKernels(); + TF_EXPECT_OK(NodeDefBuilder("testWhere", "Where") + .Input(FakeInput(DT_FLOAT)) + .Attr("T", DT_FLOAT) + .Device("/job:localhost/replica:0/task:0/device:XLA_CPU:0") + .Finalize(node_def())); + TF_EXPECT_OK(InitOp()); + + // Add inputs. + Tensor* a = + CreateDeviceTensor(TensorShape({2, 3}), {0., 1., 1., 0., 0., 0.}); + inputs_.push_back({nullptr, a}); + + CreateContext(); + + std::vector args(1); + args[0].kind = XlaCompiler::Argument::kParameter; + args[0].type = DT_FLOAT; + args[0].shape = TensorShape({2, 3}); + + const XlaCompiler::CompilationResult* result; + xla::PjRtLoadedExecutable* executable; + CompileToExecutable(args, &result, &executable); + + std::vector inputs; + inputs.push_back(a); + TF_ASSERT_OK_AND_ASSIGN(auto execute_outputs, + RunExecutable(inputs, {}, result, executable)); + + TF_EXPECT_OK(PopulateCtxOutputsFromPjRtExecutableOutputs( + /*num_missing_prefix_ctx_inputs=*/0, inputs, {}, *result, + /*use_pjrt_tensor_buffer=*/false, execute_outputs, context_.get())); + // The expected output is indices of non-zero inputs. + Tensor* expected = + CreateHostTensor(TensorShape({2, 2}), {0, 1, 0, 2}); + test::ExpectTensorEqual(*expected, *GetOutput(0)); +} + +TEST_F(PjRtExecutionUtilTest, PopulateCtxOutputsVariableInputs) { + XlaOpRegistry::RegisterCompilationKernels(); + TF_EXPECT_OK(NodeDefBuilder("AddV2", "AddV2") + .Input(FakeInput(DT_INT32)) + .Input(FakeInput(DT_INT32)) + .Attr("T", DT_INT32) + .Device("/job:localhost/replica:0/task:0/device:XLA_CPU:0") + .Finalize(node_def())); + TF_EXPECT_OK(InitOp()); + + AddVariableInput("var1", TensorShape({1, 2}), {1, 2}); + AddVariableInput("var2", TensorShape({1, 2}), {3, 4}); + + CreateContext(); + + std::vector args(2); + args[0].kind = XlaCompiler::Argument::kParameter; + args[0].initialized = true; + args[0].type = DT_INT32; + args[0].shape = TensorShape({1, 2}); + args[1].kind = XlaCompiler::Argument::kParameter; + args[1].initialized = true; + args[1].type = DT_INT32; + args[1].shape = TensorShape({1, 2}); + + const XlaCompiler::CompilationResult* result; + xla::PjRtLoadedExecutable* executable; + CompileToExecutable(args, &result, &executable); + + std::vector inputs = InputsFromContext(context_.get()); + std::vector variables_indices = + GetResourceVariableIndicesFromContext(context_.get()); + std::vector variables; + variables.reserve(variables_indices.size()); + TF_ASSERT_OK(GetVariableInfosFromInputs(context_->resource_manager(), + context_->device(), inputs, + variables_indices, &variables)); + TF_ASSERT_OK_AND_ASSIGN(auto execute_outputs, + RunExecutable(inputs, variables, result, executable)); + TF_EXPECT_OK(PopulateCtxOutputsFromPjRtExecutableOutputs( + /*num_missing_prefix_ctx_inputs=*/0, inputs, variables, *result, + /*use_pjrt_tensor_buffer=*/false, execute_outputs, context_.get())); + + Tensor* expected = CreateHostTensor(TensorShape({1, 2}), {4, 6}); + test::ExpectTensorEqual(*expected, *GetOutput(0)); +} + +TEST_F(PjRtExecutionUtilTest, PopulateCtxOutputsResourceUpdates) { + XlaOpRegistry::RegisterCompilationKernels(); + TF_EXPECT_OK(NodeDefBuilder("AssignAddVariableOp", "AssignAddVariableOp") + .Input(FakeInput(DT_RESOURCE)) + .Input(FakeInput(DT_INT32)) + .Attr("dtype", DT_INT32) + .Device("/job:localhost/replica:0/task:0/device:XLA_CPU:0") + .Finalize(node_def())); + TF_EXPECT_OK(InitOp()); + + AddVariableInput("var", TensorShape({1, 3}), {1, 2, 3}); + Tensor* a = CreateDeviceTensor(TensorShape({1, 3}), {2, 2, 2}); + inputs_.push_back({nullptr, a}); + + CreateContext(); + + std::vector inputs = InputsFromContext(context_.get()); + std::vector variables_indices = + GetResourceVariableIndicesFromContext(context_.get()); + std::vector variables; + variables.reserve(variables_indices.size()); + TF_ASSERT_OK(GetVariableInfosFromInputs(context_->resource_manager(), + context_->device(), inputs, + variables_indices, &variables)); + TF_ASSERT_OK_AND_ASSIGN(std::vector constant_input_indices, + GetConstantInputIndicesFromContext(context_.get())); + TF_ASSERT_OK(LockVariables(absl::MakeSpan(variables))); + TF_ASSERT_OK_AND_ASSIGN( + std::vector args, + XlaComputationLaunchContext::BuildXlaCompilerArguments( + constant_input_indices, inputs, variables, + static_cast(context_->device()))); + + const XlaCompiler::CompilationResult* result; + xla::PjRtLoadedExecutable* executable; + CompileToExecutable(args, &result, &executable); + TF_ASSERT_OK_AND_ASSIGN(auto execute_outputs, + RunExecutable(inputs, variables, result, executable)); + + TF_EXPECT_OK(PopulateCtxOutputsFromPjRtExecutableOutputs( + /*num_missing_prefix_ctx_inputs=*/0, inputs, variables, *result, + /*use_pjrt_tensor_buffer=*/false, execute_outputs, context_.get())); + + // Verify that there are no outputs. + EXPECT_EQ(context_->num_outputs(), 0); + + // Verify that the original variable was updated. + ResourceMgr* rm = device_->resource_manager(); + Var* var = nullptr; + TF_ASSERT_OK(rm->Lookup(rm->default_container(), "var", &var)); + core::ScopedUnref var_ref(var); + + Tensor* device_tensor = var->tensor(); + Tensor* host_tensor = new Tensor(host_allocator_, device_tensor->dtype(), + device_tensor->shape()); + tensors_.push_back(host_tensor); + TF_ASSERT_OK(device_context_->CopyDeviceTensorToCPUSync( + device_tensor, "", device_, host_tensor)); + + Tensor* expected = CreateHostTensor(TensorShape({1, 3}), {3, 4, 5}); + test::ExpectTensorEqual(*expected, *host_tensor); +} + +TEST(XlaLaunchUtilTest, GetPjRtExecuteOptions) { + xla::ExecuteOptions options = + GetPjRtExecuteOptions(DeviceType(DEVICE_GPU), {}); + EXPECT_FALSE(options.strict_shape_checking); + EXPECT_TRUE(options.use_major_to_minor_data_layout_for_callbacks); +} + +TEST_F(PjRtExecutionUtilTest, RunPjRtExecutable) { + XlaOpRegistry::RegisterCompilationKernels(); + TF_EXPECT_OK(NodeDefBuilder("AddV2", "AddV2") + .Input(FakeInput(DT_INT32)) + .Input(FakeInput(DT_INT32)) + .Attr("T", DT_INT32) + .Device("/job:localhost/replica:0/task:0/device:XLA_CPU:0") + .Finalize(node_def())); + TF_EXPECT_OK(InitOp()); + + AddVariableInput("var1", TensorShape({1, 2}), {1, 2}); + AddVariableInput("var2", TensorShape({1, 2}), {3, 4}); + + CreateContext(); + + std::vector args(2); + args[0].kind = XlaCompiler::Argument::kParameter; + args[0].initialized = true; + args[0].type = DT_INT32; + args[0].shape = TensorShape({1, 2}); + args[1].kind = XlaCompiler::Argument::kParameter; + args[1].initialized = true; + args[1].type = DT_INT32; + args[1].shape = TensorShape({1, 2}); + + const XlaCompiler::CompilationResult* result; + xla::PjRtLoadedExecutable* executable; + CompileToExecutable(args, &result, &executable); + + std::vector inputs = InputsFromContext(context_.get()); + std::vector variables_indices = + GetResourceVariableIndicesFromContext(context_.get()); + std::vector variables; + variables.reserve(variables_indices.size()); + TF_ASSERT_OK(GetVariableInfosFromInputs(context_->resource_manager(), + context_->device(), inputs, + variables_indices, &variables)); + TF_ASSERT_OK(RunPjRtExecutable(inputs, variables, *result, pjrt_client_, + executable, context_.get())); + + Tensor* expected = CreateHostTensor(TensorShape({1, 2}), {4, 6}); + test::ExpectTensorEqual(*expected, *GetOutput(0)); +} + +TEST_F(PjRtExecutionUtilTest, + RunPjRtExecutableWithVariableSnapshotsAndMissingInputs) { + XlaOpRegistry::RegisterCompilationKernels(); + TF_EXPECT_OK(NodeDefBuilder("Fill", "Fill") + .Input(FakeInput(DT_INT32)) + .Input(FakeInput(DT_INT32)) + .Attr("index_type", DT_INT32) + .Attr("T", DT_INT32) + .Device("/job:localhost/replica:0/task:0/device:XLA_CPU:0") + .Finalize(node_def())); + TF_EXPECT_OK(InitOp()); + + Tensor* dims = CreateHostTensor(TensorShape({1}), {2}); + Tensor* value = CreateDeviceTensor(TensorShape(), {1}); + inputs_.push_back({nullptr, dims}); + inputs_.push_back({nullptr, value}); + + CreateContext(); + + TF_ASSERT_OK_AND_ASSIGN(std::vector constant_input_indices, + GetConstantInputIndicesFromContext(context_.get())); + EXPECT_EQ(constant_input_indices.size(), 1); + + // All inputs are present when compilation is requested i.e. in XlaCompile + // (including constants) + std::vector inputs = InputsFromContext(context_.get()); + std::vector variables_indices = + GetResourceVariableIndicesFromContext(context_.get()); + + absl::flat_hash_map variable_snapshots; + const XlaCompiler::CompilationResult* result; + xla::PjRtLoadedExecutable* executable; + { + std::vector variables; + variables.reserve(variables_indices.size()); + TF_ASSERT_OK(GetVariableInfosFromInputs(context_->resource_manager(), + context_->device(), inputs, + variables_indices, &variables)); + TF_ASSERT_OK(LockVariables(absl::MakeSpan(variables))); + variable_snapshots = GetVariableSnapshots(variables); + TF_ASSERT_OK_AND_ASSIGN( + std::vector args, + XlaComputationLaunchContext::BuildXlaCompilerArguments( + constant_input_indices, inputs, variables, + static_cast(context_->device()))); + CompileToExecutable(args, &result, &executable); + } + + // Must-be-constant inputs that appear in the beginning are stripped out at + // the time of execution i.e. in XlaRun. + inputs = {inputs.begin() + constant_input_indices.size(), inputs.end()}; + { + TF_ASSERT_OK_AND_ASSIGN(std::vector updated_variables, + GatherVariableInfo(context_.get(), *result, + constant_input_indices.size())); + TF_ASSERT_OK(LockVariables(absl::MakeSpan(updated_variables))); + TF_ASSERT_OK(RunPjRtExecutable( + constant_input_indices.size(), inputs, variable_snapshots, + updated_variables, *result, pjrt_client_, executable, context_.get())); + } + Tensor* expected = CreateHostTensor(TensorShape({2}), {1, 1}); + test::ExpectTensorEqual(*expected, *GetOutput(0)); +} + +TEST_F(PjRtExecutionUtilTest, RunPjRtExecutableWithoutCtx) { + XlaOpRegistry::RegisterCompilationKernels(); + TF_ASSERT_OK(NodeDefBuilder("AddV2", "AddV2") + .Input(FakeInput(DT_INT32)) + .Input(FakeInput(DT_INT32)) + .Attr("T", DT_INT32) + .Device("/job:localhost/replica:0/task:0/device:XLA_CPU:0") + .Finalize(node_def())); + TF_ASSERT_OK(InitOp()); + + AddVariableInput("var1", TensorShape({1, 2}), {1, 2}); + AddVariableInput("var2", TensorShape({1, 2}), {3, 4}); + + CreateContext(); + + std::vector args(2); + args[0].kind = XlaCompiler::Argument::kParameter; + args[0].initialized = true; + args[0].type = DT_INT32; + args[0].shape = TensorShape({1, 2}); + args[1].kind = XlaCompiler::Argument::kParameter; + args[1].initialized = true; + args[1].type = DT_INT32; + args[1].shape = TensorShape({1, 2}); + + const XlaCompiler::CompilationResult* result; + xla::PjRtLoadedExecutable* executable; + CompileToExecutable(args, &result, &executable); + + std::vector inputs = InputsFromContext(context_.get()); + std::vector variables_indices = + GetResourceVariableIndicesFromContext(context_.get()); + std::vector variables; + variables.reserve(variables_indices.size()); + TF_ASSERT_OK(GetVariableInfosFromInputs(context_->resource_manager(), + context_->device(), inputs, + variables_indices, &variables)); + const bool use_pjrt_tensor_buffer = context_->device() + ->tensorflow_accelerator_device_info() + ->use_pjrt_tensor_buffer; + const DeviceType& device_type = GetDeviceType(context_.get()); + const int pjrt_device_id = + tsl::GetDeviceIdFromDeviceParsedName(context_->device()->parsed_name()); + TF_ASSERT_OK_AND_ASSIGN(xla::PjRtDevice * pjrt_device, + pjrt_client_->LookupAddressableDevice( + xla::LocalDeviceId(pjrt_device_id))); + + absl::flat_hash_map variable_snapshots; + for (int i = 0; i < variables.size(); i++) { + variable_snapshots[variables[i].index()] = variables[i].var()->tensor(); + } + + TF_ASSERT_OK_AND_ASSIGN( + std::vector> execute_outputs, + RunPjRtExecutable(/*num_missing_prefix_ctx_inputs=*/0, inputs, + variable_snapshots, variables, device_type, + use_pjrt_tensor_buffer, *result, pjrt_device, + pjrt_client_, executable)); + + for (const auto& output : execute_outputs) { + TF_ASSERT_OK(output->GetReadyFuture().Await()); + } + + ASSERT_EQ(execute_outputs.size(), 1); + std::shared_ptr literal = + *execute_outputs[0]->ToLiteral().Await(); + EXPECT_TRUE(xla::LiteralTestUtil::Equal( + *literal, xla::LiteralUtil::CreateR2({{4, 6}}))); +} + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_ops_on_regular_devices.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_ops_on_regular_devices.cc new file mode 100644 index 0000000000000000000000000000000000000000..d18ce2e6fadd7e9375d2a6572d0ba1037f13c02c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_ops_on_regular_devices.cc @@ -0,0 +1,125 @@ +/* Copyright 2020 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. +==============================================================================*/ + +// Register XlaXXX operations on regular CPU/GPU devices using +// `XlaCompileOnDemandOp`. +#include "tensorflow/compiler/jit/xla_compile_on_demand_op.h" +#include "tensorflow/core/framework/op_kernel.h" + +namespace tensorflow { + +#define REGISTER_XLA_OPS_ON_DEVICE(DEVICE) \ + REGISTER_KERNEL_BUILDER(Name("XlaConv") \ + .HostMemory("window_strides") \ + .HostMemory("padding") \ + .HostMemory("lhs_dilation") \ + .HostMemory("rhs_dilation") \ + .HostMemory("feature_group_count") \ + .Device(DEVICE), \ + XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER(Name("XlaConvV2") \ + .HostMemory("window_strides") \ + .HostMemory("padding") \ + .HostMemory("lhs_dilation") \ + .HostMemory("rhs_dilation") \ + .HostMemory("feature_group_count") \ + .Device(DEVICE), \ + XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER( \ + Name("XlaBroadcastHelper").HostMemory("broadcast_dims").Device(DEVICE), \ + XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER(Name("XlaSelfAdjointEig").Device(DEVICE), \ + XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER(Name("XlaSvd").Device(DEVICE), \ + XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER(Name("XlaDot").Device(DEVICE), \ + XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER(Name("XlaDotV2").Device(DEVICE), \ + XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER( \ + Name("XlaDynamicSlice").HostMemory("size_indices").Device(DEVICE), \ + XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER(Name("XlaDynamicUpdateSlice").Device(DEVICE), \ + XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER(Name("XlaIf").Device(DEVICE), XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER(Name("XlaOptimizationBarrier").Device(DEVICE), \ + XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER(Name("XlaPad") \ + .HostMemory("padding_low") \ + .HostMemory("padding_high") \ + .HostMemory("padding_interior") \ + .Device(DEVICE), \ + XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER(Name("XlaRecv").Device(DEVICE), \ + XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER(Name("XlaReduce").Device(DEVICE), \ + XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER(Name("XlaVariadicReduce").Device(DEVICE), \ + XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER(Name("XlaVariadicReduceV2").Device(DEVICE), \ + XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER(Name("XlaReducePrecision").Device(DEVICE), \ + XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER(Name("XlaReduceWindow") \ + .HostMemory("window_dimensions") \ + .HostMemory("window_strides") \ + .HostMemory("base_dilations") \ + .HostMemory("window_dilations") \ + .HostMemory("padding") \ + .Device(DEVICE), \ + XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER(Name("XlaSelectAndScatter") \ + .HostMemory("window_dimensions") \ + .HostMemory("window_strides") \ + .HostMemory("padding") \ + .Device(DEVICE), \ + XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER(Name("XlaSend").Device(DEVICE), \ + XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER(Name("XlaSort").Device(DEVICE), \ + XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER(Name("XlaKeyValueSort").Device(DEVICE), \ + XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER( \ + Name("XlaVariadicSort").HostMemory("dimension").Device(DEVICE), \ + XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER(Name("XlaWhile").Device(DEVICE), \ + XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER(Name("XlaDequantize").Device(DEVICE), \ + XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER(Name("XlaEinsum").Device(DEVICE), \ + XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER(Name("XlaRngBitGenerator") \ + .HostMemory("algorithm") \ + .HostMemory("shape") \ + .Device(DEVICE), \ + XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER(Name("XlaSpmdShardToFullShape").Device(DEVICE), \ + XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER(Name("XlaSharding").Device(DEVICE), \ + XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER(Name("XlaReplicaId").Device(DEVICE), \ + XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER( \ + Name("XlaGather").HostMemory("slice_sizes").Device(DEVICE), \ + XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER(Name("XlaScatter").Device(DEVICE), \ + XlaCompileOnDemandOp); \ + REGISTER_KERNEL_BUILDER(Name("XlaCallModule").Device(DEVICE), \ + XlaCompileOnDemandOp); +REGISTER_XLA_OPS_ON_DEVICE(DEVICE_CPU); +REGISTER_XLA_OPS_ON_DEVICE(DEVICE_GPU); + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_platform_info.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_platform_info.cc new file mode 100644 index 0000000000000000000000000000000000000000..fe20957aaac43ebf128cfb151cf3c75385f236a1 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_platform_info.cc @@ -0,0 +1,424 @@ +/* Copyright 2020 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/compiler/jit/xla_platform_info.h" + +#include +#include +#include +#include +#include +#include + +#include "absl/algorithm/container.h" +#include "absl/status/status.h" +#include "absl/strings/str_split.h" +#include "absl/strings/string_view.h" +#include "tensorflow/compiler/jit/device_executable_persistor.h" +#include "tensorflow/compiler/jit/flags.h" +#include "tensorflow/compiler/jit/pjrt_device_compiler_client.h" +#include "tensorflow/compiler/jit/xla_compile_util.h" +#include "tensorflow/compiler/jit/xla_device_compiler_client.h" +#include "xla/client/client_library.h" +#include "xla/client/local_client.h" +#include "xla/pjrt/pjrt_client.h" +#include "xla/service/compiler.h" +#include "xla/stream_executor/platform_manager.h" +#include "xla/tsl/framework/device_type.h" +#include "tensorflow/core/framework/function.h" +#include "tensorflow/core/framework/op_kernel.h" +#include "tensorflow/core/framework/resource_mgr.h" +#include "tensorflow/core/framework/types.h" +#include "tensorflow/core/platform/errors.h" +#include "tensorflow/core/platform/status.h" +#include "tensorflow/core/tfrt/common/create_pjrt_client_util.h" +#include "tensorflow/core/tfrt/common/global_state.h" +#include "tensorflow/core/tfrt/common/pjrt_util.h" +#include "tensorflow/core/tpu/tpu_defs.h" + +namespace tensorflow { +namespace { +using XlaDeviceCompiler = + DeviceCompiler; +using PjRtDeviceCompiler = + DeviceCompiler; +using XlaDeviceExecutablePersistor = + DeviceExecutablePersistor; +using PjRtDeviceExecutablePersistor = + DeviceExecutablePersistor; + +XlaDeviceCompiler* CreateXlaDeviceCompiler( + const XlaDeviceExecutablePersistor::Config& persistor_config, + DeviceType compilation_device_type, xla::LocalClient* local_client) { + return new XlaDeviceCompiler( + std::make_unique( + std::move(persistor_config), compilation_device_type), + std::make_unique(local_client)); +} + +PjRtDeviceCompiler* CreatePjRtDeviceCompiler(DeviceType compilation_device_type, + xla::PjRtClient* pjrt_client) { + std::string persistent_cache_directory = + GetPersistentCacheDirectory(compilation_device_type); + + PjRtDeviceExecutablePersistor::Config persistor_config( + persistent_cache_directory, + GetMarkForCompilationPassFlags()->tf_xla_disable_strict_signature_checks, + GetMarkForCompilationPassFlags()->tf_xla_persistent_cache_prefix, + GetMarkForCompilationPassFlags()->tf_xla_persistent_cache_read_only); + + return new PjRtDeviceCompiler( + std::make_unique( + std::move(persistor_config), compilation_device_type), + std::make_unique(pjrt_client)); +} + +absl::StatusOr>> GetAllowedGpus( + FunctionLibraryRuntime* flr) { + std::optional> gpu_ids = std::nullopt; + + if (flr->config_proto()) { + std::string allowed_gpus = + flr->config_proto()->gpu_options().visible_device_list(); + TF_ASSIGN_OR_RETURN(gpu_ids, ParseVisibleDeviceList(allowed_gpus)); + } + + return gpu_ids; +} + +absl::Status GetCompilationDeviceTypeAndPjRtClient( + const XlaPlatformInfo& platform_info, FunctionLibraryRuntime* flr, + DeviceType* compilation_device_type, xla::PjRtClient** pjrt_client) { + DeviceType device_type = platform_info.device_type(); + + if (platform_info.xla_device_metadata()) { + VLOG(2) << "Building PjRtDeviceCompiler using " + "platform_info.xla_device_metadata()."; + + *compilation_device_type = + platform_info.xla_device_metadata()->jit_device_type(); + TF_ASSIGN_OR_RETURN(*pjrt_client, GetOrCreatePjRtClient(device_type)); + return absl::OkStatus(); + } + + if (platform_info.pjrt_device_metadata()) { + VLOG(2) << "Building PjRtDeviceCompiler using " + "platform_info.pjrt_device_metadata()."; + + *compilation_device_type = + platform_info.pjrt_device_metadata()->jit_device_type(); + TF_ASSIGN_OR_RETURN(*pjrt_client, GetOrCreatePjRtClient(device_type)); + return absl::OkStatus(); + } + + // TFRT-TPU is used if device_type is `DEVICE_TPU` and platform_info does not + // have `xla_device_metadata`. + if (device_type == DEVICE_TPU) { + *compilation_device_type = DeviceType(DEVICE_TPU_XLA_JIT); + TF_ASSIGN_OR_RETURN(*pjrt_client, GetOrCreatePjRtClient(device_type)); + return absl::OkStatus(); + } + + VLOG(2) << "platform_info.xla_device_metadata not found and " + "platform_info.device_type() != DEVICE_TPU. Building " + "PjRtDeviceCompiler for non-XLA device."; + + const XlaOpRegistry::DeviceRegistration* registration; + if (!XlaOpRegistry::GetCompilationDevice(device_type.type(), ®istration)) { + return absl::InvalidArgumentError( + absl::StrCat("No JIT device registered for ", device_type.type())); + } + *compilation_device_type = DeviceType(registration->compilation_device_name); + + TF_ASSIGN_OR_RETURN(auto allowed_gpus, GetAllowedGpus(flr)); + // TODO(b/255826209): Set platform, intra op parallelism threads if required + // and when supported by GetOrCreatePjRtClient(). + // The `allowed_gpus` argument is used only if the `device_type` is GPU. + TF_ASSIGN_OR_RETURN(*pjrt_client, + GetOrCreatePjRtClient(device_type, allowed_gpus)); + + return absl::OkStatus(); +} +} // namespace + +std::string GetPersistentCacheDirectory( + const DeviceType& compilation_device_type) { + // If a persistent cache device type is specified, ensure it matches + // compilation device type. + if (!GetMarkForCompilationPassFlags() + ->tf_xla_persistent_cache_device_types.empty() && + !absl::c_any_of(absl::StrSplit(GetMarkForCompilationPassFlags() + ->tf_xla_persistent_cache_device_types, + ','), + [&](absl::string_view device) { + return compilation_device_type == DeviceType(device); + })) { + return ""; + } + return GetMarkForCompilationPassFlags()->tf_xla_persistent_cache_directory; +} + +absl::StatusOr>> ParseVisibleDeviceList( + absl::string_view visible_device_list) { + std::set gpu_ids; + if (visible_device_list.empty()) { + return {{std::nullopt}}; + } + const std::vector visible_devices = + absl::StrSplit(visible_device_list, ','); + for (const std::string& platform_device_id_str : visible_devices) { + int32_t platform_device_id; + if (!absl::SimpleAtoi(platform_device_id_str, &platform_device_id)) { + return absl::InvalidArgumentError( + absl::StrCat("Could not parse entry in 'visible_device_list': '", + platform_device_id_str, + "'. visible_device_list = ", visible_device_list)); + } + gpu_ids.insert(platform_device_id); + } + return {{gpu_ids}}; +} + +absl::StatusOr GetCompilationDeviceType( + const DeviceType& platform_device_type) { + DeviceType compilation_device_type = platform_device_type; + const XlaOpRegistry::DeviceRegistration* registration = nullptr; + if (!XlaOpRegistry::GetCompilationDevice(platform_device_type.type(), + ®istration)) { + return absl::InvalidArgumentError(absl::StrCat( + "No JIT device registered for ", platform_device_type.type())); + } + compilation_device_type = DeviceType(registration->compilation_device_name); + return compilation_device_type; +} + +absl::Status BuildXlaDeviceCompiler(DeviceBase* device, + FunctionLibraryRuntime* flr, + const XlaPlatformInfo& platform_info, + DeviceType compilation_device_type, + XlaDeviceCompiler** xla_device_compiler) { + if (platform_info.platform_id() == nullptr && + platform_info.device_type() == DEVICE_GPU) { + // We do not need to (and cannot) build a real device compiler for GPU + // if the platform ID is null. We just return a placeholder for supporting + // cross platform lowering. + *xla_device_compiler = new XlaDeviceCompiler(/*persistor=*/nullptr, + /*compiler_client=*/nullptr); + return absl::OkStatus(); + } + std::string persistent_cache_directory = + GetPersistentCacheDirectory(platform_info.device_type()); + + XlaDeviceExecutablePersistor::Config persistor_config( + persistent_cache_directory, + GetMarkForCompilationPassFlags()->tf_xla_disable_strict_signature_checks, + GetMarkForCompilationPassFlags()->tf_xla_persistent_cache_prefix, + GetMarkForCompilationPassFlags()->tf_xla_persistent_cache_read_only); + + if (platform_info.xla_device_metadata()) { + *xla_device_compiler = CreateXlaDeviceCompiler( + persistor_config, + platform_info.xla_device_metadata()->jit_device_type(), + platform_info.xla_device_metadata()->client()); + return absl::OkStatus(); + } + + // TFRT-TPU is used if device type is `DEVICE_TPU` and platform_info does not + // have `xla_device_metadata`. This is used for TFRT-TPU when + // BuildXlaDeviceCompiler() is called in GetCompilerIr(). Currently only + // lowering to HLO is needed there and xla::LocalClient doesn't support + // building the executable for TFRT-TPU and hence, is set to nullptr here. + if (platform_info.device_type() == DEVICE_TPU) { + *xla_device_compiler = CreateXlaDeviceCompiler( + persistor_config, DeviceType(DEVICE_TPU_XLA_JIT), nullptr); + return absl::OkStatus(); + } + + if (platform_info.platform_id() == nullptr) { + return absl::InvalidArgumentError("platform_id is null."); + } + auto platform = + se::PlatformManager::PlatformWithId(platform_info.platform_id()); + if (!platform.ok()) { + return platform.status(); + } + + absl::StatusOr> compiler_for_platform = + xla::Compiler::GetForPlatform(platform.value()->id()); + if (!compiler_for_platform.ok()) { + // In some rare cases (usually in unit tests with very small clusters) we + // may end up transforming an XLA cluster with at least one GPU operation + // (which would normally force the cluster to be compiled using XLA:GPU) + // into an XLA cluster with no GPU operations (i.e. containing only CPU + // operations). Such a cluster can fail compilation (in way that + // MarkForCompilation could not have detected) if the CPU JIT is not + // linked in. + // + // So bail out of _XlaCompile in this case, and let the executor handle + // the situation for us. + const absl::Status& status = compiler_for_platform.status(); + if (status.code() == error::NOT_FOUND) { + return absl::UnimplementedError( + absl::StrCat("Could not find compiler for platform ", + platform.value()->Name(), ": ", status.ToString())); + } + } + + xla::LocalClientOptions client_options; + client_options.set_platform(platform.value()); + if (device != nullptr) { + client_options.set_intra_op_parallelism_threads( + device->tensorflow_cpu_worker_threads()->num_threads); + } + + if (flr != nullptr) { + TF_ASSIGN_OR_RETURN(auto allowed_gpus, GetAllowedGpus(flr)); + client_options.set_allowed_devices(allowed_gpus); + } + + TF_ASSIGN_OR_RETURN( + auto client, xla::ClientLibrary::GetOrCreateLocalClient(client_options)); + + *xla_device_compiler = CreateXlaDeviceCompiler( + persistor_config, compilation_device_type, client); + return absl::OkStatus(); +} + +absl::Status GetOrCreatePjRtDeviceCompilerAndProfiler( + const XlaPlatformInfo& platform_info, ResourceMgr* rm, + FunctionLibraryRuntime* flr, PjRtDeviceCompiler** pjrt_device_compiler, + DeviceCompilationProfiler** profiler) { + const auto& device_type = platform_info.device_type(); + const std::string& compiler_name = + GetPjRtDeviceCompilerResourceName(device_type); + const std::string& profiler_name = + GetPjRtDeviceCompilationProfilerResourceName(device_type); + bool deleted_old_device_compiler = false; + + // Lookup the DeviceCompiler, create one if not found. + absl::Status s = rm->Lookup( + rm->default_container(), compiler_name, pjrt_device_compiler); + if (s.ok() && device_type == DEVICE_TPU) { + auto* existing_pjrt_client = (*pjrt_device_compiler)->client(); + TF_ASSIGN_OR_RETURN(auto* latest_pjrt_client, GetPjRtClient(device_type)); + + if (existing_pjrt_client != latest_pjrt_client) { + // PjRtClient has changed. Delete the PjRtDeviceCompiler (and the cache + // within) and create a new one. + TF_RETURN_IF_ERROR(rm->Delete(rm->default_container(), + compiler_name)); + TF_RETURN_IF_ERROR(rm->Delete( + rm->default_container(), profiler_name)); + + deleted_old_device_compiler = true; + } + } + + // TODO(b/308698131): Try consolidating all PJRT-related state into one class + // instead of directly storing it in the ResourceMgr. + if (!s.ok() || deleted_old_device_compiler) { + DeviceType compilation_device_type(""); + xla::PjRtClient* pjrt_client = nullptr; + TF_RETURN_IF_ERROR(GetCompilationDeviceTypeAndPjRtClient( + platform_info, flr, &compilation_device_type, &pjrt_client)); + + TF_RETURN_IF_ERROR(rm->LookupOrCreate( + rm->default_container(), compiler_name, pjrt_device_compiler, + [&](PjRtDeviceCompiler** pjrt_device_compiler) { + *pjrt_device_compiler = + CreatePjRtDeviceCompiler(compilation_device_type, pjrt_client); + return absl::OkStatus(); + })); + } + + TF_RETURN_IF_ERROR(rm->LookupOrCreate( + rm->default_container(), profiler_name, profiler, + [](DeviceCompilationProfiler** profiler) { + *profiler = new DeviceCompilationProfiler(); + return absl::OkStatus(); + })); + + return absl::OkStatus(); +} + +absl::Status GetOrCreatePjRtDeviceCompilerAndProfiler( + const OpKernelContext& ctx, const XlaPlatformInfo& platform_info, + FunctionLibraryRuntime* flr, + DeviceCompiler** + pjrt_device_compiler, + DeviceCompilationProfiler** profiler) { + TF_ASSIGN_OR_RETURN(ResourceMgr * rm, GetResourceMgrForDeviceCompiler( + ctx, platform_info.device_type())); + return GetOrCreatePjRtDeviceCompilerAndProfiler( + platform_info, rm, flr, pjrt_device_compiler, profiler); +} + +XlaPlatformInfo XlaPlatformInfoFromDevice(DeviceBase* device_base) { + se::Platform::Id platform_id = nullptr; + const XlaDevice::Metadata* xla_device_metadata = nullptr; + const PjRtBaseDevice::Metadata* pjrt_device_metadata = nullptr; + std::shared_ptr custom_allocator; + + const std::string& device_type = device_base->device_type(); + if (device_type == DEVICE_CPU) { + platform_id = se::host::kHostPlatformId; + } else if (device_type == DEVICE_GPU) { + auto device = static_cast(device_base); + platform_id = device->tensorflow_accelerator_device_info() + ->stream->parent() + ->GetPlatform() + ->id(); + } else if (XlaDevice::GetMetadataFromDevice(device_base, &xla_device_metadata) + .ok()) { + // If we are on an XlaDevice, use the underlying XLA platform's allocator + // directly. We could use the StreamExecutor's allocator which may + // theoretically be more correct, but XLA returns a nice OOM message in a + // Status and StreamExecutor does not. + // + // Importantly we can't use ctx->device()->GetAllocator() as the allocator + // (which xla_allocator above uses) as on an XlaDevice, this is a dummy + // allocator that returns XlaTensor objects. The XlaCompiler needs a real + // allocator to allocate real buffers. + platform_id = xla_device_metadata->platform()->id(); + custom_allocator = + xla_device_metadata->client()->backend().shared_memory_allocator(); + } else if (auto metadata = PjRtBaseDevice::GetMetadataFromDevice(device_base); + metadata.ok()) { + pjrt_device_metadata = *metadata; + } + + return XlaPlatformInfo(DeviceType(device_type), platform_id, + xla_device_metadata, pjrt_device_metadata, + custom_allocator); +} + +std::shared_ptr GetAllocator( + DeviceBase* device, se::Stream* stream, + const XlaPlatformInfo& platform_info) { + if (platform_info.custom_allocator()) { + return platform_info.custom_allocator(); + } + auto* alloc = device->GetAllocator({}); + if (!stream) { + // Stream is not set for the host platform. + se::Platform* platform = + se::PlatformManager::PlatformWithId(platform_info.platform_id()) + .value(); + return std::make_shared(alloc, platform); + } + return std::make_shared(alloc, stream); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_platform_info.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_platform_info.h new file mode 100644 index 0000000000000000000000000000000000000000..e1022a0f84d35e39ceebdc70e8ef95d0045ded18 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_platform_info.h @@ -0,0 +1,173 @@ +/* Copyright 2020 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_XLA_PLATFORM_INFO_H_ +#define TENSORFLOW_COMPILER_JIT_XLA_PLATFORM_INFO_H_ + +#include +#include +#include + +#include "tensorflow/compiler/jit/device_compiler.h" +#include "tensorflow/compiler/jit/pjrt_base_device.h" +#include "tensorflow/compiler/jit/xla_device.h" +#include "xla/stream_executor/integrations/tf_allocator_adapter.h" +#include "tensorflow/core/framework/op_kernel.h" + +namespace tensorflow { + +// Holds some information about the platform on which an +// XlaLaunch/_XlaCompile/_XlaRun op must run on. Provides a common layer of +// abstraction for normal, XLA devices and devices inheriting from +// PjRtBaseDevice. +class XlaPlatformInfo { + public: + XlaPlatformInfo() : device_type_("") {} + XlaPlatformInfo(XlaPlatformInfo&&) = default; + explicit XlaPlatformInfo( + const DeviceType device_type, se::Platform::Id platform_id, + const XlaDevice::Metadata* xla_device_metadata, + const PjRtBaseDevice::Metadata* pjrt_device_metadata, + std::shared_ptr device_allocator) + : device_type_(device_type), + platform_id_(platform_id), + xla_device_metadata_(xla_device_metadata), + pjrt_device_metadata_(pjrt_device_metadata), + device_allocator_(device_allocator) {} + + XlaPlatformInfo& operator=(XlaPlatformInfo&& other) = default; + + bool UseMultipleStreams() const { + return xla_device_metadata_ && xla_device_metadata_->UseMultipleStreams(); + } + + // Non-null only when run on an XLA device. + std::shared_ptr custom_allocator() + const { + return device_allocator_; + } + + DeviceType device_type() const { return device_type_; } + + // This is equal to xla_device_metadata()->platform()->id() if + // xla_device_metadata() is not nullptr. + se::Platform::Id platform_id() const { return platform_id_; } + + // This may be null if the op this XlaPlatformInfo is for was not placed on an + // XLA device. + const XlaDevice::Metadata* xla_device_metadata() const { + return xla_device_metadata_; + } + bool is_on_xla_device() const { return xla_device_metadata() != nullptr; } + + const PjRtBaseDevice::Metadata* pjrt_device_metadata() const { + return pjrt_device_metadata_; + } + + private: + DeviceType device_type_; + se::Platform::Id platform_id_; + + // xla_device_metadata_ lives in the tensorflow::DeviceBase in which the + // XlaLaunch/_XlaCompile/_XlaRun op is placed and thus does not die before the + // XlaLaunch/_XlaCompile/_XlaRun OpKernel. + const XlaDevice::Metadata* xla_device_metadata_; + + // pjrt_device_metadata_ lives in tensorflow::PjRtBaseDevice in which the + // XlaLaunch/XlaCompileOnDemand op is placed and thus does not die before the + // op kernel. + const PjRtBaseDevice::Metadata* pjrt_device_metadata_; + + // If the op associated with this XlaPlatformInfo is placed on an XLA device + // then device_allocator_ is the xla::Backend's memory allocator. If the op + // is placed on a regular CPU or GPU device then device_allocator_ is null. + // The allocator is of unknown provenance; keep it in a shared pointer to + // set an artificial refcount of one. + std::shared_ptr device_allocator_; + + XlaPlatformInfo(const XlaPlatformInfo&) = delete; + void operator=(const XlaPlatformInfo&) = delete; +}; + +// Returns a set containing the device ids contained in visible_device_list or +// nullopt if it is empty. It returns error in case of malformed configuration +// string. +absl::StatusOr>> ParseVisibleDeviceList( + absl::string_view visible_device_list); + +// Returns the device type for building a DeviceCompiler from the given platform +// type. +absl::StatusOr GetCompilationDeviceType( + const DeviceType& platform_device_type); + +// Builds a DeviceCompiler that uses xla::LocalClient using `platform_info` and +// `compilation_device_type` (in non-TPU case) and sets *xla_device_compiler to +// point to it. Uses flags from `MarkForCompilationPassFlags` for configuring +// the persistor used in the DeviceCompiler. The platform ID from +// `platform_info` must not be null in CPU case. +absl::Status BuildXlaDeviceCompiler( + DeviceBase* dev, FunctionLibraryRuntime* flr, + const XlaPlatformInfo& platform_info, DeviceType compilation_device_type, + DeviceCompiler** + xla_device_compiler); + +// Fetches a DeviceCompiler from the tfrt_global resource manager (or creates +// one there if not found) that uses xla::PjRtClient using an appropriate +// PjRtClient for `platform_info.device_type()` and sets *pjrt_device_compiler +// to point to it. Also fetches/creates a DeviceCompilationProfiler from/in the +// tfrt_global resource manager for `platform_info.device_type()` and sets +// *profiler to point to it. Uses flags from `MarkForCompilationPassFlags` for +// configuring the persistor used in the DeviceCompiler. Please note that +// non-XLA devices aren't supported yet. This is because: +// 1. PjRtClient doesn't support data transfer for non-XLA devices yet +// 2. Fetching the PjRtClient for non-XLA devices is also not supported yet +absl::Status GetOrCreatePjRtDeviceCompilerAndProfiler( + const OpKernelContext& ctx, const XlaPlatformInfo& platform_info, + FunctionLibraryRuntime* flr, + DeviceCompiler** + pjrt_device_compiler, + DeviceCompilationProfiler** profiler); + +// Same as the above function but takes the resource manager `rm` instead of an +// OpKernelContext. +absl::Status GetOrCreatePjRtDeviceCompilerAndProfiler( + const XlaPlatformInfo& platform_info, ResourceMgr* rm, + FunctionLibraryRuntime* flr, + DeviceCompiler** + pjrt_device_compiler, + DeviceCompilationProfiler** profiler); + +// Returns information about the platform from kernel context. +XlaPlatformInfo XlaPlatformInfoFromDevice(DeviceBase* device); + +// Obtains persistent cache directory for executables that target a given device +// based off xla flags. If you shouldn't use persistent caching, returns "". +std::string GetPersistentCacheDirectory( + const DeviceType& compilation_device_type); + +// Returns allocator from platform info if non-null, or populate and return a +// pointer to the allocator adapter with allocator from context. +// +// This is necessary because for XLA devices the underlying TF allocator returns +// dummy tensors. +// +// `stream` parameter is nullable when running on host. +std::shared_ptr GetAllocator( + DeviceBase* device, se::Stream* stream, + const XlaPlatformInfo& platform_info); + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_XLA_PLATFORM_INFO_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_platform_info_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_platform_info_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..0cc6d8c11a00c1af661741d0bd5a12f8a707fc24 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_platform_info_test.cc @@ -0,0 +1,318 @@ +/* Copyright 2023 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/compiler/jit/xla_platform_info.h" + +#include + +#include +#include "absl/log/check.h" +#include "tensorflow/compiler/jit/device_compilation_profiler.h" +#include "tensorflow/compiler/jit/device_compiler.h" +#include "tensorflow/compiler/jit/flags.h" +#include "tensorflow/compiler/jit/test_util.h" +#include "tensorflow/compiler/tf2xla/xla_op_registry.h" +#include "xla/client/local_client.h" +#include "xla/pjrt/pjrt_client.h" +#include "xla/pjrt/plugin/xla_cpu/cpu_client_options.h" +#include "xla/pjrt/plugin/xla_cpu/xla_cpu_pjrt_client.h" +#include "xla/tsl/lib/core/status_test_util.h" +#include "xla/tsl/platform/statusor.h" +#include "tensorflow/core/framework/device_base.h" +#include "tensorflow/core/framework/types.h" +#include "tensorflow/core/platform/refcount.h" +#include "tensorflow/core/protobuf/error_codes.pb.h" +#include "tensorflow/core/tfrt/common/create_pjrt_client_util.h" +#include "tensorflow/core/tfrt/common/pjrt_util.h" +#include "tensorflow/core/tpu/tpu_defs.h" + +namespace tensorflow { +namespace { +using XlaDeviceCompiler = + DeviceCompiler; +using PjRtDeviceCompiler = + DeviceCompiler; + +class XlaPlatformInfoTest : public ::testing::Test { + protected: + void SetUp() override { + tensorflow::GetXlaDeviceFlags()->tf_xla_enable_xla_devices = true; + tensorflow::GetMarkForCompilationPassFlags() + ->tf_xla_persistent_cache_directory = ""; + tensorflow::GetMarkForCompilationPassFlags() + ->tf_xla_persistent_cache_device_types = ""; + } + + DeviceSetup device_setup_; +}; + +class StubDevice : public DeviceBase { + public: + StubDevice() : DeviceBase(nullptr) {} +}; + +#if GOOGLE_CUDA || TENSORFLOW_USE_ROCM +TEST_F(XlaPlatformInfoTest, BuildXlaDeviceCompilerXlaDeviceMetadata) { + device_setup_.AddDevicesAndSetUp({DEVICE_XLA_GPU}); + + Device* device = device_setup_.GetDevice(DEVICE_XLA_GPU); + const XlaDevice::Metadata* metadata = nullptr; + CHECK_OK(XlaDevice::GetMetadataFromDevice(device, &metadata)); + XlaPlatformInfo platform_info = XlaPlatformInfoFromDevice(device); + + TF_ASSERT_OK_AND_ASSIGN( + DeviceType compilation_device_type, + GetCompilationDeviceType(platform_info.device_type())); + + XlaDeviceCompiler* xla_device_compiler = nullptr; + TF_EXPECT_OK(BuildXlaDeviceCompiler(device, device_setup_.flr(), + platform_info, compilation_device_type, + &xla_device_compiler)); + core::ScopedUnref xla_device_compiler_ref(xla_device_compiler); + + EXPECT_EQ(xla_device_compiler->device_type(), metadata->jit_device_type()); + EXPECT_EQ(xla_device_compiler->client(), metadata->client()); +} + +TEST_F(XlaPlatformInfoTest, BuildXlaDeviceCompilerXlaDeviceCacheEnabled) { + tensorflow::GetMarkForCompilationPassFlags() + ->tf_xla_persistent_cache_directory = "/tmp/xla_cache"; + tensorflow::GetMarkForCompilationPassFlags() + ->tf_xla_persistent_cache_device_types = DEVICE_XLA_GPU; + device_setup_.AddDevicesAndSetUp({DEVICE_XLA_GPU}); + + Device* device = device_setup_.GetDevice(DEVICE_XLA_GPU); + const XlaDevice::Metadata* metadata = nullptr; + CHECK_OK(XlaDevice::GetMetadataFromDevice(device, &metadata)); + XlaPlatformInfo platform_info = XlaPlatformInfoFromDevice(device); + + TF_ASSERT_OK_AND_ASSIGN( + DeviceType compilation_device_type, + GetCompilationDeviceType(platform_info.device_type())); + + XlaDeviceCompiler* xla_device_compiler = nullptr; + TF_EXPECT_OK(BuildXlaDeviceCompiler(device, device_setup_.flr(), + platform_info, compilation_device_type, + &xla_device_compiler)); + core::ScopedUnref xla_device_compiler_ref(xla_device_compiler); + + EXPECT_EQ(xla_device_compiler->device_type(), metadata->jit_device_type()); + EXPECT_EQ(xla_device_compiler->client(), metadata->client()); + EXPECT_EQ(xla_device_compiler->persistor()->persistent_cache_directory(), + "/tmp/xla_cache"); +} + +TEST_F(XlaPlatformInfoTest, BuildXlaDeviceCompilerNonXlaDevice) { + device_setup_.AddDevicesAndSetUp({DEVICE_GPU}); + Device* device = device_setup_.GetDevice(DEVICE_GPU); + + XlaPlatformInfo platform_info = XlaPlatformInfoFromDevice(device); + TF_ASSERT_OK_AND_ASSIGN( + DeviceType compilation_device_type, + GetCompilationDeviceType(platform_info.device_type())); + XlaDeviceCompiler* xla_device_compiler = nullptr; + TF_EXPECT_OK(BuildXlaDeviceCompiler(device, device_setup_.flr(), + platform_info, compilation_device_type, + &xla_device_compiler)); + core::ScopedUnref xla_device_compiler_ref(xla_device_compiler); + + EXPECT_EQ(xla_device_compiler->device_type(), DeviceType(DEVICE_GPU_XLA_JIT)); + EXPECT_TRUE(xla_device_compiler->client() != nullptr); +} + +TEST_F(XlaPlatformInfoTest, GetOrCreatePjRtDeviceCompilerAndProfilerXlaDevice) { + DeviceType device_type = DeviceType(DEVICE_XLA_GPU); + device_setup_.AddDevicesAndSetUp({device_type.type()}); + + Device* device = device_setup_.GetDevice(device_type.type()); + const XlaDevice::Metadata* metadata = nullptr; + CHECK_OK(XlaDevice::GetMetadataFromDevice(device, &metadata)); + XlaPlatformInfo platform_info = XlaPlatformInfoFromDevice(device); + + ResourceMgr resource_mgr(""); + OpKernelContext::Params params; + params.resource_manager = &resource_mgr; + params.device = device; + OpKernelContext ctx(¶ms, 0); + + PjRtDeviceCompiler* pjrt_device_compiler = nullptr; + DeviceCompilationProfiler* profiler = nullptr; + TF_EXPECT_OK(GetOrCreatePjRtDeviceCompilerAndProfiler( + ctx, platform_info, device_setup_.flr(), &pjrt_device_compiler, + &profiler)); + core::ScopedUnref pjrt_device_compiler_ref(pjrt_device_compiler); + core::ScopedUnref profiler_ref(profiler); + + TF_ASSERT_OK_AND_ASSIGN(auto pjrt_client, GetOrCreatePjRtClient(device_type)); + EXPECT_EQ(pjrt_device_compiler->device_type(), metadata->jit_device_type()); + EXPECT_EQ(pjrt_device_compiler->client(), pjrt_client); +} + +TEST_F(XlaPlatformInfoTest, + GetOrCreatePjRtDeviceCompilerAndProfilerGpuDeviceCacheEnabled) { + tensorflow::GetMarkForCompilationPassFlags() + ->tf_xla_persistent_cache_directory = "/tmp/xla_cache"; + tensorflow::GetMarkForCompilationPassFlags() + ->tf_xla_persistent_cache_device_types = DEVICE_GPU_XLA_JIT; + device_setup_.AddDevicesAndSetUp({DEVICE_GPU}); + Device* device = device_setup_.GetDevice(DEVICE_GPU); + XlaPlatformInfo platform_info = XlaPlatformInfoFromDevice(device); + + ResourceMgr resource_mgr(""); + OpKernelContext::Params params; + params.resource_manager = &resource_mgr; + params.device = device; + OpKernelContext ctx(¶ms, 0); + + PjRtDeviceCompiler* pjrt_device_compiler = nullptr; + DeviceCompilationProfiler* profiler = nullptr; + TF_EXPECT_OK(GetOrCreatePjRtDeviceCompilerAndProfiler( + ctx, platform_info, device_setup_.flr(), &pjrt_device_compiler, + &profiler)); + EXPECT_EQ(pjrt_device_compiler->persistor()->persistent_cache_directory(), + "/tmp/xla_cache"); + core::ScopedUnref pjrt_device_compiler_ref(pjrt_device_compiler); + core::ScopedUnref profiler_ref(profiler); +} +#endif + +TEST_F(XlaPlatformInfoTest, BuildXlaDeviceCompilerTpuDevice) { + DeviceType compilation_device_type = DeviceType(DEVICE_TPU_XLA_JIT); + + // Instead of creating/initializing a TPU device, create a dummy platform_info + // and use a nullptr for Device for testing purposes. Only + // XlaPlatformInfo::device_type() is needed to build the appropriate + // XlaDeviceCompiler. + Device* device = nullptr; + XlaPlatformInfo platform_info(DeviceType(DEVICE_TPU), /*platform_id=*/nullptr, + /*xla_device_metadata=*/nullptr, + /*pjrt_device_metadata=*/nullptr, + /*device_allocator=*/nullptr); + + XlaDeviceCompiler* xla_device_compiler = nullptr; + TF_EXPECT_OK(BuildXlaDeviceCompiler(device, nullptr, platform_info, + compilation_device_type, + &xla_device_compiler)); + core::ScopedUnref xla_device_compiler_ref(xla_device_compiler); + + EXPECT_EQ(xla_device_compiler->device_type(), compilation_device_type); + // TFRT-TPU is used if device type is `DEVICE_TPU` and `platform_info` does + // not have `xla_device_metadata`. XlaDeviceCompiler/xla::LocalClient is not + // used in this case. + EXPECT_EQ(xla_device_compiler->client(), nullptr); +} + +TEST_F(XlaPlatformInfoTest, BuildXlaDeviceCompilerNoCompilationCache) { + DeviceType compilation_device_type = DeviceType(DEVICE_TPU_XLA_JIT); + tensorflow::GetMarkForCompilationPassFlags() + ->tf_xla_persistent_cache_directory = "/tmp/xla_cache"; + tensorflow::GetMarkForCompilationPassFlags() + ->tf_xla_persistent_cache_device_types = DEVICE_XLA_GPU; + + // Instead of creating/initializing a TPU device, create a dummy platform_info + // and use a nullptr for Device for testing purposes. Only + // XlaPlatformInfo::device_type() is needed to build the appropriate + // XlaDeviceCompiler. + Device* device = nullptr; + XlaPlatformInfo platform_info(DeviceType(DEVICE_TPU), /*platform_id=*/nullptr, + /*xla_device_metadata=*/nullptr, + /*pjrt_device_metadata=*/nullptr, + /*device_allocator=*/nullptr); + + XlaDeviceCompiler* xla_device_compiler = nullptr; + TF_EXPECT_OK(BuildXlaDeviceCompiler(device, nullptr, platform_info, + compilation_device_type, + &xla_device_compiler)); + core::ScopedUnref xla_device_compiler_ref(xla_device_compiler); + + EXPECT_EQ(xla_device_compiler->device_type(), compilation_device_type); + // Check to make sure compilation cache path is empty. + EXPECT_TRUE( + xla_device_compiler->persistor()->persistent_cache_directory().empty()); +} + +// TODO(b/255826209): Look into using an actual TPU device for the unit test, +// and move this out of OSS. +TEST_F(XlaPlatformInfoTest, + GetOrCreatePjRtDeviceCompilerAndProfilerTpuDeviceNoCompilationCache) { + tensorflow::GetMarkForCompilationPassFlags() + ->tf_xla_persistent_cache_directory = "/tmp/xla_cache"; + tensorflow::GetMarkForCompilationPassFlags() + ->tf_xla_persistent_cache_device_types = DEVICE_GPU_XLA_JIT; + DeviceType device_type = DeviceType(DEVICE_TPU); + DeviceType compilation_device_type = DeviceType(DEVICE_TPU_XLA_JIT); + // Use a CPU PjRtClient instead of a TPU one just for testing whether + // GetOrCreatePjRtClient() is being called with the correct arguments. + xla::CpuClientOptions options; + options.asynchronous = true; + options.cpu_device_count = 1; + CHECK_OK(SetPjRtClientInTFGlobalResourceManager( + device_type, xla::GetXlaPjrtCpuClient(options).value())); + TF_ASSERT_OK_AND_ASSIGN(auto pjrt_client, GetOrCreatePjRtClient(device_type)); + + // Instead of creating/initializing a TPU device, create a dummy platform_info + // for testing purposes. Only XlaPlatformInfo::device_type() is needed to + // build the appropriate PjRtDeviceCompiler. + XlaPlatformInfo platform_info(device_type, /*platform_id=*/nullptr, + /*xla_device_metadata=*/nullptr, + /*pjrt_device_metadata=*/nullptr, + /*device_allocator=*/nullptr); + + OpKernelContext::Params params; + StubDevice stub_device; + params.device = &stub_device; + OpKernelContext ctx(¶ms, 0); + + PjRtDeviceCompiler* pjrt_device_compiler = nullptr; + DeviceCompilationProfiler* profiler = nullptr; + TF_EXPECT_OK(GetOrCreatePjRtDeviceCompilerAndProfiler( + ctx, platform_info, nullptr, &pjrt_device_compiler, &profiler)); + core::ScopedUnref pjrt_device_compiler_ref(pjrt_device_compiler); + core::ScopedUnref profiler_ref(profiler); + + EXPECT_EQ(pjrt_device_compiler->device_type(), compilation_device_type); + EXPECT_EQ(pjrt_device_compiler->client(), pjrt_client); + EXPECT_TRUE( + pjrt_device_compiler->persistor()->persistent_cache_directory().empty()); +} + +TEST_F(XlaPlatformInfoTest, GetPersistentCacheDirectoryMultiple) { + tensorflow::GetMarkForCompilationPassFlags() + ->tf_xla_persistent_cache_directory = "/tmp/xla_cache"; + tensorflow::GetMarkForCompilationPassFlags() + ->tf_xla_persistent_cache_device_types = "GPU,CPU"; + DeviceType device_gpu = DeviceType(DEVICE_GPU); + EXPECT_EQ(GetPersistentCacheDirectory(device_gpu), "/tmp/xla_cache"); + DeviceType device_cpu = DeviceType(DEVICE_CPU); + EXPECT_EQ(GetPersistentCacheDirectory(device_cpu), "/tmp/xla_cache"); + DeviceType device_tpu = DeviceType(DEVICE_TPU); + EXPECT_TRUE(GetPersistentCacheDirectory(device_tpu).empty()); +} + +TEST_F(XlaPlatformInfoTest, GetPersistentCacheDirectoryNoDeviceTypes) { + tensorflow::GetMarkForCompilationPassFlags() + ->tf_xla_persistent_cache_directory = "/tmp/xla_cache"; + tensorflow::GetMarkForCompilationPassFlags() + ->tf_xla_persistent_cache_device_types = ""; + DeviceType device_gpu = DeviceType(DEVICE_GPU); + EXPECT_EQ(GetPersistentCacheDirectory(device_gpu), "/tmp/xla_cache"); + DeviceType device_cpu = DeviceType(DEVICE_CPU); + EXPECT_EQ(GetPersistentCacheDirectory(device_cpu), "/tmp/xla_cache"); + DeviceType device_tpu = DeviceType(DEVICE_TPU); + EXPECT_EQ(GetPersistentCacheDirectory(device_tpu), "/tmp/xla_cache"); +} + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_tensor.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_tensor.cc new file mode 100644 index 0000000000000000000000000000000000000000..3c17f87e271ae0d31ed077d96ab43fbdaf1a54b1 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_tensor.cc @@ -0,0 +1,130 @@ +/* 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 "tensorflow/compiler/jit/xla_tensor.h" + +#include "tensorflow/compiler/tf2xla/shape_util.h" +#include "xla/shape_util.h" + +namespace tensorflow { + +/*static*/ XlaTensor* XlaTensor::FromTensor(const Tensor* tensor) { + if (tensor->NumElements() == 0) { + return nullptr; + } + XlaTensor* xla_tensor = + FromOpaquePointer(const_cast(tensor->tensor_data().data())); + return xla_tensor; +} + +/*static*/ stream_executor::DeviceAddressBase XlaTensor::DeviceMemoryFromTensor( + const Tensor& tensor) { + const XlaTensor* xla_tensor = FromTensor(&tensor); + if (xla_tensor) { + CHECK(xla_tensor->has_shaped_buffer()); + return xla_tensor->shaped_buffer().root_buffer(); + } else { + return stream_executor::DeviceAddressBase( + const_cast(tensor.tensor_data().data()), + tensor.tensor_data().size()); + } +} + +absl::Status XlaTensor::AllocateShapedBuffer(DataType dtype, + const xla::Shape& on_device_shape, + xla::LocalClient* client, + int device_ordinal) { + xla::Shape on_host_shape = + xla::ShapeUtil::DeviceShapeToHostShape(on_device_shape); + xla::ScopedShapedBuffer shaped_buffer(on_host_shape, on_device_shape, + client->backend().memory_allocator(), + device_ordinal); + for (auto& index_to_buffer : shaped_buffer.buffers()) { + xla::Shape subshape = + xla::ShapeUtil::GetSubshape(on_device_shape, index_to_buffer.first); + uint64_t size = + client->backend().transfer_manager()->GetByteSizeRequirement(subshape); + TF_ASSIGN_OR_RETURN(se::ScopedDeviceAddress buffer, + client->backend().memory_allocator()->Allocate( + device_ordinal, size, /*retry_on_failure=*/false, + subshape.layout().memory_space())); + // Move our buffer into shaped_buffer, which takes ownership of it. + index_to_buffer.second = buffer.Release(); + } + + VLOG(4) << shaped_buffer.ToString(); + + set_shaped_buffer(std::move(shaped_buffer)); + return absl::OkStatus(); +} + +void XlaTensor::WaitForDefinitionEventOnStream(se::Stream* stream) { + mutex_lock lock(mu_); + if (!definition_event_) { + return; + } + + // The set of defined streams is expected to be very small indeed (usually + // 1-2), so a simple linear scan should be fast enough. + if (std::find(streams_defined_on_.begin(), streams_defined_on_.end(), + stream) != streams_defined_on_.end()) { + // stream is in streams_defined_on_; it doesn't need to be waited on. + return; + } + + stream->WaitFor(definition_event_.get()).IgnoreError(); + streams_defined_on_.push_back(stream); +} + +void XlaTensor::ResetDefinitionEvent(std::shared_ptr event, + se::Stream* stream) { + mutex_lock lock(mu_); + definition_event_ = std::move(event); + streams_defined_on_ = {stream}; +} + +absl::Status XlaTensor::RefreshStatusOfStreams() { + mutex_lock lock(mu_); + absl::Status status; + for (se::Stream* stream : streams_defined_on_) { + status.Update(stream->RefreshStatus()); + } + return status; +} + +// The pointer tag, OR-ed into the XlaTensor's address to distinguish it from +// device-side tensors, which are either CPU or GPU memory pointers. This works +// because we're guaranteed that CPU and GPU pointers are aligned to > 1 bits. +namespace { +constexpr uintptr_t kTag = 0x1ULL; +} + +/*static*/ XlaTensor* XlaTensor::FromOpaquePointer(void* ptr) { + uintptr_t value = reinterpret_cast(ptr); + if (value & kTag) { + return reinterpret_cast(value & ~kTag); + } else { + return nullptr; + } +} + +/*static*/ void* XlaTensor::ToOpaquePointer(XlaTensor* tensor) { + uintptr_t value = reinterpret_cast(tensor); + CHECK_EQ(value & kTag, 0); + value |= kTag; + return reinterpret_cast(value); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_tensor.h b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_tensor.h new file mode 100644 index 0000000000000000000000000000000000000000..2a37b7044400f23d322a4ad8850df3d277a9f6a2 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_tensor.h @@ -0,0 +1,117 @@ +/* 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_JIT_XLA_TENSOR_H_ +#define TENSORFLOW_COMPILER_JIT_XLA_TENSOR_H_ + +#include + +#include "absl/memory/memory.h" +#include "xla/client/local_client.h" +#include "xla/service/shaped_buffer.h" +#include "tensorflow/core/framework/allocator.h" +#include "tensorflow/core/framework/device_base.h" +#include "tensorflow/core/lib/core/status.h" +#include "tensorflow/core/platform/mutex.h" + +namespace tensorflow { + +// The implementation of a Tensor for an XlaDevice. All device tensors are +// actually one of these. +// +// To distinguish between "normal" device tensors and XlaTensors, the raw +// pointer data stored in the TensorBuffer is a tagged pointer. +class XlaTensor { + public: + // Downcast from a Tensor to an XlaTensor. Return nullptr if the downcast + // fails. + static XlaTensor* FromTensor(const Tensor* tensor); + + // Create a DeviceMemoryBase from a Tensor. The Tensor can be an XlaTensor, in + // which case the returned value is shaped_buffer()->root_buffer(), or a + // normal Tensor in which case the returned value is + // {tensor.tensor_data().data(), tensor.tensor_data().size}. + static stream_executor::DeviceAddressBase DeviceMemoryFromTensor( + const Tensor& tensor); + + // Assign the internal ShapedBuffer to new memory for the given dtype and + // shape. If a ShapedBuffer exists already (has_shaped_buffer() == true), it + // is replaced and the managed memory deallocated. + absl::Status AllocateShapedBuffer(DataType dtype, + const xla::Shape& on_device_shape, + xla::LocalClient* client, + int device_ordinal); + + // Some Tensors can have complex on-device shapes, including tuple shapes. To + // manage the memory for these tensors a ShapedBuffer may be required. + + // Return true if this XlaTensor contains a ShapedBuffer. + bool has_shaped_buffer() const { return shaped_buffer_.has_value(); } + // Return the contained ShapedBuffer. + // REQUIRES: has_shaped_buffer() + const xla::ShapedBuffer& shaped_buffer() const { + CHECK(has_shaped_buffer()); + return *shaped_buffer_; + } + xla::ShapedBuffer& shaped_buffer() { + CHECK(has_shaped_buffer()); + return *shaped_buffer_; + } + // Mutates the XlaTensor to set the ShapedBuffer. + void set_shaped_buffer(xla::ScopedShapedBuffer shaped_buffer) { + shaped_buffer_ = std::move(shaped_buffer); + } + + // Adds synchronization events to 'stream' that wait for this tensor to be + // defined on 'stream'. Does nothing if the tensor is already defined on that + // stream. + void WaitForDefinitionEventOnStream(se::Stream* stream); + + // (Re)sets the definition event of the tensor to 'event', and promises that + // the tensor has already been defined on stream. Removes any previous + // definition event or any previous promises about the tensor being defined on + // streams. + // It is legal to reset the definition event of a tensor when overwriting the + // tensor's value (at which point, it is effectively a new tensor once again.) + void ResetDefinitionEvent(std::shared_ptr event, + se::Stream* stream); + + // Refresh the status of streams_defined_on_. Return the first not-OK stream's + // status or OK. + absl::Status RefreshStatusOfStreams(); + + // Convert from a raw pointer to an XlaTensor, removing the pointer tag. + static XlaTensor* FromOpaquePointer(void* ptr); + // Convert to a raw pointer from an XlaTensor, adding the pointer tag. + static void* ToOpaquePointer(XlaTensor* tensor); + + private: + // The optional contained ShapedBuffer. + std::optional shaped_buffer_; + // An optional host tensor value. + std::optional host_tensor_; + // An optional event that is triggered when the tensor's content has been + // defined. If this event is nullptr, it is assumed that the tensor's content + // is always defined. + std::shared_ptr definition_event_; + // A list of all streams for which the tensor's content is defined for any + // newly enqueued command. + absl::InlinedVector streams_defined_on_ TF_GUARDED_BY(mu_); + mutex mu_; +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_JIT_XLA_TENSOR_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_tpu_device.cc b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_tpu_device.cc new file mode 100644 index 0000000000000000000000000000000000000000..d1283ce17943f058c1f50ece6d63904a88f1a962 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/jit/xla_tpu_device.cc @@ -0,0 +1,34 @@ +/* Copyright 2020 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. +==============================================================================*/ + +#if !defined(PLATFORM_GOOGLE) + +#include "tensorflow/compiler/jit/kernels/xla_ops.h" +#include "tensorflow/compiler/jit/xla_device_ops.h" +#include "tensorflow/core/framework/kernel_def.pb.h" +#include "tensorflow/core/tpu/tpu_defs.h" + +namespace tensorflow { + +// We automatically register this if we are building for open source. For +// Google platforms, we initialize these devices in other places. + +REGISTER_XLA_LAUNCH_KERNEL(DEVICE_TPU_NODE, XlaLocalLaunchOp, kTpuAllTypes); +REGISTER_XLA_COMPILE_KERNEL(DEVICE_TPU_NODE, XlaCompileOp, kTpuAllTypes); +REGISTER_XLA_RUN_KERNEL(DEVICE_TPU_NODE, XlaRunOp, kTpuAllTypes); + +} // namespace tensorflow + +#endif // PLATFORM_GOOGLE diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..b28fe2391384cf613170eddb898fc0945da3e30a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/BUILD @@ -0,0 +1,277 @@ +# Description: +# TensorFlow/TensorFlow Lite/XLA MLIR dialects and tools. + +load( + "//tensorflow:tensorflow.bzl", + "tf_cc_binary", + "tf_cc_test", +) +load("//tensorflow:tensorflow.default.bzl", "filegroup") +load("//tensorflow/core/platform:rules_cc.bzl", "cc_library") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:license"], + default_visibility = ["//visibility:public"], + licenses = ["notice"], +) + +package_group( + name = "subpackages", + packages = ["//tensorflow/compiler/mlir/..."], +) + +exports_files(glob(["g3doc/*.md"] + ["g3doc/_includes/tf_passes.md"])) + +# To reference all tablegen files here when checking for updates to them. +filegroup( + name = "td_files", + srcs = glob(["**/*.td"]), +) + +cc_library( + name = "op_or_arg_name_mapper", + srcs = ["op_or_arg_name_mapper.cc"], + hdrs = ["op_or_arg_name_mapper.h"], + deps = [ + "//tensorflow/compiler/mlir/utils:name_utils", + "@com_google_absl//absl/strings", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + ], +) + +tf_cc_test( + name = "op_or_arg_name_mapper_test", + srcs = ["op_or_arg_name_mapper_test.cc"], + deps = [ + ":op_or_arg_name_mapper", + "@com_google_absl//absl/strings", + "@com_google_googletest//:gtest_main", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + ], +) + +cc_library( + name = "tf_mlir_opt_main", + testonly = True, + srcs = ["tf_mlir_opt_main.cc"], + deps = [ + ":init_mlir", + ":passes", + ":register_common_dialects", + "//tensorflow/compiler/mlir/quantization/stablehlo:bridge_passes", + "//tensorflow/compiler/mlir/tensorflow:mlprogram_util", + "//tensorflow/compiler/mlir/tensorflow/transforms:tensorflow_passes", + "//tensorflow/compiler/mlir/tensorflow/transforms:tensorflow_test_passes", + "//tensorflow/compiler/mlir/tensorflow/transforms:tf_graph_optimization_pass", + "//tensorflow/compiler/mlir/tensorflow/transforms:tf_saved_model_passes", # buildcleaner: keep + "//tensorflow/compiler/mlir/tensorflow/transforms/host_runtime:lower_cluster_to_runtime_ops", + "//tensorflow/compiler/mlir/tensorflow/transforms/host_runtime:runtime_passes", + "//tensorflow/compiler/mlir/tensorflow/transforms/sparsecore:sparsecore_passes", + "//tensorflow/compiler/mlir/tf2xla:compile_mlir_util", + "//tensorflow/compiler/mlir/tf2xla/internal/passes:clustering_passes", + "//tensorflow/compiler/mlir/tf2xla/internal/passes:mlir_to_graph_passes", + "//tensorflow/compiler/mlir/tf2xla/transforms:tf_xla_passes", + "//tensorflow/compiler/mlir/tf2xla/transforms:xla_legalize_tf", + "@llvm-project//mlir:AllPassesAndDialects", + "@llvm-project//mlir:MlirOptLib", + "@llvm-project//mlir:RegisterAllDialects", # buildcleaner: keep + "@llvm-project//mlir:RegisterAllPasses", # buildcleaner: keep + "@llvm-project//mlir:Support", + "@llvm-project//mlir:Transforms", + "@xla//xla/mlir_hlo:all_passes", + ], +) + +cc_library( + name = "passes", + visibility = ["//visibility:public"], + deps = [ + "@llvm-project//mlir:AffineDialect", + "@llvm-project//mlir:QuantOps", + # Link jit lib to link JIT devices required to run + # xla-legalize-tf-with-tf2xla pass. + "//tensorflow/compiler/jit", + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/compiler/mlir/tensorflow/transforms:tensorflow_passes", + "//tensorflow/compiler/mlir/tensorflow/transforms:tf_dialect_passes", + "//tensorflow/compiler/mlir/tf2xla:compile_mlir_util", + ], +) + +cc_library( + name = "init_mlir", + srcs = ["init_mlir.cc"], + hdrs = ["init_mlir.h"], + visibility = ["//visibility:public"], + deps = [ + "//tensorflow/core:lib", + "@llvm-project//llvm:Support", + ], +) + +cc_library( + name = "mlir_graph_optimization_pass", + srcs = ["mlir_graph_optimization_pass.cc"], + hdrs = ["mlir_graph_optimization_pass.h"], + deps = [ + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/compiler/mlir/tensorflow:attribute_utils", + "//tensorflow/compiler/mlir/tensorflow:device_util", + "//tensorflow/compiler/mlir/tensorflow:dump_mlir_util", + "//tensorflow/compiler/mlir/tensorflow:mlir_roundtrip_flags", + "//tensorflow/compiler/mlir/tf2xla:mlir_bridge_rollout_policy", + "//tensorflow/compiler/mlir/tf2xla/api/v2:graph_to_tf_executor", + "//tensorflow/compiler/mlir/tf2xla/api/v2:tf_executor_to_graph", + "//tensorflow/core:core_cpu", + "//tensorflow/core:framework", + "//tensorflow/core:lib", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core/common_runtime:device_set", + "//tensorflow/core/protobuf:for_core_protos_cc", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/log", + "@com_google_absl//absl/log:check", + "@com_google_absl//absl/status", + "@com_google_absl//absl/strings:string_view", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:FuncExtensions", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:ShapeDialect", + ], + alwayslink = 1, +) + +cc_library( + name = "mlir_graph_optimization_pass_registration", + srcs = [ + "mlir_graph_optimization_pass_registration.cc", + ], + deps = [ + ":mlir_graph_optimization_pass", + "//tensorflow/core:core_cpu", + ], + alwayslink = 1, +) + +# This should just be a wrapper around tf_mlir_opt_main. Don't add +# direct dependencies to this binary. +tf_cc_binary( + name = "tf-opt", + testonly = True, + deps = [ + ":tf_mlir_opt_main", + ], +) + +tf_cc_binary( + name = "tf-reduce", + testonly = True, + srcs = ["tf_mlir_reduce_main.cc"], + deps = [ + ":init_mlir", + ":register_common_dialects", + "//tensorflow/compiler/mlir/tensorflow", + "@llvm-project//mlir:AllPassesAndDialects", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:MlirReduceLib", + "@llvm-project//mlir:Reducer", + "@llvm-project//mlir:Support", + ], +) + +cc_library( + name = "register_common_dialects", + testonly = True, + srcs = ["register_common_dialects.cc"], + hdrs = ["register_common_dialects.h"], + deps = [ + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/compiler/mlir/tensorflow:mlprogram_util", + "//tensorflow/compiler/mlir/tools/kernel_gen/ir:tf_framework_ops", + "//tensorflow/core/ir/types:Dialect", + "@llvm-project//mlir:AllExtensions", + "@llvm-project//mlir:AllPassesAndDialects", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:RegisterAllDialects", # buildcleaner: keep + "@llvm-project//mlir:RegisterAllExtensions", # buildcleaner: keep + "@llvm-project//mlir:RegisterAllPasses", # buildcleaner: keep + "@llvm-project//mlir:ShapeDialect", + "@llvm-project//mlir:TensorDialect", + "@llvm-project//mlir:TosaDialect", + "@stablehlo//:register", + "@xla//xla/mlir_hlo:hlo_dialect_registration", + ], +) + +tf_cc_test( + name = "register_common_dialects_test", + srcs = ["register_common_dialects_test.cc"], + deps = [ + ":register_common_dialects", + "@com_google_googletest//:gtest_main", + "@llvm-project//mlir:IR", + ], +) + +tf_cc_binary( + name = "tf-mlir-translate", + testonly = True, + srcs = ["tf_mlir_translate_main.cc"], + deps = [ + ":init_mlir", + "//tensorflow/compiler/mlir/tensorflow:tf_xla_mlir_translate", + "//tensorflow/compiler/mlir/tensorflow:translate_lib", + "//tensorflow/compiler/mlir/tf2xla/tests/registration:graph_to_tf_executor_registration", + "//tensorflow/compiler/mlir/tools:translate_cl_options", + "//tensorflow/compiler/mlir/tools:translate_registration", + "//tensorflow/core:lib", + "//tensorflow/core:tensorflow", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/types:span", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TranslateLib", + "@xla//xla/hlo/translate/hlo_to_mhlo:translate_registration", + "@xla//xla/hlo/translate/mhlo_to_hlo:translate_registration", + ], +) + +tf_cc_test( + name = "mlir_graph_optimization_pass_test", + srcs = ["mlir_graph_optimization_pass_test.cc"], + deps = [ + ":mlir_graph_optimization_pass", + "//tensorflow/core:core_cpu", + "//tensorflow/core:framework", + "//tensorflow/core:ops", + "//tensorflow/core:portable_gif_internal", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core:test", + "//tensorflow/core:test_main", + "//tensorflow/core/common_runtime:device_set", + "//tensorflow/core/common_runtime:optimization_registry", + "//tensorflow/core/framework:tensor_testutil", + "//tensorflow/core/lib/monitoring:cell_reader", + "//tensorflow/core/platform:status", + "//tensorflow/core/protobuf:for_core_protos_cc", + "@com_google_absl//absl/status", + "@com_google_googletest//:gtest_main", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:IR", + ], +) + +filegroup( + name = "litfiles", + srcs = glob(["runlit*py"]), + visibility = ["//tensorflow:__subpackages__"], +) + +exports_files(["run_lit.sh"]) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/g3doc/README.md b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/g3doc/README.md new file mode 100644 index 0000000000000000000000000000000000000000..c2168b6c95d13ed4cd2f4e6c9355fb2f565ca1b3 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/g3doc/README.md @@ -0,0 +1,3 @@ +# TensorFlow MLIR + +These are the docs for: https://www.tensorflow.org/mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/g3doc/_book.yaml b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/g3doc/_book.yaml new file mode 100644 index 0000000000000000000000000000000000000000..cfccf4ba052f963be03e0a9b4fadb2ffd068abb5 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/g3doc/_book.yaml @@ -0,0 +1,29 @@ +upper_tabs: +# Tabs left of dropdown menu +- include: /_upper_tabs_left.yaml +- include: /api_docs/_upper_tabs_api.yaml +# Dropdown menu +- name: Resources + path: /resources + is_default: true + menu: + - include: /resources/_menu_toc.yaml + lower_tabs: + # Subsite tabs + other: + - name: Guide + contents: + - title: Overview + path: /mlir/overview + - heading: Dialects + - title: Overview + path: /mlir/dialects + - title: TensorFlow + path: /mlir/tf_ops + - title: TensorFlow Lite + path: /mlir/tfl_ops + - heading: Passes + - title: TF dialect + path: /mlir/tf_passes + +- include: /_upper_tabs_right.yaml diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/g3doc/_includes/tf_passes.md b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/g3doc/_includes/tf_passes.md new file mode 100644 index 0000000000000000000000000000000000000000..2d34c2790a4f1e2b7c93519f0fa7e6b664a81101 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/g3doc/_includes/tf_passes.md @@ -0,0 +1,2366 @@ + +### `-cluster-tf-ops-by-host` + +_Cluster the TensorFlow ops by host so that each function only contains ops placed on the same host_ + +### `-constant-op-device-assignment` + +_Assign device for tf.Const ops_ + +### `-convert-tf-control-flow-to-scf` + +_Convert TensorFlow control flow to SCF._ + +This pass can be used for all direct control flow lowerings from the TensorFlow +dialect to the SCF dialect. +### `-prepare-tpu-computation-for-tf-export` + +_Prepare TPU computation to be legal for export to TensorFlow_ + +Prepares TPU computation module attached to _TPUCompileMlir op for +TensorFlow graph export by making transformation such as replacing or +removing MLIR or XLA specific attributes that are not legal in TensorFlow +graph. +### `-tf-batch-matmul-to-tf-einsum` + +_Replace TF BatchMatMul op by TF Einsum op._ + +### `-tf-broadcast-fold` + +_Fold explicit broadcasts into the following operations if they support implicit broadcasting on their operand._ + +### `-tf-canonicalize-compile-and-replicate-attributes` + +_Canonicalize compilation and replication attributes._ + +A pass that converts existing compilation and replication attributes into +unified attributes. For example, `_tpu_replicate="cluster"` in the +following code + +```mlir +%control = tf_executor.island wraps "tf.TPUReplicateMetadata"() {_tpu_replicate = "cluster", allow_soft_placement = false, computation_shape = [], device = "", device_assignment = [], host_compute_core = [], name = "TPUReplicateMetadata", num_cores_per_replica = 1 : i64, num_replicas = 1 : i64, step_marker_location = "STEP_MARK_AT_ENTRY", topology = "", use_tpu = true, use_spmd_for_xla_partitioning = false} : () -> () +``` + +wll be replaced by `_replication_info="cluster"` and `_xla_compile_device_type="TPU"`. + +```mlir +%control = tf_executor.island wraps "tf.TPUReplicateMetadata"() {_replication_info = "cluster", _xla_compile_device_type = "TPU", allow_soft_placement = false, computation_shape = [], device = "", device_assignment = [], host_compute_core = [], name = "TPUReplicateMetadata", num_cores_per_replica = 1 : i64, num_replicas = 1 : i64, step_marker_location = "STEP_MARK_AT_ENTRY", topology = "", use_spmd_for_xla_partitioning = false, use_tpu = true} : () -> () +``` + +`_XlaMustCompile=true` in the following code + +```mlir +%outputs_67, %control_68 = tf_executor.island wraps "tf.PartitionedCall"(%arg0, %outputs_0) {_XlaMustCompile = true, _collective_manager_ids = [], _read_only_resource_inputs = [], config = "", config_proto = "\0A\07\0A\03CPU\10\01\0A\07\0A\03GPU\10\00\0A\07\0A\03TPU\10\02\0A\0E\0A\0ATPU_SYSTEM\10\012\02J\008\01\82\01\05h\01\88\01\01", device = "", executor_type = "", f = @__inference__jit_compiled_convolution_op_1510} : (tensor<4x32x32x8xf32>, tensor<*xf32>) -> tensor<*xf32> +``` + +will be replaced by `_xla_compile_device_type`, with its value set to the value of `device`. + +```mlir +%outputs_67, %control_68 = tf_executor.island wraps "tf.PartitionedCall"(%arg0, %outputs_0) {_collective_manager_ids = [], _read_only_resource_inputs = [], _xla_compile_device_type = "", config = "", config_proto = "\0A\07\0A\03CPU\10\01\0A\07\0A\03GPU\10\00\0A\07\0A\03TPU\10\02\0A\0E\0A\0ATPU_SYSTEM\10\012\02J\008\01\82\01\05h\01\88\01\01", device = "", executor_type = "", f = @__inference__jit_compiled_convolution_op_1510} : (tensor<4x32x32x8xf32>, tensor<*xf32>) -> tensor<*xf32> +``` +### `-tf-convert-to-legacy-compile-and-replicate-attributes` + +_Convert unified compilation and replication attributes back to legacy attributes._ + +This transformation pass converts unified compilation and replication +attributes (`_replication_info` and `_xla_compile_device_type`) into legacy +attributes. This ensures the unified attributes do not get exposed outside +of the MLIR bridge with V1 pipeline in some cases. The pass expects to have +either none or both of the unified attributes present in an op for the +conversion to happen. Otherwise it will fail. + +For example, `_replication_info="cluster"` and +`_xla_compile_device_type="TPU"` in the following code + +```mlir +%control = tf_executor.island wraps "tf.TPUReplicateMetadata"() {_replication_info = "cluster", _xla_compile_device_type = "TPU", allow_soft_placement = false, computation_shape = [], device = "", device_assignment = [], host_compute_core = [], name = "TPUReplicateMetadata", num_cores_per_replica = 1 : i64, num_replicas = 1 : i64, step_marker_location = "STEP_MARK_AT_ENTRY", topology = "", use_spmd_for_xla_partitioning = false, use_tpu = true} : () -> () +``` + +wll be replaced by `_tpu_replicate="cluster"` as follows, + + +```mlir +%control = tf_executor.island wraps "tf.TPUReplicateMetadata"() {_tpu_replicate = "cluster", allow_soft_placement = false, computation_shape = [], device = "", device_assignment = [], host_compute_core = [], name = "TPUReplicateMetadata", num_cores_per_replica = 1 : i64, num_replicas = 1 : i64, step_marker_location = "STEP_MARK_AT_ENTRY", topology = "", use_tpu = true, use_spmd_for_xla_partitioning = false} : () -> () +``` +### `-tf-data-optimization` + +_Performs tf.data optimizations_ + +### `-tf-decompose-reduce-dataset` + +_Decomposes ReduceDataset op into dataset operations._ + + Decomposes ReduceDataset op into a while loop that iterates the dataset and calls + into the reduction function. This decomposition is only done if the + ReduceDataset op is marked for compilation with the _xla_compile_device_type + attribute. + + For example, for the following function the ReduceDataset op: + + ```mlir + func.func @single_state_single_dataset_type_no_arguments( + %arg0: tensor, + %arg1: tensor + ) { + %1 = "tf.ReduceDataset"(%arg0, %arg1) { + Targuments = [], + Tstate = [i64], device = "", + f = @__reduce_func_1, f._tf_data_function = true, + output_shapes = [#tf_type.shape<>], + output_types = [i64], use_inter_op_parallelism = true, _xla_compile_device_type="TPU"} : + (tensor, tensor) -> (tensor) + func.return + } + ``` + + with the following reduction function: + + ```mlir + func.func private @__reduce_func_1(%arg0: tensor {tf._user_specified_name = "args_0"}, + %arg1: tensor<32xf32> {tf._user_specified_name = "args_1"}) -> (tensor) + attributes {tf._tf_data_function = true, tf.signature.is_stateful} { + %0 = "tf.JustPretend"(%arg1) : (tensor<32xf32>) -> (tensor) + func.return %0 : tensor + } + ``` + + will be transformed into: + + ```mlir + func.func @single_state_single_dataset_type_no_arguments(%arg0: tensor, %arg1: tensor) { + %0 = "tf.AnonymousIteratorV3"() {output_shapes = [#tf_type.shape<32>], output_types = [f32]} : () -> tensor + "tf.MakeIterator"(%arg0, %0) : (tensor, tensor) -> () + %cst = "tf.Const"() {value = dense : tensor} : () -> tensor + %1:2 = "tf.WhileRegion"(%cst, %arg1) ({ + ^bb0(%arg2: tensor, %arg3: tensor): + "tf.Yield"(%arg2) : (tensor) -> () + }, { + ^bb0(%arg2: tensor, %arg3: tensor): + %2 = "tf.IteratorGetNextAsOptional"(%0) {output_shapes = [#tf_type.shape<32>], output_types = [f32]} : (tensor) -> tensor + %3 = "tf.OptionalHasValue"(%2) : (tensor) -> tensor + %4 = "tf.IfRegion"(%3) ({ + %5 = "tf.OptionalGetValue"(%2) : (tensor) -> tensor<32xf32> + %6 = func.call @__reduce_func_1(%arg3, %5) {_xla_compile_device_type = "TPU"} : (tensor, tensor<32xf32>) -> tensor + "tf.Yield"(%6) : (tensor) -> () + }, { + "tf.Yield"(%arg3) : (tensor) -> () + }) {_lower_using_switch_merge = true, is_stateless = false} : (tensor) -> tensor + "tf.Yield"(%3, %4) : (tensor, tensor) -> () + }) {_lower_using_switch_merge = true, is_stateless = false, parallel_iterations = 10 : i64} : (tensor, tensor) -> (tensor, tensor) + return +} +``` +### `-tf-device-assignment-by-func-attr` + +_Device assignment in TF dialect using the device specified in the function attribute._ + +### `-tf-device-cluster-formation` + +_Form clusters from instructions assigned to same device_ + +Clusters operations with the same device assignment id. For each +cluster, creates a "tf_device.device_launch" op with a Region containing the +ops in each cluster and replaces the ops with the new launch op. + +For example, given the following program: + +```mlir + %2 = "tf.A"(%arg0) : (tensor) -> tensor + %3 = "tf.B"(%2) {device = "tpu0"} : (tensor) -> tensor + %4 = "tf.C"(%2, %3) {device = "tpu0"} : (tensor, tensor) -> tensor + %5 = "tf.D"(%4) : (tensor) -> tensor +``` + +After the pass, we will have: + +```mlir + %0 = "tf.A"(%arg0) : (tensor) -> tensor + %1 = "tf_device.launch"() ( { + %3 = "tf.B"(%0) : (tensor) -> tensor + %4 = "tf.C"(%0, %3) : (tensor, tensor) -> tensor + tf_device.return %4 : tensor + }) {device = "tpu0"} : () -> tensor + %2 = "tf.D"(%1) : (tensor) -> tensor + return %2 : tensor +``` +### `-tf-device-cluster-outlining` + +_Outlines regions of tf_device.cluster operations_ + +This pass outlines the body of a `tf_device.cluster` into a function and +replaces the `tf_device.cluster` op with an equivalent `tf_device.cluster_func` +op. Implicit operands will be captured and materialized as explicit arguments to +the newly created functions and associated `tf_device.cluster_func` ops. + +For example, the following: + +```mlir +func @computation(%arg0: tensor) -> tensor { + %cluster = "tf_device.cluster"() ( { + %identity = "tf.Identity"(%arg0) : (tensor) -> tensor + tf_device.return %identity : tensor + }) : () -> (tensor) + return %cluster : tensor +} +``` + +will be transformed into: + +```mlir +func @computation(%arg0: tensor) -> tensor { + %cluster = "tf_device.cluster_func"(%arg0) {func = @_func} : (tensor) -> tensor + return %cluster : tensor +} + +func @_func(%arg0: tensor) -> tensor { + %identity = "tf.Identity"(%arg0) : (tensor) -> tensor + return %identity : tensor +} +``` +### `-tf-device-constant-sinking` + +_Sinks constants implicitly captured in a tf_device.cluster region._ + +This pass sinks implicitly captured constants (`tf.Const` ops) used by and into +a `tf_device.cluster` region. Performing this prior to outlining will reduce the +number of arguments of the outlined function. + +For example, the following: + +```mlir +func @cluster() -> tensor { + %const = "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %cluster = "tf_device.cluster"() ( { + %identity = "tf.Identity"(%const) : (tensor) -> tensor + tf_device.return %identity : tensor + }) : () -> (tensor) + return %cluster : tensor +} +``` + +will be transformed into: + +```mlir +func @cluster() -> tensor { + %cluster = "tf_device.cluster"() ( { + %const = "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %identity = "tf.Identity"(%const) : (tensor) -> tensor + tf_device.return %identity : tensor + }) : () -> (tensor) + return %cluster : tensor +} +``` +### `-tf-device-convert-launch-func-to-tf-call` + +_Rewrites tf_device::LaunchFuncOp to TF::PartitionedCallOp_ + +This pass converts tf_device::LaunchFuncOp into an equivalent +TF::PartitionedCallOp so that it can be exported to TensorFlow GraphDef. +### `-tf-device-index-selector` + +_Fold tf.DeviceIndex to constant._ + +### `-tf-device-launch-outlining` + +_Outlines regions of tf_device.launch operations_ + +This pass outlines the body of a `tf_device.launch` into a function and +replaces the `tf_device.launch` op with an equivalent `tf_device.launch_func` +op. Implicit operands will be captured and materialized as explicit arguments to +the newly created functions and associated `tf_device.launch_func` ops. The +`device` attribute from the `launch` op is transferred to `launch_func`. + +For example, the following: + +```mlir +func @computation(%arg0: tensor) -> tensor { + %launch = "tf_device.launch"() ( { + %identity = "tf.Identity"(%arg0) : (tensor) -> tensor + tf_device.return %identity : tensor + }) {device = "some_device"} : () -> (tensor) + return %launch : tensor +} +``` + +will be transformed into: + +```mlir +func @computation(%arg0: tensor) -> tensor { + %launch = "tf_device.launch_func"(%arg0) {device = "some_device", func = @_func} : (tensor) -> tensor + return %launch : tensor +} + +func @_func(%arg0: tensor) -> tensor { + %identity = "tf.Identity"(%arg0) : (tensor) -> tensor + return %identity : tensor +} +``` + +#### Options +``` +-globally-unique-func-names : If true, the pass adds extra identifiers to make function names globally unique within a process, not just within a module. +``` +### `-tf-device-mark-input-output-aliases` + +_Marks device cluster inputs-output pairs that read/write to the same variable as aliases_ + +This pass analyzes the inputs and outputs to device cluster and marks those +input-output pairs as aliases (using `tf.aliasing_output` attribute) which read +and write to the same resource. This aliasing information can then be propagated +to XLA compiler for input/output buffer space optimizations. +### `-tf-drop-while-shape-invariant` + +_Drop `shape_invariant` attribute from While/WhileRegion ops._ + +Drop `shape_invariant` attribute from tf.While and tf.WhileRegion op. This +would allow shape inference pass to further refine operand/result shapes of +these ops. This is only safe to do when compiling to XLA. +### `-tf-drop-while-shape-invariant-in-device-cluster` + +_Drop `shape_invariant` attribute from While/WhileRegion ops inside device cluster._ + +Drop `shape_invariant` attribute from tf.While and tf.WhileRegion op only +inside device cluster. This would allow shape inference pass to further +refine operand/result shapes of these ops. This is only safe to do when +compiling to XLA. +### `-tf-einsum` + +_Transform Einsum to other TF Ops for the supported variants_ + +### `-tf-embedding-pipelining` + +_Rewrite graph for embedding pipelining_ + +For architectures that support accelerated embedding lookups, this pass will +rewrite the graph to use pipelining for better device utilization. +### `-tf-embedding-program-key` + +_Sets the program key for embedding ops._ + + Passes in the program key to embedding ops. Will move the embedding ops + after a _TPUCompileMlir op if there is no predecessor _TPUCompileMlir op. + Both the embedding op and compile op are assumed to be wrapped in separate + tf_device.launch() ops. This is because the embedding op is head outside + compiled and the compile op is wrapped in launch to execute on host + during TPURewritePass. + + For example, the tf.OpA with the `mini_batch_splits` attribute will be + moved after _TPUCompileMlir and the first input will use the + _TPUCompileMlir program output: + + ```mlir + "tf_device.launch"() ({ + %cst_0 = "tf.Const"() {value = dense<""> : tensor<1x!tf_type.string>} : () -> tensor<1x!tf_type.string> + "tf.OpA"(%cst_0) { mini_batch_splits = ""} : (tensor<1x!tf_type.string>) -> () + tf_device.return +}) {device = "/job:localhost/replica:0/task:0/device:CPU:0"} : () -> () +%0:2 = "tf_device.launch"() ({ + %compilation_status, %program = "tf._TPUCompileMlir"() { metadata = "...", mlir_module = "..." } : () -> (tensor, tensor<3x!tf_type.string>) + tf_device.return %compilation_status, %program : tensor, tensor<3x!tf_type.string> +}) {device = "/job:localhost/replica:0/task:0/device:CPU:0"} : () -> (tensor, tensor<3x!tf_type.string>) +``` + +becomes: + +```mlir + %0:2 = "tf_device.launch"() ({ + %compilation_status, %program = "tf._TPUCompileMlir"() {metadata = "...", mlir_module = "..."} : () -> (tensor, tensor<3x!tf_type.string>) + tf_device.return %compilation_status, %program : tensor, tensor<3x!tf_type.string> + }) {device = "/job:localhost/replica:0/task:0/device:CPU:0"} : () -> (tensor, tensor<3x!tf_type.string>) + "tf_device.launch"() ({ + %cst = "tf.Const"() {value = dense<""> : tensor<1x!tf_type.string>} : () -> tensor<1x!tf_type.string> + "tf.OpA"(%0#1) {mini_batch_splits = ""} : (tensor<3x!tf_type.string>) -> () + tf_device.return + }) {device = "/job:localhost/replica:0/task:0/device:CPU:0"} : () -> () +``` +### `-tf-embedding-sequencing` + +_Rewrite graph for sequential execution of embeddings_ + +This is a strictly sequential and formally correct fallback option for the +embedding pipelining pass intended for debugging during pipelining +development. +### `-tf-executor-break-up-islands` + +_Transform from TF control dialect to TF executor dialect._ + +### `-tf-executor-check-control-dependencies` + +_Checks control dependencies_ + +This pass analyzes control dependencies between islands and warns about +dependencies that are not explainable by side effects of the involved ops. +More precisely, for every minimal unexplainable control dependency path +we emit op warnings for all involved ops. The pass does not report +intermediate dummy ops for grouping control dependencies (Identity, NoOp), +unless they are part of an unexplainable path between other ops. +This pass is useful to understand control dependency conservatism for a +given MLIR module. + +For example, the following function +```mlir +func.func @path_with_intermediate_ops( + %arg0: tensor>>, + %arg1: tensor>>, + %arg2: tensor) -> () { + tf_executor.graph { + %island1 = tf_executor.island wraps "tf.AssignVariableOp"(%arg0, %arg2) : (tensor>>, tensor) -> () + %island2 = tf_executor.island(%island1) wraps "tf.NoOp"() : () -> () + %island3 = tf_executor.island(%island2) wraps "tf.NoOp"() : () -> () + %island4 = tf_executor.island(%island3) wraps "tf.AssignVariableOp"(%arg1, %arg2) : (tensor>>, tensor) -> () + tf_executor.fetch + } + func.return +} +``` +produces the following warnings +```mlir + 6:45: warning: unexpected control dependency path: path 0, node 0 (source) + %island1 = tf_executor.island wraps "tf.AssignVariableOp"(%arg0, %arg2) : (tensor>>, tensor) -> () + ^ + 6:45: note: see current operation: %control = tf_executor.island wraps "tf.AssignVariableOp"(%arg0, %arg2) : (tensor>>, tensor) -> () + 7:55: warning: unexpected control dependency path: path 0, node 1 (intermediate) + %island2 = tf_executor.island(%island1) wraps "tf.NoOp"() : () -> () + ^ + 7:55: note: see current operation: %control_0 = tf_executor.island(%control) wraps "tf.NoOp"() : () -> () + 8:55: warning: unexpected control dependency path: path 0, node 2 (intermediate) + %island3 = tf_executor.island(%island2) wraps "tf.NoOp"() : () -> () + ^ + 8:55: note: see current operation: %control_1 = tf_executor.island(%control_0) wraps "tf.NoOp"() : () -> () + 9:55: warning: unexpected control dependency path: path 0, node 3 (target) + %island4 = tf_executor.island(%island3) wraps "tf.AssignVariableOp"(%arg1, %arg2) : (tensor>>, tensor) -> () + ^ + 9:55: note: see current operation: %control_2 = tf_executor.island(%control_1) wraps "tf.AssignVariableOp"(%arg1, %arg2) : (tensor>>, tensor) -> () +``` +because the first and last `AssignVariableOp`s access different resources +and therefore should be independent. Note that the `NoOp`s are considered +as intermediate ops for control dependency grouping. +### `-tf-executor-convert-control-to-data-outputs` + +_Chain control outputs of while loop body_ + +This pass converts the control outputs of a while loop body function to data +outputs. Thus, inter iteration control dependencies are transformed to +data dependencies. Since data dependencies can express which particular +operations in the while loop body are dependent on which inputs, it captures +inter iteration parallelism in while loop. Control dependencies on the other +hand create a barrier at the end of while loop body thus blocking any +parallelism across iterations. + +For example, the following while loop body has a `%barrier` at the end. +Although there is no data/control dependency between `tf.AssignVariableOp` +for `%arg0` to `tf.AssignVariableOp` for `%arg1` across any iteration, the +while loop body has a control barrier (`%barrier`) at the end which forces +a dependency and the two assign variable ops must wait for each other to +complete before starting the next iteration. Transforming these control +outputs to data outputs removes the dependency between the two assign +variable ops, thus allowing them to run in parallel across iterations. + +Before: + +```mlir +!tf_res = type tensor>> +func @while_body(%arg0: !tf_res, %arg1: !tf_res, %arg2: tensor, %arg3: tensor) -> (!tf_res, !tf_res, tensor, tensor) { + %graph:4 = tf_executor.graph { + %assign_0_control = tf_executor.island wraps "tf.AssignVariableOp"(%arg0, %arg2) : (!tf_res, tensor) -> () + %assign_1_control = tf_executor.island wraps "tf.AssignVariableOp"(%arg1, %arg3) : (!tf_res, tensor) -> () + %add_out, %add_control = tf_executor.island wraps "tf.Add"(%arg2, %arg3) : (tensor, tensor) -> tensor + %mul_out, %mul_control = tf_executor.island wraps "tf.Mul"(%arg2, %arg3) : (tensor, tensor) -> tensor + %barrier = tf_executor.island(%assign_0_control, %assign_1_control, %add_control, %mul_control) wraps "tf.NoOp"() : () -> () + tf_executor.fetch %arg0, %arg1, %add_out, %mul_out, %barrier : !tf_res, !tf_res, tensor, tensor, !tf_executor.control + } + return %graph#0, %graph#1, %graph#2, %graph#3 : !tf_res, !tf_res, tensor, tensor +} +``` + +After: + +```mlir +func @while_body(%arg0: !tf_res, %arg1: !tf_res, %arg2: tensor, %arg3: tensor, %chain_0: tensor, %chain_1: tensor) -> (!tf_res, !tf_res, tensor, tensor, tensor, tensor) { + %graph:6 = tf_executor.graph { + %_, %chain_0_src = tf_executor.island wraps "tf.Identity"(%chain_0) : (tensor) -> tensor + %_, %chain_1_src = tf_executor.island wraps "tf.Identity"(%chain_1) : (tensor) -> tensor + %assign_0_control = tf_executor.island(%chain_0_src) wraps "tf.AssignVariableOp"(%arg0, %arg2) : (!tf_res, tensor) -> () + %assign_1_control = tf_executor.island(%chain_1_src) wraps "tf.AssignVariableOp"(%arg1, %arg3) : (!tf_res, tensor) -> () + %add_out, %add_control = tf_executor.island wraps "tf.Add"(%arg2, %arg3) : (tensor, tensor) -> tensor + %mul_out, %mul_control = tf_executor.island wraps "tf.Mul"(%arg2, %arg3) : (tensor, tensor) -> tensor + %chain_0_sink, %_ = tf_executor.island(%assign_0_control) wraps "tf.Identity"(%chain_0) : (tensor) -> tensor + %chain_1_sink, %_ = tf_executor.island(%assign_1_control) wraps "tf.Identity"(%chain_1) : (tensor) -> tensor + tf_executor.fetch %arg0, %arg1, %add_out, %mul_out, %chain_0_sink, %chain_1_sink : !tf_res, !tf_res, tensor, tensor, tensor, tensor + } + return %graph#0, %graph#1, %graph#2, %graph#3, %graph#4, %graph#5 : !tf_res, !tf_res, tensor, tensor, tensor, tensor +} +``` +### `-tf-executor-graph-pruning` + +_Prunes unreachable ops in a tf_executor.graph_ + +This pass removes ops from a `tf_executor.graph` that are not transitively, via +data or control dependencies, connected to the associated `tf_executor.fetch` +op. The order of ops will be preserved. Functions named `main` with no +`tf.entry_function` attribute will not be pruned, as such graphs/functions may +have been imported from a V1 TensorFlow graph, where feeds/fetches/targets are +not provided at certain stages of IR transformation (e.g. pre-placement). + +Option `ops-to-preserve` allows to specify ops that should not be pruned, +regardless of their reachability. + +For example, the following: + +```mlir +func @graph(%arg0: tensor, %arg1: tensor) -> tensor { + %graph = tf_executor.graph { + %transitive_reachable_data:2 = tf_executor.island wraps "tf.Identity"(%arg0) : (tensor) -> tensor + %reachable_data:2 = tf_executor.island wraps "tf.Identity"(%transitive_reachable_data#0) : (tensor) -> tensor + %unreachable_data:2 = tf_executor.island wraps "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %transitive_reachable_control = tf_executor.island wraps "tf.NoOp"() : () -> () + %reachable_control = tf_executor.island(%transitive_reachable_control) wraps "tf.NoOp"() : () -> () + %unreachable_control = tf_executor.island wraps "tf.NoOp"() : () -> tensor + tf_executor.fetch %reachable_data#0, %reachable_control : tensor, !tf_executor.control + } + return %graph : tensor +} +``` + +will be transformed into: + +```mlir +func @graph(%arg0: tensor, %arg1: tensor) -> tensor { + %graph = tf_executor.graph { + %transitive_reachable_data:2 = tf_executor.island wraps "tf.Identity"(%arg0) : (tensor) -> tensor + %reachable_data:2 = tf_executor.island wraps "tf.Identity"(%transitive_reachable_data#0) : (tensor) -> tensor + %transitive_reachable_control = tf_executor.island wraps "tf.NoOp"() : () -> () + %reachable_control = tf_executor.island(%transitive_reachable_control) wraps "tf.NoOp"() : () -> () + tf_executor.fetch %reachable_data#0, %reachable_control : tensor, !tf_executor.control + } + return %graph : tensor +} +``` + +#### Options +``` +-ops-to-preserve : Comma separated list of ops that should not be pruned regardless of reachability +``` +### `-tf-executor-island-coarsening` + +_Walks tf_executor::GraphOp and merges individual tf_executor::IslandOps._ + +This pass performs whole graph analysis for a graph encapsulated into tf_executor::GraphOp. +The analysis identifies all IslandOps within the graph which could be merged together. +The goal is to merge as many islands as possible. +Once analysis is completed, the pass merges all IslandOps in a single scan. + +For example given the following program with two disjunct islands: + +```mlir + func @test(%arg0 : tensor) -> tensor { + %0 = tf_executor.graph { + %1:2 = tf_executor.island { + %3 = "tf.opA"(%arg0) : (tensor) -> tensor + tf_executor.yield %3 : tensor + } + %2:2 = tf_executor.island(%1#1) { + %4 = "tf.opB"() : () -> tensor + tf_executor.yield %4 : tensor + } + tf_executor.fetch %2#0 : tensor + } + return %0 : tensor + } +``` + +After running this pass, the two islands are merged: + +```mlir + func @test(%arg0: tensor) -> tensor { + %0 = tf_executor.graph { + %outputs, %control = tf_executor.island { + %1 = "tf.opA"(%arg0) : (tensor) -> tensor + %2 = "tf.opB"() : () -> tensor + tf_executor.yield %2 : tensor + } + tf_executor.fetch %outputs : tensor + } + return %0 : tensor + } +``` +### `-tf-executor-split-into-island-per-op` + +_Transform from TF control dialect to TF executor dialect._ + +Splits an island with multiple ops into multiple islands (one per op). Does +not create any control dependencies between new islands, and does not +propagate control dependencies that potentially existed between the old +islands into the new islands. Maintains existing data dependencies between +ops wrapped by the new islands. + +Example: original program: + +```mlir + func.func @dangling_print(%arg0: tensor<*xi32>, %arg1: tensor) -> (tensor<*xi32>, tensor<*xi32>) { + %graph:2 = tf_executor.graph { + %island1:3 = tf_executor.island { + %add1 = "tf.Add"(%arg0, %arg1) : (tensor<*xi32>, tensor) -> tensor<*xi32> + %add2 = "tf.Add"(%add1, %arg1) : (tensor<*xi32>, tensor) -> tensor<*xi32> + %res = "tf.Print"(%add2) { message = "add result" } : (tensor<*xi32>) -> (tensor<*xi32>) + tf_executor.yield %add1, %add2 : tensor<*xi32>, tensor<*xi32> + } + tf_executor.fetch %island1#0, %island1#1 : tensor<*xi32>, tensor<*xi32> + } + func.return %graph#0, %graph#1 : tensor<*xi32>, tensor<*xi32> + } +``` + +will be converted by this pass into: + +```mlir + func.func @dangling_print(%arg0: tensor<*xi32>, %arg1: tensor) -> (tensor<*xi32>, tensor<*xi32>) { + %0:2 = tf_executor.graph { + %outputs, %control = tf_executor.island wraps "tf.Add"(%arg0, %arg1) : (tensor<*xi32>, tensor) -> tensor<*xi32> + %outputs_0, %control_1 = tf_executor.island wraps "tf.Add"(%outputs, %arg1) : (tensor<*xi32>, tensor) -> tensor<*xi32> + %outputs_2, %control_3 = tf_executor.island wraps "tf.Print"(%outputs_0) {message = "add result"} : (tensor<*xi32>) -> tensor<*xi32> + tf_executor.fetch %outputs, %outputs_0 : tensor<*xi32>, tensor<*xi32> + } + return %0#0, %0#1 : tensor<*xi32>, tensor<*xi32> + } +``` +### `-tf-executor-to-functional-conversion` + +_Lifts tf_executor.island inner ops from a tf_executor.graph_ + +This pass converts tf_executor.graphs consisting of only tf_executor.islands and +a tf_executor.fetch into a sea of nodes consisting of TensorFlow Dialect ops by +lifting such ops out of a tf_executor.graph's tf_executor.islands. If V1 control +flow ops are present in a tf_executor.graph, an error will be returned. + +For example, the following: + +```mlir +func @my_fn(%arg0: tensor, %arg1: tensor) -> (tensor, tensor) { + %graph_results:2 = tf_executor.graph { + %island_0_result, %island_0_control = tf_executor.island { + %identity = "tf.Identity"(%arg0) : (tensor) -> tensor + tf_executor.yield %identity : tensor + } + %island_1_result, %island_1_control = tf_executor.island { + %identity_n:2 = "tf.IdentityN"(%arg1, %island_0_result) : (tensor, tensor) -> (tensor, tensor) + tf_executor.yield %identity_n#0 + } + tf_executor.fetch %island_0_result, %island_1_result : tensor, tensor + } + return %graph_results#0, %graph_results#1 : tensor, tensor +} +``` + +will be transformed into: + +```mlir +func @my_fn(%arg0: tensor, %arg1: tensor) -> (tensor, tensor) { + %identity = "tf.Identity"(%arg0) : (tensor) -> tensor + %identity_n:2 = "tf.IdentityN"(%arg1, %identity) : (tensor, tensor) -> (tensor, tensor) + return %identity, %identity_n#0 : tensor, tensor +} +``` +### `-tf-executor-tpu-v1-island-coarsening` + +_Merges TPU clusters IslandOps, intended for V1 compatibility mode_ + +This pass is a variant of ExecutorIslandCoarseningPass that is limited to +TPU-annotated operations and intended to preserve backward compatibility with +TFv1. +### `-tf-executor-tpu-v1-island-inlining` + +_Inline calls to the nested TPU module._ + +This pass inlines the islands calling into the nested module that was +outlined, thus reversing the effect of the +`-tf-executor-tpu-v1-island-outlining` pass. + +For example, the following: +```mlir +module { + func @foo(%arg0: tensor) -> tensor { + %0 = tf_executor.graph { + %outputs, %control = tf_executor.island wraps "tf.PartitionedCall"(%arg0) {f = @_tpu_v1_compat_outlined::@bar} : (tensor) -> tensor + tf_executor.fetch %outputs : tensor + } + return %0 : tensor + } + module @_tpu_v1_compat_outlined { + func nested @bar(%arg0: tensor) -> tensor { + %0 = "tf.opA"(%arg0) : (tensor) -> tensor + return %0 : tensor + } + } +} +``` + +will be transformed into: + +```mlir +module { + func @foo(%arg0: tensor) -> tensor { + %0 = tf_executor.graph { + %outputs, %control = tf_executor.island { + %1 = "tf.opA"(%arg0) : (tensor) -> tensor + tf_executor.yield %1 : tensor + } + tf_executor.fetch %outputs : tensor + } + return %0 : tensor + } +} +``` +### `-tf-executor-tpu-v1-island-outlining` + +_Outline TPU clusters from island into a nested module, so it can be processed like a V2 module, intended for V1 compatibility mode_ + +Extract the islands containing a TPU cluster computation into an outlined +function in a nested module. This will allow to run the usual bridge on this +nested module which now exhibits a more friendly "V2-like" structure. +This is only intended for V1 compatibility mode where the bridge runs without +feed/fetches on session create/extend. + +So given e.g. + +```mlir + func @test() -> tensor { + %0 = tf_executor.graph { + %output, %control = tf_executor.island { + ... + tf_executor.yield %result : tensor + } + tf_executor.fetch %output : tensor + } + return %0 + } +``` + +This pass will create an additional function containing the code in +tf_executor.island: + +```mlir + func nested @_tpu_v1_compat_outlined_func0() -> tensor { + ... + } +``` + +and will then replace the island with the wrapped call: + +```mlir + func @test() -> tensor { + %0 = tf_executor.graph { + %outputs, %control = tf_executor.island wraps "tf.PartitionedCall"() { + f = @_tpu_v1_compat_outlined::@_tpu_v1_compat_outlined_func0 + } : () -> tensor + tf_executor.fetch %outputs : tensor + } + return %0 : tensor + } +``` +### `-tf-executor-update-control-dependencies` + +_Computes and applies all necessary control dependencies based on side effect analysis._ + +This pass is intended to run after the split_into_island_per_op +pass. That pass splits up multi-op islands into multiple individual islands +wrapping a single op without applying any control deps between the new +islands. So, this pass is needed in order to make preservation of the +semantic ordering relationships between ops as determined by side effect +analysis explicit in the IR. + +Example: original program: + +```mlir + func.func @example(%arg0: tensor<*x!tf_type.resource>>, %arg1: tensor<32xf32>) -> (tensor<32xf32>) { + %graph = tf_executor.graph { + %read0, %read0_control = tf_executor.island wraps "tf.ReadVariableOp"(%arg0) : (tensor<*x!tf_type.resource>>) -> tensor<32xf32> + %assign0_control = tf_executor.island wraps "tf.AssignVariableOp"(%arg0, %arg1) : (tensor<*x!tf_type.resource>>, tensor<32xf32>) -> () + %read1, %read1_control = tf_executor.island wraps "tf.ReadVariableOp"(%arg0) : (tensor<*x!tf_type.resource>>) -> tensor<32xf32> + %print, %print_control = tf_executor.island wraps "tf.Print"(%read1) { message = "read1 value" } : (tensor<32xf32>) -> (tensor<32xf32>) + tf_executor.fetch %read1#0 : tensor<32xf32> + } + func.return %graph : tensor<32xf32> + } +``` + +will be converted by this pass into: + +```mlir + func.func @example(%arg0: tensor<*x!tf_type.resource>>, %arg1: tensor<32xf32>) -> tensor<32xf32> { + %0 = tf_executor.graph { + %read0, %read0_control = tf_executor.island wraps "tf.ReadVariableOp"(%arg0) : (tensor<*x!tf_type.resource>>) -> tensor<32xf32> + %assign0_control = tf_executor.island(%read0_control) wraps "tf.AssignVariableOp"(%arg0, %arg1) : (tensor<*x!tf_type.resource>>, tensor<32xf32>) -> () + %read1, %read1_control = tf_executor.island(%assign0_control) wraps "tf.ReadVariableOp"(%arg0) : (tensor<*x!tf_type.resource>>) -> tensor<32xf32> + %print, %print_control = tf_executor.island(%read1_control) wraps "tf.Print"(%read1) {message = "read1 value"} : (tensor<32xf32>) -> tensor<32xf32> + tf_executor.fetch %read1, %print_control : tensor<32xf32>, !tf_executor.control + } + return %0 : tensor<32xf32> + } +``` +### `-tf-extract-head-tail-outside-compilation` + +_Extracts head or tail outside compilation to separate host launches before/after device cluster._ + +This pass extracts a CPU computation cluster with `_xla_outside_compilation` +annotation from the head or tail of a Device cluster. + +For example: + +```mlir + %cluster = "tf_device.cluster"() ( { + %a = "tf.A"(%arg0) {_xla_outside_compilation = "cluster1"} : (tensor) -> tensor + %b = "tf.B"(%a) : (tensor) -> tensor + %c = "tf.C"(%b) {_xla_outside_compilation = "cluster1"} : (tensor) -> tensor + tf_device.return %c : tensor + }) {num_cores_per_replica = 1, step_marker_location = "", padding_map = [], topology = "", device_assignment = []} : () -> tensor + return %cluster : tensor +``` + +becomes: + +```mlir +%0 = "tf_device.launch"() ( { + %3 = "tf.A"(%arg0) : (tensor) -> tensor + tf_device.return %3 : tensor +}) {device = "/job:worker/replica:0/task:0/device:CPU:0"} : () -> tensor +%1 = "tf_device.cluster"() ( { + %3 = "tf.B"(%0) : (tensor) -> tensor + tf_device.return %3 : tensor +}) {device_assignment = [], num_cores_per_replica = 1 : i64, padding_map = [], step_marker_location = "", topology = ""} : () -> tensor +%2 = "tf_device.launch"() ( { + %3 = "tf.C"(%1) : (tensor) -> tensor + tf_device.return %3 : tensor +}) {device = "/job:worker/replica:0/task:0/device:CPU:0"} : () -> tensor +return %2 : tensor + +``` +### `-tf-extract-outside-compilation` + +_Extracts device outside compilation computation to a separate tf_device.parallel_execute region._ + +This pass extracts a CPU computation cluster with `_xla_outside_compilation` +annotation, which denotes ops that should be run on CPU/host, from a device cluster. +Each outside compilation cluster is moved to +a tf_device.parallel_execute region. The device cluster is also moved to a +tf_device.parallel_execute region. Communication ops between device and host are +added to pass inputs/outputs to/from the outside compiled region. + +For example, the following tf_device.cluster with an op marked for `xla_outside_compilation`: + +```mlir +func @outside_compilation() -> tensor { + %0 = "tf_device.cluster"() ( { + %1 = "tf.Const"() {_xla_outside_compilation = "0", value = dense<1.0> : tensor} : () -> (tensor) + %2 = "tf.Identity"(%1) {_xla_outside_compilation = "0"} : (tensor) -> (tensor) + %3 = "tf.AddV2"(%1, %2) : (tensor, tensor) -> (tensor) + tf_device.return %3 : tensor + }) {num_cores_per_replica = 1, topology = "", device_assignment = []} : () -> tensor + return %0 : tensor +} +``` + +will become a tf_device.parallel_execute op with a CPU/host region and +a tf_device.cluster with communication ops to send data to/from device/host: + +```mlir +func @outside_compilation() -> tensor { + %0 = "tf_device.parallel_execute"() ( { + "tf_device.launch"() ( { + %1 = "tf._XlaCompileMlirPlaceholderProgramKey"() : () -> tensor<3x!tf_type.string> + %2 = "tf._XlaRecvAtHost"(%1) {device_ordinal = 0 : i64, key = "host_compute_channel_0_0_args"} : (tensor<3x!tf_type.string>) -> tensor + %3 = "tf.Identity"(%2) : (tensor) -> tensor + "tf._XlaSendFromHost"(%3, %1) {device_ordinal = 0 : i64, key = "host_compute_channel_0_0_retvals"} : (tensor, tensor<3x!tf_type.string>) -> () + tf_device.return + }) {device = "/job:worker/replica:0/task:0/device:CPU:0"} : () -> () + tf_device.return + }, { + %1 = "tf_device.cluster"() ( { + %2 = "tf.Const"() {value = dense<1.000000e+00> : tensor} : () -> tensor + %3 = "tf._XlaHostComputeMlir"(%2) {recv_key = "host_compute_channel_0_0_retvals", send_key = "host_compute_channel_0_0_args", tpu_core = 0 : i64} : (tensor) -> tensor + %4 = "tf.AddV2"(%2, %3) : (tensor, tensor) -> tensor + tf_device.return %4 : tensor + }) {device_assignment = [], num_cores_per_replica = 1 : i64, topology = ""} : () -> tensor + tf_device.return %1 : tensor + }) : () -> tensor + return %0 : tensor +} +``` +### `-tf-extract-tpu-copy-with-dynamic-shape-op` + +_Extract the TPUCopyWithDynamicShapeOp out of the host launch and place it on device launch_ + +This pass looks for TPUCopyWithDynamicShapeOp which wraps in a +`tf_device.launch` with host device attribute. It extracts the ops and wrap +them in `tf_device.launch` with tpu device attribute so that ops can be +run on TPU instead of CPU while still being compiled on host. +### `-tf-functional-control-flow-to-cfg` + +_Transform functional control flow Ops to MLIR Control Form Graph (CFG) form_ + +### `-tf-functional-control-flow-to-regions` + +_Transforms functional control flow operations to their region-based counterparts_ + +This pass transforms functional control flow operations in the TensorFlow +dialect to their region-based counterparts, i.e., `tf.If` is transformed to +`tf.IfRegion` and `tf.While` is transformed to `tf.WhileRegion`. + +For example, this functional operation + +```mlir + %0 = "tf.If"(%arg0, %arg1) { + then_branch = @then_branch_func, else_branch = @else_branch_func, is_stateless = false + } : (tensor, tensor<*xf32>) -> tensor<*xf32> +``` + +will be transformed into this region-based operation + +```mlir + %0 = "tf.IfRegion"(%arg0) ( { + %1 = call @then_branch_func(%arg1) : (tensor<*xf32>) -> tensor<*xf32> + "tf.Yield"(%1) : (tensor<*xf32>) -> () + }, { + %1 = call @else_branch_func(%arg1) : (tensor<*xf32>) -> tensor<*xf32> + "tf.Yield"(%1) : (tensor<*xf32>) -> () + }) {is_stateless = false} : (tensor) -> tensor<*xf32> +``` +### `-tf-functional-to-executor-conversion` + +_Transform from func op to TF executor dialect._ + +### `-tf-fused-kernel-matcher` + +_Matches computations corresponding to optimized fused kernels_ + +### `-tf-gpu-op-fusion` + +_Fusion optimization for GPU targets_ + +This pass is performing fusion specific to GPU targets. This is an ad-hoc +pass for now, but should be integrated with some notion of "target" in the +MLIR pipeline in the future. +### `-tf-group-by-dialect` + +_Groups ops into functions that only contain one dialect._ + +Factors operations into subroutines such that all functions only +contain a single dialect. Which of the dialects are allowed in the +"top" function is configurable. + +For example, the code + x.a() + x.b() + %c = y.c() + x.d(%c) +would be transformed into something like + call @x_1() + %c = call @y_1() + call @x_2(%c) +with @x_1, @x_2 and @y_1 filled in. +### `-tf-guarantee-all-funcs-one-use` + +_Guarantee all FuncOp's have only a single use._ + +### `-tf-hoist-loop-invariant` + +_Hoists loop invariant ops to the outside of the loop_ + + Hoists loop invariant to the outside of the loop. The pass is similar to + LoopInvariantCodeMotion pass, but it also hoists ReadVariableOps, + if the variable is read only. + + For example, the following pseudo MLIR code (types are left out for + brevity) + ```mlir + func.func @hoist_loop_invariant(%arg0, %arg1) { +%var = "tf.VarHandleOp"() {container="", shared_name="var_name", device = "/device:CPU:0"} + %results:2 = "tf.WhileRegion"(%arg0, %arg1) ({ + ^bb0(%arg2, %arg3): + %0 = "tf.OpA"() {is_stateless = true} + "tf.Yield"(%0) + }, { + ^bb0(%arg2, %arg3): + %1 = "tf.ReadVariableOp"(%var) + %2 = "tf.OpB"(%1) {is_stateless = true} + %3 = "tf.OpC"(%arg2, %2) {is_stateless = true} + %4 = "tf.OpD"(%arg3, %2) {is_stateless = true} + "tf.Yield"(%3, %4) + }) {is_stateless = true} + return %results#0, %results#1 + } + ``` + would be transformed to + ```mlir + func.func @hoist_loop_invariant(%arg0, %arg1) { +%var = "tf.VarHandleOp"() {container="", shared_name="var_name", device = "/device:CPU:0"} +%1 = "tf.ReadVariableOp"(%var) + %2 = "tf.OpB"(%1) {is_stateless = true} + %results:2 = "tf.WhileRegion"(%arg0, %arg1) ({ + ^bb0(%arg2, %arg3): + %0 = "tf.OpA"() {is_stateless = true} + "tf.Yield"(%0) + }, { + ^bb0(%arg2, %arg3): + %3 = "tf.OpC"(%arg2, %2) {is_stateless = true} + %4 = "tf.OpD"(%arg3, %2) {is_stateless = true} + "tf.Yield"(%3, %4) + }) {is_stateless = true} + return %results#0, %results#1 + } + ``` + The `tf.ReadVariableOp` and `tf.OpB` can be hoisted to the outside of + the loop. + +### `-tf-hoist-replicate-invariant-resource-writes` + +_Hoists writes to replicate invariant resource variables._ + +This pass hoists replicate invariant resource variable writes outside +tf_device.replicate op. These may have been inserted by other passes such as +resource op lifting. However, if the resource variable is not replicated, writes +to such variables for each replica are redundant and can be replaced by writing +a single value from first replica. + +The benefit of this optimization is reduced memory requirement on host. For +multiple writes (one from each replica) to such variables, the host would +allocate buffer space to receive the device output from all replicas, which is +not required. We can use the output of first replica in such cases. +### `-tf-init-text-file-to-import` + +_Convert InitializeTableFromTextFileV2 ops to LookupTableImportV2Op to remove the dependency on asset files_ + + +#### Options +``` +-tf-saved-model-dir : Directory containing the model exported as a TensorFlow SavedModel. If your model is not based on the TensorFlow SavedModel, use an empty value. +``` +### `-tf-layout-assignment` + +_Layout assignment pass._ + + +#### Options +``` +-force-data-format : Force data format for all layout sensitive ops. +``` +### `-tf-localize-var-handles` + +_Creates VarHandleOps next to the operations that use them._ + +Creates VarHandleOps right next to the operations that use them, one +per operation. +This is useful for transformations that only end up with a few small +snippets of remaining TF code, and wish for those snippets to be +self-contained. +For example, this would transform + +"tf_saved_model.global_tensor"() { sym_name = "v" ... } +func @f(%arg0 {tf_saved_model.bound_input = @v}) { + %1 = "tf.ReadVariableOp"(%arg0) + ... +} + +to + +func @f(%arg0 {tf_saved_model.bound_input = @v}) { + %0 = "tf.VarHandleOp"(sym_name = "v") + %1 = "tf.ReadVariableOp"(%0) + ... +} + +Note that this pass might leave behind unused values +(like e.g. %arg0 in the example above), which can later be +pruned using DCE. +### `-tf-lower-quantized` + +_Lowers ops that require quantized input or output._ + +This pass rewrites all ops that have at least one input or output that must +be a quantized type to ops whose inputs and outputs allow non-quantized +types. Examples of quantized types are TF_Qint8 or TF_Quint8. + +An example is TF_DequantizeOp, which converts a quantized type to a float. +This op is rewritten to generic ops that perform the scale and shift +and can operate on non-quantized types. + +Currently, TF_DequantizeOp is the only op with a lowering that falls +in this category. When more lowerings are added (e.g. QuantizeV2Op), +they should be added to this pass. +### `-tf-mark-ops-for-outside-compilation` + +_Marks ops in device cluster for outside compilation if they are unsupported on device._ + +This pass marks unsupported ops in a device cluster with +`_xla_outside_compilation` attribute so the operations will run on the host +instead of the device. Unsupported ops are ops that can not be code +generated to run on the device for the cluster including: + +1. String operations on TPUs. +2. Operations that don't have a kernel defined for the device. + +This pass is conservative in that it will mark all ops for outside compilation +that can not be compiled for the device. Exceptions for this are added for ops +that will be rewritten or decomposed before compiling on device. + + +For example, tf_device.cluster op with an unsupported op, tf.UnsupportedOp: + +```mlir +func @unsupported_op() -> tensor { + %0 = "tf_device.cluster"() ( { + %1 = "tf.UnsupportedOp"() : () -> tensor + %2 = "tf.Identity"(%1) : (tensor) -> tensor + tf_device.return %2 : tensor + }) {allow_soft_placement = true, num_cores_per_replica = 1, topology = "", device_assignment = []} : () -> tensor + return %0 : tensor +} +``` + +will mark tf.UnsupportedOp with `_xla_outside_compilation` attribute: + +```mlir +func @unsupported_op() -> tensor { + %0 = "tf_device.cluster"() ( { + %1 = "tf.UnsupportedOp"() {_xla_outside_compilation = "auto0"} : () -> tensor + %2 = "tf.Identity"(%1) : (tensor) -> tensor + tf_device.return %2 : tensor + }) {allow_soft_placement = true, device_assignment = [], num_cores_per_replica = 1 : i64, topology = ""} : () -> tensor + return %0 : tensor +} +``` +### `-tf-materialize-passthrough-op` + +_Materialize the MlirPassthroughOp by replacing it with the MLIR module attached as an attribute_ + +A pass that replaces MlirPassthrough ops with the code they have in +their `mlir_module` string attribute. +### `-tf-merge-control-flow` + +_Merges IfRegion ops together with a common predicate._ + +This pass merges IfRegion ops together if they have the same predicate and it +is safe to do so (there are no intermediate dependencies, they are in the +same block, etc). + +For example: + +```mlir +"tf.IfRegion"(%0) ( { + %2 = "tf.A"() : () -> (tensor) + "tf.Yield"() : () -> () + }, { + "tf.Yield"() : () -> () + }) { is_stateless = true } : (tensor) -> () +"tf.IfRegion"(%0) ( { + %2 = "tf.B"() : () -> (tensor) + "tf.Yield"() : () -> () + }, { + "tf.Yield"() : () -> () + }) { is_stateless = true } : (tensor) -> () +``` + +Would be transformed to: + +```mlir +"tf.IfRegion"(%0) ( { + %2 = "tf.A"() : () -> (tensor) + %3 = "tf.B"() : () -> (tensor) + "tf.Yield"() : () -> () + }, { + "tf.Yield"() : () -> () + }) { is_stateless = true } : (tensor) -> () +``` +### `-tf-move-transposes` + +_Move transposes pass._ + + +#### Options +``` +-fold-transpose-in-ops : Whether to fold transposes in ops which can support folding. +-direction : Move transposes to the beginning or the end of the block where they are defined. +``` +### `-tf-name-anonymous-iterators` + +_Converts anonymous iterators to named iterators_ + +This converts AnonymousIterator ops to Iterator, thus giving them a name. +For example, this will convert + %0 = "tf.AnonymousIteratorV3"() {...} +to + %0 = "tf.Iterator"() {shared_name = "_iterator1", ...} +### `-tf-optimize` + +_Optimize TensorFlow module_ + +### `-tf-order-by-dialect` + +_Reorders ops so ops of the same dialect are next to each other._ + +Performs a reordering of ops so that + (a) ops of the same dialect are next to each other + (b) order within a dialect is preserved +. +For example, this would transform + %a = "x.f"() + %b = "y.f"(%a) + %c = "x.f"(%a) +to + %a = "x.f"() + %c = "x.f"(%a) + %b = "y.f"(%a) +so that the two "x" dialect instructions are next to each other. +### `-tf-outside-compiled-to-host-launch` + +_Wraps each op with the _xla_outside_compiled attribute in a separate tf_device.launch on replicated host device._ + +This pass wraps ops with the same `_xla_outside_compilation` +attribute value in a tf_device.launch op with host device assignment. + +A simple example: + +```mlir + "tf_device.cluster"() ( { + "tf.A"() + "tf.B"() {_xla_outside_compilation = "cluster1"} + "tf.C"() + tf_device.return + }) {num_cores_per_replica = 1, topology = "", device_assignment = []} +``` + +Would become the following ops (unimportant attribute, type are omitted): + +```mlir + "tf_device.cluster"() ( { + "tf.A"() + "tf_device.launch"() { + "tf.B"() {_xla_outside_compilation = "cluster1"} + tf_device.return + } {device = "TPU_REPLICATED_HOST_0"} : () -> () + "tf.C"() + tf_device.return + }) {num_cores_per_replica = 1, topology = "", device_assignment = []} +``` +### `-tf-parallel-execute-to-islands` + +_Lowers device parallel_execute to executor islands_ + + +#### Options +``` +-legacy-graph-export : Determines whether or not this pass should execute logic that is reserved for the legacy graph export pipeline to maintain expected invariants. In the case of this pass, that means manually propagating controls to lifted parallel execute regions to the graph fetch to ensure the ops execute. +``` +### `-tf-promote-resources-to-args` + +_Promote resources reads/writes to function inputs/outputs._ + +This pass promotes resource accesses in function(s) (by default, the main) +to input arguments and outputs of the function(s). + +Two types of resources are supported: +(1) A function argument of TF::ResourceType type (this pass). +(2) A VarHandleOp in the function (tf-promote-var-handles-to-args). + +After the pass, + + . The function will have an input argument for each resource that is + already provided as an input argument or is read. The type of the input + argument will become the shape of the value represented by the resource. + + . The function will have an output for each resource that is written. The + type of the output will become the shape of the resource. + +The information of variable identification and input-output alising is +recorded as named attributes of the input argument or output: + + . 'tf.resource_name' matches 'shared_name' of VarHandleOp, which represents + the identifier of the corresponding resource. This attribute is added to + an input argument if the initial value of the resource is read, or to the + output if the initial value is not read. + + . 'tf.aliasing_output' is the index of the function output that is an alias + of the input argument. This attribute is added only to the input argument + when the initial value of the corresponding resource is read, and the + resource is written later. + +Assumption of this pass: + . Compound resource operations have already been decomposed. + . Dead functions have already been removed, as resource arguments in dead + functions can cause the pass to fail. + +#### Options +``` +-functions : Comma separated list of functions whose resources read/writes should be promoted to function inputs/outputs. +``` +### `-tf-promote-var-handles-to-args` + +_Promote tf.VarHandleOps to function arguments._ + +See joint description in promote resources to args.### `-tf-readonly-references-to-resources` + +_Convert readonly reference variables to resource variables._ + +### `-tf-region-control-flow-to-functional` + +_Transforms region-based control flow operations to their functional counterparts_ + +This pass transforms region-based control flow operations in the TensorFlow +dialect to their functional counterparts, i.e., `tf.IfRegion` is transformed to +`tf.If` and `tf.WhileRegion` is transformed to `tf.While`. + +For example, this region-based operation + +```mlir + %0 = "tf.IfRegion"(%arg0) ( { + %1 = call @then_branch_func(%arg1) : (tensor<*xf32>) -> tensor<*xf32> + "tf.Yield"(%1) : (tensor<*xf32>) -> () + }, { + %1 = call @else_branch_func(%arg1) : (tensor<*xf32>) -> tensor<*xf32> + "tf.Yield"(%1) : (tensor<*xf32>) -> () + }) {is_stateless = false} : (tensor) -> tensor<*xf32> +``` + +will be transformed into this functional operation + +```mlir + %0 = "tf.If"(%arg0, %arg1) { + then_branch = @then_branch_func, else_branch = @else_branch_func, is_stateless = false + } : (tensor, tensor<*xf32>) -> tensor<*xf32> +``` +### `-tf-remove-unused-arguments` + +_Removes unused args from private functions & their callers._ + +Removes arguments from functions that aren't used in the function +body, outside of returns. Also adjusts the callers of said functions. + +For example, the code + func.func @f(%arg0, %arg1) { + SomeOpThatUsesArg0(%arg0) + return %arg0 + } + ... + call @x_1(x, y) + +would be transformed into + func.func @f(%arg0) { + return %arg0 + } + ... + call @x_1(x) + +Note that, in the above example, both args would be removed if there +wasn't the "SomeOpThatUsesArg0(%arg0)" line. +### `-tf-remove-unused-while-results` + +_Removes unused results from tf.WhileRegion ops_ + +Removes unused results from `tf.WhileRegion` ops along with the defining +ops in the body, if it is safe to do so. +Currently, the pass detects results with following properties: +- the result is unused outside of the `tf.WhileRegion` op +- the defining op of the result in the body can be safely removed +- the operand corresponding to the result is not used by any other op in + the condition or body (in particular, there must not be intermediate + pass-through ops like `tf.Identity`) + + +For example, the following pseudo MLIR code (types are left out for +brevity) +```mlir + func.func @remove_first_result(%arg0, %arg1) { + %0:2 = "tf.WhileRegion"(%arg0, %arg1) ({ + ^bb0(%arg2, %arg3): + %1 = "tf.OpA"() {is_stateless = true} + "tf.Yield"(%1) + }, { + ^bb0(%arg2, %arg3): + %1 = "tf.OpB"(%arg2) {is_stateless = true} + %2 = "tf.OpC"(%arg3) {is_stateless = true} + "tf.Yield"(%1, %2) + }) {is_stateless = true} + return %0#1 + } +``` +would be transformed to +```mlir + func.func @remove_first_result(%arg0, %arg1) { + %0 = "tf.WhileRegion"(%arg1) ({ + ^bb0(%arg3): + %1 = "tf.OpA"() {is_stateless = true} + "tf.Yield"(%1) + }, { + ^bb0(%arg3): + %1 = "tf.OpC"(%arg3) {is_stateless = true} + "tf.Yield"(%1) + }) {is_stateless = true} + return %0 + } +``` +(the first result can be removed along with its defining op `tf.OpB`). + +### `-tf-replica-id-to-device-ordinal` + +_Set device ordinal with replica id_ + +This pass sets the device ordinal attribute of the ops using the replica id +attribute. This is run immediately after the replica_to_island pass which +sets the replica id attribute of these ops. Note for single chip usecase, +the pass will check if there is one op and sets the device ordinal attribute +to be zero. +### `-tf-replicate-invariant-op-hoisting` + +_Hoists replicate invariant operations out of replicate_ + +This pass looks for replicate invariant ops in a `tf_device.replicate` op +region and hoists them out. It also makes `tf.Shape` ops replicate invariant +if possible. This currently updates or replaces `tf.Shape` ops of replicated +arguments, either tensors or resources. + +The primary benefit of the pass is to hoist `num_replicas` `_TPUCompile`s +into a single `_TPUCompile`. + +This pass assumes that when a `tf.Shape` directly inputs from `replicate` +params, then it is the same shape across replicas. + +For example, the following + +```mlir +tf_device.replicate([%0, %1] as %ri: tensor<*xi32>) {n = 2 : i32} { + %2 = "tf.Shape"(%ri) : (tensor<*xi32>) -> tensor + tf_device.return +} +``` + +gets converted to + +```mlir +tf_device.replicate([%0, %1] as %ri: tensor<*xi32>) {n = 2 : i32} { + %2 = "tf.Shape"(%0) : (tensor<*xi32>) -> tensor + tf_device.return +} +``` + +and for resource variables the following + +```mlir +tf_device.replicate([%0, %1] as %ri: tensor<*x!tf_type.resource>) {n = 2 : i32} { + %2 = "tf.ReadVariableOp"(%ri) : tensor<*x!tf_type.resource> -> tensor<*xi32> + %3 = "tf.Shape"(%2) : (tensor<*xi32>) -> tensor + tf_device.return +} +``` + +gets converted to + +```mlir +tf_device.replicate([%0, %1] as %ri: tensor<*x!tf_type.resource>) {n = 2 : i32} { + %2 = "tf.ReadVariableOp"(%ri) : tensor<*x!tf_type.resource> -> tensor<*xi32> + %3 = "tf.VariableShape"(%0) : (tensor<*x!tf_type.resource>) -> tensor + tf_device.return +} +``` +### `-tf-replicate-tensor-list-init-ops` + +_Replicate TensorList init ops for correct shape assignments in shape inference_ + +If we pass same TensorList to a while op as multiple arguments or just use +the same TensorList at multiple places and assign different +TensorListSetItem to elements of TensorList, the shape inference is then +unable to identify the Shape of these args and thus the input TensorList +shape is unidentifiable. +All of these args are supposed to be independent and not related to original +creation of TensorList. + +This pass will create multiple instances of TensorList for each arg of the +while op and each use and thus there will be not a conflict in resolving the +shape of these different inputs. +### `-tf-replicate-to-island` + +_Lowers device replicate to executor islands_ + + +#### Options +``` +-legacy-graph-export : Determines whether or not this pass should execute logic that is reserved for the legacy graph export pipeline to maintain expected invariants. In the case of this pass, that means manually propagating controls to lifted parallel execute regions to the graph fetch to ensure the ops execute, as well as determining whether or not the islands created by this pass should be split after the replicated ops have been lifted. +``` +### `-tf-resource-device-inference` + +_Propagates the device attribute on resources from callers to callees._ + +A pass that propagates device assignment of resources on a module. It +performs in-function propagation, as well as cross-function propagation from +callers to callees. + +This pass changes the module by adding "tf.device" attribute to function +arguments and adding "device" attribute to TF ops. + +For example, given the function + +```mlir + !tf_res = type tensor<*x!tf_type.resource>> + + func @test(%arg0: !tf_res {tf.device = "/TPU:0"}) { + tf_executor.graph { + %control = tf_executor.island { + %id0 = "tf.Identity"(%arg0) : (!tf_res) -> !tf_res + tf_executor.yield + } + tf_executor.fetch %control : !tf_executor.control + } + return + } +``` + +Observe how the op inside the island obtains a `/TPU:0` device assignment: + +```mlir + !tf_res = type tensor<*x!tf_type.resource>> + + func @test(%arg0: !tf_res {tf.device = "/TPU:0"}) { + tf_executor.graph { + %control = tf_executor.island { + %0 = "tf.Identity"(%arg0) {device = "/TPU:0"} : (!tf_res) -> !tf_res + tf_executor.yield + } + tf_executor.fetch %control : !tf_executor.control + } + return + } +``` +### `-tf-rewrite-tpu-embedding-ops` + +_Rewrites TPU embedding send/recv ops by adding TPU embedding deduplication data_ + +### `-tf-shape-inference` + +_Shape inference on TF dialect and ops implementing InferTypeOpInterface_ + +Fixed point shape refinement pass that utilizes the shape functions +registered on ops using the InferTypeOpInterface as well as by bridging to +the TensorFlow op registry's shape functions. This is an interprocedural +pass that propagates information across function calls/control flow +operations where possible (the GuaranteeAllFuncsOneUsePass is often run +before this pass to enable more propagation opportunities). It refines +both the outermost element type of tensors as well as the nested component +type (e.g., for tensor lists). + +During shape refinement this pass may insert additional cast operations as +well as fold some constant shape computations to enable more exact shape +inference. Therefore it does do some mutation of the graph. Constant folding +required to produce more exact shapes is also performed but these values +are only kept in the context rather than the ops folded/IR mutated. + +#### Options +``` +-max-iterations : Maximum shape inference iterations +``` +### `-tf-simple-device-assignment` + +_Simple device assignment in TF dialect._ + +Assigns the default device to all ops that have an empty (or +nonexistent) device attribute. + +For example, if we have the code + +```mlir + %0 = "tf.Const"() {value = dense<[[42.0]]> : tensor<1x1xf32>} : () -> tensor<1x1xf32> + %1 = "tf.Const"() {device = "", value = dense<[[42.0]]> : tensor<1x1xf32>} : () -> tensor<1x1xf32> + %2 = "tf.Const"() {device = "baz", value = dense<[[42.0]]> : tensor<1x1xf32>} : () -> tensor<1x1xf32> +``` + +then running this pass with 'default-device=foobar', we get: + +```mlir + %0 = "tf.Const"() {device = "foobar" value = dense<[[42.0]]> : tensor<1x1xf32>} : () -> tensor<1x1xf32> + %1 = "tf.Const"() {device = "foobar", value = dense<[[42.0]]> : tensor<1x1xf32>} : () -> tensor<1x1xf32> + %2 = "tf.Const"() {device = "baz", value = dense<[[42.0]]> : tensor<1x1xf32>} : () -> tensor<1x1xf32> +``` + +#### Options +``` +-default-device : The default device to assign. +``` +### `-tf-stack-ops-decomposition` + +_Decompose stack operations into local variable operations. Needs static shapes._ + +A pass that converts stack operations to tensor operations and read/assign +ops on local variables. A later resource lifting pass can further remove the +local variables. + +This pass requires that the full shape of the stack can be inferred: 1) the +maximum size needs to be a constant and 2) a push op can be found with a +known shape, and all push ops need to have the same shape. + +A stack creation op "tf.StackV2" will be turned in to two zero-initialized +variables, for the buffer and current size. Each push will be turned into +```mlir + %old_val = "tf.ReadVariableOp"(%buffer) + %old_size = "tf.ReadVariableOp"(%size) + %offsets = "tf.ConcatV2"(%old_size, %other_dims_0s, %const0) + %new_val = "tf.XlaDynamicUpdateSlice"(%old_val, %push_val, %offsets) + "tf.AssignVariableOp"(%buffer, %new_val) + %new_size = "tf.AddV2"(%old_size, %const1) + "tf.AssignVariableOp"(%size, %new_size) +``` + +and each pop will be turned into + +```mlir + %old_val = "tf.ReadVariableOp"(%buffer) + %old_size = "tf.ReadVariableOp"(%size) + %new_size = "tf.Sub"(%old_size, %const1) + %offsets = "tf.ConcatV2"(%old_size, %other_dims_0s, %const0) + %slice = "tf.Slice"(%old_val, %offsets, %slice_size_const) + %pop_result = "tf.Reshape"(%slice, %elem_size_const) + "tf.AssignVariableOp"(%size, %new_size) +``` + +The pass also works across control flow and functional calls. +### `-tf-strip-noinline-attribute` + +_Strip the tf._noinline attribute from top-level functions._ + +### `-tf-strip-tf-attributes` + +_Removes TF specific attributes_ + +Removes attributes that are TF specific (start with "tf.") or that +have a value from the TF dialect. Useful after legalizing TF graphs +to other dialects, to remove any TF remnants. +### `-tf-tensor-array-ops-decomposition` + +_Decompose tensor array operations into local variable operations._ + +A pass that converts tensor array operations to tensor operations and +read/assign ops on local variables. A later resource lifting pass can further +remove the local variables. + +This pass requires that the full shape of the tensor array can be inferred: +1) the size needs to be a constant, 2) it specifies the full element shape, +or that can be inferred from a later write, and 3) all elements have the same +shape. +### `-tf-tensor-device-copy` + +_Fold the tf.Identity op and the tf.IdentityN op if the op has the same device as its operand_ + +### `-tf-tensor-list-ops-decomposition` + +_Decomposes TensorList operations into generic operations on tensors._ + +This pass rewrites TensorList operations into generic and non-mutating +operations on tensors. This results in operations that can be legalized to XLA. + +The list is converted to a single large tensor that includes all list elements, +with a new first dimension for the list index. List update operations are +converted to operations that create a new tensor representing the list. + +In the current implementation, the resulting operations are statically shaped, +which means it must be possible to infer a bound on the full shape of the +TensorList. That is, the `element_shape` and `num_elements` arguments to a +tensor list creation op are constant. + +A tensor list creation op `tf.EmptyTensorList`/`tf.TensorListReserve` will be +turned in to a zero-initialized buffer, and the size is initialized to 0 +for `tf.EmptyTensorList` or the specified size for `tf.TensorListReserve`. +Each push will be turned into `tf.XlaDynamicUpdateSlice` with the incremented +size, and each pop will be turned into a `tf.Slice` and a copy of the buffer +with decremented size. Each `tf.TensorListSetItem` will be turned into a +`tf.XlaDynamicUpdateSlice` with unchanged size, and each `tf.TensorListGetItem` +will be rewritten to a `tf.Slice`. + +The pass also works across control flow and functional calls. + +For example, the TensorList ops in the following function: + +```mlir +func @main(%arg0: tensor<8x4xf32>) { + %elem_shape = "tf.Const"() {value = dense<[8, 4]> : tensor<2xi32>} : () -> tensor<2xi32> + %max_size = "tf.Const"() {value = dense<10> : tensor} : () -> tensor + %tl = "tf.EmptyTensorList"(%elem_shape, %max_size) : (tensor<2xi32>, tensor) -> tensor>> + %push = "tf.TensorListPushBack"(%tl, %arg0) : (tensor>>, tensor<8x4xf32>) -> tensor>> + return +} +``` + +will be transformed to: + +```mlir +func @main(%arg0: tensor<8x4xf32>) { + // EmptyTensorList lowering + %emptyi = "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %emptyf = "tf.Cast"(%emptyi) : (tensor) -> tensor + %size_shape = "tf.Const"() {value = dense<[10, 8, 4]> : tensor<3xi32>} : () -> tensor<3xi32> + %tl = "tf.BroadcastTo"(%emptyf, %size_shape) : (tensor, tensor<3xi32>) -> tensor<10x8x4xf32> + // TensorListPushBack lowering + %index_in_list = "tf.Const"() {value = dense<0> : tensor<1xi32>} : () -> tensor<1xi32> + %arg0_shape = "tf.Const"() {value = dense<[1, 8, 4]> : tensor<3xi32>} : () -> tensor<3xi32> + %arg0_reshaped = "tf.Reshape"(%arg0, %arg0_shape) : (tensor<8x4xf32>, tensor<3xi32>) -> tensor<1x8x4xf32> + %zeroi2 = "tf.Const"() {value = dense<0> : tensor<2xi32>} : () -> tensor<2xi32> + %axis = "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %start_indices = "tf.ConcatV2"(%index_in_list, %zeroi2, %axis) : (tensor<1xi32>, tensor<2xi32>, tensor) -> tensor<3xi32> + %push = "tf.XlaDynamicUpdateSlice"(%tl, %arg0_reshaped, %start_indices) : (tensor<10x8x4xf32>, tensor<1x8x4xf32>, tensor<3xi32>) -> tensor<10x8x4xf32> + %one = "tf.Const"() {value = dense<1> : tensor<1xi32>} : () -> tensor<1xi32> + %next_index_in_list = "tf.AddV2"(%index_in_list, %one) : (tensor<1xi32>, tensor<1xi32>) -> tensor<1xi32> + return +} +``` +### `-tf-tpu-annotate-dynamic-shape-inputs` + +_Annotate the inputs returned by TPUCopyWithDynamicShapeOp with dynamic shape_ + +This pass looks for the usage of the result of TPUCopyWithDynamicShapeOp +and sets the shape of these inputs to be dynamic shaped. This will ensure +that the generated HLO program is correctly reflecting the dynamic shape. +### `-tf-tpu-cleanup-cluster-attributes` + +_Eliminate _replication_info and other attributes from ops in a cluster_ + +This pass eliminate `_replication_info` and `device` attribute on operations +that are contained in a tf_device.cluster op. +### `-tf-tpu-cluster-formation` + +_Forms clusters from operations assigned to the same TPU computation_ + +TPU computations from the frontend are composed of a `tf.TPUReplicateMetadata` +op, a subgraph of ops (TensorFlow Dialect) each with a matching +`_replication_info` attribute relative to the associated +`tf.TPUReplicateMetadata` op, and optionally `tf.TPUReplicatedInput` and +`tf.TPUReplicatedOutput` ops feeding in inputs and outputs to and from a +replicated TPU computation. The number of times a TPU computation is +replicated is defined in the `tf.TPUReplicateMetadata` op (`num_replicas` +attribute) and operand and result sizes of `tf.TPUReplicatedInput` and +`tf.TPUReplicatedOutput` respectively must match, excluding packed tensors. +It is also assumed ops of the same TPU computation do not have ops outside +of the TPU computation that are both inputs and outputs to the same TPU +computation. Furthermore, we assume that every node has either none or both +of `_replication_info` and `_xla_compile_device_type` attributes defined. + +This pass takes the TPU computation subgraph, moves them into a +`tf_device.cluster`, and copies over attributes from the associated +`tf.TPUReplicateMetadata` op to the newly created `tf_device.cluster`. If the +computation is replicated (`num_replicas` > 1), the `num_replicas` attribute is +not copied over but instead the `tf_device.cluster` is further wrapped with a +`tf_device.replicate`, and associated `tf.TPUReplicatedInput` and +`tf.TPUReplicatedOutput` ops are replaced as the `tf_device.replicate` operands +and results. Otherwise, the single operands and results of the associated +`tf.TPUReplicatedInput` and `tf.TPUReplicatedOutput` ops are simply forwarded to +the `tf_device.cluster`. + +For example, the following non replicated computation: + +```mlir +func @tpu_computation(%arg0: tensor) -> tensor { + // Metadata op for cluster `cluster` with 1 replica, 1 core per replica and + // with topology ``. + "tf.TPUReplicateMetadata"() {_xla_compile_device_type = "TPU", _replication_info = "cluster", num_relicas = 1, num_cores_per_replica = 1, topology = "", device_assignment = [], padding_map = []} : () -> () + %replicated_input = "tf.TPUReplicatedInput"(%arg0) : (tensor) -> tensor + %identity = "tf.Identity"(%replicated_input) {_xla_compile_device_type = "TPU", _replication_info = "cluster"} : (tensor) -> tensor + %replicated_output = "tf.TPUReplicatedOutput(%identity) : (tensor) -> tensor + return %replicated_output : tensor +} +``` + +will be transformed into: + +```mlir +func @tpu_computation(%arg0: tensor) -> tensor { + %cluster = "tf_device.cluster"() ( { + %identity = "tf.Identity"(%arg0) : (tensor) -> tensor + tf_device.return %identity : tensor + }) {_xla_compile_device_type = "TPU", _replication_info = "cluster", num_cores_per_replica = 1, topology = "topology", device_assignment = [], padding_map = []} : () -> (tensor) + return %cluster : tensor +} +``` + +The following replicated computation: + +```mlir +func @tpu_computation(%arg0: tensor, %arg1: tensor) -> (tensor, tensor) { + "tf.TPUReplicateMetadata"() {_xla_compile_device_type = "TPU", _replication_info = "cluster", num_relicas = 2, num_cores_per_replica = 1, topology = "topology", device_assignment = [], padding_map = []} : () -> () + %replicated_input = "tf.TPUReplicatedInput"(%arg0, %arg1) : (tensor, tensor) -> tensor + %identity = "tf.Identity"(%replicated_input) {_xla_compile_device_type = "TPU", _replication_info = "cluster"} : (tensor) -> tensor + %replicated_output:2 = "tf.TPUReplicatedOutput(%identity) : (tensor) -> (tensor, tensor) + return %replicated_output#0, %replicated_output#1 : tensor, tensor +} +``` + +will be transformed into: + +```mlir +func @tpu_computation(%arg0: tensor, %arg1: tensor) -> (tensor, tensor) { + %replicate:2 = tf_device.replicate([%arg0, %arg1] as %replicated_input) {n = 2 : i32} { + %cluster = "tf_device.cluster"() ( { + %identity = "tf.Identity"(%replicated_input) : (tensor) -> tensor + tf_device.return %identity : tensor + }) {_xla_compile_device_type = "TPU", _replication_info = "cluster", num_cores_per_replica = 1, topology = "topology", device_assignment = [], padding_map = []} : () -> (tensor) + tf_device.return %cluster : tensor + } + return %replicate#0, %replicate#1 : tensor, tensor +} +``` +### `-tf-tpu-colocate-composite-resource-ops` + +_Colocate resource with composite device assignment to TPU device._ + +Pass that co-locates resource ops that use composite device resources +(packed tensors) with the underlying physical TPU device. + +So for example, if we have a function that does (inside a `tf_device.replicate`): + +```mlir + %0 = "tf.ReadVariableOp"(%arg1) : (tensor<*x!tf_type.resource>>) -> tensor<4xf32> +``` + +Then said `ReadVariableOp` is going to get replaced by: + +```mlir + %0 = "tf_device.launch"() ( { + %2 = "tf.ReadVariableOp"(%arg1) : (tensor<*x!tf_type.resource>>) -> tensor<4xf32> + tf_device.return %2 : tensor<4xf32> + }) {...} : () -> tensor<4xf32> +``` +### `-tf-tpu-colocate-splits` + +_Colocates each Split op with its predecessor_ + +It is beneficial for performance to assign a `Split` op to the same device +as its predecessor. This is because the weight of cut edges is always +minimized when the `Split` is with its predecessor. This colocation +constraint will be used by the placer graph optimization to assign a device +to the op. + +This pass should run in the export pipeline after tf-replicate-to-island so +each replica has its own distinct (predecessor, Split) pair. + +The colocation class (`_class`) of the `Split` is set to the same class as +its predecessor: + +```mlir +%outputs1:2, %control1 = tf_executor.island wraps "tf.IteratorGetNext"(%arg) + {_class = ["loc:@dataset_iterator_1"]} +%outputs2:2, %control2 = tf_executor.island wraps "tf.Split"(%outputs0, %outputs1#1) + {_class = ["loc:@dataset_iterator_1", num_split = 2 : i32} +``` +### `-tf-tpu-device-propagation` + +_Propagates TPU devices from ops to users_ + +### `-tf-tpu-dynamic-layout-pass` + +_Inserts TPU layout ops to determine layout at run time._ + +A pass that allows TPU input layout to be determined after JIT compilation. +This is done by adding run-time ops that interpret compilation result and +copy the input to device with that layout. + +Example: original program: + +```mlir + %input = "tf.IteratorGetNext"(...) {device = "/CPU:0"} + %compile:2 = "tf._TPUCompileMlir"(...) + %execute = "tf.TPUExecute"(%input, ..., %compile#1) {device = "/TPU:0"} +``` + +Without this pass, later TF graph partitioning passes will insert send/recv +between %input and %execute and data will be copied to device in a fixed +layout. With this pass, the program will be transformed into: + +```mlir + %input = "tf.IteratorGetNext"(...) {device = "/CPU:0"} + %compile:2 = "tf._TPUCompileMlir"(...) + %get_layout = "tf.TPUGetLayoutOp"(%compile#1) {...} + %copy_to_device = "tf.TPUCopyWithLayout"(%input, %get_layout) + {device = "/TPU:0"} + %execute = "tf.TPUExecute"(%copy_to_device, ..., %compile#1) + {device = "/TPU:0"} +``` + +This way, %compile will determine the layout, which will be respected by +%copy_to_device. There will not be send/recv ops added by later passes, +because tf.TPUCopyWithLayout accepts a host input and produces a device +output. +### `-tf-tpu-host-computation-expansion` + +_Expands host computation before and after TPU computation._ + +This pass expands outside compilation attributes to Identity/Cast ops +at the head of TPU computation if it's only used by outside compiled ops. +### `-tf-tpu-identity-pruning` + +_Removes Identity/IdentityN ops from the TPU computation_ + +### `-tf-tpu-merge-variables-with-execute` + +_Merges device variable reads and updates into TPU execute ops_ + +This pass finds on-device resource variable reads and updates surrounding a +`tf.TPUExecute` op and merges them into a `tf.TPUExecuteAndUpdateVariables` +op. This allows the TPU execution to perform more efficient in-place +variable updates. + +For example, + +```mlir + %0 = "tf.ReadVariableOp"(%arg0) + %1 = "tf.ReadVariableOp"(%arg1) + %2 = "tf.TPUExecute"(%0, %1, %compile) + %3 = "tf.AssignVariableOp"(%arg0, %2) +``` + +will be transformed into + +```mlir + %2 = "tf.TPUExecuteAndUpdateVariables"(%arg0, %arg1, %compile) + { device_var_reads_indices = [0, 1], + device_var_updates_indices = [0, -1] } +```` + +The transformation happens only for on-device variables. The above +transformation requires `%arg0`, `%arg1` to have the same device assignment +as the `TPUExecute` op. +### `-tf-tpu-parallel-execute-sink-resource-write` + +_Moves tf.AssignVariableOp consumers of tf_device.parallel_execute into tf_device.parallel_execute regions_ + +### `-tf-tpu-partitioned-op-conversion` + +_Rewrite all TPU Partitioned ops into their V2 counterparts._ + +### `-tf-tpu-reorder-replicate-partitioned-inputs` + +_Reorder replicated and partitioned input ops._ + +This pass rewrites how data parallelism and model parallelism is expressed for +inputs. It reorders `tf.TPUPartitionedInput` (model parallelism) and +`tf.TPUReplicatedInput` (data parallelism) ops. It transforms a DAG where +multiple `tf.TPUPartitionedInput` ops are feeding into a single +`tf.TPUReplicatedInput` into a DAG where multiple `tf.TPUReplicatedInput` ops +are feeding into a single `tf.TPUPartitionedInput`. Transforming the IR in such +a manner will allow subsequent cluster formation pass to handle IR with both +data and model parallelism in an easier manner. + +For example, the following: + +```mlir +!rtype = type tensor>> +func @data_and_model_parallelism(%arg0: !rtype, %arg1: !rtype, %arg2: !rtype, %arg3: !rtype) -> !rtype { + %pi_0 = "tf.TPUPartitionedInput"(%arg0, %arg1) {_XlaSharding = "", device = "", partition_dim = -1 : i64} : (!rtype, !rtype) -> !rtype + %pi_1 = "tf.TPUPartitionedInput"(%arg2, %arg3) {_XlaSharding = "", device = "", partition_dim = -1 : i64} : (!rtype, !rtype) -> !rtype + %ri = "tf.TPUReplicatedInput"(%pi_0, %pi_1) : (!rtype, !rtype) -> !rtype + return %ri : !rtype +} +``` + +will be transformed into: + +```mlir +!rtype = type tensor>> +func @data_and_model_parallelism(%arg0: !rtype, %arg1: !rtype, %arg2: !rtype, %arg3: !rtype) -> !rtype { + %ri_0 = "tf.TPUReplicatedInput"(%arg0, %arg2) : (!rtype, !rtype) -> !rtype + %ri_1 = "tf.TPUReplicatedInput"(%arg1, %arg3) : (!rtype, !rtype) -> !rtype + %pi = "tf.TPUPartitionedInput"(%ri_0, %ri_1) {_XlaSharding = "", device = "", partition_dim = -1 : i64} : (!rtype, !rtype) -> !rtype + return %pi : !rtype +} +``` +### `-tf-tpu-resource-partition` + +_Partitions unpartitioned resource read/write to partitioned resource variables._ + +This pass creates individual resource reads/writes from the unpartitioned +resource variable (from `tf.TPUPartitionedInput`) to individual partitioned +resource variables (`tf.TPUPartitionedInput` operands). As resource op +decomposition/lifting occurs with the unpartitioned resource variables, +transforming the IR in such a manner will allow for subsequent passes to operate +on individual resource variable handles per core/device. + +For example, the following: + +```mlir +func @cluster(%arg0: tensor>>, %arg1: tensor>>) { + %partitioned_variable = "tf.TPUPartitionedInput"(%arg0, %arg1) {N = 2 : i64, _XlaSharding = "", partition_dim = -1 : i64} : (tensor>>, tensor>>) -> tensor>> + %read = "tf.ReadVariableOp"(%partitioned_variable) : (tensor>>) -> tensor + %computation = "tf_device.cluster_func"(%read) {func = @computation, use_spmd_for_xla_partitioning = true} : (tensor) -> tensor + "tf.AssignVariableOp"(%partitioned_variable, %computation) : (tensor>>, tensor) -> () + return +} + +func @computation(%arg0: tensor) -> tensor { + return %arg0: tensor +} +``` + +will be transformed into: + +```mlir +func @cluster(%arg0: tensor>>, %arg1: tensor>>) { + %read0 = "tf.ReadVariableOp"(%arg0) : (tensor>>) -> tensor + %read1 = "tf.ReadVariableOp"(%arg1) : (tensor>>) -> tensor + %partitioned_input = "tf.TPUPartitionedInput"(%read0, %read1) {N = 2 : i64, _XlaSharding = "", partition_dim = -1 : i64} : (tensor, tensor) -> tensor + %computation = "tf_device.cluster_func"(%partitioned_input) {func = @computation, use_spmd_for_xla_partitioning = true} : (tensor) -> tensor + %partitioned_output:2 = "tf.TPUPartitionedOutput"(%computation) {N = 2 : i64, _XlaSharding = "", partition_dim = -1 : i64} : (tensor) -> (tensor, tensor) + "tf.AssignVariableOp"(%arg0, %partitioned_output#0) : (tensor>>, tensor) -> () + "tf.AssignVariableOp"(%arg1, %partitioned_output#1) : (tensor>>, tensor) -> () + return +} + +func @computation(%arg0: tensor) -> tensor { + return %arg0: tensor +} +``` +### `-tf-tpu-resource-read-for-write` + +_Inserts tf.ReadVariableOp inputs to a TPU cluster for resource writes with no reads_ + +This pass materializes `tf.ReadVariableOp` inputs to an outlined TPU computation +for resource variables where only writes are present so later in the pipeline +such resource variables can be fused with generated `tf.TPUExecute` ops, which +only supports resource variable read or read + write. For all TPU computations, +resource variables are required to be initialized prior to execution. Write only +resource variable uses can be generated currently via packed tensor uses. + +For example, the following: + +```mlir +func @write_only_resource(%value: tensor, %resource: tensor<*x!tf_type.resource>>) { + %0 = "tf_device.cluster_func"(%value) {func = @cluster} : (tensor) -> tensor + "tf.AssignVariableOp"(%resource, %0) : (tensor<*x!tf_type.resource>>, tensor) -> () + return +} + +func @cluster(%arg0: tensor) -> tensor { + %identity = "tf.Identity"(%arg0) : (tensor) -> tensor + return %identity : tensor +} +``` + +will be transformed into: + +```mlir +func @write_only_resource(%value: tensor, %resource: tensor<*x!tf_type.resource>>) { + %resource_read = "tf.ReadVariableOp"(%resource) : (tensor<*x!tf_type.resource>>) -> tensor + %0 = "tf_device.cluster_func"(%value, %resource_read) {func = @cluster} : (tensor, tensor) -> tensor + "tf.AssignVariableOp"(%resource, %0) : (tensor<*x!tf_type.resource>>, tensor) -> () + return +} + +func @cluster(%arg0: tensor, %arg1: tensor) -> tensor { + %identity = "tf.Identity"(%arg0) : (tensor) -> tensor + return %identity : tensor +} +``` +### `-tf-tpu-rewrite` + +_Rewrites a `tf_device.cluster_func` on TPUs into TPU runtime operations._ + +This pass rewrites a `tf_device.cluster_func` operation into a sequence of `tf._TPUCompileMlir` +and `tf.TPUExecute` operations. `tf._TPUCompileMlir` contains a MLIR module that is +functionally equivalent to the function referenced by `tf_device.cluster_func`. +This makes the module to be jit-compiled and executed on TPU. +If it is not possible to rewrite the operation or device assignment fails, +a failure will be returned. + +Note, many parameters to the `tf_device.cluster_func` are omitted in this +and following examples. +For example, a non replicated `tf_device.cluster_func`: + +```mlir +func @tf_tpu_rewrite(%arg0: tensor) { + %0 = "tf_device.cluster_func"(%arg0) {_xla_compile_device_type = "TPU", _replication_info = "cluster0", func = @func} : (tensor) -> tensor + return +} +``` + +will be rewritten as: + +```mlir +func @tf_tpu_rewrite(%arg0: tensor) { + %0:2 = "tf_device.launch"() ( { + %compilation_status, %program = "tf._TPUCompileMlir"() {mlir_module = ""} : () -> (tensor, tensor<3x!tf_type.string>) + tf_device.return %compilation_status, %program : tensor, tensor<3x!tf_type.string> + }) {device = "/job:worker/replica:0/task:0/device:CPU:0"} : () -> (tensor, tensor<3x!tf_type.string>) + "tf_device.launch"() ( { + "tf.TPUCompileSucceededAssert"(%0#0) : (tensor) -> () + tf_device.return + }) {device = "/job:worker/replica:0/task:0/device:CPU:0"} : () -> () + %1 = "tf_device.launch"() ( { + %2 = "tf.TPUExecute"(%arg0, %0#1) : (tensor, tensor<3x!tf_type.string>) -> tensor + tf_device.return %2 : tensor + }) {device = "/job:worker/replica:0/task:0/device:TPU:0"} : () -> tensor + return +} +``` + +A replicated `tf_device.cluster_func`: + +```mlir +func @tf_tpu_rewrite(%arg0: tensor, %arg1: tensor) { + %0:2 = tf_device.replicate([%arg0, %arg1] as %ri: tensor) {n = 2 : i32} { + %1 = "tf_device.cluster_func"(%ri) {_xla_compile_device_type = "TPU", _replication_info = "cluster0", func = @func} : (tensor) -> tensor + tf_device.return %1 : tensor + } + return +} +``` + +will be rewritten as: + +```mlir +func @tf_tpu_rewrite(%arg0: tensor, %arg1: tensor) { + %0:2 = tf_device.replicate([%arg0, %arg1] as %arg2: tensor) {devices = {TPU_REPLICATED_CORE_0 = ["/job:worker/replica:0/task:0/device:TPU:0", "/job:worker/replica:0/task:0/device:TPU:1"], TPU_REPLICATED_HOST_0 = ["/job:worker/replica:0/task:0/device:CPU:0", "/job:worker/replica:0/task:0/device:CPU:0"]}, n = 2 : i32} { + %1:2 = "tf_device.launch"() ( { + %compilation_status, %program = "tf._TPUCompileMlir"() {mlir_module = ""} : () -> (tensor, tensor<3x!tf_type.string>) + tf_device.return %compilation_status, %program : tensor, tensor<3x!tf_type.string> + }) {device = "/job:worker/replica:0/task:0/device:CPU:0"} : () -> (tensor, tensor<3x!tf_type.string>) + "tf_device.launch"() ( { + "tf.TPUCompileSucceededAssert"(%1#0) : (tensor) -> () + tf_device.return + }) {device = "/job:worker/replica:0/task:0/device:CPU:0"} : () -> () + %2 = "tf_device.launch"() ( { + %3 = "tf.TPUExecute"(%arg2, %1#1) : (tensor, tensor<3x!tf_type.string>) -> tensor + tf_device.return %3 : tensor + }) {device = "TPU_REPLICATED_CORE_0"} : () -> tensor + tf_device.return %2 : tensor + } + return +} +``` + +A non replicated `tf_device.cluster_func` with the model parallelism: + +```mlir +func @tf_tpu_rewrite(%arg0: tensor<8xi32>) -> tensor<8xi32> { + %0 = "tf_device.cluster_func"(%arg0) {_xla_compile_device_type = "TPU", _replication_info = "cluster0", func = @func, num_cores_per_replica = 2, input_sharding_configuration = ["\08\01\1A\01\01\22\01\00"], output_sharding_configuration = ["\08\01\1A\01\01\22\01\00"]} : (tensor<8xi32>) -> tensor<8xi32> + return %0 : tensor<8xi32> +} +``` + +will be rewritten as: + +```mlir +func @tf_tpu_rewrite(%arg0: tensor<8xi32>) -> tensor<8xi32> { + %0:3 = "tf_device.launch"() ( { + %compilation_status, %program:2 = "tf._TPUCompileMlir"() {mlir_module = ""} : () -> (tensor, tensor<3x!tf_type.string>, tensor<3x!tf_type.string>) + tf_device.return %compilation_status, %program#0, %program#1 : tensor, tensor<3x!tf_type.string>, tensor<3x!tf_type.string> + }) {device = "/job:localhost/replica:0/task:0/device:CPU:0"} : () -> (tensor, tensor<3x!tf_type.string>, tensor<3x!tf_type.string>) + "tf_device.launch"() ( { + "tf.TPUCompileSucceededAssert"(%0#0) : (tensor) -> () + tf_device.return + }) {device = "/job:localhost/replica:0/task:0/device:CPU:0"} : () -> () + %1 = "tf_device.parallel_execute"() ( { + %2 = "tf_device.launch"() ( { + %3 = "tf.TPUExecute"(%arg0, %0#1) : (tensor<8xi32>, tensor<3x!tf_type.string>) -> tensor<8xi32> + tf_device.return %3 : tensor<8xi32> + }) {device = "/job:localhost/replica:0/task:0/device:TPU:0"} : () -> tensor<8xi32> + tf_device.return %2 : tensor<8xi32> + }, { + "tf_device.launch"() ( { + "tf.TPUExecute"(%0#2) : (tensor<3x!tf_type.string>) -> () + tf_device.return + }) {device = "/job:localhost/replica:0/task:0/device:TPU:1"} : () -> () + tf_device.return + }) : () -> tensor<8xi32> + return %1 : tensor<8xi32> +} +``` + +#### Options +``` +-tpu-compile-metadata-debug : Whether to serialize TPUCompileMetadataProto metadata in 'tf._TPUCompileMlir' op as a proto debug string +``` +### `-tf-tpu-sharding-identification` + +_Identifies and handles inputs/outputs of TPU computation that is sharded across logical cores._ + +Bubbles up sharding configuration from `cluster_func` regions into +the attributes of `cluster_func`. This is done by parsing the +`XlaSharding` / `TPUPartitionedOutput` / `TPUPartitionedInput` ops inside +`cluster_func`. + +For example, given the following `cluster_func` wrapping `func`: + +```mlir + func @test(%arg0: tensor<*xi32>) { + "tf_device.cluster_func"(%arg0) { + func = @func, + step_marker_location = ""} : (tensor<*xi32>) -> tensor<*xi32> + return + } + + func @func(%arg0: tensor<*xi32>) -> tensor<*xi32> { + %0 = "tf.XlaSharding"(%arg0) {_XlaSharding = "\01\02\03", + sharding = "\01\02\03"} : (tensor<*xi32>) -> tensor<*xi32> + %1 = "tf.A"(%0) : (tensor<*xi32>) -> (tensor<*xi32>) + return %1 : tensor<*xi32> + } +``` + +Now, cluster_func receives the following `*_sharding_configuration` +attributes, and `func` receives the mhlo.sharding attribute: + +```mlir + func @test(%arg0: tensor<*xi32>) { + %0 = "tf_device.cluster_func"(%arg0) { + func = @func, + input_sharding_configuration = ["\01\02\03"], + output_sharding_configuration = ["\08\01\1A\01\01\22\01\00"], + step_marker_location = ""} : (tensor<*xi32>) -> tensor<*xi32> + return + } + func @func(%arg0: tensor<*xi32> {mhlo.sharding = "\01\02\03"}) -> + (tensor<*xi32> {mhlo.sharding = "\08\01\1A\01\01\22\01\00"}) { + %0 = "tf.XlaSharding"(%arg0) {_XlaSharding = "\01\02\03", sharding = "\01\02\03"} : (tensor<*xi32>) -> tensor<*xi32> + %1 = "tf.A"(%0) : (tensor<*xi32>) -> tensor<*xi32> + return %1 : tensor<*xi32> + } +``` +### `-tf-tpu-space-to-depth-pass` + +_Applies automatic space to depth transform for the first or frontier convolutions consume host inputs on TPU._ + +Automatic space to depth transform is done by adding space to depth transform op after host input +and applying space to depth transform for the first convolution and its backprop filter on TPU. + +For example, original program: + +```mlir +module { + func @while_body { + %input = "tf.IteratorGetNext"(...) {device = "/CPU:0"}: -> tensor<2x224x224x3xf32> + %device_launch = "tf_device.cluster_func"(%input,...) {func = @_func,...) + return ... + } + func @_func(%input: tensor<2x224x224x3xf32>, %filter: tensor<7x7x3x64xf32>) { + %6 = "tf.Conv2D"(%input, %filter) {strides = [1, 2, 2, 1]}: (tensor<2x230x230x3xf32>, tensor<7x7x3x64xf32>) -> tensor<2x112x112x64xf32> + } +} +``` + +The program will be transformed into: + +```mlir +module { + func @while_body { + %input = "tf.IteratorGetNext"(...) {device = "/CPU:0"} -> tensor<2x224x224x3xf32> + %space_to_depth = "tf.SpaceToDepth"(%input) {block_size = 2, ...}: (tensor<2x224x224x3xf32>) -> tensor<2x112x112x12xf32> + %device_launch = "tf_device.cluster_func"(%space_to_depth,...) {func = @_func,...) + return ... + } + func @_func(%input: tensor<2x112x112x12xf32>, %filter: tensor<7x7x3x64xf32>) { + %filter_transform = "tf.Pad/tf.Transpose/tf.Reshape"(%filter): tensor<7x7x3x64xf32>) -> tensor<4x4x12x64xf32> + %conv = "tf.Conv2D"(%input, %filter_transfrom) {strides = [1, 1, 1, 1]}: (tensor<2x112x112x12xf32>, tensor<4x4x12x64xf32>) -> tensor<2x112x112x64xf32> + } +} +``` + +This way, the first convolution with 3 feature dimension will be transformed +to 12 feature dimension, which has better performance on TPU. +### `-tf-tpu-update-embedding-enqueue-op-inputs` + +_Updates inputs to TPU embedding enqueue ops depending on whether graph is in training mode or in evaluation mode._ + +Updates inputs to TPU embedding enqueue ops depending on whether graph +is in training mode or in evaluation mode. +### `-tf-tpu-validate-inputs` + +_Validates inputs to the TPU TF/XLA bridge_ + +This pass checks that the IR has valid input to TPU TF/XLA bridge. +It checks the relations of multiple ops. Properties of single ops are +checked by the 'verify' method of ops. +### `-tf-tpu-variable-runtime-reformatting` + +_Adds device variable formatting op to allow compilation-guided variable formatting._ + +A pass that takes advantage of a loop to add ops that allow the execution to +avoid repeatedly formatting variables back and forth. The desired formatting +is determined by TPU program compilation, so this pass does not include how +to reformat the variables, but only inserts general TPUReshardVariablesOps in +proper places, and TPUReshardVariablesOps interpret the compilation. + +The core idea of this optimization is to keep track of the formatting state +of variables, and when the next desired state does not change, it can avoid +reformatting. We associate a set of variables on a device with a formatting +state, and TPUReshardVariablesOps compares the current state with a desired +state (which can be the compilation result). If they mismatch, +TPUReshardVariablesOp reformats the variables to the desired state; if they +match, TPUReshardVariablesOp is a no-op. + +A major use of this pass is weight-update sharding in data parallelism, so we +require there is a tf_device.replicate in the loop. + +For example, suppose we have a training loop (for simplicity we write the +loop body inine): + +```mlir + %var0 = ... + %var1 = ... + tf.while (..., %var0, %var1) { + tf_device.replicate ([%var0, %var1] as %rvar) { + %compile:2 = "tf._TPUCompileMlir"() + tf.TPUExecuteAndUpdateVariablesOp(%rvar, compile#1) + } + } +``` + +This pass will transform it into + +```mlir + %var0 = ... + %var1 = ... + %state_var0 = ... + %state_var1 = ... + tf.while (..., %var0, %var1, %state_var0, %state_var1) { + tf_device.replicate ([%var0, %var1] as %rvar, + [%state_var0, %state_var1] as %rstate) { + %compile:2 = "tf._TPUCompileMlir"() + tf.TPUReshardVariablesOp(%rvar, %compile#1, %rstate) + tf.TPUExecuteAndUpdateVariablesOp(%rvar, compile#1) + } + } + %default_format = tf.constant() + tf_device.replicate ([%var0, %var1] as %rvar, + [%state_var0, %state_var1] as %rstate) { + tf.TPUReshardVariablesOp(%rvar, %default_format, %rstate) + } +``` +### `-tf-unroll-batch-matmul` + +_Unroll TF BatchMatMul op into Reshape, Slice, MatMul, Pack ops._ + +### `-tf-verify-for-export` + +_Verify module is suitable for export back to TF Graph_ + +Verifies whether all functions in module are of single tf_executor.graph and +each tf_executor.island in tf_executor.graph only has a single op. +### `-tf-xla-call-module-deserialization` + +_Deserializes StableHLO functions embedded in `tf.XlaCallModule` to top level module_ + +This pass deserializes the StableHLO bytecodes embedded in tf.XlaCallModule, +then outlines the functions in the deserialized StableHLO module to the top +level MLIR module, with function renamings to avoid naming conflicts. + +After the outlining, it updates tf.XlaCallModule's module attribute to be +empty, adds an `_entry_function` attribute referring to the entry function. +It also adds a `_from_xla_call_module: true` attribute to each lifted +StableHLO function. +### `-tf-xla-call-module-serialization` + +_Serializes StableHLO functions from top-level module into `tf.XlaCallModule`'s `module` attribute_ + +This pass collects StableHLO functions referenced from `tf.XlaCallModule`'s +`_entry_function` attribute into a module, serializes the module into MLIR +bytecode, and embed the bytecode to `tf.XlaCallModule`'s `module` attribute. + +After serialization, this pass removes the `_entry_function` attribute from +`tf.XlaCallModule`, and removes all the serialized stablehlo functions +from the top-level module. +### `-tfe-legalize-tfg` + +_Legalize from TFG to the TFE dialect_ + diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/g3doc/_index.yaml b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/g3doc/_index.yaml new file mode 100644 index 0000000000000000000000000000000000000000..5eed3537d0d7b45a9c16b51646df0cb8ba9d69d2 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/g3doc/_index.yaml @@ -0,0 +1,55 @@ +book_path: /mlir/_book.yaml +project_path: /mlir/_project.yaml +description: An intermediate representation and compiler framework, MLIR unifies the + infrastructure for high-performance ML models in TensorFlow. +landing_page: + custom_css_path: /site-assets/css/style.css + rows: + - heading: MLIR unifies the infrastructure for high-performance ML models in TensorFlow. + items: + - description: > + The MLIR project defines a common + intermediate representation (IR) that unifies the infrastructure required to execute high + performance machine learning models in TensorFlow and similar ML frameworks. This project + will include the application of HPC techniques, along with integration of + search algorithms like reinforcement learning. MLIR aims to reduce the + cost to bring up new hardware, and improve usability for existing + TensorFlow users. + + - code_block: | +
+        // Syntactically similar to LLVM:
+        func @testFunction(%arg0: i32) {
+          %x = call @thingToCall(%arg0) : (i32) -> i32
+          br ^bb1
+        ^bb1:
+          %y = arith.addi %x, %x : i32
+          return %y : i32
+        }
+        
+ + - classname: devsite-landing-row-cards + items: + - heading: "Multi-Level Intermediate Representation for Compiler Infrastructure" + youtube_id: qzljG6DKgic + buttons: + - label: Watch the video + path: https://www.youtube.com/watch?v=qzljG6DKgic + - heading: "A new intermediate representation and compiler framework" + image_path: /resources/images/tf-logo-card-16x9.png + path: https://blog.tensorflow.org/2019/04/mlir-new-intermediate-representation.html + buttons: + - label: Read on TensorFlow blog + path: https://blog.tensorflow.org/2019/04/mlir-new-intermediate-representation.html + - heading: MLIR on GitHub + image_path: /resources/images/github-card-16x9.png + path: https://github.com/llvm/llvm-project/tree/main/mlir + buttons: + - label: View on GitHub + path: https://github.com/llvm/llvm-project/tree/main/mlir + - heading: TensorFlow MLIR on GitHub + image_path: /resources/images/github-card-16x9.png + path: https://github.com/tensorflow/tensorflow/tree/master/tensorflow/compiler/mlir + buttons: + - label: View on GitHub + path: https://github.com/tensorflow/tensorflow/tree/master/tensorflow/compiler/mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/g3doc/dialects.md b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/g3doc/dialects.md new file mode 100644 index 0000000000000000000000000000000000000000..7b53258dd82dba0a8060d194f84ceeafe481f905 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/g3doc/dialects.md @@ -0,0 +1,37 @@ +# MLIR dialects + +## Overview + + +To separate different hardware and software targets, MLIR has “dialects”, +including: + +* TensorFlow IR, which represents all things possible in TensorFlow graphs. +* XLA HLO IR, which is designed to take advantage of XLA’s compilation + abilities (with output to, among other things, TPUs). +* An experimental affine dialect, which focuses on + [polyhedral representations](https://en.wikipedia.org/wiki/Polytope_model) + and optimizations. +* LLVM IR, which has a 1:1 mapping between it and LLVM’s own representation, + allowing MLIR to emit GPU and CPU code through LLVM. +* TensorFlow Lite, which will translate to running code on mobile platforms. + +Each dialect consists of a set of defined operations which have invariants +placed on them, like: “This is a binary operator, and the inputs and outputs +have the same types.” + +## Adding to MLIR + +MLIR has no fixed/built-in list of globally known operations (no “intrinsics”). +Dialects can define entirely custom types, which is how MLIR can model things +like the LLVM IR type system (which has first class aggregates), domain +abstractions important for ML-optimized accelerators like quantized types, and +even the Swift or Clang type systems (which are built around Swift/Clang +declaration nodes) in the future. + +If you want to connect a new low-level compiler, you would create a new dialect +and the lowerings between the TensorFlow Graph dialect and your dialect. +This smooths the path for hardware and compiler makers. You can even target +dialects at different levels in the same model; the higher-level optimizers +will respect the unfamiliar parts of the IR and wait for a lower level to handle +it. diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/g3doc/images/mlir-infra.svg b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/g3doc/images/mlir-infra.svg new file mode 100644 index 0000000000000000000000000000000000000000..aec0986ba02f1a1c83d3bc48a43c2936ba1340f0 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/g3doc/images/mlir-infra.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/g3doc/overview.md b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/g3doc/overview.md new file mode 100644 index 0000000000000000000000000000000000000000..7fa23f49ebf24bc8314f446f0861fe1a420a0ed6 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/g3doc/overview.md @@ -0,0 +1,36 @@ +# MLIR + +## Overview + +MLIR, or Multi-Level Intermediate Representation, is a representation format +and library of compiler utilities that sits between the model representation +and low-level compilers/executors that generate hardware-specific code. + +MLIR is, at its heart, a flexible infrastructure for modern optimizing +compilers. This means it consists of a specification for intermediate +representations (IR) and a code toolkit to perform transformations on that +representation. (In compiler parlance, as you move from higher-level +representations to lower-level representations, these transformations can be +called “lowerings”) + +MLIR is highly influenced by [LLVM](https://llvm.org/) and unabashedly reuses +many great ideas from it. It has a flexible type system, and allows +representing, analyzing and transforming graphs combining multiple levels of +abstraction in the same compilation unit. These abstractions include TensorFlow +operations, nested polyhedral loop regions, and even LLVM instructions and fixed +hardware operations and types. + +We expect MLIR to be of interest to many groups, including: + +* Compiler researchers and implementers looking to optimize performance and + memory consumption of machine learning models +* Hardware makers looking for a way to connect their hardware to TensorFlow, + such as TPUs, portable neural hardware in phones, and other custom ASICs +* People writing language bindings that want to take advantage of optimizing + compilers and hardware acceleration. + +The TensorFlow ecosystem contains a number of compilers and optimizers that +operate at multiple levels of the software and hardware stack. We expect the +gradual adoption of MLIR to simplify every aspect of this stack. + +MLIR overview diagram diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/g3doc/tf_passes.md b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/g3doc/tf_passes.md new file mode 100644 index 0000000000000000000000000000000000000000..72fa0158203c51a8287327cc60c1c2e51006c945 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/g3doc/tf_passes.md @@ -0,0 +1,7 @@ +# TensorFlow passes + +[TOC] + +## TF dialect passes + +<<_includes/tf_passes.md>> diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/glob_lit_test.bzl b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/glob_lit_test.bzl new file mode 100644 index 0000000000000000000000000000000000000000..44eed47b4e2c349b55e41238ab98ba416fff79cb --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/glob_lit_test.bzl @@ -0,0 +1,163 @@ +# Test definitions for Lit, the LLVM test runner. +# +# This is reusing the LLVM Lit test runner in the interim until the new build +# rules are upstreamed. +# TODO(b/136126535): remove this custom rule. +"""Lit runner globbing test +""" + +load("@bazel_skylib//lib:paths.bzl", "paths") +load("@rules_python//python:py_test.bzl", "py_test") +load( + "@xla//xla:lit.bzl", + "lit_script_with_xla_gpu_cuda_data_dir", +) + +# Default values used by the test runner. +_default_test_file_exts = ["mlir", ".pbtxt", ".td"] +_default_driver = "@llvm-project//mlir:run_lit.sh" +_default_size = "small" +_default_tags = [] + +# These are patterns which we should never match, for tests, subdirectories, or +# test input data files. +_ALWAYS_EXCLUDE = [ + "**/LICENSE.txt", + "**/README.txt", + "**/lit.local.cfg", + # Exclude input files that have spaces in their names, since bazel + # cannot cope with such "targets" in the srcs list. + "**/* *", + "**/* */**", +] + +def get_canonical_repo_name(apparent_repo_name): + """Returns the canonical repo name for the given apparent repo name seen by the module this bzl file belongs to.""" + if not apparent_repo_name.startswith("@"): + apparent_repo_name = "@" + apparent_repo_name + + return Label(apparent_repo_name).workspace_name + +def _run_lit_test(name, data, size, tags, driver, features, exec_properties): + """Runs lit on all tests it can find in `data` under tensorflow/compiler/mlir. + + Note that, due to Bazel's hermetic builds, lit only sees the tests that + are included in the `data` parameter, regardless of what other tests might + exist in the directory searched. + + Args: + name: str, the name of the test, including extension. + data: [str], the data input to the test. + size: str, the size of the test. + tags: [str], tags to attach to the test. + driver: str, label of the driver shell script. + Note: use of a custom driver is not currently supported + and specifying a default driver will abort the tests. + features: [str], list of extra features to enable. + """ + + # Disable tests on windows for now, to enable testing rest of all xla and mlir. + py_test( + name = name, + srcs = ["@llvm-project//llvm:lit"], + tags = tags + ["no_pip", "no_windows"], + args = [ + "tensorflow/compiler/mlir/" + paths.basename(data[-1]) + " --config-prefix=runlit -v", + ] + features, + data = data + [ + "//tensorflow/compiler/mlir:litfiles", + "@llvm-project//llvm:FileCheck", + "@llvm-project//llvm:count", + "@llvm-project//llvm:not", + ], + deps = ["@pypi//lit"], + size = size, + main = "lit.py", + env = { + "LLVM_CANONICAL_REPO_NAME": get_canonical_repo_name("@llvm-project"), + "XLA_CANONICAL_REPO_NAME": get_canonical_repo_name("@xla"), + }, + exec_properties = exec_properties, + ) + +def glob_lit_tests( + name = None, + exclude = [], + test_file_exts = _default_test_file_exts, + default_size = _default_size, + size_override = {}, + data = [], + per_test_extra_data = {}, + default_tags = _default_tags, + tags_override = {}, + driver = _default_driver, + features = [], + exec_properties = {}, + use_lit_test_suite = None, # @unused + hermetic_cuda_data_dir = None): + """Creates all plausible Lit tests (and their inputs) under this directory. + + Args: + name: str, name of the test_suite rule to generate for running all tests. + exclude: [str], paths to exclude (for tests and inputs). + test_file_exts: [str], extensions for files that are tests. + default_size: str, the test size for targets not in "size_override". + size_override: {str: str}, sizes to use for specific tests. + data: [str], additional input data to the test. + per_test_extra_data: {str: [str]}, extra data to attach to a given file. + default_tags: [str], additional tags to attach to the test. + tags_override: {str: str}, tags to add to specific tests. + driver: str, label of the driver shell script. + Note: use of a custom driver is not currently supported + and specifying a default driver will abort the tests. + features: [str], list of extra features to enable. + exec_properties: a dictionary of properties to pass on. + hermetic_cuda_data_dir: string. If set, the tests will be run with a + `--xla_gpu_cuda_data_dir` flag set to the hermetic CUDA data directory. + use_lit_test_suite: unused. For compatibility. + """ + + # Ignore some patterns by default for tests and input data. + exclude = _ALWAYS_EXCLUDE + exclude + + tests = native.glob( + ["*." + ext for ext in test_file_exts], + exclude = exclude, + ) + + # Run tests individually such that errors can be attributed to a specific + # failure. + all_tests = [] + for curr_test in tests: + final_test_name = curr_test + if hermetic_cuda_data_dir: + output_file = "with_xla_gpu_cuda_data_dir_{}".format(curr_test) + rule_name = "script_{}".format(output_file) + lit_script_with_xla_gpu_cuda_data_dir( + rule_name, + curr_test, + output_file, + hermetic_cuda_data_dir, + ) + final_test_name = output_file + all_tests.append(final_test_name + ".test") + + # Instantiate this test with updated parameters. + _run_lit_test( + name = final_test_name + ".test", + data = data + [final_test_name] + + per_test_extra_data.get(curr_test, []), + size = size_override.get(curr_test, default_size), + tags = default_tags + tags_override.get(curr_test, []), + driver = driver, + features = features, + exec_properties = exec_properties, + ) + + # TODO: remove this check after making it a required param. + if name: + native.test_suite( + name = name, + tests = all_tests, + tags = ["manual"], + ) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/init_mlir.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/init_mlir.cc new file mode 100644 index 0000000000000000000000000000000000000000..af9dea47e5fa4e78646e716c6354f839bd1aa74f --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/init_mlir.cc @@ -0,0 +1,64 @@ +/* Copyright 2019 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/compiler/mlir/init_mlir.h" + +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/PrettyStackTrace.h" +#include "tensorflow/core/platform/init_main.h" + +static llvm::cl::extrahelp FlagSplittingHelp(R"( +The command line parsing is split between the two flag parsing libraries used by +TensorFlow and LLVM: + * Flags before the first '--' are parsed by tensorflow::InitMain while those + post are parsed by LLVM's command line parser. + * If there is no separator, then no flags are parsed by InitMain and only + LLVM command line parser used.e +The above help options reported are for LLVM's parser, run with `--help --` for +TensorFlow's help. +)"); + +namespace tensorflow { + +InitMlir::InitMlir(int *argc, char ***argv) { + llvm::setBugReportMsg( + "TensorFlow crashed, please file a bug on " + "https://github.com/tensorflow/tensorflow/issues with the trace " + "below.\n"); + + constexpr char kSeparator[] = "--"; + + // Find index of separator between two sets of flags. + int pass_remainder = 1; + bool split = false; + for (int i = 0; i < *argc; ++i) { + if (llvm::StringRef((*argv)[i]) == kSeparator) { + pass_remainder = i; + *argc -= (i + 1); + split = true; + break; + } + } + + tensorflow::port::InitMain((*argv)[0], &pass_remainder, argv); + if (split) { + *argc += pass_remainder; + (*argv)[1] = (*argv)[0]; + ++*argv; + } +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/init_mlir.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/init_mlir.h new file mode 100644 index 0000000000000000000000000000000000000000..45baa14553f3e2bc6f91583a00e4cbcf4a29fabc --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/init_mlir.h @@ -0,0 +1,33 @@ +/* Copyright 2019 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_INIT_MLIR_H_ +#define TENSORFLOW_COMPILER_MLIR_INIT_MLIR_H_ + +namespace tensorflow { + +// Initializer to perform TF's InitMain initialization. +// InitMain also performs flag parsing and '--' is used to separate flags passed +// to it: Flags before the first '--' are parsed by InitMain and argc and argv +// progressed to the flags post. If there is no separator, then no flags are +// parsed by InitMain and argc/argv left unadjusted. +class InitMlir { + public: + InitMlir(int *argc, char ***argv); +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_MLIR_INIT_MLIR_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/kernels/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/kernels/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..45e4709872e0f51aeeb8076bdbd71302aedf982a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/kernels/BUILD @@ -0,0 +1,22 @@ +load("@rules_cc//cc:cc_library.bzl", "cc_library") +load("//tensorflow:tensorflow.default.bzl", "get_compatible_with_portable") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:license"], + default_visibility = [ + "//visibility:public", + ], + licenses = ["notice"], +) + +# LINT.IfChange(cc_library_padding) +cc_library( + name = "padding", + srcs = [], + hdrs = ["padding.h"], + compatible_with = get_compatible_with_portable(), + visibility = [ + "//tensorflow/compiler/mlir/quantization/tensorflow/utils:__pkg__", + ], +) +# LINT.ThenChange(//tensorflow/lite/kernels/BUILD) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/kernels/padding.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/kernels/padding.h new file mode 100644 index 0000000000000000000000000000000000000000..7ab114886c6b13f97ef10119e83ea71259d807c4 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/kernels/padding.h @@ -0,0 +1,63 @@ +/* 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_KERNELS_PADDING_H_ +#define TENSORFLOW_COMPILER_MLIR_KERNELS_PADDING_H_ + +typedef enum { + kTfLitePaddingUnknown = 0, + kTfLitePaddingSame, + kTfLitePaddingValid, +} TfLitePadding; + +// LINT.IfChange +namespace tflite_migration { + +// Matching GetWindowedOutputSize in TensorFlow. +inline int ComputeOutSize(TfLitePadding padding, int image_size, + int filter_size, int stride, int dilation_rate = 1) { + int effective_filter_size = (filter_size - 1) * dilation_rate + 1; + + // TODO(b/186448822): This uses 0 since the function has no other way to + // report error case + if (stride == 0) return 0; + + switch (padding) { + case kTfLitePaddingSame: + return (image_size + stride - 1) / stride; + case kTfLitePaddingValid: + return (image_size + stride - effective_filter_size) / stride; + default: + return 0; + } +} + +// It's not guaranteed that padding is symmetric. It's important to keep +// offset for algorithms need all paddings. +inline int ComputePaddingWithOffset(int stride, int dilation_rate, int in_size, + int filter_size, int out_size, + int* offset) { + int effective_filter_size = (filter_size - 1) * dilation_rate + 1; + int total_padding = + ((out_size - 1) * stride + effective_filter_size - in_size); + total_padding = total_padding > 0 ? total_padding : 0; + *offset = total_padding % 2; + return total_padding / 2; +} + +} // namespace tflite_migration + +// LINT.ThenChange(//tensorflow/lite/kernels/padding.h) + +#endif // TENSORFLOW_COMPILER_MLIR_KERNELS_PADDING_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..93d2d48f1e4cbce7724025f63853af363d9fbdee --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/BUILD @@ -0,0 +1,2341 @@ +load("@bazel_skylib//lib:selects.bzl", "selects") +load("@bazel_skylib//rules:build_test.bzl", "build_test") +load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") +load("@llvm-project//mlir:tblgen.bzl", "gentbl_cc_library", "td_library") +load("@rules_python//python:proto.bzl", "py_proto_library") +load("//tensorflow:tensorflow.bzl", "if_google", "if_oss", "tf_cc_binary", "tf_cc_test", "tf_native_cc_binary") +load("//tensorflow:tensorflow.default.bzl", "filegroup", "get_compatible_with_portable") +load("//tensorflow/compiler/mlir/lite:build_def.bzl", "tflite_copts_warnings") +load( + "//tensorflow/core/platform:build_config.bzl", + "tf_proto_library", +) +load("//tensorflow/core/platform:rules_cc.bzl", "cc_library") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:license"], + default_visibility = ["//visibility:public"], + licenses = ["notice"], +) + +package_group( + name = "friends", + packages = [ + "//learning/brain/experimental/mlir/tflite/tfmrt/...", + "//learning/brain/mlir/...", + "//learning/brain/mlir/mlir_lsp_server/...", + "//learning/brain/research/babelfish/inference/speech_tflite/mlir/...", + "//research/language_modeling/sentence_explorer/ondevice/...", + "//tensorflow/compiler/mlir/...", + "//tensorflow/lite/...", + "//third_party/iree/...", + "//third_party/odml/infra/...", + "//third_party/odml/litert/...", + "//waymo/accelerator/alpine/tools/...", + "//waymo/ml/compiler/mlir/...", + ], +) + +exports_files(glob([ + "testdata/*.bin", +])) + +filegroup( + name = "tflite_internal_cc_3p_api_deps_src", + srcs = [ + "allocation.cc", + "allocation.h", + "mmap_allocation.cc", + ], + visibility = ["//tensorflow/lite:__pkg__"], +) + +td_library( + name = "tensorflow_lite_ops_td_files", + srcs = [ + "ir/tfl_op_enums.td", + "ir/tfl_op_interfaces.td", + "ir/tfl_ops.td", + ], + compatible_with = get_compatible_with_portable(), + deps = [ + "//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib:quantization_td_files", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_ops_td_files", + "@llvm-project//mlir:FuncTdFiles", + "@llvm-project//mlir:InferTypeOpInterfaceTdFiles", + "@llvm-project//mlir:LoopLikeInterfaceTdFiles", + "@llvm-project//mlir:OpBaseTdFiles", + "@llvm-project//mlir:SideEffectInterfacesTdFiles", + ], +) + +td_library( + name = "tensorflow_lite_patterns_td_files", + srcs = [ + "transforms/legalize_patterns.td", + "transforms/legalize_variables.td", + "transforms/optimize_batch_matmul.td", + "transforms/optimize_broadcast_like_patterns.td", + "transforms/optimize_patterns.td", + "transforms/post_quantize_patterns.td", + "transforms/prepare_patterns.td", + "transforms/quantization/strict_quantize_patterns.td", + "transforms/quantize_by_converter_patterns.td", + "transforms/quantize_patterns.td", + "transforms/tensorlist_patterns.td", + "utils/utils.td", + ], + compatible_with = get_compatible_with_portable(), + visibility = ["//visibility:private"], + deps = [ + ":tensorflow_lite_ops_td_files", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_ops_td_files", + "//tensorflow/compiler/mlir/tensorflow/transforms:tensorflow_optimize_td_files", + "@llvm-project//mlir:ArithOpsTdFiles", + "@llvm-project//mlir:FuncTdFiles", + "@llvm-project//mlir:OpBaseTdFiles", + ], +) + +gentbl_cc_library( + name = "tensorflow_lite_passes_inc_gen", + compatible_with = get_compatible_with_portable(), + tbl_outs = {"transforms/passes.h.inc": [ + "-gen-pass-decls", + "-name=TensorFlowLiteTd", + ]}, + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "transforms/passes.td", + deps = [ + "@llvm-project//mlir:PassBaseTdFiles", + ], +) + +gentbl_cc_library( + name = "tensorflow_lite_ops_inc_gen", + compatible_with = get_compatible_with_portable(), + tbl_outs = { + "ir/tfl_ops.h.inc": ["-gen-op-decls"], + "ir/tfl_ops.cc.inc": ["-gen-op-defs"], + "g3doc/tfl_ops.md": [ + "-gen-dialect-doc", + "-dialect=tfl", + ], + }, + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "ir/tfl_ops.td", + deps = [ + ":tensorflow_lite_ops_td_files", + ], +) + +gentbl_cc_library( + name = "tensorflow_lite_op_interfaces_inc_gen", + compatible_with = get_compatible_with_portable(), + tbl_outs = { + "ir/tfl_ops_interface.h.inc": ["-gen-op-interface-decls"], + "ir/tfl_ops_interface.cc.inc": ["-gen-op-interface-defs"], + "ir/tfl_ops_dialect.h.inc": ["-gen-dialect-decls"], + "ir/tfl_ops_dialect.cc.inc": ["-gen-dialect-defs"], + }, + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "ir/tfl_op_interfaces.td", + deps = [ + ":tensorflow_lite_ops_td_files", + ], +) + +gentbl_cc_library( + name = "tensorflow_lite_op_enums_inc_gen", + compatible_with = get_compatible_with_portable(), + tbl_outs = { + "ir/tfl_ops_enums.h.inc": ["-gen-enum-decls"], + "ir/tfl_ops_enums.cc.inc": ["-gen-enum-defs"], + "ir/tfl_ops_attrdefs.h.inc": ["-gen-attrdef-decls"], + "ir/tfl_ops_attrdefs.cc.inc": ["-gen-attrdef-defs"], + }, + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "ir/tfl_op_enums.td", + deps = [ + ":tensorflow_lite_ops_td_files", + ], +) + +gentbl_cc_library( + name = "tensorflow_lite_prepare_tf_inc_gen", + compatible_with = get_compatible_with_portable(), + tbl_outs = {"transforms/generated_prepare_tf.inc": ["-gen-rewriters"]}, + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "transforms/prepare_patterns.td", + deps = [":tensorflow_lite_patterns_td_files"], +) + +gentbl_cc_library( + name = "tensorflow_lite_lower_static_tensor_list_inc_gen", + compatible_with = get_compatible_with_portable(), + tbl_outs = {"transforms/generated_lower_static_tensor_list.inc": ["-gen-rewriters"]}, + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "transforms/tensorlist_patterns.td", + deps = [":tensorflow_lite_patterns_td_files"], +) + +gentbl_cc_library( + name = "tensorflow_lite_legalize_tf_inc_gen", + compatible_with = get_compatible_with_portable(), + tbl_outs = {"transforms/generated_legalize_tf.inc": ["-gen-rewriters"]}, + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "transforms/legalize_patterns.td", + deps = [":tensorflow_lite_patterns_td_files"], +) + +gentbl_cc_library( + name = "tensorflow_lite_legalize_variables_inc_gen", + compatible_with = get_compatible_with_portable(), + tbl_outs = {"transforms/generated_legalize_variables.inc": ["-gen-rewriters"]}, + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "transforms/legalize_variables.td", + deps = [":tensorflow_lite_patterns_td_files"], +) + +gentbl_cc_library( + name = "tensorflow_lite_optimize_inc_gen", + compatible_with = get_compatible_with_portable(), + tbl_outs = {"transforms/generated_optimize.inc": ["-gen-rewriters"]}, + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "transforms/optimize_patterns.td", + deps = [":tensorflow_lite_patterns_td_files"], +) + +gentbl_cc_library( + name = "tensorflow_lite_optimize_batch_matmul_inc_gen", + compatible_with = get_compatible_with_portable(), + tbl_outs = {"transforms/generated_optimize_batch_matmul.inc": ["-gen-rewriters"]}, + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "transforms/optimize_batch_matmul.td", + deps = [":tensorflow_lite_patterns_td_files"], +) + +gentbl_cc_library( + name = "optimize_broadcast_like_inc_gen", + compatible_with = get_compatible_with_portable(), + tbl_outs = {"transforms/generated_optimize_broadcast_like.inc": ["-gen-rewriters"]}, + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "transforms/optimize_broadcast_like_patterns.td", + deps = [":tensorflow_lite_patterns_td_files"], +) + +gentbl_cc_library( + name = "tensorflow_lite_quantize_inc_gen", + compatible_with = get_compatible_with_portable(), + tbl_outs = {"transforms/generated_quantize.inc": ["-gen-rewriters"]}, + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "transforms/quantize_patterns.td", + deps = [":tensorflow_lite_patterns_td_files"], +) + +gentbl_cc_library( + name = "tensorflow_lite_strict_quantize_inc_gen", + compatible_with = get_compatible_with_portable(), + tbl_outs = {"transforms/quantization/generated_strict_quantize.inc": ["-gen-rewriters"]}, + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "transforms/quantization/strict_quantize_patterns.td", + deps = [":tensorflow_lite_patterns_td_files"], +) + +gentbl_cc_library( + name = "tensorflow_lite_quantize_by_converter_inc_gen", + compatible_with = get_compatible_with_portable(), + tbl_outs = {"transforms/generated_quantize_by_converter.inc": ["-gen-rewriters"]}, + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "transforms/quantize_by_converter_patterns.td", + deps = [":tensorflow_lite_patterns_td_files"], +) + +gentbl_cc_library( + name = "tensorflow_lite_post_quantize_inc_gen", + compatible_with = get_compatible_with_portable(), + tbl_outs = {"transforms/generated_post_quantize.inc": ["-gen-rewriters"]}, + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "transforms/post_quantize_patterns.td", + deps = [":tensorflow_lite_patterns_td_files"], +) + +gentbl_cc_library( + name = "tensorflow_lite_legalize_tensorlist_inc_gen", + compatible_with = get_compatible_with_portable(), + tbl_outs = {"transforms/generated_legalize_tensorlist.inc": ["-gen-rewriters"]}, + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "transforms/legalize_tensorlist.td", + deps = [":tensorflow_lite_patterns_td_files"], +) + +cc_library( + name = "validators", + srcs = [ + "utils/validators.cc", + ], + hdrs = [ + "utils/validators.h", + ], + deps = [ + "@llvm-project//mlir:Dialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + ], +) + +cc_library( + name = "stateful_error_reporter", + hdrs = ["stateful_error_reporter.h"], + compatible_with = get_compatible_with_portable(), + deps = ["//tensorflow/compiler/mlir/lite/core/api:error_reporter"], +) + +gentbl_cc_library( + name = "tensorflow_lite_canonicalize_inc_gen", + compatible_with = get_compatible_with_portable(), + tbl_outs = {"ir/tfl_canonicalize.inc": ["-gen-rewriters"]}, + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "ir/tfl_canonicalize.td", + deps = [":tensorflow_lite_patterns_td_files"], +) + +cc_library( + name = "register_lite_dialects", + testonly = True, + srcs = ["register_lite_dialects.cc"], + hdrs = ["register_lite_dialects.h"], + deps = [ + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/lite/quantization/ir:QuantOps", + "@llvm-project//mlir:IR", + ], +) + +tf_cc_test( + name = "register_lite_dialects_test", + srcs = ["register_lite_dialects_test.cc"], + deps = [ + ":register_lite_dialects", + "@com_google_googletest//:gtest_main", + "@llvm-project//mlir:IR", + ], +) + +# Don't add dependencies to this binary. It's only a wrapper around litert_mlir_opt_main. +tf_cc_binary( + name = "litert-opt", + testonly = True, + deps = [ + ":litert_mlir_opt_main", + ], +) + +cc_library( + name = "litert_mlir_opt_main", + testonly = True, + srcs = ["litert_mlir_opt_main.cc"], + deps = [ + ":register_lite_dialects", + ":tensorflow_lite", + ":tf_tfl_passes", # buildcleaner:keep + "//tensorflow/compiler/mlir:init_mlir", + "//tensorflow/compiler/mlir:passes", + "//tensorflow/compiler/mlir:register_common_dialects", + "//tensorflow/compiler/mlir/lite:lift_tflite_flex_ops", + "//tensorflow/compiler/mlir/lite/quantization/ir:QuantOps", + "//tensorflow/compiler/mlir/tensorflow/transforms:tensorflow_passes", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:AllPassesAndDialects", + "@llvm-project//mlir:MlirOptLib", + "@llvm-project//mlir:RegisterAllDialects", + "@llvm-project//mlir:RegisterAllPasses", + ], +) + +cc_library( + name = "utils", + hdrs = ["utils/utils.h"], + deps = [ + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:Dialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + ], +) + +tf_cc_test( + name = "utils_test", + srcs = ["utils/utils_test.cc"], + deps = [ + ":utils", + "@com_google_googletest//:gtest_main", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + ], +) + +cc_library( + name = "attribute_utils", + srcs = ["utils/attribute_utils.cc"], + hdrs = ["utils/attribute_utils.h"], + deps = [ + "@llvm-project//llvm:Support", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + ], +) + +cc_library( + name = "pass_options", + hdrs = ["transforms/pass_options.h"], + compatible_with = get_compatible_with_portable(), + visibility = ["//visibility:public"], + deps = [ + "@llvm-project//mlir:Pass", + ], +) + +cc_library( + name = "pass", + hdrs = ["transforms/pass.h"], + compatible_with = get_compatible_with_portable(), + visibility = ["//visibility:public"], + deps = [ + ":pass_options", + ":pass_options_setter", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + ], +) + +cc_library( + name = "pipeline", + hdrs = ["transforms/pipeline.h"], + compatible_with = get_compatible_with_portable(), + visibility = ["//visibility:public"], + deps = [ + ":pass", + ":pass_options", + ":pass_options_setter", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + ], +) + +cc_library( + name = "pass_options_setter", + hdrs = ["transforms/pass_options_setter.h"], + compatible_with = get_compatible_with_portable(), + visibility = ["//visibility:public"], + deps = [ + "@llvm-project//llvm:Support", + ], +) + +cc_library( + name = "converter_pass_options_setter", + srcs = ["transforms/converter_pass_options_setter.cc"], + hdrs = ["transforms/converter_pass_options_setter.h"], + compatible_with = get_compatible_with_portable(), + visibility = ["//visibility:public"], + deps = [ + ":common", + ":converter_flags_proto_cc", + ":optimize_broadcast_like_pass_options", + ":optimize_pass_options", + ":pass_options", + ":pass_options_setter", + ":variable_freezing_pipeline_options", + "@llvm-project//llvm:Support", + ], +) + +cc_library( + name = "tensorflow_lite_ops", + srcs = [ + "ir/tfl_ops.cc", + "ir/tfl_ops.cc.inc", + "ir/tfl_ops.h.inc", + "ir/tfl_ops_attrdefs.h.inc", + "ir/tfl_ops_dialect.cc.inc", + "ir/tfl_ops_dialect.h.inc", + "ir/tfl_ops_enums.cc.inc", + "ir/tfl_ops_enums.h.inc", + "ir/tfl_ops_interface.cc.inc", + "ir/tfl_ops_interface.h.inc", + "runtime_verifiers.inc", + ], + hdrs = [ + "ir/tfl_ops.h", + ], + deps = [ + ":attribute_utils", + ":converter_inc", + ":cost_estimators", + ":shape_and_size_utils", + ":tensorflow_lite_canonicalize_inc_gen", + ":tensorflow_lite_op_enums_inc_gen", + ":tensorflow_lite_op_interfaces_inc_gen", + ":tensorflow_lite_ops_inc_gen", + ":utils", + "//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib", + "//tensorflow/compiler/mlir/lite/quantization/ir:QuantOps", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs", + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/compiler/mlir/tensorflow:dynamic_shape_utils", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_op_interfaces", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_ops", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_traits", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_types", + "//tensorflow/core:framework", + "//tensorflow/core/platform:status", + "@com_google_absl//absl/base:core_headers", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/synchronization", + "@eigen_archive//:eigen3", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:ControlFlowInterfaces", + "@llvm-project//mlir:DerivedAttributeOpInterface", + "@llvm-project//mlir:Dialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:InferTypeOpInterface", + "@llvm-project//mlir:InliningUtils", + "@llvm-project//mlir:LoopLikeInterface", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:SideEffectInterfaces", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + ], +) + +cc_library( + name = "pass_registry_utils", + hdrs = ["transforms/pass_registry_utils.h"], + compatible_with = get_compatible_with_portable(), + visibility = ["//visibility:public"], + deps = [ + ":pass_options", + ":pipeline", + "@llvm-project//mlir:Pass", + ], +) + +cc_library( + name = "variable_freezing_pipeline_options", + hdrs = ["transforms/variable_freezing_pipeline_options.h"], + deps = [ + "@llvm-project//llvm:Support", + "@llvm-project//mlir:Pass", + ], +) + +cc_library( + name = "variable_freezing_pipeline", + srcs = ["transforms/variable_freezing_pipeline.cc"], + hdrs = ["transforms/variable_freezing_pipeline.h"], + compatible_with = get_compatible_with_portable(), + visibility = ["//visibility:public"], + deps = [ + ":pass_registry_utils", + ":pipeline", + ":tensorflow_lite_tf_unfreeze_global_tensors", + ":variable_freezing_pipeline_options", + "//tensorflow/compiler/mlir/tensorflow/transforms:tf_saved_model_passes", + "@llvm-project//mlir:Support", + ], +) + +cc_library( + name = "tensorflow_lite", + srcs = [ + "ir/tfl_canonicalize.inc", + ], + hdrs = [ + "ir/tfl_ops.h", + "transforms/canonicalize_boundary_value_pass.h", + "transforms/cleanup_optimization_barrier_pass.h", + "transforms/optimize_batch_matmul_pass.h", + "transforms/optimize_broadcast_like_pass.h", + "transforms/optimize_pass.h", + "transforms/pass_registry_utils.h", + "transforms/passes.h", + "transforms/push_transpose_through_ewise_pass.h", + "transforms/tf_legalizations/analyze_variables_pass.h", + "transforms/tf_legalizations/legalize_tensorlist_pass.h", + "transforms/tf_legalizations/while_loop_outline_pass.h", + "transforms/tflite_passes/split_merged_operands_pass.h", + "transforms/tflite_passes/unfold_large_splat_constants_pass.h", + "transforms/unfreeze_global_constants.h", + "utils/attribute_utils.h", + "utils/utils.h", + ], + deps = [ + ":attribute_utils", + ":canonicalize_boundary_value", + ":cleanup_optimization_barrier", + ":converter_inc", + ":cost_estimators", + ":downcast_x64", + ":elements_attr_roundtrip_pass", + ":optimize_broadcast_like_pass", + ":optimize_broadcast_like_pass_options", + ":optimize_pass_options", + ":pass", + ":pass_options", + ":pass_registry_utils", + ":pipeline", + ":shape_and_size_utils", + ":tensorflow_lite_canonicalize_inc_gen", + ":tensorflow_lite_legalize_tf_analyze_variables", + ":tensorflow_lite_legalize_tf_legalize_tensorlist", + ":tensorflow_lite_legalize_tf_while_loop_outline", + ":tensorflow_lite_op_enums_inc_gen", + ":tensorflow_lite_op_interfaces_inc_gen", + ":tensorflow_lite_ops", + ":tensorflow_lite_ops_inc_gen", + ":tensorflow_lite_optimize", + ":tensorflow_lite_passes_inc_gen", + ":tensorflow_lite_push_transpose_through_ewise_pass", + ":tensorflow_lite_split_merged_operands", + ":tensorflow_lite_tf_unfreeze_global_tensors", + ":tensorflow_lite_unfold_large_splat_constants", + "//tensorflow/compiler/mlir:op_or_arg_name_mapper", + "//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib", + "//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib:quantization_config", + "//tensorflow/compiler/mlir/lite/quantization/ir:QuantOps", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs", + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/compiler/mlir/tensorflow:dynamic_shape_utils", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_op_interfaces", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_ops", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_traits", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_types", + "//tensorflow/core:framework", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@eigen_archive//:eigen3", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:DerivedAttributeOpInterface", + "@llvm-project//mlir:Dialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:InferTypeOpInterface", + "@llvm-project//mlir:InliningUtils", + "@llvm-project//mlir:LoopLikeInterface", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:SideEffectInterfaces", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@stablehlo//:stablehlo_ops", + ], +) + +cc_library( + name = "variables_utils", + srcs = [ + "utils/variables_utils.cc", + ], + hdrs = [ + "utils/variables_utils.h", + ], + deps = [ + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_types", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:QuantOps", + ], +) + +cc_library( + name = "shape_and_size_utils", + srcs = [ + "utils/shape_and_size_utils.cc", + ], + hdrs = [ + "utils/shape_and_size_utils.h", + ], + deps = [ + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + ], +) + +cc_library( + name = "mlir_module_utils", + hdrs = ["utils/mlir_module_utils.h"], + deps = [ + ":const_tensor_utils", + "//tensorflow/compiler/mlir/tensorflow", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + ], +) + +tf_cc_test( + name = "shape_and_size_utils_test", + size = "small", + srcs = ["utils/shape_and_size_utils_test.cc"], + deps = [ + ":shape_and_size_utils", + "//tensorflow/core:test", + "//tensorflow/core:test_main", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@llvm-project//mlir:IR", + ], +) + +cc_library( + name = "cost_estimators", + hdrs = [ + "utils/arithmetic_count_util.h", + ], + deps = [ + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + ], +) + +cc_library( + name = "constant_utils", + srcs = [ + "utils/constant_utils.cc", + ], + hdrs = [ + "utils/constant_utils.h", + ], + deps = [ + "//tensorflow/compiler/mlir/tensorflow:mangling_util", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_attributes", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core/platform:status", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:BytecodeOpInterface", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@tsl//tsl/platform:statusor", + ], +) + +cc_library( + name = "elements_attr_roundtrip_pass", + srcs = [ + "transforms/utilities/elements_attr_roundtrip_pass.cc", + ], + hdrs = [ + "transforms/utilities/elements_attr_roundtrip_pass.h", + ], + deps = [ + ":pass", + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_types", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + ], +) + +cc_library( + name = "low_bit_utils", + srcs = [ + "utils/low_bit_utils.cc", + ], + hdrs = [ + "utils/low_bit_utils.h", + ], + deps = [ + "@com_google_absl//absl/functional:function_ref", + "@com_google_absl//absl/status", + "@com_google_absl//absl/strings:string_view", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:Support", + ], +) + +tf_cc_test( + name = "low_bit_utils_test", + size = "small", + srcs = ["utils/low_bit_utils_test.cc"], + deps = [ + ":low_bit_utils", + "//tensorflow/core:test", + "//tensorflow/core:test_main", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:status_matchers", + "@com_google_absl//absl/strings:string_view", + "@com_google_googletest//:gtest", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + ], +) + +cc_library( + name = "lstm_utils", + srcs = [ + "utils/lstm_utils.cc", + ], + hdrs = [ + "utils/lstm_utils.h", + ], + deps = [ + ":tensorflow_lite", + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/compiler/mlir/tensorflow:dynamic_shape_utils", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TensorDialect", + ], +) + +cc_library( + name = "metadata_utils", + hdrs = [ + "utils/metadata_utils.h", + ], +) + +cc_library( + name = "fake_quant_utils", + srcs = [ + "utils/fake_quant_utils.cc", + ], + hdrs = [ + "utils/fake_quant_utils.h", + ], + deps = [ + ":tensorflow_lite", + "//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib", + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_ops", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + ], +) + +cc_library( + name = "nms_utils", + srcs = [ + "utils/nms_utils.cc", + ], + hdrs = [ + "utils/nms_utils.h", + ], + deps = [ + ":tensorflow_lite", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_attributes", + "@flatbuffers", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + ], +) + +cc_library( + name = "tftext_utils", + srcs = [ + "utils/tftext_utils.cc", + ], + hdrs = [ + "utils/tftext_utils.h", + ], + deps = [ + ":tensorflow_lite", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_attributes", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_types", + "//tensorflow/core:framework", + "//tensorflow/core/ir/types:Dialect", + "@flatbuffers", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + ], +) + +tf_cc_test( + name = "tftext_utils_test", + size = "small", + srcs = ["utils/tftext_utils_test.cc"], + deps = [ + ":tftext_utils", + "//tensorflow/core:framework", + "//tensorflow/core:test", + "//tensorflow/core:test_main", + "@com_google_absl//absl/status", + "@tsl//tsl/platform:status", + ], +) + +cc_library( + name = "perception_ops_utils", + srcs = [ + "utils/perception_ops_utils.cc", + ], + hdrs = [ + "utils/perception_ops_utils.h", + ], + deps = [ + ":tensorflow_lite", + "//tensorflow/compiler/mlir/lite/core/c:tflite_common", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_attributes", + "@flatbuffers//:runtime_cc", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + ], +) + +cc_library( + name = "stateful_ops_utils", + srcs = [ + "utils/stateful_ops_utils.cc", + ], + hdrs = [ + "utils/stateful_ops_utils.h", + ], + deps = [ + ":tensorflow_lite_ops", + ":utils", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + ], +) + +tf_cc_test( + name = "lstm_utils_test", + size = "small", + srcs = ["utils/lstm_utils_test.cc"], + deps = [ + ":lstm_utils", + ":tensorflow_lite", + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/core:test", + "//tensorflow/core:test_main", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TensorDialect", + ], +) + +tf_cc_test( + name = "perception_ops_utils_test", + size = "small", + srcs = ["utils/perception_ops_utils_test.cc"], + deps = [ + ":perception_ops_utils", + ":tensorflow_lite", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_attributes", + "//tensorflow/core:test", + "//tensorflow/core:test_main", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + ], +) + +cc_library( + name = "canonicalize_boundary_value", + srcs = [ + "transforms/canonicalize_boundary_value_pass.cc", + ], + hdrs = [ + "transforms/canonicalize_boundary_value_pass.h", + ], + deps = [ + ":pass", + ":utils", + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_types", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@stablehlo//:stablehlo_ops", + ], +) + +cc_library( + name = "cleanup_optimization_barrier", + srcs = [ + "transforms/cleanup_optimization_barrier_pass.cc", + ], + hdrs = [ + "transforms/cleanup_optimization_barrier_pass.h", + ], + deps = [ + ":pass", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_types", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@stablehlo//:stablehlo_ops", + ], +) + +cc_library( + name = "downcast_x64", + srcs = [ + "transforms/downcast_x64_pass.cc", + ], + hdrs = [ + "transforms/downcast_x64_pass.h", + ], + deps = [ + ":pass", + ":pass_options", + ":tensorflow_lite_ops", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + ], +) + +cc_library( + name = "tensorflow_lite_legalize_tf_analyze_variables", + srcs = [ + "transforms/tf_legalizations/analyze_variables_pass.cc", + ], + hdrs = [ + "transforms/tf_legalizations/analyze_variables_pass.h", + ], + deps = [ + ":pass", + ":tensorflow_lite_ops", + "//tensorflow/compiler/mlir/tensorflow", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + ], +) + +cc_library( + name = "tensorflow_lite_legalize_tf_legalize_tensorlist", + srcs = [ + "transforms/tf_legalizations/legalize_tensorlist_pass.cc", + ], + hdrs = [ + "transforms/tf_legalizations/legalize_tensorlist_pass.h", + ], + deps = [ + ":convert_type", + ":pass", + ":tensorflow_lite_legalize_tensorlist_inc_gen", + ":tensorflow_lite_ops", + ":utils", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs", + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_ops", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_types", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + ], +) + +cc_library( + name = "tensorflow_lite_legalize_tf_while_loop_outline", + srcs = [ + "transforms/tf_legalizations/while_loop_outline_pass.cc", + ], + hdrs = [ + "transforms/tf_legalizations/while_loop_outline_pass.h", + ], + deps = [ + ":pass", + ":tensorflow_lite_ops", + "//tensorflow/compiler/mlir:op_or_arg_name_mapper", + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_ops", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + ], +) + +cc_library( + name = "tensorflow_lite_unfold_large_splat_constants", + srcs = [ + "transforms/tflite_passes/unfold_large_splat_constants_pass.cc", + ], + hdrs = [ + "transforms/tflite_passes/unfold_large_splat_constants_pass.h", + ], + deps = [ + ":pass", + ":tensorflow_lite_ops", + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/compiler/mlir/tensorflow:dynamic_shape_utils", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_ops", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + ], +) + +cc_library( + name = "tensorflow_lite_split_merged_operands", + srcs = [ + "transforms/tflite_passes/split_merged_operands_pass.cc", + ], + hdrs = [ + "transforms/tflite_passes/split_merged_operands_pass.h", + ], + deps = [ + ":pass", + ":pass_options", + ":stateful_ops_utils", + ":tensorflow_lite_ops", + ":utils", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + ], +) + +cc_library( + name = "tensorflow_lite_legalize_tf", + srcs = [ + "transforms/dilated_conv.cc", + "transforms/generated_legalize_tf.inc", + "transforms/generated_legalize_variables.inc", + "transforms/generated_lower_static_tensor_list.inc", + "transforms/generated_prepare_tf.inc", + "transforms/get_arithmetic_count.cc", + "transforms/gpu_compatibility.cc", + "transforms/if_outline.cc", + "transforms/insert_call_once_op.cc", + "transforms/legalize_hashtables.cc", + "transforms/legalize_jax_random.cc", + "transforms/legalize_tf.cc", + "transforms/legalize_tf_while.cc", + "transforms/legalize_variables.cc", + "transforms/lower_static_tensor_list.cc", + "transforms/optimize_functional_ops.cc", + "transforms/partitioned_topological_sort.cc", + "transforms/pin_ops_with_side_effects.cc", + "transforms/prepare_composite_functions_tf.cc", + "transforms/prepare_tf.cc", + "transforms/raise_custom_ops.cc", + "transforms/rank_reduction.cc", + "transforms/reduce_type_precision.cc", + "transforms/reduce_while_operands.cc", + "transforms/runtime_verify.cc", + "transforms/trim_functions_tf.cc", + ], + hdrs = [ + "ir/tfl_ops_interface.h.inc", + "transforms/dilated_conv.h", + "transforms/passes.h", + ], + deps = [ + ":attribute_utils", + ":constant_utils", + ":convert_type", + ":cost_estimators", + ":downcast_x64", + ":elements_attr_roundtrip_pass", + ":fake_quant_utils", + ":lstm_utils", + ":nms_utils", + ":optimize_broadcast_like_pass_options", + ":perception_ops_utils", + ":shape_and_size_utils", + ":stateful_ops_utils", + ":tensorflow_lite", + ":tensorflow_lite_legalize_tf_analyze_variables", + ":tensorflow_lite_legalize_tf_inc_gen", + ":tensorflow_lite_legalize_tf_legalize_tensorlist", + ":tensorflow_lite_legalize_variables_inc_gen", + ":tensorflow_lite_lower_static_tensor_list_inc_gen", + ":tensorflow_lite_passes_inc_gen", + ":tensorflow_lite_prepare_tf_inc_gen", + ":tensorflow_lite_tf_unfreeze_global_tensors", + ":tftext_utils", + ":validators", + ":variables_utils", + "//tensorflow/compiler/mlir:op_or_arg_name_mapper", + "//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib", + "//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib:quantization_config", + "//tensorflow/compiler/mlir/lite/quantization/ir:QuantOps", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs", + "//tensorflow/compiler/mlir/lite/stablehlo:optimize_layout", + "//tensorflow/compiler/mlir/lite/stablehlo:prepare_hlo", + "//tensorflow/compiler/mlir/lite/stablehlo:tf_legalize_hlo", + "//tensorflow/compiler/mlir/lite/stablehlo:tfl_legalize_chlo", + "//tensorflow/compiler/mlir/lite/stablehlo:tfl_legalize_hlo", + "//tensorflow/compiler/mlir/quantization/common/ir:QuantOps", + "//tensorflow/compiler/mlir/stablehlo:legalize_tf", + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/compiler/mlir/tensorflow:convert_tensor", + "//tensorflow/compiler/mlir/tensorflow:dynamic_shape_utils", + "//tensorflow/compiler/mlir/tensorflow:error_util", + "//tensorflow/compiler/mlir/tensorflow:mangling_util", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_attributes", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_ops", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_types", + "//tensorflow/compiler/mlir/tensorflow:verification_utils", + "//tensorflow/compiler/mlir/tensorflow/transforms:lower_tf_lib", + "//tensorflow/compiler/mlir/tensorflow/transforms:tensorflow_passes", + "//tensorflow/compiler/mlir/tensorflow/transforms:unroll_batch_matmul_pass", + "//tensorflow/compiler/mlir/tf2xla/transforms:xla_legalize_tf", + "//tensorflow/compiler/mlir/tf2xla/transforms:xla_legalize_tf_with_tf2xla", + "//tensorflow/core:framework", + "//tensorflow/core:lib", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core/kernels:tensor_list", + "@com_google_absl//absl/algorithm:container", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/container:inlined_vector", + "@com_google_absl//absl/memory", + "@com_google_absl//absl/numeric:bits", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:AffineAnalysis", + "@llvm-project//mlir:Analysis", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:LoopLikeInterface", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:SideEffectInterfaces", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@llvm-project//mlir:Transforms", + "@stablehlo//:stablehlo_ops", + "@xla//xla/mlir_hlo", + "@xla//xla/mlir_hlo:type_conversion", + "@xla//xla/mlir_hlo:unfuse_batch_norm", + ], +) + +cc_library( + name = "tensorflow_lite_optimize", + srcs = [ + "transforms/generated_optimize.inc", + "transforms/optimize_pass.cc", + ], + hdrs = [ + "transforms/optimize_pass.h", + ], + deps = [ + ":attribute_utils", + ":constant_utils", + ":convert_type", + ":optimize_pass_options", + ":pass", + ":tensorflow_lite_ops", + ":tensorflow_lite_optimize_inc_gen", + ":utils", + ":validators", + "//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib", + "//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib:quantization_config", + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/compiler/mlir/tensorflow:dynamic_shape_utils", + "//tensorflow/compiler/mlir/tensorflow:verification_utils", + "@com_google_absl//absl/container:flat_hash_set", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + ], +) + +cc_library( + name = "optimize_batch_matmul_utils", + srcs = ["transforms/tflite_passes/optimize_batch_matmul_utils.cc"], + hdrs = ["transforms/tflite_passes/optimize_batch_matmul_utils.h"], + deps = [ + ":utils", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + ], +) + +tf_cc_test( + name = "optimize_batch_matmul_utils_test", + srcs = ["transforms/tflite_passes/optimize_batch_matmul_utils_test.cc"], + deps = [ + ":optimize_batch_matmul_utils", + "@com_google_googletest//:gtest_main", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:IR", + ], +) + +cc_library( + name = "tensorflow_lite_optimize_batch_matmul", + srcs = [ + "transforms/generated_optimize_batch_matmul.inc", + "transforms/optimize_batch_matmul_pass.cc", + ], + hdrs = [ + "transforms/optimize_batch_matmul_pass.h", + ], + deps = [ + ":convert_type", + ":optimize_batch_matmul_utils", + ":pass", + ":pass_options", + ":tensorflow_lite_ops", + ":tensorflow_lite_optimize_batch_matmul_inc_gen", + ":tensorflow_lite_passes_inc_gen", + ":utils", + ":validators", + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/compiler/mlir/tensorflow:dynamic_shape_utils", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_types", + "//tensorflow/compiler/mlir/tensorflow:verification_utils", + "@com_google_absl//absl/container:flat_hash_set", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + ], +) + +cc_library( + name = "optimize_broadcast_like_pass", + srcs = [ + "transforms/optimize_broadcast_like_pass.cc", + ], + hdrs = [ + "transforms/optimize_broadcast_like_pass.h", + ], + deps = [ + ":optimize_broadcast_like_inc_gen", + ":optimize_broadcast_like_pass_options", + ":pass", + ":tensorflow_lite_ops", + ":utils", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:Dialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + ], +) + +cc_library( + name = "tensorflow_lite_push_transpose_through_ewise_pass", + srcs = [ + "transforms/push_transpose_through_ewise_pass.cc", + ], + hdrs = [ + "transforms/push_transpose_through_ewise_pass.h", + ], + deps = [ + ":pass", + ":tensorflow_lite_ops", + ":utils", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + ], +) + +cc_library( + name = "tensorflow_lite_tf_unfreeze_global_tensors", + srcs = [ + "transforms/unfreeze_global_constants.cc", + ], + hdrs = [ + "transforms/unfreeze_global_constants.h", + ], + deps = [ + ":pass", + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_ops", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_types", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + ], +) + +cc_library( + name = "tensorflow_lite_quantize", + srcs = [ + "transforms/decompose_hybrid_quantization.cc", + "transforms/default_quant_params.cc", + "transforms/generated_post_quantize.inc", + "transforms/generated_quantize.inc", + "transforms/lower_quant_annotations_helper.cc", + "transforms/lower_quant_annotations_pass.cc", + "transforms/modify_io_nodes.cc", + "transforms/optimize_op_order.cc", + "transforms/post_quantize.cc", + "transforms/prepare_quantize.cc", + "transforms/prepare_quantize_dynamic_range.cc", + "transforms/prepare_quantize_helper.cc", + "transforms/quantization/bias_quantizer_pass.cc", + "transforms/quantization/fuse_qdq_pass.cc", + "transforms/quantization/propagate_qsv_pass.cc", + "transforms/quantization/quant_utils.cc", + "transforms/quantize.cc", + "transforms/quantize_variables.cc", + "utils/generated_op_quant_spec_getters.inc", + ], + hdrs = [ + "transforms/lower_quant_annotations_helper.h", + "transforms/passes.h", + "transforms/prepare_quantize_helper.h", + "transforms/quantization/quant_utils.h", + ], + deps = [ + "convert_type", + ":downcast_x64", + ":elements_attr_roundtrip_pass", + ":op_quant_spec_getters_inc", + ":optimize_broadcast_like_pass_options", + ":shape_and_size_utils", + ":stateful_ops_utils", + ":tensorflow_lite", + ":tensorflow_lite_passes_inc_gen", + ":tensorflow_lite_post_quantize_inc_gen", + ":tensorflow_lite_quantize_by_converter_inc_gen", + ":tensorflow_lite_quantize_inc_gen", + ":tensorflow_lite_strict_quantize_inc_gen", + ":validators", + "//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib", + "//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib:quantization_config", + "//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib:tfl_quantization_driver", + "//tensorflow/compiler/mlir/lite/quantization/ir:QuantOps", + "//tensorflow/compiler/mlir/lite/quantization/lite:tfl_to_std", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs", + "//tensorflow/compiler/mlir/lite/tools/optimize:operator_property", + "//tensorflow/compiler/mlir/quantization/common:uniform_quantized_types", + "//tensorflow/compiler/mlir/quantization/common/ir:QuantOps", + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/compiler/mlir/tensorflow:dynamic_shape_utils", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_types", + "//tensorflow/core:lib", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core/platform:logging", + "@com_google_absl//absl/base:no_destructor", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/log:check", + "@com_google_absl//absl/memory", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@stablehlo//:stablehlo_ops", + "@xla//xla/mlir_hlo", + ], +) + +cc_library( + name = "tensorflow_lite_d2s", + srcs = [ + "transforms/dense_to_sparse_pass.cc", + ], + hdrs = [ + "transforms/dense_to_sparse_pass.h", + ], + deps = [ + ":pass", + ":pass_options", + ":tensorflow_lite_ops", + "//tensorflow/compiler/mlir/lite/kernels/internal/utils:sparsity_format_converter", + "@com_google_absl//absl/memory", + "@eigen_archive//:eigen3", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + ], +) + +cc_library( + name = "lift_tflite_flex_ops", + srcs = [ + "transforms/lift_tflite_flex_ops.cc", + ], + hdrs = [ + "transforms/lift_tflite_flex_ops.h", + ], + deps = [ + ":tensorflow_lite", + ":tensorflow_lite_passes_inc_gen", + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/compiler/mlir/tensorflow:convert_attr", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_attributes", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_ops", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_types", + "//tensorflow/core:framework", + "//tensorflow/core:lib", + "//tensorflow/core:protos_all_cc", + "@com_google_absl//absl/strings", + "@flatbuffers", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + ], +) + +filegroup( + name = "generated_op_quant_spec_getters", + srcs = [ + "utils/generated_op_quant_spec_getters.inc", + ], +) + +gentbl_cc_library( + name = "op_quant_spec_getters_inc", + compatible_with = get_compatible_with_portable(), + tbl_outs = {"utils/generated_op_quant_spec_getters.inc": []}, + tblgen = "//tensorflow/compiler/mlir/lite/quantization:op_quant_spec_getters_gen", + td_file = "ir/tfl_ops.td", + deps = [ + ":tensorflow_lite_ops_td_files", + ], +) + +gentbl_cc_library( + name = "tflite_op_coverage_spec_inc", + compatible_with = get_compatible_with_portable(), + tbl_outs = {"utils/tflite_op_coverage_spec.inc": []}, + tblgen = "//tensorflow/compiler/mlir/lite/quantization:tflite_op_coverage_spec_getters_gen", + td_file = "ir/tfl_ops.td", + visibility = ["//learning/brain/mobile/model_optimization/g3doc/autogen:__pkg__"], + deps = [ + ":tensorflow_lite_ops_td_files", + ], +) + +tf_native_cc_binary( + name = "converter-gen", + srcs = [ + "converter_gen.cc", + ], + compatible_with = get_compatible_with_portable(), + deps = [ + "@llvm-project//llvm:Support", + "@llvm-project//llvm:TableGen", + "@llvm-project//mlir:TableGen", + ], +) + +gentbl_cc_library( + name = "converter_inc", + compatible_with = get_compatible_with_portable(), + tbl_outs = { + "operator_converters.inc": ["--gen-operator-converters"], + "runtime_verifiers.inc": ["--gen-runtime-verifiers"], + }, + tblgen = ":converter-gen", + td_file = "ir/tfl_ops.td", + test = 1, + deps = [ + ":tensorflow_lite_ops_td_files", + "//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib:quantization_td_files", + ], +) + +cc_library( + name = "flatbuffer_tflite_operator_lib", + srcs = [ + "flatbuffer_operator.cc", + "operator_converters.inc", + ], + hdrs = [ + "flatbuffer_operator.h", + ], + deps = [ + ":convert_type", + ":converter_inc", + ":tensorflow_lite", + "//tensorflow/compiler/mlir/lite/core/c:tflite_common", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs_with_mutable", + "//tensorflow/compiler/mlir/lite/schema:schema_utils", + "//tensorflow/compiler/mlir/tensorflow:dynamic_shape_utils", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_types", + "//tensorflow/core/platform:errors", + "//tensorflow/core/platform:status", + "//tensorflow/core/platform:statusor", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@flatbuffers", + "@llvm-project//llvm:Analysis", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@stablehlo//:stablehlo_ops", + "@stablehlo//:vhlo_ops", + "@stablehlo//:vhlo_types", + "@tsl//tsl/platform:status", + ], +) + +tf_native_cc_binary( + name = "flatbuffer_to_string", + srcs = ["flatbuffer_to_string.cc"], + deps = [ + "//tensorflow/compiler/mlir/lite/core:absl_error_model_builder", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs_with_reflection", + "@flatbuffers", + ], +) + +tf_native_cc_binary( + name = "json_to_flatbuffer", + srcs = ["json_to_flatbuffer.cc"], + deps = [ + "//tensorflow/compiler/mlir/lite/schema:schema_fbs", + "@flatbuffers", + ], +) + +cc_library( + name = "flatbuffer_export", + srcs = [ + "flatbuffer_export.cc", + ], + hdrs = [ + "flatbuffer_export.h", + "flatbuffer_export_flags.h", + ], + deps = [ + ":const_tensor_utils", + ":control_edges", + ":convert_type", + ":converter_flags_proto_cc", + ":flatbuffer_tflite_operator_lib", + ":lite_version", + ":low_bit_utils", + ":metadata_utils", + ":mlir_module_utils", + ":region_isolation", + ":stateful_ops_utils", + ":string_utils", + ":tensorflow_lite", + "//tensorflow/compiler/mlir:op_or_arg_name_mapper", + "//tensorflow/compiler/mlir/lite/core:macros", + "//tensorflow/compiler/mlir/lite/core/c:tflite_common", + "//tensorflow/compiler/mlir/lite/delegates/flex:allowlisted_flex_ops_lib", + "//tensorflow/compiler/mlir/lite/experimental/remat:metadata_util", + "//tensorflow/compiler/mlir/lite/metrics:converter_error_data_proto_cc", + "//tensorflow/compiler/mlir/lite/metrics:error_collector_inst", + "//tensorflow/compiler/mlir/lite/quantization/ir:QuantOps", + "//tensorflow/compiler/mlir/lite/schema:debug_metadata_fbs_with_mutable", + "//tensorflow/compiler/mlir/lite/schema:schema_conversion_utils", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs_with_mutable", + "//tensorflow/compiler/mlir/lite/tools/versioning", + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/compiler/mlir/tensorflow:convert_tensor", + "//tensorflow/compiler/mlir/tensorflow:dynamic_shape_utils", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_types", + "//tensorflow/compiler/mlir/tensorflow:translate_utils", + "//tensorflow/core:framework", + "//tensorflow/core:portable_gif_internal", + "//tensorflow/core:protos_all_cc", + "@com_google_absl//absl/algorithm:container", + "@com_google_absl//absl/base:core_headers", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/functional:any_invocable", + "@com_google_absl//absl/functional:function_ref", + "@com_google_absl//absl/log", + "@com_google_absl//absl/log:check", + "@com_google_absl//absl/status", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/strings:str_format", + "@flatbuffers", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:Support", + "@stablehlo//:stablehlo_ops", + "@stablehlo//:vhlo_ops", + "@tsl//tsl/platform:tstring", + ], +) + +cc_library( + name = "flatbuffer_import", + srcs = [ + "flatbuffer_import.cc", + ], + hdrs = [ + "flatbuffer_import.h", + ], + deps = [ + ":const_tensor_utils", + ":control_edges", + ":convert_type", + ":flatbuffer_tflite_operator_lib", + ":metadata_utils", + ":offset_buffer", + ":shape_and_size_utils", + ":tensorflow_lite", + "//tensorflow/compiler/mlir/lite/core:absl_error_model_builder", + "//tensorflow/compiler/mlir/lite/experimental/remat:metadata_util", + "//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib", + "//tensorflow/compiler/mlir/lite/quantization/ir:QuantOps", + "//tensorflow/compiler/mlir/lite/schema:debug_metadata_fbs_with_mutable", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs_with_mutable", + "//tensorflow/compiler/mlir/lite/schema:schema_utils", + "//tensorflow/compiler/mlir/lite/stablehlo:legalize_stablehlo_composite_to_tfl_custom", + "//tensorflow/compiler/mlir/lite/stablehlo:legalize_stablehlo_to_vhlo_pass", + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/compiler/mlir/tensorflow:convert_tensor", + "//tensorflow/compiler/mlir/tensorflow:dynamic_shape_utils", + "//tensorflow/compiler/mlir/tensorflow:mangling_util", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_attributes", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_types", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core/platform:errors", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/log", + "@com_google_absl//absl/log:check", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@llvm-project//llvm:Analysis", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:FuncExtensions", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:ReconcileUnrealizedCasts", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TranslateLib", + "@stablehlo//:stablehlo_ops", + "@stablehlo//:vhlo_ops", + "@xla//xla/tsl/platform:errors", + "@xla//xla/tsl/platform:statusor", + ], +) + +cc_library( + name = "convert_type", + srcs = [ + "utils/convert_type.cc", + ], + hdrs = [ + "utils/convert_type.h", + ], + deps = [ + ":tensorflow_lite_ops", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_types", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core/platform:errors", + "@com_google_absl//absl/status:statusor", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + ], +) + +cc_library( + name = "region_isolation", + srcs = [ + "utils/region_isolation.cc", + ], + hdrs = [ + "utils/region_isolation.h", + ], + deps = [ + "@com_google_absl//absl/strings:str_format", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + ], +) + +tf_cc_test( + name = "region_isolation_test", + srcs = [ + "utils/region_isolation_test.cc", + ], + deps = [ + ":region_isolation", + "@com_google_googletest//:gtest_main", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:IR", + "@stablehlo//:stablehlo_ops", + ], +) + +cc_library( + name = "optimize_pass_options", + hdrs = ["transforms/optimize_pass_options.h"], + deps = [ + "@llvm-project//llvm:Support", + "@llvm-project//mlir:Pass", + ], +) + +cc_library( + name = "optimize_broadcast_like_pass_options", + hdrs = ["transforms/optimize_broadcast_like_pass_options.h"], + deps = [ + "@llvm-project//llvm:Support", + "@llvm-project//mlir:Pass", + ], +) + +cc_library( + name = "flatbuffer_translate_lib", + hdrs = [ + "flatbuffer_export.h", + "flatbuffer_export_flags.h", + "flatbuffer_import.h", + "utils/convert_type.h", + ], + deps = [ + ":converter_flags_proto_cc", + ":flatbuffer_export", + ":flatbuffer_import", + "//tensorflow/compiler/mlir:op_or_arg_name_mapper", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs", + "//tensorflow/core:protos_all_cc", + "@com_google_absl//absl/functional:any_invocable", + "@com_google_absl//absl/functional:function_ref", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + ], +) + +cc_library( + name = "flatbuffer_translate_registration", + srcs = [ + "flatbuffer_translate.cc", + ], + deps = [ + ":flatbuffer_translate_lib", + ":tensorflow_lite", + "//tensorflow/compiler/mlir/lite/quantization/ir:QuantOps", + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/compiler/mlir/tensorflow:mlir_roundtrip_flags", + "//tensorflow/compiler/mlir/tensorflow/translate/tools:parsers", + "@com_google_absl//absl/strings:string_view", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:MlirTranslateMain", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TranslateLib", + "@stablehlo//:stablehlo_ops", + "@stablehlo//:vhlo_ops", + ], + alwayslink = 1, +) + +tf_cc_binary( + name = "flatbuffer_translate", + deps = [ + ":flatbuffer_translate_registration", + ], +) + +cc_library( + name = "tf_tfl_translate_cl_options", + srcs = [ + "tf_tfl_translate_cl.cc", + ], + hdrs = [ + "tf_tfl_translate_cl.h", + ], + deps = [ + "@llvm-project//llvm:Support", + ], + alwayslink = 1, +) + +cc_library( + name = "common", + hdrs = [ + "common/tfl_pass_config.h", + ], + deps = [ + "//tensorflow/compiler/mlir/lite:converter_flags_proto_cc", + "//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib:quantization_config", + "@com_google_absl//absl/strings", + "@llvm-project//llvm:Support", + ], +) + +filegroup( + name = "tf_tfl_translate_main", + srcs = [ + "tf_tfl_translate.cc", + ], +) + +tf_cc_binary( + name = "tf_tfl_translate", + srcs = [ + ":tf_tfl_translate_main", + ], + # visibility = ["//visibility:public"], # copybara:uncomment + deps = [ + ":common", + ":converter_flags_proto_cc", + ":flatbuffer_translate_lib", + ":flatbuffer_translate_registration", + ":tensorflow_lite", + ":tf_tfl_translate_cl_options", + ":tf_to_tfl_flatbuffer", + "//tensorflow/compiler/mlir:init_mlir", + "//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib:quantization_config", + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/compiler/mlir/tensorflow:mlir_roundtrip_flags", + "//tensorflow/compiler/tf2xla/kernels:xla_ops", + "//tensorflow/core:core_cpu_base", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core/platform:errors", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/types:span", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:FuncExtensions", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Parser", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + "@stablehlo//:chlo_ops", + "@stablehlo//:stablehlo_ops", + "@xla//xla/hlo/translate/hlo_to_mhlo:translate", + "@xla//xla/mlir_hlo", + ], +) + +cc_library( + name = "tf_tfl_passes", + srcs = ["tf_tfl_passes.cc"], + hdrs = [ + "tf_tfl_passes.h", + ], + deps = [ + ":common", + ":converter_flags_proto_cc", + ":converter_pass_options_setter", + ":downcast_x64", + ":fake_quant_utils", + ":optimize_broadcast_like_pass", + ":pass", + ":pass_registry_utils", + ":tensorflow_lite_d2s", # buildcleaner: keep + ":tensorflow_lite_legalize_tf", # buildcleaner: keep + ":tensorflow_lite_optimize", # buildcleaner: keep + ":tensorflow_lite_optimize_batch_matmul", # buildcleaner: keep + ":tensorflow_lite_push_transpose_through_ewise_pass", # buildcleaner: keep + ":tensorflow_lite_quantize", # buildcleaner: keep + ":variable_freezing_pipeline", + "//tensorflow/compiler/mlir/lite/core:macros", + "//tensorflow/compiler/mlir/lite/quantization:quantization_passes", + "//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib:quantization_config", + "//tensorflow/compiler/mlir/lite/quantization/tensorflow:tf_quantization_passes", + "//tensorflow/compiler/mlir/lite/stablehlo:build_stablehlo_composite", + "//tensorflow/compiler/mlir/lite/stablehlo:compose_uniform_quantized_type_pass", + "//tensorflow/compiler/mlir/lite/stablehlo:composite_lowering", + "//tensorflow/compiler/mlir/lite/stablehlo:legalize_qdq_custom_call", + "//tensorflow/compiler/mlir/lite/stablehlo:legalize_stablehlo_composite_to_tfl_custom", + "//tensorflow/compiler/mlir/lite/stablehlo:legalize_tf_xla_call_module_to_stablehlo_pass", + "//tensorflow/compiler/mlir/lite/stablehlo:lift_callsite_loc_caller", + "//tensorflow/compiler/mlir/lite/stablehlo:prepare_hlo", # buildcleaner: keep + "//tensorflow/compiler/mlir/lite/stablehlo:tf_legalize_hlo", # buildcleaner: keep + "//tensorflow/compiler/mlir/lite/stablehlo:tfl_legalize_chlo", # buildcleaner: keep + "//tensorflow/compiler/mlir/lite/stablehlo:tfl_legalize_hlo", # buildcleaner: keep + "//tensorflow/compiler/mlir/lite/stablehlo:transforms", + "//tensorflow/compiler/mlir/lite/stablehlo:uniform_quantized_stablehlo_to_tfl_pass", + "//tensorflow/compiler/mlir/stablehlo:rename_entrypoint_to_main", + "//tensorflow/compiler/mlir/tensorflow/transforms:tensorflow_passes", + "//tensorflow/compiler/mlir/tensorflow/transforms:tf_saved_model_passes", + "//tensorflow/core:core_cpu_base", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:Transforms", + "@stablehlo//:stablehlo_passes", + "@xla//xla/mlir_hlo:mhlo_passes", + "@xla//xla/mlir_hlo:stablehlo_extension_passes", + ], +) + +cc_library( + name = "tf_to_tfl_flatbuffer", + srcs = ["tf_to_tfl_flatbuffer.cc"], + hdrs = [ + "tf_to_tfl_flatbuffer.h", + ], + deps = [ + ":common", + ":converter_flags_proto_cc", + ":flatbuffer_translate_lib", + ":mlir_module_utils", + ":tensorflow_lite", + ":tf_tfl_passes", + "//tensorflow/cc/saved_model:loader", + "//tensorflow/compiler/mlir:op_or_arg_name_mapper", + "//tensorflow/compiler/mlir/lite/debug", + "//tensorflow/compiler/mlir/lite/experimental/remat:metadata_util", + "//tensorflow/compiler/mlir/lite/metrics:converter_error_data_proto_cc", + "//tensorflow/compiler/mlir/lite/metrics:error_collector_inst", + "//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib:quantization_config", + "//tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy:quantize_weights", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs", + "//tensorflow/compiler/mlir/lite/stablehlo:legalize_stablehlo_composite_to_tfl_custom", + "//tensorflow/compiler/mlir/lite/stablehlo:legalize_stablehlo_to_vhlo_pass", + "//tensorflow/compiler/mlir/lite/stablehlo:op_stat_pass", + "//tensorflow/compiler/mlir/lite/stablehlo:stablehlo_util", + "//tensorflow/compiler/mlir/lite/stablehlo:transforms", + "//tensorflow/compiler/mlir/lite/tools/optimize:reduced_precision_metadata", + "//tensorflow/compiler/mlir/quantization/stablehlo:quantization_config_proto_cc", + "//tensorflow/compiler/mlir/quantization/stablehlo:quantize_passes", + "//tensorflow/compiler/mlir/quantization/tensorflow:quantization_options_proto_cc", + "//tensorflow/compiler/mlir/quantization/tensorflow:quantize_passes", + "//tensorflow/compiler/mlir/quantization/tensorflow:quantize_preprocess", + "//tensorflow/compiler/mlir/quantization/tensorflow/python:py_function_lib", + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/compiler/mlir/tensorflow:error_util", + "//tensorflow/compiler/mlir/tensorflow:file_tf_mlir_translate", + "//tensorflow/compiler/mlir/tensorflow:mlir_import_options", + "//tensorflow/compiler/mlir/tensorflow:mlir_roundtrip_flags", + "//tensorflow/compiler/mlir/tensorflow:tf_dialect_lib", + "//tensorflow/compiler/mlir/tensorflow:translate_lib", + "//tensorflow/compiler/mlir/tensorflow/transforms:tf_dialect_passes", + "//tensorflow/core:core_cpu_base", + "//tensorflow/core:framework", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core/ir/types:Dialect", + "//tensorflow/core/platform:status", + "@com_google_absl//absl/log", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/types:span", + "@flatbuffers//:runtime_cc", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:BytecodeWriter", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:FuncExtensions", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Parser", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:ReconcileUnrealizedCasts", + "@llvm-project//mlir:Support", + "@stablehlo//:stablehlo_ops", + "@stablehlo//:vhlo_ops", + "@tsl//tsl/platform:protobuf", + ], +) + +cc_library( + name = "offset_buffer", + hdrs = ["offset_buffer.h"], +) + +cc_library( + name = "const_tensor_utils", + srcs = ["utils/const_tensor_utils.cc"], + hdrs = ["utils/const_tensor_utils.h"], + deps = [ + ":convert_type", + ":low_bit_utils", + ":string_utils", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs", + "//tensorflow/compiler/mlir/tensorflow:dynamic_shape_utils", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_types", + "//tensorflow/core:protos_all_cc", + "@com_google_absl//absl/base", + "@com_google_absl//absl/base:core_headers", + "@com_google_absl//absl/meta:type_traits", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@eigen_archive//:eigen3", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:Support", + "@tsl//tsl/platform:statusor", + ], +) + +cc_library( + name = "string_utils", + srcs = ["utils/string_utils.cc"], + hdrs = ["utils/string_utils.h"], + visibility = ["//visibility:public"], +) + +exports_files(srcs = ["allocation.h"]) + +cc_library( + name = "allocation", + srcs = [ + "allocation.cc", + ] + select({ + ":tflite_mmap_disabled": [ + "mmap_allocation_disabled.cc", + ], + "//conditions:default": [ + "mmap_allocation.cc", + ], + }), + hdrs = [ + "allocation.h", + ], + compatible_with = get_compatible_with_portable(), + copts = tflite_copts_warnings(), + visibility = ["//visibility:public"], + deps = [ + "//tensorflow/compiler/mlir/lite/core/api:error_reporter", + "//tensorflow/lite:util", + ], +) + +exports_files(srcs = ["utils/control_edges.h"]) + +cc_library( + name = "control_edges", + hdrs = ["utils/control_edges.h"], + visibility = ["//tensorflow/compiler/mlir/lite/experimental/remat:__pkg__"], +) + +tf_cc_test( + name = "offset_buffer_test", + srcs = ["offset_buffer_test.cc"], + deps = [ + ":offset_buffer", + "//tensorflow/core:test", + "//tensorflow/core:test_main", + ], +) + +build_test( + name = "tensorflow_lite_build_test", + targets = [ + ":tensorflow_lite", + ], +) + +# LINT.IfChange + +bool_flag( + name = "enable_fuchsia_mmap", + build_setting_default = True, +) + +config_setting( + name = "tflite_with_xnnpack_explicit_false", + define_values = {"tflite_with_xnnpack": "false"}, +) + +# A config for enabling tensorflow profiler in TFLite. Currently, it only supports dynamic +# allocation. Add '--define=tflite_tensorflow_profiler=true' in your build command line to use it. +config_setting( + name = "tensorflow_profiler_config", + define_values = {"tflite_tensorflow_profiler": "true"}, +) + +config_setting( + name = "fuchsia_mmap_disabled", + constraint_values = if_google( + ["//third_party/bazel_platforms/os:fuchsia"], + [], + ), + flag_values = { + ":enable_fuchsia_mmap": "False", + }, + values = if_oss( + # TODO(b/149248802) When we have a Fuchsia Bazel SDK update to use the values it sets. + {"cpu": "fuchsia"}, + {}, + ), +) + +config_setting( + name = "tflite_mmap_disabled_true", + values = { + "copt": "-DTFLITE_MMAP_DISABLED", + }, +) + +selects.config_setting_group( + name = "tflite_mmap_disabled", + match_any = [ + ":fuchsia_mmap_disabled", + ":tflite_mmap_disabled_true", + "//tensorflow:windows", + ], +) +# LINT.ThenChange(//tensorflow/lite/BUILD) + +# LINT.IfChange(version) +cc_library( + name = "lite_version", + hdrs = ["version.h"], + compatible_with = get_compatible_with_portable(), + copts = tflite_copts_warnings(), +) +# LINT.ThenChange(//tensorflow/lite:version) + +# OSS only: This target is header-only. Link `types_proto_cc_impl` only to +# `libtensorflow_framework.so` via `lib_internal_impl`. Do NOT link `types_flags_proto_cc_impl` +# directly unless the target does not link `libtensorflow_framework.so`. +tf_proto_library( + name = "types_proto", + srcs = ["types.proto"], + make_default_target_header_only = True, + visibility = ["//visibility:public"], +) + +# OSS only: This target is header-only. Link `converter_flags_proto_cc_impl` only to +# `libtensorflow_framework.so` via `lib_internal_impl`. Do NOT link `converter_flags_proto_cc_impl` +# directly unless the target does not link `libtensorflow_framework.so`. +tf_proto_library( + name = "converter_flags_proto", + srcs = ["converter_flags.proto"], + make_default_target_header_only = True, + protodeps = [ + ":types_proto", + "//tensorflow/compiler/mlir/lite/debug:debug_options_proto", + ], + visibility = ["//visibility:public"], +) + +tf_proto_library( + name = "model_flags_proto", + srcs = ["model_flags.proto"], + make_default_target_header_only = True, + protodeps = [":types_proto"], + visibility = ["//visibility:public"], +) + +# copybara:uncomment_begin(google-only) +# py_proto_library( +# name = "types_py_proto", +# visibility = ["//visibility:public"], +# deps = [":types_proto"], +# ) +# +# py_proto_library( +# name = "model_flags_py_proto", +# visibility = ["//visibility:public"], +# deps = [":model_flags_proto"], +# ) +# +# py_proto_library( +# name = "converter_flags_py_proto", +# visibility = ["//visibility:public"], +# deps = [":converter_flags_proto"], +# ) +# copybara:uncomment_end + +filegroup( + name = "litfiles", + srcs = glob(["runlit*py"]), + visibility = ["//tensorflow/compiler/mlir/lite:__subpackages__"], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/README.md b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/README.md new file mode 100644 index 0000000000000000000000000000000000000000..05beb7362c6a406d007e696708da2e1d9583c036 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/README.md @@ -0,0 +1,35 @@ +# The new MLIR based TensorFlow to TensorFlow Lite converter + +This directory contains: + +1. [MLIR](https://github.com/llvm/llvm-project/tree/main/mlir) dialects, + transformation passes and utilities for TensorFlow Lite. + +## API: + +The API for converting TensorFlow models to TensorFlow Lite will be through +`tf.lite.TFLiteConverter`. All the conversion code is open sourced, and +the API will be integrated soon. + +### The conversion process from TensorFlow to TensorFlow Lite includes the +following major passes: + +- Import from GraphDef, in .pb or .pbtxt format, into MLIR. +- Raise to Control-flow-graph. Converts TF Control Flow dialect to TF dialect. +- The Canonicalization pass iteratively applies canonicalization +transformations in a greedy way until no further changes occur. +Canonicalization includes constant folding. +- The Legalize pass converts TensorFlow operations to TensorFlow Lite +ones. The operations that cannot be mapped to TensorFlow Lite dialect +are left as TensorFlow operations. Unsupported op handling follows the +proposed TFLite mechanism. +- Optimizations are performed in both the TF & TFLite dialect; aiming for small +size and high performance (among the core value proposition of +TensorFlow Lite models). +- The Export pass writes out TensorFlow Lite FlatBuffer format. This pass +operates on MLIR TensorFlow Lite dialect and is simple/direct translation. + +See +https://github.com/tensorflow/tensorflow/blob/master/tensorflow/compiler/mlir/lite/tf_tfl_passes.cc +for the full list of MLIR passes for conversion from TensorFlow to TensorFlow +Lite. diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/allocation.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/allocation.cc new file mode 100644 index 0000000000000000000000000000000000000000..645f6155926c0ae8d0b45e47d04b01904374bc9c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/allocation.cc @@ -0,0 +1,163 @@ +/* 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/compiler/mlir/lite/allocation.h" + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "tensorflow/compiler/mlir/lite/core/api/error_reporter.h" + +namespace tflite { + +#ifndef TFLITE_MCU +FileCopyAllocation::FileCopyAllocation(const char* filename, + ErrorReporter* error_reporter) + : Allocation(error_reporter, Allocation::Type::kFileCopy) { + // Obtain the file size using fstat, or report an error if that fails. + std::unique_ptr file(fopen(filename, "rb"), fclose); + if (!file) { + error_reporter_->Report("Could not open '%s'.", filename); + return; + } +// Use 64-bit stat on Windows so that files larger than 2GB report a correct +// size. MSVC's `struct stat`/`fstat` use a 32-bit `st_size` and fail for such +// files; `_stat64`/`_fstat64` provide a 64-bit `st_size`. +#ifdef _WIN32 + struct _stat64 sb; + if (_fstat64(_fileno(file.get()), &sb) != 0) { +#else + struct stat sb; + if (fstat(fileno(file.get()), &sb) != 0) { +#endif + error_reporter_->Report("Failed to get file size of '%s'.", filename); + return; + } + buffer_size_bytes_ = sb.st_size; + std::unique_ptr buffer(new char[buffer_size_bytes_]); + if (!buffer) { + error_reporter_->Report("Malloc of buffer to hold copy of '%s' failed.", + filename); + return; + } + size_t bytes_read = + fread(buffer.get(), sizeof(char), buffer_size_bytes_, file.get()); + if (bytes_read != buffer_size_bytes_) { + error_reporter_->Report("Read of '%s' failed (too few bytes read).", + filename); + return; + } + // Versions of GCC before 6.2.0 don't support std::move from non-const + // char[] to const char[] unique_ptrs. + copied_buffer_.reset(const_cast(buffer.release())); +} + +FileCopyAllocation::~FileCopyAllocation() = default; + +const void* FileCopyAllocation::base() const { return copied_buffer_.get(); } + +size_t FileCopyAllocation::bytes() const { return buffer_size_bytes_; } + +bool FileCopyAllocation::valid() const { return copied_buffer_ != nullptr; } +#endif + +MemoryAllocation::MemoryAllocation(const void* ptr, size_t num_bytes, + ErrorReporter* error_reporter) + : Allocation(error_reporter, Allocation::Type::kMemory) { +#ifdef __arm__ + if ((reinterpret_cast(ptr) & 0x3) != 0) { + // The flatbuffer schema has alignment requirements of up to 16 bytes to + // guarantee that data can be correctly accesses by various backends. + // Therefore, model pointer should also be 16-bytes aligned to preserve this + // requirement. But this condition only checks 4-bytes alignment which is + // the mininum requirement to prevent SIGBUS fault on 32bit ARM. Some models + // could require 8 or 16 bytes alignment which is not checked yet. + // + // Note that 64-bit ARM may also suffer a performance impact, but no crash - + // that case is not checked. + TF_LITE_REPORT_ERROR(error_reporter, + "The supplied buffer is not 4-bytes aligned"); + buffer_ = nullptr; + buffer_size_bytes_ = 0; + return; + } +#endif // __arm__ + +// `android_local_test` doesn't support zipalign b/356640509 so we need this +// workaround to keep our clients working. +// TODO: b/356413060 - Remove the workaround once b/356640509 is fixed. +#if defined(__x86_64__) && defined(UNDEFINED_BEHAVIOR_SANITIZER) + if ((reinterpret_cast(ptr) & 0x3) != 0) { +#if defined(_WIN32) + // Windows / MSVC + aligned_ptr_ = _aligned_malloc(num_bytes, 4); +#elif defined(__ANDROID__) && __ANDROID_API__ < 28 + // Older Android (API < 28) + if (posix_memalign(&aligned_ptr_, 4, num_bytes) != 0) { + aligned_ptr_ = nullptr; + } + +#elif defined(__APPLE__) + // macOS/iOS: aligned_alloc is technically 10.15+, + // posix_memalign is safer for backwards compatibility. + if (posix_memalign(&aligned_ptr_, 4, num_bytes) != 0) { + aligned_ptr_ = nullptr; + } + +#else + // Standard C11 (Modern Linux, Android API 28+) + aligned_ptr_ = ::aligned_alloc(4, num_bytes); +#endif + + if (aligned_ptr_ == nullptr) { + TF_LITE_REPORT_ERROR(error_reporter, "Failed to allocate aligned buffer"); + buffer_ = nullptr; + buffer_size_bytes_ = 0; + return; + } + memcpy(aligned_ptr_, ptr, num_bytes); + buffer_ = aligned_ptr_; + } else { + buffer_ = ptr; + } +#else // defined(__x86_64__) && defined(UNDEFINED_BEHAVIOR_SANITIZER) + buffer_ = ptr; +#endif // defined(__x86_64__) && defined(UNDEFINED_BEHAVIOR_SANITIZER) + + buffer_size_bytes_ = num_bytes; +} + +MemoryAllocation::~MemoryAllocation() { +#if defined(__x86_64__) && defined(UNDEFINED_BEHAVIOR_SANITIZER) + if (aligned_ptr_) { + free(aligned_ptr_); + } +#endif +} + +const void* MemoryAllocation::base() const { return buffer_; } + +size_t MemoryAllocation::bytes() const { return buffer_size_bytes_; } + +bool MemoryAllocation::valid() const { return buffer_ != nullptr; } + +} // namespace tflite diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/allocation.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/allocation.h new file mode 100644 index 0000000000000000000000000000000000000000..dee556df397b611192ec682bd05d4d755af74945 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/allocation.h @@ -0,0 +1,173 @@ +/* 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. +==============================================================================*/ +/// \file +/// +/// Memory management for TF Lite. +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_ALLOCATION_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_ALLOCATION_H_ + +#include + +#include +#include +#include + +#include "tensorflow/compiler/mlir/lite/core/api/error_reporter.h" + +namespace tflite { + +/// A memory allocation handle. This could be a mmap or shared memory. +class Allocation { + public: + using Ptr = std::unique_ptr; + + virtual ~Allocation() = default; + + enum class Type { + kMMap, + kFileCopy, + kMemory, + }; + + /// Base pointer of this allocation + virtual const void* base() const = 0; + /// Size in bytes of the allocation + virtual size_t bytes() const = 0; + /// Whether the allocation is valid + virtual bool valid() const = 0; + /// Return the type of the Allocation. + Type type() const { return type_; } + + protected: + Allocation(ErrorReporter* error_reporter, Type type) + : error_reporter_(error_reporter), type_(type) {} + ErrorReporter* error_reporter_; + + private: + const Type type_; +}; + +/// Note that not all platforms support MMAP-based allocation. +/// Use `IsSupported()` to check. +class MMAPAllocation : public Allocation { + public: + /// Loads and maps the provided file to a memory region. + /// If map_private is true, the mapping is private and writeable. Otherwise, + /// the mapping is shared and read-only. + MMAPAllocation(const char* filename, ErrorReporter* error_reporter, + bool map_private = false); + + /// Loads and maps the provided file to a memory region at the given + /// offset and length (both in bytes). + /// If map_private is true, the mapping is private and writeable. Otherwise, + /// the mapping is shared and read-only. + MMAPAllocation(const char* filename, size_t offset, size_t length, + ErrorReporter* error_reporter, bool map_private = false); + + /// Maps the provided file descriptor to a memory region. + /// If map_private is true, the mapping is private and writeable. Otherwise, + /// the mapping is shared and read-only. + /// Note: The provided file descriptor will be dup'ed for usage; the caller + /// retains ownership of the provided descriptor and should close accordingly. + MMAPAllocation(int fd, ErrorReporter* error_reporter, + bool map_private = false); + + /// Maps the provided file descriptor, with the given offset and length (both + /// in bytes), to a memory region. + /// If map_private is true, the mapping is private and writeable. Otherwise, + /// the mapping is shared and read-only. + /// Note: The provided file descriptor will be dup'ed for usage; the caller + /// retains ownership of the provided descriptor and should close accordingly. + MMAPAllocation(int fd, size_t offset, size_t length, + ErrorReporter* error_reporter, bool map_private = false); + + ~MMAPAllocation() override; + const void* base() const override; + size_t bytes() const override; + bool valid() const override; + + int fd() const { return mmap_fd_; } + + // The start address of the mmapped buffer. + // This will be base() rounded down to the nearest page boundary. + const void* mmapped_buffer() const { return mmapped_buffer_; } + + // The size of the mmapped buffer. + size_t mmapped_buffer_size() const { return bytes() + offset_in_buffer_; } + + // Offset of mmapped_buffer() in the file referenced by the file descriptor. + size_t mmapped_buffer_offset_in_file() const { + return offset_of_buffer_in_file_; + } + + static bool IsSupported(); + + protected: + // Data required for mmap. + int mmap_fd_ = -1; // mmap file descriptor + const void* mmapped_buffer_; + size_t buffer_size_bytes_ = 0; + // Used when the address to mmap is not page-aligned. + size_t offset_in_buffer_ = 0; + size_t offset_of_buffer_in_file_ = 0; + + private: + // Assumes ownership of the provided `owned_fd` instance. + MMAPAllocation(ErrorReporter* error_reporter, int owned_fd, bool map_private); + + // Assumes ownership of the provided `owned_fd` instance, and uses the given + // offset and length (both in bytes) for memory mapping. + MMAPAllocation(ErrorReporter* error_reporter, int owned_fd, size_t offset, + size_t length, bool map_private); +}; + +class FileCopyAllocation : public Allocation { + public: + /// Loads the provided file into a heap memory region. + FileCopyAllocation(const char* filename, ErrorReporter* error_reporter); + ~FileCopyAllocation() override; + const void* base() const override; + size_t bytes() const override; + bool valid() const override; + + private: + std::unique_ptr copied_buffer_; + size_t buffer_size_bytes_ = 0; +}; + +class MemoryAllocation : public Allocation { + public: + /// Provides a (read-only) view of the provided buffer region as an + /// allocation. + /// Note: The caller retains ownership of `ptr`, and must ensure it remains + /// valid for the lifetime of the class instance. + MemoryAllocation(const void* ptr, size_t num_bytes, + ErrorReporter* error_reporter); + ~MemoryAllocation() override; + const void* base() const override; + size_t bytes() const override; + bool valid() const override; + + private: + const void* buffer_; +#if defined(__x86_64__) && defined(UNDEFINED_BEHAVIOR_SANITIZER) + void* aligned_ptr_ = nullptr; +#endif + size_t buffer_size_bytes_ = 0; +}; + +} // namespace tflite + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_ALLOCATION_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/build_def.bzl b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/build_def.bzl new file mode 100644 index 0000000000000000000000000000000000000000..c98b61a95849795bad02404c16a4cbf4796bdaf8 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/build_def.bzl @@ -0,0 +1,89 @@ +"""Build macros for TF Lite.""" + +load("//tensorflow/compiler/mlir/lite:special_rules.bzl", "tflite_copts_extra") +load("//tensorflow/lite:build_def.bzl", "clean_dep") + +# LINT.IfChange(tflite_copts) +def tflite_copts(): + """Defines common compile time flags for TFLite libraries.""" + copts = [ + "-DFARMHASH_NO_CXX_STRING", + "-DEIGEN_ALLOW_UNALIGNED_SCALARS", # TODO(b/296071640): Remove when underlying bugs are fixed. + ] + select({ + clean_dep("//tensorflow:android_arm"): [ + "-mfpu=neon", + ], + # copybara:uncomment_begin(google-only) + # clean_dep("//tensorflow:chromiumos_x86_64"): [], + # copybara:uncomment_end + clean_dep("//tensorflow:ios_x86_64"): [ + "-msse4.1", + ], + clean_dep("//tensorflow:linux_x86_64"): [ + "-msse4.2", + ], + clean_dep("//tensorflow:linux_x86_64_no_sse"): [], + clean_dep("//tensorflow:windows"): [ + # copybara:uncomment_begin(no MSVC flags in google) + # "-DTFL_COMPILE_LIBRARY", + # "-Wno-sign-compare", + # copybara:uncomment_end_and_comment_begin + "/DTFL_COMPILE_LIBRARY", + "/wd4018", # -Wno-sign-compare + # copybara:comment_end + ], + "//conditions:default": [ + "-Wno-sign-compare", + ], + }) + select({ + clean_dep("//tensorflow:optimized"): ["-O3"], + "//conditions:default": [], + }) + select({ + clean_dep("//tensorflow:android"): [ + "-ffunction-sections", # Helps trim binary size. + "-fdata-sections", # Helps trim binary size. + ], + "//conditions:default": [], + }) + select({ + clean_dep("//tensorflow:windows"): [], + "//conditions:default": [ + "-fno-exceptions", # Exceptions are unused in TFLite. + ], + }) + select({ + clean_dep("//tensorflow/compiler/mlir/lite:tflite_with_xnnpack_explicit_false"): ["-DTFLITE_WITHOUT_XNNPACK"], + "//conditions:default": [], + }) + select({ + clean_dep("//tensorflow/compiler/mlir/lite:tensorflow_profiler_config"): ["-DTF_LITE_TENSORFLOW_PROFILER"], + "//conditions:default": [], + }) + select({ + clean_dep("//tensorflow/compiler/mlir/lite/delegates:tflite_debug_delegate"): ["-DTFLITE_DEBUG_DELEGATE"], + "//conditions:default": [], + }) + select({ + clean_dep("//tensorflow/compiler/mlir/lite:tflite_mmap_disabled"): ["-DTFLITE_MMAP_DISABLED"], + "//conditions:default": [], + }) + + return copts + tflite_copts_extra() + +# LINT.ThenChange(//tensorflow/lite/build_def.bzl:tflite_copts) + +# LINT.IfChange(tflite_copts_warnings) +def tflite_copts_warnings(): + """Defines common warning flags used primarily by internal TFLite libraries.""" + + # TODO(b/155906820): Include with `tflite_copts()` after validating clients. + + return select({ + clean_dep("//tensorflow:windows"): [ + # We run into trouble on Windows toolchains with warning flags, + # as mentioned in the comments below on each flag. + # We could be more aggressive in enabling supported warnings on each + # Windows toolchain, but we compromise with keeping BUILD files simple + # by limiting the number of config_setting's. + ], + "//conditions:default": [ + "-Wall", + ], + }) + +# LINT.ThenChange(//tensorflow/lite/build_def.bzl:tflite_copts_warnings) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/common/tfl_pass_config.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/common/tfl_pass_config.h new file mode 100644 index 0000000000000000000000000000000000000000..107cd64ad665d9dd9e36ce7599798897310b39af --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/common/tfl_pass_config.h @@ -0,0 +1,159 @@ +/* Copyright 2019 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_COMMON_TFL_PASS_CONFIG_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_COMMON_TFL_PASS_CONFIG_H_ + +#include +#include + +#include "absl/strings/str_join.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/Support/raw_ostream.h" +#include "tensorflow/compiler/mlir/lite/converter_flags.pb.h" +#include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_config.h" + +namespace mlir { +namespace TFL { + +// A config that controls which passes get run as part TFLite converter. +struct PassConfig { + explicit PassConfig(QuantizationSpecs specs) + : quant_specs(std::move(specs)) {} + + // If `emit_builtin_tflite_ops` is true, TF Lite legalization passes will be + // added, which produces TF Lite ops. + bool emit_builtin_tflite_ops = true; + // If `lower_tensor_list_ops` is true, tensorlist ops will be lowered to basic + // TF ops before legalization to TF Lite dialect. + bool lower_tensor_list_ops = false; + // The allowlist of functions that would be preserved after trimming. + llvm::ArrayRef trim_functions_allowlist; + // All information about quantization. + QuantizationSpecs quant_specs; + // If `form_clusters` is true , clusters are formed by grouping consecutive + // ops of the same device, under a `tf_device.launch` op. + bool form_clusters = false; + // If `unfold_batch_matmul` is true, the tf.BatchMatMul is unfolded to a set + // of tfl.fully_connected ops. + bool unfold_batch_matmul = true; + // Whether to outline WhileOp at the end of the pipeline. + bool outline_tf_while = false; + // Whether to do shape inference. + bool shape_inference = true; + // Whether to do TFLite runtime verification. + bool runtime_verification = true; + // Whether to enable TFLite variables or not, this will allow + // mutable variables and produce ReadVariable/AssignVariable ops in TFLite. + bool enable_tflite_variables = false; + // Whether to unfold large splat constant tensors and replace them with + // fill operation. + bool unfold_large_splat_constant = false; + // Whether to run the `GuaranteeAllFuncsOneUsePass` to ensure each function + // has a single use. + bool guarantee_all_funcs_one_use = false; + // Whether to enable the hlo/stablehlo to tf conversion. This also supports + // the case where a saved model contains both TF module and serialized + // StableHLO module. + bool enable_hlo_to_tf_conversion = false; + // Whether to disable the direct hlo/stablehlo to Tensorflow Lite conversion. + // + // This prevents from directly converting from HLO to TFLite without going + // through TF for some of the ops. Some conversions are only supported through + // this path. + bool disable_hlo_to_tfl_conversion = false; + // Whether to enable to use DynamicUpdateSlice op. + bool enable_dynamic_update_slice = false; + // Whether to preserve AssertOp during legalization. + bool preserve_assert_op = false; + // Whether to enable TF->stablehlo passes. + bool enable_stablehlo_conversion = false; + // Whether to convert `tf.TensorList*` to `tfl.custom_op` if they can all + // be supported. + bool legalize_custom_tensor_list_ops = false; + // Whether to convert some tensor types to a lower precision if all values + // within that tensor are within the range of the lower precision. This could + // have side effects e.g. reduced flatbuffer size. Only certain type + // conversions are supported. + bool reduce_type_precision = false; + // Whether to consider this model a quantized model with quantize/dequantize + // ops and to convert kernels to quantized kernels wherever appropriate. + QDQConversionMode qdq_conversion_mode = QDQConversionMode::kQDQNone; + + // When set to true, StableHLO Quantizer is run. The full configuration for + // the quantizer is at `ConverterFlags::quantization_config`. + bool enable_stablehlo_quantizer = false; + + // Enables the attempt to directly lower composites into tflite ops. + bool enable_composite_direct_lowering = true; + + // Specifies the framework of the original model. + tflite::ConverterFlags::ModelOriginFramework model_origin_framework = + tflite::ConverterFlags::UNSET; + + // When set to true, convert +Inf/-Inf to MIN/MAX float value and output of + // convert only contains finite values. + bool canonicalizing_inf_as_min_max_float = true; + + // When set to true, allows fusion of dynamic shaped broadcast ops. It helps + // fusing implicit broadcasting ops when output shape has dynamic dimensions, + // but it may cause incorrect results when broadcasting ops are introduced by + // explicit broadcasting in the source model. + bool unsafe_fuse_dynamic_shaped_broadcast = false; + + // When set to true, enable unsafe single batch rank reduction. + bool unsafe_single_batch_rank_reduction = false; +}; + +inline llvm::raw_ostream& operator<<(llvm::raw_ostream& os, + const PassConfig& pass_config) { + return os << "emit_builtin_tflite_ops: " + << pass_config.emit_builtin_tflite_ops + << "\nlower_tensor_list_ops: " << pass_config.lower_tensor_list_ops + << "\ntrim_functions_allowlist: " + << absl::StrJoin(pass_config.trim_functions_allowlist.vec(), ",") + << "\nform_clusters: " << pass_config.form_clusters + << "\nunfold_batch_matmul: " << pass_config.unfold_batch_matmul + << "\noutline_tf_while: " << pass_config.outline_tf_while + << "\nshape_inference: " << pass_config.shape_inference + << "\nruntime_verification: " << pass_config.runtime_verification + << "\nenable_tflite_variables: " + << pass_config.enable_tflite_variables + << "\nunfold_large_splat_constant: " + << pass_config.unfold_large_splat_constant + << "\nguarantee_all_funcs_one_use: " + << pass_config.guarantee_all_funcs_one_use + << "\nenable_hlo_to_tf_conversion: " + << pass_config.enable_hlo_to_tf_conversion + << "\nenable_stablehlo_conversion: " + << pass_config.enable_stablehlo_conversion + << "\nlegalize_custom_tensor_list_ops: " + << pass_config.legalize_custom_tensor_list_ops + << "\nunsafe_fuse_dynamic_shaped_broadcast: " + << pass_config.unsafe_fuse_dynamic_shaped_broadcast + << "\nreduce_type_precision: " << pass_config.reduce_type_precision + << "\nconvert_qdq_format: " + << GetQDQQuantModeString(pass_config.qdq_conversion_mode) + << "\nmodel_origin_framework: " + << tflite::ConverterFlags::ModelOriginFramework_Name( + pass_config.model_origin_framework) + << "\nunsafe_single_batch_rank_reduction: " + << pass_config.unsafe_single_batch_rank_reduction << "\n"; +} + +} // namespace TFL +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_COMMON_TFL_PASS_CONFIG_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/converter_flags.proto b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/converter_flags.proto new file mode 100644 index 0000000000000000000000000000000000000000..da8390fd5d5762acb6dfbdd942357b93f5144d07 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/converter_flags.proto @@ -0,0 +1,389 @@ +// Copyright 2023 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. +syntax = "proto2"; + +package tflite; + +import "tensorflow/compiler/mlir/lite/debug/debug_options.proto"; +import "tensorflow/compiler/mlir/lite/types.proto"; + +// Supported I/O file formats. Some formats may be input-only or output-only. +enum FileFormat { + FILE_FORMAT_UNKNOWN = 0; + + // GraphDef, tensorflow/core/framework/graph.proto + TENSORFLOW_GRAPHDEF = 1; + + // Tensorflow's mobile inference model. + // tensorflow/lite/schema/schema.fbs + TFLITE = 2; + + // GraphViz + // Export-only. + GRAPHVIZ_DOT = 3; +} + +// TocoFlags encodes extra parameters that drive tooling operations, that +// are not normally encoded in model files and in general may not be thought +// of as properties of models, instead describing how models are to be +// processed in the context of the present tooling job. +// +// Next ID to use: 71. +message ConverterFlags { + reserved 54, 61; + + // Input file format + optional FileFormat input_format = 1; + + // Output file format + optional FileFormat output_format = 2; + + // Similar to inference_type, but allows to control specifically the + // quantization of input arrays, separately from other arrays. + // + // If not set, then the value of inference_type is implicitly used, i.e. + // by default input arrays are quantized like other arrays. + // + // Like inference_type, this only affects real-number arrays. By "real-number" + // we mean float arrays, and quantized arrays. This excludes plain + // integer arrays, strings arrays, and every other data type. + // + // The typical use for this flag is for vision models taking a bitmap + // as input, typically with uint8 channels, yet still requiring floating-point + // inference. For such image models, the uint8 input is quantized, i.e. + // the uint8 values are interpreted as real numbers, and the quantization + // parameters used for such input arrays are their mean_value, std_value + // parameters. + optional IODataType inference_input_type = 11; + + // Sets the type of real-number arrays in the output file, that is, controls + // the representation (quantization) of real numbers in the output file, + // except for input arrays, which are controlled by inference_input_type. + // + // NOTE: this flag only impacts real-number arrays. By "real-number" + // we mean float arrays, and quantized arrays. This excludes plain + // integer arrays, strings arrays, and every other data type. + // + // For real-number arrays, the impact of this flag is to allow the output + // file to choose a different real-numbers representation (quantization) + // from what the input file used. For any other types of arrays, changing + // the data type would not make sense. + // + // Specifically: + // - If FLOAT, then real-numbers arrays will be of type float in + // the output file. If they were quantized in the input file, then + // they get dequantized. + // - If QUANTIZED_UINT8, then real-numbers arrays will be quantized + // as uint8 in the output file. If they were float in the input file, + // then they get quantized. + // - If not set, then all real-numbers arrays retain the same type in the + // output file as they have in the input file. + // + optional IODataType inference_type = 4; + + // default_ranges_min and default_ranges_max are helpers to experiment + // with quantization of models. Normally, quantization requires the input + // model to have (min, max) range information for every activations array. + // This is needed in order to know how to quantize arrays and still achieve + // satisfactory accuracy. However, in some circumstances one would just like + // to estimate the performance of quantized inference, without caring about + // accuracy. That is what default_ranges_min and default_ranges_max are for: + // when specified, they will be used as default (min, max) range boundaries + // for all activation arrays that lack (min, max) range information, thus + // allowing for quantization to proceed. + // + // It should be clear from the above explanation that these parameters are + // for experimentation purposes only and should not be used in production: + // they make it easy to quantize models, but the resulting quantized model + // will be inaccurate. + // + // These values only apply to arrays quantized with the kUint8 data type. + optional float default_ranges_min = 5; + optional float default_ranges_max = 6; + // Equivalent versions of default_ranges_min/_max for arrays quantized with + // the kInt16 data type. + optional float default_int16_ranges_min = 15; + optional float default_int16_ranges_max = 16; + + // Ignore and discard FakeQuant nodes. For instance, that can be used to + // generate plain float code without fake-quantization from a quantized + // graph. + optional bool drop_fake_quant = 7; + + // Normally, FakeQuant nodes must be strict boundaries for graph + // transformations, in order to ensure that quantized inference has the + // exact same arithmetic behavior as quantized training --- which is the + // whole point of quantized training and of FakeQuant nodes in the first + // place. However, that entails subtle requirements on where exactly + // FakeQuant nodes must be placed in the graph. Some quantized graphs + // have FakeQuant nodes at unexpected locations, that prevent graph + // transformations that are necessary in order to generate inference + // code for these graphs. Such graphs should be fixed, but as a + // temporary work-around, setting this reorder_across_fake_quant flag + // allows toco to perform necessary graph transformations on them, + // at the cost of no longer faithfully matching inference and training + // arithmetic. + optional bool reorder_across_fake_quant = 8; + + // If true, allow TOCO to create TF Lite Custom operators for all the + // unsupported Tensorflow ops. + optional bool allow_custom_ops = 10; + + // Applies only to the case when the input format is TENSORFLOW_GRAPHDEF. + // If true, then control dependencies will be immediately dropped during + // import. + // If not set, the default behavior is as follows: + // - Default to false if the output format is TENSORFLOW_GRAPHDEF. + // - Default to true in all other cases. + optional bool drop_control_dependency = 12; + + // Disables transformations that fuse subgraphs such as known LSTMs (not all + // LSTMs are identified). + optional bool debug_disable_recurrent_cell_fusion = 13; + + // Uses the FakeQuantWithMinMaxArgs.num_bits attribute to adjust quantized + // array data types throughout the graph. The graph must be properly annotated + // with FakeQuant* ops on at least the edges and may contain additional ops on + // the interior of the graph to widen/narrow as desired. + // + // Input and output array data types may change because of this propagation + // and users must be sure to query the final data_type values. + optional bool propagate_fake_quant_num_bits = 14; + + // Some fast uint8 GEMM kernels require uint8 weights to avoid the value 0. + // This flag allows nudging them to 1 to allow proceeding, with moderate + // inaccuracy. + optional bool allow_nudging_weights_to_use_fast_gemm_kernel = 17; + + // Minimum size of constant arrays to deduplicate; arrays smaller will not be + // deduplicated. + optional int64 dedupe_array_min_size_bytes = 18 [default = 64]; + + // Split the LSTM inputs from 5 tensors to 18 tensors for TFLite. + // Ignored if the output format is not TFLite. + optional bool split_tflite_lstm_inputs = 19 [default = true]; + + // Store weights as quantized weights followed by dequantize operations. + // Computation is still done in float, but reduces model size (at the cost of + // accuracy and latency). + // DEPRECATED: Please use post_training_quantize instead. + optional bool quantize_weights = 20 [default = false]; + + // Full filepath of folder to dump the graphs at various stages of processing + // GraphViz .dot files. Preferred over --output_format=GRAPHVIZ_DOT in order + // to keep the requirements of the output file. + optional string dump_graphviz_dir = 24; + + // Boolean indicating whether to dump the graph after every graph + // transformation. + optional bool dump_graphviz_include_video = 25; + + // Boolean indicating whether to quantize the weights of the converted float + // model. Model size will be reduced and there will be latency improvements + // (at the cost of accuracy). + optional bool post_training_quantize = 26 [default = false]; + + // This flag only works when converting to TensorFlow Lite format. + // When enabled, unsupported ops will be converted to select TensorFlow ops. + // `enable_select_tf_ops` should always be used with `allow_custom_ops`. + // WARNING: Experimental interface, subject to change + optional bool enable_select_tf_ops = 27 [default = false]; + + // This flag only works when converting to TensorFlow Lite format. + // When enabled, all TensorFlow ops will be converted to select TensorFlow + // ops. + // This will force `enable_select_tf_ops` to true. + // `force_select_tf_ops` should always be used with `enable_select_tf_ops`. + // WARNING: Experimental interface, subject to change + optional bool force_select_tf_ops = 28 [default = false]; + + // Boolean indicating whether to convert float32 constant buffers to + // float16. This is typically done to reduce model size. Delegates may also + // wish to implement kernels on reduced precision floats for performance + // gains. + optional bool quantize_to_float16 = 29 [default = false]; + + // Boolean flag indicating whether the converter should allow models with + // dynamic Tensor shape. When set to False, the converter will generate + // runtime memory offsets for activation Tensors (with 128 bits alignment) + // and error out on models with undetermined Tensor shape. (Default: True) + optional bool allow_dynamic_tensors = 30 [default = true]; + + // Full filepath of the folder to dump conversion logs. This includes a global + // view of the conversion process, and user can choose to submit those logs. + optional string conversion_summary_dir = 31; + + // String representing the custom ops OpDefs that are included in the + // GraphDef. + // Deprecated do not use. + repeated string custom_opdefs = 32 [deprecated = true]; + + // Name of user's defined Tensorflow ops required in the TensorFlow Lite + // runtime. These ops will be supported as select TensorFlow ops. + repeated string select_user_tf_ops = 33; + + // Whether to enable tflite resource variables during conversion or not. + // Note: This is an experimental feature. + optional bool enable_tflite_resource_variables = 34 [default = true]; + + // Whether to unfold tf.BatchMatMul to a set of tfl.fully_connected ops. If + // not, translate to tfl.batch_matmul. + // WARNING: Experimental interface, subject to change. + optional bool unfold_batchmatmul = 35 [default = false]; + + // Whether to lower static Tensor List ops to builtin ops. If not, use Flex + // tensor list ops. + // WARNING: Experimental interface, subject to change. + optional bool lower_tensor_list_ops = 36 [default = true]; + + // The accumulation type to use when quantize_to_float16 is true. Typical + // choices would be either float16 or float32. + optional IODataType accumulation_type = 37; + + // Whether this model supports inference in bfloat16. + // Note: This is an experimental feature. + optional bool allow_bfloat16 = 38 [default = false]; + + // If true, automatically adds all tf ops into the model as select Tensorflow + // ops. + optional bool allow_all_select_tf_ops = 39; + + // Whether to unfold large splat constant tensors in the flatbuffer to reduce + // model size. + optional bool unfold_large_splat_constant = 40 [default = false]; + + // Name of TFLite backends which are needed to check compatibility. + // WARNING: Experimental interface, subject to change. + repeated string supported_backends = 41; + + // Whether to force to use batch size one when the batch size is None during + // lowering tensor list ops. + optional bool default_to_single_batch_in_tensor_list_ops = 42 + [default = false]; + + // Disable per_channel quantization for dynamic range quantization. + // Note: This is an experimental feature + optional bool disable_per_channel_quantization = 43 [default = false]; + + // If false, the old TOCO dynamic range quantization is used. + // Note: This is an experimental feature + optional bool enable_mlir_dynamic_range_quantizer = 44 [default = false]; + + // When the output model is used for TF Quantization, this flag indicates the + // mode of TF Quantization. Ex: DEFAULT, LEGACY_INTEGER,... + optional string tf_quantization_mode = 45; + + // Disable inferring tensor range for quantization. + // Note: This is an experimental feature + optional bool disable_infer_tensor_range = 46 [default = false]; + + // Enable using num bits set in fake quant attributes for quantization. + // Note: This is an experimental feature + optional bool use_fake_quant_num_bits = 47 [default = false]; + + // Enable converting to DynamicUpdateSlice op (for ops like TensorListSetItem) + // Note: This is an experimental feature + optional bool enable_dynamic_update_slice = 48 [default = false]; + + // Whether to preserve `TF::AssertOp`. + optional bool preserve_assert_op = 49 [default = false]; + + // Whether to ensure each function has a single use. + optional bool guarantee_all_funcs_one_use = 50 [default = false]; + + // Whether to convert model to stablehlo. + optional bool convert_to_stablehlo = 51 [default = false]; + + // If false, skip the variable quantization passes. + // Note: This is an experimental feature + optional bool enable_mlir_variable_quantization = 52 [default = false]; + + // If true, disable folding mul->fc as in layer norm during optimize pass. + optional bool disable_fuse_mul_and_fc = 53 [default = false]; + + // Flag to enable hlo to tf conversion. + // This is useful to exercise StableHLO -> HLO -> TF -> TFLite path. + optional bool enable_hlo_to_tf_conversion = 55 + [default = false, deprecated = true]; + + // Additional parameters for controlling debug facilities. + optional tensorflow.converter.DebugOptions debug_options = 56; + + // If true, TFlite will use offsets instead of flatbuffers array to store + // buffers and custom options Note: This is an experimental feature + optional bool use_buffer_offset = 57 [default = false]; + + // Whether to legalize "tf.TensorList*" ops to custom tflite if they + // can all be supported. + optional bool legalize_custom_tensor_list_ops = 58 [default = false]; + + // Whether to convert some tensor types to a lower precision if all values + // within that tensor are within the range of the lower precision. This could + // have side effects e.g. reduced flatbuffer size. Only certain type + // conversions are supported. + // WARNING: Experimental interface, subject to change. + optional bool reduce_type_precision = 59 [default = false]; + + // Whether to consider this model a quantized model with quantize/dequantize + // ops and to convert kernels to quantized kernels wherever appropriate. + // WARNING: Experimental interface, subject to change. + optional string qdq_conversion_mode = 60 [default = "NONE"]; + + // Disables per channel weights quantization for Dense layers and enables + // legacy per tensor quantization. The legacy quantization for Dense layers is + // inconsistent with Conv 1x1 which always performs per channel quantization. + optional bool disable_per_channel_quantization_for_dense_layers = 62 + [default = false]; + + // Enables the attempt to directly lower composites into tflite ops. + // WARNING: Experimental interface, subject to change. + optional bool enable_composite_direct_lowering = 63 [default = false]; + + // The source model framework. + enum ModelOriginFramework { + UNSET = 0; + TENSORFLOW = 1; + KERAS = 2; + JAX = 3; + PYTORCH = 4; + } + + // The source model type. + optional ModelOriginFramework model_origin_framework = 64 [default = UNSET]; + + // When set to true, convert +Inf/-Inf to MIN/MAX float value and output of + // convert only contains finite values. + optional bool canonicalizing_inf_as_min_max_float = 65 [default = false]; + + // When set to true, debug metadata will be generated and attached to + // serialized TFLite flatbuffer. + optional bool serialize_debug_metadata = 66 [default = false]; + + // When set, adheres to the QDQ annotations added by the framework when + // possible rather than quantizing any op that is possible to quantize. + // WARNING: Experimental interface, subject to change. + optional bool strict_qdq_mode = 67 [default = false]; + + // When set to true, allows fusion of dynamic shaped broadcast ops. It helps + // fusing implicit broadcasting ops when output shape has dynamic dimensions, + // but it may cause incorrect results when broadcasting ops are introduced by + // explicit broadcasting in the source model. + optional bool unsafe_fuse_dynamic_shaped_broadcast = 68 [default = false]; + + // If false, downcast x64 tensors and inputs to x32. + optional bool enable_x64 = 69 [default = true]; + + // If true, enable unsafe single batch rank reduction. + optional bool unsafe_single_batch_rank_reduction = 70 [default = false]; +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/converter_gen.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/converter_gen.cc new file mode 100644 index 0000000000000000000000000000000000000000..3c1260ee0a073f0e794da84b4df1bcff04a6eb39 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/converter_gen.cc @@ -0,0 +1,652 @@ +/* Copyright 2019 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 "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/FormatVariadic.h" +#include "llvm/Support/InitLLVM.h" +#include "llvm/Support/Signals.h" +#include "llvm/TableGen/Error.h" +#include "llvm/TableGen/Main.h" +#include "llvm/TableGen/Record.h" +#include "llvm/TableGen/TableGenBackend.h" +#include "mlir/TableGen/Attribute.h" // from @llvm-project +#include "mlir/TableGen/Format.h" // from @llvm-project +#include "mlir/TableGen/Operator.h" // from @llvm-project +#include "mlir/TableGen/Predicate.h" // from @llvm-project + +using llvm::DefInit; +using llvm::dyn_cast; +using llvm::formatv; +using llvm::LessRecord; +using llvm::raw_ostream; +using llvm::Record; +using llvm::RecordKeeper; +using llvm::RecordRecTy; +using llvm::SmallVector; +using llvm::StringInit; +using llvm::StringRef; + +enum ActionType { + OpConv, + RuntimeVerify, +}; + +// NOLINTNEXTLINE +llvm::cl::opt action( + llvm::cl::desc("Action to perform:"), + llvm::cl::values(clEnumValN(OpConv, "gen-operator-converters", + "Generate operator converters"), + clEnumValN(RuntimeVerify, "gen-runtime-verifiers", + "Generate TFLite runtime verifiers"))); + +// Returns the associated option name for the given op definition. +static inline std::string GetOperatorOptionName(const Record &def) { + assert(def.getName().starts_with("TFL_") && "unexpected op prefix"); + assert(def.getName().ends_with("Op") && "unexpected op suffix"); + + auto *custom_option = dyn_cast(def.getValueInit("customOption")); + std::ostringstream oss; + if (custom_option) + oss << custom_option->getValue().str(); + else + oss << def.getName().drop_front(4).drop_back(2).str() << "Options"; + return oss.str(); +} + +// Returns the builder function name for the given op definition. +static inline std::string GetOperatorBuilderName(StringRef op_name) { + assert(op_name.starts_with("TFL_") && "unexpected op prefix"); + assert(op_name.ends_with("Op") && "unexpected op suffix"); + + // E.g., AddOp -> CreateAddOperator + std::ostringstream oss; + oss << "Create" << op_name.drop_front(4).str() << "erator"; + return oss.str(); +} + +static inline bool IsLstmOp(const StringRef op_name) { + return op_name.take_back(6) == "LSTMOp"; +} + +static int HasOptions(const Record &def) { + if (def.getValueAsBit("hasOptions")) { + return 1; + } + if (def.getValueAsBit("hasOptions2")) { + return 2; + } + return 0; +} + +static void EmitOptionBuilders(const RecordKeeper &record_keeper, + const std::vector &defs, + raw_ostream *ostream) { + raw_ostream &os = *ostream; + + const auto attr_type = record_keeper.getClass("Attr"); + for (const auto *def : defs) { + const int has_options = HasOptions(*def); + // TFLite ops without options are skipped over. + if (!has_options) { + continue; + } + + StringRef op_name = def->getName().drop_front(4); // Strip 'TFL_' prefix + std::string option_name = GetOperatorOptionName(*def); + std::string tflite_option_name = + option_name == "BasicLSTMOptions" ? "LSTMOptions" : option_name; + + os << "flatbuffers::Offset Create" + << option_name << "(mlir::TFL::" << op_name + << " op, flatbuffers::FlatBufferBuilder *fbb) {\n"; + + // Construct all the builder option needed. + SmallVector options; + // Add options due to attributes (not-derived). + auto *arg_values = def->getValueAsDag("arguments"); + mlir::tblgen::Operator op(*def); + for (unsigned i = 0, e = arg_values->getNumArgs(); i != e; ++i) { + auto arg = arg_values->getArg(i); + const auto *arg_def = dyn_cast(arg); + if (!arg_def) continue; + if (arg_def->getDef()->isSubClassOf(attr_type)) { + // This binds the name of the attribute in the TD file with the name + // of the add function of the builder and also with the conversion + // function to convert from the internal representation to the format + // expected by the flatbuffer builder. While this constrains the + // naming of the ops/attributes in the TD file, this also removes the + // need for specifying indirection. This tool is specific to TFLite + // conversion generation and so the simplicity was chosen over the + // flexibility. + StringRef arg_name = arg_values->getArgNameStr(i); + // Skip any "intermiadiateXXX" attribute as they are specially handled + // in the exporter. They are special because though they are attributes + // in the MLIR they are expressed as tensors in the flatbuffer instead + // of option. + if (IsLstmOp(op_name) && arg_name.take_back(12) == "intermediate") + continue; + os << formatv( + " auto {0} = Convert{1}ForOptionWriter(op.{2}(), fbb);\n", + arg_name, mlir::tblgen::Attribute(arg_def).getAttrDefName(), + op.getGetterName(arg_name)); + options.push_back(arg_name.str()); + } + } + + // Add options due to derived attributes. + for (const auto &val : def->getValues()) { + if (auto *record = dyn_cast(val.getType())) { + if (record->isSubClassOf(attr_type)) { + if (record->getClasses().size() != 1) { + PrintFatalError( + def->getLoc(), + "unsupported attribute modelling, only single class expected"); + } + os << formatv( + " auto {0} = Convert{1}ForOptionWriter(op.{2}(), fbb);\n", + val.getName(), record->getClasses()[0]->getName(), + op.getGetterName(val.getName())); + options.push_back(std::string(val.getName())); + } + } + } + + os << " tflite::" << tflite_option_name << "Builder b(*fbb);\n"; + for (const auto &option : options) + os << formatv(" b.add_{0}(std::move({0}));\n", option); + os << " return b.Finish();\n}\n"; + } +} + +// For each TFLite op, emits a builder function that packs the TFLite op into +// the corresponding FlatBuffer object. +// +// TODO(hinsu): Revisit if only builtin_options and mutating_variable_inputs +// arguments that depend on op definitions should be auto-generated and then +// operator should be built by the caller because it does not require +// auto-generation. +static void EmitOperatorBuilders(const std::vector &defs, + raw_ostream *ostream) { + raw_ostream &os = *ostream; + + for (const auto *def : defs) { + StringRef op_name = def->getName().drop_front(4); + + const bool has_intermediates = op_name.take_back(6) == "LSTMOp"; + // Signature + os << "static flatbuffers::Offset " + << GetOperatorBuilderName(def->getName()) << "(mlir::TFL::" << op_name + << " tflOp, uint32_t opcode_index, " + << "const std::vector& operands," + << "const std::vector& results," + << (has_intermediates ? "const std::vector& intermediate_index," + : "") + << "flatbuffers::FlatBufferBuilder *fbb," + << "int debug_metadata_index) {\n"; + + // Inputs & outputs + os << " auto inputs = fbb->CreateVector(operands);\n" + " auto outputs = fbb->CreateVector(results);\n\n"; + // Intermediates for LSTM. + if (has_intermediates) { + os << " auto intermediates = fbb->CreateVector(intermediate_index);\n"; + } + + // Build the FlatBuffer operator + os << " return tflite::CreateOperator(\n" + " *fbb, opcode_index, inputs, outputs,\n"; + const int has_options = HasOptions(*def); + if (has_options == 1) { + auto option_name = GetOperatorOptionName(*def); + std::string tflite_option_name = + option_name == "BasicLSTMOptions" ? "LSTMOptions" : option_name; + os << " tflite::BuiltinOptions_" << tflite_option_name << ", " + << "Create" << option_name << "(tflOp, fbb).Union(),\n"; + } else { + os << " tflite::BuiltinOptions_NONE, /*builtin_options=*/0,\n"; + } + // Only built-in ops' builders are auto-generated. custom_options are only + // used by custom or flex ops and those ops are handled manually. + os << " /*custom_options=*/0, " + << "tflite::CustomOptionsFormat_FLEXBUFFERS,\n" + << " /*mutating_variable_inputs=*/0," + << (has_intermediates ? "intermediates" : "/*intermediates=*/0"); + + if (has_options == 2) { + os << ",\n" + << " /*large_custom_options_offset=*/0,\n" + << " /*large_custom_options_size=*/0"; + os << ",\n"; + const std::string option_name = GetOperatorOptionName(*def); + os << " tflite::BuiltinOptions2_" << option_name << ", " + << "Create" << option_name << "(tflOp, fbb).Union()"; + } else { + os << ",\n" + << " /*large_custom_options_offset=*/0,\n" + << " /*large_custom_options_size=*/0"; + os << ",\n"; + os << " tflite::BuiltinOptions2_NONE, /*builtin_options2=*/0"; + } + + os << ",\n" + << " /*debug_metadata_index=*/debug_metadata_index"; + + os << ");\n}\n\n"; + } +} + +static inline std::string GetOperatorName(const Record &def) { + auto name = def.getValueAsString("opName"); + // Special case for basic_lstm. + if (name == "basic_lstm") { + return "LSTM"; + } + return name.upper(); +} + +// Emits a function that returns built-in operator code for each TFLite op. +// +// The signature of the function is: +// +// std::optional +// mlir::GetBuiltinOpCode(mlir::Operation* op); +// +// TODO(hinsu): Consider converting this to a static constant associative +// container instead of a series of if conditions, if required. +static void EmitGetBuiltinOpCode(const std::vector &defs, + raw_ostream *ostream) { + raw_ostream &os = *ostream; + + os << "std::optional " + "mlir::GetBuiltinOpCode(mlir::Operation* op) {\n"; + + for (const auto *def : defs) { + StringRef op_name = def->getName().drop_front(4); + auto operator_name = GetOperatorName(*def); + os << " if (isa(op))\n" + << " return tflite::BuiltinOperator_" << operator_name << ";\n"; + } + + os << " return std::nullopt;\n" + "}\n"; +} + +// Emits functions that return the min/max operand numbers for a given tflite op +// name. +// +// Signature: +// llvm::MinMax mlir::OperandNumbersMinMax(llvm::StringRef op_name) { +// if(const auto *op = op_union.AsOptions()) { +// return {min, max}; +// } +// ... +// return {0, 0}; +// } +static void EmitOperandNumbers(const RecordKeeper &record_keeper, + const std::vector &defs, + raw_ostream *ostream) { + raw_ostream &os = *ostream; + const auto attr_type = record_keeper.getClass("Attr"); + const auto optional_tensor = record_keeper.getClass("TFL_TensorOfOrNone"); + os << "llvm::MinMax mlir::OperandNumbersMinMax(llvm::StringRef op_name) {\n"; + for (const auto *def : defs) { + auto op_name = def->getValueAsString("opName"); + int tail_optional_tensor = 0, tensor_number_max = 0; + auto *arg_values = def->getValueAsDag("arguments"); + for (int i = 0, e = arg_values->getNumArgs(); i < e; ++i) { + auto arg = arg_values->getArg(i); + auto *arg_def = dyn_cast(arg); + if (!arg_def) continue; + if (!arg_def->getDef()->isSubClassOf(attr_type)) { + tensor_number_max++; + if (arg_def->getDef()->isSubClassOf(optional_tensor)) { + tail_optional_tensor++; + } else { + tail_optional_tensor = 0; + } + } + } + const int tensor_number_min = tensor_number_max - tail_optional_tensor; + + os << formatv(" if (op_name == \"tfl.{0}\") {{\n", op_name) + << " return {" << tensor_number_min << ", " << tensor_number_max + << "};\n }\n"; + } + os << " return {0, 0};\n}\n"; +} + +// Emits a builder function that returns the packed FlatBuffer object given +// a general mlir::Operation. +// +// The signature of the function is: +// +// std::optional> +// mlir::CreateFlatBufferOperator( +// mlir::Operation* op, +// uint32_t opcode_index, +// const std::vector& operands, +// const std::vector& results, +// const std::vector& intermediates, +// flatbuffers::FlatBufferBuilder *fbb, +// std::optional debug_metadata_index); +static void EmitBuildOperator(const std::vector &defs, + raw_ostream *ostream) { + raw_ostream &os = *ostream; + + // Signature + os << "std::optional>\n" + "mlir::CreateFlatBufferOperator(mlir::Operation* op, " + "uint32_t opcode_index, " + "const std::vector& operands," + "const std::vector& results," + "const std::vector& intermediates," + "flatbuffers::FlatBufferBuilder *fbb," + "std::optional debug_metadata_index) {\n"; + + for (const auto *def : defs) { + StringRef op_name = def->getName().drop_front(4); + + // Try to cast to each op case and call the corresponding op builder + os << " if (auto tflOp = llvm::dyn_cast(op))\n" + << " return " << GetOperatorBuilderName(def->getName()) + << "(tflOp, opcode_index, operands, results, " + << (op_name.take_back(6) == "LSTMOp" ? "intermediates, " : "") + << "fbb, debug_metadata_index.value_or(-1));\n"; + } + + os << " return std::nullopt;\n" + "}\n"; +} + +// Emit a function that converts a BuiltinOptionsUnion to a vector of attributes +// Signature: +// void mlir::BuiltinOptions{id}ToAttributes( +// tflite::BuiltinOptions{id}Union op_union, +// mlir::Builder builder, +// llvm::SmallVectorImpl &attributes); +// +// where id is an empty string if builtin_options_id is 1, or builtin_options_id +// otherwise. +static void EmitBuiltinOptionsToAttributes( + const RecordKeeper &record_keeper, const std::vector &defs, + raw_ostream *ostream, const int builtin_options_id) { + raw_ostream &os = *ostream; + + const std::string builtin_options_suffix = [&] { + switch (builtin_options_id) { + case 1: + return ""; + case 2: + return "2"; + } + return "UnknownId"; + }(); + + // Signature + os << "void mlir::BuiltinOptions" << builtin_options_suffix + << "ToAttributes(" + "tflite::BuiltinOptions" + << builtin_options_suffix + << "Union op_union, " + "mlir::Builder builder, " + "llvm::SmallVectorImpl &attributes) {\n"; + + const auto attr_type = record_keeper.getClass("Attr"); + for (const auto *def : defs) { + const int has_options = HasOptions(*def); + if (has_options != builtin_options_id) { + continue; + } + auto option_name = GetOperatorOptionName(*def); + // Basic LSTM and LSTM ops share the same option to attribute converter. + if (option_name == "BasicLSTMOptions") { + continue; + } + + os << formatv(" if(const auto *op = op_union.As{0}()) {{\n", option_name); + + // We only care about options that are in arguments + auto *arg_values = def->getValueAsDag("arguments"); + for (unsigned i = 0, e = arg_values->getNumArgs(); i != e; ++i) { + auto arg = arg_values->getArg(i); + const auto *arg_def = dyn_cast(arg); + if (!arg_def) continue; + if (arg_def->getDef()->isSubClassOf(attr_type)) { + StringRef arg_name = arg_values->getArgNameStr(i); + // Already handle this case in flatbuffer_import.cc. + if ((option_name == "LSTMOptions" || + option_name == "UnidirectionalSequenceLSTMOptions") && + arg_name.take_back(12) == "intermediate") + continue; + StringRef attr_type = mlir::tblgen::Attribute(arg_def).getAttrDefName(); + os << formatv( + " attributes.emplace_back(builder.getNamedAttr(\"{0}\"," + " Build{1}(op->{0}, builder)));\n", + arg_name, attr_type); + } + } + + os << " return;\n"; + os << " }\n"; + } + if (builtin_options_id == 2) { + os << " BuiltinOptions2ToAttributesManual(op_union, builder, " + "attributes);\n"; + } + // Fallthrough case is no attributes + os << "}"; +} + +// The function below has a non-constant reference as that is required by LLVM's +// TableGenMain. +// NOLINTNEXTLINE +static bool OperatorWritersMain(raw_ostream &os, const RecordKeeper &records) { + emitSourceFileHeader("MLIR TFLite FlatBuffer Builders", os); + + // Retrieve all the definitions derived from TFL_Op and sort by record name. + std::vector defs = records.getAllDerivedDefinitions("TFL_Op"); + llvm::sort(defs, LessRecord()); + + for (const auto *def : defs) { + // TFLite ops in the .td file are expected to follow the naming convention: + // TFL_Op. + // The generated TFLite op C++ class should be TFL::Op. + // The generated operator's options should be tflite::Options. + // The option builder should be CreateOptions. + if (!def->getName().starts_with("TFL_")) + PrintFatalError(def->getLoc(), + "unexpected op name format: 'TFL_' prefix missing"); + if (!def->getName().ends_with("Op")) + PrintFatalError(def->getLoc(), + "unexpected op name format: 'Op' suffix missing"); + } + + EmitOptionBuilders(records, defs, &os); + os << "\n\n"; + EmitOperatorBuilders(defs, &os); + os << "\n\n"; + EmitGetBuiltinOpCode(defs, &os); + os << "\n\n"; + EmitBuildOperator(defs, &os); + os << "\n\n"; + EmitBuiltinOptionsToAttributes(records, defs, &os, /*builtin_options_id=*/1); + os << "\n\n"; + EmitBuiltinOptionsToAttributes(records, defs, &os, /*builtin_options_id=*/2); + os << "\n\n"; + EmitOperandNumbers(records, defs, &os); + + return false; +} + +static void GenOperandResultVerifier(raw_ostream &os, + llvm::ArrayRef values, + StringRef valueKind) { + mlir::tblgen::FmtContext fctx; + + bool first = true; + for (const auto &static_value : llvm::enumerate(values)) { + auto *definit = llvm::cast(static_value.value()); + auto *val = definit->getDef()->getValue("tflRuntimeTypePredicate"); + if (!val) continue; + + // Create code block on first type to verify. + if (first) { + os << " {\n"; + os << " unsigned index = " << static_value.index() << ";\n"; + first = false; + } + + mlir::tblgen::Pred pred(dyn_cast(val->getValue())); + auto desc = + definit->getDef()->getValueAsString("tflRuntimeTypeDescription"); + + // Emit a loop to check all operands. + os << formatv(" for (Value v : top.getODS{0}{1}s({2})) {{\n", + // Capitalize the first letter to match the function name + valueKind.substr(0, 1).upper(), valueKind.substr(1), + static_value.index()); + + os << " (void)v;\n" + << " if (!(" + << tgfmt(pred.getCondition(), &fctx.withSelf("v.getType()")) << ")) {\n" + << " if (emit_error_on_verify_fail) {\n" + << formatv( + " return op->emitOpError(\"{0} #\") << index " + "<< \" must be {1}, but got \" << v.getType();\n", + valueKind, desc) + << " } else {\n" + << " return failure();\n" + << " }\n" + << " }\n" // if + << " ++index;\n" + << " }\n"; // for + } + + // Emit closing brace if needed. + if (!first) os << " }\n"; +} + +// NOLINTNEXTLINE +static bool RuntimeVerifierWriterMain(raw_ostream &os, + const RecordKeeper &records) { + emitSourceFileHeader("MLIR TFLite Runtime Verifiers", os); + + // Retrieve all the definitions derived from TFL_Op and sort by record name. + std::vector defs = records.getAllDerivedDefinitions("Op"); + llvm::sort(defs, LessRecord()); + + // Iterate through all the ops defined. + for (const auto *def : defs) { + mlir::tblgen::Operator op(*def); + if (!op.getTrait("TflRuntimeVerifyOpInterface::Trait")) continue; + + mlir::tblgen::FmtContext verify_ctx; + os << "::mlir::LogicalResult " << op.getCppClassName() + << "::VerifyTflRuntimeConstraints(::mlir::Operation *op, bool " + "emit_error_on_verify_fail) {\n"; + os << " auto top = cast<" << op.getCppClassName() << ">(op); (void)top;\n"; + verify_ctx.addSubst("_op", "(*op)"); + + for (int i = 0, e = op.getNumOperands(); i < e; ++i) { + auto &value = op.getOperand(i); + // Skip from first variadic operands for now. Else getOperand index used + // below doesn't match. + if (value.isVariableLength()) break; + if (!value.name.empty()) + verify_ctx.addSubst(value.name, formatv("op->getOperand({0})", i)); + } + for (int i = 0, e = op.getNumResults(); i < e; ++i) { + auto &value = op.getResult(i); + // Skip from first variadic results for now. Else getResult index used + // below doesn't match. + if (value.isVariableLength()) break; + if (!value.name.empty()) + verify_ctx.addSubst(value.name, formatv("op->getResult({0})", i)); + } + GenOperandResultVerifier(os, def->getValueAsDag("arguments")->getArgs(), + "operand"); + GenOperandResultVerifier(os, def->getValueAsDag("results")->getArgs(), + "result"); + + for (auto &trait : op.getTraits()) { + if (!trait.getDef().isSubClassOf("GenInternalOpTrait")) { + continue; + } + if (trait.getDef().getValueAsString("trait") != + "::mlir::OpTrait::TFLRuntimeOpTrait") { + continue; + } + + auto *val = trait.getDef().getValue("tflRuntimePredicate"); + if (!val) continue; + + auto desc = trait.getDef().getValueAsString("tflRuntimeDescription"); + + mlir::tblgen::Pred pred(dyn_cast(val->getValue())); + os << tgfmt( + " if (!($0)) {\n " + " if (emit_error_on_verify_fail) {\n" + " return top.emitOpError(\"failed to verify that $1\");\n" + " } else {\n" + " return failure();\n }\n }\n", + &verify_ctx, tgfmt(pred.getCondition(), &verify_ctx), desc); + } + os << " if (!emit_error_on_verify_fail) {\n"; + os << "// Ignore transient errors by registering an no-op handler.\n" + "// Applying legalization patterns will emit unwanted, transient \n" + "// errors when the replaced TFLite ops do not meet the sanity \n" + "// checks. \n" + "// In order to ignore the transient errors, the following lines \n" + "// override a diagnostic handler with an no-op handler only\n" + "// while this pass runs.\n" + "uint64_t current_thread_id = llvm::get_threadid();\n" + "ScopedDiagnosticHandler scoped_diag_handler(\n" + " top.getContext(), [¤t_thread_id](Diagnostic&) -> " + "LogicalResult " + "{\n" + " // Consume only errors that are coming from the same thread " + "in order not\n" + " // to ignore errors from other passes that are running. Things\n" + " // running\n" + " // in the pass manager can be multi-threaded.\n" + " return success(current_thread_id == llvm::get_threadid());\n" + " });\n"; + os << " return top.verifyInvariants();\n"; + os << " } else {\n"; + os << " return top.verifyInvariants();\n}\n"; + os << "}\n"; + } + + return false; +} + +int main(int argc, char **argv) { + llvm::InitLLVM y(argc, argv); + llvm::cl::ParseCommandLineOptions(argc, argv); + if (action == ActionType::OpConv) + return TableGenMain(argv[0], &OperatorWritersMain); + return TableGenMain(argv[0], &RuntimeVerifierWriterMain); +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..17892504391d230de0f3fa39be5a255dd5f3bfcd --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/BUILD @@ -0,0 +1,55 @@ +load("@rules_cc//cc:cc_library.bzl", "cc_library") +load("//tensorflow:tensorflow.default.bzl", "get_compatible_with_portable") +load("//tensorflow/compiler/mlir/lite:build_def.bzl", "tflite_copts_warnings") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + licenses = ["notice"], +) + +exports_files( + [ + "model_builder_base.h", + ], + visibility = ["//tensorflow/lite/core:__pkg__"], +) + +cc_library( + name = "macros", + hdrs = ["macros.h"], + compatible_with = get_compatible_with_portable(), + visibility = ["//visibility:public"], +) + +cc_library( + name = "model_builder_base", + srcs = ["model_builder_base.cc"], + hdrs = ["model_builder_base.h"], + compatible_with = get_compatible_with_portable(), + copts = tflite_copts_warnings(), + visibility = ["//visibility:public"], + deps = [ + ":macros", + "//tensorflow/compiler/mlir/lite:allocation", + "//tensorflow/compiler/mlir/lite/core/api:error_reporter", + "//tensorflow/compiler/mlir/lite/core/api:verifier", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs", + "@com_google_absl//absl/strings", + "@flatbuffers", + ], + alwayslink = 1, +) + +cc_library( + name = "absl_error_model_builder", + srcs = ["absl_error_model_builder.cc"], + hdrs = ["absl_error_model_builder.h"], + compatible_with = get_compatible_with_portable(), + copts = tflite_copts_warnings(), + visibility = ["//visibility:public"], + deps = [ + ":model_builder_base", + "//tensorflow/compiler/mlir/lite/core/api:error_reporter", + "@com_google_absl//absl/log", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/absl_error_model_builder.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/absl_error_model_builder.cc new file mode 100644 index 0000000000000000000000000000000000000000..678d3efe843f202ffcfbc068998067dfd919549b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/absl_error_model_builder.cc @@ -0,0 +1,40 @@ +/* Copyright 2024 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/compiler/mlir/lite/core/absl_error_model_builder.h" + +#include +#include + +#include "absl/log/log.h" +#include "tensorflow/compiler/mlir/lite/core/api/error_reporter.h" + +namespace mlir::TFL { + +int AbslErrorReporter::Report(const char* format, va_list args) { + char buffer[1024]; +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wformat-nonliteral" + vsprintf(buffer, format, args); +#pragma clang diagnostic pop + LOG(ERROR) << buffer; + return 0; +} + +tflite::ErrorReporter* GetAbslErrorReporter() { + static AbslErrorReporter* error_reporter = new AbslErrorReporter; + return error_reporter; +} + +} // namespace mlir::TFL diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/absl_error_model_builder.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/absl_error_model_builder.h new file mode 100644 index 0000000000000000000000000000000000000000..b98a4fef34ac59e54940378a57c04a3d7b50588f --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/absl_error_model_builder.h @@ -0,0 +1,47 @@ +/* Copyright 2024 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_CORE_ABSL_ERROR_MODEL_BUILDER_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_CORE_ABSL_ERROR_MODEL_BUILDER_H_ + +#include + +#include "tensorflow/compiler/mlir/lite/core/api/error_reporter.h" +#include "tensorflow/compiler/mlir/lite/core/model_builder_base.h" + +namespace mlir::TFL { + +// An error reporter that uses absl logging. +class AbslErrorReporter : public tflite::ErrorReporter { + int Report(const char* format, va_list args) override; +}; + +tflite::ErrorReporter* GetAbslErrorReporter(); + +class FlatBufferModelAbslError + : public tflite::impl::FlatBufferModelBase { + public: + // Use stderr_reporter as the default error reporter. + static tflite::ErrorReporter* GetDefaultErrorReporter() { + return GetAbslErrorReporter(); + } + + // Inherit all constructors from FlatBufferModelBase since inherited factory + // methods refer to them. + using FlatBufferModelBase::FlatBufferModelBase; +}; + +} // namespace mlir::TFL + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_CORE_ABSL_ERROR_MODEL_BUILDER_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/api/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/api/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..22f42d243cd6863fb6542c9953ba4ee00c921998 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/api/BUILD @@ -0,0 +1,84 @@ +load("@rules_cc//cc:cc_library.bzl", "cc_library") +load("//tensorflow:tensorflow.bzl", "tf_cc_test") +load("//tensorflow:tensorflow.default.bzl", "get_compatible_with_portable") +load("//tensorflow/compiler/mlir/lite:build_def.bzl", "tflite_copts") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + default_visibility = ["//visibility:public"], + licenses = ["notice"], +) + +exports_files(["error_reporter.h"]) + +filegroup( + name = "tflite_internal_cc_3p_api_deps_src", + srcs = [ + "error_reporter.cc", + "error_reporter.h", + "verifier.h", + ], + visibility = ["//tensorflow/lite:__pkg__"], +) + +cc_library( + name = "error_reporter", + srcs = ["error_reporter.cc"], + hdrs = ["error_reporter.h"], + compatible_with = get_compatible_with_portable(), + copts = tflite_copts(), + deps = [], +) + +exports_files(["verifier.h"]) + +cc_library( + name = "verifier", + hdrs = ["verifier.h"], + compatible_with = get_compatible_with_portable(), + copts = tflite_copts(), + visibility = ["//visibility:public"], + deps = [":error_reporter"], +) + +tf_cc_test( + name = "error_reporter_test", + size = "small", + srcs = ["error_reporter_test.cc"], + deps = [ + ":error_reporter", + "@com_google_googletest//:gtest_main", + ], +) + +cc_library( + name = "flatbuffer_conversions", + srcs = ["flatbuffer_conversions.cc"], + hdrs = [ + "flatbuffer_conversions.h", + ], + compatible_with = get_compatible_with_portable(), + copts = tflite_copts(), + deps = [ + "//tensorflow/compiler/mlir/lite/core/c:tflite_common", + "//tensorflow/compiler/mlir/lite/kernels/internal:compatibility_macros", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs", + "@com_google_absl//absl/log:absl_log", + "@com_google_absl//absl/status", + "@com_google_absl//absl/strings:str_format", + "@flatbuffers//:runtime_cc", + ], +) + +tf_cc_test( + name = "flatbuffer_conversions_test", + size = "small", + srcs = ["flatbuffer_conversions_test.cc"], + deps = [ + ":flatbuffer_conversions", + "//tensorflow/compiler/mlir/lite/core/c:tflite_common", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs", + "@com_google_googletest//:gtest_main", + "@flatbuffers//:runtime_cc", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/api/error_reporter.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/api/error_reporter.cc new file mode 100644 index 0000000000000000000000000000000000000000..68f62eb5c39daf0fe09084034e2978b5816b795a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/api/error_reporter.cc @@ -0,0 +1,39 @@ +/* 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/compiler/mlir/lite/core/api/error_reporter.h" + +#include + +namespace tflite { + +int ErrorReporter::Report(const char* format, ...) { + va_list args; + va_start(args, format); + int code = Report(format, args); + va_end(args); + return code; +} + +// TODO(aselle): Make the name of ReportError on context the same, so +// we can use the ensure functions w/o a context and w/ a reporter. +int ErrorReporter::ReportError(void*, const char* format, ...) { + va_list args; + va_start(args, format); + int code = Report(format, args); + va_end(args); + return code; +} + +} // namespace tflite diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/api/error_reporter.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/api/error_reporter.h new file mode 100644 index 0000000000000000000000000000000000000000..e227168eae131444f9a3b64d37627d78092ac8b2 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/api/error_reporter.h @@ -0,0 +1,72 @@ +/* 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_CORE_API_ERROR_REPORTER_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_CORE_API_ERROR_REPORTER_H_ + +#include + +namespace tflite { + +/// A functor that reports error to supporting system. Invoked similar to +/// printf. +/// +/// Usage: +/// ErrorReporter foo; +/// foo.Report("test %d", 5); +/// or +/// va_list args; +/// foo.Report("test %d", args); // where args is va_list +/// +/// Subclass ErrorReporter to provide another reporting destination. +/// For example, if you have a GUI program, you might redirect to a buffer +/// that drives a GUI error log box. +class ErrorReporter { + public: + virtual ~ErrorReporter() = default; + /// Converts `args` to character equivalents according to `format` string, + /// constructs the error string and report it. + /// Returns number of characters written or zero on success, and negative + /// number on error. + virtual int Report(const char* format, va_list args) = 0; + + /// Converts arguments to character equivalents according to `format` string, + /// constructs the error string and report it. + /// Returns number of characters written or zero on success, and negative + /// number on error. + int Report(const char* format, ...); + + /// Equivalent to `Report` above. The additional `void*` parameter is unused. + /// This method is for compatibility with macros that takes `TfLiteContext`, + /// like TF_LITE_ENSURE and related macros. + int ReportError(void*, const char* format, ...); +}; + +} // namespace tflite + +// You should not make bare calls to the error reporter, instead use the +// TF_LITE_REPORT_ERROR macro, since this allows message strings to be +// stripped when the binary size has to be optimized. If you are looking to +// reduce binary size, define TF_LITE_STRIP_ERROR_STRINGS when compiling and +// every call will be stubbed out, taking no memory. +#ifndef TF_LITE_STRIP_ERROR_STRINGS +#define TF_LITE_REPORT_ERROR(reporter, ...) \ + do { \ + static_cast<::tflite::ErrorReporter*>(reporter)->Report(__VA_ARGS__); \ + } while (false) +#else // TF_LITE_STRIP_ERROR_STRINGS +#define TF_LITE_REPORT_ERROR(reporter, ...) +#endif // TF_LITE_STRIP_ERROR_STRINGS + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_CORE_API_ERROR_REPORTER_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/api/error_reporter_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/api/error_reporter_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..87b958687520982ae110a2a172810ccc850bbe5a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/api/error_reporter_test.cc @@ -0,0 +1,61 @@ +/* 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 "tensorflow/compiler/mlir/lite/core/api/error_reporter.h" + +#include + +#include + +namespace tflite { + +class MockErrorReporter : public ErrorReporter { + public: + MockErrorReporter() { buffer_[0] = 0; } + int Report(const char* format, va_list args) override { + vsnprintf(buffer_, kBufferSize, format, args); + return 0; + } + char* GetBuffer() { return buffer_; } + + private: + static constexpr int kBufferSize = 256; + char buffer_[kBufferSize]; +}; + +TEST(ErrorReporter, TestReport) { + MockErrorReporter mock_reporter; + ErrorReporter* reporter = &mock_reporter; + reporter->Report("Error: %d", 23); + EXPECT_EQ(0, strcmp(mock_reporter.GetBuffer(), "Error: 23")); +} + +TEST(ErrorReporter, TestReportMacro) { + MockErrorReporter mock_reporter; + // Only define the reporter if it's used, to avoid warnings. +#ifndef TF_LITE_STRIP_ERROR_STRINGS + ErrorReporter* reporter = &mock_reporter; +#endif // TFLITE_STRIP_ERROR_STRINGS + + TF_LITE_REPORT_ERROR(reporter, "Error: %d", 23); + +#ifndef TF_LITE_STRIP_ERROR_STRINGS + EXPECT_EQ(0, strcmp(mock_reporter.GetBuffer(), "Error: 23")); +#else // TF_LITE_STRIP_ERROR_STRINGS + EXPECT_EQ(0, strcmp(mock_reporter.GetBuffer(), "")); +#endif // TF_LITE_STRIP_ERROR_STRINGS +} + +} // namespace tflite diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/api/flatbuffer_conversions.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/api/flatbuffer_conversions.cc new file mode 100644 index 0000000000000000000000000000000000000000..3f61adb3079e8e1e27fd8ce54bc532678c28afdd --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/api/flatbuffer_conversions.cc @@ -0,0 +1,3243 @@ +/* Copyright 2024 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/compiler/mlir/lite/core/api/flatbuffer_conversions.h" + +#include +#include +#include +#include + +#include "absl/log/absl_log.h" +#include "absl/status/status.h" +#include "absl/strings/str_format.h" +#include "flatbuffers/vector.h" // from @flatbuffers +#include "tensorflow/compiler/mlir/lite/core/c/builtin_op_data.h" +#include "tensorflow/compiler/mlir/lite/core/c/tflite_types.h" +#include "tensorflow/compiler/mlir/lite/kernels/internal/compatibility_macros.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" + +/// Check whether the value `a` is true, and if not return +/// absl::InvalidArgumentError from the current function, while also +/// reporting the location of the error. +#define TFL_FILE_ENSURE(a) \ + do { \ + if (!(a)) { \ + auto error_message = \ + absl::StrFormat("%s:%d %s was not true.", __FILE__, __LINE__, #a); \ + ABSL_LOG(ERROR) << error_message; \ + return absl::InvalidArgumentError(error_message); \ + } \ + } while (0) + +#define TFL_FILE_ENSURE_STATUS(a) \ + do { \ + const absl::Status s = (a); \ + if (!s.ok()) { \ + return s; \ + } \ + } while (0) + +namespace tflite_file { +namespace flatbuffer_conversions { +using absl::OkStatus; +using tflite::ActivationFunctionType; +using tflite::ActivationFunctionType_NONE; +using tflite::ActivationFunctionType_RELU; +using tflite::ActivationFunctionType_RELU6; +using tflite::ActivationFunctionType_RELU_N1_TO_1; +using tflite::ActivationFunctionType_SIGN_BIT; +using tflite::ActivationFunctionType_TANH; +using tflite::AddOptions; +using tflite::ArgMaxOptions; +using tflite::ArgMinOptions; +using tflite::BuiltinOperator; +using tflite::BuiltinOperator_ABS; +using tflite::BuiltinOperator_ADD; +using tflite::BuiltinOperator_ADD_N; +using tflite::BuiltinOperator_ARG_MAX; +using tflite::BuiltinOperator_ARG_MIN; +using tflite::BuiltinOperator_ASSIGN_VARIABLE; +using tflite::BuiltinOperator_ATAN2; +using tflite::BuiltinOperator_AVERAGE_POOL_2D; +using tflite::BuiltinOperator_BATCH_MATMUL; +using tflite::BuiltinOperator_BATCH_TO_SPACE_ND; +using tflite::BuiltinOperator_BIDIRECTIONAL_SEQUENCE_LSTM; +using tflite::BuiltinOperator_BIDIRECTIONAL_SEQUENCE_RNN; +using tflite::BuiltinOperator_BITCAST; +using tflite::BuiltinOperator_BITWISE_XOR; +using tflite::BuiltinOperator_BROADCAST_ARGS; +using tflite::BuiltinOperator_BROADCAST_TO; +using tflite::BuiltinOperator_BUCKETIZE; +using tflite::BuiltinOperator_CALL; +using tflite::BuiltinOperator_CALL_ONCE; +using tflite::BuiltinOperator_CAST; +using tflite::BuiltinOperator_CEIL; +using tflite::BuiltinOperator_COMPLEX_ABS; +using tflite::BuiltinOperator_CONCAT_EMBEDDINGS; +using tflite::BuiltinOperator_CONCATENATION; +using tflite::BuiltinOperator_CONV_2D; +using tflite::BuiltinOperator_CONV_3D; +using tflite::BuiltinOperator_CONV_3D_TRANSPOSE; +using tflite::BuiltinOperator_COS; +using tflite::BuiltinOperator_CUMSUM; +using tflite::BuiltinOperator_CUSTOM; +using tflite::BuiltinOperator_DELEGATE; +using tflite::BuiltinOperator_DENSIFY; +using tflite::BuiltinOperator_DEPTH_TO_SPACE; +using tflite::BuiltinOperator_DEPTHWISE_CONV_2D; +using tflite::BuiltinOperator_DEQUANTIZE; +using tflite::BuiltinOperator_DILATE; +using tflite::BuiltinOperator_DIV; +using tflite::BuiltinOperator_DYNAMIC_UPDATE_SLICE; +using tflite::BuiltinOperator_ELU; +using tflite::BuiltinOperator_EMBEDDING_LOOKUP; +using tflite::BuiltinOperator_EMBEDDING_LOOKUP_SPARSE; +using tflite::BuiltinOperator_EQUAL; +using tflite::BuiltinOperator_EXP; +using tflite::BuiltinOperator_EXPAND_DIMS; +using tflite::BuiltinOperator_FAKE_QUANT; +using tflite::BuiltinOperator_FILL; +using tflite::BuiltinOperator_FLOOR; +using tflite::BuiltinOperator_FLOOR_DIV; +using tflite::BuiltinOperator_FLOOR_MOD; +using tflite::BuiltinOperator_FULLY_CONNECTED; +using tflite::BuiltinOperator_GATHER; +using tflite::BuiltinOperator_GATHER_ND; +using tflite::BuiltinOperator_GELU; +using tflite::BuiltinOperator_GREATER; +using tflite::BuiltinOperator_GREATER_EQUAL; +using tflite::BuiltinOperator_HARD_SWISH; +using tflite::BuiltinOperator_HASHTABLE; +using tflite::BuiltinOperator_HASHTABLE_FIND; +using tflite::BuiltinOperator_HASHTABLE_IMPORT; +using tflite::BuiltinOperator_HASHTABLE_LOOKUP; +using tflite::BuiltinOperator_HASHTABLE_SIZE; +using tflite::BuiltinOperator_IF; +using tflite::BuiltinOperator_IMAG; +using tflite::BuiltinOperator_L2_NORMALIZATION; +using tflite::BuiltinOperator_L2_POOL_2D; +using tflite::BuiltinOperator_LEAKY_RELU; +using tflite::BuiltinOperator_LESS; +using tflite::BuiltinOperator_LESS_EQUAL; +using tflite::BuiltinOperator_LOCAL_RESPONSE_NORMALIZATION; +using tflite::BuiltinOperator_LOG; +using tflite::BuiltinOperator_LOG_SOFTMAX; +using tflite::BuiltinOperator_LOGICAL_AND; +using tflite::BuiltinOperator_LOGICAL_NOT; +using tflite::BuiltinOperator_LOGICAL_OR; +using tflite::BuiltinOperator_LOGISTIC; +using tflite::BuiltinOperator_LSH_PROJECTION; +using tflite::BuiltinOperator_LSTM; +using tflite::BuiltinOperator_MATRIX_DIAG; +using tflite::BuiltinOperator_MATRIX_SET_DIAG; +using tflite::BuiltinOperator_MAX_POOL_2D; +using tflite::BuiltinOperator_MAXIMUM; +using tflite::BuiltinOperator_MEAN; +using tflite::BuiltinOperator_MINIMUM; +using tflite::BuiltinOperator_MIRROR_PAD; +using tflite::BuiltinOperator_MUL; +using tflite::BuiltinOperator_MULTINOMIAL; +using tflite::BuiltinOperator_NEG; +using tflite::BuiltinOperator_NON_MAX_SUPPRESSION_V4; +using tflite::BuiltinOperator_NON_MAX_SUPPRESSION_V5; +using tflite::BuiltinOperator_NOT_EQUAL; +using tflite::BuiltinOperator_ONE_HOT; +using tflite::BuiltinOperator_PACK; +using tflite::BuiltinOperator_PAD; +using tflite::BuiltinOperator_PADV2; +using tflite::BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES; +using tflite::BuiltinOperator_POW; +using tflite::BuiltinOperator_PRELU; +using tflite::BuiltinOperator_QUANTIZE; +using tflite::BuiltinOperator_RANDOM_STANDARD_NORMAL; +using tflite::BuiltinOperator_RANDOM_UNIFORM; +using tflite::BuiltinOperator_RANGE; +using tflite::BuiltinOperator_RANK; +using tflite::BuiltinOperator_READ_VARIABLE; +using tflite::BuiltinOperator_REAL; +using tflite::BuiltinOperator_REDUCE_ALL; +using tflite::BuiltinOperator_REDUCE_ANY; +using tflite::BuiltinOperator_REDUCE_MAX; +using tflite::BuiltinOperator_REDUCE_MIN; +using tflite::BuiltinOperator_REDUCE_PROD; +using tflite::BuiltinOperator_REDUCE_WINDOW; +using tflite::BuiltinOperator_RELU; +using tflite::BuiltinOperator_RELU6; +using tflite::BuiltinOperator_RELU_0_TO_1; +using tflite::BuiltinOperator_RELU_N1_TO_1; +using tflite::BuiltinOperator_RESHAPE; +using tflite::BuiltinOperator_RESIZE_BILINEAR; +using tflite::BuiltinOperator_RESIZE_NEAREST_NEIGHBOR; +using tflite::BuiltinOperator_REVERSE_SEQUENCE; +using tflite::BuiltinOperator_REVERSE_V2; +using tflite::BuiltinOperator_RFFT2D; +using tflite::BuiltinOperator_RIGHT_SHIFT; +using tflite::BuiltinOperator_RNN; +using tflite::BuiltinOperator_ROUND; +using tflite::BuiltinOperator_RSQRT; +using tflite::BuiltinOperator_SCATTER_ND; +using tflite::BuiltinOperator_SEGMENT_SUM; +using tflite::BuiltinOperator_SELECT; +using tflite::BuiltinOperator_SELECT_V2; +using tflite::BuiltinOperator_SHAPE; +using tflite::BuiltinOperator_SIGN; +using tflite::BuiltinOperator_SIN; +using tflite::BuiltinOperator_SKIP_GRAM; +using tflite::BuiltinOperator_SLICE; +using tflite::BuiltinOperator_SOFTMAX; +using tflite::BuiltinOperator_SPACE_TO_BATCH_ND; +using tflite::BuiltinOperator_SPACE_TO_DEPTH; +using tflite::BuiltinOperator_SPARSE_TO_DENSE; +using tflite::BuiltinOperator_SPLIT; +using tflite::BuiltinOperator_SPLIT_V; +using tflite::BuiltinOperator_SQRT; +using tflite::BuiltinOperator_SQUARE; +using tflite::BuiltinOperator_SQUARED_DIFFERENCE; +using tflite::BuiltinOperator_SQUEEZE; +using tflite::BuiltinOperator_STABLEHLO_ABS; +using tflite::BuiltinOperator_STABLEHLO_ADD; +using tflite::BuiltinOperator_STABLEHLO_AND; +using tflite::BuiltinOperator_STABLEHLO_BROADCAST_IN_DIM; +using tflite::BuiltinOperator_STABLEHLO_CASE; +using tflite::BuiltinOperator_STABLEHLO_CBRT; +using tflite::BuiltinOperator_STABLEHLO_CLAMP; +using tflite::BuiltinOperator_STABLEHLO_COMPARE; +using tflite::BuiltinOperator_STABLEHLO_COMPOSITE; +using tflite::BuiltinOperator_STABLEHLO_CONCATENATE; +using tflite::BuiltinOperator_STABLEHLO_CONVERT; +using tflite::BuiltinOperator_STABLEHLO_CONVOLUTION; +using tflite::BuiltinOperator_STABLEHLO_COSINE; +using tflite::BuiltinOperator_STABLEHLO_CUSTOM_CALL; +using tflite::BuiltinOperator_STABLEHLO_DIVIDE; +using tflite::BuiltinOperator_STABLEHLO_DOT_GENERAL; +using tflite::BuiltinOperator_STABLEHLO_DYNAMIC_SLICE; +using tflite::BuiltinOperator_STABLEHLO_DYNAMIC_UPDATE_SLICE; +using tflite::BuiltinOperator_STABLEHLO_EXPONENTIAL; +using tflite::BuiltinOperator_STABLEHLO_FLOOR; +using tflite::BuiltinOperator_STABLEHLO_GATHER; +using tflite::BuiltinOperator_STABLEHLO_IOTA; +using tflite::BuiltinOperator_STABLEHLO_LOG; +using tflite::BuiltinOperator_STABLEHLO_LOGISTIC; +using tflite::BuiltinOperator_STABLEHLO_MAXIMUM; +using tflite::BuiltinOperator_STABLEHLO_MINIMUM; +using tflite::BuiltinOperator_STABLEHLO_MULTIPLY; +using tflite::BuiltinOperator_STABLEHLO_NEGATE; +using tflite::BuiltinOperator_STABLEHLO_OR; +using tflite::BuiltinOperator_STABLEHLO_PAD; +using tflite::BuiltinOperator_STABLEHLO_POWER; +using tflite::BuiltinOperator_STABLEHLO_REDUCE; +using tflite::BuiltinOperator_STABLEHLO_REDUCE_WINDOW; +using tflite::BuiltinOperator_STABLEHLO_REMAINDER; +using tflite::BuiltinOperator_STABLEHLO_RESHAPE; +using tflite::BuiltinOperator_STABLEHLO_RNG_BIT_GENERATOR; +using tflite::BuiltinOperator_STABLEHLO_RSQRT; +using tflite::BuiltinOperator_STABLEHLO_SCATTER; +using tflite::BuiltinOperator_STABLEHLO_SELECT; +using tflite::BuiltinOperator_STABLEHLO_SHIFT_LEFT; +using tflite::BuiltinOperator_STABLEHLO_SLICE; +using tflite::BuiltinOperator_STABLEHLO_SORT; +using tflite::BuiltinOperator_STABLEHLO_SUBTRACT; +using tflite::BuiltinOperator_STABLEHLO_TANH; +using tflite::BuiltinOperator_STABLEHLO_TRANSPOSE; +using tflite::BuiltinOperator_STABLEHLO_WHILE; +using tflite::BuiltinOperator_STRIDED_SLICE; +using tflite::BuiltinOperator_SUB; +using tflite::BuiltinOperator_SUM; +using tflite::BuiltinOperator_SVDF; +using tflite::BuiltinOperator_TANH; +using tflite::BuiltinOperator_TILE; +using tflite::BuiltinOperator_TOPK_V2; +using tflite::BuiltinOperator_TRANSPOSE; +using tflite::BuiltinOperator_TRANSPOSE_CONV; +using tflite::BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM; +using tflite::BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_RNN; +using tflite::BuiltinOperator_UNIQUE; +using tflite::BuiltinOperator_UNPACK; +using tflite::BuiltinOperator_UNSORTED_SEGMENT_MAX; +using tflite::BuiltinOperator_UNSORTED_SEGMENT_MIN; +using tflite::BuiltinOperator_UNSORTED_SEGMENT_PROD; +using tflite::BuiltinOperator_UNSORTED_SEGMENT_SUM; +using tflite::BuiltinOperator_VAR_HANDLE; +using tflite::BuiltinOperator_WHERE; +using tflite::BuiltinOperator_WHILE; +using tflite::BuiltinOperator_ZEROS_LIKE; +using tflite::CallOnceOptions; +using tflite::CombinerType; +using tflite::CombinerType_MEAN; +using tflite::CombinerType_SQRTN; +using tflite::CombinerType_SUM; +using tflite::ConcatenationOptions; +using tflite::Conv2DOptions; +using tflite::DepthwiseConv2DOptions; +using tflite::FullyConnectedOptions; +using tflite::FullyConnectedOptionsWeightsFormat_DEFAULT; +using tflite::FullyConnectedOptionsWeightsFormat_SHUFFLED4x16INT8; +using tflite::IfOptions; +using tflite::L2NormOptions; +using tflite::LSHProjectionType; +using tflite::LSHProjectionType_DENSE; +using tflite::LSHProjectionType_SPARSE; +using tflite::LSTMKernelType_BASIC; +using tflite::LSTMKernelType_FULL; +using tflite::MirrorPadMode; +using tflite::MirrorPadMode_REFLECT; +using tflite::MirrorPadMode_SYMMETRIC; +using tflite::MirrorPadOptions; +using tflite::MulOptions; +using tflite::Operator; +using tflite::PackOptions; +using tflite::Padding; +using tflite::Padding_SAME; +using tflite::Padding_VALID; +using tflite::Pool2DOptions; +using tflite::ReducerOptions; +using tflite::ReduceWindowFunction_ADD; +using tflite::ReduceWindowFunction_ALL; +using tflite::ReduceWindowFunction_ANY; +using tflite::ReduceWindowFunction_MAXIMUM; +using tflite::ReduceWindowFunction_MINIMUM; +using tflite::ReduceWindowFunction_MUL; +using tflite::ReduceWindowFunction_UNSUPPORTED; +using tflite::ReshapeOptions; +using tflite::ResizeBilinearOptions; +using tflite::ResizeNearestNeighborOptions; +using tflite::RngAlgorithm; +using tflite::RngAlgorithm_DEFAULT; +using tflite::RngAlgorithm_PHILOX; +using tflite::RngAlgorithm_THREEFRY; +using tflite::ShapeOptions; +using tflite::SoftmaxOptions; +using tflite::SplitOptions; +using tflite::SplitVOptions; +using tflite::SqueezeOptions; +using tflite::StableHLOCompositeOptions; +using tflite::StablehloGatherOptions; +using tflite::StablehloPadOptions; +using tflite::StablehloReduceWindowOptions; +using tflite::StablehloRngBitGeneratorOptions; +using tflite::StablehloScatterOptions; +using tflite::StridedSliceOptions; +using tflite::SubOptions; +using tflite::SVDFOptions; +using tflite::TensorType; +using tflite::TensorType_BFLOAT16; +using tflite::TensorType_BOOL; +using tflite::TensorType_COMPLEX128; +using tflite::TensorType_COMPLEX64; +using tflite::TensorType_FLOAT16; +using tflite::TensorType_FLOAT32; +using tflite::TensorType_FLOAT64; +using tflite::TensorType_FLOAT8_E4M3FN; +using tflite::TensorType_FLOAT8_E5M2; +using tflite::TensorType_INT16; +using tflite::TensorType_INT2; +using tflite::TensorType_INT32; +using tflite::TensorType_INT4; +using tflite::TensorType_INT64; +using tflite::TensorType_INT8; +using tflite::TensorType_RESOURCE; +using tflite::TensorType_STRING; +using tflite::TensorType_UINT16; +using tflite::TensorType_UINT32; +using tflite::TensorType_UINT4; +using tflite::TensorType_UINT64; +using tflite::TensorType_UINT8; +using tflite::TensorType_VARIANT; +using tflite::TransposeConvOptions; +using tflite::UnpackOptions; +using tflite::VarHandleOptions; +using tflite::WhileOptions; + +// LINT.IfChange +namespace { + +// Utility class for safely allocating POD data. This is useful for avoiding +// leaks in cases where op params are allocated but fail to propagate to the +// parsed op data (e.g., when model parameters are invalid). +class SafeBuiltinDataAllocator { + public: + class BuiltinDataDeleter { + public: + explicit BuiltinDataDeleter(BuiltinDataAllocator* allocator) + : allocator_(allocator) {} + + void operator()(void* data) { allocator_->Deallocate(data); } + + private: + BuiltinDataAllocator* allocator_; + }; + + template + using BuiltinDataPtr = std::unique_ptr; + + explicit SafeBuiltinDataAllocator(BuiltinDataAllocator* allocator) + : allocator_(allocator) {} + + template + BuiltinDataPtr Allocate() { + return BuiltinDataPtr(allocator_->AllocatePOD(), + BuiltinDataDeleter(allocator_)); + } + + private: + BuiltinDataAllocator* allocator_; +}; + +// All the Parse functions take some pointers as params and this function has +// the common DCHECKs to catch if any of those are nullptr. +void CheckParsePointerParams(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data) { + TFLITE_DCHECK(op != nullptr); + TFLITE_DCHECK(allocator != nullptr); + TFLITE_DCHECK(builtin_data != nullptr); +} + +// Copies the contents from the flatbuffer int vector `flatbuffer` into the +// int array `buffer`. `flat_vector` and `buffer` represent the same +// configuration operation for a given operation. +template +static absl::Status FlatBufferIntVectorToArray( + int max_size_of_buffer, const flatbuffers::Vector* flat_vector, + DataType* buffer, const char* op_name) { + if (!flat_vector) { + auto error_message = absl::StrFormat( + "Input array not provided for operation '%s'.\n", op_name); + ABSL_LOG(ERROR) << error_message; + return absl::InvalidArgumentError(error_message); + } else { + size_t num_dimensions = flat_vector->size(); + if (num_dimensions > max_size_of_buffer / sizeof(DataType)) { + auto error_message = absl::StrFormat( + "Found too many dimensions in the input array of operation '%s'.\n", + op_name); + ABSL_LOG(ERROR) << error_message; + return absl::InvalidArgumentError(error_message); + } else { + for (size_t i = 0; i < num_dimensions; ++i) { + buffer[i] = flat_vector->Get(i); + } + } + } + return OkStatus(); +} + +// Converts the flatbuffer activation to what is used at runtime. +TfLiteFusedActivation ConvertActivation(ActivationFunctionType activation) { + switch (activation) { + case ActivationFunctionType_NONE: + return kTfLiteActNone; + case ActivationFunctionType_RELU: + return kTfLiteActRelu; + case ActivationFunctionType_RELU_N1_TO_1: + return kTfLiteActReluN1To1; + case ActivationFunctionType_RELU6: + return kTfLiteActRelu6; + case ActivationFunctionType_TANH: + return kTfLiteActTanh; + case ActivationFunctionType_SIGN_BIT: + return kTfLiteActSignBit; + } + return kTfLiteActNone; +} + +TfLitePadding ConvertPadding(Padding padding) { + switch (padding) { + case Padding_SAME: + return kTfLitePaddingSame; + case Padding_VALID: + return kTfLitePaddingValid; + } + return kTfLitePaddingUnknown; +} + +// Converts the flatbuffer mirror padding enum to what is used at runtime. +TfLiteMirrorPaddingMode ConvertMirrorPadding(MirrorPadMode padding) { + switch (padding) { + case MirrorPadMode_REFLECT: + return kTfLiteMirrorPaddingReflect; + case MirrorPadMode_SYMMETRIC: + return kTfLiteMirrorPaddingSymmetric; + } + return kTfLiteMirrorPaddingUnknown; +} + +TfLiteRngAlgorithm ConvertRngAlgorithm(RngAlgorithm algorithm) { + switch (algorithm) { + case RngAlgorithm_THREEFRY: + return kTfLiteRngAlgorithmThreefry; + case RngAlgorithm_PHILOX: + return kTfLiteRngAlgorithmPhilox; + case RngAlgorithm_DEFAULT: + return kTfLiteRngAlgorithmDefault; + } + return kTfLiteRngAlgorithmUnknown; +} + +#ifndef TF_LITE_STATIC_MEMORY +absl::Status ParseOpDataTfLite(const Operator* op, BuiltinOperator op_type, + BuiltinDataAllocator* allocator, + void** builtin_data) { + auto parseLSHProjectionType = [](LSHProjectionType type) { + switch (type) { + case LSHProjectionType_SPARSE: + return kTfLiteLshProjectionSparse; + case LSHProjectionType_DENSE: + return kTfLiteLshProjectionDense; + default: + return kTfLiteLshProjectionUnknown; + } + }; + auto parseCombinerType = [](CombinerType type) { + switch (type) { + case CombinerType_MEAN: + return kTfLiteCombinerTypeMean; + case CombinerType_SQRTN: + return kTfLiteCombinerTypeSqrtn; + case CombinerType_SUM: + default: + return kTfLiteCombinerTypeSum; + } + }; + + SafeBuiltinDataAllocator safe_allocator(allocator); + *builtin_data = nullptr; + switch (op_type) { + case BuiltinOperator_ABS: { + return ParseAbs(op, allocator, builtin_data); + } + + case BuiltinOperator_ADD: { + return ParseAdd(op, allocator, builtin_data); + } + + case BuiltinOperator_ADD_N: { + return ParseAddN(op, allocator, builtin_data); + } + + case BuiltinOperator_ARG_MAX: { + return ParseArgMax(op, allocator, builtin_data); + } + + case BuiltinOperator_ARG_MIN: { + return ParseArgMin(op, allocator, builtin_data); + } + + case BuiltinOperator_ASSIGN_VARIABLE: { + return ParseAssignVariable(op, allocator, builtin_data); + } + + case BuiltinOperator_AVERAGE_POOL_2D: { + return ParsePool(op, allocator, builtin_data); + } + + case BuiltinOperator_BATCH_MATMUL: { + return ParseBatchMatMul(op, allocator, builtin_data); + } + + case BuiltinOperator_BATCH_TO_SPACE_ND: { + return ParseBatchToSpaceNd(op, allocator, builtin_data); + } + + case BuiltinOperator_BROADCAST_ARGS: { + return ParseBroadcastArgs(op, allocator, builtin_data); + } + + case BuiltinOperator_BROADCAST_TO: { + return ParseBroadcastTo(op, allocator, builtin_data); + } + + case BuiltinOperator_CALL_ONCE: { + return ParseCallOnce(op, allocator, builtin_data); + } + + case BuiltinOperator_CEIL: { + return ParseCeil(op, allocator, builtin_data); + } + + case BuiltinOperator_CONCATENATION: { + return ParseConcatenation(op, allocator, builtin_data); + } + + case BuiltinOperator_CONV_2D: { + return ParseConv2D(op, allocator, builtin_data); + } + + case BuiltinOperator_CUMSUM: { + return ParseCumsum(op, allocator, builtin_data); + } + + case BuiltinOperator_DEPTH_TO_SPACE: { + return ParseDepthToSpace(op, allocator, builtin_data); + } + + case BuiltinOperator_DEPTHWISE_CONV_2D: { + return ParseDepthwiseConv2D(op, allocator, builtin_data); + } + + case BuiltinOperator_DEQUANTIZE: { + return ParseDequantize(op, allocator, builtin_data); + } + + case BuiltinOperator_DIV: { + return ParseDiv(op, allocator, builtin_data); + } + + case BuiltinOperator_ELU: { + return ParseElu(op, allocator, builtin_data); + } + + case BuiltinOperator_EMBEDDING_LOOKUP: { + return ParseEmbeddingLookup(op, allocator, builtin_data); + } + + case BuiltinOperator_EXP: { + return ParseExp(op, allocator, builtin_data); + } + + case BuiltinOperator_EXPAND_DIMS: { + return ParseExpandDims(op, allocator, builtin_data); + } + + case BuiltinOperator_FILL: { + return ParseFill(op, allocator, builtin_data); + } + + case BuiltinOperator_FLOOR: { + return ParseFloor(op, allocator, builtin_data); + } + + case BuiltinOperator_FLOOR_DIV: { + return ParseFloorDiv(op, allocator, builtin_data); + } + + case BuiltinOperator_FLOOR_MOD: { + return ParseFloorMod(op, allocator, builtin_data); + } + + case BuiltinOperator_FULLY_CONNECTED: { + return ParseFullyConnected(op, allocator, builtin_data); + } + + case BuiltinOperator_GATHER_ND: { + return ParseGatherNd(op, allocator, builtin_data); + } + + case BuiltinOperator_GREATER: { + return ParseGreater(op, allocator, builtin_data); + } + + case BuiltinOperator_GREATER_EQUAL: { + return ParseGreaterEqual(op, allocator, builtin_data); + } + + case BuiltinOperator_HARD_SWISH: { + return ParseHardSwish(op, allocator, builtin_data); + } + + case BuiltinOperator_L2_NORMALIZATION: { + return ParseL2Normalization(op, allocator, builtin_data); + } + + case BuiltinOperator_L2_POOL_2D: { + return ParsePool(op, allocator, builtin_data); + } + + case BuiltinOperator_LEAKY_RELU: { + return ParseLeakyRelu(op, allocator, builtin_data); + } + + case BuiltinOperator_LESS: { + return ParseLess(op, allocator, builtin_data); + } + + case BuiltinOperator_LESS_EQUAL: { + return ParseLessEqual(op, allocator, builtin_data); + } + + case BuiltinOperator_LOG: { + return ParseLog(op, allocator, builtin_data); + } + + case BuiltinOperator_LOGICAL_AND: { + return ParseLogicalAnd(op, allocator, builtin_data); + } + + case BuiltinOperator_LOGICAL_NOT: { + return ParseLogicalNot(op, allocator, builtin_data); + } + + case BuiltinOperator_LOGICAL_OR: { + return ParseLogicalOr(op, allocator, builtin_data); + } + + case BuiltinOperator_LOGISTIC: { + return ParseLogistic(op, allocator, builtin_data); + } + + case BuiltinOperator_LOG_SOFTMAX: { + return ParseLogSoftmax(op, allocator, builtin_data); + } + + case BuiltinOperator_LSTM: { + return ParseLSTM(op, allocator, builtin_data); + } + + case BuiltinOperator_MAXIMUM: { + return ParseMaximum(op, allocator, builtin_data); + } + + case BuiltinOperator_MAX_POOL_2D: { + return ParsePool(op, allocator, builtin_data); + } + + case BuiltinOperator_MIRROR_PAD: { + return ParseMirrorPad(op, allocator, builtin_data); + } + + case BuiltinOperator_MEAN: { + return ParseReducer(op, allocator, builtin_data); + } + + case BuiltinOperator_MINIMUM: { + return ParseMinimum(op, allocator, builtin_data); + } + + case BuiltinOperator_MUL: { + return ParseMul(op, allocator, builtin_data); + } + + case BuiltinOperator_NEG: { + return ParseNeg(op, allocator, builtin_data); + } + + case BuiltinOperator_NOT_EQUAL: { + return ParseNotEqual(op, allocator, builtin_data); + } + + case BuiltinOperator_PACK: { + return ParsePack(op, allocator, builtin_data); + } + + case BuiltinOperator_PAD: { + return ParsePad(op, allocator, builtin_data); + } + + case BuiltinOperator_PADV2: { + return ParsePadV2(op, allocator, builtin_data); + } + + case BuiltinOperator_POW: { + return ParsePow(op, allocator, builtin_data); + } + + case BuiltinOperator_PRELU: { + return ParsePrelu(op, allocator, builtin_data); + } + + case BuiltinOperator_QUANTIZE: { + return ParseQuantize(op, allocator, builtin_data); + } + + case BuiltinOperator_READ_VARIABLE: { + return ParseReadVariable(op, allocator, builtin_data); + } + + case BuiltinOperator_REDUCE_ANY: { + return ParseReducer(op, allocator, builtin_data); + } + + case BuiltinOperator_REDUCE_ALL: { + return ParseReducer(op, allocator, builtin_data); + } + + case BuiltinOperator_REDUCE_MAX: { + return ParseReducer(op, allocator, builtin_data); + } + + case BuiltinOperator_REDUCE_MIN: { + return ParseReducer(op, allocator, builtin_data); + } + + case BuiltinOperator_REDUCE_PROD: { + return ParseReducer(op, allocator, builtin_data); + } + + case BuiltinOperator_RELU: { + return ParseRelu(op, allocator, builtin_data); + } + + case BuiltinOperator_RELU6: { + return ParseRelu6(op, allocator, builtin_data); + } + + case BuiltinOperator_RESHAPE: { + return ParseReshape(op, allocator, builtin_data); + } + + case BuiltinOperator_RESIZE_BILINEAR: { + return ParseResizeBilinear(op, allocator, builtin_data); + } + + case BuiltinOperator_RESIZE_NEAREST_NEIGHBOR: { + return ParseResizeNearestNeighbor(op, allocator, builtin_data); + } + + case BuiltinOperator_ROUND: { + return ParseRound(op, allocator, builtin_data); + } + + case BuiltinOperator_RSQRT: { + return ParseRsqrt(op, allocator, builtin_data); + } + + case BuiltinOperator_SELECT_V2: { + return ParseSelectV2(op, allocator, builtin_data); + } + + case BuiltinOperator_SHAPE: { + return ParseShape(op, allocator, builtin_data); + } + + case BuiltinOperator_SIN: { + return ParseSin(op, allocator, builtin_data); + } + + case BuiltinOperator_SOFTMAX: { + return ParseSoftmax(op, allocator, builtin_data); + } + + case BuiltinOperator_SPACE_TO_BATCH_ND: { + return ParseSpaceToBatchNd(op, allocator, builtin_data); + } + + case BuiltinOperator_SPACE_TO_DEPTH: { + return ParseSpaceToDepth(op, allocator, builtin_data); + } + + case BuiltinOperator_SPLIT: { + return ParseSplit(op, allocator, builtin_data); + } + + case BuiltinOperator_SPLIT_V: { + return ParseSplitV(op, allocator, builtin_data); + } + + case BuiltinOperator_SQRT: { + return ParseSqrt(op, allocator, builtin_data); + } + + case BuiltinOperator_SQUARE: { + return ParseSquare(op, allocator, builtin_data); + } + + case BuiltinOperator_SQUARED_DIFFERENCE: { + return ParseSquaredDifference(op, allocator, builtin_data); + } + + case BuiltinOperator_SQUEEZE: { + return ParseSqueeze(op, allocator, builtin_data); + } + + case BuiltinOperator_STRIDED_SLICE: { + return ParseStridedSlice(op, allocator, builtin_data); + } + + case BuiltinOperator_SUB: { + return ParseSub(op, allocator, builtin_data); + } + + case BuiltinOperator_SUM: { + return ParseReducer(op, allocator, builtin_data); + } + + case BuiltinOperator_SVDF: { + return ParseSvdf(op, allocator, builtin_data); + } + + case BuiltinOperator_TANH: { + return ParseTanh(op, allocator, builtin_data); + } + + case BuiltinOperator_TRANSPOSE_CONV: { + return ParseTransposeConv(op, allocator, builtin_data); + } + + case BuiltinOperator_UNPACK: { + return ParseUnpack(op, allocator, builtin_data); + } + + case BuiltinOperator_VAR_HANDLE: { + return ParseVarHandle(op, allocator, builtin_data); + } + + case BuiltinOperator_ZEROS_LIKE: { + return ParseZerosLike(op, allocator, builtin_data); + } + + case BuiltinOperator_BITWISE_XOR: { + return ParseBitwiseXor(op, allocator, builtin_data); + } + + case BuiltinOperator_RIGHT_SHIFT: { + return ParseRightShift(op, allocator, builtin_data); + } + + case BuiltinOperator_CAST: { + return ParseCast(op, allocator, builtin_data); + } + case BuiltinOperator_LSH_PROJECTION: { + auto params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + if (const auto* lshParams = + op->builtin_options_as_LSHProjectionOptions()) { + params->type = parseLSHProjectionType(lshParams->type()); + } + *builtin_data = params.release(); + return OkStatus(); + } + case BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_RNN: { + auto params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + if (const auto* sequence_rnn_params = + op->builtin_options_as_SequenceRNNOptions()) { + params->activation = + ConvertActivation(sequence_rnn_params->fused_activation_function()); + params->time_major = sequence_rnn_params->time_major(); + params->asymmetric_quantize_inputs = + sequence_rnn_params->asymmetric_quantize_inputs(); + } + *builtin_data = params.release(); + return OkStatus(); + } + case BuiltinOperator_BIDIRECTIONAL_SEQUENCE_RNN: { + auto params = + safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + if (const auto* bidi_sequence_rnn_params = + op->builtin_options_as_BidirectionalSequenceRNNOptions()) { + params->activation = ConvertActivation( + bidi_sequence_rnn_params->fused_activation_function()); + params->time_major = bidi_sequence_rnn_params->time_major(); + params->merge_outputs = bidi_sequence_rnn_params->merge_outputs(); + params->asymmetric_quantize_inputs = + bidi_sequence_rnn_params->asymmetric_quantize_inputs(); + } + *builtin_data = params.release(); + return OkStatus(); + } + case BuiltinOperator_RNN: { + auto params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + if (const auto* rnn_params = op->builtin_options_as_RNNOptions()) { + params->activation = + ConvertActivation(rnn_params->fused_activation_function()); + params->asymmetric_quantize_inputs = + rnn_params->asymmetric_quantize_inputs(); + } + *builtin_data = params.release(); + return OkStatus(); + } + case BuiltinOperator_EMBEDDING_LOOKUP_SPARSE: { + auto params = + safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + if (const auto* embedding_params = + op->builtin_options_as_EmbeddingLookupSparseOptions()) { + params->combiner = parseCombinerType(embedding_params->combiner()); + } + *builtin_data = params.release(); + return OkStatus(); + } + + case BuiltinOperator_HASHTABLE_LOOKUP: + // no-op. + return OkStatus(); + + case BuiltinOperator_LOCAL_RESPONSE_NORMALIZATION: { + auto params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + if (const auto* schema_params = + op->builtin_options_as_LocalResponseNormalizationOptions()) { + params->radius = schema_params->radius(); + params->bias = schema_params->bias(); + params->alpha = schema_params->alpha(); + params->beta = schema_params->beta(); + } + *builtin_data = params.release(); + return OkStatus(); + } + case BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM: { + return ParseUnidirectionalSequenceLSTM(op, allocator, builtin_data); + } + case BuiltinOperator_BIDIRECTIONAL_SEQUENCE_LSTM: { + auto params = + safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + if (const auto* bidi_lstm_params = + op->builtin_options_as_BidirectionalSequenceLSTMOptions()) { + params->activation = + ConvertActivation(bidi_lstm_params->fused_activation_function()); + params->cell_clip = bidi_lstm_params->cell_clip(); + params->proj_clip = bidi_lstm_params->proj_clip(); + params->merge_outputs = bidi_lstm_params->merge_outputs(); + params->time_major = bidi_lstm_params->time_major(); + params->asymmetric_quantize_inputs = + bidi_lstm_params->asymmetric_quantize_inputs(); + } + *builtin_data = params.release(); + return OkStatus(); + } + case BuiltinOperator_SKIP_GRAM: { + auto params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + if (const auto* skip_gram_params = + op->builtin_options_as_SkipGramOptions()) { + params->ngram_size = skip_gram_params->ngram_size(); + params->max_skip_size = skip_gram_params->max_skip_size(); + params->include_all_ngrams = skip_gram_params->include_all_ngrams(); + } + *builtin_data = params.release(); + return OkStatus(); + } + + case BuiltinOperator_GATHER: { + return ParseGather(op, allocator, builtin_data); + } + case BuiltinOperator_SPARSE_TO_DENSE: { + auto params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + if (const auto* sparse_to_dense_params = + op->builtin_options_as_SparseToDenseOptions()) { + params->validate_indices = sparse_to_dense_params->validate_indices(); + } + *builtin_data = params.release(); + return OkStatus(); + } + case BuiltinOperator_DELEGATE: { + auto error_msg = "DELEGATE op shouldn't exist in model."; + ABSL_LOG(ERROR) << error_msg; + return absl::InvalidArgumentError(error_msg); + } + case BuiltinOperator_FAKE_QUANT: { + auto params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + if (const auto* schema_params = + op->builtin_options_as_FakeQuantOptions()) { + params->min = schema_params->min(); + params->max = schema_params->max(); + params->num_bits = schema_params->num_bits(); + params->narrow_range = schema_params->narrow_range(); + } + *builtin_data = params.release(); + return OkStatus(); + } + case BuiltinOperator_ONE_HOT: { + auto params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + if (const auto* schema_params = op->builtin_options_as_OneHotOptions()) { + params->axis = schema_params->axis(); + } + *builtin_data = params.release(); + return OkStatus(); + } + case BuiltinOperator_UNIQUE: { + auto params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + const auto* unique_params = op->builtin_options_as_UniqueOptions(); + if (unique_params != nullptr) { + params->index_out_type = + unique_params->idx_out_type() == tflite::TensorType_INT64 + ? TfLiteType::kTfLiteInt64 + : TfLiteType::kTfLiteInt32; + } + *builtin_data = params.release(); + return OkStatus(); + } + case BuiltinOperator_REVERSE_SEQUENCE: { + auto params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + if (const auto* reverse_seq_params = + op->builtin_options_as_ReverseSequenceOptions()) { + params->seq_dim = reverse_seq_params->seq_dim(); + params->batch_dim = reverse_seq_params->batch_dim(); + } + *builtin_data = params.release(); + return OkStatus(); + } + case BuiltinOperator_IF: { + auto params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + if (const auto* if_params = op->builtin_options_as_IfOptions()) { + params->then_subgraph_index = if_params->then_subgraph_index(); + params->else_subgraph_index = if_params->else_subgraph_index(); + } + *builtin_data = params.release(); + return OkStatus(); + } + case BuiltinOperator_WHILE: { + auto params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + if (const auto* while_params = op->builtin_options_as_WhileOptions()) { + params->cond_subgraph_index = while_params->cond_subgraph_index(); + params->body_subgraph_index = while_params->body_subgraph_index(); + } + *builtin_data = params.release(); + return OkStatus(); + } + case BuiltinOperator_CONV_3D: + case BuiltinOperator_CONV_3D_TRANSPOSE: { + auto params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + if (const auto* conv3d_params = op->builtin_options_as_Conv3DOptions()) { + params->padding = ConvertPadding(conv3d_params->padding()); + params->activation = + ConvertActivation(conv3d_params->fused_activation_function()); + params->stride_depth = conv3d_params->stride_d(); + params->stride_height = conv3d_params->stride_h(); + params->stride_width = conv3d_params->stride_w(); + params->dilation_depth_factor = conv3d_params->dilation_d_factor(); + params->dilation_height_factor = conv3d_params->dilation_h_factor(); + params->dilation_width_factor = conv3d_params->dilation_w_factor(); + } + *builtin_data = params.release(); + return OkStatus(); + } + case BuiltinOperator_HASHTABLE: { + auto params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + if (const auto* hashtable_params = + op->builtin_options_as_HashtableOptions()) { + params->table_id = hashtable_params->table_id(); + TFL_FILE_ENSURE_STATUS(ConvertTensorType(hashtable_params->key_dtype(), + ¶ms->key_dtype)); + TFL_FILE_ENSURE_STATUS(ConvertTensorType( + hashtable_params->value_dtype(), ¶ms->value_dtype)); + } + *builtin_data = params.release(); + return OkStatus(); + } + case BuiltinOperator_MULTINOMIAL: { + auto params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + if (const auto* multinomial_params = + op->builtin_options_as_RandomOptions()) { + params->seed = multinomial_params->seed(); + params->seed2 = multinomial_params->seed2(); + } + *builtin_data = params.release(); + return OkStatus(); + } + case BuiltinOperator_RANDOM_STANDARD_NORMAL: { + auto params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + if (const auto* random_std_normal_params = + op->builtin_options_as_RandomOptions()) { + params->seed = random_std_normal_params->seed(); + params->seed2 = random_std_normal_params->seed2(); + } + *builtin_data = params.release(); + return OkStatus(); + } + case BuiltinOperator_BUCKETIZE: { + auto params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + if (const auto* bucketize_params = + op->builtin_options_as_BucketizeOptions()) { + const flatbuffers::Vector* boundaries = + bucketize_params->boundaries(); + if (boundaries == nullptr) { + auto error_message = + "boundaries array not provided for operation 'bucketize'.\n"; + ABSL_LOG(ERROR) << error_message; + return absl::InvalidArgumentError(error_message); + } + params->num_boundaries = boundaries->size(); + if (boundaries->data() == nullptr) { + auto error_message = + "boundaries.data() returned nullptr for " + "operation 'bucketize'.\n"; + ABSL_LOG(ERROR) << error_message; + return absl::InvalidArgumentError(error_message); + } + params->boundaries = boundaries->data(); + } + *builtin_data = params.release(); + return OkStatus(); + } + case BuiltinOperator_RANDOM_UNIFORM: { + auto params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + if (const auto* random_uniform_params = + op->builtin_options_as_RandomOptions()) { + params->seed = random_uniform_params->seed(); + params->seed2 = random_uniform_params->seed2(); + } + *builtin_data = params.release(); + return OkStatus(); + } + case BuiltinOperator_GELU: { + auto params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + if (const auto* gelu_params = op->builtin_options_as_GeluOptions()) { + params->approximate = gelu_params->approximate(); + } + *builtin_data = params.release(); + return OkStatus(); + } + case BuiltinOperator_STABLEHLO_SCATTER: { + return ParseStablehloScatter(op, allocator, builtin_data); + } + case BuiltinOperator_STABLEHLO_RNG_BIT_GENERATOR: { + return ParseStablehloRngBitGenerator(op, allocator, builtin_data); + } + case BuiltinOperator_STABLEHLO_GATHER: { + return ParseStablehloGather(op, allocator, builtin_data); + } + case BuiltinOperator_STABLEHLO_REDUCE_WINDOW: { + return ParseStablehloReduceWindow(op, allocator, builtin_data); + } + case BuiltinOperator_REDUCE_WINDOW: { + auto params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + if (const auto* reduce_params = + op->builtin_options_2_as_ReduceWindowOptions()) { + switch (reduce_params->reduce_function()) { + case ReduceWindowFunction_ADD: + params->reduce_function = TfLiteReduceWindowFunctionAdd; + break; + case ReduceWindowFunction_MUL: + params->reduce_function = TfLiteReduceWindowFunctionMul; + break; + case ReduceWindowFunction_MINIMUM: + params->reduce_function = TfLiteReduceWindowFunctionMin; + break; + case ReduceWindowFunction_MAXIMUM: + params->reduce_function = TfLiteReduceWindowFunctionMax; + break; + case ReduceWindowFunction_ALL: + params->reduce_function = TfLiteReduceWindowFunctionAll; + break; + case ReduceWindowFunction_ANY: + params->reduce_function = TfLiteReduceWindowFunctionAny; + break; + case ReduceWindowFunction_UNSUPPORTED: + default: + return absl::InvalidArgumentError("Unsupported reduce function"); + } + } + *builtin_data = params.release(); + return OkStatus(); + } + case BuiltinOperator_STABLEHLO_PAD: { + return ParseStablehloPad(op, allocator, builtin_data); + } + case BuiltinOperator_STABLEHLO_COMPOSITE: { + return ParseStablehloComposite(op, allocator, builtin_data); + } + case BuiltinOperator_STABLEHLO_SHIFT_LEFT: { + return ParseStablehloShiftLeft(op, allocator, builtin_data); + } + case BuiltinOperator_STABLEHLO_CASE: { + return ParseStablehloCase(op, allocator, builtin_data); + } + // TODO: skip param parsing for now since ops below don't have kernels + case BuiltinOperator_STABLEHLO_SLICE: + case BuiltinOperator_STABLEHLO_BROADCAST_IN_DIM: + case BuiltinOperator_STABLEHLO_CONVOLUTION: + case BuiltinOperator_STABLEHLO_LOGISTIC: + case BuiltinOperator_STABLEHLO_ADD: + case BuiltinOperator_STABLEHLO_DIVIDE: + case BuiltinOperator_STABLEHLO_MULTIPLY: + case BuiltinOperator_STABLEHLO_MAXIMUM: + case BuiltinOperator_STABLEHLO_RESHAPE: + case BuiltinOperator_STABLEHLO_CLAMP: + case BuiltinOperator_STABLEHLO_CONCATENATE: + case BuiltinOperator_STABLEHLO_CUSTOM_CALL: + case BuiltinOperator_STABLEHLO_REDUCE: + case BuiltinOperator_STABLEHLO_ABS: + case BuiltinOperator_STABLEHLO_AND: + case BuiltinOperator_STABLEHLO_COSINE: + case BuiltinOperator_STABLEHLO_EXPONENTIAL: + case BuiltinOperator_STABLEHLO_FLOOR: + case BuiltinOperator_STABLEHLO_LOG: + case BuiltinOperator_STABLEHLO_MINIMUM: + case BuiltinOperator_STABLEHLO_NEGATE: + case BuiltinOperator_STABLEHLO_OR: + case BuiltinOperator_STABLEHLO_POWER: + case BuiltinOperator_STABLEHLO_REMAINDER: + case BuiltinOperator_STABLEHLO_RSQRT: + case BuiltinOperator_STABLEHLO_SELECT: + case BuiltinOperator_STABLEHLO_SUBTRACT: + case BuiltinOperator_STABLEHLO_TANH: + case BuiltinOperator_STABLEHLO_DYNAMIC_SLICE: + case BuiltinOperator_STABLEHLO_DYNAMIC_UPDATE_SLICE: + case BuiltinOperator_STABLEHLO_IOTA: + case BuiltinOperator_STABLEHLO_COMPARE: + case BuiltinOperator_STABLEHLO_CONVERT: + case BuiltinOperator_STABLEHLO_DOT_GENERAL: + case BuiltinOperator_STABLEHLO_SORT: + case BuiltinOperator_STABLEHLO_WHILE: + case BuiltinOperator_STABLEHLO_TRANSPOSE: + case BuiltinOperator_STABLEHLO_CBRT: + + // Below are the ops with no builtin_data structure. + // TODO(aselle): Implement call in BuiltinOptions, but nullptrs are + // ok for now, since there is no call implementation either. + case BuiltinOperator_CALL: + case BuiltinOperator_COMPLEX_ABS: + case BuiltinOperator_CONCAT_EMBEDDINGS: + case BuiltinOperator_COS: + case BuiltinOperator_CUSTOM: + case BuiltinOperator_DENSIFY: + case BuiltinOperator_DYNAMIC_UPDATE_SLICE: + case BuiltinOperator_EQUAL: + case BuiltinOperator_HASHTABLE_FIND: + case BuiltinOperator_HASHTABLE_IMPORT: + case BuiltinOperator_HASHTABLE_SIZE: + case BuiltinOperator_IMAG: + case BuiltinOperator_MATRIX_DIAG: + case BuiltinOperator_MATRIX_SET_DIAG: + case BuiltinOperator_NON_MAX_SUPPRESSION_V4: + case BuiltinOperator_NON_MAX_SUPPRESSION_V5: + case BuiltinOperator_RELU_N1_TO_1: + case BuiltinOperator_RELU_0_TO_1: + case BuiltinOperator_SCATTER_ND: + case BuiltinOperator_SELECT: + case BuiltinOperator_SLICE: + case BuiltinOperator_TILE: + case BuiltinOperator_TOPK_V2: + case BuiltinOperator_TRANSPOSE: + case BuiltinOperator_RANGE: + case BuiltinOperator_RANK: + case BuiltinOperator_REAL: + case BuiltinOperator_RFFT2D: + case BuiltinOperator_SEGMENT_SUM: + case BuiltinOperator_REVERSE_V2: + case BuiltinOperator_UNSORTED_SEGMENT_MAX: + case BuiltinOperator_UNSORTED_SEGMENT_MIN: + case BuiltinOperator_UNSORTED_SEGMENT_PROD: + case BuiltinOperator_UNSORTED_SEGMENT_SUM: + case BuiltinOperator_ATAN2: + case BuiltinOperator_SIGN: + case BuiltinOperator_BITCAST: + case BuiltinOperator_WHERE: + case BuiltinOperator_DILATE: + return OkStatus(); + case BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES: + return absl::UnimplementedError("Unsupported op"); + } + return absl::UnimplementedError("Unsupported op"); +} // NOLINT[readability/fn_size] +#endif // !defined(TF_LITE_STATIC_MEMORY) +} // namespace + +absl::Status ConvertTensorType(TensorType tensor_type, TfLiteType* type) { + switch (tensor_type) { + case TensorType_FLOAT16: + *type = kTfLiteFloat16; + return OkStatus(); + case TensorType_BFLOAT16: + *type = kTfLiteBFloat16; + return OkStatus(); + case TensorType_FLOAT32: + *type = kTfLiteFloat32; + return OkStatus(); + case TensorType_FLOAT64: + *type = kTfLiteFloat64; + return OkStatus(); + case TensorType_INT16: + *type = kTfLiteInt16; + return OkStatus(); + case TensorType_UINT16: + *type = kTfLiteUInt16; + return OkStatus(); + case TensorType_INT32: + *type = kTfLiteInt32; + return OkStatus(); + case TensorType_UINT32: + *type = kTfLiteUInt32; + return OkStatus(); + case TensorType_UINT8: + *type = kTfLiteUInt8; + return OkStatus(); + case TensorType_INT8: + *type = kTfLiteInt8; + return OkStatus(); + case TensorType_INT64: + *type = kTfLiteInt64; + return OkStatus(); + case TensorType_UINT64: + *type = kTfLiteUInt64; + return OkStatus(); + case TensorType_STRING: + *type = kTfLiteString; + return OkStatus(); + case TensorType_BOOL: + *type = kTfLiteBool; + return OkStatus(); + case TensorType_COMPLEX64: + *type = kTfLiteComplex64; + return OkStatus(); + case TensorType_COMPLEX128: + *type = kTfLiteComplex128; + return OkStatus(); + case TensorType_RESOURCE: + *type = kTfLiteResource; + return OkStatus(); + case TensorType_VARIANT: + *type = kTfLiteVariant; + return OkStatus(); + case TensorType_INT4: + *type = kTfLiteInt4; + return OkStatus(); + case TensorType_INT2: + *type = kTfLiteInt2; + return OkStatus(); + case TensorType_UINT4: + *type = kTfLiteUInt4; + return OkStatus(); + case TensorType_FLOAT8_E4M3FN: + *type = kTfLiteFloat8E4M3FN; + return OkStatus(); + case TensorType_FLOAT8_E5M2: + *type = kTfLiteFloat8E5M2; + return OkStatus(); + default: + *type = kTfLiteNoType; + auto error_message = + absl::StrFormat("Unsupported data type %d in tensor", tensor_type); + ABSL_LOG(ERROR) << error_message; + return absl::InvalidArgumentError(error_message); + } +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseAbs(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +absl::Status ParseAdd(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const AddOptions* schema_params = op->builtin_options_as_AddOptions(); + + if (schema_params != nullptr) { + params->activation = + ConvertActivation(schema_params->fused_activation_function()); + params->pot_scale_int16 = schema_params->pot_scale_int16(); + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + + *builtin_data = params.release(); + return OkStatus(); +} + +absl::Status ParseAddN(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data) { + return OkStatus(); +} + +absl::Status ParseArgMax(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const ArgMaxOptions* schema_params = op->builtin_options_as_ArgMaxOptions(); + + if (schema_params != nullptr) { + TFL_FILE_ENSURE_STATUS( + ConvertTensorType(schema_params->output_type(), ¶ms->output_type)); + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + + *builtin_data = params.release(); + return OkStatus(); +} + +absl::Status ParseArgMin(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const ArgMinOptions* schema_params = op->builtin_options_as_ArgMinOptions(); + + if (schema_params != nullptr) { + TFL_FILE_ENSURE_STATUS( + ConvertTensorType(schema_params->output_type(), ¶ms->output_type)); + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + + *builtin_data = params.release(); + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseAssignVariable(const Operator*, BuiltinDataAllocator*, + void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseBatchMatMul(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + auto params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + if (const auto* bmm_params = op->builtin_options_as_BatchMatMulOptions()) { + params->adj_x = bmm_params->adj_x(); + params->adj_y = bmm_params->adj_y(); + params->asymmetric_quantize_inputs = + bmm_params->asymmetric_quantize_inputs(); + } + *builtin_data = params.release(); + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseBatchToSpaceNd(const Operator*, BuiltinDataAllocator*, + void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseBroadcastArgs(const Operator*, BuiltinDataAllocator*, + void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseBroadcastTo(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +absl::Status ParseCallOnce(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const CallOnceOptions* schema_params = + op->builtin_options_as_CallOnceOptions(); + + if (schema_params != nullptr) { + params->init_subgraph_index = schema_params->init_subgraph_index(); + + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + + *builtin_data = params.release(); + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseCast(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + auto params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + if (const auto* schema_params = op->builtin_options_as_CastOptions()) { + TFL_FILE_ENSURE_STATUS(ConvertTensorType(schema_params->in_data_type(), + ¶ms->in_data_type)); + TFL_FILE_ENSURE_STATUS(ConvertTensorType(schema_params->out_data_type(), + ¶ms->out_data_type)); + } + *builtin_data = params.release(); + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseCeil(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +absl::Status ParseConcatenation(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const ConcatenationOptions* schema_params = + op->builtin_options_as_ConcatenationOptions(); + + if (schema_params != nullptr) { + params->activation = + ConvertActivation(schema_params->fused_activation_function()); + params->axis = schema_params->axis(); + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + + *builtin_data = params.release(); + return OkStatus(); +} + +absl::Status ParseConv2D(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const Conv2DOptions* schema_params = op->builtin_options_as_Conv2DOptions(); + + if (schema_params != nullptr) { + params->padding = ConvertPadding(schema_params->padding()); + params->stride_width = schema_params->stride_w(); + params->stride_height = schema_params->stride_h(); + params->activation = + ConvertActivation(schema_params->fused_activation_function()); + + params->dilation_width_factor = schema_params->dilation_w_factor(); + params->dilation_height_factor = schema_params->dilation_h_factor(); + TFL_FILE_ENSURE_STATUS(ConvertTensorType( + schema_params->quantized_bias_type(), ¶ms->quantized_bias_type)); + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + + *builtin_data = params.release(); + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseCumsum(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + auto params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + if (const auto* cumsum_params = op->builtin_options_as_CumsumOptions()) { + params->exclusive = cumsum_params->exclusive(); + params->reverse = cumsum_params->reverse(); + } + *builtin_data = params.release(); + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseCos(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +absl::Status ParseDepthToSpace(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const auto* schema_params = op->builtin_options_as_DepthToSpaceOptions(); + if (schema_params != nullptr) { + params->block_size = schema_params->block_size(); + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + + *builtin_data = params.release(); + return OkStatus(); +} + +absl::Status ParseDepthwiseConv2D(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const DepthwiseConv2DOptions* schema_params = + op->builtin_options_as_DepthwiseConv2DOptions(); + + if (schema_params != nullptr) { + params->padding = ConvertPadding(schema_params->padding()); + params->stride_width = schema_params->stride_w(); + params->stride_height = schema_params->stride_h(); + params->depth_multiplier = schema_params->depth_multiplier(); + params->activation = + ConvertActivation(schema_params->fused_activation_function()); + + params->dilation_width_factor = schema_params->dilation_w_factor(); + params->dilation_height_factor = schema_params->dilation_h_factor(); + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + + *builtin_data = params.release(); + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseDequantize(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +absl::Status ParseDiv(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + auto params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + if (const auto* schema_params = op->builtin_options_as_DivOptions()) { + params->activation = + ConvertActivation(schema_params->fused_activation_function()); + } + *builtin_data = params.release(); + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseElu(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseEmbeddingLookup(const Operator*, BuiltinDataAllocator*, + void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseEqual(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseExp(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseExpandDims(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseFill(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseFloor(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseFloorDiv(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseFloorMod(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +absl::Status ParseFullyConnected(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const FullyConnectedOptions* schema_params = + op->builtin_options_as_FullyConnectedOptions(); + + if (schema_params != nullptr) { + params->activation = + ConvertActivation(schema_params->fused_activation_function()); + params->keep_num_dims = schema_params->keep_num_dims(); + params->asymmetric_quantize_inputs = + schema_params->asymmetric_quantize_inputs(); + TFL_FILE_ENSURE_STATUS(ConvertTensorType( + schema_params->quantized_bias_type(), ¶ms->quantized_bias_type)); + switch (schema_params->weights_format()) { + case FullyConnectedOptionsWeightsFormat_DEFAULT: + params->weights_format = kTfLiteFullyConnectedWeightsFormatDefault; + break; + case FullyConnectedOptionsWeightsFormat_SHUFFLED4x16INT8: + params->weights_format = + kTfLiteFullyConnectedWeightsFormatShuffled4x16Int8; + break; + default: + auto error_message = "Unhandled fully-connected weights format."; + ABSL_LOG(ERROR) << error_message; + return absl::InvalidArgumentError(error_message); + } + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + + *builtin_data = params.release(); + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseGather(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + auto params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + params->axis = 0; + params->batch_dims = 0; + if (const auto* gather_params = op->builtin_options_as_GatherOptions()) { + params->axis = gather_params->axis(); + params->batch_dims = gather_params->batch_dims(); + } + + *builtin_data = params.release(); + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseGatherNd(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseGreater(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseGreaterEqual(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseHardSwish(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +absl::Status ParseIf(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const IfOptions* schema_params = op->builtin_options_as_IfOptions(); + + if (schema_params != nullptr) { + params->then_subgraph_index = schema_params->then_subgraph_index(); + params->else_subgraph_index = schema_params->else_subgraph_index(); + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + + *builtin_data = params.release(); + return OkStatus(); +} + +absl::Status ParseL2Normalization(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const L2NormOptions* schema_params = op->builtin_options_as_L2NormOptions(); + + if (schema_params != nullptr) { + params->activation = + ConvertActivation(schema_params->fused_activation_function()); + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + + *builtin_data = params.release(); + return OkStatus(); +} + +absl::Status ParseLeakyRelu(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + auto params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + if (const auto* leaky_relu_params = + op->builtin_options_as_LeakyReluOptions()) { + params->alpha = leaky_relu_params->alpha(); + } + *builtin_data = params.release(); + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseLess(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseLessEqual(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseLog(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseLogicalAnd(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseLogicalNot(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseLogicalOr(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseLogistic(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseLogSoftmax(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +absl::Status ParseLSTM(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + auto params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + if (const auto* lstm_params = op->builtin_options_as_LSTMOptions()) { + params->activation = + ConvertActivation(lstm_params->fused_activation_function()); + params->cell_clip = lstm_params->cell_clip(); + params->proj_clip = lstm_params->proj_clip(); + switch (lstm_params->kernel_type()) { + case LSTMKernelType_FULL: + params->kernel_type = kTfLiteLSTMFullKernel; + break; + case LSTMKernelType_BASIC: + params->kernel_type = kTfLiteLSTMBasicKernel; + break; + default: + auto error_message = absl::StrFormat("Unhandled LSTM kernel type: %d", + lstm_params->kernel_type()); + ABSL_LOG(ERROR) << error_message; + return absl::InvalidArgumentError(error_message); + } + params->asymmetric_quantize_inputs = + lstm_params->asymmetric_quantize_inputs(); + } else { + auto error_message = "No valid LSTM builtin options exist"; + ABSL_LOG(ERROR) << error_message; + return absl::InvalidArgumentError(error_message); + } + *builtin_data = params.release(); + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseMaximum(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseMinimum(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +absl::Status ParseMirrorPad(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const MirrorPadOptions* schema_params = + op->builtin_options_as_MirrorPadOptions(); + + if (schema_params != nullptr) { + params->mode = ConvertMirrorPadding(schema_params->mode()); + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + + *builtin_data = params.release(); + return OkStatus(); +} + +absl::Status ParseMul(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const MulOptions* schema_params = op->builtin_options_as_MulOptions(); + + if (schema_params != nullptr) { + params->activation = + ConvertActivation(schema_params->fused_activation_function()); + } else { + // Default activation is none. + } + + *builtin_data = params.release(); + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseNeg(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseNotEqual(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +absl::Status ParsePack(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const PackOptions* schema_params = op->builtin_options_as_PackOptions(); + + if (schema_params != nullptr) { + params->values_count = schema_params->values_count(); + params->axis = schema_params->axis(); + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + + *builtin_data = params.release(); + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParsePad(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParsePadV2(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +absl::Status ParsePool(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const Pool2DOptions* schema_params = op->builtin_options_as_Pool2DOptions(); + + if (schema_params != nullptr) { + params->padding = ConvertPadding(schema_params->padding()); + params->stride_width = schema_params->stride_w(); + params->stride_height = schema_params->stride_h(); + params->filter_width = schema_params->filter_width(); + params->filter_height = schema_params->filter_height(); + params->activation = + ConvertActivation(schema_params->fused_activation_function()); + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + + *builtin_data = params.release(); + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParsePow(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParsePrelu(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseQuantize(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseReadVariable(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +absl::Status ParseReducer(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const ReducerOptions* schema_params = op->builtin_options_as_ReducerOptions(); + + if (schema_params != nullptr) { + params->keep_dims = schema_params->keep_dims(); + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + + *builtin_data = params.release(); + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseRelu(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseRelu6(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +absl::Status ParseReshape(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const ReshapeOptions* schema_params = op->builtin_options_as_ReshapeOptions(); + + if (schema_params != nullptr) { + const flatbuffers::Vector* new_shape = schema_params->new_shape(); + if (new_shape != nullptr) { + TFL_FILE_ENSURE_STATUS(FlatBufferIntVectorToArray( + sizeof(params->shape), new_shape, params->shape, "reshape")); + params->num_dimensions = new_shape->size(); + } else { + // TODO(b/157480169) TODO(b/147203660): We should either return + // kTfLiteError or fill in some reasonable defaults in the params struct. + // We are not doing so until we better undertand the ramifications of + // changing the legacy behavior. + } + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + + *builtin_data = params.release(); + return OkStatus(); +} + +absl::Status ParseResizeBilinear(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const ResizeBilinearOptions* schema_params = + op->builtin_options_as_ResizeBilinearOptions(); + + if (schema_params != nullptr) { + params->align_corners = schema_params->align_corners(); + params->half_pixel_centers = schema_params->half_pixel_centers(); + } else { + params->align_corners = false; + params->half_pixel_centers = false; + } + + *builtin_data = params.release(); + return OkStatus(); +} + +absl::Status ParseResizeNearestNeighbor(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const ResizeNearestNeighborOptions* schema_params = + op->builtin_options_as_ResizeNearestNeighborOptions(); + + if (schema_params != nullptr) { + params->align_corners = schema_params->align_corners(); + params->half_pixel_centers = schema_params->half_pixel_centers(); + } else { + params->align_corners = false; + params->half_pixel_centers = false; + } + + *builtin_data = params.release(); + return OkStatus(); +} + +absl::Status ParseStablehloReduceWindow(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + auto params = safe_allocator.Allocate(); + + const StablehloReduceWindowOptions* schema_params = + op->builtin_options_2_as_StablehloReduceWindowOptions(); + if (schema_params) { + if (!schema_params->window_dimensions() || + schema_params->window_dimensions()->empty()) { + auto error_message = + "'window_dimensions' attribute is not optional for " + "'stablehlo.reduce_window' and cannot be empty."; + ABSL_LOG(ERROR) << error_message; + return absl::InvalidArgumentError(error_message); + } + + const size_t rank = schema_params->window_dimensions()->size(); + + auto LoadAttr = [](int64_t* params_array, size_t params_array_size_bytes, + const flatbuffers::Vector* flatbuffer_vector, + const char* attr_name, const size_t expected_size, + const int64_t fill_value) -> absl::Status { + if (flatbuffer_vector && !flatbuffer_vector->empty()) { + if (expected_size != 0 && flatbuffer_vector->size() != expected_size) { + auto error_message = absl::StrFormat( + "'%s' attribute of 'stablehlo.reduce_window' does not have the " + "expected size (%llu != %llu).", + attr_name, flatbuffer_vector->size(), expected_size); + ABSL_LOG(ERROR) << error_message; + return absl::InvalidArgumentError(error_message); + } + absl::Status status = FlatBufferIntVectorToArray( + params_array_size_bytes, flatbuffer_vector, params_array, + "stablehlo.reduce_window"); + if (!status.ok()) { + auto error_message = absl::StrFormat("%s Check the '%s' attribute.", + status.message(), attr_name); + ABSL_LOG(ERROR) << error_message; + return absl::InvalidArgumentError(error_message); + } + } else { + std::fill_n(params_array, params_array_size_bytes / sizeof(int64_t), + fill_value); + } + return OkStatus(); + }; + + TFL_FILE_ENSURE_STATUS( + LoadAttr(params->window_dimensions, sizeof(params->window_dimensions), + schema_params->window_dimensions(), "window_dimensions", + /*expected_size=*/rank, /*fill_value=*/1)); + TFL_FILE_ENSURE_STATUS( + LoadAttr(params->window_strides, sizeof(params->window_strides), + schema_params->window_strides(), "window_strides", + /*expected_size=*/rank, /*fill_value=*/1)); + TFL_FILE_ENSURE_STATUS( + LoadAttr(params->base_dilations, sizeof(params->base_dilations), + schema_params->base_dilations(), "base_dilations", + /*expected_size=*/rank, /*fill_value=*/1)); + TFL_FILE_ENSURE_STATUS( + LoadAttr(params->window_dilations, sizeof(params->window_dilations), + schema_params->window_dilations(), "window_dilations", + /*expected_size=*/rank, /*fill_value=*/1)); + TFL_FILE_ENSURE_STATUS(LoadAttr(params->padding, sizeof(params->padding), + schema_params->padding(), "padding", + /*expected_size=*/2 * rank, + /*fill_value=*/0)); + + params->body_subgraph_index = schema_params->body_subgraph_index(); + *builtin_data = params.release(); + return OkStatus(); + } + auto error_message = + "Could not get 'stablehlo.reduce_window' operation parameters."; + ABSL_LOG(ERROR) << error_message; + return absl::InvalidArgumentError(error_message); +} + +absl::Status ParseStablehloScatter(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const StablehloScatterOptions* schema_params = + op->builtin_options_2_as_StablehloScatterOptions(); + if (schema_params) { + params->indices_are_sorted = schema_params->indices_are_sorted(); + + if (schema_params->update_window_dims()) { + TFL_FILE_ENSURE_STATUS(FlatBufferIntVectorToArray( + schema_params->update_window_dims()->size() * sizeof(int64_t), + schema_params->update_window_dims(), params->update_window_dims, + "stablehlo_scatter")); + params->num_update_window_dims = + schema_params->update_window_dims()->size(); + } + + if (schema_params->inserted_window_dims()) { + TFL_FILE_ENSURE_STATUS(FlatBufferIntVectorToArray( + schema_params->inserted_window_dims()->size() * sizeof(int64_t), + schema_params->inserted_window_dims(), params->inserted_window_dims, + "stablehlo_scatter")); + params->num_inserted_window_dims = + schema_params->inserted_window_dims()->size(); + } + + if (schema_params->scatter_dims_to_operand_dims()) { + TFL_FILE_ENSURE_STATUS(FlatBufferIntVectorToArray( + schema_params->scatter_dims_to_operand_dims()->size() * + sizeof(int64_t), + schema_params->scatter_dims_to_operand_dims(), + params->scatter_dims_to_operand_dims, "stablehlo_scatter")); + params->num_scatter_dims_to_operand_dims = + schema_params->scatter_dims_to_operand_dims()->size(); + } + + params->index_vector_dim = schema_params->index_vector_dim(); + params->unique_indices = schema_params->unique_indices(); + params->update_computation_subgraph_index = + schema_params->update_computation_subgraph_index(); + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + *builtin_data = params.release(); + return OkStatus(); +} + +absl::Status ParseStablehloRngBitGenerator(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const StablehloRngBitGeneratorOptions* schema_params = + op->builtin_options_2_as_StablehloRngBitGeneratorOptions(); + if (schema_params != nullptr) { + params->algorithm = ConvertRngAlgorithm(schema_params->algorithm()); + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + + *builtin_data = params.release(); + return OkStatus(); +} + +absl::Status ParseStablehloGather(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const StablehloGatherOptions* schema_params = + op->builtin_options_2_as_StablehloGatherOptions(); + + if (schema_params != nullptr) { + TFL_FILE_ENSURE_STATUS(FlatBufferIntVectorToArray( + /*max_size_of_buffer=*/schema_params->offset_dims()->size() * + sizeof(int64_t), + /*flat_vector=*/schema_params->offset_dims(), + /*buffer=*/params->offset_dims, + /*op_name=*/"stablehlo_gather")); + params->num_offset_dims = schema_params->offset_dims()->size(); + + TFL_FILE_ENSURE_STATUS(FlatBufferIntVectorToArray( + schema_params->collapsed_slice_dims()->size() * sizeof(int64_t), + schema_params->collapsed_slice_dims(), params->collapsed_slice_dims, + "stablehlo_gather")); + params->num_collapsed_slice_dims = + schema_params->collapsed_slice_dims()->size(); + + TFL_FILE_ENSURE_STATUS(FlatBufferIntVectorToArray( + schema_params->start_index_map()->size() * sizeof(int64_t), + schema_params->start_index_map(), params->start_index_map, + "stablehlo_gather")); + params->num_start_index_map = schema_params->start_index_map()->size(); + + params->index_vector_dim = schema_params->index_vector_dim(); + + TFL_FILE_ENSURE_STATUS(FlatBufferIntVectorToArray( + schema_params->slice_sizes()->size() * sizeof(int64_t), + schema_params->slice_sizes(), params->slice_sizes, "stablehlo_gather")); + params->num_slice_sizes = schema_params->slice_sizes()->size(); + + params->indices_are_sorted = schema_params->indices_are_sorted(); + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + + *builtin_data = params.release(); + return OkStatus(); +} + +absl::Status ParseStablehloPad(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + auto params = safe_allocator.Allocate(); + const StablehloPadOptions* schema_params = + op->builtin_options_2_as_StablehloPadOptions(); + + if (schema_params) { + auto LoadAttr = + [](int64_t* params_array, const size_t params_array_size_bytes, + const flatbuffers::Vector* const flatbuffer_vector, + const char* const attr_name) -> absl::Status { + absl::Status status = + FlatBufferIntVectorToArray(params_array_size_bytes, flatbuffer_vector, + params_array, "stablehlo.pad"); + if (!status.ok()) { + auto error_message = absl::StrFormat("%s Check the '%s' attribute.", + status.message(), attr_name); + ABSL_LOG(ERROR) << error_message; + return absl::InvalidArgumentError(error_message); + } + return status; + }; + + TFL_FILE_ENSURE_STATUS( + LoadAttr(params->edge_padding_low, sizeof(params->edge_padding_low), + schema_params->edge_padding_low(), "edge_padding_low")); + TFL_FILE_ENSURE_STATUS( + LoadAttr(params->edge_padding_high, sizeof(params->edge_padding_high), + schema_params->edge_padding_high(), "edge_padding_high")); + TFL_FILE_ENSURE_STATUS( + LoadAttr(params->interior_padding, sizeof(params->interior_padding), + schema_params->interior_padding(), "interior_padding")); + if (schema_params->edge_padding_low()->size() != + schema_params->edge_padding_high()->size() || + schema_params->edge_padding_low()->size() != + schema_params->interior_padding()->size()) { + auto error_message = + "'stablehlo.pad' operation parameter array sizes are not consistent."; + ABSL_LOG(ERROR) << error_message; + return absl::InvalidArgumentError(error_message); + } + *builtin_data = params.release(); + return OkStatus(); + } + auto error_message = "Could not get 'stablehlo.pad' operation parameters."; + ABSL_LOG(ERROR) << error_message; + return absl::InvalidArgumentError(error_message); +} + +absl::Status ParseStablehloComposite(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + auto params = safe_allocator.Allocate(); + const StableHLOCompositeOptions* schema_params = + op->builtin_options_2_as_StableHLOCompositeOptions(); + if (schema_params) { + if (schema_params->name() == nullptr) { + auto error_message = + "'stablehlo.composite' missing required option 'name'."; + ABSL_LOG(ERROR) << error_message; + return absl::InvalidArgumentError(error_message); + } + if (schema_params->composite_attributes() == nullptr) { + auto error_message = + "'stablehlo.composite' missing required option " + "'composite_attributes'."; + ABSL_LOG(ERROR) << error_message; + return absl::InvalidArgumentError(error_message); + } + params->name = schema_params->name()->c_str(); + params->version = schema_params->version(); + params->subgraph_index = schema_params->decomposition_subgraph_index(); + params->attributes = schema_params->composite_attributes()->data(); + params->attributes_size = schema_params->composite_attributes()->size(); + *builtin_data = params.release(); + return OkStatus(); + } + auto error_message = + "Could not get 'stablehlo.composite' operation parameters."; + ABSL_LOG(ERROR) << error_message; + return absl::InvalidArgumentError(error_message); +} + +absl::Status ParseStablehloShiftLeft(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseRound(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseRsqrt(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseSelectV2(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +absl::Status ParseShape(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data) { + SafeBuiltinDataAllocator safe_allocator(allocator); + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const ShapeOptions* schema_params = op->builtin_options_as_ShapeOptions(); + + if (schema_params != nullptr) { + TFL_FILE_ENSURE_STATUS( + ConvertTensorType(schema_params->out_type(), ¶ms->out_type)); + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + + *builtin_data = params.release(); + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseSin(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseSlice(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +absl::Status ParseSoftmax(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const SoftmaxOptions* schema_params = op->builtin_options_as_SoftmaxOptions(); + + if (schema_params != nullptr) { + params->beta = schema_params->beta(); + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + + *builtin_data = params.release(); + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseSpaceToBatchNd(const Operator*, BuiltinDataAllocator*, + void**) { + return OkStatus(); +} + +absl::Status ParseSpaceToDepth(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const auto* schema_params = op->builtin_options_as_SpaceToDepthOptions(); + if (schema_params != nullptr) { + params->block_size = schema_params->block_size(); + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + + *builtin_data = params.release(); + return OkStatus(); +} + +absl::Status ParseSplit(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const SplitOptions* schema_params = op->builtin_options_as_SplitOptions(); + + if (schema_params != nullptr) { + params->num_splits = schema_params->num_splits(); + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + + *builtin_data = params.release(); + return OkStatus(); +} + +absl::Status ParseSplitV(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + SafeBuiltinDataAllocator safe_allocator(allocator); + + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const SplitVOptions* schema_params = op->builtin_options_as_SplitVOptions(); + + if (schema_params != nullptr) { + params->num_splits = schema_params->num_splits(); + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + + *builtin_data = params.release(); + return OkStatus(); +} + +absl::Status ParseUnidirectionalSequenceLSTM(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + SafeBuiltinDataAllocator safe_allocator(allocator); + auto params = + safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + if (const auto* seq_lstm_params = + op->builtin_options_as_UnidirectionalSequenceLSTMOptions()) { + params->activation = + ConvertActivation(seq_lstm_params->fused_activation_function()); + params->cell_clip = seq_lstm_params->cell_clip(); + params->proj_clip = seq_lstm_params->proj_clip(); + params->time_major = seq_lstm_params->time_major(); + params->asymmetric_quantize_inputs = + seq_lstm_params->asymmetric_quantize_inputs(); + params->diagonal_recurrent_tensors = + seq_lstm_params->diagonal_recurrent_tensors(); + } + *builtin_data = params.release(); + return OkStatus(); +} + +absl::Status ParseSqueeze(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + SafeBuiltinDataAllocator safe_allocator(allocator); + + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const SqueezeOptions* schema_params = op->builtin_options_as_SqueezeOptions(); + + if (schema_params != nullptr) { + const auto* squeeze_dims = schema_params->squeeze_dims(); + if (squeeze_dims != nullptr) { + TFL_FILE_ENSURE_STATUS( + FlatBufferIntVectorToArray(sizeof(params->squeeze_dims), squeeze_dims, + params->squeeze_dims, "squeeze")); + params->num_squeeze_dims = squeeze_dims->size(); + } else { + params->num_squeeze_dims = 0; + } + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + + *builtin_data = params.release(); + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseSqrt(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseSquare(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseSquaredDifference(const Operator*, BuiltinDataAllocator*, + void**) { + return OkStatus(); +} + +absl::Status ParseStridedSlice(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const StridedSliceOptions* schema_params = + op->builtin_options_as_StridedSliceOptions(); + + if (schema_params != nullptr) { + params->begin_mask = schema_params->begin_mask(); + params->end_mask = schema_params->end_mask(); + params->ellipsis_mask = schema_params->ellipsis_mask(); + params->new_axis_mask = schema_params->new_axis_mask(); + params->shrink_axis_mask = schema_params->shrink_axis_mask(); + params->offset = schema_params->offset(); + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + + *builtin_data = params.release(); + return OkStatus(); +} + +absl::Status ParseSub(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const SubOptions* schema_params = op->builtin_options_as_SubOptions(); + + if (schema_params != nullptr) { + params->activation = + ConvertActivation(schema_params->fused_activation_function()); + params->pot_scale_int16 = schema_params->pot_scale_int16(); + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + + *builtin_data = params.release(); + return OkStatus(); +} + +absl::Status ParseSvdf(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const SVDFOptions* schema_params = op->builtin_options_as_SVDFOptions(); + if (schema_params != nullptr) { + params->rank = schema_params->rank(); + params->activation = + ConvertActivation(schema_params->fused_activation_function()); + params->asymmetric_quantize_inputs = + schema_params->asymmetric_quantize_inputs(); + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + + *builtin_data = params.release(); + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseTanh(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} +// +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseTranspose(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +absl::Status ParseTransposeConv(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + const TransposeConvOptions* transpose_conv_params = + op->builtin_options_as_TransposeConvOptions(); + if (transpose_conv_params != nullptr) { + params->padding = ConvertPadding(transpose_conv_params->padding()); + params->stride_width = transpose_conv_params->stride_w(); + params->stride_height = transpose_conv_params->stride_h(); + + params->activation = + ConvertActivation(transpose_conv_params->fused_activation_function()); + TFL_FILE_ENSURE_STATUS( + ConvertTensorType(transpose_conv_params->quantized_bias_type(), + ¶ms->quantized_bias_type)); + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + *builtin_data = params.release(); + return OkStatus(); +} + +absl::Status ParseUnpack(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const UnpackOptions* schema_params = op->builtin_options_as_UnpackOptions(); + + if (schema_params != nullptr) { + params->num = schema_params->num(); + params->axis = schema_params->axis(); + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + + *builtin_data = params.release(); + return OkStatus(); +} + +absl::Status ParseVarHandle(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const VarHandleOptions* schema_params = + op->builtin_options_as_VarHandleOptions(); + + if (schema_params != nullptr) { + if (schema_params->container()) { + params->container = schema_params->container()->c_str(); + } + if (schema_params->shared_name()) { + params->shared_name = schema_params->shared_name()->c_str(); + } + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + + *builtin_data = params.release(); + return OkStatus(); +} + +absl::Status ParseWhile(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + std::unique_ptr + params = safe_allocator.Allocate(); + TFL_FILE_ENSURE(params != nullptr); + + const WhileOptions* schema_params = op->builtin_options_as_WhileOptions(); + + if (schema_params != nullptr) { + params->cond_subgraph_index = schema_params->cond_subgraph_index(); + params->body_subgraph_index = schema_params->body_subgraph_index(); + } else { + // TODO(b/157480169): We should either return kTfLiteError or fill in some + // reasonable defaults in the params struct. We are not doing so until we + // better understand the ramifications of changing the legacy behavior. + } + + *builtin_data = params.release(); + return OkStatus(); +} + +absl::Status ParseStablehloCase(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data) { + CheckParsePointerParams(op, allocator, builtin_data); + + SafeBuiltinDataAllocator safe_allocator(allocator); + auto params = safe_allocator.Allocate(); + + const tflite::StablehloCaseOptions* schema_params = + op->builtin_options_2_as_StablehloCaseOptions(); + + if (!schema_params) { + return absl::InternalError("Could not get 'stablehlo.case' params"); + } + + TFL_FILE_ENSURE_STATUS(FlatBufferIntVectorToArray( + sizeof(params->branch_subgraph_indices), + schema_params->branch_subgraph_indices(), params->branch_subgraph_indices, + "stablehlo.case")); + + params->num_branches = schema_params->branch_subgraph_indices()->size(); + *builtin_data = params.release(); + + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseZerosLike(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseBitwiseXor(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +// We have this parse function instead of directly returning OkStatus() from the +// switch-case in ParseOpData because this function is used as part of the +// selective registration for the OpResolver implementation in micro. +absl::Status ParseRightShift(const Operator*, BuiltinDataAllocator*, void**) { + return OkStatus(); +} + +absl::Status ParseOpData(const Operator* op, BuiltinOperator op_type, + BuiltinDataAllocator* allocator, void** builtin_data) { +// TODO(b/145762662): It would be preferable to have the build graph for TF Lite +// Micro not have the ParseOpData function at all. This would require splitting +// the current file into two separate files, one of which defines the +// ParseOpData function and the other that defines the operator specific parse +// functions (e.g. ParseAdd). +// +// Such a split was attempted but was not worth the effort at the time because +// of the following reasons: +// * We could either duplicate the functions and the SafeBuiltinDataAllocator +// class in the anonymous namespace of this file, or attempt to make a common +// library with these helper functions and class. +// * Making a common library with a separate build target was not feasible as +// it introduced circular dependencies due to the ErrorReporter and a common +// .cc and .h within the same api build target the also cause circular +// dependencies due to the BuiltinDataAllocator class. +// * If all the builtin operators were to have their own parse functions, or we +// were ok with some amount of code duplication, then this split of the .cc +// files would be a lot more feasible. +#ifdef TF_LITE_STATIC_MEMORY + auto error_message = + "ParseOpData is unsupported on TfLiteMicro, please use the operator " + "specific parse functions (e.g. ParseAdd etc.).\n"; + ABSL_LOG(ERROR) << error_message; + return absl::UnimplementedError(error_message); +#else + return ParseOpDataTfLite(op, op_type, allocator, builtin_data); +#endif +} + +} // namespace flatbuffer_conversions +} // namespace tflite_file +// LINT.ThenChange(//tensorflow/lite/core/api/flatbuffer_conversions.cc) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/api/flatbuffer_conversions.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/api/flatbuffer_conversions.h new file mode 100644 index 0000000000000000000000000000000000000000..0dcb20ad4cff81f2ea874db3b84af1ee795475e0 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/api/flatbuffer_conversions.h @@ -0,0 +1,427 @@ +/* Copyright 2024 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_CORE_API_FLATBUFFER_CONVERSIONS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_CORE_API_FLATBUFFER_CONVERSIONS_H_ + +#include +#include + +#include "absl/status/status.h" +#include "tensorflow/compiler/mlir/lite/core/c/tflite_types.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" + +// The namespace tflite_file is for the data structures that define the .tflite +// file format, and code that is tightly coupled with those data structures. +// The .tflite file format is the serialized flatbuffer representation of +// computations on tensors that TF Lite uses for distribution of compiled ML +// models. +namespace tflite_file { + +// This namespace contains functions that transform code and data structures +// that are defined in the flatbuffer serialization format into +// in-memory values that are used by the runtime API, interpreter and compiler. +namespace flatbuffer_conversions { + +using tflite::Operator; + +// Interface class for builtin data allocations. +class BuiltinDataAllocator { + public: + virtual void* Allocate(size_t size, size_t alignment_hint) = 0; + virtual void Deallocate(void* data) = 0; + + // Allocate a structure, but make sure it is a POD structure that doesn't + // require constructors to run. The reason we do this, is that Interpreter's C + // extension part will take ownership so destructors will not be run during + // deallocation. + template + T* AllocatePOD() { + static_assert(std::is_trivially_destructible::value, + "Builtin data structure must be POD."); + void* allocated_memory = this->Allocate(sizeof(T), alignof(T)); + return new (allocated_memory) T(); + } + + virtual ~BuiltinDataAllocator() = default; +}; + +// Parse the appropriate data out of the op. +// +// This handles builtin data explicitly as there are flatbuffer schemas. +// If it returns kTfLiteOk, it passes the data out with `builtin_data`. The +// calling function has to pass in an allocator object, and this allocator +// will be called to reserve space for the output data. If the calling +// function's allocator reserves memory on the heap, then it's the calling +// function's responsibility to free it. +// If it returns kTfLiteError, `builtin_data` will be `nullptr`. +absl::Status ParseOpData(const tflite::Operator* op, + tflite::BuiltinOperator op_type, + BuiltinDataAllocator* allocator, void** builtin_data); + +// Converts the tensor data type used in the flat buffer to the representation +// used by the runtime. +absl::Status ConvertTensorType(tflite::TensorType tensor_type, + TfLiteType* type); + +absl::Status ParseAbs(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseAdd(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseAddN(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseArgMax(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseArgMin(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseAssignVariable(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseBatchMatMul(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseBatchToSpaceNd(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseBroadcastArgs(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseBroadcastTo(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseCallOnce(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseCeil(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseCast(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseConcatenation(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseConv2D(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseCos(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseCumsum(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseDepthToSpace(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseDepthwiseConv2D(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseDequantize(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseDiv(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseElu(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseEmbeddingLookup(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseEqual(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseExp(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseExpandDims(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseFill(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseFloor(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseFloorDiv(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseFloorMod(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseFullyConnected(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseGather(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseGatherNd(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseGreater(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseGreaterEqual(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseHardSwish(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseIf(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseL2Normalization(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseLeakyRelu(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseLess(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseLessEqual(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseLog(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseLogicalAnd(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseLogicalNot(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseLogicalOr(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseLogistic(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseLogSoftmax(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseLSTM(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseMaximum(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseMinimum(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseMirrorPad(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseMul(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseNeg(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseNotEqual(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParsePack(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParsePad(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParsePadV2(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParsePool(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParsePow(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParsePrelu(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseQuantize(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseReadVariable(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseReducer(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseRelu(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseRelu6(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseReshape(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseResizeBilinear(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseResizeNearestNeighbor(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseRound(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseRsqrt(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseSelectV2(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseShape(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseSin(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseSlice(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseSoftmax(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseSpaceToBatchNd(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseSpaceToDepth(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseSplit(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseSplitV(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); +absl::Status ParseSqueeze(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseSqrt(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); +absl::Status ParseSquare(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseSquaredDifference(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseStridedSlice(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseSub(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseSvdf(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseTanh(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseTranspose(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseTransposeConv(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseUnpack(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseUnidirectionalSequenceLSTM(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseVarHandle(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseWhile(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseZerosLike(const Operator* op, BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseBitwiseXor(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseRightShift(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseStablehloScatter(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseStablehloRngBitGenerator(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseStablehloGather(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseStablehloReduceWindow(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseStablehloPad(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseStablehloComposite(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseStablehloShiftLeft(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +absl::Status ParseStablehloCase(const Operator* op, + BuiltinDataAllocator* allocator, + void** builtin_data); + +} // namespace flatbuffer_conversions +} // namespace tflite_file + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_CORE_API_FLATBUFFER_CONVERSIONS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/api/flatbuffer_conversions_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/api/flatbuffer_conversions_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..6b6940dd42183571c7285761dbfe3cf94f477129 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/api/flatbuffer_conversions_test.cc @@ -0,0 +1,875 @@ +/* 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 "tensorflow/compiler/mlir/lite/core/api/flatbuffer_conversions.h" + +#include +#include +#include +#include +#include +#include + +#include +#include +#include "flatbuffers/buffer.h" // from @flatbuffers +#include "flatbuffers/flatbuffer_builder.h" // from @flatbuffers +#include "tensorflow/compiler/mlir/lite/core/c/builtin_op_data.h" +#include "tensorflow/compiler/mlir/lite/core/c/tflite_types.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" + +using testing::AllOf; +using testing::Each; +using testing::ElementsAre; +using testing::Eq; +using testing::HasSubstr; +using testing::StrEq; +using tflite::BuiltinOptions; +using tflite::BuiltinOptions2; +using tflite::BuiltinOptions_SqueezeOptions; +using tflite::CustomOptionsFormat_FLEXBUFFERS; + +namespace tflite_file { +namespace flatbuffer_conversions { +using tflite::ActivationFunctionType_RELU; +using tflite::BuiltinOperator_CONV_2D; +using tflite::BuiltinOperator_CUSTOM; +using tflite::BuiltinOperator_FULLY_CONNECTED; +using tflite::BuiltinOperator_RESHAPE; +using tflite::BuiltinOperator_SQUEEZE; +using tflite::BuiltinOperator_STABLEHLO_PAD; +using tflite::BuiltinOperator_STABLEHLO_REDUCE_WINDOW; +using tflite::BuiltinOptions2_StablehloPadOptions; +using tflite::BuiltinOptions2_StablehloReduceWindowOptions; +using tflite::BuiltinOptions_Conv2DOptions; +using tflite::BuiltinOptions_FullyConnectedOptions; +using tflite::BuiltinOptions_NONE; +using tflite::BuiltinOptions_ReshapeOptions; +using tflite::CreateReshapeOptions; +using tflite::CreateSqueezeOptions; +using tflite::CreateStablehloPadOptions; +using tflite::CreateStablehloReduceWindowOptions; +using tflite::FullyConnectedOptionsWeightsFormat; +using tflite::Padding_SAME; +using tflite::TensorType_BFLOAT16; +using tflite::TensorType_FLOAT16; +using tflite::TensorType_FLOAT32; +using tflite::TensorType_INT4; + +namespace { + +using std::string; + +// Used to determine how the op data parsing function creates its working space. +class MockDataAllocator : public BuiltinDataAllocator { + public: + MockDataAllocator() : is_allocated_(false) {} + void* Allocate(size_t size, size_t alignment_hint) override { + EXPECT_FALSE(is_allocated_); + const int max_size = kBufferSize; + EXPECT_LE(size, max_size); + is_allocated_ = true; + return buffer_; + } + void Deallocate(void* data) override { is_allocated_ = false; } + + private: + static constexpr int kBufferSize = 1024; + char buffer_[kBufferSize]; + bool is_allocated_; +}; + +} // namespace + +class FlatbufferConversionsTest : public ::testing::Test { + public: + const Operator* BuildTestOperator(BuiltinOptions op_type, + flatbuffers::Offset options) { + flatbuffers::Offset offset = + CreateOperatorDirect(builder_, 0, nullptr, nullptr, op_type, options, + nullptr, CustomOptionsFormat_FLEXBUFFERS, nullptr); + builder_.Finish(offset); + void* pointer = builder_.GetBufferPointer(); + return flatbuffers::GetRoot(pointer); + } + + const Operator* BuildTestOperator(BuiltinOptions2 op_type, + flatbuffers::Offset options) { + flatbuffers::Offset offset = CreateOperatorDirect( + builder_, /*opcode_index=*/0, /*inputs=*/nullptr, /*outputs=*/nullptr, + /*builtin_options_type=*/tflite::BuiltinOptions_NONE, + /*builtin_options=*/0, /*custom_options=*/nullptr, + /*custom_options_format=*/tflite::CustomOptionsFormat_FLEXBUFFERS, + /*mutating_variable_inputs=*/nullptr, /*intermediates=*/nullptr, + /*large_custom_options_offset=*/0, /*large_custom_options_size=*/0, + /*builtin_options_2_type=*/op_type, + /*builtin_options_2=*/options); + builder_.Finish(offset); + void* pointer = builder_.GetBufferPointer(); + return flatbuffers::GetRoot(pointer); + } + + protected: + MockDataAllocator mock_allocator_; + flatbuffers::FlatBufferBuilder builder_; +}; + +TEST_F(FlatbufferConversionsTest, ParseSqueezeAll) { + const Operator* op = BuildTestOperator( + BuiltinOptions_SqueezeOptions, CreateSqueezeOptions(builder_).Union()); + void* output_data = nullptr; + EXPECT_TRUE( + ParseOpData(op, BuiltinOperator_SQUEEZE, &mock_allocator_, &output_data) + .ok()); +} + +TEST_F(FlatbufferConversionsTest, ParseDynamicReshape) { + const Operator* op = BuildTestOperator( + BuiltinOptions_ReshapeOptions, CreateReshapeOptions(builder_).Union()); + void* output_data = nullptr; + EXPECT_TRUE( + ParseOpData(op, BuiltinOperator_RESHAPE, &mock_allocator_, &output_data) + .ok()); +} + +TEST_F(FlatbufferConversionsTest, TestParseOpDataConv) { + const Operator* conv_op = + BuildTestOperator(BuiltinOptions_Conv2DOptions, + CreateConv2DOptions(builder_, Padding_SAME, 1, 2, + ActivationFunctionType_RELU, 3, 4) + .Union()); + void* output_data = nullptr; + EXPECT_TRUE(ParseOpData(conv_op, BuiltinOperator_CONV_2D, &mock_allocator_, + &output_data) + .ok()); + EXPECT_NE(nullptr, output_data); + TfLiteConvParams* params = reinterpret_cast(output_data); + EXPECT_EQ(kTfLitePaddingSame, params->padding); + EXPECT_EQ(1, params->stride_width); + EXPECT_EQ(2, params->stride_height); + EXPECT_EQ(kTfLiteActRelu, params->activation); + EXPECT_EQ(3, params->dilation_width_factor); + EXPECT_EQ(4, params->dilation_height_factor); +} + +TEST_F(FlatbufferConversionsTest, ParseBadFullyConnected) { + const Operator* conv_op = BuildTestOperator( + BuiltinOptions_FullyConnectedOptions, + CreateFullyConnectedOptions( + builder_, ActivationFunctionType_RELU, + static_cast(-1), true) + .Union()); + void* output_data = nullptr; + EXPECT_FALSE(ParseOpData(conv_op, BuiltinOperator_FULLY_CONNECTED, + &mock_allocator_, &output_data) + .ok()); +} + +TEST_F(FlatbufferConversionsTest, TestParseOpDataCustom) { + const Operator* custom_op = + BuildTestOperator(BuiltinOptions_NONE, flatbuffers::Offset()); + void* output_data = nullptr; + EXPECT_TRUE(ParseOpData(custom_op, BuiltinOperator_CUSTOM, &mock_allocator_, + &output_data) + .ok()); + EXPECT_EQ(nullptr, output_data); +} + +TEST_F(FlatbufferConversionsTest, TestConvertTensorType) { + TfLiteType type; + EXPECT_TRUE(ConvertTensorType(TensorType_FLOAT32, &type).ok()); + EXPECT_EQ(kTfLiteFloat32, type); +} + +TEST_F(FlatbufferConversionsTest, TestConvertTensorTypeFloat16) { + TfLiteType type; + EXPECT_TRUE(ConvertTensorType(TensorType_FLOAT16, &type).ok()); + EXPECT_EQ(kTfLiteFloat16, type); +} + +TEST_F(FlatbufferConversionsTest, TestConvertTensorTypeBFloat16) { + TfLiteType type; + EXPECT_TRUE(ConvertTensorType(TensorType_BFLOAT16, &type).ok()); + EXPECT_EQ(kTfLiteBFloat16, type); +} + +TEST_F(FlatbufferConversionsTest, TestConvertTensorTypeInt4) { + TfLiteType type; + EXPECT_TRUE(ConvertTensorType(TensorType_INT4, &type).ok()); + EXPECT_EQ(kTfLiteInt4, type); +} + +class StablehloReduceWindowFlatbufferConversionsTest + : public FlatbufferConversionsTest { + public: + static constexpr int kMaxDims = + TFLITE_STABLEHLO_REDUCE_WINDOW_PARAMS_MAX_DIMENSION_COUNT; + static constexpr int64_t kValidValue = 5; + + auto ValidAttr() { + return builder_.CreateVector(std::vector(kMaxDims, kValidValue)); + } + + auto InvalidAttr() { + return builder_.CreateVector( + std::vector(kMaxDims + 1, kValidValue)); + } + + auto ValidPaddingAttr() { + return builder_.CreateVector( + std::vector(2 * kMaxDims, kValidValue)); + } + + auto InvalidPaddingAttr() { + return builder_.CreateVector( + std::vector(2 * kMaxDims + 1, kValidValue)); + } + + auto EmptyAttr() { return builder_.CreateVector({}); } +}; + +TEST_F(StablehloReduceWindowFlatbufferConversionsTest, Succeeds) { + const Operator* stablehlo_reduce_window_op = BuildTestOperator( + BuiltinOptions2_StablehloReduceWindowOptions, + CreateStablehloReduceWindowOptions( + builder_, + /*window_dimensions=*/builder_.CreateVector({1, 2}), + /*window_strides=*/builder_.CreateVector({3, 4}), + /*base_dilations=*/builder_.CreateVector({5, 6}), + /*window_dilations=*/builder_.CreateVector({7, 8}), + /*padding=*/builder_.CreateVector({9, 10, 11, 12}), + /*body_subgraph_index=*/13) + .Union()); + TfLiteStablehloReduceWindowParams* output_data = nullptr; + EXPECT_TRUE(ParseOpData(stablehlo_reduce_window_op, + BuiltinOperator_STABLEHLO_REDUCE_WINDOW, + &mock_allocator_, (void**)&output_data) + .ok()); + + EXPECT_THAT(std::make_tuple(output_data->window_dimensions, 2), + ElementsAre(1, 2)); + EXPECT_THAT(std::make_tuple(output_data->window_strides, 2), + ElementsAre(3, 4)); + EXPECT_THAT(std::make_tuple(output_data->base_dilations, 2), + ElementsAre(5, 6)); + EXPECT_THAT(std::make_tuple(output_data->window_dilations, 2), + ElementsAre(7, 8)); + EXPECT_THAT(std::make_tuple(output_data->padding, 4), + ElementsAre(9, 10, 11, 12)); + EXPECT_THAT(output_data->body_subgraph_index, Eq(13)); +} + +TEST_F(StablehloReduceWindowFlatbufferConversionsTest, + FailsWithNoWindowDimensions) { + TfLiteStablehloReduceWindowParams* output_data = nullptr; + auto status = ParseOpData( + BuildTestOperator( + BuiltinOptions2_StablehloReduceWindowOptions, + CreateStablehloReduceWindowOptions(builder_, + /*window_dimensions=*/0, + /*window_strides=*/ValidAttr(), + /*base_dilations=*/ValidAttr(), + /*window_dilations=*/ValidAttr(), + /*padding=*/ValidPaddingAttr(), + /*body_subgraph_index=*/13) + .Union()), + BuiltinOperator_STABLEHLO_REDUCE_WINDOW, &mock_allocator_, + (void**)&output_data); + EXPECT_FALSE(status.ok()); + EXPECT_THAT(status.message(), + HasSubstr("'window_dimensions' attribute is not optional for " + "'stablehlo.reduce_window' and cannot be empty.")); +} + +TEST_F(StablehloReduceWindowFlatbufferConversionsTest, + SucceedsWithNoWindowStrides) { + TfLiteStablehloReduceWindowParams* output_data = nullptr; + auto status = ParseOpData( + BuildTestOperator( + BuiltinOptions2_StablehloReduceWindowOptions, + CreateStablehloReduceWindowOptions(builder_, + /*window_dimensions=*/ValidAttr(), + /*window_strides=*/0, + /*base_dilations=*/ValidAttr(), + /*window_dilations=*/ValidAttr(), + /*padding=*/ValidPaddingAttr(), + /*body_subgraph_index=*/13) + .Union()), + BuiltinOperator_STABLEHLO_REDUCE_WINDOW, &mock_allocator_, + (void**)&output_data); + EXPECT_TRUE(status.ok()); + EXPECT_THAT(status.message(), StrEq("")); + EXPECT_THAT(std::make_tuple(output_data->window_dimensions, kMaxDims), + Each(kValidValue)); + EXPECT_THAT(std::make_tuple(output_data->window_strides, kMaxDims), Each(1)); + EXPECT_THAT(std::make_tuple(output_data->base_dilations, kMaxDims), + Each(kValidValue)); + EXPECT_THAT(std::make_tuple(output_data->window_dilations, kMaxDims), + Each(kValidValue)); + EXPECT_THAT(std::make_tuple(output_data->padding, 2 * kMaxDims), + Each(kValidValue)); + EXPECT_THAT(output_data->body_subgraph_index, Eq(13)); +} + +TEST_F(StablehloReduceWindowFlatbufferConversionsTest, + SucceedsWithNoBaseDilations) { + TfLiteStablehloReduceWindowParams* output_data = nullptr; + auto status = ParseOpData( + BuildTestOperator( + BuiltinOptions2_StablehloReduceWindowOptions, + CreateStablehloReduceWindowOptions(builder_, + /*window_dimensions=*/ValidAttr(), + /*window_strides=*/ValidAttr(), + /*base_dilations=*/0, + /*window_dilations=*/ValidAttr(), + /*padding=*/ValidPaddingAttr(), + /*body_subgraph_index=*/13) + .Union()), + BuiltinOperator_STABLEHLO_REDUCE_WINDOW, &mock_allocator_, + (void**)&output_data); + EXPECT_TRUE(status.ok()); + EXPECT_THAT(status.message(), StrEq("")); + EXPECT_THAT(std::make_tuple(output_data->window_dimensions, kMaxDims), + Each(kValidValue)); + EXPECT_THAT(std::make_tuple(output_data->window_strides, kMaxDims), + Each(kValidValue)); + EXPECT_THAT(std::make_tuple(output_data->base_dilations, kMaxDims), Each(1)); + EXPECT_THAT(std::make_tuple(output_data->window_dilations, kMaxDims), + Each(kValidValue)); + EXPECT_THAT(std::make_tuple(output_data->padding, 2 * kMaxDims), + Each(kValidValue)); + EXPECT_THAT(output_data->body_subgraph_index, Eq(13)); +} + +TEST_F(StablehloReduceWindowFlatbufferConversionsTest, + SucceedsWithNoWindowDilations) { + TfLiteStablehloReduceWindowParams* output_data = nullptr; + auto status = ParseOpData( + BuildTestOperator( + BuiltinOptions2_StablehloReduceWindowOptions, + CreateStablehloReduceWindowOptions(builder_, + /*window_dimensions=*/ValidAttr(), + /*window_strides=*/ValidAttr(), + /*base_dilations=*/ValidAttr(), + /*window_dilations=*/0, + /*padding=*/ValidPaddingAttr(), + /*body_subgraph_index=*/13) + .Union()), + BuiltinOperator_STABLEHLO_REDUCE_WINDOW, &mock_allocator_, + (void**)&output_data); + EXPECT_TRUE(status.ok()); + EXPECT_THAT(status.message(), StrEq("")); + EXPECT_THAT(std::make_tuple(output_data->window_dimensions, kMaxDims), + Each(kValidValue)); + EXPECT_THAT(std::make_tuple(output_data->window_strides, kMaxDims), + Each(kValidValue)); + EXPECT_THAT(std::make_tuple(output_data->base_dilations, kMaxDims), + Each(kValidValue)); + EXPECT_THAT(std::make_tuple(output_data->window_dilations, kMaxDims), + Each(1)); + EXPECT_THAT(std::make_tuple(output_data->padding, 2 * kMaxDims), + Each(kValidValue)); + EXPECT_THAT(output_data->body_subgraph_index, Eq(13)); +} + +TEST_F(StablehloReduceWindowFlatbufferConversionsTest, SucceedsWithNoPadding) { + TfLiteStablehloReduceWindowParams* output_data = nullptr; + auto status = ParseOpData( + BuildTestOperator( + BuiltinOptions2_StablehloReduceWindowOptions, + CreateStablehloReduceWindowOptions(builder_, + /*window_dimensions=*/ValidAttr(), + /*window_strides=*/ValidAttr(), + /*base_dilations=*/ValidAttr(), + /*window_dilations=*/ValidAttr(), + /*padding=*/0, + /*body_subgraph_index=*/13) + .Union()), + BuiltinOperator_STABLEHLO_REDUCE_WINDOW, &mock_allocator_, + (void**)&output_data); + EXPECT_TRUE(status.ok()); + EXPECT_THAT(status.message(), StrEq("")); + EXPECT_THAT(std::make_tuple(output_data->window_dimensions, kMaxDims), + Each(kValidValue)); + EXPECT_THAT(std::make_tuple(output_data->window_strides, kMaxDims), + Each(kValidValue)); + EXPECT_THAT(std::make_tuple(output_data->base_dilations, kMaxDims), + Each(kValidValue)); + EXPECT_THAT(std::make_tuple(output_data->window_dilations, kMaxDims), + Each(kValidValue)); + EXPECT_THAT(std::make_tuple(output_data->padding, 2 * kMaxDims), Each(0)); + EXPECT_THAT(output_data->body_subgraph_index, Eq(13)); +} + +TEST_F(StablehloReduceWindowFlatbufferConversionsTest, + FailsWithEmptyWindowDimensions) { + TfLiteStablehloReduceWindowParams* output_data = nullptr; + auto status = ParseOpData( + BuildTestOperator( + BuiltinOptions2_StablehloReduceWindowOptions, + CreateStablehloReduceWindowOptions(builder_, + /*window_dimensions=*/EmptyAttr(), + /*window_strides=*/ValidAttr(), + /*base_dilations=*/ValidAttr(), + /*window_dilations=*/ValidAttr(), + /*padding=*/ValidPaddingAttr(), + /*body_subgraph_index=*/13) + .Union()), + BuiltinOperator_STABLEHLO_REDUCE_WINDOW, &mock_allocator_, + (void**)&output_data); + EXPECT_FALSE(status.ok()); + EXPECT_THAT(status.message(), + HasSubstr("'window_dimensions' attribute is not optional for " + "'stablehlo.reduce_window' and cannot be empty.")); +} + +TEST_F(StablehloReduceWindowFlatbufferConversionsTest, + SucceedsWithEmptyWindowStrides) { + TfLiteStablehloReduceWindowParams* output_data = nullptr; + auto status = ParseOpData( + BuildTestOperator( + BuiltinOptions2_StablehloReduceWindowOptions, + CreateStablehloReduceWindowOptions(builder_, + /*window_dimensions=*/ValidAttr(), + /*window_strides=*/EmptyAttr(), + /*base_dilations=*/ValidAttr(), + /*window_dilations=*/ValidAttr(), + /*padding=*/ValidPaddingAttr(), + /*body_subgraph_index=*/13) + .Union()), + BuiltinOperator_STABLEHLO_REDUCE_WINDOW, &mock_allocator_, + (void**)&output_data); + EXPECT_TRUE(status.ok()); + EXPECT_THAT(status.message(), StrEq("")); + EXPECT_THAT(std::make_tuple(output_data->window_dimensions, kMaxDims), + Each(kValidValue)); + EXPECT_THAT(std::make_tuple(output_data->window_strides, kMaxDims), Each(1)); + EXPECT_THAT(std::make_tuple(output_data->base_dilations, kMaxDims), + Each(kValidValue)); + EXPECT_THAT(std::make_tuple(output_data->window_dilations, kMaxDims), + Each(kValidValue)); + EXPECT_THAT(std::make_tuple(output_data->padding, 2 * kMaxDims), + Each(kValidValue)); + EXPECT_THAT(output_data->body_subgraph_index, Eq(13)); +} + +TEST_F(StablehloReduceWindowFlatbufferConversionsTest, + SucceedsWithEmptyBaseDilations) { + TfLiteStablehloReduceWindowParams* output_data = nullptr; + auto status = ParseOpData( + BuildTestOperator( + BuiltinOptions2_StablehloReduceWindowOptions, + CreateStablehloReduceWindowOptions(builder_, + /*window_dimensions=*/ValidAttr(), + /*window_strides=*/ValidAttr(), + /*base_dilations=*/EmptyAttr(), + /*window_dilations=*/ValidAttr(), + /*padding=*/ValidPaddingAttr(), + /*body_subgraph_index=*/13) + .Union()), + BuiltinOperator_STABLEHLO_REDUCE_WINDOW, &mock_allocator_, + (void**)&output_data); + EXPECT_TRUE(status.ok()); + EXPECT_THAT(status.message(), StrEq("")); + EXPECT_THAT(std::make_tuple(output_data->window_dimensions, kMaxDims), + Each(kValidValue)); + EXPECT_THAT(std::make_tuple(output_data->window_strides, kMaxDims), + Each(kValidValue)); + EXPECT_THAT(std::make_tuple(output_data->base_dilations, kMaxDims), Each(1)); + EXPECT_THAT(std::make_tuple(output_data->window_dilations, kMaxDims), + Each(kValidValue)); + EXPECT_THAT(std::make_tuple(output_data->padding, 2 * kMaxDims), + Each(kValidValue)); + EXPECT_THAT(output_data->body_subgraph_index, Eq(13)); +} + +TEST_F(StablehloReduceWindowFlatbufferConversionsTest, + SucceedsWithEmptyWindowDilations) { + TfLiteStablehloReduceWindowParams* output_data = nullptr; + auto status = ParseOpData( + BuildTestOperator( + BuiltinOptions2_StablehloReduceWindowOptions, + CreateStablehloReduceWindowOptions(builder_, + /*window_dimensions=*/ValidAttr(), + /*window_strides=*/ValidAttr(), + /*base_dilations=*/ValidAttr(), + /*window_dilations=*/EmptyAttr(), + /*padding=*/ValidPaddingAttr(), + /*body_subgraph_index=*/13) + .Union()), + BuiltinOperator_STABLEHLO_REDUCE_WINDOW, &mock_allocator_, + (void**)&output_data); + EXPECT_TRUE(status.ok()); + EXPECT_THAT(status.message(), StrEq("")); + EXPECT_THAT(std::make_tuple(output_data->window_dimensions, kMaxDims), + Each(kValidValue)); + EXPECT_THAT(std::make_tuple(output_data->window_strides, kMaxDims), + Each(kValidValue)); + EXPECT_THAT(std::make_tuple(output_data->base_dilations, kMaxDims), + Each(kValidValue)); + EXPECT_THAT(std::make_tuple(output_data->window_dilations, kMaxDims), + Each(1)); + EXPECT_THAT(std::make_tuple(output_data->padding, 2 * kMaxDims), + Each(kValidValue)); + EXPECT_THAT(output_data->body_subgraph_index, Eq(13)); +} + +TEST_F(StablehloReduceWindowFlatbufferConversionsTest, + SucceedsWithEmptyPadding) { + TfLiteStablehloReduceWindowParams* output_data = nullptr; + auto status = ParseOpData( + BuildTestOperator( + BuiltinOptions2_StablehloReduceWindowOptions, + CreateStablehloReduceWindowOptions(builder_, + /*window_dimensions=*/ValidAttr(), + /*window_strides=*/ValidAttr(), + /*base_dilations=*/ValidAttr(), + /*window_dilations=*/ValidAttr(), + /*padding=*/EmptyAttr(), + /*body_subgraph_index=*/13) + .Union()), + BuiltinOperator_STABLEHLO_REDUCE_WINDOW, &mock_allocator_, + (void**)&output_data); + EXPECT_TRUE(status.ok()); + EXPECT_THAT(status.message(), StrEq("")); + EXPECT_THAT(std::make_tuple(output_data->window_dimensions, kMaxDims), + Each(kValidValue)); + EXPECT_THAT(std::make_tuple(output_data->window_strides, kMaxDims), + Each(kValidValue)); + EXPECT_THAT(std::make_tuple(output_data->base_dilations, kMaxDims), + Each(kValidValue)); + EXPECT_THAT(std::make_tuple(output_data->window_dilations, kMaxDims), + Each(kValidValue)); + EXPECT_THAT(std::make_tuple(output_data->padding, 2 * kMaxDims), Each(0)); + EXPECT_THAT(output_data->body_subgraph_index, Eq(13)); +} + +TEST_F(StablehloReduceWindowFlatbufferConversionsTest, + SucceedsWithParamsAtMaxDims) { + TfLiteStablehloReduceWindowParams* output_data = nullptr; + auto status = ParseOpData( + BuildTestOperator( + BuiltinOptions2_StablehloReduceWindowOptions, + CreateStablehloReduceWindowOptions(builder_, + /*window_dimensions=*/ValidAttr(), + /*window_strides=*/ValidAttr(), + /*base_dilations=*/ValidAttr(), + /*window_dilations=*/ValidAttr(), + /*padding=*/ValidPaddingAttr(), + /*body_subgraph_index=*/13) + .Union()), + BuiltinOperator_STABLEHLO_REDUCE_WINDOW, &mock_allocator_, + (void**)&output_data); + EXPECT_TRUE(status.ok()); + EXPECT_THAT(status.message(), StrEq("")); +} + +TEST_F(StablehloReduceWindowFlatbufferConversionsTest, + FailsWhenWindowDimensionsHasMoreThanMaxDims) { + TfLiteStablehloReduceWindowParams* output_data = nullptr; + auto status = ParseOpData( + BuildTestOperator(BuiltinOptions2_StablehloReduceWindowOptions, + CreateStablehloReduceWindowOptions( + builder_, + /*window_dimensions=*/InvalidAttr(), + /*window_strides=*/ValidAttr(), + /*base_dilations=*/ValidAttr(), + /*window_dilations=*/ValidAttr(), + /*padding=*/ValidPaddingAttr(), + /*body_subgraph_index=*/13) + .Union()), + BuiltinOperator_STABLEHLO_REDUCE_WINDOW, &mock_allocator_, + (void**)&output_data); + EXPECT_FALSE(status.ok()); + EXPECT_THAT(status.message(), + AllOf(HasSubstr("Found too many dimensions in the input array of " + "operation 'stablehlo.reduce_window'."), + HasSubstr("Check the 'window_dimensions' attribute."))); +} + +TEST_F(StablehloReduceWindowFlatbufferConversionsTest, + FailsWhenWindowStridesHasWrongDimCount) { + TfLiteStablehloReduceWindowParams* output_data = nullptr; + auto status = ParseOpData( + BuildTestOperator( + BuiltinOptions2_StablehloReduceWindowOptions, + CreateStablehloReduceWindowOptions(builder_, + /*window_dimensions=*/ValidAttr(), + /*window_strides=*/InvalidAttr(), + /*base_dilations=*/ValidAttr(), + /*window_dilations=*/ValidAttr(), + /*padding=*/ValidPaddingAttr(), + /*body_subgraph_index=*/13) + .Union()), + BuiltinOperator_STABLEHLO_REDUCE_WINDOW, &mock_allocator_, + (void**)&output_data); + EXPECT_FALSE(status.ok()); + EXPECT_THAT( + status.message(), + HasSubstr("'window_strides' attribute of 'stablehlo.reduce_window' does " + "not have the expected size")); +} + +TEST_F(StablehloReduceWindowFlatbufferConversionsTest, + FailsWhenBaseDilationsHasWrongDimCount) { + TfLiteStablehloReduceWindowParams* output_data = nullptr; + auto status = ParseOpData( + BuildTestOperator( + BuiltinOptions2_StablehloReduceWindowOptions, + CreateStablehloReduceWindowOptions(builder_, + /*window_dimensions=*/ValidAttr(), + /*window_strides=*/ValidAttr(), + /*base_dilations=*/InvalidAttr(), + /*window_dilations=*/ValidAttr(), + /*padding=*/ValidPaddingAttr(), + /*body_subgraph_index=*/13) + .Union()), + BuiltinOperator_STABLEHLO_REDUCE_WINDOW, &mock_allocator_, + (void**)&output_data); + EXPECT_FALSE(status.ok()); + EXPECT_THAT( + status.message(), + HasSubstr("'base_dilations' attribute of 'stablehlo.reduce_window' does " + "not have the expected size")); +} + +TEST_F(StablehloReduceWindowFlatbufferConversionsTest, + FailsWhenWindowDilationsHasWrongDimCount) { + TfLiteStablehloReduceWindowParams* output_data = nullptr; + auto status = ParseOpData( + BuildTestOperator( + BuiltinOptions2_StablehloReduceWindowOptions, + CreateStablehloReduceWindowOptions(builder_, + /*window_dimensions=*/ValidAttr(), + /*window_strides=*/ValidAttr(), + /*base_dilations=*/ValidAttr(), + /*window_dilations=*/InvalidAttr(), + /*padding=*/ValidPaddingAttr(), + /*body_subgraph_index=*/13) + .Union()), + BuiltinOperator_STABLEHLO_REDUCE_WINDOW, &mock_allocator_, + (void**)&output_data); + EXPECT_FALSE(status.ok()); + EXPECT_THAT( + status.message(), + HasSubstr( + "'window_dilations' attribute of 'stablehlo.reduce_window' does " + "not have the expected size")); +} + +TEST_F(StablehloReduceWindowFlatbufferConversionsTest, + FailsWhenPaddingHasWrongDimCount) { + TfLiteStablehloReduceWindowParams* output_data = nullptr; + auto status = ParseOpData( + BuildTestOperator( + BuiltinOptions2_StablehloReduceWindowOptions, + CreateStablehloReduceWindowOptions(builder_, + /*window_dimensions=*/ValidAttr(), + /*window_strides=*/ValidAttr(), + /*base_dilations=*/ValidAttr(), + /*window_dilations=*/ValidAttr(), + /*padding=*/InvalidPaddingAttr(), + /*body_subgraph_index=*/13) + .Union()), + BuiltinOperator_STABLEHLO_REDUCE_WINDOW, &mock_allocator_, + (void**)&output_data); + EXPECT_FALSE(status.ok()); + EXPECT_THAT(status.message(), + HasSubstr("'padding' attribute of 'stablehlo.reduce_window' does " + "not have the expected size")); +} + +TEST_F(StablehloReduceWindowFlatbufferConversionsTest, FailsWithWrongOptions) { + const Operator* stablehlo_reduce_window_op = + BuildTestOperator(BuiltinOptions2_StablehloReduceWindowOptions, 0); + TfLiteStablehloReduceWindowParams* output_data = nullptr; + auto status = ParseOpData(stablehlo_reduce_window_op, + BuiltinOperator_STABLEHLO_REDUCE_WINDOW, + &mock_allocator_, (void**)&output_data); + EXPECT_FALSE(status.ok()); + EXPECT_THAT( + status.message(), + HasSubstr( + "Could not get 'stablehlo.reduce_window' operation parameters.")); +} + +TEST_F(StablehloReduceWindowFlatbufferConversionsTest, DeathTests) { + const Operator* stablehlo_reduce_window_op = BuildTestOperator( + BuiltinOptions2_StablehloReduceWindowOptions, + CreateStablehloReduceWindowOptions( + builder_, /*window_dimensions=*/ValidAttr(), + /*window_strides=*/ValidAttr(), + /*base_dilations=*/ValidAttr(), + /*window_dilations=*/ValidAttr(), + /*padding=*/ValidPaddingAttr(), /*body_subgraph_index=*/13) + .Union()); + TfLiteStablehloReduceWindowParams* output_data = nullptr; +#ifdef NDEBUG + GTEST_SKIP(); +#endif + EXPECT_DEATH(ParseOpData(nullptr, BuiltinOperator_STABLEHLO_REDUCE_WINDOW, + &mock_allocator_, (void**)&output_data) + .IgnoreError(), + ""); + EXPECT_DEATH(ParseOpData(stablehlo_reduce_window_op, + BuiltinOperator_STABLEHLO_REDUCE_WINDOW, nullptr, + (void**)&output_data) + .IgnoreError(), + ""); + EXPECT_DEATH(ParseOpData(stablehlo_reduce_window_op, + BuiltinOperator_STABLEHLO_REDUCE_WINDOW, + &mock_allocator_, nullptr) + .IgnoreError(), + ""); +} + +class StablehloPadFlatbufferConversionsTest : public FlatbufferConversionsTest { + public: + static constexpr int kMaxDims = + TFLITE_STABLEHLO_PAD_PARAMS_MAX_DIMENSION_COUNT; + static constexpr int64_t kValidValue = 5; +}; + +TEST_F(StablehloPadFlatbufferConversionsTest, Succeeds) { + const Operator* stablehlo_pad_op = BuildTestOperator( + BuiltinOptions2_StablehloPadOptions, + CreateStablehloPadOptions( + builder_, + /*edge_padding_low=*/builder_.CreateVector({1, 0, -1}), + /*edge_padding_high=*/builder_.CreateVector({2, 0, -2}), + /*interior_padding=*/builder_.CreateVector({3, 0, 3})) + .Union()); + TfLiteStablehloPadParams* output_data = nullptr; + EXPECT_TRUE(ParseOpData(stablehlo_pad_op, BuiltinOperator_STABLEHLO_PAD, + &mock_allocator_, (void**)&output_data) + .ok()); + EXPECT_THAT(std::make_tuple(output_data->edge_padding_low, 3), + ElementsAre(1, 0, -1)); + EXPECT_THAT(std::make_tuple(output_data->edge_padding_high, 3), + ElementsAre(2, 0, -2)); + EXPECT_THAT(std::make_tuple(output_data->interior_padding, 3), + ElementsAre(3, 0, 3)); +} + +TEST_F(StablehloPadFlatbufferConversionsTest, FailsWithMissingLowPadding) { + const Operator* stablehlo_pad_op = BuildTestOperator( + BuiltinOptions2_StablehloPadOptions, + CreateStablehloPadOptions( + builder_, + /*edge_padding_low=*/0, + /*edge_padding_high=*/builder_.CreateVector({2, 0, -2}), + /*interior_padding=*/builder_.CreateVector({3, 0, 3})) + .Union()); + TfLiteStablehloPadParams* output_data = nullptr; + auto status = ParseOpData(stablehlo_pad_op, BuiltinOperator_STABLEHLO_PAD, + &mock_allocator_, (void**)&output_data); + EXPECT_FALSE(status.ok()); + EXPECT_THAT( + status.message(), + AllOf( + HasSubstr("Input array not provided for operation 'stablehlo.pad'."), + HasSubstr("Check the 'edge_padding_low' attribute."))); +} + +TEST_F(StablehloPadFlatbufferConversionsTest, FailsWithMissingHighPadding) { + const Operator* stablehlo_pad_op = BuildTestOperator( + BuiltinOptions2_StablehloPadOptions, + CreateStablehloPadOptions( + builder_, + /*edge_padding_low=*/builder_.CreateVector({1, 0, -1}), + /*edge_padding_high=*/0, + /*interior_padding=*/builder_.CreateVector({3, 0, 3})) + .Union()); + TfLiteStablehloPadParams* output_data = nullptr; + auto status = ParseOpData(stablehlo_pad_op, BuiltinOperator_STABLEHLO_PAD, + &mock_allocator_, (void**)&output_data); + EXPECT_FALSE(status.ok()); + EXPECT_THAT( + status.message(), + AllOf( + HasSubstr("Input array not provided for operation 'stablehlo.pad'."), + HasSubstr("Check the 'edge_padding_high' attribute."))); +} + +TEST_F(StablehloPadFlatbufferConversionsTest, FailsWithMissingInteriorPadding) { + const Operator* stablehlo_pad_op = BuildTestOperator( + BuiltinOptions2_StablehloPadOptions, + CreateStablehloPadOptions( + builder_, + /*edge_padding_low=*/builder_.CreateVector({1, 0, -1}), + /*edge_padding_high=*/builder_.CreateVector({2, 0, -2}), + /*interior_padding=*/0) + .Union()); + TfLiteStablehloPadParams* output_data = nullptr; + auto status = ParseOpData(stablehlo_pad_op, BuiltinOperator_STABLEHLO_PAD, + &mock_allocator_, (void**)&output_data); + EXPECT_FALSE(status.ok()); + EXPECT_THAT( + status.message(), + AllOf( + HasSubstr("Input array not provided for operation 'stablehlo.pad'."), + HasSubstr("Check the 'interior_padding' attribute."))); +} + +TEST_F(StablehloPadFlatbufferConversionsTest, FailsInconsistentSizes) { + const Operator* stablehlo_pad_op = BuildTestOperator( + BuiltinOptions2_StablehloPadOptions, + CreateStablehloPadOptions( + builder_, + /*edge_padding_low=*/builder_.CreateVector({1, 0, -1}), + /*edge_padding_high=*/builder_.CreateVector({2, 0, -2}), + /*interior_padding=*/builder_.CreateVector({3, 0, -3, 5})) + .Union()); + TfLiteStablehloPadParams* output_data = nullptr; + auto status = ParseOpData(stablehlo_pad_op, BuiltinOperator_STABLEHLO_PAD, + &mock_allocator_, (void**)&output_data); + EXPECT_FALSE(status.ok()); + EXPECT_THAT(status.message(), + HasSubstr("'stablehlo.pad' operation parameter array sizes are " + "not consistent.")); +} + +TEST_F(StablehloPadFlatbufferConversionsTest, FailsWithWrongOptions) { + const Operator* stablehlo_pad_op = BuildTestOperator(BuiltinOptions_NONE, 0); + TfLiteStablehloPadParams* output_data = nullptr; + auto status = ParseOpData(stablehlo_pad_op, BuiltinOperator_STABLEHLO_PAD, + &mock_allocator_, (void**)&output_data); + EXPECT_FALSE(status.ok()); + EXPECT_THAT(status.message(), + HasSubstr("Could not get 'stablehlo.pad' operation parameters.")); +} + +TEST_F(StablehloPadFlatbufferConversionsTest, DeathTests) { + const Operator* stablehlo_pad_op = BuildTestOperator(BuiltinOptions_NONE, 0); + TfLiteStablehloPadParams* output_data = nullptr; +#ifdef NDEBUG + GTEST_SKIP(); +#endif + EXPECT_DEATH(ParseOpData(nullptr, BuiltinOperator_STABLEHLO_PAD, + &mock_allocator_, (void**)&output_data) + .IgnoreError(), + ""); + EXPECT_DEATH(ParseOpData(stablehlo_pad_op, BuiltinOperator_STABLEHLO_PAD, + nullptr, (void**)&output_data) + .IgnoreError(), + ""); + EXPECT_DEATH(ParseOpData(stablehlo_pad_op, BuiltinOperator_STABLEHLO_PAD, + &mock_allocator_, nullptr) + .IgnoreError(), + ""); +} + +} // namespace flatbuffer_conversions +} // namespace tflite_file diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/api/verifier.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/api/verifier.h new file mode 100644 index 0000000000000000000000000000000000000000..1be8c76d8c884766ee75c8641a863f622331e049 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/api/verifier.h @@ -0,0 +1,39 @@ +/* Copyright 2020 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. +==============================================================================*/ +/// \file +/// +/// Abstract interface for verifying a model. +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_CORE_API_VERIFIER_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_CORE_API_VERIFIER_H_ + +#include "tensorflow/compiler/mlir/lite/core/api/error_reporter.h" + +namespace tflite { + +/// Abstract interface that verifies whether a given model is legit. +/// It facilitates the use-case to verify and build a model without loading it +/// twice. +/// (See also "tensorflow/lite/tools/verifier.h".) +class TfLiteVerifier { + public: + /// Returns true if the model is legit. + virtual bool Verify(const char* data, int length, + ErrorReporter* reporter) = 0; + virtual ~TfLiteVerifier() = default; +}; + +} // namespace tflite + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_CORE_API_VERIFIER_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/c/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/c/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..10f52de0566dd28ff91788e617ae893af242fb7f --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/c/BUILD @@ -0,0 +1,67 @@ +load("@rules_cc//cc:cc_library.bzl", "cc_library") +load("//tensorflow:tensorflow.default.bzl", "get_compatible_with_portable") +load("//tensorflow/compiler/mlir/lite:build_def.bzl", "tflite_copts") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + default_visibility = [ + "//visibility:public", + ], + licenses = ["notice"], +) + +filegroup( + name = "tflite_internal_cc_3p_api_deps_src", + srcs = [ + ":tflite_types.h", + ], + visibility = ["//tensorflow/lite:__pkg__"], +) + +filegroup( + name = "lite_headers_filegroup", + srcs = [ + "builtin_op_data.h", + "tflite_types.h", + ], + visibility = ["//visibility:public"], +) + +exports_files( + srcs = [ + "builtin_op_data.h", + "tflite_types.h", + ], + visibility = [ + # copybara:uncomment_begin(google-only) + # "//tensorflow/lite:__subpackages__", + # copybara:uncomment_end_and_comment_begin + "//visibility:public", + # copybara:comment_end + ], +) + +cc_library( + name = "tflite_types", + hdrs = [ + "tflite_types.h", + ], + compatible_with = get_compatible_with_portable(), + copts = tflite_copts(), + visibility = ["//visibility:public"], +) + +# LINT.IfChange(common) +cc_library( + name = "tflite_common", + srcs = [], + hdrs = [ + "builtin_op_data.h", + "tflite_types.h", + ], + compatible_with = get_compatible_with_portable(), + copts = tflite_copts(), + visibility = ["//visibility:public"], + alwayslink = 1, # Why?? TODO(b/161243354): eliminate this. +) +# LINT.ThenChange(//tensorflow/lite/core/c:common) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/c/builtin_op_data.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/c/builtin_op_data.h new file mode 100644 index 0000000000000000000000000000000000000000..e2462f49302ac2c507a5787d98dcf3eddecc334b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/c/builtin_op_data.h @@ -0,0 +1,671 @@ +/* Copyright 2024 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. +==============================================================================*/ + +/// WARNING: Users of TensorFlow Lite should not include this file directly, +/// only the TensorFlow Lite implementation itself should. + +// IWYU pragma: private, include "third_party/tensorflow/lite/c/builtin_op_data.h" + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_CORE_C_BUILTIN_OP_DATA_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_CORE_C_BUILTIN_OP_DATA_H_ + +#include // IWYU pragma: keep +#include +#include + +#include "tensorflow/compiler/mlir/lite/core/c/tflite_types.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +// TfLiteReshapeParams can't have dynamic data so we fix the maximum possible +// number of dimensions. +#define TFLITE_RESHAPE_PARAMS_MAX_DIMENSION_COUNT 8 +#define TFLITE_STABLEHLO_SCATTER_PARAMS_MAX_DIMENSION_COUNT 8 +#define TFLITE_STABLEHLO_GATHER_PARAMS_MAX_DIMENSION_COUNT 8 +#define TFLITE_STABLEHLO_REDUCE_WINDOW_PARAMS_MAX_DIMENSION_COUNT 8 +#define TFLITE_STABLEHLO_PAD_PARAMS_MAX_DIMENSION_COUNT 8 +#define TFLITE_STABLEHLO_CASE_PARAMS_MAX_BRANCHES_COUNT 20 + +// TODO(aselle): Consider using "if this then that" for testing. + +// Useful placeholder to put in otherwise empty structs to avoid size warnings. +typedef struct { + char dummy; +} EmptyStructPlaceholder; + +// IMPORTANT: All new members of structs must be added at the end to ensure +// backwards compatibility. + +// Possible padding types (for convolutions) +typedef enum { + kTfLitePaddingUnknown = 0, + kTfLitePaddingSame, + kTfLitePaddingValid, +} TfLitePadding; + +typedef enum { + kTfLiteMirrorPaddingUnknown = 0, + kTfLiteMirrorPaddingReflect, + kTfLiteMirrorPaddingSymmetric, +} TfLiteMirrorPaddingMode; + +// TODO(b/130259536): We should move this out of builtin_op_data. +typedef struct { + int width; + int height; + int width_offset; + int height_offset; +} TfLitePaddingValues; + +typedef struct { + TfLiteMirrorPaddingMode mode; +} TfLiteMirrorPaddingParams; + +// Possible fused activation functions. +typedef enum { + kTfLiteActNone = 0, + kTfLiteActRelu, + kTfLiteActReluN1To1, // min(max(-1, x), 1) + kTfLiteActRelu6, // min(max(0, x), 6) + kTfLiteActTanh, + kTfLiteActSignBit, + kTfLiteActSigmoid, +} TfLiteFusedActivation; + +typedef struct { + // Parameters for CONV_2D version 1. + TfLitePadding padding; + int stride_width; + int stride_height; + TfLiteFusedActivation activation; + + // Parameters for CONV_2D version 2. + // Note: Version 2 supports dilation values not equal to 1. + int dilation_width_factor; + int dilation_height_factor; + + // Parameters for CONV_2D version 7 or above. + // Used to determine the default value for the quantized bias. + TfLiteType quantized_bias_type; +} TfLiteConvParams; + +typedef struct { + TfLitePadding padding; + int stride_width; + int stride_height; + int stride_depth; + int dilation_width_factor; + int dilation_height_factor; + int dilation_depth_factor; + TfLiteFusedActivation activation; +} TfLiteConv3DParams; + +typedef TfLiteConv3DParams TfLiteConv3DTransposeParams; + +typedef struct { + TfLitePadding padding; + int stride_width; + int stride_height; + int filter_width; + int filter_height; + TfLiteFusedActivation activation; + struct { + TfLitePaddingValues padding; + } computed; +} TfLitePoolParams; + +typedef struct { + // Parameters for DepthwiseConv version 1 or above. + TfLitePadding padding; + int stride_width; + int stride_height; + // `depth_multiplier` is redundant. It's used by CPU kernels in + // TensorFlow 2.0 or below, but ignored in versions above. + // + // The information can be deduced from the shape of input and the shape of + // weights. Since the TFLiteConverter toolchain doesn't support partially + // specified shapes, relying on `depth_multiplier` stops us from supporting + // graphs with dynamic shape tensors. + // + // Note: Some of the delegates (e.g. NNAPI, GPU) are still relying on this + // field. + int depth_multiplier; + TfLiteFusedActivation activation; + // Parameters for DepthwiseConv version 2 or above. + int dilation_width_factor; + int dilation_height_factor; +} TfLiteDepthwiseConvParams; + +typedef struct { + int rank; + TfLiteFusedActivation activation; + + // Parameter for SVDF version 4. + bool asymmetric_quantize_inputs; +} TfLiteSVDFParams; + +typedef struct { + TfLiteFusedActivation activation; + + // Parameter for RNN version 3. + bool asymmetric_quantize_inputs; +} TfLiteRNNParams; + +typedef struct { + bool time_major; + TfLiteFusedActivation activation; + + // Parameter for Sequence RNN version 3. + bool asymmetric_quantize_inputs; +} TfLiteSequenceRNNParams; + +typedef struct { + bool time_major; + TfLiteFusedActivation activation; + bool merge_outputs; + + // Parameter for Bidirectional RNN version 3. + bool asymmetric_quantize_inputs; +} TfLiteBidirectionalSequenceRNNParams; + +typedef enum { + kTfLiteFullyConnectedWeightsFormatDefault = 0, + kTfLiteFullyConnectedWeightsFormatShuffled4x16Int8 = 1, +} TfLiteFullyConnectedWeightsFormat; + +typedef struct { + // Parameters for FullyConnected version 1 or above. + TfLiteFusedActivation activation; + + // Parameters for FullyConnected version 2 or above. + TfLiteFullyConnectedWeightsFormat weights_format; + + // Parameters for FullyConnected version 5 or above. + // If set to true, then the number of dimensions in the input and the output + // tensors are the same. Furthermore, all but the last dimension of the input + // and output shapes will be equal. + bool keep_num_dims; + + // Parameters for FullyConnected version 7 or above. + // If set to true and the weights are quantized, then non constant inputs + // are quantized at evaluation time with asymmetric quantization. + bool asymmetric_quantize_inputs; + + // Parameters for FullyConnected version 10 or above. + // Used to determine the default value for the quantized bias. + TfLiteType quantized_bias_type; +} TfLiteFullyConnectedParams; + +typedef enum { + kTfLiteLshProjectionUnknown = 0, + kTfLiteLshProjectionSparse = 1, + kTfLiteLshProjectionDense = 2, +} TfLiteLSHProjectionType; + +typedef struct { + TfLiteLSHProjectionType type; +} TfLiteLSHProjectionParams; + +typedef struct { + float beta; +} TfLiteSoftmaxParams; + +typedef struct { + int axis; + TfLiteFusedActivation activation; +} TfLiteConcatenationParams; + +typedef struct { + TfLiteFusedActivation activation; + // Parameter added for the version 4. + bool pot_scale_int16; +} TfLiteAddParams; + +typedef struct { + EmptyStructPlaceholder placeholder; +} TfLiteSpaceToBatchNDParams; + +typedef struct { + EmptyStructPlaceholder placeholder; +} TfLiteBatchToSpaceNDParams; + +typedef struct { + bool adj_x; + bool adj_y; + // Parameters for BatchMatMul version 4 or above. + // If set to true and the weights are quantized, then non constant inputs + // are quantized at evaluation time with asymmetric quantization. + bool asymmetric_quantize_inputs; +} TfLiteBatchMatMulParams; + +typedef struct { + TfLiteFusedActivation activation; +} TfLiteMulParams; + +typedef struct { + TfLiteFusedActivation activation; + // Parameter added for the version 5. + bool pot_scale_int16; +} TfLiteSubParams; + +typedef struct { + TfLiteFusedActivation activation; +} TfLiteDivParams; + +typedef struct { + TfLiteFusedActivation activation; +} TfLiteL2NormParams; + +typedef struct { + int radius; + float bias; + float alpha; + float beta; +} TfLiteLocalResponseNormParams; + +typedef enum { + kTfLiteLSTMFullKernel = 0, + kTfLiteLSTMBasicKernel +} TfLiteLSTMKernelType; + +typedef struct { + // Parameters for LSTM version 1. + TfLiteFusedActivation activation; + float cell_clip; + float proj_clip; + + // Parameters for LSTM version 2. + // kTfLiteLSTMBasicKernel is only supported in version 2 or above. + TfLiteLSTMKernelType kernel_type; + + // Parameters for LSTM version 4. + bool asymmetric_quantize_inputs; +} TfLiteLSTMParams; + +typedef struct { + // Parameters needed for the underlying LSTM. + TfLiteFusedActivation activation; + float cell_clip; + float proj_clip; + + // If set to true then the first dimension is time, otherwise batch. + bool time_major; + + // Parameter for unidirectional sequence RNN version 3. + bool asymmetric_quantize_inputs; + + // Parameter for unidirectional sequence RNN version 4. + bool diagonal_recurrent_tensors; +} TfLiteUnidirectionalSequenceLSTMParams; + +typedef struct { + // Parameters supported by version 1: + // Parameters inherited for the LSTM kernel. + TfLiteFusedActivation activation; + float cell_clip; + float proj_clip; + + // If true, store the outputs of both directions in the first output. + bool merge_outputs; + + // Parameters supported by version 2: + // If set to true then the first dimension is time, otherwise batch. + bool time_major; + + // Parameters supported by version 3: + // If set to true, then hybrid ops use asymmetric quantization for inputs. + bool asymmetric_quantize_inputs; +} TfLiteBidirectionalSequenceLSTMParams; + +typedef struct { + bool align_corners; + // half_pixel_centers assumes pixels are of half the actual dimensions, and + // yields more accurate resizes. Corresponds to the same argument for the + // original TensorFlow op in TF2.0. + bool half_pixel_centers; +} TfLiteResizeBilinearParams; + +typedef struct { + bool align_corners; + bool half_pixel_centers; +} TfLiteResizeNearestNeighborParams; + +typedef struct { + EmptyStructPlaceholder placeholder; +} TfLitePadParams; + +typedef struct { + EmptyStructPlaceholder placeholder; +} TfLitePadV2Params; + +typedef struct { + // These fields are only used in old models for backward compatibility. + // In the current implementation, we use the 2nd input of the op as the shape, + // and these fields are unused. + int32_t shape[TFLITE_RESHAPE_PARAMS_MAX_DIMENSION_COUNT]; + int num_dimensions; +} TfLiteReshapeParams; + +typedef struct { + int ngram_size; + int max_skip_size; + bool include_all_ngrams; +} TfLiteSkipGramParams; + +typedef struct { + int block_size; +} TfLiteSpaceToDepthParams; + +typedef struct { + int block_size; +} TfLiteDepthToSpaceParams; + +typedef struct { + TfLiteType in_data_type; + TfLiteType out_data_type; +} TfLiteCastParams; + +typedef enum { + kTfLiteCombinerTypeSum = 0, + kTfLiteCombinerTypeMean = 1, + kTfLiteCombinerTypeSqrtn = 2, +} TfLiteCombinerType; + +typedef struct { + TfLiteCombinerType combiner; +} TfLiteEmbeddingLookupSparseParams; + +typedef struct { + int axis; + int batch_dims; +} TfLiteGatherParams; + +typedef struct { + EmptyStructPlaceholder placeholder; +} TfLiteTransposeParams; + +typedef struct { + bool keep_dims; +} TfLiteReducerParams; + +typedef struct { + int num_splits; +} TfLiteSplitParams; + +typedef struct { + int num_splits; +} TfLiteSplitVParams; + +typedef struct { + // TODO(ahentz): We can't have dynamic data in this struct, at least not yet. + // For now we will fix the maximum possible number of dimensions. + int32_t squeeze_dims[8]; + int num_squeeze_dims; +} TfLiteSqueezeParams; + +typedef struct { + int begin_mask; + int end_mask; + int ellipsis_mask; + int new_axis_mask; + int shrink_axis_mask; + + // Parameters supported by version 8: + // If true, then the end tensor is an offset of the begin tensor. + bool offset; +} TfLiteStridedSliceParams; + +typedef struct { + TfLiteType output_type; +} TfLiteArgMaxParams; + +typedef struct { + TfLiteType output_type; +} TfLiteArgMinParams; + +typedef struct { + // Parameters supported by version 1: + TfLitePadding padding; + int stride_width; + int stride_height; + + // Parameters supported by version 4: + TfLiteFusedActivation activation; + + // Parameters for TransposeConv version 5 or above. + // Used to determine the default value for the quantized bias. + TfLiteType quantized_bias_type; +} TfLiteTransposeConvParams; + +typedef struct { + bool validate_indices; +} TfLiteSparseToDenseParams; + +typedef struct { + TfLiteType out_type; +} TfLiteShapeParams; + +typedef struct { + EmptyStructPlaceholder placeholder; +} TfLiteRankParams; + +typedef struct { + // Parameters supported by version 1: + float min; + float max; + int num_bits; + + // Parameters supported by version 2: + bool narrow_range; +} TfLiteFakeQuantParams; + +typedef struct { + int values_count; + int axis; +} TfLitePackParams; + +typedef struct { + int axis; +} TfLiteOneHotParams; + +typedef struct { + int num; + int axis; +} TfLiteUnpackParams; + +typedef struct { + float alpha; +} TfLiteLeakyReluParams; + +typedef struct { + TfLiteType index_out_type; +} TfLiteUniqueParams; + +typedef struct { + int seq_dim; + int batch_dim; +} TfLiteReverseSequenceParams; + +typedef struct { + EmptyStructPlaceholder placeholder; +} TfLiteMatrixDiagParams; + +typedef struct { + EmptyStructPlaceholder placeholder; +} TfLiteMatrixSetDiagParams; + +typedef struct { + int then_subgraph_index; + int else_subgraph_index; +} TfLiteIfParams; + +typedef struct { + int cond_subgraph_index; + int body_subgraph_index; +} TfLiteWhileParams; + +typedef struct { + bool exclusive; + bool reverse; +} TfLiteCumsumParams; + +typedef struct { + int init_subgraph_index; +} TfLiteCallOnceParams; + +typedef struct { + int table_id; + TfLiteType key_dtype; + TfLiteType value_dtype; +} TfLiteHashtableParams; + +typedef struct { + const char* container; + const char* shared_name; +} TfLiteVarHandleParams; + +typedef struct { + int seed; + int seed2; +} TfLiteRandomParams; + +typedef struct { + int num_boundaries; + // This points to the memory stored in the model (flatbuffer), + // and is not owned. + const float* boundaries; +} TfLiteBucketizeParams; + +typedef struct { + bool approximate; +} TfLiteGeluParams; + +typedef struct { + int64_t dimension; +} TfLiteStablehloConcatenateParams; + +typedef struct { + // See the stablehlo spec for the explanation of the attributes: + // https://github.com/openxla/stablehlo/blob/main/docs/spec.md#scatter + bool indices_are_sorted; + int64_t + update_window_dims[TFLITE_STABLEHLO_SCATTER_PARAMS_MAX_DIMENSION_COUNT]; + int num_update_window_dims; + int64_t + inserted_window_dims[TFLITE_STABLEHLO_SCATTER_PARAMS_MAX_DIMENSION_COUNT]; + int num_inserted_window_dims; + int64_t scatter_dims_to_operand_dims + [TFLITE_STABLEHLO_SCATTER_PARAMS_MAX_DIMENSION_COUNT]; + int num_scatter_dims_to_operand_dims; + int64_t index_vector_dim; + bool unique_indices; + int update_computation_subgraph_index; +} TfLiteStablehloScatterParams; + +typedef enum { + kTfLiteRngAlgorithmUnknown = 0, + // An algorithm auto-selected by the system according to device type. + kTfLiteRngAlgorithmDefault, + // The Philox algorithm, as described in paper + // ['Parallel Random Numbers: As Easy as 1, 2, 3'] + // (https://www.thesalmons.org/john/random123/papers/random123sc11.pdf) + kTfLiteRngAlgorithmPhilox, + // The ThreeFry algorithm, as described in paper + // ['Parallel Random Numbers: As Easy as 1, 2, 3'] + // (https://www.thesalmons.org/john/random123/papers/random123sc11.pdf) + kTfLiteRngAlgorithmThreefry, +} TfLiteRngAlgorithm; + +typedef struct { + TfLiteRngAlgorithm algorithm; +} TfLiteStablehloRngBitGeneratorParams; + +typedef struct { + // See the stablehlo spec for the explanation of the attributes: + // https://github.com/openxla/stablehlo/blob/main/docs/spec.md#gather + int64_t offset_dims[TFLITE_STABLEHLO_GATHER_PARAMS_MAX_DIMENSION_COUNT]; + int num_offset_dims; + int64_t + collapsed_slice_dims[TFLITE_STABLEHLO_GATHER_PARAMS_MAX_DIMENSION_COUNT]; + int num_collapsed_slice_dims; + int64_t start_index_map[TFLITE_STABLEHLO_GATHER_PARAMS_MAX_DIMENSION_COUNT]; + int num_start_index_map; + int64_t index_vector_dim; + int64_t slice_sizes[TFLITE_STABLEHLO_GATHER_PARAMS_MAX_DIMENSION_COUNT]; + int num_slice_sizes; + bool indices_are_sorted; +} TfLiteStablehloGatherParams; + +typedef struct { + // See the stablehlo spec for the explanation of the attributes: + // https://github.com/openxla/stablehlo/blob/main/docs/spec.md#reduce_window + int64_t window_dimensions + [TFLITE_STABLEHLO_REDUCE_WINDOW_PARAMS_MAX_DIMENSION_COUNT]; + int64_t + window_strides[TFLITE_STABLEHLO_REDUCE_WINDOW_PARAMS_MAX_DIMENSION_COUNT]; + int64_t + base_dilations[TFLITE_STABLEHLO_REDUCE_WINDOW_PARAMS_MAX_DIMENSION_COUNT]; + int64_t window_dilations + [TFLITE_STABLEHLO_REDUCE_WINDOW_PARAMS_MAX_DIMENSION_COUNT]; + int64_t + padding[2 * TFLITE_STABLEHLO_REDUCE_WINDOW_PARAMS_MAX_DIMENSION_COUNT]; + int body_subgraph_index; +} TfLiteStablehloReduceWindowParams; + +enum TfLiteReduceWindowFunction { + TfLiteReduceWindowFunctionUnsupported, + TfLiteReduceWindowFunctionAdd, + TfLiteReduceWindowFunctionMul, + TfLiteReduceWindowFunctionMin, + TfLiteReduceWindowFunctionMax, + TfLiteReduceWindowFunctionAll, + TfLiteReduceWindowFunctionAny +}; + +typedef struct { + enum TfLiteReduceWindowFunction reduce_function; +} TfLiteReduceWindowParams; + +typedef struct { + // See the stablehlo spec for the explanation of the attributes: + // https://github.com/openxla/stablehlo/blob/main/docs/spec.md#pad + int64_t edge_padding_low[TFLITE_STABLEHLO_PAD_PARAMS_MAX_DIMENSION_COUNT]; + int64_t edge_padding_high[TFLITE_STABLEHLO_PAD_PARAMS_MAX_DIMENSION_COUNT]; + int64_t interior_padding[TFLITE_STABLEHLO_PAD_PARAMS_MAX_DIMENSION_COUNT]; +} TfLiteStablehloPadParams; + +typedef struct { + const char* name; + int32_t subgraph_index; + int32_t version; + const uint8_t* attributes; + size_t attributes_size; +} TfLiteStablehloCompositeParams; + +typedef struct { + // See the stablehlo spec for the explanation of the attributes: + // https://github.com/openxla/stablehlo/blob/main/docs/spec.md#case + int32_t + branch_subgraph_indices[TFLITE_STABLEHLO_CASE_PARAMS_MAX_BRANCHES_COUNT]; + uint32_t num_branches; +} TfLiteStablehloCaseParams; + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_CORE_C_BUILTIN_OP_DATA_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/c/tflite_types.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/c/tflite_types.h new file mode 100644 index 0000000000000000000000000000000000000000..c3497bf80080c6460a4b0ea8177d8af22f419725 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/c/tflite_types.h @@ -0,0 +1,94 @@ +/* Copyright 2024 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. +==============================================================================*/ +// This file hosts data structures that are needed both for LiteRT and +// Compiler. + +// WARNING: Users of TensorFlow Lite should not include this file directly, but +// should instead include "third_party/tensorflow/lite/c/c_api_types.h". +// Only the TensorFlow Lite implementation itself should include this file +// directly. + +// clang-format off +// NOLINTBEGIN(whitespace/line_length) +/// \note Users of TensorFlow Lite should use +/// \code +/// #include "tensorflow/lite/c/c_api_types.h" +/// \endcode +/// to access the APIs documented on this page. +// NOLINTEND(whitespace/line_length) +// clang-format on + +// IWYU pragma: private, include "third_party/tensorflow/lite/c/c_api_types.h" + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_CORE_C_TFLITE_TYPES_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_CORE_C_TFLITE_TYPES_H_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/// Types supported by tensor +// LINT.IfChange +typedef enum { + kTfLiteNoType = 0, + kTfLiteFloat32 = 1, + kTfLiteInt32 = 2, + kTfLiteUInt8 = 3, + kTfLiteInt64 = 4, + kTfLiteString = 5, + kTfLiteBool = 6, + kTfLiteInt16 = 7, + kTfLiteComplex64 = 8, + kTfLiteInt8 = 9, + kTfLiteFloat16 = 10, + kTfLiteFloat64 = 11, + kTfLiteComplex128 = 12, + kTfLiteUInt64 = 13, + kTfLiteResource = 14, + kTfLiteVariant = 15, + kTfLiteUInt32 = 16, + kTfLiteUInt16 = 17, + kTfLiteInt4 = 18, + kTfLiteBFloat16 = 19, + kTfLiteInt2 = 20, + kTfLiteUInt4 = 21, + kTfLiteFloat8E4M3FN = 22, + kTfLiteFloat8E5M2 = 23, +} TfLiteType; +// LINT.ThenChange(//tensorflow/lite/profiling/proto/model_runtime_info.proto:EdgeDataType) + +/// Legacy. Will be deprecated in favor of `TfLiteAffineQuantization`. +/// If per-layer quantization is specified this field will still be populated in +/// addition to `TfLiteAffineQuantization`. +/// Parameters for asymmetric quantization. Quantized values can be converted +/// back to float using: `real_value = scale * (quantized_value - zero_point)` +typedef struct TfLiteQuantizationParams { + float scale; + int32_t zero_point; +} TfLiteQuantizationParams; + +/// Storage format of each dimension in a sparse tensor. +typedef enum TfLiteDimensionType { + kTfLiteDimDense = 0, + kTfLiteDimSparseCSR, +} TfLiteDimensionType; + +#ifdef __cplusplus +} // extern C +#endif + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_CORE_C_TFLITE_TYPES_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/macros.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/macros.h new file mode 100644 index 0000000000000000000000000000000000000000..3cdc04852a90de9b078cde0d8433064e1174f37d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/macros.h @@ -0,0 +1,50 @@ +/* Copyright 2024 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. +==============================================================================*/ +// This provides utility macros and functions that are inherently platform +// specific or shared across runtime & converter. +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_CORE_MACROS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_CORE_MACROS_H_ + +#ifndef TF_LITE_STATIC_MEMORY +// maximum size of a valid flatbuffer +inline constexpr unsigned int flatbuffer_size_max = 2147483648; +// If none zero then the buffer is stored outside of the flatbuffers, string +inline constexpr char tflite_metadata_buffer_location[] = "buffer_location"; +// field for minimum runtime version, string +inline constexpr char tflite_metadata_min_runtime_version[] = + "min_runtime_version"; +// the stablehlo op version is supported by the tflite runtime +inline constexpr char tflite_supported_stablehlo_version[] = "1.0.0"; +#endif + +// LINT.IfChange(TFLITE_NOINLINE) + +#ifdef _WIN32 +#define TFLITE_NOINLINE __declspec(noinline) +#else +#ifdef __has_attribute +#if __has_attribute(noinline) +#define TFLITE_NOINLINE __attribute__((noinline)) +#else +#define TFLITE_NOINLINE +#endif // __has_attribute(noinline) +#else +#define TFLITE_NOINLINE +#endif // __has_attribute +#endif // _WIN32 + +// LINT.ThenChange(//tensorflow/lite/core/macros.h) + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_CORE_MACROS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/model_builder_base.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/model_builder_base.cc new file mode 100644 index 0000000000000000000000000000000000000000..fc546c794ffa8b7e769256fa7d8dd51fda3ddb06 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/model_builder_base.cc @@ -0,0 +1,62 @@ +/* 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/compiler/mlir/lite/core/model_builder_base.h" + +#include + +#include +#include + +#include "absl/strings/str_cat.h" +#include "tensorflow/compiler/mlir/lite/allocation.h" +#include "tensorflow/compiler/mlir/lite/core/api/error_reporter.h" + +namespace tflite { + +#ifndef TFLITE_MCU +// Loads a model from `filename`. If `mmap_file` is true then use mmap, +// otherwise make a copy of the model in a buffer. +std::unique_ptr GetAllocationFromFile(const char* filename, + ErrorReporter* error_reporter, + bool allow_modifications) { + std::unique_ptr allocation; + if (MMAPAllocation::IsSupported()) { + allocation = std::make_unique(filename, error_reporter, + allow_modifications); + } else { + allocation = std::make_unique(filename, error_reporter); + } + return allocation; +} + +// Loads a model from `fd`. If `mmap_file` is true then use mmap, +// otherwise make a copy of the model in a buffer. +std::unique_ptr GetAllocationFromFile(int fd, + ErrorReporter* error_reporter, + bool allow_modifications) { + std::unique_ptr allocation; + if (MMAPAllocation::IsSupported()) { + allocation = std::make_unique(fd, error_reporter, + allow_modifications); + } else { + allocation = std::make_unique( + absl::StrCat("/proc/self/fd/", fd).c_str(), error_reporter); + } + return allocation; +} + +#endif + +} // namespace tflite diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/model_builder_base.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/model_builder_base.h new file mode 100644 index 0000000000000000000000000000000000000000..5d80563826d94f7b09227b20331612c73bb7262c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/model_builder_base.h @@ -0,0 +1,627 @@ +/* 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. +==============================================================================*/ +/// \file +/// +/// Deserialization infrastructure for tflite. Provides functionality +/// to go from a serialized tflite model in flatbuffer format to an +/// in-memory representation of the model. +/// +/// WARNING: Users of TensorFlow Lite should not include this file directly, +/// but should instead include "third_party/tensorflow/lite/model_builder.h". +/// Only the TensorFlow Lite implementation itself should include this +/// file directly. +// IWYU pragma: private, include "third_party/tensorflow/lite/model_builder.h" + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_CORE_MODEL_BUILDER_BASE_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_CORE_MODEL_BUILDER_BASE_H_ + +#include + +#include +#include +#include +#include +#include +#include + +#include "flatbuffers/base.h" // from @flatbuffers +#include "flatbuffers/buffer.h" // from @flatbuffers +#include "flatbuffers/vector.h" // from @flatbuffers +#include "flatbuffers/verifier.h" // from @flatbuffers +#include "tensorflow/compiler/mlir/lite/allocation.h" +#include "tensorflow/compiler/mlir/lite/core/api/error_reporter.h" +#include "tensorflow/compiler/mlir/lite/core/api/verifier.h" +#include "tensorflow/compiler/mlir/lite/core/macros.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" + +namespace tflite { + +std::unique_ptr GetAllocationFromFile( + const char* filename, ErrorReporter* error_reporter, + bool allow_modifications = false); + +std::unique_ptr GetAllocationFromFile( + int fd, ErrorReporter* error_reporter, bool allow_modifications = false); + +namespace impl { + +/// An RAII object that represents a read-only tflite model, copied from disk, +/// or mmapped. This uses flatbuffers as the serialization format. +/// +/// NOTE: The current API requires that a FlatBufferModelBase instance be kept +/// alive by the client as long as it is in use by any dependent Interpreter +/// instances. As the FlatBufferModelBase instance is effectively immutable +/// after creation, the client may safely use a single model with multiple +/// dependent Interpreter instances, even across multiple threads (though note +/// that each Interpreter instance is *not* thread-safe). +/// +///

+/// using namespace tflite;
+/// StderrReporter error_reporter;
+/// auto model = FlatBufferModelBase::BuildFromFile("interesting_model.tflite",
+///                                             &error_reporter);
+/// MyOpResolver resolver;  // You need to subclass OpResolver to provide
+///                         // implementations.
+/// InterpreterBuilder builder(*model, resolver);
+/// std::unique_ptr interpreter;
+/// if(builder(&interpreter) == kTfLiteOk) {
+///   .. run model inference with interpreter
+/// }
+/// 
+/// +/// OpResolver must be defined to provide your kernel implementations to the +/// interpreter. This is environment specific and may consist of just the +/// builtin ops, or some custom operators you defined to extend tflite. +template +class FlatBufferModelBase { + public: + /// Builds a model based on a file. + /// Caller retains ownership of `error_reporter` and must ensure its lifetime + /// is longer than the FlatBufferModelBase instance. + /// Returns a nullptr in case of failure. + static std::unique_ptr BuildFromFile( + const char* filename, + ErrorReporter* error_reporter = T::GetDefaultErrorReporter()) { + error_reporter = ValidateErrorReporter(error_reporter); + std::unique_ptr model = BuildFromAllocation( + GetAllocationFromFile(filename, error_reporter), error_reporter); +#if FLATBUFFERS_LITTLEENDIAN == 1 + return model; +#else + return ByteConvertModel(std::move(model), error_reporter); +#endif + } + + /// Verifies whether the content of the file is legit, then builds a model + /// based on the file. + /// The extra_verifier argument is an additional optional verifier for the + /// file contents. By default, we always check with tflite::VerifyModelBuffer. + /// If extra_verifier is supplied, the file contents is also checked against + /// the extra_verifier after the check against tflite::VerifyModelBuilder. + /// Caller retains ownership of `error_reporter` and must ensure its lifetime + /// is longer than the FlatBufferModelBase instance. + /// Returns a nullptr in case of failure. + static std::unique_ptr VerifyAndBuildFromFile( + const char* filename, TfLiteVerifier* extra_verifier = nullptr, + ErrorReporter* error_reporter = T::GetDefaultErrorReporter()) { + error_reporter = ValidateErrorReporter(error_reporter); + std::unique_ptr model = VerifyAndBuildFromAllocation( + GetAllocationFromFile(filename, error_reporter), extra_verifier, + error_reporter); +#if FLATBUFFERS_LITTLEENDIAN == 1 + return model; +#else + return ByteConvertModel(std::move(model), error_reporter); +#endif + } + + /// Builds a model based on a file descriptor. + /// Caller retains ownership of `error_reporter` and must ensure its lifetime + /// is longer than the FlatBufferModelBase instance. Caller retains ownership + /// of `fd` and must ensure it is closed after BuildFromFile returns. Returns + /// a nullptr in case of failure. + static std::unique_ptr BuildFromFileDescriptor( + int fd, ErrorReporter* error_reporter = T::GetDefaultErrorReporter()) { + error_reporter = ValidateErrorReporter(error_reporter); + std::unique_ptr model = BuildFromAllocation( + GetAllocationFromFile(fd, error_reporter), error_reporter); +#if FLATBUFFERS_LITTLEENDIAN == 1 + return model; +#else + return ByteConvertModel(std::move(model), error_reporter); +#endif + } + + /// Verifies whether the content of the file descriptor is legit, then builds + /// a model based on the file. + /// The extra_verifier argument is an additional optional verifier for the + /// file contents. By default, we always check with tflite::VerifyModelBuffer. + /// If extra_verifier is supplied, the file contents is also checked against + /// the extra_verifier after the check against tflite::VerifyModelBuilder. + /// Caller retains ownership of `error_reporter` and must ensure its lifetime + /// is longer than the FlatBufferModelBase instance. + /// Returns a nullptr in case of failure. + static std::unique_ptr VerifyAndBuildFromFileDescriptor( + int fd, TfLiteVerifier* extra_verifier = nullptr, + ErrorReporter* error_reporter = T::GetDefaultErrorReporter()) { + error_reporter = ValidateErrorReporter(error_reporter); + std::unique_ptr> model = + VerifyAndBuildFromAllocation(GetAllocationFromFile(fd, error_reporter), + extra_verifier, error_reporter); +#if FLATBUFFERS_LITTLEENDIAN == 1 + return model; +#else + return ByteConvertModel(std::move(model), error_reporter); +#endif + } + + /// Builds a model based on a pre-loaded flatbuffer. + /// Caller retains ownership of the buffer and should keep it alive until + /// the returned object is destroyed. Caller also retains ownership of + /// `error_reporter` and must ensure its lifetime is longer than the + /// FlatBufferModelBase instance. + /// Returns a nullptr in case of failure. + /// NOTE: this validates the base Flatbuffer buffer structure. Use + /// VerifyAndBuildFromBuffer if an additional TfLiteVerifier check is + /// required. + static std::unique_ptr BuildFromBuffer( + const char* caller_owned_buffer, size_t buffer_size, + ErrorReporter* error_reporter = T::GetDefaultErrorReporter()) { + error_reporter = ValidateErrorReporter(error_reporter); + std::unique_ptr allocation( + new MemoryAllocation(caller_owned_buffer, buffer_size, error_reporter)); + return BuildFromAllocation(std::move(allocation), error_reporter); + } + + /// Verifies whether the content of the buffer is legit, then builds a model + /// based on the pre-loaded flatbuffer. + /// The extra_verifier argument is an additional optional verifier for the + /// buffer. By default, we always check with tflite::VerifyModelBuffer. If + /// extra_verifier is supplied, the buffer is checked against the + /// extra_verifier after the check against tflite::VerifyModelBuilder. The + /// caller retains ownership of the buffer and should keep it alive until the + /// returned object is destroyed. Caller retains ownership of `error_reporter` + /// and must ensure its lifetime is longer than the FlatBufferModelBase + /// instance. Returns a nullptr in case of failure. + static std::unique_ptr VerifyAndBuildFromBuffer( + const char* caller_owned_buffer, size_t buffer_size, + TfLiteVerifier* extra_verifier = nullptr, + ErrorReporter* error_reporter = T::GetDefaultErrorReporter()) { + error_reporter = ValidateErrorReporter(error_reporter); + std::unique_ptr allocation( + new MemoryAllocation(caller_owned_buffer, buffer_size, error_reporter)); + return VerifyAndBuildFromAllocation(std::move(allocation), extra_verifier, + error_reporter); + } + +#if FLATBUFFERS_LITTLEENDIAN == 0 + + static void ByteSwapSerializedModel(std::string* serialized_model, + bool from_big_endian) { + const uint8_t* buffer = + reinterpret_cast(serialized_model->c_str()); + const tflite::Model* input_model = tflite::GetModel(buffer); + ByteSwapTFLiteModel(input_model, from_big_endian); + } + + static void ByteSwapBuffer(int8_t tensor_type, size_t buffer_size, + uint8_t* buffer, bool from_big_endian) { + switch (tensor_type) { + case tflite::TensorType_STRING: { + auto bp = reinterpret_cast(buffer); + int num_of_strings = + from_big_endian ? bp[0] : flatbuffers::EndianSwap(bp[0]); + for (int i = 0; i < num_of_strings + 2; i++) + bp[i] = flatbuffers::EndianSwap(bp[i]); + break; + } + // 16-bit types + case tflite::TensorType_FLOAT16: + case tflite::TensorType_INT16: + case tflite::TensorType_UINT16: { + auto bp = reinterpret_cast(buffer); + for (int i = 0; i < buffer_size / 2; i++) + bp[i] = flatbuffers::EndianSwap(bp[i]); + break; + } + // 32-bit types + case tflite::TensorType_FLOAT32: + case tflite::TensorType_INT32: + case tflite::TensorType_UINT32: + case tflite::TensorType_COMPLEX64: { + auto bp = reinterpret_cast(buffer); + for (int i = 0; i < buffer_size / 4; i++) + bp[i] = flatbuffers::EndianSwap(bp[i]); + break; + } + // 64-bit types + case tflite::TensorType_INT64: + case tflite::TensorType_FLOAT64: + case tflite::TensorType_UINT64: + case tflite::TensorType_COMPLEX128: { + auto bp = reinterpret_cast(buffer); + for (int i = 0; i < buffer_size / 8; i++) + bp[i] = flatbuffers::EndianSwap(bp[i]); + break; + } + default: + break; + } + } + + static void ByteSwapTFLiteModel(const tflite::Model* tfl_model, + bool from_big_endian) { + std::vector buffer_swapped(tfl_model->buffers()->size(), false); + for (size_t subgraph_idx = 0; subgraph_idx < tfl_model->subgraphs()->size(); + subgraph_idx++) { + const tflite::SubGraph* subgraph = + tfl_model->subgraphs()->Get(subgraph_idx); + for (size_t ts_idx = 0; ts_idx < subgraph->tensors()->size(); ts_idx++) { + const tflite::Tensor* tensor = subgraph->tensors()->Get(ts_idx); + if (tensor->buffer() > 0 && + tensor->buffer() < tfl_model->buffers()->size() && + !buffer_swapped[tensor->buffer()]) { + const tflite::Buffer* buffer_ = + (*tfl_model->buffers())[tensor->buffer()]; + if (!buffer_ || !buffer_->data()) continue; + auto* buffer = buffer_->data(); + uint8_t* buff_ = const_cast(buffer->data()); + ByteSwapBuffer(tensor->type(), buffer->size(), buff_, + from_big_endian); + buffer_swapped[tensor->buffer()] = true; + } + } + } + } + + static std::unique_ptr ByteConvertModel(std::unique_ptr model, + ErrorReporter* error_reporter, + bool from_big_endian = false) { + if (model == nullptr) return model; + auto tfl_model = model->GetModel(); + if (tfl_model->subgraphs()->size() == 0) return model; + if (tfl_model->subgraphs()->Get(0)->tensors()->size() == 0) return model; + if (tfl_model->buffers()->size() < 2) return model; + return ByteSwapFlatBufferModelBase(std::move(model), error_reporter, + from_big_endian); + } + + static std::unique_ptr ByteSwapFlatBufferModelBase( + std::unique_ptr model, ErrorReporter* error_reporter, + bool from_big_endian) { + FlatBufferModelBase* modelp = model.release(); + auto tflite_model = modelp->GetModel(); + auto copied_model = std::make_unique(); + tflite_model->UnPackTo(copied_model.get(), nullptr); + ByteSwapTFLiteModelT(copied_model.get(), from_big_endian); + std::unique_ptr builder( + new flatbuffers::FlatBufferBuilder()); + auto packed_model = tflite::Model::Pack(*builder, copied_model.get()); + tflite::FinishModelBuffer(*builder, packed_model); + flatbuffers::FlatBufferBuilder* builder_ = builder.release(); + return BuildFromBuffer( + reinterpret_cast(builder_->GetBufferPointer()), + builder_->GetSize(), error_reporter); + } + + static void ByteSwapTFLiteModelT(tflite::ModelT* tfl_modelt, + bool from_big_endian) { + size_t bytes_per_elem = 0; + std::vector buffer_swapped(tfl_modelt->buffers.size(), false); + for (size_t subgraph_idx = 0; subgraph_idx < tfl_modelt->subgraphs.size(); + subgraph_idx++) { + tflite::SubGraphT* subgraph = + tfl_modelt->subgraphs.at(subgraph_idx).get(); + for (size_t ts_idx = 0; ts_idx < subgraph->tensors.size(); ts_idx++) { + tflite::TensorT* tensor = subgraph->tensors[ts_idx].get(); + if (tensor->buffer > 0 && tensor->buffer < tfl_modelt->buffers.size() && + !buffer_swapped[tensor->buffer]) { + const auto* buffer = + &(tfl_modelt->buffers[tensor->buffer].get()->data); + if (buffer && buffer->data()) { + uint8_t* buff_ = const_cast(buffer->data()); + ByteSwapBuffer(tensor->type, buffer->size(), buff_, + from_big_endian); + buffer_swapped[tensor->buffer] = true; + } + } + } + } + } + +#endif + + private: + // Internal helper to build a model from an allocation without running base + // verification. + static std::unique_ptr BuildFromAllocationInternal( + std::unique_ptr allocation, ErrorReporter* error_reporter) { + std::unique_ptr model(new T(std::move(allocation), error_reporter)); + if (!model->initialized()) { + model.reset(); + } else { + model->ValidateModelBuffers(model->error_reporter()); + } + return model; + } + + public: + /// Builds a model directly from an allocation. + /// Ownership of the allocation is passed to the model, but the caller + /// retains ownership of `error_reporter` and must ensure its lifetime is + /// longer than the FlatBufferModelBase instance. + /// Returns a nullptr in case of failure (e.g., the allocation is invalid). + static std::unique_ptr BuildFromAllocation( + std::unique_ptr allocation, + ErrorReporter* error_reporter = T::GetDefaultErrorReporter()) { + return VerifyAndBuildFromAllocation(std::move(allocation), + /*extra_verifier=*/nullptr, + error_reporter); + } + + /// Verifies whether the content of the allocation is legit, then builds a + /// model based on the provided allocation. + /// The extra_verifier argument is an additional optional verifier for the + /// buffer. By default, we always check with tflite::VerifyModelBuffer. If + /// extra_verifier is supplied, the buffer is checked against the + /// extra_verifier after the check against tflite::VerifyModelBuilder. + /// Ownership of the allocation is passed to the model, but the caller + /// retains ownership of `error_reporter` and must ensure its lifetime is + /// longer than the FlatBufferModelBase instance. + /// Returns a nullptr in case of failure. + static std::unique_ptr VerifyAndBuildFromAllocation( + std::unique_ptr allocation, + TfLiteVerifier* extra_verifier = nullptr, + ErrorReporter* error_reporter = T::GetDefaultErrorReporter()) { + error_reporter = ValidateErrorReporter(error_reporter); + if (!allocation || !allocation->valid()) { + TF_LITE_REPORT_ERROR(error_reporter, + "The model allocation is null/empty"); + return nullptr; + } + + { + // Flatbuffers can only be smaller than 2GB. The file format appends some + // data after the actual flabuffer. We truncate the allocation size to 2GB + // so that the verifier doesn't early exit on us. + size_t allocation_size = + std::min(allocation->bytes(), + static_cast(FLATBUFFERS_MAX_BUFFER_SIZE - 1)); + flatbuffers::Verifier base_verifier( + reinterpret_cast(allocation->base()), allocation_size, + flatbuffers::Verifier::Options()); + if (!VerifyModelBuffer(base_verifier)) { + TF_LITE_REPORT_ERROR(error_reporter, + "The model is not a valid Flatbuffer buffer"); + return nullptr; + } + + if (extra_verifier && + !extra_verifier->Verify(static_cast(allocation->base()), + allocation_size, error_reporter)) { + // The verifier will have already logged an appropriate error message. + return nullptr; + } + } + + return BuildFromAllocationInternal(std::move(allocation), error_reporter); + } + + /// Builds a model directly from a flatbuffer pointer + /// Caller retains ownership of the buffer and should keep it alive until the + /// returned object is destroyed. Caller retains ownership of `error_reporter` + /// and must ensure its lifetime is longer than the FlatBufferModelBase + /// instance. Returns a nullptr in case of failure. + static std::unique_ptr BuildFromModel( + const tflite::Model* caller_owned_model_spec, + ErrorReporter* error_reporter = T::GetDefaultErrorReporter()) { + error_reporter = ValidateErrorReporter(error_reporter); + + if (CheckBufferOutsideModel(caller_owned_model_spec)) { + TF_LITE_REPORT_ERROR(error_reporter, + "The model contains weights not accessible from " + "tflite::Model *, please use other api"); + return nullptr; + } + + std::unique_ptr model(new T(caller_owned_model_spec, error_reporter)); + if (!model->initialized()) { + model.reset(); + } else { + model->ValidateModelBuffers(error_reporter); + } + return model; + } + + // Releases memory or unmaps mmaped memory. + ~FlatBufferModelBase() = default; + + // Copying or assignment is disallowed to simplify ownership semantics. + FlatBufferModelBase(const FlatBufferModelBase&) = delete; + FlatBufferModelBase& operator=(const FlatBufferModelBase&) = delete; + + bool initialized() const { return model_ != nullptr; } + const tflite::Model* operator->() const { return model_; } + const tflite::Model* GetModel() const { return model_; } + ErrorReporter* error_reporter() const { return error_reporter_; } + const Allocation* allocation() const { return allocation_.get(); } + + // Returns the minimum runtime version from the flatbuffer. This runtime + // version encodes the minimum required interpreter version to run the + // flatbuffer model. If the minimum version can't be determined, an empty + // string will be returned. + // Note that the returned minimum version is a lower-bound but not a strict + // lower-bound; ops in the graph may not have an associated runtime version, + // in which case the actual required runtime might be greater than the + // reported minimum. + std::string GetMinimumRuntime() const { + if (!model_ || !model_->metadata()) return ""; + + for (int i = 0; i < model_->metadata()->size(); ++i) { + auto metadata = model_->metadata()->Get(i); + if (metadata->name()->str() == tflite_metadata_min_runtime_version) { + auto buf = metadata->buffer(); + auto* buffer = (*model_->buffers())[buf]; + auto* array = buffer->data(); + // Get the real length of the runtime string, since there might be + // trailing + // '\0's in the buffer. + for (int len = 0; len < array->size(); ++len) { + if (array->data()[len] == '\0') { + return std::string(reinterpret_cast(array->data()), + len); + } + } + // If there is no '\0' in the buffer, this indicates that the flatbuffer + // is malformed. + TF_LITE_REPORT_ERROR( + error_reporter_, + "Min_runtime_version in model metadata is malformed"); + break; + } + } + return ""; + } + + // Return model metadata as a mapping of name & buffer strings. + // See Metadata table in TFLite schema. + std::map ReadAllMetadata() const { + return ReadAllMetadata(model_); + } + + // // Return model metadata as a mapping of name & buffer strings. + // // See Metadata table in TFLite schema. + static std::map ReadAllMetadata( + const ::tflite::Model* model) { + std::map keys_values; + if (!model || !model->metadata() || !model->buffers()) return keys_values; + + for (size_t i = 0; i < model->metadata()->size(); ++i) { + auto metadata = model->metadata()->Get(i); + auto buf = metadata->buffer(); + if (buf >= model->buffers()->size()) continue; + const tflite::Buffer* buffer = (*model->buffers())[buf]; + if (!buffer || !buffer->data()) continue; + const flatbuffers::Vector* array = buffer->data(); + if (!array) continue; + std::string val = std::string( + reinterpret_cast(array->data()), array->size()); + // Skip if key or value of metadata is empty. + if (!metadata->name() || val.empty()) continue; + keys_values[metadata->name()->str()] = val; + } + return keys_values; + } + + // Validates if the FlatBufferModelBase's buffer is well-formed. Specifically, + // it checks if the 0th entry of the model buffers is an empty buffer + // (sentinel). This is a convention so that tensors without a buffer can + // provide 0 as their buffer. NOTE: The function doesn't explicitly fail for + // backward compatibility reasons; it just provides a warning in case of + // failures. + void ValidateModelBuffers(ErrorReporter* error_reporter) { + auto buffers = model_->buffers(); + if (buffers && !buffers->empty()) { + auto first_buffer = buffers->Get(0); + if (first_buffer && first_buffer->size() != 0) { + // Note the 0th entry of this array must be an empty buffer (sentinel). + // This is a convention so that tensors without a buffer can provide 0 + // as their buffer. + TF_LITE_REPORT_ERROR( + error_reporter, + "The 0th entry of the model buffer must be an empty buffer."); + } + } + } + + /// Returns true if the model identifier is correct (otherwise false and + /// reports an error). + bool CheckModelIdentifier() const { + if (allocation_->bytes() < 7) { + TF_LITE_REPORT_ERROR( + error_reporter_, + "Model provided must have at least 7 bytes to hold identifier.\n"); + return false; + } + if (!tflite::ModelBufferHasIdentifier(allocation_->base())) { + const char* ident = flatbuffers::GetBufferIdentifier(allocation_->base()); + // Suppress unused variable warning. + (void)ident; + TF_LITE_REPORT_ERROR( + error_reporter_, + "Model provided has model identifier '%c%c%c%c', should be '%s'\n", + ident[0], ident[1], ident[2], ident[3], tflite::ModelIdentifier()); + return false; + } + return true; + } + + /// Check If the buffer is stored as part of the Flatbuffer or outside + /// Return false if the buffers are part of the Flatbuffer + static bool CheckBufferOutsideModel(const tflite::Model* model) { + if (!model || !model->metadata()) return false; + + for (int i = 0; i < model->metadata()->size(); ++i) { + auto metadata = model->metadata()->Get(i); + if (metadata->name()->str() == tflite_metadata_buffer_location) { + return true; + } + } + return false; + } + + protected: + /// Loads a model from a given allocation. FlatBufferModelBase will take over + /// the ownership of `allocation`, and delete it in destructor. The ownership + /// of `error_reporter`remains with the caller and must have lifetime at least + /// as much as FlatBufferModelBase. This is to allow multiple models to use + /// the same ErrorReporter instance. + explicit FlatBufferModelBase( + std::unique_ptr allocation, + ErrorReporter* error_reporter = T::GetDefaultErrorReporter()) + : error_reporter_(ValidateErrorReporter(error_reporter)), + allocation_(std::move(allocation)) { + if (!allocation_ || !allocation_->valid() || !CheckModelIdentifier()) { + return; + } + + model_ = ::tflite::GetModel(allocation_->base()); + } + + /// Loads a model from Model flatbuffer. The `model` has to remain alive and + /// unchanged until the end of this flatbuffer model's lifetime. + FlatBufferModelBase(const Model* model, ErrorReporter* error_reporter) + : model_(model), error_reporter_(ValidateErrorReporter(error_reporter)) {} + + static ErrorReporter* ValidateErrorReporter(ErrorReporter* error_reporter) { + return error_reporter ? error_reporter : T::GetDefaultErrorReporter(); + } + + /// Flatbuffer traverser pointer. (Model* is a pointer that is within the + /// allocated memory of the data allocated by allocation's internals. + const tflite::Model* model_ = nullptr; + /// The error reporter to use for model errors and subsequent errors when + /// the interpreter is created + ErrorReporter* error_reporter_; + /// The allocator used for holding memory of the model. Note that this will + /// be null if the client provides a tflite::Model directly. + std::unique_ptr allocation_; +}; + +} // namespace impl + +} // namespace tflite + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_CORE_MODEL_BUILDER_BASE_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/special_rules.bzl b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/special_rules.bzl new file mode 100644 index 0000000000000000000000000000000000000000..cb300ec1c3d4d3ea20502af4350c72b8ea5320ea --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/core/special_rules.bzl @@ -0,0 +1,19 @@ +# Copyright 2022 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. +# ============================================================================== +"""Build macros for C++ headers.""" + +def macros_visibility_allowlist(): + """Returns a list of packages that can depend on macros.h.""" + return [] diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/debug/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/debug/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..99c9005c994789b38863618b14445aada20ff9f3 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/debug/BUILD @@ -0,0 +1,69 @@ +load("@rules_cc//cc:cc_library.bzl", "cc_library") +load("//tensorflow:tensorflow.bzl", "tf_cc_test") +load( + "//tensorflow/core/platform:build_config.bzl", + "tf_proto_library", +) + +# copybara:uncomment package(default_applicable_licenses = ["//tensorflow:LICENSE"]) + +# OSS only: This target is header-only. Link `debug_options_proto_cc_impl` only to +# `libtensorflow_framework.so` via `lib_internal_impl`. Do NOT link `debug_options_proto_cc_impl` +# directly unless the target does not link `libtensorflow_framework.so`. +tf_proto_library( + name = "debug_options_proto", + srcs = ["debug_options.proto"], + make_default_target_header_only = True, + visibility = ["//visibility:public"], +) + +cc_library( + name = "debug", + srcs = ["debug.cc"], + hdrs = ["debug.h"], + visibility = ["//tensorflow/compiler/mlir/lite:__subpackages__"], + deps = [ + ":debug_options_proto_cc", + "//tensorflow/compiler/mlir/lite/metrics:error_collector_inst", + "//tensorflow/core:portable_gif_internal", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/log", + "@com_google_absl//absl/status", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/time", + "@com_googlesource_code_re2//:re2", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:Transforms", + "@tsl//tsl/platform:path", + "@tsl//tsl/platform:stringpiece", + "@xla//xla/tsl/lib/io:buffered_file", + "@xla//xla/tsl/platform:env", + ], +) + +tf_cc_test( + name = "debug_test", + srcs = ["debug_test.cc"], + deps = [ + ":debug", + ":debug_options_proto_cc", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/core:portable_gif_internal", + "@com_google_absl//absl/status", + "@com_google_absl//absl/strings", + "@com_google_googletest//:gtest_main", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + "@tsl//tsl/platform:path", + "@xla//xla/tsl/lib/core:status_test_util", + "@xla//xla/tsl/platform:env", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/debug/debug.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/debug/debug.cc new file mode 100644 index 0000000000000000000000000000000000000000..0c08b08451c2d6f19d6f3a88b01c49fd6f82b2c8 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/debug/debug.cc @@ -0,0 +1,358 @@ +/* Copyright 2023 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/compiler/mlir/lite/debug/debug.h" + +#include +#include + +#include +#include +#include +#include +#include + +#include "absl/container/flat_hash_set.h" +#include "absl/log/log.h" +#include "absl/status/status.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/str_join.h" +#include "absl/time/clock.h" +#include "absl/time/time.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/raw_ostream.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/OperationSupport.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassInstrumentation.h" // from @llvm-project +#include "mlir/Pass/PassManager.h" // from @llvm-project +#include "mlir/Support/FileUtilities.h" // from @llvm-project +#include "mlir/Transforms/ViewOpGraph.h" // from @llvm-project +#include "re2/re2.h" // IWYU pragma: keep +#include "tensorflow/compiler/mlir/lite/debug/debug_options.pb.h" +#include "tensorflow/compiler/mlir/lite/metrics/error_collector_inst.h" +#include "xla/tsl/lib/io/buffered_file.h" +#include "xla/tsl/platform/env.h" +#include "xla/tsl/platform/file_system.h" +#include "tensorflow/core/platform/logging.h" +#include "tsl/platform/path.h" +#include "tsl/platform/stringpiece.h" + +// IWYU pragma: no_include "util/regexp/re2/re2.h" + +namespace tensorflow { +namespace { + +// Simple raw_ostream that prints to a file. +struct WritableFileRawStream : public llvm::raw_ostream { + explicit WritableFileRawStream(std::unique_ptr file) + : file(std::move(file)) { + SetUnbuffered(); + } + ~WritableFileRawStream() override = default; + + uint64_t current_pos() const override { + int64_t position; + if (file->Tell(&position).ok()) { + return position; + } else { + // MLIR uses os.tell() to determine whether something was written by + // a subroutine or not, so it's important we have a working current_pos(). + LOG(WARNING) + << "Couldn't query file position. Stream might be malformed.\n"; + return -1; + } + } + + void write_impl(const char* ptr, size_t size) override { + // Write the file if it is still valid. If the write fails, null out the + // file to avoid encountering another error. + if (file && !file->Append(absl::string_view(ptr, size)).ok()) { + file = nullptr; + } + } + + // The file being written to. + std::unique_ptr file; +}; + +// Reproducer stream that emits a reproducer to the given `llvm::raw_ostream`. +class ReproducerStream : public mlir::ReproducerStream { + public: + ReproducerStream(std::string name, std::unique_ptr os) + : name_(std::move(name)), os_(std::move(os)) {} + + llvm::StringRef description() override { return name_; } + + llvm::raw_ostream& os() override { return *os_; } + + private: + std::string name_; + std::unique_ptr os_; +}; + +// Returns a function that builds a reproducer stream, or nullptr if the MLIR +// reproducer will not be enabled. +mlir::ReproducerStreamFactory GetReproducerStreamFactory( + absl::string_view dump_dir) { + std::string path = tsl::io::JoinPath(dump_dir, "tfl_mlir_crash_repro.mlir"); + + return [path = std::move(path)]( + std::string& error) -> std::unique_ptr { + std::unique_ptr file; + if (auto status = tsl::Env::Default()->NewWritableFile(path, &file); + !status.ok()) { + error = status.ToString(); + absl::StrAppend(&error, "; failed to open '", path, + "' for writing an MLIR reproducer"); + return nullptr; + } + file = std::make_unique(std::move(file)); + + return std::make_unique( + path, std::make_unique(std::move(file))); + }; +} + +// Removes unwanted characters for readability and to eliminate issues when +// saving a file. +std::string Sanitize(absl::string_view string) { + static const auto& kUnwantedChars = *new absl::flat_hash_set{ + '<', '>', ':', '\"', '/', '\\', '|', '?', '*', ' ', '(', ')'}; + + std::string sanitized; + sanitized.reserve(string.size()); + + bool skip = false; + for (const char& c : string) { + if (auto it = kUnwantedChars.find(c); it != kUnwantedChars.end()) { + skip = true; + continue; + } + if (skip) { + skip = false; + sanitized.push_back('_'); + } + sanitized.push_back(c); + } + + return sanitized; +} + +// Pass instrumentation that dumps MLIR based on the criteria specified by +// `ir_dump_*` debug options. +// +// While `mlir::PassManager::enableIRPrinting` provides a similar functionality, +// it is cumbersome to manually copy printed IRs and run them with `litert-opt`. +// Also, long MLIR dumps are often truncated during printing. Instead, this +// instrumentation dumps MLIR to external directories for convenience. +class DumpInstrumentation : public mlir::PassInstrumentation { + public: + explicit DumpInstrumentation(absl::string_view dump_dir, + absl::string_view dump_pass_regex, + absl::string_view dump_func_regex) + : dump_dir_(dump_dir), + dump_pass_re_(std::make_unique(dump_pass_regex)), + dump_func_re_(std::make_unique(dump_func_regex)) {} + + DumpInstrumentation(const DumpInstrumentation& other) = delete; + DumpInstrumentation& operator=(const DumpInstrumentation& other) = delete; + + void runBeforePass(mlir::Pass* pass, mlir::Operation* op) override { + // Always print before the first pass. + if (!printed_) { + Dump("before_all", op); + printed_ = true; + } + + if (RE2::FullMatch(pass->getName(), *dump_pass_re_)) { + Dump(absl::StrCat(absl::string_view(pass->getName()), "_before"), op, + absl::StrCat(absl::Hex(pass_counter_, absl::kZeroPad8))); + } + } + + void runAfterPass(mlir::Pass* pass, mlir::Operation* op) override { + if (RE2::FullMatch(pass->getName(), *dump_pass_re_)) { + Dump(absl::StrCat(absl::string_view(pass->getName()), "_after"), op, + absl::StrCat(absl::Hex(pass_counter_++, absl::kZeroPad8))); + } + } + + private: + // Dumps the given op. `name` is used as part of the filename to help + // distinguish dumps at different passes. + void Dump(absl::string_view name, mlir::Operation* op, + std::string prefix = "") { + static constexpr char kFiletypeSuffix[] = "mlir"; + // Find names of all func ops with public visibility and check whether at + // least one of them matches `dump_func_re_`. + llvm::SmallVector func_names; + bool match = false; + op->walk([&](mlir::func::FuncOp func) { + if (func.isPublic()) { + const absl::string_view name = func.getSymName(); + if (name.empty()) { + return; + } + + func_names.push_back(name); + if (RE2::FullMatch(name, *dump_func_re_)) { + match = true; + } + } + }); + if (!func_names.empty() && !match) { + return; + } + + // Sort the function names for determinism. + llvm::sort(func_names); + + std::string joined_func_names = Sanitize(absl::StrJoin(func_names, "-")); + std::string sanitized_name = Sanitize(name); + + std::vector name_parts; + if (!prefix.empty()) { + name_parts.emplace_back(prefix); + } + if (!joined_func_names.empty()) { + name_parts.emplace_back(joined_func_names); + } + name_parts.emplace_back(sanitized_name); + name_parts.emplace_back(kFiletypeSuffix); + + // Build a filename such that it contains function names and pass names for + // easy disambiguation. + const std::string filename = tsl::io::JoinPath( + dump_dir_, absl::StrJoin(name_parts.begin(), name_parts.end(), ".")); + + // Open the file for dumping. Failures are logged instead of being + // propagated to the client because they are non-fatal. + + std::unique_ptr file; + if (auto status = tsl::Env::Default()->NewWritableFile(filename, &file); + !status.ok()) { + LOG(ERROR) << "Unable to open '" << filename + << "' for dumping TFLite MLIR output: " << status; + return; + } + file = std::make_unique(std::move(file)); + + WritableFileRawStream os(std::move(file)); + op->print(os); + } + + const std::string dump_dir_; + const std::unique_ptr dump_pass_re_; + const std::unique_ptr dump_func_re_; + + // Counter used for pass name prefix to signify sequence + int pass_counter_ = 0; + + bool printed_ = false; +}; + +std::function CreatePrintIRFun( + const std::string& pass_regex) { + std::function fun; + if (pass_regex.empty()) { + return fun; + } + return [pr = pass_regex](mlir::Pass* p, mlir::Operation*) { + static const RE2* const re = new RE2(pr); + if (RE2::FullMatch(p->getName(), *re)) { + return true; + } + return false; + }; +} + +} // namespace + +void InitPassManager(mlir::PassManager& pm, + const converter::DebugOptions& options, + llvm::raw_ostream& out) { + std::string dump_dir = options.ir_dump_dir(); + + bool dump_to_dir = !dump_dir.empty(); + bool print_to_stdout = + !options.print_ir_before().empty() || !options.print_ir_after().empty(); + + if (dump_to_dir || print_to_stdout) { + // Necessary for maintaining sequence of passes when dumping MLIR to files + // or stdout. + pm.getContext()->disableMultithreading(); + } + + if (dump_to_dir) { + dump_dir = tsl::io::JoinPath( + dump_dir, absl::FormatTime("%E4Y%m%d_%H%M%E6S", absl::Now(), + absl::LocalTimeZone())); + + // Get a valid file path to dump with. + tsl::Env* env = tsl::Env::Default(); + if (auto status = env->RecursivelyCreateDir(dump_dir); !status.ok()) { + LOG(WARNING) << "Failed to create '" << dump_dir + << "' directory for dumping: " << status; + return; + } + + // Set a default crash reproducer for easier debugging. + if (auto reproducer_stream_factory = GetReproducerStreamFactory(dump_dir)) { + pm.enableCrashReproducerGeneration(std::move(reproducer_stream_factory)); + } + + pm.addInstrumentation(std::make_unique( + dump_dir, options.ir_dump_pass_regex(), options.ir_dump_func_regex())); + } + + if (print_to_stdout) { + std::function + should_print_ir_before_pass( + CreatePrintIRFun(options.print_ir_before())); + std::function + should_print_ir_after_pass(CreatePrintIRFun(options.print_ir_after())); + + mlir::OpPrintingFlags opPrintingFlags = mlir::OpPrintingFlags(); + + if (options.has_elide_elementsattrs_if_larger()) { + opPrintingFlags.elideLargeElementsAttrs( + options.elide_elementsattrs_if_larger()); + } + + pm.enableIRPrinting(should_print_ir_before_pass, should_print_ir_after_pass, + options.print_ir_module_scope(), + /*printAfterOnlyOnChange=*/true, + /*printAfterOnlyOnFailure=*/false, out, + opPrintingFlags); + } + + // Enable pass timing. Note: MLIR expects `mlir::PassManager::enableTiming` to + // be called after all instrumentations are added. + if (options.enable_timing()) { + pm.enableTiming(); + } + + pm.addInstrumentation( + std::make_unique( + pm.getContext())); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/debug/debug.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/debug/debug.h new file mode 100644 index 0000000000000000000000000000000000000000..3bde799aad20191cf85b5f45e8cca3514cbd8632 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/debug/debug.h @@ -0,0 +1,34 @@ +/* Copyright 2023 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_DEBUG_DEBUG_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_DEBUG_DEBUG_H_ + +#include "llvm/Support/raw_ostream.h" +#include "mlir/Pass/PassManager.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/debug/debug_options.pb.h" + +namespace tensorflow { + +// Initializes the pass manager with default options that make debugging easier. +// The `out` method parameter is exposed for testing purposes and not intended +// to be specified by client code. +void InitPassManager(mlir::PassManager& pm, + const converter::DebugOptions& options, + llvm::raw_ostream& out = llvm::outs()); + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_DEBUG_DEBUG_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/debug/debug_options.proto b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/debug/debug_options.proto new file mode 100644 index 0000000000000000000000000000000000000000..6177e8d165d68f237360d64823fbb5392d2dc600 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/debug/debug_options.proto @@ -0,0 +1,61 @@ +/* Copyright 2023 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. +==============================================================================*/ + +syntax = "proto2"; + +package tensorflow.converter; + +// Additional parameters that control the debug behavior of the Converter. +// +// Next ID: 9 +message DebugOptions { + // If not empty, dumps MLIR to the specified directory. The initial state of + // the MLIR after import will be dumped at the beginning of each pass manager + // run. Additionally, MLIR will be dumped before and after each pass + // depending on pass names and functions matched using the + // `ir_dump_pass_regex` and `ir_dump_func_regex` values. + optional string ir_dump_dir = 1 [default = ""]; + + // Regular expression that matches the names of passes in pascal case (e.g., + // FooPass) before/after which MLIR will be dumped. Effective only if + // ir_dump_dir is not empty. + optional string ir_dump_pass_regex = 2 [default = ".*"]; + + // Regular expression that matches the names of functions to be dumped. MLIR + // modules are dumped only if there's at least one public function in the + // module whose name matches the pattern. Effective only if ir_dump_dir is + // not empty. + optional string ir_dump_func_regex = 3 [default = ".*"]; + + // If true, report the execution time of each MLIR pass. + optional bool enable_timing = 4 [default = false]; + + // Prints MLIR before specified passes. Supports regular expressions for + // matching against the names of the desired passes. + optional string print_ir_before = 5 [default = ""]; + + // Prints MLIR after specified passes. Supports regular expressions for + // matching against the names of the desired passes. Currently only prints + // after a pass if the MLIR is mutated. + optional string print_ir_after = 6 [default = ""]; + + // If true, always print the top-level operation when printing IR for + // print_ir_[before|after]. + optional bool print_ir_module_scope = 7 [default = true]; + + // Elide ElementsAttrs with \"...\" that have more elements than the given + // upper limit. + optional int64 elide_elementsattrs_if_larger = 8; +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/debug/debug_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/debug/debug_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..5af2617f276902ee335c6a7545b5ecf1645320ca --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/debug/debug_test.cc @@ -0,0 +1,288 @@ +/* Copyright 2023 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/compiler/mlir/lite/debug/debug.h" + +#include + +#include +#include +#include +#include + +#include +#include +#include "absl/status/status.h" +#include "absl/strings/str_cat.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/Support/raw_ostream.h" +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinDialect.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/Dialect.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/OpDefinition.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/OwningOpRef.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassManager.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Support/TypeID.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/debug/debug_options.pb.h" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "xla/tsl/lib/core/status_test_util.h" +#include "xla/tsl/platform/env.h" +#include "tensorflow/core/platform/types.h" +#include "tsl/platform/path.h" + +namespace tensorflow { +namespace debug_test { + +class NopPass : public mlir::PassWrapper> { + public: + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(NopPass) + + void runOnOperation() override {} +}; + +class MutatePass : public mlir::PassWrapper> { + public: + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(MutatePass) + + void runOnOperation() override { + mlir::OpBuilder builder(&getContext()); + getOperation()->setAttr("tfl.random_attr", builder.getUnitAttr()); + } +}; + +class AlwaysFailPass + : public mlir::PassWrapper> { + public: + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(AlwaysFailPass) + + void runOnOperation() override { signalPassFailure(); } +}; + +} // namespace debug_test + +namespace { + +using ::testing::HasSubstr; +using ::testing::IsEmpty; +using ::testing::Not; +using namespace tensorflow::debug_test; + +class InitPassManagerTest : public testing::Test { + protected: + InitPassManagerTest() + : path_(GetOutputPath()), context_([]() { + mlir::registerPassManagerCLOptions(); + mlir::DialectRegistry registry; + registry.insert(); + registry.insert(); + registry.insert(); + registry.insert(); + return registry; + }()) { + context_.loadAllAvailableDialects(); + + mlir::OpBuilder builder(&context_); + module_ = mlir::ModuleOp::create(builder, builder.getUnknownLoc()); + + builder.setInsertionPointToStart(module_->getBody()); + auto func = mlir::func::FuncOp::create(builder, // + builder.getUnknownLoc(), "main", + builder.getFunctionType({}, {})); + func->setAttr("tfl.func", builder.getUnitAttr()); + builder.setInsertionPointToStart(func.addEntryBlock()); + llvm::SmallVector shape{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + mlir::arith::ConstantOp::create( + builder, builder.getUnknownLoc(), + mlir::DenseIntElementsAttr::get( + mlir::RankedTensorType::get(shape.size(), builder.getI32Type()), + shape)); + mlir::func::ReturnOp::create(builder, builder.getUnknownLoc()); + } + + absl::Status GetDumpDir(std::string* dump_dir) { + std::vector files; + if (auto status = tsl::Env::Default()->GetChildren(path_, &files); + !status.ok()) { + return status; + } + if (files.size() != 1) { + return absl::FailedPreconditionError( + "Expecting directory to have one child."); + } + *dump_dir = tsl::io::JoinPath(path_, files[0]); + return absl::OkStatus(); + } + + std::string path_; + mlir::MLIRContext context_; + mlir::OwningOpRef module_; + + private: + std::string GetOutputPath() { + const auto* const test_info = + testing::UnitTest::GetInstance()->current_test_info(); + return tsl::io::JoinPath( + getenv("TEST_UNDECLARED_OUTPUTS_DIR"), + absl::StrCat(test_info->test_suite_name(), ".", test_info->name())); + } +}; + +TEST_F(InitPassManagerTest, CrashReproducer) { + converter::DebugOptions debug_options; + *debug_options.mutable_ir_dump_dir() = path_; + + mlir::PassManager pm(&context_); + InitPassManager(pm, debug_options); + pm.addPass(std::make_unique()); + ASSERT_TRUE(mlir::failed(pm.run(*module_))); + + std::string dump_dir; + TF_ASSERT_OK(GetDumpDir(&dump_dir)); + + std::string mlir_dump; + TF_ASSERT_OK(tsl::ReadFileToString( + tsl::Env::Default(), + tsl::io::JoinPath(dump_dir, "tfl_mlir_crash_repro.mlir"), &mlir_dump)); + EXPECT_THAT(mlir_dump, Not(IsEmpty())); +} + +TEST_F(InitPassManagerTest, DumpToDir) { + converter::DebugOptions debug_options; + *debug_options.mutable_ir_dump_dir() = path_; + *debug_options.mutable_ir_dump_pass_regex() = R"(.*NopPass)"; + + mlir::PassManager pm(&context_); + InitPassManager(pm, debug_options); + pm.addPass(std::make_unique()); + ASSERT_TRUE(mlir::succeeded(pm.run(*module_))); + std::string dump_dir; + TF_ASSERT_OK(GetDumpDir(&dump_dir)); + + { + std::string mlir_dump; + TF_ASSERT_OK(tsl::ReadFileToString( + tsl::Env::Default(), + tsl::io::JoinPath( + dump_dir, "00000000.main.tensorflow_debug_test_NopPass_after.mlir"), + &mlir_dump)); + EXPECT_THAT(mlir_dump, Not(IsEmpty())); + } + { + std::string mlir_dump; + TF_ASSERT_OK(tsl::ReadFileToString( + tsl::Env::Default(), + tsl::io::JoinPath( + dump_dir, + "00000000.main.tensorflow_debug_test_NopPass_before.mlir"), + &mlir_dump)); + EXPECT_THAT(mlir_dump, Not(IsEmpty())); + } +} + +TEST_F(InitPassManagerTest, PrintIRBeforeEverything) { + converter::DebugOptions debug_options; + *debug_options.mutable_print_ir_before() = R"(.*)"; + std::string captured_out; + llvm::raw_string_ostream out(captured_out); + + mlir::PassManager pm(&context_); + InitPassManager(pm, debug_options, out); + pm.addPass(std::make_unique()); + ASSERT_TRUE(mlir::succeeded(pm.run(*module_))); + + EXPECT_THAT(captured_out, + HasSubstr("IR Dump Before tensorflow::debug_test::NopPass")); + EXPECT_THAT(captured_out, + Not(HasSubstr("IR Dump After tensorflow::debug_test::NopPass"))); +} + +TEST_F(InitPassManagerTest, PrintIRAfterEverything) { + converter::DebugOptions debug_options; + *debug_options.mutable_print_ir_after() = R"(.*)"; + std::string captured_out; + llvm::raw_string_ostream out(captured_out); + + mlir::PassManager pm(&context_); + InitPassManager(pm, debug_options, out); + pm.addPass(std::make_unique()); + ASSERT_TRUE(mlir::succeeded(pm.run(*module_))); + + EXPECT_THAT(captured_out, + HasSubstr("IR Dump After tensorflow::debug_test::MutatePass")); + EXPECT_THAT( + captured_out, + Not(HasSubstr("IR Dump Before tensorflow::debug_test::MutatePass"))); +} + +TEST_F(InitPassManagerTest, PrintIRBeforeAndAfterEverything) { + converter::DebugOptions debug_options; + *debug_options.mutable_print_ir_before() = R"(.*)"; + *debug_options.mutable_print_ir_after() = R"(.*)"; + std::string captured_out; + llvm::raw_string_ostream out(captured_out); + + mlir::PassManager pm(&context_); + InitPassManager(pm, debug_options, out); + pm.addPass(std::make_unique()); + ASSERT_TRUE(mlir::succeeded(pm.run(*module_))); + + EXPECT_THAT(captured_out, + HasSubstr("IR Dump After tensorflow::debug_test::MutatePass")); + EXPECT_THAT(captured_out, + HasSubstr("IR Dump Before tensorflow::debug_test::MutatePass")); +} + +TEST_F(InitPassManagerTest, ElideLargeElementAttrs) { + converter::DebugOptions debug_options; + *debug_options.mutable_print_ir_before() = R"(.*)"; + debug_options.set_elide_elementsattrs_if_larger(5); + std::string captured_out; + llvm::raw_string_ostream out(captured_out); + + mlir::PassManager pm(&context_); + InitPassManager(pm, debug_options, out); + pm.addPass(std::make_unique()); + ASSERT_TRUE(mlir::succeeded(pm.run(*module_))); + + EXPECT_THAT(captured_out, HasSubstr("dense_resource<__elided__>")); +} + +TEST_F(InitPassManagerTest, DontElideSmallerElementAttrs) { + converter::DebugOptions debug_options; + *debug_options.mutable_print_ir_before() = R"(.*)"; + debug_options.set_elide_elementsattrs_if_larger(11); + std::string captured_out; + llvm::raw_string_ostream out(captured_out); + + mlir::PassManager pm(&context_); + InitPassManager(pm, debug_options, out); + pm.addPass(std::make_unique()); + ASSERT_TRUE(mlir::succeeded(pm.run(*module_))); + + EXPECT_THAT(captured_out, + HasSubstr("dense<[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]>")); +} + +} // namespace +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/delegates/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/delegates/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..52eaac2e671569cf1678bfa587219b5b75fc5d0e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/delegates/BUILD @@ -0,0 +1,27 @@ +# Copyright 2019 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. +# ============================================================================== + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + default_visibility = ["//visibility:public"], + licenses = ["notice"], +) + +# LINT.IfChange(config_setting) +config_setting( + name = "tflite_debug_delegate", + define_values = {"tflite_debug_delegate": "true"}, +) +# LINT.ThenChange(//tensorflow/lite/delegates/BUILD) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/delegates/flex/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/delegates/flex/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..974d64fb94bdb796afe600b0bd5fea57827f8c00 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/delegates/flex/BUILD @@ -0,0 +1,45 @@ +load("@rules_cc//cc:cc_library.bzl", "cc_library") +load( + "//tensorflow:tensorflow.bzl", + "if_mobile", + "if_not_mobile", +) +load("//tensorflow:tensorflow.default.bzl", "get_compatible_with_portable") +load("//tensorflow/lite:special_rules.bzl", "internal_visibility_allowlist") + +default_visibility = [ + "//tensorflow/compiler/mlir/lite:__subpackages__", + "//tensorflow/lite/android:__subpackages__", + "//tensorflow/lite/toco/tflite:__subpackages__", +] + +# +# This is a TF Lite delegate that is powered by TensorFlow's Eager. +# +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + default_visibility = default_visibility, + licenses = ["notice"], +) + +cc_library( + name = "allowlisted_flex_ops_lib", + srcs = [ + "allowlisted_flex_ops.cc", + ], + hdrs = [ + "allowlisted_flex_ops.h", + "allowlisted_flex_ops_internal.h", + ], + compatible_with = get_compatible_with_portable(), + copts = select({ + "//tensorflow:android": ["-Wno-private-header"], + "//conditions:default": [], + }), + visibility = internal_visibility_allowlist(), + deps = if_mobile([ + "//tensorflow/core:portable_tensorflow_lib_lite", + ]) + if_not_mobile([ + "//tensorflow/core:framework", + ]), +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/delegates/flex/allowlisted_flex_ops.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/delegates/flex/allowlisted_flex_ops.cc new file mode 100644 index 0000000000000000000000000000000000000000..9c6ad485b1cbee8ee4a2b2973945d6d449b0a2d7 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/delegates/flex/allowlisted_flex_ops.cc @@ -0,0 +1,852 @@ +/* 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 "tensorflow/compiler/mlir/lite/delegates/flex/allowlisted_flex_ops.h" + +#include +#include + +#include "tensorflow/compiler/mlir/lite/delegates/flex/allowlisted_flex_ops_internal.h" +#include "tensorflow/core/framework/op.h" + +namespace tflite { +namespace flex { + +const std::set& GetFlexAllowlist() { + // LINT.IfChange + static const std::set* allowlisted_flex_ops = + new std::set({ + // go/keep-sorted start + "Abort", + "Abs", + "Add", + "AddN", + "AddV2", + "AdjustContrast", + "AdjustContrastv2", + "AdjustHue", + "AdjustSaturation", + "All", + "Angle", + "Any", + "ApplyAdaMax", + "ApplyAdadelta", + "ApplyAdagrad", + "ApplyAdagradDA", + "ApplyAdagradV2", + "ApplyAdam", + "ApplyAddSign", + "ApplyCenteredRMSProp", + "ApplyFtrl", + "ApplyFtrlV2", + "ApplyGradientDescent", + "ApplyMomentum", + "ApplyPowerSign", + "ApplyProximalAdagrad", + "ApplyProximalGradientDescent", + "ApplyRMSProp", + "ApproximateEqual", + "ArgMax", + "ArgMin", + "AsString", + "Assert", + "Assign", + "AssignAdd", + "AssignAddVariableOp", + "AssignSub", + "AssignSubVariableOp", + "AssignVariableOp", + "Atan", + "Atan2", + "AudioSpectrogram", + "AvgPool", + "AvgPool3D", + "AvgPool3DGrad", + "AvgPoolGrad", + "BatchCholesky", + "BatchDatasetV2", + "BatchMatMul", + "BatchMatMulV2", + "BatchMatrixBandPart", + "BatchMatrixDeterminant", + "BatchMatrixDiag", + "BatchMatrixDiagPart", + "BatchMatrixInverse", + "BatchMatrixSetDiag", + "BatchMatrixTriangularSolve", + "BatchNormWithGlobalNormalization", + "BatchNormWithGlobalNormalizationGrad", + "BatchToSpace", + "BatchToSpaceND", + "BiasAdd", + "BiasAddGrad", + "BiasAddV1", + "Bincount", + "Bitcast", + "BitwiseAnd", + "BitwiseOr", + "BitwiseXor", + "BroadcastArgs", + "BroadcastGradientArgs", + "BroadcastTo", + "Bucketize", + "CTCBeamSearchDecoder", + "CTCGreedyDecoder", + "Case", + "Cast", + "Ceil", + "CheckNumerics", + "CheckNumericsV2", + "Cholesky", + "ClipByValue", + "CombinedNonMaxSuppression", + "Complex", + "ComplexAbs", + "Concat", + "ConcatOffset", + "ConcatV2", + "Conj", + "ConjugateTranspose", + "Const", + "ControlTrigger", + "Conv2D", + "Conv2DBackpropFilter", + "Conv2DBackpropInput", + "Conv3D", + "Conv3DBackpropFilter", + "Conv3DBackpropFilterV2", + "Conv3DBackpropInput", + "Conv3DBackpropInputV2", + "Cos", + "Cosh", + "CropAndResize", + "CropAndResizeGradBoxes", + "CropAndResizeGradImage", + "Cumprod", + "Cumsum", + "CumulativeLogsumexp", + "DataFormatDimMap", + "DataFormatVecPermute", + "DebugGradientIdentity", + "DebugGradientRefIdentity", + "DecodeAndCropJpeg", + "DecodeBase64", + "DecodeBmp", + "DecodeGif", + "DecodeImage", + "DecodeJpeg", + "DecodePaddedRaw", + "DecodePng", + "DecodeRaw", + "DecodeWav", + "DeepCopy", + "DeleteSessionTensor", + "DenseBincount", + "DenseToDenseSetOperation", + "DenseToSparseSetOperation", + "DepthToSpace", + "DepthwiseConv2dNative", + "DepthwiseConv2dNativeBackpropFilter", + "DepthwiseConv2dNativeBackpropInput", + "Dequantize", + "DestroyResourceOp", + "DestroyTemporaryVariable", + "Diag", + "DiagPart", + "Dilation2D", + "Dilation2DBackpropFilter", + "Dilation2DBackpropInput", + "Div", + "DivNoNan", + "DynamicPartition", + "DynamicStitch", + "Einsum", + "Elu", + "EluGrad", + "Empty", + "EmptyTensorList", + "EmptyTensorMap", + "EncodeBase64", + "EncodeJpeg", + "EncodeJpegVariableQuality", + "EncodePng", + "EncodeWav", + "EnsureShape", + "Enter", + "Equal", + "Erf", + "Exit", + "Exp", + "ExpandDims", + "ExtractImagePatches", + "FFT", + "FFT2D", + "FFT3D", + "FIFOQueue", + "FIFOQueueV2", + "FakeQuantWithMinMaxArgs", + "FakeQuantWithMinMaxArgsGradient", + "FakeQuantWithMinMaxVars", + "FakeQuantWithMinMaxVarsGradient", + "FakeQuantWithMinMaxVarsPerChannel", + "FakeQuantWithMinMaxVarsPerChannelGradient", + "FakeQueue", + "Fill", + "FilterDataset", + "FinalizeDataset", + "Fingerprint", + "FlatMapDataset", + "Floor", + "FloorDiv", + "FloorMod", + "FusedBatchNorm", + "FusedBatchNormGrad", + "FusedBatchNormGradV2", + "FusedBatchNormGradV3", + "FusedBatchNormV2", + "FusedBatchNormV3", + "FusedPadConv2D", + "FusedResizeAndPadConv2D", + "Gather", + "GatherNd", + "GatherV2", + "GetSessionHandle", + "GetSessionHandleV2", + "GetSessionTensor", + "Greater", + "GreaterEqual", + "HSVToRGB", + "HashTable", + "HashTableV2", + "HistogramSummary", + "IFFT", + "IFFT2D", + "IFFT3D", + "IRFFT", + "IRFFT2D", + "IRFFT3D", + "Identity", + "IdentityN", + "Imag", + "ImageProjectiveTransformV2", + "ImageProjectiveTransformV3", + "ImmutableConst", + "InTopK", + "InTopKV2", + "InitializeTable", + "InitializeTableFromDataset", + "InitializeTableFromTextFile", + "InitializeTableFromTextFileV2", + "InitializeTableV2", + "InplaceAdd", + "InplaceSub", + "InplaceUpdate", + "Inv", + "InvGrad", + "Invert", + "InvertPermutation", + "IsFinite", + "IsNan", + "IsVariableInitialized", + "LRN", + "LeakyRelu", + "LeakyReluGrad", + "LeftShift", + "Less", + "LessEqual", + "LinSpace", + "ListDiff", + "Log", + "LogMatrixDeterminant", + "LogSoftmax", + "LogicalAnd", + "LogicalNot", + "LogicalOr", + "LookupTableExport", + "LookupTableExportV2", + "LookupTableFind", + "LookupTableFindV2", + "LookupTableImport", + "LookupTableImportV2", + "LookupTableInsert", + "LookupTableInsertV2", + "LookupTableRemoveV2", + "LookupTableSize", + "LookupTableSizeV2", + "LoopCond", + "MapDataset", + "MatMul", + "MatrixBandPart", + "MatrixDeterminant", + "MatrixDiag", + "MatrixDiagPart", + "MatrixDiagPartV2", + "MatrixDiagPartV3", + "MatrixDiagV2", + "MatrixDiagV3", + "MatrixInverse", + "MatrixSetDiag", + "MatrixSetDiagV2", + "MatrixSetDiagV3", + "MatrixTriangularSolve", + "Max", + "MaxPool", + "MaxPool3D", + "MaxPool3DGrad", + "MaxPool3DGradGrad", + "MaxPoolGrad", + "MaxPoolGradGrad", + "MaxPoolGradGradV2", + "MaxPoolGradV2", + "MaxPoolGradWithArgmax", + "MaxPoolV2", + "MaxPoolWithArgmax", + "Maximum", + "Mean", + "Merge", + "MergeSummary", + "MergeV2Checkpoints", + "Mfcc", + "Min", + "Minimum", + "MirrorPad", + "MirrorPadGrad", + "ModelDataset", + "Mul", + "MulNoNan", + "Multinomial", + "MutableDenseHashTable", + "MutableDenseHashTableV2", + "MutableHashTable", + "MutableHashTableOfTensors", + "MutableHashTableOfTensorsV2", + "MutableHashTableV2", + "Neg", + "NextIteration", + "NoOp", + "NonMaxSuppression", + "NonMaxSuppressionV2", + "NonMaxSuppressionV3", + "NonMaxSuppressionV4", + "NonMaxSuppressionV5", + "NonMaxSuppressionWithOverlaps", + "NotEqual", + "OneHot", + "OnesLike", + "OptimizeDatasetV2", + "OptionalFromValue", + "OptionalGetValue", + "OptionalHasValue", + "OptionalNone", + "Pack", + "Pad", + "PadV2", + "PaddingFIFOQueue", + "PaddingFIFOQueueV2", + "ParallelConcat", + "ParallelDynamicStitch", + "ParseExample", + "ParseExampleV2", + "ParseSequenceExample", + "ParseSequenceExampleV2", + "ParseSingleExample", + "ParseSingleSequenceExample", + "Placeholder", + "PlaceholderV2", + "PlaceholderWithDefault", + "PopulationCount", + "Pow", + "PreventGradient", + "Print", + "PrintV2", + "Prod", + "Qr", + "QuantizeDownAndShrinkRange", + "QuantizeV2", + "QuantizedAdd", + "QuantizedAvgPool", + "QuantizedBatchNormWithGlobalNormalization", + "QuantizedBiasAdd", + "QuantizedConcat", + "QuantizedConv2D", + "QuantizedInstanceNorm", + "QuantizedMatMul", + "QuantizedMaxPool", + "QuantizedMul", + "QuantizedRelu", + "QuantizedRelu6", + "QuantizedReshape", + "QuantizedResizeBilinear", + "QueueClose", + "QueueCloseV2", + "QueueDequeue", + "QueueDequeueMany", + "QueueDequeueManyV2", + "QueueDequeueUpTo", + "QueueDequeueUpToV2", + "QueueDequeueV2", + "QueueEnqueue", + "QueueEnqueueMany", + "QueueEnqueueManyV2", + "QueueEnqueueV2", + "QueueIsClosed", + "QueueIsClosedV2", + "QueueSize", + "QueueSizeV2", + "RFFT", + "RFFT2D", + "RFFT3D", + "RGBToHSV", + "RaggedBincount", + "RaggedGather", + "RaggedRange", + "RaggedTensorFromVariant", + "RaggedTensorToSparse", + "RaggedTensorToTensor", + "RaggedTensorToVariant", + "RaggedTensorToVariantGradient", + "RandomGamma", + "RandomPoisson", + "RandomPoissonV2", + "RandomShuffle", + "RandomStandardNormal", + "RandomUniform", + "RandomUniformInt", + "Range", + "Rank", + "ReadFile", + "ReadVariableOp", + "Real", + "RealDiv", + "Reciprocal", + "ReciprocalGrad", + "Recv", + "ReduceDataset", + "ReduceJoin", + "RefEnter", + "RefExit", + "RefIdentity", + "RefMerge", + "RefNextIteration", + "RefSelect", + "RefSwitch", + "RegexFullMatch", + "RegexReplace", + "Relu", + "Relu6", + "Relu6Grad", + "ReluGrad", + "RemoteCall", + "RepeatDataset", + "RequantizationRange", + "Requantize", + "Reshape", + "ResizeBicubic", + "ResizeBicubicGrad", + "ResizeBilinear", + "ResizeBilinearGrad", + "ResizeNearestNeighbor", + "ResizeNearestNeighborGrad", + "ResourceApplyAdaMax", + "ResourceApplyAdadelta", + "ResourceApplyAdagrad", + "ResourceApplyAdagradDA", + "ResourceApplyAdagradV2", + "ResourceApplyAdam", + "ResourceApplyAdamWithAmsgrad", + "ResourceApplyAddSign", + "ResourceApplyCenteredRMSProp", + "ResourceApplyFtrl", + "ResourceApplyFtrlV2", + "ResourceApplyGradientDescent", + "ResourceApplyKerasMomentum", + "ResourceApplyMomentum", + "ResourceApplyPowerSign", + "ResourceApplyProximalAdagrad", + "ResourceApplyProximalGradientDescent", + "ResourceApplyRMSProp", + "ResourceGather", + "ResourceGatherNd", + "ResourceScatterAdd", + "ResourceScatterDiv", + "ResourceScatterMax", + "ResourceScatterMin", + "ResourceScatterMul", + "ResourceScatterNdAdd", + "ResourceScatterNdMax", + "ResourceScatterNdMin", + "ResourceScatterNdSub", + "ResourceScatterNdUpdate", + "ResourceScatterSub", + "ResourceScatterUpdate", + "ResourceSparseApplyAdadelta", + "ResourceSparseApplyAdagrad", + "ResourceSparseApplyAdagradDA", + "ResourceSparseApplyAdagradV2", + "ResourceSparseApplyCenteredRMSProp", + "ResourceSparseApplyFtrl", + "ResourceSparseApplyFtrlV2", + "ResourceSparseApplyKerasMomentum", + "ResourceSparseApplyMomentum", + "ResourceSparseApplyProximalAdagrad", + "ResourceSparseApplyProximalGradientDescent", + "ResourceSparseApplyRMSProp", + "ResourceStridedSliceAssign", + "Restore", + "RestoreSlice", + "RestoreV2", + "Reverse", + "ReverseSequence", + "ReverseV2", + "RightShift", + "Roll", + "Round", + "Rsqrt", + "RsqrtGrad", + "SampleDistortedBoundingBox", + "SampleDistortedBoundingBoxV2", + "Save", + "SaveSlices", + "SaveV2", + "ScalarSummary", + "ScatterNd", + "ScatterNdAdd", + "ScatterNdMax", + "ScatterNdMin", + "ScatterNdNonAliasingAdd", + "ScatterNdSub", + "ScatterNdUpdate", + "SegmentMax", + "SegmentMean", + "SegmentMin", + "SegmentProd", + "SegmentSum", + "Select", + "SelectV2", + "Selu", + "SeluGrad", + "Send", + "SerializeTensor", + "Shape", + "ShapeN", + "ShardedFilename", + "ShardedFilespec", + "Sigmoid", + "SigmoidGrad", + "Sign", + "Sin", + "Sinh", + "Size", + "Slice", + "Softmax", + "SoftmaxCrossEntropyWithLogits", + "Softplus", + "SoftplusGrad", + "Softsign", + "SoftsignGrad", + "SpaceToBatch", + "SpaceToBatchND", + "SpaceToDepth", + "SparseAdd", + "SparseApplyAdadelta", + "SparseApplyAdagrad", + "SparseApplyAdagradDA", + "SparseApplyAdagradV2", + "SparseApplyCenteredRMSProp", + "SparseApplyFtrl", + "SparseApplyFtrlV2", + "SparseApplyMomentum", + "SparseApplyProximalAdagrad", + "SparseApplyProximalGradientDescent", + "SparseApplyRMSProp", + "SparseBincount", + "SparseCross", + "SparseCrossHashed", + "SparseCrossV2", + "SparseFillEmptyRows", + "SparseFillEmptyRowsGrad", + "SparseReduceSum", + "SparseReorder", + "SparseReshape", + "SparseSegmentMean", + "SparseSegmentMeanGrad", + "SparseSegmentMeanWithNumSegments", + "SparseSegmentSqrtN", + "SparseSegmentSqrtNGrad", + "SparseSegmentSqrtNWithNumSegments", + "SparseSegmentSum", + "SparseSegmentSumGrad", + "SparseSegmentSumWithNumSegments", + "SparseSlice", + "SparseSoftmaxCrossEntropyWithLogits", + "SparseTensorDenseMatMul", + "SparseToDense", + "SparseToSparseSetOperation", + "Split", + "SplitV", + "Sqrt", + "SqrtGrad", + "Square", + "SquaredDifference", + "Squeeze", + "Stack", + "StackClose", + "StackCloseV2", + "StackPop", + "StackPopV2", + "StackPush", + "StackPushV2", + "StackV2", + "StatelessMultinomial", + "StatelessRandomGammaV2", + "StatelessRandomGammaV3", + "StatelessRandomGetAlg", + "StatelessRandomGetKeyCounter", + "StatelessRandomGetKeyCounterAlg", + "StatelessRandomNormal", + "StatelessRandomNormalV2", + "StatelessRandomPoisson", + "StatelessRandomUniform", + "StatelessRandomUniformFullInt", + "StatelessRandomUniformFullIntV2", + "StatelessRandomUniformInt", + "StatelessRandomUniformIntV2", + "StatelessRandomUniformV2", + "StatelessSampleDistortedBoundingBox", + "StatelessTruncatedNormal", + "StatelessTruncatedNormalV2", + "StaticRegexFullMatch", + "StaticRegexReplace", + "StopGradient", + "StridedSlice", + "StridedSliceAssign", + "StridedSliceGrad", + "StringFormat", + "StringJoin", + "StringLength", + "StringLower", + "StringSplit", + "StringSplitV2", + "StringStrip", + "StringToHashBucket", + "StringToHashBucketFast", + "StringToHashBucketStrong", + "StringToNumber", + "Sub", + "Substr", + "Sum", + "Switch", + "SymbolicGradient", + "TakeDataset", + "TakeWhileDataset", + "Tan", + "Tanh", + "TanhGrad", + "TemporaryVariable", + "TensorArray", + "TensorArrayClose", + "TensorArrayCloseV2", + "TensorArrayCloseV3", + "TensorArrayConcat", + "TensorArrayConcatV2", + "TensorArrayConcatV3", + "TensorArrayGather", + "TensorArrayGatherV2", + "TensorArrayGatherV3", + "TensorArrayGrad", + "TensorArrayGradV2", + "TensorArrayGradV3", + "TensorArrayGradWithShape", + "TensorArrayPack", + "TensorArrayRead", + "TensorArrayReadV2", + "TensorArrayReadV3", + "TensorArrayScatter", + "TensorArrayScatterV2", + "TensorArrayScatterV3", + "TensorArraySize", + "TensorArraySizeV2", + "TensorArraySizeV3", + "TensorArraySplit", + "TensorArraySplitV2", + "TensorArraySplitV3", + "TensorArrayUnpack", + "TensorArrayV2", + "TensorArrayV3", + "TensorArrayWrite", + "TensorArrayWriteV2", + "TensorArrayWriteV3", + "TensorListConcat", + "TensorListConcatLists", + "TensorListConcatV2", + "TensorListElementShape", + "TensorListFromTensor", + "TensorListGather", + "TensorListGetItem", + "TensorListLength", + "TensorListPopBack", + "TensorListPushBack", + "TensorListPushBackBatch", + "TensorListReserve", + "TensorListResize", + "TensorListScatter", + "TensorListScatterIntoExistingList", + "TensorListScatterV2", + "TensorListSetItem", + "TensorListSplit", + "TensorListStack", + "TensorMapErase", + "TensorMapHasKey", + "TensorMapInsert", + "TensorMapLookup", + "TensorMapSize", + "TensorMapStackKeys", + "TensorScatterAdd", + "TensorScatterMax", + "TensorScatterMin", + "TensorScatterSub", + "TensorScatterUpdate", + "TensorSliceDataset", + "TensorStridedSliceUpdate", + "Tile", + "TileGrad", + "Timestamp", + "TopK", + "TopKV2", + "Transpose", + "TruncateDiv", + "TruncatedNormal", + "UnicodeDecode", + "UnicodeDecodeWithOffsets", + "UnicodeEncode", + "UnicodeTranscode", + "Unique", + "UniqueV2", + "UniqueWithCounts", + "UniqueWithCountsV2", + "Unpack", + "UnsortedSegmentJoin", + "UnsortedSegmentMax", + "UnsortedSegmentMin", + "UnsortedSegmentProd", + "UnsortedSegmentSum", + "UnwrapDatasetVariant", + "UpperBound", + "VarHandleOp", + "VarIsInitializedOp", + "Variable", + "VariableShape", + "VariableV2", + "Where", + "WrapDatasetVariant", + "WriteFile", + "Xdivy", + "Xlog1py", + "Xlogy", + "ZerosLike", + "_Arg", + "_ArrayToList", + "_DeviceArg", + "_DeviceRetval", + "_FusedConv2D", + "_HostCast", + "_HostRecv", + "_HostSend", + "_ListToArray", + "_ParallelConcatStart", + "_ParallelConcatUpdate", + "_ReadVariablesOp", + "_Recv", + "_Retval", + "_Send", + "_SwitchN", + "_VarHandlesOp", + // go/keep-sorted end + }); + // LINT.ThenChange(//tensorflow/lite/g3doc/guide/op_select_allowlist.md) + + return *allowlisted_flex_ops; + // Prevent lint error about this function being too long. This function + // is a set of ops, and making it shorter won't help readbility. + // NOLINTNEXTLINE +} + +const std::set& GetTFTextFlexAllowlist() { + // LINT.IfChange + static const std::set* tftext_flex_ops = + new std::set({ + "CaseFoldUTF8", + "ConstrainedSequence", + "MaxSpanningTree", + "NormalizeUTF8", + "NormalizeUTF8WithOffsetsMap", + "RegexSplitWithOffsets", + "RougeL", + "SentenceFragments", + "SentencepieceOp", + "SentencepieceTokenizeOp", + "SentencepieceTokenizeWithOffsetsOp", + "SentencepieceDetokenizeOp", + "SentencepieceVocabSizeOp", + "SplitMergeTokenizeWithOffsets", + "TFText>NgramsStringJoin", + "TFText>WhitespaceTokenizeWithOffsetsV2", + "TokenizerFromLogits", + "UnicodeScriptTokenizeWithOffsets", + "WhitespaceTokenizeWithOffsets", + "WordpieceTokenizeWithOffsets", + }); + // LINT.ThenChange(//tensorflow/lite/g3doc/guide/op_select_allowlist.md) + + return *tftext_flex_ops; +} + +// Allow the tf.text ops if they are registered in the global op registry. +bool IsAllowedTFTextOpForFlex(const std::string& op_name) { + if (GetTFTextFlexAllowlist().count(op_name) == 0) return false; + return tensorflow::OpRegistry::Global()->LookUp(op_name) != nullptr; +} + +const std::set& GetSentencePieceFlexAllowlist() { + // LINT.IfChange + static const std::set* sentencepiece_flex_ops = + new std::set({ + "SentencepieceGetPieceSize", + "SentencepiecePieceToId", + "SentencepieceIdToPiece", + "SentencepieceEncodeDense", + "SentencepieceEncodeSparse", + "SentencepieceDecode", + }); + // LINT.ThenChange(//tensorflow/lite/g3doc/guide/op_select_allowlist.md) + + return *sentencepiece_flex_ops; +} + +// Allow the sentencepiece ops if they are registered in the global op registry. +bool IsAllowedSentencePieceOpForFlex(const std::string& op_name) { + if (GetSentencePieceFlexAllowlist().count(op_name) == 0) return false; + return tensorflow::OpRegistry::Global()->LookUp(op_name) != nullptr; +} + +bool IsAllowlistedFlexOp(const std::string& tensorflow_op_name) { + if (GetFlexAllowlist().count(tensorflow_op_name) != 0) return true; + + // Check if the op is an allowlisted tf.text or sentencepiece op. + return IsAllowedTFTextOpForFlex(tensorflow_op_name) || + IsAllowedSentencePieceOpForFlex(tensorflow_op_name); +} + +} // namespace flex +} // namespace tflite diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/delegates/flex/allowlisted_flex_ops.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/delegates/flex/allowlisted_flex_ops.h new file mode 100644 index 0000000000000000000000000000000000000000..442e7fa665626f2c98b6ebae4d2998786a092ce3 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/delegates/flex/allowlisted_flex_ops.h @@ -0,0 +1,45 @@ +/* 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_DELEGATES_FLEX_ALLOWLISTED_FLEX_OPS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_DELEGATES_FLEX_ALLOWLISTED_FLEX_OPS_H_ + +#include +#include + +namespace tflite { +namespace flex { + +// Whether the given op has been statically allowlisted for flex export. +// +// This static allowlist is formed by the intersection of ops supported by +// TensorFlowMobile on both iOS and Android. As the converter is likely running +// on a host that has the full suite of TensorFlow ops available, we use this +// static allowlist to ensure compatibility when deploying to a mobile device. +// TODO(b/118389105): Automate generation of the allowlisted flex ops. +bool IsAllowlistedFlexOp(const std::string& tensorflow_op_name); + +// Return the list of allowlisted flex ops. +const std::set& GetFlexAllowlist(); + +// Return the list of TF.Text flex ops. +const std::set& GetTFTextFlexAllowlist(); + +// Return the list of SentencePiece flex ops. +const std::set& GetSentencePieceFlexAllowlist(); + +} // namespace flex +} // namespace tflite + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_DELEGATES_FLEX_ALLOWLISTED_FLEX_OPS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/delegates/flex/allowlisted_flex_ops_internal.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/delegates/flex/allowlisted_flex_ops_internal.h new file mode 100644 index 0000000000000000000000000000000000000000..cd45d48f0be87604dcac2b1f6911ad34d3715ede --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/delegates/flex/allowlisted_flex_ops_internal.h @@ -0,0 +1,29 @@ +/* Copyright 2020 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_DELEGATES_FLEX_ALLOWLISTED_FLEX_OPS_INTERNAL_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_DELEGATES_FLEX_ALLOWLISTED_FLEX_OPS_INTERNAL_H_ + +#include + +namespace tflite { +namespace flex { + +// Return true if op_name is a tf.text op need to be supported by flex delegate. +bool IsAllowedTFTextOpForFlex(const std::string& op_name); + +} // namespace flex +} // namespace tflite + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_DELEGATES_FLEX_ALLOWLISTED_FLEX_OPS_INTERNAL_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/delegates/flex/build_def.bzl b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/delegates/flex/build_def.bzl new file mode 100644 index 0000000000000000000000000000000000000000..fb1f078a7edc1077361fd3ee9bdaece6a60213d8 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/delegates/flex/build_def.bzl @@ -0,0 +1,190 @@ +"""Generate custom flex delegate library.""" + +load("@rules_cc//cc:cc_library.bzl", "cc_library") +load( + "//tensorflow:tensorflow.bzl", + "clean_dep", + "if_android", + "if_ios", + "if_mobile", + "tf_cc_binary", + "tf_copts", + "tf_defines_nortti_if_lite_protos", + "tf_features_nomodules_if_mobile", + "tf_opts_nortti_if_lite_protos", + "tf_portable_full_lite_protos", +) +load("//tensorflow/compiler/mlir/lite:special_rules.bzl", "flex_portable_tensorflow_deps") + +# LINT.IfChange + +def generate_flex_kernel_header( + name, + models, + testonly = 0, + additional_deps = []): + """A rule to generate a header file listing only used operators. + + Args: + name: Name of the generated library. + models: TFLite models to interpret. + testonly: Should be marked as true if additional_deps is testonly. + additional_deps: Dependencies for additional TF ops. + + Returns: + A struct with 'header' and 'include_path' fields that + contain the generated header and the required include entry. + """ + include_path = "%s_tf_generated_kernel_header" % name + header = include_path + "/ops_to_register.h" + + if type(models) != type([]): + models = [models] + + # List all flex ops from models. + model_file_args = " --graphs=%s" % ",".join( + ["$(location %s)" % f for f in models], + ) + list_ops_output = include_path + "/list_flex_ops" + list_ops_tool = clean_dep("//tensorflow/lite/tools:list_flex_ops_main") + if additional_deps: + tf_cc_binary( + name = "%s_list_flex_ops_main" % name, + deps = [ + clean_dep("//tensorflow/lite/tools:list_flex_ops_main_lib"), + ] + additional_deps, + testonly = testonly, + ) + list_ops_tool = ":%s_list_flex_ops_main" % name + native.genrule( + name = "%s_list_flex_ops" % name, + srcs = models, + outs = [list_ops_output], + tools = [list_ops_tool], + message = "Listing flex ops from %s..." % ",".join(models), + cmd = ("$(location " + list_ops_tool + ")" + + model_file_args + " > \"$@\""), + testonly = testonly, + ) + + # Generate the kernel registration header file from list of flex ops. + tool = clean_dep("//tensorflow/python/tools:print_selective_registration_header") + native.genrule( + name = "%s_kernel_registration" % name, + srcs = [list_ops_output], + outs = [header], + tools = [tool], + message = "Processing %s..." % list_ops_output, + cmd = ("$(location " + tool + ")" + + " --default_ops=\"\"" + + " --proto_fileformat=ops_list" + + " --graphs=" + "$(location " + list_ops_output + ") > \"$@\""), + ) + return struct(include_path = include_path, header = header) + +def tflite_flex_cc_library( + name, + models = [], + additional_deps = [], + testonly = 0, + visibility = ["//visibility:public"], + link_symbol = True, + compatible_with = None): + """A rule to generate a flex delegate with only ops to run listed models. + + Args: + name: Name of the generated flex delegate. + models: TFLite models to interpret. The library will only include ops and kernels + to support these models. If empty, the library will include all Tensorflow + ops and kernels. + additional_deps: Dependencies for additional TF ops. + testonly: Mark this library as testonly if true. + visibility: visibility of the generated rules. + link_symbol: If true, add delegate_symbol to deps. + compatible_with: The standard compatible_with attribute. + """ + portable_tensorflow_lib = clean_dep("//tensorflow/core:portable_tensorflow_lib") + if models: + CUSTOM_KERNEL_HEADER = generate_flex_kernel_header( + name = "%s_tf_op_headers" % name, + models = models, + additional_deps = additional_deps, + testonly = testonly, + ) + + # Define a custom tensorflow_lib with selective registration. + # The library will only contain ops exist in provided models. + cc_library( + name = "%s_tensorflow_lib" % name, + srcs = if_mobile([ + clean_dep("//tensorflow/core:portable_op_registrations_and_gradients"), + clean_dep("//tensorflow/core/kernels:portable_core_ops"), + clean_dep("//tensorflow/core/kernels:portable_extended_ops"), + ]) + [CUSTOM_KERNEL_HEADER.header], + copts = tf_copts(android_optimization_level_override = None) + tf_opts_nortti_if_lite_protos() + if_ios(["-Os"]), + compatible_with = compatible_with, + defines = [ + "SELECTIVE_REGISTRATION", + "SUPPORT_SELECTIVE_REGISTRATION", + "EIGEN_NEON_GEBP_NR=4", + ] + tf_portable_full_lite_protos( + full = [], + lite = ["TENSORFLOW_LITE_PROTOS"], + ) + tf_defines_nortti_if_lite_protos(), + features = tf_features_nomodules_if_mobile(), + linkopts = if_android(["-lz"]) + if_ios(["-lz"]), + includes = [ + CUSTOM_KERNEL_HEADER.include_path, + ], + textual_hdrs = [ + clean_dep("//tensorflow/core/kernels:portable_all_ops_textual_hdrs"), + ], + visibility = visibility, + deps = flex_portable_tensorflow_deps() + [ + clean_dep("@ducc//:fft_wrapper"), + clean_dep("//tensorflow/core:protos_all_cc"), + clean_dep("//tensorflow/core:portable_tensorflow_lib_lite"), + clean_dep("//tensorflow/core/platform:strong_hash"), + clean_dep("//tensorflow/lite/delegates/flex:portable_images_lib"), + ], + alwayslink = 1, + testonly = testonly, + ) + portable_tensorflow_lib = ":%s_tensorflow_lib" % name + + delegate_symbol = [] + if link_symbol: + delegate_symbol.append(clean_dep("//tensorflow/lite/delegates/flex:delegate_symbol")) + + # Define a custom flex delegate with above tensorflow_lib. + cc_library( + name = name, + hdrs = [ + clean_dep("//tensorflow/lite/delegates/flex:delegate.h"), + ], + compatible_with = compatible_with, + visibility = visibility, + deps = [ + clean_dep("//tensorflow/lite/delegates/flex:delegate_data"), + clean_dep("//tensorflow/lite/delegates/flex:delegate_only_runtime"), + clean_dep("//tensorflow/lite/delegates/utils:simple_delegate"), + ] + select({ + clean_dep("//tensorflow:android"): [ + portable_tensorflow_lib, + ], + clean_dep("//tensorflow:ios"): [ + portable_tensorflow_lib, + ], + clean_dep("//tensorflow:chromiumos"): [ + portable_tensorflow_lib, + ], + "//conditions:default": [ + clean_dep("//tensorflow/core:tensorflow"), + clean_dep("//tensorflow/lite/core/c:private_common"), + ], + }) + additional_deps + delegate_symbol, + testonly = testonly, + alwayslink = 1, + ) + +# LINT.ThenChange(//tensorflow/lite/delegates/flex/build_def.bzl) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/common/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/common/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..0305e35598824bedcd9730f78b588ae0505bb27a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/common/BUILD @@ -0,0 +1,22 @@ +load("@rules_cc//cc:cc_library.bzl", "cc_library") +load("//tensorflow:tensorflow.default.bzl", "get_compatible_with_portable") + +# copybara:uncomment package(default_applicable_licenses = ["//tensorflow:LICENSE"]) + +cc_library( + name = "outline_operations", + srcs = ["outline_operations.cc"], + hdrs = ["outline_operations.h"], + compatible_with = get_compatible_with_portable(), + visibility = ["//visibility:public"], + deps = [ + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/tensorflow:cluster_util", + "@com_google_absl//absl/strings", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/common/outline_operations.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/common/outline_operations.cc new file mode 100644 index 0000000000000000000000000000000000000000..175c75fc35912b2fb604dab63c63d71593660d1d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/common/outline_operations.cc @@ -0,0 +1,212 @@ +/* Copyright 2022 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/compiler/mlir/lite/experimental/common/outline_operations.h" + +#include +#include + +#include "absl/strings/str_cat.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SetVector.h" +#include "llvm/ADT/SmallVector.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/Block.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/Matchers.h" // from @llvm-project +#include "mlir/IR/OpDefinition.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/IR/Visitors.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/lite/utils/utils.h" +#include "tensorflow/compiler/mlir/tensorflow/utils/cluster_util.h" + +namespace mlir { +namespace TFL { +namespace common { + +bool IsConstantOrNone(Operation* op) { + return (op->getNumResults() == 1 && + mlir::isa(op->getResult(0).getType())) || + matchPattern(op, m_Constant()) || isa(op); +} + +// Pre-order traverse, adding results and BlockArgs to `been_defined` and +// collecting operands not contained within `been_defined`. If we encounter an +// operand that references a Value that has been defined (and added to +// `been_defined`) it is garuanteed that the Value definition is not contained +// in descedant node of reference, and given that the input DAG is valid, the +// definition is self-contained within `op` so it is not depended upon. +// Otherwise, the operand must have been defined somewhere above the Subgraph, +// so union with other operand dependencies. +llvm::SmallVector AccumulateOperandsDefinedAbove( + const llvm::SetVector& partition_ops) { + // Assuming that all are topologically sorted. + llvm::SetVector been_defined; + llvm::SetVector results; + auto update_from_op = [&](Operation* op) { + been_defined.insert(op->getResults().begin(), op->getResults().end()); + for (Value input : op->getOperands()) { + if (been_defined.contains(input)) { + continue; + } + results.insert(input); + } + }; + for (Operation* op : partition_ops) { + update_from_op(op); + op->walk([&](Block* nested_block) { + been_defined.insert(nested_block->getArguments().begin(), + nested_block->getArguments().end()); + for (Operation& op : nested_block->getOperations()) update_from_op(&op); + }); + } + return SmallVector(results.getArrayRef()); +} + +llvm::SmallVector AccumulateResultsDefinedWithin( + const llvm::SetVector& partition_ops) { + llvm::SmallVector values_for_results; + for (Operation* op : partition_ops) { + if (IsConstantOrNone(op)) { + continue; + } + for (Value output : op->getResults()) { + bool output_consumed_outside_subgraph = false; + for (Operation* consumer : output.getUsers()) { + if (llvm::all_of(partition_ops, [&](Operation* op) { + return !op->isAncestor(consumer); + })) { + output_consumed_outside_subgraph = true; + } + } + if (output_consumed_outside_subgraph) { + values_for_results.push_back(output); + } + } + } + return values_for_results; +} + +// Compute signature for raised func from arugments and outputs of +// Operation partition. +llvm::SmallVector TypesFromValues( + const llvm::SmallVector& values) { + llvm::SmallVector types; + for (auto value : values) { + types.push_back(value.getType()); + } + return types; +} + +func::FuncOp BuildFuncOp(const Subgraph& subgraph, OpBuilder& builder, + ModuleOp& module, OpsAdded& ops_added) { + // The parameters of the new MLIR function are taken to be the union + // of all operands referenced by Operations within the subraph. + // Likewise the results of the function are any Value(s) that are defined + // within the subgraph and are referenced outside the subgraph. + llvm::SmallVector input_types = + TypesFromValues(subgraph.FuncArguments()); + llvm::SmallVector return_types = + TypesFromValues(subgraph.FuncOutputs()); + + FunctionType function_type = + builder.getFunctionType(input_types, return_types); + + std::string function_name = absl::StrCat("func_", subgraph.subgraph_id_); + + func::FuncOp new_func = func::FuncOp::create(builder.getUnknownLoc(), + function_name, function_type); + new_func.setVisibility(func::FuncOp::Visibility::Private); + new_func.addEntryBlock(); + + // To form the body of the new function we need to clone each + // Operation along with its respective operands and result Values(s). + // The semantic of `Operation::clone` is copying given entity *into* this + // entity. The new FuncOp body is populated by cloning partitioned ops into + // it. Cloning Operation(s) will create cloned Value(s) for the results of a + // cloned op, but it needs a reference to the new operand Value(s) which are + // the result of the cloned ops. The approach is to traverse the subgraph in + // order, accumulating clones of defined Values into a `IRMapping` + // and pass that map to calls to clone ops. + OpBuilder function_builder(new_func.getBody()); + // Preferred data structure for mapping MLIR values. + IRMapping values_in_scope; + // Function arguments can appear as operands, so they clone should + // be aware of them. + assert(subgraph.FuncArguments().size() == new_func.getNumArguments()); + for (int i = 0; i < subgraph.FuncArguments().size(); ++i) { + Value original_value = subgraph.FuncArguments()[i]; + Value new_func_arg = new_func.getArgument(i); + values_in_scope.map(original_value, new_func_arg); + } + + for (Operation* op : subgraph.partition_ops_) { + function_builder.clone(*op, values_in_scope); + } + SmallVector return_operands; + for (Value result : subgraph.FuncOutputs()) { + Value cloned_output = values_in_scope.lookup(result); + return_operands.push_back(cloned_output); + } + mlir::func::ReturnOp::create(function_builder, new_func.getLoc(), + return_operands); + ops_added.func_op = new_func; + module.push_back(new_func); + return new_func; +} + +void ExtractSubgraphToFunc(const Subgraph& subgraph, OpBuilder& builder, + ModuleOp& module, OpsAdded& ops_added) { + func::FuncOp func = BuildFuncOp(subgraph, builder, module, ops_added); + + // We just use the location of the last ops in the subgraph as the location + // for the call_op. + Operation* last_output = subgraph.partition_ops_.back(); + + builder.setInsertionPoint(last_output); + auto call_op = func::CallOp::create(builder, last_output->getLoc(), func, + subgraph.FuncArguments()); + ops_added.call_op = call_op; + // FuncOutputs refer to the original `Values` in input module which are now + // invalid after pulling out the defining ops. The values in + // `call_ops.getResult` refer to the clones of original `Values` which are now + // returned by the new `FuncOp`. We can replace each in `FuncOutputs` with + // clone in `call_op` to fix up. + for (int i = 0; i < subgraph.FuncOutputs().size(); ++i) { + Value output = subgraph.FuncOutputs()[i]; + output.replaceAllUsesWith(call_op.getResult(i)); + } + + // Clear the subgraph. + // Those ops should be removed. + for (auto* op : subgraph.partition_ops_) { + if (IsConstantOrNone(op)) { + continue; + } + op->dropAllDefinedValueUses(); + op->dropAllReferences(); + op->erase(); + } + // Ensure that users of the call op's results appear after the launch op in + // order to preserve the dominance property. + TF::ReorderOpResultUses(call_op); +} + +} // namespace common +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/common/outline_operations.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/common/outline_operations.h new file mode 100644 index 0000000000000000000000000000000000000000..b7b90f72730e3cbdf427f96efe97dbbd9aaaee26 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/common/outline_operations.h @@ -0,0 +1,133 @@ +/* Copyright 2022 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_COMMON_OUTLINE_OPERATIONS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_COMMON_OUTLINE_OPERATIONS_H_ + +#include +#include +#include + +#include "llvm/ADT/SetVector.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/raw_os_ostream.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Block.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/OpDefinition.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/UseDefLists.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/IR/ValueRange.h" // from @llvm-project +#include "mlir/IR/Visitors.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/lite/utils/utils.h" + +namespace mlir { +namespace TFL { +namespace common { + +// Returns true if the `op` is a constant-like op or produces none type. +bool IsConstantOrNone(Operation* op); + +// Computes the list of Value(s) referenced by Subgraph Operations that are +// not defined within the Subgraph. Any such Value(s) +// are validly in-scope for the initial Operation. They must be either +// defined above the subgraph or appear as an argument to the containing func. +// These Value(s) are taken to be the arguments of the new raised func. +// An operand dependency is a Value referenced anywhere in an Op +// that is defined above the Op. All SSA Values are assigned/defined in a +// BlockArg or as a result of an Operation. +llvm::SmallVector AccumulateOperandsDefinedAbove( + const llvm::SetVector& partition_ops); + +// Similar to `AccumulateOperandsDefinedAbove()`, computes the Value(s) that are +// defined within a Subgraph and referenced in a descendant Operation. These +// Values(s) are to be returned by the new raised function. +llvm::SmallVector AccumulateResultsDefinedWithin( + const llvm::SetVector& partition_ops); + +// Represents a view of a set of mlir Operations that form a subgraph of the +// entire Module's DAG. `Subgraph` can be thought of as segment of sequential +// Operations within a func definition. Additional facts: +// 1. Subgraphs are restricted to a single Block. They do not span +// branching instructions. Thus the subgraph is a simple 1-degree path. +// 2. All Operations in a subgraph belong to the same block in a +// funtion body. +// 3. Function bodies are assumed to have only one block in some places. +class Subgraph { + // Set vector preserves insertion order, must insert Ops in topological order. + public: + const llvm::SetVector partition_ops_; + + // Subgraphs are given a unique incremented integer id based on when + // they were encountered in this pass. + const int subgraph_id_; + + const llvm::StringRef dialect_namespace_; + + Subgraph(const llvm::SetVector partition_ops, int num_subgraphs) + : partition_ops_(partition_ops), + subgraph_id_(num_subgraphs), + func_arguments_(AccumulateOperandsDefinedAbove(partition_ops)), + func_outputs_(AccumulateResultsDefinedWithin(partition_ops)) {} + + const llvm::SmallVector& FuncArguments() const { + // `Value`s in MLIR library are implemented as having "value semantics" + // see "llvm/llvm-project/mlir/include/mlir/IR/Value.h" so copying is fine. + return func_arguments_; + } + const llvm::SmallVector& FuncOutputs() const { return func_outputs_; } + + private: + // Compute once at construction and save as field. + const llvm::SmallVector func_arguments_; + const llvm::SmallVector func_outputs_; +}; + +// Helper data structure for output parameters to `ExtractSubgraphToFunc`. +// `ExtractSubgraphToFunc` adds exactly two "new" `Operations`, a FuncOp and +// a CallOp. Pass these back to the caller for setting more specific attributes +// after graph mutation has taken place. +struct OpsAdded { + mlir::func::FuncOp func_op; + mlir::func::CallOp call_op; +}; + +// Given a `Subgraph` containing a sequence of adjacent `Operations` from +// the `module`, raise these `Operations` (and any ops contained nested within) +// to the body of a new seperate root level function. Replace in their current +// location with a `CallOp` which invokes said `FuncOp`. The inputs to +// this new functions are taken to be the `Values` that appear as operands +// to ops in the subgraph, which are not self-contained within the subgraph. +// The outputs of this function are taken to be the results of ops in the +// subgraph which are referenced as operands outside of the subgraph. +// Also refer to documention of `AccumulateOperandsDefinedAbove` & +// `AccumulateResultsDefinedWithin`. +void ExtractSubgraphToFunc(const Subgraph& subgraph, OpBuilder& builder, + ModuleOp& module, OpsAdded& ops_added); + +} // namespace common +} // namespace TFL +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_COMMON_OUTLINE_OPERATIONS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/remat/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/remat/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..1eff78546105bb3e5d271fc39037709c2f23fb6b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/remat/BUILD @@ -0,0 +1,44 @@ +load("@rules_cc//cc:cc_library.bzl", "cc_library") +load("//tensorflow:tensorflow.bzl", "tf_cc_test") +load("//tensorflow:tensorflow.default.bzl", "get_compatible_with_portable") + +# copybara:uncomment package(default_applicable_licenses = ["//tensorflow:LICENSE"]) + +cc_library( + name = "rematerializer", + srcs = ["rematerializer.cc"], + hdrs = ["rematerializer.h"], + deps = [ + ], +) + +tf_cc_test( + name = "rematerializer_test", + size = "small", + srcs = ["rematerializer_test.cc"], + deps = [ + ":rematerializer", + "@com_google_googletest//:gtest_main", + ], +) + +cc_library( + name = "metadata_util", + srcs = ["metadata_util.cc"], + hdrs = ["metadata_util.h"], + compatible_with = get_compatible_with_portable(), + visibility = ["//visibility:public"], + deps = [ + "//tensorflow/compiler/mlir/lite:control_edges", + ], +) + +tf_cc_test( + name = "metadata_util_test", + size = "small", + srcs = ["metadata_util_test.cc"], + deps = [ + ":metadata_util", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/remat/metadata_util.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/remat/metadata_util.cc new file mode 100644 index 0000000000000000000000000000000000000000..d940113fad5861fc4f3c7b63a3ce0e07b43fa5bb --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/remat/metadata_util.cc @@ -0,0 +1,124 @@ +/* Copyright 2022 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/compiler/mlir/lite/experimental/remat/metadata_util.h" + +#include +#include +#include +#include +#include + +namespace { + +// We serialize unsigneds as protobuf varints, i.e., in chunks of 7 bits each. +constexpr int kMod = (1 << 7); + +void Serialize(std::string* out, uint32_t value) { + for (; value >= kMod; value /= kMod) { + out->push_back(value % kMod + kMod); + } + out->push_back(value); +} + +bool Parse(const char** data, size_t* size, uint32_t* out) { + *out = 0; + uint32_t mul = 1; + for (bool done = false; !done; + mul *= kMod, done = !(**data & kMod), ++*data, --*size) { + if (*size == 0) { + return false; + } + *out += static_cast(**data) % kMod * mul; + } + return true; +} + +// Signed ints are zigzag-encoded as unsigned varints, [..., -2, -1, 0, 1, 2, +// ...] -> +// [..., 3, 1, 0, 2, 4, ...]. +void Serialize(std::string* out, int32_t value) { + Serialize(out, static_cast( + value < 0 ? static_cast(-(value + 1)) * 2 + 1 + : static_cast(value) * 2)); +} + +bool Parse(const char** data, size_t* size, int32_t* out) { + uint32_t value = 0; + if (!Parse(data, size, &value)) { + return false; + } + const int32_t magnitude = value / 2; + *out = (value % 2) ? (-magnitude - 1) : magnitude; + return true; +} + +// Pairs are serialized as the concatenation of their elements' serialization. +template +void Serialize(std::string* out, const std::pair& in) { + Serialize(out, in.first); + Serialize(out, in.second); +} + +template +bool Parse(const char** data, size_t* size, std::pair* out) { + return Parse(data, size, &(out->first)) && Parse(data, size, &(out->second)); +} + +// Vectors are serialized as the concetation of the serialization of their size +// and the the serializations of their elements. +template +void Serialize(std::string* out, const std::vector& in) { + Serialize(out, static_cast(in.size())); + for (const auto& val : in) { + Serialize(out, val); + } +} + +template +bool Parse(const char** data, size_t* size, std::vector* out) { + uint32_t num_elems = 0; + if (!Parse(data, size, &num_elems)) { + return false; + } + out->assign(num_elems, T{}); + for (auto& elem : *out) { + if (!Parse(data, size, &elem)) { + return false; + } + } + return true; +} + +} // namespace + +namespace tflite { +std::string SerializeModelControlDependencies( + const ModelControlDependencies& in) { + std::string out; + Serialize(&out, kModelControlDependenciesMetadataVersion); + Serialize(&out, in); + return out; +} + +bool ParseModelControlDependencies(const char* data, size_t size, + ModelControlDependencies* out) { + out->clear(); + uint32_t version = 0; + return Parse(&data, &size, &version) && + (version == kModelControlDependenciesMetadataVersion) && + Parse(&data, &size, out) && (size == 0); +} + +} // namespace tflite diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/remat/metadata_util.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/remat/metadata_util.h new file mode 100644 index 0000000000000000000000000000000000000000..5a8763e348f1a6368685eb2caf4dc170939e8a17 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/remat/metadata_util.h @@ -0,0 +1,62 @@ +/* Copyright 2022 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. +==============================================================================*/ +/// \file +/// +/// Functions for serializiation/deserialization of control dependency +/// information to/from model metadata. +/// + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_REMAT_METADATA_UTIL_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_REMAT_METADATA_UTIL_H_ + +#include +#include +#include +#include + +#include "tensorflow/compiler/mlir/lite/utils/control_edges.h" + +namespace tflite { + +/// Control dependencies for the model is the collection of control dependencies +/// for its subgraphs. +using ModelControlDependencies = std::vector; + +/// Serializes `in` into the returned string. The result is parseable with +/// ParseModelControlDependencies. +std::string SerializeModelControlDependencies( + const ModelControlDependencies& in); + +/// Deserializes `*out` from a character buffer of size `size` at `data`. +/// Returns true iff successful. `*out` needn't be empty before invocation. +/// When returning false, `*out`'s state is undefined. +bool ParseModelControlDependencies(const char* data, size_t size, + ModelControlDependencies* out); + +/// The key under which to store the serialized control dependencies in the +/// model's metadata. +constexpr char kModelControlDependenciesMetadataKey[] = + "model_control_dependencies"; + +/// To allow future changes to the format, serialized control dependency data +/// will contain a version; this constant is the version that will be used for +/// serialization. For deserialization, past versions should remain parseable. +constexpr uint32_t kModelControlDependenciesMetadataVersion = 1; + +inline constexpr char kModelUseStablehloTensorKey[] = "keep_stablehlo_constant"; + +} // namespace tflite + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_REMAT_METADATA_UTIL_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/remat/metadata_util_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/remat/metadata_util_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..afe8f7da3d47a29c9f36a3c02d0ff9a4a2fb19e5 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/remat/metadata_util_test.cc @@ -0,0 +1,55 @@ +/* Copyright 2022 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/compiler/mlir/lite/experimental/remat/metadata_util.h" + +#include +#include +#include + +#include +#include + +namespace tflite { +namespace { + +class MetadataSerializerTest : public ::testing::Test { + protected: + static constexpr auto kHuge = std::numeric_limits::max(); + static constexpr auto kTiny = std::numeric_limits::min(); + + std::string RoundTrip(const ModelControlDependencies &in) const { + ModelControlDependencies out = {{{-1, -1}}}; + const std::string serialized = + tflite::SerializeModelControlDependencies(in); + return tflite::ParseModelControlDependencies(serialized.data(), + serialized.size(), &out) + ? (out == in) ? "ok" : "mismatch" + : "malformed"; + } +}; + +TEST_F(MetadataSerializerTest, nothing) { EXPECT_THAT(RoundTrip({}), "ok"); } + +TEST_F(MetadataSerializerTest, something) { + EXPECT_THAT( + RoundTrip({{{1, 2}, {2, 3}, {4, 5}}, + {}, + {{kHuge, kTiny}, {kTiny, kHuge}, {kHuge - 1, kTiny + 1}}, + {{1, 0}}}), + "ok"); +} + +} // namespace +} // namespace tflite diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/remat/rematerializer.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/remat/rematerializer.cc new file mode 100644 index 0000000000000000000000000000000000000000..6fb91689e2dcfa06cdadcbd12ec56940e60a75ab --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/remat/rematerializer.cc @@ -0,0 +1,381 @@ +/* Copyright 2023 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/compiler/mlir/lite/experimental/remat/rematerializer.h" + +#include +#include +#include +#include +#include + +namespace mlir { +namespace TFL { + +namespace { + +// Helper functions for sorted + deduped int vectors. + +std::tuple::iterator, bool> Find(const int item, + std::vector& items) { + const auto iter = std::lower_bound(items.begin(), items.end(), item); + return std::make_tuple(iter, iter != items.end() && *iter == item); +} + +void Insert(const int item, std::vector& items) { + const auto [iter, found] = Find(item, items); + if (!found) items.insert(iter, item); +} + +void Erase(const int item, std::vector& items) { + const auto [iter, found] = Find(item, items); + if (found) items.erase(iter); +} + +} // namespace + +int Rematerializer::AddOperation(const bool is_stateful) { + operations_.emplace_back(); + operations_.back().is_stateful = is_stateful; + return operations_.size() - 1; +} + +int Rematerializer::AddTensor(const SizeT size) { + tensors_.emplace_back(); + tensors_.back().size = size; + return tensors_.size() - 1; +} + +void Rematerializer::DelUse(const int ioperation, const int itensor) { + auto& tensor = tensors_[itensor]; + auto& operation = operations_[ioperation]; + + const auto& size = tensor.size; + + // Was the dependence to be deleted the first/last (or both) use of this + // tensor? + const bool was_first_use = + (!tensor.operations.empty() && ioperation == tensor.first_use()); + const bool was_last_use = + (!tensor.operations.empty() && ioperation == tensor.last_use()); + Erase(ioperation, tensor.operations); + Erase(itensor, operation.tensors); + if (was_first_use) { + operation.alloc -= size; + if (!was_last_use) { + operations_[tensor.first_use()].alloc += size; + } + } + if (was_last_use) { + operation.dealloc -= size; + if (!was_first_use) { + operations_[tensor.last_use()].dealloc += size; + } + } +} + +void Rematerializer::AddUse(const int ioperation, const int itensor) { + auto& tensor = tensors_[itensor]; + auto& operation = operations_[ioperation]; + + const auto& size = tensor.size; + + const bool will_be_first_use = + tensor.operations.empty() || ioperation < tensor.first_use(); + const bool will_be_last_use = + tensor.operations.empty() || ioperation > tensor.last_use(); + + if (will_be_first_use) { + operation.alloc += size; + if (!will_be_last_use) { + operations_[tensor.first_use()].alloc -= size; + } + } + if (will_be_last_use) { + operation.dealloc += size; + if (!will_be_first_use) { + operations_[tensor.last_use()].dealloc -= size; + } + } + Insert(ioperation, tensor.operations); + Insert(itensor, operation.tensors); +} + +Rematerializer::SizeT Rematerializer::MaxSavings(const int begin, const int end, + const int peak_loc) const { + SizeT max_savings = 0; + + // We're looking at the outputs of all operators in [`begin`, `end`). If an + // output is alive after `peak_loc`, rematerializing the operator anywhere + // after `peak_loc` would lower the memory profile at `peak_loc`. + for (int ioperation = begin; ioperation != end; ++ioperation) { + for (const int itensor : operations_[ioperation].tensors) { + if (const Tensor& tensor = tensors_[itensor]; + tensor.first_use() == ioperation /* output */ && + tensor.last_use() > peak_loc /* used later */) { + max_savings += tensor.size; + } + } + } + return max_savings; +} + +std::tuple +Rematerializer::FindBestRemat(const SizeT min_savings, const int begin_len, + const int end_len) const { + const auto peak = GetPeakMemory(); + SizeT best_peak_mem = peak.size; + RematSpec best_remat = {}; + + for (int len = begin_len; len < end_len; ++len) { + std::vector> pre_screen; + for (int begin = 0, end = begin + len; end <= peak.op_index; + ++begin, ++end) { + if (!std::any_of(operations_.begin() + begin, operations_.begin() + end, + [](const Operation& s) { return s.is_stateful; })) { + if (const auto max_savings = MaxSavings(begin, end, peak.op_index); + max_savings >= min_savings) { + pre_screen.emplace_back(max_savings, begin, end); + } + } + } + std::sort(pre_screen.begin(), pre_screen.end()); + + for (; !pre_screen.empty(); pre_screen.pop_back()) { + const auto& [max_savings, begin, end] = pre_screen.back(); + const auto insert_before = FindBestRematPoint(begin, end, peak.op_index); + if (insert_before == operations_.size()) { + continue; + } + const RematSpec this_remat = {begin, end, insert_before}; + if (const auto new_peak = GetPeakMemory(this_remat); + new_peak.size < best_peak_mem && + peak.size >= new_peak.size + min_savings) { + best_peak_mem = new_peak.size; + best_remat = this_remat; + } + + // If the actual savings achieved is bigger than the maximal savings that + // can be possibly achieved, leave early. + if (peak.size >= max_savings + best_peak_mem) { + break; + } + } + // We already found one savings for this length. + if (peak.size >= min_savings + best_peak_mem) { + break; + } + } + return std::make_tuple(best_peak_mem, best_remat); +} + +std::vector Rematerializer::GetDeltas( + const RematSpec& remat) const { + // We're cloning operations [remat.begin, remat.end) at position + // remat.insert. We store changes to the alloc/dealloc sizes due to the + // insertion in a vector `delta`: A change `c_alloc` of `operations_[i].alloc` + // as `delta[i] += c_alloc`, and a change `c_dealloc` of + // `operations_[i].dealloc` as `delta[i+1] -= c_dealloc`. + std::vector deltas; + + if (remat.begin == remat.end) { + return deltas; + } + + // Helper lambda: converts an operation index in the original range to its + // equivalent in the cloned range. + const auto source_to_target = [&](int i) { + return i + (remat.insert - remat.begin); + }; + + // Helper struct: bundles first and last use of a tensor within + // a contiguous range of operations. + struct TensorUse { + int first_use; + int last_use; + }; + + // For all tensors in the operation range to be cloned, store their first and + // last use within that range. This will be needed below to decide whether a + // tensor's life will be extended, or if it is to be rematerialized. + std::map source_uses; + for (int ioperation = remat.begin; ioperation < remat.end; ++ioperation) { + const auto& operation = operations_[ioperation]; + for (const int itensor : operation.tensors) { + // insertion will only happen for the first use. + const auto [iter, inserted] = source_uses.emplace( + itensor, + TensorUse{/*first_use=*/ioperation, /*last_use=*/ioperation}); + if (!inserted) { + // Otherwise, update last_use. + iter->second.last_use = ioperation; + } + } + } + + deltas.reserve(2 * source_uses.size()); + + for (const auto& [itensor, source] : source_uses) { + auto& tensor = tensors_[itensor]; + const TensorUse global = {tensor.first_use(), tensor.last_use()}; + + auto add_alloc = [&](int pos) { deltas.emplace_back(pos, tensor.size); }; + auto add_dealloc = [&](int pos) { + deltas.emplace_back(pos + 1, -tensor.size); + }; + auto del_dealloc = [&](int pos) { + deltas.emplace_back(pos + 1, tensor.size); + }; + + if (global.first_use < remat.begin) { + // The tensor is created before the source range. + if (global.last_use < remat.insert) { + // It currently gets deallocated before the newly inserted range, so we + // need to extend its lifetime: It will now be deallocated at its last + // use in the inserted range. + del_dealloc(global.last_use); + add_dealloc(source_to_target(source.last_use)); + } + } else { + // Tensor is created in the source range. It will be rematerialized in the + // cloned range. + add_alloc(source_to_target(source.first_use)); + if (global.last_use < remat.insert) { + // The last use of the original tensor is before the target range, so + // the lifetime of the rematerialized tensor ends with the target range. + add_dealloc(source_to_target(source.last_use)); + } else { + // There are uses of the original tensor after the cloned range. They + // can be replaced with uses of the rematerialized tensor, and the + // original tensor can be deallocated after its last use before the + // rematerialization. + add_dealloc(*std::partition_point( + tensor.operations.rbegin(), tensor.operations.rend(), + [&](int i) { return i >= remat.insert; })); + } + } + } + std::sort(deltas.begin(), deltas.end(), ByOpIndex); + return deltas; +} + +Rematerializer::MemProfile Rematerializer::GetMemProfile( + const RematSpec& remat) const { + const auto num_inserted = remat.end - remat.begin; + std::vector profile(operations_.size() + num_inserted); + MapMem([&](const MemSpec& m) { profile[m.op_index] = m.size; }, remat); + return profile; +} + +Rematerializer::MemSpec Rematerializer::GetPeakMemory( + const RematSpec& remat) const { + MemSpec peak; + MapMem([&](const MemSpec& m) { peak = std::max(m, peak, BySize); }, remat); + return peak; +} + +int Rematerializer::FindBestRematPoint(const int begin, const int end, + const int peak_loc) const { + // All tensors necessarily have their last use before or at the last operator + // (which is the yield operation of a function), so an unchanged best == + // operations_.size() value means that there is no valid configuration. + int best = operations_.size(); + + // All tensors whose first use is in [begin, end) and that are not destroyed + // until after peak_loc. + for (int ioperation = begin; ioperation < end; ++ioperation) { + for (const int itensor : operations_[ioperation].tensors) { + if (const auto& tensor = tensors_[itensor]; + tensor.first_use() >= begin && tensor.first_use() < end && + tensor.last_use() > peak_loc) { + for (const int ioperation : tensor.operations) { + // We return the earliest dependence on any output tensor. + if (ioperation > peak_loc && ioperation < best) { + best = ioperation; + break; + } + } + } + } + } + return best; +} + +void Rematerializer::Remat(const RematSpec& remat) { + const int num_inserted = remat.end - remat.begin; + // Rewrite all operation indices. + for (auto& tensor : tensors_) { + std::for_each(std::lower_bound(tensor.operations.begin(), + tensor.operations.end(), remat.insert), + tensor.operations.end(), + [&](int& iop) { iop += num_inserted; }); + } + operations_.insert(operations_.begin() + remat.insert, num_inserted, {}); + + // Copy all tensors dependencies of the old region to the new region. + // For any output tensor, a new tensor will be created. + std::vector> new_tensors; + for (int iop_old = remat.begin, iop_new = remat.insert; iop_old < remat.end; + ++iop_old, ++iop_new) { + for (const auto itensor : operations_[iop_old].tensors) { + if (tensors_[itensor].first_use() == iop_old) { + new_tensors.emplace_back(itensor, AddTensor(tensors_[itensor].size)); + } + AddUse(iop_new, itensor); + } + } + std::sort(new_tensors.begin(), new_tensors.end()); + + // Let all inserted + downstream operations refer to the new tensors. + for (int iop = remat.insert; iop < operations_.size(); ++iop) { + // We have to copy, otherwise we're modifying the set during the iteration. + for (const int old_tensor : std::vector(operations_[iop].tensors)) { + const auto new_tensor = + std::lower_bound(new_tensors.begin(), new_tensors.end(), + std::make_pair(old_tensor, 0)); + if (new_tensor != new_tensors.end() && new_tensor->first == old_tensor) { + DelUse(iop, old_tensor); + AddUse(iop, new_tensor->second); + } + } + } +} + +void Rematerializer::RunGreedyAlgorithm(const int max_cost, + const int max_block_length, + const SizeT min_savings) { + const bool unlimited_cost = (max_cost < 0); + for (int min_block_length = 1, cost = 0; + min_block_length <= max_block_length && + (unlimited_cost || cost <= max_cost); + min_block_length *= 2) { + while (unlimited_cost || cost <= max_cost) { + const auto [peak, remat] = FindBestRemat( + /*min_savings*/ min_savings, + /*begin_len=*/min_block_length, + /*end_len=*/ + std::min(1 + (unlimited_cost + ? max_block_length + : std::min(max_block_length, max_cost - cost)), + 2 * min_block_length)); + if (remat.begin == remat.end) break; + Remat(remat); + ApplyRemat(remat); + cost += (remat.end - remat.begin); + } + } +} + +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/remat/rematerializer.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/remat/rematerializer.h new file mode 100644 index 0000000000000000000000000000000000000000..32a9179ab7a320becd1d49dc3f6d757c818dc879 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/remat/rematerializer.h @@ -0,0 +1,262 @@ +/* Copyright 2023 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_REMAT_REMATERIALIZER_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_REMAT_REMATERIALIZER_H_ + +// This file declares the Rematerializer class, which is used by an MLIR-based +// set of transformations for TFLite IR that lower memory usage by redoing +// operations with small inputs and large outputs instead of keeping the result +// in memory. This class allows us to compactly and efficiently represent the +// (idealized) memory profile of a TFLite graph and simulate the effect of +// re-inserting operations on that memory profile. + +#include +#include +#include +#include +#include + +namespace mlir { +namespace TFL { + +// A class that +// (1) Encodes in concise form the memory requirements of a computational graph +// (2) Allows for the fast simulation of changes to the peak memory requirement +// under rematerialization of intermediate results in the graph +// (3) Implements a greedy algorithm for finding rematerializations of +// intermediate results in that graph to lower peak memory requirements. +class Rematerializer { + public: + Rematerializer() = default; + virtual ~Rematerializer() = default; + + // The type used for memory sizes (in bytes) and differences thereof. + using SizeT = int64_t; + + // The memory profile: The i-th element gives the amount of memory + // that is needed when performing the i-th operation. This is the + // sum of the sizes of + // + // (1) input tensors of that operation, + // (2) output tensors of that operation, + // (3) output tensors of preceding operations that are input tensors + // of subsequent operations. + using MemProfile = std::vector; + + // Used for specifying memory consumption at a certain operation in the + // computational graph. + struct MemSpec { + int op_index; // The index of the operation + SizeT size; // The amount of memory needed in order to execute this + // operation, i.e., the sum of input and output sizes and the + // sizes of outputs of previous operations that are needed as + // inputs of subsequent operations. + explicit MemSpec(int op_index = 0, SizeT size = 0) + : op_index(op_index), size(size) {} + }; + + static bool BySize(const MemSpec& a, const MemSpec& b) { + return std::tie(a.size, a.op_index) < std::tie(b.size, b.op_index); + } + + static bool ByOpIndex(const MemSpec& a, const MemSpec& b) { + return std::tie(a.op_index, a.size) < std::tie(b.op_index, b.size); + } + + // Specifies an elementary rematerialization operation: The operations in + // operations [`begin`, `end`) will be rescheduled before operation `insert`. + // A valid `RematSpec` requires begin <= end <= insert <= number of + // operations. Note that (1) `end` is exclusive -- begin == end signifies a + // trivial RematSpec (no operation will be rescheduled), (2) the + // zero-initialized RematSpec {} is trivial and always valid. + struct RematSpec { + int begin; + int end; + int insert; + }; + + // Gives the peak memory location and size after inserting operations + // according to `remat` (but doesn't actually insert them.) Ties are broken + // towards later locations. `remat` must be valid (see above). + MemSpec GetPeakMemory(const RematSpec& remat = {}) const; + + // Gives memory profile after inserting operations according to `remat` (but + // doesn't actually insert them). `remat` must be valid (see above). + MemProfile GetMemProfile(const RematSpec& remat = {}) const; + + // Runs the greedy incremental block algorithm: Finds a sequence of + // rematerializations of block size up to max_block_length, each reducing peak + // memory by at least min_savings. If max_cost >= 0, at most max_cost + // operations will be re-inserted. For each rematerialization found, + // ApplyRemat is invoked (which can be used to apply the rematerialization to + // the higher- level representation, e.g., MLIR, flatbuffer, ...) + void RunGreedyAlgorithm(int max_cost, int max_block_length, + SizeT min_savings); + + virtual void ApplyRemat(const RematSpec& remat) {} + + protected: + // Rematerializes the outputs of the operations [`remat.begin`, `remat.end`) + // before operation remat.insert by copying that operation range before + // remat.insert and updating tensor references so that any operation that can + // will make use of the rematerialized outputs rather than the original ones. + // `remat` must be valid (see above). + void Remat(const RematSpec& remat); + + // The protected methods below are to be used by derived classes to create the + // low-level (this class) representation from a high-level one. + + // Creates a new tensor-like object that takes `size` bytes. Returns a + // contiguous increasing index for each new object, starting at 0. + int AddTensor(SizeT size); + + // Creates an operation. If `is_stateful`, the operation (and any block of + // operations containing it) will never be considered for rematerialization. + // Returns a contiguous increasing index for each new object, starting at 0. + int AddOperation(bool is_stateful); + + // The operator with index `ioperation` will be assumed to produce and/or + // consume the tensor with index `itensor`. NoOp if that's already the case. + // The arguments must be valid indices (i.e., obtained with + // `AddOperation`/`AddTensor`). + void AddUse(int ioperation, int itensor); + + // Undoes an AddUse(ioperation, itensor). NoOp if there was no prior `AddUse`. + // The arguments must be valid indices (i.e., obtained with + // `AddOperation`/`AddTensor`). + void DelUse(int ioperation, int itensor); + + private: + // Find the best remat operation that saves at least `min_savings` bytes for a + // block of operators with a length is between [`begin_len`, `end_len`). + // 'Best' means with the highest savings, ties are broken towards shorter + // blocks. + std::tuple FindBestRemat(SizeT min_savings, int begin_len, + int end_len) const; + + // Optimization: Estimate (from above) the remat savings of instruction block + // [begin, end) after operation `peak_location` + SizeT MaxSavings(int begin, int end, int peak_loc) const; + + // If I want to remat ops [begin, end) after the op at operation `peak_loc`, + // find the latest point at which to reinsert them (the op before which to + // insert.) + int FindBestRematPoint(int begin, int end, int peak_loc) const; + + // The memory objects. + struct Tensor { + SizeT size; // The size of the object (in bytes.) + std::vector operations; // The operations it is used in. This vector + // is kept sorted + unique. + + // The operation that makes the first use of this tensor. + int first_use() const { return *operations.begin(); } + + // The operation that makes the last use of this tensor. + int last_use() const { return *operations.rbegin(); } + }; + + // The operators. + struct Operation { + bool is_stateful = false; // Results of an Operation can be rematerialized + // only if `!is_stateful`. This probably should + // be replaced with a more-fine grained + // approach--for example, the results of a "read + // resource variable" operation can be + // rematerialized as long as this doesn't happen + // after the corresponding "write resource + // variable" operation. + + std::vector tensors; // The tensors that are used (input or output) by + // this operation. They needn't correspond to + // tensors in the TF graph -- we may add fake + // tensors to model memory consumed in addition + // to input and output tensors. This vector is + // kept sorted + unique. + + SizeT alloc = 0; // The number of bytes that need to be allocated before + // this operation. + SizeT dealloc = 0; // The number of bytes that can be deallocated after + // this operation. + }; + + // Given the current state of `operations_` and `tensors_`, return a vector of + // corrections that transform the current memory profile into the one that we + // would get after applying `remat`. + // + // The memory profile of a sequence of operations is the partial sum of the + // sizes of the allocations that are necessary before an operation and the + // negative sizes of the deallocations that are possible after the previous + // operation. + // + // If we modify the operation sequence by cloning an operation range, that + // memory profile will change--cloning makes it necessary to extend the + // lifetime of some tensors, while other tensors can be deallocated early and + // rematerialized later. + // + // This method represents these changes in compact form: It returns a vector + // of (position of operation, delta) pairs in lexicographic order; one + // obtains the memory profile after `remat` by adding the deltas from any + // entries (i, delta) to the i-th entry of the partial sum. + // + // This allows us to efficiently compute the change to the peak of a memory + // profile due to cloning an operation range without having to actually clone + // that range and without having to build a profile vector. + // + // The returned vector has at most 2 entries for each tensor referenced in + // [remat.begin, remat.end). There may be multiple entries for a single + // operation position; operation positions refer to the sequence *after* + // cloning [`remat.begin`, `remat.end`) before `remat.insert`. + std::vector GetDeltas(const RematSpec& remat) const; + + // Helper template: Iterates through all `MemSpec`s (i.e., operation + // index/memory usage pairs) for the current graph in operation order and + // calls `mapper` on them. This is an optimization -- by instantiating with an + // appropriate `Mapper`, it allows us to e.g. compute the peak memory without + // having to instantiate an actual memory profile vector. + template + void MapMem(const Mapper& mapper, const RematSpec& remat) const { + const auto deltas = GetDeltas(remat); + const auto len = (remat.end - remat.begin); + auto idelta = deltas.begin(); + + for (MemSpec m; m.op_index < operations_.size() + len; ++m.op_index) { + // Are we in the cloned portion of the new operation sequence? + // Then all alloc/dealloc information must come from deltas. + const bool patch = + (m.op_index >= remat.insert) && (m.op_index < remat.insert + len); + // Are we past the insertion portion of the new operation sequence? + // Then we need to convert indices back to the original sequence. + const int shift = (m.op_index >= remat.insert + len) ? len : 0; + m.size += patch ? 0 : operations_[m.op_index - shift].alloc; + // deltas is sorted by location; apply any corrections to the current + // operator. + for (; idelta != deltas.end() && idelta->op_index == m.op_index; + ++idelta) { + m.size += idelta->size; + } + mapper(m); + m.size -= patch ? 0 : operations_[m.op_index - shift].dealloc; + } + } + + std::vector operations_; + std::vector tensors_; +}; + +} // namespace TFL +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_REMAT_REMATERIALIZER_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/remat/rematerializer_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/remat/rematerializer_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..4e2ef1d2471f656374cc3698e9060c6460f78705 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/remat/rematerializer_test.cc @@ -0,0 +1,519 @@ +/* Copyright 2023 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/compiler/mlir/lite/experimental/remat/rematerializer.h" + +#include +#include +#include +#include +#include +#include + +#include +#include + +namespace mlir { +namespace TFL { + +namespace { + +using ::testing::ElementsAre; +using ::testing::ElementsAreArray; +using ::testing::Eq; +using ::testing::FieldsAre; +using ::testing::StrictMock; + +class RematTest : public ::testing::Test { + protected: + class TestableRematerializer : public Rematerializer { + public: + using Rematerializer::AddOperation; + using Rematerializer::AddTensor; + using Rematerializer::AddUse; + using Rematerializer::DelUse; + using Rematerializer::Remat; + }; + TestableRematerializer r_; +}; + +TEST_F(RematTest, TensorUseSimple) { + for (int i = 0; i < 6; ++i) { + r_.AddOperation(/*is_stateful=*/false); + r_.AddTensor(/*size=*/1 << i); + } + + r_.AddUse(/*ioperation=*/2, /*itensor=*/2); + EXPECT_THAT(r_.GetMemProfile(), ElementsAre(0, 0, 4, 0, 0, 0)); + EXPECT_THAT(r_.GetPeakMemory(), FieldsAre(Eq(2), Eq(4))); + + r_.AddUse(/*ioperation=*/2, /*itensor=*/2); + EXPECT_THAT(r_.GetMemProfile(), ElementsAre(0, 0, 4, 0, 0, 0)); + EXPECT_THAT(r_.GetPeakMemory(), FieldsAre(Eq(2), Eq(4))); + + r_.AddUse(/*ioperation=*/4, /*itensor=*/2); + EXPECT_THAT(r_.GetMemProfile(), ElementsAre(0, 0, 4, 4, 4, 0)); + EXPECT_THAT(r_.GetPeakMemory(), FieldsAre(Eq(4), Eq(4))); + + r_.DelUse(/*ioperation=*/2, /*itensor=*/2); + EXPECT_THAT(r_.GetMemProfile(), ElementsAre(0, 0, 0, 0, 4, 0)); + EXPECT_THAT(r_.GetPeakMemory(), FieldsAre(Eq(4), Eq(4))); + + r_.DelUse(/*ioperation=*/2, /*itensor=*/2); + EXPECT_THAT(r_.GetMemProfile(), ElementsAre(0, 0, 0, 0, 4, 0)); + EXPECT_THAT(r_.GetPeakMemory(), FieldsAre(Eq(4), Eq(4))); + + r_.DelUse(/*ioperation=*/4, /*itensor=*/2); + EXPECT_THAT(r_.GetMemProfile(), ElementsAre(0, 0, 0, 0, 0, 0)); + EXPECT_THAT(r_.GetPeakMemory(), FieldsAre(Eq(5), Eq(0))); +} + +TEST_F(RematTest, TensorUseMany) { + constexpr int n = 6; + for (int i = 0; i < n; ++i) { + r_.AddUse(/*ioperation=*/r_.AddOperation(/*is_stateful=*/false), + /*itensor=*/r_.AddTensor(1 << (n - i - 1))); + } + for (int i = 0; i < n; ++i) { + r_.AddUse(/*ioperation=*/r_.AddOperation(/*is_stateful=*/false), + /*itensor=*/n - 1 - i); + } + + EXPECT_THAT(r_.GetMemProfile(), ElementsAreArray({32, 48, 56, 60, 62, 63, 63, + 62, 60, 56, 48, 32})); + EXPECT_THAT(r_.GetPeakMemory(), FieldsAre(Eq(6), Eq(63))); +} + +TEST_F(RematTest, PeakTiesAreBrokenInFavorOfLaterOperations) { + r_.AddUse(/*ioperation=*/r_.AddOperation(/*is_stateful=*/false), + /*itensor=*/r_.AddTensor(100)); + r_.AddUse(/*ioperation=*/r_.AddOperation(/*is_stateful=*/false), + /*itensor=*/r_.AddTensor(1)); + r_.AddUse(/*ioperation=*/r_.AddOperation(/*is_stateful=*/false), + /*itensor=*/r_.AddTensor(100)); + ASSERT_THAT(r_.GetMemProfile(), ElementsAreArray({100, 1, 100})); + EXPECT_THAT(r_.GetPeakMemory(), FieldsAre(Eq(2), Eq(100))); +} + +TEST_F(RematTest, RematRecreatesOutput) { + r_.AddUse(r_.AddOperation(/*is_stateful=*/false), r_.AddTensor(100)); + r_.AddOperation(/*is_stateful=*/false); + // /* before: */ + // %0 = f1() + // f2() + ASSERT_THAT(r_.GetMemProfile(), ElementsAre(100, 0)); + + EXPECT_THAT(r_.GetMemProfile({/*begin=*/0, /*end=*/1, /*insert=*/2}), + ElementsAre(100, 0, 100)); + r_.Remat({/*begin=*/0, /*end=*/1, /*insert=*/2}); + // /* after: */ + // %0 = f1() + // f2() + // %1 = f1() + EXPECT_THAT(r_.GetMemProfile(), ElementsAre(100, 0, 100)); + EXPECT_THAT(r_.AddTensor(0), 2); +} + +TEST_F(RematTest, RematExtendsInputAndRecreatesOutput) { + r_.AddUse(/*ioperation=*/r_.AddOperation(/*is_stateful=*/false), + /*itensor=*/r_.AddTensor(1)); + r_.AddUse(/*ioperation=*/r_.AddOperation(/*is_stateful=*/false), + /*itensor=*/r_.AddTensor(100)); + r_.AddUse(/*ioperation=*/1, 0); + r_.AddOperation(/*is_stateful=*/false); + r_.AddOperation(/*is_stateful=*/false); + + // /* before: */ + // %0 = f1() + // %1 = f2(%0) + // f3() + // f4() + + ASSERT_THAT(r_.GetMemProfile(), ElementsAre(1, 101, 0, 0)); + + EXPECT_THAT(r_.GetMemProfile({/*begin=*/1, /*end=*/2, /*insert=*/3}), + ElementsAre(1, 101, 1, 101, 0)); + r_.Remat({/*begin=*/1, /*end=*/2, /*insert=*/3}); + + // /* after: */ + // %0 = f1() + // %1 = f2(%0) + // f3() /* %0 is kept alive */ + // %2 = f2(%0) + // f4() + EXPECT_THAT(r_.GetMemProfile(), ElementsAre(1, 101, 1, 101, 0)); + EXPECT_THAT(r_.AddTensor(0), 3); +} + +TEST_F(RematTest, BlockRematDuplicatesIntraBlockValues) { + r_.AddUse(/*ioperation=*/r_.AddOperation(/*is_stateful=*/false), + /*itensor=*/r_.AddTensor(1)); + r_.AddUse(/*ioperation=*/r_.AddOperation(/*is_stateful=*/false), + /*itensor=*/r_.AddTensor(10)); + r_.AddUse(/*ioperation=*/r_.AddOperation(/*is_stateful=*/false), + /*itensor=*/r_.AddTensor(100)); + r_.AddUse(/*ioperation=*/r_.AddOperation(/*is_stateful=*/false), + /*itensor=*/r_.AddTensor(1000)); + r_.AddOperation(/*is_stateful=*/false); + r_.AddUse(/*ioperation=*/1, /*itensor=*/0); + r_.AddUse(/*ioperation=*/2, /*itensor=*/0); + r_.AddUse(/*ioperation=*/2, /*itensor=*/1); + r_.AddUse(/*ioperation=*/3, /*itensor=*/0); + r_.AddUse(/*ioperation=*/3, /*itensor=*/1); + r_.AddUse(/*ioperation=*/3, /*itensor=*/2); + + // /* before */ + // %0 = f1() + // %1 = f2(%0) + // %2 = f3(%0,%1) + // %3 = f4(%0,%1,%2) + // f5() + ASSERT_THAT(r_.GetMemProfile(), ElementsAre(1, 11, 111, 1111, 0)); + + EXPECT_THAT(r_.GetMemProfile({/*begin=*/1, /*end=*/4, /*insert=*/5}), + ElementsAre(1, 11, 111, 1111, 1, 11, 111, 1111)); + + r_.Remat({/*begin=*/1, /*end=*/4, /*insert=*/5}); + + EXPECT_THAT(r_.GetMemProfile(), + ElementsAre(1, 11, 111, 1111, 1, 11, 111, 1111)); + EXPECT_THAT(r_.AddTensor(0), 7); + // /* after */ + // %0 = f1() + // %1 = f2(%0) + // %2 = f3(%0,%1) + // %3 = f4(%0,%1,%2) + // f5() + // %4 = f2(%0) + // %5 = f3(%0,%4) + // %6 = f4(%0,%4,%5) +} + +class RematSimulationTest : public testing::Test { + protected: + class RandomRemat : public Rematerializer { + public: + using Rematerializer::Remat; // For testing. + RandomRemat(const int num_operations, const int num_tensors, + const int num_uses, std::mt19937& rng) { + std::uniform_int_distribution some_size_log(0, 16); + std::uniform_int_distribution some_tensor(0, num_tensors - 1); + std::uniform_int_distribution some_operation(0, num_operations - 1); + + for (int i = 0; i < num_tensors; ++i) { + AddTensor(SizeT{1} << some_size_log(rng)); + } + for (int i = 0; i < num_operations; ++i) { + AddOperation(/*is_stateful=*/false); + } + for (int i = 0; i < num_uses; ++i) { + AddUse(some_operation(rng), some_tensor(rng)); + } + } + }; +}; + +TEST_F(RematSimulationTest, SimulationAgreesWithReality) { + constexpr int kNumOperations = 128; + constexpr int kNumTensors = 32; + constexpr int kNumUses = kNumOperations * kNumTensors / 4; + + std::mt19937 rng; + for (int i = 0; i < 1024; ++i) { + RandomRemat remat(kNumOperations, kNumTensors, kNumUses, rng); + // Worst-case scenario: we might double the length of the computation + // schedule each time we remat, so only a few iterations... + std::array randos; + const auto& [begin, end, insert] = randos; + for (int i = 0, num_operations = kNumOperations; i < 4; + ++i, num_operations += end - begin) { + std::uniform_int_distribution some_op(0, num_operations - 1); + for (auto& rando : randos) { + rando = some_op(rng); + } + // We need begin <= end <= insert. + std::sort(randos.begin(), randos.end()); + const Rematerializer::RematSpec spec{begin, end, insert}; + const auto simulated_profile = remat.GetMemProfile(spec); + remat.Remat(spec); + const auto actual_profile = remat.GetMemProfile(); + EXPECT_THAT(simulated_profile, ElementsAreArray(actual_profile)); + } + } +} + +class GreedyRematTest : public testing::Test { + protected: + // A Rematerializer for a graph whose memory profile is a sequence of + // "rainbows" due to nested ops (similar to what happens in backwards pass + // gradient calculations.) + class RainbowRemat : public Rematerializer { + public: + // `sizes` is a vector of vector of sizes. Each sizes vector of length n + // generates a `rainbow` profile of the SSA form + // + // %1 = f1() + // %2 = f2() + // ... + // %n = fn() + // gn(%n) + // ... + // g2(%2) + // g1(%1) + // + // with the sizes of intermediates %1,...%n given by the sizes entries. + // + // If extra_ops > 0, each assignment above becomes a sequence + // %i.1 = fi.1() + // %i.2 = fi.1(%i.1) + // %i.3 = fi.1(%i.2) + // ... + // %i = f1(%i.m) + // where the 'dotted' intermediates have size `extra_size`. This + // allows for testing the effect of window sizes. + explicit RainbowRemat(const std::vector>& sizes, + int extra_ops = 0, SizeT extra_size = 0) { + for (const auto& rainbow : sizes) { + int tensor = 0; + int op = 0; + for (const auto& size : rainbow) { + for (int i = 0; i < extra_ops; ++i) { + op = AddOperation(/*is_stateful=*/false); + if (i != 0) { + AddUse(op, tensor); + } + tensor = AddTensor(extra_size); + AddUse(op, tensor); + } + // using negative sizes to signal forbidden operations. + op = AddOperation(/*is_stateful=*/size < 0); + if (extra_ops > 0) { + AddUse(op, tensor); + } + tensor = AddTensor(std::abs(size)); + AddUse(op, tensor); + } + for (int i = 0; i < rainbow.size(); ++i) { + op = AddOperation(/*is_stateful=*/false); + AddUse(op, tensor - i); + } + } + } + }; + + // Similar to above: A multilayer perceptron. The forward pass is + // f[n](f[n-1](...(f[1]())), with dimensions |f[1]|...|f[n]| handed in the + // constructor. The backward pass calculates the loss gradient for a single + // weight in the first layer. + class MlpRemat : public Rematerializer { + public: + explicit MlpRemat(const std::vector& sizes) { + int forward_tensor = -1; + int backward_tensor = -1; + int op = -1; + // Forward pass: + for (const int size : sizes) { + op = AddOperation(/*is_stateful=*/false); + if (forward_tensor >= 0) AddUse(op, forward_tensor); + forward_tensor = AddTensor(size); + AddUse(op, forward_tensor); + } + + // Backward pass: Right-multiply the jacobian from the outside in. + // dLoss/df[n] * df[n]/df[n-1] * ... + // The i-th term g[i] depends on g[i-1] and f[n-i] and has size |f[n-i]|. + for (; forward_tensor >= 0; --forward_tensor) { + op = AddOperation(/*is_stateful=*/false); + AddUse(op, forward_tensor); + if (backward_tensor >= 0) AddUse(op, backward_tensor); + backward_tensor = AddTensor(sizes[forward_tensor]); + AddUse(op, backward_tensor); + } + } + // We will also instrument the actual optimizations performed. + MOCK_METHOD(void, ApplyRemat, (const RematSpec&)); + }; +}; + +TEST_F(GreedyRematTest, MlpBasic) { + StrictMock remat(std::vector({1, 1, 1})); + // (o)ut, (i)n, (l)ive: 0 1 2 3 4 5 Sum + // %0 = f0() o 1 + // %1 = f1(%0) i o 2 + // %2 = f2(%1) l i o 3 + // %3 = g2( %2) l l i o 4 + // %4 = g1(%3,%1) l i i o 4 + // %5 = g0(%4,%0) i i o 3 + ASSERT_THAT(remat.GetMemProfile(), ElementsAreArray({1, 2, 3, 4, 4, 3})); + + // Little we can do here -- remat %0 before %5 + EXPECT_CALL(remat, ApplyRemat(FieldsAre(/*begin=*/0, + /*end=*/1, + /*insert=*/5))); + // (o)ut, (i)n, (l)ive: 0 1 2 3 4 5 Sum of live sizes + // %0 = f0() o 1 + // %1 = f1(%0) i o 2 + // %2 = f2(%1) i o 2 + // %3 = g2( %2) l i o 3 + // %4 = g1(%3,%1) i i o 3 + // %0' = f0() o l 2 + // %5 = g0(%4,%0') i i o 3 + remat.RunGreedyAlgorithm(/*max_cost=*/-1, /*max_block_length=*/1, + /*min_savings=*/1); + + EXPECT_THAT(remat.GetMemProfile(), ElementsAreArray({1, 2, 2, 3, 3, 2, 3})); +} + +TEST_F(GreedyRematTest, MlpBinary) { + StrictMock remat(std::vector({1, 2, 4, 8})); + // (o)ut, (i)n, (l)ive: 0 1 2 3 4 5 6 7 Sum of live sizes + // %0 = f0() o 1 + // %1 = f1(%0) i o 3 + // %2 = f2(%1) l i o 7 + // %3 = f3(%2) l l i o 15 + // %4 = g3( %3) l l l i o 23 + // %5 = g2(%4 %2) l l i i o 19 + // %6 = g1(%5,%1) l i i o 9 + // %7 = g0(%6,%0) i i o 4 + ASSERT_THAT(remat.GetMemProfile(), + ElementsAreArray({1, 3, 7, 15, 23, 19, 9, 4})); + + EXPECT_CALL(remat, ApplyRemat(FieldsAre(/*begin=*/2, + /*end=*/3, + /*insert=*/5))); + EXPECT_CALL(remat, ApplyRemat(FieldsAre(/*begin=*/0, + /*end=*/1, + /*insert=*/8))); + + // (o)ut, (i)n, (l)ive: 0 1 2 3 4 5 6 7 Sum of live sizes + // %0 = f0() o 1 + // %1 = f1(%0) i o 3 + // %2 = f2(%1) i o 6 + // %3 = f3(%2) l i o 14 + // %4 = g3( %3) l i o 18 + // %2'= f2(%1) l o l 14 + // %5 = g2(%4 %2) l i i o 18 + // %6 = g1(%5,%1) i i o 8 + // %0'= f(0) o l 3 + // %7 = g0(%6,%0) i i o 4 + remat.RunGreedyAlgorithm(/*max_cost=*/-1, /*max_block_length=*/4, + /*min_savings=*/1); + EXPECT_THAT(remat.GetMemProfile(), + ElementsAreArray({1, 3, 6, 14, 18, 14, 18, 8, 3, 4})); +} + +TEST_F(GreedyRematTest, SimpleMax) { + RainbowRemat remat({{1, 2, 4, 8, 16}}); + ASSERT_THAT(remat.GetMemProfile(), + ElementsAreArray({1, 3, 7, 15, 31, 31, 15, 7, 3, 1})); + remat.RunGreedyAlgorithm(/*max_cost=*/-1, /*max_block_length=*/1, + /*min_savings=*/1); + // Profile is flattened to its minimum--16. + EXPECT_THAT(remat.GetMemProfile(), + ElementsAreArray({1, 2, 4, 8, 16, 16, 8, 8, 4, 4, 2, 2, 1, 1})); +} + +TEST_F(GreedyRematTest, SimpleMaxLongWindow) { + RainbowRemat remat({{1, 2, 4, 8, 16}}); + ASSERT_THAT(remat.GetMemProfile(), + ElementsAreArray({1, 3, 7, 15, 31, 31, 15, 7, 3, 1})); + remat.RunGreedyAlgorithm(/*max_cost=*/-1, /*max_block_length=*/4, + /*min_savings=*/1); + // Profile is flattened to its minimum--16. + EXPECT_THAT(remat.GetMemProfile(), + ElementsAreArray({1, 2, 4, 8, 16, 16, 8, 8, 4, 4, 2, 2, 1, 1})); +} + +TEST_F(GreedyRematTest, SimpleSizeThreshold) { + RainbowRemat remat({{1, 2, 4, 8, 16}}); + ASSERT_THAT(remat.GetMemProfile(), + ElementsAreArray({1, 3, 7, 15, 31, 31, 15, 7, 3, 1})); + // Only do remats of at least 4 bytes of savings -- this will lower the + // profile by 4 + 8 instead of 1 + 2 + 4 + 8 as before. + remat.RunGreedyAlgorithm(/*max_cost=*/-1, /*max_block_length=*/1, + /*min_savings=*/4); + EXPECT_THAT(remat.GetMemProfile(), + ElementsAreArray({1, 3, 7, 11, 19, 19, 11, 11, 7, 7, 3, 1})); +} + +TEST_F(GreedyRematTest, SimpleCostThreshold) { + RainbowRemat remat({{1, 2, 4, 8, 16}}); + ASSERT_THAT(remat.GetMemProfile(), + ElementsAreArray({1, 3, 7, 15, 31, 31, 15, 7, 3, 1})); + // Do at most 1 remat -- this will lower the profile by 8. + remat.RunGreedyAlgorithm(/*max_cost=*/1, /*max_block_length=*/1, + /*min_savings=*/1); + // Only as single remat is done -- this will be the best one possible, + // lowering the profile by 8 instead of the maximum 1 + 2 + 4 + 8. + EXPECT_THAT(remat.GetMemProfile(), + ElementsAreArray({1, 3, 7, 15, 23, 23, 15, 15, 7, 3, 1})); +} + +TEST_F(GreedyRematTest, SimpleForbiddenOps) { + // Operator generating size-4 tensor is stateful, so it won't be materialized. + RainbowRemat remat({{1, 2, -4, 8, 16}}); + ASSERT_THAT(remat.GetMemProfile(), + ElementsAreArray({1, 3, 7, 15, 31, 31, 15, 7, 3, 1})); + // Best we can do is lower the profile to 8 + 2 + 1. + remat.RunGreedyAlgorithm(/*max_cost=*/-1, /*max_block_length=*/1, + /*min_savings=*/1); + EXPECT_THAT(remat.GetMemProfile(), + ElementsAreArray({1, 2, 4, 12, 20, 20, 12, 12, 4, 2, 2, 1, 1})); +} + +TEST_F(GreedyRematTest, DoubleMax) { + // Generate a profile with two local maxima of size 31 and 28. + RainbowRemat remat({{1, 2, 4, 8, 16}, {4, 8, 16}}); + ASSERT_THAT(remat.GetMemProfile(), + ElementsAreArray( + {1, 3, 7, 15, 31, 31, 15, 7, 3, 1, 4, 12, 28, 28, 12, 4})); + remat.RunGreedyAlgorithm(/*max_cost=*/-1, /*max_block_length=*/1, + /*min_savings=*/1); + // Two rainbows; the first is lowered by 1 + 2 + 4 + 8 == 15, the second by 4 + // + 8 == 12. + EXPECT_THAT(remat.GetMemProfile(), + ElementsAreArray({1, 2, 4, 8, 16, 16, 8, 8, 4, 4, 2, + 2, 1, 1, 4, 8, 16, 16, 8, 8, 4, 4})); +} + +TEST_F(GreedyRematTest, DoubleCostThreshold) { + // Generate a profile with two local maxima of size 31 and 28. + RainbowRemat remat({{1, 2, 4, 8, 16}, {4, 8, 16}}); + ASSERT_THAT(remat.GetMemProfile(), + ElementsAreArray( + {1, 3, 7, 15, 31, 31, 15, 7, 3, 1, 4, 12, 28, 28, 12, 4})); + remat.RunGreedyAlgorithm(/*max_cost=*/2, /*max_block_length=*/1, + /*min_savings=*/1); + // Profile can be flattened twice--first, the global maximum of 31 is reduced + // by 8; then the newly-global maximum of 28 is reduced by 8. + EXPECT_THAT(remat.GetMemProfile(), + ElementsAreArray({1, 3, 7, 15, 23, 23, 15, 15, 7, 3, 1, 4, 12, 20, + 20, 12, 12, 4})); +} + +TEST_F(GreedyRematTest, SingleLongerBlocksByWindowSize) { + std::vector best_for_window_size; + for (int window_size : {0, 1, 2, 3, 4, 5}) { + RainbowRemat remat({{1, 2, 4, 8}}, 2, 16); + remat.RunGreedyAlgorithm(/*max_cost=*/-1, /*max_block_length=*/window_size, + /*min_savings=*/1); + best_for_window_size.push_back(remat.GetPeakMemory().size); + } + EXPECT_THAT(best_for_window_size, ElementsAreArray({44, 36, 36, 32, 32, 32})); +} + +} // namespace + +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..9edb7358051d0680ff2be986835d7fa8a40bbf41 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/BUILD @@ -0,0 +1,399 @@ +load("@flatbuffers//:build_defs.bzl", "flatbuffer_cc_library") +load( + "@llvm-project//mlir:tblgen.bzl", + "gentbl_cc_library", +) +load("@xla//third_party/rules_python/python:py_library.bzl", "py_library") +load("//tensorflow:tensorflow.bzl", "if_google", "tf_cc_binary", "tf_cc_test") +load("//tensorflow:tensorflow.default.bzl", "get_compatible_with_portable") +load( + "//tensorflow/core/platform:build_config.bzl", + "tf_proto_library", +) +load("//tensorflow/core/platform:rules_cc.bzl", "cc_library") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + default_visibility = [ + "//visibility:public", + ], + licenses = ["notice"], +) + +flatbuffer_cc_library( + name = "runtime_metadata_fbs", + srcs = ["runtime_metadata.fbs"], + compatible_with = get_compatible_with_portable(), +) + +cc_library( + name = "execution_metadata_exporter", + srcs = [ + "execution_metadata_exporter.cc", + ], + hdrs = [ + "execution_metadata_exporter.h", + ], + deps = [ + ":common", + ":runtime_metadata_fbs", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/lite/experimental/tac/hardwares:target_hardware", + "//tensorflow/compiler/mlir/lite/quantization/ir:QuantOps", + "//tensorflow/compiler/mlir/tensorflow", + "@flatbuffers", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + ], +) + +tf_cc_test( + name = "execution_metadata_exporter_test", + srcs = [ + "execution_metadata_exporter_test.cc", + ], + deps = [ + ":execution_metadata_exporter", + ":runtime_metadata_fbs", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/lite/experimental/tac/hardwares:all-target-hardwares", + "@com_google_googletest//:gtest_main", + "@flatbuffers", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Parser", + ], +) + +cc_library( + name = "common", + srcs = [ + "common/utils.cc", + ], + hdrs = [ + "common/cost.h", + "common/subgraph.h", + "common/targets.h", + "common/utils.h", + ], + deps = [ + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:BytecodeOpInterface", + "@llvm-project//mlir:CallOpInterfaces", + "@llvm-project//mlir:CastInterfaces", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:SideEffectInterfaces", + "@llvm-project//mlir:Support", + ], +) + +gentbl_cc_library( + name = "transform_patterns_inc_gen", + compatible_with = get_compatible_with_portable(), + tbl_outs = {"transforms/generated_transform_patterns.inc": ["-gen-rewriters"]}, + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "transforms/transform_patterns.td", + deps = [ + "//tensorflow/compiler/mlir/lite:tensorflow_lite_ops_td_files", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_ops_td_files", + "@llvm-project//mlir:ArithOpsTdFiles", + "@llvm-project//mlir:FuncTdFiles", + ], +) + +cc_library( + name = "device_transform_patterns", + srcs = [ + "transforms/device_transform_patterns.cc", + ], + hdrs = [ + "transforms/device_transform_patterns.h", + ], + textual_hdrs = [ + "transforms/generated_transform_patterns.inc", + ], + deps = [ + ":common", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/compiler/mlir/tensorflow:verification_utils", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:Support", + ], +) + +cc_library( + name = "device_transform_gpu", + srcs = [ + "transforms/device_transform_gpu.cc", + ], + hdrs = [ + "transforms/device_transform_gpu.h", + ], + deps = [ + ":common", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/lite/experimental/tac/hardwares:gpu_hardware", + "//tensorflow/compiler/mlir/tensorflow", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + ], + alwayslink = 1, +) + +cc_library( + name = "device_transform_nnapi", + srcs = [ + "transforms/device_transform_nnapi.cc", + ], + deps = [ + ":common", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/lite/experimental/tac/hardwares:nnapi_hardware", + "//tensorflow/compiler/mlir/tensorflow", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + ], + alwayslink = 1, +) + +cc_library( + name = "device_transform", + srcs = [ + "transforms/device_transform.cc", + ], + hdrs = [ + "transforms/device_transform.h", + ], + deps = [ + ":common", + ":device_transform_gpu", + ":device_transform_nnapi", + ":device_transform_patterns", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/lite/experimental/tac/hardwares:target_hardware", + "//tensorflow/compiler/mlir/tensorflow", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@llvm-project//mlir:Transforms", + ], +) + +cc_library( + name = "cost_model", + srcs = [ + "transforms/cost_model.cc", + ], + hdrs = [ + "transforms/cost_model.h", + ], + deps = [ + ":common", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/lite/experimental/tac/hardwares:target_hardware", + "@com_google_absl//absl/strings", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:CallOpInterfaces", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + ], + alwayslink = 1, +) + +# TODO(b/177376459): split tac_module and passes dependency to separate libraries. +cc_library( + name = "target_aware_conversion", + srcs = [ + "tac_module.cc", + "transforms/compute_cost.cc", + "transforms/fold_constants_to_subgraph.cc", + "transforms/get_alternative_subgraph.cc", + "transforms/pick_subgraphs.cc", + "transforms/raise_target_subgraphs.cc", + "transforms/tac_filter.cc", + "transforms/target_annotation.cc", + ], + hdrs = [ + "tac_module.h", + "transforms/passes.h", + "transforms/tac_pass.h", + ], + deps = [ + ":common", + ":cost_model", + ":device_transform", + ":tac_filter_proto_cc", + ":tac_importer_exporter", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/lite:tf_tfl_passes", + "//tensorflow/compiler/mlir/lite/experimental/common:outline_operations", + "//tensorflow/compiler/mlir/lite/experimental/tac/hardwares:target_hardware", + "//tensorflow/compiler/mlir/tensorflow:cluster_util", + "//tensorflow/compiler/mlir/tensorflow:error_util", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_analysis", + "@com_google_absl//absl/status", + "@com_google_absl//absl/strings", + "@com_google_protobuf//:protobuf_headers", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:CallOpInterfaces", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:SideEffectInterfaces", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@llvm-project//mlir:Transforms", + "@stablehlo//:stablehlo_ops", + ], + alwayslink = 1, +) + +cc_library( + name = "tac-opt_lib", + testonly = True, + deps = [ + ":target_aware_conversion", + "//tensorflow/compiler/mlir/lite:litert_mlir_opt_main", + ], + alwayslink = 1, +) + +# Binary with no hardwares linked. +tf_cc_binary( + name = "tac-opt", + testonly = True, + deps = [ + ":tac-opt_lib", + ], +) + +# Binary with all backends linked. +tf_cc_binary( + name = "tac-opt-all-backends", + testonly = True, + deps = [ + ":tac-opt_lib", + "//tensorflow/compiler/mlir/lite/experimental/common:outline_operations", + "//tensorflow/compiler/mlir/lite/experimental/tac/hardwares:all-target-hardwares", + ], +) + +cc_library( + name = "tac-translate-lib", + srcs = [ + "tac_translate.cc", + ], + deps = [ + ":common", + ":execution_metadata_exporter", + ":target_aware_conversion", + ":tflite_importer_exporter", + "//tensorflow/compiler/mlir:init_mlir", + "//tensorflow/compiler/mlir/lite:tensorflow_lite_legalize_tf", + "//tensorflow/compiler/mlir/lite:tensorflow_lite_optimize", + "//tensorflow/compiler/mlir/lite/experimental/tac/hardwares:target_hardware", + "//tensorflow/compiler/mlir/lite/experimental/tac/utils", + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/compiler/mlir/tensorflow:error_util", + "//tensorflow/compiler/mlir/tensorflow/transforms:tensorflow_passes", + "//tensorflow/core:lib", + "@com_google_absl//absl/log", + "@com_google_absl//absl/status", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/strings:str_format", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Parser", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:Transforms", + "@llvm-project//mlir:TranslateLib", + ], +) + +tf_cc_binary( + name = "tac-translate", + deps = [ + ":tac-translate-lib", + "//tensorflow/compiler/mlir/lite/experimental/tac/hardwares:all-target-hardwares", + ], +) + +cc_library( + name = "tac_importer_exporter", + hdrs = ["tac_importer_exporter.h"], + deps = [ + "@com_google_absl//absl/status:statusor", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + ], +) + +cc_library( + name = "tflite_importer_exporter", + srcs = ["tflite_import_export.cc"], + hdrs = ["tflite_import_export.h"], + deps = [ + ":common", + ":execution_metadata_exporter", + ":tac_importer_exporter", + "//tensorflow/compiler/mlir/lite/experimental/tac/hardwares:target_hardware", + "//tensorflow/compiler/mlir/lite/experimental/tac/utils", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:Support", + ], +) + +exports_files([ + "run_lit.sh", +]) + +py_library( + name = "tac", + srcs = [ + "tac.py", + ], + strict_deps = True, + visibility = ["//visibility:public"], + deps = [ + "//tensorflow/compiler/mlir/lite/experimental/tac/py_wrapper:_pywrap_tac_wrapper", + ], +) + +tf_proto_library( + name = "tac_filter_proto", + srcs = ["tac_filter.proto"], + protodeps = if_google(["//google/protobuf:any"]), + visibility = ["//visibility:public"], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/README.md b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/README.md new file mode 100644 index 0000000000000000000000000000000000000000..d12776776e9bfec41c9f2ea2d02c054d37499a01 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/README.md @@ -0,0 +1,271 @@ +# Target Aware Conversion (TAC) + +Different hardwares have different capabilities and restrictions. + +TAC is designed to leverage hardwares' capabilities to: + +* Perform device-specific optimizations (such as unsupported ops lowering, + layout transformations, etc.) +* Graph partitioning based on the hardware costs modeling. +* It supports general import/export where you can hook your own + importer/exporter from any format to MLIR and export MLIR to anything. + +For more details, please checkout the +[TAC workflow](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/compiler/mlir/lite/experimental/tac/README.md#tac-workflow) +section + +## How to use + +Once you have a converted TfLite model ready, you can use the following command +to use TAC to optimize for your model: + +``` +bazel run -c opt //tensorflow/compiler/mlir/lite/experimental/tac:tac-translate -- -o= -device-specs= +``` + +The devices_specs is a list of the names of the desired hardware backends, +separated by comma, e.g., "GPU,CPU". + +If you're interested in what are the subgraphs being explored for different +backends, you can pass in `-output-mlir -inline-subgraphs=false` and check out +the output mlir file. + +## How to add a hardware backend + +If you want to add a hardware backend for TAC, you can start with the +`SimpleHardware` interface. + +For example: + +``` +class FooHardware : public SimpleHardware { + public: + static constexpr char kId[] = "FOO"; + + mlir::RewritePatternSet GetTransformations( + MLIRContext* context) const override { + mlir::RewritePatternSet patterns; + // Pick the transformations that we want to perform, + // We can add other transformations we like here. + patterns.add(context); + return patterns; + } + + mlir::TypeID GetTypeId() const override { + return mlir::TypeID::get(); + } + + // We can specify what ops are not supported here. + bool IsNotSupportedOp(mlir::Operation* op) const override { return false; } + + // This is basically saying how fast are we comparing to CPU. + // The larger the value the better. + float AdvantageOverCPU() const override { return 5.0; } +}; +``` + +Then we need to register our hardware like below: + +``` +std::unique_ptr CreateFooHardware() { + return std::make_unique(); +} + +TargetHardwareRegistration foo_hardware( + "Target device for FOO", CreateFooHardware); +``` + +### Advanced user + +For advanced users (e.g., you may already have your own hardware dialect +defined), please just use `TargetHardware` directly. See the following code +snippet for reference. + +``` +class MyCustomHardware : public TargetHardware { + public: + static constexpr char kId[] = "MY_CUSTOM_HARDWARE"; + + mlir::TypeID GetTypeId() const override { + return mlir::TypeID::get(); + } + + bool IsOpSupported(mlir::Operation* op) const override { + // check whether the op is supported, if the user has they own dialect, + // this can be target dialect legalization process. + } + + double GetHardwareSwitchingCost(const TargetHardware* from, + size_t buffer_size) const override { + // Get the hardware switching cost from the source hardware. + } + + double GetOpCost(mlir::Operation* op) const override { + // call customized cost model. + } + + mlir::RewritePatternSet GetTransformations( + MLIRContext* context) const override { + // customized transformations patterns: ops lowering/fusion, layout + // transformation, etc. + } +}; +``` + +## TAC workflow + +The workflow of target-aware-conversion is as followed: + +1 Try to break down the whole graph into several subgraphs based on hardwares' +capabilities. See the diagram below, let's say our desired target backends are +"GPU" and "CPU", and currently "C" is not supported on "GPU", but the rest are +supported by "GPU". So we will end up with 3 subgraphs as shown in the diagram. + +![Target Annotation](g3doc/images/target_annotation.png) + +2 Perform ops-lowering & target-specific optimizations for + different hardware backends. As shown in the below diagram, the red & the + yellow subgraph will be duplicated as "alternative subgraph view" for "CPU". + "C" op can be lowered into "G" + "H" op which can be supported by "GPU". + +![Target Optimization](g3doc/images/target_optimization.png) + +3 Estimate the costs for each subgraph (and their alternative views) + based on the hardware cost model. See the following diagram. + +![Estimate costs](g3doc/images/compute_cost.png) + +4 Pick the proper subgraphs from the alternative views for execution based on +costs(computation costs, transfer costs, quant/dequant costs). As shown in the +diagram below, since cross-device data transferring cost is high, even "G" + "H" +running on GPU maybe less efficient than "C" running on "CPU", we will still +pick "G" + "H" subgraph. + +![Pick subgraphs](g3doc/images/pick_subgraphs.png) + +The final graph looks like below: + +![Final graph](g3doc/images/final_graph.png) + +## TAC components + +### Hardwares + +Hardwares are used to modeling target device capabilities & also ops cost for +the target devices. + +We have already modeled `cpu_hardware` & `gpu_hardware` as well as the +`nnapi_hardware`. + +### Passes + +#### Target Annotation Pass +In this pass, every op will be targeted with the user specified targets based on +the device capabilites. For example, If the user specified the desired targets +are "GPU", "CPU", `conv2d` can run on both "GPU" and "CPU", we will annotate +the op `conv2d` with "GPU" since it's preferred; `pack` can only run on "CPU", +so we will annotate the op with "CPU" since "GPU" does not support this op. + +#### Raise Target Subgraphs Pass + +In this pass, ops will be broken down into subgraph. Those ops have the same +target annotation will be raised as subgraphs. + +In this pass, subgraph is actually implemented with `FuncOp`. + +Take the following code as an example: + +``` +func @simpleTest(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>, %arg2: tensor<1xf32>, %arg3: tensor<1xf32>) -> tensor<2x1xf32> { + %0 = "tfl.add"(%arg0, %arg1) {tac.device = "GPU", fused_activation_function = "RELU6", tac.inference_type = "FLOAT"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + %1 = "tfl.mul"(%0, %arg2) {tac.device = "GPU", fused_activation_function = "RELU6", tac.inference_type = "FLOAT"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + %2 = "tfl.add"(%arg0, %arg3) {tac.device = "GPU", fused_activation_function = "RELU6", tac.inference_type = "FLOAT"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + %3 = "tfl.pack"(%1, %2) {tac.device = "CPU", tac.inference_type = "FLOAT", axis = 0 : i32, values_count = 2 : i32} : (tensor<1xf32>, tensor<1xf32>) -> tensor<2x1xf32> + return %3 : tensor<2x1xf32> +} +``` + +In this code, `%3` is annotated with "CPU", while others are annotated with +"GPU", in this case, `%3` will be raised as a separate function like below: + +``` + func private @func_1_GPU_FLOAT(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>) -> tensor<1xf32> attributes {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_1"} { + %0 = tfl.add %arg0, %arg1 {fused_activation_function = "RELU6", tac.device = "GPU", tac.inference_type = "FLOAT"} : tensor<1xf32> + return %0 : tensor<1xf32> + } +``` + +And the rest ops will be raised as below: + +``` + func private @func_2_CPU_FLOAT(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>) -> tensor<2x1xf32> attributes {tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "func_2"} { + %0 = "tfl.pack"(%arg0, %arg1) {axis = 0 : i32, tac.device = "CPU", tac.inference_type = "FLOAT", values_count = 2 : i32} : (tensor<1xf32>, tensor<1xf32>) -> tensor<2x1xf32> + return %0 : tensor<2x1xf32> + } + +func private @func_0_GPU_FLOAT(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>, %arg2: tensor<1xf32>) -> tensor<1xf32> attributes {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} { + %0 = tfl.add %arg0, %arg1 {fused_activation_function = "RELU6", tac.device = "GPU", tac.inference_type = "FLOAT"} : tensor<1xf32> + %1 = tfl.mul %0, %arg2 {fused_activation_function = "RELU6", tac.device = "GPU", tac.inference_type = "FLOAT"} : tensor<1xf32> + return %1 : tensor<1xf32> + } +``` + +And the original function will be replaced by `CallOps` to those `FuncOps`: + +``` +func @simpleTest(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>, %arg2: tensor<1xf32>, %arg3: tensor<1xf32>) -> tensor<2x1xf32> { + %0 = call @func_0_GPU_FLOAT(%arg0, %arg1, %arg2) {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} : (tensor<1xf32>, tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + %1 = call @func_1_GPU_FLOAT(%arg0, %arg3) {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_1"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + %2 = call @func_2_CPU_FLOAT(%0, %1) {tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "func_2"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<2x1xf32> + return %2 : tensor<2x1xf32> + } +``` + +Why we need to raise those ops into `FuncOps`? Please see the following section. + +#### Get Alternative Subgraph View Pass +In the Get Alternative Subgraph View Pass, we will essentially duplicate those +`FuncOps` and perform unsupported ops lowering & target-specific optimization. + +For example, `Pack` is not supported by "GPU", but it can be lowered into +`Concat` + `Reshape` which can be supported by "GPU". + +So the original example: + +``` + func private @func_1_GPU_FLOAT(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>) -> tensor<1xf32> attributes {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_1"} { + %0 = tfl.add %arg0, %arg1 {fused_activation_function = "RELU6", tac.device = "GPU", tac.inference_type = "FLOAT"} : tensor<1xf32> + return %0 : tensor<1xf32> + } +``` + +Will be transformed into: + +``` + func private @func_2_CPU_FLOAT(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>) -> tensor<2x1xf32> attributes {tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "func_2"} { + %0 = "tfl.pack"(%arg0, %arg1) {axis = 0 : i32, tac.device = "CPU", tac.inference_type = "FLOAT", values_count = 2 : i32} : (tensor<1xf32>, tensor<1xf32>) -> tensor<2x1xf32> + return %0 : tensor<2x1xf32> + } + +func private @func_2_GPU_FLOAT(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>) -> tensor<2x1xf32> attributes {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_2"} { + %cst = arith.constant dense<1> : tensor<4xi32> + %cst_0 = arith.constant dense<2> : tensor<1xi32> + %cst_1 = arith.constant dense<[2, 1]> : tensor<2xi32> + %0 = "tfl.reshape"(%arg0, %cst) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<1xf32>, tensor<4xi32>) -> tensor<1x1x1x1xf32> + %1 = "tfl.reshape"(%arg1, %cst) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<1xf32>, tensor<4xi32>) -> tensor<1x1x1x1xf32> + %2 = "tfl.concatenation"(%0, %1) {axis = 3 : i32, fused_activation_function = "NONE", tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<1x1x1x1xf32>, tensor<1x1x1x1xf32>) -> tensor<1x1x1x2xf32> + %3 = "tfl.reshape"(%2, %cst_0) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<1x1x1x2xf32>, tensor<1xi32>) -> tensor<2xf32> + %4 = "tfl.reshape"(%3, %cst_1) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<2xf32>, tensor<2xi32>) -> tensor<2x1xf32> + return %4 : tensor<2x1xf32> + } +``` + +#### Compute Costs Pass +In the compute cost pass, we will essentially compute the cost of each op within +the `FuncOp` based on the target-device cost model and sum them together. + + +#### Pick Subgraphs Pass +In the pick subgraphs pass, we will pick those subgraphs which can minimize the +global costs (we will take the tensor transferring costs as well). diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/common/cost.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/common/cost.h new file mode 100644 index 0000000000000000000000000000000000000000..4a0cecea575914c6ab0ed5f864fde6ad00df7733 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/common/cost.h @@ -0,0 +1,50 @@ +/* Copyright 2021 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_COMMON_COST_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_COMMON_COST_H_ + +#include + +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project + +namespace mlir { +namespace TFL { +namespace tac { + +// Cost attribute string on the TFL dialect. +constexpr char kCost[] = "tac.cost"; + +inline void UpdateCost(Operation* op, float cost, OpBuilder* builder) { + op->setAttr(kCost, builder->getF32FloatAttr(cost)); +} + +// Get the cost annotated with kCost. +inline bool GetCostOnOp(Operation* op, float* cost) { + auto cost_type = op->getAttrOfType(kCost); + if (cost_type == nullptr) { + return false; + } + + *cost = cost_type.getValueAsDouble(); + return true; +} + +} // namespace tac +} // namespace TFL +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_COMMON_COST_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/common/subgraph.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/common/subgraph.h new file mode 100644 index 0000000000000000000000000000000000000000..9c05e0410a4b63eaa9df9619f2f9d4c9cca9aa14 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/common/subgraph.h @@ -0,0 +1,52 @@ +/* Copyright 2021 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_COMMON_SUBGRAPH_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_COMMON_SUBGRAPH_H_ + +#include +#include + +#include "llvm/ADT/StringRef.h" +#include "mlir/IR/Operation.h" // from @llvm-project + +namespace mlir { +namespace TFL { +namespace tac { + +// Interface name here is the "hook" between the CallOp and FuncOps. +// Take the following example: +// +// call @func_1_CPU {tac.interface_name = "func_1"} +// +// "func_1" is the interface name where "func_1_cpu" is the real implementation +// we can have multiple FuncOps like "func_1_cpu" and "func_1_gpu" and they +// both implement "func_1". +// +// The attribute on the FuncOp means what it actually implements while the +// attribute on the CallOp means what it actually looks for. +constexpr char kInterfaceNameAttr[] = "tac.interface_name"; + +inline std::optional GetInterFaceName(Operation* op) { + auto name_attr = op->getAttrOfType(kInterfaceNameAttr); + if (!name_attr) return std::nullopt; + return name_attr.getValue().str(); +} + +} // namespace tac +} // namespace TFL +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_COMMON_SUBGRAPH_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/common/targets.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/common/targets.h new file mode 100644 index 0000000000000000000000000000000000000000..b8914da50a95f3c065ee3ac5009ec37ace67c6f2 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/common/targets.h @@ -0,0 +1,158 @@ +/* Copyright 2021 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_COMMON_TARGETS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_COMMON_TARGETS_H_ + +#include +#include +#include +#include +#include +#include +#include + +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/StringRef.h" +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project + +namespace mlir { +namespace TFL { +namespace tac { + +// Device attribute string on the TFL dialect. +constexpr char kDevice[] = "tac.device"; + +// Inference type. +constexpr char kInferenceType[] = "tac.inference_type"; + +// Inference type. +constexpr char kSkipTargetAnnotation[] = "tac.skip_target_annotation"; + +// Custom options fingerprint to apply different options for different filters. +constexpr char kCustomOptionsFingerprint[] = "tac.custom_options_fingerprint"; + +// Delegate compiler stack version. +constexpr char kDelegateCompilerVersion[] = "tac.delegate_compiler_version"; + +// TODO(renjieliu): Add more inference types. +enum InferenceType { + UNKNOWN = 0, + FLOAT = 1, + QUANTIZED_INT8 = 2, + QUANTIZED_UINT8 = 3, + HYBRID = 4 +}; + +inline InferenceType GetInferenceTypeEnum(llvm::StringRef inference_type_str) { + if (inference_type_str == "FLOAT") { + return FLOAT; + } else if (inference_type_str == "QUANTIZED_INT8") { + return QUANTIZED_INT8; + } else if (inference_type_str == "QUANTIZED_UINT8") { + return QUANTIZED_UINT8; + } else if (inference_type_str == "HYBRID") { + return HYBRID; + } else { + return UNKNOWN; + } +} + +inline std::string GetInferenceString(InferenceType inference_type) { + if (inference_type == FLOAT) { + return "FLOAT"; + } else if (inference_type == QUANTIZED_INT8) { + return "QUANTIZED_INT8"; + } else if (inference_type == QUANTIZED_UINT8) { + return "QUANTIZED_UINT8"; + } else if (inference_type == HYBRID) { + return "HYBRID"; + } else { + return "UNKNOWN"; + } +} + +// Returns canonical representation for hardware name (All uppercase). +// TODO(b/177376459): Remove this in favor of the string defined by hardwares +// MyHardware::kId. +inline std::string GetCanonicalHardwareName(const std::string& hardware_name) { + std::string name = hardware_name; + std::transform( + name.begin(), name.end(), name.begin(), + [](unsigned char c) -> unsigned char { return std::toupper(c); }); + return name; +} + +// Get the target annotation form the op. +inline std::optional GetTargetAnnotation(Operation* op) { + auto device = op->getAttrOfType(kDevice); + if (device == nullptr || device.getValue().empty()) return std::nullopt; + + return GetCanonicalHardwareName(device.getValue().str()); +} + +// Get inference type attribute from the operation if available. +inline std::optional GetInferenceTypeAnnotation(Operation* op) { + auto inference_type = op->getAttrOfType(kInferenceType); + if (inference_type == nullptr) return std::nullopt; + + llvm::StringRef device_name_str = inference_type.getValue(); + return GetInferenceTypeEnum(device_name_str); +} + +// InferenceDeviceType is a combination of the hardware with inference type. +struct InferenceDeviceType { + std::string hardware; + InferenceType inference_type; + + bool operator==(const InferenceDeviceType& other) const { + return (hardware == other.hardware) && + (inference_type == other.inference_type); + } + + bool operator!=(const InferenceDeviceType& other) const { + return !(*this == other); + } + + struct inference_device_type_hash { + size_t operator()(const InferenceDeviceType& p) const { + auto hash1 = std::hash{}(p.hardware); + auto hash2 = std::hash{}(p.inference_type); + return hash1 ^ hash2; + } + }; +}; + +// Get InferenceDeviceType attribute from the operation if available. +inline std::optional GetInferenceDeviceTypeForOp( + Operation* op) { + auto hardware = GetTargetAnnotation(op); + if (!hardware.has_value()) return std::nullopt; + + auto inference_type = GetInferenceTypeAnnotation(op); + if (!inference_type.has_value()) return std::nullopt; + + InferenceDeviceType inference_device_type; + inference_device_type.hardware = hardware.value(); + inference_device_type.inference_type = inference_type.value(); + return inference_device_type; +} + +} // namespace tac +} // namespace TFL +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_COMMON_TARGETS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/common/utils.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/common/utils.cc new file mode 100644 index 0000000000000000000000000000000000000000..0ae4856e5913f8ddeafae59b1ed7f6ebcc87f64a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/common/utils.cc @@ -0,0 +1,80 @@ +/* Copyright 2021 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/compiler/mlir/lite/experimental/tac/common/utils.h" + +#include "llvm/Support/Casting.h" +#include "mlir/IR/OpDefinition.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/targets.h" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/lite/utils/utils.h" + +namespace mlir { +namespace TFL { +namespace tac { + +bool NotTFLQuantDequantizeOp(Operation* op) { + if (!op) return false; + if (llvm::isa(op)) return false; + return true; +} + +bool IsTerminatorOp(Operation* op) { + if (!op) return false; + return op->hasTrait(); +} + +// Try to guess the inference type of the op. +InferenceType GetInferenceType(Operation* op) { + bool float_type_observed = false; + bool int8_type_observed = false; + bool uint8_type_observed = false; + for (auto& input : op->getOpOperands()) { + auto input_type = input.get().getType(); + if (IsF32ShapedType(input_type)) { + float_type_observed = true; + } else if (IsQI8Type(input_type)) { + int8_type_observed = true; + } else if (IsQUI8Type(input_type)) { + uint8_type_observed = true; + } + } + + // We should not observe both uint8 & int8. + if (int8_type_observed && uint8_type_observed) return UNKNOWN; + + if (float_type_observed) { + if (int8_type_observed || uint8_type_observed) { + return HYBRID; + } else { + return FLOAT; + } + } + + if (int8_type_observed) { + return QUANTIZED_INT8; + } + + if (uint8_type_observed) { + return QUANTIZED_UINT8; + } + + // Default to float inference. + return FLOAT; +} + +} // namespace tac +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/common/utils.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/common/utils.h new file mode 100644 index 0000000000000000000000000000000000000000..626900a5f7b162b53fa2be5dbc5a084b898d9b97 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/common/utils.h @@ -0,0 +1,94 @@ +/* Copyright 2021 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_COMMON_UTILS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_COMMON_UTILS_H_ + +#include "llvm/Support/Casting.h" +#include "mlir/Bytecode/BytecodeOpInterface.h" // from @llvm-project +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Dialect/Quant/IR/QuantTypes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/OpDefinition.h" // from @llvm-project +#include "mlir/IR/Types.h" // from @llvm-project +#include "mlir/Interfaces/CallInterfaces.h" // from @llvm-project +#include "mlir/Interfaces/CastInterfaces.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/targets.h" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/lite/utils/utils.h" + +namespace mlir { +namespace TFL { +namespace tac { + +// Returns true if 'op' is non const op. Returns false otherwise or if +// 'op' is null. +inline bool IsNonConstOp(Operation* op) { + if (!op) return false; + if (llvm::isa(op)) return false; + if (op->hasTrait()) return false; + if (llvm::isa(op)) return false; + return true; +} + +// Returns true if 'op' is a terminator op, otherwise false. +bool IsTerminatorOp(Operation* op); + +// Returns true if 'op' is not TFL Quant / Dequant op. Returns False otherwise +// or if 'op' is null. +bool NotTFLQuantDequantizeOp(Operation* op); + +// Returns true if it is a shaped type of f32 elements. +inline bool IsF32ShapedType(Type t) { + if (auto shaped_type = mlir::dyn_cast_or_null(t)) { + return shaped_type.getElementType().isF32(); + } + return false; +} + +// Return true when the given element_type is QI8. +inline bool IsQI8Type(Type t) { + auto quantized_type = quant::QuantizedType::getQuantizedElementType(t); + return quantized_type != nullptr && + quantized_type.getStorageTypeIntegralWidth() == 8 && + quantized_type.isSigned(); +} + +// Return true when the given element_type is QUI8. +inline bool IsQUI8Type(Type t) { + auto quantized_type = quant::QuantizedType::getQuantizedElementType(t); + return quantized_type != nullptr && + quantized_type.getStorageTypeIntegralWidth() == 8 && + !quantized_type.isSigned(); +} + +// Return true when the given element_type is QI32. +inline bool IsQI32Type(Type t) { + auto quantized_type = quant::QuantizedType::getQuantizedElementType(t); + return quantized_type != nullptr && + quantized_type.getStorageTypeIntegralWidth() == 32 && + quantized_type.isSigned(); +} + +// Try to guess the inference type of the op. +InferenceType GetInferenceType(Operation* op); + +} // namespace tac +} // namespace TFL +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_COMMON_UTILS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/examples/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/examples/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..0bfa6965b622c13df903fbf415f3ef6ba52de85a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/examples/BUILD @@ -0,0 +1,31 @@ +load("//tensorflow:tensorflow.bzl", "tf_cc_binary") +load("//tensorflow/core/platform:rules_cc.bzl", "cc_library") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + default_visibility = [ + "//visibility:public", + ], + licenses = ["notice"], # Apache 2.0 +) + +cc_library( + name = "example_hardware", + srcs = ["example_hardware.cc"], + hdrs = ["example_hardware.h"], + deps = [ + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/lite/experimental/tac:device_transform_patterns", + "//tensorflow/compiler/mlir/lite/experimental/tac/hardwares:simple_hardware", + ], + alwayslink = 1, +) + +tf_cc_binary( + name = "example-hardware-translate", + deps = [ + ":example_hardware", + "//tensorflow/compiler/mlir/lite/experimental/tac:tac-translate-lib", + "//tensorflow/compiler/mlir/lite/experimental/tac/hardwares:all-target-hardwares", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/examples/example_hardware.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/examples/example_hardware.cc new file mode 100644 index 0000000000000000000000000000000000000000..1ba10b02afd14594cae85d3c4c6eb1f6e894d456 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/examples/example_hardware.cc @@ -0,0 +1,47 @@ +/* Copyright 2021 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/compiler/mlir/lite/experimental/tac/examples/example_hardware.h" + +#include + +#include "tensorflow/compiler/mlir/lite/experimental/tac/transforms/device_transform_patterns.h" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" + +namespace mlir { +namespace TFL { +namespace tac { + +constexpr char ExampleHardware::kId[]; // Define kId. + +mlir::RewritePatternSet ExampleHardware::GetTransformations( + MLIRContext* context) const { + mlir::RewritePatternSet patterns(context); + + patterns.add(context); + return patterns; +} + +std::unique_ptr CreateExampleHardware() { + return std::make_unique(); +} + +TargetHardwareRegistration example_hardware( + "Example device", CreateExampleHardware); + +} // namespace tac +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/examples/example_hardware.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/examples/example_hardware.h new file mode 100644 index 0000000000000000000000000000000000000000..d5bd89ea9069525f6cf56f24f20dced1f5fb2b99 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/examples/example_hardware.h @@ -0,0 +1,45 @@ +/* Copyright 2021 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_EXAMPLES_EXAMPLE_HARDWARE_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_EXAMPLES_EXAMPLE_HARDWARE_H_ + +#include "tensorflow/compiler/mlir/lite/experimental/tac/hardwares/simple_hardware.h" + +namespace mlir { +namespace TFL { +namespace tac { + +class ExampleHardware : public SimpleHardware { + public: + static constexpr char kId[] = "ExampleHardware"; + + mlir::RewritePatternSet GetTransformations( + MLIRContext* context) const override; + + mlir::TypeID GetTypeId() const override { + return mlir::TypeID::get(); + } + + bool IsNotSupportedOp(mlir::Operation* op) const override { return false; } + + float AdvantageOverCPU() const override { return 5.0; } +}; + +} // namespace tac +} // namespace TFL +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_EXAMPLES_EXAMPLE_HARDWARE_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/execution_metadata_exporter.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/execution_metadata_exporter.cc new file mode 100644 index 0000000000000000000000000000000000000000..301f2ca5d991a76f86e100dfd63cdd7466b01158 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/execution_metadata_exporter.cc @@ -0,0 +1,206 @@ +// Copyright 2020 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/compiler/mlir/lite/experimental/tac/execution_metadata_exporter.h" + +#include +#include +#include +#include +#include +#include + +#include "flatbuffers/buffer.h" // from @flatbuffers +#include "flatbuffers/flatbuffer_builder.h" // from @flatbuffers +#include "flatbuffers/string.h" // from @flatbuffers +#include "flatbuffers/vector.h" // from @flatbuffers +#include "llvm/Support/Casting.h" +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/Region.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/targets.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/hardwares/target_hardware.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/runtime_metadata_generated.h" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" + +namespace tflite { +namespace { + +bool IsConst(mlir::Operation* op) { + return llvm::isa(op); +} + +bool IsOpSupported(mlir::Operation* op, const std::string& hardware) { + auto* devce_hardware = mlir::TFL::tac::GetTargetHardware(hardware); + if (devce_hardware == nullptr) return {}; + return devce_hardware->IsOpSupported(op); +} + +bool HasValidHardwareTarget(mlir::Operation* op) { + // All TFLite ops has CPU interface, should be enough to check for cpu. + return IsOpSupported(op, "CPU"); +} + +std::optional GetDeviceName(mlir::Operation* op) { + if (IsConst(op)) return std::nullopt; + + // The model may contain quant stats op which is unrelevant to the + // execution. + if (llvm::isa(op)) + return std::nullopt; + + if (!HasValidHardwareTarget(op)) return std::nullopt; + + auto device = op->getAttrOfType(mlir::TFL::tac::kDevice); + if (device == nullptr) return std::nullopt; + + llvm::StringRef device_name_str = device.getValue(); + return device_name_str.str(); +} + +std::optional> GetPerDeviceCosts( + const std::map& hardware_map, mlir::Operation* op) { + auto device_costs_attr = + op->getAttrOfType("per_device_costs"); + if (device_costs_attr == nullptr) return std::nullopt; + + std::vector device_costs(hardware_map.size(), -1.f); + + for (const auto& kv : hardware_map) { + auto cost_attr = device_costs_attr.getNamed(kv.first); + if (!cost_attr.has_value()) return std::nullopt; + float cost = mlir::dyn_cast_or_null(cost_attr->getValue()) + .getValueAsDouble(); + device_costs[kv.second] = cost; + } + return device_costs; +} + +flatbuffers::Offset CreateSubgraphMetadata( + const std::map& hardware_map, mlir::Region* Region, + flatbuffers::FlatBufferBuilder* builder) { + auto& block = Region->front(); + int index = 0; + std::vector> ops; + for (auto& inst : block) { + // Const nodes are mapped to const vectors in flatbuffer, so skip. + if (IsConst(&inst)) continue; + + // The model may contain quant stats op which is unrelevant to the + // execution. + if (llvm::isa(&inst)) + continue; + + // If an op doesn't implement any of the hardware interface we skip it. + // This can happen in cases like Flex when we have non TFLite ops. + auto device_name = GetDeviceName(&inst); + + if (device_name.has_value()) { + // Add per device costs if present. + auto per_device_cost = GetPerDeviceCosts(hardware_map, &inst); + flatbuffers::Offset> per_device_cost_offset; + + if (per_device_cost.has_value()) { + per_device_cost_offset = builder->CreateVector(*per_device_cost); + } + + OpMetadataBuilder op_builder(*builder); + op_builder.add_index(index); + uint8_t hardware = hardware_map.at(*device_name); + op_builder.add_hardware(hardware); + + if (per_device_cost.has_value()) { + op_builder.add_op_costs(per_device_cost_offset); + } + + ops.push_back(op_builder.Finish()); + } + index++; + } + return CreateSubgraphMetadata(*builder, builder->CreateVector(ops)); +} + +flatbuffers::Offset +CreateHardwareMetadataAndPopulateLookupTable( + std::vector* funcs, + flatbuffers::FlatBufferBuilder* builder, + std::map* hardware_names) { + uint8_t index = 0; + for (auto& func : *funcs) { + func.walk([&hardware_names, &index](mlir::Operation* op) { + auto device_name = GetDeviceName(op); + if (!device_name.has_value()) return; + + auto iter = hardware_names->find(*device_name); + if (iter == hardware_names->end()) { + hardware_names->insert({*device_name, index++}); + } + }); + } + + // Build the flatbuffer. + std::vector> hardwares; + for (const auto& kv : *hardware_names) { + hardwares.push_back(builder->CreateString(kv.first)); + } + + return CreateHardwareMetadata(*builder, builder->CreateVector(hardwares)); +} + +} // namespace + +std::optional ExportRuntimeMetadata(mlir::ModuleOp module) { + mlir::func::FuncOp main_fn = module.lookupSymbol("main"); + if (!main_fn) return std::string(""); + + flatbuffers::FlatBufferBuilder fb_builder; + std::vector funcs; + funcs.push_back(main_fn); + module.walk([&](mlir::func::FuncOp fn) { + if (fn != main_fn) { + funcs.push_back(fn); + } + }); + + // Populate the hardware metadata. + // And collect the hardwares used. + std::map hardware_map; + flatbuffers::Offset hardware_metadata_offset = + CreateHardwareMetadataAndPopulateLookupTable(&funcs, &fb_builder, + &hardware_map); + + // Populate the runtime metadata. + std::vector> subgraphs_metadata; + subgraphs_metadata.reserve(funcs.size()); + for (auto& func : funcs) { + subgraphs_metadata.push_back( + CreateSubgraphMetadata(hardware_map, &func.getBody(), &fb_builder)); + } + auto runtime_metadata = + CreateRuntimeMetadata(fb_builder, hardware_metadata_offset, + fb_builder.CreateVector(subgraphs_metadata)); + fb_builder.Finish(runtime_metadata); + return std::string( + reinterpret_cast(fb_builder.GetBufferPointer()), + fb_builder.GetSize()); +} +} // namespace tflite diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/execution_metadata_exporter.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/execution_metadata_exporter.h new file mode 100644 index 0000000000000000000000000000000000000000..70eba89710fdf748d529a8c4ee969aeee137ddc6 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/execution_metadata_exporter.h @@ -0,0 +1,31 @@ +// Copyright 2020 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. + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_EXECUTION_METADATA_EXPORTER_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_EXECUTION_METADATA_EXPORTER_H_ + +#include +#include + +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project + +namespace tflite { + +// Returns serialized string for the generated flatbuffer. +std::optional ExportRuntimeMetadata(mlir::ModuleOp module); + +} // namespace tflite + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_EXECUTION_METADATA_EXPORTER_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/execution_metadata_exporter_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/execution_metadata_exporter_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..95b7966d06433dc3fcb8c92f953e78cd95783fde --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/execution_metadata_exporter_test.cc @@ -0,0 +1,128 @@ +// Copyright 2020 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/compiler/mlir/lite/experimental/tac/execution_metadata_exporter.h" + +#include +#include + +#include +#include "flatbuffers/buffer.h" // from @flatbuffers +#include "flatbuffers/flatbuffer_builder.h" // from @flatbuffers +#include "flatbuffers/string.h" // from @flatbuffers +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/DialectRegistry.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/OwningOpRef.h" // from @llvm-project +#include "mlir/Parser/Parser.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/tac/runtime_metadata_generated.h" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" + +namespace tflite { + +std::string CreateRuntimeMetadata() { + flatbuffers::FlatBufferBuilder fb_builder; + + std::vector> device_names = { + fb_builder.CreateString("GPU"), fb_builder.CreateString("CPU")}; + + const auto hardwares = + CreateHardwareMetadata(fb_builder, fb_builder.CreateVector(device_names)); + const auto ops = { + CreateOpMetadata(fb_builder, 0, 0, + fb_builder.CreateVector(std::vector({1.0, 5.0}))), + CreateOpMetadata(fb_builder, 1, 0, + fb_builder.CreateVector(std::vector({1.0, 5.0}))), + CreateOpMetadata(fb_builder, 2, 0, + fb_builder.CreateVector(std::vector({1.0, 5.0}))), + CreateOpMetadata( + fb_builder, 3, 1, + fb_builder.CreateVector(std::vector({-1.0, 2.0}))), + }; + const auto subgraphs = {CreateSubgraphMetadata( + fb_builder, fb_builder.CreateVector(ops.begin(), ops.size()))}; + + const auto metadata = CreateRuntimeMetadata( + fb_builder, hardwares, + fb_builder.CreateVector(subgraphs.begin(), subgraphs.size())); + fb_builder.Finish(metadata); + + return std::string( + reinterpret_cast(fb_builder.GetBufferPointer()), + fb_builder.GetSize()); +} + +void Verify(const RuntimeMetadata* result, const RuntimeMetadata* expected) { + EXPECT_EQ(result->subgraph_metadata()->size(), + expected->subgraph_metadata()->size()); + for (int i = 0; i < result->subgraph_metadata()->size(); ++i) { + auto result_subgraph_metadata = + result->subgraph_metadata()->GetAs(i); + auto expected_subgraph_metadata = + expected->subgraph_metadata()->GetAs(i); + if (expected_subgraph_metadata->op_metadata() == nullptr && + result_subgraph_metadata->op_metadata() == nullptr) { + return; + } + ASSERT_EQ(expected_subgraph_metadata->op_metadata()->size(), + result_subgraph_metadata->op_metadata()->size()); + for (int j = 0; j < expected_subgraph_metadata->op_metadata()->size(); + ++j) { + auto result_op_metadata = + result_subgraph_metadata->op_metadata()->GetAs(j); + auto expected_op_metadata = + expected_subgraph_metadata->op_metadata()->GetAs(j); + EXPECT_EQ(result_op_metadata->index(), expected_op_metadata->index()); + EXPECT_EQ(result_op_metadata->hardware(), + expected_op_metadata->hardware()); + + EXPECT_EQ(result_op_metadata->op_costs()->size(), + expected_op_metadata->op_costs()->size()); + for (int i = 0; i < result_op_metadata->op_costs()->size(); ++i) { + EXPECT_FLOAT_EQ(result_op_metadata->op_costs()->Get(i), + expected_op_metadata->op_costs()->Get(i)); + } + } + } +} + +TEST(ExporterTest, Valid) { + const std::string kMLIR = R"( +func.func @main(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>, %arg2: tensor<1xf32>, %arg3: tensor<1xf32>) -> tensor<2x1xf32> { + %0 = "tfl.add"(%arg0, %arg1) {fused_activation_function = "RELU6", per_device_costs = {CPU = 5.0 : f32, GPU = 1.0 : f32}, tac.device = "GPU"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + %1 = "tfl.mul"(%0, %arg2) {fused_activation_function = "RELU6", per_device_costs = {CPU = 5.0 : f32, GPU = 1.0 : f32}, tac.device = "GPU"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + %2 = "tfl.add"(%arg0, %arg3) {fused_activation_function = "RELU6", per_device_costs = {CPU = 5.0 : f32, GPU = 1.0 : f32}, tac.device = "GPU"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + %3 = "tfl.pack"(%1, %2) {axis = 0 : i32, per_device_costs = {CPU = 2.0 : f32, GPU = -1.0 : f32}, values_count = 2 : i32, tac.device = "CPU"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<2x1xf32> + func.return %3 : tensor<2x1xf32> +})"; + const std::string kExpectedFB = CreateRuntimeMetadata(); + mlir::DialectRegistry registry; + registry.insert(); + mlir::MLIRContext context(registry); + auto module = mlir::OwningOpRef( + mlir::parseSourceString(kMLIR, &context)); + auto module_op = module.get(); + auto serialized_result_fb = ExportRuntimeMetadata(module_op); + const auto* result = GetRuntimeMetadata(serialized_result_fb.value().c_str()); + const auto* expected = GetRuntimeMetadata(kExpectedFB.c_str()); + ASSERT_TRUE(result != nullptr); + ASSERT_TRUE(result->subgraph_metadata() != nullptr); + ASSERT_TRUE(expected->subgraph_metadata() != nullptr); + Verify(result, expected); +} + +} // namespace tflite diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/g3doc/images/compute_cost.png b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/g3doc/images/compute_cost.png new file mode 100644 index 0000000000000000000000000000000000000000..33dc9f86395e633062edf4d9bafd3977a5e11a44 Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/g3doc/images/compute_cost.png differ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/g3doc/images/final_graph.png b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/g3doc/images/final_graph.png new file mode 100644 index 0000000000000000000000000000000000000000..04bed461080c4bdda0745cfa988460d895c1a97a Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/g3doc/images/final_graph.png differ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/g3doc/images/pick_subgraphs.png b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/g3doc/images/pick_subgraphs.png new file mode 100644 index 0000000000000000000000000000000000000000..ddcd26ecfec3afca050e09df7ad607d3a7d05126 Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/g3doc/images/pick_subgraphs.png differ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/g3doc/images/target_annotation.png b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/g3doc/images/target_annotation.png new file mode 100644 index 0000000000000000000000000000000000000000..61cd9f3d2e55a80258160f4839a7605a1345866c Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/g3doc/images/target_annotation.png differ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/g3doc/images/target_optimization.png b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/g3doc/images/target_optimization.png new file mode 100644 index 0000000000000000000000000000000000000000..e6100f4060e24555bd6ed0408a2f29b2bbb3f338 Binary files /dev/null and b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/g3doc/images/target_optimization.png differ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/hardwares/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/hardwares/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..a26a2ac1da6e331d87dec9cba35f628fbe190fb5 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/hardwares/BUILD @@ -0,0 +1,89 @@ +load("//tensorflow/core/platform:rules_cc.bzl", "cc_library") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + default_visibility = [ + "//visibility:public", + ], + licenses = ["notice"], +) + +cc_library( + name = "gpu_hardware", + srcs = ["gpu_hardware.cc"], + hdrs = ["gpu_hardware.h"], + deps = [ + ":target_hardware", + "//tensorflow/compiler/mlir/lite:cost_estimators", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/lite/experimental/tac:common", + "//tensorflow/compiler/mlir/lite/experimental/tac:device_transform_patterns", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + ], + alwayslink = 1, +) + +cc_library( + name = "cpu_hardware", + srcs = ["cpu_hardware.cc"], + deps = [ + ":target_hardware", + "//tensorflow/compiler/mlir/lite:cost_estimators", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/lite/experimental/tac:common", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + ], + alwayslink = 1, +) + +cc_library( + name = "nnapi_hardware", + srcs = ["nnapi_hardware.cc"], + hdrs = ["nnapi_hardware.h"], + deps = [ + ":target_hardware", + "//tensorflow/compiler/mlir/lite:cost_estimators", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/lite/experimental/tac:device_transform_patterns", + "//tensorflow/compiler/mlir/lite/experimental/tac/hardwares:simple_hardware", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + ], + alwayslink = 1, +) + +cc_library( + name = "target_hardware", + srcs = ["target_hardware.cc"], + hdrs = ["target_hardware.h"], + deps = [ + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/lite/experimental/tac:common", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + ], +) + +cc_library( + name = "simple_hardware", + srcs = ["simple_hardware.cc"], + hdrs = ["simple_hardware.h"], + deps = [ + ":target_hardware", + "@llvm-project//mlir:IR", + ], +) + +cc_library( + name = "all-target-hardwares", + deps = [ + ":cpu_hardware", + ":gpu_hardware", + ":nnapi_hardware", + ], + alwayslink = 1, +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/hardwares/cpu_hardware.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/hardwares/cpu_hardware.cc new file mode 100644 index 0000000000000000000000000000000000000000..abcface1013572c757d9bd6c823f76f24daa2db8 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/hardwares/cpu_hardware.cc @@ -0,0 +1,181 @@ +/* Copyright 2021 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 "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Support/TypeID.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/targets.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/utils.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/hardwares/target_hardware.h" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/lite/utils/arithmetic_count_util.h" + +namespace mlir { +namespace TFL { +namespace tac { +namespace { +// CPU +constexpr float kCPUArithmeticUnitCost = 1.0; + +// This basically assumes pure load/store. This is just fake data. +constexpr float kCPUCopyUnitCost = 0.5; + +// Default values. +constexpr float kCPUDefaultFixedValuedCost = 10000.0; + +// Quantized inference cost efficiency. +// For CPU, quantized inference is ~3x faster than the float alternative, this +// is just an estimation. +constexpr float kQuantizedInferenceEfficiency = 0.3; + +inline float InferenceTypeEfficiency(InferenceType inference_type) { + if (inference_type == QUANTIZED_INT8 || inference_type == QUANTIZED_UINT8) { + return kQuantizedInferenceEfficiency; + } + return 1.0; +} + +// CPU hardware class which handles CPU capabilities in TFLite. +// This is used by TAC to get op supported/ op cost estimates on CPU. +class CpuHardware : public TargetHardware { + public: + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(CpuHardware) + + // String Identifier for CPU hardware. + static constexpr char kId[] = "CPU"; + + double GetHardwareSwitchingCost(const TargetHardware* from, + size_t buffer_size) const override { + auto from_type = from->GetTypeId(); + auto to_type = GetTypeId(); + if (from_type == to_type) return 0.0f; + + // TODO(renjieliu): Implement a better version for different hardware cases. + return buffer_size * kCrossHardwareTransferPerByteCost / 8.0 + + kCrossHardwareTransferFixedCost; + } + + mlir::RewritePatternSet GetTransformations( + MLIRContext* context) const override { + return {context}; + } + + mlir::TypeID GetTypeId() const override { + return mlir::TypeID::get(); + } + + bool IsOpSupported(mlir::Operation* op) const override { + // All ops in TFL dialect are supported on CPU. + if (op->getDialect() == nullptr) return false; + if (op->getDialect()->getNamespace() != "tfl") return false; + return true; + } +}; + +constexpr char CpuHardware::kId[]; // Define kId. + +std::unique_ptr CreateCpuHardware() { + return std::make_unique(); +} + +TargetHardwareRegistration cpu_hardware("Target device for CPU", + CreateCpuHardware); + +#define TAC_REGISTER_CPU_OP(Op, Create) \ + TargetHardwareOpRegistration Op##_CpuHardware_hardware( \ + Create); + +// Operation costs on CPU + +// Currently used for these ops: +// tfl.conv_2d / tfl.depthwise_conv_2d / tfl.fully_connected +class CpuConvOp : public TargetHardwareOperation { + double GetOpCost(mlir::Operation* op) const override { + float cost = 0.0; + int64_t arithmetic_count; + if (ArithmeticCountUtilHelper::GetArithmeticCountForConvAndFullyconnectedOp( + op, &arithmetic_count)) { + cost = arithmetic_count * kCPUArithmeticUnitCost; + } else { + cost = kCPUDefaultFixedValuedCost; + } + return cost * InferenceTypeEfficiency(GetInferenceType(op)); + } + + bool IsOpSupported(mlir::Operation* op) const override { return true; } +}; +std::unique_ptr CreateConvOp() { + return std::make_unique(); +} + +// Currently used for these ops: +// tfl.Add / tfl.mul +class CpuArithmeticOp : public TargetHardwareOperation { + double GetOpCost(mlir::Operation* op) const override { + float cost = 0.0; + int64_t count; + if (ArithmeticCountUtilHelper::GetFirstOutputCount(op, &count)) { + cost = kCPUArithmeticUnitCost * count; + } else { + cost = kCPUDefaultFixedValuedCost; + } + return cost * InferenceTypeEfficiency(GetInferenceType(op)); + } + + bool IsOpSupported(mlir::Operation* op) const override { return true; } +}; +std::unique_ptr CreateArithmeticOp() { + return std::make_unique(); +} + +// Currently used for these ops: +// tfl.concatenation / tfl.reshape / tfl.pack +class CpuConcatOp : public TargetHardwareOperation { + double GetOpCost(mlir::Operation* op) const override { + float cost = 0.0; + int64_t count; + if (ArithmeticCountUtilHelper::GetInputTensorTotalSize(op, &count)) { + cost = kCPUCopyUnitCost * count; + } else { + cost = kCPUDefaultFixedValuedCost; + } + return cost * InferenceTypeEfficiency(GetInferenceType(op)); + } + + bool IsOpSupported(mlir::Operation* op) const override { return true; } +}; +std::unique_ptr CreateConcatOp() { + return std::make_unique(); +} + +TAC_REGISTER_CPU_OP(Conv2DOp, CreateConvOp); +TAC_REGISTER_CPU_OP(DepthwiseConv2DOp, CreateConvOp); +TAC_REGISTER_CPU_OP(FullyConnectedOp, CreateConvOp); +TAC_REGISTER_CPU_OP(AddOp, CreateArithmeticOp); +TAC_REGISTER_CPU_OP(MulOp, CreateArithmeticOp); +TAC_REGISTER_CPU_OP(ConcatenationOp, CreateConcatOp); +TAC_REGISTER_CPU_OP(ReshapeOp, CreateConcatOp); +TAC_REGISTER_CPU_OP(PackOp, CreateConcatOp); + +#undef TAC_REGISTER_CPU_OP +} // namespace +} // namespace tac +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/hardwares/gpu_hardware.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/hardwares/gpu_hardware.cc new file mode 100644 index 0000000000000000000000000000000000000000..0bfa31d04c0a129df21e766b3267463ee858920f --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/hardwares/gpu_hardware.cc @@ -0,0 +1,229 @@ +/* Copyright 2021 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/compiler/mlir/lite/experimental/tac/hardwares/gpu_hardware.h" + +#include +#include +#include + +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/targets.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/utils.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/hardwares/target_hardware.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/transforms/device_transform_patterns.h" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/lite/utils/arithmetic_count_util.h" + +namespace mlir { +namespace TFL { +namespace tac { +namespace { +#include "tensorflow/compiler/mlir/lite/experimental/tac/transforms/generated_transform_patterns.inc" +} // namespace + +constexpr char GpuHardware::kId[]; // Define kId. + +mlir::RewritePatternSet GpuHardware::GetTransformations( + MLIRContext* context) const { + mlir::RewritePatternSet patterns(context); + + patterns.add(context); + return patterns; +} + +double GpuHardware::GetHardwareSwitchingCost(const TargetHardware* from, + size_t buffer_size) const { + auto from_type = from->GetTypeId(); + auto to_type = GetTypeId(); + if (from_type == to_type) return 0.0f; + + // TODO(renjieliu): Implement a better version for different hardware cases. + return buffer_size * kCrossHardwareTransferPerByteCost / 8.0 + + kCrossHardwareTransferFixedCost; +} + +bool GpuHardware::IsOpSupported(mlir::Operation* op) const { + if (TargetHardware::IsOpSupported(op)) { + return true; + } + + // We also support quantized ops. + return !NotTFLQuantDequantizeOp(op); +} + +namespace { +// GPU +constexpr float kGPUArithmeticUnitCost = 0.2; + +// The copy can be non-consectutive copy. This is just fake data. +constexpr float kGPUCopyUnitCost = 0.2; + +// Default values. +constexpr float kGPUDefaultFixedValuedCost = 10000.0; + +std::unique_ptr CreateGpuHardware() { + return std::make_unique(); +} + +TargetHardwareRegistration gpu_hardware("Target device for GPU", + CreateGpuHardware); + +#define TAC_REGISTER_GPU_OP(Op, Create) \ + TargetHardwareOpRegistration Op##_GpuHardware_hardware( \ + Create); + +// Currently used for these ops: +// tfl.Abs / tfl.Average_pool_2d / tfl.Cos / tfl.div / tfl.exp / tfl.hardswish / +// tfl.log / tfl.logistic / tfl.max_pool_2d / tfl.mirror_pad / tfl.maximum / +// tfl.custom / tfl.mean / tfl.minimum / tfl.pad / tfl.pow / tfl.prelu / +// tfl.relu / tfl.relu6 / tfl.rsqrt / tfl.sin / tfl.slice / tfl.softmax / +// tfl.space_to_depth / tfl.sqrt / tfl.square / tfl.squared_difference / +// tfl.strided_slice / tfl.tanh / tfl.transpose / tfl.transpose_conv +class GpuBasicSupportedOpNoCost : public TargetHardwareOperation { + double GetOpCost(mlir::Operation* op) const override { return 0; } + + bool IsOpSupported(mlir::Operation* op) const override { + InferenceType inference_type = GetInferenceType(op); + if (inference_type != FLOAT) { + return false; + } + return true; + } +}; +std::unique_ptr CreateBasicOpNoCost() { + return std::make_unique(); +} + +// Currently used for these ops: +// tfl.Add / tfl.mul +class GpuArithmeticOp : public TargetHardwareOperation { + double GetOpCost(mlir::Operation* op) const override { + int64_t count; + if (ArithmeticCountUtilHelper::GetFirstOutputCount(op, &count)) + return kGPUArithmeticUnitCost * count; + return kGPUDefaultFixedValuedCost; + } + + bool IsOpSupported(mlir::Operation* op) const override { + InferenceType inference_type = GetInferenceType(op); + if (inference_type != FLOAT) { + return false; + } + return true; + } +}; +std::unique_ptr CreateArithmeticOp() { + return std::make_unique(); +} + +// Currently used for these ops: +// tfl.concatenation / tfl.reshape +class GpuConcatOp : public TargetHardwareOperation { + double GetOpCost(mlir::Operation* op) const override { + int64_t count; + if (ArithmeticCountUtilHelper::GetInputTensorTotalSize(op, &count)) + return kGPUCopyUnitCost * count; + return kGPUDefaultFixedValuedCost; + } + + bool IsOpSupported(mlir::Operation* op) const override { + InferenceType inference_type = GetInferenceType(op); + if (inference_type != FLOAT) { + return false; + } + return true; + } +}; +std::unique_ptr CreateConcatOp() { + return std::make_unique(); +} + +// Currently used for these ops: +// tfl.conv_2d / tfl.depthwise_conv_2d / tfl.fully_connected +class GpuConvOp : public TargetHardwareOperation { + double GetOpCost(mlir::Operation* op) const override { + int64_t arithmetic_count; + if (ArithmeticCountUtilHelper::GetArithmeticCountForConvAndFullyconnectedOp( + op, &arithmetic_count)) { + return arithmetic_count * kGPUArithmeticUnitCost; + } + return kGPUDefaultFixedValuedCost; + } + + bool IsOpSupported(mlir::Operation* op) const override { + InferenceType inference_type = GetInferenceType(op); + if (inference_type != FLOAT) { + return false; + } + return true; + } +}; +std::unique_ptr CreateConvOp() { + return std::make_unique(); +} + +// Op registrations +TAC_REGISTER_GPU_OP(AbsOp, CreateBasicOpNoCost); +TAC_REGISTER_GPU_OP(AveragePool2DOp, CreateBasicOpNoCost); +TAC_REGISTER_GPU_OP(CosOp, CreateBasicOpNoCost); +TAC_REGISTER_GPU_OP(DivOp, CreateBasicOpNoCost); +TAC_REGISTER_GPU_OP(ExpOp, CreateBasicOpNoCost); +TAC_REGISTER_GPU_OP(HardSwishOp, CreateBasicOpNoCost); +TAC_REGISTER_GPU_OP(LogOp, CreateBasicOpNoCost); +TAC_REGISTER_GPU_OP(LogisticOp, CreateBasicOpNoCost); +TAC_REGISTER_GPU_OP(MaxPool2DOp, CreateBasicOpNoCost); +TAC_REGISTER_GPU_OP(MirrorPadOp, CreateBasicOpNoCost); +TAC_REGISTER_GPU_OP(MaximumOp, CreateBasicOpNoCost); +TAC_REGISTER_GPU_OP(MinimumOp, CreateBasicOpNoCost); +TAC_REGISTER_GPU_OP(MeanOp, CreateBasicOpNoCost); +TAC_REGISTER_GPU_OP(CustomOp, CreateBasicOpNoCost); +TAC_REGISTER_GPU_OP(PadOp, CreateBasicOpNoCost); +TAC_REGISTER_GPU_OP(PowOp, CreateBasicOpNoCost); +TAC_REGISTER_GPU_OP(PReluOp, CreateBasicOpNoCost); +TAC_REGISTER_GPU_OP(ReluOp, CreateBasicOpNoCost); +TAC_REGISTER_GPU_OP(Relu6Op, CreateBasicOpNoCost); +TAC_REGISTER_GPU_OP(RsqrtOp, CreateBasicOpNoCost); +TAC_REGISTER_GPU_OP(SinOp, CreateBasicOpNoCost); +TAC_REGISTER_GPU_OP(SliceOp, CreateBasicOpNoCost); +TAC_REGISTER_GPU_OP(SoftmaxOp, CreateBasicOpNoCost); +TAC_REGISTER_GPU_OP(SpaceToDepthOp, CreateBasicOpNoCost); +TAC_REGISTER_GPU_OP(SqrtOp, CreateBasicOpNoCost); +TAC_REGISTER_GPU_OP(SquareOp, CreateBasicOpNoCost); +TAC_REGISTER_GPU_OP(SquaredDifferenceOp, CreateBasicOpNoCost); +TAC_REGISTER_GPU_OP(StridedSliceOp, CreateBasicOpNoCost); +TAC_REGISTER_GPU_OP(TanhOp, CreateBasicOpNoCost); +TAC_REGISTER_GPU_OP(TransposeOp, CreateBasicOpNoCost); +TAC_REGISTER_GPU_OP(TransposeConvOp, CreateBasicOpNoCost); + +TAC_REGISTER_GPU_OP(ConcatenationOp, CreateConcatOp); +TAC_REGISTER_GPU_OP(ReshapeOp, CreateConcatOp); + +TAC_REGISTER_GPU_OP(Conv2DOp, CreateConvOp); +TAC_REGISTER_GPU_OP(DepthwiseConv2DOp, CreateConvOp); +TAC_REGISTER_GPU_OP(FullyConnectedOp, CreateConvOp); + +TAC_REGISTER_GPU_OP(AddOp, CreateArithmeticOp); +TAC_REGISTER_GPU_OP(MulOp, CreateArithmeticOp); + +#undef TAC_REGISTER_GPU_OP +} // namespace +} // namespace tac +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/hardwares/gpu_hardware.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/hardwares/gpu_hardware.h new file mode 100644 index 0000000000000000000000000000000000000000..62021a13b76b94e4c7116eeaab3f755810d3e672 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/hardwares/gpu_hardware.h @@ -0,0 +1,51 @@ +/* Copyright 2021 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_HARDWARES_GPU_HARDWARE_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_HARDWARES_GPU_HARDWARE_H_ + +#include + +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Support/TypeID.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/tac/hardwares/target_hardware.h" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" + +namespace mlir { +namespace TFL { +namespace tac { +// Gpu hardware class which handles GPU capabilities in TFLite. +// This is used by TAC to get op supported/ op cost estimates on GPU. +class GpuHardware : public TargetHardware { + public: + static constexpr char kId[] = "GPU"; + mlir::RewritePatternSet GetTransformations( + MLIRContext* context) const override; + + mlir::TypeID GetTypeId() const override { + return mlir::TypeID::get(); + } + + double GetHardwareSwitchingCost(const TargetHardware* from, + size_t buffer_size) const override; + + bool IsOpSupported(mlir::Operation* op) const override; +}; +} // namespace tac +} // namespace TFL +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_HARDWARES_GPU_HARDWARE_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/hardwares/nnapi_hardware.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/hardwares/nnapi_hardware.cc new file mode 100644 index 0000000000000000000000000000000000000000..863558a66af892c049a484cce5c325a729c57503 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/hardwares/nnapi_hardware.cc @@ -0,0 +1,100 @@ +/* Copyright 2021 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/compiler/mlir/lite/experimental/tac/hardwares/nnapi_hardware.h" + +#include +#include + +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/tac/hardwares/target_hardware.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/transforms/device_transform_patterns.h" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/lite/utils/arithmetic_count_util.h" + +namespace mlir { +namespace TFL { +namespace tac { + +// The copy can be non-consectutive copy. This is just fake data. +constexpr float kNNAPICopyUnitCost = 0.2; + +// Default values. +constexpr float kNNAPIDefaultFixedValuedCost = 10000.0; + +constexpr char NNAPIHardware::kId[]; // Define kId. + +mlir::RewritePatternSet NNAPIHardware::GetTransformations( + MLIRContext* context) const { + mlir::RewritePatternSet patterns(context); + + patterns.add(context); + return patterns; +} + +std::unique_ptr CreateNNAPIHardware() { + return std::make_unique(); +} + +TargetHardwareRegistration nnapi_hardware( + "Target device for NNAPI", CreateNNAPIHardware); + +// Currently used for these ops: +// tfl.squared_difference +class NNAPIBasicSupportedOpNoCost : public TargetHardwareOperation { + double GetOpCost(mlir::Operation* op) const override { return 0; } + + bool IsOpSupported(mlir::Operation* op) const override { + return true; + } +}; + +std::unique_ptr CreateBasicOpNoCost() { + return std::make_unique(); +} + +// Currently used for these ops: +// tfl.concatenation / tfl.reshape / tfl.pack +class NNAPIConcatOp : public TargetHardwareOperation { + double GetOpCost(mlir::Operation* op) const override { + int64_t count; + if (ArithmeticCountUtilHelper::GetInputTensorTotalSize(op, &count)) + return kNNAPICopyUnitCost * count; + return kNNAPIDefaultFixedValuedCost; + } + + bool IsOpSupported(mlir::Operation* op) const override { return true; } +}; +std::unique_ptr CreateConcatOp() { + return std::make_unique(); +} + +#define TAC_REGISTER_NNAPI_OP(Op, Create) \ + TargetHardwareOpRegistration Op##_NNAPIHardware_hardware( \ + Create); + +// Op registeration +TAC_REGISTER_NNAPI_OP(SquaredDifferenceOp, CreateBasicOpNoCost); +TAC_REGISTER_NNAPI_OP(ConcatenationOp, CreateConcatOp); +TAC_REGISTER_NNAPI_OP(ReshapeOp, CreateConcatOp); +TAC_REGISTER_NNAPI_OP(PackOp, CreateConcatOp); + +#undef TAC_REGISTER_NNAPI_OP +} // namespace tac +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/hardwares/nnapi_hardware.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/hardwares/nnapi_hardware.h new file mode 100644 index 0000000000000000000000000000000000000000..7bf5397baed8b4252ee02af311c0a9abe399fc48 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/hardwares/nnapi_hardware.h @@ -0,0 +1,50 @@ +/* Copyright 2021 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. +==============================================================================*/ + +/* NNAPI Hardware Implementation */ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_HARDWARES_NNAPI_HARDWARE_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_HARDWARES_NNAPI_HARDWARE_H_ + +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Support/TypeID.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/tac/hardwares/simple_hardware.h" + +namespace mlir { +namespace TFL { +namespace tac { + +class NNAPIHardware : public SimpleHardware { + public: + static constexpr char kId[] = "NNAPI"; + + mlir::RewritePatternSet GetTransformations( + MLIRContext* context) const override; + + mlir::TypeID GetTypeId() const override { + return mlir::TypeID::get(); + } + + bool IsNotSupportedOp(mlir::Operation* op) const override { return false; } + + float AdvantageOverCPU() const override { return 5.0; } +}; + +} // namespace tac +} // namespace TFL +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_HARDWARES_NNAPI_HARDWARE_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/hardwares/simple_hardware.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/hardwares/simple_hardware.cc new file mode 100644 index 0000000000000000000000000000000000000000..3a282f835d88d86d453dcd7cd3a0d2d2c5cd899c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/hardwares/simple_hardware.cc @@ -0,0 +1,53 @@ +/* Copyright 2021 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/compiler/mlir/lite/experimental/tac/hardwares/simple_hardware.h" + +#include + +#include "mlir/IR/Operation.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/tac/hardwares/target_hardware.h" + +namespace mlir { +namespace TFL { +namespace tac { + +bool SimpleHardware::IsOpSupported(mlir::Operation* op) const { + if (IsNotSupportedOp(op)) { + return false; + } + const TargetHardware* cpu = GetTargetHardware("CPU"); + return cpu->IsOpSupported(op); +} + +double SimpleHardware::GetHardwareSwitchingCost(const TargetHardware* from, + size_t buffer_size) const { + auto from_type = from->GetTypeId(); + auto to_type = GetTypeId(); + if (from_type == to_type) return 0.0f; + + // TODO(renjieliu): Implement a better version for different hardware cases. + return buffer_size * kCrossHardwareTransferPerByteCost / 8.0 + + kCrossHardwareTransferFixedCost; +} + +double SimpleHardware::GetOpCost(mlir::Operation* op) const { + const TargetHardware* cpu = GetTargetHardware("CPU"); + return cpu->GetOpCost(op) / AdvantageOverCPU(); +} + +} // namespace tac +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/hardwares/simple_hardware.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/hardwares/simple_hardware.h new file mode 100644 index 0000000000000000000000000000000000000000..4d5f1187e1d8dc00852e529940e9625479fd2be7 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/hardwares/simple_hardware.h @@ -0,0 +1,67 @@ +/* Copyright 2021 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_HARDWARES_SIMPLE_HARDWARE_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_HARDWARES_SIMPLE_HARDWARE_H_ + +#include + +#include "mlir/IR/Operation.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/tac/hardwares/target_hardware.h" + +namespace mlir { +namespace TFL { +namespace tac { + +// A simple hardware is an interface makes you add a target backend easily if +// you don't want too much customization. +// +// It allows you to easily specify the ops capabilities (by +// specifying the denylist), the rest ops will be considered supported. Also you +// can also specify the advantage over CPU. +// +// If you need more customization, e.g., if you have your own hardware dialect, +// please consider use TargetHardware directly. +class SimpleHardware : public TargetHardware { + public: + // This is essentially a denylist. + // TODO(renjieliu): Consider whether we want an allowlist for custom op as + // well. + virtual bool IsNotSupportedOp(mlir::Operation* op) const = 0; + + // The larger the value is, the more preferrable over CPU. + // If the value > 1, means the hardware has advantage over CPU. + // If the value < 1, means CPU is more preferred. + // If we specify 10.0, meaning the hardware is 10x faster than CPU. + // The value should be > 0. + // TODO(renjieliu): Consider add an interface for more detailed customization, + // for example, users should be able to specify some ops are preferred and + // some are less preferred. + virtual float AdvantageOverCPU() const = 0; + + private: + bool IsOpSupported(mlir::Operation* op) const override; + + double GetHardwareSwitchingCost(const TargetHardware* from, + size_t buffer_size) const override; + + double GetOpCost(mlir::Operation* op) const override; +}; + +} // namespace tac +} // namespace TFL +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_HARDWARES_SIMPLE_HARDWARE_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/hardwares/target_hardware.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/hardwares/target_hardware.cc new file mode 100644 index 0000000000000000000000000000000000000000..bf89a831a914ef4bd900bd65b741e1c31cd72847 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/hardwares/target_hardware.cc @@ -0,0 +1,284 @@ +/* Copyright 2020 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/compiler/mlir/lite/experimental/tac/hardwares/target_hardware.h" + +#include +#include +#include +#include + +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/DenseMap.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/raw_ostream.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/OpDefinition.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/Support/TypeID.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/targets.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/utils.h" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/lite/utils/utils.h" + +namespace mlir { +namespace TFL { +namespace tac { +namespace { + +struct RegisteredTargetHardware { + RegisteredTargetHardware( + const std::string& name, const std::string& description, + mlir::TypeID type_id, + std::function()> target_hardware_factory) + : unique_name(GetCanonicalHardwareName(name)), + description(description), + type_id(type_id), + target_hardware(target_hardware_factory()), + target_hardware_factory(target_hardware_factory) {} + + std::string unique_name; + std::string description; + mlir::TypeID type_id; + std::unique_ptr target_hardware; + std::function()> target_hardware_factory; +}; + +struct RegisteredTargetHardwareOps { + explicit RegisteredTargetHardwareOps(mlir::TypeID hardware_type) + : hardware_typeid(hardware_type) {} + // Key is the Operation TypeID + llvm::DenseMap> + target_hardware_ops; + // Key is the Operation TypeID + llvm::DenseMap()>> + target_hardware_ops_factory; + mlir::TypeID hardware_typeid; +}; + +std::vector>* +GetRegisteredTargetHardwareOps() { + static std::vector>* + hardwares_ops = + []() -> std::vector>* { + return new std::vector>(); + }(); + return hardwares_ops; +} + +std::vector* GetRegisteredHardwares() { + static std::vector* hardwares = + []() -> std::vector* { + return new std::vector(); + }(); + return hardwares; +} + +llvm::DenseMap>* +getRegisteredOperationsForHardware(mlir::TypeID type_id) { + auto* hardwares = GetRegisteredTargetHardwareOps(); + for (auto& hardware : *hardwares) { + if (hardware->hardware_typeid == type_id) { + return &hardware->target_hardware_ops; + } + } + return nullptr; +} + +// A deny list for op cost computation since those ops are not arithemtic. +inline bool IsNonArithmeticOp(mlir::Operation* op) { + if (llvm::isa(op)) return true; + if (op->hasTrait()) return true; + if (llvm::isa(op)) return true; + if (!NotTFLQuantDequantizeOp(op)) return true; + return false; +} + +} // namespace + +bool TargetHardware::Init() { + auto* hardware_ops_factory = GetRegisteredTargetHardwareOps(); + for (auto& hardware_ops : *hardware_ops_factory) { + if (hardware_ops->hardware_typeid != this->GetTypeId()) continue; + auto& op_factories = hardware_ops->target_hardware_ops_factory; + for (auto& op_factory : op_factories) { + hardware_ops_.emplace_back(op_factory.getSecond()()); + } + break; + } + return true; +} + +double TargetHardware::GetOpCost(mlir::Operation* op) const { + auto* registered_ops = getRegisteredOperationsForHardware(GetTypeId()); + if (registered_ops == nullptr) { + return kDefaultFixedValuedCost; + } + auto abstract_op = op->getRegisteredInfo(); + auto hardware_op = registered_ops->find(abstract_op->getTypeID()); + if (hardware_op == registered_ops->end()) return kDefaultFixedValuedCost; + return hardware_op->second->GetOpCost(op); +} + +bool TargetHardware::IsOpSupported(mlir::Operation* op) const { + auto* registered_ops = getRegisteredOperationsForHardware(GetTypeId()); + if (registered_ops == nullptr) { + return false; + } + auto abstract_op = op->getRegisteredInfo(); + auto hardware_op = registered_ops->find(abstract_op->getTypeID()); + if (hardware_op == registered_ops->end()) return false; + return hardware_op->second->IsOpSupported(op); +} + +double TargetHardware::GetFuncCost(func::FuncOp* func) const { + double total_cost = 0.0; + func->walk([&](Operation* op) { + if (IsNonArithmeticOp(op)) return; + // We will always defer to the hardware to decide the cost. + total_cost += GetOpCost(op); + }); + return total_cost; +} + +const TargetHardware* GetTargetHardware(const std::string& hardware_name) { + const std::string canonical_name = GetCanonicalHardwareName(hardware_name); + // Just loop for now, we don't expect number of hardwares to be huge. + // Revisit to have map if number of elements increased. + auto* registered_hardwares = GetRegisteredHardwares(); + for (const auto& hardware : *registered_hardwares) { + if (hardware.unique_name == canonical_name) { + return hardware.target_hardware.get(); + } + } + return nullptr; +} + +std::function()> GetTargetHardwareFactory( + const std::string& hardware_name) { + const std::string canonical_name = GetCanonicalHardwareName(hardware_name); + // Just loop for now, we don't expect number of hardwares to be huge. + // Revisit to have map if number of elements increased. + auto* registered_hardwares = GetRegisteredHardwares(); + for (const auto& hardware : *registered_hardwares) { + if (hardware.unique_name == canonical_name) { + return hardware.target_hardware_factory; + } + } + return nullptr; +} + +namespace internal { + +void RegisterTargetHardwareFactory( + const std::string& unique_name, const std::string& description, + mlir::TypeID type_id, + std::function()> target_hardware_factory) { + auto* registered_hardwares = GetRegisteredHardwares(); + for (auto& hardware : *registered_hardwares) { + if (hardware.unique_name == unique_name) { + llvm::errs() << "Ignoring duplicate hardware. Hardware " << unique_name + << " already registered\n"; + hardware.target_hardware_factory = target_hardware_factory; + return; + } + } + registered_hardwares->push_back(RegisteredTargetHardware( + unique_name, description, type_id, target_hardware_factory)); +} + +void RegisterTargetHardwareOp( + mlir::TypeID hardware_type, mlir::TypeID op_type, + std::function()> + target_hardware_op_factory) { + auto* registered_hardware_ops = GetRegisteredTargetHardwareOps(); + for (auto& hardware : *registered_hardware_ops) { + if (hardware->hardware_typeid == hardware_type) { + if (hardware->target_hardware_ops.count(op_type)) { + llvm::errs() << "Trying to register duplicate Op"; + return; + } + hardware->target_hardware_ops[op_type] = target_hardware_op_factory(); + return; + } + } + registered_hardware_ops->push_back( + std::make_unique( + RegisteredTargetHardwareOps(hardware_type))); + registered_hardware_ops->back()->target_hardware_ops[op_type] = + target_hardware_op_factory(); +} + +void RegisterTargetHardwareOpFactory( + mlir::TypeID hardware_type, mlir::TypeID op_type, + std::function()> + target_hardware_op_factory) { + auto* registered_hardware_ops = GetRegisteredTargetHardwareOps(); + for (auto& hardware : *registered_hardware_ops) { + if (hardware->hardware_typeid == hardware_type) { + if (hardware->target_hardware_ops_factory.count(op_type)) { + llvm::errs() << "Trying to register duplicate Op"; + return; + } + hardware->target_hardware_ops_factory[op_type] = + target_hardware_op_factory; + return; + } + } + registered_hardware_ops->push_back( + std::make_unique( + RegisteredTargetHardwareOps(hardware_type))); + registered_hardware_ops->back()->target_hardware_ops_factory[op_type] = + target_hardware_op_factory; +} + +} // namespace internal + +bool ProcessTargetDevices(llvm::ArrayRef specified_device_specs, + std::vector* device_specs) { + bool cpu_include = false; + for (auto& device_spec : specified_device_specs) { + auto device = GetCanonicalHardwareName(device_spec); + + if (device == "CPU") cpu_include = true; + device_specs->push_back(device); + } + if (!cpu_include) { + device_specs->push_back("CPU"); + } + + // Make sure all the devices are registered. + for (const std::string& device : *device_specs) { + if (GetTargetHardware(device) == nullptr) { + llvm::errs() << "cannot get target hardware for device: " << device; + return false; + } + } + + return true; +} + +std::string GetHardwareName(const TargetHardware* hardware) { + const auto* registered_hardwares = GetRegisteredHardwares(); + for (const auto& registered_hardware : *registered_hardwares) { + if (registered_hardware.type_id == hardware->GetTypeId()) + return registered_hardware.unique_name; + } + return ""; +} + +} // namespace tac +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/hardwares/target_hardware.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/hardwares/target_hardware.h new file mode 100644 index 0000000000000000000000000000000000000000..e13cede45a373e5b8b416522c5c66c9ffb4ef863 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/hardwares/target_hardware.h @@ -0,0 +1,196 @@ +/* Copyright 2020 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_HARDWARES_TARGET_HARDWARE_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_HARDWARES_TARGET_HARDWARE_H_ + +#include +#include +#include +#include +#include + +#include "llvm/ADT/ArrayRef.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/DialectRegistry.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Support/TypeID.h" // from @llvm-project + +namespace mlir { +namespace TFL { +namespace tac { + +// Default fixed values for ops. +constexpr static float kDefaultFixedValuedCost = 1000000.0; + +// This is just fake data. +constexpr static float kCrossHardwareTransferPerByteCost = 5.0f; + +// This is just fake data. +constexpr static float kCrossHardwareTransferFixedCost = 10.f; + +// Interface for an Operation capabilities which should be tied to +// a specific hardware. +// Users should implement the interface and use TargetHardwareOpRegistration +// for registering the operation. +class TargetHardwareOperation { + public: + virtual ~TargetHardwareOperation() = default; + + virtual double GetOpCost(mlir::Operation* op) const = 0; + + virtual bool IsOpSupported(mlir::Operation* op) const = 0; +}; + +// Abstract base class for a hardware. +// To introduce new hardware +// users should implement the interface and use TargetHardwareRegistration +// for registering the hardware. +// Subclasses must implement the pure virtual function interface and +// define static member variable that retrieves string identifying the Target +// Hardware. Example, +// class MyType : public TargetHardware { +// public: +// static constexpr char kId[] = "MyHardware"; +// }; +class TargetHardware { + public: + virtual ~TargetHardware() = default; + + // Initializes all TargetHardwareOperation registered for this hardware. + // Users overriding this function, should call the base class method to + // initialize the ops. + virtual bool Init(); + + // Returns the cost of running 'op' on this Hardware. + virtual double GetOpCost(mlir::Operation* op) const; + + // Returns the cost of running the whole function on this hardware. + // By default this is the sum of the cost of individual cost for each op. + virtual double GetFuncCost(func::FuncOp* func) const; + + // Returns true if 'op' can run on this Hardware. + virtual bool IsOpSupported(mlir::Operation* op) const; + + // Switching cost between from hardware and this hardware. + // If both the hardwares are the same, the transfer cost is basically 0. + virtual double GetHardwareSwitchingCost(const TargetHardware* from, + size_t buffer_size) const = 0; + + // Returns a list of all patterns to apply for this hardware. + virtual mlir::RewritePatternSet GetTransformations( + MLIRContext* context) const = 0; + + // Returns TypeId for the provided hardware. + // Usually should be something like mlir::TypeID::get() + virtual mlir::TypeID GetTypeId() const = 0; + + virtual void GetDependentDialects(mlir::DialectRegistry& registry) const {} + + protected: + // All registered hardware ops. + std::vector> hardware_ops_; +}; + +// Returns pointer to the Hardware identified by 'hardware_name'. +// If not found nullptr is returned. +// DEPRECATED: Do not use, prefer GetTargetHardwareFactory instead. +const TargetHardware* GetTargetHardware(const std::string& hardware_name); + +// Returns the factory method for the requested hardware if present. +std::function()> GetTargetHardwareFactory( + const std::string& hardware_name); + +namespace internal { + +void RegisterTargetHardwareFactory( + const std::string& unique_name, const std::string& description, + mlir::TypeID type_id, + std::function()> target_hardware_factory); + +// Registers the provided target hardware factory. +template +void RegisterTargetHardwareFactory( + const std::string& description, + std::function()> target_hardware_factory) { + RegisterTargetHardwareFactory(T::kId, description, mlir::TypeID::get(), + target_hardware_factory); +} + +// DEPRECATED: Do not use, prefer RegisterTargetHardwareOpFactory intstead. +void RegisterTargetHardwareOp( + mlir::TypeID hardware_type, mlir::TypeID op_type, + std::function()> + target_hardware_op_factory); + +void RegisterTargetHardwareOpFactory( + mlir::TypeID hardware_type, mlir::TypeID op_type, + std::function()> + target_hardware_op_factory); +} // namespace internal + +// Register target hardware. +template +struct TargetHardwareRegistration { + TargetHardwareRegistration(const std::string& description, + std::function()> + target_hardware_factory) { + internal::RegisterTargetHardwareFactory(description, + target_hardware_factory); + } +}; + +// Register Op capabilities for specific hardware. +template +struct TargetHardwareOpRegistration { + explicit TargetHardwareOpRegistration( + std::function()> + target_hardware_op_factory) { + // TODO(b/177376459): remove this. + internal::RegisterTargetHardwareOp(mlir::TypeID::get(), + mlir::TypeID::get(), + target_hardware_op_factory); + internal::RegisterTargetHardwareOpFactory(mlir::TypeID::get(), + mlir::TypeID::get(), + target_hardware_op_factory); + } +}; + +//======== util functions ========== + +// Process user specified device specs, will always add CPU if it's not there. +// specified_device_specs: ',' separated, like "GPU,DSP,CPU". +// device_specs: processed device specs enum. +bool ProcessTargetDevices(llvm::ArrayRef specified_device_specs, + std::vector* device_specs); + +// Check whether two hardwares are the same. +inline bool IsSameHardware(const TargetHardware* lhs, + const TargetHardware* rhs) { + return lhs->GetTypeId() == rhs->GetTypeId(); +} + +// Returns the ID identifying 'hardware'. This should match the ID defined +// in the hardware field ID. +// For example, if MyHardware is passed the value returned should match +// MyHardware::kId. +std::string GetHardwareName(const TargetHardware* hardware); + +} // namespace tac +} // namespace TFL +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_HARDWARES_TARGET_HARDWARE_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/py_wrapper/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/py_wrapper/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..1e45c2a53cbb5cd40b2c81ccd08f9723bdb5f0de --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/py_wrapper/BUILD @@ -0,0 +1,110 @@ +load("@rules_cc//cc:cc_library.bzl", "cc_library") +load("//tensorflow:tensorflow.bzl", "VERSION") +load("//tensorflow:tensorflow.default.bzl", "pybind_extension") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + default_visibility = [ + "//tensorflow/compiler/mlir/lite/experimental/tac:__subpackages__", + ], + licenses = ["notice"], +) + +cc_library( + name = "tac_wrapper_lib", + srcs = ["tac_wrapper.cc"], + hdrs = [ + "tac_wrapper.h", + ], + deps = [ + "//tensorflow/compiler/mlir/lite/experimental/tac:tac_importer_exporter", + "//tensorflow/compiler/mlir/lite/experimental/tac:target_aware_conversion", + "//tensorflow/compiler/mlir/lite/experimental/tac:tflite_importer_exporter", + "//tensorflow/compiler/mlir/lite/experimental/tac/hardwares:all-target-hardwares", + "//tensorflow/compiler/mlir/tensorflow", + "@com_google_absl//absl/status", + "@llvm-project//mlir:IR", + "@xla//third_party/python_runtime:headers", # buildcleaner: keep + ], +) + +pybind_extension( + name = "_pywrap_tac_wrapper", + srcs = [ + "tac_wrapper_pybind11.cc", + ], + hdrs = ["tac_wrapper.h"], + dynamic_deps = select({ + "//tensorflow:macos": ["//tensorflow:libtensorflow_framework.%s.dylib" % VERSION], + "//tensorflow:windows": [], + "//conditions:default": ["//tensorflow:libtensorflow_framework.so.%s" % VERSION], + }), + enable_stub_generation = True, + pytype_srcs = [ + "_pywrap_tac_wrapper.pyi", + ], + static_deps = [ + "@arm_neon_2_x86_sse//:__subpackages__", + "@bazel_tools//:__subpackages__", + "@boringssl//:__subpackages__", + "@clog//:__subpackages__", + "@com_github_cares_cares//:__subpackages__", + "@com_github_googlecloudplatform_tensorflow_gcp_tools//:__subpackages__", + "@com_github_grpc_grpc//:__subpackages__", + "@com_google_absl//:__subpackages__", + "@com_google_googleapis//:__subpackages__", + "@com_google_protobuf//:__subpackages__", + "@com_googlesource_code_re2//:__subpackages__", + "@compute_library//:__subpackages__", + "@cpuinfo//:__subpackages__", + "@curl//:__subpackages__", + "@eigen_archive//:__subpackages__", + "@farmhash_archive//:__subpackages__", + "@farmhash_gpu_archive//:__subpackages__", + "@fft2d//:__subpackages__", + "@flatbuffers//:__subpackages__", + "@FP16//:__subpackages__", + "@FXdiv//:__subpackages__", + "@gemmlowp//:__subpackages__", + "@go_googleapis//google/api:annotations_proto", + "@gif//:__subpackages__", + "@highwayhash//:__subpackages__", + "@hwloc//:__subpackages__", + "@icu//:__subpackages__", + "@jsoncpp_git//:__subpackages__", + "@libjpeg_turbo//:__subpackages__", + "@llvm_openmp//:__subpackages__", + "@llvm-project//:__subpackages__", + "@llvm_terminfo//:__subpackages__", + "@llvm_zlib//:__subpackages__", + "@local_config_cuda//:__subpackages__", + "@local_config_git//:__subpackages__", + "@local_config_nccl//:__subpackages__", + "@local_config_python//:__subpackages__", + "@local_config_rocm//:__subpackages__", + "@local_config_tensorrt//:__subpackages__", + "@mkl_dnn_acl_compatible//:__subpackages__", + "@onednn//:__subpackages__", + "@org_sqlite//:__subpackages__", + "@platforms//:__subpackages__", + "@png//:__subpackages__", + "@pthreadpool//:__subpackages__", + "@pybind11//:__subpackages__", + "@ruy//:__subpackages__", + "@snappy//:__subpackages__", + "@sobol_data//:__subpackages__", + "@stablehlo//:__subpackages__", + "//:__subpackages__", + "@upb//:__subpackages__", + "@XNNPACK//:__subpackages__", + "@zlib//:__subpackages__", + "@tsl//tsl:__subpackages__", + "@xla//xla:__subpackages__", + ], + deps = [ + ":tac_wrapper_lib", + "//tensorflow/python/lib/core:pybind11_lib", + "@pybind11", + "@xla//third_party/python_runtime:headers", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/py_wrapper/_pywrap_tac_wrapper.pyi b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/py_wrapper/_pywrap_tac_wrapper.pyi new file mode 100644 index 0000000000000000000000000000000000000000..ee645ca929ce8332bb4bda3b5d40ba8276e0d433 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/py_wrapper/_pywrap_tac_wrapper.pyi @@ -0,0 +1,16 @@ +# Copyright 2023 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. +# ============================================================================== + +def run_tac(arg0: str, arg1: list[str], arg2: str) -> bool: ... diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/py_wrapper/tac_wrapper.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/py_wrapper/tac_wrapper.cc new file mode 100644 index 0000000000000000000000000000000000000000..ecb0cda14a56ea5398a66be8eedd4f596d133c56 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/py_wrapper/tac_wrapper.cc @@ -0,0 +1,72 @@ +/* Copyright 2021 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/compiler/mlir/lite/experimental/tac/py_wrapper/tac_wrapper.h" + +#include +#include +#include + +#include "absl/status/status.h" +#include "mlir/IR/DialectRegistry.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/tac/tac_importer_exporter.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/tac_module.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/tflite_import_export.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/transforms/passes.h" +#include "tensorflow/compiler/mlir/tensorflow/dialect_registration.h" + +namespace tflite { +namespace { +std::unique_ptr CreateTfLiteImporter( + const std::string input_file_name) { + mlir::TFL::tac::TfLiteImporter::Options options; + options.file_name = input_file_name; + options.input_mlir = false; + return std::make_unique(options); +} + +std::unique_ptr CreateTfLiteExporter( + const std::string output_file_name, + const std::vector& target_hardware_backends) { + mlir::TFL::tac::TfLiteExporter::Options options; + options.output_mlir = false; + options.output_file_name = output_file_name; + options.export_runtime_metadata = false; + options.target_hardware_backends = target_hardware_backends; + return std::make_unique(options); +} +} // namespace + +// Run target-aware-conversion for the given tflite model with the given device +// specs. +// Warning: The API is experimental and subject to changes. +bool run_tac(const std::string& model_file_path, + const std::vector& device_specs, + const std::string& model_output_path) { + mlir::TFL::tac::TacModule::Options options; + options.hardware_backends = device_specs; + options.enable_inliner = true; + options.legalize_to_tflite_ops = true; + mlir::TFL::tac::TacModule tac_module(options); + mlir::DialectRegistry registry; + mlir::RegisterAllTensorFlowDialects(registry); + tac_module.RegisterExtraDialects(registry); + tac_module.SetImporter(CreateTfLiteImporter(model_file_path)); + tac_module.SetExporter( + CreateTfLiteExporter(model_output_path, options.hardware_backends)); + return tac_module.Run().ok(); +} + +} // namespace tflite diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/py_wrapper/tac_wrapper.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/py_wrapper/tac_wrapper.h new file mode 100644 index 0000000000000000000000000000000000000000..c51b7be7f719ae6a31223933e24684ecea235c6a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/py_wrapper/tac_wrapper.h @@ -0,0 +1,42 @@ +/* Copyright 2021 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_PY_WRAPPER_TAC_WRAPPER_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_PY_WRAPPER_TAC_WRAPPER_H_ + +#include +#include +#include +#include + +// Place `` before to avoid build failures in macOS. +#include + +// The empty line above is on purpose as otherwise clang-format will +// automatically move before . +#include + +namespace tflite { + +// Run target-aware-conversion for the given tflite model with the given device +// specs. +// Warning: The API is experimental and subject to change. +bool run_tac(const std::string& model_file_path, + const std::vector& device_specs, + const std::string& model_output_path); + +} // namespace tflite + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_PY_WRAPPER_TAC_WRAPPER_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/py_wrapper/tac_wrapper_pybind11.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/py_wrapper/tac_wrapper_pybind11.cc new file mode 100644 index 0000000000000000000000000000000000000000..6d3f566976754a072e2cde05f227a92561c8705a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/py_wrapper/tac_wrapper_pybind11.cc @@ -0,0 +1,37 @@ +/* Copyright 2021 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 "pybind11/pybind11.h" // from @pybind11 +#include "tensorflow/compiler/mlir/lite/experimental/tac/py_wrapper/tac_wrapper.h" + +// Warning: The API is experimental and subject to change. +PYBIND11_MODULE(_pywrap_tac_wrapper, m) { + m.def( + "run_tac", + [](const std::string& model_file_path, + const std::vector& device_specs, + const std::string& model_output_path) { + return ::tflite::run_tac(model_file_path, device_specs, + model_output_path); + }, + R"pbdoc( + Run target-aware-conversion with the given device specs. + )pbdoc"); +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/runtime_metadata.fbs b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/runtime_metadata.fbs new file mode 100644 index 0000000000000000000000000000000000000000..2cb859818183ae651cf9b7554d7d51a817887593 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/runtime_metadata.fbs @@ -0,0 +1,89 @@ +// Copyright 2020 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. + +namespace tflite; + +// Here is an overview of the pipeline & runtime metadata looks like: +// +// ======== IRs after TAC ======== +// op1 = .... {tac.device = "CPU"} +// op2 = .... {tac.device = "GPU"} +// op3 = .... {tac.device = "GPU"} +// op4 = .... {tac.device = "CPU"} +// +// We will run a separate cost estimation for each op with each different +// hardwares. +// +// ======== IRs after cost-estimation ======== +// op1 = .... {tac.device = "CPU"} {"CPU": 10, "GPU": -1.0} +// op2 = .... {tac.device = "GPU"} {"CPU": 20, "GPU": 5.0} +// op3 = .... {tac.device = "GPU"} {"CPU": 30, "GPU": 10.0} +// op4 = .... {tac.device = "CPU"} {"CPU": 40, "GPU": -1.0} +// +// The exported runtime metadata will look like below: +// ======== Runtime Metadata ======== +// +// RuntimeMetadata: { +// hardwares: { +// ["CPU", "GPU"] +// } +// +// subgraph_metadata: { +// [ +// op1: +// hardware: 0 +// op_costs: [10, -1], +// op2: +// hardware: 1 +// op_costs: [20, 5], +// op3: +// hardware: 1 +// op_costs: [30, 10], +// op4: +// hardware: 0 +// op_costs: [40, -1], +// ] +// } +// } +table HardwareMetadata { + // List of different hardwares this model can use. + hardware_name:[string]; +} + +// Metadata for a single Op. +table OpMetadata { + // Node index in the corresponding subgraph. + index:int; + // The hardware suggested for inference. + // This will correspond to the index in the hardware_name field in the + // hardware metadata. + hardware:uint8; + // The estimated costs for the given op. + // The index matches the hardware array. + op_costs: [float]; +} + +table SubgraphMetadata { + // Metadata for all ops in the current subgraph. + op_metadata:[OpMetadata]; +} + +table RuntimeMetadata { + // Metadata about different hardwares used by the model. + hardwares: HardwareMetadata; + // Metadata for each subgraph in same order as in the TFLite model file. + subgraph_metadata:[SubgraphMetadata]; +} + +root_type RuntimeMetadata; diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tac.py b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tac.py new file mode 100644 index 0000000000000000000000000000000000000000..89c61444a1c1c04567a498dc855315e0e2df48c8 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tac.py @@ -0,0 +1,46 @@ +# Copyright 2021 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. +# ============================================================================== +"""Target aware conversion for TFLite model.""" + +from tensorflow.compiler.mlir.lite.experimental.tac.py_wrapper import _pywrap_tac_wrapper + + +def run_tac(model_path, targets, output_path): + """Run target aware conversion for the given tflite model file. + + Args: + model_path: Path to the tflite model file. + targets: A list of string of the desired targets. E.g., ['GPU', 'CPU']. + output_path: The output path. + + Returns: + Whether the optimization succeeded. + + Raises: + ValueError: + Invalid model_path. + Targets are not specified. + Invalid output_path. + """ + if not model_path: + raise ValueError("Invalid model_path.") + + if not targets: + raise ValueError("Targets are not specified.") + + if not output_path: + raise ValueError("Invalid output_path.") + + return _pywrap_tac_wrapper.run_tac(model_path, targets, output_path) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tac_filter.proto b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tac_filter.proto new file mode 100644 index 0000000000000000000000000000000000000000..27de55b011e95b05f4798b4e6690d5883046e8b6 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tac_filter.proto @@ -0,0 +1,72 @@ +syntax = "proto3"; + +package third_party.tensorflow.compiler.mlir.lite.experimental.tac; + +import "google/protobuf/any.proto"; + +// A list of filters for TAC users to run ops/functions on ML hardwares. The +// intuition is that, for ops/functions that can be run on ML hardware (e.g. +// EdgeTPU) and TFLite CPU, TAC users give a hint that they're more performant +// to run on TFLite CPU. These filters give the TAC users freedom to specify the +// parts that they want to use other hardware to accelerate. +message TacFilters { + // A list of filters/rules to specify the parts that user wants to run on + // other hardware. + repeated TacFilter tac_filters = 1; +} + +// A filter can be used for an op or function. +message TacFilter { + oneof filter { + OpFilter op_filter = 1; + FunctionFilter function_filter = 2; + } +} + +// Function filter is to include/exclude a function in the target annotation +// pass in the TAC tool pipeline. +message FunctionFilter { + // Function filter types that are supported. If one function is matched for + // two rules with conflict, INCLUDE_TARGET_ANNOTATION has higher priority. + enum FunctionFilterType { + // To skip this function in the target annotation pass. This means all ops + // in this function run on TFLite CPU. + SKIP_TARGET_ANNOTATION = 0; + // To include this function in the target annotation pass. This has higher + // priority than `SKIP_TARGET_ANNOTATION`. + INCLUDE_TARGET_ANNOTATION = 1; + } + // This name corresponds to the TFLite subgraph name in the flatbuffer. + // `function_name_pattern` supports regex matching. + string function_name_pattern = 1; + FunctionFilterType filter_type = 2; +} + +// Op filter is to filter out ops that user wants to run. Ops with this filter +// run on TFLite CPU. +message OpFilter { + // This name corresponds to the mlir::Location of the tensor. + // `op_name_pattern` supports regex matching. + string op_name_pattern = 1; + enum MatchType { + // To match the op name with the `op_name_pattern` directly. + MATCH = 0; + // To invert the match with the `op_name_pattern`, ie, the filter will + // select an op if its name does not match the pattern above. + INVERT_MATCH = 1; + } + MatchType match_type = 2; + enum DeviceType { + // To run the op on CPU. + CPU = 0; + // To run the op on an hardware unit other than the CPU. + OTHER = 1; + } + DeviceType device_type = 3; + // An arbitrary string that can be used to identify the filter or a set of + // filters. + string tag = 4; + // Custom options that can be used to pass additional information for the + // filter. + google.protobuf.Any custom_options = 5; +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tac_importer_exporter.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tac_importer_exporter.h new file mode 100644 index 0000000000000000000000000000000000000000..93b24efb75be59c968a6455863d8729cd2db45c7 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tac_importer_exporter.h @@ -0,0 +1,52 @@ +/* Copyright 2020 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_TAC_IMPORTER_EXPORTER_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_TAC_IMPORTER_EXPORTER_H_ + +#include "absl/status/statusor.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project + +namespace mlir { +namespace TFL { +namespace tac { + +// Interface for Importing program to TAC (Target Aware Conversion) Module. +// This class is an interface for importing program in TAC. +// See TacModule in how to register it with the module and use it. +class TacImporter { + public: + virtual ~TacImporter() = default; + + // Imports and returns the Module for the imported program. + virtual absl::StatusOr> Import() = 0; +}; + +// Interface for exporting a module. +// Users should implement the interface for exporting the result from TAC +// in their preferred way. +// See TacModule in how to register it with the module and use it. +class TacExporter { + public: + virtual ~TacExporter() = default; + + // Imports and returns the Module for the imported program. + virtual absl::Status Export(mlir::ModuleOp module) = 0; +}; +} // namespace tac +} // namespace TFL +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_TAC_IMPORTER_EXPORTER_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tac_module.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tac_module.cc new file mode 100644 index 0000000000000000000000000000000000000000..6b6a535bdd9397a33cb92960a9f3ad315d0d44ee --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tac_module.cc @@ -0,0 +1,149 @@ +/* Copyright 2021 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/compiler/mlir/lite/experimental/tac/tac_module.h" + +#include +#include +#include + +#include "absl/status/status.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Transforms/Passes.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/tac/hardwares/target_hardware.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/transforms/passes.h" +#include "tensorflow/compiler/mlir/lite/transforms/passes.h" +#include "tensorflow/compiler/mlir/tensorflow/utils/error_util.h" + +namespace mlir { +namespace TFL { +namespace tac { +namespace { +// TODO(b/177376459): We should make this configureable. +void AddExportTFLPass(mlir::OpPassManager* pass_manager, bool enable_inliner) { + if (enable_inliner) pass_manager->addPass(mlir::createInlinerPass()); + pass_manager->addPass(mlir::createSymbolDCEPass()); + pass_manager->addNestedPass( + mlir::createCanonicalizerPass()); + pass_manager->addNestedPass(mlir::createCSEPass()); +} +} // namespace + +// TODO(b/177376459): We should make this configureable. +void TacModule::AddTACPass(mlir::OpPassManager* pass_manager, + llvm::ArrayRef device_specs) { + pass_manager->addPass(mlir::TFL::tac::CreateTargetAnnotationPass(this)); + pass_manager->addPass(mlir::TFL::tac::CreateRaiseTargetSubgraphsPass()); + pass_manager->addPass(mlir::TFL::tac::CreateFoldConstantsToSubgraphPass( + /*fold_all_constants=*/false)); + pass_manager->addPass( + mlir::TFL::tac::CreateAlternativeSubgraphPass(device_specs)); + if (options_.legalize_to_tflite_ops) { + // After we creat the alternative subgraph, we can still do canonicalization + // legalization & other optimizations as long as we're not inlining the + // function. + // And in fact, we probably need to do the proper legalization, for the + // compute cost to work. (in case we added some TF ops) + pass_manager->addPass(mlir::TFL::CreatePrepareTFPass( + /*unfold_batch_matmul=*/true, + /*allow_bf16_and_f16_type_legalization=*/false)); + pass_manager->addNestedPass( + mlir::createCanonicalizerPass()); + pass_manager->addPass( + mlir::TFL::CreateLegalizeTFPass(/*run_tfl_runtime_verification=*/true)); + pass_manager->addPass(mlir::TFL::CreateOptimizePass()); + } + + pass_manager->addPass(mlir::TFL::tac::CreateComputeCostPass()); + pass_manager->addPass(mlir::TFL::tac::CreatePickSubgraphsPass()); + // After this pass, we may consider add a pass to merge small functions into + // large functions (and maybe other metadata as well). +} + +const tac::TargetHardware* TacModule::GetTargetHardware( + const std::string& hardware_name) const { + for (auto& hardware : backends_) { + if (GetHardwareName(hardware.get()) == hardware_name) return hardware.get(); + } + return nullptr; +} + +absl::Status TacModule::RunTacPasses(mlir::ModuleOp* module, bool debug_mode) { + mlir::PassManager pm((*module)->getName(), + mlir::OpPassManager::Nesting::Implicit); + AddTACPass(&pm, options_.hardware_backends); + if (!debug_mode) { + AddExportTFLPass(&pm, options_.enable_inliner); + } + + mlir::StatusScopedDiagnosticHandler statusHandler(module->getContext(), + /*propagate=*/true); + if (failed(pm.run(*module))) { + return absl::InternalError("conversion error"); + } + return absl::OkStatus(); +} + +std::vector> +TacModule::InstantiateBackends() { + std::vector> backends; + for (const auto& hardware_name : options_.hardware_backends) { + auto factory = tac::GetTargetHardwareFactory(hardware_name); + backends.emplace_back(factory()); + backends.back()->Init(); + } + return backends; +} + +absl::Status TacModule::Run() { + // Construct all backends. + backends_ = InstantiateBackends(); + const_backends_.resize(backends_.size()); + for (const auto& backend : backends_) + const_backends_.emplace_back(backend.get()); + + if (!importer_) { + return absl::Status(absl::StatusCode::kFailedPrecondition, + "Null Importer provided"); + } + if (!exporter_) { + return absl::Status(absl::StatusCode::kFailedPrecondition, + "Null Exporter provided"); + } + + auto module_status = importer_->Import(); + if (!module_status.ok()) { + return module_status.status(); + } + auto module = module_status->get(); + auto* context = module->getContext(); + context->appendDialectRegistry(registry_); + context->loadAllAvailableDialects(); + + // Run TAC passes. + auto status = RunTacPasses(&module, options_.debug_mode); + + if (!status.ok()) { + return status; + } + + return exporter_->Export(module); +} + +void TacModule::RegisterExtraDialects(mlir::DialectRegistry& registry) { + registry.appendTo(registry_); +} +} // namespace tac +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tac_module.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tac_module.h new file mode 100644 index 0000000000000000000000000000000000000000..b2fb60484eb20b4a10d05b418826c1bbd916a232 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tac_module.h @@ -0,0 +1,123 @@ +/* Copyright 2021 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_TAC_MODULE_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_TAC_MODULE_H_ + +#include +#include +#include +#include + +#include "absl/status/status.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/Pass/PassManager.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/tac/hardwares/target_hardware.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/tac_importer_exporter.h" + +namespace mlir { +namespace TFL { +namespace tac { + +// Main class for using Target Aware Conversion (TAC). +// To run TAC: +// 1) users should create object form this class, with desired options +// (TacModule::Options). +// 2) Use SetImporter/SetExporter to the desired importer +// and exporter. +// 3) Call Run() +// +// The module fetches all TargetHardware backends registered in the binary +// and only create TargetHardware requested in Options. +// +// This class is not thread safe. +class TacModule { + public: + // TAC options. Contains knobs to configure TAC as needed. + struct Options { + // List of names for the requested Target hardware. + std::vector hardware_backends; + // Debug mode. + // This will output different alternative subgraphs in mlir format for debug + // purpose. + bool debug_mode = false; + // Whether to enable inliner passes or not. + bool enable_inliner = false; + // Whether to legalize ops to TFLite ops before exporting. + bool legalize_to_tflite_ops = false; + }; + + virtual ~TacModule() = default; + + explicit TacModule(const Options& options) : options_(options) {} + + void SetImporter(std::unique_ptr importer) { + importer_ = std::move(importer); + } + + void SetExporter(std::unique_ptr exporter) { + exporter_ = std::move(exporter); + } + + // Returns pointer to the TargetHardware that is identified by 'hardware_name' + // Returns NULL If no hardware with this name found. + const tac::TargetHardware* GetTargetHardware( + const std::string& hardware_name) const; + + // Runs the TAC workflow, configured as in the options provided during + // construction. + // SetImporter/SetExporter should be called prior to invoking `Run`. + // Returns Status of the Run. + virtual absl::Status Run(); + + // Returns all available hardware backends registered in this module + // instance. + const std::vector& GetAvailableHardwares() const { + return const_backends_; + } + + // Registers all dialects in 'registry' with the module. + // This to allow clients to register extra dialects required. + void RegisterExtraDialects(mlir::DialectRegistry& registry); + + protected: + // Adds TAC passes to the 'pass_manager'. + virtual void AddTACPass(mlir::OpPassManager* pass_manager, + llvm::ArrayRef device_specs); + + private: + // Runs all TAC passes on the provided module. + absl::Status RunTacPasses(mlir::ModuleOp* module, bool debug_mode = false); + + // Create instances of all registered hardwares. + std::vector> InstantiateBackends(); + + std::unique_ptr importer_; + std::unique_ptr exporter_; + // Owned list of all target hardware backends. + std::vector> backends_; + // Holder for const pointers for the data in 'backends_' + std::vector const_backends_; + // Extra dialects requested by the user. + mlir::DialectRegistry registry_; + + const Options options_; +}; + +} // namespace tac +} // namespace TFL +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_TAC_MODULE_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tac_translate.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tac_translate.cc new file mode 100644 index 0000000000000000000000000000000000000000..d337c503f0a16e09b1e26d86fdad636c0c533b3b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tac_translate.cc @@ -0,0 +1,148 @@ +/* Copyright 2021 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 "absl/log/log.h" +#include "absl/status/status.h" +#include "absl/strings/str_split.h" +#include "llvm/ADT/SmallString.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/Path.h" +#include "llvm/Support/SourceMgr.h" +#include "llvm/Support/ToolOutputFile.h" +#include "llvm/Support/raw_ostream.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/Parser/Parser.h" // from @llvm-project +#include "mlir/Pass/PassManager.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/FileUtilities.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Tools/mlir-translate/Translation.h" // from @llvm-project +#include "mlir/Transforms/Passes.h" // from @llvm-project +#include "tensorflow/compiler/mlir/init_mlir.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/targets.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/utils.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/execution_metadata_exporter.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/hardwares/target_hardware.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/tac_module.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/tflite_import_export.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/transforms/passes.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/utils/utils.h" +#include "tensorflow/compiler/mlir/lite/transforms/passes.h" +#include "tensorflow/compiler/mlir/tensorflow/dialect_registration.h" +#include "tensorflow/compiler/mlir/tensorflow/transforms/passes.h" +#include "tensorflow/compiler/mlir/tensorflow/utils/error_util.h" +#include "tensorflow/core/platform/init_main.h" + +using llvm::cl::opt; + +// NOLINTNEXTLINE +opt input_file_name(llvm::cl::Positional, + llvm::cl::desc(""), + llvm::cl::init("-")); + +// NOLINTNEXTLINE +opt output_file_name("o", llvm::cl::desc(""), + llvm::cl::value_desc("filename"), + llvm::cl::init("-")); +// NOLINTNEXTLINE +opt input_mlir("input-mlir", + llvm::cl::desc("Input is MLIR rather than FlatBuffer"), + llvm::cl::init(false)); + +// NOLINTNEXTLINE +opt output_mlir( + "output-mlir", + llvm::cl::desc( + "Output MLIR rather than FlatBuffer for the generated TFLite model"), + llvm::cl::init(false)); + +// NOLINTNEXTLINE +opt inline_subgraphs( + "inline-subgraphs", + llvm::cl::desc("Whether or not to inline all the subgraphs"), + llvm::cl::init(true)); + +// NOLINTNEXTLINE +opt device_specs( + "device-specs", llvm::cl::desc("comma separated list of device specs."), + llvm::cl::init("")); + +// NOLINTNEXTLINE +opt export_runtime_metadata( + "export-runtime-metadata", + llvm::cl::desc("Whether or not to export metadata, if yes, the metadata " + "file will be exported along with the output model"), + llvm::cl::init(false)); + +namespace { + +std::unique_ptr CreateTfLiteImporter() { + mlir::TFL::tac::TfLiteImporter::Options options; + options.file_name = input_file_name; + options.input_mlir = input_mlir; + return std::make_unique(options); +} + +std::unique_ptr CreateTfLiteExporter( + const std::vector& target_hardware_backends) { + mlir::TFL::tac::TfLiteExporter::Options options; + options.output_mlir = output_mlir; + options.output_file_name = output_file_name; + options.export_runtime_metadata = export_runtime_metadata; + options.target_hardware_backends = target_hardware_backends; + return std::make_unique(options); +} + +absl::Status TargetAwareConversionMain() { + std::vector device_specs_array = + absl::StrSplit(device_specs, ',', absl::SkipEmpty()); + mlir::TFL::tac::TacModule::Options options; + options.hardware_backends = device_specs_array; + options.debug_mode = true; + if (!output_mlir || inline_subgraphs) { + options.debug_mode = false; + } + options.enable_inliner = true; + options.legalize_to_tflite_ops = true; + mlir::TFL::tac::TacModule tac_module(options); + mlir::DialectRegistry registry; + mlir::RegisterAllTensorFlowDialects(registry); + tac_module.RegisterExtraDialects(registry); + tac_module.SetImporter(CreateTfLiteImporter()); + tac_module.SetExporter(CreateTfLiteExporter(options.hardware_backends)); + return tac_module.Run(); +} +} // namespace + +int main(int argc, char** argv) { + tensorflow::InitMlir y(&argc, &argv); + + llvm::cl::ParseCommandLineOptions(argc, argv, "Target aware conversion\n"); + + absl::Status status = TargetAwareConversionMain(); + if (!status.ok()) { + LOG(ERROR) << status; + } + return 0; +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..42df6692fa4f0d6c4afd8535d59e058e1c3e4f1f --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/BUILD @@ -0,0 +1,26 @@ +load("//tensorflow:tensorflow.default.bzl", "filegroup") +load("//tensorflow/compiler/mlir:glob_lit_test.bzl", "glob_lit_tests") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + licenses = ["notice"], +) + +glob_lit_tests( + name = "all_tests", + data = [":test_utilities"], + driver = "@llvm-project//mlir:run_lit.sh", + test_file_exts = ["mlir"], +) + +# Bundle together all of the test utilities that are used by tests. +filegroup( + name = "test_utilities", + testonly = True, + data = [ + "//tensorflow/compiler/mlir/lite/experimental/common:outline_operations", + "//tensorflow/compiler/mlir/lite/experimental/tac:tac-opt-all-backends", + "@llvm-project//llvm:FileCheck", + "@llvm-project//llvm:not", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/compute-cost.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/compute-cost.mlir new file mode 100644 index 0000000000000000000000000000000000000000..2bd4247c1bf9e80ca3650e2d14fee49e5f3b4ae3 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/compute-cost.mlir @@ -0,0 +1,51 @@ +// RUN: tac-opt-all-backends -tfl-compute-cost %s -split-input-file -verify-diagnostics | FileCheck %s + +// CHECK: tac.cost = 7.864320e+05 +func.func @func_0_CPU(%arg0: tensor<256x32x32x3xf32>, %arg1: tensor<256x32x32x3xf32>) -> tensor<256x32x32x3xf32> attributes {tac.device = "CPU", tac.interface_name = "func_0"} { + %0 = "tfl.add"(%arg0, %arg1) {fused_activation_function = "RELU", tac.device = "CPU"} : (tensor<256x32x32x3xf32>, tensor<256x32x32x3xf32>) -> tensor<256x32x32x3xf32> + func.return %0 : tensor<256x32x32x3xf32> +} + +// CHECK: tac.cost = 157286.4 +func.func @func_0_GPU(%arg0: tensor<256x32x32x3xf32>, %arg1: tensor<256x32x32x3xf32>) -> tensor<256x32x32x3xf32> attributes {tac.device = "GPU", tac.interface_name = "func_0"} { + %0 = "tfl.add"(%arg0, %arg1) {fused_activation_function = "RELU", tac.device = "GPU"} : (tensor<256x32x32x3xf32>, tensor<256x32x32x3xf32>) -> tensor<256x32x32x3xf32> + func.return %0 : tensor<256x32x32x3xf32> +} + +// ----- + +// CHECK: tac.cost = 1.000000e+03 +func.func @func_0_CPU(%arg0: tensor<10x10x10xf32>, %arg1: tensor<10xf32>) -> tensor<10x10x10xf32> attributes {tac.device = "CPU", tac.interface_name = "func_0"} { + %0 = "tfl.add"(%arg0, %arg1) {fused_activation_function = "RELU", tac.device = "CPU"} : (tensor<10x10x10xf32>, tensor<10xf32>) -> tensor<10x10x10xf32> + func.return %0 : tensor<10x10x10xf32> +} + +// ----- + +// CHECK: tac.cost = 2.000000e+02 +func.func @func_0_GPU(%arg0: tensor<10x10x10xf32>, %arg1: tensor) -> tensor<10x10x10xf32> attributes {tac.device = "GPU", tac.interface_name = "func_0"} { + %0 = "tfl.add"(%arg0, %arg1) {fused_activation_function = "RELU", tac.device = "GPU"} : (tensor<10x10x10xf32>, tensor) -> tensor<10x10x10xf32> + func.return %0 : tensor<10x10x10xf32> +} + +// ----- + +// CHECK: tac.cost = 2.000000e+03 +func.func @func_0_CPU(%arg0: tensor<10x10x10xf32>, %arg1: tensor<10xf32>) -> tensor<10x10x10xf32> attributes {tac.device = "CPU", tac.interface_name = "func_0"} { + %0 = "tfl.add"(%arg0, %arg1) {fused_activation_function = "RELU", tac.device = "CPU"} : (tensor<10x10x10xf32>, tensor<10xf32>) -> tensor<10x10x10xf32> + %1 = "tfl.mul"(%0, %arg1) {fused_activation_function = "RELU", tac.device = "CPU"} : (tensor<10x10x10xf32>, tensor<10xf32>) -> tensor<10x10x10xf32> + func.return %1 : tensor<10x10x10xf32> +} + +// ----- + +// CHECK: tac.cost = 0x4B673001 +func.func @quantize_ops_CPU_QUANTIZED_INT8(%arg0: tensor<384x512x!quant.uniform>, %arg1: tensor<128x512x!quant.uniform:f32, 0.1>>, %arg2: tensor<128x!quant.uniform>, %arg3: tensor<128x!quant.uniform>) -> tensor<1x384x128x!quant.uniform> attributes {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8", tac.interface_name = "quantize_ops"} { + %0 = "tfl.pseudo_qconst"() {qtype = tensor<128x!quant.uniform>, value = dense<0> : tensor<128xi32>} : () -> tensor<128x!quant.uniform> + %1 = "tfl.fully_connected"(%arg0, %arg1, %0) {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8", fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"} : (tensor<384x512x!quant.uniform>, tensor<128x512x!quant.uniform:f32, 0.1>>, tensor<128x!quant.uniform>) -> tensor<384x128x!quant.uniform> + %2 = "tfl.pseudo_const"() {value = dense<[1, 384, 128]> : tensor<3xi32>} : () -> tensor<3xi32> + %3 = "tfl.reshape"(%1, %2) {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8"} : (tensor<384x128x!quant.uniform>, tensor<3xi32>) -> tensor<1x384x128x!quant.uniform> + %4 = "tfl.mul"(%3, %arg2) {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8", fused_activation_function = "NONE"} : (tensor<1x384x128x!quant.uniform>, tensor<128x!quant.uniform>) -> tensor<1x384x128x!quant.uniform> + %5 = "tfl.add"(%4, %arg3) {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8", fused_activation_function = "NONE"} : (tensor<1x384x128x!quant.uniform>, tensor<128x!quant.uniform>) -> tensor<1x384x128x!quant.uniform> + func.return %5 : tensor<1x384x128x!quant.uniform> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/device-transform-gpu.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/device-transform-gpu.mlir new file mode 100644 index 0000000000000000000000000000000000000000..af484a136d000eeda8acf2a155cfba33f5e38c7f --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/device-transform-gpu.mlir @@ -0,0 +1,225 @@ +// RUN: tac-opt-all-backends -tfl-device-transform-gpu %s -split-input-file -verify-diagnostics | FileCheck %s + +func.func @pack(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>) -> tensor<2x1xf32> { + %0 = "tfl.pack"(%arg0, %arg1) {axis = 0 : i32, values_count = 2 : i32} : (tensor<1xf32>, tensor<1xf32>) -> tensor<2x1xf32> + func.return %0 : tensor<2x1xf32> +} + +// CHECK: func @pack(%[[VAL_0:.*]]: tensor<1xf32>, %[[VAL_1:.*]]: tensor<1xf32>) -> tensor<2x1xf32> { +// CHECK-DAG: %[[VAL_2:.*]] = "tfl.pseudo_const"{{.*}}dense<1> : tensor<4xi32> +// CHECK-DAG: %[[VAL_3:.*]] = "tfl.pseudo_const"{{.*}}dense<2> : tensor<1xi32> +// CHECK-DAG: %[[VAL_4:.*]] = "tfl.pseudo_const"{{.*}}dense<[2, 1]> : tensor<2xi32> +// CHECK: %[[VAL_5:.*]] = "tfl.reshape"(%[[VAL_0]], %[[VAL_2]]) : (tensor<1xf32>, tensor<4xi32>) -> tensor<1x1x1x1xf32> +// CHECK: %[[VAL_6:.*]] = "tfl.reshape"(%[[VAL_1]], %[[VAL_2]]) : (tensor<1xf32>, tensor<4xi32>) -> tensor<1x1x1x1xf32> +// CHECK: %[[VAL_7:.*]] = "tfl.concatenation"(%[[VAL_5]], %[[VAL_6]]) <{axis = 3 : i32, fused_activation_function = "NONE"}> : (tensor<1x1x1x1xf32>, tensor<1x1x1x1xf32>) -> tensor<1x1x1x2xf32> +// CHECK: %[[VAL_8:.*]] = "tfl.reshape"(%[[VAL_7]], %[[VAL_3]]) : (tensor<1x1x1x2xf32>, tensor<1xi32>) -> tensor<2xf32> +// CHECK: %[[VAL_9:.*]] = "tfl.reshape"(%[[VAL_8]], %[[VAL_4]]) : (tensor<2xf32>, tensor<2xi32>) -> tensor<2x1xf32> +// CHECK: return %[[VAL_9]] : tensor<2x1xf32> +// CHECK: } + +// ----- + +// CHECK-LABEL: @avoidPackToConcatenationOnUnknownShapes +func.func @avoidPackToConcatenationOnUnknownShapes(%arg0: tensor, %arg1: tensor) -> tensor { + %0 = "tfl.pack"(%arg0, %arg1) {axis = 0 : i32, values_count = 2 : i32} : (tensor, tensor) -> tensor + func.return %0 : tensor +} + +// CHECK-NOT: "tfl.reshape" +// CHECK: "tfl.pack" + +// ----- + +// CHECK-LABEL: @avoidPackToConcatenationOnUnknownRank +func.func @avoidPackToConcatenationOnUnknownRank(%arg0: tensor<*xf32>, %arg1: tensor<*xf32>) -> tensor<*xf32> { + %0 = "tfl.pack"(%arg0, %arg1) {axis = 0 : i32, values_count = 2 : i32} : (tensor<*xf32>, tensor<*xf32>) -> tensor<*xf32> + func.return %0 : tensor<*xf32> +} + +// CHECK-NOT: "tfl.reshape" +// CHECK: "tfl.pack" + +// ----- + +func.func @squaredDifference(%arg0: tensor<4xf32>, %arg1: tensor<4xf32>) -> tensor<4xf32> { + %0 = "tfl.squared_difference"(%arg0, %arg1) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> + func.return %0 : tensor<4xf32> +} + +// CHECK: func @squaredDifference(%arg0: tensor<4xf32>, %arg1: tensor<4xf32>) -> tensor<4xf32> { +// CHECK: %0 = "tf.Sub"(%arg0, %arg1) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> +// CHECK: %1 = "tf.Mul"(%0, %0) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> +// CHECK: return %1 : tensor<4xf32> +// CHECK: } + +// ----- + +func.func @unrollSplit(%arg0: tensor, %arg1: tensor<1x8x8x1024xf32>) -> (tensor<1x8x8x256xf32>, tensor<1x8x8x256xf32>, tensor<1x8x8x256xf32>) { + %0:4 = "tfl.split"(%arg0, %arg1) {num_splits = 4 : i32, tac.device = "CPU"} : (tensor, tensor<1x8x8x1024xf32>) -> (tensor<1x8x8x256xf32>, tensor<1x8x8x256xf32>, tensor<1x8x8x256xf32>, tensor<1x8x8x256xf32>) + func.return %0#0, %0#1, %0#3 : tensor<1x8x8x256xf32>, tensor<1x8x8x256xf32>, tensor<1x8x8x256xf32> +} + +// CHECK: func @unrollSplit([[VAL_0:%.*]]: tensor, [[VAL_1:%.*]]: tensor<1x8x8x1024xf32>) -> (tensor<1x8x8x256xf32>, tensor<1x8x8x256xf32>, tensor<1x8x8x256xf32>) { +// CHECK-DAG: [[VAL_2:%.*]] = "tfl.pseudo_const"{{.*}}dense<0> : tensor<4xi32> +// CHECK-DAG: [[VAL_3:%.*]] = "tfl.pseudo_const"{{.*}}dense<[0, 0, 0, 256]> : tensor<4xi32> +// CHECK-DAG: [[VAL_4:%.*]] = "tfl.pseudo_const"{{.*}}dense<[0, 0, 0, 768]> : tensor<4xi32> +// CHECK-DAG: [[VAL_5:%.*]] = "tfl.pseudo_const"{{.*}}dense<[-1, -1, -1, 256]> : tensor<4xi32> +// CHECK: [[VAL_6:%.*]] = "tfl.slice"([[VAL_1]], [[VAL_2]], [[VAL_5]]) : (tensor<1x8x8x1024xf32>, tensor<4xi32>, tensor<4xi32>) -> tensor<1x8x8x256xf32> +// CHECK: [[VAL_7:%.*]] = "tfl.slice"([[VAL_1]], [[VAL_3]], [[VAL_5]]) : (tensor<1x8x8x1024xf32>, tensor<4xi32>, tensor<4xi32>) -> tensor<1x8x8x256xf32> +// CHECK: [[VAL_8:%.*]] = "tfl.slice"([[VAL_1]], [[VAL_4]], [[VAL_5]]) : (tensor<1x8x8x1024xf32>, tensor<4xi32>, tensor<4xi32>) -> tensor<1x8x8x256xf32> +// CHECK: return [[VAL_6]], [[VAL_7]], [[VAL_8]] : tensor<1x8x8x256xf32>, tensor<1x8x8x256xf32>, tensor<1x8x8x256xf32> +// CHECK: } + +// ----- + +func.func @unrollSplitUnknownRankResults(%arg0: tensor, %arg1: tensor<1x8x8x1024xf32>) -> (tensor<*xf32>, tensor<*xf32>, tensor<*xf32>) { + %0:4 = "tfl.split"(%arg0, %arg1) {num_splits = 4 : i32, tac.device = "CPU"} : (tensor, tensor<1x8x8x1024xf32>) -> (tensor<*xf32>, tensor<*xf32>, tensor<*xf32>, tensor<*xf32>) + func.return %0#0, %0#1, %0#3 : tensor<*xf32>, tensor<*xf32>, tensor<*xf32> +} + +// CHECK-LABEL: @unrollSplitUnknownRankResults +// CHECK-NOT: "tfl.slice" +// CHECK: "tfl.split" + +// ----- + +func.func @unrollSplitV(%arg0: tensor) -> (tensor, tensor, tensor) { + %0 = "tfl.pseudo_const"() {value = dense<[2, 2, 81]> : tensor<3xi32>} : () -> tensor<3xi32> + %1 = "tfl.pseudo_const"() {value = dense<-1> : tensor} : () -> tensor + %2:3 = "tfl.split_v"(%arg0, %0, %1) {num_splits = 3 : i32} : (tensor, tensor<3xi32>, tensor) -> (tensor, tensor, tensor) + func.return %2#0, %2#1, %2#2 : tensor, tensor, tensor +} + +// CHECK: func @unrollSplitV(%[[VAL_0:.*]]: tensor) -> (tensor, tensor, tensor) { +// CHECK-DAG: %[[VAL_1:.*]] = "tfl.pseudo_const"{{.*}}dense<0> : tensor<4xi32> +// CHECK-DAG: %[[VAL_2:.*]] = "tfl.pseudo_const"{{.*}}dense<[0, 0, 0, 2]> : tensor<4xi32> +// CHECK-DAG: %[[VAL_3:.*]] = "tfl.pseudo_const"{{.*}}dense<[-1, -1, -1, 2]> : tensor<4xi32> +// CHECK-DAG: %[[VAL_4:.*]] = "tfl.pseudo_const"{{.*}}dense<[0, 0, 0, 4]> : tensor<4xi32> +// CHECK-DAG: %[[VAL_5:.*]] = "tfl.pseudo_const"{{.*}}dense<[-1, -1, -1, 81]> : tensor<4xi32> +// CHECK: %[[VAL_6:.*]] = "tfl.slice"(%[[VAL_0]], %[[VAL_1]], %[[VAL_3]]) : (tensor, tensor<4xi32>, tensor<4xi32>) -> tensor +// CHECK: %[[VAL_7:.*]] = "tfl.slice"(%[[VAL_0]], %[[VAL_2]], %[[VAL_3]]) : (tensor, tensor<4xi32>, tensor<4xi32>) -> tensor +// CHECK: %[[VAL_8:.*]] = "tfl.slice"(%[[VAL_0]], %[[VAL_4]], %[[VAL_5]]) : (tensor, tensor<4xi32>, tensor<4xi32>) -> tensor +// CHECK: return %[[VAL_6]], %[[VAL_7]], %[[VAL_8]] : tensor, tensor, tensor +// CHECK: } + +// ----- + +func.func @unrollSplitVUnknownRankResults(%arg0: tensor) -> (tensor<*xf32>, tensor<*xf32>, tensor<*xf32>) { + %0 = "tfl.pseudo_const"() {value = dense<[2, 2, 81]> : tensor<3xi32>} : () -> tensor<3xi32> + %1 = "tfl.pseudo_const"() {value = dense<-1> : tensor} : () -> tensor + %2:3 = "tfl.split_v"(%arg0, %0, %1) {num_splits = 3 : i32} : (tensor, tensor<3xi32>, tensor) -> (tensor<*xf32>, tensor<*xf32>, tensor<*xf32>) + func.return %2#0, %2#1, %2#2 : tensor<*xf32>, tensor<*xf32>, tensor<*xf32> +} + +// CHECK-LABEL: @unrollSplitVUnknownRankResults +// CHECK-NOT: "tfl.slice" +// CHECK: "tfl.split_v" + +// ----- + +func.func @sub(%arg0: tensor<1x384x384x3xf32>, %arg1: tensor<3xf32>) -> tensor<1x384x384x3xf32> { + %0 = "tfl.sub"(%arg0, %arg1) {fused_activation_function = "NONE"} : (tensor<1x384x384x3xf32>, tensor<3xf32>) -> tensor<1x384x384x3xf32> + func.return %0 : tensor<1x384x384x3xf32> +} + +// CHECK: func @sub(%[[VAL_0:.*]]: tensor<1x384x384x3xf32>, %[[VAL_1:.*]]: tensor<3xf32>) -> tensor<1x384x384x3xf32> { +// CHECK: %[[VAL_2:.*]] = arith.constant dense<-1.000000e+00> : tensor +// CHECK: %[[VAL_3:.*]] = tfl.mul(%[[VAL_1]], %[[VAL_2]]) <{fused_activation_function = "NONE"}> : (tensor<3xf32>, tensor) -> tensor<3xf32> +// CHECK: %[[VAL_4:.*]] = tfl.add(%[[VAL_0]], %[[VAL_3]]) <{fused_activation_function = "NONE"}> : (tensor<1x384x384x3xf32>, tensor<3xf32>) -> tensor<1x384x384x3xf32> +// CHECK: return %[[VAL_4]] : tensor<1x384x384x3xf32> +// CHECK: } + +// ----- + +func.func @ensureBiasForConv2d(%arg0: tensor<128x32x32x3xf32>, %arg1: tensor<32x1x1x3xf32>) -> tensor<128x32x32x32xf32> { + %cst = "tfl.no_value"() {value = unit} : () -> none + %0 = "tfl.conv_2d"(%arg0, %arg1, %cst) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32} : (tensor<128x32x32x3xf32>, tensor<32x1x1x3xf32>, none) -> tensor<128x32x32x32xf32> + func.return %0 : tensor<128x32x32x32xf32> +} + +// CHECK: func @ensureBiasForConv2d(%[[VAL_0:.*]]: tensor<128x32x32x3xf32>, %[[VAL_1:.*]]: tensor<32x1x1x3xf32>) -> tensor<128x32x32x32xf32> { +// CHECK: %[[VAL_2:.*]] = "tfl.pseudo_const"{{.*}}dense<0.000000e+00> : tensor<32xf32> +// CHECK: %[[VAL_3:.*]] = "tfl.conv_2d"(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]]) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<128x32x32x3xf32>, tensor<32x1x1x3xf32>, tensor<32xf32>) -> tensor<128x32x32x32xf32> +// CHECK: return %[[VAL_3]] : tensor<128x32x32x32xf32> +// CHECK: } + +// ----- + +func.func @padSliceTo4D(%arg0: tensor<4x384x32xf32>) -> tensor<1x384x32xf32> { + %0 = "tfl.pseudo_const"() {value = dense<0> : tensor<3xi32>} : () -> tensor<3xi32> + %1 = "tfl.pseudo_const"() {value = dense<[1, 384, 32]> : tensor<3xi32>} : () -> tensor<3xi32> + %2 = "tfl.slice"(%arg0, %0, %1) : (tensor<4x384x32xf32>, tensor<3xi32>, tensor<3xi32>) -> tensor<1x384x32xf32> + func.return %2 : tensor<1x384x32xf32> +} + +// CHECK: func @padSliceTo4D(%[[VAL_0:.*]]: tensor<4x384x32xf32>) -> tensor<1x384x32xf32> { +// CHECK-DAG: %[[VAL_1:.*]] = "tf.Const"() <{value = dense<0> : tensor<4xi32>}> : () -> tensor<4xi32> +// CHECK-DAG: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<[1, 1, 384, 32]> : tensor<4xi32>}> : () -> tensor<4xi32> +// CHECK-DAG: %[[VAL_3:.*]] = "tfl.pseudo_const"{{.*}}dense<[1, 4, 384, 32]> : tensor<4xi32> +// CHECK-DAG: %[[VAL_4:.*]] = "tfl.pseudo_const"() <{value = dense<[1, 384, 32]> : tensor<3xi32> +// CHECK: %[[VAL_5:.*]] = "tfl.reshape"(%[[VAL_0]], %[[VAL_3]]) : (tensor<4x384x32xf32>, tensor<4xi32>) -> tensor<1x4x384x32xf32> +// CHECK: %[[VAL_6:.*]] = "tfl.slice"(%[[VAL_5]], %[[VAL_1]], %[[VAL_2]]) : (tensor<1x4x384x32xf32>, tensor<4xi32>, tensor<4xi32>) -> tensor<1x1x384x32xf32> +// CHECK: %[[VAL_7:.*]] = "tfl.reshape"(%[[VAL_6]], %[[VAL_4]]) : (tensor<1x1x384x32xf32>, tensor<3xi32>) -> tensor<1x384x32xf32> +// CHECK: return %[[VAL_7]] : tensor<1x384x32xf32> +// CHECK: } + +// ----- + +// CHECK-LABEL: @avoidPadSliceTo4DOnUnknownOutputShape +func.func @avoidPadSliceTo4DOnUnknownOutputShape(%arg0: tensor<4x384x32xf32>) -> tensor<1x?x?xf32> { + %0 = "tfl.pseudo_const"() {value = dense<0> : tensor<3xi32>} : () -> tensor<3xi32> + %1 = "tfl.pseudo_const"() {value = dense<[1, 384, 32]> : tensor<3xi32>} : () -> tensor<3xi32> + %2 = "tfl.slice"(%arg0, %0, %1) : (tensor<4x384x32xf32>, tensor<3xi32>, tensor<3xi32>) -> tensor<1x?x?xf32> + func.return %2 : tensor<1x?x?xf32> +} + +// CHECK-NOT: "tfl.reshape" +// CHECK: "tfl.slice" + +// ----- + +func.func @fullyConnectedToConv(%arg0: tensor<384x384xf32>, %arg1: tensor<512x384xf32>, %arg2: tensor<512xf32>) -> tensor<384x512xf32> { + %0 = "tfl.fully_connected"(%arg0, %arg1, %arg2) {fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"} : (tensor<384x384xf32>, tensor<512x384xf32>, tensor<512xf32>) -> tensor<384x512xf32> + func.return %0: tensor<384x512xf32> +} + +// CHECK: func @fullyConnectedToConv(%[[VAL_0:.*]]: tensor<384x384xf32>, %[[VAL_1:.*]]: tensor<512x384xf32>, %[[VAL_2:.*]]: tensor<512xf32>) -> tensor<384x512xf32> { +// CHECK-DAG: %[[VAL_3:.*]] = "tfl.pseudo_const"{{.*}}dense<[1, 1, 384, 384]> : tensor<4xi32> +// CHECK-DAG: %[[VAL_4:.*]] = "tfl.pseudo_const"{{.*}}dense<[512, 1, 1, 384]> : tensor<4xi32> +// CHECK-DAG: %[[VAL_5:.*]] = "tfl.pseudo_const"{{.*}}dense<[384, 512]> : tensor<2xi32> +// CHECK: %[[VAL_6:.*]] = "tfl.reshape"(%[[VAL_0]], %[[VAL_3]]) : (tensor<384x384xf32>, tensor<4xi32>) -> tensor<1x1x384x384xf32> +// CHECK: %[[VAL_7:.*]] = "tfl.reshape"(%[[VAL_1]], %[[VAL_4]]) : (tensor<512x384xf32>, tensor<4xi32>) -> tensor<512x1x1x384xf32> +// CHECK: %[[VAL_8:.*]] = "tfl.conv_2d"(%[[VAL_6]], %[[VAL_7]], %[[VAL_2]]) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x1x384x384xf32>, tensor<512x1x1x384xf32>, tensor<512xf32>) -> tensor<1x1x384x512xf32> +// CHECK: %[[VAL_9:.*]] = "tfl.reshape"(%[[VAL_8]], %[[VAL_5]]) : (tensor<1x1x384x512xf32>, tensor<2xi32>) -> tensor<384x512xf32> +// CHECK: return %[[VAL_9]] : tensor<384x512xf32> +// CHECK: } + +// ----- + +func.func @padConcatTo4D(%arg0: tensor<384x384xf32>, %arg1: tensor<384x384xf32>, %arg2: tensor<384x384xf32>, %arg3: tensor<384x384xf32>) -> tensor<1536x384xf32> { + %0 = "tfl.concatenation"(%arg0, %arg1, %arg2, %arg3) {axis = 0 : i32, fused_activation_function = "NONE"} : (tensor<384x384xf32>, tensor<384x384xf32>, tensor<384x384xf32>, tensor<384x384xf32>) -> tensor<1536x384xf32> + func.return %0: tensor<1536x384xf32> +} + +// CHECK: func @padConcatTo4D(%[[VAL_0:.*]]: tensor<384x384xf32>, %[[VAL_1:.*]]: tensor<384x384xf32>, %[[VAL_2:.*]]: tensor<384x384xf32>, %[[VAL_3:.*]]: tensor<384x384xf32>) -> tensor<1536x384xf32> { +// CHECK-DAG: %[[VAL_4:.*]] = "tfl.pseudo_const"{{.*}}dense<[1, 1, 384, 384]> : tensor<4xi32> +// CHECK-DAG: %[[VAL_5:.*]] = "tfl.pseudo_const"{{.*}}dense<[1536, 384]> : tensor<2xi32> +// CHECK: %[[VAL_6:.*]] = "tfl.reshape"(%[[VAL_0]], %[[VAL_4]]) : (tensor<384x384xf32>, tensor<4xi32>) -> tensor<1x1x384x384xf32> +// CHECK: %[[VAL_7:.*]] = "tfl.reshape"(%[[VAL_1]], %[[VAL_4]]) : (tensor<384x384xf32>, tensor<4xi32>) -> tensor<1x1x384x384xf32> +// CHECK: %[[VAL_8:.*]] = "tfl.reshape"(%[[VAL_2]], %[[VAL_4]]) : (tensor<384x384xf32>, tensor<4xi32>) -> tensor<1x1x384x384xf32> +// CHECK: %[[VAL_9:.*]] = "tfl.reshape"(%[[VAL_3]], %[[VAL_4]]) : (tensor<384x384xf32>, tensor<4xi32>) -> tensor<1x1x384x384xf32> +// CHECK: %[[VAL_10:.*]] = "tfl.concatenation"(%[[VAL_6]], %[[VAL_7]], %[[VAL_8]], %[[VAL_9]]) <{axis = 2 : i32, fused_activation_function = "NONE"}> : (tensor<1x1x384x384xf32>, tensor<1x1x384x384xf32>, tensor<1x1x384x384xf32>, tensor<1x1x384x384xf32>) -> tensor<1x1x1536x384xf32> +// CHECK: %[[VAL_11:.*]] = "tfl.reshape"(%[[VAL_10]], %[[VAL_5]]) : (tensor<1x1x1536x384xf32>, tensor<2xi32>) -> tensor<1536x384xf32> +// CHECK: return %[[VAL_11]] : tensor<1536x384xf32> +// CHECK: } + +// ----- + +// CHECK-LABEL: @avoidPadConcatTo4DOnUnknownOutputShape +func.func @avoidPadConcatTo4DOnUnknownOutputShape(%arg0: tensor<384x384xf32>, %arg1: tensor<384x384xf32>, %arg2: tensor<384x384xf32>, %arg3: tensor<384x384xf32>) -> tensor { + %0 = "tfl.concatenation"(%arg0, %arg1, %arg2, %arg3) {axis = 0 : i32, fused_activation_function = "NONE"} : (tensor<384x384xf32>, tensor<384x384xf32>, tensor<384x384xf32>, tensor<384x384xf32>) -> tensor + func.return %0: tensor +} + +// CHECK-NOT: "tfl.reshape" +// CHECK: "tfl.concatenation" diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/device-transform-nnapi.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/device-transform-nnapi.mlir new file mode 100644 index 0000000000000000000000000000000000000000..59c6ce1fb040680c091432d93a35797e4fe24bbe --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/device-transform-nnapi.mlir @@ -0,0 +1,58 @@ +// RUN: tac-opt-all-backends -tfl-device-transform-nnapi %s -split-input-file -verify-diagnostics | FileCheck %s + +func.func @mean_4d_keepdim(%arg0: tensor<1x48x48x512xf32>) -> tensor<1x1x1x512xf32> { + %cst = arith.constant dense<[1, 2]> : tensor<2xi32> + %0 = "tfl.mean"(%arg0, %cst) {keep_dims = true} : (tensor<1x48x48x512xf32>, tensor<2xi32>) -> tensor<1x1x1x512xf32> + func.return %0 : tensor<1x1x1x512xf32> +} + +// CHECK: func @mean_4d_keepdim([[VAL_0:%.*]]: tensor<1x48x48x512xf32>) -> tensor<1x1x1x512xf32> { +// CHECK: [[VAL_1:%.*]] = "tfl.average_pool_2d"([[VAL_0]]) <{filter_height = 48 : i32, filter_width = 48 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x48x48x512xf32>) -> tensor<1x1x1x512xf32> +// CHECK: return [[VAL_1]] : tensor<1x1x1x512xf32> +// CHECK: } + +// ----- + +func.func @mean_4d_no_keepdim(%arg0: tensor<1x48x48x512xf32>) -> tensor<1x512xf32> { + %cst = arith.constant dense<[1, 2]> : tensor<2xi32> + %0 = "tfl.mean"(%arg0, %cst) {keep_dims = false} : (tensor<1x48x48x512xf32>, tensor<2xi32>) -> tensor<1x512xf32> + func.return %0 : tensor<1x512xf32> +} + +// CHECK: func @mean_4d_no_keepdim([[VAL_0:%.*]]: tensor<1x48x48x512xf32>) -> tensor<1x512xf32> { +// CHECK: [[VAL_1:%.*]] = "tfl.pseudo_const"(){{.*}}dense<[1, 512]> : tensor<2xi32> +// CHECK: [[VAL_2:%.*]] = "tfl.average_pool_2d"([[VAL_0]]) <{filter_height = 48 : i32, filter_width = 48 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x48x48x512xf32>) -> tensor<1x1x1x512xf32> +// CHECK: [[VAL_3:%.*]] = "tfl.reshape"([[VAL_2]], [[VAL_1]]) : (tensor<1x1x1x512xf32>, tensor<2xi32>) -> tensor<1x512xf32> +// CHECK: return [[VAL_3]] : tensor<1x512xf32> +// CHECK: } + +// ----- + +func.func @mean_quant_same_scale(%arg0: tensor>) -> tensor> { + %0 = "tfl.pseudo_const"() {value = dense<[1, 2]> : tensor<2xi32>} : () -> tensor<2xi32> + %1 = "tfl.mean"(%arg0, %0) {keep_dims = false} : (tensor>, tensor<2xi32>) -> tensor> + func.return %1 : tensor> +} + +// CHECK: func @mean_quant_same_scale(%[[VAL_0:.*]]: tensor>) -> tensor> { +// CHECK: %[[VAL_1:.*]] = "tfl.pseudo_const"(){{.*}}dense<[-1, 2048]> : tensor<2xi32> +// CHECK: %[[VAL_2:.*]] = "tfl.average_pool_2d"(%[[VAL_0]]) <{filter_height = 7 : i32, filter_width = 7 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor>) -> tensor> +// CHECK: %[[VAL_3:.*]] = "tfl.reshape"(%[[VAL_2]], %[[VAL_1]]) : (tensor>, tensor<2xi32>) -> tensor> +// CHECK: return %[[VAL_3]] : tensor> +// CHECK: } + +// ----- + +func.func @mean_quant_different_scales(%arg0: tensor>) -> tensor> { + %0 = "tfl.pseudo_const"() {value = dense<[1, 2]> : tensor<2xi32>} : () -> tensor<2xi32> + %1 = "tfl.mean"(%arg0, %0) {keep_dims = false} : (tensor>, tensor<2xi32>) -> tensor> + func.return %1 : tensor> +} + +// CHECK: func @mean_quant_different_scales(%[[VAL_0:.*]]: tensor>) -> tensor> { +// CHECK: %[[VAL_1:.*]] = "tfl.pseudo_const"(){{.*}}dense<[-1, 2048]> : tensor<2xi32> +// CHECK: %[[VAL_2:.*]] = "tfl.average_pool_2d"(%[[VAL_0]]) <{filter_height = 7 : i32, filter_width = 7 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor>) -> tensor> +// CHECK: %[[VAL_3:.*]] = "tfl.reshape"(%[[VAL_2]], %[[VAL_1]]) : (tensor>, tensor<2xi32>) -> tensor> +// CHECK: %[[VAL_4:.*]] = "tfl.quantize"(%[[VAL_3]]) <{qtype = tensor>}> : (tensor>) -> tensor> +// CHECK: return %[[VAL_4]] : tensor> +// CHECK: } diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/e2e/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/e2e/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..08aedf9b67121f48ed9fa5eb757db74fa6488510 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/e2e/BUILD @@ -0,0 +1,30 @@ +load("//tensorflow:tensorflow.default.bzl", "filegroup") +load("//tensorflow/compiler/mlir:glob_lit_test.bzl", "glob_lit_tests") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + default_visibility = [ + "//visibility:public", + ], + licenses = ["notice"], +) + +glob_lit_tests( + name = "all_tests", + data = [":test_utilities"], + driver = "@llvm-project//mlir:run_lit.sh", + test_file_exts = [ + "mlir", + "cc", + ], +) + +# Bundle together all of the test utilities that are used by tests. +filegroup( + name = "test_utilities", + testonly = True, + data = [ + "//tensorflow/compiler/mlir/lite/experimental/tac:tac-translate", + "@llvm-project//llvm:FileCheck", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/e2e/device-transform-nnapi.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/e2e/device-transform-nnapi.mlir new file mode 100644 index 0000000000000000000000000000000000000000..8ac9904ae6ccdc7477f6a7fc5de8954fceb43469 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/e2e/device-transform-nnapi.mlir @@ -0,0 +1,21 @@ +// RUN: tac-translate -input-mlir -output-mlir -device-specs=NNAPI %s -o - 2>&1 | FileCheck %s + +module { + // CHECK-LABEL: main + func.func @main(%arg0: tensor<4xf32>, %arg1: tensor<4xf32>) -> tensor<4xf32> { + %0 = "tfl.squared_difference"(%arg0, %arg1) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> + func.return %0 : tensor<4xf32> + // CHECK: [[VAL_0:%.*]] = tfl.sub %arg0, %arg1 {fused_activation_function = "NONE"} : tensor<4xf32> + // CHECK: [[VAL_1:%.*]] = tfl.mul [[VAL_0]], [[VAL_0]] {fused_activation_function = "NONE"} : tensor<4xf32 + } + + // CHECK-LABEL: pack + func.func @pack(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>) -> tensor<2x1xf32> { + %0 = "tfl.pack"(%arg0, %arg1) {axis = 0 : i32, values_count = 2 : i32} : (tensor<1xf32>, tensor<1xf32>) -> tensor<2x1xf32> + func.return %0 : tensor<2x1xf32> + // CHECK: %[[VAL_0:.*]] = arith.constant dense<[2, 1]> : tensor<2xi32> + // CHECK: %[[CONCAT:.*]] = "tfl.concatenation"(%arg0, %arg1) <{axis = 0 : i32, fused_activation_function = "NONE"}> : (tensor<1xf32>, tensor<1xf32>) -> tensor<2xf32> + // CHECK: %[[VAL_1:.*]] = "tfl.reshape"(%[[CONCAT]], %[[VAL_0]]) : (tensor<2xf32>, tensor<2xi32>) -> tensor<2x1xf32> + // CHECK: return %[[VAL_1]] + } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/e2e/simple-graph.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/e2e/simple-graph.mlir new file mode 100644 index 0000000000000000000000000000000000000000..6d16ee54b9292633eef5509d72dfd546f573eaa2 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/e2e/simple-graph.mlir @@ -0,0 +1,18 @@ +// RUN: tac-translate -input-mlir -output-mlir -device-specs=GPU %s -o - 2>&1 | FileCheck %s + +module { +func.func @main(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>, %arg2: tensor<1xf32>, %arg3: tensor<1xf32>) -> tensor<2x1xf32> attributes {tf.entry_function = {inputs = "input0,input1,input2,input3", outputs = "output"}} { + %0 = "tfl.add"(%arg0, %arg1) {fused_activation_function = "RELU6"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + %1 = "tfl.mul"(%0, %arg2) {fused_activation_function = "RELU6"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + %2 = "tfl.add"(%arg0, %arg3) {fused_activation_function = "RELU6"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + %3 = "tfl.pack"(%1, %2) {axis = 0 : i32, values_count = 2 : i32} : (tensor<1xf32>, tensor<1xf32>) -> tensor<2x1xf32> + func.return %3 : tensor<2x1xf32> +} + +// CHECK: %[[CST:.*]] = arith.constant dense<1> : tensor<4xi32> +// CHECK: [[VAL_0:%.*]] = "tfl.reshape"(%1, %[[CST]]) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<1xf32>, tensor<4xi32>) -> tensor<1x1x1x1xf32> +// CHECK: [[VAL_1:%.*]] = "tfl.reshape"(%2, %[[CST]]) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<1xf32>, tensor<4xi32>) -> tensor<1x1x1x1xf32> +// CHECK: [[VAL_2:%.*]] = "tfl.concatenation"([[VAL_0]], [[VAL_1]]) <{axis = 3 : i32, fused_activation_function = "NONE"}> {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<1x1x1x1xf32>, tensor<1x1x1x1xf32>) -> tensor<1x1x1x2xf32> +// CHECK: [[VAL_3:%.*]] = "tfl.reshape"([[VAL_2]], %{{.*}}) : (tensor<1x1x1x2xf32>, tensor<2xi32>) -> tensor<2x1xf32> + +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/fold-constants-to-subgraph.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/fold-constants-to-subgraph.mlir new file mode 100644 index 0000000000000000000000000000000000000000..ad8c010c1fd121224144d0276117d4f8da3f6fac --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/fold-constants-to-subgraph.mlir @@ -0,0 +1,190 @@ +// Test for partial folding: only fold i32 constants. +// RUN: tac-opt-all-backends -tfl-fold-constants-to-subgraph='fold-all-constants=false' %s -split-input-file -verify-diagnostics | FileCheck --check-prefix=PARTIAL %s + +// Test for fold all constants. +// RUN: tac-opt-all-backends -tfl-fold-constants-to-subgraph='fold-all-constants=true' %s -split-input-file -verify-diagnostics | FileCheck --check-prefix=ALL %s + +module { + +func.func @main(%arg0: tensor<4x384x32xf32>) -> tensor<1x384x32xf32> { + %0 = "tfl.pseudo_const"() {value = dense<0> : tensor<3xi32>} : () -> tensor<3xi32> + %1 = "tfl.pseudo_const"() {value = dense<[1, 384, 32]> : tensor<3xi32>} : () -> tensor<3xi32> + %2 = func.call @simple_test(%arg0, %0, %1) {tac.interface_name = "func1"} : (tensor<4x384x32xf32>, tensor<3xi32>, tensor<3xi32>) -> tensor<1x384x32xf32> + func.return %2 : tensor<1x384x32xf32> +} + +// PARTIAL-LABEL: @simple_test +func.func @simple_test(%arg0: tensor<4x384x32xf32>, %arg1: tensor<3xi32>, %arg2: tensor<3xi32>) -> tensor<1x384x32xf32> attributes {tac.interface_name = "func1"} { + %0 = "tfl.slice"(%arg0, %arg1, %arg2) : (tensor<4x384x32xf32>, tensor<3xi32>, tensor<3xi32>) -> tensor<1x384x32xf32> + func.return %0 : tensor<1x384x32xf32> +} + +// PARTIAL: func @simple_test(%[[VAL_0:.*]]: tensor<4x384x32xf32>, %[[VAL_1:.*]]: tensor<3xi32>, %[[VAL_2:.*]]: tensor<3xi32>) -> tensor<1x384x32xf32> attributes {tac.interface_name = "func1"} { +// PARTIAL: %[[VAL_3:.*]] = "tfl.pseudo_const"() <{value = dense<[1, 384, 32]> : tensor<3xi32>}> : () -> tensor<3xi32> +// PARTIAL: %[[VAL_4:.*]] = "tfl.pseudo_const"() <{value = dense<0> : tensor<3xi32>}> : () -> tensor<3xi32> +// PARTIAL: %[[VAL_5:.*]] = "tfl.slice"(%[[VAL_0]], %[[VAL_4]], %[[VAL_3]]) : (tensor<4x384x32xf32>, tensor<3xi32>, tensor<3xi32>) -> tensor<1x384x32xf32> +// PARTIAL: return %[[VAL_5]] : tensor<1x384x32xf32> +// PARTIAL: } +} + +// ----- + +module { + +func.func @main(%arg0: tensor<4x384x32xf32>) -> (tensor<1x384x32xf32>, tensor<1x384x32xf32>) { + %0 = "tfl.pseudo_const"() {value = dense<0> : tensor<3xi32>} : () -> tensor<3xi32> + %1 = "tfl.pseudo_const"() {value = dense<[1, 384, 32]> : tensor<3xi32>} : () -> tensor<3xi32> + %2 = func.call @arg_reuse_test_1(%arg0, %0, %1) {tac.interface_name = "func1"} : (tensor<4x384x32xf32>, tensor<3xi32>, tensor<3xi32>) -> tensor<1x384x32xf32> + %3 = func.call @arg_reuse_test_2(%arg0, %0, %1) {tac.interface_name = "func2"} : (tensor<4x384x32xf32>, tensor<3xi32>, tensor<3xi32>) -> tensor<1x384x32xf32> + func.return %2, %3 : tensor<1x384x32xf32>, tensor<1x384x32xf32> +} + +// PARTIAL-LABEL: @arg_reuse_test_1 +func.func @arg_reuse_test_1(%arg0: tensor<4x384x32xf32>, %arg1: tensor<3xi32>, %arg2: tensor<3xi32>) -> tensor<1x384x32xf32> attributes {tac.interface_name = "func1"} { + %0 = "tfl.slice"(%arg0, %arg1, %arg2) : (tensor<4x384x32xf32>, tensor<3xi32>, tensor<3xi32>) -> tensor<1x384x32xf32> + func.return %0 : tensor<1x384x32xf32> +} + +// PARTIAL-LABEL: @arg_reuse_test_2 +func.func @arg_reuse_test_2(%arg0: tensor<4x384x32xf32>, %arg1: tensor<3xi32>, %arg2: tensor<3xi32>) -> tensor<1x384x32xf32> attributes {tac.interface_name = "func2"} { + %0 = "tfl.slice"(%arg0, %arg1, %arg2) : (tensor<4x384x32xf32>, tensor<3xi32>, tensor<3xi32>) -> tensor<1x384x32xf32> + func.return %0 : tensor<1x384x32xf32> +} + +// PARTIAL: func @arg_reuse_test_1(%[[VAL_0:.*]]: tensor<4x384x32xf32>, %[[VAL_1:.*]]: tensor<3xi32>, %[[VAL_2:.*]]: tensor<3xi32>) -> tensor<1x384x32xf32> attributes {tac.interface_name = "func1"} { +// PARTIAL: %[[VAL_3:.*]] = "tfl.pseudo_const"() <{value = dense<[1, 384, 32]> : tensor<3xi32>}> : () -> tensor<3xi32> +// PARTIAL: %[[VAL_4:.*]] = "tfl.pseudo_const"() <{value = dense<0> : tensor<3xi32>}> : () -> tensor<3xi32> +// PARTIAL: %[[VAL_5:.*]] = "tfl.slice"(%[[VAL_0]], %[[VAL_4]], %[[VAL_3]]) : (tensor<4x384x32xf32>, tensor<3xi32>, tensor<3xi32>) -> tensor<1x384x32xf32> +// PARTIAL: return %[[VAL_5]] : tensor<1x384x32xf32> +// PARTIAL: } + +// PARTIAL: func @arg_reuse_test_2(%[[VAL_6:.*]]: tensor<4x384x32xf32>, %[[VAL_7:.*]]: tensor<3xi32>, %[[VAL_8:.*]]: tensor<3xi32>) -> tensor<1x384x32xf32> attributes {tac.interface_name = "func2"} { +// PARTIAL: %[[VAL_9:.*]] = "tfl.pseudo_const"() <{value = dense<[1, 384, 32]> : tensor<3xi32>}> : () -> tensor<3xi32> +// PARTIAL: %[[VAL_10:.*]] = "tfl.pseudo_const"() <{value = dense<0> : tensor<3xi32>}> : () -> tensor<3xi32> +// PARTIAL: %[[VAL_11:.*]] = "tfl.slice"(%[[VAL_6]], %[[VAL_10]], %[[VAL_9]]) : (tensor<4x384x32xf32>, tensor<3xi32>, tensor<3xi32>) -> tensor<1x384x32xf32> +// PARTIAL: return %[[VAL_11]] : tensor<1x384x32xf32> +// PARTIAL: } + +} + +// ----- + +module { +func.func @main(%arg0: tensor<384x512x!quant.uniform>) -> tensor<384x128x!quant.uniform> { + %0 = "tfl.pseudo_qconst"() {qtype = tensor<128x512x!quant.uniform:f32, 0.01>>, value = dense<0> : tensor<128x512xi8>} : () -> tensor<128x512x!quant.uniform:f32, 0.01>> + %1 = "tfl.pseudo_qconst"() {qtype = tensor<128x!quant.uniform>, value = dense<0> : tensor<128xi32>} : () -> tensor<128x!quant.uniform> + %2 = func.call @quantization_test(%arg0, %0, %1) {tac.interface_name = "func1"} : (tensor<384x512x!quant.uniform>, tensor<128x512x!quant.uniform:f32, 0.01>>, tensor<128x!quant.uniform>) -> tensor<384x128x!quant.uniform> + func.return %2 : tensor<384x128x!quant.uniform> +} + +// PARTIAL-LABEL: @quantization_test +func.func @quantization_test(%arg0: tensor<384x512x!quant.uniform>, %arg1: tensor<128x512x!quant.uniform:f32, 0.01>>, %arg2: tensor<128x!quant.uniform>) -> tensor<384x128x!quant.uniform> { + %0 = "tfl.fully_connected"(%arg0, %arg1, %arg2) {fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"} : (tensor<384x512x!quant.uniform>, tensor<128x512x!quant.uniform:f32, 0.01>>, tensor<128x!quant.uniform>) -> tensor<384x128x!quant.uniform> + func.return %0 : tensor<384x128x!quant.uniform> +} + +// PARTIAL: func @quantization_test(%[[VAL_0:.*]]: tensor<384x512x!quant.uniform>, %[[VAL_1:.*]]: tensor<128x512x!quant.uniform:f32, 1.000000e-02>>, %[[VAL_2:.*]]: tensor<128x!quant.uniform>) -> tensor<384x128x!quant.uniform> { +// PARTIAL: %[[VAL_3:.*]] = "tfl.pseudo_qconst"() <{qtype = tensor<128x!quant.uniform>, value = dense<0> : tensor<128xi32>}> : () -> tensor<128x!quant.uniform> +// PARTIAL: %[[VAL_4:.*]] = "tfl.fully_connected"(%[[VAL_0]], %[[VAL_1]], %[[VAL_3]]) <{fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"}> : (tensor<384x512x!quant.uniform>, tensor<128x512x!quant.uniform:f32, 1.000000e-02>>, tensor<128x!quant.uniform>) -> tensor<384x128x!quant.uniform> +// PARTIAL: return %[[VAL_4]] : tensor<384x128x!quant.uniform> +// PARTIAL: } + +} + +// ----- + +module { +func.func @main(%arg0: tensor<256x32x32x3xf32>) -> tensor<256x30x30x16xf32> { + %0 = "tfl.pseudo_const"() {value = dense<1.000000e+00> : tensor<16x3x3x3xf32>} : () -> tensor<16x3x3x3xf32> + %1 = "tfl.pseudo_const"() {value = dense<1.000000e+00> : tensor<16xf32>} : () -> tensor<16xf32> + %2 = func.call @fold_all_test(%arg0, %0, %1) : (tensor<256x32x32x3xf32>, tensor<16x3x3x3xf32>, tensor<16xf32>) -> tensor<256x30x30x16xf32> + func.return %2 : tensor<256x30x30x16xf32> +} + +// ALL-LABEL: @fold_all_test +func.func @fold_all_test(%arg0: tensor<256x32x32x3xf32>, %arg1: tensor<16x3x3x3xf32>, %arg2: tensor<16xf32>) -> tensor<256x30x30x16xf32> { + %0 = "tfl.conv_2d"(%arg0, %arg1, %arg2) {tac.device = "GPU", tac.inference_type = "FLOAT", dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32} : (tensor<256x32x32x3xf32>, tensor<16x3x3x3xf32>, tensor<16xf32>) -> tensor<256x30x30x16xf32> + func.return %0 : tensor<256x30x30x16xf32> +} + +// ALL: func @fold_all_test(%[[VAL_0:.*]]: tensor<256x32x32x3xf32>, %[[VAL_1:.*]]: tensor<16x3x3x3xf32>, %[[VAL_2:.*]]: tensor<16xf32>) -> tensor<256x30x30x16xf32> { +// ALL: %[[VAL_3:.*]] = "tfl.pseudo_const"() <{value = dense<1.000000e+00> : tensor<16xf32>}> : () -> tensor<16xf32> +// ALL: %[[VAL_4:.*]] = "tfl.pseudo_const"() <{value = dense<1.000000e+00> : tensor<16x3x3x3xf32>}> : () -> tensor<16x3x3x3xf32> +// ALL: %[[VAL_5:.*]] = "tfl.conv_2d"(%[[VAL_0]], %[[VAL_4]], %[[VAL_3]]) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32}> {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<256x32x32x3xf32>, tensor<16x3x3x3xf32>, tensor<16xf32>) -> tensor<256x30x30x16xf32> +// ALL: return %[[VAL_5]] : tensor<256x30x30x16xf32> +// ALL: } +} + +// ----- + +module { +func.func @main(%arg0: tensor<256x32x32x3xf32>) -> tensor<256x30x30x16xf32> { + %0 = stablehlo.constant dense<1.000000e+00> : tensor<16x3x3x3xf32> + %1 = stablehlo.constant dense<1.000000e+00> : tensor<16xf32> + %2 = func.call @fold_stablehlo_constant_test(%arg0, %0, %1) : (tensor<256x32x32x3xf32>, tensor<16x3x3x3xf32>, tensor<16xf32>) -> tensor<256x30x30x16xf32> + func.return %2 : tensor<256x30x30x16xf32> +} + +func.func @fold_stablehlo_constant_test(%arg0: tensor<256x32x32x3xf32>, %arg1: tensor<16x3x3x3xf32>, %arg2: tensor<16xf32>) -> tensor<256x30x30x16xf32> { + %0 = "tfl.conv_2d"(%arg0, %arg1, %arg2) {tac.device = "GPU", tac.inference_type = "FLOAT", dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32} : (tensor<256x32x32x3xf32>, tensor<16x3x3x3xf32>, tensor<16xf32>) -> tensor<256x30x30x16xf32> + func.return %0 : tensor<256x30x30x16xf32> +} + +// ALL-LABEL: func @fold_stablehlo_constant_test +// ALL-SAME: (%[[VAL_0:.*]]: tensor<256x32x32x3xf32>, %[[VAL_1:.*]]: tensor<16x3x3x3xf32>, %[[VAL_2:.*]]: tensor<16xf32>) -> tensor<256x30x30x16xf32> { +// ALL-DAG: %[[VAL_3:.*]] = stablehlo.constant dense<1.000000e+00> : tensor<16xf32> +// ALL-DAG: %[[VAL_4:.*]] = stablehlo.constant dense<1.000000e+00> : tensor<16x3x3x3xf32> +// ALL: %[[VAL_5:.*]] = "tfl.conv_2d"(%[[VAL_0]], %[[VAL_4]], %[[VAL_3]]) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32}> {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<256x32x32x3xf32>, tensor<16x3x3x3xf32>, tensor<16xf32>) -> tensor<256x30x30x16xf32> +// ALL: return %[[VAL_5]] : tensor<256x30x30x16xf32> +// ALL: } +} + +// ----- + +module { + +func.func @main(%arg0: tensor<4x384x32xf32>) -> tensor<1x384x32xf32> { + %0 = arith.constant dense<0> : tensor<3xi32> + %1 = arith.constant dense<[1, 384, 32]> : tensor<3xi32> + %2 = func.call @simple_test(%arg0, %0, %1) {tac.interface_name = "func1"} : (tensor<4x384x32xf32>, tensor<3xi32>, tensor<3xi32>) -> tensor<1x384x32xf32> + func.return %2 : tensor<1x384x32xf32> +} + +// PARTIAL-LABEL: @simple_test +func.func @simple_test(%arg0: tensor<4x384x32xf32>, %arg1: tensor<3xi32>, %arg2: tensor<3xi32>) -> tensor<1x384x32xf32> attributes {tac.interface_name = "func1"} { + %0 = "tfl.slice"(%arg0, %arg1, %arg2) : (tensor<4x384x32xf32>, tensor<3xi32>, tensor<3xi32>) -> tensor<1x384x32xf32> + func.return %0 : tensor<1x384x32xf32> +} + +// PARTIAL: func @simple_test(%[[VAL_0:.*]]: tensor<4x384x32xf32>, %[[VAL_1:.*]]: tensor<3xi32>, %[[VAL_2:.*]]: tensor<3xi32>) -> tensor<1x384x32xf32> attributes {tac.interface_name = "func1"} { +// PARTIAL: %[[VAL_3:.*]] = arith.constant dense<[1, 384, 32]> : tensor<3xi32> +// PARTIAL: %[[VAL_4:.*]] = arith.constant dense<0> : tensor<3xi32> +// PARTIAL: %[[VAL_5:.*]] = "tfl.slice"(%[[VAL_0]], %[[VAL_4]], %[[VAL_3]]) : (tensor<4x384x32xf32>, tensor<3xi32>, tensor<3xi32>) -> tensor<1x384x32xf32> +// PARTIAL: return %[[VAL_5]] : tensor<1x384x32xf32> +// PARTIAL: } +} + +// ----- + +module { + +func.func @main(%arg0: tensor<4x384x32xf32>) -> tensor<1x384x32xf32> { + %0 = stablehlo.constant dense<0> : tensor<3xi32> + %1 = stablehlo.constant dense<[1, 384, 32]> : tensor<3xi32> + %2 = func.call @partial_stablehlo_constant_test(%arg0, %0, %1) {tac.interface_name = "func1"} : (tensor<4x384x32xf32>, tensor<3xi32>, tensor<3xi32>) -> tensor<1x384x32xf32> + func.return %2 : tensor<1x384x32xf32> +} + +func.func @partial_stablehlo_constant_test(%arg0: tensor<4x384x32xf32>, %arg1: tensor<3xi32>, %arg2: tensor<3xi32>) -> tensor<1x384x32xf32> attributes {tac.interface_name = "func1"} { + %0 = "tfl.slice"(%arg0, %arg1, %arg2) : (tensor<4x384x32xf32>, tensor<3xi32>, tensor<3xi32>) -> tensor<1x384x32xf32> + func.return %0 : tensor<1x384x32xf32> +} + +// PARTIAL-LABEL: func @partial_stablehlo_constant_test +// PARTIAL-SAME: (%[[VAL_0:.*]]: tensor<4x384x32xf32>, %[[VAL_1:.*]]: tensor<3xi32>, %[[VAL_2:.*]]: tensor<3xi32>) -> tensor<1x384x32xf32> attributes {tac.interface_name = "func1"} { +// PARTIAL-DAG: %[[VAL_3:.*]] = stablehlo.constant dense<[1, 384, 32]> : tensor<3xi32> +// PARTIAL-DAG: %[[VAL_4:.*]] = stablehlo.constant dense<0> : tensor<3xi32> +// PARTIAL: %[[VAL_5:.*]] = "tfl.slice"(%[[VAL_0]], %[[VAL_4]], %[[VAL_3]]) : (tensor<4x384x32xf32>, tensor<3xi32>, tensor<3xi32>) -> tensor<1x384x32xf32> +// PARTIAL: return %[[VAL_5]] : tensor<1x384x32xf32> +// PARTIAL: } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/get-alternative-subgraph.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/get-alternative-subgraph.mlir new file mode 100644 index 0000000000000000000000000000000000000000..5e9aa07cb4fda9545d64102a9d8b88d6b567164b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/get-alternative-subgraph.mlir @@ -0,0 +1,168 @@ +// RUN: tac-opt-all-backends -tfl-get-alternative-subgraph='device-specs=GPU' %s -split-input-file -verify-diagnostics | FileCheck %s + +module { + func.func @simpleTest(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>, %arg2: tensor<1xf32>, %arg3: tensor<1xf32>) -> tensor<2x1xf32> { + %0 = func.call @func_0_GPU_FLOAT(%arg0, %arg1, %arg2) {tac.interface_name = "func_0"} : (tensor<1xf32>, tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + %1 = func.call @func_1_GPU_FLOAT(%arg0, %arg3) {tac.interface_name = "func_1"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + %2 = func.call @func_2_CPU_FLOAT(%0, %1) {tac.interface_name = "func_2"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<2x1xf32> + func.return %2 : tensor<2x1xf32> + } + + func.func private @func_2_CPU_FLOAT(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>) -> tensor<2x1xf32> attributes {tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "func_2"} { + %0 = "tfl.pack"(%arg0, %arg1) {axis = 0 : i32, tac.device = "CPU", tac.inference_type = "FLOAT", values_count = 2 : i32} : (tensor<1xf32>, tensor<1xf32>) -> tensor<2x1xf32> + func.return %0 : tensor<2x1xf32> + } + + func.func private @func_0_GPU_FLOAT(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>, %arg2: tensor<1xf32>) -> tensor<1xf32> attributes {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} { + %0 = tfl.add %arg0, %arg1 {fused_activation_function = "RELU6", tac.device = "GPU", tac.inference_type = "FLOAT"} : tensor<1xf32> + %1 = tfl.mul %0, %arg2 {fused_activation_function = "RELU6", tac.device = "GPU", tac.inference_type = "FLOAT"} : tensor<1xf32> + func.return %1 : tensor<1xf32> + } + + func.func private @func_1_GPU_FLOAT(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>) -> tensor<1xf32> attributes {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_1"} { + %0 = tfl.add %arg0, %arg1 {fused_activation_function = "RELU6", tac.device = "GPU", tac.inference_type = "FLOAT"} : tensor<1xf32> + func.return %0 : tensor<1xf32> + } + +// CHECK: func @simpleTest(%[[VAL_0:.*]]: tensor<1xf32>, %[[VAL_1:.*]]: tensor<1xf32>, %[[VAL_2:.*]]: tensor<1xf32>, %[[VAL_3:.*]]: tensor<1xf32>) -> tensor<2x1xf32> { +// CHECK: %[[VAL_4:.*]] = call @func_0_GPU_FLOAT(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]]) {tac.interface_name = "func_0"} : (tensor<1xf32>, tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> +// CHECK: %[[VAL_5:.*]] = call @func_1_GPU_FLOAT(%[[VAL_0]], %[[VAL_3]]) {tac.interface_name = "func_1"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> +// CHECK: %[[VAL_6:.*]] = call @func_2_CPU_FLOAT(%[[VAL_4]], %[[VAL_5]]) {tac.interface_name = "func_2"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<2x1xf32> +// CHECK: return %[[VAL_6]] : tensor<2x1xf32> +// CHECK: } + +// CHECK: func private @func_2_CPU_FLOAT(%[[VAL_0:.*]]: tensor<1xf32>, %[[VAL_1:.*]]: tensor<1xf32>) -> tensor<2x1xf32> attributes {tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "func_2"} { +// CHECK: %[[VAL_2:.*]] = "tfl.pack"(%[[VAL_0]], %[[VAL_1]]) <{axis = 0 : i32, values_count = 2 : i32}> {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<2x1xf32> +// CHECK: return %[[VAL_2]] : tensor<2x1xf32> +// CHECK: } + +// CHECK: func private @func_0_GPU_FLOAT(%[[VAL_0:.*]]: tensor<1xf32>, %[[VAL_1:.*]]: tensor<1xf32>, %[[VAL_2:.*]]: tensor<1xf32>) -> tensor<1xf32> attributes {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} { +// CHECK: %[[VAL_3:.*]] = tfl.add %[[VAL_0]], %[[VAL_1]] {fused_activation_function = "RELU6", tac.device = "GPU", tac.inference_type = "FLOAT"} : tensor<1xf32> +// CHECK: %[[VAL_4:.*]] = tfl.mul %[[VAL_3]], %[[VAL_2]] {fused_activation_function = "RELU6", tac.device = "GPU", tac.inference_type = "FLOAT"} : tensor<1xf32> +// CHECK: return %[[VAL_4]] : tensor<1xf32> +// CHECK: } + +// CHECK: func private @func_1_GPU_FLOAT(%[[VAL_0:.*]]: tensor<1xf32>, %[[VAL_1:.*]]: tensor<1xf32>) -> tensor<1xf32> attributes {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_1"} { +// CHECK: %[[VAL_2:.*]] = tfl.add %[[VAL_0]], %[[VAL_1]] {fused_activation_function = "RELU6", tac.device = "GPU", tac.inference_type = "FLOAT"} : tensor<1xf32> +// CHECK: return %[[VAL_2]] : tensor<1xf32> +// CHECK: } + +// CHECK: func private @func_2_GPU_FLOAT(%[[VAL_0:.*]]: tensor<1xf32>, %[[VAL_1:.*]]: tensor<1xf32>) -> tensor<2x1xf32> attributes {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_2"} { +// CHECK-DAG: %[[VAL_2:.*]] = "tfl.pseudo_const"(){{.*}} dense<1> : tensor<4xi32> +// CHECK-DAG: %[[VAL_3:.*]] = "tfl.pseudo_const"(){{.*}}dense<2> : tensor<1xi32> +// CHECK-DAG: %[[VAL_4:.*]] = "tfl.pseudo_const"(){{.*}}dense<[2, 1]> : tensor<2xi32> +// CHECK: %[[VAL_5:.*]] = "tfl.reshape"(%[[VAL_0]], %[[VAL_2]]) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<1xf32>, tensor<4xi32>) -> tensor<1x1x1x1xf32> +// CHECK: %[[VAL_6:.*]] = "tfl.reshape"(%[[VAL_1]], %[[VAL_2]]) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<1xf32>, tensor<4xi32>) -> tensor<1x1x1x1xf32> +// CHECK: %[[VAL_7:.*]] = "tfl.concatenation"(%[[VAL_5]], %[[VAL_6]]) <{axis = 3 : i32, fused_activation_function = "NONE"}> {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<1x1x1x1xf32>, tensor<1x1x1x1xf32>) -> tensor<1x1x1x2xf32> +// CHECK: %[[VAL_8:.*]] = "tfl.reshape"(%[[VAL_7]], %[[VAL_3]]) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<1x1x1x2xf32>, tensor<1xi32>) -> tensor<2xf32> +// CHECK: %[[VAL_9:.*]] = "tfl.reshape"(%[[VAL_8]], %[[VAL_4]]) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<2xf32>, tensor<2xi32>) -> tensor<2x1xf32> +// CHECK: return %[[VAL_9]] : tensor<2x1xf32> +// CHECK: } + +// CHECK: func private @func_0_CPU_FLOAT(%[[VAL_0:.*]]: tensor<1xf32>, %[[VAL_1:.*]]: tensor<1xf32>, %[[VAL_2:.*]]: tensor<1xf32>) -> tensor<1xf32> attributes {tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} { +// CHECK: %[[VAL_3:.*]] = tfl.add %[[VAL_0]], %[[VAL_1]] {fused_activation_function = "RELU6", tac.device = "CPU", tac.inference_type = "FLOAT"} : tensor<1xf32> +// CHECK: %[[VAL_4:.*]] = tfl.mul %[[VAL_3]], %[[VAL_2]] {fused_activation_function = "RELU6", tac.device = "CPU", tac.inference_type = "FLOAT"} : tensor<1xf32> +// CHECK: return %[[VAL_4]] : tensor<1xf32> +// CHECK: } + +// CHECK: func private @func_1_CPU_FLOAT(%[[VAL_0:.*]]: tensor<1xf32>, %[[VAL_1:.*]]: tensor<1xf32>) -> tensor<1xf32> attributes {tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "func_1"} { +// CHECK: %[[VAL_2:.*]] = tfl.add %[[VAL_0]], %[[VAL_1]] {fused_activation_function = "RELU6", tac.device = "CPU", tac.inference_type = "FLOAT"} : tensor<1xf32> +// CHECK: return %[[VAL_2]] : tensor<1xf32> +// CHECK: } + +} + +// ----- + +module { +func.func private @func_10_CPU_FLOAT(%arg0: tensor<3xi32>, %arg1: tensor, %arg2: tensor, %arg3: tensor) -> tensor<*xf32> attributes {tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "func_10"} { + %0 = "tfl.one_hot"(%arg0, %arg1, %arg2, %arg3) {axis = -1 : i32, tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<3xi32>, tensor, tensor, tensor) -> tensor<*xf32> + func.return %0 : tensor<*xf32> +} + +// CHECK: func private @func_10_CPU_FLOAT(%[[VAL_0:.*]]: tensor<3xi32>, %[[VAL_1:.*]]: tensor, %[[VAL_2:.*]]: tensor, %[[VAL_3:.*]]: tensor) -> tensor<*xf32> attributes {tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "func_10"} { +// CHECK: %[[VAL_4:.*]] = "tfl.one_hot"(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]], %[[VAL_3]]) <{axis = -1 : i32}> {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<3xi32>, tensor, tensor, tensor) -> tensor<*xf32> +// CHECK: return %[[VAL_4]] : tensor<*xf32> +// CHECK: } + +} + +// ----- + +module { +func.func private @func_20_GPU_FLOAT(%arg0: tensor<128x128xf32>, %arg1: tensor<3xi32>) -> tensor<1x128x128xf32> attributes {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_20"} { + %0 = "tfl.reshape"(%arg0, %arg1) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<128x128xf32>, tensor<3xi32>) -> tensor<1x128x128xf32> + func.return %0 : tensor<1x128x128xf32> +} + +// CHECK: func private @func_20_GPU_FLOAT(%[[VAL_0:.*]]: tensor<128x128xf32>, %[[VAL_1:.*]]: tensor<3xi32>) -> tensor<1x128x128xf32> attributes {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_20"} { +// CHECK: %[[VAL_2:.*]] = "tfl.reshape"(%[[VAL_0]], %[[VAL_1]]) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<128x128xf32>, tensor<3xi32>) -> tensor<1x128x128xf32> +// CHECK: return %[[VAL_2]] : tensor<1x128x128xf32> +// CHECK: } + +// CHECK: func private @func_20_CPU_FLOAT(%[[VAL_0:.*]]: tensor<128x128xf32>, %[[VAL_1:.*]]: tensor<3xi32>) -> tensor<1x128x128xf32> attributes {tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "func_20"} { +// CHECK: %[[VAL_2:.*]] = "tfl.reshape"(%[[VAL_0]], %[[VAL_1]]) {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<128x128xf32>, tensor<3xi32>) -> tensor<1x128x128xf32> +// CHECK: return %[[VAL_2]] : tensor<1x128x128xf32> +// CHECK: } +} + +// ----- + +module { +func.func private @quantize_ops_CPU_QUANTIZED_INT8(%arg0: tensor<384x512x!quant.uniform>, %arg1: tensor<128x512x!quant.uniform:f32, 0.1>>, %arg2: tensor<128x!quant.uniform>, %arg3: tensor<128x!quant.uniform>) -> tensor<1x384x128x!quant.uniform> attributes {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8", tac.interface_name = "quantize_ops"} { + %0 = "tfl.pseudo_qconst"() {qtype = tensor<128x!quant.uniform>, value = dense<0> : tensor<128xi32>} : () -> tensor<128x!quant.uniform> + %1 = "tfl.fully_connected"(%arg0, %arg1, %0) {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8", fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"} : (tensor<384x512x!quant.uniform>, tensor<128x512x!quant.uniform:f32, 0.1>>, tensor<128x!quant.uniform>) -> tensor<384x128x!quant.uniform> + %2 = "arith.constant"() {value = dense<[1, 384, 128]> : tensor<3xi32>} : () -> tensor<3xi32> + %3 = "tfl.reshape"(%1, %2) {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8"} : (tensor<384x128x!quant.uniform>, tensor<3xi32>) -> tensor<1x384x128x!quant.uniform> + %4 = "tfl.mul"(%3, %arg2) {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8", fused_activation_function = "NONE"} : (tensor<1x384x128x!quant.uniform>, tensor<128x!quant.uniform>) -> tensor<1x384x128x!quant.uniform> + %5 = "tfl.add"(%4, %arg3) {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8", fused_activation_function = "NONE"} : (tensor<1x384x128x!quant.uniform>, tensor<128x!quant.uniform>) -> tensor<1x384x128x!quant.uniform> + func.return %5 : tensor<1x384x128x!quant.uniform> +} + +// CHECK: func private @quantize_ops_CPU_QUANTIZED_INT8(%[[VAL_0:.*]]: tensor<384x512x!quant.uniform>, %[[VAL_1:.*]]: tensor<128x512x!quant.uniform:f32, 1.000000e-01>>, %[[VAL_2:.*]]: tensor<128x!quant.uniform>, %[[VAL_3:.*]]: tensor<128x!quant.uniform>) -> tensor<1x384x128x!quant.uniform> attributes {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8", tac.interface_name = "quantize_ops"} { +// CHECK-DAG: %[[VAL_4:.*]] = arith.constant dense<[1, 384, 128]> : tensor<3xi32> +// CHECK-DAG: %[[VAL_5:.*]] = "tfl.pseudo_qconst"() <{qtype = tensor<128x!quant.uniform>, value = dense<0> : tensor<128xi32>}> : () -> tensor<128x!quant.uniform> +// CHECK: %[[VAL_6:.*]] = "tfl.fully_connected"(%[[VAL_0]], %[[VAL_1]], %[[VAL_5]]) <{fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"}> {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8"} : (tensor<384x512x!quant.uniform>, tensor<128x512x!quant.uniform:f32, 1.000000e-01>>, tensor<128x!quant.uniform>) -> tensor<384x128x!quant.uniform> +// CHECK: %[[VAL_7:.*]] = "tfl.reshape"(%[[VAL_6]], %[[VAL_4]]) {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8"} : (tensor<384x128x!quant.uniform>, tensor<3xi32>) -> tensor<1x384x128x!quant.uniform> +// CHECK: %[[VAL_8:.*]] = tfl.mul(%[[VAL_7]], %[[VAL_2]]) <{fused_activation_function = "NONE"}> {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8"} : (tensor<1x384x128x!quant.uniform>, tensor<128x!quant.uniform>) -> tensor<1x384x128x!quant.uniform> +// CHECK: %[[VAL_9:.*]] = tfl.add(%[[VAL_8]], %[[VAL_3]]) <{fused_activation_function = "NONE"}> {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8"} : (tensor<1x384x128x!quant.uniform>, tensor<128x!quant.uniform>) -> tensor<1x384x128x!quant.uniform> +// CHECK: return %[[VAL_9]] : tensor<1x384x128x!quant.uniform> +// CHECK: } + +// CHECK: func private @quantize_ops_GPU_FLOAT(%[[VAL_0:.*]]: tensor<384x512x!quant.uniform>, %[[VAL_1:.*]]: tensor<128x512x!quant.uniform:f32, 1.000000e-01>>, %[[VAL_2:.*]]: tensor<128x!quant.uniform>, %[[VAL_3:.*]]: tensor<128x!quant.uniform>) -> tensor<1x384x128x!quant.uniform> attributes {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "quantize_ops"} { +// CHECK-DAG: %[[VAL_4:.*]] = "tfl.pseudo_const"(){{.*}}dense<0.000000e+00> : tensor<128xf32> +// CHECK-DAG: %[[VAL_5:.*]] = arith.constant dense<[1, 384, 128]> : tensor<3xi32> +// CHECK-DAG: %[[VAL_6:.*]] = "tfl.pseudo_const"(){{.*}}dense<[1, 1, 384, 512]> : tensor<4xi32> +// CHECK-DAG: %[[VAL_7:.*]] = "tfl.pseudo_const"(){{.*}}dense<[128, 1, 1, 512]> : tensor<4xi32> +// CHECK-DAG: %[[VAL_8:.*]] = "tfl.pseudo_const"(){{.*}}dense<[384, 128]> : tensor<2xi32> +// CHECK: %[[VAL_9:.*]] = "tfl.dequantize"(%[[VAL_0]]) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<384x512x!quant.uniform>) -> tensor<384x512xf32> +// CHECK: %[[VAL_10:.*]] = "tfl.dequantize"(%[[VAL_1]]) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<128x512x!quant.uniform:f32, 1.000000e-01>>) -> tensor<128x512xf32> +// CHECK: %[[VAL_11:.*]] = "tfl.reshape"(%[[VAL_9]], %[[VAL_6]]) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<384x512xf32>, tensor<4xi32>) -> tensor<1x1x384x512xf32> +// CHECK: %[[VAL_12:.*]] = "tfl.reshape"(%[[VAL_10]], %[[VAL_7]]) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<128x512xf32>, tensor<4xi32>) -> tensor<128x1x1x512xf32> +// CHECK: %[[VAL_13:.*]] = "tfl.conv_2d"(%[[VAL_11]], %[[VAL_12]], %[[VAL_4]]) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32}> {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<1x1x384x512xf32>, tensor<128x1x1x512xf32>, tensor<128xf32>) -> tensor<1x1x384x128xf32> +// CHECK: %[[VAL_14:.*]] = "tfl.reshape"(%[[VAL_13]], %[[VAL_8]]) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<1x1x384x128xf32>, tensor<2xi32>) -> tensor<384x128xf32> +// CHECK: %[[VAL_15:.*]] = "tfl.reshape"(%[[VAL_14]], %[[VAL_5]]) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<384x128xf32>, tensor<3xi32>) -> tensor<1x384x128xf32> +// CHECK: %[[VAL_16:.*]] = "tfl.dequantize"(%[[VAL_2]]) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<128x!quant.uniform>) -> tensor<128xf32> +// CHECK: %[[VAL_17:.*]] = tfl.mul(%[[VAL_15]], %[[VAL_16]]) <{fused_activation_function = "NONE"}> {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<1x384x128xf32>, tensor<128xf32>) -> tensor<1x384x128xf32> +// CHECK: %[[VAL_18:.*]] = "tfl.dequantize"(%[[VAL_3]]) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<128x!quant.uniform>) -> tensor<128xf32> +// CHECK: %[[VAL_19:.*]] = tfl.add(%[[VAL_17]], %[[VAL_18]]) <{fused_activation_function = "NONE"}> {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<1x384x128xf32>, tensor<128xf32>) -> tensor<1x384x128xf32> +// CHECK: %[[VAL_20:.*]] = "tfl.quantize"(%[[VAL_19]]) <{qtype = tensor<1x384x128x!quant.uniform>}> {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<1x384x128xf32>) -> tensor<1x384x128x!quant.uniform> +// CHECK: return %[[VAL_20]] : tensor<1x384x128x!quant.uniform> +// CHECK: } + +// CHECK: func private @quantize_ops_CPU_FLOAT(%[[VAL_0:.*]]: tensor<384x512x!quant.uniform>, %[[VAL_1:.*]]: tensor<128x512x!quant.uniform:f32, 1.000000e-01>>, %[[VAL_2:.*]]: tensor<128x!quant.uniform>, %[[VAL_3:.*]]: tensor<128x!quant.uniform>) -> tensor<1x384x128x!quant.uniform> attributes {tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "quantize_ops"} { +// CHECK-DAG: %[[VAL_4:.*]] = "tfl.pseudo_const"(){{.*}}dense<0.000000e+00> : tensor<128xf32> +// CHECK-DAG: %[[VAL_5:.*]] = arith.constant dense<[1, 384, 128]> : tensor<3xi32> +// CHECK: %[[VAL_6:.*]] = "tfl.dequantize"(%[[VAL_0]]) {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<384x512x!quant.uniform>) -> tensor<384x512xf32> +// CHECK: %[[VAL_7:.*]] = "tfl.dequantize"(%[[VAL_1]]) {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<128x512x!quant.uniform:f32, 1.000000e-01>>) -> tensor<128x512xf32> +// CHECK: %[[VAL_8:.*]] = "tfl.fully_connected"(%[[VAL_6]], %[[VAL_7]], %[[VAL_4]]) <{fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"}> {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<384x512xf32>, tensor<128x512xf32>, tensor<128xf32>) -> tensor<384x128xf32> +// CHECK: %[[VAL_9:.*]] = "tfl.reshape"(%[[VAL_8]], %[[VAL_5]]) {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<384x128xf32>, tensor<3xi32>) -> tensor<1x384x128xf32> +// CHECK: %[[VAL_10:.*]] = "tfl.dequantize"(%[[VAL_2]]) {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<128x!quant.uniform>) -> tensor<128xf32> +// CHECK: %[[VAL_11:.*]] = tfl.mul(%[[VAL_9]], %[[VAL_10]]) <{fused_activation_function = "NONE"}> {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<1x384x128xf32>, tensor<128xf32>) -> tensor<1x384x128xf32> +// CHECK: %[[VAL_12:.*]] = "tfl.dequantize"(%[[VAL_3]]) {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<128x!quant.uniform>) -> tensor<128xf32> +// CHECK: %[[VAL_13:.*]] = tfl.add(%[[VAL_11]], %[[VAL_12]]) <{fused_activation_function = "NONE"}> {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<1x384x128xf32>, tensor<128xf32>) -> tensor<1x384x128xf32> +// CHECK: %[[VAL_14:.*]] = "tfl.quantize"(%[[VAL_13]]) <{qtype = tensor<1x384x128x!quant.uniform>}> {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<1x384x128xf32>) -> tensor<1x384x128x!quant.uniform> +// CHECK: return %[[VAL_14]] : tensor<1x384x128x!quant.uniform> +// CHECK: } + +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/get-op-cost.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/get-op-cost.mlir new file mode 100644 index 0000000000000000000000000000000000000000..67b211265ef72a3937693b44ba042b535ee06536 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/get-op-cost.mlir @@ -0,0 +1,87 @@ +// RUN: tac-opt-all-backends -tfl-get-op-cost %s -split-input-file -verify-diagnostics | FileCheck %s + +func.func @func_0_CPU(%arg0: tensor<256x32x32x3xf32>, %arg1: tensor<256x32x32x3xf32>) -> tensor<256x32x32x3xf32> attributes {tac.device = "CPU", tac.interface_name = "func_0"} { + // CHECK: tac.cost = 7.864320e+05 + %0 = "tfl.add"(%arg0, %arg1) {fused_activation_function = "RELU", tac.device = "CPU"} : (tensor<256x32x32x3xf32>, tensor<256x32x32x3xf32>) -> tensor<256x32x32x3xf32> + func.return %0 : tensor<256x32x32x3xf32> +} + +func.func @func_0_GPU(%arg0: tensor<256x32x32x3xf32>, %arg1: tensor<256x32x32x3xf32>) -> tensor<256x32x32x3xf32> attributes {tac.device = "GPU", tac.interface_name = "func_0"} { + // CHECK: tac.cost = 157286.40 + %0 = "tfl.add"(%arg0, %arg1) {fused_activation_function = "RELU", tac.device = "GPU"} : (tensor<256x32x32x3xf32>, tensor<256x32x32x3xf32>) -> tensor<256x32x32x3xf32> + func.return %0 : tensor<256x32x32x3xf32> +} + +// ----- + +func.func @func_0_CPU(%arg0: tensor<10x10x10xf32>, %arg1: tensor<10xf32>) -> tensor<10x10x10xf32> attributes {tac.device = "CPU", tac.interface_name = "func_0"} { + // CHECK: tac.cost = 1.000000e+03 + %0 = "tfl.add"(%arg0, %arg1) {fused_activation_function = "RELU", tac.device = "CPU"} : (tensor<10x10x10xf32>, tensor<10xf32>) -> tensor<10x10x10xf32> + func.return %0 : tensor<10x10x10xf32> +} + +// ----- + +func.func @func_0_CPU(%arg0: tensor<10x10x10xf32>, %arg1: tensor<10xf32>) -> tensor<10x10x10xf32> attributes {tac.device = "CPU", tac.interface_name = "func_0"} { + // CHECK: tac.cost = 1.000000e+03 + %0 = "tfl.add"(%arg0, %arg1) {fused_activation_function = "RELU", tac.device = "CPU"} : (tensor<10x10x10xf32>, tensor<10xf32>) -> tensor<10x10x10xf32> + // CHECK: tac.cost = 1.000000e+03 + %1 = "tfl.mul"(%0, %arg1) {fused_activation_function = "RELU", tac.device = "CPU"} : (tensor<10x10x10xf32>, tensor<10xf32>) -> tensor<10x10x10xf32> + func.return %1 : tensor<10x10x10xf32> +} + +// ----- + +func.func @pack_CPU(%arg0: tensor<100xf32>, %arg1: tensor<100xf32>) -> tensor<2x100xf32> attributes {tac.device = "CPU", tac.interface_name = "func_2"} { + // CHECK: tac.cost = 1.000000e+02 + %0 = "tfl.pack"(%arg0, %arg1) {axis = 0 : i32, tac.device = "CPU", values_count = 2 : i32} : (tensor<100xf32>, tensor<100xf32>) -> tensor<2x100xf32> + func.return %0 : tensor<2x100xf32> +} + +func.func @concat_reshape_GPU(%arg0: tensor<100xf32>, %arg1: tensor<100xf32>) -> tensor<2x100xf32> attributes {tac.device = "GPU", tac.interface_name = "func_2"} { + %cst = arith.constant dense<[2, 100]> : tensor<2xi64> + // CHECK: tac.cost = 4.000000e+01 + %0 = "tfl.concatenation"(%arg0, %arg1) {axis = 0 : i32, fused_activation_function = "NONE", tac.device = "GPU"} : (tensor<100xf32>, tensor<100xf32>) -> tensor<200xf32> + // CHECK: tac.cost = 4.040000e+01 + %1 = "tfl.reshape"(%0, %cst) {tac.device = "GPU"} : (tensor<200xf32>, tensor<2xi64>) -> tensor<2x100xf32> + func.return %1 : tensor<2x100xf32> +} + +func.func @concat_reshape_CPU(%arg0: tensor<100xf32>, %arg1: tensor<100xf32>) -> tensor<2x100xf32> attributes {tac.device = "CPU", tac.interface_name = "func_2"} { + %cst = arith.constant dense<[2, 100]> : tensor<2xi64> + // CHECK: tac.cost = 1.000000e+02 + %0 = "tfl.concatenation"(%arg0, %arg1) {axis = 0 : i32, fused_activation_function = "NONE", tac.device = "CPU"} : (tensor<100xf32>, tensor<100xf32>) -> tensor<200xf32> + // CHECK: tac.cost = 1.010000e+02 + %1 = "tfl.reshape"(%0, %cst) {tac.device = "CPU"} : (tensor<200xf32>, tensor<2xi64>) -> tensor<2x100xf32> + func.return %1 : tensor<2x100xf32> +} + +// ----- + +func.func @testConv2DCPU(tensor<256x32x32x3xf32>, tensor<16x3x3x3xf32>, tensor<16xf32>) -> tensor<256x32x32x16xf32> { +^bb0(%arg0: tensor<256x32x32x3xf32>, %arg1: tensor<16x3x3x3xf32>, %arg2: tensor<16xf32>): + // CHECK: tac.cost = 0x4D5C0000 + %0 = "tfl.conv_2d"(%arg0, %arg1, %arg2) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32, fused_activation_function = "RELU6", tac.device = "CPU"} : (tensor<256x32x32x3xf32>, tensor<16x3x3x3xf32>, tensor<16xf32>) -> tensor<256x32x32x16xf32> + func.return %0 : tensor<256x32x32x16xf32> +} + +func.func @testConv2DGPU(tensor<256x32x32x3xf32>, tensor<16x3x3x3xf32>, tensor<16xf32>) -> tensor<256x32x32x16xf32> { +^bb0(%arg0: tensor<256x32x32x3xf32>, %arg1: tensor<16x3x3x3xf32>, %arg2: tensor<16xf32>): + // CHECK: tac.cost = 0x4C300000 + %0 = "tfl.conv_2d"(%arg0, %arg1, %arg2) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32, fused_activation_function = "RELU6", tac.device = "GPU"} : (tensor<256x32x32x3xf32>, tensor<16x3x3x3xf32>, tensor<16xf32>) -> tensor<256x32x32x16xf32> + func.return %0 : tensor<256x32x32x16xf32> +} + +// ----- + +func.func @testConv2DNoBiasCPU(%arg0: tensor<128x32x32x3xf32>, %arg1: tensor<64x3x3x3xf32>, %arg2: none) -> tensor<128x32x32x64xf32> { + // CHECK: tac.cost = 0x4DD80000 + %0 = "tfl.conv_2d"(%arg0, %arg1, %arg2) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32, fused_activation_function = "RELU6", tac.device = "CPU"} : (tensor<128x32x32x3xf32>, tensor<64x3x3x3xf32>, none) -> tensor<128x32x32x64xf32> + func.return %0 : tensor<128x32x32x64xf32> +} + +func.func @testConv2DNoBiasGPU(%arg0: tensor<128x32x32x3xf32>, %arg1: tensor<64x3x3x3xf32>, %arg2: none) -> tensor<128x32x32x64xf32> { + // CHECK: tac.cost = 0x4CACCCCD + %0 = "tfl.conv_2d"(%arg0, %arg1, %arg2) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32, fused_activation_function = "RELU6", tac.device = "GPU"} : (tensor<128x32x32x3xf32>, tensor<64x3x3x3xf32>, none) -> tensor<128x32x32x64xf32> + func.return %0 : tensor<128x32x32x64xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/pick-subgraphs.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/pick-subgraphs.mlir new file mode 100644 index 0000000000000000000000000000000000000000..71f8a90f36088658c4098d73817e510478f1a53c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/pick-subgraphs.mlir @@ -0,0 +1,190 @@ +// RUN: tac-opt-all-backends -tfl-pick-subgraphs %s -split-input-file -verify-diagnostics | FileCheck %s + +module { + func.func @main(%arg0: tensor<100xf32>, %arg1: tensor<100xf32>, %arg2: tensor<100xf32>, %arg3: tensor<100xf32>) -> tensor<2x100xf32> { + %0 = func.call @func_0_GPU_FLOAT(%arg0, %arg1, %arg2) {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} : (tensor<100xf32>, tensor<100xf32>, tensor<100xf32>) -> tensor<100xf32> + %1 = func.call @func_1_GPU_FLOAT(%arg0, %arg3) {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_1"} : (tensor<100xf32>, tensor<100xf32>) -> tensor<100xf32> + %2 = func.call @func_2_CPU_FLOAT(%0, %1) {tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "func_2"} : (tensor<100xf32>, tensor<100xf32>) -> tensor<2x100xf32> + func.return %2 : tensor<2x100xf32> + } + func.func @func_2_CPU_FLOAT(%arg0: tensor<100xf32>, %arg1: tensor<100xf32>) -> tensor<2x100xf32> attributes {tac.cost = 2.000000e+01 : f32, tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "func_2"} { + %0 = "tfl.pack"(%arg0, %arg1) {axis = 0 : i32, tac.device = "CPU", values_count = 2 : i32} : (tensor<100xf32>, tensor<100xf32>) -> tensor<2x100xf32> + func.return %0 : tensor<2x100xf32> + } + func.func @func_0_GPU_FLOAT(%arg0: tensor<100xf32>, %arg1: tensor<100xf32>, %arg2: tensor<100xf32>) -> tensor<100xf32> attributes {tac.cost = 4.000000e+01 : f32, tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} { + %0 = tfl.add %arg0, %arg1 {fused_activation_function = "RELU6", tac.device = "GPU"} : tensor<100xf32> + %1 = tfl.mul %0, %arg2 {fused_activation_function = "RELU6", tac.device = "GPU"} : tensor<100xf32> + func.return %1 : tensor<100xf32> + } + func.func @func_1_GPU_FLOAT(%arg0: tensor<100xf32>, %arg1: tensor<100xf32>) -> tensor<100xf32> attributes {tac.cost = 2.000000e+01 : f32, tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_1"} { + %0 = tfl.add %arg0, %arg1 {fused_activation_function = "RELU6", tac.device = "GPU"} : tensor<100xf32> + func.return %0 : tensor<100xf32> + } + func.func @func_2_GPU_FLOAT(%arg0: tensor<100xf32>, %arg1: tensor<100xf32>) -> tensor<2x100xf32> attributes {tac.cost = 8.040000e+01 : f32, tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_2"} { + %cst = arith.constant dense<[2, 100]> : tensor<2xi64> + %0 = "tfl.concatenation"(%arg0, %arg1) {axis = 0 : i32, fused_activation_function = "NONE"} : (tensor<100xf32>, tensor<100xf32>) -> tensor<200xf32> + %1 = "tfl.reshape"(%0, %cst) : (tensor<200xf32>, tensor<2xi64>) -> tensor<2x100xf32> + func.return %1 : tensor<2x100xf32> + } + func.func @func_0_CPU_FLOAT(%arg0: tensor<100xf32>, %arg1: tensor<100xf32>, %arg2: tensor<100xf32>) -> tensor<100xf32> attributes {tac.cost = 2.000000e+02 : f32, tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} { + %0 = tfl.add %arg0, %arg1 {fused_activation_function = "RELU6", tac.device = "GPU"} : tensor<100xf32> + %1 = tfl.mul %0, %arg2 {fused_activation_function = "RELU6", tac.device = "GPU"} : tensor<100xf32> + func.return %1 : tensor<100xf32> + } + func.func @func_1_CPU_FLOAT(%arg0: tensor<100xf32>, %arg1: tensor<100xf32>) -> tensor<100xf32> attributes {tac.cost = 1.000000e+02 : f32, tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "func_1"} { + %0 = tfl.add %arg0, %arg1 {fused_activation_function = "RELU6", tac.device = "GPU"} : tensor<100xf32> + func.return %0 : tensor<100xf32> + } + +// CHECK: func @main([[VAL_0:%.*]]: tensor<100xf32>, [[VAL_1:%.*]]: tensor<100xf32>, [[VAL_2:%.*]]: tensor<100xf32>, [[VAL_3:%.*]]: tensor<100xf32>) -> tensor<2x100xf32> { +// CHECK: [[VAL_4:%.*]] = call @func_0_GPU_FLOAT([[VAL_0]], [[VAL_1]], [[VAL_2]]) {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} : (tensor<100xf32>, tensor<100xf32>, tensor<100xf32>) -> tensor<100xf32> +// CHECK: [[VAL_5:%.*]] = call @func_1_GPU_FLOAT([[VAL_0]], [[VAL_3]]) {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_1"} : (tensor<100xf32>, tensor<100xf32>) -> tensor<100xf32> +// CHECK: [[VAL_6:%.*]] = call @func_2_GPU_FLOAT([[VAL_4]], [[VAL_5]]) {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_2"} : (tensor<100xf32>, tensor<100xf32>) -> tensor<2x100xf32> +// CHECK: return [[VAL_6]] : tensor<2x100xf32> +// CHECK: } +} + +// ----- + +module { + func.func @main(%arg0: tensor<1x200x200x200xf32>) -> tensor<2x1x200x200x200xf32> attributes {tf.entry_function = {inputs = "Placeholder", outputs = "mul_1"}} { + %0 = "tfl.pseudo_const"() {value = dense<0.962260901> : tensor<1xf32>} : () -> tensor<1xf32> + %1 = func.call @func_0_GPU_FLOAT(%arg0, %0) {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} : (tensor<1x200x200x200xf32>, tensor<1xf32>) -> tensor<1x200x200x200xf32> + %2 = "tfl.pseudo_const"() {value = dense<0.895973444> : tensor<1xf32>} : () -> tensor<1xf32> + %3 = func.call @func_1_GPU_FLOAT(%arg0, %2) {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_1"} : (tensor<1x200x200x200xf32>, tensor<1xf32>) -> tensor<1x200x200x200xf32> + %4 = func.call @func_2_CPU_FLOAT(%3, %1) {tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "func_2"} : (tensor<1x200x200x200xf32>, tensor<1x200x200x200xf32>) -> tensor<2x1x200x200x200xf32> + %5 = "tfl.pseudo_const"() {value = dense<0.0778453499> : tensor<1xf32>} : () -> tensor<1xf32> + %6 = func.call @func_3_GPU_FLOAT(%4, %5) {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_3"} : (tensor<2x1x200x200x200xf32>, tensor<1xf32>) -> tensor<2x1x200x200x200xf32> + func.return %6 : tensor<2x1x200x200x200xf32> + } + func.func @func_2_CPU_FLOAT(%arg0: tensor<1x200x200x200xf32>, %arg1: tensor<1x200x200x200xf32>) -> tensor<2x1x200x200x200xf32> attributes {tac.cost = 1.600000e+06 : f32, tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "func_2"} { + %0 = "tfl.pack"(%arg0, %arg1) {axis = 0 : i32, tac.device = "CPU", values_count = 2 : i32} : (tensor<1x200x200x200xf32>, tensor<1x200x200x200xf32>) -> tensor<2x1x200x200x200xf32> + func.return %0 : tensor<2x1x200x200x200xf32> + } + func.func @func_0_GPU_FLOAT(%arg0: tensor<1x200x200x200xf32>, %arg1: tensor<1xf32>) -> tensor<1x200x200x200xf32> attributes {tac.cost = 1.600000e+06 : f32, tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} { + %0 = "tfl.add"(%arg0, %arg1) {fused_activation_function = "NONE", tac.device = "GPU"} : (tensor<1x200x200x200xf32>, tensor<1xf32>) -> tensor<1x200x200x200xf32> + func.return %0 : tensor<1x200x200x200xf32> + } + func.func @func_1_GPU_FLOAT(%arg0: tensor<1x200x200x200xf32>, %arg1: tensor<1xf32>) -> tensor<1x200x200x200xf32> attributes {tac.cost = 1.600000e+06 : f32, tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_1"} { + %0 = "tfl.mul"(%arg0, %arg1) {fused_activation_function = "NONE", tac.device = "GPU"} : (tensor<1x200x200x200xf32>, tensor<1xf32>) -> tensor<1x200x200x200xf32> + func.return %0 : tensor<1x200x200x200xf32> + } + func.func @func_3_GPU_FLOAT(%arg0: tensor<2x1x200x200x200xf32>, %arg1: tensor<1xf32>) -> tensor<2x1x200x200x200xf32> attributes {tac.cost = 3.200000e+06 : f32, tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_3"} { + %0 = "tfl.mul"(%arg0, %arg1) {fused_activation_function = "NONE", tac.device = "GPU"} : (tensor<2x1x200x200x200xf32>, tensor<1xf32>) -> tensor<2x1x200x200x200xf32> + func.return %0 : tensor<2x1x200x200x200xf32> + } + func.func @func_2_GPU_FLOAT(%arg0: tensor<1x200x200x200xf32>, %arg1: tensor<1x200x200x200xf32>) -> tensor<2x1x200x200x200xf32> attributes {tac.cost = 0x4AC35002 : f32, tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_2"} { + %cst = arith.constant dense<[2, 1, 200, 200, 200]> : tensor<5xi64> + %0 = "tfl.concatenation"(%arg0, %arg1) {axis = 0 : i32, fused_activation_function = "NONE"} : (tensor<1x200x200x200xf32>, tensor<1x200x200x200xf32>) -> tensor<2x200x200x200xf32> + %1 = "tfl.reshape"(%0, %cst) : (tensor<2x200x200x200xf32>, tensor<5xi64>) -> tensor<2x1x200x200x200xf32> + func.return %1 : tensor<2x1x200x200x200xf32> + } + func.func @func_0_CPU_FLOAT(%arg0: tensor<1x200x200x200xf32>, %arg1: tensor<1xf32>) -> tensor<1x200x200x200xf32> attributes {tac.cost = 8.000000e+06 : f32, tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} { + %0 = "tfl.add"(%arg0, %arg1) {fused_activation_function = "NONE", tac.device = "GPU"} : (tensor<1x200x200x200xf32>, tensor<1xf32>) -> tensor<1x200x200x200xf32> + func.return %0 : tensor<1x200x200x200xf32> + } + func.func @func_1_CPU_FLOAT(%arg0: tensor<1x200x200x200xf32>, %arg1: tensor<1xf32>) -> tensor<1x200x200x200xf32> attributes {tac.cost = 8.000000e+06 : f32, tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name= "func_1"} { + %0 = "tfl.mul"(%arg0, %arg1) {fused_activation_function = "NONE", tac.device = "GPU"} : (tensor<1x200x200x200xf32>, tensor<1xf32>) -> tensor<1x200x200x200xf32> + func.return %0 : tensor<1x200x200x200xf32> + } + func.func @func_3_CPU_FLOAT(%arg0: tensor<2x1x200x200x200xf32>, %arg1: tensor<1xf32>) -> tensor<2x1x200x200x200xf32> attributes {tac.cost = 1.600000e+07 : f32, tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "func_3"} { + %0 = "tfl.mul"(%arg0, %arg1) {fused_activation_function = "NONE", tac.device = "GPU"} : (tensor<2x1x200x200x200xf32>, tensor<1xf32>) -> tensor<2x1x200x200x200xf32> + func.return %0 : tensor<2x1x200x200x200xf32> + } + +// CHECK: func @main([[VAL_0:%.*]]: tensor<1x200x200x200xf32>) -> tensor<2x1x200x200x200xf32> attributes {tf.entry_function = {inputs = "Placeholder", outputs = "mul_1"}} { +// CHECK: [[VAL_1:%.*]] = "tfl.pseudo_const"() <{value = dense<0.962260901> : tensor<1xf32>}> : () -> tensor<1xf32> +// CHECK: [[VAL_2:%.*]] = call @func_0_GPU_FLOAT([[VAL_0]], [[VAL_1]]) {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} : (tensor<1x200x200x200xf32>, tensor<1xf32>) -> tensor<1x200x200x200xf32> +// CHECK: [[VAL_3:%.*]] = "tfl.pseudo_const"() <{value = dense<0.895973444> : tensor<1xf32>}> : () -> tensor<1xf32> +// CHECK: [[VAL_4:%.*]] = call @func_1_GPU_FLOAT([[VAL_0]], [[VAL_3]]) {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_1"} : (tensor<1x200x200x200xf32>, tensor<1xf32>) -> tensor<1x200x200x200xf32> +// CHECK: [[VAL_5:%.*]] = call @func_2_GPU_FLOAT([[VAL_4]], [[VAL_2]]) {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_2"} : (tensor<1x200x200x200xf32>, tensor<1x200x200x200xf32>) -> tensor<2x1x200x200x200xf32> +// CHECK: [[VAL_6:%.*]] = "tfl.pseudo_const"() <{value = dense<0.0778453499> : tensor<1xf32>}> : () -> tensor<1xf32> +// CHECK: [[VAL_7:%.*]] = call @func_3_GPU_FLOAT([[VAL_5]], [[VAL_6]]) {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_3"} : (tensor<2x1x200x200x200xf32>, tensor<1xf32>) -> tensor<2x1x200x200x200xf32> +// CHECK: return [[VAL_7]] : tensor<2x1x200x200x200xf32> +// CHECK: } +} + +// ----- + +module { + func.func @main(%arg0: tensor<100x!quant.uniform>, %arg1: tensor<100x!quant.uniform>, %arg2: tensor<100x!quant.uniform>, %arg3: tensor<100x!quant.uniform>) -> tensor<2x100x!quant.uniform> attributes {tf.entry_function = {inputs = "input0,input1,input2,input3", outputs = "output"}} { + %0 = func.call @func_0_CPU_QUANTIZED_INT8(%arg0, %arg1, %arg2) {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8", tac.interface_name = "func_0"} : (tensor<100x!quant.uniform>, tensor<100x!quant.uniform>, tensor<100x!quant.uniform>) -> tensor<100x!quant.uniform> + %1 = func.call @func_1_CPU_QUANTIZED_INT8(%arg0, %arg3) {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8", tac.interface_name = "func_1"} : (tensor<100x!quant.uniform>, tensor<100x!quant.uniform>) -> tensor<100x!quant.uniform> + %2 = func.call @func_2_CPU_QUANTIZED_INT8(%0, %1) {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8", tac.interface_name = "func_2"} : (tensor<100x!quant.uniform>, tensor<100x!quant.uniform>) -> tensor<2x100x!quant.uniform> + func.return %2 : tensor<2x100x!quant.uniform> + } + func.func private @func_0_CPU_QUANTIZED_INT8(%arg0: tensor<100x!quant.uniform>, %arg1: tensor<100x!quant.uniform>, %arg2: tensor<100x!quant.uniform>) -> tensor<100x!quant.uniform> attributes {tac.cost = 2.000000e+02 : f32, tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8", tac.interface_name = "func_0"} { + %0 = tfl.add %arg0, %arg1 {fused_activation_function = "RELU6", tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8"} : tensor<100x!quant.uniform> + %1 = tfl.mul %0, %arg2 {fused_activation_function = "RELU6", tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8"} : tensor<100x!quant.uniform> + func.return %1 : tensor<100x!quant.uniform> + } + func.func private @func_2_CPU_QUANTIZED_INT8(%arg0: tensor<100x!quant.uniform>, %arg1: tensor<100x!quant.uniform>) -> tensor<2x100x!quant.uniform> attributes {tac.cost = 1.000000e+02 : f32, tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8", tac.interface_name = "func_2"} { + %0 = "tfl.pack"(%arg0, %arg1) {axis = 0 : i32, tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8", values_count = 2 : i32} : (tensor<100x!quant.uniform>, tensor<100x!quant.uniform>) -> tensor<2x100x!quant.uniform> + func.return %0 : tensor<2x100x!quant.uniform> + } + func.func private @func_1_CPU_QUANTIZED_INT8(%arg0: tensor<100x!quant.uniform>, %arg1: tensor<100x!quant.uniform>) -> tensor<100x!quant.uniform> attributes {tac.cost = 1.000000e+02 : f32, tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8", tac.interface_name = "func_1"} { + %0 = tfl.add %arg0, %arg1 {fused_activation_function = "RELU6", tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8"} : tensor<100x!quant.uniform> + func.return %0 : tensor<100x!quant.uniform> + } + func.func private @func_0_GPU_FLOAT(%arg0: tensor<100x!quant.uniform>, %arg1: tensor<100x!quant.uniform>, %arg2: tensor<100x!quant.uniform>) -> tensor<100x!quant.uniform> attributes {tac.cost = 4.000000e+01 : f32, tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} { + %0 = "tfl.dequantize"(%arg0) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<100x!quant.uniform>) -> tensor<100xf32> + %1 = "tfl.dequantize"(%arg1) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<100x!quant.uniform>) -> tensor<100xf32> + %2 = tfl.add %0, %1 {fused_activation_function = "RELU6", tac.device = "GPU", tac.inference_type = "FLOAT"} : tensor<100xf32> + %3 = "tfl.dequantize"(%arg2) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<100x!quant.uniform>) -> tensor<100xf32> + %4 = tfl.mul %2, %3 {fused_activation_function = "RELU6", tac.device = "GPU", tac.inference_type = "FLOAT"} : tensor<100xf32> + %5 = "tfl.quantize"(%4) {qtype = tensor<100x!quant.uniform>, tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<100xf32>) -> tensor<100x!quant.uniform> + func.return %5 : tensor<100x!quant.uniform> + } + func.func private @func_0_CPU_FLOAT(%arg0: tensor<100x!quant.uniform>, %arg1: tensor<100x!quant.uniform>, %arg2: tensor<100x!quant.uniform>) -> tensor<100x!quant.uniform> attributes {tac.cost = 2.000000e+02 : f32, tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} { + %0 = "tfl.dequantize"(%arg0) {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<100x!quant.uniform>) -> tensor<100xf32> + %1 = "tfl.dequantize"(%arg1) {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<100x!quant.uniform>) -> tensor<100xf32> + %2 = tfl.add %0, %1 {fused_activation_function = "RELU6", tac.device = "CPU", tac.inference_type = "FLOAT"} : tensor<100xf32> + %3 = "tfl.dequantize"(%arg2) {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<100x!quant.uniform>) -> tensor<100xf32> + %4 = tfl.mul %2, %3 {fused_activation_function = "RELU6", tac.device = "CPU", tac.inference_type = "FLOAT"} : tensor<100xf32> + %5 = "tfl.quantize"(%4) {qtype = tensor<100x!quant.uniform>, tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<100xf32>) -> tensor<100x!quant.uniform> + func.return %5 : tensor<100x!quant.uniform> + } + func.func private @func_2_GPU_FLOAT(%arg0: tensor<100x!quant.uniform>, %arg1: tensor<100x!quant.uniform>) -> tensor<2x100x!quant.uniform> attributes {tac.cost = 162.200012 : f32, tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_2"} { + %cst = arith.constant dense<[1, 1, 1, 100]> : tensor<4xi32> + %cst_0 = arith.constant dense<200> : tensor<1xi32> + %cst_1 = arith.constant dense<[2, 100]> : tensor<2xi32> + %0 = "tfl.dequantize"(%arg0) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<100x!quant.uniform>) -> tensor<100xf32> + %1 = "tfl.dequantize"(%arg1) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<100x!quant.uniform>) -> tensor<100xf32> + %2 = "tfl.reshape"(%0, %cst) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<100xf32>, tensor<4xi32>) -> tensor<1x1x1x100xf32> + %3 = "tfl.reshape"(%1, %cst) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<100xf32>, tensor<4xi32>) -> tensor<1x1x1x100xf32> + %4 = "tfl.concatenation"(%2, %3) {axis = 3 : i32, fused_activation_function = "NONE", tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<1x1x1x100xf32>, tensor<1x1x1x100xf32>) -> tensor<1x1x1x200xf32> + %5 = "tfl.reshape"(%4, %cst_0) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<1x1x1x200xf32>, tensor<1xi32>) -> tensor<200xf32> + %6 = "tfl.reshape"(%5, %cst_1) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<200xf32>, tensor<2xi32>) -> tensor<2x100xf32> + %7 = "tfl.quantize"(%6) {qtype = tensor<2x100x!quant.uniform>, tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<2x100xf32>) -> tensor<2x100x!quant.uniform> + func.return %7 : tensor<2x100x!quant.uniform> + } + func.func private @func_2_CPU_FLOAT(%arg0: tensor<100x!quant.uniform>, %arg1: tensor<100x!quant.uniform>) -> tensor<2x100x!quant.uniform> attributes {tac.cost = 1.000000e+02 : f32, tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "func_2"} { + %0 = "tfl.dequantize"(%arg0) {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<100x!quant.uniform>) -> tensor<100xf32> + %1 = "tfl.dequantize"(%arg1) {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<100x!quant.uniform>) -> tensor<100xf32> + %2 = "tfl.pack"(%0, %1) {axis = 0 : i32, tac.device = "CPU", tac.inference_type = "FLOAT", values_count = 2 : i32} : (tensor<100xf32>, tensor<100xf32>) -> tensor<2x100xf32> + %3 = "tfl.quantize"(%2) {qtype = tensor<2x100x!quant.uniform>, tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<2x100xf32>) -> tensor<2x100x!quant.uniform> + func.return %3 : tensor<2x100x!quant.uniform> + } + func.func private @func_1_GPU_FLOAT(%arg0: tensor<100x!quant.uniform>, %arg1: tensor<100x!quant.uniform>) -> tensor<100x!quant.uniform> attributes {tac.cost = 2.000000e+01 : f32, tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_1"} { + %0 = "tfl.dequantize"(%arg0) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<100x!quant.uniform>) -> tensor<100xf32> + %1 = "tfl.dequantize"(%arg1) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<100x!quant.uniform>) -> tensor<100xf32> + %2 = tfl.add %0, %1 {fused_activation_function = "RELU6", tac.device = "GPU", tac.inference_type = "FLOAT"} : tensor<100xf32> + %3 = "tfl.quantize"(%2) {qtype = tensor<100x!quant.uniform>, tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<100xf32>) -> tensor<100x!quant.uniform> + func.return %3 : tensor<100x!quant.uniform> + } + func.func private @func_1_CPU_FLOAT(%arg0: tensor<100x!quant.uniform>, %arg1: tensor<100x!quant.uniform>) -> tensor<100x!quant.uniform> attributes {tac.cost = 1.000000e+02 : f32, tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "func_1"} { + %0 = "tfl.dequantize"(%arg0) {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<100x!quant.uniform>) -> tensor<100xf32> + %1 = "tfl.dequantize"(%arg1) {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<100x!quant.uniform>) -> tensor<100xf32> + %2 = tfl.add %0, %1 {fused_activation_function = "RELU6", tac.device = "CPU", tac.inference_type = "FLOAT"} : tensor<100xf32> + %3 = "tfl.quantize"(%2) {qtype = tensor<100x!quant.uniform>, tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<100xf32>) -> tensor<100x!quant.uniform> + func.return %3 : tensor<100x!quant.uniform> + } + +// CHECK: @main(%[[VAL_0:.*]]: tensor<100x!quant.uniform>, %[[VAL_1:.*]]: tensor<100x!quant.uniform>, %[[VAL_2:.*]]: tensor<100x!quant.uniform>, %[[VAL_3:.*]]: tensor<100x!quant.uniform>) -> tensor<2x100x!quant.uniform> attributes {tf.entry_function = {inputs = "input0,input1,input2,input3", outputs = "output"}} { +// CHECK: %[[VAL_4:.*]] = call @func_0_GPU_FLOAT(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]]) {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} : (tensor<100x!quant.uniform>, tensor<100x!quant.uniform>, tensor<100x!quant.uniform>) -> tensor<100x!quant.uniform> +// CHECK: %[[VAL_5:.*]] = call @func_1_GPU_FLOAT(%[[VAL_0]], %[[VAL_3]]) {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_1"} : (tensor<100x!quant.uniform>, tensor<100x!quant.uniform>) -> tensor<100x!quant.uniform> +// CHECK: %[[VAL_6:.*]] = call @func_2_GPU_FLOAT(%[[VAL_4]], %[[VAL_5]]) {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_2"} : (tensor<100x!quant.uniform>, tensor<100x!quant.uniform>) -> tensor<2x100x!quant.uniform> +// CHECK: return %[[VAL_6]] : tensor<2x100x!quant.uniform> +// CHECK: } + +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/raise-target-subgraphs.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/raise-target-subgraphs.mlir new file mode 100644 index 0000000000000000000000000000000000000000..7f682e25537eaeda8ff72541ffd9491a6ba62dd8 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/raise-target-subgraphs.mlir @@ -0,0 +1,590 @@ +// RUN: tac-opt-all-backends -tfl-raise-target-subgraphs %s -split-input-file | FileCheck %s +// RUN: tac-opt-all-backends -tfl-raise-target-subgraphs="skip-raise-cpu-ops=true" %s -split-input-file | FileCheck %s --check-prefixes=CHECK-SKIP-CPU +// RUN: tac-opt-all-backends -tfl-raise-target-subgraphs="ignore-inference-type=true" %s -split-input-file | FileCheck %s --check-prefixes=CHECK-IGNORE-INFERENCE-TYPE + +module { +func.func @simpleWhile(%arg0: tensor) -> tensor { + %0 = "tfl.while"(%arg0) ({ + ^bb0(%block: tensor): + "tfl.yield"(%block) : (tensor) -> () + },{ + ^bb0(%block: tensor): + "tfl.yield"(%block) : (tensor) -> () + }) {tac.device = "CPU", fused_activation_function = "RELU6", tac.inference_type = "FLOAT"} : (tensor) -> tensor + func.return %0 : tensor +} +} + +// CHECK: func.func @simpleWhile(%arg0: tensor) -> tensor { +// CHECK: %0 = call @func_0_CPU_FLOAT(%arg0) {tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} : (tensor) -> tensor +// CHECK: return %0 : tensor +// CHECK: } +// CHECK: func.func private @func_0_CPU_FLOAT(%arg0: tensor) -> tensor attributes {tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} { +// CHECK: %0 = "tfl.while"(%arg0) ({ +// CHECK: ^bb0(%arg1: tensor): +// CHECK: "tfl.yield"(%arg1) : (tensor) -> () +// CHECK: }, { +// CHECK: ^bb0(%arg1: tensor): +// CHECK: "tfl.yield"(%arg1) : (tensor) -> () +// CHECK: }) {fused_activation_function = "RELU6", tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor) -> tensor +// CHECK: return %0 : tensor +// CHECK: } + +// ----- + +module { +func.func @whileWithNested(%arg0: tensor) -> tensor { + %0 = "tfl.while"(%arg0) ({ + ^bb0(%block: tensor): + %1 = "tfl.add"(%arg0, %arg0) { fused_activation_function = "NONE", tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor, tensor) -> (tensor) + %2 = "tfl.add"(%1, %1) { fused_activation_function = "NONE", tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor, tensor) -> (tensor) + "tfl.yield"(%2) : (tensor) -> () + },{ + ^bb0(%block: tensor): + "tfl.yield"(%block) : (tensor) -> () + }) {tac.device = "CPU", fused_activation_function = "RELU6", tac.inference_type = "FLOAT"} : (tensor) -> tensor + func.return %0 : tensor +} +} + +// CHECK: func.func @whileWithNested(%arg0: tensor) -> tensor { +// CHECK: %0 = call @func_0_CPU_FLOAT(%arg0) {tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} : (tensor) -> tensor +// CHECK: return %0 : tensor +// CHECK: } +// CHECK: func.func private @func_0_CPU_FLOAT(%arg0: tensor) -> tensor attributes {tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} { +// CHECK: %0 = "tfl.while"(%arg0) ({ +// CHECK: ^bb0(%arg1: tensor): +// CHECK: %1 = tfl.add %arg0, %arg0 {fused_activation_function = "NONE", tac.device = "CPU", tac.inference_type = "FLOAT"} : tensor +// CHECK: %2 = func.call @func_1_GPU_FLOAT(%1) {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_1"} : (tensor) -> tensor +// CHECK: "tfl.yield"(%2) : (tensor) -> () +// CHECK: }, { +// CHECK: ^bb0(%arg1: tensor): +// CHECK: "tfl.yield"(%arg1) : (tensor) -> () +// CHECK: }) {fused_activation_function = "RELU6", tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor) -> tensor +// CHECK: return %0 : tensor +// CHECK: } +// CHECK: func.func private @func_1_GPU_FLOAT(%arg0: tensor) -> tensor attributes {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_1"} { +// CHECK: %0 = tfl.add %arg0, %arg0 {fused_activation_function = "NONE", tac.device = "GPU", tac.inference_type = "FLOAT"} : tensor +// CHECK: return %0 : tensor +// CHECK: } + + + + + +// ----- + +module { +func.func @degenerateCase(%arg0: tensor<1xf32>) -> tensor<1xf32> { + %0 = "tfl.add"(%arg0, %arg0) {tac.device = "GPU", fused_activation_function = "RELU6", tac.inference_type = "FLOAT"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + func.return %0 : tensor<1xf32> +} +} + +// CHECK: func.func @degenerateCase(%arg0: tensor<1xf32>) -> tensor<1xf32> { +// CHECK: %0 = call @func_0_GPU_FLOAT(%arg0) {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} : (tensor<1xf32>) -> tensor<1xf32> +// CHECK: return %0 : tensor<1xf32> +// CHECK: } +// CHECK: func.func private @func_0_GPU_FLOAT(%arg0: tensor<1xf32>) -> tensor<1xf32> attributes {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} { +// CHECK: %0 = tfl.add %arg0, %arg0 {fused_activation_function = "RELU6", tac.device = "GPU", tac.inference_type = "FLOAT"} : tensor<1xf32> +// CHECK: return %0 : tensor<1xf32> +// CHECK: } + +// ----- + +module { +func.func @simpleTest(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>, %arg2: tensor<1xf32>, %arg3: tensor<1xf32>) -> tensor<2x1xf32> { + %0 = "tfl.add"(%arg0, %arg1) {tac.device = "GPU", fused_activation_function = "RELU6", tac.inference_type = "FLOAT"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + %1 = "tfl.mul"(%0, %arg2) {tac.device = "GPU", fused_activation_function = "RELU6", tac.inference_type = "FLOAT"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + %2 = "tfl.add"(%arg0, %arg3) {tac.device = "GPU", fused_activation_function = "RELU6", tac.inference_type = "FLOAT"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + %3 = "tfl.pack"(%1, %2) {tac.device = "CPU", tac.inference_type = "FLOAT", axis = 0 : i32, values_count = 2 : i32} : (tensor<1xf32>, tensor<1xf32>) -> tensor<2x1xf32> + func.return %3 : tensor<2x1xf32> +} +} + +// CHECK: func @simpleTest(%[[VAL_0:.*]]: tensor<1xf32>, %[[VAL_1:.*]]: tensor<1xf32>, %[[VAL_2:.*]]: tensor<1xf32>, %[[VAL_3:.*]]: tensor<1xf32>) -> tensor<2x1xf32> { +// CHECK: %[[VAL_4:.*]]:2 = call @func_0_GPU_FLOAT(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]], %[[VAL_3]]) {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} : (tensor<1xf32>, tensor<1xf32>, tensor<1xf32>, tensor<1xf32>) -> (tensor<1xf32>, tensor<1xf32>) +// CHECK: %[[VAL_5:.*]] = call @func_1_CPU_FLOAT(%[[VAL_4]]#0, %[[VAL_4]]#1) {tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "func_1"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<2x1xf32> +// CHECK: return %[[VAL_5]] : tensor<2x1xf32> +// CHECK: } + +// CHECK: func private @func_0_GPU_FLOAT(%[[VAL_0:.*]]: tensor<1xf32>, %[[VAL_1:.*]]: tensor<1xf32>, %[[VAL_2:.*]]: tensor<1xf32>, %[[VAL_4:.*]]: tensor<1xf32>) -> (tensor<1xf32>, tensor<1xf32>) attributes {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} { +// CHECK: %[[VAL_5:.*]] = tfl.add %[[VAL_0]], %[[VAL_1]] {fused_activation_function = "RELU6", tac.device = "GPU", tac.inference_type = "FLOAT"} : tensor<1xf32> +// CHECK: %[[VAL_6:.*]] = tfl.mul %[[VAL_5]], %[[VAL_2]] {fused_activation_function = "RELU6", tac.device = "GPU", tac.inference_type = "FLOAT"} : tensor<1xf32> +// CHECK: %[[VAL_7:.*]] = tfl.add %[[VAL_0]], %[[VAL_4]] {fused_activation_function = "RELU6", tac.device = "GPU", tac.inference_type = "FLOAT"} : tensor<1xf32> +// CHECK: return %[[VAL_6]], %[[VAL_7]] : tensor<1xf32>, tensor<1xf32> +// CHECK: } + +// CHECK: func private @func_1_CPU_FLOAT(%[[VAL_0:.*]]: tensor<1xf32>, %[[VAL_1:.*]]: tensor<1xf32>) -> tensor<2x1xf32> attributes {tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "func_1"} { +// CHECK: %[[VAL_2:.*]] = "tfl.pack"(%[[VAL_0]], %[[VAL_1]]) <{axis = 0 : i32, values_count = 2 : i32}> {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<2x1xf32> +// CHECK: return %[[VAL_2]] : tensor<2x1xf32> +// CHECK: } + +// ----- + +module { +func.func @constWeight(%arg0: tensor<256x32x32x3xf32>) -> tensor<256x30x30x16xf32> { + %0 = "tfl.pseudo_const"() {value = dense<1.000000e+00> : tensor<16x3x3x3xf32>} : () -> tensor<16x3x3x3xf32> + %1 = "tfl.pseudo_const"() {value = dense<1.000000e+00> : tensor<16xf32>} : () -> tensor<16xf32> + %2 = "tfl.conv_2d"(%arg0, %0, %1) {tac.device = "GPU", tac.inference_type = "FLOAT", dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32} : (tensor<256x32x32x3xf32>, tensor<16x3x3x3xf32>, tensor<16xf32>) -> tensor<256x30x30x16xf32> + %3 = "tfl.pseudo_const"() {value = dense<1.000000e+00> : tensor<16x3x3x16xf32>} : () -> tensor<16x3x3x16xf32> + %4 = "tfl.pseudo_const"() {value = dense<1.000000e+00> : tensor<16xf32>} : () -> tensor<16xf32> + %5 = "tfl.conv_2d"(%2, %3, %4) {tac.device = "GPU", tac.inference_type = "FLOAT", dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32} : (tensor<256x30x30x16xf32>, tensor<16x3x3x16xf32>, tensor<16xf32>) -> tensor<256x30x30x16xf32> + func.return %5 : tensor<256x30x30x16xf32> +} + +// CHECK: func @constWeight(%[[VAL_0:.*]]: tensor<256x32x32x3xf32>) -> tensor<256x30x30x16xf32> { +// CHECK-DAG: %[[VAL_1:.*]] = "tfl.pseudo_const"() <{value = dense<1.000000e+00> : tensor<16x3x3x3xf32>}> : () -> tensor<16x3x3x3xf32> +// CHECK-DAG: %[[VAL_2:.*]] = "tfl.pseudo_const"() <{value = dense<1.000000e+00> : tensor<16xf32>}> : () -> tensor<16xf32> +// CHECK-DAG: %[[VAL_3:.*]] = "tfl.pseudo_const"() <{value = dense<1.000000e+00> : tensor<16x3x3x16xf32>}> : () -> tensor<16x3x3x16xf32> +// CHECK-DAG: %[[VAL_4:.*]] = "tfl.pseudo_const"() <{value = dense<1.000000e+00> : tensor<16xf32>}> : () -> tensor<16xf32> +// CHECK: %[[VAL_5:.*]] = call @func_0_GPU_FLOAT(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]], %[[VAL_3]], %[[VAL_4]]) {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} : (tensor<256x32x32x3xf32>, tensor<16x3x3x3xf32>, tensor<16xf32>, tensor<16x3x3x16xf32>, tensor<16xf32>) -> tensor<256x30x30x16xf32> +// CHECK: return %[[VAL_5]] : tensor<256x30x30x16xf32> +// CHECK: } + +// CHECK: func private @func_0_GPU_FLOAT(%[[VAL_0:.*]]: tensor<256x32x32x3xf32>, %[[VAL_1:.*]]: tensor<16x3x3x3xf32>, %[[VAL_2:.*]]: tensor<16xf32>, %[[VAL_3:.*]]: tensor<16x3x3x16xf32>, %[[VAL_4:.*]]: tensor<16xf32>) -> tensor<256x30x30x16xf32> attributes {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} { +// CHECK: %[[VAL_5:.*]] = "tfl.conv_2d"(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]]) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32}> {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<256x32x32x3xf32>, tensor<16x3x3x3xf32>, tensor<16xf32>) -> tensor<256x30x30x16xf32> +// CHECK: %[[VAL_6:.*]] = "tfl.conv_2d"(%[[VAL_5]], %[[VAL_3]], %[[VAL_4]]) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32}> {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<256x30x30x16xf32>, tensor<16x3x3x16xf32>, tensor<16xf32>) -> tensor<256x30x30x16xf32> +// CHECK: return %[[VAL_6]] : tensor<256x30x30x16xf32> +// CHECK: } + +} + +// ----- + +module { +func.func @norm1(%arg0: tensor<1x128x128xf32>) -> tensor<1x128x128xf32> { + %0 = "tfl.pseudo_const"() {value = dense<1.000000e+00> : tensor<128xf32>} : () -> tensor<128xf32> + %1 = "tfl.add"(%arg0, %0) {tac.device = "GPU", tac.inference_type = "FLOAT", fused_activation_function = "NONE"} : (tensor<1x128x128xf32>, tensor<128xf32>) -> tensor<1x128x128xf32> + %2 = "tfl.pseudo_const"() {value = dense<[128, 128]> : tensor<2xi32>} : () -> tensor<2xi32> + %3 = "tfl.reshape"(%1, %2) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<1x128x128xf32>, tensor<2xi32>) -> tensor<128x128xf32> + %4 = "tfl.relu"(%3) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<128x128xf32>) -> tensor<128x128xf32> + %5 = "tfl.pseudo_const"() {value = dense<[1, 128, 128]> : tensor<3xi32>} : () -> tensor<3xi32> + %6 = "tfl.reshape"(%4, %5) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<128x128xf32>, tensor<3xi32>) -> tensor<1x128x128xf32> + %7 = "tfl.add"(%1, %6) {tac.device = "GPU", tac.inference_type = "FLOAT", fused_activation_function = "NONE"} : (tensor<1x128x128xf32>, tensor<1x128x128xf32>) -> tensor<1x128x128xf32> + func.return %7 : tensor<1x128x128xf32> +} + +// CHECK: func @norm1(%[[VAL_0:.*]]: tensor<1x128x128xf32>) -> tensor<1x128x128xf32> { +// CHECK-DAG: %[[VAL_1:.*]] = "tfl.pseudo_const"() <{value = dense<1.000000e+00> : tensor<128xf32>}> : () -> tensor<128xf32> +// CHECK-DAG: %[[VAL_2:.*]] = "tfl.pseudo_const"() <{value = dense<128> : tensor<2xi32>}> : () -> tensor<2xi32> +// CHECK-DAG: %[[VAL_3:.*]] = "tfl.pseudo_const"() <{value = dense<[1, 128, 128]> : tensor<3xi32>}> : () -> tensor<3xi32> +// CHECK: %[[VAL_4:.*]] = call @func_0_GPU_FLOAT(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]], %[[VAL_3]]) {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} : (tensor<1x128x128xf32>, tensor<128xf32>, tensor<2xi32>, tensor<3xi32>) -> tensor<1x128x128xf32> +// CHECK: return %[[VAL_4]] : tensor<1x128x128xf32> +// CHECK: } + +// CHECK: func private @func_0_GPU_FLOAT(%[[VAL_0:.*]]: tensor<1x128x128xf32>, %[[VAL_1:.*]]: tensor<128xf32>, %[[VAL_2:.*]]: tensor<2xi32>, %[[VAL_3:.*]]: tensor<3xi32>) -> tensor<1x128x128xf32> attributes {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} { +// CHECK: %[[VAL_4:.*]] = tfl.add(%[[VAL_0]], %[[VAL_1]]) <{fused_activation_function = "NONE"}> {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<1x128x128xf32>, tensor<128xf32>) -> tensor<1x128x128xf32> +// CHECK: %[[VAL_5:.*]] = "tfl.reshape"(%[[VAL_4]], %[[VAL_2]]) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<1x128x128xf32>, tensor<2xi32>) -> tensor<128x128xf32> +// CHECK: %[[VAL_6:.*]] = "tfl.relu"(%[[VAL_5]]) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<128x128xf32>) -> tensor<128x128xf32> +// CHECK: %[[VAL_7:.*]] = "tfl.reshape"(%[[VAL_6]], %[[VAL_3]]) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<128x128xf32>, tensor<3xi32>) -> tensor<1x128x128xf32> +// CHECK: %[[VAL_8:.*]] = tfl.add %[[VAL_4]], %[[VAL_7]] {fused_activation_function = "NONE", tac.device = "GPU", tac.inference_type = "FLOAT"} : tensor<1x128x128xf32> +// CHECK: return %[[VAL_8]] : tensor<1x128x128xf32> +// CHECK: } + +} + +// ----- + +module { +func.func @norm2(%arg0: tensor<1x128x128xf32>) -> tensor<1x128x128xf32> { + %0 = "tfl.pseudo_const"() {value = dense<1.000000e+00> : tensor<128xf32>} : () -> tensor<128xf32> + %1 = "tfl.add"(%arg0, %0) {tac.device = "GPU", tac.inference_type = "FLOAT", fused_activation_function = "NONE"} : (tensor<1x128x128xf32>, tensor<128xf32>) -> tensor<1x128x128xf32> + %2 = "tfl.pseudo_const"() {value = dense<[128, 128]> : tensor<2xi32>} : () -> tensor<2xi32> + %3 = "tfl.reshape"(%1, %2) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<1x128x128xf32>, tensor<2xi32>) -> tensor<128x128xf32> + %4 = "tfl.relu"(%3) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<128x128xf32>) -> tensor<128x128xf32> + %5 = "tfl.pseudo_const"() {value = dense<1.000000e+00> : tensor<128x128xf32>} : () -> tensor<128x128xf32> + %6 = "tfl.pseudo_const"() {value = dense<1.000000e+00> : tensor<128xf32>} : () -> tensor<128xf32> + %7 = "tfl.fully_connected"(%4, %5, %6) {tac.device = "CPU", tac.inference_type = "FLOAT", fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"} : (tensor<128x128xf32>, tensor<128x128xf32>, tensor<128xf32>) -> tensor<128x128xf32> + %8 = "tfl.pseudo_const"() {value = dense<[1, 128, 128]> : tensor<3xi32>} : () -> tensor<3xi32> + %9 = "tfl.reshape"(%7, %8) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<128x128xf32>, tensor<3xi32>) -> tensor<1x128x128xf32> + %10 = "tfl.add"(%1, %9) {tac.device = "GPU", tac.inference_type = "FLOAT", fused_activation_function = "NONE"} : (tensor<1x128x128xf32>, tensor<1x128x128xf32>) -> tensor<1x128x128xf32> + func.return %10 : tensor<1x128x128xf32> +} +} + +// CHECK: func @norm2(%[[VAL_0:.*]]: tensor<1x128x128xf32>) -> tensor<1x128x128xf32> { +// CHECK-DAG: %[[VAL_1:.*]] = "tfl.pseudo_const"() <{value = dense<1.000000e+00> : tensor<128xf32>}> : () -> tensor<128xf32> +// CHECK-DAG: %[[VAL_2:.*]] = "tfl.pseudo_const"() <{value = dense<128> : tensor<2xi32>}> : () -> tensor<2xi32> +// CHECK: %[[VAL_3:.*]]:2 = call @func_0_GPU_FLOAT(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]]) {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} : (tensor<1x128x128xf32>, tensor<128xf32>, tensor<2xi32>) -> (tensor<1x128x128xf32>, tensor<128x128xf32>) +// CHECK-DAG: %[[VAL_4:.*]] = "tfl.pseudo_const"() <{value = dense<1.000000e+00> : tensor<128x128xf32>}> : () -> tensor<128x128xf32> +// CHECK-DAG: %[[VAL_5:.*]] = "tfl.pseudo_const"() <{value = dense<1.000000e+00> : tensor<128xf32>}> : () -> tensor<128xf32> +// CHECK: %[[VAL_6:.*]] = call @func_2_CPU_FLOAT(%[[VAL_3]]#1, %[[VAL_4]], %[[VAL_5]]) {tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "func_2"} : (tensor<128x128xf32>, tensor<128x128xf32>, tensor<128xf32>) -> tensor<128x128xf32> +// CHECK: %[[VAL_7:.*]] = "tfl.pseudo_const"() <{value = dense<[1, 128, 128]> : tensor<3xi32>}> : () -> tensor<3xi32> +// CHECK: %[[VAL_8:.*]] = call @func_1_GPU_FLOAT(%[[VAL_6]], %[[VAL_7]], %[[VAL_3]]#0) {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_1"} : (tensor<128x128xf32>, tensor<3xi32>, tensor<1x128x128xf32>) -> tensor<1x128x128xf32> +// CHECK: return %[[VAL_8]] : tensor<1x128x128xf32> +// CHECK: } + +// CHECK: func.func private @func_0_GPU_FLOAT(%[[VAL_0:.*]]: tensor<1x128x128xf32>, %[[VAL_1:.*]]: tensor<128xf32>, %[[VAL_2:.*]]: tensor<2xi32>) -> (tensor<1x128x128xf32>, tensor<128x128xf32>) attributes {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} { +// CHECK: %[[VAL_3:.*]] = tfl.add(%[[VAL_0]], %[[VAL_1]]) <{fused_activation_function = "NONE"}> {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<1x128x128xf32>, tensor<128xf32>) -> tensor<1x128x128xf32> +// CHECK: %[[VAL_4:.*]] = "tfl.reshape"(%[[VAL_3]], %[[VAL_2]]) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<1x128x128xf32>, tensor<2xi32>) -> tensor<128x128xf32> +// CHECK: %[[VAL_5:.*]] = "tfl.relu"(%[[VAL_4]]) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<128x128xf32>) -> tensor<128x128xf32> +// CHECK: return %[[VAL_3]], %[[VAL_5]] : tensor<1x128x128xf32>, tensor<128x128xf32> +// CHECK: } + +// CHECK: func.func private @func_1_GPU_FLOAT(%[[VAL_0:.*]]: tensor<128x128xf32>, %[[VAL_1:.*]]: tensor<3xi32>, %[[VAL_2:.*]]: tensor<1x128x128xf32>) -> tensor<1x128x128xf32> attributes {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_1"} { +// CHECK: %[[VAL_3:.*]] = "tfl.reshape"(%[[VAL_0]], %[[VAL_1]]) {tac.device = "GPU", tac.inference_type = "FLOAT"} : (tensor<128x128xf32>, tensor<3xi32>) -> tensor<1x128x128xf32> +// CHECK: %[[VAL_4:.*]] = tfl.add %[[VAL_2]], %[[VAL_3]] {fused_activation_function = "NONE", tac.device = "GPU", tac.inference_type = "FLOAT"} : tensor<1x128x128xf32> +// CHECK: return %[[VAL_4]] : tensor<1x128x128xf32> +// CHECK: } + +// CHECK: func.func private @func_2_CPU_FLOAT(%[[VAL_0:.*]]: tensor<128x128xf32>, %[[VAL_1:.*]]: tensor<128x128xf32>, %[[VAL_2:.*]]: tensor<128xf32>) -> tensor<128x128xf32> attributes {tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "func_2"} { +// CHECK: %[[VAL_3:.*]] = "tfl.fully_connected"(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]]) <{fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"}> {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<128x128xf32>, tensor<128x128xf32>, tensor<128xf32>) -> tensor<128x128xf32> +// CHECK: return %[[VAL_3]] : tensor<128x128xf32> +// CHECK: } + +// ----- + +module { + +func.func @quantizedOpOnly(%arg0: tensor<1x!quant.uniform>, %arg1: tensor<1x!quant.uniform>) -> tensor<2x1x!quant.uniform> { + %0 = "tfl.pseudo_qconst"() {qtype = tensor<1x!quant.uniform>, value = dense<127> : tensor<1xi8>} : () -> tensor<1x!quant.uniform> + %1 = "tfl.pseudo_qconst"() {qtype = tensor<1x!quant.uniform>, value = dense<127> : tensor<1xi8>} : () -> tensor<1x!quant.uniform> + %2 = "tfl.mul"(%arg0, %0) {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8", fused_activation_function = "NONE"} : (tensor<1x!quant.uniform>, tensor<1x!quant.uniform>) -> tensor<1x!quant.uniform> + %3 = "tfl.add"(%2, %1) {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8", fused_activation_function = "NONE"} : (tensor<1x!quant.uniform>, tensor<1x!quant.uniform>) -> tensor<1x!quant.uniform> + %4 = "tfl.add"(%arg1, %0) {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8", fused_activation_function = "NONE"} : (tensor<1x!quant.uniform>, tensor<1x!quant.uniform>) -> tensor<1x!quant.uniform> + %5 = "tfl.pack"(%3, %4) {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8", axis = 0 : i32, values_count = 2 : i32} : (tensor<1x!quant.uniform>, tensor<1x!quant.uniform>) -> tensor<2x1x!quant.uniform> + func.return %5: tensor<2x1x!quant.uniform> +} + +// CHECK: func @quantizedOpOnly(%[[VAL_0:.*]]: tensor<1x!quant.uniform>, %[[VAL_1:.*]]: tensor<1x!quant.uniform>) -> tensor<2x1x!quant.uniform> { +// CHECK: %[[VAL_2:.*]] = "tfl.pseudo_qconst"() <{qtype = tensor<1x!quant.uniform>, value = dense<127> : tensor<1xi8>}> : () -> tensor<1x!quant.uniform> +// CHECK: %[[VAL_3:.*]] = "tfl.pseudo_qconst"() <{qtype = tensor<1x!quant.uniform>, value = dense<127> : tensor<1xi8>}> : () -> tensor<1x!quant.uniform> +// CHECK: %[[VAL_4:.*]] = call @func_0_CPU_QUANTIZED_INT8(%[[VAL_0]], %[[VAL_2]], %[[VAL_3]], %[[VAL_1]]) {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8", tac.interface_name = "func_0"} : (tensor<1x!quant.uniform>, tensor<1x!quant.uniform>, tensor<1x!quant.uniform>, tensor<1x!quant.uniform>) -> tensor<2x1x!quant.uniform> +// CHECK: return %[[VAL_4]] : tensor<2x1x!quant.uniform> +// CHECK: } + +// CHECK: func private @func_0_CPU_QUANTIZED_INT8(%[[VAL_0:.*]]: tensor<1x!quant.uniform>, %[[VAL_1:.*]]: tensor<1x!quant.uniform>, %[[VAL_2:.*]]: tensor<1x!quant.uniform>, %[[VAL_3:.*]]: tensor<1x!quant.uniform>) -> tensor<2x1x!quant.uniform> attributes {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8", tac.interface_name = "func_0"} { +// CHECK: %[[VAL_5:.*]] = tfl.mul %[[VAL_0]], %[[VAL_1]] {fused_activation_function = "NONE", tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8"} : tensor<1x!quant.uniform> +// CHECK: %[[VAL_6:.*]] = tfl.add %[[VAL_5]], %[[VAL_2]] {fused_activation_function = "NONE", tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8"} : tensor<1x!quant.uniform> +// CHECK: %[[VAL_7:.*]] = tfl.add %[[VAL_3]], %[[VAL_1]] {fused_activation_function = "NONE", tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8"} : tensor<1x!quant.uniform> +// CHECK: %[[VAL_8:.*]] = "tfl.pack"(%[[VAL_6]], %[[VAL_7]]) <{axis = 0 : i32, values_count = 2 : i32}> {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8"} : (tensor<1x!quant.uniform>, tensor<1x!quant.uniform>) -> tensor<2x1x!quant.uniform> +// CHECK: return %[[VAL_8]] : tensor<2x1x!quant.uniform> +// CHECK: } + +} + +// ----- + +module { +func.func @quantizationWithFloat(%arg0: tensor<1x1x384x!quant.uniform>, %arg1: tensor<1x1x384x!quant.uniform>) -> tensor<1x384x384x!quant.uniform> { + %0 = "tfl.pseudo_qconst"() {qtype = tensor<1x384x1x!quant.uniform>, value = dense<127> : tensor<1x384x1xi8>} : () -> tensor<1x384x1x!quant.uniform> + %1 = "tfl.mul"(%arg0, %0) {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8", fused_activation_function = "NONE"} : (tensor<1x1x384x!quant.uniform>, tensor<1x384x1x!quant.uniform>) -> tensor<1x384x384x!quant.uniform> + %2 = "tfl.dequantize"(%1) : (tensor<1x384x384x!quant.uniform>) -> tensor<1x384x384xf32> + %3 = "tfl.pseudo_const"() {value = dense<1.000000e+00> : tensor<1x1x384xf32>} : () -> tensor<1x384x384xf32> + %4 = "tfl.add"(%2, %3) {tac.device = "GPU", tac.inference_type = "FLOAT", fused_activation_function = "NONE"} : (tensor<1x384x384xf32>, tensor<1x384x384xf32>) -> tensor<1x384x384xf32> + %5 = "tfl.quantize"(%4) {qtype = tensor<1x384x1x!quant.uniform>} : (tensor<1x384x384xf32>) -> tensor<1x384x384x!quant.uniform> + %6 = "tfl.mul"(%arg1, %5) {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8", fused_activation_function = "NONE"} : (tensor<1x1x384x!quant.uniform>, tensor<1x384x384x!quant.uniform>) -> tensor<1x384x384x!quant.uniform> + func.return %6: tensor<1x384x384x!quant.uniform> +} +} + +// CHECK: func @quantizationWithFloat(%[[VAL_0:.*]]: tensor<1x1x384x!quant.uniform>, %[[VAL_1:.*]]: tensor<1x1x384x!quant.uniform>) -> tensor<1x384x384x!quant.uniform> { +// CHECK: %[[VAL_2:.*]] = "tfl.pseudo_qconst"() <{qtype = tensor<1x384x1x!quant.uniform>, value = dense<127> : tensor<1x384x1xi8>}> : () -> tensor<1x384x1x!quant.uniform> +// CHECK: %[[VAL_3:.*]] = call @func_1_CPU_QUANTIZED_INT8(%[[VAL_0]], %[[VAL_2]]) {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8", tac.interface_name = "func_1"} : (tensor<1x1x384x!quant.uniform>, tensor<1x384x1x!quant.uniform>) -> tensor<1x384x384x!quant.uniform> +// CHECK: %[[VAL_4:.*]] = "tfl.dequantize"(%[[VAL_3]]) : (tensor<1x384x384x!quant.uniform>) -> tensor<1x384x384xf32> +// CHECK: %[[VAL_5:.*]] = "tfl.pseudo_const"() <{value = dense<1.000000e+00> : tensor<1x1x384xf32>}> : () -> tensor<1x384x384xf32> +// CHECK: %[[VAL_6:.*]] = call @func_0_GPU_FLOAT(%[[VAL_4]], %[[VAL_5]]) {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} : (tensor<1x384x384xf32>, tensor<1x384x384xf32>) -> tensor<1x384x384xf32> +// CHECK: %[[VAL_7:.*]] = "tfl.quantize"(%[[VAL_6]]) <{qtype = tensor<1x384x1x!quant.uniform>}> : (tensor<1x384x384xf32>) -> tensor<1x384x384x!quant.uniform> +// CHECK: %[[VAL_8:.*]] = call @func_2_CPU_QUANTIZED_INT8(%[[VAL_1]], %[[VAL_7]]) {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8", tac.interface_name = "func_2"} : (tensor<1x1x384x!quant.uniform>, tensor<1x384x384x!quant.uniform>) -> tensor<1x384x384x!quant.uniform> +// CHECK: return %[[VAL_8]] : tensor<1x384x384x!quant.uniform> +// CHECK: } + +// CHECK: func private @func_0_GPU_FLOAT(%[[VAL_0:.*]]: tensor<1x384x384xf32>, %[[VAL_1:.*]]: tensor<1x384x384xf32>) -> tensor<1x384x384xf32> attributes {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} { +// CHECK: %[[VAL_2:.*]] = tfl.add %[[VAL_0]], %[[VAL_1]] {fused_activation_function = "NONE", tac.device = "GPU", tac.inference_type = "FLOAT"} : tensor<1x384x384xf32> +// CHECK: return %[[VAL_2]] : tensor<1x384x384xf32> +// CHECK: } + +// CHECK: func private @func_1_CPU_QUANTIZED_INT8(%[[VAL_0:.*]]: tensor<1x1x384x!quant.uniform>, %[[VAL_1:.*]]: tensor<1x384x1x!quant.uniform>) -> tensor<1x384x384x!quant.uniform> attributes {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8", tac.interface_name = "func_1"} { +// CHECK: %[[VAL_2:.*]] = tfl.mul(%[[VAL_0]], %[[VAL_1]]) <{fused_activation_function = "NONE"}> {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8"} : (tensor<1x1x384x!quant.uniform>, tensor<1x384x1x!quant.uniform>) -> tensor<1x384x384x!quant.uniform> +// CHECK: return %[[VAL_2]] : tensor<1x384x384x!quant.uniform> +// CHECK: } + +// CHECK: func private @func_2_CPU_QUANTIZED_INT8(%[[VAL_0:.*]]: tensor<1x1x384x!quant.uniform>, %[[VAL_1:.*]]: tensor<1x384x384x!quant.uniform>) -> tensor<1x384x384x!quant.uniform> attributes {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8", tac.interface_name = "func_2"} { +// CHECK: %[[VAL_2:.*]] = tfl.mul(%[[VAL_0]], %[[VAL_1]]) <{fused_activation_function = "NONE"}> {tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8"} : (tensor<1x1x384x!quant.uniform>, tensor<1x384x384x!quant.uniform>) -> tensor<1x384x384x!quant.uniform> +// CHECK: return %[[VAL_2]] : tensor<1x384x384x!quant.uniform> +// CHECK: } + +// ----- + +func.func @cond_false_72730(%arg0: tensor, %arg1: tensor, %arg2: tensor, %arg3: tensor, %arg4: tensor, %arg5: tensor, %arg6: tensor) -> (tensor, tensor) { + %cst = arith.constant dense<[1, 0]> : tensor<2xi32> + %cst_0 = arith.constant dense<0> : tensor + %cst_1 = arith.constant dense<-1> : tensor<1xi32> + %cst_2 = arith.constant dense<1> : tensor<1xi32> + %cst_3 = arith.constant dense<0> : tensor<1xi32> + %0 = "tfl.shape"(%arg2) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor) -> tensor<2xi32> + %1 = "tfl.strided_slice"(%0, %cst_3, %cst_2, %cst_2) {begin_mask = 0 : i32, ellipsis_mask = 0 : i32, end_mask = 0 : i32, new_axis_mask = 0 : i32, shrink_axis_mask = 1 : i32, offset = false, tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %2 = "tfl.custom"(%cst_1, %1) {custom_code = "FlexTensorListReserve", custom_option = #tfl, tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<1xi32>, tensor) -> tensor>> + %3 = "tfl.custom"(%cst_1, %1) {custom_code = "FlexTensorListReserve", custom_option = #tfl, tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<1xi32>, tensor) -> tensor>> + %4:8 = "tfl.while"(%cst_0, %cst_0, %arg5, %arg6, %2, %2, %3, %3) ({ + ^bb0(%arg7: tensor, %arg8: tensor, %arg9: tensor, %arg10: tensor, %arg11: tensor>>, %arg12: tensor>>, %arg13: tensor>>, %arg14: tensor>>): + %9 = tfl.less(%arg8, %1) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor) -> tensor + %10 = tfl.less(%arg7, %1) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor) -> tensor + %11 = tfl.logical_and %10, %9 {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : tensor + "tfl.yield"(%11) : (tensor) -> () + }, { + ^bb0(%arg7: tensor, %arg8: tensor, %arg9: tensor, %arg10: tensor, %arg11: tensor>>, %arg12: tensor>>, %arg13: tensor>>, %arg14: tensor>>): + %cst_4 = arith.constant dense<[0, 0, 1, 1, 1]> : tensor<5xi32> + %cst_5 = arith.constant dense<[0, 1, 0, 1, 1]> : tensor<5xi32> + %cst_6 = arith.constant dense<2> : tensor + %cst_7 = arith.constant dense<"*"> : tensor + %cst_8 = arith.constant dense<-1> : tensor<1xi32> + %cst_9 = arith.constant dense<-1> : tensor + %cst_10 = arith.constant dense<2> : tensor<1xi32> + %cst_11 = arith.constant dense<1> : tensor + %cst_12 = arith.constant dense<0> : tensor + %cst_13 = arith.constant dense<1> : tensor<1xi32> + %cst_14 = arith.constant dense<0> : tensor<1xi32> + %9 = "tfl.shape"(%arg1) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor) -> tensor<2xi32> + %10 = "tfl.strided_slice"(%9, %cst_14, %cst_13, %cst_13) {begin_mask = 0 : i32, ellipsis_mask = 0 : i32, end_mask = 0 : i32, new_axis_mask = 0 : i32, shrink_axis_mask = 1 : i32, offset = false, tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %11 = "tfl.range"(%cst_12, %10, %cst_11) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor, tensor) -> tensor + %12 = "tfl.pack"(%10, %cst_11) {axis = 0 : i32, tac.device = "DARWINN", tac.inference_type = "FLOAT", values_count = 2 : i32} : (tensor, tensor) -> tensor<2xi32> + %13 = "tfl.strided_slice"(%9, %cst_13, %cst_10, %cst_13) {begin_mask = 0 : i32, ellipsis_mask = 0 : i32, end_mask = 0 : i32, new_axis_mask = 0 : i32, shrink_axis_mask = 1 : i32, offset = false, tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %14 = tfl.mul(%11, %13) {fused_activation_function = "NONE", tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor) -> tensor + %15 = "tfl.reshape"(%14, %12) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor<2xi32>) -> tensor + %16 = "tfl.strided_slice"(%9, %cst_14, %cst_10, %cst_13) {begin_mask = 1 : i32, ellipsis_mask = 0 : i32, end_mask = 0 : i32, new_axis_mask = 0 : i32, shrink_axis_mask = 0 : i32, offset = false, tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<2xi32> + %17 = "tfl.reduce_prod"(%16, %cst_14) {keep_dims = true, tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<2xi32>, tensor<1xi32>) -> tensor<1xi32> + %18 = "tfl.reshape"(%arg1, %17) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor<1xi32>) -> tensor + %19 = "tfl.shape"(%arg0) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor) -> tensor<2xi32> + %20 = "tfl.strided_slice"(%19, %cst_14, %cst_13, %cst_13) {begin_mask = 0 : i32, ellipsis_mask = 0 : i32, end_mask = 0 : i32, new_axis_mask = 0 : i32, shrink_axis_mask = 1 : i32, offset = false, tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %21 = "tfl.range"(%cst_12, %20, %cst_11) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor, tensor) -> tensor + %22 = "tfl.pack"(%20, %cst_11) {axis = 0 : i32, tac.device = "DARWINN", tac.inference_type = "FLOAT", values_count = 2 : i32} : (tensor, tensor) -> tensor<2xi32> + %23 = "tfl.strided_slice"(%19, %cst_13, %cst_10, %cst_13) {begin_mask = 0 : i32, ellipsis_mask = 0 : i32, end_mask = 0 : i32, new_axis_mask = 0 : i32, shrink_axis_mask = 1 : i32, offset = false, tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %24 = tfl.mul(%21, %23) {fused_activation_function = "NONE", tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor) -> tensor + %25 = "tfl.reshape"(%24, %22) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor<2xi32>) -> tensor + %26 = "tfl.strided_slice"(%19, %cst_14, %cst_10, %cst_13) {begin_mask = 1 : i32, ellipsis_mask = 0 : i32, end_mask = 0 : i32, new_axis_mask = 0 : i32, shrink_axis_mask = 0 : i32, offset = false, tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<2xi32> + %27 = "tfl.reduce_prod"(%26, %cst_14) {keep_dims = true, tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<2xi32>, tensor<1xi32>) -> tensor<1xi32> + %28 = "tfl.reshape"(%arg0, %27) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor<1xi32>) -> tensor + %29 = tfl.add %arg8, %cst_11 {fused_activation_function = "NONE", tac.device = "DARWINN", tac.inference_type = "FLOAT"} : tensor + %30 = "tfl.expand_dims"(%arg9, %cst_9) {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor, tensor) -> tensor + %31 = tfl.add %30, %25 {fused_activation_function = "NONE", tac.device = "DARWINN", tac.inference_type = "FLOAT"} : tensor + %32 = "tfl.reshape"(%31, %cst_8) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor<1xi32>) -> tensor + %33 = "tfl.gather"(%28, %32) <{axis = 0 : i32, batch_dims = 0 : i32}> {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor) -> tensor + %34 = "tfl.reshape"(%33, %cst_8) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor<1xi32>) -> tensor + %35 = "tfl.shape"(%34) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor) -> tensor<1xi32> + %36 = "tfl.fill"(%35, %cst_7) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor<1xi32>, tensor) -> tensor + %37 = "tfl.expand_dims"(%arg10, %cst_9) {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor, tensor) -> tensor + %38 = tfl.add %37, %15 {fused_activation_function = "NONE", tac.device = "DARWINN", tac.inference_type = "FLOAT"} : tensor + %39 = "tfl.reshape"(%38, %cst_8) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor<1xi32>) -> tensor + %40 = "tfl.gather"(%18, %39) <{axis = 0 : i32, batch_dims = 0 : i32}> {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor) -> tensor + %41 = "tfl.reshape"(%40, %cst_8) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor<1xi32>) -> tensor + %42 = "tfl.shape"(%41) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor) -> tensor<1xi32> + %43 = "tfl.fill"(%42, %cst_7) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor<1xi32>, tensor) -> tensor + %44 = "tfl.gather"(%arg2, %arg8) <{axis = 0 : i32, batch_dims = 0 : i32}> {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor) -> tensor + %45 = "tfl.equal"(%44, %cst_6) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor) -> tensor + %46 = "tfl.custom"(%45, %36, %34) {custom_code = "FlexSelect", custom_option = #tfl, tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor, tensor, tensor) -> tensor + %47 = "tfl.custom"(%arg11, %arg8, %46) {custom_code = "FlexTensorListSetItem", custom_option = #tfl, tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor>>, tensor, tensor) -> tensor>> + %48 = "tfl.equal"(%44, %cst_11) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor) -> tensor + %49 = "tfl.custom"(%48, %43, %41) {custom_code = "FlexSelect", custom_option = #tfl, tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor, tensor, tensor) -> tensor + %50 = "tfl.custom"(%arg12, %arg8, %49) {custom_code = "FlexTensorListSetItem", custom_option = #tfl, tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor>>, tensor, tensor) -> tensor>> + %51 = "tfl.gather"(%cst_5, %44) <{axis = 0 : i32, batch_dims = 0 : i32}> {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor<5xi32>, tensor) -> tensor + %52 = tfl.add %arg9, %51 {fused_activation_function = "NONE", tac.device = "DARWINN", tac.inference_type = "FLOAT"} : tensor + %53 = "tfl.custom"(%arg13, %arg8, %52) {custom_code = "FlexTensorListSetItem", custom_option = #tfl, tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor>>, tensor, tensor) -> tensor>> + %54 = "tfl.gather"(%cst_4, %44) <{axis = 0 : i32, batch_dims = 0 : i32}> {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor<5xi32>, tensor) -> tensor + %55 = tfl.add %arg10, %54 {fused_activation_function = "NONE", tac.device = "DARWINN", tac.inference_type = "FLOAT"} : tensor + %56 = "tfl.custom"(%arg14, %arg8, %55) {custom_code = "FlexTensorListSetItem", custom_option = #tfl, tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor>>, tensor, tensor) -> tensor>> + %57 = tfl.add %arg7, %cst_11 {fused_activation_function = "NONE", tac.device = "DARWINN", tac.inference_type = "FLOAT"} : tensor + "tfl.yield"(%57, %29, %52, %55, %47, %50, %53, %56) : (tensor, tensor, tensor, tensor, tensor>>, tensor>>, tensor>>, tensor>>) -> () + }) {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor, tensor, tensor, tensor, tensor>>, tensor>>, tensor>>, tensor>>) -> (tensor, tensor, tensor, tensor, tensor>>, tensor>>, tensor>>, tensor>>) + %5 = "tfl.custom"(%4#4, %cst_1) {custom_code = "FlexTensorListStack", custom_option = #tfl, tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor>>, tensor<1xi32>) -> tensor + %6 = "tfl.custom"(%5, %cst) {custom_code = "FlexTranspose", custom_option = #tfl, tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor, tensor<2xi32>) -> tensor + %7 = "tfl.custom"(%4#5, %cst_1) {custom_code = "FlexTensorListStack", custom_option = #tfl, tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor>>, tensor<1xi32>) -> tensor + %8 = "tfl.custom"(%7, %cst) {custom_code = "FlexTranspose", custom_option = #tfl, tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor, tensor<2xi32>) -> tensor + return %6, %8 : tensor, tensor + } + +// CHECK: func.func @cond_false_72730(%arg0: tensor, %arg1: tensor, %arg2: tensor, %arg3: tensor, %arg4: tensor, %arg5: tensor, %arg6: tensor) -> (tensor, tensor) { +// CHECK: %cst = arith.constant dense<[1, 0]> : tensor<2xi32> +// CHECK: %cst_0 = arith.constant dense<0> : tensor +// CHECK: %cst_1 = arith.constant dense<-1> : tensor<1xi32> +// CHECK: %cst_2 = arith.constant dense<1> : tensor<1xi32> +// CHECK: %cst_3 = arith.constant dense<0> : tensor<1xi32> +// CHECK: %0 = call @func_0_DARWINN_FLOAT(%arg2, %cst_3, %cst_2) {tac.device = "DARWINN", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} : (tensor, tensor<1xi32>, tensor<1xi32>) -> tensor +// CHECK: %1:2 = call @func_1_CPU_FLOAT(%cst_1, %0, %cst_0, %arg5, %arg6, %arg1, %arg0, %arg2, %cst) {tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "func_1"} : (tensor<1xi32>, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor<2xi32>) -> (tensor, tensor) +// CHECK: return %1#0, %1#1 : tensor, tensor +// CHECK: } +// CHECK: func.func private @func_0_DARWINN_FLOAT(%arg0: tensor, %arg1: tensor<1xi32>, %arg2: tensor<1xi32>) -> tensor attributes {tac.device = "DARWINN", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} { +// CHECK: %0 = "tfl.shape"(%arg0) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor) -> tensor<2xi32> +// CHECK: %1 = "tfl.strided_slice"(%0, %arg1, %arg2, %arg2) <{begin_mask = 0 : i32, ellipsis_mask = 0 : i32, end_mask = 0 : i32, new_axis_mask = 0 : i32, offset = false, shrink_axis_mask = 1 : i32}> {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor +// CHECK: return %1 : tensor +// CHECK: } +// CHECK: func.func private @func_1_CPU_FLOAT(%arg0: tensor<1xi32>, %arg1: tensor, %arg2: tensor, %arg3: tensor, %arg4: tensor, %arg5: tensor, %arg6: tensor, %arg7: tensor, %arg8: tensor<2xi32>) -> (tensor, tensor) attributes {tac.device = "CPU", tac.inference_type = "FLOAT", tac.interface_name = "func_1"} { +// CHECK: %0 = "tfl.custom"(%arg0, %arg1) <{custom_code = "FlexTensorListReserve", custom_option = #tfl}> {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<1xi32>, tensor) -> tensor>> +// CHECK: %1 = "tfl.custom"(%arg0, %arg1) <{custom_code = "FlexTensorListReserve", custom_option = #tfl}> {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<1xi32>, tensor) -> tensor>> +// CHECK: %2:8 = "tfl.while"(%arg2, %arg2, %arg3, %arg4, %0, %0, %1, %1) ({ +// CHECK: ^bb0(%arg9: tensor, %arg10: tensor, %arg11: tensor, %arg12: tensor, %arg13: tensor>>, %arg14: tensor>>, %arg15: tensor>>, %arg16: tensor>>): +// CHECK: %7 = func.call @func_2_DARWINN_FLOAT(%arg10, %arg1, %arg9) {tac.device = "DARWINN", tac.inference_type = "FLOAT", tac.interface_name = "func_2"} : (tensor, tensor, tensor) -> tensor +// CHECK: "tfl.yield"(%7) : (tensor) -> () +// CHECK: }, { +// CHECK: ^bb0(%arg9: tensor, %arg10: tensor, %arg11: tensor, %arg12: tensor, %arg13: tensor>>, %arg14: tensor>>, %arg15: tensor>>, %arg16: tensor>>): +// CHECK: %cst = arith.constant dense<[0, 0, 1, 1, 1]> : tensor<5xi32> +// CHECK: %cst_0 = arith.constant dense<[0, 1, 0, 1, 1]> : tensor<5xi32> +// CHECK: %cst_1 = arith.constant dense<2> : tensor +// CHECK: %cst_2 = arith.constant dense<"*"> : tensor +// CHECK: %cst_3 = arith.constant dense<-1> : tensor<1xi32> +// CHECK: %cst_4 = arith.constant dense<-1> : tensor +// CHECK: %cst_5 = arith.constant dense<2> : tensor<1xi32> +// CHECK: %cst_6 = arith.constant dense<1> : tensor +// CHECK: %cst_7 = arith.constant dense<0> : tensor +// CHECK: %cst_8 = arith.constant dense<1> : tensor<1xi32> +// CHECK: %cst_9 = arith.constant dense<0> : tensor<1xi32> +// CHECK: %7:2 = func.call @func_3_DARWINN_FLOAT(%arg5, %cst_9, %cst_8, %cst_7, %cst_6, %cst_5) {tac.device = "DARWINN", tac.inference_type = "FLOAT", tac.interface_name = "func_3"} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor, tensor, tensor<1xi32>) -> (tensor, tensor<2xi32>) +// CHECK: %8 = "tfl.reduce_prod"(%7#1, %cst_9) <{keep_dims = true}> {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<2xi32>, tensor<1xi32>) -> tensor<1xi32> +// CHECK: %9:3 = func.call @func_4_DARWINN_FLOAT(%arg5, %8, %arg6, %cst_9, %cst_8, %cst_7, %cst_6, %cst_5) {tac.device = "DARWINN", tac.inference_type = "FLOAT", tac.interface_name = "func_4"} : (tensor, tensor<1xi32>, tensor, tensor<1xi32>, tensor<1xi32>, tensor, tensor, tensor<1xi32>) -> (tensor, tensor, tensor<2xi32>) +// CHECK: %10 = "tfl.reduce_prod"(%9#2, %cst_9) <{keep_dims = true}> {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor<2xi32>, tensor<1xi32>) -> tensor<1xi32> +// CHECK: %11 = "tfl.expand_dims"(%arg11, %cst_4) {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor, tensor) -> tensor +// CHECK: %12 = "tfl.expand_dims"(%arg12, %cst_4) {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor, tensor) -> tensor +// CHECK: %13:10 = func.call @func_5_DARWINN_FLOAT(%arg6, %10, %arg10, %cst_6, %11, %9#1, %cst_3, %cst_2, %12, %7#0, %9#0, %arg7, %cst_1, %cst_0, %arg11, %cst, %arg12, %arg9) {tac.device = "DARWINN", tac.inference_type = "FLOAT", tac.interface_name = "func_5"} : (tensor, tensor<1xi32>, tensor, tensor, tensor, tensor, tensor<1xi32>, tensor, tensor, tensor, tensor, tensor, tensor, tensor<5xi32>, tensor, tensor<5xi32>, tensor, tensor) -> (tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor) +// CHECK: %14 = "tfl.custom"(%13#5, %13#2, %13#1) <{custom_code = "FlexSelect", custom_option = #tfl}> {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor, tensor, tensor) -> tensor +// CHECK: %15 = "tfl.custom"(%arg13, %arg10, %14) <{custom_code = "FlexTensorListSetItem", custom_option = #tfl}> {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor>>, tensor, tensor) -> tensor>> +// CHECK: %16 = "tfl.custom"(%13#6, %13#4, %13#3) <{custom_code = "FlexSelect", custom_option = #tfl}> {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor, tensor, tensor) -> tensor +// CHECK: %17 = "tfl.custom"(%arg14, %arg10, %16) <{custom_code = "FlexTensorListSetItem", custom_option = #tfl}> {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor>>, tensor, tensor) -> tensor>> +// CHECK: %18 = "tfl.custom"(%arg15, %arg10, %13#7) <{custom_code = "FlexTensorListSetItem", custom_option = #tfl}> {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor>>, tensor, tensor) -> tensor>> +// CHECK: %19 = "tfl.custom"(%arg16, %arg10, %13#8) <{custom_code = "FlexTensorListSetItem", custom_option = #tfl}> {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor>>, tensor, tensor) -> tensor>> +// CHECK: "tfl.yield"(%13#9, %13#0, %13#7, %13#8, %15, %17, %18, %19) : (tensor, tensor, tensor, tensor, tensor>>, tensor>>, tensor>>, tensor>>) -> () +// CHECK: }) {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor, tensor, tensor, tensor, tensor>>, tensor>>, tensor>>, tensor>>) -> (tensor, tensor, tensor, tensor, tensor>>, tensor>>, tensor>>, tensor>>) +// CHECK: %3 = "tfl.custom"(%2#4, %arg0) <{custom_code = "FlexTensorListStack", custom_option = #tfl}> {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor>>, tensor<1xi32>) -> tensor +// CHECK: %4 = "tfl.custom"(%3, %arg8) <{custom_code = "FlexTranspose", custom_option = #tfl}> {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor, tensor<2xi32>) -> tensor +// CHECK: %5 = "tfl.custom"(%2#5, %arg0) <{custom_code = "FlexTensorListStack", custom_option = #tfl}> {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor>>, tensor<1xi32>) -> tensor +// CHECK: %6 = "tfl.custom"(%5, %arg8) <{custom_code = "FlexTranspose", custom_option = #tfl}> {tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor, tensor<2xi32>) -> tensor +// CHECK: return %4, %6 : tensor, tensor +// CHECK: } +// CHECK: func.func private @func_2_DARWINN_FLOAT(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tac.device = "DARWINN", tac.inference_type = "FLOAT", tac.interface_name = "func_2"} { +// CHECK: %0 = tfl.less(%arg0, %arg1) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor) -> tensor +// CHECK: %1 = tfl.less(%arg2, %arg1) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor) -> tensor +// CHECK: %2 = tfl.logical_and %1, %0 {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : tensor +// CHECK: return %2 : tensor +// CHECK: } +// CHECK: func.func private @func_3_DARWINN_FLOAT(%arg0: tensor, %arg1: tensor<1xi32>, %arg2: tensor<1xi32>, %arg3: tensor, %arg4: tensor, %arg5: tensor<1xi32>) -> (tensor, tensor<2xi32>) attributes {tac.device = "DARWINN", tac.inference_type = "FLOAT", tac.interface_name = "func_3"} { +// CHECK: %0 = "tfl.shape"(%arg0) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor) -> tensor<2xi32> +// CHECK: %1 = "tfl.strided_slice"(%0, %arg1, %arg2, %arg2) <{begin_mask = 0 : i32, ellipsis_mask = 0 : i32, end_mask = 0 : i32, new_axis_mask = 0 : i32, offset = false, shrink_axis_mask = 1 : i32}> {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor +// CHECK: %2 = "tfl.range"(%arg3, %1, %arg4) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor, tensor) -> tensor +// CHECK: %3 = "tfl.pack"(%1, %arg4) <{axis = 0 : i32, values_count = 2 : i32}> {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor) -> tensor<2xi32> +// CHECK: %4 = "tfl.strided_slice"(%0, %arg2, %arg5, %arg2) <{begin_mask = 0 : i32, ellipsis_mask = 0 : i32, end_mask = 0 : i32, new_axis_mask = 0 : i32, offset = false, shrink_axis_mask = 1 : i32}> {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor +// CHECK: %5 = tfl.mul(%2, %4) <{fused_activation_function = "NONE"}> {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor) -> tensor +// CHECK: %6 = "tfl.reshape"(%5, %3) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor<2xi32>) -> tensor +// CHECK: %7 = "tfl.strided_slice"(%0, %arg1, %arg5, %arg2) <{begin_mask = 1 : i32, ellipsis_mask = 0 : i32, end_mask = 0 : i32, new_axis_mask = 0 : i32, offset = false, shrink_axis_mask = 0 : i32}> {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<2xi32> +// CHECK: return %6, %7 : tensor, tensor<2xi32> +// CHECK: } +// CHECK: func.func private @func_4_DARWINN_FLOAT(%arg0: tensor, %arg1: tensor<1xi32>, %arg2: tensor, %arg3: tensor<1xi32>, %arg4: tensor<1xi32>, %arg5: tensor, %arg6: tensor, %arg7: tensor<1xi32>) -> (tensor, tensor, tensor<2xi32>) attributes {tac.device = "DARWINN", tac.inference_type = "FLOAT", tac.interface_name = "func_4"} { +// CHECK: %0 = "tfl.reshape"(%arg0, %arg1) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor<1xi32>) -> tensor +// CHECK: %1 = "tfl.shape"(%arg2) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor) -> tensor<2xi32> +// CHECK: %2 = "tfl.strided_slice"(%1, %arg3, %arg4, %arg4) <{begin_mask = 0 : i32, ellipsis_mask = 0 : i32, end_mask = 0 : i32, new_axis_mask = 0 : i32, offset = false, shrink_axis_mask = 1 : i32}> {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor +// CHECK: %3 = "tfl.range"(%arg5, %2, %arg6) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor, tensor) -> tensor +// CHECK: %4 = "tfl.pack"(%2, %arg6) <{axis = 0 : i32, values_count = 2 : i32}> {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor) -> tensor<2xi32> +// CHECK: %5 = "tfl.strided_slice"(%1, %arg4, %arg7, %arg4) <{begin_mask = 0 : i32, ellipsis_mask = 0 : i32, end_mask = 0 : i32, new_axis_mask = 0 : i32, offset = false, shrink_axis_mask = 1 : i32}> {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor +// CHECK: %6 = tfl.mul(%3, %5) <{fused_activation_function = "NONE"}> {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor) -> tensor +// CHECK: %7 = "tfl.reshape"(%6, %4) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor<2xi32>) -> tensor +// CHECK: %8 = "tfl.strided_slice"(%1, %arg3, %arg7, %arg4) <{begin_mask = 1 : i32, ellipsis_mask = 0 : i32, end_mask = 0 : i32, new_axis_mask = 0 : i32, offset = false, shrink_axis_mask = 0 : i32}> {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<2xi32> +// CHECK: return %0, %7, %8 : tensor, tensor, tensor<2xi32> +// CHECK: } +// CHECK: func.func private @func_5_DARWINN_FLOAT(%arg0: tensor, %arg1: tensor<1xi32>, %arg2: tensor, %arg3: tensor, %arg4: tensor, %arg5: tensor, %arg6: tensor<1xi32>, %arg7: tensor, %arg8: tensor, %arg9: tensor, %arg10: tensor, %arg11: tensor, %arg12: tensor, %arg13: tensor<5xi32>, %arg14: tensor, %arg15: tensor<5xi32>, %arg16: tensor, %arg17: tensor) -> (tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor) attributes {tac.device = "DARWINN", tac.inference_type = "FLOAT", tac.interface_name = "func_5"} { +// CHECK: %0 = "tfl.reshape"(%arg0, %arg1) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor<1xi32>) -> tensor +// CHECK: %1 = tfl.add %arg2, %arg3 {fused_activation_function = "NONE", tac.device = "DARWINN", tac.inference_type = "FLOAT"} : tensor +// CHECK: %2 = tfl.add %arg4, %arg5 {fused_activation_function = "NONE", tac.device = "DARWINN", tac.inference_type = "FLOAT"} : tensor +// CHECK: %3 = "tfl.reshape"(%2, %arg6) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor<1xi32>) -> tensor +// CHECK: %4 = "tfl.gather"(%0, %3) <{axis = 0 : i32, batch_dims = 0 : i32}> {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor) -> tensor +// CHECK: %5 = "tfl.reshape"(%4, %arg6) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor<1xi32>) -> tensor +// CHECK: %6 = "tfl.shape"(%5) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor) -> tensor<1xi32> +// CHECK: %7 = "tfl.fill"(%6, %arg7) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor<1xi32>, tensor) -> tensor +// CHECK: %8 = tfl.add %arg8, %arg9 {fused_activation_function = "NONE", tac.device = "DARWINN", tac.inference_type = "FLOAT"} : tensor +// CHECK: %9 = "tfl.reshape"(%8, %arg6) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor<1xi32>) -> tensor +// CHECK: %10 = "tfl.gather"(%arg10, %9) <{axis = 0 : i32, batch_dims = 0 : i32}> {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor) -> tensor +// CHECK: %11 = "tfl.reshape"(%10, %arg6) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor<1xi32>) -> tensor +// CHECK: %12 = "tfl.shape"(%11) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor) -> tensor<1xi32> +// CHECK: %13 = "tfl.fill"(%12, %arg7) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor<1xi32>, tensor) -> tensor +// CHECK: %14 = "tfl.gather"(%arg11, %arg2) <{axis = 0 : i32, batch_dims = 0 : i32}> {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor) -> tensor +// CHECK: %15 = "tfl.equal"(%14, %arg12) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor) -> tensor +// CHECK: %16 = "tfl.equal"(%14, %arg3) {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor, tensor) -> tensor +// CHECK: %17 = "tfl.gather"(%arg13, %14) <{axis = 0 : i32, batch_dims = 0 : i32}> {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor<5xi32>, tensor) -> tensor +// CHECK: %18 = tfl.add %arg14, %17 {fused_activation_function = "NONE", tac.device = "DARWINN", tac.inference_type = "FLOAT"} : tensor +// CHECK: %19 = "tfl.gather"(%arg15, %14) <{axis = 0 : i32, batch_dims = 0 : i32}> {tac.device = "DARWINN", tac.inference_type = "FLOAT"} : (tensor<5xi32>, tensor) -> tensor +// CHECK: %20 = tfl.add %arg16, %19 {fused_activation_function = "NONE", tac.device = "DARWINN", tac.inference_type = "FLOAT"} : tensor +// CHECK: %21 = tfl.add %arg17, %arg3 {fused_activation_function = "NONE", tac.device = "DARWINN", tac.inference_type = "FLOAT"} : tensor +// CHECK: return %1, %5, %7, %11, %13, %15, %16, %18, %20, %21 : tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor +// CHECK: } + +// ----- + +// CHECK-SKIP-CPU-LABEL: testSkipCpuOps +func.func @testSkipCpuOps(%arg0: tensor<1xf32>) -> (tensor<1xf32>, tensor<1xf32>) { + %0 = "tfl.add"(%arg0, %arg0) {tac.device = "GPU", fused_activation_function = "RELU6", tac.inference_type = "FLOAT"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + %1 = "tfl.add"(%arg0, %0) {tac.device = "CPU", fused_activation_function = "RELU6", tac.inference_type = "FLOAT"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + func.return %0, %1 : tensor<1xf32>, tensor<1xf32> +} + +// CHECK-SKIP-CPU: %[[RES0:.*]] = call @func_0_GPU_FLOAT(%arg0) {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} : (tensor<1xf32>) -> tensor<1xf32> +// CHECK-SKIP-CPU: %[[RES1:.*]] = tfl.add %arg0, %[[RES0]] {fused_activation_function = "RELU6", tac.device = "CPU", tac.inference_type = "FLOAT"} : tensor<1xf32> +// CHECK-SKIP-CPU: return %[[RES0]], %[[RES1]] : tensor<1xf32>, tensor<1xf32> +// CHECK-SKIP-CPU: } +// CHECK-SKIP-CPU: func.func private @func_0_GPU_FLOAT(%arg0: tensor<1xf32>) -> tensor<1xf32> attributes {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} { +// CHECK-SKIP-CPU: %[[RES2:.*]] = tfl.add %arg0, %arg0 {fused_activation_function = "RELU6", tac.device = "GPU", tac.inference_type = "FLOAT"} : tensor<1xf32> +// CHECK-SKIP-CPU: return %[[RES2]] : tensor<1xf32> +// CHECK-SKIP-CPU: } + +// ----- + +// CHECK-SKIP-CPU-LABEL: testSkipCpuOpsWithinLoop +func.func @testSkipCpuOpsWithinLoop(%arg0: tensor) -> tensor { + %0 = "tfl.while"(%arg0) ({ + ^bb0(%block: tensor): + "tfl.yield"(%block) : (tensor) -> () + },{ + ^bb0(%block: tensor): + %0 = "tfl.add"(%arg0, %block) {tac.device = "GPU", fused_activation_function = "RELU6", tac.inference_type = "FLOAT"} : (tensor, tensor) -> tensor + "tfl.yield"(%0) : (tensor) -> () + }) {tac.device = "CPU", fused_activation_function = "RELU6", tac.inference_type = "FLOAT"} : (tensor) -> tensor + func.return %0 : tensor +} + +// CHECK-SKIP-CPU: "tfl.while" +// CHECK-SKIP-CPU: ^bb0(%[[ARG0:.*]]: tensor): +// CHECK-SKIP-CPU: "tfl.yield"(%[[ARG0]]) : (tensor) -> () +// CHECK-SKIP-CPU: }, { +// CHECK-SKIP-CPU: ^bb0(%[[ARG1:.*]]: tensor): +// CHECK-SKIP-CPU: %[[RES0:.*]] = func.call @func_0_GPU_FLOAT(%{{.*}}, %[[ARG1]]) {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} : (tensor, tensor) -> tensor +// CHECK-SKIP-CPU: "tfl.yield"(%[[RES0]]) : (tensor) -> () +// CHECK-SKIP-CPU: }) {fused_activation_function = "RELU6", tac.device = "CPU", tac.inference_type = "FLOAT"} : (tensor) -> tensor + +// CHECK-SKIP-CPU: func.func private @func_0_GPU_FLOAT(%[[ARG2:.*]]: tensor, %[[ARG3:.*]]: tensor) -> tensor attributes {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} { +// CHECK-SKIP-CPU: %[[RES1:.*]] = tfl.add %[[ARG2]], %[[ARG3]] {fused_activation_function = "RELU6", tac.device = "GPU", tac.inference_type = "FLOAT"} : tensor +// CHECK-SKIP-CPU: return %[[RES1]] : tensor +// CHECK-SKIP-CPU: } + +// ----- + +// CHECK-IGNORE-INFERENCE-TYPE-LABEL: testIgnoreInferenceType +func.func @testIgnoreInferenceType(%arg0: tensor<1x384x384xf32>, %arg1: tensor<1x1x384x!quant.uniform>) -> (tensor<1x384x384xf32>, tensor<1x1x384x!quant.uniform>) { + // These 2 ops are clustered together when `ignore-inference-type` sets to true. + %0 = "tfl.add"(%arg0, %arg0) {tac.device = "GPU", tac.inference_type = "FLOAT", fused_activation_function = "NONE"} : (tensor<1x384x384xf32>, tensor<1x384x384xf32>) -> tensor<1x384x384xf32> + %1 = "tfl.mul"(%arg1, %arg1) {tac.device = "GPU", tac.inference_type = "QUANTIZED_INT8", fused_activation_function = "NONE"} : (tensor<1x1x384x!quant.uniform>, tensor<1x1x384x!quant.uniform>) -> tensor<1x1x384x!quant.uniform> + func.return %0, %1: tensor<1x384x384xf32>, tensor<1x1x384x!quant.uniform> +} + +// CHECK-IGNORE-INFERENCE-TYPE: %[[RES0:.*]]:2 = call @[[FUNC_NAME:.*]](%arg0, %arg1) {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} : (tensor<1x384x384xf32>, tensor<1x1x384x!quant.uniform>) -> (tensor<1x384x384xf32>, tensor<1x1x384x!quant.uniform>) +// CHECK-IGNORE-INFERENCE-TYPE: return %[[RES0]]#0, %[[RES0]]#1 : tensor<1x384x384xf32>, tensor<1x1x384x!quant.uniform> +// CHECK-IGNORE-INFERENCE-TYPE: } +// CHECK-IGNORE-INFERENCE-TYPE: func.func private @[[FUNC_NAME]](%arg0: tensor<1x384x384xf32>, %arg1: tensor<1x1x384x!quant.uniform>) -> (tensor<1x384x384xf32>, tensor<1x1x384x!quant.uniform>) attributes {tac.device = "GPU", tac.inference_type = "FLOAT", tac.interface_name = "func_0"} { +// CHECK-IGNORE-INFERENCE-TYPE: %[[RES1:.*]] = tfl.add %arg0, %arg0 {fused_activation_function = "NONE", tac.device = "GPU", tac.inference_type = "FLOAT"} : tensor<1x384x384xf32> +// CHECK-IGNORE-INFERENCE-TYPE: %[[RES2:.*]] = tfl.mul %arg1, %arg1 {fused_activation_function = "NONE", tac.device = "GPU", tac.inference_type = "QUANTIZED_INT8"} : tensor<1x1x384x!quant.uniform> +// CHECK-IGNORE-INFERENCE-TYPE: return %[[RES1]], %[[RES2]] : tensor<1x384x384xf32>, tensor<1x1x384x!quant.uniform> +// CHECK-IGNORE-INFERENCE-TYPE: } + +// ----- + +func.func @testStackVersionPropagation(%arg0: tensor<1x4x4x1920xf32>) -> tensor<1x4x4x64xf32> { + %cst = arith.constant dense<1.200000e+00> : tensor<64xf32> + %0 = "tfl.pseudo_qconst"() <{qtype = tensor<64x1x1x1920x!quant.uniform:f32, 0.002>>, value = dense<1> : tensor<64x1x1x1920xi8>}> : () -> tensor<64x1x1x1920x!quant.uniform:f32, 0.002>> + %1 = "tfl.conv_2d"(%arg0, %0, %cst) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32}> {tac.delegate_compiler_version = "V_1_0", tac.device = "DARWINN", tac.inference_type = "HYBRID"} : (tensor<1x4x4x1920xf32>, tensor<64x1x1x1920x!quant.uniform:f32, 0.002>>, tensor<64xf32>) -> tensor<1x4x4x64xf32> + %2 = tfl.add %1, %1 {fused_activation_function = "NONE", tac.delegate_compiler_version = "V_1_5", tac.device = "DARWINN", tac.inference_type = "FLOAT"} : tensor<1x4x4x64xf32> + func.return %2 : tensor<1x4x4x64xf32> +} + +// CHECK-LABEL: func @testStackVersionPropagation +// CHECK: %[[RES0:.*]] = call @func_0_DARWINN_HYBRID(%arg0, %{{.*}}, %{{.*}}) {tac.device = "DARWINN", tac.inference_type = "HYBRID", tac.interface_name = "func_0"} : (tensor<1x4x4x1920xf32>, tensor<64x1x1x1920x!quant.uniform:f32, 2.000000e-03>>, tensor<64xf32>) -> tensor<1x4x4x64xf32> +// CHECK: %[[RES1:.*]] = call @func_1_DARWINN_FLOAT(%[[RES0]]) {tac.device = "DARWINN", tac.inference_type = "FLOAT", tac.interface_name = "func_1"} : (tensor<1x4x4x64xf32>) -> tensor<1x4x4x64xf32> +// CHECK: return %[[RES1]] +// CHECK: } +// CHECK: func.func private @func_0_DARWINN_HYBRID(%arg0: tensor<1x4x4x1920xf32>, %arg1: tensor<64x1x1x1920x!quant.uniform:f32, 2.000000e-03>>, %arg2: tensor<64xf32>) -> tensor<1x4x4x64xf32> attributes {tac.delegate_compiler_version = "V_1_0", tac.device = "DARWINN", tac.inference_type = "HYBRID", tac.interface_name = "func_0"} +// CHECK: func.func private @func_1_DARWINN_FLOAT(%arg0: tensor<1x4x4x64xf32>) -> tensor<1x4x4x64xf32> attributes {tac.delegate_compiler_version = "V_1_5", tac.device = "DARWINN", tac.inference_type = "FLOAT", tac.interface_name = "func_1"} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/tac-filter.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/tac-filter.mlir new file mode 100644 index 0000000000000000000000000000000000000000..27270047c348d140c79986d972ed5ff8e099c4e5 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/tac-filter.mlir @@ -0,0 +1,84 @@ +// RUN: tac-opt-all-backends -tfl-tac-filter='use-test-setting=true' %s -split-input-file -verify-diagnostics | FileCheck %s + +// expected-remark@below {{Tac filter (0): filter type: function filter SKIP_TARGET_ANNOTATION, filter_pattern: "^testFunction"}} +// expected-remark@below {{Tac filter (1): filter type: function filter INCLUDE_TARGET_ANNOTATION, filter_pattern: "testFunctionInclude"}} +// expected-remark@below {{Tac filter (1) specified but not applied to any op}} +// expected-remark@below {{Tac filter (2): filter type: op filter, filter_pattern: "^test_op"}} +// expected-remark@below {{Tac filter (2) specified but not applied to any op}} +module { + // CHECK-LABEL: testFunctionSkiped + // expected-remark@+1 {{filtered by tac filter (0)}} + func.func @testFunctionSkiped(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>) { + // CHECK: tfl.add + // CHECK-SAME: tac.skip_target_annotation + %0 = "tfl.add"(%arg0, %arg1) {fused_activation_function = "RELU6"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + // CHECK: tfl.add + // CHECK-SAME: tac.skip_target_annotation + %1 = "tfl.add"(%arg0, %0) {fused_activation_function = "RELU"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + // CHECK: tfl.relu + // CHECK-SAME: tac.skip_target_annotation + %2 = "tfl.relu"(%arg0) : (tensor<1xf32>) -> tensor<1xf32> + func.return + } +} + +// ----- + +// expected-remark@below {{Tac filter (0): filter type: function filter SKIP_TARGET_ANNOTATION, filter_pattern: "^testFunction"}} +// expected-remark@below {{Tac filter (1): filter type: function filter INCLUDE_TARGET_ANNOTATION, filter_pattern: "testFunctionInclude"}} +// expected-remark@below {{Tac filter (2): filter type: op filter, filter_pattern: "^test_op"}} +// expected-remark@below {{Tac filter (2) specified but not applied to any op}} +module { + // CHECK-LABEL: testFunctionInclude + // CHECK-NOT: tac.skip_target_annotation + // expected-remark@+2 {{filtered by tac filter (0)}} + // expected-remark@+1 {{filtered by tac filter (1)}} + func.func @testFunctionInclude(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>) { + %0 = "tfl.add"(%arg0, %arg1) {fused_activation_function = "RELU6"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + func.return + } +} + +// ----- + +// expected-remark@below {{Tac filter (0): filter type: function filter SKIP_TARGET_ANNOTATION, filter_pattern: "^testFunction"}} +// expected-remark@below {{Tac filter (0) specified but not applied to any op}} +// expected-remark@below {{Tac filter (1): filter type: function filter INCLUDE_TARGET_ANNOTATION, filter_pattern: "testFunctionInclude"}} +// expected-remark@below {{Tac filter (1) specified but not applied to any op}} +// expected-remark@below {{Tac filter (2): filter type: op filter, filter_pattern: "^test_op"}} +module { + // CHECK-LABEL: testOpFilter + // expected-remark@+1 {{all ops filtered by tac filter (2): "tfl.add", "tfl.relu"}} + func.func @testOpFilter(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>) { + // CHECK: tfl.add + // CHECK-SAME: tac.skip_target_annotation + %0 = "tfl.add"(%arg0, %arg1) {fused_activation_function = "RELU6"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> loc("test_op_0") + // CHECK: tfl.add + // CHECK-NOT: tac.skip_target_annotation + %1 = "tfl.add"(%arg0, %0) {fused_activation_function = "RELU"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> loc("non_test_op") + // CHECK: tfl.relu + // CHECK-SAME: tac.skip_target_annotation + %2 = "tfl.relu"(%arg0) : (tensor<1xf32>) -> tensor<1xf32> loc("test_op_1") + func.return + } +} + +// ----- + +// expected-remark@below {{Tac filter (0): filter type: function filter SKIP_TARGET_ANNOTATION, filter_pattern: "^testFunction"}} +// expected-remark@below {{Tac filter (0) specified but not applied to any op}} +// expected-remark@below {{Tac filter (1): filter type: function filter INCLUDE_TARGET_ANNOTATION, filter_pattern: "testFunctionInclude"}} +// expected-remark@below {{Tac filter (1) specified but not applied to any op}} +// expected-remark@below {{Tac filter (2): filter type: op filter, filter_pattern: "^test_op"}} +module { + // CHECK-LABEL: testOpMultipleResults + // expected-remark@+1 {{all ops filtered by tac filter (2): "tfl.split_v"}} + func.func @testOpMultipleResults(%arg0: tensor<16x4x4xf32>) -> (tensor<7x4x4xf32>, tensor<3x4x4xf32>, tensor<6x4x4xf32>) { + %size_splits = arith.constant dense<[7, 3, 6]> : tensor<3xi32> + %split_dim = arith.constant dense<0> : tensor + // CHECK: tfl.split_v + // CHECK-SAME: tac.skip_target_annotation + %0, %1, %2 = "tfl.split_v"(%arg0, %size_splits, %split_dim) {num_splits = 3 : i32} : (tensor<16x4x4xf32>, tensor<3xi32>, tensor) -> (tensor<7x4x4xf32>, tensor<3x4x4xf32>, tensor<6x4x4xf32>) loc("test_op_split"("/tmp/test_model.tflite":0:0)) + func.return %0, %1, %2 : tensor<7x4x4xf32>, tensor<3x4x4xf32>, tensor<6x4x4xf32> + } +} \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/target-annotation.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/target-annotation.mlir new file mode 100644 index 0000000000000000000000000000000000000000..86577517a071df58b211fd547dcd8633ea92a511 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tests/target-annotation.mlir @@ -0,0 +1,91 @@ +// RUN: tac-opt-all-backends -tfl-target-annotation='device-specs=GPU' %s -split-input-file -verify-diagnostics | FileCheck %s + +func.func @testConv(%arg0: tensor<256x32x32x3xf32>, %arg1: tensor<16x3x3x3xf32>, %arg2: tensor<16xf32>) -> tensor<256x30x30x16xf32> { + // CHECK: tac.device = "GPU", tac.inference_type = "FLOAT" + %0 = "tfl.conv_2d"(%arg0, %arg1, %arg2) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32} : (tensor<256x32x32x3xf32>, tensor<16x3x3x3xf32>, tensor<16xf32>) -> tensor<256x30x30x16xf32> + func.return %0 : tensor<256x30x30x16xf32> +} + +// ----- + +func.func @testDepthwiseConv(%arg0: tensor<1x112x112x32xf32>, %arg1: tensor<1x3x3x32xf32>, %arg2: tensor<32xf32>) -> tensor<1x112x112x32xf32> { + // CHECK: tac.device = "GPU", tac.inference_type = "FLOAT" + %0 = "tfl.depthwise_conv_2d"(%arg0, %arg1, %arg2) {depth_multiplier = 1 : i32, dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32} : (tensor<1x112x112x32xf32>, tensor<1x3x3x32xf32>, tensor<32xf32>) -> tensor<1x112x112x32xf32> + func.return %0 : tensor<1x112x112x32xf32> +} + +// ----- + +func.func @testAvgPool(%arg0: tensor<256x32x32x3xf32>) -> tensor<256x30x30x16xf32> { + // CHECK: tac.device = "GPU", tac.inference_type = "FLOAT" + %0 = "tfl.average_pool_2d"(%arg0) {filter_height = 3 : i32, filter_width = 3 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32} : (tensor<256x32x32x3xf32>) -> tensor<256x30x30x16xf32> + func.return %0 : tensor<256x30x30x16xf32> +} + +// ----- + +func.func @testMaxPool(%arg0: tensor<256x32x32x3xf32>) -> tensor<256x30x30x16xf32> { + // CHECK: tac.device = "GPU", tac.inference_type = "FLOAT" + %0 = "tfl.max_pool_2d"(%arg0) {filter_height = 3 : i32, filter_width = 3 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32} : (tensor<256x32x32x3xf32>) -> tensor<256x30x30x16xf32> + func.return %0 : tensor<256x30x30x16xf32> +} + +// ----- + +func.func @testAddReluPack(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>) { + // CHECK: tac.device = "GPU", tac.inference_type = "FLOAT" + %0 = "tfl.add"(%arg0, %arg1) {fused_activation_function = "RELU6"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + // CHECK: tac.device = "GPU", tac.inference_type = "FLOAT" + %1 = "tfl.add"(%arg0, %0) {fused_activation_function = "RELU"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + // CHECK: tac.device = "GPU", tac.inference_type = "FLOAT" + %2 = "tfl.relu"(%arg0) : (tensor<1xf32>) -> tensor<1xf32> + // CHECK: tac.device = "CPU", tac.inference_type = "FLOAT" + %3 = "tfl.pack"(%arg0, %arg1) {axis = 0 : i32, values_count = 2 : i32} : (tensor<1xf32>, tensor<1xf32>) -> tensor<2x1xf32> + func.return +} + +func.func @notAnnotateConst(%arg0: tensor<256x32x32x3xf32>) -> tensor<256x30x30x16xf32> { + // CHECK-NOT: tac.device tac.inference_type + %0 = "tfl.pseudo_const"() {value = dense<1.000000e+00> : tensor<16x3x3x3xf32>} : () -> tensor<16x3x3x3xf32> + // CHECK-NOT: tac.device tac.inference_type + %1 = "tfl.pseudo_const"() {value = dense<1.000000e+00> : tensor<16xf32>} : () -> tensor<16xf32> + // CHECK: tac.device = "GPU", tac.inference_type = "FLOAT" + %2 = "tfl.conv_2d"(%arg0, %0, %1) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32} : (tensor<256x32x32x3xf32>, tensor<16x3x3x3xf32>, tensor<16xf32>) -> tensor<256x30x30x16xf32> + func.return %2 : tensor<256x30x30x16xf32> +} + +// ----- + +func.func @notAnnotateQuantizeDequantize(%arg0: tensor<4x384x32x!quant.uniform>) -> tensor<4x384x32xf32> { + // CHECK-NOT: tac.device tac.inference_type + %0 = "tfl.pseudo_const"() {value = dense<[1, 4, 384, 32]> : tensor<4xi32>} : () -> tensor<4xi32> + // CHECK-NOT: tac.device tac.inference_type + %1 = "tfl.pseudo_const"() {value = dense<[4, 384, 32]> : tensor<3xi32>} : () -> tensor<3xi32> + // CHECK: tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8" + %2 = "tfl.reshape"(%arg0, %0) : (tensor<4x384x32x!quant.uniform>, tensor<4xi32>) -> tensor<1x4x384x32x!quant.uniform> + // CHECK-NOT: tac.device tac.inference_type + %3 = "tfl.quantize"(%2) {qtype = tensor<1x4x384x32x!quant.uniform>} : (tensor<1x4x384x32x!quant.uniform>) -> tensor<1x4x384x32x!quant.uniform> + // CHECK: tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8" + %4 = "tfl.reshape"(%3, %1) : (tensor<1x4x384x32x!quant.uniform>, tensor<3xi32>) -> tensor<4x384x32x!quant.uniform> + // CHECK-NOT: tac.device tac.inference_type + %5 = "tfl.dequantize"(%4) : (tensor<4x384x32x!quant.uniform>) -> tensor<4x384x32xf32> + func.return %5 : tensor<4x384x32xf32> + +} + +func.func @annotateInferenceType(%arg0: tensor<1x1x384x!quant.uniform>) -> tensor<1x384x384x!quant.uniform>{ + // CHECK-NOT: tac.device tac.inference_type + %0 = "tfl.pseudo_qconst"() {qtype = tensor<1x384x1x!quant.uniform>, value = dense<127> : tensor<1x384x1xi8>} : () -> tensor<1x384x1x!quant.uniform> + // CHECK: tac.device = "CPU", tac.inference_type = "QUANTIZED_INT8" + %1 = "tfl.mul"(%arg0, %0) {fused_activation_function = "NONE"} : (tensor<1x1x384x!quant.uniform>, tensor<1x384x1x!quant.uniform>) -> tensor<1x384x384x!quant.uniform> + func.return %1 : tensor<1x384x384x!quant.uniform> +} + +// ----- + +// CHECK-LABEL: testSkipAnnotation +func.func @testSkipAnnotation(%arg0: tensor<256x32x32x3xf32>, %arg1: tensor<16x3x3x3xf32>, %arg2: tensor<16xf32>) -> tensor<256x30x30x16xf32> { + // CHECK-NOT: tac.device + %0 = "tfl.conv_2d"(%arg0, %arg1, %arg2) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32, tac.skip_target_annotation } : (tensor<256x32x32x3xf32>, tensor<16x3x3x3xf32>, tensor<16xf32>) -> tensor<256x30x30x16xf32> + func.return %0 : tensor<256x30x30x16xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tflite_import_export.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tflite_import_export.cc new file mode 100644 index 0000000000000000000000000000000000000000..e773b780218ec67cc2d2f43fd3940dadd2cea19b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tflite_import_export.cc @@ -0,0 +1,120 @@ +/* Copyright 2021 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/compiler/mlir/lite/experimental/tac/tflite_import_export.h" + +#include +#include +#include + +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/str_cat.h" +#include "llvm/Support/Path.h" +#include "llvm/Support/ToolOutputFile.h" +#include "mlir/Support/FileUtilities.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/targets.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/utils.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/execution_metadata_exporter.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/hardwares/target_hardware.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/utils/utils.h" + +namespace mlir { +namespace TFL { +namespace tac { +namespace { +void AttachCostPerDevice(mlir::ModuleOp module, + llvm::ArrayRef device_specs) { + std::set processed_device_specs; + for (const auto& device_spec : device_specs) { + processed_device_specs.insert( + mlir::TFL::tac::GetCanonicalHardwareName(device_spec)); + } + processed_device_specs.insert("CPU"); + + module.walk([&](mlir::Operation* op) { + if (!mlir::TFL::tac::IsNonConstOp(op) && + !llvm::isa(op)) + return; + + // Attach cost per target. + // Unsupported op will have negative values. + mlir::SmallVector device_costs; + for (const auto& device : processed_device_specs) { + auto* target_hardware = mlir::TFL::tac::GetTargetHardware(device); + float cost = -1; + if (target_hardware->IsOpSupported(op)) { + cost = target_hardware->GetOpCost(op); + } + + mlir::StringAttr device_identifier = + mlir::StringAttr::get(module.getContext(), device); + auto float_type = mlir::Float32Type::get(module.getContext()); + auto float_attr = + mlir::FloatAttr::get(float_type, static_cast(cost)); + device_costs.push_back({device_identifier, float_attr}); + } + + op->setAttr("per_device_costs", + mlir::DictionaryAttr::get(module.getContext(), device_costs)); + }); +} + +} // namespace + +//////////// Importer //////////// +absl::StatusOr> TfLiteImporter::Import() { + source_mgr_handler_ = std::make_unique( + source_mgr_, &context_); + return ImportFlatbufferOrMlir( + options_.file_name, options_.input_mlir, + /*experimental_prune_unreachable_nodes_unconditionally=*/true, + &source_mgr_, &context_); +} + +//////////// Exporter //////////// +absl::Status TfLiteExporter::Export(mlir::ModuleOp module) { + // return absl::OkStatus(); + if (options_.export_runtime_metadata) { + // Run the cost model for each device/op. + AttachCostPerDevice(module, options_.target_hardware_backends); + + // We will export the runtime metadata with the same name under the same + // directory except with a different extention ".rtmeta". + llvm::SmallString<128> metadata_filename(options_.output_file_name); + const char kRuntimeMetadataName[] = "rtmeta"; + llvm::sys::path::replace_extension(metadata_filename, kRuntimeMetadataName); + + std::string error_msg; + auto output = mlir::openOutputFile(metadata_filename, &error_msg); + if (output == nullptr) { + return absl::InvalidArgumentError( + absl::StrCat("cannot open output file: ", error_msg)); + } + auto result = tflite::ExportRuntimeMetadata(module); + if (!result) { + return absl::InvalidArgumentError("Cannot export runtime metadata."); + } + output->os() << result; + output->keep(); + } + + return mlir::TFL::tac::ExportFlatbufferOrMlir(options_.output_file_name, + options_.output_mlir, module, + /*enable_select_tf_ops=*/false); +} + +} // namespace tac +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tflite_import_export.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tflite_import_export.h new file mode 100644 index 0000000000000000000000000000000000000000..ad60e8b9ab947cbaee248d83852d0bb0adfc1c13 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/tflite_import_export.h @@ -0,0 +1,74 @@ +/* Copyright 2021 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_TFLITE_IMPORT_EXPORT_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_TFLITE_IMPORT_EXPORT_H_ + +#include +#include +#include + +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "llvm/Support/SourceMgr.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/tac_importer_exporter.h" + +namespace mlir { +namespace TFL { +namespace tac { +// TAC Importer for TFLite. +// This import to MLIR from tflite file or MLIR +class TfLiteImporter : public mlir::TFL::tac::TacImporter { + public: + // Options for configuring the importer. + struct Options { + std::string file_name; + // Whether the input file is an MLIR not tflite file. + bool input_mlir = false; + }; + + explicit TfLiteImporter(const Options& options) : options_(options) {} + + absl::StatusOr> Import() override; + + private: + Options options_; + mlir::MLIRContext context_; + llvm::SourceMgr source_mgr_; + std::unique_ptr source_mgr_handler_; +}; + +// TAC Exporter. It exports the provided Module to a tflite file. +class TfLiteExporter : public mlir::TFL::tac::TacExporter { + public: + // Exporter configuration options. + struct Options { + bool export_runtime_metadata = false; + bool output_mlir = false; + std::string output_file_name; + std::vector target_hardware_backends; + }; + + explicit TfLiteExporter(const Options& options) : options_(options) {} + + absl::Status Export(mlir::ModuleOp module) override; + + private: + Options options_; +}; +} // namespace tac +} // namespace TFL +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_TFLITE_IMPORT_EXPORT_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/compute_cost.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/compute_cost.cc new file mode 100644 index 0000000000000000000000000000000000000000..32a3150d6c6c95c57e468d4e5dcdc89650e2d5a0 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/compute_cost.cc @@ -0,0 +1,115 @@ +/* Copyright 2021 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 "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/DenseSet.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/CommandLine.h" +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/OpDefinition.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/cost.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/subgraph.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/targets.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/utils.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/transforms/cost_model.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/transforms/passes.h" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" + +namespace mlir { +namespace TFL { +namespace tac { +namespace { + +// We will caculate the total compute cost for each Func Op. +// +// The compute cost is simply an add-up of the costs of all the operations +// within the FuncOp. (Excluding const ops since they're just "data".) +// We will ignore quant/dequant/requant costs within the Func Op as well, +// intuition: +// +// The assumpution is that quant/dequant/requant will only happen at the begin +// and the end of the FuncOp (basically the "boundaries" of the subgraph). +// So we can imagine if multiple "same-inference-typed" graph are presented at +// the same time, the quant/dequant ops pair can be squashed: +// +// dequant ------------ +// | +// ops... FuncOp1 +// | +// quant ------------- +// | <--- can be squashed +// dequant ------------- +// | +// ops... FuncOp2 +// | +// quant --------------- +// +// But it's true quant & dequant ops can happen "within" the FuncOp as well, +// normally as "quantization params" adjust. We should check more careful to +// include those as those ops wouldn't be "squashed". + +class ComputeCostPass + : public mlir::PassWrapper> { + public: + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(ComputeCostPass) + + private: + llvm::StringRef getArgument() const final { return "tfl-compute-cost"; } + llvm::StringRef getDescription() const final { + return "Compute the total cost for each available subgraph."; + } + void runOnOperation() override; +}; + +void ComputeCostPass::runOnOperation() { + auto module = getOperation(); + + for (auto func : module.getOps()) { + // We only care about those functions annotated with "tac.interface_name". + auto interface_name = GetInterFaceName(func); + if (!interface_name.has_value()) continue; + + auto target = GetTargetAnnotation(func); + if (!target.has_value()) { + func.emitError("we cannot get hardware info for this function."); + signalPassFailure(); + } + + float total_cost = GetCostForFunc(&func, *target); + OpBuilder builder(func); + UpdateCost(func, total_cost, &builder); + } +} + +} // namespace + +std::unique_ptr> CreateComputeCostPass() { + return std::make_unique(); +} + +static PassRegistration pass; + +} // namespace tac +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/cost_model.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/cost_model.cc new file mode 100644 index 0000000000000000000000000000000000000000..d70edee71b70db32de866386ffaa4612f24cfa3b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/cost_model.cc @@ -0,0 +1,204 @@ +/* Copyright 2021 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/compiler/mlir/lite/experimental/tac/transforms/cost_model.h" + +#include +#include +#include +#include + +#include "absl/strings/str_cat.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/raw_ostream.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/Visitors.h" // from @llvm-project +#include "mlir/Interfaces/CallInterfaces.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/cost.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/targets.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/utils.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/hardwares/target_hardware.h" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" + +namespace mlir { +namespace TFL { +namespace tac { +namespace { + +// These are just fake costs. +constexpr float kDequantCost = 2.0; +constexpr float kQuantCost = 2.0; +constexpr float kRequantCost = 2.0; + +// TODO(renjieliu): Ideally this should consider different kinds of SOCs as +// well. + +// Get total bytes transferred. +int64_t GetTransferredTensorBytes(func::CallOp from_graph, + func::CallOp to_graph) { + int64_t total_size_transferred = 0; + for (auto input : to_graph.getOperands()) { + Operation* input_op = input.getDefiningOp(); + if (input_op && input_op == from_graph.getOperation()) { + auto input_type = + mlir::dyn_cast_or_null(input.getType()); + if (input_type == nullptr || !input_type.hasStaticShape()) continue; + // Quantized type does not support getSizeInBits. + if (IsQUI8Type(input_type) || IsQI8Type(input_type)) { + total_size_transferred += input_type.getNumElements() * 8; + } else { + auto s_type = mlir::cast(input_type); + total_size_transferred += + s_type.getNumElements() * s_type.getElementTypeBitWidth(); + } + } + } + return total_size_transferred; +} + +// Get total tensor element size transferred. +int64_t GetTransferredElementCount(func::CallOp from_graph, + func::CallOp to_graph) { + int64_t total_element_count = 0; + for (auto input : to_graph.getOperands()) { + Operation* input_op = input.getDefiningOp(); + if (input_op && input_op == from_graph.getOperation()) { + auto input_type = + mlir::dyn_cast_or_null(input.getType()); + if (input_type == nullptr || !input_type.hasStaticShape()) continue; + total_element_count += input_type.getNumElements(); + } + } + return total_element_count; +} + +struct GetOpCostPass + : mlir::PassWrapper> { + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(GetOpCostPass) + + llvm::StringRef getArgument() const final { return "tfl-get-op-cost"; } + llvm::StringRef getDescription() const final { + return "Get cost for every op"; + } + void runOnOperation() override; +}; + +void GetOpCostPass::runOnOperation() { + auto func = getOperation(); + OpBuilder builder(func); + func.walk([&](Operation* op) { + if (IsNonConstOp(op) && !IsTerminatorOp(op) && + !llvm::isa(op)) { + auto hardware = GetTargetAnnotation(op); + if (!hardware) return; + float cost = GetCostForOp(op, *hardware); + UpdateCost(op, cost, &builder); + } + }); +} + +} // namespace + +float GetCostForOp(Operation* op, const std::string& hardware) { + auto* device_hardware = GetTargetHardware(hardware); + if (device_hardware == nullptr) { + return kDefaultFixedValuedCost; + } + + return device_hardware->GetOpCost(op); +} + +float GetCostForFunc(func::FuncOp* func, const std::string& hardware) { + auto* device_hardware = GetTargetHardware(hardware); + if (device_hardware == nullptr) { + return kDefaultFixedValuedCost; + } + + return device_hardware->GetFuncCost(func); +} + +float GetTransferCost(const std::string& from_hardware_str, + const std::string& to_hardware_str, + func::CallOp from_graph, func::CallOp to_graph) { + auto from_hardware = GetTargetHardware(from_hardware_str); + auto to_hardware = GetTargetHardware(to_hardware_str); + if (from_hardware == nullptr) { + from_graph.emitError(absl::StrCat( + "we cannot find the registered hardware: ", from_hardware_str)); + } + + if (to_hardware == nullptr) { + to_graph.emitError(absl::StrCat("we cannot find the registered hardware: ", + to_hardware_str)); + } + + const int64_t total_size_transferred = + GetTransferredTensorBytes(from_graph, to_graph); + return to_hardware->GetHardwareSwitchingCost(from_hardware, + total_size_transferred); +} + +float GetQuantDequantCost(InferenceType from_inference_type, + InferenceType to_inference_type, + func::CallOp from_graph, func::CallOp to_graph) { + // Same inference type, no dequant/quant happens. + if (from_inference_type == to_inference_type) return 0; + + const int64_t total_element_count_transferred = + GetTransferredElementCount(from_graph, to_graph); + + if (from_inference_type == FLOAT || from_inference_type == HYBRID) { + // FLOAT <-> HYBRID will have no quant/dequant as well. + if (to_inference_type == FLOAT || to_inference_type == HYBRID) { + return 0; + } else if (to_inference_type == QUANTIZED_INT8 || + to_inference_type == QUANTIZED_UINT8) { + // QUANT path. + return kQuantCost * total_element_count_transferred; + } + } + + if (from_inference_type == QUANTIZED_INT8 || + from_inference_type == QUANTIZED_UINT8) { + // Dequant path. + if (to_inference_type == FLOAT || to_inference_type == HYBRID) { + return kDequantCost * total_element_count_transferred; + } else if (to_inference_type == QUANTIZED_INT8 || + to_inference_type == QUANTIZED_UINT8) { + // Requant path. + return kRequantCost * total_element_count_transferred; + } + } + + // Default quant/dequant/requant cost. + return kDefaultFixedValuedCost; +} + +std::unique_ptr> CreateGetOpCostPass() { + return std::make_unique(); +} + +static PassRegistration pass; + +} // namespace tac +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/cost_model.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/cost_model.h new file mode 100644 index 0000000000000000000000000000000000000000..89998968d5c16990e8dce42e82a5cbcbc805e488 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/cost_model.h @@ -0,0 +1,64 @@ +/* Copyright 2021 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_TRANSFORMS_COST_MODEL_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_TRANSFORMS_COST_MODEL_H_ + +#include + +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/targets.h" + +namespace mlir { +namespace TFL { +namespace tac { + +// TODO(renjieliu): We need to come up with a better strategy to do cost +// estimatation. Maybe build a big lookup table for all the ops. + +// TODO(renjieliu): We need to consider what's the default value if we cannot +// analyze the cost. + +// ================== Interface ======================== + +// Get the estimated cost for the op under the given hardware spec senario. +float GetCostForOp(Operation* op, const std::string& hardware); + +// Get the estimated cost for the whole function under the given hardware. +float GetCostForFunc(func::FuncOp* func, const std::string& hardware); + +// Get the transfer cost given from & to hardware info. +// We will only calculate for the "necessary" tensor transferred. +// from_graph & to_graph are used to compute the "necessary" tensors. +// from_graph +// / \ \ +// out1 out2 out3 +// \ / +// to_graph +// So only out2 & out3 are counted. +float GetTransferCost(const std::string& from_hardware_str, + const std::string& to_hardware_str, + func::CallOp from_graph, func::CallOp to_graph); + +// Get the cross quantization/dequantization boundary cost. +float GetQuantDequantCost(InferenceType from_inference_type, + InferenceType to_inference_type, + func::CallOp from_graph, func::CallOp to_graph); + +} // namespace tac +} // namespace TFL +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_TRANSFORMS_COST_MODEL_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/device_transform.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/device_transform.cc new file mode 100644 index 0000000000000000000000000000000000000000..c4d606340e8184bb544f89c1133bb7438d0dfd63 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/device_transform.cc @@ -0,0 +1,220 @@ +/* Copyright 2021 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/compiler/mlir/lite/experimental/tac/transforms/device_transform.h" + +#include +#include + +#include "llvm/ADT/SmallVector.h" +#include "llvm/Support/Casting.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Dialect/Quant/IR/QuantTypes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/targets.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/utils.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/hardwares/target_hardware.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/transforms/device_transform_gpu.h" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" + +namespace mlir { +namespace TFL { +namespace tac { +namespace { +#include "tensorflow/compiler/mlir/lite/experimental/tac/transforms/generated_transform_patterns.inc" +} // namespace + +RewritePatternSet GetHardwareRewritePatterns(MLIRContext* context, + const std::string& hardware) { + auto* devce_hardware = GetTargetHardware(hardware); + if (devce_hardware == nullptr) return {context}; + return devce_hardware->GetTransformations(context); +} + +bool IsSupported(Operation* op, const std::string& hardware) { + auto* devce_hardware = GetTargetHardware(hardware); + if (devce_hardware == nullptr) return {}; + return devce_hardware->IsOpSupported(op); +} + +// ================== Convert Quantized Op ============================ + +// Walk through the func and convert the quantize ops to their float version. +void ConvertQuantizedOpToFloat(mlir::func::FuncOp func, OpBuilder* builder) { + func.walk([&](Operation* op) { + // TODO(renjieliu): Find a generic way to deal with const ops. + if (op->hasTrait() || + llvm::isa(op) || + llvm::isa(op)) + return; + + bool int8_type_observed = false; + bool uint8_type_observed = false; + for (auto& input : op->getOpOperands()) { + auto input_type = input.get().getType(); + if (IsQI8Type(input_type)) { + int8_type_observed = true; + } else if (IsQUI8Type(input_type)) { + uint8_type_observed = true; + } + } + + // TODO(renjieliu): We probably should check whether the op supports float + // execution to be safe. Although normally they should support float + // execution. Not Quantized ops. + if (!int8_type_observed && !uint8_type_observed) return; + + // Insert dequantize ops for every quantized input. + SmallVector dequantized_inputs; + for (auto& input : op->getOpOperands()) { + auto input_type = input.get().getType(); + if (IsQI8Type(input_type) || IsQUI8Type(input_type) || + IsQI32Type(input_type)) { + auto dequantized_input_type = + mlir::quant::QuantizedType::castToExpressedType(input_type); + builder->setInsertionPoint(op); + auto dequantize_op = TFL::DequantizeOp::create( + *builder, op->getLoc(), dequantized_input_type, input.get()); + dequantized_inputs.push_back(dequantize_op); + } else { + dequantized_inputs.push_back(input.get()); + } + } + + // Result types. + SmallVector result_types; + for (auto result_type : op->getResultTypes()) { + if (IsQI8Type(result_type) || IsQUI8Type(result_type)) { + auto dequantized_result_type = + mlir::quant::QuantizedType::castToExpressedType(result_type); + result_types.push_back(dequantized_result_type); + } else { + result_types.push_back(result_type); + } + } + + // Build the new float-versioned op. + OperationState state(op->getLoc(), op->getName()); + state.operands = dequantized_inputs; + state.types = result_types; + state.attributes = op->getAttrs(); + state.successors = op->getSuccessors(); + builder->setInsertionPoint(op); + Operation* new_op = builder->create(state); + + // Insert quantize ops for every outputs and rewrite. + for (int i = 0; i < op->getNumResults(); ++i) { + auto result = op->getResult(i); + auto result_type = result.getType(); + + Value new_result = new_op->getResult(i); + if (IsQI8Type(result_type) || IsQUI8Type(result_type)) { + builder->setInsertionPoint(op); + TFL::QuantizeOp quant_op = + TFL::QuantizeOp::create(*builder, op->getLoc(), result_type, + new_result, TypeAttr::get(result_type)); + new_result = quant_op.getResult(); + } + + // Rewire the outputs. + result.replaceAllUsesWith(new_result); + } + + // Remove the old op. + op->erase(); + }); +} + +// Fold quantized i32 (normally bias) into their float values. +struct FoldQuantizedI32ToFloat : public OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(TFL::DequantizeOp dequant_op, + PatternRewriter& rewriter) const override { + // We only fold i32 -> float pattern. + auto input = dequant_op.getInput().getDefiningOp(); + if (!input) return failure(); + + auto input_dequant = llvm::dyn_cast_or_null(input); + if (!input_dequant) return failure(); + + if (!IsQI32Type(input_dequant.getType())) return failure(); + + auto output_type = + mlir::dyn_cast_or_null(dequant_op.getOutput().getType()); + if (!output_type || !output_type.getElementType().isF32()) return failure(); + + auto input_type = mlir::dyn_cast(input_dequant.getType()); + // TODO(renjieliu): support UniformQuantizedPerAxisType. + auto q_type = mlir::dyn_cast_or_null( + input_type.getElementType()); + if (!q_type) return failure(); + + const float scale = q_type.getScale(); + const float zp = q_type.getZeroPoint(); + + auto input_values = input_dequant.getValue(); + + // mapValues always takes a function returning APInt, even when the output + // is actually float. + using DequantizeFuncType = llvm::APInt(const llvm::APInt&); + auto dequantize_func = [&](const APInt& ap_int_value) -> APInt { + const int64_t int_value = ap_int_value.getSExtValue(); + + const float real = (int_value - zp) * scale; + + auto real_int = absl::bit_cast(real); + return APInt(/*numBits=*/32, real_int); + }; + + auto dequant_values = + mlir::cast(input_values) + .mapValues(Float32Type::get(rewriter.getContext()), + llvm::function_ref(dequantize_func)); + rewriter.replaceOpWithNewOp(dequant_op, dequant_op.getType(), + dequant_values); + + return success(); + } +}; + +// If the quant op has no consumer, we will remove them. +struct RemoveUnusedQuant : public OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(TFL::QuantizeOp quant_op, + PatternRewriter& rewriter) const override { + if (!quant_op.getResult().use_empty()) return failure(); + + rewriter.eraseOp(quant_op); + return success(); + } +}; + +void OptimizeQuantizedOpToFloat(func::FuncOp func, MLIRContext* context) { + RewritePatternSet patterns(func.getContext()); + patterns + .add( + context); + (void)applyPatternsGreedily(func, std::move(patterns)); +} + +} // namespace tac +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/device_transform.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/device_transform.h new file mode 100644 index 0000000000000000000000000000000000000000..5777b4b44b22085aadef4b24f67701840e1b1263 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/device_transform.h @@ -0,0 +1,49 @@ +/* Copyright 2021 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_TRANSFORMS_DEVICE_TRANSFORM_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_TRANSFORMS_DEVICE_TRANSFORM_H_ + +#include + +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/targets.h" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" + +namespace mlir { +namespace TFL { +namespace tac { + +// Returns true if 'op' is supported to run on 'hardware'. +bool IsSupported(Operation* op, const std::string& hardware); + +// Return proper rewriter patterns for different hardwares. +RewritePatternSet GetHardwareRewritePatterns(MLIRContext* context, + const std::string& hardware); + +// Convert quantized ops to float, this will essentially insert dequantize & +// quantize pair around the op. +void ConvertQuantizedOpToFloat(func::FuncOp func, OpBuilder* builder); + +// This will optimize the quantized ops -> float graph. +void OptimizeQuantizedOpToFloat(func::FuncOp func, MLIRContext* context); + +} // namespace tac +} // namespace TFL +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_TRANSFORMS_DEVICE_TRANSFORM_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/device_transform_gpu.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/device_transform_gpu.cc new file mode 100644 index 0000000000000000000000000000000000000000..40150ac0800e24ce41ba7b52fbc3ce8fbb234737 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/device_transform_gpu.cc @@ -0,0 +1,85 @@ +/* Copyright 2021 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/compiler/mlir/lite/experimental/tac/transforms/device_transform_gpu.h" + +#include +#include + +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/raw_ostream.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/targets.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/hardwares/gpu_hardware.h" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" + +namespace mlir { +namespace TFL { +namespace tac { +namespace { + +struct DeviceTransformGPUPass + : public mlir::PassWrapper> { + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(DeviceTransformGPUPass) + + llvm::StringRef getArgument() const final { + return "tfl-device-transform-gpu"; + } + llvm::StringRef getDescription() const final { + return "Suitable transformation for gpu only."; + } + void getDependentDialects(DialectRegistry& registry) const override { + registry.insert(); + } + void runOnOperation() override; +}; + +void DeviceTransformGPUPass::runOnOperation() { + auto func = getOperation(); + auto* ctx = &getContext(); + RewritePatternSet patterns = GetHardwareRewritePatternsGPU(ctx); + (void)applyPatternsGreedily(func, std::move(patterns)); +} + +} // namespace + +RewritePatternSet GetHardwareRewritePatternsGPU(MLIRContext* context) { + GpuHardware gpu_hardware; + return gpu_hardware.GetTransformations(context); +} + +std::unique_ptr> CreateDeviceTransformGPUPass() { + return std::make_unique(); +} + +static PassRegistration pass; + +} // namespace tac +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/device_transform_gpu.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/device_transform_gpu.h new file mode 100644 index 0000000000000000000000000000000000000000..6ffc681054a1aadb63bd6c2f563839731f2459a4 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/device_transform_gpu.h @@ -0,0 +1,34 @@ +/* Copyright 2021 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_TRANSFORMS_DEVICE_TRANSFORM_GPU_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_TRANSFORMS_DEVICE_TRANSFORM_GPU_H_ + +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project + +namespace mlir { +namespace TFL { +namespace tac { + +// nit: Returns all the gpu suitable transformation patterns. +RewritePatternSet GetHardwareRewritePatternsGPU(MLIRContext* context); + +} // namespace tac +} // namespace TFL +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_TRANSFORMS_DEVICE_TRANSFORM_GPU_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/device_transform_nnapi.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/device_transform_nnapi.cc new file mode 100644 index 0000000000000000000000000000000000000000..39d83c0a75e0a886d65ce39e28ffcf99c170cef2 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/device_transform_nnapi.cc @@ -0,0 +1,75 @@ +/* Copyright 2021 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 "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/raw_ostream.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/targets.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/hardwares/nnapi_hardware.h" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" + +namespace mlir { +namespace TFL { +namespace tac { +namespace { + +struct DeviceTransformNNAPIPass + : public mlir::PassWrapper> { + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(DeviceTransformNNAPIPass) + + llvm::StringRef getArgument() const final { + return "tfl-device-transform-nnapi"; + } + llvm::StringRef getDescription() const final { + return "Suitable transformation for nnapi only."; + } + void getDependentDialects(DialectRegistry& registry) const override { + registry.insert(); + } + void runOnOperation() override; +}; + +void DeviceTransformNNAPIPass::runOnOperation() { + auto func = getOperation(); + auto* ctx = &getContext(); + NNAPIHardware nnapi_hardware; + RewritePatternSet patterns = nnapi_hardware.GetTransformations(ctx); + (void)applyPatternsGreedily(func, std::move(patterns)); +} + +} // namespace + +static PassRegistration pass; + +} // namespace tac +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/device_transform_patterns.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/device_transform_patterns.cc new file mode 100644 index 0000000000000000000000000000000000000000..67809e47e0e4f5b9134e0e9d26fba115767dd1b4 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/device_transform_patterns.cc @@ -0,0 +1,706 @@ +/* Copyright 2021 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/compiler/mlir/lite/experimental/tac/transforms/device_transform_patterns.h" + +#include +#include + +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/Support/Casting.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Dialect/Quant/IR/QuantTypes.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/Matchers.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/targets.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/utils.h" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/lite/utils/attribute_utils.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" +#include "tensorflow/compiler/mlir/tensorflow/utils/verification_utils.h" + +namespace mlir { +namespace TFL { +namespace tac { +namespace { + +// ================== Common ======================== + +// Converts any IntegerAttr to an IntegerAttr of an i32 type. +// The value won't change in the new attribute, but if the value is out of +// the bound of i32, the function returns a failure. +LogicalResult ConvertToI32Attr(IntegerAttr attr, IntegerAttr* attr_i32) { + if (attr.getType().isInteger(/*width=*/32)) { + *attr_i32 = attr; + return success(); + } + + int64_t value = attr.getInt(); + if (value > std::numeric_limits::max() || + value < std::numeric_limits::min()) { + return failure(); + } + + *attr_i32 = IntegerAttr::get( + IntegerType::get(attr.getContext(), /*width=*/32), value); + return success(); +} + +TFL::ReshapeOp InsertReshapeOp(Location loc, Value input, Type element_type, + llvm::ArrayRef new_shape_array, + OpBuilder* builder) { + auto reshape_shape_type = mlir::RankedTensorType::get( + new_shape_array.size(), builder->getIntegerType(32)); + + // This is to workaround the unnecessary cast i64 -> i32. :( + // TODO(renjieliu): Revisit this later. + SmallVector new_shape_array_i32; + for (auto size : new_shape_array) { + new_shape_array_i32.push_back( + ShapedType::isDynamic(size) ? -1 : static_cast(size)); + } + auto new_shape_attr = + mlir::DenseIntElementsAttr::get(reshape_shape_type, new_shape_array_i32); + + auto new_shape = TFL::ConstOp::create(*builder, loc, new_shape_attr); + + auto reshape_out_type = RankedTensorType::get(new_shape_array, element_type); + return TFL::ReshapeOp::create(*builder, loc, reshape_out_type, input, + new_shape); +} + +LogicalResult EnsureBias(Operation* op, int bias_idx, + PatternRewriter& rewriter) { + auto bias = op->getOperand(bias_idx); + + if (!mlir::isa(bias.getType())) return failure(); + + // Proceed to create a zero bias. + auto output = op->getResult(0); + auto output_type = mlir::dyn_cast_or_null(output.getType()); + if (!output_type) return failure(); + + // bias should be a vector sized of the last output dim. + int64_t num_units = output_type.getDimSize(output_type.getRank() - 1); + auto bias_type = + mlir::RankedTensorType::get({num_units}, output_type.getElementType()); + + mlir::DenseElementsAttr bias_attr; + if (output_type.getElementType().isF32()) { + float val = 0.0; + bias_attr = mlir::DenseFPElementsAttr::get(bias_type, val); + } else { + // TODO(renjieliu): Refactor this and share the logic with + // CreateConstOpWithSingleValue. Also, make sure it works with QConst. + return failure(); + } + + auto zero_bias = TFL::ConstOp::create(rewriter, op->getLoc(), bias_attr); + op->setOperand(bias_idx, zero_bias); + + return success(); +} + +TF::ConstOp PadConstValues(Operation* input_op, int value_to_pad, + int pad_dimensions, Location loc, + OpBuilder* builder) { + if (input_op == nullptr) return nullptr; + + mlir::DenseIntElementsAttr attr; + if (!matchPattern(input_op, m_Constant(&attr))) { + return nullptr; + } + + auto value_shape_type = mlir::RankedTensorType::get( + {pad_dimensions}, builder->getIntegerType(32)); + + SmallVector value_i32; + value_i32.reserve(pad_dimensions); + for (int i = 0; i < pad_dimensions - attr.getNumElements(); ++i) { + value_i32.push_back(value_to_pad); + } + for (const auto& size : attr) { + value_i32.push_back(static_cast(size.getSExtValue())); + } + auto new_value_i32_attr = + mlir::DenseIntElementsAttr::get(value_shape_type, value_i32); + + return TF::ConstOp::create(*builder, loc, new_value_i32_attr); +} + +SmallVector SliceOutputs(Operation* split_op, Value input, + RankedTensorType input_type, int split_dim, + int num_splits, PatternRewriter* rewriter) { + SmallVector slice_outputs; + int begin = 0; + for (int i = 0; i < num_splits; ++i) { + // Create slice op. + // Populate begin & size. + SmallVector slice_begin; + SmallVector slice_size; + auto current_output = split_op->getResult(i); + auto current_output_type = + mlir::cast(current_output.getType()); + for (int d = 0; d < input_type.getRank(); ++d) { + if (d == split_dim) { + // Split dimension. + slice_begin.push_back(begin); + int64_t size = current_output_type.getDimSize(d); + slice_size.push_back(size); + begin += size; + } else { + slice_begin.push_back(0); + // -1 means every elements. + slice_size.push_back(-1); + } + } + + auto slice_type = mlir::RankedTensorType::get(slice_begin.size(), + rewriter->getIntegerType(32)); + auto slice_begin_attr = + mlir::DenseIntElementsAttr::get(slice_type, slice_begin); + auto slice_size_attr = + mlir::DenseIntElementsAttr::get(slice_type, slice_size); + + auto slice_begin_const = + TFL::ConstOp::create(*rewriter, split_op->getLoc(), slice_begin_attr); + auto slice_size_const = + TFL::ConstOp::create(*rewriter, split_op->getLoc(), slice_size_attr); + + auto slice_op = + TFL::SliceOp::create(*rewriter, split_op->getLoc(), current_output_type, + input, slice_begin_const, slice_size_const); + + // Rewire output. + slice_outputs.push_back(slice_op.getResult()); + } + return slice_outputs; +} + +} // namespace + +// ================== Pack ======================== + +LogicalResult LowerPackIntoConcatReshape::matchAndRewrite( + TFL::PackOp pack_op, PatternRewriter& rewriter) const { + // Pack op should have same shape type. + SmallVector pack_inputs(pack_op.getValues()); + auto input_type = mlir::dyn_cast(pack_inputs[0].getType()); + if (!input_type) return failure(); + + // Figure out output shapes. + SmallVector concat_out_shape; + SmallVector pack_out_shape; + + const int64_t rank = input_type.getRank(); + int64_t pack_axis = pack_op.getAxis(); + size_t count = pack_inputs.size(); + if (pack_axis < 0) { + pack_axis += rank; + } + + // Concat out shape. + for (int i = 0; i < rank; ++i) { + int64_t dim_size = input_type.getDimSize(i); + if (i == pack_axis) { + dim_size *= count; + } + concat_out_shape.push_back(dim_size); + } + + // Pack out shape. + int j = 0; + for (int i = 0; i < rank + 1; ++i) { + if (i == pack_axis) { + pack_out_shape.push_back(count); + } else { + pack_out_shape.push_back(input_type.getDimSize(j)); + j++; + } + } + + if (failed(TF::VerifyShapeOfReshapeOp(pack_out_shape))) return failure(); + + // Insert the concat op. + auto concat_out_type = + RankedTensorType::get(concat_out_shape, input_type.getElementType()); + auto concat_op = + TFL::ConcatenationOp::create(rewriter, pack_op.getLoc(), concat_out_type, + pack_inputs, pack_op.getAxis(), "NONE"); + + auto reshape_op = + InsertReshapeOp(pack_op.getLoc(), concat_op, input_type.getElementType(), + pack_out_shape, &rewriter); + + // Rewire output & get rid of the pack op. + rewriter.replaceOp(pack_op, reshape_op.getResult()); + return success(); +} + +// ================== squared_difference ======================== + +LogicalResult SquaredDifference::matchAndRewrite( + TFL::SquaredDifferenceOp squared_diff_op, PatternRewriter& rewriter) const { + auto x = squared_diff_op.getLhs(); + auto y = squared_diff_op.getRhs(); + auto x_type = mlir::dyn_cast(x.getType()); + auto y_type = mlir::dyn_cast(y.getType()); + if (!x_type || !y_type) return failure(); + if (x_type.getShape() != y_type.getShape()) return failure(); + + auto result_type = squared_diff_op.getType(); + if (!result_type) return failure(); + + auto sub_op = + TF::SubOp::create(rewriter, squared_diff_op.getLoc(), result_type, x, y); + auto mul_op = + TF::MulOp::create(rewriter, squared_diff_op.getLoc(), result_type, + sub_op.getResult(), sub_op.getResult()); + rewriter.replaceOp(squared_diff_op, mul_op.getResult()); + + return success(); +} + +// ================== split ======================== + +LogicalResult UnrollSplit::matchAndRewrite(TFL::SplitOp split_op, + PatternRewriter& rewriter) const { + auto num_splits = split_op.getNumSplits(); + auto input = split_op.getValue(); + auto input_type = mlir::dyn_cast(input.getType()); + if (input_type == nullptr || !input_type.hasStaticShape()) return failure(); + + for (auto result : split_op.getResults()) { + auto result_type = mlir::dyn_cast(result.getType()); + if (result_type == nullptr) return failure(); + } + + auto output = split_op.getResult(0); + auto output_type = mlir::cast(output.getType()); + + // TODO(renjieliu): change to use split_dim when we raise the constants + // as well. + int64_t split_dim = -1; + for (int64_t d = 0; d < input_type.getRank(); ++d) { + if (input_type.getDimSize(d) != output_type.getDimSize(d)) split_dim = d; + } + + const SmallVector& slice_outputs = SliceOutputs( + split_op, input, input_type, split_dim, num_splits, &rewriter); + rewriter.replaceOp(split_op, slice_outputs); + return success(); +} + +// ================== splitV ======================== + +LogicalResult UnrollSplitV::matchAndRewrite(TFL::SplitVOp splitv_op, + PatternRewriter& rewriter) const { + // We need to make sure both splits & split dim are constants. + auto splits = splitv_op.getSizeSplits().getDefiningOp(); + mlir::DenseIntElementsAttr splits_attr; + if (!splits || !matchPattern(splits, m_Constant(&splits_attr))) + return failure(); + + auto split_dim = splitv_op.getSplitDim().getDefiningOp(); + mlir::ElementsAttr split_dim_attr; + if (!split_dim || !matchPattern(split_dim, m_Constant(&split_dim_attr))) + return failure(); + + auto input = splitv_op.getValue(); + auto input_type = mlir::dyn_cast_or_null(input.getType()); + if (!input_type || !input_type.hasRank()) return failure(); + + for (auto result : splitv_op.getResults()) { + auto result_type = mlir::dyn_cast(result.getType()); + if (result_type == nullptr) return failure(); + } + + const int64_t rank = input_type.getRank(); + + IntegerAttr dim_int = ExtractSingleElementAsInteger(split_dim_attr); + + // "axis" operand could be a i64 tensor. Resolve it here. + IntegerAttr dim_i32; + if (failed(ConvertToI32Attr(dim_int, &dim_i32))) return failure(); + + int dim = dim_i32.getInt(); + if (dim < 0) dim += rank; + + const SmallVector& slice_outputs = SliceOutputs( + splitv_op, input, input_type, dim, splitv_op.getNumSplits(), &rewriter); + rewriter.replaceOp(splitv_op, slice_outputs); + + return success(); +} + +// ================== conv_2d ======================== + +LogicalResult EnsureBiasForConv2d::matchAndRewrite( + TFL::Conv2DOp conv_op, PatternRewriter& rewriter) const { + return EnsureBias(conv_op, 2, rewriter); +} + +// ================== slice ============================ + +// If a slice op has < 4d dimension, will pad it to 4d. +LogicalResult PadSlice::matchAndRewrite(TFL::SliceOp slice_op, + PatternRewriter& rewriter) const { + // We have to know the shape of the input, as well as the begin/size. + // also, begin and size have to be constants. + auto input = slice_op.getInput(); + auto input_type = mlir::dyn_cast_or_null(input.getType()); + if (!input_type || !input_type.hasStaticShape()) return failure(); + + if (input_type.getRank() >= 4) return failure(); + + auto begin = slice_op.getBegin(); + auto begin_type = mlir::dyn_cast_or_null(begin.getType()); + if (!begin_type || !begin_type.hasStaticShape()) return failure(); + + auto size = slice_op.getSize(); + auto size_type = mlir::dyn_cast_or_null(size.getType()); + if (!size_type || !size_type.hasStaticShape()) return failure(); + + auto output_type = + mlir::dyn_cast_or_null(slice_op.getType()); + if (!output_type || !output_type.hasStaticShape()) return failure(); + + // Pad 0s in front of the begin. + TF::ConstOp new_begin = + PadConstValues(begin.getDefiningOp(), 0, 4, slice_op.getLoc(), &rewriter); + if (!new_begin) return failure(); + + // Pad 1s in front of the size. + TF::ConstOp new_size = + PadConstValues(size.getDefiningOp(), 1, 4, slice_op.getLoc(), &rewriter); + if (!new_size) return failure(); + + // Reshape the input to 4d. + SmallVector new_shape; + const int current_rank = input_type.getRank(); + for (int i = 0; i < 4 - current_rank; ++i) { + new_shape.push_back(1); + } + for (auto size : input_type.getShape()) { + new_shape.push_back(size); + } + + auto reshape_op = + InsertReshapeOp(slice_op.getLoc(), input, input_type.getElementType(), + new_shape, &rewriter); + + // Replace with the new slice op. + SmallVector new_output_shape; + for (int i = 0; i < 4 - current_rank; ++i) { + new_output_shape.push_back(1); + } + for (auto size : output_type.getShape()) { + new_output_shape.push_back(size); + } + + RankedTensorType new_output_type = + RankedTensorType::get(new_output_shape, output_type.getElementType()); + + auto new_slice = + TFL::SliceOp::create(rewriter, slice_op.getLoc(), new_output_type, + reshape_op, new_begin, new_size); + + // Append a reshape at the bottom. + auto output_reshape_op = InsertReshapeOp(slice_op.getLoc(), new_slice, + output_type.getElementType(), + output_type.getShape(), &rewriter); + rewriter.replaceOp(slice_op, output_reshape_op.getResult()); + + return success(); +} + +// ================== fully_connected ======================== + +// TFL fully_connected basically does: +// Weight * Input + bias. +// Input layout is : [..., depth] +// Weight layout is : [output, depth] +// Bias is [output]. +// +// While conv2d is: +// Filter: [NHWC] +// Input is also: [NHWC] +// Bias is [N] +// +// So to perform the transform, we need to insert a few reshape ops: +// +// Input weight bias +// \ / / +// FC +// | +// output +// +// | +// \/ +// +// Input weight +// | | +// Reshape Reshape bias +// | | / +// conv +// | +// reshape +// | +// output +LogicalResult FullyConnectedToConv::matchAndRewrite( + TFL::FullyConnectedOp fc_op, PatternRewriter& rewriter) const { + // We have to know the shape of the input. + auto input = fc_op.getInput(); + auto input_type = mlir::dyn_cast_or_null(input.getType()); + if (!input_type || !input_type.hasStaticShape()) return failure(); + + // We have to know the shape of the weight. + auto weight = fc_op.getFilter(); + auto weight_type = mlir::dyn_cast_or_null(weight.getType()); + if (!weight_type || !weight_type.hasStaticShape()) return failure(); + + // We have to know the shape of the output as well. + auto output = fc_op.getResult(0); + auto output_type = mlir::dyn_cast_or_null(output.getType()); + if (!output_type || !output_type.hasStaticShape()) return failure(); + + // Insert a reshape after the input. + // Since the input maybe more than 2-d, we may collect the flat size of the + // input then reshape into [1, 1, flat_size / depth, depth]. + const int64_t depth = input_type.getDimSize(input_type.getRank() - 1); + const int64_t flat_size = input_type.getNumElements(); + const int64_t width = flat_size / depth; + SmallVector input_new_shape({1, 1, width, depth}); + auto reshaped_input = + InsertReshapeOp(fc_op.getLoc(), input, input_type.getElementType(), + input_new_shape, &rewriter); + + // Insert a reshape after the weight. + // We will reshape the weight into [output, 1, 1, depth] + const int64_t output_size = weight_type.getDimSize(0); + SmallVector weight_new_shape({output_size, 1, 1, depth}); + auto reshaped_weight = + InsertReshapeOp(fc_op.getLoc(), weight, weight_type.getElementType(), + weight_new_shape, &rewriter); + + // Replace the fc with conv. + // The output would be [1, 1, width, output]. + auto conv_output_type = RankedTensorType::get({1, 1, width, output_size}, + output_type.getElementType()); + auto conv = TFL::Conv2DOp::create( + rewriter, fc_op.getLoc(), conv_output_type, reshaped_input, + reshaped_weight, fc_op.getBias(), rewriter.getI32IntegerAttr(1), + rewriter.getI32IntegerAttr(1), fc_op.getFusedActivationFunctionAttr(), + rewriter.getStringAttr("VALID"), rewriter.getI32IntegerAttr(1), + rewriter.getI32IntegerAttr(1)); + + // Insert a shape after the conv. + auto reshaped_conv = + InsertReshapeOp(fc_op.getLoc(), conv, output_type.getElementType(), + output_type.getShape(), &rewriter); + + rewriter.replaceOp(fc_op, reshaped_conv.getResult()); + + return success(); +} + +// ================== concat ============================ + +// If a concat op has < 4d dimension, will pad it to 4d. +LogicalResult PadConcat::matchAndRewrite(TFL::ConcatenationOp concat_op, + PatternRewriter& rewriter) const { + int rank = -1; + for (auto input : concat_op.getValues()) { + auto input_type = mlir::dyn_cast_or_null(input.getType()); + if (!input_type || !input_type.hasStaticShape()) return failure(); + + rank = input_type.getRank(); + } + + auto output_type = + mlir::dyn_cast_or_null(concat_op.getType()); + if (!output_type || !output_type.hasStaticShape()) return failure(); + + if (rank >= 4) return failure(); + + // All values should have the same rank. + // We will insert a reshape op after every input. + SmallVector reshape_ops; + for (auto input : concat_op.getValues()) { + auto input_type = mlir::cast(input.getType()); + // Get the new shape. + SmallVector new_shape; + for (int i = 0; i < 4 - rank; ++i) { + new_shape.push_back(1); + } + for (auto size : input_type.getShape()) { + new_shape.push_back(size); + } + + auto reshape_op = + InsertReshapeOp(concat_op.getLoc(), input, input_type.getElementType(), + new_shape, &rewriter); + reshape_ops.push_back(reshape_op.getResult()); + } + + // Deal with the axis. + // We don't need to handle axis < 0, since it's counting reversely. + int32_t axis = concat_op.getAxis(); + if (axis >= 0) { + axis += (4 - rank); + } + + // Replace with the new concat op. + SmallVector new_output_shape; + for (int i = 0; i < 4 - rank; ++i) { + new_output_shape.push_back(1); + } + for (auto size : output_type.getShape()) { + new_output_shape.push_back(size); + } + + RankedTensorType new_output_type = + RankedTensorType::get(new_output_shape, output_type.getElementType()); + + auto new_concat = TFL::ConcatenationOp::create( + rewriter, concat_op.getLoc(), new_output_type, reshape_ops, axis, + concat_op.getFusedActivationFunction()); + + // Append a reshape at the bottom. + auto output_reshape_op = InsertReshapeOp(concat_op.getLoc(), new_concat, + output_type.getElementType(), + output_type.getShape(), &rewriter); + rewriter.replaceOp(concat_op, output_reshape_op.getResult()); + + return success(); +} + +// ================== mean ======================== + +// Currently NNAPI does not support mean op with different scales (quantization +// cases), and in TFLite avg_pool will ensure the input & output has the same +// scales. +LogicalResult ReduceMeanToAvgPool::matchAndRewrite( + TFL::MeanOp mean_op, PatternRewriter& rewriter) const { + auto input = mean_op.getInput(); + auto input_type = mlir::dyn_cast_or_null(input.getType()); + // Only 4d is supported here. + if (!input_type || input_type.getRank() != 4) return failure(); + + // The axes has to be [1, 2]. + DenseElementsAttr axis_const; + if (!matchPattern(mean_op.getAxis(), m_Constant(&axis_const))) + return failure(); + if (axis_const.size() != 2) return failure(); + auto axis_values = axis_const.getValues(); + int i = 1; + for (auto axis_value : axis_values) { + if (axis_value != i++) return failure(); + } + + auto output = mean_op.getOutput(); + auto output_type = mlir::dyn_cast_or_null(output.getType()); + if (!output_type) return failure(); + + auto input_quantized_type = + quant::QuantizedType::getQuantizedElementType(input_type); + auto output_quantized_type = + quant::QuantizedType::getQuantizedElementType(output_type); + // If both the input & output types are non-quantized, they will be both + // nullptrs. + if (input_quantized_type != output_quantized_type) { + return failure(); + } + + int64_t batch = input_type.getDimSize(0); + int64_t height = input_type.getDimSize(1); + int64_t width = input_type.getDimSize(2); + int64_t channel = input_type.getDimSize(3); + + auto avg_pool_output_type = RankedTensorType::get( + {batch, 1, 1, channel}, input_type.getElementType()); + auto avg_pool = TFL::AveragePool2DOp::create( + rewriter, mean_op.getLoc(), avg_pool_output_type, input, + rewriter.getI32IntegerAttr(height), rewriter.getI32IntegerAttr(width), + rewriter.getStringAttr("VALID"), rewriter.getI32IntegerAttr(1), + rewriter.getI32IntegerAttr(1), rewriter.getStringAttr("NONE")); + + auto value_to_replace = avg_pool.getResult(); + + // If it's not keep dim, we need to insert a reshape after the average + // pool. + if (!mean_op.getKeepDims()) { + // Insert the reshape. + SmallVector new_shape({batch, channel}); + auto reshape_op = + InsertReshapeOp(mean_op.getLoc(), avg_pool.getResult(), + input_type.getElementType(), new_shape, &rewriter); + value_to_replace = reshape_op.getResult(); + } + + rewriter.replaceOp(mean_op, value_to_replace); + return success(); +} + +// Insert a "requant" op after the mean op if the mean has different scales for +// input & output. +// Please note: THIS IS NOT a mathmetically-equivalent transformation and it may +// loose accuracy, so we need to use this very very carefully. +LogicalResult InsertRequantForReduceMean::matchAndRewrite( + TFL::MeanOp mean_op, PatternRewriter& rewriter) const { + auto input = mean_op.getInput(); + auto input_type = mlir::dyn_cast_or_null(input.getType()); + if (!input_type) return failure(); + + // Only need to do this for quantized input. + auto input_quantized_type = + quant::QuantizedType::getQuantizedElementType(input_type); + if (!input_quantized_type) return failure(); + + auto output = mean_op.getOutput(); + auto output_type = mlir::dyn_cast_or_null(output.getType()); + if (!output_type) return failure(); + auto output_quantized_type = + quant::QuantizedType::getQuantizedElementType(output_type); + + // If the quantized type is the same, we don't need to do anything. + if (input_quantized_type == output_quantized_type) return failure(); + + auto new_output_type = + RankedTensorType::get(output_type.getShape(), input_quantized_type); + auto new_mean_op = + TFL::MeanOp::create(rewriter, mean_op->getLoc(), new_output_type, input, + mean_op.getAxis(), mean_op.getKeepDims()); + + // Insert a requant op. + rewriter.replaceOpWithNewOp( + mean_op, output_type, new_mean_op, mlir::TypeAttr::get(output_type)); + return success(); +} + +} // namespace tac +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/device_transform_patterns.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/device_transform_patterns.h new file mode 100644 index 0000000000000000000000000000000000000000..75df904b1da1b364fc8485a8a2444306c82c8888 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/device_transform_patterns.h @@ -0,0 +1,115 @@ +/* Copyright 2021 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_TRANSFORMS_DEVICE_TRANSFORM_PATTERNS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_TRANSFORMS_DEVICE_TRANSFORM_PATTERNS_H_ + +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" + +namespace mlir { +namespace TFL { +namespace tac { + +// TODO(renjieliu): add more patterns. + +// This basically: +// Pack => (Concat -> Reshape) +struct LowerPackIntoConcatReshape : public OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(TFL::PackOp pack_op, + PatternRewriter& rewriter) const override; +}; + +struct SquaredDifference : public OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(TFL::SquaredDifferenceOp squared_diff_op, + PatternRewriter& rewriter) const override; +}; + +// Unroll split into a bunch of slice ops. +struct UnrollSplit : public OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(TFL::SplitOp split_op, + PatternRewriter& rewriter) const override; +}; + +// Unroll splitv into a bunch of slice ops. +struct UnrollSplitV : public OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(TFL::SplitVOp splitv_op, + PatternRewriter& rewriter) const override; +}; + +// Ensure bias for conv2d op. +struct EnsureBiasForConv2d : public OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(TFL::Conv2DOp conv_op, + PatternRewriter& rewriter) const override; +}; + +// Pad slice to 4d. +struct PadSlice : public OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(TFL::SliceOp slice_op, + PatternRewriter& rewriter) const override; +}; + +// Fully connected to conv2d. +struct FullyConnectedToConv : public OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(TFL::FullyConnectedOp fc_op, + PatternRewriter& rewriter) const override; +}; + +// Pad concat to 4d. +struct PadConcat : public OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(TFL::ConcatenationOp concat_op, + PatternRewriter& rewriter) const override; +}; + +// Convert reduce mean 4d to avg pool. +struct ReduceMeanToAvgPool : public OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(TFL::MeanOp mean_op, + PatternRewriter& rewriter) const override; +}; + +// Insert Requant ops for reduce_mean. +struct InsertRequantForReduceMean : public OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(TFL::MeanOp mean_op, + PatternRewriter& rewriter) const override; +}; + +} // namespace tac +} // namespace TFL +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_TRANSFORMS_DEVICE_TRANSFORM_PATTERNS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/fold_constants_to_subgraph.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/fold_constants_to_subgraph.cc new file mode 100644 index 0000000000000000000000000000000000000000..a36658d490db938c2353b4eaff612cf1fb74eb3e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/fold_constants_to_subgraph.cc @@ -0,0 +1,189 @@ +/* Copyright 2021 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 "absl/strings/str_cat.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/DenseSet.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/Casting.h" +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "stablehlo/dialect/StablehloOps.h" // from @stablehlo +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/subgraph.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/transforms/passes.h" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" + +namespace mlir { +namespace TFL { +namespace tac { +namespace { + +// This pass is used to fold tfl.const ops to each subgraph (func::FuncOp): +// See the example below: +// +// In main: +// %0 = tfl.const... +// %1 = tfl.const... +// %2 = call func_1(..., %0,...) +// %3 = call func_2(..., %0, ..., %1...) +// ... +// +// Then those consts will be copied into each function and replace their usage. +// func_1: +// %0 = tfl.const... +// func_2: +// %0 = tfl.const... +// %1 = tfl.const... +class FoldConstantsToSubgraphPass + : public mlir::PassWrapper> { + public: + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(FoldConstantsToSubgraphPass) + + llvm::StringRef getArgument() const final { + return "tfl-fold-constants-to-subgraph"; + } + llvm::StringRef getDescription() const final { + return "Fold constants into each subgraph."; + } + FoldConstantsToSubgraphPass() = default; + FoldConstantsToSubgraphPass(const FoldConstantsToSubgraphPass& other) { + this->fold_all_constants_flag_ = other.fold_all_constants_flag_; + } + explicit FoldConstantsToSubgraphPass(bool fold_all_constants) { + fold_all_constants_flag_ = fold_all_constants; + } + + private: + void runOnOperation() override; + + Option fold_all_constants_flag_{ + *this, "fold-all-constants", + llvm::cl::desc("Whether to fold all constants or just i32."), + llvm::cl::init(false)}; +}; + +void CopyConstantIntoFunc(int argument_index, Operation* const_op, + func::FuncOp func) { + assert((llvm::isa(const_op)) && + "Expect QConst or Const op."); + OpBuilder builder(func.getBody()); + auto cloned_const_op = const_op->clone(); + cloned_const_op->setLoc(func.getBody().getLoc()); + builder.insert(cloned_const_op); + // Rewire the usage. + func.getArgument(argument_index) + .replaceAllUsesWith(cloned_const_op->getResult(0)); +} + +bool IsConstOrQConstInt(Operation* op) { + if (!llvm::isa(op)) + return false; + + if (auto arith_const_op = dyn_cast_or_null(op)) { + // arith ConstOp path. + auto type = + mlir::cast(arith_const_op.getType()).getElementType(); + if (!type.isInteger(32) && !type.isInteger(64)) return false; + } else if (auto shlo_const_op = dyn_cast_or_null(op)) { + auto type = + mlir::cast(shlo_const_op.getType()).getElementType(); + if (!type.isInteger(32) && !type.isInteger(64)) return false; + } else if (auto const_op = dyn_cast_or_null(op)) { + // ConstOp path. + auto type = mlir::cast(const_op.getType()).getElementType(); + if (!type.isInteger(32) && !type.isInteger(64)) return false; + } else { + // QConstOp path. + auto qconst_op = dyn_cast(op); + auto type = + quant::QuantizedType::getQuantizedElementType(qconst_op.getType()); + if (type.getStorageTypeIntegralWidth() != 32) { + return false; + } + } + return true; +} + +void FoldConstantsToSubgraphPass::runOnOperation() { + auto module = getOperation(); + + for (auto fn : module.getOps()) { + fn.walk([&](Operation* op) { + if (!llvm::isa(op)) + return; + + // We only fold int32/int64 for Const and i32 for QConst if not specify + // all constants flag. (Since they're more like "configs" or i32 biases.) + // We will fold every const ops (and q_const ops) if we speicfy the + // fold_all_constants_flag. + if (!fold_all_constants_flag_) { + if (!IsConstOrQConstInt(op)) return; + } + + for (auto consumer : op->getResult(0).getUsers()) { + auto consumer_call = llvm::dyn_cast_or_null(consumer); + + if (!consumer_call) continue; + + auto function_name = consumer_call.getCallee(); + + // Locate the argument position of the use. + int argument_index = -1; + for (int i = 0; i < consumer_call.getNumOperands(); ++i) { + if (consumer_call.getOperand(i) == op->getResult(0)) { + argument_index = i; + break; + } + } + + // Copy the const into the consumer func and replace their usages. + func::FuncOp func = module.lookupSymbol(function_name); + + CopyConstantIntoFunc(argument_index, op, func); + } + }); + } +} + +} // namespace + +std::unique_ptr> CreateFoldConstantsToSubgraphPass( + bool fold_all_constants) { + return std::make_unique(fold_all_constants); +} + +static PassRegistration pass; + +} // namespace tac +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/get_alternative_subgraph.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/get_alternative_subgraph.cc new file mode 100644 index 0000000000000000000000000000000000000000..c90d29a70c6ef7e4e2823da8f3cb120340b89ee7 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/get_alternative_subgraph.cc @@ -0,0 +1,314 @@ +/* Copyright 2021 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 "absl/strings/str_cat.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/DenseSet.h" +#include "llvm/ADT/SetVector.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/CommandLine.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Block.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/OperationSupport.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Interfaces/CallInterfaces.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/subgraph.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/targets.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/utils.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/hardwares/target_hardware.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/transforms/device_transform.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/transforms/passes.h" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" + +namespace mlir { +namespace TFL { +namespace tac { +namespace { + +// Given the function interface name and the InferenceDeviceType, return the +// new function name. +std::string GetFunctionImplName( + std::string interface_name, + const InferenceDeviceType& device_inference_type) { + return absl::StrCat(interface_name, "_", device_inference_type.hardware, "_", + GetInferenceString(device_inference_type.inference_type)); +} + +// For every device, we will do the following: +// If the inference type is quantized, we will try the float alternative. +// If it's float, we will just keep it as it is. +std::vector GetAllAlternativeInferenceDeviceType( + InferenceType inference_type, ArrayRef devices) { + std::vector all_device_inference_types; + for (const auto& device : devices) { + if (inference_type == QUANTIZED_INT8) { + all_device_inference_types.push_back({device, QUANTIZED_INT8}); + } else if (inference_type == QUANTIZED_UINT8) { + all_device_inference_types.push_back({device, QUANTIZED_UINT8}); + } + + // We will alway enable float. + all_device_inference_types.push_back({device, FLOAT}); + } + + return all_device_inference_types; +} + +// This pass will try to get alternative subgraph: +// Say a subgraph is annotated with CPU (it probably means the ops it contains +// cannot be run on other deviecs): +// +// We will try: +// 1) If we can do some mathmatically equaivalent transformation so this +// subgraph can be run on other devices. +// 2) We will other apply device-specifics optimizations as well, that includes +// maybe tensor layout transformation, device specific fusion, etc. +class AlternativeSubgraphPass + : public mlir::PassWrapper> { + public: + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(AlternativeSubgraphPass) + + llvm::StringRef getArgument() const final { + return "tfl-get-alternative-subgraph"; + } + llvm::StringRef getDescription() const final { + return "Get alternative subgraph representation (if appliable) for all the " + "given devices, will by default include the cpu implementation."; + } + AlternativeSubgraphPass() = default; + AlternativeSubgraphPass(const AlternativeSubgraphPass&) {} + explicit AlternativeSubgraphPass(llvm::ArrayRef device_specs) { + device_specs_flag_ = device_specs; + } + + private: + void runOnOperation() override; + + // Given a func and targeted devices, we will try to clonse the func & + // transform/optimize for those devices. + // This will only happen if the whole subgraph can be supported by the target + // or can be supported after some transformations. + void GetAlternativeGraphForFunc(ArrayRef devices, + func::FuncOp func, ModuleOp module, + OpBuilder* builder); + + // If all ops in the func op is able to be represented in the hardware, we + // will return true, else will be false. + // This is basically all or nothing. + bool IsAllSupportedbySpec(func::FuncOp func, + const InferenceDeviceType& inference_type); + + // Given a func and a targeted device, we will try to clonse the func & + // transform/optimize for that device. + // It's simply clone the FuncOp and hardware specific transformations. + func::FuncOp GetAlternativeViewForSpec( + func::FuncOp func, + const InferenceDeviceType& current_device_inference_type, + const InferenceDeviceType& target_device_inference_type, ModuleOp module, + OpBuilder* builder); + + // Apply any device-specific optimizations. + void Optimize(func::FuncOp func, const std::string& hardware); + + ListOption device_specs_flag_{ + *this, "device-specs", + llvm::cl::desc( + "comma separated list of device specs, like CPU, GPU, DPS."), + llvm::cl::ZeroOrMore}; +}; + +void AlternativeSubgraphPass::GetAlternativeGraphForFunc( + ArrayRef devices, func::FuncOp func, ModuleOp module, + OpBuilder* builder) { + auto current_device = GetTargetAnnotation(func); + if (current_device->empty()) { + func.emitError( + "cannot find target annotation or unknown device specified for current " + "function"); + return; + } + + auto current_inference_type = GetInferenceTypeAnnotation(func); + if (!current_inference_type.has_value() || + current_inference_type == UNKNOWN) { + func.emitError( + "cannot find inference type annotation or unknown inference type " + "specified for current " + "function"); + return; + } + + const InferenceDeviceType current_device_type( + {*current_device, *current_inference_type}); + + const std::vector& all_inference_device_type = + GetAllAlternativeInferenceDeviceType(*current_inference_type, devices); + + for (const auto& device_inference_type : all_inference_device_type) { + if (device_inference_type != current_device_type) { + func::FuncOp cloned_func = GetAlternativeViewForSpec( + func, current_device_type, device_inference_type, module, builder); + // If we found unsupported ops, we will just go ahead and remove this + // function. + // TODO(b/160284136): currently we check if the ops are supported then + // see if we need to erase the func op. + // Ideally it would be nice if we can utilize dynamic illegal op to do + // the job. + if (device_inference_type.hardware != "CPU" && + !IsAllSupportedbySpec(cloned_func, device_inference_type)) { + cloned_func.erase(); + } + } + } + + // Perform the device-specific optimization last. + // We need to run the optimization for the current device last because we + // need to avoid any changes made the current graph polluting other + // alternative graph views. + Optimize(func, *current_device); +} + +bool AlternativeSubgraphPass::IsAllSupportedbySpec( + func::FuncOp func, const InferenceDeviceType& device_inference_type) { + bool found_unsupported = false; + func.walk([&](Operation* op) { + if (IsNonConstOp(op) && !IsTerminatorOp(op) && + !llvm::isa(op) && + !IsSupported(op, device_inference_type.hardware)) { + found_unsupported = true; + } + }); + return !found_unsupported; +} + +void AlternativeSubgraphPass::Optimize(func::FuncOp func, + const std::string& hardware) { + auto* ctx = &getContext(); + RewritePatternSet patterns = GetHardwareRewritePatterns(ctx, hardware); + (void)applyPatternsGreedily(func, std::move(patterns)); +} + +// Get the alternative view of the func for the given device_inference_type. +// It's possible the transformed func can still contain unsupported ops for the +// given device_inference_type. +func::FuncOp AlternativeSubgraphPass::GetAlternativeViewForSpec( + func::FuncOp func, const InferenceDeviceType& current_device_inference_type, + const InferenceDeviceType& target_device_inference_type, ModuleOp module, + OpBuilder* builder) { + func::FuncOp cloned_func = func.clone(); + cloned_func.setPrivate(); + auto interface_name = GetInterFaceName(func); + if (!interface_name.has_value()) { + func.emitError("the func op does not have interface_name"); + return nullptr; + } + + cloned_func->setAttr( + kDevice, builder->getStringAttr(target_device_inference_type.hardware)); + cloned_func->setAttr(kInferenceType, + builder->getStringAttr(GetInferenceString( + target_device_inference_type.inference_type))); + std::string new_function_name = + GetFunctionImplName(*interface_name, target_device_inference_type); + cloned_func.setName(new_function_name); + + // If it's quantized -> float, we need to wrap all the ops around with dequant + // and quant. + if ((current_device_inference_type.inference_type == QUANTIZED_UINT8 || + current_device_inference_type.inference_type == QUANTIZED_INT8) && + target_device_inference_type.inference_type == FLOAT) { + OpBuilder cloned_func_builder(cloned_func); + ConvertQuantizedOpToFloat(cloned_func, &cloned_func_builder); + OptimizeQuantizedOpToFloat(cloned_func, &getContext()); + } + + Optimize(cloned_func, target_device_inference_type.hardware); + + // Set device for each op. + cloned_func.walk([&](Operation* op) { + if (IsNonConstOp(op) && !IsTerminatorOp(op) && + !llvm::isa(op)) { + op->setAttr(kDevice, builder->getStringAttr( + target_device_inference_type.hardware)); + op->setAttr(kInferenceType, + builder->getStringAttr(GetInferenceString( + target_device_inference_type.inference_type))); + } + }); + + module.push_back(cloned_func); + return cloned_func; +} + +void AlternativeSubgraphPass::runOnOperation() { + auto module = getOperation(); + + // Process devices specs. + if (device_specs_flag_.empty()) { + module.emitError("no device specs specified"); + signalPassFailure(); + } + + std::vector device_specs; + if (!ProcessTargetDevices(device_specs_flag_, &device_specs)) { + module.emitError("unknown devices specified"); + signalPassFailure(); + } + + SmallVector funcs_to_be_processed; + // We only process if func has device annotations. + for (auto func : module.getOps()) { + auto device_attr = func->getAttrOfType(kDevice); + if (device_attr != nullptr) funcs_to_be_processed.push_back(func); + } + + OpBuilder builder(module); + // Go head to process those funcs. + // We don't process in the previous loop is we're adding new funcs, + // this is to avoid unnecessary processing. + for (auto func : funcs_to_be_processed) { + GetAlternativeGraphForFunc(device_specs, func, module, &builder); + } +} + +} // namespace + +std::unique_ptr> CreateAlternativeSubgraphPass( + llvm::ArrayRef device_specs) { + return std::make_unique(device_specs); +} + +static PassRegistration pass; + +} // namespace tac +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/passes.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/passes.h new file mode 100644 index 0000000000000000000000000000000000000000..6b1c6a0704029eefeb383498eee885d049f9679b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/passes.h @@ -0,0 +1,83 @@ +/* Copyright 2021 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_TRANSFORMS_PASSES_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_TRANSFORMS_PASSES_H_ + +#include +#include +#include + +#include "llvm/ADT/ArrayRef.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/tac/tac_filter.pb.h" + +namespace mlir { +namespace TFL { +namespace tac { +class TacModule; + +// Create an instance of the TargetAnnotationPass. +// TODO(b/177376459): Remove in favor of the one below. +std::unique_ptr> CreateTargetAnnotationPass( + llvm::ArrayRef device_specs); + +// Create and instance of TargetAnnotationPass. +std::unique_ptr> CreateTargetAnnotationPass( + const TacModule* module); + +// Create an instance of the RaiseTargetSubgraphsPass. If `skip_raise_cpu_ops`, +// we skip clustering for CPU ops for better clustering of ops running on other +// ML accelerators. When `ignore_inference_type` is set to true, the inference +// types are set to "NOT_CARE" for better clustering. +std::unique_ptr> CreateRaiseTargetSubgraphsPass( + bool skip_raise_cpu_ops = false, bool ignore_inference_type = false); + +// Create an instance of the AlternativeSubgraphPass. +std::unique_ptr> CreateAlternativeSubgraphPass( + llvm::ArrayRef device_specs); + +// Create an instance of ComputeCostPass. +std::unique_ptr> CreateComputeCostPass(); + +// Create an instance of PickSubgraphsPass. +std::unique_ptr> CreatePickSubgraphsPass(); + +// Create an instance of DeviceTransformGPUPass. +std::unique_ptr> CreateDeviceTransformGPUPass(); + +// Create an instance of GetOpCostPass. +std::unique_ptr> CreateGetOpCostPass(); + +// Create an instance of FoldConstantsToSubgraphPass. +std::unique_ptr> CreateFoldConstantsToSubgraphPass( + bool fold_all_constants); + +// Create an instance of TacFilterPass. +std::unique_ptr> CreateTacFilterPass( + ::third_party::tensorflow::compiler::mlir::lite::experimental::tac:: + TacFilters* tac_filters, + std::function + custom_options_callback); + +} // namespace tac +} // namespace TFL +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_TRANSFORMS_PASSES_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/pick_subgraphs.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/pick_subgraphs.cc new file mode 100644 index 0000000000000000000000000000000000000000..3ebf9c8b01d73945f0a276b841a509e88cbf3768 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/pick_subgraphs.cc @@ -0,0 +1,526 @@ +/* Copyright 2021 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 + +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/DenseSet.h" +#include "llvm/ADT/SetVector.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/Casting.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Block.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/cost.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/subgraph.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/targets.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/utils.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/transforms/cost_model.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/transforms/passes.h" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" + +// This pass is used to "determine" the best combinations of the whole "graph". +// +// Assume we have the graph looks like below: +// subgraph1 (CPU/GPU) subgraph2 (CPU) +// \ / +// subgraph3 (CPU/GPU) subgraph4 (CPU/GPU) +// | / +// subgraph5 (CPU/GPU) +// | +// subgraph6 (CPU) +// +// We want to evaluate the possible options and minize the overall costs to +// produce a graph like below: +// +// subgraph1 (GPU) subgraph2(CPU) +// \ / +// subgraph3 (GPU) subgraph4(GPU) +// | / +// subgraph5 (GPU) +// | +// subgraph6 (CPU) +// +// The overall workflow of the pick subgraphs pass: +// 1) Build subgraphs +// 1.1) Collect output subgraphs. +// 1.2) Build `Subgraph` and their "alternative view" from FuncOp. +// 2) Pick subgraphs +// 2.1) Populate the "dp table" for (subgraph, hardware). +// 2.2) Make decisions based on the populated dp table. +// 2.3) Rewire the whole graph based on the desicions. +// +namespace mlir { +namespace TFL { +namespace tac { +namespace { + +// GrapView is used to hold the aggregated cost for the given hardware +// view. +struct GraphView { + float total_cost; + std::unordered_map input_subgraph_plans; +}; + +// Subgraph is to hold the "conceptual" subgraph. +// A subgraph may associate with 1...n FuncOp, and each FuncOp may correspond +// with different hardwares. +struct Subgraph { + // The call can be thought as an "API". + func::CallOp call; + + // available_choces can be viewed as "real implementation" assosicated with + // the hardware. + std::unordered_map + available_choices; + + // This will include self (the subgraph itself). + // subgraphn + // | + // current_subgraph <- aggregated cost + std::unordered_map + aggregated_cost_with_decisions; +}; + +// If the output is produced by a callop, will return the callop, otherwise, +// will return nullptr. +inline func::CallOp GetProducerCallOpOrNull(Value output) { + Operation* output_op = output.getDefiningOp(); + if (output_op != nullptr && llvm::isa(output_op)) { + return llvm::cast(output_op); + } + return nullptr; +} + +class PickSubgraphsPass + : public mlir::PassWrapper> { + public: + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(PickSubgraphsPass) + + private: + llvm::StringRef getArgument() const final { return "tfl-pick-subgraphs"; } + llvm::StringRef getDescription() const final { + return "Pick the best subgraphs to minimize the overall total costs."; + } + void runOnOperation() override; + + std::unordered_map> + CollectSubgraphFuncs(ModuleOp module); + + void BuildSubgraphs( + func::FuncOp main_fn, + const std::unordered_map>& + func_impls, + llvm::SetVector* unprocessed_subgraphs, + SmallVector* output_subgraphs); + + void ProcessSubgraph(func::CallOp current_graph, + llvm::SetVector* unprocessed_subgraphs); + + bool PickSubgraphs( + llvm::SetVector* all_subgraphs, + ArrayRef output_subgraphs, + const std::unordered_map>& + collected_impl_funcs, + OpBuilder* builder); + + // Make the decisions based on the subgraphs. + // It may be the case we cannot decide the best scenarios for the user, + // in this case, we just return false. + bool MakeDecisions(ArrayRef output_subgraphs); + + // Rewire the subgraphs based on the decisions made. + // If we cannot make a decisions, we just don't do anything. + // TODO(renjieliu): we may change the vector to a map of hardware with + // corresponding ipml. + void RewireSubgraphs( + const std::unordered_map>& + collected_impl_funcs, + OpBuilder* builder); + + float GetCostOrFail(func::FuncOp func); + + llvm::DenseMap subgraphs_; + + llvm::DenseMap decisions_; +}; + +float PickSubgraphsPass::GetCostOrFail(func::FuncOp func) { + float self_cost; + if (!GetCostOnOp(func, &self_cost)) { + func.emitError("we cannot find cost for this func"); + signalPassFailure(); + } + return self_cost; +} + +// Here we choose to do a greedy dynamic programming based algorithm for +// simplicity. +// +// See the following graph: +// +// input_subgraph_1 .... input_subgraph_n +// \ / +// \ / +// current_subgraph +// / | \ +// +// Assume all the input subgraphs of the current subgraph are independent. +// If we already got optimal results for all the input subgraphs. +// Then the current_subgraph's aggregated optimal costs with regards to target +// perspective is simply: +// for target in current_subgraph.supported_targets: +// total_cost = 0 +// for input_subgraph in current_subgraph.input_subgraphs: +// input_cost = kInfinity +// for input_target in input_subgraphs.upported_targets: +// # cost = aggregated cost for input_subgraph with transfer cost. +// input_cost = min(input_cost, cost) +// total_cost += input_cost +// total_cost += current_subgraph.get_computation_cost(target) +// +// Note: for input subgraphs are not independent case, the dp case it a little +// bit complicated to handle. A potential thought is resolve only where +// conflict "happened". +// +// The above mentioned thought should probably be revisited for better thought +// or expanded more for more careful design. +// TODO(renjieliu): We may revisit this later. +void PickSubgraphsPass::ProcessSubgraph( + func::CallOp current_graph_call, + llvm::SetVector* unprocessed_subgraphs) { + Subgraph& current_subgraph = subgraphs_.find(current_graph_call)->second; + + std::vector input_subgraphs; + for (auto input : current_graph_call.getOperands()) { + func::CallOp input_call = GetProducerCallOpOrNull(input); + // If the input subgraph is not processed yet, we just go ahead and process + // that one first. + if (input_call == nullptr) continue; + + if (unprocessed_subgraphs->count(input_call) > 0) { + unprocessed_subgraphs->remove(input_call); + ProcessSubgraph(input_call, unprocessed_subgraphs); + } + Subgraph& input_subgraph = subgraphs_.find(input_call)->second; + input_subgraphs.push_back(&input_subgraph); + } + + // Find the best plan for the current subgraph. + for (const auto& kv : current_subgraph.available_choices) { + const auto& current_inference_device_type = kv.first; + func::FuncOp impl_target = kv.second; + float self_compute_cost = GetCostOrFail(impl_target); + + GraphView current_graph_view; + auto& input_subgraph_plans = current_graph_view.input_subgraph_plans; + + float inputs_total_costs = 0.0; + for (Subgraph* input_subgraph : input_subgraphs) { + float input_total_cost = std::numeric_limits::max(); + for (const auto& input_kv : input_subgraph->available_choices) { + const auto& input_inference_device_type = input_kv.first; + func::FuncOp input_impl_target = input_kv.second; + float input_compute_cost = GetCostOrFail(input_impl_target); + + float transfer_cost = + GetTransferCost(input_inference_device_type.hardware, + current_inference_device_type.hardware, + input_subgraph->call, current_graph_call); + float quant_dequant_cost = + GetQuantDequantCost(input_inference_device_type.inference_type, + current_inference_device_type.inference_type, + input_subgraph->call, current_graph_call); + float summed_cost = + transfer_cost + quant_dequant_cost + input_compute_cost; + + if (summed_cost < input_total_cost) { + // Looks this hardware is better for this input_subgraph, let's change + // it. + input_total_cost = summed_cost; + input_subgraph_plans[input_subgraph->call] = + input_inference_device_type; + } + } // for every hardware of input_subgraph + inputs_total_costs += input_total_cost; + } // for every input_subgraph + current_graph_view.total_cost = inputs_total_costs + self_compute_cost; + current_subgraph + .aggregated_cost_with_decisions[current_inference_device_type] = + current_graph_view; + } // for every subgraph +} + +void PickSubgraphsPass::BuildSubgraphs( + func::FuncOp fn, + const std::unordered_map>& + func_impls, + llvm::SetVector* unprocessed_subgraphs, + SmallVector* output_subgraphs) { + llvm::DenseSet returned_call_op_set; + // Collect all returns first from the main function. + // all the outputs of the main function are actually the final outputs. + // main_func: + // %output1 = call @subgraph_1... + // ... + // %output2 = call @subgraph_m... + // ... + // %outputn = call @subgraph_k... + // return %output1, output2, ..., outputn. + fn.walk([&](func::ReturnOp return_op) { + for (auto output : return_op.getOperands()) { + func::CallOp output_call = GetProducerCallOpOrNull(output); + if (output_call != nullptr) { + returned_call_op_set.insert(output_call); + } + } + }); + + // Each call op actually is the entry of the subgraph. + fn.walk([&](func::CallOp call_op) { + auto interface_name = GetInterFaceName(call_op); + // we only need to care about the call ops those have interface_name. + if (!interface_name.has_value()) return; + + unprocessed_subgraphs->insert(call_op); + + // Build the subgraph. + Subgraph subgraph; + subgraph.call = call_op; + auto impl_iter = func_impls.find(*interface_name); + if (impl_iter == func_impls.end()) { + call_op.emitError( + "we cannot find corresponding implementation for this call op"); + signalPassFailure(); + } + + for (auto impl : impl_iter->second) { + auto inference_device_type = GetInferenceDeviceTypeForOp(impl); + if (!inference_device_type.has_value()) { + impl.emitError("we cannot find inference device type for this func"); + signalPassFailure(); + } + subgraph.available_choices.emplace(*inference_device_type, impl); + } + + // Insert in the subgraphs. + subgraphs_.try_emplace(call_op, subgraph); + + // If it's an output subgraph, we will add to the output_subgraphs. + if (returned_call_op_set.find(call_op) != returned_call_op_set.end()) { + output_subgraphs->push_back(call_op); + } + }); +} + +// Collect all the subgraphs (and their alternatives) in the module. +std::unordered_map> +PickSubgraphsPass::CollectSubgraphFuncs(ModuleOp module) { + std::unordered_map> func_impls; + for (auto func : module.getOps()) { + auto interface_name = GetInterFaceName(func); + if (interface_name.has_value()) { + auto impls_iter = func_impls.find(*interface_name); + if (impls_iter == func_impls.end()) + impls_iter = + func_impls.emplace(*interface_name, std::vector()) + .first; + impls_iter->second.push_back(func); + } + } + return func_impls; +} + +// Given the final outputs, evaluate on the overall costs and pick the best +// plan, if we cannot make a decision, nothing would change, just fallback +// to the original plan. +bool PickSubgraphsPass::MakeDecisions(ArrayRef output_subgraphs) { + // BFS to make decisions. + std::queue processing_queue; + for (func::CallOp output : output_subgraphs) { + const GraphView* preferred_graph_view; + float minimum_cost = std::numeric_limits::max(); + + const Subgraph& subgraph = subgraphs_.find(output)->second; + for (const auto& kv : subgraph.aggregated_cost_with_decisions) { + if (minimum_cost > kv.second.total_cost) { + minimum_cost = kv.second.total_cost; + preferred_graph_view = &kv.second; + decisions_[output] = kv.first; + } + } + + processing_queue.push(preferred_graph_view); + } + + // If we see conflict, we will just abort. + while (!processing_queue.empty()) { + const GraphView* current = processing_queue.front(); + processing_queue.pop(); + for (const auto& input_with_plans : current->input_subgraph_plans) { + func::CallOp input = llvm::cast(input_with_plans.first); + const InferenceDeviceType& input_decision = input_with_plans.second; + auto made_input_decision_it = decisions_.find(input); + if (made_input_decision_it == decisions_.end()) { + // Input is not processed. + // Let's process it, also push it to the queue. + decisions_[input] = input_decision; + const Subgraph& input_subgraph = subgraphs_.find(input)->second; + const GraphView& input_subgraph_view = + input_subgraph.aggregated_cost_with_decisions.find(input_decision) + ->second; + processing_queue.push(&input_subgraph_view); + } else if (made_input_decision_it->second != input_decision) { + // We see confliction, we need to abort. + return false; + } + } + } + return true; +} + +// This rewire subgraph is essentially "hook" the call op with the "best" choice +// (subgraph). +void PickSubgraphsPass::RewireSubgraphs( + const std::unordered_map>& + collected_impl_funcs, + OpBuilder* builder) { + for (auto& kv : decisions_) { + func::CallOp call = llvm::cast(kv.first); + + const InferenceDeviceType& preferred_inference_device_type = kv.second; + + // We need to rewire the call. + std::string interface_name = *GetInterFaceName(call); + for (auto impl : collected_impl_funcs.find(interface_name)->second) { + const auto& impl_inference_device_type = + GetInferenceDeviceTypeForOp(impl); + if (*impl_inference_device_type == preferred_inference_device_type) { + if (call.getCallee() != impl.getName()) { + // We need to rebuild the call op. :( + builder->setInsertionPoint(call); + auto new_call = func::CallOp::create(*builder, call.getLoc(), impl, + call.getOperands()); + + // Set interface_name & target to the call_op as well. + new_call->setAttr(kInterfaceNameAttr, + builder->getStringAttr(interface_name)); + new_call->setAttr( + kDevice, + builder->getStringAttr(preferred_inference_device_type.hardware)); + new_call->setAttr( + kInferenceType, + builder->getStringAttr(GetInferenceString( + preferred_inference_device_type.inference_type))); + + call.replaceAllUsesWith(new_call.getResults()); + call.erase(); + } + } + } + } +} + +bool PickSubgraphsPass::PickSubgraphs( + llvm::SetVector* all_subgraphs, + ArrayRef output_subgraphs, + const std::unordered_map>& + collected_impl_funcs, + OpBuilder* builder) { + // Process those collected unprocessed subgraphs. + // + // Algorithm complexity for this: + // This Complexity should be O(edge * specs ^ 2). + // We should expect the specs to be a small number. + // In future, the spesc can be Hardwares x inference_types + // The Hardware can be {CPU, GPU, DSP, EDGE_TPU} + // The inference_types can be {float, Q_INT8, float16}. + // But still, we should expect the specs to be a small number. + // + // The process is essentially evaluating the accumulated cost for the dp table + // for all the subgraphs (and their alternatives). + while (!all_subgraphs->empty()) { + func::CallOp current_subgraph = + llvm::cast(all_subgraphs->front()); + all_subgraphs->remove(current_subgraph); + ProcessSubgraph(current_subgraph, all_subgraphs); + } + + // Make decisions given the "outputs" and the populated dp table. + // This is hoping to achieve a global minimum. + if (!MakeDecisions(output_subgraphs)) { + return false; + } + + // Once the design has been made. + // Start from the outputs and go back and checkout the plan. + RewireSubgraphs(collected_impl_funcs, builder); + + return true; +} + +void PickSubgraphsPass::runOnOperation() { + auto module = getOperation(); + // Collect & build the subgraphs. + // Also collect the output subgraphs. + // Output subgraphs are essentially those subgraphs pointed by the return + // op. + const std::unordered_map> func_impls = + CollectSubgraphFuncs(module); + llvm::SetVector unprocessed_subgraphs; + SmallVector output_subgraphs; + + for (auto fn : module.getOps()) { + BuildSubgraphs(fn, func_impls, &unprocessed_subgraphs, &output_subgraphs); + } + OpBuilder builder(module); + if (!PickSubgraphs(&unprocessed_subgraphs, output_subgraphs, func_impls, + &builder)) { + module.emitWarning( + "we cannot find the best scenarios for your case, so we just use " + "your original model plans"); + } +} + +} // namespace + +std::unique_ptr> CreatePickSubgraphsPass() { + return std::make_unique(); +} + +static PassRegistration pass; + +} // namespace tac +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/raise_target_subgraphs.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/raise_target_subgraphs.cc new file mode 100644 index 0000000000000000000000000000000000000000..f3fa5e469d08551ba3d0304533ede075aeb49c7e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/raise_target_subgraphs.cc @@ -0,0 +1,341 @@ +/* Copyright 2021 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 "absl/strings/str_cat.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/DenseSet.h" +#include "llvm/ADT/SetVector.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringMap.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/raw_ostream.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Block.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/OpDefinition.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/IR/Visitors.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Transforms/RegionUtils.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/common/outline_operations.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/subgraph.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/targets.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/utils.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/transforms/passes.h" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/lite/utils/utils.h" +#include "tensorflow/compiler/mlir/tensorflow/analysis/side_effect_analysis.h" +#include "tensorflow/compiler/mlir/tensorflow/utils/cluster_util.h" + +namespace mlir { +namespace TFL { +namespace tac { +namespace { + +constexpr StringRef kCpuDeviceName = "CPU"; + +using ::mlir::TFL::common::OpsAdded; +using ::mlir::TFL::common::Subgraph; + +class RaiseTargetSubgraphsPass + : public PassWrapper> { + public: + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(RaiseTargetSubgraphsPass) + + RaiseTargetSubgraphsPass() = default; + RaiseTargetSubgraphsPass(const RaiseTargetSubgraphsPass& other) { + this->skip_raise_cpu_ops_ = other.skip_raise_cpu_ops_; + this->ignore_inference_type_ = other.ignore_inference_type_; + } + explicit RaiseTargetSubgraphsPass(bool skip_raise_cpu_ops, + bool ignore_inference_type) { + skip_raise_cpu_ops_ = skip_raise_cpu_ops; + ignore_inference_type_ = ignore_inference_type; + } + + private: + Option skip_raise_cpu_ops_{ + *this, "skip-raise-cpu-ops", + llvm::cl::desc("Whether to cluster and raise CPU ops."), + llvm::cl::init(false)}; + + Option ignore_inference_type_{ + *this, "ignore-inference-type", + llvm::cl::desc("Whether to ignore the inference type in clustering."), + llvm::cl::init(false)}; + + llvm::StringRef getArgument() const final { + return "tfl-raise-target-subgraphs"; + } + llvm::StringRef getDescription() const final { + return "This pass will merge those have target-annotated TFL IRs together " + "& raise them as a function."; + } + void runOnOperation() override; + + bool RaiseTargetSubgraphsForBlock( + Block& block, OpBuilder& builder, ModuleOp module, bool skip_cpu, + int& func_count, const TF::SideEffectAnalysis::Info& side_effect_info); +}; + +// Returns the value of the string attribute `attr_name` for an added function +// op, by walking through the ops in the function and collating all the +// attribute values. If the attribute values are not all the same, returns +// std::nullopt. +std::optional GetConsolidatedStringAttr(func::FuncOp func, + StringRef attr_name) { + StringAttr attr_value; + WalkResult result = func.walk([&](Operation* op) { + if (op->hasAttr(attr_name)) { + StringAttr current_attr = mlir::cast(op->getAttr(attr_name)); + if (attr_value && attr_value != current_attr) { + // If the attribute value is not null, and it is different + // from the current op's attribute value, then it is an + // error. + return WalkResult::interrupt(); + } + attr_value = current_attr; + } + return WalkResult::advance(); + }); + if (result.wasInterrupted()) { + return std::nullopt; + } + return attr_value; +} + +// After raising ops and adding the Func & Call op, call this function +// to set attributes specific to this pass. +bool AddAttrs(OpsAdded& ops_added, OpBuilder& builder, int func_count) { + func::FuncOp& added_func_op = ops_added.func_op; + func::CallOp& added_call_op = ops_added.call_op; + StringAttr interface_name = + builder.getStringAttr(absl::StrCat("func_", func_count)); + + added_func_op->setAttr(kInterfaceNameAttr, interface_name); + added_call_op->setAttr(kInterfaceNameAttr, interface_name); + auto custom_options_fingerprint = + GetConsolidatedStringAttr(added_func_op, kCustomOptionsFingerprint); + if (!custom_options_fingerprint.has_value()) { + return false; + } + if (custom_options_fingerprint.value() != nullptr) { + added_func_op->setAttr(kCustomOptionsFingerprint, + custom_options_fingerprint.value()); + } + + auto delegate_compiler_version = + GetConsolidatedStringAttr(added_func_op, kDelegateCompilerVersion); + if (!delegate_compiler_version.has_value()) { + return false; + } + if (delegate_compiler_version.value() != nullptr) { + added_func_op->setAttr(kDelegateCompilerVersion, + delegate_compiler_version.value()); + } + + StringAttr device = mlir::cast( + added_func_op->getRegion(0).getBlocks().front().front().getAttr(kDevice)); + StringAttr inference_type = mlir::cast( + added_func_op->getRegion(0).getBlocks().front().front().getAttr( + kInferenceType)); + added_call_op->setAttr(kDevice, device); + added_call_op->setAttr(kInferenceType, inference_type); + added_func_op->setAttr(kDevice, device); + added_func_op->setAttr(kInferenceType, inference_type); + + std::string function_name = absl::StrCat(interface_name.getValue().str(), "_", + device.getValue().str(), "_", + inference_type.getValue().str()); + added_func_op.setName(builder.getStringAttr(function_name)); + added_call_op.setCallee(builder.getStringAttr(function_name)); + return true; +} + +// Raises partitioned sequential `Operations` from a block to a new function +// definition. `Operations` are partitioned into classes from the cartesian +// product of possible devices and inference datatypes. For example, we might +// raise a chunk of sequential operations from a block all having attributes +// `{ tac.device = "GPU", tac.inference_type = "FLOAT"}` to a function +// with the matching attributes. Assumed is that device type "CPU" +// is the only device that is allowed to call other devices. I.e. ancestors of a +// "CPU" `Operation` may only `Operations` without a device or other "CPU" +// `Operations`. Implied is that "CPU" ops may contain subgraphs of different +// device types which also need to be raised. The `side_effect_info` is used in +// the cluster algorithm for ops with side effect. +bool RaiseTargetSubgraphsPass::RaiseTargetSubgraphsForBlock( + Block& block, OpBuilder& builder, ModuleOp module, bool skip_cpu, + int& func_count, const TF::SideEffectAnalysis::Info& side_effect_info) { + llvm::SetVector partition_ops; + + auto device_is = [&](InferenceDeviceType t, llvm::StringRef device) -> bool { + return t.hardware == device; + }; + + auto op_has_device = [&](Operation& op, InferenceDeviceType& device) -> bool { + std::optional op_device = + GetInferenceDeviceTypeForOp(&op); + if (!op_device.has_value()) return false; + device = op_device.value(); + return true; + }; + + auto op_device_is = [&](Operation& op, llvm::StringRef device) -> bool { + InferenceDeviceType device_type; + if (!op_has_device(op, device_type)) return false; + return device_is(device_type, device); + }; + + // Given a list of `Operation`s to partition, raise them to a new + // function. If the partitons is of type "CPU" then it may contain + // other device subgraphs that need to be raised. We recur on + // any nested blocks of "CPU" ops and skip raising "CPU" ops for the + // remainder of that recursive call. + auto extract = [&](const llvm::SetVector& partition_ops) -> bool { + if (partition_ops.empty()) return true; + InferenceDeviceType device = + GetInferenceDeviceTypeForOp(partition_ops.front()).value(); + Subgraph old_subgraph(partition_ops, ++func_count); + OpsAdded ops_added; + ExtractSubgraphToFunc(old_subgraph, builder, module, ops_added); + if (!AddAttrs(ops_added, builder, func_count)) { + return false; + } + // Ops in "CPU" subgraphs may nested regions with other device subgraphs. + // We recur into these nested blocks to raise those as well. We don't raise + // "CPU" ops who are themselves nested within a "CPU" op, so set + // `skip_cpu` to true. + if (device_is(device, kCpuDeviceName)) { + for (auto& block : ops_added.func_op->getRegion(0).getBlocks()) + for (auto& op : block) { + auto op_device = GetInferenceDeviceTypeForOp(&op); + if (op_device_is(op, kCpuDeviceName)) { + // The recently raised func is device type cpu & `op` is a "CPU". + // Recursivley call again to raise any non-"CPU" subgraphs contained + // within nested region of `op`. + for (auto& region : op.getRegions()) + for (auto& block : region.getBlocks()) + if (!RaiseTargetSubgraphsForBlock(block, builder, module, + /*skip_cpu=*/true, func_count, + side_effect_info)) { + return false; + } + } + } + } + return true; + }; + + auto get_inference_device_type_string = [&](Operation* op) { + auto device_type = GetInferenceDeviceTypeForOp(op); + if (!device_type.has_value()) { + return std::string(""); + } + std::string concat_inference_device_type_string = + ignore_inference_type_ + ? device_type.value().hardware + : absl::StrCat( + device_type.value().hardware, "_", + GetInferenceString(device_type.value().inference_type)); + // Append the custom options fingerprint to the inference device type + // string, to compile functions with different fingerprints separately. + auto custom_options_fingerprint = op->getAttr(kCustomOptionsFingerprint); + if (custom_options_fingerprint != nullptr) { + StringAttr stack_str = mlir::cast(custom_options_fingerprint); + absl::StrAppend(&concat_inference_device_type_string, "_", + stack_str.getValue().str()); + } + return concat_inference_device_type_string; + }; + + auto op_can_be_ignored = [&](Operation* op) { + auto device_type = GetInferenceDeviceTypeForOp(op); + return !device_type.has_value() || + (skip_cpu && device_is(device_type.value(), kCpuDeviceName)); + }; + + const llvm::StringMap>& all_clusters = + TF::BuildAllClusters(block, side_effect_info, + get_inference_device_type_string, op_can_be_ignored); + for (const auto& [device, clusters] : all_clusters) { + for (const TF::Cluster& cluster : clusters) { + extract(cluster.ops); + } + } + if (skip_cpu) { + for (auto& op : block) { + auto op_device = GetInferenceDeviceTypeForOp(&op); + if (op_device_is(op, kCpuDeviceName)) { + // The recently raised func is device type cpu & `op` is a "CPU". + // Recursivley call again to raise any non-"CPU" subgraphs contained + // within nested region of `op`. + for (auto& region : op.getRegions()) + for (auto& block : region.getBlocks()) + if (!RaiseTargetSubgraphsForBlock(block, builder, module, + /*skip_cpu=*/true, func_count, + side_effect_info)) { + return false; + } + } + } + } + return true; +} + +void RaiseTargetSubgraphsPass::runOnOperation() { + ModuleOp module = getOperation(); + auto& side_effect_analysis = getAnalysis(); + SmallVector funcs(module.getOps()); + int func_count = -1; + for (auto func : funcs) { + const auto& info = side_effect_analysis.GetAnalysisForFunc(func); + for (auto& block : func) { + OpBuilder builder = OpBuilder::atBlockBegin(&block); + if (!RaiseTargetSubgraphsForBlock(block, builder, module, + /*skip_cpu=*/skip_raise_cpu_ops_, + func_count, info)) { + module.emitError("Raising target subgraph for block failed."); + signalPassFailure(); + } + } + } +} + +} // namespace + +std::unique_ptr> CreateRaiseTargetSubgraphsPass( + bool skip_raise_cpu_ops, bool ignore_inference_type) { + return std::make_unique(skip_raise_cpu_ops, + ignore_inference_type); +} + +static PassRegistration pass; + +} // namespace tac +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/tac_filter.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/tac_filter.cc new file mode 100644 index 0000000000000000000000000000000000000000..3724ce807b4e6accab66c6bd478c7718edbb1304 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/tac_filter.cc @@ -0,0 +1,343 @@ +/* Copyright 2023 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 "google/protobuf/text_format.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/Twine.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/FormatVariadic.h" +#include "llvm/Support/Regex.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/Diagnostics.h" // from @llvm-project +#include "mlir/IR/Location.h" // from @llvm-project +#include "mlir/Interfaces/CallInterfaces.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/targets.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/utils.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/tac_filter.pb.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/transforms/passes.h" +#include "tensorflow/compiler/mlir/lite/utils/utils.h" + +namespace mlir { +namespace TFL { +namespace tac { +namespace { + +using ::third_party::tensorflow::compiler::mlir::lite::experimental::tac:: + FunctionFilter; +using ::third_party::tensorflow::compiler::mlir::lite::experimental::tac:: + OpFilter; +using ::third_party::tensorflow::compiler::mlir::lite::experimental::tac:: + TacFilter; +using ::third_party::tensorflow::compiler::mlir::lite::experimental::tac:: + TacFilters; + +class TacFilterPass + : public PassWrapper> { + public: + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TacFilterPass) + + TacFilterPass() = default; + TacFilterPass(const TacFilterPass& other) { + this->tac_filters_ = other.tac_filters_; + } + explicit TacFilterPass( + TacFilters* tac_filters, + std::function + custom_options_callback) { + tac_filters_ = tac_filters; + custom_options_callback_ = custom_options_callback; + } + + private: + TacFilters* tac_filters_ = nullptr; + + llvm::StringRef getArgument() const final { return "tfl-tac-filter"; } + llvm::StringRef getDescription() const final { + return "This pass marks the ops to skip target annotation by inserting " + "`tac.skip_target_annotation` attribute to them based on user " + "provided config."; + } + + Option use_test_setting_{ + *this, "use-test-setting", + llvm::cl::desc( + "Whether to use the test config for the tac filter protobuf."), + llvm::cl::init(false)}; + + std::function + custom_options_callback_; + + void runOnOperation() override; +}; + +void ApplyFunctionTacFilter(func::FuncOp func, + FunctionFilter::FunctionFilterType type, + OpBuilder& builder) { + for (Operation& op : func.front()) { + if (type == FunctionFilter::SKIP_TARGET_ANNOTATION) { + op.setAttr(kSkipTargetAnnotation, builder.getUnitAttr()); + } else if (type == FunctionFilter::INCLUDE_TARGET_ANNOTATION) { + op.removeAttr(kSkipTargetAnnotation); + } + } +} + +void ApplyTacFilter( + ModuleOp module, const TacFilter& tac_filter, + SmallVector& filtered_ops, OpBuilder& builder, + std::function + custom_options_callback) { + if (tac_filter.has_function_filter()) { + llvm::Regex func_regex( + tac_filter.function_filter().function_name_pattern()); + for (auto func : module.getOps()) { + if (!func_regex.match(func.getName())) { + continue; + } + + ApplyFunctionTacFilter(func, tac_filter.function_filter().filter_type(), + builder); + filtered_ops.push_back(func); + } + return; + } + + auto should_filter_op = [](mlir::Operation* op) { + return IsNonConstOp(op) && !IsTerminatorOp(op) && + !llvm::isa(op); + }; + + auto map_op_to_cpu = [&](mlir::Operation* op) { + if (!should_filter_op(op)) { + return; + } + op->setAttr(kSkipTargetAnnotation, builder.getUnitAttr()); + filtered_ops.push_back(op); + }; + + auto map_op_to_custom_device = [&](mlir::Operation* op) { + if (!should_filter_op(op)) { + return; + } + if (!tac_filter.op_filter().has_custom_options()) { + return; + } + + const google::protobuf::Any& custom_options = + tac_filter.op_filter().custom_options(); + custom_options_callback(op, custom_options); + }; + + llvm::Regex op_regex(tac_filter.op_filter().op_name_pattern()); + OpFilter::MatchType match_type = tac_filter.op_filter().match_type(); + OpFilter::DeviceType device_type = tac_filter.op_filter().device_type(); + module.walk([&](Operation* op) { + NameLoc loc; + if (auto name_loc = mlir::dyn_cast(op->getLoc())) { + loc = name_loc; + } else if (auto fused_loc = mlir::dyn_cast(op->getLoc())) { + loc = dyn_cast(fused_loc.getLocations().front()); + } + + if (!loc) { + return; + } + // There can be two kinds of `match_type`: + // 1. MATCH: the op name matches the pattern. + // 2. INVERT_MATCH: the op name does not match the pattern. + // + // On each of the above, the op filter can specify if it should be run on + // the CPU or on a custom device, with the `device_type` field. + // Running on CPU and the match type MATCH is the default if not specified. + // + // The code below maps an op to the appropriate device based on the above + // fields. + if (op_regex.match(loc.getName())) { + switch (match_type) { + case OpFilter::MATCH: + if (device_type == OpFilter::CPU) { + map_op_to_cpu(op); + return; + } + map_op_to_custom_device(op); + break; + default: + break; + } + } else { + switch (match_type) { + case OpFilter::INVERT_MATCH: + if (device_type == OpFilter::CPU) { + map_op_to_cpu(op); + return; + } + map_op_to_custom_device(op); + break; + default: + break; + } + } + }); +} + +// A custom string for tac filter. +std::string TacFilterToString(const TacFilter& tac_filter) { + std::string tac_filter_type_str; + std::string tac_filter_name_pattern_str; + if (tac_filter.has_function_filter()) { + tac_filter_type_str = (llvm::Twine("function filter ") + + FunctionFilter::FunctionFilterType_Name( + tac_filter.function_filter().filter_type())) + .str(); + tac_filter_name_pattern_str = + tac_filter.function_filter().function_name_pattern(); + } else { + tac_filter_type_str = "op filter"; + tac_filter_name_pattern_str = tac_filter.op_filter().op_name_pattern(); + } + return (llvm::Twine("filter type: ") + tac_filter_type_str + + ", filter_pattern: \"" + tac_filter_name_pattern_str + "\"") + .str(); +} + +void PrintTacFilterResult(Location module_loc, const TacFilter& tac_filter, + int count, + const SmallVector& filtered_ops) { + emitRemark(module_loc) << llvm::formatv("Tac filter ({0}): {1}", count, + TacFilterToString(tac_filter)); + if (filtered_ops.empty()) { + emitRemark(module_loc) << llvm::formatv( + "Tac filter ({0}) specified but not applied to any op", count); + return; + } + + if (tac_filter.has_function_filter()) { + for (Operation* op : filtered_ops) { + auto func = cast(op); + func.emitRemark() << llvm::formatv("filtered by tac filter ({0})", count); + } + return; + } + + DenseMap> func_to_filtered_ops_map; + for (Operation* op : filtered_ops) { + auto func = op->getParentOfType(); + func_to_filtered_ops_map[func].push_back(op); + } + for (auto& [func, ops] : func_to_filtered_ops_map) { + std::string interleaved_op_name; + llvm::raw_string_ostream os(interleaved_op_name); + llvm::interleaveComma( + ops, os, [&](Operation* op) { os << "\"" << op->getName() << "\""; }); + os.flush(); + func.emitRemark() << llvm::formatv( + "all ops filtered by tac filter ({0}): {1}", count, + interleaved_op_name); + } +} + +void TacFilterPass::runOnOperation() { + TacFilters test_tac_filters; + if (use_test_setting_) { + // Sets up the test config used in the mlir LIT test. + google::protobuf::TextFormat::ParseFromString(R"( + tac_filters { + function_filter { + function_name_pattern: "^testFunction" + } + } + tac_filters { + function_filter { + function_name_pattern: "testFunctionInclude" + filter_type: INCLUDE_TARGET_ANNOTATION + } + } + tac_filters { + op_filter { + op_name_pattern: "^test_op" + } + } + )", + &test_tac_filters); + tac_filters_ = &test_tac_filters; + } + + if (!tac_filters_) { + return; + } + + ModuleOp module = getOperation(); + OpBuilder builder(module); + std::sort(tac_filters_->mutable_tac_filters()->pointer_begin(), + tac_filters_->mutable_tac_filters()->pointer_end(), + [](const TacFilter* a, const TacFilter* b) { + const bool a_is_function_filter = a->has_function_filter(); + const bool b_is_function_filter = b->has_function_filter(); + if (a_is_function_filter != b_is_function_filter) { + // Function filter is applied before op filter. + return a_is_function_filter > b_is_function_filter; + } + + if (!a_is_function_filter && !b_is_function_filter) { + // The order of 2 op filters don't matter. + return false; + } + + const bool a_is_function_exclude = + (a->function_filter().filter_type() == + FunctionFilter::SKIP_TARGET_ANNOTATION); + const bool b_is_function_exclude = + (b->function_filter().filter_type() == + FunctionFilter::SKIP_TARGET_ANNOTATION); + // Function exclude filter is applied before function include + // filter. + return a_is_function_exclude > b_is_function_exclude; + }); + + for (const auto& tac_filter : llvm::enumerate(tac_filters_->tac_filters())) { + SmallVector filtered_ops; + ApplyTacFilter(module, tac_filter.value(), filtered_ops, builder, + custom_options_callback_); + PrintTacFilterResult(module.getLoc(), tac_filter.value(), + tac_filter.index(), filtered_ops); + } +} + +} // namespace + +std::unique_ptr> CreateTacFilterPass( + TacFilters* tac_filters, + std::function + custom_options_callback) { + return std::make_unique(tac_filters, custom_options_callback); +} + +static PassRegistration pass; + +} // namespace tac +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/tac_pass.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/tac_pass.h new file mode 100644 index 0000000000000000000000000000000000000000..583c04dab1d237951ec27890951ec9e83ada4aa2 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/tac_pass.h @@ -0,0 +1,90 @@ +/* Copyright 2021 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_TRANSFORMS_TAC_PASS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_TRANSFORMS_TAC_PASS_H_ + +#include +#include + +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/tac/hardwares/target_hardware.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/tac_module.h" + +namespace mlir { +namespace TFL { +namespace tac { +// An OperationPass<> with access to the TAC module instance that the +// pass is running part of. +// See OperationPass<> comments for all details/restrictions of OperationPass. +// +// When adding new Pass to TAC, users should use this class as the base class +// as it provides access to the TAC module. +template +class TacPass : public OperationPass { + public: + using OperationPass::OperationPass; + explicit TacPass(const TacModule* module) + : OperationPass::OperationPass(mlir::TypeID::get()), + module_(module) {} + + ~TacPass() override = default; + + const TargetHardware* GetTargetHardware( + const std::string& hardware_name) const { + return module_ != nullptr + ? module_->GetTargetHardware(hardware_name) + : mlir::TFL::tac::GetTargetHardware(hardware_name); + } + + protected: + const TacModule* module_ = nullptr; // Not owned. +}; + +// A FunctionPass but with access to TAC module. +// See FunctionPass comments for all details/restrictions of FunctionPass. +// +// When adding new Pass to TAC, users should use this class as the base class +// as it provides access to the TAC module. +template +class TacFunctionPass : public TacPass { + public: + using TacPass::TacPass; + + ~TacFunctionPass() override = default; + + mlir::func::FuncOp getFunction() { return getOperation(); } + + virtual void runOnFunction() = 0; + + void runOnOperation() final { + if (!getFunction().isExternal()) runOnFunction(); + } + + protected: + // Returns the derived pass name. + StringRef getName() const override { return llvm::getTypeName(); } + + // A clone method to create a copy of this pass. + std::unique_ptr clonePass() const override { + return std::make_unique(*static_cast(this)); + } +}; + +} // namespace tac +} // namespace TFL +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_TRANSFORMS_TAC_PASS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/target_annotation.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/target_annotation.cc new file mode 100644 index 0000000000000000000000000000000000000000..324fa32f6f1c89683d6fcea8fd50561cb78363d7 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/target_annotation.cc @@ -0,0 +1,166 @@ +/* Copyright 2021 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 "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/DenseSet.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/CommandLine.h" +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/targets.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/utils.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/hardwares/target_hardware.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/transforms/device_transform.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/transforms/passes.h" +#include "tensorflow/compiler/mlir/lite/experimental/tac/transforms/tac_pass.h" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" + +namespace mlir { +namespace TFL { +namespace tac { +namespace { + +class TargetAnnotationPass : public TacFunctionPass { + public: + llvm::StringRef getArgument() const final { return "tfl-target-annotation"; } + llvm::StringRef getDescription() const final { + return "Add user specified target annotations to the TFL operations given " + "operation capabilities, will default to CPU."; + } + // using TacFunctionPass::TacFunctionPass; + TargetAnnotationPass() : TacFunctionPass(nullptr) {} + TargetAnnotationPass(const TargetAnnotationPass& copy) + : TacFunctionPass(copy.module_) {} + explicit TargetAnnotationPass(llvm::ArrayRef device_specs) + : TacFunctionPass(nullptr) { + device_specs_flag_ = device_specs; + } + + explicit TargetAnnotationPass(const TacModule* module) + : TacFunctionPass(module) {} + + private: + void runOnFunction() override; + void SetTargetAnnotation(Operation* op, + llvm::ArrayRef device_specs, + OpBuilder* builder); + + ListOption device_specs_flag_{ + *this, "device-specs", + llvm::cl::desc( + "comma separated list of device specs, like CPU, GPU, Hexagon."), + llvm::cl::ZeroOrMore}; + + void getDependentDialects(mlir::DialectRegistry& registry) const override { + if (!module_) { + for (const auto& device : device_specs_flag_) { + auto* hardware = this->GetTargetHardware(device); + if (hardware == nullptr) continue; + hardware->GetDependentDialects(registry); + } + } + } +}; + +void SetAnnotation(Operation* op, std::string attribute, std::string annotation, + OpBuilder* builder) { + // TODO(karimnosseir): Maybe set device capabilities to allow us to have + // more flexbility when raise the subgraphs. + auto default_target = builder->getStringAttr(annotation); + op->setAttr(attribute, default_target); +} + +void TargetAnnotationPass::SetTargetAnnotation( + Operation* op, llvm::ArrayRef device_specs, + OpBuilder* builder) { + if (op->hasAttr(kSkipTargetAnnotation)) { + return; + } + const InferenceType inference_type = GetInferenceType(op); + const std::string inference_type_str = GetInferenceString(inference_type); + SetAnnotation(op, kInferenceType, inference_type_str, builder); + bool device_is_set = false; + // TODO(b/177376459): Remove the usage of device_specs. + // TODO(b/177376459): Update if needed to make testing easy. + if (!module_) { + for (const auto& device : device_specs) { + auto* hardware = this->GetTargetHardware(device); + if (hardware == nullptr) continue; + if (hardware->IsOpSupported(op)) { + SetAnnotation(op, kDevice, device, builder); + device_is_set = true; + break; + } + } + } else { + for (const auto* hardware : module_->GetAvailableHardwares()) { + if (hardware == nullptr) continue; + if (hardware->IsOpSupported(op)) { + SetAnnotation(op, kDevice, GetHardwareName(hardware), builder); + device_is_set = true; + break; + } + } + } + // default to CPU + if (!device_is_set) { + if (IsNonConstOp(op) && !IsTerminatorOp(op) && + !llvm::isa(op)) { + SetAnnotation(op, kDevice, "CPU", builder); + device_is_set = true; + } + } + if (!device_is_set) { + op->emitError("cannot set target device for this ops"); + } +} + +void TargetAnnotationPass::runOnFunction() { + auto func = getFunction(); + OpBuilder builder(func); + + func.walk([&](Operation* op) { + // We only care about TFL dialect. + if (IsNonConstOp(op) && !IsTerminatorOp(op) && + !llvm::isa(op)) { + SetTargetAnnotation(op, device_specs_flag_, &builder); + } + }); +} + +} // namespace + +std::unique_ptr> CreateTargetAnnotationPass( + llvm::ArrayRef device_specs) { + return std::make_unique(device_specs); +} + +std::unique_ptr> CreateTargetAnnotationPass( + const TacModule* module) { + return std::make_unique(module); +} + +static PassRegistration pass; + +} // namespace tac +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/transform_patterns.td b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/transform_patterns.td new file mode 100644 index 0000000000000000000000000000000000000000..0badeea5efde7635e5ca2d2f243ce2afa907f248 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/transform_patterns.td @@ -0,0 +1,32 @@ +/* Copyright 2021 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 "mlir/IR/OpBase.td" +include "mlir/IR/PatternBase.td" +include "mlir/Dialect/Arith/IR/ArithOps.td" +include "mlir/Dialect/Func/IR/FuncOps.td" +include "tensorflow/compiler/mlir/lite/ir/tfl_ops.td" +include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.td" + +// Basically x - y => x + y * -1 +def SubToAdd : Pat<(TFL_SubOp $lhs, $rhs, $act), + (TFL_AddOp $lhs, (TFL_MulOp $rhs, + (Arith_ConstantOp ConstantAttr, + "-1.0f">), TFL_AF_None), $act)>; + +// Squash tfl.dequantize and tfl.quantize pairs. +// TODO(b/185915462): Compare the scale of input and output. This can also be +// squashed to a requantize op if the scales are different. +def FoldQuantizeDequantize: Pat<(TFL_DequantizeOp (TFL_QuantizeOp $arg, $qt)), (replaceWithValue $arg)>; diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/utils/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/utils/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..3355577143090baab64596d43c4b1fdeb95c6b7a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/utils/BUILD @@ -0,0 +1,38 @@ +load("//tensorflow/core/platform:rules_cc.bzl", "cc_library") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + default_visibility = [ + "//visibility:public", + ], + licenses = ["notice"], +) + +cc_library( + name = "utils", + srcs = [ + "utils.cc", + ], + hdrs = [ + "utils.h", + ], + deps = [ + "//tensorflow/compiler/mlir/lite:flatbuffer_translate_lib", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/lite/experimental/tac:common", + "//tensorflow/compiler/mlir/lite/stablehlo:prepare_hlo", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings:str_format", + "@com_google_absl//absl/strings:string_view", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:FuncExtensions", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Parser", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:ReconcileUnrealizedCasts", + "@llvm-project//mlir:Support", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/utils/utils.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/utils/utils.cc new file mode 100644 index 0000000000000000000000000000000000000000..2ed5fb2d0daaeac6f2b56ba3df88e7f0588cb87d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/utils/utils.cc @@ -0,0 +1,146 @@ +/* Copyright 2021 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/compiler/mlir/lite/experimental/tac/utils/utils.h" + +#include +#include +#include +#include +#include + +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/str_format.h" +#include "absl/strings/string_view.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/SMLoc.h" +#include "llvm/Support/SourceMgr.h" +#include "llvm/Support/ToolOutputFile.h" +#include "llvm/Support/raw_ostream.h" +#include "mlir/Conversion/ReconcileUnrealizedCasts/ReconcileUnrealizedCasts.h" // from @llvm-project +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/Dialect/Func/Extensions/AllExtensions.h" // from @llvm-project +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/Location.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/OwningOpRef.h" // from @llvm-project +#include "mlir/Parser/Parser.h" // from @llvm-project +#include "mlir/Pass/PassManager.h" // from @llvm-project +#include "mlir/Support/FileUtilities.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/experimental/tac/common/targets.h" +#include "tensorflow/compiler/mlir/lite/flatbuffer_export.h" +#include "tensorflow/compiler/mlir/lite/flatbuffer_import.h" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h" + +namespace mlir { +namespace TFL { +namespace tac { + +absl::StatusOr> ImportFlatbufferOrMlir( + const std::string& input_filename, bool input_mlir, + bool experimental_prune_unreachable_nodes_unconditionally, + llvm::SourceMgr* source_mgr, mlir::MLIRContext* context) { + std::string error; + std::unique_ptr buffer = + mlir::openInputFile(input_filename, &error); + if (buffer == nullptr) { + return absl::InvalidArgumentError(absl::StrFormat( + "Cannot open input file: %s. %s", input_filename, error)); + } + + if (input_mlir) { + mlir::DialectRegistry registry; + registry.insert(); + mlir::func::registerAllExtensions(registry); + context->appendDialectRegistry(registry); + source_mgr->AddNewSourceBuffer(std::move(buffer), llvm::SMLoc()); + return mlir::OwningOpRef( + mlir::parseSourceFile(*source_mgr, context)); + } + + mlir::Location loc = + mlir::FileLineColLoc::get(context, input_filename, /*line=*/0, + /*column=*/0); + std::vector inputs; + std::vector outputs; + return tflite::FlatBufferToMlir( + absl::string_view(buffer->getBufferStart(), buffer->getBufferSize()), + context, loc, /*use_external_constant=*/false, inputs, outputs, + experimental_prune_unreachable_nodes_unconditionally); +} + +absl::Status ExportFlatbufferOrMlir( + const std::string& output_filename, bool output_mlir, mlir::ModuleOp module, + bool enable_select_tf_ops, std::optional custom_option_alignment) { + std::string error_msg; + auto output = mlir::openOutputFile(output_filename, &error_msg); + if (output == nullptr) { + llvm::errs() << error_msg << '\n'; + return absl::InvalidArgumentError("cannot open output file."); + } + + std::string result; + if (output_mlir) { + llvm::raw_string_ostream os(result); + module.print(os); + os.flush(); + } else { + // This extra attribute is added by TAC pass. We need to remove it before + // converting to VHLO. + module.walk([&](mlir::Operation* op) { + if (op->hasAttr(mlir::TFL::tac::kSkipTargetAnnotation)) { + op->removeAttr(mlir::TFL::tac::kSkipTargetAnnotation); + } + }); + // Converts stablehlo to vhlo so that flatbuffer export can handle it. + auto pass_manager = + std::make_unique(module.getContext()); + pass_manager->addPass(mlir::odml::createLegalizeStablehloToVhloPass()); + pass_manager->addPass(mlir::createReconcileUnrealizedCastsPass()); + if (failed(pass_manager->run(module))) { + return absl::UnknownError("Failed to legalize stablehlo to vhlo."); + } + + tflite::FlatbufferExportOptions options; + options.converter_flags.set_force_select_tf_ops(false); + options.converter_flags.set_allow_custom_ops(true); + if (enable_select_tf_ops) { + options.converter_flags.set_enable_select_tf_ops(true); + options.converter_flags.set_allow_all_select_tf_ops(true); + } else { + options.converter_flags.set_enable_select_tf_ops(false); + } + if (custom_option_alignment.has_value()) { + options.custom_option_alignment = *custom_option_alignment; + } + if (!tflite::MlirToFlatBufferTranslateFunction( + module, options, &result, /*serialize_stablehlo_ops=*/true)) { + return absl::UnknownError("Failed to export tflite file."); + } + } + + output->os() << result; + output->keep(); + return absl::OkStatus(); +} + +} // namespace tac +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/utils/utils.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/utils/utils.h new file mode 100644 index 0000000000000000000000000000000000000000..b472ab057d6d250e93847f66d311b07a91c51578 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/utils/utils.h @@ -0,0 +1,50 @@ +/* Copyright 2021 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_UTILS_UTILS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_UTILS_UTILS_H_ + +#include +#include + +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/OwningOpRef.h" // from @llvm-project +#include "mlir/Parser/Parser.h" // from @llvm-project + +namespace mlir { +namespace TFL { +namespace tac { + +// Import the file as mlir module, the input maybe flatbuffer or mlir file. +absl::StatusOr> ImportFlatbufferOrMlir( + const std::string& input_filename, bool input_mlir, + bool experimental_prune_unreachable_nodes_unconditionally, + llvm::SourceMgr* source_mgr, mlir::MLIRContext* context); + +// Export the module to file, can be either mlir or flatbuffer. +absl::Status ExportFlatbufferOrMlir( + const std::string& output_filename, bool output_mlir, mlir::ModuleOp module, + bool enable_select_tf_ops, + std::optional custom_option_alignment = std::nullopt); + +} // namespace tac +} // namespace TFL +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_UTILS_UTILS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_export.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_export.cc new file mode 100644 index 0000000000000000000000000000000000000000..3bb207657efd84f19dd81f7a8b1349d340713692 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_export.cc @@ -0,0 +1,4730 @@ +/* Copyright 2022 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/compiler/mlir/lite/flatbuffer_export.h" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "absl/algorithm/container.h" +#include "absl/base/attributes.h" +#include "absl/container/flat_hash_map.h" +#include "absl/container/flat_hash_set.h" +#include "absl/functional/any_invocable.h" +#include "absl/functional/function_ref.h" +#include "absl/log/check.h" +#include "absl/log/log.h" +#include "absl/status/status.h" +#include "absl/strings/match.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/str_format.h" +#include "absl/strings/str_join.h" +#include "absl/strings/string_view.h" +#include "flatbuffers/buffer.h" // from @flatbuffers +#include "flatbuffers/flatbuffer_builder.h" // from @flatbuffers +#include "flatbuffers/flexbuffers.h" // from @flatbuffers +#include "flatbuffers/vector.h" // from @flatbuffers +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/StringSwitch.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/FormatVariadic.h" +#include "llvm/Support/SwapByteOrder.h" +#include "llvm/Support/raw_ostream.h" +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Dialect/Quant/IR/QuantTypes.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/Diagnostics.h" // from @llvm-project +#include "mlir/IR/DialectResourceBlobManager.h" // from @llvm-project // IWYU pragma: keep +#include "mlir/IR/Location.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/OpDefinition.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/TypeUtilities.h" // from @llvm-project +#include "mlir/IR/Types.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/IR/Visitors.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "stablehlo/dialect/StablehloOps.h" // from @stablehlo +#include "stablehlo/dialect/VhloOps.h" // from @stablehlo +#include "tensorflow/compiler/mlir/lite/converter_flags.pb.h" +#include "tensorflow/compiler/mlir/lite/core/c/builtin_op_data.h" +#include "tensorflow/compiler/mlir/lite/core/macros.h" +#include "tensorflow/compiler/mlir/lite/delegates/flex/allowlisted_flex_ops.h" +#include "tensorflow/compiler/mlir/lite/experimental/remat/metadata_util.h" +#include "tensorflow/compiler/mlir/lite/flatbuffer_operator.h" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/lite/metrics/converter_error_data.pb.h" +#include "tensorflow/compiler/mlir/lite/metrics/error_collector_inst.h" +#include "tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.h" +#include "tensorflow/compiler/mlir/lite/schema/mutable/debug_metadata_generated.h" +#include "tensorflow/compiler/mlir/lite/schema/mutable/schema_generated.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_conversion_utils.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" +#include "tensorflow/compiler/mlir/lite/tools/versioning/op_version.h" +#include "tensorflow/compiler/mlir/lite/tools/versioning/runtime_version.h" +#include "tensorflow/compiler/mlir/lite/utils/const_tensor_utils.h" +#include "tensorflow/compiler/mlir/lite/utils/control_edges.h" +#include "tensorflow/compiler/mlir/lite/utils/convert_type.h" +#include "tensorflow/compiler/mlir/lite/utils/low_bit_utils.h" +#include "tensorflow/compiler/mlir/lite/utils/metadata_utils.h" +#include "tensorflow/compiler/mlir/lite/utils/mlir_module_utils.h" +#include "tensorflow/compiler/mlir/lite/utils/region_isolation.h" +#include "tensorflow/compiler/mlir/lite/utils/stateful_ops_utils.h" +#include "tensorflow/compiler/mlir/lite/utils/string_utils.h" +#include "tensorflow/compiler/mlir/lite/version.h" +#include "tensorflow/compiler/mlir/op_or_arg_name_mapper.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_dialect.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_executor.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_saved_model.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_types.h" +#include "tensorflow/compiler/mlir/tensorflow/utils/convert_tensor.h" +#include "tensorflow/compiler/mlir/tensorflow/utils/dynamic_shape_utils.h" +#include "tensorflow/compiler/mlir/tensorflow/utils/translate_utils.h" +#include "tensorflow/core/framework/attr_value.pb.h" +#include "tensorflow/core/framework/node_def.pb.h" +#include "tensorflow/core/framework/op.h" +#include "tensorflow/core/framework/tensor.h" +#include "tensorflow/core/framework/types.pb.h" +#include "tensorflow/core/platform/tstring.h" +#include "tsl/platform/tstring.h" + +using absl::StatusOr; +using llvm::dyn_cast; +using llvm::formatv; +using llvm::isa; +using llvm::StringRef; +using mlir::Dialect; +using mlir::ElementsAttr; +using mlir::MLIRContext; +using mlir::ModuleOp; +using mlir::NoneType; +using mlir::Operation; +using mlir::Region; +using mlir::StringAttr; +using mlir::TensorType; +using mlir::Twine; +using mlir::Type; +using mlir::UnknownLoc; +using mlir::Value; +using mlir::WalkResult; +using mlir::func::FuncOp; +using tensorflow::OpOrArgLocNameMapper; +using tensorflow::OpOrArgNameMapper; +using tensorflow::Status; +using tflite::flex::IsAllowlistedFlexOp; + +template +using BufferOffset = flatbuffers::Offset; + +template +using VectorBufferOffset = flatbuffers::Offset>; + +using CustomOptionsOffset = VectorBufferOffset; + +namespace tfl = mlir::TFL; + +ABSL_CONST_INIT const absl::string_view kFlexOpNamePrefix = "Flex"; + +// LINT.IfChange(optional_tensor) +// Taken from third_party/tensorflow/lite/core/c/common.h +constexpr int kTfLiteMigrationOptionalTensor = -1; +// LINT.ThenChange(//tensorflow/lite/core/c/common.h:optional_tensor) + +// Use initial buffer size in flatbuffer builder to be same as the initial size +// used by the TOCO export. (It does not explain rationale for this choice.) +constexpr size_t kInitialBufferSize = 10240; + +// Flatbuffer fields to be padded to 16 bytes aligned. +constexpr size_t kFbAlignment = 16; + +// Set `isSigned` to false if the `type` is an 8-bit unsigned integer type. +// Since tflite doesn't support unsigned for other types, returns error if +// `isSigned` is set to false for other types. +static StatusOr GetTFLiteType(Type type, + bool is_signed = true) { + if (!is_signed) { + if (type.isSignlessInteger(4)) { + return tflite::TensorType_UINT4; + } else if (type.isSignlessInteger(8)) { + return tflite::TensorType_UINT8; + } else if (type.isSignlessInteger(16)) { + return tflite::TensorType_UINT16; + } else { + return Status(absl::StatusCode::kInvalidArgument, + "'isSigned' can only be set for 4/8/16-bits integer type"); + } + } + + if (type.isF32()) { + return tflite::TensorType_FLOAT32; + } else if (type.isF16()) { + return tflite::TensorType_FLOAT16; + } else if (type.isBF16()) { + return tflite::TensorType_BFLOAT16; + } else if (type.isF64()) { + return tflite::TensorType_FLOAT64; + } else if (mlir::isa(type)) { + return tflite::TensorType_FLOAT8_E4M3FN; + } else if (mlir::isa(type)) { + return tflite::TensorType_FLOAT8_E5M2; + } else if (mlir::isa(type)) { + return tflite::TensorType_STRING; + } else if (mlir::isa(type)) { + return tflite::TensorType_UINT8; + } else if (auto complex_type = mlir::dyn_cast(type)) { + auto ftype = complex_type.getElementType(); + if (ftype.isF32()) { + return tflite::TensorType_COMPLEX64; + } + if (ftype.isF64()) { + return tflite::TensorType_COMPLEX128; + } + return Status(absl::StatusCode::kInvalidArgument, "Unsupported type"); + } else if (auto itype = mlir::dyn_cast(type)) { + switch (itype.getWidth()) { + case 1: + return tflite::TensorType_BOOL; + case 2: + if (itype.isUnsigned()) { + return Status(absl::StatusCode::kInvalidArgument, + "Unsupported 2bit unsigned int type"); + } else { + return tflite::TensorType_INT2; + } + case 4: + return itype.isUnsigned() ? tflite::TensorType_UINT4 + : tflite::TensorType_INT4; + case 8: + return itype.isUnsigned() ? tflite::TensorType_UINT8 + : tflite::TensorType_INT8; + case 16: + return itype.isUnsigned() ? tflite::TensorType_UINT16 + : tflite::TensorType_INT16; + case 32: + return itype.isUnsigned() ? tflite::TensorType_UINT32 + : tflite::TensorType_INT32; + case 64: + return itype.isUnsigned() ? tflite::TensorType_UINT64 + : tflite::TensorType_INT64; + } + } else if (auto q_uniform_type = + mlir::dyn_cast(type)) { + return GetTFLiteType(q_uniform_type.getStorageType(), + q_uniform_type.isSigned()); + } else if (auto q_peraxis_type = + mlir::dyn_cast( + type)) { + return GetTFLiteType(q_peraxis_type.getStorageType(), + q_peraxis_type.isSigned()); + } else if (auto q_calibrated_type = + mlir::dyn_cast(type)) { + return GetTFLiteType(q_calibrated_type.getExpressedType()); + } else if (mlir::isa(type)) { + return tflite::TensorType_RESOURCE; + } else if (mlir::isa(type)) { + return tflite::TensorType_VARIANT; + } + // TFLite export fills FLOAT32 for unknown data types. Returning an error + // for now for safety and this could be revisited when required. + return Status(absl::StatusCode::kInvalidArgument, "Unsupported type"); +} + +static bool IsConst(Operation* op) { + return isa(op); +} + +static bool IsTFResourceOp(Operation* op) { + for (const auto& operand : op->getOperands()) { + auto elementType = getElementTypeOrSelf(operand.getType()); + if (mlir::isa(elementType)) { + return true; + } + } + for (const auto& result : op->getResults()) { + auto elementType = getElementTypeOrSelf(result.getType()); + if (mlir::isa(elementType)) { + return true; + } + } + return false; +} + +// Returns whether the current op is not supported by the TF Lite runtime. +static bool IsUnsupportedFlexOp(const std::string& op_name) { + return op_name == "PartitionedCall" || op_name == "StatefulPartitionedCall"; +} + +// Returns whether the current location is supported by the TF Lite runtime. +// Refer to "LocationType" in +// tensorflow/compiler/mlir/lite/schema/debug_metadata.fbs. +static bool IsUnsupportedLocation(const mlir::Location& loc) { + return !mlir::isa(loc); +} + +// Create description of operation that could not be converted. +static std::string GetOpDescriptionForDebug(Operation* inst) { + const int kLargeElementsAttr = 16; + std::string op_str; + llvm::raw_string_ostream os(op_str); + inst->getName().print(os); + os << "("; + if (!inst->getOperandTypes().empty()) { + bool first = true; + for (Type operand_type : inst->getOperandTypes()) { + os << (!first ? ", " : ""); + first = false; + os << operand_type; + } + } + os << ") -> ("; + if (!inst->getResultTypes().empty()) { + bool first = true; + for (Type result_type : inst->getResultTypes()) { + os << (!first ? ", " : ""); + first = false; + os << result_type; + } + } + os << ")"; + // Print out attributes except for large elementsattributes (which should + // rarely be the cause why the legalization didn't happen). + if (!inst->getAttrDictionary().empty()) { + os << " : {"; + bool first = true; + for (auto& named_attr : inst->getAttrDictionary()) { + os << (!first ? ", " : ""); + first = false; + os << named_attr.getName().getValue() << " = "; + if (auto element_attr = + mlir::dyn_cast(named_attr.getValue())) { + if (element_attr.getNumElements() <= kLargeElementsAttr) { + element_attr.print(os); + } else { + os << ""; + } + } else { + named_attr.getValue().print(os); + } + } + os << "}"; + } + return os.str(); +} + +// Create a summary with the given information regarding op names and +// descriptions. +static std::string GetOpsSummary( + const std::map>& ops, + const std::string& summary_title) { + std::string op_str; + llvm::raw_string_ostream os(op_str); + + std::vector keys; + keys.reserve(ops.size()); + + std::vector values; + values.reserve(ops.size()); + + for (auto const& op_name_and_details : ops) { + keys.push_back(op_name_and_details.first); + for (auto const& op_detail : op_name_and_details.second) { + values.push_back(op_detail); + } + } + + os << summary_title << " ops: " << absl::StrJoin(keys, ", ") << "\n"; + os << "Details:\n\t" << absl::StrJoin(values, "\n\t"); + + return os.str(); +} + +template +static bool HasValidTFLiteType(Value value, T& error_handler) { + // None type is allowed to represent unspecified operands. + if (mlir::isa(value.getType())) return true; + + auto type = mlir::dyn_cast(value.getType()); + if (!type) { + if (auto op = value.getDefiningOp()) { + error_handler.emitError() + << '\'' << op << "' should produce value of tensor type instead of " + << value.getType(); + return false; + } + error_handler.emitError("expected tensor type, got ") << value.getType(); + return false; + } + + Type element_type = type.getElementType(); + auto status = GetTFLiteType(element_type); + if (!status.ok()) { + return error_handler.emitError( + formatv("Failed to convert element type '{0}': {1}", + element_type, status.status().message())), + false; + } + return true; +} + +// Returns true if the module holds all the invariants expected by the +// Translator class. +// TODO(hinsu): Now that translation is done by making a single pass over the +// MLIR module, consider inlining these validation checks at the place where +// these invariants are assumed instead of checking upfront. +static bool IsValidTFLiteMlirModule(ModuleOp module) { + MLIRContext* context = module.getContext(); + + // Verify that module has a function named main. + FuncOp main_fn = module.lookupSymbol("main"); + if (!main_fn) { + int entry_func_count = 0; + for (auto fn : module.getOps()) { + auto attrs = fn->getAttrOfType("tf.entry_function"); + if (attrs && !attrs.empty()) { + ++entry_func_count; + } + } + + // Verify that module has a least one enrty function. + if (entry_func_count == 0) { + return emitError(UnknownLoc::get(context), + "should have a least one entry function"), + false; + } + } + + for (auto fn : module.getOps()) { + if (!llvm::hasSingleElement(fn)) { + return fn.emitError("should have exactly one basic block"), false; + } + auto& bb = fn.front(); + + for (auto arg : bb.getArguments()) { + if (!HasValidTFLiteType(arg, fn)) { + auto elementType = getElementTypeOrSelf(arg.getType()); + if (mlir::isa(elementType)) { + return fn.emitError( + "function argument uses variant type. Currently, the " + "variant type is not natively supported in TFLite. Please " + "consider not using the variant type: ") + << arg.getType(), + false; + } + return fn.emitError("invalid TFLite type: ") << arg.getType(), false; + } + } + + // Verify that all operations except the terminator have exactly one + // result of type supported by TFLite (or is a ControlType, which + // will be removed later by ExtractControlEdges.) + for (auto& inst : bb) { + if (inst.hasTrait()) break; + + for (auto result : inst.getResults()) { + if (mlir::isa(result.getType())) continue; + if (!HasValidTFLiteType(result, inst)) { + auto elementType = getElementTypeOrSelf(result.getType()); + if (mlir::isa(elementType)) { + return inst.emitError( + "operand result uses variant type. Currently, the " + "variant type is not natively supported in TFLite. " + "Please " + "consider not using the variant type: ") + << result.getType(), + false; + } + return fn.emitError("invalid TFLite type: ") << result.getType(), + false; + } + } + } + } + + return true; +} + +static std::unique_ptr<::tensorflow::NodeDef> GetTensorFlowNodeDef( + ::mlir::Operation* inst) { + // We pass empty string for the original node_def name since Flex runtime + // does not care about this being set correctly on node_def. There is no + // "easy" (see b/120948529) way yet to get this from MLIR inst. + auto status_or_node_def = tensorflow::ConvertTFDialectOpToNodeDef( + inst, /*name=*/"", /*ignore_unregistered_attrs=*/true); + if (!status_or_node_def.ok()) { + inst->emitOpError( + Twine("failed to obtain TensorFlow nodedef with status: " + + status_or_node_def.status().ToString())); + return {}; + } + return std::move(status_or_node_def.value()); +} + +// Converts a mlir padding StringRef to TfLitePadding. +// Returns std::nullopt if conversion fails. +static std::optional GetTflitePadding(Operation* inst, + llvm::StringRef padding) { + const tflite::Padding padding_attr = + std::move(llvm::StringSwitch(padding) + .Case("SAME", tflite::Padding_SAME) + .Case("VALID", tflite::Padding_VALID)); + if (padding_attr == tflite::Padding_SAME) { + return kTfLitePaddingSame; + } + if (padding_attr == tflite::Padding_VALID) { + return kTfLitePaddingValid; + } + + return inst->emitOpError() << "Invalid padding attribute: " << padding, + std::nullopt; +} + +// Extracts TfLitePoolParams from a TFL custom op. +// Template parameter, TFLOp, should be a TFL custom op containing attributes +// generated from TfLitePoolParams. +// Returns std::nullopt if conversion fails. +template +static std::optional GetTflitePoolParams(Operation* inst, + TFLOp op) { + TfLitePoolParams pool_params; + pool_params.stride_height = op.stride_h().getSExtValue(); + pool_params.stride_width = op.stride_w().getSExtValue(); + pool_params.filter_height = op.filter_h().getSExtValue(); + pool_params.filter_width = op.filter_w().getSExtValue(); + const auto padding = GetTflitePadding(inst, op.padding()); + if (padding) { + pool_params.padding = *padding; + pool_params.activation = kTfLiteActNone; + pool_params.computed.padding = TfLitePaddingValues{0, 0, 0, 0}; + return pool_params; + } + + return std::nullopt; +} + +namespace { + +using ::mlir::tf_saved_model::kTfSavedModelExportedNamesAttr; +using ::mlir::tf_saved_model::kTfSavedModelIndexPathAttr; + +// Helper struct that wraps inputs/outputs of a single SignatureDef. +struct SignatureDefData { + // Note, we are using maps here to make order deterministic + // for easily testing only. + + // Inputs defined in the signature def mapped to tensor names. + std::map inputs; + // Outputs defined in the signature def mapped to tensor names. + std::map outputs; + // Signature key. + std::string signature_key; + // Subgraph index. + uint32_t subgraph_index; +}; + +// Helper class to store buffers for export. This class is used to store +// buffers for export and provides utility functions to get size and hash of +// the buffers. +template +class ExportBufferStorage { + public: + using ApplyDataFuncT = absl::FunctionRef; + using ApplyDataFromBufferFuncT = absl::AnyInvocable; + + class ExportBuffer { + public: + ExportBuffer(ItemT item, ApplyDataFromBufferFuncT apply, uint64_t hash, + uint64_t byte_size_hint = 0) + : item_(std::move(item)), + apply_(std::move(apply)), + hash_(hash), + byte_size_hint_(byte_size_hint) {} + + inline absl::Status ApplyData( + ExportBufferStorage::ApplyDataFuncT apply_data_func) { + return apply_(item_, apply_data_func); + } + + inline std::string GetData() { + std::string data; + data.reserve(32); + auto status = ApplyData([&data](absl::string_view chunk) { + data.append(chunk.data(), chunk.size()); + return absl::OkStatus(); + }); + return data; + } + + uint64_t hash() const { return hash_; } + uint64_t byte_size_hint() const { return byte_size_hint_; } + + ExportBuffer(const ExportBuffer&) = delete; + ExportBuffer& operator=(const ExportBuffer&) = delete; + ExportBuffer(ExportBuffer&&) noexcept = default; + ExportBuffer& operator=(ExportBuffer&&) noexcept = default; + + private: + ItemT item_; + ExportBufferStorage::ApplyDataFromBufferFuncT apply_; + uint64_t hash_; + uint64_t byte_size_hint_ = 0; + }; + + inline void Insert(KeyT key, ItemT item, + ExportBufferStorage::ApplyDataFromBufferFuncT apply, + uint64_t hash, uint64_t byte_size_hint = 0) { + auto export_buffer = std::make_unique( + std::move(item), std::move(apply), hash, byte_size_hint); + buffers_[key] = std::move(export_buffer); + } + + inline void Insert(KeyT key, std::unique_ptr buffer) { + buffers_[key] = std::move(buffer); + } + + auto& buffers() { return buffers_; } + + ExportBufferStorage(const ExportBufferStorage&) = delete; + ExportBufferStorage& operator=(const ExportBufferStorage&) = delete; + ExportBufferStorage(ExportBufferStorage&&) noexcept = default; + ExportBufferStorage& operator=(ExportBufferStorage&&) noexcept = default; + ExportBufferStorage() = default; + + private: + // Store pointers to the base class + absl::flat_hash_map> buffers_; +}; + +// Translates an MLIR module in TFLite dialect to TFLite FlatBuffer. +class Translator { + public: + // Translates the given MLIR module into TFLite FlatBuffer format and returns + // the serialized output. Returns std::nullopt on unsupported, invalid inputs + // or internal error. + static absl::Status Translate(ModuleOp module, + const tflite::FlatbufferExportOptions& options, + llvm::raw_pwrite_stream& export_stream); + + private: + enum class OpType : char { kTfliteBuiltin, kSelectTf, kCustomOp }; + explicit Translator( + ModuleOp module, llvm::raw_pwrite_stream& export_stream, + const tflite::ConverterFlags& converter_flags, + const std::unordered_set& saved_model_tags, + OpOrArgNameMapper* op_or_arg_name_mapper, + const std::map& metadata, + std::optional custom_option_alignment, + bool disable_buffer_deduping, + const std::vector& + attribute_buffer_applier_factories) + : module_(module), + name_mapper_(*op_or_arg_name_mapper), + builder_(kInitialBufferSize), + saved_model_tags_(saved_model_tags), + allow_all_select_tf_ops_(converter_flags.allow_all_select_tf_ops()), + select_user_tf_ops_(converter_flags.select_user_tf_ops().begin(), + converter_flags.select_user_tf_ops().end()), + metadata_(metadata), + supported_backends_(converter_flags.supported_backends().begin(), + converter_flags.supported_backends().end()), + serialize_debug_metadata_(converter_flags.serialize_debug_metadata()), + use_buffer_offset_(converter_flags.use_buffer_offset()), + custom_option_alignment_(custom_option_alignment), + disable_buffer_deduping_(disable_buffer_deduping), + export_stream_(export_stream), + attribute_buffer_applier_factories_( + attribute_buffer_applier_factories) { + // The first buffer must be empty according to the schema definition. + empty_buffer_ = tflite::CreateBuffer(builder_); + buffers_.push_back(empty_buffer_); + if (!converter_flags.force_select_tf_ops()) { + enabled_op_types_.emplace(OpType::kTfliteBuiltin); + } + if (converter_flags.enable_select_tf_ops()) { + enabled_op_types_.emplace(OpType::kSelectTf); + } + if (converter_flags.allow_custom_ops()) { + enabled_op_types_.emplace(OpType::kCustomOp); + } + tf_dialect_ = + module.getContext()->getOrLoadDialect(); + tfl_dialect_ = module.getContext() + ->getOrLoadDialect(); + vhlo_dialect_ = + module.getContext()->getOrLoadDialect(); + // Right now the TF executor dialect is still needed to build NodeDef. + module.getContext() + ->getOrLoadDialect(); + } + + absl::Status TranslateInternal(); + + // Returns TFLite buffer populated with constant value if the operation is + // TFLite constant operation. Otherwise, returns an empty buffer. Emits error + // and returns std::nullopt on failure. The buffer index may be changed if + // duplicated buffer is found. + std::optional> BuildBuffer( + Value value, bool can_be_deduplicated, int& index); + + // Builds external buffer and external buffer group from the given value. If + // the value is not defined by a constant op with external buffer attributes, + // returns std::nullopt. + std::optional> BuildExternalBuffer( + Value value, uint32_t external_buffer_id); + + // Build TFLite tensor from the given type. This function is for tfl.lstm + // intermediates, which should have UniformQuantizedType. + std::optional> BuildTensorFromType( + mlir::Type type, const std::string& name); + + // Builds TF::VariantType from the given element type. Returns std::nullopt if + // failure. Returns empty vector if the element type is not TF::VariantType or + // there is empty TensorType in the TF::VariantType. + std::optional>> + BuildTFVariantType(mlir::Type element_type); + + // Builds TFLite tensor from the given value. `buffer_idx` is index of the + // corresponding buffer. Emits error and returns std::nullopt on failure. + std::optional> BuildTensor( + Value value, const std::string& name, unsigned buffer_idx, + unsigned external_buffer_id, + const std::optional>& + quant_parameters); + + // TODO(b/137395003): Legalize tf.IfOp to TFLite dialect, and change the + // following method to handle TFL::IfOp. + BufferOffset BuildIfOperator( + mlir::TF::IfOp op, const std::vector& operands, + const std::vector& results); + + // Build while operator where cond & body are regions. + std::optional> BuildWhileOperator( + mlir::TFL::WhileOp op, const std::vector& operands, + const std::vector& results); + + // Build while operator where then & else are regions. + std::optional> BuildIfOperator( + mlir::TFL::IfOp op, const std::vector& operands, + const std::vector& results); + + // Build call once operator. + BufferOffset BuildCallOnceOperator( + mlir::TFL::CallOnceOp op, const std::vector& operands, + const std::vector& results); + + BufferOffset BuildNumericVerifyOperator( + mlir::TFL::NumericVerifyOp op, const std::vector& operands, + const std::vector& results); + + BufferOffset BuildCustomOperator( + Operation* inst, mlir::TFL::CustomOp op, + const std::vector& operands, + const std::vector& results); + + std::optional CreateFlexOpCustomOptions( + const ::tensorflow::NodeDef& node_def, const mlir::Location& loc); + + std::optional CreateCustomOpCustomOptions( + const ::tensorflow::NodeDef& node_def, const mlir::Location& loc); + + std::unique_ptr CreateFlexBuilderWithNodeAttrs( + const ::tensorflow::NodeDef& node_def, const mlir::Location& loc); + + // Returns opcode index for op identified by the op_name, if already + // available. Otherwise, creates a new OperatorCode using the given `builtin` + // operator and associates it with `op_name`. + uint32_t GetOpcodeIndex(const std::string& op_name, + tflite::BuiltinOperator builtin); + + // Builds operator for the given operation with specified operand and result + // tensor indices. Emits an error and returns std::nullopt on failure. + std::optional> BuildOperator( + Operation* inst, std::vector operands, + const std::vector& results, + const std::vector& intermediates); + + // Returns the quantization parameters for output value of "quant.stats" op. + BufferOffset + GetQuantizationForQuantStatsOpOutput(mlir::quantfork::StatisticsOp stats_op); + + // Build a subgraph with a given name out of the region either corresponding + // to a function's body or while op. Modifies *region by calling + // ExtractControlEdges. + std::optional> BuildSubGraph( + const std::string& name, Region* region, int index); + + // Modifies *block by unwrapping all ControlNodeOps. The DAG of the control + // dependencies is returned as a vector of its edges, with node indices into + // *block. + std::vector> ExtractControlEdges(mlir::Block* block); + + // Builds Metadata with the given `name` and buffer `content`. + BufferOffset BuildMetadata(StringRef name, + StringRef content); + + // Encodes the `tfl.metadata` dictionary attribute of the module to the + // metadata section in the final model. + std::optional>> + CreateMetadataVector(); + + // Serialize module's debug metadata. + std::string SerializeDebugMetadata(mlir::ModuleOp module); + + // Builds and returns list of tfl.SignatureDef sections in the model. + std::optional>> + CreateSignatureDefs(const std::vector& signature_defs); + + // Returns list of offsets for the passed 'items' in TensorMap structure + // inside the flatbuffer. + // 'items' is a map from tensor name in signatureDef to tensor name in + // the subgraph, specified by the 'subgraph_index' argument. + std::vector> GetList( + int subgraph_index, const std::map& items); + + // Uses the tf.entry_function attribute (if set) to initialize the op to name + // mapping. + void InitializeNamesFromAttribute(FuncOp fn, bool* has_input_attr); + + // Determines if the specified operation op's operand at operand_index + // is marked as a stateful operand. + bool IsStatefulOperand(mlir::Operation* op, int operand_index); + + // Returns a unique name for `val`. + std::string UniqueName(mlir::Value val); + + BufferOffset BuildSparsityParameters( + const mlir::TFL::SparsityParameterAttr& s_attr); + + bool EstimateArithmeticCount(int64_t* count); + + // Append constant and custom op buffers at the end of the flatbuffer and + // calculate the offsets + absl::Status AppendBufferData(); + + // Update constant & custom op buffer offsets + // Return false if fail to update offset + bool UpdateBufferOffsets(tflite::Model* mutable_model); + + // helper function for build stablehlo operators + std::optional> + BuildStablehloOperatorwithoutOptions(Operation* inst, + const std::vector& operands, + const std::vector& results, + tflite::BuiltinOperator op_code); + BufferOffset> BuildStablehloPrecisionConfig( + ::mlir::ArrayAttr precisionConfig); + + BufferOffset> BuildVhloPrecisionConfigV1( + mlir::vhlo::ArrayV1Attr precisionConfig); + + std::optional> BuildStablehloGatherOp( + mlir::stablehlo::GatherOp gather_op, const std::vector& operands, + const std::vector& results); + + std::optional> BuildStablehloScatterOp( + mlir::stablehlo::ScatterOp scatter_op, + const std::vector& operands, + const std::vector& results); + + std::optional> BuildStablehloReduceWindowOp( + mlir::stablehlo::ReduceWindowOp reduce_window_op, + const std::vector& operands, + const std::vector& results); + + std::optional> BuildStablehloRngBitGeneratorOp( + mlir::stablehlo::RngBitGeneratorOp rng_op, + const std::vector& operands, + const std::vector& results); + + std::optional> BuildStablehloPadOp( + mlir::stablehlo::PadOp pad_op, const std::vector& operands, + const std::vector& results); + + std::optional> BuildVhloGatherV1Op( + mlir::vhlo::GatherOpV1 gather_op, const std::vector& operands, + const std::vector& results, + mlir::VhloToStablehloTypeConverter& vhlo_type_converter); + + std::optional> BuildVhloCompositeV1Op( + mlir::vhlo::CompositeOpV1 composite_op, + const std::vector& operands, const std::vector& results, + std::string op_name); + + std::optional> BuildVhloScatterV1Op( + mlir::vhlo::ScatterOpV1 scatter_op, const std::vector& operands, + const std::vector& results, + mlir::VhloToStablehloTypeConverter& vhlo_type_converter); + + std::optional> BuildVhloReduceWindowV1Op( + mlir::vhlo::ReduceWindowOpV1 reduce_window_op, + const std::vector& operands, const std::vector& results, + mlir::VhloToStablehloTypeConverter& vhlo_type_converter); + + std::optional> BuildVhloRngBitGeneratorV1Op( + mlir::vhlo::RngBitGeneratorOpV1 rng_op, + const std::vector& operands, const std::vector& results, + mlir::VhloToStablehloTypeConverter& vhlo_type_converter); + + std::optional> BuildVhloPadV1Op( + mlir::vhlo::PadOpV1 pad_op, const std::vector& operands, + const std::vector& results, + mlir::VhloToStablehloTypeConverter& vhlo_type_converter); + + std::optional> BuildVhloCaseOp( + mlir::vhlo::CaseOpV1 op, const std::vector& operands, + const std::vector& results, + mlir::VhloToStablehloTypeConverter& vhlo_type_converter); + + // create a subgraph given a unnamed mlir region, return the corresponding + // subgraph index + int32_t UnnamedRegionToSubgraph(mlir::Region* region, + tflite::BuiltinOperator op_code); + + // Get debug metadata index of the given operator. + int GetOperatorDebugMetadataIndex(Operation* inst); + + ModuleOp module_; + + tensorflow::OpOrArgNameMapper& name_mapper_; + + flatbuffers::FlatBufferBuilder builder_; + BufferOffset empty_buffer_; + + std::vector> buffers_; + + // External buffers + std::vector> external_buffers_; + std::vector> + external_buffer_groups_; + absl::flat_hash_map external_buffer_group_map_; + + // Maps subgraph index and tensor name in the graph to the tensor index. + absl::flat_hash_map> + tensor_index_map_; + + // Maps op name to index of the corresponding OperatorCode in opcodes_ vector. + absl::flat_hash_map opcode_index_map_; + std::vector> opcodes_; + + // Maps function name to index of the corresponding subgraph in the FlatBuffer + // model. + absl::flat_hash_map subgraph_index_map_; + absl::flat_hash_set enabled_op_types_; + + // Maps buffer data to corresponding buffer index + // in the idx map, the value is a pair of offset and size + absl::flat_hash_map> buffer_idx_map_; + ExportBufferStorage> + const_buffer_storage_; + + // Maps custom options data to corresponding node + // Key is set to be the list of input tensor indices and list of output tensor + // indices + // in the idx map, the value is a pair of offset and size + absl::flat_hash_map, std::vector>, + std::pair> + custom_op_idx_map_; + ExportBufferStorage, std::vector>, + mlir::TFL::ConstBytesAttr> + custom_op_buffer_storage_; + + // Points to TensorFlow and TFLite dialects, respectively. nullptr if the + // dialect is not registered. + const Dialect* tf_dialect_; + const Dialect* tfl_dialect_; + const Dialect* vhlo_dialect_; + + // The failed ops during legalization. + std::map> failed_flex_ops_; + std::map> failed_custom_ops_; + + // Ops to provide warning messages. + std::map> custom_ops_; + std::map> flex_ops_; + + // Resource ops to provide warning messages. + std::map> resource_ops_; + + // Set of saved model tags, if any. + const std::unordered_set saved_model_tags_; + // Allows automatic pass through of TF ops as select Tensorflow ops. + const bool allow_all_select_tf_ops_; + // User's defined ops allowed with Flex. + const std::unordered_set select_user_tf_ops_; + // Map of key value pairs of metadata to export. + const std::map metadata_; + // User's defined supported backends. + const std::unordered_set supported_backends_; + // A mapping table to mlir::Operation objects for TFL subgraph and operator + // index in a flatbuffer. + std::vector> subgraph_op_inst_map_; + // A list of subgraphs in the model + std::vector> subgraphs_; + + // Will be populated by ExtractControlEdges to contain the control + // dependencies contained in the ControlNodeOps. Will then be used to populate + // metadata in the exported flatbuffer file. + tflite::ModelControlDependencies model_control_dependencies_; + + // Decide if we convert stablehlo ops in flatbuffer + bool convert_stablehlo_ = true; + + // Decide if the debug metadata is serialized. + bool serialize_debug_metadata_ = false; + + bool use_buffer_offset_ = false; + + std::optional custom_option_alignment_ = std::nullopt; + + bool disable_buffer_deduping_ = false; + + // Map from mlir constant attribute to the buffer index. This is used to + // deduplicate the buffers in the flatbuffer. + llvm::DenseMap const_attribute_to_buffer_map_; + + // Map subgraph name to its debug metadata index and all of its operations' + // debug metadata indexes. It is built during debug metadata creation, and is + // used during serializing subgraph/operator. + // ) + absl::flat_hash_map>> + debug_metadata_idx_map_; + + // The stream to export the flatbuffer bytes to. + std::reference_wrapper export_stream_; + + // The attribute buffer applier factories to use for exporting custom derived + // types of ElementsAttr as constant buffer holders. + std::vector + attribute_buffer_applier_factories_; + + std::string debug_metadata_serialized_string_; +}; + +bool Translator::EstimateArithmeticCount(int64_t* count) { + int64_t result = 0; + bool encounter_undetermined_mac = false; + module_->walk([&](mlir::TFL::TflArithmeticCountOpInterface op) { + int64_t mac_count = op.GetArithmeticCount(op); + if (mac_count < 0) { + encounter_undetermined_mac = true; + return; + } + result += mac_count; + }); + + *count = result; + return !encounter_undetermined_mac; +} + +std::string Translator::UniqueName(mlir::Value val) { + return std::string(name_mapper_.GetUniqueName(val)); +} + +std::optional> +Translator::BuildExternalBuffer(mlir::Value value, + uint32_t external_buffer_id) { + if (value.getDefiningOp() == nullptr) { + return std::nullopt; + } + auto inst = mlir::dyn_cast(value.getDefiningOp()); + if (!inst) { + return std::nullopt; + } + auto meta = inst.getExternalBufferAttr(); + if (!meta) { + return std::nullopt; + } + + std::string group_name = meta.getGroupName().str(); + uint64_t offset = meta.getOffset(); + uint64_t length = meta.getLength(); + std::string packing = meta.getPacking().str(); + + uint32_t group_index = 0; + if (auto it = external_buffer_group_map_.find(group_name); + it != external_buffer_group_map_.end()) { + group_index = it->second; + } else { + int index = external_buffer_groups_.size(); + external_buffer_groups_.push_back(tflite::CreateExternalBufferGroup( + builder_, builder_.CreateString(group_name))); + external_buffer_group_map_[group_name] = index; + group_index = index; + } + + auto external_buffer = tflite::CreateExternalBuffer( + builder_, external_buffer_id, group_index, offset, length, + builder_.CreateString(packing)); + return external_buffer; +} + +std::optional> Translator::BuildBuffer( + mlir::Value value, bool can_be_deduplicated, int& index) { + can_be_deduplicated = can_be_deduplicated && !disable_buffer_deduping_; + auto inst = value.getDefiningOp(); + ElementsAttr attr; + if (auto cst = dyn_cast(inst)) { + // arith::ConstantOp have ElementAttr at this point due to validation of the + // TFLite module. + attr = mlir::cast(cst.getValue()); + } else if (auto cst = dyn_cast(inst)) { + attr = cst.getValue(); + } else if (auto cst = dyn_cast(inst)) { + attr = cst.getValue(); + } else if (auto cst = dyn_cast(inst)) { + attr = cst.getValue(); + } else if (auto cst = dyn_cast(inst)) { + attr = cst.getValue(); + } else if (auto cst = dyn_cast(inst)) { + mlir::VhloToStablehloTypeConverter vhlo_type_converter; + auto tensor_v1_attr = mlir::cast(cst.getValue()); + attr = mlir::DenseIntOrFPElementsAttr::getFromRawBuffer( + mlir::cast( + vhlo_type_converter.convertType(tensor_v1_attr.getType())), + tensor_v1_attr.getData().getRawData()); + } else if (auto cst = dyn_cast(inst)) { + attr = cst.getCompressedData(); + } else if (auto cst = dyn_cast(inst)) { + attr = cst.getCompressedData(); + } else { + return empty_buffer_; + } + + if (can_be_deduplicated) { + if (const_attribute_to_buffer_map_.find(attr) != + const_attribute_to_buffer_map_.end()) { + index = const_attribute_to_buffer_map_[attr]; + return empty_buffer_; + } + const_attribute_to_buffer_map_[attr] = index; + } + + tflite::FlatbufferExportAttributeBufferApplier applier = nullptr; + + // TF doesn't currently support 4-bit types (DT_INT4), so we'll run into + // trouble calling ConvertToTensor(). For now, extract the tensor data from + // ElementsAttr directly in this and read type from tflite::TensorType instead + // of tensorflow::DataType. + auto type = mlir::cast(value.getType()); + tflite::TensorType tflite_element_type = + GetTFLiteType(type.getElementType()).value(); + + // Default appliers + if (tflite_element_type == tflite::TensorType_INT4 || + tflite_element_type == tflite::TensorType_UINT4 || + tflite_element_type == tflite::TensorType_INT2) { + applier = + [tflite_element_type]( + const std::pair& attr_and_inst, + auto apply) { + auto attr = mlir::cast(attr_and_inst.first); + bool is_8bit_raw_data = + !attr.isSplat() && + attr.getNumElements() == attr.getRawData().size(); + bool is_4bit_data = tflite_element_type == tflite::TensorType_INT4 || + tflite_element_type == tflite::TensorType_UINT4; + + if (is_8bit_raw_data) { + if (is_4bit_data) { + return tflite::StreamPackLowBitValues8Bit( + attr.getRawData(), apply); + } else { + return tflite::StreamPackLowBitValues8Bit( + attr.getRawData(), apply); + } + } else { + if (is_4bit_data) { + return tflite::StreamPackLowBitValues( + attr.getValues(), apply); + } else { + return tflite::StreamPackLowBitValues( + attr.getValues(), apply); + } + } + }; + } else { + applier = + [](const std::pair& attr_and_inst, + auto apply) { + auto [attr_, inst] = attr_and_inst; + auto attr = mlir::cast(attr_); + auto shaped_type = mlir::dyn_cast(attr.getType()); + + // Optimization Path for Numeric Dtypes (f32, i32, etc.) + // Flatbuffer requires little endian for numeric types. If the host + // is big endian, rely on the TensorFlow path below to reverse the + // byte order. + if (llvm::sys::IsLittleEndianHost && shaped_type && + shaped_type.getElementType().isIntOrFloat()) { + int64_t expected_size = mlir::TFL::GetSizeInBytes(shaped_type); + + // DenseElementsAttr + if (auto dense_attr = + mlir::dyn_cast(attr)) { + // Only use the optimization if it's not a single-value (Splat) + // and the raw memory matches the logical dimensions exactly. + if (!dense_attr.isSplat() && + dense_attr.getRawData().size() == expected_size) { + return apply(absl::string_view(dense_attr.getRawData().data(), + dense_attr.getRawData().size())); + } + } + + // DenseResourceElementsAttr + if (auto res_attr = + mlir::dyn_cast(attr)) { + if (auto blob = + res_attr.getRawHandle().getResource()->getBlob()) { + auto data = blob->getData(); + if (data.size() == expected_size) { + return apply(absl::string_view( + reinterpret_cast(data.data()), data.size())); + } + } + } + } + + // Fallback Path: use TensorFlow's conversion for compatibility. + auto tensor = std::make_unique(); + auto status = tensorflow::ConvertToTensor(attr, tensor.get()); + + if (!status.ok()) { + std::string error_message = + "failed to convert value attribute to tensor with error: " + + status.ToString(); + inst->emitError(Twine(error_message)); + return absl::InvalidArgumentError(error_message); + } + + // TensorFlow and TensorFlow Lite use different string encoding + // formats. Convert to TensorFlow Lite format is it's a constant + // string tensor. + if (tensor->dtype() == tensorflow::DT_STRING) { + ::mlir::TFL::SimpleDynamicBuffer dynamic_buffer; + auto flat = tensor->flat<::tensorflow::tstring>(); + for (int i = 0; i < flat.size(); ++i) { + const auto& str = flat(i); + if (!dynamic_buffer.AddString(str.c_str(), str.length())) { + std::string error_message = + "failed to add string to dynamic buffer with error: " + + status.ToString(); + inst->emitError(Twine(error_message)); + return absl::InvalidArgumentError(error_message); + } + } + + char* tensor_buffer; + int bytes = dynamic_buffer.WriteToBuffer(&tensor_buffer); + auto apply_result = apply(absl::string_view(tensor_buffer, bytes)); + free(tensor_buffer); + + return apply_result; + } else { + return apply(tensor->tensor_data()); + } + }; + } + + // Try to get custom appliers + for (auto& factory : attribute_buffer_applier_factories_) { + auto applier_or = factory(attr, inst); + if (applier_or.has_value()) { + applier = std::move(applier_or.value()); + break; + } + } + + if (use_buffer_offset_) { + // If use_buffer_offset_ is true, creates an empty buffer with offset 0. + + // The hash value of the attribute is used as the buffer fingerprint for + // deduplication. This is much cheaper than serializing the attribute to a + // string and computing the hash of the string, but can be reliable in some + // cases where the MLIR attributes are not deduped properly (e.g. when two + // consts of the same value are held in different attribute types). + const_buffer_storage_.Insert( + index, std::make_pair(attr, inst), std::move(applier), + /*hash=*/mlir::hash_value(attr), + /*byte_size_hint=*/mlir::TFL::GetSizeInBytes(type)); + return tflite::CreateBuffer(builder_, 0, 1, 1); + } else { + // Otherwise, creates a buffer with the flatbuffer immediately. + ExportBufferStorage>:: + ExportBuffer buffer(std::make_pair(attr, inst), std::move(applier), + /*hash=*/0); + + std::string buffer_data = buffer.GetData(); + if (custom_option_alignment_.has_value()) { + builder_.ForceVectorAlignment(buffer_data.size(), sizeof(uint8_t), + custom_option_alignment_.value()); + } + return tflite::CreateBuffer( + builder_, builder_.CreateVector( + reinterpret_cast(buffer_data.data()), + buffer_data.size())); + } +} + +int32_t Translator::UnnamedRegionToSubgraph( + mlir::Region* region, const tflite::BuiltinOperator op_code) { + int32_t subgraph_index = subgraphs_.size(); + std::string op_name = tflite::EnumNamesBuiltinOperator()[op_code]; + std::string graph_name = op_name + std::to_string(subgraph_index); + auto subgraph = BuildSubGraph(graph_name, region, subgraph_index); + if (!subgraph.has_value()) { + mlir::emitError(region->getLoc(), "failed to build subgraph"); + return -1; + } + subgraphs_.push_back(subgraph.value()); + subgraph_index_map_[graph_name] = subgraph_index; + return subgraph_index; +} + +int Translator::GetOperatorDebugMetadataIndex(Operation* inst) { + int operator_debug_metadata_idx = -1; + if (auto funcOp = inst->getParentOfType()) { + if (auto it = debug_metadata_idx_map_.find(funcOp.getName().str()); + it != debug_metadata_idx_map_.end()) { + auto& operator_index_map = it->second.second; + if (auto it = operator_index_map.find(inst); + it != operator_index_map.end()) { + operator_debug_metadata_idx = it->second; + } + } + } + return operator_debug_metadata_idx; +} + +std::optional>> +Translator::BuildTFVariantType(mlir::Type element_type) { + std::vector> variant_params; + auto variant_type = mlir::dyn_cast(element_type); + if (!variant_type) { + return variant_params; + } + + // We only support up to one nested type in tf_type.variant_type. + if (variant_type.getSubtypes().size() > 1) { + return std::nullopt; + } + if (variant_type.getSubtypes().empty()) { + return variant_params; + } + mlir::TensorType tensor_type = variant_type.getSubtypes().front(); + tflite::TensorType tflite_element_type = + GetTFLiteType(tensor_type.getElementType()).value(); + std::vector shape; + if (tensor_type.hasRank()) { + llvm::ArrayRef shape_ref = tensor_type.getShape(); + shape = std::vector(shape_ref.begin(), shape_ref.end()); + } + + variant_params.push_back( + tflite::CreateVariantSubType(builder_, builder_.CreateVector(shape), + tflite_element_type, tensor_type.hasRank())); + return variant_params; +} + +std::optional> Translator::BuildTensorFromType( + mlir::Type type, const std::string& name) { + auto tensor_type = mlir::cast(type); + + llvm::ArrayRef shape_ref; + std::vector shape; + + if (tensor_type.hasRank()) { + if (tensor_type.hasStaticShape()) { + shape_ref = tensor_type.getShape(); + shape = std::vector(shape_ref.begin(), shape_ref.end()); + } else { + return std::nullopt; + } + } + + auto element_type = tensor_type.getElementType(); + tflite::TensorType tflite_element_type = + GetTFLiteType(tensor_type.getElementType()).value(); + std::optional>> + variant_params = BuildTFVariantType(element_type); + if (!variant_params.has_value()) { + return std::nullopt; + } + BufferOffset q_params = 0; + if (auto qtype = + mlir::dyn_cast(element_type)) { + std::vector scales = {static_cast(qtype.getScale())}; + std::vector zero_points = {qtype.getZeroPoint()}; + q_params = tflite::CreateQuantizationParameters( + builder_, /*min=*/0, /*max=*/0, builder_.CreateVector(scales), + builder_.CreateVector(zero_points)); + } else if (auto qtype = mlir::dyn_cast( + element_type)) { + std::vector mins = {static_cast(qtype.getMin())}; + std::vector maxs = {static_cast(qtype.getMax())}; + q_params = tflite::CreateQuantizationParameters( + builder_, builder_.CreateVector(mins), + builder_.CreateVector(maxs)); + } + return tflite::CreateTensor( + builder_, builder_.CreateVector(shape), tflite_element_type, + /*buffer=*/0, builder_.CreateString(name), q_params, + /*is_variable=*/false, /*sparsity=*/0, /*shape_signature=*/0, + /*has_rank=*/tensor_type.hasRank(), + variant_params->empty() ? 0 : builder_.CreateVector(*variant_params), + /*external_buffer=*/0); +} + +std::optional> Translator::BuildTensor( + Value value, const std::string& name, unsigned buffer_idx, + unsigned external_buffer_id, + const std::optional>& + quant_parameters) { + auto type = mlir::cast(value.getType()); + + // TFLite requires tensor shape only for the inputs and constants. + // However, we output all known shapes for better round-tripping + auto check_shape = + [&](llvm::ArrayRef shape_ref) -> mlir::LogicalResult { + auto is_out_of_range = [](int64_t dim) { + return dim > std::numeric_limits::max(); + }; + + if (std::any_of(shape_ref.begin(), shape_ref.end(), is_out_of_range)) + return mlir::emitError( + value.getLoc(), + "result shape dimensions out of 32 bit int type range"); + + return mlir::success(); + }; + + std::vector shape; + std::vector shape_signature; + auto* inst = value.getDefiningOp(); + if (type.hasStaticShape()) { + llvm::ArrayRef shape_ref = type.getShape(); + if (mlir::failed(check_shape(shape_ref))) return std::nullopt; + + shape = std::vector(shape_ref.begin(), shape_ref.end()); + } else if (inst && IsConst(inst)) { + // Const op can have a result of dynamic shaped type (e.g. due to constant + // folding), but we can still derive the shape of a constant tensor for + // its attribute type. + auto tensor_attr = mlir::cast(inst->getAttr("value")); + llvm::ArrayRef shape_ref = + mlir::cast(tensor_attr.getType()).getShape(); + if (mlir::failed(check_shape(shape_ref))) return std::nullopt; + + shape = std::vector(shape_ref.begin(), shape_ref.end()); + } else if (type.hasRank()) { + llvm::ArrayRef shape_ref = type.getShape(); + if (mlir::failed(check_shape(shape_ref))) return std::nullopt; + + shape.reserve(shape_ref.size()); + for (auto& dim : shape_ref) { + // translate dynamic shapes from mlir to tfl values + shape.push_back( + dim == mlir::ShapedType::kDynamic ? 1 : static_cast(dim)); + shape_signature.push_back(static_cast( + dim == mlir::ShapedType::kDynamic ? tensorflow::kTFDynamicSize + : dim)); + } + } + + BufferOffset s_params = 0; + if (auto* inst = value.getDefiningOp()) { + if (auto cst = dyn_cast(inst)) { + s_params = BuildSparsityParameters(cst.getSParam()); + } else if (auto cst = dyn_cast(inst)) { + s_params = BuildSparsityParameters(cst.getSParam()); + } + } + + Type element_type = type.getElementType(); + tflite::TensorType tflite_element_type = + GetTFLiteType(type.getElementType()).value(); + + std::optional>> + variant_params = BuildTFVariantType(element_type); + if (!variant_params.has_value()) { + return std::nullopt; + } + + BufferOffset q_params; + if (auto qtype = + mlir::dyn_cast(element_type)) { + std::vector scales = {static_cast(qtype.getScale())}; + std::vector zero_points = {qtype.getZeroPoint()}; + q_params = tflite::CreateQuantizationParameters( + // min and max values are not stored in the quantized type from MLIR, so + // both are set to 0 in the flatbuffer when they are exported. + builder_, /*min=*/0, /*max=*/0, builder_.CreateVector(scales), + builder_.CreateVector(zero_points)); + } else if (auto qtype = + mlir::dyn_cast( + element_type)) { + std::vector scales(qtype.getScales().begin(), + qtype.getScales().end()); + std::vector zero_points(qtype.getZeroPoints().begin(), + qtype.getZeroPoints().end()); + q_params = tflite::CreateQuantizationParameters( + builder_, /*min=*/0, /*max=*/0, builder_.CreateVector(scales), + builder_.CreateVector(zero_points), + tflite::QuantizationDetails_NONE, /*details=*/0, + qtype.getQuantizedDimension()); + } else if (quant_parameters.has_value()) { + q_params = quant_parameters.value(); + } else { + q_params = tflite::CreateQuantizationParameters(builder_); + } + // Check if the value's uses includes an op and usage at an operand index + // marked as a stateful. If so, set the tensor's is_variable as true + // This is v1 ref variable semantics in the TFLite runtime. + bool is_variable = false; + for (auto& use : value.getUses()) { + is_variable = IsStatefulOperand(use.getOwner(), use.getOperandNumber()); + if (is_variable) { + break; + } + } + // The value is used as a variable if produced by an op with "tfl.is_variable" + // attribute. This provides a hook for the user to represent the variable + // tensor in the MLIR level. + if (auto* inst = value.getDefiningOp(); + inst && inst->hasAttr("tfl.is_variable")) { + is_variable = true; + } + + bool has_rank = type.hasRank(); + + if (shape_signature.empty()) { + return tflite::CreateTensor( + builder_, builder_.CreateVector(shape), tflite_element_type, + (is_variable ? 0 : buffer_idx), builder_.CreateString(name), q_params, + /*is_variable=*/is_variable, s_params, /*shape_signature=*/0, + /*has_rank=*/has_rank, + variant_params->empty() ? 0 : builder_.CreateVector(*variant_params), + external_buffer_id); + } else { + return tflite::CreateTensor( + builder_, builder_.CreateVector(shape), tflite_element_type, + (is_variable ? 0 : buffer_idx), builder_.CreateString(name), q_params, + /*is_variable=*/is_variable, s_params, + /*shape_signature=*/builder_.CreateVector(shape_signature), + /*has_rank=*/has_rank, + variant_params->empty() ? 0 : builder_.CreateVector(*variant_params), + external_buffer_id); + } +} + +BufferOffset Translator::BuildIfOperator( + mlir::TF::IfOp op, const std::vector& operands, + const std::vector& results) { + auto opcode_index = GetOpcodeIndex("if", tflite::BuiltinOperator_IF); + int then_subgraph_index = subgraph_index_map_.at(op.getThenBranch().str()); + int else_subgraph_index = subgraph_index_map_.at(op.getElseBranch().str()); + auto builtin_options = tflite::CreateIfOptions(builder_, then_subgraph_index, + else_subgraph_index) + .Union(); + auto inputs = builder_.CreateVector(operands); + auto outputs = builder_.CreateVector(results); + return tflite::CreateOperator( + builder_, opcode_index, inputs, outputs, tflite::BuiltinOptions_IfOptions, + builtin_options, /*custom_options=*/0, + /*custom_options_format=*/tflite::CustomOptionsFormat_FLEXBUFFERS, + /*mutating_variable_inputs=*/0, + /*intermediates=*/0, + /*large_custom_options_offset=*/0, + /*large_custom_options_size=*/0, + /*builtin_options_2_type=*/tflite::BuiltinOptions2_NONE, + /*builtin_options_2=*/0, + /*debug_metadata_index=*/ + GetOperatorDebugMetadataIndex(op.getOperation())); +} + +BufferOffset Translator::BuildCallOnceOperator( + mlir::TFL::CallOnceOp op, const std::vector& operands, + const std::vector& results) { + auto opcode_index = + GetOpcodeIndex("call_once", tflite::BuiltinOperator_CALL_ONCE); + int init_subgraph_index = + subgraph_index_map_.at(op.getSessionInitFunction().str()); + auto builtin_options = + tflite::CreateCallOnceOptions(builder_, init_subgraph_index).Union(); + auto inputs = builder_.CreateVector(operands); + auto outputs = builder_.CreateVector(results); + return tflite::CreateOperator( + builder_, opcode_index, inputs, outputs, + tflite::BuiltinOptions_CallOnceOptions, builtin_options, + /*custom_options=*/0, + /*custom_options_format=*/tflite::CustomOptionsFormat_FLEXBUFFERS, + /*mutating_variable_inputs=*/0, + /*intermediates=*/0, + /*large_custom_options_offset=*/0, + /*large_custom_options_size=*/0, + /*builtin_options_2_type=*/tflite::BuiltinOptions2_NONE, + /*builtin_options_2=*/0, + /*debug_metadata_index=*/ + GetOperatorDebugMetadataIndex(op.getOperation())); +} + +std::optional> Translator::BuildWhileOperator( + mlir::TFL::WhileOp op, const std::vector& operands, + const std::vector& results) { + auto opcode_index = GetOpcodeIndex("while", tflite::BuiltinOperator_WHILE); + auto get_call_index = [&](mlir::Block& b) -> std::optional { + if (b.getOperations().size() != 2) return std::nullopt; + if (auto call_op = dyn_cast(b.front())) + return subgraph_index_map_.at(call_op.getCallee().str()); + return std::nullopt; + }; + auto body_subgraph_index = get_call_index(op.getBody().front()); + auto cond_subgraph_index = get_call_index(op.getCond().front()); + if (!body_subgraph_index || !cond_subgraph_index) + return op.emitOpError("only single call cond/body while export supported"), + std::nullopt; + auto builtin_options = + tflite::CreateWhileOptions(builder_, *cond_subgraph_index, + *body_subgraph_index) + .Union(); + auto inputs = builder_.CreateVector(operands); + auto outputs = builder_.CreateVector(results); + return tflite::CreateOperator( + builder_, opcode_index, inputs, outputs, + tflite::BuiltinOptions_WhileOptions, builtin_options, + /*custom_options=*/0, + /*custom_options_format=*/tflite::CustomOptionsFormat_FLEXBUFFERS, + /*mutating_variable_inputs=*/0, /*intermediates=*/0, + /*large_custom_options_offset=*/0, /*large_custom_options_size=*/0, + /*builtin_options_2_type=*/tflite::BuiltinOptions2_NONE, + /*builtin_options_2=*/0, + GetOperatorDebugMetadataIndex(op.getOperation())); +} + +std::optional> Translator::BuildIfOperator( + mlir::TFL::IfOp op, const std::vector& operands, + const std::vector& results) { + auto opcode_index = GetOpcodeIndex("if", tflite::BuiltinOperator_IF); + auto get_call_op = [&](mlir::Block& b) -> std::optional { + if (b.getOperations().size() != 2) return std::nullopt; + if (auto call_op = dyn_cast(b.front())) return call_op; + return std::nullopt; + }; + auto then_call_op = get_call_op(op.getThenRegion().front()); + auto else_call_op = get_call_op(op.getElseRegion().front()); + if (!then_call_op || !else_call_op) + return op.emitOpError("only single call then/else while export supported"), + std::nullopt; + auto then_subgraph_index = + subgraph_index_map_.at(then_call_op.value().getCallee().str()); + auto else_subgraph_index = + subgraph_index_map_.at(else_call_op.value().getCallee().str()); + auto builtin_options = tflite::CreateIfOptions(builder_, then_subgraph_index, + else_subgraph_index) + .Union(); + + // Get the subgraph index of IF op. + auto subgraph_func = op->getParentOfType(); + auto subgraph_idx = subgraph_index_map_[subgraph_func.getSymName().str()]; + auto new_operands = operands; + + // Then/Else region shares the same operands, only adding once as the new + // operands for the IF op. + if (then_call_op.value().getOperands() != + else_call_op.value().getOperands()) { + return op.emitOpError("Then/Else region does not contain same operands."), + std::nullopt; + } + + for (auto call_arg : then_call_op.value().getOperands()) { + auto name_of_call_arg = name_mapper_.GetUniqueName(call_arg); + const auto call_arg_tensor_id = + tensor_index_map_[subgraph_idx][name_of_call_arg]; + new_operands.push_back(call_arg_tensor_id); + } + auto inputs = builder_.CreateVector(new_operands); + auto outputs = builder_.CreateVector(results); + return tflite::CreateOperator( + builder_, opcode_index, inputs, outputs, tflite::BuiltinOptions_IfOptions, + builtin_options, /*custom_options=*/0, + /*custom_options_format=*/tflite::CustomOptionsFormat_FLEXBUFFERS, + /*mutating_variable_inputs=*/0, /*intermediates=*/0, + /*large_custom_options_offset=*/0, /*large_custom_options_size=*/0, + /*builtin_options_2_type=*/tflite::BuiltinOptions2_NONE, + /*builtin_options_2=*/0, + GetOperatorDebugMetadataIndex(op.getOperation())); +} + +BufferOffset Translator::BuildNumericVerifyOperator( + mlir::TFL::NumericVerifyOp op, const std::vector& operands, + const std::vector& results) { + float tolerance = op.getTolerance().convertToFloat(); + bool log_if_failed = op.getLogIfFailed(); + auto fbb = std::make_unique(); + fbb->Map([&]() { + fbb->Float("tolerance", tolerance); + fbb->Bool("log_if_failed", log_if_failed); + }); + fbb->Finish(); + auto f = std::unique_ptr(fbb.release()); + auto custom_option = f->GetBuffer(); + auto opcode_index = + GetOpcodeIndex("NumericVerify", tflite::BuiltinOperator_CUSTOM); + + return tflite::CreateOperator( + builder_, opcode_index, builder_.CreateVector(operands), + builder_.CreateVector(results), tflite::BuiltinOptions_NONE, + /*builtin_options=*/0, + /*custom_options=*/builder_.CreateVector(custom_option), + /*custom_options_format=*/tflite::CustomOptionsFormat_FLEXBUFFERS, + /*mutating_variable_inputs=*/0, + /*intermediates=*/0, /*large_custom_options_offset=*/0, + /*large_custom_options_size=*/0, + /*builtin_options_2_type=*/tflite::BuiltinOptions2_NONE, + /*builtin_options_2=*/0, + /*debug_metadata_index=*/ + GetOperatorDebugMetadataIndex(op.getOperation())); +} + +BufferOffset Translator::BuildCustomOperator( + Operation* inst, mlir::TFL::CustomOp op, + const std::vector& operands, const std::vector& results) { + ExportBufferStorage, std::vector>, + mlir::TFL::ConstBytesAttr>::ExportBuffer + export_buffer( + op.getCustomOption(), + [](const mlir::TFL::ConstBytesAttr& attr, auto apply) { + return apply(attr.getValue().str()); + }, + /*hash=*/mlir::hash_value(op.getCustomOption()), + /*byte_size_hint=*/op.getCustomOption().getValue().size()); + + auto opcode_index = + GetOpcodeIndex(op.getCustomCode().str(), tflite::BuiltinOperator_CUSTOM); + if (use_buffer_offset_) { + custom_op_buffer_storage_.Insert( + std::make_pair(operands, results), + std::make_unique(std::move(export_buffer))); + return tflite::CreateOperator( + builder_, opcode_index, builder_.CreateVector(operands), + builder_.CreateVector(results), tflite::BuiltinOptions_NONE, + /*builtin_options=*/0, /*custom_options=*/0, + /*custom_options_format=*/tflite::CustomOptionsFormat_FLEXBUFFERS, + /*mutating_variable_inputs=*/0, /*intermediates=*/0, + /*large_custom_options_offset=*/1, /*large_custom_options_size=*/1, + /*builtin_options_2_type=*/tflite::BuiltinOptions2_NONE, + /*builtin_options_2=*/0, + /*debug_metadata_index=*/ + GetOperatorDebugMetadataIndex(inst)); + } else { + std::string custom_option_data = export_buffer.GetData(); + + if (custom_option_alignment_.has_value()) { + builder_.ForceVectorAlignment(custom_option_data.size(), sizeof(uint8_t), + custom_option_alignment_.value()); + } + auto custom_option_fbs_vector = builder_.CreateVector( + reinterpret_cast(custom_option_data.data()), + custom_option_data.size()); + return tflite::CreateOperator( + builder_, opcode_index, builder_.CreateVector(operands), + builder_.CreateVector(results), tflite::BuiltinOptions_NONE, + /*builtin_options=*/0, custom_option_fbs_vector, + tflite::CustomOptionsFormat_FLEXBUFFERS, /*mutating_variable_inputs=*/0, + /*intermediates=*/0, /*large_custom_options_offset=*/0, + /*large_custom_options_size=*/0, + /*builtin_options_2_type=*/tflite::BuiltinOptions2_NONE, + /*builtin_options_2=*/0, + /*debug_metadata_index=*/ + GetOperatorDebugMetadataIndex(inst)); + } +} + +std::optional Translator::CreateFlexOpCustomOptions( + const ::tensorflow::NodeDef& node_def, const mlir::Location& loc) { + std::string node_def_str; + if (!node_def.SerializeToString(&node_def_str)) { + return emitError(loc, "failed to serialize tensorflow node_def"), + std::nullopt; + } + + auto flex_builder = std::make_unique(); + flex_builder->Vector([&]() { + flex_builder->String(node_def.op()); + flex_builder->String(node_def_str); + }); + flex_builder->Finish(); + return builder_.CreateVector(flex_builder->GetBuffer()); +} + +std::optional Translator::CreateCustomOpCustomOptions( + const ::tensorflow::NodeDef& node_def, const mlir::Location& loc) { + auto flex_builder = CreateFlexBuilderWithNodeAttrs(node_def, loc); + return builder_.CreateVector(flex_builder->GetBuffer()); +} + +std::unique_ptr +Translator::CreateFlexBuilderWithNodeAttrs( + const ::tensorflow::NodeDef& node_def, const mlir::Location& loc) { + auto flex_builder = std::make_unique(); + size_t map_start = flex_builder->StartMap(); + using Item = std::pair; + std::vector attrs(node_def.attr().begin(), node_def.attr().end()); + std::sort(attrs.begin(), attrs.end(), + [](Item& p1, Item& p2) -> bool { return p1.first < p2.first; }); + for (const Item& pair : attrs) { + const char* key = pair.first.c_str(); + const ::tensorflow::AttrValue& attr = pair.second; + switch (attr.value_case()) { + case ::tensorflow::AttrValue::kS: + flex_builder->String(key, attr.s()); + break; + case ::tensorflow::AttrValue::kType: { + auto status_or_tfl_type = tflite::TfTypeToTflType(attr.type()); + if (status_or_tfl_type.ok()) { + flex_builder->Int(key, status_or_tfl_type.value()); + } else { + emitWarning(loc, "ignoring unsupported tensorflow type: ") + << std::to_string(attr.type()); + } + break; + } + case ::tensorflow::AttrValue::kI: + flex_builder->Int(key, attr.i()); + break; + case ::tensorflow::AttrValue::kF: + flex_builder->Float(key, attr.f()); + break; + case ::tensorflow::AttrValue::kB: + flex_builder->Bool(key, attr.b()); + break; + case tensorflow::AttrValue::kList: + if (attr.list().s_size() > 0) { + auto start = flex_builder->StartVector(key); + for (const std::string& v : attr.list().s()) { + flex_builder->Add(v); + } + flex_builder->EndVector(start, /*typed=*/true, /*fixed=*/false); + } else if (attr.list().i_size() > 0) { + auto start = flex_builder->StartVector(key); + for (const int64_t v : attr.list().i()) { + flex_builder->Add(v); + } + flex_builder->EndVector(start, /*typed=*/true, /*fixed=*/false); + } else if (attr.list().f_size() > 0) { + auto start = flex_builder->StartVector(key); + for (const float v : attr.list().f()) { + flex_builder->Add(v); + } + flex_builder->EndVector(start, /*typed=*/true, /*fixed=*/false); + } else { + emitWarning(loc, + "ignoring unsupported type in list attribute with key: ") + << key; + } + break; + default: + emitWarning(loc, "ignoring unsupported attribute type with key: ") + << key; + break; + } + } + flex_builder->EndMap(map_start); + flex_builder->Finish(); + return flex_builder; +} + +uint32_t Translator::GetOpcodeIndex(const std::string& op_name, + tflite::BuiltinOperator builtin) { + auto it = opcode_index_map_.insert({op_name, 0}); + + // If the insert succeeded, the opcode has not been created already. Create a + // new operator code and update its index value in the map. + if (it.second) { + it.first->second = opcodes_.size(); + auto custom_code = builtin == tflite::BuiltinOperator_CUSTOM + ? builder_.CreateString(op_name) + : BufferOffset(); + // Use version 0 for builtin op. This is a way to serialize version field to + // flatbuffer (since 0 is non default) and it will be corrected later. + int32_t op_version = builtin != tflite::BuiltinOperator_CUSTOM ? 0 : 1; + opcodes_.push_back(CreateOperatorCode(builder_, /*builtin_code=*/builtin, + custom_code, op_version)); + } + return it.first->second; +} + +void CreateFlexbufferVector( + const std::unique_ptr& flex_builder, + std::optional key, const mlir::Attribute& attr) { + auto start = key.has_value() + ? flex_builder->StartVector(std::string(*key).c_str()) + : flex_builder->StartVector(); + auto array = mlir::cast(attr).getValue(); + + for (int i = 0; i < array.size(); i++) { + if (llvm::isa(array[i])) { + flex_builder->Bool(mlir::cast(array[i]).getValue()); + } else if (llvm::isa(array[i])) { + flex_builder->String( + mlir::cast(array[i]).getValue().str()); + } else if (llvm::isa(array[i])) { + flex_builder->Bool( + mlir::cast(array[i]).getValue()); + } else if (llvm::isa(array[i])) { + flex_builder->String( + mlir::cast(array[i]).getValue().str()); + } else if (llvm::isa(array[i])) { + flex_builder->Int(mlir::cast(array[i]) + .getValue() + .getSExtValue()); + } else if (llvm::isa(array[i])) { + flex_builder->Float(mlir::cast(array[i]) + .getValue() + .convertToFloat()); + + } else if (llvm::isa(array[i])) { + CreateFlexbufferVector(flex_builder, std::nullopt, array[i]); + } + } + + flex_builder->EndVector(start, /*typed=*/false, /*fixed=*/false); +} + +std::optional> +Translator::BuildStablehloOperatorwithoutOptions( + Operation* inst, const std::vector& operands, + const std::vector& results, + const tflite::BuiltinOperator op_code) { + std::string op_name = inst->getName().getStringRef().str(); + uint32_t opcode_index = GetOpcodeIndex(op_name, op_code); + + return tflite::CreateOperator( + builder_, opcode_index, builder_.CreateVector(operands), + builder_.CreateVector(results), tflite::BuiltinOptions_NONE, 0, 0, + tflite::CustomOptionsFormat_FLEXBUFFERS, 0, 0, 0, 0, + tflite::BuiltinOptions2_NONE, 0, GetOperatorDebugMetadataIndex(inst)); +} + +BufferOffset> +Translator::BuildStablehloPrecisionConfig(::mlir::ArrayAttr precisionConfig) { + std::vector precision_config_vec; + + for (auto it = precisionConfig.begin(); it != precisionConfig.end(); it++) { + precision_config_vec.push_back(static_cast( + (mlir::cast(*it)).getValue())); + } + return builder_.CreateVector(precision_config_vec); +} + +BufferOffset> +Translator::BuildVhloPrecisionConfigV1( + mlir::vhlo::ArrayV1Attr precisionConfig) { + std::vector precision_config_vec; + auto values = precisionConfig.getValue(); + for (auto it = values.begin(); it != values.end(); it++) { + precision_config_vec.push_back(static_cast( + (mlir::cast(*it)).getValue())); + } + return builder_.CreateVector(precision_config_vec); +} + +std::optional> +Translator::BuildStablehloGatherOp(mlir::stablehlo::GatherOp gather_op, + const std::vector& operands, + const std::vector& results) { + std::string op_name = + gather_op.getOperation()->getName().getStringRef().str(); + uint32_t opcode_index = + GetOpcodeIndex(op_name, tflite::BuiltinOperator_STABLEHLO_GATHER); + + std::vector offset_dims_vec( + gather_op.getDimensionNumbers().getOffsetDims().begin(), + gather_op.getDimensionNumbers().getOffsetDims().end()); + std::vector collapsed_slice_dims_vec( + gather_op.getDimensionNumbers().getCollapsedSliceDims().begin(), + gather_op.getDimensionNumbers().getCollapsedSliceDims().end()); + std::vector start_index_map_vec( + gather_op.getDimensionNumbers().getStartIndexMap().begin(), + gather_op.getDimensionNumbers().getStartIndexMap().end()); + + auto offset_dims = builder_.CreateVector(offset_dims_vec); + auto collapsed_slice_dims = builder_.CreateVector(collapsed_slice_dims_vec); + auto start_index_map = builder_.CreateVector(start_index_map_vec); + auto slice_sizes = builder_.CreateVector( + mlir::GetOptionalVector(gather_op.getSliceSizes())); + + auto gather_option = tflite::CreateStablehloGatherOptions( + builder_, offset_dims, collapsed_slice_dims, start_index_map, + gather_op.getDimensionNumbers().getIndexVectorDim(), slice_sizes, + gather_op.getIndicesAreSorted()); + + return tflite::CreateOperator( + builder_, opcode_index, builder_.CreateVector(operands), + builder_.CreateVector(results), tflite::BuiltinOptions_NONE, + /*builtin_options=*/0, /*custom_options=*/0, + tflite::CustomOptionsFormat_FLEXBUFFERS, /*mutating_variable_inputs=*/0, + /*intermediates=*/0, /*large_custom_options_offset=*/0, + /*large_custom_options_size=*/0, + tflite::BuiltinOptions2_StablehloGatherOptions, gather_option.Union()); +} + +std::optional> +Translator::BuildVhloCompositeV1Op(mlir::vhlo::CompositeOpV1 composite_op, + const std::vector& operands, + const std::vector& results, + std::string op_name) { + uint32_t opcode_index = + GetOpcodeIndex(op_name, tflite::BuiltinOperator_STABLEHLO_COMPOSITE); + + int32_t api_version = + mlir::cast(composite_op.getVersion()) + .getValue() + .getSExtValue(); + + auto name = builder_.CreateString( + mlir::cast(composite_op.getName()) + .getValue() + .str()); + + auto composite_attributes = mlir::cast( + composite_op.getCompositeAttributes()); + auto flex_builder = std::make_unique(); + size_t map_start = flex_builder->StartMap(); + + for (auto namedAttr : composite_attributes.getValue()) { + auto name = + mlir::cast(namedAttr.first).getValue().str(); + auto attr = namedAttr.second; + + if (llvm::isa(attr)) + flex_builder->Bool(name.c_str(), + mlir::cast(attr).getValue()); + else if (llvm::isa(attr)) + flex_builder->String(name.c_str(), + mlir::cast(attr).getValue().str()); + else if (llvm::isa(attr)) + flex_builder->Bool( + name.c_str(), mlir::cast(attr).getValue()); + else if (llvm::isa(attr)) + flex_builder->String( + name.c_str(), + mlir::cast(attr).getValue().str()); + else if (llvm::isa(attr)) + flex_builder->Int(name.c_str(), + mlir::cast(attr) + .getValue() + .getSExtValue()); + else if (llvm::isa(attr)) + flex_builder->Float(name.c_str(), + mlir::cast(attr) + .getValue() + .convertToFloat()); + else if (llvm::isa(attr)) + CreateFlexbufferVector(flex_builder, name, attr); + else if (llvm::isa(attr)) { + // For TensorV1Attr, it's encoded as the following format: + // _TENSOR_V1_: { + // TENSOR_SHAPE: Vector, + // TENSOR_TYPE: tflite::TensorType (casted to i64), + // TENSOR_DATA: Vector or Vector + // } + auto attr_name = "_TENSOR_V1_" + name; + size_t attr_start = flex_builder->StartMap(attr_name.c_str()); + mlir::VhloToStablehloTypeConverter vhlo_type_converter; + auto tensor_v1_attr = mlir::cast(attr); + + auto dense = mlir::DenseIntOrFPElementsAttr::getFromRawBuffer( + mlir::cast( + vhlo_type_converter.convertType(tensor_v1_attr.getType())), + tensor_v1_attr.getData().getRawData()); + auto type = mlir::cast(dense.getType()); + tflite::TensorType tflite_element_type = + GetTFLiteType(type.getElementType()).value(); + auto shape = mlir::cast(vhlo_type_converter.convertType( + tensor_v1_attr.getType())) + .getShape(); + flex_builder->Vector("TENSOR_SHAPE", [&]() { + for (int d : shape) flex_builder->Int(d); + }); + flex_builder->Int("TENSOR_TYPE", tflite_element_type); + if (tflite_element_type == tflite::TensorType_INT32) { + auto elements = mlir::GetVector( + mlir::cast(attr), vhlo_type_converter); + flex_builder->Vector("TENSOR_DATA", [&]() { + for (int d : elements) flex_builder->Int(d); + }); + } else if (tflite_element_type == tflite::TensorType_FLOAT32) { + auto elements = mlir::GetVector( + mlir::cast(attr), vhlo_type_converter); + flex_builder->Vector("TENSOR_DATA", [&]() { + for (int d : elements) flex_builder->Float(d); + }); + } else if (tflite_element_type == tflite::TensorType_INT64) { + auto elements = mlir::GetVector( + mlir::cast(attr), vhlo_type_converter); + flex_builder->Vector("TENSOR_DATA", [&]() { + for (int d : elements) flex_builder->Int(d); + }); + } else { + // Unhandled data type. + return std::nullopt; + } + flex_builder->EndMap(attr_start); + } else { + // Unhandled attribute type. + return std::nullopt; + } + } + + flex_builder->EndMap(map_start); + flex_builder->Finish(); + + int32_t decomposition_subgraph_index = + subgraph_index_map_[mlir::cast( + composite_op.getDecomposition()) + .getValue() + .str()]; + + auto composite_option = tflite::CreateStableHLOCompositeOptions( + builder_, name, decomposition_subgraph_index, + builder_.CreateVector(flex_builder->GetBuffer()), + tflite::CustomOptionsFormat_FLEXBUFFERS, api_version); + + return tflite::CreateOperator( + builder_, opcode_index, builder_.CreateVector(operands), + builder_.CreateVector(results), tflite::BuiltinOptions_NONE, + /*builtin_options=*/0, /*custom_options=*/0, + tflite::CustomOptionsFormat_FLEXBUFFERS, + /*mutating_variable_inputs=*/0, /*intermediates=*/0, + /*large_custom_options_offset=*/0, /*large_custom_options_size=*/0, + tflite::BuiltinOptions2_StableHLOCompositeOptions, + composite_option.Union(), GetOperatorDebugMetadataIndex(composite_op)); +} + +std::optional> +Translator::BuildStablehloScatterOp(mlir::stablehlo::ScatterOp scatter_op, + const std::vector& operands, + const std::vector& results) { + std::string op_name = + scatter_op.getOperation()->getName().getStringRef().str(); + uint32_t opcode_index = + GetOpcodeIndex(op_name, tflite::BuiltinOperator_STABLEHLO_SCATTER); + + Region& body = scatter_op.getUpdateComputation(); + int32_t subgraph_index = + UnnamedRegionToSubgraph(&body, tflite::BuiltinOperator_STABLEHLO_SCATTER); + if (subgraph_index < 0) return std::nullopt; + + mlir::stablehlo::ScatterDimensionNumbersAttr scatter_dimension_numbers = + scatter_op.getScatterDimensionNumbers(); + llvm::ArrayRef update_window_dims_mlir = + scatter_dimension_numbers.getUpdateWindowDims(); + llvm::ArrayRef inserted_window_dims_mlir = + scatter_dimension_numbers.getInsertedWindowDims(); + llvm::ArrayRef scatter_dims_to_operand_dims_mlir = + scatter_dimension_numbers.getScatterDimsToOperandDims(); + + std::vector update_window_dims_vec(update_window_dims_mlir.begin(), + update_window_dims_mlir.end()); + std::vector inserted_window_dims_vec( + inserted_window_dims_mlir.begin(), inserted_window_dims_mlir.end()); + std::vector scatter_dims_to_operand_dims_vec( + scatter_dims_to_operand_dims_mlir.begin(), + scatter_dims_to_operand_dims_mlir.end()); + + int64_t index_vector_dim = scatter_dimension_numbers.getIndexVectorDim(); + bool unique_indices = scatter_op.getUniqueIndices(); + bool indices_are_sorted = scatter_op.getIndicesAreSorted(); + + auto update_window_dims = builder_.CreateVector(update_window_dims_vec); + auto inserted_window_dims = builder_.CreateVector(inserted_window_dims_vec); + auto scatter_dims_to_operand_dims = + builder_.CreateVector(scatter_dims_to_operand_dims_vec); + + auto options = tflite::CreateStablehloScatterOptions( + builder_, indices_are_sorted, update_window_dims, inserted_window_dims, + scatter_dims_to_operand_dims, index_vector_dim, unique_indices, + subgraph_index); + + return tflite::CreateOperator( + builder_, opcode_index, builder_.CreateVector(operands), + builder_.CreateVector(results), tflite::BuiltinOptions_NONE, + /*builtin_options=*/0, /*custom_options=*/0, + tflite::CustomOptionsFormat_FLEXBUFFERS, /*mutating_variable_inputs=*/0, + /*intermediates=*/0, /*large_custom_options_offset=*/0, + /*large_custom_options_size=*/0, + tflite::BuiltinOptions2_StablehloScatterOptions, options.Union()); +} + +std::optional> +Translator::BuildStablehloReduceWindowOp( + mlir::stablehlo::ReduceWindowOp reduce_window_op, + const std::vector& operands, const std::vector& results) { + std::string op_name = + reduce_window_op.getOperation()->getName().getStringRef().str(); + uint32_t opcode_index = + GetOpcodeIndex(op_name, tflite::BuiltinOperator_STABLEHLO_REDUCE_WINDOW); + + auto wd = reduce_window_op.getWindowDimensions(); + auto window_dimensions = + builder_.CreateVector(std::vector(wd.begin(), wd.end())); + auto window_strides = builder_.CreateVector( + mlir::GetOptionalVector(reduce_window_op.getWindowStrides())); + auto base_dilations = builder_.CreateVector( + mlir::GetOptionalVector(reduce_window_op.getBaseDilations())); + auto window_dilations = builder_.CreateVector( + mlir::GetOptionalVector(reduce_window_op.getWindowDilations())); + auto padding = builder_.CreateVector( + mlir::GetOptionalVector(reduce_window_op.getPadding())); + + auto& body = reduce_window_op.getBody(); + int32_t subgraph_index = UnnamedRegionToSubgraph( + &body, tflite::BuiltinOperator_STABLEHLO_REDUCE_WINDOW); + if (subgraph_index < 0) return std::nullopt; + + auto reduce_window_option = tflite::CreateStablehloReduceWindowOptions( + builder_, window_dimensions, window_strides, base_dilations, + window_dilations, padding, subgraph_index); + + return tflite::CreateOperator( + builder_, opcode_index, /*inputs=*/builder_.CreateVector(operands), + /*outputs=*/builder_.CreateVector(results), tflite::BuiltinOptions_NONE, + /*builtin_options=*/0, /*custom_options=*/0, + tflite::CustomOptionsFormat_FLEXBUFFERS, /*mutating_variable_inputs=*/0, + /*intermediates=*/0, /*large_custom_options_offset=*/0, + /*large_custom_options_size=*/0, + tflite::BuiltinOptions2_StablehloReduceWindowOptions, + reduce_window_option.Union()); +} + +std::optional> +Translator::BuildStablehloRngBitGeneratorOp( + mlir::stablehlo::RngBitGeneratorOp rng_op, + const std::vector& operands, const std::vector& results) { + std::string op_name = rng_op.getOperation()->getName().getStringRef().str(); + uint32_t opcode_index = GetOpcodeIndex( + op_name, tflite::BuiltinOperator_STABLEHLO_RNG_BIT_GENERATOR); + tflite::RngAlgorithm algorithm = tflite::RngAlgorithm_DEFAULT; + switch (rng_op.getRngAlgorithm()) { + case mlir::stablehlo::RngAlgorithm::THREE_FRY: + algorithm = tflite::RngAlgorithm_THREEFRY; + break; + case mlir::stablehlo::RngAlgorithm::PHILOX: + algorithm = tflite::RngAlgorithm_PHILOX; + break; + case mlir::stablehlo::RngAlgorithm::DEFAULT: + break; + } + auto rng_options = + tflite::CreateStablehloRngBitGeneratorOptions(builder_, algorithm); + return tflite::CreateOperator( + builder_, opcode_index, builder_.CreateVector(operands), + builder_.CreateVector(results), tflite::BuiltinOptions_NONE, + /*builtin_options=*/0, /*custom_options=*/0, + tflite::CustomOptionsFormat_FLEXBUFFERS, /*mutating_variable_inputs=*/0, + /*intermediates=*/0, /*large_custom_options_offset=*/0, + /*large_custom_options_size=*/0, + tflite::BuiltinOptions2_StablehloRngBitGeneratorOptions, + rng_options.Union()); +} + +std::optional> Translator::BuildStablehloPadOp( + mlir::stablehlo::PadOp pad_op, const std::vector& operands, + const std::vector& results) { + std::string op_name = pad_op->getName().getStringRef().str(); + uint32_t opcode_index = + GetOpcodeIndex(op_name, tflite::BuiltinOperator_STABLEHLO_PAD); + + auto edge_padding_low = + builder_.CreateVector(pad_op.getEdgePaddingLow().vec()); + auto edge_padding_high = + builder_.CreateVector(pad_op.getEdgePaddingHigh().vec()); + auto interior_padding = + builder_.CreateVector(pad_op.getInteriorPadding().vec()); + + auto pad_option = tflite::CreateStablehloPadOptions( + builder_, edge_padding_low, edge_padding_high, interior_padding); + + return tflite::CreateOperator( + builder_, opcode_index, builder_.CreateVector(operands), + builder_.CreateVector(results), tflite::BuiltinOptions_NONE, + /*builtin_options=*/0, /*custom_options=*/0, + tflite::CustomOptionsFormat_FLEXBUFFERS, /*mutating_variable_inputs=*/0, + /*intermediates=*/0, /*large_custom_options_offset=*/0, + /*large_custom_options_size=*/0, + tflite::BuiltinOptions2_StablehloPadOptions, pad_option.Union()); +} + +std::optional> Translator::BuildVhloGatherV1Op( + mlir::vhlo::GatherOpV1 gather_op, const std::vector& operands, + const std::vector& results, + mlir::VhloToStablehloTypeConverter& vhlo_type_converter) { + std::string op_name = + gather_op.getOperation()->getName().getStringRef().str(); + uint32_t opcode_index = + GetOpcodeIndex(op_name, tflite::BuiltinOperator_STABLEHLO_GATHER); + + auto offset_dims = builder_.CreateVector(mlir::GetVector( + mlir::cast(gather_op.getOffsetDims()), + vhlo_type_converter)); + auto collapsed_slice_dims = builder_.CreateVector(mlir::GetVector( + mlir::cast(gather_op.getCollapsedSliceDims()), + vhlo_type_converter)); + auto start_index_map = builder_.CreateVector(mlir::GetVector( + mlir::cast(gather_op.getStartIndexMap()), + vhlo_type_converter)); + auto slice_sizes = builder_.CreateVector(mlir::GetVector( + mlir::cast(gather_op.getSliceSizes()), + vhlo_type_converter)); + + auto gather_option = tflite::CreateStablehloGatherOptions( + builder_, offset_dims, collapsed_slice_dims, start_index_map, + mlir::cast(gather_op.getIndexVectorDim()) + .getValue() + .getSExtValue(), + slice_sizes, + mlir::cast(gather_op.getIndicesAreSorted()) + .getValue()); + + return tflite::CreateOperator( + builder_, opcode_index, builder_.CreateVector(operands), + builder_.CreateVector(results), tflite::BuiltinOptions_NONE, + /*builtin_options=*/0, /*custom_options=*/0, + tflite::CustomOptionsFormat_FLEXBUFFERS, /*mutating_variable_inputs=*/0, + /*intermediates=*/0, /*large_custom_options_offset=*/0, + /*large_custom_options_size=*/0, + tflite::BuiltinOptions2_StablehloGatherOptions, gather_option.Union(), + /*debug_metadata_index=*/ + GetOperatorDebugMetadataIndex(gather_op.getOperation())); +} + +std::optional> Translator::BuildVhloScatterV1Op( + mlir::vhlo::ScatterOpV1 scatter_op, const std::vector& operands, + const std::vector& results, + mlir::VhloToStablehloTypeConverter& vhlo_type_converter) { + std::string op_name = + scatter_op.getOperation()->getName().getStringRef().str(); + uint32_t opcode_index = + GetOpcodeIndex(op_name, tflite::BuiltinOperator_STABLEHLO_SCATTER); + + Region& body = scatter_op.getUpdateComputation(); + int32_t subgraph_index = + UnnamedRegionToSubgraph(&body, tflite::BuiltinOperator_STABLEHLO_SCATTER); + if (subgraph_index < 0) return std::nullopt; + + int64_t index_vector_dim = + mlir::cast(scatter_op.getIndexVectorDim()) + .getValue() + .getSExtValue(); + bool unique_indices = + mlir::cast(scatter_op.getUniqueIndices()) + .getValue(); + bool indices_are_sorted = + mlir::cast(scatter_op.getIndicesAreSorted()) + .getValue(); + + auto update_window_dims = builder_.CreateVector(mlir::GetVector( + mlir::cast(scatter_op.getUpdateWindowDims()), + vhlo_type_converter)); + auto inserted_window_dims = builder_.CreateVector(mlir::GetVector( + mlir::cast(scatter_op.getInsertedWindowDims()), + vhlo_type_converter)); + auto scatter_dims_to_operand_dims = builder_.CreateVector( + mlir::GetVector(mlir::cast( + scatter_op.getScatterDimsToOperandDims()), + vhlo_type_converter)); + + auto options = tflite::CreateStablehloScatterOptions( + builder_, indices_are_sorted, update_window_dims, inserted_window_dims, + scatter_dims_to_operand_dims, index_vector_dim, unique_indices, + subgraph_index); + + return tflite::CreateOperator( + builder_, opcode_index, builder_.CreateVector(operands), + builder_.CreateVector(results), tflite::BuiltinOptions_NONE, + /*builtin_options=*/0, /*custom_options=*/0, + tflite::CustomOptionsFormat_FLEXBUFFERS, /*mutating_variable_inputs=*/0, + /*intermediates=*/0, /*large_custom_options_offset=*/0, + /*large_custom_options_size=*/0, + tflite::BuiltinOptions2_StablehloScatterOptions, options.Union(), + /*debug_metadata_index=*/ + GetOperatorDebugMetadataIndex(scatter_op.getOperation())); +} + +std::optional> +Translator::BuildVhloReduceWindowV1Op( + mlir::vhlo::ReduceWindowOpV1 reduce_window_op, + const std::vector& operands, const std::vector& results, + mlir::VhloToStablehloTypeConverter& vhlo_type_converter) { + std::string op_name = + reduce_window_op.getOperation()->getName().getStringRef().str(); + uint32_t opcode_index = + GetOpcodeIndex(op_name, tflite::BuiltinOperator_STABLEHLO_REDUCE_WINDOW); + + auto window_dimensions = builder_.CreateVector( + mlir::GetVector(mlir::cast( + reduce_window_op.getWindowDimensions()), + vhlo_type_converter)); + auto window_strides = builder_.CreateVector(mlir::GetVector( + mlir::cast(reduce_window_op.getWindowStrides()), + vhlo_type_converter)); + auto base_dilations = builder_.CreateVector(mlir::GetVector( + mlir::cast(reduce_window_op.getBaseDilations()), + vhlo_type_converter)); + auto window_dilations = builder_.CreateVector( + mlir::GetVector(mlir::cast( + reduce_window_op.getWindowDilations()), + vhlo_type_converter)); + auto padding = builder_.CreateVector(mlir::GetVector( + mlir::cast(reduce_window_op.getPadding()), + vhlo_type_converter)); + auto& body = reduce_window_op.getBody(); + int32_t subgraph_index = UnnamedRegionToSubgraph( + &body, tflite::BuiltinOperator_STABLEHLO_REDUCE_WINDOW); + if (subgraph_index < 0) return std::nullopt; + + auto reduce_window_option = tflite::CreateStablehloReduceWindowOptions( + builder_, window_dimensions, window_strides, base_dilations, + window_dilations, padding, subgraph_index); + + return tflite::CreateOperator( + builder_, opcode_index, /*inputs=*/builder_.CreateVector(operands), + /*outputs=*/builder_.CreateVector(results), tflite::BuiltinOptions_NONE, + /*builtin_options=*/0, /*custom_options=*/0, + tflite::CustomOptionsFormat_FLEXBUFFERS, /*mutating_variable_inputs=*/0, + /*intermediates=*/0, /*large_custom_options_offset=*/0, + /*large_custom_options_size=*/0, + tflite::BuiltinOptions2_StablehloReduceWindowOptions, + reduce_window_option.Union(), + GetOperatorDebugMetadataIndex(reduce_window_op.getOperation())); +} + +std::optional> +Translator::BuildVhloRngBitGeneratorV1Op( + mlir::vhlo::RngBitGeneratorOpV1 rng_op, + const std::vector& operands, const std::vector& results, + mlir::VhloToStablehloTypeConverter& vhlo_type_converter) { + std::string op_name = rng_op.getOperation()->getName().getStringRef().str(); + uint32_t opcode_index = GetOpcodeIndex( + op_name, tflite::BuiltinOperator_STABLEHLO_RNG_BIT_GENERATOR); + tflite::RngAlgorithm algorithm = tflite::RngAlgorithm_DEFAULT; + switch (mlir::cast(rng_op.getRngAlgorithm()) + .getValue()) { + case mlir::vhlo::RngAlgorithmV1::THREE_FRY: + algorithm = tflite::RngAlgorithm_THREEFRY; + break; + case mlir::vhlo::RngAlgorithmV1::PHILOX: + algorithm = tflite::RngAlgorithm_PHILOX; + break; + case mlir::vhlo::RngAlgorithmV1::DEFAULT: + break; + } + auto rng_options = + tflite::CreateStablehloRngBitGeneratorOptions(builder_, algorithm); + return tflite::CreateOperator( + builder_, opcode_index, builder_.CreateVector(operands), + builder_.CreateVector(results), tflite::BuiltinOptions_NONE, + /*builtin_options=*/0, /*custom_options=*/0, + tflite::CustomOptionsFormat_FLEXBUFFERS, /*mutating_variable_inputs=*/0, + /*intermediates=*/0, /*large_custom_options_offset=*/0, + /*large_custom_options_size=*/0, + tflite::BuiltinOptions2_StablehloRngBitGeneratorOptions, + rng_options.Union(), /*debug_metadata_index=*/ + GetOperatorDebugMetadataIndex(rng_op.getOperation())); +} + +std::optional> Translator::BuildVhloPadV1Op( + mlir::vhlo::PadOpV1 pad_op, const std::vector& operands, + const std::vector& results, + mlir::VhloToStablehloTypeConverter& vhlo_type_converter) { + std::string op_name = pad_op->getName().getStringRef().str(); + uint32_t opcode_index = + GetOpcodeIndex(op_name, tflite::BuiltinOperator_STABLEHLO_PAD); + + auto edge_padding_low = builder_.CreateVector(mlir::GetVector( + mlir::cast(pad_op.getEdgePaddingLow()), + vhlo_type_converter)); + auto edge_padding_high = builder_.CreateVector(mlir::GetVector( + mlir::cast(pad_op.getEdgePaddingHigh()), + vhlo_type_converter)); + auto interior_padding = builder_.CreateVector(mlir::GetVector( + mlir::cast(pad_op.getInteriorPadding()), + vhlo_type_converter)); + + auto pad_option = tflite::CreateStablehloPadOptions( + builder_, edge_padding_low, edge_padding_high, interior_padding); + + return tflite::CreateOperator( + builder_, opcode_index, builder_.CreateVector(operands), + builder_.CreateVector(results), tflite::BuiltinOptions_NONE, + /*builtin_options=*/0, /*custom_options=*/0, + tflite::CustomOptionsFormat_FLEXBUFFERS, /*mutating_variable_inputs=*/0, + /*intermediates=*/0, /*large_custom_options_offset=*/0, + /*large_custom_options_size=*/0, + tflite::BuiltinOptions2_StablehloPadOptions, pad_option.Union(), + GetOperatorDebugMetadataIndex(pad_op.getOperation())); +} + +std::optional> Translator::BuildVhloCaseOp( + mlir::vhlo::CaseOpV1 op, const std::vector& operands, + const std::vector& results, + mlir::VhloToStablehloTypeConverter& vhlo_type_converter) { + const uint32_t opcode_ind = + GetOpcodeIndex(op->getName().getStringRef().str(), + tflite::BuiltinOperator_STABLEHLO_CASE); + + // Figure out the "functional form" of the case op by isolating the regions. + // This has to be done on the fly during export time because a "functional" + // case op is not valid mlir, (but required for flatbuffer control-flow). + // NOTE: This won't round-trip without custom import logic. + + // Update the regions to be self-contained all with matching signature. + + mlir::OpBuilder b(op->getContext()); + + auto iso_result = mlir::TFL::IsolateRegions(op.getOperation(), b); + if (!iso_result.has_value()) { + op->emitError() + << "Failed to isolate stablehlo.case_op branches during export.\n"; + return std::nullopt; + } + + // Make flatbuffer subgraphs from regions. + + std::vector subgraph_inds; + for (auto& region : op.getBranches()) { + const int32_t subgraph_ind = UnnamedRegionToSubgraph( + ®ion, tflite::BuiltinOperator_STABLEHLO_CASE); + if (subgraph_ind < 0) { + op->emitError() << "Failed to serialize stablehlo.case_op branch\n"; + return std::nullopt; + } + subgraph_inds.push_back(subgraph_ind); + } + + auto opts = tflite::CreateStablehloCaseOptions( + builder_, builder_.CreateVector(subgraph_inds)); + + // Compute what the signature of case op would be if it is was in "functional + // form". This is index arg concatted with the new region(s) signature. + + auto parent_func = op->getParentOfType(); + const auto parent_subgraph_ind = + subgraph_index_map_[parent_func.getSymName().str()]; + + std::vector new_operands(operands); + for (auto val : *iso_result) { + const auto val_name = name_mapper_.GetUniqueName(val); + const auto val_tensor_id = tensor_index_map_[parent_subgraph_ind][val_name]; + new_operands.push_back(val_tensor_id); + } + + return tflite::CreateOperator( + builder_, opcode_ind, /*inputs=*/builder_.CreateVector(new_operands), + /*outputs=*/builder_.CreateVector(results), tflite::BuiltinOptions_NONE, + /*builtin_options=*/0, /*custom_options=*/0, + tflite::CustomOptionsFormat_FLEXBUFFERS, /*mutating_variable_inputs=*/0, + /*intermediates=*/0, /*large_custom_options_offset=*/0, + /*large_custom_options_size=*/0, + tflite::BuiltinOptions2_StablehloCaseOptions, opts.Union(), + GetOperatorDebugMetadataIndex(op.getOperation())); +} + +std::optional> Translator::BuildOperator( + Operation* inst, std::vector operands, + const std::vector& results, + const std::vector& intermediates) { + const auto* dialect = inst->getDialect(); + if (!dialect) { + inst->emitOpError("dialect is not registered"); + return std::nullopt; + } + + // If TFLite built in op, create operator as a builtin op. + if (dialect == tfl_dialect_) { + // Only if built-in TFLite op emission is enabled, would legalization have + // converted any TF->TFL. + if (!enabled_op_types_.contains(OpType::kTfliteBuiltin)) { + return inst->emitOpError( + "is a TFLite builtin op but builtin emission is not " + "enabled"), + std::nullopt; + } + + auto builtin_code = GetBuiltinOpCode(inst); + if (!builtin_code) { + if (auto verify_op = dyn_cast(inst)) { + return BuildNumericVerifyOperator(verify_op, operands, results); + } + if (auto custom_op = dyn_cast(inst)) { + return BuildCustomOperator(inst, custom_op, operands, results); + } + if (auto whileOp = dyn_cast(inst)) { + if (inst->getNumOperands() != inst->getNumResults()) { + inst->emitOpError( + "number of operands and results don't match, only canonical " + "TFL While supported"); + return std::nullopt; + } + return BuildWhileOperator(whileOp, operands, results); + } + if (auto ifOp = dyn_cast(inst)) { + return BuildIfOperator(ifOp, operands, results); + } + + inst->emitOpError("is not a supported TFLite op"); + return std::nullopt; + } + + if (*builtin_code == tflite::BuiltinOperator_CALL_ONCE) { + if (auto initOp = dyn_cast(inst)) { + return BuildCallOnceOperator(initOp, operands, results); + } + } + + std::string op_name = inst->getName().getStringRef().str(); + uint32_t opcode_index = GetOpcodeIndex(op_name, *builtin_code); + + // If this is TransposeConv we need to do a special case of ignoring the + // optional tensor, to allow newly created models to run on old runtimes. + if (*builtin_code == tflite::BuiltinOperator_TRANSPOSE_CONV) { + if (operands.size() == 4 && operands.at(3) == -1) { + operands.pop_back(); + } + } + + auto offset = CreateFlatBufferOperator(inst, opcode_index, operands, + results, intermediates, &builder_, + GetOperatorDebugMetadataIndex(inst)); + if (!offset) { + inst->emitOpError("is not a supported TFLite op"); + } + return offset; + } + + if (dialect == vhlo_dialect_) { + mlir::VhloToStablehloTypeConverter vhlo_type_converter; + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildVhloCaseOp(vhlo_op, operands, results, vhlo_type_converter); + } + + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildVhloScatterV1Op(vhlo_op, operands, results, + vhlo_type_converter); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildVhloRngBitGeneratorV1Op(vhlo_op, operands, results, + vhlo_type_converter); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildVhloGatherV1Op(vhlo_op, operands, results, + vhlo_type_converter); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildStablehloOperatorwithoutOptions( + inst, operands, results, tflite::BuiltinOperator_STABLEHLO_ADD); + } + if (auto sub_op = llvm::dyn_cast(inst)) { + return BuildStablehloOperatorwithoutOptions( + inst, operands, results, tflite::BuiltinOperator_STABLEHLO_SUBTRACT); + } + if (auto or_op = llvm::dyn_cast(inst)) { + return BuildStablehloOperatorwithoutOptions( + inst, operands, results, tflite::BuiltinOperator_STABLEHLO_OR); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildStablehloOperatorwithoutOptions( + inst, operands, results, tflite::BuiltinOperator_STABLEHLO_MULTIPLY); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildVhloReduceWindowV1Op(vhlo_op, operands, results, + vhlo_type_converter); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildStablehloOperatorwithoutOptions( + inst, operands, results, tflite::BuiltinOperator_STABLEHLO_MAXIMUM); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildStablehloOperatorwithoutOptions( + inst, operands, results, tflite::BuiltinOperator_STABLEHLO_MINIMUM); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildVhloPadV1Op(vhlo_op, operands, results, vhlo_type_converter); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildStablehloOperatorwithoutOptions( + inst, operands, results, + tflite::BuiltinOperator_STABLEHLO_SHIFT_LEFT); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildStablehloOperatorwithoutOptions( + inst, operands, results, tflite::BuiltinOperator_STABLEHLO_AND); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + auto op = BuildVhloCompositeV1Op(vhlo_op, operands, results, + inst->getName().getStringRef().str()); + if (!op) + return inst->emitOpError("Failed to build VhloCompositeOpV1."), + std::nullopt; + return op; + } + // for ops don't have kernels, only serialize when conversion is set to + // true + if (convert_stablehlo_) { + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildStablehloOperatorwithoutOptions( + inst, operands, results, + tflite::BuiltinOperator_STABLEHLO_LOGISTIC); + } + + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildStablehloOperatorwithoutOptions( + inst, operands, results, tflite::BuiltinOperator_STABLEHLO_DIVIDE); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildStablehloOperatorwithoutOptions( + inst, operands, results, tflite::BuiltinOperator_STABLEHLO_MAXIMUM); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildStablehloOperatorwithoutOptions( + inst, operands, results, tflite::BuiltinOperator_STABLEHLO_RESHAPE); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildStablehloOperatorwithoutOptions( + inst, operands, results, tflite::BuiltinOperator_STABLEHLO_CLAMP); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildStablehloOperatorwithoutOptions( + inst, operands, results, tflite::BuiltinOperator_STABLEHLO_ABS); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildStablehloOperatorwithoutOptions( + inst, operands, results, tflite::BuiltinOperator_STABLEHLO_ADD); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildStablehloOperatorwithoutOptions( + inst, operands, results, tflite::BuiltinOperator_STABLEHLO_COSINE); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildStablehloOperatorwithoutOptions( + inst, operands, results, + tflite::BuiltinOperator_STABLEHLO_EXPONENTIAL); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildStablehloOperatorwithoutOptions( + inst, operands, results, tflite::BuiltinOperator_STABLEHLO_FLOOR); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildStablehloOperatorwithoutOptions( + inst, operands, results, tflite::BuiltinOperator_STABLEHLO_LOG); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildStablehloOperatorwithoutOptions( + inst, operands, results, tflite::BuiltinOperator_STABLEHLO_MINIMUM); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildStablehloOperatorwithoutOptions( + inst, operands, results, tflite::BuiltinOperator_STABLEHLO_NEGATE); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildStablehloOperatorwithoutOptions( + inst, operands, results, tflite::BuiltinOperator_STABLEHLO_OR); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildStablehloOperatorwithoutOptions( + inst, operands, results, tflite::BuiltinOperator_STABLEHLO_POWER); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildStablehloOperatorwithoutOptions( + inst, operands, results, + tflite::BuiltinOperator_STABLEHLO_REMAINDER); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildStablehloOperatorwithoutOptions( + inst, operands, results, tflite::BuiltinOperator_STABLEHLO_RSQRT); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildStablehloOperatorwithoutOptions( + inst, operands, results, tflite::BuiltinOperator_STABLEHLO_SELECT); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildStablehloOperatorwithoutOptions( + inst, operands, results, + tflite::BuiltinOperator_STABLEHLO_SUBTRACT); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildStablehloOperatorwithoutOptions( + inst, operands, results, tflite::BuiltinOperator_STABLEHLO_TANH); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildStablehloOperatorwithoutOptions( + inst, operands, results, tflite::BuiltinOperator_STABLEHLO_CONVERT); + } + if (auto vhlo_op = + llvm::dyn_cast(inst)) { + return BuildStablehloOperatorwithoutOptions( + inst, operands, results, + tflite::BuiltinOperator_STABLEHLO_DYNAMIC_UPDATE_SLICE); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + std::string op_name = inst->getName().getStringRef().str(); + uint32_t opcode_index = + GetOpcodeIndex(op_name, tflite::BuiltinOperator_STABLEHLO_IOTA); + + auto iota_option = tflite::CreateStablehloIotaOptions( + builder_, + mlir::cast(vhlo_op.getIotaDimension()) + .getValue() + .getSExtValue()); + + return tflite::CreateOperator( + builder_, opcode_index, builder_.CreateVector(operands), + builder_.CreateVector(results), tflite::BuiltinOptions_NONE, 0, 0, + tflite::CustomOptionsFormat_FLEXBUFFERS, 0, 0, 0, 0, + tflite::BuiltinOptions2_StablehloIotaOptions, iota_option.Union(), + GetOperatorDebugMetadataIndex(inst)); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + std::string op_name = inst->getName().getStringRef().str(); + uint32_t opcode_index = GetOpcodeIndex( + op_name, tflite::BuiltinOperator_STABLEHLO_DYNAMIC_SLICE); + + auto slice_sizes = builder_.CreateVector(mlir::GetVector( + mlir::cast(vhlo_op.getSliceSizes()), + vhlo_type_converter)); + + auto dynamic_slice_option = + tflite::CreateStablehloDynamicSliceOptions(builder_, slice_sizes); + + return tflite::CreateOperator( + builder_, opcode_index, builder_.CreateVector(operands), + builder_.CreateVector(results), tflite::BuiltinOptions_NONE, 0, 0, + tflite::CustomOptionsFormat_FLEXBUFFERS, 0, 0, 0, 0, + tflite::BuiltinOptions2_StablehloDynamicSliceOptions, + dynamic_slice_option.Union(), GetOperatorDebugMetadataIndex(inst)); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + std::string op_name = inst->getName().getStringRef().str(); + uint32_t opcode_index = + GetOpcodeIndex(op_name, tflite::BuiltinOperator_STABLEHLO_COMPARE); + + auto compare_type_attr = vhlo_op.getCompareType(); + tflite::StablehloComparisonType compare_type = + tflite::StablehloComparisonType_STABLEHLO_COMPARISON_TYPE_NOTYPE; + if (compare_type_attr) + compare_type = static_cast( + mlir::cast(compare_type_attr) + .getValue()); + auto compare_option = tflite::CreateStablehloCompareOptions( + builder_, + static_cast( + mlir::cast( + vhlo_op.getComparisonDirection()) + .getValue()), + compare_type); + + return tflite::CreateOperator( + builder_, opcode_index, builder_.CreateVector(operands), + builder_.CreateVector(results), tflite::BuiltinOptions_NONE, 0, 0, + tflite::CustomOptionsFormat_FLEXBUFFERS, 0, 0, 0, 0, + tflite::BuiltinOptions2_StablehloCompareOptions, + compare_option.Union(), GetOperatorDebugMetadataIndex(inst)); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + std::string op_name = inst->getName().getStringRef().str(); + uint32_t opcode_index = GetOpcodeIndex( + op_name, tflite::BuiltinOperator_STABLEHLO_CONCATENATE); + + auto concat_option = tflite::CreateStablehloConcatenateOptions( + builder_, + mlir::cast(vhlo_op.getDimension()) + .getValue() + .getSExtValue()); + + return tflite::CreateOperator( + builder_, opcode_index, builder_.CreateVector(operands), + builder_.CreateVector(results), tflite::BuiltinOptions_NONE, 0, 0, + tflite::CustomOptionsFormat_FLEXBUFFERS, 0, 0, 0, 0, + tflite::BuiltinOptions2_StablehloConcatenateOptions, + concat_option.Union(), GetOperatorDebugMetadataIndex(inst)); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + std::string op_name = inst->getName().getStringRef().str(); + uint32_t opcode_index = + GetOpcodeIndex(op_name, tflite::BuiltinOperator_STABLEHLO_SLICE); + + auto start_indices = builder_.CreateVector((mlir::GetVector( + mlir::cast(vhlo_op.getStartIndicesAttr()), + vhlo_type_converter))); + auto limit_indices = builder_.CreateVector(mlir::GetVector( + mlir::cast(vhlo_op.getLimitIndicesAttr()), + vhlo_type_converter)); + auto strides = builder_.CreateVector(mlir::GetVector( + mlir::cast(vhlo_op.getStridesAttr()), + vhlo_type_converter)); + + auto slice_option = tflite::CreateStablehloSliceOptions( + builder_, start_indices, limit_indices, strides); + + return tflite::CreateOperator( + builder_, opcode_index, builder_.CreateVector(operands), + builder_.CreateVector(results), tflite::BuiltinOptions_NONE, 0, 0, + tflite::CustomOptionsFormat_FLEXBUFFERS, 0, 0, 0, 0, + tflite::BuiltinOptions2_StablehloSliceOptions, slice_option.Union(), + GetOperatorDebugMetadataIndex(inst)); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + std::string op_name = inst->getName().getStringRef().str(); + uint32_t opcode_index = GetOpcodeIndex( + op_name, tflite::BuiltinOperator_STABLEHLO_CONVOLUTION); + + auto window_strides = builder_.CreateVector(mlir::GetVector( + mlir::cast(vhlo_op.getWindowStrides()), + vhlo_type_converter)); + auto padding = builder_.CreateVector(mlir::GetVector( + mlir::cast(vhlo_op.getPadding()), + vhlo_type_converter)); + auto lhs_dialation = builder_.CreateVector(mlir::GetVector( + mlir::cast(vhlo_op.getLhsDilation()), + vhlo_type_converter)); + auto rhs_dialation = builder_.CreateVector(mlir::GetVector( + mlir::cast(vhlo_op.getRhsDilation()), + vhlo_type_converter)); + auto window_reversal = builder_.CreateVector(mlir::GetVector( + mlir::cast(vhlo_op.getWindowReversal()), + vhlo_type_converter)); + auto input_batch_dimension = mlir::cast( + vhlo_op.getInputBatchDimension()) + .getValue() + .getSExtValue(); + auto input_feature_dimension = mlir::cast( + vhlo_op.getInputFeatureDimension()) + .getValue() + .getSExtValue(); + auto kernel_input_feature_dimension = + mlir::cast( + vhlo_op.getKernelInputFeatureDimension()) + .getValue() + .getSExtValue(); + auto kernel_output_feature_dimension = + mlir::cast( + vhlo_op.getKernelOutputFeatureDimension()) + .getValue() + .getSExtValue(); + auto output_batch_dimension = mlir::cast( + vhlo_op.getOutputBatchDimension()) + .getValue() + .getSExtValue(); + auto output_feature_dimension = mlir::cast( + vhlo_op.getOutputFeatureDimension()) + .getValue() + .getSExtValue(); + + auto kernel_spatial_dimensions = builder_.CreateVector( + mlir::GetVector(mlir::cast( + vhlo_op.getKernelSpatialDimensions()), + vhlo_type_converter)); + auto output_spatial_dimension = builder_.CreateVector( + mlir::GetVector(mlir::cast( + vhlo_op.getOutputSpatialDimensions()), + vhlo_type_converter)); + auto input_spatial_dimension = builder_.CreateVector( + mlir::GetVector(mlir::cast( + vhlo_op.getInputSpatialDimensions()), + vhlo_type_converter)); + BufferOffset> precision_config = 0; + if (vhlo_op.getPrecisionConfig()) { + precision_config = BuildVhloPrecisionConfigV1( + mlir::dyn_cast( + vhlo_op.getPrecisionConfig())); + } + + auto convolution_option = tflite::CreateStablehloConvolutionOptions( + builder_, window_strides, padding, lhs_dialation, rhs_dialation, + window_reversal, input_batch_dimension, input_feature_dimension, + input_spatial_dimension, kernel_input_feature_dimension, + kernel_output_feature_dimension, kernel_spatial_dimensions, + output_batch_dimension, output_feature_dimension, + output_spatial_dimension, + mlir::cast( + vhlo_op.getFeatureGroupCount()) + .getValue() + .getSExtValue(), + mlir::cast(vhlo_op.getBatchGroupCount()) + .getValue() + .getSExtValue(), + precision_config); + + return tflite::CreateOperator( + builder_, opcode_index, builder_.CreateVector(operands), + builder_.CreateVector(results), tflite::BuiltinOptions_NONE, 0, 0, + tflite::CustomOptionsFormat_FLEXBUFFERS, 0, 0, 0, 0, + tflite::BuiltinOptions2_StablehloConvolutionOptions, + convolution_option.Union(), GetOperatorDebugMetadataIndex(inst)); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + std::string op_name = inst->getName().getStringRef().str(); + uint32_t opcode_index = GetOpcodeIndex( + op_name, tflite::BuiltinOperator_STABLEHLO_BROADCAST_IN_DIM); + + auto broadcast_dimensions = builder_.CreateVector( + mlir::GetVector(mlir::cast( + vhlo_op.getBroadcastDimensions()), + vhlo_type_converter)); + + auto broadcast_option = tflite::CreateStablehloBroadcastInDimOptions( + builder_, broadcast_dimensions); + + return tflite::CreateOperator( + builder_, opcode_index, builder_.CreateVector(operands), + builder_.CreateVector(results), tflite::BuiltinOptions_NONE, 0, 0, + tflite::CustomOptionsFormat_FLEXBUFFERS, 0, 0, 0, 0, + tflite::BuiltinOptions2_StablehloBroadcastInDimOptions, + broadcast_option.Union(), GetOperatorDebugMetadataIndex(inst)); + } + // TODO(b/327690610): fix custom_call definition + if (auto vhlo_op = llvm::dyn_cast(inst)) { + std::string op_name = inst->getName().getStringRef().str(); + uint32_t opcode_index = GetOpcodeIndex( + op_name, tflite::BuiltinOperator_STABLEHLO_CUSTOM_CALL); + auto op_api_version = + mlir::cast( + vhlo_op.getApiVersion()) + .getValue(); + int32_t api_version = 0; + if (op_api_version == + mlir::vhlo::CustomCallApiVersionV1::API_VERSION_UNSPECIFIED) + api_version = 0; + if (op_api_version == + mlir::vhlo::CustomCallApiVersionV1::API_VERSION_ORIGINAL) + api_version = 1; + if (op_api_version == + mlir::vhlo::CustomCallApiVersionV1::API_VERSION_STATUS_RETURNING) + api_version = 2; + if (op_api_version == mlir::vhlo::CustomCallApiVersionV1:: + API_VERSION_STATUS_RETURNING_UNIFIED) + api_version = 3; + + auto call_target_name = builder_.CreateString( + mlir::cast(vhlo_op.getCallTargetName()) + .getValue() + .str()); + auto backend_config = builder_.CreateString( + mlir::cast(vhlo_op.getBackendConfig()) + .getValue() + .str()); + // building the computation info + auto flex_builder = std::make_unique(); + size_t map_start = flex_builder->StartMap(); + auto attrs = vhlo_op->getAttrs(); + std::vector extra_attributes; + + for (size_t i = 0; i < attrs.size(); ++i) { + auto name = attrs[i].getName().str(); + auto attr = attrs[i].getValue(); + if (name == "call_target_name" || name == "backend_config") continue; + if (llvm::isa(attr)) + flex_builder->Bool(name.c_str(), + mlir::cast(attr).getValue()); + if (llvm::isa(attr)) + flex_builder->String( + name.c_str(), + mlir::cast(attr).getValue().str()); + if (llvm::isa(attr)) + flex_builder->Bool( + name.c_str(), + mlir::cast(attr).getValue()); + if (llvm::isa(attr)) + flex_builder->String( + name.c_str(), + mlir::cast(attr).getValue().str()); + } + flex_builder->EndMap(map_start); + flex_builder->Finish(); + auto custom_call_option = tflite::CreateStablehloCustomCallOptions( + builder_, call_target_name, + mlir::cast<::mlir::vhlo::BooleanV1Attr>(vhlo_op.getHasSideEffect()) + .getValue(), + backend_config, api_version, 0, + builder_.CreateVector(flex_builder->GetBuffer())); + + return tflite::CreateOperator( + builder_, opcode_index, builder_.CreateVector(operands), + builder_.CreateVector(results), tflite::BuiltinOptions_NONE, 0, 0, + tflite::CustomOptionsFormat_FLEXBUFFERS, 0, 0, 0, 0, + tflite::BuiltinOptions2_StablehloCustomCallOptions, + custom_call_option.Union(), GetOperatorDebugMetadataIndex(inst)); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + std::string op_name = inst->getName().getStringRef().str(); + uint32_t opcode_index = + GetOpcodeIndex(op_name, tflite::BuiltinOperator_STABLEHLO_REDUCE); + + auto dimension = builder_.CreateVector(mlir::GetVector( + mlir::cast(vhlo_op.getDimensions()), + vhlo_type_converter)); + auto& body = vhlo_op.getBody(); + int32_t subgraph_index = UnnamedRegionToSubgraph( + &body, tflite::BuiltinOperator_STABLEHLO_REDUCE); + if (subgraph_index < 0) return std::nullopt; + + auto reduce_option = tflite::CreateStablehloReduceOptions( + builder_, dimension, subgraph_index); + + return tflite::CreateOperator( + builder_, opcode_index, builder_.CreateVector(operands), + builder_.CreateVector(results), tflite::BuiltinOptions_NONE, 0, 0, + tflite::CustomOptionsFormat_FLEXBUFFERS, 0, 0, 0, 0, + tflite::BuiltinOptions2_StablehloReduceOptions, + reduce_option.Union(), GetOperatorDebugMetadataIndex(inst)); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + std::string op_name = inst->getName().getStringRef().str(); + uint32_t opcode_index = GetOpcodeIndex( + op_name, tflite::BuiltinOperator_STABLEHLO_DOT_GENERAL); + + auto lhs_batching_dimensions = builder_.CreateVector( + mlir::GetVector(mlir::cast( + vhlo_op.getLhsBatchingDimensions()), + vhlo_type_converter)); + auto rhs_batching_dimensions = builder_.CreateVector( + mlir::GetVector(mlir::cast( + vhlo_op.getRhsBatchingDimensions()), + vhlo_type_converter)); + auto lhs_contracting_dimensions = builder_.CreateVector( + mlir::GetVector(mlir::cast( + vhlo_op.getLhsContractingDimensions()), + vhlo_type_converter)); + auto rhs_contracting_dimensions = builder_.CreateVector( + mlir::GetVector(mlir::cast( + vhlo_op.getRhsContractingDimensions()), + vhlo_type_converter)); + + BufferOffset> precision_config = 0; + if (vhlo_op.getPrecisionConfig()) { + precision_config = + BuildVhloPrecisionConfigV1(mlir::cast( + vhlo_op.getPrecisionConfig())); + } + + auto dot_geneoral_option = tflite::CreateStablehloDotGeneralOptions( + builder_, lhs_batching_dimensions, rhs_batching_dimensions, + lhs_contracting_dimensions, rhs_contracting_dimensions, + precision_config); + + return tflite::CreateOperator( + builder_, opcode_index, builder_.CreateVector(operands), + builder_.CreateVector(results), tflite::BuiltinOptions_NONE, 0, 0, + tflite::CustomOptionsFormat_FLEXBUFFERS, 0, 0, 0, 0, + tflite::BuiltinOptions2_StablehloDotGeneralOptions, + dot_geneoral_option.Union(), GetOperatorDebugMetadataIndex(inst)); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + std::string op_name = inst->getName().getStringRef().str(); + uint32_t opcode_index = + GetOpcodeIndex(op_name, tflite::BuiltinOperator_STABLEHLO_SORT); + auto& comparator = vhlo_op.getComparator(); + int32_t comparator_subgraph_index = UnnamedRegionToSubgraph( + &comparator, tflite::BuiltinOperator_STABLEHLO_SORT); + if (comparator_subgraph_index < 0) return std::nullopt; + + auto sort_option = tflite::CreateStablehloSortOptions( + builder_, + mlir::cast(vhlo_op.getDimension()) + .getValue() + .getSExtValue(), + mlir::cast(vhlo_op.getIsStable()) + .getValue(), + comparator_subgraph_index); + + return tflite::CreateOperator( + builder_, opcode_index, builder_.CreateVector(operands), + builder_.CreateVector(results), tflite::BuiltinOptions_NONE, 0, 0, + tflite::CustomOptionsFormat_FLEXBUFFERS, 0, 0, 0, 0, + tflite::BuiltinOptions2_StablehloSortOptions, sort_option.Union(), + GetOperatorDebugMetadataIndex(inst)); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + std::string op_name = inst->getName().getStringRef().str(); + uint32_t opcode_index = + GetOpcodeIndex(op_name, tflite::BuiltinOperator_STABLEHLO_WHILE); + + auto& cond = vhlo_op.getCond(); + int32_t cond_subgraph_index = UnnamedRegionToSubgraph( + &cond, tflite::BuiltinOperator_STABLEHLO_WHILE); + if (cond_subgraph_index < 0) return std::nullopt; + + auto& body = vhlo_op.getBody(); + int32_t body_subgraph_index = UnnamedRegionToSubgraph( + &body, tflite::BuiltinOperator_STABLEHLO_WHILE); + if (body_subgraph_index < 0) return std::nullopt; + + auto while_option = tflite::CreateStablehloWhileOptions( + builder_, cond_subgraph_index, body_subgraph_index); + + return tflite::CreateOperator( + builder_, opcode_index, builder_.CreateVector(operands), + builder_.CreateVector(results), tflite::BuiltinOptions_NONE, 0, 0, + tflite::CustomOptionsFormat_FLEXBUFFERS, 0, 0, 0, 0, + tflite::BuiltinOptions2_StablehloWhileOptions, while_option.Union(), + GetOperatorDebugMetadataIndex(inst)); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + std::string op_name = inst->getName().getStringRef().str(); + uint32_t opcode_index = GetOpcodeIndex( + op_name, tflite::BuiltinOperator_STABLEHLO_TRANSPOSE); + + auto transpose_option = tflite::CreateStablehloTransposeOptions( + builder_, + builder_.CreateVector(mlir::GetVector( + mlir::cast(vhlo_op.getPermutation()), + vhlo_type_converter))); + + return tflite::CreateOperator( + builder_, opcode_index, builder_.CreateVector(operands), + builder_.CreateVector(results), tflite::BuiltinOptions_NONE, 0, 0, + tflite::CustomOptionsFormat_FLEXBUFFERS, 0, 0, 0, 0, + tflite::BuiltinOptions2_StablehloTransposeOptions, + transpose_option.Union(), GetOperatorDebugMetadataIndex(inst)); + } + if (auto vhlo_op = llvm::dyn_cast(inst)) { + return BuildStablehloOperatorwithoutOptions( + inst, operands, results, tflite::BuiltinOperator_STABLEHLO_CBRT); + } + } + return inst->emitOpError("is not part of the vhlo support yet."), + std::nullopt; + } + + if (dialect == tf_dialect_) { + if (auto ifOp = dyn_cast(inst)) { + return BuildIfOperator(ifOp, operands, results); + } + + CustomOptionsOffset custom_options; + + // Ops in TF dialect can either be custom ops or flex ops. + // The reason we go directly from TensorFlow dialect MLIR to tensorflow + // node instead of going to TF table gen'd ops via generated code is that + // we do not want to restrict custom and flex op conversion support to + // only those TF ops that are currently registered in MLIR. The current + // model is of an open op system. + // + // The following algorithm is followed: + // if flex is enabled and the op is allowlisted as flex + // we emit op as flex. + // if custom is enabled + // we emit the op as custom. + auto node_def = GetTensorFlowNodeDef(inst); + if (!node_def) { + return std::nullopt; + } + + std::string op_name = node_def->op(); + std::string op_desc = GetOpDescriptionForDebug(inst); + + if (IsTFResourceOp(inst)) { + resource_ops_[op_name].insert(op_desc); + } + + const bool is_allowed_flex_op = + !IsUnsupportedFlexOp(node_def->op()) && + (IsAllowlistedFlexOp(node_def->op()) || + (((select_user_tf_ops_.count(node_def->op()) != 0) || + allow_all_select_tf_ops_) && + (tensorflow::OpRegistry::Global()->LookUp(node_def->op()) != + nullptr))); + + // Flex op case + // Eventually, the allowlist will go away and we will rely on some TF op + // trait (e.g. No side effect) to determine if it is a supported "Flex" + // op or not. + if (is_allowed_flex_op && enabled_op_types_.contains(OpType::kSelectTf)) { + // Construct ops as flex op encoding TensorFlow node definition + // as custom options. + // Flex ops are named with the kFlexOpNamePrefix prefix to the actual + // TF op name. + op_name = std::string(kFlexOpNamePrefix) + node_def->op(); + if (auto options = CreateFlexOpCustomOptions(*node_def, inst->getLoc())) { + custom_options = *options; + } else { + return std::nullopt; + } + + // Gather flex ops. + flex_ops_[op_name].insert(op_desc); + } else if (enabled_op_types_.contains(OpType::kCustomOp)) { + // Generic case of custom ops - write using flex buffers since that + // is the only custom options supported by TFLite today. + op_name = node_def->op(); + if (auto options = + CreateCustomOpCustomOptions(*node_def, inst->getLoc())) { + custom_options = *options; + } else { + return std::nullopt; + } + + // Gather custom ops. + custom_ops_[op_name].insert(op_desc); + } else { + // Insert failed op to `flex_ops` or `custom_ops`. + if (is_allowed_flex_op) { + failed_flex_ops_[op_name].insert(op_desc); + tfl::AttachErrorCode( + inst->emitOpError("is neither a custom op nor a flex op"), + tflite::metrics::ConverterErrorData::ERROR_NEEDS_FLEX_OPS); + } else { + failed_custom_ops_[op_name].insert(op_desc); + tfl::AttachErrorCode( + inst->emitOpError("is neither a custom op nor a flex op"), + tflite::metrics::ConverterErrorData::ERROR_NEEDS_CUSTOM_OPS); + } + return std::nullopt; + } + + uint32_t opcode_index = + GetOpcodeIndex(op_name, tflite::BuiltinOperator_CUSTOM); + auto inputs = builder_.CreateVector(operands); + auto outputs = builder_.CreateVector(results); + + return tflite::CreateOperator( + builder_, opcode_index, inputs, outputs, tflite::BuiltinOptions_NONE, + /*builtin_options=*/0, + /*custom_options=*/custom_options, + tflite::CustomOptionsFormat_FLEXBUFFERS, + /*mutating_variable_inputs=*/0, + /*intermediates=*/0, + /*large_custom_options_offset=*/0, + /*large_custom_options_size=*/0, + /*builtin_options_2_type=*/tflite::BuiltinOptions2_NONE, + /*builtin_options_2=*/0, + /*debug_metadata_index=*/GetOperatorDebugMetadataIndex(inst)); + } + + return inst->emitOpError( + "is not any of a builtin TFLite op, a flex TensorFlow op or a " + "custom TensorFlow op"), + std::nullopt; +} + +void Translator::InitializeNamesFromAttribute(FuncOp fn, bool* has_input_attr) { + auto dict_attr = fn->getAttrOfType("tf.entry_function"); + if (!dict_attr) return; + + llvm::SmallVector input_names; + llvm::SmallVector output_names; + if (auto str = + mlir::dyn_cast_or_null(dict_attr.get("inputs"))) { + str.getValue().split(input_names, ',', /*MaxSplit=*/-1, + /*KeepEmpty=*/false); + if (input_names.size() != fn.getNumArguments()) { + fn.emitWarning() << "invalid entry function specification"; + return; + } + for (const auto& it : llvm::enumerate(fn.getArguments())) { + name_mapper_.InitOpName(it.value(), input_names[it.index()].trim()); + } + *has_input_attr = true; + } + + if (auto str = + mlir::dyn_cast_or_null(dict_attr.get("outputs"))) { + str.getValue().split(output_names, ',', /*MaxSplit=*/-1, + /*KeepEmpty=*/false); + auto term = fn.back().getTerminator(); + if (output_names.size() != term->getNumOperands()) { + fn.emitWarning() << "output names (" << output_names.size() + << ") != terminator operands (" << term->getNumOperands() + << ")"; + return; + } + for (const auto& it : llvm::enumerate(term->getOperands())) { + name_mapper_.InitOpName(it.value(), output_names[it.index()].trim()); + } + } +} + +bool Translator::IsStatefulOperand(mlir::Operation* op, int operand_index) { + std::vector operand_indices; + if (!mlir::TFL::IsStatefulOp(op, &operand_indices)) return false; + return absl::c_find(operand_indices, operand_index) != operand_indices.end(); +} + +BufferOffset +Translator::GetQuantizationForQuantStatsOpOutput( + mlir::quantfork::StatisticsOp stats_op) { + auto layer_stats = + mlir::cast(stats_op.getLayerStats()); + std::optional axis_stats = stats_op.getAxisStats(); + std::optional axis = stats_op.getAxis(); + std::vector mins, maxs; + mlir::DenseFPElementsAttr min_max_attr = + axis_stats.has_value() + ? mlir::cast(axis_stats.value()) + : layer_stats; + + for (const auto& index_and_value : + llvm::enumerate(min_max_attr.getValues())) { + const llvm::APFloat value = index_and_value.value(); + if (index_and_value.index() % 2 == 0) { + mins.push_back(value.convertToFloat()); + } else { + maxs.push_back(value.convertToFloat()); + } + } + + return tflite::CreateQuantizationParameters( + builder_, builder_.CreateVector(mins), + builder_.CreateVector(maxs), /*scale=*/0, /*zero_point=*/0, + tflite::QuantizationDetails_NONE, /*details=*/0, + /*quantized_dimension=*/axis.has_value() ? axis.value() : 0); +} + +std::optional> Translator::BuildSubGraph( + const std::string& name, Region* region, const int index) { + const auto control_edges = ExtractControlEdges(®ion->front()); + bool has_input_attr = false; + if (auto fn = dyn_cast(region->getParentOp())) { + InitializeNamesFromAttribute(fn, &has_input_attr); + } + std::vector> tensors; + llvm::DenseMap tensor_index_map; + + // Builds tensor and buffer for argument or operation result. Returns false + // on failure. + auto build_tensor_and_buffer = [&](Value value, const int subgraph_index, + const std::string& tensor_name) { + // NoneType represents optional and may be skipped here. + if (mlir::isa(value.getType())) { + tensor_index_map.insert({value, kTfLiteMigrationOptionalTensor}); + return true; + } + + tensor_index_map.insert({value, tensors.size()}); + tensor_index_map_[subgraph_index][tensor_name] = tensors.size(); + std::optional> + quant_parameters; + if (value.hasOneUse()) { + auto stats_op = + llvm::dyn_cast(*value.user_begin()); + if (stats_op) { + quant_parameters = GetQuantizationForQuantStatsOpOutput(stats_op); + } + } + + // External buffer id is enforced to have MSB set to 1 to distinguish from + // buffer index/id, with the assumption that the number of external buffers + // are less than 2^31. + uint32_t external_buffer_id = + (1 << 31) | static_cast(external_buffers_.size()); + int buffer_index = buffers_.size(); + + // TODO(ashwinm): Check if for stateful tensors, if it is also needed to + // make the Buffer empty apart from setting the buffer_idx=0 in the + // Tensor. This does not seem to affect runtime behavior for RNN/LSTM, + // but would be good for reducing memory footprint. + if (auto external_buffer_or = + BuildExternalBuffer(value, external_buffer_id); + external_buffer_or.has_value()) { + buffer_index = 0; + external_buffers_.push_back(*external_buffer_or); + } else if (value.getDefiningOp()) { + // If a constant is returned as subgraph's output, this constant cannot be + // deduplicated. + const bool not_returned_by_subgraph = + llvm::none_of(value.getUsers(), [](Operation* user) { + return llvm::isa(user); + }); + auto buffer_or = + BuildBuffer(value, not_returned_by_subgraph, buffer_index); + if (!buffer_or) return false; + external_buffer_id = 0; + buffers_.push_back(*buffer_or); + } else { + external_buffer_id = 0; + buffers_.push_back(empty_buffer_); + } + + auto tensor_or = BuildTensor(value, tensor_name, buffer_index, + external_buffer_id, quant_parameters); + if (!tensor_or) return false; + tensors.push_back(*tensor_or); + + return true; + }; + + std::vector> operators; + + // Maps positions of operations in bb to positions in operators + llvm::DenseMap operation_index_to_operator_index; + std::vector operators_in_mlir; + auto& bb = region->front(); + + // Main function's arguments are first passed to `input` op so they don't + // have associated tensor and buffer. Build FlatBuffer tensor and buffer for + // other functions. + for (unsigned i = 0, e = bb.getNumArguments(); i < e; ++i) { + mlir::BlockArgument arg = bb.getArgument(i); + std::string tensor_name; + if (has_input_attr) + tensor_name = std::string(name_mapper_.GetUniqueName(arg)); + if (tensor_name.empty()) { + if (name == "main") { + tensor_name = absl::StrCat("arg", i); + } else { + tensor_name = absl::StrCat(name, "_arg", i); + } + } + if (!build_tensor_and_buffer(arg, index, tensor_name)) return std::nullopt; + } + + bool failed_once = false; + for (const auto& item : llvm::enumerate(bb)) { + Operation& inst = item.value(); + const int operation_index = item.index(); + if (inst.hasTrait()) break; + // For "quant.stats" op, it's used to store the quantization parameters + // info and its output should be then replaced by its input value. + if (auto quant_stats_op = + llvm::dyn_cast(inst)) { + continue; + } + std::vector intermediates; + // Build intermediate tensors for tfl.lstm and insert these tensors into + // flatbuffer. + if (llvm::isa( + inst)) { + std::vector intermediate_names = { + "input_to_input_intermediate", "input_to_forget_intermediate", + "input_to_cell_intermediate", "input_to_output_intermediate", + "effective_hidden_scale_intermediate"}; + for (const std::string& intermediate : intermediate_names) { + auto intermediate_attr = inst.getAttr(intermediate); + if (auto attr = + mlir::dyn_cast_or_null(intermediate_attr)) { + Type qtype = attr.getValue(); + auto tensor_or = BuildTensorFromType( + qtype, name_mapper_.GetUniqueName(intermediate).str()); + if (!tensor_or.has_value()) { + continue; + } else { + intermediates.push_back(tensors.size()); + tensors.push_back(tensor_or.value()); + } + } + } + } + + for (auto val : inst.getResults()) { + std::string tensor_name = UniqueName(val); + // For "tfl.numeric_verify" op, the name is used to find out the + // original activation tensor rather than its own unique name in the + // visualization or debugging tools. + auto builtin_code = GetBuiltinOpCode(&inst); + if (!builtin_code && dyn_cast(&inst)) { + // The first operand is the quantized activation, the target of this + // NumericVerify op. + auto quantized_op_val = inst.getOperands().front(); + tensor_name = "NumericVerify/" + UniqueName(quantized_op_val) + ":" + + std::to_string(tensor_index_map[quantized_op_val]); + } + if (!build_tensor_and_buffer(val, index, tensor_name)) + return std::nullopt; + } + + // Skip constant ops as they don't represent a TFLite operator. + if (IsConst(&inst)) continue; + + // Fetch operand and result tensor indices. + std::vector results; + results.reserve(inst.getNumResults()); + for (auto result : inst.getResults()) { + results.push_back(tensor_index_map.lookup(result)); + } + Operation* real_inst = &inst; + std::vector operands; + operands.reserve(real_inst->getNumOperands()); + for (auto operand : real_inst->getOperands()) { + if (mlir::isa(operand.getType())) + operands.push_back(kTfLiteMigrationOptionalTensor); + else if (auto stats_op = + llvm::dyn_cast_or_null( + operand.getDefiningOp())) + operands.push_back(tensor_index_map.lookup(stats_op.getArg())); + else + operands.push_back(tensor_index_map.lookup(operand)); + } + + // CustomTfOp is just a wrapper around a TF op, we export the custom Op + // not the wrapper, so we fetch the op from the region. + if (auto custom_op = dyn_cast(inst)) { + // If we have custom op with a region, then use the first op in the + // region, if it exists, otherwise just use params for custom op. + if (!custom_op.getBody().empty()) { + real_inst = &custom_op.getBody().front().front(); + } else { + module_.emitError( + "Invalid CustomTfOp: Custom TF Op have empty region."); + } + } + if (auto tfl_operator = + BuildOperator(real_inst, operands, results, intermediates)) { + operation_index_to_operator_index.try_emplace(operation_index, + operators.size()); + operators.push_back(*tfl_operator); + operators_in_mlir.push_back(real_inst); + } else { + failed_once = true; + } + } + if (index + 1 > subgraph_op_inst_map_.size()) { + subgraph_op_inst_map_.resize(index + 1); + } + subgraph_op_inst_map_[index] = operators_in_mlir; + if (failed_once) return std::nullopt; + + // Get input and output tensor indices for the subgraph. + std::vector inputs, outputs; + for (auto arg : bb.getArguments()) { + inputs.push_back(tensor_index_map[arg]); + } + for (auto result : bb.getTerminator()->getOperands()) { + outputs.push_back(tensor_index_map[result]); + } + for (const auto& [from, to] : control_edges) { + for (int what : {from, to}) { + if (operation_index_to_operator_index.count(what) == 0) { + module_.emitError( + "dangling control edge -- at least one vertex Operation isn't a " + "flatbuffer Operator."); + } + } + model_control_dependencies_[index].emplace_back( + operation_index_to_operator_index[from], + operation_index_to_operator_index[to]); + } + + int subgraph_debug_metadata_index = -1; + if (auto it = debug_metadata_idx_map_.find(name); + it != debug_metadata_idx_map_.end()) { + subgraph_debug_metadata_index = it->second.first; + } + + return tflite::CreateSubGraph( + builder_, builder_.CreateVector(tensors), builder_.CreateVector(inputs), + builder_.CreateVector(outputs), builder_.CreateVector(operators), + /*name=*/builder_.CreateString(name), subgraph_debug_metadata_index); +} + +BufferOffset Translator::BuildMetadata(StringRef name, + StringRef content) { + auto buffer_index = buffers_.size(); + auto buffer_data = builder_.CreateVector( + reinterpret_cast(content.data()), content.size()); + buffers_.push_back(tflite::CreateBuffer(builder_, buffer_data)); + return tflite::CreateMetadataDirect(builder_, name.data(), buffer_index); +} + +// Define a custom hasher for mlir::Location. +struct MlirLocationHasher { + std::size_t operator()(const mlir::Location& loc) const noexcept { + return hash_value(loc); + } +}; + +// Recursively create debug metadata location from a mlir_location, and put the +// created locations inside attribute vector. `location_map` is used for +// deduplication purpose. +uint32_t CreateLocation( + flatbuffers::FlatBufferBuilder& builder, + const mlir::Location& mlir_location, + std::vector& attribute_type_vector, + std::vector>& attribute_vector, + absl::flat_hash_map& + location_map) { + // Use the existing index if the location has been created. + if (auto it = location_map.find(mlir_location); it != location_map.end()) { + return it->second; + } + + flatbuffers::Offset location; + if (auto name_loc = mlir::dyn_cast(mlir_location)) { + uint32_t child_idx = + CreateLocation(builder, name_loc.getChildLoc(), attribute_type_vector, + attribute_vector, location_map); + location = debug_metadata::CreateLocation( + builder, debug_metadata::LocationType_NameLoc, + debug_metadata::CreateNameLoc( + builder, builder.CreateString(name_loc.getName().str()), child_idx) + .Union()); + } else if (auto file_line_col_loc = + mlir::dyn_cast(mlir_location)) { + location = debug_metadata::CreateLocation( + builder, debug_metadata::LocationType_FileLineColLoc, + debug_metadata::CreateFileLineColLoc( + builder, + builder.CreateString(file_line_col_loc.getFilename().str()), + file_line_col_loc.getLine(), file_line_col_loc.getColumn()) + .Union()); + } else if (auto call_site_loc = + mlir::dyn_cast(mlir_location)) { + auto callee_location_idx = + CreateLocation(builder, call_site_loc.getCallee(), + attribute_type_vector, attribute_vector, location_map); + auto caller_location_idx = + CreateLocation(builder, call_site_loc.getCaller(), + attribute_type_vector, attribute_vector, location_map); + location = debug_metadata::CreateLocation( + builder, debug_metadata::LocationType_CallSiteLoc, + debug_metadata::CreateCallSiteLoc(builder, callee_location_idx, + caller_location_idx) + .Union()); + } else if (auto fused_loc = mlir::dyn_cast(mlir_location)) { + auto fused_locations = fused_loc.getLocations(); + std::vector location_indexes; + location_indexes.reserve(fused_locations.size()); + for (const auto& loc : fused_locations) { + location_indexes.push_back(CreateLocation( + builder, loc, attribute_type_vector, attribute_vector, location_map)); + } + location = debug_metadata::CreateLocation( + builder, debug_metadata::LocationType_FusedLoc, + debug_metadata::CreateFusedLoc(builder, + builder.CreateVector(location_indexes)) + .Union()); + } else if (auto unknown_loc = + mlir::dyn_cast(mlir_location)) { + location = debug_metadata::CreateLocation( + builder, debug_metadata::LocationType_UnknownLoc, + debug_metadata::CreateUnknownLoc(builder).Union()); + } else { + LOG(WARNING) << "Location type not supported"; + // Return an index for a placeholder UnknownLoc. + // Check if UnknownLoc has been created. + if (auto it = location_map.find( + mlir::UnknownLoc::get(mlir_location.getContext())); + it != location_map.end()) { + return it->second; + } + location = debug_metadata::CreateLocation( + builder, debug_metadata::LocationType_UnknownLoc, + debug_metadata::CreateUnknownLoc(builder).Union()); + } + + // Append to attributes. + attribute_type_vector.push_back(debug_metadata::Attribute_Location); + attribute_vector.push_back(location.Union()); + + uint32_t new_index = attribute_vector.size() - 1; + location_map.insert({mlir_location, new_index}); + + // Also add a map entry for UnknownLoc if we logged a warning, so we don't + // create multiple placeholders. + if (IsUnsupportedLocation(mlir_location)) { + location_map.insert( + {mlir::UnknownLoc::get(mlir_location.getContext()), new_index}); + } + + return new_index; +} + +// Create debug metadata location for an operation. +// This function is updated to unwrap the location structure created on import +// to restore the original location for roundtrip consistency. +std::vector CreateOpLocation( + flatbuffers::FlatBufferBuilder& builder, mlir::Operation* op, + std::vector& attribute_type_vector, + std::vector>& attribute_vector, + absl::flat_hash_map& + location_map) { + mlir::Location loc_to_serialize = op->getLoc(); + + // The import process via `OpLoc` wraps the original location (`orig_loc`) + // to attach tensor names. The structure is either: + // 1. For multi-output ops: `FusedLoc([NameLoc(t1, orig_loc), ...])` + // 2. For single-output ops (after canonicalization): `NameLoc(t1, orig_loc)` + // This logic reverses this wrapping to retrieve `orig_loc` without touching + // any nested structures inside `orig_loc`. + if (auto fused_loc = mlir::dyn_cast(loc_to_serialize)) { + if (auto str_attr = + mlir::dyn_cast_or_null(fused_loc.getMetadata()); + str_attr && str_attr.getValue() == mlir::TFL::kImporterWrapper) { + // This is our wrapper! It's safe to unwrap. + // The original location is the child of the first NameLoc inside. + const auto& inner_locs = fused_loc.getLocations(); + if (!inner_locs.empty()) { + if (auto name_loc = mlir::dyn_cast(inner_locs[0]); + name_loc != nullptr) { + loc_to_serialize = name_loc.getChildLoc(); + } + } + } + } + + // If the marker is not found, loc_to_serialize remains unchanged, + // preserving the original debug info from earlier conversion steps. + uint32_t loc_idx = + CreateLocation(builder, loc_to_serialize, attribute_type_vector, + attribute_vector, location_map); + return {loc_idx}; +} + +// Serialize debug metadata from module into a string. +std::string Translator::SerializeDebugMetadata(mlir::ModuleOp module) { + flatbuffers::FlatBufferBuilder builder; + + std::vector> + subgraphs_debug_metadata; + + std::vector attribute_type_vector; + std::vector> attribute_vector; + + // Map from mlir::Location to the index of its created flatbuffer location. + absl::flat_hash_map + location_map; + + module.walk([&](FuncOp func) { + debug_metadata_idx_map_.insert({func.getName().str(), + {subgraphs_debug_metadata.size(), + absl::flat_hash_map()}}); + auto& operator_debug_metadata_map = + debug_metadata_idx_map_[func.getName().str()].second; + + std::vector> + operators_debug_metadata; + + auto& first_bb = func.getBody().front(); + for (auto& op : first_bb) { + // Skip terminal op. + if (op.hasTrait()) continue; + + operator_debug_metadata_map[&op] = operators_debug_metadata.size(); + + std::vector attribute_indexes = CreateOpLocation( + builder, &op, attribute_type_vector, attribute_vector, location_map); + auto operator_debug_metadata = + debug_metadata::CreateOperatorDebugMetadata( + builder, builder.CreateVector(attribute_indexes)); + operators_debug_metadata.push_back(operator_debug_metadata); + } + + // Build SubgraphDebugMetadata. + flatbuffers::Offset + subgraph_debug_metadata = debug_metadata::CreateSubgraphDebugMetadata( + builder, builder.CreateVector(operators_debug_metadata)); + subgraphs_debug_metadata.push_back(subgraph_debug_metadata); + }); + + // Build ConversionDebugMetadata. + flatbuffers::Offset + conversion_debug_metadata = debug_metadata::CreateConversionDebugMetadata( + builder, builder.CreateVector(subgraphs_debug_metadata), + builder.CreateVector(attribute_type_vector), + builder.CreateVector(attribute_vector)); + + // Build root DebugMetadata. + std::vector debug_metadata_type_vector; + debug_metadata_type_vector.push_back( + debug_metadata::DebugMetadataType_ConversionDebugMetadata); + auto debug_metadata = debug_metadata::CreateDebugMetadata( + builder, builder.CreateVector(debug_metadata_type_vector), + builder.CreateVector({conversion_debug_metadata.Union()})); + builder.Finish(debug_metadata); + + return std::string(reinterpret_cast(builder.GetBufferPointer()), + builder.GetSize()); +} + +std::optional>> +Translator::CreateMetadataVector() { + constexpr StringRef kRuntimeVersionMetadataKey = "min_runtime_version"; + auto dict_attr = module_->getAttrOfType("tfl.metadata"); + std::vector> metadata; + if (dict_attr) { + for (const auto& named_attr : dict_attr) { + StringRef name = named_attr.getName(); + if (name == kRuntimeVersionMetadataKey) { + LOG(WARNING) << "Skipping runtime version metadata in the model. This " + "will be generated by the exporter."; + continue; + } + mlir::Attribute attr = named_attr.getValue(); + if (auto content = mlir::dyn_cast(attr)) { + metadata.push_back(BuildMetadata(name, content.getValue())); + } else { + module_.emitError( + "all values in tfl.metadata's dictionary key-value pairs should " + "be string attributes"); + return std::nullopt; + } + } + } + // Runtime version string is generated after we update the op + // versions. Here we put a 16-byte dummy string as a placeholder. We choose + // 16-byte because it's the alignment of buffers in flatbuffer, so it won't + // cause any waste of space if the actual string is shorter than 16 bytes. + constexpr std::size_t kByteStringSize = 16; + metadata.push_back(BuildMetadata(kRuntimeVersionMetadataKey, + std::string(kByteStringSize, '\0'))); + if (use_buffer_offset_) { + metadata.push_back( + BuildMetadata(tflite_metadata_buffer_location, "outside flatbuffers")); + } + for (const auto& kv : metadata_) { + const std::string& val = kv.second; + // Only take the first kByteStringSize values. + const int count = std::min(kByteStringSize, val.length()); + std::string value = std::string(kByteStringSize, '\0') + .assign(val.begin(), val.begin() + count); + metadata.push_back(BuildMetadata(kv.first, value)); + } + + // Populate the model control dependencies metadata entry. + if (std::any_of( + model_control_dependencies_.begin(), + model_control_dependencies_.end(), + [](const tflite::ControlEdges& edges) { return !edges.empty(); })) { + metadata.push_back( + BuildMetadata(tflite::kModelControlDependenciesMetadataKey, + tflite::SerializeModelControlDependencies( + model_control_dependencies_))); + } + + // Debug metadata. + if (serialize_debug_metadata_) { + metadata.push_back( + BuildMetadata("debug_metadata", debug_metadata_serialized_string_)); + } + + return builder_.CreateVector(metadata); +} + +// Helper method that returns list of all strings in a StringAttr identified +// by 'attr_key' and values are separated by a comma. +llvm::SmallVector GetStringsFromAttrWithSeparator( + mlir::DictionaryAttr attr, const std::string& attr_key) { + llvm::SmallVector result; + if (auto str = mlir::dyn_cast_or_null(attr.get(attr_key))) { + str.getValue().split(result, ',', /*MaxSplit=*/-1, + /*KeepEmpty=*/false); + } + return result; +} + +// Helper method that return list of string for all the StringAttr in the +// Attribute identified by 'attr_name'. +std::vector GetStringsFromDictionaryAttr( + const llvm::SmallVector& dict_attrs, + const StringRef attr_name) { + std::vector result; + for (const auto& arg_attr : dict_attrs) { + if (!arg_attr) continue; + + auto attrs = arg_attr.getValue(); + for (const auto attr : attrs) { + if (attr.getName() == attr_name) { + auto array_attr = + mlir::dyn_cast_or_null(attr.getValue()); + if (!array_attr || array_attr.empty()) continue; + auto string_attr = + mlir::dyn_cast_or_null(array_attr[0]); + if (!string_attr) continue; + result.push_back(string_attr.getValue().str()); + } + } + } + return result; +} + +std::vector BuildSignaturedef( + FuncOp main_op, const std::string& saved_model_tag, + const uint32_t subgraph_index, tensorflow::OpOrArgNameMapper& name_mapper) { + static const char kEntryFunctionAttributes[] = "tf.entry_function"; + + // Fetch inputs and outputs from the signature. + llvm::SmallVector arg_attrs, res_attrs; + main_op.getAllArgAttrs(arg_attrs); + main_op.getAllResultAttrs(res_attrs); + std::vector sig_def_inputs = + GetStringsFromDictionaryAttr(arg_attrs, kTfSavedModelIndexPathAttr); + std::vector sig_def_outputs = + GetStringsFromDictionaryAttr(res_attrs, kTfSavedModelIndexPathAttr); + + // If no defined saved model signature, then return empty list. + // This can happen when we are converting model not from SavedModel. + if (sig_def_inputs.empty() && sig_def_outputs.empty()) return {}; + + // Fetch function inputs and outputs tensor names. + auto dict_attr = + main_op->getAttrOfType(kEntryFunctionAttributes); + if (!dict_attr) { + main_op.emitWarning() << "failed to get entry function attr."; + return {}; + } + + // Get Input and output tensor names from attribute. + llvm::SmallVector input_names = + GetStringsFromAttrWithSeparator(dict_attr, /*attr_key=*/"inputs"); + llvm::SmallVector output_names = + GetStringsFromAttrWithSeparator(dict_attr, /*attr_key=*/"outputs"); + + // Verify input size match the number of arguments. + if (input_names.size() != main_op.getNumArguments()) { + main_op.emitWarning() << "invalid entry function specification."; + return {}; + } + // Verify output size match the number of arguments. + auto term = main_op.back().getTerminator(); + if (output_names.size() != term->getNumOperands()) { + main_op.emitWarning() << "output names (" << output_names.size() + << ") != terminator operands (" + << term->getNumOperands() << ")"; + return {}; + } + // Verify number of tensors for inputs and outputs matches size + // of the list in the signature def. + if (input_names.size() != sig_def_inputs.size() || + output_names.size() != sig_def_outputs.size()) { + main_op.emitWarning( + "Mismatch between signature def inputs/outputs and main function " + "arguments."); + return {}; + } + // Exported method name. + auto exported_name = + main_op->getAttrOfType(kTfSavedModelExportedNamesAttr); + if (!exported_name || exported_name.empty()) { + main_op.emitError("Empty exported names for main Function."); + return {}; + } + // Fill the SignatureDefData container. + // We create vector of size 1 as TFLite now supports only 1 signatureDef. + std::vector result(1); + for (int i = 0; i < input_names.size(); ++i) { + result[0].inputs[sig_def_inputs[i]] = input_names[i].str(); + } + for (int i = 0; i < output_names.size(); ++i) { + // Fetch the name from the actual operand and not rely on names from + // outputs as deduping can make them invalid after conversion. + auto& operand = term->getOpOperand(i); + auto unique_name = std::string(name_mapper.GetUniqueName(operand.get())); + result[0].outputs[sig_def_outputs[i]] = unique_name; + } + if (auto name_attr = mlir::dyn_cast_or_null(exported_name[0])) + result[0].signature_key = name_attr.getValue().str(); + result[0].subgraph_index = subgraph_index; + return result; +} + +std::vector> Translator::GetList( + const int subgraph_index, const std::map& items) { + std::vector> result; + for (const auto& item : items) { + auto name_buf = builder_.CreateString(item.first); + tflite::TensorMapBuilder tensor_map_builder(builder_); + tensor_map_builder.add_name(name_buf); + tensor_map_builder.add_tensor_index( + tensor_index_map_[subgraph_index][item.second]); + result.push_back(tensor_map_builder.Finish()); + } + return result; +} + +std::optional>> +Translator::CreateSignatureDefs( + const std::vector& signature_defs) { + std::vector> signature_defs_buffer; + // When we export each function in the module op, intentionally, we export + // the entry functions at the beginning of the subgraph list and the + // subgraph_index is the index in entry functions and at the same, is the + // index in the subgraph list. + int subgraph_index = 0; + for (const auto& signature_def_data : signature_defs) { + auto inputs = GetList(subgraph_index, signature_def_data.inputs); + auto outputs = GetList(subgraph_index, signature_def_data.outputs); + auto inputs_buf = builder_.CreateVector(inputs); + auto outputs_buf = builder_.CreateVector(outputs); + auto signature_key_buf = + builder_.CreateString(signature_def_data.signature_key); + tflite::SignatureDefBuilder sig_def_builder(builder_); + sig_def_builder.add_inputs(inputs_buf); + sig_def_builder.add_outputs(outputs_buf); + sig_def_builder.add_signature_key(signature_key_buf); + sig_def_builder.add_subgraph_index(signature_def_data.subgraph_index); + signature_defs_buffer.push_back(sig_def_builder.Finish()); + ++subgraph_index; + } + + return builder_.CreateVector(signature_defs_buffer); +} + +bool UpdateEntryFunction(ModuleOp module) { + if (module.lookupSymbol("main") != nullptr) { + // We already have an entry function. + return true; + } + + int entry_func_count = 0; + FuncOp entry_func = nullptr; + for (auto fn : module.getOps()) { + auto attrs = fn->getAttrOfType("tf.entry_function"); + if (!attrs || attrs.empty()) continue; + ++entry_func_count; + entry_func = fn; + } + + // We should have at least one entry function. + if (entry_func_count == 0) return false; + + if (entry_func_count == 1) { + // Update the entry func to main when the entry func is only & one. + entry_func.setName(StringAttr::get(module.getContext(), "main")); + } + return true; +} + +constexpr absl::string_view kFlatbufferSizeLimitExceededError = + "Flatbuffer size exceeds 2gb limit."; + +absl::Status Translator::Translate( + ModuleOp module, const tflite::FlatbufferExportOptions& options, + llvm::raw_pwrite_stream& export_stream) { + OpOrArgNameMapper* op_or_arg_name_mapper = options.op_or_arg_name_mapper; + OpOrArgLocNameMapper default_op_or_arg_name_mapper; + if (!op_or_arg_name_mapper) { + op_or_arg_name_mapper = &default_op_or_arg_name_mapper; + } + if (!UpdateEntryFunction(module)) { + return absl::InvalidArgumentError("No entry function found."); + } + if (!IsValidTFLiteMlirModule(module)) { + return absl::InvalidArgumentError("Invalid TFLite MLIR module."); + } + + // Best-effort attempt to enable buffer offset for models that are close to + // the flatbuffer size limit (a lossy threshold is used to encourage buffer + // offset and avoid a second retry) + const int64_t enable_buffer_offset_threshold = + flatbuffer_size_max - (512 * 1024 * 1024 /* 512MB */); + auto converter_flags = options.converter_flags; + if (!converter_flags.use_buffer_offset() && + mlir::TFL::GetApproximateModuleSize(module) > + enable_buffer_offset_threshold) { + llvm::errs() << "Module size is greater than 2GB\n"; + converter_flags.set_use_buffer_offset(true); + } + + auto translate = [&]() { + Translator translator(module, export_stream, converter_flags, + options.saved_model_tags, op_or_arg_name_mapper, + options.metadata, options.custom_option_alignment, + options.disable_buffer_deduping, + options.attribute_buffer_applier_factories); + translator.convert_stablehlo_ = options.serialize_stablehlo_ops; + return translator.TranslateInternal(); + }; + + auto status = translate(); + if (!converter_flags.use_buffer_offset() && !status.ok() && + absl::StrContains(status.message(), kFlatbufferSizeLimitExceededError)) { + // Automatically retry once with use_buffer_offset enabled when the model + // is too large to fit into a flatbuffer and use_buffer_offset is not + // already enabled. + converter_flags.set_use_buffer_offset(true); + status = translate(); + } + return status; +} + +absl::Status Translator::TranslateInternal() { + // Debug metadata needs to be serialized before subgraph/operator + // serialization, since subgraph/operator serialization needs debug metadata + // index. + if (serialize_debug_metadata_) { + debug_metadata_serialized_string_ = SerializeDebugMetadata(module_); + } + + // A list of named regions in the module with main function being the first + // in the list. The main function is required as the first subgraph in the + // model is entry point for the model. + std::vector> named_regions; + named_regions.reserve(std::distance(module_.begin(), module_.end())); + + int subgraph_idx = 0; + + // Entry functions for signature defs. + std::vector entry_functions; + std::vector non_entry_functions; + FuncOp main_fn = module_.lookupSymbol("main"); + if (main_fn != nullptr) { + // Treat the main function as a signature def when the given main function + // contains on the tf.entry_function attribute. + auto attrs = + main_fn->getAttrOfType("tf.entry_function"); + if (attrs && !attrs.empty()) { + entry_functions.push_back(main_fn); + } else { + non_entry_functions.push_back(main_fn); + } + } + + // Walk over the module collection ops with functions and while ops. + module_.walk([&](FuncOp fn) { + if (main_fn == fn) return WalkResult::advance(); + auto attrs = fn->getAttrOfType("tf.entry_function"); + if (attrs && !attrs.empty()) { + entry_functions.push_back(fn); + } else { + non_entry_functions.push_back(fn); + } + return WalkResult::advance(); + }); + + // Assign the subgraph index. Among the given functions, it will put entry + // functions at the beginning of the list of the subgraphs. + for (auto fn : entry_functions) { + subgraph_index_map_[fn.getName().str()] = subgraph_idx++; + named_regions.emplace_back(fn.getName().str(), &fn.getBody()); + } + for (auto fn : non_entry_functions) { + subgraph_index_map_[fn.getName().str()] = subgraph_idx++; + named_regions.emplace_back(fn.getName().str(), &fn.getBody()); + } + + // Build subgraph for each of the named regions. + subgraphs_.resize(named_regions.size()); + model_control_dependencies_.assign(named_regions.size(), {}); + int first_failed_func = -1; + + // When we export each function in the module op, intentionally, we export + // the entry functions at the beginning of the subgraph list and the + // subgraph_index is the index in entry functions and at the same, is the + // index in the subgraph list. + int subgraph_index = 0; + for (const auto& it : llvm::enumerate(named_regions)) { + auto subgraph_or = + BuildSubGraph(it.value().first, it.value().second, subgraph_index); + if (!subgraph_or) { + if (first_failed_func == -1) + // Record the index of the first region that cannot be converted. + // Keep looping through all subgraphs in the module to make sure that + // we collect the list of missing ops from the entire module. + first_failed_func = it.index(); + } else { + subgraphs_[subgraph_index] = *subgraph_or; + ++subgraph_index; + } + } + + if (!resource_ops_.empty()) { + std::string resource_ops_summary = + GetOpsSummary(resource_ops_, /*summary_title=*/"Resource"); + LOG(WARNING) << "Graph contains the following resource op(s), that use(s) " + "resource type. Currently, the " + "resource type is not natively supported in TFLite. Please " + "consider not using the resource type if there are issues " + "with either TFLite converter or TFLite runtime:\n" + << resource_ops_summary; + } + + if (!flex_ops_.empty()) { + std::string flex_ops_summary = + GetOpsSummary(flex_ops_, /*summary_title=*/"Flex"); + LOG(WARNING) << "TFLite interpreter needs to link Flex delegate in order " + "to run the model since it contains the following Select TF" + "op(s):\n" + << flex_ops_summary + << "\nSee instructions: " + "https://www.tensorflow.org/lite/guide/ops_select"; + } + + if (!custom_ops_.empty()) { + std::string custom_ops_summary = + GetOpsSummary(custom_ops_, /*summary_title=*/"Custom"); + LOG(WARNING) << "The following operation(s) need TFLite custom op " + "implementation(s):\n" + << custom_ops_summary + << "\nSee instructions: " + "https://www.tensorflow.org/lite/guide/ops_custom"; + } + + if (first_failed_func != -1) { + std::string failed_flex_ops_summary = + GetOpsSummary(failed_flex_ops_, /*summary_title=*/"TF Select"); + std::string failed_custom_ops_summary = + GetOpsSummary(failed_custom_ops_, /*summary_title=*/"Custom"); + std::string err; + if (!failed_flex_ops_.empty()) + err += + "\nSome ops are not supported by the native TFLite runtime, you " + "can " + "enable TF kernels fallback using TF Select. See instructions: " + "https://www.tensorflow.org/lite/guide/ops_select \n" + + failed_flex_ops_summary + "\n"; + if (!failed_custom_ops_.empty()) + err += + "\nSome ops in the model are custom ops, " + "See instructions to implement " + "custom ops: https://www.tensorflow.org/lite/guide/ops_custom \n" + + failed_custom_ops_summary + "\n"; + + auto& failed_region = named_regions[first_failed_func]; + failed_region.second->getParentOp()->emitError() + << "failed while converting: '" << failed_region.first << "': " << err; + return absl::InvalidArgumentError(err); + } + + // Log MAC count. + int64_t ops_count; + if (EstimateArithmeticCount(&ops_count)) { + const int64_t million = 1e6; + const int64_t billion = 1e9; + std::string flops_str; + std::string mac_str; + if (ops_count < 10000) { + flops_str = absl::StrFormat("%ld ", ops_count); + mac_str = absl::StrFormat("%ld ", ops_count / 2); + } else if (ops_count < billion) { + flops_str = + absl::StrFormat("%.3f M ", static_cast(ops_count) / million); + mac_str = absl::StrFormat("%.3f M ", + static_cast(ops_count / 2) / million); + } else { + flops_str = + absl::StrFormat("%.3f G ", static_cast(ops_count) / billion); + mac_str = absl::StrFormat("%.3f G ", + static_cast(ops_count / 2) / billion); + } + LOG(INFO) << "Estimated count of arithmetic ops: " << flops_str + << " ops, equivalently " << mac_str << " MACs"; + } + + std::string model_description; + if (auto attr = module_->getAttrOfType("tfl.description")) { + model_description = attr.getValue().str(); + } else { + model_description = "MLIR Converted."; + } + + // Build the model and finish the model building process. + auto description = builder_.CreateString(model_description.data()); + VectorBufferOffset metadata_buffer = 0; // Deprecated + auto metadata = CreateMetadataVector(); + if (!metadata) { + return absl::InternalError("Failed to create metadata vector"); + } + + std::vector signature_defs_vec; + subgraph_index = 0; + // Build SignatureDefs for the tf.entry_function based func ops. + for (auto fn : entry_functions) { + auto signature_defs = BuildSignaturedef( + fn, saved_model_tags_.empty() ? "" : *saved_model_tags_.begin(), + subgraph_index, name_mapper_); + for (const auto& signature_def : signature_defs) { + signature_defs_vec.push_back(signature_def); + } + // When we export each function in the module op, intentionally, we export + // the entry functions at the beginning of the subgraph list and the + // subgraph_index is the index in entry functions and at the same, is the + // index in the subgraph list. + ++subgraph_index; + } + auto signature_defs = CreateSignatureDefs(signature_defs_vec); + + bool has_external_buffers = !external_buffers_.empty(); + auto model = tflite::CreateModel( + builder_, TFLITE_SCHEMA_VERSION, builder_.CreateVector(opcodes_), + builder_.CreateVector(subgraphs_), description, + builder_.CreateVector(buffers_), metadata_buffer, *metadata, + *signature_defs, + has_external_buffers ? builder_.CreateVector(external_buffer_groups_) : 0, + has_external_buffers ? builder_.CreateVector(external_buffers_) : 0); + + tflite::FinishModelBuffer(builder_, model); + // There is a limit of 2GB for a flatbuffer. + if (builder_.GetSize() > flatbuffer_size_max) { + auto status = absl::InternalError(kFlatbufferSizeLimitExceededError); + LOG(ERROR) << "Model structure size is bigger than 2gb"; + return status; + } + tflite::UpdateOpVersion(builder_.GetBufferPointer()); + tflite::UpdateMinimumRuntimeVersionForModel(builder_.GetBufferPointer()); + + // Return serialized string for the built FlatBuffer. + if (use_buffer_offset_) { + uint64_t padded_buffer_size = builder_.GetSize(); + // Pad to be 16 bytes aligned + padded_buffer_size += kFbAlignment - builder_.GetSize() % kFbAlignment; + export_stream_.get().write_zeros(padded_buffer_size); + + if (auto status = AppendBufferData(); !status.ok()) { + return status; + } + + auto mutable_model = tflite::GetMutableModel(builder_.GetBufferPointer()); + if (!UpdateBufferOffsets(mutable_model)) { + return absl::InternalError("Failed to update buffer offsets"); + } + + export_stream_.get().pwrite( + reinterpret_cast(builder_.GetBufferPointer()), + builder_.GetSize(), /*offset=*/0); + } else { + export_stream_.get().write( + reinterpret_cast(builder_.GetBufferPointer()), + builder_.GetSize()); + } + return absl::OkStatus(); +} + +absl::Status Translator::AppendBufferData() { + auto offset = [this]() { return export_stream_.get().tell(); }; + + // Reserve extra space for the buffer data. The pessimistic estimation is to + // account for the worst-case padding of 32 bytes for each buffer. + uint64_t estimated_buffer_size = 0; + for (const auto& [_, buffer] : const_buffer_storage_.buffers()) { + estimated_buffer_size += buffer->byte_size_hint(); + estimated_buffer_size += 32; // worst case padding + } + for (const auto& [_, buffer] : custom_op_buffer_storage_.buffers()) { + estimated_buffer_size += buffer->byte_size_hint(); + estimated_buffer_size += 32; // worst case padding + } + export_stream_.get().reserveExtraSpace(estimated_buffer_size); + + std::unordered_map> hashcode_to_pos; + for (const auto& [index, buffer] : const_buffer_storage_.buffers()) { + uint64_t hash = buffer->hash(); + if (hashcode_to_pos.find(hash) == hashcode_to_pos.end()) { + int64_t size = 0; + int64_t buffer_offset = offset(); + auto status = buffer->ApplyData([this, &size](absl::string_view data) { + size += data.size(); + export_stream_.get().write(data.data(), data.size()); + return absl::OkStatus(); + }); + if (!status.ok()) { + return status; + } + hashcode_to_pos[hash] = std::make_pair(buffer_offset, size); + buffer_idx_map_[index] = std::make_pair(buffer_offset, size); + } else { + // only update offset/index. + buffer_idx_map_[index] = hashcode_to_pos[hash]; + } + } + // pad 16 bytes for the last buffer for XNNPack + export_stream_.get().write_zeros(16); + + // pad to be 16 bytes aligned + export_stream_.get().write_zeros(kFbAlignment - offset() % kFbAlignment); + + for (const auto& [key, buffer] : custom_op_buffer_storage_.buffers()) { + export_stream_.get().write_zeros(kFbAlignment - offset() % kFbAlignment); + + if (custom_option_alignment_.has_value()) { + auto alignment = custom_option_alignment_.value(); + export_stream_.get().write_zeros(alignment - offset() % alignment); + } + + int64_t size = 0; + int64_t buffer_offset = offset(); + auto status = buffer->ApplyData([this, &size](absl::string_view data) { + size += data.size(); + export_stream_.get().write(data.data(), data.size()); + return absl::OkStatus(); + }); + if (!status.ok()) { + return status; + } + custom_op_idx_map_[key] = std::make_pair(buffer_offset, size); + } + + // pad to be 16 bytes aligned + export_stream_.get().write_zeros(kFbAlignment - offset() % kFbAlignment); + + return absl::OkStatus(); +} + +bool Translator::UpdateBufferOffsets(tflite::Model* mutable_model) { + auto buffers = mutable_model->mutable_buffers(); + bool ret = true; + for (auto& it : buffer_idx_map_) { + tflite::Buffer* buffer = buffers->GetMutableObject(it.first); + + ret &= buffer->mutate_offset(it.second.first); + ret &= buffer->mutate_size(it.second.second); + } + + if (!ret) { + LOG(ERROR) << "failed to update buffer offsets\n"; + return ret; + } + + auto mutable_subgraphs = mutable_model->mutable_subgraphs(); + for (auto msubgraph : *mutable_subgraphs) { + auto operators = msubgraph->mutable_operators(); + for (auto op : *operators) { + auto opcode_idx = op->opcode_index(); + auto opcodes = mutable_model->operator_codes(); + auto opcode = (*opcodes)[opcode_idx]->builtin_code(); + if (opcode == tflite::BuiltinOperator_CUSTOM) { + std::vector inputs(op->inputs()->begin(), op->inputs()->end()); + std::vector outputs(op->outputs()->begin(), + op->outputs()->end()); + for (auto& custom_op : custom_op_idx_map_) { + std::vector key_inputs = custom_op.first.first; + std::vector key_outputs = custom_op.first.second; + // TODO(b/287306548): we need more rigorous check to make sure the + // node we're updating is the right one for now we're just ensuring + // they have the same number of input, output and first output is + // the same + if (key_inputs.size() == inputs.size() && + key_outputs.size() == outputs.size() && + key_outputs[0] == outputs[0]) { + ret &= + op->mutate_large_custom_options_offset(custom_op.second.first); + ret &= + op->mutate_large_custom_options_size(custom_op.second.second); + } + } + } + } + } + if (!ret) LOG(ERROR) << "failed to update custom op offsets\n"; + + return ret; +} + +BufferOffset Translator::BuildSparsityParameters( + const mlir::TFL::SparsityParameterAttr& s_attr) { + const int dim_size = s_attr.getDimMetadata().size(); + std::vector> fb_dim_metadata( + dim_size); + for (int i = 0; i < dim_size; i++) { + const auto dim_metadata = mlir::dyn_cast( + s_attr.getDimMetadata()[i]); + if (dim_metadata.getFormat().getValue() == + mlir::TFL::DimensionType::DENSE) { + fb_dim_metadata[i] = tflite::CreateDimensionMetadata( + builder_, tflite::DimensionType_DENSE, dim_metadata.getDenseSize()); + + } else { + auto segments = dim_metadata.getSegments(); + std::vector vector_segments(segments.size(), 0); + for (int j = 0, end = segments.size(); j < end; j++) { + vector_segments[j] = segments[j]; + } + tflite::SparseIndexVector segments_type; + BufferOffset array_segments; + // The segment array is sorted. + // TODO(b/147449640): Clean this up with util functions. + int max_of_segments = vector_segments[segments.size() - 1]; + if (max_of_segments <= UINT8_MAX) { + segments_type = tflite::SparseIndexVector_Uint8Vector; + std::vector uint8_vector(vector_segments.begin(), + vector_segments.end()); + array_segments = tflite::CreateUint8Vector( + builder_, builder_.CreateVector(uint8_vector)) + .Union(); + } else if (max_of_segments <= UINT16_MAX) { + segments_type = tflite::SparseIndexVector_Uint16Vector; + std::vector uint16_vector(vector_segments.begin(), + vector_segments.end()); + array_segments = tflite::CreateUint16Vector( + builder_, builder_.CreateVector(uint16_vector)) + .Union(); + } else { + segments_type = tflite::SparseIndexVector_Int32Vector; + array_segments = tflite::CreateInt32Vector( + builder_, builder_.CreateVector(vector_segments)) + .Union(); + } + + auto indices = dim_metadata.getIndices(); + std::vector vector_indices(indices.size(), 0); + int max_of_indices = 0; + for (int j = 0, end = indices.size(); j < end; j++) { + vector_indices[j] = indices[j]; + if (vector_indices[j] > max_of_indices) { + max_of_indices = vector_indices[j]; + } + } + tflite::SparseIndexVector indices_type; + BufferOffset array_indices; + if (max_of_indices <= UINT8_MAX) { + indices_type = tflite::SparseIndexVector_Uint8Vector; + std::vector uint8_vector(vector_indices.begin(), + vector_indices.end()); + array_indices = tflite::CreateUint8Vector( + builder_, builder_.CreateVector(uint8_vector)) + .Union(); + } else if (max_of_indices <= UINT16_MAX) { + indices_type = tflite::SparseIndexVector_Uint16Vector; + std::vector uint16_vector(vector_indices.begin(), + vector_indices.end()); + array_indices = tflite::CreateUint16Vector( + builder_, builder_.CreateVector(uint16_vector)) + .Union(); + } else { + indices_type = tflite::SparseIndexVector_Int32Vector; + array_indices = tflite::CreateInt32Vector( + builder_, builder_.CreateVector(vector_indices)) + .Union(); + } + + fb_dim_metadata[i] = tflite::CreateDimensionMetadata( + builder_, tflite::DimensionType_SPARSE_CSR, 0, segments_type, + array_segments, indices_type, array_indices); + } + } + + std::vector traversal_order(dim_size); + for (int i = 0; i < dim_size; i++) { + traversal_order[i] = s_attr.getTraversalOrder()[i]; + } + const int block_map_size = s_attr.getBlockMap().size(); + std::vector block_map(block_map_size); + for (int i = 0; i < block_map_size; i++) { + block_map[i] = s_attr.getBlockMap()[i]; + } + + return tflite::CreateSparsityParameters( + builder_, builder_.CreateVector(traversal_order), + builder_.CreateVector(block_map), builder_.CreateVector(fb_dim_metadata)); +} + +std::vector> Translator::ExtractControlEdges( + mlir::Block* block) { + std::vector> control_edges; + + mlir::IRRewriter rewriter(block->getParentOp()->getContext()); + + // Since we're modifying *block, we store integer offsets to block->begin(). + llvm::DenseMap control_nodes_at; + std::vector control_nodes; + for (const auto& item : llvm::enumerate(*block)) { + if (llvm::isa(item.value())) { + control_nodes.push_back(&item.value()); + control_nodes_at.try_emplace(&item.value(), item.index()); + } + } + + for (auto outer_op : control_nodes) { + auto control_node_op = dyn_cast(outer_op); + auto* inner_op = &control_node_op.getBody().front().front(); + auto control_token = control_node_op.getControl(); + + // Now go through all uses. Since *block is in executable order, control + // edges always point to operations we haven't modified yet. + for (auto& use : control_token.getUses()) { + auto owner = use.getOwner(); + // Control tokens can only be consumed by other ControlNodeOps, + assert(llvm::isa(owner)); + assert(control_nodes_at.find(owner) != control_nodes_at.end()); + // Control edge in terms of offsets. + control_edges.emplace_back(control_nodes_at[outer_op], + control_nodes_at[owner]); + } + control_token.dropAllUses(); + + // Replace the ControlNodeOp with the wrapped operation. + rewriter.setInsertionPointAfter(outer_op); + auto* cloned_inner = rewriter.clone(*inner_op); + for (auto it : + llvm::zip(control_node_op.getOutputs(), cloned_inner->getResults())) { + std::get<0>(it).replaceAllUsesWith(std::get<1>(it)); + } + rewriter.eraseOp(outer_op); + } + return control_edges; +} + +} // namespace + +namespace tflite { + +absl::Status MlirToFlatBufferTranslateFunction( + mlir::ModuleOp module, const FlatbufferExportOptions& options, + llvm::raw_pwrite_stream& export_stream) { + return Translator::Translate(module, options, export_stream); +} + +// Legacy API that returns a boolean and populates a string with the +// serialized flatbuffer on success. +bool MlirToFlatBufferTranslateFunction(mlir::ModuleOp module, + const FlatbufferExportOptions& options, + std::string* serialized_flatbuffer, + bool serialize_stablehlo_ops) { + llvm::SmallVector buffer; + llvm::raw_svector_ostream export_stream(buffer); + + absl::Status status; + if (serialize_stablehlo_ops != options.serialize_stablehlo_ops) { + FlatbufferExportOptions new_options = options; + new_options.serialize_stablehlo_ops = serialize_stablehlo_ops; + status = + MlirToFlatBufferTranslateFunction(module, new_options, export_stream); + } else { + status = MlirToFlatBufferTranslateFunction(module, options, export_stream); + } + + if (!status.ok()) { + return false; + } + serialized_flatbuffer->assign(buffer.data(), buffer.size()); + return true; +} + +} // namespace tflite diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_export.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_export.h new file mode 100644 index 0000000000000000000000000000000000000000..ba2b9e565ccb2d7e3acc29c1c35eb3c3e1d18dbf --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_export.h @@ -0,0 +1,103 @@ +/* Copyright 2019 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_FLATBUFFER_EXPORT_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_FLATBUFFER_EXPORT_H_ + +#include +#include +#include +#include +#include +#include +#include + +#include "absl/functional/any_invocable.h" +#include "absl/functional/function_ref.h" +#include "absl/status/status.h" +#include "absl/strings/string_view.h" +#include "llvm/Support/raw_ostream.h" +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/converter_flags.pb.h" +#include "tensorflow/compiler/mlir/op_or_arg_name_mapper.h" + +namespace tflite { + +using FlatbufferExportAttributeBufferApplier = absl::AnyInvocable&, + absl::FunctionRef) const>; + +using FlatbufferExportAttributeBufferApplierFactory = + absl::FunctionRef( + mlir::Attribute, mlir::Operation*) const>; + +// Options for exporting to Flatbuffer. +struct FlatbufferExportOptions { + // ConverterFlags proto. The following fields are migrated. + // bool emit_builtin_tflite_ops -> !converter_flags.force_select_tf_ops() + // bool emit_select_tf_ops -> converter_flags.enable_select_tf_ops() + // bool emit_custom_ops -> converter_flags.allow_custom_ops() + // bool allow_all_select_tf_ops -> converter_flags.allow_all_select_tf_ops() + // std::set<> select_user_tf_ops -> converter_flags.select_user_tf_ops() + tflite::ConverterFlags converter_flags; + // When exporting from SavedModel, this will have the requested tags. + std::unordered_set saved_model_tags; + // Metadata key/value pairs to write to the flatbuffer. + std::map metadata; + // OpOrArgNameMapper to convert location of the op to name in flatbuffer. + // If not set, a default mapper will be used. + tensorflow::OpOrArgNameMapper* op_or_arg_name_mapper = nullptr; + // User-specified value of flatbuffer alignment requirement for custom + // options. If specified, the value should be multiplier of 16 (default + // alignment for TFL flatbuffer). + std::optional custom_option_alignment = std::nullopt; + // Whether to disable buffer-deduping which emits tensors with shared + // buffers. + bool disable_buffer_deduping = false; + // If true, convert and serialize VHLO ops to equivalent StableHLO ops in + // flatbuffer. + bool serialize_stablehlo_ops = false; + + // (Internal use only) Additional factories for creating attribute buffer + // appliers. These are used to support exporting custom derived types of + // ElementsAttr as constant buffer holders. + /// The factories are called in the order they are added, and the first + /// factory that returns a valid applier for a given attribute will be used. + /// If no factories return a valid applier, the export will use the default + /// applier that assumes the attribute is a dense host tensor. + std::vector + attribute_buffer_applier_factories; +}; + +// (Legacy) Translates the given MLIR `module` into a FlatBuffer and stores the +// serialized flatbuffer into the string. +// Returns true on successful exporting, false otherwise. +bool MlirToFlatBufferTranslateFunction(mlir::ModuleOp module, + const FlatbufferExportOptions& options, + std::string* serialized_flatbuffer, + bool serialize_stablehlo_ops = false); + +// Translates the given MLIR `module` into a FlatBuffer and writes the +// serialized flatbuffer into the export stream. +// Returns true on successful exporting, false otherwise +absl::Status MlirToFlatBufferTranslateFunction( + mlir::ModuleOp module, const FlatbufferExportOptions& options, + llvm::raw_pwrite_stream& export_stream); + +} // namespace tflite + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_FLATBUFFER_EXPORT_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_export_flags.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_export_flags.h new file mode 100644 index 0000000000000000000000000000000000000000..c5bba80145e1d3f98c285870ef85ba01c4fd7452 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_export_flags.h @@ -0,0 +1,34 @@ +/* Copyright 2019 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_FLATBUFFER_EXPORT_FLAGS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_FLATBUFFER_EXPORT_FLAGS_H_ + +#include + +// These flags are used to control the emission or not of different kinds of ops +// during the flatbuffer translation. +extern bool emit_builtin_tflite_ops; +extern bool emit_select_tf_ops; +extern bool emit_custom_ops; +// The flag to control whether to lower tensorlist ops into TF ops. +extern bool lower_tensor_list_ops; +// The flag to control whether debug info gets stripped on export. +extern bool strip_debug_info; +// The flag to control whether to store constant & custom buffers inside +// flatbuffer +extern bool use_buffer_offset; + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_FLATBUFFER_EXPORT_FLAGS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_import.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_import.cc new file mode 100644 index 0000000000000000000000000000000000000000..bc5c3fdc4e85d4be63f1b86aa73be7a237a94b33 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_import.cc @@ -0,0 +1,2075 @@ +/* Copyright 2019 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/compiler/mlir/lite/flatbuffer_import.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "absl/container/flat_hash_map.h" +#include "absl/container/flat_hash_set.h" +#include "absl/log/check.h" +#include "absl/log/log.h" +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/str_join.h" +#include "absl/strings/str_split.h" +#include "absl/strings/string_view.h" +#include "llvm/ADT/APFloat.h" +#include "llvm/ADT/APInt.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringExtras.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/StringSet.h" +#include "llvm/Analysis/AssumeBundleQueries.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/FormatVariadic.h" +#include "llvm/Support/LogicalResult.h" +#include "mlir/Conversion/ReconcileUnrealizedCasts/ReconcileUnrealizedCasts.h" // from @llvm-project +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/Dialect/Func/Extensions/AllExtensions.h" // from @llvm-project +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Dialect/Quant/IR/Quant.h" // from @llvm-project +#include "mlir/Dialect/Quant/IR/QuantTypes.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/Diagnostics.h" // from @llvm-project +#include "mlir/IR/IRMapping.h" // from @llvm-project +#include "mlir/IR/Location.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/Matchers.h" // from @llvm-project +#include "mlir/IR/OpDefinition.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/OperationSupport.h" // from @llvm-project +#include "mlir/IR/SymbolTable.h" // from @llvm-project +#include "mlir/IR/Types.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Pass/PassManager.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Tools/mlir-translate/Translation.h" // from @llvm-project +#include "stablehlo/dialect/StablehloOps.h" // from @stablehlo +#include "stablehlo/dialect/VhloOps.h" // from @stablehlo +#include "tensorflow/compiler/mlir/lite/core/absl_error_model_builder.h" +#include "tensorflow/compiler/mlir/lite/experimental/remat/metadata_util.h" +#include "tensorflow/compiler/mlir/lite/flatbuffer_operator.h" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/lite/offset_buffer.h" +#include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_traits.h" +#include "tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.h" +#include "tensorflow/compiler/mlir/lite/schema/mutable/debug_metadata_generated.h" +#include "tensorflow/compiler/mlir/lite/schema/mutable/schema_generated.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_utils.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h" +#include "tensorflow/compiler/mlir/lite/utils/const_tensor_utils.h" +#include "tensorflow/compiler/mlir/lite/utils/control_edges.h" +#include "tensorflow/compiler/mlir/lite/utils/convert_type.h" +#include "tensorflow/compiler/mlir/lite/utils/metadata_utils.h" +#include "tensorflow/compiler/mlir/lite/utils/shape_and_size_utils.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_attributes.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_dialect.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_saved_model.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_types.h" +#include "tensorflow/compiler/mlir/tensorflow/utils/convert_tensor.h" +#include "tensorflow/compiler/mlir/tensorflow/utils/dynamic_shape_utils.h" +#include "tensorflow/compiler/mlir/tensorflow/utils/mangling_util.h" +#include "xla/tsl/platform/errors.h" +#include "xla/tsl/platform/statusor.h" +#include "tensorflow/core/framework/tensor.pb.h" +#include "tensorflow/core/framework/tensor_shape.pb.h" +#include "tensorflow/core/framework/types.pb.h" +#include "tensorflow/core/platform/errors.h" + +using absl::Status; +using absl::StatusOr; +using llvm::ArrayRef; +using mlir::Builder; +using mlir::DenseElementsAttr; +using mlir::Location; +using mlir::MLIRContext; +using mlir::OpBuilder; +using mlir::Operation; +using mlir::OperationState; +using mlir::OwningOpRef; +using mlir::RankedTensorType; +using mlir::Value; +using mlir::func::FuncOp; +using tflite::OperatorT; +using tflite::TensorT; + +namespace errors = tensorflow::errors; +namespace tfl = mlir::TFL; + +namespace { + +constexpr char kScatterRegionFuncName[] = "update_computation_func_name"; + +using ::mlir::tf_saved_model::kTfSavedModelExportedNamesAttr; +using ::mlir::tf_saved_model::kTfSavedModelIndexPathAttr; +using ::tflite::IsValidBufferOffset; + +struct DebugMetadata { + // Debug metadata locations. + std::vector debug_metadata_locations; + + // Maps from operator (subgraph_debug_metadata_idx, + // operator_debug_metadata_idx) to its top-level location index in + // `debug_metadata_locations`, which is: + // <, location_idx>. + absl::flat_hash_map> operator_location_map; +}; + +// Recovers the original operation names from a semicolon-separated string of +// tensor names that were mangled during graph export. +// +// In multi-output operations (e.g., a fused layer or a multi-head attention), +// exporters often append result indices to the base name (e.g., "Layer2" +// becoming "Layer2", "Layer21", "Layer22"). This function groups these +// variations by their common root and selects the shortest variant (the +// presumed original) while maintaining the original sequence of operations. +std::string RecoverOriginalNameFromMangledTensorName( + absl::string_view mangled_name) { + struct OpMetadata { + std::string shortest_name; + size_t first_seen_index; + }; + + std::vector parts = absl::StrSplit(mangled_name, ';'); + absl::flat_hash_map groups; + std::vector order_preserved_roots; + + for (absl::string_view original : parts) { + if (original.empty()) { + order_preserved_roots.push_back(""); + continue; + } + + // Identify the 'root' by stripping trailing digits and common delimiters. + // This allows "Layer2" and "Layer21" to be recognized as the same + // operation. + absl::string_view stem = original; + while (!stem.empty() && std::isdigit(stem.back())) { + stem.remove_suffix(1); + } + if (!stem.empty() && (stem.back() == '.' || stem.back() == '_')) { + stem.remove_suffix(1); + } + + std::string root(stem); + auto it = groups.find(root); + if (it == groups.end()) { + groups[root] = {std::string(original), order_preserved_roots.size()}; + order_preserved_roots.push_back(root); + } else { + // Keep the shortest version found in the group as the canonical name. + if (original.size() < it->second.shortest_name.size()) { + it->second.shortest_name = std::string(original); + } + } + } + + std::vector results; + results.reserve(order_preserved_roots.size()); + for (const auto& root : order_preserved_roots) { + results.push_back(groups[root].shortest_name); + } + + return absl::StrJoin(results, ";"); +} + +// Create the MLIR NamedLoc location corresponding to a given tensor +Location TensorLoc(const TensorT& tensor, Builder builder, Location base) { + if (tensor.name.empty()) { + return base; + } + std::string name = RecoverOriginalNameFromMangledTensorName(tensor.name); + return mlir::NameLoc::get(builder.getStringAttr(name), base); +} + +// Build and return the MLIR location. +StatusOr BuildLocation( + Builder builder, const debug_metadata::Location& location, + const std::vector& debug_metadata_locations, + const absl::flat_hash_map& + attribute_location_idx_map) { + switch (location.location_type()) { + // FileLineColLoc. + case debug_metadata::LocationType_FileLineColLoc: { + auto file_line_col_loc = + static_cast( + location.location()); + return mlir::FileLineColLoc::get( + builder.getContext(), + builder.getStringAttr(file_line_col_loc->filename()->string_view()), + file_line_col_loc->line(), file_line_col_loc->column()); + } + // CallSiteLoc. + case debug_metadata::LocationType_CallSiteLoc: { + auto callsite_loc = + static_cast(location.location()); + if (!attribute_location_idx_map.contains(callsite_loc->callee_index()) || + !attribute_location_idx_map.contains(callsite_loc->caller_index())) { + return absl::InternalError( + "Invalid/corrupt DebugMetadata, expected invariant broken (callee " + "or caller index of a CallSiteLoc is not valid)"); + } + return mlir::CallSiteLoc::get( + debug_metadata_locations[attribute_location_idx_map.at( + callsite_loc->callee_index())], + debug_metadata_locations[attribute_location_idx_map.at( + callsite_loc->caller_index())]); + } + // NameLoc. + case debug_metadata::LocationType_NameLoc: { + auto name_loc = + static_cast(location.location()); + if (!attribute_location_idx_map.contains(name_loc->child_index())) { + return absl::InternalError( + "Invalid/corrupt DebugMetadata, expected invariant broken (child " + "index of a NameLoc is not valid)"); + } + return mlir::NameLoc::get( + builder.getStringAttr(name_loc->name()->string_view()), + debug_metadata_locations[attribute_location_idx_map.at( + name_loc->child_index())]); + } + // FusedLoc. + case debug_metadata::LocationType_FusedLoc: { + auto fused_loc = + static_cast(location.location()); + auto fused_location_indexes = fused_loc->location_indexes(); + std::vector fused_locations; + fused_locations.reserve(fused_location_indexes->size()); + for (int fused_loc_idx = 0; + fused_loc_idx < fused_location_indexes->size(); ++fused_loc_idx) { + if (!attribute_location_idx_map.contains( + fused_location_indexes->Get(fused_loc_idx))) { + return absl::InternalError( + "Invalid/corrupt DebugMetadata, expected invariant broken " + "(location index of a FusedLoc is not valid)"); + } + fused_locations.push_back( + debug_metadata_locations[attribute_location_idx_map.at( + fused_location_indexes->Get(fused_loc_idx))]); + } + return mlir::FusedLoc::get( + fused_locations, mlir::StringAttr::get(builder.getContext(), ""), + builder.getContext()); + } + default: { + return mlir::UnknownLoc::get(builder.getContext()); + } + } +} + +// Parses all locations in ConversionDebugMetadata, build the mlir::location +// counterparts, and put them inside debug_metadata_. Additionally, maintain a +// map that maps the top location index of each operator. +Status ParseAndBuildLocation( + Builder builder, + const debug_metadata::ConversionDebugMetadata* conversion_debug_metadata, + DebugMetadata& debug_metadata_var) { + auto attribute_types = conversion_debug_metadata->attributes_type(); + auto attributes = conversion_debug_metadata->attributes(); + + auto& debug_metadata_locations = debug_metadata_var.debug_metadata_locations; + debug_metadata_locations.reserve(attribute_types->size()); + + // Map index in the attribute_vector to the index in the data structure we + // are building: DebugMetadata::debug_metadata_locations. + absl::flat_hash_map attribute_location_idx_map; + + for (int i = 0; i < attribute_types->size(); ++i) { + if (attribute_types->Get(i) == debug_metadata::Attribute_Location) { + auto location = + static_cast(attributes->Get(i)); + TF_ASSIGN_OR_RETURN( + auto mlir_location, + BuildLocation(builder, *location, debug_metadata_locations, + attribute_location_idx_map)); + debug_metadata_locations.push_back(mlir_location); + + // Create index mapping. + attribute_location_idx_map[i] = debug_metadata_locations.size() - 1; + } + } + + // Collect the top location idx of each operator. + auto subgraphs_debug_metadata = + conversion_debug_metadata->subgraphs_debug_metadata(); + for (int subgraph_idx = 0; subgraph_idx < subgraphs_debug_metadata->size(); + ++subgraph_idx) { + const auto* subgraph_debug_metadata = + subgraphs_debug_metadata->Get(subgraph_idx); + auto operators_debug_metadata = + subgraph_debug_metadata->operators_debug_metadata(); + for (int operator_idx = 0; operator_idx < operators_debug_metadata->size(); + ++operator_idx) { + const auto* operator_debug_metadata = + operators_debug_metadata->Get(operator_idx); + // Find the location attribute of the operator. Note that there should + // be at most one idx pointing to location attribute for each operator. + std::vector location_attribute_idxs; + for (int i = 0; + i < operator_debug_metadata->attribute_metadata_indexes()->size(); + ++i) { + auto attribute_idx = + operator_debug_metadata->attribute_metadata_indexes()->Get(i); + if (attribute_types->Get(attribute_idx) == + debug_metadata::Attribute_Location) { + location_attribute_idxs.push_back(attribute_idx); + } + } + if (location_attribute_idxs.size() > 1) { + return absl::InternalError( + "Invalid/corrupt DebugMetadata, expected invariant broken (more " + "than one location attribute for an operator)"); + } + if (location_attribute_idxs.empty()) { + continue; + } + + if (!attribute_location_idx_map.contains(location_attribute_idxs[0])) { + return absl::InternalError( + "Invalid/corrupt DebugMetadata, expected invariant broken " + "(location attribute index of an operator is not valid)"); + } + debug_metadata_var.operator_location_map[subgraph_idx][operator_idx] = + attribute_location_idx_map[location_attribute_idxs[0]]; + } + } + + return absl::OkStatus(); +} + +// Parse the DebugMetadata flatbuffer and store debug metadata in struct +// `debug_metadata`. +Status ParseDebugMetadata(Builder builder, const char* data, size_t size, + DebugMetadata& debug_metadata_var) { + auto debug_metadata_fb = debug_metadata::GetDebugMetadata(data); + + if (debug_metadata_fb->debug_metadata_type()->size() != + debug_metadata_fb->debug_metadata()->size()) { + return absl::InternalError( + "Invalid/corrupt DebugMetadata, expected invariant broken (size of " + "debug_metadata_type and debug_metadata not equal)"); + } + + for (int i = 0; i < debug_metadata_fb->debug_metadata_type()->size(); ++i) { + if (debug_metadata_fb->debug_metadata_type()->Get(i) == + debug_metadata::DebugMetadataType_ConversionDebugMetadata) { + auto conversion_debug_metadata = + static_cast( + debug_metadata_fb->debug_metadata()->Get(i)); + TF_RETURN_IF_ERROR(ParseAndBuildLocation( + builder, conversion_debug_metadata, debug_metadata_var)); + } else { + LOG(WARNING) << "Unsupported DebugMetadataType: " + << debug_metadata_fb->debug_metadata_type()->Get(i); + } + } + + return absl::OkStatus(); +} + +// Return MLIR location if it exists in the debug metadata. Otherwise, create a +// MLIR location by fusing its output tensor names. +Location OpLoc(const OperatorT& op, Builder builder, + DebugMetadata& debug_metadata, const tflite::SubGraphT& subgraph, + Location base) { + const int subgraph_debug_metadata_idx = subgraph.debug_metadata_index; + + // Check if we have specific debug metadata for this op. + if (debug_metadata.operator_location_map.contains( + subgraph_debug_metadata_idx) && + debug_metadata.operator_location_map.at(subgraph_debug_metadata_idx) + .contains(op.debug_metadata_index)) { + // SCENARIO 1: Debug metadata exists. + // Create the marked wrapper to ensure roundtrip consistency. + int location_idx = + debug_metadata.operator_location_map[subgraph_debug_metadata_idx] + [op.debug_metadata_index]; + Location child_loc = debug_metadata.debug_metadata_locations[location_idx]; + + llvm::SmallVector locations; + if (!op.outputs.empty()) { + locations.reserve(op.outputs.size()); + for (auto tensor_index : op.outputs) { + locations.push_back( + TensorLoc(*subgraph.tensors[tensor_index], builder, child_loc)); + } + } else { + // For ops with no outputs, we can't create a TensorLoc. + // We'll just wrap the original location directly to carry the marker. + mlir::Attribute marker = + builder.getStringAttr(mlir::TFL::kImporterWrapper); + return mlir::FusedLoc::get(builder.getContext(), child_loc, marker); + } + + mlir::Attribute marker = builder.getStringAttr(mlir::TFL::kImporterWrapper); + return mlir::FusedLoc::get(builder.getContext(), locations, marker); + + } else { + // SCENARIO 2: No debug metadata for this op. + // This is the "clean import" path. Create the simple location structure + // without any special wrappers or markers. + if (op.outputs.empty()) return base; + + llvm::SmallVector locations; + locations.reserve(op.outputs.size()); + for (auto tensor_index : op.outputs) { + locations.push_back( + TensorLoc(*subgraph.tensors[tensor_index], builder, base)); + } + return mlir::FusedLoc::get(builder.getContext(), locations); + } +} + +// Extract the min max information in the tensor and create the quant stats op. +// If the input `tensor` has scale/zero_point, `res` should have quantized type, +// thus none stats op is required and nullptr is returned. If the min max +// information is invalid, nullptr is returned. +mlir::Operation* ConvertMinMaxToStatsOp(const TensorT& tensor, OpBuilder b, + Value res) { + // If the `tensor` has scale/zero_point, it must have been quantized, then the + // min/max stats is just for comments, so ignore it. + if (!tensor.quantization || tfl::IsQuantized(tensor)) return nullptr; + // If the result isn't float and unquantizable, the min/max is ignored. + if (!llvm::isa( + llvm::cast(res.getType()).getElementType())) { + return nullptr; + } + auto mins = tensor.quantization->min; + auto maxs = tensor.quantization->max; + if (mins.size() != maxs.size() || mins.empty()) return nullptr; + + llvm::SmallVector min_maxs; + min_maxs.reserve(mins.size() * 2); + for (int i = 0, end = mins.size(); i < end; ++i) { + llvm::APFloat min(mins[i]); + llvm::APFloat max(maxs[i]); + min_maxs.push_back(min); + min_maxs.push_back(max); + } + // The layer stats contain only the first min/max pairs. + mlir::ElementsAttr layer_stats = mlir::DenseFPElementsAttr::get( + tensorflow::GetTypeFromTFTensorShape({2}, b.getF32Type()), + {min_maxs[0], min_maxs[1]}); + mlir::ElementsAttr axis_stats; + mlir::IntegerAttr axis; + if (mins.size() > 1) { + llvm::SmallVector axis_stats_shape{ + static_cast(mins.size()), 2}; + axis_stats = mlir::DenseFPElementsAttr::get( + tensorflow::GetTypeFromTFTensorShape(axis_stats_shape, b.getF32Type()), + min_maxs); + // TODO(fengliuai): this quantization dimension isn't correct. + axis = b.getI64IntegerAttr(tensor.quantization->quantized_dimension); + } + return mlir::quantfork::StatisticsOp::create(b, b.getUnknownLoc(), res, + layer_stats, axis_stats, axis); +} + +// Returns true if this is a basic LSTM op. +bool IsBasicLSTMOp(tflite::BuiltinOptionsUnion op_union) { + if (const auto* op = op_union.AsLSTMOptions()) { + return op->kernel_type == tflite::LSTMKernelType_BASIC; + } else { + return false; + } +} + +// Gets the MLIR op name with the dialect name for the flatbuffer operator. +std::string GetMlirOpName(const tflite::OperatorT& op, + const tflite::OperatorCodeT& op_code) { + if (IsBasicLSTMOp(op.builtin_options)) { + return std::string("tfl.basic_lstm"); + } + return mlir::GetMlirOpNameFromOpCode(op_code); +} + +StatusOr BuildExternalConstOpWithBufferIndex( + const tflite::TensorT& tensor, int32_t buffer_index, OpBuilder builder, + Location loc) { + TF_ASSIGN_OR_RETURN(mlir::TensorType type, + tfl::GetTensorType(tensor, builder, + /*is_constant=*/true)); + auto shaped_type = llvm::dyn_cast(type); + if (!shaped_type) { + return absl::InternalError("Constant doesn't have a shape"); + } + auto op = tfl::ExternalConstOp::create( + builder, loc, shaped_type, + /*buffer_index=*/builder.getI32IntegerAttr(buffer_index), + /*external_buffer=*/nullptr); + return op.getOperation(); +} + +StatusOr BuildExternalConstOpWithExternalBuffer( + const tflite::ModelT& model, const tflite::TensorT& tensor, + OpBuilder builder, Location loc) { + TF_ASSIGN_OR_RETURN(mlir::TensorType type, + tfl::GetTensorType(tensor, builder, + /*is_constant=*/true)); + auto shaped_type = llvm::dyn_cast(type); + if (!shaped_type) { + return absl::InternalError("Constant doesn't have a shape"); + } + + tflite::ExternalBufferT* external_buffer = nullptr; + for (const auto& extbuf : model.external_buffers) { + if (extbuf->id == tensor.external_buffer) { + external_buffer = extbuf.get(); + break; + } + } + if (external_buffer == nullptr) { + return absl::InternalError("External buffer not found"); + } + + std::string group_name = + model.external_buffer_groups[external_buffer->group]->name; + auto op = tfl::ExternalConstOp::create( + builder, loc, shaped_type, /*buffer_index=*/nullptr, + /*external_buffer=*/ + tfl::ExternalBufferAttr::get( + builder.getContext(), + /*group_name=*/builder.getStringAttr(group_name), + /*offset=*/external_buffer->offset, + /*length=*/external_buffer->length, + /*packing=*/builder.getStringAttr(external_buffer->packing))); + return op.getOperation(); +} + +// TODO(b/172664358): Creates a new op instead of reusing constant op. +// Creates a constant op with "tfl.is_variable" attribute to represent stateful +// variable. The function static variable `stateful_variable_idx` is used as a +// unique value for each constant to avoid CSEed. `tensor` is the data structure +// of flatbuffer. `shaped_type` is the ShapedType for the const op. +StatusOr BuildVariableOp(const tflite::TensorT& tensor, + OpBuilder builder, Location loc) { + TF_ASSIGN_OR_RETURN(mlir::TensorType type, + tfl::GetTensorType(tensor, builder, + /*is_constant=*/true)); + auto shaped_type = llvm::dyn_cast(type); + if (!shaped_type) { + return absl::InternalError("Constant doesn't have a shape"); + } + + static int stateful_variable_idx = 0; + mlir::ElementsAttr value = + tfl::GetSplat(shaped_type, stateful_variable_idx++, builder); + if (tfl::IsQuantized(tensor)) { + auto op = tfl::QConstOp::create(builder, loc, + mlir::TypeAttr::get(shaped_type), value); + return op.getOperation(); + } + auto op = tfl::ConstOp::create(builder, loc, value); + op->setAttr("tfl.is_variable", builder.getUnitAttr()); + if (tensor.quantization && !tensor.quantization->min.empty()) { + if (auto stats_op = + ConvertMinMaxToStatsOp(tensor, builder, op.getResult())) { + return stats_op; + } + } + return op.getOperation(); +} + +static StatusOr> ConvertSparseIndexVector( + const tflite::SparseIndexVectorUnion& sparse_index_vector) { + if (sparse_index_vector.type == tflite::SparseIndexVector_Int32Vector) { + return sparse_index_vector.AsInt32Vector()->values; + } else if (sparse_index_vector.type == + tflite::SparseIndexVector_Uint16Vector) { + const auto& inputs = sparse_index_vector.AsUint16Vector()->values; + std::vector outputs(inputs.size()); + std::transform(inputs.begin(), inputs.end(), outputs.begin(), + [](auto x) { return static_cast(x); }); + return outputs; + } else if (sparse_index_vector.type == + tflite::SparseIndexVector_Uint8Vector) { + const auto& inputs = sparse_index_vector.AsUint8Vector()->values; + std::vector outputs(inputs.size()); + std::transform(inputs.begin(), inputs.end(), outputs.begin(), + [](auto x) { return static_cast(x); }); + return outputs; + } else { + return absl::UnimplementedError("Unsupported SparseIndexVector type"); + } +} + +static StatusOr BuildSparseConstOp(const tflite::TensorT& tensor, + llvm::ArrayRef buffer, + OpBuilder& builder, + Location loc) { + TF_ASSIGN_OR_RETURN(mlir::TensorType type, + tfl::GetTensorType(tensor, builder, + /*is_constant=*/true)); + auto shaped_type = llvm::dyn_cast(type); + if (!shaped_type) { + return absl::InternalError("Constant doesn't have a shape"); + } + + TF_ASSIGN_OR_RETURN(type, tfl::GetTensorType(tensor, builder, + /*is_constant=*/true, + /*is_intermediate=*/false, + /*get_storage=*/true)); + auto value_type = mlir::dyn_cast(type); + + tensorflow::TensorProto repr = tfl::ConvertTfliteConstTensor(tensor, buffer); + repr.clear_tensor_shape(); + if (tfl::IsQuantized(tensor)) { + repr.mutable_tensor_shape()->add_dim()->set_size(buffer.size()); + repr.set_dtype(tensorflow::DT_INT8); + } else { + repr.mutable_tensor_shape()->add_dim()->set_size( + buffer.size() / (shaped_type.getElementTypeBitWidth() / CHAR_BIT)); + } + TF_ASSIGN_OR_RETURN(mlir::ElementsAttr compressed_data, + tensorflow::ConvertTensorProto(repr, &builder)); + + const int dim_metadata_size = tensor.sparsity->dim_metadata.size(); + std::vector dim_metadata(dim_metadata_size); + for (int i = 0; i < dim_metadata_size; i++) { + if (tensor.sparsity->dim_metadata[i]->format == + tflite::DimensionType_DENSE) { + dim_metadata[i] = tfl::DimensionMetadataAttr::get( + builder.getContext(), + mlir::TFL::DimensionTypeAttr::get(builder.getContext(), + tfl::DimensionType::DENSE), + tensor.sparsity->dim_metadata[i]->dense_size, {}, {}); + } else if (tensor.sparsity->dim_metadata[i]->format == + tflite::DimensionType_SPARSE_CSR) { + TF_ASSIGN_OR_RETURN( + auto segments, ConvertSparseIndexVector( + tensor.sparsity->dim_metadata[i]->array_segments)); + TF_ASSIGN_OR_RETURN(auto indices, + ConvertSparseIndexVector( + tensor.sparsity->dim_metadata[i]->array_indices)); + dim_metadata[i] = tfl::DimensionMetadataAttr::get( + builder.getContext(), + mlir::TFL::DimensionTypeAttr::get(builder.getContext(), + tfl::DimensionType::SPARSE_CSR), + 0, segments, indices); + } else { + return absl::UnimplementedError("Unsupported dimension metadata type"); + } + } + auto s_param = tfl::SparsityParameterAttr::get( + builder.getContext(), tensor.sparsity->traversal_order, + tensor.sparsity->block_map, dim_metadata); + + std::vector dense_buffer( + value_type.getElementType().getIntOrFloatBitWidth() / CHAR_BIT); + mlir::TypedAttr dummy_value = + mlir::DenseIntOrFPElementsAttr::getFromRawBuffer(value_type, + dense_buffer); + + if (tfl::IsQuantized(tensor)) { + return tfl::SparseQConstOp::create(builder, loc, + mlir::TypeAttr::get(shaped_type), + dummy_value, s_param, compressed_data) + .getOperation(); + } + return tfl::SparseConstOp::create(builder, loc, dummy_value, s_param, + compressed_data) + .getOperation(); +} + +StatusOr BuildConstOp(const tflite::TensorT& tensor, + llvm::ArrayRef buffer, + bool is_variable, OpBuilder builder, + Location loc, bool use_stablehlo_constant) { + if (tensor.sparsity != nullptr) { + return BuildSparseConstOp(tensor, buffer, builder, loc); + } + + if (is_variable) { + return BuildVariableOp(tensor, builder, loc); + } + + TF_ASSIGN_OR_RETURN(mlir::TensorType type, + tfl::GetTensorType(tensor, builder, + /*is_constant=*/true, + /*is_intermediate=*/false, + /*get_storage=*/true)); + auto shaped_type = llvm::dyn_cast(type); + if (!shaped_type) { + return absl::InternalError("Constant doesn't have a shape"); + } + + mlir::ElementsAttr value; + if (tfl::IsQuantized(tensor)) { + bool truncate = shaped_type.getElementType().getIntOrFloatBitWidth() == 64; + TF_ASSIGN_OR_RETURN(value, + tfl::ConvertIntBuffer(shaped_type, buffer, truncate)); + TF_ASSIGN_OR_RETURN( + mlir::quant::QuantizedType type, + tfl::GetQuantizedType(tensor, builder, /*is_constant=*/true, + /*storage_type=*/value.getElementType())); + shaped_type = shaped_type.clone(type); + auto op = tfl::QConstOp::create(builder, loc, + mlir::TypeAttr::get(shaped_type), value); + return op.getOperation(); + } + + auto elem_type = shaped_type.getElementType(); + if (auto float_type = llvm::dyn_cast(elem_type)) { + TF_ASSIGN_OR_RETURN(value, tfl::ConvertFloatBuffer(shaped_type, buffer)); + } else if (llvm::isa(elem_type)) { + TF_ASSIGN_OR_RETURN(value, tfl::ConvertIntBuffer(shaped_type, buffer)); + } else if (llvm::isa(elem_type)) { + tensorflow::TensorProto repr = + tfl::ConvertTfliteConstTensor(tensor, buffer); + std::vector refs; + refs.reserve(repr.string_val_size()); + + for (const auto& ref : repr.string_val()) + refs.push_back({ref.data(), ref.size()}); + + value = mlir::DenseStringElementsAttr::get(shaped_type, refs); + } else if (llvm::isa( + elem_type)) { + tensorflow::TensorProto repr = + tfl::ConvertTfliteConstTensor(tensor, buffer); + std::string mangled = tensorflow::mangling_util::MangleTensor(repr); + + value = mlir::TF::TensorProtoAttr::get(shaped_type, mangled); + } else { + return absl::UnimplementedError("Constant of unsupported type"); + } + + if (use_stablehlo_constant) { + mlir::StablehloVhloTypeConverter vhlo_type_converter; + llvm::ArrayRef val_ref(reinterpret_cast(buffer.data()), + buffer.size()); + auto vhlo_val = mlir::vhlo::TensorV1Attr::get( + builder.getContext(), vhlo_type_converter.convertType(shaped_type), + DenseElementsAttr::getFromRawBuffer(shaped_type, val_ref)); + auto op = + mlir::vhlo::ConstantOpV1::create(builder, loc, shaped_type, vhlo_val); + return op.getOperation(); + } + auto op = tfl::ConstOp::create(builder, loc, value); + return op.getOperation(); +} + +StatusOr> +ConvertSubgraphIdxsToFunctionAttrs(tflite::BuiltinOptionsUnion options, + const std::vector& func_names, + Builder builder) { + if (auto* opts = options.AsCallOnceOptions()) { + uint32_t init_idx = opts->init_subgraph_index; + if (init_idx >= func_names.size()) { + return absl::InvalidArgumentError( + absl::StrCat("subgraph with index not found: ", init_idx)); + } + auto init_attr = builder.getStringAttr(func_names.at(init_idx)); + + return llvm::SmallVector{ + builder.getNamedAttr("session_init_function", init_attr)}; + } + if (auto* opts = options.AsIfOptions()) { + uint32_t then_idx = opts->then_subgraph_index; + if (then_idx >= func_names.size()) { + return absl::InvalidArgumentError( + absl::StrCat("subgraph with index not found: ", then_idx)); + } + auto then_attr = + mlir::SymbolRefAttr::get(builder.getContext(), func_names.at(then_idx)); + uint32_t else_idx = opts->else_subgraph_index; + if (else_idx >= func_names.size()) { + return absl::InvalidArgumentError( + absl::StrCat("subgraph with index not found: ", else_idx)); + } + auto else_attr = + mlir::SymbolRefAttr::get(builder.getContext(), func_names.at(else_idx)); + + return llvm::SmallVector{ + builder.getNamedAttr("then_branch", then_attr), + builder.getNamedAttr("else_branch", else_attr), + // TODO(b/139667752): Analyze statelessness correctly + builder.getNamedAttr("is_stateless", builder.getBoolAttr(false))}; + } + if (auto* opts = options.AsWhileOptions()) { + uint32_t cond_idx = opts->cond_subgraph_index; + if (cond_idx >= func_names.size()) { + return absl::InvalidArgumentError( + absl::StrCat("subgraph with index not found: ", cond_idx)); + } + auto cond_attr = + mlir::SymbolRefAttr::get(builder.getContext(), func_names.at(cond_idx)); + uint32_t body_idx = opts->body_subgraph_index; + if (body_idx >= func_names.size()) { + return absl::InvalidArgumentError( + absl::StrCat("subgraph with index not found: ", body_idx)); + } + auto body_attr = + mlir::SymbolRefAttr::get(builder.getContext(), func_names.at(body_idx)); + + return llvm::SmallVector{ + builder.getNamedAttr("cond", cond_attr), + builder.getNamedAttr("body", body_attr)}; + } + return llvm::SmallVector{}; +} + +Status ConvertSubgraphIdxToStablehloRegion( + const tflite::OperatorT& op, const std::vector& func_names, + Builder builder, OperationState& op_state) { + if (auto* opts = op.builtin_options_2.AsStablehloReduceOptions()) { + int32_t body_idx = opts->body_subgraph_index; + if (body_idx >= func_names.size()) { + return absl::AbortedError("subgraph with index not found: " + + std::to_string(body_idx)); + } + auto body_attr = + mlir::SymbolRefAttr::get(builder.getContext(), func_names.at(body_idx)); + + op_state.addAttribute("body", body_attr); + + return absl::OkStatus(); + } + if (auto* opts = op.builtin_options_2.AsStablehloReduceWindowOptions()) { + int32_t body_idx = opts->body_subgraph_index; + if (body_idx >= func_names.size()) { + return absl::AbortedError("subgraph with index not found: " + + std::to_string(body_idx)); + } + auto body_attr = + mlir::SymbolRefAttr::get(builder.getContext(), func_names.at(body_idx)); + + op_state.addAttribute("body", body_attr); + + return absl::OkStatus(); + } + if (auto* opts = op.builtin_options_2.AsStablehloSortOptions()) { + int32_t comparator_idx = opts->comparator_subgraph_index; + if (comparator_idx >= func_names.size()) { + return absl::AbortedError("subgraph with index not found: " + + std::to_string(comparator_idx)); + } + auto comparator_attr = mlir::SymbolRefAttr::get( + builder.getContext(), func_names.at(comparator_idx)); + + op_state.addAttribute("comparator", comparator_attr); + + return absl::OkStatus(); + } + if (auto* opts = op.builtin_options_2.AsStablehloWhileOptions()) { + int32_t body_idx = opts->body_subgraph_index; + int32_t cond_idx = opts->cond_subgraph_index; + if (body_idx >= func_names.size()) { + return absl::AbortedError("subgraph with index not found: " + + std::to_string(body_idx)); + } + if (cond_idx >= func_names.size()) { + return absl::AbortedError("subgraph with index not found: " + + std::to_string(cond_idx)); + } + auto body_attr = + mlir::SymbolRefAttr::get(builder.getContext(), func_names.at(body_idx)); + auto cond_attr = + mlir::SymbolRefAttr::get(builder.getContext(), func_names.at(cond_idx)); + + op_state.addAttribute("body", body_attr); + op_state.addAttribute("cond", cond_attr); + + return absl::OkStatus(); + } + if (auto* opts = op.builtin_options_2.AsStablehloScatterOptions()) { + uint32_t subgraph_idx = opts->update_computation_subgraph_index; + + if (subgraph_idx >= func_names.size()) { + return absl::AbortedError( + absl::StrCat("subgraph with index not found: ", subgraph_idx)); + } + mlir::FlatSymbolRefAttr subgraph_attr = mlir::SymbolRefAttr::get( + builder.getContext(), func_names.at(subgraph_idx)); + + op_state.addAttribute(kScatterRegionFuncName, subgraph_attr); + + return absl::OkStatus(); + } + if (auto* opts = op.builtin_options_2.AsStablehloCaseOptions()) { + llvm::SmallVector branches; + for (int32_t branch_idx : opts->branch_subgraph_indices) { + if (branch_idx >= func_names.size()) { + return absl::AbortedError("subgraph with index not found: " + + std::to_string(branch_idx)); + } + branches.push_back(mlir::SymbolRefAttr::get(builder.getContext(), + func_names.at(branch_idx))); + } + op_state.addAttribute("branches", builder.getArrayAttr(branches)); + return absl::OkStatus(); + } + // skip if not supported + return absl::OkStatus(); +} + +Status AddOpIntermediatesForLstm( + const tflite::OperatorT& op, + const std::vector& intermediate_types, + OperationState& op_state, Location loc, OpBuilder& builder) { + if (!op.intermediates.empty()) { + if (op.intermediates.size() != 5) { + auto err = absl::InvalidArgumentError( + "operator has intermediate tensors but the number of them is not " + "five."); + return emitError(loc, err.ToString()), err; + } + // Create intermediate value + + const llvm::SmallVector kIntermediateNames = { + "input_to_input_intermediate", "input_to_forget_intermediate", + "input_to_cell_intermediate", "input_to_output_intermediate", + "effective_hidden_scale_intermediate"}; + for (auto type_and_name : + llvm::zip(intermediate_types, kIntermediateNames)) { + mlir::TypeAttr type_attr = + mlir::TypeAttr::get(std::get<0>(type_and_name)); + auto named_attr = + builder.getNamedAttr(std::get<1>(type_and_name), type_attr); + op_state.addAttribute(named_attr.getName(), named_attr.getValue()); + } + } + return absl::OkStatus(); +} + +// TODO(krzysd) Handle function calls +StatusOr ConvertOp( + const tflite::OperatorT& op, const std::vector& vals_map, + const std::vector& intermediate_types, + Value optional_arg_marker, + const std::vector>& op_codes, + const std::vector& func_names, + const std::vector>& tensors, Location loc, + OpBuilder builder, + const std::unique_ptr& model_ptr) { + llvm::SmallVector operands; + llvm::SmallVector outputTypes; + + const tflite::OperatorCodeT& op_code = *op_codes.at(op.opcode_index); + + const std::string op_name = GetMlirOpName(op, op_code); + + OperationState op_state(loc, op_name); + + for (auto input_num : op.inputs) { + if (input_num == -1) { + assert(optional_arg_marker != nullptr); + op_state.addOperands({optional_arg_marker}); + } else { + op_state.addOperands({vals_map.at(input_num)}); + } + } + + for (auto output_num : op.outputs) { + auto& tensor = *tensors.at(output_num); + auto type_or_err = tfl::GetTensorType(tensor, builder); + if (!type_or_err.ok()) { + return emitError(loc, type_or_err.status().ToString()), + type_or_err.status(); + } + auto type = std::move(type_or_err).value(); + + if (op_name == "tfl.quantize") { + // Special case for quantize: return type must also be in qtype attribute + op_state.addAttribute("qtype", mlir::TypeAttr::get(type)); + } else if (op_name == "tfl.reshape" && op_state.operands.size() == 1) { + // Special case for reshape: the second op is optional in the old + // converter and kernel, so we create the second operand, which is + // required by the new converter, from the reshape op's option. + auto new_shape = op.builtin_options.AsReshapeOptions()->new_shape; + auto shape_type = tensorflow::GetTypeFromTFTensorShape( + {static_cast(new_shape.size())}, builder.getIntegerType(32)); + + mlir::SmallVector shape; + for (auto s : new_shape) { + shape.push_back( + builder.getI32IntegerAttr(mlir::TFL::ConvertToTfliteSize(s))); + } + auto output_shape = DenseElementsAttr::get(shape_type, shape); + auto shape_op = tfl::ConstOp::create(builder, loc, output_shape); + op_state.addOperands({shape_op}); + } + + op_state.addTypes({type}); + } + + // While the last several tensors could be optional tensors for a tfl op, the + // number of input operands could vary. Gets the min/max number of operands + // from tflite op name. + // Also, since the above code special-handles the `tfl.reshape` op and add an + // additional input, we put these function block here. + llvm::MinMax input_min_max = mlir::OperandNumbersMinMax(op_name); + int input_max_num = input_min_max.Max; + int op_input_num = op_state.operands.size(); + if (input_max_num != 0 && input_max_num > op_input_num) { + // If the number of current inputs is less than the op definition, fill in + // with `none` value, + llvm::SmallVector none_operands( + input_max_num - op_input_num, + mlir::TFL::NoValueOp::create(builder, loc, builder.getNoneType(), + builder.getUnitAttr())); + op_state.addOperands(ArrayRef(none_operands)); + } + + if (op_name == "tfl.lstm") { + // TODO(b/147587779): add the right region if region is empty. + op_state.addRegion(); + CHECK_OK(AddOpIntermediatesForLstm(op, intermediate_types, op_state, loc, + builder)); + } + if (op_name == "tfl.while") { + // Adds two empty regions for "tfl.while". We will fill the regions after + // creating the callee functions because the "tfl.while" input/output types + // may be different with the callee functions, and the call ops need to sync + // with callee function types. + op_state.addRegion(); + op_state.addRegion(); + } + if (op_name == "tfl.unidirectional_sequence_lstm") { + CHECK_OK(AddOpIntermediatesForLstm(op, intermediate_types, op_state, loc, + builder)); + } + if (op_name == "tfl.reshape") { + // Flattens reshape ops when more than one dimension shape operand is given. + mlir::DenseIntElementsAttr shape_attr; + if (matchPattern(op_state.operands[1], m_Constant(&shape_attr))) { + auto shape_ty = llvm::dyn_cast( + op_state.operands[1].getType()); + if (shape_ty != nullptr && shape_ty.hasRank() && shape_ty.getRank() > 1) { + llvm::SmallVector shape; + int32_t dim_size = 0; + for (const auto& dim : + llvm::enumerate(shape_attr.getValues())) { + shape.push_back(builder.getI32IntegerAttr( + mlir::TFL::ConvertToTfliteSize(dim.value().getSExtValue()))); + ++dim_size; + } + auto shape_type = tensorflow::GetTypeFromTFTensorShape( + {static_cast(dim_size)}, builder.getIntegerType(32)); + auto output_shape = mlir::DenseElementsAttr::get(shape_type, shape); + auto shape_op = tfl::ConstOp::create(builder, loc, output_shape); + op_state.operands[1] = shape_op; + } + } + } + if (op_name == "vhlo.reduce_v1" || op_name == "vhlo.reduce_window_v1" || + op_name == "vhlo.sort_v1" || op_name == "vhlo.scatter_v1") { + op_state.addRegion(); + } + if (op_name == "vhlo.while_v1") { + op_state.addRegion(); + op_state.addRegion(); + } + if (op_name == "vhlo.case_v1") { + if (auto* opts = op.builtin_options_2.AsStablehloCaseOptions()) { + for (int i = 0; i < opts->branch_subgraph_indices.size(); ++i) { + op_state.addRegion(); + } + } + } + + llvm::SmallVector attrs; + auto builtin_code = tflite::GetBuiltinCode(&op_code); + if (builtin_code == tflite::BuiltinOperator_CUSTOM) { + auto status = absl::OkStatus(); + + llvm::ArrayRef custom_options; + + if (IsValidBufferOffset(op.large_custom_options_offset)) { + custom_options = llvm::ArrayRef( + reinterpret_cast(model_ptr->allocation()->base()) + + op.large_custom_options_offset, + op.large_custom_options_size); + } else { + custom_options = op.custom_options; + } + + status = mlir::CustomOptionsToAttributes( + op_code.custom_code, custom_options, builder, loc, &attrs); + if (!status.ok()) { + return emitError(loc, status.ToString()), status; + } + } else { + mlir::BuiltinOptionsToAttributes(op.builtin_options, builder, attrs); + mlir::BuiltinOptions2ToAttributes(op.builtin_options_2, builder, attrs); + } + + if (builtin_code == tflite::BuiltinOperator_STABLEHLO_COMPOSITE) { + auto composite_options = op.builtin_options_2.AsStableHLOCompositeOptions(); + std::string decomposition = ""; + if (composite_options->decomposition_subgraph_index > -1) { + decomposition = + func_names.at(composite_options->decomposition_subgraph_index); + } + + attrs.emplace_back(builder.getNamedAttr( + "decomposition", + mlir::vhlo::StringV1Attr::get(builder.getContext(), decomposition))); + } + + op_state.addAttributes(attrs); + + // Handle the conversion from subgraph index to functions for If and While. We + // will add CallOps in the region to call the functions later for While. + TF_ASSIGN_OR_RETURN(auto function_ref_attrs, + ConvertSubgraphIdxsToFunctionAttrs(op.builtin_options, + func_names, builder)); + op_state.addAttributes(function_ref_attrs); + // Handle conversion from subgraph to regions in StableHLO ops. + auto status = + ConvertSubgraphIdxToStablehloRegion(op, func_names, builder, op_state); + if (!status.ok()) { + return emitError(loc, status.ToString()), status; + } + + return builder.create(op_state); +} + +// Returns indices of the given tensors in the subgraph. Returns error if a +// tensor name cannot be found in the subgraph. +StatusOr> GetTensorIndices( + const tflite::SubGraphT& subgraph, + const std::vector& tensor_names) { + absl::flat_hash_map name_to_index; + for (const auto& index_and_tensor : llvm::enumerate(subgraph.tensors)) { + name_to_index[index_and_tensor.value()->name] = index_and_tensor.index(); + } + + std::vector indices; + indices.reserve(tensor_names.size()); + + for (const auto& name : tensor_names) { + auto found = name_to_index.find(name); + if (found != name_to_index.end()) { + indices.push_back(found->second); + } else { + return absl::InvalidArgumentError( + absl::StrCat("could not find tensor in subgraph: ", name)); + } + } + + return indices; +} + +// Given a list of tensor indices, returns true if any of the tensors have +// non-empty name strings. +bool HasNonEmptyNames(const tflite::SubGraphT& subgraph, + ArrayRef indices) { + return llvm::any_of(indices, [&](int i) { + return i != -1 && !subgraph.tensors.at(i)->name.empty(); + }); +} + +// Given a list of tensor indices, returns a string of concatenated tensor names +// wrapped in a NamedAttribute. +mlir::NamedAttribute BuildTFEntryFunctionAttribute( + const tflite::SubGraphT& subgraph, Builder* builder, + const std::string& name, ArrayRef indices) { + auto tensor_names = llvm::map_range(indices, [&](int i) { + return i == -1 ? "" : subgraph.tensors.at(i)->name; + }); + return builder->getNamedAttr( + name, builder->getStringAttr(llvm::join(tensor_names, ","))); +} + +// Traverses the subgraph from output_indices to input_indices and returns the +// set of ops that are visited. +StatusOr> PruneSubgraph( + const tflite::SubGraphT& subgraph, ArrayRef input_indices, + ArrayRef output_indices) { + // Create a map from tensor index to defining op. + absl::flat_hash_map defining_op; + for (const auto& op : subgraph.operators) { + for (int32_t output : op->outputs) { + if (!llvm::is_contained(input_indices, output)) { + defining_op[output] = op.get(); + } + } + } + + std::vector queue; + for (int32_t output : output_indices) { + if (auto& op = defining_op[output]) { + queue.push_back(op); + } + } + + // Traverse the graph towards inputs. + absl::flat_hash_set visited; + while (!queue.empty()) { + const tflite::OperatorT* op = queue.back(); + queue.pop_back(); + if (!visited.insert(op).second) { + // The node has already been visited. + continue; + } + + for (int32_t input : op->inputs) { + // Input tensor may not have a defining op in case it is a subgraph input + // or a constant tensor. + if (auto& op = defining_op[input]) { + queue.push_back(op); + } + } + } + + return visited; +} + +// We want to adjust the func op according to some cross ops information. +static StatusOr PostProcessFuncOp(FuncOp func) { + OpBuilder builder(func); + // When a quantized constant is imported, its quantization parameter is set + // to be narrow range. Here revert to be the fully range if the user doesn't + // require narrow range. + func.walk([&](tfl::QConstOp cst) { + Value value = cst.getResult(); + Value full_range_const = value; + auto qtype = mlir::quant::UniformQuantizedType::getQuantizedElementType( + value.getType()); + // Only the 8-bit constants are imported with narrow range. + if (!qtype || qtype.getStorageTypeIntegralWidth() != 8 || + !(llvm::isa(qtype) || + llvm::isa(qtype))) { + return; + } + for (auto& use : value.getUses()) { + Operation* user = use.getOwner(); + if (user->hasTrait()) continue; + + auto affine_user = + llvm::dyn_cast(user); + if (affine_user && + affine_user.GetAffineOperandIndex() == use.getOperandNumber() && + affine_user.RequiredNarrowRangeAffineOperand()) + continue; + // Create a fully range quantized constant. + if (full_range_const == value) { + mlir::quant::QuantizedType new_qtype; + if (auto per_axis = + llvm::dyn_cast( + qtype)) { + new_qtype = mlir::quant::UniformQuantizedPerAxisType::get( + per_axis.getFlags(), per_axis.getStorageType(), + per_axis.getExpressedType(), per_axis.getScales(), + per_axis.getZeroPoints(), per_axis.getQuantizedDimension(), + per_axis.getStorageTypeMin() - 1, per_axis.getStorageTypeMax()); + } else if (auto per_tensor = + llvm::dyn_cast( + qtype)) { + new_qtype = mlir::quant::UniformQuantizedType::get( + per_tensor.getFlags(), per_tensor.getStorageType(), + per_tensor.getExpressedType(), per_tensor.getScale(), + per_tensor.getZeroPoint(), per_tensor.getStorageTypeMin() - 1, + per_tensor.getStorageTypeMax()); + } else { + return; // Should not reach here, as it's already checked. + } + auto new_output_type = new_qtype.castFromExpressedType( + mlir::quant::UniformQuantizedType::castToExpressedType( + value.getType())); + builder.setInsertionPointAfter(cst.getOperation()); + auto new_op = tfl::QConstOp::create( + builder, cst.getLoc(), new_output_type, + mlir::TypeAttr::get(new_output_type), cst.getValueAttr()); + full_range_const = new_op.getOutput(); + } + use.set(full_range_const); + } + if (cst.use_empty()) cst.erase(); + }); + return func; +} + +// Helper method that returns the index of the tensor with name 'tensor_name' +// in the list of tensor names 'tensors'. It allows excluding some indices. +int GetTensorIndex(const std::string& tensor_name, + llvm::SmallVector tensors, + const std::set& exclude_indices = {}) { + for (const auto& tensor_index_pair : llvm::enumerate(tensors)) { + if (tensor_index_pair.value() == tensor_name && + exclude_indices.find(tensor_index_pair.index()) == + exclude_indices.end()) + return tensor_index_pair.index(); + } + return -1; +} + +// Helper method that returns list of all strings in a StringAttr identified +// by 'attr_key' and values are separated by a comma. +llvm::SmallVector GetStringsFromAttrWithSeparator( + mlir::DictionaryAttr attr, const std::string& attr_key) { + llvm::SmallVector result; + if (auto str = + llvm::dyn_cast_if_present(attr.get(attr_key))) { + str.getValue().split(result, ',', /*MaxSplit=*/-1, + /*KeepEmpty=*/false); + } + return result; +} + +// Sets signature attributes on the function. +void SetSignature( + FuncOp func, const tflite::SignatureDefT* signature, + const std::vector>& tensors) { + auto* context = func->getContext(); + static const char kEntryFunctionAttributes[] = "tf.entry_function"; + + auto dict_attr = + func->getAttrOfType(kEntryFunctionAttributes); + if (!dict_attr) return; + + // Get Input and output tensor names from attribute. + llvm::SmallVector input_names = + GetStringsFromAttrWithSeparator(dict_attr, /*attr_key=*/"inputs"); + llvm::SmallVector output_names = + GetStringsFromAttrWithSeparator(dict_attr, /*attr_key=*/"outputs"); + + for (const auto& input_pair : llvm::enumerate(signature->inputs)) { + const int arg_index = GetTensorIndex( + tensors[input_pair.value()->tensor_index]->name, input_names); + if (arg_index == -1) { + func->emitWarning("Invalid signature tensors specified."); + return; + } + func.setArgAttr( + arg_index, kTfSavedModelIndexPathAttr, + mlir::ArrayAttr::get(context, {mlir::StringAttr::get( + context, input_pair.value()->name)})); + } + // Multiple signature outputs can refer to the same tensor. Avoid setting + // signature output attribute at the same index by maintaining a set. + std::set seen_indices; + for (const auto& output_pair : llvm::enumerate(signature->outputs)) { + const int arg_index = + GetTensorIndex(tensors[output_pair.value()->tensor_index]->name, + output_names, seen_indices); + if (arg_index == -1) { + func->emitWarning("Invalid signature tensors specified."); + return; + } + func.setResultAttr(arg_index, kTfSavedModelIndexPathAttr, + mlir::ArrayAttr::get( + context, {mlir::StringAttr::get( + context, output_pair.value()->name)})); + seen_indices.insert(arg_index); + } + func->setAttr( + kTfSavedModelExportedNamesAttr, + mlir::ArrayAttr::get( + context, {mlir::StringAttr::get(context, signature->signature_key)})); +} + +// There are control nodes at each end of each control edge. For each of them, +// we store the source vertices of the incoming edges (if any) and the control +// node's output token. To improve testability, we use an ordered set for the +// source vertices. +struct ControlNodeDesc { + std::set incoming; + std::optional outgoing; +}; + +using ControlNodes = llvm::DenseMap; + +// Helper function: After op has been emitted as the MLIR representation of +// a subgraph's operators[op_index], check *control_nodes whether it needs to be +// wrapped in a ControlNode because it's at either end of a control edge from +// the metadata. If it is, wrap it in a ControlNode, store the resulting +// ControlType token in *control_nodes, and return the non-ControlType (i.e., +// tensor) results. If it isn't, just return the original operator's results. +mlir::ResultRange MaybeWrapInControlNode(mlir::Operation* op, + OpBuilder op_builder, int op_index, + Location op_loc, + ControlNodes* control_nodes) { + const ControlNodes::iterator maybe_control_node = + control_nodes->find(op_index); + if (maybe_control_node == control_nodes->end()) { + return op->getResults(); + } + mlir::Region region; + region.push_back(new mlir::Block); + auto saved_pos = op_builder.saveInsertionPoint(); + op_builder.setInsertionPointToEnd(®ion.front()); + mlir::Operation* cloned_op = op_builder.clone(*op); + // Add the yield operation. + mlir::TFL::YieldOp::create(op_builder, op_loc, cloned_op->getResults()); + // Now emit into the function body again. + op_builder.restoreInsertionPoint(saved_pos); + + // The ControlNodeOp depends on all control tokens emitted by the nodes at the + // other end of the incoming edges. Since we're proceding in a valid + // topological order, all lookups of these tokens in + // (*control_nodes)[incoming] should be valid. However, we might (in theory) + // have pruned an operator above, so we only emit values that have been + // populated. + llvm::SmallVector control_tokens; + for (const int incoming : maybe_control_node->second.incoming) { + if (const auto& outgoing = (*control_nodes)[incoming].outgoing; outgoing) { + control_tokens.push_back(*outgoing); + } + } + + // Create the ControlNodeOp. + auto ctrl_op = mlir::TFL::ControlNodeOp::create( + op_builder, op_loc, cloned_op->getResultTypes(), + mlir::TFL::ControlType::get(op->getContext()), control_tokens); + ctrl_op.getBody().takeBody(region); + + // Store the control_token output for use by downstream nodes. + maybe_control_node->second.outgoing = ctrl_op.getControl(); + + // Remove the original op. + op->replaceAllUsesWith(ctrl_op.getOutputs()); + op->erase(); + return ctrl_op.getOutputs(); +} + +// Build a FuncOp from a tflite SubGraph +// The buffers are directly taken +// from the deserialized flatbuffer as we do not have the type information to +// interpret them until this point. The base_loc parameter is the location of +// the flatbuffer as a whole (usually a file). If ordered_output_arrays is not +// empty, then the imported mlir function will only return nodes in +// ordered_output_arrays in the same order. If signature is not null, then the +// inputs/outputs in signature will be attached to the FuncOp. +StatusOr ConvertSubgraph( + const tflite::ModelT& model, const tflite::SubGraphT& subgraph, + llvm::StringRef name, + const std::vector>& op_codes, + const std::vector& func_names, + const std::vector>& buffers, + Location base_loc, Builder builder, bool is_entry_point, + bool use_external_constant, + const std::vector& ordered_input_arrays, + const std::vector& ordered_output_arrays, + bool experimental_prune_unreachable_nodes_unconditionally, + const tflite::SignatureDefT* signature, + const tflite::ControlEdges& control_edges, + const std::unique_ptr& model_ptr, + bool use_stablehlo_constant, DebugMetadata& debug_metadata) { + // Populate from metadata. + ControlNodes control_nodes; + for (const auto [from, to] : control_edges) { + control_nodes.try_emplace(from); + control_nodes[to].incoming.insert(from); + } + + llvm::SmallVector ret_types; + llvm::SmallVector input_types; + + auto func_loc = mlir::NameLoc::get(builder.getStringAttr(name), base_loc); + std::vector func_inputs = subgraph.inputs; + if (is_entry_point && !ordered_input_arrays.empty()) { + if (!experimental_prune_unreachable_nodes_unconditionally) { + // TODO(b/149922113): Resolve input-arrays/pruning flags interaction. + return absl::InvalidArgumentError( + "input-arrays should be used with experimental pruning flag"); + } + TF_ASSIGN_OR_RETURN(func_inputs, + GetTensorIndices(subgraph, ordered_input_arrays)); + } + + for (int input : func_inputs) { + if (input == -1) { + input_types.push_back(builder.getNoneType()); + continue; + } + auto& tensor = *subgraph.tensors.at(input); + auto type_or_err = tfl::GetTensorType(tensor, builder); + if (!type_or_err.ok()) { + emitError(func_loc, "error reading argument types") + << type_or_err.status().ToString(); + return type_or_err.status(); + } + auto type = std::move(type_or_err).value(); + input_types.push_back(type); + } + + llvm::SmallVector is_op_output(subgraph.tensors.size(), false); + for (auto& op : subgraph.operators) { + for (auto output : op->outputs) { + is_op_output[output] = true; + } + } + + std::vector func_outputs = subgraph.outputs; + if (is_entry_point && !ordered_output_arrays.empty()) { + TF_ASSIGN_OR_RETURN(func_outputs, + GetTensorIndices(subgraph, ordered_output_arrays)); + } + + for (auto output : func_outputs) { + if (output == -1) { + ret_types.push_back(builder.getNoneType()); + continue; + } + const bool is_func_input = std::find(func_inputs.begin(), func_inputs.end(), + output) != func_inputs.end(); + bool is_constant = !is_op_output[output] && !is_func_input; + + auto type_or_err = + tfl::GetTensorType(*subgraph.tensors.at(output), builder, is_constant); + if (!type_or_err.ok()) { + emitError(func_loc, "error reading return types") + << type_or_err.status().ToString(); + return type_or_err.status(); + } + auto type = std::move(type_or_err).value(); + ret_types.push_back(type); + } + auto func_type = builder.getFunctionType(input_types, ret_types); + + // Construct function object + auto func = FuncOp::create(func_loc, name, func_type, /* attrs= */ {}); + func.addEntryBlock(); + auto& body = func.getBody(); + OpBuilder op_builder{body}; + + std::vector vals_map(subgraph.tensors.size(), nullptr); + Value maybe_optional_arg_marker = nullptr; + + // Get or construct MLIR values for each input + for (int i = 0, e = func_inputs.size(); i < e; i++) { + auto input_tensor = func_inputs[i]; + if (input_tensor == -1) { + continue; + } + const auto& tensor = *subgraph.tensors.at(input_tensor); + auto loc = TensorLoc(tensor, builder, base_loc); + if (vals_map[input_tensor]) { + auto err = absl::FailedPreconditionError("duplicate input arguments"); + return emitError(loc, err.ToString()), err; + } + Value input_value = func.getArgument(i); + + // If the `tensor` has min/max and doesn't have scale/zero_point + // information, a stats op is created to use the input_value, then the + // `tensor` should be mapped to the result of this new stats op. + if (auto stats_op = + ConvertMinMaxToStatsOp(tensor, op_builder, input_value)) { + vals_map[input_tensor] = stats_op->getResult(0); + } else { + vals_map[input_tensor] = input_value; + } + } + + // Set tf.entry_function attribute + if (is_entry_point) { + llvm::SmallVector attributes; + if (HasNonEmptyNames(subgraph, func_inputs)) { + attributes.push_back(BuildTFEntryFunctionAttribute( + subgraph, &builder, "inputs", func_inputs)); + } + if (HasNonEmptyNames(subgraph, func_outputs)) { + attributes.push_back(BuildTFEntryFunctionAttribute( + subgraph, &builder, "outputs", func_outputs)); + } + if (!attributes.empty()) { + func->setAttr("tf.entry_function", builder.getDictionaryAttr(attributes)); + } + } else { + func.setPrivate(); + } + + // Set signature on function. + if (signature) { + SetSignature(func, signature, subgraph.tensors); + } + + absl::flat_hash_set pruned_subgraph_ops; + if (experimental_prune_unreachable_nodes_unconditionally) { + TF_ASSIGN_OR_RETURN(pruned_subgraph_ops, + PruneSubgraph(subgraph, func_inputs, func_outputs)); + } + + // Construct MLIR operators from TFLite operators + for (const auto& it : llvm::enumerate(subgraph.operators)) { + auto& op = it.value(); + + if (experimental_prune_unreachable_nodes_unconditionally && + !pruned_subgraph_ops.contains(op)) { + continue; + } + + for (auto input_num : op->inputs) { + // The operators in a graph are topologically sorted + // and so if no previous operation has produced a tensor + // it must be a constant. + if (input_num == -1) { + if (maybe_optional_arg_marker == nullptr) { + maybe_optional_arg_marker = + mlir::TFL::NoValueOp::create(op_builder, base_loc, + builder.getNoneType(), + builder.getUnitAttr()) + .getResult(); + } + } else if (!vals_map.at(input_num)) { + auto& const_tensor = *subgraph.tensors[input_num]; + auto const_loc = TensorLoc(const_tensor, builder, base_loc); + StatusOr op_or_err; + llvm::ArrayRef buffer; + // Check if constant tensor is stored outside of the flatbuffers. + if (const_tensor.external_buffer != 0) { + op_or_err = BuildExternalConstOpWithExternalBuffer( + model, const_tensor, op_builder, const_loc); + } else { + if (IsValidBufferOffset(buffers[const_tensor.buffer]->offset)) { + const uint8_t* file_begin_ptr = reinterpret_cast( + model_ptr->allocation()->base()); + + buffer = llvm::ArrayRef( + file_begin_ptr + buffers[const_tensor.buffer]->offset, + buffers[const_tensor.buffer]->size); + } else { + buffer = buffers[const_tensor.buffer]->data; + } + op_or_err = + use_external_constant + ? BuildExternalConstOpWithBufferIndex(const_tensor, + const_tensor.buffer, + op_builder, const_loc) + : BuildConstOp(const_tensor, buffer, const_tensor.is_variable, + op_builder, const_loc, use_stablehlo_constant); + } + + if (!op_or_err.ok()) { + return emitError(const_loc, op_or_err.status().ToString()), + op_or_err.status(); + } + vals_map[input_num] = op_or_err.value()->getResult(0); + } + } + + // Intermediate tensors for LSTMs are used to carry quantization range + // in their types, so we only need and extract their types. + std::vector intermediate_types; + intermediate_types.reserve(5); + for (auto intermediate : op->intermediates) { + TF_ASSIGN_OR_RETURN( + mlir::TensorType type, + tfl::GetTensorType(*subgraph.tensors[intermediate], builder, + /*is_constant=*/false, + /*is_intermediate=*/true)); + intermediate_types.emplace_back(type); + } + + auto op_loc = OpLoc(*op, builder, debug_metadata, subgraph, base_loc); + + // If there's an optional argument, maybe_optional_arg_marker has been set + // to a valid Value + TF_ASSIGN_OR_RETURN( + auto* mlir_op, + ConvertOp(*op, vals_map, intermediate_types, maybe_optional_arg_marker, + op_codes, func_names, subgraph.tensors, op_loc, op_builder, + model_ptr)); + + // Add the results to the value maps. There are two cases: 1. the result + // tensor does not have min/max values, the original op result is used + // directly; 2. the result tensor has some min/max values, a stats op is + // created, then the result of the stats op is used. + for (const auto& pair : llvm::enumerate(MaybeWrapInControlNode( + mlir_op, op_builder, it.index(), op_loc, &control_nodes))) { + int output_tensor_index = op->outputs[pair.index()]; + auto& tensor = *subgraph.tensors[output_tensor_index]; + if (auto stats_op = + ConvertMinMaxToStatsOp(tensor, op_builder, pair.value())) { + vals_map[output_tensor_index] = stats_op->getResult(0); + } else { + vals_map[output_tensor_index] = pair.value(); + } + } + } + + // Construct return values + llvm::SmallVector return_operands; + for (auto index : func_outputs) { + if (index == -1) { + if (maybe_optional_arg_marker == nullptr) { + maybe_optional_arg_marker = + mlir::TFL::NoValueOp::create(op_builder, base_loc, + builder.getNoneType(), + builder.getUnitAttr()) + .getResult(); + } + return_operands.push_back(maybe_optional_arg_marker); + continue; + } + if (!vals_map.at(index)) { + auto& const_tensor = *subgraph.tensors[index]; + auto const_loc = TensorLoc(const_tensor, builder, base_loc); + StatusOr op_or_err; + llvm::ArrayRef buffer; + // Check if constant tensor is stored outside of the flatbuffers. + if (const_tensor.external_buffer != 0) { + op_or_err = BuildExternalConstOpWithExternalBuffer( + model, const_tensor, op_builder, const_loc); + } else { + if (IsValidBufferOffset(buffers[const_tensor.buffer]->offset)) { + const uint8_t* file_begin_ptr = + reinterpret_cast(model_ptr->allocation()->base()); + + buffer = llvm::ArrayRef( + file_begin_ptr + buffers[const_tensor.buffer]->offset, + buffers[const_tensor.buffer]->size); + } else { + buffer = buffers[const_tensor.buffer]->data; + } + op_or_err = + use_external_constant + ? BuildExternalConstOpWithBufferIndex( + const_tensor, const_tensor.buffer, op_builder, const_loc) + : BuildConstOp(const_tensor, buffer, const_tensor.is_variable, + op_builder, const_loc, use_stablehlo_constant); + } + + if (!op_or_err.ok()) { + return emitError(const_loc, op_or_err.status().ToString()), + op_or_err.status(); + } + vals_map[index] = op_or_err.value()->getResult(0); + } + return_operands.push_back(vals_map[index]); + } + + mlir::func::ReturnOp::create(op_builder, base_loc, return_operands); + + return PostProcessFuncOp(func); +} + +// TFLite subgraphs do not necessarily have names, though MLIR functions must +// have them, so we generate a name for subgraphs that are missing one here. +// Note: in TFLite, the first subgraph is the entry point, and in MLIR that +// represents TFLite, this entry point must be called "main" +std::string SubgraphName(bool set_implicit_main_func, unsigned index, + const tflite::SubGraphT& subgraph) { + if (index == 0 && set_implicit_main_func) { + return "main"; + } + if (subgraph.name.empty()) { + return llvm::formatv("fn_{0}", index).str(); + } + return subgraph.name; +} + +// Adds a CallOp in `region` to call the `func` and returns the results of +// CallOp. +void AddCallOpInWhileOpRegion(mlir::Region& region, mlir::func::FuncOp func) { + OpBuilder op_builder{region}; + region.push_back(new mlir::Block()); + Location loc = region.getLoc(); + auto inputs = func.getFunctionType().getInputs(); + region.addArguments(inputs, mlir::SmallVector(inputs.size(), loc)); + op_builder.setInsertionPointToStart(®ion.front()); + auto call_op = mlir::func::CallOp::create( + op_builder, loc, func.getFunctionType().getResults(), func.getSymName(), + region.getArguments()); + mlir::TFL::YieldOp::create(op_builder, loc, call_op.getResults()); +} + +void InlineVhloOpRegion(mlir::Region& region, mlir::func::FuncOp func, + mlir::IRMapping& mapper) { + OpBuilder op_builder{region}; + func.getBody().cloneInto(®ion, mapper); + mlir::Operation& return_op = region.back().back(); + mlir::Location loc = return_op.getLoc(); + op_builder.setInsertionPointToEnd(®ion.back()); + mlir::vhlo::ReturnOpV1::create(op_builder, loc, return_op.getOperands()); + return_op.erase(); +} + +void InlineVhloOpRegion(mlir::Region& region, mlir::func::FuncOp func) { + mlir::IRMapping mapper; + InlineVhloOpRegion(region, func, mapper); +} + +// TFL::WhileOp has regions, so we add CallOp to call the FuncOp in the regions +// if we have while ops. +void AddRegionsForTflWhileOp(mlir::ModuleOp module) { + mlir::SymbolTable symbol_table(module); + module.walk([&](mlir::TFL::WhileOp while_op) { + auto cond = symbol_table.lookup( + llvm::cast(while_op->getAttr("cond")) + .getValue()); + AddCallOpInWhileOpRegion(while_op.getCond(), cond); + while_op->removeAttr("cond"); + auto body = symbol_table.lookup( + llvm::cast(while_op->getAttr("body")) + .getValue()); + AddCallOpInWhileOpRegion(while_op.getBody(), body); + while_op->removeAttr("body"); + }); +} + +void AddRegionsForStableHLOOp(mlir::ModuleOp module) { + mlir::SymbolTable symbol_table(module); + std::vector to_delete_funcs; + module.walk([&](mlir::vhlo::ReduceOpV1 reduce_op) { + auto body = symbol_table.lookup( + llvm::cast(reduce_op->getAttr("body")) + .getValue()); + InlineVhloOpRegion(reduce_op.getBody(), body); + reduce_op->removeAttr("body"); + to_delete_funcs.push_back(body); + }); + module.walk([&](mlir::vhlo::ReduceWindowOpV1 reduce_window_op) { + auto body = symbol_table.lookup( + llvm::cast(reduce_window_op->getAttr("body")) + .getValue()); + InlineVhloOpRegion(reduce_window_op.getBody(), body); + reduce_window_op->removeAttr("body"); + to_delete_funcs.push_back(body); + }); + module.walk([&](mlir::vhlo::ScatterOpV1 scatter_op) { + auto update_computation = symbol_table.lookup( + llvm::cast( + scatter_op->getAttr(kScatterRegionFuncName)) + .getValue()); + InlineVhloOpRegion(scatter_op.getUpdateComputation(), update_computation); + scatter_op->removeAttr(kScatterRegionFuncName); + to_delete_funcs.push_back(update_computation); + }); + module.walk([&](mlir::vhlo::SortOpV1 sort_op) { + auto comparator = symbol_table.lookup( + llvm::cast(sort_op->getAttr("comparator")) + .getValue()); + InlineVhloOpRegion(sort_op.getComparator(), comparator); + sort_op->removeAttr("comparator"); + to_delete_funcs.push_back(comparator); + }); + module.walk([&](mlir::vhlo::CaseOpV1 case_op) { + auto branches = llvm::cast(case_op->getAttr("branches")); + for (int i = 0; i < branches.size(); ++i) { + auto branch_func_name = + llvm::cast(branches[i]).getLeafReference(); + auto branch_func = + symbol_table.lookup(branch_func_name); + + mlir::IRMapping mapper; + for (int j = 0; j < branch_func.getNumArguments(); ++j) { + mapper.map(branch_func.getArgument(j), case_op->getOperand(j + 1)); + } + + InlineVhloOpRegion(case_op.getRegion(i), branch_func, mapper); + if (!case_op.getRegion(i).empty() && + case_op.getRegion(i).front().getNumArguments() > 0) { + case_op.getRegion(i).front().eraseArguments( + [](mlir::BlockArgument arg) { return true; }); + } + to_delete_funcs.push_back(branch_func); + } + case_op->removeAttr("branches"); + while (case_op->getNumOperands() > 1) { + case_op->eraseOperand(1); + } + }); + module.walk([&](mlir::vhlo::WhileOpV1 while_op) { + auto cond = symbol_table.lookup( + llvm::cast(while_op->getAttr("cond")) + .getValue()); + InlineVhloOpRegion(while_op.getCond(), cond); + while_op->removeAttr("cond"); + auto body = symbol_table.lookup( + llvm::cast(while_op->getAttr("body")) + .getValue()); + InlineVhloOpRegion(while_op.getBody(), body); + while_op->removeAttr("body"); + to_delete_funcs.push_back(body); + to_delete_funcs.push_back(cond); + }); + for (auto& func : to_delete_funcs) { + func.erase(); + } +} +} // namespace + +OwningOpRef tflite::FlatBufferToMlir( + absl::string_view buffer, MLIRContext* context, Location base_loc, + bool use_external_constant, + const std::vector& ordered_input_arrays, + const std::vector& ordered_output_arrays, + bool experimental_prune_unreachable_nodes_unconditionally, + bool disable_vhlo_to_stablehlo) { + mlir::DialectRegistry registry; + registry.insert(); + mlir::func::registerAllExtensions(registry); + context->appendDialectRegistry(registry); + + context->loadDialect< + mlir::arith::ArithDialect, mlir::func::FuncDialect, + mlir::quant::QuantDialect, mlir::quantfork::QuantizationForkDialect, + mlir::TFL::TensorFlowLiteDialect, mlir::TF::TensorFlowDialect, + mlir::stablehlo::StablehloDialect, mlir::vhlo::VhloDialect>(); + + auto model_ptr = tfl::FlatBufferModelAbslError::VerifyAndBuildFromBuffer( + buffer.data(), buffer.length()); + if (nullptr == model_ptr) { + return emitError(base_loc, "couldn't parse flatbuffer"), nullptr; + } + + std::unique_ptr model(model_ptr->GetModel()->UnPack()); + + auto builder = Builder(context); + + tflite::ModelControlDependencies model_control_dependencies( + model->subgraphs.size()); + + bool use_stablehlo_constant = false; + + llvm::SmallVector metadata_attrs; + mlir::StringSet<> seen_attr; + DebugMetadata debug_metadata; + for (const auto& metadata : model->metadata) { + if (metadata->name == tflite::kModelControlDependenciesMetadataKey) { + llvm::ArrayRef data(model->buffers[metadata->buffer]->data); + if (!ParseModelControlDependencies( + reinterpret_cast(data.data()), data.size(), + &model_control_dependencies)) { + return emitError(base_loc, + "invalid model_control_dependencies metadata"), + nullptr; + } + continue; + } + + // Skip already seen attributes. Ideally there should be no duplicates here. + if (!seen_attr.try_emplace(metadata->name).second) continue; + + // check if the model is serialized using stablehlo constant tensor + if (metadata->name == tflite::kModelUseStablehloTensorKey) { + use_stablehlo_constant = true; + metadata_attrs.emplace_back(builder.getStringAttr(metadata->name), + builder.getStringAttr("true")); + continue; + } + + if (metadata->name == "debug_metadata") { + llvm::ArrayRef data(model->buffers[metadata->buffer]->data); + auto status = ParseDebugMetadata( + builder, reinterpret_cast(data.data()), data.size(), + debug_metadata); + if (!status.ok()) { + return emitError(base_loc, std::string(status.message())), nullptr; + } + continue; + } + + const auto& buffer = model->buffers[metadata->buffer]->data; + metadata_attrs.emplace_back( + builder.getStringAttr(metadata->name), + builder.getStringAttr(llvm::StringRef( + reinterpret_cast(buffer.data()), buffer.size()))); + } + + std::vector func_names; + for (auto& subgraph : model->subgraphs) { + func_names.push_back(subgraph->name); + } + + auto module = mlir::ModuleOp::create(base_loc); + // We currently don't use this to make decisions, but we could + // use it in exports or if there are breaking changes + module->setAttr("tfl.schema_version", + builder.getI32IntegerAttr(model->version)); + if (!model->description.empty()) { + module->setAttr("tfl.description", + builder.getStringAttr(model->description)); + } + + if (!metadata_attrs.empty()) { + module->setAttr("tfl.metadata", builder.getDictionaryAttr(metadata_attrs)); + } + + if (!model->signature_defs.empty()) { + module->setAttr("tf_saved_model.semantics", + mlir::UnitAttr::get(builder.getContext())); + } + + absl::flat_hash_map + subgraph_to_signature_map; + for (int i = 0; i < model->signature_defs.size(); i++) { + auto* signature_def = model->signature_defs[i].get(); + const uint32_t subgraph_index = signature_def->subgraph_index; + subgraph_to_signature_map[subgraph_index] = signature_def; + } + + const bool set_implicit_main_func = subgraph_to_signature_map.size() <= 1; + for (const auto& e : llvm::enumerate(model->subgraphs)) { + auto& subgraph = e.value(); + std::string name = + SubgraphName(set_implicit_main_func, e.index(), *subgraph); + uint32_t subgraph_index = static_cast(e.index()); + auto func_or_error = ConvertSubgraph( + *model, *subgraph, name, model->operator_codes, func_names, + model->buffers, base_loc, builder, + /*is_entry_point=*/ + set_implicit_main_func + ? e.index() == 0 + : subgraph_to_signature_map.contains(subgraph_index), + /*use_external_constant=*/use_external_constant, ordered_input_arrays, + ordered_output_arrays, + experimental_prune_unreachable_nodes_unconditionally, + subgraph_to_signature_map.contains(subgraph_index) + ? subgraph_to_signature_map.at(subgraph_index) + : nullptr, + model_control_dependencies[subgraph_index], model_ptr, + use_stablehlo_constant, debug_metadata); + if (!func_or_error.ok()) { + return emitError(base_loc, "could not translate function ") + << subgraph->name << ": " << func_or_error.status().message(), + nullptr; + } + module.push_back(std::move(func_or_error).value()); + } + AddRegionsForTflWhileOp(module); + AddRegionsForStableHLOOp(module); + if (!disable_vhlo_to_stablehlo) { + mlir::PassManager pass_manager(module.getContext()); + pass_manager.addPass(mlir::odml::createLegalizeVhloToStablehloPass()); + pass_manager.addPass(mlir::createReconcileUnrealizedCastsPass()); + auto result = pass_manager.run(module); + if (failed(result)) { + return nullptr; + } + } + return OwningOpRef(module); +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_import.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_import.h new file mode 100644 index 0000000000000000000000000000000000000000..ef1ff6fe464c1113f57787ee327b44c910fa3a95 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_import.h @@ -0,0 +1,49 @@ +/* Copyright 2019 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_FLATBUFFER_IMPORT_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_FLATBUFFER_IMPORT_H_ + +#include +#include + +#include "absl/strings/string_view.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/Location.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project + +namespace tflite { +// Converts a TFLite flatbuffer stored in `buffer` to a MLIR module +// The buffer must live for the duration of the function call, +// The caller receives ownership of the module. +// `base_loc` is used for error reporting and debug info. +// If ordered_output_arrays is not empty, then the imported mlir function will +// only return nodes in ordered_output_arrays in the same order. Returns nullptr +// on failure, and more specific errors will be emitted via the context. +// If `use_external_constant` is true, it will create `tfl.external_const` +// instead of `tfl.const`. +// If `experimental_prune_unreachable_nodes_unconditionally` is true, nodes that +// are not ancestors of the output nodes will be pruned. +mlir::OwningOpRef FlatBufferToMlir( + absl::string_view buffer, mlir::MLIRContext* context, + mlir::Location base_loc, bool use_external_constant = false, + const std::vector& ordered_input_arrays = {}, + const std::vector& ordered_output_arrays = {}, + bool experimental_prune_unreachable_nodes_unconditionally = false, + bool disable_vhlo_to_stablehlo = false); +} // namespace tflite + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_FLATBUFFER_IMPORT_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_operator.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_operator.cc new file mode 100644 index 0000000000000000000000000000000000000000..3cbf4fdc9c3c512d0212643361bed84212b96ca2 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_operator.cc @@ -0,0 +1,1020 @@ +/* Copyright 2019 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/compiler/mlir/lite/flatbuffer_operator.h" + +#include +#include +#include +#include +#include +#include + +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/str_cat.h" +#include "flatbuffers/buffer.h" // from @flatbuffers +#include "flatbuffers/flatbuffer_builder.h" // from @flatbuffers +#include "flatbuffers/flatbuffers.h" // from @flatbuffers +#include "flatbuffers/flexbuffers.h" // from @flatbuffers +#include "flatbuffers/string.h" // from @flatbuffers +#include "flatbuffers/vector.h" // from @flatbuffers +#include "llvm/ADT/APFloat.h" +#include "llvm/ADT/APInt.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/StringSwitch.h" +#include "llvm/ADT/Twine.h" +#include "llvm/Support/ErrorHandling.h" +#include "mlir/IR/AttrTypeSubElements.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "stablehlo/dialect/StablehloOps.h" // from @stablehlo +#include "stablehlo/dialect/VhloOps.h" // from @stablehlo +#include "tensorflow/compiler/mlir/lite/core/c/builtin_op_data.h" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/lite/schema/mutable/schema_generated.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_utils.h" +#include "tensorflow/compiler/mlir/lite/utils/convert_type.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_types.h" +#include "tensorflow/compiler/mlir/tensorflow/utils/dynamic_shape_utils.h" +#include "tensorflow/core/platform/errors.h" +#include "tensorflow/core/platform/status.h" +#include "tsl/platform/status.h" + +namespace { + +using ::absl::StatusOr; +using ::tensorflow::Status; +using ::tensorflow::errors::InvalidArgument; + +StatusOr GetPaddingAttr(TfLitePadding pad_params, + mlir::Builder builder, + mlir::Location loc) { + auto padding = tflite::Padding::Padding_VALID; + if (pad_params == TfLitePadding::kTfLitePaddingSame) { + padding = tflite::Padding_SAME; + } else if (pad_params == TfLitePadding::kTfLitePaddingValid) { + padding = tflite::Padding_VALID; + } else { + return absl::InvalidArgumentError( + absl::StrCat("Invalid padding type", std::to_string(pad_params))); + } + + const char* option_name = tflite::EnumNamePadding(padding); + return builder.getStringAttr(option_name); +} + +} // namespace + +bool mlir::IsStablehloOp(const tflite::OperatorCodeT& op_code) { + llvm::StringRef op_name( + tflite::EnumNameBuiltinOperator(tflite::GetBuiltinCode(&op_code))); + return op_name.starts_with("STABLEHLO_"); +} + +std::string mlir::GetMlirOpNameFromOpCode( + const tflite::OperatorCodeT& op_code) { + auto builtin_code = tflite::GetBuiltinCode(&op_code); + if (builtin_code == tflite::BuiltinOperator_IF) { + return std::string("tf.If"); + } + + llvm::StringRef op_name(tflite::EnumNameBuiltinOperator(builtin_code)); + + // If the Op name contains stablehlo + if (IsStablehloOp(op_code)) { + return llvm::Twine( + llvm::Twine("vhlo.", op_name.drop_front(10).lower()).str(), + "_v1") + .str(); + } + return llvm::Twine("tfl.", op_name.lower()).str(); +} + +// TODO(jpienaar): This is a placeholder. This should be done in more efficient +// way when part of the translation of module. +static tflite::ActivationFunctionType ConvertTFL_AFAttrForOptionWriter( + llvm::StringRef str, flatbuffers::FlatBufferBuilder* builder) { + return llvm::StringSwitch(str) + .Case("NONE", tflite::ActivationFunctionType_NONE) + .Case("RELU", tflite::ActivationFunctionType_RELU) + .Case("RELU_N1_TO_1", tflite::ActivationFunctionType_RELU_N1_TO_1) + .Case("RELU6", tflite::ActivationFunctionType_RELU6) + .Case("TANH", tflite::ActivationFunctionType_TANH) + .Case("SIGN_BIT", tflite::ActivationFunctionType_SIGN_BIT); +} + +static tflite::TensorType ConvertDerivedTFLiteTypeAttrForOptionWriter( + tflite::TensorType type, flatbuffers::FlatBufferBuilder* builder) { + if (type == tflite::TensorType_INT64) { + return tflite::TensorType_INT64; + } else if (type == tflite::TensorType_INT32) { + return tflite::TensorType_INT32; + } + llvm_unreachable("invalid type in conversion."); +} + +static tflite::Padding ConvertTFL_PaddingAttrForOptionWriter( + llvm::StringRef str, flatbuffers::FlatBufferBuilder* builder) { + return llvm::StringSwitch(str) + .Case("SAME", tflite::Padding_SAME) + .Case("VALID", tflite::Padding_VALID); +} + +static tflite::MirrorPadMode ConvertTFL_MirrorPaddingAttrForOptionWriter( + mlir::TFL::MirrorPaddingType padding, + flatbuffers::FlatBufferBuilder* builder) { + switch (padding) { + case mlir::TFL::MirrorPaddingType::REFLECT: + return tflite::MirrorPadMode_REFLECT; + case mlir::TFL::MirrorPaddingType::SYMMETRIC: + return tflite::MirrorPadMode_SYMMETRIC; + } + llvm_unreachable("invalid mirror_pad_enum in conversion."); +} + +static tflite::TensorType ConvertDerivedTypeAttrForOptionWriter( + mlir::Type type, flatbuffers::FlatBufferBuilder* builder) { + return tflite::ConvertTypeToTensorType(type); +} + +// I32Attr already returns an int as required by flatbuffer builders. +static int ConvertI32AttrForOptionWriter( + int i, flatbuffers::FlatBufferBuilder* builder) { + return i; +} + +// I64Attr already returns a int64_t as required by flatbuffer builders. +static int64_t ConvertI64AttrForOptionWriter( + int64_t i, flatbuffers::FlatBufferBuilder* builder) { + return i; +} + +static int ConvertPositiveI32AttrForOptionWriter( + int i, flatbuffers::FlatBufferBuilder* builder) { + return ConvertI32AttrForOptionWriter(i, builder); +} + +static flatbuffers::Offset> +ConvertI64ArrayAttrForOptionWriter(mlir::ArrayAttr attrArray, + flatbuffers::FlatBufferBuilder* builder) { + std::vector intVec; + intVec.reserve(attrArray.getValue().size()); + for (auto attr : attrArray.getValue()) { + intVec.push_back(mlir::cast(attr).getInt()); + } + return builder->CreateVector(intVec); +} + +static flatbuffers::Offset> +ConvertF32ArrayAttrForOptionWriter(mlir::ArrayAttr attrArray, + flatbuffers::FlatBufferBuilder* builder) { + std::vector floatVec; + floatVec.reserve(attrArray.getValue().size()); + for (auto attr : attrArray.getValue()) { + floatVec.push_back( + mlir::cast(attr).getValue().convertToFloat()); + } + return builder->CreateVector(floatVec); +} + +// F32Attr already returns a float as required by flatbuffer builders. +static float ConvertF32AttrForOptionWriter( + llvm::APFloat f, flatbuffers::FlatBufferBuilder* builder) { + return f.convertToFloat(); +} + +// BoolAttr already returns a bool as required by flatbuffer builders. +static bool ConvertBoolAttrForOptionWriter( + bool b, flatbuffers::FlatBufferBuilder* builder) { + return b; +} + +// Overloading of ConvertBoolAttrForOptionWriter which takes std::optional +// as an input. If value is not specified, false is set for the attribute. +static bool ConvertBoolAttrForOptionWriter( + std::optional b, flatbuffers::FlatBufferBuilder* builder) { + return b.has_value() ? b.value() : false; +} + +static flatbuffers::Offset ConvertStrAttrForOptionWriter( + llvm::StringRef str, flatbuffers::FlatBufferBuilder* builder) { + return builder->CreateString(str.str()); +} + +static tflite::TensorType ConvertTypeAttrForOptionWriter( + mlir::Type type, flatbuffers::FlatBufferBuilder* builder) { + return tflite::ConvertTypeToTensorType(type); +} + +static flatbuffers::Offset> +ConvertDerivedShapeAttrForOptionWriter( + llvm::ArrayRef r, flatbuffers::FlatBufferBuilder* builder) { + std::vector intVec(r.begin(), r.end()); + return builder->CreateVector(intVec); +} + +static tflite::FullyConnectedOptionsWeightsFormat +ConvertTFL_FullyConnectedOptionsWeightFormatAttrForOptionWriter( + llvm::StringRef str, flatbuffers::FlatBufferBuilder* builder) { + return llvm::StringSwitch(str) + .Case("DEFAULT", tflite::FullyConnectedOptionsWeightsFormat_DEFAULT) + .Case("SHUFFLED4x16INT8", + tflite::FullyConnectedOptionsWeightsFormat_SHUFFLED4x16INT8); +} + +static tflite::LSTMKernelType ConvertTFL_LSTMKernelTypeAttrForOptionWriter( + mlir::TFL::LSTMKernelType kernel_type, + flatbuffers::FlatBufferBuilder* builder) { + switch (kernel_type) { + case mlir::TFL::LSTMKernelType::FULL: + return tflite::LSTMKernelType_FULL; + case mlir::TFL::LSTMKernelType::BASIC: + return tflite::LSTMKernelType_BASIC; + } + llvm_unreachable("invalid lstm_kernel_type in conversion."); +} + +static mlir::Attribute BuildBoolAttr(bool value, mlir::Builder builder) { + return builder.getBoolAttr(value); +} + +static mlir::Attribute BuildStrAttr(llvm::StringRef str, + mlir::Builder builder) { + return builder.getStringAttr(str); +} + +static mlir::Attribute BuildF32Attr(float value, mlir::Builder builder) { + return builder.getF32FloatAttr(value); +} + +static mlir::Attribute BuildI32Attr(int32_t value, mlir::Builder builder) { + return builder.getI32IntegerAttr(value); +} + +static mlir::Attribute BuildI64Attr(int64_t value, mlir::Builder builder) { + return builder.getI64IntegerAttr(value); +} + +static mlir::Attribute BuildI64ArrayAttr(std::vector value, + mlir::Builder builder) { + std::vector typecast(value.begin(), value.end()); + return builder.getI64ArrayAttr(typecast); +} + +static mlir::Attribute BuildVhloBooleanV1Attr(bool value, + mlir::Builder builder) { + return mlir::vhlo::BooleanV1Attr::get(builder.getContext(), value); +} + +static mlir::Attribute BuildVhloIntV1Attr(int64_t value, + mlir::Builder builder) { + mlir::StablehloVhloTypeConverter type_converter; + auto vhlo_type = + type_converter.convertType(builder.getI64IntegerAttr(value).getType()); + return mlir::vhlo::IntegerV1Attr::get(builder.getContext(), vhlo_type, + llvm::APInt(64, value)); +} + +static mlir::Attribute BuildVhloStringV1Attr(llvm::StringRef str, + mlir::Builder builder) { + return mlir::vhlo::StringV1Attr::get(builder.getContext(), str); +} + +static mlir::Attribute BuildVhloArrayV1Attr(std::vector value, + mlir::Builder builder) { + return mlir::vhlo::ArrayV1Attr::get(builder.getContext(), value); +} + +static mlir::Attribute BuildVhloDictionaryV1Attr( + std::vector> value, + mlir::Builder builder) { + return mlir::vhlo::DictionaryV1Attr::get(builder.getContext(), value); +} + +static mlir::Attribute BuildVhloFloatV1Attr(float value, + mlir::Builder builder) { + mlir::StablehloVhloTypeConverter type_converter; + auto vhlo_type = + type_converter.convertType(builder.getF32FloatAttr(value).getType()); + return mlir::vhlo::FloatV1Attr::get(builder.getContext(), vhlo_type, + ::llvm::APFloat(value)); +} + +static mlir::Attribute BuildRankedTensorAttr(std::vector shape, + std::vector value, + mlir::Builder builder) { + // The implementation of getBoolVectorAttr is flawed, so we bypass it here + std::vector extendVec; + extendVec.resize(value.size()); + for (size_t i = 0; i < value.size(); ++i) { + extendVec[i] = llvm::APInt(1, value[i]); + } + mlir::RankedTensorType ty = + tensorflow::GetTypeFromTFTensorShape(shape, builder.getIntegerType(1)); + return mlir::DenseIntElementsAttr::get(ty, extendVec); +} + +static mlir::Attribute BuildRankedTensorAttr(std::vector shape, + std::vector value, + mlir::Builder builder) { + mlir::RankedTensorType ty = + tensorflow::GetTypeFromTFTensorShape(shape, builder.getIntegerType(32)); + return mlir::DenseIntElementsAttr::get(ty, value); +} + +static mlir::Attribute BuildRankedTensorAttr(std::vector shape, + std::vector value, + mlir::Builder builder) { + mlir::RankedTensorType ty = + tensorflow::GetTypeFromTFTensorShape(shape, builder.getIntegerType(64)); + return mlir::DenseIntElementsAttr::get(ty, value); +} + +static mlir::Attribute BuildRankedTensorAttr(std::vector shape, + std::vector value, + mlir::Builder builder) { + mlir::RankedTensorType ty = + tensorflow::GetTypeFromTFTensorShape(shape, builder.getF32Type()); + return mlir::DenseFPElementsAttr::get(ty, value); +} + +static mlir::Attribute BuildVhloTensorV1Attr(std::vector shape, + std::vector value, + mlir::Builder builder) { + mlir::StablehloVhloTypeConverter type_converter; + auto builtin_attr = mlir::dyn_cast( + BuildRankedTensorAttr(shape, value, builder)); + auto vhlo_type = type_converter.convertType(builtin_attr.getType()); + return mlir::vhlo::TensorV1Attr::get(builder.getContext(), vhlo_type, + builtin_attr); +} + +static mlir::Attribute BuildVhloTensorV1Attr(std::vector shape, + std::vector value, + mlir::Builder builder) { + mlir::StablehloVhloTypeConverter type_converter; + auto builtin_attr = mlir::dyn_cast( + BuildRankedTensorAttr(shape, value, builder)); + auto vhlo_type = type_converter.convertType(builtin_attr.getType()); + return mlir::vhlo::TensorV1Attr::get(builder.getContext(), vhlo_type, + builtin_attr); +} + +static mlir::Attribute BuildVhloTensorV1Attr(std::vector shape, + std::vector value, + mlir::Builder builder) { + mlir::StablehloVhloTypeConverter type_converter; + auto builtin_attr = mlir::dyn_cast( + BuildRankedTensorAttr(shape, value, builder)); + auto vhlo_type = type_converter.convertType(builtin_attr.getType()); + return mlir::vhlo::TensorV1Attr::get(builder.getContext(), vhlo_type, + builtin_attr); +} + +static mlir::Attribute BuildVhloTensorV1Attr(std::vector shape, + std::vector value, + mlir::Builder builder) { + mlir::StablehloVhloTypeConverter type_converter; + auto builtin_attr = mlir::dyn_cast( + BuildRankedTensorAttr(shape, value, builder)); + auto vhlo_type = type_converter.convertType(builtin_attr.getType()); + return mlir::vhlo::TensorV1Attr::get(builder.getContext(), vhlo_type, + builtin_attr); +} + +static mlir::Attribute BuildVhloPrecisionConfigV1Attr( + std::vector value, + mlir::Builder builder) { + llvm::SmallVector precision_attrs; + for (size_t i = 0; i < value.size(); ++i) { + precision_attrs.push_back(mlir::vhlo::PrecisionV1Attr::get( + builder.getContext(), + mlir::vhlo::symbolizePrecisionV1(static_cast(value[i])) + .value())); + } + return mlir::vhlo::ArrayV1Attr::get(builder.getContext(), precision_attrs); +} + +static mlir::Attribute BuildF32ArrayAttr(std::vector value, + mlir::Builder builder) { + std::vector typecast(value.begin(), value.end()); + return builder.getF32ArrayAttr(typecast); +} + +static mlir::Attribute BuildPositiveI32Attr(int32_t value, + mlir::Builder builder) { + return builder.getI32IntegerAttr(value); +} + +static mlir::Attribute BuildTypeAttr(tflite::TensorType value, + mlir::Builder builder) { + return mlir::TypeAttr::get(ConvertElementType(value, builder)); +} + +static mlir::Attribute BuildTFL_AFAttr(tflite::ActivationFunctionType value, + mlir::Builder builder) { + const char* option_name = tflite::EnumNameActivationFunctionType(value); + return builder.getStringAttr(option_name); +} + +static mlir::Attribute BuildTFL_FullyConnectedOptionsWeightFormatAttr( + tflite::FullyConnectedOptionsWeightsFormat value, mlir::Builder builder) { + const char* option_name = + tflite::EnumNameFullyConnectedOptionsWeightsFormat(value); + return builder.getStringAttr(option_name); +} + +static mlir::Attribute BuildTFL_LSTMKernelTypeAttr(tflite::LSTMKernelType value, + mlir::Builder builder) { + mlir::TFL::LSTMKernelType kernel_type; + switch (value) { + case tflite::LSTMKernelType_FULL: + kernel_type = mlir::TFL::LSTMKernelType::FULL; + break; + case tflite::LSTMKernelType_BASIC: + kernel_type = mlir::TFL::LSTMKernelType::BASIC; + break; + } + return mlir::TFL::LSTMKernelTypeAttr::get(builder.getContext(), kernel_type); +} + +static mlir::Attribute BuildTFL_MirrorPaddingAttr(tflite::MirrorPadMode value, + mlir::Builder builder) { + mlir::TFL::MirrorPaddingType padding; + switch (value) { + case tflite::MirrorPadMode_REFLECT: + padding = mlir::TFL::MirrorPaddingType::REFLECT; + break; + case tflite::MirrorPadMode_SYMMETRIC: + default: + padding = mlir::TFL::MirrorPaddingType::SYMMETRIC; + break; + } + return mlir::TFL::MirrorPaddingTypeAttr::get(builder.getContext(), padding); +} + +static std::vector BuildAttributeVectorFromFlatbuffer( + flexbuffers::Vector flatbuffer_vector, mlir::Builder builder) { + std::vector mlir_vector; + + for (int i = 0; i < flatbuffer_vector.size(); ++i) { + auto value = flatbuffer_vector[i]; + + if (value.IsBool()) { + mlir_vector.push_back(BuildVhloBooleanV1Attr(value.AsBool(), builder)); + } else if (value.IsString()) { + mlir_vector.push_back( + BuildVhloStringV1Attr(value.AsString().str(), builder)); + } else if (value.IsInt()) { + mlir_vector.push_back(BuildVhloIntV1Attr(value.AsInt64(), builder)); + } else if (value.IsFloat()) { + mlir_vector.push_back(BuildVhloFloatV1Attr(value.AsFloat(), builder)); + } else if (value.IsVector()) { + std::vector nested_mlir_vector = + BuildAttributeVectorFromFlatbuffer(value.AsVector(), builder); + mlir_vector.push_back( + BuildVhloArrayV1Attr(std::move(nested_mlir_vector), builder)); + } + } + + return mlir_vector; +} + +static mlir::Attribute BuildTFL_PaddingAttr(tflite::Padding value, + mlir::Builder builder) { + const char* option_name = tflite::EnumNamePadding(value); + return builder.getStringAttr(option_name); +} + +static mlir::Attribute BuildStablehlo_PrecisionConfigAttr( + std::vector value, + mlir::Builder builder) { + llvm::SmallVector precision_attrs; + for (size_t i = 0; i < value.size(); ++i) { + precision_attrs.push_back(mlir::stablehlo::PrecisionAttr::get( + builder.getContext(), + mlir::stablehlo::symbolizePrecision(static_cast(value[i])) + .value())); + } + return builder.getArrayAttr(precision_attrs); +} + +Status mlir::CustomOptionsToAttributes( + const std::string& custom_code, llvm::ArrayRef custom_options, + mlir::Builder builder, mlir::Location loc, + llvm::SmallVectorImpl* attributes) { + attributes->emplace_back( + builder.getNamedAttr("custom_code", builder.getStringAttr(custom_code))); + std::string content; + content.assign(reinterpret_cast(custom_options.data()), + custom_options.size()); + attributes->emplace_back(builder.getNamedAttr( + "custom_option", + mlir::TFL::ConstBytesAttr::get(builder.getContext(), content))); + + return absl::OkStatus(); +} + +// TODO(zichuanwei@): Populate Builtin_options_2 manual for now, should +// automate these in the future +void BuiltinOptions2ToAttributesManual( + tflite::BuiltinOptions2Union op_union, mlir::Builder builder, + llvm::SmallVectorImpl& attributes) { + if (const auto* op = op_union.AsStablehloConcatenateOptions()) { + attributes.emplace_back(builder.getNamedAttr( + "dimension", BuildVhloIntV1Attr(op->dimension, builder))); + return; + } + if (const auto* op = op_union.AsStablehloBroadcastInDimOptions()) { + attributes.emplace_back(builder.getNamedAttr( + "broadcast_dimensions", + BuildVhloTensorV1Attr( + {static_cast(op->broadcast_dimensions.size())}, + op->broadcast_dimensions, builder))); + return; + } + if (const auto* op = op_union.AsStablehloSliceOptions()) { + std::vector shape = { + static_cast(op->start_indices.size())}; + attributes.emplace_back(builder.getNamedAttr( + "start_indices", + BuildVhloTensorV1Attr(shape, op->start_indices, builder))); + attributes.emplace_back(builder.getNamedAttr( + "limit_indices", + BuildVhloTensorV1Attr(shape, op->limit_indices, builder))); + attributes.emplace_back(builder.getNamedAttr( + "strides", BuildVhloTensorV1Attr(shape, op->strides, builder))); + return; + } + if (const auto* op = op_union.AsStablehloConvolutionOptions()) { + if (!(op->window_strides.empty())) { + std::vector shape; + shape.push_back(static_cast(op->window_strides.size())); + attributes.emplace_back(builder.getNamedAttr( + "window_strides", + BuildVhloTensorV1Attr(shape, op->window_strides, builder))); + } else { + std::vector data(op->input_spatial_dimensions.size(), 1); + std::vector shape; + shape.push_back(static_cast(data.size())); + attributes.emplace_back(builder.getNamedAttr( + "window_strides", BuildVhloTensorV1Attr(shape, data, builder))); + } + if (!(op->padding.empty())) { + std::vector shape; + shape.push_back(static_cast(op->padding.size()) / 2); + shape.push_back(2); + attributes.emplace_back(builder.getNamedAttr( + "padding", BuildVhloTensorV1Attr(shape, op->padding, builder))); + } else { + std::vector data(op->input_spatial_dimensions.size() * 2, 0); + std::vector shape; + shape.push_back(static_cast(data.size()) / 2); + shape.push_back(2); + attributes.emplace_back(builder.getNamedAttr( + "padding", BuildVhloTensorV1Attr(shape, data, builder))); + } + if (!(op->lhs_dilation.empty())) { + std::vector shape; + shape.push_back(static_cast(op->lhs_dilation.size())); + attributes.emplace_back(builder.getNamedAttr( + "lhs_dilation", + BuildVhloTensorV1Attr(shape, op->lhs_dilation, builder))); + } else { + std::vector data(op->input_spatial_dimensions.size(), 1); + std::vector shape; + shape.push_back(static_cast(data.size())); + attributes.emplace_back(builder.getNamedAttr( + "lhs_dilation", BuildVhloTensorV1Attr(shape, data, builder))); + } + if (!(op->rhs_dilation.empty())) { + std::vector shape; + shape.push_back(static_cast(op->rhs_dilation.size())); + attributes.emplace_back(builder.getNamedAttr( + "rhs_dilation", + BuildVhloTensorV1Attr(shape, op->rhs_dilation, builder))); + } else { + std::vector data(op->input_spatial_dimensions.size(), 1); + std::vector shape; + shape.push_back(static_cast(data.size())); + attributes.emplace_back(builder.getNamedAttr( + "rhs_dilation", BuildVhloTensorV1Attr(shape, data, builder))); + } + attributes.emplace_back(builder.getNamedAttr( + "window_reversal", + BuildVhloTensorV1Attr( + {static_cast(op->window_reversal.size())}, + op->window_reversal, builder))); + attributes.emplace_back(builder.getNamedAttr( + "input_batch_dimension", + BuildVhloIntV1Attr(op->input_batch_dimension, builder))); + attributes.emplace_back(builder.getNamedAttr( + "input_feature_dimension", + BuildVhloIntV1Attr(op->input_feature_dimension, builder))); + attributes.emplace_back(builder.getNamedAttr( + "kernel_input_feature_dimension", + BuildVhloIntV1Attr(op->kernel_input_feature_dimension, builder))); + attributes.emplace_back(builder.getNamedAttr( + "kernel_output_feature_dimension", + BuildVhloIntV1Attr(op->kernel_output_feature_dimension, builder))); + attributes.emplace_back(builder.getNamedAttr( + "output_batch_dimension", + BuildVhloIntV1Attr(op->output_batch_dimension, builder))); + attributes.emplace_back(builder.getNamedAttr( + "output_feature_dimension", + BuildVhloIntV1Attr(op->output_feature_dimension, builder))); + attributes.emplace_back(builder.getNamedAttr( + "input_spatial_dimensions", + BuildVhloTensorV1Attr( + {static_cast(op->input_spatial_dimensions.size())}, + op->input_spatial_dimensions, builder))); + attributes.emplace_back(builder.getNamedAttr( + "kernel_spatial_dimensions", + BuildVhloTensorV1Attr( + {static_cast(op->kernel_spatial_dimensions.size())}, + op->kernel_spatial_dimensions, builder))); + attributes.emplace_back(builder.getNamedAttr( + "output_spatial_dimensions", + BuildVhloTensorV1Attr( + {static_cast(op->output_spatial_dimensions.size())}, + op->output_spatial_dimensions, builder))); + attributes.emplace_back(builder.getNamedAttr( + "feature_group_count", + BuildVhloIntV1Attr(op->feature_group_count, builder))); + attributes.emplace_back(builder.getNamedAttr( + "batch_group_count", + BuildVhloIntV1Attr(op->batch_group_count, builder))); + attributes.emplace_back(builder.getNamedAttr( + "precision_config", + BuildVhloPrecisionConfigV1Attr(op->precision_config, builder))); + + return; + } + if (const auto* op = op_union.AsStablehloCustomCallOptions()) { + // hard coding api version for now, we should rework this by updating the + // STABLEHLO_CUSTOM_CALL definition + attributes.emplace_back(builder.getNamedAttr( + "api_version", + mlir::vhlo::CustomCallApiVersionV1Attr::get( + builder.getContext(), + mlir::vhlo::symbolizeCustomCallApiVersionV1(op->api_version) + .value()))); + attributes.emplace_back(builder.getNamedAttr( + "call_target_name", + BuildVhloStringV1Attr(op->call_target_name, builder))); + attributes.emplace_back(builder.getNamedAttr( + "backend_config", BuildVhloStringV1Attr(op->backend_config, builder))); + attributes.emplace_back(builder.getNamedAttr( + "called_computations", BuildVhloArrayV1Attr({}, builder))); + attributes.emplace_back(builder.getNamedAttr( + "operand_layouts", BuildVhloArrayV1Attr({}, builder))); + attributes.emplace_back(builder.getNamedAttr( + "output_operand_aliases", BuildVhloArrayV1Attr({}, builder))); + attributes.emplace_back(builder.getNamedAttr( + "result_layouts", BuildVhloArrayV1Attr({}, builder))); + + std::string has_side_effect_key = "has_side_effect"; + bool has_side_effect_set = false; + const flexbuffers::Map& computation_map = + flexbuffers::GetRoot(op->custom_attributes).AsMap(); + const auto& keys = computation_map.Keys(); + for (size_t i = 0; i < keys.size(); ++i) { + const auto key = keys[i].AsKey(); + const auto& value = computation_map[key]; + if (has_side_effect_key == key) has_side_effect_set = true; + if (value.IsBool()) { + auto attr = value.AsBool(); + auto named_attr = + builder.getNamedAttr(key, BuildVhloBooleanV1Attr(attr, builder)); + attributes.emplace_back(named_attr); + } + if (value.IsString()) { + auto attr = value.AsString(); + auto named_attr = builder.getNamedAttr( + key, BuildVhloStringV1Attr(attr.str(), builder)); + attributes.emplace_back(named_attr); + } + } + if (!has_side_effect_set) + attributes.emplace_back(builder.getNamedAttr( + "has_side_effect", BuildVhloBooleanV1Attr(false, builder))); + return; + } + if (const auto* op = op_union.AsStableHLOCompositeOptions()) { + attributes.emplace_back( + builder.getNamedAttr("name", BuildVhloStringV1Attr(op->name, builder))); + + attributes.emplace_back(builder.getNamedAttr( + "version", BuildVhloIntV1Attr(op->version, builder))); + + auto composite_attribute_pairs = + std::vector>(); + + auto composite_attributes = + flexbuffers::GetRoot(op->composite_attributes).AsMap(); + + const auto& keys = composite_attributes.Keys(); + for (size_t i = 0; i < keys.size(); ++i) { + const auto key = keys[i].AsKey(); + const auto& value = composite_attributes[key]; + + std::pair composite_attribute_pair; + composite_attribute_pair.first = BuildVhloStringV1Attr(key, builder); + + if (value.IsBool()) { + composite_attribute_pair.second = + BuildVhloBooleanV1Attr(value.AsBool(), builder); + } + if (value.IsString()) { + composite_attribute_pair.second = + BuildVhloStringV1Attr(value.AsString().str(), builder); + } + if (value.IsInt()) { + composite_attribute_pair.second = + BuildVhloIntV1Attr(value.AsInt64(), builder); + } + if (value.IsFloat()) { + composite_attribute_pair.second = + BuildVhloFloatV1Attr(value.AsFloat(), builder); + } + + if (value.IsVector()) { + std::vector mlir_vector = + BuildAttributeVectorFromFlatbuffer(value.AsVector(), builder); + + composite_attribute_pair.second = + BuildVhloArrayV1Attr(std::move(mlir_vector), builder); + } + + if (value.IsMap()) { + auto tensor_attr = value.AsMap(); + if (std::string(key).rfind("_TENSOR_V1_", 0) == 0) { + const char* attribute_name = key + 11; // length of "_TENSOR_V1_" + composite_attribute_pair.first = + BuildVhloStringV1Attr(attribute_name, builder); + auto shape = tensor_attr["TENSOR_SHAPE"].AsVector(); + std::vector shape_vector; + shape_vector.reserve(shape.size()); + for (int i = 0; i < shape.size(); ++i) { + shape_vector.push_back(shape[i].AsInt64()); + } + tflite::TensorType type = + tflite::TensorType(tensor_attr["TENSOR_TYPE"].AsInt32()); + auto data = tensor_attr["TENSOR_DATA"].AsVector(); + if (type == tflite::TensorType_INT32) { + std::vector data_vector; + data_vector.reserve(data.size()); + for (int i = 0; i < data.size(); ++i) { + data_vector.push_back(data[i].AsInt32()); + } + composite_attribute_pair.second = + BuildVhloTensorV1Attr(shape_vector, data_vector, builder); + } else if (type == tflite::TensorType_INT64) { + std::vector data_vector; + data_vector.reserve(data.size()); + for (int i = 0; i < data.size(); ++i) { + data_vector.push_back(data[i].AsInt64()); + } + composite_attribute_pair.second = + BuildVhloTensorV1Attr(shape_vector, data_vector, builder); + } else if (type == tflite::TensorType_FLOAT32) { + std::vector data_vector; + data_vector.reserve(data.size()); + for (int i = 0; i < data.size(); ++i) { + data_vector.push_back(data[i].AsFloat()); + } + composite_attribute_pair.second = + BuildVhloTensorV1Attr(shape_vector, data_vector, builder); + } + } + } + + composite_attribute_pairs.emplace_back(composite_attribute_pair); + } + + attributes.emplace_back(builder.getNamedAttr( + "composite_attributes", + BuildVhloDictionaryV1Attr(std::move(composite_attribute_pairs), + builder))); + return; + } + if (const auto* op = op_union.AsStablehloPadOptions()) { + std::vector shape = { + static_cast(op->edge_padding_low.size())}; + attributes.emplace_back(builder.getNamedAttr( + "edge_padding_low", + BuildVhloTensorV1Attr(shape, op->edge_padding_low, builder))); + attributes.emplace_back(builder.getNamedAttr( + "edge_padding_high", + BuildVhloTensorV1Attr(shape, op->edge_padding_high, builder))); + attributes.emplace_back(builder.getNamedAttr( + "interior_padding", + BuildVhloTensorV1Attr(shape, op->interior_padding, builder))); + return; + } + if (const auto* op = op_union.AsStablehloDynamicSliceOptions()) { + attributes.emplace_back(builder.getNamedAttr( + "slice_sizes", + BuildVhloTensorV1Attr({static_cast(op->slice_sizes.size())}, + op->slice_sizes, builder))); + return; + } + if (const auto* op = op_union.AsStablehloCompareOptions()) { + attributes.emplace_back(builder.getNamedAttr( + "comparison_direction", + mlir::vhlo::ComparisonDirectionV1Attr::get( + builder.getContext(), + mlir::vhlo::symbolizeComparisonDirectionV1(op->comparison_direction) + .value()))); + attributes.emplace_back(builder.getNamedAttr( + "compare_type", + mlir::vhlo::ComparisonTypeV1Attr::get( + builder.getContext(), + mlir::vhlo::symbolizeComparisonTypeV1(op->compare_type).value()))); + return; + } + if (const auto* op = op_union.AsStablehloIotaOptions()) { + attributes.emplace_back(builder.getNamedAttr( + "iota_dimension", BuildVhloIntV1Attr(op->iota_dimension, builder))); + return; + } + if (const auto* op = op_union.AsStablehloReduceOptions()) { + attributes.emplace_back(builder.getNamedAttr( + "dimensions", + BuildVhloTensorV1Attr({static_cast(op->dimensions.size())}, + op->dimensions, builder))); + return; + } + if (const auto* op = op_union.AsStablehloReduceWindowOptions()) { + if (!op->window_dimensions.empty()) { + attributes.emplace_back(builder.getNamedAttr( + "window_dimensions", + BuildVhloTensorV1Attr( + {static_cast(op->window_dimensions.size())}, + op->window_dimensions, builder))); + } + if (!op->window_strides.empty()) { + attributes.emplace_back(builder.getNamedAttr( + "window_strides", + BuildVhloTensorV1Attr( + {static_cast(op->window_strides.size())}, + op->window_strides, builder))); + } + if (!op->base_dilations.empty()) { + attributes.emplace_back(builder.getNamedAttr( + "base_dilations", + BuildVhloTensorV1Attr( + {static_cast(op->base_dilations.size())}, + op->base_dilations, builder))); + } + if (!op->window_dilations.empty()) { + attributes.emplace_back(builder.getNamedAttr( + "window_dilations", + BuildVhloTensorV1Attr( + {static_cast(op->window_dilations.size())}, + op->window_dilations, builder))); + } + if (!op->padding.empty()) { + attributes.emplace_back(builder.getNamedAttr( + "padding", BuildVhloTensorV1Attr( + {static_cast(op->padding.size()) / 2, 2}, + op->padding, builder))); + } + return; + } + if (const auto* op = op_union.AsStablehloDotGeneralOptions()) { + attributes.emplace_back(builder.getNamedAttr( + "lhs_batching_dimensions", + BuildVhloTensorV1Attr( + {static_cast(op->lhs_batching_dimensions.size())}, + op->lhs_batching_dimensions, builder))); + attributes.emplace_back(builder.getNamedAttr( + "rhs_batching_dimensions", + BuildVhloTensorV1Attr( + {static_cast(op->rhs_batching_dimensions.size())}, + op->rhs_batching_dimensions, builder))); + attributes.emplace_back(builder.getNamedAttr( + "lhs_contracting_dimensions", + BuildVhloTensorV1Attr( + {static_cast(op->lhs_contracting_dimensions.size())}, + op->lhs_contracting_dimensions, builder))); + attributes.emplace_back(builder.getNamedAttr( + "rhs_contracting_dimensions", + BuildVhloTensorV1Attr( + {static_cast(op->rhs_contracting_dimensions.size())}, + op->rhs_contracting_dimensions, builder))); + attributes.emplace_back(builder.getNamedAttr( + "precision_config", + BuildVhloPrecisionConfigV1Attr(op->precision_config, builder))); + return; + } + if (const auto* op = op_union.AsStablehloSortOptions()) { + attributes.emplace_back(builder.getNamedAttr( + "dimension", BuildVhloIntV1Attr(op->dimension, builder))); + attributes.emplace_back(builder.getNamedAttr( + "is_stable", + mlir::vhlo::BooleanV1Attr::get(builder.getContext(), op->is_stable))); + return; + } + if (const auto* op = op_union.AsStablehloScatterOptions()) { + attributes.emplace_back(builder.getNamedAttr( + "update_window_dims", + BuildVhloTensorV1Attr( + {static_cast(op->update_window_dims.size())}, + op->update_window_dims, builder))); + attributes.emplace_back(builder.getNamedAttr( + "inserted_window_dims", + BuildVhloTensorV1Attr( + {static_cast(op->inserted_window_dims.size())}, + op->inserted_window_dims, builder))); + attributes.emplace_back(builder.getNamedAttr( + "scatter_dims_to_operand_dims", + BuildVhloTensorV1Attr( + {static_cast(op->scatter_dims_to_operand_dims.size())}, + op->scatter_dims_to_operand_dims, builder))); + attributes.emplace_back(builder.getNamedAttr( + "unique_indices", mlir::vhlo::BooleanV1Attr::get(builder.getContext(), + op->unique_indices))); + attributes.emplace_back(builder.getNamedAttr( + "indices_are_sorted", + mlir::vhlo::BooleanV1Attr::get(builder.getContext(), + op->indices_are_sorted))); + attributes.emplace_back(builder.getNamedAttr( + "index_vector_dim", BuildVhloIntV1Attr(op->index_vector_dim, builder))); + return; + } + if (const auto* op = op_union.AsStablehloGatherOptions()) { + attributes.emplace_back(builder.getNamedAttr( + "slice_sizes", + BuildVhloTensorV1Attr({static_cast(op->slice_sizes.size())}, + op->slice_sizes, builder))); + attributes.emplace_back(builder.getNamedAttr( + "collapsed_slice_dims", + BuildVhloTensorV1Attr( + {static_cast(op->collapsed_slice_dims.size())}, + op->collapsed_slice_dims, builder))); + attributes.emplace_back(builder.getNamedAttr( + "start_index_map", + BuildVhloTensorV1Attr( + {static_cast(op->start_index_map.size())}, + op->start_index_map, builder))); + attributes.emplace_back(builder.getNamedAttr( + "index_vector_dim", BuildVhloIntV1Attr(op->index_vector_dim, builder))); + attributes.emplace_back(builder.getNamedAttr( + "offset_dims", + BuildVhloTensorV1Attr({static_cast(op->offset_dims.size())}, + op->offset_dims, builder))); + attributes.emplace_back(builder.getNamedAttr( + "indices_are_sorted", + mlir::vhlo::BooleanV1Attr::get(builder.getContext(), + op->indices_are_sorted))); + return; + } + if (const auto* op = op_union.AsStablehloTransposeOptions()) { + if (!op->permutation.empty()) { + attributes.emplace_back(builder.getNamedAttr( + "permutation", + BuildVhloTensorV1Attr({static_cast(op->permutation.size())}, + op->permutation, builder))); + } + return; + } + if (const auto* op = op_union.AsStablehloRngBitGeneratorOptions()) { + mlir::vhlo::RngAlgorithmV1 algorithm; + switch (op->algorithm) { + case tflite::RngAlgorithm_THREEFRY: + algorithm = mlir::vhlo::RngAlgorithmV1::THREE_FRY; + break; + case tflite::RngAlgorithm_PHILOX: + algorithm = mlir::vhlo::RngAlgorithmV1::PHILOX; + break; + case tflite::RngAlgorithm_DEFAULT: + algorithm = mlir::vhlo::RngAlgorithmV1::DEFAULT; + } + auto attr = + mlir::vhlo::RngAlgorithmV1Attr::get(builder.getContext(), algorithm); + attributes.emplace_back(builder.getNamedAttr("rng_algorithm", attr)); + return; + } +} + +// Pull in FlatBuffer writers for TFLite generated using TableGen +#include "tensorflow/compiler/mlir/lite/operator_converters.inc" diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_operator.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_operator.h new file mode 100644 index 0000000000000000000000000000000000000000..a109d607b9215bc9b96f295a4ea259a9d9316ea3 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_operator.h @@ -0,0 +1,309 @@ +/* Copyright 2019 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_FLATBUFFER_OPERATOR_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_FLATBUFFER_OPERATOR_H_ + +#include + +#include +#include +#include + +#include "absl/container/flat_hash_map.h" +#include "absl/status/status.h" +#include "flatbuffers/flatbuffers.h" // from @flatbuffers +#include "llvm/ADT/SmallVector.h" +#include "llvm/Analysis/AssumeBundleQueries.h" +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "stablehlo/dialect/StablehloOps.h" // from @stablehlo +#include "stablehlo/dialect/VhloOps.h" // from @stablehlo +#include "stablehlo/dialect/VhloTypes.h" // from @stablehlo +#include "tensorflow/compiler/mlir/lite/schema/mutable/schema_generated.h" +#include "tensorflow/core/platform/status.h" +#include "tensorflow/core/platform/statusor.h" + +namespace mlir { + +// duplicated from +// https://github.com/openxla/stablehlo/blob/e5ad51715a11721c78b6748ab5de7945df24b1b8/stablehlo/transforms/StablehloLegalizeToVhlo.cpp#L756 +// so we can create correct vhlo types +class StablehloVhloTypeConverter : public mlir::vhlo::VhloTypeConverter { + public: + StablehloVhloTypeConverter() : mlir::vhlo::VhloTypeConverter() { + addConversion([](mlir::Type type) -> mlir::Type { + if (type.getDialect().getNamespace() == + mlir::vhlo::VhloDialect::getDialectNamespace()) { + return type; + } + return {}; + }); + addConversion([](mlir::stablehlo::TokenType token) -> mlir::Type { + return mlir::vhlo::TokenV1Type::get(token.getContext()); + }); + addBuiltinToVhloConversions(); + } + + mlir::Attribute convertEncoding(mlir::Attribute attr) const final { + // Must be VHLO encoding, or convertible to VHLO encoding. + if (attr.getDialect().getNamespace() == + mlir::vhlo::VhloDialect::getDialectNamespace()) + return attr; + + if (auto stablehloAttr = + mlir::dyn_cast_or_null(attr)) { + return mlir::vhlo::TypeExtensionsV1Attr::get(stablehloAttr.getContext(), + stablehloAttr.getBounds()); + } + + // Was not VHLO encoding, or convertible. + return {}; + } +}; + +// from +// https://github.com/openxla/stablehlo/blob/e5ad51715a11721c78b6748ab5de7945df24b1b8/stablehlo/transforms/VhloLegalizeToStablehlo.cpp#L45C70-L45C70 +class VhloToStablehloTypeConverter : public vhlo::VhloTypeConverter { + public: + VhloToStablehloTypeConverter() : vhlo::VhloTypeConverter() { + addConversion([](Type type) -> Type { return type; }); + addConversion([](vhlo::TokenV1Type token) -> Type { + return stablehlo::TokenType::get(token.getContext()); + }); + addVhloToBuiltinConversions(); + } + + Attribute convertEncoding(Attribute attr) const final { + if (auto vhloAttr = + mlir::dyn_cast_or_null(attr)) { + return stablehlo::TypeExtensionsAttr::get(vhloAttr.getContext(), + vhloAttr.getBounds()); + } + // All encodings supported in StableHLO. + return attr; + } +}; + +// Returns true if the op_code belongs to a stablehlo operation. +bool IsStablehloOp(const tflite::OperatorCodeT& op_code); + +// Returns the MLIR op name for the flatbuffer operator corresponding to +// `op_code`. +std::string GetMlirOpNameFromOpCode(const ::tflite::OperatorCodeT& op_code); + +// Returns the builtin op code for the given MLIR operation on success; emits +// error and returns std::nullopt on failure. +std::optional GetBuiltinOpCode(Operation* mlir_op); + +// Packs the given MLIR operation into a TFLite FlatBuffer operator object. +// Returns the FlatBuffer offset for the operator on success; emits error and +// returns std::nullopt on failure. +std::optional> CreateFlatBufferOperator( + Operation* mlir_op, uint32_t opcode_index, + const std::vector& operands, const std::vector& results, + const std::vector& intermediates, + flatbuffers::FlatBufferBuilder* fbb, + std::optional debug_metadata_index = -1); + +// Populates the array of mlir::NamedAttributes corresponding to the given +// tflite::FlatbufferOptionsUnion. +// We use an out parameter per LLVM convention +void BuiltinOptionsToAttributes( + tflite::BuiltinOptionsUnion op_union, mlir::Builder builder, + // NOLINTNEXTLINE + llvm::SmallVectorImpl& attributes); + +// While the last several tensors could be optional tensors for an tfl op, the +// number of input operands could vary. This function gets the min/max number of +// operands from tflite op name. +llvm::MinMax OperandNumbersMinMax(llvm::StringRef op_name); + +// Populates the `custom_code` and `custom_options` to attributes. +// `custom_code` is used to identify CustomOp. +// `custom_options` are opaque attribute used to store infomations for this +// custom op. +absl::Status CustomOptionsToAttributes( + const std::string& custom_code, llvm::ArrayRef custom_options, + mlir::Builder builder, + // NOLINTNEXTLINE + Location loc, llvm::SmallVectorImpl* attributes); + +// TODO(zichuanwei@): Populate Builtin_options_2 manual for now, should automate +// these in the future +void BuiltinOptions2ToAttributes( + tflite::BuiltinOptions2Union op_union, mlir::Builder builder, + llvm::SmallVectorImpl& attributes); + +// Function calls with a non-specialized type will result to a linker error. +template +inline std::vector GetVector(DenseElementsAttr elements); + +// TODO(zichuanwei@): for each type, we need to make sure the element type +// matches the expected type otherwise an error should be thrown, but for now +// we're just returning empty vector +template <> +inline std::vector GetVector(DenseElementsAttr elements) { + auto type = elements.getType(); + auto elemType = type.getElementType(); + if (elemType.isSignlessInteger(1)) { + auto vec = llvm::to_vector( + llvm::map_range(elements.getValues(), + [&](bool value) -> uint8_t { return value ? 1 : 0; })); + return std::vector(vec.begin(), vec.end()); + } + + return std::vector(); +} + +template <> +inline std::vector GetVector(DenseElementsAttr elements) { + auto type = elements.getType(); + auto elemType = type.getElementType(); + if (elemType.isSignlessInteger(8)) { + auto vec = llvm::to_vector(llvm::map_range( + elements.getValues(), + [&](APInt value) -> int8_t { return value.getSExtValue(); })); + return std::vector(vec.begin(), vec.end()); + } + + return std::vector(); +} + +template <> +inline std::vector GetVector(DenseElementsAttr elements) { + auto type = elements.getType(); + auto elemType = type.getElementType(); + if (elemType.isSignlessInteger(16)) { + auto vec = llvm::to_vector(llvm::map_range( + elements.getValues(), + [&](APInt value) -> int16_t { return value.getSExtValue(); })); + return std::vector(vec.begin(), vec.end()); + } + + return std::vector(); +} + +template <> +inline std::vector GetVector(DenseElementsAttr elements) { + auto type = elements.getType(); + auto elemType = type.getElementType(); + if (elemType.isSignlessInteger(32)) { + auto vec = llvm::to_vector(llvm::map_range( + elements.getValues(), + [&](APInt value) -> int32_t { return value.getSExtValue(); })); + return std::vector(vec.begin(), vec.end()); + } + + return std::vector(); +} + +template <> +inline std::vector GetVector(DenseElementsAttr elements) { + auto type = elements.getType(); + auto elemType = type.getElementType(); + if (elemType.isSignlessInteger(64)) { + auto vec = llvm::to_vector(llvm::map_range( + elements.getValues(), + [&](APInt value) -> int64_t { return value.getSExtValue(); })); + return std::vector(vec.begin(), vec.end()); + } + + return std::vector(); +} + +template <> +inline std::vector GetVector(DenseElementsAttr elements) { + auto type = elements.getType(); + auto elemType = type.getElementType(); + if (elemType.isSignlessInteger(64)) { + auto vec = llvm::to_vector(llvm::map_range( + elements.getValues(), + [&](APInt value) -> uint64_t { return value.getSExtValue(); })); + return std::vector(vec.begin(), vec.end()); + } + + return std::vector(); +} + +template <> +inline std::vector GetVector(DenseElementsAttr elements) { + auto type = elements.getType(); + auto elemType = type.getElementType(); + if (elemType.isF32()) { + auto vec = llvm::to_vector(llvm::map_range( + elements.getValues(), + [&](APFloat value) -> float { return value.convertToFloat(); })); + return std::vector(vec.begin(), vec.end()); + } + + return std::vector(); +} + +template <> +inline std::vector GetVector(DenseElementsAttr elements) { + auto type = elements.getType(); + auto elemType = type.getElementType(); + if (elemType.isF64()) { + auto vec = llvm::to_vector(llvm::map_range( + elements.getValues(), + [&](APFloat value) -> double { return value.convertToFloat(); })); + return std::vector(vec.begin(), vec.end()); + } + + return std::vector(); +} + +// Handles the case when the DenseElementsAttr doesn't exist, and when it +// doesn't returns a vector of length `default_size` all with the same value +// `default_value`. +template +static inline std::vector GetOptionalVector( + std::optional elements, int64_t default_size = 0, + int64_t default_value = 0) { + if (elements.has_value()) { + return GetVector(elements.value()); + } + return std::vector(default_size, default_value); +} + +// Handles the case when the ArrayRef doesn't exist, and when it +// doesn't returns a vector of length `default_size` all with the same value +// `default_value`. +template +static inline std::vector GetOptionalVector( + std::optional> values, int64_t default_size = 0, + int64_t default_value = 0) { + if (values.has_value()) { + return std::vector(values->begin(), values->end()); + } + return std::vector(default_size, default_value); +} + +template +static inline std::vector GetVector( + vhlo::TensorV1Attr elements, + mlir::vhlo::VhloTypeConverter& vhlo_type_converter) { + return GetOptionalVector(mlir::DenseIntElementsAttr::getFromRawBuffer( + mlir::cast( + vhlo_type_converter.convertType(elements.getType())), + elements.getData().getRawData())); +} + +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_FLATBUFFER_OPERATOR_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_to_string.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_to_string.cc new file mode 100644 index 0000000000000000000000000000000000000000..b3cf563e37c7c63877cdfbd818c5b2ed769f0189 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_to_string.cc @@ -0,0 +1,152 @@ +/* Copyright 2019 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. +==============================================================================*/ + +// Dumps a TFLite flatbuffer to a textual output format. +// This tool is intended to be used to simplify unit testing/debugging. + +#include +#include + +#include +#include +#include + +#include "flatbuffers/flatbuffers.h" // from @flatbuffers +#include "flatbuffers/minireflect.h" // from @flatbuffers +#include "tensorflow/compiler/mlir/lite/schema/reflection/schema_generated.h" +#if FLATBUFFERS_LITTLEENDIAN == 0 +#include "tensorflow/compiler/mlir/lite/core/absl_error_model_builder.h" +#endif + +namespace tflite { +namespace { + +// Reads a model from a provided file path and verifies if it is a valid +// flatbuffer, and returns false with the model in serialized_model if valid +// else true. +bool ReadAndVerify(const std::string& file_path, + std::string* serialized_model) { + if (file_path == "-") { + *serialized_model = std::string{std::istreambuf_iterator(std::cin), + std::istreambuf_iterator()}; + } else { + std::ifstream t(file_path); + if (!t.is_open()) { + std::cerr << "Failed to open input file.\n"; + return true; + } + *serialized_model = std::string{std::istreambuf_iterator(t), + std::istreambuf_iterator()}; + } + + flatbuffers::Verifier model_verifier( + reinterpret_cast(serialized_model->c_str()), + serialized_model->length()); + if (!model_verifier.VerifyBuffer()) { + std::cerr << "Verification failed.\n"; + return true; + } + return false; +} + +// A FlatBuffer visitor that outputs a FlatBuffer as a string with proper +// indention for sequence fields. +// TODO(wvo): ToStringVisitor already has indentation functionality, use +// that directly instead of this sub-class? +struct IndentedToStringVisitor : flatbuffers::ToStringVisitor { + std::string indent_str; + int indent_level; + + IndentedToStringVisitor(const std::string& delimiter, + const std::string& indent) + : ToStringVisitor(delimiter), indent_str(indent), indent_level(0) {} + + void indent() { + for (int i = 0; i < indent_level; ++i) s.append(indent_str); + } + + // Adjust indention for fields in sequences. + + void StartSequence() override { + s += "{"; + s += d; + ++indent_level; + } + + void EndSequence() override { + s += d; + --indent_level; + indent(); + s += "}"; + } + + void Field(size_t /*field_idx*/, size_t set_idx, + flatbuffers::ElementaryType /*type*/, bool /*is_vector*/, + const flatbuffers::TypeTable* /*type_table*/, const char* name, + const uint8_t* val) override { + if (!val) return; + if (set_idx) { + s += ","; + s += d; + } + indent(); + if (name) { + s += name; + s += ": "; + } + } + + void StartVector() override { s += "[ "; } + void EndVector() override { s += " ]"; } + + void Element(size_t i, flatbuffers::ElementaryType /*type*/, + const flatbuffers::TypeTable* /*type_table*/, + const uint8_t* /*val*/) override { + if (i) s += ", "; + } +}; + +void ToString(const std::string& serialized_model) { + IndentedToStringVisitor visitor(/*delimiter=*/"\n", /*indent=*/" "); + IterateFlatBuffer(reinterpret_cast(serialized_model.c_str()), + ModelTypeTable(), &visitor); + std::cout << visitor.s << "\n\n"; +} + +} // end namespace +} // end namespace tflite + +int main(int argc, char** argv) { + if (argc < 2) { + std::cerr << "Missing input argument. Usage:\n" + << argv[0] << " \n\n" + << "Converts TensorFlowLite flatbuffer to textual output format. " + << "One positional input argument representing the source of the " + << "flatbuffer is supported.\n"; + return 1; + } + + std::string serialized_model; + if (tflite::ReadAndVerify(argv[1], &serialized_model)) return 1; +#if FLATBUFFERS_LITTLEENDIAN == 0 + // If the flatbuffer model comes from stdin, convert its tensor content from + // BE to LE to ensure the output text string is the same as on LE platforms. + if (std::string(argv[1]) == "-") + mlir::TFL::FlatBufferModelAbslError::ByteSwapSerializedModel( + &serialized_model, true); +#endif + tflite::ToString(serialized_model); + return 0; +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_translate.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_translate.cc new file mode 100644 index 0000000000000000000000000000000000000000..adf3ff6a35109837000a912bde67e4ad96ec9f94 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_translate.cc @@ -0,0 +1,251 @@ +/* Copyright 2019 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 "absl/strings/string_view.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/FormatVariadic.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/SourceMgr.h" +#include "llvm/Support/ToolOutputFile.h" +#include "llvm/Support/raw_ostream.h" +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Dialect/Quant/IR/Quant.h" // from @llvm-project +#include "mlir/Dialect/Quant/IR/QuantTypes.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/Location.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/Types.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Tools/mlir-translate/Translation.h" // from @llvm-project +#include "stablehlo/dialect/StablehloOps.h" // from @stablehlo +#include "stablehlo/dialect/VhloOps.h" // from @stablehlo +#include "tensorflow/compiler/mlir/lite/flatbuffer_export.h" +#include "tensorflow/compiler/mlir/lite/flatbuffer_import.h" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.h" +#include "tensorflow/compiler/mlir/tensorflow/dialect_registration.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" +#include "tensorflow/compiler/mlir/tensorflow/translate/mlir_roundtrip_flags.h" +#include "tensorflow/compiler/mlir/tensorflow/translate/tools/parsers.h" + +using llvm::cl::opt; + +// Commandline flag to enable the control of flatbuffer import. +bool use_external_constant; + +// Commandline flag to enable graph pruning. +bool experimental_prune_unreachable_nodes_unconditionally; + +// NOLINTNEXTLINE +static opt use_external_constant_flag( + "use-external-constant", + llvm::cl::desc("Use external constant during flatbuffer import"), + llvm::cl::location(use_external_constant), llvm::cl::init(false)); + +// TODO(b/147111261): After the importer supports generic custom ops, we should +// change the flag to a more lightwise flag, e.g. +// "import_custom_ops_as_side_effect_free_ops", and let the MLIR DCE to prune +// the operations. +// NOLINTNEXTLINE +static opt experimental_prune_unreachable_nodes_unconditionally_flg( + "experimental-prune-unreachable-nodes-unconditionally", + llvm::cl::desc("Prune nodes that are not ancestors of the output nodes."), + llvm::cl::location(experimental_prune_unreachable_nodes_unconditionally), + llvm::cl::init(false)); + +// NOLINTNEXTLINE +static opt input_arrays_flag( + "input-arrays", + llvm::cl::desc( + "List of input tensors, if different from the default inputs"), + llvm::cl::init("")); + +// NOLINTNEXTLINE +static opt output_arrays_flag( + "output-arrays", + llvm::cl::desc( + "List of output tensors, if different from the default outputs"), + llvm::cl::init("")); + +using llvm::cl::opt; + +// These command line flags enable control of the translation implementation. +bool emit_builtin_tflite_ops; +bool emit_custom_ops; +bool emit_select_tf_ops; +bool lower_tensor_list_ops; +bool strip_debug_info; +bool use_buffer_offset; +bool emit_stablehlo_ops; +bool disable_vhlo_to_stablehlo; +bool serialize_debug_metadata; + +// NOLINTNEXTLINE +static opt emit_builtin_tflite_ops_flag( + "emit-builtin-tflite-ops", + llvm::cl::desc( + "Emit TFLite built in operations in the generated TFLite model"), + llvm::cl::location(emit_builtin_tflite_ops), llvm::cl::init(true)); + +// NOLINTNEXTLINE +static opt emit_select_tf_ops_flag( + "emit-select-tf-ops", + llvm::cl::desc( + "Emit Select TF operations (Flex ops) in the generated TFLite model"), + llvm::cl::location(emit_select_tf_ops), llvm::cl::init(false)); + +// NOLINTNEXTLINE +static opt emit_custom_ops_flag( + "emit-custom-ops", + llvm::cl::desc("Emit Custom operations in the generated TFLite model"), + llvm::cl::location(emit_custom_ops), llvm::cl::init(false)); + +// NOLINTNEXTLINE +static opt lower_tensor_list_ops_flag( + "lower-tensor-list-ops", + llvm::cl::desc("Lower the TensorList ops within the TFLite dialect"), + llvm::cl::location(lower_tensor_list_ops), llvm::cl::init(false)); + +// NOLINTNEXTLINE +static opt strip_debug_info_flag( + "strip-debug-info", llvm::cl::desc("Strip debug info during export"), + llvm::cl::location(strip_debug_info), llvm::cl::init(false)); + +// NOLINTNEXTLINE +static opt use_buffer_offset_flag( + "use-buffer-offset", + llvm::cl::desc("store constant buffers outside of Flatbuffers"), + llvm::cl::location(use_buffer_offset), llvm::cl::init(false)); + +// NOLINTNEXTLINE +static opt emit_stablehlo_ops_flag( + "emit-stablehlo-ops", + llvm::cl::desc("Whether serialize stablehlo ops or not"), + llvm::cl::location(emit_stablehlo_ops), llvm::cl::init(false)); + +// Flatbuffer import by default will also perform vhlo to stablehlo legalization +// to hide serialization detail from user, but for debug purpose we need to be +// able to dump raw vhlo ops as well +// NOLINTNEXTLINE +static opt disable_vhlo_to_stablehlo_flag( + "disable-vhlo-to-stablehlo", + llvm::cl::desc("Whether to deserialize to stablehlo ops or not"), + llvm::cl::location(disable_vhlo_to_stablehlo), llvm::cl::init(false)); + +// NOLINTNEXTLINE +static opt serialize_debug_metadata_flag( + "serialize-debug-metadata", + llvm::cl::desc("Whether to serialize debug metadata or not"), + llvm::cl::location(serialize_debug_metadata), llvm::cl::init(false)); + +// NOLINTNEXTLINE +static opt disable_buffer_deduping_flag( + "disable-buffer-deduping", + llvm::cl::desc("Whether to disable buffer deduping or not"), + llvm::cl::init(false)); + +namespace mlir { +namespace { +static OwningOpRef FlatBufferFileToMlirTrans( + llvm::SourceMgr* source_mgr, MLIRContext* context, + bool use_external_constant, + bool experimental_prune_unreachable_nodes_unconditionally) { + const llvm::MemoryBuffer* input = + source_mgr->getMemoryBuffer(source_mgr->getMainFileID()); + std::string error; + auto loc = + mlir::FileLineColLoc::get(context, input->getBufferIdentifier(), 0, 0); + + // Parses input/output names from command line options. + std::vector inputs; + std::vector outputs; + // Use output parser since we only have tensor names. + if (!tensorflow::ParseOutputArrayInfo(input_arrays_flag, &inputs).ok()) { + return emitError(loc, "parsing input array info failed ") + << input_arrays_flag, + nullptr; + } + if (!tensorflow::ParseOutputArrayInfo(output_arrays_flag, &outputs).ok()) { + return emitError(loc, "parsing output array info failed ") + << output_arrays_flag, + nullptr; + } + return tflite::FlatBufferToMlir( + absl::string_view(input->getBufferStart(), input->getBufferSize()), + context, loc, use_external_constant, inputs, outputs, + experimental_prune_unreachable_nodes_unconditionally, + disable_vhlo_to_stablehlo); +} + +static LogicalResult MlirToFlatBufferFileTranslateFunction( + ModuleOp module, llvm::raw_ostream& output) { + std::string serialized_flatbuffer; + std::unique_ptr op_or_arg_name_mapper; + if (strip_debug_info) { + op_or_arg_name_mapper = + std::make_unique(); + } else { + op_or_arg_name_mapper = + std::make_unique(); + } + tflite::FlatbufferExportOptions options; + options.converter_flags.set_force_select_tf_ops(!emit_builtin_tflite_ops); + options.converter_flags.set_enable_select_tf_ops(emit_select_tf_ops); + options.converter_flags.set_allow_custom_ops(emit_custom_ops); + options.converter_flags.set_use_buffer_offset(use_buffer_offset); + options.op_or_arg_name_mapper = op_or_arg_name_mapper.get(); + options.converter_flags.set_serialize_debug_metadata( + serialize_debug_metadata); + options.disable_buffer_deduping = disable_buffer_deduping_flag.getValue(); + if (!tflite::MlirToFlatBufferTranslateFunction( + module, options, &serialized_flatbuffer, emit_stablehlo_ops)) + return mlir::failure(); + + output << serialized_flatbuffer; + return success(); +} +} // namespace + +static TranslateToMLIRRegistration FlatBufferFileToMlirTransReg( + "tflite-flatbuffer-to-mlir", "tflite-flatbuffer-to-mlir", + [](llvm::SourceMgr& source_mgr, MLIRContext* context) { + return FlatBufferFileToMlirTrans( + &source_mgr, context, use_external_constant, + experimental_prune_unreachable_nodes_unconditionally); + }); + +static TranslateFromMLIRRegistration MLIRToFlatBufferTranslate( + "mlir-to-tflite-flatbuffer", "mlir-to-tflite-flatbuffer", + MlirToFlatBufferFileTranslateFunction, [](DialectRegistry& registry) { + registry.insert(); + mlir::RegisterAllTensorFlowDialects(registry); + registry.insert(); + registry.insert(); + registry.insert(); + registry.insert(); + registry.insert(); + }); +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_translate.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_translate.h new file mode 100644 index 0000000000000000000000000000000000000000..06e8123f6e29b05186a325bd8f0dee8719a07c23 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_translate.h @@ -0,0 +1,44 @@ +/* Copyright 2019 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_FLATBUFFER_TRANSLATE_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_FLATBUFFER_TRANSLATE_H_ + +#include + +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "tensorflow/compiler/mlir/op_or_arg_name_mapper.h" + +namespace tflite { + +// Translates the given MLIR `module` into a FlatBuffer and stores the +// serialized flatbuffer into the string. This uses OpOrArgLocNameMapper to +// convert location of the op to name in flatbuffer. Returns true if translation +// fails, otherwise returns false. +bool MlirToFlatBufferTranslateFunction(mlir::ModuleOp module, + std::string* serialized_flatbuffer, + bool emit_builtin_tflite_ops, + bool emit_select_tf_ops, + bool emit_custom_ops); + +// Same as the above but with a custom op name mapper. +bool MlirToFlatBufferTranslateFunction( + mlir::ModuleOp module, std::string* serialized_flatbuffer, + bool emit_builtin_tflite_ops, bool emit_select_tf_ops, bool emit_custom_ops, + tensorflow::OpOrArgNameMapper* op_or_arg_name_mapper); +} // namespace tflite + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_FLATBUFFER_TRANSLATE_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_translate_flags.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_translate_flags.h new file mode 100644 index 0000000000000000000000000000000000000000..2c912475888669a3097813127f931ea8cbaeb098 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/flatbuffer_translate_flags.h @@ -0,0 +1,31 @@ +/* Copyright 2019 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_FLATBUFFER_TRANSLATE_FLAGS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_FLATBUFFER_TRANSLATE_FLAGS_H_ + +#include + +// These flags are used to control the emission or not of different kinds of ops +// during the flatbuffer translation. +extern bool emit_builtin_tflite_ops; +extern bool emit_select_tf_ops; +extern bool emit_custom_ops; +// The flag to control whether to lower tensorlist ops into TF ops. +extern bool lower_tensor_list_ops; +// The flag to control whether debug info gets stripped on export. +extern bool strip_debug_info; + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_FLATBUFFER_TRANSLATE_FLAGS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/glob_lit_test.bzl b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/glob_lit_test.bzl new file mode 100644 index 0000000000000000000000000000000000000000..0562730201bc6e33e3a04de5e224f74f91143340 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/glob_lit_test.bzl @@ -0,0 +1,153 @@ +# Test definitions for Lit, the LLVM test runner. +# +# This is reusing the LLVM Lit test runner in the interim until the new build +# rules are upstreamed. +# TODO(b/136126535): remove this custom rule. +"""Lit runner globbing test +""" + +load("@bazel_skylib//lib:paths.bzl", "paths") +load("@rules_python//python:py_test.bzl", "py_test") +load( + "@xla//xla:lit.bzl", + "lit_script_with_xla_gpu_cuda_data_dir", +) + +# Default values used by the test runner. +_default_test_file_exts = ["mlir", ".pbtxt", ".td"] +_default_driver = "@llvm-project//mlir:run_lit.sh" +_default_size = "small" +_default_tags = [] + +# These are patterns which we should never match, for tests, subdirectories, or +# test input data files. +_ALWAYS_EXCLUDE = [ + "**/LICENSE.txt", + "**/README.txt", + "**/lit.local.cfg", + # Exclude input files that have spaces in their names, since bazel + # cannot cope with such "targets" in the srcs list. + "**/* *", + "**/* */**", +] + +def _run_lit_test(name, data, size, tags, driver, features, exec_properties): + """Runs lit on all tests it can find in `data` under tflite/converter. + + Note that, due to Bazel's hermetic builds, lit only sees the tests that + are included in the `data` parameter, regardless of what other tests might + exist in the directory searched. + + Args: + name: str, the name of the test, including extension. + data: [str], the data input to the test. + size: str, the size of the test. + tags: [str], tags to attach to the test. + driver: str, label of the driver shell script. + Note: use of a custom driver is not currently supported + and specifying a default driver will abort the tests. + features: [str], list of extra features to enable. + exec_properties: a dictionary of properties to pass on. + """ + + # Disable tests on windows for now, to enable testing rest of all xla and mlir. + py_test( + name = name, + srcs = ["@llvm-project//llvm:lit"], + tags = tags + ["no_pip", "no_windows"], + args = [ + "tflite/converter/" + paths.basename(data[-1]) + " --config-prefix=runlit -v", + ] + features, + data = data + [ + "//tensorflow/compiler/mlir/lite:litfiles", + "@llvm-project//llvm:FileCheck", + "@llvm-project//llvm:count", + "@llvm-project//llvm:not", + ], + deps = ["@pypi//lit"], + size = size, + main = "lit.py", + exec_properties = exec_properties, + ) + +def glob_lit_tests( + name = None, + exclude = [], + test_file_exts = _default_test_file_exts, + default_size = _default_size, + size_override = {}, + data = [], + per_test_extra_data = {}, + default_tags = _default_tags, + tags_override = {}, + driver = _default_driver, + features = [], + exec_properties = {}, + use_lit_test_suite = None, # @unused + hermetic_cuda_data_dir = None): + """Creates all plausible Lit tests (and their inputs) under this directory. + + Args: + name: str, name of the test_suite rule to generate for running all tests. + exclude: [str], paths to exclude (for tests and inputs). + test_file_exts: [str], extensions for files that are tests. + default_size: str, the test size for targets not in "size_override". + size_override: {str: str}, sizes to use for specific tests. + data: [str], additional input data to the test. + per_test_extra_data: {str: [str]}, extra data to attach to a given file. + default_tags: [str], additional tags to attach to the test. + tags_override: {str: str}, tags to add to specific tests. + driver: str, label of the driver shell script. + Note: use of a custom driver is not currently supported + and specifying a default driver will abort the tests. + features: [str], list of extra features to enable. + exec_properties: a dictionary of properties to pass on. + hermetic_cuda_data_dir: string. If set, the tests will be run with a + `--xla_gpu_cuda_data_dir` flag set to the hermetic CUDA data directory. + use_lit_test_suite: unused. For compatibility. + """ + + # Ignore some patterns by default for tests and input data. + exclude = _ALWAYS_EXCLUDE + exclude + + tests = native.glob( + ["*." + ext for ext in test_file_exts], + exclude = exclude, + ) + + # Run tests individually such that errors can be attributed to a specific + # failure. + all_tests = [] + for curr_test in tests: + final_test_name = curr_test + if hermetic_cuda_data_dir: + output_file = "with_xla_gpu_cuda_data_dir_{}".format(curr_test) + rule_name = "script_{}".format(output_file) + lit_script_with_xla_gpu_cuda_data_dir( + rule_name, + curr_test, + output_file, + hermetic_cuda_data_dir, + ) + final_test_name = output_file + all_tests.append(final_test_name + ".test") + + # Instantiate this test with updated parameters. + _run_lit_test( + name = final_test_name + ".test", + data = data + [final_test_name] + + per_test_extra_data.get(curr_test, []), + size = size_override.get(curr_test, default_size), + tags = default_tags + tags_override.get(curr_test, []), + driver = driver, + features = features, + exec_properties = exec_properties, + ) + + # TODO: remove this check after making it a required param. + if name: + native.test_suite( + name = name, + tests = all_tests, + tags = ["manual"], + ) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/ir/tfl_canonicalize.td b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/ir/tfl_canonicalize.td new file mode 100644 index 0000000000000000000000000000000000000000..4373afea9b47e4467bda42c34c0fb5f3e2924c6e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/ir/tfl_canonicalize.td @@ -0,0 +1,61 @@ +/* Copyright 2023 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. +==============================================================================*/ + +// This is the optimization pattern definition file for TensorFlow Lite. + +include "mlir/IR/OpBase.td" +include "mlir/IR/PatternBase.td" +include "mlir/Dialect/Arith/IR/ArithOps.td" +include "tensorflow/compiler/mlir/lite/ir/tfl_ops.td" +include "tensorflow/compiler/mlir/lite/utils/utils.td" + +// Returns Squeezed shape of a ranked-tensor. +// Squeezed, here, means eliminating any 1s' in the +// dimensions of the tensor +def GetSqueezedShape: NativeCodeCall<"GetSqueezedShape($0)">; + +// This is a utility function to deduct the effective permutation to apply on +// TFL_TransposeOp when the tensor has some dimensions with value==1 +def GetSqueezedPermutation: NativeCodeCall<"GetSqueezedPermutation($0, $1)">; + +// Check to see if the tensor dimensions can be Squeezed by eliminating 1s' +def CanSqueezeTensor : Constraint GetSqueezedShape($0).getNumElements()">>; + + +// Pattern to convert TFL_TransposeOp with rank>6 to rank<=6 if there are +// redundant dimensions in the tensor. For example- [2x1x3] == [2x3] and 1 is +// not contributing to the dimentionality. This will run if the rank>6 +// Pattern will convert- +// %0 = "tfl.transpose"(%arg0, %cst) : (tensor<56x8x56x1x1x1x7xf32>, tensor<7xi32>) -> tensor<1x1x8x56x56x7x1xf32> +// to- +// %0 = "tfl.reshape"(%arg0, %cst) : (tensor<56x8x56x1x1x1x7xf32>, tensor<4xi32>) -> tensor<56x8x56x7xf32> +// %1 = "tfl.transpose"(%0, %cst_0) : (tensor<56x8x56x7xf32>, tensor<4xi32>) -> tensor<8x56x56x7xf32> +// %2 = "tfl.reshape"(%1, %cst_1) : (tensor<8x56x56x7xf32>, tensor<7xi32>) -> tensor<1x1x8x56x56x7x1xf32> +def ConvertTransposeToDecreaseRank : Pat< + (TFL_TransposeOp:$output_transpose $input, (Arith_ConstantOp:$permutation $_)), + (TFL_ReshapeOp + (TFL_TransposeOp + (TFL_ReshapeOp $input, (Arith_ConstantOp (GetSqueezedShape $input))), + (Arith_ConstantOp (GetSqueezedPermutation $input, $permutation))), + (Arith_ConstantOp (GetShapeAttr $output_transpose))), + [(AnyStaticShapeTensor $input), + (HasRankAtLeast<7> $input), + (CanSqueezeTensor $input)]>; + +def RemoveNoopTranspose : Pat< + (TFL_TransposeOp $input, $perm), + (replaceWithValue $input), + [(IsTransposeNoop $perm)]>; \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/ir/tfl_op_enums.td b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/ir/tfl_op_enums.td new file mode 100644 index 0000000000000000000000000000000000000000..30f43f8667061919ce8e80d63debb9d00fea2873 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/ir/tfl_op_enums.td @@ -0,0 +1,181 @@ +/* Copyright 2022 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. +==============================================================================*/ + +// This is the operation enums definition file for TensorFlow Lite. + +#ifndef TFL_OP_ENUMS +#define TFL_OP_ENUMS + +include "mlir/IR/AttrTypeBase.td" +include "mlir/IR/EnumAttr.td" +include "mlir/IR/OpBase.td" +include "tensorflow/compiler/mlir/lite/ir/tfl_op_interfaces.td" + +// A string attribute whose value are one of the values in `cases`. +// Referred TF_AnyStrAttrOf in tensorflow/compiler/mlir/tensorflow/ir/tf_op_base.td +class TFL_AnyStrAttrOf cases> : StringBasedAttr< + CPred($_self).getValue() == \"" # !head(cases) # "\"", + !foreach(case, !tail(cases), + "llvm::cast($_self).getValue() == \"" # case # "\""), + prev, cur, prev # " || " # cur)>, + "string attribute whose value is " # + !foldl(/*init*/!head(cases), /*list*/!tail(cases), + prev, cur, prev # ", or " # cur)>; + +// Attributes used for encoding sparse tensors. +// Please find detailed explanation of these parameters in the TFLite schema. +def TFL_DT_Dense : I32EnumAttrCase<"DENSE", 0>; +def TFL_DT_SparseCSR : I32EnumAttrCase<"SPARSE_CSR", 1>; + +def TFL_DimensionType : I32EnumAttr< + "DimensionType", "dimension_type", [TFL_DT_Dense, TFL_DT_SparseCSR]> { + let genSpecializedAttr = 0; + let cppNamespace = "::mlir::TFL"; +} + +def TFL_DimensionTypeAttr : EnumAttr { + let convertFromStorage = "$_self"; +} + +// Allowed activation function cases +// These should match the ActivationFunctionType enum in TFLite schema. +def TFL_AFEnum_None : I32EnumAttrCase<"NONE", 0>; +def TFL_AFEnum_Relu : I32EnumAttrCase<"RELU", 1>; +def TFL_AFEnum_Relu1 : I32EnumAttrCase<"RELU_N1_TO_1", 2>; +def TFL_AFEnum_Relu6 : I32EnumAttrCase<"RELU6", 3>; +def TFL_AFEnum_Tanh : I32EnumAttrCase<"TANH", 4>; +def TFL_AFEnum_Sign : I32EnumAttrCase<"SIGN_BIT", 5>; + +def TFL_AFAttr : TFL_AnyStrAttrOf<[ + TFL_AFEnum_None.symbol, TFL_AFEnum_Relu.symbol, TFL_AFEnum_Relu1.symbol, + TFL_AFEnum_Relu6.symbol, TFL_AFEnum_Tanh.symbol, TFL_AFEnum_Sign.symbol + ]>; + +// Predefined constant attributes of activation function +def TFL_AF_None : ConstantStrAttr; +def TFL_AF_Relu : ConstantStrAttr; +def TFL_AF_Relu1 : ConstantStrAttr; +def TFL_AF_Relu6 : ConstantStrAttr; +def TFL_AF_Tanh : ConstantStrAttr; +def TFL_AF_Sign : ConstantStrAttr; + +// Allowed padding cases +// These should match the padding enum in TFLite schema. +def TFL_PADEnum_Same : I32EnumAttrCase<"SAME", 0>; +def TFL_PADEnum_Valid : I32EnumAttrCase<"VALID", 1>; +def TFL_PaddingAttr : TFL_AnyStrAttrOf<[ + TFL_PADEnum_Same.symbol, TFL_PADEnum_Valid.symbol + ]>; +def TFL_PAD_Same : ConstantStrAttr; +def TFL_PAD_Valid : ConstantStrAttr; + +// FullyConnectedOptionsWeightFormat attributes +def TFL_FCWOEnum_Default : I32EnumAttrCase<"DEFAULT", 0>; +def TFL_FCWOEnum_Shuffled4x16i8 : I32EnumAttrCase<"SHUFFLED4x16INT8", 1>; +def TFL_FullyConnectedOptionsWeightFormatAttr : + TFL_AnyStrAttrOf<[ + TFL_FCWOEnum_Default.symbol, + TFL_FCWOEnum_Shuffled4x16i8.symbol + ]>; +def TFL_FCWO_Default : ConstantStrAttr< + TFL_FullyConnectedOptionsWeightFormatAttr, TFL_FCWOEnum_Default.symbol>; +def TFL_FCWO_Shuffled4x16i8 : ConstantStrAttr< + TFL_FullyConnectedOptionsWeightFormatAttr, TFL_FCWOEnum_Shuffled4x16i8.symbol>; + +// MirrorPadding type attributes +def TFL_MIRRORPAD_Reflect : I32EnumAttrCase<"REFLECT", 0>; +def TFL_MIRRORPAD_Symmetric : I32EnumAttrCase<"SYMMETRIC", 1>; +def TFL_MirrorPaddingType : I32EnumAttr<"MirrorPaddingType", "mirror_pad_enum", [ + TFL_MIRRORPAD_Reflect, TFL_MIRRORPAD_Symmetric + ]> { + let genSpecializedAttr = 0; + let cppNamespace = "::mlir::TFL"; +} +def TFL_MirrorPaddingAttr : EnumAttr; + +// LSTM Kernel Type attributes +def TFL_LSTM_KT_FULL : I32EnumAttrCase<"FULL", 0>; +def TFL_LSTM_KT_BASIC : I32EnumAttrCase<"BASIC", 1>; +def TFL_LSTMKernelType : I32EnumAttr<"LSTMKernelType", "lstm_kernel_type", + [ + TFL_LSTM_KT_FULL, TFL_LSTM_KT_BASIC + ]>{ + let genSpecializedAttr = 0; + let cppNamespace = "::mlir::TFL"; +} +def TFL_LSTMKernelTypeAttr : EnumAttr; + +def I32ArrayParameter : + AttrOrTypeParameter<"::llvm::ArrayRef", ""> { + let allocator = [{$_dst = $_allocator.copyInto($_self);}]; + let cppStorageType = "::llvm::SmallVector"; + let parser = "::mlir::TFL::parseI32Array($_parser)"; + let printer = "$_printer << '[' << $_self << ']'"; +} + +def DimensionMetadataAttr : AttrDef { + let mnemonic = "dimension_metadata"; + let parameters = (ins + TFL_DimensionTypeAttr:$format, + "int32_t":$dense_size, + I32ArrayParameter:$segments, + I32ArrayParameter:$indices + ); + let summary = "Dimension metadata."; + let assemblyFormat = "`<` struct(params) `>`"; +} + +def SparsityParameterAttr : AttrDef { + let mnemonic = "sparsity_parameter"; + let parameters = (ins + I32ArrayParameter:$traversal_order, + I32ArrayParameter:$block_map, + ArrayRefParameter<"DimensionMetadataAttr">:$dim_metadata + ); + let summary = "Sparsity parameter."; + let assemblyFormat = "`<` struct(params) `>`"; +} + +def TFL_ConstBytesAttr : AttrDef { + let summary = "A string attribute representation of compiled bytes"; + let description = [{ + Syntax Examples: + + ```mlir + #tfl + ``` + }]; + let mnemonic = "const_bytes"; + let parameters = (ins StringRefParameter<"">:$value); + let hasCustomAssemblyFormat = 1; +} + +def TFL_ExternalBufferAttr : AttrDef { + let mnemonic = "external_buffer"; + let parameters = (ins + "::mlir::StringAttr":$group_name, + "uint64_t":$offset, + "uint64_t":$length, + "::mlir::StringAttr":$packing + ); + let summary = "Flatbuffer external buffer metadata."; + let assemblyFormat = "`<` struct(params) `>`"; +} + +#endif // TFL_OP_ENUMS diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/ir/tfl_op_interfaces.td b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/ir/tfl_op_interfaces.td new file mode 100644 index 0000000000000000000000000000000000000000..6e52d2a421143abd24a94f2a282bc3954058ec9f --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/ir/tfl_op_interfaces.td @@ -0,0 +1,134 @@ +/* Copyright 2020 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. +==============================================================================*/ + +// This is the operation interface definition file for TensorFlow Lite. + +#ifndef TFL_OP_INTERFACES +#define TFL_OP_INTERFACES + +include "mlir/IR/OpBase.td" + +def TFL_Dialect : Dialect { + let name = "tfl"; + + let description = [{ + The TensorFlow Lite dialect. + + This dialect maps to TensorFlow Lite operations. + + Invariants: + + * All values are of Tensor type (in particular, scalars are + represented using zero-dimensional tensors); + }]; + + let cppNamespace = "::mlir::TFL"; + + let useDefaultAttributePrinterParser = 1; + let useDefaultTypePrinterParser = 1; + + let extraClassDeclaration = [{ + ParseResult parseOneResultSameOperandTypeOp(OpAsmParser &parser, + OperationState &result); + + void printOneResultOp(Operation *op, OpAsmPrinter &p); + + // Registered hook to materialize a constant operation from a given + // attribute value with the desired resultant type. + Operation *materializeConstant(OpBuilder &builder, Attribute value, + Type type, Location loc) override; + }]; +} + +//===----------------------------------------------------------------------===// +// TFL op interface for stateful operands. + +def TFL_StatefulOp : OpInterface<"StatefulOpInterface"> { + let description = [{ + Interface for ops that are stateful and need to identify stateful operands. + + Stateful operands correspond to TF's variables semantics. An op that has 1 + or more stateful operands is a stateful op. + }]; + + let methods = [ + InterfaceMethod< + [{Returns the indices of stateful operands.}], + "std::vector", "GetStatefulOperands", (ins) + >, + ]; +} + +//===----------------------------------------------------------------------===// +// TFL op interface for sparse operands. + +def TFL_SparseOp : OpInterface<"SparseOpInterface"> { + let description = [{ + Interface for ops that support sparse computation. + }]; + + let methods = [ + InterfaceMethod< + [{Returns the indices of sparse operands.}], + "std::vector", "GetSparseOperands", (ins) + >, + InterfaceMethod< + [{Returns the supported block size of float sparse operands.}], + "std::vector>", "GetFloatBlockSize", (ins) + >, + InterfaceMethod< + [{Returns the supported block size of quantized sparse operands.}], + "std::vector>", "GetQuantizedBlockSize", (ins) + >, + ]; +} + +//===----------------------------------------------------------------------===// +// TFL runtime type verification of operand/result types. + +def TFL_RuntimeVerification : OpInterface<"TflRuntimeVerifyOpInterface"> { + let description = [{ + Interface to verify TFLite runtime op verification. + + This verifies that the converted TFLite ops has operand/result type + supported by the TFLite runtime. + }]; + + let methods = [ + StaticInterfaceMethod< + [{Returns whether the op's operands/results are supported by runtime.}], + "LogicalResult", "VerifyTflRuntimeConstraints", + (ins "Operation*":$op, "bool":$emit_error_on_verify_fail) + >, + ]; +} + +//===----------------------------------------------------------------------===// +// TFL arithmetic count interface. + +def TFL_ArithmeticCount : OpInterface<"TflArithmeticCountOpInterface"> { + let description = [{ + Interface for TFLite ops to calculate arithmetic count (Multiply-Add Count). + }]; + + let methods = [ + StaticInterfaceMethod< + [{Returns an integer representing the op's arithmetic count (Multiply-Add + Count), return -1 if the arithmetic count cannot be determined.}], + "int64_t", "GetArithmeticCount", (ins "Operation*":$op) + >, + ]; +} +#endif // TFL_OP_INTERFACES diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/ir/tfl_ops.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/ir/tfl_ops.cc new file mode 100644 index 0000000000000000000000000000000000000000..b6501fd34be37b38ea697f73dbfacb643b64cdce --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/ir/tfl_ops.cc @@ -0,0 +1,6279 @@ +/* Copyright 2023 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/compiler/mlir/lite/ir/tfl_ops.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "absl/base/const_init.h" +#include "absl/container/flat_hash_map.h" +#include "absl/container/flat_hash_set.h" +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/escaping.h" +#include "absl/synchronization/mutex.h" +#include "Eigen/Core" // from @eigen_archive +#include "llvm/ADT/APFloat.h" +#include "llvm/ADT/APInt.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/FloatingPointMode.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/STLFunctionalExtras.h" +#include "llvm/ADT/Sequence.h" +#include "llvm/ADT/SetVector.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/SmallVectorExtras.h" +#include "llvm/ADT/StringExtras.h" +#include "llvm/ADT/TypeSwitch.h" // IWYU pragma: keep +#include "llvm/Support/Casting.h" +#include "llvm/Support/FormatVariadic.h" +#include "llvm/Support/Threading.h" +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Dialect/Quant/IR/QuantTypes.h" // from @llvm-project +#include "mlir/Dialect/Traits.h" // from @llvm-project +#include "mlir/IR/AsmState.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/Diagnostics.h" // from @llvm-project +#include "mlir/IR/DialectImplementation.h" // from @llvm-project +#include "mlir/IR/Location.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/Matchers.h" // from @llvm-project +#include "mlir/IR/OpDefinition.h" // from @llvm-project +#include "mlir/IR/OpImplementation.h" // from @llvm-project +#include "mlir/IR/OperationSupport.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/Region.h" // from @llvm-project +#include "mlir/IR/TypeUtilities.h" // from @llvm-project +#include "mlir/IR/Types.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Interfaces/ControlFlowInterfaces.h" // from @llvm-project +#include "mlir/Interfaces/FoldInterfaces.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/FoldUtils.h" // from @llvm-project +#include "mlir/Transforms/InliningUtils.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_traits.h" +#include "tensorflow/compiler/mlir/lite/utils/arithmetic_count_util.h" +#include "tensorflow/compiler/mlir/lite/utils/attribute_utils.h" +#include "tensorflow/compiler/mlir/lite/utils/shape_and_size_utils.h" +#include "tensorflow/compiler/mlir/lite/utils/utils.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_op_interfaces.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops_a_m.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_types.h" +#include "tensorflow/compiler/mlir/tensorflow/utils/dynamic_shape_utils.h" +#include "tensorflow/core/framework/kernel_shape_util.h" +#include "tensorflow/core/util/padding.h" + +// TODO(b/493282789): Enable DenseResourceElementsAttr constant folding with +// more peformant folder implementations. +#ifndef ENABLE_DENSE_RESOURCE_ATTR_FOLD +#define ENABLE_DENSE_RESOURCE_ATTR_FOLD 0 +#else +#define ENABLE_DENSE_RESOURCE_ATTR_FOLD 1 +#endif + +namespace mlir { +namespace TFL { + +// go/keep-sorted start +INFER_RETURN_TYPE_COMPONENTS_FROM_OPERANDS(CeilOp); +INFER_RETURN_TYPE_COMPONENTS_FROM_OPERANDS(CosOp); +INFER_RETURN_TYPE_COMPONENTS_FROM_OPERANDS(FloorOp); +INFER_RETURN_TYPE_COMPONENTS_FROM_OPERANDS(LocalResponseNormalizationOp); +INFER_RETURN_TYPE_COMPONENTS_FROM_OPERANDS(NegOp); +INFER_RETURN_TYPE_COMPONENTS_FROM_OPERANDS(RoundOp); +INFER_RETURN_TYPE_COMPONENTS_FROM_OPERANDS(SinOp); +INFER_RETURN_TYPE_COMPONENTS_FROM_OPERANDS(SquareOp); +// go/keep-sorted end + +namespace { + +ParseResult parseOneResultSameOperandTypeOp(OpAsmParser& parser, + OperationState& result) { + SmallVector ops; + // If the operand list is in-between parentheses, then we have a generic form. + // (see the fallback in `printOneResultOp`). + if (!parser.parseOptionalLParen()) { + if (parser.parseOperandList(ops) || parser.parseRParen()) return failure(); + return parser.parseGenericOperationAfterOpName(result, ops); + } + Type type; + return failure(parser.parseOperandList(ops) || + parser.parseOptionalAttrDict(result.attributes) || + parser.parseColonType(type) || + parser.resolveOperands(ops, type, result.operands) || + parser.addTypeToList(type, result.types)); +} + +void printOneResultOp(Operation* op, OpAsmPrinter& p) { + assert(op->getNumResults() == 1 && "op should have one result"); + + // If not all the operand and result types are the same, just use the + // generic assembly form to avoid omitting information in printing. + auto resultType = op->getResult(0).getType(); + if (llvm::any_of(op->getOperandTypes(), + [&](Type type) { return type != resultType; })) { + p.printGenericOp(op, /*printOpName=*/false); + return; + } + + p << ' '; + p.printOperands(op->getOperands()); + p.printOptionalAttrDict(op->getAttrs()); + // Now we can output only one type for all operands and the result. + p << " : " << resultType; +} + +Operation* getDefiningBroadcastArgsOp(Value operand) { + auto* defining_op = operand.getDefiningOp(); + if (!llvm::dyn_cast_or_null(defining_op) && + !llvm::dyn_cast_or_null(defining_op)) { + return nullptr; + } + + Value broadcast_shape = defining_op->getOperand( + 1); // Broadcasted shape operand of BroadcastTo op. + Operation* parent_of_defining_op = broadcast_shape.getDefiningOp(); + if (!llvm::dyn_cast_or_null(parent_of_defining_op) && + !llvm::dyn_cast_or_null(parent_of_defining_op)) { + return nullptr; + } + return parent_of_defining_op; +} + +// Returns shape of a ranked tensor. +// Precondition: value_tensor's is ranked tensor. +// Returns a Squeezed shape. Truncation here means eliminating the redundant +// dimensions 1. +DenseElementsAttr GetSqueezedShape(Value value_tensor) { + auto value_shape_type = llvm::dyn_cast(value_tensor.getType()); + assert(value_shape_type.hasRank() && "value_tensor should be ranked tensor"); + + auto value_shape = value_shape_type.getShape(); + SmallVector return_squeeze_shape; + return_squeeze_shape.reserve(value_shape.size()); + + for (size_t dim_idx = 0; dim_idx < value_shape.size(); ++dim_idx) { + int64_t dim = value_shape[dim_idx]; + if (dim == 1) { + continue; + } + return_squeeze_shape.push_back( + ShapedType::isDynamic(dim) ? -1 : static_cast(dim)); + } + + return mlir::DenseElementsAttr::get( + RankedTensorType::get( + {static_cast(return_squeeze_shape.size())}, + mlir::IntegerType::get(value_tensor.getContext(), 32)), + llvm::ArrayRef(return_squeeze_shape)); +} + +// This is a utility function to deduce the effective permutation to apply on +// TFL_TransposeOp when the tensor has some dimensions with value==1 +// Example- "tfl.transpose"(tensor<56x8x56x1x1x1x7xf32>, [4, 5, 1, 2, 0, 6, 3]) +// Permutation before squeese is [4, 5, 1, 2, 0, 6, 3] becomes [1, 2, 0, 3] +// after squeeze is performed to retain the relative ordering of the non-1 dims. +DenseElementsAttr GetSqueezedPermutation(Value input_value, + Value input_permutation) { + auto input_shape = + mlir::dyn_cast(input_value.getType()).getShape(); + absl::flat_hash_map permutation_map; + + for (size_t before_dim_idx = 0, after_dim_idx = 0; + before_dim_idx < input_shape.size(); ++before_dim_idx) { + if (input_shape[before_dim_idx] == 1) { + continue; + } + permutation_map.insert({before_dim_idx, after_dim_idx++}); + } + + SmallVector squeezed_permutation; + DenseElementsAttr input_perm_const; + if (matchPattern(input_permutation, m_Constant(&input_perm_const))) { + for (int32_t idx = 0; idx < input_perm_const.getNumElements(); ++idx) { + size_t perm = input_perm_const.getValues()[idx].getSExtValue(); + if (input_shape[perm] == 1) { + continue; + } + squeezed_permutation.push_back(permutation_map[perm]); + } + } + + return mlir::DenseElementsAttr::get( + RankedTensorType::get( + {static_cast(squeezed_permutation.size())}, + mlir::IntegerType::get(input_permutation.getContext(), 32)), + llvm::ArrayRef(squeezed_permutation)); +} + +bool HasDenseResourceOperand(mlir::Operation* op) { + ElementsAttr elements_attr; + for (auto operand : op->getOperands()) { + if (matchPattern(operand, m_Constant(&elements_attr))) { + if (llvm::isa(elements_attr)) { + return true; + } + } + } + return false; +} + +// Utility function to determine if an operation should be folded. +// This is a heuristic to avoid folding operations that result in larger +// constants. +// TODO(b/394905516): Remove this once we have a way to configure the threshold. +bool ShouldFoldOperation(Operation* inst) { + if (!(ENABLE_DENSE_RESOURCE_ATTR_FOLD) && HasDenseResourceOperand(inst)) { + return false; + } + + auto get_size = [&](TypeRange types) { + int64_t size = 0; + for (auto t : types) { + auto tensor_type = mlir::cast(t); + // Ignore types with undefined bit widths. + if (!tensor_type.getElementType().isIntOrFloat()) continue; + // Ignore types with dynamic shapes. + if (!tensor_type.hasStaticShape()) continue; + size += tensor_type.getNumElements() * + tensor_type.getElementType().getIntOrFloatBitWidth(); + } + return size; + }; + + int64_t inputs_size = get_size(inst->getOperandTypes()); + int64_t outputs_size = get_size(inst->getResultTypes()); + + constexpr int64_t kInputsSizeThreshold = 200L * 1024 * 1024 * 8; // 200 MiB + constexpr int64_t kOutputsSizeThreshold = + 2 * kInputsSizeThreshold; // 400 MiB + + auto output_size_is_smaller_than_inputs = outputs_size <= inputs_size; + + auto inputs_and_outputs_smaller_than_arbitrary_thresholds = + (inputs_size <= kInputsSizeThreshold) && + (outputs_size <= kOutputsSizeThreshold); + + // Folding rules are: + // 1. if the size of the resulting outputs are smaller than the inputs then + // just do the fold. The model size will be smaller as a result. + // 2. if the inputs and outputs sizes are smaller than certain thresholds, do + // the fold regardless of their impact on model size. + return output_size_is_smaller_than_inputs || + inputs_and_outputs_smaller_than_arbitrary_thresholds; +} + +// Returns dimension index for the given axis that supports negative +// indexing. +int64_t NormalizeDim(int64_t axis, int64_t rank) { + return axis >= 0 ? axis : axis + rank; +} + +Type InferReductionOpType(Value input, Value reduction_indices, + BoolAttr keep_dims) { + Type input_ty = input.getType(); + Type element_ty = getElementTypeOrSelf(input_ty); + + // Output type is unranked if input type is not ranked. + auto ranked_ty = mlir::dyn_cast(input_ty); + if (!ranked_ty) return UnrankedTensorType::get(element_ty); + int64_t rank = ranked_ty.getRank(); + + DenseIntElementsAttr indices; + if (!matchPattern(reduction_indices, m_Constant(&indices))) { + // Output type is unranked if reduction indices are not constant and reduced + // dimensions are not kept. + if (!keep_dims.getValue()) return UnrankedTensorType::get(element_ty); + + // Otherwise, output type has same rank as the input. + return RankedTensorType::get( + SmallVector(rank, ShapedType::kDynamic), element_ty); + } + + int64_t num_reduce_dim = 0; + llvm::SmallVector is_reduce_dim(rank, false); + for (const APInt& index : indices.getValues()) { + int64_t dim = NormalizeDim(index.getSExtValue(), rank); + // Invalid input. + assert(dim >= 0 && dim < rank); + + if (!is_reduce_dim[dim]) { + is_reduce_dim[dim] = true; + num_reduce_dim++; + } + } + + ArrayRef shape = ranked_ty.getShape(); + SmallVector out_shape; + out_shape.reserve(rank - (keep_dims.getValue() ? 0 : num_reduce_dim)); + for (int64_t i = 0; i < rank; ++i) { + if (!is_reduce_dim[i]) + out_shape.push_back(shape[i]); + else if (keep_dims.getValue()) + out_shape.push_back(1); + } + return RankedTensorType::get(out_shape, element_ty); +} + +#include "tensorflow/compiler/mlir/lite/ir/tfl_canonicalize.inc" + +} // namespace + +// Returns true when the given type lists contain a single element of shaped +// type with compatible shapes (unranked shape is compatible with any ranked +// shape, ranked shapes are compatible if their respective dimensions are +// compatible, dynamic dimensions are compatible with any size, static +// dimensions must be equal to be compatible) and identical element types. +bool VerifyCompatibleShapesSameElementType(TypeRange lhs, TypeRange rhs) { + if (lhs.size() != rhs.size() || lhs.size() != 1) return false; + if (failed(mlir::verifyCompatibleShape(lhs[0], rhs[0]))) return false; + auto lhsShaped = mlir::cast(lhs[0]); + auto rhsShaped = mlir::cast(rhs[0]); + return lhsShaped.getElementType() == rhsShaped.getElementType(); +} + +// Returns true when the given operand arguments have the same shape or +// broadcastable shape within the given rank. If any given shapes are +// non-static and maximum rank is within the given rank, this method returns +// true. +bool VerifyOperandsHaveSameShapesOrBroadcastableShape( + Operation* op, ArrayRef indices, int max_bcast_rank) { + if (indices.empty()) return true; + + // First, it checks there are any inputs that has unknown rank. + bool has_unknown_shape_input = false; + bool has_same_shape = true; + bool reach_first_known_shape = false; + int64_t max_rank = -1; + + ArrayRef pivot_shape; + SmallVector current_shape; + SmallVector result_shape; + + for (unsigned index : indices) { + ShapedType shaped_type = + mlir::dyn_cast(op->getOperand(index).getType()); + if (!shaped_type || !shaped_type.hasRank()) { + // Marks that we have an unknown rank input. + has_unknown_shape_input = true; + continue; + } + max_rank = std::max(max_rank, shaped_type.getRank()); + if (!shaped_type.hasStaticShape()) { + // Marks that we have an unknown shape input. + has_unknown_shape_input = true; + continue; + } + + ArrayRef shape = shaped_type.getShape(); + if (!reach_first_known_shape) { + pivot_shape = shape; + current_shape.assign(shape.begin(), shape.end()); + reach_first_known_shape = true; + continue; + } + + if (!pivot_shape.equals(shape)) { + has_same_shape = false; + } + // Checks if all the inputs are broadcastable since they have not all the + // same shapes. + if (!OpTrait::util::getBroadcastedShape(current_shape, shape, + result_shape)) { + return false; + } + current_shape = result_shape; + } + + // If all the shape is known and same, CPU kernels are able to handle inputs + // regardless of dimension size. + if (!has_unknown_shape_input) { + return has_same_shape || max_rank <= max_bcast_rank; + } + + // It will treat the unknown shape inputs as acceptable inputs for model + // compatibility if all known ranks are no bigger than the allowed broadcast + // maximum rank. + if (max_rank <= max_bcast_rank) { + return true; + } + + // Checks if all operands are broadcasted by BroadcastTo ops with the shape + // is calculated from the same BroadcastArgs op. In such case, all operands + // will have the same shape. + Operation* broadcast_args_pivot = nullptr; + for (unsigned index : indices) { + Operation* parent_broadcast_args = + getDefiningBroadcastArgsOp(op->getOperand(index)); + if (parent_broadcast_args == nullptr) { + return false; + } + + if (broadcast_args_pivot == nullptr) { + broadcast_args_pivot = parent_broadcast_args; + continue; + } + + if (broadcast_args_pivot != parent_broadcast_args) { + return false; + } + } + return true; +} + +// Return true when the given element_type is QI8. +bool IsQI8Type(Type element_type) { + auto quantized_type = mlir::dyn_cast(element_type); + return quantized_type != nullptr && + quantized_type.getStorageTypeIntegralWidth() == 8 && + quantized_type.isSigned(); +} + +// Return true when the given element_type is QUI8. +bool IsQUI8Type(Type element_type) { + auto quantized_type = mlir::dyn_cast(element_type); + return quantized_type != nullptr && + quantized_type.getStorageTypeIntegralWidth() == 8 && + !quantized_type.isSigned(); +} + +// Return true when the given element_type is QI16. +bool IsQI16Type(Type element_type) { + auto quantized_type = mlir::dyn_cast(element_type); + return quantized_type != nullptr && + quantized_type.getStorageTypeIntegralWidth() == 16 && + quantized_type.isSigned(); +} + +// Return true when the given element_type is I16. +bool IsI16Type(Type element_type) { + return element_type.isInteger(16) && !element_type.isUnsignedInteger(); +} + +// Return true when the given element_type is I32. +bool IsI32Type(Type element_type) { + return element_type.isInteger(32) && !element_type.isUnsignedInteger(); +} + +// Return true when the given element_type is UI32. +bool IsUI32Type(Type element_type) { + return element_type.isInteger(32) && element_type.isUnsignedInteger(); +} + +// Return true when the given element_type is I64. +bool IsI64Type(Type element_type) { + return element_type.isInteger(64) && !element_type.isUnsignedInteger(); +} + +// Return true if the value is a splat tensor constant zero. +bool EqualsZero(Value value) { + DenseElementsAttr constant; + if (!matchPattern(value, m_Constant(&constant)) || !constant.isSplat()) { + return false; + } + + Type element_type = mlir::cast(value.getType()).getElementType(); + if (mlir::isa(element_type)) { + return constant.getSplatValue().isZero(); + } else { + return false; + } +} + +// Replaces the bias operand with a "none" type value if the bias value is +// constant zero. +// `ConcreteOpType` must be a concrete MLIR op class that has an optional +// bias operand named 'bias'. +template +struct RemoveOptionalZeroBias : public OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(ConcreteOpType op, + PatternRewriter& rewriter) const override { + if (EqualsZero(op.getBias())) { + auto none_value = TFL::NoValueOp::create( + rewriter, rewriter.getUnknownLoc(), rewriter.getNoneType(), + rewriter.getUnitAttr()); + op.getBiasMutable().assign(none_value); + return success(); + } + + return failure(); + } +}; + +// Return true if the given Add operation has the CPU kernel supported shapes. +bool VerifyAddOpShapeConstraints(AddOp op) { + auto element_type = getElementTypeOrSelf(op.getOutput().getType()); + + // Allows F32, QI8, QUI8 and I32 outputs when the operands have valid shapes, + // which are broadcastable shapes up to four dimensions or have same shapes. + if (element_type.isF32() || element_type.isF16() || IsQI8Type(element_type) || + IsQUI8Type(element_type) || IsI16Type(element_type) || + IsI32Type(element_type) || IsI64Type(element_type)) { + return VerifyOperandsHaveSameShapesOrBroadcastableShape( + /*op=*/op.getOperation(), /*indices=*/ArrayRef{0, 1}, + /*max_bcast_rank=*/6); + } + + // Allows QI16 output when operands have the same shape. + if (IsQI16Type(element_type)) { + return succeeded(mlir::verifyCompatibleShape(op.getLhs().getType(), + op.getRhs().getType())); + } + return false; +} + +// Return true if the given Sub operation has the CPU kernel supported shapes. +bool VerifySubOpShapeConstraints(SubOp op) { + auto element_type = getElementTypeOrSelf(op.getOutput().getType()); + + // Allows F32, QUI8, and QI16 outputs when the operands have valid shapes, + // which are broadcastable shapes up to five dimension or have same shapes. + if (element_type.isF32() || IsI32Type(element_type) || + IsI64Type(element_type) || IsQUI8Type(element_type) || + IsQI16Type(element_type)) { + return VerifyOperandsHaveSameShapesOrBroadcastableShape( + /*op=*/op.getOperation(), /*indices=*/ArrayRef{0, 1}, + /*max_bcast_rank=*/6); + } + + // Allows QI8 output when the operands have valid shapes, which are + // broadcastable shapes up to four dimension or have same shapes. + if (IsQI8Type(element_type)) { + return VerifyOperandsHaveSameShapesOrBroadcastableShape( + /*op=*/op.getOperation(), /*indices=*/ArrayRef{0, 1}, + /*max_bcast_rank=*/6); + } + return false; +} + +// Return true if the given Mul operation has the CPU kernel supported shapes. +bool VerifyMulOpShapeConstraints(MulOp op) { + auto element_type = getElementTypeOrSelf(op.getOutput().getType()); + + // Allows QI8 and QUI8 inputs up to five dimension broadcasting unless the + // output type is not QI16. If the output type is Q16, allows only the same + // shape operands. + if (IsQI8Type(element_type) || IsQUI8Type(element_type)) { + if (IsQI16Type(getElementTypeOrSelf(op.getLhs().getType()))) { + return succeeded(mlir::verifyCompatibleShape(op.getLhs().getType(), + op.getRhs().getType())); + } + return VerifyOperandsHaveSameShapesOrBroadcastableShape( + /*op=*/op.getOperation(), /*indices=*/ArrayRef{0, 1}, + /*max_bcast_rank=*/6); + } + + // Allows I32, I64, QI16 and F32 outputs when the operands have valid shapes, + // which are broadcastable shapes up to four dimension or have same shapes. + if (IsI32Type(element_type) || IsUI32Type(element_type) || + IsI64Type(element_type) || IsQI16Type(element_type) || + IsI16Type(element_type) || mlir::isa(element_type) || + element_type.isF32() || element_type.isF16()) { + return VerifyOperandsHaveSameShapesOrBroadcastableShape( + /*op=*/op.getOperation(), /*indices=*/ArrayRef{0, 1}, + /*max_bcast_rank=*/6); + } + return false; +} + +class FlatIndHelper { + public: + explicit FlatIndHelper(mlir::ShapedType type) : type_(type) {} + + llvm::SmallVector GetShapedInd(int64_t flat_ind) const { + llvm::SmallVector result; + result.reserve(type_.getRank()); + + auto num_els = type_.getNumElements(); + + for (auto d : type_.getShape()) { + num_els /= d; + result.push_back(flat_ind / num_els); + flat_ind %= num_els; + } + + return result; + } + + int64_t GetFlatInd(llvm::ArrayRef shaped_ind) const { + int64_t result = 0; + + auto num_els = type_.getNumElements(); + for (auto [max_dim, ind_dim] : llvm::zip(type_.getShape(), shaped_ind)) { + num_els /= max_dim; + result += num_els * ind_dim; + } + + return result; + } + + static void AddOffset(llvm::SmallVector& target, + const llvm::SmallVector& offset) { + for (auto [ind, val] : llvm::enumerate(offset)) { + target[ind] += val; + } + } + + private: + mlir::ShapedType type_; +}; + +template +DenseElementsAttr ConstFoldCumsumOp(ShapedType result_type, + DenseElementsAttr input_elements, int axis, + bool exclusive, bool reverse) { + std::vector in_data(input_elements.value_begin(), + input_elements.value_end()); + std::vector out_data(result_type.getNumElements()); + + auto shape = result_type.getShape(); + int64_t outer_size = 1; + for (int m = 0; m < axis; ++m) { + outer_size *= shape[m]; + } + int64_t axis_size = shape[axis]; + int64_t inner_size = 1; + for (int m = axis + 1; m < shape.size(); ++m) { + inner_size *= shape[m]; + } + + for (int64_t o = 0; o < outer_size; ++o) { + for (int64_t i = 0; i < inner_size; ++i) { + int64_t slice_offset = o * axis_size * inner_size + i; + T val = 0; + for (int64_t j = 0; j < axis_size; ++j) { + int64_t d = reverse ? axis_size - 1 - j : j; + int64_t idx = slice_offset + d * inner_size; + T in_val = in_data[idx]; + if (exclusive) { + out_data[idx] = val; + val += in_val; + } else { + val += in_val; + out_data[idx] = val; + } + } + } + } + + return DenseElementsAttr::get(result_type, llvm::ArrayRef(out_data)); +} + +//===----------------------------------------------------------------------===// +// TensorFlowLiteDialect +//===----------------------------------------------------------------------===// + +struct TensorFlowLiteInlinerInterface : public DialectInlinerInterface { + using DialectInlinerInterface::DialectInlinerInterface; + + //===--------------------------------------------------------------------===// + // Analysis Hooks + //===--------------------------------------------------------------------===// + + // Allow all call operations to be inlined. + bool isLegalToInline(Operation* call, Operation* callable, + bool wouldBeCloned) const final { + return true; + } + bool isLegalToInline(Operation* op, Region* dest, bool wouldBeCloned, + IRMapping&) const final { + // No TFLite op restricts inlining today, revise as needed in the future. + return true; + } + bool isLegalToInline(Region* dest, Region* src, bool wouldBeCloned, + IRMapping& valueMapping) const final { + return isa(dest->getParentOp()); + } +}; + +struct TensorFlowLiteDialectFoldInterface : public DialectFoldInterface { + using DialectFoldInterface::DialectFoldInterface; + + // Registered hook to check if the given region, which is attached to an + // operation that is *not* isolated from above (i.e. no internal regions + // reference values defined in an enclosing region), should be used when + // materializing constants. + // In the TFLite dialect we materialize inside a while regions as slightly + // more efficient computationally. + bool shouldMaterializeInto(Region* region) const final { + return isa(region->getParentOp()); + } +}; + +void TFLDialect::printType(Type type, DialectAsmPrinter& os) const { + if (mlir::isa(type)) { + os << "control"; + return; + } + os << ""; +} + +Type TFLDialect::parseType(DialectAsmParser& parser) const { + StringRef data_type; + if (parser.parseKeyword(&data_type)) return Type(); + if (data_type == "control") return ControlType::get(getContext()); + parser.emitError(parser.getNameLoc()) << "unknown TFL type: " << data_type; + return nullptr; +} + +void TFLDialect::initialize() { + addOperations< +#define GET_OP_LIST +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.cc.inc" + >(); + addAttributes< +#define GET_ATTRDEF_LIST +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops_attrdefs.cc.inc" + >(); + addInterfaces(); + addTypes(); +} + +//===----------------------------------------------------------------------===// +// Common support logic +//===----------------------------------------------------------------------===// + +namespace { + +// Returns true if the dimensions in `a` is a suffix of the ones in `b`. +// For example, dimensions {2}, {1, 2}, and {3, 1, 2} are all suffixes to +// {5, 4, 3, 1, 2}, while {1}, {5, 4}, and {1, 3, 2} are all not. +inline bool IsTrailingDimensions(ArrayRef a, ArrayRef b) { + if (a.size() > b.size()) return false; + + return std::equal(a.rbegin(), a.rend(), b.rbegin()); +} + +// Returns true if it is a shaped type of f32 elements. +inline bool IsF32ShapedType(Type t) { + if (auto shaped_type = mlir::dyn_cast_or_null(t)) { + return shaped_type.getElementType().isF32(); + } + return false; +} + +// Returns true if it is a shaped type of f16 elements. +inline bool IsF16ShapedType(Type t) { + if (auto shaped_type = mlir::dyn_cast_or_null(t)) { + return shaped_type.getElementType().isF16(); + } + return false; +} + +// Returns true if it is a shaped type of bf16 elements. +inline bool IsBF16ShapedType(Type t) { + if (auto shaped_type = mlir::dyn_cast_or_null(t)) { + return shaped_type.getElementType().isBF16(); + } + return false; +} + +// Returns true if it is a shaped type of FloatType elements. +inline bool IsFloatShapedType(Type t) { + if (auto shaped_type = mlir::dyn_cast_or_null(t)) { + return mlir::isa(shaped_type.getElementType()); + } + return false; +} + +// Returns new shape with rank 'new_dims' with padded ones on the +// left if needed. +inline std::vector GetPaddedShape(ArrayRef old_shape, + int new_dims) { + std::vector new_shape(new_dims, 1); + std::copy_backward(old_shape.begin(), old_shape.end(), new_shape.end()); + return new_shape; +} + +// Helper method that given and 'current_index' representing +// index in broadcasted tensor, get the index in the flat original tensor. +// 'shape' is the original shape with padding to match result shape. +int64_t GetElementIndex(const std::vector& shape, + const std::vector& current_index) { + int64_t ind = 0; + int64_t mul = 1; + for (int i = shape.size() - 1; i >= 0; --i) { + ind += (current_index[i] % shape[i]) * mul; + mul *= shape[i]; + } + return ind; +} + +// Helper method that increment index represented in 'current_index_ptr' +// in the shape of 'result_shape'. +void IncrementIndex(ArrayRef result_shape, + std::vector* current_index_ptr) { + std::vector& current_index = *current_index_ptr; + for (int i = result_shape.size() - 1; i >= 0; --i) { + current_index[i]++; + if (current_index[i] == result_shape[i]) { + current_index[i] = 0; + } else { + break; + } + } +} + +/// Performs const folding `calculate` with broadcast behavior on the two +/// attributes `operand1` and `operand2` and returns the result if possible. +/// This function assumes the both operands are verified to have value +/// attributes of broadcastable types. +template > +Attribute ConstFoldBinaryOpDenseElements(ShapedType result_type, + DenseElementsAttr lhs, + DenseElementsAttr rhs, + const CalculationT& calculate) { + auto type = llvm::dyn_cast_or_null( + OpTrait::util::getBroadcastedType(lhs.getType(), rhs.getType())); + if (!type) { + return {}; + } + + type = type.clone(result_type.getElementType()); + + const bool rhs_is_splat = rhs.isSplat(); + const bool lhs_is_splat = lhs.isSplat(); + + auto lhs_values = lhs.try_value_begin(); + auto rhs_values = rhs.try_value_begin(); + if (failed(lhs_values) || failed(rhs_values)) { + return {}; + } + + // If both of them are splat, compute and return. + if (lhs_is_splat && rhs_is_splat) { + return DenseElementsT::get( + type, calculate(*lhs_values.value(), *rhs_values.value())); + } + + auto num_elements = type.getNumElements(); + + SmallVector new_values; + new_values.reserve(num_elements); + const auto result_shape = type.getShape(); + std::vector current_index(type.getRank(), 0); + + // Create the new shape with ones padded to the left. + const auto lhs_new_shape = + GetPaddedShape(lhs.getType().getShape(), type.getRank()); + const auto rhs_new_shape = + GetPaddedShape(rhs.getType().getShape(), type.getRank()); + + // Add each pair of the corresponding values in the dense elements + // attributes. + for (int64_t i = 0; i < num_elements; ++i) { + // current_index represents the index + // in the N-dimension tensor. GetElementIndex returns + // the index in the flat representation of the original tensor + // to use. + const int64_t lhs_index = + lhs_is_splat ? 0 : GetElementIndex(lhs_new_shape, current_index); + const int64_t rhs_index = + rhs_is_splat ? 0 : GetElementIndex(rhs_new_shape, current_index); + + new_values.push_back(calculate(*(lhs_values.value() + lhs_index), + *(rhs_values.value() + rhs_index))); + IncrementIndex(result_shape, ¤t_index); + } + return DenseElementsT::get(type, new_values); +} + +template +void RunBinaryCalculation(llvm::ArrayRef lhs_data, + llvm::ArrayRef rhs_data, + llvm::MutableArrayRef result_data, + ShapedType lhs_type, ShapedType rhs_type, + ShapedType result_type, bool lhs_is_splat, + bool rhs_is_splat, const CalculationT& calculate) { + const auto result_shape = result_type.getShape(); + std::vector current_index(result_type.getRank(), 0); + auto num_elements = result_type.getNumElements(); + auto element_type = result_type.getElementType(); + auto bit_width = element_type.getIntOrFloatBitWidth(); + bool is_signed = element_type.isSignedInteger(); + + // Create the new shape with ones padded to the left. + const auto lhs_shape = + GetPaddedShape(lhs_type.getShape(), result_type.getRank()); + const auto rhs_shape = + GetPaddedShape(rhs_type.getShape(), result_type.getRank()); + + // Add each pair of the corresponding values in the dense elements + // attributes. + for (int64_t result_index = 0; result_index < num_elements; ++result_index) { + // current_index represents the index + // in the N-dimension tensor. GetElementIndex returns + // the index in the flat representation of the original tensor + // to use. + const int64_t lhs_index = + lhs_is_splat ? 0 : GetElementIndex(lhs_shape, current_index); + const int64_t rhs_index = + rhs_is_splat ? 0 : GetElementIndex(rhs_shape, current_index); + + if constexpr (std::is_same_v) { + result_data[result_index] = + calculate(APFloat(lhs_data[lhs_index]), APFloat(rhs_data[rhs_index])) + .convertToFloat(); + } else { + result_data[result_index] = + calculate(APInt(bit_width, lhs_data[lhs_index], is_signed), + APInt(bit_width, rhs_data[rhs_index], is_signed)) + .getSExtValue(); + } + + IncrementIndex(result_shape, ¤t_index); + } +} + +int64_t GetSizeInBytes(Type type) { + if (auto shapedType = dyn_cast(type)) + return shapedType.getNumElements() * + GetSizeInBytes(shapedType.getElementType()); + + if (type.isIntOrFloat()) + return std::max(type.getIntOrFloatBitWidth(), (unsigned)8) / 8; + + if (auto complexType = dyn_cast(type)) + return GetSizeInBytes(complexType.getElementType()) * 2; + + return 0; +} + +template +Attribute ConstFoldUnaryResource(DenseResourceElementsAttr data, + llvm::function_ref calculate) { + ArrayRef input_data = GetValues(data); + auto fold_result_blob = + mlir::HeapAsmResourceBlob::allocate(GetSizeInBytes(data.getType()), + /*align=*/64, + /*dataIsMutable=*/true); + ArrayRef new_data = fold_result_blob.getDataAs(); + llvm::MutableArrayRef result_data = mlir::MutableArrayRef( + const_cast(new_data.data()), new_data.size()); + for (size_t i = 0; i < input_data.size(); ++i) { + result_data[i] = calculate(input_data[i]); + } + return DenseResourceElementsAttr::get(data.getType(), "unary_fold_result", + std::move(fold_result_blob)); +} + +/// Performs const folding `calculate` with broadcast behavior on the two +/// attributes `operand1` and `operand2` and returns the result if possible. +/// This function assumes the both operands are verified to have value +/// attributes of broadcastable types. +template > +Attribute ConstFoldBinaryOpDenseResources(ShapedType result_type, + ElementsAttr lhs, ElementsAttr rhs, + bool lhs_has_one_use, + bool rhs_has_one_use, + const CalculationT& calculate) { + // Get the broadcasted type. Return if both the inputs are not compatible to + // form a valid output type. + auto type = llvm::dyn_cast_or_null( + OpTrait::util::getBroadcastedType(lhs.getType(), rhs.getType())); + if (!type) return {}; + + bool lhs_is_resource = llvm::isa(lhs); + bool rhs_is_resource = llvm::isa(rhs); + + type = type.clone(result_type.getElementType()); + + // Flags to check if we can rewrite the LHS or RHS. + bool can_rewrite_lhs = + lhs_is_resource && + (lhs.getShapedType().getShape() == result_type.getShape()) && + lhs_has_one_use; + bool can_rewrite_rhs = + rhs_is_resource && + (rhs.getShapedType().getShape() == result_type.getShape()) && + rhs_has_one_use; + + ArrayRef lhs_data = GetValues(lhs); + ArrayRef rhs_data = GetValues(rhs); + + if (can_rewrite_lhs) { + auto lhs_dense_resource_attr = llvm::cast(lhs); + auto lhs_data_handle = lhs_dense_resource_attr.getRawHandle(); + AsmResourceBlob* lhs_blob = lhs_data_handle.getBlob(); + if (!lhs_blob) return {}; + + RunBinaryCalculation( + lhs_data, rhs_data, + mlir::MutableArrayRef(const_cast(lhs_data.data()), + lhs_data.size()), + lhs.getShapedType(), rhs.getShapedType(), type, lhs.isSplat(), + rhs.isSplat(), calculate); + + return DenseResourceElementsAttr::get( + result_type, "rewrite_lhs_binary_fold_result", std::move(*lhs_blob)); + } else if (can_rewrite_rhs) { + auto rhs_dense_resource_attr = llvm::cast(rhs); + auto rhs_data_handle = rhs_dense_resource_attr.getRawHandle(); + AsmResourceBlob* rhs_blob = rhs_data_handle.getBlob(); + if (!rhs_blob) return {}; + + RunBinaryCalculation( + lhs_data, rhs_data, + + mlir::MutableArrayRef(const_cast(rhs_data.data()), + rhs_data.size()), + lhs.getShapedType(), rhs.getShapedType(), type, lhs.isSplat(), + rhs.isSplat(), calculate); + + return DenseResourceElementsAttr::get( + result_type, "rewrite_rhs_binary_fold_result", std::move(*rhs_blob)); + } else { + // We can't rewrite the LHS or RHS. So create a new blob and write the + // result. + auto fold_result_blob = + mlir::HeapAsmResourceBlob::allocate(GetSizeInBytes(type), + /*align=*/64, + /*dataIsMutable=*/true); + ArrayRef new_data = fold_result_blob.getDataAs(); + RunBinaryCalculation( + lhs_data, rhs_data, + mlir::MutableArrayRef(const_cast(new_data.data()), + new_data.size()), + lhs.getShapedType(), rhs.getShapedType(), type, lhs.isSplat(), + rhs.isSplat(), calculate); + + return DenseResourceElementsAttr::get(result_type, "binary_fold_result", + std::move(fold_result_blob)); + } +} + +/// Performs const folding `calculate` with broadcast behavior on the two +/// attributes `operand1` and `operand2` and returns the result if possible. +/// This function assumes the two operands are verified to have value +/// attributes of broadcastable types. +template > +Attribute ConstFoldBinaryOp(ShapedType result_type, Attribute operand1, + Attribute operand2, const CalculationT& calculate) { + if (mlir::dyn_cast_or_null(operand1) && + mlir::dyn_cast_or_null(operand2)) { + return ConstFoldBinaryOpDenseElements( + result_type, mlir::cast(operand1), + mlir::cast(operand2), calculate); + } + + // TODO: support other attribute kinds + + return {}; +} + +/// Performs const folding with broadcast behavior on the two attributes in +/// `operands` and returns the result if possible. +/// Depending on the given `resultType`, either `floatCalculate` or +/// `intCalculate` is chosen to conduct the calculate. +Attribute ConstFoldBinaryOp( + ShapedType type, OperandRange operands, + llvm::function_ref float_calculate, + llvm::function_ref int_calculate) { + ElementsAttr lhs_elements, rhs_elements; + if (!matchPattern(operands[0], m_Constant(&lhs_elements)) || + !matchPattern(operands[1], m_Constant(&rhs_elements))) { + return {}; + } + auto element_type = type.getElementType(); + if ((lhs_elements && mlir::isa(lhs_elements)) || + (rhs_elements && mlir::isa(rhs_elements))) { + if (mlir::isa(element_type)) { + auto bit_width = element_type.getIntOrFloatBitWidth(); + switch (bit_width) { + case 32: + return ConstFoldBinaryOpDenseResources< + DenseResourceElementsAttr, float, float, + llvm::function_ref>( + type, lhs_elements, rhs_elements, operands[0].hasOneUse(), + operands[1].hasOneUse(), float_calculate); + default: + return {}; + } + } + + if (element_type.isSignlessInteger()) { + auto bit_width = element_type.getIntOrFloatBitWidth(); + switch (bit_width) { + case 32: + return ConstFoldBinaryOpDenseResources< + DenseResourceElementsAttr, uint32_t, uint32_t, + llvm::function_ref>( + type, lhs_elements, rhs_elements, operands[0].hasOneUse(), + operands[1].hasOneUse(), int_calculate); + default: + return {}; + } + } + + } else { + if (mlir::isa(element_type)) + return ConstFoldBinaryOp( + type, lhs_elements, rhs_elements, float_calculate); + + if (element_type.isSignlessInteger()) + return ConstFoldBinaryOp( + type, lhs_elements, rhs_elements, int_calculate); + } + + return {}; +} + +/// Performs const folding a attributes `operand` and returns the result if +/// possible. +/// The function currently asserts that the `result_type` to be a f32 tensor +/// type. +/// TODO: Extend this function to handle integral tensor for ops like +/// "tfl.logical_not". +Attribute ConstFoldUnaryOp(Type result_type, Attribute operand, + llvm::function_ref calculate) { + assert(IsF32ShapedType(result_type) || IsF16ShapedType(result_type) || + IsBF16ShapedType(result_type)); + auto result_shape_type = mlir::cast(result_type); + + if (!result_shape_type.hasStaticShape()) return {}; + + auto elements = mlir::dyn_cast_or_null(operand); + if (!elements) return {}; + + if (auto dense_elements = mlir::dyn_cast(elements)) { + SmallVector new_values; + const int num_elements = result_shape_type.getNumElements(); + new_values.reserve(num_elements); + + for (const APFloat& old_value : dense_elements.getValues()) { + new_values.push_back(calculate(old_value)); + } + return DenseElementsAttr::get(result_shape_type, new_values); + } else if (result_shape_type.getElementType().isF32()) { + if (auto res_attr = mlir::dyn_cast(elements)) { + return ConstFoldUnaryResource(res_attr, [&](float f) -> float { + return calculate(APFloat(f)).convertToFloat(); + }); + } + } + + return {}; +} + +void buildComparisonBinOp(Builder* builder, OperationState& result, Value lhs, + Value rhs) { + auto result_type = + OpTrait::util::getBroadcastedType(lhs.getType(), rhs.getType()); + if (!result_type) + emitError(result.location) + << "non-broadcastable operands: " << lhs.getType() << " and " + << rhs.getType(); + result.addOperands({lhs, rhs}); + // Comparison binary ops always return i1 tensor. + if (auto shaped_type = mlir::dyn_cast(result_type)) { + auto result_shape = shaped_type.getShape(); + result.types.push_back(tensorflow::GetTypeFromTFTensorShape( + result_shape, builder->getI1Type())); + } else { + result.types.push_back(UnrankedTensorType::get(builder->getI1Type())); + } +} + +void buildFusedBroadcastableBinOp(Builder* builder, OperationState& result, + Value lhs, Value rhs, + StringAttr fused_activation_function) { + auto result_type = + OpTrait::util::getBroadcastedType(lhs.getType(), rhs.getType()); + + if (!result_type) + emitError(result.location) + << "non-broadcastable operands: " << lhs.getType() << " and " + << rhs.getType(); + + result.addOperands({lhs, rhs}); + result.addAttribute("fused_activation_function", fused_activation_function); + result.types.push_back(result_type); +} + +} // end anonymous namespace + +//===----------------------------------------------------------------------===// +// AddOp +//===----------------------------------------------------------------------===// + +OpFoldResult AddOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + auto operands = adaptor.getOperands(); + // TODO(b/142478136): Handle fused ops. + if (getFusedActivationFunction() != "NONE") return {}; + + if (llvm::isa(getType().getElementType())) { + // Quantized folding not supported for the following. + return {}; + } + + auto lhs_elements = mlir::dyn_cast_or_null(operands[0]); + auto rhs_elements = mlir::dyn_cast_or_null(operands[1]); + + auto is_zero = [](Attribute a) { + return matchPattern(a, m_Zero()) || matchPattern(a, m_AnyZeroFloat()); + }; + + if (lhs_elements && is_zero(lhs_elements) && + getRhs().getType() == getType()) { + return getRhs(); + } + + if (rhs_elements && is_zero(rhs_elements) && + getLhs().getType() == getType()) { + return getLhs(); + } + + if (lhs_elements && rhs_elements) { + return ConstFoldBinaryOp( + getType(), getOperands(), [](APFloat a, APFloat b) { return a + b; }, + [](APInt a, APInt b) { return a + b; }); + } + + return {}; +} + +int64_t AddOp::GetArithmeticCount(Operation* op) { + int64_t count; + if (ArithmeticCountUtilHelper::GetFirstOutputCount(op, &count)) return count; + + return -1; +} + +//===----------------------------------------------------------------------===// +// CeilOp +//===----------------------------------------------------------------------===// + +OpFoldResult CeilOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + auto operands = adaptor.getOperands(); + auto result_type = getType(); + if (!IsF32ShapedType(result_type)) return {}; + + auto compute = [](APFloat value) -> APFloat { + float f = value.convertToFloat(); + float result = std::ceil(f); + return APFloat(result); + }; + + return ConstFoldUnaryOp(result_type, operands[0], compute); +} + +//===----------------------------------------------------------------------===// +// FloorOp +//===----------------------------------------------------------------------===// + +OpFoldResult FloorOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + auto operands = adaptor.getOperands(); + auto result_type = getType(); + if (!IsF32ShapedType(result_type)) return {}; + + auto compute = [](APFloat value) -> APFloat { + float f = value.convertToFloat(); + float result = std::floor(f); + return APFloat(result); + }; + + return ConstFoldUnaryOp(result_type, operands[0], compute); +} + +//===----------------------------------------------------------------------===// +// BitwiseXorOp +//===----------------------------------------------------------------------===// + +OpFoldResult BitwiseXorOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + auto compute = [](APInt lhs, APInt rhs) -> APInt { + lhs ^= rhs; + return lhs; + }; + + return ConstFoldBinaryOp( + getType(), adaptor.getLhs(), adaptor.getRhs(), compute); +} + +//===----------------------------------------------------------------------===// +// ExpOp +//===----------------------------------------------------------------------===// + +OpFoldResult ExpOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + auto operands = adaptor.getOperands(); + auto result_type = getType(); + if (!IsF32ShapedType(result_type)) return {}; + + auto compute = [](APFloat value) -> APFloat { + float f = value.convertToFloat(); + float result = std::exp(f); + return APFloat(result); + }; + + return ConstFoldUnaryOp(result_type, operands[0], compute); +} + +//===----------------------------------------------------------------------===// +// LogicalNotOp +//===----------------------------------------------------------------------===// + +OpFoldResult LogicalNotOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + auto elements = mlir::dyn_cast_or_null(adaptor.getLhs()); + if (!elements) { + return {}; + } + + if (auto data = llvm::dyn_cast(elements)) { + auto compute = [](bool value) { return !value; }; + return DenseIntElementsAttr::get( + data.getType(), + llvm::to_vector(llvm::map_range(data.getValues(), compute))); + } else if (auto data = mlir::dyn_cast(elements)) { + if (data.getElementType().isInteger(1)) { + return ConstFoldUnaryResource( + data, [](uint8_t b) -> uint8_t { return !static_cast(b); }); + } + } + return {}; +} + +//===----------------------------------------------------------------------===// +// ConcatenationOp +//===----------------------------------------------------------------------===// +// TODO(ashwinm): Implement shape inference for Concatenation + +namespace { + +int64_t GetConcatenationOpAxis(ConcatenationOp op) { + auto output_type = mlir::cast(op.getOutput().getType()); + int32_t axis = op.getAxis(); + if (axis < 0) axis += output_type.getRank(); + return axis; +} + +// Verify operand types and the result type: +// +// 1. Operand type ranks must be equal to the output type rank. +// +// 2. Operand dimension sizes (except dimension `axis`) must be equal to +// previously seen dimension sizes of the same dimension. +// +// 3. Sum of operand dimension sizes of the `axis` dimension must be equal to +// the dimension size of the `axis` dimension of output. +// +// Note: If an operand has unranked tensor type or has dynamic dimension size, +// those dimensions will be skipped. +LogicalResult VerifyConcatenationOpTypes(Operation* op, + RankedTensorType output_type, + ArrayRef operand_types, + int64_t axis) { + const int64_t output_rank = output_type.getRank(); + + SmallVector result_dim_sizes_loc(output_rank, + ShapedType::kDynamic); + SmallVector result_dim_sizes(output_type.getShape().begin(), + output_type.getShape().end()); + result_dim_sizes[axis] = 0; + + auto FormatLoc = [&result_dim_sizes_loc](int64_t dim) { + const int64_t loc = result_dim_sizes_loc[dim]; + if (loc == ShapedType::kDynamic) return std::string("output"); + return llvm::formatv("operand #{0}", loc).str(); + }; + + for (const auto& operand : llvm::enumerate(operand_types)) { + auto operand_type = mlir::dyn_cast(operand.value()); + if (!operand_type) { + result_dim_sizes[axis] = ShapedType::kDynamic; + continue; + } + + const int64_t operand_rank = operand_type.getRank(); + if (operand_rank != output_rank) + return op->emitOpError() << "rank of operand #" << operand.index() + << " must be equal to rank of output, expected " + << output_rank << ", got " << operand_rank; + + for (int64_t dim = 0; dim < output_rank; ++dim) { + const int64_t operand_dim_size = operand_type.getDimSize(dim); + const int64_t result_dim_size = result_dim_sizes[dim]; + + if (dim == axis) { + if (ShapedType::isDynamic(operand_dim_size) || + ShapedType::isDynamic(result_dim_size)) { + result_dim_sizes[axis] = ShapedType::kDynamic; + } else { + result_dim_sizes[axis] += operand_dim_size; + } + continue; + } + + if (ShapedType::isDynamic(operand_dim_size)) continue; + + if (ShapedType::isDynamic(result_dim_size)) { + result_dim_sizes[dim] = operand_dim_size; + result_dim_sizes_loc[dim] = operand.index(); + continue; + } + + if (result_dim_size != operand_dim_size) + return op->emitOpError() + << "dimension size of dimension #" << dim << " of operand #" + << operand.index() << " must be equal to " + << "dimension size of dimension #" << dim << " of " + << FormatLoc(dim) << ", expected " << result_dim_size << ", got " + << operand_dim_size; + } + } + + const int64_t output_concated_dim_size = output_type.getDimSize(axis); + if (!ShapedType::isDynamic(output_concated_dim_size) && + !ShapedType::isDynamic(result_dim_sizes[axis]) && + result_dim_sizes[axis] != output_concated_dim_size) + return op->emitOpError() + << "dimension size of dimension #" << axis << " of output " + << "must be equal to the sum of dimension sizes of dimension #" + << axis << ", expected " << result_dim_sizes[axis] << ", got " + << output_concated_dim_size; + + return success(); +} + +// Returns true when all operands are instances of DenseElementsAttr and the +// output type has a static shape. +bool IsConcatenationOpConstFoldable(ConcatenationOp op, + ArrayRef operands, + RankedTensorType output_type, + int64_t axis) { + if (operands.empty()) return false; + if (!output_type.hasStaticShape()) return false; + if (axis < 0) return false; + + return llvm::all_of(operands, [](Attribute operand) { + return operand && mlir::isa(operand); + }); +} + +DenseElementsAttr ConstFoldConcatenateOpDense(ArrayRef operands, + RankedTensorType output_type, + int64_t axis) { + const auto outer_dims = output_type.getShape().take_front(axis); + const int64_t outer_size = std::accumulate( + outer_dims.begin(), outer_dims.end(), 1, std::multiplies()); + + const auto base_inner_dims = output_type.getShape().drop_front(axis + 1); + const int64_t base_inner_size = + std::accumulate(base_inner_dims.begin(), base_inner_dims.end(), 1, + std::multiplies()); + + // Splits each input operand into outer_size pieces and combines them in + // round-robin ordering. + std::vector out_attrs(output_type.getNumElements()); + int64_t out = 0; + for (int64_t outer = 0; outer < outer_size; ++outer) { + for (auto op : operands) { + auto typed_attr = mlir::cast(op); + const int64_t dim_size = + mlir::cast(typed_attr.getType()).getDimSize(axis); + const int64_t inner_size = dim_size * base_inner_size; + + auto input_attrs = + mlir::cast(op).getValues(); + auto input_iter = input_attrs.begin() + outer * inner_size; + for (int64_t inner = 0; inner < inner_size; ++inner) + out_attrs[out++] = *input_iter++; + } + } + + return DenseElementsAttr::get(output_type, out_attrs); +} + +} // end anonymous namespace + +LogicalResult ConcatenationOp::verify() { + ConcatenationOp op = *this; + auto output_type = mlir::dyn_cast(op.getOutput().getType()); + + // If the output type is unranked, there is nothing else to be verified. + if (!output_type) return success(); + + const int64_t axis = GetConcatenationOpAxis(op); + if (axis < 0 || axis >= output_type.getRank()) + return op.emitOpError("concatenation dimension must be in [-rank, rank)"); + + SmallVector operand_types; + for (Value operand : op.getValues()) + operand_types.push_back(mlir::cast(operand.getType())); + + return VerifyConcatenationOpTypes(op.getOperation(), output_type, + operand_types, axis); +} + +OpFoldResult ConcatenationOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + auto operands = adaptor.getOperands(); + if (getFusedActivationFunction() == "NONE") { + if (auto output_type = + mlir::dyn_cast(getOutput().getType())) { + const int64_t axis = GetConcatenationOpAxis(*this); + if (IsConcatenationOpConstFoldable(*this, operands, output_type, axis)) + return ConstFoldConcatenateOpDense(operands, output_type, axis); + } + } + + // Remove all empty values. + SmallVector non_empty_values; + for (Value value : this->getValues()) { + const auto shaped_type = mlir::cast(value.getType()); + if (shaped_type.hasStaticShape() && shaped_type.getNumElements() == 0) { + continue; + } + non_empty_values.push_back(value); + } + + // All are not empty, do nothing. + if (non_empty_values.size() == getNumOperands()) return nullptr; + + // If only one input is non-empty, just return it as the result of folding. + if (non_empty_values.size() == 1) { + return non_empty_values[0]; + } + + // Otherwise, build a new concatenation op with non-empty values. + mlir::OpBuilder builder(getOperation()); + auto new_concat = TFL::ConcatenationOp::create( + builder, getLoc(), getType(), non_empty_values, + builder.getIntegerAttr(builder.getIntegerType(32), getAxis()), + builder.getStringAttr(getFusedActivationFunction())); + return new_concat.getResult(); +} + +//===----------------------------------------------------------------------===// +// CustomOp +//===----------------------------------------------------------------------===// + +// TODO(b/241745316): Confirm that this is always valid +mlir::LogicalResult CustomOp::verify() { + // Currently, this is always valid as it is a wrapper around a StringRef of 0 + // or more characters. + return success(); +} + +//===----------------------------------------------------------------------===// +// CustomTfOp +//===----------------------------------------------------------------------===// + +LogicalResult CustomTfOp::inferReturnTypes( + MLIRContext*, std::optional location, ValueRange operands, + DictionaryAttr attr, PropertyRef properties, RegionRange regions, + SmallVectorImpl& inferredReturnTypes) { + CustomTfOpAdaptor op(operands, attr, properties, regions); + + if (op.getRegions().empty()) return success(); + auto* real_op = &op.getBody().front().front(); + if (llvm::isa(real_op)) { + Value input = *operands.begin(); + inferredReturnTypes.assign({input.getType()}); + } + return success(); +} + +bool CustomTfOp::isCompatibleReturnTypes(TypeRange lhs, TypeRange rhs) { + if (lhs.empty()) return true; + if (lhs.size() != rhs.size() || lhs.size() != 1) return false; + if (failed(mlir::verifyCompatibleShape(lhs[0], rhs[0]))) return false; + return true; +} + +//===----------------------------------------------------------------------===// +// Gather op +//===----------------------------------------------------------------------===// + +LogicalResult GatherOp::verify() { + GatherOp op = *this; + ShapedType params_type = mlir::cast(op.getParams().getType()); + // TFLite gather kernel supports 1D string input only. + if (isa(params_type.getElementType())) { + if (params_type.hasRank() && params_type.getRank() != 1) { + return op.emitOpError( + "expect 1d input when the given type is string, got ") + << params_type; + } + } + return mlir::success(); +} + +OpFoldResult GatherOp::fold(GatherOp::FoldAdaptor adaptor) { + // Don't fold when the tensor we gather from (params) has non-int/float/index + // type. E.g. in case of quantized type, casting to DenseElementsAttr will + // strip the quantization information and cause type mismatch problems. + if (!getType().getElementType().isIntOrIndexOrFloat()) { + return {}; + } + + // Get the params tensor type/shape/data + auto params = mlir::dyn_cast_or_null(adaptor.getParams()); + if (!params) { + return {}; + } + auto params_type = params.getType(); + auto params_shape = params_type.getShape(); + auto params_data = params.getValues(); + // Get the indices tensor type/shape/data + auto indices = + mlir::dyn_cast_or_null(adaptor.getIndices()); + if (!indices) { + return {}; + } + auto indices_type = indices.getType(); + auto indices_shape = indices_type.getShape(); + auto indices_data = indices.getValues(); + // Get the axis value + int64_t axis = adaptor.getAxisAttr().getInt(); + if (axis < 0) { + axis += params_shape.size(); + } + // Get the batch_dims value + int64_t batch_dims = adaptor.getBatchDimsAttr().getInt(); + if (axis < 0 || axis >= params_shape.size()) { + return {}; + } + if (batch_dims < 0) { + batch_dims += params_shape.size(); + } + // Check the values are valid + if (batch_dims < 0) return {}; + if (batch_dims >= params_shape.size()) return {}; + if (batch_dims >= indices_shape.size()) return {}; + if (axis < batch_dims) return {}; + for (int i = 0; i < batch_dims; ++i) { + if (params_shape[i] != indices_shape[i]) return {}; + } + + // Figure out the result shape. It will have this structure: + // [batch dims.. , outer dims.., indexed dims.., inner dims..] + // Where: + // - batch dims are the first common batch_dims dimensions of params and + // indices + // - outer dims are dimensions of params from batch_dims to the indexed axis + // - indexed dims are all dimensions of indices + // - inner dims are the dimensions of params after the indexed axis + llvm::SmallVector result_shape; + // batch dims: + int64_t batch_flat_size = 1; + for (int64_t i = 0; i < batch_dims; ++i) { + batch_flat_size *= params_shape[i]; + result_shape.push_back(params_shape[i]); + } + // outer dims: + int64_t outer_flat_size = 1; + for (int64_t i = batch_dims; i < axis; ++i) { + outer_flat_size *= params_shape[i]; + result_shape.push_back(params_shape[i]); + } + // indexed dims: + const int64_t params_axis_size = params_shape[axis]; + int64_t indices_flat_size = 1; + for (int64_t i = batch_dims; i < indices_shape.size(); ++i) { + indices_flat_size *= indices_shape[i]; + result_shape.push_back(indices_shape[i]); + } + // inner dims: + int64_t inner_flat_size = 1; + for (int64_t i = axis + 1; i < params_shape.size(); ++i) { + inner_flat_size *= params_shape[i]; + result_shape.push_back(params_shape[i]); + } + // flat size of the params tensor (used by a check below): + int64_t params_flat_size = 1; + for (auto params_dim : params_shape) { + params_flat_size *= params_dim; + } + // flat size of the results tensor: + int64_t results_flat_size = 1; + for (auto result_dim : result_shape) { + results_flat_size *= result_dim; + } + // Result type is the params element type with the result shape + auto result_type = params_type.clone(result_shape); + + // Figure out the result value: + // Follow the reference TFLite kernel implementation at + // tensorflow/lite/kernels/internal/reference/gather.h + std::vector result_values; + result_values.reserve(results_flat_size); + for (int64_t batch = 0; batch < batch_flat_size; ++batch) { + for (int64_t outer = 0; outer < outer_flat_size; ++outer) { + for (int64_t indices_idx = 0; indices_idx < indices_flat_size; + ++indices_idx) { + int64_t params_idx = + indices_data[batch * indices_flat_size + indices_idx].getInt(); + int64_t from_pos = + (((batch * outer_flat_size) + outer) * params_axis_size + + params_idx) * + inner_flat_size; + + if (from_pos < 0 || from_pos + inner_flat_size > params_flat_size) { + return {}; + } + std::copy_n(params_data.begin() + from_pos, inner_flat_size, + std::back_inserter(result_values)); + } + } + } + return DenseElementsAttr::get(result_type, result_values); +} + +//===----------------------------------------------------------------------===// +// GatherNd op +//===----------------------------------------------------------------------===// + +OpFoldResult GatherNdOp::fold(GatherNdOp::FoldAdaptor adaptor) { + auto params = mlir::dyn_cast_or_null(adaptor.getParams()); + auto indices = + mlir::dyn_cast_or_null(adaptor.getIndices()); + + if (!params || !indices) { + return nullptr; + } + + auto params_type = params.getType(); + auto indices_type = indices.getType(); + auto params_shape = params_type.getShape(); + auto indices_shape = indices_type.getShape(); + + // The last dimension of 'indices' is the coordinate depth. + if (indices_shape.empty()) { + return nullptr; // Invalid indices shape. + } + int64_t index_depth = indices_shape.back(); + + // The index depth cannot exceed the rank of the params tensor. + assert(index_depth <= params_shape.size() && + "Index depth cannot be greater than params rank."); + + // Calculate the output shape. + // output_shape = indices_shape[:-1] + params_shape[index_depth:] + llvm::SmallVector output_shape; + output_shape.append(indices_shape.begin(), indices_shape.end() - 1); + output_shape.append(params_shape.begin() + index_depth, params_shape.end()); + + auto output_type = params_type.clone(output_shape); + + // Calculate the size of each slice we will gather. + int64_t slice_size = 1; + for (int64_t i = index_depth; i < params_shape.size(); ++i) { + slice_size *= params_shape[i]; + } + + // Prepare for the gathering logic. + auto params_values = params.getValues(); + auto indices_values_it = indices.getValues().begin(); + std::vector result_values; + result_values.reserve(output_type.getNumElements()); + + const int64_t num_indices = indices.getNumElements() / index_depth; + + // Implement the core gathering logic. + for (int64_t i = 0; i < num_indices; ++i) { + // For each output element, first find the N-d coordinate from 'indices'. + uint64_t linear_index = 0; + for (int64_t j = 0; j < index_depth; ++j) { + // Read the coordinate value and advance the iterator. + uint64_t coord = (*indices_values_it++).getZExtValue(); + + // Convert the N-d coordinate into a linear index for the slice. + // This is equivalent to: linear_index = (linear_index * params_shape[0] + + // c_0) * params_shape[1] + c_1 ... + linear_index = linear_index * params_shape[j] + coord; + } + + // The start of the slice in the flattened 'params' buffer. + uint64_t slice_start_offset = linear_index * slice_size; + + // Copy the entire slice into the result vector. + for (int64_t j = 0; j < slice_size; ++j) { + result_values.push_back(params_values[slice_start_offset + j]); + } + } + + return mlir::DenseElementsAttr::get(output_type, result_values); +} + +//===----------------------------------------------------------------------===// +// BroadcastToOp +//===----------------------------------------------------------------------===// + +// Canonicalizes BroadcastToOp to ReshapeOp if the input and output has the same +// number of elements. +struct ConvertBroadcastToReshape : public OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(BroadcastToOp op, + PatternRewriter& rewriter) const override { + auto input_type = mlir::cast(op.getInput().getType()); + auto output_type = mlir::cast(op.getType()); + if (!input_type.hasStaticShape() || !output_type.hasStaticShape() || + input_type.getNumElements() != output_type.getNumElements()) { + return failure(); + } + // Reshape op supports only new shape as I32. Add a cast op to I32 always + // to make sure the introduced Reshape Op is a valid one. + auto result_type = tensorflow::GetTypeFromTFTensorShape( + mlir::cast(op.getShape().getType()).getShape(), + rewriter.getI32Type()); + auto cast_op = + TFL::CastOp::create(rewriter, op->getLoc(), result_type, op.getShape()); + + rewriter.replaceOpWithNewOp(op, op.getType(), op.getInput(), + cast_op); + return success(); + } +}; + +void BroadcastToOp::getCanonicalizationPatterns(RewritePatternSet& results, + MLIRContext* context) { + results.add(context); +} + +//===----------------------------------------------------------------------===// +// BatchMatMulOp +//===----------------------------------------------------------------------===// + +// Verifier to check if following is true. Will perform the verification only if +// the BatchMatMulOp inputs and output are RankedTensors +// 1. The operands of a BatchMatMulOp are broadcast compatible +// 2. The result dimensions are correct +LogicalResult BatchMatMulOp::verify() { + BatchMatMulOp op = *this; + // batch size in lhs and rhs must be broadcastable + RankedTensorType x_ty = mlir::dyn_cast(op.getX().getType()); + RankedTensorType y_ty = mlir::dyn_cast(op.getY().getType()); + + if (!x_ty || !y_ty) return success(); + ArrayRef x_shape = x_ty.getShape(); + ArrayRef y_shape = y_ty.getShape(); + + llvm::SmallVector result_batch_shape; + llvm::ArrayRef x_batches = x_shape.drop_back(2); + llvm::ArrayRef y_batches = y_shape.drop_back(2); + + if (!OpTrait::util::getBroadcastedShape(x_batches, y_batches, + result_batch_shape)) { + return op.emitOpError() + << "found incompatible broadcast batch dimensions for lhs shape " + << x_ty << " and rhs shape " << y_ty; + } + + RankedTensorType output_ty = + mlir::dyn_cast(op.getOutput().getType()); + if (!output_ty) return success(); + + int64_t expected_output_rank = std::max(x_ty.getRank(), y_ty.getRank()); + if (output_ty.getRank() != expected_output_rank) { + return op.emitOpError() + << "found invalid output rank, expected " << expected_output_rank + << " but got " << output_ty.getRank(); + } + + // Check output batch dim with potential broadcasting. + ArrayRef output_shape = output_ty.getShape(); + for (int i = 0; i < result_batch_shape.size(); ++i) { + if (output_shape[i] != ShapedType::kDynamic && + result_batch_shape[i] != ShapedType::kDynamic && + output_shape[i] != result_batch_shape[i]) + return op.emitOpError() + << "has mismatching input batch dimension " + << result_batch_shape[i] << " and output batch dimension " + << output_shape[i]; + } + + // Check output shape for non-batch dimension, following documentation below. + // https://www.tensorflow.org/api_docs/cc/class/tensorflow/ops/batch-mat-mul + int64_t x_row_dim = x_shape[x_shape.size() - 2]; + int64_t x_col_dim = x_shape[x_shape.size() - 1]; + int64_t y_row_dim = y_shape[y_shape.size() - 2]; + int64_t y_col_dim = y_shape[y_shape.size() - 1]; + int64_t out_row_dim = output_shape[output_shape.size() - 2]; + int64_t out_col_dim = output_shape[output_shape.size() - 1]; + + int64_t expected_out_row_dim = op.getAdjX() ? x_col_dim : x_row_dim; + int64_t expected_out_col_dim = op.getAdjY() ? y_row_dim : y_col_dim; + + if (expected_out_row_dim != ShapedType::kDynamic && + out_row_dim != ShapedType::kDynamic && + out_row_dim != expected_out_row_dim) + return op.emitOpError() + << "found invalid output dimension on row, expected " + << expected_out_row_dim << " but got " << out_row_dim; + if (expected_out_col_dim != ShapedType::kDynamic && + out_col_dim != ShapedType::kDynamic && + out_col_dim != expected_out_col_dim) + return op.emitOpError() + << "found invalid output dimension on col, expected " + << expected_out_col_dim << " but got " << out_col_dim; + + return success(); +} + +//===----------------------------------------------------------------------===// +// FullyConnectedOp +//===----------------------------------------------------------------------===// + +LogicalResult FullyConnectedOp::verify() { + FullyConnectedOp op = *this; + ShapedType input_type = mlir::cast(op.getInput().getType()); + ShapedType filter_type = mlir::cast(op.getFilter().getType()); + if (filter_type.hasRank() && filter_type.getRank() != 2) { + return op.emitOpError("expect 2d filter, got ") << filter_type; + } + + if (!input_type.hasStaticShape() || !filter_type.hasStaticShape()) { + return mlir::success(); + } + + // Input's element size must be multiple of parameter's z_in dimension. + const int z_in = filter_type.getDimSize(1); + const int64_t num_input_elements = input_type.getNumElements(); + if (z_in != 0 && num_input_elements % z_in != 0) { + return op.emitOpError(llvm::formatv( + "expect 'input' num_elements % {0} == 0, got input type ", z_in)) + << input_type; + } + + // TODO(jpienaar): Include more shape verification for SHUFFLED4x16INT8 + // format. + if (op.getWeightsFormat() == "DEFAULT") { + ShapedType output_type = + mlir::cast((*op.getOutput().begin()).getType()); + if (!output_type.hasStaticShape()) { + return mlir::success(); + } + + const int64_t num_output_elements = output_type.getNumElements(); + const int z_out = filter_type.getDimSize(0); + if (num_output_elements % z_out != 0) { + return op.emitOpError(llvm::formatv( + "expect 'output' num_elements % {0} == 0, got ", z_out)) + << output_type; + } + + if (z_in != 0 && num_input_elements / z_in != num_output_elements / z_out) { + return op.emitOpError( + "num_input_elements / z_in != num_output_elements / z_out"); + } + } + + return mlir::success(); +} + +LogicalResult FullyConnectedOp::fold(FoldAdaptor adaptor, + SmallVectorImpl& results) { + assert(adaptor.getOperands().size() == 3); + + // Folding not implemented with any activation function or any weight type + // besides the default. + if (getFusedActivationFunction() != "NONE") return failure(); + if (getWeightsFormat() != "DEFAULT") return failure(); + + // Bias tensor is optional. + const bool has_bias = + !(!getBias() || mlir::isa(getBias().getType())); + + // Get the tensors. + auto operands = adaptor.getOperands(); + DenseElementsAttr input_tensor = + dyn_cast_or_null(operands[0]); + DenseElementsAttr weights_tensor = + dyn_cast_or_null(operands[1]); + DenseElementsAttr bias_tensor; + + if (!input_tensor || !weights_tensor) { + return failure(); + } + + if (has_bias) { + bias_tensor = dyn_cast_or_null(operands[2]); + if (!bias_tensor) return failure(); + } + + // Get the tensor types. + const auto input_type = mlir::cast(input_tensor.getType()); + const auto weights_type = mlir::cast(weights_tensor.getType()); + const auto bias_type = + has_bias ? mlir::cast(bias_tensor.getType()) : ShapedType{}; + + const auto output_type = mlir::cast(getType(0)); + + // Folding only implemented for float tensors. + if (!input_type.getElementType().isF32() || + !weights_type.getElementType().isF32() || + !output_type.getElementType().isF32() || + (has_bias && !bias_type.getElementType().isF32())) { + return failure(); + } + + // Folding only implemented for static shapes + if (!input_type.hasStaticShape() || !weights_type.hasStaticShape() || + (has_bias && !bias_type.hasStaticShape())) { + return failure(); + } + + if (weights_type.getRank() != 2) { + return failure(); + } + + const int64_t in_dim = weights_type.getDimSize(1); + const int64_t out_dim = weights_type.getDimSize(0); + + if (has_bias) { + if (bias_type.getRank() > 2 || + (bias_type.getRank() == 2 && bias_type.getDimSize(0) != 1) || + bias_type.getNumElements() != out_dim) { + return failure(); + } + } + + const int64_t batch_size = input_type.getNumElements() / in_dim; + + if (output_type.getNumElements() != batch_size * out_dim) { + return failure(); + } + + auto input_values_range = input_tensor.getValues(); + auto weights_values_range = weights_tensor.getValues(); + std::optional bias_values_range; + if (has_bias) bias_values_range = bias_tensor.getValues(); + + // Do the actual folding, one output at a time. + std::vector result_values; + result_values.reserve(batch_size * out_dim); + + for (int b = 0; b < batch_size; ++b) { + for (int o = 0; o < out_dim; ++o) { + // Dot product with Kahan/Neumaier summation to minimize numeric errors. + float sum = has_bias ? (*bias_values_range)[o] : 0.0f; + float compensation = 0.0f; + for (int i = 0; i < in_dim; ++i) { + const float addend = input_values_range[b * in_dim + i] * + weights_values_range[o * in_dim + i]; + const float new_sum = sum + addend; + // DO NOT enable -funsafe-math-optimizations here. + // There is a test detecting unsafe optimizations. + // Unsafe math optimizations can reorder float formulas, and set the + // compensation to constant 0. The formula must be evaluated as written + // for the algorithm to work. + // (Note: -ffast-math is a superset of -funsafe-math-optimizations.) + if (std::abs(sum) >= std::abs(addend)) { + compensation += (sum - new_sum) + addend; + } else { + compensation += (addend - new_sum) + sum; + } + sum = new_sum; + } + result_values.push_back(sum + compensation); + } + } + + // Set result tensor + const auto folded = + DenseElementsAttr::get(output_type, ArrayRef(result_values)); + results.assign({folded}); + + return success(); +} + +void FullyConnectedOp::getCanonicalizationPatterns(RewritePatternSet& results, + MLIRContext* context) { + results.add>(context); +} + +int64_t FullyConnectedOp::GetArithmeticCount(Operation* op) { + int64_t count; + if (ArithmeticCountUtilHelper::GetArithmeticCountForConvAndFullyconnectedOp( + op, &count)) + return count; + + return -1; +} + +//===----------------------------------------------------------------------===// +// Conv2DOp +//===----------------------------------------------------------------------===// + +void Conv2DOp::getCanonicalizationPatterns(RewritePatternSet& results, + MLIRContext* context) { + // TODO(b/180121750): Enable the pattern after the integration tests are + // fixed. + // results.add>(context); +} + +static LogicalResult ComputeConvWindowedOutputSize( + int64_t input_size, int64_t filter_size, int64_t dilation_rate, + int64_t stride, tensorflow::Padding padding, int64_t* output_size) { + int64_t pad_low; + int64_t pad_high; + + absl::Status status = tensorflow::GetWindowedOutputSizeVerbose( + input_size, filter_size, dilation_rate, stride, padding, output_size, + &pad_low, &pad_high); + // Return failure if expected_output_size could not be calculated. + if (!status.ok()) return failure(); + return success(); +} + +LogicalResult Conv2DOp::inferReturnTypes( + MLIRContext*, std::optional location, ValueRange operands, + DictionaryAttr attr, PropertyRef properties, RegionRange, + SmallVectorImpl& inferredReturnTypes) { + Conv2DOpAdaptor op(operands, attr, properties); + + const Value input = op.getInput(); + const Value filter = op.getFilter(); + + const RankedTensorType input_ty = + mlir::dyn_cast_or_null(input.getType()); + const RankedTensorType filter_ty = + mlir::dyn_cast_or_null(filter.getType()); + // If indeed both input type & filter type are ranked type and have ranks. + // We will need to check their ranks are valid. + if ((input_ty && input_ty.hasRank() && input_ty.getRank() != 4) || + (filter_ty && filter_ty.hasRank() && filter_ty.getRank() != 4)) { + return emitOptionalError(location, "Invalid ranks"); + } + + // If either input or filter is unranked, we will just return unranked output + // shape. + if (!input_ty || !filter_ty || !input_ty.hasRank() || !filter_ty.hasRank()) { + Type result_type; + result_type = UnrankedTensorType::get( + mlir::cast(input.getType()).getElementType()); + inferredReturnTypes.assign({result_type}); + return success(); + } + + auto stride_h = op.getStrideHAttr().getInt(); + auto stride_w = op.getStrideWAttr().getInt(); + auto dilation_h = op.getDilationHFactorAttr().getInt(); + auto dilation_w = op.getDilationWFactorAttr().getInt(); + + // We don't have EXPLICIT PADDING in TfLite. + auto paddings = op.getPadding(); + tensorflow::Padding padding; + auto padding_is_valid = GetPaddingFromString(paddings.str(), &padding); + if (!padding_is_valid.ok()) { + return emitOptionalError(location, "invalid padding format provided"); + } + + // Output always have rank 4. All dimensions are initialized to + // dynamic size and can be partially inferred. + // TFL's conv2d is always NHWC format & the filter is OHWI. + SmallVector return_shape(4, ShapedType::kDynamic); + return_shape[0] = input_ty.getDimSize(0); + return_shape[3] = filter_ty.getDimSize(0); + + // Spatial dimensions can be inferred only when both input and filter are + // ranked because we need to get their spatial dimensions. + + // Height. + if (!input_ty.isDynamicDim(1) && !filter_ty.isDynamicDim(1)) { + int64_t output_height; + if (failed(ComputeConvWindowedOutputSize( + input_ty.getDimSize(1), filter_ty.getDimSize(1), dilation_h, + stride_h, padding, &output_height))) { + return failure(); + } + return_shape[1] = output_height; + } + + // Width. + if (!input_ty.isDynamicDim(2) && !filter_ty.isDynamicDim(2)) { + int64_t output_width; + if (failed(ComputeConvWindowedOutputSize( + input_ty.getDimSize(2), filter_ty.getDimSize(2), dilation_w, + stride_w, padding, &output_width))) { + return failure(); + } + return_shape[2] = output_width; + } + + auto result_type = tensorflow::GetTypeFromTFTensorShape( + return_shape, input_ty.getElementType()); + + inferredReturnTypes.assign({result_type}); + return success(); +} + +bool Conv2DOp::isCompatibleReturnTypes(TypeRange lhs, TypeRange rhs) { + if (lhs.size() != rhs.size() || lhs.size() != 1) return false; + if (failed(mlir::verifyCompatibleShape(lhs[0], rhs[0]))) return false; + return true; +} + +int64_t Conv2DOp::GetArithmeticCount(Operation* op) { + int64_t count; + if (ArithmeticCountUtilHelper::GetArithmeticCountForConvAndFullyconnectedOp( + op, &count)) + return count; + + return -1; +} + +//===----------------------------------------------------------------------===// +// DepthwiseConv2DOp +//===----------------------------------------------------------------------===// + +void DepthwiseConv2DOp::getCanonicalizationPatterns(RewritePatternSet& results, + MLIRContext* context) { + // TODO(b/180121750): Enable the pattern after the integration tests are + // fixed. + // results.add>(context); +} + +int64_t DepthwiseConv2DOp::GetArithmeticCount(Operation* op) { + int64_t count; + if (ArithmeticCountUtilHelper::GetArithmeticCountForConvAndFullyconnectedOp( + op, &count)) + return count; + + return -1; +} + +//===----------------------------------------------------------------------===// +// GatherOp +//===----------------------------------------------------------------------===// + +static void BuildGatherOp(OpBuilder* builder, OperationState& result, + Value params, Value indices, IntegerAttr axis, + IntegerAttr batch_dims) { + auto params_type = mlir::cast(params.getType()); + auto indices_type = mlir::cast(indices.getType()); + + // If params/indices is unranked, then output is unranked. + if (!params_type.hasRank() || !indices_type.hasRank()) + return TFL::GatherOp::build( + *builder, result, UnrankedTensorType::get(params_type.getElementType()), + params, indices, axis, batch_dims); + + int64_t params_rank = params_type.getRank(); + int64_t indices_rank = indices_type.getRank(); + + // params rank is guaranteed to be at least 1. + // Produces an output tensor with shape: + // params.shape[:axis] + indices.shape + params.shape[axis + 1:] + std::vector shape(params_type.getShape()); + int64_t axis_i = axis.getInt(); + + // For neg axis values, we wrap around params, e.g. axis = -1 => params[:-1] + if (axis_i < 0) { + axis_i += params_rank; + } + + // params must be at least rank axis + 1 + if (params_rank < axis_i + 1) { + emitError(result.location, "params must be at least rank axis + 1"); + } + + int64_t batch_dims_i = batch_dims.getInt(); + if (batch_dims_i < 0) { + batch_dims_i += indices_rank; + } + + if (batch_dims_i > axis_i) { + emitError(result.location, + "axis should be bigger than or equal to batch_dims"); + } + if (batch_dims_i >= params_rank || batch_dims_i > indices_rank) { + emitError(result.location, + "batch_dims must be smaller than params' rank and smaller than " + "or equal to indices'rank"); + } + for (int i = 0; i < batch_dims_i; ++i) { + if (indices_type.getShape()[i] != params_type.getShape()[i]) { + emitError(result.location, + "batch dimensions of params must be equal to batch dimensions " + "of indices"); + } + } + + if ((indices_rank == 0) || (indices_rank == batch_dims_i)) { + // Scalar indices (output is rank(params) - 1). + // Erase shape[axis] + shape.erase(shape.begin() + axis_i); + } else if (indices_rank == 1) { + // Vector indices (output is rank(params)). + // Copy indices.shape into params.shape[axis] + std::copy(std::begin(indices_type.getShape()), + std::end(indices_type.getShape()), std::begin(shape) + axis_i); + } else { + // Higher rank indices (output is rank(params) + rank(indices) - 1). + shape.resize(params_rank + indices_rank - 1 - batch_dims_i); + // Copy params.shape[axis + 1: ] into shape[axis + indices_rank:] + std::copy(std::begin(params_type.getShape()) + axis_i + 1, + std::end(params_type.getShape()), + std::begin(shape) + axis_i + indices_rank - batch_dims_i); + + // Copy indices.shape into params.shape[axis] + std::copy(std::begin(indices_type.getShape()) + batch_dims_i, + std::end(indices_type.getShape()), std::begin(shape) + axis_i); + } + + TFL::GatherOp::build( + *builder, result, + tensorflow::GetTypeFromTFTensorShape(shape, params_type.getElementType()), + params, indices, axis, batch_dims); +} + +//===----------------------------------------------------------------------===// +// ScatterNdOp +//===----------------------------------------------------------------------===// + +mlir::LogicalResult ScatterNdOp::verify() { + ScatterNdOp op = *this; + auto indices = op.getIndices(); + auto updates = op.getUpdates(); + auto shape = op.getShape(); + auto output = op.getOutput(); + + auto updates_type = mlir::cast(updates.getType()); + auto indices_type = mlir::cast(indices.getType()); + + if (!indices_type.hasStaticShape() || !updates_type.hasStaticShape()) { + return success(); + } + + // Checks if the shape of `updates` is a tensor of shape + // `indices.shape[:-1] + shape[indices.shape[-1]:]`, as described in + // ScatterNd op description. + + auto outer_dims = indices_type.getRank() - 1; + auto outermost_dim = indices_type.getDimSize(outer_dims); + // Checks whether the first `outer_dims` dimensions of `indices` and + // `updates` are equal. + for (auto i = 0; i < outer_dims; i++) { + if (indices_type.getDimSize(i) != updates_type.getDimSize(i)) { + return op.emitOpError() + << "indices.Dims(" << i << ") == " << indices_type.getDimSize(i) + << ", but updates.Dims(" << i + << ") == " << updates_type.getDimSize(i); + } + } + + auto output_type = mlir::cast(output.getType()); + auto shape_type = mlir::cast(shape.getType()); + if (shape_type.hasStaticShape()) { + // Check the rank of `shape`. + auto output_rank = outermost_dim + updates_type.getRank() - outer_dims; + if (shape_type.getDimSize(0) != output_rank) { + return op.emitOpError() + << "shape must be a vector of length " << output_rank; + } + if (output_type.hasRank()) { + if (output_type.getRank() != output_rank) { + return op.emitOpError() + << "output must have the same rank with the length of shape = " + << output_rank; + } + } + } + + DenseIntElementsAttr shape_value; + if (matchPattern(shape, m_Constant(&shape_value))) { + for (const auto shape_elem : shape_value) { + if (shape_elem.getSExtValue() <= 0) { + return op.emitOpError("all elements of shape must be > 0"); + } + } + + // Checks whether the last `(shape_type.getDimSize(0) - outermost_dim)` + // dimensions of `updates` and `shape` are equal. + for (const auto& shape_it : llvm::enumerate(shape_value)) { + int64_t i = shape_it.index(); + auto value = shape_it.value().getSExtValue(); + if (i >= outermost_dim) { + auto corresponding_dim = i - outermost_dim + outer_dims; + if (value != updates_type.getDimSize(corresponding_dim)) { + return op.emitOpError() + << "updates.Dims(" << i + << ") == " << updates_type.getDimSize(corresponding_dim) + << ", but shape[" << i << "] == " << value; + } + } + } + + // Checks if the output has the shape specified by `shape`. + if (output_type.hasStaticShape()) { + for (const auto& shape_it : llvm::enumerate(shape_value)) { + int i = shape_it.index(); + auto value = shape_it.value().getSExtValue(); + if (output_type.getDimSize(i) != value) { + return op.emitOpError() + << "output shape [" << output_type.getShape() + << "] must be equal to the value of shape " << shape_value; + } + } + } + } + return success(); +} + +//===----------------------------------------------------------------------===// +// MulOp +//===----------------------------------------------------------------------===// + +OpFoldResult MulOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + auto operands = adaptor.getOperands(); + // TODO(b/142478136): Handle fused ops. + if (getFusedActivationFunction() != "NONE") return {}; + + auto is_zero = [](Attribute a) { + return matchPattern(a, m_Zero()) || matchPattern(a, m_AnyZeroFloat()); + }; + auto is_one = [](Attribute a) { + return matchPattern(a, m_One()) || matchPattern(a, m_OneFloat()); + }; + + // Quantized folding not supported. + const bool is_quantized = + llvm::isa(getType().getElementType()); + + auto lhs = llvm::dyn_cast_or_null(adaptor.getLhs()); + auto rhs = llvm::dyn_cast_or_null(adaptor.getRhs()); + + if (!is_quantized) { + if (lhs && is_zero(lhs)) { + auto lhs_type = getLhs().getType(); + // If the zero is a splat and has the same type as the output, return it. + // Otherwise, return a zero splat of the output type. + if (lhs_type == getType()) return getLhs(); + return DenseElementsAttr::get( + getType(), cast(lhs).getSplatValue()); + } + if (rhs && is_zero(rhs)) { + auto rhs_type = getRhs().getType(); + if (rhs_type == getType()) return getRhs(); + return DenseElementsAttr::get( + getType(), cast(rhs).getSplatValue()); + } + if (lhs && is_one(lhs) && getRhs().getType() == getType()) { + return getRhs(); + } + if (rhs && is_one(rhs) && getLhs().getType() == getType()) { + return getLhs(); + } + } + + // This function is performance critical for op fusion patterns, e.g. + // FuseBinaryOpToPrecedingAffine and FuseMulOrDivWithConv2dOrDepthwiseConv2d. + // So a few specializations are provided to evaluate the math operation + // more efficiently. + + if ((operands[0] && mlir::isa(operands[0])) && + (operands[1] && mlir::isa(operands[1]))) { + // Specialization for f32 type. + if (mlir::cast(getType()).getElementType().isF32()) { + return ConstFoldBinaryOp( + getType(), operands[0], operands[1], + [](float a, float b) { return a * b; }); + } + + // Specialization for bf16 type. + if (mlir::cast(getType()).getElementType().isBF16()) { + return ConstFoldBinaryOp( + getType(), operands[0], operands[1], + [](Eigen::bfloat16 a, Eigen::bfloat16 b) { return a * b; }); + } + } + + // Generic fallback with APFloat + return ConstFoldBinaryOp( + getType(), getOperands(), [](APFloat a, APFloat b) { return a * b; }, + [](APInt a, APInt b) { return a * b; }); +} + +int64_t MulOp::GetArithmeticCount(Operation* op) { + int64_t count; + if (ArithmeticCountUtilHelper::GetFirstOutputCount(op, &count)) return count; + + return -1; +} + +//===----------------------------------------------------------------------===// +// PowOp +//===----------------------------------------------------------------------===// + +OpFoldResult PowOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + if (getType().getElementType().isF32()) { + return ConstFoldBinaryOp( + getType(), adaptor.getLhs(), adaptor.getRhs(), + [](float lhs, float rhs) { return std::pow(lhs, rhs); }); + } + if (getType().getElementType().isInteger(32)) { + return ConstFoldBinaryOp( + getType(), adaptor.getLhs(), adaptor.getRhs(), + [](int32_t lhs, int32_t rhs) { return std::pow(lhs, rhs); }); + } + return {}; +} + +//===----------------------------------------------------------------------===// +// DivOp +//===----------------------------------------------------------------------===// + +OpFoldResult DivOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + // TODO(b/142478136): Handle fused ops. + if (getFusedActivationFunction() != "NONE") return {}; + + if (llvm::isa(getType().getElementType())) { + // Quantized folding not supported for the following. + return {}; + } + + auto rhs = llvm::dyn_cast_or_null(adaptor.getRhs()); + auto lhs = llvm::dyn_cast_or_null(adaptor.getLhs()); + + auto is_one = [](Attribute a) { + return matchPattern(a, m_One()) || matchPattern(a, m_OneFloat()); + }; + + if (rhs && is_one(rhs) && getLhs().getType() == getType()) { + return getLhs(); + } + + if (rhs && lhs) { + return ConstFoldBinaryOp( + getType(), getOperands(), [](APFloat a, APFloat b) { return a / b; }, + [](APInt a, APInt b) { return a.sdiv(b); }); + } + + return {}; +} + +int64_t DivOp::GetArithmeticCount(Operation* op) { + int64_t count; + if (ArithmeticCountUtilHelper::GetFirstOutputCount(op, &count)) return count; + + return -1; +} + +//===----------------------------------------------------------------------===// +// PackOp +//===----------------------------------------------------------------------===// + +// TODO(b/133486129): Implement shape inference for pack + +mlir::LogicalResult PackOp::verify() { + PackOp op = *this; + // TODO(antiagainst): Implement other checks as in + // tensorflow/lite/kernels/pack.cc + + if (op.getOperation()->getNumOperands() != op.getValuesCount()) + return op.emitOpError("input count should match 'values_count' attribute"); + + Value operand0 = op.getOperand(0); + auto input_type = mlir::cast(operand0.getType()); + + // Check axis bounds. + if (input_type.hasRank()) { + int32_t axis_value = op.getAxis(); + if (axis_value < 0) axis_value += input_type.getRank() + 1; + if (axis_value < 0 || axis_value >= input_type.getRank() + 1) + return op.emitOpError() + << "op attribute 'axis' should be in range [-rank - 1, rank + 1), " + << "got rank = " << input_type.getRank() + << ", and axis = " << op.getAxis(); + } + + // Make sure all inputs have the same shape and element type. + // TODO(b/135032063): Simplify once fixed. + for (Type operand_type : op.getOperandTypes()) { + if (failed(mlir::verifyCompatibleShape(input_type, operand_type))) + return op.emitOpError("operands should be of the same type. got ") + << input_type << ", " << operand_type; + } + + return success(); +} + +//===----------------------------------------------------------------------===// +// PReluOp +//===----------------------------------------------------------------------===// + +mlir::LogicalResult PReluOp::verify() { + PReluOp op = *this; + auto input_type = mlir::cast(op.getInput().getType()); + auto alpha_type = mlir::cast(op.getAlpha().getType()); + auto output_type = mlir::cast(op.getOutput().getType()); + + if (input_type.hasStaticShape() && alpha_type.hasStaticShape()) { + if (input_type.getRank() != alpha_type.getRank() + 1) { + return op.emitOpError("'alpha' should have one less rank than 'input'."); + } + + // Check if alpha is broadcastable + for (int i = 0; i < alpha_type.getRank(); i++) { + if (alpha_type.getDimSize(i) != input_type.getDimSize(i + 1) && + alpha_type.getDimSize(i) != 1) { + return op.emitOpError( + llvm::formatv("'alpha' is not broadcastable at dimension {0}.", i)); + } + } + } + + if (input_type.hasStaticShape() && output_type.hasStaticShape()) { + if (input_type.getRank() != output_type.getRank()) { + return op.emitOpError("'input' and 'output' should have the same rank."); + } + + // Check if input and output shapes are same + for (int i = 0; i < input_type.getRank(); i++) { + if (input_type.getDimSize(i) != output_type.getDimSize(i)) { + return op.emitOpError( + "'input' and 'output' should have the same shape."); + } + } + } + return success(); +} + +//===----------------------------------------------------------------------===// +// ReshapeOp +//===----------------------------------------------------------------------===// + +namespace { +// This pattern matches and merges a tfl.reshape under the following +// condition: +// * The input's defining op is another tfl.reshape. +// TODO(antiagainst): This pattern probably should be moved to the peephole +// category, after we have the infra for peephole passes. +struct RemoveAdjacentReshape : public RewritePattern { + explicit RemoveAdjacentReshape(MLIRContext* context) + : RewritePattern(ReshapeOp::getOperationName(), 1, context) {} + + LogicalResult matchAndRewrite(Operation* op, + PatternRewriter& rewriter) const override { + auto thisOp = cast(op); + auto prevOp = + dyn_cast_or_null(thisOp.getOperand(0).getDefiningOp()); + if (!prevOp) { + return failure(); + } + // Replace + // %1 = "tfl.reshape"(%0, %shape0) + // %2 = "tfl.reshape"(%1, %shape1) + // With + // %2 = "tfl.reshape"(%0, %shape1) + rewriter.replaceOpWithNewOp( + op, thisOp.getType(), prevOp.getOperand(0), thisOp.getOperand(1)); + return success(); + } +}; + +// The kernel expects an 1-D tensor for the shape operand if it presents. If all +// the dimensions are '1's except the last dimension, it will be reshaped to a +// 1-D tensor. +// Note that this pattern doesn't check or change the content of the shape +// tensor. +struct ConvertShapeTo1D : public OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(ReshapeOp reshape, + PatternRewriter& rewriter) const override { + if (!reshape.getShape().hasOneUse()) return failure(); + + DenseIntElementsAttr shape; + if (!matchPattern(reshape.getShape(), m_Constant(&shape))) { + return failure(); + } + // It is already a 1-D constant, no change. + auto old_shape = shape.getShapedType().getShape(); + if (old_shape.size() == 1) { + return failure(); + } + // Verify all the leading dimensions are length one, except the last one. + for (auto it = ++old_shape.rbegin(); it != old_shape.rend(); ++it) { + if (*it != 1) { + reshape->emitError( + "Non-vector shape input is used, might cause runtime error"); + return failure(); + } + } + auto new_shape = shape.reshape(tensorflow::GetTypeFromTFTensorShape( + {*old_shape.rbegin()}, shape.getShapedType().getElementType())); + rewriter.replaceOpWithNewOp( + reshape.getShape().getDefiningOp(), new_shape); + return success(); + } +}; + +bool InputOutputHasSameShape(mlir::Type input_type, mlir::Type output_type) { + auto input_shaped_type = mlir::dyn_cast_or_null(input_type); + if (!input_shaped_type || !input_shaped_type.hasStaticShape()) return false; + + auto output_shaped_type = mlir::dyn_cast_or_null(output_type); + if (!output_shaped_type || !output_shaped_type.hasStaticShape()) return false; + + return input_shaped_type == output_shaped_type; +} + +} // end anonymous namespace + +OpFoldResult ReshapeOp::fold(FoldAdaptor adaptor) { + auto operands = adaptor.getOperands(); + // Remove identity reshape with both static result and input shape. + auto result_type = mlir::cast(getType()); + auto input_type = mlir::cast(getOperand(0).getType()); + if (InputOutputHasSameShape(input_type, result_type)) return getInput(); + + // If the result type isn't static, tries to derive the result type from + // the #2 operand. + if (!result_type.hasStaticShape()) { + auto shape_elements = + mlir::dyn_cast_or_null(operands[1]); + if (!shape_elements) return nullptr; + + SmallVector shape_data; + for (const auto& it : shape_elements.getValues()) { + shape_data.push_back(it.getSExtValue()); + } + result_type = tensorflow::GetTypeFromTFTensorShape( + shape_data, input_type.getElementType()); + } + + // Constant folding + if (auto dense_elements = + mlir::dyn_cast_or_null(operands[0])) { + return dense_elements.reshape(result_type); + } else if (auto dense_resource_elements = + mlir::dyn_cast_or_null( + operands[0])) { + if (AsmResourceBlob* blob = + dense_resource_elements.getRawHandle().getBlob()) { + auto key = dense_resource_elements.getRawHandle().getKey(); + if (getInput().hasOneUse()) { + return DenseResourceElementsAttr::get(result_type, key, + std::move(*blob)); + } + auto new_blob = mlir::HeapAsmResourceBlob::allocate( + blob->getData().size(), /*align=*/64, true); + memcpy(const_cast(new_blob.getData().data()), + blob->getData().data(), blob->getData().size()); + return DenseResourceElementsAttr::get(result_type, key, + std::move(new_blob)); + } + } + + return nullptr; +} + +void ReshapeOp::getCanonicalizationPatterns(RewritePatternSet& results, + MLIRContext* context) { + results.add(context); +} + +using ReshapeErrorHandler = + llvm::function_ref; + +LogicalResult GetReshapeOutputType(Value input, Value shape, + ReshapeErrorHandler error_handler, + TensorType& output_ty) { + auto input_ty = mlir::cast(input.getType()); + auto element_ty = input_ty.getElementType(); + output_ty = UnrankedTensorType::get(element_ty); + + auto shape_ty = mlir::dyn_cast(shape.getType()); + if (!shape_ty) return success(); + if (shape_ty.getRank() != 1) + return error_handler(llvm::formatv( + "requires 'shape' to be rank 1, but got {0}", shape_ty.getRank())); + + DenseIntElementsAttr shape_attr; + if (!matchPattern(shape, m_Constant(&shape_attr))) { + // If only shape of `shape` is known, return ranked but dynamic output + // shape. + if (shape_ty.hasStaticShape()) { + llvm::SmallVector dynamic_shape(shape_ty.getDimSize(0), + ShapedType::kDynamic); + output_ty = + tensorflow::GetTypeFromTFTensorShape(dynamic_shape, element_ty); + } + return success(); + } + + // Detect if reshape output shape is folded. + bool shape_ty_zero_dim = false; + int unknown_index = -1; + // The product of constant shape argument excluding unknown dimension. + int64_t shape_ty_size = 1; + llvm::SmallVector output_ty_shape; + output_ty_shape.reserve(shape_attr.getNumElements()); + for (const auto& dim : llvm::enumerate(shape_attr.getValues())) { + const int64_t size = dim.value().getSExtValue(); + if (size == tensorflow::kTFDynamicSize || // NOLINT + size == ShapedType::kDynamic) { // NOLINT + if (unknown_index != -1) + return error_handler(llvm::formatv( + "requires 'shape' to have at most one dynamic dimension, but got " + "multiple dynamic dimensions at indices {0} and {1}. You need to " + "set up the unspecified size(s) to avoid this problem, for example," + "setting batch size in keras model or setting unspecified input " + "size(s) with fixed ones.", + unknown_index, dim.index())); + + unknown_index = dim.index(); + } else if (size == 0) { + shape_ty_zero_dim = true; + } else if (size > 0) { + shape_ty_size *= size; + } else { + return error_handler( + llvm::formatv("requires 'shape' to have dimensions greater than -1, " + "but got {0} at index {1}", + size, dim.index())); + } + output_ty_shape.push_back(size); + } + + if (!input_ty.hasStaticShape()) { + output_ty = + tensorflow::GetTypeFromTFTensorShape(output_ty_shape, element_ty); + return success(); + } + + // Compute the value of the unknown dimension. + if (unknown_index != -1) { + // Compute number of elements in tensor shape. + int64_t input_ty_size = 1; + bool input_ty_zero_dim = false; + for (const auto& dim : input_ty.getShape()) { + if (dim > 0 || !shape_ty_zero_dim) { + input_ty_size *= dim; + } else { + input_ty_zero_dim = true; + } + } + + const int64_t missing_dim = input_ty_size / shape_ty_size; + if (!input_ty_zero_dim && shape_ty_size * missing_dim != input_ty_size) + return error_handler( + llvm::formatv("requires 'input' number of elements be a multiple of " + "{0}, but got {1}", + shape_ty_size, input_ty_size)); + + // Set the unknown dimension such that total number of elements remain + // constant. + output_ty_shape[unknown_index] = missing_dim; + } + + if (quant::UniformQuantizedPerAxisType per_axis_quant = + dyn_cast_or_null(element_ty)) { + // Get the updated quantization dimension if the reshape op changes the + // quantization dimension. + + int32_t input_quant_dim = per_axis_quant.getQuantizedDimension(); + auto input_shape = mlir::cast(input.getType()).getShape(); + absl::StatusOr new_quant_dim = GetQuantDimensionAfterReshape( + input_shape, output_ty_shape, input_quant_dim); + if (!new_quant_dim.ok()) return failure(); + if (*new_quant_dim != input_quant_dim) { + // Use the default storage bound + quant::UniformQuantizedPerAxisType new_element_type = + mlir::quant::UniformQuantizedPerAxisType::getChecked( + input.getLoc(), per_axis_quant.getFlags(), + per_axis_quant.getStorageType(), + per_axis_quant.getExpressedType(), per_axis_quant.getScales(), + per_axis_quant.getZeroPoints(), *new_quant_dim, + per_axis_quant.getStorageTypeMin(), + per_axis_quant.getStorageTypeMax()); + + output_ty = RankedTensorType::getChecked(input.getLoc(), output_ty_shape, + new_element_type); + return success(); + } + } + output_ty = tensorflow::GetTypeFromTFTensorShape(output_ty_shape, element_ty); + + return success(); +} + +mlir::LogicalResult ReshapeOp::verify() { + ReshapeOp op = *this; + auto error_handler = [&op](const llvm::Twine& message) -> LogicalResult { + return op.emitOpError() << message; + }; + TensorType expected_ty; + if (failed(GetReshapeOutputType(op.getInput(), op.getShape(), error_handler, + expected_ty))) + return failure(); + + auto output_ty = mlir::dyn_cast(op.getType()); + if (!output_ty) return success(); + auto input_ty = mlir::cast(op.getInput().getType()); + if (output_ty.hasStaticShape() && input_ty.hasStaticShape()) { + const int64_t output_ty_size = output_ty.getNumElements(); + const int64_t input_ty_size = input_ty.getNumElements(); + if (input_ty_size != output_ty_size) + return op.emitOpError() << "requires 'output' number of elements to " + "match 'input' number of elements, but got " + << output_ty_size << " and " << input_ty_size; + } + + if (!TF::AreCastCompatible({output_ty, expected_ty})) + return op.emitOpError() + << "requires 'output' type " << output_ty + << " to be cast compatible with expected type " << expected_ty; + + return success(); +} + +LogicalResult ReshapeOp::inferReturnTypes( + MLIRContext* context, std::optional location, ValueRange operands, + DictionaryAttr attr, PropertyRef properties, RegionRange, + SmallVectorImpl& inferredReturnTypes) { + ReshapeOpAdaptor op(operands, attr, properties); + const Value input = op.getInput(); + const Value shape = op.getShape(); + + auto error_handler = [&](const llvm::Twine& message) -> LogicalResult { + // A dummy error handler. + // Errors when computing the output shape will be raised in + // ReshapeOp::verify call. + return failure(); + }; + TensorType output_type; + if (GetReshapeOutputType(input, shape, error_handler, output_type) + .succeeded()) { + inferredReturnTypes.assign({output_type}); + return success(); + } + Type result_type; + result_type = UnrankedTensorType::get( + mlir::cast(input.getType()).getElementType()); + inferredReturnTypes.assign({result_type}); + return success(); +} + +bool ReshapeOp::isCompatibleReturnTypes(TypeRange lhs, TypeRange rhs) { + if (lhs.size() != rhs.size() || lhs.size() != 1) return false; + if (failed(mlir::verifyCompatibleShape(lhs[0], rhs[0]))) return false; + return true; +} + +//===----------------------------------------------------------------------===// +// PackOp +//===----------------------------------------------------------------------===// + +// Remove redundant unpack pack op. +// If a unpack op is followed by a pack op, we can remove the pack op, if the +// unpack op is only consumed by the pack op, it will be removed as well. +// An example illustration is: +// Unpack [5, 8, 9], axis = 1 +// / \ +// value ... value [5, 9], 8 values in total +// \ / +// pack, axis = 1 +// | +// value [5, 8, 9] +// +// This can actually be simplified into just: +// +// => Value [5, 8, 9] +// TODO(b/133341698): Move to tablegen when variadic is supported. +struct RemoveRedundantUnpackPack : public RewritePattern { + explicit RemoveRedundantUnpackPack(MLIRContext* context) + : RewritePattern(PackOp::getOperationName(), 2, context) {} + + LogicalResult matchAndRewrite(Operation* op, + PatternRewriter& rewriter) const override { + TFL::PackOp pack_op = cast(op); + Operation* first_input = pack_op.getOperand(0).getDefiningOp(); + if (!first_input) return failure(); + auto input_unpack_op = dyn_cast_or_null(first_input); + if (!input_unpack_op) return failure(); + + // The unpack & pack should have the same axis & num inputs/outputs. + if (pack_op.getAxis() != input_unpack_op.getAxis() || + pack_op.getValuesCount() != input_unpack_op.getNum()) + return failure(); + + const int total_pack_inputs = pack_op.getNumOperands(); + const int num_results = input_unpack_op.getNumResults(); + if (total_pack_inputs != num_results) return failure(); + for (auto input_output : + llvm::zip(pack_op.getOperands(), input_unpack_op.getResults())) { + Value pack_input = std::get<0>(input_output); + Value unpack_output = std::get<1>(input_output); + // Make sure the ordering is the same for the pack op & unpack op. + if (pack_input != unpack_output) return failure(); + } + + // Replace the pack's output to the unpack's input. + rewriter.replaceOp(pack_op, input_unpack_op.getOperand()); + // At this point, we don't manually remove the redundant pack op & unpack op + // (we cannot actually), but trust the PatterRewriter to garbage collect + // these two ops. + return success(); + } +}; + +// Replace PackOp with a reshape when there is only one operand. +struct ReplacePackWithReshape : public RewritePattern { + explicit ReplacePackWithReshape(MLIRContext* context) + : RewritePattern(PackOp::getOperationName(), 2, context) {} + LogicalResult matchAndRewrite(Operation* op, + PatternRewriter& rewriter) const override { + TFL::PackOp pack_op = cast(op); + if (pack_op.getNumOperands() != 1) return failure(); + + Location loc = pack_op.getLoc(); + auto output_type = mlir::cast(pack_op.getType()); + if (!output_type.hasStaticShape()) return failure(); + + // This is to workaround the unnecessary cast i64 -> i32. + SmallVector new_shape_array; + for (auto size : output_type.getShape()) { + new_shape_array.push_back(ConvertToTfliteSize(size)); + } + + auto new_shape = TFL::ConstOp::create( + rewriter, loc, + DenseIntElementsAttr::get( + tensorflow::GetTypeFromTFTensorShape( + {static_cast(new_shape_array.size())}, + rewriter.getIntegerType(32)), + new_shape_array)); + + rewriter.replaceOpWithNewOp(op, output_type, + pack_op.getOperand(0), new_shape); + return success(); + } +}; + +void PackOp::getCanonicalizationPatterns(RewritePatternSet& results, + MLIRContext* context) { + results.add(context); +} + +//===----------------------------------------------------------------------===// +// SliceOp +//===----------------------------------------------------------------------===// + +llvm::SmallVector UnpackIndexOperand(Attribute operand) { + auto data = llvm::dyn_cast_or_null(operand); + if (!data) { + return {}; + } + + const bool is_i32 = data.getElementType().isSignlessInteger(32); + + if (data.isSplat()) { + int64_t splat_val; + + if (is_i32) { + splat_val = data.getSplatValue(); + } else { + splat_val = data.getSplatValue(); + } + + return llvm::SmallVector(data.getShapedType().getNumElements(), + splat_val); + } + + if (!is_i32) { + return llvm::to_vector(data.getValues()); + } + + return llvm::to_vector(llvm::map_range(data.getValues(), [](auto v) { + return static_cast(v); + })); +} + +OpFoldResult SliceOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + if (!getType().hasStaticShape()) { + return {}; + } + + auto input_type = getInput().getType(); + if (input_type == getType()) { + return getInput(); + } + + auto input = adaptor.getInput(); + if (!input) { + return {}; + } + auto begin = adaptor.getBegin(); + if (!begin) { + return {}; + } + auto size = adaptor.getSize(); + if (!size) { + return {}; + } + + auto begin_type = getBegin().getType(); + auto size_type = getSize().getType(); + + if (size_type.getRank() != 1 || begin_type.getRank() != 1) { + return {}; + } + + if (begin_type.getDimSize(0) != input_type.getRank() || + size_type.getDimSize(0) != input_type.getRank()) { + return {}; + } + + auto begin_vals = UnpackIndexOperand(begin); + auto size_vals = UnpackIndexOperand(size); + + if (size_vals != getType().getShape()) { + return {}; + } + + for (auto [begin, size, dim] : + llvm::zip(begin_vals, size_vals, input_type.getShape())) { + if (size < 0) { + // TODO: b/351437662 - Add support for this case. + return {}; + } + if (begin + size > dim) { + return {}; + } + } + + // TODO:(b/385000124) - Add support for dense resource. + // We need a way to get the input data from the resource as Attribute and also + // we need a way to create a new resource with the result data. + if (auto input_dense = dyn_cast(input)) { + std::vector input_data(input_dense.value_begin(), + input_dense.value_end()); + + const FlatIndHelper write_inds(getType()); + const FlatIndHelper read_inds(input_type); + + std::vector result_data(getType().getNumElements()); + + for (int w_flat_ind = 0; w_flat_ind < getType().getNumElements(); + ++w_flat_ind) { + auto write_shaped_ind = write_inds.GetShapedInd(w_flat_ind); + + FlatIndHelper::AddOffset(write_shaped_ind, begin_vals); + const auto read_flat_ind = read_inds.GetFlatInd(write_shaped_ind); + + result_data[w_flat_ind] = input_data[read_flat_ind]; + } + + return DenseElementsAttr::get(getType(), result_data); + } + return nullptr; +} + +mlir::LogicalResult SliceOp::verify() { + SliceOp op = *this; + auto input_type = mlir::cast(op.getInput().getType()); + auto begin_type = mlir::cast(op.getBegin().getType()); + auto size_type = mlir::cast(op.getSize().getType()); + if (input_type.hasStaticShape() && begin_type.hasStaticShape() && + size_type.hasStaticShape()) { + if (input_type.getRank() != begin_type.getNumElements()) { + return op.emitError( + "begin tensor elements size is not equal to input tensor rank"); + } + + if (input_type.getRank() != size_type.getNumElements()) { + return op.emitError( + "size tensor elements size is not equal to input tensor rank"); + } + } + + DenseIntElementsAttr begin; + if (matchPattern(op.getBegin(), m_Constant(&begin))) { + int axis = 0; + for (const auto& begin_i : llvm::enumerate(begin)) { + if (begin_i.value().getSExtValue() < 0) { + return op.emitError( + llvm::formatv("begin[{0}] cannot be negative", axis)); + } + axis++; + } + } + + DenseIntElementsAttr size; + if (matchPattern(op.getSize(), m_Constant(&size))) { + int axis = 0; + for (const auto& size_i : llvm::enumerate(size)) { + if (size_i.value().getSExtValue() < -1) { + return op.emitError( + llvm::formatv("size[{0}] cannot be negative other than -1", axis)); + } + axis++; + } + } + + if (begin && size && input_type.hasStaticShape()) { + for (uint64_t i = 0, end = begin.getNumElements(); i < end; i++) { + int begin_i = begin.getValues()[i].getSExtValue(); + int size_i = size.getValues()[i].getSExtValue(); + int dim_i = input_type.getShape()[i]; + if (begin_i > dim_i) { + return op.emitOpError(llvm::formatv( + "begin[{0}] cannot exceed dimension length: {1}", i, dim_i)); + } + if (size_i >= 0 && begin_i + size_i > dim_i) { + return op.emitError(llvm::formatv( + "begin[{0}] + size[{0}] cannot exceed dimension length: {1}", i, + dim_i)); + } + } + } + + return success(); +} + +TFL::ConstOp NarrowDownInt64InputValuesForOp(Operation* input_op, + RankedTensorType value_type, + Location loc, OpBuilder* builder) { + if (input_op == nullptr) return nullptr; + + mlir::DenseIntElementsAttr attr; + if (!matchPattern(input_op, m_Constant(&attr))) { + return nullptr; + } + + auto value_shape_type = tensorflow::GetTypeFromTFTensorShape( + value_type.getShape(), builder->getIntegerType(32)); + + SmallVector value_i32; + value_i32.reserve(value_type.getRank()); + for (const auto& size : attr) { + value_i32.push_back(ConvertToTfliteSize(size.getSExtValue())); + } + auto new_value_i32_attr = + mlir::DenseIntElementsAttr::get(value_shape_type, value_i32); + + return TFL::ConstOp::create(*builder, loc, new_value_i32_attr); +} + +// This will cast down int64 values for TFL slice op. +// This will require the begin & size are constants. +struct CastDonwInt64BeginEndToInt32 : public OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(TFL::SliceOp slice_op, + PatternRewriter& rewriter) const override { + auto begin = slice_op.getBegin(); + auto size = slice_op.getSize(); + auto begin_type = mlir::dyn_cast_or_null(begin.getType()); + auto size_type = mlir::dyn_cast_or_null(size.getType()); + auto begin_op = begin.getDefiningOp(); + auto size_op = size.getDefiningOp(); + + if (begin_op == nullptr && size_op == nullptr) return failure(); + + if (begin_type == nullptr && size_type == nullptr) return failure(); + + // Handle begin. + if (begin_op && begin_type && begin_type.getElementType().isInteger(64)) { + auto new_begin = NarrowDownInt64InputValuesForOp( + begin_op, begin_type, slice_op.getLoc(), &rewriter); + if (new_begin != nullptr) { + slice_op.setOperand(1, new_begin); + return success(); + } + } + + // Handle size. + if (size_op && size_type && size_type.getElementType().isInteger(64)) { + auto new_size = NarrowDownInt64InputValuesForOp( + size_op, size_type, slice_op.getLoc(), &rewriter); + if (new_size != nullptr) { + slice_op.setOperand(2, new_size); + return success(); + } + } + + return failure(); + } +}; + +void SliceOp::getCanonicalizationPatterns(RewritePatternSet& results, + MLIRContext* context) { + results.add(context); +} + +//===----------------------------------------------------------------------===// +// SqueezeOp +//===----------------------------------------------------------------------===// + +OpFoldResult SqueezeOp::fold(FoldAdaptor) { + auto input_ty = mlir::dyn_cast(getInput().getType()); + auto result_ty = mlir::dyn_cast(getType()); + + if (!input_ty || !result_ty) return {}; + if (input_ty == result_ty) return getInput(); + return {}; +} + +//===----------------------------------------------------------------------===// +// SubOp +//===----------------------------------------------------------------------===// + +OpFoldResult SubOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + // TODO(b/142478136): Handle fused ops. + if (getFusedActivationFunction() != "NONE") return {}; + + if (llvm::isa(getType().getElementType())) { + // Quant types not supported for the following. + return {}; + } + + auto lhs = llvm::dyn_cast_or_null(adaptor.getLhs()); + auto rhs = llvm::dyn_cast_or_null(adaptor.getRhs()); + + auto is_zero = [](Attribute a) { + return matchPattern(a, m_Zero()) || matchPattern(a, m_AnyZeroFloat()); + }; + + if (rhs && is_zero(rhs) && getLhs().getType() == getType()) { + return getLhs(); + } + + if (lhs && rhs) { + return ConstFoldBinaryOp( + getType(), getOperands(), [](APFloat a, APFloat b) { return a - b; }, + [](APInt a, APInt b) { return a - b; }); + } + + return {}; +} + +int64_t SubOp::GetArithmeticCount(Operation* op) { + int64_t count; + if (ArithmeticCountUtilHelper::GetFirstOutputCount(op, &count)) return count; + + return -1; +} + +//===----------------------------------------------------------------------===// +// TopKOp +//===----------------------------------------------------------------------===// + +static void BuildTopKOp(OpBuilder* builder, OperationState& result, Value input, + Value k) { + // Output size is only known if k is constant value. A negative dimension is + // considered dynamic so use -1 here if k is not a constant value. + int const_k = -1; + ElementsAttr cst; + if (matchPattern(k, m_Constant(&cst))) + // These casts should all be valid due to how Tensor constants are stored. + // TODO(jpienaar): This should use a helper function. + const_k = cst.getValues()[0].getValue().getSExtValue(); + + auto val_type = mlir::cast(input.getType()); + // If value is unranked, then so is results. + if (!val_type.hasRank()) + return TFL::TopKV2Op::build( + *builder, result, UnrankedTensorType::get(val_type.getElementType()), + UnrankedTensorType::get(k.getType()), input, k); + + // Resultant shape is value.shape[:-1] + [k] + std::vector shape(val_type.getShape()); + shape[shape.size() - 1] = const_k; + TFL::TopKV2Op::build( + *builder, result, + tensorflow::GetTypeFromTFTensorShape(shape, val_type.getElementType()), + tensorflow::GetTypeFromTFTensorShape(shape, k.getType()), input, k); +} + +//===----------------------------------------------------------------------===// +// FakeQuantOp +//===----------------------------------------------------------------------===// + +// Return true if the op has non-empty "minmax" attribute. +static inline bool HasValidMinMaxAttribute(Operation* op) { + auto minmax = op->getAttrOfType("minmax"); + return minmax && minmax.getValue().size() == 2; +} + +namespace { + +/// This pattern matches and remove a tfl.fake_quant if all the users of this op +/// and itself have "minmax" attribute set. +struct DropFakeQuant : public RewritePattern { + explicit DropFakeQuant(MLIRContext* context) + : RewritePattern(FakeQuantOp::getOperationName(), 1, context) {} + + LogicalResult matchAndRewrite(Operation* op, + PatternRewriter& rewriter) const override { + // We only match the op with valid "minmax" attribute. + if (!HasValidMinMaxAttribute(op)) return failure(); + + // If all the users of this op have valid "minmax" attributes, it is matched + // and can be removed. + auto fakeQuantOp = cast(op); + for (auto* operand : fakeQuantOp.getResult().getUsers()) + if (!HasValidMinMaxAttribute(operand)) return failure(); + + // Replace the matched FakeQuantOp by its primary operand. + rewriter.replaceOp(op, op->getOperand(0)); + return success(); + } +}; +} // end anonymous namespace + +void FakeQuantOp::getCanonicalizationPatterns(RewritePatternSet& results, + MLIRContext* context) { + results.add(context); +} + +//===----------------------------------------------------------------------===// +// UnpackOp +//===----------------------------------------------------------------------===// + +// TODO(b/133486129): Implement shape inference for unpack + +LogicalResult UnpackOp::inferReturnTypes( + MLIRContext* context, std::optional loc, ValueRange operands, + DictionaryAttr attributes, PropertyRef properties, RegionRange regions, + SmallVectorImpl& inferredReturnTypes) { + UnpackOpAdaptor op(operands, attributes, properties); + // TODO(jpienaar): Refactor verify + if (failed(op.verify(loc.has_value() ? *loc : UnknownLoc::get(context)))) + return failure(); + + if (operands.size() != 1) { + return emitOptionalError(loc, "input count should be equal to 1"); + } + + const int64_t num_value = op.getNumAttr().getInt(); + auto input_type = mlir::dyn_cast(operands[0].getType()); + if (!input_type || !input_type.hasRank()) { + // If input is unranked, then so is output. + inferredReturnTypes.assign( + num_value, UnrankedTensorType::get(input_type.getElementType())); + return success(); + } + + if (input_type.hasStaticShape() && input_type.getNumElements() <= 0) { + return emitOptionalError( + loc, "number of elements in input should be larger than 0"); + } + + const int64_t rank = input_type.getRank(); + if (rank <= 0) { + return emitOptionalError(loc, "input should be of rank larger than 0"); + } + + int64_t axis_value = op.getAxisAttr().getInt(); + if (axis_value < 0) { + axis_value += rank; + } + if (axis_value < 0 || axis_value >= rank) { + return emitOptionalError( + loc, "attribute 'axis' should be in range [-rank, rank), got axis = ", + op.getAxisAttr().getInt(), ", and rank = ", rank); + } + + if (!ShapedType::isDynamic(input_type.getDimSize(axis_value)) && + input_type.getDimSize(axis_value) != num_value) { + return emitOptionalError(loc, "output count should match 'num' attribute"); + } + + auto output_shape = llvm::to_vector<4>(input_type.getShape()); + output_shape.erase(output_shape.begin() + axis_value); + + auto output_type = tensorflow::GetTypeFromTFTensorShape( + output_shape, input_type.getElementType()); + inferredReturnTypes.assign(num_value, output_type); + + return success(); +} + +bool UnpackOp::isCompatibleReturnTypes(TypeRange lhs, TypeRange rhs) { + if (lhs.size() != rhs.size()) return false; + for (auto pair : llvm::zip(lhs, rhs)) { + if (failed( + mlir::verifyCompatibleShape(std::get<0>(pair), std::get<1>(pair)))) + return false; + } + return true; +} + +//===----------------------------------------------------------------------===// +// SplitOp +//===----------------------------------------------------------------------===// + +// Extracts and returns the signed integer constant in a 0-rank integer tensor +// or 1-element 1-rank integer tensor if 'value' is a constant. +static std::optional ExtractConstantIntFromTensor(Value value) { + ElementsAttr attr; + if (!matchPattern(value, m_Constant(&attr))) return {}; + if (attr.getNumElements() != 1) return {}; + IntegerAttr int_attr = *attr.getValues().begin(); + return int_attr.getValue().getSExtValue(); +} + +// Returns a RankedTensorType which is similar to `input_type` but replaces the +// dimension size of `dim` with `dim_size`. For example, +// `SubstituteRankedTensorTypeDimSize(tensor<3x4xi32>, 1, 2)` returns +// `tensor<3x2xi32>`. +static RankedTensorType SubstituteRankedTensorTypeDimSize( + RankedTensorType input_type, int64_t dim, int64_t dim_size) { + auto shape = input_type.getShape().vec(); + shape[dim] = dim_size; + return tensorflow::GetTypeFromTFTensorShape(shape, + input_type.getElementType()); +} + +// Verifies the output tensor types of SplitOp or SplitVOp. +template +static LogicalResult VerifySplitOpOutputTypes( + Operation* op, int64_t num_splits, + ExpectedOutputTypeGetter get_expected_output_type) { + for (int64_t i = 0; i < num_splits; ++i) { + auto expected_output_type = get_expected_output_type(i); + Value output = op->getResult(i); + if (failed(verifyCompatibleShape(output.getType(), expected_output_type))) + return op->emitOpError() + << "output #" << i << " should be " << expected_output_type + << " instead got " << output.getType(); + } + return success(); +} + +mlir::LogicalResult SplitOp::verify() { + SplitOp op = *this; + int64_t num_splits = op.getNumSplits(); + if (op.getNumResults() != num_splits) + return op.emitOpError("output count should match 'num_splits' attribute"); + + // If 'split_dim' is not a constant, there are no other checks. + std::optional split_dim_opt = + ExtractConstantIntFromTensor(op.getSplitDim()); + if (!split_dim_opt) return success(); + + // If 'input' is not a ranked tensor, there are no other checks. + auto input_type = mlir::dyn_cast(op.getValue().getType()); + if (!input_type) return success(); + + int64_t split_dim = split_dim_opt.value(); + const int64_t rank = input_type.getRank(); + if (split_dim < 0) split_dim += rank; + if (split_dim < 0 || split_dim >= rank) + return op.emitOpError("'split_dim' should be in [-rank, rank)"); + + // If the 'split_dim' dimension of the 'input' tensor has a dynamic size, + // there are no other checks. + const int64_t dim_size = input_type.getDimSize(split_dim); + if (ShapedType::isDynamic(dim_size)) return success(); + + if (dim_size % num_splits != 0) + return op.emitOpError("'num_splits' should evenly divide 'split_dim' axis"); + + // Verifies output tensor types. + RankedTensorType expected_output_type = SubstituteRankedTensorTypeDimSize( + input_type, split_dim, dim_size / num_splits); + return VerifySplitOpOutputTypes( + op.getOperation(), num_splits, + [expected_output_type](int64_t) { return expected_output_type; }); +} + +mlir::LogicalResult SplitVOp::verify() { + SplitVOp op = *this; + int64_t num_splits = op.getNumSplits(); + if (op.getNumResults() != num_splits) + return op.emitOpError("output count should match 'num_splits' attribute"); + + // If 'split_dim' is not a constant, there are no other checks. + std::optional split_dim_opt = + ExtractConstantIntFromTensor(op.getSplitDim()); + if (!split_dim_opt) return success(); + + // If 'input' is not a ranked tensor, there are no other checks. + auto input_type = mlir::dyn_cast(op.getValue().getType()); + if (!input_type) return success(); + + int64_t split_dim = split_dim_opt.value(); + const int64_t rank = input_type.getRank(); + if (split_dim < 0) split_dim += rank; + if (split_dim < 0 || split_dim >= rank) + return op.emitOpError("'split_dim' should be in [-rank, rank)"); + + // If the 'split_dim' dimension of the 'input' tensor has a dynamic size, + // there are no other checks. + const int64_t dim_size = input_type.getDimSize(split_dim); + if (ShapedType::isDynamic(dim_size)) return success(); + + // If 'size_splits' is not a constant, there are no other checks. + ElementsAttr size_splits_attr; + if (!matchPattern(op.getSizeSplits(), m_Constant(&size_splits_attr))) + return success(); + + if (size_splits_attr.getNumElements() != num_splits) { + auto size_splits_type = + mlir::cast(op.getSizeSplits().getType()); + RankedTensorType expected_size_splits_type = + tensorflow::GetTypeFromTFTensorShape({num_splits}, + size_splits_type.getElementType()); + return op.emitOpError("'size_splits' should be ") + << expected_size_splits_type; + } + + // Normalizes and verifies 'size_splits'. + // Note: TensorFlow allows one -1 element in 'size_splits'. The -1 element + // means the rest of the dimension size. + llvm::SmallVector size_splits; + size_splits.reserve(num_splits); + + int64_t negative_size_split_loc = -1; + int64_t total_size_splits = 0; + + for (int64_t i = 0; i < num_splits; ++i) { + auto size_split_attr = size_splits_attr.getValues()[i]; + int64_t size_split = size_split_attr.getValue().getSExtValue(); + size_splits.push_back(size_split); + if (size_split >= 0) { + total_size_splits += size_split; + continue; + } + if (size_split < -1) + return op.emitOpError( + "elements of 'size_splits' should be greater than or equal to -1"); + if (negative_size_split_loc != -1) + return op.emitOpError("'size_splits' can only have one -1"); + negative_size_split_loc = i; + } + + if (negative_size_split_loc != -1) { + if (total_size_splits > dim_size) + return op.emitOpError( + "sum of non-negative elements of 'size_splits' is greater than the " + "dimension size of 'split_dim' axis"); + size_splits[negative_size_split_loc] = dim_size - total_size_splits; + total_size_splits = dim_size; + } + + if (total_size_splits != dim_size) + return op.emitOpError( + "sum of 'size_splits' should match the dimension size of 'split_dim' " + "axis"); + + // Verifies result tensor types. + auto get_expected_output_type = [input_type, split_dim, + &size_splits](int64_t i) { + return SubstituteRankedTensorTypeDimSize(input_type, split_dim, + size_splits[i]); + }; + return VerifySplitOpOutputTypes(op.getOperation(), num_splits, + get_expected_output_type); +} + +//===----------------------------------------------------------------------===// +// MeanOp +//===----------------------------------------------------------------------===// + +// TODO(b/133854225): Implement shape inference to Mean + +//===----------------------------------------------------------------------===// +// LSTMOp +//===----------------------------------------------------------------------===// + +mlir::LogicalResult LSTMOp::verify() { + LSTMOp op = *this; + auto operands = op.GetStatefulOperands(); + if (operands.size() != 2 || operands[0] != 18 || operands[1] != 19) { + return op.emitOpError("LSTMOp expected to have two stateful operands"); + } + + const auto input_type = mlir::cast(op.getInput().getType()); + // Since TFLite runtime generally supports dynamic shape/rank, if `input_type` + // doesn't have static shape, we skip the shape check below. + if (!input_type.hasStaticShape()) return success(); + // The input should be at least 2D tensor since it will go through fully + // connected layer. + if (!input_type.hasRank() || input_type.getRank() < 2) + return op.emitOpError( + "the first input operand should have more than 2 dimensions."); + + const auto activation_state = + mlir::cast(op.getInputActivationState().getType()); + const auto cell_state = + mlir::cast(op.getInputCellState().getType()); + const auto input_to_output_weights = + mlir::cast(op.getInputToOutputWeights().getType()); + const auto recurrent_to_output_weights = + mlir::cast(op.getRecurrentToOutputWeights().getType()); + if (activation_state.hasStaticShape() && cell_state.hasStaticShape() && + input_to_output_weights.hasStaticShape() && + recurrent_to_output_weights.hasStaticShape()) { + const int n_input = input_type.getDimSize(input_type.getRank() - 1); + const int n_cell = input_to_output_weights.getDimSize(0); + const int n_output = recurrent_to_output_weights.getDimSize(1); + const int output_state_size = activation_state.getNumElements(); + const int n_batch = input_type.getRank() == 2 ? input_type.getDimSize(0) + : input_type.getDimSize(1); + const int state_size = cell_state.getNumElements(); + + // Check if the dimension of the inputs matches. + if ((output_state_size != n_batch * n_output) || + (state_size != n_batch * n_cell) || + (input_to_output_weights.getDimSize(1) != n_input) || + (recurrent_to_output_weights.getRank() != 2) || + (recurrent_to_output_weights.getDimSize(0) != n_cell) || + (input_to_output_weights.getRank() != 2)) { + return op.emitOpError("inputs don't match with the dimensions."); + } + + const bool is_layer_norm_lstm = + !mlir::isa(op.getForgetLayerNormCoefficients().getType()); + if (is_layer_norm_lstm) { + const auto forget_layer_norm_coefficients = + mlir::cast(op.getForgetLayerNormCoefficients().getType()); + // If this lstm has layer normalization, this input value, + // "forget_layer_norm_coefficients" should be a 1D tensor. + if (!forget_layer_norm_coefficients.hasRank() || + forget_layer_norm_coefficients.getRank() != 1 || + forget_layer_norm_coefficients.getDimSize(0) != n_cell) + return op.emitOpError( + "coefficient inputs have more than 2 dimensions or " + "don't match the dimension with input operand " + "`input_to_output_weights`."); + } + } + + return success(); +} + +namespace { + +// Replaces the optional bias operands with a "none" type value if the bias +// values are constant zeros. +struct RemoveLSTMOpZeroBias : public OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(LSTMOp op, + PatternRewriter& rewriter) const override { + if (EqualsZero(op.getInputGateBias())) { + auto none_value = TFL::NoValueOp::create( + rewriter, rewriter.getUnknownLoc(), rewriter.getNoneType(), + rewriter.getUnitAttr()); + op.getInputGateBiasMutable().assign(none_value); + return success(); + } + + if (EqualsZero(op.getProjectionBias())) { + auto none_value = TFL::NoValueOp::create( + rewriter, rewriter.getUnknownLoc(), rewriter.getNoneType(), + rewriter.getUnitAttr()); + op.getProjectionBiasMutable().assign(none_value); + return success(); + } + + return failure(); + } +}; + +} // namespace + +void LSTMOp::getCanonicalizationPatterns(RewritePatternSet& results, + MLIRContext* context) { + results.add(context); +} + +//===----------------------------------------------------------------------===// +// UnidirectionalSequenceLSTMOp +//===----------------------------------------------------------------------===// + +mlir::LogicalResult UnidirectionalSequenceLSTMOp::verify() { + UnidirectionalSequenceLSTMOp op = *this; + auto operands = op.GetStatefulOperands(); + if (operands.size() == 2 && operands[0] == 18 && operands[1] == 19) { + return success(); + } + return op.emitError( + "UnidirectionalSequenceLSTMOp expected to have two stateful operands"); +} + +LogicalResult UnidirectionalSequenceLSTMOp::inferReturnTypes( + MLIRContext*, std::optional, ValueRange operands, + DictionaryAttr attr, PropertyRef properties, RegionRange, + SmallVectorImpl& inferredReturnTypes) { + Value input = operands[0]; + auto input_type = mlir::dyn_cast_or_null(input.getType()); + + Value output_state = operands[18]; + auto output_state_type = + mlir::dyn_cast_or_null(output_state.getType()); + + if (input_type && input_type.hasRank() && input_type.getRank() != 3) { + return failure(); + } + + if (output_state_type && output_state_type.hasRank() && + output_state_type.getRank() != 2) { + return failure(); + } + + if (!input_type || !input_type.hasRank() || !output_state_type || + !output_state_type.hasRank()) { + // We cannot infer the output shape since we don't know the input shape or + // the output state shape. We will set the output shape as unranked. + Type result_type; + result_type = UnrankedTensorType::get( + mlir::cast(input.getType()).getElementType()); + inferredReturnTypes.assign({result_type}); + return success(); + } + + // Default to non-time_major. + std::optional time_major_attr = + attr.getNamed("time_major"); + bool time_majored = + time_major_attr + ? mlir::cast(time_major_attr->getValue()).getValue() + : false; + + int64_t batch = + time_majored ? input_type.getDimSize(1) : input_type.getDimSize(0); + int64_t time = + time_majored ? input_type.getDimSize(0) : input_type.getDimSize(1); + int64_t n_output = output_state_type.getDimSize(1); + + // Build the output shape. + SmallVector output_shape; + if (time_majored) { + output_shape = {time, batch, n_output}; + } else { + output_shape = {batch, time, n_output}; + } + auto result_type = tensorflow::GetTypeFromTFTensorShape( + output_shape, input_type.getElementType()); + + inferredReturnTypes.assign({result_type}); + return success(); +} + +bool UnidirectionalSequenceLSTMOp::isCompatibleReturnTypes(TypeRange lhs, + TypeRange rhs) { + if (lhs.size() != rhs.size() || lhs.size() != 1) return false; + if (failed(mlir::verifyCompatibleShape(lhs[0], rhs[0]))) return false; + return true; +} + +//===----------------------------------------------------------------------===// +// BidirectionalSequenceLSTMOp +//===----------------------------------------------------------------------===// + +mlir::LogicalResult BidirectionalSequenceLSTMOp::verify() { + BidirectionalSequenceLSTMOp op = *this; + auto operands = op.GetStatefulOperands(); + if (operands.size() == 4 && operands[0] == 35 && operands[1] == 36 && + operands[2] == 37 && operands[3] == 38) { + return success(); + } + return op.emitError( + "BidirectionalSequenceLSTMOp expected to have four stateful operands"); +} + +//===----------------------------------------------------------------------===// +// UnidirectionalSequenceRNNOp +//===----------------------------------------------------------------------===// + +mlir::LogicalResult UnidirectionalSequenceRNNOp::verify() { + UnidirectionalSequenceRNNOp op = *this; + auto operands = op.GetStatefulOperands(); + if (operands.size() == 1 && operands[0] == 4) { + return success(); + } + return op.emitError( + "UnidirectionalSequenceRNNOp expected to have one stateful operand"); +} + +//===----------------------------------------------------------------------===// +// SvdfOp +//===----------------------------------------------------------------------===// + +mlir::LogicalResult SVDFOp::verify() { + SVDFOp op = *this; + auto operands = op.GetStatefulOperands(); + if (operands.size() == 1 && operands[0] == 4) { + return success(); + } + return op.emitError("SvdfOp expected to have one stateful operand"); +} + +//===----------------------------------------------------------------------===// +// AbsOp +//===----------------------------------------------------------------------===// + +OpFoldResult AbsOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + auto operands = adaptor.getOperands(); + Type result_type = getType(); + // Only constant fold for tensor of f32 is implemented. + if (!IsF32ShapedType(result_type)) return nullptr; + + auto compute = [](APFloat value) -> APFloat { return llvm::abs(value); }; + return ConstFoldUnaryOp(result_type, operands[0], compute); +} + +//===----------------------------------------------------------------------===// +// NegOp +//===----------------------------------------------------------------------===// + +OpFoldResult NegOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + auto operands = adaptor.getOperands(); + Type result_type = getType(); + // Only constant fold for tensor of f32 is implemented. + if (!IsF32ShapedType(result_type)) return nullptr; + + auto compute = [](APFloat value) -> APFloat { return llvm::neg(value); }; + return ConstFoldUnaryOp(result_type, operands[0], compute); +} + +//===----------------------------------------------------------------------===// +// SinOp +//===----------------------------------------------------------------------===// + +OpFoldResult SinOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + auto operands = adaptor.getOperands(); + Type result_type = getType(); + // Only constant fold for tensor of f32/f16/bf16 is implemented. + if (!IsF32ShapedType(result_type) && !IsF16ShapedType(result_type) && + !IsBF16ShapedType(result_type)) + return nullptr; + + auto compute = [](APFloat value) -> APFloat { + bool loseInfo; + const llvm::fltSemantics& original_float_semantics = value.getSemantics(); + value.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, + &loseInfo); + float f = value.convertToFloat(); + float result = std::sin(f); + APFloat ap_result(result); + ap_result.convert(original_float_semantics, APFloat::rmNearestTiesToEven, + &loseInfo); + return ap_result; + }; + return ConstFoldUnaryOp(result_type, operands[0], compute); +} + +//===----------------------------------------------------------------------===// +// CosOp +//===----------------------------------------------------------------------===// + +OpFoldResult CosOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + auto operands = adaptor.getOperands(); + Type result_type = getType(); + // Only constant fold for tensor of f32/f16/bf16 is implemented. + if (!IsF32ShapedType(result_type) && !IsF16ShapedType(result_type) && + !IsBF16ShapedType(result_type)) + return nullptr; + + auto compute = [](APFloat value) -> APFloat { + bool loseInfo; + const llvm::fltSemantics& original_float_semantics = value.getSemantics(); + value.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, + &loseInfo); + float f = value.convertToFloat(); + float result = std::cos(f); + APFloat ap_result(result); + ap_result.convert(original_float_semantics, APFloat::rmNearestTiesToEven, + &loseInfo); + return ap_result; + }; + return ConstFoldUnaryOp(result_type, operands[0], compute); +} + +//===----------------------------------------------------------------------===// +// LogOp +//===----------------------------------------------------------------------===// + +OpFoldResult LogOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + auto operands = adaptor.getOperands(); + Type result_type = getType(); + // Only constant fold for tensor of f32 is implemented. + if (!IsF32ShapedType(result_type)) return nullptr; + + auto compute = [](APFloat value) -> APFloat { + float f = value.convertToFloat(); + float result = std::log(f); + return APFloat(result); + }; + return ConstFoldUnaryOp(result_type, operands[0], compute); +} + +//===----------------------------------------------------------------------===// +// ShapeOp +//===----------------------------------------------------------------------===// + +OpFoldResult ShapeOp::fold(FoldAdaptor) { + auto input_type = mlir::cast(getInput().getType()); + if (!input_type.hasStaticShape()) return nullptr; + + ArrayRef shape = input_type.getShape(); + auto result_type = mlir::cast(getType()); + if (result_type.getElementType().isInteger(64)) { + return DenseElementsAttr::get(result_type, shape); + } else if (result_type.getElementType().isInteger(32)) { + SmallVector shape_i32; + shape_i32.reserve(shape.size()); + for (int64_t dim : shape) { + shape_i32.push_back(dim); + } + return DenseElementsAttr::get(result_type, shape_i32); + } + return nullptr; +} + +//===----------------------------------------------------------------------===// +// SqrtOp +//===----------------------------------------------------------------------===// + +OpFoldResult SqrtOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + auto operands = adaptor.getOperands(); + Type result_type = getType(); + // Only constant fold for tensor of f32 is implemented. + if (!IsF32ShapedType(result_type)) return nullptr; + + auto compute = [](APFloat value) -> APFloat { + float f = value.convertToFloat(); + float result = std::sqrt(f); + return APFloat(result); + }; + return ConstFoldUnaryOp(result_type, operands[0], compute); +} + +//===----------------------------------------------------------------------===// +// RsqrtOp +//===----------------------------------------------------------------------===// + +OpFoldResult RsqrtOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + auto operands = adaptor.getOperands(); + Type result_type = getType(); + // Only constant fold for tensor of f32/bf16 is implemented. + if (!IsF32ShapedType(result_type) && !IsBF16ShapedType(result_type)) + return nullptr; + + auto compute = [](APFloat value) -> APFloat { + bool loseInfo; + const llvm::fltSemantics& original_float_semantics = value.getSemantics(); + value.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, + &loseInfo); + float f = value.convertToFloat(); + APFloat result(1.f / std::sqrt(f)); + result.convert(original_float_semantics, APFloat::rmNearestTiesToEven, + &loseInfo); + return result; + }; + return ConstFoldUnaryOp(result_type, operands[0], compute); +} + +//===----------------------------------------------------------------------===// +// SquareOp +//===----------------------------------------------------------------------===// + +OpFoldResult SquareOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + auto operands = adaptor.getOperands(); + Type result_type = getType(); + // Only constant fold for tensor of f32 is implemented. + if (!IsF32ShapedType(result_type)) return nullptr; + + auto compute = [](APFloat value) -> APFloat { return value * value; }; + return ConstFoldUnaryOp(result_type, operands[0], compute); +} + +//===----------------------------------------------------------------------===// +// ReluOp +//===----------------------------------------------------------------------===// + +template +T ComputeRelu(T val) { + return std::max(static_cast(0), val); +} + +// TODO: b/361137571 - Add folding for quantized types if it is needed. +OpFoldResult ReluOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + auto elements = mlir::dyn_cast_or_null(adaptor.getX()); + if (!elements) { + return {}; + } + + if (auto data = llvm::dyn_cast(elements)) { + if (getType().getElementType().isSignlessInteger(32)) { + return DenseIntElementsAttr::get( + data.getType(), + llvm::map_to_vector(data.getValues(), ComputeRelu)); + } + if (getType().getElementType().isF32()) { + return DenseFPElementsAttr::get( + data.getType(), + llvm::map_to_vector(data.getValues(), ComputeRelu)); + } + } else if (auto data = mlir::dyn_cast(elements)) { + if (getType().getElementType().isSignlessInteger(32)) { + return ConstFoldUnaryResource(data, ComputeRelu); + } + if (getType().getElementType().isF32()) { + return ConstFoldUnaryResource(data, ComputeRelu); + } + } + + return {}; +} + +//===----------------------------------------------------------------------===// +// MaximumOp +//===----------------------------------------------------------------------===// + +OpFoldResult MaximumOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + auto lhs_type = getLhs().getType(); + auto rhs_type = getRhs().getType(); + + auto lhs = mlir::dyn_cast_or_null(adaptor.getLhs()); + auto rhs = mlir::dyn_cast_or_null(adaptor.getRhs()); + + if (lhs && rhs) { + return ConstFoldBinaryOp( + getType(), getOperands(), + [](APFloat a, APFloat b) { return llvm::maximum(a, b); }, + [](APInt a, APInt b) { return a.slt(b) ? b : a; }); + } + + lhs = mlir::dyn_cast_or_null(adaptor.getLhs()); + rhs = mlir::dyn_cast_or_null(adaptor.getRhs()); + // Only constant fold for float tensors of the same type is implemented. + if (lhs_type != rhs_type || !IsFloatShapedType(lhs_type)) return nullptr; + + if (lhs && lhs.isSplat()) { + APFloat lhs_value = lhs.getSplatValue(); + lhs_value.changeSign(); + if (lhs_value.isLargest() || lhs_value.isInfinity()) return getRhs(); + } + if (rhs && rhs.isSplat()) { + APFloat rhs_value = rhs.getSplatValue(); + rhs_value.changeSign(); + if (rhs_value.isLargest() || rhs_value.isInfinity()) return getLhs(); + } + return nullptr; +} + +//===----------------------------------------------------------------------===// +// MinimumOp +//===----------------------------------------------------------------------===// + +OpFoldResult MinimumOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + auto lhs_type = getLhs().getType(); + auto rhs_type = getRhs().getType(); + + auto lhs = mlir::dyn_cast_or_null(adaptor.getLhs()); + auto rhs = mlir::dyn_cast_or_null(adaptor.getRhs()); + + if (lhs && rhs) { + return ConstFoldBinaryOp( + getType(), getOperands(), + [](APFloat a, APFloat b) { return llvm::minimum(a, b); }, + [](APInt a, APInt b) { return a.slt(b) ? a : b; }); + } + + lhs = mlir::dyn_cast_or_null(adaptor.getLhs()); + rhs = mlir::dyn_cast_or_null(adaptor.getRhs()); + + if (lhs_type != rhs_type || !IsFloatShapedType(lhs_type)) return nullptr; + + if (lhs && lhs.isSplat()) { + auto splat = lhs.getSplatValue(); + if (splat.isLargest() || splat.isInfinity()) return getRhs(); + } + if (rhs && rhs.isSplat()) { + auto splat = rhs.getSplatValue(); + if (splat.isLargest() || splat.isInfinity()) return getLhs(); + } + return nullptr; +} + +//===----------------------------------------------------------------------===// +// Comparison and Logical Ops +//===----------------------------------------------------------------------===// + +OpFoldResult LessOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + if (getLhs().getType().getElementType().isInteger(32)) { + return ConstFoldBinaryOp( + getType(), adaptor.getLhs(), adaptor.getRhs(), + [](int32_t lhs, int32_t rhs) { return lhs < rhs; }); + } + if (getLhs().getType().getElementType().isInteger(64)) { + return ConstFoldBinaryOp( + getType(), adaptor.getLhs(), adaptor.getRhs(), + [](int64_t lhs, int64_t rhs) { return lhs < rhs; }); + } + if (getLhs().getType().getElementType().isF32()) { + return ConstFoldBinaryOp( + getType(), adaptor.getLhs(), adaptor.getRhs(), + [](float lhs, float rhs) { return lhs < rhs; }); + } + return {}; +} + +OpFoldResult LessEqualOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + if (getLhs().getType().getElementType().isInteger(32)) { + return ConstFoldBinaryOp( + getType(), adaptor.getLhs(), adaptor.getRhs(), + [](int32_t lhs, int32_t rhs) { return lhs <= rhs; }); + } + if (getLhs().getType().getElementType().isInteger(64)) { + return ConstFoldBinaryOp( + getType(), adaptor.getLhs(), adaptor.getRhs(), + [](int64_t lhs, int64_t rhs) { return lhs <= rhs; }); + } + if (getLhs().getType().getElementType().isF32()) { + return ConstFoldBinaryOp( + getType(), adaptor.getLhs(), adaptor.getRhs(), + [](float lhs, float rhs) { return lhs <= rhs; }); + } + return {}; +} + +OpFoldResult GreaterOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + if (getLhs().getType().getElementType().isInteger(32)) { + return ConstFoldBinaryOp( + getType(), adaptor.getLhs(), adaptor.getRhs(), + [](int32_t lhs, int32_t rhs) { return lhs > rhs; }); + } + if (getLhs().getType().getElementType().isInteger(64)) { + return ConstFoldBinaryOp( + getType(), adaptor.getLhs(), adaptor.getRhs(), + [](int64_t lhs, int64_t rhs) { return lhs > rhs; }); + } + if (getLhs().getType().getElementType().isF32()) { + return ConstFoldBinaryOp( + getType(), adaptor.getLhs(), adaptor.getRhs(), + [](float lhs, float rhs) { return lhs > rhs; }); + } + return {}; +} + +OpFoldResult GreaterEqualOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + if (getLhs().getType().getElementType().isInteger(32)) { + return ConstFoldBinaryOp( + getType(), adaptor.getLhs(), adaptor.getRhs(), + [](int32_t lhs, int32_t rhs) { return lhs >= rhs; }); + } + if (getLhs().getType().getElementType().isInteger(64)) { + return ConstFoldBinaryOp( + getType(), adaptor.getLhs(), adaptor.getRhs(), + [](int64_t lhs, int64_t rhs) { return lhs >= rhs; }); + } + if (getLhs().getType().getElementType().isF32()) { + return ConstFoldBinaryOp( + getType(), adaptor.getLhs(), adaptor.getRhs(), + [](float lhs, float rhs) { return lhs >= rhs; }); + } + return {}; +} + +OpFoldResult EqualOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + if (getX().getType().getElementType().isInteger(32)) { + return ConstFoldBinaryOp( + getType(), adaptor.getX(), adaptor.getY(), + [](int32_t lhs, int32_t rhs) { return lhs == rhs; }); + } + if (getX().getType().getElementType().isInteger(64)) { + return ConstFoldBinaryOp( + getType(), adaptor.getX(), adaptor.getY(), + [](int64_t lhs, int64_t rhs) { return lhs == rhs; }); + } + if (getX().getType().getElementType().isF32()) { + return ConstFoldBinaryOp( + getType(), adaptor.getX(), adaptor.getY(), + [](float lhs, float rhs) { return lhs == rhs; }); + } + return {}; +} + +OpFoldResult NotEqualOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + if (getLhs().getType().getElementType().isInteger(32)) { + return ConstFoldBinaryOp( + getType(), adaptor.getLhs(), adaptor.getRhs(), + [](int32_t lhs, int32_t rhs) { return lhs != rhs; }); + } + if (getLhs().getType().getElementType().isInteger(64)) { + return ConstFoldBinaryOp( + getType(), adaptor.getLhs(), adaptor.getRhs(), + [](int64_t lhs, int64_t rhs) { return lhs != rhs; }); + } + if (getLhs().getType().getElementType().isF32()) { + return ConstFoldBinaryOp( + getType(), adaptor.getLhs(), adaptor.getRhs(), + [](float lhs, float rhs) { return lhs != rhs; }); + } + return {}; +} + +OpFoldResult LogicalAndOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + return ConstFoldBinaryOp( + getType(), adaptor.getLhs(), adaptor.getRhs(), + [](bool lhs, bool rhs) { return lhs && rhs; }); +} + +OpFoldResult LogicalOrOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + return ConstFoldBinaryOp( + getType(), adaptor.getLhs(), adaptor.getRhs(), + [](bool lhs, bool rhs) { return lhs || rhs; }); +} + +//===----------------------------------------------------------------------===// +// SelectOp +//===----------------------------------------------------------------------===// + +// TODO: b/359275356 - Expand this to handle the broadcast case similar +// to `ConstFoldBinaryOpDense`. +OpFoldResult SelectOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + auto lhs_type = getX().getType(); + auto rhs_type = getY().getType(); + auto condition_type = getCondition().getType(); + auto out_type = getType(); + + if (lhs_type != rhs_type) { + return {}; + } + + if (lhs_type.getShape() != condition_type.getShape()) { + // "broadcasted" condition not yet supported. + return {}; + } + + auto condition_vals = + llvm::dyn_cast_or_null(adaptor.getCondition()); + if (!condition_vals || !condition_vals.getElementType().isInteger(1)) { + return {}; + } + + if (condition_vals.isSplat()) { + const bool val = condition_vals.getSplatValue(); + return val ? adaptor.getX() : adaptor.getY(); + } + + auto lhs_vals = llvm::dyn_cast_or_null(adaptor.getX()); + auto rhs_vals = llvm::dyn_cast_or_null(adaptor.getY()); + if (!lhs_vals || !rhs_vals) { + return {}; + } + + llvm::SmallVector results; + results.reserve(condition_type.getNumElements()); + + auto lhs_it = lhs_vals.getValues().begin(); + auto lhs_end = lhs_vals.getValues().end(); + auto rhs_it = rhs_vals.getValues().begin(); + auto rhs_end = rhs_vals.getValues().end(); + + auto condition_it = condition_vals.getValues().begin(); + auto condition_end = condition_vals.getValues().end(); + + while (condition_it < condition_end && lhs_it < lhs_end && rhs_it < rhs_end) { + if (*condition_it++) { + results.push_back(*lhs_it); + } else { + results.push_back(*rhs_it); + } + + if (!lhs_vals.isSplat()) { + lhs_it++; + } + if (!rhs_vals.isSplat()) { + rhs_it++; + } + } + + return DenseElementsAttr::get(out_type, results); +} + +//===----------------------------------------------------------------------===// +// SumOp +//===----------------------------------------------------------------------===// + +static size_t GetOutputFlatIndex(size_t input_flat_ind, + const FlatIndHelper& input_flat_ind_helper, + const FlatIndHelper& output_flat_ind_helper, + const absl::flat_hash_set& axes_set, + bool keep_dims) { + auto input_shaped_ind = input_flat_ind_helper.GetShapedInd(input_flat_ind); + + std::vector output_shaped_ind; + for (int dim = 0; dim < input_shaped_ind.size(); ++dim) { + bool is_reduced_dim = axes_set.contains(dim); + if (is_reduced_dim && keep_dims) { + output_shaped_ind.push_back(0); + } else if (!is_reduced_dim) { + output_shaped_ind.push_back(input_shaped_ind[dim]); + } + } + + return output_flat_ind_helper.GetFlatInd(output_shaped_ind); +} + +OpFoldResult SumOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + auto input = adaptor.getInput(); + auto axes = adaptor.getAxes(); + + if (!input || !axes) { + return {}; + } + + auto input_type = getInput().getType(); + if (!input_type.getElementType().isF32()) { + return {}; + } + + const auto input_rank = input_type.getRank(); + auto axes_data = llvm::cast(axes); + absl::flat_hash_set axes_set(axes_data.getValues().begin(), + axes_data.getValues().end()); + + llvm::SmallVector out_shape; + for (int input_dim = 0; input_dim < input_rank; ++input_dim) { + bool is_reduced_dim = axes_set.contains(input_dim); + + if (is_reduced_dim && adaptor.getKeepDims()) { + out_shape.push_back(1); + } else if (!is_reduced_dim) { + out_shape.push_back(input_type.getDimSize(input_dim)); + } + } + + auto out_type = RankedTensorType::get(out_shape, input_type.getElementType()); + int64_t output_size = std::accumulate(out_shape.begin(), out_shape.end(), 1, + std::multiplies()); + + std::vector out_data(output_size, 0.0); + FlatIndHelper input_flat_ind_helper(input_type); + FlatIndHelper output_flat_ind_helper(out_type); + + auto in_data = llvm::cast(input); + + size_t input_flat_ind = 0; + for (auto it = in_data.value_begin(); it < in_data.value_end(); + ++it) { + // Find the corresponding reduced output index. + size_t output_flat_ind = GetOutputFlatIndex( + input_flat_ind, input_flat_ind_helper, output_flat_ind_helper, axes_set, + adaptor.getKeepDims()); + + out_data[output_flat_ind] += *it; + input_flat_ind++; + } + + return DenseFPElementsAttr::get(out_type, out_data); +} + +void SumOp::build(OpBuilder& builder, OperationState& result, Value input, + Value axes, BoolAttr keep_dims) { + Type out_ty = InferReductionOpType(input, axes, keep_dims); + build(builder, result, out_ty, input, axes, keep_dims); +} + +//===----------------------------------------------------------------------===// +// RankOp +//===----------------------------------------------------------------------===// + +OpFoldResult RankOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + auto operands = adaptor.getOperands(); + assert(operands.size() == 1); + auto result_type = mlir::cast(getType()); + if (auto elements_attr = mlir::dyn_cast_or_null(operands[0])) { + auto rank = static_cast(elements_attr.getShapedType().getRank()); + return DenseElementsAttr::get(result_type, {rank}); + } + + // Also fold if `input` has a known rank. + auto input_type = mlir::cast(getInput().getType()); + // Do not fold if rank is zero because the TFLite converter doesn't + // distinguish between unranked input and scalar input due to b/138865275. + // TODO(b/138865275): Remove `input_type.getRank() != 0` in the following + // predicate and fold the op when rank is zero. + if (input_type.hasRank() && input_type.getRank() != 0) { + auto rank = static_cast(input_type.getRank()); + return DenseElementsAttr::get(result_type, {rank}); + } + + return nullptr; +} + +//===----------------------------------------------------------------------===// +// ConstOp +//===----------------------------------------------------------------------===// + +OpFoldResult ConstOp::fold(FoldAdaptor adaptor) { + auto operands = adaptor.getOperands(); + (void)operands; + assert(operands.empty() && "constant has no operands"); + // Return the held attribute value. + return getValue(); +} + +bool ConstOp::isCompatibleReturnTypes(TypeRange l, TypeRange r) { + // Allow the type inferred to not match exactly the inferred type as the + // inferred type is from the element attribute's type while the op may have + // gotten constructed from TF const op or be in a partial state of shape + // refinement, so allow it to only be compatible. The op will be refined + // during shape inference and casts inserted as needed to satisfy type + // constraints of consumers. + return succeeded(verifyCompatibleShapes(l, r)); +} + +namespace { +struct FoldPseudoConstOp : public OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(ConstOp const_op, + PatternRewriter& rewriter) const override { + if (arith::ConstantOp::isBuildableWith(const_op.getValue(), + const_op.getType())) { + rewriter.replaceOpWithNewOp(const_op, + const_op.getValue()); + return success(); + } else if (TFL::NoValueOp::isBuildableWith(const_op.getValue(), + const_op.getType())) { + rewriter.replaceOpWithNewOp( + const_op, rewriter.getNoneType(), + mlir::cast(const_op.getValue())); + return success(); + } + return failure(); + } +}; + +} // namespace + +void ConstOp::getCanonicalizationPatterns(RewritePatternSet& results, + MLIRContext* context) { + results.add(context); +} + +//===----------------------------------------------------------------------===// +// CastOp +//===----------------------------------------------------------------------===// + +OpFoldResult CastIntToInt(DenseIntElementsAttr data, IntegerType in_type, + IntegerType out_type) { + const bool is_unsigned = in_type.isUnsigned(); + const bool involves_bool = + in_type.getWidth() == 1 || out_type.getWidth() == 1; + const int output_bitwidth = out_type.getWidth(); + // The integer cast op is the same as C integer cast. Depends on the operand + // type's signedness, we will determine whether or not sign extension is + // needed. + auto cast = [&](APInt value) { + if (involves_bool) { + // Handle boolean inputs or outputs explicitly as it doesn't have the same + // behavior as extension or truncation. + // true input should always be cast to 1 and not -1 as the sign extension + // would do for signed outputs. Similarly, non-zero inputs should be cast + // to true. Truncating even numbers to one bit will result in `false`. + return APInt(out_type.getWidth(), value != 0); + } + return is_unsigned ? value.zextOrTrunc(output_bitwidth) + : value.sextOrTrunc(output_bitwidth); + }; + + return data.mapValues(out_type, cast); +} + +OpFoldResult CastFloatToInt(DenseFPElementsAttr data, FloatType in_type, + IntegerType out_type) { + if (!in_type.isF32()) { + return {}; + } + + const auto out_width = out_type.getWidth(); + if (out_width != 64 && out_width != 32) { + return {}; + } + + auto cast = [&](APFloat value) -> APInt { + APSInt result(out_width, false); + bool is_exact; + value.convertToInteger(result, llvm::RoundingMode::TowardZero, &is_exact); + return result; + }; + + return data.mapValues(out_type, cast); +} + +template +llvm::SmallVector MapStaticCast(DenseElementsAttr data) { + return llvm::map_to_vector(data.getValues(), + [](InType v) { return static_cast(v); }); +} + +OpFoldResult CastIntToFloat(DenseIntElementsAttr data, IntegerType in_type, + FloatType out_type) { + auto result_type = data.getType().clone(out_type); + if (!out_type.isF32()) { + return {}; + } + + if (in_type.isSignlessInteger(32)) { + return DenseFPElementsAttr::get(result_type, + MapStaticCast(data)); + } + if (in_type.isSignlessInteger(1)) { + return DenseFPElementsAttr::get(result_type, + MapStaticCast(data)); + } + + return {}; +} + +OpFoldResult CastFloatToFloat(DenseFPElementsAttr data, FloatType in_type, + FloatType out_type) { + auto result_type = data.getType().clone(out_type); + if (in_type.isF32() && out_type.isF64()) { + return DenseFPElementsAttr::get(result_type, + MapStaticCast(data)); + } + + if (in_type.isF64() && out_type.isF32()) { + return DenseFPElementsAttr::get(result_type, + MapStaticCast(data)); + } + + if (in_type.isF32() && out_type.isF16()) { + return data.mapValues(out_type, [&](const APFloat& old_value) { + APFloat value(old_value); + bool unused_loses_info; + value.convert(out_type.getFloatSemantics(), APFloat::rmNearestTiesToEven, + &unused_loses_info); + return value.bitcastToAPInt(); + }); + } + return {}; +} + +OpFoldResult CastOp::fold(FoldAdaptor adaptor) { + auto in_type = getInput().getType().getElementType(); + auto out_type = getType().getElementType(); + + if (!ShouldFoldOperation(this->getOperation())) return {}; + + auto operands = adaptor.getOperands(); + if (operands.size() != 1) { + return {}; + } + if (getInput().getType() == getType()) { + return getInput(); + } + + auto input = operands[0]; + + if (auto int_in_type = llvm::dyn_cast_or_null(in_type)) { + auto in_data = llvm::dyn_cast_or_null(input); + if (!in_data) { + return {}; + } + if (auto float_out_type = llvm::dyn_cast_or_null(out_type)) { + return CastIntToFloat(in_data, int_in_type, float_out_type); + } + if (auto int_out_type = llvm::dyn_cast_or_null(out_type)) { + return CastIntToInt(in_data, int_in_type, int_out_type); + } + } + + if (auto float_in_type = llvm::dyn_cast_or_null(in_type)) { + auto in_data = llvm::dyn_cast_or_null(input); + if (!in_data) { + return {}; + } + if (auto float_out_type = llvm::dyn_cast_or_null(out_type)) { + return CastFloatToFloat(in_data, float_in_type, float_out_type); + } + if (auto int_out_type = llvm::dyn_cast_or_null(out_type)) { + return CastFloatToInt(in_data, float_in_type, int_out_type); + } + } + + return {}; +} + +//===----------------------------------------------------------------------===// +// SelectV2Op +//===----------------------------------------------------------------------===// + +static void BuildSelectV2Op(Builder* builder, OperationState& result, + Value cond, Value x, Value y) { + auto operand_type = + OpTrait::util::getBroadcastedType(x.getType(), y.getType()); + + if (!operand_type) + emitError(result.location) << "non-broadcastable operands: " << x.getType() + << " and " << y.getType(); + + bool has_static_cond_shape = false; + bool has_static_operand_shape = false; + ArrayRef cond_shape; + ArrayRef operand_shape; + + if (auto shaped_type = mlir::dyn_cast(cond.getType())) { + if (shaped_type.hasStaticShape()) { + has_static_cond_shape = true; + cond_shape = shaped_type.getShape(); + } + } + if (auto shaped_type = mlir::dyn_cast(operand_type)) { + if (shaped_type.hasStaticShape()) { + has_static_operand_shape = true; + operand_shape = shaped_type.getShape(); + } + } + + SmallVector broadcastedShape; + if (has_static_cond_shape && has_static_operand_shape && + !OpTrait::util::getBroadcastedShape(cond_shape, operand_shape, + broadcastedShape)) { + emitError(result.location) << "non-broadcastable operands: " << operand_type + << " and " << cond.getType(); + } + + result.addOperands({cond, x, y}); + + auto elementType = mlir::dyn_cast(x.getType()).getElementType(); + if (has_static_cond_shape && has_static_operand_shape) { + result.types.push_back( + tensorflow::GetTypeFromTFTensorShape(broadcastedShape, elementType)); + } else { + result.types.push_back(UnrankedTensorType::get(elementType)); + } +} + +//===----------------------------------------------------------------------===// +// RangeOp +//===----------------------------------------------------------------------===// + +namespace { + +// Compute the length of a range (1-D) tensor given `start`, `limit`, `delta`. +// Template parameter `FloatOrInt` must be standard C integer or floating-point +// types. +template +int GetLengthOfRange(FloatOrInt start, FloatOrInt limit, FloatOrInt delta) { + // Refer to the implementation in + // tensorflow/lite/kernels/range.cc. + return std::is_integral::value + ? ((std::abs(limit - start) + std::abs(delta) - 1) / + std::abs(delta)) + : std::ceil(std::abs((limit - start) / delta)); +} + +// Builds a constant range tensor of `result_elem_type` elements. +// Template parameter `FloatOrIntAtrr` must be mlir::IntegerAttr or +// mlir::FloatAttr. +template +DenseElementsAttr BuildConstRangeTensor(Type result_elem_type, int num_elements, + FloatOrIntAtrr start_attr, + FloatOrIntAtrr delta_attr) { + using ValueType = typename FloatOrIntAtrr::ValueType; // APInt or APFloat + ValueType start = start_attr.getValue(); + ValueType delta = delta_attr.getValue(); + + SmallVector new_values; + new_values.reserve(num_elements); + ValueType new_value = start; + for (int i = 0; i < num_elements; ++i) { + new_values.push_back(new_value); + new_value = new_value + delta; + } + // Result is always a 1-D tensor. + auto new_result_type = + tensorflow::GetTypeFromTFTensorShape({num_elements}, result_elem_type); + return DenseElementsAttr::get(new_result_type, new_values); +} +} // namespace + +OpFoldResult RangeOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + auto operands = adaptor.getOperands(); + assert(operands.size() == 3); + auto start_tensor = mlir::dyn_cast_or_null(operands[0]); + auto limit_tensor = mlir::dyn_cast_or_null(operands[1]); + auto delta_tensor = mlir::dyn_cast_or_null(operands[2]); + if (start_tensor && limit_tensor && delta_tensor) { + // Operands should all be scalars + assert(start_tensor.getShapedType().getRank() == 0 && + limit_tensor.getShapedType().getRank() == 0 && + delta_tensor.getShapedType().getRank() == 0); + Type elem_type = mlir::cast(getType()).getElementType(); + if (elem_type.isSignlessInteger()) { + auto start_attr = start_tensor.getValues()[0]; + auto limit_attr = limit_tensor.getValues()[0]; + auto delta_attr = delta_tensor.getValues()[0]; + const int num_elements = GetLengthOfRange( + start_attr.getInt(), limit_attr.getInt(), delta_attr.getInt()); + return BuildConstRangeTensor(elem_type, num_elements, start_attr, + delta_attr); + } else if (mlir::isa(elem_type)) { + auto start_attr = start_tensor.getValues()[0]; + auto limit_attr = limit_tensor.getValues()[0]; + auto delta_attr = delta_tensor.getValues()[0]; + const int num_elements = GetLengthOfRange(start_attr.getValueAsDouble(), + limit_attr.getValueAsDouble(), + delta_attr.getValueAsDouble()); + return BuildConstRangeTensor(elem_type, num_elements, start_attr, + delta_attr); + } + } + + return nullptr; +} + +//===----------------------------------------------------------------------===// +// TransposeConvOp +//===----------------------------------------------------------------------===// + +mlir::LogicalResult TransposeConvOp::verify() { + TransposeConvOp op = *this; + ShapedType output_type = mlir::cast(op.getOutput().getType()); + ShapedType output_shape_type = + mlir::cast(op.getOutputShape().getType()); + if (output_type.hasRank() && output_shape_type.hasStaticShape()) { + if (output_type.getRank() != output_shape_type.getDimSize(0)) { + return op.emitOpError(llvm::formatv( + "expect output type has rank = {0}, got output type {1}", + output_shape_type.getDimSize(0), output_type)); + } + } + + DenseIntElementsAttr output_shape_elements; + if (!matchPattern(op.getOutputShape(), m_Constant(&output_shape_elements))) { + return success(); + } + + llvm::SmallVector output_shape; + output_shape.reserve(output_shape_elements.getNumElements()); + for (auto dim : output_shape_elements.getValues()) { + output_shape.push_back(dim); + } + + auto expected_output_type = tensorflow::GetTypeFromTFTensorShape( + output_shape, output_type.getElementType()); + if (failed(mlir::verifyCompatibleShape(output_type, expected_output_type))) { + return op.emitOpError(llvm::formatv("expect output type {0}, got {1}", + expected_output_type, output_type)); + } + + return success(); +} + +int64_t TransposeConvOp::GetArithmeticCount(Operation* op) { + int64_t count = -1; + auto transpose_conv = llvm::dyn_cast(op); + auto input_type = mlir::dyn_cast_or_null( + transpose_conv.getInput().getType()); + auto weight_type = mlir::dyn_cast_or_null( + transpose_conv.getWeights().getType()); + if (input_type && weight_type && input_type.hasStaticShape() && + weight_type.hasStaticShape()) { + // Compute op count from the seven nested loops of + // tflite::reference_ops::TransposeConv(): + count = 2 * input_type.getNumElements() * weight_type.getDimSize(0) * + weight_type.getDimSize(1) * weight_type.getDimSize(2); + } + + return count; +} + +//===----------------------------------------------------------------------===// +// StridedSliceOp +//===----------------------------------------------------------------------===// + +bool VerifyStridedSliceOpInputRankConstraints(StridedSliceOp op) { + auto ranked_input_type = + mlir::dyn_cast(op.getInput().getType()); + + // If input is unranked, there is nothing else to be verified. + if (!ranked_input_type) return true; + const int num_input_dims = ranked_input_type.getRank(); + + // The kernel will reshape the input tensor with new axis, it only supports + // this reshaped tensor up to 6D. + const uint32_t ellipsis_mask = op.getEllipsisMask(); + const uint32_t new_axis_mask = op.getNewAxisMask(); + int num_added_axis = 0; + for (int i = 0; i < 8; ++i) { + if (!((1 << i) & ellipsis_mask) && ((1 << i) & new_axis_mask)) { + num_added_axis++; + } + } + return (num_input_dims + num_added_axis <= 6); +} + +LogicalResult StridedSliceOp::verify() { + StridedSliceOp op = *this; + auto ranked_input_type = + mlir::dyn_cast(op.getInput().getType()); + + // If input is unranked, there is nothing else to be verified. + if (!ranked_input_type) return success(); + int num_input_dims = ranked_input_type.getRank(); + + if (auto begin_type = + mlir::dyn_cast(op.getBegin().getType())) { + if (begin_type.getRank() != 1) return failure(); + if (begin_type.getDimSize(0) > num_input_dims) return failure(); + } + + if (auto end_type = mlir::dyn_cast(op.getEnd().getType())) { + if (end_type.getRank() != 1) return failure(); + if (end_type.getDimSize(0) > num_input_dims) return failure(); + } + + if (auto strides_type = + mlir::dyn_cast(op.getStrides().getType())) { + if (strides_type.getRank() != 1) return failure(); + if (strides_type.getDimSize(0) > num_input_dims) return failure(); + } + + return success(); +} + +OpFoldResult StridedSliceOp::fold(FoldAdaptor) { + // Currently only support all masks being 0. + if (getBeginMask() != 0 || getEndMask() != 0 || getEllipsisMask() != 0 || + getNewAxisMask() != 0 || getShrinkAxisMask() != 0) + return {}; + + auto input_type = + mlir::dyn_cast_or_null(getInput().getType()); + if (!input_type || !input_type.hasStaticShape()) return {}; + + // Begin has to be all 0s. + DenseIntElementsAttr begin_dense_elem_attr; + if (!matchPattern(getBegin(), m_Constant(&begin_dense_elem_attr))) { + return {}; + } + for (auto begin_ele : begin_dense_elem_attr) { + if (begin_ele.getSExtValue() != 0) { + return {}; + } + } + + // Strides has to be all 1s. + DenseIntElementsAttr strides_dense_elem_attr; + if (!matchPattern(getStrides(), m_Constant(&strides_dense_elem_attr))) { + return {}; + } + for (auto stride_ele : strides_dense_elem_attr) { + if (stride_ele.getSExtValue() != 1) { + return {}; + } + } + // End has to map the input shape. + DenseIntElementsAttr end_dense_elem_attr; + if (!matchPattern(getEnd(), m_Constant(&end_dense_elem_attr))) { + return {}; + } + int i = 0; + for (auto end_ele : end_dense_elem_attr) { + if (end_ele.getSExtValue() != input_type.getDimSize(i)) { + return {}; + } + ++i; + } + + return getInput(); +} + +//===----------------------------------------------------------------------===// +// TransposeOp +//===----------------------------------------------------------------------===// + +namespace { + +// The function recursively traverses the dimensions of the output tensor in +// a row-major order and writes the value of the output tensor into +// `output_element_addr`. +// TODO(@lukeboyer) make element byte size a template param. +void ComputePermutation(ArrayRef perms, ArrayRef output_shape, + const char* raw_input, const int element_byte_size, + const int64_t current_axis, char*& output_element_addr, + MutableArrayRef current_input_index, + ShapedType input_shape_type) { + const int64_t input_axis = perms[current_axis]; + const bool is_last_axis = current_axis == output_shape.size() - 1; + + // Assert that raw_input is not NULL before we start. + assert(raw_input != nullptr && "raw_input is NULL"); + + for (int i = 0; i < output_shape[current_axis]; ++i) { + // Update the input indices on `input_axis`. + current_input_index[input_axis] = i; + // Write the value from `input_tensor` if it is the last axis or + // recurse into the next axis. + if (is_last_axis) { + int64_t input_flat_index = ElementsAttr::getFlattenedIndex( + input_shape_type, current_input_index); + // Address of input element to write raw data. + const char* input_element_addr = + raw_input + (input_flat_index * element_byte_size); + + // Assert that input_element_addr is not NULL before memcpy. + assert(input_element_addr != nullptr && "input_element_addr is NULL"); + + // Assert that output_element_addr is not NULL. We're modifying + // it through a reference, but it should still point to valid memory. + assert(output_element_addr != nullptr && "output_element_addr is NULL"); + + std::memcpy(output_element_addr, input_element_addr, element_byte_size); + // Increment the next output address to write to by bytes equal to + // width of constituent elements. + output_element_addr += element_byte_size; + } else { + ComputePermutation(perms, output_shape, raw_input, element_byte_size, + current_axis + 1, output_element_addr, + current_input_index, input_shape_type); + } + } +} + +} // namespace + +void TransposeOp::getCanonicalizationPatterns(RewritePatternSet& results, + MLIRContext* context) { + results.add(context); +} + +OpFoldResult TransposeOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + auto operands = adaptor.getOperands(); + + auto input_tensor = mlir::dyn_cast_or_null(operands[0]); + auto perm_tensor = mlir::dyn_cast_or_null(operands[1]); + if (!input_tensor || !perm_tensor) return nullptr; + + // Do not try to fold elements attr of a quant type because + // DenseElementsAttr does not support it. + if (!mlir::cast(getType()) + .getElementType() + .isSignlessIntOrFloat()) + return nullptr; + + // TODO(b/280099953) This algorithm only works for fixed width element types. + // This is the usual case, but consider falling back to old approach + // if transposing string tensors becomes needed while folding. + if (!input_tensor.getElementType().isIntOrIndexOrFloat()) return nullptr; + auto input_type = mlir::cast(input_tensor.getType()); + const int num_dimensions = input_type.getRank(); + + // If the input tensor is a 1d tensor, then transpose is a no-op. + if (num_dimensions == 1) return input_tensor; + + ArrayRef input_shape = input_type.getShape(); + auto attr_iter = GetValues(perm_tensor); + SmallVector output_shape; + SmallVector perms; + for (int i = 0; i < num_dimensions; ++i) { + perms.push_back(attr_iter[i]); + output_shape.push_back(input_shape[perms[i]]); + } + + if (auto dense_elements = + mlir::dyn_cast_or_null(operands[0])) { + // If the input tensor values are splat, then it has exactly one value. + // It is sufficient then to just reshape the input data. + if (dense_elements.isSplat()) { + return dense_elements.reshape(dense_elements.getType().cloneWith( + output_shape, dense_elements.getElementType())); + } + + // MLIR implementation pads elements < 8 bits to 8 bits and pads non byte + // aligned to the nearest byte. So this is allowed. + const char* raw_input = dense_elements.getRawData().data(); + const int element_byte_size = + dense_elements.getElementType().getIntOrFloatBitWidth() / 8; + + // Hold current ND index in input tensor when computing + // permutation. + llvm::SmallVector current_input_index(input_type.getRank()); + + // Allocate raw data and retrieve address of the first char in its raw + // buffer. + llvm::SmallVector raw_output_arr(dense_elements.getRawData().begin(), + dense_elements.getRawData().end()); + char* raw_output = (char*)raw_output_arr.data(); + + // Compute the result and write to `raw_output`. + ComputePermutation(perms, output_shape, raw_input, element_byte_size, + /*current_axis=*/0, raw_output, current_input_index, + input_type); + + const bool valid_output_buffer = + DenseElementsAttr::isValidRawBuffer(input_type, raw_output_arr); + if (!valid_output_buffer) return nullptr; + + auto result_type = + RankedTensorType::get(output_shape, input_tensor.getElementType()); + return DenseElementsAttr::getFromRawBuffer(result_type, raw_output_arr); + + } else if (auto dense_resource_elements = + mlir::dyn_cast_or_null( + operands[0])) { + if (AsmResourceBlob* blob = + dense_resource_elements.getRawHandle().getBlob()) { + const int element_byte_size = + input_tensor.getElementType().getIntOrFloatBitWidth() / 8; + + // Hold current ND index in input tensor when computing + // permutation. + llvm::SmallVector current_input_index(input_type.getRank()); + + // Allocate raw data and retrieve address of the first char in its raw + // buffer. + auto result_type = + RankedTensorType::get(output_shape, input_tensor.getElementType()); + auto raw_output_blob = + mlir::HeapAsmResourceBlob::allocate(GetSizeInBytes(result_type), + /*align=*/64, + /*dataIsMutable=*/true); + ArrayRef data = raw_output_blob.getDataAs(); + llvm::MutableArrayRef raw_output_arr = mlir::MutableArrayRef( + const_cast(data.data()), data.size()); + char* raw_output = (char*)raw_output_arr.data(); + const char* raw_input = blob->getData().data(); + if (raw_input != nullptr) { + static absl::Mutex compute_permutation_mutex(absl::kConstInit); + absl::MutexLock lock(compute_permutation_mutex); + // Compute the result and write to `raw_output`. + ComputePermutation(perms, output_shape, raw_input, element_byte_size, + /*current_axis=*/0, raw_output, current_input_index, + input_type); + return DenseResourceElementsAttr::get(result_type, + "tfl_transpose_op_fold_result", + std::move(raw_output_blob)); + } + } + } + + return nullptr; +} + +mlir::LogicalResult TransposeOp::verify() { + TransposeOp op = *this; + auto input_type = mlir::cast(op.getInput().getType()); + auto perm_type = mlir::cast(op.getPerm().getType()); + auto output_type = mlir::cast(op.getOutput().getType()); + if (input_type.hasStaticShape() && perm_type.hasStaticShape()) { + if (perm_type.getNumElements() != input_type.getRank()) { + return op.emitOpError( + "perm tensor elements size is not equal to input tensor rank"); + } + } + + DenseIntElementsAttr perm; + if (!matchPattern(op.getPerm(), m_Constant(&perm))) { + return success(); + } + + int index = 0; + llvm::SmallVector axes; + for (const auto& axis_int : perm.getValues()) { + int64_t axis = axis_int.getSExtValue(); + if (axis < 0) { + axis += input_type.getRank(); + } + if (axis < 0 || (input_type.hasRank() && axis >= input_type.getRank())) { + return op.emitOpError( + llvm::formatv("perm[{0}] must be in [-rank, rank)", index)); + } + if (std::count(axes.begin(), axes.end(), axis) > 0) { + return op.emitOpError( + llvm::formatv("perm[{0}] cannot have duplicated axis", index)); + } + axes.push_back(axis); + index++; + } + + if (input_type.hasStaticShape() && output_type.hasStaticShape()) { + llvm::SmallVector transposed_shape; + for (int64_t axis : axes) { + transposed_shape.push_back(input_type.getDimSize(axis)); + } + auto expected_output_type = tensorflow::GetTypeFromTFTensorShape( + transposed_shape, input_type.getElementType()); + if (failed( + mlir::verifyCompatibleShape(output_type, expected_output_type))) { + return op.emitOpError(llvm::formatv("expect output type {0}, got {1}", + expected_output_type, output_type)); + } + } + + // Verify the quantized axis if the type is UniformQuantizedPerAxisType. Other + // verifications to make sure the input and output has the same quantization + // type, scale and zero point are performed by the SameOperandsAndResultsScale + // trait. + auto in_per_axis_qtype = + mlir::dyn_cast_or_null( + QuantizedType::getQuantizedElementType(input_type)); + auto out_per_axis_qtype = + mlir::dyn_cast_or_null( + QuantizedType::getQuantizedElementType(output_type)); + if (in_per_axis_qtype && out_per_axis_qtype) { + if (out_per_axis_qtype.getQuantizedDimension() < axes.size() && + axes[out_per_axis_qtype.getQuantizedDimension()] != + in_per_axis_qtype.getQuantizedDimension()) { + return op.emitOpError( + "has mismatched quantized axes of input and output"); + } + } + + return success(); +} + +static void BuildTransposeOp(OpBuilder* builder, OperationState& result, + Value input, Value perm) { + // Output size is only known if input is ranked and perm is a constant. + auto input_type = mlir::cast(input.getType()); + DenseIntElementsAttr perm_const; + if (!input_type.hasRank() || !matchPattern(perm, m_Constant(&perm_const)) || + perm_const.empty()) { + TFL::TransposeOp::build( + *builder, result, UnrankedTensorType::get(input_type.getElementType()), + input, perm); + return; + } + + const auto perm_value_it = perm_const.value_begin(); + + const ArrayRef input_shape = input_type.getShape(); + SmallVector output_shape(input_shape.size()); + + for (int i = 0; i < output_shape.size(); ++i) { + const APInt perm_val = perm_value_it[i]; + output_shape[i] = input_shape[perm_val.getSExtValue()]; + } + + auto element_type = input_type.getElementType(); + // For UniformQuantizedPerAxisType element type, the quantized dimension + // should be changed corresponding with the transpose. + auto per_axis_qtype = + mlir::dyn_cast_or_null( + QuantizedType::getQuantizedElementType(input_type)); + if (per_axis_qtype) { + int32_t quantized_dimension = per_axis_qtype.getQuantizedDimension(); + for (int i = 0; i < output_shape.size(); ++i) { + const APInt perm_val = perm_value_it[i]; + if (perm_val.getSExtValue() == quantized_dimension) { + quantized_dimension = i; + break; + } + } + element_type = quant::UniformQuantizedPerAxisType::get( + per_axis_qtype.getFlags(), per_axis_qtype.getStorageType(), + per_axis_qtype.getExpressedType(), per_axis_qtype.getScales(), + per_axis_qtype.getZeroPoints(), quantized_dimension, + per_axis_qtype.getStorageTypeMin(), per_axis_qtype.getStorageTypeMax()); + } + + TFL::TransposeOp::build( + *builder, result, + tensorflow::GetTypeFromTFTensorShape(output_shape, element_type), input, + perm); +} + +//===----------------------------------------------------------------------===// +// IfOp +//===----------------------------------------------------------------------===// + +/// Given the region at `index`, or the parent operation if `index` is None, +/// return the successor regions. These are the regions that may be selected +/// during the flow of control. `operands` is a set of optional attributes that +/// correspond to a constant value for each operand, or null if that operand is +/// not a constant. +void IfOp::getSuccessorRegions(RegionBranchPoint point, + + SmallVectorImpl& regions) { + // The `then` and the `else` region branch back to the parent operation. + if (!point.isParent()) { + regions.push_back(RegionSuccessor(getOperation())); + return; + } + + // Don't consider the else region if it is empty. + Region* else_reg = &getElseRegion(); + if (else_reg->empty()) else_reg = nullptr; + + // If the condition isn't constant, both regions may be executed. + regions.push_back(RegionSuccessor(&getThenRegion())); + // If the else region does not exist, it is not a viable successor. + if (else_reg) regions.push_back(RegionSuccessor(else_reg)); +} + +void IfOp::getEntrySuccessorRegions(ArrayRef operands, + SmallVectorImpl& regions) { + // Don't consider the else region if it is empty. + Region* else_reg = &getElseRegion(); + if (else_reg->empty()) else_reg = nullptr; + + bool condition; + if (auto cond_attr = mlir::dyn_cast_or_null(operands.front())) { + condition = cond_attr.getValue().isOne(); + + // Add the successor regions using the condition. + regions.push_back(RegionSuccessor(condition ? &getThenRegion() : else_reg)); + } else { + // If the condition isn't constant, both regions may be executed. + regions.push_back(RegionSuccessor(&getThenRegion())); + // If the else region does not exist, it is not a viable successor. + if (else_reg) regions.push_back(RegionSuccessor(else_reg)); + return; + } +} + +mlir::ValueRange IfOp::getSuccessorInputs(RegionSuccessor successor) { + return successor.isOperation() ? getOperation()->getResults() : ValueRange(); +} + +//===----------------------------------------------------------------------===// +// PolyCallOp +//===----------------------------------------------------------------------===// + +namespace { +// Canonicalize converted TF ops into PolymorphicCall op so different +// representations are preserved. +struct PolyCallResultOperandsMatchAndImplicitCapture + : public OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(PolyCallOp while_op, + PatternRewriter& rewriter) const override { + // Finish this. + return success(); + } +}; + +} // namespace + +void PolyCallOp::getCanonicalizationPatterns(RewritePatternSet& results, + MLIRContext* context) { + results.add(context); +} + +void PolyCallOp::getSuccessorRegions( + RegionBranchPoint point, SmallVectorImpl& regions) { + // Defaults to first region for TFLite execution. +} + +//===----------------------------------------------------------------------===// +// WhileOp +//===----------------------------------------------------------------------===// + +LogicalResult WhileOp::verify() { + WhileOp op = *this; + if (op.getNumOperands() != op.getNumResults()) + return op.emitOpError(llvm::formatv( + "number of operands does not match number of results ({0} != {1})", + op.getNumOperands(), op.getNumResults())); + if (op.getCond().front().getNumArguments() != + op.getBody().front().getNumArguments()) + return op.emitOpError(llvm::formatv( + "number of arguments in condition function does not match number of " + "arguments in body function ({0} != {1})", + op.getCond().front().getNumArguments(), + op.getBody().front().getNumArguments())); + // Verify shapes are compatible. + for (auto it : llvm::zip(op.getCond().front().getArgumentTypes(), + op.getBody().front().getArgumentTypes())) { + if (failed(mlir::verifyCompatibleShape(std::get<0>(it), std::get<1>(it)))) + return op->emitOpError(llvm::formatv( + "condition function's argument type does not match body " + "function's argument type ({0} != {1})", + std::get<0>(it), std::get<1>(it))); + } + + return success(); +} + +namespace { +// Canonicalize While op so that results and operands match and external values +// are via implicit capture rather than via block args. +struct WhileResultOperandsMatchAndImplicitCapture + : public OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(WhileOp while_op, + PatternRewriter& rewriter) const override { + // Replace values simply passed through the body with extern values + // (in both body and condition regions as well as while result). The + // block arguments of body and while match and so the corresponding cond + // argument can be easily found. + bool unchanged = true; + auto& body_block = while_op.getBody().front(); + auto& cond_block = while_op.getCond().front(); + auto& yield = *body_block.getTerminator(); + for (auto ba : body_block.getArguments()) { + int arg_no = ba.getArgNumber(); + // Skip removing resources that are not read-only variables. + if (mlir::isa(getElementTypeOrSelf(ba.getType()))) { + bool has_read_only_variables = true; + for (auto user : ba.getUsers()) { + // Terminator ops, for example, tfl::yield op, should be ignored since + // the argument can be used for yielding as the `body` function result + // and that does not give any meaningful points to the decision + // whether the given argument is a read-only variable or not. + if (user->hasTrait()) continue; + if (!llvm::isa(user)) { + has_read_only_variables = false; + break; + } + } + if (!has_read_only_variables) continue; + } + if (ba == yield.getOperand(arg_no)) { + unchanged = false; + auto value = while_op.getOperand(arg_no); + ba.replaceAllUsesWith(value); + cond_block.getArgument(arg_no).replaceAllUsesWith(value); + + // This could be relaxed and casts inserted. + if (while_op.getResult(arg_no).getType() == value.getType()) + while_op.getResult(arg_no).replaceAllUsesWith(value); + } + } + + // The While ops operands and result types need to match + SmallVector new_operands; + SmallVector new_body_yield; + SmallVector removed_operand(while_op.getNumOperands(), false); + llvm::SmallVector types; + new_operands.reserve(while_op.getNumOperands()); + new_body_yield.reserve(while_op.getNumOperands()); + types.reserve(while_op.getNumOperands()); + + // Remove block arguments not used in either cond or body. This leaves the + // block arguments of body and cond matching still. + int arg_index = 0; + for (int while_index = 0, e = while_op.getNumOperands(); while_index < e; + ++while_index) { + auto value = while_op.getOperand(while_index); + if (body_block.getArgument(arg_index).use_empty() && + cond_block.getArgument(arg_index).use_empty() && + // Note: since we are not erasing results, need to use while_index + // to check if the corresponding result is unused. + while_op.getResult(while_index).use_empty()) { + unchanged = false; + body_block.eraseArgument(arg_index); + cond_block.eraseArgument(arg_index); + + // Mark operand for removal. + removed_operand[while_index] = true; + } else { + new_operands.push_back(value); + new_body_yield.push_back(yield.getOperand(while_index)); + auto type = while_op.getResult(while_index).getType(); + types.push_back(type); + ++arg_index; + } + } + + // Done if no values removed from blocks and operands & results match. + if (unchanged) return failure(); + + // Replace with new While with matching operands and results. + Operation* op = while_op.getOperation(); + Operation* new_op = rewriter.insert(Operation::create( + op->getLoc(), op->getName(), types, new_operands, op->getAttrs(), + op->getPropertiesStorage(), {}, /*numRegions=*/2)); + + for (int i = 0; i < 2; ++i) new_op->getRegion(i).takeBody(op->getRegion(i)); + int new_index = 0; + for (int op_index = 0, e = op->getNumResults(); op_index < e; ++op_index) { + if (removed_operand[op_index]) continue; + op->getResult(op_index).replaceAllUsesWith(new_op->getResult(new_index)); + ++new_index; + } + rewriter.eraseOp(op); + + Block& new_body_block = cast(new_op).getBody().front(); + rewriter.setInsertionPointToEnd(&new_body_block); + rewriter.replaceOpWithNewOp(new_body_block.getTerminator(), + new_body_yield); + + return success(); + } +}; + +} // namespace + +void WhileOp::getCanonicalizationPatterns(RewritePatternSet& results, + MLIRContext* context) { + results.add(context); +} + +SmallVector WhileOp::getLoopRegions() { return {&getBody()}; } + +bool WhileOp::isDefinedOutsideOfLoop(Value value) { + // TODO(jpienaar): This is to overly conservative and disables anything other + // than constant hoisting initially. + return false; +} + +//===----------------------------------------------------------------------===// +// CumsumOp +//===----------------------------------------------------------------------===// + +OpFoldResult CumsumOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + auto input = adaptor.getInput(); + auto axis = adaptor.getAxis(); + if (!input || !axis) return nullptr; + + auto input_elements = mlir::dyn_cast(input); + if (!input_elements) return nullptr; + + auto axis_elements = mlir::dyn_cast(axis); + if (!axis_elements || axis_elements.getNumElements() != 1) return nullptr; + int64_t axis_val = axis_elements.getValues()[0].getSExtValue(); + + auto input_type = mlir::cast(getInput().getType()); + if (!input_type.hasRank()) return nullptr; + int rank = input_type.getRank(); + if (axis_val < 0) { + axis_val += rank; + } + if (axis_val < 0 || axis_val >= rank) return nullptr; + + auto result_type = mlir::cast(getType()); + if (!result_type.hasStaticShape()) return nullptr; + + bool exclusive = adaptor.getExclusive(); + bool reverse = adaptor.getReverse(); + + Type elem_type = input_type.getElementType(); + if (elem_type.isF32()) { + return ConstFoldCumsumOp(result_type, input_elements, axis_val, + exclusive, reverse); + } else if (elem_type.isInteger(32)) { + return ConstFoldCumsumOp(result_type, input_elements, axis_val, + exclusive, reverse); + } else if (elem_type.isInteger(64)) { + return ConstFoldCumsumOp(result_type, input_elements, axis_val, + exclusive, reverse); + } + + return nullptr; +} + +//===----------------------------------------------------------------------===// +// LogisticOp +//===----------------------------------------------------------------------===// + +int64_t LogisticOp::GetArithmeticCount(Operation* op) { + int64_t count; + // As a very rough ballpark, the cost of evaluating a math function + // such as tanh or logistic is about 32 multiplications, and about as + // many additions/subtractions. (Just a power-of-two order-of-magnitude + // from looking at actual implementations that we use in runtime/code). + if (ArithmeticCountUtilHelper::GetFirstOutputCount(op, &count)) + return 64 * count; + + return -1; +} + +OpFoldResult LogisticOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + auto operands = adaptor.getOperands(); + Type result_type = getType(); + // Only constant fold for tensor of f32 is implemented. + if (!IsF32ShapedType(result_type)) return nullptr; + + auto compute = [](APFloat value) -> APFloat { + float f = value.convertToFloat(); + float result = 1.0f / (1.0f + std::exp(-f)); + return APFloat(result); + }; + return ConstFoldUnaryOp(result_type, operands[0], compute); +} + +//===----------------------------------------------------------------------===// +// LogSoftmaxOp +//===----------------------------------------------------------------------===// + +int64_t LogSoftmaxOp::GetArithmeticCount(Operation* op) { + int64_t count; + // As a very rough ballpark, the cost of evaluating a math function + // such as tanh or logistic is about 32 multiplications, and about as + // many additions/subtractions. (Just a power-of-two order-of-magnitude + // from looking at actual implementations that we use in runtime/code). + if (ArithmeticCountUtilHelper::GetFirstOutputCount(op, &count)) + return 64 * count; + + return -1; +} + +//===----------------------------------------------------------------------===// +// SoftmaxOp +//===----------------------------------------------------------------------===// + +int64_t SoftmaxOp::GetArithmeticCount(Operation* op) { + int64_t count; + // As a very rough ballpark, the cost of evaluating a math function + // such as tanh or logistic is about 32 multiplications, and about as + // many additions/subtractions. (Just a power-of-two order-of-magnitude + // from looking at actual implementations that we use in runtime/code). + if (ArithmeticCountUtilHelper::GetFirstOutputCount(op, &count)) + return 64 * count; + + return -1; +} + +//===----------------------------------------------------------------------===// +// TanhOp +//===----------------------------------------------------------------------===// + +OpFoldResult TanhOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + auto operands = adaptor.getOperands(); + Type result_type = getType(); + // Only constant fold for tensor of f32 is implemented. + if (!IsF32ShapedType(result_type)) return nullptr; + + auto compute = [](APFloat value) -> APFloat { + float f = value.convertToFloat(); + float result = std::tanh(f); + return APFloat(result); + }; + return ConstFoldUnaryOp(result_type, operands[0], compute); +} + +int64_t TanhOp::GetArithmeticCount(Operation* op) { + int64_t count; + // As a very rough ballpark, the cost of evaluating a math function + // such as tanh or logistic is about 32 multiplications, and about as + // many additions/subtractions. (Just a power-of-two order-of-magnitude + // from looking at actual implementations that we use in runtime/code). + if (ArithmeticCountUtilHelper::GetFirstOutputCount(op, &count)) + return 64 * count; + + return -1; +} + +//===----------------------------------------------------------------------===// +// AddNOp +//===----------------------------------------------------------------------===// + +int64_t AddNOp::GetArithmeticCount(Operation* op) { + int64_t count; + if (ArithmeticCountUtilHelper::GetFirstOutputCount(op, &count)) { + // AddN cost is roughly the same cost as N-1 Adds. + const int64_t num_adds = op->getNumOperands() - 1; + return num_adds * count; + } + + return -1; +} + +//===----------------------------------------------------------------------===// +// AveragePool2DOp +//===----------------------------------------------------------------------===// + +int64_t AveragePool2DOp::GetArithmeticCount(Operation* op) { + int64_t count; + if (ArithmeticCountUtilHelper::GetFirstOutputCount(op, &count)) { + auto avg_pool = llvm::dyn_cast(op); + return avg_pool.getFilterHeight() * avg_pool.getFilterWidth() * count; + } + + return -1; +} + +//===----------------------------------------------------------------------===// +// MaxPool2DOp +//===----------------------------------------------------------------------===// + +int64_t MaxPool2DOp::GetArithmeticCount(Operation* op) { + int64_t count; + if (ArithmeticCountUtilHelper::GetFirstOutputCount(op, &count)) { + auto max_pool = llvm::dyn_cast(op); + return max_pool.getFilterHeight() * max_pool.getFilterWidth() * count; + } + + return -1; +} + +//===----------------------------------------------------------------------===// +// ReverseV2Op +//===----------------------------------------------------------------------===// + +OpFoldResult ReverseV2Op::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + auto input = adaptor.getInput(); + auto axis = adaptor.getAxis(); + + if (!input || !axis) { + return {}; + } + + auto axis_val = llvm::cast(axis); + llvm::SetVector axis_set(axis_val.value_begin(), + axis_val.value_end()); + + auto input_type = getInput().getType(); + const FlatIndHelper helper(input_type); + + std::vector new_data(input_type.getNumElements()); + auto input_data = llvm::cast(input); + + for (auto [i, val] : llvm::enumerate(input_data.getValues())) { + auto shaped_ind = helper.GetShapedInd(i); + + for (int d = 0; d < shaped_ind.size(); ++d) { + if (!axis_set.contains(d)) { + continue; + } + + shaped_ind[d] = input_type.getDimSize(d) - 1 - shaped_ind[d]; + } + + new_data[helper.GetFlatInd(shaped_ind)] = val; + } + + return DenseElementsAttr::get(getType(), new_data); +} + +//===----------------------------------------------------------------------===// +// L2NormalizationOp +//===----------------------------------------------------------------------===// + +int64_t L2NormalizationOp::GetArithmeticCount(Operation* op) { + int64_t count; + // Computing the squared L2 norm is N multiply-adds so 2N ops, + // then the single inverse-sqrt is negligible, then we multiply each + // value by the resulting multiplier, so an extra N ops. count 3N ops. + if (ArithmeticCountUtilHelper::GetFirstOutputCount(op, &count)) { + return 3 * count; + } + + return -1; +} + +//===----------------------------------------------------------------------===// +// PadOp +//===----------------------------------------------------------------------===// + +OpFoldResult PadOp::fold(FoldAdaptor) { + if (InputOutputHasSameShape(getInput().getType(), getOutput().getType())) + return getInput(); + + return {}; +} + +// When padding amounts are constants, cast them to i32. XNN can only +// consume i32 pad amounts in some cases. +struct CastConstPadAmounts : public OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(PadOp op, + PatternRewriter& rewriter) const override { + auto padding_amount_op = op.getPadding().getDefiningOp(); + if (!padding_amount_op || + !padding_amount_op->hasTrait()) { + return failure(); + } + + auto padding_type = op.getPadding().getType(); + if (!padding_type.getElementType().isSignlessInteger(64)) { + return failure(); + } + + auto cast = rewriter.createOrFold( + padding_amount_op->getLoc(), + padding_type.clone(rewriter.getIntegerType(32)), op.getPadding()); + + rewriter.modifyOpInPlace(op, [&]() { op->setOperand(1, cast); }); + + return success(); + } +}; + +void PadOp::getCanonicalizationPatterns(RewritePatternSet& results, + MLIRContext* context) { + results.add(context); +} + +//===----------------------------------------------------------------------===// +// PadV2Op +//===----------------------------------------------------------------------===// + +OpFoldResult PadV2Op::fold(FoldAdaptor) { + if (InputOutputHasSameShape(getInput().getType(), getOutput().getType())) + return getInput(); + + return {}; +} + +//===----------------------------------------------------------------------===// +// NoValueOp +//===----------------------------------------------------------------------===// + +OpFoldResult NoValueOp::fold(FoldAdaptor) { return getValueAttr(); } + +bool NoValueOp::isBuildableWith(Attribute value, Type type) { + return mlir::isa(value) && mlir::isa(type); +} + +YieldOp ControlNodeOp::GetYield() { + return llvm::cast(GetBody().back()); +} + +// Checks if a TFL.control_node wraps a single operation and the single +// operation results are perfectly forwarded to the wrapper's yield. +bool ControlNodeOp::WrapsSinglePerfectlyForwardedOp() { + auto body = GetBody().without_terminator(); + if (!hasSingleElement(body)) return false; + + Operation& controlled_op = *body.begin(); + YieldOp yield = GetYield(); + return controlled_op.getNumResults() == yield.getNumOperands() && + std::equal(controlled_op.getResults().begin(), + controlled_op.getResults().end(), + yield.getOperands().begin()); +} + +mlir::LogicalResult ControlNodeOp::verify() { + ControlNodeOp control_node = *this; + if (!control_node.GetBody().args_empty()) + return control_node.emitOpError() << "expects body without any arguments"; + + Operation& yield = control_node.GetBody().back(); + if (!isa(yield)) + return yield.emitOpError() + << "invalid TFL.control_node terminator, yield expected"; + + // Ensure that the terminator's operands and the control_node results match in + // types. + const int result_count = + control_node.getNumResults() - 1; // 1 for control token + const int num_operands = yield.getNumOperands(); + if (num_operands != result_count) + return yield.emitOpError() + << "has " << yield.getNumOperands() + << " operand, but control_node returns " << result_count; + for (const int operand_idx : llvm::seq(0, yield.getNumOperands())) { + if (control_node.getResult(operand_idx).getType() != + yield.getOperand(operand_idx).getType()) + return yield.emitOpError() << "operand #" << operand_idx + << " type mismatch control_node results"; + } + return success(); +} + +void ControlNodeOp::print(OpAsmPrinter& p) { + if (getNumOperands()) { + // These are always control operand, no explicit type needed. + p << '('; + p.printOperands(getOperands()); + p << ')'; + } + // Check if we can print the short "controls" form: that is if the + // control_node contains a single operation and the results of this operation + // are perfectly forwarded to the yield. + if (getOperation()->getAttrs().empty() && WrapsSinglePerfectlyForwardedOp()) { + Operation& controlled_op = GetBody().front(); + // The "controls" syntax only encodes a single location. + YieldOp yield_op = GetYield(); + // In order to correctly round-trip, we can only use this syntax when all + // the locations are identical. + if (controlled_op.getLoc() == getLoc() && yield_op.getLoc() == getLoc()) { + p << " controls "; + p.printGenericOp(&controlled_op); + return; + } + } + p << ' '; + p.printRegion(getOperation()->getRegion(0)); + p.printOptionalAttrDict(getOperation()->getAttrs()); +} + +ParseResult ControlNodeOp::parse(OpAsmParser& parser, OperationState& result) { + // Parse the body region. + llvm::SMLoc loc = parser.getCurrentLocation(); + Type control_type = ControlType::get(parser.getBuilder().getContext()); + + // Parse optional argument list (control dependencies only). + SmallVector op_infos; + if (parser.parseOperandList(op_infos, OpAsmParser::Delimiter::OptionalParen)) + return failure(); + if (!op_infos.empty()) { + SmallVector types(op_infos.size(), control_type); + if (parser.resolveOperands(op_infos, types, loc, result.operands)) + return failure(); + } + + Region& body = *result.addRegion(); + + if (succeeded(parser.parseOptionalKeyword("controls"))) { + // If we parse the short version of the control node, we have an operation + // in the generic form that follows the "controls" keyword. Parse it inside + // the region and forward all of its results as-is to the yield operation. + body.push_back(new Block); + Block& block = body.back(); + Operation* controlled_op = + parser.parseGenericOperation(&block, block.begin()); + if (!controlled_op) return failure(); + OpBuilder builder(parser.getBuilder().getContext()); + builder.setInsertionPointToEnd(&block); + YieldOp::create(builder, controlled_op->getLoc(), + controlled_op->getResults()); + result.location = controlled_op->getLoc(); + } else if (parser.parseRegion(body)) { + return failure(); + } + + ControlNodeOp::ensureTerminator(body, parser.getBuilder(), result.location); + + // Get the results type for the control node from the terminator operands. + Operation& yield = body.back().back(); + result.types.reserve(yield.getNumOperands() + 1); + result.types.append(yield.operand_type_begin(), yield.operand_type_end()); + result.types.push_back(control_type); + + // Parse the optional attribute list. + if (parser.parseOptionalAttrDict(result.attributes)) return failure(); + return success(); +} + +//===----------------------------------------------------------------------===// +// EmbeddingLookupOp +//===----------------------------------------------------------------------===// + +OpFoldResult EmbeddingLookupOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + auto operands = adaptor.getOperands(); + auto lookup_attr = mlir::dyn_cast_or_null(operands[0]); + auto value_attr = mlir::dyn_cast_or_null(operands[1]); + if (!lookup_attr || !value_attr) { + return {}; + } + + // Hybrid lookup is not supported. + if (value_attr.getType().getElementType() != getType().getElementType()) { + return {}; + } + + // Expect 1d lookup tensor and a non-scalar input. + if (lookup_attr.getType().getRank() != 1 || + value_attr.getType().getRank() == 0) { + return {}; + } + + int64_t outer_dim_size = value_attr.getType().getShape()[0]; + int64_t inner_dims_size = value_attr.getNumElements() / outer_dim_size; + + std::vector new_values; + for (auto outer_index : lookup_attr.getValues()) { + // Fail if index is out of bounds. + if (outer_index < 0 || outer_index >= outer_dim_size) { + return {}; + } + + for (int i = 0; i < inner_dims_size; ++i) { + new_values.push_back( + value_attr.getValues()[outer_index * inner_dims_size + i]); + } + } + + std::vector new_shape = value_attr.getType().getShape().vec(); + new_shape[0] = lookup_attr.getType().getShape()[0]; + auto new_type = value_attr.getType().clone(new_shape); + + return DenseElementsAttr::get(new_type, new_values); +} + +//===----------------------------------------------------------------------===// +// ConstBytesAttr +//===----------------------------------------------------------------------===// + +Attribute ConstBytesAttr::parse(AsmParser& parser, Type type) { + if (parser.parseColon()) { + return nullptr; + } + + std::string data; + if (parser.parseString(&data)) { + return nullptr; + } + if (data.size() >= 2 && data.substr(0, 2) == "0x") { + std::string bytes_data = absl::HexStringToBytes(data.substr(2)); + return ConstBytesAttr::get(parser.getBuilder().getContext(), bytes_data); + } + + return ConstBytesAttr::get(parser.getBuilder().getContext(), data); +} + +void ConstBytesAttr::print(mlir::AsmPrinter& printer) const { + StringRef bytes_str = getValue(); + // Elide the attribute if flag is set. + std::optional limit = OpPrintingFlags().getLargeElementsAttrLimit(); + printer << " : \""; + if (limit && limit.value() < bytes_str.size()) { + printer << "__elided__"; + } else { + printer << "0x" << llvm::toHex(bytes_str); + } + printer << "\""; +} + +//===----------------------------------------------------------------------===// +// BitcastOp +//===----------------------------------------------------------------------===// + +int64_t GetTypeBitWidth(mlir::Type type) { + if (auto quant_type = mlir::dyn_cast(type)) { + return quant_type.getStorageTypeIntegralWidth(); + } + if (type.isIntOrFloat()) { + return std::max(type.getIntOrFloatBitWidth(), + static_cast(CHAR_BIT)); + } + return -1; +} + +LogicalResult BitcastOp::verify() { + BitcastOp op = *this; + auto input_type = mlir::cast(op.getInput().getType()); + auto output_type = mlir::cast(op.getOutput().getType()); + + auto input_element_type = input_type.getElementType(); + auto output_element_type = output_type.getElementType(); + + if (input_type.hasStaticShape()) { + const int input_element_type_bitwidth = GetTypeBitWidth(input_element_type); + const int output_element_type_bitwidth = + GetTypeBitWidth(output_element_type); + + if (input_element_type_bitwidth < 0 || output_element_type_bitwidth < 0) { + // Only supports quantized type, int and float types. + return op.emitOpError("Unsupported element type."); + } + + if (input_element_type_bitwidth < output_element_type_bitwidth) { + if (output_element_type_bitwidth % input_element_type_bitwidth != 0) { + return op.emitOpError( + "output element bitwidth is not multiple of input element " + "bitwidth"); + } + if (input_type.getShape().empty() || + input_type.getShape().back() % (output_element_type_bitwidth / + input_element_type_bitwidth) != + 0) { + return op.emitOpError( + "input rightmost dimension size is not multiple of the divisor"); + } + } else if (input_element_type_bitwidth > output_element_type_bitwidth) { + if (input_element_type_bitwidth % output_element_type_bitwidth != 0) { + return op.emitOpError( + "input element bitwidth is not multiple of output element " + "bitwidth"); + } + } + } + return success(); +} + +OpFoldResult BitcastOp::fold(FoldAdaptor adaptor) { + if (!ShouldFoldOperation(this->getOperation())) return {}; + + if (getType() == getInput().getType()) return getInput(); + return {}; +} + +//===----------------------------------------------------------------------===// +// DynamicUpdateSliceOp +//===----------------------------------------------------------------------===// + +OpFoldResult DynamicUpdateSliceOp::fold(FoldAdaptor) { + // Check if update replaces the whole tensor, meaning operand and update has + // the same shape and all start indices are zero. + DenseIntElementsAttr indices_attr; + if (matchPattern(getStartIndices(), m_Constant(&indices_attr)) && + indices_attr.isSplat() && indices_attr.getSplatValue() == 0 && + getOperand().getType().hasStaticShape() && + getUpdate().getType().hasStaticShape() && + getOperand().getType() == getUpdate().getType()) { + return getUpdate(); + } + + return {}; +} + +//===----------------------------------------------------------------------===// +// TableGen'd op method definitions +//===----------------------------------------------------------------------===// + +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops_interface.cc.inc" + +static FailureOr> parseI32Array(AsmParser& parser) { + SmallVector elements; + auto elementParser = [&]() { + int32_t element; + if (failed(parser.parseInteger(element))) return failure(); + elements.push_back(element); + return success(); + }; + if (parser.parseCommaSeparatedList(AsmParser::Delimiter::Square, + elementParser)) + return failure(); + return elements; +} + +} // namespace TFL +} // namespace mlir + +using namespace mlir; // NOLINT +using mlir::TFL::ControlType; +using mlir::TFL::LSTMKernelTypeAttr; + +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops_dialect.cc.inc" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops_enums.cc.inc" +#define GET_ATTRDEF_CLASSES +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops_attrdefs.cc.inc" +#define GET_OP_CLASSES +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.cc.inc" + +namespace mlir { +namespace TFL { + +#include "tensorflow/compiler/mlir/lite/runtime_verifiers.inc" + +Operation* TFLDialect::materializeConstant(OpBuilder& builder, Attribute value, + Type type, Location loc) { + // If this is a constant bytes attribute or the result type doesn't match the + // attribute type, then generate a tfl.pseudo_const. + if (mlir::isa(value) || + (mlir::isa(value) && + mlir::cast(value).getType() != type)) + return ConstOp::create(builder, loc, type, mlir::cast(value)); + if (arith::ConstantOp::isBuildableWith(value, type)) + return arith::ConstantOp::create(builder, loc, type, + cast(value)); + if (NoValueOp::isBuildableWith(value, type)) + return NoValueOp::create(builder, loc, type, mlir::cast(value)); + return nullptr; +} + +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/ir/tfl_ops.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/ir/tfl_ops.h new file mode 100644 index 0000000000000000000000000000000000000000..db515dfcf815ce2a967b3a70e73fcbe6a9c7c985 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/ir/tfl_ops.h @@ -0,0 +1,66 @@ +/* Copyright 2019 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. +==============================================================================*/ + +// This file defines the operations used in the MLIR TensorFlow Lite dialect. + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_IR_TFL_OPS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_IR_TFL_OPS_H_ + +#include "mlir/Dialect/Traits.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/Dialect.h" // from @llvm-project +#include "mlir/IR/DialectImplementation.h" // from @llvm-project +#include "mlir/IR/OpImplementation.h" // from @llvm-project +#include "mlir/IR/TypeSupport.h" // from @llvm-project +#include "mlir/IR/Types.h" // from @llvm-project +#include "mlir/Interfaces/DerivedAttributeOpInterface.h" // from @llvm-project +#include "mlir/Interfaces/InferTypeOpInterface.h" // from @llvm-project +#include "mlir/Interfaces/LoopLikeInterface.h" // from @llvm-project +#include "mlir/Interfaces/SideEffectInterfaces.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/TypeID.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops_dialect.h.inc" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops_enums.h.inc" +#include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_utils.h" // IWYU pragma: keep +#include "tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" +#include "tensorflow/compiler/mlir/lite/utils/utils.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_traits.h" +#define GET_ATTRDEF_CLASSES +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops_attrdefs.h.inc" + +namespace mlir { +namespace TFL { + +typedef TFLDialect TensorFlowLiteDialect; + +// The Control type is a token-like value that models control dependencies +class ControlType : public Type::TypeBase { + public: + using Base::Base; + static constexpr StringLiteral name = "tfl.control"; +}; + +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops_interface.h.inc" + +} // end namespace TFL +} // end namespace mlir + +#define GET_OP_CLASSES +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h.inc" + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_IR_TFL_OPS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/ir/tfl_ops.td b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/ir/tfl_ops.td new file mode 100644 index 0000000000000000000000000000000000000000..b29e619a650302672ebed55b813beeda8401c5e3 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/ir/tfl_ops.td @@ -0,0 +1,6136 @@ +/* Copyright 2023 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. +==============================================================================*/ + +// LINT.IfChange +// This is the operation definition file for TensorFlow Lite. + +#ifndef TFL_OPS +#define TFL_OPS + +include "mlir/IR/OpBase.td" +include "mlir/Interfaces/ControlFlowInterfaces.td" +include "mlir/Interfaces/FunctionInterfaces.td" +include "mlir/Interfaces/InferTypeOpInterface.td" +include "mlir/Dialect/Quant/IR/QuantBase.td" +include "mlir/Interfaces/LoopLikeInterface.td" +include "mlir/Interfaces/SideEffectInterfaces.td" +include "tensorflow/compiler/mlir/lite/ir/tfl_op_interfaces.td" +include "tensorflow/compiler/mlir/lite/ir/tfl_op_enums.td" +include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization.td" +include "tensorflow/compiler/mlir/tensorflow/ir/tf_op_base.td" + +//===----------------------------------------------------------------------===// +// TFLite dialect string type - uses the TF string type as implementation +//===----------------------------------------------------------------------===// +def TFL_Str : Type($_self)">, + "TFLite string type">, + BuildableType<"getType()">; + +//===----------------------------------------------------------------------===// +// TFLite dialect quint8 type - uses the TF quint8 type as implementation +//===----------------------------------------------------------------------===// +def TFL_Quint8 : Type($_self)">, + "TFLite quint8 type">, + BuildableType<"getType()">; + +//===----------------------------------------------------------------------===// +// Type that represents control dependencies +//===----------------------------------------------------------------------===// +def TFL_Control: Type($_self)">, "control">, + BuildableType<"$_builder.getType()">; + + +//===----------------------------------------------------------------------===// +// TensorType attribute definitions. +//===----------------------------------------------------------------------===// +// A type attribute containing the TensorType. +def TensorTypeAttr : TypeAttrBase<"TensorType", "Tensor type attribute">; + +//===----------------------------------------------------------------------===// +// Derived shape attribute class. +//===----------------------------------------------------------------------===// +class DerivedShapeAttr : DerivedAttr<"ArrayRef", body>; +class DerivedTFLiteTypeAttr : + DerivedAttr<"tflite::TensorType", body, convert>; + +// TFL Runtime op trait predicate. +class TFL_RuntimePredOpTrait : + GenInternalOpTrait<"TFLRuntimeOpTrait"> { + Pred tflRuntimePredicate = pred; + string tflRuntimeDescription = desc; +} + +class TFL_OperandsHaveSameShapesOrBroadcastableShape< + list indices, int max_bcast_rank> : + TFL_RuntimePredOpTrait<"operands do not have the same shape or " + "broadcastable shapes within the rank " # max_bcast_rank, + CPred<"TFL::VerifyOperandsHaveSameShapesOrBroadcastableShape(" + "&$_op, llvm::ArrayRef({" # !interleave(indices, ", ") # + "}), " # max_bcast_rank # ")">>; + +// These additional types/type constraints here are used to decouple the ops +// from runtime support for the ops. Prefer to use these types when defining +// new TF_Ops for uniformity. + +// TFL Runtime type predicate. +class TFL_RuntimeType { + Pred tflRuntimeTypePredicate = t.predicate; + string tflRuntimeTypeDescription = t.summary; +} + +class TFL_AnyTypeOf allowedRuntimeTypes, string description = "", + list allowedOpTypes = [AnyType]> : + AnyTypeOf, + TFL_RuntimeType>; + +class TFL_TensorOf allowedRuntimeTypes, + list allowedOpTypes = [AnyType]> : + TensorOf, TFL_RuntimeType> { + // Set the summary equal to that representing the runtime types. + let summary = TensorOf.summary; +} + +class TFL_TensorOfOrNone allowedRuntimeTypes, string description = "", + list allowedOpTypes = [AnyType]> : + AnyTypeOf<[TFL_TensorOf, NoneType], description>, + TFL_RuntimeType, NoneType]>>; + +class TFL_VariadicTensorOf allowedRuntimeTypes, + list allowedOpTypes = [AnyType]> : + Variadic>, + TFL_RuntimeType>>; + +def TFL_I2 : I<2>; +def TFL_I4 : I<4>; +def TFL_Int32Or64 : SignlessIntOfWidths<[32, 64]>; + +def TFL_BoolTensor : TFL_TensorOf<[I1]>; +def TFL_FpTensor : TFL_TensorOf<[F32]>; +def TFL_I32OrI64Tensor : TFL_TensorOf<[TFL_Int32Or64]>; +def TFL_I32Tensor : TFL_TensorOf<[I32]>; +def TFL_I64Tensor : TFL_TensorOf<[I64]>; +def TFL_Complex64Tensor : TFL_TensorOf<[Complex>]>; +def TFL_ResourceTensor : TFL_TensorOf<[TF_Resource]>; + +// TODO(jpienaar): Expand to all int types. +def TFL_IntTensor : TypeAlias; + +class TFL_0DTensorOf allowedRuntimeTypes, + list allowedOpTypes = [AnyType]> : + 0DTensorOf, TFL_RuntimeType>; +class TFL_1DTensorOf allowedRuntimeTypes, + list allowedOpTypes = [AnyType]> : + 1DTensorOf, TFL_RuntimeType>; +class TFL_2DTensorOf allowedRuntimeTypes, + list allowedOpTypes = [AnyType]> : + 2DTensorOf, TFL_RuntimeType>; + +class TFL_1DTensorOfOrNone allowedRuntimeTypes, string description = "", + list allowedOpTypes = [AnyType]> : + AnyTypeOf<[TensorOf, NoneType], description>, + TFL_RuntimeType, NoneType]>>; + +// This is used to represent the type of "ref tensors" or tensors that are +// used as variables to track state. +def TFL_StatefulTensor : TypeAlias; + +//===----------------------------------------------------------------------===// +// Rank/Shape helpers. +//===----------------------------------------------------------------------===// + +// Returns true of operand is none type. +class TFL_OperandIsNoneType : + CPred<"llvm::isa($_op.getOperand(" # i # ").getType())">; + +class TFL_OperandIsUnrankedPred : + CPred<"llvm::isa($_op.getOperand(" # n # ").getType())">; + +// TODO: Some of these could be generalized and/or moved to more general +// location. +// Returns true if the n-th operand has unknown rank or has rank m. +class TFL_OperandHasRank : + PredOpTrait<"operand " # n # " is " # m # "-D", + Or<[TFL_OperandIsUnrankedPred, + CPred<"llvm::cast($_op.getOperand(" # n # + ").getType()).getRank() == " # m>]>>; + +// Returns true if the n-th operand is ranked and has rank dim. +class TFL_OperandHasKnownRank : And<[ + CPred<"llvm::isa($_op.getOperand(" # n # ").getType())">, + CPred<"llvm::cast($_op.getOperand(" # n # ").getType()).getRank() == " + # dim>]>; + +// True if operand n is ranked and has a rank > dim. +class TFL_OperandIsRankedAndHasDimPred : And<[ + CPred<"llvm::isa($_op.getOperand(" # n # ").getType())">, + CPred<"llvm::cast($_op.getOperand(" # n # ").getType()).getRank() > " + # dim>]>; + +// Returns true if the n-th operand is ranked and has a dimension length = size +// at the rank dim. +class TFL_OperandDimEquals : And<[ + TFL_OperandIsRankedAndHasDimPred, + CPred<"llvm::cast($_op.getOperand(" # n # ").getType())" + ".getShape()[" # dim # " ] == " # size>]>; + +// Returns true if the n-th operand is ranked and has a dimension length <= +// size at the rank dim. +class TFL_OperandDimIsAtMost : And<[ + TFL_OperandIsRankedAndHasDimPred, + CPred<"llvm::cast($_op.getOperand(" # n # ").getType())" + ".getShape()[" # dim # " ] <= " # size>]>; + +// Returns true if the n-th operand has unknown rank or at least rank m. +class TFL_OperandHasAtleastRank : + PredOpTrait<"operand " # n # " is " # m # "-D", + Or<[CPred<"llvm::isa($_op.getOperand(" # n # ").getType())">, + CPred<"llvm::cast($_op.getOperand(" # n # + ").getType()).getRank() >= " # m>]>>; + +class TFL_OperandRankEquals1DimOfOperand : + PredOpTrait<"operand " # x # "'s rank equals operand " # y # "'s size", + Or<[TFL_OperandIsUnrankedPred, + TFL_OperandIsUnrankedPred, + CPred<"!llvm::cast($_op.getOperand(" # y # + ").getType()).hasStaticShape()">, + CPred<"llvm::cast($_op.getOperand(" # x # + ").getType()).getRank() == " + "llvm::cast($_op.getOperand(" # y # + ").getType()).getShape()[0]">]>>; + +class TFL_Operand0DOr1ElementTensor : + PredOpTrait<"operand #" # x # " is an 0-d tensor or 1-d tensor w/ 1 element", + Or<[TFL_OperandHasKnownRank, + And<[TFL_OperandHasKnownRank, TFL_OperandDimEquals]>]>>; + +// Return true if i-th dim of x-th operand is the same as j-th dim of y-th +// operand or any of those operands does not have static shape. +class TFL_OperandsHaveSameDims : + Or<[TFL_OperandIsUnrankedPred, + TFL_OperandIsUnrankedPred, + CPred<"!llvm::cast($_op.getOperand(" # x # + ").getType()).hasStaticShape()">, + CPred<"!llvm::cast($_op.getOperand(" # y # + ").getType()).hasStaticShape()">, + CPred<"llvm::cast($_op.getOperand(" # x # + ").getType()).getShape()[" # i # "] == " + "llvm::cast($_op.getOperand(" # y # + ").getType()).getShape()[" # j # "]">]>; + +class TFL_OperandsHaveSameDimsTrait : + PredOpTrait<"dim " # i # " of operand " # x # " equals to dim " # j # + " of operand " # y, + TFL_OperandsHaveSameDims>; + +// Return true if number of elements of x-th operand is the same as j-th dim of +// y-th operand or any of those operands does not have static shape. +class TFL_NumElementsEqualsDim : + Or<[TFL_OperandIsUnrankedPred, + TFL_OperandIsUnrankedPred, + CPred<"!llvm::cast($_op.getOperand(" # x # + ").getType()).hasStaticShape()">, + CPred<"!llvm::cast($_op.getOperand(" # y # + ").getType()).hasStaticShape()">, + CPred<"llvm::cast($_op.getOperand(" # x # + ").getType()).getNumElements() == " + "llvm::cast($_op.getOperand(" # y # + ").getType()).getShape()[" # j # "]">]>; + +class TFL_NumElementsEqualsDimTrait : + PredOpTrait<"operand " # x # " has num of elements equals to dim " # j # + " of operand " # y, + TFL_NumElementsEqualsDim>; + +// Return true if number of elements of x-th operand equals to n. +class TFL_NumElements : + Or<[TFL_OperandIsUnrankedPred, + CPred<"!llvm::cast($_op.getOperand(" # x # + ").getType()).hasStaticShape()">, + CPred<"llvm::cast($_op.getOperand(" # x # + ").getType()).getNumElements() == " # n>]>; + +class TFL_NumElementsTrait : + PredOpTrait<"operand " # x # " has num of elements equals to " # n, + TFL_NumElements>; + +// tf.uint8 and tf.quint8 are mapped to the same tflite types, so they are equal +// when used as element types. +class TFL_TFTypesWithSameBits : + And<[ + Or<[CPred<"llvm::isa(getElementTypeOrSelf($_op.getResult(" # i # ")))">, + CPred<"getElementTypeOrSelf($_op.getResult(" # i # ")).isUnsignedInteger(" # num # ")">]>, + Or<[CPred<"llvm::isa(getElementTypeOrSelf($_op.getOperand(" # j # ")))">, + CPred<"getElementTypeOrSelf($_op.getOperand(" # j # ")).isUnsignedInteger(" # num # ")">]>]>; + +class TFL_TFOperandTypesWithSameBits : + And<[ + Or<[CPred<"llvm::isa(getElementTypeOrSelf($_op.getOperand(" # i # ")))">, + CPred<"getElementTypeOrSelf($_op.getOperand(" # i # ")).isUnsignedInteger(" # num # ")">]>, + Or<[CPred<"llvm::isa(getElementTypeOrSelf($_op.getOperand(" # j # ")))">, + CPred<"getElementTypeOrSelf($_op.getOperand(" # j # ")).isUnsignedInteger(" # num # ")">]>]>; + +class TFL_OperandIsNoneOrHasRank : + PredOpTrait<"operand " # n # " is " # m # "-D", + Or<[ + TFL_OperandIsNoneType, + TFL_OperandIsUnrankedPred, + CPred<"llvm::cast($_op.getOperand(" # n # + ").getType()).getRank() == " # m>]>>; + +class TFL_OperandIsNoneOrHasRankAtMost : + PredOpTrait<"operand " # n # " is at most " # m # "-D", + Or<[ + TFL_OperandIsNoneType, + TFL_OperandIsUnrankedPred, + CPred<"llvm::cast($_op.getOperand(" # n # + ").getType()).getRank() <= " # m>]>>; + +class TFL_OperandHasRankAtMostPred : + Or<[TFL_OperandIsUnrankedPred, + CPred<"llvm::cast($_op.getOperand(" # n # + ").getType()).getRank() <= " # m>]>; + +class TFL_OperandHasRankAtMost : + PredOpTrait<"operand " # n # " is at most " # m # "-D", + TFL_OperandHasRankAtMostPred>; + +// Not all dimentions in the tensor will contribute to the data move in a +// TransposeOp. Effective rank is the number of dimentions != 1 +class TFL_TransposeOperandHasEffectiveRankAtMost : + PredOpTrait<"operand " # n # " is at most " # m # "-D", + Or<[TFL_OperandIsUnrankedPred, + CPred<"llvm::cast(GetSqueezedShape($_op.getOperand(" # n # + "))).size() <= " # m>]>>; + +class TFL_OperandHasRankAtLeast : + PredOpTrait<"operand " # n # " is at least " # m # "-D", + Or<[TFL_OperandIsUnrankedPred, + CPred<"llvm::cast($_op.getOperand(" # n # + ").getType()).getRank() >= " # m>]>>; + +class TFL_OperandHasRankRange : + PredOpTrait<"operand " # n # " has rank range [" # x # ", " # y # "]", + Or<[TFL_OperandIsUnrankedPred, + CPred<"llvm::cast($_op.getOperand(" # n # ").getType()).getRank() " + ">= " # x # " && llvm::cast($_op.getOperand(" # n # ").getType())." + "getRank() <= " # y>]>>; + +def TFL_FloatNonNegative : AttrConstraint< + CPred<"llvm::isa($_self) && " + "!llvm::cast($_self).getValue().isNegative()">, + "whose value is non-negative">; + +def TFL_BoolTrue : AttrConstraint< + CPred<"llvm::isa($_self) && llvm::cast($_self).getValue()">, + "whose value is true">; + +def TFL_BoolFalse : AttrConstraint< + CPred<"llvm::isa($_self) && !llvm::cast($_self).getValue()">, + "whose value is false">; + +class TFL_StringEqualsTo : AttrConstraint< + CPred<"llvm::cast($_self).getValue() == \"" # value # "\"">, + "whose value equals to '" # value # "'">; + +// Ensures the array attribute's size is within the given maximum size. +class TFL_ArrayMaxCount : AttrConstraint< + CPred<"llvm::isa($_self) && llvm::cast($_self).size() <= " # n>, + "whose size is at most " # n>; + +// Ensures the given integer attribute has the given value. +class TFL_IntEqualsTo : AttrConstraint< + CPred<"llvm::isa($_self) && " + "llvm::cast($_self).getInt() == " # n>, + "whose value is " # n>; + +// Ensures the given LSTMKernelType attribute has the given value. +class TFL_LSTMKernelTypeEqualsTo : AttrConstraint< + CPred<"llvm::isa($_self) && " + "llvm::cast($_self).getValue() == " # value>, + "whose value is " # value>; + +// Helper to allow 4-bit integer types compatibility (i4, ui4, si4, quantized 4-bit). +class TFL_4BitIntTypesAreCompatible : And<[ + Or<[CPred<"getElementTypeOrSelf($_op.getResult(" # i # ")).isInteger(4)">, + CPred<"llvm::isa(getElementTypeOrSelf($_op.getResult(" # i # "))) && " + "llvm::cast(getElementTypeOrSelf($_op.getResult(" # i # "))).getStorageTypeIntegralWidth() == 4">]>, + Or<[CPred<"getElementTypeOrSelf($_op.getOperand(" # j # ")).isInteger(4)">, + CPred<"llvm::isa(getElementTypeOrSelf($_op.getOperand(" # j # "))) && " + "llvm::cast(getElementTypeOrSelf($_op.getOperand(" # j # "))).getStorageTypeIntegralWidth() == 4">]> +]>; + +// This is a quantization-aware version of TCresVTEtIsSameAsOp +class TFL_TCresVTEtIsSameAsOp : And<[ + TCOpResIsShapedTypePred, + Or<[ + TCresVTEtIsSameAsOpBase, + TFL_TFTypesWithSameBits, + TFL_4BitIntTypesAreCompatible, + And<[ + SubstLeaves<"$_self", "getElementTypeOrSelf($_op.getOperand(" # j # "))", + quant_QuantizedType.predicate>, + CPred<"quant::QuantizedType::castToStorageType(" + "getElementTypeOrSelf($_op.getResult(" # i # "))) == " + "quant::QuantizedType::castToStorageType(" + "getElementTypeOrSelf($_op.getOperand(" # j # ")))">]>]>]>; + +def TFL_SameFirstOperandAndFirstResultElementType : + PredOpTrait<"values and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>; + +// This is a quantization-aware version of TCopVTEtAreSameAt +class TFL_TCopVTEtAreSameAt : Or<[ + TCopVTEtAreSameAt<[i, j]>, + TFL_TFOperandTypesWithSameBits, + And<[ + SubstLeaves<"$_self", "getElementTypeOrSelf($_op.getOperand(" # j # "))", + quant_QuantizedType.predicate>, + CPred<"quant::QuantizedType::castToStorageType(" + "getElementTypeOrSelf($_op.getOperand(" # i # "))) == " + "quant::QuantizedType::castToStorageType(" + "getElementTypeOrSelf($_op.getOperand(" # j # ")))">]>]>; + +//===----------------------------------------------------------------------===// +// TFL op common constraints. +//===----------------------------------------------------------------------===// +def OperandsSameElementTypeConstraintBasePred : + Or<[TCopVTEtIsSameAs<0, 1>, + // Two operands' values are both quantized and their type have the same + // underlying storage type. + And<[ + SubstLeaves<"$_self", "getElementTypeOrSelf($_op.getOperand(0))", + quant_QuantizedType.predicate>, + CPred<"quant::QuantizedType::castToStorageType(" + "getElementTypeOrSelf($_op.getOperand(0))) == " + "quant::QuantizedType::castToStorageType(" + "getElementTypeOrSelf($_op.getOperand(1)))">]>]>; + +class OperandsSameElementTypeConstraintBase : + PredOpTrait; + +// This is a constraint for most of the binary ops, e.g., add, mul, div, etc. +// Binary ops lhs & rhs should have the same value type, and is capable to +// compare quantization types as well. +def BinaryOpSameElementTypeConstraint : + OperandsSameElementTypeConstraintBase<"binary op">; + +// This is a constraint for most of the comparison ops, e.g., equal, not_equal, +// greater, greater_equal, less, etc. Comparison ops lhs & rhs should have the +// same value type, and is capable to compare quantization types as well. +def ComparisonOpSameElementTypeConstraint : + OperandsSameElementTypeConstraintBase<"comparison op">; + +//===----------------------------------------------------------------------===// +// TFL common builders. +//===----------------------------------------------------------------------===// + +def TFL_BroadcastableBinaryBuilder : + OpBuilder<(ins "Value":$lhs, "Value":$rhs), + [{ + auto resultType = + OpTrait::util::getBroadcastedType(lhs.getType(), rhs.getType()); + if (!resultType) + mlir::emitError($_state.location, "non-broadcastable operands"); + $_state.addOperands({lhs, rhs}); + $_state.types.push_back(resultType); + }]>; + +def TFL_FusedBroadcastableBinaryBuilder : + OpBuilder<(ins "Value":$lhs, "Value":$rhs, + "StringAttr":$fusedActivationFunction), + [{ + buildFusedBroadcastableBinOp( + &$_builder, $_state, lhs, rhs, fusedActivationFunction); + }]>; + +def TFL_ComparisonBinaryBuilder : + OpBuilder<(ins "Value":$lhs, "Value":$rhs), + [{ + buildComparisonBinOp(&$_builder, $_state, lhs, rhs); + }]>; + +//===----------------------------------------------------------------------===// +// TFL op base class. +//===----------------------------------------------------------------------===// + +// LINT.IfChange + +class TFL_Op traits = []> : + Op])> { + // FlatBuffer generation specific information. + // ------------------------------------------- + // When generating the FlatBuffer output some operations have + // Options (as defined in the schema). These options are effectively + // the attributes of the operations (e.g., what padding is to be used + // for a pooling operator). Not all operations have Options and some + // operations share Options. The following attributes indicate whether + // the operation has Options in the serialized FlatBuffer. + + // Whether the TFLite operator has options in the schema representation. + bit hasOptions = 0b0; + // Whether the TFLite operator has options2 in the schema representation. + bit hasOptions2 = 0b0; + + // Use to specify a custom options type for TFLite operators where + // the option's name does not match the TFLite operator's name. + // If no customOption is specified then Options is used if the op + // hasOptions. + string customOption = ?; +} + +class TFL_ConvOp additional_traits = []> : + TFL_Op, + AffineQuantizedOpInterface, + AffineOpCoefficient, + QuantizableResult, + TFL_SparseOp] # additional_traits> { + let summary = opSummary # " operator"; + + let description = [{ + Performs convolution operation on inputs. + + Inputs: + `inputs[0]`: required: the input activation tensor + `inputs[1]`: required: the filter weight tensor + `inputs[2]`: optional: the bias tensor + }]; + + let results = (outs TFL_TensorOf<[F32, QI8, QUI8, QI16]>:$output); + + let hasOptions = 0b1; +} + + +//===----------------------------------------------------------------------===// +// TFL op definitions. +//===----------------------------------------------------------------------===// +def TFL_AbsOp : TFL_Op<"abs", [ + Pure, + QuantizableResult, + SameOperandsAndResultShape]> { + let summary = "Absolute value operator"; + + let description = [{ +Given a tensor `x`, this operation returns a tensor containing the absolute +value of each element in `x`. For example, if x is an input element and y is +an output element, this operation computes \\(y = |x|\\). + }]; + + let arguments = (ins TFL_TensorOf<[I16, I32, F32, QI8, QI16]>:$x); + + let results = (outs TFL_TensorOf<[I16, I32, F32, QI8, QI16]>:$y); + + let hasFolder = 1; + + // This builder doesn't work with quantized type, so it can only be used by + // non-quantization tablegen patterns. + let builders = [ + OpBuilder<(ins "Value":$input), + [{ + $_state.addOperands({input}); + $_state.addTypes(input.getType()); + }]> + ]; +} + +def TFL_DilateOp : TFL_Op<"dilate", [ + PredOpTrait<"output and input must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + PredOpTrait<"output and padding value must have same element type", + TCresVTEtIsSameAsOpBase<0, 2>>, + Pure]> { + let summary = "Dilation operator"; + + let description = "Extends a tensor by adding new elements between the existing ones."; + + let arguments = (ins + TFL_TensorOf<[I8, I16, I32, I64, UI8, UI16, UI32, UI64, F32, F64]>:$input, + TFL_TensorOf<[I32]>:$dilations, + TFL_0DTensorOf<[I8, I16, I32, I64, UI8, UI16, UI32, UI64, F32, F64]>:$padding_value + ); + + let results = (outs TFL_TensorOf<[I8, I16, I32, I64, UI8, UI16, UI32, UI64, F32, F64]>:$output); +} + +def TFL_AddOp : TFL_Op<"add", [ + TFL_RuntimePredOpTrait<"Operands should have valid shapes and element type needs to match", + And<[CPred<"TFL::VerifyAddOpShapeConstraints(llvm::cast($_op))">, + OperandsSameElementTypeConstraintBasePred]>>, + ResultsBroadcastableShape, + Pure, + Commutative, + QuantizableResult, + DeclareOpInterfaceMethods]> { + let summary = "Addition operator"; + + let description = [{ + Element-wise addition operation. + }]; + + let arguments = ( + ins TFL_TensorOf<[F32, F16, I16, I32, I64, QI8, QUI8, QI16]>:$lhs, + TFL_TensorOf<[F32, F16, I16, I32, I64, QI8, QUI8, QI16]>:$rhs, + TFL_AFAttr:$fused_activation_function); + + let results = (outs + TFL_TensorOf<[F32, F16, I16, I32, I64, QI8, QUI8, QI16]>:$output); + + let hasFolder = 1; + + let builders = [TFL_FusedBroadcastableBinaryBuilder]; + + let hasCustomAssemblyFormat = 1; + + let extraClassDefinition = [{ + ParseResult $cppClass::parse(OpAsmParser &parser, OperationState &result) { + return parseOneResultSameOperandTypeOp(parser, result); + } + void $cppClass::print(OpAsmPrinter &p) { + return printOneResultOp(getOperation(), p); + } + }]; + + let hasOptions = 1; +} + +def TFL_AddNOp : TFL_Op<"add_n", [ + Commutative, + Pure, + SameOperandsAndResultsScale, + DeclareOpInterfaceMethods]> { + let summary = "add_n operator"; + + let description = [{ + Adds all input tensors element-wise. + }]; + + let arguments = (ins + TFL_VariadicTensorOf<[F32, I32]>:$inputs + ); + + let results = (outs + TFL_TensorOf<[F32, I32]>:$sum + ); +} + +def TFL_ReduceAnyOp : TFL_Op<"reduce_any", [Pure]> { + let summary = [{ +Computes the "logical or" of elements across dimensions of a tensor. + }]; + + let description = [{ +Reduces `input` along the dimensions given in `axis`. Unless +`keep_dims` is true, the rank of the tensor is reduced by 1 for each entry in +`axis`. If `keep_dims` is true, the reduced dimensions are +retained with length 1. + }]; + + let arguments = (ins + TFL_BoolTensor:$input, + TFL_I32Tensor:$reduction_indices, + + DefaultValuedOptionalAttr:$keep_dims + ); + + let results = (outs + TFL_BoolTensor:$output + ); + + let hasOptions = 1; + let customOption = "ReducerOptions"; +} + +def TFL_ReduceAllOp : TFL_Op<"reduce_all", [Pure]> { + let summary = [{ +Computes the "logical and" of elements across dimensions of a tensor. + }]; + + let description = [{ +Reduces `input` along the dimensions given in `axis`. Unless +`keep_dims` is true, the rank of the tensor is reduced by 1 for each entry in +`axis`. If `keep_dims` is true, the reduced dimensions are +retained with length 1. + }]; + + let arguments = (ins + TFL_BoolTensor:$input, + TFL_I32Tensor:$reduction_indices, + + DefaultValuedOptionalAttr:$keep_dims + ); + + let results = (outs + TFL_BoolTensor:$output + ); + + let hasOptions = 1; + let customOption = "ReducerOptions"; +} + +def TFL_TransposeConvOp: TFL_Op<"transpose_conv", [ + Pure, + TFL_OperandHasRank<0, 1>, + TFL_OperandHasRank<1, 4>, + TFL_OperandHasRank<2, 4>, + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 2>>, + AccumulatorUniformScale<3, 1, 2>, + AffineQuantizedOpInterface, AffineOpCoefficient<0, 1>, + QuantizableResult, + TFL_SparseOp, + DeclareOpInterfaceMethods, + DynamicRangeQuantizedOpInterface, + RequiresQuantizedBiasInterface]> { + let summary = "Transpose convolution operator"; + + let description = [{ + Performs transpose convolution operation on input. + }]; + + let arguments = (ins + TFL_I32Tensor:$output_shape, + TFL_TensorOf<[F32, QI8, QUI8, QI16]>:$weights, + TFL_TensorOf<[F32, QI8, QUI8, QI16]>:$input, + TFL_TensorOfOrNone<[F32, QI32, I64]>:$bias, + TFL_PaddingAttr:$padding, + ConfinedAttr:$stride_h, + ConfinedAttr:$stride_w, + TFL_AFAttr:$fused_activation_function + ); + + let results = (outs TFL_TensorOf<[F32, QI8, QUI8, QI16]>:$output); + + let hasOptions = 1; + + let hasVerifier = 1; + + let extraClassDeclaration = [{ + // AffineQuantizedOpInterface: + int GetChannelDimIndex() { return 0; } + int GetQuantizationDimIndex() { return 0; } + // SparseOpInterface: + std::vector GetSparseOperands() { return {1}; } + std::vector> GetFloatBlockSize() { return {}; } + std::vector> GetQuantizedBlockSize() { return {}; } + // DynamicRangeQuantizedOpInterface: + std::vector GetQuantizableOperandIndices() { return {1}; } + // RequiresQuantizedBiasInterface + int GetBiasOperandIndex() { return 3; } + std::vector GetNonBiasOperandIndices() { return {1, 2}; } + }]; +} + +def TFL_AveragePool2DOp: + TFL_Op<"average_pool_2d", + [Pure, + SameOperandsAndResultsScale, + QuantizableResult, + DeclareOpInterfaceMethods]> { + let summary = "Average_pool_2d operator"; + + let description = [{ + Performs average-pooling operation on input. + }]; + + let arguments = ( + ins TFL_TensorOf<[F32, QI8, QUI8, QI16]>:$input, + I32Attr:$filter_height, + I32Attr:$filter_width, + TFL_PaddingAttr:$padding, + I32Attr:$stride_h, + I32Attr:$stride_w, + TFL_AFAttr:$fused_activation_function + ); + + let results = (outs TFL_TensorOf<[F32, QI8, QUI8, QI16]>:$output); + + let hasOptions = 1; + let customOption = "Pool2DOptions"; +} + +def TFL_ArgMaxOp : TFL_Op<"arg_max", [ + QuantizableResult, + Pure]> { + let summary = "ArgMax operator"; + + let description = [{ + Returns the index with the largest value across dimensions of a tensor. + }]; + + let arguments = ( + ins TFL_TensorOf<[I1, F32, I32, I8, UI8, QI8, QUI8]>:$input, + TFL_I32OrI64Tensor:$dim + ); + + let results = (outs + TFL_I32OrI64Tensor:$output + ); + + let hasOptions = 1; + + DerivedTFLiteTypeAttr output_type = DerivedTFLiteTypeAttr<[{ + return llvm::cast(llvm::cast( + getResult().getType()).getElementType()).getWidth() > 32 ? + tflite::TensorType_INT64 : + tflite::TensorType_INT32; + }], [{ + TypeAttr::get(llvm::cast(getResult().getType()).getElementType()) + }]>; +} + +def TFL_ArgMinOp : TFL_Op<"arg_min", [ + QuantizableResult, + Pure]> { + let summary = "ArgMin operator"; + + let description = [{ + Returns the index with the smallest value across dimensions of a tensor. + a = [1, 10, 26.9, 2.8, 166.32, 62.3] + b = tf.math.argmin(input = a) + c = tf.keras.backend.eval(b) + }]; + + let arguments = ( + ins TFL_TensorOf<[I1, F32, I32, I8, UI8, QI8, QUI8]>:$input, + TFL_I32OrI64Tensor:$dim + ); + + let results = (outs + TFL_I32OrI64Tensor:$output + ); + + let hasOptions = 1; + + DerivedTFLiteTypeAttr output_type = DerivedTFLiteTypeAttr<[{ + return llvm::cast(llvm::cast( + getResult().getType()).getElementType()).getWidth() > 32 ? + tflite::TensorType_INT64 : + tflite::TensorType_INT32; + }], [{ + TypeAttr::get(llvm::cast(getResult().getType()).getElementType()) + }]>; +} + +def TFL_CeilOp: TFL_Op<"ceil", [ + Pure, + TF_SameOperandsAndResultTypeResolveRef]> { + let summary = "Ceil operator"; + + let description = [{ + Returns element-wise ceil value of the input. + }]; + + let arguments = (ins TFL_FpTensor:$x); + + let results = (outs TFL_FpTensor:$y); + + let hasFolder = 1; + + let extraClassDeclaration = [{ + // Returns whether the return types are compatible. + static bool isCompatibleReturnTypes(TypeRange l, TypeRange r) { + return TF::ArraysAreCastCompatible(l, r); + } + }]; +} + +def TFL_ConcatenationOp : TFL_Op<"concatenation", + [ + Pure, + TFL_SameFirstOperandAndFirstResultElementType, + SameOperandsAndResultsScale, + QuantizableResult + ]> { + let summary = "Concatenation operator"; + + let description = [{ + Concatenates tensors along one dimension + }]; + + let arguments = ( + ins TFL_VariadicTensorOf< + [F32, F16, I64, I32, I16, QI4, I8, QI8, QUI8, UI8, UI32, I1]>:$values, + I32Attr:$axis, + TFL_AFAttr:$fused_activation_function + ); + + let results = (outs + TFL_TensorOf< + [F32, F16, I64, I32, I16, QI4, I8, QI8, QUI8, UI8, UI32, I1]>:$output + ); + + let hasOptions = 1; + + let hasFolder = 1; + + let hasVerifier = 1; + + let extraClassDeclaration = [{ + // SameScalesOpInterface: + bool RequiredSameOperandsAndResultsScale(bool sign, int bit_width) { + // uint8 doesn't require same operands and results scales. + bool is_uint8 = !sign && (bit_width == 8); + return !is_uint8; + } + }]; +} + +def TFL_ConstOp : Op]> { + let summary = "Constant pseudo op."; + + let description = [{ + Represents a constant value in TensorFlow Lite dialect. This is not an + actual operation and it will be lowered to buffer instead. + + The op is allowed to have all the same type of attributes as tf.Const does + (e.g., opaque TF attributes are allowed). + }]; + + let arguments = (ins ElementsAttr:$value); + + let results = (outs AnyTensor:$output); + + let hasFolder = 1; + let hasCanonicalizer = 1; + + let builders = [ + OpBuilder<(ins "TypedAttr":$value), + [{ + $_state.addAttribute("value", value); + $_state.addTypes(value.getType()); + }]> + ]; + + let extraClassDeclaration = [{ + static bool isCompatibleReturnTypes(TypeRange l, TypeRange r); + }]; +} + +def TFL_SparseConstOp : Op]> { + let summary = "Sparse constant pseudo op."; + + let description = [{ + Represents a sparse constant value in TensorFlow Lite dialect. This is not + an actual operation and it will be lowered to buffer instead. + }]; + + let arguments = (ins ElementsAttr:$value, + SparsityParameterAttr:$s_param, + ElementsAttr:$compressed_data); + + let results = (outs AnyTensor:$output); + + let builders = [ + OpBuilder<(ins "TypedAttr":$value, "SparsityParameterAttr":$s_param, + "Attribute":$compressed_data), + [{ + $_state.addTypes(value.getType()); + $_state.addAttribute("value", value); + $_state.addAttribute("s_param", s_param); + $_state.addAttribute("compressed_data", compressed_data); + }]> + ]; +} + +def TFL_ExternalConstOp : Op]> { + let summary = "External const op."; + + let description = [{ + External const op that can hold : + - `buffer_index` which points to a constant in the flatbuffer. + - `external_buffer` which contains metadata for external buffer outside flatbuffer. + }]; + + let arguments = (ins + OptionalAttr:$buffer_index, + OptionalAttr:$external_buffer + ); + + let results = (outs AnyTensor:$output); +} + +def TFL_Conv2DOp : TFL_ConvOp<"conv_2d", "Convolution", 0, + [DeclareOpInterfaceMethods, + DeclareOpInterfaceMethods, + DeclareOpInterfaceMethods, + DynamicRangeQuantizedOpInterface, + RequiresQuantizedBiasInterface]> { + let arguments = ( + ins TFL_TensorOf<[F32, QI8, QUI8, QI16]>:$input, + TFL_TensorOf<[F32, QI4, QI8, QUI8]>:$filter, + TFL_1DTensorOfOrNone<[F32, I32, I64]>:$bias, + I32Attr:$dilation_h_factor, + I32Attr:$dilation_w_factor, + TFL_AFAttr:$fused_activation_function, + TFL_PaddingAttr:$padding, + I32Attr:$stride_h, + I32Attr:$stride_w + ); + + let hasCanonicalizer = 1; + + let extraClassDeclaration = [{ + // AffineQuantizedOpInterface: + int GetChannelDimIndex() { return 0; } + int GetQuantizationDimIndex() { return 0; } + // SparseOpInterface: + std::vector GetSparseOperands() { return {1}; } + std::vector> GetFloatBlockSize() { return {}; } + std::vector> GetQuantizedBlockSize() { return {}; } + // RequiresQuantizedBiasInterface + int GetBiasOperandIndex() { return 2; } + std::vector GetNonBiasOperandIndices() { return {0, 1}; } + + + // Returns whether the return types are compatible. + static bool isCompatibleReturnTypes(TypeRange l, TypeRange r); + + // DynamicRangeQuantizedOpInterface: + bool GetDynamicRangeQuantKernelSupport() { return true; } + std::vector GetQuantizableOperandIndices() { return {1}; } + }]; +} + +def TFL_CosOp: TFL_Op<"cos", [ + Pure, + TF_SameOperandsAndResultTypeResolveRef]> { + let summary = "Cosine operator"; + + let description = [{ + Computes element-wise Cosine of input + }]; + + let arguments = (ins TFL_TensorOf<[F32, F16]>:$x); + + let results = (outs TFL_TensorOf<[F32, F16]>:$y); + + let hasFolder = 1; + + let extraClassDeclaration = [{ + // Returns whether the return types are compatible. + static bool isCompatibleReturnTypes(TypeRange l, TypeRange r) { + return TF::ArraysAreCastCompatible(l, r); + } + + }]; +} + +def TFL_CumsumOp: TFL_Op<"cumsum", [ + Pure, + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + TFL_OperandHasRank<1, 0>]> { + let summary = "Cumsum operator"; + + let description = [{ + Compute the cumulative sum of the tensor x along axis. + }]; + + let arguments = ( + ins TFL_TensorOf<[F32, I32, I64]>:$input, + TFL_I32Tensor:$axis, + DefaultValuedOptionalAttr:$exclusive, + DefaultValuedOptionalAttr:$reverse + ); + + let results = (outs TFL_TensorOf<[F32, I32, I64]>:$output); + + let hasOptions = 1; + let hasFolder = 1; +} + +def TFL_DepthwiseConv2DOp : + TFL_ConvOp<"depthwise_conv_2d", "Depthwise-separable convolution", 3, + [DeclareOpInterfaceMethods, + DynamicRangeQuantizedOpInterface, + RequiresQuantizedBiasInterface]> { + let arguments = ( + ins TFL_TensorOf<[F32, QI8, QUI8, QI16]>:$input, + TFL_TensorOf<[F32, QI4, QI8, QUI8]>:$filter, + TFL_1DTensorOfOrNone<[F32, I32, I64]>:$bias, + I32Attr:$dilation_h_factor, + I32Attr:$dilation_w_factor, + TFL_AFAttr:$fused_activation_function, + TFL_PaddingAttr:$padding, + I32Attr:$stride_h, + I32Attr:$stride_w, + I32Attr:$depth_multiplier + ); + + let hasCanonicalizer = 1; + + let extraClassDeclaration = [{ + // AffineQuantizedOpInterface: + int GetChannelDimIndex() { return 3; } + int GetQuantizationDimIndex() { return 3; } + // SparseOpInterface: + std::vector GetSparseOperands() { return {1}; } + std::vector> GetFloatBlockSize() { return {}; } + std::vector> GetQuantizedBlockSize() { return {}; } + // DynamicRangeQuantizedOpInterface: + bool GetDynamicRangeQuantKernelSupport() { return true; } + std::vector GetQuantizableOperandIndices() { return {1}; } + // RequiresQuantizedBiasInterface + int GetBiasOperandIndex() { return 2; } + std::vector GetNonBiasOperandIndices() { return {0, 1}; } + }]; +} + +// TODO(jpienaar): Update post discussion on semantics of FC OP. +def TFL_FullyConnectedOp : TFL_Op<"fully_connected", [ + Pure, AccumulatorUniformScale<2, 0, 1>, + AffineQuantizedOpInterface, + AffineOpCoefficient<0, 1>, + TFL_SparseOp, + DeclareOpInterfaceMethods, + QuantizableResult, + DynamicRangeQuantizedOpInterface, + RequiresQuantizedBiasInterface]> { + let summary = "Fully connected op"; + + let arguments = (ins + TFL_TensorOf<[F32, QI8, QUI8, QI16, QUI16]>:$input, + TFL_TensorOf<[F32, QI2, QI4, QI8, QUI8, QI16]>:$filter, + TFL_TensorOfOrNone<[F32, QI32, QUI32]>:$bias, + + TFL_AFAttr:$fused_activation_function, + TFL_FullyConnectedOptionsWeightFormatAttr:$weights_format, + BoolAttr:$keep_num_dims, + // Used in post-training dynamic range quantization. If the value is true, + // input activations are asymmetrically quantized. + OptionalAttr:$asymmetric_quantize_inputs + ); + + // Depending on the weights format, this op can have one or two outputs. + let results = (outs + TFL_VariadicTensorOf<[F32, QI8, QUI8, QI16, QUI16]>:$output + ); + + let hasVerifier = 1; + + let hasOptions = 1; + + let hasCanonicalizer = 1; + + let hasFolder = 1; + + let extraClassDeclaration = [{ + // AffineQuantizedOpInterface: + int GetChannelDimIndex() { return 0; } + int GetQuantizationDimIndex() { return 0; } + // SparseOpInterface: + std::vector GetSparseOperands() { return {1}; } + std::vector> GetFloatBlockSize() { return {{1, 4}}; } + std::vector> GetQuantizedBlockSize() { return {{1, 16}}; } + // DynamicRangeQuantizedOpInterface: + bool RequireAsymmetricQuantizeInputsAttr() { return true; } + bool GetDynamicRangeQuantKernelSupport() { return true; } + std::vector GetQuantizableOperandIndices() { return {1}; } + // RequiresQuantizedBiasInterface + int GetBiasOperandIndex() { return 2; } + std::vector GetNonBiasOperandIndices() { return {0, 1}; } + }]; +} + +def TFL_BatchMatMulOp : TFL_Op<"batch_matmul", [ + Pure, + TFL_OperandHasAtleastRank<0, 2>, + TFL_OperandHasAtleastRank<1, 2>, + QuantizableResult, + TFL_RuntimePredOpTrait<"x and output must have same element type or they are int8 and int32", + Or<[TFL_TCresVTEtIsSameAsOp<0, 0>, + And<[CPred<"getElementTypeOrSelf($_op.getOperand(0)).isInteger(8)">, + CPred<"getElementTypeOrSelf($_op.getOperand(1)).isInteger(8)">, + CPred<"getElementTypeOrSelf($_op.getResult(0)).isInteger(32)">]>]>>, + TFL_RuntimePredOpTrait<"lhs and rhs of this op must have rank between [2, 5]", + And<[TFL_OperandHasRankAtMostPred<0, 5>, + TFL_OperandHasRankAtMostPred<1, 5>]>>, + DynamicRangeQuantizedOpInterface]> { + + let summary = "Batch Matrix Multiply Operator"; + + let description = [{ +Performs a batched matrix multiplication on the inputs. Follows the +conventions of TensorFlow BatchMatMulV2, with support for unknown dimensions +in the batch dimensions and broadcasting. + + Inputs: + `inputs[0]`: required: input LHS + `inputs[1]`: required: input RHS + `adjoint_lhs`: optional: Transpose LHS (default false) + `adjoint_rhs`: optional: Transpose RHS (default false) + }]; + + let arguments = (ins + TFL_TensorOf<[F32, QI8, QI16, I8]>:$x, + TFL_TensorOf<[F32, QI8, QI16, I8]>:$y, + DefaultValuedOptionalAttr:$adj_x, + DefaultValuedOptionalAttr:$adj_y, + // Used in post-training dynamic range quantization. If the value is true, + // input activations are asymmetrically quantized. + OptionalAttr:$asymmetric_quantize_inputs + ); + + let results = (outs + TFL_TensorOf<[F32, QI8, QI16, I32]>:$output + ); + + let hasOptions = 1; + + let hasVerifier = 1; + + let extraClassDeclaration = [{ + // DynamicRangeQuantizedOpInterface: + bool RequireAsymmetricQuantizeInputsAttr() { return true; } + bool GetDynamicRangeQuantKernelSupport() { return true; } + std::vector GetQuantizableOperandIndices() { return {1}; } + }]; +} + +def TFL_GatherOp : TFL_Op<"gather", [ + Pure, + QuantizableResult, + SameOperandsAndResultsScale, + TFL_OperandHasAtleastRank<0, 1>, + PredOpTrait<"params and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + DynamicRangeQuantizedOpInterface + ]> { + let summary = "Gather operator"; + + let description = [{ + Gather slices from `params` axis `axis` according to `indices`. + }]; + + let arguments = (ins + TFL_TensorOf<[F32, I1, TFL_I4, I8, I16, I32, I64, TFL_Str, UI8, QI8, QUI8, QI16]>:$params, + TFL_TensorOf<[I16, I32, I64]>:$indices, + I32Attr:$axis, + DefaultValuedOptionalAttr:$batch_dims + ); + + let builders = + [ + OpBuilder<(ins "Value":$params, "Value":$indices, "IntegerAttr":$axis, "IntegerAttr":$batch_dims), + [{ BuildGatherOp(&$_builder, $_state, params, indices, axis, batch_dims); }]> + ]; + + let results = (outs + TFL_TensorOf<[F32, I1, TFL_I4, I8, I16, I32, I64, TFL_Str, UI8, QI8, QUI8, QI16]>:$output + ); + + let hasOptions = 1; + + let hasVerifier = 1; + + let hasFolder = 1; + + let extraClassDeclaration = [{ + // DynamicRangeQuantizedOpInterface: + std::vector GetQuantizableOperandIndices() { return {0}; } + }]; +} + +def TFL_GatherNdOp : TFL_Op<"gather_nd", [ + Pure, + QuantizableResult, + SameOperandsAndResultsScale, + PredOpTrait<"params and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>]> { + let summary = "Gather_nd operator"; + + let description = [{ + Gather slices from `params` into a Tensor with shape specified by `indices`. + }]; + + let arguments = (ins + TFL_TensorOf<[F32, I1, I8, I16, I64, I32, UI8, QI8, TFL_Str]>:$params, + TFL_TensorOf<[I16, I32, I64]>:$indices + ); + + let results = (outs + TFL_TensorOf<[F32, I1, I8, I16, I64, I32, UI8, QI8, TFL_Str]>:$output + ); + + let hasFolder = 1; +} + +def TFL_ScatterNdOp : TFL_Op<"scatter_nd", [ + Pure, + QuantizableResult, + SameOperandsAndResultsScale, + TFL_OperandHasAtleastRank<0, 1>, + TFL_OperandHasAtleastRank<1, 1>, + PredOpTrait<"updates and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 1>> + ]> { + let summary = "Scatter_nd operator"; + + let description = [{ + Scatter `updates` into a new tensor according to `indices` + }]; + + let arguments = (ins + TFL_TensorOf<[I32]>:$indices, + TFL_TensorOf<[F32, I8, I64, I32, UI8, I1]>:$updates, + TFL_1DTensorOf<[I32]>:$shape + ); + + let results = (outs + TFL_TensorOf<[F32, I8, I64, I32, UI8, I1]>:$output + ); + + let hasVerifier = 1; + + let hasOptions = 1; +} + +// Same type check of lhs and rhs is handled by the ResultsBroadcastableShape trait. +def TFL_LessEqualOp : TFL_Op<"less_equal", [ + ResultsBroadcastableShape, + QuantizableResult, + ComparisonOpSameElementTypeConstraint, + TFL_OperandsHaveSameShapesOrBroadcastableShape<[0, 1], 4>, + Pure]> { + let summary = "Less_equal operator"; + + let description = [{ + Element-wise less_equal operation. + }]; + + let arguments = ( + ins TFL_TensorOf<[F32, I32, I64, QI8, QUI8]>:$lhs, + TFL_TensorOf<[F32, I32, I64, QI8, QUI8]>:$rhs); + + let results = (outs TFL_BoolTensor:$output); + + let builders = [TFL_ComparisonBinaryBuilder]; + + let hasCustomAssemblyFormat = 1; + + let hasFolder = 1; + + let extraClassDefinition = [{ + ParseResult $cppClass::parse(OpAsmParser &parser, OperationState &result) { + return parseOneResultSameOperandTypeOp(parser, result); + } + void $cppClass::print(OpAsmPrinter &p) { + return printOneResultOp(getOperation(), p); + } + }]; + + let hasOptions = 0; +} + +def TFL_LocalResponseNormalizationOp : TFL_Op<"local_response_normalization", [ + TFL_OperandHasRank<0, 4>, + TF_SameOperandsAndResultTypeResolveRef, + Pure]> { + let summary = "Local Response Normalization."; + + let description = [{ +The 4-D `input` tensor is treated as a 3-D array of 1-D vectors (along the last +dimension), and each vector is normalized independently. Within a given vector, +each component is divided by the weighted, squared sum of inputs within +`depth_radius`. In detail, + + sqr_sum[a, b, c, d] = + sum(input[a, b, c, d - depth_radius : d + depth_radius + 1] ** 2) + output = input / (bias + alpha * sqr_sum) ** beta + +For details, see [Krizhevsky et al., ImageNet classification with deep +convolutional neural networks (NIPS 2012)](http://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks). + }]; + + let arguments = (ins + TFL_FpTensor:$input, + I32Attr:$radius, + F32Attr:$bias, + F32Attr:$alpha, + F32Attr:$beta + ); + + let results = (outs + TFL_FpTensor:$output + ); + + let hasOptions = 1; + + let extraClassDeclaration = [{ + // Returns whether the return types are compatible. + static bool isCompatibleReturnTypes(TypeRange l, TypeRange r) { + return TF::ArraysAreCastCompatible(l, r); + } + }]; +} + +def TFL_GreaterEqualOp : TFL_Op<"greater_equal", [ + QuantizableResult, + TFL_OperandsHaveSameShapesOrBroadcastableShape<[0, 1], 4>, + ResultsBroadcastableShape, + ComparisonOpSameElementTypeConstraint, + Pure]> { + let summary = "Greater_equal operator"; + + let description = [{ + Element-wise greater_equal operation. + }]; + + let arguments = ( + ins TFL_TensorOf<[F32, I16, I32, I64, QUI8, QI8]>:$lhs, + TFL_TensorOf<[F32, I16, I32, I64, QUI8, QI8]>:$rhs); + + let results = (outs TFL_BoolTensor:$output); + + let builders = [TFL_ComparisonBinaryBuilder]; + + let hasCustomAssemblyFormat = 1; + + let hasFolder = 1; + + let extraClassDefinition = [{ + ParseResult $cppClass::parse(OpAsmParser &parser, OperationState &result) { + return parseOneResultSameOperandTypeOp(parser, result); + } + void $cppClass::print(OpAsmPrinter &p) { + return printOneResultOp(getOperation(), p); + } + }]; + + let hasOptions = 0; +} + +def TFL_MatrixDiagOp : TFL_Op<"matrix_diag", [ + QuantizableResult, + Pure, + TFL_OperandHasAtleastRank<0, 1>, + PredOpTrait<"operand and result must have the same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>]> { + let summary = [{ + Returns a tensor with the provided diagonal and everything else padded with zeros. + }]; + + let description = [{ + Given a diagonal, returns a tensor with the diagonal and everything else padded with zeros. + Assume diagonal has k dimensions `[I, J, K, ..., N]`, then the output is a tensor of rank `k+1` + with dimensions `[I, J, K, ..., N, N]` where: + `output[i, j, k, ..., m, n] = 1{m=n} * diagonal[i, j, k, ..., n].` + }]; + + let arguments = (ins + TFL_TensorOf<[F32, I8, I16, I32, I64, UI8, QUI8, QI8, TFL_Quint8]>:$diagonal + ); + + let results = (outs + TFL_TensorOf<[F32, I8, I16, I32, I64, UI8, QUI8, QI8, TFL_Quint8]>:$output + ); + + let hasOptions = 0; +} + +def TFL_MatrixSetDiagOp : TFL_Op<"matrix_set_diag", [ + QuantizableResult, + TFL_OperandHasAtleastRank<0, 2>, + PredOpTrait<"input and result must have the same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + Pure]> { + let summary = [{ + Returns a batched matrix tensor with new batched diagonal values. + }]; + + let description = [{ +Given `input` and `diagonal`, this operation returns a tensor with the +same shape and values as `input`, except for the main diagonal of the +innermost matrices. These will be overwritten by the values in `diagonal`. + }]; + + let arguments = (ins + TFL_TensorOf<[F32, I8, I16, I32, I64, UI8, QI8, QI16, QUI8, TFL_Quint8]>:$input, + TFL_TensorOf<[F32, I8, I16, I32, I64, UI8, QI8, QI16, QUI8, TFL_Quint8]>:$diagonal + ); + + let results = (outs + TFL_TensorOf<[F32, I8, I16, I32, I64, UI8, QI8, QI16, QUI8, TFL_Quint8]>:$result + ); + + let hasOptions = 0; +} + +// These ops are named NonMaxSuppressionV4 & NonMaxSuppressionV5 to be +// consistent with TensorFlow's naming. They are NOT 'versions' of NMS in the +// sense that one is an incremental change over the other. +// In reality NonMaxSuppressionV5 implements Soft Non Max Suppression and +// NonMaxSuppressionV4 performs hard NMS. + +def TFL_NonMaxSuppressionV4Op : TFL_Op<"non_max_suppression_v4", [ + Pure, + // Operand 0 (boxes) should have rank 2 with the dim[1] == 4 (box corners) + TFL_OperandHasRank<0, 2>, + PredOpTrait<"boxes should have dim[1] == 4", + TFL_OperandDimEquals<0, 1, 4>>, + // Operand 1 (scores) should be a 1-dim tensor + TFL_OperandHasRank<1, 1>, + // Other operands are scalar params. + TFL_OperandHasRank<2, 0>, TFL_OperandHasRank<3, 0>, + TFL_OperandHasRank<4, 0>]> { + let summary = [{ +Greedily selects a subset of bounding boxes in descending order of score, + }]; + + let description = [{ +pruning away boxes that have high intersection-over-union (IOU) overlap +with previously selected boxes. Bounding boxes with score less than +`score_threshold` are removed. Bounding boxes are supplied as +[y1, x1, y2, x2], where (y1, x1) and (y2, x2) are the coordinates of any +diagonal pair of box corners and the coordinates can be provided as normalized +(i.e., lying in the interval [0, 1]) or absolute. Note that this algorithm +is agnostic to where the origin is in the coordinate system and more +generally is invariant to orthogonal transformations and translations +of the coordinate system; thus translating or reflections of the coordinate +system result in the same boxes being selected by the algorithm. +The output of this operation is a set of integers indexing into the input +collection of bounding boxes representing the selected boxes. The bounding +box coordinates corresponding to the selected indices can then be obtained +using the `tf.gather operation`. For example: + selected_indices = tf.image.non_max_suppression_v2( + boxes, scores, max_output_size, iou_threshold, score_threshold) + selected_boxes = tf.gather(boxes, selected_indices) + }]; + + let arguments = (ins + TFL_FpTensor:$boxes, + TFL_FpTensor:$scores, + TFL_I32Tensor:$max_output_size, + TFL_FpTensor:$iou_threshold, + TFL_FpTensor:$score_threshold + ); + + let results = (outs + TFL_I32Tensor:$selected_indices, + TFL_I32Tensor:$valid_outputs + ); +} + +def TFL_NonMaxSuppressionV5Op : TFL_Op<"non_max_suppression_v5", [ + Pure, + // Operand 0 (boxes) should have rank 2 with the dim[1] == 4 (box corners) + TFL_OperandHasRank<0, 2>, + PredOpTrait<"boxes should have dim[1] == 4", + TFL_OperandDimEquals<0, 1, 4>>, + // Operand 1 (scores) should be a 1-dim tensor + TFL_OperandHasRank<1, 1>, + // Other operands are scalar params. + TFL_OperandHasRank<2, 0>, TFL_OperandHasRank<3, 0>, + TFL_OperandHasRank<4, 0>, TFL_OperandHasRank<5, 0>]> { + let summary = [{ +Greedily selects a subset of bounding boxes in descending order of score, + }]; + + let description = [{ +pruning away boxes that have high intersection-over-union (IOU) overlap +with previously selected boxes. Bounding boxes with score less than +`score_threshold` are removed. Bounding boxes are supplied as +[y1, x1, y2, x2], where (y1, x1) and (y2, x2) are the coordinates of any +diagonal pair of box corners and the coordinates can be provided as normalized +(i.e., lying in the interval [0, 1]) or absolute. Note that this algorithm +is agnostic to where the origin is in the coordinate system and more +generally is invariant to orthogonal transformations and translations +of the coordinate system; thus translating or reflections of the coordinate +system result in the same boxes being selected by the algorithm. +The output of this operation is a set of integers indexing into the input +collection of bounding boxes representing the selected boxes. The bounding +box coordinates corresponding to the selected indices can then be obtained +using the `tf.gather operation`. For example: + selected_indices = tf.image.non_max_suppression_v2( + boxes, scores, max_output_size, iou_threshold, score_threshold) + selected_boxes = tf.gather(boxes, selected_indices) +This op also supports a Soft-NMS (with Gaussian weighting) mode (c.f. +Bodla et al, https://arxiv.org/abs/1704.04503) where boxes reduce the score +of other overlapping boxes instead of directly causing them to be pruned. +To enable this Soft-NMS mode, set the `soft_nms_sigma` parameter to be +larger than 0. + }]; + + let arguments = (ins + TFL_FpTensor:$boxes, + TFL_FpTensor:$scores, + TFL_I32Tensor:$max_output_size, + TFL_FpTensor:$iou_threshold, + TFL_FpTensor:$score_threshold, + TFL_FpTensor:$soft_nms_sigma + ); + + let results = (outs + TFL_I32Tensor:$selected_indices, + TFL_FpTensor:$selected_scores, + TFL_I32Tensor:$valid_outputs + ); +} + +def TFL_NotEqualOp : TFL_Op<"not_equal", [ + TFL_OperandsHaveSameShapesOrBroadcastableShape<[0, 1], 4>, + ComparisonOpSameElementTypeConstraint, + ResultsBroadcastableShape, + Commutative, + Pure]> { + let summary = "Not_equal operator"; + + let description = [{ + Element-wise not_equal operation. + }]; + + let arguments = ( + ins TFL_TensorOf<[I1, F32, I32, I64, QUI8, QI8, QI16, TFL_Quint8, TFL_Str]>:$lhs, + TFL_TensorOf<[I1, F32, I32, I64, QUI8, QI8, QI16, TFL_Quint8, TFL_Str]>:$rhs); + + let results = (outs TFL_BoolTensor:$output); + + let hasFolder = 1; + + let builders = + [ + OpBuilder<(ins "Value":$lhs, "Value":$rhs), + [{ + buildComparisonBinOp(&$_builder, $_state, lhs, rhs); + }]> + ]; + + let hasCustomAssemblyFormat = 1; + + let extraClassDefinition = [{ + ParseResult $cppClass::parse(OpAsmParser &parser, OperationState &result) { + return parseOneResultSameOperandTypeOp(parser, result); + } + void $cppClass::print(OpAsmPrinter &p) { + return printOneResultOp(getOperation(), p); + } + }]; +} + +def TFL_DivOp : TFL_Op<"div", [ + // TODO(fengliuai): NoQuantizableResult is only correct for int8 + // quantization. update to handle Uint8 quantization. + BinaryOpSameElementTypeConstraint, + TFL_OperandsHaveSameShapesOrBroadcastableShape<[0, 1], 6>, + ResultsBroadcastableShape, + Pure, + QuantizableResult, + DeclareOpInterfaceMethods]> { + let summary = "Division operator"; + + let description = [{ + Element-wise division operation. + }]; + + let arguments = ( + ins TFL_TensorOf<[F32, I32, QUI8, QI8, QI16]>:$lhs, + TFL_TensorOf<[F32, I32, QUI8, QI8, QI16]>:$rhs, + TFL_AFAttr:$fused_activation_function); + + let results = (outs TFL_TensorOf<[F32, I32, QUI8, QI8, QI16]>:$output); + + let builders = [TFL_FusedBroadcastableBinaryBuilder]; + + let hasCustomAssemblyFormat = 1; + + let extraClassDefinition = [{ + ParseResult $cppClass::parse(OpAsmParser &parser, OperationState &result) { + return parseOneResultSameOperandTypeOp(parser, result); + } + void $cppClass::print(OpAsmPrinter &p) { + return printOneResultOp(getOperation(), p); + } + }]; + + let hasOptions = 1; + + let hasFolder = 1; +} + +def TFL_EluOp: TFL_Op<"elu", [ + Pure, + SameOperandsAndResultShape, + TFL_SameFirstOperandAndFirstResultElementType]> { + let summary = "Exponential Linear Unit operator"; + let description = [{ + Computes the exponential linear + f(x) -> exp(x) - 1 for x < 0, x for x >= 0. + element-wise. + }]; + + let arguments = (ins TFL_TensorOf<[F32, I8]>:$x); + + let results = (outs TFL_TensorOf<[F32, I8]>:$y); + + let hasOptions = 0; + + let builders = [ + OpBuilder<(ins "Value":$input), + [{ + $_state.addOperands({input}); + $_state.addTypes(input.getType()); + }]> + ]; +} + +def TFL_EmbeddingLookupOp: TFL_Op<"embedding_lookup", + [Pure, + TFL_OperandHasRank<0, 1>, + TFL_OperandHasRankAtLeast<1, 2>, + AffineQuantizedOpInterface, + DynamicRangeQuantizedOpInterface, + QuantizableResult + ]> { + let summary = "Embedding lookup operator"; + + let description = [{ + Looks up ids in a list of embedding tensors. + }]; + + let arguments = (ins + TFL_TensorOf<[I32]>:$lookup, + TFL_TensorOf<[F32, I8, UI8, QI8, QUI8, QI4, QI2]>:$value + ); + + let results = (outs TFL_TensorOf<[F32, I8, UI8]>:$output); + + let hasFolder = 1; + + let extraClassDeclaration = [{ + // AffineQuantizedOpInterface: + int GetChannelDimIndex() { return 0; } + int GetQuantizationDimIndex() { return 0; } + // DynamicRangeQuantizedOpInterface: + std::vector GetQuantizableOperandIndices() { return {1}; } + }]; +} + +def TFL_EqualOp: TFL_Op<"equal", [ + Commutative, + Pure, + ResultsBroadcastableShape, + TFL_OperandsHaveSameShapesOrBroadcastableShape<[0, 1], 4>, + QuantizableResult, + ComparisonOpSameElementTypeConstraint]> { + let summary = "Equal operator"; + + let description = [{ + Returns the truth element of x == y element-wise + }]; + + let arguments = ( + ins + TFL_TensorOf<[I1, F32, I16, I32, I64, QI8, QUI8, QI16, UI8, TFL_Str]>:$x, + TFL_TensorOf<[I1, F32, I16, I32, I64, QI8, QUI8, QI16, UI8, TFL_Str]>:$y + ); + + let results = (outs TFL_BoolTensor:$output); + + let builders = [TFL_ComparisonBinaryBuilder]; + + let hasFolder = 1; +} + +def TFL_ExpOp: TFL_Op<"exp", [ + Pure, + QuantizableResult, + TFL_SameFirstOperandAndFirstResultElementType]> { + let summary = "Natural exponentiation operator"; + + let description = [{ + Performs element-wise natural exponentiation operation on input. + }]; + + let arguments = (ins TFL_TensorOf<[F32, QI8, QI16]>:$x); + + let results = (outs TFL_TensorOf<[F32, QI8, QI16]>:$y); + + let hasFolder = 1; + + // This builder doesn't work with quantized type, so it can only be used by + // non-quantization tablegen patterns. Currently, it is used by the + // elementwise-move reordering pattern in the optimize_patterns.td + let builders = [ + OpBuilder<(ins "Value":$input), + [{ + $_state.addOperands({input}); + $_state.addTypes(input.getType()); + }]> + ]; + + let hasOptions = 0b1; + + let extraClassDeclaration = [{ + // Returns whether the return types are compatible. + static bool isCompatibleReturnTypes(TypeRange l, TypeRange r) { + return TF::ArraysAreCastCompatible(l, r); + } + }]; +} + +def TFL_ExpandDimsOp: TFL_Op<"expand_dims", [ + Pure, + SameOperandsAndResultsScale, + QuantizableResult, + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>]> { + let summary = "Inserts a dimension of 1 into a tensor's shape."; + + let description = [{ +Given a tensor `input`, this operation inserts a dimension of 1 at the +dimension index `axis` of `input`'s shape. The dimension index `axis` starts at +zero; if you specify a negative number for `axis` it is counted backward from +the end. + +This operation is useful if you want to add a batch dimension to a single +element. For example, if you have a single image of shape `[height, width, +channels]`, you can make it a batch of 1 image with `expand_dims(image, 0)`, +which will make the shape `[1, height, width, channels]`. + +Other examples: + +``` +# 't' is a tensor of shape [2] +shape(expand_dims(t, 0)) ==> [1, 2] +shape(expand_dims(t, 1)) ==> [2, 1] +shape(expand_dims(t, -1)) ==> [2, 1] + +# 't2' is a tensor of shape [2, 3, 5] +shape(expand_dims(t2, 0)) ==> [1, 2, 3, 5] +shape(expand_dims(t2, 2)) ==> [2, 3, 1, 5] +shape(expand_dims(t2, 3)) ==> [2, 3, 5, 1] +``` + +This operation requires that: + +`-1-input.dims() <= dim <= input.dims()` + +This operation is related to `squeeze()`, which removes dimensions of +size 1. + }]; + + // TODO: Restriction on dim's size and valid range are not modeled here. + let arguments = (ins AnyTensor:$input, TFL_I32OrI64Tensor:$dim); + + let results = (outs AnyTensor:$output); + + let hasOptions = 1; +} + +def TFL_SqueezeOp: TFL_Op<"squeeze", [Pure, + QuantizableResult, + SameOperandsAndResultsScale]> { + let summary = "Removes dimensions of size 1 from the shape of a tensor."; + + let description = [{ +Given a tensor `input`, this operation returns a tensor of the same type with +all dimensions of size 1 removed. If you don't want to remove all size 1 +dimensions, you can remove specific size 1 dimensions by specifying +`squeeze_dims`. + +For example: + +``` +# 't' is a tensor of shape [1, 2, 1, 3, 1, 1] +shape(squeeze(t)) ==> [2, 3] +``` + +Or, to remove specific size 1 dimensions: + +``` +# 't' is a tensor of shape [1, 2, 1, 3, 1, 1] +shape(squeeze(t, [2, 4])) ==> [1, 2, 3, 1] +``` + }]; + + let arguments = (ins + AnyTensor:$input, + ConfinedAttr, [TFL_ArrayMaxCount<8>]>:$squeeze_dims + ); + + let results = (outs + AnyTensor:$output + ); + + let hasFolder = 1; + let hasOptions = 1; + + let customOption = "SqueezeOptions"; +} + +def TFL_FillOp: TFL_Op<"fill", [ + Pure, + SameOperandsAndResultsScale, + QuantizableResult, + PredOpTrait<"input and result must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 1>>]> { + let summary = "Fill the tensor with given value."; + let description = [{ + Fill the tensor with given value. + }]; + + let arguments = (ins TFL_I32OrI64Tensor:$dims, + TFL_TensorOf<[F32, F16, I32, I64, I1, QI8, QI16, TFL_Str]>:$input); + + let results = (outs TFL_TensorOf<[F32, F16, I32, I64, I1, QI8, QI16, TFL_Str]>:$result); + + let hasOptions = 0; +} + +def TFL_FloorOp: TFL_Op<"floor", [ + Pure, + TF_SameOperandsAndResultTypeResolveRef]> { + let summary = "Floor operator"; + + let description = [{ + Returns element-wise floor value of the input. + }]; + + let arguments = (ins TFL_FpTensor:$x); + + let results = (outs TFL_FpTensor:$y); + + let hasFolder = 1; + + let extraClassDeclaration = [{ + // Returns whether the return types are compatible. + static bool isCompatibleReturnTypes(TypeRange l, TypeRange r) { + return TF::ArraysAreCastCompatible(l, r); + } + }]; +} + +def TFL_FloorDivOp : TFL_Op<"floor_div", [ + ResultsBroadcastableShape, + Pure, + BinaryOpSameElementTypeConstraint, + PredOpTrait<"lhs and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + TFL_OperandsHaveSameShapesOrBroadcastableShape<[0, 1], 4>]> { + let summary = "Floor div operator"; + + let description = [{ + Element-wise floor div operation. + }]; + + let arguments = ( + ins TFL_TensorOf<[F32, I8, I16, I32]>:$lhs, + TFL_TensorOf<[F32, I8, I16, I32]>:$rhs); + + let results = (outs TFL_TensorOf<[F32, I8, I16, I32]>:$output); + + let builders = [TFL_BroadcastableBinaryBuilder]; + + let hasCustomAssemblyFormat = 1; + + let extraClassDefinition = [{ + ParseResult $cppClass::parse(OpAsmParser &parser, OperationState &result) { + return parseOneResultSameOperandTypeOp(parser, result); + } + void $cppClass::print(OpAsmPrinter &p) { + return printOneResultOp(getOperation(), p); + } + }]; +} + +def TFL_FloorModOp : TFL_Op<"floor_mod", [ + ResultsBroadcastableShape, + Pure, + BinaryOpSameElementTypeConstraint, + PredOpTrait<"lhs and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + TFL_OperandsHaveSameShapesOrBroadcastableShape<[0, 1], 4>]> { + let summary = "Division reminder"; + + let description = [{ + Element-wise division reminder operation. + }]; + + let arguments = ( + ins TFL_TensorOf<[I8, I16, I32, I64, F32]>:$lhs, + TFL_TensorOf<[I8, I16, I32, I64, F32]>:$rhs); + + let results = (outs TFL_TensorOf<[I8, I16, I32, I64, F32]>:$output); + + let builders = [TFL_BroadcastableBinaryBuilder]; +} + +def TFL_GreaterOp : TFL_Op<"greater", [ + ResultsBroadcastableShape, + ComparisonOpSameElementTypeConstraint, + TFL_OperandsHaveSameShapesOrBroadcastableShape<[0, 1], 4>, + QuantizableResult, + Pure]> { + let summary = "Greater operator"; + + let description = [{ + Element-wise greater operation. + }]; + + let arguments = ( + ins TFL_TensorOf<[F32, I32, I64, QUI8, QI8, TFL_Quint8]>:$lhs, + TFL_TensorOf<[F32, I32, I64, QUI8, QI8, TFL_Quint8]>:$rhs); + + let results = (outs TFL_BoolTensor:$output); + + let builders = [TFL_ComparisonBinaryBuilder]; + + let hasCustomAssemblyFormat = 1; + + let hasFolder = 1; + + let extraClassDefinition = [{ + ParseResult $cppClass::parse(OpAsmParser &parser, OperationState &result) { + return parseOneResultSameOperandTypeOp(parser, result); + } + void $cppClass::print(OpAsmPrinter &p) { + return printOneResultOp(getOperation(), p); + } + }]; +} + +def TFL_HardSwishOp: TFL_Op<"hard_swish", [ + Pure, + SameOperandsAndResultShape, + QuantizableResult, + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>]> { + let summary = "Hardswish activation function."; + let description = [{ + Computes hard-swish activation function + f(x) -> (x * relu6(x+3))/6 + element-wise. + }]; + + let arguments = (ins TFL_TensorOf<[F32, QUI8, QI8]>:$input); + + let results = (outs TFL_TensorOf<[F32, QUI8, QI8]>:$output); + + let hasOptions = 0; + + // This builder doesn't work with quantized type, so it can only be used by + // non-quantization tablegen patterns. + let builders = [ + OpBuilder<(ins "Value":$input), + [{ + $_state.addOperands({input}); + $_state.addTypes(input.getType()); + }]> + ]; +} + +def TFL_L2NormalizationOp : TFL_Op<"l2_normalization", [Pure, + FixedOutputRangeInterface, + QuantizableResult, + DeclareOpInterfaceMethods]> { + let summary = "L2 Normalize Operator"; + + let description = [{ + L2Normalization Op + }]; + + let arguments = (ins + TFL_TensorOf<[F32, QUI8, QI8, QUI16, QI16, I8]>:$input, + TFL_AFAttr:$fused_activation_function + ); + + let results = (outs TFL_TensorOf<[F32, QUI8, QI8, QUI16, QI16, I8]>:$output); + + let hasOptions = 1; + + let customOption = "L2NormOptions"; + + let extraClassDeclaration = [{ + // FixedOutputRangeInterface: + quant::UniformQuantizedType GetFixedOutputRange( + bool is_signed, int bit_width) { + auto result_type = getOutput().getType(); + // central_value = min_value / 2 + (max_value - 1) / 2 + 1 + // zero_point = central_value + // scale = 1. / (central_value - min_value) + return mlir::TFL::GetFixedOutputRange(is_signed, bit_width, result_type, + /*scale=*/1.0 / (1<<(bit_width-1)), /*zero_point=*/0); + } + }]; +} + +def TFL_LeakyReluOp: TFL_Op<"leaky_relu", [ + SameOperandsAndResultShape, + QuantizableResult, + Pure, + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>]> { + let summary = "Leaky Relu operator"; + + let description = [{ + Element-wise Leaky ReLU operator + x -> x >= 0 ? x : (alpha * x) + }]; + + let arguments = ( + ins TFL_TensorOf<[F32, QUI8, QI8, TFL_Quint8, QI16]>:$input, + // Slope of the activation function at x < 0. + F32Attr:$alpha + ); + + let results = (outs TFL_TensorOf<[F32, QUI8, QI8, TFL_Quint8, QI16]>:$output); + + let hasOptions = 0b1; +} + +def TFL_LessOp : TFL_Op<"less", [ + ResultsBroadcastableShape, + ComparisonOpSameElementTypeConstraint, + QuantizableResult, + TFL_OperandsHaveSameShapesOrBroadcastableShape<[0, 1], 4>, + Pure]> { + let summary = "Less operator"; + + let description = [{ + Element-wise less operation. + }]; + + let arguments = ( + ins TFL_TensorOf<[F32, I16, I32, I64, QUI8, QI8, TFL_Quint8]>:$lhs, + TFL_TensorOf<[F32, I16, I32, I64, QUI8, QI8, TFL_Quint8]>:$rhs); + + let results = (outs TFL_BoolTensor:$output); + + let builders = [TFL_ComparisonBinaryBuilder]; + + let hasCustomAssemblyFormat = 1; + + let hasFolder = 1; + + let extraClassDefinition = [{ + ParseResult $cppClass::parse(OpAsmParser &parser, OperationState &result) { + return parseOneResultSameOperandTypeOp(parser, result); + } + void $cppClass::print(OpAsmPrinter &p) { + return printOneResultOp(getOperation(), p); + } + }]; +} + +def TFL_LogicalAndOp : TFL_Op<"logical_and", [ResultsBroadcastableShape, Pure]> { + let summary = "Logical AND operator"; + + let description = [{ + Element-wise logical AND operation. + }]; + + let arguments = ( + ins TFL_BoolTensor:$lhs, + TFL_BoolTensor:$rhs); + + let results = (outs TFL_BoolTensor:$output); + + let hasCustomAssemblyFormat = 1; + + let hasFolder = 1; + + let extraClassDefinition = [{ + ParseResult $cppClass::parse(OpAsmParser &parser, OperationState &result) { + return parseOneResultSameOperandTypeOp(parser, result); + } + void $cppClass::print(OpAsmPrinter &p) { + return printOneResultOp(getOperation(), p); + } + }]; +} + +def TFL_LogicalNotOp : TFL_Op<"logical_not", [ + Pure, + SameOperandsAndResultType + ]> { + let summary = "Logical NOT operator"; + + let description = [{ + Element-wise logical NOT operation. + }]; + + let arguments = (ins TFL_BoolTensor:$lhs); + + let results = (outs TFL_BoolTensor:$output); + + let hasFolder = 1; +} + +def TFL_LogicalOrOp : TFL_Op<"logical_or", [ResultsBroadcastableShape, Pure]> { + let summary = "Logical OR operator"; + + let description = [{ + Element-wise logical OR operation. + }]; + + let arguments = ( + ins TFL_BoolTensor:$lhs, + TFL_BoolTensor:$rhs); + + let results = (outs TFL_BoolTensor:$output); + + let hasCustomAssemblyFormat = 1; + + let hasFolder = 1; + + let extraClassDefinition = [{ + ParseResult $cppClass::parse(OpAsmParser &parser, OperationState &result) { + return parseOneResultSameOperandTypeOp(parser, result); + } + void $cppClass::print(OpAsmPrinter &p) { + return printOneResultOp(getOperation(), p); + } + }]; +} + +def TFL_LogisticOp: TFL_Op<"logistic", [ + Pure, + PredOpTrait<"x and y must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + SameOperandsAndResultShape, + FixedOutputRangeInterface, + QuantizableResult, + DeclareOpInterfaceMethods]> { + let summary = "Logistic operator"; + + let description = [{ + Computes element-wise Sigmoid of input + }]; + + let arguments = (ins TFL_TensorOf<[F32, QI8, QUI8, QI16, TFL_Quint8]>:$x); + + let results = (outs TFL_TensorOf<[F32, QI8, QUI8, QI16, TFL_Quint8]>:$y); + + let extraClassDeclaration = [{ + // FixedOutputRangeInterface: + quant::UniformQuantizedType GetFixedOutputRange( + bool is_signed, int bit_width) { + auto result_type = getY().getType(); + // zero_point = 0 + // scale = 1. / (max_value + 1) + return mlir::TFL::GetFixedOutputRange(is_signed, bit_width, result_type, + /*scale=*/1.0 / (1<<(bit_width)), + /*zero_point=*/-(1<<(bit_width-1))); + } + }]; + + // This builder doesn't work with quantized type, so it can only be used by + // non-quantization tablegen patterns. Currently, it is used by the + // elementwise-move reordering pattern in the optimize_patterns.td + let builders = [ + OpBuilder<(ins "Value":$input), + [{ + $_state.addOperands({input}); + $_state.addTypes(input.getType()); + }]> + ]; + + let hasFolder = 1; +} + +def TFL_LogOp: TFL_Op<"log", [ + Pure, + QuantizableResult, + TFL_SameFirstOperandAndFirstResultElementType]> { + let summary = "Natural logarithm operator"; + + let description = [{ + Performs element-wise natural logarithm operation on input. + }]; + + let arguments = (ins TFL_TensorOf<[F32, QI8]>:$x); + + let results = (outs TFL_TensorOf<[F32, QI8]>:$y); + + let hasFolder = 1; + + let extraClassDeclaration = [{ + // Returns whether the return types are compatible. + static bool isCompatibleReturnTypes(TypeRange l, TypeRange r) { + return TF::ArraysAreCastCompatible(l, r); + } + }]; + + // This builder doesn't work with quantized type, so it can only be used by + // non-quantization tablegen patterns. + let builders = [ + OpBuilder<(ins "Value":$input), + [{ + $_state.addOperands({input}); + $_state.addTypes(input.getType()); + }]> + ]; +} + +def TFL_LogSoftmaxOp : TFL_Op<"log_softmax", [ + Pure, + SameOperandsAndResultShape, + PredOpTrait<"x and y must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + FixedOutputRangeInterface, + QuantizableResult, + DeclareOpInterfaceMethods]> { + let summary = "Log softmax operator"; + + let description = [{ + Computes element-wise log softmax activations with the following formula + + input - log(reduce_sum(exp(input), dim)) + }]; + + let arguments = (ins TFL_TensorOf<[F32, QUI8, QI8, TFL_Quint8]>:$input); + + let results = (outs TFL_TensorOf<[F32, QUI8, QI8, TFL_Quint8]>:$output); + + let hasOptions = 1; + + let extraClassDeclaration = [{ + // FixedOutputRangeInterface: + quant::UniformQuantizedType GetFixedOutputRange( + bool is_signed, int bit_width) { + auto result_type = getOutput().getType(); + // zero_point = max_value + // scale = -log_softmax_output_min / (max_value + 1) + return mlir::TFL::GetFixedOutputRange(is_signed, bit_width, result_type, + /*scale=*/16.0 / 256, /*zero_point=*/127); + } + }]; +} + +def TFL_MaxPool2DOp : TFL_Op<"max_pool_2d", [ + TFL_OperandHasRank<0, 4>, + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + Pure, + SameOperandsAndResultsScale, + QuantizableResult, + DeclareOpInterfaceMethods]> { + let summary = "Max Pool 2D op"; + + let description = [{ + Performs max pool 2D on input. + + Inputs: + `inputs[0]`: required: the input tensor + }]; + + let arguments = ( + ins TFL_TensorOf<[F32, QUI8, QI8, QI16, TFL_Quint8]>:$input, + TFL_PaddingAttr:$padding, + I32Attr:$stride_w, + I32Attr:$stride_h, + I32Attr:$filter_width, + I32Attr:$filter_height, + TFL_AFAttr:$fused_activation_function + ); + + let results = (outs TFL_TensorOf<[F32, QUI8, QI8, QI16, TFL_Quint8]>:$output); + + let hasOptions = 1; + + let customOption = "Pool2DOptions"; +} + +def TFL_MaximumOp : TFL_Op<"maximum", [ + ResultsBroadcastableShape, + Pure, + QuantizableResult, + TFL_OperandsHaveSameShapesOrBroadcastableShape<[0, 1], 5>, + Commutative, + SameOperandsAndResultsScale]> { + let summary = "Max operator"; + let description = [{ + Element-wise max operation. + }]; + + let arguments = ( + ins TFL_TensorOf<[F32, TFL_Int32Or64, QI8, QUI8, QI16]>:$lhs, + TFL_TensorOf<[F32, TFL_Int32Or64, QI8, QUI8, QI16]>:$rhs + ); + + let results = (outs + TFL_TensorOf<[F32, TFL_Int32Or64, QI8, QUI8, QI16]>:$max + ); + + let hasFolder = 1; + + let builders = [TFL_BroadcastableBinaryBuilder]; + + let hasOptions = 0; +} + +def TFL_MeanOp : TFL_Op<"mean", [ + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + QuantizableResult, + Pure]> { + let summary = "Mean operator"; + + let description = [{ + Computes the mean of elements across dimensions of a tensor. + Reduces input_tensor along the dimensions given in axis. + Unless keepdims is true, the rank of the tensor is reduced by 1 for + each entry in axis. If keepdims is true, the reduced dimensions are retained + with length 1. + }]; + + let arguments = (ins + TFL_TensorOf<[F32, I32, I64, QI8, QUI8, UI8, QI16]>:$input, + TFL_TensorOf<[I32]>:$axis, + BoolAttr:$keep_dims + ); + + let results = (outs + TFL_TensorOf<[F32, I32, I64, QI8, QUI8, UI8, QI16]>:$output); + + let hasOptions = 1; + let customOption = "ReducerOptions"; +} + +def TFL_OneHotOp : TFL_Op<"one_hot", [ + QuantizableResult, + Pure]> { + let summary = "OneHot operator"; + + let description = [{ + Returns a one-hot tensor.The locations represented by indices in `indices` + take value `on_value`, while all other locations take value `off_value`. + + If the input `indices` is rank `N`, the output will have rank `N+1`, + The new axis is created at dimension `axis` (default: the new axis is + appended at the end). + }]; + + let arguments = (ins + TFL_TensorOf<[I32, I64]>:$indices, + TFL_I32Tensor:$depth, + TFL_TensorOf<[F32, I32, I64, I1, I8, UI8]>:$on_value, + TFL_TensorOf<[F32, I32, I64, I1, I8, UI8]>:$off_value, + + I32Attr:$axis + ); + + let results = (outs + TFL_TensorOf<[F32, I32, I64, I1, I8, UI8]>:$output + ); + + let hasOptions = 1; +} + +def TFL_RoundOp: TFL_Op<"round", [ + Pure, + TF_SameOperandsAndResultTypeResolveRef]> { + let summary = "Round operator"; + + let description = [{ +Rounds the values of a tensor to the nearest integer, element-wise. + }]; + + let arguments = (ins + TFL_FpTensor:$x + ); + + let results = (outs + TFL_FpTensor:$y + ); + + let extraClassDeclaration = [{ + // Returns whether the return types are compatible. + static bool isCompatibleReturnTypes(TypeRange l, TypeRange r) { + return TF::ArraysAreCastCompatible(l, r); + } + }]; +} + +def TFL_SliceOp : TFL_Op<"slice", [ + QuantizableResult, + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + Pure, + SameOperandsAndResultsScale, + TFL_RuntimePredOpTrait<"input must have rank at most 6", + TFL_OperandHasRankAtMostPred<0, 6>>, + TFL_OperandHasRankAtMost<1, 1>, + TFL_OperandHasRankAtMost<2, 1>]> { + let summary = "Return a slice from 'input'."; + + let description = [{ +The output tensor is a tensor with dimensions described by 'size' +whose values are extracted from 'input' starting at the offsets in +'begin'. + +`begin` is zero-based; `size` is one-based. If size[i] is -1, all remaining +elements in dimension i are included in the slice. In other words, this is +equivalent to setting: + size[i] = input.dim_size(i) - begin[i] + +*Requirements*: + 0 <= begin[i] <= begin[i] + size[i] <= Di for i in [0, n) + }]; + + let arguments = (ins + TFL_TensorOf<[F32, F16, I32, I64, QI4, I8, UI8, UI32, I1, TFL_Str, QI8, QUI8, TFL_Quint8, QI16]>:$input, + TFL_I32OrI64Tensor:$begin, + TFL_I32OrI64Tensor:$size + ); + + let results = (outs + TFL_TensorOf<[F32, F16, I32, I64, QI4, I8, UI8, UI32, I1, TFL_Str, QI8, QUI8, TFL_Quint8, QI16]>:$output + ); + + let hasVerifier = 1; + + let hasCanonicalizer = 1; + + let hasFolder = 1; +} + +def TFL_SumOp: TFL_Op<"sum", [ + QuantizableResult, + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + // TODO(b/215655380): Re-enable this once there is 16-bit MLIR quantizer. + // SameOperandsAndResultsScale, + Pure]> { + + let summary = "Sum operator"; + + let description = [{ + Computes the sum reduction along the specified axes + }]; + + let arguments = (ins + TFL_TensorOf<[F32, I32, I64, QI8, QUI8, TFL_Quint8, QI16]>:$input, + TFL_I32Tensor:$axes, + BoolAttr:$keep_dims + ); + + let results = (outs TFL_TensorOf<[F32, I32, I64, QI8, QUI8, TFL_Quint8, QI16]>:$output); + + let hasOptions = 1; + let customOption = "ReducerOptions"; + + let hasFolder = 1; + + let builders = [ + OpBuilder<(ins "Value":$input, "Value":$axes, + "BoolAttr":$keep_dims)> + ]; + + // TODO(b/215655380): Re-enable this once there is 16-bit MLIR quantizer. + // + //let extraClassDeclaration = [{ + // // SameScalesOpInterface: + // bool RequiredSameOperandsAndResultsScale(bool sign, int bit_width) { + // // Eight-bit types don't require same operands and results scales. + // return bit_width != 8; + // } + //}]; +} + +def TFL_ReduceMinOp: TFL_Op<"reduce_min", [ + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + Pure, + QuantizableResult, + SameOperandsAndResultsScale]> { + let summary = "Min-reduction operator"; + + let description = [{ + Computes the min reduction along the specified axes + }]; + + let arguments = (ins + TFL_TensorOf<[F32, I32, I64, QI8, QUI8, TFL_Quint8, QI16]>:$input, + TFL_I32Tensor:$axes, + BoolAttr:$keep_dims + ); + + let results = (outs + TFL_TensorOf<[F32, I32, I64, QI8, QUI8, TFL_Quint8, QI16]>:$output); + + let hasOptions = 1; + let customOption = "ReducerOptions"; +} + +def TFL_ReduceMaxOp: TFL_Op<"reduce_max", [ + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + Pure, + QuantizableResult, + SameOperandsAndResultsScale]> { + let summary = "Max-reduction operator"; + + let description = [{ + Computes the max reduction along the specified axes + }]; + + let arguments = (ins + TFL_TensorOf<[F32, I32, I64, QI8, QUI8, TFL_Quint8, QI16]>:$input, + TFL_I32Tensor:$axes, + BoolAttr:$keep_dims + ); + + let results = (outs + TFL_TensorOf<[F32, I32, I64, QI8, QUI8, TFL_Quint8, QI16]>:$output); + + let hasOptions = 1; + let customOption = "ReducerOptions"; +} + +def TFL_ReduceProdOp: TFL_Op<"reduce_prod", [ + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + QuantizableResult, + Pure]> { + let summary = "Prod-reduction operator"; + + let description = [{ + Computes the product along the specified axes + }]; + + let arguments = (ins + TFL_TensorOf<[F32, I32, I64, QI8, QUI8, TFL_Quint8, QI16]>:$input, + TFL_I32Tensor:$axes, + BoolAttr:$keep_dims + ); + + let results = (outs + TFL_TensorOf<[F32, I32, I64, QI8, QUI8, TFL_Quint8, QI16]>:$output); + + let hasOptions = 1; + let customOption = "ReducerOptions"; +} + +def TFL_MinimumOp : TFL_Op<"minimum", [ + ResultsBroadcastableShape, + Pure, + TFL_OperandsHaveSameShapesOrBroadcastableShape<[0, 1], 5>, + Commutative, + QuantizableResult, + SameOperandsAndResultsScale]> { + let summary = "Min operator"; + let description = [{ + Element-wise min operation. + }]; + + let arguments = ( + ins TFL_TensorOf<[F32, TFL_Int32Or64, QI8, QUI8, QI16]>:$lhs, + TFL_TensorOf<[F32, TFL_Int32Or64, QI8, QUI8, QI16]>:$rhs + ); + + let results = (outs + TFL_TensorOf<[F32, TFL_Int32Or64, QI8, QUI8, QI16]>:$min + ); + + let hasFolder = 1; + + let builders = [TFL_BroadcastableBinaryBuilder]; + + let hasOptions = 0; +} + +def TFL_MulOp : TFL_Op<"mul", [ + ResultsBroadcastableShape, + Pure, + Commutative, + QuantizableResult, + BinaryOpSameElementTypeConstraint, + TFL_RuntimePredOpTrait<"Operands do not have valid shapes", + CPred<"TFL::VerifyMulOpShapeConstraints(llvm::cast($_op))">>, + DeclareOpInterfaceMethods]> { + let summary = "Multiplication operator"; + + let description = [{ + Element-wise multiplication operation. + }]; + + let arguments = ( + ins TFL_TensorOf<[F32, F16, I32, UI32, I64, QI8, QUI8, QI16, I16, Complex>]>:$lhs, + TFL_TensorOf<[F32, F16, I32, UI32, I64, QI8, QUI8, QI16, I16, Complex>]>:$rhs, + TFL_AFAttr:$fused_activation_function); + + let results = (outs TFL_TensorOf<[F32, F16, I32, UI32, I64, QI8, QUI8, QI16, I16, Complex>]>:$output); + + let hasFolder = 1; + + let builders = [TFL_FusedBroadcastableBinaryBuilder]; + + let hasCustomAssemblyFormat = 1; + + let extraClassDefinition = [{ + ParseResult $cppClass::parse(OpAsmParser &parser, OperationState &result) { + return parseOneResultSameOperandTypeOp(parser, result); + } + void $cppClass::print(OpAsmPrinter &p) { + return printOneResultOp(getOperation(), p); + } + }]; + + let hasOptions = 1; +} + +def TFL_NegOp: TFL_Op<"neg", [ + Pure, + TF_SameOperandsAndResultTypeResolveRef]> { + let summary = "Negation operator"; + + let description = [{ + Computes element-wise negation of input + }]; + + let arguments = (ins TFL_TensorOf<[F32, I32, I64]>:$x); + + let results = (outs TFL_TensorOf<[F32, I32, I64]>:$y); + + let hasOptions = 0b1; + + let hasFolder = 1; + + let extraClassDeclaration = [{ + // Returns whether the return types are compatible. + static bool isCompatibleReturnTypes(TypeRange l, TypeRange r) { + return TF::ArraysAreCastCompatible(l, r); + } + }]; +} + +def TFL_PackOp : TFL_Op<"pack", [ + TFL_SameFirstOperandAndFirstResultElementType, + Pure, + QuantizableResult, + SameOperandsAndResultsScale]> { + let summary = "Packs a list of tensors along a dimension into one tensor"; + + let description = [{ + Packs a list of `values_count` rank-`R` tensors into one rank-`(R+1)` + tensor. + + Packs the `values_count` tensors in `values` into a tensor with rank one + higher than each tensor in `values`, by packing them along the `axis` + dimension. + + Given a list of tensors of shape `(A, B, C)`; + + if `axis == 0` then the `output` tensor will have the shape `(N, A, B, C)`. + if `axis == 1` then the `output` tensor will have the shape `(A, N, B, C)`. + Etc. + + For example: + + ``` + # 'x' is [1, 4] + # 'y' is [2, 5] + # 'z' is [3, 6] + pack([x, y, z]) => [[1, 4], [2, 5], [3, 6]] # Pack along first dim. + pack([x, y, z], axis=1) => [[1, 2, 3], [4, 5, 6]] + ``` + + This is the opposite of `unpack`. + }]; + + let arguments = (ins + TFL_VariadicTensorOf<[F32, I8, I16, I32, I64, UI8, UI32, QI8, QUI8, QI16, TFL_Quint8]>:$values, + + ConfinedAttr:$values_count, + I32Attr:$axis + ); + + let results = (outs + TFL_TensorOf<[F32, I8, I16, I32, I64, UI8, UI32, QI8, QUI8, QI16, TFL_Quint8]>:$output + ); + + let hasVerifier = 1; + + let hasCanonicalizer = 1; + + let hasOptions = 1; +} + +def TFL_PadOp : TFL_Op<"pad", [ + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + Pure, + SameOperandsAndResultsScale, + TFL_OperandHasRankAtMost<0, 5>, + TFL_OperandHasRank<1, 2>, + TFL_OperandRankEquals1DimOfOperand<0, 1>, + QuantizableResult, + PredOpTrait<"the first dim size of the padding argument must be at most 5", + Or<[TFL_OperandIsUnrankedPred<1>, + TFL_OperandDimIsAtMost<1, 0, 5>]>>]> { + let summary = "Padding operator"; + + let description = [{ + This operation pads a `input` with zeros according to the `paddings` you + specify. `paddings` is an integer tensor with shape `[Dn, 2]`, where n is + the rank of `input`. For each dimension D of `input`, `paddings[D, 0]` + indicates how many zeros to add before the contents of `input` in that + dimension, and `paddings[D, 1]` indicates how many zeros to add after the + contents of `input` in that dimension. + + The padded size of each dimension D of the output is: + + `paddings(D, 0) + input.dim_size(D) + paddings(D, 1)` + + For example: + + ``` + # 't' is [[1, 1], [2, 2]] + # 'paddings' is [[1, 1], [2, 2]] + # rank of 't' is 2 + pad(t, paddings) ==> [[0, 0, 0, 0, 0, 0] + [0, 0, 1, 1, 0, 0] + [0, 0, 2, 2, 0, 0] + [0, 0, 0, 0, 0, 0]] + ``` + }]; + + let arguments = (ins TFL_TensorOf<[F32, I1, I32, I64, QI8, QUI8, TFL_Quint8, QI16]>:$input, + TFL_I32OrI64Tensor:$padding); + + let results = (outs TFL_TensorOf<[F32, I1, I32, I64, QI8, QUI8, TFL_Quint8, QI16]>:$output); + + let hasOptions = 1; + + let hasFolder = 1; + + let hasCanonicalizer = 1; +} + +def TFL_PadV2Op : TFL_Op<"padv2", [ + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + Pure, + QuantizableResult, + SameOperandsAndResultsScale, + TFL_OperandHasRankAtMost<0, 5>, + TFL_OperandHasRank<1, 2>, + TFL_OperandHasRank<2, 0>, + TFL_OperandRankEquals1DimOfOperand<0, 1>, + PredOpTrait<"the first dim size of the padding argument must be at most 5", + Or<[TFL_OperandIsUnrankedPred<1>, + TFL_OperandDimIsAtMost<1, 0, 5>]>>, + PredOpTrait<"input and constant value operands must have same element type", + TFL_TCopVTEtAreSameAt<0, 2>>]> { + let summary = "Padding operator v2"; + + let description = [{ + This operation pads a `input` according to the `paddings` and + `constant_values` you specify. `paddings` is an integer tensor with shape + `[Dn, 2]`, where n is the rank of `input`. For each dimension D of `input`, + `paddings[D, 0]` indicates how many zeros to add before the contents of + `input` in that dimension, and `paddings[D, 1]` indicates how many zeros to + add after the contents of `input` in that dimension. `constant_values` is a + scalar tensor of the same type as `input` that indicates the value to use + for padding `input`. + + The padded size of each dimension D of the output is: + + `paddings(D, 0) + input.dim_size(D) + paddings(D, 1)` + + For example: + + ``` + # 't' is [[1, 1], [2, 2]] + # 'paddings' is [[1, 1], [2, 2]] + # rank of 't' is 2 + pad(t, paddings) ==> [[0, 0, 0, 0, 0, 0] + [0, 0, 1, 1, 0, 0] + [0, 0, 2, 2, 0, 0] + [0, 0, 0, 0, 0, 0]] + ``` + }]; + + let arguments = ( + ins TFL_TensorOf<[F32, I1, I32, I64, UI8, QI8, QUI8, TFL_Quint8]>:$input, + TFL_I32OrI64Tensor:$padding, + TFL_TensorOf<[F32, I1, I32, I64, UI8, QI8, QUI8, TFL_Quint8]>:$constant_values); + + let results = (outs TFL_TensorOf<[F32, I1, I32, I64, UI8, QI8, QUI8, TFL_Quint8]>:$output); + + let hasOptions = 1; + + let hasFolder = 1; +} + +def TFL_PolyCallOp : Op, + SingleBlockImplicitTerminator<"YieldOp">]> { + let summary = [{Poly call}]; + + let description = [{ + Have multiple function bodies for the same computation. This allows a + program compiler/interpreter to choose one of the available options to + execute the program based on which one is most suitable for the target + backend. + + input: A list of input tensors whose types are T. + output: A list of output tensors whose types are T. + + call: Multiple regions, each of which encapsulates the same semantic + computation but in different forms. + }]; + + let arguments = (ins Variadic:$input); + + let results = (outs Variadic:$output); + + let regions = (region VariadicRegion>:$calls); + + let hasCanonicalizer = 1; +} + + +def TFL_PowOp : TFL_Op<"pow", [ + ResultsBroadcastableShape, + Pure, + TFL_OperandsHaveSameShapesOrBroadcastableShape<[0, 1], 4>]> { + let summary = "Power operator"; + + let description = [{ + Element-wise power operation. + }]; + + let arguments = ( + ins TFL_TensorOf<[F32, I32]>:$lhs, + TFL_TensorOf<[F32, I32]>:$rhs); + + let results = (outs TFL_TensorOf<[F32, I32]>:$output); + + let hasCustomAssemblyFormat = 1; + + let hasFolder = 1; + + let extraClassDefinition = [{ + ParseResult $cppClass::parse(OpAsmParser &parser, OperationState &result) { + return parseOneResultSameOperandTypeOp(parser, result); + } + void $cppClass::print(OpAsmPrinter &p) { + return printOneResultOp(getOperation(), p); + } + }]; + + let builders = [TFL_BroadcastableBinaryBuilder]; +} + +def TFL_PReluOp : TFL_Op<"prelu", [ + Pure, + QuantizableResult, + ResultsBroadcastableShape, + TFL_OperandsHaveSameShapesOrBroadcastableShape<[0, 1], 4>, + BinaryOpSameElementTypeConstraint, + PredOpTrait<"input and output must have the same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + AffineQuantizedOpInterface, AffineOpCoefficient<-1, 1>]> { + let summary = "Parameterized Relu operator"; + + let description = [{ + Parameterized Relu operator + x -> x >= 0 ? x : (alpha * x) + where alpha is a trainable tensor. + input and alpha should be the same size as input or be broadcastable. + }]; + + let arguments = ( + ins TFL_TensorOf<[F32, QI8, QUI8, TFL_Quint8]>:$input, + TFL_TensorOf<[F32, QI8, QUI8, TFL_Quint8]>:$alpha + ); + + let results = (outs TFL_TensorOf<[F32, QI8, QUI8, TFL_Quint8]>:$output); + + let hasVerifier = 1; + + let extraClassDeclaration = [{ + // AffineQuantizedOpInterface: + int GetChannelDimIndex() { return 0; } + int GetQuantizationDimIndex() { return -1; } + }]; +} + +def TFL_RankOp: TFL_Op<"rank", [ + QuantizableResult, + Pure]> { + let summary = "Rank operator."; + let description = [{ + Returns the rank of a tensor. + }]; + + let arguments = (ins AnyTensor:$input); + + let results = (outs TFL_IntTensor:$output); + + let hasFolder = 1; +} + +def TFL_ReluOp: TFL_Op<"relu", [ + PredOpTrait<"x and y must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + Pure, + QuantizableResult, + SameOperandsAndResultShape]> { + let summary = "Relu operator"; + + let description = [{ + Element-wise Relu operator + x -> max(0, x) + }]; + + let arguments = (ins TFL_TensorOf<[F32, QUI8, QI8, QI16]>:$x); + + let results = (outs TFL_TensorOf<[F32, QUI8, QI8, QI16]>:$y); + + let hasFolder = 1; + + // This builder doesn't work with quantized type, so it can only be used by + // non-quantization tablegen patterns. Currently, it is used by the + // elementwise-move reordering pattern in the optimize_patterns.td + let builders = [ + OpBuilder<(ins "Value":$input), + [{ + $_state.addOperands({input}); + $_state.addTypes(input.getType()); + }]> + ]; +} + +def TFL_Relu6Op: TFL_Op<"relu6", [ + PredOpTrait<"x and y must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + Pure, + QuantizableResult, + SameOperandsAndResultShape]> { + let summary = "Relu6 operator"; + + let description = [{ + Element-wise Relu6 operator + x -> max(0, min(6, x)) + }]; + + let arguments = (ins TFL_TensorOf<[F32, QUI8, QI8]>:$x); + + let results = (outs TFL_TensorOf<[F32, QUI8, QI8]>:$y); + + // This builder doesn't work with quantized type, so it can only be used by + // non-quantization tablegen patterns. Currently, it is used by the + // elementwise-move reordering pattern in the optimize_patterns.td + let builders = [ + OpBuilder<(ins "Value":$input), + [{ + $_state.addOperands({input}); + $_state.addTypes(input.getType()); + }]> + ]; +} + +def TFL_Relu0To1Op: TFL_Op<"relu_0_to_1", [ + PredOpTrait<"x and y must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + Pure, + QuantizableResult, + SameOperandsAndResultShape]> { + let summary = "Relu0To1 operator"; + + let description = [{ + Element-wise Relu0To1 operator + x -> max(0, min(1, x)) + }]; + + let arguments = (ins TFL_TensorOf<[F32, QUI8, QI8]>:$x); + + let results = (outs TFL_TensorOf<[F32, QUI8, QI8]>:$y); + + // This builder doesn't work with quantized type, so it can only be used by + // non-quantization tablegen patterns. + let builders = [ + OpBuilder<(ins "Value":$input), + [{ + $_state.addOperands({input}); + $_state.addTypes(input.getType()); + }]> + ]; +} + +def TFL_Relu1Op: TFL_Op<"relu_n1_to_1", [ + PredOpTrait<"x and y must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + Pure, + QuantizableResult, + SameOperandsAndResultShape]> { + let summary = "Relu1 operator"; + + let description = [{ + Element-wise Relu1 operator + x -> max(-1, min(1, x)) + }]; + + let arguments = (ins TFL_TensorOf<[F32, QUI8, QI8]>:$x); + + let results = (outs TFL_TensorOf<[F32, QUI8, QI8]>:$y); + + // This builder doesn't work with quantized type, so it can only be used by + // non-quantization tablegen patterns. Currently, it is used by the + // elementwise-move reordering pattern in the optimize_patterns.td + let builders = [ + OpBuilder<(ins "Value":$input), + [{ + $_state.addOperands({input}); + $_state.addTypes(input.getType()); + }]> + ]; +} + +def TFL_ReshapeOp: TFL_Op<"reshape", [ + QuantizableResult, + Pure, + SameOperandsAndResultsScale, + DeclareOpInterfaceMethods]> { + let summary = "Reshape operator"; + + let description = [{ + Produces a tensor with the same values but different static shape defined + by the output type. + }]; + + let arguments = ( + ins AnyTensor:$input, + TFL_I32Tensor:$shape); + + let results = (outs AnyTensor:$output); + let hasCanonicalizer = 0b1; + let hasFolder = 1; + let hasVerifier = 1; + + let extraClassDeclaration = [{ + static bool isCompatibleReturnTypes(TypeRange l, TypeRange r); + // Quantized axes are verified in the Verify function. + bool RequiredSameQuantizedAxes() { return false; } + }]; +} + +def TFL_ReverseSequenceOp : TFL_Op<"reverse_sequence", [ + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + Pure, + QuantizableResult, + TFL_OperandHasRank<1, 1>]> { + let summary = "Reverses variable length slices."; + + let description = [{ +This op first slices `input` along the dimension `batch_dim`, and for each +slice `i`, reverses the first `seq_lengths[i]` elements along +the dimension `seq_dim`. + +The elements of `seq_lengths` must obey `seq_lengths[i] <= input.dims[seq_dim]`, +and `seq_lengths` must be a vector of length `input.dims[batch_dim]`. + +The output slice `i` along dimension `batch_dim` is then given by input +slice `i`, with the first `seq_lengths[i]` slices along dimension +`seq_dim` reversed. + }]; + + let arguments = (ins + TFL_TensorOf<[F32, I32, I64, QI16, QUI8, TFL_Quint8]>:$input, + TFL_I32OrI64Tensor:$seq_lengths, + + ConfinedAttr:$seq_dim, + ConfinedAttr:$batch_dim + ); + + let results = (outs + TFL_TensorOf<[F32, I32, I64, QI16, QUI8, TFL_Quint8]>:$output + ); + + let hasOptions = 1; +} + +def TFL_RsqrtOp: TFL_Op<"rsqrt", [Pure, + QuantizableResult, + TFL_SameFirstOperandAndFirstResultElementType, + SameOperandsAndResultShape]> { + let summary = "Reciprocal of square root operator"; + + let description = [{ + Computes element-wise reverse square root of input + }]; + + let arguments = (ins TFL_TensorOf<[F32, QI8, QI16]>:$x); + + let results = (outs TFL_TensorOf<[F32, QI8, QI16]>:$y); + + let hasFolder = 1; + + // This builder doesn't work with quantized type, so it can only be used by + // non-quantization tablegen patterns. + let builders = [ + OpBuilder<(ins "Value":$input), + [{ + $_state.addOperands({input}); + $_state.addTypes(input.getType()); + }]> + ]; +} + +def TFL_ShapeOp: TFL_Op<"shape", [ + QuantizableResult, + Pure]> { + let summary = "Shape operator"; + + let description = [{ + Returns the shape of a tensor. + }]; + + let arguments = (ins AnyTensor:$input); + + let results = (outs TFL_TensorOf<[I32, I64]>:$output); + + DerivedTypeAttr out_type = DerivedTypeAttr<[{ + return llvm::cast(getResult().getType()).getElementType(); + }]>; + + let hasOptions = 1; + + let hasFolder = 1; +} + +def TFL_RangeOp: TFL_Op<"range", [ + Pure, + TFL_OperandHasRank<0, 0>, + TFL_OperandHasRank<1, 0>, + TFL_OperandHasRank<2, 0>, + PredOpTrait<"operands and output must have same element type", + And<[TCresVTEtIsSameAsOp<0, 0>, TCresVTEtIsSameAsOp<0, 1>, + TCresVTEtIsSameAsOp<0, 2>]>>]> { + let summary = "Range operator"; + + let description = [{ + Returns a 1D tensor defined by a sequence from `start` to `limit` with + a given `delta`. + }]; + + let arguments = (ins + TFL_TensorOf<[I32, F32, I64]>:$start, + TFL_TensorOf<[I32, F32, I64]>:$limit, + TFL_TensorOf<[I32, F32, I64]>:$delta); + + let results = (outs TFL_TensorOf<[I32, F32, I64]>:$result); + + let hasFolder = 1; +} + +def TFL_ReverseV2Op: TFL_Op<"reverse_v2", [ + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + Pure, + QuantizableResult, + TFL_OperandHasRank<1, 1>]> { + let summary = "ReverseV2 Operator"; + + let description = [{ + Reverses specific dimensions of a tensor. + + Given a tensor, and a int32/int64 tensor axis representing the set + of dimensions of tensor to reverse. + This operation reverses each dimension i for + which there exists j s.t. axis[j] == i. + + Args: + tensor: A Tensor. Must be one of the following types: + uint8, int8, int16, int32, int64, float32, bool Up to 8-D. + + axis: A Tensor. Must be one of the following types: int32, int64. + with only 1 element which is the axis index. + TODO: Add support for multiple elements. + }]; + + let hasFolder = 1; + + let arguments = ( + ins + TFL_TensorOf<[F32, UI8, I16, I32, I64, QI16, QUI8, QI8, TFL_Quint8, I1]>:$input, + TFL_I32Tensor:$axis + ); + + let results = (outs + TFL_TensorOf<[F32, UI8, I16, I32, I64, QI16, QUI8, QI8, TFL_Quint8, I1]>:$output); +} + +// Select has many instances in TF models where one or more of its operands +// are unranked. Therefore, we skip adding shape constraints here. +def TFL_SelectOp : TFL_Op<"select", [ + Pure, + SameOperandsAndResultsScale, + QuantizableResult, + PredOpTrait<"operands have same element type", TFL_TCopVTEtAreSameAt<1, 2>>, + PredOpTrait<"operands and result have same element type", + TFL_TCresVTEtIsSameAsOp<0, 1>>]> { + let summary = "Select operator"; + + let description = [{ + Select values of 'x' if the corresponding value of 'condition' is true or + the value of 'y' if false. There are valid condition input sizes: + + 1. Either the same shape (in which case the select is elementwise), or + 2. condition must be Rank 1 and match over the first dimension. + }]; + + let arguments = (ins + TFL_BoolTensor:$condition, + TFL_TensorOf<[F32, I1, I8, I16, I32, I64, UI32, QI8, QUI8, QI16, TFL_Quint8]>:$x, + TFL_TensorOf<[F32, I1, I8, I16, I32, I64, UI32, QI8, QUI8, QI16, TFL_Quint8]>:$y); + + let results = (outs + TFL_TensorOf<[F32, I1, I8, I16, I32, I64, UI32, QI8, QUI8, QI16, TFL_Quint8]>:$output); + + let hasFolder = 1; + + // TODO(jpienaar): autogenerate this. + let builders = [ + OpBuilder<(ins "Value":$condition, "Value":$x, "Value":$y), + [{ + auto resultType = x.getType(); + $_state.addOperands({condition, x, y}); + $_state.types.push_back(resultType); + }]>]; + + let hasOptions = 1; +} + +def TFL_SelectV2Op : TFL_Op<"select_v2", [ + ResultsBroadcastableShape, + Pure, + QuantizableResult, + SameOperandsAndResultsScale, + TFL_OperandsHaveSameShapesOrBroadcastableShape<[0, 1, 2], 5>, + PredOpTrait<"operands have same element type", TFL_TCopVTEtAreSameAt<1, 2>>, + PredOpTrait<"operands and result have same element type", + TFL_TCresVTEtIsSameAsOp<0, 1>>]> { + let summary = "SelectV2 operator"; + + let description = [{ + Select values of 'x' if the corresponding value of 'condition' is true or + the value of 'y' if false. There are valid condition input sizes: + + 1. Either the same shape (in which case the select is elementwise), or + 2. Broadcastable shapes between 'condition', 'x' and 'y'. + }]; + + let arguments = (ins + TFL_BoolTensor:$condition, + TFL_TensorOf<[F32, I1, I8, I16, I32, I64, UI32, QI8, QUI8, QI16, TFL_Quint8]>:$x, + TFL_TensorOf<[F32, I1, I8, I16, I32, I64, UI32, QI8, QUI8, QI16, TFL_Quint8]>:$y); + + let results = (outs + TFL_TensorOf<[F32, I1, I8, I16, I32, I64, UI32, QI8, QUI8, QI16, TFL_Quint8]>:$output); + + let builders = [ + OpBuilder<(ins "Value":$cond, "Value":$x, "Value":$y), + [{ + BuildSelectV2Op(&$_builder, $_state, cond, x, y); + }]>]; + + let hasOptions = 1; +} + +def TFL_SinOp: TFL_Op<"sin", [ + Pure, + TF_SameOperandsAndResultTypeResolveRef]> { + let summary = "Sine operator"; + + let description = [{ + Computes element-wise Sine of input + }]; + + let arguments = (ins TFL_TensorOf<[F32, F16]>:$x); + + let results = (outs TFL_TensorOf<[F32, F16]>:$y); + + let hasFolder = 1; + + let extraClassDeclaration = [{ + // Returns whether the return types are compatible. + static bool isCompatibleReturnTypes(TypeRange l, TypeRange r) { + return TF::ArraysAreCastCompatible(l, r); + } + }]; +} + +def TFL_SoftmaxOp : TFL_Op<"softmax", [ + Pure, + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + TFL_OperandHasRankAtLeast<0, 1>, + SameOperandsAndResultShape, + QuantizableResult, + FixedOutputRangeInterface, + DeclareOpInterfaceMethods]> { + let summary = "Softmax operator"; + + let description = [{ + Computes element-wise softmax activations with the following formula + + exp(input * beta) / tf.reduce_sum(exp(input * beta), dim) + }]; + + let arguments = ( + ins TFL_TensorOf<[F32, F16, QI8, QUI8, TFL_Quint8, QI16]>:$input, + F32Attr:$beta + ); + + let results = (outs TFL_TensorOf<[F32, F16, QI8, QUI8, TFL_Quint8, QI16]>:$output); + + let hasOptions = 1; + + let extraClassDeclaration = [{ + // FixedOutputRangeInterface: + quant::UniformQuantizedType GetFixedOutputRange( + bool is_signed, int bit_width) { + if (bit_width != 8 && bit_width != 16) { return nullptr; } + auto result_type = getOutput().getType(); + // zero_point = 0 + // scale = 1. / (max_value + 1) + return mlir::TFL::GetFixedOutputRange(is_signed, bit_width, result_type, + /*scale=*/1.0 / (bit_width == 8 ? (1<<(bit_width)) : (1<<(bit_width-1))), + /*zero_point=*/bit_width == 8 ? -(1<<(bit_width-1)): 0); + } + }]; +} + +def TFL_SqrtOp: TFL_Op<"sqrt", [ + Pure, + QuantizableResult, + TFL_SameFirstOperandAndFirstResultElementType, + SameOperandsAndResultShape]> { + let summary = "Square root operator"; + + let description = [{ + Computes element-wise Square root of input + }]; + + let arguments = (ins TFL_TensorOf<[F32, QI8, QI16]>:$x); + + let results = (outs TFL_TensorOf<[F32, QI8, QI16]>:$y); + + let hasFolder = 1; + + let builders = [ + OpBuilder<(ins "Value":$input), + [{ + $_state.addOperands({input}); + $_state.addTypes(input.getType()); + }]> + ]; + + let extraClassDeclaration = [{ + // Returns whether the return types are compatible. + static bool isCompatibleReturnTypes(TypeRange l, TypeRange r) { + return TF::ArraysAreCastCompatible(l, r); + } + }]; +} + +def TFL_SquareOp: TFL_Op<"square", [ + Pure, + TF_SameOperandsAndResultTypeResolveRef]> { + let summary = "Square operator"; + + let description = [{ + Computes element-wise Square of input + }]; + + let arguments = (ins TFL_FpTensor:$x); + + let results = (outs TFL_FpTensor:$y); + + let hasOptions = 0b1; + + let hasFolder = 1; + + let extraClassDeclaration = [{ + // Returns whether the return types are compatible. + static bool isCompatibleReturnTypes(TypeRange l, TypeRange r) { + return TF::ArraysAreCastCompatible(l, r); + } + }]; +} + +def TFL_SubOp : TFL_Op<"sub", [ + ResultsBroadcastableShape, + BinaryOpSameElementTypeConstraint, + TFL_RuntimePredOpTrait<"Operands do not have valid shapes", + CPred<"TFL::VerifySubOpShapeConstraints(llvm::cast($_op))">>, + Pure, + QuantizableResult, + DeclareOpInterfaceMethods]> { + let summary = "Subtraction operator"; + + let description = [{ + Element-wise subtraction operation. + }]; + + let arguments = ( + ins TFL_TensorOf<[F32, I32, I64, QI8, QUI8, QI16]>:$lhs, + TFL_TensorOf<[F32, I32, I64, QI8, QUI8, QI16]>:$rhs, + TFL_AFAttr:$fused_activation_function); + + let results = (outs TFL_TensorOf<[F32, I32, I64, QI8, QUI8, QI16]>:$output); + + let hasFolder = 1; + + let builders = [TFL_FusedBroadcastableBinaryBuilder]; + + let hasCustomAssemblyFormat = 1; + + let extraClassDefinition = [{ + ParseResult $cppClass::parse(OpAsmParser &parser, OperationState &result) { + return parseOneResultSameOperandTypeOp(parser, result); + } + void $cppClass::print(OpAsmPrinter &p) { + return printOneResultOp(getOperation(), p); + } + }]; + + let hasOptions = 1; +} + +def TFL_SquaredDifferenceOp : TFL_Op<"squared_difference", [ + TFL_OperandsHaveSameShapesOrBroadcastableShape<[0, 1], 4>, + BinaryOpSameElementTypeConstraint, + TFL_SameFirstOperandAndFirstResultElementType, + ResultsBroadcastableShape, + QuantizableResult, + Pure]> { + let summary = "Squared difference operator"; + + let description = [{ + Element-wise squared difference operation. + }]; + + let arguments = ( + ins TFL_TensorOf<[F32, I32, QI8]>:$lhs, + TFL_TensorOf<[F32, I32, QI8]>:$rhs); + + let results = (outs TFL_TensorOf<[F32, I32, QI8]>:$output); + + let builders = [TFL_BroadcastableBinaryBuilder]; + + let hasCustomAssemblyFormat = 1; + + let extraClassDefinition = [{ + ParseResult $cppClass::parse(OpAsmParser &parser, OperationState &result) { + return parseOneResultSameOperandTypeOp(parser, result); + } + void $cppClass::print(OpAsmPrinter &p) { + return printOneResultOp(getOperation(), p); + } + }]; +} + +def TFL_TanhOp: TFL_Op<"tanh", [ + Pure, + SameOperandsAndResultShape, + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + FixedOutputRangeInterface, + QuantizableResult, + DeclareOpInterfaceMethods]> { + let summary = "Hyperbolic tangent operator"; + + let description = [{ + Computes element-wise Hyperbolic tangent of input + }]; + + let arguments = (ins TFL_TensorOf<[F32, QI8, QUI8, QI16, TFL_Quint8]>:$input); + + let results = (outs TFL_TensorOf<[F32, QI8, QUI8, QI16, TFL_Quint8]>:$output); + + // This builder doesn't work with quantized type, so it can only be used by + // non-quantization tablegen patterns. Currently, it is used by the + // elementwise-move reordering pattern in the optimize_patterns.td + let builders = [ + OpBuilder<(ins "Value":$input), + [{ + $_state.addOperands({input}); + $_state.addTypes(input.getType()); + }]> + ]; + + let extraClassDeclaration = [{ + // FixedOutputRangeInterface: + quant::UniformQuantizedType GetFixedOutputRange( + bool is_signed, int bit_width) { + auto result_type = getOutput().getType(); + // central_value = min_value / 2 + (max_value - 1) / 2 + 1 + // zero_point = central_value + // scale = 1. / (central_value - min_value) + return mlir::TFL::GetFixedOutputRange(is_signed, bit_width, result_type, + /*scale=*/1.0 / (1<<(bit_width-1)), /*zero_point=*/0); + } + }]; + + let hasFolder = 1; +} + +def TFL_TileOp: TFL_Op<"tile", [ + Pure, + SameOperandsAndResultsScale, + QuantizableResult, + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>]> { + let summary = "Tile operator."; + let description = [{ + Constructs a tensor by tiling a given tensor. + + This operation creates a new tensor by replicating input + multiples times. The output tensor's i'th dimension has + input.dims(i) * multiples[i] elements, and the values of input + are replicated multiples[i] times along the 'i'th dimension. + For example, tiling [a b c d] by [2] produces [a b c d a b c d]. + }]; + + let arguments = (ins + TFL_TensorOf<[F32, I1, I32, I64, UI8, QI8, QUI8, TFL_Str]>:$input, + TFL_I32OrI64Tensor:$multiples); + + let results = (outs + TFL_TensorOf<[F32, I1, I32, I64, UI8, QI8, QUI8, TFL_Str]>:$output); + + let hasOptions = 0; +} + +// TODO(jpienaar): Maybe make it accept any single element tensor as `k`. +// TODO(jpienaar): Check that input has one or more dimensions. +// TODO(jpienaar): Check that k is less or equal the internal dimension +def TFL_TopKV2Op: TFL_Op<"topk_v2", [ + QuantizableResult, + Pure, + TFL_OperandHasRankAtLeast<0, 1>, + TFL_OperandHasRank<1, 0>, + PredOpTrait<"result and input element type match", + TFL_TCresVTEtIsSameAsOp<0,0>>, + SameOperandsAndResultsScale]> { + let summary = "TopK operator"; + + let description = [{ + Returns the top `k` largest element along each last dimensional slice of + `input` and the indices of values within the last dimension of the input + tensor. + + Results are always sorted in the descending order. + }]; + + let arguments = (ins + TFL_TensorOf<[F32, I8, I16, I32, I64, UI8, QI8, QUI8]>:$input, + TFL_TensorOf<[I16, I32]>:$k); + + let results = (outs + TFL_TensorOf<[F32, I8, I16, I32, I64, UI8, QI8, QUI8]>:$values, + TFL_TensorOf<[I16, I32]>:$indices); + + let builders = [ + OpBuilder<(ins "Value":$input, "Value":$k), + [{ BuildTopKOp(&$_builder, $_state, input, k); }]>]; + + let hasOptions = 1; +} + +def TFL_TransposeOp : TFL_Op<"transpose", [ + Pure, + QuantizableResult, + TFL_TransposeOperandHasEffectiveRankAtMost<0, 8>, + TFL_OperandHasRank<1, 1>, + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + SameOperandsAndResultsScale]> { + let summary = "Transpose operator"; + + let description = [{ + Returns the Transpose of x + }]; + + let arguments = (ins + TFL_TensorOf<[I32, BF16, F32, F16, I8, UI8, QI8, QUI8, TFL_I4, QI4, TFL_Quint8, I1, I64, QI16]>:$input, + TFL_TensorOf<[I32]>:$perm + ); + + let results = (outs + TFL_TensorOf<[I32, BF16, F32, F16, I8, UI8, QI8, QUI8, TFL_I4, QI4, TFL_Quint8, I1, I64, QI16]>:$output + ); + + let hasVerifier = 1; + + let hasFolder = 1; + + let hasCanonicalizer = 1; + + let builders = [ + OpBuilder<(ins "Value":$input, "Value":$perm), + [{ BuildTransposeOp(&$_builder, $_state, input, perm); }]> + ]; + + let extraClassDeclaration = [{ + // Quantized axes are verified in the Verify function. + bool RequiredSameQuantizedAxes() { return false; } + }]; +} + +def TFL_UnpackOp : TFL_Op<"unpack", [ + Pure, + QuantizableResult, + SameOperandsAndResultElementType, + SameOperandsAndResultsScale, + DeclareOpInterfaceMethods]> { + let summary = "Unpacks a tensor along a dimension into multiple tensors"; + + let description = [{ + Unpacks a given dimension of a rank-`R` tensor into `num` rank-`(R-1)` tensors. + + Unpacks `num` tensors from `value` by chipping it along the `axis` dimension. + For example, given a tensor of shape `(A, B, C, D)`; + + If `axis == 0` then the i'th tensor in `output` is the slice `value[i, :, :, :]` + and each tensor in `output` will have shape `(B, C, D)`. (Note that the + dimension unpacked along is gone, unlike `split`). + + If `axis == 1` then the i'th tensor in `output` is the slice `value[:, i, :, :]` + and each tensor in `output` will have shape `(A, C, D)`. + Etc. + + This is the opposite of `pack`. + }]; + + let arguments = (ins + TFL_TensorOf<[F32, F16, BF16, I1, I8, UI8, I32, QI8, QUI8, I16, QI16]>:$input, + + ConfinedAttr:$num, + I32Attr:$axis + ); + + let results = (outs + TFL_VariadicTensorOf<[F32, F16, BF16, I1, I8, UI8, I32, QI8, QUI8, I16, QI16]>:$outputs + ); + + let extraClassDeclaration = [{ + static bool isCompatibleReturnTypes(TypeRange l, TypeRange r); + }]; + + let hasOptions = 1; +} + +def TFL_ZerosLikeOp: TFL_Op<"zeros_like", [ + Pure, + SameOperandsAndResultType + ]> { + let summary = "ZerosLike operator"; + + let description = [{ + Returns a tensor of zeros with the same shape and type as the input tensor. + }]; + + let arguments = (ins TFL_TensorOf<[I64, I32, F32]>:$input); + + let results = (outs TFL_TensorOf<[I64, I32, F32]>:$output); + + let hasOptions = 1; +} + +def TFL_BatchToSpaceNdOp: TFL_Op<"batch_to_space_nd", [ + Pure, + SameOperandsAndResultsScale, + QuantizableResult, + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + TFL_OperandHasRankRange<0, 3, 4>, + TFL_OperandHasRank<1, 1>, + TFL_OperandHasRank<2, 2> + ]> { + let summary = "BatchToSpaceNd operator"; + + let description = [{ + This operation reshapes the "batch" dimension 0 into space dimensions. + }]; + + let arguments = (ins + TFL_TensorOf<[F32, I8, I32, I64, UI8, QI8, QUI8, QI16]>:$input, + TFL_TensorOf<[I32]>:$block_shape, + TFL_TensorOf<[I32]>:$indices + ); + + let results = (outs + TFL_TensorOf<[F32, I16, I32, I64, UI8, QI8, QUI8, QI16]>:$output + ); +} + +def TFL_SpaceToBatchNdOp: TFL_Op<"space_to_batch_nd", [ + Pure, + QuantizableResult, + SameOperandsAndResultsScale, + TFL_OperandHasRankRange<0, 3, 4>, + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>> + ]> { + let summary = "SpaceToBatchNd operator"; + + let description = [{ + This operation reshapes space dimensions into the "batch" dimension 0 + }]; + + let arguments = (ins + TFL_TensorOf<[F32, I32, I64, QI8, QUI8, TFL_Quint8, QI16]>:$input, + TFL_I32Tensor:$block_shape, + TFL_I32Tensor:$paddings + ); + + let results = (outs + TFL_TensorOf<[F32, I32, I64, QI8, QUI8, TFL_Quint8, QI16]>:$output + ); +} + +def TFL_SpaceToDepthOp: TFL_Op<"space_to_depth", [ + Pure, + QuantizableResult, + SameOperandsAndResultsScale, + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + TFL_OperandHasRankAtMost<0, 4> + ]> { + let summary = "SpaceToDepth operator"; + + let description = [{ + Rearranges blocks of spatial data, into depth. More specifically, + this op outputs a copy of the input tensor where values from the `height` + and `width` dimensions are moved to the `depth` dimension. + `block_size` indicates the input block size. + }]; + + let arguments = (ins + TFL_TensorOf<[F32, I32, I64, QI8, QUI8, TFL_Quint8]>:$input, + ConfinedAttr:$block_size + ); + + let results = (outs + TFL_TensorOf<[F32, I32, I64, QI8, QUI8, TFL_Quint8]>:$output + ); + + let hasOptions = 1; +} + +def TFL_DepthToSpaceOp: TFL_Op<"depth_to_space", [ + Pure, + QuantizableResult, + SameOperandsAndResultsScale, + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + TFL_OperandHasRankAtMost<0, 4> + ]> { + let summary = "DepthToSpace operator"; + + let description = [{ + Rearranges data from depth into blocks of spatial data. + This is the reverse transformation of SpaceToDepth. More specifically, + this op outputs a copy of the input tensor where values from the `depth` + dimension are moved in spatial blocks to the `height` and `width` + dimensions. The attr `block_size` indicates the input block size and how + the data is moved. + }]; + + let arguments = (ins + TFL_TensorOf<[F32, I8, I32, I64, TFL_Quint8, UI8, QI8, QUI8]>:$input, + ConfinedAttr:$block_size + ); + + let results = (outs + TFL_TensorOf<[F32, I8, I32, I64, TFL_Quint8, UI8, QI8, QUI8]>:$output + ); + + let hasOptions = 1; +} + +def TFL_SplitOp : TFL_Op<"split", [ + Pure, + TFL_Operand0DOr1ElementTensor<0>, + QuantizableResult, + SameOperandsAndResultsScale]> { + let summary = "Splits a tensor into `num_split` tensors along one dimension."; + + let description = [{ + Splits the `value` tensor along `split_dim` into a number of sub-tensors + with same shape as the original one, except for `split_dim`. Same as + tf.Split. + }]; + + let arguments = (ins + TFL_TensorOf<[I32]>:$split_dim, + TFL_TensorOf<[F32, I16, I32, I8, UI8, QI8, QUI8, QI16]>:$value, + ConfinedAttr:$num_splits + ); + + let results = (outs + TFL_VariadicTensorOf<[F32, I16, I32, I8, UI8, QI8, QUI8, QI16]>:$outputs + ); + + let hasVerifier = 1; + + let hasOptions = 1; +} + +def TFL_SplitVOp : TFL_Op<"split_v", [ + Pure, + QuantizableResult, + SameOperandsAndResultsScale]> { + let summary = "Splits a tensor into `num_split` tensors along one dimension."; + + let description = [{ + Splits the `value` tensor along `split_dim` into a number of sub-tensors + with same shape as the original one, except for `split_dim`. The grouping + of the resultant sub-tensors is decided by `size-splits`. Same as tf.SplitV. + }]; + + let arguments = (ins + TFL_TensorOf<[F32, I16, I32, I64, I8, UI8, QI8, QUI8, QI16]>:$value, + TFL_1DTensorOf<[I32], [I32]>:$size_splits, + TFL_0DTensorOf<[I32], [I32]>:$split_dim, + ConfinedAttr:$num_splits + ); + + let results = (outs + TFL_VariadicTensorOf<[F32, I16, I32, I64, I8, UI8, QI8, QUI8, QI16]>:$outputs + ); + + let hasVerifier = 1; + + let hasOptions = 1; +} + +def TFL_ResizeBilinearOp: TFL_Op<"resize_bilinear", [ + Pure, + QuantizableResult, + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + TFL_OperandHasRank<0, 4>, + TFL_OperandHasRank<1, 1>, + SameOperandsAndResultsScale]> { + let summary = "ResizeBilinear Op"; + + let description = [{ + Resize `images` to `size` using bilinear interpolation. + }]; + + let arguments = (ins + TFL_TensorOf<[F32, TFL_Quint8, QUI8, QI8, QI16]>:$input, + TFL_I32Tensor:$size, + BoolAttr:$align_corners, + DefaultValuedOptionalAttr:$half_pixel_centers + ); + + let results = (outs + TFL_TensorOf<[F32, TFL_Quint8, QUI8, QI8, QI16]>:$output + ); + + let hasOptions = 1; +} + +def TFL_ResizeNearestNeighborOp : TFL_Op<"resize_nearest_neighbor", [ + Pure, + QuantizableResult, + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + TFL_OperandHasRank<0, 4>, + TFL_OperandHasRank<1, 1>, + SameOperandsAndResultsScale]> { + let summary = "ResizeNearestNeighbor Op"; + + let description = [{ + Resize `images` to `size` using nearest neighbor interpolation. + }]; + + let arguments = (ins + TFL_TensorOf<[F32, TFL_Quint8, QUI8, QI8, QI16]>:$input, + TFL_I32Tensor:$size, + BoolAttr:$align_corners, + DefaultValuedOptionalAttr:$half_pixel_centers + ); + + let results = (outs + TFL_TensorOf<[F32, TFL_Quint8, QUI8, QI8, QI16]>:$output + ); + + let hasOptions = 1; +} + +def TFL_SparseToDenseOp : TFL_Op<"sparse_to_dense", [ + Pure, + QuantizableResult, + PredOpTrait<"sparse_values and dense must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 2>>, + PredOpTrait<"default_value and dense must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 3>>, + TFL_OperandHasRankAtMost<0, 2>, + TFL_OperandHasRankAtMost<1, 1>, + TFL_OperandHasRankAtMost<2, 1>, + PredOpTrait<"the first operand should have a rank <= 2, when its rank is 2 and has static shape, the second dim should be <= 4", + Or<[TFL_OperandIsUnrankedPred<0>, + CPred<"llvm::cast($_op.getOperand(0).getType()).getRank() <= 1">, + CPred<"llvm::cast($_op.getOperand(0).getType()).getRank() == 2 && !llvm::cast($_op.getOperand(0).getType()).hasStaticShape()">, + CPred<"llvm::cast($_op.getOperand(0).getType()).getRank() == 2 && llvm::cast($_op.getOperand(0).getType()).getShape()[1] <= 4">]>>]> { + let summary = "Converts a sparse representation into a dense tensor."; + + let description = [{ +Builds an array `dense` with shape `output_shape` such that + +``` +# If sparse_indices is scalar +dense[i] = (i == sparse_indices ? sparse_values : default_value) + +# If sparse_indices is a vector, then for each i +dense[sparse_indices[i]] = sparse_values[i] + +# If sparse_indices is an n by d matrix, then for each i in [0, n) +dense[sparse_indices[i][0], ..., sparse_indices[i][d-1]] = sparse_values[i] +``` + +All other values in `dense` are set to `default_value`. If `sparse_values` is a +scalar, all sparse indices are set to this single value. + +Indices should be sorted in lexicographic order, and indices must not +contain any repeats. If `validate_indices` is true, these properties +are checked during execution. + }]; + + let arguments = (ins + TFL_I32OrI64Tensor:$sparse_indices, + TFL_I32OrI64Tensor:$output_shape, + TFL_TensorOf<[I32, I64, I8, QI8, UI8, QUI8, TFL_Quint8, F32]>:$sparse_values, + TFL_TensorOf<[I32, I64, I8, QI8, UI8, QUI8, TFL_Quint8, F32]>:$default_value + ); + + let results = (outs + TFL_TensorOf<[I32, I64, I8, QI8, UI8, QUI8, TFL_Quint8, F32]>:$dense + ); +} + +def TFL_StridedSliceOp: TFL_Op<"strided_slice", [ + Pure, + QuantizableResult, + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + SameOperandsAndResultsScale, + TFL_RuntimePredOpTrait<"input (with new_axis) must have rank at most 6", + CPred<"TFL::VerifyStridedSliceOpInputRankConstraints(llvm::cast($_op))">>, + TFL_OperandHasRank<1, 1>, + TFL_OperandHasRank<2, 1>, + TFL_OperandHasRank<3, 1> + ]> { + let summary = "StridedSlice Op"; + + let description = [{ + Return a strided slice from `input`. + }]; + + let arguments = (ins + TFL_TensorOf<[F32, I32, I64, I8, UI8, UI32, QI8, QUI8, I1, I16, QI16, TFL_Quint8, TFL_Str]>:$input, + TFL_I32Tensor:$begin, + TFL_I32Tensor:$end, + TFL_I32Tensor:$strides, + + I32Attr:$begin_mask, + I32Attr:$end_mask, + I32Attr:$ellipsis_mask, + I32Attr:$new_axis_mask, + I32Attr:$shrink_axis_mask, + BoolAttr:$offset + ); + + let results = (outs + TFL_TensorOf<[F32, I32, I64, I8, UI8, UI32, QI8, QUI8, I1, I16, QI16, TFL_Quint8, TFL_Str]>:$output + ); + + // TFLite kernel only supports up to 5D input including added axis. + let hasVerifier = 1; + + let hasOptions = 1; + + let hasFolder = 1; +} + +// If there is a change in supporting more types in the TFLite cast op kernel, +// the While loop outline pass should be updated since it inserts cast op(s) +// after the TF -> TFL legalization pass is done. +// LINT.IfChange +def TFL_CastOp : TFL_Op<"cast", [ + Pure, + SameOperandsAndResultShape]> { + let summary = "Cast operator"; + + let description = [{ + Casts input from input type to output type. + }]; + + let arguments = (ins + TFL_TensorOf<[F16, BF16, F32, F64, I1, TFL_I2, TFL_I4, UI<4>, I16, UI16, I32, UI32, I64, TFL_Quint8, UI8, I8, Complex>]>:$input + ); + + let results = (outs TFL_TensorOf<[F16, BF16, F32, F64, I1, TFL_I2, TFL_I4, UI<4>, I16, UI16, I32, UI32, I64, TFL_Quint8, UI8, I8, Complex>]>:$output); + + // TFLite's cast op does not utilize CastOptions, instead derives types + // from the TfLiteTensors. + let hasOptions = 0; + + let hasFolder = 1; +} +// LINT.ThenChange(//tensorflow/compiler/mlir/lite/transforms/tf_legalizations/while_loop_outline_pass.cc) + +def TFL_MirrorPadOp: TFL_Op<"mirror_pad", [ + SameOperandsAndResultsScale, + QuantizableResult, + Pure, + TFL_OperandHasRank<1, 2>]> { + let summary = "MirrorPad Operator. Pads a tensor with mirrored values."; + + let description = [{ + This operation pads a input with mirrored values according to the paddings + you specify. paddings is an integer tensor with shape [n, 2], + where n is the rank of input. + For each dimension D of input, paddings[D, 0] indicates how many values + to add before the contents of input in that dimension, + and paddings[D, 1] indicates how many values to add after the contents of + input in that dimension. + + Both paddings[D, 0] and paddings[D, 1] must be no greater than + input.dim_size(D) (or input.dim_size(D) - 1) + if copy_border is true (if false, respectively). + + The padded size of each dimension D of the output is: + + paddings(D, 0) + input.dim_size(D) + paddings(D, 1) + }]; + + let arguments = (ins + TFL_TensorOf<[F32, I32, I64, I8, UI8, QI8, QUI8, QI16]>:$input, + TFL_TensorOf<[I32, I64]>:$pad, + TFL_MirrorPaddingAttr:$mode + ); + + let results = (outs + TFL_TensorOf<[F32, I32, I64, I8, UI8, QI8, QUI8, QI16]>:$output + ); + + let hasOptions = 1; +} + +def TFL_UniqueOp: TFL_Op<"unique", [ + TFL_OperandHasRank<0, 1>, + QuantizableResult, + Pure]> { + let summary = "Unique Op."; + + let description = [{ + This operation returns a tensor `output` containing all of the unique elements +of `input` sorted in the same order that they occur in `input`. This operation +also returns a tensor `idx` the same size as `x` that contains the index of each +value of `input` in the unique output `output`. In other words: + }]; + + let arguments = (ins + TFL_TensorOf<[I8, QI8, UI8, QUI8, I16, QI16, I32, I64, F32]>:$input + ); + + let results = (outs + TFL_TensorOf<[I8, QI8, UI8, QUI8, I16, QI16, I32, I64, F32]>:$output, + TFL_I32OrI64Tensor:$idx + ); + + DerivedTFLiteTypeAttr idx_out_type = DerivedTFLiteTypeAttr<[{ + return llvm::cast(llvm::cast( + getResult(1).getType()).getElementType()).getWidth() > 32 ? + tflite::TensorType_INT64 : + tflite::TensorType_INT32; + }], [{ + TypeAttr::get(llvm::cast(getResult(1).getType()).getElementType()) + }]>; + + let hasOptions = 1; +} + +def TFL_GeluOp: TFL_Op<"gelu", [ + Pure, + SameOperandsAndResultShape, + QuantizableResult, + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>]> { + let summary = "GELU activation function."; + let description = [{ + Computes GELU activation function element-wise. + }]; + + let arguments = (ins + TFL_TensorOf<[F32, F16, QI8, QUI8]>:$input, + DefaultValuedOptionalAttr:$approximate + ); + + let results = (outs TFL_TensorOf<[F32, F16, QI8, QUI8]>:$output); + + let hasOptions = 1; +} + +def TFL_DynamicUpdateSliceOp: TFL_Op<"dynamic_update_slice", [ + Pure, + QuantizableResult, + SameOperandsAndResultsScale, + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>]> { + let summary = "DynamicUpdateSlice."; + let description = [{ + DynamicUpdateSlice op that have the same semantics with XLA + DynamicUpdateSlice. + Generates a result which is the value of the input array + operand, with a slice update overwritten at start_indices. + + See https://www.tensorflow.org/xla/operation_semantics#dynamicupdateslice. + }]; + + let arguments = (ins + TFL_TensorOf<[I1, TFL_I4, QI4, QI8, I8, I16, I32, I64, F32, F16, BF16]>:$operand, + TFL_TensorOf<[I1, TFL_I4, QI4, QI8, I8, I16, I32, I64, F32, F16, BF16]>:$update, + TFL_I32OrI64Tensor:$start_indices + ); + + let results = ( + outs TFL_TensorOf<[I1, TFL_I4, QI4, QI8, I8, I16, I32, I64, F32, F16, BF16]>:$output); + + let extraClassDeclaration = [{ + // Quantized axes are verified in the Verify function. + bool RequiredSameQuantizedAxes() { return false; } + }]; + + let hasFolder = 1; +} + +def TFL_BitcastOp : TFL_Op<"bitcast", [Pure]> { + let summary = "Bitcast operator"; + + let description = [{ + Bitcasts a tensor from one type to another. + }]; + + let arguments = (ins AnyTensor:$input); + + let results = (outs AnyTensor:$output); + + // TFLite's bitcast bitop does not utilize options, instead derives types + // from the TfLiteTensors. + let hasOptions = 0; + + let hasFolder = 1; + + let hasVerifier = 1; +} + +def TFL_BitwiseXorOp : TFL_Op<"bitwise_xor", [ + ResultsBroadcastableShape, + Commutative, + SameOperandsAndResultElementType, + Pure]> { + let summary = "Bitwise Xor operator"; + + let description = [{ + Elementwise computes the bitwise XOR of `lhs` and `rhs`. + }]; + + let arguments = (ins + TFL_TensorOf<[I8, UI8, I16, UI16, I32, UI32]>:$lhs, + TFL_TensorOf<[I8, UI8, I16, UI16, I32, UI32]>:$rhs + ); + + let results = (outs + TFL_TensorOf<[I8, UI8, I16, UI16, I32, UI32]>:$output + ); + + let hasFolder = 1; +} + +def TFL_RightShiftOp : TFL_Op<"right_shift", [ + SameOperandsAndResultElementType, + Pure]> { + let summary = "Right Shift operator"; + + let description = [{ + Elementwise computes the bitwise right-shift of `lhs` by `rhs`. + }]; + + let arguments = (ins + TFL_TensorOf<[I8, UI8, I16, UI16, I32, UI32]>:$lhs, + TFL_TensorOf<[I8, UI8, I16, UI16, I32, UI32]>:$rhs + ); + + let results = (outs + TFL_TensorOf<[I8, UI8, I16, UI16, I32, UI32]>:$output + ); +} + +//===----------------------------------------------------------------------===// +// Quantization ops. +//===----------------------------------------------------------------------===// + +def TFL_DequantizeOp: TFL_Op<"dequantize", [NoMemoryEffect]> { + let summary = "Dequantize operator"; + + let description = [{ + Converts quantized array of integers to floating-points according to the + quantization parameters. + }]; + + let arguments = (ins TFL_TensorOf<[QI2, QI4, QUI4, QI8, QUI8, QI16, F16]>:$input); + + let results = (outs TFL_FpTensor:$output); + + let builders = [ + OpBuilder<(ins "Value":$input), + [{ + $_state.addOperands({input}); + $_state.addTypes(mlir::cast(input.getType()).hasRank() ? + static_cast(RankedTensorType::get( + mlir::cast(input.getType()).getShape(), + $_builder.getF32Type())) : + static_cast( + UnrankedTensorType::get($_builder.getF32Type()))); + }]> + ]; +} + +def TFL_FakeQuantOp : TFL_Op<"fake_quant", [ + Pure, + QuantizableResult]> { + let summary = "FakeQuant operator"; + + let description = [{ + Fake-quantize the 'inputs' tensor of type float via float scalars min and + max to 'outputs' tensor of same shape as inputs. + }]; + + let arguments = ( + ins TFL_FpTensor:$input, + // The expected [min, max] range of values. + F32Attr:$min, + F32Attr:$max, + + // The bitwidth of the quantization; between 2 and 16, inclusive. + ConfinedAttr, IntMaxValue<16>]>:$num_bits, + // Quantization range starts from 0 or 1; starts from 1 if true. + ConfinedAttr:$narrow_range); + + let results = (outs TFL_FpTensor:$output); + + let hasCanonicalizer = 0b1; + + let hasOptions = 1; +} + +// TODO(b/200841823): consider adding TFL_RuntimeVerification trait. +def TFL_QConstOp : Op { + let summary = "Quantized constant pseudo op"; + + let description = [{ + Represents a quantized constant value in TensorFlow Lite dialect. This is + not an actual operation and it will be lowered to buffer instead. The + quantization parameters are stored as a type attribute in this constant. + }]; + + let arguments = ( + ins TensorTypeAttr:$qtype, + ElementsAttr:$value + ); + + let results = (outs TFL_TensorOf<[QUI8, QI8, QUI4, QI16, QUI16, TFL_Quint8]>:$output); + + let builders = [ + OpBuilder<(ins "TypeAttr":$qtype, "Attribute":$value), + [{ + $_state.addAttribute("qtype", qtype); + $_state.addAttribute("value", value); + $_state.addTypes(qtype.getValue()); + }]> + ]; +} + +def TFL_SparseQConstOp : Op]> { + let summary = "Sparse quantized constant pseudo op"; + + let description = [{ + Represents a sparse quantized constant value in TensorFlow Lite dialect. + This is not an actual operation and it will be lowered to buffer instead. + The quantization parameters are stored as a type attribute in this constant. + }]; + + let arguments = ( + ins TensorTypeAttr:$qtype, + ElementsAttr:$value, + SparsityParameterAttr:$s_param, + ElementsAttr:$compressed_data + ); + + let results = (outs TFL_TensorOf<[QUI8, QI8, QUI4, QI16, QUI16, TFL_Quint8]>:$output); + + let builders = [ + OpBuilder<(ins "TypeAttr":$qtype, "Attribute":$value, + "SparsityParameterAttr":$s_param, "Attribute":$compressed_data), + [{ + $_state.addTypes(qtype.getValue()); + $_state.addAttribute("qtype", qtype); + $_state.addAttribute("value", value); + $_state.addAttribute("s_param", s_param); + $_state.addAttribute("compressed_data", compressed_data); + }]> + ]; +} + +def TFL_QuantizeOp: TFL_Op<"quantize", [ + FirstAttrDerivedResultType, + SameOperandsAndResultShape, NoMemoryEffect]> { + let summary = "Quantize operator"; + + let description = [{ + Converts floating point tensors to quantized integer tensors according to + the quantization parameters defined in the type attribute. + }]; + + let arguments = ( + ins TFL_TensorOf<[F32, QI4, QUI4, QI8, QUI8, QI16, TFL_Quint8]>:$input, + TensorTypeAttr:$qtype + ); + + let results = (outs TFL_TensorOf<[QI4, QUI4, QI8, QUI8, QI16, TFL_Quint8]>:$output); +} + +def TFL_DensifyOp: TFL_Op<"densify", [ + Pure, + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>]> { + let summary = "Densify operator"; + + let description = [{ + Converts sparse tensor to dense format. + }]; + + let arguments = (ins TFL_TensorOf<[F32, I8]>:$input); + + let results = (outs TFL_TensorOf<[F32, I8]>:$output); +} + +//===----------------------------------------------------------------------===// +// LSTM Ops +//===----------------------------------------------------------------------===// + +def LstmMandatoryInputsConstraint : PredOpTrait< + "mandatory operands element types should match", + // TODO(ashwinm): Replace the indices with input tensor names when that + // support is available. + Or<[ + TCopVTEtAreSameAt<[0, 2, 3, 4, 6, 7, 8, 13, 14, 15, 18, 19]>, + Neg>]>>; + +def LstmOptionalPeepholeWeightConstraint : PredOpTrait< + "the optional peephole weights should all be specified or none", + // Ignore input 9 (cell_to_input_weights) for LSTM with CIFG. + And<[ + TFL_TCopVTEtAreSameAt<10, 11, 16>, + Or<[TFL_TCopVTEtAreSameAt<9, 10, 16>, + And<[TypeIsPred<"input_to_input_weights", NoneType>, + TypeIsPred<"cell_to_input_weights", NoneType>]>]>]>>; + +def LstmProjectionWeightBiasConstraint : PredOpTrait< + "either projection weight must be specified or both projection weight and " + "projection bias must not be specified", + Or<[ + And<[TypeIsPred<"projection_weights", NoneType>, + TypeIsPred<"projection_bias", NoneType>]>, + Neg>]>>; + +def LstmCifgInputConstraint : PredOpTrait< + "the cifg inputs should all be specified or none", + // If LSTM has combined input/forget gate, input 1, 5, 9, 12, 20 are all none + // or 1, 5, 12 should not be none. Inputs 9 and 20 depend on LSTM's variants. + Or<[ + And<[TypeIsPred<"input_to_input_weights", NoneType>, + TypeIsPred<"recurrent_to_input_weights", NoneType>, + TypeIsPred<"cell_to_input_weights", NoneType>, + TypeIsPred<"input_gate_bias", NoneType>, + TypeIsPred<"input_layer_norm_coefficients", NoneType>]>, + Neg, + TypeIsPred<"recurrent_to_input_weights", NoneType>, + TypeIsPred<"input_gate_bias", NoneType>]>>]>>; + + +// TODO(b/137798843): Need to add an additional constraint for both LSTM and +// UnidirectionalSequenceLstm +// For layer norm: if layer norm is false, tensor {20, 21, 22, 23} +// are null; if layer norm is true, tensors {21, 22, 23} are not null; tensor +// {20} is not null if additionally cifg = false. + +def LstmResultConstraint : PredOpTrait< + "the input and result tensor elemental types must be same", + TFL_TCresVTEtIsSameAsOp<0, 0>>; + +// This is the basic kernel type LSTM op. +// TODO(b/142417845): Refactor this part to return its tflite node name as +// "lstm". +def TFL_BasicLSTMOp : TFL_Op<"basic_lstm", [Pure, + TFL_OperandHasRank<0, 2>, + TFL_OperandHasRank<1, 2>, + TFL_OperandHasRank<2, 2>, + TFL_OperandHasRank<3, 1>, + TFL_OperandHasRank<4, 2>, + QuantizableResult]> { + let summary = "The basic lstm operator"; + + let description = [{ + basic LSTM Cell Operator. + }]; + + let arguments = ( + ins TFL_TensorOf<[F32, QUI8]>:$data_input, + TFL_TensorOf<[F32, QUI8]>:$prev_activ_input, + TFL_TensorOf<[F32, QUI8]>:$weights_input, + TFL_TensorOf<[F32, QI32]>:$biases_input, + TFL_TensorOf<[F32, QI16]>:$prev_state_input, + + // Attributes + DefaultValuedStrAttr:$fused_activation_function, + ConfinedAttr, [TFL_FloatNonNegative]>:$cell_clip, + ConfinedAttr, [TFL_FloatNonNegative]>:$proj_clip, + // Since this op is the BASIC kernel only, constrain it. + ConfinedAttr< + DefaultValuedOptionalAttr, + [TFL_LSTMKernelTypeEqualsTo<"mlir::TFL::LSTMKernelType::BASIC">]>:$kernel_type + ); + + let hasOptions = 1; + + let results = (outs TFL_2DTensorOf<[F32, QUI8]>:$activ_output, + TFL_2DTensorOf<[F32, QUI16]>:$state_output, + TFL_2DTensorOf<[F32, QUI8]>:$concat_temp, + TFL_2DTensorOf<[F32, QUI16]>:$activ_temp); +} + +// This is the FULL kernel type LSTM op. +def TFL_LSTMOp : + TFL_Op<"lstm", + [LstmMandatoryInputsConstraint, + LstmOptionalPeepholeWeightConstraint, + LstmProjectionWeightBiasConstraint, + LstmCifgInputConstraint, + LstmResultConstraint, + TFL_OperandHasRank<2, 2>, // input_to_forget_weights + TFL_OperandHasRank<3, 2>, // input_to_cell_weights + TFL_OperandIsNoneOrHasRank<5, 2>, // recurrent_to_input_weights + TFL_OperandHasRank<6, 2>, // recurrent_to_forget_weights + TFL_OperandHasRank<7, 2>, // recurrent_to_cell_weights + TFL_OperandIsNoneOrHasRank<9, 1>, // cell_to_input_weights + TFL_OperandIsNoneOrHasRank<10, 1>, // cell_to_forget_weights + TFL_OperandIsNoneOrHasRank<11, 1>, // cell_to_output_weights + TFL_OperandHasRank<13, 1>, // forget_gate_bias + TFL_OperandHasRank<14, 1>, // cell_gate_bias + TFL_OperandHasRank<15, 1>, // output_gate_bias + TFL_OperandIsNoneOrHasRank<16, 2>, // projection_weights + TFL_OperandIsNoneOrHasRank<17, 1>, // projection_bias + TFL_StatefulOp, + QuantizableResult, + DynamicRangeQuantizedOpInterface]> { + let summary = "The full lstm operator"; + + let description = [{ +Long short-term memory unit (LSTM) recurrent network layer. +The default non-peephole implementation is based on: +http://deeplearning.cs.cmu.edu/pdfs/Hochreiter97_lstm.pdf +S. Hochreiter and J. Schmidhuber. 'Long Short-Term Memory'. Neural Computation, +9(8):1735-1780, 1997. +The peephole implementation is based on: +https://research.google.com/pubs/archive/43905.pdf +Hasim Sak, Andrew Senior, and Francoise Beaufays. 'Long short-term memory +recurrent neural network architectures for large scale acoustic modeling.' +INTERSPEECH, 2014. +The coupling of input and forget gate (CIFG) is based on: +http://arxiv.org/pdf/1503.04069.pdf +Greff et al. 'LSTM: A Search Space Odyssey' +The layer normalization is based on: +https://arxiv.org/pdf/1607.06450.pdf +Ba et al. 'Layer Normalization' + }]; + + let arguments = ( + ins TFL_TensorOf<[F32, QI8, QI16]>:$input, + + // Weights + TFL_TensorOfOrNone<[F32, QI8]>:$input_to_input_weights, + TFL_TensorOf<[F32, QI8]>:$input_to_forget_weights, + TFL_TensorOf<[F32, QI8]>:$input_to_cell_weights, + TFL_TensorOf<[F32, QI8]>:$input_to_output_weights, + + // Recurrent weights + TFL_TensorOfOrNone<[F32, QI8]>:$recurrent_to_input_weights, + TFL_TensorOf<[F32, QI8]>:$recurrent_to_forget_weights, + TFL_TensorOf<[F32, QI8]>:$recurrent_to_cell_weights, + TFL_TensorOf<[F32, QI8]>:$recurrent_to_output_weights, + + // Cell weights + TFL_TensorOfOrNone<[F32, QI8, QI16]>:$cell_to_input_weights, + // Optional input + TFL_TensorOfOrNone<[F32, QI8, QI16]>:$cell_to_forget_weights, + // Optional input + TFL_TensorOfOrNone<[F32, QI8, QI16]>:$cell_to_output_weights, + + // Bias + TFL_TensorOfOrNone<[F32, QI32]>:$input_gate_bias, + TFL_TensorOf<[F32, QI32]>:$forget_gate_bias, + TFL_TensorOf<[F32, QI32]>:$cell_bias, + TFL_TensorOf<[F32, QI32]>:$output_gate_bias, + + // Projection weight and bias + TFL_TensorOfOrNone<[F32, QI8]>:$projection_weights, + // Optional input + TFL_TensorOfOrNone<[F32, QI32]>:$projection_bias, + + // Stateful activation and cell states. + TFL_StatefulTensor:$input_activation_state, + TFL_StatefulTensor:$input_cell_state, + + // Layer norm coefficients + TFL_TensorOfOrNone<[F32, QI16]>:$input_layer_norm_coefficients, + TFL_TensorOfOrNone<[F32, QI16]>:$forget_layer_norm_coefficients, + TFL_TensorOfOrNone<[F32, QI16]>:$cell_layer_norm_coefficients, + TFL_TensorOfOrNone<[F32, QI16]>:$output_layer_norm_coefficients, + + // Attributes + TFL_AFAttr:$fused_activation_function, + ConfinedAttr, [TFL_FloatNonNegative]>:$cell_clip, + ConfinedAttr, [TFL_FloatNonNegative]>:$proj_clip, + // Since this op is the FULL kernel only, constrain it. + ConfinedAttr< + DefaultValuedOptionalAttr, + [TFL_LSTMKernelTypeEqualsTo<"mlir::TFL::LSTMKernelType::FULL">]>:$kernel_type, + // Used in post-training dynamic range quantization. If the value is true, + // input activations are asymmetrically quantized. + OptionalAttr:$asymmetric_quantize_inputs, + + // Types of the optional intermediate tensors, which exist for fully + // quantized LSTM op and hold the ranges of the intermediate tensors. + // The type for intermediate tensors are be quant.calibrated when imported + // to only store calibrated min, max values. The proper quantization spec is + // determined while going through quantization passes. + OptionalAttr:$input_to_input_intermediate, + OptionalAttr:$input_to_forget_intermediate, + OptionalAttr:$input_to_cell_intermediate, + OptionalAttr:$input_to_output_intermediate, + OptionalAttr:$effective_hidden_scale_intermediate + ); + + let results = (outs AnyTensor:$output); + + // TODO(fengliuai): customize printer and parser to not display + // empty region. + let regions = (region AnyRegion:$internal); + + let hasOptions = 1; + + let hasCanonicalizer = 1; + + let hasVerifier = 1; + + let extraClassDeclaration = [{ + // StatefulOpInterface: + std::vector GetStatefulOperands() { return {18, 19}; } + // DynamicRangeQuantizedOpInterface: + bool RequireAsymmetricQuantizeInputsAttr() { return true; } + bool GetDynamicRangeQuantKernelSupport() { return true; } + std::vector GetQuantizableOperandIndices() { + return {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 16}; + } + }]; +} + +// UnidirectionalSequenceLstm op. +// TODO(ashwinm): Add constraint to validate the combination of operands +// that are valid for hybrid vs fully quantized vs float only semantics +def TFL_UnidirectionalSequenceLSTMOp : + TFL_Op<"unidirectional_sequence_lstm", + [LstmMandatoryInputsConstraint, + LstmOptionalPeepholeWeightConstraint, + LstmProjectionWeightBiasConstraint, + LstmCifgInputConstraint, + LstmResultConstraint, + TFL_OperandHasRankAtLeast<0, 2>, // input + TFL_OperandIsNoneOrHasRank<1, 2>, // input_to_input_weights + TFL_OperandHasRank<2, 2>, // input_to_forget_weights + TFL_OperandHasRank<3, 2>, // input_to_cell_weights + TFL_OperandHasRank<4, 2>, // input_to_output_weights + TFL_OperandIsNoneOrHasRankAtMost<5, 2>,// recurrent_to_input_weights + TFL_OperandHasRankAtMost<6, 2>, // recurrent_to_forget_weights + TFL_OperandHasRankAtMost<7, 2>, // recurrent_to_cell_weights + TFL_OperandHasRankAtMost<8, 2>, // recurrent_to_output_weights + TFL_OperandIsNoneOrHasRank<9, 1>, // cell_to_input_weights + TFL_OperandIsNoneOrHasRank<10, 1>, // cell_to_forget_weights + TFL_OperandIsNoneOrHasRank<11, 1>, // cell_to_output_weights + TFL_OperandIsNoneOrHasRank<12, 1>, // input_gate_bias + TFL_OperandHasRank<13, 1>, // forget_gate_bias + TFL_OperandHasRank<14, 1>, // cell_gate_bias + TFL_OperandHasRank<15, 1>, // output_gate_bias + TFL_OperandIsNoneOrHasRank<16, 2>, // projection_weights + TFL_OperandIsNoneOrHasRank<17, 1>, // projection_bias + TFL_StatefulOp, + DeclareOpInterfaceMethods, + QuantizableResult, + DynamicRangeQuantizedOpInterface + ]> { + let summary = "Unidirectional sequence lstm operator"; + + let description = [{ + A recurrent neural network specified by an LSTM cell. This Op supports + unrolling the input along the time or batch dimensions, and + implements the following operation for + each element in the sequence s = 1...sequence_length: + outputs[s] = state = activation(LSTMOp(inputs[s])) + + where LSTMOp is LSTM TF Lite Op and the “activation” is the function passed + as the “fused_activation_function” argument (if not “NONE”). + }]; + + let arguments = ( + ins TFL_FpTensor:$input, + + // Weights + TFL_TensorOfOrNone<[F32, QI8]>:$input_to_input_weights, + TFL_TensorOf<[F32, QI8]>:$input_to_forget_weights, + TFL_TensorOf<[F32, QI8]>:$input_to_cell_weights, + TFL_TensorOf<[F32, QI8]>:$input_to_output_weights, + + // Recurrent weights + TFL_TensorOfOrNone<[F32, QI8]>:$recurrent_to_input_weights, + TFL_TensorOf<[F32, QI8]>:$recurrent_to_forget_weights, + TFL_TensorOf<[F32, QI8]>:$recurrent_to_cell_weights, + TFL_TensorOf<[F32, QI8]>:$recurrent_to_output_weights, + + // Cell weights + TFL_TensorOfOrNone<[F32, QI8]>:$cell_to_input_weights, + // Optional input + TFL_TensorOfOrNone<[F32, QI8]>:$cell_to_forget_weights, + // Optional input + TFL_TensorOfOrNone<[F32, QI8]>:$cell_to_output_weights, + + // Bias + TFL_TensorOfOrNone<[F32]>:$input_gate_bias, + TFL_FpTensor:$forget_gate_bias, + TFL_FpTensor:$cell_bias, + TFL_FpTensor:$output_gate_bias, + + // Projection weight and bias + TFL_TensorOfOrNone<[F32, QI8]>:$projection_weights, + // Optional input + TFL_TensorOfOrNone<[F32]>:$projection_bias, + + // Stateful activation and cell states. + TFL_StatefulTensor:$input_activation_state, + TFL_StatefulTensor:$input_cell_state, + + // Layer norm coefficients + TFL_TensorOfOrNone<[F32, QI8]>:$input_layer_norm_coefficients, + TFL_TensorOfOrNone<[F32, QI8]>:$forget_layer_norm_coefficients, + TFL_TensorOfOrNone<[F32, QI8]>:$cell_layer_norm_coefficients, + TFL_TensorOfOrNone<[F32, QI8]>:$output_layer_norm_coefficients, + + // Attributes + TFL_AFAttr:$fused_activation_function, + ConfinedAttr, [TFL_FloatNonNegative]>:$cell_clip, + ConfinedAttr, [TFL_FloatNonNegative]>:$proj_clip, + BoolAttr:$time_major, + // Used in post-training dynamic range quantization. If the value is true, + // input activations are asymmetrically quantized. + OptionalAttr:$asymmetric_quantize_inputs, + // IndyLSTM optimizations (i.e. optimizations enabled because of diagonal + // recurrent weight matrices that are provided as vectors) + OptionalAttr:$diagonal_recurrent_tensors, + + // Types of the optional intermediate tensors, which exist for fully + // quantized op and hold the ranges of the intermediate tensors. + // The type for intermediate tensors are be quant.calibrated when imported + // to only store calibrated min, max values. The proper quantization spec is + // determined while going through quantization passes. + OptionalAttr:$input_to_input_intermediate, + OptionalAttr:$input_to_forget_intermediate, + OptionalAttr:$input_to_cell_intermediate, + OptionalAttr:$input_to_output_intermediate, + OptionalAttr:$effective_hidden_scale_intermediate + ); + + let results = (outs TFL_TensorOf<[F32, QI8]>:$output); + + let hasOptions = 1; + + let hasVerifier = 1; + + let extraClassDeclaration = [{ + // StatefulOpInterface: + std::vector GetStatefulOperands() { return {18, 19}; } + + // Compatiable return types check + static bool isCompatibleReturnTypes(TypeRange l, TypeRange r); + + // DynamicRangeQuantizedOpInterface: + bool RequireAsymmetricQuantizeInputsAttr() { return true; } + bool GetDynamicRangeQuantKernelSupport() { return true; } + std::vector GetQuantizableOperandIndices() { + return {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 16}; + } + }]; +} + +def BidiLstmMandatoryInputsConstraint : PredOpTrait< + "mandatory operands element types should match", + // TODO(ashwinm): Replace the indices with input tensor names when that + // support is available. + Or<[ + TCopVTEtAreSameAt<[0, 2, 3, 4, 6, 7, 8, 13, 14, 15, 19, 20, 21, 23, 24, 25, + 30, 31, 32, 35, 36, 37, 38]>, + Neg>]>>; + +// TODO(b/172517537): support quantized types +def BidiLstmOptionalPeepholeWeightConstraint : PredOpTrait< + "the optional peephole weights should all be specified or none", + TCopVTEtAreSameAt<[9, 10, 11, 26, 27, 28]>>; + +def BidiLstmProjectionWeightBiasConstraint : PredOpTrait< + "either projection weight must be specified or both projection weight and " + "projection bias must not be specified", + Or<[ + And<[TypeIsPred<"fw_projection_weights", NoneType>, + TypeIsPred<"fw_projection_bias", NoneType>, + TypeIsPred<"bw_projection_weights", NoneType>, + TypeIsPred<"bw_projection_bias", NoneType>]>, + And<[ + Neg>, + Neg>, + ]> + ]>>; + +// BidirectionalSequenceLstm op. +// TODO(ashwinm): Add constraint to validate the combination of operands +// that are valid for hybrid vs fully quantized vs float only semantics +def TFL_BidirectionalSequenceLSTMOp : + TFL_Op<"bidirectional_sequence_lstm", + [BidiLstmMandatoryInputsConstraint, + BidiLstmOptionalPeepholeWeightConstraint, + BidiLstmProjectionWeightBiasConstraint, + LstmResultConstraint, + TFL_OperandHasRank<0, 3>, // input + TFL_OperandHasRank<1, 2>, // fw_input_to_input_weights + TFL_OperandHasRank<2, 2>, // fw_input_to_forget_weights + TFL_OperandHasRank<3, 2>, // fw_input_to_cell_weights + TFL_OperandHasRank<4, 2>, // fw_input_to_output_weights + TFL_OperandHasRank<5, 2>, // fw_recurrent_to_input_weights + TFL_OperandHasRank<6, 2>, // fw_recurrent_to_forget_weights + TFL_OperandHasRank<7, 2>, // fw_recurrent_to_cell_weights + TFL_OperandHasRank<8, 2>, // fw_recurrent_to_output_weights + TFL_OperandHasRank<9, 1>, // fw_cell_to_input_weights + TFL_OperandHasRank<10, 1>, // fw_cell_to_forget_weights + TFL_OperandHasRank<11, 1>, // fw_cell_to_output_weights + TFL_OperandHasRank<12, 1>, // fw_input_gate_bias + TFL_OperandHasRank<13, 1>, // fw_forget_gate_bias + TFL_OperandHasRank<14, 1>, // fw_cell_bias + TFL_OperandHasRank<15, 1>, // fw_output_gate_bias + TFL_OperandHasRank<16, 2>, // fw_projection_weights + TFL_OperandHasRank<17, 1>, // fw_projection_bias + TFL_OperandHasRank<18, 2>, // bw_input_to_input_weights + TFL_OperandHasRank<19, 2>, // bw_input_to_forget_weights + TFL_OperandHasRank<20, 2>, // bw_input_to_cell_weights + TFL_OperandHasRank<21, 2>, // bw_input_to_output_weights + TFL_OperandHasRank<22, 2>, // bw_recurrent_to_input_weights + TFL_OperandHasRank<23, 2>, // bw_recurrent_to_forget_weights + TFL_OperandHasRank<24, 2>, // bw_recurrent_to_cell_weights + TFL_OperandHasRank<25, 2>, // bw_recurrent_to_output_weights + TFL_OperandHasRank<26, 1>, // bw_cell_to_input_weights + TFL_OperandHasRank<27, 1>, // bw_cell_to_forget_weights + TFL_OperandHasRank<28, 1>, // bw_cell_to_output_weights + TFL_OperandHasRank<29, 1>, // bw_input_gate_bias + TFL_OperandHasRank<30, 1>, // bw_forget_gate_bias + TFL_OperandHasRank<31, 1>, // bw_cell_bias + TFL_OperandHasRank<32, 1>, // bw_output_gate_bias + TFL_OperandHasRank<33, 2>, // bw_projection_weights + TFL_OperandHasRank<34, 1>, // bw_projection_bias + TFL_StatefulOp, + QuantizableResult, + DynamicRangeQuantizedOpInterface]> { + let summary = "Bidirectional sequence lstm operator"; + + let description = [{ + Bidirectional lstm is essentially two lstms, one running forward & the + other running backward. And the output is the concatenation of the two + lstms. + }]; + + let arguments = ( + ins TFL_TensorOf<[F32, I8]>:$input, + + // Forward LSTM Weights + TFL_TensorOfOrNone<[F32, I8]>:$fw_input_to_input_weights, + TFL_TensorOf<[F32, I8]>:$fw_input_to_forget_weights, + TFL_TensorOf<[F32, I8]>:$fw_input_to_cell_weights, + TFL_TensorOf<[F32, I8]>:$fw_input_to_output_weights, + + // Forward Recurrent weights + TFL_TensorOfOrNone<[F32, I8]>:$fw_recurrent_to_input_weights, + TFL_TensorOf<[F32, I8]>:$fw_recurrent_to_forget_weights, + TFL_TensorOf<[F32, I8]>:$fw_recurrent_to_cell_weights, + TFL_TensorOf<[F32, I8]>:$fw_recurrent_to_output_weights, + + // Forward Cell weights + TFL_TensorOfOrNone<[F32, I8]>:$fw_cell_to_input_weights, + // Optional Forward cell weights + TFL_TensorOfOrNone<[F32, I8]>:$fw_cell_to_forget_weights, + // Optional Forward cell weights + TFL_TensorOfOrNone<[F32, I8]>:$fw_cell_to_output_weights, + + // Forward Bias + TFL_TensorOfOrNone<[F32]>:$fw_input_gate_bias, + TFL_TensorOf<[F32]>:$fw_forget_gate_bias, + TFL_TensorOf<[F32]>:$fw_cell_bias, + TFL_TensorOf<[F32]>:$fw_output_gate_bias, + + // Forward Projection weight and bias + TFL_TensorOfOrNone<[F32, I8]>:$fw_projection_weights, + // Forward Optional input + TFL_TensorOfOrNone<[F32]>:$fw_projection_bias, + + // Backward LSTM Weights + TFL_TensorOfOrNone<[F32, I8]>:$bw_input_to_input_weights, + TFL_TensorOf<[F32, I8]>:$bw_input_to_forget_weights, + TFL_TensorOf<[F32, I8]>:$bw_input_to_cell_weights, + TFL_TensorOf<[F32, I8]>:$bw_input_to_output_weights, + + // Backward Recurrent weights + TFL_TensorOfOrNone<[F32, I8]>:$bw_recurrent_to_input_weights, + TFL_TensorOf<[F32, I8]>:$bw_recurrent_to_forget_weights, + TFL_TensorOf<[F32, I8]>:$bw_recurrent_to_cell_weights, + TFL_TensorOf<[F32, I8]>:$bw_recurrent_to_output_weights, + + // Backward Cell weights + TFL_TensorOfOrNone<[F32, I8]>:$bw_cell_to_input_weights, + // Optional Forward cell weights + TFL_TensorOfOrNone<[F32, I8]>:$bw_cell_to_forget_weights, + // Optional Forward cell weights + TFL_TensorOfOrNone<[F32, I8]>:$bw_cell_to_output_weights, + + // Backward Bias + TFL_TensorOfOrNone<[F32]>:$bw_input_gate_bias, + TFL_TensorOf<[F32]>:$bw_forget_gate_bias, + TFL_TensorOf<[F32]>:$bw_cell_bias, + TFL_TensorOf<[F32]>:$bw_output_gate_bias, + + // Backward Projection weight and bias + TFL_TensorOfOrNone<[F32, I8]>:$bw_projection_weights, + // Backward Optional input + TFL_TensorOfOrNone<[F32]>:$bw_projection_bias, + + // Stateful activation and cell states. + TFL_StatefulTensor:$fw_input_activation_state, + TFL_StatefulTensor:$fw_input_cell_state, + TFL_StatefulTensor:$bw_input_activation_state, + TFL_StatefulTensor:$bw_input_cell_state, + + // Auxiliary input & weights. + TFL_TensorOfOrNone<[F32, I8]>:$aux_input, + // Auxiliary fw weights. + TFL_TensorOfOrNone<[F32, I8]>:$fw_aux_input_to_input_weights, + TFL_TensorOfOrNone<[F32, I8]>:$fw_aux_input_to_forget_weights, + TFL_TensorOfOrNone<[F32, I8]>:$fw_aux_input_to_cell_weights, + TFL_TensorOfOrNone<[F32, I8]>:$fw_aux_input_to_output_weights, + // Auxiliary bw weights. + TFL_TensorOfOrNone<[F32, I8]>:$bw_aux_input_to_input_weights, + TFL_TensorOfOrNone<[F32, I8]>:$bw_aux_input_to_forget_weights, + TFL_TensorOfOrNone<[F32, I8]>:$bw_aux_input_to_cell_weights, + TFL_TensorOfOrNone<[F32, I8]>:$bw_aux_input_to_output_weights, + + // Attributes + TFL_AFAttr:$fused_activation_function, + ConfinedAttr, [TFL_FloatNonNegative]>:$cell_clip, + ConfinedAttr, [TFL_FloatNonNegative]>:$proj_clip, + BoolAttr:$merge_outputs, + BoolAttr:$time_major, + // Used in post-training dynamic range quantization. If the value is true, + // input activations are asymmetrically quantized. + OptionalAttr:$asymmetric_quantize_inputs + ); + + let results = (outs + AnyTensor:$fw_output, + AnyTensor:$bw_output + ); + + let hasOptions = 1; + + let hasVerifier = 1; + + let extraClassDeclaration = [{ + // StatefulOpInterface: + std::vector GetStatefulOperands() { return {35, 36, 37, 38}; } + + // DynamicRangeQuantizedOpInterface: + bool RequireAsymmetricQuantizeInputsAttr() { return true; } + bool GetDynamicRangeQuantKernelSupport() { return true; } + std::vector GetQuantizableOperandIndices() { + return {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 16, 18, 19, 20, 21, + 22, 23, 24, 25, 26, 27, 28, 33, 40, 41, 42, 43, 44, 45, 46, 47}; + } + }]; +} + +// UnidirectionalSequenceRNN op. +def TFL_UnidirectionalSequenceRNNOp : TFL_Op<"unidirectional_sequence_rnn", [ + TFL_OperandHasRank<4, 2>, + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + PredOpTrait<"input and constant value operands must have same element type", + TFL_TCopVTEtAreSameAt<1, 2>>, + TFL_StatefulOp, + QuantizableResult, + DynamicRangeQuantizedOpInterface]> { + let summary = "Unidirectional sequence rnn operator"; + + let description = [{ + A recurrent neural network specified by an RNN cell. This Op takes in input + in a format {batch_size, seq_len, input_size} or + {seq_len, batch_size, input_size} if it's time-majored. + + It implements the following operation for + each element in the sequence s = 1...sequence_length: + outputs[s] = state = activation(RNNOp(inputs[s])) + + where RNNOp is RNNOp TF Lite Op and the “activation” is the function passed + as the “fused_activation_function” argument (if not “NONE”). + }]; + + let arguments = ( + ins TFL_FpTensor:$input, + + // Weights + TFL_TensorOf<[F32, QI8]>:$input_to_input_weights, + + // Recurrent weights + TFL_TensorOf<[F32, QI8]>:$recurrent_to_input_weights, + + // Bias + TFL_FpTensor:$input_gate_bias, + + // Hidden state. + TFL_StatefulTensor:$hidden_state, + + // Attributes + BoolAttr:$time_major, + TFL_AFAttr:$fused_activation_function, + // Used in post-training dynamic range quantization. If the value is true, + // input activations are asymmetrically quantized. + OptionalAttr:$asymmetric_quantize_inputs + ); + + let results = (outs TFL_FpTensor:$output); + + let hasOptions = 1; + + let customOption = "SequenceRNNOptions"; + + let hasVerifier = 1; + + let extraClassDeclaration = [{ + // StatefulOpInterface: + std::vector GetStatefulOperands() { return {4}; } + + // DynamicRangeQuantizedOpInterface: + bool RequireAsymmetricQuantizeInputsAttr() { return true; } + bool GetDynamicRangeQuantKernelSupport() { return true; } + std::vector GetQuantizableOperandIndices() { + return {1, 2}; + } + }]; +} + +def TFL_WhereOp : TFL_Op<"where", [Pure]> { + let summary = "Returns locations of nonzero / true values in a tensor."; + + let description = [{ +This operation returns the coordinates of true elements in `condition`. The +coordinates are returned in a 2-D tensor where the first dimension (rows) +represents the number of true elements, and the second dimension (columns) +represents the coordinates of the true elements. Keep in mind, the shape of +the output tensor can vary depending on how many true values there are in +`condition`. Indices are output in row-major order. + }]; + + let arguments = (ins + TFL_TensorOf<[I1, F32, TFL_Int32Or64, I8, UI8, UI32]>:$condition + ); + + let results = (outs + TFL_I64Tensor:$index + ); +} + +def TFL_NumericVerifyOp : Op]> { + + let summary = "Verifies the numericals of the two operands"; + + let description = [{ + The NumericVerify op is a debugging op to verify the numericals of the two + activations. It is a custom op in TFLite. + If log_if_failed is true, the NumericVerify op calculates statistics on + differences between float and quantized activations, output + logs, set differences to the output tensors, and throws an error if errors + above tolerance exist. If log_if_failed = false, then it doesn't care about + errors. + }]; + + let arguments = (ins + TFL_TensorOf<[QI8, QUI8, QI16, F16, TFL_Quint8]>:$input, + TFL_TensorOf<[F32]>:$ref, + + // Attributes + DefaultValuedOptionalAttr:$tolerance, + DefaultValuedOptionalAttr:$log_if_failed + ); + + let results = (outs TFL_FpTensor:$output); +} + +// SVDF op. +def TFL_SVDFOp : + TFL_Op<"svdf", [ + PredOpTrait<"the input and result tensor elemental types must be same", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + TFL_StatefulOp, + AccumulatorUniformScale<3, 2, 4>, + QuantizableResult, + DynamicRangeQuantizedOpInterface, + RequiresQuantizedBiasInterface]> { + + let summary = "Single value decomposition filter operator"; + + let description = [{ + The SVDF op is a decomposition of a densely connected op into low rank + filters. + For details: https://research.google.com/pubs/pub43813.html + https://arxiv.org/abs/1812.02802 + }]; + + let arguments = ( + ins TFL_TensorOf<[F32, QI8]>:$input, + + // Feature Weights. + TFL_TensorOf<[F32, QI8, QUI8]>:$feature_weights, + + // Time weights + TFL_TensorOf<[F32, QI16]>:$time_weights, + + // Bias + TFL_TensorOfOrNone<[F32, QI32]>:$input_gate_bias, + + // Activation state. + TFL_StatefulTensor:$activation_state, + + // Attributes + ConfinedAttr:$rank, + TFL_AFAttr:$fused_activation_function, + // Used in post-training dynamic range quantization. If the value is true, + // input activations are asymmetrically quantized. + OptionalAttr:$asymmetric_quantize_inputs + ); + + let results = (outs TFL_TensorOf<[F32, QI8]>:$output); + + let hasOptions = 1; + + let hasVerifier = 1; + + let extraClassDeclaration = [{ + // StatefulOpInterface: + std::vector GetStatefulOperands() { return {4}; } + + // DynamicRangeQuantizedOpInterface: + bool RequireAsymmetricQuantizeInputsAttr() { return true; } + bool GetDynamicRangeQuantKernelSupport() { return true; } + std::vector GetQuantizableOperandIndices() { + return {1, 2}; + } + // RequiresQuantizedBiasInterface + int GetBiasOperandIndex() { return 3; } + std::vector GetNonBiasOperandIndices() { return {2, 4}; } + }]; +} + +def TFL_SegmentSumOp: TFL_Op<"segment_sum", [ + Pure, + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>]> { + let summary = "SegmentSum operator"; + + let description = [{ + Computes the sum along segments of a tensor. + }]; + + let arguments = (ins + TFL_TensorOf<[F32, I32]>:$input, + TFL_I32Tensor:$segment_ids + ); + let results = (outs TFL_TensorOf<[F32, I32]>:$output); +} + +def TFL_UnsortedSegmentProdOp: TFL_Op<"unsorted_segment_prod", [ + Pure, + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>]> { + let summary = "UnsortedSegmentProd operator"; + + let description = [{ + Computes the product along segments of a tensor. + }]; + + let arguments = (ins + TFL_TensorOf<[F32, I32]>:$input, + TFL_I32Tensor:$segment_ids, + TFL_I32Tensor:$num_segments + ); + let results = (outs TFL_TensorOf<[F32, I32]>:$output); +} + +def TFL_UnsortedSegmentMaxOp: TFL_Op<"unsorted_segment_max", [ + Pure, PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>]> { + + let summary = "UnsortedSegmentMax operator"; + + let description = [{ + Computes the maximum value along segments of a tensor such that + output[i] = max(data[j....]) where segment_ids[j...] = i + if the maximum is empty for a given segment ID i, + it outputs the smallest possible value for the specific numeric type, + output[i] = numeric_limits::lowest(). + Note the values of segment_ids are always validated to be less than + num_segments and an error is thrown for out-of-bound indices. + }]; + + let arguments = (ins + TFL_TensorOf<[F32, I32]>:$input, + TFL_I32Tensor:$segment_ids, + TFL_I32Tensor:$num_segments + ); + + let results = (outs TFL_TensorOf<[F32, I32]>:$output); +} + +def TFL_UnsortedSegmentMinOp: TFL_Op<"unsorted_segment_min", [ + Pure, PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>]> { + + let summary = "UnsortedSegmentMin operator"; + + let description = [{ + Computes the minimum value along segments of a tensor such that + output[i] = min(data[j....]) where segment_ids[j...] = i + if the minimum is empty for a given segment ID i, + it outputs the largest possible value for the specific numeric type, + output[i] = numeric_limits::max(). + Note the values of segment_ids are always validated to be less than + num_segments and an error is thrown for out-of-bound indices. + }]; + + let arguments = (ins + TFL_TensorOf<[F32, I32]>:$input, + TFL_I32Tensor:$segment_ids, + TFL_I32Tensor:$num_segments + ); + + let results = (outs TFL_TensorOf<[F32, I32]>:$output); +} + +def TFL_UnsortedSegmentSumOp: TFL_Op<"unsorted_segment_sum", [ + Pure, PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>]> { + + let summary = "UnsortedSegmentSum operator"; + + let description = [{ + From a tensor segmentation, computes the `output` resulting from + summing together elements mapped to the same segment_id. I.e. `output[i]` is + equal to the tensor sum of all elements from the input tensor mapped to + segment_id `i`. If no tensors are mapped to a particular included + segment_id, the output at that indice will be a zero tensor with the + appropriate shape. Note the values of segment_ids are always validated to be + less than num_segments and an error is thrown for out-of-bound indices + }]; + + let arguments = (ins + TFL_TensorOf<[F32, I32]>:$input, + TFL_I32Tensor:$segment_ids, + TFL_I32Tensor:$num_segments + ); + + let results = (outs TFL_TensorOf<[F32, I32]>:$output); +} + +def TFL_Atan2Op: TFL_Op<"atan2", [ + Pure, + SameOperandsAndResultType + ]> { + + let summary = "Atan2 operation"; + let description = [{ + The "atan2" operation computes the arctangent of y/x element-wise, + respecting signs of the arguments. + }]; + + let arguments = (ins + TFL_TensorOf<[F32, F64]>:$y, + TFL_TensorOf<[F32, F64]>:$x + ); + + let results = (outs + TFL_TensorOf<[F32, F64]>:$output + ); +} + +def TFL_SignOp: TFL_Op<"sign", [ + Pure, + SameOperandsAndResultType + ]> { + + let summary = "Sign operation"; + let description = [{ + Returns NaN if x is NaN, 0 if x is 0, -1 if x < 0 and 1 if x > 0. + }]; + + let arguments = (ins + TFL_TensorOf<[F32, F64, I32]>:$x + ); + + let results = (outs + TFL_TensorOf<[F32, F64, I32]>:$output + ); +} + +def TFL_YieldOp : Op]> { + let summary = "Yield operation"; + let description = [{ + The "yield" operation represents a return operation within the conditional + and body of structured control flow (e.g., while), and a terminator for ControlNodeOp. + The operation takes a variable number of operands and produces no results. + The operand number and types must match the signature of the region that contains the operation. + }]; + let arguments = (ins Variadic); + + // Default builder needed for ensureTerminator + let builders = [ + OpBuilder<(ins), + [{ + build($_builder, $_state, {}); + }]> + ]; +} + +def TFL_IfOp : Op, + SingleBlockImplicitTerminator<"YieldOp">, + RecursiveMemoryEffects, + NoRegionArguments, + DeclareOpInterfaceMethods]> { + let summary = [{if-then-else operation}]; + + let description = [{ + The `tfl.if` operation represents an if-then-else construct for + conditionally executing two regions of code. The operand to an if operation + is a boolean value. For example: + + ```mlir + tfl.if %b { + ... + } else { + ... + } + ``` + + `tfl.if` may also return results that are defined in its regions. The + values defined are determined by which execution path is taken. + + Example: + + ```mlir + %x, %y = tfl.if %b -> (tensor, tensor) { + %x_true = ... + %y_true = ... + tfl.yield %x_true, %y_true : tensor, tensor + } else { + %x_false = ... + %y_false = ... + tfl.yield %x_false, %y_false : tensor, tensor + } + ``` + + `tfl.if` regions are always terminated with "tfl.yield". If "tfl.if" + defines no values, the "tfl.yield" can be left out, and will be inserted + implicitly. Otherwise, it must be explicit. + Also, if "tfl.if" defines one or more values, the 'else' block cannot be + omitted. + + Example: + + ```mlir + tfl.if %b { + ... + } + ``` + }]; + + let arguments = (ins TFL_BoolTensor:$cond); + let results = (outs Variadic:$results); + let regions = (region SizedRegion<1>:$then_region, AnyRegion:$else_region); + + let extraClassDeclaration = [{ + OpBuilder getThenBodyBuilder(OpBuilder::Listener *listener = nullptr) { + Block* body = getBody(0); + return getResults().empty() ? OpBuilder::atBlockTerminator(body, listener) + : OpBuilder::atBlockEnd(body, listener); + } + OpBuilder getElseBodyBuilder(OpBuilder::Listener *listener = nullptr) { + Block* body = getBody(1); + return getResults().empty() ? OpBuilder::atBlockTerminator(body, listener) + : OpBuilder::atBlockEnd(body, listener); + } + }]; + + // Canonicalizer wasn't defined for this one. In practise, we legalize the + // tf.IfOp to scf.If op first and then legalize it to tfl.if to reduce + // code redundancy. +} + +def TFL_WhileOp : Op, + SingleBlockImplicitTerminator<"YieldOp">, + DeclareOpInterfaceMethods]> { + let summary = [{While loop}]; + + let description = [{ + output = input; while (cond(output)) { output = body(output) } + + While loop where all values are passes through arguments with implicit + capture. + + input: A list of input tensors whose types are T. + output: A list of output tensors whose types are T. + cond: A region that takes 'input' and returns a boolean scalar tensor. + body: A region that takes a list of tensors and returns another + list of tensors. Both lists have the same types. + }]; + + let arguments = (ins + Variadic:$input, + + // Used to map StatelessWhile and While op defined in TensorFlow to a common + // op. + DefaultValuedOptionalAttr:$is_stateless + ); + let results = (outs Variadic:$output); + + let regions = (region SizedRegion<1>:$cond, SizedRegion<1>:$body); + + let hasVerifier = 1; + + let hasCanonicalizer = 1; +} + +def TFL_CallOnceOp : TFL_Op<"call_once", []> { + let summary = "Invokes an initialization function"; + + let description = [{ +This operation invokes the given initialization function for the session +initializer in tf saved model dialect. + }]; + + let arguments = (ins + StrAttr:$session_init_function + ); + + let results = (outs); +} + +def TFL_CustomOp : Op]> { + let summary = "Custom op"; + + let description = [{ + A generic op for any TFLite custom operation. + + input: A list of inputs in the original op. + custom_code: A string used to identify which exactly this op is, which + corresponds to operator_codes.custom_code in the flatbuffer. + custom_option: a holder to save the op attributes in bytes fashion. + output: A list of outputs in the original op. + }]; + + let arguments = (ins + Variadic>:$input, + StrAttr:$custom_code, + TFL_ConstBytesAttr:$custom_option + ); + let results = (outs Variadic:$output); + + let hasVerifier = 1; +} + +def TFL_CustomTfOp : Op, + DeclareOpInterfaceMethods, + DeclareOpInterfaceMethods]> { + let summary = "Wrapper Op for TF custom ops."; + + let description = [{ + A wrapper op around any Custom TF op. These includes ops defined using + custom_opdefs or linked which are not defined in TF dialect. + This Op just wraps the custom op inside a region. + Note #1, this Op will not include TF Lite custom ops defined using CustomOp. + Note #2, this op is just internal representation inside the converter and + are not exposed/exported when the model is exported to Flatbuffer. + }]; + + let arguments = (ins + Variadic>:$input + ); + let results = (outs Variadic:$output); + + let regions = (region SizedRegion<1>:$body); + + let extraClassDeclaration = [{ + // Returns whether the return types are compatible. + static bool isCompatibleReturnTypes(TypeRange l, TypeRange r); + }]; +} + +def TFL_BroadcastToOp : TFL_Op<"broadcast_to", [ + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + TFL_OperandHasRankAtMost<0, 8>, + SameOperandsAndResultsScale, + TFL_OperandHasRank<1, 1>, + PredOpTrait<"output dimension count must be at most 8", + Or<[TFL_OperandIsUnrankedPred<1>, + TFL_OperandDimIsAtMost<1, 0, 8>]>>, + QuantizableResult, + Pure]> { + let summary = "Broadcast an array for a compatible shape."; + + let description = [{ +Broadcasting is the process of making arrays to have compatible shapes +for arithmetic operations. Two shapes are compatible if for each +dimension pair they are either equal or one of them is one. When trying +to broadcast a Tensor to a shape, it starts with the trailing dimensions, +and works its way forward. + +For example, + +>>> x = tf.constant([1, 2, 3]) +>>> y = tf.broadcast_to(x, [3, 3]) +>>> print(y) +tf.Tensor( + [[1 2 3] + [1 2 3] + [1 2 3]], shape=(3, 3), dtype=int32) + +In the above example, the input Tensor with the shape of `[1, 3]` +is broadcasted to output Tensor with shape of `[3, 3]`. + +When doing broadcasted operations such as multiplying a tensor +by a scalar, broadcasting (usually) confers some time or space +benefit, as the broadcasted tensor is never materialized. + +However, `broadcast_to` does not carry with it any such benefits. +The newly-created tensor takes the full memory of the broadcasted +shape. (In a graph context, `broadcast_to` might be fused to +subsequent operation and then be optimized away, however.) + }]; + + let arguments = (ins + TFL_TensorOf<[F32, I32, I1, TFL_I4, I8, QI8, UI8, UI32, QUI8, I16, QI16, I64, Complex>]>:$input, + TFL_I32OrI64Tensor:$shape + ); + + let results = (outs + TFL_TensorOf<[F32, I32, I1, TFL_I4, I8, QI8, UI8, UI32, QUI8, I16, QI16, I64, Complex>]>:$output + ); + + let extraClassDeclaration = [{ + // Quantized axes are verified in the Verify function. + bool RequiredSameQuantizedAxes() { return false; } + }]; + + let hasCanonicalizer = 1; +} + +def TFL_RFFT2dOp : TFL_Op<"rfft2d", [Pure]> { + let summary = "2D real-valued fast Fourier transform."; + + let description = [{ +Computes the 2-dimensional discrete Fourier transform of a real-valued signal +over the inner-most 2 dimensions of `input`. + +Since the DFT of a real signal is Hermitian-symmetric, `RFFT2D` only returns the +`fft_length / 2 + 1` unique components of the FFT for the inner-most dimension +of `output`: the zero-frequency term, followed by the `fft_length / 2` +positive-frequency terms. + +Along each axis `RFFT2D` is computed on, if `fft_length` is smaller than the +corresponding dimension of `input`, the dimension is cropped. If it is larger, +the dimension is padded with zeros. + }]; + + let arguments = (ins + TFL_FpTensor:$input, + TFL_I32Tensor:$fft_length + ); + + let results = (outs + TFL_Complex64Tensor:$output + ); +} + +def TFL_VarHandleOp : TFL_Op<"var_handle", []> { + let summary = "Returns a handle to a variable resource from its name."; + + let description = [{ + Returns a handle for a variable resource from its name. + container: the container this variable is placed in. + shared_name: the name by which this variable is referred to. + }]; + + let arguments = (ins + DefaultValuedStrAttr:$container, + DefaultValuedStrAttr:$shared_name + ); + + let results = (outs TFL_ResourceTensor:$resource_handle); + + let hasOptions = 1; +} + +def TFL_AssignVariableOp : TFL_Op<"assign_variable", []> { + let summary = "Assigns a new value to a variable."; + + let description = [{ +Any ReadVariableOp with a control dependency on this op is guaranteed to return +this value or a subsequent newer value of the variable. + }]; + + let arguments = (ins + TFL_ResourceTensor:$resource_id, + TFL_TensorOf<[F32, F64, I1, UI8, I8, QI8, QUI8, I32, I64, QI16, Complex>, Complex>]>:$value + ); + + let results = (outs); +} + +def TFL_ReadVariableOp : TFL_Op<"read_variable", []> { + let summary = "Reads variable value."; + + let description = [{ +Read variable data identified by 'resource_id'. + }]; + + let arguments = (ins + TFL_ResourceTensor:$resource_id + ); + + let results = (outs TFL_TensorOf<[F32, F64, I1, UI8, I8, QI8, QUI8, I32, I64, QI16, Complex>, Complex>]>:$result); +} + +def TFL_Conv3DOp : TFL_Op<"conv_3d", [ + Pure, + AccumulatorUniformScale<2, 0, 1>, + TFL_OperandHasRank<0, 5>, + TFL_OperandHasRank<1, 5>, + // Channel dimension in input and filter should match. + TFL_OperandsHaveSameDimsTrait<0, 1, 4, 3>, + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + PredOpTrait<"bias and output must have same element type", + Or<[ + TFL_OperandIsNoneType<2>, + TFL_TCresVTEtIsSameAsOp<0, 2>]>>, + PredOpTrait<"bias must has num of elements equals to 4th dim of filter", + Or<[ + TFL_OperandIsNoneType<2>, + TFL_NumElementsEqualsDim<2, 1, 4>]>>, + RequiresQuantizedBiasInterface]> { + let summary = "Convolution 3D operator"; + + let description = [{ + Performs convolution operation on 3D inputs. + Inputs: + `inputs[0]`: required: the input activation tensor + `inputs[1]`: required: the filter weight tensor + `inputs[2]`: optional: the bias tensor + }]; + + let arguments = (ins + TFL_TensorOf<[F32]>:$input, + TFL_TensorOf<[F32]>:$filter, + TFL_TensorOfOrNone<[F32]>:$bias, + I32Attr:$dilation_d_factor, + I32Attr:$dilation_h_factor, + I32Attr:$dilation_w_factor, + TFL_AFAttr:$fused_activation_function, + TFL_PaddingAttr:$padding, + I32Attr:$stride_d, + I32Attr:$stride_h, + I32Attr:$stride_w + ); + + let results = (outs TFL_TensorOf<[F32]>:$output); + + let hasOptions = 1; + + let customOption = "Conv3DOptions"; + + let extraClassDeclaration = [{ + // RequiresQuantizedBiasInterface + int GetBiasOperandIndex() { return 2; } + std::vector GetNonBiasOperandIndices() { return {0, 1}; } + }]; +} + +def TFL_Conv3DTransposeOp : TFL_Op<"conv_3d_transpose", [ + Pure, + AccumulatorUniformScale<3, 1, 2>, + TFL_OperandHasRank<0, 1>, + TFL_OperandHasRank<1, 5>, + TFL_OperandHasRank<2, 5>, + TFL_NumElementsTrait<0, 5>, + // Channel dimension in input and filter should match. + TFL_OperandsHaveSameDimsTrait<2, 1, 4, 4>, + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 2>>, + PredOpTrait<"bias and output must have same element type", + Or<[ + TFL_OperandIsNoneType<3>, + TFL_TCresVTEtIsSameAsOp<0, 3>]>>, + PredOpTrait<"bias must has num of elements equals to 4th dim of filter", + Or<[ + TFL_OperandIsNoneType<3>, + TFL_NumElementsEqualsDim<3, 1, 4>]>>, + RequiresQuantizedBiasInterface]> { + let summary = "Transposed Convolution 3D operator"; + + let description = [{ + Performs transposed convolution operation on 3D inputs. + Inputs: + `inputs[0]`: required: the shape of output tensor + `inputs[1]`: required: the filter weight tensor + `inputs[2]`: required: the input activation tensor + `inputs[3]`: optional: the bias tensor + }]; + + let arguments = (ins + TFL_I32Tensor:$output_shape, + TFL_TensorOf<[F32]>:$filter, + TFL_TensorOf<[F32]>:$input, + TFL_TensorOfOrNone<[F32]>:$bias, + I32Attr:$dilation_d_factor, + I32Attr:$dilation_h_factor, + I32Attr:$dilation_w_factor, + TFL_AFAttr:$fused_activation_function, + TFL_PaddingAttr:$padding, + I32Attr:$stride_d, + I32Attr:$stride_h, + I32Attr:$stride_w + ); + + let results = (outs TFL_TensorOf<[F32]>:$output); + + let hasOptions = 1; + + let customOption = "Conv3DOptions"; + + let extraClassDeclaration = [{ + // RequiresQuantizedBiasInterface + int GetBiasOperandIndex() { return 3; } + std::vector GetNonBiasOperandIndices() { return {0, 1, 2}; } + }]; +} + +def TFL_ComplexAbsOp : TFL_Op<"complex_abs", [ + Pure, + SameOperandsAndResultShape]> { + let summary = "Computes the complex absolute value of a tensor."; + + let description = [{ +Given a tensor `x` of complex numbers, this operation returns a tensor of type +`float` or `double` that is the absolute value of each element in `x`. All +elements in `x` must be complex numbers of the form \\(a + bj\\). The absolute +value is computed as \\( \sqrt{a^2 + b^2}\\). + }]; + + let arguments = (ins + TFL_TensorOf<[Complex>, Complex>]>:$input + ); + + let results = (outs + TFL_TensorOf<[F32, F64]>:$output + ); + + let builders = [ + OpBuilder<(ins "Value":$input), + [{ + $_state.addOperands({input}); + $_state.addTypes(dyn_cast(input.getType()).getElementType()); + }]> + ]; +} + +def TFL_RealOp : TFL_Op<"real", [ + Pure, + SameOperandsAndResultShape]> { + let summary = "Returns the real part of a complex number."; + + let description = [{ +Given a tensor `input` of complex numbers, this operation returns a tensor of +type `float` that is the real part of each element in `input`. All elements in +`input` must be complex numbers of the form \\(a + bj\\), where *a* is the real + part returned by this operation and *b* is the imaginary part. + }]; + + let arguments = (ins + TFL_TensorOf<[Complex>, Complex>]>:$input + ); + + let results = (outs + TFL_TensorOf<[F32, F64]>:$output + ); + + let builders = [ + OpBuilder<(ins "Value":$input), + [{ + $_state.addOperands({input}); + $_state.addTypes(dyn_cast(input.getType()).getElementType()); + }]> + ]; +} + +def TFL_ImagOp : TFL_Op<"imag", [ + Pure, + SameOperandsAndResultShape]> { + let summary = "Returns the imaginary part of a complex number."; + + let description = [{ +Given a tensor `input` of complex numbers, this operation returns a tensor of +type `float` that is the imaginary part of each element in `input`. All +elements in `input` must be complex numbers of the form \\(a + bj\\), where *a* +is the real part and *b* is the imaginary part returned by this operation. + }]; + + let arguments = (ins + TFL_TensorOf<[Complex>, Complex>]>:$input + ); + + let results = (outs + TFL_TensorOf<[F32, F64]>:$output + ); + + let builders = [ + OpBuilder<(ins "Value":$input), + [{ + $_state.addOperands({input}); + $_state.addTypes(dyn_cast(input.getType()).getElementType()); + }]> + ]; +} + +def TFL_HashtableOp: TFL_Op<"hashtable", []> { + let summary = "Creates a non-initialized hash table."; + let description = [{ +This op creates a hash table, specifying the type of its keys and values. +Before using the table you will have to initialize it. After initialization the +table will be immutable. + }]; + + let arguments = (ins + I32Attr:$table_id, + TypeAttr:$key_dtype, + TypeAttr:$value_dtype + ); + + let results = (outs TFL_ResourceTensor:$out); + + let hasOptions = 1; +} + +def TFL_HashtableFindOp: TFL_Op<"hashtable_find", []> { + let summary = "Looks up keys in a table, outputs the corresponding values."; + + let description = [{ +The tensor `keys` must of the same type as the keys of the table. +The output `values` is of the type of the table values. + +The scalar `default_value` is the value output for keys not present in the +table. It must also be of the same type as the table values. + }]; + + let arguments = (ins + TFL_ResourceTensor:$hash_table, + TFL_TensorOf<[I32, TFL_Str, I64]>:$keys, + TFL_TensorOf<[F32, I32, TFL_Str, I64]>:$default_value + ); + + let results = (outs TFL_TensorOf<[F32, I32, TFL_Str, I64]>:$out); +} + +def TFL_HashtableImportOp: TFL_Op<"hashtable_import", []> { + let summary = [{ +Replaces the contents of the table with the specified keys and values. + }]; + + let description = [{ +The tensor `keys` must be of the same type as the keys of the table. +The tensor `values` must be of the type of the table values. + }]; + + let arguments = (ins + TFL_ResourceTensor:$hash_table, + TFL_TensorOf<[I32, TFL_Str, I64]>:$keys, + TFL_TensorOf<[F32, I32, TFL_Str, I64]>:$values + ); + + let results = (outs); +} + + +def TFL_HashtableSizeOp: TFL_Op<"hashtable_size", []> { + let summary = "Computes the number of elements in the given table."; + + let arguments = (ins + TFL_ResourceTensor:$hash_table + ); + + let results = (outs + TFL_I64Tensor:$out + ); +} + +def TFL_BroadcastArgsOp : TFL_Op<"broadcast_args",[ + OperandsSameElementTypeConstraintBase<"BroadcastArgs op">, + PredOpTrait<"input and output must have same element type", + TFL_TCresVTEtIsSameAsOp<0, 0>>, + TFL_OperandHasRank<0, 1>, + TFL_OperandHasRank<1, 1>, + Pure]> { + let summary = "Return the shape of s0 op s1 with broadcast."; + + let description = [{ +Given `s0` and `s1`, tensors that represent shapes, compute `r0`, the +broadcasted shape. `s0`, `s1` and `r0` are all integer vectors. + }]; + + let arguments = (ins + TFL_I32OrI64Tensor:$s0, + TFL_I32OrI64Tensor:$s1 + ); + + let results = (outs + TFL_I32OrI64Tensor:$r0 + ); +} + +def TFL_BucketizeOp + : TFL_Op<"bucketize", [Pure, SameOperandsAndResultShape]> { + let summary = "Bucketizes 'input' based on 'boundaries'."; + + let description = [{ +Example: + +If the inputs are `boundaries = [0, 10, 100]` and +`input = [[-5, 10000][150, 10][5, 100]]`, +then the output will be `output = [[0, 3][3, 2][1, 3]]`. + }]; + + let arguments = (ins + TFL_TensorOf<[F32, F64, I32, I64]>:$input, + F32ArrayAttr:$boundaries + ); + + let results = (outs + TFL_TensorOf<[I32]>:$output + ); + + let hasOptions = 1; +} + +def TFL_RandomUniformOp : TFL_Op<"random_uniform", []> { + let summary = "Outputs random values from a uniform distribution."; + + let description = [{ +The generated values follow a uniform distribution in the range `[0, 1)`. The +lower bound 0 is included in the range, while the upper bound 1 is excluded. + }]; + + let arguments = (ins + TFL_I32Tensor:$shape, + DefaultValuedOptionalAttr:$seed, + DefaultValuedOptionalAttr:$seed2 + ); + + let results = (outs + TFL_TensorOf<[F32]>:$out); + let hasOptions = 1; + let customOption = "RandomOptions"; +} + +def TFL_RandomStandardNormalOp : TFL_Op<"random_standard_normal", []> { + let summary = "Outputs random values from a normal distribution."; + + let description = [{ +The generated values will have mean 0 and standard deviation 1. + }]; + + let arguments = (ins + TFL_I32Tensor:$shape, + DefaultValuedOptionalAttr:$seed, + DefaultValuedOptionalAttr:$seed2 + ); + + let results = (outs + TFL_TensorOf<[F32]>:$out); + let hasOptions = 1; + let customOption = "RandomOptions"; +} + +def TFL_MultinomialOp : TFL_Op<"multinomial", []> { + let summary = "Draws samples from a categorical distribution."; + + let description = [{ +The generated values will have a categorical distribution based on the `logits` +or unnormalized log-probabilities provided for all classes. + }]; + + let arguments = (ins + TFL_FpTensor:$logits, + TFL_I32Tensor:$num_samples, + DefaultValuedOptionalAttr:$seed, + DefaultValuedOptionalAttr:$seed2 + ); + + let results = (outs + TFL_TensorOf<[I32, I64]>:$out); + + let hasOptions = 1; + let customOption = "RandomOptions"; +} + +def TFL_NoValueOp : Op { + let summary = "constant representing no value."; + + let description = [{ +No value constant op. + }]; + + let arguments = (ins UnitAttr:$value); + + let results = (outs NoneType:$none_val); + + let hasFolder = 1; + + let extraClassDeclaration = [{ + /// Returns true if a constant operation can be built with the given value + /// and result type. + static bool isBuildableWith(Attribute value, Type type); + }]; +} + +def TFL_ControlNodeOp : Op, RecursiveMemoryEffects, SingleBlockImplicitTerminator<"YieldOp">]> { + let summary = [{ + The `TFL.control_node` operation wraps single-block operations in order to attach control edges. + }]; + + let description = [{ + This is used to wrap regions and attach control dependencies to them. Typically, + this will happen in one of the last steps before emitting the flatbuffer model + in order to enable optimizations that rely on a fixed order of operations (such + as rematerialization.) + The flatbuffer exporter will unwrap the wrapped region and annotate the generated + model with metadata such that any runtime reorderings will respect the order + given by the control dependencies. + }]; + + // May take values of type !tfl.control as inputs + let arguments = (ins + Variadic:$controlInputs + ); + + // Adds a single value of type !tfl.control as the last output (all other + // outputs are copied from the wrapped region.) + let results = (outs + Variadic:$outputs, + TFL_Control:$control + ); + + let regions = (region SizedRegion<1>:$body); + + let extraClassDeclaration = [{ + Block &GetBody() { return getOperation()->getRegion(0).front(); } + YieldOp GetYield(); + bool WrapsSinglePerfectlyForwardedOp(); + }]; + + let hasCanonicalizer = 0; + let hasVerifier = 1; + let hasFolder = 0; + let hasCustomAssemblyFormat = 1; +} + +#endif // TFL_OPS + +// LINT.ThenChange() +// LINT.ThenChange(//tensorflow/compiler/mlir/lite/tools/versioning/op_version.cc) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/json_to_flatbuffer.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/json_to_flatbuffer.cc new file mode 100644 index 0000000000000000000000000000000000000000..2e7e619ce8016233fd83bf3ce932eba42cc730e0 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/json_to_flatbuffer.cc @@ -0,0 +1,62 @@ +/* Copyright 2020 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 "flatbuffers/flatbuffers.h" // from @flatbuffers +#include "flatbuffers/idl.h" // from @flatbuffers +#include "flatbuffers/util.h" // from @flatbuffers + +int main(int argc, char** argv) { + // load FlatBuffer schema (.fbs) and JSON from disk + if (argc < 2) { + std::cerr << "Missing input argument. Usage:\n" + << argv[0] << " \n\n"; + return 1; + } + const char* schema_path = argv[1]; + const char* json_path = argv[2]; + std::string schema; + std::string json; + + const bool status = + flatbuffers::LoadFile(schema_path, /*binary=*/false, &schema) && + flatbuffers::LoadFile(json_path, /*binary=*/false, &json); + if (!status) { + std::cerr << "couldn't load files!\n"; + return 1; + } + + // parse schema first, so we can use it to parse the data after + flatbuffers::Parser parser; + const bool schema_parse_result = + parser.Parse(schema.c_str()) && parser.Parse(json.c_str()); + if (!schema_parse_result) { + std::cerr << "Parse error.\n"; + return 1; + } + const size_t length = parser.builder_.GetSize(); + const size_t n = + std::fwrite(parser.builder_.GetBufferPointer(), 1, length, stdout); + if (n != length) { + std::cerr << "print to stdout filed.\n"; + return 1; + } + return 0; +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..c388009f27548a0330605d6245546e2e4dde0f0e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/BUILD @@ -0,0 +1,23 @@ +load("@rules_cc//cc:cc_library.bzl", "cc_library") +load("//tensorflow:tensorflow.default.bzl", "get_compatible_with_portable") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + default_visibility = [ + "//visibility:public", + ], + licenses = ["notice"], +) + +# LINT.IfChange(cc_library_padding) +cc_library( + name = "padding", + srcs = [], + hdrs = ["padding.h"], + compatible_with = get_compatible_with_portable(), + visibility = ["//tensorflow/compiler/mlir/lite/kernels:__pkg__"], + deps = [ + "//tensorflow/compiler/mlir/lite/core/c:tflite_common", + ], +) +# LINT.ThenChange(//tensorflow/lite/kernels/BUILD) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..ba50ff88179cceb92fb813ede5d234f9bed1e7e1 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/BUILD @@ -0,0 +1,115 @@ +load("@bazel_skylib//lib:selects.bzl", "selects") +load("@rules_cc//cc:cc_library.bzl", "cc_library") +load("//tensorflow:tensorflow.bzl", "tf_cc_test") +load("//tensorflow:tensorflow.default.bzl", "get_compatible_with_portable") +load("//tensorflow/compiler/mlir/lite:build_def.bzl", "tflite_copts") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + default_visibility = [ + "//visibility:public", + ], + licenses = ["notice"], +) + +cc_library( + name = "compatibility_macros", + hdrs = ["compatibility_macros.h"], + compatible_with = get_compatible_with_portable(), + copts = tflite_copts(), +) + +cc_library( + name = "runtime_shape", + srcs = ["runtime_shape.cc"], + hdrs = ["runtime_shape.h"], + compatible_with = get_compatible_with_portable(), + deps = [":compatibility_macros"], +) + +tf_cc_test( + name = "runtime_shape_test", + srcs = ["runtime_shape_test.cc"], + deps = [ + ":runtime_shape", + "@com_google_absl//absl/algorithm:container", + "@com_google_absl//absl/types:span", + "@com_google_googletest//:gtest_main", + ], +) + +# LINT.IfChange + +cc_library( + name = "cppmath", + srcs = [], + hdrs = [ + "cppmath.h", + ], + compatible_with = get_compatible_with_portable(), + copts = tflite_copts(), +) + +cc_library( + name = "quantization_util", + srcs = ["quantization_util.cc"], + hdrs = ["quantization_util.h"], + compatible_with = get_compatible_with_portable(), + copts = tflite_copts(), + linkopts = select({ + "//tensorflow:windows": [], + "//conditions:default": ["-lm"], + }), + deps = [ + ":compatibility_macros", + ":cppmath", + ], +) + +config_setting( + name = "x86_32", + constraint_values = ["@platforms//cpu:x86_32"], +) + +config_setting( + name = "x86_64", + constraint_values = ["@platforms//cpu:x86_64"], +) + +selects.config_setting_group( + name = "x86_any", + match_any = [ + ":x86_32", + ":x86_64", + ], +) + +cc_library( + name = "cpu_check", + hdrs = [ + "optimized/neon_check.h", + ], + compatible_with = get_compatible_with_portable(), + copts = tflite_copts(), + visibility = ["//visibility:public"], + deps = select({ + ":x86_any": ["@arm_neon_2_x86_sse"], + "//conditions:default": [], + }), +) + +cc_library( + name = "common", + srcs = ["common.cc"], + hdrs = ["common.h"], + compatible_with = get_compatible_with_portable(), + copts = tflite_copts(), + deps = [ + ":compatibility_macros", + ":cpu_check", + "//tensorflow/compiler/mlir/lite/core:macros", + "@gemmlowp//:fixedpoint", + ], +) + +# LINT.ThenChange(//tensorflow/lite/kernels/internal/BUILD) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/README b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/README new file mode 100644 index 0000000000000000000000000000000000000000..b6567f3a68a70bbf0f0507ef7f46485e10852dec --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/README @@ -0,0 +1,3 @@ +This folder mirrors some files in tensorflow/lite/kernels/internal that are +needed in the converter. They mostly have the same name, although +compatibility_macros.h mirrors compatibility.h and op_macros.h. \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/common.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/common.cc new file mode 100644 index 0000000000000000000000000000000000000000..4fef1bf00b1ec79fc2e68643b5ad222daf7baf21 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/common.cc @@ -0,0 +1,107 @@ +/* Copyright 2023 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/compiler/mlir/lite/kernels/internal/common.h" + +// LINT.IfChange + +namespace tflite_migration { + +// Single-rounding MultiplyByQuantizedMultiplier +#if TFLITE_SINGLE_ROUNDING +int32_t MultiplyByQuantizedMultiplier(int32_t x, int32_t quantized_multiplier, + int shift) { + TFLITE_DCHECK(quantized_multiplier >= 0); + TFLITE_DCHECK(shift >= -31 && shift <= 30); + + const int64_t total_shift = 31 - shift; + const int64_t round = static_cast(1) << (total_shift - 1); + int64_t result = x * static_cast(quantized_multiplier) + round; + result = result >> total_shift; + + TFLITE_DCHECK(result >= std::numeric_limits::min() && + result <= std::numeric_limits::max()); + return static_cast(result); +} + +int32_t MultiplyByQuantizedMultiplier(int64_t x, int32_t quantized_multiplier, + int shift) { + // Inputs: + // - quantized_multiplier has fixed point at bit 31 + // - shift is -31 to +7 (negative for right shift) + // + // Assumptions: The following input ranges are assumed + // - quantize_scale>=0 (the usual range is (1<<30) to (1>>31)-1) + // - scaling is chosen so final scaled result fits in int32_t + // - input x is in the range -(1<<47) <= x < (1<<47) + TFLITE_DCHECK(quantized_multiplier >= 0); + TFLITE_DCHECK(shift >= -31 && shift < 8); + TFLITE_DCHECK(x >= -(static_cast(1) << 47) && + x < (static_cast(1) << 47)); + + const int32_t reduced_multiplier = + (quantized_multiplier < 0x7FFF0000) + ? ((quantized_multiplier + (1 << 15)) >> 16) + : 0x7FFF; + const int64_t total_shift = 15 - shift; + const int64_t round = static_cast(1) << (total_shift - 1); + int64_t result = x * static_cast(reduced_multiplier) + round; + result = result >> total_shift; + + TFLITE_DCHECK(result >= std::numeric_limits::min() && + result <= std::numeric_limits::max()); + return static_cast(result); +} +// Double-rounding MultiplyByQuantizedMultiplier +#else +int32_t MultiplyByQuantizedMultiplier(int32_t x, int32_t quantized_multiplier, + int shift) { + using gemmlowp::RoundingDivideByPOT; + using gemmlowp::SaturatingRoundingDoublingHighMul; + int left_shift = shift > 0 ? shift : 0; + int right_shift = shift > 0 ? 0 : -shift; + return RoundingDivideByPOT(SaturatingRoundingDoublingHighMul( + x * (1 << left_shift), quantized_multiplier), + right_shift); +} + +int32_t MultiplyByQuantizedMultiplier(int64_t x, int32_t quantized_multiplier, + int shift) { + // Inputs: + // - quantized_multiplier has fixed point at bit 31 + // - shift is -31 to +7 (negative for right shift) + // + // Assumptions: The following input ranges are assumed + // - quantize_scale>=0 (the usual range is (1<<30) to (1>>31)-1) + // - scaling is chosen so final scaled result fits in int32_t + // - input x is in the range -(1<<47) <= x < (1<<47) + assert(quantized_multiplier >= 0); + assert(shift >= -31 && shift < 8); + assert(x >= -(static_cast(1) << 47) && + x < (static_cast(1) << 47)); + + int32_t reduced_multiplier = (quantized_multiplier < 0x7FFF0000) + ? ((quantized_multiplier + (1 << 15)) >> 16) + : 0x7FFF; + int total_shift = 15 - shift; + x = (x * (int64_t)reduced_multiplier) + ((int64_t)1 << (total_shift - 1)); + int32_t result = x >> total_shift; + return result; +} +#endif // TFLITE_SINGLE_ROUNDING + +} // namespace tflite_migration + +// LINT.ThenChange(//tensorflow/lite/kernels/internal/common.cc) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/common.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/common.h new file mode 100644 index 0000000000000000000000000000000000000000..a254d32da1290f522de5a9da79720a9c0efce2cb --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/common.h @@ -0,0 +1,236 @@ +/* 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_KERNELS_INTERNAL_COMMON_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_KERNELS_INTERNAL_COMMON_H_ + +#include +#include +#include + +#ifndef ALLOW_SLOW_GENERIC_DEPTHWISECONV_FALLBACK +#ifdef GEMMLOWP_ALLOW_SLOW_SCALAR_FALLBACK +#define ALLOW_SLOW_GENERIC_DEPTHWISECONV_FALLBACK +#endif +#endif + +#include +#include + +#include "fixedpoint/fixedpoint.h" +#include "tensorflow/compiler/mlir/lite/core/macros.h" +#include "tensorflow/compiler/mlir/lite/kernels/internal/compatibility_macros.h" +#include "tensorflow/compiler/mlir/lite/kernels/internal/optimized/neon_check.h" + +// LINT.IfChange + +namespace tflite_migration { + +constexpr int kReverseShift = -1; + +TFLITE_NOINLINE int32_t MultiplyByQuantizedMultiplier( + int32_t x, int32_t quantized_multiplier, int shift); + +TFLITE_NOINLINE int32_t MultiplyByQuantizedMultiplier( + int64_t x, int32_t quantized_multiplier, int shift); + +// Single-rounding MultiplyByQuantizedMultiplier +#if TFLITE_SINGLE_ROUNDING +inline int32_t MultiplyByQuantizedMultiplierSmallerThanOneExp( + int32_t x, int32_t quantized_multiplier, int shift) { + TFLITE_DCHECK_LE(shift, 0); + return MultiplyByQuantizedMultiplier(x, quantized_multiplier, shift); +} + +inline int32_t MultiplyByQuantizedMultiplierGreaterThanOne( + int32_t x, int32_t quantized_multiplier, int shift) { + TFLITE_DCHECK_GE(shift, 0); + return MultiplyByQuantizedMultiplier(x, quantized_multiplier, shift); +} + +#ifdef USE_NEON +inline int32x4x4_t MultiplyByQuantizedMultiplier4Rows( + int32x4x4_t input_val, int32_t quantized_multiplier, int shift) { + TFLITE_DCHECK(quantized_multiplier >= 0); + + const int right_shift = std::min(-1, shift); + const int left_shift = shift - right_shift; + + const int32x4_t multiplier_dup = vdupq_n_s32(quantized_multiplier); + const int32x4_t left_shift_dup = vdupq_n_s32(left_shift); + const int32x4_t right_shift_dup = vdupq_n_s32(right_shift); + + int32x4x4_t result; + result.val[0] = vrshlq_s32( + vqdmulhq_s32(vshlq_s32(input_val.val[0], left_shift_dup), multiplier_dup), + right_shift_dup); + + result.val[1] = vrshlq_s32( + vqdmulhq_s32(vshlq_s32(input_val.val[1], left_shift_dup), multiplier_dup), + right_shift_dup); + + result.val[2] = vrshlq_s32( + vqdmulhq_s32(vshlq_s32(input_val.val[2], left_shift_dup), multiplier_dup), + right_shift_dup); + + result.val[3] = vrshlq_s32( + vqdmulhq_s32(vshlq_s32(input_val.val[3], left_shift_dup), multiplier_dup), + right_shift_dup); + + return result; +} +#endif // USE_NEON +// Double-rounding MultiplyByQuantizedMultiplier +#else +inline int32_t MultiplyByQuantizedMultiplierSmallerThanOneExp( + int32_t x, int32_t quantized_multiplier, int left_shift) { + using gemmlowp::RoundingDivideByPOT; + using gemmlowp::SaturatingRoundingDoublingHighMul; + return RoundingDivideByPOT( + SaturatingRoundingDoublingHighMul(x, quantized_multiplier), -left_shift); +} + +inline int32_t MultiplyByQuantizedMultiplierGreaterThanOne( + int32_t x, int32_t quantized_multiplier, int left_shift) { + using gemmlowp::SaturatingRoundingDoublingHighMul; + return SaturatingRoundingDoublingHighMul(x * (1 << left_shift), + quantized_multiplier); +} + +#ifdef USE_NEON +// Round uses ARM's rounding shift right. +inline int32x4x4_t MultiplyByQuantizedMultiplier4Rows( + int32x4x4_t input_val, int32_t quantized_multiplier, int shift) { + const int left_shift = std::max(shift, 0); + const int right_shift = std::min(shift, 0); + int32x4x4_t result; + + int32x4_t multiplier_dup = vdupq_n_s32(quantized_multiplier); + int32x4_t left_shift_dup = vdupq_n_s32(left_shift); + int32x4_t right_shift_dup = vdupq_n_s32(right_shift); + + result.val[0] = + vrshlq_s32(vqrdmulhq_s32(vshlq_s32(input_val.val[0], left_shift_dup), + multiplier_dup), + right_shift_dup); + + result.val[1] = + vrshlq_s32(vqrdmulhq_s32(vshlq_s32(input_val.val[1], left_shift_dup), + multiplier_dup), + right_shift_dup); + + result.val[2] = + vrshlq_s32(vqrdmulhq_s32(vshlq_s32(input_val.val[2], left_shift_dup), + multiplier_dup), + right_shift_dup); + + result.val[3] = + vrshlq_s32(vqrdmulhq_s32(vshlq_s32(input_val.val[3], left_shift_dup), + multiplier_dup), + right_shift_dup); + + return result; +} +#endif // USE_NEON +#endif // TFLITE_SINGLE_ROUNDING + +template +int CountLeadingZeros(T integer_input) { + static_assert(std::is_unsigned::value, + "Only unsigned integer types handled."); + if (integer_input == 0) { + return std::numeric_limits::digits; + } +#if defined(__GNUC__) + if (std::is_same::value) { + return __builtin_clz(integer_input); + } else if (std::is_same::value) { + return __builtin_clzll(integer_input); + } +#endif + const T one_in_leading_positive = static_cast(1) + << (std::numeric_limits::digits - 1); + int leading_zeros = 0; + while (integer_input < one_in_leading_positive) { + integer_input <<= 1; + ++leading_zeros; + } + return leading_zeros; +} + +inline void GetInvSqrtQuantizedMultiplierExp(int32_t input, int reverse_shift, + int32_t* output_inv_sqrt, + int* output_shift) { + TFLITE_DCHECK_GE(input, 0); + if (input <= 1) { + // Handle the input value 1 separately to avoid overflow in that case + // in the general computation below (b/143972021). Also handle 0 as if it + // were a 1. 0 is an invalid input here (divide by zero) and 1 is a valid + // but rare/unrealistic input value. We can expect both to occur in some + // incompletely trained models, but probably not in fully trained models. + *output_inv_sqrt = std::numeric_limits::max(); + *output_shift = 0; + return; + } + TFLITE_DCHECK_GT(input, 1); + *output_shift = 11; + while (input >= (1 << 29)) { + input /= 4; + ++*output_shift; + } + const unsigned max_left_shift_bits = + CountLeadingZeros(static_cast(input)) - 1; + const unsigned max_left_shift_bit_pairs = max_left_shift_bits / 2; + const unsigned left_shift_bit_pairs = max_left_shift_bit_pairs - 1; + *output_shift -= left_shift_bit_pairs; + input <<= 2 * left_shift_bit_pairs; + TFLITE_DCHECK_GE(input, (1 << 27)); + TFLITE_DCHECK_LT(input, (1 << 29)); + using gemmlowp::FixedPoint; + using gemmlowp::Rescale; + using gemmlowp::SaturatingRoundingMultiplyByPOT; + // Using 3 integer bits gives us enough room for the internal arithmetic in + // this Newton-Raphson iteration. + using F3 = FixedPoint; + using F0 = FixedPoint; + const F3 fixedpoint_input = F3::FromRaw(input >> 1); + const F3 fixedpoint_half_input = + SaturatingRoundingMultiplyByPOT<-1>(fixedpoint_input); + const F3 fixedpoint_half_three = + GEMMLOWP_CHECKED_FIXEDPOINT_CONSTANT(F3, (1 << 28) + (1 << 27), 1.5); + // Newton-Raphson iteration + // Naive unoptimized starting guess: x = 1 + F3 x = F3::One(); + // Naive unoptimized number of iterations: 5 + for (int i = 0; i < 5; i++) { + const F3 x3 = Rescale<3>(x * x * x); + x = Rescale<3>(fixedpoint_half_three * x - fixedpoint_half_input * x3); + } + const F0 fixedpoint_half_sqrt_2 = + GEMMLOWP_CHECKED_FIXEDPOINT_CONSTANT(F0, 1518500250, std::sqrt(2.) / 2.); + x = x * fixedpoint_half_sqrt_2; + *output_inv_sqrt = x.raw(); + if (*output_shift < 0) { + *output_inv_sqrt <<= -*output_shift; + *output_shift = 0; + } + // Convert right shift (right is positive) to left shift. + *output_shift *= reverse_shift; +} + +} // namespace tflite_migration + +// LINT.ThenChange(//tensorflow/lite/kernels/internal/common.h) + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_KERNELS_INTERNAL_COMMON_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/compatibility_macros.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/compatibility_macros.h new file mode 100644 index 0000000000000000000000000000000000000000..5363d5cd8dd5f84432dde8a541308c356133add2 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/compatibility_macros.h @@ -0,0 +1,63 @@ +/* Copyright 2024 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_KERNELS_INTERNAL_COMPATIBILITY_MACROS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_KERNELS_INTERNAL_COMPATIBILITY_MACROS_H_ + +#ifndef TFLITE_ABORT +#define TFLITE_ABORT abort() +#endif + +#ifndef TFLITE_ASSERT_FALSE +#if defined(NDEBUG) +#define TFLITE_ASSERT_FALSE (static_cast(0)) +#else +#define TFLITE_ASSERT_FALSE TFLITE_ABORT +#endif +#endif + +// LINT.IfChange + +#ifndef TFLITE_DCHECK +#define TFLITE_DCHECK(condition) (condition) ? (void)0 : TFLITE_ASSERT_FALSE +#endif + +#ifndef TFLITE_DCHECK_EQ +#define TFLITE_DCHECK_EQ(x, y) ((x) == (y)) ? (void)0 : TFLITE_ASSERT_FALSE +#endif + +#ifndef TFLITE_DCHECK_NE +#define TFLITE_DCHECK_NE(x, y) ((x) != (y)) ? (void)0 : TFLITE_ASSERT_FALSE +#endif + +#ifndef TFLITE_DCHECK_GE +#define TFLITE_DCHECK_GE(x, y) ((x) >= (y)) ? (void)0 : TFLITE_ASSERT_FALSE +#endif + +#ifndef TFLITE_DCHECK_GT +#define TFLITE_DCHECK_GT(x, y) ((x) > (y)) ? (void)0 : TFLITE_ASSERT_FALSE +#endif + +#ifndef TFLITE_DCHECK_LE +#define TFLITE_DCHECK_LE(x, y) ((x) <= (y)) ? (void)0 : TFLITE_ASSERT_FALSE +#endif + +#ifndef TFLITE_DCHECK_LT +#define TFLITE_DCHECK_LT(x, y) ((x) < (y)) ? (void)0 : TFLITE_ASSERT_FALSE +#endif + +// LINT.ThenChange(//tensorflow/lite/kernels/internal/compatibility.h) + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_KERNELS_INTERNAL_COMPATIBILITY_MACROS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/cppmath.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/cppmath.h new file mode 100644 index 0000000000000000000000000000000000000000..4aae261ce2a2abd3ac36926f42f867a34388348b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/cppmath.h @@ -0,0 +1,43 @@ +/* Copyright 2020 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_KERNELS_INTERNAL_CPPMATH_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_KERNELS_INTERNAL_CPPMATH_H_ + +#include + +// LINT.IfChange + +namespace tflite_migration { + +#if defined(TF_LITE_USE_GLOBAL_CMATH_FUNCTIONS) || \ + (defined(__ANDROID__) && !defined(__NDK_MAJOR__)) || defined(__ZEPHYR__) +#define TF_LITE_GLOBAL_STD_PREFIX +#else +#define TF_LITE_GLOBAL_STD_PREFIX std +#endif + +#define DECLARE_STD_GLOBAL_SWITCH1(tf_name, std_name) \ + template \ + inline T tf_name(const T x) { \ + return TF_LITE_GLOBAL_STD_PREFIX::std_name(x); \ + } + +DECLARE_STD_GLOBAL_SWITCH1(TfLiteRound, round) + +} // namespace tflite_migration + +// LINT.ThenChange(//tensorflow/lite/kernels/internal/cppmath.h) + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_KERNELS_INTERNAL_CPPMATH_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/optimized/neon_check.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/optimized/neon_check.h new file mode 100644 index 0000000000000000000000000000000000000000..a5d2987a8b6e45a2b4100f7e0a961f28c3211e6f --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/optimized/neon_check.h @@ -0,0 +1,32 @@ +/* Copyright 2019 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_KERNELS_INTERNAL_OPTIMIZED_NEON_CHECK_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_KERNELS_INTERNAL_OPTIMIZED_NEON_CHECK_H_ + +// LINT.IfChange + +#if defined(__ARM_NEON__) || defined(__ARM_NEON) +#define USE_NEON +#include // IWYU pragma: export +#endif + +#if defined __GNUC__ && defined __SSE4_1__ && !defined TF_LITE_DISABLE_X86_NEON +#define USE_NEON +#include "NEON_2_SSE.h" // IWYU pragma: export +#endif + +// LINT.ThenChange(//tensorflow/lite/kernels/internal/optimized/neon_check.h) + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_KERNELS_INTERNAL_OPTIMIZED_NEON_CHECK_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/quantization_util.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/quantization_util.cc new file mode 100644 index 0000000000000000000000000000000000000000..b8f5b289274453185233a2abaecc291bae4b84c9 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/quantization_util.cc @@ -0,0 +1,333 @@ +/* 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/compiler/mlir/lite/kernels/internal/quantization_util.h" + +#include +#include +#include + +#include "tensorflow/compiler/mlir/lite/kernels/internal/compatibility_macros.h" +#include "tensorflow/compiler/mlir/lite/kernels/internal/cppmath.h" + +namespace tflite_migration { + +// LINT.IfChange + +namespace { +// These constants are used to manipulate the binary representation of doubles. +// Double-precision binary64 floating point format is: +// Bit | 63 | 62-52 | 51-0 | +// | Sign | Exponent | Fraction | +// To avoid 64-bit integers as much as possible, I break this into high and +// low 32-bit chunks. High is: +// Bit | 31 | 30-20 | 19-0 | +// | Sign | Exponent | High Fraction | +// Low is: +// Bit | 31-0 | +// | Low Fraction | +// We then access the components through logical bit-wise operations to +// extract the parts needed, with the positions and masks derived from the +// layout shown above. +constexpr uint64_t kSignMask = 0x8000000000000000LL; +constexpr uint64_t kExponentMask = 0x7ff0000000000000LL; +constexpr int32_t kExponentShift = 52; +constexpr int32_t kExponentBias = 1023; +constexpr uint32_t kExponentIsBadNum = 0x7ff; +constexpr uint64_t kFractionMask = 0x000fffffffc00000LL; +constexpr uint32_t kFractionShift = 22; +constexpr uint32_t kFractionRoundingMask = 0x003fffff; +constexpr uint32_t kFractionRoundingThreshold = 0x00200000; +} // namespace + +void QuantizeMultiplier(double double_multiplier, int32_t* quantized_multiplier, + int* shift) { +#if TFLITE_SINGLE_ROUNDING + // Single-rounding MultiplyByQuantizedMultiplier only supports positive + // multipliers. + // TFLITE_DCHECK(double_multiplier >= 0); +#endif + if (double_multiplier == 0.) { + *quantized_multiplier = 0; + *shift = 0; + return; + } +#ifdef TFLITE_EMULATE_FLOAT + // If we're trying to avoid the use of floating-point instructions (for + // example on microcontrollers) then use an alternative implementation + // that only requires integer and bitwise operations. To enable this, you + // need to set the define during the build process for your platform. + int64_t q_fixed = IntegerFrExp(double_multiplier, shift); +#else // TFLITE_EMULATE_FLOAT + const double q = std::frexp(double_multiplier, shift); + auto q_fixed = static_cast(TfLiteRound(q * (1LL << 31))); +#endif // TFLITE_EMULATE_FLOAT + TFLITE_DCHECK(q_fixed <= (1LL << 31)); + if (q_fixed == (1LL << 31)) { + q_fixed /= 2; + ++*shift; + } + TFLITE_DCHECK_LE(q_fixed, std::numeric_limits::max()); + // A shift amount smaller than -31 would cause all bits to be shifted out + // and thus all results would be zero. We implement that instead with + // q_fixed==0, so as to avoid hitting issues with right-shift + // operations with shift amounts greater than 31. Note that this happens + // roughly when abs(double_multiplier) < 2^-31 and the present handling means + // that we're effectively flushing tiny double_multiplier's to zero. + // We could conceivably handle values in the range (roughly) [32, 63] + // as 'denormals' i.e. (shift==0, q_fixed < 2^30). In that point of view + // the present handling is just doing 'flush denormals to zero'. We could + // reconsider and actually generate nonzero denormals if a need arises. + if (*shift < -31) { + *shift = 0; + q_fixed = 0; + } +#if TFLITE_SINGLE_ROUNDING + // Single-rounding MultiplyByQuantizedMultiplier doesn't support a shift > 30, + // saturate it. + if (*shift > 30) { + *shift = 30; + q_fixed = (1LL << 31) - 1; + } +#endif + *quantized_multiplier = static_cast(q_fixed); +} + +void QuantizeMultiplierGreaterThanOne(double double_multiplier, + int32_t* quantized_multiplier, + int* left_shift) { + TFLITE_DCHECK_GT(double_multiplier, 1.); + QuantizeMultiplier(double_multiplier, quantized_multiplier, left_shift); + TFLITE_DCHECK_GE(*left_shift, 0); +} + +int64_t IntegerFrExp(double input, int* shift) { + // Make sure our assumptions about the double layout hold. + TFLITE_DCHECK_EQ(8, sizeof(double)); + + // We want to access the bits of the input double value directly, which is + // tricky to do safely, so use a union to handle the casting. + union { + double double_value; + uint64_t double_as_uint; + } cast_union; + cast_union.double_value = input; + const uint64_t u = cast_union.double_as_uint; + + // If the bitfield is all zeros apart from the sign bit, this is a normalized + // zero value, so return standard values for this special case. + if ((u & ~kSignMask) == 0) { + *shift = 0; + return 0; + } + + // Deal with NaNs and Infs, which are always indicated with a fixed pattern in + // the exponent, and distinguished by whether the fractions are zero or + // non-zero. + const uint32_t exponent_part = ((u & kExponentMask) >> kExponentShift); + if (exponent_part == kExponentIsBadNum) { + *shift = std::numeric_limits::max(); + if (u & kFractionMask) { + // NaN, so just return zero (with the exponent set to INT_MAX). + return 0; + } else { + // Infinity, so return +/- INT_MAX. + if (u & kSignMask) { + return std::numeric_limits::min(); + } else { + return std::numeric_limits::max(); + } + } + } + + // The shift is fairly easy to extract from the high bits of the double value, + // just by masking it out and applying a bias. The std::frexp() implementation + // always returns values between 0.5 and 1.0 though, whereas the exponent + // assumes 1.0 to 2.0 is the standard range, so I add on one to match that + // interface. + *shift = (exponent_part - kExponentBias) + 1; + + // There's an implicit high bit in the double format definition, so make sure + // we include that at the top, and then reconstruct the rest of the fractional + // value from the remaining fragments. + int64_t fraction = 0x40000000 + ((u & kFractionMask) >> kFractionShift); + + // We're cutting off some bits at the bottom, so to exactly match the standard + // frexp implementation here we'll apply rounding by adding one to the least + // significant bit of the result if the discarded portion is over half of the + // maximum. + if ((u & kFractionRoundingMask) > kFractionRoundingThreshold) { + fraction += 1; + } + // Negate the fraction if the sign bit was set. + if (u & kSignMask) { + fraction *= -1; + } + + return fraction; +} + +double DoubleFromFractionAndShift(int64_t fraction, int shift) { + union { + double double_value; + uint64_t double_as_uint; + } result; + + // Detect NaNs and infinities. + if (shift == std::numeric_limits::max()) { + if (fraction == 0) { + return std::numeric_limits::quiet_NaN(); + } else if (fraction > 0) { + return std::numeric_limits::infinity(); + } else { + return -std::numeric_limits::infinity(); + } + } + + // Return a normalized zero for a zero fraction. + if (fraction == 0) { + result.double_as_uint = 0; + return result.double_value; + } + + bool is_negative = (fraction < 0); + int64_t encoded_fraction = is_negative ? -fraction : fraction; + int64_t encoded_shift = (shift - 1); + while (encoded_fraction < 0x40000000) { + encoded_fraction *= 2; + encoded_shift -= 1; + } + while (encoded_fraction > 0x80000000) { + encoded_fraction /= 2; + encoded_shift += 1; + } + encoded_fraction -= 0x40000000; + if (encoded_shift < -1022) { + encoded_shift = -1023; + } else if (encoded_shift > 1022) { + encoded_shift = 1023; + } + encoded_shift += kExponentBias; + uint64_t encoded_sign = is_negative ? kSignMask : 0; + result.double_as_uint = encoded_sign | (encoded_shift << kExponentShift) | + (encoded_fraction << kFractionShift); + return result.double_value; +} + +double IntegerDoubleMultiply(double a, double b) { + int a_shift; + const int64_t a_fraction = IntegerFrExp(a, &a_shift); + int b_shift; + const int64_t b_fraction = IntegerFrExp(b, &b_shift); + // Detect NaNs and infinities. + if (a_shift == std::numeric_limits::max() || + (b_shift == std::numeric_limits::max())) { + return std::numeric_limits::quiet_NaN(); + } + const int result_shift = a_shift + b_shift + 1; + const int64_t result_fraction = (a_fraction * b_fraction) >> 32; + return DoubleFromFractionAndShift(result_fraction, result_shift); +} + +int IntegerDoubleCompare(double a, double b) { + int a_shift; + const int64_t a_fraction = IntegerFrExp(a, &a_shift); + int b_shift; + const int64_t b_fraction = IntegerFrExp(b, &b_shift); + + // Detect NaNs and infinities. + if (a_shift == std::numeric_limits::max() || + (b_shift == std::numeric_limits::max())) { + return 1; + } + + if ((a_fraction == 0) && (b_fraction < 0)) { + return 1; + } else if ((a_fraction < 0) && (b_fraction == 0)) { + return -1; + } else if (a_shift < b_shift) { + return -1; + } else if (a_shift > b_shift) { + return 1; + } else if (a_fraction < b_fraction) { + return -1; + } else if (a_fraction > b_fraction) { + return 1; + } else { + return 0; + } +} + +void PreprocessSoftmaxScaling(double beta, double input_scale, + int input_integer_bits, + int32_t* quantized_multiplier, int* left_shift) { + // If the overall multiplier (input and beta) is large, then exp() of an + // input difference of 1 scaled by this will be large. In other words, we + // can cap the multiplier and know that, when it is used, the output will be + // (round to) zero wherever the input is not at the maximum value. + + // If the overall scale is less than one, and input_integer_bits=0, then the + // result is double equivalent of Q0.31 (actually with more precision). Thus + // this generates a Q(input_integer_bits).(31-input_integer_bits) + // representation. +#if TFLITE_SINGLE_ROUNDING + const double max_real_multiplier = (1LL << 30) - 1.0; +#else + const double max_real_multiplier = (1LL << 31) - 1.0; +#endif + +#ifdef TFLITE_EMULATE_FLOAT + const double input_beta = IntegerDoubleMultiply(beta, input_scale); + int shift; + int64_t fraction = IntegerFrExp(input_beta, &shift); + shift += (31 - input_integer_bits); + double input_beta_real_multiplier = + DoubleFromFractionAndShift(fraction, shift); + if (IntegerDoubleCompare(input_beta_real_multiplier, max_real_multiplier) > + 0) { + input_beta_real_multiplier = max_real_multiplier; + } +#else // TFLITE_EMULATE_FLOAT + const double input_beta_real_multiplier = + std::min(beta * input_scale * (1 << (31 - input_integer_bits)), + max_real_multiplier); +#endif // TFLITE_EMULATE_FLOAT + + QuantizeMultiplierGreaterThanOne(input_beta_real_multiplier, + quantized_multiplier, left_shift); +} + +int CalculateInputRadius(int input_integer_bits, int input_left_shift, + int total_signed_bits) { +#ifdef TFLITE_EMULATE_FLOAT + int64_t result = (1 << input_integer_bits) - 1; + result <<= (total_signed_bits - input_integer_bits); + result >>= input_left_shift; + return result; +#else // TFLITE_EMULATE_FLOAT + const double max_input_rescaled = + 1.0 * ((1 << input_integer_bits) - 1) * + (1LL << (total_signed_bits - input_integer_bits)) / + (1LL << input_left_shift); + // Tighten bound using floor. Suppose that we could use the exact value. + // After scaling the difference, the result would be at the maximum. Thus we + // must ensure that our value has lower magnitude. + return static_cast(std::floor(max_input_rescaled)); +#endif // TFLITE_EMULATE_FLOAT +} + +// LINT.ThenChange(//tensorflow/lite/kernels/internal/quantization_util.cc) + +} // namespace tflite_migration diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/quantization_util.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/quantization_util.h new file mode 100644 index 0000000000000000000000000000000000000000..158d31f178810c61904440e0def5ba1e0b30a3e0 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/quantization_util.h @@ -0,0 +1,166 @@ +/* Copyright 2024 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_KERNELS_INTERNAL_QUANTIZATION_UTIL_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_KERNELS_INTERNAL_QUANTIZATION_UTIL_H_ + +#include +#include +#include + +namespace tflite_migration { + +// LINT.IfChange + +// Decompose a double multiplier into a Q0.31 int32 representation of its +// significand, and shift representation of its exponent. +// +// Restricted to the case where the multiplier > 1. +void QuantizeMultiplierGreaterThanOne(double double_multiplier, + int32_t* quantized_multiplier, + int* left_shift); + +// Decompose a double multiplier into a Q0.31 int32 representation of its +// significand, and shift representation of its exponent. +// +// Handles an arbitrary positive multiplier. The 'shift' output-value is +// basically the 'floating-point exponent' of the multiplier: +// Negative for a right-shift (when the multiplier is <1), positive for a +// left-shift (when the multiplier is >1) +void QuantizeMultiplier(double double_multiplier, int32_t* quantized_multiplier, + int* shift); + +// Splits a double input value into a returned fraction, and a shift value from +// the exponent, using only bitwise and integer operations to support +// microcontrollers and other environments without floating-point support. +// +// This is designed to be a replacement for how std::frexp() is used within the +// QuantizeMultiplier() function, and so has a different signature than the +// standard version, returning a 64-bit integer rather than a double. This +// result has a maximum value of 1<<31, with the fraction expressed as a +// proportion of that maximum. +// +// std::frexp() returns NaNs and infinities unmodified, but since we're +// returning integers that can't represent those values, instead we return +// a shift of std::numeric_limits::max() for all bad numbers, with an int64 +// result of 0 for NaNs, std:numeric_limits::max() for +INFINITY, and +// std::numeric_limits::min() for -INFINITY. Denormalized inputs will +// result in return values that end up truncating some bits at the end, +// reflecting the loss of precision inherent in denormalization. +int64_t IntegerFrExp(double input, int* shift); + +// Converts an integer fraction in the format produced by IntegerFrExp (where +// 0x40000000 is 1.0) and an exponent shift (between -1022 and +1022) into an +// IEEE binary64 double format result. The implementation uses only integer and +// bitwise operators, so no floating point hardware support or emulation is +// needed. This is here so quantized operations can run non-time-critical +// preparation calculations on microcontrollers and other platforms without +// float support. +double DoubleFromFractionAndShift(int64_t fraction, int shift); + +// Performs a multiplication of two numbers in double format, using only integer +// and bitwise instructions. This is aimed at supporting housekeeping functions +// for quantized operations on microcontrollers without floating-point hardware. +double IntegerDoubleMultiply(double a, double b); + +// Returns -1 if a is less than b, 0 if a and b are equal, and +1 if a is +// greater than b. It is implemented using only integer and logical instructions +// so that it can be easily run on microcontrollers for quantized operations. +int IntegerDoubleCompare(double a, double b); + +// This first creates a multiplier in a double equivalent of +// Q(input_integer_bits).(31-input_integer_bits) representation, with extra +// precision in the double's fractional bits. It then splits the result into +// significand and exponent. +void PreprocessSoftmaxScaling(double beta, double input_scale, + int input_integer_bits, + int32_t* quantized_multiplier, int* left_shift); +// Like PreprocessSoftmaxScaling, but inverse scaling factors also calculated. + +// Calculate the largest input that will result in a within-bounds intermediate +// result within MultiplyByQuantizedMultiplierGreaterThanOne. In other words, +// it must not overflow before we reduce the value by multiplication by the +// input multiplier. The negative radius is used as the minimum difference in +// Softmax. +int CalculateInputRadius(int input_integer_bits, int input_left_shift, + int total_signed_bits = 31); + +// Converts a floating-point number to an integer. For all inputs x where +// static_cast(x) is legal according to the C++ standard, the result +// is identical to that cast (i.e. the result is x with its fractional part +// truncated whenever that is representable as IntOut). +// +// static_cast would cause undefined behavior for the following cases, which +// have well-defined behavior for this function: +// +// 1. If x is NaN, the result is zero. +// +// 2. If the truncated form of x is above the representable range of IntOut, +// the result is std::numeric_limits::max(). +// +// 3. If the truncated form of x is below the representable range of IntOut, +// the result is std::numeric_limits::min(). +// +// Note that cases #2 and #3 cover infinities as well as finite numbers. +// +// The range of FloatIn must include the range of IntOut, otherwise +// the results are undefined. +// TODO(sfeuz): Replace by absl::SafeCast once available. +template +IntOut SafeCast(FloatIn x) { + static_assert(!std::numeric_limits::is_integer, + "FloatIn is integer"); + static_assert(std::numeric_limits::is_integer, + "IntOut is not integer"); + static_assert(std::numeric_limits::radix == 2, "IntOut is base 2"); + + // Special case NaN, for which the logic below doesn't work. + if (std::isnan(x)) { + return 0; + } + + // Negative values all clip to zero for unsigned results. + if (!std::numeric_limits::is_signed && x < 0) { + return 0; + } + + // Handle infinities. + if (std::isinf(x)) { + return x < 0 ? std::numeric_limits::min() + : std::numeric_limits::max(); + } + + // Set exp such that x == f * 2^exp for some f with |f| in [0.5, 1.0), + // unless x is zero in which case exp == 0. Note that this implies that the + // magnitude of x is strictly less than 2^exp. + int exp = 0; + std::frexp(x, &exp); + + // Let N be the number of non-sign bits in the representation of IntOut. If + // the magnitude of x is strictly less than 2^N, the truncated version of x + // is representable as IntOut. The only representable integer for which this + // is not the case is kMin for signed types (i.e. -2^N), but that is covered + // by the fall-through below. + if (exp <= std::numeric_limits::digits) { + return x; + } + + // Handle numbers with magnitude >= 2^N. + return x < 0 ? std::numeric_limits::min() + : std::numeric_limits::max(); +} +// LINT.ThenChange(//tensorflow/lite/kernels/internal/quantization_util.h) +} // namespace tflite_migration + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_KERNELS_INTERNAL_QUANTIZATION_UTIL_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/runtime_shape.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/runtime_shape.cc new file mode 100644 index 0000000000000000000000000000000000000000..f13c07108cb78d0208e315dc58fc4321f2255113 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/runtime_shape.cc @@ -0,0 +1,144 @@ +/* Copyright 2023 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. +==============================================================================*/ + +// This file is the MLIR copy of runtime_shape as part of the effort to +// decouple TFLite from MLIR. +// LINT.IfChange + +#include "tensorflow/compiler/mlir/lite/kernels/internal/runtime_shape.h" + +#include +#include +#include + +#include "tensorflow/compiler/mlir/lite/kernels/internal/compatibility_macros.h" + +namespace mlir { + +namespace { + +bool CheckedMul(size_t a, size_t b, size_t& product) { + if (a != 0 && b > std::numeric_limits::max() / a) { + return false; + } + product = a * b; + return true; +} + +bool CheckedMul(size_t a, int32_t b, size_t& product) { + if (b < 0) return false; + return CheckedMul(a, static_cast(b), product); +} + +bool CheckedCast(size_t in, int& out) { + if (in > static_cast(std::numeric_limits::max())) return false; + out = static_cast(in); + return true; +} + +} // namespace + +RuntimeShape::~RuntimeShape() { +#ifndef TF_LITE_STATIC_MEMORY + if (size_ > kMaxSmallSize) { + delete[] dims_pointer_; + } +#endif // TF_LITE_STATIC_MEMORY +} + +int32_t RuntimeShape::Dims(int i) const { + TFLITE_DCHECK_GE(i, 0); + TFLITE_DCHECK_LT(i, size_); +#ifndef TF_LITE_STATIC_MEMORY + return size_ > kMaxSmallSize ? dims_pointer_[i] : dims_[i]; +#else + return dims_[i]; +#endif // TF_LITE_STATIC_MEMORY +} + +void RuntimeShape::ReplaceWith(int dimensions_count, const int32_t* dims_data) { + TFLITE_DCHECK_GE(dimensions_count, 0); +#ifndef TF_LITE_STATIC_MEMORY + Resize(dimensions_count); + int32_t* dst_dims = DimsData(); +#else + TFLITE_DCHECK_LE(dimensions_count, kMaxSmallSize); + size_ = dimensions_count; + int32_t* dst_dims = DimsData(); +#endif // TF_LITE_STATIC_MEMORY + std::memcpy(dst_dims, dims_data, dimensions_count * sizeof(int32_t)); +} + +int RuntimeShape::FlatSize() const { + int buffer_size = 1; + const int* dims_data = reinterpret_cast(DimsData()); + for (int i = 0; i < size_; i++) { + buffer_size *= dims_data[i]; + } + return buffer_size; +} + +bool RuntimeShape::CheckedNumElementsInRange(int start, int end, + size_t& out) const { + if (start < 0 || end < start || end > size_) return false; + size_t checked_out = 1; + const int32_t* dims_data = DimsData(); + for (int i = start; i < end; ++i) { + if (!CheckedMul(checked_out, dims_data[i], checked_out)) return false; + } + out = checked_out; + return true; +} + +bool RuntimeShape::CheckedNumElementsInRange(int start, int end, + int& out) const { + size_t checked_out = 0; + return CheckedNumElementsInRange(start, end, checked_out) && + CheckedCast(checked_out, out); +} + +bool RuntimeShape::CheckedSizeToDimension(int end, size_t& out) const { + return CheckedNumElementsInRange(0, end, out); +} + +bool RuntimeShape::CheckedSizeToDimension(int end, int& out) const { + return CheckedNumElementsInRange(0, end, out); +} + +bool RuntimeShape::CheckedSizeFromDimension(int start, size_t& out) const { + return CheckedNumElementsInRange(start, size_, out); +} + +bool RuntimeShape::CheckedSizeFromDimension(int start, int& out) const { + return CheckedNumElementsInRange(start, size_, out); +} + +bool RuntimeShape::CheckedFlatSize(size_t& flat_size) const { + return CheckedNumElementsInRange(0, size_, flat_size); +} + +bool RuntimeShape::CheckedFlatSizeSkipDim(int skip_dim, + size_t& flat_size) const { + if (skip_dim < 0 || skip_dim >= size_) return false; + size_t prefix = 0; + if (!CheckedNumElementsInRange(0, skip_dim, prefix)) return false; + size_t suffix = 0; + if (!CheckedNumElementsInRange(skip_dim + 1, size_, suffix)) return false; + return CheckedMul(prefix, suffix, flat_size); +} + +} // namespace mlir + +// LINT.ThenChange(//tensorflow/lite/kernels/internal/runtime_shape.cc) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/runtime_shape.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/runtime_shape.h new file mode 100644 index 0000000000000000000000000000000000000000..5f07ccf421e10a6a83fa8d4efa9474e9fd189c17 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/runtime_shape.h @@ -0,0 +1,406 @@ +/* Copyright 2021 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_KERNELS_INTERNAL_RUNTIME_SHAPE_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_KERNELS_INTERNAL_RUNTIME_SHAPE_H_ + +// This file is the MLIR copy of runtime_shape as part of the effort to +// decouple TFLite from MLIR. +// LINT.IfChange + +#include +#include +#include +#include +#include +#include + +#include "tensorflow/compiler/mlir/lite/kernels/internal/compatibility_macros.h" + +namespace mlir { + +template +struct Dims { + int sizes[N]; + int strides[N]; +}; + +class RuntimeShape { + public: + // Shapes with dimensions up to 6 are stored directly in the structure, while + // larger shapes are separately allocated. + static constexpr int kMaxSmallSize = 6; + + RuntimeShape& operator=(RuntimeShape const&) = delete; + + RuntimeShape() : size_(0) {} + + explicit RuntimeShape(int dimensions_count) : size_(dimensions_count) { + TFLITE_DCHECK_GE(dimensions_count, 0); +#ifndef TF_LITE_STATIC_MEMORY + if (dimensions_count > kMaxSmallSize) { + dims_pointer_ = new int32_t[dimensions_count]; + } +#else + TFLITE_DCHECK_LE(dimensions_count, kMaxSmallSize); +#endif // TF_LITE_STATIC_MEMORY + } + +#ifndef TF_LITE_STATIC_MEMORY + RuntimeShape(int shape_size, int32_t value) : size_(0) { + TFLITE_DCHECK_GE(shape_size, 0); + Resize(shape_size); +#else + RuntimeShape(int shape_size, int32_t value) : size_(shape_size) { + TFLITE_DCHECK_GE(shape_size, 0); + TFLITE_DCHECK_LE(shape_size, kMaxSmallSize); +#endif // TF_LITE_STATIC_MEMORY + for (int i = 0; i < shape_size; ++i) { + SetDim(i, value); + } + } + + RuntimeShape(int dimensions_count, const int32_t* dims_data) : size_(0) { + TFLITE_DCHECK_GE(dimensions_count, 0); + ReplaceWith(dimensions_count, dims_data); + } + +#ifndef TF_LITE_STATIC_MEMORY + RuntimeShape(const std::initializer_list init_list) : size_(0) { + BuildFrom(init_list); + } + + // Avoid using this constructor. We should be able to delete it when C++17 + // rolls out. + RuntimeShape(RuntimeShape const& other) : size_(other.DimensionsCount()) { + if (size_ > kMaxSmallSize) { + dims_pointer_ = new int32_t[size_]; + } + std::memcpy(DimsData(), other.DimsData(), sizeof(int32_t) * size_); + } +#endif // TF_LITE_STATIC_MEMORY + + bool operator==(const RuntimeShape& comp) const { + return this->size_ == comp.size_ && + std::memcmp(DimsData(), comp.DimsData(), size_ * sizeof(int32_t)) == + 0; + } + + ~RuntimeShape(); + + inline int32_t DimensionsCount() const { return size_; } + + int32_t Dims(int i) const; + + inline void SetDim(int i, int32_t val) { + TFLITE_DCHECK_GE(i, 0); + TFLITE_DCHECK_LT(i, size_); +#ifndef TF_LITE_STATIC_MEMORY + if (size_ > kMaxSmallSize) { + dims_pointer_[i] = val; + } else { + dims_[i] = val; + } +#else + dims_[i] = val; +#endif // TF_LITE_STATIC_MEMORY + } + + inline int32_t* DimsData() { +#ifndef TF_LITE_STATIC_MEMORY + return size_ > kMaxSmallSize ? dims_pointer_ : dims_; +#else + return dims_; +#endif // TF_LITE_STATIC_MEMORY + } + inline const int32_t* DimsData() const { +#ifndef TF_LITE_STATIC_MEMORY + return size_ > kMaxSmallSize ? dims_pointer_ : dims_; +#else + return dims_; +#endif // TF_LITE_STATIC_MEMORY + } + // The caller must ensure that the shape is no bigger than 5-D. + inline const int32_t* DimsDataUpTo5D() const { return dims_; } + +#ifndef TF_LITE_STATIC_MEMORY + inline void Resize(int dimensions_count) { + TFLITE_DCHECK_GE(dimensions_count, 0); + const int32_t old_size = size_; + size_ = dimensions_count; + + if (old_size <= kMaxSmallSize) { + if (dimensions_count <= kMaxSmallSize) { + return; + } else { // Small to big. + int32_t* new_big_data = new int32_t[dimensions_count]; + memcpy(new_big_data, dims_, sizeof(int32_t) * old_size); + dims_pointer_ = new_big_data; + } + } else { + if (dimensions_count > kMaxSmallSize && dimensions_count <= old_size) { + return; + } + std::unique_ptr old_data(dims_pointer_); + if (dimensions_count <= old_size) { // Big to small. + memcpy(dims_, old_data.get(), sizeof(int32_t) * dimensions_count); + } else { // Big to bigger. + dims_pointer_ = new int32_t[dimensions_count]; + memcpy(dims_pointer_, old_data.get(), sizeof(int32_t) * old_size); + } + } + } +#endif // TF_LITE_STATIC_MEMORY + + void ReplaceWith(int dimensions_count, const int32_t* dims_data); + +#ifndef TF_LITE_STATIC_MEMORY + template + inline void BuildFrom(const T& src_iterable) { + const int dimensions_count = + std::distance(src_iterable.begin(), src_iterable.end()); + Resize(dimensions_count); + int32_t* data = DimsData(); + for (auto it : src_iterable) { + *data = it; + ++data; + } + } +#endif // TF_LITE_STATIC_MEMORY + + // This will probably be factored out. Old code made substantial use of 4-D + // shapes, and so this function is used to extend smaller shapes. Note that + // (a) as Dims<4>-dependent code is eliminated, the reliance on this should be + // reduced, and (b) some kernels are stricly 4-D, but then the shapes of their + // inputs should already be 4-D, so this function should not be needed. + inline static RuntimeShape ExtendedShape(int new_shape_size, + const RuntimeShape& shape) { + TFLITE_DCHECK_GE(new_shape_size, 0); +#ifdef TF_LITE_STATIC_MEMORY + TFLITE_DCHECK_LE(new_shape_size, kMaxSmallSize); +#endif // TF_LITE_STATIC_MEMORY + return RuntimeShape(new_shape_size, shape, 1); + } + +#ifndef TF_LITE_STATIC_MEMORY + inline void BuildFrom(const std::initializer_list init_list) { + BuildFrom>(init_list); + } +#endif // TF_LITE_STATIC_MEMORY + + // NOTE: This function does not handle potential integer overflow. + // The caller should verify that the size calculation won't overflow + // before calling this function -- for example, by calling `CheckedFlatSize` + // in a non-hot-path code and verifying that the return value is `true` + int FlatSize() const; + + /** + * Returns false if any dimension is negative or if the product of all + * dimensions would overflow size_t. + * @param flat_size The output parameter in which to store the product of the + * dimensions. + * @return False if any dimension is negative, or if the product would + * overflow size_t. Returns true otherwise. Returns 1 if the shape is empty. + */ + bool CheckedFlatSize(size_t& flat_size) const; + + /** + * Returns the checked product of dimensions in the half-open interval + * [start, end). It does a similar thing to FlatSize(), but it is more + * general and more secure. + * @param start The starting dimension index (inclusive). + * @param end The ending dimension index (exclusive). + * @param out The output parameter in which to store the product of the + * dimensions. + * @return False if the range [start, end) is invalid or if any dimension is + * negative, or if the product would overflow size_t. Returns true otherwise. + * An empty range [start, start) will return 1. + */ + bool CheckedNumElementsInRange(int start, int end, size_t& out) const; + bool CheckedNumElementsInRange(int start, int end, int& out) const; + + /** + * Returns the checked product of dimensions in the half-open interval [0, + * end). It does a similar thing to FlatSize(), but it is more general and + * more secure. + * @param end The ending dimension index (exclusive). + * @param out The output parameter in which to store the product of the + * dimensions. + * @return False if the index end is out of bounds or if any dimension in the + * interval [0, end) is negative, or if the product would overflow size_t. + * Returns true otherwise. Returns 1 if end is 0. + */ + bool CheckedSizeToDimension(int end, size_t& out) const; + + /** + * Returns the checked product of dimensions in the half-open interval [0, + * end). It does a similar thing to FlatSize(), but it is more general and + * more secure. + * @param end The ending dimension index (exclusive). + * @param out The output parameter in which to store the product of the + * dimensions. + * @return False if the index end is out of bounds or if any dimension in the + * interval [0, end) is negative, or if the product would overflow int. + * Returns true otherwise. Returns 1 if end is 0. + */ + bool CheckedSizeToDimension(int end, int& out) const; + + /** + * Returns the checked product of dimensions in the half-open interval [start, + * DimensionsCount()). + * @param start The starting dimension index (inclusive). + * @param out The output parameter in which to store the product of the + * dimensions. + * @return False if the index start is out of bounds or if any dimension in + * the interval [start, DimensionsCount()) is negative, or if the product + * would overflow size_t. Returns true otherwise. Returns 1 if start is + * DimensionsCount(). + */ + bool CheckedSizeFromDimension(int start, size_t& out) const; + /** + * Returns the checked product of dimensions in the half-open interval [start, + * DimensionsCount()). + * @param start The starting dimension index (inclusive). + * @param out The output parameter in which to store the product of the + * dimensions. + * @return False if the index start is out of bounds or if any dimension in + * the interval [start, DimensionsCount()) is negative, or if the product + * would overflow int. Returns true otherwise. Returns 1 if start is + * DimensionsCount(). + */ + bool CheckedSizeFromDimension(int start, int& out) const; + + /** + * Returns the checked product of dimensions in the half-open interval [0, + * DimensionsCount()) excluding the dimension at the given index. + * @param skip_dim The index of the dimension to exclude from the product. + * @param flat_size The output parameter in which to store the product of the + * dimensions. + * @return False if the index skip_dim is out of bounds or if any dimension + * in the interval [0, DimensionsCount()) excluding the dimension at the given + * index is negative, or if the product would overflow size_t. Returns true + * otherwise. Returns 1 if the shape has only one dimension. + */ + bool CheckedFlatSizeSkipDim(int skip_dim, size_t& flat_size) const; + + bool operator!=(const RuntimeShape& comp) const { return !((*this) == comp); } + + private: + // For use only by ExtendedShape(), written to guarantee (return-value) copy + // elision in C++17. + // This creates a shape padded to the desired size with the specified value. + RuntimeShape(int new_shape_size, const RuntimeShape& shape, int pad_value) +#ifndef TF_LITE_STATIC_MEMORY + : size_(0) { + // If the following check fails, it is likely because a 4D-only kernel is + // being used with an array of larger dimension count. + TFLITE_DCHECK_GE(new_shape_size, shape.DimensionsCount()); + Resize(new_shape_size); +#else + : size_(new_shape_size) { + // If the following check fails, it is likely because a 4D-only kernel is + // being used with an array of larger dimension count. + TFLITE_DCHECK_GE(new_shape_size, shape.DimensionsCount()); +#endif // TF_LITE_STATIC_MEMORY + const int size_increase = new_shape_size - shape.DimensionsCount(); + for (int i = 0; i < size_increase; ++i) { + SetDim(i, pad_value); + } + std::memcpy(DimsData() + size_increase, shape.DimsData(), + sizeof(int32_t) * shape.DimensionsCount()); + } + + // Number of dimensions in the shape. + // size_t * sizeof(int32_t) is the number of bytes to allocate which should + // not exceed the maximum value of size_t. + int32_t size_; + union { + int32_t dims_[kMaxSmallSize]; +#ifndef TF_LITE_STATIC_MEMORY + int32_t* dims_pointer_; +#endif // TF_LITE_STATIC_MEMORY + }; +}; + +// Converts inference-style shape to legacy tflite::Dims<4>. +inline mlir::Dims<4> ToRuntimeDims(const mlir::RuntimeShape& array_shape) { + mlir::Dims<4> result; + const int dimensions_count = array_shape.DimensionsCount(); + TFLITE_DCHECK_LE(dimensions_count, 4); + int cum_prod = 1; + for (int i = 0; i < 4; i++) { + const int new_dim = + (i < dimensions_count) ? array_shape.Dims(dimensions_count - 1 - i) : 1; + result.sizes[i] = new_dim; + result.strides[i] = cum_prod; + cum_prod *= new_dim; + } + return result; +} + +#ifndef TF_LITE_STATIC_MEMORY +// TODO(b/80418076): Move to legacy ops file, update invocations. +inline RuntimeShape DimsToShape(const mlir::Dims<4>& dims) { + return RuntimeShape( + {dims.sizes[3], dims.sizes[2], dims.sizes[1], dims.sizes[0]}); +} +#endif // TF_LITE_STATIC_MEMORY + +// Since tensors with '0' in their shape are valid in TF, these offset functions +// allow that as long as the corresponding index is also 0. It is upto the +// calling ops to ensure that they perform verification checks on tensor shapes +// if they don't support a particular behavior. + +inline int Offset(const RuntimeShape& shape, int i0, int i1, int i2, int i3) { + TFLITE_DCHECK_EQ(shape.DimensionsCount(), 4); + const int* dims_data = reinterpret_cast(shape.DimsDataUpTo5D()); + TFLITE_DCHECK((dims_data[0] == 0 && i0 == 0) || + (i0 >= 0 && i0 < dims_data[0])); + TFLITE_DCHECK((dims_data[1] == 0 && i1 == 0) || + (i1 >= 0 && i1 < dims_data[1])); + TFLITE_DCHECK((dims_data[2] == 0 && i2 == 0) || + (i2 >= 0 && i2 < dims_data[2])); + TFLITE_DCHECK((dims_data[3] == 0 && i3 == 0) || + (i3 >= 0 && i3 < dims_data[3])); + return ((i0 * dims_data[1] + i1) * dims_data[2] + i2) * dims_data[3] + i3; +} + +inline int Offset(const RuntimeShape& shape, int i0, int i1, int i2, int i3, + int i4) { + TFLITE_DCHECK_EQ(shape.DimensionsCount(), 5); + const int* dims_data = reinterpret_cast(shape.DimsDataUpTo5D()); + TFLITE_DCHECK((dims_data[0] == 0 && i0 == 0) || + (i0 >= 0 && i0 < dims_data[0])); + TFLITE_DCHECK((dims_data[1] == 0 && i1 == 0) || + (i1 >= 0 && i1 < dims_data[1])); + TFLITE_DCHECK((dims_data[2] == 0 && i2 == 0) || + (i2 >= 0 && i2 < dims_data[2])); + TFLITE_DCHECK((dims_data[3] == 0 && i3 == 0) || + (i3 >= 0 && i3 < dims_data[3])); + TFLITE_DCHECK((dims_data[4] == 0 && i4 == 0) || + (i4 >= 0 && i4 < dims_data[4])); + return (((i0 * dims_data[1] + i1) * dims_data[2] + i2) * dims_data[3] + i3) * + dims_data[4] + + i4; +} + +inline int Offset(const RuntimeShape& shape, int* index) { + return Offset(shape, index[0], index[1], index[2], index[3]); +} + +} // namespace mlir + +// LINT.ThenChange(//tensorflow/lite/kernels/internal/runtime_shape.h) + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_KERNELS_INTERNAL_RUNTIME_SHAPE_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/runtime_shape_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/runtime_shape_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..c0278f9fc3c6d38117ff1eb901077c93754dbe11 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/runtime_shape_test.cc @@ -0,0 +1,408 @@ +/* Copyright 2023 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. +==============================================================================*/ +// This file is the MLIR copy of runtime_shape as part of the effort to +// decouple TFLite from MLIR. +// LINT.IfChange + +#include "tensorflow/compiler/mlir/lite/kernels/internal/runtime_shape.h" + +#include +#include +#include +#include +#include + +#include +#include +#include "absl/algorithm/container.h" +#include "absl/types/span.h" + +using testing::Each; +using testing::ElementsAreArray; + +namespace mlir { +namespace { + +constexpr int kSmallSize = RuntimeShape::kMaxSmallSize; +constexpr int kBigSize = RuntimeShape::kMaxSmallSize + 1; + +std::vector IotaVector(int size, int start = 0) { + std::vector vec(size); + absl::c_iota(vec, start); + return vec; +} + +absl::Span AsSpan(const RuntimeShape& shape) { + return absl::Span(shape.DimsData(), shape.DimensionsCount()); +} + +class RuntimeShapeTest : public testing::TestWithParam {}; + +TEST(RuntimeShapeTest, TestDefaultConstructor) { + const RuntimeShape shape; + EXPECT_EQ(shape.DimensionsCount(), 0); +} + +TEST_P(RuntimeShapeTest, TestConstructorWithSize) { + const int size = GetParam(); + const RuntimeShape shape(size); + EXPECT_EQ(shape.DimensionsCount(), size); +} + +TEST_P(RuntimeShapeTest, TestConstructorWithSizeAndDefaultValue) { + const int size = GetParam(); + const RuntimeShape shape(size, 34); + EXPECT_EQ(shape.DimensionsCount(), size); + EXPECT_THAT(AsSpan(shape), Each(34)); +} + +TEST_P(RuntimeShapeTest, TestConstructorFromCArray) { + const int size = GetParam(); + const std::vector src = IotaVector(size); + const RuntimeShape shape(size, src.data()); + EXPECT_EQ(shape.DimensionsCount(), size); + EXPECT_THAT(AsSpan(shape), ElementsAreArray(src)); +} + +TEST(RuntimeShapeTest, TestConstructorFromSmallInitList) { + std::initializer_list init{1, 2, 3}; + // Ensure we are testing a small initializer list. + ASSERT_LE(init.size(), RuntimeShape::kMaxSmallSize); + const RuntimeShape shape(init); + EXPECT_EQ(shape.DimensionsCount(), init.size()); + EXPECT_THAT(AsSpan(shape), ElementsAreArray(init)); +} + +TEST(RuntimeShapeTest, TestConstructorFromBigInitList) { + std::initializer_list init{1, 2, 3, 4, 5, 6, 7, 8, 9}; + // Ensure we are testing a big initializer list. + ASSERT_GT(init.size(), RuntimeShape::kMaxSmallSize); + const RuntimeShape shape(init); + EXPECT_EQ(shape.DimensionsCount(), init.size()); + EXPECT_THAT(AsSpan(shape), ElementsAreArray(init)); +} + +TEST_P(RuntimeShapeTest, TestCopyConstructorFromShape) { + const int size = GetParam(); + const RuntimeShape src(size, 34); + const RuntimeShape dst(src); + EXPECT_EQ(dst.DimensionsCount(), src.DimensionsCount()); + EXPECT_THAT(AsSpan(dst), ElementsAreArray(AsSpan(src))); +} + +TEST_P(RuntimeShapeTest, TestEqualityOperator) { + const int size = GetParam(); + const RuntimeShape shape1(size, 34); + const RuntimeShape shape2(size, 34); + EXPECT_TRUE(shape1 == shape2); + EXPECT_FALSE(shape1 != shape2); +} + +TEST_P(RuntimeShapeTest, TestEqualityOperatorDifferentSizes) { + const int size = GetParam(); + const RuntimeShape shape1(size, 34); + const RuntimeShape shape2(size + 1, 34); + EXPECT_FALSE(shape1 == shape2); + EXPECT_TRUE(shape1 != shape2); +} + +TEST_P(RuntimeShapeTest, TestEqualityOperatorDifferentValues) { + const int size = GetParam(); + const RuntimeShape shape1(size, 34); + const RuntimeShape shape2(size, 43); + EXPECT_FALSE(shape1 == shape2); + EXPECT_TRUE(shape1 != shape2); +} + +TEST_P(RuntimeShapeTest, TestSetterGetter) { + const int size = GetParam(); + RuntimeShape shape(size); + for (int i = 0; i < size; ++i) { + shape.SetDim(i, i); + EXPECT_EQ(shape.Dims(i), i); + } + EXPECT_THAT(AsSpan(shape), ElementsAreArray(IotaVector(size))); +} + +TEST(RuntimeShapeTest, TestResizeSmallSmall) { + ASSERT_GE(kSmallSize, 1); + RuntimeShape shape(kSmallSize - 1, 23); + shape.Resize(kSmallSize); + EXPECT_EQ(shape.DimensionsCount(), kSmallSize); + EXPECT_THAT(absl::Span(shape.DimsData(), kSmallSize - 1), + Each(23)); +} + +TEST(RuntimeShapeTest, TestResizeSmallBig) { + RuntimeShape shape(kSmallSize, 23); + shape.Resize(kBigSize); + EXPECT_EQ(shape.DimensionsCount(), kBigSize); + EXPECT_THAT(absl::Span(shape.DimsData(), kSmallSize), + Each(23)); +} + +TEST(RuntimeShapeTest, TestResizeBigSmall) { + RuntimeShape shape(kBigSize, 23); + shape.Resize(kSmallSize); + EXPECT_EQ(shape.DimensionsCount(), kSmallSize); + EXPECT_THAT(absl::Span(shape.DimsData(), kSmallSize), + Each(23)); +} + +TEST(RuntimeShapeTest, TestResizeDownBigBig) { + RuntimeShape shape(kBigSize + 3, 23); + shape.Resize(kBigSize); + EXPECT_EQ(shape.DimensionsCount(), kBigSize); + EXPECT_THAT(absl::Span(shape.DimsData(), kBigSize), Each(23)); +} + +TEST(RuntimeShapeTest, TestResizeUpBigBig) { + RuntimeShape shape(kBigSize, 23); + shape.Resize(kBigSize + 1); + EXPECT_EQ(shape.DimensionsCount(), kBigSize + 1); + EXPECT_THAT(absl::Span(shape.DimsData(), kBigSize), Each(23)); +} + +TEST_P(RuntimeShapeTest, TestReplaceWith) { + static_assert( + RuntimeShape::kMaxSmallSize > 2, + "kMaxSmallSize should be greater than 2 for this test to work."); + const int size = GetParam(); + for (const int offset : {-2, 2}) { + const std::vector src = + IotaVector(offset + RuntimeShape::kMaxSmallSize); + RuntimeShape shape(size); + shape.ReplaceWith(src.size(), src.data()); + EXPECT_EQ(shape.DimensionsCount(), src.size()); + EXPECT_THAT(AsSpan(shape), testing::ElementsAreArray(src)); + } +} + +TEST_P(RuntimeShapeTest, TestBuildFrom) { + const int size = GetParam(); + const std::vector src = IotaVector(size); + RuntimeShape shape; + shape.BuildFrom(src); + EXPECT_EQ(shape.DimensionsCount(), src.size()); + EXPECT_THAT(AsSpan(shape), testing::ElementsAreArray(src)); +} + +TEST(RuntimeShapeTest, TestExtendedShapeSmall) { + ASSERT_GE(kSmallSize, 2); + const std::vector dims = IotaVector(kSmallSize - 2); + const RuntimeShape src(dims.size(), dims.data()); + const RuntimeShape extended = RuntimeShape::ExtendedShape(kSmallSize, src); + EXPECT_EQ(extended.DimensionsCount(), kSmallSize); + EXPECT_EQ(extended.Dims(0), 1); + EXPECT_EQ(extended.Dims(1), 1); + EXPECT_THAT(absl::Span(extended.DimsData() + 2, dims.size()), + ElementsAreArray(dims)); +} + +TEST(RuntimeShapeTest, TestExtendedShapeBig) { + ASSERT_GE(kSmallSize, 2); + const std::vector dims = IotaVector(kBigSize); + const RuntimeShape src(dims.size(), dims.data()); + const RuntimeShape extended = RuntimeShape::ExtendedShape(kBigSize + 2, src); + EXPECT_EQ(extended.DimensionsCount(), kBigSize + 2); + EXPECT_EQ(extended.Dims(0), 1); + EXPECT_EQ(extended.Dims(1), 1); + EXPECT_THAT(absl::Span(extended.DimsData() + 2, dims.size()), + ElementsAreArray(dims)); +} + +TEST(RuntimeShapeTest, TestExtendedShapeSmallToBig) { + const std::vector dims = IotaVector(kSmallSize); + const RuntimeShape src(dims.size(), dims.data()); + const RuntimeShape extended = RuntimeShape::ExtendedShape(kBigSize, src); + EXPECT_EQ(extended.DimensionsCount(), kBigSize); + EXPECT_THAT( + absl::Span(extended.DimsData(), kBigSize - kSmallSize), + Each(1)); + EXPECT_THAT(absl::Span( + extended.DimsData() + kBigSize - kSmallSize, dims.size()), + ElementsAreArray(dims)); +} + +TEST_P(RuntimeShapeTest, TestFlatSize) { + const std::vector src = IotaVector(kSmallSize); + const RuntimeShape shape(src.size(), src.data()); + int32_t flat_size = 1; + for (std::vector::const_iterator it = src.begin(); it != src.end(); ++it) + flat_size *= *it; + EXPECT_EQ(shape.FlatSize(), flat_size); +} + +TEST(RuntimeShapeTest, TestCheckedFlatSize) { + const RuntimeShape shape({2, 3, 4}); + size_t flat_size = 0; + EXPECT_TRUE(shape.CheckedFlatSize(flat_size)); + EXPECT_EQ(flat_size, 24u); +} + +TEST(RuntimeShapeTest, TestCheckedFlatSizeScalar) { + const RuntimeShape shape; + size_t flat_size = 0; + EXPECT_TRUE(shape.CheckedFlatSize(flat_size)); + EXPECT_EQ(flat_size, 1u); +} + +TEST(RuntimeShapeTest, TestCheckedFlatSizeRejectsNegativeDim) { + const RuntimeShape shape({2, -3, 4}); + size_t flat_size = 0; + EXPECT_FALSE(shape.CheckedFlatSize(flat_size)); +} + +TEST(RuntimeShapeTest, TestCheckedFlatSizeRejectsOverflow) { + const RuntimeShape shape({std::numeric_limits::max(), + std::numeric_limits::max(), + std::numeric_limits::max()}); + size_t flat_size = 0; + EXPECT_FALSE(shape.CheckedFlatSize(flat_size)); +} + +TEST(RuntimeShapeTest, TestCheckedNumElementsInRange) { + const RuntimeShape shape({2, 3, 4, 5}); + size_t flat_size = 0; + EXPECT_TRUE( + shape.CheckedNumElementsInRange(/*start=*/1, /*end=*/3, flat_size)); + EXPECT_EQ(flat_size, 12u); +} + +TEST(RuntimeShapeTest, TestCheckedNumElementsInRangeToInt) { + const RuntimeShape shape({2, 3, 4, 5}); + int flat_size = 0; + EXPECT_TRUE( + shape.CheckedNumElementsInRange(/*start=*/1, /*end=*/3, flat_size)); + EXPECT_EQ(flat_size, 12); +} + +TEST(RuntimeShapeTest, TestCheckedNumElementsInRangeAllowsEmptyRange) { + const RuntimeShape shape({2, 3, 4}); + size_t flat_size = 0; + EXPECT_TRUE( + shape.CheckedNumElementsInRange(/*start=*/1, /*end=*/1, flat_size)); + EXPECT_EQ(flat_size, 1u); +} + +TEST(RuntimeShapeTest, TestCheckedNumElementsInRangeRejectsInvalidRange) { + const RuntimeShape shape({2, 3, 4}); + size_t flat_size = 0; + EXPECT_FALSE( + shape.CheckedNumElementsInRange(/*start=*/-1, /*end=*/2, flat_size)); + EXPECT_FALSE( + shape.CheckedNumElementsInRange(/*start=*/2, /*end=*/1, flat_size)); + EXPECT_FALSE( + shape.CheckedNumElementsInRange(/*start=*/0, /*end=*/4, flat_size)); +} + +TEST(RuntimeShapeTest, TestCheckedNumElementsInRangeRejectsNegativeDim) { + const RuntimeShape shape({2, -3, 4}); + size_t flat_size = 0; + EXPECT_FALSE( + shape.CheckedNumElementsInRange(/*start=*/0, /*end=*/3, flat_size)); +} + +TEST(RuntimeShapeTest, TestCheckedNumElementsInRangeRejectsOverflow) { + const RuntimeShape shape({std::numeric_limits::max(), + std::numeric_limits::max(), + std::numeric_limits::max()}); + size_t flat_size = 0; + EXPECT_FALSE( + shape.CheckedNumElementsInRange(/*start=*/0, /*end=*/3, flat_size)); +} + +TEST(RuntimeShapeTest, TestCheckedNumElementsInRangeToIntRejectsOverflow) { + const RuntimeShape shape({std::numeric_limits::max(), + std::numeric_limits::max()}); + int flat_size = 0; + EXPECT_FALSE( + shape.CheckedNumElementsInRange(/*start=*/0, /*end=*/2, flat_size)); +} + +TEST(RuntimeShapeTest, TestCheckedSizeToDimension) { + const RuntimeShape shape({2, 3, 4, 5}); + size_t flat_size = 0; + EXPECT_TRUE(shape.CheckedSizeToDimension(/*end=*/3, flat_size)); + EXPECT_EQ(flat_size, 24u); +} + +TEST(RuntimeShapeTest, TestCheckedSizeFromDimension) { + const RuntimeShape shape({2, 3, 4, 5}); + size_t flat_size = 0; + EXPECT_TRUE(shape.CheckedSizeFromDimension(/*start=*/1, flat_size)); + EXPECT_EQ(flat_size, 60u); +} + +TEST(RuntimeShapeTest, TestCheckedFlatSizeSkipDim) { + const RuntimeShape shape({2, 3, 4, 5}); + size_t flat_size = 0; + EXPECT_TRUE(shape.CheckedFlatSizeSkipDim(/*skip_dim=*/2, flat_size)); + EXPECT_EQ(flat_size, 30u); +} + +TEST(RuntimeShapeTest, TestCheckedFlatSizeSkipDimRejectsInvalidSkipDim) { + const RuntimeShape shape({2, 3, 4}); + size_t flat_size = 0; + EXPECT_FALSE(shape.CheckedFlatSizeSkipDim(/*skip_dim=*/-1, flat_size)); + EXPECT_FALSE(shape.CheckedFlatSizeSkipDim(/*skip_dim=*/3, flat_size)); +} + +TEST(RuntimeShapeTest, TestCheckedFlatSizeSkipDimRejectsNegativeDim) { + const RuntimeShape shape({2, -3, 4}); + size_t flat_size = 0; + EXPECT_FALSE(shape.CheckedFlatSizeSkipDim(/*skip_dim=*/0, flat_size)); +} + +TEST(RuntimeShapeTest, TestCheckedFlatSizeSkipDimRejectsOverflow) { + const RuntimeShape shape({std::numeric_limits::max(), + std::numeric_limits::max(), + std::numeric_limits::max(), + std::numeric_limits::max()}); + size_t flat_size = 0; + EXPECT_FALSE(shape.CheckedFlatSizeSkipDim(/*skip_dim=*/0, flat_size)); +} + +#ifndef NDEBUG +TEST(RuntimeShapeTest, NegativeDimensionsCountInConstructor) { + EXPECT_DEATH(RuntimeShape shape(-1), ""); +} + +TEST(RuntimeShapeTest, NegativeDimensionsCountInConstructorWithValue) { + EXPECT_DEATH(RuntimeShape shape(-1, 0), ""); +} + +TEST(RuntimeShapeTest, NegativeDimensionsCountInResize) { + RuntimeShape shape; + EXPECT_DEATH(shape.Resize(-1), ""); +} + +TEST(RuntimeShapeTest, NegativeDimensionsCountInReplaceWith) { + RuntimeShape shape; + int32_t dims_data[] = {1, 2, 3}; + EXPECT_DEATH(shape.ReplaceWith(-1, dims_data), ""); +} +#endif + +INSTANTIATE_TEST_SUITE_P(BigSmall, RuntimeShapeTest, + testing::Values(kSmallSize, kBigSize), + [](const testing::TestParamInfo& info) { + return info.param == kSmallSize ? "Small" : "Big"; + }); + +} // namespace +} // namespace mlir + +// LINT.ThenChange(//tensorflow/lite/kernels/internal/runtime_shape_test.cc) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/utils/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/utils/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..0200cc8241e0dcdc263821945307e8a2552253e6 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/utils/BUILD @@ -0,0 +1,27 @@ +load("@rules_cc//cc:cc_library.bzl", "cc_library") +load("//tensorflow:tensorflow.default.bzl", "get_compatible_with_portable") +load("//tensorflow/compiler/mlir/lite:build_def.bzl", "tflite_copts") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + default_visibility = [ + "//visibility:public", + ], + licenses = ["notice"], +) + +# LINT.IfChange(sparsity_format_converter) + +cc_library( + name = "sparsity_format_converter", + srcs = ["sparsity_format_converter.cc"], + hdrs = ["sparsity_format_converter.h"], + compatible_with = get_compatible_with_portable(), + copts = tflite_copts(), + deps = [ + "//tensorflow/compiler/mlir/lite/core/c:tflite_common", + "@eigen_archive//:eigen3", + ], +) + +# LINT.ThenChange(//tensorflow/lite/kernels/internal/utils/BUILD) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/utils/sparsity_format_converter.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/utils/sparsity_format_converter.cc new file mode 100644 index 0000000000000000000000000000000000000000..57aeed19c978eaba67e91c3f9c789245effbbf51 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/utils/sparsity_format_converter.cc @@ -0,0 +1,204 @@ +/* Copyright 2020 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/compiler/mlir/lite/kernels/internal/utils/sparsity_format_converter.h" + +#include +#include +#include + +#include "Eigen/Core" // from @eigen_archive +#include "tensorflow/compiler/mlir/lite/core/c/tflite_types.h" + +namespace tflite_migration { +namespace internal { +namespace sparsity { + +// LINT.IfChange + +template +FormatConverter::FormatConverter( + const std::vector& shape, const std::vector& traversal_order, + const std::vector& format, + const std::vector& block_size, const std::vector& block_map) + : dense_shape_(shape), + traversal_order_(traversal_order), + block_size_(block_size), + block_map_(block_map) { + dense_size_ = 1; + int block_dim = 0; + blocked_shape_.resize(shape.size()); + format_.resize(shape.size() + block_map.size()); + for (int i = 0; i < shape.size(); i++) { + format_[i] = format[traversal_order[i]]; + dense_size_ *= shape[i]; + if (block_dim < block_map.size() && block_map[block_dim] == i) { + blocked_shape_[i] = shape[i] / block_size[block_dim]; + block_dim++; + } else { + blocked_shape_[i] = shape[i]; + } + } + + // Only dense blocks are supported. + for (int i = 0; i < block_map.size(); i++) { + format_[i + shape.size()] = kTfLiteDimDense; + } +} + +template +void FormatConverter::DenseToSparse(const T* src_data) { + int num_original_dims = dense_shape_.size(); + int num_block_dims = block_map_.size(); + int num_expanded_dims = num_original_dims + num_block_dims; + std::vector expanded_shape(num_expanded_dims); + for (int i = 0; i < num_expanded_dims; i++) { + if (i < num_original_dims) { + expanded_shape[i] = blocked_shape_[i]; + } else { + expanded_shape[i] = block_size_[i - num_original_dims]; + } + } + + std::vector shape_offset(num_original_dims); + shape_offset[shape_offset.size() - 1] = 1; + for (int i = num_original_dims - 1; i > 0; --i) { + shape_offset[i - 1] = shape_offset[i] * dense_shape_[i]; + } + + std::vector expanded_shape_offset(num_expanded_dims); + for (int i = 0; i < num_original_dims; ++i) { + expanded_shape_offset[i] = shape_offset[i]; + } + for (int i = 0; i < num_block_dims; ++i) { + int mapped_dim = block_map_[i]; + expanded_shape_offset[num_original_dims + i] = shape_offset[mapped_dim]; + expanded_shape_offset[mapped_dim] *= block_size_[i]; + } + + std::vector dst_ordered_offset(num_expanded_dims); + for (int i = 0; i < num_expanded_dims; ++i) { + dst_ordered_offset[i] = expanded_shape_offset[traversal_order_[i]]; + } + + std::vector dst_dim_has_nonzeroes(num_expanded_dims); + std::fill(dst_dim_has_nonzeroes.begin(), dst_dim_has_nonzeroes.end(), false); + std::vector inner_compressed_dim(num_expanded_dims); + int most_recent_compressed_dim = -1; + std::vector num_segments_of_next_compressed_dim(num_expanded_dims); + int segment_count = 1; + for (int i = num_expanded_dims - 1; i >= 0; --i) { + inner_compressed_dim[i] = most_recent_compressed_dim; + if (format_[i] == kTfLiteDimSparseCSR) { + most_recent_compressed_dim = i; + num_segments_of_next_compressed_dim[i] = segment_count; + segment_count = 1; + } else { + num_segments_of_next_compressed_dim[i] = -1; + segment_count *= expanded_shape[traversal_order_[i]]; + } + } + + dim_metadata_.resize(num_expanded_dims * 2); + std::vector dst_sparse_dims; + dst_sparse_dims.reserve(num_expanded_dims); + for (int i = 0; i < num_expanded_dims; ++i) { + dim_metadata_[i * 2].clear(); + dim_metadata_[i * 2 + 1].clear(); + if (format_[i] == kTfLiteDimDense) { + // If dimension is dense, just store the shape. + dim_metadata_[i * 2].push_back(expanded_shape[traversal_order_[i]]); + } else { + dim_metadata_[i * 2].push_back(0); // Segment array always begins with 0. + dst_sparse_dims.push_back(i); // Add dimension to the sparse list. + } + } + + // This algorithm assumes that the block size is small enough for all the + // elements to fit in cache, so the strided accesses from different traversal + // order and the write-first-erase-later strategy shouldn't be too slow + int dst_dim_idx = num_expanded_dims; + std::vector coordinate(num_expanded_dims, 0); + int dense_tensor_idx = 0; + while (dst_dim_idx >= 0) { + if (dst_dim_idx == num_expanded_dims) { + // We have a complete coordinate. Add the element to the value array if it + // is not zero, or if the last dimension is dense. + if (!IsZero(src_data[dense_tensor_idx])) { + data_.push_back(src_data[dense_tensor_idx]); + // Mark all sparse dimensions that their current indices have nonzeroes. + for (auto dst_dim : dst_sparse_dims) { + if (!dst_dim_has_nonzeroes[dst_dim]) { + // Only add the index to the indices array if the current nonzero + // is the first nonzero of the block. + dim_metadata_[2 * dst_dim + 1].push_back(coordinate[dst_dim]); + dst_dim_has_nonzeroes[dst_dim] = true; + } + } + } else if (format_[num_expanded_dims - 1] == kTfLiteDimDense) { + data_.push_back(src_data[dense_tensor_idx]); + } + --dst_dim_idx; + } else { + int original_dim_idx = traversal_order_[dst_dim_idx]; + int dim_size = expanded_shape[original_dim_idx]; + if (dst_dim_has_nonzeroes[dst_dim_idx]) { + // If the previous block has nonzeroes, reset the flag to false since + // we have just moved to a new block. + dst_dim_has_nonzeroes[dst_dim_idx] = false; + } else if (format_[dst_dim_idx] == kTfLiteDimSparseCSR) { + // This block is empty. Delete unnecessary values if compressed. + int next_compressed_dim = inner_compressed_dim[dst_dim_idx]; + int erase_offset = dim_metadata_[2 * dst_dim_idx + 1].size() * + num_segments_of_next_compressed_dim[dst_dim_idx]; + if (next_compressed_dim >= 0) { + auto& segments = dim_metadata_[2 * inner_compressed_dim[dst_dim_idx]]; + segments.erase(segments.begin() + 1 + erase_offset, segments.end()); + } else { + data_.erase(data_.begin() + erase_offset, data_.end()); + } + } + if (++coordinate[dst_dim_idx] < dim_size) { + // The current dst_dim_idx is valid (not out of bound). + dense_tensor_idx += dst_ordered_offset[dst_dim_idx]; + ++dst_dim_idx; + } else { + // dst_dim_idx has reached its dim size. Update segment array and go + // back to incrementing the previous dimension (dst_dim_idx - 1). + if (format_[dst_dim_idx] == kTfLiteDimSparseCSR) { + dim_metadata_[2 * dst_dim_idx].push_back( + dim_metadata_[2 * dst_dim_idx + 1].size()); + } + coordinate[dst_dim_idx] = -1; + dense_tensor_idx -= dst_ordered_offset[dst_dim_idx] * dim_size; + --dst_dim_idx; + } + } + } +} + +template +bool FormatConverter::IsZero(const T val) { + return (val == static_cast(0)); +} + +template class FormatConverter; +template class FormatConverter; +template class FormatConverter; + +// LINT.ThenChange(//tensorflow/lite/kernels/internal/utils/sparsity_format_converter.cc) + +} // namespace sparsity +} // namespace internal +} // namespace tflite_migration diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/utils/sparsity_format_converter.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/utils/sparsity_format_converter.h new file mode 100644 index 0000000000000000000000000000000000000000..44cace3f9f545f7b6aef0ae22ccc27689c5d703b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/internal/utils/sparsity_format_converter.h @@ -0,0 +1,102 @@ +/* Copyright 2020 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_KERNELS_INTERNAL_UTILS_SPARSITY_FORMAT_CONVERTER_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_KERNELS_INTERNAL_UTILS_SPARSITY_FORMAT_CONVERTER_H_ + +#include + +#include "Eigen/Core" // from @eigen_archive +#include "tensorflow/compiler/mlir/lite/core/c/tflite_types.h" + +namespace tflite_migration { +namespace internal { +namespace sparsity { + +// LINT.IfChange + +// A converter that keeps an internal representation of sparse tensor parameters +// and converts tensors between dense and sparse formats. +template +class FormatConverter { + public: + /* + * Creates a dense to sparse converter. + * @param shape Shape of the dense tensor. + * @param traversal_order In what order to traverse all dimensions, + * including block dimensions. + * @param format Whether each dimension in the dense tensor is + * dense or sparse (not in the traversal order). + * @param block_size Size of each block dimension. + * @param block_map Map from block dimension to original tensor + * dimension. + */ + FormatConverter(const std::vector& shape, + const std::vector& traversal_order, + const std::vector& format, + const std::vector& block_size = {}, + const std::vector& block_map = {}); + + const std::vector& GetData() { return data_; } + + const std::vector>& GetDimMetadata() { + return dim_metadata_; + } + + // Method for dense to sparse conversion. Need to call GetData() method to get + // the compressed data. + + void DenseToSparse(const T* src_data); + + // Check if val is equal to zero. + bool IsZero(T val); + + // Shape of the conceptual dense tensor. + std::vector dense_shape_; + // Shape of the dense tensor with inner blocks reduced. For example, a (4, 4) + // tensor with (2, 2) block has blocked_shape (2, 2). + std::vector blocked_shape_; + // Total number of elements in the dense tensor. + size_t dense_size_; + // Has n(original dimension)+k(block_dimension) elements. + std::vector traversal_order_; + // Format of each dimension in the traversal order. + std::vector format_; + // Size of each block dimension, in the same order as block map. + std::vector block_size_; + // Map from block dimension to the original tensor dimension. + std::vector block_map_; + // Metadata of each dimension in the traversal order. + // Each dimension needs two vectors. For dense dimensions, the first vector + // stores the size of that dimension, and the second vector is empty. For + // sparse dimensions, the first vector stores the segments and the second one + // stores the indices. + std::vector> dim_metadata_; + // Actual buffer holding data after conversion. Could be sparse buffer or + // dense buffer. + std::vector data_; +}; + +extern template class FormatConverter; +extern template class FormatConverter; +extern template class FormatConverter; +extern template class FormatConverter; + +// LINT.ThenChange(//tensorflow/lite/kernels/internal/utils/sparsity_format_converter.h) + +} // namespace sparsity +} // namespace internal +} // namespace tflite_migration + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_KERNELS_INTERNAL_UTILS_SPARSITY_FORMAT_CONVERTER_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/padding.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/padding.h new file mode 100644 index 0000000000000000000000000000000000000000..621532436785e30ef91f99bf65d44b72300b105a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/kernels/padding.h @@ -0,0 +1,59 @@ +/* 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_KERNELS_PADDING_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_KERNELS_PADDING_H_ + +// LINT.IfChange +#include "tensorflow/compiler/mlir/lite/core/c/builtin_op_data.h" + +namespace tflite_migration { + +// Matching GetWindowedOutputSize in TensorFlow. +inline int ComputeOutSize(TfLitePadding padding, int image_size, + int filter_size, int stride, int dilation_rate = 1) { + int effective_filter_size = (filter_size - 1) * dilation_rate + 1; + + // TODO(b/186448822): This uses 0 since the function has no other way to + // report error case + if (stride == 0) return 0; + + switch (padding) { + case kTfLitePaddingSame: + return (image_size + stride - 1) / stride; + case kTfLitePaddingValid: + return (image_size + stride - effective_filter_size) / stride; + default: + return 0; + } +} + +// It's not guaranteed that padding is symmetric. It's important to keep +// offset for algorithms need all paddings. +inline int ComputePaddingWithOffset(int stride, int dilation_rate, int in_size, + int filter_size, int out_size, + int* offset) { + int effective_filter_size = (filter_size - 1) * dilation_rate + 1; + int total_padding = + ((out_size - 1) * stride + effective_filter_size - in_size); + total_padding = total_padding > 0 ? total_padding : 0; + *offset = total_padding % 2; + return total_padding / 2; +} + +} // namespace tflite_migration + +// LINT.ThenChange(//tensorflow/lite/kernels/padding.h) + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_KERNELS_PADDING_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/litert_mlir_opt_main.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/litert_mlir_opt_main.cc new file mode 100644 index 0000000000000000000000000000000000000000..929d4d6a5d55fad34b773de49baab70f7461bbc3 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/litert_mlir_opt_main.cc @@ -0,0 +1,38 @@ +/* Copyright 2025 Google Inc. 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 "llvm/Support/LogicalResult.h" +#include "mlir/InitAllPasses.h" // from @llvm-project +#include "mlir/Tools/mlir-opt/MlirOptMain.h" // from @llvm-project +#include "tensorflow/compiler/mlir/init_mlir.h" +#include "tensorflow/compiler/mlir/lite/register_lite_dialects.h" +#include "tensorflow/compiler/mlir/lite/transforms/passes.h" +#include "tensorflow/compiler/mlir/register_common_dialects.h" +#include "tensorflow/compiler/mlir/tensorflow/transforms/passes.h" + +int main(int argc, char** argv) { + tensorflow::InitMlir y(&argc, &argv); + + mlir::registerAllPasses(); + mlir::registerTensorFlowPasses(); + mlir::TFL::registerTensorFlowLitePasses(); + + mlir::DialectRegistry registry; + mlir::RegisterCommonToolingDialects(registry); + tflite::RegisterLiteToolingDialects(registry); + + return failed( + mlir::MlirOptMain(argc, argv, "TensorFlow pass driver\n", registry)); +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/metrics/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/metrics/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..139cd36976b231a9b876f40071be8f72fa4970fc --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/metrics/BUILD @@ -0,0 +1,100 @@ +load("@rules_python//python:proto.bzl", "py_proto_library") +load("//tensorflow:tensorflow.bzl", "tf_cc_test") +load( + "//tensorflow/core/platform:build_config.bzl", + "tf_proto_library", +) +load("//tensorflow/core/platform:rules_cc.bzl", "cc_library") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + default_visibility = [ + "//tensorflow:__pkg__", + "//tensorflow/compiler/mlir/lite:__subpackages__", + ], + licenses = ["notice"], +) + +cc_library( + name = "error_collector", + srcs = ["error_collector.cc"], + hdrs = ["error_collector.h"], + deps = [ + ":converter_error_data_proto_cc", + ":types_util", + "@com_google_absl//absl/strings", + ], +) + +cc_library( + name = "error_collector_inst", + srcs = ["error_collector_inst.cc"], + hdrs = ["error_collector_inst.h"], + deps = [ + ":converter_error_data_proto_cc", + ":error_collector", + ":types_util", + "@com_google_absl//absl/strings", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + ], +) + +tf_cc_test( + name = "error_collector_inst_test", + srcs = ["error_collector_inst_test.cc"], + data = [ + "testdata/strided_slice.mlir", + ], + deps = [ + ":converter_error_data_proto_cc", + ":error_collector", + ":error_collector_inst", + ":types_util", + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/core:test", + "//tensorflow/core/platform:errors", + "//tensorflow/core/platform:resource_loader", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_googletest//:gtest_main", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Parser", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + "@xla//xla/tsl/platform:statusor", + ], +) + +cc_library( + name = "types_util", + srcs = ["types_util.cc"], + hdrs = ["types_util.h"], + deps = [ + ":converter_error_data_proto_cc", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + ], +) + +tf_proto_library( + name = "converter_error_data_proto", + srcs = ["converter_error_data.proto"], + visibility = [ + "//visibility:public", + ], +) + +# copybara:uncomment_begin(google-only) +# py_proto_library( +# name = "converter_error_data_proto_py", +# visibility = [ +# "//visibility:public", +# ], +# deps = [":converter_error_data_proto"], +# ) +# copybara:uncomment_end diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/metrics/converter_error_data.proto b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/metrics/converter_error_data.proto new file mode 100644 index 0000000000000000000000000000000000000000..edd59a2fc77450b5d0140ab0cac4d25da56d3923 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/metrics/converter_error_data.proto @@ -0,0 +1,94 @@ +// Copyright 2021 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. + +// This schema defines the converter error format to communicate between C++ +// and python. + +syntax = "proto2"; + +package tflite.metrics; + +message ConverterErrorData { + // Error code for popular errors. + enum ErrorCode { + UNKNOWN = 0; + ERROR_NEEDS_FLEX_OPS = 1; + ERROR_NEEDS_CUSTOM_OPS = 2; + ERROR_UNSUPPORTED_CONTROL_FLOW_V1 = 3; + ERROR_STATEFUL_PARTITIONED_CALL_IN_FINAL_IR = 4; + + // 200- 209 error codes are reserved for backend(delegate) compatibility. + // Backend compatibility is checked at MlirToFlatBufferTranslateFunction() + // with the converted flatbuffer model. If some nodes are incompatibile with + // the given backends in TocoFlags.supported_backends, the error will be + // raised. + ERROR_GPU_NOT_COMPATIBLE = 200; + } + + // Information about the op where the error occurs. + message Operator { + // The op name has "." format, Ex: "tf.Abs". + optional string name = 1; + } + + // Represents the type of location. + enum LocationType { + // No location information available. + UNKNOWNLOC = 0; + // The location is the nodename; + NAMELOC = 1; + // The location is a stacktrace. + CALLSITELOC = 2; + // The location is a fused location, usually represents the list of output + // tensor locations of that node. + FUSEDLOC = 3; + } + + // Represents a source location with file name, line and column number. + message FileLoc { + optional string filename = 1; + optional uint32 line = 2; + optional uint32 column = 3; + } + + // Represents the node name and its source location. + message SourceLoc { + optional string name = 1; + optional FileLoc source = 2; + } + + // Represents the location information of current node. + message Location { + optional LocationType type = 1; + // For each location type, this field is different. If type is: + // - UNKNOWNLOC: call is empty. + // - NAMELOC: call has a single element representing the current node. + // - CALLSITELOC: call is a chain of source locations representing a + // stacktrace. + // - FUSEDLOC: call is a list, represents the list of output tensor + // locations. + repeated SourceLoc call = 2; + } + + // The name of the component from which the error was originally thrown. + optional string component = 1; + // The name of the subcomponent from which the error was originally thrown. In + // MLIR, this field contains the pass name. + optional string subcomponent = 2; + + optional ErrorCode error_code = 3; + optional string error_message = 4; + optional Operator operator = 5; + optional Location location = 6; +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/metrics/error_collector.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/metrics/error_collector.cc new file mode 100644 index 0000000000000000000000000000000000000000..6202ad8166eed5b6c54ab0d12d2a1eed4e1e3df8 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/metrics/error_collector.cc @@ -0,0 +1,30 @@ +/* Copyright 2021 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/compiler/mlir/lite/metrics/error_collector.h" + +namespace mlir { +namespace TFL { + +ErrorCollector* ErrorCollector::error_collector_instance_ = nullptr; + +ErrorCollector* ErrorCollector::GetErrorCollector() { + if (error_collector_instance_ == nullptr) { + error_collector_instance_ = new ErrorCollector(); + } + return error_collector_instance_; +} + +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/metrics/error_collector.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/metrics/error_collector.h new file mode 100644 index 0000000000000000000000000000000000000000..037f89c5e5d6044cb194c4a24cf08512c2812678 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/metrics/error_collector.h @@ -0,0 +1,58 @@ +/* Copyright 2021 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_METRICS_ERROR_COLLECTOR_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_METRICS_ERROR_COLLECTOR_H_ + +#include +#include +#include + +#include "tensorflow/compiler/mlir/lite/metrics/converter_error_data.pb.h" +#include "tensorflow/compiler/mlir/lite/metrics/types_util.h" + +namespace mlir { +namespace TFL { + +// A singleton to store errors collected by the instrumentation. +class ErrorCollector { + using ConverterErrorData = tflite::metrics::ConverterErrorData; + using ConverterErrorDataSet = + std::unordered_set; + + public: + const ConverterErrorDataSet &CollectedErrors() { return collected_errors_; } + + void ReportError(const ConverterErrorData &error) { + collected_errors_.insert(error); + } + + // Clear the set of collected errors. + void Clear() { collected_errors_.clear(); } + + // Returns the global instance of ErrorCollector. + static ErrorCollector* GetErrorCollector(); + + private: + ErrorCollector() = default; + + ConverterErrorDataSet collected_errors_; + + static ErrorCollector* error_collector_instance_; +}; + +} // namespace TFL +} // namespace mlir +#endif // TENSORFLOW_COMPILER_MLIR_LITE_METRICS_ERROR_COLLECTOR_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/metrics/error_collector_inst.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/metrics/error_collector_inst.cc new file mode 100644 index 0000000000000000000000000000000000000000..0e0277efd6381fc1cbf5dff9246d3b890ef2c4a9 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/metrics/error_collector_inst.cc @@ -0,0 +1,147 @@ +/* Copyright 2021 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/compiler/mlir/lite/metrics/error_collector_inst.h" + +#include +#include +#include + +#include "absl/strings/match.h" +#include "absl/strings/str_split.h" +#include "mlir/IR/Diagnostics.h" // from @llvm-project +#include "mlir/IR/Location.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/metrics/error_collector.h" +#include "tensorflow/compiler/mlir/lite/metrics/types_util.h" + +namespace mlir { +namespace TFL { +namespace { + +// The signature contains namespaces (Ex: mlir::TFL::(anonymous namespace)::). +// So only extract the function name as the pass name. +inline std::string extract_pass_name(const std::string &signature) { + const std::vector &v = absl::StrSplit(signature, "::"); + return v.back(); +} + +// Errors raised by emitOpError start with "'.' op". Returns an +// empty string if the pattern is not found or the operator is not in tf or tfl +// dialect. +inline std::string extract_op_name_from_error_message( + const std::string &error_message) { + int end_pos = error_message.find("' op"); + if ((absl::StartsWith(error_message, "'tf.") || + absl::StartsWith(error_message, "'tfl.")) && + end_pos != std::string::npos) { + return error_message.substr(1, end_pos - 1); + } + return ""; +} + +// Only notes with character count smaller than kMaxAcceptedNoteSize will be +// appended to the error message. +const int kMaxAcceptedNoteSize = 1024; +} // namespace + +ErrorCollectorInstrumentation::ErrorCollectorInstrumentation( + MLIRContext *context) + : error_collector_(ErrorCollector::GetErrorCollector()) { + handler_ = std::make_unique( + context, [this](Diagnostic &diag) { + if (diag.getSeverity() == DiagnosticSeverity::Error) { + Location loc = diag.getLocation(); + std::string error_message = diag.str(); + std::string op_name, error_code; + if (loc_to_name_.count(loc)) { + op_name = loc_to_name_[loc]; + } else { + op_name = extract_op_name_from_error_message(diag.str()); + } + + for (const auto ¬e : diag.getNotes()) { + const std::string note_str = note.str(); + if (absl::StartsWith(note_str, kErrorCodePrefix)) { + error_code = note_str.substr(sizeof(kErrorCodePrefix) - 1); + } + + error_message += "\n"; + if (note_str.size() <= kMaxAcceptedNoteSize) { + error_message += note_str; + } else { + error_message += note_str.substr(0, kMaxAcceptedNoteSize); + error_message += "..."; + } + } + + ErrorCode error_code_enum = ConverterErrorData::UNKNOWN; + bool has_valid_error_code = + ConverterErrorData::ErrorCode_Parse(error_code, &error_code_enum); + if (!op_name.empty() || has_valid_error_code) { + error_collector_->ReportError(NewConverterErrorData( + pass_name_, error_message, error_code_enum, op_name, loc)); + } else { + common_error_message_ += diag.str(); + common_error_message_ += "\n"; + } + } + return failure(); + }); +} + +void ErrorCollectorInstrumentation::runBeforePass(Pass *pass, + Operation *module) { + // Find the op names with tf or tfl dialect prefix, Ex: "tf.Abs" or "tfl.Abs". + auto collectOps = [this](Operation *op) { + const auto &op_name = op->getName().getStringRef().str(); + if (absl::StartsWith(op_name, "tf.") || absl::StartsWith(op_name, "tfl.")) { + loc_to_name_.emplace(op->getLoc(), op_name); + } + }; + + for (auto ®ion : module->getRegions()) { + region.walk(collectOps); + } + + pass_name_ = extract_pass_name(pass->getName().str()); + error_collector_->Clear(); +} + +void ErrorCollectorInstrumentation::runAfterPass(Pass *pass, + Operation *module) { + loc_to_name_.clear(); + pass_name_.clear(); + common_error_message_.clear(); + error_collector_->Clear(); +} + +void ErrorCollectorInstrumentation::runAfterPassFailed(Pass *pass, + Operation *module) { + // Create a new error if no errors collected yet. + if (error_collector_->CollectedErrors().empty() && + !common_error_message_.empty()) { + error_collector_->ReportError(NewConverterErrorData( + pass_name_, common_error_message_, ConverterErrorData::UNKNOWN, + /*op_name=*/"", module->getLoc())); + } + + loc_to_name_.clear(); + pass_name_.clear(); + common_error_message_.clear(); +} + +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/metrics/error_collector_inst.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/metrics/error_collector_inst.h new file mode 100644 index 0000000000000000000000000000000000000000..8399cfc43bf37044bc883ad5367664ce49936b2f --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/metrics/error_collector_inst.h @@ -0,0 +1,78 @@ +/* Copyright 2021 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_METRICS_ERROR_COLLECTOR_INST_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_METRICS_ERROR_COLLECTOR_INST_H_ + +#include +#include +#include +#include + +#include "mlir/IR/Diagnostics.h" // from @llvm-project +#include "mlir/IR/Location.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/Pass/PassInstrumentation.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/metrics/converter_error_data.pb.h" +#include "tensorflow/compiler/mlir/lite/metrics/error_collector.h" +#include "tensorflow/compiler/mlir/lite/metrics/types_util.h" + +namespace mlir { +namespace TFL { + +// Collects errors when running the pass manager. +class ErrorCollectorInstrumentation : public PassInstrumentation { + using ConverterErrorData = tflite::metrics::ConverterErrorData; + using ErrorCode = ConverterErrorData::ErrorCode; + + public: + explicit ErrorCollectorInstrumentation(MLIRContext *context); + + private: + // Instrumentation hooks. These hooks don't need to be thread-safe. The pass + // manager runs each pass for the entire module, then it walks through + // each op in the module and runs the pass on them, may be in async mode. + void runBeforePass(Pass *pass, Operation *module) override; + void runAfterPass(Pass *pass, Operation *module) override; + void runAfterPassFailed(Pass *pass, Operation *module) override; + + // The handler to capture error messages. + std::unique_ptr handler_; + // A map from location to op name. + std::unordered_map loc_to_name_; + // Stores the error message for errors without op name and error code. + std::string common_error_message_; + // Name of the running pass. + std::string pass_name_; + // Pointer to the global ErrorCollector instance. + ErrorCollector *error_collector_; +}; + +// Prefix when adding error code as a note in Diagnostic. +constexpr char kErrorCodePrefix[] = "Error code: "; + +// Adds error code to a newly created InFlightDiagnostic. +inline InFlightDiagnostic AttachErrorCode(InFlightDiagnostic &&diag, + int error_code) { + using tflite::metrics::ConverterErrorData; + diag.attachNote() << kErrorCodePrefix + << ConverterErrorData::ErrorCode_Name(error_code); + return std::move(diag); +} + +} // namespace TFL +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_METRICS_ERROR_COLLECTOR_INST_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/metrics/error_collector_inst_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/metrics/error_collector_inst_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..3cdb73d31278fbe6bb70d6c6529182da75f38910 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/metrics/error_collector_inst_test.cc @@ -0,0 +1,214 @@ +/* Copyright 2021 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/compiler/mlir/lite/metrics/error_collector_inst.h" + +#include +#include +#include +#include + +#include +#include +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "llvm/Support/SMLoc.h" +#include "llvm/Support/SourceMgr.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/DialectRegistry.h" // from @llvm-project +#include "mlir/IR/Location.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/OwningOpRef.h" // from @llvm-project +#include "mlir/Parser/Parser.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassManager.h" // from @llvm-project +#include "mlir/Support/FileUtilities.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Support/TypeID.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/metrics/converter_error_data.pb.h" +#include "tensorflow/compiler/mlir/lite/metrics/error_collector.h" +#include "tensorflow/compiler/mlir/lite/metrics/types_util.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_dialect.h" +#include "xla/tsl/platform/statusor.h" +#include "tensorflow/core/platform/errors.h" +#include "tensorflow/core/platform/resource_loader.h" +#include "tensorflow/core/platform/test.h" + +namespace mlir { +namespace TFL { +namespace { +using tsl::StatusOr; + +// MockSuccessPass reports errors but doesn't fail. +class MockSuccessPass + : public PassWrapper> { + void getDependentDialects(DialectRegistry& registry) const override { + registry.insert(); + } + + public: + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(MockSuccessPass) + + explicit MockSuccessPass() = default; + + private: + void runOnOperation() override { + getOperation().walk([](Operation* nestedOp) { + nestedOp->emitError() + << "Error at " << nestedOp->getName().getStringRef().str() << " op"; + }); + }; +}; + +// MockFailurePass reports errors and fails. +class MockFailurePass + : public PassWrapper> { + void getDependentDialects(DialectRegistry& registry) const override { + registry.insert(); + } + + public: + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(MockFailurePass) + + explicit MockFailurePass() = default; + + private: + void runOnOperation() override { + getOperation().walk([](Operation* nestedOp) { + if (nestedOp->getName().getStringRef().str().rfind("tf.") != -1) { + AttachErrorCode( + nestedOp->emitError() + << "Failed at " << nestedOp->getName().getStringRef().str() + << " op", + tflite::metrics::ConverterErrorData::ERROR_NEEDS_FLEX_OPS); + } + }); + signalPassFailure(); + }; +}; + +absl::StatusOr> LoadModule( + MLIRContext* context, const std::string& file_name) { + std::string error_message; + auto file = openInputFile(file_name, &error_message); + if (!file) { + return absl::InvalidArgumentError("fail to open input file"); + } + + llvm::SourceMgr source_mgr; + source_mgr.AddNewSourceBuffer(std::move(file), llvm::SMLoc()); + return OwningOpRef( + parseSourceFile(source_mgr, context)); +} + +TEST(ErrorCollectorTest, TessSuccessPass) { + std::string input_file = tensorflow::GetDataDependencyFilepath( + "tensorflow/compiler/mlir/lite/metrics/testdata/strided_slice.mlir"); + MLIRContext context; + context.getOrLoadDialect(); + context.getOrLoadDialect(); + context.enableMultithreading(); + + auto module = LoadModule(&context, input_file); + EXPECT_EQ(module.ok(), true); + + PassManager pm(module.value().get()->getName(), + OpPassManager::Nesting::Implicit); + pm.addPass(std::make_unique()); + + pm.addInstrumentation( + std::make_unique(&context)); + EXPECT_EQ(succeeded(pm.run(module.value().get())), true); + + auto collected_errors = + ErrorCollector::GetErrorCollector()->CollectedErrors(); + EXPECT_EQ(collected_errors.size(), 0); +} + +TEST(ErrorCollectorTest, TessFailurePass) { + using tflite::metrics::ConverterErrorData; + MLIRContext context; + context.getOrLoadDialect(); + context.getOrLoadDialect(); + const std::string input_file = + "tensorflow/compiler/mlir/lite/metrics/testdata/strided_slice.mlir"; + auto input_file_id = StringAttr::get(&context, input_file); + + context.enableMultithreading(); + + auto module = + LoadModule(&context, tensorflow::GetDataDependencyFilepath(input_file)); + EXPECT_EQ(module.ok(), true); + + PassManager pm(module.value().get()->getName(), + OpPassManager::Nesting::Implicit); + pm.addPass(std::make_unique()); + pm.addPass(std::make_unique()); + + pm.addInstrumentation( + std::make_unique(&context)); + EXPECT_EQ(succeeded(pm.run(module.value().get())), false); + + auto collected_errors = + ErrorCollector::GetErrorCollector()->CollectedErrors(); + + EXPECT_EQ(collected_errors.size(), 3); + EXPECT_EQ(collected_errors.count(NewConverterErrorData( + "MockFailurePass", + "Failed at tf.Const op\nsee current operation: %0 = " + "\"tf.Const\"() <{value = dense<1> : tensor<4xi32>}> : () -> " + "tensor<4xi32>\nError code: ERROR_NEEDS_FLEX_OPS", + ConverterErrorData::ERROR_NEEDS_FLEX_OPS, "tf.Const", + mlir::FileLineColLoc::get(input_file_id, 2, 9))), + 1); + EXPECT_EQ(collected_errors.count(NewConverterErrorData( + "MockFailurePass", + "Failed at tf.Const op\nsee current operation: %1 = " + "\"tf.Const\"() <{value = dense<0> : tensor<4xi32>}> : () -> " + "tensor<4xi32>\nError code: ERROR_NEEDS_FLEX_OPS", + ConverterErrorData::ERROR_NEEDS_FLEX_OPS, "tf.Const", + mlir::FileLineColLoc::get(input_file_id, 2, 9))), + 1); + EXPECT_EQ( + collected_errors.count(NewConverterErrorData( + "MockFailurePass", + "Failed at tf.StridedSlice op\nsee current operation: %2 = " + "\"tf.StridedSlice\"(%arg0, %1, %1, %0) <{begin_mask = 11 : " + "i64, ellipsis_mask = 0 : i64, end_mask = 11 : i64, new_axis_mask = " + "4 : i64, shrink_axis_mask = 0 : i64}> {device = \"\"} : " + "(tensor<*xf32>, tensor<4xi32>, tensor<4xi32>, tensor<4xi32>) " + "-> tensor<*xf32>\nError code: ERROR_NEEDS_FLEX_OPS", + ConverterErrorData::ERROR_NEEDS_FLEX_OPS, "tf.StridedSlice", + mlir::FileLineColLoc::get(input_file_id, 4, 10))), + 1); + + // Check the location information. + std::vector locations; + for (const auto& error : collected_errors) { + EXPECT_TRUE(error.has_location()); + locations.push_back(error.location().DebugString()); + } + + EXPECT_THAT(locations, Each(testing::HasSubstr("CALLSITELOC"))); + EXPECT_THAT(locations, Each(testing::HasSubstr(input_file))); + EXPECT_THAT(locations, Contains(testing::HasSubstr("line: 2"))); + EXPECT_THAT(locations, Contains(testing::HasSubstr("column: 9"))); + EXPECT_THAT(locations, Contains(testing::HasSubstr("line: 4"))); + EXPECT_THAT(locations, Contains(testing::HasSubstr("column: 10"))); +} +} // namespace +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/metrics/testdata/strided_slice.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/metrics/testdata/strided_slice.mlir new file mode 100644 index 0000000000000000000000000000000000000000..4d5e6bbac566f9888f1720cd50a6d0cedeb8f0e9 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/metrics/testdata/strided_slice.mlir @@ -0,0 +1,6 @@ +func.func @strided_slice(%arg0 : tensor<*xf32>) -> tensor<*xf32> { + %18 = "tf.Const"() {value = dense<1> : tensor<4xi32>} : () -> tensor<4xi32> + %57 = "tf.Const"() {value = dense<0> : tensor<4xi32>} : () -> tensor<4xi32> + %534 = "tf.StridedSlice"(%arg0, %57, %57, %18) {begin_mask = 11 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 11 : i64, new_axis_mask = 4 : i64, shrink_axis_mask = 0 : i64} : (tensor<*xf32>, tensor<4xi32>, tensor<4xi32>, tensor<4xi32>) -> tensor<*xf32> + "func.return"(%534) : (tensor<*xf32>) -> () +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/metrics/types_util.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/metrics/types_util.cc new file mode 100644 index 0000000000000000000000000000000000000000..156911beab8b36e771056a66b8612ab3a5231fbe --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/metrics/types_util.cc @@ -0,0 +1,133 @@ +/* Copyright 2021 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/compiler/mlir/lite/metrics/types_util.h" + +#include +#include + +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/TypeSwitch.h" +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/Location.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/metrics/converter_error_data.pb.h" + +namespace mlir { +namespace TFL { +namespace { + +// Extracts information from mlir::FileLineColLoc to the proto message +// tflite::metrics::ConverterErrorData::FileLoc. +void ExtractFileLine(const FileLineColLoc& loc, + tflite::metrics::ConverterErrorData::FileLoc* fileline) { + fileline->set_filename(loc.getFilename().str()); + fileline->set_line(loc.getLine()); + fileline->set_column(loc.getColumn()); +} + +// Defines a child class of Location to access its protected members. +class LocationExtractor : public Location { + public: + explicit LocationExtractor(const Location& loc) : Location(loc) {} + + void Extract(tflite::metrics::ConverterErrorData* error_data) { + using tflite::metrics::ConverterErrorData; + auto mutable_location = error_data->mutable_location(); + + llvm::TypeSwitch(impl) + .Case([&](OpaqueLoc loc) { + LocationExtractor(loc.getFallbackLocation()).Extract(error_data); + }) + .Case([&](UnknownLoc loc) { + mutable_location->set_type(ConverterErrorData::UNKNOWNLOC); + }) + .Case([&](FileLineColLoc loc) { + if (!mutable_location->has_type()) { + mutable_location->set_type(ConverterErrorData::CALLSITELOC); + } + auto new_call = mutable_location->mutable_call()->Add(); + ExtractFileLine(loc, new_call->mutable_source()); + }) + .Case([&](NameLoc loc) { + if (!mutable_location->has_type()) { + mutable_location->set_type(ConverterErrorData::NAMELOC); + } + + auto new_call = mutable_location->mutable_call()->Add(); + new_call->set_name(loc.getName().str()); + // Add child as the source location. + auto child_loc = loc.getChildLoc(); + if (mlir::isa(child_loc)) { + auto typed_child_loc = mlir::dyn_cast(child_loc); + ExtractFileLine(typed_child_loc, new_call->mutable_source()); + } + }) + .Case([&](CallSiteLoc loc) { + mutable_location->set_type(ConverterErrorData::CALLSITELOC); + LocationExtractor(loc.getCallee()).Extract(error_data); + LocationExtractor(loc.getCaller()).Extract(error_data); + }) + .Case([&](FusedLoc loc) { + auto locations = loc.getLocations(); + size_t num_locs = locations.size(); + // Skip the first location if it stores information for propagating + // op_type metadata. + if (num_locs > 0) { + if (auto name_loc = mlir::dyn_cast(locations[0])) { + if (name_loc.getName().strref().ends_with(":")) { + if (num_locs == 2) { + return LocationExtractor(locations[1]).Extract(error_data); + } else if (num_locs > 2) { + locations = {locations.begin() + 1, locations.end()}; + } + } + } + } + + mutable_location->set_type(ConverterErrorData::FUSEDLOC); + llvm::interleave( + locations, + [&](Location l) { LocationExtractor(l).Extract(error_data); }, + [&]() {}); + }); + } +}; +} // namespace + +tflite::metrics::ConverterErrorData NewConverterErrorData( + const std ::string& pass_name, const std::string& error_message, + tflite::metrics::ConverterErrorData::ErrorCode error_code, + const std::string& op_name, const Location& location) { + using tflite::metrics::ConverterErrorData; + ConverterErrorData error; + if (!pass_name.empty()) { + error.set_subcomponent(pass_name); + } + + if (!error_message.empty()) { + error.set_error_message(error_message); + } + + if (!op_name.empty()) { + error.mutable_operator_()->set_name(op_name); + } + + error.set_error_code(error_code); + LocationExtractor(location).Extract(&error); + return error; +} + +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/metrics/types_util.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/metrics/types_util.h new file mode 100644 index 0000000000000000000000000000000000000000..2ae3f61ed27c63e51ffe5ed2326589123ea1049c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/metrics/types_util.h @@ -0,0 +1,71 @@ +/* Copyright 2021 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_METRICS_TYPES_UTIL_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_METRICS_TYPES_UTIL_H_ + +#include +#include +#include + +#include "mlir/IR/Location.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/metrics/converter_error_data.pb.h" + +namespace mlir { +namespace TFL { + +// The hash function for mlir::Location. +struct LocationHash { + std::size_t operator()(const Location& v) const noexcept { + return hash_value(v); + } +}; + +// The hash function for ConverterErrorData. +struct ConverterErrorDataHash { + std::size_t operator()( + const tflite::metrics::ConverterErrorData& v) const noexcept { + std::size_t hash_result = std::hash{}(v.error_message()); + if (v.has_subcomponent()) { + hash_result ^= std::hash{}(v.subcomponent()) << 1; + } + if (v.has_error_code()) { + hash_result ^= std::hash{}(v.error_code()) << 2; + } + if (v.has_operator_() && v.operator_().has_name()) { + hash_result ^= std::hash{}(v.operator_().name()) << 3; + } + return hash_result; + } +}; + +// The comparison function for ConverterErrorData. +struct ConverterErrorDataComparison { + std::size_t operator()( + const tflite::metrics::ConverterErrorData& a, + const tflite::metrics::ConverterErrorData& b) const noexcept { + return ConverterErrorDataHash()(a) == ConverterErrorDataHash()(b); + } +}; + +// Helper function to create a new ConverterErrorData. +tflite::metrics::ConverterErrorData NewConverterErrorData( + const std ::string& pass_name, const std::string& error_message, + tflite::metrics::ConverterErrorData::ErrorCode error_code, + const std::string& op_name, const Location& location); + +} // namespace TFL +} // namespace mlir +#endif // TENSORFLOW_COMPILER_MLIR_LITE_METRICS_TYPES_UTIL_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/mmap_allocation.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/mmap_allocation.cc new file mode 100644 index 0000000000000000000000000000000000000000..5b8170e412105a321d222c542c568b4303d7293a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/mmap_allocation.cc @@ -0,0 +1,147 @@ +/* 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 +#include +#include +#include +#include + +#include + +#include "tensorflow/compiler/mlir/lite/allocation.h" +#include "tensorflow/compiler/mlir/lite/core/api/error_reporter.h" +#include "tensorflow/lite/util.h" + +namespace tflite { +namespace { + +size_t GetFdSizeBytes(int fd) { + if (fd < 0) { + return 0; + } + + struct stat fd_stat; + if (fstat(fd, &fd_stat) != 0) { + return 0; + } + + return fd_stat.st_size; +} + +} // namespace + +MMAPAllocation::MMAPAllocation(const char* filename, + ErrorReporter* error_reporter, bool map_private) + : MMAPAllocation(error_reporter, open(filename, O_RDONLY), map_private) { + if (mmap_fd_ == -1) { + TF_LITE_REPORT_ERROR(error_reporter, "Could not open '%s'.", filename); + } +} + +MMAPAllocation::MMAPAllocation(int fd, ErrorReporter* error_reporter, + bool map_private) + : MMAPAllocation(error_reporter, dup(fd), map_private) { + if (mmap_fd_ == -1) { + TF_LITE_REPORT_ERROR(error_reporter, "Failed to dup '%d' file descriptor.", + fd); + } +} + +MMAPAllocation::MMAPAllocation(const char* filename, size_t offset, + size_t length, ErrorReporter* error_reporter, + bool map_private) + : MMAPAllocation(error_reporter, open(filename, O_RDONLY), offset, length, + map_private) { + if (mmap_fd_ == -1) { + TF_LITE_REPORT_ERROR(error_reporter, "Could not open '%s'.", filename); + } +} + +MMAPAllocation::MMAPAllocation(int fd, size_t offset, size_t length, + ErrorReporter* error_reporter, bool map_private) + : MMAPAllocation(error_reporter, dup(fd), offset, length, map_private) { + if (mmap_fd_ == -1) { + TF_LITE_REPORT_ERROR(error_reporter, "Failed to dup '%d' file descriptor.", + fd); + } +} + +MMAPAllocation::MMAPAllocation(ErrorReporter* error_reporter, int owned_fd, + bool map_private) + : MMAPAllocation(error_reporter, owned_fd, /*offset=*/0, + /*length=*/GetFdSizeBytes(owned_fd), map_private) {} + +MMAPAllocation::MMAPAllocation(ErrorReporter* error_reporter, int owned_fd, + size_t offset, size_t length, bool map_private) + : Allocation(error_reporter, Allocation::Type::kMMap), + mmap_fd_(owned_fd), + mmapped_buffer_(MAP_FAILED), + buffer_size_bytes_(length) { + if (owned_fd < 0) { + return; + } + + static int pagesize = sysconf(_SC_PAGE_SIZE); + + offset_in_buffer_ = offset % pagesize; + offset_of_buffer_in_file_ = offset - offset_in_buffer_; + + size_t file_size = GetFdSizeBytes(mmap_fd_); + CheckedInt checked_length_offset = + CheckedInt(length) + offset; + if (checked_length_offset.Overflow() || + checked_length_offset.Value() > file_size) { + TF_LITE_REPORT_ERROR(error_reporter, + "Asked to mmap '%d' bytes from fd '%d' at offset " + "'%d'. This is over the length of file '%d'.", + length, mmap_fd_, offset, file_size); + return; + } + + mmapped_buffer_ = mmap(nullptr, /*__len=*/length + offset_in_buffer_, + map_private ? (PROT_READ | PROT_WRITE) : PROT_READ, + map_private ? MAP_PRIVATE : MAP_SHARED, mmap_fd_, + /*__offset=*/offset_of_buffer_in_file_); + if (mmapped_buffer_ == MAP_FAILED) { + TF_LITE_REPORT_ERROR(error_reporter, + "Mmap of '%d' at offset '%d' failed with error '%d'.", + mmap_fd_, offset, errno); + return; + } +} + +MMAPAllocation::~MMAPAllocation() { + if (valid()) { + munmap(const_cast(mmapped_buffer_), + buffer_size_bytes_ + offset_in_buffer_); + } + if (mmap_fd_ >= 0) { + close(mmap_fd_); + } +} + +const void* MMAPAllocation::base() const { + return reinterpret_cast( + reinterpret_cast(mmapped_buffer_) + offset_in_buffer_); +} + +size_t MMAPAllocation::bytes() const { return buffer_size_bytes_; } + +bool MMAPAllocation::valid() const { return mmapped_buffer_ != MAP_FAILED; } + +bool MMAPAllocation::IsSupported() { return true; } + +} // namespace tflite diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/mmap_allocation_disabled.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/mmap_allocation_disabled.cc new file mode 100644 index 0000000000000000000000000000000000000000..e15c0904cca6fe4c1afd420a564dc46f6128ba5b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/mmap_allocation_disabled.cc @@ -0,0 +1,57 @@ +/* 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 + +#include "tensorflow/compiler/mlir/lite/allocation.h" + +namespace tflite { + +MMAPAllocation::MMAPAllocation(const char* filename, + ErrorReporter* error_reporter, bool map_private) + : MMAPAllocation(error_reporter, -1, map_private) {} + +MMAPAllocation::MMAPAllocation(int fd, ErrorReporter* error_reporter, + bool map_private) + : MMAPAllocation(error_reporter, -1, map_private) {} + +MMAPAllocation::MMAPAllocation(int fd, size_t offset, size_t length, + ErrorReporter* error_reporter, bool map_private) + : MMAPAllocation(error_reporter, -1, map_private) {} + +MMAPAllocation::MMAPAllocation(const char* filename, size_t offset, + size_t length, ErrorReporter* error_reporter, + bool map_private) + : MMAPAllocation(error_reporter, -1, map_private) {} + +MMAPAllocation::MMAPAllocation(ErrorReporter* error_reporter, int owned_fd, + bool map_private) + : Allocation(error_reporter, Allocation::Type::kMMap), + mmapped_buffer_(nullptr) { + // The disabled variant should never be created. + assert(false); +} + +MMAPAllocation::~MMAPAllocation() {} + +const void* MMAPAllocation::base() const { return nullptr; } + +size_t MMAPAllocation::bytes() const { return 0; } + +bool MMAPAllocation::valid() const { return false; } + +bool MMAPAllocation::IsSupported() { return false; } + +} // namespace tflite diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/model_flags.proto b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/model_flags.proto new file mode 100644 index 0000000000000000000000000000000000000000..31a26bc766d9ef79a4dbd2951950662461c3dbb5 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/model_flags.proto @@ -0,0 +1,225 @@ +// 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. +syntax = "proto2"; + +package tflite; + +import "tensorflow/compiler/mlir/lite/types.proto"; + +message InputArrayShape { + // Dimensions of the tensor. + repeated int32 dims = 2; + + // If true, the number of dimensions in the shape is unknown. + // + // If true, "dims.size()" must be 0. + optional bool unknown_rank = 3; +} + +// Next ID to USE: 7. +message InputArray { + // Name of the input arrays, i.e. the arrays from which input activations + // will be read. + optional string name = 1; + + // Shape of the input. For many applications the dimensions are {batch, + // height, width, depth}. Often the batch is left "unspecified" by providing + // a value of -1. + // + // The last dimension is typically called 'depth' or 'channels'. For example, + // for an image model taking RGB images as input, this would have the value 3. + optional InputArrayShape shape = 6; + + // mean_value and std_value parameters control the interpretation of raw input + // activation values (elements of the input array) as real numbers. The + // mapping is given by: + // + // real_value = (raw_input_value - mean_value) / std_value + // + // In particular, the defaults (mean_value=0, std_value=1) yield + // real_value = raw_input_value. Often, non-default values are used in image + // models. For example, an image model taking uint8 image channel values as + // its raw inputs, in [0, 255] range, may use mean_value=128, std_value=128 to + // map them into the interval [-1, 1). + // + // Note: this matches exactly the meaning of mean_value and std_value in + // (TensorFlow via LegacyFedInput). + optional float mean_value = 3; + optional float std_value = 4 [default = 1.]; + + // Data type of the input. + // + // In many graphs, the input arrays already have defined data types, + // e.g. Placeholder nodes in a TensorFlow GraphDef have a dtype attribute. + // In those cases, it is not needed to specify this data_type flag. + // The purpose of this flag is only to define the data type of input + // arrays whose type isn't defined in the input graph file. For example, + // when specifying an arbitrary (not Placeholder) --input_array into + // a TensorFlow GraphDef. + // + // When this data_type is quantized (e.g. QUANTIZED_UINT8), the + // corresponding quantization parameters are the mean_value, std_value + // fields. + // + // It is also important to understand the nuance between this data_type + // flag and the inference_input_type in TocoFlags. The basic difference + // is that this data_type (like all ModelFlags) describes a property + // of the input graph, while inference_input_type (like all TocoFlags) + // describes an aspect of the toco transformation process and thus of + // the output file. The types of input arrays may be different between + // the input and output files if quantization or dequantization occurred. + // Such differences can only occur for real-number data i.e. only + // between FLOAT and quantized types (e.g. QUANTIZED_UINT8). + optional IODataType data_type = 5; +} + +message RnnState { + optional string state_array = 1; + optional string back_edge_source_array = 2; + optional bool discardable = 5; + // size allows to specify a 1-D shape for the RNN state array. + // Will be expanded with 1's to fit the model. + // TODO(benoitjacob): should allow a generic, explicit shape. + optional int32 size = 3; + optional int32 num_dims = 4; +} + +// An ArraysExtraInfo message stores a collection of additional Information +// about arrays in a model, complementing the information in the model itself. +// It is intentionally a separate message so that it may be serialized and +// passed separately from the model. See --arrays_extra_info_file. +// +// A typical use case is to manually specify MinMax for specific arrays in a +// model that does not already contain such MinMax information. +message ArraysExtraInfo { + message Entry { + // Next ID to use: 8. + optional string name = 1; + optional string name_regexp = 7; + optional double min = 2; + optional double max = 3; + optional IODataType data_type = 4; + optional InputArrayShape shape = 5; + optional float constant_float_value = 6; + } + repeated Entry entries = 1; +} + +// ModelFlags encodes properties of a model that, depending on the file +// format, may or may not be recorded in the model file. The purpose of +// representing these properties in ModelFlags is to allow passing them +// separately from the input model file, for instance as command-line +// parameters, so that we can offer a single uniform interface that can +// handle files from different input formats. +// +// For each of these properties, and each supported file format, we +// detail in comments below whether the property exists in the given file +// format. +// +// Obsolete flags that have been removed: +// optional int32 input_depth = 3; +// optional int32 input_width = 4; +// optional int32 input_height = 5; +// optional int32 batch = 6 [ default = 1]; +// optional float mean_value = 7; +// optional float std_value = 8 [default = 1.]; +// optional int32 input_dims = 11 [ default = 4]; +// repeated int32 input_shape = 13; +// +// Next ID to USE: 27. +message ModelFlags { + // Information about the input arrays, i.e. the arrays from which input + // activations will be read. + repeated InputArray input_arrays = 1; + + // Name of the output arrays, i.e. the arrays into which output activations + // will be written. + repeated string output_arrays = 2; + + // Name of the control outputs. + repeated string control_output_arrays = 24; + + // If true, the model accepts an arbitrary batch size. Mutually exclusive with + // the 'batch' field: at most one of these two fields can be set. + optional bool variable_batch = 10; + + repeated RnnState rnn_states = 12; + + // Checks applied to the model, typically after toco's comprehensive + // graph transformations. + // Next ID to USE: 4. + message ModelCheck { + // Use the name of a type of operator to check its counts. + // Use "Total" for overall operator counts. + // Use "Arrays" for overall array counts. + optional string count_type = 1 [default = "None"]; + // A count of zero is a meaningful check, so negative used to mean disable. + optional int32 count_min = 2 [default = -1]; + // If count_max < count_min, then count_min is only allowed value. + optional int32 count_max = 3 [default = -1]; + } + repeated ModelCheck model_checks = 14; + + // If true, will allow passing inexistent arrays in --input_arrays + // and --output_arrays. This makes little sense, is only useful to + // more easily get graph visualizations. + optional bool allow_nonexistent_arrays = 16; + + // If true, will allow passing non-ascii-printable characters in + // --input_arrays and --output_arrays. By default (if false), only + // ascii printable characters are allowed, i.e. character codes + // ranging from 32 to 127. This is disallowed by default so as to + // catch common copy-and-paste issues where invisible unicode + // characters are unwittingly added to these strings. + optional bool allow_nonascii_arrays = 17; + + // If set, this ArraysExtraInfo allows to pass extra information about arrays + // not specified in the input model file, such as extra MinMax information. + optional ArraysExtraInfo arrays_extra_info = 18; + + // When set to false, toco will not change the input ranges and the output + // ranges of concat operator to the overlap of all input ranges. + optional bool change_concat_input_ranges = 19 [default = true]; + + // Filepath of the saved model to be converted. This value will be non-empty + // only when the saved model import path will be used. Otherwise, the graph + // def-based conversion will be processed. + optional string saved_model_dir = 20; + + // SavedModel file format version of The saved model file to be converted. + // This value will be set only when the SavedModel import path will be used. + optional int32 saved_model_version = 21; + + // Set of string saved model tags, formatted in the comma-separated value. + // This value will be set only when the SavedModel import path will be used. + repeated string saved_model_tags = 22; + + // Names to be exported (default: export all) when the saved model import path + // is on. This value will be set only when the SavedModel import path will be + // used. + repeated string saved_model_exported_names = 23; + + // Whether or not to use hlo import. + optional bool use_hlo_import = 25; + + // The hlo file type enum. + enum HloFileType { + UNKNOWN = 0; + HLO_TEXT = 1; + HLO_PROTO = 2; + } + + // Hlo file type, this will be used for hlo import. + optional HloFileType hlo_file_type = 26; +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/offset_buffer.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/offset_buffer.h new file mode 100644 index 0000000000000000000000000000000000000000..5bb1ccceaca1ef8bbf38fcef27b01d8dd92932f1 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/offset_buffer.h @@ -0,0 +1,31 @@ +/* Copyright 2023 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_OFFSET_BUFFER_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_OFFSET_BUFFER_H_ + +#include + +namespace tflite { + +// Check if the model is using custom_option_offset to store custom op +// buffers. When this field is not explicitly set by the user, then FlatBuffer +// will omit the field and interpret this as 0, to ensure this field is +// populated. The flatbuffer exporter will always set it to 1, and it's also not +// a valid buffer offset value. So it's only valid when it's > 1. +inline bool IsValidBufferOffset(const int64_t offset) { return offset > 1; } + +} // namespace tflite + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_OFFSET_BUFFER_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/offset_buffer_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/offset_buffer_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..ef660984c0df9b6a66b540933a71129c8cf2362e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/offset_buffer_test.cc @@ -0,0 +1,32 @@ +/* Copyright 2023 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/compiler/mlir/lite/offset_buffer.h" + +#include "tensorflow/core/platform/test.h" + +namespace tflite { +namespace { + +TEST(OffsetBufferTest, IsValidBufferOffsetTrueGreaterThan1) { + EXPECT_TRUE(IsValidBufferOffset(/*offset=*/2)); +} + +TEST(OffsetBufferTest, IsValidBufferOffsetFalseForLessThanOrEqualTo1) { + EXPECT_FALSE(IsValidBufferOffset(/*offset=*/1)); + EXPECT_FALSE(IsValidBufferOffset(/*offset=*/0)); +} + +} // namespace +} // namespace tflite diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..170250342f8315fca6816abb87470fcf1e4e07dd --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/BUILD @@ -0,0 +1,261 @@ +load("@xla//third_party/rules_python/python:py_library.bzl", "py_library") +load("@xla//xla/tsl/platform:build_config_root.bzl", "if_pywrap") +load("//tensorflow:tensorflow.default.bzl", "tf_python_pybind_extension") +load("//tensorflow/core/platform:rules_cc.bzl", "cc_library") + +licenses(["notice"]) + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + default_visibility = [ + ":friends", + "//tensorflow:__pkg__", + ], +) + +package_group( + name = "friends", + packages = [ + "//learning/brain/mobile/lite/tooling/model_analyzer/...", + "//learning/brain/mobile/lite/tools/analyzer/...", + "//tensorflow/lite/python/...", + "//tensorflow/lite/python/converter/...", + "//tensorflow/lite/toco/...", + "//third_party/odml/litert/litert/...", + ], +) + +cc_library( + name = "tf_tfl_flatbuffer_helpers", + srcs = ["tf_tfl_flatbuffer_helpers.cc"], + hdrs = ["tf_tfl_flatbuffer_helpers.h"], + deps = [ + "//tensorflow/compiler/mlir/lite:common", + "//tensorflow/compiler/mlir/lite:converter_flags_proto_cc", + "//tensorflow/compiler/mlir/lite:model_flags_proto_cc", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/lite:tf_to_tfl_flatbuffer", + "//tensorflow/compiler/mlir/lite:types_proto_cc", + "//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib:quantization_config", + "//tensorflow/compiler/mlir/lite/tools/optimize:reduced_precision_metadata", + "//tensorflow/compiler/mlir/quantization/tensorflow/python:py_function_lib", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_ops", + "//tensorflow/core:core_cpu_base", + "//tensorflow/core:framework", + "//tensorflow/core:lib", + "//tensorflow/core:protos_all_cc", + "@com_google_absl//absl/log", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + ], +) + +cc_library( + name = "graphdef_to_tfl_flatbuffer", + srcs = ["graphdef_to_tfl_flatbuffer.cc"], + hdrs = [ + "graphdef_to_tfl_flatbuffer.h", + ], + deps = [ + ":tf_tfl_flatbuffer_helpers", + "//tensorflow/compiler/mlir/lite:common", + "//tensorflow/compiler/mlir/lite:converter_flags_proto_cc", + "//tensorflow/compiler/mlir/lite:model_flags_proto_cc", + "//tensorflow/compiler/mlir/lite:types_proto_cc", + "//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib:quantization_config", + "//tensorflow/compiler/mlir/tensorflow:mlir_roundtrip_flags", + "//tensorflow/compiler/mlir/tensorflow/translate/tools:parsers", + "//tensorflow/compiler/mlir/tf2xla/api/v2:graph_to_tf_executor", + "//tensorflow/core:core_cpu_base", + "//tensorflow/core:framework", + "//tensorflow/core:lib", + "//tensorflow/core:protos_all_cc", + "@com_google_absl//absl/status", + "@llvm-project//mlir:IR", + ], +) + +cc_library( + name = "saved_model_to_tfl_flatbuffer", + srcs = ["saved_model_to_tfl_flatbuffer.cc"], + hdrs = [ + "saved_model_to_tfl_flatbuffer.h", + ], + deps = [ + ":tf_tfl_flatbuffer_helpers", + "//tensorflow/compiler/mlir/lite:common", + "//tensorflow/compiler/mlir/lite:converter_flags_proto_cc", + "//tensorflow/compiler/mlir/lite:model_flags_proto_cc", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/lite:tf_to_tfl_flatbuffer", + "//tensorflow/compiler/mlir/lite:types_proto_cc", + "//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib:quantization_config", + "//tensorflow/compiler/mlir/quantization/tensorflow/python:py_function_lib", + "//tensorflow/compiler/mlir/tensorflow:mlir_roundtrip_flags", + "//tensorflow/core:lib", + "//tensorflow/core:protos_all_cc", + "@com_google_absl//absl/status", + "@com_google_absl//absl/types:span", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + ], +) + +cc_library( + name = "jax_to_tfl_flatbuffer", + srcs = ["jax_to_tfl_flatbuffer.cc"], + hdrs = [ + "jax_to_tfl_flatbuffer.h", + ], + deps = [ + ":tf_tfl_flatbuffer_helpers", + "//tensorflow/compiler/mlir/lite:common", + "//tensorflow/compiler/mlir/lite:converter_flags_proto_cc", + "//tensorflow/compiler/mlir/lite:model_flags_proto_cc", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/lite:types_proto_cc", + "//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib:quantization_config", + "//tensorflow/core:lib", + "//tensorflow/core:protos_all_cc", + "@com_google_absl//absl/log", + "@com_google_absl//absl/status", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/utility", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@xla//xla/hlo/parser:hlo_parser", + "@xla//xla/hlo/translate:stablehlo", + "@xla//xla/service:hlo_proto_cc", + ], +) + +# Smaller version of flatbuffer_translate which only converts flatbuffer to MLIR. +cc_library( + name = "flatbuffer_to_mlir", + srcs = [ + "flatbuffer_to_mlir.cc", + ], + hdrs = [ + "flatbuffer_to_mlir.h", + ], + deps = [ + "//tensorflow/compiler/mlir/lite:flatbuffer_import", + "@com_google_absl//absl/strings:string_view", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TranslateLib", + ], +) + +py_library( + name = "wrap_converter", + srcs = [ + "wrap_converter.py", + ], + strict_deps = True, + visibility = ["//visibility:public"], + deps = [ + ":_pywrap_converter_api", + "//tensorflow/compiler/mlir/quantization/tensorflow/python:py_function_lib_py", + "//tensorflow/python:pywrap_tensorflow", + ] + if_pywrap( + if_true = [] + select({ + "//conditions:default": [], + # This reqirement is for LiteRT only as tensorflow/compiler/mlirt/lite is + # duplicated in LiteRT/tflite/converter. + "//tensorflow:disable_tf_lite_py": [ + "//tensorflow/lite/python:pywrap_tflite", + ], + }), + ), +) + +config_setting( + name = "tflite_convert_with_select_tf_ops", + define_values = {"tflite_convert_with_select_tf_ops": "true"}, + visibility = ["//visibility:private"], +) + +filegroup( + name = "converter_python_api_hdrs", + srcs = [ + "converter_python_api.h", + ], + visibility = ["//visibility:private"], +) + +cc_library( + name = "converter_python_api", + srcs = ["converter_python_api.cc"], + hdrs = ["converter_python_api.h"], + features = ["-parse_headers"], + visibility = [ + "//visibility:public", + ], + deps = [ + ":flatbuffer_to_mlir", + ":graphdef_to_tfl_flatbuffer", + ":jax_to_tfl_flatbuffer", + ":saved_model_to_tfl_flatbuffer", + "//tensorflow/c:kernels", + "//tensorflow/c:tf_status_headers", + "//tensorflow/compiler/mlir/lite:converter_flags_proto_cc", + "//tensorflow/compiler/mlir/lite:model_flags_proto_cc", + "//tensorflow/compiler/mlir/lite:types_proto_cc", + "//tensorflow/compiler/mlir/lite/core:absl_error_model_builder", + "//tensorflow/compiler/mlir/lite/debug:debug_options_proto_cc", + "//tensorflow/compiler/mlir/lite/metrics:error_collector", + "//tensorflow/compiler/mlir/lite/python/interpreter_wrapper:python_error_reporter", + "//tensorflow/compiler/mlir/lite/python/interpreter_wrapper:python_utils", + "//tensorflow/compiler/mlir/lite/quantization/lite:quantize_model", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs", + "//tensorflow/compiler/mlir/lite/sparsity:sparsify_model", + "//tensorflow/compiler/mlir/quantization/tensorflow/python:py_function_lib", + "//tensorflow/core:framework", + "//tensorflow/core:lib", + "//tensorflow/core:protos_all_cc", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/log", + "@com_google_absl//absl/status", + "@com_google_absl//absl/strings:string_view", + "@com_google_protobuf//:protobuf", + "@com_google_protobuf//:protobuf_headers", + "@flatbuffers//:runtime_cc", + "@xla//third_party/python_runtime:headers", # build_cleaner: keep; DNR: b/35864863 + "@xla//xla/tsl/platform:status", + ] + select({ + # This is required when running `tflite_convert` from `bazel`. + # It requires to link with TensorFlow Ops to get the op definitions. + ":tflite_convert_with_select_tf_ops": [ + "//tensorflow/core:ops", + ], + "//conditions:default": [], + }), + alwayslink = True, +) + +tf_python_pybind_extension( + name = "_pywrap_converter_api", + srcs = [ + "converter_python_api_wrapper.cc", + ], + hdrs = [":converter_python_api_hdrs"], + enable_stub_generation = True, + pytype_srcs = [ + "_pywrap_converter_api.pyi", + ], + visibility = ["//visibility:public"], + deps = [ + "//tensorflow/compiler/mlir/quantization/tensorflow/python:py_function_lib", + "//tensorflow/python/lib/core:pybind11_lib", + "@pybind11", + "@xla//third_party/python_runtime:headers", + ] + if_pywrap([":converter_python_api"]), +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/_pywrap_converter_api.pyi b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/_pywrap_converter_api.pyi new file mode 100644 index 0000000000000000000000000000000000000000..1a38c9e82fa5218874995ca44cba67253e56e4d4 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/_pywrap_converter_api.pyi @@ -0,0 +1,21 @@ +# Copyright 2023 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. +# ============================================================================== + +def Convert(model_flags_proto_txt_raw: object, converter_flags_proto_txt_raw: object, input_contents_txt_raw: object, extended_return: bool = ..., debug_info_txt_raw: object = ..., quantization_py_function_library=...) -> object: ... +def ExperimentalMlirQuantizeModel(input_contents_txt_raw: object, disable_per_channel: bool = ..., fully_quantize: bool = ..., inference_type: int = ..., input_data_type: int = ..., output_data_type: int = ..., enable_numeric_verify: bool = ..., enable_whole_model_verify: bool = ..., op_blocklist: object = ..., node_blocklist: object = ..., enable_variable_quantization: bool = ..., disable_per_channel_for_dense_layers: bool = ..., debug_options_proto_txt_raw: object = ...) -> object: ... +def ExperimentalMlirSparsifyModel(input_contents_txt_raw: object) -> object: ... +def FlatBufferToMlir(arg0: str, arg1: bool) -> str: ... +def RegisterCustomOpdefs(custom_opdefs_txt_raw: object) -> object: ... +def RetrieveCollectedErrors() -> list: ... diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/converter_python_api.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/converter_python_api.cc new file mode 100644 index 0000000000000000000000000000000000000000..8b2dcd88014c61334a7e28169ff3d61acfcb4dc6 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/converter_python_api.cc @@ -0,0 +1,440 @@ +/* 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/compiler/mlir/lite/python/converter_python_api.h" + +#include + +#include +#include +#include +#include +#include + +#include "absl/container/flat_hash_set.h" +#include "absl/log/log.h" +#include "absl/status/status.h" +#include "absl/strings/string_view.h" +#include "flatbuffers/flatbuffer_builder.h" // from @flatbuffers +#include "google/protobuf/text_format.h" +#include "tensorflow/c/kernels.h" +#include "tensorflow/c/tf_status.h" +#include "tensorflow/compiler/mlir/lite/converter_flags.pb.h" +#include "tensorflow/compiler/mlir/lite/core/absl_error_model_builder.h" +#include "tensorflow/compiler/mlir/lite/debug/debug_options.pb.h" +#include "tensorflow/compiler/mlir/lite/metrics/error_collector.h" +#include "tensorflow/compiler/mlir/lite/model_flags.pb.h" +#include "tensorflow/compiler/mlir/lite/python/flatbuffer_to_mlir.h" +#include "tensorflow/compiler/mlir/lite/python/graphdef_to_tfl_flatbuffer.h" +#include "tensorflow/compiler/mlir/lite/python/interpreter_wrapper/python_error_reporter.h" +#include "tensorflow/compiler/mlir/lite/python/interpreter_wrapper/python_utils.h" +#include "tensorflow/compiler/mlir/lite/python/jax_to_tfl_flatbuffer.h" +#include "tensorflow/compiler/mlir/lite/python/saved_model_to_tfl_flatbuffer.h" +#include "tensorflow/compiler/mlir/lite/quantization/lite/quantize_model.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" +#include "tensorflow/compiler/mlir/lite/sparsity/sparsify_model.h" +#include "tensorflow/compiler/mlir/lite/types.pb.h" +#include "tensorflow/compiler/mlir/quantization/tensorflow/python/py_function_lib.h" +#include "tensorflow/core/framework/graph.pb.h" +#include "tensorflow/core/framework/graph_debug_info.pb.h" +#include "tensorflow/core/framework/op.h" +#include "tensorflow/core/framework/op_def.pb.h" +#include "tensorflow/core/framework/op_def_builder.h" +#include "tensorflow/core/platform/status.h" + +namespace tflite { + +// NOTE(aselle): We are using raw PyObject's here because we want to make +// sure we input and output bytes rather than unicode strings for Python3. +PyObject* Convert(PyObject* model_flags_proto_txt_raw, + PyObject* converter_flags_proto_txt_raw, + PyObject* input_contents_txt_raw, bool extended_return, + PyObject* debug_info_txt_raw, + const tensorflow::quantization::PyFunctionLibrary* + quantization_py_function_library) { + // Use Python C API to validate and convert arguments. In py3 (bytes), + // in py2 (str). + auto ConvertArg = [&](PyObject* obj, bool* error) { + char* buf; + Py_ssize_t len; + if (mlirlite::python_utils::ConvertFromPyString(obj, &buf, &len) == -1) { + *error = true; + return std::string(); + } else { + *error = false; + return std::string(buf, len); + } + }; + + bool error; + std::string model_flags_proto_txt = + ConvertArg(model_flags_proto_txt_raw, &error); + if (error) { + PyErr_SetString(PyExc_ValueError, "Model flags are invalid."); + return nullptr; + } + std::string converter_flags_proto_txt = + ConvertArg(converter_flags_proto_txt_raw, &error); + if (error) { + PyErr_SetString(PyExc_ValueError, "Converter flags are invalid."); + return nullptr; + } + + // Produce new outputs. + tflite::ModelFlags model_flags; + if (!model_flags.ParseFromString(model_flags_proto_txt)) { + PyErr_SetString(PyExc_ValueError, + "Failed to convert Model to Python String."); + return nullptr; + } + tflite::ConverterFlags converter_flags; + if (!converter_flags.ParseFromString(converter_flags_proto_txt)) { + PyErr_SetString(PyExc_ValueError, + "Failed to convert ConverterFlags to Python String."); + return nullptr; + } + + tensorflow::GraphDebugInfo debug_info; + if (debug_info_txt_raw && debug_info_txt_raw != Py_None) { + std::string debug_info_txt = ConvertArg(debug_info_txt_raw, &error); + if (error) { + PyErr_SetString(PyExc_ValueError, "Input DebugInfo is invalid."); + return nullptr; + } + if (!debug_info.ParseFromString(debug_info_txt)) { + PyErr_SetString(PyExc_ValueError, + "Failed to convert DebugInfo to Python String."); + return nullptr; + } + } + + tensorflow::GraphDef graph_def; + std::string input_contents_txt; + if (model_flags.saved_model_dir().empty()) { + input_contents_txt = ConvertArg(input_contents_txt_raw, &error); + if (error) { + PyErr_SetString(PyExc_ValueError, "Input GraphDef is invalid."); + return nullptr; + } + if (!model_flags.use_hlo_import() && + !graph_def.ParseFromString(input_contents_txt)) { + PyErr_SetString(PyExc_ValueError, + "Failed to convert GraphDef to Python String."); + return nullptr; + } + } + + std::string output_file_contents_txt; + absl::Status status; + + // Convert model. + if (model_flags.use_hlo_import() && model_flags.has_saved_model_dir()) { + PyErr_SetString(PyExc_ValueError, + "Cannot specify both saved_model and hlo import."); + return nullptr; + } + + if (model_flags.use_hlo_import()) { + status = tensorflow::ConvertJaxToTFLiteFlatBuffer( + input_contents_txt, model_flags, converter_flags, + &output_file_contents_txt); + } else if (!model_flags.saved_model_dir().empty()) { + status = tensorflow::ConvertSavedModelToTFLiteFlatBuffer( + model_flags, converter_flags, &output_file_contents_txt, + quantization_py_function_library); + } else { + tensorflow::GraphDef graph_def; + if (!graph_def.ParseFromString(input_contents_txt)) { + PyErr_SetString(PyExc_ValueError, + "Failed to convert GraphDef to Python String."); + return nullptr; + } + + status = tensorflow::ConvertGraphDefToTFLiteFlatBuffer( + model_flags, converter_flags, debug_info, graph_def, + &output_file_contents_txt); + } + + if (!status.ok()) { + PyErr_SetString(PyExc_Exception, absl::StatusMessageAsCStr(status)); + return nullptr; + } + // Convert arguments back to byte (py3) or str (py2) + return mlirlite::python_utils::ConvertToPyString( + output_file_contents_txt.data(), output_file_contents_txt.size()); +} + +tflite::TensorType FromConverterFlagsToTfLiteDType(int inference_type) { + switch (inference_type) { + case tflite::IODataType::QUANTIZED_INT16: + return tflite::TensorType_INT16; + case tflite::IODataType::QUANTIZED_UINT8: + return tflite::TensorType_UINT8; + case tflite::IODataType::UINT8: + return tflite::TensorType_UINT8; + case tflite::IODataType::QUANTIZED_INT8: + return tflite::TensorType_INT8; + case tflite::IODataType::INT8: + return tflite::TensorType_INT8; + default: + return tflite::TensorType_FLOAT32; + } +} + +int ToStringSet(PyObject* py_denylist, + absl::flat_hash_set* string_set) { + using mlirlite::python_utils::ConvertFromPyString; + // Ensure op_denylist is non null + if (!py_denylist) { + return 0; + } + if (PyList_Check(py_denylist)) { + for (int i = 0; i < PyList_GET_SIZE(py_denylist); ++i) { + PyObject* value = PyList_GetItem(py_denylist, i); + char* str_buf; + Py_ssize_t length; + if (ConvertFromPyString(value, &str_buf, &length) == -1) { + return -1; + } + string_set->emplace(str_buf, length); + } + } + if (PySet_Check(py_denylist)) { + auto* tmp = PySet_New(py_denylist); + while (PySet_GET_SIZE(tmp)) { + PyObject* value = PySet_Pop(tmp); + char* str_buf; + Py_ssize_t length; + if (ConvertFromPyString(value, &str_buf, &length) == -1) { + return -1; + } + string_set->emplace(str_buf, length); + } + } + return 0; +} + +PyObject* MlirQuantizeModel(PyObject* data, bool disable_per_channel, + bool fully_quantize, int inference_type, + int input_data_type, int output_data_type, + bool enable_numeric_verify, + bool enable_whole_model_verify, + PyObject* op_denylist, PyObject* node_denylist, + bool enable_variable_quantization, + bool disable_per_channel_for_dense_layers, + PyObject* debug_options_proto_txt_raw) { + using tflite_migration::interpreter_wrapper::PythonErrorReporter; + char* buf = nullptr; + Py_ssize_t length; + std::unique_ptr error_reporter = + std::make_unique(); + + if (mlirlite::python_utils::ConvertFromPyString(data, &buf, &length) == -1) { + PyErr_Format(PyExc_ValueError, "Failed to convert input PyObject"); + return nullptr; + } + + std::optional debug_options = + tensorflow::converter::DebugOptions(); + if (debug_options_proto_txt_raw != nullptr) { + auto ConvertArg = [&](PyObject* obj, bool* error) { + char* buf; + Py_ssize_t len; + if (mlirlite::python_utils::ConvertFromPyString(obj, &buf, &len) == -1) { + *error = true; + return std::string(); + } else { + *error = false; + return std::string(buf, len); + } + }; + + bool error; + std::string debug_options_proto_txt = + ConvertArg(debug_options_proto_txt_raw, &error); + if (error) { + PyErr_SetString(PyExc_ValueError, "Converter flags are invalid."); + return nullptr; + } + + if (!debug_options->ParseFromString(debug_options_proto_txt)) { + PyErr_SetString(PyExc_ValueError, + "Failed to convert ConverterFlags to Python String."); + return nullptr; + } + } else { + debug_options = std::nullopt; + } + + absl::flat_hash_set denylisted_ops; + absl::flat_hash_set denylisted_nodes; + if (ToStringSet(op_denylist, &denylisted_ops) == -1) { + PyErr_Format(PyExc_ValueError, "Failed to convert op denylist PyObject"); + return nullptr; + } + if (ToStringSet(node_denylist, &denylisted_nodes) == -1) { + PyErr_Format(PyExc_ValueError, "Failed to convert node denylist PyObject"); + return nullptr; + } + + std::unique_ptr model = + mlir::TFL::FlatBufferModelAbslError::BuildFromBuffer( + buf, length, error_reporter.get()); + if (!model) { + PyErr_Format(PyExc_ValueError, "Invalid model"); + return nullptr; + } + auto tflite_model = std::make_unique(); + model->GetModel()->UnPackTo(tflite_model.get(), nullptr); + + const tflite::TensorType inference_tensor_type = + FromConverterFlagsToTfLiteDType(inference_type); + const tflite::TensorType input_type = + FromConverterFlagsToTfLiteDType(input_data_type); + const tflite::TensorType output_type = + FromConverterFlagsToTfLiteDType(output_data_type); + + std::string output_model; + const absl::string_view input_model_buffer(buf, length); + auto status = mlir::lite::QuantizeModel( + input_model_buffer, input_type, output_type, inference_tensor_type, + /*operator_names=*/{}, disable_per_channel, fully_quantize, output_model, + enable_numeric_verify, enable_whole_model_verify, + /*legacy_float_scale=*/true, denylisted_ops, denylisted_nodes, + enable_variable_quantization, disable_per_channel_for_dense_layers, + debug_options); + if (!status.ok()) { + LOG(ERROR) << "Failed to quantize model: " << status; + error_reporter->exception(); + return nullptr; + } + + return mlirlite::python_utils::ConvertToPyString(output_model.data(), + output_model.size()); +} + +PyObject* MlirSparsifyModel(PyObject* data) { + using tflite_migration::interpreter_wrapper::PythonErrorReporter; + char* buf = nullptr; + Py_ssize_t length; + std::unique_ptr error_reporter = + std::make_unique(); + + if (mlirlite::python_utils::ConvertFromPyString(data, &buf, &length) == -1) { + PyErr_Format(PyExc_ValueError, "Failed to convert input PyObject"); + return nullptr; + } + std::unique_ptr model = + mlir::TFL::FlatBufferModelAbslError::BuildFromBuffer( + buf, length, error_reporter.get()); + if (!model) { + PyErr_Format(PyExc_ValueError, "Invalid model"); + return nullptr; + } + auto tflite_model = std::make_unique(); + model->GetModel()->UnPackTo(tflite_model.get(), nullptr); + + flatbuffers::FlatBufferBuilder builder; + auto status = mlir::lite::SparsifyModel(*tflite_model, &builder); + + if (!status.ok()) { + error_reporter->exception(); + return nullptr; + } + return mlirlite::python_utils::ConvertToPyString( + reinterpret_cast(builder.GetCurrentBufferPointer()), + builder.GetSize()); +} + +PyObject* RegisterCustomOpdefs(PyObject* list) { + if (!PyList_Check(list)) { + PyErr_SetString(PyExc_TypeError, "Expected list in argument"); + return nullptr; + } + + int64_t size = PyList_Size(list); + for (int i = 0; i < size; ++i) { + // Get character array from Python object. + char* tf_opdefs; + Py_ssize_t len; + if (mlirlite::python_utils::ConvertFromPyString(PyList_GetItem(list, i), + &tf_opdefs, &len) == -1) { + PyErr_Format(PyExc_ValueError, + "Failed to convert Python string at index %d of custom op " + "defs argument", + i); + return nullptr; + } + + // Parse op def from character array. + tensorflow::OpDef opdef; + if (!tensorflow::protobuf::TextFormat::ParseFromString(tf_opdefs, &opdef)) { + PyErr_Format( + PyExc_ValueError, + "Failed to parse opdefs at index %d of custom op defs argument: %s", + i, tf_opdefs); + return nullptr; + } + + // Register extra opdefs to TensorFlow global op registry. + tensorflow::OpRegistry::Global()->Register( + [opdef](tensorflow::OpRegistrationData* op_reg_data) -> absl::Status { + *op_reg_data = tensorflow::OpRegistrationData(opdef); + return absl::OkStatus(); + }); + + // Register the corresponding fake op kernel. + const char* node_name = opdef.name().c_str(); + const char* op_name = opdef.name().c_str(); + const char* device_name = "CPU"; + static auto fake_compute_func = [](void* kernel, TF_OpKernelContext* ctx) { + }; + + TF_KernelBuilder* builder = + TF_NewKernelBuilder(op_name, device_name, /*create_func=*/nullptr, + fake_compute_func, /*delete_func=*/nullptr); + + TF_Status* status = TF_NewStatus(); + TF_RegisterKernelBuilder(node_name, builder, status); + if (TF_GetCode(status) != TF_OK) { + TF_DeleteStatus(status); + PyErr_Format(PyExc_ValueError, + "Failed to register fake op kernel at index %d of custom op " + "defs argument", + i); + return nullptr; + } + TF_DeleteStatus(status); + } + + Py_RETURN_TRUE; +} + +std::vector RetrieveCollectedErrors() { + mlir::TFL::ErrorCollector* collector = + mlir::TFL::ErrorCollector::GetErrorCollector(); + std::vector collected_errors; + for (const auto& error_data : collector->CollectedErrors()) { + collected_errors.push_back(error_data.SerializeAsString()); + } + collector->Clear(); + return collected_errors; +} + +std::string FlatBufferFileToMlir(const std::string& model, + bool input_is_filepath) { + return ::tensorflow::FlatBufferFileToMlir(model, input_is_filepath); +} + +} // namespace tflite diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/converter_python_api.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/converter_python_api.h new file mode 100644 index 0000000000000000000000000000000000000000..dd3967e1383e040fff09cc6a907b0ca423c51f2f --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/converter_python_api.h @@ -0,0 +1,74 @@ +/* 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_PYTHON_CONVERTER_PYTHON_API_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_PYTHON_CONVERTER_PYTHON_API_H_ + +#include + +#include +#include + +#include "tensorflow/compiler/mlir/quantization/tensorflow/python/py_function_lib.h" + +namespace tflite { + +// Convert a model represented in `input_contents`. `model_flags_proto` +// describes model parameters. `flags_proto` describes conversion +// parameters (see relevant .protos for more information). Returns a string +// representing the contents of the converted model. When extended_return +// flag is set to true returns a dictionary that contains string representation +// of the converted model and some statistics like arithmetic ops count. +// `debug_info_str` contains the `GraphDebugInfo` proto. +PyObject* Convert(PyObject* model_flags_proto_txt_raw, + PyObject* converter_flags_proto_txt_raw, + PyObject* input_contents_txt_raw, + bool extended_return = false, + PyObject* debug_info_txt_raw = nullptr, + const tensorflow::quantization::PyFunctionLibrary* + quantization_py_function_library = nullptr); + +// Quantize the model with calibration data. Throw errors if `fully_quantize` +// is specified by the calibration data are not sufficient to quantize the +// model. +PyObject* MlirQuantizeModel(PyObject* data, bool disable_per_channel, + bool fully_quantize, int inference_type, + int input_data_type, int output_data_type, + bool enable_numeric_verify = false, + bool enable_whole_model_verify = false, + PyObject* op_denylist = nullptr, + PyObject* node_denylist = nullptr, + bool enable_variable_quantization = false, + bool disable_per_channel_for_dense_layers = false, + PyObject* debug_options_proto_txt_raw = nullptr); + +// Sparsifies model to encode sparse tensors with proper format. Throws error if +// sparsification fails. +PyObject* MlirSparsifyModel(PyObject* data); + +// Registers the given custom opdefs to TensorFlow global op registry. +PyObject* RegisterCustomOpdefs(PyObject* list); + +// Returns the collected TFLite conversion errors. +std::vector RetrieveCollectedErrors(); + +// Returns MLIR string dump of the given Flatbuffer model. +std::string FlatBufferFileToMlir(const std::string& model, + bool input_is_filepath); + +// All the exported functions should be listed in +// tensorflow/tools/def_file_filter/symbols_pybind.txt for the Windows build. +} // namespace tflite + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_PYTHON_CONVERTER_PYTHON_API_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/converter_python_api_wrapper.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/converter_python_api_wrapper.cc new file mode 100644 index 0000000000000000000000000000000000000000..519cd9940f83c185f91e2327c37028e21f221704 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/converter_python_api_wrapper.cc @@ -0,0 +1,128 @@ +/* Copyright 2015 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 "pybind11/pybind11.h" // from @pybind11 +#include "tensorflow/compiler/mlir/lite/python/converter_python_api.h" +#include "tensorflow/compiler/mlir/quantization/tensorflow/python/py_function_lib.h" +#include "tensorflow/python/lib/core/pybind11_lib.h" + +namespace py = pybind11; + +PYBIND11_MODULE(_pywrap_converter_api, m) { + m.def( + "Convert", + [](py::object model_flags_proto_txt_raw, + py::object converter_flags_proto_txt_raw, + py::object input_contents_txt_raw, bool extended_return, + py::object debug_info_txt_raw, + const tensorflow::quantization::PyFunctionLibrary* + quantization_py_function_library) { + return tensorflow::PyoOrThrow(tflite::Convert( + model_flags_proto_txt_raw.ptr(), + converter_flags_proto_txt_raw.ptr(), input_contents_txt_raw.ptr(), + extended_return, debug_info_txt_raw.ptr(), + quantization_py_function_library)); + }, + py::arg("model_flags_proto_txt_raw"), + py::arg("converter_flags_proto_txt_raw"), + py::arg("input_contents_txt_raw"), py::arg("extended_return") = false, + py::arg("debug_info_txt_raw") = py::none(), + py::arg("quantization_py_function_library") = py::none(), + R"pbdoc( + Convert a model represented in `input_contents`. `model_flags_proto` + describes model parameters. `flags_proto` describes conversion + parameters (see relevant .protos for more information). Returns a string + representing the contents of the converted model. When extended_return + flag is set to true returns a dictionary that contains string representation + of the converted model and some statistics like arithmetic ops count. + `debug_info_str` contains the `GraphDebugInfo` proto. + )pbdoc"); + m.def( + "ExperimentalMlirQuantizeModel", + [](py::object input_contents_txt_raw, bool disable_per_channel, + bool fully_quantize, int inference_type, int input_data_type, + int output_data_type, bool enable_numeric_verify, + bool enable_whole_model_verify, py::object op_blocklist, + py::object node_blocklist, bool enable_variable_quantization, + bool disable_per_channel_for_dense_layers, + py::object debug_options_proto_txt_raw) { + return tensorflow::PyoOrThrow(tflite::MlirQuantizeModel( + input_contents_txt_raw.ptr(), disable_per_channel, fully_quantize, + inference_type, input_data_type, output_data_type, + enable_numeric_verify, enable_whole_model_verify, + op_blocklist.ptr(), node_blocklist.ptr(), + enable_variable_quantization, disable_per_channel_for_dense_layers, + debug_options_proto_txt_raw.ptr())); + }, + py::arg("input_contents_txt_raw"), py::arg("disable_per_channel") = false, + py::arg("fully_quantize") = true, py::arg("inference_type") = 9, + py::arg("input_data_type") = 0, py::arg("output_data_type") = 0, + py::arg("enable_numeric_verify") = false, + py::arg("enable_whole_model_verify") = false, + py::arg("op_blocklist") = py::none(), + py::arg("node_blocklist") = py::none(), + py::arg("enable_variable_quantization") = false, + py::arg("disable_per_channel_for_dense_layers") = false, + py::arg("debug_options_proto_txt_raw") = nullptr, + R"pbdoc( + Returns a quantized model. + )pbdoc"); + m.def( + "ExperimentalMlirSparsifyModel", + [](py::object input_contents_txt_raw) { + return tensorflow::PyoOrThrow( + tflite::MlirSparsifyModel(input_contents_txt_raw.ptr())); + }, + py::arg("input_contents_txt_raw"), + R"pbdoc( + Returns a sparsified model. + )pbdoc"); + m.def( + "RegisterCustomOpdefs", + [](py::object custom_opdefs_txt_raw) { + return tensorflow::PyoOrThrow( + tflite::RegisterCustomOpdefs(custom_opdefs_txt_raw.ptr())); + }, + py::arg("custom_opdefs_txt_raw"), + R"pbdoc( + Registers the given custom opdefs to the TensorFlow global op registry. + )pbdoc"); + m.def( + "RetrieveCollectedErrors", + []() { + std::vector collected_errors = + tflite::RetrieveCollectedErrors(); + pybind11::list serialized_message_list(collected_errors.size()); + int i = 0; + for (const auto& error_data : collected_errors) { + serialized_message_list[i++] = pybind11::bytes(error_data); + } + return serialized_message_list; + }, + R"pbdoc( + Returns and clears the list of collected errors in ErrorCollector. + )pbdoc"); + m.def( + "FlatBufferToMlir", + [](const std::string& model, bool input_is_filepath) { + return tflite::FlatBufferFileToMlir(model, input_is_filepath); + }, + R"pbdoc( + Returns MLIR dump of the given TFLite model. + )pbdoc"); +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/flatbuffer_to_mlir.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/flatbuffer_to_mlir.cc new file mode 100644 index 0000000000000000000000000000000000000000..de08006f5d7bb773f4c8399da3d0abc9987c0a2d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/flatbuffer_to_mlir.cc @@ -0,0 +1,94 @@ +/* Copyright 2021 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 "absl/strings/string_view.h" +#include "llvm/Support/LogicalResult.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/SMLoc.h" +#include "llvm/Support/SourceMgr.h" +#include "llvm/Support/raw_ostream.h" +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/Location.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/IR/Verifier.h" // from @llvm-project +#include "mlir/Support/FileUtilities.h" // from @llvm-project +#include "mlir/Tools/mlir-translate/Translation.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/flatbuffer_import.h" + +namespace tensorflow { +namespace { +static mlir::OwningOpRef FlatBufferFileToMlirTranslation( + llvm::SourceMgr* source_mgr, mlir::MLIRContext* context) { + const llvm::MemoryBuffer* input = + source_mgr->getMemoryBuffer(source_mgr->getMainFileID()); + std::string error; + auto loc = + mlir::FileLineColLoc::get(context, input->getBufferIdentifier(), 0, 0); + std::vector inputs; + std::vector outputs; + return tflite::FlatBufferToMlir( + absl::string_view(input->getBufferStart(), input->getBufferSize()), + context, loc, false, inputs, outputs, false); +} + +} // namespace + +std::string FlatBufferFileToMlir(const std::string& model_file_or_buffer, + bool input_is_filepath) { + // referred logic from mlir::mlirTranslateMain(). + + std::string errorMessage; + std::unique_ptr input; + if (input_is_filepath) { + input = mlir::openInputFile(model_file_or_buffer, &errorMessage); + if (!input) { + llvm::errs() << errorMessage << "\n"; + return ""; + } + } else { + input = llvm::MemoryBuffer::getMemBuffer(model_file_or_buffer, "flatbuffer", + false); + if (!input) { + llvm::errs() << "Can't get llvm::MemoryBuffer\n"; + return ""; + } + } + + mlir::MLIRContext context; + context.printOpOnDiagnostic(true); + llvm::SourceMgr sourceMgr; + sourceMgr.AddNewSourceBuffer(std::move(input), llvm::SMLoc()); + + mlir::OwningOpRef module = + FlatBufferFileToMlirTranslation(&sourceMgr, &context); + if (!module || failed(verify(*module))) return ""; + + std::string mlir_output; + llvm::raw_string_ostream output_stream(mlir_output); + // Dump MLIR with eliding large elements. + module->print( + output_stream, + mlir::OpPrintingFlags().useLocalScope().elideLargeElementsAttrs()); + return mlir_output; +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/flatbuffer_to_mlir.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/flatbuffer_to_mlir.h new file mode 100644 index 0000000000000000000000000000000000000000..869de9f0fc9e417eaff75b4113f19865e5b240d3 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/flatbuffer_to_mlir.h @@ -0,0 +1,30 @@ +/* Copyright 2021 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_PYTHON_FLATBUFFER_TO_MLIR_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_PYTHON_FLATBUFFER_TO_MLIR_H_ + +#include + +namespace tensorflow { + +// Translates the given FlatBuffer filename or buffer into MLIR and returns +// translated MLIR as string. +std::string FlatBufferFileToMlir(const std::string& model_file_or_buffer, + bool input_is_filepath); + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_PYTHON_FLATBUFFER_TO_MLIR_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/graphdef_to_tfl_flatbuffer.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/graphdef_to_tfl_flatbuffer.cc new file mode 100644 index 0000000000000000000000000000000000000000..dcbf3d003e55921dc9500201c04451135d1f1978 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/graphdef_to_tfl_flatbuffer.cc @@ -0,0 +1,133 @@ +/* Copyright 2022 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/compiler/mlir/lite/python/graphdef_to_tfl_flatbuffer.h" + +#include +#include +#include +#include +#include + +#include "absl/status/status.h" +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/common/tfl_pass_config.h" +#include "tensorflow/compiler/mlir/lite/converter_flags.pb.h" +#include "tensorflow/compiler/mlir/lite/model_flags.pb.h" +#include "tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.h" +#include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_config.h" +#include "tensorflow/compiler/mlir/lite/types.pb.h" +#include "tensorflow/compiler/mlir/tensorflow/translate/mlir_roundtrip_flags.h" +#include "tensorflow/compiler/mlir/tensorflow/translate/tools/parsers.h" +#include "tensorflow/compiler/mlir/tf2xla/api/v2/graph_to_tf_executor.h" +#include "xla/tsl/platform/errors.h" +#include "xla/tsl/platform/statusor.h" +#include "tensorflow/core/common_runtime/graph_constructor.h" +#include "tensorflow/core/framework/graph.pb.h" +#include "tensorflow/core/framework/graph_debug_info.pb.h" +#include "tensorflow/core/framework/op.h" +#include "tensorflow/core/framework/types.pb.h" +#include "tensorflow/core/graph/graph.h" + +namespace tensorflow { + +absl::Status ConvertGraphDefToTFLiteFlatBuffer( + const tflite::ModelFlags& model_flags, + tflite::ConverterFlags& converter_flags, const GraphDebugInfo& debug_info, + const GraphDef& input, std::string* result) { + auto context = std::make_unique(); + GraphImportConfig specs; + mlir::TFL::QuantizationSpecs quant_specs; + + // Parse input arrays. + std::vector node_names; + std::vector node_dtypes; + std::vector>> node_shapes; + std::vector> node_mins; + std::vector> node_maxs; + + // Populate quantization specs. + TF_RETURN_IF_ERROR(internal::PopulateQuantizationSpecs( + model_flags, converter_flags, &quant_specs, &node_names, &node_dtypes, + &node_shapes, &node_mins, &node_maxs)); + + TF_RETURN_IF_ERROR( + ParseInputArrayInfo(node_names, node_dtypes, node_shapes, &specs.inputs)); + + // Parse output arrays. + std::vector output_arrays(model_flags.output_arrays().begin(), + model_flags.output_arrays().end()); + TF_RETURN_IF_ERROR(ParseOutputArrayInfo(output_arrays, &specs.outputs)); + + // Parse control output arrays. + std::vector control_output_arrays( + model_flags.control_output_arrays().begin(), + model_flags.control_output_arrays().end()); + TF_RETURN_IF_ERROR( + ParseOutputArrayInfo(control_output_arrays, &specs.control_outputs)); + + specs.prune_unused_nodes = true; + specs.convert_legacy_fed_inputs = true; + specs.graph_as_function = false; + specs.upgrade_legacy = true; + specs.unconditionally_use_set_output_shapes = true; + internal::WarningUnusedFlags(model_flags, converter_flags); + + // Register all custom ops, including user-specified custom ops. + TF_RETURN_IF_ERROR(internal::RegisterAllCustomOps(converter_flags)); + GraphConstructorOptions options; + options.allow_internal_ops = true; + options.upgrade_legacy = specs.upgrade_legacy; + Graph graph(OpRegistry::Global()); + TF_RETURN_IF_ERROR(ConvertGraphDefToGraph(options, input, &graph)); + TF_ASSIGN_OR_RETURN( + auto module, + tensorflow::tf2xla::v2::ConvertGraphToTfExecutor( + graph, debug_info, graph.flib_def(), specs, context.get())); + + mlir::TFL::PassConfig pass_config(quant_specs); + bool emit_builtin_tflite_ops = !converter_flags.force_select_tf_ops(); + pass_config.emit_builtin_tflite_ops = emit_builtin_tflite_ops; + pass_config.unfold_batch_matmul = converter_flags.unfold_batchmatmul(); + pass_config.lower_tensor_list_ops = converter_flags.lower_tensor_list_ops(); + pass_config.legalize_custom_tensor_list_ops = + converter_flags.legalize_custom_tensor_list_ops(); + // Disable the unfolding of the 16x16 TF::BatchMatMulOp to avoid the + // conversion to an unsupported 16x16 TFL::FullyConnectedOp. + if (converter_flags.inference_type() == tflite::IODataType::QUANTIZED_INT16) { + pass_config.unfold_batch_matmul = false; + } + pass_config.unfold_large_splat_constant = + converter_flags.unfold_large_splat_constant(); + pass_config.enable_dynamic_update_slice = + converter_flags.enable_dynamic_update_slice(); + pass_config.preserve_assert_op = converter_flags.preserve_assert_op(); + pass_config.guarantee_all_funcs_one_use = + converter_flags.guarantee_all_funcs_one_use(); + pass_config.enable_stablehlo_conversion = + converter_flags.convert_to_stablehlo(); + pass_config.canonicalizing_inf_as_min_max_float = + converter_flags.canonicalizing_inf_as_min_max_float(); + + // StableHLO Quantizer is not supported for GraphDef inputs, so + // quantization_py_function_lib is set to nullptr. + return internal::ConvertMLIRToTFLiteFlatBuffer( + model_flags, converter_flags, std::move(context), std::move(module), + pass_config, + /*saved_model_tags=*/{}, result, + /*quantization_py_function_lib=*/nullptr); +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/graphdef_to_tfl_flatbuffer.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/graphdef_to_tfl_flatbuffer.h new file mode 100644 index 0000000000000000000000000000000000000000..4cd3e38f0d66c25b5ac6801f7989820f9ed14bf5 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/graphdef_to_tfl_flatbuffer.h @@ -0,0 +1,38 @@ +/* Copyright 2019 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_PYTHON_GRAPHDEF_TO_TFL_FLATBUFFER_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_PYTHON_GRAPHDEF_TO_TFL_FLATBUFFER_H_ + +#include + +#include "absl/status/status.h" +#include "tensorflow/compiler/mlir/lite/converter_flags.pb.h" +#include "tensorflow/compiler/mlir/lite/model_flags.pb.h" +#include "tensorflow/core/framework/graph.pb.h" +#include "tensorflow/core/framework/graph_debug_info.pb.h" + +namespace tensorflow { + +// Converts the given GraphDef to a TF Lite FlatBuffer string according to the +// given model flags, converter flags and debug information. Returns error +// status if it fails to convert the input. +absl::Status ConvertGraphDefToTFLiteFlatBuffer( + const tflite::ModelFlags& model_flags, + tflite::ConverterFlags& converter_flags, const GraphDebugInfo& debug_info, + const GraphDef& input, std::string* result); + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_PYTHON_GRAPHDEF_TO_TFL_FLATBUFFER_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/interpreter_wrapper/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/interpreter_wrapper/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..7e997a953dae3f1f3fa2ef0a08662082929da4c5 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/interpreter_wrapper/BUILD @@ -0,0 +1,29 @@ +load("@rules_cc//cc:cc_library.bzl", "cc_library") +load("//tensorflow:tensorflow.default.bzl", "get_compatible_with_portable") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + default_visibility = ["//visibility:public"], + licenses = ["notice"], +) + +cc_library( + name = "python_utils", + srcs = ["python_utils.cc"], + hdrs = ["python_utils.h"], + compatible_with = get_compatible_with_portable(), + deps = [ + "@xla//third_party/python_runtime:headers", # buildcleaner: keep + ], +) + +cc_library( + name = "python_error_reporter", + srcs = ["python_error_reporter.cc"], + hdrs = ["python_error_reporter.h"], + compatible_with = get_compatible_with_portable(), + deps = [ + "//tensorflow/compiler/mlir/lite:stateful_error_reporter", + "@xla//third_party/python_runtime:headers", # buildcleaner: keep + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/interpreter_wrapper/python_error_reporter.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/interpreter_wrapper/python_error_reporter.cc new file mode 100644 index 0000000000000000000000000000000000000000..bded222ae95cfba6538c976f1b562fddded2ca58 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/interpreter_wrapper/python_error_reporter.cc @@ -0,0 +1,49 @@ +/* Copyright 2024 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/compiler/mlir/lite/python/interpreter_wrapper/python_error_reporter.h" + +#include + +#include +#include +#include + +namespace tflite_migration { +namespace interpreter_wrapper { + +// Report an error message +int PythonErrorReporter::Report(const char* format, va_list args) { + char buf[1024]; + int formatted = vsnprintf(buf, sizeof(buf), format, args); + buffer_ << buf; + return formatted; +} + +// Set's a Python runtime exception with the last error. +PyObject* PythonErrorReporter::exception() { + std::string last_message = message(); + PyErr_SetString(PyExc_RuntimeError, last_message.c_str()); + return nullptr; +} + +// Gets the last error message and clears the buffer. +std::string PythonErrorReporter::message() { + std::string value = buffer_.str(); + buffer_.clear(); + return value; +} +} // namespace interpreter_wrapper +} // namespace tflite_migration diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/interpreter_wrapper/python_error_reporter.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/interpreter_wrapper/python_error_reporter.h new file mode 100644 index 0000000000000000000000000000000000000000..f992ca50c9bd70046e3d20f26302f21ac359d7c8 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/interpreter_wrapper/python_error_reporter.h @@ -0,0 +1,50 @@ +/* Copyright 2024 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_PYTHON_INTERPRETER_WRAPPER_PYTHON_ERROR_REPORTER_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_PYTHON_INTERPRETER_WRAPPER_PYTHON_ERROR_REPORTER_H_ + +#include + +#include +#include +#include + +#include "tensorflow/compiler/mlir/lite/stateful_error_reporter.h" + +namespace tflite_migration { +namespace interpreter_wrapper { + +class PythonErrorReporter : public tflite_migration::StatefulErrorReporter { + public: + PythonErrorReporter() = default; + + // Report an error message + int Report(const char* format, va_list args) override; + + // Sets a Python runtime exception with the last error and + // clears the error message buffer. + PyObject* exception(); + + // Gets the last error message and clears the buffer. + std::string message() override; + + private: + std::stringstream buffer_; +}; + +} // namespace interpreter_wrapper +} // namespace tflite_migration +#endif // TENSORFLOW_COMPILER_MLIR_LITE_PYTHON_INTERPRETER_WRAPPER_PYTHON_ERROR_REPORTER_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/interpreter_wrapper/python_utils.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/interpreter_wrapper/python_utils.cc new file mode 100644 index 0000000000000000000000000000000000000000..6449f7c6b674c8bf14c144e64cf7dffa88234ed7 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/interpreter_wrapper/python_utils.cc @@ -0,0 +1,45 @@ +/* Copyright 2024 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/compiler/mlir/lite/python/interpreter_wrapper/python_utils.h" + +#include + +namespace mlirlite { +namespace python_utils { + +int ConvertFromPyString(PyObject* obj, char** data, Py_ssize_t* length) { +#if PY_MAJOR_VERSION >= 3 + if (PyUnicode_Check(obj)) { + // const_cast<> is for CPython 3.7 finally adding const to the API. + *data = const_cast(PyUnicode_AsUTF8AndSize(obj, length)); + return *data == nullptr ? -1 : 0; + } + return PyBytes_AsStringAndSize(obj, data, length); +#else + return PyString_AsStringAndSize(obj, data, length); +#endif +} + +PyObject* ConvertToPyString(const char* data, size_t length) { +#if PY_MAJOR_VERSION >= 3 + return PyBytes_FromStringAndSize(data, length); +#else + return PyString_FromStringAndSize(data, length); +#endif +} + +} // namespace python_utils +} // namespace mlirlite diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/interpreter_wrapper/python_utils.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/interpreter_wrapper/python_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..bc50e95b6e150210e990bb02057f29ac4b426837 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/interpreter_wrapper/python_utils.h @@ -0,0 +1,34 @@ +/* Copyright 2024 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_PYTHON_INTERPRETER_WRAPPER_PYTHON_UTILS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_PYTHON_INTERPRETER_WRAPPER_PYTHON_UTILS_H_ + +#include + +#include + +namespace mlirlite { +namespace python_utils { + +struct PyDecrefDeleter { + void operator()(PyObject* p) const { Py_DECREF(p); } +}; + +int ConvertFromPyString(PyObject* obj, char** data, Py_ssize_t* length); +PyObject* ConvertToPyString(const char* data, size_t length); + +} // namespace python_utils +} // namespace mlirlite +#endif // TENSORFLOW_COMPILER_MLIR_LITE_PYTHON_INTERPRETER_WRAPPER_PYTHON_UTILS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/jax_to_tfl_flatbuffer.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/jax_to_tfl_flatbuffer.cc new file mode 100644 index 0000000000000000000000000000000000000000..f0ad0c80e0f8c9148904786c50439f8ee28ff904 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/jax_to_tfl_flatbuffer.cc @@ -0,0 +1,169 @@ +/* Copyright 2021 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/compiler/mlir/lite/python/jax_to_tfl_flatbuffer.h" + +#include +#include +#include +#include +#include + +#include "absl/log/log.h" +#include "absl/status/status.h" +#include "absl/strings/str_join.h" +#include "absl/strings/string_view.h" +#include "llvm/ADT/SmallVector.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/OwningOpRef.h" // from @llvm-project +#include "mlir/IR/TypeUtilities.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/common/tfl_pass_config.h" +#include "tensorflow/compiler/mlir/lite/converter_flags.pb.h" +#include "tensorflow/compiler/mlir/lite/model_flags.pb.h" +#include "tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.h" +#include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_config.h" +#include "tensorflow/compiler/mlir/lite/transforms/passes.h" +#include "tensorflow/compiler/mlir/lite/types.pb.h" +#include "xla/hlo/parser/hlo_parser.h" +#include "xla/hlo/translate/stablehlo.h" +#include "xla/service/hlo.pb.h" +#include "xla/tsl/platform/errors.h" +#include "xla/tsl/platform/statusor.h" +#include "tensorflow/core/framework/graph.pb.h" +#include "tensorflow/core/framework/graph_debug_info.pb.h" +#include "tensorflow/core/framework/types.pb.h" +#include "tensorflow/core/lib/core/errors.h" +#include "tensorflow/core/platform/errors.h" +#include "tsl/platform/protobuf.h" // IWYU pragma: keep + +namespace tensorflow { +namespace { + +// Error collector that simply ignores errors reported. +class NoOpErrorCollector : public tsl::protobuf::io::ErrorCollector { + public: + void RecordError(int line, tsl::protobuf::io::ColumnNumber column, + absl::string_view message) override {} +}; + +absl::StatusOr LoadHloProto(const std::string& contents) { + xla::HloProto hlo_proto; + // NOLINTNEXTLINE: Use tsl::protobuf to be compatible with OSS. + tsl::protobuf::TextFormat::Parser parser; + NoOpErrorCollector collector; + parser.RecordErrorsTo(&collector); + bool status = + hlo_proto.ParseFromString(contents) || + parser.ParseFromString(contents, &hlo_proto) || + hlo_proto.mutable_hlo_module()->ParseFromString(contents) || + parser.ParseFromString(contents, hlo_proto.mutable_hlo_module()); + if (!status) { + return absl::InternalError("Failed to parse HloProto"); + } + return hlo_proto; +} + +} // namespace + +absl::Status ConvertJaxToTFLiteFlatBuffer( + const std::string& input, const tflite::ModelFlags& model_flags, + tflite::ConverterFlags& converter_flags, std::string* result) { + auto context = std::make_unique(); + mlir::TFL::QuantizationSpecs quant_specs; + + // Parse input arrays. + std::vector node_names; + std::vector node_dtypes; + std::vector>> node_shapes; + std::vector> node_mins; + std::vector> node_maxs; + + // Populate quantization specs. + TF_RETURN_IF_ERROR(internal::PopulateQuantizationSpecs( + model_flags, converter_flags, &quant_specs, &node_names, &node_dtypes, + &node_shapes, &node_mins, &node_maxs)); + + internal::WarningUnusedFlags(model_flags, converter_flags); + + // Register all custom ops, including user-specified custom ops. + TF_RETURN_IF_ERROR(internal::RegisterAllCustomOps(converter_flags)); + + mlir::TFL::PassConfig pass_config(quant_specs); + bool emit_builtin_tflite_ops = !converter_flags.force_select_tf_ops(); + pass_config.emit_builtin_tflite_ops = emit_builtin_tflite_ops; + pass_config.enable_tflite_variables = + converter_flags.enable_tflite_resource_variables(); + pass_config.unfold_batch_matmul = converter_flags.unfold_batchmatmul(); + pass_config.lower_tensor_list_ops = converter_flags.lower_tensor_list_ops(); + // Disable the unfolding of the 16x16 TF::BatchMatMulOp to avoid the + // conversion to an unsupported 16x16 TFL::FullyConnectedOp. + if (converter_flags.inference_type() == tflite::IODataType::QUANTIZED_INT16) { + pass_config.unfold_batch_matmul = false; + } + pass_config.unfold_large_splat_constant = + converter_flags.unfold_large_splat_constant(); + pass_config.enable_hlo_to_tf_conversion = true; + pass_config.enable_stablehlo_conversion = + converter_flags.convert_to_stablehlo(); + + mlir::OwningOpRef module; + std::string content(input.data(), input.size()); + if (model_flags.hlo_file_type() == tflite::ModelFlags::HLO_TEXT) { + TF_ASSIGN_OR_RETURN(auto hlo_module, + xla::ParseAndReturnUnverifiedModule(content)); + TF_ASSIGN_OR_RETURN(auto module, + xla::ConvertHloToStablehlo(*context, hlo_module.get())); + } else if (model_flags.hlo_file_type() == tflite::ModelFlags::HLO_PROTO) { + TF_ASSIGN_OR_RETURN(xla::HloProto hlo_proto, LoadHloProto(content)); + TF_ASSIGN_OR_RETURN(module, xla::ConvertHloToStablehlo( + *context, hlo_proto.mutable_hlo_module())); + } else { + return absl::InvalidArgumentError("Unknown hlo format type"); + } + + // Set the input names. + auto main_func = module->lookupSymbol("main"); + if (!main_func) + return absl::InternalError("Failed to find the main function."); + // Retrieve input names from model flags. + std::vector input_names; + for (const auto& input : model_flags.input_arrays()) { + input_names.push_back(input.name()); + } + + const auto& inputs = absl::StrJoin(input_names, ","); + mlir::OpBuilder builder(*module); + llvm::SmallVector attrs; + attrs.push_back( + builder.getNamedAttr("inputs", builder.getStringAttr(inputs))); + // Jax wrapped the output nodes in a tuple, so it's pretty hard to us + // to tell the output at this point, we will set the output at the export + // phase. + main_func->setAttr("tf.entry_function", builder.getDictionaryAttr(attrs)); + + // StableHLO Quantizer is not supported for JAX input models, so + // quantization_py_function_lib is set to nullptr. + auto status = internal::ConvertMLIRToTFLiteFlatBuffer( + model_flags, converter_flags, std::move(context), std::move(module), + pass_config, /*saved_model_tags=*/{}, result, + /*quantization_py_function_lib=*/nullptr); + return status; +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/jax_to_tfl_flatbuffer.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/jax_to_tfl_flatbuffer.h new file mode 100644 index 0000000000000000000000000000000000000000..30032b869cf71b26a963778e4966ba7f0395a0b4 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/jax_to_tfl_flatbuffer.h @@ -0,0 +1,38 @@ +/* Copyright 2021 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_PYTHON_JAX_TO_TFL_FLATBUFFER_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_PYTHON_JAX_TO_TFL_FLATBUFFER_H_ + +#include + +#include "absl/status/status.h" +#include "tensorflow/compiler/mlir/lite/converter_flags.pb.h" +#include "tensorflow/compiler/mlir/lite/model_flags.pb.h" +#include "tensorflow/core/lib/core/errors.h" +#include "tensorflow/core/platform/status.h" +#include "tensorflow/core/platform/types.h" + +namespace tensorflow { + +// Converts the given Jax model to a TF Lite FlatBuffer +// string according to the given model flags, converter flags and tags. Returns +// error status if it fails to convert the input. +absl::Status ConvertJaxToTFLiteFlatBuffer( + const std::string& input, const tflite::ModelFlags& model_flags, + tflite::ConverterFlags& converter_flags, std::string* result); + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_PYTHON_JAX_TO_TFL_FLATBUFFER_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/saved_model_to_tfl_flatbuffer.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/saved_model_to_tfl_flatbuffer.cc new file mode 100644 index 0000000000000000000000000000000000000000..8dee8599959f8ed045963394b0776fa5c44bb7ff --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/saved_model_to_tfl_flatbuffer.cc @@ -0,0 +1,262 @@ +/* Copyright 2022 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/compiler/mlir/lite/python/saved_model_to_tfl_flatbuffer.h" + +#include +#include +#include +#include +#include +#include + +#include "absl/status/status.h" +#include "absl/types/span.h" +#include "llvm/ADT/StringSet.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/OwningOpRef.h" // from @llvm-project +#include "mlir/IR/TypeUtilities.h" // from @llvm-project +#include "mlir/Support/FileUtilities.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/common/tfl_pass_config.h" +#include "tensorflow/compiler/mlir/lite/converter_flags.pb.h" +#include "tensorflow/compiler/mlir/lite/model_flags.pb.h" +#include "tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.h" +#include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_config.h" +#include "tensorflow/compiler/mlir/lite/tf_to_tfl_flatbuffer.h" +#include "tensorflow/compiler/mlir/lite/transforms/passes.h" +#include "tensorflow/compiler/mlir/lite/types.pb.h" +#include "tensorflow/compiler/mlir/quantization/tensorflow/python/py_function_lib.h" +#include "tensorflow/compiler/mlir/tensorflow/translate/mlir_roundtrip_flags.h" +#include "xla/tsl/platform/errors.h" +#include "xla/tsl/platform/statusor.h" +#include "tensorflow/core/framework/graph.pb.h" +#include "tensorflow/core/framework/graph_debug_info.pb.h" +#include "tensorflow/core/framework/types.pb.h" +#include "tensorflow/core/lib/core/errors.h" +#include "tensorflow/core/platform/status.h" +#include "tensorflow/core/platform/types.h" + +namespace tensorflow { + +using tensorflow::quantization::PyFunctionLibrary; + +absl::Status HandleInputOutputArraysWithModule( + const tflite::ModelFlags& model_flags, + mlir::OwningOpRef* module) { + mlir::func::FuncOp entry_function = nullptr; + for (auto func : module->get().getOps()) { + if (auto tf_attrs = + func->getAttrOfType("tf.entry_function")) { + // TODO(b/184697652): There could be multiple entry functions. Let's + // handle such cases if there are any needs for that. + if (entry_function != nullptr) { + return absl::InvalidArgumentError( + "There should be only one tf.entry_function"); + } + entry_function = func; + } + } + if (entry_function == nullptr) { + return absl::InvalidArgumentError("no tf.entry_function found"); + } + + // Get the list of input Op names from the function attribute. + mlir::DictionaryAttr tf_attrs = + entry_function->getAttrOfType("tf.entry_function"); + llvm::SmallVector function_input_names; + function_input_names.reserve(model_flags.input_arrays().size()); + auto input_attr = tf_attrs.get("inputs"); + if (!input_attr) { + return absl::InvalidArgumentError("no inputs attribute found"); + } + auto input_names = mlir::cast(input_attr).getValue(); + input_names.split(function_input_names, ",", /*MaxSplit=*/-1, + /*KeepEmpty=*/false); + const int function_input_names_size = function_input_names.size(); + if (function_input_names_size != model_flags.input_arrays().size()) { + return absl::InvalidArgumentError(absl::StrCat( + "input array size mismatch: got ", function_input_names.size(), + ", expected: ", model_flags.input_arrays().size())); + } + llvm::StringSet<> function_input_names_set; + function_input_names_set.insert(function_input_names.begin(), + function_input_names.end()); + for (const auto& input_array : model_flags.input_arrays()) { + if (function_input_names_set.count(input_array.name()) == 0) { + return absl::InvalidArgumentError( + absl::StrCat("input array name (", input_array.name(), + ") does not exist in the given graph")); + } + } + + // Get the list of output Op names from the function attribute. + llvm::SmallVector function_output_names; + function_output_names.reserve(model_flags.output_arrays().size()); + auto output_attr = tf_attrs.get("outputs"); + if (!output_attr) { + return absl::InvalidArgumentError("no outputs attribute found"); + } + auto output_names = mlir::cast(output_attr).getValue(); + output_names.split(function_output_names, ",", /*MaxSplit=*/-1, + /*KeepEmpty=*/false); + const int function_output_names_size = function_output_names.size(); + if (function_output_names_size != model_flags.output_arrays().size()) { + return absl::InvalidArgumentError(absl::StrCat( + "output array size mismatch: got ", function_output_names.size(), + ", expected: ", model_flags.output_arrays().size())); + } + llvm::StringSet<> function_output_names_set; + function_output_names_set.insert(function_output_names.begin(), + function_output_names.end()); + for (const auto& output_array : model_flags.output_arrays()) { + if (function_output_names_set.count(output_array) == 0) { + return absl::InvalidArgumentError( + absl::StrCat("output array name (", output_array, + ") does not exist in the given graph")); + } + } + return absl::OkStatus(); +} + +absl::Status ConvertSavedModelToTFLiteFlatBuffer( + const tflite::ModelFlags& model_flags, + tflite::ConverterFlags& converter_flags, std::string* result, + const PyFunctionLibrary* quantization_py_function_lib) { + auto context = std::make_unique(); + mlir::TFL::QuantizationSpecs quant_specs; + + // Parse input arrays. + std::vector node_names; + std::vector node_dtypes; + std::vector>> node_shapes; + std::vector> node_mins; + std::vector> node_maxs; + + // Populate quantization specs. + TF_RETURN_IF_ERROR(internal::PopulateQuantizationSpecs( + model_flags, converter_flags, &quant_specs, &node_names, &node_dtypes, + &node_shapes, &node_mins, &node_maxs)); + + internal::WarningUnusedFlags(model_flags, converter_flags); + + // Register all custom ops, including user-specified custom ops. + TF_RETURN_IF_ERROR(internal::RegisterAllCustomOps(converter_flags)); + + auto& saved_model_tags = model_flags.saved_model_tags(); + auto& saved_model_exported_names = model_flags.saved_model_exported_names(); + std::unordered_set tags(saved_model_tags.begin(), + saved_model_tags.end()); + auto exported_names_in_vector = std::vector( + saved_model_exported_names.begin(), saved_model_exported_names.end()); + absl::Span exported_names(exported_names_in_vector); + + if (exported_names.empty()) { + return absl::UnimplementedError("Need at least one exported name."); + } + + tensorflow::GraphImportConfig specs; + specs.upgrade_legacy = true; + + std::vector custom_opdefs( + converter_flags.custom_opdefs().begin(), + converter_flags.custom_opdefs().end()); + TF_ASSIGN_OR_RETURN( + auto module, + ImportSavedModel(model_flags.saved_model_dir(), + model_flags.saved_model_version(), tags, + absl::MakeSpan(custom_opdefs), exported_names, specs, + /*enable_variable_lifting=*/true, context.get(), + /*saved_model_bundle=*/nullptr)); + + if (!model_flags.input_arrays().empty() || + !model_flags.output_arrays().empty()) { + TF_RETURN_IF_ERROR(HandleInputOutputArraysWithModule(model_flags, &module)); + } + + mlir::TFL::PassConfig pass_config(quant_specs); + bool emit_builtin_tflite_ops = !converter_flags.force_select_tf_ops(); + pass_config.emit_builtin_tflite_ops = emit_builtin_tflite_ops; + pass_config.enable_tflite_variables = + converter_flags.enable_tflite_resource_variables(); + pass_config.unfold_batch_matmul = converter_flags.unfold_batchmatmul(); + pass_config.lower_tensor_list_ops = converter_flags.lower_tensor_list_ops(); + // Disable the unfolding of the 16x16 TF::BatchMatMulOp to avoid the + // conversion to an unsupported 16x16 TFL::FullyConnectedOp. + if (converter_flags.inference_type() == tflite::IODataType::QUANTIZED_INT16) { + pass_config.unfold_batch_matmul = false; + } + pass_config.unfold_large_splat_constant = + converter_flags.unfold_large_splat_constant(); + pass_config.enable_dynamic_update_slice = + converter_flags.enable_dynamic_update_slice(); + pass_config.preserve_assert_op = converter_flags.preserve_assert_op(); + pass_config.guarantee_all_funcs_one_use = + converter_flags.guarantee_all_funcs_one_use(); + pass_config.enable_stablehlo_conversion = + converter_flags.convert_to_stablehlo(); + pass_config.legalize_custom_tensor_list_ops = + converter_flags.legalize_custom_tensor_list_ops(); + pass_config.enable_composite_direct_lowering = + converter_flags.enable_composite_direct_lowering(); + pass_config.model_origin_framework = converter_flags.model_origin_framework(); + pass_config.canonicalizing_inf_as_min_max_float = + converter_flags.canonicalizing_inf_as_min_max_float(); + pass_config.unsafe_fuse_dynamic_shaped_broadcast = + converter_flags.unsafe_fuse_dynamic_shaped_broadcast(); + + pass_config.unsafe_single_batch_rank_reduction = + converter_flags.unsafe_single_batch_rank_reduction(); + + if (converter_flags.strict_qdq_mode()) { + pass_config.quant_specs.qdq_conversion_mode = + mlir::TFL::QDQConversionMode::kQDQStrict; + } else if (converter_flags.qdq_conversion_mode() == "STATIC") { + pass_config.quant_specs.qdq_conversion_mode = + mlir::TFL::QDQConversionMode::kQDQStatic; + } else if (converter_flags.qdq_conversion_mode() == "DYNAMIC") { + pass_config.quant_specs.qdq_conversion_mode = + mlir::TFL::QDQConversionMode::kQDQDynamic; + // Need to set this or else the ops will still use floating point kernels + pass_config.quant_specs.inference_type = tensorflow::DT_QINT8; + } else if (converter_flags.qdq_conversion_mode() == "NONE") { + pass_config.quant_specs.qdq_conversion_mode = + mlir::TFL::QDQConversionMode::kQDQNone; + } else { + return absl::InvalidArgumentError( + absl::StrCat("Unknown QDQ conversion mode: ", + converter_flags.qdq_conversion_mode())); + } + + if (converter_flags.has_qdq_conversion_mode() && + converter_flags.qdq_conversion_mode() != "NONE") { + // Setting this flag causes + // PrepareQuantize::SetInputNodesQuantizationParams() to be false and allows + // PrepareQuantizePass to complete. For the most part this step is + // unnecessary for non-TF QDQ models. + pass_config.quant_specs.disable_set_input_nodes_quantization_params = true; + } + + // TODO(b/153507667): Pass the session object when importing logic is removed. + auto status = internal::ConvertMLIRToTFLiteFlatBuffer( + model_flags, converter_flags, std::move(context), std::move(module), + pass_config, tags, result, quantization_py_function_lib); + return status; +} + +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/saved_model_to_tfl_flatbuffer.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/saved_model_to_tfl_flatbuffer.h new file mode 100644 index 0000000000000000000000000000000000000000..0b2c55cb7b1284ae81799e128e87903ec77dafe5 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/saved_model_to_tfl_flatbuffer.h @@ -0,0 +1,40 @@ +/* Copyright 2020 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_PYTHON_SAVED_MODEL_TO_TFL_FLATBUFFER_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_PYTHON_SAVED_MODEL_TO_TFL_FLATBUFFER_H_ + +#include "absl/status/status.h" +#include "tensorflow/compiler/mlir/lite/converter_flags.pb.h" +#include "tensorflow/compiler/mlir/lite/model_flags.pb.h" +#include "tensorflow/compiler/mlir/quantization/tensorflow/python/py_function_lib.h" +#include "tensorflow/core/framework/graph.pb.h" +#include "tensorflow/core/framework/graph_debug_info.pb.h" +#include "tensorflow/core/lib/core/errors.h" +#include "tensorflow/core/platform/status.h" +#include "tensorflow/core/platform/types.h" + +namespace tensorflow { + +// Converts the given saved_model(either v1 or v2) to a TF Lite FlatBuffer +// string according to the given model flags, converter flags and tags. Returns +// error status if it fails to convert the input. +absl::Status ConvertSavedModelToTFLiteFlatBuffer( + const tflite::ModelFlags& model_flags, + tflite::ConverterFlags& converter_flags, std::string* result, + const quantization::PyFunctionLibrary* quantization_py_function_lib); + +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_PYTHON_SAVED_MODEL_TO_TFL_FLATBUFFER_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc new file mode 100644 index 0000000000000000000000000000000000000000..ec1115592505926dec25caee24d7cbacf9cd9a9b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc @@ -0,0 +1,384 @@ +/* Copyright 2020 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/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.h" + +#include +#include +#include +#include +#include +#include + +#include "absl/log/log.h" +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/OwningOpRef.h" // from @llvm-project +#include "mlir/IR/Visitors.h" // from @llvm-project +#include "mlir/Support/FileUtilities.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/common/tfl_pass_config.h" +#include "tensorflow/compiler/mlir/lite/converter_flags.pb.h" +#include "tensorflow/compiler/mlir/lite/model_flags.pb.h" +#include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_config.h" +#include "tensorflow/compiler/mlir/lite/tf_to_tfl_flatbuffer.h" +#include "tensorflow/compiler/mlir/lite/tools/optimize/reduced_precision_metadata.h" +#include "tensorflow/compiler/mlir/lite/transforms/passes.h" +#include "tensorflow/compiler/mlir/lite/types.pb.h" +#include "tensorflow/compiler/mlir/quantization/tensorflow/python/py_function_lib.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops_n_z.h" +#include "xla/tsl/platform/statusor.h" +#include "tensorflow/core/framework/graph.pb.h" +#include "tensorflow/core/framework/graph_debug_info.pb.h" +#include "tensorflow/core/framework/op.h" +#include "tensorflow/core/framework/op_def.pb.h" +#include "tensorflow/core/framework/op_def_builder.h" +#include "tensorflow/core/framework/types.pb.h" +#include "tensorflow/core/lib/core/errors.h" +#include "tsl/platform/protobuf.h" // IWYU pragma: keep + +namespace tensorflow { +namespace internal { +namespace { + +using ::tensorflow::quantization::PyFunctionLibrary; +using ::tflite::optimize::ReducedPrecisionSupport; + +// Op def string for TFLite_Detection_PostProcess Op. +constexpr mlir::StringRef kDetectionPostProcessOp = + "name: 'TFLite_Detection_PostProcess' input_arg: { name: " + "'raw_outputs/box_encodings' type: DT_FLOAT } input_arg: { name: " + "'raw_outputs/class_predictions' type: DT_FLOAT } input_arg: { name: " + "'anchors' type: DT_FLOAT } output_arg: { name: " + "'TFLite_Detection_PostProcess' type: DT_FLOAT } output_arg: { name: " + "'TFLite_Detection_PostProcess:1' type: DT_FLOAT } output_arg: { name: " + "'TFLite_Detection_PostProcess:2' type: DT_FLOAT } output_arg: { name: " + "'TFLite_Detection_PostProcess:3' type: DT_FLOAT } attr : { name: " + "'h_scale' type: 'float'} attr : { name: 'max_classes_per_detection' " + "type: 'int'} attr : { name: 'max_detections' type: 'int'} attr : { " + "name: 'nms_iou_threshold' type: 'float'} attr : { name: " + "'nms_score_threshold' type: 'float'} attr : { name: 'num_classes' type: " + "'int'} attr : { name: 'w_scale' type: 'float'} attr : { name: 'x_scale' " + "type: 'float'} attr : { name: 'y_scale' type: 'float'} attr { name: " + "'detections_per_class' type: 'int' default_value { i : 100 }} attr { " + "name: 'use_regular_nms' type: 'bool' default_value { b : false }}"; + +constexpr mlir::StringRef kUnidirectionalSequenceLstmOp = + "name: 'UnidirectionalSequenceLstm' input_arg: {name: 'Input' type: " + "DT_FLOAT} input_arg: { name: 'InputToInputWeights' type: DT_FLOAT } " + "input_arg: { name: 'InputToForgetWeights' type: DT_FLOAT } input_arg: { " + "name: 'InputToCellWeights' type: DT_FLOAT} input_arg: { name: " + "'InputToOutputWeights' type: DT_FLOAT } input_arg: { name: " + "'RecurrentToInputWeights' type: DT_FLOAT} input_arg: { name: " + "'RecurrentToForgetWeights' type: DT_FLOAT} input_arg: { name: " + "'RecurrentToCellWeights' type: DT_FLOAT } input_arg: { name: " + "'RecurrentToOutputWeights' type: DT_FLOAT } input_arg: { name: " + "'CellToInputWeights' type: DT_FLOAT} input_arg: { name: " + "'CellToForgetWeights' type: DT_FLOAT } input_arg: { name: " + "'CellToOutputWeights' type: DT_FLOAT } input_arg: { name: 'InputGateBias' " + "type: DT_FLOAT } input_arg: { name: 'ForgetGateBias' type: DT_FLOAT } " + "input_arg: { name: 'kCellGateBias' type: DT_FLOAT } input_arg: { name: " + "'OutputGateBias' type: DT_FLOAT } input_arg: { name: 'ProjectionWeights' " + "type: DT_FLOAT } input_arg: { name: 'ProjectionBias' type: DT_FLOAT } " + "input_arg: { name: 'InputActivationState' type: DT_FLOAT} input_arg: { " + "name: 'InputCellStateTensor' type: DT_FLOAT } " + "output_arg: { name: 'Concat' type: DT_FLOAT} " + "output_arg: { name: " + "'LastState' type: DT_FLOAT } output_arg: { name: 'Output' type: DT_FLOAT} " + "attr : { name: '_tflite_input_indices' type: 'list(int)'}"; + +constexpr mlir::StringRef kUnidirectionalSequenceRnnOp = + "name: 'UnidirectionalSequenceRnn' input_arg: {name: 'Input' type: " + "DT_FLOAT} input_arg: { name: 'Weights' type: DT_FLOAT } " + "input_arg: { name: 'RecurrentWeights' type: DT_FLOAT } input_arg: { " + "name: 'Bias' type: DT_FLOAT} " + "input_arg: { name: 'HiddenState' type: DT_FLOAT} " + "output_arg: { name: " + "'LastState' type: DT_FLOAT } output_arg: { name: 'Output' type: " + "DT_FLOAT} " + "attr : { name: '_tflite_input_indices' type: 'list(int)'}"; + +// Converts the tflite::IODataType to tensorflow::DataType. Only contains the +// conversion mapping for constants defined in TFLite Python API. +DataType ConvertIODataTypeToDataType(tflite::IODataType dtype) { + switch (dtype) { + case tflite::IODataType::FLOAT: + return DT_FLOAT; + case tflite::IODataType::FLOAT16: + return DT_HALF; + case tflite::IODataType::FLOAT64: + return DT_DOUBLE; + case tflite::IODataType::QUANTIZED_UINT8: + return DT_QUINT8; + case tflite::IODataType::QUANTIZED_INT8: + return DT_QINT8; + case tflite::IODataType::QUANTIZED_INT16: + return DT_QINT16; + case tflite::IODataType::INT8: + return DT_INT8; + case tflite::IODataType::INT16: + return DT_INT16; + case tflite::IODataType::UINT16: + return DT_UINT16; + case tflite::IODataType::INT32: + return DT_INT32; + case tflite::IODataType::UINT32: + return DT_UINT32; + case tflite::IODataType::INT64: + return DT_INT64; + case tflite::IODataType::UINT8: + return DT_UINT8; + case tflite::IODataType::UINT64: + return DT_UINT64; + case tflite::IODataType::STRING: + return DT_STRING; + case tflite::IODataType::BOOL: + return DT_BOOL; + case tflite::IODataType::COMPLEX64: + return DT_COMPLEX64; + case tflite::IODataType::COMPLEX128: + return DT_COMPLEX128; + case tflite::IODataType::RESOURCE: + return DT_RESOURCE; + case tflite::IODataType::VARIANT: + return DT_VARIANT; + default: + return DT_INVALID; + } +} + +absl::StatusOr> InputStatsToMinMax(double mean, + double std, + DataType type) { + // Only qint8 and quint8 are considered here. + double qmin, qmax; + if (type == DT_QUINT8) { + qmin = 0.0; + qmax = 255.0; + } else if (type == DT_QINT8) { + qmin = -128.0; + qmax = 127.0; + } else { + return absl::InvalidArgumentError("Only int8 and uint8 are considered."); + } + return std::make_pair((qmin - mean) / std, (qmax - mean) / std); +} + +absl::Status RegisterCustomBuiltinOps( + const std::vector extra_tf_opdefs) { + for (const auto& tf_opdefs_string : extra_tf_opdefs) { + OpDef opdef; + // NOLINTNEXTLINE: Use tsl::protobuf to be compatible with OSS. + if (!tsl::protobuf::TextFormat::ParseFromString(tf_opdefs_string, &opdef)) { + return absl::InvalidArgumentError("fail to parse extra OpDef"); + } + // Make sure the op is not already registered. If registered continue. + const OpRegistrationData* op_reg = + OpRegistry::Global()->LookUp(opdef.name()); + if (op_reg) continue; + + OpRegistry::Global()->Register( + [opdef](OpRegistrationData* op_reg_data) -> absl::Status { + *op_reg_data = OpRegistrationData(opdef); + return absl::OkStatus(); + }); + } + return absl::OkStatus(); +} + +} // namespace + +absl::Status RegisterAllCustomOps( + const tflite::ConverterFlags& converter_flags) { + // Register any custom OpDefs. + std::vector extra_tf_opdefs( + converter_flags.custom_opdefs().begin(), + converter_flags.custom_opdefs().end()); + extra_tf_opdefs.push_back(kDetectionPostProcessOp.str()); + extra_tf_opdefs.push_back(kUnidirectionalSequenceLstmOp.str()); + extra_tf_opdefs.push_back(kUnidirectionalSequenceRnnOp.str()); + return RegisterCustomBuiltinOps(extra_tf_opdefs); +} + +absl::Status PopulateQuantizationSpecs( + const tflite::ModelFlags& model_flags, + tflite::ConverterFlags& converter_flags, + mlir::TFL::QuantizationSpecs* quant_specs, + std::vector* node_names, std::vector* node_dtypes, + std::vector>>* node_shapes, + std::vector>* node_mins, + std::vector>* node_maxs) { + quant_specs->inference_input_type = + ConvertIODataTypeToDataType(converter_flags.inference_input_type()); + DataType inference_type = + ConvertIODataTypeToDataType(converter_flags.inference_type()); + // Use non-float flag `inference_input_type` to override the `inference_type` + // because we have to apply quantization to satisfy that. + if (quant_specs->inference_input_type != DT_FLOAT) { + inference_type = quant_specs->inference_input_type; + } + + for (auto& flag : model_flags.input_arrays()) { + node_names->push_back(flag.name()); + // TOCO doesn't required `data_type` to be filled for every input. + // If it's not filled, make it an empty string so the importer will use + // the data type in the NodeDef. + auto tflite_data_type = flag.data_type(); + if (tflite_data_type == tflite::IODataType::IO_DATA_TYPE_UNKNOWN) { + node_dtypes->push_back(""); + } else { + node_dtypes->push_back( + DataType_Name(ConvertIODataTypeToDataType(tflite_data_type))); + } + if (flag.shape().unknown_rank()) { + node_shapes->push_back(std::nullopt); + } else { + node_shapes->push_back(std::vector(flag.shape().dims().begin(), + flag.shape().dims().end())); + } + // Currently, only UINT8 and INT8 require inputs stats + if (inference_type == DT_QINT8 || inference_type == DT_QUINT8) { + if (flag.has_mean_value() && flag.has_std_value()) { + TF_ASSIGN_OR_RETURN( + auto min_max, InputStatsToMinMax(flag.mean_value(), + flag.std_value(), inference_type)); + node_mins->push_back(min_max.first); + node_maxs->push_back(min_max.second); + } else { + node_mins->push_back(std::nullopt); + node_maxs->push_back(std::nullopt); + } + } + } + + if (mlir::TFL::GetInputNodeQuantSpecs(*node_names, *node_mins, *node_maxs, + inference_type, quant_specs)) { + return absl::InvalidArgumentError("Failed to get input quant spec."); + } + + // Some extra flag related to post training quantization. If post-training + // quantization is enabled, `inference_type` and `inference_input_type` are + // not used by MLIR passes. + if (converter_flags.post_training_quantize()) { + quant_specs->weight_quantization = true; + quant_specs->disable_per_channel = + converter_flags.disable_per_channel_quantization(); + if (converter_flags.quantize_to_float16()) { + quant_specs->inference_type = DT_HALF; + quant_specs->inference_input_type = DT_HALF; + } else { + quant_specs->inference_type = DT_QINT8; + quant_specs->inference_input_type = DT_QINT8; + } + } else { + // These flags are incompatible with post_training_quantize() as only + // QAT models can provide required ranges. + quant_specs->disable_infer_tensor_range = + converter_flags.disable_infer_tensor_range(); + quant_specs->use_fake_quant_num_bits = + converter_flags.use_fake_quant_num_bits(); + } + + // Add information about half-precision support if fp16 quantization applies. + // TODO(b/195945955): Add e2e test for this. + if (converter_flags.quantize_to_float16() || + converter_flags.allow_bfloat16()) { + ReducedPrecisionSupport mask = ReducedPrecisionSupport::None; + if (converter_flags.quantize_to_float16()) { + mask |= ReducedPrecisionSupport::Float16Inference; + } + if (converter_flags.allow_bfloat16()) { + mask |= ReducedPrecisionSupport::Bfloat16Inference; + } + if (converter_flags.accumulation_type() == tflite::IODataType::FLOAT16) { + mask |= ReducedPrecisionSupport::Float16Accumulation; + } else { + mask |= ReducedPrecisionSupport::Float32Accumulation; + } + quant_specs->support_mask = mask; + } + + // Other flags. + if (converter_flags.has_default_ranges_min()) { + quant_specs->default_ranges.first = converter_flags.default_ranges_min(); + } + if (converter_flags.has_default_ranges_max()) { + quant_specs->default_ranges.second = converter_flags.default_ranges_max(); + } + quant_specs->enable_mlir_dynamic_range_quantizer = + converter_flags.enable_mlir_dynamic_range_quantizer(); + quant_specs->enable_mlir_variable_quantization = + converter_flags.enable_mlir_variable_quantization(); + quant_specs->disable_per_channel_for_dense_layers = + converter_flags.disable_per_channel_quantization_for_dense_layers(); + return absl::OkStatus(); +} + +absl::Status ConvertMLIRToTFLiteFlatBuffer( + const tflite::ModelFlags& model_flags, + tflite::ConverterFlags& converter_flags, + std::unique_ptr&& context, + mlir::OwningOpRef module, + const mlir::TFL::PassConfig& pass_config, + const std::unordered_set& saved_model_tags, + std::string* result, + const PyFunctionLibrary* quantization_py_function_lib) { + mlir::TFL::PassConfig pass_config_copy = pass_config; + pass_config_copy.outline_tf_while = true; + + // Checks whether the model contains an `XlaCallModuleOp` operation which + // is a wrapper around StableHLO. + // This option is mutually exclusive to `enable_stablehlo_conversion`, the + // latter of which takes precedence. + // TODO(b/290109282): explore removing the enable_hlo_to_tf_conversion flag + // entirely, such that the added passes are no-ops in the non-shlo case. + module->walk([&](mlir::TF::XlaCallModuleOp xla_call_module_op) { + pass_config_copy.enable_hlo_to_tf_conversion = true; + mlir::WalkResult::interrupt(); + }); + + auto status = ConvertTFExecutorToTFLOrFlatbuffer( + std::move(context), std::move(module), converter_flags, pass_config_copy, + saved_model_tags, model_flags.saved_model_dir(), result, + /*export_to_mlir=*/false, quantization_py_function_lib); + + return status; +} + +void WarningUnusedFlags(const tflite::ModelFlags& model_flags, + const tflite::ConverterFlags& converter_flags) { + if (converter_flags.output_format()) { + LOG(WARNING) << "Ignored output_format."; + } + if (converter_flags.drop_control_dependency()) { + LOG(WARNING) << "Ignored drop_control_dependency."; + } + if (converter_flags.reorder_across_fake_quant()) { + LOG(WARNING) << "Ignored reorder_across_fake_quant."; + } + if (model_flags.change_concat_input_ranges()) { + LOG(WARNING) << "Ignored change_concat_input_ranges."; + } + if (converter_flags.dump_graphviz_include_video()) { + LOG(WARNING) << "Ignored dump_graphviz_video."; + } + if (model_flags.allow_nonexistent_arrays()) { + LOG(WARNING) << "Allow allow_nonexistent_arrays."; + } +} + +} // namespace internal +} // namespace tensorflow diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.h new file mode 100644 index 0000000000000000000000000000000000000000..ecc8d5b070124130c48f187fa57e300fb0f05c7f --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.h @@ -0,0 +1,73 @@ +/* Copyright 2020 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_PYTHON_TF_TFL_FLATBUFFER_HELPERS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_PYTHON_TF_TFL_FLATBUFFER_HELPERS_H_ + +#include +#include +#include +#include + +#include "absl/status/status.h" +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/OwningOpRef.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/common/tfl_pass_config.h" +#include "tensorflow/compiler/mlir/lite/converter_flags.pb.h" +#include "tensorflow/compiler/mlir/lite/model_flags.pb.h" +#include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_config.h" +#include "tensorflow/compiler/mlir/lite/transforms/passes.h" +#include "tensorflow/compiler/mlir/lite/types.pb.h" +#include "tensorflow/compiler/mlir/quantization/tensorflow/python/py_function_lib.h" +#include "tensorflow/core/platform/status.h" +#include "tensorflow/core/platform/types.h" + +namespace tensorflow { +namespace internal { + +// Register all custom ops including user specified custom ops. +absl::Status RegisterAllCustomOps( + const tflite::ConverterFlags& converter_flags); + +// Populate quantization specs (or not) given user specified ranges for each +// input arrays. +absl::Status PopulateQuantizationSpecs( + const tflite::ModelFlags& model_flags, + tflite::ConverterFlags& converter_flags, + mlir::TFL::QuantizationSpecs* quant_specs, + std::vector* node_names, std::vector* node_dtypes, + std::vector>>* node_shapes, + std::vector>* node_mins, + std::vector>* node_maxs); + +// Convert imported MLIR file to TfLite flatbuffer. +// This will also run relevant passes as well. +absl::Status ConvertMLIRToTFLiteFlatBuffer( + const tflite::ModelFlags& model_flags, + tflite::ConverterFlags& converter_flags, + std::unique_ptr&& context, + mlir::OwningOpRef module, + const mlir::TFL::PassConfig& pass_config, + const std::unordered_set& saved_model_tags, + std::string* result, + const quantization::PyFunctionLibrary* quantization_py_function_lib); + +// Give a warning for any unused flags that have been specified. +void WarningUnusedFlags(const tflite::ModelFlags& model_flags, + const tflite::ConverterFlags& converter_flags); +} // namespace internal +} // namespace tensorflow + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_PYTHON_TF_TFL_FLATBUFFER_HELPERS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/wrap_converter.py b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/wrap_converter.py new file mode 100644 index 0000000000000000000000000000000000000000..6d4e150ced91a3e130de8510cef79bd75acea2f7 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/python/wrap_converter.py @@ -0,0 +1,90 @@ +# Copyright 2019 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. +# ============================================================================== +"""Wraps TFLite Converter interface with python lazy loader.""" +# We need to import pywrap_tensorflow prior to the converter wrapper. +# pylint: disable=invalid-import-order,g-bad-import-order +from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import +from tensorflow.compiler.mlir.lite.python import _pywrap_converter_api +from tensorflow.compiler.mlir.quantization.tensorflow.python import py_function_lib + + +def wrapped_convert( + model_flags_str, + converter_flags_str, + input_data_str, + debug_info_str, +): + """Wraps Convert with lazy loader.""" + return _pywrap_converter_api.Convert( + model_flags_str, + converter_flags_str, + input_data_str, + False, # extended_return + debug_info_str, + py_function_lib.PyFunctionLibrary(), + ) + + +def wrapped_experimental_mlir_quantize( + input_data_str, + disable_per_channel, + fully_quantize, + inference_type, + input_data_type, + output_data_type, + enable_numeric_verify, + enable_whole_model_verify, + denylisted_ops, + denylisted_nodes, + enable_variable_quantization, + disable_per_channel_for_dense_layers, + debug_options_str, +): + """Wraps experimental mlir quantize model.""" + return _pywrap_converter_api.ExperimentalMlirQuantizeModel( + input_data_str, + disable_per_channel, + fully_quantize, + inference_type, + input_data_type, + output_data_type, + enable_numeric_verify, + enable_whole_model_verify, + denylisted_ops, + denylisted_nodes, + enable_variable_quantization, + disable_per_channel_for_dense_layers, + debug_options_str, + ) + + +def wrapped_experimental_mlir_sparsify(input_data_str): + """Wraps experimental mlir sparsify model.""" + return _pywrap_converter_api.ExperimentalMlirSparsifyModel(input_data_str) + + +def wrapped_register_custom_opdefs(custom_opdefs_list): + """Wraps RegisterCustomOpdefs with lazy loader.""" + return _pywrap_converter_api.RegisterCustomOpdefs(custom_opdefs_list) + + +def wrapped_retrieve_collected_errors(): + """Wraps RetrieveCollectedErrors with lazy loader.""" + return _pywrap_converter_api.RetrieveCollectedErrors() + + +def wrapped_flat_buffer_file_to_mlir(model, input_is_filepath): + """Wraps FlatBufferFileToMlir with lazy loader.""" + return _pywrap_converter_api.FlatBufferToMlir(model, input_is_filepath) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..cdad1daf88bd8207bf4eff58ea142811955c5985 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/BUILD @@ -0,0 +1,136 @@ +load("//tensorflow:tensorflow.bzl", "tf_cc_test", "tf_native_cc_binary") +load("//tensorflow/core/platform:build_config.bzl", "tf_proto_library") +load("//tensorflow/core/platform:rules_cc.bzl", "cc_library") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + # copybara:uncomment_begin(google-only) + # default_visibility = [ + # ":friends", + # "//tensorflow:__pkg__", + # ], + # copybara:uncomment_end_and_comment_begin + default_visibility = [ + "//visibility:public", + ], + # copybara:comment_end + licenses = ["notice"], +) + +package_group( + name = "friends", + packages = [ + "//learning/brain/mlir/quantization/...", + "//tensorflow/compiler/mlir/...", + "//tensorflow/lite/tools/optimize/...", + ], +) + +tf_proto_library( + name = "quantization_info_proto", + srcs = [ + "quantization_info.proto", + ], +) + +cc_library( + name = "quantization_passes", + srcs = [ + "import_quant_stats_pass.cc", + ], + hdrs = [ + "quantization_passes.h", + ], + deps = [ + ":quantization_info_proto_cc", + "//tensorflow/compiler/mlir/lite/quantization/ir:QuantOps", + "//tensorflow/compiler/mlir/tensorflow:import_utils", + "//tensorflow/compiler/mlir/tensorflow:location_utils", + "@com_google_absl//absl/strings", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:Support", + ], + alwayslink = 1, +) + +tf_native_cc_binary( + name = "op_quant_spec_getters_gen", + srcs = [ + "tools/op_quant_spec_getters_gen.cc", + ], + deps = [ + "@llvm-project//llvm:Support", + "@llvm-project//llvm:TableGen", + "@llvm-project//mlir:TableGen", + ], +) + +tf_native_cc_binary( + name = "tflite_op_coverage_spec_getters_gen", + srcs = [ + "tools/tflite_op_coverage_spec_getters_gen.cc", + ], + deps = [ + "@com_google_absl//absl/strings", + "@llvm-project//llvm:Support", + "@llvm-project//llvm:TableGen", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:TableGen", + "@tsl//tsl/platform:regexp", + "@xla//xla/tsl/platform:logging", + ], +) + +cc_library( + name = "numerical_utils", + srcs = ["numerical_utils.cc"], + hdrs = ["numerical_utils.h"], + deps = [ + "@com_google_absl//absl/types:optional", + ], +) + +cc_library( + name = "device_target", + srcs = ["device_target.cc"], + hdrs = ["device_target.h"], + deps = [ + ":numerical_utils", + "//tensorflow/compiler/mlir/lite/quantization/ir:QuantOps", + "@com_google_absl//absl/types:optional", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:Support", + ], +) + +cc_library( + name = "quantization_context", + srcs = ["quantization_context.cc"], + hdrs = ["quantization_context.h"], + deps = [ + ":device_target", + "//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib", + "//tensorflow/compiler/mlir/lite/quantization/ir:QuantOps", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:Support", + ], +) + +tf_cc_test( + name = "numerical_utils_test", + srcs = ["numerical_utils_test.cc"], + deps = [ + ":numerical_utils", + "@com_google_absl//absl/types:optional", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..63cfaea5736febeadc379af519dc6acd94bb0f8d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/BUILD @@ -0,0 +1,41 @@ +load("@rules_cc//cc:cc_library.bzl", "cc_library") +load("//tensorflow:tensorflow.default.bzl", "get_compatible_with_portable") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + # By default, these targets should only be used within the quantization library. + default_visibility = [ + "//learning/brain/mlir/quantization:__subpackages__", + "//tensorflow/compiler/mlir/lite:__subpackages__", + "//tensorflow/compiler/mlir/quantization:__subpackages__", + "//tensorflow/compiler/mlir/stablehlo:__subpackages__", + ], + licenses = ["notice"], +) + +cc_library( + name = "test_base", + testonly = 1, + srcs = [], + hdrs = ["test_base.h"], + compatible_with = get_compatible_with_portable(), + deps = [ + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/lite/quantization/ir:QuantOps", + "//tensorflow/compiler/mlir/quantization/common:func", + "//tensorflow/compiler/mlir/quantization/common/ir:QuantOps", + "//tensorflow/compiler/mlir/quantization/stablehlo/cc:context", + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_types", + "//tensorflow/core:test", + "@com_google_absl//absl/strings:string_view", + "@com_google_googletest//:gtest_main", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Parser", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:Support", + "@stablehlo//:stablehlo_ops", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..44ebfb7948774a753cd85f362c1bf596f95363f0 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/BUILD @@ -0,0 +1,150 @@ +load("@llvm-project//mlir:tblgen.bzl", "gentbl_cc_library", "td_library") +load("//tensorflow:tensorflow.bzl", "tf_cc_test") +load("//tensorflow:tensorflow.default.bzl", "get_compatible_with_portable") +load("//tensorflow/core/platform:rules_cc.bzl", "cc_library") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + # By default, these targets should only be used within the quantization library. + # copybara:uncomment_begin(google-only) + # default_visibility = [ + # "//learning/brain/mlir/quantization:__subpackages__", + # "//third_party/odml/litert:__subpackages__", + # "//tensorflow:__subpackages__", + # ], + # copybara:uncomment_end_and_comment_begin + default_visibility = [ + "//visibility:public", + ], + # copybara:comment_end + licenses = ["notice"], +) + +cc_library( + name = "tfl_quantization_driver", + srcs = [ + "tfl_quantization_driver.cc", + ], + hdrs = [ + "tfl_quantization_driver.h", + ], + deps = [ + ":quantization_config", + ":quantization_lib", + "//tensorflow/compiler/mlir/lite:tensorflow_lite_ops", + "//tensorflow/compiler/mlir/lite/quantization/ir:QuantOps", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/container:flat_hash_set", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:Support", + ], +) + +cc_library( + name = "quantization_lib", + srcs = [ + "quantization_driver.cc", + "quantization_interface.cc.inc", + "quantization_utils.cc", + ], + hdrs = [ + "quantization_driver.h", + "quantization_interface.h.inc", + "quantization_traits.h", + "quantization_utils.h", + ], + deps = [ + ":quantization_config", + ":quantization_interfaces_inc_gen", + "//tensorflow/compiler/mlir/lite/quantization/ir:QuantOps", + "//tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy:portable_tensor_utils", + "//tensorflow/compiler/mlir/quantization/common/ir:QuantOps", + "//tensorflow/compiler/mlir/tools/optimize:quantization_utils", + "//tensorflow/core:lib_proto_parsing", + "//tensorflow/core:protos_all_cc", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/memory", + "@com_google_absl//absl/strings", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:Support", + ], +) + +tf_cc_test( + name = "quantization_driver_test", + srcs = ["quantization_driver_test.cc"], + deps = [ + ":quantization_lib", + "//tensorflow/compiler/mlir/lite/quantization/common:test_base", + "//tensorflow/compiler/mlir/lite/quantization/ir:QuantOps", + "//tensorflow/compiler/mlir/quantization/common:attrs_and_constraints", + "//tensorflow/compiler/mlir/quantization/common:func", + "//tensorflow/compiler/mlir/tensorflow", + "@com_google_absl//absl/strings:string_view", + "@com_google_googletest//:gtest_main", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:Support", + ], +) + +td_library( + name = "quantization_td_files", + srcs = [ + "quantization.td", + ], + compatible_with = get_compatible_with_portable(), + deps = [ + "//tensorflow/compiler/mlir/lite/quantization/ir:QuantizationOpsTdFiles", + "@llvm-project//mlir:OpBaseTdFiles", + ], +) + +gentbl_cc_library( + name = "quantization_interfaces_inc_gen", + compatible_with = get_compatible_with_portable(), + tbl_outs = { + "quantization_interface.h.inc": ["-gen-op-interface-decls"], + "quantization_interface.cc.inc": ["-gen-op-interface-defs"], + }, + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "quantization.td", + deps = [ + ":quantization_td_files", + ], +) + +cc_library( + name = "quantization_config", + srcs = [ + "quantization_config.cc", + ], + hdrs = [ + "quantization_config.h", + ], + deps = [ + "//tensorflow/compiler/mlir/lite/tools/optimize:reduced_precision_metadata", + "//tensorflow/core:protos_all_cc", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/strings", + "@llvm-project//llvm:Support", + ], +) + +exports_files([ + "quantization_traits.h", + "quantization_config.h", + "quantization_utils.h", +]) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization.td b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization.td new file mode 100644 index 0000000000000000000000000000000000000000..f78acdd944cdec911517bde8730488c9d8ef95a9 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization.td @@ -0,0 +1,248 @@ +/* Copyright 2019 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. +==============================================================================*/ + +// This is the quantization definition file for TensorFlow. + +#ifdef TF_Quantization +#else +#define TF_Quantization + +include "mlir/IR/OpBase.td" +include "mlir/Dialect/Quant/IR/QuantBase.td" + +//===----------------------------------------------------------------------===// +// QuantizedType definitions. +//===----------------------------------------------------------------------===// + +// The base class of a quantized type. Signed quantized types may be expressed +// as signless integers (i.e. up to op interpretation), but we include an +// explicit signedness check to differentiate the signed/unsigned constraints +// predicates from one another at the TD level. +class QuantizedType params, bit signed> + : Type($_self)">, + CPred<"llvm::cast($_self)" # + ".getStorageTypeIntegralWidth() == " # !head(params)>, + Or<[CPred<"llvm::cast($_self)" # + ".getStorageType().isSignlessInteger()">, + CPred<"llvm::cast($_self)" # + ".getStorageType().isSignedInteger() == " # signed>]>]>, + "Q" # !if (signed, "I", "UI") # !head(params) # " type"> { + string name = n; + string asTraitArgsStr = + !interleave(params, ", ") # !if(signed, ", true", ", false"); +} + +// Uniform quantized types. Two integers "smantissa" and "sexp" are used to +// express the Mantissa and Exponent components of the floating-point scale so +// the scale of the quantized type is "smantissa * 10 ^ sexp". +class UInt8UniformQuantizedType + : QuantizedType<"Uniform", + [8, zero_pt, smantissa, sexp, 0, 255], 0>; +class Int8UniformQuantizedType + : QuantizedType<"Uniform", + [8, zero_pt, smantissa, sexp, -128, 127], 1>; + +// General uniform quantized types. The definitions can be used to specify +// operand's tensor types. +def QI2 : QuantizedType<"Uniform", [2], 1>; +def QI4 : QuantizedType<"Uniform", [4], 1>; +def QUI4 : QuantizedType<"Uniform", [4], 0>; +def QUI8 : QuantizedType<"Uniform", [8], 0>; +def QI8 : QuantizedType<"Uniform", [8], 1>; +def QUI16 : QuantizedType<"Uniform", [16], 0>; +def QI16 : QuantizedType<"Uniform", [16], 1>; +def QUI32 : QuantizedType<"Uniform", [32], 0>; +def QI32 : QuantizedType<"Uniform", [32], 1>; + +//===----------------------------------------------------------------------===// +// TFL native op traits (for quantization). +// +// Ops in this link should have those traits specified: +// https://www.tensorflow.org/lite/performance/quantization_spec +//===----------------------------------------------------------------------===// + +def FixedOutputRangeInterface : OpInterface< + "FixedOutputRangeInterface"> { + let cppNamespace = "TFL"; + + let description = [{ + Interface for defining the fixed output range. + }]; + + let methods = [ + InterfaceMethod< + [{Returns the fixed output range.}], + "UniformQuantizedType", "GetFixedOutputRange", + (ins "bool":$sign, "int":$bit_width) + >, + ]; +} + +def AffineQuantizedOpInterface : OpInterface< + "AffineQuantizedOpInterface"> { + let cppNamespace = "TFL"; + + let description = [{ + Interface for affine quantized ops (conv2d, fully_connected, etc.) + }]; + + let methods = [ + InterfaceMethod< + [{Returns the affine operand index.}], + "int", "GetAffineOperandIndex", + (ins), [{}], [{return 1;}]>, + InterfaceMethod< + [{Returns whether narrow range is required for the affine operand.}], + "bool", "RequiredNarrowRangeAffineOperand", + (ins), [{}], [{return true;}]>, + InterfaceMethod< + [{Returns quantization dim for the affine operand.}], + "int", "GetQuantizationDimIndex", + (ins)>, + InterfaceMethod< + [{Returns the dimension index of the output channels.}], + "int", "GetChannelDimIndex", (ins) + >, + ]; +} + +def SameOperandsAndResultsScale : OpInterface<"SameScalesOpInterface"> { + let cppNamespace = "TFL"; + + let description = [{ + Interface for ops potentially have same operands and results scales. + }]; + + let methods = [ + InterfaceMethod< + [{Returns whether same operands and results scales are required.}], + "bool", "RequiredSameOperandsAndResultsScale", + (ins "bool":$sign, "int":$bit_width), [{}], [{return true;}] + >, + InterfaceMethod< + [{Returns whether operands and results must have the same quantized axis.}], + "bool", "RequiredSameQuantizedAxes", + (ins), [{}], [{return true;}] + >, + ]; + + let verify = [{ + return mlir::TFL::VerifySameScales($_op); + }]; +} + +def RequiresQuantizedBiasInterface : OpInterface<"RequiresQuantizedBiasInterface"> { + let description = [{ + Interface for ops that require the converter to quantize their bias based on + the scales of the other operands. + }]; + + let methods = [ + InterfaceMethod< + [{Returns the bias operand index.}], + "int", "GetBiasOperandIndex", + (ins), [{}], [{return -1;}]>, + InterfaceMethod< + [{Returns the non-bias operand indices.}], + "std::vector", "GetNonBiasOperandIndices", + (ins), [{}], [{return {};}]>, + ]; +} + + +def DynamicRangeQuantizedOpInterface : OpInterface< + "DynamicRangeQuantizedOpInterface"> { + let cppNamespace = "TFL"; + + let description = [{ + Interface for ops dynamic range quantization is supported. + + If the op has the kernel support for dynamic range quantization, Q/DQ op + pairs connected to the op are rewritten by its quantized alternatives where + a new op uses Q ops for its operands instead of DQ op. Otherwise, it is + left as is for weight-only which means the weight is dequantized at runtime. + + For example, if the kernel does not support dynamic range quantization the + graph will be converted into the following IR: + + %q_w = "tfl.pseudo_qconst"() { + qtype = tensor<64x3x3x3x!quant.uniform:f32, 1.000000e+00>> + %w = "tfl.dequantize"(%q_w) : + (tensor<64x3x3x3x!quant.uniform:f32, 1.000000e+00>>) -> + tensor<64x3x3x3xf32> + %conv = "tfl.conv_2d"(%input_act, %w, %bias) + + but if it is supported, it will be rewritten as: + + %q_w = "tfl.pseudo_qconst"() { + qtype = tensor<64x3x3x3x!quant.uniform:f32, 1.000000e+00>> + %conv = "tfl.conv_2d"(%input_act, %q_w, %bias) + + Note that this is part of reaching feature parity with the old quantizer for + dynamic range quantization except: + - Only use_updated_hybrid_scheme=True is supported which means the ops with + the asymmetrically quantizing input support is enabled to use this feature + during MLIR graph rewriting passes while it is configurable in the old + quantizer. So when those ops are matched during graph rewriting passes, + MLIR quantizer will always ignore the pre-set value of the attribute, if + there's any, and set it to True. The reason behind this decision is that + generally activations of these ops show better accuracy with asymmetric + input quantization so we want to deprecate symmetric activation quantization + for those ops eventually. + - Unlike to the old quantizer, per-channel quantization is supported for + weight-only TransposeConvOp. + }]; + + let methods = [ + InterfaceMethod< + [{Returns the quantizable operand indices of the op.}], + "std::vector", "GetQuantizableOperandIndices", + (ins), [{}], [{return {};}]>, + InterfaceMethod< + [{Returns whether the op has the kernel support for dynamic range + quantization.}], + "bool", "GetDynamicRangeQuantKernelSupport", + (ins), [{}], [{return false;}]>, + InterfaceMethod< + [{Returns whether the op requires asymmetric quantize input attribute + setting.}], + "bool", "RequireAsymmetricQuantizeInputsAttr", + (ins), [{}], [{return false;}]>, + ]; +} + +// Specify this trait if the op has a fixed output value range. +class FixedResultScale : NativeOpTrait::Impl")>; + +// Specify this trait if the bias-th input of the op is a bias input, which +// needs a scale based on the scales of op1 and op2. +class AccumulatorUniformScale : NativeOpTrait< + !strconcat("TFL::AccumulatorUniformScale<", + !interleave([bias, op1, op2], ", "), + ">::Impl")>; + +// Specify the operand index of the coefficient operand for an affine op +// and also the quantization dimension if per-axis quantization is support. +// If the quantization dimension is -1, per-axis quantization isn't supported. +class AffineOpCoefficient : NativeOpTrait< + !strconcat("TFL::AffineOpCoefficient<", + !interleave([dim, index], ", "), + ">::Impl")>; + +// Specify this trait if the op does have quantizable output. Quantizers will +// apply quantization on this op. +def QuantizableResult : NativeOpTrait<"TFL::QuantizableResult">; +#endif // TF_Quantization diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_config.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_config.cc new file mode 100644 index 0000000000000000000000000000000000000000..63a551eb5c7e04c6c5ca441ed28e6774d871a629 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_config.cc @@ -0,0 +1,183 @@ +/* Copyright 2019 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/compiler/mlir/lite/quantization/common/quantization_lib/quantization_config.h" + +#include +#include +#include +#include +#include + +#include "absl/strings/numbers.h" +#include "absl/strings/str_split.h" +#include "absl/strings/string_view.h" +#include "llvm/Support/Debug.h" +#include "llvm/Support/raw_ostream.h" +#include "tensorflow/core/framework/types.pb.h" + +// Returns whether the given dtype is a quantization type in TensorFlow. +static bool IsQuantizationType(tensorflow::DataType dtype) { + switch (dtype) { + case tensorflow::DT_QINT8: + case tensorflow::DT_QUINT8: + case tensorflow::DT_QINT16: + case tensorflow::DT_QUINT16: + case tensorflow::DT_QINT32: + return true; + default: + return false; + } +} + +namespace mlir { +namespace TFL { +namespace { +bool GetBooleanSpecs(const std::string& bool_val) { + bool result; + std::stringstream iss(bool_val); + iss >> std::boolalpha >> result; + return result; +} +} // namespace + +void ParseCustomOpSpecs(const absl::string_view node_names, + const CustomOpUpdateOptions& update_option, + CustomOpMap& custom_op_map) { + if (node_names.empty()) return; + + const std::vector custom_nodes = absl::StrSplit(node_names, ','); + + for (const std::string& cur_node : custom_nodes) { + const std::vector node_infos = absl::StrSplit(cur_node, '='); + const std::string& node_name = node_infos[0]; + const std::string& node_specification = node_infos[1]; + CustomOpInfo new_node_info; + switch (update_option) { + case CustomOpUpdateOptions::kInputIndices: { + const std::vector indices = + absl::StrSplit(node_specification, '-'); + for (const std::string& cur_index : indices) { + custom_op_map[node_name].quantizable_input_indices.push_back( + std::stoi(cur_index)); + } + break; + } + case CustomOpUpdateOptions::kWeightOnly: + custom_op_map[node_name].is_weight_only = + GetBooleanSpecs(node_specification); + break; + case CustomOpUpdateOptions::kNoSideEffect: + custom_op_map[node_name].no_side_effect = + GetBooleanSpecs(node_specification); + break; + } + } +} + +bool ParseInputNodeQuantSpecs(const absl::string_view node_names, + const absl::string_view min_values, + const absl::string_view max_values, + const absl::string_view inference_type, + QuantizationSpecs* quant_specs) { + const std::vector input_nodes = absl::StrSplit(node_names, ','); + std::vector> node_mins; + if (!min_values.empty()) { + std::vector node_mins_str = absl::StrSplit(min_values, ','); + for (const std::string& node_mins_str : node_mins_str) { + double value; + if (!absl::SimpleAtod(node_mins_str, &value)) { + llvm::errs() << "Unexpected mins: " << node_mins_str << "\n"; + return true; + } + node_mins.push_back(value); + } + } + + std::vector> node_maxs; + if (!max_values.empty()) { + const std::vector node_maxs_str = + absl::StrSplit(max_values, ','); + for (const std::string& node_maxs_str : node_maxs_str) { + double value; + if (!absl::SimpleAtod(node_maxs_str, &value)) { + llvm::errs() << "Unexpected mins: " << node_maxs_str << "\n"; + return true; + } + node_maxs.push_back(value); + } + } + + tensorflow::DataType final_type = tensorflow::DT_FLOAT; + if (!inference_type.empty() && !DataType_Parse(inference_type, &final_type)) { + return true; + } + return GetInputNodeQuantSpecs(input_nodes, node_mins, node_maxs, final_type, + quant_specs); +} + +bool GetInputNodeQuantSpecs(const std::vector& node_names, + const std::vector>& node_mins, + const std::vector>& node_maxs, + const tensorflow::DataType inference_type, + QuantizationSpecs* quant_specs) { + quant_specs->inference_type = inference_type; + + // If min/max are not specified, just return; + if (node_mins.empty() || node_maxs.empty()) return false; + + // Otherwise make sure min/max has the same size as inputs. + if (IsQuantizationType(inference_type)) { + // min/max should have same size as inputs, or shouldn't be specified. + if (node_names.size() != node_mins.size() || + node_names.size() != node_maxs.size()) { + return true; + } + for (int i = 0; i < node_names.size(); ++i) { + quant_specs->input_ranges.push_back({node_mins[i], node_maxs[i]}); + } + return false; + } + if (!node_mins.empty()) { + llvm::dbgs() << "Ignored input_min_values."; + } + if (!node_maxs.empty()) { + llvm::dbgs() << "Ignored input_max_values."; + } + return false; +} + +std::string GetQDQQuantModeString(const QDQConversionMode mode) { + switch (mode) { + case QDQConversionMode::kQDQStatic: + return "Static"; + case QDQConversionMode::kQDQDynamic: + return "Dynamic"; + case QDQConversionMode::kQDQStrict: + return "Strict"; + default: + return "NoQDQ"; + } +} + +QDQConversionMode GetQDQQuantModeFromString(const std::string& mode_str) { + if (mode_str == "Static") return QDQConversionMode::kQDQStatic; + if (mode_str == "Dynamic") return QDQConversionMode::kQDQDynamic; + if (mode_str == "Strict") return QDQConversionMode::kQDQStrict; + return QDQConversionMode::kQDQNone; +} + +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_config.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_config.h new file mode 100644 index 0000000000000000000000000000000000000000..91a64457626ac3a0fec178a7eec1cd5961622dbd --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_config.h @@ -0,0 +1,255 @@ +/* Copyright 2019 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. +==============================================================================*/ + +// This header file defines node specs for quantization and the methods to parse +// command line flags to these specs. + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_COMMON_QUANTIZATION_LIB_QUANTIZATION_CONFIG_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_COMMON_QUANTIZATION_LIB_QUANTIZATION_CONFIG_H_ + +#include +#include +#include +#include +#include +#include + +#include "absl/container/flat_hash_set.h" +#include "absl/strings/string_view.h" +#include "tensorflow/compiler/mlir/lite/tools/optimize/reduced_precision_metadata.h" +#include "tensorflow/core/framework/types.pb.h" + +namespace mlir { +namespace TFL { + +// Stores information about how to quantize a user-specified custom operation. +struct CustomOpInfo { + std::vector quantizable_input_indices; + bool is_weight_only = false; + bool no_side_effect = true; +}; + +using CustomOpMap = std::unordered_map; +enum CustomOpUpdateOptions { kInputIndices, kWeightOnly, kNoSideEffect }; +enum class QDQConversionMode { kQDQNone, kQDQStatic, kQDQDynamic, kQDQStrict }; + +struct QuantizationSpecs { + // Which function this node quant specifications belong to. + std::string target_func = "main"; + + // Whether to trigger quantization passses for post-training quantization. + // If true, the model input doesn't require user specified input ranges. + bool post_training_quantization = false; + + // Whether to allow dynamic range quantization. This is the easiest + // quantization mode which doesn't require QAT or sample inputs. + // This option only targets `DT_HALF` and `DT_QINT8` inference type. + bool weight_quantization = false; + + // Whether to use the MLIR dynamic range quantizer instead of TOCO. + bool enable_mlir_dynamic_range_quantizer = false; + + // Whether to allow weight-only quantization. This scheme quantizes + // weights but will dequantize them back at runtime which is useful for + // memory bound case without kernel support available in lower precisions. + // Used in MLIR dynamic range quantizer. + bool weight_only_quantization = false; + + // The minimum number of elements in a weights array required to apply + // quantization. This is especially useful not to quantize small tensors as + // it is hard to get performance benefits from them with quantization. Used + // in MLIR dynamic range quantizer with int8 weight data type. + int64_t minimum_elements_for_weights = 1024; + + // Whether to calculate scales in float to keep quantized values the same with + // old TOCO quantizer. + bool legacy_float_scale = false; + + // Whether to perform per-tensor quantization. Currently, this option is only + // valid when the quantization parameters need to be created by scanning the + // constant content (post-training quantization or QAT without weight + // FakeQuant). + bool disable_per_channel = false; + + // Whether to disable per-channel weight quantization and enable legacy per + // tensor quantization. The legacy quantization for Dense layers is + // inconsistent with Conv 1x1 which always performs per channel quantization. + bool disable_per_channel_for_dense_layers = false; + + // Whether to use fixed output ranges of the activation ops (tanh, sigmoid, + // etc.) and not infer weight constants. + // If this option is set, quantization emulation ops should be placed after + // the ops in the input graph. This flag should be set to false for + // post-training quantization. + bool disable_infer_tensor_range = false; + + // Whether to use the unfrozen variable quantization in MLIR. Typically, + // variables are frozen for passing passes, but some variables aren't frozen. + // If it is true, QuantizeVariables pass will be added after the + // PrepareQuantizePass. + bool enable_mlir_variable_quantization = false; + + // The node type when the model is exported. Currently this is limited to + // DT_FLOAT, DT_HALF, DT_QINT8, and DT_QUINT8. When DT_HALF is used, the + // `weight_quantization` flag needs to set to true. When DT_QUINT8 is used, + // the `weight_quantization` flag needs to set to false. + tensorflow::DataType inference_type = tensorflow::DT_FLOAT; + + // The input and output data type during inference. This flag is only used + // when `inference_type` is different from DT_FLOAT. This flag can only be set + // to DT_FLOAT or as same as `inference_type`. If this flag is different + // from `inference_type`, adaptor ops are inserted as heading and tailing ops + // in the result model. + tensorflow::DataType inference_input_type = tensorflow::DT_FLOAT; + + // Input node ranges. These ranges are stored as the same order of function + // arguments. They are only used when `weight_quantization` is set to false, + // and the model is required to have quantization parameters, either from + // quantization aware training or calibration, for the remaining tensors. + std::vector, std::optional>> + input_ranges; + + // Whether to disable setting the quantization parameters of the input nodes + // using input ranges. + bool disable_set_input_nodes_quantization_params = false; + + // The default ranges can be used when a tensor doesn't have quantization + // parameters and couldn't be quantized. Used only for latency tests. + std::pair, std::optional> default_ranges; + + // A serialized "QuantizationInfo" object to specify value ranges for some of + // the tensors with known names. + std::string serialized_quant_stats = ""; + + // A bitmask to encode support for reduced precision inference in the model. + tflite::optimize::ReducedPrecisionSupport support_mask = + tflite::optimize::ReducedPrecisionSupport::None; + + // Whether to run the passes to propagate the quantization parameters and + // graph rewrites. Returns false if the inference_type is DT_FLOAT or + // `weight_quantization` flag is set. + bool RunPropagationAndRewriteQuantizationPasses() const { + return inference_type != tensorflow::DT_FLOAT && !weight_quantization; + } + + // TODO: b/202075505 - make implicit weight type clearer + // Whether run the passes and graph rewrites for dynamic range quantization. + bool RunAndRewriteDynamicRangeQuantizationPasses() const { + bool dynamic_range_quantize = + (inference_type != tensorflow::DT_FLOAT) && weight_quantization && + !post_training_quantization && !disable_infer_tensor_range && + enable_mlir_dynamic_range_quantizer; + return dynamic_range_quantize; + } + + // Returns whether this inference type represents a signed storage type. + bool IsSignedInferenceType() const { + switch (inference_type) { + case tensorflow::DT_QUINT8: + case tensorflow::DT_QUINT16: + return false; + default: + return true; + } + } + + // Gets the width of this quantization type. Returns 0 if it isn't a + // quantization type. + int64_t GetQuantizationTypeWidth() const { + switch (inference_type) { + case tensorflow::DT_INT8: + case tensorflow::DT_UINT8: + case tensorflow::DT_QINT8: + case tensorflow::DT_QUINT8: + return 8; + case tensorflow::DT_INT16: + case tensorflow::DT_UINT16: + case tensorflow::DT_QINT16: + case tensorflow::DT_QUINT16: + return 16; + case tensorflow::DT_INT32: + case tensorflow::DT_QINT32: + return 32; + default: + return 0; + } + } + + // Whether to add the NumericVerify ops to verify numbers before and after + // quantization. + bool verify_numeric = false; + // Whether to add verification for layer by layer, or on whole model. When + // disabled (per-layer) float and quantized ops will be run from same input + // (output of previous quantized layer). When enabled, float and quantized ops + // will run with respective float and quantized output of previous ops. + bool whole_model_verify = false; + + // Whether to use fake quant attributes to calculate quantization parameters. + bool use_fake_quant_num_bits = false; + + // Names of ops to block from quantization. Used in QuantizePass. + // For dynamic range quantization, ops in blocklist are quantized in weight- + // only manner. + absl::flat_hash_set ops_blocklist; + + // Names of locations to block from quantization. Used in QuantizePass. + absl::flat_hash_set nodes_blocklist; + + // Map from custom op code to custom op quantization information. + // For dynamic range quantization, among the custom ops in the graph those + // specified in this map are subject to quantization. + CustomOpMap custom_map; + + // If other than kQDQNone, the model is a floating point graph with QDQ ops + // to be eliminated and fused into quantized kernels. + QDQConversionMode qdq_conversion_mode = QDQConversionMode::kQDQNone; + + // When set, adheres to the QDQ annotations added by the framework when + // possible rather than quantizing any op that is possible to quantize. + bool strict_qdq_mode = false; +}; + +// Parses the command line flag strings to the CustomOpMap specification. +void ParseCustomOpSpecs(absl::string_view node_names, + const CustomOpUpdateOptions& update_option, + CustomOpMap& custom_op_map); + +// Parses the command line flag strings to the quantization specification for +// input arrays of a graph. The array names are not stored in the spec, and will +// be matched by position. Returns true if failed. +bool ParseInputNodeQuantSpecs(absl::string_view node_names, + absl::string_view min_values, + absl::string_view max_values, + absl::string_view inference_type, + QuantizationSpecs* quant_specs); + +// Gets the quantization specification for input arrays. The array names are not +// stored in the spec, and will be matched by position. The min/max will be +// ignored if the inference_type isn't a quantized type. Returns true if failed. +bool GetInputNodeQuantSpecs(const std::vector& node_names, + const std::vector>& node_mins, + const std::vector>& node_maxs, + tensorflow::DataType inference_type, + QuantizationSpecs* quant_specs); + +// Returns a human-readable string of the QDQQuantMode enum class +std::string GetQDQQuantModeString(QDQConversionMode mode); + +// Returns the QDQQuantMode enum class from a human-readable string +QDQConversionMode GetQDQQuantModeFromString(const std::string& mode_str); +} // namespace TFL +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_COMMON_QUANTIZATION_LIB_QUANTIZATION_CONFIG_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_driver.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_driver.cc new file mode 100644 index 0000000000000000000000000000000000000000..1b23c3a42c9f9b91c28ca1b61782d4a8cdc6e850 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_driver.cc @@ -0,0 +1,959 @@ +/* Copyright 2019 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/compiler/mlir/lite/quantization/common/quantization_lib/quantization_driver.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/SmallVector.h" +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Dialect/Quant/IR/QuantTypes.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/Matchers.h" // from @llvm-project +#include "mlir/IR/OpDefinition.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_traits.h" +#include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_utils.h" +#include "tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.h" + +namespace mlir { +namespace TFL { +namespace { + +constexpr int32_t kBiasMax = std::numeric_limits::max() / 2; + +// Uses the type of `value` to set the initial state of the index-th result if +// `as_result` is true or index-th operand if `as_result` is false. The state +// is immutable if the type is a quantized type. Returns the index of this +// new state in the state vector. +void InitializeStateForValue( + Operation* op, const int index, const Value value, const bool as_result, + std::vector& states, + DenseMap& value_to_state, + DenseMap& operand_states, + DenseMap& result_states) { + const auto [cached, inserted] = value_to_state.try_emplace(value, 0); + if (!inserted) { + if (as_result) { + result_states[{op, index}] = cached->second; + } else { + operand_states[{op, index}] = cached->second; + } + return; + } + + const QuantizedType quantized_type = + QuantizedType::getQuantizedElementType(value.getType()); + + const bool immutable = quantized_type != nullptr; + const QuantizationDriver::QuantStateIndex next_state_index = states.size(); + states.push_back({quantized_type, immutable}); + if (as_result) { + result_states[{op, index}] = next_state_index; + } else { + operand_states[{op, index}] = next_state_index; + } + + cached->second = next_state_index; +} + +bool HasPerAxisQuantizedOperand(Operation* op) { + for (int i = 0; i < op->getNumOperands(); ++i) { + if (auto dq_op = dyn_cast_or_null( + op->getOperand(i).getDefiningOp())) { + auto type = + mlir::cast(dq_op.getArg().getType()).getElementType(); + if (auto per_axis_qtype = + mlir::dyn_cast_or_null( + QuantizedType::getQuantizedElementType(type))) { + return true; + } + } + } + return false; +} + +} // namespace + +void QuantizationDriver::InitializeArgState(const BlockArgument arg, + const Value arg_value) { + const auto [cached, inserted] = value_to_state_.try_emplace(arg_value, 0); + if (!inserted) { + arg_states_[arg] = cached->second; + return; + } + + const QuantizedType quantized_type = + QuantizedType::getQuantizedElementType(arg_value.getType()); + const bool immutable = quantized_type != nullptr; + const QuantizationDriver::QuantStateIndex next_state_index = states_.size(); + states_.push_back({quantized_type, immutable}); + arg_states_[arg] = next_state_index; + cached->second = next_state_index; +} + +void QuantizationDriver::InitializeOperandState(Operation* op, const int index, + const Value value) { + InitializeStateForValue(op, index, value, /*as_result=*/false, states_, + value_to_state_, operand_states_, result_states_); +} + +void QuantizationDriver::InitializeResultState(Operation* op, const int index, + const Value value) { + InitializeStateForValue(op, index, value, /*as_result=*/true, states_, + value_to_state_, operand_states_, result_states_); +} + +std::unique_ptr QuantizationDriver::GetQuantSpec(Operation* op) { + return op_quant_spec_getter_(op); +} + +std::unique_ptr QuantizationDriver::GetQuantScaleSpec( + Operation* op) { + return op_quant_scale_spec_getter_(op); +} + +bool QuantizationDriver::IsQuantized(Operation* op) { + for (int i = 0; i < op->getNumResults(); ++i) { + if (GetResultQuantState(op, i).IsEmpty()) return false; + } + return true; +} + +bool QuantizationDriver::SetConstantResultParams(Operation* op) { + DenseFPElementsAttr attr; + const Value result = op->getResult(0); + if (!matchPattern(result, m_Constant(&attr))) { + return false; + } + // TODO: b/323478683 - Make storage_type_width and narrow_range configurable. + Type final_type; + const auto it = optimized_weights_.find(op); + const bool is_weight = it != optimized_weights_.end(); + const bool is_weight_with_per_channel_support = + is_weight && it->second != -1 && is_signed_; + + if (is_weight_with_per_channel_support && !disable_per_channel_) { + // When `disable_per_channel_` is false, per-channel symmetric quantization + // parameters are created from the weights when the ops support per-channel + // quantization. Otherwise, uses per-tensor asymmetric quantization with + // narrow range. + + // per-axis quantization weight, with symmetric min/max enforced. + final_type = GetUniformQuantizedPerAxisTypeForWeight( + attr, it->second, /*symmetric=*/true, /*num_bits=*/8, is_signed_, + /*narrow_range=*/true, legacy_float_scale_); + } else { + // per-tensor quantization weight + final_type = GetUniformQuantizedTypeForWeight( + attr, /*symmetric=*/is_weight && is_signed_, + /*num_bits=*/8, is_signed_, + /*narrow_range=*/is_weight, legacy_float_scale_); + } + if (const auto quant_type = mlir::dyn_cast_or_null(final_type); + quant_type != nullptr) { + return SetResultParams(op, /*result_index=*/0, quant_type); + } + return false; +} + +bool QuantizationDriver::SetResultParams(Operation* op, const int result_index, + const QuantizedType quantized_type) { + QuantState& state = GetResultQuantState(op, result_index); + if (state.params == quantized_type) { + return false; + } + if (!state.IsEmpty()) { + RequantizeStates& rescales = GetResultRequantizeStates(op, result_index); + RequantizeState& rescale = rescales.emplace_back(); + rescale.pos = RequantizeState::ON_INPUT; + rescale.params = quantized_type; + return true; + } + state.params = quantized_type; + AddUserToList(op, result_index); + return true; +} + +QuantizedType QuantizationDriver::GetBiasParams( + Operation* op, const int bias_index, + const ArrayRef non_bias_operand_indices, + const AccumulatorScaleFunc func) { + QuantState& bias_state = GetOperandQuantState(op, bias_index); + if (!bias_state.IsEmpty()) { + return bias_state.params; + } + std::vector op_types{}; + op_types.reserve(non_bias_operand_indices.size()); + + int adjusted_quant_dim = -1; + if (op->getNumOperands() > bias_index) { + // Some kernels allow 1D bias, broadcasting it inside the kernel. In this + // case, the `quantizedDimension=0` when quantizing per-channel. + // However, for some kernels which require bias to be already broadcasted + // to match the accumulation shape, the very last index should be used. + Operation* bias_op = op->getOperand(bias_index).getDefiningOp(); + if (bias_op != nullptr) { + Type bias_type = bias_op->getResult(0).getType(); + if (bias_type != builder_.getNoneType()) { + const int bias_rank = mlir::dyn_cast(bias_type).getRank(); + adjusted_quant_dim = bias_rank > 1 ? bias_rank - 1 : 0; + } + } + } + + for (const int non_bias_operand_index : non_bias_operand_indices) { + const QuantState& non_bias_state = + GetOperandQuantState(op, non_bias_operand_index); + op_types.push_back(non_bias_state.params); + } + return func(op_types, adjusted_quant_dim, legacy_float_scale_); +} + +bool QuantizationDriver::SetOperandParams(Operation* op, + const int operand_index, + const QuantizedType quantized_type, + const bool override) { + QuantState& state = GetOperandQuantState(op, operand_index); + if (state.params == quantized_type) { + return false; + } + + if (!state.IsEmpty() && !override) { + RequantizeStates& rescales = GetOperandRequantizeStates(op, operand_index); + for (RequantizeState& rescale : rescales) { + if (rescale.params == quantized_type) { + rescale.users.emplace_back(op, operand_index); + return true; + } + } + RequantizeState& rescale = rescales.emplace_back(); + rescale.pos = RequantizeState::ON_OUTPUT; + rescale.params = quantized_type; + rescale.users.emplace_back(op, operand_index); + return true; + } + + state.params = quantized_type; + AddOperandToList(op, operand_index); + return true; +} + +void QuantizationDriver::QuantizeOpResult(Operation* op, const int result_index, + const QuantizedType quantized_type) { + builder_.setInsertionPointAfter(op); + const Value original_result = op->getResult(result_index); + QuantizeValue(original_result, quantized_type, op->getLoc()); +} + +void QuantizationDriver::QuantizeArg(BlockArgument arg, + const QuantizedType quantized_type) { + builder_.setInsertionPointToStart(arg.getOwner()); + QuantizeValue(arg, quantized_type, builder_.getUnknownLoc()); +} + +void QuantizationDriver::QuantizeValue(Value value, + QuantizedType quantized_type, + const Location loc) { + const Type expressed_type = value.getType(); + const Type new_value_type = + quantized_type.castFromExpressedType(expressed_type); + // Skip if `value` or `value`'s element type doesn't match the expressed type + // of `quantized_type`. + if (new_value_type == nullptr) return; + + auto quantize = + quantfork::QuantizeCastOp::create(builder_, loc, new_value_type, value); + auto dequantize = quantfork::DequantizeCastOp::create( + builder_, loc, expressed_type, quantize.getResult()); + + // This attribute is set to distinguish the quantize ops being added by the + // quantization pass. These ops can be removed without losing original + // program accuracy. + // TODO: b/323478683 - Make the attribute being part of op definition. + quantize->setAttr(kVolatileOpAttrName, builder_.getUnitAttr()); + + // `original_result` has a use to `quantize`, so this will replace that use + // by the result of `dequantize`. Remember to reset that use afterwards + value.replaceAllUsesWith(dequantize); + quantize.getOperation()->replaceUsesOfWith(dequantize, value); +} + +void QuantizationDriver::RequantizeOpResult(Operation* op, + const int result_index, + RequantizeStates& states) { + if (states.empty()) return; + + builder_.setInsertionPointAfter(op); + Value value = op->getResult(result_index); + RequantizeState::RequantizePosition pos = states.front().pos; + if (pos == RequantizeState::NO_REQUANTIZE) { + return; + } + for (const RequantizeState& state : states) { + // Check that all requantization positions are the same for each state. + // Unsure if this check is required. + if (state.pos != pos) { + return; + } + } + if (pos == RequantizeState::ON_OUTPUT) { + Operation* user = value.getUses().begin().getUser(); + if (isa(user)) { + // The requantize op is inserted between `quantize` and `dequantize` ops. + value = user->getResult(0); + builder_.setInsertionPointAfter(user); + } + } + RequantizeValue(value, states, op->getLoc()); +} + +void QuantizationDriver::RequantizeArg(const BlockArgument arg, + RequantizeStates& states) { + Value value = arg; + builder_.setInsertionPointToStart(arg.getOwner()); + if (value.hasOneUse()) { + Operation* user = value.use_begin().getUser(); + if (auto q = dyn_cast(user)) { + value = q.getResult(); + builder_.setInsertionPoint(arg.getOwner(), ++Block::iterator(user)); + } + } + RequantizeValue(value, states, builder_.getUnknownLoc()); +} + +void QuantizationDriver::RequantizeValue(Value value, RequantizeStates& states, + const Location loc) { + if (states.empty() || states.front().pos == RequantizeState::NO_REQUANTIZE) { + return; + } + if (states.front().pos == RequantizeState::ON_INPUT) { + RequantizeState& state = states.front(); + const Type expressed_type = value.getType(); + // The value needs to be requantized. A Quantize op will be created to use + // it as the operand and replace its uses. + const Type new_type = state.params.castFromExpressedType(expressed_type); + if (!new_type) return; + auto requantize_op = + quantfork::QuantizeCastOp::create(builder_, loc, new_type, value); + value.replaceAllUsesWith(requantize_op); + requantize_op.getOperation()->replaceUsesOfWith(requantize_op, value); + // This requantization was defined as required for the result value, so + // there should be only one requant state. + return; + } + + // If this is an operand that requires requantization, then the value should + // only have one `DequantizeCastOp` user which produces the operand value. + if (!value.hasOneUse()) { + return; + } + auto dequant_op = dyn_cast_or_null( + value.use_begin().getUser()); + if (!dequant_op) { + return; + } + // It is possible that the dequant value is used by a op that doesn't require + // requant, so only overwrite the first if that is not the case. + const int num_uses = std::distance(dequant_op.getResult().use_begin(), + dequant_op.getResult().use_end()); + + // Whether to replace quantization params of the first dequantize op + // after the quantized value is produced. + // If there is a use other than the requantize states, then we can't clobber. + bool clobber_first = num_uses <= states.size(); + for (RequantizeState& state : states) { + Type expressed_type = QuantizedType::castToExpressedType(value.getType()); + if (!expressed_type) continue; + // The value needs to be requantized. A Quantize op will be created to use + // it as the operand and replace its uses. + const Type new_type = state.params.castFromExpressedType(expressed_type); + // This value isn't an expressed type (float), skip. + if (!new_type) continue; + + auto requantize_op = + quantfork::QuantizeCastOp::create(builder_, loc, new_type, value); + + if (clobber_first) { + dequant_op.setOperand(requantize_op.getResult()); + // All ops requiring this value already use the result of dequant. + clobber_first = false; + } else { + auto new_dequant_op = quantfork::DequantizeCastOp::create( + builder_, loc, dequant_op.getResult().getType(), + requantize_op.getResult()); + for (auto [op, operand_idx] : state.users) { + op->setOperand(operand_idx, new_dequant_op.getResult()); + } + } + } +} + +// A heuristic to get quantization parameters satisfies the same scale +// constraints: +// - If there are immutable states, +// - use the single input, or, +// - use the single output, or, +// - use the first one in the collection, +// - use the single input if it is ready, or, +// - use the single output if it is ready, or, +// - use the first ready one in the collection. +QuantizedType QuantizationDriver::GetQuantParamsForSameScaleConstraint( + Operation* op) { + // Two vector to collect Non-empty operands and results states. + std::vector mutable_states, immutable_states; + for (int i = 0; i < op->getNumOperands(); ++i) { + QuantState& state = GetOperandQuantState(op, i); + if (state.immutable) { + immutable_states.push_back(&state); + } else if (!state.IsEmpty()) { + mutable_states.push_back(&state); + } + } + + const int immutable_operands_num = immutable_states.size(); + const int mutable_operands_num = mutable_states.size(); + // Use the operand's state if it is immutable and it is the only one + // operand. + if (op->getNumOperands() == 1 && immutable_operands_num == 1) { + return immutable_states.front()->params; + } + + for (int i = 0; i < op->getNumResults(); ++i) { + QuantState& state = GetResultQuantState(op, i); + if (state.immutable) { + immutable_states.push_back(&state); + } else if (!state.IsEmpty()) { + mutable_states.push_back(&state); + } + } + + const int immutable_results_num = + immutable_states.size() - immutable_operands_num; + const int mutable_results_num = mutable_states.size() - mutable_operands_num; + // Use the result's state if it is immutable and it is the only one result. + if (op->getNumResults() == 1 && immutable_results_num == 1) { + return immutable_states.back()->params; + } + + // Use the first immutable state to quantize the rest operands and results. + if (!immutable_states.empty()) return immutable_states.front()->params; + + // If there are no immutable states, use the operand's state if it is the + // only one operand and has parameters propagated. + if (op->getNumOperands() == 1 && mutable_operands_num == 1) { + return mutable_states.front()->params; + } + + // If there are no immutable states, use the result's state if it is the + // only one result and has parameters propagated. + if (op->getNumResults() == 1 && mutable_results_num == 1) { + return mutable_states.back()->params; + } + + // Use the first propagated state to quantize the rest operands and results. + if (!mutable_states.empty()) return mutable_states.front()->params; + + // None operands/results have parameters propagated, skip this node for now. + return {}; +} + +void QuantizationDriver::PreprocessConstantOps() { + fn_.walk([&](arith::ConstantOp cst) { + // Non-float tensors are neither weights nor require quantization. + const auto type = mlir::dyn_cast(cst.getType()); + if (!type || !mlir::isa(type.getElementType())) return; + + // Skip if the value is NaN or INF. + // Otherwise the illegal scale/zp will be calculated. + auto float_attr = mlir::dyn_cast(cst.getValueAttr()); + if (float_attr && (float_attr.getValues().empty() || + !float_attr.getValues()[0].isFinite())) { + return; + } + + const Value value = cst.getResult(); + builder_.setInsertionPoint(cst); + + // The following loop will change the value uses, thus we cache all the uses + // needs to be changed. + SmallVector> uses; + for (OpOperand& use : value.getUses()) { + uses.push_back({use.getOwner(), use.getOperandNumber()}); + } + for (const auto [user, operand_num] : uses) { + const std::unique_ptr spec = GetQuantSpec(user); + const std::unique_ptr scale_spec = + GetQuantScaleSpec(user); + const BiasParamsMap biases = spec->biases_params; + + // The quantization parameters of a `weight` shouldn't be determined by + // other values. So any constants which are not bias, an operand of an + // op with same scale requirements, and haven't been quantized are + // weights. + if (!biases.contains(operand_num) && + !scale_spec->has_same_scale_requirement && + !dyn_cast(user)) { + // Needs to scan the content of weights to get the quantization + // parameters if there are no quantization parameters (FakeQuant ops). + // For this case, the weight will not be duplicated. + weights_.insert(cst); + if (spec->coeff_op_quant_dim.find(operand_num) != + spec->coeff_op_quant_dim.end()) { + optimized_weights_.insert( + {cst, spec->coeff_op_quant_dim[operand_num]}); + } + } else { + // This is a bias or an operand of an op with same scale requirements, + // so the quantization parameter are propagated from or determined by + // other values. Duplicate this constant in case it is shared by + // different users. + if (uses.size() > 1) { + auto new_constant_op = + arith::ConstantOp::create(builder_, cst.getLoc(), cst.getValue()); + user->setOperand(operand_num, new_constant_op); + } + } + } + }); +} + +void QuantizationDriver::SetupAllStates() { + for (BlockArgument arg : fn_.getArguments()) { + args_.push_back(arg); + Value value = arg; + // If the argument is quantized, it should only has one user. + if (arg.hasOneUse()) { + Operation* user = value.use_begin().getUser(); + if (auto q = dyn_cast(user)) { + value = q.getResult(); + } + } + InitializeArgState(arg, value); + } + + fn_.walk([&](Operation* op) { + std::unique_ptr scale_spec = GetQuantScaleSpec(op); + if (!IsOpQuantizable(op) && !scale_spec->has_same_scale_requirement) { + return; + } + work_list_.push_back(op); + + for (int i = 0; i < op->getNumOperands(); ++i) { + Value operand = op->getOperand(i); + if (Operation* inst = operand.getDefiningOp()) { + // If the operand comes from a `quantfork::DequantizeCastOp`, we use + // the quantized input of this `quantfork::DequantizeCastOp` to set the + // state. + if (auto dq = dyn_cast(inst)) { + operand = dq.getArg(); + } + } + InitializeOperandState(op, i, operand); + } + + for (int i = 0; i < op->getNumResults(); ++i) { + Value result = op->getResult(i); + // If the result has been quantized, it should only be used by a + // `quantfork::QuantizeCastOp`. For this case, we uses the quantized + // result to create the state and mark it immutable. + if (result.hasOneUse()) { + Operation* user = result.use_begin().getUser(); + if (auto q = dyn_cast(user)) { + result = q.getResult(); + } + } + InitializeResultState(op, i, result); + } + }); +} + +arith::ConstantOp QuantizationDriver::DuplicateConstantOpIfNeeded( + arith::ConstantOp op, Operation* target_op, const int operand_index) { + if (op.getResult().hasOneUse()) { + return op; + } + OpBuilder builder(op->getContext()); + builder.setInsertionPointAfter(op); + arith::ConstantOp new_op = cast(builder.clone(*op)); + target_op->getOpOperand(operand_index).set(new_op.getResult()); + InitializeOperandState(target_op, operand_index, new_op.getResult()); + InitializeResultState(new_op, 0, new_op.getResult()); + return new_op; +} + +bool QuantizationDriver::ShouldCheckBiasScale( + Operation* op, const int bias_index, ArrayRef input_indices, + const QuantizedType quantized_type, int& input_index, int& filter_index) { + // For now, restrict scale adjustment to ops with affine quantized weights, + // and having weights and biases as constants. This currently only applies to + // FC and Conv* ops. Restriction for the weight can be relaxed if there are + // needs for adjusting scale of variable weights. + auto affine_op = dyn_cast(op); + auto bias_op = op->getOperand(bias_index).getDefiningOp(); + if (!affine_op || !bias_op || input_indices.size() != 2) return false; + if (!mlir::isa(bias_op.getValue())) return false; + filter_index = affine_op.GetAffineOperandIndex(); + if (!op->getOperand(filter_index).getDefiningOp()) { + return false; + } + if (filter_index == input_indices[0]) { + input_index = input_indices[1]; + } else if (filter_index == input_indices[1]) { + input_index = input_indices[0]; + } else { + return false; + } + + const QuantState& input_state = GetOperandQuantState(op, input_index); + const QuantState& filter_state = GetOperandQuantState(op, filter_index); + // If quantization parameter for the filter is fixed, should return it as-is. + // Only checks ops with 8-bit input and weights, and 32-bit biases. + return input_state.params.getStorageTypeIntegralWidth() == 8 && + filter_state.params.getStorageTypeIntegralWidth() == 8 && + quantized_type.getStorageTypeIntegralWidth() == 32; +} + +bool QuantizationDriver::SetBiasParamsWithAdjustments( + Operation* op, const int bias_index, ArrayRef input_indices, + const QuantizedType params) { + bool changed = false; + + int input_index; + int filter_index; + if (!ShouldCheckBiasScale(op, bias_index, input_indices, params, input_index, + filter_index)) { + return SetOperandParams(op, bias_index, params); + } + + QuantState input_state = GetOperandQuantState(op, input_index); + QuantState filter_state = GetOperandQuantState(op, filter_index); + auto bias_op = op->getOperand(bias_index).getDefiningOp(); + const double input_scale = + mlir::cast(input_state.params).getScale(); + + auto bias_values = mlir::cast(bias_op.getValue()); + // Restrict maximum absolute value of bias within INT_MAX / 2, to make some + // room for accumulator. + if (auto bias_quantized_type = mlir::dyn_cast(params); + bias_quantized_type != nullptr) { + double bias_half_range = 0.0f; + for (auto bias : bias_values.getValues()) { + if (bias_half_range < std::abs(bias.convertToFloat())) { + bias_half_range = std::abs(bias.convertToFloat()); + } + } + if (bias_half_range / bias_quantized_type.getScale() < kBiasMax) { + return SetOperandParams(op, bias_index, params); + } + const double new_bias_scale = + static_cast(bias_half_range) / kBiasMax; + + changed |= SetOperandParams( + op, bias_index, + UniformQuantizedType::getChecked( + bias_op->getLoc(), params.getFlags(), params.getStorageType(), + params.getExpressedType(), new_bias_scale, 0, + params.getStorageTypeMin(), params.getStorageTypeMax())); + arith::ConstantOp filter_op = DuplicateConstantOpIfNeeded( + op->getOperand(filter_index).getDefiningOp(), op, + filter_index); + if (!filter_op) { + return SetOperandParams(op, bias_index, params); + } + + const auto filter_quantized_type = + mlir::cast(filter_state.params); + changed |= SetOperandParams( + op, filter_index, + UniformQuantizedType::getChecked( + filter_op->getLoc(), filter_quantized_type.getFlags(), + filter_quantized_type.getStorageType(), + filter_quantized_type.getExpressedType(), + new_bias_scale / input_scale, 0, + filter_quantized_type.getStorageTypeMin(), + filter_quantized_type.getStorageTypeMax()), + /*override=*/true); + } else if (auto bias_quantized_type = + mlir::dyn_cast(params); + bias_quantized_type != nullptr) { + const auto filter_quantized_type = + mlir::cast(filter_state.params); + std::vector new_bias_scales = bias_quantized_type.getScales().vec(); + std::vector new_filter_scales = + filter_quantized_type.getScales().vec(); + + bool needs_adjustment = false; + for (int i = 0; i < bias_quantized_type.getScales().size(); ++i) { + const float abs_bias = std::abs(bias_values.getValues()[i]); + if (abs_bias / new_bias_scales[i] > kBiasMax) { + new_bias_scales[i] = static_cast(abs_bias) / kBiasMax; + new_filter_scales[i] = new_bias_scales[i] / input_scale; + needs_adjustment = true; + } + } + if (!needs_adjustment) { + return SetOperandParams(op, bias_index, params); + } + changed |= SetOperandParams( + op, bias_index, + quant::UniformQuantizedPerAxisType::getChecked( + bias_op->getLoc(), params.getFlags(), params.getStorageType(), + params.getExpressedType(), new_bias_scales, + bias_quantized_type.getZeroPoints(), + bias_quantized_type.getQuantizedDimension(), + params.getStorageTypeMin(), params.getStorageTypeMax())); + + arith::ConstantOp filter_op = DuplicateConstantOpIfNeeded( + op->getOperand(filter_index).getDefiningOp(), op, + filter_index); + changed |= SetOperandParams( + op, filter_index, + quant::UniformQuantizedPerAxisType::getChecked( + filter_op->getLoc(), filter_quantized_type.getFlags(), + filter_quantized_type.getStorageType(), + filter_quantized_type.getExpressedType(), new_filter_scales, + filter_quantized_type.getZeroPoints(), + filter_quantized_type.getQuantizedDimension(), + filter_quantized_type.getStorageTypeMin(), + filter_quantized_type.getStorageTypeMax()), + /*override=*/true); + } + return changed; +} + +// This method scans the operations in the function to setup the initial +// states for quantization parameter propagation. +// TODO: b/323478683 - This algorithm assumes there are only one pair of +// `quantfork::QuantizeCastOp` and `quantfork::DequantizeCastOp` ops between two +// quantizable ops. A sanity check should be applied. +void QuantizationDriver::Initialize() { + // Duplicate the bias constant, so the states can be setup correctly. + // TODO: b/323478683 - Function definition should also be duplicated if there + // are multiple call sites. + PreprocessConstantOps(); + + // Setup all the internal states. + SetupAllStates(); +} + +// Propagates the quantization parameters to the operands, results, and biases. +// TODO: b/323478683 - Do not use while loop to handle this logic. +bool QuantizationDriver::PropagateParamsAndReturnIfChanged() { + // TODO: b/323478683 - Use a typed indicator instead of a bool value. + bool changed = false; + while (!work_list_.empty()) { + Operation* op = work_list_.back(); + work_list_.pop_back(); + + // This op has been quantized, so we should not consider it again. + if (quantized_.contains(op)) continue; + quantized_.insert(op); + + if (auto constant_op = dyn_cast(op); constant_op) { + // If the workflow requires inferring ranges from the content + // (post-training quantization) and it is weight (filter) and hasn't + // been quantized, we infer the quantization parameters from the content. + if (infer_tensor_range_ && IsWeight(constant_op) && !IsQuantized(op)) { + // The quantization parameters are determined by the content of the + // constant. + changed |= SetConstantResultParams(op); + } + continue; + } + + std::unique_ptr scale_spec = GetQuantScaleSpec(op); + + if (scale_spec->has_same_scale_requirement) { + const QuantizedType params = GetQuantParamsForSameScaleConstraint(op); + // The quantization parameters haven't been propagated to any operands + // or results. Skip this node for now. + if (!params) { + quantized_.erase(op); + continue; + } + + // If this is a QDQ conversion only, the op could have a same-scale + // requirement for the floating point kernel but allow per-axis + // quantization for the quantized kernel. If the quantized dimension + // changes, the following logic no longer works as the same `params` + // shouldn't be used for both input and output quantization params. + // E.g. During TransposeOp's quantization propagation in + // PrepareQuantize, if the quantization is per-axis and the + // QuantizedDimension is transposed, then the output q-dq params must + // reflect the new QuantizedDimension. So, check and skip the + // propagation if any of the operands has a per-axis quantized type param + // and `RequiredSameQuantizedAxes` set to false. + // Currently, these lines of code are only applicable to TFL_TransposeOp + // and TFL_ReshapeOp. And the output q-dq propagation for this Op is + // performed in `PropagateTransposedPerAxisQuantDim` and + // `PropagateReshapedPerAxisQuantDim` respectively. + if (is_qdq_conversion_ && + !scale_spec->required_same_quantized_axes_func()) { + if (HasPerAxisQuantizedOperand(op)) continue; + } + + // Use the final state to set all the operands' parameters. + for (int i = 0; i < op->getNumOperands(); ++i) { + if (auto type = + mlir::dyn_cast(op->getOperand(i).getType())) { + // Without this check, it will accidentally propagate the quantization + // information by the shared non-float tensors. + if (mlir::isa(type.getElementType())) + changed |= SetOperandParams(op, i, params); + } + } + + // Use the final state to set all the results' parameters. + for (int i = 0; i < op->getNumResults(); ++i) + if (auto type = mlir::dyn_cast(op->getResult(i).getType()); + type != nullptr) { + // Without this check, it will accidentally propagate the quantization + // information by the shared non-float-tensors. + if (mlir::isa(type.getElementType())) + changed |= SetResultParams(op, i, params); + } + } + + // If the model already contains immutable QDQs, require upstream to + // explicitly fix output range instead. + if (scale_spec->has_fixed_output_range && infer_tensor_range_ && + !is_qdq_conversion_) { + // Infer ranges from the activation ops. This is usually required for + // the post-training quantization workflow. + // TODO: b/323478683 - Different result can have different fixed range. + const QuantizedType params = + scale_spec->fixed_output_range_func(is_signed_, bit_width_); + for (auto i = 0; i < op->getNumResults(); ++i) { + // The range is null if the result has been quantized. + if (params) { + changed |= SetResultParams(op, i, params); + } + } + } + + const std::unique_ptr spec = GetQuantSpec(op); + for (const auto& [bias_operand_idx, non_bias_params] : + spec->biases_params) { + const auto& [non_bias_operand_indices, accumulator_scale_func] = + non_bias_params; + const QuantizedType params = + GetBiasParams(op, bias_operand_idx, non_bias_operand_indices, + accumulator_scale_func); + if (!params) { + quantized_.erase(op); + continue; + } + changed |= SetBiasParamsWithAdjustments(op, bias_operand_idx, + non_bias_operand_indices, params); + } + } + + return changed; +} + +// Finalizes the arguments and result states in the function. +void QuantizationDriver::Finalize() { + for (BlockArgument arg : args_) { + const QuantState& state = GetArgQuantState(arg); + RequantizeStates& requantizes = GetArgRequantizeStates(arg); + if (state.IsEmpty() || (state.immutable && requantizes.empty())) { + continue; + } + + if (!state.immutable) { + QuantizeArg(arg, state.params); + } + + if (!requantizes.empty()) { + RequantizeArg(arg, requantizes); + } + } + + for (const auto& [op_with_result_idx, quant_state_idx] : result_states_) { + const auto [op, result_idx] = op_with_result_idx; + const QuantState& state = GetResultQuantState(op, result_idx); + RequantizeStates& requantizes = GetResultRequantizeStates(op, result_idx); + if (state.IsEmpty() || (state.immutable && requantizes.empty())) { + continue; + } + + if (!state.immutable) { + QuantizeOpResult(op, result_idx, state.params); + } + + if (!requantizes.empty()) { + RequantizeOpResult(op, result_idx, requantizes); + } + } +} + +// Runs quantization in following steps: +// 1. Scans the operations in the function to setup the initial +// states for quantization parameter propagation. +// 2. Propagates the quantization parameters to the operands, results, and +// biases. +// 3. Finalizes the arguments and result states in the function. +void QuantizationDriver::Run() { + Initialize(); + if (PropagateParamsAndReturnIfChanged()) { + Finalize(); + } +} + +void ApplyQuantizationParamsPropagation( + const func::FuncOp func, const bool is_signed, const int bit_width, + const bool disable_per_channel, + const OpQuantSpecGetter op_quant_spec_getter, + const bool infer_tensor_ranges, const bool legacy_float_scale, + const bool is_qdq_conversion) { + ApplyQuantizationParamsPropagation( + func, is_signed, bit_width, disable_per_channel, op_quant_spec_getter, + GetDefaultQuantScaleSpec, infer_tensor_ranges, legacy_float_scale, + is_qdq_conversion); +} + +void ApplyQuantizationParamsPropagation( + const func::FuncOp func, const bool is_signed, const int bit_width, + const bool disable_per_channel, + const OpQuantSpecGetter op_quant_spec_getter, + const OpQuantScaleSpecGetter op_quant_scale_spec_getter, + const bool infer_tensor_ranges, const bool legacy_float_scale, + const bool is_qdq_conversion) { + QuantizationDriver(func, is_signed, bit_width, disable_per_channel, + op_quant_spec_getter, op_quant_scale_spec_getter, + infer_tensor_ranges, legacy_float_scale, is_qdq_conversion) + .Run(); +} + +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_driver.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_driver.h new file mode 100644 index 0000000000000000000000000000000000000000..96335a7be22c347e9d23a580bc2fff0ba5d56dae --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_driver.h @@ -0,0 +1,387 @@ +/* Copyright 2024 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_COMMON_QUANTIZATION_LIB_QUANTIZATION_DRIVER_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_COMMON_QUANTIZATION_LIB_QUANTIZATION_DRIVER_H_ + +#include +#include +#include + +#include "absl/container/flat_hash_map.h" +#include "absl/container/flat_hash_set.h" +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/STLExtras.h" +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Dialect/Quant/IR/QuantTypes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/OpDefinition.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_utils.h" + +namespace mlir { +namespace TFL { + +// The state for each op result during the quantization parameters propagation. +struct QuantState { + // Quantization parameters propagated to an op result. + QuantizedType params; + // A flag indicates this state (the params) shouldn't be changed after it is + // initialized. This flag will be set to true if the quantization parameters + // are from the quantization-aware training. + const bool immutable; + + bool IsEmpty() const { return params == nullptr; } +}; + +// The state for rescaling the propagated quantization parameters. This can be +// on the input side to satisfy the constraint of previous operation, or on the +// output side to satisfy the constraint of the next operation. +struct RequantizeState { + // Sometimes, we have to "requantize" the quantization result to satisfy all + // the constraints. The "requantize" can happen either on the input or output + // of the quantization result. + enum RequantizePosition { + NO_REQUANTIZE, + ON_INPUT, + ON_OUTPUT + } pos = NO_REQUANTIZE; + + // Quantization parameters will be used to add the requantize ops. + QuantizedType params; + + // Avoid clobbering all uses of the value, limit to just these ops. + SmallVector> users; +}; + +using RequantizeStates = SmallVector; + +// This is a worklist-driven driver for propagating quantization parameters +// across operations. +// +// The initial quantization parameters are extracted from the quantized type +// between adjacent `quantfork::QuantizeCastOp` and +// `quantfork::DequantizeCastOp`s. All these initial parameters are marked as +// immutable because they are from quantization-aware training. +// +// The algorithm traverses each op and sets the quantization parameters of its +// operands and results, according to its quantization specification, and then +// adds the operands and results to the worklist. If there are any conflicts +// (for example, there are quantization parameters propagated from the previous +// iteration), this process stops if the existing parameters are the immutable, +// or adding `requantize` op to resolve the conflicts. +// +// After the algorithm is converged, pairs of `quantfork::QuantizeCastOp` and +// `quantfork::DequantizeCastOp` are inserted to the right position to +// materialize the propagation and requantize results. +// +class QuantizationDriver { + public: + // Type alias of int used to access `states_`. + using QuantStateIndex = int; + + // (op, operand index) pair. + using OpWithOperandIndex = std::pair; + + // (op, result index) pair. + using OpWithResultIndex = std::pair; + + explicit QuantizationDriver(func::FuncOp func_op, const bool is_signed, + const int bit_width, + const bool disable_per_channel, + OpQuantSpecGetter op_quant_spec_getter, + OpQuantScaleSpecGetter op_quant_scale_spec_getter, + const bool infer_tensor_range, + const bool legacy_float_scale = false, + const bool is_qdq_conversion = false) + : fn_(func_op), + builder_(func_op.getBody()), + is_signed_(is_signed), + bit_width_(bit_width), + disable_per_channel_(disable_per_channel), + op_quant_spec_getter_(op_quant_spec_getter), + op_quant_scale_spec_getter_(op_quant_scale_spec_getter), + infer_tensor_range_(infer_tensor_range), + legacy_float_scale_(legacy_float_scale), + is_qdq_conversion_(is_qdq_conversion) {} + + // The entry point of the quantization parameters propagation. + void Run(); + + // Sets up the states for all the op results in the function. + void Initialize(); + + // Propagates the quantization parameters across all the ops. + bool PropagateParamsAndReturnIfChanged(); + + // Inserts the Quantize and Dequantize ops according to the propagation + // result. + void Finalize(); + + SmallVector GetArgs() { return args_; } + + llvm::DenseMap, int> GetResultStates() { + return result_states_; + } + + DenseMap result_states_; + + // Returns the state of the block argument. + QuantState& GetArgQuantState(BlockArgument arg) { + return states_[arg_states_[arg]]; + } + + // Returns the state of the index-th result of the op. + QuantState& GetResultQuantState(Operation* op, const int index) { + return states_[result_states_[{op, index}]]; + } + + private: + // Duplicates the constant op if it has multiple uses, and replaces + // target_op->operand[operand_index] with the newly created op. This also + // replaces corresponsing quantization states. + arith::ConstantOp DuplicateConstantOpIfNeeded(arith::ConstantOp op, + Operation* target_op, + int operand_index); + + // Adjusts bias scale that is derived from other scales (fc, conv ops) to + // prevent overflow of quantized bias values. This also changes quantization + // state of other inputs when needed. + bool SetBiasParamsWithAdjustments(Operation* op, int bias_index, + ArrayRef input_indices, + QuantizedType params); + + // Checks preconditions to adjust bias scale. + bool ShouldCheckBiasScale(Operation* op, int bias_index, + ArrayRef input_indices, + QuantizedType quantized_type, int& input_index, + int& filter_index); + + // Preprocesses the constants by doing the following: + // - Duplicates constants if it is used by multiple ops. For example, if a + // constant is used by multiple ops as a bias, duplicate constants and + // let each op assign its own quantization parameter for bias. + // - Adds all the non-bias constants (weights) to a set for looking up + // later. + // - Adds all per-channel weights to a set for looking up later. + void PreprocessConstantOps(); + + // Sets up all the data structures for quantization propagation. + void SetupAllStates(); + + // Returns Whether the constant is a weight, which shouldn't be shared by + // different ops. + bool IsWeight(Operation* cst) { return llvm::is_contained(weights_, cst); } + + // Returns all the related quantization constraints of the op. + std::unique_ptr GetQuantSpec(Operation* op); + std::unique_ptr GetQuantScaleSpec(Operation* op); + + // Returns whether quantization parameters have been propagated to the results + // of this op. + bool IsQuantized(Operation* op); + + // Adds all the users of index-th result of op to the work list. + void AddUserToList(Operation* op, const int index) { + for (Operation* user : op->getResult(index).getUsers()) { + work_list_.push_back(user); + } + } + + // Adds the defining op of index-th operand of op to the work list. + void AddOperandToList(Operation* op, const int index) { + if (Operation* operand_op = op->getOperand(index).getDefiningOp(); + operand_op != nullptr) { + work_list_.push_back(operand_op); + } + } + + // Returns the quantization params for the bias input from the non-bias + // operands which have their indexes in the `non_biases` vector. The returned + // parameters are calculated by `func`. + QuantizedType GetBiasParams(Operation* op, int bias_index, + ArrayRef non_bias_operand_indices, + AccumulatorScaleFunc func); + + // Sets the quantization parameters of the result to `quantized_type`. If + // any quantization parameters have been propagated, a requantize will + // happen on the input of propagated quantization. Returns `true` if internal + // state has been modified. + bool SetResultParams(Operation* op, int result_index, + QuantizedType quantized_type); + + // Sets the quantization parameters of the operand to `quantized_type`. If any + // quantization parameters have been propagated, a `requantize` will happen on + // the output of propagated quantization. When `override` is set, quantization + // state of the value is replaced instead of adding requantization. Returns + // `true` if internal state has been modified. + bool SetOperandParams(Operation* op, int operand_index, + QuantizedType quantized_type, bool override = false); + + // Sets the quantization parameters of the constant result according to its + // content. + bool SetConstantResultParams(Operation* op); + + // Inserts the Quantize and Dequantize ops after `op`'s `index`-th result. The + // quantized element type for the result is `quantized_type`. + void QuantizeOpResult(Operation* op, int result_index, + QuantizedType quantized_type); + + // Inserts the Quantize and Dequantize ops after `arg`. The quantized element + // type for `arg` is `quantized_type`. + void QuantizeArg(BlockArgument arg, QuantizedType quantized_type); + + // Inserts the Quantize and Dequantize ops (i.e. QDQ) after `value`. The + // quantized element type for `value` is `quantized_type`. + void QuantizeValue(Value value, QuantizedType quantized_type, Location loc); + + // Inserts the Quantize ops for requantizing the index-th result of the op. + void RequantizeOpResult(Operation* op, int result_index, + RequantizeStates& states); + + // Inserts the Quantize ops for requantizing a block argument. + void RequantizeArg(BlockArgument arg, RequantizeStates& states); + + // Inserts the Quantize and Dequantize ops to quantize the value and returns + // the Quantize op. + void RequantizeValue(Value value, RequantizeStates& states, Location loc); + + // Returns the quantization parameter satisfies the same scale + // constraints for the op. Returns an empty option if this quantization + // parameter doesn't exist. + QuantizedType GetQuantParamsForSameScaleConstraint(Operation* op); + + // Returns the state of the index-th operand of the op. + QuantState& GetOperandQuantState(Operation* op, const int index) { + return states_[operand_states_[{op, index}]]; + } + + // Returns the states of the index-th operand of the op. + RequantizeStates& GetOperandRequantizeStates(Operation* op, const int index) { + return rescale_states_[operand_states_[{op, index}]]; + } + + // Returns the states of the index-th result of the op. + RequantizeStates& GetResultRequantizeStates(Operation* op, const int index) { + return rescale_states_[result_states_[{op, index}]]; + } + + // Returns the states of the arg. + RequantizeStates& GetArgRequantizeStates(BlockArgument arg) { + return rescale_states_[arg_states_[arg]]; + } + + // Sets the state of an argument. If this value is cached, uses the cached + // result without creating new entry in the state vector. Otherwise, allocate + // a new entry in the state vector. + void InitializeArgState(BlockArgument arg, Value arg_value); + + // Sets the state of the index-th operand of the op. If this operand is + // cached, uses the cached result without creating new entry in the state + // vector. Otherwise, allocate a new entry in the state vector. + void InitializeOperandState(Operation* op, int index, Value value); + + // Sets the state of the index-th result of the op. If this result is cached, + // uses the cached result without creating new entry in the state vector. + // Otherwise, allocate a new entry in the state vector. + void InitializeResultState(Operation* op, int index, Value value); + + func::FuncOp fn_; + OpBuilder builder_; + const bool is_signed_; + const int bit_width_; + const bool disable_per_channel_; + + // We should distinguish weights and bias constants. Biases are specified by + // the quantization spec or are the operands of ops with same scale spec. The + // rest are weights. + DenseSet weights_; + + // The weights require narrow_range quantization. This map collects all the + // weight operands defined by the op quant spec. The value of each entry is + // the quantization dimension. If it is positive, per-channel quantization is + // required. + DenseMap optimized_weights_; + + // All the ops needs to propagate the quantization parameters to. + std::vector work_list_; + absl::flat_hash_set quantized_; + + // The vector contains all the quantization parameters propagated from the + // defining operations of the value, or from the quantization aware training. + std::vector states_; + + // The map contains all the quantization parameters which are required to + // satisfy the same operands and results constraint. The keys of this map are + // the values from `operand_states_` and `result_state_`. + absl::flat_hash_map rescale_states_; + + // Maps of indexes to the propagation state vector from the ops operands, + // results and arguments. + DenseMap operand_states_; + DenseMap arg_states_; + DenseMap value_to_state_; + + // This vector is to preserve the arguments order, so the newly inserted + // quantized ops for the arguments are deterministically ordered. + SmallVector args_; + + OpQuantSpecGetter op_quant_spec_getter_; + OpQuantScaleSpecGetter op_quant_scale_spec_getter_; + + // Infer output ranges for activation ops and constants. This is usually + // required for post-training quantization. + const bool infer_tensor_range_; + + // Calculate scales in float instead of double, so that the scales and + // quantized values are exactly the same with the TOCO quantizer. + const bool legacy_float_scale_; + + // If true, the model is a floating point graph with QDQ ops to be eliminated + // and fused into quantized kernels. + const bool is_qdq_conversion_; +}; + +// Propagates quantization parameters across ops in this function and satisfies +// the quantization specification of the ops. This methods assumes the initial +// quantization parameters are stored as adjacent quantize and dequantize ops +// and the propagation results are materialized by inserting pairs of quantize +// and dequantize ops to this function. Set `disable_per_channel` to true to not +// use per channel quantization even the op supports it. +// Setting `infer_tensor_range` to true, to infer quantization parameters from +// the activation ops and weight constants. This is only used for post-training +// quantization. +void ApplyQuantizationParamsPropagation(func::FuncOp func, bool is_signed, + int bit_width, bool disable_per_channel, + OpQuantSpecGetter op_quant_spec_getter, + bool infer_tensor_ranges, + bool legacy_float_scale, + bool is_qdq_conversion); + +void ApplyQuantizationParamsPropagation( + func::FuncOp func, bool is_signed, int bit_width, bool disable_per_channel, + OpQuantSpecGetter op_quant_spec_getter, + OpQuantScaleSpecGetter op_quant_scale_spec_getter, bool infer_tensor_ranges, + bool legacy_float_scale, bool is_qdq_conversion); + +} // namespace TFL +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_COMMON_QUANTIZATION_LIB_QUANTIZATION_DRIVER_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_driver_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_driver_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..becfc4819b187ea9c3a54fd78d457c2571e019d0 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_driver_test.cc @@ -0,0 +1,169 @@ +/* Copyright 2024 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/compiler/mlir/lite/quantization/common/quantization_lib/quantization_driver.h" + +#include +#include +#include +#include + +#include +#include +#include "absl/strings/string_view.h" +#include "llvm/ADT/DenseMap.h" +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Dialect/Quant/IR/QuantTypes.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/OwningOpRef.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_utils.h" +#include "tensorflow/compiler/mlir/lite/quantization/common/test_base.h" +#include "tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.h" +#include "tensorflow/compiler/mlir/quantization/common/attrs_and_constraints.h" // IWYU pragma: keep +#include "tensorflow/compiler/mlir/quantization/common/func.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" + +namespace mlir::TFL { +namespace { + +using ApplyQuantizationParamsPropagationTest = + mlir::quant::QuantizationTestBase; +using ::testing::IsEmpty; +using ::testing::Not; + +constexpr absl::string_view kModuleTFLite = R"mlir( + module { + func.func @main(%arg0: tensor<1x4x4x3xf32>) -> tensor<1x4x4x3xf32> attributes {_from_xla_call_module} { + %cst_0 = arith.constant dense<1.0> : tensor<3x1x1x3xf32> + %cst_1 = arith.constant dense<2.0> : tensor<3xf32> + %0 = "tf.XlaCallModule"(%arg0, %cst_0, %cst_1) <{Sout = [#tf_type.shape<1x4x4x3>], module = "", version = 9 : i64}> {_entry_function = @composite_fn_1, _stablehlo_version = "1.0.0", _original_entry_function = "composite_fn_1", _tfl_quant_trait = "fully_quantizable"} : (tensor<1x4x4x3xf32>, tensor<3x1x1x3xf32>, tensor<3xf32>) -> tensor<1x4x4x3xf32> + %1 = "tf.XlaCallModule"(%0, %cst_0, %cst_1) <{Sout = [#tf_type.shape<1x4x4x3>], module = "", version = 9 : i64}> {_entry_function = @composite_fn_2, _stablehlo_version = "1.0.0", _original_entry_function = "composite_fn_2", _tfl_quant_trait = "fully_quantizable"} : (tensor<1x4x4x3xf32>, tensor<3x1x1x3xf32>, tensor<3xf32>) -> tensor<1x4x4x3xf32> + return %1 : tensor<1x4x4x3xf32> + } + func.func private @composite_fn_1(%arg0: tensor<1x4x4x3xf32>, %arg1: tensor<3x1x1x3xf32>, %arg2: tensor<3xf32>) -> tensor<1x4x4x3xf32> attributes {tf_quant.composite_function} { + %0 = "tfl.conv_2d"(%arg0, %arg1, %arg2) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "RELU", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32} : (tensor<1x4x4x3xf32>, tensor<3x1x1x3xf32>, tensor<3xf32>) -> tensor<1x4x4x3xf32> + return %0 : tensor<1x4x4x3xf32> + } + func.func private @composite_fn_2(%arg0: tensor<1x4x4x3xf32>, %arg1: tensor<3x1x1x3xf32>, %arg2: tensor<3xf32>) -> tensor<1x4x4x3xf32> attributes {tf_quant.composite_function} { + %0 = "tfl.conv_2d"(%arg0, %arg1, %arg2) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "RELU", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32} : (tensor<1x4x4x3xf32>, tensor<3x1x1x3xf32>, tensor<3xf32>) -> tensor<1x4x4x3xf32> + return %0 : tensor<1x4x4x3xf32> + } + } +)mlir"; + +// TOOD: b/323478683 - Directly use types rather than creating a `unique_ptr`. +std::unique_ptr GetOpQuantSpec( + const mlir::Operation* op, + bool disable_per_channel_for_dense_layers = false) { + auto spec = std::make_unique(); + spec->coeff_op_quant_dim[1] = 3; + spec->biases_params[2] = {{0, 1}, GetUniformQuantizedTypeForBias}; + for (const auto& [key, value] : spec->coeff_op_quant_dim) { + spec->quantizable_operands.insert(key); + } + return spec; +} + +TEST_F(ApplyQuantizationParamsPropagationTest, + ConstsUsedMultipleTimesAreDuplicated) { + const OwningOpRef module_op_ref = + mlir::quant::QuantizationTestBase::ParseModuleOpString(kModuleTFLite); + func::FuncOp main_fn = mlir::quant::FindMainFuncOp(*module_op_ref); + + auto op_quant_spec_getter = [&](mlir::Operation* op) { + return GetOpQuantSpec(op, /*disable_per_channel_for_dense_layers=*/false); + }; + QuantizationDriver quantization_driver( + main_fn, /*is_signed=*/true, /*bit_width=*/8, + /*disable_per_channel=*/false, op_quant_spec_getter, + GetDefaultQuantScaleSpec, + /*infer_tensor_range=*/true, /*legacy_float_scale=*/false, + /*is_qdq_conversion=*/false); + + quantization_driver.Initialize(); + + int64_t num_constant_op = 0; + main_fn.walk([&](arith::ConstantOp cst) { ++num_constant_op; }); + EXPECT_EQ(num_constant_op, 4); +} + +TEST_F(ApplyQuantizationParamsPropagationTest, + PropagateParamsCreatesQuantState) { + const OwningOpRef module_op_ref = + ParseModuleOpString(kModuleTFLite); + func::FuncOp main_fn = mlir::quant::FindMainFuncOp(*module_op_ref); + + auto op_quant_spec_getter = [&](mlir::Operation* op) { + return GetOpQuantSpec(op, /*disable_per_channel_for_dense_layers=*/false); + }; + QuantizationDriver quantization_driver( + main_fn, /*is_signed=*/true, /*bit_width=*/8, + /*disable_per_channel=*/false, op_quant_spec_getter, + GetDefaultQuantScaleSpec, + /*infer_tensor_range=*/true, /*legacy_float_scale=*/false, + /*is_qdq_conversion=*/false); + + quantization_driver.Initialize(); + ASSERT_TRUE(quantization_driver.PropagateParamsAndReturnIfChanged()); + EXPECT_THAT(quantization_driver.GetArgs(), Not(IsEmpty())); + + for (const auto& arg : quantization_driver.GetArgs()) { + const QuantState& state = quantization_driver.GetArgQuantState(arg); + EXPECT_TRUE(isa(state.params)); + } + for (const auto& result : quantization_driver.GetResultStates()) { + mlir::Operation* op = result.first.first; + const int res_index = result.first.second; + const QuantState state = + quantization_driver.GetResultQuantState(op, res_index); + EXPECT_TRUE(isa(state.params)); + } +} + +TEST_F(ApplyQuantizationParamsPropagationTest, FinalizeInsertsQDQOps) { + const OwningOpRef module_op_ref = + ParseModuleOpString(kModuleTFLite); + func::FuncOp main_fn = mlir::quant::FindMainFuncOp(*module_op_ref); + + auto op_quant_spec_getter = [&](mlir::Operation* op) { + return GetOpQuantSpec(op, /*disable_per_channel_for_dense_layers=*/false); + }; + ApplyQuantizationParamsPropagation( + main_fn, /*is_signed=*/true, /*bit_width=*/8, + /*disable_per_channel=*/false, op_quant_spec_getter, + /*infer_tensor_ranges=*/true, /*legacy_float_scale=*/false, + /*is_qdq_conversion=*/false); + mlir::Operation* xla_call_module_op = + quant::FindOperationOfType(main_fn); + mlir::Operation* filter_dcast_op = + xla_call_module_op->getOperand(1).getDefiningOp(); + mlir::Operation* filter_qcast_op = + filter_dcast_op->getOperand(0).getDefiningOp(); + ASSERT_NE(filter_qcast_op, nullptr); + EXPECT_TRUE(isa(filter_qcast_op)); + EXPECT_TRUE(isa(filter_dcast_op)); + EXPECT_TRUE(isa( + mlir::cast(filter_qcast_op->getResult(0).getType()) + .getElementType())); +} + +} // namespace +} // namespace mlir::TFL diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_traits.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_traits.h new file mode 100644 index 0000000000000000000000000000000000000000..b6061de69c3efa7d8d6de8820f9c48ba753d2ae1 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_traits.h @@ -0,0 +1,152 @@ +/* Copyright 2019 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. +==============================================================================*/ + +// This file defines the op traits used in the MLIR TensorFlow Lite dialect. + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_COMMON_QUANTIZATION_LIB_QUANTIZATION_TRAITS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_COMMON_QUANTIZATION_LIB_QUANTIZATION_TRAITS_H_ + +#include +#include +#include + +#include "mlir/Dialect/Quant/IR/QuantTypes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/OpDefinition.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project + +using QuantizedType = mlir::quant::QuantizedType; +using UniformQuantizedType = mlir::quant::UniformQuantizedType; + +namespace mlir { +namespace TFL { +// Verifies that the op satisfies the same operands and results scales +// constraints. Note that this constraint can only be applied on some +// storage types of the op. +LogicalResult VerifySameScales(Operation* op); +} // namespace TFL + +// This includes the interface class definition. It couldn't be in a namespace +// because the table gen doesn't emit the namespace when it is used. +#include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_interface.h.inc" + +namespace OpTrait { +namespace TFL { + +// The base class that all the quantization related OpTrait implements. +template class TraitType> +struct QuantizationSpecTraitBase : public TraitBase { + static bool IsBias(int index) { return false; } + static bool IsQuantizable() { return true; } +}; + +// This class provides the API for ops that has a fixed output value range. +// This is used as a trait like this: +// +// class SoftmaxOp +// : public Op::Impl> { +// +// TODO(fengliuai): create a better way to express floating point scale in the +// template argument list. +template +class FixedResultUniformScale { + public: + template + class Impl + : public QuantizationSpecTraitBase< + ConcreteType, FixedResultUniformScale< + BitWidth, ZeroPoint, ScaleMantissa, ScaleExp, + StorageTypeMin, StorageTypeMax, Sign>::Impl> { + public: + QuantizedType GetResultQuantizedType(int index) { + auto op = this->getOperation(); + const auto result_type = + op->getResult(index).getType().template cast(); + if (!result_type.getElementType().template isa()) return {}; + Builder builder(op->getContext()); + const IntegerType storage_type = builder.getIntegerType(BitWidth); + const double scale = static_cast(ScaleMantissa) * + std::pow(10.0, static_cast(ScaleExp)); + return UniformQuantizedType::getChecked( + Sign, storage_type, result_type.getElementType(), scale, ZeroPoint, + StorageTypeMin, StorageTypeMax, builder.getUnknownLoc()); + } + }; +}; + +// This class provides the API for ops that has input as bias. This is used +// as a trait like this: +// +// class Conv2DOp +// : public Op::Impl> +// +// TODO(fengliuai): supports a configurable accumulator bit width. +template +class AccumulatorUniformScale { + public: + template + class Impl + : public QuantizationSpecTraitBase< + ConcreteType, AccumulatorUniformScale::Impl> { + public: + // Whether the index-th operand is a bias. + static bool IsBias(int index) { return index == Bias; } + + // Returns the indexes of all the non-bias operands. + static std::vector GetAllNonBiasOperands() { + return std::vector({Operands...}); + } + }; +}; + +// The trait to specify the operand index of the coefficient for an affine op +// and also the quantization dimension if per-axis quantization is support. +// If the quantization dimension is -1, per-axis quantization isn't supported. +// +// class Conv2DOp +// : public Op::Impl> +// +template +class AffineOpCoefficient { + public: + template + class Impl + : public TraitBase::Impl> { + public: + static int GetCoefficientOperandIndex() { return OperandIndex; } + static int GetQuantizationDim() { return QuantDim; } + }; +}; + +// This class provides the API for ops that can be quantized. +// This is as a trait like this: +// +// class LessOp : public Op { +// +template +class QuantizableResult + : public QuantizationSpecTraitBase {}; + +} // namespace TFL +} // namespace OpTrait +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_COMMON_QUANTIZATION_LIB_QUANTIZATION_TRAITS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_utils.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_utils.cc new file mode 100644 index 0000000000000000000000000000000000000000..134a0e1f96fe8080591ef2839456deaa9295b530 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_utils.cc @@ -0,0 +1,1075 @@ +/* Copyright 2019 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/compiler/mlir/lite/quantization/common/quantization_lib/quantization_utils.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/raw_ostream.h" +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Dialect/Quant/IR/QuantTypes.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/Diagnostics.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/OpDefinition.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_traits.h" +#include "tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.h" +#include "tensorflow/compiler/mlir/lite/quantization/ir/QuantizeUtils.h" +#include "tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/portable_tensor_utils.h" +#include "tensorflow/compiler/mlir/quantization/common/ir/FakeQuantSupport.h" +#include "tensorflow/compiler/mlir/quantization/common/ir/UniformSupport.h" +#include "tensorflow/compiler/mlir/tools/optimize/quantization_utils.h" + +namespace mlir { + +// This includes the interface class definition. It couldn't be in a namespace +// because the table gen doesn't emit the namespace when it is used. +#include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_interface.cc.inc" + +namespace TFL { +namespace { + +constexpr double kSmallestHalfRange = kNearZeroTolerance / 2; +using QType = quant::QuantizedType; + +// Repeats the content of `data` multiple times to resize to `target_size`. +// Note that this only broadcast across one dimension. +template +bool BroadcastVector(int target_size, SmallVectorImpl& data) { + const int size = data.size(); + if (size != target_size) { + if (target_size % size != 0) return true; + data.reserve(target_size); + for (int i = 1; i < target_size / size; ++i) { + data.insert(data.end(), data.begin(), data.begin() + size); + } + } + return false; +} + +// Expands the range to be larger than or equal to 1.0e-6, if it is +// very small (< 1.0e-6). This is to prevent very large quantized value by this +// range. +void ExpandVerySmallRange(const ArrayRef mins, + const ArrayRef maxs, + SmallVectorImpl& effective_mins, + SmallVectorImpl& effective_maxs) { + for (const auto [min, max] : llvm::zip(mins, maxs)) { + // The range is small. Expands the range to stride 0.0 and also at least + // 1.0e-6. + if (max - min > kNearZeroTolerance) { + effective_mins.push_back(min); + effective_maxs.push_back(max); + } else { + effective_mins.push_back(std::min(min, -kSmallestHalfRange)); + effective_maxs.push_back(std::max(max, kSmallestHalfRange)); + } + } +} + +// Sets the min / max, scale and zero_points from the fake quant num_bits +// attribute from QAT. +QuantizedType ResetMinMaxFromNumBits(const QuantizedType type, + const int num_bits, + const bool narrow_range, + const bool is_signed) { + if (num_bits >= 8) { + return type; + } + int64_t qmin = QType::getDefaultMinimumForInteger(is_signed, num_bits); + int64_t qmax = QType::getDefaultMaximumForInteger(is_signed, num_bits); + if (narrow_range) { + qmin += 1; + } + const int64_t storage_type_min = type.getStorageTypeMin(); + const int64_t storage_type_max = type.getStorageTypeMax(); + const double rate = + static_cast(storage_type_max - storage_type_min) / (qmax - qmin); + const auto& recalculate_scale = [&](double scale) -> double { + return scale * rate; + }; + const auto& recalculate_zero_point = [&](int64_t zero_point) -> int64_t { + return qmax - std::round((storage_type_max - zero_point) / rate); + }; + if (auto q_type = dyn_cast(type)) { + const double scale = recalculate_scale(q_type.getScale()); + const double zero_point = recalculate_zero_point(q_type.getZeroPoint()); + return UniformQuantizedType::get(q_type.getFlags(), q_type.getStorageType(), + q_type.getExpressedType(), scale, + zero_point, qmin, qmax); + } else if (auto q_type = dyn_cast(type)) { + const int size = q_type.getScales().size(); + SmallVector scales(size); + SmallVector zero_points(size); + for (int i = 0; i < size; ++i) { + scales[i] = recalculate_scale(q_type.getScales()[i]); + zero_points[i] = recalculate_zero_point(q_type.getZeroPoints()[i]); + } + return quant::UniformQuantizedPerAxisType::get( + q_type.getFlags(), q_type.getStorageType(), q_type.getExpressedType(), + scales, zero_points, q_type.getQuantizedDimension(), qmin, qmax); + } else { + llvm_unreachable("Unsupported QuantizedType in ResetMinMaxFromNumBits"); + } + return type; +} + +// Changes the axis of the input per-channel quantized type to match the +// dimension of the target type. Returns nullptr if it fails. +quant::UniformQuantizedPerAxisType ResetAxisAndBroadcast( + const ArrayRef shape, + const quant::UniformQuantizedPerAxisType qtype, const Type target, + const int quant_dim) { + const auto shaped = dyn_cast(target); + if (!shaped) return {}; + const ArrayRef new_shape = shaped.getShape(); + + SmallVector scales(qtype.getScales().begin(), + qtype.getScales().end()); + SmallVector zero_points(qtype.getZeroPoints().begin(), + qtype.getZeroPoints().end()); + + if (new_shape.size() == shape.size()) { // same rank + // Broadcast the scales and zero points to match the target size, which is + // usually the axis-th dimension of the target type. Currently, it covers + // two cases: + // - for Transpose, the data layout is changed so the `dim[axis]` still + // equals to the `scales_size`. The broadcast skips; + // - for Reshape, the data layout isn't changed but the innermost dimension + // is expand to cover the last two original dimensions. Thus we just need to + // be repeated the `scales` dim[2] times to covers the new dim length. + if (BroadcastVector(shaped.getDimSize(quant_dim), scales) || + BroadcastVector(shaped.getDimSize(quant_dim), zero_points)) { + return {}; + } + } else if ((new_shape.size() == shape.size() + 1) && new_shape.front() == 1) { + // Handle the [A, B, C] -> [1, A, B, C] reshape case. + if (!(std::equal(shape.begin(), shape.end(), new_shape.begin() + 1) && + quant_dim == new_shape.size() - 1)) { + return {}; + } + } else { + return {}; + } + + return quant::UniformQuantizedPerAxisType::get( + qtype.getFlags(), qtype.getStorageType(), qtype.getExpressedType(), + scales, zero_points, quant_dim, qtype.getStorageTypeMin(), + qtype.getStorageTypeMax()); +} + +} // namespace + +bool IsOpQuantizable(Operation* op) { + if (isa(op)) { + // Constant ops do not have QuantizableResult attribute but they can deal + // with quantized tensors. + return true; + } else if (op->hasTrait() || + isa(op)) { + // Terminators, qcast and decast are not quantizable. + return false; + } + + const bool attr_enforced_quantizable = + op->hasAttrOfType(kQuantTraitAttrName) && + op->getAttrOfType(kQuantTraitAttrName).getValue().str() == + QuantTraitValues[QuantizationTrait::FullyQuantizable]; + + const bool attr_output_quantized = QuantizableOpSupportsFloatOutputType(op); + + const bool trait_enforced_quantizable = + op->hasTrait(); + + return attr_enforced_quantizable || trait_enforced_quantizable || + attr_output_quantized; +} + +// Checks if an op has specific attributes that enable quantized inputs with +// float outputs. +bool QuantizableOpSupportsFloatOutputType(Operation* op) { + static constexpr char kOutputTypes[] = "_output_types"; + static constexpr char kSupportOutputTypeFloat[] = + "_support_output_type_float_in_quantized_op"; + + if (!(op->hasAttrOfType(kOutputQuantized) && + op->getAttrOfType(kOutputQuantized).getValue())) { + return false; + } + + if (!(op->hasAttrOfType(kSupportOutputTypeFloat) && + op->getAttrOfType(kSupportOutputTypeFloat) + .getValue())) { + return false; + } + + if (!op->hasAttrOfType(kOutputTypes)) { + return false; + } + + auto output_types_attr = op->getAttrOfType(kOutputTypes); + + if (output_types_attr.size() != op->getResultTypes().size()) { + return false; + } + + for (const auto [attr_element, result_type] : + llvm::zip_equal(output_types_attr, op->getResultTypes())) { + auto type_attr = mlir::dyn_cast_or_null(attr_element); + + if (!type_attr) { + return false; + } + + auto tensor_type = mlir::dyn_cast_or_null(result_type); + + if (!tensor_type) { + return false; + } + + if (type_attr.getValue() != tensor_type.getElementType()) { + return false; + } + } + + return true; +} + +// Returns the quantized type for the +// input_type/min/max/storag_type_width/narrow_range. +// This is entry point to the Quant dialect and used for both quantizing +// activations and weights. +Type GetQuantizedType(Builder builder, const Type input_type, + const ArrayRef min, const ArrayRef max, + const int quant_dim, const int storage_type_width, + const bool narrow_range, const bool is_signed, + const bool legacy_float_scale, + const bool use_fake_quant_num_bits) { + auto converter = + mlir::quant::ir::ExpressedToQuantizedConverter::forInputType(input_type); + + // Expand the range to prevent extremely small scales and large quantized + // integers which can cause overflow. This leads to scale + // 7.843137254901961e-9 with 8 bits. + SmallVector effective_mins, effective_maxs; + ExpandVerySmallRange(min, max, effective_mins, effective_maxs); + + quant::QuantizedType quantized_element_type; + if (min.size() == 1 && max.size() == 1 && quant_dim == -1) { + quantized_element_type = quantfork::fakeQuantAttrsToType( + builder.getUnknownLoc(), storage_type_width, effective_mins[0], + effective_maxs[0], narrow_range, converter.expressed_type, is_signed); + if (legacy_float_scale) { + quantized_element_type = + DownCastScale(quantized_element_type, effective_mins[0], + effective_maxs[0], builder.getUnknownLoc()); + } + } else if (min.size() == max.size()) { + auto shape = dyn_cast(input_type); + if (!shape || shape.getRank() <= quant_dim || + static_cast(min.size()) != shape.getDimSize(quant_dim)) { + return {}; + } + // The quantization dim is set to the last dimension. + quantized_element_type = quantfork::fakeQuantAttrsToType( + builder.getUnknownLoc(), storage_type_width, quant_dim, effective_mins, + effective_maxs, narrow_range, converter.expressed_type, is_signed); + if (legacy_float_scale) { + quantized_element_type = + DownCastScale(quantized_element_type, effective_mins, effective_maxs, + builder.getUnknownLoc()); + } + } + if (!quantized_element_type) return {}; + // Use fake quant configured bit-widths (only supported for + // 1 < num_bits < 8 bits) instead of using 8-bit defaults. + if (use_fake_quant_num_bits && storage_type_width > 1 && + storage_type_width < 8 && + quantized_element_type.getStorageTypeMax() > + QType::getDefaultMinimumForInteger(is_signed, storage_type_width)) { + const auto resetEleType = ResetMinMaxFromNumBits( + quantized_element_type, storage_type_width, narrow_range, is_signed); + return converter.convert(resetEleType); + } + return converter.convert(quantized_element_type); +} + +// TODO(fengliuai): promote this utility method to mlir QuantOps. +TypeAttr RescaleQuantizedType(const Type input, const Attribute factor) { + const auto factor_values = dyn_cast_or_null(factor); + if (!factor_values) return {}; + const auto element_type = + quant::QuantizedType::getQuantizedElementType(input); + if (!element_type) return {}; + if (auto qtype = dyn_cast(element_type)) { + const ArrayRef scales = qtype.getScales(); + // Broadcasting hasn't been implemented yet. + if (static_cast(scales.size()) != factor_values.getNumElements()) + return {}; + SmallVector new_scales; + new_scales.reserve(scales.size()); + auto scales_iter = scales.begin(); + for (const auto& f : factor_values) { + new_scales.push_back(*scales_iter * + std::fabs(FloatAttr::getValueAsDouble(f))); + ++scales_iter; + } + // We are assuming symmetric quantization. + auto new_ele_type = quant::UniformQuantizedPerAxisType::get( + qtype.getFlags(), qtype.getStorageType(), qtype.getExpressedType(), + new_scales, qtype.getZeroPoints(), qtype.getQuantizedDimension(), + qtype.getStorageTypeMin(), qtype.getStorageTypeMax()); + if (const auto new_type = new_ele_type.castFromExpressedType( + quant::QuantizedType::castToExpressedType(input))) { + return TypeAttr::get(new_type); + } + } + // Currently, we only support per-axis quantized type. + return {}; +} + +TypeAttr GetQuantizedTypeAttr(const Builder builder, const Type input_type, + const Attribute min, const Attribute max, + const int quant_dim, const IntegerAttr num_bits, + const BoolAttr narrow_range, const bool is_signed, + const bool legacy_float_scale, + const bool use_fake_quant_num_bits) { + SmallVector min_value, max_value; + const auto mins = dyn_cast(min); + const auto maxs = dyn_cast(max); + if (mins && maxs) { + min_value.reserve(mins.getNumElements()); + max_value.reserve(maxs.getNumElements()); + for (auto it = mins.begin(); it != mins.end(); ++it) { + min_value.push_back(FloatAttr::getValueAsDouble(*it)); + } + for (auto it = maxs.begin(); it != maxs.end(); ++it) { + max_value.push_back(FloatAttr::getValueAsDouble(*it)); + } + } else { + const auto fmin = dyn_cast(min); + const auto fmax = dyn_cast(max); + if (fmin && fmax) { + min_value.push_back(fmin.getValueAsDouble()); + max_value.push_back(fmax.getValueAsDouble()); + } else { + return {}; + } + } + const Type final_type = + GetQuantizedType(builder, input_type, min_value, max_value, quant_dim, + num_bits.getInt(), narrow_range.getValue(), is_signed, + legacy_float_scale, use_fake_quant_num_bits); + if (!final_type) return {}; + return TypeAttr::get(final_type); +} + +TypeAttr CastQuantizedTypeAttrFromExpressedType(const Builder builder, + const TypeAttr source, + const Type target, + const int axis) { + const auto source_type = dyn_cast_or_null(source.getValue()); + if (!source_type) return {}; + const auto src_ele_type = source_type.getElementType(); + auto qtype = dyn_cast(src_ele_type); + + // Reset the quantization dimensions if it is per-axis. + if (const auto per_axis = + dyn_cast_or_null(qtype)) { + // For the pass-through ops, we don't know which the dimension will be the + // new quantization dimension. Only if the new quantization dimension can + // be inferred, it is safe to reset the per-axis quantized type. + if (axis == -1) return {}; + qtype = + ResetAxisAndBroadcast(source_type.getShape(), per_axis, target, axis); + } + if (!qtype) return {}; + const Type final_type = qtype.castFromExpressedType(target); + if (!final_type) return {}; + return TypeAttr::get(final_type); +} + +void ExtractMinMaxFromAttr(const DenseFPElementsAttr values, const int dim_size, + const int slice_size, bool symmetric, + SmallVectorImpl& mins, + SmallVectorImpl& maxs) { + // If all the element values are same we don't need to scan the content. + if (values.isSplat()) { + const double single_value = + FloatAttr::getValueAsDouble(values.getSplatValue()); + + // When the single value isn't 0.0, we expand it to a range to include + // this single value and 0.0. This will give us a scale and zero point + // works for both this value and 0.0. + if (single_value < 0.0) { + mins[0] = single_value; + maxs[0] = symmetric ? -single_value : 0.0; + } else if (single_value > 0.0) { + mins[0] = symmetric ? -single_value : 0.0; + maxs[0] = single_value; + } else { + mins[0] = maxs[0] = single_value; + } + for (int i = 1; i < dim_size; ++i) { + mins[i] = mins[0]; + maxs[i] = maxs[0]; + } + } else { + int64_t flatten_index = 0; + auto begin = values.begin(); + auto end = values.end(); + for (auto it = begin; it != end; ++it, ++flatten_index) { + const double ele_value = FloatAttr::getValueAsDouble(*it); + const int slice_index = flatten_index / slice_size; + const int channel_index = slice_index % dim_size; + mins[channel_index] = std::min(mins[channel_index], ele_value); + maxs[channel_index] = std::max(maxs[channel_index], ele_value); + } + // Expand range to include 0. + for (int i = 0; i < dim_size; ++i) { + maxs[i] = std::max(maxs[i], 0.0); + mins[i] = std::min(mins[i], 0.0); + } + if (symmetric) { + for (int i = 0; i < dim_size; ++i) { + maxs[i] = std::max(std::abs(mins[i]), std::abs(maxs[i])); + mins[i] = -maxs[i]; + } + } + } +} + +Type GetUniformQuantizedTypeForWeight( + const ElementsAttr attr, const bool symmetric, const unsigned num_bits, + const bool is_signed, const bool narrow_range, + const bool legacy_float_scale, const bool use_fake_quant_num_bits) { + const Builder builder(attr.getContext()); + // `symmetric` can only be used when it is `signed` and `narrow_range`. + if (symmetric && (!is_signed || !narrow_range)) return {}; + + SmallVector mins(1, std::numeric_limits::max()); + SmallVector maxs(1, std::numeric_limits::min()); + const auto fp = dyn_cast(attr); + if (!fp) return {}; + + // Computes the effective min/max values of the attribute values. + ExtractMinMaxFromAttr(fp, /*dim_size=*/1, /*slice_size=*/1, symmetric, mins, + maxs); + + const auto type = + GetQuantizedType(builder, attr.getType(), mins[0], maxs[0], + /*quant_dim=*/-1, num_bits, narrow_range, is_signed, + legacy_float_scale, use_fake_quant_num_bits); + if (const auto ele_type = dyn_cast_or_null(type)) + return ele_type.getElementType(); + + return {}; +} + +Type GetUniformQuantizedPerAxisTypeForWeight( + const ElementsAttr attr, const int quant_dim, const bool symmetric, + const unsigned num_bits, const bool is_signed, const bool narrow_range, + const bool legacy_float_scale, const bool use_fake_quant_num_bits) { + const Builder builder(attr.getContext()); + const auto shape = cast(attr.getType()).getShape(); + if (static_cast(shape.size()) <= quant_dim) return {}; + // `symmetric` can only be used when it is `signed` and `narrow_range`. + if (symmetric && (!is_signed || !narrow_range)) return {}; + + const int dim_size = shape[quant_dim]; + const int slice_size = + std::accumulate(std::next(shape.begin(), quant_dim + 1), shape.end(), 1, + std::multiplies()); + SmallVector mins(dim_size, std::numeric_limits::max()); + SmallVector maxs(dim_size, std::numeric_limits::min()); + const auto fp = dyn_cast(attr); + if (!fp) return {}; + + // Computes the effective min/max values of the attribute values. + ExtractMinMaxFromAttr(fp, dim_size, slice_size, symmetric, mins, maxs); + + const auto type = GetQuantizedType( + builder, attr.getType(), mins, maxs, quant_dim, num_bits, narrow_range, + is_signed, legacy_float_scale, use_fake_quant_num_bits); + if (auto ele_type = dyn_cast_or_null(type)) + return ele_type.getElementType(); + + return {}; +} + +quant::QuantizedType GetUniformQuantizedTypeForBias( + const std::vector& op_types, + const int adjusted_quant_dim, const bool legacy_float_scale) { + if (op_types.empty()) return {}; + + size_t axis_size = 1; + int32_t quant_dim = -1; + Type expressed_type; + // Requires all the op types are valid UniformQuantizedTypes or + // UniformQuantizedPerAxisTypes and also have same expressed type. For all + // the UniformQuantizedPerAxisTypes, the quantization dimension index and + // dimension sizes are same. + for (const auto op_type : op_types) { + if (!op_type) return {}; + if (expressed_type && expressed_type != op_type.getExpressedType()) { + return {}; + } + expressed_type = op_type.getExpressedType(); + + if (const auto type = + dyn_cast(op_type)) { + if (axis_size != 1 && axis_size != type.getScales().size()) return {}; + if (quant_dim != -1 && quant_dim != type.getQuantizedDimension()) + return {}; + axis_size = type.getScales().size(); + quant_dim = type.getQuantizedDimension(); + } else if (!isa(op_type)) { + return {}; + } + } + + // The scale from the UniformQuantizedTypes is broadcasted if there are + // UniformQuantizedPerAxisTypes. + SmallVector scales(axis_size, 1.0); + for (const auto op_type : op_types) { + if (const auto type = + dyn_cast(op_type)) { + for (const auto& index_scale : llvm::enumerate(type.getScales())) { + scales[index_scale.index()] *= index_scale.value(); + } + } else if (const auto type = + dyn_cast(op_type)) { + for (int index = 0; index < axis_size; ++index) { + scales[index] *= type.getScale(); + } + } + } + if (legacy_float_scale) { + for (int i = 0; i < scales.size(); ++i) { + scales[i] = static_cast(scales[i]); + } + } + + // Builds the result quantized type, which has signed 32 bits storage type. + Builder builder(expressed_type.getContext()); + const IntegerType storage_type = builder.getIntegerType(32); + const int64_t storage_type_min = + quant::QuantizedType::getDefaultMinimumForInteger(/*isSigned=*/true, 32); + const int64_t storage_type_max = + quant::QuantizedType::getDefaultMaximumForInteger(/*isSigned=*/true, 32); + if (axis_size == 1) { + return quant::UniformQuantizedType::getChecked( + builder.getUnknownLoc(), + /*flags=*/true, storage_type, expressed_type, scales[0], + /*zeroPoint=*/0, storage_type_min, storage_type_max); + } else { + SmallVector zero_points(axis_size, 0); + // If the bias is a 1-D tensor, set the `quantizedDimension` to 0. + // If the bias rank is larger than 1 because it was already broadcasted + // to match the output shape, use the last index. + return quant::UniformQuantizedPerAxisType::getChecked( + builder.getUnknownLoc(), + /*flags=*/true, storage_type, expressed_type, scales, zero_points, + /*quantizedDimension=*/std::max(adjusted_quant_dim, 0), + storage_type_min, storage_type_max); + } +} + +ElementsAttr QuantizeLegacy(const Attribute real_value, + const Type tensor_type) { + if (!isa(real_value) || + !quant::QuantizedType::getQuantizedElementType(tensor_type)) { + return {}; + } + const auto real_values_attr = cast(real_value); + auto q_type = quant::QuantizedType::getQuantizedElementType(tensor_type); + std::vector real_values; + SmallVector quantized_attr; + real_values.reserve(real_values_attr.getNumElements()); + quantized_attr.reserve(real_values_attr.getNumElements()); + std::transform(real_values_attr.begin(), real_values_attr.end(), + std::back_inserter(real_values), [&](APFloat value) -> float { + return value.convertToFloat(); + }); + const ShapedType new_dense_type = dyn_cast_or_null( + q_type.castExpressedToStorageType(real_values_attr.getType())); + const int width = dyn_cast(q_type.getStorageType()).getWidth(); + + if (width == 8 && q_type.getStorageTypeMax() == 127 && + q_type.getStorageTypeMin() == -127) { + std::vector quantized_values(real_values_attr.getNumElements()); + if (auto uniform_type = dyn_cast(q_type)) { + float min, max, scale; + mlir::lite::toco_legacy::PortableSymmetricQuantizeFloats( + real_values.data(), real_values.size(), quantized_values.data(), &min, + &max, &scale); + // The scale has been adjusted, so the adjusted scale should be respected. + if (std::abs(scale - uniform_type.getScale()) > 1e-3) { + return Quantize(real_value, tensor_type); + } + } else if (auto uniform_type = + dyn_cast(q_type)) { + std::vector scales_inv; + std::vector dimension; + dimension.insert(dimension.end(), new_dense_type.getShape().begin(), + new_dense_type.getShape().end()); + std::transform(uniform_type.getScales().begin(), + uniform_type.getScales().end(), + std::back_inserter(scales_inv), + [](float scale) { return 1.0 / scale; }); + + tflite_migration::optimize::utils::SymmetricPerChannelQuantizeValues( + real_values.data(), scales_inv, dimension, + uniform_type.getQuantizedDimension(), &quantized_values); + } else { + return {}; + } + std::transform(quantized_values.begin(), quantized_values.end(), + std::back_inserter(quantized_attr), + [&](int8_t value) -> APInt { + return APInt(8, value, /*isSigned=*/true); + }); + return DenseElementsAttr::get(new_dense_type, quantized_attr); + } else if (width == 8) { + // This can be a state tensor, or an actual constant tensor with + // asymmetric range. For a state tensor, assigning correct quantization + // parameters is sufficient, and for constants with asymmetric range it's + // not correctly quantized by legacy quantizer so call the new Quantize. + return Quantize(real_value, tensor_type); + } else if (width == 16) { + if (const auto uniform_type = dyn_cast(q_type)) { + const auto quantized_values = + tflite_migration::optimize::utils::SymmetricQuantizeFloatsToInt16( + real_values.data(), real_values.size(), uniform_type.getScale()); + std::transform(quantized_values.begin(), quantized_values.end(), + std::back_inserter(quantized_attr), + [&](int16_t value) -> APInt { + return APInt(16, value, /*isSigned=*/true); + }); + return DenseElementsAttr::get(new_dense_type, quantized_attr); + } + } else if (width == 32) { + std::vector scales; + if (const auto uniform_type = dyn_cast(q_type)) { + scales.push_back(uniform_type.getScale()); + } else if (const auto uniform_type = + dyn_cast(q_type)) { + scales.insert(scales.end(), uniform_type.getScales().begin(), + uniform_type.getScales().end()); + } else { + return {}; + } + const auto quantized_bias = + tflite_migration::optimize::utils::SymmetricBiasQuantize( + real_values.data(), real_values.size(), scales); + std::transform(quantized_bias.begin(), quantized_bias.end(), + std::back_inserter(quantized_attr), + [&](int32_t value) -> APInt { + return APInt(32, value, /*isSigned=*/true); + }); + return DenseElementsAttr::get(new_dense_type, quantized_attr); + } + return {}; +} + +ElementsAttr Quantize(const Attribute real_value, const Type tensor_type) { + if (const auto q_type = + quant::QuantizedType::getQuantizedElementType(tensor_type)) { + Type converted_type; + return dyn_cast_or_null( + quantfork::quantizeAttr(real_value, q_type, converted_type)); + } + return {}; +} + +quant::QuantizedType DownCastScale(QuantizedType type, double min, double max, + Location loc) { + const SmallVector mins = {min}; + const SmallVector maxs = {max}; + return DownCastScale(type, mins, maxs, loc); +} + +quant::QuantizedType DownCastScale(QuantizedType type, + const SmallVectorImpl& mins, + const SmallVectorImpl& maxs, + Location loc) { + // The given type can be null. For example, there can be an invalid scale and + // so on. + if (!type) return type; + SmallVector scales(mins.size()); + SmallVector zero_points(mins.size()); + if (auto q_type = dyn_cast(type)) { + zero_points.push_back(q_type.getZeroPoint()); + } else if (auto q_type = dyn_cast(type)) { + zero_points = {q_type.getZeroPoints().begin(), + q_type.getZeroPoints().end()}; + } + for (int i = 0; i < mins.size(); ++i) { + scales[i] = (static_cast(maxs[i]) - static_cast(mins[i])) / + (type.getStorageTypeMax() - type.getStorageTypeMin()); + if (type.getStorageTypeMax() != -type.getStorageTypeMin()) { + // Only applies for asymmetric quantized range with original scale. + const float zero_point_from_min = + type.getStorageTypeMin() - mins[i] / scales[i]; + if (zero_point_from_min < type.getStorageTypeMin()) { + zero_points[i] = static_cast(type.getStorageTypeMin()); + } else if (zero_point_from_min > type.getStorageTypeMax()) { + zero_points[i] = static_cast(type.getStorageTypeMax()); + } else { + zero_points[i] = static_cast(std::round(zero_point_from_min)); + } + } + } + if (auto q_type = dyn_cast(type)) { + return UniformQuantizedType::get(q_type.getFlags(), q_type.getStorageType(), + q_type.getExpressedType(), scales[0], + zero_points[0], q_type.getStorageTypeMin(), + q_type.getStorageTypeMax()); + } else if (auto q_type = dyn_cast(type)) { + return quant::UniformQuantizedPerAxisType::get( + q_type.getFlags(), q_type.getStorageType(), q_type.getExpressedType(), + scales, zero_points, q_type.getQuantizedDimension(), + q_type.getStorageTypeMin(), q_type.getStorageTypeMax()); + } + return type; +} + +// A heuristic to determine whether the scales needs to be from operands or +// from results for the ops with the `SameOperandsAndResultsScale` property. +// The current implementation is based on the number of operands. +static bool PreferResultScale(Operation* op) { + int float_operands = 0; + for (auto operand : op->getOperands()) { + if (auto operand_type = dyn_cast(operand.getType())) { + if (isa(operand_type.getElementType())) { + if (++float_operands > 1) return true; + } + } + } + return false; +} + +std::unique_ptr GetDefaultQuantScaleSpec(Operation* op) { + auto spec = std::make_unique(); + if (isa(op)) { + spec->has_same_scale_requirement = true; + spec->required_same_scale_func = [op](const bool sign, + const int bit_width) { + return cast(op) + .RequiredSameOperandsAndResultsScale(sign, bit_width); + }; + spec->required_same_quantized_axes_func = [op]() { + return cast(op).RequiredSameQuantizedAxes(); + }; + } + if (isa(op)) { + spec->has_fixed_output_range = true; + spec->fixed_output_range_func = [op](bool sign, int bit_width) { + return cast(op).GetFixedOutputRange(sign, + bit_width); + }; + } + return spec; +} + +// The stats op of some of the ops can be redundant. The current implementation +// only considers the ops with restricted output params. +static bool IsStatsRedundant( + Operation* op, const OpQuantSpecGetter op_quant_spec_getter, + const OpQuantScaleSpecGetter op_quant_scale_spec_getter) { + // If it has FixedOutputRangeInterface, no need to manually create spec. + return isa(op) || + op_quant_scale_spec_getter(op)->has_fixed_output_range; +} + +static bool IsSameScaleOp( + Operation* op, const OpQuantScaleSpecGetter op_quant_scale_spec_getter) { + // If it has SameScalesOpInterface, no need to manually create spec. + return dyn_cast(op) || + op_quant_scale_spec_getter(op)->has_same_scale_requirement; +} + +bool RemoveRedundantStatsOps( + func::FuncOp func, const OpQuantSpecGetter op_quant_spec_getter, + const OpQuantScaleSpecGetter op_quant_scale_spec_getter) { + SmallVector all_stats_ops; + llvm::DenseSet redundant_stats_ops; + + // Step 0: remove the quantfork::StatisticsOp which are used by the + // quant.qcast op in case it overrides the information from training FakeQuant + // ops. + func.walk([&](quantfork::QuantizeCastOp q) { + auto input_op = q.getArg().getDefiningOp(); + if (auto stats = dyn_cast_or_null(input_op)) { + q.setOperand(stats.getArg()); + if (stats.use_empty()) stats.erase(); + } + }); + + // Step 1: forward pass: propagate any value scales which are not produces + // by `SameOperandsAndResultsScale`. Additionally, remove the value scales + // which are produced by the ops with the `FixedOutputRangeInterface`. + // Note that we don't propagate across the multiple-operands + // `SameOperandsAndResultsScale` ops like `concatenation`. + func.walk([&](quantfork::StatisticsOp stats_op) { + all_stats_ops.push_back(stats_op); + }); + + while (!all_stats_ops.empty()) { + quantfork::StatisticsOp stats_op = all_stats_ops.back(); + all_stats_ops.pop_back(); + + if (auto def = stats_op.getArg().getDefiningOp()) { + if (IsStatsRedundant(def, op_quant_spec_getter, + op_quant_scale_spec_getter)) { + redundant_stats_ops.insert(stats_op); + } + } + + for (Operation* user : stats_op.getResult().getUsers()) { + // We don't propagate this parameter down if it has multiple operands. + // We want to use the result parameter scales instead. + if (!IsSameScaleOp(user, op_quant_scale_spec_getter) || + PreferResultScale(user)) { + continue; + } + for (Value res : user->getResults()) { + if (!res.hasOneUse()) { + continue; + } + if (auto next_stats = + dyn_cast(*res.getUsers().begin())) { + // quantization parameters can be propagated to next_stats + redundant_stats_ops.insert(next_stats); + // add next_stats to the work list so propagation can continue. + all_stats_ops.push_back(next_stats); + } + } + } + } + + // Step 2: backward pass: For the ops skipped in the forward pass, propagate + // its results scale backwards as far as possible. + func.walk([&](quantfork::StatisticsOp stats_op) { + if (redundant_stats_ops.find(stats_op) == redundant_stats_ops.end()) { + all_stats_ops.push_back(stats_op); + } + }); + + while (!all_stats_ops.empty()) { + quantfork::StatisticsOp stats_op = all_stats_ops.back(); + all_stats_ops.pop_back(); + + if (Operation* def = stats_op.getArg().getDefiningOp()) { + if (!IsSameScaleOp(def, op_quant_scale_spec_getter)) { + continue; + } + for (Value input : def->getOperands()) { + if (auto next_stats = dyn_cast_or_null( + input.getDefiningOp())) { + redundant_stats_ops.insert(next_stats); + all_stats_ops.push_back(next_stats); + } + } + } + } + + // Step3: Remove all the redundant stats ops + for (Operation* it : redundant_stats_ops) { + if (!isa(it)) return true; + auto stats_op = cast(it); + stats_op.getResult().replaceAllUsesWith(stats_op.getArg()); + stats_op.erase(); + } + + // Returns false if the steps finish without errors. + return false; +} + +LogicalResult VerifySameScales(Operation* op) { + auto same_scale_op = cast(op); + + SmallVector collected_quant_params; + for (Value input : op->getOperands()) { + QuantizedType quant_params = + QuantizedType::getQuantizedElementType(input.getType()); + // Skip non-quantizable operands. + if (quant_params) { + collected_quant_params.push_back(quant_params); + } + } + + for (Value output : op->getResults()) { + const QuantizedType quant_params = + QuantizedType::getQuantizedElementType(output.getType()); + // Skip non-quantizable results. + if (quant_params) { + collected_quant_params.push_back(quant_params); + } + } + + if (collected_quant_params.size() <= 1) return success(); + const auto& expected_params = collected_quant_params[0]; + for (int i = 1; i < collected_quant_params.size(); ++i) { + const auto& compared_params = collected_quant_params[i]; + // For some ops (such as Transpose or Squeeze), the quantized axis might not + // be the same, this function only verifies the scale and zero point in + // that case. The quantized axis should be verified in their own verifier + // method. + if (!same_scale_op.RequiredSameQuantizedAxes()) { + const auto expected_per_axis_qtype = + dyn_cast(expected_params); + const auto compared_per_axis_qtype = + dyn_cast(compared_params); + if (expected_per_axis_qtype && compared_per_axis_qtype && + llvm::equal(expected_per_axis_qtype.getScales(), + compared_per_axis_qtype.getScales()) && + llvm::equal(expected_per_axis_qtype.getZeroPoints(), + compared_per_axis_qtype.getZeroPoints()) && + expected_params.getStorageType() == + compared_params.getStorageType() && + expected_params.getExpressedType() == + compared_params.getExpressedType()) { + continue; + } + } + // Same quantization parameters are always ok. + if (expected_params == compared_params) continue; + // If the quantization parameters are not the same, as long as it has the + // same storage type and the op interface doesn't require same scale + // constraint for this storage type, it is still ok. + if (expected_params.isSigned() == compared_params.isSigned() && + expected_params.getStorageTypeIntegralWidth() == + compared_params.getStorageTypeIntegralWidth() && + !same_scale_op.RequiredSameOperandsAndResultsScale( + expected_params.isSigned(), + expected_params.getStorageTypeIntegralWidth())) + continue; + + std::string err_msg = + "quantization parameters violate the same scale constraint: "; + llvm::raw_string_ostream os(err_msg); + expected_params.print(os); + os << " vs. "; + compared_params.print(os); + os.flush(); + return op->emitOpError(err_msg); + } + return success(); +} + +quant::UniformQuantizedType GetFixedOutputRange( + const bool is_signed, const int bit_width, const Type tensor_type, + const double scale, int64_t zero_point, int64_t storage_min, + int64_t storage_max) { + const auto result_type = cast(tensor_type); + if (!isa(result_type.getElementType())) return {}; + Builder builder(result_type.getContext()); + + // Only support 8-bits and 16-bits + if (bit_width != 8 && bit_width != 16) return {}; + const IntegerType storage_type = builder.getIntegerType(bit_width); + if (!is_signed && bit_width == 8) { + zero_point += 128; + storage_min += 128; + storage_max += 128; + } + return quant::UniformQuantizedType::getChecked( + builder.getUnknownLoc(), is_signed, storage_type, + result_type.getElementType(), scale, zero_point, storage_min, + storage_max); +} + +quant::UniformQuantizedType GetFixedOutputRange(const bool is_signed, + const int bit_width, + const Type tensor_type, + const double scale, + const int64_t zero_point) { + return GetFixedOutputRange(is_signed, bit_width, tensor_type, scale, + zero_point, + /*storage_min=*/-(1 << (bit_width - 1)), + /*storage_max=*/(1 << (bit_width - 1)) - 1); +} + +Type ConvertSignedQuantizedToUnsigned(const Type signed_tensor_type, + const Location loc) { + const auto qtype = QType::getQuantizedElementType(signed_tensor_type); + if (!qtype || !qtype.isSigned()) return {}; + + const int num_bits = qtype.getStorageTypeIntegralWidth(); + // This is a negative value, and will be applied on zero points and fixed + // point ranges. + const int64_t offset = + QType::getDefaultMinimumForInteger(/*isSigned=*/true, num_bits) - + QType::getDefaultMinimumForInteger(/*isSigned=*/false, num_bits); + + const auto flags = !quant::QuantizationFlags::Signed; + QType new_qtype; + if (auto uqtype = dyn_cast(qtype)) { + new_qtype = quant::UniformQuantizedType::getChecked( + loc, flags, qtype.getStorageType(), qtype.getExpressedType(), + uqtype.getScale(), uqtype.getZeroPoint() - offset, + uqtype.getStorageTypeMin() - offset, + uqtype.getStorageTypeMax() - offset); + } else if (auto aqtype = + dyn_cast(qtype)) { + const auto zero_points = aqtype.getZeroPoints(); + SmallVector new_zero_points(zero_points.begin(), + zero_points.end()); + for (int i = 0; i < new_zero_points.size(); ++i) { + new_zero_points[i] -= offset; + } + new_qtype = quant::UniformQuantizedPerAxisType::getChecked( + loc, flags, qtype.getStorageType(), qtype.getExpressedType(), + aqtype.getScales(), new_zero_points, aqtype.getQuantizedDimension(), + aqtype.getStorageTypeMin() - offset, + aqtype.getStorageTypeMax() - offset); + } + return new_qtype.castFromExpressedType( + QType::castToExpressedType(signed_tensor_type)); +} + +LogicalResult RemoveDebugAttrPattern::matchAndRewrite( + Operation* op, PatternRewriter& rewriter) const { + // removeAttr will return nullptr if the attribute did not exist. Thus we can + // return success(result) to indicate if this op has changed. + return success(/*isSuccess=*/ + op->removeAttr(kDebugModeOpQuantAttrName) || + op->removeAttr(kDebugModeOpFloatAttrName)); +} + +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_utils.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..3907240c965364f2b33b2193ff58476c848efe79 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_utils.h @@ -0,0 +1,973 @@ +/* Copyright 2019 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. +==============================================================================*/ + +// This header file defines common utils used by TFLite transformation +// passes to work with op attributes. + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_COMMON_QUANTIZATION_LIB_QUANTIZATION_UTILS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_COMMON_QUANTIZATION_LIB_QUANTIZATION_UTILS_H_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "absl/container/flat_hash_map.h" +#include "absl/container/flat_hash_set.h" +#include "absl/strings/string_view.h" +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/Support/Casting.h" +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Dialect/Quant/IR/QuantTypes.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/IRMapping.h" // from @llvm-project +#include "mlir/IR/Location.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/Matchers.h" // from @llvm-project +#include "mlir/IR/OpDefinition.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/OperationSupport.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/Types.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_config.h" +#include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_traits.h" +#include "tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.h" +#include "tensorflow/compiler/mlir/quantization/common/ir/FakeQuantSupport.h" +#include "tensorflow/core/framework/types.pb.h" + +namespace mlir { +namespace TFL { + +// A unit attribute can be attached to the quantize/dequantize ops which are +// added by the quantization passes. These ops can be removed erased without +// losing accuracy. +inline constexpr char kVolatileOpAttrName[] = "volatile"; + +// Following attributes are used to mark ops that are not quantizable during +// debug model generation process for whole-model verify mode. If these +// attributes are attached, the upstream float/quantized ops know which ops to +// connect to, and it also prevents these ops from being copied again. +inline constexpr char kDebugModeOpFloatAttrName[] = "debug_float"; +inline constexpr char kDebugModeOpQuantAttrName[] = "debug_quant"; + +// Used to annotate custom ops if they are quantizable. +inline constexpr char kQuantTraitAttrName[] = "_tfl_quant_trait"; +enum QuantizationTrait { FullyQuantizable = 0, NotQuantizable = 1 }; +inline constexpr absl::string_view QuantTraitValues[] = {"fully_quantizable", + "not_quantizable"}; +inline constexpr char kOutputQuantized[] = "_output_quantized"; + +inline constexpr double kNearZeroTolerance = 1.0e-6; + +using QuantParams = QuantizedType; +using QuantSpec = QuantizationSpecs; +using SignedInteger = std::pair; // bitwidth and sign +using QuantParamsForResults = llvm::SmallVector; +using AccumulatorScaleFunc = + std::function&, int, bool)>; +using BiasParamsMap = + absl::flat_hash_map, AccumulatorScaleFunc>>; +// UniformQuantizedType GetFixedOutputRange(bool sign, int bit_width) +using GetFixedOutputRangeFunc = std::function; +// bool RequiredSameOperandsAndResultsScale(bool sign, int $bit_width) +using RequiredSameOperandsAndResultsScaleFunc = std::function; +// bool RequiredSameQuantizedAxes() +using RequiredSameQuantizedAxesFunc = std::function; + +using CustomMap = CustomOpMap; +using Operation = ::mlir::Operation; + +// Quantization spec of an op, driving the quantization algorithm. +struct OpQuantSpec { + // Maps the operand index of a bias input to its quantization specifications, + // including the non-bias operand indexes and the method retrieving + // quantization parameters from list of parameters of the non-bias operands. + // This map is empty if the op doesn't have a bias operand. + BiasParamsMap biases_params; + + // Quantization parameters for value restricted outputs. This is the + // "hard-coded" parameters and should be used unconditionally for the + // quantized op. This vector is empty if the op doesn't have value restricted + // outputs. + llvm::DenseMap restricted_output_params; + + // Coefficient operand index and whether supporting per-channel quantization. + // For QAT, this information is carried by the FakeQuant*/Quantize/Dequantize + // ops, but post-training quantization, the quantization parameters need to be + // inferred from the tensor content and op property. A "-1" value indicates + // the operand doesn't support per-channel quantization. + llvm::DenseMap coeff_op_quant_dim; + + // Indices of quantizable operands. Biases are not included in this field, + // the indices of biases can be found in the `biases_params`. + absl::flat_hash_set quantizable_operands; +}; + +// A function signature for getting the particular OpQuantSpec for the provided +// op. +using OpQuantSpecGetter = + std::function(mlir::Operation*)>; + +// Quantization scale spec of an op. The information defined in the MLIR +// interfaces FixedOutputRangeInterface and SameOperandsAndResultsScale should +// be checked first if present. +// TODO: b/323478683: Consider deprecating this. +struct OpQuantScaleSpec { + // Whether this op has a fixed range requirement (e.g. sigmoid) + bool has_fixed_output_range = false; + // Whether this op should have same operand and result scales (e.g. concat) + bool has_same_scale_requirement = false; + // Whether this op should have same operand and result type (e.g. gather) + bool has_same_operand_and_result_type_requirement = false; + // Returns the fixed output range, when has_fixed_output_range is set. + GetFixedOutputRangeFunc fixed_output_range_func; + // Returns whether same operands and results scales are required. + RequiredSameOperandsAndResultsScaleFunc required_same_scale_func = + [](bool sign, int bit_width) { return true; }; + // Returns whether operands and results must have the same quantized axis. + RequiredSameQuantizedAxesFunc required_same_quantized_axes_func = []() { + return true; + }; +}; + +// A function signature for getting the particular OpQuantScaleSpec for the +// provided op. +using OpQuantScaleSpecGetter = + std::function(mlir::Operation*)>; + +// Used in TFL Numeric Verify +struct NumericVerifySpec { + // Whether to enable numeric verification + bool verify_numeric = false; + + // Tolerance level from the quantized value for verification. If the tolerance + // is very small(<0.1), only the stats of the diff is displayed. + float error_tolerance = 5.0f; + + // Whether to verify numerical correctness layer by layer or by whole model + bool whole_model_verify = false; + + // Whether to enable log for failures + bool log_if_failed_flag = false; +}; + +// Used in TFL Quantize Pass +struct QuantPassSpec { + // Variables to control TFL Numeric Verify + NumericVerifySpec numeric_verify_spec; + + // Variables related to quantization + QuantSpec quant_spec; +}; + +// Re-calculates scales again in float instead of simply downcasting existing +// scales. +quant::QuantizedType DownCastScale(quant::QuantizedType type, + const SmallVectorImpl& mins, + const SmallVectorImpl& maxs, + Location loc); + +quant::QuantizedType DownCastScale(quant::QuantizedType type, double min, + double max, Location loc); + +bool IsOpQuantizable(mlir::Operation* op); +bool QuantizableOpSupportsFloatOutputType(mlir::Operation* op); + +// Specialized version of location to string for flatbuffer exported locations. +inline std::string GetTensorNameFromLoc(Location loc) { + if (auto name_loc = llvm::dyn_cast(loc)) { + return name_loc.getName().str(); + } + return ""; +} + +template +struct ConvertStatsToQDQs : public OpRewritePattern { + ConvertStatsToQDQs(int num_bits, bool narrow_range, bool is_signed, + bool legacy_float_scale, MLIRContext* context) + : OpRewritePattern(context), + num_bits(num_bits), + narrow_range(narrow_range), + is_signed(is_signed), + legacy_float_scale(legacy_float_scale) {} + + LogicalResult matchAndRewrite(quantfork::StatisticsOp op, + PatternRewriter& rewriter) const override { + Type expressed = llvm::cast(op.getType()).getElementType(); + quant::QuantizedType quant_type; + SmallVector mins, maxs; + + if (op.getAxisStats().has_value()) { + // Per axis quantization (or per channel quantization) + int stats_num = op.getAxisStats()->getNumElements(); + if (stats_num == 0 || stats_num % 2 != 0) return failure(); + auto stats = llvm::dyn_cast(*op.getAxisStats()); + if (!stats) return failure(); + + for (auto it = stats.begin(), e = stats.end(); it != e; ++it) { + double rmin = FloatAttr::getValueAsDouble(*it++); + double rmax = FloatAttr::getValueAsDouble(*it); + // The default nudging implementation of mlir quant library might cause + // clamping during inference if the calibration range isn't wide enough. + // So here we adjust the range to include 0.0. + rmin = std::min(rmin, 0.0); + rmax = std::max(rmax, 0.0); + if (num_bits == 16) { + // TODO: b/266536261 - Since the kernel implementation assumes that + // 16x8 integer quantization is symmetric, this MLIR quantizer + // supports only symmetric quantization. + rmax = std::max(std::abs(rmin), std::abs(rmax)); + rmin = -rmax; + } + TensorRangeSanityCheck(op, rmin, rmax); + mins.push_back(rmin); + maxs.push_back(rmax); + } + quant_type = quantfork::fakeQuantAttrsToType( + op.getLoc(), num_bits, *op.getAxis(), mins, maxs, narrow_range, + expressed, is_signed); + if (legacy_float_scale) { + quant_type = + mlir::TFL::DownCastScale(quant_type, mins, maxs, op->getLoc()); + } + } else if (auto stats = + llvm::dyn_cast(op.getLayerStats())) { + // Per tensor quantization + auto statValues = stats.getValues(); + double rmin = FloatAttr::getValueAsDouble(statValues[0]); + double rmax = FloatAttr::getValueAsDouble(statValues[1]); + // The default nudging implementation of mlir quant library might cause + // clamping during inference if the calibration range isn't wide enough. + // So here we adjust the range to include 0.0. + rmin = std::min(rmin, 0.0); + rmax = std::max(rmax, 0.0); + if (num_bits == 16) { + // TODO: b/266536261 - Since the kernel implementation assumes that + // 16x8 integer quantization is symmetric, this MLIR quantizer supports + // only symmetric quantization. + rmax = std::max(std::abs(rmin), std::abs(rmax)); + rmin = -rmax; + } + TensorRangeSanityCheck(op, rmin, rmax); + quant_type = + quantfork::fakeQuantAttrsToType(op.getLoc(), num_bits, rmin, rmax, + narrow_range, expressed, is_signed); + if (legacy_float_scale) { + quant_type = + mlir::TFL::DownCastScale(quant_type, rmin, rmax, op->getLoc()); + } + } else { + return failure(); + } + + rewriter.setInsertionPointAfter(op.getOperation()); + Type result_type = quant_type.castFromExpressedType(op.getType()); + auto q = + QuantizeOpT::create(rewriter, op.getLoc(), result_type, op.getArg()); + q->setAttr(kVolatileOpAttrName, rewriter.getUnitAttr()); + + auto dq = DequantizeOpT::create(rewriter, op.getLoc(), op.getType(), q); + op.getResult().replaceAllUsesWith(dq); + q.getOperation()->replaceUsesOfWith(dq, op.getArg()); + op.erase(); + + return success(); + } + + private: + int num_bits; + bool narrow_range; + bool is_signed; + bool legacy_float_scale; + + // Emits an op warning message if the calibrated range is larger than 10.0 and + // the storage type is less than or equal to 8 bits. + void TensorRangeSanityCheck(quantfork::StatisticsOp op, double& min, + double& max) const { + double range = std::fabs(max - min); + if (num_bits <= 8 && range >= 10.0) { + op.emitWarning() + << "Tensor range is too wide to be quantized. Use tf.clip_by_value " + "or tf.relu6 to narrow the tensor range. Range: " + << range << ", bit width: " << num_bits; + } + if (std::abs(max - min) < kNearZeroTolerance) { + op.emitWarning() << "Tensor range (" << min << ", " << max + << ") is too narrow and it might cause overflow. " + "Expanding range symmetrically by " + << kNearZeroTolerance; + min -= kNearZeroTolerance; + max += kNearZeroTolerance; + } + } +}; + +template +bool UsedBy(mlir::Operation* op) { + for (mlir::Operation* user : op->getUsers()) { + if (llvm::isa_and_nonnull(user)) return true; + } + return false; +} + +template +void CreateVerifier(mlir::Operation* quantizing_op, + mlir::Operation* quantized_op, PatternRewriter& rewriter, + int result_idx, const QuantPassSpec& quant_params) { + rewriter.setInsertionPointAfter(quantized_op); + FloatAttr tolerance = rewriter.getF32FloatAttr( + quant_params.numeric_verify_spec.error_tolerance); + BoolAttr log = + rewriter.getBoolAttr(quant_params.numeric_verify_spec.log_if_failed_flag); + // Verify the quantized value by sending the result to the verifier. + VerifierT::create(rewriter, quantizing_op->getLoc(), + quantized_op->getResult(result_idx).getType(), + quantized_op->getResult(result_idx), + quantizing_op->getResult(result_idx), tolerance, log); +} + +template <> +inline bool UsedBy(mlir::Operation* op) { + return false; +} + +// This specialization is not going to be called, but needed for compilation. +template <> +inline void CreateVerifier(mlir::Operation* quantizing_op, + mlir::Operation* quantized_op, + PatternRewriter& rewriter, int result_idx, + const QuantPassSpec& quant_params) {} + +// A base rewrite pattern which matches any N-in-M-out operations with +// quantization parameters propagated to at least one of its operands. The +// quantization parameters are annotated by the QuantizeOp/DequantizeOp pairs. +// Each matched pattern are rewritten by its quantized alternatives. +// +// The concrete pattern, extends from this base pattern, can specify whether it +// allows dynamic range quantized operands and results for the operations in the +// current context. These "DynamicRangeQuantized" operands and results don't +// have quantization parameters propagated to, so will be in float in the +// quantized results. The concrete pattern should define the following two +// functions: +// +// bool AllowDynamicRangeQuantizedOperand(Operation *) const +// bool AllowDynamicRangeQuantizedResult(Operation *) const +// +// Full integer quantization disallows "DynamicRangeQuantized" operands or +// results. Dynamic range quantization allows "DynamicRangeQuantized" operands +// and results. +template +class QuantizationPattern : public RewritePattern { + public: + using BaseType = QuantizationPattern; + + explicit QuantizationPattern(MLIRContext* context, + const QuantPassSpec& quant_params) + // Set the score to a large number so it is always preferred. + : RewritePattern(RootOpT::getOperationName(), 300, context), + quant_params_(quant_params) {} + + LogicalResult matchAndRewrite(mlir::Operation* op, + PatternRewriter& rewriter) const override { + llvm::SmallVector quantizing_ops; + + // Collect all the ops to quantize, as the user / producer of the root op. + if constexpr (std::is_same_v) { + if (op->getNumResults() != 1) { + return failure(); + } + auto users = op->getResult(0).getUsers(); + quantizing_ops.append(users.begin(), users.end()); + } else if constexpr (std::is_same_v) { + if (op->getNumOperands() != 1) { + return failure(); + } + Value quantize_operand = op->getOperand(0); + if (QuantizedType::getQuantizedElementType(quantize_operand.getType())) { + // The input of this QuantizeOp has already been quantized, i.e. + // rescale. + return failure(); + } + DenseFPElementsAttr attr; + if (matchPattern(quantize_operand, m_Constant(&attr))) { + // Const-> QuantizeOp pattern will be handled separately. + return failure(); + } + if (mlir::Operation* quantizing_op = quantize_operand.getDefiningOp()) { + quantizing_ops.push_back(quantizing_op); + } + } + + tensorflow::DataType inference_type = + quant_params_.quant_spec.inference_type; + bool weight_only_quantization = + quant_params_.quant_spec.weight_only_quantization; + bool enable_verify = quant_params_.numeric_verify_spec.verify_numeric; + bool enable_whole_model_verify = + quant_params_.numeric_verify_spec.whole_model_verify; + absl::flat_hash_set ops_blocklist = + quant_params_.quant_spec.ops_blocklist; + absl::flat_hash_set nodes_blocklist = + quant_params_.quant_spec.nodes_blocklist; + CustomMap custom_map = quant_params_.quant_spec.custom_map; + + // Rewrite the floating-point ops to the quantized version, by fusing + // preceding dequantize ops and succeding quantize ops. + for (mlir::Operation* quantizing_op : quantizing_ops) { + // If it is requantize op, we shouldn't rewrite this op. + if (llvm::isa(quantizing_op)) { + return failure(); + } + + // If the op is terminator, not quantizable or any ops from the mlir quant + // ops dialect, we shouldn't rewrite. In case of whole-model verify debug + // mode, not-quantizable ops should be duplicated to keep parallel + // float/quant model execution. + if (quantizing_op->hasTrait()) { + return failure(); + } + + if (!IsOpQuantizable(quantizing_op) && + !static_cast(this)->IsQuantizableCustomOp( + quantizing_op, custom_map)) { + if (!(enable_verify && enable_whole_model_verify)) { + return failure(); + } + if (quantizing_op->hasAttr(kDebugModeOpQuantAttrName) || + quantizing_op->hasAttr(kDebugModeOpFloatAttrName)) { + return failure(); + } + + rewriter.setInsertionPoint(quantizing_op); + mlir::Operation* float_op = rewriter.clone(*quantizing_op); + quantizing_op->setAttr(kDebugModeOpQuantAttrName, + rewriter.getUnitAttr()); + float_op->setAttr(kDebugModeOpFloatAttrName, rewriter.getUnitAttr()); + RewireFloatModelBackbone(quantizing_op, float_op); + return success(); + } + + // Blocklist op is checked in advance for non-dynamic range quantization + // case. + if (!quant_params_.quant_spec.weight_quantization && + (ops_blocklist.find(quantizing_op->getName().getStringRef().str()) != + ops_blocklist.end())) { + return failure(); + } + + if (!nodes_blocklist.empty()) { + if (auto name_loc = llvm::dyn_cast(quantizing_op->getLoc())) { + std::string sloc = name_loc.getName().str(); + if (!sloc.empty() && + (nodes_blocklist.find(sloc) != nodes_blocklist.end())) { + return failure(); + } + } + } + + // An op with float inputs and outputs are expected when it's used by a + // NumericVerify op. Skip this op. + if (enable_verify && UsedBy(quantizing_op)) { + continue; + } + + bool is_operand_or_result_modified = false; + // Collect all the quantized inputs and "clone" the matched op by these + // inputs. + SmallVector inputs; + inputs.reserve(quantizing_op->getNumOperands()); + for (auto operand : quantizing_op->getOperands()) { + Type operand_type = operand.getType(); + if (isa(operand_type)) { + inputs.push_back(operand); + continue; + } + + auto ele_type = + llvm::cast(operand.getType()).getElementType(); + if (static_cast(this) + ->AllowDynamicRangeQuantizedOperand(quantizing_op, + custom_map)) { + auto dq_op = dyn_cast_or_null(operand.getDefiningOp()); + + if (dq_op && inference_type == tensorflow::DT_QINT8 && + !static_cast(this)->IsWeightOnlyOp( + quantizing_op, ops_blocklist, weight_only_quantization, + custom_map)) { + // Dynamic range quantization is applied by having QuantizeOp as an + // input. Only int8 weight is supported for now. + inputs.push_back(dq_op.getOperand()); + is_operand_or_result_modified = true; + } else { + // Otherwise, it's the case where the operand is activations or the + // quantizing_op is non-supported/weight-only. + inputs.push_back(operand); + } + } else { + if (auto dq_op = + dyn_cast_or_null(operand.getDefiningOp())) { + is_operand_or_result_modified = true; + inputs.push_back(dq_op.getOperand()); + } else if (!ele_type.isF32()) { + // If the operand is an integer tensor, then it doesn't require the + // DequantizeOp in the pattern. + inputs.push_back(operand); + } else { + return failure(); + } + } + } + + mlir::Operation* quantized_op; + if (QuantizableOpSupportsFloatOutputType(quantizing_op)) { + rewriter.setInsertionPointAfter(quantizing_op); + OperationState new_state( + quantizing_op->getLoc(), quantizing_op->getName().getStringRef(), + inputs, quantizing_op->getResultTypes(), quantizing_op->getAttrs()); + for (const auto& indexed_regions : + llvm::enumerate(quantizing_op->getRegions())) { + Region* target_region = new_state.addRegion(); + IRMapping mapping; + indexed_regions.value().cloneInto(target_region, mapping); + } + quantized_op = rewriter.create(new_state); + rewriter.replaceOp(quantizing_op, quantized_op); + } else { + // Collect all the quantized outputs and replace them by the results of + // the new quantized op. + llvm::SmallDenseMap outputs_replaced; + SmallVector output_types; + output_types.reserve(quantizing_op->getNumResults()); + for (const auto& enumerated_result : + llvm::enumerate(quantizing_op->getResults())) { + Value result = enumerated_result.value(); + Type result_type = result.getType(); + // Add this to the test coverage once we create test ops with none + // type results. + if (isa(result_type)) { + outputs_replaced.insert({result, enumerated_result.index()}); + output_types.push_back(result_type); + continue; + } + Type result_ele_type = + llvm::cast(result.getType()).getElementType(); + // If the user is the QuantizeOp, it must be the only user. + if (result.hasOneUse() && + llvm::isa(*result.user_begin())) { + auto user = llvm::cast(*result.user_begin()); + outputs_replaced.insert( + {user.getResult(), enumerated_result.index()}); + output_types.push_back(user.getType()); + is_operand_or_result_modified = true; + } else if (!result_ele_type.isF32()) { + // If the result is an integer tensor, then it doesn't require the + // D op in the pattern. + outputs_replaced.insert({result, enumerated_result.index()}); + output_types.push_back(result.getType()); + } else if (static_cast(this) + ->AllowDynamicRangeQuantizedResult(quantizing_op, + custom_map)) { + outputs_replaced.insert({result, enumerated_result.index()}); + output_types.push_back(result.getType()); + } else { + return failure(); + } + } + + // For float16 quantization if none of the operand or result is + // modified, replacing the op. See b/335025403. + if (inference_type == tensorflow::DT_HALF && + !is_operand_or_result_modified) { + return failure(); + } + + rewriter.setInsertionPointAfter(quantizing_op); + OperationState new_state( + quantizing_op->getLoc(), quantizing_op->getName().getStringRef(), + inputs, output_types, quantizing_op->getAttrs()); + for (int i = 0; i < quantizing_op->getNumRegions(); ++i) { + new_state.addRegion(); + } + quantized_op = rewriter.create(new_state); + if (quantizing_op->getNumRegions() != 0) { + for (const auto& indexed_regions : + llvm::enumerate(quantizing_op->getRegions())) { + Region& target_region = + quantized_op->getRegion(indexed_regions.index()); + IRMapping mapping; + indexed_regions.value().cloneInto(&target_region, mapping); + } + } + for (auto output : outputs_replaced) { + output.getFirst().replaceAllUsesWith( + quantized_op->getResult(output.getSecond())); + } + } + + // To verify the numericals, the original floating-point ops are + // preserved in the graph. The result of these floating-point ops are sent + // to a numeric verifier op as the reference. + if (enable_verify && !std::is_same_v) { + // For constant operands, the floating-point constant is duplicated in + // case it is quantized. + for (int i = 0, e = quantized_op->getNumOperands(); i < e; ++i) { + auto def = quantized_op->getOperand(i).getDefiningOp(); + if (auto q = llvm::dyn_cast_or_null(def)) { + DenseFPElementsAttr attr; + if (!matchPattern(q.getOperand(), m_Constant(&attr))) { + continue; + } + auto cst = arith::ConstantOp::create(rewriter, + quantized_op->getLoc(), attr); + quantizing_op->setOperand(i, cst.getResult()); + } + } + + for (int i = 0, e = quantized_op->getNumResults(); i < e; ++i) { + if (!isa( + cast(quantizing_op->getResult(i).getType()) + .getElementType())) { + continue; + } + CreateVerifier(quantizing_op, quantized_op, rewriter, i, + quant_params_); + + if (enable_whole_model_verify) { + RewireFloatModelBackbone(quantized_op, quantizing_op); + } + } + } + } + return success(); + } + + private: + // Reconnects float ops in the whole-model verify mode. Works for both + // Quantizable ops and Unquantizable ops + void RewireFloatModelBackbone(mlir::Operation* quantized_op, + mlir::Operation* float_op) const { + for (int i = 0, e = quantized_op->getNumResults(); i < e; ++i) { + if (!llvm::cast(float_op->getResult(i).getType()) + .getElementType() + .isF32()) { + continue; + } + // Find the Quantize/Dequantize users of the new op results, and replace + // the usage. Then all the floating-point ops are connected, forming a + // separate float "backbone" model that the quantized model can be + // compared against in parallel. + // N.B. the return op will use this floating-point result. + Value result; + if (!IsOpQuantizable(float_op)) { + // For not quantizable ops, search for dequantize attached to the + // quantized op of the output. + if (mlir::Operation* quantize_op = dyn_cast_or_null( + *quantized_op->getResult(i).getUsers().begin())) { + result = quantize_op->getResult(0); + } else { + quantized_op->emitError() + << "Output[" << i + << "] is expected to have only one user [QUANTIZE]"; + return; + } + } else { + result = quantized_op->getResult(i); + } + for (auto user : result.getUsers()) { + // Skip the Requantize op and set the user to the following dequantize + // op. This happens when the quantizer tries to match the scale conflict + // with QuantizeOp - QuantizeOp(requant) - DequantizeOp triples. The + // correct float op should be the user of the last DequantizeOp. + if (llvm::isa(user)) { + user = *user->getResult(0).getUsers().begin(); + } + if (auto dequantize = llvm::dyn_cast(user)) { + // Replace all uses, except not quantizable ops that are being used in + // the float backbone. + dequantize.getResult().replaceUsesWithIf( + float_op->getResult(i), [&](OpOperand& use) { + return !use.getOwner()->hasAttr(kDebugModeOpQuantAttrName); + }); + } + } + } + } + + QuantPassSpec quant_params_; +}; + +// A pattern that removes debug attributes that are annotated to ops during +// the debug model creation. +class RemoveDebugAttrPattern : public RewritePattern { + public: + explicit RemoveDebugAttrPattern(MLIRContext* context) + : RewritePattern(MatchAnyOpTypeTag(), /*benefit=*/1, context) {} + LogicalResult matchAndRewrite(mlir::Operation* op, + PatternRewriter& rewriter) const override; +}; + +// Converts quantized tensor type with signed integer type to quantized tensor +// type with unsigned integer type. +Type ConvertSignedQuantizedToUnsigned(Type signed_tensor_type, Location loc); + +// Converts quantize ops with unsigned quantized types to these with signed +// quantized types and preserves the scales. +template +struct ConvertUnsignedToSigned : public OpRewritePattern { + using BaseType = ConvertUnsignedToSigned; + using QType = quant::QuantizedType; + + explicit ConvertUnsignedToSigned(MLIRContext* context) + : OpRewritePattern(context, 1) {} + + LogicalResult matchAndRewrite(QuantizeOpT op, + PatternRewriter& rewriter) const override { + Type output_type = op.getResult().getType(); + auto qtype = QType::getQuantizedElementType(output_type); + if (!qtype || qtype.isSigned()) return failure(); + + int num_bits = qtype.getStorageTypeIntegralWidth(); + if (num_bits == 8) { + // If storage is 8-bit, trained num bits may be less than 8 so check here. + num_bits = + static_cast(std::ceil(std::log2(qtype.getStorageTypeMax()))); + } + // This is a positive value, and will be applied on zero points and fixed + // point ranges. + int64_t offset = + QType::getDefaultMinimumForInteger(/*isSigned=*/false, num_bits) - + QType::getDefaultMinimumForInteger(/*isSigned=*/true, num_bits); + + auto flags = quant::QuantizationFlags::Signed; + QType new_qtype; + if (auto uqtype = llvm::dyn_cast(qtype)) { + new_qtype = quant::UniformQuantizedType::getChecked( + op.getLoc(), flags, qtype.getStorageType(), qtype.getExpressedType(), + uqtype.getScale(), uqtype.getZeroPoint() - offset, + uqtype.getStorageTypeMin() - offset, + uqtype.getStorageTypeMax() - offset); + } else if (auto aqtype = + llvm::dyn_cast(qtype)) { + auto zero_points = aqtype.getZeroPoints(); + llvm::SmallVector new_zero_points(zero_points.begin(), + zero_points.end()); + for (int i = 0, e = new_zero_points.size(); i < e; ++i) { + new_zero_points[i] -= offset; + } + new_qtype = quant::UniformQuantizedPerAxisType::getChecked( + op.getLoc(), flags, qtype.getStorageType(), qtype.getExpressedType(), + aqtype.getScales(), new_zero_points, aqtype.getQuantizedDimension(), + aqtype.getStorageTypeMin() - offset, + aqtype.getStorageTypeMax() - offset); + } else { + return failure(); + } + + if (!new_qtype) return failure(); + Type new_output_type = new_qtype.castFromExpressedType( + QType::castToExpressedType(output_type)); + rewriter.replaceOpWithNewOp(op, new_output_type, op.getArg()); + return success(); + } +}; + +// Fold Extra Requantize ops if the preceding ops has free scale requirement. +template +struct FoldTrivalRequantizeOp : public OpRewritePattern { + explicit FoldTrivalRequantizeOp(MLIRContext* context) + : OpRewritePattern(context, 1) {} + + LogicalResult matchAndRewrite(RequantizeOpT op, + PatternRewriter& rewriter) const override { + Value pre_quantized = op->getOperand(0); + auto pre_quantized_type = + quant::QuantizedType::getQuantizedElementType(pre_quantized.getType()); + if (!pre_quantized_type) return failure(); + + mlir::Operation* def = pre_quantized.getDefiningOp(); + if (!def) return failure(); + if (llvm::isa(def) || + !def->hasTrait()) { + return failure(); + } + + // This op should not clobber def, if more than one requant of this value. + if (!pre_quantized.hasOneUse()) { + return failure(); + } + + op.emitWarning("Remove trivial `rescale` op. Please fix the source graph."); + + llvm::SmallVector new_output_types; + for (auto result : def->getResults()) { + if (result.hasOneUse() && *result.getUsers().begin() == op) { + new_output_types.push_back(op.getResult().getType()); + } else { + new_output_types.push_back(result.getType()); + } + } + + // Remove this rescale op. + rewriter.replaceOp(op, {pre_quantized}); + + // Replace the output scale of the preceding op. + rewriter.setInsertionPointAfter(def); + OperationState new_state(def->getLoc(), def->getName().getStringRef(), + def->getOperands(), new_output_types, + def->getAttrs()); + Operation* new_op = rewriter.create(new_state); + + rewriter.replaceOp(def, new_op->getResults()); + return success(); + } +}; + +// Given a quantized type `input`, magnifying its scales by the factor stored in +// `factor`. If `input` isn't a quantized type or the `factor` doesn't match the +// dimension size of `input` or isn't floating-point, nullptr will be returned. +TypeAttr RescaleQuantizedType(Type input, Attribute factor); + +// Converts the min/max/num_bits/narrow_range information to a +// QuantizedType, and then returns the attribute containing the QuantizedType. +// The `min` and `max` arguments can be FloatAttr or DenseFPElementsAttr and +// returns UniformQuantizedType or UniformQuantizedPerAxisType respectively. +// `narrow_range` is set to true for weights and `is_signed` is set to true +// if it is using signed int symmetric quantization. +// +// Note that this method may broadcast min and max to match the dimension length +// of `input_type`, if the `quant_dim` is valid. On the other hand, the +// symmetry of min and max is not adjusted by this method. The QAT workflow +// should set min/max correctly (and use `narrow_range`=true, `is_signed`=true) +// if symmetric quantization is required. +TypeAttr GetQuantizedTypeAttr(Builder builder, Type input_type, Attribute min, + Attribute max, int quant_dim, + IntegerAttr num_bits, BoolAttr narrow_range, + bool is_signed, bool legacy_float_scale = false, + bool use_fake_quant_num_bits = false); + +// Casts the `target` type to a quantized type by using the quantization +// parameters from the type in the `source` type attribute. +// Examples: +// f32 -> !quant.uniform +// tensor<4xf32> -> tensor<4x!quant.uniform> +// The result is wrapped by a type attribute. Returns nullptr if the cast +// isn't valid. +// +// `axis` is to specify the quantization dimension in the `target` and only +// used if the element type of `source` is a per-channel quantized type. During +// the casting, the quantization dimension of the result type needs to be set +// this new `axis` value. +TypeAttr CastQuantizedTypeAttrFromExpressedType(Builder builder, + TypeAttr source, Type target, + int axis); + +// Quantizes the elements in the attribute `real_value` by the quantization +// parameters in `tensor_type`. Returns empty Attribute if the +// `tensor_type` is not a QuantizedType or the quantization fails. +ElementsAttr Quantize(Attribute real_value, Type tensor_type); + +// Quantizes the elements in "legacy mode", where it calls TOCO's methods to +// to quantize values with float scale. +ElementsAttr QuantizeLegacy(Attribute real_value, Type tensor_type); + +// Returns the quantized type for an element attribute. The quantization +// parameters in this type is based on the min and max element of the +// attribute. When the elements in the `attr` are not in floating-point, or +// the value range isn't straddling zero, an empty type is returned. The min/max +// are adjusted to be symmetric if `symmetric` flag is set to True. And +// `symmetric` can only be set to true when it is signed and narrow_range. +Type GetUniformQuantizedTypeForWeight(ElementsAttr attr, bool symmetric, + unsigned num_bits, bool is_signed, + bool narrow_range, + bool legacy_float_scale = false, + bool use_fake_quant_num_bits = false); + +// Returns the per channel quantized type for an element attribute. +// `quant_dim` defines the quantization axis. The channel min/max are adjusted +// to be symmetric if `symmetric` flag is set to True. And `symmetric` can only +// be set to true when it is signed and narrow_range. +Type GetUniformQuantizedPerAxisTypeForWeight( + ElementsAttr attr, int quant_dim, bool symmetric, unsigned num_bits, + bool is_signed, bool narrow_range, bool legacy_float_scale = false, + bool use_fake_quant_num_bits = false); + +// Returns the quantized type of a bias input, given the quantized types of +// other operands which are multiply-accumulated (the bias is added to the +// accumulated value). +quant::QuantizedType GetUniformQuantizedTypeForBias( + const std::vector& op_types, int adjusted_quant_dim, + bool legacy_float_scale = false); + +// Gets quantization scale specs (e.g. fixed output range, same result and +// operand scales) from the default quantization interfaces. The op should +// outlive returned spec for its interface methods to be properly referenced. +std::unique_ptr GetDefaultQuantScaleSpec(Operation* op); + +// The function might contain more stats ops than required, and it will +// introduce requantize if the calibration stats have conflicts. This method +// tries to remove all the redundant stats ops. +bool RemoveRedundantStatsOps(mlir::func::FuncOp func, + OpQuantSpecGetter op_quant_spec_getter, + OpQuantScaleSpecGetter op_quant_scale_spec_getter = + GetDefaultQuantScaleSpec); + +// Given quantization parameters for int8, compute the quantization parameters +// for uint if it is required, and wrap the result in an UniformQuantizedType. +quant::UniformQuantizedType GetFixedOutputRange(bool is_signed, int bit_width, + Type tensor_type, double scale, + int64_t zero_point, + int64_t storage_min, + int64_t storage_max); + +quant::UniformQuantizedType GetFixedOutputRange(bool is_signed, int bit_width, + Type tensor_type, double scale, + int64_t zero_point); + +// Extracts min and max values from the DenseFPElementsAttr, and stores them +// into `mins` and `maxs`. When mins and maxs are extracted per-channel, +// `dim_size` is number of channels and `slice_size` is the size of slice per +// each channel. When `symmetric` is true, the range is expanded to [-M, M]. +void ExtractMinMaxFromAttr(DenseFPElementsAttr values, int dim_size, + int slice_size, bool symmetric, + SmallVectorImpl& mins, + SmallVectorImpl& maxs); + +// Returns the quantized type for the +// input_type/min/max/storage_type_width/narrow_range. +Type GetQuantizedType(Builder builder, Type input_type, ArrayRef min, + ArrayRef max, int quant_dim, + int storage_type_width, bool narrow_range, bool is_signed, + bool legacy_float_scale = false, + bool use_fake_quant_num_bits = false); +} // namespace TFL +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_COMMON_QUANTIZATION_LIB_QUANTIZATION_UTILS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/tfl_quantization_driver.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/tfl_quantization_driver.cc new file mode 100644 index 0000000000000000000000000000000000000000..c9fccbc864b8a9779de906d31323116e44616aea --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/tfl_quantization_driver.cc @@ -0,0 +1,1143 @@ +/* Copyright 2025 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/compiler/mlir/lite/quantization/common/quantization_lib/tfl_quantization_driver.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/SmallVector.h" +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Dialect/Quant/IR/QuantTypes.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/Matchers.h" // from @llvm-project +#include "mlir/IR/OpDefinition.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_config.h" +#include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_traits.h" +#include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_utils.h" +#include "tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.h" + +namespace mlir { +namespace TFL { +namespace temp { +namespace { + +using ::mlir::Operation; + +constexpr int32_t kBiasMax = std::numeric_limits::max() / 2; + +// Uses the type of `value` to set the initial state of the index-th result if +// `as_result` is true or index-th operand if `as_result` is false. The state +// is immutable if the type is a quantized type. Returns the index of this +// new state in the state vector. +void InitializeStateForValue( + Operation* op, const int index, const Value value, const bool as_result, + std::vector& states, + DenseMap& value_to_state, + DenseMap& operand_states, + DenseMap& result_states) { + const auto [cached, inserted] = value_to_state.try_emplace(value, 0); + if (!inserted) { + if (as_result) { + result_states[{op, index}] = cached->second; + } else { + operand_states[{op, index}] = cached->second; + } + return; + } + + const QuantizedType quantized_type = + QuantizedType::getQuantizedElementType(value.getType()); + + const bool immutable = quantized_type != nullptr; + const QuantizationDriver::QuantStateIndex next_state_index = states.size(); + states.push_back({quantized_type, immutable}); + if (as_result) { + result_states[{op, index}] = next_state_index; + } else { + operand_states[{op, index}] = next_state_index; + } + + cached->second = next_state_index; +} + +bool HasPerAxisQuantizedOperand(Operation* op) { + for (int i = 0; i < op->getNumOperands(); ++i) { + if (auto dq_op = dyn_cast_or_null( + op->getOperand(i).getDefiningOp())) { + auto type = + mlir::cast(dq_op.getArg().getType()).getElementType(); + if (auto per_axis_qtype = + mlir::dyn_cast_or_null( + QuantizedType::getQuantizedElementType(type))) { + return true; + } + } + } + return false; +} + +} // namespace + +void QuantizationDriver::InitializeArgState(const BlockArgument arg, + const Value arg_value) { + const auto [cached, inserted] = value_to_state_.try_emplace(arg_value, 0); + if (!inserted) { + arg_states_[arg] = cached->second; + return; + } + + const QuantizedType quantized_type = + QuantizedType::getQuantizedElementType(arg_value.getType()); + const bool immutable = quantized_type != nullptr; + const QuantizationDriver::QuantStateIndex next_state_index = states_.size(); + states_.push_back({quantized_type, immutable}); + arg_states_[arg] = next_state_index; + cached->second = next_state_index; +} + +void QuantizationDriver::InitializeOperandState(Operation* op, const int index, + const Value value) { + InitializeStateForValue(op, index, value, /*as_result=*/false, states_, + value_to_state_, operand_states_, result_states_); +} + +void QuantizationDriver::InitializeResultState(Operation* op, const int index, + const Value value) { + InitializeStateForValue(op, index, value, /*as_result=*/true, states_, + value_to_state_, operand_states_, result_states_); +} + +std::unique_ptr QuantizationDriver::GetQuantSpec(Operation* op) { + return op_quant_spec_getter_(op); +} + +std::unique_ptr QuantizationDriver::GetQuantScaleSpec( + Operation* op) { + return op_quant_scale_spec_getter_(op); +} + +bool QuantizationDriver::IsQuantized(Operation* op) { + for (int i = 0; i < op->getNumResults(); ++i) { + if (GetResultQuantState(op, i).IsEmpty()) return false; + } + return true; +} + +bool QuantizationDriver::SetConstantResultParams(Operation* op) { + DenseFPElementsAttr attr; + const Value result = op->getResult(0); + if (!matchPattern(result, m_Constant(&attr))) { + return false; + } + // TODO: b/323478683 - Make storage_type_width and narrow_range configurable. + Type final_type; + const auto it = optimized_weights_.find(op); + const bool is_weight = it != optimized_weights_.end(); + const bool is_weight_with_per_channel_support = + is_weight && it->second != -1 && is_signed_; + + if (is_weight_with_per_channel_support && !disable_per_channel_) { + // When `disable_per_channel_` is false, per-channel symmetric quantization + // parameters are created from the weights when the ops support per-channel + // quantization. Otherwise, uses per-tensor asymmetric quantization with + // narrow range. + + // per-axis quantization weight, with symmetric min/max enforced. + final_type = GetUniformQuantizedPerAxisTypeForWeight( + attr, it->second, /*symmetric=*/true, /*num_bits=*/8, is_signed_, + /*narrow_range=*/true, legacy_float_scale_); + } else { + // per-tensor quantization weight + final_type = GetUniformQuantizedTypeForWeight( + attr, /*symmetric=*/is_weight && is_signed_, + /*num_bits=*/8, is_signed_, + /*narrow_range=*/is_weight, legacy_float_scale_); + } + if (const auto quant_type = mlir::dyn_cast_or_null(final_type); + quant_type != nullptr) { + return SetResultParams(op, /*result_index=*/0, quant_type); + } + return false; +} + +bool QuantizationDriver::SetResultParams(Operation* op, const int result_index, + const QuantizedType quantized_type) { + QuantState& state = GetResultQuantState(op, result_index); + if (state.params == quantized_type) { + return false; + } + if (!state.IsEmpty()) { + RequantizeStates& rescales = GetResultRequantizeStates(op, result_index); + RequantizeState& rescale = rescales.emplace_back(); + rescale.pos = RequantizeState::ON_INPUT; + rescale.params = quantized_type; + return true; + } + state.params = quantized_type; + AddUserToList(op, result_index); + return true; +} + +QuantizedType QuantizationDriver::GetBiasParams( + Operation* op, const int bias_index, + const ArrayRef non_bias_operand_indices, + const AccumulatorScaleFunc func) { + QuantState& bias_state = GetOperandQuantState(op, bias_index); + if (!bias_state.IsEmpty()) { + return bias_state.params; + } + std::vector op_types{}; + op_types.reserve(non_bias_operand_indices.size()); + + int adjusted_quant_dim = -1; + if (op->getNumOperands() > bias_index) { + // Some kernels allow 1D bias, broadcasting it inside the kernel. In this + // case, the `quantizedDimension=0` when quantizing per-channel. + // However, for some kernels which require bias to be already broadcasted + // to match the accumulation shape, the very last index should be used. + Operation* bias_op = op->getOperand(bias_index).getDefiningOp(); + if (bias_op != nullptr) { + Type bias_type = bias_op->getResult(0).getType(); + if (bias_type != builder_.getNoneType()) { + const int bias_rank = mlir::dyn_cast(bias_type).getRank(); + adjusted_quant_dim = bias_rank > 1 ? bias_rank - 1 : 0; + } + } + } + + for (const int non_bias_operand_index : non_bias_operand_indices) { + const QuantState& non_bias_state = + GetOperandQuantState(op, non_bias_operand_index); + op_types.push_back(non_bias_state.params); + } + return func(op_types, adjusted_quant_dim, legacy_float_scale_); +} + +bool QuantizationDriver::SetOperandParams(Operation* op, + const int operand_index, + const QuantizedType quantized_type, + const bool override) { + QuantState& state = GetOperandQuantState(op, operand_index); + if (state.params == quantized_type) { + return false; + } + + if (!state.IsEmpty() && !override) { + RequantizeStates& rescales = GetOperandRequantizeStates(op, operand_index); + for (RequantizeState& rescale : rescales) { + if (rescale.params == quantized_type) { + rescale.users.emplace_back(op, operand_index); + return true; + } + } + RequantizeState& rescale = rescales.emplace_back(); + rescale.pos = RequantizeState::ON_OUTPUT; + rescale.params = quantized_type; + rescale.users.emplace_back(op, operand_index); + return true; + } + + state.params = quantized_type; + AddOperandToList(op, operand_index); + return true; +} + +void QuantizationDriver::QuantizeOpResult(Operation* op, const int result_index, + const QuantizedType quantized_type) { + builder_.setInsertionPointAfter(op); + const Value original_result = op->getResult(result_index); + QuantizeValue(original_result, quantized_type, op->getLoc()); +} + +void QuantizationDriver::QuantizeArg(BlockArgument arg, + const QuantizedType quantized_type) { + builder_.setInsertionPointToStart(arg.getOwner()); + QuantizeValue(arg, quantized_type, builder_.getUnknownLoc()); +} + +void QuantizationDriver::QuantizeValue(Value value, + QuantizedType quantized_type, + const Location loc) { + const Type expressed_type = value.getType(); + const Type new_value_type = + quantized_type.castFromExpressedType(expressed_type); + // Skip if `value` or `value`'s element type doesn't match the expressed type + // of `quantized_type`. + if (new_value_type == nullptr) return; + + auto quantize = + quantfork::QuantizeCastOp::create(builder_, loc, new_value_type, value); + auto dequantize = quantfork::DequantizeCastOp::create( + builder_, loc, expressed_type, quantize.getResult()); + + // This attribute is set to distinguish the quantize ops being added by the + // quantization pass. These ops can be removed without losing original + // program accuracy. + // TODO: b/323478683 - Make the attribute being part of op definition. + quantize->setAttr(kVolatileOpAttrName, builder_.getUnitAttr()); + + // `original_result` has a use to `quantize`, so this will replace that use + // by the result of `dequantize`. Remember to reset that use afterwards + value.replaceAllUsesWith(dequantize); + quantize.getOperation()->replaceUsesOfWith(dequantize, value); +} + +void QuantizationDriver::RequantizeOpResult(Operation* op, + const int result_index, + RequantizeStates& states) { + if (states.empty()) return; + + builder_.setInsertionPointAfter(op); + Value value = op->getResult(result_index); + RequantizeState::RequantizePosition pos = states.front().pos; + if (pos == RequantizeState::NO_REQUANTIZE) { + return; + } + for (const RequantizeState& state : states) { + // Check that all requantization positions are the same for each state. + // Unsure if this check is required. + if (state.pos != pos) { + return; + } + } + if (pos == RequantizeState::ON_OUTPUT) { + Operation* user = value.getUses().begin().getUser(); + if (isa(user)) { + // The requantize op is inserted between `quantize` and `dequantize` ops. + value = user->getResult(0); + builder_.setInsertionPointAfter(user); + } + } + RequantizeValue(value, states, op->getLoc()); +} + +void QuantizationDriver::RequantizeArg(const BlockArgument arg, + RequantizeStates& states) { + Value value = arg; + builder_.setInsertionPointToStart(arg.getOwner()); + if (value.hasOneUse()) { + Operation* user = value.use_begin().getUser(); + if (auto q = dyn_cast(user)) { + value = q.getResult(); + builder_.setInsertionPoint(arg.getOwner(), ++Block::iterator(user)); + } + } + RequantizeValue(value, states, builder_.getUnknownLoc()); +} + +void QuantizationDriver::RequantizeValue(Value value, RequantizeStates& states, + const Location loc) { + if (states.empty() || states.front().pos == RequantizeState::NO_REQUANTIZE) { + return; + } + if (states.front().pos == RequantizeState::ON_INPUT) { + RequantizeState& state = states.front(); + const Type expressed_type = value.getType(); + // The value needs to be requantized. A Quantize op will be created to use + // it as the operand and replace its uses. + const Type new_type = state.params.castFromExpressedType(expressed_type); + if (!new_type) return; + auto requantize_op = + quantfork::QuantizeCastOp::create(builder_, loc, new_type, value); + value.replaceAllUsesWith(requantize_op); + requantize_op.getOperation()->replaceUsesOfWith(requantize_op, value); + // This requantization was defined as required for the result value, so + // there should be only one requant state. + return; + } + + // If this is an operand that requires requantization, then the value should + // only have one `DequantizeCastOp` user which produces the operand value. + if (!value.hasOneUse()) { + return; + } + auto dequant_op = dyn_cast_or_null( + value.use_begin().getUser()); + if (!dequant_op) { + return; + } + // It is possible that the dequant value is used by a op that doesn't require + // requant, so only overwrite the first if that is not the case. + const int num_uses = std::distance(dequant_op.getResult().use_begin(), + dequant_op.getResult().use_end()); + + // Whether to replace quantization params of the first dequantize op + // after the quantized value is produced. + // If there is a use other than the requantize states, then we can't clobber. + bool clobber_first = num_uses <= states.size(); + for (RequantizeState& state : states) { + Type expressed_type = QuantizedType::castToExpressedType(value.getType()); + if (!expressed_type) continue; + // The value needs to be requantized. A Quantize op will be created to use + // it as the operand and replace its uses. + const Type new_type = state.params.castFromExpressedType(expressed_type); + // This value isn't an expressed type (float), skip. + if (!new_type) continue; + + auto requantize_op = + quantfork::QuantizeCastOp::create(builder_, loc, new_type, value); + + if (clobber_first) { + dequant_op.setOperand(requantize_op.getResult()); + // All ops requiring this value already use the result of dequant. + clobber_first = false; + } else { + auto new_dequant_op = quantfork::DequantizeCastOp::create( + builder_, loc, dequant_op.getResult().getType(), + requantize_op.getResult()); + for (auto [op, operand_idx] : state.users) { + op->setOperand(operand_idx, new_dequant_op.getResult()); + } + } + } +} + +// A heuristic to get quantization parameters satisfies the same scale +// constraints: +// - If there are immutable states, +// - use the single input, or, +// - use the single output, or, +// - use the first one in the collection, +// - use the single input if it is ready, or, +// - use the single output if it is ready, or, +// - use the first ready one in the collection. +QuantizedType QuantizationDriver::GetQuantParamsForSameScaleConstraint( + Operation* op) { + // Two vector to collect Non-empty operands and results states. + std::vector mutable_states, immutable_states; + for (int i = 0; i < op->getNumOperands(); ++i) { + QuantState& state = GetOperandQuantState(op, i); + if (state.immutable) { + immutable_states.push_back(&state); + } else if (!state.IsEmpty()) { + mutable_states.push_back(&state); + } + } + + const int immutable_operands_num = immutable_states.size(); + const int mutable_operands_num = mutable_states.size(); + // Use the operand's state if it is immutable and it is the only one + // operand. + if (op->getNumOperands() == 1 && immutable_operands_num == 1) { + return immutable_states.front()->params; + } + + for (int i = 0; i < op->getNumResults(); ++i) { + QuantState& state = GetResultQuantState(op, i); + if (state.immutable) { + immutable_states.push_back(&state); + } else if (!state.IsEmpty()) { + mutable_states.push_back(&state); + } + } + + const int immutable_results_num = + immutable_states.size() - immutable_operands_num; + const int mutable_results_num = mutable_states.size() - mutable_operands_num; + // Use the result's state if it is immutable and it is the only one result. + if (op->getNumResults() == 1 && immutable_results_num == 1) { + return immutable_states.back()->params; + } + + // Use the first immutable state to quantize the rest operands and results. + if (!immutable_states.empty()) return immutable_states.front()->params; + + // If there are no immutable states, use the operand's state if it is the + // only one operand and has parameters propagated. + if (op->getNumOperands() == 1 && mutable_operands_num == 1) { + return mutable_states.front()->params; + } + + // If there are no immutable states, use the result's state if it is the + // only one result and has parameters propagated. + if (op->getNumResults() == 1 && mutable_results_num == 1) { + return mutable_states.back()->params; + } + + // Use the first propagated state to quantize the rest operands and results. + if (!mutable_states.empty()) return mutable_states.front()->params; + + // None operands/results have parameters propagated, skip this node for now. + return {}; +} + +void QuantizationDriver::PreprocessConstantOps() { + fn_.walk([&](arith::ConstantOp cst) { + // Non-float tensors are neither weights nor require quantization. + const auto type = mlir::dyn_cast(cst.getType()); + if (!type || !mlir::isa(type.getElementType())) return; + + // Skip if the value is NaN or INF. + // Otherwise the illegal scale/zp will be calculated. + auto float_attr = mlir::dyn_cast(cst.getValueAttr()); + if (float_attr && (float_attr.getValues().empty() || + !float_attr.getValues()[0].isFinite())) { + return; + } + + const Value value = cst.getResult(); + builder_.setInsertionPoint(cst); + + // The following loop will change the value uses, thus we cache all the uses + // needs to be changed. + SmallVector> uses; + for (OpOperand& use : value.getUses()) { + uses.push_back({use.getOwner(), use.getOperandNumber()}); + } + for (const auto [user, operand_num] : uses) { + const std::unique_ptr spec = GetQuantSpec(user); + const std::unique_ptr scale_spec = + GetQuantScaleSpec(user); + const BiasParamsMap biases = spec->biases_params; + + // The quantization parameters of a `weight` shouldn't be determined by + // other values. So any constants which are not bias, an operand of an + // op with same scale requirements, and haven't been quantized are + // weights. + if (!biases.contains(operand_num) && + !scale_spec->has_same_scale_requirement && + !dyn_cast(user)) { + // Needs to scan the content of weights to get the quantization + // parameters if there are no quantization parameters (FakeQuant ops). + // For this case, the weight will not be duplicated. + weights_.insert(cst); + if (spec->coeff_op_quant_dim.find(operand_num) != + spec->coeff_op_quant_dim.end()) { + optimized_weights_.insert( + {cst, spec->coeff_op_quant_dim[operand_num]}); + } + } else { + // This is a bias or an operand of an op with same scale requirements, + // so the quantization parameter are propagated from or determined by + // other values. Duplicate this constant in case it is shared by + // different users. + if (uses.size() > 1) { + auto new_constant_op = + arith::ConstantOp::create(builder_, cst.getLoc(), cst.getValue()); + user->setOperand(operand_num, new_constant_op); + } + } + } + }); +} + +void QuantizationDriver::SetupAllStates() { + for (BlockArgument arg : fn_.getArguments()) { + args_.push_back(arg); + Value value = arg; + // If the argument is quantized, it should only has one user. + if (arg.hasOneUse()) { + Operation* user = value.use_begin().getUser(); + if (auto q = dyn_cast(user)) { + value = q.getResult(); + } + } + InitializeArgState(arg, value); + } + + fn_.walk([&](Operation* op) { + std::unique_ptr scale_spec = GetQuantScaleSpec(op); + if (!IsOpQuantizable(op) && !scale_spec->has_same_scale_requirement) { + return; + } + work_list_.push_back(op); + + for (int i = 0; i < op->getNumOperands(); ++i) { + Value operand = op->getOperand(i); + if (Operation* inst = operand.getDefiningOp()) { + // If the operand comes from a `quantfork::DequantizeCastOp`, we use + // the quantized input of this `quantfork::DequantizeCastOp` to set the + // state. + if (auto dq = dyn_cast(inst)) { + operand = dq.getArg(); + } + } + InitializeOperandState(op, i, operand); + } + + for (int i = 0; i < op->getNumResults(); ++i) { + Value result = op->getResult(i); + // If the result has been quantized, it should only be used by a + // `quantfork::QuantizeCastOp`. For this case, we uses the quantized + // result to create the state and mark it immutable. + if (result.hasOneUse()) { + Operation* user = result.use_begin().getUser(); + if (auto q = dyn_cast(user)) { + result = q.getResult(); + } + } + InitializeResultState(op, i, result); + } + }); +} + +arith::ConstantOp QuantizationDriver::DuplicateConstantOpIfNeeded( + arith::ConstantOp op, Operation* target_op, const int operand_index) { + if (op.getResult().hasOneUse()) { + return op; + } + OpBuilder builder(op->getContext()); + builder.setInsertionPointAfter(op); + arith::ConstantOp new_op = cast(builder.clone(*op)); + target_op->getOpOperand(operand_index).set(new_op.getResult()); + InitializeOperandState(target_op, operand_index, new_op.getResult()); + InitializeResultState(new_op, 0, new_op.getResult()); + return new_op; +} + +bool QuantizationDriver::ShouldCheckBiasScale( + Operation* op, const int bias_index, ArrayRef input_indices, + const QuantizedType quantized_type, int& input_index, int& filter_index) { + // For now, restrict scale adjustment to ops with affine quantized weights, + // and having weights and biases as constants. This currently only applies to + // FC and Conv* ops. Restriction for the weight can be relaxed if there are + // needs for adjusting scale of variable weights. + auto affine_op = dyn_cast(op); + auto bias_op = op->getOperand(bias_index).getDefiningOp(); + if (!affine_op || !bias_op || input_indices.size() != 2) return false; + if (!mlir::isa(bias_op.getValue())) return false; + filter_index = affine_op.GetAffineOperandIndex(); + if (!op->getOperand(filter_index).getDefiningOp()) { + return false; + } + if (filter_index == input_indices[0]) { + input_index = input_indices[1]; + } else if (filter_index == input_indices[1]) { + input_index = input_indices[0]; + } else { + return false; + } + + const QuantState& input_state = GetOperandQuantState(op, input_index); + const QuantState& filter_state = GetOperandQuantState(op, filter_index); + // If quantization parameter for the filter is fixed, should return it as-is. + // Only checks ops with 8-bit input and weights, and 32-bit biases. + return input_state.params.getStorageTypeIntegralWidth() == 8 && + filter_state.params.getStorageTypeIntegralWidth() == 8 && + quantized_type.getStorageTypeIntegralWidth() == 32; +} + +bool QuantizationDriver::SetBiasParamsWithAdjustments( + Operation* op, const int bias_index, ArrayRef input_indices, + const QuantizedType params) { + bool changed = false; + + int input_index; + int filter_index; + if (!ShouldCheckBiasScale(op, bias_index, input_indices, params, input_index, + filter_index)) { + return SetOperandParams(op, bias_index, params); + } + + QuantState input_state = GetOperandQuantState(op, input_index); + QuantState filter_state = GetOperandQuantState(op, filter_index); + auto bias_op = op->getOperand(bias_index).getDefiningOp(); + const double input_scale = + mlir::cast(input_state.params).getScale(); + + auto bias_values = mlir::cast(bias_op.getValue()); + // Restrict maximum absolute value of bias within INT_MAX / 2, to make some + // room for accumulator. + if (auto bias_quantized_type = mlir::dyn_cast(params); + bias_quantized_type != nullptr) { + double bias_half_range = 0.0f; + for (auto bias : bias_values.getValues()) { + if (bias_half_range < std::abs(bias.convertToFloat())) { + bias_half_range = std::abs(bias.convertToFloat()); + } + } + if (bias_half_range / bias_quantized_type.getScale() < kBiasMax) { + return SetOperandParams(op, bias_index, params); + } + const double new_bias_scale = + static_cast(bias_half_range) / kBiasMax; + + changed |= SetOperandParams( + op, bias_index, + UniformQuantizedType::getChecked( + bias_op->getLoc(), params.getFlags(), params.getStorageType(), + params.getExpressedType(), new_bias_scale, 0, + params.getStorageTypeMin(), params.getStorageTypeMax())); + arith::ConstantOp filter_op = DuplicateConstantOpIfNeeded( + op->getOperand(filter_index).getDefiningOp(), op, + filter_index); + if (!filter_op) { + return SetOperandParams(op, bias_index, params); + } + + const auto filter_quantized_type = + mlir::cast(filter_state.params); + changed |= SetOperandParams( + op, filter_index, + UniformQuantizedType::getChecked( + filter_op->getLoc(), filter_quantized_type.getFlags(), + filter_quantized_type.getStorageType(), + filter_quantized_type.getExpressedType(), + new_bias_scale / input_scale, 0, + filter_quantized_type.getStorageTypeMin(), + filter_quantized_type.getStorageTypeMax()), + /*override=*/true); + } else if (auto bias_quantized_type = + mlir::dyn_cast(params); + bias_quantized_type != nullptr) { + const auto filter_quantized_type = + mlir::cast(filter_state.params); + std::vector new_bias_scales = bias_quantized_type.getScales().vec(); + std::vector new_filter_scales = + filter_quantized_type.getScales().vec(); + + bool needs_adjustment = false; + for (int i = 0; i < bias_quantized_type.getScales().size(); ++i) { + const float abs_bias = std::abs(bias_values.getValues()[i]); + if (abs_bias / new_bias_scales[i] > kBiasMax) { + new_bias_scales[i] = static_cast(abs_bias) / kBiasMax; + new_filter_scales[i] = new_bias_scales[i] / input_scale; + needs_adjustment = true; + } + } + if (!needs_adjustment) { + return SetOperandParams(op, bias_index, params); + } + changed |= SetOperandParams( + op, bias_index, + quant::UniformQuantizedPerAxisType::getChecked( + bias_op->getLoc(), params.getFlags(), params.getStorageType(), + params.getExpressedType(), new_bias_scales, + bias_quantized_type.getZeroPoints(), + bias_quantized_type.getQuantizedDimension(), + params.getStorageTypeMin(), params.getStorageTypeMax())); + + arith::ConstantOp filter_op = DuplicateConstantOpIfNeeded( + op->getOperand(filter_index).getDefiningOp(), op, + filter_index); + changed |= SetOperandParams( + op, filter_index, + quant::UniformQuantizedPerAxisType::getChecked( + filter_op->getLoc(), filter_quantized_type.getFlags(), + filter_quantized_type.getStorageType(), + filter_quantized_type.getExpressedType(), new_filter_scales, + filter_quantized_type.getZeroPoints(), + filter_quantized_type.getQuantizedDimension(), + filter_quantized_type.getStorageTypeMin(), + filter_quantized_type.getStorageTypeMax()), + /*override=*/true); + } + return changed; +} + +// This method scans the operations in the function to setup the initial +// states for quantization parameter propagation. +// TODO: b/323478683 - This algorithm assumes there are only one pair of +// `quantfork::QuantizeCastOp` and `quantfork::DequantizeCastOp` ops between two +// quantizable ops. A sanity check should be applied. +void QuantizationDriver::Initialize() { + // Duplicate the bias constant, so the states can be setup correctly. + // TODO: b/323478683 - Function definition should also be duplicated if there + // are multiple call sites. + PreprocessConstantOps(); + + // Setup all the internal states. + SetupAllStates(); +} + +namespace { + +bool IsConcatWithUint8QuantizedTypes(Operation* op) { + auto concat = mlir::dyn_cast_or_null(op); + if (!concat) { + return false; + } + + QuantizedType t = nullptr; + for (auto operand : concat.getOperands()) { + auto def_op = operand.getDefiningOp(); + if (!def_op) { + continue; + } + + auto dq_op = mlir::dyn_cast_or_null(def_op); + if (!dq_op) { + continue; + } + + auto qtype = + QuantizedType::getQuantizedElementType(dq_op.getArg().getType()); + if (!qtype) { + continue; + } + + t = qtype; + break; + } + + if (!t) { + return false; + } + + auto st = mlir::dyn_cast_or_null(t.getStorageType()); + if (!st) { + return false; + } + + return !t.isSigned() && st.getWidth() == 8; +} + +std::tuple ExtractMinMax(UniformQuantizedType type) { + double scale = type.getScale(); + int64_t zero_point = type.getZeroPoint(); + int64_t storage_type_min = type.getStorageTypeMin(); + int64_t storage_type_max = type.getStorageTypeMax(); + double real_min = static_cast(storage_type_min - zero_point) * scale; + double real_max = static_cast(storage_type_max - zero_point) * scale; + return {real_min, real_max}; +} + +QuantizedType CalculateNewQuantizedType( + llvm::ArrayRef qtypes) { + if (qtypes.size() == 1) { + return qtypes[0]; + } + + double real_min = std::numeric_limits::max(); + double real_max = std::numeric_limits::min(); + for (auto uniform_qtype : qtypes) { + auto min_max = ExtractMinMax(uniform_qtype); + real_min = std::min(real_min, std::get<0>(min_max)); + real_max = std::max(real_max, std::get<1>(min_max)); + } + auto uniform_qtype = qtypes[0]; + double q_min = static_cast(uniform_qtype.getStorageTypeMin()); + double q_max = static_cast(uniform_qtype.getStorageTypeMax()); + double scale = (real_max - real_min) / (q_max - q_min); + int64_t zero_point = static_cast(q_min - (real_min / scale)); + + return UniformQuantizedType::get( + uniform_qtype.getFlags(), uniform_qtype.getStorageType(), + uniform_qtype.getExpressedType(), scale, zero_point, + uniform_qtype.getStorageTypeMin(), uniform_qtype.getStorageTypeMax()); +} + +} // namespace + +// Propagates the quantization parameters to the operands, results, and biases. +// TODO: b/323478683 - Do not use while loop to handle this logic. +bool QuantizationDriver::PropagateParamsAndReturnIfChanged() { + // TODO: b/323478683 - Use a typed indicator instead of a bool value. + bool changed = false; + while (!work_list_.empty()) { + Operation* op = work_list_.back(); + work_list_.pop_back(); + + // This op has been quantized, so we should not consider it again. + if (quantized_.contains(op)) continue; + quantized_.insert(op); + + if (auto constant_op = dyn_cast(op); constant_op) { + // If the workflow requires inferring ranges from the content + // (post-training quantization) and it is weight (filter) and hasn't + // been quantized, we infer the quantization parameters from the content. + if (qdq_conversion_mode_ != QDQConversionMode::kQDQStrict && + infer_tensor_range_ && IsWeight(constant_op) && !IsQuantized(op)) { + // The quantization parameters are determined by the content of the + // constant. + changed |= SetConstantResultParams(op); + } + continue; + } + + if (qdq_conversion_mode_ != QDQConversionMode::kQDQStrict && + IsConcatWithUint8QuantizedTypes(op)) { + auto concat = mlir::dyn_cast_or_null(op); + llvm::DenseMap operand_qtypes; + auto operands = concat.getOperands(); + for (auto i = 0; i < operands.size(); i++) { + auto op = operands[i].getDefiningOp(); + if (!op) { + continue; + } + + auto dq_op = mlir::dyn_cast_or_null(op); + if (!dq_op) { + continue; + } + + auto qtype = + QuantizedType::getQuantizedElementType(dq_op.getArg().getType()); + if (!qtype) { + continue; + } + + auto uniform_qtype = + mlir::dyn_cast_or_null(qtype); + if (!uniform_qtype) { + continue; + } + + operand_qtypes[i] = uniform_qtype; + } + + llvm::DenseMap result_qtypes; + llvm::SmallVector users(op->user_begin(), op->user_end()); + for (auto i = 0; i < users.size(); i++) { + auto user = users[i]; + auto q_op = mlir::dyn_cast_or_null(user); + if (!q_op) { + continue; + } + + auto qtype = QuantizedType::getQuantizedElementType(q_op.getType()); + if (!qtype) { + continue; + } + + auto uniform_qtype = + mlir::dyn_cast_or_null(qtype); + if (!uniform_qtype) { + continue; + } + + result_qtypes[i] = uniform_qtype; + } + + // If all operands and results are already quantized then leave it be. + if (operand_qtypes.size() == operands.size() && + result_qtypes.size() == users.size()) { + continue; + } + + // Calculate a new scale and zp using existing parameters. + // If no result qtype exists then calculate a new one based off of the + // ones specified on the operands. + // If no operand qtypes exist use the result qtype. + // We know that at least one operand or result type is quantized at this + // point. + llvm::SmallVector qtypes; + if (result_qtypes.empty()) { + for (auto [idx, qtype] : operand_qtypes) { + qtypes.push_back(qtype); + } + } else { + qtypes.push_back(result_qtypes[0]); + } + + auto new_qtype = CalculateNewQuantizedType(qtypes); + + for (int i = 0; i < op->getNumOperands(); ++i) { + auto it = operand_qtypes.find(i); + if (it != operand_qtypes.end()) { + continue; + } + changed |= SetOperandParams(op, i, new_qtype); + } + + for (int i = 0; i < op->getNumResults(); ++i) { + auto it = result_qtypes.find(i); + if (it != result_qtypes.end()) { + continue; + } + changed |= SetResultParams(op, i, new_qtype); + } + + continue; + } + + std::unique_ptr scale_spec = GetQuantScaleSpec(op); + + if (scale_spec->has_same_scale_requirement) { + const QuantizedType params = GetQuantParamsForSameScaleConstraint(op); + // The quantization parameters haven't been propagated to any operands + // or results. Skip this node for now. + if (!params) { + quantized_.erase(op); + continue; + } + + // If this is a QDQ conversion only, the op could have a same-scale + // requirement for the floating point kernel but allow per-axis + // quantization for the quantized kernel. If the quantized dimension + // changes, the following logic no longer works as the same `params` + // shouldn't be used for both input and output quantization params. + // E.g. During TransposeOp's quantization propagation in + // PrepareQuantize, if the quantization is per-axis and the + // QuantizedDimension is transposed, then the output q-dq params must + // reflect the new QuantizedDimension. So, check and skip the + // propagation if any of the operands has a per-axis quantized type param + // and `RequiredSameQuantizedAxes` set to false. + // Currently, these lines of code are only applicable to TFL_TransposeOp + // and TFL_ReshapeOp. And the output q-dq propagation for this Op is + // performed in `PropagateTransposedPerAxisQuantDim` and + // `PropagateReshapedPerAxisQuantDim` respectively. + if (qdq_conversion_mode_ != QDQConversionMode::kQDQNone && + !scale_spec->required_same_quantized_axes_func()) { + if (HasPerAxisQuantizedOperand(op)) continue; + } + + // Use the final state to set all the operands' parameters. + for (int i = 0; i < op->getNumOperands(); ++i) { + if (auto type = + mlir::dyn_cast(op->getOperand(i).getType())) { + // Without this check, it will accidentally propagate the quantization + // information by the shared non-float tensors. + if (mlir::isa(type.getElementType())) + changed |= SetOperandParams(op, i, params); + } + } + + // Use the final state to set all the results' parameters. + for (int i = 0; i < op->getNumResults(); ++i) + if (auto type = mlir::dyn_cast(op->getResult(i).getType()); + type != nullptr) { + // Without this check, it will accidentally propagate the quantization + // information by the shared non-float-tensors. + if (mlir::isa(type.getElementType())) + changed |= SetResultParams(op, i, params); + } + } + + // If the model already contains immutable QDQs, require upstream to + // explicitly fix output range instead. + if (scale_spec->has_fixed_output_range && infer_tensor_range_ && + qdq_conversion_mode_ == QDQConversionMode::kQDQNone) { + // Infer ranges from the activation ops. This is usually required for + // the post-training quantization workflow. + // TODO: b/323478683 - Different result can have different fixed range. + const QuantizedType params = + scale_spec->fixed_output_range_func(is_signed_, bit_width_); + for (auto i = 0; i < op->getNumResults(); ++i) { + // The range is null if the result has been quantized. + if (params) { + changed |= SetResultParams(op, i, params); + } + } + } + + const std::unique_ptr spec = GetQuantSpec(op); + for (const auto& [bias_operand_idx, non_bias_params] : + spec->biases_params) { + const auto& [non_bias_operand_indices, accumulator_scale_func] = + non_bias_params; + const QuantizedType params = + GetBiasParams(op, bias_operand_idx, non_bias_operand_indices, + accumulator_scale_func); + if (!params) { + quantized_.erase(op); + continue; + } + changed |= SetBiasParamsWithAdjustments(op, bias_operand_idx, + non_bias_operand_indices, params); + } + } + + return changed; +} + +// Finalizes the arguments and result states in the function. +void QuantizationDriver::Finalize() { + for (BlockArgument arg : args_) { + const QuantState& state = GetArgQuantState(arg); + RequantizeStates& requantizes = GetArgRequantizeStates(arg); + if (state.IsEmpty() || (state.immutable && requantizes.empty())) { + continue; + } + + if (!state.immutable) { + QuantizeArg(arg, state.params); + } + + if (!requantizes.empty()) { + RequantizeArg(arg, requantizes); + } + } + + for (const auto& [op_with_result_idx, quant_state_idx] : result_states_) { + const auto [op, result_idx] = op_with_result_idx; + const QuantState& state = GetResultQuantState(op, result_idx); + RequantizeStates& requantizes = GetResultRequantizeStates(op, result_idx); + if (state.IsEmpty() || (state.immutable && requantizes.empty())) { + continue; + } + + if (!state.immutable) { + QuantizeOpResult(op, result_idx, state.params); + } + + if (!requantizes.empty()) { + RequantizeOpResult(op, result_idx, requantizes); + } + } +} + +// Runs quantization in following steps: +// 1. Scans the operations in the function to setup the initial +// states for quantization parameter propagation. +// 2. Propagates the quantization parameters to the operands, results, and +// biases. +// 3. Finalizes the arguments and result states in the function. +void QuantizationDriver::Run() { + Initialize(); + if (PropagateParamsAndReturnIfChanged()) { + Finalize(); + } +} + +void ApplyQuantizationParamsPropagation( + const func::FuncOp func, const bool is_signed, const int bit_width, + const bool disable_per_channel, + const OpQuantSpecGetter op_quant_spec_getter, + const bool infer_tensor_ranges, const bool legacy_float_scale, + QDQConversionMode qdq_conversion_mode) { + ApplyQuantizationParamsPropagation( + func, is_signed, bit_width, disable_per_channel, op_quant_spec_getter, + GetDefaultQuantScaleSpec, infer_tensor_ranges, legacy_float_scale, + qdq_conversion_mode); +} + +void ApplyQuantizationParamsPropagation( + const func::FuncOp func, const bool is_signed, const int bit_width, + const bool disable_per_channel, + const OpQuantSpecGetter op_quant_spec_getter, + const OpQuantScaleSpecGetter op_quant_scale_spec_getter, + const bool infer_tensor_ranges, const bool legacy_float_scale, + QDQConversionMode qdq_conversion_mode) { + QuantizationDriver(func, is_signed, bit_width, disable_per_channel, + op_quant_spec_getter, op_quant_scale_spec_getter, + infer_tensor_ranges, qdq_conversion_mode, + legacy_float_scale) + .Run(); +} +} // namespace temp +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/tfl_quantization_driver.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/tfl_quantization_driver.h new file mode 100644 index 0000000000000000000000000000000000000000..3772c062ba83746181ac512f4b4475ff619ad045 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/tfl_quantization_driver.h @@ -0,0 +1,391 @@ +/* Copyright 2025 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_COMMON_QUANTIZATION_LIB_TFL_QUANTIZATION_DRIVER_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_COMMON_QUANTIZATION_LIB_TFL_QUANTIZATION_DRIVER_H_ + +#include +#include +#include + +#include "absl/container/flat_hash_map.h" +#include "absl/container/flat_hash_set.h" +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/STLExtras.h" +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Dialect/Quant/IR/QuantTypes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/OpDefinition.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_config.h" +#include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_utils.h" + +namespace mlir { +namespace TFL { +// TODO(b/413355305): Remove temp namespace after TFL's 2 quantization_drivers +// are merged. +namespace temp { + +// The state for each op result during the quantization parameters propagation. +struct QuantState { + // Quantization parameters propagated to an op result. + QuantizedType params; + // A flag indicates this state (the params) shouldn't be changed after it is + // initialized. This flag will be set to true if the quantization parameters + // are from the quantization-aware training. + const bool immutable; + + bool IsEmpty() const { return params == nullptr; } +}; + +// The state for rescaling the propagated quantization parameters. This can be +// on the input side to satisfy the constraint of previous operation, or on the +// output side to satisfy the constraint of the next operation. +struct RequantizeState { + // Sometimes, we have to "requantize" the quantization result to satisfy all + // the constraints. The "requantize" can happen either on the input or output + // of the quantization result. + enum RequantizePosition { + NO_REQUANTIZE, + ON_INPUT, + ON_OUTPUT + } pos = NO_REQUANTIZE; + + // Quantization parameters will be used to add the requantize ops. + QuantizedType params; + + // Avoid clobbering all uses of the value, limit to just these ops. + SmallVector> users; +}; + +using RequantizeStates = SmallVector; + +// This is a worklist-driven driver for propagating quantization parameters +// across operations. +// +// The initial quantization parameters are extracted from the quantized type +// between adjacent `quantfork::QuantizeCastOp` and +// `quantfork::DequantizeCastOp`s. All these initial parameters are marked as +// immutable because they are from quantization-aware training. +// +// The algorithm traverses each op and sets the quantization parameters of its +// operands and results, according to its quantization specification, and then +// adds the operands and results to the worklist. If there are any conflicts +// (for example, there are quantization parameters propagated from the previous +// iteration), this process stops if the existing parameters are the immutable, +// or adding `requantize` op to resolve the conflicts. +// +// After the algorithm is converged, pairs of `quantfork::QuantizeCastOp` and +// `quantfork::DequantizeCastOp` are inserted to the right position to +// materialize the propagation and requantize results. +// +class QuantizationDriver { + public: + // Type alias of int used to access `states_`. + using QuantStateIndex = int; + + // (op, operand index) pair. + using OpWithOperandIndex = std::pair; + + // (op, result index) pair. + using OpWithResultIndex = std::pair; + + explicit QuantizationDriver(func::FuncOp func_op, const bool is_signed, + const int bit_width, + const bool disable_per_channel, + OpQuantSpecGetter op_quant_spec_getter, + OpQuantScaleSpecGetter op_quant_scale_spec_getter, + const bool infer_tensor_range, + const QDQConversionMode qdq_conversion_mode, + const bool legacy_float_scale = false) + : fn_(func_op), + builder_(func_op.getBody()), + is_signed_(is_signed), + bit_width_(bit_width), + disable_per_channel_(disable_per_channel), + op_quant_spec_getter_(op_quant_spec_getter), + op_quant_scale_spec_getter_(op_quant_scale_spec_getter), + infer_tensor_range_(infer_tensor_range), + legacy_float_scale_(legacy_float_scale), + qdq_conversion_mode_(qdq_conversion_mode) {} + + // The entry point of the quantization parameters propagation. + void Run(); + + // Sets up the states for all the op results in the function. + void Initialize(); + + // Propagates the quantization parameters across all the ops. + bool PropagateParamsAndReturnIfChanged(); + + // Inserts the Quantize and Dequantize ops according to the propagation + // result. + void Finalize(); + + SmallVector GetArgs() { return args_; } + + llvm::DenseMap, int> GetResultStates() { + return result_states_; + } + + DenseMap result_states_; + + // Returns the state of the block argument. + QuantState& GetArgQuantState(BlockArgument arg) { + return states_[arg_states_[arg]]; + } + + // Returns the state of the index-th result of the op. + QuantState& GetResultQuantState(Operation* op, const int index) { + return states_[result_states_[{op, index}]]; + } + + private: + // Duplicates the constant op if it has multiple uses, and replaces + // target_op->operand[operand_index] with the newly created op. This also + // replaces corresponsing quantization states. + arith::ConstantOp DuplicateConstantOpIfNeeded(arith::ConstantOp op, + Operation* target_op, + int operand_index); + + // Adjusts bias scale that is derived from other scales (fc, conv ops) to + // prevent overflow of quantized bias values. This also changes quantization + // state of other inputs when needed. + bool SetBiasParamsWithAdjustments(Operation* op, int bias_index, + ArrayRef input_indices, + QuantizedType params); + + // Checks preconditions to adjust bias scale. + bool ShouldCheckBiasScale(Operation* op, int bias_index, + ArrayRef input_indices, + QuantizedType quantized_type, int& input_index, + int& filter_index); + + // Preprocesses the constants by doing the following: + // - Duplicates constants if it is used by multiple ops. For example, if a + // constant is used by multiple ops as a bias, duplicate constants and + // let each op assign its own quantization parameter for bias. + // - Adds all the non-bias constants (weights) to a set for looking up + // later. + // - Adds all per-channel weights to a set for looking up later. + void PreprocessConstantOps(); + + // Sets up all the data structures for quantization propagation. + void SetupAllStates(); + + // Returns Whether the constant is a weight, which shouldn't be shared by + // different ops. + bool IsWeight(Operation* cst) { return llvm::is_contained(weights_, cst); } + + // Returns all the related quantization constraints of the op. + std::unique_ptr GetQuantSpec(Operation* op); + std::unique_ptr GetQuantScaleSpec(Operation* op); + + // Returns whether quantization parameters have been propagated to the results + // of this op. + bool IsQuantized(Operation* op); + + // Adds all the users of index-th result of op to the work list. + void AddUserToList(Operation* op, const int index) { + for (Operation* user : op->getResult(index).getUsers()) { + work_list_.push_back(user); + } + } + + // Adds the defining op of index-th operand of op to the work list. + void AddOperandToList(Operation* op, const int index) { + if (Operation* operand_op = op->getOperand(index).getDefiningOp(); + operand_op != nullptr) { + work_list_.push_back(operand_op); + } + } + + // Returns the quantization params for the bias input from the non-bias + // operands which have their indexes in the `non_biases` vector. The returned + // parameters are calculated by `func`. + QuantizedType GetBiasParams(Operation* op, int bias_index, + ArrayRef non_bias_operand_indices, + AccumulatorScaleFunc func); + + // Sets the quantization parameters of the result to `quantized_type`. If + // any quantization parameters have been propagated, a requantize will + // happen on the input of propagated quantization. Returns `true` if internal + // state has been modified. + bool SetResultParams(Operation* op, int result_index, + QuantizedType quantized_type); + + // Sets the quantization parameters of the operand to `quantized_type`. If any + // quantization parameters have been propagated, a `requantize` will happen on + // the output of propagated quantization. When `override` is set, quantization + // state of the value is replaced instead of adding requantization. Returns + // `true` if internal state has been modified. + bool SetOperandParams(Operation* op, int operand_index, + QuantizedType quantized_type, bool override = false); + + // Sets the quantization parameters of the constant result according to its + // content. + bool SetConstantResultParams(Operation* op); + + // Inserts the Quantize and Dequantize ops after `op`'s `index`-th result. The + // quantized element type for the result is `quantized_type`. + void QuantizeOpResult(Operation* op, int result_index, + QuantizedType quantized_type); + + // Inserts the Quantize and Dequantize ops after `arg`. The quantized element + // type for `arg` is `quantized_type`. + void QuantizeArg(BlockArgument arg, QuantizedType quantized_type); + + // Inserts the Quantize and Dequantize ops (i.e. QDQ) after `value`. The + // quantized element type for `value` is `quantized_type`. + void QuantizeValue(Value value, QuantizedType quantized_type, Location loc); + + // Inserts the Quantize ops for requantizing the index-th result of the op. + void RequantizeOpResult(Operation* op, int result_index, + RequantizeStates& states); + + // Inserts the Quantize ops for requantizing a block argument. + void RequantizeArg(BlockArgument arg, RequantizeStates& states); + + // Inserts the Quantize and Dequantize ops to quantize the value and returns + // the Quantize op. + void RequantizeValue(Value value, RequantizeStates& states, Location loc); + + // Returns the quantization parameter satisfies the same scale + // constraints for the op. Returns an empty option if this quantization + // parameter doesn't exist. + QuantizedType GetQuantParamsForSameScaleConstraint(Operation* op); + + // Returns the state of the index-th operand of the op. + QuantState& GetOperandQuantState(Operation* op, const int index) { + return states_[operand_states_[{op, index}]]; + } + + // Returns the states of the index-th operand of the op. + RequantizeStates& GetOperandRequantizeStates(Operation* op, const int index) { + return rescale_states_[operand_states_[{op, index}]]; + } + + // Returns the states of the index-th result of the op. + RequantizeStates& GetResultRequantizeStates(Operation* op, const int index) { + return rescale_states_[result_states_[{op, index}]]; + } + + // Returns the states of the arg. + RequantizeStates& GetArgRequantizeStates(BlockArgument arg) { + return rescale_states_[arg_states_[arg]]; + } + + // Sets the state of an argument. If this value is cached, uses the cached + // result without creating new entry in the state vector. Otherwise, allocate + // a new entry in the state vector. + void InitializeArgState(BlockArgument arg, Value arg_value); + + // Sets the state of the index-th operand of the op. If this operand is + // cached, uses the cached result without creating new entry in the state + // vector. Otherwise, allocate a new entry in the state vector. + void InitializeOperandState(Operation* op, int index, Value value); + + // Sets the state of the index-th result of the op. If this result is cached, + // uses the cached result without creating new entry in the state vector. + // Otherwise, allocate a new entry in the state vector. + void InitializeResultState(Operation* op, int index, Value value); + + func::FuncOp fn_; + OpBuilder builder_; + const bool is_signed_; + const int bit_width_; + const bool disable_per_channel_; + + // We should distinguish weights and bias constants. Biases are specified by + // the quantization spec or are the operands of ops with same scale spec. The + // rest are weights. + DenseSet weights_; + + // The weights require narrow_range quantization. This map collects all the + // weight operands defined by the op quant spec. The value of each entry is + // the quantization dimension. If it is positive, per-channel quantization is + // required. + DenseMap optimized_weights_; + + // All the ops needs to propagate the quantization parameters to. + std::vector work_list_; + absl::flat_hash_set quantized_; + + // The vector contains all the quantization parameters propagated from the + // defining operations of the value, or from the quantization aware training. + std::vector states_; + + // The map contains all the quantization parameters which are required to + // satisfy the same operands and results constraint. The keys of this map are + // the values from `operand_states_` and `result_state_`. + absl::flat_hash_map rescale_states_; + + // Maps of indexes to the propagation state vector from the ops operands, + // results and arguments. + DenseMap operand_states_; + DenseMap arg_states_; + DenseMap value_to_state_; + + // This vector is to preserve the arguments order, so the newly inserted + // quantized ops for the arguments are deterministically ordered. + SmallVector args_; + + OpQuantSpecGetter op_quant_spec_getter_; + OpQuantScaleSpecGetter op_quant_scale_spec_getter_; + + // Infer output ranges for activation ops and constants. This is usually + // required for post-training quantization. + const bool infer_tensor_range_; + + // Calculate scales in float instead of double, so that the scales and + // quantized values are exactly the same with the TOCO quantizer. + const bool legacy_float_scale_; + + // The type of qdq conversion. + const QDQConversionMode qdq_conversion_mode_; +}; + +// Propagates quantization parameters across ops in this function and satisfies +// the quantization specification of the ops. This methods assumes the initial +// quantization parameters are stored as adjacent quantize and dequantize ops +// and the propagation results are materialized by inserting pairs of quantize +// and dequantize ops to this function. Set `disable_per_channel` to true to not +// use per channel quantization even the op supports it. +// Setting `infer_tensor_range` to true, to infer quantization parameters from +// the activation ops and weight constants. This is only used for post-training +// quantization. +void ApplyQuantizationParamsPropagation(func::FuncOp func, bool is_signed, + int bit_width, bool disable_per_channel, + OpQuantSpecGetter op_quant_spec_getter, + bool infer_tensor_ranges, + bool legacy_float_scale, + QDQConversionMode qdq_conversion_mode); + +void ApplyQuantizationParamsPropagation( + func::FuncOp func, bool is_signed, int bit_width, bool disable_per_channel, + OpQuantSpecGetter op_quant_spec_getter, + OpQuantScaleSpecGetter op_quant_scale_spec_getter, bool infer_tensor_ranges, + bool legacy_float_scale, QDQConversionMode qdq_conversion_mode); + +} // namespace temp +} // namespace TFL +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_COMMON_QUANTIZATION_LIB_TFL_QUANTIZATION_DRIVER_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/test_base.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/test_base.h new file mode 100644 index 0000000000000000000000000000000000000000..9250de6f52fd4257e9d564348f3a977f87f3477d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/common/test_base.h @@ -0,0 +1,88 @@ +/* Copyright 2023 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_COMMON_TEST_BASE_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_COMMON_TEST_BASE_H_ + +#include + +#include +#include "absl/strings/string_view.h" +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Dialect/Quant/IR/Quant.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/OwningOpRef.h" // from @llvm-project +#include "mlir/Parser/Parser.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "stablehlo/dialect/StablehloOps.h" // from @stablehlo +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.h" +#include "tensorflow/compiler/mlir/quantization/common/func.h" +#include "tensorflow/compiler/mlir/quantization/common/ir/QuantOps.h" +#include "tensorflow/compiler/mlir/quantization/stablehlo/cc/context.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_dialect.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_executor.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_saved_model.h" +#include "tensorflow/core/platform/test.h" + +namespace mlir::quant { + +using ::testing::Test; + +class QuantizationTestBase : public Test { + protected: + QuantizationTestBase() + : ctx_(stablehlo::CreateMlirContextForQuantization()), + builder_(ctx_.get()) { + ctx_->loadDialect< + arith::ArithDialect, mlir::stablehlo::StablehloDialect, + func::FuncDialect, TF::TensorFlowDialect, TFL::TensorFlowLiteDialect, + tf_saved_model::TensorFlowSavedModelDialect, + tf_executor::TensorFlowExecutorDialect, quant::QuantDialect, + quantfork::QuantizationForkDialect, ir::TFQuantDialect>(); + } + + // Parses `module_op_str` to create a `ModuleOp`. + OwningOpRef ParseModuleOpString( + const absl::string_view module_op_str) { + return parseSourceString(module_op_str, ctx_.get()); + } + + // Convenience function that returns the first operation of type `OpT` from + // the `@main` function in `module_op`. Useful when testing with a text + // representation of a `ModuleOp` containing a single function `@main`. + // Returns `failure` iff there is no `@main` or no such operation is found in + // `@main`. + template + FailureOr FindFirstOpFromMainFunc(ModuleOp module_op) { + func::FuncOp main_func_op = FindMainFuncOp(module_op); + if (main_func_op == nullptr) return failure(); + + auto ops = main_func_op.getOps(); + if (ops.empty()) return failure(); + + return *ops.begin(); + } + + std::unique_ptr ctx_; + OpBuilder builder_; +}; + +} // namespace mlir::quant + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_COMMON_TEST_BASE_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/device_target.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/device_target.cc new file mode 100644 index 0000000000000000000000000000000000000000..8680bf67d8340c81db453e08e62e6ecd8dda9ce1 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/device_target.cc @@ -0,0 +1,184 @@ +/* Copyright 2020 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/compiler/mlir/lite/quantization/device_target.h" + +#include +#include +#include +#include + +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/Casting.h" +#include "mlir/Dialect/Quant/IR/QuantTypes.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/Types.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.h" +#include "tensorflow/compiler/mlir/lite/quantization/numerical_utils.h" + +namespace mlir { +namespace quant { + +constexpr int k8Bits = 8; +constexpr int k32Bits = 32; +constexpr unsigned kSigned = QuantizationFlags::Signed; + +DeviceTarget::DeviceTarget(MLIRContext* ctx) : ctx_(ctx) { + f32_ = Float32Type::get(ctx_); + i8_ = IntegerType::get(ctx_, k8Bits); + i8_min_ = QuantizedType::getDefaultMinimumForInteger(kSigned, k8Bits); + i8_max_ = QuantizedType::getDefaultMaximumForInteger(kSigned, k8Bits); + i32_ = IntegerType::get(ctx_, k32Bits); + i32_min_ = QuantizedType::getDefaultMinimumForInteger(kSigned, k32Bits); + i32_max_ = QuantizedType::getDefaultMaximumForInteger(kSigned, k32Bits); + any_ = AnyQuantizedType(); + qi8_ = AnyQuantizedType::get(kSigned, i8_, f32_, i8_min_, i8_max_); + qi8n_ = AnyQuantizedType::get(kSigned, i8_, f32_, i8_min_ + 1, i8_max_); + qi32_ = AnyQuantizedType::get(kSigned, i32_, f32_, i32_min_, i32_max_); + assert(qi8n_ == qi8n_); +} + +std::optional DeviceTarget::GetKernelSpec( + llvm::StringRef kernel, const KernelSpecs::Signature& signature) const { + auto kernel_specs_it = specs_.find(kernel); + if (kernel_specs_it == specs_.end()) return std::nullopt; + return kernel_specs_it->getValue().Find(signature); +} + +ScaleDecomposeFn DeviceTarget::GetDecomposeFn( + quantfork::QuantizeRegionOp op) const { + auto kernel_specs_it = specs_.find(op.getLogicalKernel()); + if (kernel_specs_it == specs_.end()) return ScaleDecomposeFn(nullptr); + return kernel_specs_it->second.GetDecomposeFn(); +} + +void DeviceTarget::AppendToSignature(Type spec, + KernelSpecs::Signature* signature) { + if (auto quant = llvm::dyn_cast_or_null(spec)) { + signature->push_back(AnyQuantizedType::get( + quant.getFlags(), quant.getStorageType(), quant.getExpressedType(), + quant.getStorageTypeMin(), quant.getStorageTypeMax())); + } else if (auto any = llvm::dyn_cast_or_null(spec)) { + signature->push_back(any); + } else { // float + signature->push_back(AnyQuantizedType()); + } +} + +LogicalResult DeviceTarget::RegisterKernel( + llvm::StringRef kernel, const KernelSpecs::Signature& signature, + const ScaleFn& fn, const ScaleDecomposeFn& dfn) { + return specs_[kernel].Add(signature, {ScaleConstraintType::CustomScale, fn}); +} + +namespace ph = std::placeholders; + +LogicalResult DeviceTarget::RegisterKernel( + llvm::StringRef kernel, const KernelSpecs::Signature& signature, + const ScaleConstraintType constraint) { + if (failed(specs_[kernel].Add(signature, {constraint, {}}))) return failure(); + switch (constraint) { + case ScaleConstraintType::OutputInputSameScale: + specs_[kernel].WithImpl(std::bind(&DeviceTarget::DecomposeSameScale, + ph::_1, ph::_2, ph::_3, ph::_4)); + return success(); + default: + return failure(); + } +} + +LogicalResult DeviceTarget::DecomposeMultiplyAccumulateScale( + Operation* op, QuantizedMultipliers* input_multipliers, + QuantizedMultipliers* output_multipliers, QuantizedRanges* output_ranges) { + auto rop = llvm::dyn_cast(op); + if (!rop) return failure(); + + llvm::SmallVector input_specs, out_specs; + for (auto spec : rop.getInputSpecs()) { + input_specs.push_back(llvm::cast(spec).getValue()); + } + for (auto spec : rop.getOutputSpecs()) { + out_specs.push_back(llvm::cast(spec).getValue()); + } + + auto in_spec = llvm::dyn_cast(input_specs[0]); + // TODO(fengliuai): handles the PerAxis QuantizedType. + auto w_spec = llvm::dyn_cast(input_specs[1]); + auto b_spec = llvm::dyn_cast(input_specs[2]); + auto o_spec = llvm::dyn_cast(out_specs[0]); + if (!in_spec || !w_spec || !b_spec || !o_spec) return failure(); + + double scale_product = in_spec.getScale() * w_spec.getScale(); + if (fabs(scale_product - b_spec.getScale()) >= 1e-6) return failure(); + + // input multipliers + input_multipliers->append(3, kUnitQuantizedMultiplier); + + // output multipliers + double real_multiplier = scale_product / o_spec.getScale(); + output_multipliers->push_back(QuantizeMultiplier(real_multiplier)); + + // output ranges + auto min = rop->getAttrOfType("min"); + auto max = rop->getAttrOfType("max"); + output_ranges->push_back(CalculateQuantizedRange( + o_spec.getScale(), o_spec.getZeroPoint(), + (min ? std::optional(min.getValueAsDouble()) : std::nullopt), + (max ? std::optional(max.getValueAsDouble()) : std::nullopt), + o_spec.getStorageTypeMin(), o_spec.getStorageTypeMax())); + + return success(); +} + +LogicalResult DeviceTarget::DecomposeSameScale( + Operation* op, QuantizedMultipliers* input_multipliers, + QuantizedMultipliers* output_multipliers, QuantizedRanges* output_ranges) { + auto rop = llvm::dyn_cast(op); + if (!rop) return failure(); + + // input multipliers + for (int i = 0; i < op->getNumOperands(); ++i) { + input_multipliers->push_back(kUnitQuantizedMultiplier); + } + + // output multipliers + for (int i = 0; i < op->getNumResults(); ++i) { + output_multipliers->push_back(kUnitQuantizedMultiplier); + } + + auto o_spec = llvm::dyn_cast( + llvm::cast(rop.getOutputSpecs()[0]).getValue()); + if (!o_spec) return failure(); + + // output ranges + auto min = rop->getAttrOfType("min"); + auto max = rop->getAttrOfType("max"); + output_ranges->push_back(CalculateQuantizedRange( + o_spec.getScale(), o_spec.getZeroPoint(), + (min ? std::optional(min.getValueAsDouble()) : std::nullopt), + (max ? std::optional(max.getValueAsDouble()) : std::nullopt), + o_spec.getStorageTypeMin(), o_spec.getStorageTypeMax())); + + return success(); +} + +} // namespace quant +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/device_target.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/device_target.h new file mode 100644 index 0000000000000000000000000000000000000000..21c5e6768ddb506e39564c02a52f1885b426a085 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/device_target.h @@ -0,0 +1,193 @@ +/* Copyright 2020 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_DEVICE_TARGET_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_DEVICE_TARGET_H_ + +#include +#include +#include + +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/Hashing.h" +#include "llvm/ADT/MapVector.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringMap.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/raw_ostream.h" +#include "mlir/Dialect/Quant/IR/QuantTypes.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/Types.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.h" +#include "tensorflow/compiler/mlir/lite/quantization/numerical_utils.h" + +namespace mlir { +namespace quant { + +class QuantizeContext; + +using AdjacentOperations = llvm::SmallVectorImpl; +using QuantizedMultipliers = llvm::SmallVector; +using QuantizedRanges = llvm::SmallVector; +using ScaleFn = std::function; + +using ScaleDecomposeFn = + std::function; + +static const QuantizedMultiplier kUnitQuantizedMultiplier{1, 0}; + +enum class ScaleConstraintType { + OutputInputSameScale, + OutputInputFreeScale, + CustomScale, +}; + +// Each kernel signature has its own specification for scales. +struct KernelSpec { + // Scale constraint + ScaleConstraintType type; + + // Custom function to derive the scales. Only available when the scale + // constraint is `CustomScale`. + ScaleFn scale_fn; +}; + +class KernelSpecs { + public: + using Signature = llvm::SmallVector; + + // Returns the kernel specification for the kernel signature. + std::optional Find(const Signature& signature) const { + auto spec_it = all_signatures_.find(signature); + if (spec_it != all_signatures_.end()) { + return spec_it->second; + } else { + return std::nullopt; + } + } + + ScaleDecomposeFn GetDecomposeFn() const { return decompose_fn_; } + + // Adds the kernel signature with the kernel specification. + LogicalResult Add(const Signature& signature, const KernelSpec& spec) { + if (all_signatures_.insert({signature, spec}).second) return success(); + return failure(); + } + + KernelSpecs& WithSignature(const KernelSpecs::Signature& signature, + const ScaleFn& fn) { + (void)Add(signature, {ScaleConstraintType::CustomScale, fn}); + return *this; + } + + KernelSpecs& WithImpl(const ScaleDecomposeFn& dfn) { + decompose_fn_ = dfn; + return *this; + } + + private: + // The signature is pattern match based. + struct SignatureInfo : public llvm::DenseMapInfo { + static Signature getEmptyKey() { return {}; } + static unsigned getHashValue(Signature val) { + return llvm::hash_combine_range(val.begin(), val.end()); + } + static bool isEqual(Signature LHS, Signature RHS) { + if (LHS.size() != RHS.size()) return false; + for (auto arg : llvm::zip(LHS, RHS)) { + if (std::get<0>(arg) != std::get<1>(arg)) return false; + } + return true; + } + }; + + // Maps the signature to the kernel spec. Note that the matching is + // pattern match based. + llvm::DenseMap all_signatures_; + + // A method to compute the effective multipliers. This is independent on the + // bits of the ports, thus all the signature shares the same here. + ScaleDecomposeFn decompose_fn_; +}; + +class DeviceTarget { + public: + explicit DeviceTarget(MLIRContext* ctx); + + // Retrieves the kernel spec for the quant region op. + std::optional GetKernelSpec( + llvm::StringRef kernel, const KernelSpecs::Signature& signature) const; + + // Retrieves the scale decomposition function for the quant region op. + ScaleDecomposeFn GetDecomposeFn(quantfork::QuantizeRegionOp op) const; + + // converts specification to signature: + // - UniformedQuantizedType -> AnyQuantizedType + // - AnyQuantizedType (int) -> AnyQuantizedType + // - Float -> {} + static void AppendToSignature(Type spec, KernelSpecs::Signature* signature); + + protected: + // Adds the kernel spec with the custom scale function for the kernel. + LogicalResult RegisterKernel(llvm::StringRef kernel, + const KernelSpecs::Signature& signature, + const ScaleFn& fn, const ScaleDecomposeFn& dfn); + + // Adds the kernel spec with the scale constraint type for the kernel. + LogicalResult RegisterKernel(llvm::StringRef kernel, + const KernelSpecs::Signature& signature, + ScaleConstraintType constraint); + + // Adds the kernel with the name. Retrun an existing one if it has been + // added before. + KernelSpecs& RegisterKernel(llvm::StringRef kernel) { return specs_[kernel]; } + + // For "mulmat->add" type of kernels, convert the scales of all the ports to + // multipliers. + static LogicalResult DecomposeMultiplyAccumulateScale( + Operation* op, QuantizedMultipliers* input_multipliers, + QuantizedMultipliers* output_multipliers, QuantizedRanges* output_ranges); + + // For "reshape" type of kernels. + static LogicalResult DecomposeSameScale( + Operation* op, QuantizedMultipliers* input_multipliers, + QuantizedMultipliers* output_multipliers, QuantizedRanges* output_ranges); + + // A set of parameters are required to build the signatures. + FloatType f32_; + IntegerType i8_, i32_; + int64_t i8_min_, i8_max_, i32_min_, i32_max_; + quant::AnyQuantizedType any_, qi8_, qi8n_, qi32_; + + private: + // Maps the kernel names to all the available kernels. + llvm::StringMap specs_; + + // Points to the global MLIRContext. + MLIRContext* ctx_; +}; + +} // namespace quant +} // namespace mlir +#endif // TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_DEVICE_TARGET_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/import_quant_stats_pass.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/import_quant_stats_pass.cc new file mode 100644 index 0000000000000000000000000000000000000000..a414826e5da5a86de9db1fc776a8460b49101e87 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/import_quant_stats_pass.cc @@ -0,0 +1,260 @@ +/* Copyright 2019 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 "absl/strings/str_split.h" +#include "llvm/ADT/APFloat.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringMap.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/Regex.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Dialect/Quant/IR/Quant.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/DialectRegistry.h" // from @llvm-project +#include "mlir/IR/Location.h" // from @llvm-project +#include "mlir/IR/OpDefinition.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/TypeID.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.h" +#include "tensorflow/compiler/mlir/lite/quantization/quantization_info.pb.h" +#include "tensorflow/compiler/mlir/lite/quantization/quantization_passes.h" +#include "tensorflow/compiler/mlir/tensorflow/utils/import_utils.h" +#include "tensorflow/compiler/mlir/tensorflow/utils/location_utils.h" + +// NOLINTNEXTLINE +static llvm::cl::opt quantize_stats( + "quant-test-stats", llvm::cl::value_desc("string"), + llvm::cl::desc("serialized quant info string. Only used in tests"), + llvm::cl::init("")); + +//===----------------------------------------------------------------------===// +// The Pass to import quantization stats to the ops in a function. This requires +// a custom method to retrieve the unique name of the operation. + +namespace mlir { +namespace quant { + +using QuantParamsEntry = QuantizationInfo::QuantParams; + +namespace { +class ImportQuantStatsPass + : public PassWrapper> { + public: + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(ImportQuantStatsPass) + + explicit ImportQuantStatsPass(OperationToName op_to_name) + : op_to_name_(op_to_name) {} + + StringRef getArgument() const final { + // This is the argument used to refer to the pass in + // the textual format (on the commandline for example). + return "quant-import-stats"; + } + StringRef getDescription() const final { + // This is a brief description of the pass. + return "Import quantization stats to the model"; + } + + void runOnOperation() override; + + void getDependentDialects(DialectRegistry ®istry) const override { + registry.insert(); + } + + // Parses the serialized quant stats protobuf and initialize the internal + // data structure. This method must be called after the pass is created. + bool ParseQuantStats(const std::string &stats_str); + + private: + void ImportAsStatsOps(OpBuilder b, Operation *op, int index, + const QuantParamsEntry &info); + + void InsertStatsOpAtResult(OpBuilder b, Value res, ElementsAttr layer_stats, + ElementsAttr axis_stats, IntegerAttr axis); + + // If the index is out of range, this method returns false. Otherwise it + // returns true if the value is a float tensor. + bool IsQuantizableResult(Operation *op, int index) { + if (index < 0 || index >= static_cast(op->getNumResults())) + return false; + Value res = op->getResult(index); + return isa(res.getType()) && + isa(cast(res.getType()).getElementType()); + } + + // A method to retrieve the name for the given op. + OperationToName op_to_name_; + + // We split the normal names and regex names, since the former can use hash + // map to lookup and the latter needs to iterate all the regex to find the + // match. + // The `int` in the following two containers are to specify the result index + // of the given op. -1 indicates all the floating-point results. + llvm::StringMap> name_to_info_; + llvm::StringMap> regex_to_info_; +}; +} // namespace + +bool ImportQuantStatsPass::ParseQuantStats(const std::string &stats_str) { + QuantizationInfo quant_stats; + if (!tensorflow::LoadProtoFromBuffer(stats_str, &quant_stats).ok()) { + return true; + } + + for (const auto &entry : quant_stats.entries()) { + if (!entry.name().empty()) { + std::vector name_and_port = + absl::StrSplit(entry.name(), ':'); + int port = name_and_port.size() == 2 ? std::stoi(name_and_port[1]) : -1; + name_to_info_.insert({name_and_port[0], {port, entry}}); + } else if (!entry.name_regex().empty()) { + std::vector name_and_port = + absl::StrSplit(entry.name_regex(), ':'); + int port = name_and_port.size() == 2 ? std::stoi(name_and_port[1]) : -1; + regex_to_info_.insert({name_and_port[0], {port, entry}}); + } + } + return false; +} + +void ImportQuantStatsPass::InsertStatsOpAtResult(OpBuilder b, Value res, + ElementsAttr layer_stats, + ElementsAttr axis_stats, + IntegerAttr axis) { + auto stats_op = quantfork::StatisticsOp::create( + b, b.getUnknownLoc(), res, layer_stats, axis_stats, axis); + res.replaceAllUsesWith(stats_op); + stats_op.getOperation()->replaceUsesOfWith(stats_op, res); +} + +void ImportQuantStatsPass::ImportAsStatsOps(OpBuilder b, Operation *op, + int index, + const QuantParamsEntry &info) { + if (info.params_size() == 0) return; + + SmallVector min_maxs; + min_maxs.reserve(info.params_size() * 2); + for (const auto ¶m : info.params()) { + llvm::APFloat min(param.min_max().min()); + llvm::APFloat max(param.min_max().max()); + min_maxs.push_back(min); + min_maxs.push_back(max); + } + // The layer stats contain only the first min/max pairs. + ElementsAttr layer_stats = DenseFPElementsAttr::get( + RankedTensorType::get({2}, b.getF32Type()), {min_maxs[0], min_maxs[1]}); + ElementsAttr axis_stats; + IntegerAttr axis; + + if (info.params_size() > 1) { + SmallVector axis_stats_shape{info.params_size(), 2}; + axis_stats = DenseFPElementsAttr::get( + RankedTensorType::get(axis_stats_shape, b.getF32Type()), min_maxs); + axis = b.getI64IntegerAttr(info.meta().quantize_axis()); + } + + b.setInsertionPointAfter(op); + if (IsQuantizableResult(op, index)) { + InsertStatsOpAtResult(b, op->getResult(index), layer_stats, axis_stats, + axis); + } else { + for (int i = 0, e = op->getNumResults(); i < e; ++i) { + if (IsQuantizableResult(op, i)) { + InsertStatsOpAtResult(b, op->getResult(i), layer_stats, axis_stats, + axis); + } + } + } +} + +void ImportQuantStatsPass::runOnOperation() { + func::FuncOp func = getOperation(); + OpBuilder builder(func); + + func.walk([&](Operation *op) { + if (op->hasTrait()) return; + auto op_name = op_to_name_(op); + + // Check the named info collection first. + auto it = name_to_info_.find(op_name); + if (it != name_to_info_.end()) { + ImportAsStatsOps(builder, op, it->second.first, it->second.second); + return; + } + + // Iterate all the regex names and matches the first one. + for (auto ®ex : regex_to_info_) { + if (llvm::Regex(regex.first()).match(op_name)) { + ImportAsStatsOps(builder, op, regex.second.first, regex.second.second); + break; + } + } + }); +} + +// Creates an instance of the default quant parameters pass. +std::unique_ptr> CreateImportQuantStatsPass( + OperationToName op_to_name, const std::string &stats_str) { + auto pass = std::make_unique(op_to_name); + if (pass->ParseQuantStats(stats_str)) return nullptr; + return pass; +} + +// Creates an instance pass to import quantization stats to the operations in +// the function. A custom method to get the name from the op is used because +// different dialect ops might have different ways to assign the name. +std::unique_ptr> +CreateImportQuantStatsPassForTFControlDialect(const std::string &stats_str) { + auto get_name_func = [](Operation *op) { + Location loc = tensorflow::GetLocationWithoutOpType(op->getLoc()); + if (auto name = llvm::dyn_cast(loc)) { + return name.getName().strref(); + } else if (auto fused_name = llvm::dyn_cast(loc)) { + for (auto sub_loc : fused_name.getLocations()) { + if (auto named_sub_loc = llvm::dyn_cast(sub_loc)) { + return named_sub_loc.getName().strref(); + } + } + } + return llvm::StringRef(""); + }; + + return CreateImportQuantStatsPass(get_name_func, stats_str); +} + +// Registers this pass with default values, only for test +static PassRegistration pass([] { + return CreateImportQuantStatsPassForTFControlDialect(quantize_stats); +}); + +} // namespace quant +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/.clang-format b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/.clang-format new file mode 100644 index 0000000000000000000000000000000000000000..b48f67accff75864d2e6e752ef616e94bdff5559 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/.clang-format @@ -0,0 +1,2 @@ +BasedOnStyle: LLVM +AlwaysBreakTemplateDeclarations: Yes diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/.clang-tidy b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/.clang-tidy new file mode 100644 index 0000000000000000000000000000000000000000..f1baa22da3993c0016bbd95f6300f78efd70eab8 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/.clang-tidy @@ -0,0 +1,76 @@ +Checks: > + # LINT.IfChange(checks) + -*,clang-diagnostic-*,llvm-*,misc-*, + -misc-unused-parameters, + -misc-non-private-member-variables-in-classes, + -misc-no-recursion, + bugprone-argument-comment, + bugprone-assert-side-effect, + bugprone-branch-clone, + bugprone-copy-constructor-init, + bugprone-dangling-handle, + bugprone-dynamic-static-initializers, + bugprone-macro-parentheses, + bugprone-macro-repeated-side-effects, + bugprone-misplaced-widening-cast, + bugprone-move-forwarding-reference, + bugprone-multiple-statement-macro, + bugprone-suspicious-semicolon, + bugprone-swapped-arguments, + bugprone-terminating-continue, + bugprone-unused-raii, + bugprone-unused-return-value, + misc-redundant-expression, + misc-static-assert, + misc-unused-parameters, + misc-unused-using-decls, + modernize-use-bool-literals, + modernize-loop-convert, + modernize-make-unique, + modernize-raw-string-literal, + modernize-use-equals-default, + modernize-use-default-member-init, + modernize-use-emplace, + modernize-use-nullptr, + modernize-use-override, + modernize-use-using, + performance-for-range-copy, + performance-implicit-conversion-in-loop, + performance-inefficient-algorithm, + performance-inefficient-vector-operation, + performance-move-const-arg, + performance-no-automatic-move, + performance-trivially-destructible, + performance-unnecessary-copy-initialization, + performance-unnecessary-value-param, + readability-avoid-const-params-in-decls, + readability-const-return-type, + readability-container-size-empty, + readability-identifier-naming, + readability-inconsistent-declaration-parameter-name, + readability-misleading-indentation, + readability-redundant-control-flow, + readability-simplify-boolean-expr, + readability-simplify-subscript-expr, + readability-use-anyofallof + # LINT.ThenChange(METADATA:checks) + +CheckOptions: + # LINT.IfChange(check-options) + - key: readability-identifier-naming.ClassCase + value: CamelCase + - key: readability-identifier-naming.EnumCase + value: CamelCase + - key: readability-identifier-naming.FunctionCase + value: camelBack + - key: readability-identifier-naming.MemberCase + value: camelBack + - key: readability-identifier-naming.ParameterCase + value: camelBack + - key: readability-identifier-naming.UnionCase + value: CamelCase + - key: readability-identifier-naming.VariableCase + value: camelBack + - key: readability-identifier-naming.IgnoreMainLikeFunctions + value: 1 + # LINT.ThenChange(METADATA:check-options) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..5f8fbda712fee5bde9e1deb7614d9cb442152a1d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/BUILD @@ -0,0 +1,88 @@ +load("@llvm-project//mlir:tblgen.bzl", "gentbl_cc_library", "td_library") +load("//tensorflow:tensorflow.default.bzl", "get_compatible_with_portable") +load("//tensorflow/core/platform:rules_cc.bzl", "cc_library") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + default_visibility = ["//visibility:public"], + licenses = ["notice"], +) + +td_library( + name = "QuantizationOpsTdFiles", + srcs = [ + "QuantOps.td", + "QuantOpsBase.td", + ], + compatible_with = get_compatible_with_portable(), + deps = [ + "@llvm-project//mlir:InferTypeOpInterfaceTdFiles", + "@llvm-project//mlir:OpBaseTdFiles", + "@llvm-project//mlir:QuantizationOpsTdFiles", + "@llvm-project//mlir:SideEffectInterfacesTdFiles", + ], +) + +gentbl_cc_library( + name = "QuantOpsIncGen", + compatible_with = get_compatible_with_portable(), + tbl_outs = { + "QuantOps.h.inc": ["-gen-op-decls"], + "QuantOps.cc.inc": ["-gen-op-defs"], + "QuantOpsDialect.h.inc": [ + "-gen-dialect-decls", + "-dialect=quantfork", + ], + "QuantOpsDialect.cc.inc": [ + "-gen-dialect-defs", + "-dialect=quantfork", + ], + }, + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "QuantOps.td", + deps = [":QuantizationOpsTdFiles"], +) + +gentbl_cc_library( + name = "QuantPassIncGen", + compatible_with = get_compatible_with_portable(), + tbl_outs = {"Passes.h.inc": [ + "-gen-pass-decls", + "-name=quantfork", + ]}, + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "Passes.td", + deps = ["@llvm-project//mlir:PassBaseTdFiles"], +) + +cc_library( + name = "QuantOps", + srcs = [ + "ConvertConst.cc", + "ConvertSimQuant.cc", + "QuantOps.cc", + "QuantizeUtils.cc", + ], + hdrs = [ + "Passes.h", + "QuantOps.h", + "QuantizeUtils.h", + ], + compatible_with = get_compatible_with_portable(), + deps = [ + ":QuantOpsIncGen", + ":QuantPassIncGen", + "//tensorflow/compiler/mlir/quantization/common/ir:QuantOps", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:BytecodeOpInterface", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:InferTypeOpInterface", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:SideEffectInterfaces", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/ConvertConst.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/ConvertConst.cc new file mode 100644 index 0000000000000000000000000000000000000000..16d1816fc6a18e72caa9e3fa82fea493e0867c95 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/ConvertConst.cc @@ -0,0 +1,122 @@ +/* Copyright 2022 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 "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Dialect/Quant/IR/QuantTypes.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/Matchers.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/quantization/ir/Passes.h" +#include "tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.h" +#include "tensorflow/compiler/mlir/lite/quantization/ir/QuantizeUtils.h" + +using namespace mlir; +using namespace mlir::quantfork; + +using mlir::quant::QuantizedType; + +namespace { +#define GEN_PASS_DEF_QUANTCONVERTCONST +#include "tensorflow/compiler/mlir/lite/quantization/ir/Passes.h.inc" + +struct ConvertConstPass : public impl::QuantConvertConstBase { + void runOnOperation() override; +}; + +struct QuantizedConstRewrite : public OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(QuantizeCastOp qbarrier, + PatternRewriter &rewriter) const override; +}; + +} // namespace + +/// Matches a [constant] -> [qbarrier] where the qbarrier results type is +/// quantized and the operand type is quantizable. + +LogicalResult QuantizedConstRewrite::matchAndRewrite( + QuantizeCastOp qbarrier, PatternRewriter &rewriter) const { + Attribute value; + + // Is the operand a constant? + if (!matchPattern(qbarrier.getArg(), m_Constant(&value))) { + return failure(); + } + + // Does the qbarrier convert to a quantized type. This will not be true + // if a quantized type has not yet been chosen or if the cast to an equivalent + // storage type is not supported. + Type qbarrierResultType = qbarrier.getResult().getType(); + QuantizedType quantizedElementType = + QuantizedType::getQuantizedElementType(qbarrierResultType); + if (!quantizedElementType) { + return failure(); + } + if (!QuantizedType::castToStorageType(qbarrierResultType)) { + return failure(); + } + + // Is the operand type compatible with the expressed type of the quantized + // type? This will not be true if the qbarrier is superfluous (converts + // from and to a quantized type). + if (!quantizedElementType.isCompatibleExpressedType( + qbarrier.getArg().getType())) { + return failure(); + } + + // Is the constant value a type expressed in a way that we support? + if (!mlir::isa(value)) { + return failure(); + } + + Type newConstValueType; + auto newConstValue = + quantizeAttr(value, quantizedElementType, newConstValueType); + if (!newConstValue) { + return failure(); + } + + // When creating the new const op, use a fused location that combines the + // original const and the qbarrier that led to the quantization. + auto fusedLoc = rewriter.getFusedLoc( + {qbarrier.getArg().getDefiningOp()->getLoc(), qbarrier.getLoc()}); + auto newConstOp = arith::ConstantOp::create( + rewriter, fusedLoc, newConstValueType, cast(newConstValue)); + rewriter.replaceOpWithNewOp(qbarrier, qbarrier.getType(), + newConstOp); + return success(); +} + +void ConvertConstPass::runOnOperation() { + RewritePatternSet patterns(&getContext()); + auto func = getOperation(); + auto *context = &getContext(); + patterns.add(context); + (void)applyPatternsGreedily(func, std::move(patterns)); +} + +std::unique_ptr> +mlir::quantfork::createConvertConstPass() { + return std::make_unique(); +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/ConvertSimQuant.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/ConvertSimQuant.cc new file mode 100644 index 0000000000000000000000000000000000000000..7b7faef30d2ce18e8a00aea3ff2bebde54a7564b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/ConvertSimQuant.cc @@ -0,0 +1,158 @@ +/* Copyright 2022 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 "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Dialect/Quant/IR/QuantTypes.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/Types.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/quantization/ir/Passes.h" +#include "tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.h" +#include "tensorflow/compiler/mlir/quantization/common/ir/FakeQuantSupport.h" +#include "tensorflow/compiler/mlir/quantization/common/ir/UniformSupport.h" + +namespace mlir::quantfork { + +#define GEN_PASS_DEF_QUANTCONVERTSIMULATEDQUANT +#include "tensorflow/compiler/mlir/lite/quantization/ir/Passes.h.inc" + +struct ConvertSimulatedQuantPass + : public impl::QuantConvertSimulatedQuantBase { + void runOnOperation() override; +}; + +/// Base class rewrites ConstFakeQuant into a qbarrier/dbarrier pair. +template +class FakeQuantRewrite : public OpRewritePattern { + public: + using OpRewritePattern::OpRewritePattern; + + FakeQuantRewrite(MLIRContext *ctx, bool *hadFailure) + : OpRewritePattern(ctx), hadFailure(hadFailure) {} + + LogicalResult matchAndRewrite(FakeQuantOp op, + PatternRewriter &rewriter) const override { + // TODO: If this pattern comes up more frequently, consider adding core + // support for failable rewrites. + if (failableRewrite(op, rewriter)) { + *hadFailure = true; + return failure(); + } + return success(); + } + + private: + bool *hadFailure; + + bool failableRewrite(FakeQuantOp op, PatternRewriter &rewriter) const { + auto converter = + mlir::quant::ir::ExpressedToQuantizedConverter::forInputType( + op.getType()); + if (!converter) { + return (op.emitError("unsupported quantized type conversion"), true); + } + + quant::QuantizedType elementType = + static_cast(this) + ->convertFakeQuantAttrsToType(op, converter.expressed_type); + + if (!elementType) { + // Note that the fakeQuantAttrsToType will have emitted the error. + return true; + } + + Type quantizedType = converter.convert(elementType); + assert(quantizedType && + "Converter accepted a type that it did not convert"); + + // TODO: Map to a qbarrier with an attribute like [Forced] to signal that + // this is a forced/hard-coded constraint. + auto qbarrier = QuantizeCastOp::create(rewriter, op.getLoc(), quantizedType, + op.getInputs()); + rewriter.replaceOpWithNewOp(op, converter.input_type, + qbarrier.getResult()); + + return false; + } +}; + +class ConstFakeQuantRewrite + : public FakeQuantRewrite { + public: + using BaseRewrite = FakeQuantRewrite; + + ConstFakeQuantRewrite(MLIRContext *ctx, bool *hadFailure) + : BaseRewrite(ctx, hadFailure) {} + + quant::QuantizedType convertFakeQuantAttrsToType(ConstFakeQuant fqOp, + Type expressedType) const { + return fakeQuantAttrsToType( + fqOp.getLoc(), fqOp.getNumBits(), fqOp.getMin().convertToFloat(), + fqOp.getMax().convertToFloat(), fqOp.getNarrowRange(), expressedType, + fqOp.getIsSigned()); + } +}; + +class ConstFakeQuantPerAxisRewrite + : public FakeQuantRewrite { + public: + using BaseRewrite = + FakeQuantRewrite; + + ConstFakeQuantPerAxisRewrite(MLIRContext *ctx, bool *hadFailure) + : BaseRewrite(ctx, hadFailure) {} + + quant::QuantizedType convertFakeQuantAttrsToType(ConstFakeQuantPerAxis fqOp, + Type expressedType) const { + SmallVector min, max; + min.reserve(fqOp.getMin().size()); + max.reserve(fqOp.getMax().size()); + for (auto m : fqOp.getMin()) + min.push_back(cast(m).getValueAsDouble()); + for (auto m : fqOp.getMax()) + max.push_back(cast(m).getValueAsDouble()); + + return fakeQuantAttrsToType(fqOp.getLoc(), fqOp.getNumBits(), + fqOp.getAxis(), min, max, fqOp.getNarrowRange(), + expressedType, fqOp.getIsSigned()); + } +}; + +void ConvertSimulatedQuantPass::runOnOperation() { + bool hadFailure = false; + auto func = getOperation(); + RewritePatternSet patterns(func.getContext()); + auto *ctx = func.getContext(); + patterns.add( + ctx, &hadFailure); + (void)applyPatternsGreedily(func, std::move(patterns)); + if (hadFailure) signalPassFailure(); +} + +std::unique_ptr> createConvertSimulatedQuantPass() { + return std::make_unique(); +} + +} // namespace mlir::quantfork diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/Passes.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/Passes.h new file mode 100644 index 0000000000000000000000000000000000000000..0b8667c55576bf46de5b28886928f8cfcde639a8 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/Passes.h @@ -0,0 +1,57 @@ +/* Copyright 2022 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. +==============================================================================*/ +// +// This file defines all of the passes owned by the quantization dialect. As +// things mature, it is expected that passes specific to certain frontend or +// backend dialects will move to those dialects directly. For now, they are +// incubated here. +// +//===----------------------------------------------------------------------===// + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_IR_PASSES_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_IR_PASSES_H_ + +#include "mlir/Pass/Pass.h" // from @llvm-project + +namespace mlir { +namespace func { +class FuncOp; +} // namespace func + +namespace quantfork { + +/// Creates a pass that converts quantization simulation operations (i.e. +/// FakeQuant and those like it) to casts into/out of supported QuantizedTypes. +std::unique_ptr> createConvertSimulatedQuantPass(); + +/// Creates a pass that converts constants followed by a qbarrier to a +/// constant whose value is quantized. This is typically one of the last +/// passes done when lowering to express actual quantized arithmetic in a +/// low level representation. Because it modifies the constant, it is +/// destructive and cannot be undone. +std::unique_ptr> createConvertConstPass(); + +//===----------------------------------------------------------------------===// +// Registration +//===----------------------------------------------------------------------===// + +/// Generate the code for registering passes. +#define GEN_PASS_REGISTRATION +#include "tensorflow/compiler/mlir/lite/quantization/ir/Passes.h.inc" + +} // namespace quantfork +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_IR_PASSES_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/Passes.td b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/Passes.td new file mode 100644 index 0000000000000000000000000000000000000000..6617147b89ee757fa762fb3fe2330fb942b32dc0 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/Passes.td @@ -0,0 +1,34 @@ +/* Copyright 2022 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. +==============================================================================*/ + +#ifndef QUANT_FORK_PASSES +#define QUANT_FORK_PASSES + +include "mlir/Pass/PassBase.td" + +def QuantConvertConst : Pass<"quant-convert-const", "func::FuncOp"> { + let summary = "Converts constants followed by qbarrier to actual quantized " + "values"; + let constructor = "mlir::quantfork::createConvertConstPass()"; +} + +def QuantConvertSimulatedQuant + : Pass<"quant-convert-simulated-quantization", "func::FuncOp"> { + let summary = "Converts training-time simulated quantization ops to " + "corresponding quantize/dequantize casts"; + let constructor = "mlir::quantfork::createConvertSimulatedQuantPass()"; +} + +#endif // QUANT_FORK_PASSES diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.cc new file mode 100644 index 0000000000000000000000000000000000000000..e345b3e455ebd6e68e67f8d984e03ba1883d873e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.cc @@ -0,0 +1,143 @@ +/* Copyright 2022 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/compiler/mlir/lite/quantization/ir/QuantOps.h" + +#include +#include +#include +#include + +#include "llvm/ADT/STLExtras.h" +#include "mlir/Dialect/Quant/IR/QuantTypes.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/OpDefinition.h" // from @llvm-project +#include "mlir/IR/Types.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project + +using namespace mlir; +using namespace mlir::quantfork; + +using mlir::quant::QuantizedType; + +#include "tensorflow/compiler/mlir/lite/quantization/ir/QuantOpsDialect.cc.inc" + +void QuantizationForkDialect::initialize() { + addOperations< +#define GET_OP_LIST +#include "tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.cc.inc" + >(); +} + +OpFoldResult StorageCastOp::fold(FoldAdaptor) { + // Matches x -> [scast -> scast] -> y, replacing the second scast with the + // value of x if the casts invert each other. + auto srcScastOp = getArg().getDefiningOp(); + if (!srcScastOp || srcScastOp.getArg().getType() != getType()) + return OpFoldResult(); + return srcScastOp.getArg(); +} + +/// The quantization specification should match the expressed type. +static bool isValidQuantizationSpec(Attribute quantSpec, Type expressed) { + if (auto typeAttr = mlir::dyn_cast(quantSpec)) { + Type spec = typeAttr.getValue(); + if (mlir::isa(spec)) return false; + + // The spec should be either a quantized type which is compatible to the + // expressed type, or a primitive type which is as same as the + // (element type of) the expressed type. + if (auto quantizedType = mlir::dyn_cast(spec)) + return quantizedType.isCompatibleExpressedType(expressed); + + if (auto tensorType = mlir::dyn_cast(expressed)) + return spec == tensorType.getElementType(); + + if (auto vectorType = mlir::dyn_cast(expressed)) + return spec == vectorType.getElementType(); + } + return false; +} + +LogicalResult QuantizeRegionOp::verify() { + // There are specifications for both inputs and outputs. + if (getNumOperands() != getInputSpecs().size() || + getNumResults() != getOutputSpecs().size()) + return emitOpError( + "has unmatched operands/results number and spec attributes number"); + + // Verify that quantization specifications are valid. + for (auto input : llvm::zip(getOperandTypes(), getInputSpecs())) { + Type inputType = std::get<0>(input); + Attribute inputSpec = std::get<1>(input); + if (!isValidQuantizationSpec(inputSpec, inputType)) { + return emitOpError() << "has incompatible specification " << inputSpec + << " and input type " << inputType; + } + } + + for (auto result : llvm::zip(getResultTypes(), getOutputSpecs())) { + Type outputType = std::get<0>(result); + Attribute outputSpec = std::get<1>(result); + if (!isValidQuantizationSpec(outputSpec, outputType)) { + return emitOpError() << "has incompatible specification " << outputSpec + << " and output type " << outputType; + } + } + return success(); +} + +LogicalResult StatisticsOp::verify() { + auto tensorArg = mlir::dyn_cast(getArg().getType()); + if (!tensorArg) return emitOpError("arg needs to be tensor type."); + + // Verify layerStats attribute. + { + auto layerStatsType = getLayerStats().getShapedType(); + if (!mlir::isa(layerStatsType.getElementType())) { + return emitOpError("layerStats must have a floating point element type"); + } + if (layerStatsType.getRank() != 1 || layerStatsType.getDimSize(0) != 2) { + return emitOpError("layerStats must have shape [2]"); + } + } + // Verify axisStats (optional) attribute. + if (getAxisStats()) { + if (!getAxis()) return emitOpError("axis must be specified for axisStats"); + + auto shape = tensorArg.getShape(); + auto argSliceSize = + std::accumulate(std::next(shape.begin(), *getAxis()), shape.end(), 1, + std::multiplies()); + + auto axisStatsType = getAxisStats()->getShapedType(); + if (!mlir::isa(axisStatsType.getElementType())) { + return emitOpError("axisStats must have a floating point element type"); + } + if (axisStatsType.getRank() != 2 || axisStatsType.getDimSize(1) != 2 || + axisStatsType.getDimSize(0) != argSliceSize) { + return emitOpError( + "axisStats must have shape [N,2] " + "where N = the slice size defined by the axis dim"); + } + } + return success(); +} + +#define GET_OP_CLASSES +#include "tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.cc.inc" diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.h new file mode 100644 index 0000000000000000000000000000000000000000..e2d3356aff326e8064815f5eeae28c1feec27307 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.h @@ -0,0 +1,34 @@ +/* Copyright 2022 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_IR_QUANTOPS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_IR_QUANTOPS_H_ + +#include "llvm/Support/MathExtras.h" +#include "mlir/Bytecode/BytecodeOpInterface.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/Dialect.h" // from @llvm-project +#include "mlir/IR/OpDefinition.h" // from @llvm-project +#include "mlir/IR/Types.h" // from @llvm-project +#include "mlir/Interfaces/InferTypeOpInterface.h" // from @llvm-project +#include "mlir/Interfaces/SideEffectInterfaces.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/quantization/ir/QuantOpsDialect.h.inc" +#define GET_OP_CLASSES + +#include "tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.h.inc" + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_IR_QUANTOPS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.td b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.td new file mode 100644 index 0000000000000000000000000000000000000000..ebc0fbb81ec8f1023e13ccde37e79876214a2133 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.td @@ -0,0 +1,294 @@ +/* Copyright 2022 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. +==============================================================================*/ +// +//===----------------------------------------------------------------------===// +// +// This is the operation definition file for Quantization. +// +//===----------------------------------------------------------------------===// + +#ifndef QUANT_FORK_OPS +#define QUANT_FORK_OPS + +include "tensorflow/compiler/mlir/lite/quantization/ir/QuantOpsBase.td" +include "mlir/Dialect/Quant/IR/QuantBase.td" +include "mlir/Interfaces/InferTypeOpInterface.td" +include "mlir/Interfaces/SideEffectInterfaces.td" + +class quant_TypedPrimitiveOrContainer : + Type.predicate, + VectorOfNonZeroRankOf<[etype]>.predicate]>, + "primitive/tensor/vector of " # etype.summary>; + +// A primitive type that can represent a real value. This is either a +// floating point value or a quantized type. +def quant_RealPrimitiveType : + Type, + "real valued primitive (float or quantized type)">; + +// A primitive type that can represent a storage value. This is either an +// integer or quantized type. +def quant_StoragePrimitiveType : + Type, + "quantized storage primitive (integer or quantized type)">; + +// A primitive or container of RealPrimitiveType. +def quant_RealValueType : + quant_TypedPrimitiveOrContainer; + +// A primitive or container of StoragePrimitiveType. +def quant_StorageValueType : + quant_TypedPrimitiveOrContainer; + +// Either a real valued or storage primitive or container type. +def quant_RealOrStorageValueType : + Type, + "real valued or storage primitive or container type">; + +//===----------------------------------------------------------------------===// +// Base classes +//===----------------------------------------------------------------------===// + +class quantfork_Op traits> : + Op; + +//===----------------------------------------------------------------------===// +// Quantization casts +//===----------------------------------------------------------------------===// +// A QuantizeCast (qcast) represents a potential type shift from a quantizable +// type to a quantized type. +// +// At runtime, a qcast will apply the transformation expressed by its +// operand and result type. For flexibility during transformation, it is also +// possible to have a qcast that performs no transformation (both its +// operand and result type are quantizable). +// +// A qcast will typically originate from either: +// a) An expressed or implied constraint in the source dialect which signals +// that a certain level of quantization is possible or required. +// b) An inference made by a quantization algorithm indicating that a +// quantized representation may be acceptable. +// +// Especially early in transformation, it is common to have pairs of +// qcast/dcast at points where a transition to a quantized type is +// required. In addition, it is also common to have an identity qcast +// (where the operand and result type are not quantized) at all points where +// it is legal to use a quantized representation (but is not known to be +// acceptable). +def quantfork_QuantizeCastOp : quantfork_Op<"qcast", [Pure]> { + let arguments = (ins quant_RealValueType:$arg); + let results = (outs quant_RealValueType); +} + +// A DequantizeCast op (dcast) represents the inverse of a qcast, +// converting back from a quantized to quantizable (expressed) type. +// +// Like qcasts, a dcast is allowed to have both its operand and result +// as non quantized types. This facilitates transformations and marks edges +// where the computation must be carried out in the expressed type. +// +// Especially early in transformation, it is common to have dcasts on +// all operands to ops that must operate with the expressed type (typically +// math ops prior to lowering to target-specific, quantized kernels). +def quantfork_DequantizeCastOp : quantfork_Op<"dcast", [Pure]> { + let arguments = (ins quant_RealValueType:$arg); + let results = (outs quant_RealValueType); +} + +// A StorageCast (scast) represents a cast from or to a type based on the +// storage type and a type based on a corresponding quantized type. +// +// This op exists to ensure type coherency for between parts of the computation +// which are operating directly on an underlying storage type and those which +// operate on quantized values. +// +// Examples from storage to quantized type: +// i8 -> !quant<"uniform[i8:f32]{1.0}"> +// tensor<4xi8> -> tensor<4x!quant<"uniform[i8:f32]{1.0}">> +// vector<4xi8> -> vector<4x!quant<"uniform[i8:f32]{1.0}">> +def quantfork_StorageCastOp : quantfork_Op<"scast", [Pure]> { + let arguments = (ins quant_RealOrStorageValueType:$arg); + let results = (outs quant_RealOrStorageValueType); + let hasFolder = 1; +} + +// A QuantizeRegion (region) represents a quantization unit which wraps +// high-precision ops with quantization specifications for all the inputs +// and outputs. Some quantization specifications can be undetermined and +// derived from other ports by the target specification of the kernel. +def quantfork_QuantizeRegionOp : quantfork_Op<"region", [ + Pure, + IsolatedFromAbove, + SingleBlockImplicitTerminator<"ReturnOp">]> { + let summary = [{ + The `region` operation wraps high-precision ops as a logical low-precision + quantized kernel. + }]; + + let arguments = (ins Variadic:$inputs, + TypeArrayAttr:$input_specs, + TypeArrayAttr:$output_specs, + StrAttr:$logical_kernel); + let results = (outs Variadic:$outputs); + let regions = (region SizedRegion<1>:$body); + let hasVerifier = 1; +} + +def quantfork_ReturnOp : quantfork_Op<"return", [Terminator]> { + let summary = [{ + The `return` operation terminates a quantize region and returns values. + }]; + + let arguments = (ins Variadic:$results); +} + +//===----------------------------------------------------------------------===// +// Training integration and instrumentation ops +//===----------------------------------------------------------------------===// + +def quantfork_ConstFakeQuant : quantfork_Op<"const_fake_quant", + [SameOperandsAndResultType, Pure]> { + let summary = [{ + Simulates the effect of uniform quantization with const range. + }]; + + let description = [{ + Given a const min, max, num_bits and narrow_range attribute, applies the + same uniform quantization simulation as is done by the TensorFlow + fake_quant_with_min_max_args op. See the fakeQuantAttrsToType() utility + method and the quant-convert-simulated-quantization pass for further details. + }]; + + let arguments = (ins + F32Tensor:$inputs, + F32Attr:$min, + F32Attr:$max, + // The bitwidth of the quantization; between 2 and 16, inclusive. + I64Attr:$num_bits, + // Quantization range starts from 0 or 1; starts from 1 if true. + DefaultValuedOptionalAttr:$narrow_range, + // The sign of the quantization. + DefaultValuedOptionalAttr:$is_signed + ); + + let results = (outs + F32Tensor:$outputs + ); +} + +def quantfork_ConstFakeQuantPerAxis : quantfork_Op<"const_fake_quant_per_axis", + [SameOperandsAndResultType, Pure]> { + let summary = [{ + Simulates the effect of per axis uniform quantization with const range. + }]; + + let description = [{ + Given a const min, max, num_bits and narrow_range attribute, applies the + same per axis uniform quantization simulation as is done by the TensorFlow + fake_quant_with_min_max_vars_per_channel op. See the fakeQuantAttrsToType() + utility method and the quant-convert-simulated-quantization pass for further + details. + }]; + + let arguments = (ins + F32Tensor:$inputs, + F32ArrayAttr:$min, + F32ArrayAttr:$max, + // The quantized dimension of the inputs tensor. + I64Attr:$axis, + // The bitwidth of the quantization; between 2 and 16, inclusive. + I64Attr:$num_bits, + // Quantization range starts from 0 or 1; starts from 1 if true. + DefaultValuedOptionalAttr:$narrow_range, + // The sign of the quantization. + DefaultValuedOptionalAttr:$is_signed + ); + + let results = (outs + F32Tensor:$outputs + ); +} + +def quantfork_StatisticsRefOp : quantfork_Op<"stats_ref", [SameOperandsAndResultType]> { + let summary = "Indicates that statistics are resolved by reference."; + + let description = [{ + This op acts as an identity that, when encountered at runtime, should result + in statistics being collected about about the value of its operand/result. + Such statistics will be stored with the provided key, allowing this node + to later be converted to a 'stats' op if statistics with that key have been + encountered. + }]; + + let arguments = (ins + quant_RealValueType:$arg, + StrAttr:$statsKey + ); + let results = (outs quant_RealValueType); +} + +def quantfork_StatisticsOp : quantfork_Op<"stats", [SameOperandsAndResultType]> { + let summary = "Identity op which associates statistics with the value."; + + let description = [{ + Associates statistics about the runtime ranges of values observed for + evaluations of this node. + + Statistics about the entire type are reported in the 'layerStats' attribute + and those for each axis, in the (optional) `axisStats` attribute. The + interpretation of each is determined by the last dimension of its shape. + Currently, only dim=2 is supported, which is interpreted as [min, max]. + + `layerStats` must be a rank 1 tensor: [2] + `axisStats` must be a rank 2 tensor: [N, 2], where N=the slice size + splitted by the `axis` dimension. For example: + + ``` + , axis=3 => N=2 + , axis=2 => N=6 + ``` + }]; + + let arguments = (ins + quant_RealValueType:$arg, + ElementsAttr:$layerStats, + OptionalAttr:$axisStats, + OptionalAttr:$axis); + let results = (outs quant_RealValueType); + let hasVerifier = 1; +} + +def quantfork_CoupledRefOp : quantfork_Op<"coupled_ref", [SameOperandsAndResultType]> { + let summary = [{ + Indicates that one point of the computation is coupled to another. + }]; + + let description = [{ + Ordinarily, relationships between ops for the purposes of determining + compatible quantized types is explicit based on the use-def chain. However, + in some situations, a use may be separated from its def by arbitrary + external connections. In such a case, during analysis, all coupled_ref + nodes in a module which share a coupledKey will be considered to be + directly connected as via an identity op for the purpose of type inference. + }]; + + let arguments = (ins + quant_RealValueType:$arg, + StrAttr:$coupledKey); + let results = (outs quant_RealValueType); +} + +#endif // QUANT_FORK_OPS diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/QuantOpsBase.td b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/QuantOpsBase.td new file mode 100644 index 0000000000000000000000000000000000000000..6345a569b52236b192a890825acc7b5669c2fd94 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/QuantOpsBase.td @@ -0,0 +1,32 @@ +/* Copyright 2022 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. +==============================================================================*/ +// +//===----------------------------------------------------------------------===// +// +// Predicates for types in the Quantization dialect. +// +//===----------------------------------------------------------------------===// + +#ifndef QUANT_FORK_BASE +#define QUANT_FORK_BASE + +include "mlir/IR/OpBase.td" + +def QuantizationFork_Dialect : Dialect { + let name = "quantfork"; + let cppNamespace = "::mlir::quantfork"; +} + +#endif // QUANT_FORK_BASE diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/QuantizeUtils.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/QuantizeUtils.cc new file mode 100644 index 0000000000000000000000000000000000000000..1fed408f92c8d134d3d5dc4b09dda1b041aac2eb --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/QuantizeUtils.cc @@ -0,0 +1,147 @@ +/* Copyright 2022 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/compiler/mlir/lite/quantization/ir/QuantizeUtils.h" + +#include "mlir/Dialect/Quant/IR/QuantTypes.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "tensorflow/compiler/mlir/quantization/common/ir/UniformSupport.h" + +using namespace mlir; +using namespace mlir::quantfork; + +/// Converts a possible primitive, real expressed value attribute to a +/// corresponding storage attribute (typically FloatAttr -> IntegerAttr). +/// quantizedElementType is the QuantizedType that describes the expressed +/// origValue. +/// Returns a converter Attribute or nullptr if conversion is not possible. +static Attribute convertPrimitiveValueAttr( + Attribute origRealValue, quant::QuantizedType quantizedElementType, + const mlir::quant::ir::UniformQuantizedValueConverter &converter, + Type &outConvertedType) { + if (mlir::isa(origRealValue)) { + FloatAttr floatAttr = mlir::cast(origRealValue); + outConvertedType = quantizedElementType.getStorageType(); + return IntegerAttr::get(quantizedElementType.getStorageType(), + converter.quantizeFloatToInt(floatAttr.getValue())); + } + + return nullptr; +} + +/// Converts a real expressed DenseFPElementsAttr to a corresponding +/// DenseElementsAttr (typically DenseIntElementsAttr) containing quantized +/// storage values assuming the given quantizedElementType and converter. +static DenseElementsAttr convertDenseFPElementsAttr( + DenseFPElementsAttr realFPElementsAttr, + quant::QuantizedType quantizedElementType, + const mlir::quant::ir::UniformQuantizedValueConverter &converter) { + return realFPElementsAttr.mapValues( + quantizedElementType.getStorageType(), + [&converter](const APFloat &realVal) { + return converter.quantizeFloatToInt(realVal); + }); +} + +/// Converts a real expressed SplatElementsAttr to a corresponding +/// SplatElementsAttr containing quantized storage values assuming the given +/// quantizedElementType and converter. +static SparseElementsAttr convertSparseElementsAttr( + SparseElementsAttr realSparseAttr, + quant::QuantizedType quantizedElementType, + const mlir::quant::ir::UniformQuantizedValueConverter &converter) { + DenseElementsAttr realDenseAttr = realSparseAttr.getValues(); + if (!mlir::isa(realDenseAttr)) { + return nullptr; + } + DenseElementsAttr quantDenseAttr = + convertDenseFPElementsAttr(mlir::cast(realDenseAttr), + quantizedElementType, converter); + if (!quantDenseAttr) { + return nullptr; + } + + // Cast from an expressed-type-based type to storage-type-based type, + // preserving the sparse shape (i.e. tensor<4xf32> -> tensor<4xi8>). + ShapedType newSparseType = mlir::dyn_cast_or_null( + quantizedElementType.castExpressedToStorageType( + realSparseAttr.getType())); + if (!newSparseType) { + return nullptr; + } + return SparseElementsAttr::get(newSparseType, realSparseAttr.getIndices(), + quantDenseAttr); +} + +/// Converts a real expressed Attribute to a corresponding Attribute containing +/// quantized storage values assuming the given uniform quantizedElementType and +/// converter. +Attribute mlir::quantfork::quantizeAttrUniform( + Attribute realValue, quant::UniformQuantizedType quantizedElementType, + const mlir::quant::ir::UniformQuantizedValueConverter &converter, + Type &outConvertedType) { + // Fork to handle different variants of constants supported. + if (mlir::isa(realValue)) { + // Dense tensor or vector constant. + auto converted = + convertDenseFPElementsAttr(mlir::cast(realValue), + quantizedElementType, converter); + outConvertedType = converted.getType(); + return converted; + } + if (mlir::isa(realValue)) { + // Sparse tensor or vector constant. + auto converted = + convertSparseElementsAttr(mlir::cast(realValue), + quantizedElementType, converter); + outConvertedType = converted.getType(); + return converted; + } + // Nothing else matched: try to convert a primitive. + return convertPrimitiveValueAttr(realValue, quantizedElementType, converter, + outConvertedType); +} + +/// Convert an attribute from a type based on +/// quantizedElementType.getExpressedType() to one based on +/// quantizedElementType.getStorageType(). +/// Returns nullptr if the conversion is not supported. +/// On success, stores the converted type in outConvertedType. +Attribute mlir::quantfork::quantizeAttr( + Attribute realValue, quant::QuantizedType quantizedElementType, + Type &outConvertedType) { + if (auto uniformQuantized = + mlir::dyn_cast(quantizedElementType)) { + mlir::quant::ir::UniformQuantizedValueConverter converter(uniformQuantized); + return quantizeAttrUniform(realValue, uniformQuantized, converter, + outConvertedType); + } + if (auto uniformQuantizedPerAxis = + mlir::dyn_cast( + quantizedElementType)) { + mlir::quant::ir::UniformQuantizedPerAxisValueConverter converter( + uniformQuantizedPerAxis); + auto converted = converter.convert(realValue); + // TODO: why we need this outConvertedType? remove it? + if (converted) { + outConvertedType = converted.getType(); + } + return converted; + } + return nullptr; +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/QuantizeUtils.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/QuantizeUtils.h new file mode 100644 index 0000000000000000000000000000000000000000..1b1e09789021d8e8bd775795448c625342d8e2e1 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/ir/QuantizeUtils.h @@ -0,0 +1,73 @@ +/* Copyright 2022 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_IR_QUANTIZEUTILS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_IR_QUANTIZEUTILS_H_ + +namespace mlir { +class Attribute; +class Type; + +namespace quant { +class QuantizedType; +class UniformQuantizedType; +namespace ir { +class UniformQuantizedValueConverter; +} // namespace ir +} // namespace quant +namespace quantfork { + +/// Converts an attribute from a type based on +/// quantizedElementType.getExpressedType() to one based on +/// quantizedElementType.getStorageType(), where quantizedElementType is as from +/// QuantizedType::getQuantizedElementType(). +/// Returns nullptr if the conversion is not supported. On success, stores the +/// converted type in outConvertedType. +/// +/// Examples: +/// 1. realValue is a primitive value attribute: +/// (realValue: FloatAttr, quantizedElementType: UniformQuantizedType[i8:f32]) +/// -> (IntegerAttr, outConvertedType: i8) +/// 2. realValue is an elements attribute: +/// (realValue: DenseElementsAttr[tensor<2x2xf32>], +/// quantizedElementType: UniformQuantizedType[i8:f32]) +/// -> (DenseElementsAttr[tensor<2x2xi8>], outConvertedType: tensor<2x2xi8>) +Attribute quantizeAttr(Attribute realValue, + quant::QuantizedType quantizedElementType, + Type &outConvertedType); + +/// Converts an attribute from a type based on +/// quantizedElementType.getExpressedType() to one based on +/// quantizedElementType.getStorageType(), where quantizedElementType is as from +/// QuantizedType::getQuantizedElementType() and casted to an +/// UniformQuantizedType. Returns nullptr if the conversion is not supported. On +/// success, stores the converted type in outConvertedType. +/// +/// Examples: +/// 1. realValue is a primitive value attribute: +/// (realValue: FloatAttr, quantizedElementType: UniformQuantizedType[i8:f32]) +/// -> (IntegerAttr, outConvertedType: i8) +/// 2. realValue is an elements attribute: +/// (realValue: DenseElementsAttr[tensor<2x2xf32>], +/// quantizedElementType: UniformQuantizedType[i8:f32]) +/// -> (DenseElementsAttr[tensor<2x2xi8>], outConvertedType: tensor<2x2xi8>) +Attribute quantizeAttrUniform( + Attribute realValue, quant::UniformQuantizedType quantizedElementType, + const mlir::quant::ir::UniformQuantizedValueConverter &converter, + Type &outConvertedType); +} // namespace quantfork +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_IR_QUANTIZEUTILS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..5548abc04941144b876a94c7ab598171dd0c7319 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/BUILD @@ -0,0 +1,231 @@ +load("//tensorflow:tensorflow.bzl", "tf_cc_binary", "tf_cc_test") +load("//tensorflow/core/platform:rules_cc.bzl", "cc_library") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + default_visibility = [ + ":friends", + "//tensorflow:__pkg__", + ], + licenses = ["notice"], +) + +exports_files(glob([ + "testdata/*.bin", +])) + +package_group( + name = "friends", + packages = [ + "//tensorflow/compiler/mlir/lite/...", + "//tensorflow/lite/...", + ], +) + +cc_library( + name = "quantize_model", + srcs = [ + "quantize_model.cc", + ], + hdrs = [ + "quantize_model.h", + ], + deps = [ + "//tensorflow/compiler/mlir/lite:common", + "//tensorflow/compiler/mlir/lite:flatbuffer_translate_lib", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/lite:tf_tfl_passes", + "//tensorflow/compiler/mlir/lite/debug", + "//tensorflow/compiler/mlir/lite/debug:debug_options_proto_cc", + "//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib:quantization_config", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs", + "//tensorflow/compiler/mlir/tensorflow:error_util", + "//tensorflow/core:protos_all_cc", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/log", + "@com_google_absl//absl/status", + "@com_google_absl//absl/strings", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + ], +) + +cc_library( + name = "quantize_weights", + srcs = [ + "quantize_weights.cc", + ], + hdrs = [ + "quantize_weights.h", + ], + deps = [ + "//tensorflow/compiler/mlir/lite:common", + "//tensorflow/compiler/mlir/lite:flatbuffer_translate_lib", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/lite:tf_tfl_passes", + "//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib:quantization_config", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs", + "//tensorflow/compiler/mlir/tensorflow:error_util", + "//tensorflow/core:protos_all_cc", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/log", + "@com_google_absl//absl/status", + "@com_google_absl//absl/strings", + "@flatbuffers//:runtime_cc", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + ], +) + +cc_library( + name = "tfl_to_std", + srcs = [ + "tfl_to_std.cc", + ], + hdrs = [ + "tfl_to_std.h", + "//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib:quantization_utils.h", + ], + deps = [ + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib", + "//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib:quantization_config", + "//tensorflow/compiler/mlir/lite/quantization/ir:QuantOps", + "//tensorflow/compiler/mlir/quantization/common/ir:QuantOps", + "//tensorflow/core:protos_all_cc", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/strings", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:Support", + ], +) + +# Binary to apply quantization on the annotated files. +tf_cc_binary( + name = "tfl_quantizer", + srcs = [ + "tfl_quantizer.cc", + ], + deps = [ + ":quantize_model", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs", + "@com_google_absl//absl/status", + "@llvm-project//llvm:Support", + ], +) + +tf_cc_test( + name = "quantize_model_test", + srcs = ["quantize_model_test.cc"], + args = [ + "--test_model_file=$(location //tensorflow/compiler/mlir/lite/quantization/lite:testdata/single_conv_weights_min_0_max_plus_10.bin)", + ], + data = [ + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/add_with_const_input.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/argmax.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/broadcast_to.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/concat.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/fc.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/fc_qat.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/gather_nd.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/lstm_calibrated.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/lstm_calibrated2.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/lstm_quantized.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/lstm_quantized2.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/maximum.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/minimum.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/mixed.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/mixed16x8.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/multi_input_add_reshape.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/pack.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/single_avg_pool_min_minus_5_max_plus_5.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/single_conv_no_bias.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/single_conv_weights_min_0_max_plus_10.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/single_conv_weights_min_minus_127_max_plus_127.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/single_softmax_min_minus_5_max_plus_5.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/split.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/svdf_calibrated.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/svdf_quantized.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/transpose.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/unidirectional_sequence_lstm_calibrated.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/unidirectional_sequence_lstm_quantized.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/unpack.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/where.bin", + ], + tags = [ + "tflite_not_portable_android", + "tflite_not_portable_ios", + ], + deps = [ + ":quantize_model", + ":test_util", + "//tensorflow/compiler/mlir/lite/core:absl_error_model_builder", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs", + "//tensorflow/compiler/mlir/lite/schema:schema_utils", + "//tensorflow/core:framework_internal", + "//tensorflow/core:lib", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/status", + "@com_google_googletest//:gtest", + "@flatbuffers", + "@xla//xla/tsl/lib/core:status_test_util", + ], +) + +tf_cc_test( + name = "quantize_weights_test", + srcs = ["quantize_weights_test.cc"], + args = [ + "--test_model_file=$(location //tensorflow/compiler/mlir/lite/quantization/lite:testdata/single_conv_weights_min_0_max_plus_10.bin)", + ], + data = [ + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/custom_op.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/quantized_with_gather.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/single_conv_weights_min_0_max_plus_10.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/weight_shared_between_convs.bin", + ], + tags = [ + # TODO(b/327796566): re-enable after the bug is fixed + "manual", + "noasan", + "noguitar", + "notap", + "tflite_not_portable_android", + "tflite_not_portable_ios", + ], + deps = [ + ":quantize_weights", + ":test_util", + "//tensorflow/compiler/mlir/lite/core:absl_error_model_builder", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs", + "//tensorflow/compiler/mlir/lite/schema:schema_utils", + "//tensorflow/core:framework_internal", + "//tensorflow/core:lib", + "@com_google_absl//absl/log", + "@com_google_absl//absl/status", + "@com_google_googletest//:gtest", + "@flatbuffers", + "@xla//xla/tsl/platform:logging", + ], +) + +cc_library( + name = "test_util", + testonly = 1, + srcs = ["test_util.cc"], + hdrs = ["test_util.h"], + visibility = ["//visibility:public"], + deps = [ + "//tensorflow/compiler/mlir/lite/core/api:error_reporter", + "@com_google_googletest//:gtest", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/quantize_model.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/quantize_model.cc new file mode 100644 index 0000000000000000000000000000000000000000..e91877240efd657149cd4ad18d4daf26a0a5b7e4 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/quantize_model.cc @@ -0,0 +1,147 @@ +/* Copyright 2019 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/compiler/mlir/lite/quantization/lite/quantize_model.h" + +#include +#include +#include + +#include "absl/container/flat_hash_set.h" +#include "absl/log/log.h" +#include "absl/status/status.h" +#include "absl/strings/string_view.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/Twine.h" +#include "llvm/Support/Debug.h" +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/Location.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/OwningOpRef.h" // from @llvm-project +#include "mlir/Pass/PassManager.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/common/tfl_pass_config.h" +#include "tensorflow/compiler/mlir/lite/debug/debug.h" +#include "tensorflow/compiler/mlir/lite/debug/debug_options.pb.h" +#include "tensorflow/compiler/mlir/lite/flatbuffer_export.h" +#include "tensorflow/compiler/mlir/lite/flatbuffer_import.h" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_config.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" +#include "tensorflow/compiler/mlir/lite/tf_tfl_passes.h" +#include "tensorflow/compiler/mlir/lite/transforms/passes.h" +#include "tensorflow/compiler/mlir/lite/utils/convert_type.h" +#include "tensorflow/compiler/mlir/tensorflow/utils/error_util.h" +#include "tensorflow/core/framework/types.pb.h" + +namespace mlir { +namespace lite { + +std::string TfLiteToMlir(const absl::string_view tflite_op_name) { + llvm::StringRef op_name(tflite_op_name.data(), tflite_op_name.size()); + return llvm::Twine("tfl.", op_name.lower()).str(); +} + +// TODO(fengliuai): check the result for `fully_quantize` flag. +absl::Status QuantizeModel( + const absl::string_view model_buffer, const tflite::TensorType &input_type, + const tflite::TensorType &output_type, + const tflite::TensorType &inference_type, + const std::unordered_set &operator_names, + bool disable_per_channel, bool fully_quantize, std::string &output_buffer, + bool verify_numeric, bool whole_model_verify, bool legacy_float_scale, + const absl::flat_hash_set &denylisted_ops, + const absl::flat_hash_set &denylisted_nodes, + const bool enable_variable_quantization, + bool disable_per_channel_for_dense_layers, + const std::optional + &debug_options) { + // Translate TFLite names to mlir op names. + absl::flat_hash_set denylisted_mlir_op_names; + for (const auto& entry : denylisted_ops) { + denylisted_mlir_op_names.insert(TfLiteToMlir(entry)); + } + + DialectRegistry registry; + registry.insert(); + MLIRContext context(registry); + StatusScopedDiagnosticHandler statusHandler(&context, + /*propagate=*/true); + + OwningOpRef module = tflite::FlatBufferToMlir( + model_buffer, &context, UnknownLoc::get(&context)); + if (!module) { + return absl::InternalError("Couldn't import flatbuffer to MLIR."); + } + + // Apply quantization passes. + PassManager pm((*module)->getName(), OpPassManager::Nesting::Implicit); + if (debug_options.has_value()) { + // Add debugging instrumentation + tensorflow::InitPassManager(pm, debug_options.value()); + } + TFL::QuantizationSpecs quant_specs; + quant_specs.inference_type = tflite::TflTypeToTfType(inference_type); + quant_specs.post_training_quantization = true; + quant_specs.disable_per_channel = disable_per_channel; + quant_specs.disable_per_channel_for_dense_layers = + disable_per_channel_for_dense_layers; + quant_specs.verify_numeric = verify_numeric; + quant_specs.whole_model_verify = whole_model_verify; + quant_specs.legacy_float_scale = legacy_float_scale; + quant_specs.ops_blocklist = denylisted_mlir_op_names; + quant_specs.nodes_blocklist = denylisted_nodes; + quant_specs.enable_mlir_variable_quantization = enable_variable_quantization; + + llvm::dbgs() << "fully_quantize: " << fully_quantize + << ", inference_type: " << quant_specs.inference_type + << ", input_inference_type: " + << tflite::EnumNameTensorType(input_type) + << ", output_inference_type: " + << tflite::EnumNameTensorType(output_type) << "\n"; + mlir::Builder mlir_builder(&context); + mlir::Type input_mlir_type = + tflite::ConvertElementType(input_type, mlir_builder); + mlir::Type output_mlir_type = + tflite::ConvertElementType(output_type, mlir_builder); + + if (fully_quantize) { + input_mlir_type = tflite::ConvertElementType(inference_type, mlir_builder); + output_mlir_type = input_mlir_type; + } + + tensorflow::AddQuantizationPasses(mlir::TFL::PassConfig(quant_specs), pm); + pm.addPass(TFL::CreateModifyIONodesPass(input_mlir_type, output_mlir_type)); + // If the first or final ops are not quantized, remove QDQ. + pm.addPass(TFL::CreatePostQuantizeRemoveQDQPass()); + if (failed(pm.run(module.get()))) { + const std::string err(statusHandler.ConsumeStatus().message()); + return absl::InternalError(err); + } + + // Export the results. + tflite::FlatbufferExportOptions options; + options.converter_flags.set_force_select_tf_ops(false); + options.converter_flags.set_enable_select_tf_ops(true); + options.converter_flags.set_allow_custom_ops(true); + if (!tflite::MlirToFlatBufferTranslateFunction(module.get(), options, + &output_buffer)) { + return absl::InternalError("Failed to export MLIR to flatbuffer."); + } + return absl::OkStatus(); +} + +} // namespace lite +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/quantize_model.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/quantize_model.h new file mode 100644 index 0000000000000000000000000000000000000000..eb45685ae12c08a356585c9c4c088bb1f30fb2bd --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/quantize_model.h @@ -0,0 +1,66 @@ +/* Copyright 2019 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_LITE_QUANTIZE_MODEL_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_LITE_QUANTIZE_MODEL_H_ + +#include +#include +#include + +#include "absl/container/flat_hash_set.h" +#include "absl/status/status.h" +#include "absl/strings/string_view.h" +#include "tensorflow/compiler/mlir/lite/debug/debug_options.pb.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" + +namespace mlir { +namespace lite { + +// Quantizes the input model represented as `model_buffer` and writes the result +// to the `output_buffer`. Both `model_buffer` and `output_buffer` should be a +// valid FlatBuffer format for Model supported by TFLite. +// +// The `input_type`, `output_type` and `inference_type` can be float32 / qint8 / +// int8 / int16. +// +// Returns a partially quantized model if `fully_quantize` is false. Returns a +// non-OK status if the quantization fails. +// +// When `verify_numeric` is true, the model will have it's original float ops +// and NumericVerify ops to compare output values from the quantized and float +// ops. +// +// When `legacy_float_scale` is true, the quantizer will use float scale instead +// of double, and call TOCO's quantization routines to maintain bit-exactness of +// the values with the TOCO quantizer. +absl::Status QuantizeModel( + absl::string_view model_buffer, const tflite::TensorType &input_type, + const tflite::TensorType &output_type, + const tflite::TensorType &inference_type, + const std::unordered_set &operator_names, + bool disable_per_channel, bool fully_quantize, std::string &output_buffer, + bool verify_numeric = false, bool whole_model_verify = false, + bool legacy_float_scale = true, + const absl::flat_hash_set &denylisted_ops = {}, + const absl::flat_hash_set &denylisted_nodes = {}, + bool enable_variable_quantization = false, + bool disable_per_channel_for_dense_layers = false, + const std::optional + &debug_options = std::nullopt); + +} // namespace lite +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_LITE_QUANTIZE_MODEL_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/quantize_model_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/quantize_model_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..1886f362bd92c4472ed0c34d83cf10c4a67345e0 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/quantize_model_test.cc @@ -0,0 +1,1787 @@ +/* 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 "tensorflow/compiler/mlir/lite/quantization/lite/quantize_model.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include "absl/container/flat_hash_set.h" +#include "absl/status/status.h" +#include "flatbuffers/flatbuffer_builder.h" // from @flatbuffers +#include "flatbuffers/vector.h" // from @flatbuffers +#include "tensorflow/compiler/mlir/lite/core/absl_error_model_builder.h" +#include "tensorflow/compiler/mlir/lite/quantization/lite/test_util.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_utils.h" +#include "xla/tsl/lib/core/status_test_util.h" +#include "tensorflow/core/platform/init_main.h" +#include "tensorflow/core/platform/path.h" +#include "tensorflow/core/platform/types.h" +#include "tensorflow/core/util/command_line_flags.h" + +// Note: branched from tensorflow/lite/tools/optimize/quantize_model_test.cc + +namespace { +std::string* g_test_model_dir = nullptr; +} // namespace + +namespace tflite { +namespace optimize { +namespace { + +using mlir::TFL::FlatBufferModelAbslError; +using testing::Eq; +using testing::FloatEq; +using testing::FloatNear; +using testing::IsEmpty; +using testing::NotNull; +using testing::SizeIs; + +// Unpacks the given flatbuffer model. +// +// This helper is useful as UnPackTo requires the input to not have any existing +// state so directly calling UnPackTo could lead to memory leaks if the model +// already had some state. Instead, the returned object from here can be used to +// overwrite existing model. +ModelT UnPackFlatBufferModel(const Model& flatbuffer_model) { + ModelT model; + flatbuffer_model.UnPackTo(&model); + return model; +} + +absl::Status QuantizeModel( + ModelT* model, const TensorType& input_type, const TensorType& output_type, + const bool allow_float, + const std::unordered_set& operator_names, + const TensorType& activations_type, std::string& output_buffer, + const bool disable_per_channel = false, + const absl::flat_hash_set& blocked_ops = {}, + const absl::flat_hash_set& blocked_nodes = {}, + const bool disable_per_channel_for_dense_layers = false) { + TensorType inference_tensor_type = activations_type; + const bool fully_quantize = !allow_float; + + flatbuffers::FlatBufferBuilder input_builder; + tflite::FinishModelBuffer(input_builder, + tflite::Model::Pack(input_builder, model)); + + const std::string input_buffer( + reinterpret_cast(input_builder.GetBufferPointer()), + input_builder.GetSize()); + auto status = mlir::lite::QuantizeModel( + input_buffer, input_type, output_type, inference_tensor_type, + /*operator_names=*/{}, disable_per_channel, fully_quantize, output_buffer, + /*verify_numeric=*/false, /*whole_model_verify=*/false, + /*legacy_float_scale=*/true, blocked_ops, blocked_nodes, + /*enable_variable_quantization=*/false, + /*disable_per_channel_for_dense_layers=*/ + disable_per_channel_for_dense_layers); + if (!status.ok()) { + return status; + } + + auto flatbuffer_model = FlatBufferModelAbslError::BuildFromBuffer( + output_buffer.data(), output_buffer.size()); + *model = UnPackFlatBufferModel(*flatbuffer_model->GetModel()); + return absl::OkStatus(); +} + +absl::Status QuantizeModel(ModelT* model, const TensorType& input_type, + const TensorType& output_type, bool allow_float, + std::string& output_buffer) { + return QuantizeModel(model, input_type, output_type, allow_float, + /*operator_names=*/{}, TensorType_INT8, output_buffer); +} + +absl::Status QuantizeModel(ModelT* model, const TensorType& input_type, + const TensorType& output_type, + std::string& output_buffer) { + return QuantizeModel(model, input_type, output_type, + /*allow_float=*/false, output_buffer); +} + +absl::Status QuantizeModel(ModelT* model, std::string& output_buffer) { + return QuantizeModel(model, TensorType_FLOAT32, TensorType_FLOAT32, + /*allow_float=*/true, output_buffer); +} + +absl::Status QuantizeModelAllOperators( + ModelT* model, const TensorType& input_type, const TensorType& output_type, + bool allow_float, const TensorType& activations_type, + bool disable_per_channel, std::string& output_buffer) { + return QuantizeModel(model, input_type, output_type, allow_float, + /*operator_names=*/{}, activations_type, output_buffer, + disable_per_channel); +} + +absl::Status QuantizeModelAllOperators(ModelT* model, + const TensorType& input_type, + const TensorType& output_type, + bool allow_float, + const TensorType& activations_type, + std::string& output_buffer) { + return QuantizeModel(model, input_type, output_type, allow_float, + /*operator_names=*/{}, activations_type, output_buffer); +} + +absl::Status QuantizeModelAllOperators( + ModelT* model, const TensorType& input_type, const TensorType& output_type, + bool allow_float, const TensorType& activations_type, + std::string& output_buffer, bool disable_per_channel_for_dense_layers) { + return QuantizeModel(model, input_type, output_type, allow_float, + /*operator_names=*/{}, activations_type, output_buffer, + /*disable_per_channel=*/false, + /* blocked_ops=*/{}, + /*blocked_nodes=*/{}, + /*disable_per_channel_for_dense_layers=*/ + disable_per_channel_for_dense_layers); +} + +std::unique_ptr ReadModel( + const std::string& model_name) { + auto model_path = tensorflow::io::JoinPath(*g_test_model_dir, model_name); + return FlatBufferModelAbslError::BuildFromFile(model_path.c_str()); +} + +template +std::vector GetAsVector(const flatbuffers::Vector* vec) { + return std::vector(vec->begin(), vec->end()); +} + +void VerifyQuantizationScale( + const QuantizationParameters& float_quant_params, + const QuantizationParametersT& quantized_quant_params, const int bit_num, + const bool symmetric) { + const float eps = 1e-7; + ASSERT_THAT(*float_quant_params.min(), SizeIs(1)); + ASSERT_THAT(*float_quant_params.max(), SizeIs(1)); + float float_min = std::min(0.f, float_quant_params.min()->Get(0)); + float float_max = std::max(0.f, float_quant_params.max()->Get(0)); + if (symmetric) { + // When the symmetric case, ConvertStatsToQDQs in PrepareQuantizePass + // updates the min and max values into bigger absolute values. + float_max = std::max(std::abs(float_min), std::abs(float_max)); + float_min = -float_max; + } + + ASSERT_THAT(quantized_quant_params.scale, SizeIs(1)); + ASSERT_THAT(quantized_quant_params.zero_point, SizeIs(1)); + float scale = (float_max - float_min) / ((1 << bit_num) - 1); + EXPECT_THAT(scale, FloatNear(quantized_quant_params.scale[0], eps)); +} + +class QuantizeModelTest : public testing::Test { + protected: + QuantizeModelTest() { + input_model_ = + ReadModel(::mlir::lite::internal::kConvModelWith0Plus10Weights); + readonly_model_ = input_model_->GetModel(); + model_ = UnPackFlatBufferModel(*readonly_model_); + } + + std::unique_ptr input_model_; + const Model* readonly_model_; + tflite::ModelT model_; + std::string output_buffer_; // Raw buffer for quantized output model. +}; + +void ExpectEqualTensor(TensorT* tensor, TensorT* expected_tensor) { + const float eps = 1e-7; + EXPECT_THAT(expected_tensor, NotNull()); + EXPECT_THAT(tensor->is_variable, Eq(expected_tensor->is_variable)); + EXPECT_THAT(tensor->shape, Eq(expected_tensor->shape)); + EXPECT_THAT(tensor->type, Eq(expected_tensor->type)); + const auto quantization_params = tensor->quantization.get(); + const auto expected_quantization_params = expected_tensor->quantization.get(); + if (quantization_params != nullptr && + expected_quantization_params != nullptr) { + for (int i = 0; i < quantization_params->scale.size(); ++i) { + if (quantization_params->scale[i] > 3e-5) { + EXPECT_THAT(quantization_params->scale[i], + FloatNear(expected_quantization_params->scale[i], eps)); + } + } + EXPECT_THAT(quantization_params->zero_point, + Eq(expected_quantization_params->zero_point)); + } +} + +TensorT* FindMatchingExpectedTensor(const SubGraphT& expected_graph, + const ModelT& expected_model, + const ModelT& quant_model, + const OperatorT& quant_op, int idx) { + const auto& builtin_code = + GetBuiltinCode(quant_model.operator_codes[quant_op.opcode_index].get()); + for (const auto& expected_op : expected_graph.operators) { + const auto& op_code = + expected_model.operator_codes[expected_op->opcode_index].get(); + const auto& expected_code = GetBuiltinCode(op_code); + if (expected_code == builtin_code) { + return expected_graph.tensors[expected_op->inputs[idx]].get(); + } + } + return nullptr; +} + +void ExpectSameModels(const ModelT& model, const ModelT& expected_model) { + ASSERT_THAT(model.subgraphs, SizeIs(expected_model.subgraphs.size())); + for (size_t subgraph_idx = 0; subgraph_idx < model.subgraphs.size(); + subgraph_idx++) { + const auto graph = model.subgraphs[subgraph_idx].get(); + const auto expected_graph = expected_model.subgraphs[subgraph_idx].get(); + for (auto& op : graph->operators) { + for (int idx = 0; idx < op->inputs.size(); idx++) { + if (op->inputs[idx] < 0) { + continue; + } + const auto& tensor = graph->tensors[op->inputs[idx]]; + auto* expected_tensor = FindMatchingExpectedTensor( + *expected_graph, expected_model, model, *op, idx); + if (!expected_tensor) { + continue; + } + ExpectEqualTensor(tensor.get(), expected_tensor); + if (expected_tensor->buffer > 0) { + const int buffer_idx = tensor->buffer; + const int expected_buffer_idx = expected_tensor->buffer; + const auto buffer = model.buffers[buffer_idx].get()->data; + const auto expected_buffer = + expected_model.buffers[expected_buffer_idx].get()->data; + EXPECT_THAT(buffer, Eq(expected_buffer)); + } + } + } + } +} + +class QuantizeConvModelTest : public QuantizeModelTest, + public testing::WithParamInterface { + protected: + QuantizeConvModelTest() { + tensor_type_ = GetParam(); + input_model_ = + ReadModel(::mlir::lite::internal::kConvModelWith0Plus10Weights); + readonly_model_ = input_model_->GetModel(); + model_ = UnPackFlatBufferModel(*readonly_model_); + // Flatbuffer is missing calibration data -- add dummy params. + auto& subgraph = model_.subgraphs[0]; + auto* input = subgraph->tensors[subgraph->inputs[0]].get(); + auto* output = subgraph->tensors[subgraph->outputs[0]].get(); + input->quantization = std::make_unique(); + output->quantization = std::make_unique(); + input->quantization->min.push_back(0.0); + output->quantization->min.push_back(0.0); + input->quantization->max.push_back(6.0); + output->quantization->max.push_back(6.0); + } + TensorType tensor_type_; +}; + +INSTANTIATE_TEST_SUITE_P(QuantizeConvModelTestInst, QuantizeConvModelTest, + testing::ValuesIn({TensorType_INT8})); + +TEST_P(QuantizeConvModelTest, QuantizationSucceeds) { + TF_EXPECT_OK(QuantizeModelAllOperators(&model_, tensor_type_, tensor_type_, + /*allow_float=*/false, tensor_type_, + output_buffer_)); + + const Model* output_model = GetModel(output_buffer_.data()); + ASSERT_TRUE(output_model); +} + +TEST_P(QuantizeConvModelTest, SkipUnspecifiedLayer) { + TF_EXPECT_OK(QuantizeModel(&model_, TensorType_FLOAT32, TensorType_FLOAT32, + /*allow_float=*/true, /*operator_names=*/{}, + TensorType_FLOAT32, output_buffer_, + /*disable_per_channel=*/false, {"CONV_2D"})); + + ModelT expected_model; + readonly_model_->UnPackTo(&expected_model); + // The resulting model should be the same. + ExpectSameModels(model_, expected_model); +} + +TEST_P(QuantizeConvModelTest, SkipUnspecifiedLayerByName) { + TF_EXPECT_OK(QuantizeModel(&model_, TensorType_FLOAT32, TensorType_FLOAT32, + /*allow_float=*/true, /*operator_names=*/{}, + TensorType_FLOAT32, output_buffer_, + /*disable_per_channel=*/false, + /*blocked_ops=*/{}, {"output"})); + + ModelT expected_model; + readonly_model_->UnPackTo(&expected_model); + // The resulting model should be the same. + ExpectSameModels(model_, expected_model); +} + +TEST_P(QuantizeConvModelTest, GraphIsFullyQuantized) { + TF_EXPECT_OK(QuantizeModelAllOperators(&model_, tensor_type_, tensor_type_, + /*allow_float=*/false, tensor_type_, + output_buffer_)); + + for (const auto& subgraph : model_.subgraphs) { + for (const auto& tensor : subgraph->tensors) { + EXPECT_TRUE(tensor->type == TensorType_INT32 || + tensor->type == TensorType_INT8); + } + } +} + +class QuantizeConvNoBiasModelTest : public QuantizeModelTest { + protected: + QuantizeConvNoBiasModelTest() { + input_model_ = ReadModel(::mlir::lite::internal::kConvModelWithNoBias); + readonly_model_ = input_model_->GetModel(); + model_ = UnPackFlatBufferModel(*readonly_model_); + } +}; + +// TODO(b/327796566): re-enable after the bug is fixed +// TEST_F(QuantizeConvNoBiasModelTest, QuantizationSucceeds) { +// auto status = QuantizeModelAllOperators( +// &model_, TensorType_INT8, TensorType_INT8, +// /*allow_float=*/false, TensorType_INT8, &error_reporter_, +// output_buffer_); +// EXPECT_THAT(status, Eq(kTfLiteOk)); +// const Model* output_model = GetModel(output_buffer_.data()); +// ASSERT_TRUE(output_model); +// } + +class QuantizeSplitModelTest : public QuantizeModelTest { + protected: + QuantizeSplitModelTest() { + input_model_ = ReadModel(::mlir::lite::internal::kModelSplit); + readonly_model_ = input_model_->GetModel(); + model_ = UnPackFlatBufferModel(*readonly_model_); + } +}; + +// There are two outputs for split with different scales, the resulting model +// should have the scales be hardcodes to the input scale value. +TEST_F(QuantizeSplitModelTest, QuantizeSplit) { + TF_EXPECT_OK(QuantizeModelAllOperators( + &model_, TensorType_INT8, TensorType_INT8, + /*allow_float=*/false, TensorType_INT8, output_buffer_)); + + // There is only one subgraph. + const int32_t subgraph_idx = 0; + const auto& subgraph = model_.subgraphs[subgraph_idx]; + const auto& readonly_subgraph = + readonly_model_->subgraphs()->Get(subgraph_idx); + + // There should be two ops: the split and add in the original model. + EXPECT_THAT(*readonly_subgraph->operators(), SizeIs(2)); + EXPECT_THAT(subgraph->operators, SizeIs(2)); + const auto& split = subgraph->operators[0]; + const auto& add = subgraph->operators[1]; + EXPECT_THAT(GetBuiltinCode(model_.operator_codes[split->opcode_index].get()), + Eq(BuiltinOperator_SPLIT)); + EXPECT_THAT(GetBuiltinCode(model_.operator_codes[add->opcode_index].get()), + Eq(BuiltinOperator_ADD)); + + // There should be 5 tensors: input, output, split, split/split_dim, split:1. + // Tensor indices could be different between original and quantized. + EXPECT_THAT(subgraph->tensors, SizeIs(5)); + const int input_idx = 0; + EXPECT_THAT(subgraph->tensors[input_idx]->type, Eq(TensorType_INT8)); + EXPECT_THAT(subgraph->tensors[input_idx]->name, Eq("input")); + EXPECT_THAT(subgraph->tensors[input_idx]->quantization->scale, SizeIs(1)); + EXPECT_THAT(subgraph->tensors[input_idx]->quantization->zero_point, + SizeIs(1)); + EXPECT_THAT(subgraph->tensors[input_idx]->quantization->scale[0], + FloatEq(1.0)); + EXPECT_THAT(subgraph->tensors[input_idx]->quantization->zero_point[0], + Eq(-128)); + const int output_idx = 4; + EXPECT_THAT(subgraph->tensors[output_idx]->type, Eq(TensorType_INT8)); + EXPECT_THAT(subgraph->tensors[output_idx]->name, Eq("output")); + EXPECT_THAT(subgraph->tensors[output_idx]->quantization->scale, SizeIs(1)); + EXPECT_THAT(subgraph->tensors[output_idx]->quantization->zero_point, + SizeIs(1)); + EXPECT_THAT(subgraph->tensors[output_idx]->quantization->scale[0], + FloatEq(1.0)); + EXPECT_THAT(subgraph->tensors[output_idx]->quantization->zero_point[0], + Eq(-128)); + const int split0_idx = 2; + EXPECT_THAT(subgraph->tensors[split0_idx]->type, Eq(TensorType_INT8)); + EXPECT_THAT(subgraph->tensors[split0_idx]->name, Eq("split;split:1")); + EXPECT_THAT(subgraph->tensors[split0_idx]->quantization->scale, SizeIs(1)); + EXPECT_THAT(subgraph->tensors[split0_idx]->quantization->zero_point, + SizeIs(1)); + EXPECT_THAT(subgraph->tensors[split0_idx]->quantization->scale[0], + FloatEq(1.0)); + EXPECT_THAT(subgraph->tensors[split0_idx]->quantization->zero_point[0], + Eq(-128)); + const int split1_idx = 3; + EXPECT_THAT(subgraph->tensors[split1_idx]->type, Eq(TensorType_INT8)); + EXPECT_THAT(subgraph->tensors[split1_idx]->name, Eq("split;split:11")); + EXPECT_THAT(subgraph->tensors[split1_idx]->quantization->scale, SizeIs(1)); + EXPECT_THAT(subgraph->tensors[split1_idx]->quantization->zero_point, + SizeIs(1)); + EXPECT_THAT(subgraph->tensors[split1_idx]->quantization->scale[0], + FloatEq(1.0)); + EXPECT_THAT(subgraph->tensors[split1_idx]->quantization->zero_point[0], + Eq(-128)); + + // check op and versioning. + EXPECT_THAT(model_.operator_codes, SizeIs(2)); + EXPECT_THAT(GetBuiltinCode(model_.operator_codes[0].get()), + Eq(BuiltinOperator_SPLIT)); + ASSERT_THAT(model_.operator_codes[0]->version, Eq(2)); +} + +class QuantizeConvModel2Test : public QuantizeModelTest, + public testing::WithParamInterface { + protected: + QuantizeConvModel2Test() { + tensor_type_ = GetParam(); + input_model_ = + ReadModel(::mlir::lite::internal::kConvModelWith0Plus10Weights); + readonly_model_ = input_model_->GetModel(); + model_ = UnPackFlatBufferModel(*readonly_model_); + auto& subgraph = model_.subgraphs[0]; + auto* input = subgraph->tensors[subgraph->inputs[0]].get(); + auto* output = subgraph->tensors[subgraph->outputs[0]].get(); + input->quantization = std::make_unique(); + output->quantization = std::make_unique(); + input->quantization->min.push_back(0.0); + output->quantization->min.push_back(0.0); + input->quantization->max.push_back(6.0); + output->quantization->max.push_back(6.0); + } + + TensorType tensor_type_; +}; + +INSTANTIATE_TEST_SUITE_P(QuantizeConvModel2TestInst, QuantizeConvModel2Test, + testing::ValuesIn({TensorType_INT8})); + +TEST_P(QuantizeConvModel2Test, VerifyConvQuantization) { + TF_ASSERT_OK(QuantizeModelAllOperators(&model_, tensor_type_, tensor_type_, + /*allow_float=*/false, tensor_type_, + output_buffer_)); + const auto& subgraph = model_.subgraphs[0]; + auto conv_op = subgraph->operators[0].get(); + const int input_tensor_idx = 0; + const int weights_tensor_idx = 1; + const int bias_tensor_index = 2; + const int output_tensor_idx = 0; + const auto bias_tensor = + subgraph->tensors[conv_op->inputs[bias_tensor_index]].get(); + const auto input_tensor = + subgraph->tensors[conv_op->inputs[input_tensor_idx]].get(); + const auto weights_tensor = + subgraph->tensors[conv_op->inputs[weights_tensor_idx]].get(); + const auto output_tensor = + subgraph->tensors[conv_op->outputs[output_tensor_idx]].get(); + + EXPECT_THAT(bias_tensor->type, + Eq(tensor_type_ == TensorType_INT8 ? TensorType_INT32 + : TensorType_INT64)); + EXPECT_THAT(input_tensor->type, Eq(tensor_type_)); + EXPECT_THAT(weights_tensor->type, Eq(TensorType_INT8)); + + ASSERT_TRUE(weights_tensor->quantization); + ASSERT_TRUE(bias_tensor->quantization); + ASSERT_TRUE(weights_tensor->quantization); + const std::vector& bias_scales = bias_tensor->quantization->scale; + const std::vector& weights_scales = + weights_tensor->quantization->scale; + const std::vector& weights_zero_points = + weights_tensor->quantization->zero_point; + const int out_channel_size = weights_tensor->shape[0]; + ASSERT_THAT(bias_scales, SizeIs(out_channel_size)); + ASSERT_THAT(weights_scales, SizeIs(out_channel_size)); + ASSERT_THAT(weights_zero_points, SizeIs(out_channel_size)); + ASSERT_THAT(input_tensor->quantization->scale, SizeIs(1)); + ASSERT_THAT(output_tensor->quantization->scale, SizeIs(1)); + + const float eps = 1e-7; + + // Bias scale should be input * per_channel_weight_scale. + for (size_t i = 0; i < out_channel_size; i++) { + EXPECT_THAT(bias_scales[i], FloatNear(input_tensor->quantization->scale[0] * + weights_scales[i], + eps)); + } + + const auto bias_buffer = model_.buffers[bias_tensor->buffer].get(); + auto control_size = tensor_type_ == TensorType_INT8 + ? sizeof(int32_t) * bias_tensor->shape[0] + : sizeof(int64_t) * bias_tensor->shape[0]; + + const auto float_op = + readonly_model_->subgraphs()->Get(0)->operators()->Get(0); + const auto original_bias_tensor = + readonly_model_->subgraphs()->Get(0)->tensors()->Get( + float_op->inputs()->Get(2)); + ASSERT_THAT(bias_buffer->data, SizeIs(control_size)); + const auto original_bias_buffer = + readonly_model_->buffers()->Get(original_bias_tensor->buffer()); + const float* bias_float_buffer = + reinterpret_cast(original_bias_buffer->data()->data()); + + if (tensor_type_ == TensorType_INT8) { + int32_t* bias_values = reinterpret_cast(bias_buffer->data.data()); + for (size_t i = 0; i < out_channel_size; i++) { + auto dequantized_value = bias_values[i] * bias_scales[i]; + EXPECT_THAT(dequantized_value, + FloatNear(bias_float_buffer[i], bias_scales[i] / 2)); + } + } + + const auto weights_buffer = model_.buffers[weights_tensor->buffer].get(); + const auto original_weights_tensor = + readonly_model_->subgraphs()->Get(0)->tensors()->Get( + float_op->inputs()->Get(1)); + const auto original_weights_buffer = + readonly_model_->buffers()->Get(original_weights_tensor->buffer()); + const int8_t* weight_values = + reinterpret_cast(weights_buffer->data.data()); + const float* weights_float_buffer = + reinterpret_cast(original_weights_buffer->data()->data()); + ASSERT_THAT(sizeof(float) * weights_buffer->data.size(), + Eq(original_weights_buffer->data()->size())); + int num_values_in_channel = weights_buffer->data.size() / out_channel_size; + for (size_t channel_idx = 0; channel_idx < out_channel_size; channel_idx++) { + for (size_t j = 0; j < num_values_in_channel; j++) { + size_t element_idx = channel_idx * out_channel_size + j; + auto scale = weights_scales[channel_idx]; + auto zero_point = weights_zero_points[channel_idx]; + auto dequantized_value = weight_values[element_idx] * scale; + EXPECT_THAT(dequantized_value, + FloatNear(weights_float_buffer[element_idx], scale / 2)); + EXPECT_THAT(zero_point, Eq(0)); + } + } + + // check op and versioning. + EXPECT_THAT(model_.operator_codes, SizeIs(1)); + EXPECT_THAT(GetBuiltinCode(model_.operator_codes[0].get()), + Eq(BuiltinOperator_CONV_2D)); + ASSERT_THAT(model_.operator_codes[0]->version, Eq(3)); +} + +TEST_P(QuantizeConvModel2Test, VerifyConvDisablePerChannelQuantization) { + TF_ASSERT_OK(QuantizeModelAllOperators(&model_, tensor_type_, tensor_type_, + /*allow_float=*/false, tensor_type_, + /*disable_per_channel=*/true, + output_buffer_)); + const auto& subgraph = model_.subgraphs[0]; + auto conv_op = subgraph->operators[0].get(); + const int input_tensor_idx = 0; + const int weights_tensor_idx = 1; + const int bias_tensor_index = 2; + const int output_tensor_idx = 0; + const auto bias_tensor = + subgraph->tensors[conv_op->inputs[bias_tensor_index]].get(); + const auto input_tensor = + subgraph->tensors[conv_op->inputs[input_tensor_idx]].get(); + const auto weights_tensor = + subgraph->tensors[conv_op->inputs[weights_tensor_idx]].get(); + const auto output_tensor = + subgraph->tensors[conv_op->outputs[output_tensor_idx]].get(); + + EXPECT_THAT(bias_tensor->type, + Eq(tensor_type_ == TensorType_INT8 ? TensorType_INT32 + : TensorType_INT64)); + EXPECT_THAT(input_tensor->type, Eq(tensor_type_)); + EXPECT_THAT(weights_tensor->type, Eq(TensorType_INT8)); + + ASSERT_TRUE(weights_tensor->quantization); + ASSERT_TRUE(bias_tensor->quantization); + ASSERT_TRUE(weights_tensor->quantization); + const std::vector& bias_scales = bias_tensor->quantization->scale; + const std::vector& weights_scales = + weights_tensor->quantization->scale; + const std::vector& weights_zero_points = + weights_tensor->quantization->zero_point; + + const int out_channel_size = 1; + ASSERT_THAT(bias_scales, SizeIs(out_channel_size)); + ASSERT_THAT(weights_scales, SizeIs(out_channel_size)); + ASSERT_THAT(weights_zero_points, SizeIs(out_channel_size)); + ASSERT_THAT(input_tensor->quantization->scale, SizeIs(1)); + ASSERT_THAT(output_tensor->quantization->scale, SizeIs(1)); + + const float eps = 1e-7; + + // Bias scale should be input * per_channel_weight_scale. + for (size_t i = 0; i < out_channel_size; i++) { + EXPECT_THAT(bias_scales[i], FloatNear(input_tensor->quantization->scale[0] * + weights_scales[i], + eps)); + } + + const auto bias_buffer = model_.buffers[bias_tensor->buffer].get(); + auto control_size = tensor_type_ == TensorType_INT8 + ? sizeof(int32_t) * bias_tensor->shape[0] + : sizeof(int64_t) * bias_tensor->shape[0]; + + ASSERT_THAT(bias_buffer->data, SizeIs(control_size)); + const auto float_op = + readonly_model_->subgraphs()->Get(0)->operators()->Get(0); + const auto original_bias_tensor = + readonly_model_->subgraphs()->Get(0)->tensors()->Get( + float_op->inputs()->Get(2)); + ASSERT_THAT(bias_buffer->data, SizeIs(control_size)); + const auto original_bias_buffer = + readonly_model_->buffers()->Get(original_bias_tensor->buffer()); + const float* bias_float_buffer = + reinterpret_cast(original_bias_buffer->data()->data()); + + if (tensor_type_ == TensorType_INT8) { + int32_t* bias_values = reinterpret_cast(bias_buffer->data.data()); + for (size_t i = 0; i < out_channel_size; i++) { + auto dequantized_value = bias_values[i] * bias_scales[i]; + EXPECT_THAT(dequantized_value, + FloatNear(bias_float_buffer[i], bias_scales[i] / 2)); + } + } + + const auto weights_buffer = model_.buffers[weights_tensor->buffer].get(); + const auto original_weights_tensor = + readonly_model_->subgraphs()->Get(0)->tensors()->Get( + float_op->inputs()->Get(1)); + const auto original_weights_buffer = + readonly_model_->buffers()->Get(original_weights_tensor->buffer()); + const int8_t* weight_values = + reinterpret_cast(weights_buffer->data.data()); + const float* weights_float_buffer = + reinterpret_cast(original_weights_buffer->data()->data()); + ASSERT_THAT(sizeof(float) * weights_buffer->data.size(), + Eq(original_weights_buffer->data()->size())); + int num_values_in_channel = weights_buffer->data.size() / out_channel_size; + for (size_t channel_idx = 0; channel_idx < out_channel_size; channel_idx++) { + for (size_t j = 0; j < num_values_in_channel; j++) { + size_t element_idx = channel_idx * out_channel_size + j; + auto scale = weights_scales[channel_idx]; + auto zero_point = weights_zero_points[channel_idx]; + auto dequantized_value = weight_values[element_idx] * scale; + EXPECT_THAT(dequantized_value, + FloatNear(weights_float_buffer[element_idx], scale / 2)); + EXPECT_THAT(zero_point, Eq(0)); + } + } + + // check op and versioning. + EXPECT_THAT(model_.operator_codes, SizeIs(1)); + EXPECT_THAT(GetBuiltinCode(model_.operator_codes[0].get()), + Eq(BuiltinOperator_CONV_2D)); + ASSERT_THAT(model_.operator_codes[0]->version, Eq(3)); +} + +class QuantizeSoftmaxTest : public QuantizeModelTest { + protected: + QuantizeSoftmaxTest() { + input_model_ = + ReadModel(::mlir::lite::internal::kSingleSoftmaxModelMinMinus5MaxPlus5); + readonly_model_ = input_model_->GetModel(); + model_ = UnPackFlatBufferModel(*readonly_model_); + } +}; + +TEST_F(QuantizeSoftmaxTest, VerifySoftmaxQuantization) { + TF_ASSERT_OK(QuantizeModelAllOperators( + &model_, TensorType_INT8, TensorType_INT8, + /*allow_float=*/false, TensorType_INT8, output_buffer_)); + + const auto& subgraph = model_.subgraphs[0]; + auto op = subgraph->operators[0].get(); + // Model has a single softmax op. + ASSERT_THAT(op->opcode_index, Eq(0)); + ASSERT_THAT(GetBuiltinCode(model_.operator_codes[0].get()), + Eq(BuiltinOperator_SOFTMAX)); + + ASSERT_THAT(op->inputs, SizeIs(1)); + ASSERT_THAT(op->outputs, SizeIs(1)); + auto float_graph = readonly_model_->subgraphs()->Get(0); + + // Verify input. + ASSERT_THAT(float_graph->tensors()->Get(op->inputs[0])->type(), + Eq(TensorType_FLOAT32)); + ASSERT_THAT(float_graph->tensors()->Get(op->outputs[0])->type(), + Eq(TensorType_FLOAT32)); + + EXPECT_THAT(subgraph->tensors[op->inputs[0]].get()->type, + Eq(TensorType_INT8)); + EXPECT_THAT(subgraph->tensors[op->outputs[0]].get()->type, + Eq(TensorType_INT8)); + + auto float_input_quant_params = + float_graph->tensors()->Get(op->inputs[0])->quantization(); + auto input_quant_params = + subgraph->tensors[op->inputs[0]]->quantization.get(); + VerifyQuantizationScale(*float_input_quant_params, *input_quant_params, + /*bit_num=*/8, /*symmetric=*/false); + + // Verify output. + auto float_output_quant_params = + float_graph->tensors()->Get(op->outputs[0])->quantization(); + auto output_quant_params = + subgraph->tensors[op->outputs[0]]->quantization.get(); + ASSERT_THAT(*float_output_quant_params->min(), SizeIs(1)); + ASSERT_THAT(*float_output_quant_params->max(), SizeIs(1)); + + ASSERT_THAT(output_quant_params->scale, SizeIs(1)); + ASSERT_THAT(output_quant_params->zero_point, SizeIs(1)); + ASSERT_THAT(1.0f / 256.0f, Eq(output_quant_params->scale[0])); + ASSERT_THAT(-128, Eq(output_quant_params->zero_point[0])); + + // check op and versioning. + EXPECT_THAT(model_.operator_codes, SizeIs(1)); + EXPECT_THAT(GetBuiltinCode(model_.operator_codes[0].get()), + Eq(BuiltinOperator_SOFTMAX)); + ASSERT_THAT(model_.operator_codes[0]->version, Eq(2)); +} + +class QuantizeAvgPoolTest : public QuantizeModelTest { + protected: + QuantizeAvgPoolTest() { + input_model_ = + ReadModel(::mlir::lite::internal::kSingleAvgPoolModelMinMinus5MaxPlus5); + readonly_model_ = input_model_->GetModel(); + model_ = UnPackFlatBufferModel(*readonly_model_); + } +}; + +TEST_F(QuantizeAvgPoolTest, VerifyAvgPoolQuantization) { + TF_ASSERT_OK(QuantizeModelAllOperators( + &model_, TensorType_INT8, TensorType_INT8, + /*allow_float=*/false, TensorType_INT8, output_buffer_)); + + const auto& subgraph = model_.subgraphs[0]; + auto op = subgraph->operators[0].get(); + // Model has a single AveragePool op. + ASSERT_THAT(op->opcode_index, Eq(0)); + ASSERT_THAT(GetBuiltinCode(model_.operator_codes[0].get()), + Eq(BuiltinOperator_AVERAGE_POOL_2D)); + + ASSERT_THAT(op->inputs, SizeIs(1)); + ASSERT_THAT(op->outputs, SizeIs(1)); + + auto float_graph = readonly_model_->subgraphs()->Get(0); + ASSERT_THAT(float_graph->tensors()->Get(op->inputs[0])->type(), + Eq(TensorType_FLOAT32)); + ASSERT_THAT(float_graph->tensors()->Get(op->outputs[0])->type(), + Eq(TensorType_FLOAT32)); + + EXPECT_THAT(subgraph->tensors[op->inputs[0]].get()->type, + Eq(TensorType_INT8)); + EXPECT_THAT(subgraph->tensors[op->outputs[0]].get()->type, + Eq(TensorType_INT8)); + + auto float_input_quant_params = + float_graph->tensors()->Get(op->inputs[0])->quantization(); + auto input_quant_params = + subgraph->tensors[op->inputs[0]]->quantization.get(); + VerifyQuantizationScale(*float_input_quant_params, *input_quant_params, + /*bit_num=*/8, /*symmetric=*/false); + + auto float_output_quant_params = + float_graph->tensors()->Get(op->outputs[0])->quantization(); + auto output_quant_params = + subgraph->tensors[op->outputs[0]]->quantization.get(); + ASSERT_THAT(*float_output_quant_params->min(), SizeIs(1)); + ASSERT_THAT(*float_output_quant_params->max(), SizeIs(1)); + ASSERT_THAT(output_quant_params->scale, SizeIs(1)); + + // Make sure the input min/maxes are propagated to outputs. + EXPECT_THAT(input_quant_params->scale[0], Eq(output_quant_params->scale[0])); + + // check op and versioning. + EXPECT_THAT(model_.operator_codes, SizeIs(1)); + EXPECT_THAT(GetBuiltinCode(model_.operator_codes[0].get()), + Eq(BuiltinOperator_AVERAGE_POOL_2D)); + ASSERT_THAT(model_.operator_codes[0]->version, Eq(2)); +} + +class QuantizeMultiInputAddWithReshapeTest : public QuantizeModelTest { + protected: + QuantizeMultiInputAddWithReshapeTest() { + input_model_ = ReadModel(::mlir::lite::internal::kMultiInputAddWithReshape); + readonly_model_ = input_model_->GetModel(); + model_ = UnPackFlatBufferModel(*readonly_model_); + } +}; + +TEST_F(QuantizeMultiInputAddWithReshapeTest, VerifyReshapeQuantization) { + TF_ASSERT_OK(QuantizeModelAllOperators( + &model_, TensorType_INT8, TensorType_INT8, + /*allow_float=*/false, TensorType_INT8, output_buffer_)); + + // Verify Reshape is quantized. + const auto& subgraph = model_.subgraphs[0]; + auto op = subgraph->operators[1].get(); + ASSERT_THAT(GetBuiltinCode(model_.operator_codes[op->opcode_index].get()), + Eq(BuiltinOperator_RESHAPE)); + + ASSERT_THAT(op->inputs, SizeIs(2)); + ASSERT_THAT(op->outputs, SizeIs(1)); + + auto float_graph = readonly_model_->subgraphs()->Get(0); + auto float_op = float_graph->operators()->Get(1); + ASSERT_THAT(float_graph->tensors()->Get(float_op->inputs()->Get(0))->type(), + Eq(TensorType_FLOAT32)); + ASSERT_THAT(float_graph->tensors()->Get(float_op->outputs()->Get(0))->type(), + Eq(TensorType_FLOAT32)); + + EXPECT_THAT(subgraph->tensors[op->inputs[0]].get()->type, + Eq(TensorType_INT8)); + EXPECT_THAT(subgraph->tensors[op->outputs[0]].get()->type, + Eq(TensorType_INT8)); + auto float_input_quant_params = + float_graph->tensors()->Get(op->inputs[0])->quantization(); + auto input_quant_params = + subgraph->tensors[op->inputs[0]]->quantization.get(); + VerifyQuantizationScale(*float_input_quant_params, *input_quant_params, + /*bit_num=*/8, /*symmetric=*/false); + + auto float_output_quant_params = + float_graph->tensors()->Get(float_op->outputs()->Get(0))->quantization(); + auto output_quant_params = + subgraph->tensors[op->outputs[0]]->quantization.get(); + ASSERT_THAT(*float_output_quant_params->min(), SizeIs(1)); + ASSERT_THAT(*float_output_quant_params->max(), SizeIs(1)); + ASSERT_THAT(output_quant_params->scale, SizeIs(1)); + + // check op and versioning. + EXPECT_THAT(model_.operator_codes, SizeIs(2)); + EXPECT_THAT(GetBuiltinCode(model_.operator_codes[0].get()), + Eq(BuiltinOperator_ADD)); + ASSERT_THAT(model_.operator_codes[0]->version, Eq(2)); + EXPECT_THAT(GetBuiltinCode(model_.operator_codes[1].get()), + Eq(BuiltinOperator_RESHAPE)); + ASSERT_THAT(model_.operator_codes[1]->version, Eq(1)); +} + +TEST_F(QuantizeMultiInputAddWithReshapeTest, VerifyAddQuantization) { + TF_ASSERT_OK(QuantizeModelAllOperators( + &model_, TensorType_INT8, TensorType_INT8, + /*allow_float=*/false, TensorType_INT8, output_buffer_)); + + // Verify ADD is quantized. + const auto& subgraph = model_.subgraphs[0]; + auto op = subgraph->operators[0].get(); + ASSERT_THAT(GetBuiltinCode(model_.operator_codes[op->opcode_index].get()), + Eq(BuiltinOperator_ADD)); + + ASSERT_THAT(op->inputs, SizeIs(2)); + ASSERT_THAT(op->outputs, SizeIs(1)); + + auto float_graph = readonly_model_->subgraphs()->Get(0); + auto float_op = float_graph->operators()->Get(0); + const int float_input0_idx = float_op->inputs()->Get(0); + const int float_input1_idx = float_op->inputs()->Get(1); + const int float_output_idx = float_op->outputs()->Get(0); + ASSERT_THAT(float_graph->tensors()->Get(float_input0_idx)->type(), + Eq(TensorType_FLOAT32)); + ASSERT_THAT(float_graph->tensors()->Get(float_input1_idx)->type(), + Eq(TensorType_FLOAT32)); + ASSERT_THAT(float_graph->tensors()->Get(float_output_idx)->type(), + Eq(TensorType_FLOAT32)); + + for (size_t input_idx = 0; input_idx < 2; ++input_idx) { + EXPECT_THAT(subgraph->tensors[op->inputs[input_idx]].get()->type, + Eq(TensorType_INT8)); + auto float_input_quant_params = + float_graph->tensors() + ->Get(float_op->inputs()->Get(input_idx)) + ->quantization(); + auto input_quant_params = + subgraph->tensors[op->inputs[input_idx]]->quantization.get(); + VerifyQuantizationScale(*float_input_quant_params, *input_quant_params, + /*bit_num=*/8, /*symmetric=*/false); + } + + EXPECT_THAT(subgraph->tensors[op->outputs[0]].get()->type, + Eq(TensorType_INT8)); + auto float_output_quant_params = + float_graph->tensors()->Get(op->outputs[0])->quantization(); + auto output_quant_params = + subgraph->tensors[op->outputs[0]]->quantization.get(); + ASSERT_THAT(*float_output_quant_params->min(), SizeIs(1)); + ASSERT_THAT(*float_output_quant_params->max(), SizeIs(1)); + ASSERT_THAT(output_quant_params->scale, SizeIs(1)); + + // check op and versioning. + EXPECT_THAT(model_.operator_codes, SizeIs(2)); + EXPECT_THAT(GetBuiltinCode(model_.operator_codes[0].get()), + Eq(BuiltinOperator_ADD)); + ASSERT_THAT(model_.operator_codes[0]->version, Eq(2)); + EXPECT_THAT(GetBuiltinCode(model_.operator_codes[1].get()), + Eq(BuiltinOperator_RESHAPE)); + ASSERT_THAT(model_.operator_codes[1]->version, Eq(1)); +} + +class QuantizeConstInputTest : public QuantizeModelTest, + public testing::WithParamInterface { + protected: + QuantizeConstInputTest() { + tensor_type_ = GetParam(); + input_model_ = ReadModel(::mlir::lite::internal::kConstInputAddModel); + readonly_model_ = input_model_->GetModel(); + model_ = UnPackFlatBufferModel(*readonly_model_); + } + + TensorType tensor_type_; +}; +INSTANTIATE_TEST_SUITE_P(QuantizeConstInputTestInst, QuantizeConstInputTest, + testing::ValuesIn({TensorType_INT8})); + +TEST_P(QuantizeConstInputTest, VerifyConstOpInput) { + TF_ASSERT_OK(QuantizeModelAllOperators(&model_, tensor_type_, tensor_type_, + /*allow_float=*/false, tensor_type_, + output_buffer_)); + + // Verify ConstOp is quantized. + const auto& subgraph = model_.subgraphs[0]; + auto op = subgraph->operators[0].get(); + ASSERT_THAT(GetBuiltinCode(model_.operator_codes[op->opcode_index].get()), + Eq(BuiltinOperator_ADD)); + + ASSERT_THAT(op->inputs, SizeIs(2)); + ASSERT_THAT(op->outputs, SizeIs(1)); + + auto float_graph = readonly_model_->subgraphs()->Get(0); + ASSERT_THAT(float_graph->tensors()->Get(op->inputs[0])->type(), + Eq(TensorType_FLOAT32)); + ASSERT_THAT(float_graph->tensors()->Get(op->outputs[0])->type(), + Eq(TensorType_FLOAT32)); + + for (size_t input_idx = 0; input_idx < 2; ++input_idx) { + EXPECT_THAT(subgraph->tensors[op->inputs[input_idx]].get()->type, + Eq(tensor_type_)); + } + + EXPECT_THAT(subgraph->tensors[op->outputs[0]].get()->type, Eq(tensor_type_)); + + // check op and versioning. + EXPECT_THAT(model_.operator_codes, SizeIs(1)); + EXPECT_THAT(GetBuiltinCode(model_.operator_codes[0].get()), + Eq(BuiltinOperator_ADD)); + ASSERT_THAT(model_.operator_codes[0]->version, Eq(2)); +} + +class QuantizeArgMaxTest : public QuantizeModelTest { + protected: + QuantizeArgMaxTest() { + input_model_ = ReadModel(::mlir::lite::internal::kModelWithArgMaxOp); + readonly_model_ = input_model_->GetModel(); + model_ = UnPackFlatBufferModel(*readonly_model_); + } +}; + +TEST_F(QuantizeArgMaxTest, VerifyArgMax) { + TF_ASSERT_OK(QuantizeModelAllOperators( + &model_, TensorType_INT8, TensorType_INT8, + /*allow_float=*/false, TensorType_INT8, output_buffer_)); + + const auto& subgraph = model_.subgraphs[0]; + auto op = subgraph->operators[0].get(); + ASSERT_THAT(GetBuiltinCode(model_.operator_codes[op->opcode_index].get()), + Eq(BuiltinOperator_ARG_MAX)); + + ASSERT_THAT(op->inputs, SizeIs(2)); + ASSERT_THAT(op->outputs, SizeIs(1)); + + auto float_graph = readonly_model_->subgraphs()->Get(0); + auto float_op = float_graph->operators()->Get(0); + // Verify ArgMax input is quantized. + ASSERT_THAT(float_graph->tensors()->Get(float_op->inputs()->Get(0))->type(), + Eq(TensorType_FLOAT32)); + EXPECT_THAT(subgraph->tensors[op->inputs[0]].get()->type, + Eq(TensorType_INT8)); + + // Verify ArgMax input axis should still be the same type. + ASSERT_THAT(float_graph->tensors()->Get(float_op->inputs()->Get(1))->type(), + Eq(subgraph->tensors[op->inputs[1]].get()->type)); + + // The output of ArgMax should still be the same type. + ASSERT_THAT(float_graph->tensors()->Get(float_op->outputs()->Get(0))->type(), + Eq(subgraph->tensors[op->outputs[0]].get()->type)); + + // check op and versioning. + EXPECT_THAT(model_.operator_codes, SizeIs(1)); + EXPECT_THAT(GetBuiltinCode(model_.operator_codes[0].get()), + Eq(BuiltinOperator_ARG_MAX)); + ASSERT_THAT(model_.operator_codes[0]->version, Eq(2)); +} + +class QuantizeLSTMTest : public QuantizeModelTest { + protected: + QuantizeLSTMTest() { + input_model_ = ReadModel(::mlir::lite::internal::kLstmCalibrated); + readonly_model_ = input_model_->GetModel(); + model_ = UnPackFlatBufferModel(*readonly_model_); + } +}; + +TEST_F(QuantizeLSTMTest, VerifyLSTM) { + TF_ASSERT_OK(QuantizeModelAllOperators( + &model_, TensorType_FLOAT32, TensorType_FLOAT32, + /*allow_float=*/true, TensorType_INT8, output_buffer_)); + + // Read expected model. + auto expected_fb_model = ReadModel(::mlir::lite::internal::kLstmQuantized); + auto expected_read_only_model = expected_fb_model->GetModel(); + ModelT expected_model; + expected_read_only_model->UnPackTo(&expected_model); + + ExpectSameModels(model_, expected_model); +} + +class QuantizeLSTM2Test : public QuantizeModelTest { + protected: + QuantizeLSTM2Test() { + input_model_ = ReadModel(::mlir::lite::internal::kLstmCalibrated2); + readonly_model_ = input_model_->GetModel(); + model_ = UnPackFlatBufferModel(*readonly_model_); + } +}; + +TEST_F(QuantizeLSTM2Test, VerifyLSTM) { + // Quantize model. + TF_ASSERT_OK(QuantizeModelAllOperators( + &model_, TensorType_FLOAT32, TensorType_FLOAT32, + /*allow_float=*/false, TensorType_INT8, output_buffer_)); + + // Read expected model. + auto expected_fb_model = ReadModel(::mlir::lite::internal::kLstmQuantized2); + auto expected_read_only_model = expected_fb_model->GetModel(); + ModelT expected_model; + expected_read_only_model->UnPackTo(&expected_model); + + ExpectSameModels(model_, expected_model); +} + +class QuantizeUnidirectionalSequenceLSTMTest : public QuantizeModelTest { + protected: + QuantizeUnidirectionalSequenceLSTMTest() { + input_model_ = ReadModel( + ::mlir::lite::internal::kUnidirectionalSequenceLstmCalibrated); + readonly_model_ = input_model_->GetModel(); + model_ = UnPackFlatBufferModel(*readonly_model_); + } +}; + +TEST_F(QuantizeUnidirectionalSequenceLSTMTest, + VerifyUnidirectionalSequenceLSTM) { + TF_ASSERT_OK(QuantizeModelAllOperators( + &model_, TensorType_FLOAT32, TensorType_FLOAT32, + /*allow_float=*/false, TensorType_INT8, output_buffer_)); + + // Read expected model. + auto expected_fb_model = + ReadModel(::mlir::lite::internal::kUnidirectionalSequenceLstmQuantized); + auto expected_read_only_model = expected_fb_model->GetModel(); + ModelT expected_model; + expected_read_only_model->UnPackTo(&expected_model); + + ExpectSameModels(model_, expected_model); +} + +class QuantizeSVDFTest : public QuantizeModelTest { + protected: + QuantizeSVDFTest() { + input_model_ = ReadModel(::mlir::lite::internal::kSvdfCalibrated); + readonly_model_ = input_model_->GetModel(); + model_ = UnPackFlatBufferModel(*readonly_model_); + } +}; + +TEST_F(QuantizeSVDFTest, VerifySVDF) { + // Quantize model. + TF_ASSERT_OK(QuantizeModelAllOperators( + &model_, TensorType_INT8, TensorType_INT8, + /*allow_float=*/false, TensorType_INT8, output_buffer_)); + + // Read expected model. + auto expected_fb_model = ReadModel(::mlir::lite::internal::kSvdfQuantized); + auto expected_read_only_model = expected_fb_model->GetModel(); + ModelT expected_model; + expected_read_only_model->UnPackTo(&expected_model); + + ExpectSameModels(model_, expected_model); +} + +class QuantizeFCTest : public QuantizeModelTest, + public testing::WithParamInterface { + protected: + QuantizeFCTest() { + disable_per_channel_quantization_for_dense_ = GetParam(); + input_model_ = ReadModel(::mlir::lite::internal::kModelWithFCOp); + readonly_model_ = input_model_->GetModel(); + model_ = UnPackFlatBufferModel(*readonly_model_); + } + + bool disable_per_channel_quantization_for_dense_; +}; + +TEST_P(QuantizeFCTest, VerifyFC8x8) { + TF_ASSERT_OK(QuantizeModelAllOperators( + &model_, TensorType_INT8, TensorType_INT8, + /*allow_float=*/false, TensorType_INT8, output_buffer_)); + + const auto& subgraph = model_.subgraphs[0]; + auto op = subgraph->operators[0].get(); + ASSERT_THAT(GetBuiltinCode(model_.operator_codes[op->opcode_index].get()), + Eq(BuiltinOperator_FULLY_CONNECTED)); + + ASSERT_THAT(op->inputs, SizeIs(3)); + ASSERT_THAT(op->outputs, SizeIs(1)); + + auto float_graph = readonly_model_->subgraphs()->Get(0); + // Verify FC input tensor and weight are int8 quantized. + auto float_op = float_graph->operators()->Get(0); + ASSERT_THAT(float_graph->tensors()->Get(float_op->inputs()->Get(0))->type(), + Eq(TensorType_FLOAT32)); + EXPECT_THAT(subgraph->tensors[op->inputs[0]].get()->type, + Eq(TensorType_INT8)); + ASSERT_THAT(float_graph->tensors()->Get(float_op->inputs()->Get(1))->type(), + Eq(TensorType_FLOAT32)); + EXPECT_THAT(subgraph->tensors[op->inputs[1]].get()->type, + Eq(TensorType_INT8)); + + // Verify FC bias should be int32 quantized. + ASSERT_THAT(float_graph->tensors()->Get(float_op->inputs()->Get(2))->type(), + Eq(TensorType_FLOAT32)); + EXPECT_THAT(subgraph->tensors[op->inputs[2]].get()->type, + Eq(TensorType_INT32)); + + // The output tensor of FC should be int8 quantized. + ASSERT_THAT(float_graph->tensors()->Get(float_op->outputs()->Get(0))->type(), + Eq(TensorType_FLOAT32)); + EXPECT_THAT(subgraph->tensors[op->outputs[0]].get()->type, + Eq(TensorType_INT8)); + + // Check op and versioning. + EXPECT_THAT(model_.operator_codes, SizeIs(1)); + EXPECT_THAT(GetBuiltinCode(model_.operator_codes[0].get()), + Eq(BuiltinOperator_FULLY_CONNECTED)); + ASSERT_THAT(model_.operator_codes[0]->version, Eq(5)); + + // check the scale + auto float_input_quant_params = + float_graph->tensors()->Get(op->inputs[0])->quantization(); + auto input_quant_params = + subgraph->tensors[op->inputs[0]]->quantization.get(); + VerifyQuantizationScale(*float_input_quant_params, *input_quant_params, + /*bit_num=*/8, /*symmetric=*/false); +} + +TEST_P(QuantizeFCTest, VerifyFCFor16x8) { + TF_ASSERT_OK(QuantizeModelAllOperators( + &model_, TensorType_INT8, TensorType_INT8, + /*allow_float=*/false, TensorType_INT16, output_buffer_)); + + const std::unique_ptr& subgraph = model_.subgraphs[0]; + const tflite::OperatorT* op = subgraph->operators[0].get(); + ASSERT_THAT(GetBuiltinCode(model_.operator_codes[op->opcode_index].get()), + Eq(BuiltinOperator_FULLY_CONNECTED)); + + ASSERT_THAT(op->inputs, SizeIs(3)); + ASSERT_THAT(op->outputs, SizeIs(1)); + + const SubGraph* float_graph = readonly_model_->subgraphs()->Get(0); + // Verify FC input tensor and weight are int16 and int8 quantized. + const Operator* float_op = float_graph->operators()->Get(0); + ASSERT_THAT(float_graph->tensors()->Get(float_op->inputs()->Get(0))->type(), + Eq(TensorType_FLOAT32)); + EXPECT_THAT(subgraph->tensors[op->inputs[0]].get()->type, + Eq(TensorType_INT16)); + ASSERT_THAT(float_graph->tensors()->Get(float_op->inputs()->Get(1))->type(), + Eq(TensorType_FLOAT32)); + EXPECT_THAT(subgraph->tensors[op->inputs[1]].get()->type, + Eq(TensorType_INT8)); + + // Verify FC bias should be int32 quantized. + ASSERT_THAT(float_graph->tensors()->Get(float_op->inputs()->Get(2))->type(), + Eq(TensorType_FLOAT32)); + EXPECT_THAT(subgraph->tensors[op->inputs[2]].get()->type, + Eq(TensorType_INT32)); + + // The output tensor of FC should be quantized as int16. + ASSERT_THAT(float_graph->tensors()->Get(float_op->outputs()->Get(0))->type(), + Eq(TensorType_FLOAT32)); + EXPECT_THAT(subgraph->tensors[op->outputs[0]].get()->type, + Eq(TensorType_INT16)); + + // Check op and versioning. + EXPECT_THAT(model_.operator_codes, SizeIs(1)); + EXPECT_THAT(GetBuiltinCode(model_.operator_codes[0].get()), + Eq(BuiltinOperator_FULLY_CONNECTED)); + ASSERT_THAT(model_.operator_codes[0]->version, Eq(11)); + + // Check the scale value. The scale value will be smaller than the int8 scale + // since the scale is calculated by dividing by 2^bit_num. + const int32_t target_input_index = op->inputs[0]; + const QuantizationParameters* float_input_quant_params = + float_graph->tensors()->Get(target_input_index)->quantization(); + const QuantizationParametersT* input_quant_params = + subgraph->tensors[target_input_index]->quantization.get(); + VerifyQuantizationScale(*float_input_quant_params, *input_quant_params, + /*bit_num=*/16, /*symmetric=*/true); +} + +TEST_P(QuantizeFCTest, VerifyDisablePerChannelQuantization) { + TF_ASSERT_OK(QuantizeModelAllOperators( + &model_, TensorType_INT8, TensorType_INT8, + /*allow_float=*/false, TensorType_INT8, output_buffer_, + /*disable_per_channel_for_dense_layers=*/ + disable_per_channel_quantization_for_dense_)); + const auto& subgraph = model_.subgraphs[0]; + auto fc_op = subgraph->operators[0].get(); + + ASSERT_THAT(fc_op->inputs, SizeIs(3)); + ASSERT_THAT(fc_op->outputs, SizeIs(1)); + + const int input_tensor_idx = 0; + const int weights_tensor_idx = 1; + const int bias_tensor_index = 2; + const int output_tensor_idx = 0; + const auto bias_tensor = + subgraph->tensors[fc_op->inputs[bias_tensor_index]].get(); + const auto input_tensor = + subgraph->tensors[fc_op->inputs[input_tensor_idx]].get(); + const auto weights_tensor = + subgraph->tensors[fc_op->inputs[weights_tensor_idx]].get(); + const auto output_tensor = + subgraph->tensors[fc_op->outputs[output_tensor_idx]].get(); + + EXPECT_THAT(bias_tensor->type, Eq(TensorType_INT32)); + EXPECT_THAT(input_tensor->type, Eq(TensorType_INT8)); + EXPECT_THAT(weights_tensor->type, Eq(TensorType_INT8)); + EXPECT_THAT(output_tensor->type, Eq(TensorType_INT8)); + + ASSERT_TRUE(weights_tensor->quantization); + ASSERT_TRUE(bias_tensor->quantization); + ASSERT_TRUE(weights_tensor->quantization); + const std::vector& bias_scales = bias_tensor->quantization->scale; + const std::vector& weights_scales = + weights_tensor->quantization->scale; + const std::vector& weights_zero_points = + weights_tensor->quantization->zero_point; + + const int out_channel_size = 2; + ASSERT_THAT(bias_scales, SizeIs(disable_per_channel_quantization_for_dense_ + ? 1 + : out_channel_size)); + ASSERT_THAT(weights_scales, SizeIs(disable_per_channel_quantization_for_dense_ + ? 1 + : out_channel_size)); + ASSERT_THAT( + weights_zero_points, + SizeIs(disable_per_channel_quantization_for_dense_ ? 1 + : out_channel_size)); + ASSERT_THAT(input_tensor->quantization->scale, SizeIs(1)); + ASSERT_THAT(output_tensor->quantization->scale, SizeIs(1)); + + const float eps = 1e-7; + + // Bias scale should be input * per_channel_weight_scale. + for (size_t i = 0; i < out_channel_size; i++) { + EXPECT_THAT((disable_per_channel_quantization_for_dense_ ? bias_scales[0] + : bias_scales[i]), + FloatNear(input_tensor->quantization->scale[0] * + (disable_per_channel_quantization_for_dense_ + ? weights_scales[0] + : weights_scales[i]), + eps)); + } + + const auto bias_buffer = model_.buffers[bias_tensor->buffer].get(); + auto control_size = sizeof(int32_t) * bias_tensor->shape[0]; + + ASSERT_THAT(bias_buffer->data, SizeIs(control_size)); + const auto float_op = + readonly_model_->subgraphs()->Get(0)->operators()->Get(0); + const auto original_bias_tensor = + readonly_model_->subgraphs()->Get(0)->tensors()->Get( + float_op->inputs()->Get(2)); + ASSERT_THAT(bias_buffer->data, SizeIs(control_size)); + const auto original_bias_buffer = + readonly_model_->buffers()->Get(original_bias_tensor->buffer()); + const float* bias_float_buffer = + reinterpret_cast(original_bias_buffer->data()->data()); + + int32_t* bias_values = reinterpret_cast(bias_buffer->data.data()); + for (size_t i = 0; i < out_channel_size; i++) { + const float bias_scale = disable_per_channel_quantization_for_dense_ + ? bias_scales[0] + : bias_scales[i]; + auto dequantized_value = bias_values[i] * bias_scale; + EXPECT_THAT(dequantized_value, + FloatNear(bias_float_buffer[i], bias_scale / 2)); + } + + const auto weights_buffer = model_.buffers[weights_tensor->buffer].get(); + const auto original_weights_tensor = + readonly_model_->subgraphs()->Get(0)->tensors()->Get( + float_op->inputs()->Get(1)); + const auto original_weights_buffer = + readonly_model_->buffers()->Get(original_weights_tensor->buffer()); + const int8_t* weight_values = + reinterpret_cast(weights_buffer->data.data()); + const float* weights_float_buffer = + reinterpret_cast(original_weights_buffer->data()->data()); + ASSERT_THAT(sizeof(float) * weights_buffer->data.size(), + Eq(original_weights_buffer->data()->size())); + int num_values_in_channel = weights_buffer->data.size() / out_channel_size; + for (size_t channel_idx = 0; channel_idx < out_channel_size; channel_idx++) { + for (size_t j = 0; j < num_values_in_channel; j++) { + size_t element_idx = channel_idx * num_values_in_channel + j; + auto scale = disable_per_channel_quantization_for_dense_ + ? weights_scales[0] + : weights_scales[channel_idx]; + auto zero_point = disable_per_channel_quantization_for_dense_ + ? weights_zero_points[0] + : weights_zero_points[channel_idx]; + auto dequantized_value = weight_values[element_idx] * scale; + EXPECT_THAT(dequantized_value, + FloatNear(weights_float_buffer[element_idx], scale / 2)); + EXPECT_THAT(zero_point, Eq(0)); + } + } + + // check op and versioning. + EXPECT_THAT(model_.operator_codes, SizeIs(1)); + EXPECT_THAT(GetBuiltinCode(model_.operator_codes[0].get()), + Eq(BuiltinOperator_FULLY_CONNECTED)); + ASSERT_THAT(model_.operator_codes[0]->version, 5); +} + +INSTANTIATE_TEST_SUITE_P(QuantizeFCTestInst, QuantizeFCTest, testing::Bool()); + +class QuantizeCustomOpTest + : public QuantizeModelTest, + public ::testing::WithParamInterface { + protected: + QuantizeCustomOpTest() { + input_model_ = ReadModel(::mlir::lite::internal::kModelMixed); + readonly_model_ = input_model_->GetModel(); + model_ = UnPackFlatBufferModel(*readonly_model_); + } +}; + +TEST_P(QuantizeCustomOpTest, VerifyMixedQuantization) { + TF_ASSERT_OK(QuantizeModelAllOperators(&model_, GetParam(), GetParam(), + /*allow_float=*/true, GetParam(), + output_buffer_)); + const auto& subgraph = model_.subgraphs[0]; + auto float_graph = readonly_model_->subgraphs()->Get(0); + // The original model reshape->custom->custom->squeeze. + ASSERT_THAT(*float_graph->operators(), SizeIs(4)); + // The resulting model should be: + // reshape->dequantize->custom->custom->quantize->squeeze. + ASSERT_THAT(subgraph->operators, SizeIs(6)); + const std::vector op_codes = { + BuiltinOperator_RESHAPE, BuiltinOperator_DEQUANTIZE, + BuiltinOperator_CUSTOM, BuiltinOperator_CUSTOM, + BuiltinOperator_QUANTIZE, BuiltinOperator_SQUEEZE}; + const std::vector op_input_types = { + GetParam(), GetParam(), TensorType_FLOAT32, + TensorType_FLOAT32, TensorType_FLOAT32, GetParam()}; + for (int i = 0; i < subgraph->operators.size(); ++i) { + OperatorT* op = subgraph->operators[i].get(); + ASSERT_THAT(GetBuiltinCode(model_.operator_codes[op->opcode_index].get()), + Eq(op_codes[i])); + ASSERT_THAT(subgraph->tensors[op->inputs[0]]->type, Eq(op_input_types[i])); + } +} + +INSTANTIATE_TEST_SUITE_P(QuantizeCustomOpTest, QuantizeCustomOpTest, + ::testing::Values(TensorType_INT8)); + +class QuantizePackTest : public QuantizeModelTest { + protected: + QuantizePackTest() { + input_model_ = ReadModel(::mlir::lite::internal::kModelPack); + readonly_model_ = input_model_->GetModel(); + model_ = UnPackFlatBufferModel(*readonly_model_); + } +}; + +TEST_F(QuantizePackTest, VerifyPack) { + TF_ASSERT_OK(QuantizeModel(&model_, output_buffer_)); + + const auto subgraph = model_.subgraphs[0].get(); + + // The model should only have 3 inputs and 1 output. + EXPECT_THAT(subgraph->inputs, SizeIs(3)); + EXPECT_THAT(subgraph->outputs, SizeIs(1)); + + const auto& op1 = subgraph->operators[1].get(); + const auto& op2 = subgraph->operators[2].get(); + const auto& op3 = subgraph->operators[3].get(); + const auto& op4 = subgraph->operators[4].get(); + + ASSERT_THAT(GetBuiltinCode(model_.operator_codes[op1->opcode_index].get()), + Eq(BuiltinOperator_QUANTIZE)); + ASSERT_THAT(GetBuiltinCode(model_.operator_codes[op2->opcode_index].get()), + Eq(BuiltinOperator_QUANTIZE)); + ASSERT_THAT(GetBuiltinCode(model_.operator_codes[op3->opcode_index].get()), + Eq(BuiltinOperator_PACK)); + ASSERT_THAT(GetBuiltinCode(model_.operator_codes[op4->opcode_index].get()), + Eq(BuiltinOperator_DEQUANTIZE)); + + const auto& pack_input0 = subgraph->tensors[op3->inputs[0]].get(); + const auto& pack_input1 = subgraph->tensors[op3->inputs[1]].get(); + const auto& pack_input2 = subgraph->tensors[op3->inputs[2]].get(); + + const auto& pack_output = subgraph->tensors[op3->outputs[0]].get(); + + // Check quantization parameters for input and output. + EXPECT_THAT(pack_input0->quantization->scale[0], + FloatEq(pack_input1->quantization->scale[0])); + EXPECT_THAT(pack_input1->quantization->scale[0], + FloatEq(pack_input2->quantization->scale[0])); + EXPECT_THAT(pack_input0->quantization->zero_point[0], + Eq(pack_input1->quantization->zero_point[0])); + EXPECT_THAT(pack_input1->quantization->zero_point[0], + Eq(pack_input2->quantization->zero_point[0])); + + EXPECT_THAT(pack_input1->quantization->scale[0], + FloatEq(pack_output->quantization->scale[0])); + EXPECT_THAT(pack_input1->quantization->zero_point[0], + Eq(pack_output->quantization->zero_point[0])); + + // Check type of input and output. + EXPECT_THAT(pack_output->type, Eq(TensorType_INT8)); + EXPECT_THAT(pack_input0->type, Eq(TensorType_INT8)); + EXPECT_THAT(pack_input1->type, Eq(TensorType_INT8)); + EXPECT_THAT(pack_input2->type, Eq(TensorType_INT8)); +} + +class QuantizeMinimumMaximumTest + : public QuantizeModelTest, + public testing::WithParamInterface { + protected: + QuantizeMinimumMaximumTest() { + input_model_ = ReadModel(GetParam()); + readonly_model_ = input_model_->GetModel(); + model_ = UnPackFlatBufferModel(*readonly_model_); + } +}; + +TEST_P(QuantizeMinimumMaximumTest, VerifyMinimumMaximum) { + TF_ASSERT_OK(QuantizeModel(&model_, output_buffer_)); + const auto& subgraph = model_.subgraphs[0]; + // Check that the first op is Quantize and the last is Dequant. + const auto& quant_op = subgraph->operators[0]; + const auto& dequant_op = subgraph->operators[subgraph->operators.size() - 1]; + const int32_t quant_idx = quant_op->opcode_index; + const int32_t dequant_idx = dequant_op->opcode_index; + EXPECT_THAT(GetBuiltinCode(model_.operator_codes[quant_idx].get()), + Eq(BuiltinOperator_QUANTIZE)); + EXPECT_THAT(GetBuiltinCode(model_.operator_codes[dequant_idx].get()), + Eq(BuiltinOperator_DEQUANTIZE)); + + const auto& op = subgraph->operators[1].get(); + + // Check that we have MINIMUM or MAXIMUM operator. + auto op_builtin_code = + GetBuiltinCode(model_.operator_codes[op->opcode_index].get()); + ASSERT_TRUE(op_builtin_code == tflite::BuiltinOperator_MINIMUM || + op_builtin_code == tflite::BuiltinOperator_MAXIMUM); + + // Check that we have two inputs and one output. + ASSERT_THAT(op->inputs, SizeIs(2)); + ASSERT_THAT(op->outputs, SizeIs(1)); + + // Check that all is quantized. + auto output = subgraph->tensors[op->outputs[0]].get(); + auto input1 = subgraph->tensors[op->inputs[0]].get(); + auto input2 = subgraph->tensors[op->inputs[1]].get(); + + EXPECT_THAT(output->type, Eq(TensorType_INT8)); + EXPECT_THAT(input1->type, Eq(TensorType_INT8)); + EXPECT_THAT(input2->type, Eq(TensorType_INT8)); + + // Check if the quantization params of the minimum/maximum inputs match + // after requantization + EXPECT_THAT(input1->quantization->scale, Eq(input2->quantization->scale)); + EXPECT_THAT(input1->quantization->zero_point, + Eq(input2->quantization->zero_point)); + + // Check the input quantization params match the output ones. + EXPECT_THAT(output->quantization->scale, Eq(input1->quantization->scale)); + EXPECT_THAT(output->quantization->zero_point, + Eq(input1->quantization->zero_point)); + EXPECT_THAT(output->quantization->scale, Eq(input2->quantization->scale)); + EXPECT_THAT(output->quantization->zero_point, + Eq(input2->quantization->zero_point)); +} + +INSTANTIATE_TEST_SUITE_P( + MinimumMaximumTestInst, QuantizeMinimumMaximumTest, + testing::ValuesIn({::mlir::lite::internal::kModelWithMinimumOp, + ::mlir::lite::internal::kModelWithMaximumOp})); + +class QuantizeUnpackTest : public QuantizeModelTest { + protected: + QuantizeUnpackTest() { + input_model_ = ReadModel(::mlir::lite::internal::kModelWithUnpack); + readonly_model_ = input_model_->GetModel(); + model_ = UnPackFlatBufferModel(*readonly_model_); + } +}; + +TEST_F(QuantizeUnpackTest, VerifyUnpack) { + TF_ASSERT_OK(QuantizeModel(&model_, output_buffer_)); + + const auto subgraph = model_.subgraphs[0].get(); + auto op = subgraph->operators[1].get(); + + auto float_graph = readonly_model_->subgraphs()->Get(0); + + ASSERT_THAT(GetBuiltinCode(model_.operator_codes[op->opcode_index].get()), + Eq(BuiltinOperator_UNPACK)); + + // Get unpack input and output tensors + auto unpack_input = subgraph->tensors[op->inputs[0]].get(); + auto unpack_output_0 = subgraph->tensors[op->outputs[0]].get(); + auto unpack_output_1 = subgraph->tensors[op->outputs[1]].get(); + + // Verify Unpack input is quantized. + ASSERT_THAT(float_graph->tensors()->Get(op->inputs[0])->type(), + Eq(TensorType_FLOAT32)); + EXPECT_THAT(unpack_input->type, Eq(TensorType_INT8)); + + // The model should only have one input and 2 outputs. + EXPECT_THAT(subgraph->inputs, SizeIs(1)); + EXPECT_THAT(subgraph->outputs, SizeIs(2)); + + // Ensure quantization parameters before and after unpack + // are preserved after quantization for all outputs of + // unpack. + EXPECT_THAT(unpack_input->quantization->scale[0], + FloatEq(unpack_output_0->quantization->scale[0])); + EXPECT_THAT(unpack_input->quantization->scale[0], + FloatEq(unpack_output_1->quantization->scale[0])); + EXPECT_THAT(unpack_input->quantization->zero_point[0], + Eq(unpack_output_0->quantization->zero_point[0])); + EXPECT_THAT(unpack_input->quantization->zero_point[0], + Eq(unpack_output_1->quantization->zero_point[0])); +} + +class QuantizeBroadcastToModelTest + : public QuantizeModelTest, + public testing::WithParamInterface { + protected: + QuantizeBroadcastToModelTest() { + tensor_type_ = GetParam(); + input_model_ = ReadModel(::mlir::lite::internal::kModelWithBroadcastToOp); + readonly_model_ = input_model_->GetModel(); + model_ = UnPackFlatBufferModel(*readonly_model_); + } + TensorType tensor_type_; +}; + +INSTANTIATE_TEST_SUITE_P(QuantizeBroadcastToModelTestInst, + QuantizeBroadcastToModelTest, + testing::ValuesIn({TensorType_INT8})); + +TEST_P(QuantizeBroadcastToModelTest, VerifyBroadcastToQuantization) { + TF_EXPECT_OK(QuantizeModelAllOperators(&model_, tensor_type_, tensor_type_, + /*allow_float=*/false, tensor_type_, + output_buffer_)); + + // There is only one subgraph. + const int32_t subgraph_idx = 0; + const auto& subgraph = model_.subgraphs[subgraph_idx]; + const auto& readonly_subgraph = + readonly_model_->subgraphs()->Get(subgraph_idx); + + // There should be a single broadcast_to op. + EXPECT_THAT(*readonly_subgraph->operators(), SizeIs(1)); + EXPECT_THAT(subgraph->operators, SizeIs(1)); + const auto& broadcast_to = subgraph->operators[0]; + EXPECT_THAT(model_.operator_codes[broadcast_to->opcode_index]->builtin_code, + Eq(BuiltinOperator_BROADCAST_TO)); + + // There should be 3 tensors: input, output, and BroadcastTo/shape. + EXPECT_THAT(subgraph->tensors, SizeIs(3)); + + // Input Tensor + EXPECT_THAT(subgraph->tensors[0]->type, Eq(tensor_type_)); + EXPECT_THAT(subgraph->tensors[0]->name, Eq("input_1")); + EXPECT_THAT(subgraph->tensors[0]->quantization->scale, SizeIs(1)); + EXPECT_THAT(subgraph->tensors[0]->quantization->zero_point, SizeIs(1)); + + // Output Tensor. The name given in the generated + // .bin test file is 'Identity' and should be preserved + EXPECT_THAT(subgraph->tensors[2]->type, Eq(tensor_type_)); + EXPECT_THAT(subgraph->tensors[2]->name, Eq("Identity")); + EXPECT_THAT(subgraph->tensors[2]->quantization->scale, SizeIs(1)); + EXPECT_THAT(subgraph->tensors[2]->quantization->zero_point, SizeIs(1)); + + // The BroadCastTo shape is of type INT32 and should not be quantized + EXPECT_THAT(subgraph->tensors[1]->type, Eq(TensorType_INT32)); + EXPECT_THAT(subgraph->tensors[1]->quantization->scale, IsEmpty()); + EXPECT_THAT(subgraph->tensors[1]->quantization->zero_point, IsEmpty()); + + // check op and versioning. + EXPECT_THAT(model_.operator_codes, SizeIs(1)); + EXPECT_THAT(model_.operator_codes[0]->builtin_code, + Eq(BuiltinOperator_BROADCAST_TO)); + ASSERT_THAT(model_.operator_codes[0]->version, Eq(3)); +} + +class QuantizeGatherNDModelTest + : public QuantizeModelTest, + public testing::WithParamInterface { + protected: + QuantizeGatherNDModelTest() { + tensor_type_ = GetParam(); + input_model_ = ReadModel(::mlir::lite::internal::kModelWithGatherNDOp); + readonly_model_ = input_model_->GetModel(); + model_ = UnPackFlatBufferModel(*readonly_model_); + } + + TensorType tensor_type_; +}; + +INSTANTIATE_TEST_SUITE_P(QuantizeGatherNDModelTestInst, + QuantizeGatherNDModelTest, + testing::ValuesIn({TensorType_INT8})); + +TEST_P(QuantizeGatherNDModelTest, QuantizeGatherND) { + TF_EXPECT_OK(QuantizeModelAllOperators(&model_, tensor_type_, tensor_type_, + /*allow_float=*/false, tensor_type_, + output_buffer_)); + + // There is only one subgraph. + const int32_t subgraph_idx = 0; + const auto& subgraph = model_.subgraphs[subgraph_idx]; + const auto& readonly_subgraph = + readonly_model_->subgraphs()->Get(subgraph_idx); + + // There should be a single gather_nd op. + EXPECT_THAT(*readonly_subgraph->operators(), SizeIs(1)); + EXPECT_THAT(subgraph->operators, SizeIs(1)); + const auto& gather_nd = subgraph->operators[0]; + EXPECT_THAT(model_.operator_codes[gather_nd->opcode_index]->builtin_code, + Eq(BuiltinOperator_GATHER_ND)); + + // There should be 3 tensors: input, output, and indices. + EXPECT_THAT(subgraph->tensors, SizeIs(3)); + + // Input Tensor + EXPECT_THAT(subgraph->tensors[0]->type, Eq(tensor_type_)); + EXPECT_THAT(subgraph->tensors[0]->name, Eq("input")); + EXPECT_THAT(subgraph->tensors[0]->quantization->scale, SizeIs(1)); + EXPECT_THAT(subgraph->tensors[0]->quantization->zero_point, SizeIs(1)); + + // Output Tensor + EXPECT_THAT(subgraph->tensors[2]->type, Eq(tensor_type_)); + EXPECT_THAT(subgraph->tensors[2]->name, Eq("output")); + EXPECT_THAT(subgraph->tensors[2]->quantization->scale, SizeIs(1)); + EXPECT_THAT(subgraph->tensors[2]->quantization->zero_point, SizeIs(1)); + + // The gather indices are of type INT32 and should not be quantized + EXPECT_THAT(subgraph->tensors[1]->type, Eq(TensorType_INT32)); + EXPECT_THAT(subgraph->tensors[1]->quantization->scale, IsEmpty()); + EXPECT_THAT(subgraph->tensors[1]->quantization->zero_point, IsEmpty()); + + // Check op and versioning. + EXPECT_THAT(model_.operator_codes, SizeIs(1)); + EXPECT_THAT(model_.operator_codes[0]->builtin_code, + Eq(BuiltinOperator_GATHER_ND)); + ASSERT_THAT(model_.operator_codes[0]->version, Eq(1)); +} + +class QuantizeWhereModelTest : public QuantizeModelTest { + protected: + QuantizeWhereModelTest() { + input_model_ = ReadModel(::mlir::lite::internal::kModelWithWhereOp); + readonly_model_ = input_model_->GetModel(); + model_ = UnPackFlatBufferModel(*readonly_model_); + } +}; + +TEST_F(QuantizeWhereModelTest, QuantizeWhere) { + // Where operator takes a BOOL tensor as input + // and outputs INT64 indices, both of which + // should not be quantized + TF_EXPECT_OK(QuantizeModel(&model_, TensorType_BOOL, TensorType_INT64, + output_buffer_)); + + // There is only one subgraph. + const int32_t subgraph_idx = 0; + const auto& subgraph = model_.subgraphs[subgraph_idx]; + const auto& readonly_subgraph = + readonly_model_->subgraphs()->Get(subgraph_idx); + + // There should be a single where op. + EXPECT_THAT(*readonly_subgraph->operators(), SizeIs(1)); + EXPECT_THAT(subgraph->operators, SizeIs(1)); + const auto& where = subgraph->operators[0]; + EXPECT_THAT(model_.operator_codes[where->opcode_index]->builtin_code, + Eq(BuiltinOperator_WHERE)); + + // There should be 2 tensors: input and output. + EXPECT_THAT(subgraph->tensors, SizeIs(2)); + + // Testing input tensor type and ensuring it + // was not quantized + EXPECT_THAT(subgraph->tensors[0]->type, Eq(TensorType_BOOL)); + EXPECT_THAT(subgraph->tensors[0]->name, Eq("input")); + EXPECT_THAT(subgraph->tensors[0]->quantization->scale, IsEmpty()); + EXPECT_THAT(subgraph->tensors[0]->quantization->zero_point, IsEmpty()); + + // Testing output (indices) tensor type and ensuring it + // was not quantized + EXPECT_THAT(subgraph->tensors[1]->type, Eq(TensorType_INT64)); + EXPECT_THAT(subgraph->tensors[1]->name, Eq("indices")); + EXPECT_THAT(subgraph->tensors[1]->quantization->scale, IsEmpty()); + EXPECT_THAT(subgraph->tensors[1]->quantization->zero_point, IsEmpty()); + + // check op and versioning. + EXPECT_THAT(model_.operator_codes, SizeIs(1)); + EXPECT_THAT(model_.operator_codes[0]->builtin_code, + Eq(BuiltinOperator_WHERE)); + ASSERT_THAT(model_.operator_codes[0]->version, Eq(1)); +} + +} // namespace +} // namespace optimize +} // namespace tflite + +int main(int argc, char** argv) { + std::string model_file; + const std::vector flag_list = { + tensorflow::Flag("test_model_file", &model_file, + "Path to test tflite model file."), + }; + + const bool parse_result = tensorflow::Flags::Parse(&argc, argv, flag_list); + if (!parse_result) { + std::cerr << "Required test_model_file\n"; + std::abort(); + } + g_test_model_dir = new std::string(tensorflow::io::Dirname(model_file)); + ::tensorflow::port::InitMain(argv[0], &argc, &argv); + return RUN_ALL_TESTS(); +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/quantize_weights.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/quantize_weights.cc new file mode 100644 index 0000000000000000000000000000000000000000..12546040f435009b6b82a650547921fe290eb89e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/quantize_weights.cc @@ -0,0 +1,237 @@ +/* Copyright 2022 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/compiler/mlir/lite/quantization/lite/quantize_weights.h" + +#include +#include +#include +#include + +#include "absl/container/flat_hash_set.h" +#include "absl/log/log.h" +#include "absl/status/status.h" +#include "absl/strings/string_view.h" +#include "flatbuffers/buffer.h" // from @flatbuffers +#include "flatbuffers/flatbuffer_builder.h" // from @flatbuffers +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/Twine.h" +#include "llvm/Support/Debug.h" +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/Location.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/OwningOpRef.h" // from @llvm-project +#include "mlir/Pass/PassManager.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/common/tfl_pass_config.h" +#include "tensorflow/compiler/mlir/lite/flatbuffer_export.h" +#include "tensorflow/compiler/mlir/lite/flatbuffer_import.h" +#include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_config.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" +#include "tensorflow/compiler/mlir/lite/tf_tfl_passes.h" +#include "tensorflow/compiler/mlir/lite/transforms/passes.h" +#include "tensorflow/compiler/mlir/lite/utils/convert_type.h" +#include "tensorflow/compiler/mlir/tensorflow/utils/error_util.h" +#include "tensorflow/core/framework/types.pb.h" + +namespace mlir { +namespace lite { + +namespace { + +using llvm::StringRef; + +// Convert op represented in TFLite builtin_code to its corresponding MLIR +// OperationName. +void TfLiteBuiltinOpToMlir(const BuiltinOperatorSet& tflite_builtin_codes, + absl::flat_hash_set& mlir_op_names) { + for (const auto& entry : tflite_builtin_codes) { + StringRef tflite_op_name = EnumNameBuiltinOperator(entry); + std::string mlir_name = llvm::Twine("tfl.", tflite_op_name.lower()).str(); + mlir_op_names.insert(std::move(mlir_name)); + } +} + +std::string TfLiteToMlir(absl::string_view tflite_op_name) { + StringRef op_name(tflite_op_name.data(), tflite_op_name.size()); + return op_name.lower(); +} + +std::unique_ptr CreateMutableModelFromFile( + const tflite::Model* input_model) { + auto copied_model = std::make_unique(); + input_model->UnPackTo(copied_model.get(), nullptr); + return copied_model; +} +} // namespace + +// TODO(b/214314076): Support MLIR model as an input for the C++ dynamic range +// quantization API +absl::Status QuantizeWeights( + flatbuffers::FlatBufferBuilder* builder, const tflite::Model* input_model, + const tflite::TensorType& inference_type, + const absl::flat_hash_set& denylisted_ops, + const CustomOpMap& custom_op_map, int64_t minimum_elements_for_weights, + bool disable_per_channel, bool weight_only_quantization, + bool legacy_float_scale) { + // Translate TFLite names to mlir op names. + absl::flat_hash_set denylisted_mlir_op_names; + for (auto& entry : denylisted_ops) { + denylisted_mlir_op_names.insert(TfLiteToMlir(entry)); + } + + DialectRegistry registry; + MLIRContext context(registry); + StatusScopedDiagnosticHandler statusHandler(&context, + /*propagate=*/true); + + // Import input_model to a MLIR module + flatbuffers::FlatBufferBuilder input_builder; + flatbuffers::Offset input_model_location = tflite::Model::Pack( + input_builder, CreateMutableModelFromFile(input_model).get()); + tflite::FinishModelBuffer(input_builder, input_model_location); + + std::string serialized_model( + reinterpret_cast(input_builder.GetBufferPointer()), + input_builder.GetSize()); + + OwningOpRef module = tflite::FlatBufferToMlir( + serialized_model, &context, UnknownLoc::get(&context)); + + // Apply quantization passes. + PassManager pm((*module)->getName(), OpPassManager::Nesting::Implicit); + TFL::QuantizationSpecs quant_specs; + quant_specs.inference_type = tflite::TflTypeToTfType(inference_type); + quant_specs.weight_quantization = true; + quant_specs.weight_only_quantization = weight_only_quantization; + quant_specs.minimum_elements_for_weights = minimum_elements_for_weights; + quant_specs.disable_per_channel = disable_per_channel; + quant_specs.legacy_float_scale = legacy_float_scale; + quant_specs.ops_blocklist = denylisted_mlir_op_names; + for (const auto& entry : custom_op_map) { + quant_specs.custom_map[entry.first].quantizable_input_indices = + entry.second.quantizable_input_indices; + quant_specs.custom_map[entry.first].is_weight_only = + entry.second.is_weight_only; + quant_specs.custom_map[entry.first].no_side_effect = + entry.second.no_side_effect; + } + + if (quant_specs.inference_type == tensorflow::DT_INT8) + quant_specs.inference_type = tensorflow::DT_QINT8; + + if (!(quant_specs.inference_type == tensorflow::DT_HALF || + quant_specs.inference_type == tensorflow::DT_QINT8)) { + LOG(ERROR) << "Couldn't apply dynamic range quantization since unsupported " + "inference_type is passed."; + return absl::InvalidArgumentError( + "Quantize weights transformation failed."); + } + + llvm::dbgs() << "weight_quantization: " << true + << ", weight_only_quantization: " + << quant_specs.weight_only_quantization << ", mlir_quantizer: " + << quant_specs.enable_mlir_dynamic_range_quantizer + << ", inference_type: " << quant_specs.inference_type << "\n"; + Builder mlir_builder(&context); + + tensorflow::AddDynamicRangeQuantizationPasses( + mlir::TFL::PassConfig(quant_specs), pm); + + if (failed(pm.run(module.get()))) { + absl::string_view err = statusHandler.ConsumeStatus().message(); + LOG(ERROR) << "Failed to quantize: " << err; + return absl::InvalidArgumentError( + "Quantize weights transformation failed."); + } + + // Export the results to the builder + std::string result; + tflite::FlatbufferExportOptions options; + options.converter_flags.set_force_select_tf_ops(false); + options.converter_flags.set_enable_select_tf_ops(true); + options.converter_flags.set_allow_custom_ops(true); + if (!tflite::MlirToFlatBufferTranslateFunction(module.get(), options, + &result)) { + LOG(ERROR) << "Failed to export MLIR to flatbuffer."; + return absl::InvalidArgumentError( + "Quantize weights transformation failed."); + } + builder->PushFlatBuffer(reinterpret_cast(result.data()), + result.size()); + + return absl::OkStatus(); +} + +absl::Status QuantizeWeights(flatbuffers::FlatBufferBuilder* builder, + const tflite::Model* input_model, + int64_t weights_min_num_elements, + bool use_hybrid_evaluation) { + return QuantizeWeights( + builder, input_model, + /*inference_type=*/tflite::TensorType_INT8, + /*denylisted_ops=*/{}, + /*custom_op_map=*/{}, + /*minimum_elements_for_weights=*/weights_min_num_elements, + /*disable_per_channel=*/false, + /*weight_only_quantization=*/!use_hybrid_evaluation, + /*legacy_float_scale=*/true); +} + +// In MLIR use_updated_hybrid_scheme = true means per-channel operation. +absl::Status QuantizeWeights(flatbuffers::FlatBufferBuilder* builder, + const tflite::Model* input_model, + BufferType quant_type, + bool use_updated_hybrid_scheme) { + tflite::TensorType inference_type; + switch (quant_type) { + case BufferType::QUANTIZED_FLOAT16: + inference_type = tflite::TensorType_FLOAT16; + break; + default: + inference_type = tflite::TensorType_INT8; + } + return QuantizeWeights(builder, input_model, inference_type, + /*denylisted_ops=*/{}, + /*custom_op_map=*/{}, + /*minimum_elements_for_weights=*/1024, + /*disable_per_channel=*/!use_updated_hybrid_scheme, + /*weight_only_quantization=*/false, + /*legacy_float_scale=*/true); +} + +absl::Status QuantizeWeights(flatbuffers::FlatBufferBuilder* builder, + const tflite::Model* input_model, + int64_t weights_min_num_elements, + const CustomOpMap& custom_op_map, + bool use_updated_hybrid_scheme, + const BuiltinOperatorSet& op_denylist) { + const tflite::TensorType inference_type = tflite::TensorType_INT8; + + absl::flat_hash_set mlir_op_denylist; + TfLiteBuiltinOpToMlir(op_denylist, mlir_op_denylist); + + return QuantizeWeights( + builder, input_model, inference_type, + /*denylisted_ops=*/mlir_op_denylist, + /*custom_op_map=*/custom_op_map, + /*minimum_elements_for_weights=*/weights_min_num_elements, + /*disable_per_channel=*/!use_updated_hybrid_scheme, + /*weight_only_quantization=*/false, + /*legacy_float_scale=*/true); +} + +} // namespace lite +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/quantize_weights.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/quantize_weights.h new file mode 100644 index 0000000000000000000000000000000000000000..8bcf7256d319908820843d6b7e448bf890dc00cb --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/quantize_weights.h @@ -0,0 +1,91 @@ +/* Copyright 2022 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_LITE_QUANTIZE_WEIGHTS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_LITE_QUANTIZE_WEIGHTS_H_ + +#include +#include +#include +#include +#include +#include + +#include "absl/container/flat_hash_set.h" +#include "absl/status/status.h" +#include "flatbuffers/flatbuffer_builder.h" // from @flatbuffers +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" + +namespace mlir { +namespace lite { + +// Supported resulting types from quantization process. +enum class BufferType { QUANTIZED_INT8, QUANTIZED_FLOAT16 }; + +// Stores information about how to quantize a user-specified custom operation. +// CustomOpInfo contains info of its corresponding CustomOp registered in the +// CustomOpMap. 'quantizable_input_indices' is used to determine which indices +// of the CustomOp are quantizable. 'is_weight_only' is used specify whether the +// custom op is quantized only for storage and dequantized at runtime. +// 'no_side_effect' is used to determine whether the op can be pruned if +// considered as trivially dead. +struct CustomOpInfo { + std::vector quantizable_input_indices; + bool is_weight_only = false; + bool no_side_effect = true; +}; + +using BuiltinOperatorSet = absl::flat_hash_set; +// Map from custom op code to custom op quantization information. +using CustomOpMap = std::unordered_map; + +// Applies dynamic range quantization for the given model wehre the input_model +// type is flatbuffer but is converted to MLIR during quantization process and +// then converted back to flatbuffer for return. Note that this is part of +// reaching feature parity with the old quantizer for dynamic range +// quantization, specifically for +// third_party/tensorflow/lite/tools/optimize/quantize_weights.h. +// TODO(b/202468183): Selective quantization + quant debugger support for +// dynamic range quantization for verify_numeric and whole_model_verify flags. +absl::Status QuantizeWeights( + flatbuffers::FlatBufferBuilder* builder, const tflite::Model* input_model, + const tflite::TensorType& inference_type, + const absl::flat_hash_set& denylisted_ops, + const CustomOpMap& custom_op_map, + int64_t minimum_elements_for_weights = 1024, + bool disable_per_channel = false, bool weight_only_quantization = false, + bool legacy_float_scale = false); + +// Overloading methods to support old quantizer versions API +absl::Status QuantizeWeights(flatbuffers::FlatBufferBuilder* builder, + const tflite::Model* input_model, + int64_t weights_min_num_elements, + bool use_hybrid_evaluation = true); + +absl::Status QuantizeWeights(flatbuffers::FlatBufferBuilder* builder, + const tflite::Model* input_model, + BufferType quant_type = BufferType::QUANTIZED_INT8, + bool use_updated_hybrid_scheme = true); + +absl::Status QuantizeWeights(flatbuffers::FlatBufferBuilder* builder, + const tflite::Model* input_model, + int64_t weights_min_num_elements, + const CustomOpMap& custom_op_map, + bool use_updated_hybrid_scheme = true, + const BuiltinOperatorSet& op_denylist = {}); + +} // namespace lite +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_LITE_QUANTIZE_WEIGHTS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/quantize_weights_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/quantize_weights_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..cbee0694fc4da422d09b996044e0df858cde8121 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/quantize_weights_test.cc @@ -0,0 +1,783 @@ +/* Copyright 2022 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/compiler/mlir/lite/quantization/lite/quantize_weights.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include "absl/log/log.h" +#include "absl/status/status.h" +#include "flatbuffers/buffer.h" // from @flatbuffers +#include "flatbuffers/flatbuffer_builder.h" // from @flatbuffers +#include "flatbuffers/vector.h" // from @flatbuffers +#include "tensorflow/compiler/mlir/lite/core/absl_error_model_builder.h" +#include "tensorflow/compiler/mlir/lite/quantization/lite/test_util.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_utils.h" +#include "xla/tsl/platform/logging.h" +#include "tensorflow/core/platform/init_main.h" +#include "tensorflow/core/platform/path.h" +#include "tensorflow/core/platform/types.h" +#include "tensorflow/core/util/command_line_flags.h" + +// Note: branched from tensorflow/lite/tools/optimize/quantize_weights_test.cc + +namespace { +std::string* g_test_model_dir = nullptr; +} // namespace + +namespace tflite { +namespace optimize { +namespace { + +using mlir::lite::BufferType; +using mlir::lite::CustomOpMap; +using mlir::lite::QuantizeWeights; +using mlir::TFL::FlatBufferModelAbslError; +constexpr bool kUseUpdatedHybridSchemeDefault = true; + +std::unique_ptr CreateMutableModelFromFile(const Model* input_model) { + auto copied_model = std::make_unique(); + input_model->UnPackTo(copied_model.get(), nullptr); + return copied_model; +} + +std::unique_ptr ReadTestModel() { + auto model_path = tensorflow::io::JoinPath( + *g_test_model_dir, ::mlir::lite::internal::kConvModelWith0Plus10Weights); + return FlatBufferModelAbslError::BuildFromFile(model_path.c_str()); +} + +std::unique_ptr ReadSharedWeightsTestModel() { + auto model_path = tensorflow::io::JoinPath( + *g_test_model_dir, ::mlir::lite::internal::kModelWithSharedWeights); + return FlatBufferModelAbslError::BuildFromFile(model_path.c_str()); +} + +std::unique_ptr ReadGatherTestModel() { + auto model_path = tensorflow::io::JoinPath( + *g_test_model_dir, ::mlir::lite::internal::kQuantizedWithGather); + return FlatBufferModelAbslError::BuildFromFile(model_path.c_str()); +} + +std::unique_ptr ReadCustomOpTestModel() { + auto model_path = tensorflow::io::JoinPath( + *g_test_model_dir, ::mlir::lite::internal::kModelWithCustomOp); + return FlatBufferModelAbslError::BuildFromFile(model_path.c_str()); +} + +template +std::vector GetAsVector(const flatbuffers::Vector* vec) { + return std::vector(vec->begin(), vec->end()); +} + +class QuantizeWeightsTest : public testing::Test { + protected: + QuantizeWeightsTest() = default; + + void LoadBasicModel() { + input_model_ = ReadTestModel(); + model_ = input_model_->GetModel(); + } + + void LoadSharedWeightsModel() { + input_model_ = ReadSharedWeightsTestModel(); + model_ = input_model_->GetModel(); + } + + void LoadGatherTestModel() { + input_model_ = ReadGatherTestModel(); + model_ = input_model_->GetModel(); + } + + void LoadCustomOpTestModel() { + input_model_ = ReadCustomOpTestModel(); + model_ = input_model_->GetModel(); + } + + std::unique_ptr input_model_; + const Model* model_; + + bool IsModelInputOrOutput(const Model* model, uint32_t tensor_idx) { + for (size_t subgraph_idx = 0; subgraph_idx < model_->subgraphs()->size(); + ++subgraph_idx) { + const auto subgraph = model->subgraphs()->Get(subgraph_idx); + for (size_t i = 0; i < subgraph->inputs()->size(); ++i) { + if (subgraph->inputs()->Get(i) == tensor_idx) { + return true; + } + } + for (size_t i = 0; i < subgraph->outputs()->size(); ++i) { + if (subgraph->outputs()->Get(i) == tensor_idx) { + return true; + } + } + } + return false; + } + + // Returns the producer op code of the specified tensor_idx. + bool GetProducerOpCode(const Model* model, uint32_t subgraph_idx, + uint32_t tensor_idx, BuiltinOperator* op_code) { + const auto subgraph = model->subgraphs()->Get(subgraph_idx); + for (size_t op_idx = 0; op_idx < subgraph->operators()->size(); ++op_idx) { + const auto op = subgraph->operators()->Get(op_idx); + for (size_t i = 0; i < op->outputs()->size(); ++i) { + if (op->outputs()->Get(i) == tensor_idx) { + const uint32_t op_code_idx = op->opcode_index(); + *op_code = GetBuiltinCode(model->operator_codes()->Get(op_code_idx)); + return true; + } + } + } + return false; + } +}; + +// Returns true if everything between the two graphs +// are identical except for name field. +// Used when comparing graphs after optimization passes, +// as tensor names may have changed via MLIR constant folding process. +bool ExpectEqualTensor(const Tensor* tensor, const Tensor* expected_tensor) { + return (tensor->is_variable() == expected_tensor->is_variable()) && + (GetAsVector(tensor->shape()) == + GetAsVector(expected_tensor->shape())); +} + +// Finds the match of the quantized tensor from the possible tensors. Each +// possible tensors can be used only once. It checks shape and name if the +// tensor is quantized and also checks buffer contents and tensor type if not +// quantized. For the quantized case, tensor type and quantizaction params are +// expected to be checked in the test body with the match. +const Tensor* FindMatchingExpectedTensor( + const Model* quantized_model, const Model* expected_model, + const Tensor* quantized_tensor, + const flatbuffers::Vector>* possible_tensors, + std::vector& used_tensors, bool quantized = false) { + std::unique_ptr quant_model = + CreateMutableModelFromFile(quantized_model); + std::unique_ptr float_model = + CreateMutableModelFromFile(expected_model); + + for (int i = 0; i < possible_tensors->size(); i++) { + // Skip if the tensor is already used for match. + auto it = std::find(used_tensors.begin(), used_tensors.end(), i); + if (it != used_tensors.end()) continue; + + const Tensor* float_tensor = possible_tensors->Get(i); + + LOG(INFO) << quantized_tensor->name()->str() << " " + << float_tensor->name()->str(); + if (ExpectEqualTensor(quantized_tensor, float_tensor)) { + if (quantized && quantized_tensor->name()->str().find("weights")) { + // If tensor is quantized, data type and buffer contents can be + // different between float and quantized tensors. So do those tests + // separately in the test body without checking them here. + used_tensors.push_back(i); + return float_tensor; + } else { + // Otherwise, do additional checks for data type and buffer contents. + const std::vector quantized_buffer = + quant_model->buffers[quantized_tensor->buffer()].get()->data; + const std::vector float_buffer = + float_model->buffers[float_tensor->buffer()].get()->data; + if ((quantized_buffer == float_buffer) && + (quantized_tensor->type() == float_tensor->type())) { + used_tensors.push_back(i); + return float_tensor; + } + } + } + } + return nullptr; +} + +TEST_F(QuantizeWeightsTest, QuantizationSucceeds) { + LoadBasicModel(); + flatbuffers::FlatBufferBuilder builder; + EXPECT_OK(QuantizeWeights(&builder, model_, 0)); + + const uint8_t* buffer = builder.GetBufferPointer(); + const Model* output_model = GetModel(buffer); + ASSERT_TRUE(output_model); +} + +TEST_F(QuantizeWeightsTest, QuantizationFails) { + LoadBasicModel(); + flatbuffers::FlatBufferBuilder builder; + EXPECT_EQ(QuantizeWeights(&builder, model_, TensorType_UINT8, {}, {}, 1024), + absl::InternalError("Quantize weights transformation failed.")); +} + +TEST_F(QuantizeWeightsTest, WeightsMinNumElements) { + LoadBasicModel(); + // Make weights_min_size sufficiently large such that no quantization should + // happen, i.e. the original model is the same size as the old one. + flatbuffers::FlatBufferBuilder builder; + const uint64_t kWeightsMinNumElements = 1000000; + EXPECT_EQ(QuantizeWeights(&builder, model_, kWeightsMinNumElements), + absl::InternalError("Quantize weights transformation failed.")); + + const uint8_t* buffer = builder.GetBufferPointer(); + const Model* output_model = GetModel(buffer); + ASSERT_TRUE(output_model); + + for (size_t subgraph_idx = 0; subgraph_idx < model_->subgraphs()->size(); + subgraph_idx++) { + const auto quantized_graph = output_model->subgraphs()->Get(subgraph_idx); + const auto float_graph = model_->subgraphs()->Get(subgraph_idx); + ASSERT_EQ(quantized_graph->tensors()->size(), + float_graph->tensors()->size()); + std::vector used_tensors; + for (size_t i = 0; i < quantized_graph->tensors()->size(); i++) { + const auto quant_tensor = quantized_graph->tensors()->Get(i); + const auto float_tensor = FindMatchingExpectedTensor( + /*quantized_model=*/output_model, /*expected_model=*/model_, + /*quantized_tensor=*/quant_tensor, + /*possible_tensors=*/float_graph->tensors(), + /*used_tensors=*/used_tensors); + EXPECT_NE(float_tensor, nullptr); + } + EXPECT_EQ(used_tensors.size(), quantized_graph->tensors()->size()); + } +} + +TEST_F(QuantizeWeightsTest, HybridConv) { + LoadBasicModel(); + flatbuffers::FlatBufferBuilder builder; + EXPECT_OK(QuantizeWeights(&builder, model_, 0)); + + const uint8_t* buffer = builder.GetBufferPointer(); + const Model* output_model = GetModel(buffer); + ASSERT_TRUE(output_model); + + // Nothing should change. + ASSERT_EQ(output_model->subgraphs()->size(), model_->subgraphs()->size()); + for (size_t subgraph_idx = 0; subgraph_idx < model_->subgraphs()->size(); + subgraph_idx++) { + const auto quantized_graph = output_model->subgraphs()->Get(subgraph_idx); + const auto float_graph = model_->subgraphs()->Get(subgraph_idx); + ASSERT_EQ(quantized_graph->tensors()->size(), + float_graph->tensors()->size()); + // Make sure the graph only has one Conv operation. + ASSERT_EQ(quantized_graph->operators()->size(), 1); + const auto op = quantized_graph->operators()->Get(0); + const uint32_t op_code_idx = op->opcode_index(); + ASSERT_EQ(GetBuiltinCode(output_model->operator_codes()->Get(op_code_idx)), + BuiltinOperator_CONV_2D); + std::vector used_tensors; + for (size_t i = 0; i < quantized_graph->tensors()->size(); i++) { + const auto quant_tensor = quantized_graph->tensors()->Get(i); + const auto float_tensor = FindMatchingExpectedTensor( + /*quantized_model=*/output_model, /*expected_model=*/model_, + /*quantized_tensor=*/quant_tensor, + /*possible_tensors=*/float_graph->tensors(), + /*used_tensors=*/used_tensors, /*quantized=*/true); + EXPECT_NE(float_tensor, nullptr); + // If the tensor is a weight, it should have type INT8, otherwise it + // should stay with type FLOAT32. + // If the tensor is a bias, it should have type FLOAT32. + // + // Check with float_tensor name since quantized tensor + // may be renamed. + if (float_tensor->name()->str() == "conv_bias") { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } else if (IsModelInputOrOutput(output_model, i)) { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } else if (quant_tensor->buffer() != 0) { + EXPECT_EQ(quant_tensor->type(), TensorType_INT8) + << quant_tensor->name()->str(); + auto shape = GetAsVector(quant_tensor->shape()); + if (kUseUpdatedHybridSchemeDefault) { + EXPECT_EQ(quant_tensor->quantization()->scale()->size(), shape[0]); + } else { + EXPECT_EQ(quant_tensor->quantization()->scale()->size(), 1); + } + } else { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } + } + EXPECT_EQ(used_tensors.size(), quantized_graph->tensors()->size()); + } +} + +TEST_F(QuantizeWeightsTest, DequantizeConv) { + LoadBasicModel(); + flatbuffers::FlatBufferBuilder builder; + EXPECT_OK(QuantizeWeights(&builder, model_, 0, + /*use_hybrid_evaluation=*/false)); + + const uint8_t* buffer = builder.GetBufferPointer(); + const Model* output_model = GetModel(buffer); + ASSERT_TRUE(output_model); + + ASSERT_EQ(output_model->subgraphs()->size(), model_->subgraphs()->size()); + for (size_t subgraph_idx = 0; subgraph_idx < model_->subgraphs()->size(); + ++subgraph_idx) { + const auto quantized_graph = output_model->subgraphs()->Get(subgraph_idx); + const auto float_graph = model_->subgraphs()->Get(subgraph_idx); + // The output graph should have an extra tensor from the added dequantize + // op. + ASSERT_EQ(quantized_graph->tensors()->size(), + float_graph->tensors()->size() + 1); + // Check that a dequantize op exists. + int32_t dequant_input_idx = -1; + int32_t dequant_output_idx = -1; + for (size_t i = 0; i < quantized_graph->operators()->size(); ++i) { + const auto op = quantized_graph->operators()->Get(i); + const uint32_t op_code_idx = op->opcode_index(); + if (GetBuiltinCode(output_model->operator_codes()->Get(op_code_idx)) == + BuiltinOperator_DEQUANTIZE) { + dequant_input_idx = op->inputs()->Get(0); + dequant_output_idx = op->outputs()->Get(0); + } + } + ASSERT_GT(dequant_input_idx, -1); + ASSERT_GT(dequant_output_idx, -1); + std::vector used_tensors; + for (size_t i = 0; i < quantized_graph->tensors()->size(); ++i) { + const auto quant_tensor = quantized_graph->tensors()->Get(i); + const auto float_tensor = FindMatchingExpectedTensor( + /*quantized_model=*/output_model, /*expected_model=*/model_, + /*quantized_tensor=*/quant_tensor, + /*possible_tensors=*/float_graph->tensors(), + /*used_tensors=*/used_tensors, /*quantized=*/true); + // If the tensor is a weight, it should have type INT8. + // If the tensor is a bias, it should have type FLOAT32. + // If the tensor is an input or output it should have type FLOAT32. + // The input to dequantize should be INT8, and all other tensors should be + // FLOAT32. + if (i == dequant_input_idx) { + EXPECT_EQ(quant_tensor->type(), TensorType_INT8); + } else if (i == dequant_output_idx) { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } else if (IsModelInputOrOutput(output_model, i)) { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } else if (float_tensor != nullptr && + float_tensor->name()->str() == "conv_bias") { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } else if (quant_tensor->buffer() != 0) { + // If it's a non-bias constant tensor, it must be the weight. + EXPECT_EQ(quant_tensor->type(), TensorType_INT8); + } else { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } + } + } +} + +TEST_F(QuantizeWeightsTest, DequantizeConvFloat16) { + LoadBasicModel(); + flatbuffers::FlatBufferBuilder builder; + EXPECT_OK(QuantizeWeights(&builder, model_, BufferType::QUANTIZED_FLOAT16)); + + const uint8_t* buffer = builder.GetBufferPointer(); + const Model* output_model = GetModel(buffer); + ASSERT_TRUE(output_model); + + ASSERT_EQ(output_model->subgraphs()->size(), model_->subgraphs()->size()); + for (size_t subgraph_idx = 0; subgraph_idx < model_->subgraphs()->size(); + ++subgraph_idx) { + const auto quantized_graph = output_model->subgraphs()->Get(subgraph_idx); + const auto float_graph = model_->subgraphs()->Get(subgraph_idx); + // The output graph should have two extra tensors from the added dequantize + // op. + ASSERT_EQ(quantized_graph->tensors()->size(), + float_graph->tensors()->size() + 2); + // Check that a dequantize op exists. + int32_t dequant_input_idx = -1; + int32_t dequant_output_idx = -1; + for (size_t i = 0; i < quantized_graph->operators()->size(); ++i) { + const auto op = quantized_graph->operators()->Get(i); + const uint32_t op_code_idx = op->opcode_index(); + if (GetBuiltinCode(output_model->operator_codes()->Get(op_code_idx)) == + BuiltinOperator_DEQUANTIZE) { + dequant_input_idx = op->inputs()->Get(0); + dequant_output_idx = op->outputs()->Get(0); + } + } + ASSERT_GT(dequant_input_idx, -1); + ASSERT_GT(dequant_output_idx, -1); + for (size_t i = 0; i < quantized_graph->tensors()->size(); ++i) { + const auto quant_tensor = quantized_graph->tensors()->Get(i); + // If the tensor is a weight, it should have type FLOAT16. + // If the tensor is a bias, it should have type FLOAT16. + // If the tensor is an input or output it should have type FLOAT32. + // The input to dequantize should be FLOAT16, and all other tensors should + // be FLOAT32. + if (i == dequant_input_idx) { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT16); + } else if (i == dequant_output_idx) { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } else if (IsModelInputOrOutput(output_model, i)) { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } else if (quant_tensor->name()->str() == "conv_bias") { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT16); + } else if ((!CreateMutableModelFromFile(output_model) + ->buffers[quant_tensor->buffer()] + .get() + ->data.empty())) { + // If it's a non-bias constant tensor, it must be the weight. + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT16); + } else { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } + } + } +} + +TEST_F(QuantizeWeightsTest, SharedWeights_Hybrid) { + LoadSharedWeightsModel(); + flatbuffers::FlatBufferBuilder builder; + EXPECT_OK(QuantizeWeights(&builder, model_, 0)); + + const uint8_t* buffer = builder.GetBufferPointer(); + const Model* output_model = GetModel(buffer); + ASSERT_TRUE(output_model); + + ASSERT_EQ(output_model->subgraphs()->size(), model_->subgraphs()->size()); + uint32_t num_conv_ops = 0; + for (size_t subgraph_idx = 0; subgraph_idx < model_->subgraphs()->size(); + ++subgraph_idx) { + const auto quantized_graph = output_model->subgraphs()->Get(subgraph_idx); + for (size_t i = 0; i < quantized_graph->operators()->size(); ++i) { + const auto op = quantized_graph->operators()->Get(i); + const uint32_t op_code_idx = op->opcode_index(); + const auto op_code = + GetBuiltinCode(output_model->operator_codes()->Get(op_code_idx)); + if (op_code == BuiltinOperator_CONV_2D) { + num_conv_ops++; + // Ensure that each convolution's weights tensor is now INT8. + const auto weights_tensor = + quantized_graph->tensors()->Get(op->inputs()->Get(1)); + EXPECT_EQ(weights_tensor->type(), TensorType_INT8); + } + } + } + // Ensure that there were exactly two convolutions in the model. + EXPECT_EQ(num_conv_ops, 2); +} + +TEST_F(QuantizeWeightsTest, SharedWeights_Dequantize) { + LoadSharedWeightsModel(); + flatbuffers::FlatBufferBuilder builder; + EXPECT_OK( + QuantizeWeights(&builder, model_, 0, /*use_hybrid_evaluation=*/false)); + + const uint8_t* buffer = builder.GetBufferPointer(); + const Model* output_model = GetModel(buffer); + ASSERT_TRUE(output_model); + + ASSERT_EQ(output_model->subgraphs()->size(), model_->subgraphs()->size()); + uint32_t num_conv_ops = 0; + for (size_t subgraph_idx = 0; subgraph_idx < model_->subgraphs()->size(); + ++subgraph_idx) { + const auto quantized_graph = output_model->subgraphs()->Get(subgraph_idx); + for (size_t i = 0; i < quantized_graph->operators()->size(); ++i) { + const auto op = quantized_graph->operators()->Get(i); + const uint32_t op_code_idx = op->opcode_index(); + const auto op_code = + GetBuiltinCode(output_model->operator_codes()->Get(op_code_idx)); + if (op_code == BuiltinOperator_CONV_2D) { + num_conv_ops++; + // Ensure that each convolution's weights tensor is still FLOAT + // (the output of the dequantize). + uint32_t weights_tensor_index = op->inputs()->Get(1); + const auto weights_tensor = + quantized_graph->tensors()->Get(weights_tensor_index); + EXPECT_EQ(weights_tensor->type(), TensorType_FLOAT32); + + // Check that it comes from a dequantize operation. + BuiltinOperator producer_op_code; + ASSERT_TRUE(GetProducerOpCode(output_model, subgraph_idx, + weights_tensor_index, &producer_op_code)); + EXPECT_EQ(producer_op_code, BuiltinOperator_DEQUANTIZE); + } + } + } + // Ensure that there were exactly two convolutions in the model. + EXPECT_EQ(num_conv_ops, 2); +} + +TEST_F(QuantizeWeightsTest, VerifyGatherQuantization) { + LoadGatherTestModel(); + flatbuffers::FlatBufferBuilder builder; + EXPECT_OK(QuantizeWeights(&builder, model_, 0)); + + const uint8_t* buffer = builder.GetBufferPointer(); + const Model* output_model = GetModel(buffer); + ASSERT_TRUE(output_model); + + ASSERT_EQ(output_model->subgraphs()->size(), model_->subgraphs()->size()); + for (size_t subgraph_idx = 0; subgraph_idx < model_->subgraphs()->size(); + ++subgraph_idx) { + const auto quantized_graph = output_model->subgraphs()->Get(subgraph_idx); + for (size_t i = 0; i < quantized_graph->operators()->size(); ++i) { + const auto op = quantized_graph->operators()->Get(i); + const uint32_t op_code_idx = op->opcode_index(); + const auto op_code = + GetBuiltinCode(output_model->operator_codes()->Get(op_code_idx)); + if (op_code == BuiltinOperator_GATHER) { + uint32_t input_tensor_index = op->inputs()->Get(0); + const auto weights_tensor = + quantized_graph->tensors()->Get(input_tensor_index); + EXPECT_EQ(weights_tensor->type(), TensorType_INT8); + } + } + } +} + +TEST_F(QuantizeWeightsTest, VerifyCustomOpQuantizationDequantize) { + LoadCustomOpTestModel(); + + // The custom op is not hybrid, and the second input is a constant that can + // be quantized. + CustomOpMap custom_op_map; + custom_op_map["CustomTestOp"] = { + {1}, // quantizable_input_indices + true, // is_weight_only + }; + + flatbuffers::FlatBufferBuilder builder; + EXPECT_OK(QuantizeWeights(&builder, model_, 0, custom_op_map)); + + const uint8_t* buffer = builder.GetBufferPointer(); + const Model* output_model = GetModel(buffer); + ASSERT_TRUE(output_model); + + ASSERT_EQ(output_model->subgraphs()->size(), model_->subgraphs()->size()); + const auto quantized_graph = output_model->subgraphs()->Get(0); + // A dequantize op should be added. + ASSERT_EQ(quantized_graph->operators()->size(), + model_->subgraphs()->Get(0)->operators()->size() + 1); + int num_custom_ops_found = 0; + for (size_t i = 0; i < quantized_graph->operators()->size(); ++i) { + const auto op = quantized_graph->operators()->Get(i); + const uint32_t op_code_idx = op->opcode_index(); + const auto op_code = + GetBuiltinCode(output_model->operator_codes()->Get(op_code_idx)); + if (op_code == BuiltinOperator_CUSTOM) { + uint32_t weights_tensor_index = op->inputs()->Get(1); + const auto weights_tensor = + quantized_graph->tensors()->Get(weights_tensor_index); + EXPECT_EQ(weights_tensor->type(), TensorType_FLOAT32); + + // Check that it comes from a dequantize operation. + BuiltinOperator producer_op_code; + ASSERT_TRUE(GetProducerOpCode(output_model, 0, weights_tensor_index, + &producer_op_code)); + EXPECT_EQ(producer_op_code, BuiltinOperator_DEQUANTIZE); + num_custom_ops_found++; + } + } + EXPECT_EQ(num_custom_ops_found, 1); +} + +TEST_F(QuantizeWeightsTest, VerifyCustomOpQuantizationHybrid) { + LoadCustomOpTestModel(); + + // The custom op is dynamic range quantizable, and the second input is a + // constant that can be quantized. + CustomOpMap custom_op_map; + custom_op_map["CustomTestOp"] = { + {1}, // quantizable_input_indices + false, // is_weight_only + }; + + flatbuffers::FlatBufferBuilder builder; + ASSERT_OK(QuantizeWeights(&builder, model_, 0, custom_op_map)); + + const uint8_t* buffer = builder.GetBufferPointer(); + const Model* output_model = GetModel(buffer); + ASSERT_TRUE(output_model); + + ASSERT_EQ(output_model->subgraphs()->size(), model_->subgraphs()->size()); + const auto quantized_graph = output_model->subgraphs()->Get(0); + ASSERT_EQ(quantized_graph->operators()->size(), + model_->subgraphs()->Get(0)->operators()->size()); + int num_custom_ops_found = 0; + for (size_t i = 0; i < quantized_graph->operators()->size(); ++i) { + const auto op = quantized_graph->operators()->Get(i); + const uint32_t op_code_idx = op->opcode_index(); + const auto op_code = + GetBuiltinCode(output_model->operator_codes()->Get(op_code_idx)); + if (op_code == BuiltinOperator_CUSTOM) { + uint32_t weights_tensor_index = op->inputs()->Get(1); + const auto weights_tensor = + quantized_graph->tensors()->Get(weights_tensor_index); + EXPECT_EQ(weights_tensor->type(), TensorType_INT8); + num_custom_ops_found++; + } + } + EXPECT_EQ(num_custom_ops_found, 1); +} + +TEST_F(QuantizeWeightsTest, VerifyUpdatedHybridSchemeFalseQuantizationHybrid) { + LoadBasicModel(); + flatbuffers::FlatBufferBuilder builder; + const CustomOpMap custom_op_map; + EXPECT_OK(QuantizeWeights(&builder, model_, 0, custom_op_map, false)); + + const uint8_t* buffer = builder.GetBufferPointer(); + const Model* output_model = GetModel(buffer); + ASSERT_TRUE(output_model); + + // Nothing should change. + ASSERT_EQ(output_model->subgraphs()->size(), model_->subgraphs()->size()); + for (size_t subgraph_idx = 0; subgraph_idx < model_->subgraphs()->size(); + subgraph_idx++) { + const auto quantized_graph = output_model->subgraphs()->Get(subgraph_idx); + const auto float_graph = model_->subgraphs()->Get(subgraph_idx); + ASSERT_EQ(quantized_graph->tensors()->size(), + float_graph->tensors()->size()); + // Make sure the graph only has one Conv operation. + ASSERT_EQ(quantized_graph->operators()->size(), 1); + const auto op = quantized_graph->operators()->Get(0); + const uint32_t op_code_idx = op->opcode_index(); + ASSERT_EQ(GetBuiltinCode(output_model->operator_codes()->Get(op_code_idx)), + BuiltinOperator_CONV_2D); + std::vector used_tensors; + for (size_t i = 0; i < quantized_graph->tensors()->size(); i++) { + const auto quant_tensor = quantized_graph->tensors()->Get(i); + const auto float_tensor = FindMatchingExpectedTensor( + /*quantized_model=*/output_model, /*expected_model=*/model_, + /*quantized_tensor=*/quant_tensor, + /*possible_tensors=*/float_graph->tensors(), + /*used_tensors=*/used_tensors, /*quantized=*/true); + EXPECT_NE(float_tensor, nullptr); + // If the tensor is a weight, it should have type INT8, otherwise it + // should stay with type FLOAT32. + // If the tensor is a bias, it should have type FLOAT32. + if (float_tensor->name()->str() == "conv_bias") { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } else if (IsModelInputOrOutput(output_model, i)) { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } else if ((!CreateMutableModelFromFile(output_model) + ->buffers[quant_tensor->buffer()] + .get() + ->data.empty())) { + EXPECT_EQ(quant_tensor->type(), TensorType_INT8) + << quant_tensor->name()->str(); + auto shape = GetAsVector(quant_tensor->shape()); + EXPECT_EQ(quant_tensor->quantization()->scale()->size(), 1); + } else { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } + } + EXPECT_EQ(used_tensors.size(), quantized_graph->tensors()->size()); + } +} + +TEST_F(QuantizeWeightsTest, DequantizeConvBlocklisted) { + LoadBasicModel(); + flatbuffers::FlatBufferBuilder builder; + const CustomOpMap custom_op_map; + EXPECT_OK(QuantizeWeights(&builder, model_, 0, custom_op_map, + /*use_updated_hybrid_scheme=*/true, + {BuiltinOperator_CONV_2D})); + + const uint8_t* buffer = builder.GetBufferPointer(); + const Model* output_model = GetModel(buffer); + ASSERT_TRUE(output_model); + + ASSERT_EQ(output_model->subgraphs()->size(), model_->subgraphs()->size()); + for (size_t subgraph_idx = 0; subgraph_idx < model_->subgraphs()->size(); + ++subgraph_idx) { + const auto quantized_graph = output_model->subgraphs()->Get(subgraph_idx); + const auto float_graph = model_->subgraphs()->Get(subgraph_idx); + // The output graph should have an extra tensor from the added dequantize + // op. + ASSERT_EQ(quantized_graph->tensors()->size(), + float_graph->tensors()->size() + 1); + // Check that a dequantize op exists. + int32_t dequant_input_idx = -1; + int32_t dequant_output_idx = -1; + for (size_t i = 0; i < quantized_graph->operators()->size(); ++i) { + const auto op = quantized_graph->operators()->Get(i); + const uint32_t op_code_idx = op->opcode_index(); + if (GetBuiltinCode(output_model->operator_codes()->Get(op_code_idx)) == + BuiltinOperator_DEQUANTIZE) { + dequant_input_idx = op->inputs()->Get(0); + dequant_output_idx = op->outputs()->Get(0); + } + } + ASSERT_GT(dequant_input_idx, -1); + ASSERT_GT(dequant_output_idx, -1); + std::vector used_tensors; + for (size_t i = 0; i < quantized_graph->tensors()->size(); ++i) { + const auto quant_tensor = quantized_graph->tensors()->Get(i); + const auto float_tensor = FindMatchingExpectedTensor( + /*quantized_model=*/output_model, /*expected_model=*/model_, + /*quantized_tensor=*/quant_tensor, + /*possible_tensors=*/float_graph->tensors(), + /*used_tensors=*/used_tensors); + // If the tensor is a weight, it should have type INT8. + // If the tensor is a bias, it should have type FLOAT32. + // If the tensor is an input or output it should have type FLOAT32. + // The input to dequantize should be INT8, and all other tensors should be + // FLOAT32. + if (i == dequant_input_idx) { + EXPECT_EQ(quant_tensor->type(), TensorType_INT8); + // The dequantize should still be quantized per-channel + EXPECT_EQ(quant_tensor->quantization()->scale()->size(), 5); + EXPECT_EQ(quant_tensor->quantization()->quantized_dimension(), 0); + } else if (i == dequant_output_idx) { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } else if (IsModelInputOrOutput(output_model, i)) { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } else if (float_tensor != nullptr && + float_tensor->name()->str() == "conv_bias") { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } else if ((!CreateMutableModelFromFile(output_model) + ->buffers[quant_tensor->buffer()] + .get() + ->data.empty())) { + // If it's a non-bias constant tensor, it must be the weight. + EXPECT_EQ(quant_tensor->type(), TensorType_INT8); + } else { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } + } + } +} + +} // namespace +} // namespace optimize +} // namespace tflite + +int main(int argc, char** argv) { + std::string model_file; + const std::vector flag_list = { + tensorflow::Flag("test_model_file", &model_file, + "Path to test tflite model file."), + }; + + const bool parse_result = tensorflow::Flags::Parse(&argc, argv, flag_list); + if (!parse_result) { + std::cerr << "Required test_model_file\n"; + std::abort(); + } + g_test_model_dir = new std::string(tensorflow::io::Dirname(model_file)); + ::tensorflow::port::InitMain(argv[0], &argc, &argv); + return RUN_ALL_TESTS(); +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/test_util.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/test_util.cc new file mode 100644 index 0000000000000000000000000000000000000000..0305093b6c40ab52550923ac17802d7a9324b31b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/test_util.cc @@ -0,0 +1,96 @@ +/* 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 "tensorflow/compiler/mlir/lite/quantization/lite/test_util.h" + +#include +#include + +#include + +namespace mlir { +namespace lite { +namespace internal { +const char* kConvModelWithMinus128Plus127Weights = + "single_conv_weights_min_minus_127_max_plus_127.bin"; + +const char* kConvModelWith0Plus10Weights = + "single_conv_weights_min_0_max_plus_10.bin"; + +const char* kConvModelWithNoBias = "single_conv_no_bias.bin"; + +const char* kSingleSoftmaxModelMinMinus5MaxPlus5 = + "single_softmax_min_minus_5_max_plus_5.bin"; + +const char* kSingleAvgPoolModelMinMinus5MaxPlus5 = + "single_avg_pool_min_minus_5_max_plus_5.bin"; + +const char* kModelWithSharedWeights = "weight_shared_between_convs.bin"; +const char* kMultiInputAddWithReshape = "multi_input_add_reshape.bin"; +const char* kQuantizedWithGather = "quantized_with_gather.bin"; + +const char* kConstInputAddModel = "add_with_const_input.bin"; + +const char* kFloatConcatMax5Max10Max10 = "concat.bin"; + +const char* kModelWithBroadcastToOp = "broadcast_to.bin"; + +const char* kModelWithCustomOp = "custom_op.bin"; + +const char* kModelWithArgMaxOp = "argmax.bin"; + +const char* kModelWithFCOp = "fc.bin"; + +const char* kModelWithGatherNDOp = "gather_nd.bin"; + +const char* kModelWithWhereOp = "where.bin"; + +const char* kModelMixed = "mixed.bin"; +const char* kModelMixed16x8 = "mixed16x8.bin"; + +const char* kModelSplit = "split.bin"; + +const char* kModelPack = "pack.bin"; + +const char* kLstmCalibrated = "lstm_calibrated.bin"; +const char* kLstmQuantized = "lstm_quantized.bin"; + +const char* kUnidirectionalSequenceLstmCalibrated = + "unidirectional_sequence_lstm_calibrated.bin"; +const char* kUnidirectionalSequenceLstmQuantized = + "unidirectional_sequence_lstm_quantized.bin"; + +const char* kModelWithMinimumOp = "minimum.bin"; +const char* kModelWithMaximumOp = "maximum.bin"; +const char* kLstmCalibrated2 = "lstm_calibrated2.bin"; +const char* kLstmQuantized2 = "lstm_quantized2.bin"; + +const char* kModelWithTranspose = "transpose.bin"; + +const char* kSvdfCalibrated = "svdf_calibrated.bin"; +const char* kSvdfQuantized = "svdf_quantized.bin"; + +const char* kModelWithUnpack = "unpack.bin"; +const char* kQatModelWithFc = "fc_qat.bin"; +const char* kModelWithResourceVarsCalibrated = "resource_vars_calibrated.bin"; + +int FailOnErrorReporter::Report(const char* format, va_list args) { + char buf[1024]; + vsnprintf(buf, sizeof(buf), format, args); + EXPECT_TRUE(false) << "Error happened: " << buf; + return 0; +} +} // namespace internal +} // namespace lite +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/test_util.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/test_util.h new file mode 100644 index 0000000000000000000000000000000000000000..dabfecd378f6d30a27730fb12ae5c52538942fe3 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/test_util.h @@ -0,0 +1,145 @@ +/* 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_LITE_TEST_UTIL_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_LITE_TEST_UTIL_H_ + +#include + +#include "tensorflow/compiler/mlir/lite/core/api/error_reporter.h" + +namespace mlir { +namespace lite { +namespace internal { +// Test model with a single convolution. +// Floating point weights of the model are all integers and lie in +// range[-127, 127]. The weights have been put in such a way that each +// channel has at least one weight as -127 and one weight as 127. +// The activations are all in range: [-128, 127] +// This means all bias computations should result in 1.0 scale. +extern const char* kConvModelWithMinus128Plus127Weights; + +// Test model with single convolution where all weights are integers between +// [0, 10] weights are randomly distributed. It is not guaranteed that min max +// for weights are going to appear in each channel. +// Activations have min = 0, max = 10. +extern const char* kConvModelWith0Plus10Weights; + +// Test model where no bias is in the conv. +extern const char* kConvModelWithNoBias; + +// A floating point model with a single softmax. The input tensor has min +// and max in range [-5, 5], not necessarily -5 or +5. +extern const char* kSingleSoftmaxModelMinMinus5MaxPlus5; + +// A floating point model with a single average pool. The input tensor has min +// and max in range [-5, 5], not necessarily -5 or +5. +extern const char* kSingleAvgPoolModelMinMinus5MaxPlus5; + +// Test model with a weights variable that is shared between a convolution layer +// and an add operation. +extern const char* kModelWithSharedWeights; + +// Test model with Add followed by a reshape. Model has 2 inputs for add. +extern const char* kMultiInputAddWithReshape; + +// Test gather operation with quantized input. +extern const char* kQuantizedWithGather; + +// Test model with a tf.constant input to tf.add. Model has 2 inputs one +// constant and other placeholder. +extern const char* kConstInputAddModel; + +// A float test model with concat that has [0, 5] and [0, 10] for inputs and [0, +// 10] as output. +extern const char* kFloatConcatMax5Max10Max10; + +// Test model with broadcast_to op. +extern const char* kModelWithBroadcastToOp; + +// Test model with a custom op. +extern const char* kModelWithCustomOp; + +// Test model with a argmax op. +extern const char* kModelWithArgMaxOp; + +// Test model with a fully connected op. +extern const char* kModelWithFCOp; + +// Test model with a gather_nd op. +extern const char* kModelWithGatherNDOp; + +// Test model with a Where op. +extern const char* kModelWithWhereOp; + +// Test model with mixed quantizable and un-quantizable ops. +// reshape->custom->custom->squeeze. +extern const char* kModelMixed; + +// Test model with mixed quantizable and +// and un-quantizable ops for +// activations in 16-bit. +extern const char* kModelMixed16x8; + +// Test model with split op. +extern const char* kModelSplit; + +// Test model with pack op. +extern const char* kModelPack; + +// Test model with LSTM op that has layer norm, has projection, without +// peephole, without cifg. +extern const char* kLstmCalibrated; +extern const char* kLstmQuantized; + +// Test model with LSTM op that has peephole, without layer norm, without +// projection, without cifg. +extern const char* kLstmCalibrated2; +extern const char* kLstmQuantized2; + +extern const char* kUnidirectionalSequenceLstmCalibrated; +extern const char* kUnidirectionalSequenceLstmQuantized; + +// Test model with a minimum op. +extern const char* kModelWithMinimumOp; + +// Test model with a maximum op. +extern const char* kModelWithMaximumOp; + +// Test model with a transpose op. +extern const char* kModelWithTranspose; + +// Test model with SVDF op. +extern const char* kSvdfCalibrated; +extern const char* kSvdfQuantized; + +// Test model with an unpack op. +extern const char* kModelWithUnpack; + +// Test QAT model with fc op. +extern const char* kQatModelWithFc; + +// Test calibrated model with resource variables. +extern const char* kModelWithResourceVarsCalibrated; + +// An error reporter that fails on testing. +class FailOnErrorReporter : public tflite::ErrorReporter { + public: + int Report(const char* format, va_list args) override; +}; +} // namespace internal +} // namespace lite +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_LITE_TEST_UTIL_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/README.md b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/README.md new file mode 100644 index 0000000000000000000000000000000000000000..397de27219c1afcdd3baedb77f598d3b23f4ab22 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/README.md @@ -0,0 +1,30 @@ +# Test models for testing quantization + +This directory contains test models for testing quantization. + +## Models + +* `single_conv_weights_min_0_max_plus_10.bin` \ + A floating point model with single convolution where all weights are + integers between [0, 10] weights are randomly distributed. It is not + guaranteed that min max for weights are going to appear in each channel. + All activations have min maxes and activations are in range [0,10]. +* `single_conv_weights_min_minus_127_max_plus_127.bin` \ + A floating point model with a single convolution where weights of the model + are all integers that lie in range[-127, 127]. The weights have been put in + such a way that each channel has at least one weight as -127 and one weight + as 127. The activations are all in range: [-128, 127]. + This means all bias computations should result in 1.0 scale. +* `single_softmax_min_minus_5_max_5.bin` \ + A floating point model with a single softmax. The input tensor has min + and max in range [-5, 5], not necessarily -5 or +5. +* `single_avg_pool_input_min_minus_5_max_5.bin` \ + A floating point model with a single average pool. The input tensor has min + and max in range [-5, 5], not necessarily -5 or +5. +* `weight_shared_between_convs.bin` \ + A floating point model with two convs that have a use the same weight tensor. +* `multi_input_add_reshape.bin` \ + A floating point model with two inputs with an add followed by a reshape. +* `quantized_with_gather.bin` \ + A floating point model with an input with a gather, modeling a situation + of mapping categorical input to embeddings. diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/add_with_const_input.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/add_with_const_input.bin new file mode 100644 index 0000000000000000000000000000000000000000..5f8df781d2dc8d18d1cfc9eb6c1013f5b7ac000a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/add_with_const_input.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:58dc33ec342cdacceae0183d12fe9eee3e8dbf8a8233ed6e3a31962c025a6ff0 +size 576 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/argmax.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/argmax.bin new file mode 100644 index 0000000000000000000000000000000000000000..6ce92eb82ea89944ea1f027a8dc2aa8cd3fd39dc --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/argmax.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:32cda32d22c0bb9e6478fc68c24c25ffb5b5c92c8b0ef04aaf2ef4e69515960e +size 528 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/broadcast_to.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/broadcast_to.bin new file mode 100644 index 0000000000000000000000000000000000000000..40df9ee597ff8a0e10b33ef847e883e02d93ad4c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/broadcast_to.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ff89b00569f4df18bd7ca627996aca23f6190bb4d509f0b537b6c588489903f8 +size 832 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/concat.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/concat.bin new file mode 100644 index 0000000000000000000000000000000000000000..cfe002f84202f7ff2b9ae833aef9475b6e9759e4 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/concat.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8f04579f1c4dcd51913f95a6469cca53f5d8f981a0b67f93fcca761fc476af79 +size 496 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/custom_op.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/custom_op.bin new file mode 100644 index 0000000000000000000000000000000000000000..d0dc1b77d3b585261cc34afdef261859d0e87db5 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/custom_op.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:41fd3d49c1db18f8e68340a01de46fd728904c27a1fa8c4f35161e81e34a6a24 +size 524 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/fc.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/fc.bin new file mode 100644 index 0000000000000000000000000000000000000000..c7c188120f05d297d3afa225a92061c5119864bb --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/fc.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e9285965f47b80a5b1770a60abb7e57094979f1882fb1055fe499eef627114f4 +size 1112 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/fc_qat.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/fc_qat.bin new file mode 100644 index 0000000000000000000000000000000000000000..07baa934ced2e2cb7faf7677ba6fe7766f6c28dc --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/fc_qat.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e6042c6c7830c19c2357c94d383a74e01e209096dfe80b7d3345902d3a34f72b +size 9656 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/gather_nd.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/gather_nd.bin new file mode 100644 index 0000000000000000000000000000000000000000..4f6e32a3a5a609aa5d65863d4a9514fe07aa09a7 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/gather_nd.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:77abceeca25922b41b88b380773b8d31d7eac1700101080a4aaa1acf82927463 +size 736 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/lstm_calibrated.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/lstm_calibrated.bin new file mode 100644 index 0000000000000000000000000000000000000000..f184c111f42f69081ccc34caa7af28f42e23b598 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/lstm_calibrated.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:30162f4236d267d1f0706502d58b5e961b553b8d5dda0ad14a1c7e27a8eebae7 +size 2908 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/lstm_calibrated2.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/lstm_calibrated2.bin new file mode 100644 index 0000000000000000000000000000000000000000..182be9e0f907c45f24eb48d20c45ae31707beb31 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/lstm_calibrated2.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a54196c266c9b6e3f6be4fc94a58f05cd283b223743ef39ffaf3269b56149da9 +size 2808 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/lstm_quantized.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/lstm_quantized.bin new file mode 100644 index 0000000000000000000000000000000000000000..35283b08e5873a0e5bb5c750b0935b5c06748964 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/lstm_quantized.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1ac5102a913d520698e925168f8450f10d9b0f3c7e685a7516fdc0fce21d8a73 +size 4328 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/lstm_quantized2.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/lstm_quantized2.bin new file mode 100644 index 0000000000000000000000000000000000000000..f2cb19986bb1a072b66bfa894bb5fdbd6799f83e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/lstm_quantized2.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f7f0e4d206eecdf8277b986b806ed3c4492588fbea9d3d07615ec23a726464c0 +size 4152 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/maximum.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/maximum.bin new file mode 100644 index 0000000000000000000000000000000000000000..c3980e5353996de432c9b2410ce081a759a4abaa --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/maximum.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:60c91bf6bc9cfd3c133e0ddac28ac235124f8bc4d18ea5291130ddbcb1c280d4 +size 532 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/minimum.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/minimum.bin new file mode 100644 index 0000000000000000000000000000000000000000..e649026b390fd529bf30ef3ff60917eded346cf5 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/minimum.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9588bef6cb73e4eb34ff96d50eb35f9017777ca86dab142d7745cf78c9eefc88 +size 532 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/mixed.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/mixed.bin new file mode 100644 index 0000000000000000000000000000000000000000..c9b4ba55cde187918de4b703ac5088f49e19f8f0 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/mixed.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7d6fc338ce075e0939c017110035097040f709f3ba4639206956bd569bbf768c +size 1096 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/mixed16x8.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/mixed16x8.bin new file mode 100644 index 0000000000000000000000000000000000000000..f344bcadb3ac932a0f3f8067d103fbac2921ebaf --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/mixed16x8.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5e504847a46e35ea9458ae20745e39fbcf01877508027508fa9f97c1ff562bff +size 1184 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/multi_input_add_reshape.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/multi_input_add_reshape.bin new file mode 100644 index 0000000000000000000000000000000000000000..3fb6497452a262cb6b0bb8ef3eb62e933c3e3980 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/multi_input_add_reshape.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3027ceb22ad1769152fc3137ee489ed1b7c2988fc4211a90425f4a441083899a +size 804 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/pack.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/pack.bin new file mode 100644 index 0000000000000000000000000000000000000000..98cd7d9041f4a7a15b8d563b463bb0f6cf0c3a5f --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/pack.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:85444930780beaf6f2f8cfe95a5c36a48b16e79cdc6d55af278ff747a3705093 +size 732 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/quantized_with_gather.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/quantized_with_gather.bin new file mode 100644 index 0000000000000000000000000000000000000000..5b0f9d041bb885aad537cea07366dfb35b5cb183 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/quantized_with_gather.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:829c30eb27dd095589907f549139933a0478424c906cdf790e04de8ab3fbbd97 +size 768 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/resource_vars_calibrated.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/resource_vars_calibrated.bin new file mode 100644 index 0000000000000000000000000000000000000000..b2acf2622700b8c138e5130cf38b52b2349dd389 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/resource_vars_calibrated.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:83e379429a0a8969f0203f7d580b4a38b78044e4e6ad166aa37127a7a8bad258 +size 2512 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/single_avg_pool_min_minus_5_max_plus_5.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/single_avg_pool_min_minus_5_max_plus_5.bin new file mode 100644 index 0000000000000000000000000000000000000000..390225a98c99f7187b040cc19e825151d61d6c7e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/single_avg_pool_min_minus_5_max_plus_5.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b5ed2c7cd9402e5f7d031e2371a9d0fb9d9c0acabc1e8645b34f1023a27e7850 +size 508 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/single_conv_no_bias.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/single_conv_no_bias.bin new file mode 100644 index 0000000000000000000000000000000000000000..b25a054d77808e11952c9286501aec984d1c588a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/single_conv_no_bias.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fd4e23b69986dd1c92f31c3a65fa7c87e0fe56fd9dbabd9ccf2c95bda9f937ab +size 1212 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/single_conv_weights_min_0_max_plus_10.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/single_conv_weights_min_0_max_plus_10.bin new file mode 100644 index 0000000000000000000000000000000000000000..2da1761b4651073931220b3177008603c711a8d3 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/single_conv_weights_min_0_max_plus_10.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:241f992adef43ea23efd35b44e3e1d5f6cbb91bb9209f336191220e3a392d579 +size 1352 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/single_conv_weights_min_minus_127_max_plus_127.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/single_conv_weights_min_minus_127_max_plus_127.bin new file mode 100644 index 0000000000000000000000000000000000000000..5cc6955094844984830d60f415653d5e9dacdb45 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/single_conv_weights_min_minus_127_max_plus_127.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:dc0ba9f624d5248830916f7b1b46c3a08e6081b3289a86fa14ebec39d2f80168 +size 1352 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/single_softmax_min_minus_5_max_plus_5.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/single_softmax_min_minus_5_max_plus_5.bin new file mode 100644 index 0000000000000000000000000000000000000000..e745ca7c8283db6bf4fcc666b8f14c00eacf0d67 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/single_softmax_min_minus_5_max_plus_5.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:075cf82692561c880b15c13d3896969400cc158bc46df11482b628a977635a66 +size 464 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/split.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/split.bin new file mode 100644 index 0000000000000000000000000000000000000000..82ef92dc3216c71739be57fa8eceda754e2a0381 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/split.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:85e05ab5fcf6c29934c052243337ac34256f0292a0f836a4ec6181de4d311ae6 +size 920 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/svdf_calibrated.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/svdf_calibrated.bin new file mode 100644 index 0000000000000000000000000000000000000000..35d4d2f6e4a834b1f15c2c5ab7184f62a72b3ad2 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/svdf_calibrated.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f8345684de6ffd6213a996bd163a26e7d1ed259500830175d483b65d1562a6f1 +size 1016 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/svdf_quantized.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/svdf_quantized.bin new file mode 100644 index 0000000000000000000000000000000000000000..2d8dbfcb6ac59394758b861fb5407bc618c76acc --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/svdf_quantized.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:834b265292c7d8b86818aac426b767424d988e05be31fb8a871cc005c45305a7 +size 1152 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/transpose.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/transpose.bin new file mode 100644 index 0000000000000000000000000000000000000000..b9cbeda1305ee608b44a1f7f87c78bb2b83bc163 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/transpose.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9bd40e9017855306100d47a8f95418155b28e8748ba37ab602136af0d2a169e3 +size 544 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/unidirectional_sequence_lstm_calibrated.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/unidirectional_sequence_lstm_calibrated.bin new file mode 100644 index 0000000000000000000000000000000000000000..9cb0f3df08605714c5c5ce69f323de7fc55a5b90 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/unidirectional_sequence_lstm_calibrated.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:461b64c8fa4454babfd7c1d62e0e777e513048af0941e4bccc0f3f4258c54106 +size 3088 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/unidirectional_sequence_lstm_quantized.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/unidirectional_sequence_lstm_quantized.bin new file mode 100644 index 0000000000000000000000000000000000000000..8269f844ca32b9e60483a66641be597fb4654458 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/unidirectional_sequence_lstm_quantized.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:427830d48d054b40cfa73762231e7d01cc19ff243b6c589c171f03e07150ac16 +size 4448 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/unpack.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/unpack.bin new file mode 100644 index 0000000000000000000000000000000000000000..22b31523764b6511a700b68fc75663ab3bc37091 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/unpack.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f319a33fc838f6d87a9843ce2b192b66862249f1103efdbc35a1c6140a4840e1 +size 616 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/weight_shared_between_convs.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/weight_shared_between_convs.bin new file mode 100644 index 0000000000000000000000000000000000000000..d0ff8bb1003a22748665a836d877a730b97f6930 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/weight_shared_between_convs.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b76aaa81346fc479c80e154c90ddd098e6aa839f6196fdf32b2920672e53b44d +size 948 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/where.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/where.bin new file mode 100644 index 0000000000000000000000000000000000000000..d23aff4682d25665cfc58354e65abe30bce3e439 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/testdata/where.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e63c523ee312be37edcfa664bffdfd64e96053903c74cd39c948d0f6e9ae0a1f +size 544 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/tfl_quantizer.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/tfl_quantizer.cc new file mode 100644 index 0000000000000000000000000000000000000000..d8366aef08ddb40814f61fde4a2b942b494c803f --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/tfl_quantizer.cc @@ -0,0 +1,71 @@ +/* Copyright 2019 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 "absl/status/status.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/InitLLVM.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/PrettyStackTrace.h" +#include "llvm/Support/raw_ostream.h" +#include "tensorflow/compiler/mlir/lite/quantization/lite/quantize_model.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" + +using llvm::cl::opt; + +// NOLINTNEXTLINE +static opt inputFileName(llvm::cl::Positional, + llvm::cl::desc(""), + llvm::cl::init("-")); + +namespace mlir { +namespace { + +absl::Status QuantizeAnnotatedModel(llvm::StringRef buffer, + std::string& output_buffer) { + return mlir::lite::QuantizeModel( + buffer, tflite::TensorType_INT8, tflite::TensorType_INT8, + tflite::TensorType_INT8, {}, /*disable_per_channel=*/false, + /*fully_quantize=*/true, output_buffer); +} + +} // namespace +} // namespace mlir + +int main(int argc, char** argv) { + llvm::InitLLVM y(argc, argv); + llvm::cl::ParseCommandLineOptions(argc, argv); + auto file_or_err = llvm::MemoryBuffer::getFileOrSTDIN(inputFileName.c_str()); + if (std::error_code error = file_or_err.getError()) { + llvm::errs() << argv[0] << ": could not open input file '" << inputFileName + << "': " << error.message() << "\n"; + return 1; + } + auto buffer = file_or_err->get(); + std::string output_buffer; + if (auto status = mlir::QuantizeAnnotatedModel(buffer->getBuffer().str(), + output_buffer); + !status.ok()) { + llvm::errs() << status.message() << "\n"; + return 1; + } + + std::cout << output_buffer << "\n"; + return 0; +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/tfl_to_std.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/tfl_to_std.cc new file mode 100644 index 0000000000000000000000000000000000000000..3919f78852cdb0746b2e2a046c2eac615ea2577e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/tfl_to_std.cc @@ -0,0 +1,86 @@ +/* Copyright 2020 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/compiler/mlir/lite/quantization/lite/tfl_to_std.h" + +#include "llvm/Support/Casting.h" +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_utils.h" +#include "tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.h" +#include "tensorflow/compiler/mlir/lite/utils/utils.h" + +namespace mlir { +namespace TFL { + +void ConvertTFLQuantOpsToMlirQuantOps(func::FuncOp func) { + OpBuilder b(func); + func.walk([&](Operation* op) { + b.setInsertionPoint(op); + if (auto dq = llvm::dyn_cast(op)) { + auto dcast = quantfork::DequantizeCastOp::create( + b, dq.getLoc(), dq.getOutput().getType(), dq.getInput()); + dq.getOutput().replaceAllUsesWith(dcast); + dq.erase(); + } else if (auto q = llvm::dyn_cast(op)) { + auto qcast = quantfork::QuantizeCastOp::create( + b, q.getLoc(), q.getOutput().getType(), q.getInput()); + q.getOutput().replaceAllUsesWith(qcast); + q.erase(); + } else if (auto q = llvm::dyn_cast(op)) { + auto value = q.getValue(); + auto type = q.getResult().getType(); + if (arith::ConstantOp::isBuildableWith(value, type)) { + auto c = arith::ConstantOp::create(b, q.getLoc(), q.getValue()); + q.getOutput().replaceAllUsesWith(c); + q.erase(); + } else if (TFL::NoValueOp::isBuildableWith(value, type)) { + auto c = TFL::NoValueOp::create(b, q.getLoc(), type, mlir::UnitAttr()); + q.getOutput().replaceAllUsesWith(c); + q.erase(); + } + } + }); +} + +void ConvertMlirQuantOpsToTFLQuantOps(func::FuncOp func) { + OpBuilder b(func); + func.walk([&](Operation* op) { + b.setInsertionPoint(op); + if (auto dq = llvm::dyn_cast(op)) { + auto dcast = DequantizeOp::create(b, dq.getLoc(), + dq.getResult().getType(), dq.getArg()); + dq.getResult().replaceAllUsesWith(dcast); + if (auto extra_attr = op->getAttr(kVolatileOpAttrName)) { + dcast->setAttr(kVolatileOpAttrName, extra_attr); + } + dq.erase(); + } else if (auto q = llvm::dyn_cast(op)) { + auto out_type = q.getResult().getType(); + auto qcast = QuantizeOp::create(b, q.getLoc(), out_type, q.getArg(), + TypeAttr::get(out_type)); + q.getResult().replaceAllUsesWith(qcast); + if (auto extra_attr = op->getAttr(kVolatileOpAttrName)) { + qcast->setAttr(kVolatileOpAttrName, extra_attr); + } + q.erase(); + } + }); +} + +} // namespace TFL +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/tfl_to_std.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/tfl_to_std.h new file mode 100644 index 0000000000000000000000000000000000000000..1c03369c38aece818a61572969cbb84f53b5505e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/tfl_to_std.h @@ -0,0 +1,64 @@ +/* Copyright 2020 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_LITE_TFL_TO_STD_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_LITE_TFL_TO_STD_H_ + +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project + +namespace mlir { +namespace TFL { + +// Converts all the tfl.quantize/tfl.dequantize ops to the ops in the mlir.quant +// dialect ones in the function. +void ConvertTFLQuantOpsToMlirQuantOps(func::FuncOp func); + +// Converts all the mlir.quant dialect ops to the tfl.quantize/tfl.dequantize +// ops in the function. +void ConvertMlirQuantOpsToTFLQuantOps(func::FuncOp func); + +// A helper class to convert target function to another representation using +// `ConvertForward` function during construction and convert target function +// back to the original representation using `ConvertBackward` function during +// deconstruction. +template +class ScopedOpsConverter { + public: + explicit ScopedOpsConverter(func::FuncOp func) : func_(func) { + ConvertForward(func_); + } + + ScopedOpsConverter(const ScopedOpsConverter&) = delete; + ScopedOpsConverter operator=(const ScopedOpsConverter&) = delete; + ScopedOpsConverter(const ScopedOpsConverter&&) = delete; + ScopedOpsConverter operator=(const ScopedOpsConverter&&) = delete; + + ~ScopedOpsConverter() { ConvertBackward(func_); } + + private: + func::FuncOp func_; +}; + +using ScopedTFLQuantOpsToMlirQuantOpsConverter = + ScopedOpsConverter; +using ScopedMlirQuantOpsToTFLQuantOpsConverter = + ScopedOpsConverter; +} // namespace TFL +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_LITE_TFL_TO_STD_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..4bb6f4c240ac72481080f37b22984b5be3206fe3 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/BUILD @@ -0,0 +1,143 @@ +load("//tensorflow:tensorflow.bzl", "tf_cc_test") +load("//tensorflow:tensorflow.default.bzl", "get_compatible_with_portable") +load("//tensorflow/core/platform:rules_cc.bzl", "cc_library") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + default_visibility = [ + "//visibility:private", + ], + licenses = ["notice"], +) + +cc_library( + name = "portable_tensor_utils", + srcs = ["portable_tensor_utils.cc"], + hdrs = ["portable_tensor_utils.h"], + visibility = ["//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib:__pkg__"], +) + +cc_library( + name = "model_utils", + srcs = ["model_utils.cc"], + hdrs = ["model_utils.h"], + deps = [ + "//tensorflow/compiler/mlir/lite/schema:schema_conversion_utils", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs", + "//tensorflow/compiler/mlir/lite/schema:schema_utils", + "@com_google_absl//absl/strings", + ], +) + +tf_cc_test( + name = "model_utils_test", + srcs = ["model_utils_test.cc"], + deps = [ + ":model_utils", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs", + "@com_google_googletest//:gtest_main", + ], +) + +cc_library( + name = "quantization_utils", + srcs = ["quantization_utils.cc"], + hdrs = ["quantization_utils.h"], + deps = [ + ":model_utils", + ":portable_tensor_utils", + "//tensorflow/compiler/mlir/lite/kernels/internal:runtime_shape", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs", + "@com_google_absl//absl/status", + "@com_google_absl//absl/strings", + "@eigen_archive//:eigen3", + ], +) + +tf_cc_test( + name = "quantization_utils_test", + srcs = ["quantization_utils_test.cc"], + args = [ + "--test_model_file=$(location //tensorflow/compiler/mlir/lite/quantization/lite:testdata/single_conv_weights_min_0_max_plus_10.bin)", + ], + data = [ + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/single_conv_weights_min_0_max_plus_10.bin", + ], + deps = [ + ":quantization_utils", + "//tensorflow/compiler/mlir/lite/core:absl_error_model_builder", + "//tensorflow/compiler/mlir/lite/quantization/lite:test_util", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs", + "//tensorflow/compiler/mlir/lite/schema:schema_utils", + "@com_google_absl//absl/status", + "@com_google_googletest//:gtest", + "@eigen_archive//:eigen3", + "@tsl//tsl/platform:path", + "@tsl//tsl/platform:platform_port", + "@xla//xla/tsl/lib/core:status_test_util", + "@xla//xla/tsl/util:command_line_flags", + ], +) + +cc_library( + name = "quantize_weights", + srcs = select({ + "//tensorflow:ios": ["quantize_weights_portable.cc"], + "//tensorflow:android": ["quantize_weights_portable.cc"], + "//conditions:default": ["quantize_weights.cc"], + }), + hdrs = ["quantize_weights.h"], + compatible_with = get_compatible_with_portable(), + visibility = ["//visibility:public"], + deps = [ + ":model_utils", + ":portable_tensor_utils", + ":quantization_utils", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs", + "//tensorflow/compiler/mlir/lite/schema:schema_utils", + "//tensorflow/core/platform:logging", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/log", + "@com_google_absl//absl/memory", + "@com_google_absl//absl/status", + "@flatbuffers//:runtime_cc", + ] + select({ + "//tensorflow:android": [], + "//tensorflow:ios": [], + "//conditions:default": [ + "//tensorflow/compiler/mlir/lite/quantization/lite:quantize_weights", + ], + }), +) + +tf_cc_test( + name = "quantize_weights_test", + srcs = ["quantize_weights_test.cc"], + args = [ + "--test_model_file=$(location //tensorflow/compiler/mlir/lite/quantization/lite:testdata/single_conv_weights_min_0_max_plus_10.bin)", + ], + data = [ + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/custom_op.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/quantized_with_gather.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/single_conv_weights_min_0_max_plus_10.bin", + "//tensorflow/compiler/mlir/lite/quantization/lite:testdata/weight_shared_between_convs.bin", + ], + tags = [ + "tflite_not_portable_android", + "tflite_not_portable_ios", + ], + deps = [ + ":quantize_weights", + "//tensorflow/compiler/mlir/lite/core:absl_error_model_builder", + "//tensorflow/compiler/mlir/lite/quantization/lite:test_util", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs", + "//tensorflow/compiler/mlir/lite/schema:schema_utils", + "//tensorflow/core:framework_internal", + "@com_google_googletest//:gtest", + "@flatbuffers", + "@tsl//tsl/platform:path", + "@tsl//tsl/platform:platform_port", + "@xla//xla/tsl/util:command_line_flags", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/README b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/README new file mode 100644 index 0000000000000000000000000000000000000000..2642ea5e39d00f96a0a61992dd6d1f8781794f10 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/README @@ -0,0 +1,7 @@ +This folder organizes legacy toco quantize_weights and its dependencies for easy +deletion, per go/tfl_split_quantize_weights. + +When the MLIR quantizer replaces the legacy TOCO quantizer entirely, this entire +folder should be deleted. The partial copies of utils in this folder should not +be used anywhere but to support the legacy TOCO quantizer in this folder for +a clean and easy deletion. \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/model_utils.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/model_utils.cc new file mode 100644 index 0000000000000000000000000000000000000000..0269977ca0693f6397addcd8d20ac194fb21a637 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/model_utils.cc @@ -0,0 +1,108 @@ +/* Copyright 2019 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. +==============================================================================*/ +// This file is the MLIR copy of part of +// third_party/tensorflow/lite/tools/optimize/model_utils.cc as part of the +// effort to decouple TFLite from MLIR. + +#include "tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/model_utils.h" + +#include +#include +#include +#include +#include + +#include "tensorflow/compiler/mlir/lite/schema/schema_conversion_utils.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_utils.h" + +namespace mlir { +namespace lite { +namespace toco_legacy { + +using std::string; +using tflite::BuiltinOperator; +using tflite::BuiltinOperator_DEQUANTIZE; +using tflite::ModelT; +using tflite::OperatorCodeT; +using tflite::OperatorT; +using tflite::TensorT; +using tflite::TensorType; + +// LINT.IfChange(GetOrInsertOpCodeIndex) +// Returns the index of the OpCode. +// If a OpCode doesn't exist, adds it and returns its index. +int32_t GetOrInsertOpCodeIndex(ModelT* model, const BuiltinOperator& op_code, + int32_t version) { + for (size_t i = 0; i < model->operator_codes.size(); ++i) { + if (tflite::GetBuiltinCode(model->operator_codes[i].get()) == op_code) { + return i; + } + } + model->operator_codes.push_back(std::make_unique()); + int op_code_idx = model->operator_codes.size() - 1; + model->operator_codes[op_code_idx]->builtin_code = op_code; + model->operator_codes[op_code_idx]->deprecated_builtin_code = + tflite::ConvertBuiltinCodeToDeprecatedBuiltinCode(op_code); + // Version 2 and onwards supports INT8 inputs. + model->operator_codes[op_code_idx]->version = version; + + // Return the index of the newly placed OperatorCodeT. + return op_code_idx; +} +// LINT.ThenChange(//tensorflow/lite/tools/optimize/model_utils.cc:GetOrInsertOpCodeIndex) + +// LINT.IfChange(MakeDequantizeOperator) +// Creates a Dequantize OperatorT object. +void MakeDequantizeOperator(ModelT* model, std::unique_ptr* op, + int32_t input, int32_t output) { + OperatorT* op_raw = new OperatorT; + // Version 2 and onwards supports INT8 inputs. + op_raw->opcode_index = + GetOrInsertOpCodeIndex(model, BuiltinOperator_DEQUANTIZE, 2); + op_raw->inputs = {input}; + op_raw->outputs = {output}; + + op->reset(op_raw); +} +// LINT.ThenChange(//tensorflow/lite/tools/optimize/model_utils.cc:MakeDequantizeOperator) + +// LINT.IfChange(MakeTensor) +// Create a new TensorT object without quantization parameters. +void MakeTensor(const string& name, const std::vector& shape, + const std::vector& shape_signature, + const TensorType& type, std::unique_ptr* tensor) { + TensorT* tensor_raw = new TensorT; + tensor_raw->name = name; + tensor_raw->shape = shape; + if (!shape_signature.empty()) { + tensor_raw->shape_signature = shape_signature; + } + tensor_raw->type = type; + + tensor->reset(tensor_raw); +} +// LINT.ThenChange(//tensorflow/lite/tools/optimize/model_utils.cc:MakeTensor) + +// LINT.IfChange(HasMinMax) +bool HasMinMax(const TensorT* tensor) { + return tensor->quantization && !tensor->quantization->min.empty() && + !tensor->quantization->max.empty(); +} +// LINT.ThenChange(//tensorflow/lite/tools/optimize/model_utils.cc:HasMinMax) + +} // namespace toco_legacy +} // namespace lite +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/model_utils.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/model_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..13acbc1d469a4f9e39496bcdaa52a3b627bca6ae --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/model_utils.h @@ -0,0 +1,60 @@ +/* Copyright 2019 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. +==============================================================================*/ +// This file is the MLIR copy of part of +// third_party/tensorflow/lite/tools/optimize/model_utils.h as part of the +// effort to decouple TFLite from MLIR. + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_LITE_TOCO_LEGACY_MODEL_UTILS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_LITE_TOCO_LEGACY_MODEL_UTILS_H_ + +#include +#include +#include +#include + +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" + +namespace mlir { +namespace lite { +namespace toco_legacy { + +using std::string; +using tflite::ModelT; +using tflite::OperatorT; +using tflite::TensorT; +using tflite::TensorType; + +// LINT.IfChange(MakeDequantizeOperator) +// Creates a Dequantize OperatorT object. +void MakeDequantizeOperator(ModelT* model, std::unique_ptr* op, + int32_t input, int32_t output); +// LINT.ThenChange(//tensorflow/lite/tools/optimize/model_utils.h:MakeDequantizeOperator) + +// LINT.IfChange(MakeTensor) +// Create a new TensorT object without quantization parameters. +void MakeTensor(const string& name, const std::vector& shape, + const std::vector& shape_signature, + const TensorType& type, std::unique_ptr* tensor); +// LINT.ThenChange(//tensorflow/lite/tools/optimize/model_utils.h:MakeTensor) + +// LINT.IfChange(HasMinMax) +bool HasMinMax(const TensorT* tensor); +// LINT.ThenChange(//tensorflow/lite/tools/optimize/model_utils.h:HasMinMax) + +} // namespace toco_legacy +} // namespace lite +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_LITE_TOCO_LEGACY_MODEL_UTILS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/model_utils_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/model_utils_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..d711a700b3cc7190a2a95edde6c643617d780460 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/model_utils_test.cc @@ -0,0 +1,50 @@ +/* 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. +==============================================================================*/ +// This file is the MLIR copy of part of +// third_party/tensorflow/lite/tools/optimize/model_utils_test.cc as part of the +// effort to decouple TFLite from MLIR. + +#include "tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/model_utils.h" + +#include +#include + +#include +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" + +namespace mlir { +namespace lite { +namespace toco_legacy { + +namespace { + +using std::string; + +// LINT.IfChange(HasMinMaxTest) +TEST(ModelUtilsTest, HasMinMax) { + tflite::TensorT tensor; + tensor.quantization = std::make_unique(); + tensor.quantization->min.push_back(0.5); + EXPECT_FALSE(mlir::lite::toco_legacy::HasMinMax(&tensor)); + tensor.quantization->max.push_back(1.5); + EXPECT_TRUE(mlir::lite::toco_legacy::HasMinMax(&tensor)); +} +// LINT.ThenChange(//tensorflow/lite/tools/optimize/model_utils_test.cc:HasMinMaxTest) + +} // namespace + +} // namespace toco_legacy +} // namespace lite +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/portable_tensor_utils.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/portable_tensor_utils.cc new file mode 100644 index 0000000000000000000000000000000000000000..a071cbf4c0871f25854dafb42ab82f041313c73a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/portable_tensor_utils.cc @@ -0,0 +1,66 @@ +/* Copyright 2019 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. +==============================================================================*/ +// This file is the MLIR copy of part of +// third_party/tensorflow/lite/kernels/internal/reference/portable_tensor_utils.cc +// as part of the effort to decouple TFLite from MLIR. + +#include "tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/portable_tensor_utils.h" + +#include +#include +#include +#include + +namespace mlir { +namespace lite { +namespace toco_legacy { + +// LINT.IfChange(portable_symmetric_quantize_floats) +void PortableSymmetricQuantizeFloats(const float* values, const int size, + int8_t* quantized_values, float* min_value, + float* max_value, float* scaling_factor) { + auto minmax = std::minmax_element(values, values + size); + *min_value = *minmax.first; + *max_value = *minmax.second; + + PortableSymmetricQuantizeFloats(values, size, quantized_values, *min_value, + *max_value, scaling_factor); +} + +void PortableSymmetricQuantizeFloats(const float* values, const int size, + int8_t* quantized_values, float min_value, + float max_value, float* scaling_factor) { + const int32_t kScale = 127; + const float range = std::max(std::abs(min_value), std::abs(max_value)); + if (range == 0) { + memset(quantized_values, 0, size * sizeof(int8_t)); + *scaling_factor = 1; + return; + } + *scaling_factor = range / kScale; + const float scaling_factor_inv = kScale / range; + for (int i = 0; i < size; ++i) { + const int32_t quantized_value = + static_cast(round(values[i] * scaling_factor_inv)); + // Clamp: just in case some odd numeric offset. + quantized_values[i] = static_cast( + std::min(kScale, std::max(-kScale, quantized_value))); + } +} +// LINT.ThenChange(//tensorflow/lite/kernels/internal/reference/portable_tensor_utils.cc:portable_symmetric_quantize_floats) + +} // namespace toco_legacy +} // namespace lite +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/portable_tensor_utils.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/portable_tensor_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..e0de30461440db43f22035d163856cd25c5ddfea --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/portable_tensor_utils.h @@ -0,0 +1,42 @@ +/* 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. +==============================================================================*/ +// This file is the MLIR copy of part of +// third_party/tensorflow/lite/kernels/internal/reference/portable_tensor_utils.h +// as part of the effort to decouple TFLite from MLIR. + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_LITE_TOCO_LEGACY_PORTABLE_TENSOR_UTILS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_LITE_TOCO_LEGACY_PORTABLE_TENSOR_UTILS_H_ + +#include + +namespace mlir { +namespace lite { +namespace toco_legacy { + +// LINT.IfChange(portable_symmetric_quantize_floats) +void PortableSymmetricQuantizeFloats(const float* values, int size, + int8_t* quantized_values, float* min_value, + float* max_value, float* scaling_factor); + +void PortableSymmetricQuantizeFloats(const float* values, int size, + int8_t* quantized_values, float min_value, + float max_value, float* scaling_factor); +// LINT.ThenChange(//tensorflow/lite/kernels/internal/reference/portable_tensor_utils.h:portable_symmetric_quantize_floats) + +} // namespace toco_legacy +} // namespace lite +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_LITE_TOCO_LEGACY_PORTABLE_TENSOR_UTILS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/quantization_utils.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/quantization_utils.cc new file mode 100644 index 0000000000000000000000000000000000000000..1ab8e13ef00382796c843b8bd5d5f3e0e1942558 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/quantization_utils.cc @@ -0,0 +1,371 @@ +/* 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. +==============================================================================*/ +// This file is the MLIR copy of part of +// third_party/tensorflow/lite/tools/optimize/quantization_utils.cc as part of +// the effort to decouple TFLite from MLIR. + +#include "tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/quantization_utils.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "absl/status/status.h" +#include "absl/strings/str_cat.h" +#include "Eigen/Core" // from @eigen_archive +#include "tensorflow/compiler/mlir/lite/kernels/internal/runtime_shape.h" +#include "tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/model_utils.h" +#include "tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/portable_tensor_utils.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" + +namespace mlir { +namespace lite { +namespace toco_legacy { + +namespace { + +// LINT.IfChange(QuantizationUtilsConstants) +const int8_t kMinQuantizedValue8bit = -127; +const int8_t kMaxQuantizedValue8bit = 127; + +const int8_t kMinQuantizedValue4bit = -7; +const int8_t kMaxQuantizedValue4bit = 7; + +// The maximum number of dimensions supported in per-channel quantization. +constexpr int kPerChannelMaxDim = 4; +// LINT.ThenChange(//tensorflow/lite/tools/optimize/quantization_utils.cc:QuantizationUtilsConstants) +} // namespace + +using absl::InternalError; +using mlir::RuntimeShape; +using tflite::BufferT; +using tflite::QuantizationParametersT; +using tflite::TensorT; +using tflite::TensorType; +using tflite::TensorType_INT8; + +// LINT.IfChange(NumElements) +absl::Status NumElements(const TensorT& tensor, uint64_t* num_elements) { + *num_elements = 1; + for (const int64_t dim : tensor.shape) { + if (dim <= 0 || *num_elements > UINT64_MAX / static_cast(dim)) { + return InternalError("Invalid tensor shape."); + } + *num_elements *= dim; + } + return absl::OkStatus(); +} +// LINT.ThenChange(//tensorflow/lite/tools/optimize/quantization_utils.cc:NumElements) + +// LINT.IfChange(FillPerChannelMinMax) +absl::Status FillPerChannelMinMax( + const float* const input, const std::vector& dimension, + int32_t channel_dim_index, QuantizationParametersT* quantization_params) { + if (!quantization_params->min.empty() || !quantization_params->max.empty()) { + return absl::InvalidArgumentError( + "Min or max already present in tensor quantization params."); + } + + if (dimension.size() > kPerChannelMaxDim) { + return absl::InvalidArgumentError(absl::StrCat( + "Expected tensor with less than %d dimensions, but got %d.", + kPerChannelMaxDim + 1, dimension.size())); + } + if (channel_dim_index >= dimension.size()) { + return absl::InvalidArgumentError(absl::StrCat( + "Expected channel_dim_index to be less than %d, but got %d.", + dimension.size(), channel_dim_index)); + } + + const int32_t channel_dim_size = dimension[channel_dim_index]; + quantization_params->quantized_dimension = channel_dim_index; + quantization_params->min = std::vector(channel_dim_size); + quantization_params->max = std::vector(channel_dim_size); + std::vector has_min_max_value(channel_dim_size, false); + int indices[kPerChannelMaxDim]; + RuntimeShape unextended_tensor_dims(dimension.size(), dimension.data()); + RuntimeShape tensor_dims = + RuntimeShape::ExtendedShape(kPerChannelMaxDim, unextended_tensor_dims); + channel_dim_index += + kPerChannelMaxDim - unextended_tensor_dims.DimensionsCount(); + + // Compute min max ranges per channel + for (indices[0] = 0; indices[0] < tensor_dims.Dims(0); indices[0]++) { + for (indices[1] = 0; indices[1] < tensor_dims.Dims(1); indices[1]++) { + for (indices[2] = 0; indices[2] < tensor_dims.Dims(2); indices[2]++) { + for (indices[3] = 0; indices[3] < tensor_dims.Dims(3); indices[3]++) { + int channel_idx = indices[channel_dim_index]; + const float val = input[Offset(tensor_dims, indices)]; + if (has_min_max_value[channel_idx]) { + if (quantization_params->min[channel_idx] > val) { + quantization_params->min[channel_idx] = val; + } else if (quantization_params->max[channel_idx] < val) { + quantization_params->max[channel_idx] = val; + } + } else { + quantization_params->min[channel_idx] = val; + quantization_params->max[channel_idx] = val; + has_min_max_value[channel_idx] = true; + } + } + } + } + } + return absl::OkStatus(); +} +// LINT.ThenChange(//tensorflow/lite/tools/optimize/quantization_utils.cc:FillPerChannelMinMax) + +// LINT.IfChange(SymmetricPerChannelQuantization) +// Per-channel quantize a tensor at the given index and fills both scales and +// quantized values. +absl::Status SymmetricPerChannelQuantization( + TensorT* tensor, const float* const input, int32_t channel_dim_index, + std::vector* output_scales, std::vector* output_value) { + if (tensor == nullptr) { + return absl::InvalidArgumentError("Cannot quantize. Tensor is null."); + } + const int32_t channel_dim_size = tensor->shape[channel_dim_index]; + // Fill per channel max and min values if needed + if (tensor->quantization == nullptr) { + tensor->quantization = std::make_unique(); + } + if (!HasMinMax(tensor)) { + absl::Status status = FillPerChannelMinMax( + input, tensor->shape, channel_dim_index, tensor->quantization.get()); + if (!status.ok()) { + return status; + } + } + + // Calculate scales per channel using max and min values from tensor. + std::vector scale_invs(channel_dim_size); + const float half_scale = kMaxQuantizedValue8bit; + for (int channel_idx = 0; channel_idx < channel_dim_size; channel_idx++) { + const float half_range = + std::max(std::abs(tensor->quantization->min[channel_idx]), + std::abs(tensor->quantization->max[channel_idx])); + output_scales->at(channel_idx) = half_range / half_scale; + if (half_range == 0) { + scale_invs[channel_idx] = 0; + } else { + scale_invs[channel_idx] = half_scale / half_range; + } + } + + // Quantize the input values. + SymmetricPerChannelQuantizeValues(input, scale_invs, tensor->shape, + channel_dim_index, output_value); + return absl::OkStatus(); +} +// LINT.ThenChange(//tensorflow/lite/tools/optimize/quantization_utils.cc:SymmetricPerChannelQuantization) + +// LINT.IfChange(SymmetricPerChannelQuantizeValues) +void SymmetricPerChannelQuantizeValues(const float* const input, + const std::vector& scales_inv, + const std::vector& dimension, + int32_t channel_dim_index, + std::vector* output_value) { + // Quantize the values. + int indices[kPerChannelMaxDim]; + RuntimeShape unextended_tensor_dims(dimension.size(), dimension.data()); + RuntimeShape tensor_dims = + RuntimeShape::ExtendedShape(kPerChannelMaxDim, unextended_tensor_dims); + channel_dim_index += + kPerChannelMaxDim - unextended_tensor_dims.DimensionsCount(); + for (indices[0] = 0; indices[0] < tensor_dims.Dims(0); indices[0]++) { + for (indices[1] = 0; indices[1] < tensor_dims.Dims(1); indices[1]++) { + for (indices[2] = 0; indices[2] < tensor_dims.Dims(2); indices[2]++) { + for (indices[3] = 0; indices[3] < tensor_dims.Dims(3); indices[3]++) { + int channel_idx = indices[channel_dim_index]; + int index = Offset(tensor_dims, indices); + const float val = input[index]; + const int32_t quantized_value = + static_cast(round(val * scales_inv[channel_idx])); + output_value->at(index) = std::min( + kMaxQuantizedValue8bit, + std::max(kMinQuantizedValue8bit, quantized_value)); + } + } + } + } +} +// LINT.ThenChange(//tensorflow/lite/tools/optimize/quantization_utils.cc:SymmetricPerChannelQuantizeValues) + +// LINT.IfChange(SymmetricQuantizeTensor) +absl::Status SymmetricQuantizeTensor(ModelT* model, TensorT* tensor) { + if (model == nullptr || tensor == nullptr) { + return absl::InvalidArgumentError("No tensor to quantize."); + } + + BufferT* buffer = model->buffers[tensor->buffer].get(); + if (buffer == nullptr) { + return absl::InvalidArgumentError("Missing buffer."); + } + const float* float_data = reinterpret_cast(buffer->data.data()); + uint64_t num_elements; + absl::Status status = NumElements(*tensor, &num_elements); + if (!status.ok()) { + return status; + } + + std::vector quantized_buffer; + quantized_buffer.resize(num_elements); + + float min_value, max_value, scaling_factor; + mlir::lite::toco_legacy::PortableSymmetricQuantizeFloats( + float_data, num_elements, quantized_buffer.data(), &min_value, &max_value, + &scaling_factor); + + if (tensor->quantization == nullptr) { + tensor->quantization = std::make_unique(); + } + tensor->quantization->scale = std::vector(1, scaling_factor); + tensor->quantization->zero_point = std::vector(1, 0); + + uint8_t* uint8_buffer = reinterpret_cast(quantized_buffer.data()); + model->buffers[tensor->buffer]->data.assign(uint8_buffer, + uint8_buffer + num_elements); + + // Update the tensor type. + tensor->type = TensorType_INT8; + + return absl::OkStatus(); +} +// LINT.ThenChange(//tensorflow/lite/tools/optimize/quantization_utils.cc:SymmetricQuantizeTensor) + +// LINT.IfChange(QuantizeTensorFloat16) +absl::Status QuantizeTensorFloat16(ModelT* model, TensorT* tensor) { + if (model == nullptr || tensor == nullptr) { + return absl::InvalidArgumentError("No tensor to quantize."); + } + + BufferT* buffer = model->buffers[tensor->buffer].get(); + if (buffer == nullptr) { + return absl::InvalidArgumentError("Missing buffer."); + } + + uint64_t num_elements; + absl::Status status = NumElements(*tensor, &num_elements); + if (!status.ok()) { + return status; + } + + // Copy single byte buffer data to float vector to guard against misalignment. + std::vector float_vector(num_elements); + uint8_t* first = buffer->data.data(); + std::copy(first, first + buffer->data.size(), + reinterpret_cast(float_vector.data())); + + // Transform float data to float16. + std::vector quantized_buffer; + quantized_buffer.resize(num_elements); + constexpr float kMaxFloat16Value = 65504.f; + constexpr float kMinFloat16Value = -65504.f; + std::transform(float_vector.begin(), float_vector.end(), + quantized_buffer.begin(), [=](float a) { + float clamped = std::min(std::max(a, kMinFloat16Value), + kMaxFloat16Value); + return static_cast(clamped); + }); + + char* half_buffer = reinterpret_cast(quantized_buffer.data()); + model->buffers[tensor->buffer]->data.assign( + half_buffer, half_buffer + sizeof(Eigen::half) * num_elements); + + // Update the tensor type. + tensor->type = tflite::TensorType_FLOAT16; + + return absl::OkStatus(); +} +// LINT.ThenChange(//tensorflow/lite/tools/optimize/quantization_utils.cc:QuantizeTensorFloat16) + +// LINT.IfChange(AddQuantizationParams) +absl::Status AddQuantizationParams(const std::vector& scales, + const std::vector& zero_point, + int quantized_dimension, + const uint8_t* buffer_data, + size_t buffer_size, TensorType output_type, + ModelT* model, TensorT* tensor) { + if (tensor->quantization == nullptr) { + tensor->quantization = std::make_unique(); + } + tensor->quantization->scale.assign(scales.begin(), scales.end()); + if (zero_point.size() != scales.size()) { + return absl::InvalidArgumentError( + absl::StrCat("Received zero_point of size %d and scales of size %d. " + "These sizes should match.", + zero_point.size(), scales.size())); + } + tensor->quantization->zero_point.assign(zero_point.begin(), zero_point.end()); + tensor->quantization->quantized_dimension = quantized_dimension; + model->buffers[tensor->buffer]->data.assign(buffer_data, + buffer_data + buffer_size); + // Update the tensor type. + tensor->type = output_type; + return absl::OkStatus(); +} +// LINT.ThenChange(//tensorflow/lite/tools/optimize/quantization_utils.cc:AddQuantizationParams) + +// LINT.IfChange(SymmetricQuantizeTensorPerChannel) +absl::Status SymmetricQuantizeTensorPerChannel(ModelT* model, TensorT* tensor, + int32_t channel_dim_index) { + if (tensor->shape.size() > kPerChannelMaxDim) { + return absl::InvalidArgumentError(absl::StrCat( + "SymmetricQuantizeTensorPerChannel requires tensor with less than %d " + "dimensions, but got %d dimension(s).", + kPerChannelMaxDim + 1, tensor->shape.size())); + } + + // Get dimensions. + uint64_t num_elements; + absl::Status status = NumElements(*tensor, &num_elements); + if (!status.ok()) { + return status; + } + const int32_t channel_dim_size = tensor->shape[channel_dim_index]; + + // Get input float data. + const BufferT* buffer = model->buffers[tensor->buffer].get(); + const float* float_input_data = + reinterpret_cast(buffer->data.data()); + + // Create container for output scale and output data. + std::vector scales(channel_dim_size); + std::vector final_buffer(num_elements); + + // Quantize the input data with respect to channel_dim_index. + status = SymmetricPerChannelQuantization( + tensor, float_input_data, channel_dim_index, &scales, &final_buffer); + if (!status.ok()) { + return status; + } + + // Set the buffers and output type. + uint8_t* uint8_buffer = reinterpret_cast(final_buffer.data()); + const size_t buffer_size = num_elements * sizeof(int8_t); + std::vector zero_point(scales.size(), 0); + return AddQuantizationParams(scales, zero_point, channel_dim_index, + uint8_buffer, buffer_size, TensorType_INT8, + model, tensor); +} +// LINT.ThenChange(//tensorflow/lite/tools/optimize/quantization_utils.cc:SymmetricQuantizeTensorPerChannel) + +} // namespace toco_legacy +} // namespace lite +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/quantization_utils.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/quantization_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..df96a5f224a7c320a3bf740e10beff90802d6c06 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/quantization_utils.h @@ -0,0 +1,110 @@ +/* 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. +==============================================================================*/ +// This file is the MLIR copy of part of +// third_party/tensorflow/lite/tools/optimize/quantization_utils.h as part of +// the effort to decouple TFLite from MLIR. + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_LITE_TOCO_LEGACY_QUANTIZATION_UTILS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_LITE_TOCO_LEGACY_QUANTIZATION_UTILS_H_ + +#include +#include +#include + +#include "absl/status/status.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" + +namespace mlir { +namespace lite { +namespace toco_legacy { + +using tflite::ModelT; +using tflite::QuantizationParametersT; +using tflite::TensorT; +using tflite::TensorType; + +// LINT.IfChange(num_elements) +// Returns the number of elements in the given tensor. +absl::Status NumElements(const TensorT& tensor, uint64_t* num_elements); +// LINT.ThenChange(//tensorflow/lite/tools/optimize/quantization_utils.h:num_elements) + +// LINT.IfChange(fill_per_channel_min_max) +// Populates the max and min values for per channel quantization. +absl::Status FillPerChannelMinMax(const float* input, + const std::vector& dimension, + int32_t channel_dim_index, + QuantizationParametersT* quantization_params); +// LINT.ThenChange(//tensorflow/lite/tools/optimize/quantization_utils.h:fill_per_channel_min_max) + +// LINT.IfChange(symmetric_per_channel_quantization) +// Per-channel quantize a tensor at the given index and returns both scales and +// quantized values. +// Parameters: +// - tensor is the tensor to be quantized, needed to access associated +// quantization parameters +// - input is the float input data to be quantized. +// - channel_dim_index is the channel index within "dimension". +// dimension[channel_dim_index] gives the number of channels. +// - output_scale is the output scale, the size of which equals the number of +// channels. +// - output_value is the output data, the size of which equals the number of +// inputs. +absl::Status SymmetricPerChannelQuantization(TensorT* tensor, + const float* input, + int32_t channel_dim_index, + std::vector* output_scales, + std::vector* output_value); +// LINT.ThenChange(//tensorflow/lite/tools/optimize/quantization_utils.h:symmetric_per_channel_quantization) + +// LINT.IfChange(symmetric_per_channel_quantize_values) +// Quantize the values given an array of scales. +void SymmetricPerChannelQuantizeValues(const float* input, + const std::vector& scales_inv, + const std::vector& dimension, + int32_t channel_dim_index, + std::vector* output_value); +// LINT.ThenChange(//tensorflow/lite/tools/optimize/quantization_utils.h:symmetric_per_channel_quantize_values) + +// LINT.IfChange(symmetric_quantize_tensor) +// Quantizes tensor using symmetric quantization with the min and max elements +// of the tensor. +absl::Status SymmetricQuantizeTensor(ModelT* model, TensorT* tensor); +// LINT.ThenChange(//tensorflow/lite/tools/optimize/quantization_utils.h:symmetric_quantize_tensor) + +// LINT.IfChange(symmetric_quantize_tensor_per_channel) +// Quantizes tensor with per channel. +absl::Status SymmetricQuantizeTensorPerChannel(ModelT* model, TensorT* tensor, + int32_t channel_dim_index); +// LINT.ThenChange(//tensorflow/lite/tools/optimize/quantization_utils.h:symmetric_quantize_tensor_per_channel) + +// LINT.IfChange(quantize_tensor_float16) +// Quantizes tensor to float16. +absl::Status QuantizeTensorFloat16(ModelT* model, TensorT* tensor); +// LINT.ThenChange(//tensorflow/lite/tools/optimize/quantization_utils.h:quantize_tensor_float16) + +// LINT.IfChange(add_quantization_params) +absl::Status AddQuantizationParams(const std::vector& scales, + const std::vector& zero_point, + int quantized_dimension, + const uint8_t* buffer_data, + size_t buffer_size, TensorType output_type, + ModelT* model, TensorT* tensor); +// LINT.ThenChange(//tensorflow/lite/tools/optimize/quantization_utils.h:add_quantization_params) + +} // namespace toco_legacy +} // namespace lite +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_LITE_TOCO_LEGACY_QUANTIZATION_UTILS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/quantization_utils_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/quantization_utils_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..c427e7664800331b66572215f543e947a065d34d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/quantization_utils_test.cc @@ -0,0 +1,496 @@ +/* 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. +==============================================================================*/ +// This file is the MLIR copy of part of +// third_party/tensorflow/lite/tools/optimize/quantization_utils_test.cc as part +// of the effort to decouple TFLite from MLIR. + +#include "tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/quantization_utils.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include "absl/status/status.h" +#include "Eigen/Core" // from @eigen_archive +#include "tensorflow/compiler/mlir/lite/core/absl_error_model_builder.h" +#include "tensorflow/compiler/mlir/lite/quantization/lite/test_util.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_utils.h" +#include "xla/tsl/lib/core/status_test_util.h" +#include "xla/tsl/util/command_line_flags.h" +#include "tsl/platform/init_main.h" +#include "tsl/platform/path.h" + +namespace { +std::string* g_test_model_dir = nullptr; +} // namespace + +namespace mlir { +namespace lite { +namespace toco_legacy { +namespace { + +using mlir::TFL::FlatBufferModelAbslError; +using tflite::BuiltinOperator_CONV_2D; +using tflite::QuantizationParametersT; +using tflite::SubGraphT; +using tflite::TensorT; +using tflite::TensorType_FLOAT16; +using tflite::TensorType_FLOAT32; +using tflite::TensorType_INT8; + +std::unique_ptr ReadModel(const char* model) { + auto model_path = tsl::io::JoinPath(*g_test_model_dir, model); + return FlatBufferModelAbslError::BuildFromFile(model_path.c_str()); +} + +std::unique_ptr ReadConvModel() { + return ReadModel(mlir::lite::internal::kConvModelWith0Plus10Weights); +} + +using ::testing::ElementsAreArray; + +class QuantizationUtilsTest : public testing::Test {}; + +TEST_F(QuantizationUtilsTest, NumElements) { + TensorT tensor; + tensor.shape = {1, 2, 3, 4}; + uint64_t num_elements; + TF_EXPECT_OK(NumElements(tensor, &num_elements)); + EXPECT_EQ(num_elements, 1 * 2 * 3 * 4); + + tensor.shape = {5}; + TF_EXPECT_OK(NumElements(tensor, &num_elements)); + EXPECT_EQ(num_elements, 5); + + tensor.shape = {}; + TF_EXPECT_OK(NumElements(tensor, &num_elements)); + // Scalars with empty shape have 1 element. + EXPECT_EQ(num_elements, 1); + + tensor.shape = {1, 2, 3, -1}; + EXPECT_EQ(NumElements(tensor, &num_elements).code(), + absl::StatusCode::kInternal); +} + +TEST_F(QuantizationUtilsTest, SymmetricPerChannelQuantizationWithNullQParams) { + // Set up an input with [3, 2, 2, 2] size and 0 is the channel index. + const std::vector input = { + 3.0, 2.0, 5.0, -2.0, 3.0, 2.0, 5.0, -2.0, // Channel 1. + 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, // Channel 2. + 1.0, 0.0, -1.0, -2.0, -3.0, -4.0, -5.0, -6.0, // Channel 3. + }; + const int channel_index = 0; + + // Create holder for output scale and data. + std::vector output_scales(3); + std::vector output_data(3 * 2 * 2 * 2); + + // Call SymmetricPerChannelQuantization with quant_params as a null pointer + // and verify the result. + TensorT tensor = TensorT(); + tensor.quantization = nullptr; + tensor.shape = {3, 2, 2, 2}; + TF_EXPECT_OK(mlir::lite::toco_legacy::SymmetricPerChannelQuantization( + &tensor, input.data(), channel_index, &output_scales, &output_data)); + const std::vector expected_output_scales = {0.0393700786, 0.0629921257, + 0.0472440943}; + const std::vector expected_output_data = { + 76, 51, 127, -51, 76, 51, 127, -51, // Channel 1. + 16, 32, 48, 64, 79, 95, 111, 127, // Channel 2. + 21, 0, -21, -42, -64, -85, -106, -127, // Channel 3. + }; + EXPECT_THAT(output_scales, ElementsAreArray(expected_output_scales)); + EXPECT_THAT(output_data, ElementsAreArray(expected_output_data)); +} + +TEST_F(QuantizationUtilsTest, SymmetricPerChannelQuantization) { + // Set up an input with [3, 2, 2, 2] size and 0 is the channel index. + const std::vector input = { + 3.0, 2.0, 5.0, -2.0, 3.0, 2.0, 5.0, -2.0, // Channel 1. + 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, // Channel 2. + 1.0, 0.0, -1.0, -2.0, -3.0, -4.0, -5.0, -6.0, // Channel 3. + }; + const int32_t channel_index = 0; + + // Create holder for output scale and data. + std::vector output_scales(3); + std::vector output_data(3 * 2 * 2 * 2); + + // Initialize pointer to quantization parameters + TensorT tensor = TensorT(); + tensor.quantization = std::make_unique(); + tensor.shape = {3, 2, 2, 2}; + TF_EXPECT_OK(mlir::lite::toco_legacy::FillPerChannelMinMax( + input.data(), tensor.shape, channel_index, tensor.quantization.get())); + + // Test that FillPerChanneMinMax worked + const std::vector expected_mins = {-2.0, 1.0, -6.0}; + const std::vector expected_maxs = {5.0, 8.0, 1.0}; + EXPECT_THAT(tensor.quantization->min, ElementsAreArray(expected_mins)); + EXPECT_THAT(tensor.quantization->max, ElementsAreArray(expected_maxs)); + + // Call SymmetricPerChannelQuantization with quant_params as a null pointer + // and verify the result. + TF_EXPECT_OK(mlir::lite::toco_legacy::SymmetricPerChannelQuantization( + &tensor, input.data(), channel_index, &output_scales, &output_data)); + const std::vector expected_output_scales = {0.0393700786, 0.0629921257, + 0.0472440943}; + const std::vector expected_output_data = { + 76, 51, 127, -51, 76, 51, 127, -51, // Channel 1. + 16, 32, 48, 64, 79, 95, 111, 127, // Channel 2. + 21, 0, -21, -42, -64, -85, -106, -127, // Channel 3. + }; + EXPECT_THAT(output_scales, ElementsAreArray(expected_output_scales)); + EXPECT_THAT(output_data, ElementsAreArray(expected_output_data)); +} + +TEST_F(QuantizationUtilsTest, SymmetricPerChannelQuantization2DTensor) { + // Set up an input with [3, 8] size and 0 is the channel index. + const std::vector input = { + 3.0, 2.0, 5.0, -2.0, 3.0, 2.0, 5.0, -2.0, // Batch 1. + 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, // Batch 2. + 1.0, 0.0, -1.0, -2.0, -3.0, -4.0, -5.0, -6.0, // Batch 3. + }; + const int32_t channel_index = 1; + + // Create holder for output scale and data. + std::vector output_scales(8); + std::vector output_data(3 * 8); + + // Initialize pointer to quantization parameters + TensorT tensor = TensorT(); + tensor.quantization = std::make_unique(); + tensor.shape = {3, 8}; + TF_EXPECT_OK(mlir::lite::toco_legacy::FillPerChannelMinMax( + input.data(), tensor.shape, channel_index, tensor.quantization.get())); + + // Test that FillPerChanneMinMax worked + const std::vector expected_mins = {1.0, 0.0, -1.0, -2.0, + -3.0, -4.0, -5.0, -6.0}; + const std::vector expected_maxs = {3.0, 2.0, 5.0, 4.0, + 5.0, 6.0, 7.0, 8.0}; + EXPECT_THAT(tensor.quantization->min, ElementsAreArray(expected_mins)); + EXPECT_THAT(tensor.quantization->max, ElementsAreArray(expected_maxs)); + + // Call SymmetricPerChannelQuantization with quant_params as a null pointer + // and verify the result. + TF_EXPECT_OK(mlir::lite::toco_legacy::SymmetricPerChannelQuantization( + &tensor, input.data(), channel_index, &output_scales, &output_data)); + const std::vector expected_output_scales = { + 0.02362204724, 0.01574803149, 0.03937007874, 0.03149606299, + 0.03937007874, 0.04724409448, 0.05511811023, 0.06299212598}; + const std::vector expected_output_data = { + 127, 127, 127, -64, 76, 42, 91, -32, // Batch 1. + 42, 127, 76, 127, 127, 127, 127, 127, // Batch 2. + 42, 0, -25, -64, -76, -85, -91, -95, // Batch 3. + }; + EXPECT_THAT(output_scales, ElementsAreArray(expected_output_scales)); + EXPECT_THAT(output_data, ElementsAreArray(expected_output_data)); +} + +TEST_F(QuantizationUtilsTest, SymmetricPerChannelQuantizeValues) { + // Set up an input with [3, 1, 1, 2] size and 0 is the channel index. + const std::vector input = { + 13.0, 21.0, // Channel 1. + 21.0, 22.0, // Channel 2. + 31.0, 40.0, // Channel 3. + }; + const std::vector scales_inv = {2, 0.5, 3}; + const std::vector dimension = {3, 1, 1, 2}; + const int channel_index = 0; + + // Create holder for output data. + std::vector output_data(3 * 1 * 1 * 2); + + // Call SymmetricPerChannelQuantizeValues and verify the result. + SymmetricPerChannelQuantizeValues(input.data(), scales_inv, dimension, + channel_index, &output_data); + const std::vector expected_output_data = { + 26, 42, // Channel 1. + 11, 11, // Channel 2. + 93, 120, // Channel 3. + }; + EXPECT_THAT(output_data, ElementsAreArray(expected_output_data)); +} + +TEST_F(QuantizationUtilsTest, FillPerChannelMinMax) { + // Set up an input with [3, 1, 1, 2] size. + const std::vector input = { + 13.0, 21.0, // Channel 1. + 21.0, 22.0, // Channel 2. + 31.0, 40.0, // Channel 3. + }; + + // Initialize pointer to quantization parameters. + QuantizationParametersT quantization_params = QuantizationParametersT(); + std::vector dimension = {3, 1, 1, 2}; + int32_t channel_dim_idx = 0; + const std::vector expected_mins = {13.0, 21.0, 31.0}; + const std::vector expected_maxs = {21.0, 22.0, 40.0}; + + TF_EXPECT_OK(mlir::lite::toco_legacy::FillPerChannelMinMax( + input.data(), dimension, channel_dim_idx, &quantization_params)); + + EXPECT_EQ(quantization_params.min, expected_mins); + EXPECT_EQ(quantization_params.max, expected_maxs); + EXPECT_EQ(quantization_params.quantized_dimension, channel_dim_idx); +} + +TEST_F(QuantizationUtilsTest, FillPerChannelMinMaxFillDim3) { + // Set up an input with [3, 1, 1, 2] size. + const std::vector input = { + // Channel 1, Channel 2 + 13.0, 21.0, 21.0, 22.0, 31.0, 40.0, + }; + + // Initialize pointer to quantization parameters. + QuantizationParametersT quantization_params = QuantizationParametersT(); + std::vector dimension = {3, 1, 1, 2}; + int32_t channel_dim_idx = 3; + const std::vector expected_mins = {13.0, 21.0}; + const std::vector expected_maxs = {31.0, 40.0}; + + TF_EXPECT_OK(mlir::lite::toco_legacy::FillPerChannelMinMax( + input.data(), dimension, channel_dim_idx, &quantization_params)); + + EXPECT_EQ(quantization_params.min, expected_mins); + EXPECT_EQ(quantization_params.max, expected_maxs); + EXPECT_EQ(quantization_params.quantized_dimension, channel_dim_idx); +} + +TEST_F(QuantizationUtilsTest, FillPerChannelMinMax2DTensor) { + // Set up an input with [3, 2] size. + const std::vector input = { + // Channel 1, Channel 2 + 13.0, 21.0, 21.0, 22.0, 31.0, 40.0, + }; + + // Initialize pointer to quantization parameters. + QuantizationParametersT quantization_params = QuantizationParametersT(); + std::vector dimension = {3, 2}; + int32_t channel_dim_idx = 1; + const std::vector expected_mins = {13.0, 21.0}; + const std::vector expected_maxs = {31.0, 40.0}; + + TF_EXPECT_OK(mlir::lite::toco_legacy::FillPerChannelMinMax( + input.data(), dimension, channel_dim_idx, &quantization_params)); + + EXPECT_EQ(quantization_params.min, expected_mins); + EXPECT_EQ(quantization_params.max, expected_maxs); + EXPECT_EQ(quantization_params.quantized_dimension, channel_dim_idx); +} + +TEST_F(QuantizationUtilsTest, SymmetricQuantizeTensorNullInputs) { + EXPECT_EQ(SymmetricQuantizeTensor(nullptr, nullptr).code(), + absl::StatusCode::kInvalidArgument); +} + +TEST_F(QuantizationUtilsTest, SymmetricQuantizeTensorNullQuantParams) { + // Conv model has weights between 0 and 10. + // Quantize the weights tensor. + ASSERT_TRUE(g_test_model_dir); + ASSERT_FALSE(g_test_model_dir->empty()); + auto test_model = ReadConvModel(); + ASSERT_TRUE(test_model); + auto readonly_model = test_model->GetModel(); + ASSERT_TRUE(readonly_model); + ASSERT_TRUE(readonly_model->subgraphs()); + ASSERT_GE(readonly_model->subgraphs()->size(), 1); + tflite::ModelT model; + readonly_model->UnPackTo(&model); + auto subgraph = model.subgraphs[0].get(); + auto conv_op = subgraph->operators.at(0).get(); + ASSERT_EQ( + GetBuiltinCode(model.operator_codes.at(conv_op->opcode_index).get()), + BuiltinOperator_CONV_2D); + int32_t weights_tensor_idx = conv_op->inputs[1]; + TensorT* weights_tensor = subgraph->tensors.at(weights_tensor_idx).get(); + // Empty quantization parameters. + weights_tensor->quantization = std::make_unique(); + + EXPECT_EQ(weights_tensor->type, TensorType_FLOAT32); + size_t float_buffer_size = + model.buffers.at(weights_tensor->buffer)->data.size(); + + TF_EXPECT_OK(SymmetricQuantizeTensor(&model, weights_tensor)); + + size_t quant_buffer_size = + model.buffers.at(weights_tensor->buffer)->data.size(); + EXPECT_EQ(weights_tensor->type, TensorType_INT8); + EXPECT_EQ(quant_buffer_size * 4, float_buffer_size); +} + +TEST_F(QuantizationUtilsTest, SymmetricQuantizeTensor) { + // Conv model has weights between 0 and 10. + // Quantize the weights tensor. + ASSERT_TRUE(g_test_model_dir); + ASSERT_FALSE(g_test_model_dir->empty()); + auto test_model = ReadConvModel(); + ASSERT_TRUE(test_model); + auto readonly_model = test_model->GetModel(); + ASSERT_TRUE(readonly_model); + ASSERT_TRUE(readonly_model->subgraphs()); + ASSERT_GE(readonly_model->subgraphs()->size(), 1); + tflite::ModelT model; + readonly_model->UnPackTo(&model); + auto subgraph = model.subgraphs[0].get(); + auto conv_op = subgraph->operators.at(0).get(); + ASSERT_EQ( + GetBuiltinCode(model.operator_codes.at(conv_op->opcode_index).get()), + BuiltinOperator_CONV_2D); + int32_t weights_tensor_idx = conv_op->inputs[1]; + TensorT* weights_tensor = subgraph->tensors.at(weights_tensor_idx).get(); + + EXPECT_EQ(weights_tensor->type, TensorType_FLOAT32); + size_t float_buffer_size = + model.buffers.at(weights_tensor->buffer)->data.size(); + + TF_EXPECT_OK(SymmetricQuantizeTensor(&model, weights_tensor)); + + size_t quant_buffer_size = + model.buffers.at(weights_tensor->buffer)->data.size(); + EXPECT_EQ(weights_tensor->type, TensorType_INT8); + EXPECT_EQ(quant_buffer_size * 4, float_buffer_size); +} + +TEST_F(QuantizationUtilsTest, QuantizeFloat16Clamp) { + // Create data. + auto model = std::make_unique(); + auto subgraph = std::make_unique(); + auto tensor = std::make_unique(); + auto buffer = std::make_unique(); + constexpr int kNumElements = 6; + const std::vector weights = {2.0, 1.0, 65504., 65505, -65504., -99999}; + auto weights_reinterpreted_data = + reinterpret_cast(weights.data()); + buffer->data.assign(weights_reinterpreted_data, + weights_reinterpreted_data + weights.size() * 4); + tensor->buffer = 0; + tensor->shape = {1, kNumElements}; + + // Wire the model. + model->subgraphs.push_back(std::move(subgraph)); + model->subgraphs[0]->tensors.push_back(std::move(tensor)); + model->buffers.push_back(std::move(buffer)); + + // Call and verify. + TF_EXPECT_OK(QuantizeTensorFloat16(model.get(), + model->subgraphs[0]->tensors[0].get())); + auto weightsf16 = reinterpret_cast( + model->buffers[model->subgraphs[0]->tensors[0]->buffer]->data.data()); + std::vector wf32(kNumElements); + std::transform(weightsf16, weightsf16 + 6, wf32.begin(), + [](Eigen::half a) { return static_cast(a); }); + + EXPECT_THAT(wf32, + ElementsAreArray({2.0, 1.0, 65504., 65504., -65504., -65504.})); + EXPECT_EQ(model->subgraphs[0]->tensors[0]->type, TensorType_FLOAT16); +} + +TEST_F(QuantizationUtilsTest, QuantizeFloat16) { + // Conv model has weights between 0 and 10. + // Quantize the weights tensor. + ASSERT_TRUE(g_test_model_dir != nullptr); + ASSERT_FALSE(g_test_model_dir->empty()); + auto test_model = ReadConvModel(); + ASSERT_TRUE(test_model); + auto readonly_model = test_model->GetModel(); + ASSERT_TRUE(readonly_model); + ASSERT_TRUE(readonly_model->subgraphs()); + ASSERT_GE(readonly_model->subgraphs()->size(), 1); + tflite::ModelT model; + readonly_model->UnPackTo(&model); + auto subgraph = model.subgraphs[0].get(); + auto conv_op = subgraph->operators.at(0).get(); + ASSERT_EQ( + GetBuiltinCode(model.operator_codes.at(conv_op->opcode_index).get()), + BuiltinOperator_CONV_2D); + int32_t weights_tensor_idx = conv_op->inputs[1]; + TensorT* weights_tensor = subgraph->tensors.at(weights_tensor_idx).get(); + + EXPECT_EQ(weights_tensor->type, TensorType_FLOAT32); + size_t float_buffer_size = + model.buffers.at(weights_tensor->buffer)->data.size(); + + TF_EXPECT_OK(QuantizeTensorFloat16(&model, weights_tensor)); + + size_t quant_buffer_size = + model.buffers.at(weights_tensor->buffer)->data.size(); + EXPECT_EQ(weights_tensor->type, TensorType_FLOAT16); + EXPECT_EQ(quant_buffer_size * 2, float_buffer_size); +} + +TEST_F(QuantizationUtilsTest, AddQuantizationParams) { + // Create data. + auto model = std::make_unique(); + auto subgraph = std::make_unique(); + auto tensor = std::make_unique(); + auto buffer = std::make_unique(); + const std::vector scales = {0.5, 1.0, 1.5}; + const std::vector zero_points = {5, 10, 15}; + const int32_t quantizated_dimension = 3; + const std::vector buffer_data = {1, 2, 3, 4}; + const int32_t buffer_size = 4; + tensor->buffer = 0; + + // Wire the model. + model->subgraphs.push_back(std::move(subgraph)); + model->subgraphs[0]->tensors.push_back(std::move(tensor)); + model->buffers.push_back(std::move(buffer)); + + // Call and verify. + TF_EXPECT_OK(AddQuantizationParams(scales, zero_points, quantizated_dimension, + buffer_data.data(), buffer_size, + TensorType_INT8, model.get(), + model->subgraphs[0]->tensors[0].get())); + EXPECT_THAT(model->subgraphs[0]->tensors[0]->quantization->scale, + ElementsAreArray(scales)); + EXPECT_THAT(model->subgraphs[0]->tensors[0]->quantization->zero_point, + ElementsAreArray(zero_points)); + EXPECT_THAT(model->buffers[model->subgraphs[0]->tensors[0]->buffer]->data, + ElementsAreArray(buffer_data)); + EXPECT_EQ(model->subgraphs[0]->tensors[0]->type, TensorType_INT8); +} + + +} // namespace +} // namespace toco_legacy +} // namespace lite +} // namespace mlir + +int main(int argc, char** argv) { + std::string model_file; + const std::vector flag_list = { + tsl::Flag("test_model_file", &model_file, + "Path to test tflite model file."), + }; + + const bool parse_result = tsl::Flags::Parse(&argc, argv, flag_list); + if (!parse_result) { + std::cerr << "Required test_model_file\n"; + std::abort(); + } + g_test_model_dir = new std::string(tsl::io::Dirname(model_file)); + ::tsl::port::InitMain(argv[0], &argc, &argv); + return RUN_ALL_TESTS(); +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/quantize_weights.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/quantize_weights.cc new file mode 100644 index 0000000000000000000000000000000000000000..e1a6323f0a08635911867f627617f9194a27c63e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/quantize_weights.cc @@ -0,0 +1,752 @@ +/* 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 "tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/quantize_weights.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "absl/container/flat_hash_map.h" +#include "absl/container/flat_hash_set.h" +#include "absl/log/log.h" +#include "absl/status/status.h" +#include "flatbuffers/buffer.h" // from @flatbuffers +#include "flatbuffers/flatbuffer_builder.h" // from @flatbuffers +#include "tensorflow/compiler/mlir/lite/quantization/lite/quantize_weights.h" +#include "tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/model_utils.h" +#include "tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/quantization_utils.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_utils.h" +#include "tensorflow/core/platform/logging.h" + +namespace mlir { +namespace lite { +namespace toco_legacy { +namespace { + +using absl::flat_hash_set; +using mlir::lite::toco_legacy:: + CustomOpMap; // Use this instead of mlir::lite::CustomOpMap because that + // uses mlir::lite::CustomOpInfo in + // tensorflow/compiler/mlir/lite/quantization/lite/quantize_weights.h, + // and we need mlir::lite::toco_legacy::CustomOpInfo, in + // tensorflow/compiler/mlir/lite/quantization/lite/optimize/quantize_weights.h +using tflite::BufferT; +using tflite::BuiltinOperator; +using tflite::BuiltinOperator_BATCH_MATMUL; +using tflite::BuiltinOperator_BIDIRECTIONAL_SEQUENCE_LSTM; +using tflite::BuiltinOperator_BIDIRECTIONAL_SEQUENCE_RNN; +using tflite::BuiltinOperator_CONV_2D; +using tflite::BuiltinOperator_CUSTOM; +using tflite::BuiltinOperator_DEPTHWISE_CONV_2D; +using tflite::BuiltinOperator_EMBEDDING_LOOKUP; +using tflite::BuiltinOperator_FULLY_CONNECTED; +using tflite::BuiltinOperator_GATHER; +using tflite::BuiltinOperator_LSTM; +using tflite::BuiltinOperator_RNN; +using tflite::BuiltinOperator_SVDF; +using tflite::BuiltinOperator_TRANSPOSE_CONV; +using tflite::BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM; +using tflite::BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_RNN; +using tflite::FinishModelBuffer; +using tflite::GetBuiltinCode; +using tflite::Model; +using tflite::ModelT; +using tflite::OperatorCodeT; +using tflite::OperatorT; +using tflite::SubGraphT; +using tflite::TensorT; +using tflite::TensorType_FLOAT32; +using tflite::TensorType_INT8; + +struct ConsumerOpInfo { + OperatorT* op; + // The index of the op in the operators vector. + int32_t op_idx; + // The index of the tensor to quantize in subgraph->tensors. + int32_t op_input_idx; +}; + +struct TensorPerChannel { + TensorT* t; + bool is_per_channel; + int channel_dim; +}; + +// The default minimum number of elements a weights array must have to be +// quantized by this transformation. +const int kWeightsMinNumElementsDefault = 1024; + +// Redefined from tensorflow/lite/core/c/common.h as local const int instead of +// discouraged #define macro. +const int kTfLiteOptionalTensor = -1; + +// Convert the MLIR CustomOpMap from the TFlite CustomOpMap as their member +// variables differ. +void ConstructMLIRCustomOpMap(mlir::lite::CustomOpMap& mlir_map, + const CustomOpMap& tflite_map) { + for (const auto& entry : tflite_map) { + mlir_map[entry.first].quantizable_input_indices = + entry.second.quantizable_input_indices; + mlir_map[entry.first].is_weight_only = !entry.second.is_hybrid; + mlir_map[entry.first].no_side_effect = true; + } +} + +// Gets the operators that consume tensor_idx. +std::vector GetTensorConsumers(const ModelT* model, + const SubGraphT* subgraph, + int32_t tensor_idx) { + // TODO(suharshs): If this proves to be too slow, avoid calling it per tensor, + // instead doing one sweep for the entire model. + std::vector consumer_ops; + for (size_t op_idx = 0; op_idx < subgraph->operators.size(); ++op_idx) { + OperatorT* op = subgraph->operators[op_idx].get(); + if (op == nullptr) { + continue; + } + for (size_t i = 0; i < op->inputs.size(); ++i) { + if (op->inputs[i] == tensor_idx) { + consumer_ops.push_back( + {op, static_cast(op_idx), static_cast(i)}); + } + } + } + return consumer_ops; +} + +// Gets the list of op->inputs indices of the weights inputs to be quantized for +// the provided op. +std::vector GetWeightInputIndices(const OperatorCodeT* op_code, + const CustomOpMap& custom_op_map) { + const BuiltinOperator builtin_op_code = GetBuiltinCode(op_code); + if (builtin_op_code == BuiltinOperator_CUSTOM) { + const std::string custom_code = op_code->custom_code; + const auto& custom_op_info = custom_op_map.find(custom_code); + if (custom_op_info != custom_op_map.end()) { + return custom_op_info->second.quantizable_input_indices; + } + } else if (builtin_op_code == BuiltinOperator_CONV_2D || + builtin_op_code == BuiltinOperator_DEPTHWISE_CONV_2D || + builtin_op_code == BuiltinOperator_FULLY_CONNECTED || + builtin_op_code == BuiltinOperator_BATCH_MATMUL || + builtin_op_code == BuiltinOperator_EMBEDDING_LOOKUP || + builtin_op_code == BuiltinOperator_TRANSPOSE_CONV) { + return {1}; + } else if (builtin_op_code == BuiltinOperator_SVDF) { + // tensorflow/lite/kernels/svdf.cc + return {1, 2}; + } else if (builtin_op_code == BuiltinOperator_LSTM || + builtin_op_code == BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM) { + // tensorflow/lite/kernels/lstm.cc + // tensorflow/lite/kernels/unidirectional_sequence_lstm.cc + return {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 16}; + } else if (builtin_op_code == BuiltinOperator_RNN || + builtin_op_code == BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_RNN) { + // tensorflow/lite/kernels/basic_rnn.cc + // tensorflow/lite/kernels/unidirectional_sequence_rnn.cc + return {1, 2}; + } else if (builtin_op_code == BuiltinOperator_BIDIRECTIONAL_SEQUENCE_LSTM) { + // tensorflow/lite/kernels/bidirectional_sequence_lstm.cc + return {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 16, 18, 19, 20, 21, + 22, 23, 24, 25, 26, 27, 28, 33, 40, 41, 42, 43, 44, 45, 46, 47}; + } else if (builtin_op_code == BuiltinOperator_BIDIRECTIONAL_SEQUENCE_RNN) { + // tensorflow/lite/kernels/bidirectional_sequence_rnn.cc + return {1, 2, 4, 5, 6, 8, 9, 10, 11}; + } else if (builtin_op_code == BuiltinOperator_GATHER) { + // tensorflow/lite/kernels/gather.cc + return {0}; + } + return {}; +} + +// Checks that a specific input can be quantized. +bool IsQuantizedInput(const OperatorCodeT* op_code, + const CustomOpMap& custom_op_map, int op_input_idx) { + const auto quantized_input_indices = + GetWeightInputIndices(op_code, custom_op_map); + return std::find(std::begin(quantized_input_indices), + std::end(quantized_input_indices), + op_input_idx) != std::end(quantized_input_indices); +} + +// Returns true if the operator supports hybrid evaluation. +bool IsHybridEvaluationOp(const OperatorT* op, const OperatorCodeT* op_code, + const CustomOpMap& custom_op_map, + bool use_updated_hybrid_scheme) { + const BuiltinOperator builtin_op_code = GetBuiltinCode(op_code); + // Operations that support hybrid evaluation. + bool eval_hybrid = false; + if (builtin_op_code == BuiltinOperator_CUSTOM) { + const std::string custom_code = op_code->custom_code; + const auto custom_op_info = custom_op_map.find(custom_code); + if (custom_op_info == custom_op_map.end()) { + return {}; + } else { + return custom_op_info->second.is_hybrid; + } + } else if (builtin_op_code == BuiltinOperator_FULLY_CONNECTED || + builtin_op_code == BuiltinOperator_BATCH_MATMUL || + builtin_op_code == BuiltinOperator_CONV_2D || + builtin_op_code == BuiltinOperator_SVDF || + builtin_op_code == BuiltinOperator_RNN || + builtin_op_code == BuiltinOperator_BIDIRECTIONAL_SEQUENCE_LSTM || + builtin_op_code == BuiltinOperator_BIDIRECTIONAL_SEQUENCE_RNN || + builtin_op_code == BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM || + builtin_op_code == BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_RNN) { + eval_hybrid = true; + } else if (builtin_op_code == BuiltinOperator_LSTM) { + const tflite::LSTMOptionsT* options = op->builtin_options.AsLSTMOptions(); + // Only lstm kernel_type full supports hybrid evaluation. + if (options->kernel_type == tflite::LSTMKernelType_FULL) { + eval_hybrid = true; + } + } else if (builtin_op_code == BuiltinOperator_DEPTHWISE_CONV_2D) { + eval_hybrid = use_updated_hybrid_scheme; + } + return eval_hybrid; +} + +// Returns true if all of the op's inputs are quantized. +bool CheckAllOpInputsQuantized(const SubGraphT* subgraph, const OperatorT* op, + const OperatorCodeT* op_code, + const CustomOpMap& custom_op_map) { + std::vector op_input_indices = + GetWeightInputIndices(op_code, custom_op_map); + for (const int32_t op_input_idx : op_input_indices) { + int32_t tensor_idx = op->inputs[op_input_idx]; + + if (tensor_idx == -1) { + // Optional tensor. + continue; + } + + TensorT* tensor = subgraph->tensors[tensor_idx].get(); + + if (tensor->type != TensorType_INT8) { + return false; + } + } + return true; +} + +// Inserts Tensors for each input tensor of op that should be +// quantized into tensor_map. +absl::Status InsertQuantizableInputTensorsFromOperator( + const ModelT* model, OperatorT* op, uint64_t weights_min_num_elements, + const CustomOpMap& custom_op_map, + absl::flat_hash_map* tensor_map, + int subgraph_index, bool use_updated_hybrid_scheme) { + SubGraphT* subgraph = model->subgraphs.at(subgraph_index).get(); + const OperatorCodeT* op_code = model->operator_codes[op->opcode_index].get(); + auto builtin_code = GetBuiltinCode(op_code); + + std::vector op_input_indices = + GetWeightInputIndices(op_code, custom_op_map); + for (const int32_t op_input_idx : op_input_indices) { + int32_t tensor_idx = op->inputs[op_input_idx]; + if (tensor_idx == -1) { + LOG(INFO) << "Skipping optional tensor input " << op_input_idx + << " of operation " << EnumNameBuiltinOperator(builtin_code); + continue; + } + + TensorT* tensor = subgraph->tensors[tensor_idx].get(); + if (tensor->type != TensorType_FLOAT32) { + LOG(INFO) << "Skipping quantization of tensor " << tensor->name + << " that is not type float."; + continue; + } + + uint64_t num_elements; + if (!mlir::lite::toco_legacy::NumElements(*tensor, &num_elements).ok()) { + return absl::InternalError("Error in quantization_utils NumElements"); + } + if (num_elements < weights_min_num_elements) { + LOG(INFO) << "Skipping quantization of tensor " << tensor->name + << " because it has fewer than " << weights_min_num_elements + << " elements (" << num_elements << ")."; + continue; + } + + // Some tensors may have a null buffer vector, indicating an intermediate + // array. + if (model->buffers[tensor->buffer]->data.data() == nullptr) { + LOG(INFO) << "Skipping quantization of tensor " << tensor->name + << " because it has no allocated buffer."; + continue; + } + + if (builtin_code == BuiltinOperator_DEPTHWISE_CONV_2D) { + tensor_map->insert({tensor_idx, + {tensor, /*is_per_channel=*/use_updated_hybrid_scheme, + /*dim=*/3}}); + } else if (builtin_code == BuiltinOperator_CONV_2D) { + tensor_map->insert({tensor_idx, + {tensor, /*is_per_channel=*/use_updated_hybrid_scheme, + /*dim=*/0}}); + } else { + switch (builtin_code) { + case BuiltinOperator_BIDIRECTIONAL_SEQUENCE_LSTM: + op->builtin_options.AsBidirectionalSequenceLSTMOptions() + ->asymmetric_quantize_inputs = use_updated_hybrid_scheme; + break; + case BuiltinOperator_BIDIRECTIONAL_SEQUENCE_RNN: + op->builtin_options.AsBidirectionalSequenceRNNOptions() + ->asymmetric_quantize_inputs = use_updated_hybrid_scheme; + break; + case BuiltinOperator_FULLY_CONNECTED: + op->builtin_options.AsFullyConnectedOptions() + ->asymmetric_quantize_inputs = use_updated_hybrid_scheme; + break; + case BuiltinOperator_BATCH_MATMUL: + op->builtin_options.AsBatchMatMulOptions() + ->asymmetric_quantize_inputs = use_updated_hybrid_scheme; + break; + case BuiltinOperator_LSTM: + op->builtin_options.AsLSTMOptions()->asymmetric_quantize_inputs = + use_updated_hybrid_scheme; + break; + case BuiltinOperator_RNN: + op->builtin_options.AsRNNOptions()->asymmetric_quantize_inputs = + use_updated_hybrid_scheme; + break; + case BuiltinOperator_SVDF: + op->builtin_options.AsSVDFOptions()->asymmetric_quantize_inputs = + use_updated_hybrid_scheme; + break; + case BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM: + op->builtin_options.AsUnidirectionalSequenceLSTMOptions() + ->asymmetric_quantize_inputs = use_updated_hybrid_scheme; + break; + case BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_RNN: + op->builtin_options.AsSequenceRNNOptions() + ->asymmetric_quantize_inputs = use_updated_hybrid_scheme; + break; + default: + break; + } + tensor_map->insert({tensor_idx, {tensor, /*is_per_channel=*/false}}); + } + } + + return absl::OkStatus(); +} + +// Updates operator code versions for the operators with INT8 inputs. +void UpdateInt8OperatorVersions(ModelT* model, bool use_updated_hybrid_scheme) { + for (int i = 0, end = model->operator_codes.size(); i < end; ++i) { + const BuiltinOperator& op_code = + GetBuiltinCode(model->operator_codes[i].get()); + if (op_code == BuiltinOperator_RNN || + op_code == BuiltinOperator_BIDIRECTIONAL_SEQUENCE_RNN || + op_code == BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM || + op_code == BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_RNN) { + model->operator_codes[i]->version = use_updated_hybrid_scheme ? 3 : 2; + } else if (op_code == BuiltinOperator_BIDIRECTIONAL_SEQUENCE_LSTM || + op_code == BuiltinOperator_EMBEDDING_LOOKUP) { + model->operator_codes[i]->version = 3; + } else if (op_code == BuiltinOperator_LSTM) { + model->operator_codes[i]->version = use_updated_hybrid_scheme ? 4 : 3; + } else if (op_code == BuiltinOperator_CONV_2D) { + model->operator_codes[i]->version = use_updated_hybrid_scheme ? 5 : 2; + } else if (op_code == BuiltinOperator_FULLY_CONNECTED) { + model->operator_codes[i]->version = use_updated_hybrid_scheme ? 9 : 3; + } else if (op_code == BuiltinOperator_BATCH_MATMUL) { + model->operator_codes[i]->version = use_updated_hybrid_scheme ? 4 : 1; + } else if (op_code == BuiltinOperator_SVDF) { + model->operator_codes[i]->version = use_updated_hybrid_scheme ? 4 : 2; + } else if (op_code == BuiltinOperator_DEPTHWISE_CONV_2D) { + model->operator_codes[i]->version = 6; + } + } +} + +// Returns true if the op in consumer_op_infos can pass through quantization. +bool IsQuantizationPassThroughOps( + const ModelT* model, const std::vector& consumer_op_infos) { + if (consumer_op_infos.size() != 1) { + return false; + } + const OperatorT* consumer_op = consumer_op_infos.front().op; + const BuiltinOperator op_code = + GetBuiltinCode(model->operator_codes[consumer_op->opcode_index].get()); + return op_code == BuiltinOperator_GATHER || + op_code == BuiltinOperator_EMBEDDING_LOOKUP; +} + +// Copies quantization parameters from input to output and returns consumers of +// the output tensor as a tuple with values: +// - index of the output tensor +// - pointer to the output tensor +// - vector of consumers ops. +std::tuple> +PassQuantizationAndGetConsumers( + const ModelT* model, const SubGraphT* subgraph, + const std::vector& consumer_op_infos, + const CustomOpMap& custom_op_map) { + const OperatorT* op = consumer_op_infos.front().op; + const OperatorCodeT* op_code = model->operator_codes[op->opcode_index].get(); + if (op->outputs.size() != 1) { + LOG(ERROR) + << "An op that passes quantization has more than one quantized output"; + return std::make_tuple(-1, nullptr, std::vector()); + } + const int32_t output_tensor_idx = op->outputs.front(); + const auto input_idx = GetWeightInputIndices(op_code, custom_op_map); + if (input_idx.size() != 1) { + LOG(ERROR) + << "An op that passes quantization has more than one quantized input"; + return std::make_tuple(-1, nullptr, std::vector()); + } + const int32_t input_tensor_idx = op->inputs[input_idx.front()]; + + // Propagate quantization params. + const TensorT* input_tensor = subgraph->tensors[input_tensor_idx].get(); + TensorT* output_tensor = subgraph->tensors[output_tensor_idx].get(); + if (!output_tensor->quantization) { + output_tensor->quantization = + std::make_unique(); + } + *output_tensor->quantization = *input_tensor->quantization; + output_tensor->type = TensorType_INT8; + return std::make_tuple( + output_tensor_idx, output_tensor, + GetTensorConsumers(model, subgraph, output_tensor_idx)); +} + +inline bool IsOpDenylisted(const flat_hash_set& op_denylist, + const BuiltinOperator op_code) { + return op_denylist.find(op_code) != op_denylist.end(); +} + +absl::Status QuantizeWeightsInt8( + flatbuffers::FlatBufferBuilder* builder, const Model* input_model, + bool use_hybrid_evaluation, uint64_t weights_min_num_elements, + const CustomOpMap& custom_op_map, bool use_updated_hybrid_scheme, + const absl::flat_hash_set& op_denylist = {}) { + std::unique_ptr model; + model.reset(input_model->UnPack()); + + for (int subgraph_index = 0, end = model->subgraphs.size(); + subgraph_index < end; ++subgraph_index) { + SubGraphT* subgraph = model->subgraphs.at(subgraph_index).get(); + + absl::flat_hash_map tensor_map; + for (int i = 0; i < subgraph->operators.size(); ++i) { + OperatorT* op = subgraph->operators[i].get(); + absl::Status status = InsertQuantizableInputTensorsFromOperator( + model.get(), op, weights_min_num_elements, custom_op_map, &tensor_map, + subgraph_index, use_updated_hybrid_scheme); + if (!status.ok()) return status; + } + + for (std::pair tensor_pair : tensor_map) { + // Quantize the tensor. + if (tensor_pair.second.is_per_channel) { + if (!mlir::lite::toco_legacy::SymmetricQuantizeTensorPerChannel( + model.get(), tensor_pair.second.t, + tensor_pair.second.channel_dim) + .ok()) { + return absl::InternalError( + "SymmetricQuantizeTensorPerChannel failed"); + } + } else { + if (!mlir::lite::toco_legacy::SymmetricQuantizeTensor( + model.get(), tensor_pair.second.t) + .ok()) { + return absl::InternalError("SymmetricQuantizeTensor failed"); + } + } + } + + // Examine the tensor consumers to determine which require dequantize ops. + for (const auto& tensor_pair : tensor_map) { + int32_t tensor_idx = tensor_pair.first; + TensorT* tensor = tensor_pair.second.t; + std::vector consumer_op_infos = + GetTensorConsumers(model.get(), subgraph, tensor_idx); + if (IsQuantizationPassThroughOps(model.get(), consumer_op_infos)) { + std::tie(tensor_idx, tensor, consumer_op_infos) = + PassQuantizationAndGetConsumers(model.get(), subgraph, + consumer_op_infos, custom_op_map); + if (tensor_idx < 0) { + // Error message is already logged by PassQuantizationAndGetConsumers. + return absl::InternalError("PassQuantizationAndGetConsumers failed"); + } + } + + std::vector dequant_op_infos; // Ops that need dequants. + for (ConsumerOpInfo& consumer_op_info : consumer_op_infos) { + OperatorT* consumer_op = consumer_op_info.op; + const OperatorCodeT* consumer_op_code = + model->operator_codes[consumer_op->opcode_index].get(); + // If the op is a hybrid op and all the required tensors are quantized, + // we have no further work to do, but for all ops that require + // dequantization we need to add a Dequantize op. + bool eval_hybrid = + use_hybrid_evaluation && + !IsOpDenylisted(op_denylist, GetBuiltinCode(consumer_op_code)) && + IsHybridEvaluationOp(consumer_op, consumer_op_code, custom_op_map, + use_updated_hybrid_scheme) && + CheckAllOpInputsQuantized(subgraph, consumer_op, consumer_op_code, + custom_op_map) && + IsQuantizedInput(consumer_op_code, custom_op_map, + consumer_op_info.op_input_idx); + if (!eval_hybrid) { + dequant_op_infos.push_back(consumer_op_info); + } + } + + // Check if this tensor is an output tensor. + int32_t output_index = -1; + for (int32_t i = 0; i < subgraph->outputs.size(); ++i) { + if (subgraph->outputs[i] == tensor_idx) { + output_index = i; + break; + } + } + + // If no ops require dequant and it is not output, we are done for this + // tensor. + if (dequant_op_infos.empty() && output_index < 0) { + continue; + } + + // Create a new tensor to be the output of the dequantize op. + std::unique_ptr dequantize_output; + const std::string dequant_name = tensor->name + "_dequantize"; + mlir::lite::toco_legacy::MakeTensor( + dequant_name, tensor->shape, tensor->shape_signature, + TensorType_FLOAT32, &dequantize_output); + const int32_t dequantize_output_idx = subgraph->tensors.size(); + subgraph->tensors.push_back(std::move(dequantize_output)); + + // Create the Dequantize operation. + std::unique_ptr dequantize_op; + mlir::lite::toco_legacy::MakeDequantizeOperator( + model.get(), &dequantize_op, tensor_idx, dequantize_output_idx); + + // Update the op_input of all the ops that need the created dequantize + // operation. + int32_t min_op_idx = subgraph->operators.size(); + for (ConsumerOpInfo& dequant_op_info : dequant_op_infos) { + dequant_op_info.op->inputs[dequant_op_info.op_input_idx] = + dequantize_output_idx; + min_op_idx = std::min(dequant_op_info.op_idx, min_op_idx); + } + // Update output name. + if (output_index >= 0) { + subgraph->outputs[output_index] = dequantize_output_idx; + } + + // Insert the newly created Dequantize operation before the earliest + // consumer, since TFLite requires operators to be topo-sorted. + subgraph->operators.insert(subgraph->operators.begin() + min_op_idx, + std::move(dequantize_op)); + } + } + + // Update the modified operator code versions. + UpdateInt8OperatorVersions(model.get(), use_updated_hybrid_scheme); + + flatbuffers::Offset output_model_location = + Model::Pack(*builder, model.get()); + FinishModelBuffer(*builder, output_model_location); + + return absl::OkStatus(); +} + +absl::Status QuantizeWeightsFloat16(flatbuffers::FlatBufferBuilder* builder, + const Model* input_model) { + std::unique_ptr model; + model.reset(input_model->UnPack()); + + for (int subgraph_index = 0, end = model->subgraphs.size(); + subgraph_index < end; ++subgraph_index) { + SubGraphT* subgraph = model->subgraphs.at(subgraph_index).get(); + + absl::flat_hash_map tensor_map; + for (int i = 0, sub_end = subgraph->operators.size(); i < sub_end; ++i) { + OperatorT* op = subgraph->operators[i].get(); + for (auto tensor_idx : op->inputs) { + // Skip optional tensors. + if (tensor_idx == kTfLiteOptionalTensor) { + continue; + } + TensorT* tensor = subgraph->tensors[tensor_idx].get(); + BufferT* buffer = model->buffers[tensor->buffer].get(); + if (buffer == nullptr) { + return absl::InternalError("Buffer is null"); + } + // Quantize tensors that have data to quantize. + bool is_constant = !model->buffers[tensor->buffer].get()->data.empty(); + if (tensor->type == TensorType_FLOAT32 && is_constant) { + tensor_map.insert({tensor_idx, tensor}); + } + } + } + + // The hash map ensures that we quantize each tensor exactly once. + for (std::pair tensor_pair : tensor_map) { + // Quantize the tensor. + if (!mlir::lite::toco_legacy::QuantizeTensorFloat16(model.get(), + tensor_pair.second) + .ok()) { + return absl::InternalError("QuantizeTensorFloat16 failed"); + } + + int32_t tensor_idx = tensor_pair.first; + TensorT* tensor = tensor_pair.second; + std::vector dequant_op_infos = + GetTensorConsumers(model.get(), subgraph, tensor_idx); + + // Create a new tensor to be the output of the dequantize op. + std::unique_ptr dequantize_output; + const std::string dequant_name = tensor->name + "_dequantize"; + mlir::lite::toco_legacy::MakeTensor( + dequant_name, tensor->shape, tensor->shape_signature, + TensorType_FLOAT32, &dequantize_output); + const int32_t dequantize_output_idx = subgraph->tensors.size(); + subgraph->tensors.push_back(std::move(dequantize_output)); + + // Create the Dequantize operation. + std::unique_ptr dequantize_op; + mlir::lite::toco_legacy::MakeDequantizeOperator( + model.get(), &dequantize_op, tensor_idx, dequantize_output_idx); + + // Update the op_input of all the ops that need the created dequantize + // operation. + int32_t min_op_idx = subgraph->operators.size(); + for (ConsumerOpInfo& dequant_op_info : dequant_op_infos) { + dequant_op_info.op->inputs[dequant_op_info.op_input_idx] = + dequantize_output_idx; + min_op_idx = std::min(dequant_op_info.op_idx, min_op_idx); + } + + // Insert the newly created Dequantize operation before the earliest + // consumer, since TFLite requires operators to be topo-sorted. + subgraph->operators.insert(subgraph->operators.begin() + min_op_idx, + std::move(dequantize_op)); + } + } + + flatbuffers::Offset output_model_location = + Model::Pack(*builder, model.get()); + FinishModelBuffer(*builder, output_model_location); + return absl::OkStatus(); +} +} // namespace + +namespace internal { +absl::Status QuantizeWeights(flatbuffers::FlatBufferBuilder* builder, + const Model* input_model, + uint64_t weights_min_num_elements, + bool use_hybrid_evaluation, + QuantizerType quantizer_type) { + // By default we require that only weights with more than + // kWeightsMinSizeDefault elements are quantized. + if (quantizer_type == QuantizerType::MLIR_QUANTIZER) { + return mlir::lite::QuantizeWeights( + builder, input_model, weights_min_num_elements, use_hybrid_evaluation); + } + CustomOpMap custom_op_map; + return QuantizeWeightsInt8(builder, input_model, use_hybrid_evaluation, + weights_min_num_elements, custom_op_map, + kUseUpdatedHybridSchemeDefault); +} +} // namespace internal + +absl::Status QuantizeWeights(flatbuffers::FlatBufferBuilder* builder, + const Model* input_model, + uint64_t weights_min_num_elements, + QuantizerType quantizer_type) { + if (quantizer_type == QuantizerType::MLIR_QUANTIZER) { + return mlir::lite::QuantizeWeights(builder, input_model, + weights_min_num_elements); + } + CustomOpMap custom_op_map; + return QuantizeWeightsInt8(builder, input_model, true, + weights_min_num_elements, custom_op_map, + kUseUpdatedHybridSchemeDefault); +} + +absl::Status QuantizeWeights(flatbuffers::FlatBufferBuilder* builder, + const Model* input_model, BufferType quant_type, + bool use_updated_hybrid_scheme, + QuantizerType quantizer_type) { + // By default we require that only weights with more than + // kWeightsMinSizeDefault elements are quantized. + if (quantizer_type == QuantizerType::MLIR_QUANTIZER) { + return mlir::lite::QuantizeWeights(builder, input_model, + (mlir::lite::BufferType)quant_type, + use_updated_hybrid_scheme); + } + switch (quant_type) { + case BufferType::QUANTIZED_INT8: { + mlir::lite::toco_legacy::CustomOpMap custom_op_map; + return QuantizeWeightsInt8(builder, input_model, true, + kWeightsMinNumElementsDefault, custom_op_map, + use_updated_hybrid_scheme); + } + case BufferType::QUANTIZED_FLOAT16: + return QuantizeWeightsFloat16(builder, input_model); + } +} + +absl::Status QuantizeWeights(flatbuffers::FlatBufferBuilder* builder, + const Model* input_model, + uint64_t weights_min_num_elements, + const CustomOpMap& custom_op_map, + QuantizerType quantizer_type) { + if (quantizer_type == QuantizerType::MLIR_QUANTIZER) { + mlir::lite::CustomOpMap mlir_custom_op_map; + ConstructMLIRCustomOpMap(mlir_custom_op_map, custom_op_map); + return mlir::lite::QuantizeWeights( + builder, input_model, weights_min_num_elements, mlir_custom_op_map); + } + return QuantizeWeightsInt8(builder, input_model, true, + weights_min_num_elements, custom_op_map, + kUseUpdatedHybridSchemeDefault); +} + +absl::Status QuantizeWeights(flatbuffers::FlatBufferBuilder* builder, + const Model* input_model, + uint64_t weights_min_num_elements, + const CustomOpMap& custom_op_map, + bool use_updated_hybrid_scheme, + const flat_hash_set& op_denylist, + QuantizerType quantizer_type) { + if (quantizer_type == QuantizerType::MLIR_QUANTIZER) { + mlir::lite::CustomOpMap mlir_custom_op_map; + ConstructMLIRCustomOpMap(mlir_custom_op_map, custom_op_map); + return mlir::lite::QuantizeWeights( + builder, input_model, weights_min_num_elements, mlir_custom_op_map, + use_updated_hybrid_scheme, op_denylist); + } + return QuantizeWeightsInt8(builder, input_model, + /*use_hybrid_evaluation=*/true, + weights_min_num_elements, custom_op_map, + use_updated_hybrid_scheme, op_denylist); +} + +} // namespace toco_legacy +} // namespace lite +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/quantize_weights.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/quantize_weights.h new file mode 100644 index 0000000000000000000000000000000000000000..ab56f449fcb94700d3d36cdca8425d64d730faf9 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/quantize_weights.h @@ -0,0 +1,109 @@ +/* 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_LITE_TOCO_LEGACY_QUANTIZE_WEIGHTS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_LITE_TOCO_LEGACY_QUANTIZE_WEIGHTS_H_ + +#include +#include +#include +#include + +#include "absl/container/flat_hash_set.h" +#include "absl/status/status.h" +#include "flatbuffers/flatbuffer_builder.h" // from @flatbuffers +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" + +namespace mlir { +namespace lite { +namespace toco_legacy { + +using ::tflite::BuiltinOperator; +using ::tflite::Model; + +// Supported resulting types from quantization process. +enum class BufferType { QUANTIZED_INT8, QUANTIZED_FLOAT16 }; +enum class QuantizerType { OLD_QUANTIZER, MLIR_QUANTIZER }; + +// Stores information about how to quantize a user-specified custom operation. +struct CustomOpInfo { + std::vector quantizable_input_indices; + bool is_hybrid; +}; + +// Map from custom op code to custom op quantization information. +using CustomOpMap = std::unordered_map; + +// This macro is for internal use for conversions requiring previous behavior. +#ifdef TFLITE_USE_PREVIOUS_HYBRID_SCHEME +// Use asymmetric quantized activations and per-channel quantized weights. +constexpr bool kUseUpdatedHybridSchemeDefault = false; +#else +// Use symmetric quantized activations and per-channel quantized weights. +constexpr bool kUseUpdatedHybridSchemeDefault = true; +#endif + +// Quantizes input_model and populates the provided builder with the new model. +// By default only weights tensors weight more than 1024 elements will be +// quantized. +// +// A tflite::Model can be obtained from the builder with: +// const uint8_t* buffer = builder->GetBufferPointer(); +// tflite::Model* model = GetModel(buffer); +absl::Status QuantizeWeights( + flatbuffers::FlatBufferBuilder* builder, const Model* input_model, + BufferType quant_type = BufferType::QUANTIZED_INT8, + bool use_updated_hybrid_scheme = kUseUpdatedHybridSchemeDefault, + QuantizerType quantizer_type = QuantizerType::OLD_QUANTIZER); + +// Same as above, but only weights with greater than or equal +// weights_min_num_elements elements will be quantized. +absl::Status QuantizeWeights( + flatbuffers::FlatBufferBuilder* builder, const Model* input_model, + uint64_t weights_min_num_elements, + QuantizerType quantizer_type = QuantizerType::OLD_QUANTIZER); + +// Same as above, but with entry point of quantizing custom ops. +absl::Status QuantizeWeights( + flatbuffers::FlatBufferBuilder* builder, const Model* input_model, + uint64_t weights_min_num_elements, const CustomOpMap& custom_op_map, + QuantizerType quantizer_type = QuantizerType::OLD_QUANTIZER); + +// Same as above, but if use updated_hybrid_scheme is false, +// use previous quantization scheme. Optional op_denylist argument +// disables hybrid evaluation for provided BuiltinOperators. +absl::Status QuantizeWeights( + flatbuffers::FlatBufferBuilder* builder, const Model* input_model, + uint64_t weights_min_num_elements, const CustomOpMap& custom_op_map, + bool use_updated_hybrid_scheme, + const absl::flat_hash_set& op_denylist = {}, + QuantizerType quantizer_type = QuantizerType::OLD_QUANTIZER); + +namespace internal { +// If use_hybrid_evaluation is false, will disable using hybrid eval for +// operations that support it. +// +// We use this internal QuantizeWeights call to test models with hybrid +// evaluation disabled. +absl::Status QuantizeWeights( + flatbuffers::FlatBufferBuilder* builder, const Model* input_model, + uint64_t weights_min_num_elements, bool use_hybrid_evaluation, + QuantizerType quantizer_type = QuantizerType::OLD_QUANTIZER); +} // namespace internal + +} // namespace toco_legacy +} // namespace lite +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_LITE_TOCO_LEGACY_QUANTIZE_WEIGHTS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/quantize_weights_portable.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/quantize_weights_portable.cc new file mode 100644 index 0000000000000000000000000000000000000000..4af479c675413358ff7e9f208277cd7bdc6940f3 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/quantize_weights_portable.cc @@ -0,0 +1,739 @@ +/* Copyright 2022 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. +==============================================================================*/ +// clang-format off +#include "tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/quantize_weights.h" +// clang-format on + +#include +#include +#include +#include + +#include "absl/container/flat_hash_map.h" +#include "absl/memory/memory.h" +#include "absl/status/status.h" +#include "flatbuffers/buffer.h" // from @flatbuffers +#include "flatbuffers/flatbuffer_builder.h" // from @flatbuffers +#include "tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/model_utils.h" +#include "tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/portable_tensor_utils.h" +#include "tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/quantization_utils.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_utils.h" +#include "tensorflow/core/platform/logging.h" + +namespace mlir { +namespace lite { +namespace toco_legacy { +namespace { + +using absl::flat_hash_set; +using mlir::lite::toco_legacy:: + CustomOpMap; // Use this instead of mlir::lite::CustomOpMap because that + // uses mlir::lite::CustomOpInfo in + // tensorflow/compiler/mlir/lite/quantization/lite/quantize_weights.h, + // and we need mlir::lite::toco_legacy::CustomOpInfo, in + // tensorflow/compiler/mlir/lite/quantization/lite/optimize/quantize_weights.h +using tflite::BufferT; +using tflite::BuiltinOperator; +using tflite::BuiltinOperator_BATCH_MATMUL; +using tflite::BuiltinOperator_BIDIRECTIONAL_SEQUENCE_LSTM; +using tflite::BuiltinOperator_BIDIRECTIONAL_SEQUENCE_RNN; +using tflite::BuiltinOperator_CONV_2D; +using tflite::BuiltinOperator_CUSTOM; +using tflite::BuiltinOperator_DEPTHWISE_CONV_2D; +using tflite::BuiltinOperator_EMBEDDING_LOOKUP; +using tflite::BuiltinOperator_FULLY_CONNECTED; +using tflite::BuiltinOperator_GATHER; +using tflite::BuiltinOperator_LSTM; +using tflite::BuiltinOperator_RNN; +using tflite::BuiltinOperator_SVDF; +using tflite::BuiltinOperator_TRANSPOSE_CONV; +using tflite::BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM; +using tflite::BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_RNN; +using tflite::FinishModelBuffer; +using tflite::GetBuiltinCode; +using tflite::Model; +using tflite::ModelT; +using tflite::OperatorCodeT; +using tflite::OperatorT; +using tflite::SubGraphT; +using tflite::TensorT; +using tflite::TensorType_FLOAT32; +using tflite::TensorType_INT8; + +struct ConsumerOpInfo { + OperatorT* op; + // The index of the op in the operators vector. + int32_t op_idx; + // The index of the tensor to quantize in subgraph->tensors. + int32_t op_input_idx; +}; + +struct TensorPerChannel { + TensorT* t; + bool is_per_channel; + int channel_dim; +}; + +// The default minimum number of elements a weights array must have to be +// quantized by this transformation. +const int kWeightsMinNumElementsDefault = 1024; + +// Redefined from tensorflow/lite/core/c/common.h as local const int instead of +// discouraged #define macro. +const int kTfLiteOptionalTensor = -1; + +// Gets the operators that consume tensor_idx. +std::vector GetTensorConsumers(const ModelT* model, + const SubGraphT* subgraph, + int32_t tensor_idx) { + // TODO(suharshs): If this proves to be too slow, avoid calling it per tensor, + // instead doing one sweep for the entire model. + std::vector consumer_ops; + for (size_t op_idx = 0; op_idx < subgraph->operators.size(); ++op_idx) { + OperatorT* op = subgraph->operators[op_idx].get(); + if (op == nullptr) { + continue; + } + for (size_t i = 0; i < op->inputs.size(); ++i) { + if (op->inputs[i] == tensor_idx) { + consumer_ops.push_back( + {op, static_cast(op_idx), static_cast(i)}); + } + } + } + return consumer_ops; +} + +// Gets the list of op->inputs indices of the weights inputs to be quantized for +// the provided op. +std::vector GetWeightInputIndices(const OperatorCodeT* op_code, + const CustomOpMap& custom_op_map) { + const BuiltinOperator builtin_op_code = GetBuiltinCode(op_code); + if (builtin_op_code == BuiltinOperator_CUSTOM) { + const std::string custom_code = op_code->custom_code; + const auto& custom_op_info = custom_op_map.find(custom_code); + if (custom_op_info != custom_op_map.end()) { + return custom_op_info->second.quantizable_input_indices; + } + } else if (builtin_op_code == BuiltinOperator_CONV_2D || + builtin_op_code == BuiltinOperator_DEPTHWISE_CONV_2D || + builtin_op_code == BuiltinOperator_FULLY_CONNECTED || + builtin_op_code == BuiltinOperator_BATCH_MATMUL || + builtin_op_code == BuiltinOperator_EMBEDDING_LOOKUP || + builtin_op_code == BuiltinOperator_TRANSPOSE_CONV) { + return {1}; + } else if (builtin_op_code == BuiltinOperator_SVDF) { + // tensorflow/lite/kernels/svdf.cc + return {1, 2}; + } else if (builtin_op_code == BuiltinOperator_LSTM || + builtin_op_code == BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM) { + // tensorflow/lite/kernels/lstm.cc + // tensorflow/lite/kernels/unidirectional_sequence_lstm.cc + return {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 16}; + } else if (builtin_op_code == BuiltinOperator_RNN || + builtin_op_code == BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_RNN) { + // tensorflow/lite/kernels/basic_rnn.cc + // tensorflow/lite/kernels/unidirectional_sequence_rnn.cc + return {1, 2}; + } else if (builtin_op_code == BuiltinOperator_BIDIRECTIONAL_SEQUENCE_LSTM) { + // tensorflow/lite/kernels/bidirectional_sequence_lstm.cc + return {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 16, 18, 19, 20, 21, + 22, 23, 24, 25, 26, 27, 28, 33, 40, 41, 42, 43, 44, 45, 46, 47}; + } else if (builtin_op_code == BuiltinOperator_BIDIRECTIONAL_SEQUENCE_RNN) { + // tensorflow/lite/kernels/bidirectional_sequence_rnn.cc + return {1, 2, 4, 5, 6, 8, 9, 10, 11}; + } else if (builtin_op_code == BuiltinOperator_GATHER) { + // tensorflow/lite/kernels/gather.cc + return {0}; + } + return {}; +} + +// Checks that a specific input can be quantized. +bool IsQuantizedInput(const OperatorCodeT* op_code, + const CustomOpMap& custom_op_map, int op_input_idx) { + const auto quantized_input_indices = + GetWeightInputIndices(op_code, custom_op_map); + return std::find(std::begin(quantized_input_indices), + std::end(quantized_input_indices), + op_input_idx) != std::end(quantized_input_indices); +} + +// Returns true if the operator supports hybrid evaluation. +bool IsHybridEvaluationOp(const OperatorT* op, const OperatorCodeT* op_code, + const CustomOpMap& custom_op_map, + bool use_updated_hybrid_scheme) { + const BuiltinOperator builtin_op_code = GetBuiltinCode(op_code); + // Operations that support hybrid evaluation. + bool eval_hybrid = false; + if (builtin_op_code == BuiltinOperator_CUSTOM) { + const std::string custom_code = op_code->custom_code; + const auto custom_op_info = custom_op_map.find(custom_code); + if (custom_op_info == custom_op_map.end()) { + return {}; + } else { + return custom_op_info->second.is_hybrid; + } + } else if (builtin_op_code == BuiltinOperator_FULLY_CONNECTED || + builtin_op_code == BuiltinOperator_BATCH_MATMUL || + builtin_op_code == BuiltinOperator_CONV_2D || + builtin_op_code == BuiltinOperator_SVDF || + builtin_op_code == BuiltinOperator_RNN || + builtin_op_code == BuiltinOperator_BIDIRECTIONAL_SEQUENCE_LSTM || + builtin_op_code == BuiltinOperator_BIDIRECTIONAL_SEQUENCE_RNN || + builtin_op_code == BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM || + builtin_op_code == BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_RNN) { + eval_hybrid = true; + } else if (builtin_op_code == BuiltinOperator_LSTM) { + const tflite::LSTMOptionsT* options = op->builtin_options.AsLSTMOptions(); + // Only lstm kernel_type full supports hybrid evaluation. + if (options->kernel_type == tflite::LSTMKernelType_FULL) { + eval_hybrid = true; + } + } else if (builtin_op_code == BuiltinOperator_DEPTHWISE_CONV_2D) { + eval_hybrid = use_updated_hybrid_scheme; + } + return eval_hybrid; +} + +// Returns true if all of the op's inputs are quantized. +bool CheckAllOpInputsQuantized(const SubGraphT* subgraph, const OperatorT* op, + const OperatorCodeT* op_code, + const CustomOpMap& custom_op_map) { + std::vector op_input_indices = + GetWeightInputIndices(op_code, custom_op_map); + for (const int32_t op_input_idx : op_input_indices) { + int32_t tensor_idx = op->inputs[op_input_idx]; + + if (tensor_idx == -1) { + // Optional tensor. + continue; + } + + TensorT* tensor = subgraph->tensors[tensor_idx].get(); + + if (tensor->type != TensorType_INT8) { + return false; + } + } + return true; +} + +// Inserts Tensors for each input tensor of op that should be +// quantized into tensor_map. +absl::Status InsertQuantizableInputTensorsFromOperator( + const ModelT* model, OperatorT* op, uint64_t weights_min_num_elements, + const CustomOpMap& custom_op_map, + absl::flat_hash_map* tensor_map, + int subgraph_index, bool use_updated_hybrid_scheme) { + SubGraphT* subgraph = model->subgraphs.at(subgraph_index).get(); + const OperatorCodeT* op_code = model->operator_codes[op->opcode_index].get(); + auto builtin_code = GetBuiltinCode(op_code); + + std::vector op_input_indices = + GetWeightInputIndices(op_code, custom_op_map); + for (const int32_t op_input_idx : op_input_indices) { + int32_t tensor_idx = op->inputs[op_input_idx]; + if (tensor_idx == -1) { + LOG(INFO) << "Skipping optional tensor input " << op_input_idx + << " of operation " << EnumNameBuiltinOperator(builtin_code); + continue; + } + + TensorT* tensor = subgraph->tensors[tensor_idx].get(); + if (tensor->type != TensorType_FLOAT32) { + LOG(INFO) << "Skipping quantization of tensor " << tensor->name + << " that is not type float."; + continue; + } + + uint64_t num_elements; + if (!mlir::lite::toco_legacy::NumElements(*tensor, &num_elements).ok()) { + return absl::InternalError("Error in quantization_utils NumElements"); + } + if (num_elements < weights_min_num_elements) { + LOG(INFO) << "Skipping quantization of tensor " << tensor->name + << " because it has fewer than " << weights_min_num_elements + << " elements (" << num_elements << ")."; + continue; + } + + // Some tensors may have a null buffer vector, indicating an intermediate + // array. + if (model->buffers[tensor->buffer]->data.data() == nullptr) { + LOG(INFO) << "Skipping quantization of tensor " << tensor->name + << " because it has no allocated buffer."; + continue; + } + + if (builtin_code == BuiltinOperator_DEPTHWISE_CONV_2D) { + tensor_map->insert({tensor_idx, + {tensor, /*is_per_channel=*/use_updated_hybrid_scheme, + /*dim=*/3}}); + } else if (builtin_code == BuiltinOperator_CONV_2D) { + tensor_map->insert({tensor_idx, + {tensor, /*is_per_channel=*/use_updated_hybrid_scheme, + /*dim=*/0}}); + } else { + switch (builtin_code) { + case BuiltinOperator_BIDIRECTIONAL_SEQUENCE_LSTM: + op->builtin_options.AsBidirectionalSequenceLSTMOptions() + ->asymmetric_quantize_inputs = use_updated_hybrid_scheme; + break; + case BuiltinOperator_BIDIRECTIONAL_SEQUENCE_RNN: + op->builtin_options.AsBidirectionalSequenceRNNOptions() + ->asymmetric_quantize_inputs = use_updated_hybrid_scheme; + break; + case BuiltinOperator_FULLY_CONNECTED: + op->builtin_options.AsFullyConnectedOptions() + ->asymmetric_quantize_inputs = use_updated_hybrid_scheme; + break; + case BuiltinOperator_BATCH_MATMUL: + op->builtin_options.AsBatchMatMulOptions() + ->asymmetric_quantize_inputs = use_updated_hybrid_scheme; + break; + case BuiltinOperator_LSTM: + op->builtin_options.AsLSTMOptions()->asymmetric_quantize_inputs = + use_updated_hybrid_scheme; + break; + case BuiltinOperator_RNN: + op->builtin_options.AsRNNOptions()->asymmetric_quantize_inputs = + use_updated_hybrid_scheme; + break; + case BuiltinOperator_SVDF: + op->builtin_options.AsSVDFOptions()->asymmetric_quantize_inputs = + use_updated_hybrid_scheme; + break; + case BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM: + op->builtin_options.AsUnidirectionalSequenceLSTMOptions() + ->asymmetric_quantize_inputs = use_updated_hybrid_scheme; + break; + case BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_RNN: + op->builtin_options.AsSequenceRNNOptions() + ->asymmetric_quantize_inputs = use_updated_hybrid_scheme; + break; + default: + break; + } + tensor_map->insert({tensor_idx, {tensor, /*is_per_channel=*/false}}); + } + } + + return absl::OkStatus(); +} + +// Updates operator code versions for the operators with INT8 inputs. +void UpdateInt8OperatorVersions(ModelT* model, bool use_updated_hybrid_scheme) { + for (int i = 0, end = model->operator_codes.size(); i < end; ++i) { + const BuiltinOperator& op_code = + GetBuiltinCode(model->operator_codes[i].get()); + if (op_code == BuiltinOperator_RNN || + op_code == BuiltinOperator_BIDIRECTIONAL_SEQUENCE_RNN || + op_code == BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM || + op_code == BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_RNN) { + model->operator_codes[i]->version = use_updated_hybrid_scheme ? 3 : 2; + } else if (op_code == BuiltinOperator_BIDIRECTIONAL_SEQUENCE_LSTM || + op_code == BuiltinOperator_EMBEDDING_LOOKUP) { + model->operator_codes[i]->version = 3; + } else if (op_code == BuiltinOperator_LSTM) { + model->operator_codes[i]->version = use_updated_hybrid_scheme ? 4 : 3; + } else if (op_code == BuiltinOperator_CONV_2D) { + model->operator_codes[i]->version = use_updated_hybrid_scheme ? 5 : 2; + } else if (op_code == BuiltinOperator_FULLY_CONNECTED) { + model->operator_codes[i]->version = use_updated_hybrid_scheme ? 9 : 3; + } else if (op_code == BuiltinOperator_BATCH_MATMUL) { + model->operator_codes[i]->version = use_updated_hybrid_scheme ? 4 : 1; + } else if (op_code == BuiltinOperator_SVDF) { + model->operator_codes[i]->version = use_updated_hybrid_scheme ? 4 : 2; + } else if (op_code == BuiltinOperator_DEPTHWISE_CONV_2D) { + model->operator_codes[i]->version = 6; + } + } +} + +// Returns true if the op in consumer_op_infos can pass through quantization. +bool IsQuantizationPassThroughOps( + const ModelT* model, const std::vector& consumer_op_infos) { + if (consumer_op_infos.size() != 1) { + return false; + } + const OperatorT* consumer_op = consumer_op_infos.front().op; + const BuiltinOperator op_code = + GetBuiltinCode(model->operator_codes[consumer_op->opcode_index].get()); + return op_code == BuiltinOperator_GATHER || + op_code == BuiltinOperator_EMBEDDING_LOOKUP; +} + +// Copies quantization parameters from input to output and returns consumers of +// the output tensor as a tuple with values: +// - index of the output tensor +// - pointer to the output tensor +// - vector of consumers ops. +std::tuple> +PassQuantizationAndGetConsumers( + const ModelT* model, const SubGraphT* subgraph, + const std::vector& consumer_op_infos, + const CustomOpMap& custom_op_map) { + const OperatorT* op = consumer_op_infos.front().op; + const OperatorCodeT* op_code = model->operator_codes[op->opcode_index].get(); + if (op->outputs.size() != 1) { + LOG(ERROR) + << "An op that passes quantization has more than one quantized output"; + return std::make_tuple(-1, nullptr, std::vector()); + } + const int32_t output_tensor_idx = op->outputs.front(); + const auto input_idx = GetWeightInputIndices(op_code, custom_op_map); + if (input_idx.size() != 1) { + LOG(ERROR) + << "An op that passes quantization has more than one quantized input"; + return std::make_tuple(-1, nullptr, std::vector()); + } + const int32_t input_tensor_idx = op->inputs[input_idx.front()]; + + // Propagate quantization params. + const TensorT* input_tensor = subgraph->tensors[input_tensor_idx].get(); + TensorT* output_tensor = subgraph->tensors[output_tensor_idx].get(); + if (!output_tensor->quantization) { + output_tensor->quantization = + std::make_unique(); + } + *output_tensor->quantization = *input_tensor->quantization; + output_tensor->type = TensorType_INT8; + return std::make_tuple( + output_tensor_idx, output_tensor, + GetTensorConsumers(model, subgraph, output_tensor_idx)); +} + +inline bool IsOpDenylisted(const flat_hash_set& op_denylist, + const BuiltinOperator op_code) { + return op_denylist.find(op_code) != op_denylist.end(); +} + +absl::Status QuantizeWeightsInt8( + flatbuffers::FlatBufferBuilder* builder, const Model* input_model, + bool use_hybrid_evaluation, uint64_t weights_min_num_elements, + const CustomOpMap& custom_op_map, bool use_updated_hybrid_scheme, + const flat_hash_set& op_denylist = {}) { + std::unique_ptr model; + model.reset(input_model->UnPack()); + + for (int subgraph_index = 0, end = model->subgraphs.size(); + subgraph_index < end; ++subgraph_index) { + SubGraphT* subgraph = model->subgraphs.at(subgraph_index).get(); + + absl::flat_hash_map tensor_map; + for (int i = 0; i < subgraph->operators.size(); ++i) { + OperatorT* op = subgraph->operators[i].get(); + if (!InsertQuantizableInputTensorsFromOperator( + model.get(), op, weights_min_num_elements, custom_op_map, + &tensor_map, subgraph_index, use_updated_hybrid_scheme) + .ok()) { + return absl::InternalError( + "Failed to insert quantizable input tensors from operator"); + } + } + + for (std::pair tensor_pair : tensor_map) { + // Quantize the tensor. + if (tensor_pair.second.is_per_channel) { + if (!mlir::lite::toco_legacy::SymmetricQuantizeTensorPerChannel( + model.get(), tensor_pair.second.t, + tensor_pair.second.channel_dim) + .ok()) { + return absl::InternalError("Failed to quantize tensor per channel"); + } + } else { + if (!mlir::lite::toco_legacy::SymmetricQuantizeTensor( + model.get(), tensor_pair.second.t) + .ok()) { + return absl::InternalError("Failed to quantize tensor"); + } + } + } + + // Examine the tensor consumers to determine which require dequantize ops. + for (const auto& tensor_pair : tensor_map) { + int32_t tensor_idx = tensor_pair.first; + TensorT* tensor = tensor_pair.second.t; + std::vector consumer_op_infos = + GetTensorConsumers(model.get(), subgraph, tensor_idx); + if (IsQuantizationPassThroughOps(model.get(), consumer_op_infos)) { + std::tie(tensor_idx, tensor, consumer_op_infos) = + PassQuantizationAndGetConsumers(model.get(), subgraph, + consumer_op_infos, custom_op_map); + if (tensor_idx < 0) { + // Error message is already logged by PassQuantizationAndGetConsumers. + return absl::InternalError( + "Failed to pass quantization and get consumers"); + } + } + + std::vector dequant_op_infos; // Ops that need dequants. + for (ConsumerOpInfo& consumer_op_info : consumer_op_infos) { + OperatorT* consumer_op = consumer_op_info.op; + const OperatorCodeT* consumer_op_code = + model->operator_codes[consumer_op->opcode_index].get(); + // If the op is a hybrid op and all the required tensors are quantized, + // we have no further work to do, but for all ops that require + // dequantization we need to add a Dequantize op. + bool eval_hybrid = + use_hybrid_evaluation && + !IsOpDenylisted(op_denylist, GetBuiltinCode(consumer_op_code)) && + IsHybridEvaluationOp(consumer_op, consumer_op_code, custom_op_map, + use_updated_hybrid_scheme) && + CheckAllOpInputsQuantized(subgraph, consumer_op, consumer_op_code, + custom_op_map) && + IsQuantizedInput(consumer_op_code, custom_op_map, + consumer_op_info.op_input_idx); + if (!eval_hybrid) { + dequant_op_infos.push_back(consumer_op_info); + } + } + + // Check if this tensor is an output tensor. + int32_t output_index = -1; + for (int32_t i = 0; i < subgraph->outputs.size(); ++i) { + if (subgraph->outputs[i] == tensor_idx) { + output_index = i; + break; + } + } + + // If no ops require dequant and it is not output, we are done for this + // tensor. + if (dequant_op_infos.empty() && output_index < 0) { + continue; + } + + // Create a new tensor to be the output of the dequantize op. + std::unique_ptr dequantize_output; + const std::string dequant_name = tensor->name + "_dequantize"; + mlir::lite::toco_legacy::MakeTensor( + dequant_name, tensor->shape, tensor->shape_signature, + TensorType_FLOAT32, &dequantize_output); + const int32_t dequantize_output_idx = subgraph->tensors.size(); + subgraph->tensors.push_back(std::move(dequantize_output)); + + // Create the Dequantize operation. + std::unique_ptr dequantize_op; + mlir::lite::toco_legacy::MakeDequantizeOperator( + model.get(), &dequantize_op, tensor_idx, dequantize_output_idx); + + // Update the op_input of all the ops that need the created dequantize + // operation. + int32_t min_op_idx = subgraph->operators.size(); + for (ConsumerOpInfo& dequant_op_info : dequant_op_infos) { + dequant_op_info.op->inputs[dequant_op_info.op_input_idx] = + dequantize_output_idx; + min_op_idx = std::min(dequant_op_info.op_idx, min_op_idx); + } + // Update output name. + if (output_index >= 0) { + subgraph->outputs[output_index] = dequantize_output_idx; + } + + // Insert the newly created Dequantize operation before the earliest + // consumer, since TFLite requires operators to be topo-sorted. + subgraph->operators.insert(subgraph->operators.begin() + min_op_idx, + std::move(dequantize_op)); + } + } + + // Update the modified operator code versions. + UpdateInt8OperatorVersions(model.get(), use_updated_hybrid_scheme); + + flatbuffers::Offset output_model_location = + Model::Pack(*builder, model.get()); + FinishModelBuffer(*builder, output_model_location); + + return absl::OkStatus(); +} + +absl::Status QuantizeWeightsFloat16(flatbuffers::FlatBufferBuilder* builder, + const Model* input_model) { + std::unique_ptr model; + model.reset(input_model->UnPack()); + + for (int subgraph_index = 0, end = model->subgraphs.size(); + subgraph_index < end; ++subgraph_index) { + SubGraphT* subgraph = model->subgraphs.at(subgraph_index).get(); + + absl::flat_hash_map tensor_map; + for (int i = 0, sub_end = subgraph->operators.size(); i < sub_end; ++i) { + OperatorT* op = subgraph->operators[i].get(); + for (auto tensor_idx : op->inputs) { + // Skip optional tensors. + if (tensor_idx == kTfLiteOptionalTensor) { + continue; + } + TensorT* tensor = subgraph->tensors[tensor_idx].get(); + BufferT* buffer = model->buffers[tensor->buffer].get(); + if (buffer == nullptr) { + return absl::InternalError("Buffer is null"); + } + // Quantize tensors that have data to quantize. + bool is_constant = !model->buffers[tensor->buffer].get()->data.empty(); + if (tensor->type == TensorType_FLOAT32 && is_constant) { + tensor_map.insert({tensor_idx, tensor}); + } + } + } + + // The hash map ensures that we quantize each tensor exactly once. + for (std::pair tensor_pair : tensor_map) { + // Quantize the tensor. + if (!mlir::lite::toco_legacy::QuantizeTensorFloat16(model.get(), + tensor_pair.second) + .ok()) { + return absl::InternalError("QuantizeTensorFloat16 failed"); + } + + int32_t tensor_idx = tensor_pair.first; + TensorT* tensor = tensor_pair.second; + std::vector dequant_op_infos = + GetTensorConsumers(model.get(), subgraph, tensor_idx); + + // Create a new tensor to be the output of the dequantize op. + std::unique_ptr dequantize_output; + const std::string dequant_name = tensor->name + "_dequantize"; + mlir::lite::toco_legacy::MakeTensor( + dequant_name, tensor->shape, tensor->shape_signature, + TensorType_FLOAT32, &dequantize_output); + const int32_t dequantize_output_idx = subgraph->tensors.size(); + subgraph->tensors.push_back(std::move(dequantize_output)); + + // Create the Dequantize operation. + std::unique_ptr dequantize_op; + mlir::lite::toco_legacy::MakeDequantizeOperator( + model.get(), &dequantize_op, tensor_idx, dequantize_output_idx); + + // Update the op_input of all the ops that need the created dequantize + // operation. + int32_t min_op_idx = subgraph->operators.size(); + for (ConsumerOpInfo& dequant_op_info : dequant_op_infos) { + dequant_op_info.op->inputs[dequant_op_info.op_input_idx] = + dequantize_output_idx; + min_op_idx = std::min(dequant_op_info.op_idx, min_op_idx); + } + + // Insert the newly created Dequantize operation before the earliest + // consumer, since TFLite requires operators to be topo-sorted. + subgraph->operators.insert(subgraph->operators.begin() + min_op_idx, + std::move(dequantize_op)); + } + } + + flatbuffers::Offset output_model_location = + Model::Pack(*builder, model.get()); + FinishModelBuffer(*builder, output_model_location); + return absl::OkStatus(); +} +} // namespace + +namespace internal { +absl::Status QuantizeWeights(flatbuffers::FlatBufferBuilder* builder, + const Model* input_model, + uint64_t weights_min_num_elements, + bool use_hybrid_evaluation, + QuantizerType quantizer_type) { + if (quantizer_type == QuantizerType::MLIR_QUANTIZER) { + LOG(ERROR) << "Portable targets cannot use the MLIR quantizer."; + return absl::InternalError( + "Portable targets cannot use the MLIR quantizer."); + } + // By default we require that only weights with more than + // kWeightsMinSizeDefault elements are quantized. + CustomOpMap custom_op_map; + return QuantizeWeightsInt8(builder, input_model, use_hybrid_evaluation, + weights_min_num_elements, custom_op_map, + kUseUpdatedHybridSchemeDefault); +} +} // namespace internal + +absl::Status QuantizeWeights(flatbuffers::FlatBufferBuilder* builder, + const Model* input_model, + uint64_t weights_min_num_elements, + QuantizerType quantizer_type) { + if (quantizer_type == QuantizerType::MLIR_QUANTIZER) { + LOG(ERROR) << "Portable targets cannot use the MLIR quantizer."; + return absl::InternalError( + "Portable targets cannot use the MLIR quantizer."); + } + CustomOpMap custom_op_map; + return QuantizeWeightsInt8(builder, input_model, true, + weights_min_num_elements, custom_op_map, + kUseUpdatedHybridSchemeDefault); +} + +absl::Status QuantizeWeights(flatbuffers::FlatBufferBuilder* builder, + const Model* input_model, BufferType quant_type, + bool use_updated_hybrid_scheme, + QuantizerType quantizer_type) { + if (quantizer_type == QuantizerType::MLIR_QUANTIZER) { + LOG(ERROR) << "Portable targets cannot use the MLIR quantizer."; + return absl::InternalError( + "Portable targets cannot use the MLIR quantizer."); + } + switch (quant_type) { + case BufferType::QUANTIZED_INT8: { + // By default we require that only weights with more than + // kWeightsMinSizeDefault elements are quantized. + CustomOpMap custom_op_map; + return QuantizeWeightsInt8(builder, input_model, true, + kWeightsMinNumElementsDefault, custom_op_map, + use_updated_hybrid_scheme); + } + case BufferType::QUANTIZED_FLOAT16: + return QuantizeWeightsFloat16(builder, input_model); + } +} + +absl::Status QuantizeWeights(flatbuffers::FlatBufferBuilder* builder, + const Model* input_model, + uint64_t weights_min_num_elements, + const CustomOpMap& custom_op_map, + QuantizerType quantizer_type) { + if (quantizer_type == QuantizerType::MLIR_QUANTIZER) { + LOG(ERROR) << "Portable targets cannot use the MLIR quantizer."; + return absl::InternalError( + "Portable targets cannot use the MLIR quantizer."); + } + return QuantizeWeightsInt8(builder, input_model, true, + weights_min_num_elements, custom_op_map, + kUseUpdatedHybridSchemeDefault); +} + +absl::Status QuantizeWeights(flatbuffers::FlatBufferBuilder* builder, + const Model* input_model, + uint64_t weights_min_num_elements, + const CustomOpMap& custom_op_map, + bool use_updated_hybrid_scheme, + const flat_hash_set& op_denylist, + QuantizerType quantizer_type) { + if (quantizer_type == QuantizerType::MLIR_QUANTIZER) { + LOG(ERROR) << "Portable targets cannot use the MLIR quantizer."; + return absl::InternalError( + "Portable targets cannot use the MLIR quantizer."); + } + return QuantizeWeightsInt8(builder, input_model, + /*use_hybrid_evaluation=*/true, + weights_min_num_elements, custom_op_map, + use_updated_hybrid_scheme, op_denylist); +} + +} // namespace toco_legacy +} // namespace lite +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/quantize_weights_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/quantize_weights_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..971adae3f9358b3009a8878a87bbb672f9f01645 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/quantize_weights_test.cc @@ -0,0 +1,701 @@ +/* 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 "tensorflow/compiler/mlir/lite/quantization/lite/toco_legacy/quantize_weights.h" + +#include +#include +#include +#include +#include +#include +#include + +#include +#include "flatbuffers/flatbuffer_builder.h" // from @flatbuffers +#include "flatbuffers/vector.h" // from @flatbuffers +#include "tensorflow/compiler/mlir/lite/core/absl_error_model_builder.h" +#include "tensorflow/compiler/mlir/lite/quantization/lite/test_util.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_utils.h" +#include "xla/tsl/util/command_line_flags.h" +#include "tsl/platform/init_main.h" +#include "tsl/platform/path.h" + +namespace { +std::string* g_test_model_dir = nullptr; +} // namespace + +namespace mlir { +namespace lite { +namespace toco_legacy { +namespace { + +using mlir::TFL::FlatBufferModelAbslError; +using tflite::BuiltinOperator_CONV_2D; +using tflite::BuiltinOperator_CUSTOM; +using tflite::BuiltinOperator_DEQUANTIZE; +using tflite::GetModel; +using tflite::Model; +using tflite::TensorType_FLOAT16; +using tflite::TensorType_FLOAT32; +using tflite::TensorType_INT8; + +std::unique_ptr ReadTestModel() { + auto model_path = tsl::io::JoinPath( + *g_test_model_dir, ::mlir::lite::internal::kConvModelWith0Plus10Weights); + return FlatBufferModelAbslError::BuildFromFile(model_path.c_str()); +} + +std::unique_ptr ReadSharedWeightsTestModel() { + auto model_path = tsl::io::JoinPath( + *g_test_model_dir, ::mlir::lite::internal::kModelWithSharedWeights); + return FlatBufferModelAbslError::BuildFromFile(model_path.c_str()); +} + +std::unique_ptr ReadGatherTestModel() { + auto model_path = tsl::io::JoinPath( + *g_test_model_dir, ::mlir::lite::internal::kQuantizedWithGather); + return FlatBufferModelAbslError::BuildFromFile(model_path.c_str()); +} + +std::unique_ptr ReadCustomOpTestModel() { + auto model_path = tsl::io::JoinPath( + *g_test_model_dir, ::mlir::lite::internal::kModelWithCustomOp); + return FlatBufferModelAbslError::BuildFromFile(model_path.c_str()); +} + +template +std::vector GetAsVector(const flatbuffers::Vector* vec) { + return std::vector(vec->begin(), vec->end()); +} + +class QuantizeWeightsTest : public testing::Test { + protected: + QuantizeWeightsTest() = default; + + void LoadBasicModel() { + input_model_ = ReadTestModel(); + model_ = input_model_->GetModel(); + } + + void LoadSharedWeightsModel() { + input_model_ = ReadSharedWeightsTestModel(); + model_ = input_model_->GetModel(); + } + + void LoadGatherTestModel() { + input_model_ = ReadGatherTestModel(); + model_ = input_model_->GetModel(); + } + + void LoadCustomOpTestModel() { + input_model_ = ReadCustomOpTestModel(); + model_ = input_model_->GetModel(); + } + + std::unique_ptr input_model_; + const Model* model_; + + bool IsModelInputOrOutput(const Model* model, uint32_t tensor_idx) { + for (size_t subgraph_idx = 0; subgraph_idx < model_->subgraphs()->size(); + ++subgraph_idx) { + const auto subgraph = model->subgraphs()->Get(subgraph_idx); + for (size_t i = 0; i < subgraph->inputs()->size(); ++i) { + if (subgraph->inputs()->Get(i) == tensor_idx) { + return true; + } + } + for (size_t i = 0; i < subgraph->outputs()->size(); ++i) { + if (subgraph->outputs()->Get(i) == tensor_idx) { + return true; + } + } + } + return false; + } + + // Returns the producer op code of the specified tensor_idx. + bool GetProducerOpCode(const Model* model, uint32_t subgraph_idx, + uint32_t tensor_idx, + tflite::BuiltinOperator* op_code) { + const auto subgraph = model->subgraphs()->Get(subgraph_idx); + for (size_t op_idx = 0; op_idx < subgraph->operators()->size(); ++op_idx) { + const auto op = subgraph->operators()->Get(op_idx); + for (size_t i = 0; i < op->outputs()->size(); ++i) { + if (op->outputs()->Get(i) == tensor_idx) { + const uint32_t op_code_idx = op->opcode_index(); + *op_code = GetBuiltinCode(model->operator_codes()->Get(op_code_idx)); + return true; + } + } + } + return false; + } +}; + +TEST_F(QuantizeWeightsTest, QuantizationSucceeds) { + LoadBasicModel(); + flatbuffers::FlatBufferBuilder builder; + ASSERT_TRUE( + QuantizeWeights(&builder, model_, 0, QuantizerType::OLD_QUANTIZER).ok()); + + const uint8_t* buffer = builder.GetBufferPointer(); + const Model* output_model = GetModel(buffer); + ASSERT_TRUE(output_model); +} + +TEST_F(QuantizeWeightsTest, WeightsMinNumElements) { + LoadBasicModel(); + // Make weights_min_size sufficiently large such that no quantization should + // happen, i.e. the original model is the same size as the old one. + flatbuffers::FlatBufferBuilder builder; + const uint64_t kWeightsMinNumElements = 1000000; + ASSERT_TRUE(QuantizeWeights(&builder, model_, kWeightsMinNumElements, + QuantizerType::OLD_QUANTIZER) + .ok()); + + const uint8_t* buffer = builder.GetBufferPointer(); + const Model* output_model = GetModel(buffer); + ASSERT_TRUE(output_model); + + for (size_t subgraph_idx = 0; subgraph_idx < model_->subgraphs()->size(); + subgraph_idx++) { + const auto quantized_graph = output_model->subgraphs()->Get(subgraph_idx); + const auto float_graph = model_->subgraphs()->Get(subgraph_idx); + ASSERT_EQ(quantized_graph->tensors()->size(), + float_graph->tensors()->size()); + for (size_t i = 0; i < quantized_graph->tensors()->size(); i++) { + const auto quant_tensor = quantized_graph->tensors()->Get(i); + const auto float_tensor = float_graph->tensors()->Get(i); + // Everything should remain equal between the two graphs. + EXPECT_EQ(quant_tensor->buffer(), float_tensor->buffer()); + EXPECT_EQ(quant_tensor->is_variable(), float_tensor->is_variable()); + EXPECT_EQ(GetAsVector(quant_tensor->shape()), + GetAsVector(float_tensor->shape())); + EXPECT_EQ(quant_tensor->name()->str(), float_tensor->name()->str()); + EXPECT_EQ(quant_tensor->type(), float_tensor->type()); + } + } +} + +TEST_F(QuantizeWeightsTest, HybridConv) { + LoadBasicModel(); + flatbuffers::FlatBufferBuilder builder; + ASSERT_TRUE( + QuantizeWeights(&builder, model_, 0, QuantizerType::OLD_QUANTIZER).ok()); + + const uint8_t* buffer = builder.GetBufferPointer(); + const Model* output_model = GetModel(buffer); + ASSERT_TRUE(output_model); + + // Nothing should change. + ASSERT_EQ(output_model->subgraphs()->size(), model_->subgraphs()->size()); + for (size_t subgraph_idx = 0; subgraph_idx < model_->subgraphs()->size(); + subgraph_idx++) { + const auto quantized_graph = output_model->subgraphs()->Get(subgraph_idx); + const auto float_graph = model_->subgraphs()->Get(subgraph_idx); + ASSERT_EQ(quantized_graph->tensors()->size(), + float_graph->tensors()->size()); + // Make sure the graph only has one Conv operation. + ASSERT_EQ(quantized_graph->operators()->size(), 1); + const auto op = quantized_graph->operators()->Get(0); + const uint32_t op_code_idx = op->opcode_index(); + ASSERT_EQ(GetBuiltinCode(output_model->operator_codes()->Get(op_code_idx)), + BuiltinOperator_CONV_2D); + for (size_t i = 0; i < quantized_graph->tensors()->size(); i++) { + const auto quant_tensor = quantized_graph->tensors()->Get(i); + const auto float_tensor = float_graph->tensors()->Get(i); + EXPECT_EQ(quant_tensor->buffer(), float_tensor->buffer()); + EXPECT_EQ(quant_tensor->is_variable(), float_tensor->is_variable()); + EXPECT_EQ(GetAsVector(quant_tensor->shape()), + GetAsVector(float_tensor->shape())); + EXPECT_EQ(quant_tensor->name()->str(), float_tensor->name()->str()); + // If the tensor is a weight, it should have type INT8, otherwise it + // should stay with type FLOAT32. + // If the tensor is a bias, it should have type FLOAT32. + if (quant_tensor->name()->str() == "conv_bias") { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } else if (IsModelInputOrOutput(output_model, i)) { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } else if (quant_tensor->buffer() != 0) { + EXPECT_EQ(quant_tensor->type(), TensorType_INT8) + << quant_tensor->name()->str(); + auto shape = GetAsVector(quant_tensor->shape()); + if (kUseUpdatedHybridSchemeDefault) { + EXPECT_EQ(quant_tensor->quantization()->scale()->size(), shape[0]); + } else { + EXPECT_EQ(quant_tensor->quantization()->scale()->size(), 1); + } + } else { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } + } + } +} + +TEST_F(QuantizeWeightsTest, DequantizeConv) { + LoadBasicModel(); + flatbuffers::FlatBufferBuilder builder; + ASSERT_TRUE(internal::QuantizeWeights(&builder, model_, 0, + /*use_hybrid_evaluation=*/false, + QuantizerType::OLD_QUANTIZER) + .ok()); + + const uint8_t* buffer = builder.GetBufferPointer(); + const Model* output_model = GetModel(buffer); + ASSERT_TRUE(output_model); + + ASSERT_EQ(output_model->subgraphs()->size(), model_->subgraphs()->size()); + for (size_t subgraph_idx = 0; subgraph_idx < model_->subgraphs()->size(); + ++subgraph_idx) { + const auto quantized_graph = output_model->subgraphs()->Get(subgraph_idx); + const auto float_graph = model_->subgraphs()->Get(subgraph_idx); + // The output graph should have an extra tensor from the added dequantize + // op. + ASSERT_EQ(quantized_graph->tensors()->size(), + float_graph->tensors()->size() + 1); + // Check that a dequantize op exists. + int32_t dequant_input_idx = -1; + int32_t dequant_output_idx = -1; + for (size_t i = 0; i < quantized_graph->operators()->size(); ++i) { + const auto op = quantized_graph->operators()->Get(i); + const uint32_t op_code_idx = op->opcode_index(); + if (GetBuiltinCode(output_model->operator_codes()->Get(op_code_idx)) == + BuiltinOperator_DEQUANTIZE) { + dequant_input_idx = op->inputs()->Get(0); + dequant_output_idx = op->outputs()->Get(0); + } + } + ASSERT_GT(dequant_input_idx, -1); + ASSERT_GT(dequant_output_idx, -1); + for (size_t i = 0; i < quantized_graph->tensors()->size(); ++i) { + const auto quant_tensor = quantized_graph->tensors()->Get(i); + // If the tensor is a weight, it should have type INT8. + // If the tensor is a bias, it should have type FLOAT32. + // If the tensor is an input or output it should have type FLOAT32. + // The input to dequantize should be INT8, and all other tensors should be + // FLOAT32. + if (i == dequant_input_idx) { + EXPECT_EQ(quant_tensor->type(), TensorType_INT8); + } else if (i == dequant_output_idx) { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } else if (IsModelInputOrOutput(output_model, i)) { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } else if (quant_tensor->name()->str() == "conv_bias") { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } else if (quant_tensor->buffer() != 0) { + // If it's a non-bias constant tensor, it must be the weight. + EXPECT_EQ(quant_tensor->type(), TensorType_INT8); + } else { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } + } + } +} + +TEST_F(QuantizeWeightsTest, DequantizeConvFloat16) { + LoadBasicModel(); + flatbuffers::FlatBufferBuilder builder; + ASSERT_TRUE(QuantizeWeights(&builder, model_, BufferType::QUANTIZED_FLOAT16, + kUseUpdatedHybridSchemeDefault, + QuantizerType::OLD_QUANTIZER) + .ok()); + + const uint8_t* buffer = builder.GetBufferPointer(); + const Model* output_model = GetModel(buffer); + ASSERT_TRUE(output_model); + + ASSERT_EQ(output_model->subgraphs()->size(), model_->subgraphs()->size()); + for (size_t subgraph_idx = 0; subgraph_idx < model_->subgraphs()->size(); + ++subgraph_idx) { + const auto quantized_graph = output_model->subgraphs()->Get(subgraph_idx); + const auto float_graph = model_->subgraphs()->Get(subgraph_idx); + // The output graph should have two extra tensors from the added dequantize + // op. + ASSERT_EQ(quantized_graph->tensors()->size(), + float_graph->tensors()->size() + 2); + // Check that a dequantize op exists. + int32_t dequant_input_idx = -1; + int32_t dequant_output_idx = -1; + for (size_t i = 0; i < quantized_graph->operators()->size(); ++i) { + const auto op = quantized_graph->operators()->Get(i); + const uint32_t op_code_idx = op->opcode_index(); + if (GetBuiltinCode(output_model->operator_codes()->Get(op_code_idx)) == + BuiltinOperator_DEQUANTIZE) { + dequant_input_idx = op->inputs()->Get(0); + dequant_output_idx = op->outputs()->Get(0); + } + } + ASSERT_GT(dequant_input_idx, -1); + ASSERT_GT(dequant_output_idx, -1); + for (size_t i = 0; i < quantized_graph->tensors()->size(); ++i) { + const auto quant_tensor = quantized_graph->tensors()->Get(i); + // If the tensor is a weight, it should have type FLOAT16. + // If the tensor is a bias, it should have type FLOAT16. + // If the tensor is an input or output it should have type FLOAT32. + // The input to dequantize should be FLOAT16, and all other tensors should + // be FLOAT32. + if (i == dequant_input_idx) { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT16); + } else if (i == dequant_output_idx) { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } else if (IsModelInputOrOutput(output_model, i)) { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } else if (quant_tensor->name()->str() == "conv_bias") { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT16); + } else if (quant_tensor->buffer() != 0) { + // If it's a non-bias constant tensor, it must be the weight. + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT16); + } else { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } + } + } +} + +TEST_F(QuantizeWeightsTest, SharedWeights_Hybrid) { + LoadSharedWeightsModel(); + flatbuffers::FlatBufferBuilder builder; + ASSERT_TRUE( + QuantizeWeights(&builder, model_, 0, QuantizerType::OLD_QUANTIZER).ok()); + + const uint8_t* buffer = builder.GetBufferPointer(); + const Model* output_model = GetModel(buffer); + ASSERT_TRUE(output_model); + + ASSERT_EQ(output_model->subgraphs()->size(), model_->subgraphs()->size()); + uint32_t num_conv_ops = 0; + for (size_t subgraph_idx = 0; subgraph_idx < model_->subgraphs()->size(); + ++subgraph_idx) { + const auto quantized_graph = output_model->subgraphs()->Get(subgraph_idx); + for (size_t i = 0; i < quantized_graph->operators()->size(); ++i) { + const auto op = quantized_graph->operators()->Get(i); + const uint32_t op_code_idx = op->opcode_index(); + const auto op_code = + GetBuiltinCode(output_model->operator_codes()->Get(op_code_idx)); + if (op_code == BuiltinOperator_CONV_2D) { + num_conv_ops++; + // Ensure that each convolution's weights tensor is now INT8. + const auto weights_tensor = + quantized_graph->tensors()->Get(op->inputs()->Get(1)); + EXPECT_EQ(weights_tensor->type(), TensorType_INT8); + } + } + } + // Ensure that there were exactly two convolutions in the model. + EXPECT_EQ(num_conv_ops, 2); +} + +TEST_F(QuantizeWeightsTest, SharedWeights_Dequantize) { + LoadSharedWeightsModel(); + flatbuffers::FlatBufferBuilder builder; + ASSERT_TRUE(internal::QuantizeWeights(&builder, model_, 0, + /*use_hybrid_evaluation*/ false, + QuantizerType::OLD_QUANTIZER) + .ok()); + + const uint8_t* buffer = builder.GetBufferPointer(); + const Model* output_model = GetModel(buffer); + ASSERT_TRUE(output_model); + + ASSERT_EQ(output_model->subgraphs()->size(), model_->subgraphs()->size()); + uint32_t num_conv_ops = 0; + for (size_t subgraph_idx = 0; subgraph_idx < model_->subgraphs()->size(); + ++subgraph_idx) { + const auto quantized_graph = output_model->subgraphs()->Get(subgraph_idx); + for (size_t i = 0; i < quantized_graph->operators()->size(); ++i) { + const auto op = quantized_graph->operators()->Get(i); + const uint32_t op_code_idx = op->opcode_index(); + const auto op_code = + GetBuiltinCode(output_model->operator_codes()->Get(op_code_idx)); + if (op_code == BuiltinOperator_CONV_2D) { + num_conv_ops++; + // Ensure that each convolution's weights tensor is still FLOAT + // (the output of the dequantize). + uint32_t weights_tensor_index = op->inputs()->Get(1); + const auto weights_tensor = + quantized_graph->tensors()->Get(weights_tensor_index); + EXPECT_EQ(weights_tensor->type(), TensorType_FLOAT32); + + // Check that it comes from a dequantize operation. + BuiltinOperator producer_op_code; + ASSERT_TRUE(GetProducerOpCode(output_model, subgraph_idx, + weights_tensor_index, &producer_op_code)); + EXPECT_EQ(producer_op_code, BuiltinOperator_DEQUANTIZE); + } + } + } + // Ensure that there were exactly two convolutions in the model. + EXPECT_EQ(num_conv_ops, 2); +} + +TEST_F(QuantizeWeightsTest, VerifyGatherQuantization) { + LoadGatherTestModel(); + flatbuffers::FlatBufferBuilder builder; + ASSERT_TRUE( + QuantizeWeights(&builder, model_, 0, QuantizerType::OLD_QUANTIZER).ok()); + + const uint8_t* buffer = builder.GetBufferPointer(); + const Model* output_model = GetModel(buffer); + ASSERT_TRUE(output_model); + + ASSERT_EQ(output_model->subgraphs()->size(), model_->subgraphs()->size()); + for (size_t subgraph_idx = 0; subgraph_idx < model_->subgraphs()->size(); + ++subgraph_idx) { + const auto quantized_graph = output_model->subgraphs()->Get(subgraph_idx); + for (size_t i = 0; i < quantized_graph->operators()->size(); ++i) { + const auto op = quantized_graph->operators()->Get(i); + const uint32_t op_code_idx = op->opcode_index(); + const auto op_code = + GetBuiltinCode(output_model->operator_codes()->Get(op_code_idx)); + if (op_code == tflite::BuiltinOperator_GATHER) { + uint32_t input_tensor_index = op->inputs()->Get(0); + const auto weights_tensor = + quantized_graph->tensors()->Get(input_tensor_index); + EXPECT_EQ(weights_tensor->type(), TensorType_INT8); + } + } + } +} + +TEST_F(QuantizeWeightsTest, VerifyCustomOpQuantizationDequantize) { + LoadCustomOpTestModel(); + + // The custom op is not hybrid, and the second input is a constant that can + // be quantized. + CustomOpMap custom_op_map; + custom_op_map["CustomTestOp"] = { + .quantizable_input_indices = {1}, + .is_hybrid = false, + }; + + flatbuffers::FlatBufferBuilder builder; + ASSERT_TRUE(QuantizeWeights(&builder, model_, 0, custom_op_map, + QuantizerType::OLD_QUANTIZER) + .ok()); + + const uint8_t* buffer = builder.GetBufferPointer(); + const Model* output_model = GetModel(buffer); + ASSERT_TRUE(output_model); + + ASSERT_EQ(output_model->subgraphs()->size(), model_->subgraphs()->size()); + const auto quantized_graph = output_model->subgraphs()->Get(0); + // A dequantize op should be added. + ASSERT_EQ(quantized_graph->operators()->size(), + model_->subgraphs()->Get(0)->operators()->size() + 1); + int num_custom_ops_found = 0; + for (size_t i = 0; i < quantized_graph->operators()->size(); ++i) { + const auto op = quantized_graph->operators()->Get(i); + const uint32_t op_code_idx = op->opcode_index(); + const auto op_code = + GetBuiltinCode(output_model->operator_codes()->Get(op_code_idx)); + if (op_code == BuiltinOperator_CUSTOM) { + uint32_t weights_tensor_index = op->inputs()->Get(1); + const auto weights_tensor = + quantized_graph->tensors()->Get(weights_tensor_index); + EXPECT_EQ(weights_tensor->type(), TensorType_FLOAT32); + + // Check that it comes from a dequantize operation. + BuiltinOperator producer_op_code; + ASSERT_TRUE(GetProducerOpCode(output_model, 0, weights_tensor_index, + &producer_op_code)); + EXPECT_EQ(producer_op_code, BuiltinOperator_DEQUANTIZE); + num_custom_ops_found++; + } + } + EXPECT_EQ(num_custom_ops_found, 1); +} + +TEST_F(QuantizeWeightsTest, VerifyCustomOpQuantizationHybrid) { + LoadCustomOpTestModel(); + + // The custom op is hybrid, and the second input is a constant that can + // be quantized. + CustomOpMap custom_op_map; + custom_op_map["CustomTestOp"] = { + .quantizable_input_indices = {1}, + .is_hybrid = true, + }; + + flatbuffers::FlatBufferBuilder builder; + ASSERT_TRUE(QuantizeWeights(&builder, model_, 0, custom_op_map, + QuantizerType::OLD_QUANTIZER) + .ok()); + + const uint8_t* buffer = builder.GetBufferPointer(); + const Model* output_model = GetModel(buffer); + ASSERT_TRUE(output_model); + + ASSERT_EQ(output_model->subgraphs()->size(), model_->subgraphs()->size()); + const auto quantized_graph = output_model->subgraphs()->Get(0); + ASSERT_EQ(quantized_graph->operators()->size(), + model_->subgraphs()->Get(0)->operators()->size()); + int num_custom_ops_found = 0; + for (size_t i = 0; i < quantized_graph->operators()->size(); ++i) { + const auto op = quantized_graph->operators()->Get(i); + const uint32_t op_code_idx = op->opcode_index(); + const auto op_code = + GetBuiltinCode(output_model->operator_codes()->Get(op_code_idx)); + if (op_code == BuiltinOperator_CUSTOM) { + uint32_t weights_tensor_index = op->inputs()->Get(1); + const auto weights_tensor = + quantized_graph->tensors()->Get(weights_tensor_index); + EXPECT_EQ(weights_tensor->type(), TensorType_INT8); + num_custom_ops_found++; + } + } + EXPECT_EQ(num_custom_ops_found, 1); +} + +TEST_F(QuantizeWeightsTest, VerifyUpdatedHybridSchemeFalseQuantizationHybrid) { + LoadBasicModel(); + flatbuffers::FlatBufferBuilder builder; + const CustomOpMap custom_op_map; + ASSERT_TRUE(QuantizeWeights(&builder, model_, 0, custom_op_map, + /*use_updated_hybrid_scheme=*/false, + /*op_denylist=*/{}, QuantizerType::OLD_QUANTIZER) + .ok()); + + const uint8_t* buffer = builder.GetBufferPointer(); + const Model* output_model = GetModel(buffer); + ASSERT_TRUE(output_model); + + // Nothing should change. + ASSERT_EQ(output_model->subgraphs()->size(), model_->subgraphs()->size()); + for (size_t subgraph_idx = 0; subgraph_idx < model_->subgraphs()->size(); + subgraph_idx++) { + const auto quantized_graph = output_model->subgraphs()->Get(subgraph_idx); + const auto float_graph = model_->subgraphs()->Get(subgraph_idx); + ASSERT_EQ(quantized_graph->tensors()->size(), + float_graph->tensors()->size()); + // Make sure the graph only has one Conv operation. + ASSERT_EQ(quantized_graph->operators()->size(), 1); + const auto op = quantized_graph->operators()->Get(0); + const uint32_t op_code_idx = op->opcode_index(); + ASSERT_EQ(GetBuiltinCode(output_model->operator_codes()->Get(op_code_idx)), + BuiltinOperator_CONV_2D); + for (size_t i = 0; i < quantized_graph->tensors()->size(); i++) { + const auto quant_tensor = quantized_graph->tensors()->Get(i); + const auto float_tensor = float_graph->tensors()->Get(i); + EXPECT_EQ(quant_tensor->buffer(), float_tensor->buffer()); + EXPECT_EQ(quant_tensor->is_variable(), float_tensor->is_variable()); + EXPECT_EQ(GetAsVector(quant_tensor->shape()), + GetAsVector(float_tensor->shape())); + EXPECT_EQ(quant_tensor->name()->str(), float_tensor->name()->str()); + // If the tensor is a weight, it should have type INT8, otherwise it + // should stay with type FLOAT32. + // If the tensor is a bias, it should have type FLOAT32. + if (quant_tensor->name()->str() == "conv_bias") { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } else if (IsModelInputOrOutput(output_model, i)) { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } else if (quant_tensor->buffer() != 0) { + EXPECT_EQ(quant_tensor->type(), TensorType_INT8) + << quant_tensor->name()->str(); + auto shape = GetAsVector(quant_tensor->shape()); + EXPECT_EQ(quant_tensor->quantization()->scale()->size(), 1); + } else { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } + } + } +} + +TEST_F(QuantizeWeightsTest, DequantizeConvBlocklisted) { + LoadBasicModel(); + flatbuffers::FlatBufferBuilder builder; + const CustomOpMap custom_op_map; + ASSERT_TRUE(QuantizeWeights(&builder, model_, 0, custom_op_map, + /*use_updated_hybrid_scheme=*/true, + /*op_denylist*/ {BuiltinOperator_CONV_2D}, + QuantizerType::OLD_QUANTIZER) + .ok()); + + const uint8_t* buffer = builder.GetBufferPointer(); + const Model* output_model = GetModel(buffer); + ASSERT_TRUE(output_model); + + ASSERT_EQ(output_model->subgraphs()->size(), model_->subgraphs()->size()); + for (size_t subgraph_idx = 0; subgraph_idx < model_->subgraphs()->size(); + ++subgraph_idx) { + const auto quantized_graph = output_model->subgraphs()->Get(subgraph_idx); + const auto float_graph = model_->subgraphs()->Get(subgraph_idx); + // The output graph should have an extra tensor from the added dequantize + // op. + ASSERT_EQ(quantized_graph->tensors()->size(), + float_graph->tensors()->size() + 1); + // Check that a dequantize op exists. + int32_t dequant_input_idx = -1; + int32_t dequant_output_idx = -1; + for (size_t i = 0; i < quantized_graph->operators()->size(); ++i) { + const auto op = quantized_graph->operators()->Get(i); + const uint32_t op_code_idx = op->opcode_index(); + if (GetBuiltinCode(output_model->operator_codes()->Get(op_code_idx)) == + BuiltinOperator_DEQUANTIZE) { + dequant_input_idx = op->inputs()->Get(0); + dequant_output_idx = op->outputs()->Get(0); + } + } + ASSERT_GT(dequant_input_idx, -1); + ASSERT_GT(dequant_output_idx, -1); + for (size_t i = 0; i < quantized_graph->tensors()->size(); ++i) { + const auto quant_tensor = quantized_graph->tensors()->Get(i); + // If the tensor is a weight, it should have type INT8. + // If the tensor is a bias, it should have type FLOAT32. + // If the tensor is an input or output it should have type FLOAT32. + // The input to dequantize should be INT8, and all other tensors should be + // FLOAT32. + if (i == dequant_input_idx) { + EXPECT_EQ(quant_tensor->type(), TensorType_INT8); + // The dequantize should still be quantized per-channel + EXPECT_EQ(quant_tensor->quantization()->scale()->size(), 5); + EXPECT_EQ(quant_tensor->quantization()->quantized_dimension(), 0); + } else if (i == dequant_output_idx) { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } else if (IsModelInputOrOutput(output_model, i)) { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } else if (quant_tensor->name()->str() == "conv_bias") { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } else if (quant_tensor->buffer() != 0) { + // If it's a non-bias constant tensor, it must be the weight. + EXPECT_EQ(quant_tensor->type(), TensorType_INT8); + } else { + EXPECT_EQ(quant_tensor->type(), TensorType_FLOAT32); + } + } + } +} + +} // namespace +} // namespace toco_legacy +} // namespace lite +} // namespace mlir + +int main(int argc, char** argv) { + std::string model_file; + const std::vector flag_list = { + tsl::Flag("test_model_file", &model_file, + "Path to test tflite model file."), + }; + + const bool parse_result = tsl::Flags::Parse(&argc, argv, flag_list); + if (!parse_result) { + std::cerr << "Required test_model_file\n"; + std::abort(); + } + g_test_model_dir = new std::string(tsl::io::Dirname(model_file)); + ::tsl::port::InitMain(argv[0], &argc, &argv); + return RUN_ALL_TESTS(); +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/numerical_utils.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/numerical_utils.cc new file mode 100644 index 0000000000000000000000000000000000000000..54d37d2d866c00e87abd5b348425877715c102f9 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/numerical_utils.cc @@ -0,0 +1,97 @@ +/* Copyright 2020 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/compiler/mlir/lite/quantization/numerical_utils.h" + +#include + +#include +#include +#include +#include +#include + +namespace mlir { +namespace quant { + +// Converts a double-precision floating-point multiplier to a quantized +// multiplier. +// +// Args: +// double_multiplier: The double-precision floating-point multiplier. +// +// Returns: +// A quantized multiplier, represented as a pair of integers: the quantized +// multiplier and the shift amount. The shift amount is the number of bits +// that the quantized multiplier should be shifted to the right before being +// used. +QuantizedMultiplier QuantizeMultiplier(double double_multiplier) { + if (double_multiplier < 1e-6) { + return {0, 0}; + } + + int32_t shift; + const double q = frexp(double_multiplier, &shift); + int64_t quantized_multiplier = round(q * (1LL << 31)); + assert(quantized_multiplier <= (1LL << 31)); + if (quantized_multiplier == (1LL << 31)) { + quantized_multiplier /= 2; + ++shift; + } + assert(quantized_multiplier <= std::numeric_limits::max()); + + // Check that the shift amount is not greater than 31 or less than -31. + if (shift > 31 || shift < -31) { + return {0, 0}; + } + + return {static_cast(quantized_multiplier), shift}; +} + +// Calculates the quantized range for a given scale, zero point, minimum and +// maximum values, and quantization range. +// +// Args: +// scale: The scale factor for the quantized values. +// zero_point: The zero point for the quantized values. +// rmin: The minimum value of the quantized values. +// rmax: The maximum value of the quantized values. +// qmin: The minimum value of the quantization range. +// qmax: The maximum value of the quantization range. +// +// Returns: +// A quantized range, represented as a pair of integers: the minimum and +// maximum quantized values. +QuantizedRange CalculateQuantizedRange(double scale, int32_t zero_point, + std::optional rmin, + std::optional rmax, int32_t qmin, + int32_t qmax) { + auto quantize = [scale, zero_point](float f) { + return zero_point + static_cast(std::round(f / scale)); + }; + + if (rmin.has_value() && rmax.has_value()) { + return {std::max(qmin, quantize(rmin.value())), + std::min(qmax, quantize(rmax.value()))}; + } else if (rmin.has_value()) { + return {std::max(qmin, quantize(rmin.value())), qmax}; + } else if (rmax.has_value()) { + return {qmin, std::min(qmax, quantize(rmax.value()))}; + } else { + return {qmin, qmax}; + } +} + +} // namespace quant +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/numerical_utils.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/numerical_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..64903d2b9ad3a3f20559474b454c151041d54302 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/numerical_utils.h @@ -0,0 +1,46 @@ +/* Copyright 2020 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_NUMERICAL_UTILS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_NUMERICAL_UTILS_H_ + +#include +#include +#include + +#include "absl/types/optional.h" + +namespace mlir { +namespace quant { + +using QuantizedMultiplier = std::pair; +using QuantizedRange = std::pair; + +// Decompose double precision multiplier to integer multiplier and exponent. +// double_multiplier = int_multiplier * 2 ^ (-31 + exponent) +// int_multiplier will be range of (2^31, 2^30]. +QuantizedMultiplier QuantizeMultiplier(double double_multiplier); + +// Calculate the effective quantized value range for the scale, zero point. The +// range is the minimum range defined by [rmin, rmax] and [qmin, qmax]. +QuantizedRange CalculateQuantizedRange(double scale, int32_t zero_point, + std::optional rmin, + std::optional rmax, int32_t qmin, + int32_t qmax); + +} // namespace quant +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_NUMERICAL_UTILS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/numerical_utils_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/numerical_utils_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..64407938338386bf0e027c63a773ed196bc62aef --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/numerical_utils_test.cc @@ -0,0 +1,113 @@ +/* Copyright 2020 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/compiler/mlir/lite/quantization/numerical_utils.h" + +#include +#include + +#include + +namespace mlir { +namespace quant { + +namespace { + +double ComposeScale(const QuantizedMultiplier& input) { + return input.first * exp2(-31 + input.second); +} + +TEST(NumericalUtils, QuantizeMultiplier) { + // Decompose multiplier larger than 1. + ASSERT_FLOAT_EQ(ComposeScale(QuantizeMultiplier(1.0e6)), 1.0e6); + ASSERT_FLOAT_EQ(ComposeScale(QuantizeMultiplier(1.0e3)), 1.0e3); + ASSERT_FLOAT_EQ(ComposeScale(QuantizeMultiplier(10.)), 10.); + ASSERT_FLOAT_EQ(ComposeScale(QuantizeMultiplier(5.)), 5.); + ASSERT_FLOAT_EQ(ComposeScale(QuantizeMultiplier(2.)), 2.); + + // Decompose multiplier between 1.0 and 1e-6. + ASSERT_FLOAT_EQ(ComposeScale(QuantizeMultiplier(0.0)), 0.0); + ASSERT_FLOAT_EQ(ComposeScale(QuantizeMultiplier(1.0)), 1.0); + ASSERT_FLOAT_EQ(ComposeScale(QuantizeMultiplier(1.0e-1)), 1.0e-1); + ASSERT_FLOAT_EQ(ComposeScale(QuantizeMultiplier(1.0e-2)), 1.0e-2); + ASSERT_FLOAT_EQ(ComposeScale(QuantizeMultiplier(1.0e-3)), 1.0e-3); + ASSERT_FLOAT_EQ(ComposeScale(QuantizeMultiplier(1.0e-4)), 1.0e-4); + ASSERT_FLOAT_EQ(ComposeScale(QuantizeMultiplier(1.0e-5)), 1.0e-5); + ASSERT_FLOAT_EQ(ComposeScale(QuantizeMultiplier(1.0e-6)), 1.0e-6); + + // When scale is smaller than 1.0e-6, it is decomposed to {0, 0}. + ASSERT_FLOAT_EQ(ComposeScale(QuantizeMultiplier(1.0e-7)), 0.0); + ASSERT_FLOAT_EQ(ComposeScale(QuantizeMultiplier(1.0e-8)), 0.0); +} + +TEST(NumericalUtils, ActivationRange) { + // zero point = 0 + auto a = + CalculateQuantizedRange(1e-6, 0, std::nullopt, std::nullopt, -128, 127); + ASSERT_EQ(a.first, -128); + ASSERT_EQ(a.second, 127); + + auto b = CalculateQuantizedRange(1e-6, 0, 0.0, std::nullopt, -128, 127); + ASSERT_EQ(b.first, 0); + ASSERT_EQ(b.second, 127); + + auto c = CalculateQuantizedRange(1e-6, 0, -1.0, 1.0, -128, 127); + ASSERT_EQ(c.first, -128); + ASSERT_EQ(c.second, 127); + + auto d = CalculateQuantizedRange(1e-6, 0, 0.0, 6.0, -128, 127); + ASSERT_EQ(d.first, 0); + ASSERT_EQ(d.second, 127); + + // zero point = 100 + auto e = + CalculateQuantizedRange(1e-6, 100, std::nullopt, std::nullopt, -128, 127); + ASSERT_EQ(e.first, -128); + ASSERT_EQ(e.second, 127); + + auto f = CalculateQuantizedRange(1e-6, 100, 0.0, std::nullopt, -128, 127); + ASSERT_EQ(f.first, 100); + ASSERT_EQ(f.second, 127); + + auto g = CalculateQuantizedRange(1e-6, 100, -1.0, 1.0, -128, 127); + ASSERT_EQ(g.first, -128); + ASSERT_EQ(g.second, 127); + + auto h = CalculateQuantizedRange(1e-6, 100, 0.0, 6.0, -128, 127); + ASSERT_EQ(h.first, 100); + ASSERT_EQ(h.second, 127); + + // zero point = -100 + auto i = CalculateQuantizedRange(1e-6, -100, std::nullopt, std::nullopt, -128, + 127); + ASSERT_EQ(i.first, -128); + ASSERT_EQ(i.second, 127); + + auto j = CalculateQuantizedRange(1e-6, -100, 0.0, std::nullopt, -128, 127); + ASSERT_EQ(j.first, -100); + ASSERT_EQ(j.second, 127); + + auto k = CalculateQuantizedRange(1e-6, -100, -1.0, 1.0, -128, 127); + ASSERT_EQ(k.first, -128); + ASSERT_EQ(k.second, 127); + + auto l = CalculateQuantizedRange(1e-6, -100, 0.0, 6.0, -128, 127); + ASSERT_EQ(l.first, -100); + ASSERT_EQ(l.second, 127); +} + +} // namespace +} // namespace quant +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/quantization_context.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/quantization_context.cc new file mode 100644 index 0000000000000000000000000000000000000000..bf53b76f22e66986e962eec59760423e29e88c4b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/quantization_context.cc @@ -0,0 +1,367 @@ +/* Copyright 2020 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/compiler/mlir/lite/quantization/quantization_context.h" + +#include + +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/Debug.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/raw_ostream.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Dialect/Quant/IR/QuantTypes.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_utils.h" +#include "tensorflow/compiler/mlir/lite/quantization/device_target.h" +#include "tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.h" + +#define DEBUG_TYPE "quantization-context" + +namespace mlir { +namespace quant { + +QuantizeContext::QuantizeContext(func::FuncOp func, const DeviceTarget &spec) + : func_(func), target_spec_(spec) { + llvm::DenseMap value_to_state; + func.walk([&](quantfork::QuantizeRegionOp op) { + for (int i = 0, e = op.getNumOperands(); i != e; ++i) { + states_manager_.InitializeOperandState(op, i, &value_to_state); + } + + for (int res = 0, e = op.getNumResults(); res != e; ++res) { + states_manager_.InitializeResultState(op, res, &value_to_state); + } + }); +} + +std::vector QuantizeContext::GetAllOps() { + std::vector all_ops; + all_ops.reserve(128); + func_.walk([&](quantfork::QuantizeRegionOp op) { all_ops.push_back(op); }); + return all_ops; +} + +KernelSpecs::Signature QuantizeContext::GetSignature( + quantfork::QuantizeRegionOp op) { + KernelSpecs::Signature signature; + signature.reserve(op.getInputSpecs().size() + op.getOutputSpecs().size()); + for (int i = 0; i < op.getNumOperands(); ++i) { + DeviceTarget::AppendToSignature(GetOperandParams(op, i), &signature); + } + for (int i = 0; i < op.getNumResults(); ++i) { + DeviceTarget::AppendToSignature(GetResultParams(op, i), &signature); + } + return signature; +} + +LogicalResult QuantizeContext::Handle( + quantfork::QuantizeRegionOp op, + llvm::SmallVectorImpl *new_items, bool *changed) { + auto signature = GetSignature(op); + auto spec = target_spec_.GetKernelSpec(op.getLogicalKernel(), signature); + if (!spec.has_value()) { + op.emitWarning( + "Couldn't find kernel from the registration for quantization."); + return success(); + } + switch (spec->type) { + case ScaleConstraintType::OutputInputFreeScale: { + // no propagation. + *changed |= false; + break; + } + case ScaleConstraintType::CustomScale: { + if (failed(spec->scale_fn(this, op, new_items, changed))) { + return failure(); + } + break; + } + case ScaleConstraintType::OutputInputSameScale: { + auto params = GetQuantParamsForSameScaleConstraint(op); + if (EmptyParams(params)) { + *changed |= false; + break; + } + // propagate this params to all the quantizable ports. + if (failed(PropagateQuantParams(op, params, new_items, changed))) { + return failure(); + } + break; + } + default: { + // TODO(fengliuai): implement the other types. + llvm_unreachable("no implementation."); + return failure(); + } + } + return success(); +} + +LogicalResult QuantizeContext::Finalize() { + MLIRContext *context = func_.getContext(); + func_.walk([&](quantfork::QuantizeRegionOp op) { + llvm::SmallVector input_specs; + auto original_input_specs = op.getInputSpecs().getValue(); + for (int i = 0, e = op.getNumOperands(); i != e; ++i) { + auto &state = states_manager_.GetOperandQuantState(op, i); + auto &requantize = states_manager_.GetOperandRequantizeState(op, i); + if (state.IsEmpty() && requantize.pos == RequantizeState::NO_REQUANTIZE) { + input_specs.push_back(original_input_specs[i]); + } else if (requantize.pos == RequantizeState::ON_OUTPUT) { + input_specs.push_back(TypeAttr::get(requantize.params)); + } else { + input_specs.push_back(TypeAttr::get(state.params)); + } + } + op->setAttr("input_specs", ArrayAttr::get(context, input_specs)); + + llvm::SmallVector output_specs; + auto original_output_specs = op.getOutputSpecs().getValue(); + for (int res = 0, e = op.getNumResults(); res != e; ++res) { + auto &state = states_manager_.GetResultQuantState(op, res); + auto &requantize = states_manager_.GetResultRequantizeState(op, res); + if (state.IsEmpty() && requantize.pos == RequantizeState::NO_REQUANTIZE) { + output_specs.push_back(original_output_specs[res]); + } else if (requantize.pos == RequantizeState::ON_INPUT) { + output_specs.push_back(TypeAttr::get(requantize.params)); + } else { + output_specs.push_back(TypeAttr::get(state.params)); + } + } + op->setAttr("output_specs", ArrayAttr::get(context, output_specs)); + }); + return success(); +} + +void QuantizeContext::DumpStates(quantfork::QuantizeRegionOp current_op) { + if (current_op) { + llvm::errs() << "\n\n\n" << current_op.getLogicalKernel() << "\n"; + } + func_.walk([&](quantfork::QuantizeRegionOp op) { + if (current_op == op) llvm::errs() << "===>>>"; + llvm::errs() << op.getLogicalKernel() << " : ("; + for (auto i = 0; i < op.getNumOperands(); ++i) { + if (auto params = GetOperandParams(op, i)) + params.print(llvm::errs()); + else + llvm::errs() << "_"; + llvm::errs() << ","; + } + llvm::errs() << ") -> ("; + for (auto i = 0; i < op.getNumResults(); ++i) { + if (auto params = GetResultParams(op, i)) + params.print(llvm::errs()); + else + llvm::errs() << "_"; + llvm::errs() << ","; + } + llvm::errs() << ")\n"; + }); +} + +// A heuristic to get quantization parameters satisfies the same scale +// constraints: +// - If there are immutable states, +// - use the single input, or, +// - use the single output, or, +// - use the first one in the collection, +// - use the single input if it is ready, or, +// - use the single output if it is ready, or, +// - use the first ready one in the collection. +TFL::QuantParams QuantizeContext::GetQuantParamsForSameScaleConstraint( + Operation *op) { + // Two vector to collect Non-empty operands and results states. + std::vector mutable_states, immutable_states; + for (int i = 0, e = op->getNumOperands(); i != e; ++i) { + auto &state = states_manager_.GetOperandQuantState(op, i); + if (state.immutable) { + immutable_states.push_back(&state); + } else if (!state.IsEmpty()) { + mutable_states.push_back(&state); + } + } + + int immutable_operands_num = immutable_states.size(); + int mutable_operands_num = mutable_states.size(); + // Use the operand's state if it is immutable and it is the only one + // operand. + if (op->getNumOperands() == 1 && immutable_operands_num == 1) { + return immutable_states.front()->params; + } + + for (int i = 0, e = op->getNumResults(); i != e; ++i) { + auto &state = states_manager_.GetResultQuantState(op, i); + if (state.immutable) { + immutable_states.push_back(&state); + } else if (!state.IsEmpty()) { + mutable_states.push_back(&state); + } + } + + int immutable_results_num = immutable_states.size() - immutable_operands_num; + int mutable_results_num = mutable_states.size() - mutable_operands_num; + // Use the result's state if it is immutable and it is the only one result. + if (op->getNumResults() == 1 && immutable_results_num == 1) { + return immutable_states.back()->params; + } + + LLVM_DEBUG(llvm::dbgs() + << "Quantization parameters are not collected in an ideal place. " + "Has to fallback values which might introduce errors.\n"); + + // Use the first immutable state to quantize the rest operands and results. + if (!immutable_states.empty()) return immutable_states.front()->params; + + // If there are no immutable states, use the operand's state if it is the + // only one operand and has parameters propagated. + if (op->getNumOperands() == 1 && mutable_operands_num == 1) { + return mutable_states.front()->params; + } + + // If there are no immutable states, use the result's state if it is the + // only one result and has parameters propagated. + if (op->getNumResults() == 1 && mutable_results_num == 1) { + return mutable_states.back()->params; + } + + // Use the first propagated state to quantize the rest operands and results. + if (!mutable_states.empty()) return mutable_states.front()->params; + + // None operands/results have parameters propagated, skip this node for now. + return {}; +} + +LogicalResult QuantizeContext::PropagateQuantParams( + Operation *op, const TFL::QuantParams params, + quant::AdjacentOperations *new_items, bool *changed) { + // Use the final state to set all the operands' parameters. + for (int i = 0, e = op->getNumOperands(); i != e; ++i) { + auto ele = + llvm::cast(op->getOperand(i).getType()).getElementType(); + if (isa(ele) && SetOperandParams(op, i, params)) { + *changed |= true; + new_items->push_back(op->getOperand(i).getDefiningOp()); + } + } + + // Use the final state to set all the results' parameters. + for (int res = 0, e = op->getNumResults(); res != e; ++res) { + auto ele = + llvm::cast(op->getResult(res).getType()).getElementType(); + if (isa(ele) && SetResultParams(op, res, params)) { + auto users = op->getResult(res).getUsers(); + *changed |= !users.empty(); + new_items->append(users.begin(), users.end()); + } + } + return success(); +} + +int QuantizeContext::StatesManager::InitializeState( + quantfork::QuantizeRegionOp op, int index, bool as_result) { + Attribute params_attr; + if (as_result) { + params_attr = op.getOutputSpecs()[index]; + } else { + params_attr = op.getInputSpecs()[index]; + } + TFL::QuantParams params = + dyn_cast(cast(params_attr).getValue()); + bool immutable = !EmptyParams(params); + int next_state_index = states_.size(); + states_.push_back({params, immutable}); + if (as_result) { + result_states_.insert({{op, index}, next_state_index}); + } else { + operand_states_.insert({{op, index}, next_state_index}); + } + return next_state_index; +} + +void QuantizeContext::StatesManager::InitializeOperandState( + quantfork::QuantizeRegionOp op, int index, + llvm::DenseMap *cache) { + Value in = op.getOperand(index); + auto cached = cache->insert({in, 0}); + if (!cached.second) { + operand_states_.insert({{op, index}, cached.first->second}); + return; + } + cached.first->second = InitializeState(op, index, /*as_result=*/false); +} + +void QuantizeContext::StatesManager::InitializeResultState( + quantfork::QuantizeRegionOp op, int index, + llvm::DenseMap *cache) { + auto res = op.getResult(index); + auto cached = cache->insert({res, 0}); + if (!cached.second) { + result_states_.insert({{op, index}, cached.first->second}); + return; + } + cached.first->second = InitializeState(op, index, /*as_result=*/true); +} + +bool QuantizeContext::StatesManager::SetConstantResultParams(Operation *op) { + llvm_unreachable("no implementation."); + return false; +} + +bool QuantizeContext::StatesManager::SetResultParams(Operation *op, + int res_index, + TFL::QuantParams params) { + auto &state = GetResultQuantState(op, res_index); + if (state.params == params) { + return false; + } + if (!state.IsEmpty()) { + auto &rescale = GetResultRequantizeState(op, res_index); + rescale.params = params; + rescale.pos = RequantizeState::ON_INPUT; + return false; + } + state.params = params; + return true; +} + +bool QuantizeContext::StatesManager::SetOperandParams(Operation *op, int index, + TFL::QuantParams params) { + auto &state = GetOperandQuantState(op, index); + if (state.params == params) { + return false; + } + + if (!state.IsEmpty()) { + auto &rescale = GetOperandRequantizeState(op, index); + rescale.params = params; + rescale.pos = RequantizeState::ON_OUTPUT; + return false; + } + state.params = params; + return true; +} +} // namespace quant +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/quantization_context.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/quantization_context.h new file mode 100644 index 0000000000000000000000000000000000000000..dbd73fa354330785224cc5a1e45c675add2b9ff3 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/quantization_context.h @@ -0,0 +1,247 @@ +/* Copyright 2020 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_QUANTIZATION_CONTEXT_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_QUANTIZATION_CONTEXT_H_ + +#include +#include +#include + +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/SmallVector.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Dialect/Quant/IR/QuantTypes.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_utils.h" +#include "tensorflow/compiler/mlir/lite/quantization/device_target.h" +#include "tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.h" + +namespace mlir { +namespace quant { + +static bool EmptyParams(TFL::QuantParams p) { + return p == quant::QuantizedType(); +} + +// The state for each op result during the quantization parameters propagation. +struct QuantState { + // Quantization parameters propagated to an op result. + TFL::QuantParams params; + // A flag indicates this state (the params) shouldn't be changed after it is + // initialized. This flag will be set to true if the quantization parameters + // are from the quantization-aware training. + const bool immutable; + + bool IsEmpty() { return EmptyParams(params); } +}; + +// The state for rescaling the propagated quantization parameters. This can be +// on the input side to satisfy the constraint of previous operation, or on the +// output side to satisfy the constraint of the next operation. +struct RequantizeState { + // Sometimes, we have to "requantize" the quantization result to satisfy all + // the constraints. The "requantize" can happen either on the input or output + // of the quantization result. + enum RequantizePosition { + NO_REQUANTIZE, + ON_INPUT, + ON_OUTPUT + } pos = NO_REQUANTIZE; + + // Quantization parameters will be used to add the requantize ops. + TFL::QuantParams params; +}; + +// This class manages all the intermediate quantization states. +class QuantizeContext { + public: + QuantizeContext(func::FuncOp func, const DeviceTarget &spec); + + // Returns all the quant region ops. + std::vector GetAllOps(); + + // For each quant region op, propagates its quantization parameters according + // to the kernel specification and also returns the adjacent quant region ops + // which get the new quantization parameters propagated. + LogicalResult Handle(quantfork::QuantizeRegionOp op, + llvm::SmallVectorImpl *new_items, + bool *changed); + + // Updates the port quantization specifications of all the quant region ops + // with the propagation results. + LogicalResult Finalize(); + + // Dumps the states stores in the state manager. + void DumpStates(quantfork::QuantizeRegionOp current_op = {}); + + // Update the quantization parameter for certain result of the op. By this + // method, the quantization parameter is propagated to all the users of the + // result as well. + bool SetResultParams(Operation *op, int index, TFL::QuantParams params) { + return states_manager_.SetResultParams(op, index, params); + } + + // Update the quantization parameter for certain operand of the op. By this + // method, the quantization parameter is propagated to the defining op of + // operand as well. + bool SetOperandParams(Operation *op, int index, TFL::QuantParams params) { + return states_manager_.SetOperandParams(op, index, params); + } + + // Return the quantization parameter of certain result of the op. + TFL::QuantParams GetResultParams(Operation *op, int index) { + return states_manager_.GetResultParams(op, index); + } + + // Return the quantization parameter of certain operand of the op. + TFL::QuantParams GetOperandParams(Operation *op, int index) { + return states_manager_.GetOperandParams(op, index); + } + + // Return the signature of the op. + KernelSpecs::Signature GetSignature(quantfork::QuantizeRegionOp op); + + // A heuristic to get quantization parameters satisfies the same scale + // constraints: + // - If there are immutable states, + // - use the single input, or, + // - use the single output, or, + // - use the first one in the collection, + // - use the single input if it is ready, or, + // - use the single output if it is ready, or, + // - use the first ready one in the collection. + TFL::QuantParams GetQuantParamsForSameScaleConstraint(Operation *op); + + // Propagate `params` to all the quantizable port of the `op`. The adjacent + // ops, which have the parameters propagated to, are collected by `new_items`, + // so they can be added to the working queue. `changed` is set to true if + // there are any new elements being added to `new_items`. + LogicalResult PropagateQuantParams(Operation *op, TFL::QuantParams params, + AdjacentOperations *new_items, + bool *changed); + + private: + class StatesManager { + public: + // Sets the quantization parameters of the constant result according to its + // content. + // + // Always returns true. + bool SetConstantResultParams(Operation *op); + + // Sets the quantization parameters of the result to a fixed value. If any + // quantization parameters have been propagated, a `requantize` will happen + // on the input of propagated quantization. + // + // Returns true, if the users of the result needs to be added to the + // worklist. + bool SetResultParams(Operation *op, int index, TFL::QuantParams params); + + // Sets the quantization parameters of the operand to a fixed value. If any + // quantization parameters have been propagated, a `requantize` will happen + // on the output of propagated quantization. + // + // Returns true, if the defining op of the operand needs to be added to the + // worklist. + bool SetOperandParams(Operation *op, int index, TFL::QuantParams params); + + // Returns the quantization parameters of the index-th result of the op. + TFL::QuantParams GetResultParams(Operation *op, int index) { + return states_[result_states_[{op, index}]].params; + } + + // Returns the quantization parameters of the index-th operand of the op. + TFL::QuantParams GetOperandParams(Operation *op, int index) { + return states_[operand_states_[{op, index}]].params; + } + + private: + friend class QuantizeContext; + + // Uses the type of `val` to set the initial state of the index-th result if + // `as_result` is true or index-th operand if `as_result` is false. The + // state is immutable if the type is a quantized type. Returns the index of + // this new state in the state vector. + int InitializeState(quantfork::QuantizeRegionOp op, int index, + bool as_result); + + // Sets the state of the index-th operand of the op. If this operand is + // cached, uses the cached result without creating new entry in the state + // vector. Otherwise, allocate a new entry in the state vector. + void InitializeOperandState(quantfork::QuantizeRegionOp op, int index, + llvm::DenseMap *cache); + + // Sets the state of the index-th result of the op. If this result is + // cached, uses the cached result without creating new entry in the state + // vector. Otherwise, allocate a new entry in the state vector. + void InitializeResultState(quantfork::QuantizeRegionOp op, int index, + llvm::DenseMap *cache); + + // Returns the state of the index-th operand of the op. + QuantState &GetOperandQuantState(Operation *op, int index) { + return states_[operand_states_[{op, index}]]; + } + + // Returns the state of the index-th result of the op. + QuantState &GetResultQuantState(Operation *op, int index) { + return states_[result_states_[{op, index}]]; + } + + // Returns the state of the index-th operand of the op. + RequantizeState &GetOperandRequantizeState(Operation *op, int index) { + return rescale_states_[operand_states_[{op, index}]]; + } + + // Returns the state of the index-th result of the op. + RequantizeState &GetResultRequantizeState(Operation *op, int index) { + return rescale_states_[result_states_[{op, index}]]; + } + + private: + // This is used to identify an operand or result of an op. The second + // element of this pair is the index of the operand or result. + using OpValue = std::pair; + + // The vector contains all the quantization parameters propagated from the + // defining operations of the value, or from the quantization aware + // training. + std::vector states_; + + // The map contains all the quantization parameters which are required to + // satisfy the same operands and results constraint. The keys of this map + // are the values from `operand_states_` and `result_state_`. + std::unordered_map rescale_states_; + + // Maps of indexes to the propagation state vector from the ops operands, + // results and arguments. + llvm::DenseMap operand_states_; + llvm::DenseMap result_states_; + }; + + func::FuncOp func_; + + DeviceTarget target_spec_; + + StatesManager states_manager_; +}; + +} // namespace quant +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_QUANTIZATION_CONTEXT_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/quantization_info.proto b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/quantization_info.proto new file mode 100644 index 0000000000000000000000000000000000000000..5c64aac35a2ededc600a840de5b7563c59b5d2bc --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/quantization_info.proto @@ -0,0 +1,74 @@ +syntax = "proto3"; + +package mlir.quant; + +// Represents the quantization parameters for a list of named tensors. +message QuantizationInfo { + // min/max of the per axis value range. To quantize the value, the metadata + // of the target properties should be specified or read from the ops + // quantization specification. + message MinMax { + float min = 1; + float max = 2; + } + + // Affine parameters to quantize the per axis value. The metadata of the + // target properties should be specified as well. + message AffineParams { + float scale = 1; + int32 zero_point = 2; + } + + // Params to quantize the axis. Only one of the field can be used. + message PerAxisParams { + oneof params_oneof { + // min/max of the ranges. + MinMax min_max = 1; + + // affine parameters to quantize the per axis value. + AffineParams affine_params = 2; + } + } + + // The metadata defines the target properties. + message Metadata { + // Bit number of fixed-point data the target kernel supports. + int32 num_bits = 1; + + // The quantized axis index if it is per-axis quantization. + int32 quantize_axis = 2; + + // The minimum allowed value of the fixed-point data range. + // This can also be used to derive the sign of storage type. + int32 range_min = 3; + + // The minimum allowed value of the fixed-point data range. + int32 range_max = 4; + } + + // The quantization parameters for a named tensor. + message QuantParams { + // The tensor name has the following convention: + // tensor_name := op_name | op_name ’:’ port_number. + // If the op has only one port, op_name can be used. + // If the op has internal states, such as fused LSTM, the port_number should + // follow a predefined convention. + oneof name_oneof { + string name = 1; + + // An regex can be used to match multiple tensors. + string name_regex = 2; + } + + // The quantization parameters for the tensor. If it is for per-axis, the + // parameters should be defined for each axis, otherwise, if it is for + // per-tensor, this repeated field should only contain a single element. + repeated PerAxisParams params = 3; + + // Metadata about the quantization parameters. + Metadata meta = 5; + } + + // List of quantization parameters for tensors. + repeated QuantParams entries = 1; +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/quantization_passes.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/quantization_passes.h new file mode 100644 index 0000000000000000000000000000000000000000..9994840d0c53d731e6f6f3041c0068403d80327c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/quantization_passes.h @@ -0,0 +1,46 @@ +/* Copyright 2019 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_QUANTIZATION_PASSES_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_QUANTIZATION_PASSES_H_ + +#include +#include + +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassManager.h" // from @llvm-project + +namespace mlir { +namespace quant { + +using OperationToName = std::function; + +// Creates an instance pass to import quantization stats to the operations in +// the function. A custom method to get the name from the op is used because +// different dialect ops might have different ways to assign the name. +std::unique_ptr> CreateImportQuantStatsPass( + OperationToName op_to_name, const std::string& stats_str); + +// Creates an instance pass to import quantization stats to the operations in +// the function. A custom method to get the name from the op is used because +// different dialect ops might have different ways to assign the name. +std::unique_ptr> +CreateImportQuantStatsPassForTFControlDialect(const std::string& stats_str); + +} // namespace quant +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_QUANTIZATION_PASSES_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tensorflow/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tensorflow/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..43aedde2f49f6cdaf04d704a29db33547d9d352d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tensorflow/BUILD @@ -0,0 +1,77 @@ +load("@llvm-project//mlir:tblgen.bzl", "gentbl_cc_library", "td_library") +load("//tensorflow:tensorflow.default.bzl", "get_compatible_with_portable") +load("//tensorflow/core/platform:rules_cc.bzl", "cc_library") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + default_visibility = [ + ":friends", + ], + licenses = ["notice"], +) + +package_group( + name = "friends", + packages = [ + "//learning/brain/mobile/model_optimization/...", + "//tensorflow/compiler/mlir/...", + "//tensorflow/compiler/mlir/lite/...", + ], +) + +td_library( + name = "ptq_td_files", + srcs = [ + "fallback_to_flex_patterns.td", + ], + compatible_with = get_compatible_with_portable(), + visibility = ["//visibility:private"], + deps = [ + "//tensorflow/compiler/mlir/tensorflow:tensorflow_ops_td_files", + "@llvm-project//mlir:ArithOpsTdFiles", + "@llvm-project//mlir:FuncTdFiles", + ], +) + +gentbl_cc_library( + name = "ptq_fallback_to_flex_inc_gen", + compatible_with = get_compatible_with_portable(), + tbl_outs = {"fallback_to_flex_patterns.inc": ["-gen-rewriters"]}, + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "fallback_to_flex_patterns.td", + deps = [":ptq_td_files"], +) + +cc_library( + name = "tf_quantization_passes", + srcs = [ + "fallback_to_flex_ops.cc", + "fallback_to_flex_patterns.inc", + "tf_to_quant.cc", + ], + hdrs = [ + "passes.h", + ], + deps = [ + ":ptq_fallback_to_flex_inc_gen", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib", + "//tensorflow/compiler/mlir/lite/quantization/ir:QuantOps", + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/compiler/mlir/tensorflow:translate_utils", + "//tensorflow/core:protos_all_cc", + "//tensorflow/core/platform:statusor", + "@com_google_absl//absl/base:core_headers", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings:string_view", + "@flatbuffers", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + ], + alwayslink = 1, +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tensorflow/fallback_to_flex_ops.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tensorflow/fallback_to_flex_ops.cc new file mode 100644 index 0000000000000000000000000000000000000000..50c908deb624a3b7821f41467a73e9b332a738dd --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tensorflow/fallback_to_flex_ops.cc @@ -0,0 +1,325 @@ +/* Copyright 2021 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 "absl/base/attributes.h" +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" +#include "flatbuffers/flexbuffers.h" // from @flatbuffers +#include "llvm/ADT/STLExtras.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/CommandLine.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/Diagnostics.h" // from @llvm-project +#include "mlir/IR/DialectRegistry.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_dialect.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" +#include "tensorflow/compiler/mlir/tensorflow/utils/translate_utils.h" +#include "tensorflow/core/framework/node_def.pb.h" + +namespace mlir { +namespace TF { +namespace internal { + +// The name prefix of Flex ops. +constexpr absl::string_view kFlexOpNamePrefix = "Flex"; +// Don't fallback to Flex op if this attribute is set. This attribute is +// transient and is only used inside this pass. First, the pass looks for +// predefined patterns and set this attribute to ops in the patterns. Then, +// when parsing the function, if find ops with this attribute, the pass +// remove the attribute and skip further processing on those ops. +constexpr char kNoFallbackAttr[] = "no_fallback"; +// TF Quantization modes. These constants are defined as char arrays so they +// can parsed by the pass option. +constexpr char kDefaultMode[] = "DEFAULT"; +constexpr char kLegacyIntegerMode[] = "LEGACY_INTEGER"; + +// Checks if the operation is TF FakeQuant ops. +bool IsTfFakeQuantOp(Operation *op) { + return llvm::isa< + // clang-format off + TF::FakeQuantWithMinMaxArgsOp, + TF::FakeQuantWithMinMaxVarsOp, + TF::FakeQuantWithMinMaxVarsPerChannelOp + // clang-format on + >(op); +} + +// Checks if the operation is allowlisted in both modes. These ops are not +// quantizable but is necessary to make the conversion successful. +bool IsAlwaysAllowlistedOp(Operation *op) { + return llvm::isa< + // clang-format off + // go/keep-sorted start + TF::ConstOp, + TF::IdentityOp, + TF::PartitionedCallOp, + TF::StatefulPartitionedCallOp + // go/keep-sorted end + // clang-format on + >(op); +} + +// LINT.IfChange +// The list of quantizable ops in the Legacy Integer mode. +ABSL_ATTRIBUTE_NOINLINE const std::set + &QuantizableOpsInLegacyMode() { + static const std::set *legacy_op_list = + new std::set({ + // clang-format off + // go/keep-sorted start + TF::AbsOp::getOperationName().str(), + TF::AddOp::getOperationName().str(), + TF::AddV2Op::getOperationName().str(), + TF::ArgMaxOp::getOperationName().str(), + TF::AvgPoolOp::getOperationName().str(), + TF::BiasAddOp::getOperationName().str(), + TF::BucketizeOp::getOperationName().str(), + TF::ConcatV2Op::getOperationName().str(), + TF::Conv2DBackpropInputOp::getOperationName().str(), + TF::Conv2DOp::getOperationName().str(), + TF::DepthwiseConv2dNativeOp::getOperationName().str(), + TF::FusedBatchNormV3Op::getOperationName().str(), + TF::GatherV2Op::getOperationName().str(), + TF::MatMulOp::getOperationName().str(), + TF::MaxPoolOp::getOperationName().str(), + TF::MaximumOp::getOperationName().str(), + TF::MeanOp::getOperationName().str(), + TF::MinimumOp::getOperationName().str(), + TF::MulOp::getOperationName().str(), + TF::PadOp::getOperationName().str(), + TF::PadV2Op::getOperationName().str(), + TF::Relu6Op::getOperationName().str(), + TF::ReluOp::getOperationName().str(), + TF::ReshapeOp::getOperationName().str(), + TF::SoftmaxOp::getOperationName().str(), + TF::SubOp::getOperationName().str(), + TF::TransposeOp::getOperationName().str(), + // go/keep-sorted end + // clang-format on + }); + return *legacy_op_list; +} + +// The list of quantizable ops in the Default mode. +ABSL_ATTRIBUTE_NOINLINE const std::set + &QuantizableOpsInDefaultMode() { + static const std::set *default_op_list = + new std::set({ + // clang-format off + // go/keep-sorted start + TF::BiasAddOp::getOperationName().str(), + TF::Conv2DBackpropInputOp::getOperationName().str(), + TF::Conv2DOp::getOperationName().str(), + TF::DepthwiseConv2dNativeOp::getOperationName().str(), + TF::FusedBatchNormV3Op::getOperationName().str(), + TF::MatMulOp::getOperationName().str(), + TF::Relu6Op::getOperationName().str(), + TF::ReluOp::getOperationName().str(), + // go/keep-sorted end + // clang-format on + }); + return *default_op_list; +} +// LINT.ThenChange(Google-internal path) + +// Checks if the operation can be fused with bias. +inline bool IsFusibleWithBiasOp(Operation *op) { + return llvm::isa< + // clang-format off + TF::MatMulOp, + TF::Conv2DOp, + TF::DepthwiseConv2dNativeOp, + TF::Conv2DBackpropInputOp, + TF::Conv3DOp, + TF::Conv3DBackpropInputV2Op + // clang-format on + >(op); +} + +// Creates the custom option of the Flex ops. +inline void CreateFlexOpCustomOptions(const std::string &op_name, + const std::string &node_def_str, + std::string &custom_option_buffer) { + auto flex_builder = std::make_unique(); + flex_builder->Vector([&]() { + flex_builder->String(op_name); + flex_builder->String(node_def_str); + }); + flex_builder->Finish(); + custom_option_buffer.assign(flex_builder->GetBuffer().begin(), + flex_builder->GetBuffer().end()); +} + +// Creates ElementsAttr for custom option. +inline TFL::ConstBytesAttr CustomOptionForFlexOp(OpBuilder *builder, + const std::string &content) { + return TFL::ConstBytesAttr::get(builder->getContext(), + StringRef(content.data(), content.size())); +} + +// Fallbacks ops that are not supported by TF Quantization to TFLite Flex ops. +class FallbackToFlexOps + : public PassWrapper> { + public: + FallbackToFlexOps() = default; + explicit FallbackToFlexOps(const std::string &mode) { mode_ = mode; } + FallbackToFlexOps(const FallbackToFlexOps &other) { mode_ = other.mode_; } + + void runOnOperation() override; + + StringRef getArgument() const final { return "quant-raise-flex-fallback"; } + + StringRef getDescription() const final { + return "Fallback TF-Quantization-unsupported ops to TFLite Flex ops."; + } + + void getDependentDialects(DialectRegistry ®istry) const override { + registry.insert(); + } + + private: + // The mode of TF Quantization, might indicate different users/devices. + Option mode_{*this, "mode", + llvm::cl::desc("The mode of TF Quantization."), + llvm::cl::init("")}; + + // Checks if the operation is allowlisted in the current mode. + bool IsAllowListedOp(Operation *op) { + std::string op_name = op->getName().getStringRef().str(); + if (IsAlwaysAllowlistedOp(op) || IsTfFakeQuantOp(op)) { + return true; + } else if (mode_ == kDefaultMode) { + return QuantizableOpsInDefaultMode().count(op_name) > 0; + } else if (mode_ == kLegacyIntegerMode) { + return QuantizableOpsInLegacyMode().count(op_name) > 0; + } else { + mlir::emitError(getOperation().getLoc(), "Unregconized mode: " + mode_); + signalPassFailure(); + return true; + } + } + + // Converts the operation to a TFLite Flex op. + bool ConvertToFlexOp(Operation *op); +}; + +bool FallbackToFlexOps::ConvertToFlexOp(Operation *op) { + absl::StatusOr> node_def = + tensorflow::ConvertTFDialectOpToNodeDef( + op, /*name=*/"", /*ignore_unregistered_attrs=*/true); + if (!node_def.ok()) { + op->emitError("Failed to obtain TensorFlow NodeDef: " + + node_def.status().ToString()); + return false; + } + std::string node_def_str; + if (!(*node_def)->SerializeToString(&node_def_str)) { + op->emitError("Failed to serialize tensorflow NodeDef"); + return false; + } + std::string op_name = (*node_def)->op(); + + OpBuilder builder(op); + std::string flex_op_name = std::string(kFlexOpNamePrefix) + op_name; + std::string custom_option_buffer; + CreateFlexOpCustomOptions(op_name, node_def_str, custom_option_buffer); + auto flex_op = TFL::CustomOp::create( + builder, op->getLoc(), op->getResultTypes(), op->getOperands(), + flex_op_name, CustomOptionForFlexOp(&builder, custom_option_buffer)); + op->replaceAllUsesWith(flex_op); + op->erase(); + return true; +} + +// Sets the "no_fallback" attribute. +Value SetNoFallbackAttr(PatternRewriter &rewriter, Value val) { + val.getDefiningOp()->setAttr(kNoFallbackAttr, rewriter.getUnitAttr()); + return val; +} + +// Returns true if the attr is a float attribute and be equal to value. +static bool FloatValueEquals(const Attribute &attr, double value) { + auto fp_attr = mlir::dyn_cast_or_null(attr); + if (fp_attr == nullptr) return false; + + if (fp_attr.isSplat()) { + return fp_attr.getSplatValue().isExactlyValue(value); + } + return llvm::all_of(fp_attr.getValues(), [value](const APFloat &f) { + return f.isExactlyValue(value); + }); +} + +// Returns true if the rank of the value equals to the given rank. +bool RankEquals(Value value, int rank) { + auto rank_type = mlir::dyn_cast(value.getType()); + return (rank_type && rank_type.getRank() == rank); +} + +#include "tensorflow/compiler/mlir/lite/quantization/tensorflow/fallback_to_flex_patterns.inc" + +void FallbackToFlexOps::runOnOperation() { + if (mode_.empty()) return; + + func::FuncOp func = getOperation(); + MLIRContext *ctx = &getContext(); + + // Convert binary ops to BiasAdd ops if possible. + RewritePatternSet patterns(ctx); + populateWithGenerated(patterns); + (void)applyPatternsGreedily(func, std::move(patterns)); + + // Convert unsupported ops to Flex ops. + auto tf_dialect = ctx->getLoadedDialect(); + func.walk([&](Operation *op) { + if (op->getDialect() != tf_dialect) return; + if (IsAllowListedOp(op)) return; + if (op->hasAttr(kNoFallbackAttr)) { + op->removeAttr(kNoFallbackAttr); + return; + } + if (!ConvertToFlexOp(op)) signalPassFailure(); + }); +} +} // namespace internal + +std::unique_ptr> CreateFallbackToFlexOpsPass( + const std::string &mode) { + return std::make_unique(mode); +} + +static PassRegistration pass([] { + return CreateFallbackToFlexOpsPass(/*mode=*/internal::kDefaultMode); +}); + +} // namespace TF +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tensorflow/fallback_to_flex_patterns.td b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tensorflow/fallback_to_flex_patterns.td new file mode 100644 index 0000000000000000000000000000000000000000..869cba538e423bd2e46c1694c92d647efa51402b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tensorflow/fallback_to_flex_patterns.td @@ -0,0 +1,88 @@ +/* Copyright 2021 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 "mlir/IR/OpBase.td" +include "mlir/IR/PatternBase.td" +include "mlir/Dialect/Func/IR/FuncOps.td" +include "mlir/Dialect/Arith/IR/ArithOps.td" +include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.td" + +// Marks the op as no fallback. +def MarkNoFallback : NativeCodeCall<"SetNoFallbackAttr($_builder, $0)">; + +def NoFallbackAttrNotSet : ConstrainthasAttr(kNoFallbackAttr)">>; + +class FloatValueEquals : Constraint>; + +class RankEquals : Constraint>; + +def IsFusibleWithBias : Constraint>; + +// Folds TF IdentityOp with constant input. +def RemoveConstIdentityOp : Pat< + (TF_IdentityOp (TF_ConstOp $input)), + (TF_ConstOp $input)>; + +// Standardizes the Max and Min ops by moving constant value to rhs. This will +// make it easier to create Relu1 matching patterns. +def SwapMaximumOperands : Pat< + (TF_MaximumOp (TF_ConstOp:$cst $cst_val), $input), + (TF_MaximumOp $input, $cst)>; + +def SwapMinimumOperands : Pat< + (TF_MinimumOp (TF_ConstOp:$cst $cst_val), $input), + (TF_MinimumOp $input, $cst)>; + +// Relu1 activation is represented as a couple of Max and Min ops, The following +// patterns recognize and keep them as TF ops so they can be converted to the +// TFLite Relu1 op. +def MatchRelu1Pattern1 : Pat< + (TF_MinimumOp:$min_op + (TF_MaximumOp $input, (TF_ConstOp:$cst_negone $NegOne)), + (TF_ConstOp:$cst_one $One)), + (MarkNoFallback + (TF_MinimumOp + (MarkNoFallback (TF_MaximumOp $input, $cst_negone)), + $cst_one)), + [(FloatValueEquals<"-1"> $NegOne), (FloatValueEquals<"1"> $One), + (NoFallbackAttrNotSet $min_op)]>; + +def MatchRelu1Pattern2 : Pat< + (TF_MaximumOp:$max_op + (TF_MinimumOp $input, (TF_ConstOp:$cst_one $One)), + (TF_ConstOp:$cst_negone $NegOne)), + (MarkNoFallback + (TF_MaximumOp + (MarkNoFallback (TF_MinimumOp $input, $cst_one)), + $cst_negone)), + [(FloatValueEquals<"-1"> $NegOne), (FloatValueEquals<"1"> $One), + (NoFallbackAttrNotSet $max_op)]>; + +// Keeps Add and Sub ops if the second operand is bias. +def KeepAddV2Op : Pat< + (TF_AddV2Op:$add_op $input, (TF_ConstOp:$bias_cst $bias)), + (MarkNoFallback (TF_AddV2Op $input, $bias_cst)), + [(IsFusibleWithBias $input), (RankEquals<"1"> $bias_cst), + (NoFallbackAttrNotSet $add_op)]>; + +def KeepSubOp : Pat< + (TF_SubOp:$sub_op $input, (TF_ConstOp:$bias_cst $bias)), + (MarkNoFallback (TF_SubOp $input, $bias_cst)), + [(IsFusibleWithBias $input), (RankEquals<"1"> $bias_cst), + (NoFallbackAttrNotSet $sub_op)]>; diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tensorflow/passes.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tensorflow/passes.h new file mode 100644 index 0000000000000000000000000000000000000000..b7dc47e7b42fefe7e3bb6ab664d21ce7f3dcd316 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tensorflow/passes.h @@ -0,0 +1,38 @@ +/* Copyright 2020 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_TENSORFLOW_PASSES_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_TENSORFLOW_PASSES_H_ + +#include +#include + +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project + +namespace mlir { +namespace TF { + +// Legalize the tf ops to the quant ops, so the quantization passes can work. +std::unique_ptr> CreateLegalizeTFToQuantPass(); + +// Fallbacks ops that are not supported by TF Quantization to TFLite Flex ops. +std::unique_ptr> CreateFallbackToFlexOpsPass( + const std::string &mode = "DEFAULT"); + +} // namespace TF +} // namespace mlir +#endif // TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_TENSORFLOW_PASSES_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tensorflow/tests/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tensorflow/tests/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..c91c2bd99fc7107afdee31cfc28a70a1197b4575 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tensorflow/tests/BUILD @@ -0,0 +1,24 @@ +load("//tensorflow:tensorflow.default.bzl", "filegroup") +load("//tensorflow/compiler/mlir:glob_lit_test.bzl", "glob_lit_tests") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + licenses = ["notice"], +) + +glob_lit_tests( + name = "all_tests", + data = [":test_utilities"], + driver = "@llvm-project//mlir:run_lit.sh", + test_file_exts = ["mlir"], +) + +# Bundle together all of the test utilities that are used by tests. +filegroup( + name = "test_utilities", + testonly = True, + data = [ + "//tensorflow/compiler/mlir/lite:litert-opt", + "@llvm-project//llvm:FileCheck", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tensorflow/tests/fallback_to_flex_ops_default.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tensorflow/tests/fallback_to_flex_ops_default.mlir new file mode 100644 index 0000000000000000000000000000000000000000..c9fc68c6a2469ad1e3499d9f357f938fe2e93ecd --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tensorflow/tests/fallback_to_flex_ops_default.mlir @@ -0,0 +1,154 @@ +// RUN: litert-opt -quant-raise-flex-fallback %s | FileCheck %s + +// CHECK-LABEL: bias_add +func.func @bias_add(%arg0: tensor<1x10x10x32xf32>, %arg1: tensor<32xf32>) -> tensor<1x10x10x32xf32> { + %0 = "tf.BiasAdd"(%arg0, %arg1) {T = "tfdtype$DT_FLOAT", data_format = "NHWC"} : (tensor<1x10x10x32xf32>, tensor<32xf32>) -> tensor<1x10x10x32xf32> + func.return %0 : tensor<1x10x10x32xf32> +// CHECK: %[[BIASADD_0:.*]] = "tf.BiasAdd"(%arg0, %arg1) <{data_format = "NHWC"}> {T = "tfdtype$DT_FLOAT"} : (tensor<1x10x10x32xf32>, tensor<32xf32>) -> tensor<1x10x10x32xf32> +// CHECK: return %[[BIASADD_0]] : tensor<1x10x10x32xf32> +} + +// CHECK-LABEL: add +func.func @add(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>) -> tensor<1xf32> { + %0 = "tf.Add"(%arg0, %arg1) : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + func.return %0: tensor<1xf32> +// CHECK: %[[CUSTOM_0:.*]] = "tfl.custom"(%arg0, %arg1) <{custom_code = "FlexAdd", custom_option = #tfl}> : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> +// CHECK: return %[[CUSTOM_0]] : tensor<1xf32> +} + +// CHECK-LABEL: softmax +func.func @softmax(%arg0: tensor<8x16xf32>) -> tensor<8x16xf32> { + %0 = "tf.Softmax"(%arg0) : (tensor<8x16xf32>) -> tensor<8x16xf32> + func.return %0 : tensor<8x16xf32> +// CHECK: %[[CUSTOM_0:.*]] = "tfl.custom"(%arg0) <{custom_code = "FlexSoftmax", custom_option = #tfl}> : (tensor<8x16xf32>) -> tensor<8x16xf32> +// CHECK: return %[[CUSTOM_0]] : tensor<8x16xf32> +} + +// CHECK-LABEL: conv2d_backprop_input_with_add +func.func @conv2d_backprop_input_with_add(%arg0: tensor<4xi32>, %arg1: tensor<3x3x1x32xf32>, %arg2: tensor<15x14x14x32xf32>) -> tensor<15x28x28x1xf32> { + %0 = "tf.Conv2DBackpropInput"(%arg0, %arg1, %arg2) {strides = [1, 2, 2, 1], padding="SAME", dilations=[1, 1, 1, 1]}: (tensor<4xi32>, tensor<3x3x1x32xf32>, tensor<15x14x14x32xf32>) -> tensor<15x28x28x1xf32> + %1 = "tf.Const"() {value = dense<1.000000e+00> : tensor<1xf32>} : () -> tensor<1xf32> + %2 = "tf.AddV2"(%0, %1): (tensor<15x28x28x1xf32>, tensor<1xf32>) -> tensor<15x28x28x1xf32> + func.return %2 : tensor<15x28x28x1xf32> +// CHECK: %[[CONST_0:.*]] = "tf.Const"() <{value = dense<1.000000e+00> : tensor<1xf32>}> : () -> tensor<1xf32> +// CHECK: %[[CONV2DBACKPROPINPUT_0:.*]] = "tf.Conv2DBackpropInput"(%arg0, %arg1, %arg2) <{dilations = [1, 1, 1, 1], padding = "SAME", strides = [1, 2, 2, 1]}> : (tensor<4xi32>, tensor<3x3x1x32xf32>, tensor<15x14x14x32xf32>) -> tensor<15x28x28x1xf32> +// CHECK: %[[ADDV2_0:.*]] = "tf.AddV2"(%[[CONV2DBACKPROPINPUT_0]], %[[CONST_0]]) : (tensor<15x28x28x1xf32>, tensor<1xf32>) -> tensor<15x28x28x1xf32> +// CHECK: return %[[ADDV2_0]] : tensor<15x28x28x1xf32> +} + +// CHECK-LABEL: conv2d_backprop_input_with_sub +func.func @conv2d_backprop_input_with_sub(%arg0: tensor<4xi32>, %arg1: tensor<3x3x1x32xf32>, %arg2: tensor<15x14x14x32xf32>) -> tensor<15x28x28x1xf32> { + %0 = "tf.Conv2DBackpropInput"(%arg0, %arg1, %arg2) {strides = [1, 2, 2, 1], padding="SAME", dilations=[1, 1, 1, 1]}: (tensor<4xi32>, tensor<3x3x1x32xf32>, tensor<15x14x14x32xf32>) -> tensor<15x28x28x1xf32> + %1 = "tf.Const"() {value = dense<1.000000e+00> : tensor<1xf32>} : () -> tensor<1xf32> + %2 = "tf.Sub"(%0, %1): (tensor<15x28x28x1xf32>, tensor<1xf32>) -> tensor<15x28x28x1xf32> + func.return %2 : tensor<15x28x28x1xf32> +// CHECK: %[[CONST_0:.*]] = "tf.Const"() <{value = dense<1.000000e+00> : tensor<1xf32>}> : () -> tensor<1xf32> +// CHECK: %[[CONV2DBACKPROPINPUT_0:.*]] = "tf.Conv2DBackpropInput"(%arg0, %arg1, %arg2) <{dilations = [1, 1, 1, 1], padding = "SAME", strides = [1, 2, 2, 1]}> : (tensor<4xi32>, tensor<3x3x1x32xf32>, tensor<15x14x14x32xf32>) -> tensor<15x28x28x1xf32> +// CHECK: %[[SUB_0:.*]] = "tf.Sub"(%[[CONV2DBACKPROPINPUT_0]], %[[CONST_0]]) : (tensor<15x28x28x1xf32>, tensor<1xf32>) -> tensor<15x28x28x1xf32> +// CHECK: return %[[SUB_0]] : tensor<15x28x28x1xf32> +} + +// CHECK-LABEL: depth_to_space +func.func @depth_to_space(%arg0: tensor<1x1x1x4xf32>) -> tensor<1x2x2x1xf32> { + %0 = "tf.DepthToSpace"(%arg0) {block_size = 2: i64, data_format = "NHWC"}: (tensor<1x1x1x4xf32>) -> tensor<1x2x2x1xf32> + func.return %0 : tensor<1x2x2x1xf32> +// CHECK: %[[CUSTOM_0:.*]] = "tfl.custom"(%arg0) <{custom_code = "FlexDepthToSpace", custom_option = #tfl}> : (tensor<1x1x1x4xf32>) -> tensor<1x2x2x1xf32> +// CHECK: return %[[CUSTOM_0]] : tensor<1x2x2x1xf32> +} + +// CHECK-LABEL: floor_mod +func.func @floor_mod(%arg0: tensor<5xf32>, %arg1: tensor<5xf32>) -> tensor<5xf32> { + %0 = "tf.FloorMod"(%arg0, %arg1) : (tensor<5xf32>, tensor<5xf32>) -> tensor<5xf32> + func.return %0 : tensor<5xf32> +// CHECK: %[[CUSTOM_0:.*]] = "tfl.custom"(%arg0, %arg1) <{custom_code = "FlexFloorMod", custom_option = #tfl}> : (tensor<5xf32>, tensor<5xf32>) -> tensor<5xf32> +// CHECK: return %[[CUSTOM_0]] : tensor<5xf32> +} + +// CHECK-LABEL: identity_with_const +func.func @identity_with_const() -> tensor<*xf32> { + %cst = "tf.Const"() {device = "", value = dense<[2.167590e-01, 2.89403105]> : tensor<2xf32>} : () -> tensor<2xf32> + %cst_1 = "tf.Const"() {device = "", value = dense<1.000000e-03> : tensor} : () -> tensor + %0 = "tf.Identity"(%cst) {device = ""} : (tensor<2xf32>) -> tensor<*xf32> + %1 = "tf.AddV2"(%0, %cst_1) {device = ""} : (tensor<*xf32>, tensor) -> tensor<*xf32> + func.return %1 : tensor<*xf32> +// CHECK: %[[CONST_0:.*]] = "tf.Const"() <{value = dense<[2.177590e-01, 2.89503098]> : tensor<2xf32>}> : () -> tensor<*xf32> +// CHECK: return %[[CONST_0]] : tensor<*xf32> +} + +func.func @identity(%arg0: tensor<2xf32>) -> tensor<*xf32> { + %cst_1 = "tf.Const"() {device = "", value = dense<1.000000e-03> : tensor} : () -> tensor + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor<2xf32>) -> tensor<*xf32> + %1 = "tf.AddV2"(%0, %cst_1) {device = ""} : (tensor<*xf32>, tensor) -> tensor<*xf32> + func.return %1 : tensor<*xf32> +// CHECK: %[[CONST_0:.*]] = "tf.Const"() <{value = dense<1.000000e-03> : tensor}> {device = ""} : () -> tensor +// CHECK: %[[IDENTITY_0:.*]] = "tf.Identity"(%arg0) {device = ""} : (tensor<2xf32>) -> tensor<*xf32> +// CHECK: %[[ADDV2_0:.*]] = "tfl.custom"(%0, %cst) <{custom_code = "FlexAddV2", custom_option = #tfl}> : (tensor<*xf32>, tensor) -> tensor<*xf32> +// CHECK: return %[[ADDV2_0]] : tensor<*xf32> +} + +// CHECK-LABEL: bias_add_with_identity +func.func @bias_add_with_identity(%arg0: tensor<4xi32>, %arg1: tensor<3x3x1x32xf32>, %arg2: tensor<15x14x14x32xf32>) -> tensor<15x28x28x1xf32> { + %cst = "tf.Const"() {device = "", value = dense<[2.167590e-01]> : tensor<1xf32>} : () -> tensor<1xf32> + %cst_1 = "tf.Const"() {device = "", value = dense<1.000000e-03> : tensor} : () -> tensor + %0 = "tf.Identity"(%cst) {device = ""} : (tensor<1xf32>) -> tensor<1xf32> + %1 = "tf.AddV2"(%0, %cst_1) {device = ""} : (tensor<1xf32>, tensor) -> tensor<1xf32> + %2 = "tf.Conv2DBackpropInput"(%arg0, %arg1, %arg2) {strides = [1, 2, 2, 1], padding="SAME", dilations=[1, 1, 1, 1]}: (tensor<4xi32>, tensor<3x3x1x32xf32>, tensor<15x14x14x32xf32>) -> tensor<15x28x28x1xf32> + %3 = "tf.AddV2"(%2, %1): (tensor<15x28x28x1xf32>, tensor<1xf32>) -> tensor<15x28x28x1xf32> + func.return %2 : tensor<15x28x28x1xf32> +// CHECK: %[[CONV2DBACKPROPINPUT_0:.*]] = "tf.Conv2DBackpropInput"(%arg0, %arg1, %arg2) <{dilations = [1, 1, 1, 1], padding = "SAME", strides = [1, 2, 2, 1]}> : (tensor<4xi32>, tensor<3x3x1x32xf32>, tensor<15x14x14x32xf32>) -> tensor<15x28x28x1xf32> +// CHECK: return %[[CONV2DBACKPROPINPUT_0]] : tensor<15x28x28x1xf32> +} + +// CHECK-LABEL: conv_with_relu1_pattern1 +func.func @conv_with_relu1_pattern1(%arg0: tensor<1x3x4x3xf32>) -> (tensor<1x3x4x2xf32>) { + %cst = "tf.Const"() {value = dense<[[[[-8.69931221, 6.44628429], [-9.18393421, 1.53671741], [8.68561744, -3.581774]]]]> : tensor<1x1x3x2xf32>} : () -> tensor<1x1x3x2xf32> + %cst_0 = "tf.Const"() {value = dense<-1.000000e+00> : tensor} : () -> tensor + %cst_1 = "tf.Const"() {value = dense<1.000000e+00> : tensor} : () -> tensor + %0 = "tf.Conv2D"(%arg0, %cst) {data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "SAME", strides = [1, 1, 1, 1]} : (tensor<1x3x4x3xf32>, tensor<1x1x3x2xf32>) -> tensor<1x3x4x2xf32> + %1 = "tf.Maximum"(%0, %cst_0) : (tensor<1x3x4x2xf32>, tensor) -> tensor<1x3x4x2xf32> + %2 = "tf.Minimum"(%1, %cst_1) : (tensor<1x3x4x2xf32>, tensor) -> tensor<1x3x4x2xf32> + func.return %2 : tensor<1x3x4x2xf32> +// CHECK-DAG: %[[CONST_0:.*]] = "tf.Const"() <{value = dense<{{.*}}> : tensor<1x1x3x2xf32>}> : () -> tensor<1x1x3x2xf32> +// CHECK-DAG: %[[CONST_1:.*]] = "tf.Const"() <{value = dense<-1.000000e+00> : tensor}> : () -> tensor +// CHECK-DAG: %[[CONST_2:.*]] = "tf.Const"() <{value = dense<1.000000e+00> : tensor}> : () -> tensor +// CHECK: %[[CONV2D_0:.*]] = "tf.Conv2D"(%arg0, %[[CONST_0]]) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "SAME", strides = [1, 1, 1, 1]}> : (tensor<1x3x4x3xf32>, tensor<1x1x3x2xf32>) -> tensor<1x3x4x2xf32> +// CHECK: %[[MAXIMUM_0:.*]] = "tf.Maximum"(%[[CONV2D_0]], %[[CONST_1]]) : (tensor<1x3x4x2xf32>, tensor) -> tensor<1x3x4x2xf32> +// CHECK: %[[MINIMUM_0:.*]] = "tf.Minimum"(%[[MAXIMUM_0]], %[[CONST_2]]) : (tensor<1x3x4x2xf32>, tensor) -> tensor<1x3x4x2xf32> +// CHECK: return %[[MINIMUM_0]] : tensor<1x3x4x2xf32> +} + +// CHECK-LABEL: conv_with_relu1_pattern2 +func.func @conv_with_relu1_pattern2(%arg0: tensor<1x3x4x3xf32>) -> (tensor<1x3x4x2xf32>) { + %cst = "tf.Const"() {value = dense<[[[[-8.69931221, 6.44628429], [-9.18393421, 1.53671741], [8.68561744, -3.581774]]]]> : tensor<1x1x3x2xf32>} : () -> tensor<1x1x3x2xf32> + %cst_0 = "tf.Const"() {value = dense<-1.000000e+00> : tensor} : () -> tensor + %cst_1 = "tf.Const"() {value = dense<1.000000e+00> : tensor} : () -> tensor + %0 = "tf.Conv2D"(%arg0, %cst) {data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "SAME", strides = [1, 1, 1, 1]} : (tensor<1x3x4x3xf32>, tensor<1x1x3x2xf32>) -> tensor<1x3x4x2xf32> + %1 = "tf.Minimum"(%0, %cst_1) : (tensor<1x3x4x2xf32>, tensor) -> tensor<1x3x4x2xf32> + %2 = "tf.Maximum"(%1, %cst_0) : (tensor<1x3x4x2xf32>, tensor) -> tensor<1x3x4x2xf32> + func.return %2 : tensor<1x3x4x2xf32> +// CHECK-DAG: %[[CONST_0:.*]] = "tf.Const"() <{value = dense<{{.*}}> : tensor<1x1x3x2xf32>}> : () -> tensor<1x1x3x2xf32> +// CHECK-DAG: %[[CONST_1:.*]] = "tf.Const"() <{value = dense<-1.000000e+00> : tensor}> : () -> tensor +// CHECK-DAG: %[[CONST_2:.*]] = "tf.Const"() <{value = dense<1.000000e+00> : tensor}> : () -> tensor +// CHECK: %[[CONV2D_0:.*]] = "tf.Conv2D"(%arg0, %[[CONST_0]]) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "SAME", strides = [1, 1, 1, 1]}> : (tensor<1x3x4x3xf32>, tensor<1x1x3x2xf32>) -> tensor<1x3x4x2xf32> +// CHECK: %[[MINIMUM_0:.*]] = "tf.Minimum"(%[[CONV2D_0]], %[[CONST_2]]) : (tensor<1x3x4x2xf32>, tensor) -> tensor<1x3x4x2xf32> +// CHECK: %[[MAXIMUM_0:.*]] = "tf.Maximum"(%[[MINIMUM_0]], %[[CONST_1]]) : (tensor<1x3x4x2xf32>, tensor) -> tensor<1x3x4x2xf32> +// CHECK: return %[[MAXIMUM_0]] : tensor<1x3x4x2xf32> +} + +// CHECK-LABEL: conv_with_relu1_invalid_pattern +func.func @conv_with_relu1_invalid_pattern(%arg0: tensor<1x3x4x3xf32>) -> (tensor<1x3x4x2xf32>) { + %cst = "tf.Const"() {value = dense<[[[[-8.69931221, 6.44628429], [-9.18393421, 1.53671741], [8.68561744, -3.581774]]]]> : tensor<1x1x3x2xf32>} : () -> tensor<1x1x3x2xf32> + %cst_0 = "tf.Const"() {value = dense<[-1.000000e+00, -3.000000e+00]> : tensor<2xf32>} : () -> tensor<2xf32> + %cst_1 = "tf.Const"() {value = dense<[1.000000e+00, 3.000000e+00]> : tensor<2xf32>} : () -> tensor<2xf32> + %0 = "tf.Conv2D"(%arg0, %cst) {data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "SAME", strides = [1, 1, 1, 1]} : (tensor<1x3x4x3xf32>, tensor<1x1x3x2xf32>) -> tensor<1x3x4x2xf32> + %1 = "tf.Minimum"(%0, %cst_1) : (tensor<1x3x4x2xf32>, tensor<2xf32>) -> tensor<1x3x4x2xf32> + %2 = "tf.Maximum"(%1, %cst_0) : (tensor<1x3x4x2xf32>, tensor<2xf32>) -> tensor<1x3x4x2xf32> + func.return %2 : tensor<1x3x4x2xf32> +// CHECK-DAG: %[[CONST_0:.*]] = "tf.Const"() <{value = dense<{{.*}}> : tensor<1x1x3x2xf32>}> : () -> tensor<1x1x3x2xf32> +// CHECK-DAG: %[[CONST_1:.*]] = "tf.Const"() <{value = dense<[-1.000000e+00, -3.000000e+00]> : tensor<2xf32>}> : () -> tensor<2xf32> +// CHECK-DAG: %[[CONST_2:.*]] = "tf.Const"() <{value = dense<[1.000000e+00, 3.000000e+00]> : tensor<2xf32>}> : () -> tensor<2xf32> +// CHECK: %[[CONV2D_0:.*]] = "tf.Conv2D"(%arg0, %[[CONST_0]]) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "SAME", strides = [1, 1, 1, 1]}> : (tensor<1x3x4x3xf32>, tensor<1x1x3x2xf32>) -> tensor<1x3x4x2xf32> +// CHECK: %[[CUSTOM_0:.*]] = "tfl.custom"(%[[CONV2D_0]], %[[CONST_2]]) <{custom_code = "FlexMinimum", custom_option = #tfl}> : (tensor<1x3x4x2xf32>, tensor<2xf32>) -> tensor<1x3x4x2xf32> +// CHECK: %[[CUSTOM_1:.*]] = "tfl.custom"(%[[CUSTOM_0]], %[[CONST_1]]) <{custom_code = "FlexMaximum", custom_option = #tfl}> : (tensor<1x3x4x2xf32>, tensor<2xf32>) -> tensor<1x3x4x2xf32> +// CHECK: return %[[CUSTOM_1]] : tensor<1x3x4x2xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tensorflow/tests/fallback_to_flex_ops_legacy.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tensorflow/tests/fallback_to_flex_ops_legacy.mlir new file mode 100644 index 0000000000000000000000000000000000000000..7bad5f111e0498520dcbe535ae090bde849b3436 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tensorflow/tests/fallback_to_flex_ops_legacy.mlir @@ -0,0 +1,78 @@ +// RUN: litert-opt -quant-raise-flex-fallback='mode=LEGACY_INTEGER' %s | FileCheck %s + +// CHECK-LABEL: bias_add +func.func @bias_add(%arg0: tensor<1x10x10x32xf32>, %arg1: tensor<32xf32>) -> tensor<1x10x10x32xf32> { + %0 = "tf.BiasAdd"(%arg0, %arg1) {T = "tfdtype$DT_FLOAT", data_format = "NHWC"} : (tensor<1x10x10x32xf32>, tensor<32xf32>) -> tensor<1x10x10x32xf32> + func.return %0 : tensor<1x10x10x32xf32> +// CHECK: %[[BIASADD_0:.*]] = "tf.BiasAdd"(%arg0, %arg1) <{data_format = "NHWC"}> {T = "tfdtype$DT_FLOAT"} : (tensor<1x10x10x32xf32>, tensor<32xf32>) -> tensor<1x10x10x32xf32> +// CHECK: return %[[BIASADD_0]] : tensor<1x10x10x32xf32> +} + +// CHECK-LABEL: add +func.func @add(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>) -> tensor<1xf32> { + %0 = "tf.AddV2"(%arg0, %arg1) : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + func.return %0: tensor<1xf32> +// CHECK: %[[ADD_0:.*]] = "tf.AddV2"(%arg0, %arg1) : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> +// CHECK: return %[[ADD_0]] : tensor<1xf32> +} + +// CHECK-LABEL: softmax +func.func @softmax(%arg0: tensor<8x16xf32>) -> tensor<8x16xf32> { + %0 = "tf.Softmax"(%arg0) : (tensor<8x16xf32>) -> tensor<8x16xf32> + func.return %0 : tensor<8x16xf32> +// CHECK: %[[SOFTMAX_0:.*]] = "tf.Softmax"(%arg0) : (tensor<8x16xf32>) -> tensor<8x16xf32> +// CHECK: return %[[SOFTMAX_0]] : tensor<8x16xf32> +} + +// CHECK-LABEL: conv2d_backprop_input_with_add +func.func @conv2d_backprop_input_with_add(%arg0: tensor<4xi32>, %arg1: tensor<3x3x1x32xf32>, %arg2: tensor<15x14x14x32xf32>) -> tensor<15x28x28x1xf32> { + %0 = "tf.Conv2DBackpropInput"(%arg0, %arg1, %arg2) {strides = [1, 2, 2, 1], padding="SAME", dilations=[1, 1, 1, 1]}: (tensor<4xi32>, tensor<3x3x1x32xf32>, tensor<15x14x14x32xf32>) -> tensor<15x28x28x1xf32> + %1 = "tf.Const"() {value = dense<1.000000e+00> : tensor<1xf32>} : () -> tensor<1xf32> + %2 = "tf.AddV2"(%0, %1): (tensor<15x28x28x1xf32>, tensor<1xf32>) -> tensor<15x28x28x1xf32> + func.return %2 : tensor<15x28x28x1xf32> +// CHECK: %[[CONST_0:.*]] = "tf.Const"() <{value = dense<1.000000e+00> : tensor<1xf32>}> : () -> tensor<1xf32> +// CHECK: %[[CONV2DBACKPROPINPUT_0:.*]] = "tf.Conv2DBackpropInput"(%arg0, %arg1, %arg2) <{dilations = [1, 1, 1, 1], padding = "SAME", strides = [1, 2, 2, 1]}> : (tensor<4xi32>, tensor<3x3x1x32xf32>, tensor<15x14x14x32xf32>) -> tensor<15x28x28x1xf32> +// CHECK: %[[ADDV2_0:.*]] = "tf.AddV2"(%[[CONV2DBACKPROPINPUT_0]], %[[CONST_0]]) {no_fallback} : (tensor<15x28x28x1xf32>, tensor<1xf32>) -> tensor<15x28x28x1xf32> +// CHECK: return %[[ADDV2_0]] : tensor<15x28x28x1xf32> +} + +// CHECK-LABEL: conv2d_backprop_input_with_sub +func.func @conv2d_backprop_input_with_sub(%arg0: tensor<4xi32>, %arg1: tensor<3x3x1x32xf32>, %arg2: tensor<15x14x14x32xf32>) -> tensor<15x28x28x1xf32> { + %0 = "tf.Conv2DBackpropInput"(%arg0, %arg1, %arg2) {strides = [1, 2, 2, 1], padding="SAME", dilations=[1, 1, 1, 1]}: (tensor<4xi32>, tensor<3x3x1x32xf32>, tensor<15x14x14x32xf32>) -> tensor<15x28x28x1xf32> + %1 = "tf.Const"() {value = dense<1.000000e+00> : tensor<1xf32>} : () -> tensor<1xf32> + %2 = "tf.Sub"(%0, %1): (tensor<15x28x28x1xf32>, tensor<1xf32>) -> tensor<15x28x28x1xf32> + func.return %2 : tensor<15x28x28x1xf32> +// CHECK: %[[CONST_0:.*]] = "tf.Const"() <{value = dense<1.000000e+00> : tensor<1xf32>}> : () -> tensor<1xf32> +// CHECK: %[[CONV2DBACKPROPINPUT_0:.*]] = "tf.Conv2DBackpropInput"(%arg0, %arg1, %arg2) <{dilations = [1, 1, 1, 1], padding = "SAME", strides = [1, 2, 2, 1]}> : (tensor<4xi32>, tensor<3x3x1x32xf32>, tensor<15x14x14x32xf32>) -> tensor<15x28x28x1xf32> +// CHECK: %[[SUB_0:.*]] = "tf.Sub"(%[[CONV2DBACKPROPINPUT_0]], %[[CONST_0]]) {no_fallback} : (tensor<15x28x28x1xf32>, tensor<1xf32>) -> tensor<15x28x28x1xf32> +// CHECK: return %[[SUB_0]] : tensor<15x28x28x1xf32> +} + +// CHECK-LABEL: depth_to_space +func.func @depth_to_space(%arg0: tensor<1x1x1x4xf32>) -> tensor<1x2x2x1xf32> { + %0 = "tf.DepthToSpace"(%arg0) {block_size = 2: i64, data_format = "NHWC"}: (tensor<1x1x1x4xf32>) -> tensor<1x2x2x1xf32> + func.return %0 : tensor<1x2x2x1xf32> +// CHECK: %[[CUSTOM_0:.*]] = "tfl.custom"(%arg0) <{custom_code = "FlexDepthToSpace", custom_option = #tfl}> : (tensor<1x1x1x4xf32>) -> tensor<1x2x2x1xf32> +// CHECK: return %[[CUSTOM_0]] : tensor<1x2x2x1xf32> +} + +// CHECK-LABEL: floor_mod +func.func @floor_mod(%arg0: tensor<5xf32>, %arg1: tensor<5xf32>) -> tensor<5xf32> { + %0 = "tf.FloorMod"(%arg0, %arg1) : (tensor<5xf32>, tensor<5xf32>) -> tensor<5xf32> + func.return %0 : tensor<5xf32> +// CHECK: %[[CUSTOM_0:.*]] = "tfl.custom"(%arg0, %arg1) <{custom_code = "FlexFloorMod", custom_option = #tfl}> : (tensor<5xf32>, tensor<5xf32>) -> tensor<5xf32> +// CHECK: return %[[CUSTOM_0]] : tensor<5xf32> +} + +// CHECK-LABEL: bias_add_with_identity +func.func @bias_add_with_identity(%arg0: tensor<4xi32>, %arg1: tensor<3x3x1x32xf32>, %arg2: tensor<15x14x14x32xf32>) -> tensor<15x28x28x1xf32> { + %cst = "tf.Const"() {device = "", value = dense<[2.167590e-01]> : tensor<1xf32>} : () -> tensor<1xf32> + %cst_1 = "tf.Const"() {device = "", value = dense<1.000000e-03> : tensor} : () -> tensor + %0 = "tf.Identity"(%cst) {device = ""} : (tensor<1xf32>) -> tensor<1xf32> + %1 = "tf.AddV2"(%0, %cst_1) {device = ""} : (tensor<1xf32>, tensor) -> tensor<1xf32> + %2 = "tf.Conv2DBackpropInput"(%arg0, %arg1, %arg2) {strides = [1, 2, 2, 1], padding="SAME", dilations=[1, 1, 1, 1]}: (tensor<4xi32>, tensor<3x3x1x32xf32>, tensor<15x14x14x32xf32>) -> tensor<15x28x28x1xf32> + %3 = "tf.AddV2"(%2, %1): (tensor<15x28x28x1xf32>, tensor<1xf32>) -> tensor<15x28x28x1xf32> + func.return %2 : tensor<15x28x28x1xf32> +// CHECK: %[[CONV2DBACKPROPINPUT_0:.*]] = "tf.Conv2DBackpropInput"(%arg0, %arg1, %arg2) <{dilations = [1, 1, 1, 1], padding = "SAME", strides = [1, 2, 2, 1]}> : (tensor<4xi32>, tensor<3x3x1x32xf32>, tensor<15x14x14x32xf32>) -> tensor<15x28x28x1xf32> +// CHECK: return %[[CONV2DBACKPROPINPUT_0]] : tensor<15x28x28x1xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tensorflow/tests/tf_to_quant.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tensorflow/tests/tf_to_quant.mlir new file mode 100644 index 0000000000000000000000000000000000000000..4dfd044f4bc5b098d9b099edbd19ad3a85c842d0 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tensorflow/tests/tf_to_quant.mlir @@ -0,0 +1,148 @@ +// RUN: litert-opt -tf-to-quant %s | FileCheck %s + +// CHECK-LABEL: fakeQuantPerChannelForActivation +func.func @fakeQuantPerChannelForActivation(%arg0: tensor<8x3xf32>) -> (tensor<8x3xf32>) { + %arg1 = arith.constant dense<[0.0, -1.0, 1.0]> : tensor<3xf32> + %arg2 = arith.constant dense<[255.0, 254.0, 256.0]> : tensor<3xf32> + %0 = "tf.FakeQuantWithMinMaxVarsPerChannel"(%arg0, %arg1, %arg2) {num_bits = 5, narrow_range = false} : (tensor<8x3xf32>, tensor<3xf32>, tensor<3xf32>) -> tensor<8x3xf32> + func.return %0 : tensor<8x3xf32> + +// CHECK: %[[fq:.*]] = "tf.FakeQuantWithMinMaxVarsPerChannel"(%arg0, %cst, %cst_0) +// CHECK: %[[q:.*]] = "quantfork.qcast"(%[[fq]]) : (tensor<8x3xf32>) -> tensor<8x3x!quant.uniform> +// CHECK: %[[dq:.*]] = "quantfork.dcast"(%[[q]]) +// CHECK: return %[[dq]] +} + +// CHECK-LABEL: fakeQuantForActivation +func.func @fakeQuantForActivation(tensor<8xf32>) -> (tensor<8xf32>) { +^bb0(%arg0: tensor<8xf32>): + %arg1 = arith.constant dense<0.0> : tensor + %arg2 = arith.constant dense<255.0> : tensor + %0 = "tf.FakeQuantWithMinMaxVars"(%arg0, %arg1, %arg2) {num_bits = 5, narrow_range = false} : (tensor<8xf32>, tensor, tensor) -> tensor<8xf32> + func.return %0 : tensor<8xf32> + +// CHECK: %0 = "tf.FakeQuantWithMinMaxVars"(%arg0, %cst, %cst_0) +// CHECK: %1 = "quantfork.qcast"(%0) : (tensor<8xf32>) -> tensor<8x!quant.uniform> +// CHECK: %2 = "quantfork.dcast"(%1) +// CHECK: return %2 +} + +// CHECK-LABEL: fakeQuantForActivationNoDuplication +func.func @fakeQuantForActivationNoDuplication(tensor<8xf32>) -> (tensor<8x!quant.uniform>) { +^bb0(%arg0: tensor<8xf32>): + %arg1 = arith.constant dense<0.0> : tensor + %arg2 = arith.constant dense<255.0> : tensor + %0 = "tf.FakeQuantWithMinMaxVars"(%arg0, %arg1, %arg2) {num_bits = 5, narrow_range = false} : (tensor<8xf32>, tensor, tensor) -> tensor<8xf32> + %1 = "quantfork.qcast"(%0) : (tensor<8xf32>) -> tensor<8x!quant.uniform> + func.return %1 : tensor<8x!quant.uniform> + +// CHECK: %0 = "tf.FakeQuantWithMinMaxVars"(%arg0, %cst, %cst_0) <{narrow_range = false, num_bits = 5 : i64}> +// CHECK: %1 = "quantfork.qcast"(%0) : (tensor<8xf32>) -> tensor<8x!quant.uniform> +// CHECK: return %1 +} + +// CHECK-LABEL: fakeQuantFolded +func.func @fakeQuantFolded() -> (tensor<8xf32>) { + %in = arith.constant dense<0.0> : tensor<8xf32> + %min = arith.constant dense<0.0> : tensor + %max = arith.constant dense<255.0> : tensor + %mini = "tf.Identity"(%min) : (tensor) -> tensor + %maxi = "tf.Identity"(%max) : (tensor) -> tensor + %rst = "tf.FakeQuantWithMinMaxVars"(%in, %mini, %maxi) {num_bits = 5, narrow_range = false} : (tensor<8xf32>, tensor, tensor) -> tensor<8xf32> + func.return %rst : tensor<8xf32> + +// CHECK: %[[CONSTANT:.*]] = "tf.Const"() <{value = dense<0.000000e+00> : tensor<8xf32>}> +// CHECK: %[[QUANTIZE:.*]] = "quantfork.qcast"(%[[CONSTANT]]) : (tensor<8xf32>) -> tensor<8x!quant.uniform> +// CHECK: %[[DEQUANTIZE:.*]] = "quantfork.dcast"(%[[QUANTIZE]]) +// CHECK: return %[[DEQUANTIZE]] : tensor<8xf32> +} + +// CHECK-LABEL: fakeQuantNotFolded +func.func @fakeQuantNotFolded(tensor<8xf32>, tensor, tensor) -> (tensor<8xf32>) { +^bb0(%arg0: tensor<8xf32>, %arg3: tensor, %arg4: tensor): + %1 = "tf.FakeQuantWithMinMaxVars"(%arg0, %arg3, %arg4) {num_bits = 5, narrow_range = false} : (tensor<8xf32>, tensor, tensor) -> tensor<8xf32> + func.return %1 : tensor<8xf32> + +// CHECK: %0 = "tf.FakeQuantWithMinMaxVars"(%arg0, %arg1, %arg2) +// CHECK: return %0 : tensor<8xf32> +} + +// CHECK-LABEL: fakeQuantWithConv2D +func.func @fakeQuantWithConv2D(tensor<256x32x32x3xf32>) -> (tensor<256x8x7x16xf32>) { +^bb0(%arg: tensor<256x32x32x3xf32>) : + %in = arith.constant dense<0.0> : tensor<3x3x3x16xf32> + %min = arith.constant dense<0.0> : tensor + %max = arith.constant dense<255.0> : tensor + %mini = "tf.Identity"(%min) : (tensor) -> tensor + %maxi = "tf.Identity"(%max) : (tensor) -> tensor + %fq = "tf.FakeQuantWithMinMaxVars"(%in, %mini, %maxi) {num_bits = 5, narrow_range = false} : (tensor<3x3x3x16xf32>, tensor, tensor) -> tensor<3x3x3x16xf32> + %rst = "tf.Conv2D"(%arg, %fq) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", dilations = [1, 2, 3, 1], padding = "SAME", strides = [1, 4, 5, 1]} : (tensor<256x32x32x3xf32>, tensor<3x3x3x16xf32>) -> tensor<256x8x7x16xf32> + func.return %rst : tensor<256x8x7x16xf32> + +// CHECK: %[[CONSTANT0:.*]] = "tf.Const"() <{value = dense<0.000000e+00> : tensor<3x3x3x16xf32>}> +// CHECK: %[[QUANTIZE:.*]] = "quantfork.qcast"(%[[CONSTANT0]]) : (tensor<3x3x3x16xf32>) -> tensor<3x3x3x16x!quant.uniform> +// CHECK: %[[DEQUANTIZE:.*]] = "quantfork.dcast"(%[[QUANTIZE]]) +// CHECK: %[[CONV:.*]] = "tf.Conv2D"(%arg0, %[[DEQUANTIZE]]) +// CHECK: return %[[CONV]] +} + +// CHECK-LABEL: perChannelFakeQuantWithConv2D +func.func @perChannelFakeQuantWithConv2D(tensor<256x32x32x3xf32>) -> (tensor<256x8x7x16xf32>) { +^bb0(%arg: tensor<256x32x32x3xf32>) : + %in = arith.constant dense<0.0> : tensor<3x3x3x16xf32> + %min = arith.constant dense<0.0> : tensor<16xf32> + %max = arith.constant dense<255.0> : tensor<16xf32> + %mini = "tf.Identity"(%min) : (tensor<16xf32>) -> tensor<16xf32> + %maxi = "tf.Identity"(%max) : (tensor<16xf32>) -> tensor<16xf32> + %fq = "tf.FakeQuantWithMinMaxVarsPerChannel"(%in, %mini, %maxi) {num_bits = 5, narrow_range = false} : (tensor<3x3x3x16xf32>, tensor<16xf32>, tensor<16xf32>) -> tensor<3x3x3x16xf32> + %rst = "tf.Conv2D"(%arg, %fq) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", dilations = [1, 2, 3, 1], padding = "SAME", strides = [1, 4, 5, 1]} : (tensor<256x32x32x3xf32>, tensor<3x3x3x16xf32>) -> tensor<256x8x7x16xf32> + func.return %rst : tensor<256x8x7x16xf32> + +// CHECK: %[[CONSTANT0:.*]] = "tf.Const"() <{value = dense<0.000000e+00> : tensor<3x3x3x16xf32>}> +// CHECK: %[[QUANTIZE:.*]] = "quantfork.qcast"(%[[CONSTANT0]]) : (tensor<3x3x3x16xf32>) -> tensor<3x3x3x16x!quant.uniform> +// CHECK: %[[DEQUANTIZE:.*]] = "quantfork.dcast"(%[[QUANTIZE]]) +// CHECK: %[[CONV:.*]] = "tf.Conv2D"(%arg0, %[[DEQUANTIZE]]) +// CHECK: return %[[CONV]] : tensor<256x8x7x16xf32> +} + +// CHECK-LABEL: fakeQuantWithDepthwiseConv2D +func.func @fakeQuantWithDepthwiseConv2D(tensor<256x32x32x3xf32>) -> (tensor<256x30x30x16xf32>) { +^bb0(%arg: tensor<256x32x32x3xf32>) : + %in = arith.constant dense<0.0> : tensor<3x3x3x16xf32> + %min = arith.constant dense<0.0> : tensor + %max = arith.constant dense<255.0> : tensor + %mini = "tf.Identity"(%min) : (tensor) -> tensor + %maxi = "tf.Identity"(%max) : (tensor) -> tensor + %fq = "tf.FakeQuantWithMinMaxVars"(%in, %mini, %maxi) {num_bits = 5, narrow_range = false} : (tensor<3x3x3x16xf32>, tensor, tensor) -> tensor<3x3x3x16xf32> + %rst = "tf.DepthwiseConv2dNative"(%arg, %fq) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", dilations = [1, 2, 3, 1], padding = "SAME", strides = [1, 4, 5, 1]} : (tensor<256x32x32x3xf32>, tensor<3x3x3x16xf32>) -> tensor<256x30x30x16xf32> + func.return %rst : tensor<256x30x30x16xf32> + +// CHECK: %[[CONSTANT0:.*]] = "tf.Const"() <{value = dense<0.000000e+00> : tensor<3x3x3x16xf32>}> +// CHECK: %[[QUANTIZE:.*]] = "quantfork.qcast"(%[[CONSTANT0]]) : (tensor<3x3x3x16xf32>) -> tensor<3x3x3x16x!quant.uniform> +// CHECK: %[[DEQUANTIZE:.*]] = "quantfork.dcast"(%[[QUANTIZE]]) +// CHECK: %[[CONV:.*]] = "tf.DepthwiseConv2dNative"(%arg0, %[[DEQUANTIZE]]) +// CHECK: return %[[CONV]] +} + +// CHECK-LABEL: perChannelFakeQuantWithDepthwiseConv2D +func.func @perChannelFakeQuantWithDepthwiseConv2D(tensor<256x32x32x3xf32>) -> (tensor<256x30x30x16xf32>) { +^bb0(%arg: tensor<256x32x32x3xf32>) : + %in = arith.constant dense<0.0> : tensor<3x3x3x16xf32> + %min = arith.constant dense<0.0> : tensor<16xf32> + %max = arith.constant dense<255.0> : tensor<16xf32> + %mini = "tf.Identity"(%min) : (tensor<16xf32>) -> tensor<16xf32> + %maxi = "tf.Identity"(%max) : (tensor<16xf32>) -> tensor<16xf32> + %fq = "tf.FakeQuantWithMinMaxVarsPerChannel"(%in, %mini, %maxi) {num_bits = 5, narrow_range = false} : (tensor<3x3x3x16xf32>, tensor<16xf32>, tensor<16xf32>) -> tensor<3x3x3x16xf32> + %rst = "tf.DepthwiseConv2dNative"(%arg, %fq) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", dilations = [1, 2, 3, 1], padding = "SAME", strides = [1, 4, 5, 1]} : (tensor<256x32x32x3xf32>, tensor<3x3x3x16xf32>) -> tensor<256x30x30x16xf32> + func.return %rst : tensor<256x30x30x16xf32> + +// CHECK: %[[CONSTANT0:.*]] = "tf.Const"() <{value = dense<0.000000e+00> : tensor<3x3x3x16xf32>}> +// CHECK: %[[QUANTIZE:.*]] = "quantfork.qcast"(%[[CONSTANT0]]) : (tensor<3x3x3x16xf32>) -> tensor<3x3x3x16x!quant.uniform> +// CHECK: %[[DEQUANTIZE:.*]] = "quantfork.dcast"(%[[QUANTIZE]]) +// CHECK: %[[CONV:.*]] = "tf.DepthwiseConv2dNative"(%arg0, %[[DEQUANTIZE]]) +// CHECK: return %[[CONV]] +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tensorflow/tests/tf_to_quant_4bit.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tensorflow/tests/tf_to_quant_4bit.mlir new file mode 100644 index 0000000000000000000000000000000000000000..f1095ec2387e664e09f159f173540b573e3a5fe3 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tensorflow/tests/tf_to_quant_4bit.mlir @@ -0,0 +1,148 @@ +// RUN: litert-opt -tf-to-quant %s | FileCheck %s + +// CHECK-LABEL: fakeQuantPerChannelForActivation +func.func @fakeQuantPerChannelForActivation(%arg0: tensor<8x3xf32>) -> (tensor<8x3xf32>) { + %arg1 = arith.constant dense<[0.0, -1.0, 1.0]> : tensor<3xf32> + %arg2 = arith.constant dense<[15.0, 14.0, 16.0]> : tensor<3xf32> + %0 = "tf.FakeQuantWithMinMaxVarsPerChannel"(%arg0, %arg1, %arg2) {num_bits = 3, narrow_range = false} : (tensor<8x3xf32>, tensor<3xf32>, tensor<3xf32>) -> tensor<8x3xf32> + func.return %0 : tensor<8x3xf32> + +// CHECK: %[[fq:.*]] = "tf.FakeQuantWithMinMaxVarsPerChannel"(%arg0, %cst, %cst_0) +// CHECK: %[[q:.*]] = "quantfork.qcast"(%[[fq]]) : (tensor<8x3xf32>) -> tensor<8x3x!quant.uniform> +// CHECK: %[[dq:.*]] = "quantfork.dcast"(%[[q]]) +// CHECK: return %[[dq]] +} + +// CHECK-LABEL: fakeQuantForActivation +func.func @fakeQuantForActivation(tensor<8xf32>) -> (tensor<8xf32>) { +^bb0(%arg0: tensor<8xf32>): + %arg1 = arith.constant dense<0.0> : tensor + %arg2 = arith.constant dense<15.0> : tensor + %0 = "tf.FakeQuantWithMinMaxVars"(%arg0, %arg1, %arg2) {num_bits = 3, narrow_range = false} : (tensor<8xf32>, tensor, tensor) -> tensor<8xf32> + func.return %0 : tensor<8xf32> + +// CHECK: %0 = "tf.FakeQuantWithMinMaxVars"(%arg0, %cst, %cst_0) +// CHECK: %1 = "quantfork.qcast"(%0) : (tensor<8xf32>) -> tensor<8x!quant.uniform> +// CHECK: %2 = "quantfork.dcast"(%1) +// CHECK: return %2 +} + +// CHECK-LABEL: fakeQuantForActivationNoDuplication +func.func @fakeQuantForActivationNoDuplication(tensor<8xf32>) -> (tensor<8x!quant.uniform>) { +^bb0(%arg0: tensor<8xf32>): + %arg1 = arith.constant dense<0.0> : tensor + %arg2 = arith.constant dense<15.0> : tensor + %0 = "tf.FakeQuantWithMinMaxVars"(%arg0, %arg1, %arg2) {num_bits = 3, narrow_range = false} : (tensor<8xf32>, tensor, tensor) -> tensor<8xf32> + %1 = "quantfork.qcast"(%0) : (tensor<8xf32>) -> tensor<8x!quant.uniform> + func.return %1 : tensor<8x!quant.uniform> + +// CHECK: %0 = "tf.FakeQuantWithMinMaxVars"(%arg0, %cst, %cst_0) <{narrow_range = false, num_bits = 3 : i64}> +// CHECK: %1 = "quantfork.qcast"(%0) : (tensor<8xf32>) -> tensor<8x!quant.uniform> +// CHECK: return %1 +} + +// CHECK-LABEL: fakeQuantFolded +func.func @fakeQuantFolded() -> (tensor<8xf32>) { + %in = arith.constant dense<0.0> : tensor<8xf32> + %min = arith.constant dense<0.0> : tensor + %max = arith.constant dense<15.0> : tensor + %mini = "tf.Identity"(%min) : (tensor) -> tensor + %maxi = "tf.Identity"(%max) : (tensor) -> tensor + %rst = "tf.FakeQuantWithMinMaxVars"(%in, %mini, %maxi) {num_bits = 3, narrow_range = false} : (tensor<8xf32>, tensor, tensor) -> tensor<8xf32> + func.return %rst : tensor<8xf32> + +// CHECK: %[[CONSTANT:.*]] = "tf.Const"() <{value = dense<0.000000e+00> : tensor<8xf32>}> +// CHECK: %[[QUANTIZE:.*]] = "quantfork.qcast"(%[[CONSTANT]]) : (tensor<8xf32>) -> tensor<8x!quant.uniform> +// CHECK: %[[DEQUANTIZE:.*]] = "quantfork.dcast"(%[[QUANTIZE]]) +// CHECK: return %[[DEQUANTIZE]] : tensor<8xf32> +} + +// CHECK-LABEL: fakeQuantNotFolded +func.func @fakeQuantNotFolded(tensor<8xf32>, tensor, tensor) -> (tensor<8xf32>) { +^bb0(%arg0: tensor<8xf32>, %arg3: tensor, %arg4: tensor): + %1 = "tf.FakeQuantWithMinMaxVars"(%arg0, %arg3, %arg4) {num_bits = 3, narrow_range = false} : (tensor<8xf32>, tensor, tensor) -> tensor<8xf32> + func.return %1 : tensor<8xf32> + +// CHECK: %0 = "tf.FakeQuantWithMinMaxVars"(%arg0, %arg1, %arg2) +// CHECK: return %0 : tensor<8xf32> +} + +// CHECK-LABEL: fakeQuantWithConv2D +func.func @fakeQuantWithConv2D(tensor<256x32x32x3xf32>) -> (tensor<256x8x7x16xf32>) { +^bb0(%arg: tensor<256x32x32x3xf32>) : + %in = arith.constant dense<0.0> : tensor<3x3x3x16xf32> + %min = arith.constant dense<0.0> : tensor + %max = arith.constant dense<15.0> : tensor + %mini = "tf.Identity"(%min) : (tensor) -> tensor + %maxi = "tf.Identity"(%max) : (tensor) -> tensor + %fq = "tf.FakeQuantWithMinMaxVars"(%in, %mini, %maxi) {num_bits = 3, narrow_range = false} : (tensor<3x3x3x16xf32>, tensor, tensor) -> tensor<3x3x3x16xf32> + %rst = "tf.Conv2D"(%arg, %fq) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", dilations = [1, 2, 3, 1], padding = "SAME", strides = [1, 4, 5, 1]} : (tensor<256x32x32x3xf32>, tensor<3x3x3x16xf32>) -> tensor<256x8x7x16xf32> + func.return %rst : tensor<256x8x7x16xf32> + +// CHECK: %[[CONSTANT0:.*]] = "tf.Const"() <{value = dense<0.000000e+00> : tensor<3x3x3x16xf32>}> +// CHECK: %[[QUANTIZE:.*]] = "quantfork.qcast"(%[[CONSTANT0]]) : (tensor<3x3x3x16xf32>) -> tensor<3x3x3x16x!quant.uniform> +// CHECK: %[[DEQUANTIZE:.*]] = "quantfork.dcast"(%[[QUANTIZE]]) +// CHECK: %[[CONV:.*]] = "tf.Conv2D"(%arg0, %[[DEQUANTIZE]]) +// CHECK: return %[[CONV]] +} + +// CHECK-LABEL: perChannelFakeQuantWithConv2D +func.func @perChannelFakeQuantWithConv2D(tensor<256x32x32x3xf32>) -> (tensor<256x8x7x16xf32>) { +^bb0(%arg: tensor<256x32x32x3xf32>) : + %in = arith.constant dense<0.0> : tensor<3x3x3x16xf32> + %min = arith.constant dense<0.0> : tensor<16xf32> + %max = arith.constant dense<15.0> : tensor<16xf32> + %mini = "tf.Identity"(%min) : (tensor<16xf32>) -> tensor<16xf32> + %maxi = "tf.Identity"(%max) : (tensor<16xf32>) -> tensor<16xf32> + %fq = "tf.FakeQuantWithMinMaxVarsPerChannel"(%in, %mini, %maxi) {num_bits = 3, narrow_range = false} : (tensor<3x3x3x16xf32>, tensor<16xf32>, tensor<16xf32>) -> tensor<3x3x3x16xf32> + %rst = "tf.Conv2D"(%arg, %fq) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", dilations = [1, 2, 3, 1], padding = "SAME", strides = [1, 4, 5, 1]} : (tensor<256x32x32x3xf32>, tensor<3x3x3x16xf32>) -> tensor<256x8x7x16xf32> + func.return %rst : tensor<256x8x7x16xf32> + +// CHECK: %[[CONSTANT0:.*]] = "tf.Const"() <{value = dense<0.000000e+00> : tensor<3x3x3x16xf32>}> +// CHECK: %[[QUANTIZE:.*]] = "quantfork.qcast"(%[[CONSTANT0]]) : (tensor<3x3x3x16xf32>) -> tensor<3x3x3x16x!quant.uniform> +// CHECK: %[[DEQUANTIZE:.*]] = "quantfork.dcast"(%[[QUANTIZE]]) +// CHECK: %[[CONV:.*]] = "tf.Conv2D"(%arg0, %[[DEQUANTIZE]]) +// CHECK: return %[[CONV]] : tensor<256x8x7x16xf32> +} + +// CHECK-LABEL: fakeQuantWithDepthwiseConv2D +func.func @fakeQuantWithDepthwiseConv2D(tensor<256x32x32x3xf32>) -> (tensor<256x30x30x16xf32>) { +^bb0(%arg: tensor<256x32x32x3xf32>) : + %in = arith.constant dense<0.0> : tensor<3x3x3x16xf32> + %min = arith.constant dense<0.0> : tensor + %max = arith.constant dense<15.0> : tensor + %mini = "tf.Identity"(%min) : (tensor) -> tensor + %maxi = "tf.Identity"(%max) : (tensor) -> tensor + %fq = "tf.FakeQuantWithMinMaxVars"(%in, %mini, %maxi) {num_bits = 3, narrow_range = false} : (tensor<3x3x3x16xf32>, tensor, tensor) -> tensor<3x3x3x16xf32> + %rst = "tf.DepthwiseConv2dNative"(%arg, %fq) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", dilations = [1, 2, 3, 1], padding = "SAME", strides = [1, 4, 5, 1]} : (tensor<256x32x32x3xf32>, tensor<3x3x3x16xf32>) -> tensor<256x30x30x16xf32> + func.return %rst : tensor<256x30x30x16xf32> + +// CHECK: %[[CONSTANT0:.*]] = "tf.Const"() <{value = dense<0.000000e+00> : tensor<3x3x3x16xf32>}> +// CHECK: %[[QUANTIZE:.*]] = "quantfork.qcast"(%[[CONSTANT0]]) : (tensor<3x3x3x16xf32>) -> tensor<3x3x3x16x!quant.uniform> +// CHECK: %[[DEQUANTIZE:.*]] = "quantfork.dcast"(%[[QUANTIZE]]) +// CHECK: %[[CONV:.*]] = "tf.DepthwiseConv2dNative"(%arg0, %[[DEQUANTIZE]]) +// CHECK: return %[[CONV]] +} + +// CHECK-LABEL: perChannelFakeQuantWithDepthwiseConv2D +func.func @perChannelFakeQuantWithDepthwiseConv2D(tensor<256x32x32x3xf32>) -> (tensor<256x30x30x16xf32>) { +^bb0(%arg: tensor<256x32x32x3xf32>) : + %in = arith.constant dense<0.0> : tensor<3x3x3x16xf32> + %min = arith.constant dense<0.0> : tensor<16xf32> + %max = arith.constant dense<15.0> : tensor<16xf32> + %mini = "tf.Identity"(%min) : (tensor<16xf32>) -> tensor<16xf32> + %maxi = "tf.Identity"(%max) : (tensor<16xf32>) -> tensor<16xf32> + %fq = "tf.FakeQuantWithMinMaxVarsPerChannel"(%in, %mini, %maxi) {num_bits = 3, narrow_range = false} : (tensor<3x3x3x16xf32>, tensor<16xf32>, tensor<16xf32>) -> tensor<3x3x3x16xf32> + %rst = "tf.DepthwiseConv2dNative"(%arg, %fq) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", dilations = [1, 2, 3, 1], padding = "SAME", strides = [1, 4, 5, 1]} : (tensor<256x32x32x3xf32>, tensor<3x3x3x16xf32>) -> tensor<256x30x30x16xf32> + func.return %rst : tensor<256x30x30x16xf32> + +// CHECK: %[[CONSTANT0:.*]] = "tf.Const"() <{value = dense<0.000000e+00> : tensor<3x3x3x16xf32>}> +// CHECK: %[[QUANTIZE:.*]] = "quantfork.qcast"(%[[CONSTANT0]]) : (tensor<3x3x3x16xf32>) -> tensor<3x3x3x16x!quant.uniform> +// CHECK: %[[DEQUANTIZE:.*]] = "quantfork.dcast"(%[[QUANTIZE]]) +// CHECK: %[[CONV:.*]] = "tf.DepthwiseConv2dNative"(%arg0, %[[DEQUANTIZE]]) +// CHECK: return %[[CONV]] +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tensorflow/tf_to_quant.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tensorflow/tf_to_quant.cc new file mode 100644 index 0000000000000000000000000000000000000000..40f14fd2c0c38cc16a1cffde14cc046d7b2692c0 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tensorflow/tf_to_quant.cc @@ -0,0 +1,193 @@ +/* Copyright 2020 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 "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Dialect/Quant/IR/Quant.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/DialectRegistry.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/Matchers.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/Types.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Support/TypeID.h" // from @llvm-project +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_utils.h" +#include "tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" + +namespace mlir { +namespace TF { + +//===----------------------------------------------------------------------===// +// The pass to legalize the quantization emulation ops from TF. +// +namespace { + +// Legalize TF quantization emulation ops to that in Quant ops dialect. +struct LegalizeTFToQuant + : public PassWrapper> { + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(LegalizeTFToQuant) + + explicit LegalizeTFToQuant() = default; + LegalizeTFToQuant(const LegalizeTFToQuant &) {} + + /// Performs the lowering to Quant ops dialect. + void runOnOperation() override; + + void getDependentDialects(DialectRegistry ®istry) const override { + registry.insert(); + } + + StringRef getArgument() const final { + // This is the argument used to refer to the pass in + // the textual format (on the commandline for example). + return "tf-to-quant"; + } + StringRef getDescription() const final { + // This is a brief description of the pass. + return "Legalize TF to quant ops dialect"; + } +}; + +// Inserts a "tfl.quantize" and "tfl.dequantize" op pair (QDQs) after the +// "tf.FakeQuantWithMinMaxVarsOp" to be constant folded. Since the constant +// folding logic will use a "arith.constant" op to replace the +// "tf.FakeQuantWithMinMaxVarsOp", the "tfl.quantize" op is used to preserve +// the quantization parameters as a TypeAttr and "tfl.dequantize" op used to +// convert the output type to the next op. Here are the transformations: +// +// input min cst max cst input min cst max cst +// \ | | \ | | +// \ (tf.Identity) (tf.Identity) => \ (tf.Identity) (tf.Identity) +// \ | | \ | | +// tf.FakeQuantWithMinMaxVars tf.FakeQuantWithMinMaxVars +// | | +// tf.quantize +// | +// tf.dequantize +// | +// If the input is a constant, the result pattern will eventually converted to +// +// quant-emulated input +// | +// tf.quantize +// | +// tf.dequantize +// | +template +struct InsertQuantOpsAfterTFFakeQuantOp + : public OpRewritePattern { + using BaseType = InsertQuantOpsAfterTFFakeQuantOp; + + explicit InsertQuantOpsAfterTFFakeQuantOp( + MLIRContext *ctx) + : OpRewritePattern(ctx) {} + + LogicalResult matchAndRewrite(TFFakeQuantOp tf_op, + PatternRewriter &rewriter) const override { + // We don't want to insert quantize/dequantize if the quantize op exists. + auto res = tf_op.getOutputs(); + if (!res.hasOneUse() || isa(*res.user_begin())) + return failure(); + + // Extract the min/max constant values from the operands. We also consider + // a special case that there are tf.Identity ops between the min/max + // constants and the tf.FakeQuantWithMinMaxVarsOp. + Value min = tf_op.getMin(), max = tf_op.getMax(); + DenseFPElementsAttr min_value, max_value; + if (auto id1 = dyn_cast_or_null(min.getDefiningOp())) { + id1.replaceAllUsesWith(id1.getInput()); + min = tf_op.getMin(); + rewriter.eraseOp(id1); + } + if (auto id2 = dyn_cast_or_null(max.getDefiningOp())) { + id2.replaceAllUsesWith(id2.getInput()); + max = tf_op.getMax(); + rewriter.eraseOp(id2); + } + if (!matchPattern(min, m_Constant(&min_value))) return failure(); + if (!matchPattern(max, m_Constant(&max_value))) return failure(); + + int quant_dim = -1; + if (PerAxis) { + // This is a special case that the quant_dim is the last dimensions + // according to the tf.FakeQuantWithMinMaxPerChannel. + quant_dim = mlir::cast(res.getType()).getRank() - 1; + } + // Use the min/max from the operands and the num_bits and narrow_range + // attribute to create the quantization parameter for the new quantize op. + rewriter.setInsertionPointAfter(tf_op.getOperation()); + IntegerAttr num_bits = rewriter.getI64IntegerAttr(tf_op.getNumBits()); + BoolAttr narrow_range = rewriter.getBoolAttr(tf_op.getNarrowRange()); + Type res_type = tf_op.getType(); + TypeAttr qtype = TFL::GetQuantizedTypeAttr( + rewriter, res_type, min_value, max_value, quant_dim, num_bits, + narrow_range, /*is_signed=*/true); + if (!qtype) return failure(); + + // Finally, use the quantization parameter to create the quantize and + // dequantize ops, and insert them between the tf.FakeQuantWithMinMaxVarsOp + // and its users. + Value value = tf_op.getOutputs(); + auto quantize = quantfork::QuantizeCastOp::create(rewriter, tf_op.getLoc(), + qtype.getValue(), value); + auto dequantize = quantfork::DequantizeCastOp::create( + rewriter, tf_op.getLoc(), res_type, quantize.getResult()); + value.replaceAllUsesWith(dequantize); + quantize.getOperation()->replaceUsesOfWith(dequantize, value); + + return success(); + } +}; + +using PreparePerTensorFakeQuant = + InsertQuantOpsAfterTFFakeQuantOp; + +using PreparePerChannelFakeQuant = + InsertQuantOpsAfterTFFakeQuantOp; + +// TODO(fengliuai): add the support of the tf.QuantizeAndDequantize* +// legalization. + +void LegalizeTFToQuant::runOnOperation() { + RewritePatternSet patterns(&getContext()); + auto func = getOperation(); + auto *ctx = func.getContext(); + patterns.add(ctx); + (void)applyPatternsGreedily(func, std::move(patterns)); +} +} // namespace + +// Creates an instance of the TensorFlow dialect to QuantOps dialect pass. +std::unique_ptr> CreateLegalizeTFToQuantPass() { + return std::make_unique(); +} + +static PassRegistration pass([] { + return CreateLegalizeTFToQuantPass(); +}); + +} // namespace TF +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tests/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tests/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..c91c2bd99fc7107afdee31cfc28a70a1197b4575 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tests/BUILD @@ -0,0 +1,24 @@ +load("//tensorflow:tensorflow.default.bzl", "filegroup") +load("//tensorflow/compiler/mlir:glob_lit_test.bzl", "glob_lit_tests") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + licenses = ["notice"], +) + +glob_lit_tests( + name = "all_tests", + data = [":test_utilities"], + driver = "@llvm-project//mlir:run_lit.sh", + test_file_exts = ["mlir"], +) + +# Bundle together all of the test utilities that are used by tests. +filegroup( + name = "test_utilities", + testonly = True, + data = [ + "//tensorflow/compiler/mlir/lite:litert-opt", + "@llvm-project//llvm:FileCheck", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tests/import_quant_stats.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tests/import_quant_stats.mlir new file mode 100644 index 0000000000000000000000000000000000000000..5bee5fa9ef15221d4232dc479c5d7e44086687be --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tests/import_quant_stats.mlir @@ -0,0 +1,47 @@ +// RUN: litert-opt %s -quant-import-stats --quant-test-stats='entries { name: "op" params { min_max { min: -1 max: 1 } } } entries { name: "op_0:0" params { min_max { min: -2 max: 2 } } } entries { name_regex: "op_*" params { min_max { min: -3 max: 3 } } }' | FileCheck %s + + +// CHECK-LABEL: import_stats_skip +func.func @import_stats_skip(%arg0: tensor<4xf32>, %cst: tensor) -> (tensor<2xf32>,tensor<2xf32>) { + %0:2 = "tfl.split"(%cst, %arg0) {num_splits = 2 : i32} : (tensor, tensor<4xf32>) -> (tensor<2xf32>, tensor<2xf32>) + loc(fused["skip1", "skip2.cc":10:8, callsite("op" at "skip3.cc":10:8)]) + func.return %0#0, %0#1 : tensor<2xf32>, tensor<2xf32> + +// CHECK-NEXT: "tfl.split" +// CHECK-NEXT: return +} + +// CHECK-LABEL: import_stats_name +func.func @import_stats_name(%arg0: tensor<4xf32>, %cst: tensor) -> (tensor<2xf32>,tensor<2xf32>) { + %0:2 = "tfl.split"(%cst, %arg0) {num_splits = 2 : i32} : (tensor, tensor<4xf32>) -> (tensor<2xf32>, tensor<2xf32>) + loc(fused["skip1.cc":10:8, "op", callsite("skip2" at "skip3.cc":10:8)]) + func.return %0#0, %0#1 : tensor<2xf32>, tensor<2xf32> + +// CHECK-NEXT: %[[split:.*]]:2 = "tfl.split" +// CHECK-NEXT: %[[stats1:.*]] = "quantfork.stats"(%[[split]]#0) <{layerStats = dense<[-1.000000e+00, 1.000000e+00]> +// CHECK-NEXT: %[[stats2:.*]] = "quantfork.stats"(%[[split]]#1) <{layerStats = dense<[-1.000000e+00, 1.000000e+00]> +// CHECK-NEXT: return %[[stats1]], %[[stats2]] : tensor<2xf32>, tensor<2xf32> +} + +// CHECK-LABEL: import_stats_name_port +func.func @import_stats_name_port(%arg0: tensor<4xf32>, %cst: tensor) -> (tensor<2xf32>,tensor<2xf32>) { + %0:2 = "tfl.split"(%cst, %arg0) {num_splits = 2 : i32} : (tensor, tensor<4xf32>) -> (tensor<2xf32>, tensor<2xf32>) + loc(fused["skip1.cc":10:8, "op_0", callsite("skip2" at "skip3.cc":10:8)]) + func.return %0#0, %0#1 : tensor<2xf32>, tensor<2xf32> + +// CHECK-NEXT: %[[split:.*]]:2 = "tfl.split" +// CHECK-NEXT: %[[stats1:.*]] = "quantfork.stats"(%[[split]]#0) <{layerStats = dense<[-2.000000e+00, 2.000000e+00]> +// CHECK-NEXT: return %[[stats1]], %[[split]]#1 : tensor<2xf32>, tensor<2xf32> +} + +// CHECK-LABEL: import_stats_name_regex +func.func @import_stats_name_regex(%arg0: tensor<4xf32>, %cst: tensor) -> (tensor<2xf32>,tensor<2xf32>) { + %0:2 = "tfl.split"(%cst, %arg0) {num_splits = 2 : i32, name = "op_regex"} : (tensor, tensor<4xf32>) -> (tensor<2xf32>, tensor<2xf32>) + loc(fused["skip1.cc":10:8, "op_regex", callsite("skip2" at "skip3.cc":10:8)]) + func.return %0#0, %0#1 : tensor<2xf32>, tensor<2xf32> + +// CHECK-NEXT: %[[split:.*]]:2 = "tfl.split" +// CHECK-NEXT: %[[stats1:.*]] = "quantfork.stats"(%[[split]]#0) <{layerStats = dense<[-3.000000e+00, 3.000000e+00]> +// CHECK-NEXT: %[[stats2:.*]] = "quantfork.stats"(%[[split]]#1) <{layerStats = dense<[-3.000000e+00, 3.000000e+00]> +// CHECK-NEXT: return %[[stats1]], %[[stats2]] : tensor<2xf32>, tensor<2xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tools/op_quant_spec_getters_gen.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tools/op_quant_spec_getters_gen.cc new file mode 100644 index 0000000000000000000000000000000000000000..fd4fed6959e529dc99046c3177189b36dbcdffe9 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tools/op_quant_spec_getters_gen.cc @@ -0,0 +1,120 @@ +/* Copyright 2019 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 "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/InitLLVM.h" +#include "llvm/Support/PrettyStackTrace.h" +#include "llvm/Support/Regex.h" +#include "llvm/TableGen/Main.h" +#include "llvm/TableGen/Record.h" +#include "llvm/TableGen/TableGenBackend.h" +#include "mlir/TableGen/Operator.h" // from @llvm-project +#include "mlir/TableGen/Trait.h" // from @llvm-project + +using llvm::LessRecord; +using llvm::raw_ostream; +using llvm::Record; +using llvm::RecordKeeper; +using mlir::tblgen::Operator; + +// Helper macro that returns indented os. +#define OUT(X) os.indent((X)) + +// The function below has a non-constant reference as that is required by LLVM's +// TableGenMain. +// NOLINTNEXTLINE +static bool OpQuantSpecWriter(raw_ostream &os, const RecordKeeper &records) { + llvm::Regex acc_uniform_trait_regex{"AccumulatorUniformScale<([0-9]*),"}; + llvm::Regex coeff_index_trait_regex{"AffineOpCoefficient<(-?[0-9]*),"}; + llvm::Regex fixed_uniform_trait_regex{ + "FixedResultUniformScale<([0-9]+).*(true|false)>"}; + emitSourceFileHeader("Generated Ops Quant Spec Getters", os); + + // Retrieve all the definitions derived from Op definition and sort by record + // name. + std::vector defs = records.getAllDerivedDefinitions("Op"); + llvm::sort(defs, LessRecord()); + + OUT(0) << "static std::unique_ptr " + "GetOpQuantSpec(mlir::Operation *op, bool " + "disable_per_channel_for_dense_layers = false) {\n"; + // TODO(b/176258587): Move to OpTrait if this should be generalized. + // Add special handling for LSTM. + OUT(2) << "if (auto lstm_op = llvm::dyn_cast(op)) {\n"; + OUT(4) << "return GetLstmOpQuantSpec(lstm_op);\n"; + OUT(2) << "} else if (auto lstm_op = " + "llvm::dyn_cast(op)) {\n"; + OUT(4) << "return " + "GetLstmOpQuantSpec(lstm_op);\n"; + OUT(2) << "}\n"; + + OUT(2) << "auto spec = std::make_unique();\n"; + llvm::SmallVector matches; + for (auto *def : defs) { + Operator op(def); + for (const auto t : op.getTraits()) { + if (auto opTrait = llvm::dyn_cast(&t)) { + auto trait_str = opTrait->getFullyQualifiedTraitName(); + if (!llvm::StringRef{trait_str}.consume_front("::mlir::OpTrait::TFL::")) + continue; + + OUT(2) << "if (auto tfl = llvm::dyn_cast<" << op.getQualCppClassName() + << ">(op)) {\n"; + // There is a "FixedResultUniformScale" trait, set the type for result. + if (fixed_uniform_trait_regex.match(trait_str, &matches)) { + OUT(4) << "for (int i = 0, e = op->getNumResults(); i != e; ++i)\n"; + OUT(6) << "spec->restricted_output_params[std::make_pair(" + << matches[1] << ", " << matches[2] + << ")].push_back(tfl.::mlir::OpTrait::TFL::" << trait_str + << "<" << op.getQualCppClassName() + << ">::GetResultQuantizedType(i));\n"; + matches.clear(); + } + // There is a "AccumulatorUniformScale" trait, set the type for bias. + if (acc_uniform_trait_regex.match(trait_str, &matches)) { + OUT(4) << "spec->biases_params.emplace(std::make_pair(" << matches[1] + << ", std::make_pair(tfl.GetAllNonBiasOperands()," + << "GetUniformQuantizedTypeForBias)));\n"; + matches.clear(); + } + // There is a "QuantChannelDim" trait, set the quantization dimension. + if (coeff_index_trait_regex.match(trait_str, &matches)) { + OUT(4) << "spec->coeff_op_quant_dim[tfl.GetCoefficientOperandIndex()" + << "] = llvm::dyn_cast(op) && " + "disable_per_channel_for_dense_layers ? -1 : " + "tfl.GetQuantizationDim();\n"; + matches.clear(); + } + + OUT(2) << "}\n"; + } + } + } + OUT(2) << "return spec;\n"; + OUT(0) << "}\n"; + return false; +} + +int main(int argc, char **argv) { + llvm::InitLLVM y(argc, argv); + llvm::cl::ParseCommandLineOptions(argc, argv); + return TableGenMain(argv[0], &OpQuantSpecWriter); +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tools/tflite_op_coverage_spec_getters_gen.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tools/tflite_op_coverage_spec_getters_gen.cc new file mode 100644 index 0000000000000000000000000000000000000000..efe4892e79327cc85f90e4d87a41ac4c130bf354 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/quantization/tools/tflite_op_coverage_spec_getters_gen.cc @@ -0,0 +1,383 @@ +/* Copyright 2022 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 "absl/strings/match.h" +#include "absl/strings/str_replace.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/InitLLVM.h" +#include "llvm/TableGen/Main.h" +#include "llvm/TableGen/Record.h" +#include "mlir/TableGen/Operator.h" // from @llvm-project +#include "tsl/platform/regexp.h" + +using llvm::LessRecord; +using llvm::raw_ostream; +using llvm::Record; +using llvm::RecordKeeper; +using mlir::tblgen::Operator; + +enum class InputDataType { INT8, UINT8, INT16 }; + +// One InputDataType will be likely mapped to multiple types in near future so +// the two structures are separated. +const std::map &GetTypeToStringRepresentation() { + static auto *entries = new std::map({ + {"F32", "32-bit float"}, + {"I32", "32-bit signless integer"}, + {"I64", "64-bit signless integer"}, + {"QI16", "QI16 type"}, + {"I8", "8-bit signless integer"}, + {"UI8", "8-bit unsigned integer"}, + {"QI8", "QI8 type"}, + {"QUI8", "QUI8 type"}, + {"TFL_Quint8", "TFLite quint8 type"}, + }); + + return *entries; +} + +void EmitDynamicRangeOp(std::vector &defs, + raw_ostream *ostream) { + std::string dynamic_quant_kernel_support_regex = + "bool GetDynamicRangeQuantKernelSupport() { return true; }"; + raw_ostream &os = *ostream; + std::vector weight_only; + llvm::sort(defs, LessRecord()); + + os.indent(0) << "const std::set &ExportDynamicRangeSpec() {\n"; + os.indent(2) << "static const std::set * result =\n"; + os.indent(4) << "new std::set({\n"; + + // Retrieve all the ops that have DynamicRangeQuantizedOpInterface trait. + for (const auto *def : defs) { + Operator op(def); + if (!op.getTrait("DynamicRangeQuantizedOpInterface::Trait")) continue; + + auto op_name = op.getCppClassName(); + auto op_extra_declaration = op.getExtraClassDeclaration().str(); + + bool kernel_support = absl::StrContains( + absl::StrReplaceAll(op_extra_declaration, {{"\n", " "}}), + dynamic_quant_kernel_support_regex); + + // Classify dynamic range and weight-only fallback + if (kernel_support) { + os.indent(6) << "\"" << op_name << "\",\n"; + } else { + weight_only.push_back(op_name.str()); + } + } + + os.indent(4) << "});"; + os.indent(2) << "return *result;\n"; + os.indent(0) << "}\n"; + + os.indent(0) + << "const std::set &ExportDynamicRangeWeightOnlySpec() {\n"; + os.indent(2) << "static const std::set * result =\n"; + os.indent(4) << "new std::set({\n"; + + // Retrieve weight-only fallback. + for (const auto &op_name : weight_only) { + os.indent(6) << "\"" << op_name << "\",\n"; + } + + os.indent(4) << "});"; + os.indent(2) << "return *result;\n"; + os.indent(0) << "}\n"; +} + +void EmitSparseOp(std::vector &defs, raw_ostream *ostream) { + raw_ostream &os = *ostream; + llvm::sort(defs, LessRecord()); + + os.indent(0) << "const std::set &ExportSparsitySpec() {\n"; + os.indent(2) << "static const std::set * result =\n"; + os.indent(4) << "new std::set({\n"; + + // Retrieve all the ops that have SparseOp trait. + for (const auto *def : defs) { + Operator op(def); + if (!op.getTrait("SparseOpInterface::Trait")) { + continue; + } + os.indent(6) << "\"" << op.getCppClassName() << "\",\n"; + } + + os.indent(4) << "});"; + os.indent(2) << "return *result;\n"; + os.indent(0) << "}\n"; +} + +bool CheckTypeConstraints(const llvm::Init *input_value, + std::list required_types, + bool per_axis) { + auto *def_init = llvm::cast(input_value); + auto *val = def_init->getDef()->getValue("tflRuntimeTypePredicate"); + + // For non-per-axis op, no predicate means accepting AnyTensor. + if (!val) return !per_axis; + + llvm::StringRef supported_types = + def_init->getDef()->getValueAsString("tflRuntimeTypeDescription"); + + for (const std::string &type : required_types) { + if (!absl::StrContains(supported_types.str(), type)) return false; + } + return true; +} + +void GenerateStaticQuantOp(std::vector &defs, + std::vector &result, + InputDataType act_type, const bool per_axis, + const bool is_toco) { + std::list required_types = { + GetTypeToStringRepresentation().at("F32")}; + + switch (act_type) { + case InputDataType::INT8: { + required_types.push_back(GetTypeToStringRepresentation().at("QI8")); + break; + } + case InputDataType::UINT8: { + required_types.push_back(GetTypeToStringRepresentation().at("QUI8")); + break; + } + case InputDataType::INT16: { + required_types.push_back(GetTypeToStringRepresentation().at("QI16")); + break; + } + default: { + // Quantization not applied. + return; + } + } + + // Dimension equals to -1 means per-channel quantization is not supported for + // the op. Therefore check whether the return value is positive integer as + // well. + static const LazyRE2 per_channel_support_regex = { + "int GetQuantizationDimIndex\\(\\) \\{ return (\\d*); \\}"}; + + for (const auto *def : defs) { + Operator op(def); + if (!op.getTrait("::mlir::OpTrait::TFL::QuantizableResult")) continue; + + const llvm::DagInit *args_in_dag = def->getValueAsDag("arguments"); + // Assumes argument name is "input" for input activations. Otherwise, assume + // the first argument is the input activation. + int input_idx = 0; + for (int i = 0; i < args_in_dag->getNumArgs(); i++) { + if (args_in_dag->getArgName(i)->getAsString() == "\"input\"") + input_idx = i; + } + if (CheckTypeConstraints(args_in_dag->getArg(input_idx), required_types, + per_axis)) { + std::string op_name = op.getCppClassName().str(); + + // TODO(b/197195711): Please add the additional operations for 16x8 MLIR + // quantizer. This code is temporary until 16x8 is fully supported in MLIR + // quantizer. + if (act_type == InputDataType::INT16) { + if (is_toco) { + // Conditions when using TOCO. + if (absl::StrContains(op_name, "LSTMOp")) continue; + } else { + // Conditions when using MLIR. + if (!(absl::StrContains(op_name, "LSTMOp") || + absl::StrContains(op_name, "SoftmaxOp") || + absl::StrContains(op_name, "LogisticOp") || + absl::StrContains(op_name, "L2NormalizationOp") || + absl::StrContains(op_name, "TanhOp"))) { + continue; + } + } + } + + if (per_axis) { + std::string op_extra_declaration = op.getExtraClassDeclaration().str(); + bool per_axis_support = RE2::PartialMatch( + absl::StrReplaceAll(op_extra_declaration, {{"\n", " "}}), + *per_channel_support_regex); + if (per_axis_support) result.emplace_back(op_name); + } else { + result.emplace_back(op_name); + } + } + } +} + +void EmitStaticInt8PerAxisQuantOp(std::vector &defs, + raw_ostream &os) { + os.indent(0) + << "const std::set &ExportStaticInt8PerAxisSpec() {\n"; + os.indent(2) << "static const std::set * result =\n"; + os.indent(4) << "new std::set({\n"; + + std::vector result; + GenerateStaticQuantOp(defs, result, InputDataType::INT8, /*per_axis=*/true, + /*is_toco=*/false); + + for (const auto &op_name : result) { + os.indent(6) << "\"" << op_name << "\",\n"; + } + + os.indent(4) << "});"; + os.indent(2) << "return *result;\n"; + os.indent(0) << "}\n"; +} + +void EmitStaticInt8PerTensorQuantOp(std::vector &defs, + raw_ostream &os) { + os.indent(0) + << "const std::set &ExportStaticInt8PerTensorSpec() {\n"; + os.indent(2) << "static const std::set * result =\n"; + os.indent(4) << "new std::set({\n"; + + std::vector result; + GenerateStaticQuantOp(defs, result, InputDataType::INT8, /*per_axis=*/false, + /*is_toco=*/false); + + for (const auto &op_name : result) { + os.indent(6) << "\"" << op_name << "\",\n"; + } + + os.indent(4) << "});"; + os.indent(2) << "return *result;\n"; + os.indent(0) << "}\n"; +} + +void EmitStaticUInt8PerAxisQuantOp(std::vector &defs, + raw_ostream &os) { + os.indent(0) + << "const std::set &ExportStaticUInt8PerAxisSpec() {\n"; + os.indent(2) << "static const std::set * result =\n"; + os.indent(4) << "new std::set({\n"; + + std::vector result; + GenerateStaticQuantOp(defs, result, InputDataType::UINT8, /*per_axis=*/true, + /*is_toco=*/false); + + for (const auto &op_name : result) { + os.indent(6) << "\"" << op_name << "\",\n"; + } + + os.indent(4) << "});"; + os.indent(2) << "return *result;\n"; + os.indent(0) << "}\n"; +} + +void EmitStaticUInt8PerTensorQuantOp(std::vector &defs, + raw_ostream &os) { + os.indent(0) + << "const std::set &ExportStaticUInt8PerTensorSpec() {\n"; + os.indent(2) << "static const std::set * result =\n"; + os.indent(4) << "new std::set({\n"; + + std::vector result; + GenerateStaticQuantOp(defs, result, InputDataType::UINT8, /*per_axis=*/false, + /*is_toco=*/false); + + for (const auto &op_name : result) { + os.indent(6) << "\"" << op_name << "\",\n"; + } + + os.indent(4) << "});"; + os.indent(2) << "return *result;\n"; + os.indent(0) << "}\n"; +} + +void EmitStaticQuantOp(std::vector &defs, + raw_ostream *ostream) { + raw_ostream &os = *ostream; + llvm::sort(defs, LessRecord()); + + EmitStaticInt8PerAxisQuantOp(defs, os); + EmitStaticInt8PerTensorQuantOp(defs, os); + EmitStaticUInt8PerAxisQuantOp(defs, os); + EmitStaticUInt8PerTensorQuantOp(defs, os); +} + +void EmitStaticQuantWithInt16ActOp(std::vector &defs, + raw_ostream *ostream) { + raw_ostream &os = *ostream; + llvm::sort(defs, LessRecord()); + + os.indent(0) + << "const std::set &ExportStaticInt8WithInt16ActSpec() {\n"; + os.indent(2) << "static const std::set * result =\n"; + os.indent(4) << "new std::set({\n"; + + std::vector result; + GenerateStaticQuantOp(defs, result, InputDataType::INT16, /*per_axis=*/false, + /*is_toco=*/false); + + for (const auto &op_name : result) { + os.indent(6) << "\"" << op_name << "\",\n"; + } + + os.indent(4) << "});"; + os.indent(2) << "return *result;\n"; + os.indent(0) << "}\n"; +} + +void EmitStaticQuantWithInt16ActTocoOp(std::vector &defs, + raw_ostream *ostream) { + raw_ostream &os = *ostream; + llvm::sort(defs, LessRecord()); + + os.indent(0) << "const std::set " + "&ExportStaticInt8WithInt16ActTocoSpec() {\n"; + os.indent(2) << "static const std::set * result =\n"; + os.indent(4) << "new std::set({\n"; + + std::vector result; + GenerateStaticQuantOp(defs, result, InputDataType::INT16, /*per_axis=*/false, + /*is_toco=*/true); + + for (const auto &op_name : result) { + os.indent(6) << "\"" << op_name << "\",\n"; + } + + os.indent(4) << "});"; + os.indent(2) << "return *result;\n"; + os.indent(0) << "}\n"; +} + +static bool TFLiteOpCoverageSpecWritersMain(raw_ostream &os, + const RecordKeeper &records) { + std::vector op_defs = + records.getAllDerivedDefinitions("TFL_Op"); + EmitStaticQuantOp(op_defs, &os); + EmitDynamicRangeOp(op_defs, &os); + EmitStaticQuantWithInt16ActOp(op_defs, &os); + EmitStaticQuantWithInt16ActTocoOp(op_defs, &os); + EmitSparseOp(op_defs, &os); + return false; +} + +int main(int argc, char **argv) { + llvm::InitLLVM y(argc, argv); + llvm::cl::ParseCommandLineOptions(argc, argv); + return TableGenMain(argv[0], &TFLiteOpCoverageSpecWritersMain); +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/register_lite_dialects.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/register_lite_dialects.cc new file mode 100644 index 0000000000000000000000000000000000000000..dbb03f835ce4bda15335b24d84c884ee220f1c74 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/register_lite_dialects.cc @@ -0,0 +1,28 @@ +/* Copyright 2025 Google Inc. 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/compiler/mlir/lite/register_lite_dialects.h" + +#include "mlir/IR/DialectRegistry.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/lite/quantization/ir/QuantOps.h" + +namespace tflite { + +void RegisterLiteToolingDialects(mlir::DialectRegistry& registry) { + registry.insert(); + registry.insert(); +} + +}; // namespace tflite diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/register_lite_dialects.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/register_lite_dialects.h new file mode 100644 index 0000000000000000000000000000000000000000..3f52ad0b7b93ba2bb7efb619923b37228f0634b0 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/register_lite_dialects.h @@ -0,0 +1,28 @@ +/* Copyright 2025 Google Inc. 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_REGISTER_LITE_DIALECTS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_REGISTER_LITE_DIALECTS_H_ + +#include "mlir/IR/DialectRegistry.h" // from @llvm-project + +namespace tflite { + +// Inserts LiteRT dialects used for offline tools. +void RegisterLiteToolingDialects(mlir::DialectRegistry& registry); + +}; // namespace tflite + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_REGISTER_LITE_DIALECTS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/register_lite_dialects_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/register_lite_dialects_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..638496c6e77beb410b8781e75688f11ddbb9a7ad --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/register_lite_dialects_test.cc @@ -0,0 +1,33 @@ +/* Copyright 2025 Google Inc. 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/compiler/mlir/lite/register_lite_dialects.h" + +#include +#include "mlir/IR/DialectRegistry.h" // from @llvm-project + +namespace tflite { +namespace { + +TEST(RegisterCommonDialectsTest, DoesntCrash) { + mlir::DialectRegistry registry; + + tflite::RegisterLiteToolingDialects(registry); + + EXPECT_FALSE(registry.getRegisteredDialectNames().empty()); +} + +} // namespace +} // namespace tflite diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/runlit.cfg.py b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/runlit.cfg.py new file mode 100644 index 0000000000000000000000000000000000000000..605cf0ef33b34c6249ec05feff476a2efd1f1eff --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/runlit.cfg.py @@ -0,0 +1,102 @@ +# Copyright 2019 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. +"""Lit runner configuration.""" + +import os +import platform +import sys +import lit.formats +from lit.llvm import llvm_config +from lit.llvm.subst import ToolSubst + +# Lint for undefined variables is disabled as config is not defined inside this +# file, instead config is injected by way of evaluating runlit.cfg.py from +# runlit.site.cfg.py which in turn is evaluated by lit.py. The structure is +# common for lit tests and intended to only persist temporarily (b/136126535). +# pylint: disable=undefined-variable +# Configuration file for the 'lit' test runner. + +# name: The name of this test suite. +config.name = 'MLIR ' + os.path.basename(config.mlir_test_dir) + +config.test_format = lit.formats.ShTest() + +# suffixes: A list of file extensions to treat as test files. +config.suffixes = ['.cc', '.hlo', '.json', '.mlir', '.pbtxt', '.py'] + +# test_source_root: The root path where tests are located. +config.test_source_root = config.mlir_test_dir + +# test_exec_root: The root path where tests should be run. +config.test_exec_root = os.environ['RUNFILES_DIR'] + +if platform.system() == 'Windows': + tool_patterns = [ + ToolSubst('FileCheck.exe', unresolved='fatal'), + # Handle these specially as they are strings searched for during testing. + ToolSubst('count.exe', unresolved='fatal'), + ToolSubst('not.exe', unresolved='fatal') + ] + + llvm_config.config.substitutions.append( + ('%python', '"%s"' % (sys.executable))) + + llvm_config.add_tool_substitutions(tool_patterns, + [llvm_config.config.llvm_tools_dir]) +else: + llvm_config.use_default_substitutions() + +llvm_config.config.substitutions.append( + ('%tfrt_bindir', 'third_party/tensorflow/compiler/aot')) + +# Tweak the PATH to include the tools dir. +llvm_config.with_environment('PATH', config.llvm_tools_dir, append_path=True) + +tool_dirs = config.mlir_tf_tools_dirs + [ + config.mlir_tools_dir, config.llvm_tools_dir +] +tool_names = [ + 'dtensor-opt', + 'flatbuffer_to_string', + 'flatbuffer_translate', + 'hlo_to_kernel', + 'json_to_flatbuffer', + 'kernel-gen-opt', + 'litert-opt', + 'mlir-hlo-opt', + 'mlir-opt', + 'mlir-tflite-runner', + 'mlir-translate', + 'odml-to-stablehlo-opt', + 'odml_to_stablehlo', + 'odml-converter', + 'stable-quant-opt', + 'tac-opt-all-backends', + 'tac-translate', + 'tf-mlir-translate', + 'tf-opt', + 'tf-quant-opt', + 'tf-reduce', + 'tf-tfrt-opt', + 'tf_tfjs_translate', + 'tf_tfl_translate', + 'tfcompile', + 'tfg-opt-no-passes', + 'tfg-transforms-opt', + 'tfg-translate', + 'tfjs-opt', +] +tools = [ToolSubst(s, unresolved='ignore') for s in tool_names] +llvm_config.add_tool_substitutions(tools, tool_dirs) +# pylint: enable=undefined-variable diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/runlit.site.cfg.py b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/runlit.site.cfg.py new file mode 100644 index 0000000000000000000000000000000000000000..7bae92ae29046ed41d79486759a7c15b7f848825 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/runlit.site.cfg.py @@ -0,0 +1,77 @@ +# Copyright 2019 Google Inc. 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. +"""Lit runner site configuration.""" + +import os +import platform +import lit.llvm + +# Handle the test srcdir for platforms. On windows, things are weird with bazel. +if platform.system() == 'Windows': + srcdir = os.environ['TEST_SRCDIR'] + real_test_srcdir = srcdir[:srcdir.find('tflite/converter')] + external_srcdir = os.path.join(real_test_srcdir, 'external') +else: + real_test_srcdir = os.environ['TEST_SRCDIR'] + external_srcdir = real_test_srcdir + +# Lint for undefined variables is disabled as config is not defined inside this +# file, instead config is injected by lit.py. The structure is common for lit +# tests and intended to only persist temporarily (b/136126535). +# pylint: disable=undefined-variable +config.llvm_tools_dir = os.path.join(external_srcdir, 'llvm-project', 'llvm') +config.mlir_obj_root = os.path.join(real_test_srcdir) +config.mlir_tools_dir = os.path.join(external_srcdir, 'llvm-project', 'mlir') +# TODO(jpienaar): Replace with suffices in build rule. +config.suffixes = ['.td', '.mlir', '.pbtxt'] + +mlir_tf_tools_dirs = [ + 'third_party/tensorflow/compiler/aot', + 'third_party/tensorflow/compiler/mlir', + 'tflite/converter', + 'tflite/converter/experimental/tac', + 'tflite/converter/stablehlo', + 'tflite/converter/stablehlo/odml_converter', + 'third_party/tensorflow/compiler/mlir/quantization/tensorflow', + 'third_party/tensorflow/compiler/mlir/tensorflow', + 'third_party/tensorflow/compiler/mlir/tfrt', + 'third_party/tensorflow/compiler/mlir/tools/kernel_gen', + os.path.join(external_srcdir, 'xla/xla/mlir_hlo'), + 'third_party/tensorflow/core/ir/importexport/', + 'third_party/tensorflow/core/ir/tests/', + 'third_party/tensorflow/core/transforms/', + 'third_party/tensorflow/dtensor/mlir/tests', +] +config.mlir_tf_tools_dirs = [ + os.path.join(real_test_srcdir, os.environ['TEST_WORKSPACE'], s) + for s in mlir_tf_tools_dirs +] +test_dir = os.environ['TEST_TARGET'] +test_dir = test_dir.strip('/').rsplit(':', 1)[0] +config.mlir_test_dir = os.path.join(real_test_srcdir, + os.environ['TEST_WORKSPACE'], test_dir) + +if platform.system() == 'Windows': + # Configure this to work with msys2, TF's preferred windows bash. + config.lit_tools_dir = '/usr/bin' + +lit.llvm.initialize(lit_config, config) + +# Let the main config do the real work. +lit_config.load_config( + config, + os.path.join( + os.path.join(real_test_srcdir, os.environ['TEST_WORKSPACE'], + 'tflite/converter/runlit.cfg.py'))) +# pylint: enable=undefined-variable diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..67b4a9bae62fd45616fb788cddcfa107479e3ea0 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/BUILD @@ -0,0 +1,162 @@ +load("@flatbuffers//:build_defs.bzl", "flatbuffer_cc_library") +load("@rules_cc//cc:cc_library.bzl", "cc_library") + +# copybara:uncomment load("@flatbuffers//:flatbuffers.bzl", "flatbuffers_library", "ts_flatbuffers_library") +load("//tensorflow:tensorflow.bzl", "tf_cc_test") +load("//tensorflow:tensorflow.default.bzl", "get_compatible_with_portable") +# copybara:uncomment load("//tools/build_defs/cc:cc_embed_data.bzl", "cc_embed_data") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + default_visibility = [ + "//visibility:public", + ], + licenses = ["notice"], +) + +exports_files( + srcs = [ + "conversion_metadata.fbs", + "debug_metadata.fbs", + "schema.fbs", + "schema_v0.fbs", + "schema_v1.fbs", + "schema_v2.fbs", + "schema_v3.fbs", + "schema_v3a.fbs", + "schema_v3b.fbs", + "schema_v3c.fbs", + ], +) + +filegroup( + name = "tflite_internal_cc_3p_api_deps_src", + srcs = [ + ":debug_metadata_fbs_srcs", + ":schema_fbs_srcs", + ":schema_utils.h", + ], + visibility = ["//tensorflow/lite:__pkg__"], +) + +flatbuffer_cc_library( + name = "schema_fbs", + srcs = ["schema.fbs"], + compatible_with = get_compatible_with_portable(), + gen_reflections = True, +) + +# copybara:uncomment_begin(google-only) +# cc_embed_data( +# name = "schema_reflection_data", +# srcs = ["schema.bfbs"], +# outs = [ +# "schema_reflection_data.cc", +# "schema_reflection_data.h", +# "schema_reflection_data.o", +# ], +# embedopts = ["--namespace=tflite"], +# ) +# copybara:uncomment_end + +# Generic schema for flatbuffer converter (but with mutable makes bigger). +flatbuffer_cc_library( + name = "schema_fbs_with_mutable", + srcs = ["schema.fbs"], + compatible_with = get_compatible_with_portable(), + flatc_args = [ + "--gen-mutable", + "--gen-object-api", + ], + out_prefix = "mutable/", +) + +flatbuffer_cc_library( + name = "debug_metadata_fbs", + srcs = ["debug_metadata.fbs"], + compatible_with = get_compatible_with_portable(), +) + +flatbuffer_cc_library( + name = "debug_metadata_fbs_with_mutable", + srcs = ["debug_metadata.fbs"], + compatible_with = get_compatible_with_portable(), + flatc_args = [ + "--gen-mutable", + "--gen-object-api", + ], + out_prefix = "mutable/", +) + +# Generic schema for inference on device (but with reflections makes bigger). +flatbuffer_cc_library( + name = "schema_fbs_with_reflection", + srcs = ["schema.fbs"], + compatible_with = get_compatible_with_portable(), + flatc_args = [ + "--reflect-types", + "--reflect-names", + "--no-union-value-namespacing", + "--gen-object-api", + ], + out_prefix = "reflection/", +) + +cc_library( + name = "schema_utils", + srcs = ["schema_utils.cc"], + hdrs = ["schema_utils.h"], + compatible_with = get_compatible_with_portable(), + # visibility = [":utils_friends"], + deps = [ + ":schema_fbs", + "//tensorflow/compiler/mlir/lite/kernels/internal:compatibility_macros", + "@flatbuffers//:runtime_cc", + ], +) + +cc_library( + name = "schema_conversion_utils", + srcs = ["schema_conversion_utils.cc"], + hdrs = ["schema_conversion_utils.h"], + compatible_with = get_compatible_with_portable(), + # visibility = [":utils_friends"], + deps = [ + ":schema_fbs", + "@flatbuffers", + ], +) + +# Schema test to make sure we don't introduce backward incompatible changes +# to schemas. +tf_cc_test( + name = "flatbuffer_compatibility_test", + size = "small", + srcs = ["flatbuffer_compatibility_test.cc"], + data = [ + "schema.fbs", + "schema_v3c.fbs", + ], + tags = [ + "no_oss", + "tflite_not_portable_android", + "tflite_not_portable_ios", + ], + deps = [ + "//tensorflow/core/platform", + "@com_google_googletest//:gtest_main", + "@flatbuffers//:flatc_library", + ], +) + +# copybara:uncomment_begin(google-only) +# flatbuffers_library( +# name = "schema_fbslib", +# srcs = ["schema.fbs"], +# ) +# +# ts_flatbuffers_library( +# name = "schema_ts_fbs", +# deps = [":schema_fbslib"], +# ) +# copybara:uncomment_end diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/README.md b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/README.md new file mode 100644 index 0000000000000000000000000000000000000000..8ee03eaf32963c43321454d9fcef3d800d52a04c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/README.md @@ -0,0 +1,2 @@ +This directory contains schema related files and targets that are used by both +the TFL converter (tf/compiler/mlir/lite/) and the runtime (tf/lite/). \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/conversion_metadata.fbs b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/conversion_metadata.fbs new file mode 100644 index 0000000000000000000000000000000000000000..2fcdcdf49c1c86de30f25874893a75f2e36654a1 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/conversion_metadata.fbs @@ -0,0 +1,94 @@ +// Copyright 2021 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. + +namespace tflite; + +// The original model format. +enum ModelType : int32 { + NONE = 0, + TF_SAVED_MODEL = 1, + KERAS_MODEL = 2, + TF_CONCRETE_FUNCTIONS = 3, + TF_GRAPH_DEF = 4, + TF_SESSION = 5, + JAX = 6, + PYTORCH = 7 +} + +// The environment during conversion. +table Environment { + // TensorFlow version string, e.g., "2.4.1". + tensorflow_version:string; + + // TensorFlow Lite Converter API used. + api_version:uint; + + // Original model format. + model_type:ModelType; + + // Model identification hash. + model_hash:uint64; + +} + +// The model optimization modes. +// When adding a new optimization mode, it must be canonical with the existing +// ones. The thousand part of the value represents the optimization technique +// and the later part is to distinguish different modes of that technique. +enum ModelOptimizationMode : int32 { + // 0 is reserved as a placeholder + + PTQ_FLOAT16 = 1001, + PTQ_DYNAMIC_RANGE = 1002, + PTQ_FULL_INTEGER = 1003, + PTQ_INT16 = 1004, + QUANTIZATION_AWARE_TRAINING = 2000, + RANDOM_SPARSITY = 3001, + BLOCK_SPARSITY = 3002, + STRUCTURED_SPARSITY = 3003, +} + +// The block size used for sparsity. +table SparsityBlockSize { + values:[uint]; +} + +table ConversionOptions { + // Applied the optimization techniques. + model_optimization_modes:[ModelOptimizationMode]; + + // Boolean indicating whether to allow custom ops. + allow_custom_ops:bool; + + // Boolean indicating whether to convert unsupported ops to select + // TensorFlow ops. + enable_select_tf_ops:bool; + + // Boolean indicating whether to convert all TensorFlow ops to select + // TensorFlow ops. + force_select_tf_ops:bool; + + // The block size used for sparsity. + sparsity_block_sizes:[SparsityBlockSize]; +} + +table ConversionMetadata { + // Metadata about the environment during conversion. + environment:Environment; + + // Conversion options used. + options:ConversionOptions; +} + +root_type ConversionMetadata; diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/conversion_metadata_generated.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/conversion_metadata_generated.h new file mode 100644 index 0000000000000000000000000000000000000000..3456964afdb4f5ce35ac530ce15390b319b50bf0 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/conversion_metadata_generated.h @@ -0,0 +1,672 @@ +/* Copyright 2021 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. +==============================================================================*/ +// automatically generated by the FlatBuffers compiler, do not modify + + +#ifndef FLATBUFFERS_GENERATED_CONVERSIONMETADATA_TFLITE_H_ +#define FLATBUFFERS_GENERATED_CONVERSIONMETADATA_TFLITE_H_ + +#include "flatbuffers/flatbuffers.h" + +// Ensure the included flatbuffers.h is the same version as when this file was +// generated, otherwise it may not be compatible. +static_assert(FLATBUFFERS_VERSION_MAJOR == 25 && + FLATBUFFERS_VERSION_MINOR == 9 && + FLATBUFFERS_VERSION_REVISION == 23, + "Non-compatible flatbuffers version included"); + +namespace tflite { + +struct Environment; +struct EnvironmentBuilder; +struct EnvironmentT; + +struct SparsityBlockSize; +struct SparsityBlockSizeBuilder; +struct SparsityBlockSizeT; + +struct ConversionOptions; +struct ConversionOptionsBuilder; +struct ConversionOptionsT; + +struct ConversionMetadata; +struct ConversionMetadataBuilder; +struct ConversionMetadataT; + +enum ModelType : int32_t { + ModelType_NONE = 0, + ModelType_TF_SAVED_MODEL = 1, + ModelType_KERAS_MODEL = 2, + ModelType_TF_CONCRETE_FUNCTIONS = 3, + ModelType_TF_GRAPH_DEF = 4, + ModelType_TF_SESSION = 5, + ModelType_JAX = 6, + ModelType_MIN = ModelType_NONE, + ModelType_MAX = ModelType_JAX +}; + +inline const ModelType (&EnumValuesModelType())[7] { + static const ModelType values[] = { + ModelType_NONE, + ModelType_TF_SAVED_MODEL, + ModelType_KERAS_MODEL, + ModelType_TF_CONCRETE_FUNCTIONS, + ModelType_TF_GRAPH_DEF, + ModelType_TF_SESSION, + ModelType_JAX + }; + return values; +} + +inline const char * const *EnumNamesModelType() { + static const char * const names[8] = { + "NONE", + "TF_SAVED_MODEL", + "KERAS_MODEL", + "TF_CONCRETE_FUNCTIONS", + "TF_GRAPH_DEF", + "TF_SESSION", + "JAX", + nullptr + }; + return names; +} + +inline const char *EnumNameModelType(ModelType e) { + if (::flatbuffers::IsOutRange(e, ModelType_NONE, ModelType_JAX)) return ""; + const size_t index = static_cast(e); + return EnumNamesModelType()[index]; +} + +enum ModelOptimizationMode : int32_t { + ModelOptimizationMode_PTQ_FLOAT16 = 1001, + ModelOptimizationMode_PTQ_DYNAMIC_RANGE = 1002, + ModelOptimizationMode_PTQ_FULL_INTEGER = 1003, + ModelOptimizationMode_PTQ_INT16 = 1004, + ModelOptimizationMode_QUANTIZATION_AWARE_TRAINING = 2000, + ModelOptimizationMode_RANDOM_SPARSITY = 3001, + ModelOptimizationMode_BLOCK_SPARSITY = 3002, + ModelOptimizationMode_STRUCTURED_SPARSITY = 3003, + ModelOptimizationMode_MIN = ModelOptimizationMode_PTQ_FLOAT16, + ModelOptimizationMode_MAX = ModelOptimizationMode_STRUCTURED_SPARSITY +}; + +inline const ModelOptimizationMode (&EnumValuesModelOptimizationMode())[8] { + static const ModelOptimizationMode values[] = { + ModelOptimizationMode_PTQ_FLOAT16, + ModelOptimizationMode_PTQ_DYNAMIC_RANGE, + ModelOptimizationMode_PTQ_FULL_INTEGER, + ModelOptimizationMode_PTQ_INT16, + ModelOptimizationMode_QUANTIZATION_AWARE_TRAINING, + ModelOptimizationMode_RANDOM_SPARSITY, + ModelOptimizationMode_BLOCK_SPARSITY, + ModelOptimizationMode_STRUCTURED_SPARSITY + }; + return values; +} + +inline const char *EnumNameModelOptimizationMode(ModelOptimizationMode e) { + switch (e) { + case ModelOptimizationMode_PTQ_FLOAT16: return "PTQ_FLOAT16"; + case ModelOptimizationMode_PTQ_DYNAMIC_RANGE: return "PTQ_DYNAMIC_RANGE"; + case ModelOptimizationMode_PTQ_FULL_INTEGER: return "PTQ_FULL_INTEGER"; + case ModelOptimizationMode_PTQ_INT16: return "PTQ_INT16"; + case ModelOptimizationMode_QUANTIZATION_AWARE_TRAINING: return "QUANTIZATION_AWARE_TRAINING"; + case ModelOptimizationMode_RANDOM_SPARSITY: return "RANDOM_SPARSITY"; + case ModelOptimizationMode_BLOCK_SPARSITY: return "BLOCK_SPARSITY"; + case ModelOptimizationMode_STRUCTURED_SPARSITY: return "STRUCTURED_SPARSITY"; + default: return ""; + } +} + +struct EnvironmentT : public ::flatbuffers::NativeTable { + typedef Environment TableType; + std::string tensorflow_version{}; + uint32_t api_version = 0; + tflite::ModelType model_type = tflite::ModelType_NONE; +}; + +struct Environment FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef EnvironmentT NativeTableType; + typedef EnvironmentBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_TENSORFLOW_VERSION = 4, + VT_API_VERSION = 6, + VT_MODEL_TYPE = 8 + }; + const ::flatbuffers::String *tensorflow_version() const { + return GetPointer(VT_TENSORFLOW_VERSION); + } + uint32_t api_version() const { + return GetField(VT_API_VERSION, 0); + } + tflite::ModelType model_type() const { + return static_cast(GetField(VT_MODEL_TYPE, 0)); + } + bool Verify(::flatbuffers::Verifier &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_TENSORFLOW_VERSION) && + verifier.VerifyString(tensorflow_version()) && + VerifyField(verifier, VT_API_VERSION, 4) && + VerifyField(verifier, VT_MODEL_TYPE, 4) && + verifier.EndTable(); + } + EnvironmentT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(EnvironmentT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const EnvironmentT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct EnvironmentBuilder { + typedef Environment Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_tensorflow_version(::flatbuffers::Offset<::flatbuffers::String> tensorflow_version) { + fbb_.AddOffset(Environment::VT_TENSORFLOW_VERSION, tensorflow_version); + } + void add_api_version(uint32_t api_version) { + fbb_.AddElement(Environment::VT_API_VERSION, api_version, 0); + } + void add_model_type(tflite::ModelType model_type) { + fbb_.AddElement(Environment::VT_MODEL_TYPE, static_cast(model_type), 0); + } + explicit EnvironmentBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateEnvironment( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::String> tensorflow_version = 0, + uint32_t api_version = 0, + tflite::ModelType model_type = tflite::ModelType_NONE) { + EnvironmentBuilder builder_(_fbb); + builder_.add_model_type(model_type); + builder_.add_api_version(api_version); + builder_.add_tensorflow_version(tensorflow_version); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateEnvironmentDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const char *tensorflow_version = nullptr, + uint32_t api_version = 0, + tflite::ModelType model_type = tflite::ModelType_NONE) { + auto tensorflow_version__ = tensorflow_version ? _fbb.CreateString(tensorflow_version) : 0; + return tflite::CreateEnvironment( + _fbb, + tensorflow_version__, + api_version, + model_type); +} + +::flatbuffers::Offset CreateEnvironment(::flatbuffers::FlatBufferBuilder &_fbb, const EnvironmentT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct SparsityBlockSizeT : public ::flatbuffers::NativeTable { + typedef SparsityBlockSize TableType; + std::vector values{}; +}; + +struct SparsityBlockSize FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef SparsityBlockSizeT NativeTableType; + typedef SparsityBlockSizeBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_VALUES = 4 + }; + const ::flatbuffers::Vector *values() const { + return GetPointer *>(VT_VALUES); + } + bool Verify(::flatbuffers::Verifier &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_VALUES) && + verifier.VerifyVector(values()) && + verifier.EndTable(); + } + SparsityBlockSizeT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(SparsityBlockSizeT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SparsityBlockSizeT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct SparsityBlockSizeBuilder { + typedef SparsityBlockSize Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_values(::flatbuffers::Offset<::flatbuffers::Vector> values) { + fbb_.AddOffset(SparsityBlockSize::VT_VALUES, values); + } + explicit SparsityBlockSizeBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateSparsityBlockSize( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::Vector> values = 0) { + SparsityBlockSizeBuilder builder_(_fbb); + builder_.add_values(values); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateSparsityBlockSizeDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const std::vector *values = nullptr) { + auto values__ = values ? _fbb.CreateVector(*values) : 0; + return tflite::CreateSparsityBlockSize( + _fbb, + values__); +} + +::flatbuffers::Offset CreateSparsityBlockSize(::flatbuffers::FlatBufferBuilder &_fbb, const SparsityBlockSizeT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct ConversionOptionsT : public ::flatbuffers::NativeTable { + typedef ConversionOptions TableType; + std::vector model_optimization_modes{}; + bool allow_custom_ops = false; + bool enable_select_tf_ops = false; + bool force_select_tf_ops = false; + std::vector> sparsity_block_sizes{}; + ConversionOptionsT() = default; + ConversionOptionsT(const ConversionOptionsT &o); + ConversionOptionsT(ConversionOptionsT&&) FLATBUFFERS_NOEXCEPT = default; + ConversionOptionsT &operator=(ConversionOptionsT o) FLATBUFFERS_NOEXCEPT; +}; + +struct ConversionOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef ConversionOptionsT NativeTableType; + typedef ConversionOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_MODEL_OPTIMIZATION_MODES = 4, + VT_ALLOW_CUSTOM_OPS = 6, + VT_ENABLE_SELECT_TF_OPS = 8, + VT_FORCE_SELECT_TF_OPS = 10, + VT_SPARSITY_BLOCK_SIZES = 12 + }; + const ::flatbuffers::Vector *model_optimization_modes() const { + return GetPointer *>(VT_MODEL_OPTIMIZATION_MODES); + } + bool allow_custom_ops() const { + return GetField(VT_ALLOW_CUSTOM_OPS, 0) != 0; + } + bool enable_select_tf_ops() const { + return GetField(VT_ENABLE_SELECT_TF_OPS, 0) != 0; + } + bool force_select_tf_ops() const { + return GetField(VT_FORCE_SELECT_TF_OPS, 0) != 0; + } + const ::flatbuffers::Vector<::flatbuffers::Offset> *sparsity_block_sizes() const { + return GetPointer> *>(VT_SPARSITY_BLOCK_SIZES); + } + bool Verify(::flatbuffers::Verifier &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_MODEL_OPTIMIZATION_MODES) && + verifier.VerifyVector(model_optimization_modes()) && + VerifyField(verifier, VT_ALLOW_CUSTOM_OPS, 1) && + VerifyField(verifier, VT_ENABLE_SELECT_TF_OPS, 1) && + VerifyField(verifier, VT_FORCE_SELECT_TF_OPS, 1) && + VerifyOffset(verifier, VT_SPARSITY_BLOCK_SIZES) && + verifier.VerifyVector(sparsity_block_sizes()) && + verifier.VerifyVectorOfTables(sparsity_block_sizes()) && + verifier.EndTable(); + } + ConversionOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(ConversionOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ConversionOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct ConversionOptionsBuilder { + typedef ConversionOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_model_optimization_modes(::flatbuffers::Offset<::flatbuffers::Vector> model_optimization_modes) { + fbb_.AddOffset(ConversionOptions::VT_MODEL_OPTIMIZATION_MODES, model_optimization_modes); + } + void add_allow_custom_ops(bool allow_custom_ops) { + fbb_.AddElement(ConversionOptions::VT_ALLOW_CUSTOM_OPS, static_cast(allow_custom_ops), 0); + } + void add_enable_select_tf_ops(bool enable_select_tf_ops) { + fbb_.AddElement(ConversionOptions::VT_ENABLE_SELECT_TF_OPS, static_cast(enable_select_tf_ops), 0); + } + void add_force_select_tf_ops(bool force_select_tf_ops) { + fbb_.AddElement(ConversionOptions::VT_FORCE_SELECT_TF_OPS, static_cast(force_select_tf_ops), 0); + } + void add_sparsity_block_sizes(::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset>> sparsity_block_sizes) { + fbb_.AddOffset(ConversionOptions::VT_SPARSITY_BLOCK_SIZES, sparsity_block_sizes); + } + explicit ConversionOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateConversionOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::Vector> model_optimization_modes = 0, + bool allow_custom_ops = false, + bool enable_select_tf_ops = false, + bool force_select_tf_ops = false, + ::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset>> sparsity_block_sizes = 0) { + ConversionOptionsBuilder builder_(_fbb); + builder_.add_sparsity_block_sizes(sparsity_block_sizes); + builder_.add_model_optimization_modes(model_optimization_modes); + builder_.add_force_select_tf_ops(force_select_tf_ops); + builder_.add_enable_select_tf_ops(enable_select_tf_ops); + builder_.add_allow_custom_ops(allow_custom_ops); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateConversionOptionsDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const std::vector *model_optimization_modes = nullptr, + bool allow_custom_ops = false, + bool enable_select_tf_ops = false, + bool force_select_tf_ops = false, + const std::vector<::flatbuffers::Offset> *sparsity_block_sizes = nullptr) { + auto model_optimization_modes__ = model_optimization_modes ? _fbb.CreateVector(*model_optimization_modes) : 0; + auto sparsity_block_sizes__ = sparsity_block_sizes ? _fbb.CreateVector<::flatbuffers::Offset>(*sparsity_block_sizes) : 0; + return tflite::CreateConversionOptions( + _fbb, + model_optimization_modes__, + allow_custom_ops, + enable_select_tf_ops, + force_select_tf_ops, + sparsity_block_sizes__); +} + +::flatbuffers::Offset CreateConversionOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ConversionOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct ConversionMetadataT : public ::flatbuffers::NativeTable { + typedef ConversionMetadata TableType; + std::unique_ptr environment{}; + std::unique_ptr options{}; + ConversionMetadataT() = default; + ConversionMetadataT(const ConversionMetadataT &o); + ConversionMetadataT(ConversionMetadataT&&) FLATBUFFERS_NOEXCEPT = default; + ConversionMetadataT &operator=(ConversionMetadataT o) FLATBUFFERS_NOEXCEPT; +}; + +struct ConversionMetadata FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef ConversionMetadataT NativeTableType; + typedef ConversionMetadataBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_ENVIRONMENT = 4, + VT_OPTIONS = 6 + }; + const tflite::Environment *environment() const { + return GetPointer(VT_ENVIRONMENT); + } + const tflite::ConversionOptions *options() const { + return GetPointer(VT_OPTIONS); + } + bool Verify(::flatbuffers::Verifier &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_ENVIRONMENT) && + verifier.VerifyTable(environment()) && + VerifyOffset(verifier, VT_OPTIONS) && + verifier.VerifyTable(options()) && + verifier.EndTable(); + } + ConversionMetadataT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(ConversionMetadataT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ConversionMetadataT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct ConversionMetadataBuilder { + typedef ConversionMetadata Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_environment(::flatbuffers::Offset environment) { + fbb_.AddOffset(ConversionMetadata::VT_ENVIRONMENT, environment); + } + void add_options(::flatbuffers::Offset options) { + fbb_.AddOffset(ConversionMetadata::VT_OPTIONS, options); + } + explicit ConversionMetadataBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateConversionMetadata( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset environment = 0, + ::flatbuffers::Offset options = 0) { + ConversionMetadataBuilder builder_(_fbb); + builder_.add_options(options); + builder_.add_environment(environment); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateConversionMetadata(::flatbuffers::FlatBufferBuilder &_fbb, const ConversionMetadataT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +inline EnvironmentT *Environment::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new EnvironmentT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void Environment::UnPackTo(EnvironmentT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = tensorflow_version(); if (_e) _o->tensorflow_version = _e->str(); } + { auto _e = api_version(); _o->api_version = _e; } + { auto _e = model_type(); _o->model_type = _e; } +} + +inline ::flatbuffers::Offset Environment::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const EnvironmentT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return CreateEnvironment(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset CreateEnvironment(::flatbuffers::FlatBufferBuilder &_fbb, const EnvironmentT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const EnvironmentT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _tensorflow_version = _o->tensorflow_version.empty() ? 0 : _fbb.CreateString(_o->tensorflow_version); + auto _api_version = _o->api_version; + auto _model_type = _o->model_type; + return tflite::CreateEnvironment( + _fbb, + _tensorflow_version, + _api_version, + _model_type); +} + +inline SparsityBlockSizeT *SparsityBlockSize::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new SparsityBlockSizeT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void SparsityBlockSize::UnPackTo(SparsityBlockSizeT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = values(); if (_e) { _o->values.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->values[_i] = _e->Get(_i); } } else { _o->values.resize(0); } } +} + +inline ::flatbuffers::Offset SparsityBlockSize::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SparsityBlockSizeT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return CreateSparsityBlockSize(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset CreateSparsityBlockSize(::flatbuffers::FlatBufferBuilder &_fbb, const SparsityBlockSizeT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const SparsityBlockSizeT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _values = _o->values.size() ? _fbb.CreateVector(_o->values) : 0; + return tflite::CreateSparsityBlockSize( + _fbb, + _values); +} + +inline ConversionOptionsT::ConversionOptionsT(const ConversionOptionsT &o) + : model_optimization_modes(o.model_optimization_modes), + allow_custom_ops(o.allow_custom_ops), + enable_select_tf_ops(o.enable_select_tf_ops), + force_select_tf_ops(o.force_select_tf_ops) { + sparsity_block_sizes.reserve(o.sparsity_block_sizes.size()); + for (const auto &sparsity_block_sizes_ : o.sparsity_block_sizes) { sparsity_block_sizes.emplace_back((sparsity_block_sizes_) ? new tflite::SparsityBlockSizeT(*sparsity_block_sizes_) : nullptr); } +} + +inline ConversionOptionsT &ConversionOptionsT::operator=(ConversionOptionsT o) FLATBUFFERS_NOEXCEPT { + std::swap(model_optimization_modes, o.model_optimization_modes); + std::swap(allow_custom_ops, o.allow_custom_ops); + std::swap(enable_select_tf_ops, o.enable_select_tf_ops); + std::swap(force_select_tf_ops, o.force_select_tf_ops); + std::swap(sparsity_block_sizes, o.sparsity_block_sizes); + return *this; +} + +inline ConversionOptionsT *ConversionOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new ConversionOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void ConversionOptions::UnPackTo(ConversionOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = model_optimization_modes(); if (_e) { _o->model_optimization_modes.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->model_optimization_modes[_i] = static_cast(_e->Get(_i)); } } else { _o->model_optimization_modes.resize(0); } } + { auto _e = allow_custom_ops(); _o->allow_custom_ops = _e; } + { auto _e = enable_select_tf_ops(); _o->enable_select_tf_ops = _e; } + { auto _e = force_select_tf_ops(); _o->force_select_tf_ops = _e; } + { auto _e = sparsity_block_sizes(); if (_e) { _o->sparsity_block_sizes.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { if(_o->sparsity_block_sizes[_i]) { _e->Get(_i)->UnPackTo(_o->sparsity_block_sizes[_i].get(), _resolver); } else { _o->sparsity_block_sizes[_i] = std::unique_ptr(_e->Get(_i)->UnPack(_resolver)); }; } } else { _o->sparsity_block_sizes.resize(0); } } +} + +inline ::flatbuffers::Offset ConversionOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ConversionOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return CreateConversionOptions(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset CreateConversionOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ConversionOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const ConversionOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _model_optimization_modes = _o->model_optimization_modes.size() ? _fbb.CreateVectorScalarCast(::flatbuffers::data(_o->model_optimization_modes), _o->model_optimization_modes.size()) : 0; + auto _allow_custom_ops = _o->allow_custom_ops; + auto _enable_select_tf_ops = _o->enable_select_tf_ops; + auto _force_select_tf_ops = _o->force_select_tf_ops; + auto _sparsity_block_sizes = _o->sparsity_block_sizes.size() ? _fbb.CreateVector<::flatbuffers::Offset> (_o->sparsity_block_sizes.size(), [](size_t i, _VectorArgs *__va) { return CreateSparsityBlockSize(*__va->__fbb, __va->__o->sparsity_block_sizes[i].get(), __va->__rehasher); }, &_va ) : 0; + return tflite::CreateConversionOptions( + _fbb, + _model_optimization_modes, + _allow_custom_ops, + _enable_select_tf_ops, + _force_select_tf_ops, + _sparsity_block_sizes); +} + +inline ConversionMetadataT::ConversionMetadataT(const ConversionMetadataT &o) + : environment((o.environment) ? new tflite::EnvironmentT(*o.environment) : nullptr), + options((o.options) ? new tflite::ConversionOptionsT(*o.options) : nullptr) { +} + +inline ConversionMetadataT &ConversionMetadataT::operator=(ConversionMetadataT o) FLATBUFFERS_NOEXCEPT { + std::swap(environment, o.environment); + std::swap(options, o.options); + return *this; +} + +inline ConversionMetadataT *ConversionMetadata::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new ConversionMetadataT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void ConversionMetadata::UnPackTo(ConversionMetadataT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = environment(); if (_e) { if(_o->environment) { _e->UnPackTo(_o->environment.get(), _resolver); } else { _o->environment = std::unique_ptr(_e->UnPack(_resolver)); } } else if (_o->environment) { _o->environment.reset(); } } + { auto _e = options(); if (_e) { if(_o->options) { _e->UnPackTo(_o->options.get(), _resolver); } else { _o->options = std::unique_ptr(_e->UnPack(_resolver)); } } else if (_o->options) { _o->options.reset(); } } +} + +inline ::flatbuffers::Offset ConversionMetadata::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ConversionMetadataT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return CreateConversionMetadata(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset CreateConversionMetadata(::flatbuffers::FlatBufferBuilder &_fbb, const ConversionMetadataT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const ConversionMetadataT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _environment = _o->environment ? CreateEnvironment(_fbb, _o->environment.get(), _rehasher) : 0; + auto _options = _o->options ? CreateConversionOptions(_fbb, _o->options.get(), _rehasher) : 0; + return tflite::CreateConversionMetadata( + _fbb, + _environment, + _options); +} + +inline const tflite::ConversionMetadata *GetConversionMetadata(const void *buf) { + return ::flatbuffers::GetRoot(buf); +} + +inline const tflite::ConversionMetadata *GetSizePrefixedConversionMetadata(const void *buf) { + return ::flatbuffers::GetSizePrefixedRoot(buf); +} + +inline bool VerifyConversionMetadataBuffer( + ::flatbuffers::Verifier &verifier) { + return verifier.VerifyBuffer(nullptr); +} + +inline bool VerifySizePrefixedConversionMetadataBuffer( + ::flatbuffers::Verifier &verifier) { + return verifier.VerifySizePrefixedBuffer(nullptr); +} + +inline void FinishConversionMetadataBuffer( + ::flatbuffers::FlatBufferBuilder &fbb, + ::flatbuffers::Offset root) { + fbb.Finish(root); +} + +inline void FinishSizePrefixedConversionMetadataBuffer( + ::flatbuffers::FlatBufferBuilder &fbb, + ::flatbuffers::Offset root) { + fbb.FinishSizePrefixed(root); +} + +inline std::unique_ptr UnPackConversionMetadata( + const void *buf, + const ::flatbuffers::resolver_function_t *res = nullptr) { + return std::unique_ptr(GetConversionMetadata(buf)->UnPack(res)); +} + +inline std::unique_ptr UnPackSizePrefixedConversionMetadata( + const void *buf, + const ::flatbuffers::resolver_function_t *res = nullptr) { + return std::unique_ptr(GetSizePrefixedConversionMetadata(buf)->UnPack(res)); +} + +} // namespace tflite + +#endif // FLATBUFFERS_GENERATED_CONVERSIONMETADATA_TFLITE_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/debug_metadata.fbs b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/debug_metadata.fbs new file mode 100644 index 0000000000000000000000000000000000000000..a6822ab69f574411879856814b040b17a20982d7 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/debug_metadata.fbs @@ -0,0 +1,98 @@ +// Copyright 2024 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. + +namespace debug_metadata; + +table DictItem { + key: string; + val: string; +} + +table CallSiteLoc { + callee_index: uint32; + caller_index: uint32; +} + +table FileLineColLoc { + filename: string; + line: uint32; + column: uint32; +} + +table FusedLoc { + // List of locations. + location_indexes: [uint32]; +} + +table NameLoc { + name: string; + // Nested location for hierarchical naming. + child_index: uint32; +} + +table OpaqueLoc { + identifier: string; + fallback_location_index: uint32; +} + +table UnknownLoc { + // No fields needed. +} + +union LocationType { + CallSiteLoc, + FileLineColLoc, + FusedLoc, + NameLoc, + OpaqueLoc, + UnknownLoc +} + +table Location { + location: LocationType; +} + +union Attribute { + Location, + DictItem, +} + +table OperatorDebugMetadata { + // Top-level location metadata index of this operator. + attribute_metadata_indexes: [uint32]; +} + +table SubgraphDebugMetadata { + // Debug metadata of all the operators. + // This list should be append-only. + operators_debug_metadata:[OperatorDebugMetadata]; +} + +table ConversionDebugMetadata { + // Debug metadata of all the subgraphs. + // This list should be append-only. + subgraphs_debug_metadata:[SubgraphDebugMetadata]; + + attributes: [Attribute]; +} + +union DebugMetadataType { + ConversionDebugMetadata, +} + +table DebugMetadata { + debug_metadata: [DebugMetadataType]; +} + +root_type DebugMetadata; \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/flatbuffer_compatibility_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/flatbuffer_compatibility_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..e3ed45202075e89a92167a545198e70fee968c6b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/flatbuffer_compatibility_test.cc @@ -0,0 +1,88 @@ +/* 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 "flatbuffers/flatc.h" // from @flatbuffers +#include "tensorflow/core/platform/platform.h" + +#ifdef PLATFORM_GOOGLE +#define TFLITE_TF_PREFIX "third_party/tensorflow/" +#else +#define TFLITE_TF_PREFIX "tensorflow/" +#endif +/// Load filename `name` +bool LoadFileRaw(const char *name, std::string *buf) { + std::ifstream fp(name, std::ios::binary); + if (!fp) { + fprintf(stderr, "Failed to read '%s'\n", name); + return false; + } + std::string s((std::istreambuf_iterator(fp)), + std::istreambuf_iterator()); + if (s.empty()) { + fprintf(stderr, "Read '%s' resulted in empty\n", name); + return false; + } + *buf = s; + return true; +} + +bool ParseFile(flatbuffers::Parser *parser, const std::string &filename, + const std::string &contents) { + std::vector include_directories; + auto local_include_directory = flatbuffers::StripFileName(filename); + include_directories.push_back(local_include_directory.c_str()); + include_directories.push_back(nullptr); + if (!parser->Parse(contents.c_str(), include_directories.data(), + filename.c_str())) { + fprintf(stderr, "Failed to parse flatbuffer schema '%s'\n", + contents.c_str()); + return false; + } + return true; +} + +// Checks to make sure current schema in current code does not cause an +// incompatibility. +TEST(SchemaTest, TestCompatibility) { + // Read file contents of schemas into strings + // TODO(aselle): Need a reliable way to load files. + std::string base_contents, current_contents; + const char* base_filename = + TFLITE_TF_PREFIX "compiler/mlir/lite/schema/schema_v3c.fbs"; + const char *current_filename = + TFLITE_TF_PREFIX "compiler/mlir/lite/schema/schema.fbs"; + + ASSERT_TRUE(LoadFileRaw(base_filename, &base_contents)); + ASSERT_TRUE(LoadFileRaw(current_filename, ¤t_contents)); + // Parse the schemas + flatbuffers::Parser base_parser, current_parser; + std::vector include_directories; + ASSERT_TRUE(ParseFile(&base_parser, base_filename, base_contents)); + ASSERT_TRUE(ParseFile(¤t_parser, current_filename, current_contents)); + // Check that the schemas conform and fail if they don't + auto err = current_parser.ConformTo(base_parser); + if (!err.empty()) { + fprintf(stderr, + "Schemas don't conform:\n%s\n" + "In other words some change you made means that new parsers can't" + "parse old files.\n", + err.c_str()); + FAIL(); + } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema.fbs b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema.fbs new file mode 100644 index 0000000000000000000000000000000000000000..40d5f5881d56d5d4ed72dffcbc1b47c10c98d593 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema.fbs @@ -0,0 +1,1736 @@ +// 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. + +// Revision History +// Version 0: Initial version. +// Version 1: Add subgraphs to schema. +// Version 2: Rename operators to conform to NN API. +// Version 3: Move buffer data from Model.Subgraph.Tensors to Model.Buffers. +// Version 3a: Add new builtin op code field. Has backward compatibility with +// version 3. +// Version 3b: Rename fields in SignatureDef. Has backward compatibility with +// version 3 and 3a. +// Version 3c: Move constant tensor buffers & custom op buffers outside from +// Flatbuffers. Has backward compatibility with version 3, 3a and +// 3b. +// Version 3d: Add ExternalBuffer tables and tensor.external_buffer field for +// referencing immutable data stored in external files. + +namespace tflite; + +// This corresponds to the version. +file_identifier "TFL3"; +// File extension of any written files. +file_extension "tflite"; + +// IMPORTANT: All new members of tables, enums and unions must be added at the +// end to ensure backwards compatibility. + +// The type of data stored in a tensor. +enum TensorType : byte { + FLOAT32 = 0, + FLOAT16 = 1, + INT32 = 2, + UINT8 = 3, + INT64 = 4, + STRING = 5, + BOOL = 6, + INT16 = 7, + COMPLEX64 = 8, + INT8 = 9, + FLOAT64 = 10, + COMPLEX128 = 11, + UINT64 = 12, + // Experimental: Resource and variant types are experimental, that are subject + // to change. Do not implement custom kernels using resource & variant types + // now. + RESOURCE = 13, + VARIANT = 14, + UINT32 = 15, + UINT16 = 16, + INT4 = 17, + BFLOAT16 = 18, + INT2 = 19, + UINT4 = 20, + FLOAT8_E4M3FN = 21, + FLOAT8_E5M2 = 22, +} + +// Custom quantization parameters for experimenting with new quantization +// techniques. +table CustomQuantization { + custom:[ubyte] (force_align: 16); +} + +// Represents a specific quantization technique's parameters. +union QuantizationDetails { + CustomQuantization, + BlockwiseQuantization, + MultiAxisQuantization, +} + +// Parameters for blockwise quantization. +table BlockwiseQuantization { + // index to the scale tensor, the tensor can be found in tensors array in + // subgraph. + scales: int; + // index to the zero point tensor. If zero_points is -1, the zero point is + // assumed to be 0, following the convention of optional tensors in tflite. + zero_points: int; + // The block size of the tensor. + block_size: int; +} + +table MultiAxisQuantization { + // index to the scale tensor, the tensor can be found in tensors array in + // subgraph. + scales: int; + + // index to the zero point tensor. If zero_points is -1, the zero point is + // assumed to be 0, following the convention of optional tensors in tflite. + zero_points: int; + + // The block size of the tensor if it's block-wise quantized. + // For per-channel, 0. + block_size: int; + + // The quantized dimensions, same as the quantization_dimension field in + // QuantizationParameters, but supports multiple dimensions. + quantized_dimensions: [int]; +} + +// Parameters for converting a quantized tensor back to float. +table QuantizationParameters { + // These four parameters are the asymmetric linear quantization parameters. + // Given a quantized value q, the corresponding float value f should be: + // f = scale * (q - zero_point) + // For other quantization types, the QuantizationDetails below is used. + min:[float]; // For importing back into tensorflow. + max:[float]; // For importing back into tensorflow. + scale:[float]; // For dequantizing the tensor's values. + zero_point:[long]; + + // If this is not none, the other quantization parameters (i.e. min, max, + // scale, zero_point fields above) are ignored and the value of the + // QuantizationDetails union should be used. + details:QuantizationDetails; + + // Specifies the dimension of the Tensor's shape that the scales and + // zero_points correspond to. For example, a tensor t, with dims=[4, 3, 2, 1] + // with quantization params: + // scale=[1.0, 2.0, 3.0], zero_point=[1, 2, 3], quantization_dimension=1 + // will be quantized across the second dimension of t. + // t[:, 0, :, :] will have scale[0]=1.0, zero_point[0]=1 + // t[:, 1, :, :] will have scale[1]=2.0, zero_point[0]=2 + // t[:, 2, :, :] will have scale[2]=3.0, zero_point[0]=3 + quantized_dimension:int; +} + +// Sparse tensors. +// We use a modification of the TACO format. +// Reference: http://tensor-compiler.org/kjolstad-oopsla17-tensor-compiler.pdf +// +// To encode a conceptual n-dimensional dense tensor with dims (d0, ..., dn-1), +// potentially with a k-dimensional block (0 <= k <= n) with dims +// (dn, ..., dn+k-1), the format needs to specify: +// 1. In what order to traverse these dimensions. For example, to store a 2-D +// matrix in row major order, the traversal order would be (d0, d1), +// whereas to store it in column major order, the traversal order would be +// (d1, d0). If the 2-D matrix has a 2-D inner block, the traversal order +// could be (d0, d1, d2, d3). +// 2. How each block dimension in (dn, ..., dn+k-1) maps to the original +// tensor dimension in (d0, ..., dn-1). +// 3. In the traversal order defined above, the format (dense vs. sparse) and +// index metadata for each dimension. For a dense dimension, this is just +// the size of that dimension. For a sparse dimension, it's the same as +// the compressed index defined in the Compressed Sparse Row (CSR) format. +// (http://scipy-lectures.org/advanced/scipy_sparse/csr_matrix.html) + +// The storage type for a dimension. Currently we support: +// 1. DENSE: each coordinate in this dimension is stored implicitly. +// 2. SPARSE_CSR: only the coordinates with non-zero elements are stored. The +// compression technique is the same what CSR uses. +// More types like a sparse dimension with a different compression technique +// could be added to the list in the future. +enum DimensionType : byte { + DENSE = 0, + SPARSE_CSR = 1, +} + +table Int32Vector { + values:[int]; +} + +table Uint16Vector { + values:[ushort] (force_align: 4); +} + +table Uint8Vector { + values:[ubyte] (force_align: 4); +} + +// Variable-typed buffer to store the index metadata for a sparse dimension. +// The widest type is Int32 instead of UInt32 because tensor's shape is a int32 +// vector. We don't want the per-dimensional index to overflow that range. +union SparseIndexVector { + Int32Vector, + Uint16Vector, + Uint8Vector +} + +table DimensionMetadata { + // Whether a dimension is dense or sparse. + format:DimensionType; + // Index metadata used for a dimension. + // - If format is DimensionType.DENSE then we use the dense_size field to + // store the size of that dimension. Each index in that dimension is + // stored implicitly. + // - If format is DimensionType.SPARSE_CSR then we use array_segments and + // array_indices to encode that dimension. array_segments represents how + // to segment the indices array, each segment corresponds to one element + // in the previous dimension. array_indices represents the index of the + // non-zero elements within this dimension (as those in the CSR matrix + // format, where the first array is row pointers and the second array is + // column indices). + dense_size:int; + array_segments:SparseIndexVector; + array_indices:SparseIndexVector; +} + +// Parameters to encode a sparse TfLite tensor. +table SparsityParameters { + // The traversal order of the dimensions defined in the `shape` field of the + // conceptual dense tensor. For a n-dimensional tensors with dims (d0, d1, + // ..., dn-1), + // - if not block sparse, the traversal_order is just a permutation of (d0, + // ..., dn-1). For example, a 2-D matrix stored in row-major order would + // have traversal_order = (d0, d1). + // - if block sparse with a k-dimensional block (0 <= k <= n), the + // traversal_order has n + k elements. The first n elements are still a + // permutation of (d0, ..., dn-1). The lask k elements are a permutation + // of (dn, ..., dn+k-1), defining how to traverse a block internally. For + // example, a 2-D matrix with 2-D blocks, both stored in row-major order + // would have traversal_order = (d0, d1, d2, d3). + traversal_order:[int]; + // For an n-dimensional tensor with a k-dimensional block (0 <= k <= n), + // stores how a block dimension in (dn, ..., dn+k-1) maps to the original + // tensor dimension in (d0, ..., dn). + // It's stored in the order of (dn, ..., dn+k-1). + // If not block-sparse, this field is NULL. + block_map:[int]; + // In the traversal order defined above, the metadata needed for + // each dimension to locate the non-zero values in the original dense tensor. + // The size of the dim_metadata array = the size of the traversal_order array + // = n + k. + dim_metadata:[DimensionMetadata]; +} + +// The nested tensor type for VARIANT type. +table VariantSubType { + // The tensor shape. + shape:[int]; + type:TensorType; + // If false, the rank or the number of tensor dimensions is unknown. + // If false, "shape" must be []. + has_rank: bool = false; +} + +table Tensor { + // The tensor shape. The meaning of each entry is operator-specific but + // builtin ops use: [batch size, height, width, number of channels] (That's + // Tensorflow's NHWC). + shape:[int]; + type:TensorType; + // An index that refers to the buffers table at the root of the model. Or, + // if there is no data buffer associated (i.e. intermediate results), then + // this is 0 (which refers to an always existent empty buffer). + // + // The data_buffer itself is an opaque container, with the assumption that the + // target device is little-endian. In addition, all builtin operators assume + // the memory is ordered such that if `shape` is [4, 3, 2], then index + // [i, j, k] maps to data_buffer[i*3*2 + j*2 + k]. + buffer:uint; + name:string; // For debugging and importing back into tensorflow. + quantization:QuantizationParameters; // Optional. + + is_variable:bool = false; + + // Parameters to encode a sparse tensor. See the example in + // tensorflow/lite/testdata/sparse_tensor.json. + sparsity:SparsityParameters; // Optional. + + // Encodes `shape` with unknown dimensions. Unknown dimensions are + // represented with -1. + shape_signature:[int]; // Optional. + + // This field is added to distinguish between scalars and tensors of unknown + // ranks (both of which shape is []). + // For scalars (rank = 0), shape = [] and has_rank = true. + // For tensors with known rank (rank > 0) and shape, shape = [...] and + // has_rank = true. + // For tensors with unknown rank and shape, shape = [] and has_rank = false. + has_rank: bool = false; + + // The nested Tensor types for VARIANT type. This is always empty for + // non-VARIANT types. This is optional because the nested type can be omitted. + // Currently only 1 subtype is supported. The field is defined as an array for + // flexibility of supporting multiple subtypes in the future. + variant_tensors:[VariantSubType]; + + // Optional reference to an ExternalBuffer entry that stores constant tensor + // data outside of the FlatBuffer. A value of 0 indicates that the tensor uses + // the traditional embedded buffer field instead. + external_buffer:uint; +} + +// A list of builtin operators. Builtin operators are slightly faster than custom +// ones, but not by much. Moreover, while custom operators accept an opaque +// object containing configuration parameters, builtins have a predetermined +// set of acceptable options. +// LINT.IfChange +enum BuiltinOperator : int32 { + ADD = 0, + AVERAGE_POOL_2D = 1, + CONCATENATION = 2, + CONV_2D = 3, + DEPTHWISE_CONV_2D = 4, + DEPTH_TO_SPACE = 5, + DEQUANTIZE = 6, + EMBEDDING_LOOKUP = 7, + FLOOR = 8, + FULLY_CONNECTED = 9, + HASHTABLE_LOOKUP = 10, + L2_NORMALIZATION = 11, + L2_POOL_2D = 12, + LOCAL_RESPONSE_NORMALIZATION = 13, + LOGISTIC = 14, + LSH_PROJECTION = 15, + LSTM = 16, + MAX_POOL_2D = 17, + MUL = 18, + RELU = 19, + // NOTE(aselle): RELU_N1_TO_1 used to be called RELU1, but it was renamed + // since different model developers use RELU1 in different ways. Never + // create another op called RELU1. + RELU_N1_TO_1 = 20, + RELU6 = 21, + RESHAPE = 22, + RESIZE_BILINEAR = 23, + RNN = 24, + SOFTMAX = 25, + SPACE_TO_DEPTH = 26, + SVDF = 27, + TANH = 28, + CONCAT_EMBEDDINGS = 29, + SKIP_GRAM = 30, + CALL = 31, + CUSTOM = 32, + EMBEDDING_LOOKUP_SPARSE = 33, + PAD = 34, + UNIDIRECTIONAL_SEQUENCE_RNN = 35, + GATHER = 36, + BATCH_TO_SPACE_ND = 37, + SPACE_TO_BATCH_ND = 38, + TRANSPOSE = 39, + MEAN = 40, + SUB = 41, + DIV = 42, + SQUEEZE = 43, + UNIDIRECTIONAL_SEQUENCE_LSTM = 44, + STRIDED_SLICE = 45, + BIDIRECTIONAL_SEQUENCE_RNN = 46, + EXP = 47, + TOPK_V2 = 48, + SPLIT = 49, + LOG_SOFTMAX = 50, + // DELEGATE is a special op type for the operations which are delegated to + // other backends. + // WARNING: Experimental interface, subject to change + DELEGATE = 51, + BIDIRECTIONAL_SEQUENCE_LSTM = 52, + CAST = 53, + PRELU = 54, + MAXIMUM = 55, + ARG_MAX = 56, + MINIMUM = 57, + LESS = 58, + NEG = 59, + PADV2 = 60, + GREATER = 61, + GREATER_EQUAL = 62, + LESS_EQUAL = 63, + SELECT = 64, + SLICE = 65, + SIN = 66, + TRANSPOSE_CONV = 67, + SPARSE_TO_DENSE = 68, + TILE = 69, + EXPAND_DIMS = 70, + EQUAL = 71, + NOT_EQUAL = 72, + LOG = 73, + SUM = 74, + SQRT = 75, + RSQRT = 76, + SHAPE = 77, + POW = 78, + ARG_MIN = 79, + FAKE_QUANT = 80, + REDUCE_PROD = 81, + REDUCE_MAX = 82, + PACK = 83, + LOGICAL_OR = 84, + ONE_HOT = 85, + LOGICAL_AND = 86, + LOGICAL_NOT = 87, + UNPACK = 88, + REDUCE_MIN = 89, + FLOOR_DIV = 90, + REDUCE_ANY = 91, + SQUARE = 92, + ZEROS_LIKE = 93, + FILL = 94, + FLOOR_MOD = 95, + RANGE = 96, + RESIZE_NEAREST_NEIGHBOR = 97, + LEAKY_RELU = 98, + SQUARED_DIFFERENCE = 99, + MIRROR_PAD = 100, + ABS = 101, + SPLIT_V = 102, + UNIQUE = 103, + CEIL = 104, + REVERSE_V2 = 105, + ADD_N = 106, + GATHER_ND = 107, + COS = 108, + WHERE = 109, + RANK = 110, + ELU = 111, + REVERSE_SEQUENCE = 112, + MATRIX_DIAG = 113, + QUANTIZE = 114, + MATRIX_SET_DIAG = 115, + ROUND = 116, + HARD_SWISH = 117, + IF = 118, + WHILE = 119, + NON_MAX_SUPPRESSION_V4 = 120, + NON_MAX_SUPPRESSION_V5 = 121, + SCATTER_ND = 122, + SELECT_V2 = 123, + DENSIFY = 124, + SEGMENT_SUM = 125, + BATCH_MATMUL = 126, + PLACEHOLDER_FOR_GREATER_OP_CODES = 127, + CUMSUM = 128, + CALL_ONCE = 129, + BROADCAST_TO = 130, + RFFT2D = 131, + CONV_3D = 132, + IMAG=133, + REAL=134, + COMPLEX_ABS=135, + HASHTABLE = 136, + HASHTABLE_FIND = 137, + HASHTABLE_IMPORT = 138, + HASHTABLE_SIZE = 139, + REDUCE_ALL = 140, + CONV_3D_TRANSPOSE = 141, + VAR_HANDLE = 142, + READ_VARIABLE = 143, + ASSIGN_VARIABLE = 144, + BROADCAST_ARGS = 145, + RANDOM_STANDARD_NORMAL = 146, + BUCKETIZE = 147, + RANDOM_UNIFORM = 148, + MULTINOMIAL = 149, + GELU = 150, + DYNAMIC_UPDATE_SLICE = 151, + RELU_0_TO_1 = 152, + UNSORTED_SEGMENT_PROD = 153, + UNSORTED_SEGMENT_MAX = 154, + UNSORTED_SEGMENT_SUM = 155, + ATAN2 = 156, + UNSORTED_SEGMENT_MIN = 157, + SIGN = 158, + BITCAST = 159, + BITWISE_XOR = 160, + RIGHT_SHIFT = 161, + // All Operators start with STABLEHLO_ prefixes are subject to change + // Many of the ops below can not be executed by TFlite runtime + STABLEHLO_LOGISTIC = 162, // WARNING: Do not have runtime support + STABLEHLO_ADD = 163, + STABLEHLO_DIVIDE = 164, // WARNING: No runtime support yet + STABLEHLO_MULTIPLY = 165, + STABLEHLO_MAXIMUM = 166, + STABLEHLO_RESHAPE = 167, // WARNING: No runtime support yet + STABLEHLO_CLAMP = 168, // WARNING: No runtime support + STABLEHLO_CONCATENATE = 169, // WARNING: No runtime support + STABLEHLO_BROADCAST_IN_DIM = 170, // WARNING: No runtime support + STABLEHLO_CONVOLUTION = 171, // WARNING: No runtime support + STABLEHLO_SLICE = 172, // WARNING: No runtime support + STABLEHLO_CUSTOM_CALL = 173, // WARNING: No runtime support + STABLEHLO_REDUCE = 174, // WARNING: No runtime support + STABLEHLO_ABS = 175, // WARNING: No runtime support + STABLEHLO_AND = 176, // WARNING: No runtime support + STABLEHLO_COSINE = 177, // WARNING: No runtime support + STABLEHLO_EXPONENTIAL = 178, // WARNING: No runtime support + STABLEHLO_FLOOR = 179, // WARNING: No runtime support + STABLEHLO_LOG = 180, // WARNING: No runtime support + STABLEHLO_MINIMUM = 181, + STABLEHLO_NEGATE = 182, // WARNING: No runtime support + STABLEHLO_OR = 183, // WARNING: No runtime support + STABLEHLO_POWER = 184, // WARNING: No runtime support + STABLEHLO_REMAINDER = 185, // WARNING: No runtime support + STABLEHLO_RSQRT = 186, // WARNING: No runtime support + STABLEHLO_SELECT = 187, // WARNING: No runtime support + STABLEHLO_SUBTRACT = 188, // WARNING: No runtime support + STABLEHLO_TANH = 189, // WARNING: No runtime support + STABLEHLO_SCATTER = 190, + STABLEHLO_COMPARE = 191, // WARNING: No runtime support + STABLEHLO_CONVERT = 192, // WARNING: No runtime support + STABLEHLO_DYNAMIC_SLICE = 193, // WARNING: No runtime support + STABLEHLO_DYNAMIC_UPDATE_SLICE = 194, // WARNING: No runtime support + STABLEHLO_PAD = 195, + STABLEHLO_IOTA = 196, // WARNING: No runtime support + STABLEHLO_DOT_GENERAL = 197, // WARNING: No runtime support + STABLEHLO_REDUCE_WINDOW = 198, + STABLEHLO_SORT = 199, // WARNING: No runtime support + STABLEHLO_WHILE = 200, // WARNING: No runtime support + STABLEHLO_GATHER = 201, + STABLEHLO_TRANSPOSE = 202, // WARNING: No runtime support + DILATE = 203, + STABLEHLO_RNG_BIT_GENERATOR = 204, + REDUCE_WINDOW = 205 (deprecated), + STABLEHLO_COMPOSITE = 206, // WARNING: No runtime support + STABLEHLO_SHIFT_LEFT = 207, + STABLEHLO_CBRT = 208, // WARNING: No runtime support + STABLEHLO_CASE = 209, +} +// LINT.ThenChange(nnapi_linter/linter.proto) + +// Options for the builtin operators. +union BuiltinOptions { + Conv2DOptions, + DepthwiseConv2DOptions, + ConcatEmbeddingsOptions, + LSHProjectionOptions, + Pool2DOptions, + SVDFOptions, + RNNOptions, + FullyConnectedOptions, + SoftmaxOptions, + ConcatenationOptions, + AddOptions, + L2NormOptions, + LocalResponseNormalizationOptions, + LSTMOptions, + ResizeBilinearOptions, + CallOptions, + ReshapeOptions, + SkipGramOptions, + SpaceToDepthOptions, + EmbeddingLookupSparseOptions, + MulOptions, + PadOptions, + GatherOptions, + BatchToSpaceNDOptions, + SpaceToBatchNDOptions, + TransposeOptions, + ReducerOptions, + SubOptions, + DivOptions, + SqueezeOptions, + SequenceRNNOptions, + StridedSliceOptions, + ExpOptions, + TopKV2Options, + SplitOptions, + LogSoftmaxOptions, + CastOptions, + DequantizeOptions, + MaximumMinimumOptions, + ArgMaxOptions, + LessOptions, + NegOptions, + PadV2Options, + GreaterOptions, + GreaterEqualOptions, + LessEqualOptions, + SelectOptions, + SliceOptions, + TransposeConvOptions, + SparseToDenseOptions, + TileOptions, + ExpandDimsOptions, + EqualOptions, + NotEqualOptions, + ShapeOptions, + PowOptions, + ArgMinOptions, + FakeQuantOptions, + PackOptions, + LogicalOrOptions, + OneHotOptions, + LogicalAndOptions, + LogicalNotOptions, + UnpackOptions, + FloorDivOptions, + SquareOptions, + ZerosLikeOptions, + FillOptions, + BidirectionalSequenceLSTMOptions, + BidirectionalSequenceRNNOptions, + UnidirectionalSequenceLSTMOptions, + FloorModOptions, + RangeOptions, + ResizeNearestNeighborOptions, + LeakyReluOptions, + SquaredDifferenceOptions, + MirrorPadOptions, + AbsOptions, + SplitVOptions, + UniqueOptions, + ReverseV2Options, + AddNOptions, + GatherNdOptions, + CosOptions, + WhereOptions, + RankOptions, + ReverseSequenceOptions, + MatrixDiagOptions, + QuantizeOptions, + MatrixSetDiagOptions, + HardSwishOptions, + IfOptions, + WhileOptions, + DepthToSpaceOptions, + NonMaxSuppressionV4Options, + NonMaxSuppressionV5Options, + ScatterNdOptions, + SelectV2Options, + DensifyOptions, + SegmentSumOptions, + BatchMatMulOptions, + CumsumOptions, + CallOnceOptions, + BroadcastToOptions, + Rfft2dOptions, + Conv3DOptions, + HashtableOptions, + HashtableFindOptions, + HashtableImportOptions, + HashtableSizeOptions, + VarHandleOptions, + ReadVariableOptions, + AssignVariableOptions, + RandomOptions, + BucketizeOptions, + GeluOptions, + DynamicUpdateSliceOptions, + UnsortedSegmentProdOptions, + UnsortedSegmentMaxOptions, + UnsortedSegmentMinOptions, + UnsortedSegmentSumOptions, + ATan2Options, + SignOptions, + BitcastOptions, + BitwiseXorOptions, + RightShiftOptions, + // DO NOT add new options this union, will cause failure in Java api + // generation otherwise + // Add new builtin options into builtin options 2 instead +} + +union BuiltinOptions2{ + StablehloConcatenateOptions, + StablehloBroadcastInDimOptions, + StablehloSliceOptions, + StablehloConvolutionOptions, + StablehloCustomCallOptions, + StablehloReduceOptions, + StablehloScatterOptions, + StablehloCompareOptions, + StablehloDynamicSliceOptions, + StablehloPadOptions, + StablehloIotaOptions, + StablehloDotGeneralOptions, + StablehloReduceWindowOptions, + StablehloSortOptions, + StablehloWhileOptions, + StablehloGatherOptions, + StablehloTransposeOptions, + DilateOptions, + StablehloRngBitGeneratorOptions, + ReduceWindowOptions (deprecated), + StableHLOCompositeOptions, + StablehloShiftLeftOptions, + StablehloCaseOptions, +} + +table StablehloGatherOptions{ + offset_dims : [long]; + collapsed_slice_dims : [long]; + start_index_map : [long]; + index_vector_dim : long; + slice_sizes : [long]; + indices_are_sorted : bool; +} + +table StablehloTransposeOptions{ + permutation : [long]; +} + +enum StablehloPrecisionConfig : uint { + DEFAULT, + HIGH, + HIGHEST, +} + +table StablehloDotGeneralOptions{ + lhs_batching_dimensions : [long]; + rhs_batching_dimensions : [long]; + lhs_contracting_dimensions : [long]; + rhs_contracting_dimensions : [long]; + precision_config : [StablehloPrecisionConfig]; +} + +table StablehloReduceWindowOptions{ + window_dimensions : [long]; + window_strides : [long]; + base_dilations : [long]; + window_dilations : [long]; + padding : [long]; + body_subgraph_index : int; +} + +table StablehloWhileOptions{ + cond_subgraph_index : int; + body_subgraph_index : int; +} + +table StablehloSortOptions{ + dimension : long; + is_stable : bool; + comparator_subgraph_index : int; +} + +table StablehloConcatenateOptions { + dimension : long; +} + +table StablehloBroadcastInDimOptions{ + broadcast_dimensions : [long]; +} + +enum StablehloComparisonDirection : uint { + STABLEHLO_COMPARISON_DIRECTION_EQ, + STABLEHLO_COMPARISON_DIRECTION_NE, + STABLEHLO_COMPARISON_DIRECTION_GE, + STABLEHLO_COMPARISON_DIRECTION_GT, + STABLEHLO_COMPARISON_DIRECTION_LE, + STABLEHLO_COMPARISON_DIRECTION_LT, + +} + +enum StablehloComparisonType : uint { + STABLEHLO_COMPARISON_TYPE_NOTYPE, + STABLEHLO_COMPARISON_TYPE_FLOAT, + STABLEHLO_COMPARISON_TYPE_FLOAT_TOTAL_ORDER, + STABLEHLO_COMPARISON_TYPE_SIGNED, + STABLEHLO_COMPARISON_TYPE_UNSIGNED, +} + +table StablehloCompareOptions{ + comparison_direction : StablehloComparisonDirection; + compare_type : StablehloComparisonType; +} + +table StablehloDynamicSliceOptions{ + slice_sizes : [long]; +} + +table StablehloPadOptions{ + edge_padding_low : [long]; + edge_padding_high : [long]; + interior_padding : [long]; +} + +table StablehloIotaOptions{ + iota_dimension : long; +} + +table StablehloCustomCallOptions { + call_target_name : string; + has_side_effect : bool; + backend_config: string; + api_version : int; // will be decprecated + called_computations: [int]; // should point to subgraphs of the computations + custom_attributes : [ubyte]; +} + +table StablehloReduceOptions { + dimensions : [long]; + body_subgraph_index : int; +} + +table StablehloSliceOptions{ + start_indices : [long]; + limit_indices : [long]; + strides : [long]; +} + +table StablehloConvolutionOptions{ + window_strides : [long]; + padding : [long]; + lhs_dilation : [long]; + rhs_dilation : [long]; + window_reversal : [bool]; + input_batch_dimension : long; + input_feature_dimension : long; + input_spatial_dimensions : [long]; + kernel_input_feature_dimension : long; + kernel_output_feature_dimension : long; + kernel_spatial_dimensions : [long]; + output_batch_dimension : long; + output_feature_dimension : long; + output_spatial_dimensions : [long]; + feature_group_count : long; + batch_group_count : long; + precision_config : [StablehloPrecisionConfig]; +} + +table StablehloScatterOptions { + indices_are_sorted: bool; + update_window_dims: [long]; + inserted_window_dims: [long]; + scatter_dims_to_operand_dims: [long]; + index_vector_dim: long; + unique_indices: bool; + update_computation_subgraph_index: int; +} + +table StablehloCaseOptions{ + branch_subgraph_indices : [int]; +} + +enum RngAlgorithm : byte { + // An algorithm auto-selected by the system according to device type. + DEFAULT = 0, + // The Philox algorithm, as described in paper + // ['Parallel Random Numbers: As Easy as 1, 2, 3'] + // (https://www.thesalmons.org/john/random123/papers/random123sc11.pdf) + PHILOX = 1, + // The ThreeFry algorithm, as described in paper + // ['Parallel Random Numbers: As Easy as 1, 2, 3'] + // (https://www.thesalmons.org/john/random123/papers/random123sc11.pdf) + THREEFRY = 2, +} + +table StablehloRngBitGeneratorOptions { + algorithm:RngAlgorithm; +} + +// LINT.IfChange +enum Padding : byte { SAME, VALID } +// LINT.ThenChange(//tensorflow/compiler/mlir/lite/ir/tfl_op_enums.td) + +// LINT.IfChange +enum ActivationFunctionType : byte { + NONE = 0, + RELU = 1, + RELU_N1_TO_1 = 2, + RELU6 = 3, + TANH = 4, + SIGN_BIT = 5, +} +// LINT.ThenChange(//tensorflow/compiler/mlir/lite/ir/tfl_op_enums.td) + +table Conv2DOptions { + padding:Padding; + stride_w:int; + stride_h:int; + fused_activation_function:ActivationFunctionType; + dilation_w_factor:int = 1; + dilation_h_factor:int = 1; + // Parameters for Conv2D version 8 or above. + // When set, quantized_bias_type defines the dtype for both bias and accumulator. + quantized_bias_type: TensorType; +} + +// Options for both Conv3D and Conv3DTranspose. +table Conv3DOptions { + padding:Padding; + stride_d:int; + stride_w:int; + stride_h:int; + fused_activation_function:ActivationFunctionType; + dilation_d_factor:int = 1; + dilation_w_factor:int = 1; + dilation_h_factor:int = 1; +} + +table Pool2DOptions { + padding:Padding; + stride_w:int; + stride_h:int; + filter_width:int; + filter_height:int; + fused_activation_function:ActivationFunctionType; +} + +table DepthwiseConv2DOptions { + // Parameters for DepthwiseConv version 1 or above. + padding:Padding; + stride_w:int; + stride_h:int; + // `depth_multiplier` is redundant. It's used by CPU kernels in + // TensorFlow 2.0 or below, but ignored in versions above. + // See comments in lite/c/builtin_op_data.h for more details. + depth_multiplier:int; + fused_activation_function:ActivationFunctionType; + // Parameters for DepthwiseConv version 2 or above. + dilation_w_factor:int = 1; + dilation_h_factor:int = 1; +} + +table ConcatEmbeddingsOptions { + num_channels:int; + num_columns_per_channel:[int]; + embedding_dim_per_channel:[int]; // This could be inferred from parameters. +} + +enum LSHProjectionType: byte { + UNKNOWN = 0, + SPARSE = 1, + DENSE = 2, +} + +table LSHProjectionOptions { + type: LSHProjectionType; +} + +table SVDFOptions { + rank:int; + fused_activation_function:ActivationFunctionType; + // For weights-only quantization, use asymmetric quantization for non + // constant inputs at evaluation time. + asymmetric_quantize_inputs:bool; +} + +// An implementation of TensorFlow RNNCell. +table RNNOptions { + fused_activation_function:ActivationFunctionType; + asymmetric_quantize_inputs:bool; +} + +// An implementation of TensorFlow dynamic_rnn with RNNCell. +table SequenceRNNOptions { + time_major:bool; + fused_activation_function:ActivationFunctionType; + asymmetric_quantize_inputs:bool; +} + +// An implementation of TensorFlow bidrectional_dynamic_rnn with RNNCell. +table BidirectionalSequenceRNNOptions { + time_major:bool; + fused_activation_function:ActivationFunctionType; + merge_outputs: bool; + asymmetric_quantize_inputs:bool; +} + +// LINT.IfChange +enum FullyConnectedOptionsWeightsFormat: byte { + DEFAULT = 0, + SHUFFLED4x16INT8 = 1, +} +// LINT.ThenChange(//tensorflow/compiler/mlir/lite/ir/tfl_op_enums.td) + +// An implementation of TensorFlow fully_connected (a.k.a Dense) layer. +table FullyConnectedOptions { + // Parameters for FullyConnected version 1 or above. + fused_activation_function:ActivationFunctionType; + + // Parameters for FullyConnected version 2 or above. + weights_format:FullyConnectedOptionsWeightsFormat = DEFAULT; + + // Parameters for FullyConnected version 5 or above. + // If set to true, then the number of dimension is preserved. Furthermore, + // all but the last dimension of the input and output shapes will be equal. + keep_num_dims: bool; + + // Parameters for FullyConnected version 7 or above. + // If set to true, then weights-only op will use asymmetric quantization for + // inputs. + asymmetric_quantize_inputs: bool; + + // Parameters for FullyConnected version 11 or above. + // When set, quantized_bias_type defines the dtype for both bias and accumulator. + quantized_bias_type: TensorType; +} + +table SoftmaxOptions { + beta: float; +} + +// An implementation of TensorFlow concat. +table ConcatenationOptions { + axis:int; + fused_activation_function:ActivationFunctionType; +} + +table AddOptions { + fused_activation_function:ActivationFunctionType; + // Parameters supported by version 3. + pot_scale_int16:bool = true; +} + +table MulOptions { + fused_activation_function:ActivationFunctionType; +} + +table L2NormOptions { + // This field is currently ignored in the L2 Norm Op. + fused_activation_function:ActivationFunctionType; +} + +table LocalResponseNormalizationOptions { + radius:int; + bias:float; + alpha:float; + beta:float; +} + +// LINT.IfChange +enum LSTMKernelType : byte { + // Full LSTM kernel which supports peephole and projection. + FULL = 0, + // Basic LSTM kernels. Equivalent to TensorFlow BasicLSTMCell. + BASIC = 1, +} +// LINT.ThenChange(//tensorflow/compiler/mlir/lite/ir/tfl_op_enums.td) + +// An implementation of TensorFlow LSTMCell and CoupledInputForgetGateLSTMCell +table LSTMOptions { + // Parameters for LSTM version 1 or above. + fused_activation_function:ActivationFunctionType; + cell_clip: float; // Optional, 0.0 means no clipping + proj_clip: float; // Optional, 0.0 means no clipping + + // Parameters for LSTM version 2 or above. + // Basic kernel is only supported in version 2 or above. + kernel_type: LSTMKernelType = FULL; + + // Parameters for LSTM version 4 or above. + asymmetric_quantize_inputs: bool; +} + +// An implementation of TensorFlow dynamic_rnn with LSTMCell. +table UnidirectionalSequenceLSTMOptions { + fused_activation_function:ActivationFunctionType; + cell_clip: float; // Optional, 0.0 means no clipping + proj_clip: float; // Optional, 0.0 means no clipping + + // If true then first dimension is sequence, otherwise batch. + time_major:bool; + + // Parameter for Unidirectional Sequence LSTM version 3. + asymmetric_quantize_inputs:bool; + + // Parameter for unidirectional sequence RNN version 4. + diagonal_recurrent_tensors:bool; +} + +table BidirectionalSequenceLSTMOptions { + // Parameters supported by version 1: + fused_activation_function:ActivationFunctionType; + cell_clip: float; // Optional, 0.0 means no clipping + proj_clip: float; // Optional, 0.0 means no clipping + + // If true, store the outputs of both directions into the first output. + merge_outputs: bool; + + // Parameters supported by version 2: + // If true then first dimension is sequence, otherwise batch. + // Version 1 implementations assumed time_major to be true, so this default + // value should never change. + time_major: bool = true; + + // Parameters for version 3 or above. + asymmetric_quantize_inputs:bool; +} + +table ResizeBilinearOptions { + new_height: int (deprecated); + new_width: int (deprecated); + align_corners: bool; + half_pixel_centers: bool; +} + +table ResizeNearestNeighborOptions { + align_corners: bool; + half_pixel_centers: bool; +} + +// A call operation options +table CallOptions { + // The subgraph index that needs to be called. + subgraph:uint; +} + +table PadOptions { +} + +table PadV2Options { +} + +table ReshapeOptions { + new_shape:[int]; +} + +table SpaceToBatchNDOptions { +} + +table BatchToSpaceNDOptions { +} + +table SkipGramOptions { + ngram_size: int; + max_skip_size: int; + include_all_ngrams: bool; +} + +table SpaceToDepthOptions { + block_size: int; +} + +table DepthToSpaceOptions { + block_size: int; +} + +table SubOptions { + fused_activation_function:ActivationFunctionType; + // Parameters supported by version 5 + pot_scale_int16:bool = true; +} + +table DivOptions { + fused_activation_function:ActivationFunctionType; +} + +table TopKV2Options { +} + +enum CombinerType : byte { + SUM = 0, + MEAN = 1, + SQRTN = 2, +} + +table EmbeddingLookupSparseOptions { + combiner:CombinerType; +} + +table GatherOptions { + axis: int; + // Parameters for Gather version 5 or above. + batch_dims: int = 0; +} + +table TransposeOptions { +} + +table ExpOptions { +} + +table CosOptions { +} + +table ReducerOptions { + keep_dims: bool; +} + +table SqueezeOptions { + squeeze_dims:[int]; +} + +table SplitOptions { + num_splits: int; +} + +table SplitVOptions { + num_splits: int; +} + +table StridedSliceOptions { + begin_mask: int; + end_mask: int; + ellipsis_mask: int; + new_axis_mask: int; + shrink_axis_mask: int; + // If true, then the end tensor is an offset of the begin tensor. + offset: bool; +} + +table LogSoftmaxOptions { +} + +table CastOptions { + in_data_type: TensorType; + out_data_type: TensorType; +} + +table DequantizeOptions { +} + +table MaximumMinimumOptions { +} + +table TileOptions { +} + +table ArgMaxOptions { + output_type : TensorType; +} + +table ArgMinOptions { + output_type : TensorType; +} + +table GreaterOptions { +} + +table GreaterEqualOptions { +} + +table LessOptions { +} + +table LessEqualOptions { +} + +table NegOptions { +} + +table SelectOptions { +} + +table SliceOptions { +} + +table TransposeConvOptions { + // Parameters supported by version 1, 2, 3: + padding:Padding; + stride_w:int; + stride_h:int; + + // Parameters supported by version 4: + fused_activation_function:ActivationFunctionType = NONE; + + // Parameters for TransposeConv version 5 or above. + // If set, use this for bias and accumulator. + // When set, quantized_bias_type defines the dtype for both bias and accumulator. + quantized_bias_type: TensorType; +} + +table ExpandDimsOptions { +} + +table SparseToDenseOptions { + validate_indices:bool; +} + +table EqualOptions { +} + +table NotEqualOptions { +} + +table ShapeOptions { + // Optional output type of the operation (int32 or int64). Defaults to int32. + out_type : TensorType; +} + +table RankOptions { +} + +table PowOptions { +} + +table FakeQuantOptions { + // Parameters supported by version 1: + min:float; + max:float; + num_bits:int; + + // Parameters supported by version 2: + narrow_range:bool; +} + +table PackOptions { + values_count:int; + axis:int; +} + +table LogicalOrOptions { +} + +table OneHotOptions { + axis:int; +} + +table AbsOptions { +} + + +table HardSwishOptions { +} + +table LogicalAndOptions { +} + +table LogicalNotOptions { +} + +table UnpackOptions { + num:int; + axis:int; +} + +table FloorDivOptions { +} + +table SquareOptions { +} + +table ZerosLikeOptions { +} + +table FillOptions { +} + +table FloorModOptions { +} + +table RangeOptions { +} + +table LeakyReluOptions { + alpha:float; +} + +table SquaredDifferenceOptions { +} + +// LINT.IfChange +enum MirrorPadMode : byte { + // Doesn't include borders. + REFLECT = 0, + // Includes borders. + SYMMETRIC = 1, +} +// LINT.ThenChange(//tensorflow/compiler/mlir/lite/ir/tfl_op_enums.td) + +table MirrorPadOptions { + mode:MirrorPadMode; +} + +table UniqueOptions { + idx_out_type:TensorType = INT32; +} + +table ReverseV2Options { +} + +table AddNOptions { +} + +table GatherNdOptions { +} + +table WhereOptions { +} + +table ReverseSequenceOptions { + seq_dim:int; + batch_dim:int = 0; +} + +table MatrixDiagOptions { +} + +table QuantizeOptions { +} + +table MatrixSetDiagOptions { +} + +table IfOptions { + then_subgraph_index:int; + else_subgraph_index:int; +} + +table CallOnceOptions { + init_subgraph_index:int; +} + +table WhileOptions { + cond_subgraph_index:int; + body_subgraph_index:int; +} + +table NonMaxSuppressionV4Options { +} + +table NonMaxSuppressionV5Options { +} + +table ScatterNdOptions { +} + +table SelectV2Options { +} + +table DensifyOptions { +} + +table SegmentSumOptions { +} + +table BatchMatMulOptions { + adj_x:bool; + adj_y:bool; + // Parameters for BatchMatMul version 4 or above. + // If set to true, then weights-only op will use asymmetric quantization for + // inputs. + asymmetric_quantize_inputs: bool; +} + +table CumsumOptions { + exclusive:bool; + reverse:bool; +} + +table BroadcastToOptions { +} + +table Rfft2dOptions { +} + +table HashtableOptions { + // The identity of hash tables. This identity will be used across different + // subgraphs in the same interpreter instance. + table_id:int; + key_dtype:TensorType; + value_dtype:TensorType; +} + +table HashtableFindOptions { +} + +table HashtableImportOptions { +} + +table HashtableSizeOptions { +} + +table VarHandleOptions { + container:string; + shared_name:string; +} + +table ReadVariableOptions { +} + +table AssignVariableOptions { +} + +table RandomOptions { + seed: long; + seed2: long; +} + +table BucketizeOptions { + boundaries: [float]; // The bucket boundaries. +} + +table GeluOptions { + approximate: bool; +} + +table DynamicUpdateSliceOptions { +} + +table UnsortedSegmentProdOptions { +} + +table UnsortedSegmentMaxOptions { +} + +table UnsortedSegmentSumOptions { +} + +table ATan2Options { +} + +table UnsortedSegmentMinOptions{ +} + +table SignOptions { +} + +table BitcastOptions { +} + +table BitwiseXorOptions { +} + +table RightShiftOptions { +} + +table DilateOptions { +} + +enum ReduceWindowFunction : int { + UNSUPPORTED, + ADD, + MUL, + MINIMUM, + MAXIMUM, + ALL, + ANY, +} + +table ReduceWindowOptions (deprecated) { + reduce_function: ReduceWindowFunction; +} + +// An OperatorCode can be an enum value (BuiltinOperator) if the operator is a +// builtin, or a string if the operator is custom. +table OperatorCode { + // This field is for backward compatibility. This field will be used when + // the value of the extended builtin_code field has less than + // BulitinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES. + deprecated_builtin_code:byte; + custom_code:string; + + // The version of the operator. The version need to be bumped whenever new + // parameters are introduced into an op. + version:int = 1; + + // This field is introduced for resolving op builtin code shortage problem + // (the original BuiltinOperator enum field was represented as a byte). + // This field will be used when the value of the extended builtin_code field + // has greater than BulitinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES. + builtin_code:BuiltinOperator; +} + +enum CustomOptionsFormat : byte { + FLEXBUFFERS = 0, +} + +table StableHLOCompositeOptions { + name:string; + decomposition_subgraph_index:int32; + composite_attributes:[ubyte]; + composite_attributes_format:CustomOptionsFormat; + version:int32; +} + +table StablehloShiftLeftOptions { +} + +// An operator takes tensors as inputs and outputs. The type of operation being +// performed is determined by an index into the list of valid OperatorCodes, +// while the specifics of each operations is configured using builtin_options +// or custom_options. +table Operator { + // Index into the operator_codes array. Using an integer here avoids + // complicate map lookups. + opcode_index:uint; + + // Optional input are indicated by -1. + inputs:[int]; + outputs:[int]; + + builtin_options:BuiltinOptions; + custom_options:[ubyte]; + custom_options_format:CustomOptionsFormat; + + // A list of booleans indicating the input tensors which are being mutated by + // this operator.(e.g. used by RNN and LSTM). + // For example, if the "inputs" array refers to 5 tensors and the second and + // fifth are mutable variables, then this list will contain + // [false, true, false, false, true]. + // + // If the list is empty, no variable is mutated in this operator. + // The list either has the same length as `inputs`, or is empty. + mutating_variable_inputs:[bool]; + + // A list of indices to the subgraph's "tensors" that are internal to an Op. + // Internal tensors are those that do not flow in or out of the operation, + // but instead are part of internal computation. As such, the operation's + // implementation may manage its memory more efficiently. They are needed + // however (i.e. not just an implementation detail) since they are part of the + // computation, which may require relevant metadata such as quantization + // parameters. + intermediates:[int]; + + // When an op is using custom_options in a model that is larger than 2GB, then + // we instead use the following attributes to find the buffer location which + // is stored outside of flatbuffers, the offset is calculated relative to the + // beginning of the file and is only valid if > 1 + large_custom_options_offset: ulong; + large_custom_options_size: ulong; + + // Flatbuffers union struct has a 128 elements limit in JAVA, so a second + // union is added, in the case of where BuitlinOptions2 runs out, a third + // one can be added + builtin_options_2 : BuiltinOptions2; + + // Index into operators_debug_metadata list. + debug_metadata_index: int = -1; +} + +// The root type, defining a subgraph, which typically represents an entire +// model. +table SubGraph { + // A list of all tensors used in this subgraph. + tensors:[Tensor]; + + // Indices of the tensors that are inputs into this subgraph. Note this is + // the list of non-static tensors that feed into the subgraph for inference. + inputs:[int]; + + // Indices of the tensors that are outputs out of this subgraph. Note this is + // the list of output tensors that are considered the product of the + // subgraph's inference. + outputs:[int]; + + // All operators, in execution order. + operators:[Operator]; + + // Name of this subgraph (used for debugging). + name:string; + + // Index into subgraphs_debug_metadata list. + debug_metadata_index: int = -1; +} + +// Table of raw data buffers (used for constant tensors). Referenced by tensors +// by index. The generous alignment accommodates mmap-friendly data structures. +table Buffer { + data:[ubyte] (force_align: 16); + + // In a model that is larger than 2GB, then buffers instead uses the following + // attributes to find stored data, which is outside of flatbuffers + // the offset is calculated relative to the beginning of the file and is only + // valid if > 1. + offset: ulong; + size: ulong; +} + +// Groups external buffers by file/URI. +table ExternalBufferGroup { + name:string; +} + +// Describes an immutable data slice stored in an external file. +table ExternalBuffer { + // Unique identifier for this external buffer. + id:uint; + // Index into the external_buffer_groups array. + group:uint; + offset:ulong; + length:ulong; + packing:string; +} + +table Metadata { + // A human readable string to uniquely identify a Metadata. + name:string; + // An index to the buffers table. + buffer:uint; +} + +// Map from an alias name of tensor to tensor index in the graph. +// This is used in Signature def. +table TensorMap { + // Represents the alias to use for this tensor. + name:string; + + // The actual tensor index in the primary graph, that 'name' corresponds to. + tensor_index:uint; +} + +// This corresponds to SignatureDef in Tensorflow SavedModel. +// The SignatureDef will be part of the SavedModel provided for conversion. +table SignatureDef { + // Named inputs for this signature. + inputs:[TensorMap]; + + // Named outputs for this signature. + outputs:[TensorMap]; + + // Key value which was in the Tensorflow SavedModel SignatureDef map. + signature_key:string; + + // Model tag, deprecated. + deprecated_tag:string (deprecated); + + // Index of subgraphs that corresponds to the exported method. + subgraph_index:uint; +} + +table Model { + // Version of the schema. + version:uint; + + // A list of all operator codes used in this model. This is + // kept in order because operators carry an index into this + // vector. + operator_codes:[OperatorCode]; + + // All the subgraphs of the model. The 0th is assumed to be the main + // model. + subgraphs:[SubGraph]; + + // A description of the model. + description:string; + + // Buffers of the model. + // Note the 0th entry of this array must be an empty buffer (sentinel). + // This is a convention so that tensors without a buffer can provide 0 as + // their buffer. + buffers:[Buffer]; + + // Metadata about the model. Indirects into the existings buffers list. + // Deprecated, prefer to use metadata field. + metadata_buffer:[int]; + + // Metadata about the model. + metadata:[Metadata]; + + // Optional SignatureDefs for the model. + signature_defs:[SignatureDef]; + + // Optional groups for external weight buffers. + external_buffer_groups:[ExternalBufferGroup]; + + // Optional list of external weight buffers referenced by tensors. + external_buffers:[ExternalBuffer]; +} + +root_type Model; diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_conversion_utils.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_conversion_utils.cc new file mode 100644 index 0000000000000000000000000000000000000000..0142bd5550bc93fc31e18677b3da7e5d5fbacbb1 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_conversion_utils.cc @@ -0,0 +1,68 @@ +/* Copyright 2020 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/compiler/mlir/lite/schema/schema_conversion_utils.h" + +#include + +namespace tflite { + +int8_t ConvertBuiltinCodeToDeprecatedBuiltinCode( + const BuiltinOperator builtin_code) { + return (builtin_code < BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES) + ? static_cast(builtin_code) + : static_cast( + BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES); +} + +// The following methods are the following `OperatorCode` table object creation +// methods for backward compatibility. These are manually copied from the +// flatbuffer generated code from schema v3. They serve as overloads for the +// v3a's CreateOperatorCode functions in schema_generated.h and enable code that +// still assumes flatbuffer schema v3 to be unchanged with the inclusion of the +// schema_utils header. +// TODO(b/162392898): remove once all callers are updated to use schema v3a +// functions. + +flatbuffers::Offset CreateOperatorCode( + flatbuffers::FlatBufferBuilder &_fbb, BuiltinOperator builtin_code, + flatbuffers::Offset custom_code, int32_t version) { + OperatorCodeBuilder builder_(_fbb); + builder_.add_version(version); + + int8_t deprecated_builtin_code = + static_cast(BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES); + if (builtin_code < BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES) { + deprecated_builtin_code = static_cast(builtin_code); + } + builder_.add_deprecated_builtin_code(deprecated_builtin_code); + builder_.add_custom_code(custom_code); + builder_.add_builtin_code(builtin_code); + return builder_.Finish(); +} + +flatbuffers::Offset CreateOperatorCodeDirect( + flatbuffers::FlatBufferBuilder &_fbb, BuiltinOperator builtin_code, + const char *custom_code, int32_t version) { + auto custom_code__ = custom_code ? _fbb.CreateString(custom_code) : 0; + int8_t deprecated_builtin_code = + static_cast(BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES); + if (builtin_code < BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES) { + deprecated_builtin_code = static_cast(builtin_code); + } + return CreateOperatorCode(_fbb, deprecated_builtin_code, custom_code__, + version, builtin_code); +} + +} // namespace tflite diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_conversion_utils.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_conversion_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..f03622baecd5f7359068cf0ba44407834595a42a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_conversion_utils.h @@ -0,0 +1,40 @@ +/* Copyright 2020 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_SCHEMA_SCHEMA_CONVERSION_UTILS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_SCHEMA_SCHEMA_CONVERSION_UTILS_H_ + +#include "flatbuffers/flatbuffers.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" + +namespace tflite { + +int8_t ConvertBuiltinCodeToDeprecatedBuiltinCode(BuiltinOperator builtin_code); + +// The following methods are for backward compatibility for the early version +// three, which does not have an extended builtin code. +flatbuffers::Offset CreateOperatorCode( + flatbuffers::FlatBufferBuilder &_fbb, + BuiltinOperator builtin_code = BuiltinOperator_ADD, + flatbuffers::Offset custom_code = 0, + int32_t version = 1); + +flatbuffers::Offset CreateOperatorCodeDirect( + flatbuffers::FlatBufferBuilder &_fbb, + BuiltinOperator builtin_code = BuiltinOperator_ADD, + const char *custom_code = nullptr, int32_t version = 1); + +} // namespace tflite + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_SCHEMA_SCHEMA_CONVERSION_UTILS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_generated.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_generated.h new file mode 100644 index 0000000000000000000000000000000000000000..358e932874cbf28f0bb30ef287d9d67082315d22 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_generated.h @@ -0,0 +1,26018 @@ +/* Copyright 2024 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. +==============================================================================*/ +// automatically generated by the FlatBuffers compiler, do not modify + + +#ifndef FLATBUFFERS_GENERATED_SCHEMA_TFLITE_H_ +#define FLATBUFFERS_GENERATED_SCHEMA_TFLITE_H_ + +#include "flatbuffers/flatbuffers.h" + +// Ensure the included flatbuffers.h is the same version as when this file was +// generated, otherwise it may not be compatible. +static_assert(FLATBUFFERS_VERSION_MAJOR == 25 && + FLATBUFFERS_VERSION_MINOR == 9 && + FLATBUFFERS_VERSION_REVISION == 23, + "Non-compatible flatbuffers version included"); + +namespace tflite { + +struct CustomQuantization; +struct CustomQuantizationBuilder; +struct CustomQuantizationT; + +struct BlockwiseQuantization; +struct BlockwiseQuantizationBuilder; +struct BlockwiseQuantizationT; + +struct MultiAxisQuantization; +struct MultiAxisQuantizationBuilder; +struct MultiAxisQuantizationT; + +struct QuantizationParameters; +struct QuantizationParametersBuilder; +struct QuantizationParametersT; + +struct Int32Vector; +struct Int32VectorBuilder; +struct Int32VectorT; + +struct Uint16Vector; +struct Uint16VectorBuilder; +struct Uint16VectorT; + +struct Uint8Vector; +struct Uint8VectorBuilder; +struct Uint8VectorT; + +struct DimensionMetadata; +struct DimensionMetadataBuilder; +struct DimensionMetadataT; + +struct SparsityParameters; +struct SparsityParametersBuilder; +struct SparsityParametersT; + +struct VariantSubType; +struct VariantSubTypeBuilder; +struct VariantSubTypeT; + +struct Tensor; +struct TensorBuilder; +struct TensorT; + +struct StablehloGatherOptions; +struct StablehloGatherOptionsBuilder; +struct StablehloGatherOptionsT; + +struct StablehloTransposeOptions; +struct StablehloTransposeOptionsBuilder; +struct StablehloTransposeOptionsT; + +struct StablehloDotGeneralOptions; +struct StablehloDotGeneralOptionsBuilder; +struct StablehloDotGeneralOptionsT; + +struct StablehloReduceWindowOptions; +struct StablehloReduceWindowOptionsBuilder; +struct StablehloReduceWindowOptionsT; + +struct StablehloWhileOptions; +struct StablehloWhileOptionsBuilder; +struct StablehloWhileOptionsT; + +struct StablehloSortOptions; +struct StablehloSortOptionsBuilder; +struct StablehloSortOptionsT; + +struct StablehloConcatenateOptions; +struct StablehloConcatenateOptionsBuilder; +struct StablehloConcatenateOptionsT; + +struct StablehloBroadcastInDimOptions; +struct StablehloBroadcastInDimOptionsBuilder; +struct StablehloBroadcastInDimOptionsT; + +struct StablehloCompareOptions; +struct StablehloCompareOptionsBuilder; +struct StablehloCompareOptionsT; + +struct StablehloDynamicSliceOptions; +struct StablehloDynamicSliceOptionsBuilder; +struct StablehloDynamicSliceOptionsT; + +struct StablehloPadOptions; +struct StablehloPadOptionsBuilder; +struct StablehloPadOptionsT; + +struct StablehloIotaOptions; +struct StablehloIotaOptionsBuilder; +struct StablehloIotaOptionsT; + +struct StablehloCustomCallOptions; +struct StablehloCustomCallOptionsBuilder; +struct StablehloCustomCallOptionsT; + +struct StablehloReduceOptions; +struct StablehloReduceOptionsBuilder; +struct StablehloReduceOptionsT; + +struct StablehloSliceOptions; +struct StablehloSliceOptionsBuilder; +struct StablehloSliceOptionsT; + +struct StablehloConvolutionOptions; +struct StablehloConvolutionOptionsBuilder; +struct StablehloConvolutionOptionsT; + +struct StablehloScatterOptions; +struct StablehloScatterOptionsBuilder; +struct StablehloScatterOptionsT; + +struct StablehloCaseOptions; +struct StablehloCaseOptionsBuilder; +struct StablehloCaseOptionsT; + +struct StablehloRngBitGeneratorOptions; +struct StablehloRngBitGeneratorOptionsBuilder; +struct StablehloRngBitGeneratorOptionsT; + +struct Conv2DOptions; +struct Conv2DOptionsBuilder; +struct Conv2DOptionsT; + +struct Conv3DOptions; +struct Conv3DOptionsBuilder; +struct Conv3DOptionsT; + +struct Pool2DOptions; +struct Pool2DOptionsBuilder; +struct Pool2DOptionsT; + +struct DepthwiseConv2DOptions; +struct DepthwiseConv2DOptionsBuilder; +struct DepthwiseConv2DOptionsT; + +struct ConcatEmbeddingsOptions; +struct ConcatEmbeddingsOptionsBuilder; +struct ConcatEmbeddingsOptionsT; + +struct LSHProjectionOptions; +struct LSHProjectionOptionsBuilder; +struct LSHProjectionOptionsT; + +struct SVDFOptions; +struct SVDFOptionsBuilder; +struct SVDFOptionsT; + +struct RNNOptions; +struct RNNOptionsBuilder; +struct RNNOptionsT; + +struct SequenceRNNOptions; +struct SequenceRNNOptionsBuilder; +struct SequenceRNNOptionsT; + +struct BidirectionalSequenceRNNOptions; +struct BidirectionalSequenceRNNOptionsBuilder; +struct BidirectionalSequenceRNNOptionsT; + +struct FullyConnectedOptions; +struct FullyConnectedOptionsBuilder; +struct FullyConnectedOptionsT; + +struct SoftmaxOptions; +struct SoftmaxOptionsBuilder; +struct SoftmaxOptionsT; + +struct ConcatenationOptions; +struct ConcatenationOptionsBuilder; +struct ConcatenationOptionsT; + +struct AddOptions; +struct AddOptionsBuilder; +struct AddOptionsT; + +struct MulOptions; +struct MulOptionsBuilder; +struct MulOptionsT; + +struct L2NormOptions; +struct L2NormOptionsBuilder; +struct L2NormOptionsT; + +struct LocalResponseNormalizationOptions; +struct LocalResponseNormalizationOptionsBuilder; +struct LocalResponseNormalizationOptionsT; + +struct LSTMOptions; +struct LSTMOptionsBuilder; +struct LSTMOptionsT; + +struct UnidirectionalSequenceLSTMOptions; +struct UnidirectionalSequenceLSTMOptionsBuilder; +struct UnidirectionalSequenceLSTMOptionsT; + +struct BidirectionalSequenceLSTMOptions; +struct BidirectionalSequenceLSTMOptionsBuilder; +struct BidirectionalSequenceLSTMOptionsT; + +struct ResizeBilinearOptions; +struct ResizeBilinearOptionsBuilder; +struct ResizeBilinearOptionsT; + +struct ResizeNearestNeighborOptions; +struct ResizeNearestNeighborOptionsBuilder; +struct ResizeNearestNeighborOptionsT; + +struct CallOptions; +struct CallOptionsBuilder; +struct CallOptionsT; + +struct PadOptions; +struct PadOptionsBuilder; +struct PadOptionsT; + +struct PadV2Options; +struct PadV2OptionsBuilder; +struct PadV2OptionsT; + +struct ReshapeOptions; +struct ReshapeOptionsBuilder; +struct ReshapeOptionsT; + +struct SpaceToBatchNDOptions; +struct SpaceToBatchNDOptionsBuilder; +struct SpaceToBatchNDOptionsT; + +struct BatchToSpaceNDOptions; +struct BatchToSpaceNDOptionsBuilder; +struct BatchToSpaceNDOptionsT; + +struct SkipGramOptions; +struct SkipGramOptionsBuilder; +struct SkipGramOptionsT; + +struct SpaceToDepthOptions; +struct SpaceToDepthOptionsBuilder; +struct SpaceToDepthOptionsT; + +struct DepthToSpaceOptions; +struct DepthToSpaceOptionsBuilder; +struct DepthToSpaceOptionsT; + +struct SubOptions; +struct SubOptionsBuilder; +struct SubOptionsT; + +struct DivOptions; +struct DivOptionsBuilder; +struct DivOptionsT; + +struct TopKV2Options; +struct TopKV2OptionsBuilder; +struct TopKV2OptionsT; + +struct EmbeddingLookupSparseOptions; +struct EmbeddingLookupSparseOptionsBuilder; +struct EmbeddingLookupSparseOptionsT; + +struct GatherOptions; +struct GatherOptionsBuilder; +struct GatherOptionsT; + +struct TransposeOptions; +struct TransposeOptionsBuilder; +struct TransposeOptionsT; + +struct ExpOptions; +struct ExpOptionsBuilder; +struct ExpOptionsT; + +struct CosOptions; +struct CosOptionsBuilder; +struct CosOptionsT; + +struct ReducerOptions; +struct ReducerOptionsBuilder; +struct ReducerOptionsT; + +struct SqueezeOptions; +struct SqueezeOptionsBuilder; +struct SqueezeOptionsT; + +struct SplitOptions; +struct SplitOptionsBuilder; +struct SplitOptionsT; + +struct SplitVOptions; +struct SplitVOptionsBuilder; +struct SplitVOptionsT; + +struct StridedSliceOptions; +struct StridedSliceOptionsBuilder; +struct StridedSliceOptionsT; + +struct LogSoftmaxOptions; +struct LogSoftmaxOptionsBuilder; +struct LogSoftmaxOptionsT; + +struct CastOptions; +struct CastOptionsBuilder; +struct CastOptionsT; + +struct DequantizeOptions; +struct DequantizeOptionsBuilder; +struct DequantizeOptionsT; + +struct MaximumMinimumOptions; +struct MaximumMinimumOptionsBuilder; +struct MaximumMinimumOptionsT; + +struct TileOptions; +struct TileOptionsBuilder; +struct TileOptionsT; + +struct ArgMaxOptions; +struct ArgMaxOptionsBuilder; +struct ArgMaxOptionsT; + +struct ArgMinOptions; +struct ArgMinOptionsBuilder; +struct ArgMinOptionsT; + +struct GreaterOptions; +struct GreaterOptionsBuilder; +struct GreaterOptionsT; + +struct GreaterEqualOptions; +struct GreaterEqualOptionsBuilder; +struct GreaterEqualOptionsT; + +struct LessOptions; +struct LessOptionsBuilder; +struct LessOptionsT; + +struct LessEqualOptions; +struct LessEqualOptionsBuilder; +struct LessEqualOptionsT; + +struct NegOptions; +struct NegOptionsBuilder; +struct NegOptionsT; + +struct SelectOptions; +struct SelectOptionsBuilder; +struct SelectOptionsT; + +struct SliceOptions; +struct SliceOptionsBuilder; +struct SliceOptionsT; + +struct TransposeConvOptions; +struct TransposeConvOptionsBuilder; +struct TransposeConvOptionsT; + +struct ExpandDimsOptions; +struct ExpandDimsOptionsBuilder; +struct ExpandDimsOptionsT; + +struct SparseToDenseOptions; +struct SparseToDenseOptionsBuilder; +struct SparseToDenseOptionsT; + +struct EqualOptions; +struct EqualOptionsBuilder; +struct EqualOptionsT; + +struct NotEqualOptions; +struct NotEqualOptionsBuilder; +struct NotEqualOptionsT; + +struct ShapeOptions; +struct ShapeOptionsBuilder; +struct ShapeOptionsT; + +struct RankOptions; +struct RankOptionsBuilder; +struct RankOptionsT; + +struct PowOptions; +struct PowOptionsBuilder; +struct PowOptionsT; + +struct FakeQuantOptions; +struct FakeQuantOptionsBuilder; +struct FakeQuantOptionsT; + +struct PackOptions; +struct PackOptionsBuilder; +struct PackOptionsT; + +struct LogicalOrOptions; +struct LogicalOrOptionsBuilder; +struct LogicalOrOptionsT; + +struct OneHotOptions; +struct OneHotOptionsBuilder; +struct OneHotOptionsT; + +struct AbsOptions; +struct AbsOptionsBuilder; +struct AbsOptionsT; + +struct HardSwishOptions; +struct HardSwishOptionsBuilder; +struct HardSwishOptionsT; + +struct LogicalAndOptions; +struct LogicalAndOptionsBuilder; +struct LogicalAndOptionsT; + +struct LogicalNotOptions; +struct LogicalNotOptionsBuilder; +struct LogicalNotOptionsT; + +struct UnpackOptions; +struct UnpackOptionsBuilder; +struct UnpackOptionsT; + +struct FloorDivOptions; +struct FloorDivOptionsBuilder; +struct FloorDivOptionsT; + +struct SquareOptions; +struct SquareOptionsBuilder; +struct SquareOptionsT; + +struct ZerosLikeOptions; +struct ZerosLikeOptionsBuilder; +struct ZerosLikeOptionsT; + +struct FillOptions; +struct FillOptionsBuilder; +struct FillOptionsT; + +struct FloorModOptions; +struct FloorModOptionsBuilder; +struct FloorModOptionsT; + +struct RangeOptions; +struct RangeOptionsBuilder; +struct RangeOptionsT; + +struct LeakyReluOptions; +struct LeakyReluOptionsBuilder; +struct LeakyReluOptionsT; + +struct SquaredDifferenceOptions; +struct SquaredDifferenceOptionsBuilder; +struct SquaredDifferenceOptionsT; + +struct MirrorPadOptions; +struct MirrorPadOptionsBuilder; +struct MirrorPadOptionsT; + +struct UniqueOptions; +struct UniqueOptionsBuilder; +struct UniqueOptionsT; + +struct ReverseV2Options; +struct ReverseV2OptionsBuilder; +struct ReverseV2OptionsT; + +struct AddNOptions; +struct AddNOptionsBuilder; +struct AddNOptionsT; + +struct GatherNdOptions; +struct GatherNdOptionsBuilder; +struct GatherNdOptionsT; + +struct WhereOptions; +struct WhereOptionsBuilder; +struct WhereOptionsT; + +struct ReverseSequenceOptions; +struct ReverseSequenceOptionsBuilder; +struct ReverseSequenceOptionsT; + +struct MatrixDiagOptions; +struct MatrixDiagOptionsBuilder; +struct MatrixDiagOptionsT; + +struct QuantizeOptions; +struct QuantizeOptionsBuilder; +struct QuantizeOptionsT; + +struct MatrixSetDiagOptions; +struct MatrixSetDiagOptionsBuilder; +struct MatrixSetDiagOptionsT; + +struct IfOptions; +struct IfOptionsBuilder; +struct IfOptionsT; + +struct CallOnceOptions; +struct CallOnceOptionsBuilder; +struct CallOnceOptionsT; + +struct WhileOptions; +struct WhileOptionsBuilder; +struct WhileOptionsT; + +struct NonMaxSuppressionV4Options; +struct NonMaxSuppressionV4OptionsBuilder; +struct NonMaxSuppressionV4OptionsT; + +struct NonMaxSuppressionV5Options; +struct NonMaxSuppressionV5OptionsBuilder; +struct NonMaxSuppressionV5OptionsT; + +struct ScatterNdOptions; +struct ScatterNdOptionsBuilder; +struct ScatterNdOptionsT; + +struct SelectV2Options; +struct SelectV2OptionsBuilder; +struct SelectV2OptionsT; + +struct DensifyOptions; +struct DensifyOptionsBuilder; +struct DensifyOptionsT; + +struct SegmentSumOptions; +struct SegmentSumOptionsBuilder; +struct SegmentSumOptionsT; + +struct BatchMatMulOptions; +struct BatchMatMulOptionsBuilder; +struct BatchMatMulOptionsT; + +struct CumsumOptions; +struct CumsumOptionsBuilder; +struct CumsumOptionsT; + +struct BroadcastToOptions; +struct BroadcastToOptionsBuilder; +struct BroadcastToOptionsT; + +struct Rfft2dOptions; +struct Rfft2dOptionsBuilder; +struct Rfft2dOptionsT; + +struct HashtableOptions; +struct HashtableOptionsBuilder; +struct HashtableOptionsT; + +struct HashtableFindOptions; +struct HashtableFindOptionsBuilder; +struct HashtableFindOptionsT; + +struct HashtableImportOptions; +struct HashtableImportOptionsBuilder; +struct HashtableImportOptionsT; + +struct HashtableSizeOptions; +struct HashtableSizeOptionsBuilder; +struct HashtableSizeOptionsT; + +struct VarHandleOptions; +struct VarHandleOptionsBuilder; +struct VarHandleOptionsT; + +struct ReadVariableOptions; +struct ReadVariableOptionsBuilder; +struct ReadVariableOptionsT; + +struct AssignVariableOptions; +struct AssignVariableOptionsBuilder; +struct AssignVariableOptionsT; + +struct RandomOptions; +struct RandomOptionsBuilder; +struct RandomOptionsT; + +struct BucketizeOptions; +struct BucketizeOptionsBuilder; +struct BucketizeOptionsT; + +struct GeluOptions; +struct GeluOptionsBuilder; +struct GeluOptionsT; + +struct DynamicUpdateSliceOptions; +struct DynamicUpdateSliceOptionsBuilder; +struct DynamicUpdateSliceOptionsT; + +struct UnsortedSegmentProdOptions; +struct UnsortedSegmentProdOptionsBuilder; +struct UnsortedSegmentProdOptionsT; + +struct UnsortedSegmentMaxOptions; +struct UnsortedSegmentMaxOptionsBuilder; +struct UnsortedSegmentMaxOptionsT; + +struct UnsortedSegmentSumOptions; +struct UnsortedSegmentSumOptionsBuilder; +struct UnsortedSegmentSumOptionsT; + +struct ATan2Options; +struct ATan2OptionsBuilder; +struct ATan2OptionsT; + +struct UnsortedSegmentMinOptions; +struct UnsortedSegmentMinOptionsBuilder; +struct UnsortedSegmentMinOptionsT; + +struct SignOptions; +struct SignOptionsBuilder; +struct SignOptionsT; + +struct BitcastOptions; +struct BitcastOptionsBuilder; +struct BitcastOptionsT; + +struct BitwiseXorOptions; +struct BitwiseXorOptionsBuilder; +struct BitwiseXorOptionsT; + +struct RightShiftOptions; +struct RightShiftOptionsBuilder; +struct RightShiftOptionsT; + +struct DilateOptions; +struct DilateOptionsBuilder; +struct DilateOptionsT; + +struct ReduceWindowOptions; +struct ReduceWindowOptionsBuilder; +struct ReduceWindowOptionsT; + +struct OperatorCode; +struct OperatorCodeBuilder; +struct OperatorCodeT; + +struct StableHLOCompositeOptions; +struct StableHLOCompositeOptionsBuilder; +struct StableHLOCompositeOptionsT; + +struct StablehloShiftLeftOptions; +struct StablehloShiftLeftOptionsBuilder; +struct StablehloShiftLeftOptionsT; + +struct Operator; +struct OperatorBuilder; +struct OperatorT; + +struct SubGraph; +struct SubGraphBuilder; +struct SubGraphT; + +struct Buffer; +struct BufferBuilder; +struct BufferT; + +struct ExternalBufferGroup; +struct ExternalBufferGroupBuilder; +struct ExternalBufferGroupT; + +struct ExternalBuffer; +struct ExternalBufferBuilder; +struct ExternalBufferT; + +struct Metadata; +struct MetadataBuilder; +struct MetadataT; + +struct TensorMap; +struct TensorMapBuilder; +struct TensorMapT; + +struct SignatureDef; +struct SignatureDefBuilder; +struct SignatureDefT; + +struct Model; +struct ModelBuilder; +struct ModelT; + +enum TensorType : int8_t { + TensorType_FLOAT32 = 0, + TensorType_FLOAT16 = 1, + TensorType_INT32 = 2, + TensorType_UINT8 = 3, + TensorType_INT64 = 4, + TensorType_STRING = 5, + TensorType_BOOL = 6, + TensorType_INT16 = 7, + TensorType_COMPLEX64 = 8, + TensorType_INT8 = 9, + TensorType_FLOAT64 = 10, + TensorType_COMPLEX128 = 11, + TensorType_UINT64 = 12, + TensorType_RESOURCE = 13, + TensorType_VARIANT = 14, + TensorType_UINT32 = 15, + TensorType_UINT16 = 16, + TensorType_INT4 = 17, + TensorType_BFLOAT16 = 18, + TensorType_INT2 = 19, + TensorType_UINT4 = 20, + TensorType_FLOAT8_E4M3FN = 21, + TensorType_FLOAT8_E5M2 = 22, + TensorType_MIN = TensorType_FLOAT32, + TensorType_MAX = TensorType_FLOAT8_E5M2 +}; + +inline const TensorType (&EnumValuesTensorType())[23] { + static const TensorType values[] = { + TensorType_FLOAT32, + TensorType_FLOAT16, + TensorType_INT32, + TensorType_UINT8, + TensorType_INT64, + TensorType_STRING, + TensorType_BOOL, + TensorType_INT16, + TensorType_COMPLEX64, + TensorType_INT8, + TensorType_FLOAT64, + TensorType_COMPLEX128, + TensorType_UINT64, + TensorType_RESOURCE, + TensorType_VARIANT, + TensorType_UINT32, + TensorType_UINT16, + TensorType_INT4, + TensorType_BFLOAT16, + TensorType_INT2, + TensorType_UINT4, + TensorType_FLOAT8_E4M3FN, + TensorType_FLOAT8_E5M2 + }; + return values; +} + +inline const char * const *EnumNamesTensorType() { + static const char * const names[24] = { + "FLOAT32", + "FLOAT16", + "INT32", + "UINT8", + "INT64", + "STRING", + "BOOL", + "INT16", + "COMPLEX64", + "INT8", + "FLOAT64", + "COMPLEX128", + "UINT64", + "RESOURCE", + "VARIANT", + "UINT32", + "UINT16", + "INT4", + "BFLOAT16", + "INT2", + "UINT4", + "FLOAT8_E4M3FN", + "FLOAT8_E5M2", + nullptr + }; + return names; +} + +inline const char *EnumNameTensorType(TensorType e) { + if (::flatbuffers::IsOutRange(e, TensorType_FLOAT32, TensorType_FLOAT8_E5M2)) return ""; + const size_t index = static_cast(e); + return EnumNamesTensorType()[index]; +} + +enum QuantizationDetails : uint8_t { + QuantizationDetails_NONE = 0, + QuantizationDetails_CustomQuantization = 1, + QuantizationDetails_BlockwiseQuantization = 2, + QuantizationDetails_MultiAxisQuantization = 3, + QuantizationDetails_MIN = QuantizationDetails_NONE, + QuantizationDetails_MAX = QuantizationDetails_MultiAxisQuantization +}; + +inline const QuantizationDetails (&EnumValuesQuantizationDetails())[4] { + static const QuantizationDetails values[] = { + QuantizationDetails_NONE, + QuantizationDetails_CustomQuantization, + QuantizationDetails_BlockwiseQuantization, + QuantizationDetails_MultiAxisQuantization + }; + return values; +} + +inline const char * const *EnumNamesQuantizationDetails() { + static const char * const names[5] = { + "NONE", + "CustomQuantization", + "BlockwiseQuantization", + "MultiAxisQuantization", + nullptr + }; + return names; +} + +inline const char *EnumNameQuantizationDetails(QuantizationDetails e) { + if (::flatbuffers::IsOutRange(e, QuantizationDetails_NONE, QuantizationDetails_MultiAxisQuantization)) return ""; + const size_t index = static_cast(e); + return EnumNamesQuantizationDetails()[index]; +} + +template struct QuantizationDetailsTraits { + static const QuantizationDetails enum_value = QuantizationDetails_NONE; +}; + +template<> struct QuantizationDetailsTraits { + static const QuantizationDetails enum_value = QuantizationDetails_CustomQuantization; +}; + +template<> struct QuantizationDetailsTraits { + static const QuantizationDetails enum_value = QuantizationDetails_BlockwiseQuantization; +}; + +template<> struct QuantizationDetailsTraits { + static const QuantizationDetails enum_value = QuantizationDetails_MultiAxisQuantization; +}; + +template struct QuantizationDetailsUnionTraits { + static const QuantizationDetails enum_value = QuantizationDetails_NONE; +}; + +template<> struct QuantizationDetailsUnionTraits { + static const QuantizationDetails enum_value = QuantizationDetails_CustomQuantization; +}; + +template<> struct QuantizationDetailsUnionTraits { + static const QuantizationDetails enum_value = QuantizationDetails_BlockwiseQuantization; +}; + +template<> struct QuantizationDetailsUnionTraits { + static const QuantizationDetails enum_value = QuantizationDetails_MultiAxisQuantization; +}; + +struct QuantizationDetailsUnion { + QuantizationDetails type; + void *value; + + QuantizationDetailsUnion() : type(QuantizationDetails_NONE), value(nullptr) {} + QuantizationDetailsUnion(QuantizationDetailsUnion&& u) FLATBUFFERS_NOEXCEPT : + type(QuantizationDetails_NONE), value(nullptr) + { std::swap(type, u.type); std::swap(value, u.value); } + QuantizationDetailsUnion(const QuantizationDetailsUnion &); + QuantizationDetailsUnion &operator=(const QuantizationDetailsUnion &u) + { QuantizationDetailsUnion t(u); std::swap(type, t.type); std::swap(value, t.value); return *this; } + QuantizationDetailsUnion &operator=(QuantizationDetailsUnion &&u) FLATBUFFERS_NOEXCEPT + { std::swap(type, u.type); std::swap(value, u.value); return *this; } + ~QuantizationDetailsUnion() { Reset(); } + + void Reset(); + + template + void Set(T&& val) { + typedef typename std::remove_reference::type RT; + Reset(); + type = QuantizationDetailsUnionTraits::enum_value; + if (type != QuantizationDetails_NONE) { + value = new RT(std::forward(val)); + } + } + + static void *UnPack(const void *obj, enum QuantizationDetails type, const ::flatbuffers::resolver_function_t *resolver); + ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr) const; + + tflite::CustomQuantizationT *AsCustomQuantization() { + return type == QuantizationDetails_CustomQuantization ? + reinterpret_cast(value) : nullptr; + } + const tflite::CustomQuantizationT *AsCustomQuantization() const { + return type == QuantizationDetails_CustomQuantization ? + reinterpret_cast(value) : nullptr; + } + tflite::BlockwiseQuantizationT *AsBlockwiseQuantization() { + return type == QuantizationDetails_BlockwiseQuantization ? + reinterpret_cast(value) : nullptr; + } + const tflite::BlockwiseQuantizationT *AsBlockwiseQuantization() const { + return type == QuantizationDetails_BlockwiseQuantization ? + reinterpret_cast(value) : nullptr; + } + tflite::MultiAxisQuantizationT *AsMultiAxisQuantization() { + return type == QuantizationDetails_MultiAxisQuantization ? + reinterpret_cast(value) : nullptr; + } + const tflite::MultiAxisQuantizationT *AsMultiAxisQuantization() const { + return type == QuantizationDetails_MultiAxisQuantization ? + reinterpret_cast(value) : nullptr; + } +}; + +template +bool VerifyQuantizationDetails(::flatbuffers::VerifierTemplate &verifier, const void *obj, QuantizationDetails type); +template +bool VerifyQuantizationDetailsVector(::flatbuffers::VerifierTemplate &verifier, const ::flatbuffers::Vector<::flatbuffers::Offset> *values, const ::flatbuffers::Vector *types); + +enum DimensionType : int8_t { + DimensionType_DENSE = 0, + DimensionType_SPARSE_CSR = 1, + DimensionType_MIN = DimensionType_DENSE, + DimensionType_MAX = DimensionType_SPARSE_CSR +}; + +inline const DimensionType (&EnumValuesDimensionType())[2] { + static const DimensionType values[] = { + DimensionType_DENSE, + DimensionType_SPARSE_CSR + }; + return values; +} + +inline const char * const *EnumNamesDimensionType() { + static const char * const names[3] = { + "DENSE", + "SPARSE_CSR", + nullptr + }; + return names; +} + +inline const char *EnumNameDimensionType(DimensionType e) { + if (::flatbuffers::IsOutRange(e, DimensionType_DENSE, DimensionType_SPARSE_CSR)) return ""; + const size_t index = static_cast(e); + return EnumNamesDimensionType()[index]; +} + +enum SparseIndexVector : uint8_t { + SparseIndexVector_NONE = 0, + SparseIndexVector_Int32Vector = 1, + SparseIndexVector_Uint16Vector = 2, + SparseIndexVector_Uint8Vector = 3, + SparseIndexVector_MIN = SparseIndexVector_NONE, + SparseIndexVector_MAX = SparseIndexVector_Uint8Vector +}; + +inline const SparseIndexVector (&EnumValuesSparseIndexVector())[4] { + static const SparseIndexVector values[] = { + SparseIndexVector_NONE, + SparseIndexVector_Int32Vector, + SparseIndexVector_Uint16Vector, + SparseIndexVector_Uint8Vector + }; + return values; +} + +inline const char * const *EnumNamesSparseIndexVector() { + static const char * const names[5] = { + "NONE", + "Int32Vector", + "Uint16Vector", + "Uint8Vector", + nullptr + }; + return names; +} + +inline const char *EnumNameSparseIndexVector(SparseIndexVector e) { + if (::flatbuffers::IsOutRange(e, SparseIndexVector_NONE, SparseIndexVector_Uint8Vector)) return ""; + const size_t index = static_cast(e); + return EnumNamesSparseIndexVector()[index]; +} + +template struct SparseIndexVectorTraits { + static const SparseIndexVector enum_value = SparseIndexVector_NONE; +}; + +template<> struct SparseIndexVectorTraits { + static const SparseIndexVector enum_value = SparseIndexVector_Int32Vector; +}; + +template<> struct SparseIndexVectorTraits { + static const SparseIndexVector enum_value = SparseIndexVector_Uint16Vector; +}; + +template<> struct SparseIndexVectorTraits { + static const SparseIndexVector enum_value = SparseIndexVector_Uint8Vector; +}; + +template struct SparseIndexVectorUnionTraits { + static const SparseIndexVector enum_value = SparseIndexVector_NONE; +}; + +template<> struct SparseIndexVectorUnionTraits { + static const SparseIndexVector enum_value = SparseIndexVector_Int32Vector; +}; + +template<> struct SparseIndexVectorUnionTraits { + static const SparseIndexVector enum_value = SparseIndexVector_Uint16Vector; +}; + +template<> struct SparseIndexVectorUnionTraits { + static const SparseIndexVector enum_value = SparseIndexVector_Uint8Vector; +}; + +struct SparseIndexVectorUnion { + SparseIndexVector type; + void *value; + + SparseIndexVectorUnion() : type(SparseIndexVector_NONE), value(nullptr) {} + SparseIndexVectorUnion(SparseIndexVectorUnion&& u) FLATBUFFERS_NOEXCEPT : + type(SparseIndexVector_NONE), value(nullptr) + { std::swap(type, u.type); std::swap(value, u.value); } + SparseIndexVectorUnion(const SparseIndexVectorUnion &); + SparseIndexVectorUnion &operator=(const SparseIndexVectorUnion &u) + { SparseIndexVectorUnion t(u); std::swap(type, t.type); std::swap(value, t.value); return *this; } + SparseIndexVectorUnion &operator=(SparseIndexVectorUnion &&u) FLATBUFFERS_NOEXCEPT + { std::swap(type, u.type); std::swap(value, u.value); return *this; } + ~SparseIndexVectorUnion() { Reset(); } + + void Reset(); + + template + void Set(T&& val) { + typedef typename std::remove_reference::type RT; + Reset(); + type = SparseIndexVectorUnionTraits::enum_value; + if (type != SparseIndexVector_NONE) { + value = new RT(std::forward(val)); + } + } + + static void *UnPack(const void *obj, enum SparseIndexVector type, const ::flatbuffers::resolver_function_t *resolver); + ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr) const; + + tflite::Int32VectorT *AsInt32Vector() { + return type == SparseIndexVector_Int32Vector ? + reinterpret_cast(value) : nullptr; + } + const tflite::Int32VectorT *AsInt32Vector() const { + return type == SparseIndexVector_Int32Vector ? + reinterpret_cast(value) : nullptr; + } + tflite::Uint16VectorT *AsUint16Vector() { + return type == SparseIndexVector_Uint16Vector ? + reinterpret_cast(value) : nullptr; + } + const tflite::Uint16VectorT *AsUint16Vector() const { + return type == SparseIndexVector_Uint16Vector ? + reinterpret_cast(value) : nullptr; + } + tflite::Uint8VectorT *AsUint8Vector() { + return type == SparseIndexVector_Uint8Vector ? + reinterpret_cast(value) : nullptr; + } + const tflite::Uint8VectorT *AsUint8Vector() const { + return type == SparseIndexVector_Uint8Vector ? + reinterpret_cast(value) : nullptr; + } +}; + +template +bool VerifySparseIndexVector(::flatbuffers::VerifierTemplate &verifier, const void *obj, SparseIndexVector type); +template +bool VerifySparseIndexVectorVector(::flatbuffers::VerifierTemplate &verifier, const ::flatbuffers::Vector<::flatbuffers::Offset> *values, const ::flatbuffers::Vector *types); + +enum BuiltinOperator : int32_t { + BuiltinOperator_ADD = 0, + BuiltinOperator_AVERAGE_POOL_2D = 1, + BuiltinOperator_CONCATENATION = 2, + BuiltinOperator_CONV_2D = 3, + BuiltinOperator_DEPTHWISE_CONV_2D = 4, + BuiltinOperator_DEPTH_TO_SPACE = 5, + BuiltinOperator_DEQUANTIZE = 6, + BuiltinOperator_EMBEDDING_LOOKUP = 7, + BuiltinOperator_FLOOR = 8, + BuiltinOperator_FULLY_CONNECTED = 9, + BuiltinOperator_HASHTABLE_LOOKUP = 10, + BuiltinOperator_L2_NORMALIZATION = 11, + BuiltinOperator_L2_POOL_2D = 12, + BuiltinOperator_LOCAL_RESPONSE_NORMALIZATION = 13, + BuiltinOperator_LOGISTIC = 14, + BuiltinOperator_LSH_PROJECTION = 15, + BuiltinOperator_LSTM = 16, + BuiltinOperator_MAX_POOL_2D = 17, + BuiltinOperator_MUL = 18, + BuiltinOperator_RELU = 19, + BuiltinOperator_RELU_N1_TO_1 = 20, + BuiltinOperator_RELU6 = 21, + BuiltinOperator_RESHAPE = 22, + BuiltinOperator_RESIZE_BILINEAR = 23, + BuiltinOperator_RNN = 24, + BuiltinOperator_SOFTMAX = 25, + BuiltinOperator_SPACE_TO_DEPTH = 26, + BuiltinOperator_SVDF = 27, + BuiltinOperator_TANH = 28, + BuiltinOperator_CONCAT_EMBEDDINGS = 29, + BuiltinOperator_SKIP_GRAM = 30, + BuiltinOperator_CALL = 31, + BuiltinOperator_CUSTOM = 32, + BuiltinOperator_EMBEDDING_LOOKUP_SPARSE = 33, + BuiltinOperator_PAD = 34, + BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_RNN = 35, + BuiltinOperator_GATHER = 36, + BuiltinOperator_BATCH_TO_SPACE_ND = 37, + BuiltinOperator_SPACE_TO_BATCH_ND = 38, + BuiltinOperator_TRANSPOSE = 39, + BuiltinOperator_MEAN = 40, + BuiltinOperator_SUB = 41, + BuiltinOperator_DIV = 42, + BuiltinOperator_SQUEEZE = 43, + BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM = 44, + BuiltinOperator_STRIDED_SLICE = 45, + BuiltinOperator_BIDIRECTIONAL_SEQUENCE_RNN = 46, + BuiltinOperator_EXP = 47, + BuiltinOperator_TOPK_V2 = 48, + BuiltinOperator_SPLIT = 49, + BuiltinOperator_LOG_SOFTMAX = 50, + BuiltinOperator_DELEGATE = 51, + BuiltinOperator_BIDIRECTIONAL_SEQUENCE_LSTM = 52, + BuiltinOperator_CAST = 53, + BuiltinOperator_PRELU = 54, + BuiltinOperator_MAXIMUM = 55, + BuiltinOperator_ARG_MAX = 56, + BuiltinOperator_MINIMUM = 57, + BuiltinOperator_LESS = 58, + BuiltinOperator_NEG = 59, + BuiltinOperator_PADV2 = 60, + BuiltinOperator_GREATER = 61, + BuiltinOperator_GREATER_EQUAL = 62, + BuiltinOperator_LESS_EQUAL = 63, + BuiltinOperator_SELECT = 64, + BuiltinOperator_SLICE = 65, + BuiltinOperator_SIN = 66, + BuiltinOperator_TRANSPOSE_CONV = 67, + BuiltinOperator_SPARSE_TO_DENSE = 68, + BuiltinOperator_TILE = 69, + BuiltinOperator_EXPAND_DIMS = 70, + BuiltinOperator_EQUAL = 71, + BuiltinOperator_NOT_EQUAL = 72, + BuiltinOperator_LOG = 73, + BuiltinOperator_SUM = 74, + BuiltinOperator_SQRT = 75, + BuiltinOperator_RSQRT = 76, + BuiltinOperator_SHAPE = 77, + BuiltinOperator_POW = 78, + BuiltinOperator_ARG_MIN = 79, + BuiltinOperator_FAKE_QUANT = 80, + BuiltinOperator_REDUCE_PROD = 81, + BuiltinOperator_REDUCE_MAX = 82, + BuiltinOperator_PACK = 83, + BuiltinOperator_LOGICAL_OR = 84, + BuiltinOperator_ONE_HOT = 85, + BuiltinOperator_LOGICAL_AND = 86, + BuiltinOperator_LOGICAL_NOT = 87, + BuiltinOperator_UNPACK = 88, + BuiltinOperator_REDUCE_MIN = 89, + BuiltinOperator_FLOOR_DIV = 90, + BuiltinOperator_REDUCE_ANY = 91, + BuiltinOperator_SQUARE = 92, + BuiltinOperator_ZEROS_LIKE = 93, + BuiltinOperator_FILL = 94, + BuiltinOperator_FLOOR_MOD = 95, + BuiltinOperator_RANGE = 96, + BuiltinOperator_RESIZE_NEAREST_NEIGHBOR = 97, + BuiltinOperator_LEAKY_RELU = 98, + BuiltinOperator_SQUARED_DIFFERENCE = 99, + BuiltinOperator_MIRROR_PAD = 100, + BuiltinOperator_ABS = 101, + BuiltinOperator_SPLIT_V = 102, + BuiltinOperator_UNIQUE = 103, + BuiltinOperator_CEIL = 104, + BuiltinOperator_REVERSE_V2 = 105, + BuiltinOperator_ADD_N = 106, + BuiltinOperator_GATHER_ND = 107, + BuiltinOperator_COS = 108, + BuiltinOperator_WHERE = 109, + BuiltinOperator_RANK = 110, + BuiltinOperator_ELU = 111, + BuiltinOperator_REVERSE_SEQUENCE = 112, + BuiltinOperator_MATRIX_DIAG = 113, + BuiltinOperator_QUANTIZE = 114, + BuiltinOperator_MATRIX_SET_DIAG = 115, + BuiltinOperator_ROUND = 116, + BuiltinOperator_HARD_SWISH = 117, + BuiltinOperator_IF = 118, + BuiltinOperator_WHILE = 119, + BuiltinOperator_NON_MAX_SUPPRESSION_V4 = 120, + BuiltinOperator_NON_MAX_SUPPRESSION_V5 = 121, + BuiltinOperator_SCATTER_ND = 122, + BuiltinOperator_SELECT_V2 = 123, + BuiltinOperator_DENSIFY = 124, + BuiltinOperator_SEGMENT_SUM = 125, + BuiltinOperator_BATCH_MATMUL = 126, + BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES = 127, + BuiltinOperator_CUMSUM = 128, + BuiltinOperator_CALL_ONCE = 129, + BuiltinOperator_BROADCAST_TO = 130, + BuiltinOperator_RFFT2D = 131, + BuiltinOperator_CONV_3D = 132, + BuiltinOperator_IMAG = 133, + BuiltinOperator_REAL = 134, + BuiltinOperator_COMPLEX_ABS = 135, + BuiltinOperator_HASHTABLE = 136, + BuiltinOperator_HASHTABLE_FIND = 137, + BuiltinOperator_HASHTABLE_IMPORT = 138, + BuiltinOperator_HASHTABLE_SIZE = 139, + BuiltinOperator_REDUCE_ALL = 140, + BuiltinOperator_CONV_3D_TRANSPOSE = 141, + BuiltinOperator_VAR_HANDLE = 142, + BuiltinOperator_READ_VARIABLE = 143, + BuiltinOperator_ASSIGN_VARIABLE = 144, + BuiltinOperator_BROADCAST_ARGS = 145, + BuiltinOperator_RANDOM_STANDARD_NORMAL = 146, + BuiltinOperator_BUCKETIZE = 147, + BuiltinOperator_RANDOM_UNIFORM = 148, + BuiltinOperator_MULTINOMIAL = 149, + BuiltinOperator_GELU = 150, + BuiltinOperator_DYNAMIC_UPDATE_SLICE = 151, + BuiltinOperator_RELU_0_TO_1 = 152, + BuiltinOperator_UNSORTED_SEGMENT_PROD = 153, + BuiltinOperator_UNSORTED_SEGMENT_MAX = 154, + BuiltinOperator_UNSORTED_SEGMENT_SUM = 155, + BuiltinOperator_ATAN2 = 156, + BuiltinOperator_UNSORTED_SEGMENT_MIN = 157, + BuiltinOperator_SIGN = 158, + BuiltinOperator_BITCAST = 159, + BuiltinOperator_BITWISE_XOR = 160, + BuiltinOperator_RIGHT_SHIFT = 161, + BuiltinOperator_STABLEHLO_LOGISTIC = 162, + BuiltinOperator_STABLEHLO_ADD = 163, + BuiltinOperator_STABLEHLO_DIVIDE = 164, + BuiltinOperator_STABLEHLO_MULTIPLY = 165, + BuiltinOperator_STABLEHLO_MAXIMUM = 166, + BuiltinOperator_STABLEHLO_RESHAPE = 167, + BuiltinOperator_STABLEHLO_CLAMP = 168, + BuiltinOperator_STABLEHLO_CONCATENATE = 169, + BuiltinOperator_STABLEHLO_BROADCAST_IN_DIM = 170, + BuiltinOperator_STABLEHLO_CONVOLUTION = 171, + BuiltinOperator_STABLEHLO_SLICE = 172, + BuiltinOperator_STABLEHLO_CUSTOM_CALL = 173, + BuiltinOperator_STABLEHLO_REDUCE = 174, + BuiltinOperator_STABLEHLO_ABS = 175, + BuiltinOperator_STABLEHLO_AND = 176, + BuiltinOperator_STABLEHLO_COSINE = 177, + BuiltinOperator_STABLEHLO_EXPONENTIAL = 178, + BuiltinOperator_STABLEHLO_FLOOR = 179, + BuiltinOperator_STABLEHLO_LOG = 180, + BuiltinOperator_STABLEHLO_MINIMUM = 181, + BuiltinOperator_STABLEHLO_NEGATE = 182, + BuiltinOperator_STABLEHLO_OR = 183, + BuiltinOperator_STABLEHLO_POWER = 184, + BuiltinOperator_STABLEHLO_REMAINDER = 185, + BuiltinOperator_STABLEHLO_RSQRT = 186, + BuiltinOperator_STABLEHLO_SELECT = 187, + BuiltinOperator_STABLEHLO_SUBTRACT = 188, + BuiltinOperator_STABLEHLO_TANH = 189, + BuiltinOperator_STABLEHLO_SCATTER = 190, + BuiltinOperator_STABLEHLO_COMPARE = 191, + BuiltinOperator_STABLEHLO_CONVERT = 192, + BuiltinOperator_STABLEHLO_DYNAMIC_SLICE = 193, + BuiltinOperator_STABLEHLO_DYNAMIC_UPDATE_SLICE = 194, + BuiltinOperator_STABLEHLO_PAD = 195, + BuiltinOperator_STABLEHLO_IOTA = 196, + BuiltinOperator_STABLEHLO_DOT_GENERAL = 197, + BuiltinOperator_STABLEHLO_REDUCE_WINDOW = 198, + BuiltinOperator_STABLEHLO_SORT = 199, + BuiltinOperator_STABLEHLO_WHILE = 200, + BuiltinOperator_STABLEHLO_GATHER = 201, + BuiltinOperator_STABLEHLO_TRANSPOSE = 202, + BuiltinOperator_DILATE = 203, + BuiltinOperator_STABLEHLO_RNG_BIT_GENERATOR = 204, + BuiltinOperator_REDUCE_WINDOW = 205, + BuiltinOperator_STABLEHLO_COMPOSITE = 206, + BuiltinOperator_STABLEHLO_SHIFT_LEFT = 207, + BuiltinOperator_STABLEHLO_CBRT = 208, + BuiltinOperator_STABLEHLO_CASE = 209, + BuiltinOperator_MIN = BuiltinOperator_ADD, + BuiltinOperator_MAX = BuiltinOperator_STABLEHLO_CASE +}; + +inline const BuiltinOperator (&EnumValuesBuiltinOperator())[210] { + static const BuiltinOperator values[] = { + BuiltinOperator_ADD, + BuiltinOperator_AVERAGE_POOL_2D, + BuiltinOperator_CONCATENATION, + BuiltinOperator_CONV_2D, + BuiltinOperator_DEPTHWISE_CONV_2D, + BuiltinOperator_DEPTH_TO_SPACE, + BuiltinOperator_DEQUANTIZE, + BuiltinOperator_EMBEDDING_LOOKUP, + BuiltinOperator_FLOOR, + BuiltinOperator_FULLY_CONNECTED, + BuiltinOperator_HASHTABLE_LOOKUP, + BuiltinOperator_L2_NORMALIZATION, + BuiltinOperator_L2_POOL_2D, + BuiltinOperator_LOCAL_RESPONSE_NORMALIZATION, + BuiltinOperator_LOGISTIC, + BuiltinOperator_LSH_PROJECTION, + BuiltinOperator_LSTM, + BuiltinOperator_MAX_POOL_2D, + BuiltinOperator_MUL, + BuiltinOperator_RELU, + BuiltinOperator_RELU_N1_TO_1, + BuiltinOperator_RELU6, + BuiltinOperator_RESHAPE, + BuiltinOperator_RESIZE_BILINEAR, + BuiltinOperator_RNN, + BuiltinOperator_SOFTMAX, + BuiltinOperator_SPACE_TO_DEPTH, + BuiltinOperator_SVDF, + BuiltinOperator_TANH, + BuiltinOperator_CONCAT_EMBEDDINGS, + BuiltinOperator_SKIP_GRAM, + BuiltinOperator_CALL, + BuiltinOperator_CUSTOM, + BuiltinOperator_EMBEDDING_LOOKUP_SPARSE, + BuiltinOperator_PAD, + BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_RNN, + BuiltinOperator_GATHER, + BuiltinOperator_BATCH_TO_SPACE_ND, + BuiltinOperator_SPACE_TO_BATCH_ND, + BuiltinOperator_TRANSPOSE, + BuiltinOperator_MEAN, + BuiltinOperator_SUB, + BuiltinOperator_DIV, + BuiltinOperator_SQUEEZE, + BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM, + BuiltinOperator_STRIDED_SLICE, + BuiltinOperator_BIDIRECTIONAL_SEQUENCE_RNN, + BuiltinOperator_EXP, + BuiltinOperator_TOPK_V2, + BuiltinOperator_SPLIT, + BuiltinOperator_LOG_SOFTMAX, + BuiltinOperator_DELEGATE, + BuiltinOperator_BIDIRECTIONAL_SEQUENCE_LSTM, + BuiltinOperator_CAST, + BuiltinOperator_PRELU, + BuiltinOperator_MAXIMUM, + BuiltinOperator_ARG_MAX, + BuiltinOperator_MINIMUM, + BuiltinOperator_LESS, + BuiltinOperator_NEG, + BuiltinOperator_PADV2, + BuiltinOperator_GREATER, + BuiltinOperator_GREATER_EQUAL, + BuiltinOperator_LESS_EQUAL, + BuiltinOperator_SELECT, + BuiltinOperator_SLICE, + BuiltinOperator_SIN, + BuiltinOperator_TRANSPOSE_CONV, + BuiltinOperator_SPARSE_TO_DENSE, + BuiltinOperator_TILE, + BuiltinOperator_EXPAND_DIMS, + BuiltinOperator_EQUAL, + BuiltinOperator_NOT_EQUAL, + BuiltinOperator_LOG, + BuiltinOperator_SUM, + BuiltinOperator_SQRT, + BuiltinOperator_RSQRT, + BuiltinOperator_SHAPE, + BuiltinOperator_POW, + BuiltinOperator_ARG_MIN, + BuiltinOperator_FAKE_QUANT, + BuiltinOperator_REDUCE_PROD, + BuiltinOperator_REDUCE_MAX, + BuiltinOperator_PACK, + BuiltinOperator_LOGICAL_OR, + BuiltinOperator_ONE_HOT, + BuiltinOperator_LOGICAL_AND, + BuiltinOperator_LOGICAL_NOT, + BuiltinOperator_UNPACK, + BuiltinOperator_REDUCE_MIN, + BuiltinOperator_FLOOR_DIV, + BuiltinOperator_REDUCE_ANY, + BuiltinOperator_SQUARE, + BuiltinOperator_ZEROS_LIKE, + BuiltinOperator_FILL, + BuiltinOperator_FLOOR_MOD, + BuiltinOperator_RANGE, + BuiltinOperator_RESIZE_NEAREST_NEIGHBOR, + BuiltinOperator_LEAKY_RELU, + BuiltinOperator_SQUARED_DIFFERENCE, + BuiltinOperator_MIRROR_PAD, + BuiltinOperator_ABS, + BuiltinOperator_SPLIT_V, + BuiltinOperator_UNIQUE, + BuiltinOperator_CEIL, + BuiltinOperator_REVERSE_V2, + BuiltinOperator_ADD_N, + BuiltinOperator_GATHER_ND, + BuiltinOperator_COS, + BuiltinOperator_WHERE, + BuiltinOperator_RANK, + BuiltinOperator_ELU, + BuiltinOperator_REVERSE_SEQUENCE, + BuiltinOperator_MATRIX_DIAG, + BuiltinOperator_QUANTIZE, + BuiltinOperator_MATRIX_SET_DIAG, + BuiltinOperator_ROUND, + BuiltinOperator_HARD_SWISH, + BuiltinOperator_IF, + BuiltinOperator_WHILE, + BuiltinOperator_NON_MAX_SUPPRESSION_V4, + BuiltinOperator_NON_MAX_SUPPRESSION_V5, + BuiltinOperator_SCATTER_ND, + BuiltinOperator_SELECT_V2, + BuiltinOperator_DENSIFY, + BuiltinOperator_SEGMENT_SUM, + BuiltinOperator_BATCH_MATMUL, + BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES, + BuiltinOperator_CUMSUM, + BuiltinOperator_CALL_ONCE, + BuiltinOperator_BROADCAST_TO, + BuiltinOperator_RFFT2D, + BuiltinOperator_CONV_3D, + BuiltinOperator_IMAG, + BuiltinOperator_REAL, + BuiltinOperator_COMPLEX_ABS, + BuiltinOperator_HASHTABLE, + BuiltinOperator_HASHTABLE_FIND, + BuiltinOperator_HASHTABLE_IMPORT, + BuiltinOperator_HASHTABLE_SIZE, + BuiltinOperator_REDUCE_ALL, + BuiltinOperator_CONV_3D_TRANSPOSE, + BuiltinOperator_VAR_HANDLE, + BuiltinOperator_READ_VARIABLE, + BuiltinOperator_ASSIGN_VARIABLE, + BuiltinOperator_BROADCAST_ARGS, + BuiltinOperator_RANDOM_STANDARD_NORMAL, + BuiltinOperator_BUCKETIZE, + BuiltinOperator_RANDOM_UNIFORM, + BuiltinOperator_MULTINOMIAL, + BuiltinOperator_GELU, + BuiltinOperator_DYNAMIC_UPDATE_SLICE, + BuiltinOperator_RELU_0_TO_1, + BuiltinOperator_UNSORTED_SEGMENT_PROD, + BuiltinOperator_UNSORTED_SEGMENT_MAX, + BuiltinOperator_UNSORTED_SEGMENT_SUM, + BuiltinOperator_ATAN2, + BuiltinOperator_UNSORTED_SEGMENT_MIN, + BuiltinOperator_SIGN, + BuiltinOperator_BITCAST, + BuiltinOperator_BITWISE_XOR, + BuiltinOperator_RIGHT_SHIFT, + BuiltinOperator_STABLEHLO_LOGISTIC, + BuiltinOperator_STABLEHLO_ADD, + BuiltinOperator_STABLEHLO_DIVIDE, + BuiltinOperator_STABLEHLO_MULTIPLY, + BuiltinOperator_STABLEHLO_MAXIMUM, + BuiltinOperator_STABLEHLO_RESHAPE, + BuiltinOperator_STABLEHLO_CLAMP, + BuiltinOperator_STABLEHLO_CONCATENATE, + BuiltinOperator_STABLEHLO_BROADCAST_IN_DIM, + BuiltinOperator_STABLEHLO_CONVOLUTION, + BuiltinOperator_STABLEHLO_SLICE, + BuiltinOperator_STABLEHLO_CUSTOM_CALL, + BuiltinOperator_STABLEHLO_REDUCE, + BuiltinOperator_STABLEHLO_ABS, + BuiltinOperator_STABLEHLO_AND, + BuiltinOperator_STABLEHLO_COSINE, + BuiltinOperator_STABLEHLO_EXPONENTIAL, + BuiltinOperator_STABLEHLO_FLOOR, + BuiltinOperator_STABLEHLO_LOG, + BuiltinOperator_STABLEHLO_MINIMUM, + BuiltinOperator_STABLEHLO_NEGATE, + BuiltinOperator_STABLEHLO_OR, + BuiltinOperator_STABLEHLO_POWER, + BuiltinOperator_STABLEHLO_REMAINDER, + BuiltinOperator_STABLEHLO_RSQRT, + BuiltinOperator_STABLEHLO_SELECT, + BuiltinOperator_STABLEHLO_SUBTRACT, + BuiltinOperator_STABLEHLO_TANH, + BuiltinOperator_STABLEHLO_SCATTER, + BuiltinOperator_STABLEHLO_COMPARE, + BuiltinOperator_STABLEHLO_CONVERT, + BuiltinOperator_STABLEHLO_DYNAMIC_SLICE, + BuiltinOperator_STABLEHLO_DYNAMIC_UPDATE_SLICE, + BuiltinOperator_STABLEHLO_PAD, + BuiltinOperator_STABLEHLO_IOTA, + BuiltinOperator_STABLEHLO_DOT_GENERAL, + BuiltinOperator_STABLEHLO_REDUCE_WINDOW, + BuiltinOperator_STABLEHLO_SORT, + BuiltinOperator_STABLEHLO_WHILE, + BuiltinOperator_STABLEHLO_GATHER, + BuiltinOperator_STABLEHLO_TRANSPOSE, + BuiltinOperator_DILATE, + BuiltinOperator_STABLEHLO_RNG_BIT_GENERATOR, + BuiltinOperator_REDUCE_WINDOW, + BuiltinOperator_STABLEHLO_COMPOSITE, + BuiltinOperator_STABLEHLO_SHIFT_LEFT, + BuiltinOperator_STABLEHLO_CBRT, + BuiltinOperator_STABLEHLO_CASE + }; + return values; +} + +inline const char * const *EnumNamesBuiltinOperator() { + static const char * const names[211] = { + "ADD", + "AVERAGE_POOL_2D", + "CONCATENATION", + "CONV_2D", + "DEPTHWISE_CONV_2D", + "DEPTH_TO_SPACE", + "DEQUANTIZE", + "EMBEDDING_LOOKUP", + "FLOOR", + "FULLY_CONNECTED", + "HASHTABLE_LOOKUP", + "L2_NORMALIZATION", + "L2_POOL_2D", + "LOCAL_RESPONSE_NORMALIZATION", + "LOGISTIC", + "LSH_PROJECTION", + "LSTM", + "MAX_POOL_2D", + "MUL", + "RELU", + "RELU_N1_TO_1", + "RELU6", + "RESHAPE", + "RESIZE_BILINEAR", + "RNN", + "SOFTMAX", + "SPACE_TO_DEPTH", + "SVDF", + "TANH", + "CONCAT_EMBEDDINGS", + "SKIP_GRAM", + "CALL", + "CUSTOM", + "EMBEDDING_LOOKUP_SPARSE", + "PAD", + "UNIDIRECTIONAL_SEQUENCE_RNN", + "GATHER", + "BATCH_TO_SPACE_ND", + "SPACE_TO_BATCH_ND", + "TRANSPOSE", + "MEAN", + "SUB", + "DIV", + "SQUEEZE", + "UNIDIRECTIONAL_SEQUENCE_LSTM", + "STRIDED_SLICE", + "BIDIRECTIONAL_SEQUENCE_RNN", + "EXP", + "TOPK_V2", + "SPLIT", + "LOG_SOFTMAX", + "DELEGATE", + "BIDIRECTIONAL_SEQUENCE_LSTM", + "CAST", + "PRELU", + "MAXIMUM", + "ARG_MAX", + "MINIMUM", + "LESS", + "NEG", + "PADV2", + "GREATER", + "GREATER_EQUAL", + "LESS_EQUAL", + "SELECT", + "SLICE", + "SIN", + "TRANSPOSE_CONV", + "SPARSE_TO_DENSE", + "TILE", + "EXPAND_DIMS", + "EQUAL", + "NOT_EQUAL", + "LOG", + "SUM", + "SQRT", + "RSQRT", + "SHAPE", + "POW", + "ARG_MIN", + "FAKE_QUANT", + "REDUCE_PROD", + "REDUCE_MAX", + "PACK", + "LOGICAL_OR", + "ONE_HOT", + "LOGICAL_AND", + "LOGICAL_NOT", + "UNPACK", + "REDUCE_MIN", + "FLOOR_DIV", + "REDUCE_ANY", + "SQUARE", + "ZEROS_LIKE", + "FILL", + "FLOOR_MOD", + "RANGE", + "RESIZE_NEAREST_NEIGHBOR", + "LEAKY_RELU", + "SQUARED_DIFFERENCE", + "MIRROR_PAD", + "ABS", + "SPLIT_V", + "UNIQUE", + "CEIL", + "REVERSE_V2", + "ADD_N", + "GATHER_ND", + "COS", + "WHERE", + "RANK", + "ELU", + "REVERSE_SEQUENCE", + "MATRIX_DIAG", + "QUANTIZE", + "MATRIX_SET_DIAG", + "ROUND", + "HARD_SWISH", + "IF", + "WHILE", + "NON_MAX_SUPPRESSION_V4", + "NON_MAX_SUPPRESSION_V5", + "SCATTER_ND", + "SELECT_V2", + "DENSIFY", + "SEGMENT_SUM", + "BATCH_MATMUL", + "PLACEHOLDER_FOR_GREATER_OP_CODES", + "CUMSUM", + "CALL_ONCE", + "BROADCAST_TO", + "RFFT2D", + "CONV_3D", + "IMAG", + "REAL", + "COMPLEX_ABS", + "HASHTABLE", + "HASHTABLE_FIND", + "HASHTABLE_IMPORT", + "HASHTABLE_SIZE", + "REDUCE_ALL", + "CONV_3D_TRANSPOSE", + "VAR_HANDLE", + "READ_VARIABLE", + "ASSIGN_VARIABLE", + "BROADCAST_ARGS", + "RANDOM_STANDARD_NORMAL", + "BUCKETIZE", + "RANDOM_UNIFORM", + "MULTINOMIAL", + "GELU", + "DYNAMIC_UPDATE_SLICE", + "RELU_0_TO_1", + "UNSORTED_SEGMENT_PROD", + "UNSORTED_SEGMENT_MAX", + "UNSORTED_SEGMENT_SUM", + "ATAN2", + "UNSORTED_SEGMENT_MIN", + "SIGN", + "BITCAST", + "BITWISE_XOR", + "RIGHT_SHIFT", + "STABLEHLO_LOGISTIC", + "STABLEHLO_ADD", + "STABLEHLO_DIVIDE", + "STABLEHLO_MULTIPLY", + "STABLEHLO_MAXIMUM", + "STABLEHLO_RESHAPE", + "STABLEHLO_CLAMP", + "STABLEHLO_CONCATENATE", + "STABLEHLO_BROADCAST_IN_DIM", + "STABLEHLO_CONVOLUTION", + "STABLEHLO_SLICE", + "STABLEHLO_CUSTOM_CALL", + "STABLEHLO_REDUCE", + "STABLEHLO_ABS", + "STABLEHLO_AND", + "STABLEHLO_COSINE", + "STABLEHLO_EXPONENTIAL", + "STABLEHLO_FLOOR", + "STABLEHLO_LOG", + "STABLEHLO_MINIMUM", + "STABLEHLO_NEGATE", + "STABLEHLO_OR", + "STABLEHLO_POWER", + "STABLEHLO_REMAINDER", + "STABLEHLO_RSQRT", + "STABLEHLO_SELECT", + "STABLEHLO_SUBTRACT", + "STABLEHLO_TANH", + "STABLEHLO_SCATTER", + "STABLEHLO_COMPARE", + "STABLEHLO_CONVERT", + "STABLEHLO_DYNAMIC_SLICE", + "STABLEHLO_DYNAMIC_UPDATE_SLICE", + "STABLEHLO_PAD", + "STABLEHLO_IOTA", + "STABLEHLO_DOT_GENERAL", + "STABLEHLO_REDUCE_WINDOW", + "STABLEHLO_SORT", + "STABLEHLO_WHILE", + "STABLEHLO_GATHER", + "STABLEHLO_TRANSPOSE", + "DILATE", + "STABLEHLO_RNG_BIT_GENERATOR", + "REDUCE_WINDOW", + "STABLEHLO_COMPOSITE", + "STABLEHLO_SHIFT_LEFT", + "STABLEHLO_CBRT", + "STABLEHLO_CASE", + nullptr + }; + return names; +} + +inline const char *EnumNameBuiltinOperator(BuiltinOperator e) { + if (::flatbuffers::IsOutRange(e, BuiltinOperator_ADD, BuiltinOperator_STABLEHLO_CASE)) return ""; + const size_t index = static_cast(e); + return EnumNamesBuiltinOperator()[index]; +} + +enum BuiltinOptions : uint8_t { + BuiltinOptions_NONE = 0, + BuiltinOptions_Conv2DOptions = 1, + BuiltinOptions_DepthwiseConv2DOptions = 2, + BuiltinOptions_ConcatEmbeddingsOptions = 3, + BuiltinOptions_LSHProjectionOptions = 4, + BuiltinOptions_Pool2DOptions = 5, + BuiltinOptions_SVDFOptions = 6, + BuiltinOptions_RNNOptions = 7, + BuiltinOptions_FullyConnectedOptions = 8, + BuiltinOptions_SoftmaxOptions = 9, + BuiltinOptions_ConcatenationOptions = 10, + BuiltinOptions_AddOptions = 11, + BuiltinOptions_L2NormOptions = 12, + BuiltinOptions_LocalResponseNormalizationOptions = 13, + BuiltinOptions_LSTMOptions = 14, + BuiltinOptions_ResizeBilinearOptions = 15, + BuiltinOptions_CallOptions = 16, + BuiltinOptions_ReshapeOptions = 17, + BuiltinOptions_SkipGramOptions = 18, + BuiltinOptions_SpaceToDepthOptions = 19, + BuiltinOptions_EmbeddingLookupSparseOptions = 20, + BuiltinOptions_MulOptions = 21, + BuiltinOptions_PadOptions = 22, + BuiltinOptions_GatherOptions = 23, + BuiltinOptions_BatchToSpaceNDOptions = 24, + BuiltinOptions_SpaceToBatchNDOptions = 25, + BuiltinOptions_TransposeOptions = 26, + BuiltinOptions_ReducerOptions = 27, + BuiltinOptions_SubOptions = 28, + BuiltinOptions_DivOptions = 29, + BuiltinOptions_SqueezeOptions = 30, + BuiltinOptions_SequenceRNNOptions = 31, + BuiltinOptions_StridedSliceOptions = 32, + BuiltinOptions_ExpOptions = 33, + BuiltinOptions_TopKV2Options = 34, + BuiltinOptions_SplitOptions = 35, + BuiltinOptions_LogSoftmaxOptions = 36, + BuiltinOptions_CastOptions = 37, + BuiltinOptions_DequantizeOptions = 38, + BuiltinOptions_MaximumMinimumOptions = 39, + BuiltinOptions_ArgMaxOptions = 40, + BuiltinOptions_LessOptions = 41, + BuiltinOptions_NegOptions = 42, + BuiltinOptions_PadV2Options = 43, + BuiltinOptions_GreaterOptions = 44, + BuiltinOptions_GreaterEqualOptions = 45, + BuiltinOptions_LessEqualOptions = 46, + BuiltinOptions_SelectOptions = 47, + BuiltinOptions_SliceOptions = 48, + BuiltinOptions_TransposeConvOptions = 49, + BuiltinOptions_SparseToDenseOptions = 50, + BuiltinOptions_TileOptions = 51, + BuiltinOptions_ExpandDimsOptions = 52, + BuiltinOptions_EqualOptions = 53, + BuiltinOptions_NotEqualOptions = 54, + BuiltinOptions_ShapeOptions = 55, + BuiltinOptions_PowOptions = 56, + BuiltinOptions_ArgMinOptions = 57, + BuiltinOptions_FakeQuantOptions = 58, + BuiltinOptions_PackOptions = 59, + BuiltinOptions_LogicalOrOptions = 60, + BuiltinOptions_OneHotOptions = 61, + BuiltinOptions_LogicalAndOptions = 62, + BuiltinOptions_LogicalNotOptions = 63, + BuiltinOptions_UnpackOptions = 64, + BuiltinOptions_FloorDivOptions = 65, + BuiltinOptions_SquareOptions = 66, + BuiltinOptions_ZerosLikeOptions = 67, + BuiltinOptions_FillOptions = 68, + BuiltinOptions_BidirectionalSequenceLSTMOptions = 69, + BuiltinOptions_BidirectionalSequenceRNNOptions = 70, + BuiltinOptions_UnidirectionalSequenceLSTMOptions = 71, + BuiltinOptions_FloorModOptions = 72, + BuiltinOptions_RangeOptions = 73, + BuiltinOptions_ResizeNearestNeighborOptions = 74, + BuiltinOptions_LeakyReluOptions = 75, + BuiltinOptions_SquaredDifferenceOptions = 76, + BuiltinOptions_MirrorPadOptions = 77, + BuiltinOptions_AbsOptions = 78, + BuiltinOptions_SplitVOptions = 79, + BuiltinOptions_UniqueOptions = 80, + BuiltinOptions_ReverseV2Options = 81, + BuiltinOptions_AddNOptions = 82, + BuiltinOptions_GatherNdOptions = 83, + BuiltinOptions_CosOptions = 84, + BuiltinOptions_WhereOptions = 85, + BuiltinOptions_RankOptions = 86, + BuiltinOptions_ReverseSequenceOptions = 87, + BuiltinOptions_MatrixDiagOptions = 88, + BuiltinOptions_QuantizeOptions = 89, + BuiltinOptions_MatrixSetDiagOptions = 90, + BuiltinOptions_HardSwishOptions = 91, + BuiltinOptions_IfOptions = 92, + BuiltinOptions_WhileOptions = 93, + BuiltinOptions_DepthToSpaceOptions = 94, + BuiltinOptions_NonMaxSuppressionV4Options = 95, + BuiltinOptions_NonMaxSuppressionV5Options = 96, + BuiltinOptions_ScatterNdOptions = 97, + BuiltinOptions_SelectV2Options = 98, + BuiltinOptions_DensifyOptions = 99, + BuiltinOptions_SegmentSumOptions = 100, + BuiltinOptions_BatchMatMulOptions = 101, + BuiltinOptions_CumsumOptions = 102, + BuiltinOptions_CallOnceOptions = 103, + BuiltinOptions_BroadcastToOptions = 104, + BuiltinOptions_Rfft2dOptions = 105, + BuiltinOptions_Conv3DOptions = 106, + BuiltinOptions_HashtableOptions = 107, + BuiltinOptions_HashtableFindOptions = 108, + BuiltinOptions_HashtableImportOptions = 109, + BuiltinOptions_HashtableSizeOptions = 110, + BuiltinOptions_VarHandleOptions = 111, + BuiltinOptions_ReadVariableOptions = 112, + BuiltinOptions_AssignVariableOptions = 113, + BuiltinOptions_RandomOptions = 114, + BuiltinOptions_BucketizeOptions = 115, + BuiltinOptions_GeluOptions = 116, + BuiltinOptions_DynamicUpdateSliceOptions = 117, + BuiltinOptions_UnsortedSegmentProdOptions = 118, + BuiltinOptions_UnsortedSegmentMaxOptions = 119, + BuiltinOptions_UnsortedSegmentMinOptions = 120, + BuiltinOptions_UnsortedSegmentSumOptions = 121, + BuiltinOptions_ATan2Options = 122, + BuiltinOptions_SignOptions = 123, + BuiltinOptions_BitcastOptions = 124, + BuiltinOptions_BitwiseXorOptions = 125, + BuiltinOptions_RightShiftOptions = 126, + BuiltinOptions_MIN = BuiltinOptions_NONE, + BuiltinOptions_MAX = BuiltinOptions_RightShiftOptions +}; + +inline const BuiltinOptions (&EnumValuesBuiltinOptions())[127] { + static const BuiltinOptions values[] = { + BuiltinOptions_NONE, + BuiltinOptions_Conv2DOptions, + BuiltinOptions_DepthwiseConv2DOptions, + BuiltinOptions_ConcatEmbeddingsOptions, + BuiltinOptions_LSHProjectionOptions, + BuiltinOptions_Pool2DOptions, + BuiltinOptions_SVDFOptions, + BuiltinOptions_RNNOptions, + BuiltinOptions_FullyConnectedOptions, + BuiltinOptions_SoftmaxOptions, + BuiltinOptions_ConcatenationOptions, + BuiltinOptions_AddOptions, + BuiltinOptions_L2NormOptions, + BuiltinOptions_LocalResponseNormalizationOptions, + BuiltinOptions_LSTMOptions, + BuiltinOptions_ResizeBilinearOptions, + BuiltinOptions_CallOptions, + BuiltinOptions_ReshapeOptions, + BuiltinOptions_SkipGramOptions, + BuiltinOptions_SpaceToDepthOptions, + BuiltinOptions_EmbeddingLookupSparseOptions, + BuiltinOptions_MulOptions, + BuiltinOptions_PadOptions, + BuiltinOptions_GatherOptions, + BuiltinOptions_BatchToSpaceNDOptions, + BuiltinOptions_SpaceToBatchNDOptions, + BuiltinOptions_TransposeOptions, + BuiltinOptions_ReducerOptions, + BuiltinOptions_SubOptions, + BuiltinOptions_DivOptions, + BuiltinOptions_SqueezeOptions, + BuiltinOptions_SequenceRNNOptions, + BuiltinOptions_StridedSliceOptions, + BuiltinOptions_ExpOptions, + BuiltinOptions_TopKV2Options, + BuiltinOptions_SplitOptions, + BuiltinOptions_LogSoftmaxOptions, + BuiltinOptions_CastOptions, + BuiltinOptions_DequantizeOptions, + BuiltinOptions_MaximumMinimumOptions, + BuiltinOptions_ArgMaxOptions, + BuiltinOptions_LessOptions, + BuiltinOptions_NegOptions, + BuiltinOptions_PadV2Options, + BuiltinOptions_GreaterOptions, + BuiltinOptions_GreaterEqualOptions, + BuiltinOptions_LessEqualOptions, + BuiltinOptions_SelectOptions, + BuiltinOptions_SliceOptions, + BuiltinOptions_TransposeConvOptions, + BuiltinOptions_SparseToDenseOptions, + BuiltinOptions_TileOptions, + BuiltinOptions_ExpandDimsOptions, + BuiltinOptions_EqualOptions, + BuiltinOptions_NotEqualOptions, + BuiltinOptions_ShapeOptions, + BuiltinOptions_PowOptions, + BuiltinOptions_ArgMinOptions, + BuiltinOptions_FakeQuantOptions, + BuiltinOptions_PackOptions, + BuiltinOptions_LogicalOrOptions, + BuiltinOptions_OneHotOptions, + BuiltinOptions_LogicalAndOptions, + BuiltinOptions_LogicalNotOptions, + BuiltinOptions_UnpackOptions, + BuiltinOptions_FloorDivOptions, + BuiltinOptions_SquareOptions, + BuiltinOptions_ZerosLikeOptions, + BuiltinOptions_FillOptions, + BuiltinOptions_BidirectionalSequenceLSTMOptions, + BuiltinOptions_BidirectionalSequenceRNNOptions, + BuiltinOptions_UnidirectionalSequenceLSTMOptions, + BuiltinOptions_FloorModOptions, + BuiltinOptions_RangeOptions, + BuiltinOptions_ResizeNearestNeighborOptions, + BuiltinOptions_LeakyReluOptions, + BuiltinOptions_SquaredDifferenceOptions, + BuiltinOptions_MirrorPadOptions, + BuiltinOptions_AbsOptions, + BuiltinOptions_SplitVOptions, + BuiltinOptions_UniqueOptions, + BuiltinOptions_ReverseV2Options, + BuiltinOptions_AddNOptions, + BuiltinOptions_GatherNdOptions, + BuiltinOptions_CosOptions, + BuiltinOptions_WhereOptions, + BuiltinOptions_RankOptions, + BuiltinOptions_ReverseSequenceOptions, + BuiltinOptions_MatrixDiagOptions, + BuiltinOptions_QuantizeOptions, + BuiltinOptions_MatrixSetDiagOptions, + BuiltinOptions_HardSwishOptions, + BuiltinOptions_IfOptions, + BuiltinOptions_WhileOptions, + BuiltinOptions_DepthToSpaceOptions, + BuiltinOptions_NonMaxSuppressionV4Options, + BuiltinOptions_NonMaxSuppressionV5Options, + BuiltinOptions_ScatterNdOptions, + BuiltinOptions_SelectV2Options, + BuiltinOptions_DensifyOptions, + BuiltinOptions_SegmentSumOptions, + BuiltinOptions_BatchMatMulOptions, + BuiltinOptions_CumsumOptions, + BuiltinOptions_CallOnceOptions, + BuiltinOptions_BroadcastToOptions, + BuiltinOptions_Rfft2dOptions, + BuiltinOptions_Conv3DOptions, + BuiltinOptions_HashtableOptions, + BuiltinOptions_HashtableFindOptions, + BuiltinOptions_HashtableImportOptions, + BuiltinOptions_HashtableSizeOptions, + BuiltinOptions_VarHandleOptions, + BuiltinOptions_ReadVariableOptions, + BuiltinOptions_AssignVariableOptions, + BuiltinOptions_RandomOptions, + BuiltinOptions_BucketizeOptions, + BuiltinOptions_GeluOptions, + BuiltinOptions_DynamicUpdateSliceOptions, + BuiltinOptions_UnsortedSegmentProdOptions, + BuiltinOptions_UnsortedSegmentMaxOptions, + BuiltinOptions_UnsortedSegmentMinOptions, + BuiltinOptions_UnsortedSegmentSumOptions, + BuiltinOptions_ATan2Options, + BuiltinOptions_SignOptions, + BuiltinOptions_BitcastOptions, + BuiltinOptions_BitwiseXorOptions, + BuiltinOptions_RightShiftOptions + }; + return values; +} + +inline const char * const *EnumNamesBuiltinOptions() { + static const char * const names[128] = { + "NONE", + "Conv2DOptions", + "DepthwiseConv2DOptions", + "ConcatEmbeddingsOptions", + "LSHProjectionOptions", + "Pool2DOptions", + "SVDFOptions", + "RNNOptions", + "FullyConnectedOptions", + "SoftmaxOptions", + "ConcatenationOptions", + "AddOptions", + "L2NormOptions", + "LocalResponseNormalizationOptions", + "LSTMOptions", + "ResizeBilinearOptions", + "CallOptions", + "ReshapeOptions", + "SkipGramOptions", + "SpaceToDepthOptions", + "EmbeddingLookupSparseOptions", + "MulOptions", + "PadOptions", + "GatherOptions", + "BatchToSpaceNDOptions", + "SpaceToBatchNDOptions", + "TransposeOptions", + "ReducerOptions", + "SubOptions", + "DivOptions", + "SqueezeOptions", + "SequenceRNNOptions", + "StridedSliceOptions", + "ExpOptions", + "TopKV2Options", + "SplitOptions", + "LogSoftmaxOptions", + "CastOptions", + "DequantizeOptions", + "MaximumMinimumOptions", + "ArgMaxOptions", + "LessOptions", + "NegOptions", + "PadV2Options", + "GreaterOptions", + "GreaterEqualOptions", + "LessEqualOptions", + "SelectOptions", + "SliceOptions", + "TransposeConvOptions", + "SparseToDenseOptions", + "TileOptions", + "ExpandDimsOptions", + "EqualOptions", + "NotEqualOptions", + "ShapeOptions", + "PowOptions", + "ArgMinOptions", + "FakeQuantOptions", + "PackOptions", + "LogicalOrOptions", + "OneHotOptions", + "LogicalAndOptions", + "LogicalNotOptions", + "UnpackOptions", + "FloorDivOptions", + "SquareOptions", + "ZerosLikeOptions", + "FillOptions", + "BidirectionalSequenceLSTMOptions", + "BidirectionalSequenceRNNOptions", + "UnidirectionalSequenceLSTMOptions", + "FloorModOptions", + "RangeOptions", + "ResizeNearestNeighborOptions", + "LeakyReluOptions", + "SquaredDifferenceOptions", + "MirrorPadOptions", + "AbsOptions", + "SplitVOptions", + "UniqueOptions", + "ReverseV2Options", + "AddNOptions", + "GatherNdOptions", + "CosOptions", + "WhereOptions", + "RankOptions", + "ReverseSequenceOptions", + "MatrixDiagOptions", + "QuantizeOptions", + "MatrixSetDiagOptions", + "HardSwishOptions", + "IfOptions", + "WhileOptions", + "DepthToSpaceOptions", + "NonMaxSuppressionV4Options", + "NonMaxSuppressionV5Options", + "ScatterNdOptions", + "SelectV2Options", + "DensifyOptions", + "SegmentSumOptions", + "BatchMatMulOptions", + "CumsumOptions", + "CallOnceOptions", + "BroadcastToOptions", + "Rfft2dOptions", + "Conv3DOptions", + "HashtableOptions", + "HashtableFindOptions", + "HashtableImportOptions", + "HashtableSizeOptions", + "VarHandleOptions", + "ReadVariableOptions", + "AssignVariableOptions", + "RandomOptions", + "BucketizeOptions", + "GeluOptions", + "DynamicUpdateSliceOptions", + "UnsortedSegmentProdOptions", + "UnsortedSegmentMaxOptions", + "UnsortedSegmentMinOptions", + "UnsortedSegmentSumOptions", + "ATan2Options", + "SignOptions", + "BitcastOptions", + "BitwiseXorOptions", + "RightShiftOptions", + nullptr + }; + return names; +} + +inline const char *EnumNameBuiltinOptions(BuiltinOptions e) { + if (::flatbuffers::IsOutRange(e, BuiltinOptions_NONE, BuiltinOptions_RightShiftOptions)) return ""; + const size_t index = static_cast(e); + return EnumNamesBuiltinOptions()[index]; +} + +template struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_NONE; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_Conv2DOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_DepthwiseConv2DOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ConcatEmbeddingsOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_LSHProjectionOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_Pool2DOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SVDFOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_RNNOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_FullyConnectedOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SoftmaxOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ConcatenationOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_AddOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_L2NormOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_LocalResponseNormalizationOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_LSTMOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ResizeBilinearOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_CallOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ReshapeOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SkipGramOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SpaceToDepthOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_EmbeddingLookupSparseOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_MulOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_PadOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_GatherOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_BatchToSpaceNDOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SpaceToBatchNDOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_TransposeOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ReducerOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SubOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_DivOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SqueezeOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SequenceRNNOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_StridedSliceOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ExpOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_TopKV2Options; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SplitOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_LogSoftmaxOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_CastOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_DequantizeOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_MaximumMinimumOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ArgMaxOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_LessOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_NegOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_PadV2Options; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_GreaterOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_GreaterEqualOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_LessEqualOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SelectOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SliceOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_TransposeConvOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SparseToDenseOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_TileOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ExpandDimsOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_EqualOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_NotEqualOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ShapeOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_PowOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ArgMinOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_FakeQuantOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_PackOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_LogicalOrOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_OneHotOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_LogicalAndOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_LogicalNotOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_UnpackOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_FloorDivOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SquareOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ZerosLikeOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_FillOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_BidirectionalSequenceLSTMOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_BidirectionalSequenceRNNOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_UnidirectionalSequenceLSTMOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_FloorModOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_RangeOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ResizeNearestNeighborOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_LeakyReluOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SquaredDifferenceOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_MirrorPadOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_AbsOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SplitVOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_UniqueOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ReverseV2Options; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_AddNOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_GatherNdOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_CosOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_WhereOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_RankOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ReverseSequenceOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_MatrixDiagOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_QuantizeOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_MatrixSetDiagOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_HardSwishOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_IfOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_WhileOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_DepthToSpaceOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_NonMaxSuppressionV4Options; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_NonMaxSuppressionV5Options; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ScatterNdOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SelectV2Options; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_DensifyOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SegmentSumOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_BatchMatMulOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_CumsumOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_CallOnceOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_BroadcastToOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_Rfft2dOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_Conv3DOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_HashtableOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_HashtableFindOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_HashtableImportOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_HashtableSizeOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_VarHandleOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ReadVariableOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_AssignVariableOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_RandomOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_BucketizeOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_GeluOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_DynamicUpdateSliceOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_UnsortedSegmentProdOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_UnsortedSegmentMaxOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_UnsortedSegmentMinOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_UnsortedSegmentSumOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ATan2Options; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SignOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_BitcastOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_BitwiseXorOptions; +}; + +template<> struct BuiltinOptionsTraits { + static const BuiltinOptions enum_value = BuiltinOptions_RightShiftOptions; +}; + +template struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_NONE; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_Conv2DOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_DepthwiseConv2DOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ConcatEmbeddingsOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_LSHProjectionOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_Pool2DOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SVDFOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_RNNOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_FullyConnectedOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SoftmaxOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ConcatenationOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_AddOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_L2NormOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_LocalResponseNormalizationOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_LSTMOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ResizeBilinearOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_CallOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ReshapeOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SkipGramOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SpaceToDepthOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_EmbeddingLookupSparseOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_MulOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_PadOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_GatherOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_BatchToSpaceNDOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SpaceToBatchNDOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_TransposeOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ReducerOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SubOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_DivOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SqueezeOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SequenceRNNOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_StridedSliceOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ExpOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_TopKV2Options; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SplitOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_LogSoftmaxOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_CastOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_DequantizeOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_MaximumMinimumOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ArgMaxOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_LessOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_NegOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_PadV2Options; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_GreaterOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_GreaterEqualOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_LessEqualOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SelectOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SliceOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_TransposeConvOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SparseToDenseOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_TileOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ExpandDimsOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_EqualOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_NotEqualOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ShapeOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_PowOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ArgMinOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_FakeQuantOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_PackOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_LogicalOrOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_OneHotOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_LogicalAndOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_LogicalNotOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_UnpackOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_FloorDivOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SquareOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ZerosLikeOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_FillOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_BidirectionalSequenceLSTMOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_BidirectionalSequenceRNNOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_UnidirectionalSequenceLSTMOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_FloorModOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_RangeOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ResizeNearestNeighborOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_LeakyReluOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SquaredDifferenceOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_MirrorPadOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_AbsOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SplitVOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_UniqueOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ReverseV2Options; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_AddNOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_GatherNdOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_CosOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_WhereOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_RankOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ReverseSequenceOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_MatrixDiagOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_QuantizeOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_MatrixSetDiagOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_HardSwishOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_IfOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_WhileOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_DepthToSpaceOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_NonMaxSuppressionV4Options; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_NonMaxSuppressionV5Options; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ScatterNdOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SelectV2Options; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_DensifyOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SegmentSumOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_BatchMatMulOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_CumsumOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_CallOnceOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_BroadcastToOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_Rfft2dOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_Conv3DOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_HashtableOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_HashtableFindOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_HashtableImportOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_HashtableSizeOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_VarHandleOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ReadVariableOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_AssignVariableOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_RandomOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_BucketizeOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_GeluOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_DynamicUpdateSliceOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_UnsortedSegmentProdOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_UnsortedSegmentMaxOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_UnsortedSegmentMinOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_UnsortedSegmentSumOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_ATan2Options; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_SignOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_BitcastOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_BitwiseXorOptions; +}; + +template<> struct BuiltinOptionsUnionTraits { + static const BuiltinOptions enum_value = BuiltinOptions_RightShiftOptions; +}; + +struct BuiltinOptionsUnion { + BuiltinOptions type; + void *value; + + BuiltinOptionsUnion() : type(BuiltinOptions_NONE), value(nullptr) {} + BuiltinOptionsUnion(BuiltinOptionsUnion&& u) FLATBUFFERS_NOEXCEPT : + type(BuiltinOptions_NONE), value(nullptr) + { std::swap(type, u.type); std::swap(value, u.value); } + BuiltinOptionsUnion(const BuiltinOptionsUnion &); + BuiltinOptionsUnion &operator=(const BuiltinOptionsUnion &u) + { BuiltinOptionsUnion t(u); std::swap(type, t.type); std::swap(value, t.value); return *this; } + BuiltinOptionsUnion &operator=(BuiltinOptionsUnion &&u) FLATBUFFERS_NOEXCEPT + { std::swap(type, u.type); std::swap(value, u.value); return *this; } + ~BuiltinOptionsUnion() { Reset(); } + + void Reset(); + + template + void Set(T&& val) { + typedef typename std::remove_reference::type RT; + Reset(); + type = BuiltinOptionsUnionTraits::enum_value; + if (type != BuiltinOptions_NONE) { + value = new RT(std::forward(val)); + } + } + + static void *UnPack(const void *obj, enum BuiltinOptions type, const ::flatbuffers::resolver_function_t *resolver); + ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr) const; + + tflite::Conv2DOptionsT *AsConv2DOptions() { + return type == BuiltinOptions_Conv2DOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::Conv2DOptionsT *AsConv2DOptions() const { + return type == BuiltinOptions_Conv2DOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::DepthwiseConv2DOptionsT *AsDepthwiseConv2DOptions() { + return type == BuiltinOptions_DepthwiseConv2DOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::DepthwiseConv2DOptionsT *AsDepthwiseConv2DOptions() const { + return type == BuiltinOptions_DepthwiseConv2DOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::ConcatEmbeddingsOptionsT *AsConcatEmbeddingsOptions() { + return type == BuiltinOptions_ConcatEmbeddingsOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::ConcatEmbeddingsOptionsT *AsConcatEmbeddingsOptions() const { + return type == BuiltinOptions_ConcatEmbeddingsOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::LSHProjectionOptionsT *AsLSHProjectionOptions() { + return type == BuiltinOptions_LSHProjectionOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::LSHProjectionOptionsT *AsLSHProjectionOptions() const { + return type == BuiltinOptions_LSHProjectionOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::Pool2DOptionsT *AsPool2DOptions() { + return type == BuiltinOptions_Pool2DOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::Pool2DOptionsT *AsPool2DOptions() const { + return type == BuiltinOptions_Pool2DOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::SVDFOptionsT *AsSVDFOptions() { + return type == BuiltinOptions_SVDFOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::SVDFOptionsT *AsSVDFOptions() const { + return type == BuiltinOptions_SVDFOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::RNNOptionsT *AsRNNOptions() { + return type == BuiltinOptions_RNNOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::RNNOptionsT *AsRNNOptions() const { + return type == BuiltinOptions_RNNOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::FullyConnectedOptionsT *AsFullyConnectedOptions() { + return type == BuiltinOptions_FullyConnectedOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::FullyConnectedOptionsT *AsFullyConnectedOptions() const { + return type == BuiltinOptions_FullyConnectedOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::SoftmaxOptionsT *AsSoftmaxOptions() { + return type == BuiltinOptions_SoftmaxOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::SoftmaxOptionsT *AsSoftmaxOptions() const { + return type == BuiltinOptions_SoftmaxOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::ConcatenationOptionsT *AsConcatenationOptions() { + return type == BuiltinOptions_ConcatenationOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::ConcatenationOptionsT *AsConcatenationOptions() const { + return type == BuiltinOptions_ConcatenationOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::AddOptionsT *AsAddOptions() { + return type == BuiltinOptions_AddOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::AddOptionsT *AsAddOptions() const { + return type == BuiltinOptions_AddOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::L2NormOptionsT *AsL2NormOptions() { + return type == BuiltinOptions_L2NormOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::L2NormOptionsT *AsL2NormOptions() const { + return type == BuiltinOptions_L2NormOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::LocalResponseNormalizationOptionsT *AsLocalResponseNormalizationOptions() { + return type == BuiltinOptions_LocalResponseNormalizationOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::LocalResponseNormalizationOptionsT *AsLocalResponseNormalizationOptions() const { + return type == BuiltinOptions_LocalResponseNormalizationOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::LSTMOptionsT *AsLSTMOptions() { + return type == BuiltinOptions_LSTMOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::LSTMOptionsT *AsLSTMOptions() const { + return type == BuiltinOptions_LSTMOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::ResizeBilinearOptionsT *AsResizeBilinearOptions() { + return type == BuiltinOptions_ResizeBilinearOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::ResizeBilinearOptionsT *AsResizeBilinearOptions() const { + return type == BuiltinOptions_ResizeBilinearOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::CallOptionsT *AsCallOptions() { + return type == BuiltinOptions_CallOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::CallOptionsT *AsCallOptions() const { + return type == BuiltinOptions_CallOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::ReshapeOptionsT *AsReshapeOptions() { + return type == BuiltinOptions_ReshapeOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::ReshapeOptionsT *AsReshapeOptions() const { + return type == BuiltinOptions_ReshapeOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::SkipGramOptionsT *AsSkipGramOptions() { + return type == BuiltinOptions_SkipGramOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::SkipGramOptionsT *AsSkipGramOptions() const { + return type == BuiltinOptions_SkipGramOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::SpaceToDepthOptionsT *AsSpaceToDepthOptions() { + return type == BuiltinOptions_SpaceToDepthOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::SpaceToDepthOptionsT *AsSpaceToDepthOptions() const { + return type == BuiltinOptions_SpaceToDepthOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::EmbeddingLookupSparseOptionsT *AsEmbeddingLookupSparseOptions() { + return type == BuiltinOptions_EmbeddingLookupSparseOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::EmbeddingLookupSparseOptionsT *AsEmbeddingLookupSparseOptions() const { + return type == BuiltinOptions_EmbeddingLookupSparseOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::MulOptionsT *AsMulOptions() { + return type == BuiltinOptions_MulOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::MulOptionsT *AsMulOptions() const { + return type == BuiltinOptions_MulOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::PadOptionsT *AsPadOptions() { + return type == BuiltinOptions_PadOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::PadOptionsT *AsPadOptions() const { + return type == BuiltinOptions_PadOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::GatherOptionsT *AsGatherOptions() { + return type == BuiltinOptions_GatherOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::GatherOptionsT *AsGatherOptions() const { + return type == BuiltinOptions_GatherOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::BatchToSpaceNDOptionsT *AsBatchToSpaceNDOptions() { + return type == BuiltinOptions_BatchToSpaceNDOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::BatchToSpaceNDOptionsT *AsBatchToSpaceNDOptions() const { + return type == BuiltinOptions_BatchToSpaceNDOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::SpaceToBatchNDOptionsT *AsSpaceToBatchNDOptions() { + return type == BuiltinOptions_SpaceToBatchNDOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::SpaceToBatchNDOptionsT *AsSpaceToBatchNDOptions() const { + return type == BuiltinOptions_SpaceToBatchNDOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::TransposeOptionsT *AsTransposeOptions() { + return type == BuiltinOptions_TransposeOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::TransposeOptionsT *AsTransposeOptions() const { + return type == BuiltinOptions_TransposeOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::ReducerOptionsT *AsReducerOptions() { + return type == BuiltinOptions_ReducerOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::ReducerOptionsT *AsReducerOptions() const { + return type == BuiltinOptions_ReducerOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::SubOptionsT *AsSubOptions() { + return type == BuiltinOptions_SubOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::SubOptionsT *AsSubOptions() const { + return type == BuiltinOptions_SubOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::DivOptionsT *AsDivOptions() { + return type == BuiltinOptions_DivOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::DivOptionsT *AsDivOptions() const { + return type == BuiltinOptions_DivOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::SqueezeOptionsT *AsSqueezeOptions() { + return type == BuiltinOptions_SqueezeOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::SqueezeOptionsT *AsSqueezeOptions() const { + return type == BuiltinOptions_SqueezeOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::SequenceRNNOptionsT *AsSequenceRNNOptions() { + return type == BuiltinOptions_SequenceRNNOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::SequenceRNNOptionsT *AsSequenceRNNOptions() const { + return type == BuiltinOptions_SequenceRNNOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::StridedSliceOptionsT *AsStridedSliceOptions() { + return type == BuiltinOptions_StridedSliceOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::StridedSliceOptionsT *AsStridedSliceOptions() const { + return type == BuiltinOptions_StridedSliceOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::ExpOptionsT *AsExpOptions() { + return type == BuiltinOptions_ExpOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::ExpOptionsT *AsExpOptions() const { + return type == BuiltinOptions_ExpOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::TopKV2OptionsT *AsTopKV2Options() { + return type == BuiltinOptions_TopKV2Options ? + reinterpret_cast(value) : nullptr; + } + const tflite::TopKV2OptionsT *AsTopKV2Options() const { + return type == BuiltinOptions_TopKV2Options ? + reinterpret_cast(value) : nullptr; + } + tflite::SplitOptionsT *AsSplitOptions() { + return type == BuiltinOptions_SplitOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::SplitOptionsT *AsSplitOptions() const { + return type == BuiltinOptions_SplitOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::LogSoftmaxOptionsT *AsLogSoftmaxOptions() { + return type == BuiltinOptions_LogSoftmaxOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::LogSoftmaxOptionsT *AsLogSoftmaxOptions() const { + return type == BuiltinOptions_LogSoftmaxOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::CastOptionsT *AsCastOptions() { + return type == BuiltinOptions_CastOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::CastOptionsT *AsCastOptions() const { + return type == BuiltinOptions_CastOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::DequantizeOptionsT *AsDequantizeOptions() { + return type == BuiltinOptions_DequantizeOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::DequantizeOptionsT *AsDequantizeOptions() const { + return type == BuiltinOptions_DequantizeOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::MaximumMinimumOptionsT *AsMaximumMinimumOptions() { + return type == BuiltinOptions_MaximumMinimumOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::MaximumMinimumOptionsT *AsMaximumMinimumOptions() const { + return type == BuiltinOptions_MaximumMinimumOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::ArgMaxOptionsT *AsArgMaxOptions() { + return type == BuiltinOptions_ArgMaxOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::ArgMaxOptionsT *AsArgMaxOptions() const { + return type == BuiltinOptions_ArgMaxOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::LessOptionsT *AsLessOptions() { + return type == BuiltinOptions_LessOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::LessOptionsT *AsLessOptions() const { + return type == BuiltinOptions_LessOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::NegOptionsT *AsNegOptions() { + return type == BuiltinOptions_NegOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::NegOptionsT *AsNegOptions() const { + return type == BuiltinOptions_NegOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::PadV2OptionsT *AsPadV2Options() { + return type == BuiltinOptions_PadV2Options ? + reinterpret_cast(value) : nullptr; + } + const tflite::PadV2OptionsT *AsPadV2Options() const { + return type == BuiltinOptions_PadV2Options ? + reinterpret_cast(value) : nullptr; + } + tflite::GreaterOptionsT *AsGreaterOptions() { + return type == BuiltinOptions_GreaterOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::GreaterOptionsT *AsGreaterOptions() const { + return type == BuiltinOptions_GreaterOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::GreaterEqualOptionsT *AsGreaterEqualOptions() { + return type == BuiltinOptions_GreaterEqualOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::GreaterEqualOptionsT *AsGreaterEqualOptions() const { + return type == BuiltinOptions_GreaterEqualOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::LessEqualOptionsT *AsLessEqualOptions() { + return type == BuiltinOptions_LessEqualOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::LessEqualOptionsT *AsLessEqualOptions() const { + return type == BuiltinOptions_LessEqualOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::SelectOptionsT *AsSelectOptions() { + return type == BuiltinOptions_SelectOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::SelectOptionsT *AsSelectOptions() const { + return type == BuiltinOptions_SelectOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::SliceOptionsT *AsSliceOptions() { + return type == BuiltinOptions_SliceOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::SliceOptionsT *AsSliceOptions() const { + return type == BuiltinOptions_SliceOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::TransposeConvOptionsT *AsTransposeConvOptions() { + return type == BuiltinOptions_TransposeConvOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::TransposeConvOptionsT *AsTransposeConvOptions() const { + return type == BuiltinOptions_TransposeConvOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::SparseToDenseOptionsT *AsSparseToDenseOptions() { + return type == BuiltinOptions_SparseToDenseOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::SparseToDenseOptionsT *AsSparseToDenseOptions() const { + return type == BuiltinOptions_SparseToDenseOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::TileOptionsT *AsTileOptions() { + return type == BuiltinOptions_TileOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::TileOptionsT *AsTileOptions() const { + return type == BuiltinOptions_TileOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::ExpandDimsOptionsT *AsExpandDimsOptions() { + return type == BuiltinOptions_ExpandDimsOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::ExpandDimsOptionsT *AsExpandDimsOptions() const { + return type == BuiltinOptions_ExpandDimsOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::EqualOptionsT *AsEqualOptions() { + return type == BuiltinOptions_EqualOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::EqualOptionsT *AsEqualOptions() const { + return type == BuiltinOptions_EqualOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::NotEqualOptionsT *AsNotEqualOptions() { + return type == BuiltinOptions_NotEqualOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::NotEqualOptionsT *AsNotEqualOptions() const { + return type == BuiltinOptions_NotEqualOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::ShapeOptionsT *AsShapeOptions() { + return type == BuiltinOptions_ShapeOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::ShapeOptionsT *AsShapeOptions() const { + return type == BuiltinOptions_ShapeOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::PowOptionsT *AsPowOptions() { + return type == BuiltinOptions_PowOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::PowOptionsT *AsPowOptions() const { + return type == BuiltinOptions_PowOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::ArgMinOptionsT *AsArgMinOptions() { + return type == BuiltinOptions_ArgMinOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::ArgMinOptionsT *AsArgMinOptions() const { + return type == BuiltinOptions_ArgMinOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::FakeQuantOptionsT *AsFakeQuantOptions() { + return type == BuiltinOptions_FakeQuantOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::FakeQuantOptionsT *AsFakeQuantOptions() const { + return type == BuiltinOptions_FakeQuantOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::PackOptionsT *AsPackOptions() { + return type == BuiltinOptions_PackOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::PackOptionsT *AsPackOptions() const { + return type == BuiltinOptions_PackOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::LogicalOrOptionsT *AsLogicalOrOptions() { + return type == BuiltinOptions_LogicalOrOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::LogicalOrOptionsT *AsLogicalOrOptions() const { + return type == BuiltinOptions_LogicalOrOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::OneHotOptionsT *AsOneHotOptions() { + return type == BuiltinOptions_OneHotOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::OneHotOptionsT *AsOneHotOptions() const { + return type == BuiltinOptions_OneHotOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::LogicalAndOptionsT *AsLogicalAndOptions() { + return type == BuiltinOptions_LogicalAndOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::LogicalAndOptionsT *AsLogicalAndOptions() const { + return type == BuiltinOptions_LogicalAndOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::LogicalNotOptionsT *AsLogicalNotOptions() { + return type == BuiltinOptions_LogicalNotOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::LogicalNotOptionsT *AsLogicalNotOptions() const { + return type == BuiltinOptions_LogicalNotOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::UnpackOptionsT *AsUnpackOptions() { + return type == BuiltinOptions_UnpackOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::UnpackOptionsT *AsUnpackOptions() const { + return type == BuiltinOptions_UnpackOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::FloorDivOptionsT *AsFloorDivOptions() { + return type == BuiltinOptions_FloorDivOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::FloorDivOptionsT *AsFloorDivOptions() const { + return type == BuiltinOptions_FloorDivOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::SquareOptionsT *AsSquareOptions() { + return type == BuiltinOptions_SquareOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::SquareOptionsT *AsSquareOptions() const { + return type == BuiltinOptions_SquareOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::ZerosLikeOptionsT *AsZerosLikeOptions() { + return type == BuiltinOptions_ZerosLikeOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::ZerosLikeOptionsT *AsZerosLikeOptions() const { + return type == BuiltinOptions_ZerosLikeOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::FillOptionsT *AsFillOptions() { + return type == BuiltinOptions_FillOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::FillOptionsT *AsFillOptions() const { + return type == BuiltinOptions_FillOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::BidirectionalSequenceLSTMOptionsT *AsBidirectionalSequenceLSTMOptions() { + return type == BuiltinOptions_BidirectionalSequenceLSTMOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::BidirectionalSequenceLSTMOptionsT *AsBidirectionalSequenceLSTMOptions() const { + return type == BuiltinOptions_BidirectionalSequenceLSTMOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::BidirectionalSequenceRNNOptionsT *AsBidirectionalSequenceRNNOptions() { + return type == BuiltinOptions_BidirectionalSequenceRNNOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::BidirectionalSequenceRNNOptionsT *AsBidirectionalSequenceRNNOptions() const { + return type == BuiltinOptions_BidirectionalSequenceRNNOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::UnidirectionalSequenceLSTMOptionsT *AsUnidirectionalSequenceLSTMOptions() { + return type == BuiltinOptions_UnidirectionalSequenceLSTMOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::UnidirectionalSequenceLSTMOptionsT *AsUnidirectionalSequenceLSTMOptions() const { + return type == BuiltinOptions_UnidirectionalSequenceLSTMOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::FloorModOptionsT *AsFloorModOptions() { + return type == BuiltinOptions_FloorModOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::FloorModOptionsT *AsFloorModOptions() const { + return type == BuiltinOptions_FloorModOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::RangeOptionsT *AsRangeOptions() { + return type == BuiltinOptions_RangeOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::RangeOptionsT *AsRangeOptions() const { + return type == BuiltinOptions_RangeOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::ResizeNearestNeighborOptionsT *AsResizeNearestNeighborOptions() { + return type == BuiltinOptions_ResizeNearestNeighborOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::ResizeNearestNeighborOptionsT *AsResizeNearestNeighborOptions() const { + return type == BuiltinOptions_ResizeNearestNeighborOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::LeakyReluOptionsT *AsLeakyReluOptions() { + return type == BuiltinOptions_LeakyReluOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::LeakyReluOptionsT *AsLeakyReluOptions() const { + return type == BuiltinOptions_LeakyReluOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::SquaredDifferenceOptionsT *AsSquaredDifferenceOptions() { + return type == BuiltinOptions_SquaredDifferenceOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::SquaredDifferenceOptionsT *AsSquaredDifferenceOptions() const { + return type == BuiltinOptions_SquaredDifferenceOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::MirrorPadOptionsT *AsMirrorPadOptions() { + return type == BuiltinOptions_MirrorPadOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::MirrorPadOptionsT *AsMirrorPadOptions() const { + return type == BuiltinOptions_MirrorPadOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::AbsOptionsT *AsAbsOptions() { + return type == BuiltinOptions_AbsOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::AbsOptionsT *AsAbsOptions() const { + return type == BuiltinOptions_AbsOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::SplitVOptionsT *AsSplitVOptions() { + return type == BuiltinOptions_SplitVOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::SplitVOptionsT *AsSplitVOptions() const { + return type == BuiltinOptions_SplitVOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::UniqueOptionsT *AsUniqueOptions() { + return type == BuiltinOptions_UniqueOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::UniqueOptionsT *AsUniqueOptions() const { + return type == BuiltinOptions_UniqueOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::ReverseV2OptionsT *AsReverseV2Options() { + return type == BuiltinOptions_ReverseV2Options ? + reinterpret_cast(value) : nullptr; + } + const tflite::ReverseV2OptionsT *AsReverseV2Options() const { + return type == BuiltinOptions_ReverseV2Options ? + reinterpret_cast(value) : nullptr; + } + tflite::AddNOptionsT *AsAddNOptions() { + return type == BuiltinOptions_AddNOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::AddNOptionsT *AsAddNOptions() const { + return type == BuiltinOptions_AddNOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::GatherNdOptionsT *AsGatherNdOptions() { + return type == BuiltinOptions_GatherNdOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::GatherNdOptionsT *AsGatherNdOptions() const { + return type == BuiltinOptions_GatherNdOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::CosOptionsT *AsCosOptions() { + return type == BuiltinOptions_CosOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::CosOptionsT *AsCosOptions() const { + return type == BuiltinOptions_CosOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::WhereOptionsT *AsWhereOptions() { + return type == BuiltinOptions_WhereOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::WhereOptionsT *AsWhereOptions() const { + return type == BuiltinOptions_WhereOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::RankOptionsT *AsRankOptions() { + return type == BuiltinOptions_RankOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::RankOptionsT *AsRankOptions() const { + return type == BuiltinOptions_RankOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::ReverseSequenceOptionsT *AsReverseSequenceOptions() { + return type == BuiltinOptions_ReverseSequenceOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::ReverseSequenceOptionsT *AsReverseSequenceOptions() const { + return type == BuiltinOptions_ReverseSequenceOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::MatrixDiagOptionsT *AsMatrixDiagOptions() { + return type == BuiltinOptions_MatrixDiagOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::MatrixDiagOptionsT *AsMatrixDiagOptions() const { + return type == BuiltinOptions_MatrixDiagOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::QuantizeOptionsT *AsQuantizeOptions() { + return type == BuiltinOptions_QuantizeOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::QuantizeOptionsT *AsQuantizeOptions() const { + return type == BuiltinOptions_QuantizeOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::MatrixSetDiagOptionsT *AsMatrixSetDiagOptions() { + return type == BuiltinOptions_MatrixSetDiagOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::MatrixSetDiagOptionsT *AsMatrixSetDiagOptions() const { + return type == BuiltinOptions_MatrixSetDiagOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::HardSwishOptionsT *AsHardSwishOptions() { + return type == BuiltinOptions_HardSwishOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::HardSwishOptionsT *AsHardSwishOptions() const { + return type == BuiltinOptions_HardSwishOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::IfOptionsT *AsIfOptions() { + return type == BuiltinOptions_IfOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::IfOptionsT *AsIfOptions() const { + return type == BuiltinOptions_IfOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::WhileOptionsT *AsWhileOptions() { + return type == BuiltinOptions_WhileOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::WhileOptionsT *AsWhileOptions() const { + return type == BuiltinOptions_WhileOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::DepthToSpaceOptionsT *AsDepthToSpaceOptions() { + return type == BuiltinOptions_DepthToSpaceOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::DepthToSpaceOptionsT *AsDepthToSpaceOptions() const { + return type == BuiltinOptions_DepthToSpaceOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::NonMaxSuppressionV4OptionsT *AsNonMaxSuppressionV4Options() { + return type == BuiltinOptions_NonMaxSuppressionV4Options ? + reinterpret_cast(value) : nullptr; + } + const tflite::NonMaxSuppressionV4OptionsT *AsNonMaxSuppressionV4Options() const { + return type == BuiltinOptions_NonMaxSuppressionV4Options ? + reinterpret_cast(value) : nullptr; + } + tflite::NonMaxSuppressionV5OptionsT *AsNonMaxSuppressionV5Options() { + return type == BuiltinOptions_NonMaxSuppressionV5Options ? + reinterpret_cast(value) : nullptr; + } + const tflite::NonMaxSuppressionV5OptionsT *AsNonMaxSuppressionV5Options() const { + return type == BuiltinOptions_NonMaxSuppressionV5Options ? + reinterpret_cast(value) : nullptr; + } + tflite::ScatterNdOptionsT *AsScatterNdOptions() { + return type == BuiltinOptions_ScatterNdOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::ScatterNdOptionsT *AsScatterNdOptions() const { + return type == BuiltinOptions_ScatterNdOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::SelectV2OptionsT *AsSelectV2Options() { + return type == BuiltinOptions_SelectV2Options ? + reinterpret_cast(value) : nullptr; + } + const tflite::SelectV2OptionsT *AsSelectV2Options() const { + return type == BuiltinOptions_SelectV2Options ? + reinterpret_cast(value) : nullptr; + } + tflite::DensifyOptionsT *AsDensifyOptions() { + return type == BuiltinOptions_DensifyOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::DensifyOptionsT *AsDensifyOptions() const { + return type == BuiltinOptions_DensifyOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::SegmentSumOptionsT *AsSegmentSumOptions() { + return type == BuiltinOptions_SegmentSumOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::SegmentSumOptionsT *AsSegmentSumOptions() const { + return type == BuiltinOptions_SegmentSumOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::BatchMatMulOptionsT *AsBatchMatMulOptions() { + return type == BuiltinOptions_BatchMatMulOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::BatchMatMulOptionsT *AsBatchMatMulOptions() const { + return type == BuiltinOptions_BatchMatMulOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::CumsumOptionsT *AsCumsumOptions() { + return type == BuiltinOptions_CumsumOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::CumsumOptionsT *AsCumsumOptions() const { + return type == BuiltinOptions_CumsumOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::CallOnceOptionsT *AsCallOnceOptions() { + return type == BuiltinOptions_CallOnceOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::CallOnceOptionsT *AsCallOnceOptions() const { + return type == BuiltinOptions_CallOnceOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::BroadcastToOptionsT *AsBroadcastToOptions() { + return type == BuiltinOptions_BroadcastToOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::BroadcastToOptionsT *AsBroadcastToOptions() const { + return type == BuiltinOptions_BroadcastToOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::Rfft2dOptionsT *AsRfft2dOptions() { + return type == BuiltinOptions_Rfft2dOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::Rfft2dOptionsT *AsRfft2dOptions() const { + return type == BuiltinOptions_Rfft2dOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::Conv3DOptionsT *AsConv3DOptions() { + return type == BuiltinOptions_Conv3DOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::Conv3DOptionsT *AsConv3DOptions() const { + return type == BuiltinOptions_Conv3DOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::HashtableOptionsT *AsHashtableOptions() { + return type == BuiltinOptions_HashtableOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::HashtableOptionsT *AsHashtableOptions() const { + return type == BuiltinOptions_HashtableOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::HashtableFindOptionsT *AsHashtableFindOptions() { + return type == BuiltinOptions_HashtableFindOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::HashtableFindOptionsT *AsHashtableFindOptions() const { + return type == BuiltinOptions_HashtableFindOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::HashtableImportOptionsT *AsHashtableImportOptions() { + return type == BuiltinOptions_HashtableImportOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::HashtableImportOptionsT *AsHashtableImportOptions() const { + return type == BuiltinOptions_HashtableImportOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::HashtableSizeOptionsT *AsHashtableSizeOptions() { + return type == BuiltinOptions_HashtableSizeOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::HashtableSizeOptionsT *AsHashtableSizeOptions() const { + return type == BuiltinOptions_HashtableSizeOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::VarHandleOptionsT *AsVarHandleOptions() { + return type == BuiltinOptions_VarHandleOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::VarHandleOptionsT *AsVarHandleOptions() const { + return type == BuiltinOptions_VarHandleOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::ReadVariableOptionsT *AsReadVariableOptions() { + return type == BuiltinOptions_ReadVariableOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::ReadVariableOptionsT *AsReadVariableOptions() const { + return type == BuiltinOptions_ReadVariableOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::AssignVariableOptionsT *AsAssignVariableOptions() { + return type == BuiltinOptions_AssignVariableOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::AssignVariableOptionsT *AsAssignVariableOptions() const { + return type == BuiltinOptions_AssignVariableOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::RandomOptionsT *AsRandomOptions() { + return type == BuiltinOptions_RandomOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::RandomOptionsT *AsRandomOptions() const { + return type == BuiltinOptions_RandomOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::BucketizeOptionsT *AsBucketizeOptions() { + return type == BuiltinOptions_BucketizeOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::BucketizeOptionsT *AsBucketizeOptions() const { + return type == BuiltinOptions_BucketizeOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::GeluOptionsT *AsGeluOptions() { + return type == BuiltinOptions_GeluOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::GeluOptionsT *AsGeluOptions() const { + return type == BuiltinOptions_GeluOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::DynamicUpdateSliceOptionsT *AsDynamicUpdateSliceOptions() { + return type == BuiltinOptions_DynamicUpdateSliceOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::DynamicUpdateSliceOptionsT *AsDynamicUpdateSliceOptions() const { + return type == BuiltinOptions_DynamicUpdateSliceOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::UnsortedSegmentProdOptionsT *AsUnsortedSegmentProdOptions() { + return type == BuiltinOptions_UnsortedSegmentProdOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::UnsortedSegmentProdOptionsT *AsUnsortedSegmentProdOptions() const { + return type == BuiltinOptions_UnsortedSegmentProdOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::UnsortedSegmentMaxOptionsT *AsUnsortedSegmentMaxOptions() { + return type == BuiltinOptions_UnsortedSegmentMaxOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::UnsortedSegmentMaxOptionsT *AsUnsortedSegmentMaxOptions() const { + return type == BuiltinOptions_UnsortedSegmentMaxOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::UnsortedSegmentMinOptionsT *AsUnsortedSegmentMinOptions() { + return type == BuiltinOptions_UnsortedSegmentMinOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::UnsortedSegmentMinOptionsT *AsUnsortedSegmentMinOptions() const { + return type == BuiltinOptions_UnsortedSegmentMinOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::UnsortedSegmentSumOptionsT *AsUnsortedSegmentSumOptions() { + return type == BuiltinOptions_UnsortedSegmentSumOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::UnsortedSegmentSumOptionsT *AsUnsortedSegmentSumOptions() const { + return type == BuiltinOptions_UnsortedSegmentSumOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::ATan2OptionsT *AsATan2Options() { + return type == BuiltinOptions_ATan2Options ? + reinterpret_cast(value) : nullptr; + } + const tflite::ATan2OptionsT *AsATan2Options() const { + return type == BuiltinOptions_ATan2Options ? + reinterpret_cast(value) : nullptr; + } + tflite::SignOptionsT *AsSignOptions() { + return type == BuiltinOptions_SignOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::SignOptionsT *AsSignOptions() const { + return type == BuiltinOptions_SignOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::BitcastOptionsT *AsBitcastOptions() { + return type == BuiltinOptions_BitcastOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::BitcastOptionsT *AsBitcastOptions() const { + return type == BuiltinOptions_BitcastOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::BitwiseXorOptionsT *AsBitwiseXorOptions() { + return type == BuiltinOptions_BitwiseXorOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::BitwiseXorOptionsT *AsBitwiseXorOptions() const { + return type == BuiltinOptions_BitwiseXorOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::RightShiftOptionsT *AsRightShiftOptions() { + return type == BuiltinOptions_RightShiftOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::RightShiftOptionsT *AsRightShiftOptions() const { + return type == BuiltinOptions_RightShiftOptions ? + reinterpret_cast(value) : nullptr; + } +}; + +template +bool VerifyBuiltinOptions(::flatbuffers::VerifierTemplate &verifier, const void *obj, BuiltinOptions type); +template +bool VerifyBuiltinOptionsVector(::flatbuffers::VerifierTemplate &verifier, const ::flatbuffers::Vector<::flatbuffers::Offset> *values, const ::flatbuffers::Vector *types); + +enum BuiltinOptions2 : uint8_t { + BuiltinOptions2_NONE = 0, + BuiltinOptions2_StablehloConcatenateOptions = 1, + BuiltinOptions2_StablehloBroadcastInDimOptions = 2, + BuiltinOptions2_StablehloSliceOptions = 3, + BuiltinOptions2_StablehloConvolutionOptions = 4, + BuiltinOptions2_StablehloCustomCallOptions = 5, + BuiltinOptions2_StablehloReduceOptions = 6, + BuiltinOptions2_StablehloScatterOptions = 7, + BuiltinOptions2_StablehloCompareOptions = 8, + BuiltinOptions2_StablehloDynamicSliceOptions = 9, + BuiltinOptions2_StablehloPadOptions = 10, + BuiltinOptions2_StablehloIotaOptions = 11, + BuiltinOptions2_StablehloDotGeneralOptions = 12, + BuiltinOptions2_StablehloReduceWindowOptions = 13, + BuiltinOptions2_StablehloSortOptions = 14, + BuiltinOptions2_StablehloWhileOptions = 15, + BuiltinOptions2_StablehloGatherOptions = 16, + BuiltinOptions2_StablehloTransposeOptions = 17, + BuiltinOptions2_DilateOptions = 18, + BuiltinOptions2_StablehloRngBitGeneratorOptions = 19, + BuiltinOptions2_ReduceWindowOptions = 20, + BuiltinOptions2_StableHLOCompositeOptions = 21, + BuiltinOptions2_StablehloShiftLeftOptions = 22, + BuiltinOptions2_StablehloCaseOptions = 23, + BuiltinOptions2_MIN = BuiltinOptions2_NONE, + BuiltinOptions2_MAX = BuiltinOptions2_StablehloCaseOptions +}; + +inline const BuiltinOptions2 (&EnumValuesBuiltinOptions2())[24] { + static const BuiltinOptions2 values[] = { + BuiltinOptions2_NONE, + BuiltinOptions2_StablehloConcatenateOptions, + BuiltinOptions2_StablehloBroadcastInDimOptions, + BuiltinOptions2_StablehloSliceOptions, + BuiltinOptions2_StablehloConvolutionOptions, + BuiltinOptions2_StablehloCustomCallOptions, + BuiltinOptions2_StablehloReduceOptions, + BuiltinOptions2_StablehloScatterOptions, + BuiltinOptions2_StablehloCompareOptions, + BuiltinOptions2_StablehloDynamicSliceOptions, + BuiltinOptions2_StablehloPadOptions, + BuiltinOptions2_StablehloIotaOptions, + BuiltinOptions2_StablehloDotGeneralOptions, + BuiltinOptions2_StablehloReduceWindowOptions, + BuiltinOptions2_StablehloSortOptions, + BuiltinOptions2_StablehloWhileOptions, + BuiltinOptions2_StablehloGatherOptions, + BuiltinOptions2_StablehloTransposeOptions, + BuiltinOptions2_DilateOptions, + BuiltinOptions2_StablehloRngBitGeneratorOptions, + BuiltinOptions2_ReduceWindowOptions, + BuiltinOptions2_StableHLOCompositeOptions, + BuiltinOptions2_StablehloShiftLeftOptions, + BuiltinOptions2_StablehloCaseOptions + }; + return values; +} + +inline const char * const *EnumNamesBuiltinOptions2() { + static const char * const names[25] = { + "NONE", + "StablehloConcatenateOptions", + "StablehloBroadcastInDimOptions", + "StablehloSliceOptions", + "StablehloConvolutionOptions", + "StablehloCustomCallOptions", + "StablehloReduceOptions", + "StablehloScatterOptions", + "StablehloCompareOptions", + "StablehloDynamicSliceOptions", + "StablehloPadOptions", + "StablehloIotaOptions", + "StablehloDotGeneralOptions", + "StablehloReduceWindowOptions", + "StablehloSortOptions", + "StablehloWhileOptions", + "StablehloGatherOptions", + "StablehloTransposeOptions", + "DilateOptions", + "StablehloRngBitGeneratorOptions", + "ReduceWindowOptions", + "StableHLOCompositeOptions", + "StablehloShiftLeftOptions", + "StablehloCaseOptions", + nullptr + }; + return names; +} + +inline const char *EnumNameBuiltinOptions2(BuiltinOptions2 e) { + if (::flatbuffers::IsOutRange(e, BuiltinOptions2_NONE, BuiltinOptions2_StablehloCaseOptions)) return ""; + const size_t index = static_cast(e); + return EnumNamesBuiltinOptions2()[index]; +} + +template struct BuiltinOptions2Traits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_NONE; +}; + +template<> struct BuiltinOptions2Traits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloConcatenateOptions; +}; + +template<> struct BuiltinOptions2Traits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloBroadcastInDimOptions; +}; + +template<> struct BuiltinOptions2Traits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloSliceOptions; +}; + +template<> struct BuiltinOptions2Traits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloConvolutionOptions; +}; + +template<> struct BuiltinOptions2Traits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloCustomCallOptions; +}; + +template<> struct BuiltinOptions2Traits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloReduceOptions; +}; + +template<> struct BuiltinOptions2Traits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloScatterOptions; +}; + +template<> struct BuiltinOptions2Traits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloCompareOptions; +}; + +template<> struct BuiltinOptions2Traits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloDynamicSliceOptions; +}; + +template<> struct BuiltinOptions2Traits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloPadOptions; +}; + +template<> struct BuiltinOptions2Traits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloIotaOptions; +}; + +template<> struct BuiltinOptions2Traits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloDotGeneralOptions; +}; + +template<> struct BuiltinOptions2Traits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloReduceWindowOptions; +}; + +template<> struct BuiltinOptions2Traits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloSortOptions; +}; + +template<> struct BuiltinOptions2Traits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloWhileOptions; +}; + +template<> struct BuiltinOptions2Traits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloGatherOptions; +}; + +template<> struct BuiltinOptions2Traits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloTransposeOptions; +}; + +template<> struct BuiltinOptions2Traits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_DilateOptions; +}; + +template<> struct BuiltinOptions2Traits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloRngBitGeneratorOptions; +}; + +template<> struct BuiltinOptions2Traits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_ReduceWindowOptions; +}; + +template<> struct BuiltinOptions2Traits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StableHLOCompositeOptions; +}; + +template<> struct BuiltinOptions2Traits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloShiftLeftOptions; +}; + +template<> struct BuiltinOptions2Traits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloCaseOptions; +}; + +template struct BuiltinOptions2UnionTraits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_NONE; +}; + +template<> struct BuiltinOptions2UnionTraits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloConcatenateOptions; +}; + +template<> struct BuiltinOptions2UnionTraits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloBroadcastInDimOptions; +}; + +template<> struct BuiltinOptions2UnionTraits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloSliceOptions; +}; + +template<> struct BuiltinOptions2UnionTraits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloConvolutionOptions; +}; + +template<> struct BuiltinOptions2UnionTraits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloCustomCallOptions; +}; + +template<> struct BuiltinOptions2UnionTraits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloReduceOptions; +}; + +template<> struct BuiltinOptions2UnionTraits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloScatterOptions; +}; + +template<> struct BuiltinOptions2UnionTraits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloCompareOptions; +}; + +template<> struct BuiltinOptions2UnionTraits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloDynamicSliceOptions; +}; + +template<> struct BuiltinOptions2UnionTraits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloPadOptions; +}; + +template<> struct BuiltinOptions2UnionTraits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloIotaOptions; +}; + +template<> struct BuiltinOptions2UnionTraits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloDotGeneralOptions; +}; + +template<> struct BuiltinOptions2UnionTraits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloReduceWindowOptions; +}; + +template<> struct BuiltinOptions2UnionTraits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloSortOptions; +}; + +template<> struct BuiltinOptions2UnionTraits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloWhileOptions; +}; + +template<> struct BuiltinOptions2UnionTraits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloGatherOptions; +}; + +template<> struct BuiltinOptions2UnionTraits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloTransposeOptions; +}; + +template<> struct BuiltinOptions2UnionTraits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_DilateOptions; +}; + +template<> struct BuiltinOptions2UnionTraits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloRngBitGeneratorOptions; +}; + +template<> struct BuiltinOptions2UnionTraits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_ReduceWindowOptions; +}; + +template<> struct BuiltinOptions2UnionTraits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StableHLOCompositeOptions; +}; + +template<> struct BuiltinOptions2UnionTraits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloShiftLeftOptions; +}; + +template<> struct BuiltinOptions2UnionTraits { + static const BuiltinOptions2 enum_value = BuiltinOptions2_StablehloCaseOptions; +}; + +struct BuiltinOptions2Union { + BuiltinOptions2 type; + void *value; + + BuiltinOptions2Union() : type(BuiltinOptions2_NONE), value(nullptr) {} + BuiltinOptions2Union(BuiltinOptions2Union&& u) FLATBUFFERS_NOEXCEPT : + type(BuiltinOptions2_NONE), value(nullptr) + { std::swap(type, u.type); std::swap(value, u.value); } + BuiltinOptions2Union(const BuiltinOptions2Union &); + BuiltinOptions2Union &operator=(const BuiltinOptions2Union &u) + { BuiltinOptions2Union t(u); std::swap(type, t.type); std::swap(value, t.value); return *this; } + BuiltinOptions2Union &operator=(BuiltinOptions2Union &&u) FLATBUFFERS_NOEXCEPT + { std::swap(type, u.type); std::swap(value, u.value); return *this; } + ~BuiltinOptions2Union() { Reset(); } + + void Reset(); + + template + void Set(T&& val) { + typedef typename std::remove_reference::type RT; + Reset(); + type = BuiltinOptions2UnionTraits::enum_value; + if (type != BuiltinOptions2_NONE) { + value = new RT(std::forward(val)); + } + } + + static void *UnPack(const void *obj, enum BuiltinOptions2 type, const ::flatbuffers::resolver_function_t *resolver); + ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr) const; + + tflite::StablehloConcatenateOptionsT *AsStablehloConcatenateOptions() { + return type == BuiltinOptions2_StablehloConcatenateOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::StablehloConcatenateOptionsT *AsStablehloConcatenateOptions() const { + return type == BuiltinOptions2_StablehloConcatenateOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::StablehloBroadcastInDimOptionsT *AsStablehloBroadcastInDimOptions() { + return type == BuiltinOptions2_StablehloBroadcastInDimOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::StablehloBroadcastInDimOptionsT *AsStablehloBroadcastInDimOptions() const { + return type == BuiltinOptions2_StablehloBroadcastInDimOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::StablehloSliceOptionsT *AsStablehloSliceOptions() { + return type == BuiltinOptions2_StablehloSliceOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::StablehloSliceOptionsT *AsStablehloSliceOptions() const { + return type == BuiltinOptions2_StablehloSliceOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::StablehloConvolutionOptionsT *AsStablehloConvolutionOptions() { + return type == BuiltinOptions2_StablehloConvolutionOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::StablehloConvolutionOptionsT *AsStablehloConvolutionOptions() const { + return type == BuiltinOptions2_StablehloConvolutionOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::StablehloCustomCallOptionsT *AsStablehloCustomCallOptions() { + return type == BuiltinOptions2_StablehloCustomCallOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::StablehloCustomCallOptionsT *AsStablehloCustomCallOptions() const { + return type == BuiltinOptions2_StablehloCustomCallOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::StablehloReduceOptionsT *AsStablehloReduceOptions() { + return type == BuiltinOptions2_StablehloReduceOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::StablehloReduceOptionsT *AsStablehloReduceOptions() const { + return type == BuiltinOptions2_StablehloReduceOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::StablehloScatterOptionsT *AsStablehloScatterOptions() { + return type == BuiltinOptions2_StablehloScatterOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::StablehloScatterOptionsT *AsStablehloScatterOptions() const { + return type == BuiltinOptions2_StablehloScatterOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::StablehloCompareOptionsT *AsStablehloCompareOptions() { + return type == BuiltinOptions2_StablehloCompareOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::StablehloCompareOptionsT *AsStablehloCompareOptions() const { + return type == BuiltinOptions2_StablehloCompareOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::StablehloDynamicSliceOptionsT *AsStablehloDynamicSliceOptions() { + return type == BuiltinOptions2_StablehloDynamicSliceOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::StablehloDynamicSliceOptionsT *AsStablehloDynamicSliceOptions() const { + return type == BuiltinOptions2_StablehloDynamicSliceOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::StablehloPadOptionsT *AsStablehloPadOptions() { + return type == BuiltinOptions2_StablehloPadOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::StablehloPadOptionsT *AsStablehloPadOptions() const { + return type == BuiltinOptions2_StablehloPadOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::StablehloIotaOptionsT *AsStablehloIotaOptions() { + return type == BuiltinOptions2_StablehloIotaOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::StablehloIotaOptionsT *AsStablehloIotaOptions() const { + return type == BuiltinOptions2_StablehloIotaOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::StablehloDotGeneralOptionsT *AsStablehloDotGeneralOptions() { + return type == BuiltinOptions2_StablehloDotGeneralOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::StablehloDotGeneralOptionsT *AsStablehloDotGeneralOptions() const { + return type == BuiltinOptions2_StablehloDotGeneralOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::StablehloReduceWindowOptionsT *AsStablehloReduceWindowOptions() { + return type == BuiltinOptions2_StablehloReduceWindowOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::StablehloReduceWindowOptionsT *AsStablehloReduceWindowOptions() const { + return type == BuiltinOptions2_StablehloReduceWindowOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::StablehloSortOptionsT *AsStablehloSortOptions() { + return type == BuiltinOptions2_StablehloSortOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::StablehloSortOptionsT *AsStablehloSortOptions() const { + return type == BuiltinOptions2_StablehloSortOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::StablehloWhileOptionsT *AsStablehloWhileOptions() { + return type == BuiltinOptions2_StablehloWhileOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::StablehloWhileOptionsT *AsStablehloWhileOptions() const { + return type == BuiltinOptions2_StablehloWhileOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::StablehloGatherOptionsT *AsStablehloGatherOptions() { + return type == BuiltinOptions2_StablehloGatherOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::StablehloGatherOptionsT *AsStablehloGatherOptions() const { + return type == BuiltinOptions2_StablehloGatherOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::StablehloTransposeOptionsT *AsStablehloTransposeOptions() { + return type == BuiltinOptions2_StablehloTransposeOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::StablehloTransposeOptionsT *AsStablehloTransposeOptions() const { + return type == BuiltinOptions2_StablehloTransposeOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::DilateOptionsT *AsDilateOptions() { + return type == BuiltinOptions2_DilateOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::DilateOptionsT *AsDilateOptions() const { + return type == BuiltinOptions2_DilateOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::StablehloRngBitGeneratorOptionsT *AsStablehloRngBitGeneratorOptions() { + return type == BuiltinOptions2_StablehloRngBitGeneratorOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::StablehloRngBitGeneratorOptionsT *AsStablehloRngBitGeneratorOptions() const { + return type == BuiltinOptions2_StablehloRngBitGeneratorOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::ReduceWindowOptionsT *AsReduceWindowOptions() { + return type == BuiltinOptions2_ReduceWindowOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::ReduceWindowOptionsT *AsReduceWindowOptions() const { + return type == BuiltinOptions2_ReduceWindowOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::StableHLOCompositeOptionsT *AsStableHLOCompositeOptions() { + return type == BuiltinOptions2_StableHLOCompositeOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::StableHLOCompositeOptionsT *AsStableHLOCompositeOptions() const { + return type == BuiltinOptions2_StableHLOCompositeOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::StablehloShiftLeftOptionsT *AsStablehloShiftLeftOptions() { + return type == BuiltinOptions2_StablehloShiftLeftOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::StablehloShiftLeftOptionsT *AsStablehloShiftLeftOptions() const { + return type == BuiltinOptions2_StablehloShiftLeftOptions ? + reinterpret_cast(value) : nullptr; + } + tflite::StablehloCaseOptionsT *AsStablehloCaseOptions() { + return type == BuiltinOptions2_StablehloCaseOptions ? + reinterpret_cast(value) : nullptr; + } + const tflite::StablehloCaseOptionsT *AsStablehloCaseOptions() const { + return type == BuiltinOptions2_StablehloCaseOptions ? + reinterpret_cast(value) : nullptr; + } +}; + +template +bool VerifyBuiltinOptions2(::flatbuffers::VerifierTemplate &verifier, const void *obj, BuiltinOptions2 type); +template +bool VerifyBuiltinOptions2Vector(::flatbuffers::VerifierTemplate &verifier, const ::flatbuffers::Vector<::flatbuffers::Offset> *values, const ::flatbuffers::Vector *types); + +enum StablehloPrecisionConfig : uint32_t { + StablehloPrecisionConfig_DEFAULT = 0, + StablehloPrecisionConfig_HIGH = 1, + StablehloPrecisionConfig_HIGHEST = 2, + StablehloPrecisionConfig_MIN = StablehloPrecisionConfig_DEFAULT, + StablehloPrecisionConfig_MAX = StablehloPrecisionConfig_HIGHEST +}; + +inline const StablehloPrecisionConfig (&EnumValuesStablehloPrecisionConfig())[3] { + static const StablehloPrecisionConfig values[] = { + StablehloPrecisionConfig_DEFAULT, + StablehloPrecisionConfig_HIGH, + StablehloPrecisionConfig_HIGHEST + }; + return values; +} + +inline const char * const *EnumNamesStablehloPrecisionConfig() { + static const char * const names[4] = { + "DEFAULT", + "HIGH", + "HIGHEST", + nullptr + }; + return names; +} + +inline const char *EnumNameStablehloPrecisionConfig(StablehloPrecisionConfig e) { + if (::flatbuffers::IsOutRange(e, StablehloPrecisionConfig_DEFAULT, StablehloPrecisionConfig_HIGHEST)) return ""; + const size_t index = static_cast(e); + return EnumNamesStablehloPrecisionConfig()[index]; +} + +enum StablehloComparisonDirection : uint32_t { + StablehloComparisonDirection_STABLEHLO_COMPARISON_DIRECTION_EQ = 0, + StablehloComparisonDirection_STABLEHLO_COMPARISON_DIRECTION_NE = 1, + StablehloComparisonDirection_STABLEHLO_COMPARISON_DIRECTION_GE = 2, + StablehloComparisonDirection_STABLEHLO_COMPARISON_DIRECTION_GT = 3, + StablehloComparisonDirection_STABLEHLO_COMPARISON_DIRECTION_LE = 4, + StablehloComparisonDirection_STABLEHLO_COMPARISON_DIRECTION_LT = 5, + StablehloComparisonDirection_MIN = StablehloComparisonDirection_STABLEHLO_COMPARISON_DIRECTION_EQ, + StablehloComparisonDirection_MAX = StablehloComparisonDirection_STABLEHLO_COMPARISON_DIRECTION_LT +}; + +inline const StablehloComparisonDirection (&EnumValuesStablehloComparisonDirection())[6] { + static const StablehloComparisonDirection values[] = { + StablehloComparisonDirection_STABLEHLO_COMPARISON_DIRECTION_EQ, + StablehloComparisonDirection_STABLEHLO_COMPARISON_DIRECTION_NE, + StablehloComparisonDirection_STABLEHLO_COMPARISON_DIRECTION_GE, + StablehloComparisonDirection_STABLEHLO_COMPARISON_DIRECTION_GT, + StablehloComparisonDirection_STABLEHLO_COMPARISON_DIRECTION_LE, + StablehloComparisonDirection_STABLEHLO_COMPARISON_DIRECTION_LT + }; + return values; +} + +inline const char * const *EnumNamesStablehloComparisonDirection() { + static const char * const names[7] = { + "STABLEHLO_COMPARISON_DIRECTION_EQ", + "STABLEHLO_COMPARISON_DIRECTION_NE", + "STABLEHLO_COMPARISON_DIRECTION_GE", + "STABLEHLO_COMPARISON_DIRECTION_GT", + "STABLEHLO_COMPARISON_DIRECTION_LE", + "STABLEHLO_COMPARISON_DIRECTION_LT", + nullptr + }; + return names; +} + +inline const char *EnumNameStablehloComparisonDirection(StablehloComparisonDirection e) { + if (::flatbuffers::IsOutRange(e, StablehloComparisonDirection_STABLEHLO_COMPARISON_DIRECTION_EQ, StablehloComparisonDirection_STABLEHLO_COMPARISON_DIRECTION_LT)) return ""; + const size_t index = static_cast(e); + return EnumNamesStablehloComparisonDirection()[index]; +} + +enum StablehloComparisonType : uint32_t { + StablehloComparisonType_STABLEHLO_COMPARISON_TYPE_NOTYPE = 0, + StablehloComparisonType_STABLEHLO_COMPARISON_TYPE_FLOAT = 1, + StablehloComparisonType_STABLEHLO_COMPARISON_TYPE_FLOAT_TOTAL_ORDER = 2, + StablehloComparisonType_STABLEHLO_COMPARISON_TYPE_SIGNED = 3, + StablehloComparisonType_STABLEHLO_COMPARISON_TYPE_UNSIGNED = 4, + StablehloComparisonType_MIN = StablehloComparisonType_STABLEHLO_COMPARISON_TYPE_NOTYPE, + StablehloComparisonType_MAX = StablehloComparisonType_STABLEHLO_COMPARISON_TYPE_UNSIGNED +}; + +inline const StablehloComparisonType (&EnumValuesStablehloComparisonType())[5] { + static const StablehloComparisonType values[] = { + StablehloComparisonType_STABLEHLO_COMPARISON_TYPE_NOTYPE, + StablehloComparisonType_STABLEHLO_COMPARISON_TYPE_FLOAT, + StablehloComparisonType_STABLEHLO_COMPARISON_TYPE_FLOAT_TOTAL_ORDER, + StablehloComparisonType_STABLEHLO_COMPARISON_TYPE_SIGNED, + StablehloComparisonType_STABLEHLO_COMPARISON_TYPE_UNSIGNED + }; + return values; +} + +inline const char * const *EnumNamesStablehloComparisonType() { + static const char * const names[6] = { + "STABLEHLO_COMPARISON_TYPE_NOTYPE", + "STABLEHLO_COMPARISON_TYPE_FLOAT", + "STABLEHLO_COMPARISON_TYPE_FLOAT_TOTAL_ORDER", + "STABLEHLO_COMPARISON_TYPE_SIGNED", + "STABLEHLO_COMPARISON_TYPE_UNSIGNED", + nullptr + }; + return names; +} + +inline const char *EnumNameStablehloComparisonType(StablehloComparisonType e) { + if (::flatbuffers::IsOutRange(e, StablehloComparisonType_STABLEHLO_COMPARISON_TYPE_NOTYPE, StablehloComparisonType_STABLEHLO_COMPARISON_TYPE_UNSIGNED)) return ""; + const size_t index = static_cast(e); + return EnumNamesStablehloComparisonType()[index]; +} + +enum RngAlgorithm : int8_t { + RngAlgorithm_DEFAULT = 0, + RngAlgorithm_PHILOX = 1, + RngAlgorithm_THREEFRY = 2, + RngAlgorithm_MIN = RngAlgorithm_DEFAULT, + RngAlgorithm_MAX = RngAlgorithm_THREEFRY +}; + +inline const RngAlgorithm (&EnumValuesRngAlgorithm())[3] { + static const RngAlgorithm values[] = { + RngAlgorithm_DEFAULT, + RngAlgorithm_PHILOX, + RngAlgorithm_THREEFRY + }; + return values; +} + +inline const char * const *EnumNamesRngAlgorithm() { + static const char * const names[4] = { + "DEFAULT", + "PHILOX", + "THREEFRY", + nullptr + }; + return names; +} + +inline const char *EnumNameRngAlgorithm(RngAlgorithm e) { + if (::flatbuffers::IsOutRange(e, RngAlgorithm_DEFAULT, RngAlgorithm_THREEFRY)) return ""; + const size_t index = static_cast(e); + return EnumNamesRngAlgorithm()[index]; +} + +enum Padding : int8_t { + Padding_SAME = 0, + Padding_VALID = 1, + Padding_MIN = Padding_SAME, + Padding_MAX = Padding_VALID +}; + +inline const Padding (&EnumValuesPadding())[2] { + static const Padding values[] = { + Padding_SAME, + Padding_VALID + }; + return values; +} + +inline const char * const *EnumNamesPadding() { + static const char * const names[3] = { + "SAME", + "VALID", + nullptr + }; + return names; +} + +inline const char *EnumNamePadding(Padding e) { + if (::flatbuffers::IsOutRange(e, Padding_SAME, Padding_VALID)) return ""; + const size_t index = static_cast(e); + return EnumNamesPadding()[index]; +} + +enum ActivationFunctionType : int8_t { + ActivationFunctionType_NONE = 0, + ActivationFunctionType_RELU = 1, + ActivationFunctionType_RELU_N1_TO_1 = 2, + ActivationFunctionType_RELU6 = 3, + ActivationFunctionType_TANH = 4, + ActivationFunctionType_SIGN_BIT = 5, + ActivationFunctionType_MIN = ActivationFunctionType_NONE, + ActivationFunctionType_MAX = ActivationFunctionType_SIGN_BIT +}; + +inline const ActivationFunctionType (&EnumValuesActivationFunctionType())[6] { + static const ActivationFunctionType values[] = { + ActivationFunctionType_NONE, + ActivationFunctionType_RELU, + ActivationFunctionType_RELU_N1_TO_1, + ActivationFunctionType_RELU6, + ActivationFunctionType_TANH, + ActivationFunctionType_SIGN_BIT + }; + return values; +} + +inline const char * const *EnumNamesActivationFunctionType() { + static const char * const names[7] = { + "NONE", + "RELU", + "RELU_N1_TO_1", + "RELU6", + "TANH", + "SIGN_BIT", + nullptr + }; + return names; +} + +inline const char *EnumNameActivationFunctionType(ActivationFunctionType e) { + if (::flatbuffers::IsOutRange(e, ActivationFunctionType_NONE, ActivationFunctionType_SIGN_BIT)) return ""; + const size_t index = static_cast(e); + return EnumNamesActivationFunctionType()[index]; +} + +enum LSHProjectionType : int8_t { + LSHProjectionType_UNKNOWN = 0, + LSHProjectionType_SPARSE = 1, + LSHProjectionType_DENSE = 2, + LSHProjectionType_MIN = LSHProjectionType_UNKNOWN, + LSHProjectionType_MAX = LSHProjectionType_DENSE +}; + +inline const LSHProjectionType (&EnumValuesLSHProjectionType())[3] { + static const LSHProjectionType values[] = { + LSHProjectionType_UNKNOWN, + LSHProjectionType_SPARSE, + LSHProjectionType_DENSE + }; + return values; +} + +inline const char * const *EnumNamesLSHProjectionType() { + static const char * const names[4] = { + "UNKNOWN", + "SPARSE", + "DENSE", + nullptr + }; + return names; +} + +inline const char *EnumNameLSHProjectionType(LSHProjectionType e) { + if (::flatbuffers::IsOutRange(e, LSHProjectionType_UNKNOWN, LSHProjectionType_DENSE)) return ""; + const size_t index = static_cast(e); + return EnumNamesLSHProjectionType()[index]; +} + +enum FullyConnectedOptionsWeightsFormat : int8_t { + FullyConnectedOptionsWeightsFormat_DEFAULT = 0, + FullyConnectedOptionsWeightsFormat_SHUFFLED4x16INT8 = 1, + FullyConnectedOptionsWeightsFormat_MIN = FullyConnectedOptionsWeightsFormat_DEFAULT, + FullyConnectedOptionsWeightsFormat_MAX = FullyConnectedOptionsWeightsFormat_SHUFFLED4x16INT8 +}; + +inline const FullyConnectedOptionsWeightsFormat (&EnumValuesFullyConnectedOptionsWeightsFormat())[2] { + static const FullyConnectedOptionsWeightsFormat values[] = { + FullyConnectedOptionsWeightsFormat_DEFAULT, + FullyConnectedOptionsWeightsFormat_SHUFFLED4x16INT8 + }; + return values; +} + +inline const char * const *EnumNamesFullyConnectedOptionsWeightsFormat() { + static const char * const names[3] = { + "DEFAULT", + "SHUFFLED4x16INT8", + nullptr + }; + return names; +} + +inline const char *EnumNameFullyConnectedOptionsWeightsFormat(FullyConnectedOptionsWeightsFormat e) { + if (::flatbuffers::IsOutRange(e, FullyConnectedOptionsWeightsFormat_DEFAULT, FullyConnectedOptionsWeightsFormat_SHUFFLED4x16INT8)) return ""; + const size_t index = static_cast(e); + return EnumNamesFullyConnectedOptionsWeightsFormat()[index]; +} + +enum LSTMKernelType : int8_t { + LSTMKernelType_FULL = 0, + LSTMKernelType_BASIC = 1, + LSTMKernelType_MIN = LSTMKernelType_FULL, + LSTMKernelType_MAX = LSTMKernelType_BASIC +}; + +inline const LSTMKernelType (&EnumValuesLSTMKernelType())[2] { + static const LSTMKernelType values[] = { + LSTMKernelType_FULL, + LSTMKernelType_BASIC + }; + return values; +} + +inline const char * const *EnumNamesLSTMKernelType() { + static const char * const names[3] = { + "FULL", + "BASIC", + nullptr + }; + return names; +} + +inline const char *EnumNameLSTMKernelType(LSTMKernelType e) { + if (::flatbuffers::IsOutRange(e, LSTMKernelType_FULL, LSTMKernelType_BASIC)) return ""; + const size_t index = static_cast(e); + return EnumNamesLSTMKernelType()[index]; +} + +enum CombinerType : int8_t { + CombinerType_SUM = 0, + CombinerType_MEAN = 1, + CombinerType_SQRTN = 2, + CombinerType_MIN = CombinerType_SUM, + CombinerType_MAX = CombinerType_SQRTN +}; + +inline const CombinerType (&EnumValuesCombinerType())[3] { + static const CombinerType values[] = { + CombinerType_SUM, + CombinerType_MEAN, + CombinerType_SQRTN + }; + return values; +} + +inline const char * const *EnumNamesCombinerType() { + static const char * const names[4] = { + "SUM", + "MEAN", + "SQRTN", + nullptr + }; + return names; +} + +inline const char *EnumNameCombinerType(CombinerType e) { + if (::flatbuffers::IsOutRange(e, CombinerType_SUM, CombinerType_SQRTN)) return ""; + const size_t index = static_cast(e); + return EnumNamesCombinerType()[index]; +} + +enum MirrorPadMode : int8_t { + MirrorPadMode_REFLECT = 0, + MirrorPadMode_SYMMETRIC = 1, + MirrorPadMode_MIN = MirrorPadMode_REFLECT, + MirrorPadMode_MAX = MirrorPadMode_SYMMETRIC +}; + +inline const MirrorPadMode (&EnumValuesMirrorPadMode())[2] { + static const MirrorPadMode values[] = { + MirrorPadMode_REFLECT, + MirrorPadMode_SYMMETRIC + }; + return values; +} + +inline const char * const *EnumNamesMirrorPadMode() { + static const char * const names[3] = { + "REFLECT", + "SYMMETRIC", + nullptr + }; + return names; +} + +inline const char *EnumNameMirrorPadMode(MirrorPadMode e) { + if (::flatbuffers::IsOutRange(e, MirrorPadMode_REFLECT, MirrorPadMode_SYMMETRIC)) return ""; + const size_t index = static_cast(e); + return EnumNamesMirrorPadMode()[index]; +} + +enum ReduceWindowFunction : int32_t { + ReduceWindowFunction_UNSUPPORTED = 0, + ReduceWindowFunction_ADD = 1, + ReduceWindowFunction_MUL = 2, + ReduceWindowFunction_MINIMUM = 3, + ReduceWindowFunction_MAXIMUM = 4, + ReduceWindowFunction_ALL = 5, + ReduceWindowFunction_ANY = 6, + ReduceWindowFunction_MIN = ReduceWindowFunction_UNSUPPORTED, + ReduceWindowFunction_MAX = ReduceWindowFunction_ANY +}; + +inline const ReduceWindowFunction (&EnumValuesReduceWindowFunction())[7] { + static const ReduceWindowFunction values[] = { + ReduceWindowFunction_UNSUPPORTED, + ReduceWindowFunction_ADD, + ReduceWindowFunction_MUL, + ReduceWindowFunction_MINIMUM, + ReduceWindowFunction_MAXIMUM, + ReduceWindowFunction_ALL, + ReduceWindowFunction_ANY + }; + return values; +} + +inline const char * const *EnumNamesReduceWindowFunction() { + static const char * const names[8] = { + "UNSUPPORTED", + "ADD", + "MUL", + "MINIMUM", + "MAXIMUM", + "ALL", + "ANY", + nullptr + }; + return names; +} + +inline const char *EnumNameReduceWindowFunction(ReduceWindowFunction e) { + if (::flatbuffers::IsOutRange(e, ReduceWindowFunction_UNSUPPORTED, ReduceWindowFunction_ANY)) return ""; + const size_t index = static_cast(e); + return EnumNamesReduceWindowFunction()[index]; +} + +enum CustomOptionsFormat : int8_t { + CustomOptionsFormat_FLEXBUFFERS = 0, + CustomOptionsFormat_MIN = CustomOptionsFormat_FLEXBUFFERS, + CustomOptionsFormat_MAX = CustomOptionsFormat_FLEXBUFFERS +}; + +inline const CustomOptionsFormat (&EnumValuesCustomOptionsFormat())[1] { + static const CustomOptionsFormat values[] = { + CustomOptionsFormat_FLEXBUFFERS + }; + return values; +} + +inline const char * const *EnumNamesCustomOptionsFormat() { + static const char * const names[2] = { + "FLEXBUFFERS", + nullptr + }; + return names; +} + +inline const char *EnumNameCustomOptionsFormat(CustomOptionsFormat e) { + if (::flatbuffers::IsOutRange(e, CustomOptionsFormat_FLEXBUFFERS, CustomOptionsFormat_FLEXBUFFERS)) return ""; + const size_t index = static_cast(e); + return EnumNamesCustomOptionsFormat()[index]; +} + +struct CustomQuantizationT : public ::flatbuffers::NativeTable { + typedef CustomQuantization TableType; + std::vector custom{}; +}; + +struct CustomQuantization FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef CustomQuantizationT NativeTableType; + typedef CustomQuantizationBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_CUSTOM = 4 + }; + const ::flatbuffers::Vector *custom() const { + return GetPointer *>(VT_CUSTOM); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_CUSTOM) && + verifier.VerifyVector(custom()) && + verifier.EndTable(); + } + CustomQuantizationT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(CustomQuantizationT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const CustomQuantizationT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct CustomQuantizationBuilder { + typedef CustomQuantization Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_custom(::flatbuffers::Offset<::flatbuffers::Vector> custom) { + fbb_.AddOffset(CustomQuantization::VT_CUSTOM, custom); + } + explicit CustomQuantizationBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateCustomQuantization( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::Vector> custom = 0) { + CustomQuantizationBuilder builder_(_fbb); + builder_.add_custom(custom); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateCustomQuantizationDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const std::vector *custom = nullptr) { + if (custom) { _fbb.ForceVectorAlignment(custom->size(), sizeof(uint8_t), 16); } + auto custom__ = custom ? _fbb.CreateVector(*custom) : 0; + return tflite::CreateCustomQuantization( + _fbb, + custom__); +} + +::flatbuffers::Offset CreateCustomQuantization(::flatbuffers::FlatBufferBuilder &_fbb, const CustomQuantizationT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct BlockwiseQuantizationT : public ::flatbuffers::NativeTable { + typedef BlockwiseQuantization TableType; + int32_t scales = 0; + int32_t zero_points = 0; + int32_t block_size = 0; +}; + +struct BlockwiseQuantization FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef BlockwiseQuantizationT NativeTableType; + typedef BlockwiseQuantizationBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_SCALES = 4, + VT_ZERO_POINTS = 6, + VT_BLOCK_SIZE = 8 + }; + int32_t scales() const { + return GetField(VT_SCALES, 0); + } + int32_t zero_points() const { + return GetField(VT_ZERO_POINTS, 0); + } + int32_t block_size() const { + return GetField(VT_BLOCK_SIZE, 0); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_SCALES, 4) && + VerifyField(verifier, VT_ZERO_POINTS, 4) && + VerifyField(verifier, VT_BLOCK_SIZE, 4) && + verifier.EndTable(); + } + BlockwiseQuantizationT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(BlockwiseQuantizationT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const BlockwiseQuantizationT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct BlockwiseQuantizationBuilder { + typedef BlockwiseQuantization Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_scales(int32_t scales) { + fbb_.AddElement(BlockwiseQuantization::VT_SCALES, scales, 0); + } + void add_zero_points(int32_t zero_points) { + fbb_.AddElement(BlockwiseQuantization::VT_ZERO_POINTS, zero_points, 0); + } + void add_block_size(int32_t block_size) { + fbb_.AddElement(BlockwiseQuantization::VT_BLOCK_SIZE, block_size, 0); + } + explicit BlockwiseQuantizationBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateBlockwiseQuantization( + ::flatbuffers::FlatBufferBuilder &_fbb, + int32_t scales = 0, + int32_t zero_points = 0, + int32_t block_size = 0) { + BlockwiseQuantizationBuilder builder_(_fbb); + builder_.add_block_size(block_size); + builder_.add_zero_points(zero_points); + builder_.add_scales(scales); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateBlockwiseQuantization(::flatbuffers::FlatBufferBuilder &_fbb, const BlockwiseQuantizationT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct MultiAxisQuantizationT : public ::flatbuffers::NativeTable { + typedef MultiAxisQuantization TableType; + int32_t scales = 0; + int32_t zero_points = 0; + int32_t block_size = 0; + std::vector quantized_dimensions{}; +}; + +struct MultiAxisQuantization FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef MultiAxisQuantizationT NativeTableType; + typedef MultiAxisQuantizationBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_SCALES = 4, + VT_ZERO_POINTS = 6, + VT_BLOCK_SIZE = 8, + VT_QUANTIZED_DIMENSIONS = 10 + }; + int32_t scales() const { + return GetField(VT_SCALES, 0); + } + int32_t zero_points() const { + return GetField(VT_ZERO_POINTS, 0); + } + int32_t block_size() const { + return GetField(VT_BLOCK_SIZE, 0); + } + const ::flatbuffers::Vector *quantized_dimensions() const { + return GetPointer *>(VT_QUANTIZED_DIMENSIONS); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_SCALES, 4) && + VerifyField(verifier, VT_ZERO_POINTS, 4) && + VerifyField(verifier, VT_BLOCK_SIZE, 4) && + VerifyOffset(verifier, VT_QUANTIZED_DIMENSIONS) && + verifier.VerifyVector(quantized_dimensions()) && + verifier.EndTable(); + } + MultiAxisQuantizationT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(MultiAxisQuantizationT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const MultiAxisQuantizationT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct MultiAxisQuantizationBuilder { + typedef MultiAxisQuantization Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_scales(int32_t scales) { + fbb_.AddElement(MultiAxisQuantization::VT_SCALES, scales, 0); + } + void add_zero_points(int32_t zero_points) { + fbb_.AddElement(MultiAxisQuantization::VT_ZERO_POINTS, zero_points, 0); + } + void add_block_size(int32_t block_size) { + fbb_.AddElement(MultiAxisQuantization::VT_BLOCK_SIZE, block_size, 0); + } + void add_quantized_dimensions(::flatbuffers::Offset<::flatbuffers::Vector> quantized_dimensions) { + fbb_.AddOffset(MultiAxisQuantization::VT_QUANTIZED_DIMENSIONS, quantized_dimensions); + } + explicit MultiAxisQuantizationBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateMultiAxisQuantization( + ::flatbuffers::FlatBufferBuilder &_fbb, + int32_t scales = 0, + int32_t zero_points = 0, + int32_t block_size = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> quantized_dimensions = 0) { + MultiAxisQuantizationBuilder builder_(_fbb); + builder_.add_quantized_dimensions(quantized_dimensions); + builder_.add_block_size(block_size); + builder_.add_zero_points(zero_points); + builder_.add_scales(scales); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateMultiAxisQuantizationDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + int32_t scales = 0, + int32_t zero_points = 0, + int32_t block_size = 0, + const std::vector *quantized_dimensions = nullptr) { + auto quantized_dimensions__ = quantized_dimensions ? _fbb.CreateVector(*quantized_dimensions) : 0; + return tflite::CreateMultiAxisQuantization( + _fbb, + scales, + zero_points, + block_size, + quantized_dimensions__); +} + +::flatbuffers::Offset CreateMultiAxisQuantization(::flatbuffers::FlatBufferBuilder &_fbb, const MultiAxisQuantizationT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct QuantizationParametersT : public ::flatbuffers::NativeTable { + typedef QuantizationParameters TableType; + std::vector min{}; + std::vector max{}; + std::vector scale{}; + std::vector zero_point{}; + tflite::QuantizationDetailsUnion details{}; + int32_t quantized_dimension = 0; +}; + +struct QuantizationParameters FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef QuantizationParametersT NativeTableType; + typedef QuantizationParametersBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_MIN = 4, + VT_MAX = 6, + VT_SCALE = 8, + VT_ZERO_POINT = 10, + VT_DETAILS_TYPE = 12, + VT_DETAILS = 14, + VT_QUANTIZED_DIMENSION = 16 + }; + const ::flatbuffers::Vector *min() const { + return GetPointer *>(VT_MIN); + } + const ::flatbuffers::Vector *max() const { + return GetPointer *>(VT_MAX); + } + const ::flatbuffers::Vector *scale() const { + return GetPointer *>(VT_SCALE); + } + const ::flatbuffers::Vector *zero_point() const { + return GetPointer *>(VT_ZERO_POINT); + } + tflite::QuantizationDetails details_type() const { + return static_cast(GetField(VT_DETAILS_TYPE, 0)); + } + const void *details() const { + return GetPointer(VT_DETAILS); + } + template const T *details_as() const; + const tflite::CustomQuantization *details_as_CustomQuantization() const { + return details_type() == tflite::QuantizationDetails_CustomQuantization ? static_cast(details()) : nullptr; + } + const tflite::BlockwiseQuantization *details_as_BlockwiseQuantization() const { + return details_type() == tflite::QuantizationDetails_BlockwiseQuantization ? static_cast(details()) : nullptr; + } + const tflite::MultiAxisQuantization *details_as_MultiAxisQuantization() const { + return details_type() == tflite::QuantizationDetails_MultiAxisQuantization ? static_cast(details()) : nullptr; + } + int32_t quantized_dimension() const { + return GetField(VT_QUANTIZED_DIMENSION, 0); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_MIN) && + verifier.VerifyVector(min()) && + VerifyOffset(verifier, VT_MAX) && + verifier.VerifyVector(max()) && + VerifyOffset(verifier, VT_SCALE) && + verifier.VerifyVector(scale()) && + VerifyOffset(verifier, VT_ZERO_POINT) && + verifier.VerifyVector(zero_point()) && + VerifyField(verifier, VT_DETAILS_TYPE, 1) && + VerifyOffset(verifier, VT_DETAILS) && + VerifyQuantizationDetails(verifier, details(), details_type()) && + VerifyField(verifier, VT_QUANTIZED_DIMENSION, 4) && + verifier.EndTable(); + } + QuantizationParametersT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(QuantizationParametersT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const QuantizationParametersT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +template<> inline const tflite::CustomQuantization *QuantizationParameters::details_as() const { + return details_as_CustomQuantization(); +} + +template<> inline const tflite::BlockwiseQuantization *QuantizationParameters::details_as() const { + return details_as_BlockwiseQuantization(); +} + +template<> inline const tflite::MultiAxisQuantization *QuantizationParameters::details_as() const { + return details_as_MultiAxisQuantization(); +} + +struct QuantizationParametersBuilder { + typedef QuantizationParameters Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_min(::flatbuffers::Offset<::flatbuffers::Vector> min) { + fbb_.AddOffset(QuantizationParameters::VT_MIN, min); + } + void add_max(::flatbuffers::Offset<::flatbuffers::Vector> max) { + fbb_.AddOffset(QuantizationParameters::VT_MAX, max); + } + void add_scale(::flatbuffers::Offset<::flatbuffers::Vector> scale) { + fbb_.AddOffset(QuantizationParameters::VT_SCALE, scale); + } + void add_zero_point(::flatbuffers::Offset<::flatbuffers::Vector> zero_point) { + fbb_.AddOffset(QuantizationParameters::VT_ZERO_POINT, zero_point); + } + void add_details_type(tflite::QuantizationDetails details_type) { + fbb_.AddElement(QuantizationParameters::VT_DETAILS_TYPE, static_cast(details_type), 0); + } + void add_details(::flatbuffers::Offset details) { + fbb_.AddOffset(QuantizationParameters::VT_DETAILS, details); + } + void add_quantized_dimension(int32_t quantized_dimension) { + fbb_.AddElement(QuantizationParameters::VT_QUANTIZED_DIMENSION, quantized_dimension, 0); + } + explicit QuantizationParametersBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateQuantizationParameters( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::Vector> min = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> max = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> scale = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> zero_point = 0, + tflite::QuantizationDetails details_type = tflite::QuantizationDetails_NONE, + ::flatbuffers::Offset details = 0, + int32_t quantized_dimension = 0) { + QuantizationParametersBuilder builder_(_fbb); + builder_.add_quantized_dimension(quantized_dimension); + builder_.add_details(details); + builder_.add_zero_point(zero_point); + builder_.add_scale(scale); + builder_.add_max(max); + builder_.add_min(min); + builder_.add_details_type(details_type); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateQuantizationParametersDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const std::vector *min = nullptr, + const std::vector *max = nullptr, + const std::vector *scale = nullptr, + const std::vector *zero_point = nullptr, + tflite::QuantizationDetails details_type = tflite::QuantizationDetails_NONE, + ::flatbuffers::Offset details = 0, + int32_t quantized_dimension = 0) { + auto min__ = min ? _fbb.CreateVector(*min) : 0; + auto max__ = max ? _fbb.CreateVector(*max) : 0; + auto scale__ = scale ? _fbb.CreateVector(*scale) : 0; + auto zero_point__ = zero_point ? _fbb.CreateVector(*zero_point) : 0; + return tflite::CreateQuantizationParameters( + _fbb, + min__, + max__, + scale__, + zero_point__, + details_type, + details, + quantized_dimension); +} + +::flatbuffers::Offset CreateQuantizationParameters(::flatbuffers::FlatBufferBuilder &_fbb, const QuantizationParametersT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct Int32VectorT : public ::flatbuffers::NativeTable { + typedef Int32Vector TableType; + std::vector values{}; +}; + +struct Int32Vector FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef Int32VectorT NativeTableType; + typedef Int32VectorBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_VALUES = 4 + }; + const ::flatbuffers::Vector *values() const { + return GetPointer *>(VT_VALUES); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_VALUES) && + verifier.VerifyVector(values()) && + verifier.EndTable(); + } + Int32VectorT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(Int32VectorT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const Int32VectorT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct Int32VectorBuilder { + typedef Int32Vector Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_values(::flatbuffers::Offset<::flatbuffers::Vector> values) { + fbb_.AddOffset(Int32Vector::VT_VALUES, values); + } + explicit Int32VectorBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateInt32Vector( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::Vector> values = 0) { + Int32VectorBuilder builder_(_fbb); + builder_.add_values(values); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateInt32VectorDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const std::vector *values = nullptr) { + auto values__ = values ? _fbb.CreateVector(*values) : 0; + return tflite::CreateInt32Vector( + _fbb, + values__); +} + +::flatbuffers::Offset CreateInt32Vector(::flatbuffers::FlatBufferBuilder &_fbb, const Int32VectorT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct Uint16VectorT : public ::flatbuffers::NativeTable { + typedef Uint16Vector TableType; + std::vector values{}; +}; + +struct Uint16Vector FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef Uint16VectorT NativeTableType; + typedef Uint16VectorBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_VALUES = 4 + }; + const ::flatbuffers::Vector *values() const { + return GetPointer *>(VT_VALUES); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_VALUES) && + verifier.VerifyVector(values()) && + verifier.EndTable(); + } + Uint16VectorT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(Uint16VectorT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const Uint16VectorT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct Uint16VectorBuilder { + typedef Uint16Vector Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_values(::flatbuffers::Offset<::flatbuffers::Vector> values) { + fbb_.AddOffset(Uint16Vector::VT_VALUES, values); + } + explicit Uint16VectorBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateUint16Vector( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::Vector> values = 0) { + Uint16VectorBuilder builder_(_fbb); + builder_.add_values(values); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateUint16VectorDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const std::vector *values = nullptr) { + if (values) { _fbb.ForceVectorAlignment(values->size(), sizeof(uint16_t), 4); } + auto values__ = values ? _fbb.CreateVector(*values) : 0; + return tflite::CreateUint16Vector( + _fbb, + values__); +} + +::flatbuffers::Offset CreateUint16Vector(::flatbuffers::FlatBufferBuilder &_fbb, const Uint16VectorT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct Uint8VectorT : public ::flatbuffers::NativeTable { + typedef Uint8Vector TableType; + std::vector values{}; +}; + +struct Uint8Vector FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef Uint8VectorT NativeTableType; + typedef Uint8VectorBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_VALUES = 4 + }; + const ::flatbuffers::Vector *values() const { + return GetPointer *>(VT_VALUES); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_VALUES) && + verifier.VerifyVector(values()) && + verifier.EndTable(); + } + Uint8VectorT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(Uint8VectorT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const Uint8VectorT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct Uint8VectorBuilder { + typedef Uint8Vector Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_values(::flatbuffers::Offset<::flatbuffers::Vector> values) { + fbb_.AddOffset(Uint8Vector::VT_VALUES, values); + } + explicit Uint8VectorBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateUint8Vector( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::Vector> values = 0) { + Uint8VectorBuilder builder_(_fbb); + builder_.add_values(values); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateUint8VectorDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const std::vector *values = nullptr) { + if (values) { _fbb.ForceVectorAlignment(values->size(), sizeof(uint8_t), 4); } + auto values__ = values ? _fbb.CreateVector(*values) : 0; + return tflite::CreateUint8Vector( + _fbb, + values__); +} + +::flatbuffers::Offset CreateUint8Vector(::flatbuffers::FlatBufferBuilder &_fbb, const Uint8VectorT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct DimensionMetadataT : public ::flatbuffers::NativeTable { + typedef DimensionMetadata TableType; + tflite::DimensionType format = tflite::DimensionType_DENSE; + int32_t dense_size = 0; + tflite::SparseIndexVectorUnion array_segments{}; + tflite::SparseIndexVectorUnion array_indices{}; +}; + +struct DimensionMetadata FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef DimensionMetadataT NativeTableType; + typedef DimensionMetadataBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_FORMAT = 4, + VT_DENSE_SIZE = 6, + VT_ARRAY_SEGMENTS_TYPE = 8, + VT_ARRAY_SEGMENTS = 10, + VT_ARRAY_INDICES_TYPE = 12, + VT_ARRAY_INDICES = 14 + }; + tflite::DimensionType format() const { + return static_cast(GetField(VT_FORMAT, 0)); + } + int32_t dense_size() const { + return GetField(VT_DENSE_SIZE, 0); + } + tflite::SparseIndexVector array_segments_type() const { + return static_cast(GetField(VT_ARRAY_SEGMENTS_TYPE, 0)); + } + const void *array_segments() const { + return GetPointer(VT_ARRAY_SEGMENTS); + } + template const T *array_segments_as() const; + const tflite::Int32Vector *array_segments_as_Int32Vector() const { + return array_segments_type() == tflite::SparseIndexVector_Int32Vector ? static_cast(array_segments()) : nullptr; + } + const tflite::Uint16Vector *array_segments_as_Uint16Vector() const { + return array_segments_type() == tflite::SparseIndexVector_Uint16Vector ? static_cast(array_segments()) : nullptr; + } + const tflite::Uint8Vector *array_segments_as_Uint8Vector() const { + return array_segments_type() == tflite::SparseIndexVector_Uint8Vector ? static_cast(array_segments()) : nullptr; + } + tflite::SparseIndexVector array_indices_type() const { + return static_cast(GetField(VT_ARRAY_INDICES_TYPE, 0)); + } + const void *array_indices() const { + return GetPointer(VT_ARRAY_INDICES); + } + template const T *array_indices_as() const; + const tflite::Int32Vector *array_indices_as_Int32Vector() const { + return array_indices_type() == tflite::SparseIndexVector_Int32Vector ? static_cast(array_indices()) : nullptr; + } + const tflite::Uint16Vector *array_indices_as_Uint16Vector() const { + return array_indices_type() == tflite::SparseIndexVector_Uint16Vector ? static_cast(array_indices()) : nullptr; + } + const tflite::Uint8Vector *array_indices_as_Uint8Vector() const { + return array_indices_type() == tflite::SparseIndexVector_Uint8Vector ? static_cast(array_indices()) : nullptr; + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_FORMAT, 1) && + VerifyField(verifier, VT_DENSE_SIZE, 4) && + VerifyField(verifier, VT_ARRAY_SEGMENTS_TYPE, 1) && + VerifyOffset(verifier, VT_ARRAY_SEGMENTS) && + VerifySparseIndexVector(verifier, array_segments(), array_segments_type()) && + VerifyField(verifier, VT_ARRAY_INDICES_TYPE, 1) && + VerifyOffset(verifier, VT_ARRAY_INDICES) && + VerifySparseIndexVector(verifier, array_indices(), array_indices_type()) && + verifier.EndTable(); + } + DimensionMetadataT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(DimensionMetadataT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const DimensionMetadataT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +template<> inline const tflite::Int32Vector *DimensionMetadata::array_segments_as() const { + return array_segments_as_Int32Vector(); +} + +template<> inline const tflite::Uint16Vector *DimensionMetadata::array_segments_as() const { + return array_segments_as_Uint16Vector(); +} + +template<> inline const tflite::Uint8Vector *DimensionMetadata::array_segments_as() const { + return array_segments_as_Uint8Vector(); +} + +template<> inline const tflite::Int32Vector *DimensionMetadata::array_indices_as() const { + return array_indices_as_Int32Vector(); +} + +template<> inline const tflite::Uint16Vector *DimensionMetadata::array_indices_as() const { + return array_indices_as_Uint16Vector(); +} + +template<> inline const tflite::Uint8Vector *DimensionMetadata::array_indices_as() const { + return array_indices_as_Uint8Vector(); +} + +struct DimensionMetadataBuilder { + typedef DimensionMetadata Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_format(tflite::DimensionType format) { + fbb_.AddElement(DimensionMetadata::VT_FORMAT, static_cast(format), 0); + } + void add_dense_size(int32_t dense_size) { + fbb_.AddElement(DimensionMetadata::VT_DENSE_SIZE, dense_size, 0); + } + void add_array_segments_type(tflite::SparseIndexVector array_segments_type) { + fbb_.AddElement(DimensionMetadata::VT_ARRAY_SEGMENTS_TYPE, static_cast(array_segments_type), 0); + } + void add_array_segments(::flatbuffers::Offset array_segments) { + fbb_.AddOffset(DimensionMetadata::VT_ARRAY_SEGMENTS, array_segments); + } + void add_array_indices_type(tflite::SparseIndexVector array_indices_type) { + fbb_.AddElement(DimensionMetadata::VT_ARRAY_INDICES_TYPE, static_cast(array_indices_type), 0); + } + void add_array_indices(::flatbuffers::Offset array_indices) { + fbb_.AddOffset(DimensionMetadata::VT_ARRAY_INDICES, array_indices); + } + explicit DimensionMetadataBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateDimensionMetadata( + ::flatbuffers::FlatBufferBuilder &_fbb, + tflite::DimensionType format = tflite::DimensionType_DENSE, + int32_t dense_size = 0, + tflite::SparseIndexVector array_segments_type = tflite::SparseIndexVector_NONE, + ::flatbuffers::Offset array_segments = 0, + tflite::SparseIndexVector array_indices_type = tflite::SparseIndexVector_NONE, + ::flatbuffers::Offset array_indices = 0) { + DimensionMetadataBuilder builder_(_fbb); + builder_.add_array_indices(array_indices); + builder_.add_array_segments(array_segments); + builder_.add_dense_size(dense_size); + builder_.add_array_indices_type(array_indices_type); + builder_.add_array_segments_type(array_segments_type); + builder_.add_format(format); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateDimensionMetadata(::flatbuffers::FlatBufferBuilder &_fbb, const DimensionMetadataT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct SparsityParametersT : public ::flatbuffers::NativeTable { + typedef SparsityParameters TableType; + std::vector traversal_order{}; + std::vector block_map{}; + std::vector> dim_metadata{}; + SparsityParametersT() = default; + SparsityParametersT(const SparsityParametersT &o); + SparsityParametersT(SparsityParametersT&&) FLATBUFFERS_NOEXCEPT = default; + SparsityParametersT &operator=(SparsityParametersT o) FLATBUFFERS_NOEXCEPT; +}; + +struct SparsityParameters FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef SparsityParametersT NativeTableType; + typedef SparsityParametersBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_TRAVERSAL_ORDER = 4, + VT_BLOCK_MAP = 6, + VT_DIM_METADATA = 8 + }; + const ::flatbuffers::Vector *traversal_order() const { + return GetPointer *>(VT_TRAVERSAL_ORDER); + } + const ::flatbuffers::Vector *block_map() const { + return GetPointer *>(VT_BLOCK_MAP); + } + const ::flatbuffers::Vector<::flatbuffers::Offset> *dim_metadata() const { + return GetPointer> *>(VT_DIM_METADATA); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_TRAVERSAL_ORDER) && + verifier.VerifyVector(traversal_order()) && + VerifyOffset(verifier, VT_BLOCK_MAP) && + verifier.VerifyVector(block_map()) && + VerifyOffset(verifier, VT_DIM_METADATA) && + verifier.VerifyVector(dim_metadata()) && + verifier.VerifyVectorOfTables(dim_metadata()) && + verifier.EndTable(); + } + SparsityParametersT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(SparsityParametersT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SparsityParametersT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct SparsityParametersBuilder { + typedef SparsityParameters Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_traversal_order(::flatbuffers::Offset<::flatbuffers::Vector> traversal_order) { + fbb_.AddOffset(SparsityParameters::VT_TRAVERSAL_ORDER, traversal_order); + } + void add_block_map(::flatbuffers::Offset<::flatbuffers::Vector> block_map) { + fbb_.AddOffset(SparsityParameters::VT_BLOCK_MAP, block_map); + } + void add_dim_metadata(::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset>> dim_metadata) { + fbb_.AddOffset(SparsityParameters::VT_DIM_METADATA, dim_metadata); + } + explicit SparsityParametersBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateSparsityParameters( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::Vector> traversal_order = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> block_map = 0, + ::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset>> dim_metadata = 0) { + SparsityParametersBuilder builder_(_fbb); + builder_.add_dim_metadata(dim_metadata); + builder_.add_block_map(block_map); + builder_.add_traversal_order(traversal_order); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateSparsityParametersDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const std::vector *traversal_order = nullptr, + const std::vector *block_map = nullptr, + const std::vector<::flatbuffers::Offset> *dim_metadata = nullptr) { + auto traversal_order__ = traversal_order ? _fbb.CreateVector(*traversal_order) : 0; + auto block_map__ = block_map ? _fbb.CreateVector(*block_map) : 0; + auto dim_metadata__ = dim_metadata ? _fbb.CreateVector<::flatbuffers::Offset>(*dim_metadata) : 0; + return tflite::CreateSparsityParameters( + _fbb, + traversal_order__, + block_map__, + dim_metadata__); +} + +::flatbuffers::Offset CreateSparsityParameters(::flatbuffers::FlatBufferBuilder &_fbb, const SparsityParametersT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct VariantSubTypeT : public ::flatbuffers::NativeTable { + typedef VariantSubType TableType; + std::vector shape{}; + tflite::TensorType type = tflite::TensorType_FLOAT32; + bool has_rank = false; +}; + +struct VariantSubType FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef VariantSubTypeT NativeTableType; + typedef VariantSubTypeBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_SHAPE = 4, + VT_TYPE = 6, + VT_HAS_RANK = 8 + }; + const ::flatbuffers::Vector *shape() const { + return GetPointer *>(VT_SHAPE); + } + tflite::TensorType type() const { + return static_cast(GetField(VT_TYPE, 0)); + } + bool has_rank() const { + return GetField(VT_HAS_RANK, 0) != 0; + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_SHAPE) && + verifier.VerifyVector(shape()) && + VerifyField(verifier, VT_TYPE, 1) && + VerifyField(verifier, VT_HAS_RANK, 1) && + verifier.EndTable(); + } + VariantSubTypeT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(VariantSubTypeT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const VariantSubTypeT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct VariantSubTypeBuilder { + typedef VariantSubType Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_shape(::flatbuffers::Offset<::flatbuffers::Vector> shape) { + fbb_.AddOffset(VariantSubType::VT_SHAPE, shape); + } + void add_type(tflite::TensorType type) { + fbb_.AddElement(VariantSubType::VT_TYPE, static_cast(type), 0); + } + void add_has_rank(bool has_rank) { + fbb_.AddElement(VariantSubType::VT_HAS_RANK, static_cast(has_rank), 0); + } + explicit VariantSubTypeBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateVariantSubType( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::Vector> shape = 0, + tflite::TensorType type = tflite::TensorType_FLOAT32, + bool has_rank = false) { + VariantSubTypeBuilder builder_(_fbb); + builder_.add_shape(shape); + builder_.add_has_rank(has_rank); + builder_.add_type(type); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateVariantSubTypeDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const std::vector *shape = nullptr, + tflite::TensorType type = tflite::TensorType_FLOAT32, + bool has_rank = false) { + auto shape__ = shape ? _fbb.CreateVector(*shape) : 0; + return tflite::CreateVariantSubType( + _fbb, + shape__, + type, + has_rank); +} + +::flatbuffers::Offset CreateVariantSubType(::flatbuffers::FlatBufferBuilder &_fbb, const VariantSubTypeT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct TensorT : public ::flatbuffers::NativeTable { + typedef Tensor TableType; + std::vector shape{}; + tflite::TensorType type = tflite::TensorType_FLOAT32; + uint32_t buffer = 0; + std::string name{}; + std::unique_ptr quantization{}; + bool is_variable = false; + std::unique_ptr sparsity{}; + std::vector shape_signature{}; + bool has_rank = false; + std::vector> variant_tensors{}; + uint32_t external_buffer = 0; + TensorT() = default; + TensorT(const TensorT &o); + TensorT(TensorT&&) FLATBUFFERS_NOEXCEPT = default; + TensorT &operator=(TensorT o) FLATBUFFERS_NOEXCEPT; +}; + +struct Tensor FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef TensorT NativeTableType; + typedef TensorBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_SHAPE = 4, + VT_TYPE = 6, + VT_BUFFER = 8, + VT_NAME = 10, + VT_QUANTIZATION = 12, + VT_IS_VARIABLE = 14, + VT_SPARSITY = 16, + VT_SHAPE_SIGNATURE = 18, + VT_HAS_RANK = 20, + VT_VARIANT_TENSORS = 22, + VT_EXTERNAL_BUFFER = 24 + }; + const ::flatbuffers::Vector *shape() const { + return GetPointer *>(VT_SHAPE); + } + tflite::TensorType type() const { + return static_cast(GetField(VT_TYPE, 0)); + } + uint32_t buffer() const { + return GetField(VT_BUFFER, 0); + } + const ::flatbuffers::String *name() const { + return GetPointer(VT_NAME); + } + const tflite::QuantizationParameters *quantization() const { + return GetPointer(VT_QUANTIZATION); + } + bool is_variable() const { + return GetField(VT_IS_VARIABLE, 0) != 0; + } + const tflite::SparsityParameters *sparsity() const { + return GetPointer(VT_SPARSITY); + } + const ::flatbuffers::Vector *shape_signature() const { + return GetPointer *>(VT_SHAPE_SIGNATURE); + } + bool has_rank() const { + return GetField(VT_HAS_RANK, 0) != 0; + } + const ::flatbuffers::Vector<::flatbuffers::Offset> *variant_tensors() const { + return GetPointer> *>(VT_VARIANT_TENSORS); + } + uint32_t external_buffer() const { + return GetField(VT_EXTERNAL_BUFFER, 0); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_SHAPE) && + verifier.VerifyVector(shape()) && + VerifyField(verifier, VT_TYPE, 1) && + VerifyField(verifier, VT_BUFFER, 4) && + VerifyOffset(verifier, VT_NAME) && + verifier.VerifyString(name()) && + VerifyOffset(verifier, VT_QUANTIZATION) && + verifier.VerifyTable(quantization()) && + VerifyField(verifier, VT_IS_VARIABLE, 1) && + VerifyOffset(verifier, VT_SPARSITY) && + verifier.VerifyTable(sparsity()) && + VerifyOffset(verifier, VT_SHAPE_SIGNATURE) && + verifier.VerifyVector(shape_signature()) && + VerifyField(verifier, VT_HAS_RANK, 1) && + VerifyOffset(verifier, VT_VARIANT_TENSORS) && + verifier.VerifyVector(variant_tensors()) && + verifier.VerifyVectorOfTables(variant_tensors()) && + VerifyField(verifier, VT_EXTERNAL_BUFFER, 4) && + verifier.EndTable(); + } + TensorT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(TensorT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const TensorT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct TensorBuilder { + typedef Tensor Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_shape(::flatbuffers::Offset<::flatbuffers::Vector> shape) { + fbb_.AddOffset(Tensor::VT_SHAPE, shape); + } + void add_type(tflite::TensorType type) { + fbb_.AddElement(Tensor::VT_TYPE, static_cast(type), 0); + } + void add_buffer(uint32_t buffer) { + fbb_.AddElement(Tensor::VT_BUFFER, buffer, 0); + } + void add_name(::flatbuffers::Offset<::flatbuffers::String> name) { + fbb_.AddOffset(Tensor::VT_NAME, name); + } + void add_quantization(::flatbuffers::Offset quantization) { + fbb_.AddOffset(Tensor::VT_QUANTIZATION, quantization); + } + void add_is_variable(bool is_variable) { + fbb_.AddElement(Tensor::VT_IS_VARIABLE, static_cast(is_variable), 0); + } + void add_sparsity(::flatbuffers::Offset sparsity) { + fbb_.AddOffset(Tensor::VT_SPARSITY, sparsity); + } + void add_shape_signature(::flatbuffers::Offset<::flatbuffers::Vector> shape_signature) { + fbb_.AddOffset(Tensor::VT_SHAPE_SIGNATURE, shape_signature); + } + void add_has_rank(bool has_rank) { + fbb_.AddElement(Tensor::VT_HAS_RANK, static_cast(has_rank), 0); + } + void add_variant_tensors(::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset>> variant_tensors) { + fbb_.AddOffset(Tensor::VT_VARIANT_TENSORS, variant_tensors); + } + void add_external_buffer(uint32_t external_buffer) { + fbb_.AddElement(Tensor::VT_EXTERNAL_BUFFER, external_buffer, 0); + } + explicit TensorBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateTensor( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::Vector> shape = 0, + tflite::TensorType type = tflite::TensorType_FLOAT32, + uint32_t buffer = 0, + ::flatbuffers::Offset<::flatbuffers::String> name = 0, + ::flatbuffers::Offset quantization = 0, + bool is_variable = false, + ::flatbuffers::Offset sparsity = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> shape_signature = 0, + bool has_rank = false, + ::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset>> variant_tensors = 0, + uint32_t external_buffer = 0) { + TensorBuilder builder_(_fbb); + builder_.add_external_buffer(external_buffer); + builder_.add_variant_tensors(variant_tensors); + builder_.add_shape_signature(shape_signature); + builder_.add_sparsity(sparsity); + builder_.add_quantization(quantization); + builder_.add_name(name); + builder_.add_buffer(buffer); + builder_.add_shape(shape); + builder_.add_has_rank(has_rank); + builder_.add_is_variable(is_variable); + builder_.add_type(type); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateTensorDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const std::vector *shape = nullptr, + tflite::TensorType type = tflite::TensorType_FLOAT32, + uint32_t buffer = 0, + const char *name = nullptr, + ::flatbuffers::Offset quantization = 0, + bool is_variable = false, + ::flatbuffers::Offset sparsity = 0, + const std::vector *shape_signature = nullptr, + bool has_rank = false, + const std::vector<::flatbuffers::Offset> *variant_tensors = nullptr, + uint32_t external_buffer = 0) { + auto shape__ = shape ? _fbb.CreateVector(*shape) : 0; + auto name__ = name ? _fbb.CreateString(name) : 0; + auto shape_signature__ = shape_signature ? _fbb.CreateVector(*shape_signature) : 0; + auto variant_tensors__ = variant_tensors ? _fbb.CreateVector<::flatbuffers::Offset>(*variant_tensors) : 0; + return tflite::CreateTensor( + _fbb, + shape__, + type, + buffer, + name__, + quantization, + is_variable, + sparsity, + shape_signature__, + has_rank, + variant_tensors__, + external_buffer); +} + +::flatbuffers::Offset CreateTensor(::flatbuffers::FlatBufferBuilder &_fbb, const TensorT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct StablehloGatherOptionsT : public ::flatbuffers::NativeTable { + typedef StablehloGatherOptions TableType; + std::vector offset_dims{}; + std::vector collapsed_slice_dims{}; + std::vector start_index_map{}; + int64_t index_vector_dim = 0; + std::vector slice_sizes{}; + bool indices_are_sorted = false; +}; + +struct StablehloGatherOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef StablehloGatherOptionsT NativeTableType; + typedef StablehloGatherOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_OFFSET_DIMS = 4, + VT_COLLAPSED_SLICE_DIMS = 6, + VT_START_INDEX_MAP = 8, + VT_INDEX_VECTOR_DIM = 10, + VT_SLICE_SIZES = 12, + VT_INDICES_ARE_SORTED = 14 + }; + const ::flatbuffers::Vector *offset_dims() const { + return GetPointer *>(VT_OFFSET_DIMS); + } + const ::flatbuffers::Vector *collapsed_slice_dims() const { + return GetPointer *>(VT_COLLAPSED_SLICE_DIMS); + } + const ::flatbuffers::Vector *start_index_map() const { + return GetPointer *>(VT_START_INDEX_MAP); + } + int64_t index_vector_dim() const { + return GetField(VT_INDEX_VECTOR_DIM, 0); + } + const ::flatbuffers::Vector *slice_sizes() const { + return GetPointer *>(VT_SLICE_SIZES); + } + bool indices_are_sorted() const { + return GetField(VT_INDICES_ARE_SORTED, 0) != 0; + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_OFFSET_DIMS) && + verifier.VerifyVector(offset_dims()) && + VerifyOffset(verifier, VT_COLLAPSED_SLICE_DIMS) && + verifier.VerifyVector(collapsed_slice_dims()) && + VerifyOffset(verifier, VT_START_INDEX_MAP) && + verifier.VerifyVector(start_index_map()) && + VerifyField(verifier, VT_INDEX_VECTOR_DIM, 8) && + VerifyOffset(verifier, VT_SLICE_SIZES) && + verifier.VerifyVector(slice_sizes()) && + VerifyField(verifier, VT_INDICES_ARE_SORTED, 1) && + verifier.EndTable(); + } + StablehloGatherOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(StablehloGatherOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloGatherOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct StablehloGatherOptionsBuilder { + typedef StablehloGatherOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_offset_dims(::flatbuffers::Offset<::flatbuffers::Vector> offset_dims) { + fbb_.AddOffset(StablehloGatherOptions::VT_OFFSET_DIMS, offset_dims); + } + void add_collapsed_slice_dims(::flatbuffers::Offset<::flatbuffers::Vector> collapsed_slice_dims) { + fbb_.AddOffset(StablehloGatherOptions::VT_COLLAPSED_SLICE_DIMS, collapsed_slice_dims); + } + void add_start_index_map(::flatbuffers::Offset<::flatbuffers::Vector> start_index_map) { + fbb_.AddOffset(StablehloGatherOptions::VT_START_INDEX_MAP, start_index_map); + } + void add_index_vector_dim(int64_t index_vector_dim) { + fbb_.AddElement(StablehloGatherOptions::VT_INDEX_VECTOR_DIM, index_vector_dim, 0); + } + void add_slice_sizes(::flatbuffers::Offset<::flatbuffers::Vector> slice_sizes) { + fbb_.AddOffset(StablehloGatherOptions::VT_SLICE_SIZES, slice_sizes); + } + void add_indices_are_sorted(bool indices_are_sorted) { + fbb_.AddElement(StablehloGatherOptions::VT_INDICES_ARE_SORTED, static_cast(indices_are_sorted), 0); + } + explicit StablehloGatherOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateStablehloGatherOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::Vector> offset_dims = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> collapsed_slice_dims = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> start_index_map = 0, + int64_t index_vector_dim = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> slice_sizes = 0, + bool indices_are_sorted = false) { + StablehloGatherOptionsBuilder builder_(_fbb); + builder_.add_index_vector_dim(index_vector_dim); + builder_.add_slice_sizes(slice_sizes); + builder_.add_start_index_map(start_index_map); + builder_.add_collapsed_slice_dims(collapsed_slice_dims); + builder_.add_offset_dims(offset_dims); + builder_.add_indices_are_sorted(indices_are_sorted); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateStablehloGatherOptionsDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const std::vector *offset_dims = nullptr, + const std::vector *collapsed_slice_dims = nullptr, + const std::vector *start_index_map = nullptr, + int64_t index_vector_dim = 0, + const std::vector *slice_sizes = nullptr, + bool indices_are_sorted = false) { + auto offset_dims__ = offset_dims ? _fbb.CreateVector(*offset_dims) : 0; + auto collapsed_slice_dims__ = collapsed_slice_dims ? _fbb.CreateVector(*collapsed_slice_dims) : 0; + auto start_index_map__ = start_index_map ? _fbb.CreateVector(*start_index_map) : 0; + auto slice_sizes__ = slice_sizes ? _fbb.CreateVector(*slice_sizes) : 0; + return tflite::CreateStablehloGatherOptions( + _fbb, + offset_dims__, + collapsed_slice_dims__, + start_index_map__, + index_vector_dim, + slice_sizes__, + indices_are_sorted); +} + +::flatbuffers::Offset CreateStablehloGatherOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloGatherOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct StablehloTransposeOptionsT : public ::flatbuffers::NativeTable { + typedef StablehloTransposeOptions TableType; + std::vector permutation{}; +}; + +struct StablehloTransposeOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef StablehloTransposeOptionsT NativeTableType; + typedef StablehloTransposeOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_PERMUTATION = 4 + }; + const ::flatbuffers::Vector *permutation() const { + return GetPointer *>(VT_PERMUTATION); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_PERMUTATION) && + verifier.VerifyVector(permutation()) && + verifier.EndTable(); + } + StablehloTransposeOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(StablehloTransposeOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloTransposeOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct StablehloTransposeOptionsBuilder { + typedef StablehloTransposeOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_permutation(::flatbuffers::Offset<::flatbuffers::Vector> permutation) { + fbb_.AddOffset(StablehloTransposeOptions::VT_PERMUTATION, permutation); + } + explicit StablehloTransposeOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateStablehloTransposeOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::Vector> permutation = 0) { + StablehloTransposeOptionsBuilder builder_(_fbb); + builder_.add_permutation(permutation); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateStablehloTransposeOptionsDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const std::vector *permutation = nullptr) { + auto permutation__ = permutation ? _fbb.CreateVector(*permutation) : 0; + return tflite::CreateStablehloTransposeOptions( + _fbb, + permutation__); +} + +::flatbuffers::Offset CreateStablehloTransposeOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloTransposeOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct StablehloDotGeneralOptionsT : public ::flatbuffers::NativeTable { + typedef StablehloDotGeneralOptions TableType; + std::vector lhs_batching_dimensions{}; + std::vector rhs_batching_dimensions{}; + std::vector lhs_contracting_dimensions{}; + std::vector rhs_contracting_dimensions{}; + std::vector precision_config{}; +}; + +struct StablehloDotGeneralOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef StablehloDotGeneralOptionsT NativeTableType; + typedef StablehloDotGeneralOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_LHS_BATCHING_DIMENSIONS = 4, + VT_RHS_BATCHING_DIMENSIONS = 6, + VT_LHS_CONTRACTING_DIMENSIONS = 8, + VT_RHS_CONTRACTING_DIMENSIONS = 10, + VT_PRECISION_CONFIG = 12 + }; + const ::flatbuffers::Vector *lhs_batching_dimensions() const { + return GetPointer *>(VT_LHS_BATCHING_DIMENSIONS); + } + const ::flatbuffers::Vector *rhs_batching_dimensions() const { + return GetPointer *>(VT_RHS_BATCHING_DIMENSIONS); + } + const ::flatbuffers::Vector *lhs_contracting_dimensions() const { + return GetPointer *>(VT_LHS_CONTRACTING_DIMENSIONS); + } + const ::flatbuffers::Vector *rhs_contracting_dimensions() const { + return GetPointer *>(VT_RHS_CONTRACTING_DIMENSIONS); + } + const ::flatbuffers::Vector *precision_config() const { + return GetPointer *>(VT_PRECISION_CONFIG); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_LHS_BATCHING_DIMENSIONS) && + verifier.VerifyVector(lhs_batching_dimensions()) && + VerifyOffset(verifier, VT_RHS_BATCHING_DIMENSIONS) && + verifier.VerifyVector(rhs_batching_dimensions()) && + VerifyOffset(verifier, VT_LHS_CONTRACTING_DIMENSIONS) && + verifier.VerifyVector(lhs_contracting_dimensions()) && + VerifyOffset(verifier, VT_RHS_CONTRACTING_DIMENSIONS) && + verifier.VerifyVector(rhs_contracting_dimensions()) && + VerifyOffset(verifier, VT_PRECISION_CONFIG) && + verifier.VerifyVector(precision_config()) && + verifier.EndTable(); + } + StablehloDotGeneralOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(StablehloDotGeneralOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloDotGeneralOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct StablehloDotGeneralOptionsBuilder { + typedef StablehloDotGeneralOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_lhs_batching_dimensions(::flatbuffers::Offset<::flatbuffers::Vector> lhs_batching_dimensions) { + fbb_.AddOffset(StablehloDotGeneralOptions::VT_LHS_BATCHING_DIMENSIONS, lhs_batching_dimensions); + } + void add_rhs_batching_dimensions(::flatbuffers::Offset<::flatbuffers::Vector> rhs_batching_dimensions) { + fbb_.AddOffset(StablehloDotGeneralOptions::VT_RHS_BATCHING_DIMENSIONS, rhs_batching_dimensions); + } + void add_lhs_contracting_dimensions(::flatbuffers::Offset<::flatbuffers::Vector> lhs_contracting_dimensions) { + fbb_.AddOffset(StablehloDotGeneralOptions::VT_LHS_CONTRACTING_DIMENSIONS, lhs_contracting_dimensions); + } + void add_rhs_contracting_dimensions(::flatbuffers::Offset<::flatbuffers::Vector> rhs_contracting_dimensions) { + fbb_.AddOffset(StablehloDotGeneralOptions::VT_RHS_CONTRACTING_DIMENSIONS, rhs_contracting_dimensions); + } + void add_precision_config(::flatbuffers::Offset<::flatbuffers::Vector> precision_config) { + fbb_.AddOffset(StablehloDotGeneralOptions::VT_PRECISION_CONFIG, precision_config); + } + explicit StablehloDotGeneralOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateStablehloDotGeneralOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::Vector> lhs_batching_dimensions = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> rhs_batching_dimensions = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> lhs_contracting_dimensions = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> rhs_contracting_dimensions = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> precision_config = 0) { + StablehloDotGeneralOptionsBuilder builder_(_fbb); + builder_.add_precision_config(precision_config); + builder_.add_rhs_contracting_dimensions(rhs_contracting_dimensions); + builder_.add_lhs_contracting_dimensions(lhs_contracting_dimensions); + builder_.add_rhs_batching_dimensions(rhs_batching_dimensions); + builder_.add_lhs_batching_dimensions(lhs_batching_dimensions); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateStablehloDotGeneralOptionsDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const std::vector *lhs_batching_dimensions = nullptr, + const std::vector *rhs_batching_dimensions = nullptr, + const std::vector *lhs_contracting_dimensions = nullptr, + const std::vector *rhs_contracting_dimensions = nullptr, + const std::vector *precision_config = nullptr) { + auto lhs_batching_dimensions__ = lhs_batching_dimensions ? _fbb.CreateVector(*lhs_batching_dimensions) : 0; + auto rhs_batching_dimensions__ = rhs_batching_dimensions ? _fbb.CreateVector(*rhs_batching_dimensions) : 0; + auto lhs_contracting_dimensions__ = lhs_contracting_dimensions ? _fbb.CreateVector(*lhs_contracting_dimensions) : 0; + auto rhs_contracting_dimensions__ = rhs_contracting_dimensions ? _fbb.CreateVector(*rhs_contracting_dimensions) : 0; + auto precision_config__ = precision_config ? _fbb.CreateVector(*precision_config) : 0; + return tflite::CreateStablehloDotGeneralOptions( + _fbb, + lhs_batching_dimensions__, + rhs_batching_dimensions__, + lhs_contracting_dimensions__, + rhs_contracting_dimensions__, + precision_config__); +} + +::flatbuffers::Offset CreateStablehloDotGeneralOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloDotGeneralOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct StablehloReduceWindowOptionsT : public ::flatbuffers::NativeTable { + typedef StablehloReduceWindowOptions TableType; + std::vector window_dimensions{}; + std::vector window_strides{}; + std::vector base_dilations{}; + std::vector window_dilations{}; + std::vector padding{}; + int32_t body_subgraph_index = 0; +}; + +struct StablehloReduceWindowOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef StablehloReduceWindowOptionsT NativeTableType; + typedef StablehloReduceWindowOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_WINDOW_DIMENSIONS = 4, + VT_WINDOW_STRIDES = 6, + VT_BASE_DILATIONS = 8, + VT_WINDOW_DILATIONS = 10, + VT_PADDING = 12, + VT_BODY_SUBGRAPH_INDEX = 14 + }; + const ::flatbuffers::Vector *window_dimensions() const { + return GetPointer *>(VT_WINDOW_DIMENSIONS); + } + const ::flatbuffers::Vector *window_strides() const { + return GetPointer *>(VT_WINDOW_STRIDES); + } + const ::flatbuffers::Vector *base_dilations() const { + return GetPointer *>(VT_BASE_DILATIONS); + } + const ::flatbuffers::Vector *window_dilations() const { + return GetPointer *>(VT_WINDOW_DILATIONS); + } + const ::flatbuffers::Vector *padding() const { + return GetPointer *>(VT_PADDING); + } + int32_t body_subgraph_index() const { + return GetField(VT_BODY_SUBGRAPH_INDEX, 0); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_WINDOW_DIMENSIONS) && + verifier.VerifyVector(window_dimensions()) && + VerifyOffset(verifier, VT_WINDOW_STRIDES) && + verifier.VerifyVector(window_strides()) && + VerifyOffset(verifier, VT_BASE_DILATIONS) && + verifier.VerifyVector(base_dilations()) && + VerifyOffset(verifier, VT_WINDOW_DILATIONS) && + verifier.VerifyVector(window_dilations()) && + VerifyOffset(verifier, VT_PADDING) && + verifier.VerifyVector(padding()) && + VerifyField(verifier, VT_BODY_SUBGRAPH_INDEX, 4) && + verifier.EndTable(); + } + StablehloReduceWindowOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(StablehloReduceWindowOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloReduceWindowOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct StablehloReduceWindowOptionsBuilder { + typedef StablehloReduceWindowOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_window_dimensions(::flatbuffers::Offset<::flatbuffers::Vector> window_dimensions) { + fbb_.AddOffset(StablehloReduceWindowOptions::VT_WINDOW_DIMENSIONS, window_dimensions); + } + void add_window_strides(::flatbuffers::Offset<::flatbuffers::Vector> window_strides) { + fbb_.AddOffset(StablehloReduceWindowOptions::VT_WINDOW_STRIDES, window_strides); + } + void add_base_dilations(::flatbuffers::Offset<::flatbuffers::Vector> base_dilations) { + fbb_.AddOffset(StablehloReduceWindowOptions::VT_BASE_DILATIONS, base_dilations); + } + void add_window_dilations(::flatbuffers::Offset<::flatbuffers::Vector> window_dilations) { + fbb_.AddOffset(StablehloReduceWindowOptions::VT_WINDOW_DILATIONS, window_dilations); + } + void add_padding(::flatbuffers::Offset<::flatbuffers::Vector> padding) { + fbb_.AddOffset(StablehloReduceWindowOptions::VT_PADDING, padding); + } + void add_body_subgraph_index(int32_t body_subgraph_index) { + fbb_.AddElement(StablehloReduceWindowOptions::VT_BODY_SUBGRAPH_INDEX, body_subgraph_index, 0); + } + explicit StablehloReduceWindowOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateStablehloReduceWindowOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::Vector> window_dimensions = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> window_strides = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> base_dilations = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> window_dilations = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> padding = 0, + int32_t body_subgraph_index = 0) { + StablehloReduceWindowOptionsBuilder builder_(_fbb); + builder_.add_body_subgraph_index(body_subgraph_index); + builder_.add_padding(padding); + builder_.add_window_dilations(window_dilations); + builder_.add_base_dilations(base_dilations); + builder_.add_window_strides(window_strides); + builder_.add_window_dimensions(window_dimensions); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateStablehloReduceWindowOptionsDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const std::vector *window_dimensions = nullptr, + const std::vector *window_strides = nullptr, + const std::vector *base_dilations = nullptr, + const std::vector *window_dilations = nullptr, + const std::vector *padding = nullptr, + int32_t body_subgraph_index = 0) { + auto window_dimensions__ = window_dimensions ? _fbb.CreateVector(*window_dimensions) : 0; + auto window_strides__ = window_strides ? _fbb.CreateVector(*window_strides) : 0; + auto base_dilations__ = base_dilations ? _fbb.CreateVector(*base_dilations) : 0; + auto window_dilations__ = window_dilations ? _fbb.CreateVector(*window_dilations) : 0; + auto padding__ = padding ? _fbb.CreateVector(*padding) : 0; + return tflite::CreateStablehloReduceWindowOptions( + _fbb, + window_dimensions__, + window_strides__, + base_dilations__, + window_dilations__, + padding__, + body_subgraph_index); +} + +::flatbuffers::Offset CreateStablehloReduceWindowOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloReduceWindowOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct StablehloWhileOptionsT : public ::flatbuffers::NativeTable { + typedef StablehloWhileOptions TableType; + int32_t cond_subgraph_index = 0; + int32_t body_subgraph_index = 0; +}; + +struct StablehloWhileOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef StablehloWhileOptionsT NativeTableType; + typedef StablehloWhileOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_COND_SUBGRAPH_INDEX = 4, + VT_BODY_SUBGRAPH_INDEX = 6 + }; + int32_t cond_subgraph_index() const { + return GetField(VT_COND_SUBGRAPH_INDEX, 0); + } + int32_t body_subgraph_index() const { + return GetField(VT_BODY_SUBGRAPH_INDEX, 0); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_COND_SUBGRAPH_INDEX, 4) && + VerifyField(verifier, VT_BODY_SUBGRAPH_INDEX, 4) && + verifier.EndTable(); + } + StablehloWhileOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(StablehloWhileOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloWhileOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct StablehloWhileOptionsBuilder { + typedef StablehloWhileOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_cond_subgraph_index(int32_t cond_subgraph_index) { + fbb_.AddElement(StablehloWhileOptions::VT_COND_SUBGRAPH_INDEX, cond_subgraph_index, 0); + } + void add_body_subgraph_index(int32_t body_subgraph_index) { + fbb_.AddElement(StablehloWhileOptions::VT_BODY_SUBGRAPH_INDEX, body_subgraph_index, 0); + } + explicit StablehloWhileOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateStablehloWhileOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + int32_t cond_subgraph_index = 0, + int32_t body_subgraph_index = 0) { + StablehloWhileOptionsBuilder builder_(_fbb); + builder_.add_body_subgraph_index(body_subgraph_index); + builder_.add_cond_subgraph_index(cond_subgraph_index); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateStablehloWhileOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloWhileOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct StablehloSortOptionsT : public ::flatbuffers::NativeTable { + typedef StablehloSortOptions TableType; + int64_t dimension = 0; + bool is_stable = false; + int32_t comparator_subgraph_index = 0; +}; + +struct StablehloSortOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef StablehloSortOptionsT NativeTableType; + typedef StablehloSortOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_DIMENSION = 4, + VT_IS_STABLE = 6, + VT_COMPARATOR_SUBGRAPH_INDEX = 8 + }; + int64_t dimension() const { + return GetField(VT_DIMENSION, 0); + } + bool is_stable() const { + return GetField(VT_IS_STABLE, 0) != 0; + } + int32_t comparator_subgraph_index() const { + return GetField(VT_COMPARATOR_SUBGRAPH_INDEX, 0); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_DIMENSION, 8) && + VerifyField(verifier, VT_IS_STABLE, 1) && + VerifyField(verifier, VT_COMPARATOR_SUBGRAPH_INDEX, 4) && + verifier.EndTable(); + } + StablehloSortOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(StablehloSortOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloSortOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct StablehloSortOptionsBuilder { + typedef StablehloSortOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_dimension(int64_t dimension) { + fbb_.AddElement(StablehloSortOptions::VT_DIMENSION, dimension, 0); + } + void add_is_stable(bool is_stable) { + fbb_.AddElement(StablehloSortOptions::VT_IS_STABLE, static_cast(is_stable), 0); + } + void add_comparator_subgraph_index(int32_t comparator_subgraph_index) { + fbb_.AddElement(StablehloSortOptions::VT_COMPARATOR_SUBGRAPH_INDEX, comparator_subgraph_index, 0); + } + explicit StablehloSortOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateStablehloSortOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + int64_t dimension = 0, + bool is_stable = false, + int32_t comparator_subgraph_index = 0) { + StablehloSortOptionsBuilder builder_(_fbb); + builder_.add_dimension(dimension); + builder_.add_comparator_subgraph_index(comparator_subgraph_index); + builder_.add_is_stable(is_stable); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateStablehloSortOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloSortOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct StablehloConcatenateOptionsT : public ::flatbuffers::NativeTable { + typedef StablehloConcatenateOptions TableType; + int64_t dimension = 0; +}; + +struct StablehloConcatenateOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef StablehloConcatenateOptionsT NativeTableType; + typedef StablehloConcatenateOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_DIMENSION = 4 + }; + int64_t dimension() const { + return GetField(VT_DIMENSION, 0); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_DIMENSION, 8) && + verifier.EndTable(); + } + StablehloConcatenateOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(StablehloConcatenateOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloConcatenateOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct StablehloConcatenateOptionsBuilder { + typedef StablehloConcatenateOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_dimension(int64_t dimension) { + fbb_.AddElement(StablehloConcatenateOptions::VT_DIMENSION, dimension, 0); + } + explicit StablehloConcatenateOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateStablehloConcatenateOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + int64_t dimension = 0) { + StablehloConcatenateOptionsBuilder builder_(_fbb); + builder_.add_dimension(dimension); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateStablehloConcatenateOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloConcatenateOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct StablehloBroadcastInDimOptionsT : public ::flatbuffers::NativeTable { + typedef StablehloBroadcastInDimOptions TableType; + std::vector broadcast_dimensions{}; +}; + +struct StablehloBroadcastInDimOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef StablehloBroadcastInDimOptionsT NativeTableType; + typedef StablehloBroadcastInDimOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_BROADCAST_DIMENSIONS = 4 + }; + const ::flatbuffers::Vector *broadcast_dimensions() const { + return GetPointer *>(VT_BROADCAST_DIMENSIONS); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_BROADCAST_DIMENSIONS) && + verifier.VerifyVector(broadcast_dimensions()) && + verifier.EndTable(); + } + StablehloBroadcastInDimOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(StablehloBroadcastInDimOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloBroadcastInDimOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct StablehloBroadcastInDimOptionsBuilder { + typedef StablehloBroadcastInDimOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_broadcast_dimensions(::flatbuffers::Offset<::flatbuffers::Vector> broadcast_dimensions) { + fbb_.AddOffset(StablehloBroadcastInDimOptions::VT_BROADCAST_DIMENSIONS, broadcast_dimensions); + } + explicit StablehloBroadcastInDimOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateStablehloBroadcastInDimOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::Vector> broadcast_dimensions = 0) { + StablehloBroadcastInDimOptionsBuilder builder_(_fbb); + builder_.add_broadcast_dimensions(broadcast_dimensions); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateStablehloBroadcastInDimOptionsDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const std::vector *broadcast_dimensions = nullptr) { + auto broadcast_dimensions__ = broadcast_dimensions ? _fbb.CreateVector(*broadcast_dimensions) : 0; + return tflite::CreateStablehloBroadcastInDimOptions( + _fbb, + broadcast_dimensions__); +} + +::flatbuffers::Offset CreateStablehloBroadcastInDimOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloBroadcastInDimOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct StablehloCompareOptionsT : public ::flatbuffers::NativeTable { + typedef StablehloCompareOptions TableType; + tflite::StablehloComparisonDirection comparison_direction = tflite::StablehloComparisonDirection_STABLEHLO_COMPARISON_DIRECTION_EQ; + tflite::StablehloComparisonType compare_type = tflite::StablehloComparisonType_STABLEHLO_COMPARISON_TYPE_NOTYPE; +}; + +struct StablehloCompareOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef StablehloCompareOptionsT NativeTableType; + typedef StablehloCompareOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_COMPARISON_DIRECTION = 4, + VT_COMPARE_TYPE = 6 + }; + tflite::StablehloComparisonDirection comparison_direction() const { + return static_cast(GetField(VT_COMPARISON_DIRECTION, 0)); + } + tflite::StablehloComparisonType compare_type() const { + return static_cast(GetField(VT_COMPARE_TYPE, 0)); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_COMPARISON_DIRECTION, 4) && + VerifyField(verifier, VT_COMPARE_TYPE, 4) && + verifier.EndTable(); + } + StablehloCompareOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(StablehloCompareOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloCompareOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct StablehloCompareOptionsBuilder { + typedef StablehloCompareOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_comparison_direction(tflite::StablehloComparisonDirection comparison_direction) { + fbb_.AddElement(StablehloCompareOptions::VT_COMPARISON_DIRECTION, static_cast(comparison_direction), 0); + } + void add_compare_type(tflite::StablehloComparisonType compare_type) { + fbb_.AddElement(StablehloCompareOptions::VT_COMPARE_TYPE, static_cast(compare_type), 0); + } + explicit StablehloCompareOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateStablehloCompareOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + tflite::StablehloComparisonDirection comparison_direction = tflite::StablehloComparisonDirection_STABLEHLO_COMPARISON_DIRECTION_EQ, + tflite::StablehloComparisonType compare_type = tflite::StablehloComparisonType_STABLEHLO_COMPARISON_TYPE_NOTYPE) { + StablehloCompareOptionsBuilder builder_(_fbb); + builder_.add_compare_type(compare_type); + builder_.add_comparison_direction(comparison_direction); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateStablehloCompareOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloCompareOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct StablehloDynamicSliceOptionsT : public ::flatbuffers::NativeTable { + typedef StablehloDynamicSliceOptions TableType; + std::vector slice_sizes{}; +}; + +struct StablehloDynamicSliceOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef StablehloDynamicSliceOptionsT NativeTableType; + typedef StablehloDynamicSliceOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_SLICE_SIZES = 4 + }; + const ::flatbuffers::Vector *slice_sizes() const { + return GetPointer *>(VT_SLICE_SIZES); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_SLICE_SIZES) && + verifier.VerifyVector(slice_sizes()) && + verifier.EndTable(); + } + StablehloDynamicSliceOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(StablehloDynamicSliceOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloDynamicSliceOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct StablehloDynamicSliceOptionsBuilder { + typedef StablehloDynamicSliceOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_slice_sizes(::flatbuffers::Offset<::flatbuffers::Vector> slice_sizes) { + fbb_.AddOffset(StablehloDynamicSliceOptions::VT_SLICE_SIZES, slice_sizes); + } + explicit StablehloDynamicSliceOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateStablehloDynamicSliceOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::Vector> slice_sizes = 0) { + StablehloDynamicSliceOptionsBuilder builder_(_fbb); + builder_.add_slice_sizes(slice_sizes); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateStablehloDynamicSliceOptionsDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const std::vector *slice_sizes = nullptr) { + auto slice_sizes__ = slice_sizes ? _fbb.CreateVector(*slice_sizes) : 0; + return tflite::CreateStablehloDynamicSliceOptions( + _fbb, + slice_sizes__); +} + +::flatbuffers::Offset CreateStablehloDynamicSliceOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloDynamicSliceOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct StablehloPadOptionsT : public ::flatbuffers::NativeTable { + typedef StablehloPadOptions TableType; + std::vector edge_padding_low{}; + std::vector edge_padding_high{}; + std::vector interior_padding{}; +}; + +struct StablehloPadOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef StablehloPadOptionsT NativeTableType; + typedef StablehloPadOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_EDGE_PADDING_LOW = 4, + VT_EDGE_PADDING_HIGH = 6, + VT_INTERIOR_PADDING = 8 + }; + const ::flatbuffers::Vector *edge_padding_low() const { + return GetPointer *>(VT_EDGE_PADDING_LOW); + } + const ::flatbuffers::Vector *edge_padding_high() const { + return GetPointer *>(VT_EDGE_PADDING_HIGH); + } + const ::flatbuffers::Vector *interior_padding() const { + return GetPointer *>(VT_INTERIOR_PADDING); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_EDGE_PADDING_LOW) && + verifier.VerifyVector(edge_padding_low()) && + VerifyOffset(verifier, VT_EDGE_PADDING_HIGH) && + verifier.VerifyVector(edge_padding_high()) && + VerifyOffset(verifier, VT_INTERIOR_PADDING) && + verifier.VerifyVector(interior_padding()) && + verifier.EndTable(); + } + StablehloPadOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(StablehloPadOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloPadOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct StablehloPadOptionsBuilder { + typedef StablehloPadOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_edge_padding_low(::flatbuffers::Offset<::flatbuffers::Vector> edge_padding_low) { + fbb_.AddOffset(StablehloPadOptions::VT_EDGE_PADDING_LOW, edge_padding_low); + } + void add_edge_padding_high(::flatbuffers::Offset<::flatbuffers::Vector> edge_padding_high) { + fbb_.AddOffset(StablehloPadOptions::VT_EDGE_PADDING_HIGH, edge_padding_high); + } + void add_interior_padding(::flatbuffers::Offset<::flatbuffers::Vector> interior_padding) { + fbb_.AddOffset(StablehloPadOptions::VT_INTERIOR_PADDING, interior_padding); + } + explicit StablehloPadOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateStablehloPadOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::Vector> edge_padding_low = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> edge_padding_high = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> interior_padding = 0) { + StablehloPadOptionsBuilder builder_(_fbb); + builder_.add_interior_padding(interior_padding); + builder_.add_edge_padding_high(edge_padding_high); + builder_.add_edge_padding_low(edge_padding_low); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateStablehloPadOptionsDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const std::vector *edge_padding_low = nullptr, + const std::vector *edge_padding_high = nullptr, + const std::vector *interior_padding = nullptr) { + auto edge_padding_low__ = edge_padding_low ? _fbb.CreateVector(*edge_padding_low) : 0; + auto edge_padding_high__ = edge_padding_high ? _fbb.CreateVector(*edge_padding_high) : 0; + auto interior_padding__ = interior_padding ? _fbb.CreateVector(*interior_padding) : 0; + return tflite::CreateStablehloPadOptions( + _fbb, + edge_padding_low__, + edge_padding_high__, + interior_padding__); +} + +::flatbuffers::Offset CreateStablehloPadOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloPadOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct StablehloIotaOptionsT : public ::flatbuffers::NativeTable { + typedef StablehloIotaOptions TableType; + int64_t iota_dimension = 0; +}; + +struct StablehloIotaOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef StablehloIotaOptionsT NativeTableType; + typedef StablehloIotaOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_IOTA_DIMENSION = 4 + }; + int64_t iota_dimension() const { + return GetField(VT_IOTA_DIMENSION, 0); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_IOTA_DIMENSION, 8) && + verifier.EndTable(); + } + StablehloIotaOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(StablehloIotaOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloIotaOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct StablehloIotaOptionsBuilder { + typedef StablehloIotaOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_iota_dimension(int64_t iota_dimension) { + fbb_.AddElement(StablehloIotaOptions::VT_IOTA_DIMENSION, iota_dimension, 0); + } + explicit StablehloIotaOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateStablehloIotaOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + int64_t iota_dimension = 0) { + StablehloIotaOptionsBuilder builder_(_fbb); + builder_.add_iota_dimension(iota_dimension); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateStablehloIotaOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloIotaOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct StablehloCustomCallOptionsT : public ::flatbuffers::NativeTable { + typedef StablehloCustomCallOptions TableType; + std::string call_target_name{}; + bool has_side_effect = false; + std::string backend_config{}; + int32_t api_version = 0; + std::vector called_computations{}; + std::vector custom_attributes{}; +}; + +struct StablehloCustomCallOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef StablehloCustomCallOptionsT NativeTableType; + typedef StablehloCustomCallOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_CALL_TARGET_NAME = 4, + VT_HAS_SIDE_EFFECT = 6, + VT_BACKEND_CONFIG = 8, + VT_API_VERSION = 10, + VT_CALLED_COMPUTATIONS = 12, + VT_CUSTOM_ATTRIBUTES = 14 + }; + const ::flatbuffers::String *call_target_name() const { + return GetPointer(VT_CALL_TARGET_NAME); + } + bool has_side_effect() const { + return GetField(VT_HAS_SIDE_EFFECT, 0) != 0; + } + const ::flatbuffers::String *backend_config() const { + return GetPointer(VT_BACKEND_CONFIG); + } + int32_t api_version() const { + return GetField(VT_API_VERSION, 0); + } + const ::flatbuffers::Vector *called_computations() const { + return GetPointer *>(VT_CALLED_COMPUTATIONS); + } + const ::flatbuffers::Vector *custom_attributes() const { + return GetPointer *>(VT_CUSTOM_ATTRIBUTES); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_CALL_TARGET_NAME) && + verifier.VerifyString(call_target_name()) && + VerifyField(verifier, VT_HAS_SIDE_EFFECT, 1) && + VerifyOffset(verifier, VT_BACKEND_CONFIG) && + verifier.VerifyString(backend_config()) && + VerifyField(verifier, VT_API_VERSION, 4) && + VerifyOffset(verifier, VT_CALLED_COMPUTATIONS) && + verifier.VerifyVector(called_computations()) && + VerifyOffset(verifier, VT_CUSTOM_ATTRIBUTES) && + verifier.VerifyVector(custom_attributes()) && + verifier.EndTable(); + } + StablehloCustomCallOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(StablehloCustomCallOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloCustomCallOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct StablehloCustomCallOptionsBuilder { + typedef StablehloCustomCallOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_call_target_name(::flatbuffers::Offset<::flatbuffers::String> call_target_name) { + fbb_.AddOffset(StablehloCustomCallOptions::VT_CALL_TARGET_NAME, call_target_name); + } + void add_has_side_effect(bool has_side_effect) { + fbb_.AddElement(StablehloCustomCallOptions::VT_HAS_SIDE_EFFECT, static_cast(has_side_effect), 0); + } + void add_backend_config(::flatbuffers::Offset<::flatbuffers::String> backend_config) { + fbb_.AddOffset(StablehloCustomCallOptions::VT_BACKEND_CONFIG, backend_config); + } + void add_api_version(int32_t api_version) { + fbb_.AddElement(StablehloCustomCallOptions::VT_API_VERSION, api_version, 0); + } + void add_called_computations(::flatbuffers::Offset<::flatbuffers::Vector> called_computations) { + fbb_.AddOffset(StablehloCustomCallOptions::VT_CALLED_COMPUTATIONS, called_computations); + } + void add_custom_attributes(::flatbuffers::Offset<::flatbuffers::Vector> custom_attributes) { + fbb_.AddOffset(StablehloCustomCallOptions::VT_CUSTOM_ATTRIBUTES, custom_attributes); + } + explicit StablehloCustomCallOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateStablehloCustomCallOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::String> call_target_name = 0, + bool has_side_effect = false, + ::flatbuffers::Offset<::flatbuffers::String> backend_config = 0, + int32_t api_version = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> called_computations = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> custom_attributes = 0) { + StablehloCustomCallOptionsBuilder builder_(_fbb); + builder_.add_custom_attributes(custom_attributes); + builder_.add_called_computations(called_computations); + builder_.add_api_version(api_version); + builder_.add_backend_config(backend_config); + builder_.add_call_target_name(call_target_name); + builder_.add_has_side_effect(has_side_effect); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateStablehloCustomCallOptionsDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const char *call_target_name = nullptr, + bool has_side_effect = false, + const char *backend_config = nullptr, + int32_t api_version = 0, + const std::vector *called_computations = nullptr, + const std::vector *custom_attributes = nullptr) { + auto call_target_name__ = call_target_name ? _fbb.CreateString(call_target_name) : 0; + auto backend_config__ = backend_config ? _fbb.CreateString(backend_config) : 0; + auto called_computations__ = called_computations ? _fbb.CreateVector(*called_computations) : 0; + auto custom_attributes__ = custom_attributes ? _fbb.CreateVector(*custom_attributes) : 0; + return tflite::CreateStablehloCustomCallOptions( + _fbb, + call_target_name__, + has_side_effect, + backend_config__, + api_version, + called_computations__, + custom_attributes__); +} + +::flatbuffers::Offset CreateStablehloCustomCallOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloCustomCallOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct StablehloReduceOptionsT : public ::flatbuffers::NativeTable { + typedef StablehloReduceOptions TableType; + std::vector dimensions{}; + int32_t body_subgraph_index = 0; +}; + +struct StablehloReduceOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef StablehloReduceOptionsT NativeTableType; + typedef StablehloReduceOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_DIMENSIONS = 4, + VT_BODY_SUBGRAPH_INDEX = 6 + }; + const ::flatbuffers::Vector *dimensions() const { + return GetPointer *>(VT_DIMENSIONS); + } + int32_t body_subgraph_index() const { + return GetField(VT_BODY_SUBGRAPH_INDEX, 0); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_DIMENSIONS) && + verifier.VerifyVector(dimensions()) && + VerifyField(verifier, VT_BODY_SUBGRAPH_INDEX, 4) && + verifier.EndTable(); + } + StablehloReduceOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(StablehloReduceOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloReduceOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct StablehloReduceOptionsBuilder { + typedef StablehloReduceOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_dimensions(::flatbuffers::Offset<::flatbuffers::Vector> dimensions) { + fbb_.AddOffset(StablehloReduceOptions::VT_DIMENSIONS, dimensions); + } + void add_body_subgraph_index(int32_t body_subgraph_index) { + fbb_.AddElement(StablehloReduceOptions::VT_BODY_SUBGRAPH_INDEX, body_subgraph_index, 0); + } + explicit StablehloReduceOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateStablehloReduceOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::Vector> dimensions = 0, + int32_t body_subgraph_index = 0) { + StablehloReduceOptionsBuilder builder_(_fbb); + builder_.add_body_subgraph_index(body_subgraph_index); + builder_.add_dimensions(dimensions); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateStablehloReduceOptionsDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const std::vector *dimensions = nullptr, + int32_t body_subgraph_index = 0) { + auto dimensions__ = dimensions ? _fbb.CreateVector(*dimensions) : 0; + return tflite::CreateStablehloReduceOptions( + _fbb, + dimensions__, + body_subgraph_index); +} + +::flatbuffers::Offset CreateStablehloReduceOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloReduceOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct StablehloSliceOptionsT : public ::flatbuffers::NativeTable { + typedef StablehloSliceOptions TableType; + std::vector start_indices{}; + std::vector limit_indices{}; + std::vector strides{}; +}; + +struct StablehloSliceOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef StablehloSliceOptionsT NativeTableType; + typedef StablehloSliceOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_START_INDICES = 4, + VT_LIMIT_INDICES = 6, + VT_STRIDES = 8 + }; + const ::flatbuffers::Vector *start_indices() const { + return GetPointer *>(VT_START_INDICES); + } + const ::flatbuffers::Vector *limit_indices() const { + return GetPointer *>(VT_LIMIT_INDICES); + } + const ::flatbuffers::Vector *strides() const { + return GetPointer *>(VT_STRIDES); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_START_INDICES) && + verifier.VerifyVector(start_indices()) && + VerifyOffset(verifier, VT_LIMIT_INDICES) && + verifier.VerifyVector(limit_indices()) && + VerifyOffset(verifier, VT_STRIDES) && + verifier.VerifyVector(strides()) && + verifier.EndTable(); + } + StablehloSliceOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(StablehloSliceOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloSliceOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct StablehloSliceOptionsBuilder { + typedef StablehloSliceOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_start_indices(::flatbuffers::Offset<::flatbuffers::Vector> start_indices) { + fbb_.AddOffset(StablehloSliceOptions::VT_START_INDICES, start_indices); + } + void add_limit_indices(::flatbuffers::Offset<::flatbuffers::Vector> limit_indices) { + fbb_.AddOffset(StablehloSliceOptions::VT_LIMIT_INDICES, limit_indices); + } + void add_strides(::flatbuffers::Offset<::flatbuffers::Vector> strides) { + fbb_.AddOffset(StablehloSliceOptions::VT_STRIDES, strides); + } + explicit StablehloSliceOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateStablehloSliceOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::Vector> start_indices = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> limit_indices = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> strides = 0) { + StablehloSliceOptionsBuilder builder_(_fbb); + builder_.add_strides(strides); + builder_.add_limit_indices(limit_indices); + builder_.add_start_indices(start_indices); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateStablehloSliceOptionsDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const std::vector *start_indices = nullptr, + const std::vector *limit_indices = nullptr, + const std::vector *strides = nullptr) { + auto start_indices__ = start_indices ? _fbb.CreateVector(*start_indices) : 0; + auto limit_indices__ = limit_indices ? _fbb.CreateVector(*limit_indices) : 0; + auto strides__ = strides ? _fbb.CreateVector(*strides) : 0; + return tflite::CreateStablehloSliceOptions( + _fbb, + start_indices__, + limit_indices__, + strides__); +} + +::flatbuffers::Offset CreateStablehloSliceOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloSliceOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct StablehloConvolutionOptionsT : public ::flatbuffers::NativeTable { + typedef StablehloConvolutionOptions TableType; + std::vector window_strides{}; + std::vector padding{}; + std::vector lhs_dilation{}; + std::vector rhs_dilation{}; + std::vector window_reversal{}; + int64_t input_batch_dimension = 0; + int64_t input_feature_dimension = 0; + std::vector input_spatial_dimensions{}; + int64_t kernel_input_feature_dimension = 0; + int64_t kernel_output_feature_dimension = 0; + std::vector kernel_spatial_dimensions{}; + int64_t output_batch_dimension = 0; + int64_t output_feature_dimension = 0; + std::vector output_spatial_dimensions{}; + int64_t feature_group_count = 0; + int64_t batch_group_count = 0; + std::vector precision_config{}; +}; + +struct StablehloConvolutionOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef StablehloConvolutionOptionsT NativeTableType; + typedef StablehloConvolutionOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_WINDOW_STRIDES = 4, + VT_PADDING = 6, + VT_LHS_DILATION = 8, + VT_RHS_DILATION = 10, + VT_WINDOW_REVERSAL = 12, + VT_INPUT_BATCH_DIMENSION = 14, + VT_INPUT_FEATURE_DIMENSION = 16, + VT_INPUT_SPATIAL_DIMENSIONS = 18, + VT_KERNEL_INPUT_FEATURE_DIMENSION = 20, + VT_KERNEL_OUTPUT_FEATURE_DIMENSION = 22, + VT_KERNEL_SPATIAL_DIMENSIONS = 24, + VT_OUTPUT_BATCH_DIMENSION = 26, + VT_OUTPUT_FEATURE_DIMENSION = 28, + VT_OUTPUT_SPATIAL_DIMENSIONS = 30, + VT_FEATURE_GROUP_COUNT = 32, + VT_BATCH_GROUP_COUNT = 34, + VT_PRECISION_CONFIG = 36 + }; + const ::flatbuffers::Vector *window_strides() const { + return GetPointer *>(VT_WINDOW_STRIDES); + } + const ::flatbuffers::Vector *padding() const { + return GetPointer *>(VT_PADDING); + } + const ::flatbuffers::Vector *lhs_dilation() const { + return GetPointer *>(VT_LHS_DILATION); + } + const ::flatbuffers::Vector *rhs_dilation() const { + return GetPointer *>(VT_RHS_DILATION); + } + const ::flatbuffers::Vector *window_reversal() const { + return GetPointer *>(VT_WINDOW_REVERSAL); + } + int64_t input_batch_dimension() const { + return GetField(VT_INPUT_BATCH_DIMENSION, 0); + } + int64_t input_feature_dimension() const { + return GetField(VT_INPUT_FEATURE_DIMENSION, 0); + } + const ::flatbuffers::Vector *input_spatial_dimensions() const { + return GetPointer *>(VT_INPUT_SPATIAL_DIMENSIONS); + } + int64_t kernel_input_feature_dimension() const { + return GetField(VT_KERNEL_INPUT_FEATURE_DIMENSION, 0); + } + int64_t kernel_output_feature_dimension() const { + return GetField(VT_KERNEL_OUTPUT_FEATURE_DIMENSION, 0); + } + const ::flatbuffers::Vector *kernel_spatial_dimensions() const { + return GetPointer *>(VT_KERNEL_SPATIAL_DIMENSIONS); + } + int64_t output_batch_dimension() const { + return GetField(VT_OUTPUT_BATCH_DIMENSION, 0); + } + int64_t output_feature_dimension() const { + return GetField(VT_OUTPUT_FEATURE_DIMENSION, 0); + } + const ::flatbuffers::Vector *output_spatial_dimensions() const { + return GetPointer *>(VT_OUTPUT_SPATIAL_DIMENSIONS); + } + int64_t feature_group_count() const { + return GetField(VT_FEATURE_GROUP_COUNT, 0); + } + int64_t batch_group_count() const { + return GetField(VT_BATCH_GROUP_COUNT, 0); + } + const ::flatbuffers::Vector *precision_config() const { + return GetPointer *>(VT_PRECISION_CONFIG); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_WINDOW_STRIDES) && + verifier.VerifyVector(window_strides()) && + VerifyOffset(verifier, VT_PADDING) && + verifier.VerifyVector(padding()) && + VerifyOffset(verifier, VT_LHS_DILATION) && + verifier.VerifyVector(lhs_dilation()) && + VerifyOffset(verifier, VT_RHS_DILATION) && + verifier.VerifyVector(rhs_dilation()) && + VerifyOffset(verifier, VT_WINDOW_REVERSAL) && + verifier.VerifyVector(window_reversal()) && + VerifyField(verifier, VT_INPUT_BATCH_DIMENSION, 8) && + VerifyField(verifier, VT_INPUT_FEATURE_DIMENSION, 8) && + VerifyOffset(verifier, VT_INPUT_SPATIAL_DIMENSIONS) && + verifier.VerifyVector(input_spatial_dimensions()) && + VerifyField(verifier, VT_KERNEL_INPUT_FEATURE_DIMENSION, 8) && + VerifyField(verifier, VT_KERNEL_OUTPUT_FEATURE_DIMENSION, 8) && + VerifyOffset(verifier, VT_KERNEL_SPATIAL_DIMENSIONS) && + verifier.VerifyVector(kernel_spatial_dimensions()) && + VerifyField(verifier, VT_OUTPUT_BATCH_DIMENSION, 8) && + VerifyField(verifier, VT_OUTPUT_FEATURE_DIMENSION, 8) && + VerifyOffset(verifier, VT_OUTPUT_SPATIAL_DIMENSIONS) && + verifier.VerifyVector(output_spatial_dimensions()) && + VerifyField(verifier, VT_FEATURE_GROUP_COUNT, 8) && + VerifyField(verifier, VT_BATCH_GROUP_COUNT, 8) && + VerifyOffset(verifier, VT_PRECISION_CONFIG) && + verifier.VerifyVector(precision_config()) && + verifier.EndTable(); + } + StablehloConvolutionOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(StablehloConvolutionOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloConvolutionOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct StablehloConvolutionOptionsBuilder { + typedef StablehloConvolutionOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_window_strides(::flatbuffers::Offset<::flatbuffers::Vector> window_strides) { + fbb_.AddOffset(StablehloConvolutionOptions::VT_WINDOW_STRIDES, window_strides); + } + void add_padding(::flatbuffers::Offset<::flatbuffers::Vector> padding) { + fbb_.AddOffset(StablehloConvolutionOptions::VT_PADDING, padding); + } + void add_lhs_dilation(::flatbuffers::Offset<::flatbuffers::Vector> lhs_dilation) { + fbb_.AddOffset(StablehloConvolutionOptions::VT_LHS_DILATION, lhs_dilation); + } + void add_rhs_dilation(::flatbuffers::Offset<::flatbuffers::Vector> rhs_dilation) { + fbb_.AddOffset(StablehloConvolutionOptions::VT_RHS_DILATION, rhs_dilation); + } + void add_window_reversal(::flatbuffers::Offset<::flatbuffers::Vector> window_reversal) { + fbb_.AddOffset(StablehloConvolutionOptions::VT_WINDOW_REVERSAL, window_reversal); + } + void add_input_batch_dimension(int64_t input_batch_dimension) { + fbb_.AddElement(StablehloConvolutionOptions::VT_INPUT_BATCH_DIMENSION, input_batch_dimension, 0); + } + void add_input_feature_dimension(int64_t input_feature_dimension) { + fbb_.AddElement(StablehloConvolutionOptions::VT_INPUT_FEATURE_DIMENSION, input_feature_dimension, 0); + } + void add_input_spatial_dimensions(::flatbuffers::Offset<::flatbuffers::Vector> input_spatial_dimensions) { + fbb_.AddOffset(StablehloConvolutionOptions::VT_INPUT_SPATIAL_DIMENSIONS, input_spatial_dimensions); + } + void add_kernel_input_feature_dimension(int64_t kernel_input_feature_dimension) { + fbb_.AddElement(StablehloConvolutionOptions::VT_KERNEL_INPUT_FEATURE_DIMENSION, kernel_input_feature_dimension, 0); + } + void add_kernel_output_feature_dimension(int64_t kernel_output_feature_dimension) { + fbb_.AddElement(StablehloConvolutionOptions::VT_KERNEL_OUTPUT_FEATURE_DIMENSION, kernel_output_feature_dimension, 0); + } + void add_kernel_spatial_dimensions(::flatbuffers::Offset<::flatbuffers::Vector> kernel_spatial_dimensions) { + fbb_.AddOffset(StablehloConvolutionOptions::VT_KERNEL_SPATIAL_DIMENSIONS, kernel_spatial_dimensions); + } + void add_output_batch_dimension(int64_t output_batch_dimension) { + fbb_.AddElement(StablehloConvolutionOptions::VT_OUTPUT_BATCH_DIMENSION, output_batch_dimension, 0); + } + void add_output_feature_dimension(int64_t output_feature_dimension) { + fbb_.AddElement(StablehloConvolutionOptions::VT_OUTPUT_FEATURE_DIMENSION, output_feature_dimension, 0); + } + void add_output_spatial_dimensions(::flatbuffers::Offset<::flatbuffers::Vector> output_spatial_dimensions) { + fbb_.AddOffset(StablehloConvolutionOptions::VT_OUTPUT_SPATIAL_DIMENSIONS, output_spatial_dimensions); + } + void add_feature_group_count(int64_t feature_group_count) { + fbb_.AddElement(StablehloConvolutionOptions::VT_FEATURE_GROUP_COUNT, feature_group_count, 0); + } + void add_batch_group_count(int64_t batch_group_count) { + fbb_.AddElement(StablehloConvolutionOptions::VT_BATCH_GROUP_COUNT, batch_group_count, 0); + } + void add_precision_config(::flatbuffers::Offset<::flatbuffers::Vector> precision_config) { + fbb_.AddOffset(StablehloConvolutionOptions::VT_PRECISION_CONFIG, precision_config); + } + explicit StablehloConvolutionOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateStablehloConvolutionOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::Vector> window_strides = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> padding = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> lhs_dilation = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> rhs_dilation = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> window_reversal = 0, + int64_t input_batch_dimension = 0, + int64_t input_feature_dimension = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> input_spatial_dimensions = 0, + int64_t kernel_input_feature_dimension = 0, + int64_t kernel_output_feature_dimension = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> kernel_spatial_dimensions = 0, + int64_t output_batch_dimension = 0, + int64_t output_feature_dimension = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> output_spatial_dimensions = 0, + int64_t feature_group_count = 0, + int64_t batch_group_count = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> precision_config = 0) { + StablehloConvolutionOptionsBuilder builder_(_fbb); + builder_.add_batch_group_count(batch_group_count); + builder_.add_feature_group_count(feature_group_count); + builder_.add_output_feature_dimension(output_feature_dimension); + builder_.add_output_batch_dimension(output_batch_dimension); + builder_.add_kernel_output_feature_dimension(kernel_output_feature_dimension); + builder_.add_kernel_input_feature_dimension(kernel_input_feature_dimension); + builder_.add_input_feature_dimension(input_feature_dimension); + builder_.add_input_batch_dimension(input_batch_dimension); + builder_.add_precision_config(precision_config); + builder_.add_output_spatial_dimensions(output_spatial_dimensions); + builder_.add_kernel_spatial_dimensions(kernel_spatial_dimensions); + builder_.add_input_spatial_dimensions(input_spatial_dimensions); + builder_.add_window_reversal(window_reversal); + builder_.add_rhs_dilation(rhs_dilation); + builder_.add_lhs_dilation(lhs_dilation); + builder_.add_padding(padding); + builder_.add_window_strides(window_strides); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateStablehloConvolutionOptionsDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const std::vector *window_strides = nullptr, + const std::vector *padding = nullptr, + const std::vector *lhs_dilation = nullptr, + const std::vector *rhs_dilation = nullptr, + const std::vector *window_reversal = nullptr, + int64_t input_batch_dimension = 0, + int64_t input_feature_dimension = 0, + const std::vector *input_spatial_dimensions = nullptr, + int64_t kernel_input_feature_dimension = 0, + int64_t kernel_output_feature_dimension = 0, + const std::vector *kernel_spatial_dimensions = nullptr, + int64_t output_batch_dimension = 0, + int64_t output_feature_dimension = 0, + const std::vector *output_spatial_dimensions = nullptr, + int64_t feature_group_count = 0, + int64_t batch_group_count = 0, + const std::vector *precision_config = nullptr) { + auto window_strides__ = window_strides ? _fbb.CreateVector(*window_strides) : 0; + auto padding__ = padding ? _fbb.CreateVector(*padding) : 0; + auto lhs_dilation__ = lhs_dilation ? _fbb.CreateVector(*lhs_dilation) : 0; + auto rhs_dilation__ = rhs_dilation ? _fbb.CreateVector(*rhs_dilation) : 0; + auto window_reversal__ = window_reversal ? _fbb.CreateVector(*window_reversal) : 0; + auto input_spatial_dimensions__ = input_spatial_dimensions ? _fbb.CreateVector(*input_spatial_dimensions) : 0; + auto kernel_spatial_dimensions__ = kernel_spatial_dimensions ? _fbb.CreateVector(*kernel_spatial_dimensions) : 0; + auto output_spatial_dimensions__ = output_spatial_dimensions ? _fbb.CreateVector(*output_spatial_dimensions) : 0; + auto precision_config__ = precision_config ? _fbb.CreateVector(*precision_config) : 0; + return tflite::CreateStablehloConvolutionOptions( + _fbb, + window_strides__, + padding__, + lhs_dilation__, + rhs_dilation__, + window_reversal__, + input_batch_dimension, + input_feature_dimension, + input_spatial_dimensions__, + kernel_input_feature_dimension, + kernel_output_feature_dimension, + kernel_spatial_dimensions__, + output_batch_dimension, + output_feature_dimension, + output_spatial_dimensions__, + feature_group_count, + batch_group_count, + precision_config__); +} + +::flatbuffers::Offset CreateStablehloConvolutionOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloConvolutionOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct StablehloScatterOptionsT : public ::flatbuffers::NativeTable { + typedef StablehloScatterOptions TableType; + bool indices_are_sorted = false; + std::vector update_window_dims{}; + std::vector inserted_window_dims{}; + std::vector scatter_dims_to_operand_dims{}; + int64_t index_vector_dim = 0; + bool unique_indices = false; + int32_t update_computation_subgraph_index = 0; +}; + +struct StablehloScatterOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef StablehloScatterOptionsT NativeTableType; + typedef StablehloScatterOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_INDICES_ARE_SORTED = 4, + VT_UPDATE_WINDOW_DIMS = 6, + VT_INSERTED_WINDOW_DIMS = 8, + VT_SCATTER_DIMS_TO_OPERAND_DIMS = 10, + VT_INDEX_VECTOR_DIM = 12, + VT_UNIQUE_INDICES = 14, + VT_UPDATE_COMPUTATION_SUBGRAPH_INDEX = 16 + }; + bool indices_are_sorted() const { + return GetField(VT_INDICES_ARE_SORTED, 0) != 0; + } + const ::flatbuffers::Vector *update_window_dims() const { + return GetPointer *>(VT_UPDATE_WINDOW_DIMS); + } + const ::flatbuffers::Vector *inserted_window_dims() const { + return GetPointer *>(VT_INSERTED_WINDOW_DIMS); + } + const ::flatbuffers::Vector *scatter_dims_to_operand_dims() const { + return GetPointer *>(VT_SCATTER_DIMS_TO_OPERAND_DIMS); + } + int64_t index_vector_dim() const { + return GetField(VT_INDEX_VECTOR_DIM, 0); + } + bool unique_indices() const { + return GetField(VT_UNIQUE_INDICES, 0) != 0; + } + int32_t update_computation_subgraph_index() const { + return GetField(VT_UPDATE_COMPUTATION_SUBGRAPH_INDEX, 0); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_INDICES_ARE_SORTED, 1) && + VerifyOffset(verifier, VT_UPDATE_WINDOW_DIMS) && + verifier.VerifyVector(update_window_dims()) && + VerifyOffset(verifier, VT_INSERTED_WINDOW_DIMS) && + verifier.VerifyVector(inserted_window_dims()) && + VerifyOffset(verifier, VT_SCATTER_DIMS_TO_OPERAND_DIMS) && + verifier.VerifyVector(scatter_dims_to_operand_dims()) && + VerifyField(verifier, VT_INDEX_VECTOR_DIM, 8) && + VerifyField(verifier, VT_UNIQUE_INDICES, 1) && + VerifyField(verifier, VT_UPDATE_COMPUTATION_SUBGRAPH_INDEX, 4) && + verifier.EndTable(); + } + StablehloScatterOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(StablehloScatterOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloScatterOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct StablehloScatterOptionsBuilder { + typedef StablehloScatterOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_indices_are_sorted(bool indices_are_sorted) { + fbb_.AddElement(StablehloScatterOptions::VT_INDICES_ARE_SORTED, static_cast(indices_are_sorted), 0); + } + void add_update_window_dims(::flatbuffers::Offset<::flatbuffers::Vector> update_window_dims) { + fbb_.AddOffset(StablehloScatterOptions::VT_UPDATE_WINDOW_DIMS, update_window_dims); + } + void add_inserted_window_dims(::flatbuffers::Offset<::flatbuffers::Vector> inserted_window_dims) { + fbb_.AddOffset(StablehloScatterOptions::VT_INSERTED_WINDOW_DIMS, inserted_window_dims); + } + void add_scatter_dims_to_operand_dims(::flatbuffers::Offset<::flatbuffers::Vector> scatter_dims_to_operand_dims) { + fbb_.AddOffset(StablehloScatterOptions::VT_SCATTER_DIMS_TO_OPERAND_DIMS, scatter_dims_to_operand_dims); + } + void add_index_vector_dim(int64_t index_vector_dim) { + fbb_.AddElement(StablehloScatterOptions::VT_INDEX_VECTOR_DIM, index_vector_dim, 0); + } + void add_unique_indices(bool unique_indices) { + fbb_.AddElement(StablehloScatterOptions::VT_UNIQUE_INDICES, static_cast(unique_indices), 0); + } + void add_update_computation_subgraph_index(int32_t update_computation_subgraph_index) { + fbb_.AddElement(StablehloScatterOptions::VT_UPDATE_COMPUTATION_SUBGRAPH_INDEX, update_computation_subgraph_index, 0); + } + explicit StablehloScatterOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateStablehloScatterOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + bool indices_are_sorted = false, + ::flatbuffers::Offset<::flatbuffers::Vector> update_window_dims = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> inserted_window_dims = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> scatter_dims_to_operand_dims = 0, + int64_t index_vector_dim = 0, + bool unique_indices = false, + int32_t update_computation_subgraph_index = 0) { + StablehloScatterOptionsBuilder builder_(_fbb); + builder_.add_index_vector_dim(index_vector_dim); + builder_.add_update_computation_subgraph_index(update_computation_subgraph_index); + builder_.add_scatter_dims_to_operand_dims(scatter_dims_to_operand_dims); + builder_.add_inserted_window_dims(inserted_window_dims); + builder_.add_update_window_dims(update_window_dims); + builder_.add_unique_indices(unique_indices); + builder_.add_indices_are_sorted(indices_are_sorted); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateStablehloScatterOptionsDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + bool indices_are_sorted = false, + const std::vector *update_window_dims = nullptr, + const std::vector *inserted_window_dims = nullptr, + const std::vector *scatter_dims_to_operand_dims = nullptr, + int64_t index_vector_dim = 0, + bool unique_indices = false, + int32_t update_computation_subgraph_index = 0) { + auto update_window_dims__ = update_window_dims ? _fbb.CreateVector(*update_window_dims) : 0; + auto inserted_window_dims__ = inserted_window_dims ? _fbb.CreateVector(*inserted_window_dims) : 0; + auto scatter_dims_to_operand_dims__ = scatter_dims_to_operand_dims ? _fbb.CreateVector(*scatter_dims_to_operand_dims) : 0; + return tflite::CreateStablehloScatterOptions( + _fbb, + indices_are_sorted, + update_window_dims__, + inserted_window_dims__, + scatter_dims_to_operand_dims__, + index_vector_dim, + unique_indices, + update_computation_subgraph_index); +} + +::flatbuffers::Offset CreateStablehloScatterOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloScatterOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct StablehloCaseOptionsT : public ::flatbuffers::NativeTable { + typedef StablehloCaseOptions TableType; + std::vector branch_subgraph_indices{}; +}; + +struct StablehloCaseOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef StablehloCaseOptionsT NativeTableType; + typedef StablehloCaseOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_BRANCH_SUBGRAPH_INDICES = 4 + }; + const ::flatbuffers::Vector *branch_subgraph_indices() const { + return GetPointer *>(VT_BRANCH_SUBGRAPH_INDICES); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_BRANCH_SUBGRAPH_INDICES) && + verifier.VerifyVector(branch_subgraph_indices()) && + verifier.EndTable(); + } + StablehloCaseOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(StablehloCaseOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloCaseOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct StablehloCaseOptionsBuilder { + typedef StablehloCaseOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_branch_subgraph_indices(::flatbuffers::Offset<::flatbuffers::Vector> branch_subgraph_indices) { + fbb_.AddOffset(StablehloCaseOptions::VT_BRANCH_SUBGRAPH_INDICES, branch_subgraph_indices); + } + explicit StablehloCaseOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateStablehloCaseOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::Vector> branch_subgraph_indices = 0) { + StablehloCaseOptionsBuilder builder_(_fbb); + builder_.add_branch_subgraph_indices(branch_subgraph_indices); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateStablehloCaseOptionsDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const std::vector *branch_subgraph_indices = nullptr) { + auto branch_subgraph_indices__ = branch_subgraph_indices ? _fbb.CreateVector(*branch_subgraph_indices) : 0; + return tflite::CreateStablehloCaseOptions( + _fbb, + branch_subgraph_indices__); +} + +::flatbuffers::Offset CreateStablehloCaseOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloCaseOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct StablehloRngBitGeneratorOptionsT : public ::flatbuffers::NativeTable { + typedef StablehloRngBitGeneratorOptions TableType; + tflite::RngAlgorithm algorithm = tflite::RngAlgorithm_DEFAULT; +}; + +struct StablehloRngBitGeneratorOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef StablehloRngBitGeneratorOptionsT NativeTableType; + typedef StablehloRngBitGeneratorOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_ALGORITHM = 4 + }; + tflite::RngAlgorithm algorithm() const { + return static_cast(GetField(VT_ALGORITHM, 0)); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_ALGORITHM, 1) && + verifier.EndTable(); + } + StablehloRngBitGeneratorOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(StablehloRngBitGeneratorOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloRngBitGeneratorOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct StablehloRngBitGeneratorOptionsBuilder { + typedef StablehloRngBitGeneratorOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_algorithm(tflite::RngAlgorithm algorithm) { + fbb_.AddElement(StablehloRngBitGeneratorOptions::VT_ALGORITHM, static_cast(algorithm), 0); + } + explicit StablehloRngBitGeneratorOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateStablehloRngBitGeneratorOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + tflite::RngAlgorithm algorithm = tflite::RngAlgorithm_DEFAULT) { + StablehloRngBitGeneratorOptionsBuilder builder_(_fbb); + builder_.add_algorithm(algorithm); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateStablehloRngBitGeneratorOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloRngBitGeneratorOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct Conv2DOptionsT : public ::flatbuffers::NativeTable { + typedef Conv2DOptions TableType; + tflite::Padding padding = tflite::Padding_SAME; + int32_t stride_w = 0; + int32_t stride_h = 0; + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE; + int32_t dilation_w_factor = 1; + int32_t dilation_h_factor = 1; + tflite::TensorType quantized_bias_type = tflite::TensorType_FLOAT32; +}; + +struct Conv2DOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef Conv2DOptionsT NativeTableType; + typedef Conv2DOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_PADDING = 4, + VT_STRIDE_W = 6, + VT_STRIDE_H = 8, + VT_FUSED_ACTIVATION_FUNCTION = 10, + VT_DILATION_W_FACTOR = 12, + VT_DILATION_H_FACTOR = 14, + VT_QUANTIZED_BIAS_TYPE = 16 + }; + tflite::Padding padding() const { + return static_cast(GetField(VT_PADDING, 0)); + } + int32_t stride_w() const { + return GetField(VT_STRIDE_W, 0); + } + int32_t stride_h() const { + return GetField(VT_STRIDE_H, 0); + } + tflite::ActivationFunctionType fused_activation_function() const { + return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); + } + int32_t dilation_w_factor() const { + return GetField(VT_DILATION_W_FACTOR, 1); + } + int32_t dilation_h_factor() const { + return GetField(VT_DILATION_H_FACTOR, 1); + } + tflite::TensorType quantized_bias_type() const { + return static_cast(GetField(VT_QUANTIZED_BIAS_TYPE, 0)); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_PADDING, 1) && + VerifyField(verifier, VT_STRIDE_W, 4) && + VerifyField(verifier, VT_STRIDE_H, 4) && + VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION, 1) && + VerifyField(verifier, VT_DILATION_W_FACTOR, 4) && + VerifyField(verifier, VT_DILATION_H_FACTOR, 4) && + VerifyField(verifier, VT_QUANTIZED_BIAS_TYPE, 1) && + verifier.EndTable(); + } + Conv2DOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(Conv2DOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const Conv2DOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct Conv2DOptionsBuilder { + typedef Conv2DOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_padding(tflite::Padding padding) { + fbb_.AddElement(Conv2DOptions::VT_PADDING, static_cast(padding), 0); + } + void add_stride_w(int32_t stride_w) { + fbb_.AddElement(Conv2DOptions::VT_STRIDE_W, stride_w, 0); + } + void add_stride_h(int32_t stride_h) { + fbb_.AddElement(Conv2DOptions::VT_STRIDE_H, stride_h, 0); + } + void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { + fbb_.AddElement(Conv2DOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); + } + void add_dilation_w_factor(int32_t dilation_w_factor) { + fbb_.AddElement(Conv2DOptions::VT_DILATION_W_FACTOR, dilation_w_factor, 1); + } + void add_dilation_h_factor(int32_t dilation_h_factor) { + fbb_.AddElement(Conv2DOptions::VT_DILATION_H_FACTOR, dilation_h_factor, 1); + } + void add_quantized_bias_type(tflite::TensorType quantized_bias_type) { + fbb_.AddElement(Conv2DOptions::VT_QUANTIZED_BIAS_TYPE, static_cast(quantized_bias_type), 0); + } + explicit Conv2DOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateConv2DOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + tflite::Padding padding = tflite::Padding_SAME, + int32_t stride_w = 0, + int32_t stride_h = 0, + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE, + int32_t dilation_w_factor = 1, + int32_t dilation_h_factor = 1, + tflite::TensorType quantized_bias_type = tflite::TensorType_FLOAT32) { + Conv2DOptionsBuilder builder_(_fbb); + builder_.add_dilation_h_factor(dilation_h_factor); + builder_.add_dilation_w_factor(dilation_w_factor); + builder_.add_stride_h(stride_h); + builder_.add_stride_w(stride_w); + builder_.add_quantized_bias_type(quantized_bias_type); + builder_.add_fused_activation_function(fused_activation_function); + builder_.add_padding(padding); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateConv2DOptions(::flatbuffers::FlatBufferBuilder &_fbb, const Conv2DOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct Conv3DOptionsT : public ::flatbuffers::NativeTable { + typedef Conv3DOptions TableType; + tflite::Padding padding = tflite::Padding_SAME; + int32_t stride_d = 0; + int32_t stride_w = 0; + int32_t stride_h = 0; + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE; + int32_t dilation_d_factor = 1; + int32_t dilation_w_factor = 1; + int32_t dilation_h_factor = 1; +}; + +struct Conv3DOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef Conv3DOptionsT NativeTableType; + typedef Conv3DOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_PADDING = 4, + VT_STRIDE_D = 6, + VT_STRIDE_W = 8, + VT_STRIDE_H = 10, + VT_FUSED_ACTIVATION_FUNCTION = 12, + VT_DILATION_D_FACTOR = 14, + VT_DILATION_W_FACTOR = 16, + VT_DILATION_H_FACTOR = 18 + }; + tflite::Padding padding() const { + return static_cast(GetField(VT_PADDING, 0)); + } + int32_t stride_d() const { + return GetField(VT_STRIDE_D, 0); + } + int32_t stride_w() const { + return GetField(VT_STRIDE_W, 0); + } + int32_t stride_h() const { + return GetField(VT_STRIDE_H, 0); + } + tflite::ActivationFunctionType fused_activation_function() const { + return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); + } + int32_t dilation_d_factor() const { + return GetField(VT_DILATION_D_FACTOR, 1); + } + int32_t dilation_w_factor() const { + return GetField(VT_DILATION_W_FACTOR, 1); + } + int32_t dilation_h_factor() const { + return GetField(VT_DILATION_H_FACTOR, 1); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_PADDING, 1) && + VerifyField(verifier, VT_STRIDE_D, 4) && + VerifyField(verifier, VT_STRIDE_W, 4) && + VerifyField(verifier, VT_STRIDE_H, 4) && + VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION, 1) && + VerifyField(verifier, VT_DILATION_D_FACTOR, 4) && + VerifyField(verifier, VT_DILATION_W_FACTOR, 4) && + VerifyField(verifier, VT_DILATION_H_FACTOR, 4) && + verifier.EndTable(); + } + Conv3DOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(Conv3DOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const Conv3DOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct Conv3DOptionsBuilder { + typedef Conv3DOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_padding(tflite::Padding padding) { + fbb_.AddElement(Conv3DOptions::VT_PADDING, static_cast(padding), 0); + } + void add_stride_d(int32_t stride_d) { + fbb_.AddElement(Conv3DOptions::VT_STRIDE_D, stride_d, 0); + } + void add_stride_w(int32_t stride_w) { + fbb_.AddElement(Conv3DOptions::VT_STRIDE_W, stride_w, 0); + } + void add_stride_h(int32_t stride_h) { + fbb_.AddElement(Conv3DOptions::VT_STRIDE_H, stride_h, 0); + } + void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { + fbb_.AddElement(Conv3DOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); + } + void add_dilation_d_factor(int32_t dilation_d_factor) { + fbb_.AddElement(Conv3DOptions::VT_DILATION_D_FACTOR, dilation_d_factor, 1); + } + void add_dilation_w_factor(int32_t dilation_w_factor) { + fbb_.AddElement(Conv3DOptions::VT_DILATION_W_FACTOR, dilation_w_factor, 1); + } + void add_dilation_h_factor(int32_t dilation_h_factor) { + fbb_.AddElement(Conv3DOptions::VT_DILATION_H_FACTOR, dilation_h_factor, 1); + } + explicit Conv3DOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateConv3DOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + tflite::Padding padding = tflite::Padding_SAME, + int32_t stride_d = 0, + int32_t stride_w = 0, + int32_t stride_h = 0, + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE, + int32_t dilation_d_factor = 1, + int32_t dilation_w_factor = 1, + int32_t dilation_h_factor = 1) { + Conv3DOptionsBuilder builder_(_fbb); + builder_.add_dilation_h_factor(dilation_h_factor); + builder_.add_dilation_w_factor(dilation_w_factor); + builder_.add_dilation_d_factor(dilation_d_factor); + builder_.add_stride_h(stride_h); + builder_.add_stride_w(stride_w); + builder_.add_stride_d(stride_d); + builder_.add_fused_activation_function(fused_activation_function); + builder_.add_padding(padding); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateConv3DOptions(::flatbuffers::FlatBufferBuilder &_fbb, const Conv3DOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct Pool2DOptionsT : public ::flatbuffers::NativeTable { + typedef Pool2DOptions TableType; + tflite::Padding padding = tflite::Padding_SAME; + int32_t stride_w = 0; + int32_t stride_h = 0; + int32_t filter_width = 0; + int32_t filter_height = 0; + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE; +}; + +struct Pool2DOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef Pool2DOptionsT NativeTableType; + typedef Pool2DOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_PADDING = 4, + VT_STRIDE_W = 6, + VT_STRIDE_H = 8, + VT_FILTER_WIDTH = 10, + VT_FILTER_HEIGHT = 12, + VT_FUSED_ACTIVATION_FUNCTION = 14 + }; + tflite::Padding padding() const { + return static_cast(GetField(VT_PADDING, 0)); + } + int32_t stride_w() const { + return GetField(VT_STRIDE_W, 0); + } + int32_t stride_h() const { + return GetField(VT_STRIDE_H, 0); + } + int32_t filter_width() const { + return GetField(VT_FILTER_WIDTH, 0); + } + int32_t filter_height() const { + return GetField(VT_FILTER_HEIGHT, 0); + } + tflite::ActivationFunctionType fused_activation_function() const { + return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_PADDING, 1) && + VerifyField(verifier, VT_STRIDE_W, 4) && + VerifyField(verifier, VT_STRIDE_H, 4) && + VerifyField(verifier, VT_FILTER_WIDTH, 4) && + VerifyField(verifier, VT_FILTER_HEIGHT, 4) && + VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION, 1) && + verifier.EndTable(); + } + Pool2DOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(Pool2DOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const Pool2DOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct Pool2DOptionsBuilder { + typedef Pool2DOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_padding(tflite::Padding padding) { + fbb_.AddElement(Pool2DOptions::VT_PADDING, static_cast(padding), 0); + } + void add_stride_w(int32_t stride_w) { + fbb_.AddElement(Pool2DOptions::VT_STRIDE_W, stride_w, 0); + } + void add_stride_h(int32_t stride_h) { + fbb_.AddElement(Pool2DOptions::VT_STRIDE_H, stride_h, 0); + } + void add_filter_width(int32_t filter_width) { + fbb_.AddElement(Pool2DOptions::VT_FILTER_WIDTH, filter_width, 0); + } + void add_filter_height(int32_t filter_height) { + fbb_.AddElement(Pool2DOptions::VT_FILTER_HEIGHT, filter_height, 0); + } + void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { + fbb_.AddElement(Pool2DOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); + } + explicit Pool2DOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreatePool2DOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + tflite::Padding padding = tflite::Padding_SAME, + int32_t stride_w = 0, + int32_t stride_h = 0, + int32_t filter_width = 0, + int32_t filter_height = 0, + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE) { + Pool2DOptionsBuilder builder_(_fbb); + builder_.add_filter_height(filter_height); + builder_.add_filter_width(filter_width); + builder_.add_stride_h(stride_h); + builder_.add_stride_w(stride_w); + builder_.add_fused_activation_function(fused_activation_function); + builder_.add_padding(padding); + return builder_.Finish(); +} + +::flatbuffers::Offset CreatePool2DOptions(::flatbuffers::FlatBufferBuilder &_fbb, const Pool2DOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct DepthwiseConv2DOptionsT : public ::flatbuffers::NativeTable { + typedef DepthwiseConv2DOptions TableType; + tflite::Padding padding = tflite::Padding_SAME; + int32_t stride_w = 0; + int32_t stride_h = 0; + int32_t depth_multiplier = 0; + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE; + int32_t dilation_w_factor = 1; + int32_t dilation_h_factor = 1; +}; + +struct DepthwiseConv2DOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef DepthwiseConv2DOptionsT NativeTableType; + typedef DepthwiseConv2DOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_PADDING = 4, + VT_STRIDE_W = 6, + VT_STRIDE_H = 8, + VT_DEPTH_MULTIPLIER = 10, + VT_FUSED_ACTIVATION_FUNCTION = 12, + VT_DILATION_W_FACTOR = 14, + VT_DILATION_H_FACTOR = 16 + }; + tflite::Padding padding() const { + return static_cast(GetField(VT_PADDING, 0)); + } + int32_t stride_w() const { + return GetField(VT_STRIDE_W, 0); + } + int32_t stride_h() const { + return GetField(VT_STRIDE_H, 0); + } + int32_t depth_multiplier() const { + return GetField(VT_DEPTH_MULTIPLIER, 0); + } + tflite::ActivationFunctionType fused_activation_function() const { + return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); + } + int32_t dilation_w_factor() const { + return GetField(VT_DILATION_W_FACTOR, 1); + } + int32_t dilation_h_factor() const { + return GetField(VT_DILATION_H_FACTOR, 1); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_PADDING, 1) && + VerifyField(verifier, VT_STRIDE_W, 4) && + VerifyField(verifier, VT_STRIDE_H, 4) && + VerifyField(verifier, VT_DEPTH_MULTIPLIER, 4) && + VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION, 1) && + VerifyField(verifier, VT_DILATION_W_FACTOR, 4) && + VerifyField(verifier, VT_DILATION_H_FACTOR, 4) && + verifier.EndTable(); + } + DepthwiseConv2DOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(DepthwiseConv2DOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const DepthwiseConv2DOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct DepthwiseConv2DOptionsBuilder { + typedef DepthwiseConv2DOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_padding(tflite::Padding padding) { + fbb_.AddElement(DepthwiseConv2DOptions::VT_PADDING, static_cast(padding), 0); + } + void add_stride_w(int32_t stride_w) { + fbb_.AddElement(DepthwiseConv2DOptions::VT_STRIDE_W, stride_w, 0); + } + void add_stride_h(int32_t stride_h) { + fbb_.AddElement(DepthwiseConv2DOptions::VT_STRIDE_H, stride_h, 0); + } + void add_depth_multiplier(int32_t depth_multiplier) { + fbb_.AddElement(DepthwiseConv2DOptions::VT_DEPTH_MULTIPLIER, depth_multiplier, 0); + } + void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { + fbb_.AddElement(DepthwiseConv2DOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); + } + void add_dilation_w_factor(int32_t dilation_w_factor) { + fbb_.AddElement(DepthwiseConv2DOptions::VT_DILATION_W_FACTOR, dilation_w_factor, 1); + } + void add_dilation_h_factor(int32_t dilation_h_factor) { + fbb_.AddElement(DepthwiseConv2DOptions::VT_DILATION_H_FACTOR, dilation_h_factor, 1); + } + explicit DepthwiseConv2DOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateDepthwiseConv2DOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + tflite::Padding padding = tflite::Padding_SAME, + int32_t stride_w = 0, + int32_t stride_h = 0, + int32_t depth_multiplier = 0, + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE, + int32_t dilation_w_factor = 1, + int32_t dilation_h_factor = 1) { + DepthwiseConv2DOptionsBuilder builder_(_fbb); + builder_.add_dilation_h_factor(dilation_h_factor); + builder_.add_dilation_w_factor(dilation_w_factor); + builder_.add_depth_multiplier(depth_multiplier); + builder_.add_stride_h(stride_h); + builder_.add_stride_w(stride_w); + builder_.add_fused_activation_function(fused_activation_function); + builder_.add_padding(padding); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateDepthwiseConv2DOptions(::flatbuffers::FlatBufferBuilder &_fbb, const DepthwiseConv2DOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct ConcatEmbeddingsOptionsT : public ::flatbuffers::NativeTable { + typedef ConcatEmbeddingsOptions TableType; + int32_t num_channels = 0; + std::vector num_columns_per_channel{}; + std::vector embedding_dim_per_channel{}; +}; + +struct ConcatEmbeddingsOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef ConcatEmbeddingsOptionsT NativeTableType; + typedef ConcatEmbeddingsOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_NUM_CHANNELS = 4, + VT_NUM_COLUMNS_PER_CHANNEL = 6, + VT_EMBEDDING_DIM_PER_CHANNEL = 8 + }; + int32_t num_channels() const { + return GetField(VT_NUM_CHANNELS, 0); + } + const ::flatbuffers::Vector *num_columns_per_channel() const { + return GetPointer *>(VT_NUM_COLUMNS_PER_CHANNEL); + } + const ::flatbuffers::Vector *embedding_dim_per_channel() const { + return GetPointer *>(VT_EMBEDDING_DIM_PER_CHANNEL); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_NUM_CHANNELS, 4) && + VerifyOffset(verifier, VT_NUM_COLUMNS_PER_CHANNEL) && + verifier.VerifyVector(num_columns_per_channel()) && + VerifyOffset(verifier, VT_EMBEDDING_DIM_PER_CHANNEL) && + verifier.VerifyVector(embedding_dim_per_channel()) && + verifier.EndTable(); + } + ConcatEmbeddingsOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(ConcatEmbeddingsOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ConcatEmbeddingsOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct ConcatEmbeddingsOptionsBuilder { + typedef ConcatEmbeddingsOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_num_channels(int32_t num_channels) { + fbb_.AddElement(ConcatEmbeddingsOptions::VT_NUM_CHANNELS, num_channels, 0); + } + void add_num_columns_per_channel(::flatbuffers::Offset<::flatbuffers::Vector> num_columns_per_channel) { + fbb_.AddOffset(ConcatEmbeddingsOptions::VT_NUM_COLUMNS_PER_CHANNEL, num_columns_per_channel); + } + void add_embedding_dim_per_channel(::flatbuffers::Offset<::flatbuffers::Vector> embedding_dim_per_channel) { + fbb_.AddOffset(ConcatEmbeddingsOptions::VT_EMBEDDING_DIM_PER_CHANNEL, embedding_dim_per_channel); + } + explicit ConcatEmbeddingsOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateConcatEmbeddingsOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + int32_t num_channels = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> num_columns_per_channel = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> embedding_dim_per_channel = 0) { + ConcatEmbeddingsOptionsBuilder builder_(_fbb); + builder_.add_embedding_dim_per_channel(embedding_dim_per_channel); + builder_.add_num_columns_per_channel(num_columns_per_channel); + builder_.add_num_channels(num_channels); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateConcatEmbeddingsOptionsDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + int32_t num_channels = 0, + const std::vector *num_columns_per_channel = nullptr, + const std::vector *embedding_dim_per_channel = nullptr) { + auto num_columns_per_channel__ = num_columns_per_channel ? _fbb.CreateVector(*num_columns_per_channel) : 0; + auto embedding_dim_per_channel__ = embedding_dim_per_channel ? _fbb.CreateVector(*embedding_dim_per_channel) : 0; + return tflite::CreateConcatEmbeddingsOptions( + _fbb, + num_channels, + num_columns_per_channel__, + embedding_dim_per_channel__); +} + +::flatbuffers::Offset CreateConcatEmbeddingsOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ConcatEmbeddingsOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct LSHProjectionOptionsT : public ::flatbuffers::NativeTable { + typedef LSHProjectionOptions TableType; + tflite::LSHProjectionType type = tflite::LSHProjectionType_UNKNOWN; +}; + +struct LSHProjectionOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef LSHProjectionOptionsT NativeTableType; + typedef LSHProjectionOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_TYPE = 4 + }; + tflite::LSHProjectionType type() const { + return static_cast(GetField(VT_TYPE, 0)); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_TYPE, 1) && + verifier.EndTable(); + } + LSHProjectionOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(LSHProjectionOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const LSHProjectionOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct LSHProjectionOptionsBuilder { + typedef LSHProjectionOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_type(tflite::LSHProjectionType type) { + fbb_.AddElement(LSHProjectionOptions::VT_TYPE, static_cast(type), 0); + } + explicit LSHProjectionOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateLSHProjectionOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + tflite::LSHProjectionType type = tflite::LSHProjectionType_UNKNOWN) { + LSHProjectionOptionsBuilder builder_(_fbb); + builder_.add_type(type); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateLSHProjectionOptions(::flatbuffers::FlatBufferBuilder &_fbb, const LSHProjectionOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct SVDFOptionsT : public ::flatbuffers::NativeTable { + typedef SVDFOptions TableType; + int32_t rank = 0; + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE; + bool asymmetric_quantize_inputs = false; +}; + +struct SVDFOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef SVDFOptionsT NativeTableType; + typedef SVDFOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_RANK = 4, + VT_FUSED_ACTIVATION_FUNCTION = 6, + VT_ASYMMETRIC_QUANTIZE_INPUTS = 8 + }; + int32_t rank() const { + return GetField(VT_RANK, 0); + } + tflite::ActivationFunctionType fused_activation_function() const { + return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); + } + bool asymmetric_quantize_inputs() const { + return GetField(VT_ASYMMETRIC_QUANTIZE_INPUTS, 0) != 0; + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_RANK, 4) && + VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION, 1) && + VerifyField(verifier, VT_ASYMMETRIC_QUANTIZE_INPUTS, 1) && + verifier.EndTable(); + } + SVDFOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(SVDFOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SVDFOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct SVDFOptionsBuilder { + typedef SVDFOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_rank(int32_t rank) { + fbb_.AddElement(SVDFOptions::VT_RANK, rank, 0); + } + void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { + fbb_.AddElement(SVDFOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); + } + void add_asymmetric_quantize_inputs(bool asymmetric_quantize_inputs) { + fbb_.AddElement(SVDFOptions::VT_ASYMMETRIC_QUANTIZE_INPUTS, static_cast(asymmetric_quantize_inputs), 0); + } + explicit SVDFOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateSVDFOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + int32_t rank = 0, + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE, + bool asymmetric_quantize_inputs = false) { + SVDFOptionsBuilder builder_(_fbb); + builder_.add_rank(rank); + builder_.add_asymmetric_quantize_inputs(asymmetric_quantize_inputs); + builder_.add_fused_activation_function(fused_activation_function); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateSVDFOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SVDFOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct RNNOptionsT : public ::flatbuffers::NativeTable { + typedef RNNOptions TableType; + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE; + bool asymmetric_quantize_inputs = false; +}; + +struct RNNOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef RNNOptionsT NativeTableType; + typedef RNNOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_FUSED_ACTIVATION_FUNCTION = 4, + VT_ASYMMETRIC_QUANTIZE_INPUTS = 6 + }; + tflite::ActivationFunctionType fused_activation_function() const { + return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); + } + bool asymmetric_quantize_inputs() const { + return GetField(VT_ASYMMETRIC_QUANTIZE_INPUTS, 0) != 0; + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION, 1) && + VerifyField(verifier, VT_ASYMMETRIC_QUANTIZE_INPUTS, 1) && + verifier.EndTable(); + } + RNNOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(RNNOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const RNNOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct RNNOptionsBuilder { + typedef RNNOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { + fbb_.AddElement(RNNOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); + } + void add_asymmetric_quantize_inputs(bool asymmetric_quantize_inputs) { + fbb_.AddElement(RNNOptions::VT_ASYMMETRIC_QUANTIZE_INPUTS, static_cast(asymmetric_quantize_inputs), 0); + } + explicit RNNOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateRNNOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE, + bool asymmetric_quantize_inputs = false) { + RNNOptionsBuilder builder_(_fbb); + builder_.add_asymmetric_quantize_inputs(asymmetric_quantize_inputs); + builder_.add_fused_activation_function(fused_activation_function); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateRNNOptions(::flatbuffers::FlatBufferBuilder &_fbb, const RNNOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct SequenceRNNOptionsT : public ::flatbuffers::NativeTable { + typedef SequenceRNNOptions TableType; + bool time_major = false; + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE; + bool asymmetric_quantize_inputs = false; +}; + +struct SequenceRNNOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef SequenceRNNOptionsT NativeTableType; + typedef SequenceRNNOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_TIME_MAJOR = 4, + VT_FUSED_ACTIVATION_FUNCTION = 6, + VT_ASYMMETRIC_QUANTIZE_INPUTS = 8 + }; + bool time_major() const { + return GetField(VT_TIME_MAJOR, 0) != 0; + } + tflite::ActivationFunctionType fused_activation_function() const { + return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); + } + bool asymmetric_quantize_inputs() const { + return GetField(VT_ASYMMETRIC_QUANTIZE_INPUTS, 0) != 0; + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_TIME_MAJOR, 1) && + VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION, 1) && + VerifyField(verifier, VT_ASYMMETRIC_QUANTIZE_INPUTS, 1) && + verifier.EndTable(); + } + SequenceRNNOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(SequenceRNNOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SequenceRNNOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct SequenceRNNOptionsBuilder { + typedef SequenceRNNOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_time_major(bool time_major) { + fbb_.AddElement(SequenceRNNOptions::VT_TIME_MAJOR, static_cast(time_major), 0); + } + void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { + fbb_.AddElement(SequenceRNNOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); + } + void add_asymmetric_quantize_inputs(bool asymmetric_quantize_inputs) { + fbb_.AddElement(SequenceRNNOptions::VT_ASYMMETRIC_QUANTIZE_INPUTS, static_cast(asymmetric_quantize_inputs), 0); + } + explicit SequenceRNNOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateSequenceRNNOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + bool time_major = false, + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE, + bool asymmetric_quantize_inputs = false) { + SequenceRNNOptionsBuilder builder_(_fbb); + builder_.add_asymmetric_quantize_inputs(asymmetric_quantize_inputs); + builder_.add_fused_activation_function(fused_activation_function); + builder_.add_time_major(time_major); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateSequenceRNNOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SequenceRNNOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct BidirectionalSequenceRNNOptionsT : public ::flatbuffers::NativeTable { + typedef BidirectionalSequenceRNNOptions TableType; + bool time_major = false; + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE; + bool merge_outputs = false; + bool asymmetric_quantize_inputs = false; +}; + +struct BidirectionalSequenceRNNOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef BidirectionalSequenceRNNOptionsT NativeTableType; + typedef BidirectionalSequenceRNNOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_TIME_MAJOR = 4, + VT_FUSED_ACTIVATION_FUNCTION = 6, + VT_MERGE_OUTPUTS = 8, + VT_ASYMMETRIC_QUANTIZE_INPUTS = 10 + }; + bool time_major() const { + return GetField(VT_TIME_MAJOR, 0) != 0; + } + tflite::ActivationFunctionType fused_activation_function() const { + return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); + } + bool merge_outputs() const { + return GetField(VT_MERGE_OUTPUTS, 0) != 0; + } + bool asymmetric_quantize_inputs() const { + return GetField(VT_ASYMMETRIC_QUANTIZE_INPUTS, 0) != 0; + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_TIME_MAJOR, 1) && + VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION, 1) && + VerifyField(verifier, VT_MERGE_OUTPUTS, 1) && + VerifyField(verifier, VT_ASYMMETRIC_QUANTIZE_INPUTS, 1) && + verifier.EndTable(); + } + BidirectionalSequenceRNNOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(BidirectionalSequenceRNNOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const BidirectionalSequenceRNNOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct BidirectionalSequenceRNNOptionsBuilder { + typedef BidirectionalSequenceRNNOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_time_major(bool time_major) { + fbb_.AddElement(BidirectionalSequenceRNNOptions::VT_TIME_MAJOR, static_cast(time_major), 0); + } + void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { + fbb_.AddElement(BidirectionalSequenceRNNOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); + } + void add_merge_outputs(bool merge_outputs) { + fbb_.AddElement(BidirectionalSequenceRNNOptions::VT_MERGE_OUTPUTS, static_cast(merge_outputs), 0); + } + void add_asymmetric_quantize_inputs(bool asymmetric_quantize_inputs) { + fbb_.AddElement(BidirectionalSequenceRNNOptions::VT_ASYMMETRIC_QUANTIZE_INPUTS, static_cast(asymmetric_quantize_inputs), 0); + } + explicit BidirectionalSequenceRNNOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateBidirectionalSequenceRNNOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + bool time_major = false, + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE, + bool merge_outputs = false, + bool asymmetric_quantize_inputs = false) { + BidirectionalSequenceRNNOptionsBuilder builder_(_fbb); + builder_.add_asymmetric_quantize_inputs(asymmetric_quantize_inputs); + builder_.add_merge_outputs(merge_outputs); + builder_.add_fused_activation_function(fused_activation_function); + builder_.add_time_major(time_major); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateBidirectionalSequenceRNNOptions(::flatbuffers::FlatBufferBuilder &_fbb, const BidirectionalSequenceRNNOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct FullyConnectedOptionsT : public ::flatbuffers::NativeTable { + typedef FullyConnectedOptions TableType; + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE; + tflite::FullyConnectedOptionsWeightsFormat weights_format = tflite::FullyConnectedOptionsWeightsFormat_DEFAULT; + bool keep_num_dims = false; + bool asymmetric_quantize_inputs = false; + tflite::TensorType quantized_bias_type = tflite::TensorType_FLOAT32; +}; + +struct FullyConnectedOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef FullyConnectedOptionsT NativeTableType; + typedef FullyConnectedOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_FUSED_ACTIVATION_FUNCTION = 4, + VT_WEIGHTS_FORMAT = 6, + VT_KEEP_NUM_DIMS = 8, + VT_ASYMMETRIC_QUANTIZE_INPUTS = 10, + VT_QUANTIZED_BIAS_TYPE = 12 + }; + tflite::ActivationFunctionType fused_activation_function() const { + return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); + } + tflite::FullyConnectedOptionsWeightsFormat weights_format() const { + return static_cast(GetField(VT_WEIGHTS_FORMAT, 0)); + } + bool keep_num_dims() const { + return GetField(VT_KEEP_NUM_DIMS, 0) != 0; + } + bool asymmetric_quantize_inputs() const { + return GetField(VT_ASYMMETRIC_QUANTIZE_INPUTS, 0) != 0; + } + tflite::TensorType quantized_bias_type() const { + return static_cast(GetField(VT_QUANTIZED_BIAS_TYPE, 0)); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION, 1) && + VerifyField(verifier, VT_WEIGHTS_FORMAT, 1) && + VerifyField(verifier, VT_KEEP_NUM_DIMS, 1) && + VerifyField(verifier, VT_ASYMMETRIC_QUANTIZE_INPUTS, 1) && + VerifyField(verifier, VT_QUANTIZED_BIAS_TYPE, 1) && + verifier.EndTable(); + } + FullyConnectedOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(FullyConnectedOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const FullyConnectedOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct FullyConnectedOptionsBuilder { + typedef FullyConnectedOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { + fbb_.AddElement(FullyConnectedOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); + } + void add_weights_format(tflite::FullyConnectedOptionsWeightsFormat weights_format) { + fbb_.AddElement(FullyConnectedOptions::VT_WEIGHTS_FORMAT, static_cast(weights_format), 0); + } + void add_keep_num_dims(bool keep_num_dims) { + fbb_.AddElement(FullyConnectedOptions::VT_KEEP_NUM_DIMS, static_cast(keep_num_dims), 0); + } + void add_asymmetric_quantize_inputs(bool asymmetric_quantize_inputs) { + fbb_.AddElement(FullyConnectedOptions::VT_ASYMMETRIC_QUANTIZE_INPUTS, static_cast(asymmetric_quantize_inputs), 0); + } + void add_quantized_bias_type(tflite::TensorType quantized_bias_type) { + fbb_.AddElement(FullyConnectedOptions::VT_QUANTIZED_BIAS_TYPE, static_cast(quantized_bias_type), 0); + } + explicit FullyConnectedOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateFullyConnectedOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE, + tflite::FullyConnectedOptionsWeightsFormat weights_format = tflite::FullyConnectedOptionsWeightsFormat_DEFAULT, + bool keep_num_dims = false, + bool asymmetric_quantize_inputs = false, + tflite::TensorType quantized_bias_type = tflite::TensorType_FLOAT32) { + FullyConnectedOptionsBuilder builder_(_fbb); + builder_.add_quantized_bias_type(quantized_bias_type); + builder_.add_asymmetric_quantize_inputs(asymmetric_quantize_inputs); + builder_.add_keep_num_dims(keep_num_dims); + builder_.add_weights_format(weights_format); + builder_.add_fused_activation_function(fused_activation_function); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateFullyConnectedOptions(::flatbuffers::FlatBufferBuilder &_fbb, const FullyConnectedOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct SoftmaxOptionsT : public ::flatbuffers::NativeTable { + typedef SoftmaxOptions TableType; + float beta = 0.0f; +}; + +struct SoftmaxOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef SoftmaxOptionsT NativeTableType; + typedef SoftmaxOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_BETA = 4 + }; + float beta() const { + return GetField(VT_BETA, 0.0f); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_BETA, 4) && + verifier.EndTable(); + } + SoftmaxOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(SoftmaxOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SoftmaxOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct SoftmaxOptionsBuilder { + typedef SoftmaxOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_beta(float beta) { + fbb_.AddElement(SoftmaxOptions::VT_BETA, beta, 0.0f); + } + explicit SoftmaxOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateSoftmaxOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + float beta = 0.0f) { + SoftmaxOptionsBuilder builder_(_fbb); + builder_.add_beta(beta); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateSoftmaxOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SoftmaxOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct ConcatenationOptionsT : public ::flatbuffers::NativeTable { + typedef ConcatenationOptions TableType; + int32_t axis = 0; + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE; +}; + +struct ConcatenationOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef ConcatenationOptionsT NativeTableType; + typedef ConcatenationOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_AXIS = 4, + VT_FUSED_ACTIVATION_FUNCTION = 6 + }; + int32_t axis() const { + return GetField(VT_AXIS, 0); + } + tflite::ActivationFunctionType fused_activation_function() const { + return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_AXIS, 4) && + VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION, 1) && + verifier.EndTable(); + } + ConcatenationOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(ConcatenationOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ConcatenationOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct ConcatenationOptionsBuilder { + typedef ConcatenationOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_axis(int32_t axis) { + fbb_.AddElement(ConcatenationOptions::VT_AXIS, axis, 0); + } + void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { + fbb_.AddElement(ConcatenationOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); + } + explicit ConcatenationOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateConcatenationOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + int32_t axis = 0, + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE) { + ConcatenationOptionsBuilder builder_(_fbb); + builder_.add_axis(axis); + builder_.add_fused_activation_function(fused_activation_function); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateConcatenationOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ConcatenationOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct AddOptionsT : public ::flatbuffers::NativeTable { + typedef AddOptions TableType; + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE; + bool pot_scale_int16 = true; +}; + +struct AddOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef AddOptionsT NativeTableType; + typedef AddOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_FUSED_ACTIVATION_FUNCTION = 4, + VT_POT_SCALE_INT16 = 6 + }; + tflite::ActivationFunctionType fused_activation_function() const { + return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); + } + bool pot_scale_int16() const { + return GetField(VT_POT_SCALE_INT16, 1) != 0; + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION, 1) && + VerifyField(verifier, VT_POT_SCALE_INT16, 1) && + verifier.EndTable(); + } + AddOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(AddOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const AddOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct AddOptionsBuilder { + typedef AddOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { + fbb_.AddElement(AddOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); + } + void add_pot_scale_int16(bool pot_scale_int16) { + fbb_.AddElement(AddOptions::VT_POT_SCALE_INT16, static_cast(pot_scale_int16), 1); + } + explicit AddOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateAddOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE, + bool pot_scale_int16 = true) { + AddOptionsBuilder builder_(_fbb); + builder_.add_pot_scale_int16(pot_scale_int16); + builder_.add_fused_activation_function(fused_activation_function); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateAddOptions(::flatbuffers::FlatBufferBuilder &_fbb, const AddOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct MulOptionsT : public ::flatbuffers::NativeTable { + typedef MulOptions TableType; + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE; +}; + +struct MulOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef MulOptionsT NativeTableType; + typedef MulOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_FUSED_ACTIVATION_FUNCTION = 4 + }; + tflite::ActivationFunctionType fused_activation_function() const { + return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION, 1) && + verifier.EndTable(); + } + MulOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(MulOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const MulOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct MulOptionsBuilder { + typedef MulOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { + fbb_.AddElement(MulOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); + } + explicit MulOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateMulOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE) { + MulOptionsBuilder builder_(_fbb); + builder_.add_fused_activation_function(fused_activation_function); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateMulOptions(::flatbuffers::FlatBufferBuilder &_fbb, const MulOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct L2NormOptionsT : public ::flatbuffers::NativeTable { + typedef L2NormOptions TableType; + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE; +}; + +struct L2NormOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef L2NormOptionsT NativeTableType; + typedef L2NormOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_FUSED_ACTIVATION_FUNCTION = 4 + }; + tflite::ActivationFunctionType fused_activation_function() const { + return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION, 1) && + verifier.EndTable(); + } + L2NormOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(L2NormOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const L2NormOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct L2NormOptionsBuilder { + typedef L2NormOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { + fbb_.AddElement(L2NormOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); + } + explicit L2NormOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateL2NormOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE) { + L2NormOptionsBuilder builder_(_fbb); + builder_.add_fused_activation_function(fused_activation_function); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateL2NormOptions(::flatbuffers::FlatBufferBuilder &_fbb, const L2NormOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct LocalResponseNormalizationOptionsT : public ::flatbuffers::NativeTable { + typedef LocalResponseNormalizationOptions TableType; + int32_t radius = 0; + float bias = 0.0f; + float alpha = 0.0f; + float beta = 0.0f; +}; + +struct LocalResponseNormalizationOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef LocalResponseNormalizationOptionsT NativeTableType; + typedef LocalResponseNormalizationOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_RADIUS = 4, + VT_BIAS = 6, + VT_ALPHA = 8, + VT_BETA = 10 + }; + int32_t radius() const { + return GetField(VT_RADIUS, 0); + } + float bias() const { + return GetField(VT_BIAS, 0.0f); + } + float alpha() const { + return GetField(VT_ALPHA, 0.0f); + } + float beta() const { + return GetField(VT_BETA, 0.0f); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_RADIUS, 4) && + VerifyField(verifier, VT_BIAS, 4) && + VerifyField(verifier, VT_ALPHA, 4) && + VerifyField(verifier, VT_BETA, 4) && + verifier.EndTable(); + } + LocalResponseNormalizationOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(LocalResponseNormalizationOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const LocalResponseNormalizationOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct LocalResponseNormalizationOptionsBuilder { + typedef LocalResponseNormalizationOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_radius(int32_t radius) { + fbb_.AddElement(LocalResponseNormalizationOptions::VT_RADIUS, radius, 0); + } + void add_bias(float bias) { + fbb_.AddElement(LocalResponseNormalizationOptions::VT_BIAS, bias, 0.0f); + } + void add_alpha(float alpha) { + fbb_.AddElement(LocalResponseNormalizationOptions::VT_ALPHA, alpha, 0.0f); + } + void add_beta(float beta) { + fbb_.AddElement(LocalResponseNormalizationOptions::VT_BETA, beta, 0.0f); + } + explicit LocalResponseNormalizationOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateLocalResponseNormalizationOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + int32_t radius = 0, + float bias = 0.0f, + float alpha = 0.0f, + float beta = 0.0f) { + LocalResponseNormalizationOptionsBuilder builder_(_fbb); + builder_.add_beta(beta); + builder_.add_alpha(alpha); + builder_.add_bias(bias); + builder_.add_radius(radius); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateLocalResponseNormalizationOptions(::flatbuffers::FlatBufferBuilder &_fbb, const LocalResponseNormalizationOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct LSTMOptionsT : public ::flatbuffers::NativeTable { + typedef LSTMOptions TableType; + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE; + float cell_clip = 0.0f; + float proj_clip = 0.0f; + tflite::LSTMKernelType kernel_type = tflite::LSTMKernelType_FULL; + bool asymmetric_quantize_inputs = false; +}; + +struct LSTMOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef LSTMOptionsT NativeTableType; + typedef LSTMOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_FUSED_ACTIVATION_FUNCTION = 4, + VT_CELL_CLIP = 6, + VT_PROJ_CLIP = 8, + VT_KERNEL_TYPE = 10, + VT_ASYMMETRIC_QUANTIZE_INPUTS = 12 + }; + tflite::ActivationFunctionType fused_activation_function() const { + return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); + } + float cell_clip() const { + return GetField(VT_CELL_CLIP, 0.0f); + } + float proj_clip() const { + return GetField(VT_PROJ_CLIP, 0.0f); + } + tflite::LSTMKernelType kernel_type() const { + return static_cast(GetField(VT_KERNEL_TYPE, 0)); + } + bool asymmetric_quantize_inputs() const { + return GetField(VT_ASYMMETRIC_QUANTIZE_INPUTS, 0) != 0; + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION, 1) && + VerifyField(verifier, VT_CELL_CLIP, 4) && + VerifyField(verifier, VT_PROJ_CLIP, 4) && + VerifyField(verifier, VT_KERNEL_TYPE, 1) && + VerifyField(verifier, VT_ASYMMETRIC_QUANTIZE_INPUTS, 1) && + verifier.EndTable(); + } + LSTMOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(LSTMOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const LSTMOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct LSTMOptionsBuilder { + typedef LSTMOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { + fbb_.AddElement(LSTMOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); + } + void add_cell_clip(float cell_clip) { + fbb_.AddElement(LSTMOptions::VT_CELL_CLIP, cell_clip, 0.0f); + } + void add_proj_clip(float proj_clip) { + fbb_.AddElement(LSTMOptions::VT_PROJ_CLIP, proj_clip, 0.0f); + } + void add_kernel_type(tflite::LSTMKernelType kernel_type) { + fbb_.AddElement(LSTMOptions::VT_KERNEL_TYPE, static_cast(kernel_type), 0); + } + void add_asymmetric_quantize_inputs(bool asymmetric_quantize_inputs) { + fbb_.AddElement(LSTMOptions::VT_ASYMMETRIC_QUANTIZE_INPUTS, static_cast(asymmetric_quantize_inputs), 0); + } + explicit LSTMOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateLSTMOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE, + float cell_clip = 0.0f, + float proj_clip = 0.0f, + tflite::LSTMKernelType kernel_type = tflite::LSTMKernelType_FULL, + bool asymmetric_quantize_inputs = false) { + LSTMOptionsBuilder builder_(_fbb); + builder_.add_proj_clip(proj_clip); + builder_.add_cell_clip(cell_clip); + builder_.add_asymmetric_quantize_inputs(asymmetric_quantize_inputs); + builder_.add_kernel_type(kernel_type); + builder_.add_fused_activation_function(fused_activation_function); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateLSTMOptions(::flatbuffers::FlatBufferBuilder &_fbb, const LSTMOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct UnidirectionalSequenceLSTMOptionsT : public ::flatbuffers::NativeTable { + typedef UnidirectionalSequenceLSTMOptions TableType; + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE; + float cell_clip = 0.0f; + float proj_clip = 0.0f; + bool time_major = false; + bool asymmetric_quantize_inputs = false; + bool diagonal_recurrent_tensors = false; +}; + +struct UnidirectionalSequenceLSTMOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef UnidirectionalSequenceLSTMOptionsT NativeTableType; + typedef UnidirectionalSequenceLSTMOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_FUSED_ACTIVATION_FUNCTION = 4, + VT_CELL_CLIP = 6, + VT_PROJ_CLIP = 8, + VT_TIME_MAJOR = 10, + VT_ASYMMETRIC_QUANTIZE_INPUTS = 12, + VT_DIAGONAL_RECURRENT_TENSORS = 14 + }; + tflite::ActivationFunctionType fused_activation_function() const { + return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); + } + float cell_clip() const { + return GetField(VT_CELL_CLIP, 0.0f); + } + float proj_clip() const { + return GetField(VT_PROJ_CLIP, 0.0f); + } + bool time_major() const { + return GetField(VT_TIME_MAJOR, 0) != 0; + } + bool asymmetric_quantize_inputs() const { + return GetField(VT_ASYMMETRIC_QUANTIZE_INPUTS, 0) != 0; + } + bool diagonal_recurrent_tensors() const { + return GetField(VT_DIAGONAL_RECURRENT_TENSORS, 0) != 0; + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION, 1) && + VerifyField(verifier, VT_CELL_CLIP, 4) && + VerifyField(verifier, VT_PROJ_CLIP, 4) && + VerifyField(verifier, VT_TIME_MAJOR, 1) && + VerifyField(verifier, VT_ASYMMETRIC_QUANTIZE_INPUTS, 1) && + VerifyField(verifier, VT_DIAGONAL_RECURRENT_TENSORS, 1) && + verifier.EndTable(); + } + UnidirectionalSequenceLSTMOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(UnidirectionalSequenceLSTMOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const UnidirectionalSequenceLSTMOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct UnidirectionalSequenceLSTMOptionsBuilder { + typedef UnidirectionalSequenceLSTMOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { + fbb_.AddElement(UnidirectionalSequenceLSTMOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); + } + void add_cell_clip(float cell_clip) { + fbb_.AddElement(UnidirectionalSequenceLSTMOptions::VT_CELL_CLIP, cell_clip, 0.0f); + } + void add_proj_clip(float proj_clip) { + fbb_.AddElement(UnidirectionalSequenceLSTMOptions::VT_PROJ_CLIP, proj_clip, 0.0f); + } + void add_time_major(bool time_major) { + fbb_.AddElement(UnidirectionalSequenceLSTMOptions::VT_TIME_MAJOR, static_cast(time_major), 0); + } + void add_asymmetric_quantize_inputs(bool asymmetric_quantize_inputs) { + fbb_.AddElement(UnidirectionalSequenceLSTMOptions::VT_ASYMMETRIC_QUANTIZE_INPUTS, static_cast(asymmetric_quantize_inputs), 0); + } + void add_diagonal_recurrent_tensors(bool diagonal_recurrent_tensors) { + fbb_.AddElement(UnidirectionalSequenceLSTMOptions::VT_DIAGONAL_RECURRENT_TENSORS, static_cast(diagonal_recurrent_tensors), 0); + } + explicit UnidirectionalSequenceLSTMOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateUnidirectionalSequenceLSTMOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE, + float cell_clip = 0.0f, + float proj_clip = 0.0f, + bool time_major = false, + bool asymmetric_quantize_inputs = false, + bool diagonal_recurrent_tensors = false) { + UnidirectionalSequenceLSTMOptionsBuilder builder_(_fbb); + builder_.add_proj_clip(proj_clip); + builder_.add_cell_clip(cell_clip); + builder_.add_diagonal_recurrent_tensors(diagonal_recurrent_tensors); + builder_.add_asymmetric_quantize_inputs(asymmetric_quantize_inputs); + builder_.add_time_major(time_major); + builder_.add_fused_activation_function(fused_activation_function); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateUnidirectionalSequenceLSTMOptions(::flatbuffers::FlatBufferBuilder &_fbb, const UnidirectionalSequenceLSTMOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct BidirectionalSequenceLSTMOptionsT : public ::flatbuffers::NativeTable { + typedef BidirectionalSequenceLSTMOptions TableType; + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE; + float cell_clip = 0.0f; + float proj_clip = 0.0f; + bool merge_outputs = false; + bool time_major = true; + bool asymmetric_quantize_inputs = false; +}; + +struct BidirectionalSequenceLSTMOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef BidirectionalSequenceLSTMOptionsT NativeTableType; + typedef BidirectionalSequenceLSTMOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_FUSED_ACTIVATION_FUNCTION = 4, + VT_CELL_CLIP = 6, + VT_PROJ_CLIP = 8, + VT_MERGE_OUTPUTS = 10, + VT_TIME_MAJOR = 12, + VT_ASYMMETRIC_QUANTIZE_INPUTS = 14 + }; + tflite::ActivationFunctionType fused_activation_function() const { + return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); + } + float cell_clip() const { + return GetField(VT_CELL_CLIP, 0.0f); + } + float proj_clip() const { + return GetField(VT_PROJ_CLIP, 0.0f); + } + bool merge_outputs() const { + return GetField(VT_MERGE_OUTPUTS, 0) != 0; + } + bool time_major() const { + return GetField(VT_TIME_MAJOR, 1) != 0; + } + bool asymmetric_quantize_inputs() const { + return GetField(VT_ASYMMETRIC_QUANTIZE_INPUTS, 0) != 0; + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION, 1) && + VerifyField(verifier, VT_CELL_CLIP, 4) && + VerifyField(verifier, VT_PROJ_CLIP, 4) && + VerifyField(verifier, VT_MERGE_OUTPUTS, 1) && + VerifyField(verifier, VT_TIME_MAJOR, 1) && + VerifyField(verifier, VT_ASYMMETRIC_QUANTIZE_INPUTS, 1) && + verifier.EndTable(); + } + BidirectionalSequenceLSTMOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(BidirectionalSequenceLSTMOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const BidirectionalSequenceLSTMOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct BidirectionalSequenceLSTMOptionsBuilder { + typedef BidirectionalSequenceLSTMOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { + fbb_.AddElement(BidirectionalSequenceLSTMOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); + } + void add_cell_clip(float cell_clip) { + fbb_.AddElement(BidirectionalSequenceLSTMOptions::VT_CELL_CLIP, cell_clip, 0.0f); + } + void add_proj_clip(float proj_clip) { + fbb_.AddElement(BidirectionalSequenceLSTMOptions::VT_PROJ_CLIP, proj_clip, 0.0f); + } + void add_merge_outputs(bool merge_outputs) { + fbb_.AddElement(BidirectionalSequenceLSTMOptions::VT_MERGE_OUTPUTS, static_cast(merge_outputs), 0); + } + void add_time_major(bool time_major) { + fbb_.AddElement(BidirectionalSequenceLSTMOptions::VT_TIME_MAJOR, static_cast(time_major), 1); + } + void add_asymmetric_quantize_inputs(bool asymmetric_quantize_inputs) { + fbb_.AddElement(BidirectionalSequenceLSTMOptions::VT_ASYMMETRIC_QUANTIZE_INPUTS, static_cast(asymmetric_quantize_inputs), 0); + } + explicit BidirectionalSequenceLSTMOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateBidirectionalSequenceLSTMOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE, + float cell_clip = 0.0f, + float proj_clip = 0.0f, + bool merge_outputs = false, + bool time_major = true, + bool asymmetric_quantize_inputs = false) { + BidirectionalSequenceLSTMOptionsBuilder builder_(_fbb); + builder_.add_proj_clip(proj_clip); + builder_.add_cell_clip(cell_clip); + builder_.add_asymmetric_quantize_inputs(asymmetric_quantize_inputs); + builder_.add_time_major(time_major); + builder_.add_merge_outputs(merge_outputs); + builder_.add_fused_activation_function(fused_activation_function); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateBidirectionalSequenceLSTMOptions(::flatbuffers::FlatBufferBuilder &_fbb, const BidirectionalSequenceLSTMOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct ResizeBilinearOptionsT : public ::flatbuffers::NativeTable { + typedef ResizeBilinearOptions TableType; + bool align_corners = false; + bool half_pixel_centers = false; +}; + +struct ResizeBilinearOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef ResizeBilinearOptionsT NativeTableType; + typedef ResizeBilinearOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_ALIGN_CORNERS = 8, + VT_HALF_PIXEL_CENTERS = 10 + }; + bool align_corners() const { + return GetField(VT_ALIGN_CORNERS, 0) != 0; + } + bool half_pixel_centers() const { + return GetField(VT_HALF_PIXEL_CENTERS, 0) != 0; + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_ALIGN_CORNERS, 1) && + VerifyField(verifier, VT_HALF_PIXEL_CENTERS, 1) && + verifier.EndTable(); + } + ResizeBilinearOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(ResizeBilinearOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ResizeBilinearOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct ResizeBilinearOptionsBuilder { + typedef ResizeBilinearOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_align_corners(bool align_corners) { + fbb_.AddElement(ResizeBilinearOptions::VT_ALIGN_CORNERS, static_cast(align_corners), 0); + } + void add_half_pixel_centers(bool half_pixel_centers) { + fbb_.AddElement(ResizeBilinearOptions::VT_HALF_PIXEL_CENTERS, static_cast(half_pixel_centers), 0); + } + explicit ResizeBilinearOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateResizeBilinearOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + bool align_corners = false, + bool half_pixel_centers = false) { + ResizeBilinearOptionsBuilder builder_(_fbb); + builder_.add_half_pixel_centers(half_pixel_centers); + builder_.add_align_corners(align_corners); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateResizeBilinearOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ResizeBilinearOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct ResizeNearestNeighborOptionsT : public ::flatbuffers::NativeTable { + typedef ResizeNearestNeighborOptions TableType; + bool align_corners = false; + bool half_pixel_centers = false; +}; + +struct ResizeNearestNeighborOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef ResizeNearestNeighborOptionsT NativeTableType; + typedef ResizeNearestNeighborOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_ALIGN_CORNERS = 4, + VT_HALF_PIXEL_CENTERS = 6 + }; + bool align_corners() const { + return GetField(VT_ALIGN_CORNERS, 0) != 0; + } + bool half_pixel_centers() const { + return GetField(VT_HALF_PIXEL_CENTERS, 0) != 0; + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_ALIGN_CORNERS, 1) && + VerifyField(verifier, VT_HALF_PIXEL_CENTERS, 1) && + verifier.EndTable(); + } + ResizeNearestNeighborOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(ResizeNearestNeighborOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ResizeNearestNeighborOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct ResizeNearestNeighborOptionsBuilder { + typedef ResizeNearestNeighborOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_align_corners(bool align_corners) { + fbb_.AddElement(ResizeNearestNeighborOptions::VT_ALIGN_CORNERS, static_cast(align_corners), 0); + } + void add_half_pixel_centers(bool half_pixel_centers) { + fbb_.AddElement(ResizeNearestNeighborOptions::VT_HALF_PIXEL_CENTERS, static_cast(half_pixel_centers), 0); + } + explicit ResizeNearestNeighborOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateResizeNearestNeighborOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + bool align_corners = false, + bool half_pixel_centers = false) { + ResizeNearestNeighborOptionsBuilder builder_(_fbb); + builder_.add_half_pixel_centers(half_pixel_centers); + builder_.add_align_corners(align_corners); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateResizeNearestNeighborOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ResizeNearestNeighborOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct CallOptionsT : public ::flatbuffers::NativeTable { + typedef CallOptions TableType; + uint32_t subgraph = 0; +}; + +struct CallOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef CallOptionsT NativeTableType; + typedef CallOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_SUBGRAPH = 4 + }; + uint32_t subgraph() const { + return GetField(VT_SUBGRAPH, 0); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_SUBGRAPH, 4) && + verifier.EndTable(); + } + CallOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(CallOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const CallOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct CallOptionsBuilder { + typedef CallOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_subgraph(uint32_t subgraph) { + fbb_.AddElement(CallOptions::VT_SUBGRAPH, subgraph, 0); + } + explicit CallOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateCallOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + uint32_t subgraph = 0) { + CallOptionsBuilder builder_(_fbb); + builder_.add_subgraph(subgraph); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateCallOptions(::flatbuffers::FlatBufferBuilder &_fbb, const CallOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct PadOptionsT : public ::flatbuffers::NativeTable { + typedef PadOptions TableType; +}; + +struct PadOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef PadOptionsT NativeTableType; + typedef PadOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + PadOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(PadOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const PadOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct PadOptionsBuilder { + typedef PadOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit PadOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreatePadOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + PadOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreatePadOptions(::flatbuffers::FlatBufferBuilder &_fbb, const PadOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct PadV2OptionsT : public ::flatbuffers::NativeTable { + typedef PadV2Options TableType; +}; + +struct PadV2Options FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef PadV2OptionsT NativeTableType; + typedef PadV2OptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + PadV2OptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(PadV2OptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const PadV2OptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct PadV2OptionsBuilder { + typedef PadV2Options Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit PadV2OptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreatePadV2Options( + ::flatbuffers::FlatBufferBuilder &_fbb) { + PadV2OptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreatePadV2Options(::flatbuffers::FlatBufferBuilder &_fbb, const PadV2OptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct ReshapeOptionsT : public ::flatbuffers::NativeTable { + typedef ReshapeOptions TableType; + std::vector new_shape{}; +}; + +struct ReshapeOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef ReshapeOptionsT NativeTableType; + typedef ReshapeOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_NEW_SHAPE = 4 + }; + const ::flatbuffers::Vector *new_shape() const { + return GetPointer *>(VT_NEW_SHAPE); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_NEW_SHAPE) && + verifier.VerifyVector(new_shape()) && + verifier.EndTable(); + } + ReshapeOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(ReshapeOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ReshapeOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct ReshapeOptionsBuilder { + typedef ReshapeOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_new_shape(::flatbuffers::Offset<::flatbuffers::Vector> new_shape) { + fbb_.AddOffset(ReshapeOptions::VT_NEW_SHAPE, new_shape); + } + explicit ReshapeOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateReshapeOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::Vector> new_shape = 0) { + ReshapeOptionsBuilder builder_(_fbb); + builder_.add_new_shape(new_shape); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateReshapeOptionsDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const std::vector *new_shape = nullptr) { + auto new_shape__ = new_shape ? _fbb.CreateVector(*new_shape) : 0; + return tflite::CreateReshapeOptions( + _fbb, + new_shape__); +} + +::flatbuffers::Offset CreateReshapeOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ReshapeOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct SpaceToBatchNDOptionsT : public ::flatbuffers::NativeTable { + typedef SpaceToBatchNDOptions TableType; +}; + +struct SpaceToBatchNDOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef SpaceToBatchNDOptionsT NativeTableType; + typedef SpaceToBatchNDOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + SpaceToBatchNDOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(SpaceToBatchNDOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SpaceToBatchNDOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct SpaceToBatchNDOptionsBuilder { + typedef SpaceToBatchNDOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit SpaceToBatchNDOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateSpaceToBatchNDOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + SpaceToBatchNDOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateSpaceToBatchNDOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SpaceToBatchNDOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct BatchToSpaceNDOptionsT : public ::flatbuffers::NativeTable { + typedef BatchToSpaceNDOptions TableType; +}; + +struct BatchToSpaceNDOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef BatchToSpaceNDOptionsT NativeTableType; + typedef BatchToSpaceNDOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + BatchToSpaceNDOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(BatchToSpaceNDOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const BatchToSpaceNDOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct BatchToSpaceNDOptionsBuilder { + typedef BatchToSpaceNDOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit BatchToSpaceNDOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateBatchToSpaceNDOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + BatchToSpaceNDOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateBatchToSpaceNDOptions(::flatbuffers::FlatBufferBuilder &_fbb, const BatchToSpaceNDOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct SkipGramOptionsT : public ::flatbuffers::NativeTable { + typedef SkipGramOptions TableType; + int32_t ngram_size = 0; + int32_t max_skip_size = 0; + bool include_all_ngrams = false; +}; + +struct SkipGramOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef SkipGramOptionsT NativeTableType; + typedef SkipGramOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_NGRAM_SIZE = 4, + VT_MAX_SKIP_SIZE = 6, + VT_INCLUDE_ALL_NGRAMS = 8 + }; + int32_t ngram_size() const { + return GetField(VT_NGRAM_SIZE, 0); + } + int32_t max_skip_size() const { + return GetField(VT_MAX_SKIP_SIZE, 0); + } + bool include_all_ngrams() const { + return GetField(VT_INCLUDE_ALL_NGRAMS, 0) != 0; + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_NGRAM_SIZE, 4) && + VerifyField(verifier, VT_MAX_SKIP_SIZE, 4) && + VerifyField(verifier, VT_INCLUDE_ALL_NGRAMS, 1) && + verifier.EndTable(); + } + SkipGramOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(SkipGramOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SkipGramOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct SkipGramOptionsBuilder { + typedef SkipGramOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_ngram_size(int32_t ngram_size) { + fbb_.AddElement(SkipGramOptions::VT_NGRAM_SIZE, ngram_size, 0); + } + void add_max_skip_size(int32_t max_skip_size) { + fbb_.AddElement(SkipGramOptions::VT_MAX_SKIP_SIZE, max_skip_size, 0); + } + void add_include_all_ngrams(bool include_all_ngrams) { + fbb_.AddElement(SkipGramOptions::VT_INCLUDE_ALL_NGRAMS, static_cast(include_all_ngrams), 0); + } + explicit SkipGramOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateSkipGramOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + int32_t ngram_size = 0, + int32_t max_skip_size = 0, + bool include_all_ngrams = false) { + SkipGramOptionsBuilder builder_(_fbb); + builder_.add_max_skip_size(max_skip_size); + builder_.add_ngram_size(ngram_size); + builder_.add_include_all_ngrams(include_all_ngrams); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateSkipGramOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SkipGramOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct SpaceToDepthOptionsT : public ::flatbuffers::NativeTable { + typedef SpaceToDepthOptions TableType; + int32_t block_size = 0; +}; + +struct SpaceToDepthOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef SpaceToDepthOptionsT NativeTableType; + typedef SpaceToDepthOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_BLOCK_SIZE = 4 + }; + int32_t block_size() const { + return GetField(VT_BLOCK_SIZE, 0); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_BLOCK_SIZE, 4) && + verifier.EndTable(); + } + SpaceToDepthOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(SpaceToDepthOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SpaceToDepthOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct SpaceToDepthOptionsBuilder { + typedef SpaceToDepthOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_block_size(int32_t block_size) { + fbb_.AddElement(SpaceToDepthOptions::VT_BLOCK_SIZE, block_size, 0); + } + explicit SpaceToDepthOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateSpaceToDepthOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + int32_t block_size = 0) { + SpaceToDepthOptionsBuilder builder_(_fbb); + builder_.add_block_size(block_size); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateSpaceToDepthOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SpaceToDepthOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct DepthToSpaceOptionsT : public ::flatbuffers::NativeTable { + typedef DepthToSpaceOptions TableType; + int32_t block_size = 0; +}; + +struct DepthToSpaceOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef DepthToSpaceOptionsT NativeTableType; + typedef DepthToSpaceOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_BLOCK_SIZE = 4 + }; + int32_t block_size() const { + return GetField(VT_BLOCK_SIZE, 0); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_BLOCK_SIZE, 4) && + verifier.EndTable(); + } + DepthToSpaceOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(DepthToSpaceOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const DepthToSpaceOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct DepthToSpaceOptionsBuilder { + typedef DepthToSpaceOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_block_size(int32_t block_size) { + fbb_.AddElement(DepthToSpaceOptions::VT_BLOCK_SIZE, block_size, 0); + } + explicit DepthToSpaceOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateDepthToSpaceOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + int32_t block_size = 0) { + DepthToSpaceOptionsBuilder builder_(_fbb); + builder_.add_block_size(block_size); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateDepthToSpaceOptions(::flatbuffers::FlatBufferBuilder &_fbb, const DepthToSpaceOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct SubOptionsT : public ::flatbuffers::NativeTable { + typedef SubOptions TableType; + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE; + bool pot_scale_int16 = true; +}; + +struct SubOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef SubOptionsT NativeTableType; + typedef SubOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_FUSED_ACTIVATION_FUNCTION = 4, + VT_POT_SCALE_INT16 = 6 + }; + tflite::ActivationFunctionType fused_activation_function() const { + return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); + } + bool pot_scale_int16() const { + return GetField(VT_POT_SCALE_INT16, 1) != 0; + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION, 1) && + VerifyField(verifier, VT_POT_SCALE_INT16, 1) && + verifier.EndTable(); + } + SubOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(SubOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SubOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct SubOptionsBuilder { + typedef SubOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { + fbb_.AddElement(SubOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); + } + void add_pot_scale_int16(bool pot_scale_int16) { + fbb_.AddElement(SubOptions::VT_POT_SCALE_INT16, static_cast(pot_scale_int16), 1); + } + explicit SubOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateSubOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE, + bool pot_scale_int16 = true) { + SubOptionsBuilder builder_(_fbb); + builder_.add_pot_scale_int16(pot_scale_int16); + builder_.add_fused_activation_function(fused_activation_function); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateSubOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SubOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct DivOptionsT : public ::flatbuffers::NativeTable { + typedef DivOptions TableType; + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE; +}; + +struct DivOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef DivOptionsT NativeTableType; + typedef DivOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_FUSED_ACTIVATION_FUNCTION = 4 + }; + tflite::ActivationFunctionType fused_activation_function() const { + return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION, 1) && + verifier.EndTable(); + } + DivOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(DivOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const DivOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct DivOptionsBuilder { + typedef DivOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { + fbb_.AddElement(DivOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); + } + explicit DivOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateDivOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE) { + DivOptionsBuilder builder_(_fbb); + builder_.add_fused_activation_function(fused_activation_function); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateDivOptions(::flatbuffers::FlatBufferBuilder &_fbb, const DivOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct TopKV2OptionsT : public ::flatbuffers::NativeTable { + typedef TopKV2Options TableType; +}; + +struct TopKV2Options FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef TopKV2OptionsT NativeTableType; + typedef TopKV2OptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + TopKV2OptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(TopKV2OptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const TopKV2OptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct TopKV2OptionsBuilder { + typedef TopKV2Options Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit TopKV2OptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateTopKV2Options( + ::flatbuffers::FlatBufferBuilder &_fbb) { + TopKV2OptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateTopKV2Options(::flatbuffers::FlatBufferBuilder &_fbb, const TopKV2OptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct EmbeddingLookupSparseOptionsT : public ::flatbuffers::NativeTable { + typedef EmbeddingLookupSparseOptions TableType; + tflite::CombinerType combiner = tflite::CombinerType_SUM; +}; + +struct EmbeddingLookupSparseOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef EmbeddingLookupSparseOptionsT NativeTableType; + typedef EmbeddingLookupSparseOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_COMBINER = 4 + }; + tflite::CombinerType combiner() const { + return static_cast(GetField(VT_COMBINER, 0)); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_COMBINER, 1) && + verifier.EndTable(); + } + EmbeddingLookupSparseOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(EmbeddingLookupSparseOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const EmbeddingLookupSparseOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct EmbeddingLookupSparseOptionsBuilder { + typedef EmbeddingLookupSparseOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_combiner(tflite::CombinerType combiner) { + fbb_.AddElement(EmbeddingLookupSparseOptions::VT_COMBINER, static_cast(combiner), 0); + } + explicit EmbeddingLookupSparseOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateEmbeddingLookupSparseOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + tflite::CombinerType combiner = tflite::CombinerType_SUM) { + EmbeddingLookupSparseOptionsBuilder builder_(_fbb); + builder_.add_combiner(combiner); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateEmbeddingLookupSparseOptions(::flatbuffers::FlatBufferBuilder &_fbb, const EmbeddingLookupSparseOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct GatherOptionsT : public ::flatbuffers::NativeTable { + typedef GatherOptions TableType; + int32_t axis = 0; + int32_t batch_dims = 0; +}; + +struct GatherOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef GatherOptionsT NativeTableType; + typedef GatherOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_AXIS = 4, + VT_BATCH_DIMS = 6 + }; + int32_t axis() const { + return GetField(VT_AXIS, 0); + } + int32_t batch_dims() const { + return GetField(VT_BATCH_DIMS, 0); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_AXIS, 4) && + VerifyField(verifier, VT_BATCH_DIMS, 4) && + verifier.EndTable(); + } + GatherOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(GatherOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const GatherOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct GatherOptionsBuilder { + typedef GatherOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_axis(int32_t axis) { + fbb_.AddElement(GatherOptions::VT_AXIS, axis, 0); + } + void add_batch_dims(int32_t batch_dims) { + fbb_.AddElement(GatherOptions::VT_BATCH_DIMS, batch_dims, 0); + } + explicit GatherOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateGatherOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + int32_t axis = 0, + int32_t batch_dims = 0) { + GatherOptionsBuilder builder_(_fbb); + builder_.add_batch_dims(batch_dims); + builder_.add_axis(axis); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateGatherOptions(::flatbuffers::FlatBufferBuilder &_fbb, const GatherOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct TransposeOptionsT : public ::flatbuffers::NativeTable { + typedef TransposeOptions TableType; +}; + +struct TransposeOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef TransposeOptionsT NativeTableType; + typedef TransposeOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + TransposeOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(TransposeOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const TransposeOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct TransposeOptionsBuilder { + typedef TransposeOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit TransposeOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateTransposeOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + TransposeOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateTransposeOptions(::flatbuffers::FlatBufferBuilder &_fbb, const TransposeOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct ExpOptionsT : public ::flatbuffers::NativeTable { + typedef ExpOptions TableType; +}; + +struct ExpOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef ExpOptionsT NativeTableType; + typedef ExpOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + ExpOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(ExpOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ExpOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct ExpOptionsBuilder { + typedef ExpOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit ExpOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateExpOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + ExpOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateExpOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ExpOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct CosOptionsT : public ::flatbuffers::NativeTable { + typedef CosOptions TableType; +}; + +struct CosOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef CosOptionsT NativeTableType; + typedef CosOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + CosOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(CosOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const CosOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct CosOptionsBuilder { + typedef CosOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit CosOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateCosOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + CosOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateCosOptions(::flatbuffers::FlatBufferBuilder &_fbb, const CosOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct ReducerOptionsT : public ::flatbuffers::NativeTable { + typedef ReducerOptions TableType; + bool keep_dims = false; +}; + +struct ReducerOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef ReducerOptionsT NativeTableType; + typedef ReducerOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_KEEP_DIMS = 4 + }; + bool keep_dims() const { + return GetField(VT_KEEP_DIMS, 0) != 0; + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_KEEP_DIMS, 1) && + verifier.EndTable(); + } + ReducerOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(ReducerOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ReducerOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct ReducerOptionsBuilder { + typedef ReducerOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_keep_dims(bool keep_dims) { + fbb_.AddElement(ReducerOptions::VT_KEEP_DIMS, static_cast(keep_dims), 0); + } + explicit ReducerOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateReducerOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + bool keep_dims = false) { + ReducerOptionsBuilder builder_(_fbb); + builder_.add_keep_dims(keep_dims); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateReducerOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ReducerOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct SqueezeOptionsT : public ::flatbuffers::NativeTable { + typedef SqueezeOptions TableType; + std::vector squeeze_dims{}; +}; + +struct SqueezeOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef SqueezeOptionsT NativeTableType; + typedef SqueezeOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_SQUEEZE_DIMS = 4 + }; + const ::flatbuffers::Vector *squeeze_dims() const { + return GetPointer *>(VT_SQUEEZE_DIMS); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_SQUEEZE_DIMS) && + verifier.VerifyVector(squeeze_dims()) && + verifier.EndTable(); + } + SqueezeOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(SqueezeOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SqueezeOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct SqueezeOptionsBuilder { + typedef SqueezeOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_squeeze_dims(::flatbuffers::Offset<::flatbuffers::Vector> squeeze_dims) { + fbb_.AddOffset(SqueezeOptions::VT_SQUEEZE_DIMS, squeeze_dims); + } + explicit SqueezeOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateSqueezeOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::Vector> squeeze_dims = 0) { + SqueezeOptionsBuilder builder_(_fbb); + builder_.add_squeeze_dims(squeeze_dims); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateSqueezeOptionsDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const std::vector *squeeze_dims = nullptr) { + auto squeeze_dims__ = squeeze_dims ? _fbb.CreateVector(*squeeze_dims) : 0; + return tflite::CreateSqueezeOptions( + _fbb, + squeeze_dims__); +} + +::flatbuffers::Offset CreateSqueezeOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SqueezeOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct SplitOptionsT : public ::flatbuffers::NativeTable { + typedef SplitOptions TableType; + int32_t num_splits = 0; +}; + +struct SplitOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef SplitOptionsT NativeTableType; + typedef SplitOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_NUM_SPLITS = 4 + }; + int32_t num_splits() const { + return GetField(VT_NUM_SPLITS, 0); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_NUM_SPLITS, 4) && + verifier.EndTable(); + } + SplitOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(SplitOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SplitOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct SplitOptionsBuilder { + typedef SplitOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_num_splits(int32_t num_splits) { + fbb_.AddElement(SplitOptions::VT_NUM_SPLITS, num_splits, 0); + } + explicit SplitOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateSplitOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + int32_t num_splits = 0) { + SplitOptionsBuilder builder_(_fbb); + builder_.add_num_splits(num_splits); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateSplitOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SplitOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct SplitVOptionsT : public ::flatbuffers::NativeTable { + typedef SplitVOptions TableType; + int32_t num_splits = 0; +}; + +struct SplitVOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef SplitVOptionsT NativeTableType; + typedef SplitVOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_NUM_SPLITS = 4 + }; + int32_t num_splits() const { + return GetField(VT_NUM_SPLITS, 0); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_NUM_SPLITS, 4) && + verifier.EndTable(); + } + SplitVOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(SplitVOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SplitVOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct SplitVOptionsBuilder { + typedef SplitVOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_num_splits(int32_t num_splits) { + fbb_.AddElement(SplitVOptions::VT_NUM_SPLITS, num_splits, 0); + } + explicit SplitVOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateSplitVOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + int32_t num_splits = 0) { + SplitVOptionsBuilder builder_(_fbb); + builder_.add_num_splits(num_splits); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateSplitVOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SplitVOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct StridedSliceOptionsT : public ::flatbuffers::NativeTable { + typedef StridedSliceOptions TableType; + int32_t begin_mask = 0; + int32_t end_mask = 0; + int32_t ellipsis_mask = 0; + int32_t new_axis_mask = 0; + int32_t shrink_axis_mask = 0; + bool offset = false; +}; + +struct StridedSliceOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef StridedSliceOptionsT NativeTableType; + typedef StridedSliceOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_BEGIN_MASK = 4, + VT_END_MASK = 6, + VT_ELLIPSIS_MASK = 8, + VT_NEW_AXIS_MASK = 10, + VT_SHRINK_AXIS_MASK = 12, + VT_OFFSET = 14 + }; + int32_t begin_mask() const { + return GetField(VT_BEGIN_MASK, 0); + } + int32_t end_mask() const { + return GetField(VT_END_MASK, 0); + } + int32_t ellipsis_mask() const { + return GetField(VT_ELLIPSIS_MASK, 0); + } + int32_t new_axis_mask() const { + return GetField(VT_NEW_AXIS_MASK, 0); + } + int32_t shrink_axis_mask() const { + return GetField(VT_SHRINK_AXIS_MASK, 0); + } + bool offset() const { + return GetField(VT_OFFSET, 0) != 0; + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_BEGIN_MASK, 4) && + VerifyField(verifier, VT_END_MASK, 4) && + VerifyField(verifier, VT_ELLIPSIS_MASK, 4) && + VerifyField(verifier, VT_NEW_AXIS_MASK, 4) && + VerifyField(verifier, VT_SHRINK_AXIS_MASK, 4) && + VerifyField(verifier, VT_OFFSET, 1) && + verifier.EndTable(); + } + StridedSliceOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(StridedSliceOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StridedSliceOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct StridedSliceOptionsBuilder { + typedef StridedSliceOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_begin_mask(int32_t begin_mask) { + fbb_.AddElement(StridedSliceOptions::VT_BEGIN_MASK, begin_mask, 0); + } + void add_end_mask(int32_t end_mask) { + fbb_.AddElement(StridedSliceOptions::VT_END_MASK, end_mask, 0); + } + void add_ellipsis_mask(int32_t ellipsis_mask) { + fbb_.AddElement(StridedSliceOptions::VT_ELLIPSIS_MASK, ellipsis_mask, 0); + } + void add_new_axis_mask(int32_t new_axis_mask) { + fbb_.AddElement(StridedSliceOptions::VT_NEW_AXIS_MASK, new_axis_mask, 0); + } + void add_shrink_axis_mask(int32_t shrink_axis_mask) { + fbb_.AddElement(StridedSliceOptions::VT_SHRINK_AXIS_MASK, shrink_axis_mask, 0); + } + void add_offset(bool offset) { + fbb_.AddElement(StridedSliceOptions::VT_OFFSET, static_cast(offset), 0); + } + explicit StridedSliceOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateStridedSliceOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + int32_t begin_mask = 0, + int32_t end_mask = 0, + int32_t ellipsis_mask = 0, + int32_t new_axis_mask = 0, + int32_t shrink_axis_mask = 0, + bool offset = false) { + StridedSliceOptionsBuilder builder_(_fbb); + builder_.add_shrink_axis_mask(shrink_axis_mask); + builder_.add_new_axis_mask(new_axis_mask); + builder_.add_ellipsis_mask(ellipsis_mask); + builder_.add_end_mask(end_mask); + builder_.add_begin_mask(begin_mask); + builder_.add_offset(offset); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateStridedSliceOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StridedSliceOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct LogSoftmaxOptionsT : public ::flatbuffers::NativeTable { + typedef LogSoftmaxOptions TableType; +}; + +struct LogSoftmaxOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef LogSoftmaxOptionsT NativeTableType; + typedef LogSoftmaxOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + LogSoftmaxOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(LogSoftmaxOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const LogSoftmaxOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct LogSoftmaxOptionsBuilder { + typedef LogSoftmaxOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit LogSoftmaxOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateLogSoftmaxOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + LogSoftmaxOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateLogSoftmaxOptions(::flatbuffers::FlatBufferBuilder &_fbb, const LogSoftmaxOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct CastOptionsT : public ::flatbuffers::NativeTable { + typedef CastOptions TableType; + tflite::TensorType in_data_type = tflite::TensorType_FLOAT32; + tflite::TensorType out_data_type = tflite::TensorType_FLOAT32; +}; + +struct CastOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef CastOptionsT NativeTableType; + typedef CastOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_IN_DATA_TYPE = 4, + VT_OUT_DATA_TYPE = 6 + }; + tflite::TensorType in_data_type() const { + return static_cast(GetField(VT_IN_DATA_TYPE, 0)); + } + tflite::TensorType out_data_type() const { + return static_cast(GetField(VT_OUT_DATA_TYPE, 0)); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_IN_DATA_TYPE, 1) && + VerifyField(verifier, VT_OUT_DATA_TYPE, 1) && + verifier.EndTable(); + } + CastOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(CastOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const CastOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct CastOptionsBuilder { + typedef CastOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_in_data_type(tflite::TensorType in_data_type) { + fbb_.AddElement(CastOptions::VT_IN_DATA_TYPE, static_cast(in_data_type), 0); + } + void add_out_data_type(tflite::TensorType out_data_type) { + fbb_.AddElement(CastOptions::VT_OUT_DATA_TYPE, static_cast(out_data_type), 0); + } + explicit CastOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateCastOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + tflite::TensorType in_data_type = tflite::TensorType_FLOAT32, + tflite::TensorType out_data_type = tflite::TensorType_FLOAT32) { + CastOptionsBuilder builder_(_fbb); + builder_.add_out_data_type(out_data_type); + builder_.add_in_data_type(in_data_type); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateCastOptions(::flatbuffers::FlatBufferBuilder &_fbb, const CastOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct DequantizeOptionsT : public ::flatbuffers::NativeTable { + typedef DequantizeOptions TableType; +}; + +struct DequantizeOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef DequantizeOptionsT NativeTableType; + typedef DequantizeOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + DequantizeOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(DequantizeOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const DequantizeOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct DequantizeOptionsBuilder { + typedef DequantizeOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit DequantizeOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateDequantizeOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + DequantizeOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateDequantizeOptions(::flatbuffers::FlatBufferBuilder &_fbb, const DequantizeOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct MaximumMinimumOptionsT : public ::flatbuffers::NativeTable { + typedef MaximumMinimumOptions TableType; +}; + +struct MaximumMinimumOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef MaximumMinimumOptionsT NativeTableType; + typedef MaximumMinimumOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + MaximumMinimumOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(MaximumMinimumOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const MaximumMinimumOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct MaximumMinimumOptionsBuilder { + typedef MaximumMinimumOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit MaximumMinimumOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateMaximumMinimumOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + MaximumMinimumOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateMaximumMinimumOptions(::flatbuffers::FlatBufferBuilder &_fbb, const MaximumMinimumOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct TileOptionsT : public ::flatbuffers::NativeTable { + typedef TileOptions TableType; +}; + +struct TileOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef TileOptionsT NativeTableType; + typedef TileOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + TileOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(TileOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const TileOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct TileOptionsBuilder { + typedef TileOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit TileOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateTileOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + TileOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateTileOptions(::flatbuffers::FlatBufferBuilder &_fbb, const TileOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct ArgMaxOptionsT : public ::flatbuffers::NativeTable { + typedef ArgMaxOptions TableType; + tflite::TensorType output_type = tflite::TensorType_FLOAT32; +}; + +struct ArgMaxOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef ArgMaxOptionsT NativeTableType; + typedef ArgMaxOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_OUTPUT_TYPE = 4 + }; + tflite::TensorType output_type() const { + return static_cast(GetField(VT_OUTPUT_TYPE, 0)); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_OUTPUT_TYPE, 1) && + verifier.EndTable(); + } + ArgMaxOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(ArgMaxOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ArgMaxOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct ArgMaxOptionsBuilder { + typedef ArgMaxOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_output_type(tflite::TensorType output_type) { + fbb_.AddElement(ArgMaxOptions::VT_OUTPUT_TYPE, static_cast(output_type), 0); + } + explicit ArgMaxOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateArgMaxOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + tflite::TensorType output_type = tflite::TensorType_FLOAT32) { + ArgMaxOptionsBuilder builder_(_fbb); + builder_.add_output_type(output_type); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateArgMaxOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ArgMaxOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct ArgMinOptionsT : public ::flatbuffers::NativeTable { + typedef ArgMinOptions TableType; + tflite::TensorType output_type = tflite::TensorType_FLOAT32; +}; + +struct ArgMinOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef ArgMinOptionsT NativeTableType; + typedef ArgMinOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_OUTPUT_TYPE = 4 + }; + tflite::TensorType output_type() const { + return static_cast(GetField(VT_OUTPUT_TYPE, 0)); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_OUTPUT_TYPE, 1) && + verifier.EndTable(); + } + ArgMinOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(ArgMinOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ArgMinOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct ArgMinOptionsBuilder { + typedef ArgMinOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_output_type(tflite::TensorType output_type) { + fbb_.AddElement(ArgMinOptions::VT_OUTPUT_TYPE, static_cast(output_type), 0); + } + explicit ArgMinOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateArgMinOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + tflite::TensorType output_type = tflite::TensorType_FLOAT32) { + ArgMinOptionsBuilder builder_(_fbb); + builder_.add_output_type(output_type); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateArgMinOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ArgMinOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct GreaterOptionsT : public ::flatbuffers::NativeTable { + typedef GreaterOptions TableType; +}; + +struct GreaterOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef GreaterOptionsT NativeTableType; + typedef GreaterOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + GreaterOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(GreaterOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const GreaterOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct GreaterOptionsBuilder { + typedef GreaterOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit GreaterOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateGreaterOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + GreaterOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateGreaterOptions(::flatbuffers::FlatBufferBuilder &_fbb, const GreaterOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct GreaterEqualOptionsT : public ::flatbuffers::NativeTable { + typedef GreaterEqualOptions TableType; +}; + +struct GreaterEqualOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef GreaterEqualOptionsT NativeTableType; + typedef GreaterEqualOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + GreaterEqualOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(GreaterEqualOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const GreaterEqualOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct GreaterEqualOptionsBuilder { + typedef GreaterEqualOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit GreaterEqualOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateGreaterEqualOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + GreaterEqualOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateGreaterEqualOptions(::flatbuffers::FlatBufferBuilder &_fbb, const GreaterEqualOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct LessOptionsT : public ::flatbuffers::NativeTable { + typedef LessOptions TableType; +}; + +struct LessOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef LessOptionsT NativeTableType; + typedef LessOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + LessOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(LessOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const LessOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct LessOptionsBuilder { + typedef LessOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit LessOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateLessOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + LessOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateLessOptions(::flatbuffers::FlatBufferBuilder &_fbb, const LessOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct LessEqualOptionsT : public ::flatbuffers::NativeTable { + typedef LessEqualOptions TableType; +}; + +struct LessEqualOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef LessEqualOptionsT NativeTableType; + typedef LessEqualOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + LessEqualOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(LessEqualOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const LessEqualOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct LessEqualOptionsBuilder { + typedef LessEqualOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit LessEqualOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateLessEqualOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + LessEqualOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateLessEqualOptions(::flatbuffers::FlatBufferBuilder &_fbb, const LessEqualOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct NegOptionsT : public ::flatbuffers::NativeTable { + typedef NegOptions TableType; +}; + +struct NegOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef NegOptionsT NativeTableType; + typedef NegOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + NegOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(NegOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const NegOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct NegOptionsBuilder { + typedef NegOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit NegOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateNegOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + NegOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateNegOptions(::flatbuffers::FlatBufferBuilder &_fbb, const NegOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct SelectOptionsT : public ::flatbuffers::NativeTable { + typedef SelectOptions TableType; +}; + +struct SelectOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef SelectOptionsT NativeTableType; + typedef SelectOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + SelectOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(SelectOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SelectOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct SelectOptionsBuilder { + typedef SelectOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit SelectOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateSelectOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + SelectOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateSelectOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SelectOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct SliceOptionsT : public ::flatbuffers::NativeTable { + typedef SliceOptions TableType; +}; + +struct SliceOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef SliceOptionsT NativeTableType; + typedef SliceOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + SliceOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(SliceOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SliceOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct SliceOptionsBuilder { + typedef SliceOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit SliceOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateSliceOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + SliceOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateSliceOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SliceOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct TransposeConvOptionsT : public ::flatbuffers::NativeTable { + typedef TransposeConvOptions TableType; + tflite::Padding padding = tflite::Padding_SAME; + int32_t stride_w = 0; + int32_t stride_h = 0; + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE; + tflite::TensorType quantized_bias_type = tflite::TensorType_FLOAT32; +}; + +struct TransposeConvOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef TransposeConvOptionsT NativeTableType; + typedef TransposeConvOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_PADDING = 4, + VT_STRIDE_W = 6, + VT_STRIDE_H = 8, + VT_FUSED_ACTIVATION_FUNCTION = 10, + VT_QUANTIZED_BIAS_TYPE = 12 + }; + tflite::Padding padding() const { + return static_cast(GetField(VT_PADDING, 0)); + } + int32_t stride_w() const { + return GetField(VT_STRIDE_W, 0); + } + int32_t stride_h() const { + return GetField(VT_STRIDE_H, 0); + } + tflite::ActivationFunctionType fused_activation_function() const { + return static_cast(GetField(VT_FUSED_ACTIVATION_FUNCTION, 0)); + } + tflite::TensorType quantized_bias_type() const { + return static_cast(GetField(VT_QUANTIZED_BIAS_TYPE, 0)); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_PADDING, 1) && + VerifyField(verifier, VT_STRIDE_W, 4) && + VerifyField(verifier, VT_STRIDE_H, 4) && + VerifyField(verifier, VT_FUSED_ACTIVATION_FUNCTION, 1) && + VerifyField(verifier, VT_QUANTIZED_BIAS_TYPE, 1) && + verifier.EndTable(); + } + TransposeConvOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(TransposeConvOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const TransposeConvOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct TransposeConvOptionsBuilder { + typedef TransposeConvOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_padding(tflite::Padding padding) { + fbb_.AddElement(TransposeConvOptions::VT_PADDING, static_cast(padding), 0); + } + void add_stride_w(int32_t stride_w) { + fbb_.AddElement(TransposeConvOptions::VT_STRIDE_W, stride_w, 0); + } + void add_stride_h(int32_t stride_h) { + fbb_.AddElement(TransposeConvOptions::VT_STRIDE_H, stride_h, 0); + } + void add_fused_activation_function(tflite::ActivationFunctionType fused_activation_function) { + fbb_.AddElement(TransposeConvOptions::VT_FUSED_ACTIVATION_FUNCTION, static_cast(fused_activation_function), 0); + } + void add_quantized_bias_type(tflite::TensorType quantized_bias_type) { + fbb_.AddElement(TransposeConvOptions::VT_QUANTIZED_BIAS_TYPE, static_cast(quantized_bias_type), 0); + } + explicit TransposeConvOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateTransposeConvOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + tflite::Padding padding = tflite::Padding_SAME, + int32_t stride_w = 0, + int32_t stride_h = 0, + tflite::ActivationFunctionType fused_activation_function = tflite::ActivationFunctionType_NONE, + tflite::TensorType quantized_bias_type = tflite::TensorType_FLOAT32) { + TransposeConvOptionsBuilder builder_(_fbb); + builder_.add_stride_h(stride_h); + builder_.add_stride_w(stride_w); + builder_.add_quantized_bias_type(quantized_bias_type); + builder_.add_fused_activation_function(fused_activation_function); + builder_.add_padding(padding); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateTransposeConvOptions(::flatbuffers::FlatBufferBuilder &_fbb, const TransposeConvOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct ExpandDimsOptionsT : public ::flatbuffers::NativeTable { + typedef ExpandDimsOptions TableType; +}; + +struct ExpandDimsOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef ExpandDimsOptionsT NativeTableType; + typedef ExpandDimsOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + ExpandDimsOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(ExpandDimsOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ExpandDimsOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct ExpandDimsOptionsBuilder { + typedef ExpandDimsOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit ExpandDimsOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateExpandDimsOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + ExpandDimsOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateExpandDimsOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ExpandDimsOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct SparseToDenseOptionsT : public ::flatbuffers::NativeTable { + typedef SparseToDenseOptions TableType; + bool validate_indices = false; +}; + +struct SparseToDenseOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef SparseToDenseOptionsT NativeTableType; + typedef SparseToDenseOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_VALIDATE_INDICES = 4 + }; + bool validate_indices() const { + return GetField(VT_VALIDATE_INDICES, 0) != 0; + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_VALIDATE_INDICES, 1) && + verifier.EndTable(); + } + SparseToDenseOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(SparseToDenseOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SparseToDenseOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct SparseToDenseOptionsBuilder { + typedef SparseToDenseOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_validate_indices(bool validate_indices) { + fbb_.AddElement(SparseToDenseOptions::VT_VALIDATE_INDICES, static_cast(validate_indices), 0); + } + explicit SparseToDenseOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateSparseToDenseOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + bool validate_indices = false) { + SparseToDenseOptionsBuilder builder_(_fbb); + builder_.add_validate_indices(validate_indices); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateSparseToDenseOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SparseToDenseOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct EqualOptionsT : public ::flatbuffers::NativeTable { + typedef EqualOptions TableType; +}; + +struct EqualOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef EqualOptionsT NativeTableType; + typedef EqualOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + EqualOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(EqualOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const EqualOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct EqualOptionsBuilder { + typedef EqualOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit EqualOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateEqualOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + EqualOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateEqualOptions(::flatbuffers::FlatBufferBuilder &_fbb, const EqualOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct NotEqualOptionsT : public ::flatbuffers::NativeTable { + typedef NotEqualOptions TableType; +}; + +struct NotEqualOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef NotEqualOptionsT NativeTableType; + typedef NotEqualOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + NotEqualOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(NotEqualOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const NotEqualOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct NotEqualOptionsBuilder { + typedef NotEqualOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit NotEqualOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateNotEqualOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + NotEqualOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateNotEqualOptions(::flatbuffers::FlatBufferBuilder &_fbb, const NotEqualOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct ShapeOptionsT : public ::flatbuffers::NativeTable { + typedef ShapeOptions TableType; + tflite::TensorType out_type = tflite::TensorType_FLOAT32; +}; + +struct ShapeOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef ShapeOptionsT NativeTableType; + typedef ShapeOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_OUT_TYPE = 4 + }; + tflite::TensorType out_type() const { + return static_cast(GetField(VT_OUT_TYPE, 0)); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_OUT_TYPE, 1) && + verifier.EndTable(); + } + ShapeOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(ShapeOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ShapeOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct ShapeOptionsBuilder { + typedef ShapeOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_out_type(tflite::TensorType out_type) { + fbb_.AddElement(ShapeOptions::VT_OUT_TYPE, static_cast(out_type), 0); + } + explicit ShapeOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateShapeOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + tflite::TensorType out_type = tflite::TensorType_FLOAT32) { + ShapeOptionsBuilder builder_(_fbb); + builder_.add_out_type(out_type); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateShapeOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ShapeOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct RankOptionsT : public ::flatbuffers::NativeTable { + typedef RankOptions TableType; +}; + +struct RankOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef RankOptionsT NativeTableType; + typedef RankOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + RankOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(RankOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const RankOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct RankOptionsBuilder { + typedef RankOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit RankOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateRankOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + RankOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateRankOptions(::flatbuffers::FlatBufferBuilder &_fbb, const RankOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct PowOptionsT : public ::flatbuffers::NativeTable { + typedef PowOptions TableType; +}; + +struct PowOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef PowOptionsT NativeTableType; + typedef PowOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + PowOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(PowOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const PowOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct PowOptionsBuilder { + typedef PowOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit PowOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreatePowOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + PowOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreatePowOptions(::flatbuffers::FlatBufferBuilder &_fbb, const PowOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct FakeQuantOptionsT : public ::flatbuffers::NativeTable { + typedef FakeQuantOptions TableType; + float min = 0.0f; + float max = 0.0f; + int32_t num_bits = 0; + bool narrow_range = false; +}; + +struct FakeQuantOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef FakeQuantOptionsT NativeTableType; + typedef FakeQuantOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_MIN = 4, + VT_MAX = 6, + VT_NUM_BITS = 8, + VT_NARROW_RANGE = 10 + }; + float min() const { + return GetField(VT_MIN, 0.0f); + } + float max() const { + return GetField(VT_MAX, 0.0f); + } + int32_t num_bits() const { + return GetField(VT_NUM_BITS, 0); + } + bool narrow_range() const { + return GetField(VT_NARROW_RANGE, 0) != 0; + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_MIN, 4) && + VerifyField(verifier, VT_MAX, 4) && + VerifyField(verifier, VT_NUM_BITS, 4) && + VerifyField(verifier, VT_NARROW_RANGE, 1) && + verifier.EndTable(); + } + FakeQuantOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(FakeQuantOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const FakeQuantOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct FakeQuantOptionsBuilder { + typedef FakeQuantOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_min(float min) { + fbb_.AddElement(FakeQuantOptions::VT_MIN, min, 0.0f); + } + void add_max(float max) { + fbb_.AddElement(FakeQuantOptions::VT_MAX, max, 0.0f); + } + void add_num_bits(int32_t num_bits) { + fbb_.AddElement(FakeQuantOptions::VT_NUM_BITS, num_bits, 0); + } + void add_narrow_range(bool narrow_range) { + fbb_.AddElement(FakeQuantOptions::VT_NARROW_RANGE, static_cast(narrow_range), 0); + } + explicit FakeQuantOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateFakeQuantOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + float min = 0.0f, + float max = 0.0f, + int32_t num_bits = 0, + bool narrow_range = false) { + FakeQuantOptionsBuilder builder_(_fbb); + builder_.add_num_bits(num_bits); + builder_.add_max(max); + builder_.add_min(min); + builder_.add_narrow_range(narrow_range); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateFakeQuantOptions(::flatbuffers::FlatBufferBuilder &_fbb, const FakeQuantOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct PackOptionsT : public ::flatbuffers::NativeTable { + typedef PackOptions TableType; + int32_t values_count = 0; + int32_t axis = 0; +}; + +struct PackOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef PackOptionsT NativeTableType; + typedef PackOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_VALUES_COUNT = 4, + VT_AXIS = 6 + }; + int32_t values_count() const { + return GetField(VT_VALUES_COUNT, 0); + } + int32_t axis() const { + return GetField(VT_AXIS, 0); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_VALUES_COUNT, 4) && + VerifyField(verifier, VT_AXIS, 4) && + verifier.EndTable(); + } + PackOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(PackOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const PackOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct PackOptionsBuilder { + typedef PackOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_values_count(int32_t values_count) { + fbb_.AddElement(PackOptions::VT_VALUES_COUNT, values_count, 0); + } + void add_axis(int32_t axis) { + fbb_.AddElement(PackOptions::VT_AXIS, axis, 0); + } + explicit PackOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreatePackOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + int32_t values_count = 0, + int32_t axis = 0) { + PackOptionsBuilder builder_(_fbb); + builder_.add_axis(axis); + builder_.add_values_count(values_count); + return builder_.Finish(); +} + +::flatbuffers::Offset CreatePackOptions(::flatbuffers::FlatBufferBuilder &_fbb, const PackOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct LogicalOrOptionsT : public ::flatbuffers::NativeTable { + typedef LogicalOrOptions TableType; +}; + +struct LogicalOrOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef LogicalOrOptionsT NativeTableType; + typedef LogicalOrOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + LogicalOrOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(LogicalOrOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const LogicalOrOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct LogicalOrOptionsBuilder { + typedef LogicalOrOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit LogicalOrOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateLogicalOrOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + LogicalOrOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateLogicalOrOptions(::flatbuffers::FlatBufferBuilder &_fbb, const LogicalOrOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct OneHotOptionsT : public ::flatbuffers::NativeTable { + typedef OneHotOptions TableType; + int32_t axis = 0; +}; + +struct OneHotOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef OneHotOptionsT NativeTableType; + typedef OneHotOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_AXIS = 4 + }; + int32_t axis() const { + return GetField(VT_AXIS, 0); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_AXIS, 4) && + verifier.EndTable(); + } + OneHotOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(OneHotOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const OneHotOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct OneHotOptionsBuilder { + typedef OneHotOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_axis(int32_t axis) { + fbb_.AddElement(OneHotOptions::VT_AXIS, axis, 0); + } + explicit OneHotOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateOneHotOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + int32_t axis = 0) { + OneHotOptionsBuilder builder_(_fbb); + builder_.add_axis(axis); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateOneHotOptions(::flatbuffers::FlatBufferBuilder &_fbb, const OneHotOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct AbsOptionsT : public ::flatbuffers::NativeTable { + typedef AbsOptions TableType; +}; + +struct AbsOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef AbsOptionsT NativeTableType; + typedef AbsOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + AbsOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(AbsOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const AbsOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct AbsOptionsBuilder { + typedef AbsOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit AbsOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateAbsOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + AbsOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateAbsOptions(::flatbuffers::FlatBufferBuilder &_fbb, const AbsOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct HardSwishOptionsT : public ::flatbuffers::NativeTable { + typedef HardSwishOptions TableType; +}; + +struct HardSwishOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef HardSwishOptionsT NativeTableType; + typedef HardSwishOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + HardSwishOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(HardSwishOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const HardSwishOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct HardSwishOptionsBuilder { + typedef HardSwishOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit HardSwishOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateHardSwishOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + HardSwishOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateHardSwishOptions(::flatbuffers::FlatBufferBuilder &_fbb, const HardSwishOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct LogicalAndOptionsT : public ::flatbuffers::NativeTable { + typedef LogicalAndOptions TableType; +}; + +struct LogicalAndOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef LogicalAndOptionsT NativeTableType; + typedef LogicalAndOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + LogicalAndOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(LogicalAndOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const LogicalAndOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct LogicalAndOptionsBuilder { + typedef LogicalAndOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit LogicalAndOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateLogicalAndOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + LogicalAndOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateLogicalAndOptions(::flatbuffers::FlatBufferBuilder &_fbb, const LogicalAndOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct LogicalNotOptionsT : public ::flatbuffers::NativeTable { + typedef LogicalNotOptions TableType; +}; + +struct LogicalNotOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef LogicalNotOptionsT NativeTableType; + typedef LogicalNotOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + LogicalNotOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(LogicalNotOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const LogicalNotOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct LogicalNotOptionsBuilder { + typedef LogicalNotOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit LogicalNotOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateLogicalNotOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + LogicalNotOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateLogicalNotOptions(::flatbuffers::FlatBufferBuilder &_fbb, const LogicalNotOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct UnpackOptionsT : public ::flatbuffers::NativeTable { + typedef UnpackOptions TableType; + int32_t num = 0; + int32_t axis = 0; +}; + +struct UnpackOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef UnpackOptionsT NativeTableType; + typedef UnpackOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_NUM = 4, + VT_AXIS = 6 + }; + int32_t num() const { + return GetField(VT_NUM, 0); + } + int32_t axis() const { + return GetField(VT_AXIS, 0); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_NUM, 4) && + VerifyField(verifier, VT_AXIS, 4) && + verifier.EndTable(); + } + UnpackOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(UnpackOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const UnpackOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct UnpackOptionsBuilder { + typedef UnpackOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_num(int32_t num) { + fbb_.AddElement(UnpackOptions::VT_NUM, num, 0); + } + void add_axis(int32_t axis) { + fbb_.AddElement(UnpackOptions::VT_AXIS, axis, 0); + } + explicit UnpackOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateUnpackOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + int32_t num = 0, + int32_t axis = 0) { + UnpackOptionsBuilder builder_(_fbb); + builder_.add_axis(axis); + builder_.add_num(num); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateUnpackOptions(::flatbuffers::FlatBufferBuilder &_fbb, const UnpackOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct FloorDivOptionsT : public ::flatbuffers::NativeTable { + typedef FloorDivOptions TableType; +}; + +struct FloorDivOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef FloorDivOptionsT NativeTableType; + typedef FloorDivOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + FloorDivOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(FloorDivOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const FloorDivOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct FloorDivOptionsBuilder { + typedef FloorDivOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit FloorDivOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateFloorDivOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + FloorDivOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateFloorDivOptions(::flatbuffers::FlatBufferBuilder &_fbb, const FloorDivOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct SquareOptionsT : public ::flatbuffers::NativeTable { + typedef SquareOptions TableType; +}; + +struct SquareOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef SquareOptionsT NativeTableType; + typedef SquareOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + SquareOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(SquareOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SquareOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct SquareOptionsBuilder { + typedef SquareOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit SquareOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateSquareOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + SquareOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateSquareOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SquareOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct ZerosLikeOptionsT : public ::flatbuffers::NativeTable { + typedef ZerosLikeOptions TableType; +}; + +struct ZerosLikeOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef ZerosLikeOptionsT NativeTableType; + typedef ZerosLikeOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + ZerosLikeOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(ZerosLikeOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ZerosLikeOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct ZerosLikeOptionsBuilder { + typedef ZerosLikeOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit ZerosLikeOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateZerosLikeOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + ZerosLikeOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateZerosLikeOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ZerosLikeOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct FillOptionsT : public ::flatbuffers::NativeTable { + typedef FillOptions TableType; +}; + +struct FillOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef FillOptionsT NativeTableType; + typedef FillOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + FillOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(FillOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const FillOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct FillOptionsBuilder { + typedef FillOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit FillOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateFillOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + FillOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateFillOptions(::flatbuffers::FlatBufferBuilder &_fbb, const FillOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct FloorModOptionsT : public ::flatbuffers::NativeTable { + typedef FloorModOptions TableType; +}; + +struct FloorModOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef FloorModOptionsT NativeTableType; + typedef FloorModOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + FloorModOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(FloorModOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const FloorModOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct FloorModOptionsBuilder { + typedef FloorModOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit FloorModOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateFloorModOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + FloorModOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateFloorModOptions(::flatbuffers::FlatBufferBuilder &_fbb, const FloorModOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct RangeOptionsT : public ::flatbuffers::NativeTable { + typedef RangeOptions TableType; +}; + +struct RangeOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef RangeOptionsT NativeTableType; + typedef RangeOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + RangeOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(RangeOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const RangeOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct RangeOptionsBuilder { + typedef RangeOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit RangeOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateRangeOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + RangeOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateRangeOptions(::flatbuffers::FlatBufferBuilder &_fbb, const RangeOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct LeakyReluOptionsT : public ::flatbuffers::NativeTable { + typedef LeakyReluOptions TableType; + float alpha = 0.0f; +}; + +struct LeakyReluOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef LeakyReluOptionsT NativeTableType; + typedef LeakyReluOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_ALPHA = 4 + }; + float alpha() const { + return GetField(VT_ALPHA, 0.0f); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_ALPHA, 4) && + verifier.EndTable(); + } + LeakyReluOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(LeakyReluOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const LeakyReluOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct LeakyReluOptionsBuilder { + typedef LeakyReluOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_alpha(float alpha) { + fbb_.AddElement(LeakyReluOptions::VT_ALPHA, alpha, 0.0f); + } + explicit LeakyReluOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateLeakyReluOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + float alpha = 0.0f) { + LeakyReluOptionsBuilder builder_(_fbb); + builder_.add_alpha(alpha); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateLeakyReluOptions(::flatbuffers::FlatBufferBuilder &_fbb, const LeakyReluOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct SquaredDifferenceOptionsT : public ::flatbuffers::NativeTable { + typedef SquaredDifferenceOptions TableType; +}; + +struct SquaredDifferenceOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef SquaredDifferenceOptionsT NativeTableType; + typedef SquaredDifferenceOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + SquaredDifferenceOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(SquaredDifferenceOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SquaredDifferenceOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct SquaredDifferenceOptionsBuilder { + typedef SquaredDifferenceOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit SquaredDifferenceOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateSquaredDifferenceOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + SquaredDifferenceOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateSquaredDifferenceOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SquaredDifferenceOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct MirrorPadOptionsT : public ::flatbuffers::NativeTable { + typedef MirrorPadOptions TableType; + tflite::MirrorPadMode mode = tflite::MirrorPadMode_REFLECT; +}; + +struct MirrorPadOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef MirrorPadOptionsT NativeTableType; + typedef MirrorPadOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_MODE = 4 + }; + tflite::MirrorPadMode mode() const { + return static_cast(GetField(VT_MODE, 0)); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_MODE, 1) && + verifier.EndTable(); + } + MirrorPadOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(MirrorPadOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const MirrorPadOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct MirrorPadOptionsBuilder { + typedef MirrorPadOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_mode(tflite::MirrorPadMode mode) { + fbb_.AddElement(MirrorPadOptions::VT_MODE, static_cast(mode), 0); + } + explicit MirrorPadOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateMirrorPadOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + tflite::MirrorPadMode mode = tflite::MirrorPadMode_REFLECT) { + MirrorPadOptionsBuilder builder_(_fbb); + builder_.add_mode(mode); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateMirrorPadOptions(::flatbuffers::FlatBufferBuilder &_fbb, const MirrorPadOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct UniqueOptionsT : public ::flatbuffers::NativeTable { + typedef UniqueOptions TableType; + tflite::TensorType idx_out_type = tflite::TensorType_INT32; +}; + +struct UniqueOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef UniqueOptionsT NativeTableType; + typedef UniqueOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_IDX_OUT_TYPE = 4 + }; + tflite::TensorType idx_out_type() const { + return static_cast(GetField(VT_IDX_OUT_TYPE, 2)); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_IDX_OUT_TYPE, 1) && + verifier.EndTable(); + } + UniqueOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(UniqueOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const UniqueOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct UniqueOptionsBuilder { + typedef UniqueOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_idx_out_type(tflite::TensorType idx_out_type) { + fbb_.AddElement(UniqueOptions::VT_IDX_OUT_TYPE, static_cast(idx_out_type), 2); + } + explicit UniqueOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateUniqueOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + tflite::TensorType idx_out_type = tflite::TensorType_INT32) { + UniqueOptionsBuilder builder_(_fbb); + builder_.add_idx_out_type(idx_out_type); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateUniqueOptions(::flatbuffers::FlatBufferBuilder &_fbb, const UniqueOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct ReverseV2OptionsT : public ::flatbuffers::NativeTable { + typedef ReverseV2Options TableType; +}; + +struct ReverseV2Options FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef ReverseV2OptionsT NativeTableType; + typedef ReverseV2OptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + ReverseV2OptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(ReverseV2OptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ReverseV2OptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct ReverseV2OptionsBuilder { + typedef ReverseV2Options Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit ReverseV2OptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateReverseV2Options( + ::flatbuffers::FlatBufferBuilder &_fbb) { + ReverseV2OptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateReverseV2Options(::flatbuffers::FlatBufferBuilder &_fbb, const ReverseV2OptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct AddNOptionsT : public ::flatbuffers::NativeTable { + typedef AddNOptions TableType; +}; + +struct AddNOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef AddNOptionsT NativeTableType; + typedef AddNOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + AddNOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(AddNOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const AddNOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct AddNOptionsBuilder { + typedef AddNOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit AddNOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateAddNOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + AddNOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateAddNOptions(::flatbuffers::FlatBufferBuilder &_fbb, const AddNOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct GatherNdOptionsT : public ::flatbuffers::NativeTable { + typedef GatherNdOptions TableType; +}; + +struct GatherNdOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef GatherNdOptionsT NativeTableType; + typedef GatherNdOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + GatherNdOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(GatherNdOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const GatherNdOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct GatherNdOptionsBuilder { + typedef GatherNdOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit GatherNdOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateGatherNdOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + GatherNdOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateGatherNdOptions(::flatbuffers::FlatBufferBuilder &_fbb, const GatherNdOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct WhereOptionsT : public ::flatbuffers::NativeTable { + typedef WhereOptions TableType; +}; + +struct WhereOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef WhereOptionsT NativeTableType; + typedef WhereOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + WhereOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(WhereOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const WhereOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct WhereOptionsBuilder { + typedef WhereOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit WhereOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateWhereOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + WhereOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateWhereOptions(::flatbuffers::FlatBufferBuilder &_fbb, const WhereOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct ReverseSequenceOptionsT : public ::flatbuffers::NativeTable { + typedef ReverseSequenceOptions TableType; + int32_t seq_dim = 0; + int32_t batch_dim = 0; +}; + +struct ReverseSequenceOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef ReverseSequenceOptionsT NativeTableType; + typedef ReverseSequenceOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_SEQ_DIM = 4, + VT_BATCH_DIM = 6 + }; + int32_t seq_dim() const { + return GetField(VT_SEQ_DIM, 0); + } + int32_t batch_dim() const { + return GetField(VT_BATCH_DIM, 0); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_SEQ_DIM, 4) && + VerifyField(verifier, VT_BATCH_DIM, 4) && + verifier.EndTable(); + } + ReverseSequenceOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(ReverseSequenceOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ReverseSequenceOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct ReverseSequenceOptionsBuilder { + typedef ReverseSequenceOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_seq_dim(int32_t seq_dim) { + fbb_.AddElement(ReverseSequenceOptions::VT_SEQ_DIM, seq_dim, 0); + } + void add_batch_dim(int32_t batch_dim) { + fbb_.AddElement(ReverseSequenceOptions::VT_BATCH_DIM, batch_dim, 0); + } + explicit ReverseSequenceOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateReverseSequenceOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + int32_t seq_dim = 0, + int32_t batch_dim = 0) { + ReverseSequenceOptionsBuilder builder_(_fbb); + builder_.add_batch_dim(batch_dim); + builder_.add_seq_dim(seq_dim); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateReverseSequenceOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ReverseSequenceOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct MatrixDiagOptionsT : public ::flatbuffers::NativeTable { + typedef MatrixDiagOptions TableType; +}; + +struct MatrixDiagOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef MatrixDiagOptionsT NativeTableType; + typedef MatrixDiagOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + MatrixDiagOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(MatrixDiagOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const MatrixDiagOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct MatrixDiagOptionsBuilder { + typedef MatrixDiagOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit MatrixDiagOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateMatrixDiagOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + MatrixDiagOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateMatrixDiagOptions(::flatbuffers::FlatBufferBuilder &_fbb, const MatrixDiagOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct QuantizeOptionsT : public ::flatbuffers::NativeTable { + typedef QuantizeOptions TableType; +}; + +struct QuantizeOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef QuantizeOptionsT NativeTableType; + typedef QuantizeOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + QuantizeOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(QuantizeOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const QuantizeOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct QuantizeOptionsBuilder { + typedef QuantizeOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit QuantizeOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateQuantizeOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + QuantizeOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateQuantizeOptions(::flatbuffers::FlatBufferBuilder &_fbb, const QuantizeOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct MatrixSetDiagOptionsT : public ::flatbuffers::NativeTable { + typedef MatrixSetDiagOptions TableType; +}; + +struct MatrixSetDiagOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef MatrixSetDiagOptionsT NativeTableType; + typedef MatrixSetDiagOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + MatrixSetDiagOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(MatrixSetDiagOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const MatrixSetDiagOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct MatrixSetDiagOptionsBuilder { + typedef MatrixSetDiagOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit MatrixSetDiagOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateMatrixSetDiagOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + MatrixSetDiagOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateMatrixSetDiagOptions(::flatbuffers::FlatBufferBuilder &_fbb, const MatrixSetDiagOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct IfOptionsT : public ::flatbuffers::NativeTable { + typedef IfOptions TableType; + int32_t then_subgraph_index = 0; + int32_t else_subgraph_index = 0; +}; + +struct IfOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef IfOptionsT NativeTableType; + typedef IfOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_THEN_SUBGRAPH_INDEX = 4, + VT_ELSE_SUBGRAPH_INDEX = 6 + }; + int32_t then_subgraph_index() const { + return GetField(VT_THEN_SUBGRAPH_INDEX, 0); + } + int32_t else_subgraph_index() const { + return GetField(VT_ELSE_SUBGRAPH_INDEX, 0); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_THEN_SUBGRAPH_INDEX, 4) && + VerifyField(verifier, VT_ELSE_SUBGRAPH_INDEX, 4) && + verifier.EndTable(); + } + IfOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(IfOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const IfOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct IfOptionsBuilder { + typedef IfOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_then_subgraph_index(int32_t then_subgraph_index) { + fbb_.AddElement(IfOptions::VT_THEN_SUBGRAPH_INDEX, then_subgraph_index, 0); + } + void add_else_subgraph_index(int32_t else_subgraph_index) { + fbb_.AddElement(IfOptions::VT_ELSE_SUBGRAPH_INDEX, else_subgraph_index, 0); + } + explicit IfOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateIfOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + int32_t then_subgraph_index = 0, + int32_t else_subgraph_index = 0) { + IfOptionsBuilder builder_(_fbb); + builder_.add_else_subgraph_index(else_subgraph_index); + builder_.add_then_subgraph_index(then_subgraph_index); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateIfOptions(::flatbuffers::FlatBufferBuilder &_fbb, const IfOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct CallOnceOptionsT : public ::flatbuffers::NativeTable { + typedef CallOnceOptions TableType; + int32_t init_subgraph_index = 0; +}; + +struct CallOnceOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef CallOnceOptionsT NativeTableType; + typedef CallOnceOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_INIT_SUBGRAPH_INDEX = 4 + }; + int32_t init_subgraph_index() const { + return GetField(VT_INIT_SUBGRAPH_INDEX, 0); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_INIT_SUBGRAPH_INDEX, 4) && + verifier.EndTable(); + } + CallOnceOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(CallOnceOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const CallOnceOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct CallOnceOptionsBuilder { + typedef CallOnceOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_init_subgraph_index(int32_t init_subgraph_index) { + fbb_.AddElement(CallOnceOptions::VT_INIT_SUBGRAPH_INDEX, init_subgraph_index, 0); + } + explicit CallOnceOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateCallOnceOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + int32_t init_subgraph_index = 0) { + CallOnceOptionsBuilder builder_(_fbb); + builder_.add_init_subgraph_index(init_subgraph_index); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateCallOnceOptions(::flatbuffers::FlatBufferBuilder &_fbb, const CallOnceOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct WhileOptionsT : public ::flatbuffers::NativeTable { + typedef WhileOptions TableType; + int32_t cond_subgraph_index = 0; + int32_t body_subgraph_index = 0; +}; + +struct WhileOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef WhileOptionsT NativeTableType; + typedef WhileOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_COND_SUBGRAPH_INDEX = 4, + VT_BODY_SUBGRAPH_INDEX = 6 + }; + int32_t cond_subgraph_index() const { + return GetField(VT_COND_SUBGRAPH_INDEX, 0); + } + int32_t body_subgraph_index() const { + return GetField(VT_BODY_SUBGRAPH_INDEX, 0); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_COND_SUBGRAPH_INDEX, 4) && + VerifyField(verifier, VT_BODY_SUBGRAPH_INDEX, 4) && + verifier.EndTable(); + } + WhileOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(WhileOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const WhileOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct WhileOptionsBuilder { + typedef WhileOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_cond_subgraph_index(int32_t cond_subgraph_index) { + fbb_.AddElement(WhileOptions::VT_COND_SUBGRAPH_INDEX, cond_subgraph_index, 0); + } + void add_body_subgraph_index(int32_t body_subgraph_index) { + fbb_.AddElement(WhileOptions::VT_BODY_SUBGRAPH_INDEX, body_subgraph_index, 0); + } + explicit WhileOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateWhileOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + int32_t cond_subgraph_index = 0, + int32_t body_subgraph_index = 0) { + WhileOptionsBuilder builder_(_fbb); + builder_.add_body_subgraph_index(body_subgraph_index); + builder_.add_cond_subgraph_index(cond_subgraph_index); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateWhileOptions(::flatbuffers::FlatBufferBuilder &_fbb, const WhileOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct NonMaxSuppressionV4OptionsT : public ::flatbuffers::NativeTable { + typedef NonMaxSuppressionV4Options TableType; +}; + +struct NonMaxSuppressionV4Options FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef NonMaxSuppressionV4OptionsT NativeTableType; + typedef NonMaxSuppressionV4OptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + NonMaxSuppressionV4OptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(NonMaxSuppressionV4OptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const NonMaxSuppressionV4OptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct NonMaxSuppressionV4OptionsBuilder { + typedef NonMaxSuppressionV4Options Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit NonMaxSuppressionV4OptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateNonMaxSuppressionV4Options( + ::flatbuffers::FlatBufferBuilder &_fbb) { + NonMaxSuppressionV4OptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateNonMaxSuppressionV4Options(::flatbuffers::FlatBufferBuilder &_fbb, const NonMaxSuppressionV4OptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct NonMaxSuppressionV5OptionsT : public ::flatbuffers::NativeTable { + typedef NonMaxSuppressionV5Options TableType; +}; + +struct NonMaxSuppressionV5Options FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef NonMaxSuppressionV5OptionsT NativeTableType; + typedef NonMaxSuppressionV5OptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + NonMaxSuppressionV5OptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(NonMaxSuppressionV5OptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const NonMaxSuppressionV5OptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct NonMaxSuppressionV5OptionsBuilder { + typedef NonMaxSuppressionV5Options Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit NonMaxSuppressionV5OptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateNonMaxSuppressionV5Options( + ::flatbuffers::FlatBufferBuilder &_fbb) { + NonMaxSuppressionV5OptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateNonMaxSuppressionV5Options(::flatbuffers::FlatBufferBuilder &_fbb, const NonMaxSuppressionV5OptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct ScatterNdOptionsT : public ::flatbuffers::NativeTable { + typedef ScatterNdOptions TableType; +}; + +struct ScatterNdOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef ScatterNdOptionsT NativeTableType; + typedef ScatterNdOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + ScatterNdOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(ScatterNdOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ScatterNdOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct ScatterNdOptionsBuilder { + typedef ScatterNdOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit ScatterNdOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateScatterNdOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + ScatterNdOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateScatterNdOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ScatterNdOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct SelectV2OptionsT : public ::flatbuffers::NativeTable { + typedef SelectV2Options TableType; +}; + +struct SelectV2Options FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef SelectV2OptionsT NativeTableType; + typedef SelectV2OptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + SelectV2OptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(SelectV2OptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SelectV2OptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct SelectV2OptionsBuilder { + typedef SelectV2Options Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit SelectV2OptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateSelectV2Options( + ::flatbuffers::FlatBufferBuilder &_fbb) { + SelectV2OptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateSelectV2Options(::flatbuffers::FlatBufferBuilder &_fbb, const SelectV2OptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct DensifyOptionsT : public ::flatbuffers::NativeTable { + typedef DensifyOptions TableType; +}; + +struct DensifyOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef DensifyOptionsT NativeTableType; + typedef DensifyOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + DensifyOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(DensifyOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const DensifyOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct DensifyOptionsBuilder { + typedef DensifyOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit DensifyOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateDensifyOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + DensifyOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateDensifyOptions(::flatbuffers::FlatBufferBuilder &_fbb, const DensifyOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct SegmentSumOptionsT : public ::flatbuffers::NativeTable { + typedef SegmentSumOptions TableType; +}; + +struct SegmentSumOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef SegmentSumOptionsT NativeTableType; + typedef SegmentSumOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + SegmentSumOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(SegmentSumOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SegmentSumOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct SegmentSumOptionsBuilder { + typedef SegmentSumOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit SegmentSumOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateSegmentSumOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + SegmentSumOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateSegmentSumOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SegmentSumOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct BatchMatMulOptionsT : public ::flatbuffers::NativeTable { + typedef BatchMatMulOptions TableType; + bool adj_x = false; + bool adj_y = false; + bool asymmetric_quantize_inputs = false; +}; + +struct BatchMatMulOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef BatchMatMulOptionsT NativeTableType; + typedef BatchMatMulOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_ADJ_X = 4, + VT_ADJ_Y = 6, + VT_ASYMMETRIC_QUANTIZE_INPUTS = 8 + }; + bool adj_x() const { + return GetField(VT_ADJ_X, 0) != 0; + } + bool adj_y() const { + return GetField(VT_ADJ_Y, 0) != 0; + } + bool asymmetric_quantize_inputs() const { + return GetField(VT_ASYMMETRIC_QUANTIZE_INPUTS, 0) != 0; + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_ADJ_X, 1) && + VerifyField(verifier, VT_ADJ_Y, 1) && + VerifyField(verifier, VT_ASYMMETRIC_QUANTIZE_INPUTS, 1) && + verifier.EndTable(); + } + BatchMatMulOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(BatchMatMulOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const BatchMatMulOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct BatchMatMulOptionsBuilder { + typedef BatchMatMulOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_adj_x(bool adj_x) { + fbb_.AddElement(BatchMatMulOptions::VT_ADJ_X, static_cast(adj_x), 0); + } + void add_adj_y(bool adj_y) { + fbb_.AddElement(BatchMatMulOptions::VT_ADJ_Y, static_cast(adj_y), 0); + } + void add_asymmetric_quantize_inputs(bool asymmetric_quantize_inputs) { + fbb_.AddElement(BatchMatMulOptions::VT_ASYMMETRIC_QUANTIZE_INPUTS, static_cast(asymmetric_quantize_inputs), 0); + } + explicit BatchMatMulOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateBatchMatMulOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + bool adj_x = false, + bool adj_y = false, + bool asymmetric_quantize_inputs = false) { + BatchMatMulOptionsBuilder builder_(_fbb); + builder_.add_asymmetric_quantize_inputs(asymmetric_quantize_inputs); + builder_.add_adj_y(adj_y); + builder_.add_adj_x(adj_x); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateBatchMatMulOptions(::flatbuffers::FlatBufferBuilder &_fbb, const BatchMatMulOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct CumsumOptionsT : public ::flatbuffers::NativeTable { + typedef CumsumOptions TableType; + bool exclusive = false; + bool reverse = false; +}; + +struct CumsumOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef CumsumOptionsT NativeTableType; + typedef CumsumOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_EXCLUSIVE = 4, + VT_REVERSE = 6 + }; + bool exclusive() const { + return GetField(VT_EXCLUSIVE, 0) != 0; + } + bool reverse() const { + return GetField(VT_REVERSE, 0) != 0; + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_EXCLUSIVE, 1) && + VerifyField(verifier, VT_REVERSE, 1) && + verifier.EndTable(); + } + CumsumOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(CumsumOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const CumsumOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct CumsumOptionsBuilder { + typedef CumsumOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_exclusive(bool exclusive) { + fbb_.AddElement(CumsumOptions::VT_EXCLUSIVE, static_cast(exclusive), 0); + } + void add_reverse(bool reverse) { + fbb_.AddElement(CumsumOptions::VT_REVERSE, static_cast(reverse), 0); + } + explicit CumsumOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateCumsumOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + bool exclusive = false, + bool reverse = false) { + CumsumOptionsBuilder builder_(_fbb); + builder_.add_reverse(reverse); + builder_.add_exclusive(exclusive); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateCumsumOptions(::flatbuffers::FlatBufferBuilder &_fbb, const CumsumOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct BroadcastToOptionsT : public ::flatbuffers::NativeTable { + typedef BroadcastToOptions TableType; +}; + +struct BroadcastToOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef BroadcastToOptionsT NativeTableType; + typedef BroadcastToOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + BroadcastToOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(BroadcastToOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const BroadcastToOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct BroadcastToOptionsBuilder { + typedef BroadcastToOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit BroadcastToOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateBroadcastToOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + BroadcastToOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateBroadcastToOptions(::flatbuffers::FlatBufferBuilder &_fbb, const BroadcastToOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct Rfft2dOptionsT : public ::flatbuffers::NativeTable { + typedef Rfft2dOptions TableType; +}; + +struct Rfft2dOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef Rfft2dOptionsT NativeTableType; + typedef Rfft2dOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + Rfft2dOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(Rfft2dOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const Rfft2dOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct Rfft2dOptionsBuilder { + typedef Rfft2dOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit Rfft2dOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateRfft2dOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + Rfft2dOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateRfft2dOptions(::flatbuffers::FlatBufferBuilder &_fbb, const Rfft2dOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct HashtableOptionsT : public ::flatbuffers::NativeTable { + typedef HashtableOptions TableType; + int32_t table_id = 0; + tflite::TensorType key_dtype = tflite::TensorType_FLOAT32; + tflite::TensorType value_dtype = tflite::TensorType_FLOAT32; +}; + +struct HashtableOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef HashtableOptionsT NativeTableType; + typedef HashtableOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_TABLE_ID = 4, + VT_KEY_DTYPE = 6, + VT_VALUE_DTYPE = 8 + }; + int32_t table_id() const { + return GetField(VT_TABLE_ID, 0); + } + tflite::TensorType key_dtype() const { + return static_cast(GetField(VT_KEY_DTYPE, 0)); + } + tflite::TensorType value_dtype() const { + return static_cast(GetField(VT_VALUE_DTYPE, 0)); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_TABLE_ID, 4) && + VerifyField(verifier, VT_KEY_DTYPE, 1) && + VerifyField(verifier, VT_VALUE_DTYPE, 1) && + verifier.EndTable(); + } + HashtableOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(HashtableOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const HashtableOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct HashtableOptionsBuilder { + typedef HashtableOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_table_id(int32_t table_id) { + fbb_.AddElement(HashtableOptions::VT_TABLE_ID, table_id, 0); + } + void add_key_dtype(tflite::TensorType key_dtype) { + fbb_.AddElement(HashtableOptions::VT_KEY_DTYPE, static_cast(key_dtype), 0); + } + void add_value_dtype(tflite::TensorType value_dtype) { + fbb_.AddElement(HashtableOptions::VT_VALUE_DTYPE, static_cast(value_dtype), 0); + } + explicit HashtableOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateHashtableOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + int32_t table_id = 0, + tflite::TensorType key_dtype = tflite::TensorType_FLOAT32, + tflite::TensorType value_dtype = tflite::TensorType_FLOAT32) { + HashtableOptionsBuilder builder_(_fbb); + builder_.add_table_id(table_id); + builder_.add_value_dtype(value_dtype); + builder_.add_key_dtype(key_dtype); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateHashtableOptions(::flatbuffers::FlatBufferBuilder &_fbb, const HashtableOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct HashtableFindOptionsT : public ::flatbuffers::NativeTable { + typedef HashtableFindOptions TableType; +}; + +struct HashtableFindOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef HashtableFindOptionsT NativeTableType; + typedef HashtableFindOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + HashtableFindOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(HashtableFindOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const HashtableFindOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct HashtableFindOptionsBuilder { + typedef HashtableFindOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit HashtableFindOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateHashtableFindOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + HashtableFindOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateHashtableFindOptions(::flatbuffers::FlatBufferBuilder &_fbb, const HashtableFindOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct HashtableImportOptionsT : public ::flatbuffers::NativeTable { + typedef HashtableImportOptions TableType; +}; + +struct HashtableImportOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef HashtableImportOptionsT NativeTableType; + typedef HashtableImportOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + HashtableImportOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(HashtableImportOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const HashtableImportOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct HashtableImportOptionsBuilder { + typedef HashtableImportOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit HashtableImportOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateHashtableImportOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + HashtableImportOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateHashtableImportOptions(::flatbuffers::FlatBufferBuilder &_fbb, const HashtableImportOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct HashtableSizeOptionsT : public ::flatbuffers::NativeTable { + typedef HashtableSizeOptions TableType; +}; + +struct HashtableSizeOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef HashtableSizeOptionsT NativeTableType; + typedef HashtableSizeOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + HashtableSizeOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(HashtableSizeOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const HashtableSizeOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct HashtableSizeOptionsBuilder { + typedef HashtableSizeOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit HashtableSizeOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateHashtableSizeOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + HashtableSizeOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateHashtableSizeOptions(::flatbuffers::FlatBufferBuilder &_fbb, const HashtableSizeOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct VarHandleOptionsT : public ::flatbuffers::NativeTable { + typedef VarHandleOptions TableType; + std::string container{}; + std::string shared_name{}; +}; + +struct VarHandleOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef VarHandleOptionsT NativeTableType; + typedef VarHandleOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_CONTAINER = 4, + VT_SHARED_NAME = 6 + }; + const ::flatbuffers::String *container() const { + return GetPointer(VT_CONTAINER); + } + const ::flatbuffers::String *shared_name() const { + return GetPointer(VT_SHARED_NAME); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_CONTAINER) && + verifier.VerifyString(container()) && + VerifyOffset(verifier, VT_SHARED_NAME) && + verifier.VerifyString(shared_name()) && + verifier.EndTable(); + } + VarHandleOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(VarHandleOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const VarHandleOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct VarHandleOptionsBuilder { + typedef VarHandleOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_container(::flatbuffers::Offset<::flatbuffers::String> container) { + fbb_.AddOffset(VarHandleOptions::VT_CONTAINER, container); + } + void add_shared_name(::flatbuffers::Offset<::flatbuffers::String> shared_name) { + fbb_.AddOffset(VarHandleOptions::VT_SHARED_NAME, shared_name); + } + explicit VarHandleOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateVarHandleOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::String> container = 0, + ::flatbuffers::Offset<::flatbuffers::String> shared_name = 0) { + VarHandleOptionsBuilder builder_(_fbb); + builder_.add_shared_name(shared_name); + builder_.add_container(container); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateVarHandleOptionsDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const char *container = nullptr, + const char *shared_name = nullptr) { + auto container__ = container ? _fbb.CreateString(container) : 0; + auto shared_name__ = shared_name ? _fbb.CreateString(shared_name) : 0; + return tflite::CreateVarHandleOptions( + _fbb, + container__, + shared_name__); +} + +::flatbuffers::Offset CreateVarHandleOptions(::flatbuffers::FlatBufferBuilder &_fbb, const VarHandleOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct ReadVariableOptionsT : public ::flatbuffers::NativeTable { + typedef ReadVariableOptions TableType; +}; + +struct ReadVariableOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef ReadVariableOptionsT NativeTableType; + typedef ReadVariableOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + ReadVariableOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(ReadVariableOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ReadVariableOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct ReadVariableOptionsBuilder { + typedef ReadVariableOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit ReadVariableOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateReadVariableOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + ReadVariableOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateReadVariableOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ReadVariableOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct AssignVariableOptionsT : public ::flatbuffers::NativeTable { + typedef AssignVariableOptions TableType; +}; + +struct AssignVariableOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef AssignVariableOptionsT NativeTableType; + typedef AssignVariableOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + AssignVariableOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(AssignVariableOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const AssignVariableOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct AssignVariableOptionsBuilder { + typedef AssignVariableOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit AssignVariableOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateAssignVariableOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + AssignVariableOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateAssignVariableOptions(::flatbuffers::FlatBufferBuilder &_fbb, const AssignVariableOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct RandomOptionsT : public ::flatbuffers::NativeTable { + typedef RandomOptions TableType; + int64_t seed = 0; + int64_t seed2 = 0; +}; + +struct RandomOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef RandomOptionsT NativeTableType; + typedef RandomOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_SEED = 4, + VT_SEED2 = 6 + }; + int64_t seed() const { + return GetField(VT_SEED, 0); + } + int64_t seed2() const { + return GetField(VT_SEED2, 0); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_SEED, 8) && + VerifyField(verifier, VT_SEED2, 8) && + verifier.EndTable(); + } + RandomOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(RandomOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const RandomOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct RandomOptionsBuilder { + typedef RandomOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_seed(int64_t seed) { + fbb_.AddElement(RandomOptions::VT_SEED, seed, 0); + } + void add_seed2(int64_t seed2) { + fbb_.AddElement(RandomOptions::VT_SEED2, seed2, 0); + } + explicit RandomOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateRandomOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + int64_t seed = 0, + int64_t seed2 = 0) { + RandomOptionsBuilder builder_(_fbb); + builder_.add_seed2(seed2); + builder_.add_seed(seed); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateRandomOptions(::flatbuffers::FlatBufferBuilder &_fbb, const RandomOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct BucketizeOptionsT : public ::flatbuffers::NativeTable { + typedef BucketizeOptions TableType; + std::vector boundaries{}; +}; + +struct BucketizeOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef BucketizeOptionsT NativeTableType; + typedef BucketizeOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_BOUNDARIES = 4 + }; + const ::flatbuffers::Vector *boundaries() const { + return GetPointer *>(VT_BOUNDARIES); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_BOUNDARIES) && + verifier.VerifyVector(boundaries()) && + verifier.EndTable(); + } + BucketizeOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(BucketizeOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const BucketizeOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct BucketizeOptionsBuilder { + typedef BucketizeOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_boundaries(::flatbuffers::Offset<::flatbuffers::Vector> boundaries) { + fbb_.AddOffset(BucketizeOptions::VT_BOUNDARIES, boundaries); + } + explicit BucketizeOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateBucketizeOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::Vector> boundaries = 0) { + BucketizeOptionsBuilder builder_(_fbb); + builder_.add_boundaries(boundaries); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateBucketizeOptionsDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const std::vector *boundaries = nullptr) { + auto boundaries__ = boundaries ? _fbb.CreateVector(*boundaries) : 0; + return tflite::CreateBucketizeOptions( + _fbb, + boundaries__); +} + +::flatbuffers::Offset CreateBucketizeOptions(::flatbuffers::FlatBufferBuilder &_fbb, const BucketizeOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct GeluOptionsT : public ::flatbuffers::NativeTable { + typedef GeluOptions TableType; + bool approximate = false; +}; + +struct GeluOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef GeluOptionsT NativeTableType; + typedef GeluOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_APPROXIMATE = 4 + }; + bool approximate() const { + return GetField(VT_APPROXIMATE, 0) != 0; + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_APPROXIMATE, 1) && + verifier.EndTable(); + } + GeluOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(GeluOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const GeluOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct GeluOptionsBuilder { + typedef GeluOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_approximate(bool approximate) { + fbb_.AddElement(GeluOptions::VT_APPROXIMATE, static_cast(approximate), 0); + } + explicit GeluOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateGeluOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + bool approximate = false) { + GeluOptionsBuilder builder_(_fbb); + builder_.add_approximate(approximate); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateGeluOptions(::flatbuffers::FlatBufferBuilder &_fbb, const GeluOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct DynamicUpdateSliceOptionsT : public ::flatbuffers::NativeTable { + typedef DynamicUpdateSliceOptions TableType; +}; + +struct DynamicUpdateSliceOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef DynamicUpdateSliceOptionsT NativeTableType; + typedef DynamicUpdateSliceOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + DynamicUpdateSliceOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(DynamicUpdateSliceOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const DynamicUpdateSliceOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct DynamicUpdateSliceOptionsBuilder { + typedef DynamicUpdateSliceOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit DynamicUpdateSliceOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateDynamicUpdateSliceOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + DynamicUpdateSliceOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateDynamicUpdateSliceOptions(::flatbuffers::FlatBufferBuilder &_fbb, const DynamicUpdateSliceOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct UnsortedSegmentProdOptionsT : public ::flatbuffers::NativeTable { + typedef UnsortedSegmentProdOptions TableType; +}; + +struct UnsortedSegmentProdOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef UnsortedSegmentProdOptionsT NativeTableType; + typedef UnsortedSegmentProdOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + UnsortedSegmentProdOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(UnsortedSegmentProdOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentProdOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct UnsortedSegmentProdOptionsBuilder { + typedef UnsortedSegmentProdOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit UnsortedSegmentProdOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateUnsortedSegmentProdOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + UnsortedSegmentProdOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateUnsortedSegmentProdOptions(::flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentProdOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct UnsortedSegmentMaxOptionsT : public ::flatbuffers::NativeTable { + typedef UnsortedSegmentMaxOptions TableType; +}; + +struct UnsortedSegmentMaxOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef UnsortedSegmentMaxOptionsT NativeTableType; + typedef UnsortedSegmentMaxOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + UnsortedSegmentMaxOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(UnsortedSegmentMaxOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentMaxOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct UnsortedSegmentMaxOptionsBuilder { + typedef UnsortedSegmentMaxOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit UnsortedSegmentMaxOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateUnsortedSegmentMaxOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + UnsortedSegmentMaxOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateUnsortedSegmentMaxOptions(::flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentMaxOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct UnsortedSegmentSumOptionsT : public ::flatbuffers::NativeTable { + typedef UnsortedSegmentSumOptions TableType; +}; + +struct UnsortedSegmentSumOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef UnsortedSegmentSumOptionsT NativeTableType; + typedef UnsortedSegmentSumOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + UnsortedSegmentSumOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(UnsortedSegmentSumOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentSumOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct UnsortedSegmentSumOptionsBuilder { + typedef UnsortedSegmentSumOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit UnsortedSegmentSumOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateUnsortedSegmentSumOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + UnsortedSegmentSumOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateUnsortedSegmentSumOptions(::flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentSumOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct ATan2OptionsT : public ::flatbuffers::NativeTable { + typedef ATan2Options TableType; +}; + +struct ATan2Options FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef ATan2OptionsT NativeTableType; + typedef ATan2OptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + ATan2OptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(ATan2OptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ATan2OptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct ATan2OptionsBuilder { + typedef ATan2Options Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit ATan2OptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateATan2Options( + ::flatbuffers::FlatBufferBuilder &_fbb) { + ATan2OptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateATan2Options(::flatbuffers::FlatBufferBuilder &_fbb, const ATan2OptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct UnsortedSegmentMinOptionsT : public ::flatbuffers::NativeTable { + typedef UnsortedSegmentMinOptions TableType; +}; + +struct UnsortedSegmentMinOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef UnsortedSegmentMinOptionsT NativeTableType; + typedef UnsortedSegmentMinOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + UnsortedSegmentMinOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(UnsortedSegmentMinOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentMinOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct UnsortedSegmentMinOptionsBuilder { + typedef UnsortedSegmentMinOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit UnsortedSegmentMinOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateUnsortedSegmentMinOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + UnsortedSegmentMinOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateUnsortedSegmentMinOptions(::flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentMinOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct SignOptionsT : public ::flatbuffers::NativeTable { + typedef SignOptions TableType; +}; + +struct SignOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef SignOptionsT NativeTableType; + typedef SignOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + SignOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(SignOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SignOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct SignOptionsBuilder { + typedef SignOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit SignOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateSignOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + SignOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateSignOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SignOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct BitcastOptionsT : public ::flatbuffers::NativeTable { + typedef BitcastOptions TableType; +}; + +struct BitcastOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef BitcastOptionsT NativeTableType; + typedef BitcastOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + BitcastOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(BitcastOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const BitcastOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct BitcastOptionsBuilder { + typedef BitcastOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit BitcastOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateBitcastOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + BitcastOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateBitcastOptions(::flatbuffers::FlatBufferBuilder &_fbb, const BitcastOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct BitwiseXorOptionsT : public ::flatbuffers::NativeTable { + typedef BitwiseXorOptions TableType; +}; + +struct BitwiseXorOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef BitwiseXorOptionsT NativeTableType; + typedef BitwiseXorOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + BitwiseXorOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(BitwiseXorOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const BitwiseXorOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct BitwiseXorOptionsBuilder { + typedef BitwiseXorOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit BitwiseXorOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateBitwiseXorOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + BitwiseXorOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateBitwiseXorOptions(::flatbuffers::FlatBufferBuilder &_fbb, const BitwiseXorOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct RightShiftOptionsT : public ::flatbuffers::NativeTable { + typedef RightShiftOptions TableType; +}; + +struct RightShiftOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef RightShiftOptionsT NativeTableType; + typedef RightShiftOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + RightShiftOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(RightShiftOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const RightShiftOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct RightShiftOptionsBuilder { + typedef RightShiftOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit RightShiftOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateRightShiftOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + RightShiftOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateRightShiftOptions(::flatbuffers::FlatBufferBuilder &_fbb, const RightShiftOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct DilateOptionsT : public ::flatbuffers::NativeTable { + typedef DilateOptions TableType; +}; + +struct DilateOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef DilateOptionsT NativeTableType; + typedef DilateOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + DilateOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(DilateOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const DilateOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct DilateOptionsBuilder { + typedef DilateOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit DilateOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateDilateOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + DilateOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateDilateOptions(::flatbuffers::FlatBufferBuilder &_fbb, const DilateOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct ReduceWindowOptionsT : public ::flatbuffers::NativeTable { + typedef ReduceWindowOptions TableType; + tflite::ReduceWindowFunction reduce_function = tflite::ReduceWindowFunction_UNSUPPORTED; +}; + +struct ReduceWindowOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef ReduceWindowOptionsT NativeTableType; + typedef ReduceWindowOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_REDUCE_FUNCTION = 4 + }; + tflite::ReduceWindowFunction reduce_function() const { + return static_cast(GetField(VT_REDUCE_FUNCTION, 0)); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_REDUCE_FUNCTION, 4) && + verifier.EndTable(); + } + ReduceWindowOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(ReduceWindowOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ReduceWindowOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct ReduceWindowOptionsBuilder { + typedef ReduceWindowOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_reduce_function(tflite::ReduceWindowFunction reduce_function) { + fbb_.AddElement(ReduceWindowOptions::VT_REDUCE_FUNCTION, static_cast(reduce_function), 0); + } + explicit ReduceWindowOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateReduceWindowOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + tflite::ReduceWindowFunction reduce_function = tflite::ReduceWindowFunction_UNSUPPORTED) { + ReduceWindowOptionsBuilder builder_(_fbb); + builder_.add_reduce_function(reduce_function); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateReduceWindowOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ReduceWindowOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct OperatorCodeT : public ::flatbuffers::NativeTable { + typedef OperatorCode TableType; + int8_t deprecated_builtin_code = 0; + std::string custom_code{}; + int32_t version = 1; + tflite::BuiltinOperator builtin_code = tflite::BuiltinOperator_ADD; +}; + +struct OperatorCode FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef OperatorCodeT NativeTableType; + typedef OperatorCodeBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_DEPRECATED_BUILTIN_CODE = 4, + VT_CUSTOM_CODE = 6, + VT_VERSION = 8, + VT_BUILTIN_CODE = 10 + }; + int8_t deprecated_builtin_code() const { + return GetField(VT_DEPRECATED_BUILTIN_CODE, 0); + } + const ::flatbuffers::String *custom_code() const { + return GetPointer(VT_CUSTOM_CODE); + } + int32_t version() const { + return GetField(VT_VERSION, 1); + } + tflite::BuiltinOperator builtin_code() const { + return static_cast(GetField(VT_BUILTIN_CODE, 0)); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_DEPRECATED_BUILTIN_CODE, 1) && + VerifyOffset(verifier, VT_CUSTOM_CODE) && + verifier.VerifyString(custom_code()) && + VerifyField(verifier, VT_VERSION, 4) && + VerifyField(verifier, VT_BUILTIN_CODE, 4) && + verifier.EndTable(); + } + OperatorCodeT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(OperatorCodeT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const OperatorCodeT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct OperatorCodeBuilder { + typedef OperatorCode Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_deprecated_builtin_code(int8_t deprecated_builtin_code) { + fbb_.AddElement(OperatorCode::VT_DEPRECATED_BUILTIN_CODE, deprecated_builtin_code, 0); + } + void add_custom_code(::flatbuffers::Offset<::flatbuffers::String> custom_code) { + fbb_.AddOffset(OperatorCode::VT_CUSTOM_CODE, custom_code); + } + void add_version(int32_t version) { + fbb_.AddElement(OperatorCode::VT_VERSION, version, 1); + } + void add_builtin_code(tflite::BuiltinOperator builtin_code) { + fbb_.AddElement(OperatorCode::VT_BUILTIN_CODE, static_cast(builtin_code), 0); + } + explicit OperatorCodeBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateOperatorCode( + ::flatbuffers::FlatBufferBuilder &_fbb, + int8_t deprecated_builtin_code = 0, + ::flatbuffers::Offset<::flatbuffers::String> custom_code = 0, + int32_t version = 1, + tflite::BuiltinOperator builtin_code = tflite::BuiltinOperator_ADD) { + OperatorCodeBuilder builder_(_fbb); + builder_.add_builtin_code(builtin_code); + builder_.add_version(version); + builder_.add_custom_code(custom_code); + builder_.add_deprecated_builtin_code(deprecated_builtin_code); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateOperatorCodeDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + int8_t deprecated_builtin_code = 0, + const char *custom_code = nullptr, + int32_t version = 1, + tflite::BuiltinOperator builtin_code = tflite::BuiltinOperator_ADD) { + auto custom_code__ = custom_code ? _fbb.CreateString(custom_code) : 0; + return tflite::CreateOperatorCode( + _fbb, + deprecated_builtin_code, + custom_code__, + version, + builtin_code); +} + +::flatbuffers::Offset CreateOperatorCode(::flatbuffers::FlatBufferBuilder &_fbb, const OperatorCodeT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct StableHLOCompositeOptionsT : public ::flatbuffers::NativeTable { + typedef StableHLOCompositeOptions TableType; + std::string name{}; + int32_t decomposition_subgraph_index = 0; + std::vector composite_attributes{}; + tflite::CustomOptionsFormat composite_attributes_format = tflite::CustomOptionsFormat_FLEXBUFFERS; + int32_t version = 0; +}; + +struct StableHLOCompositeOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef StableHLOCompositeOptionsT NativeTableType; + typedef StableHLOCompositeOptionsBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_NAME = 4, + VT_DECOMPOSITION_SUBGRAPH_INDEX = 6, + VT_COMPOSITE_ATTRIBUTES = 8, + VT_COMPOSITE_ATTRIBUTES_FORMAT = 10, + VT_VERSION = 12 + }; + const ::flatbuffers::String *name() const { + return GetPointer(VT_NAME); + } + int32_t decomposition_subgraph_index() const { + return GetField(VT_DECOMPOSITION_SUBGRAPH_INDEX, 0); + } + const ::flatbuffers::Vector *composite_attributes() const { + return GetPointer *>(VT_COMPOSITE_ATTRIBUTES); + } + tflite::CustomOptionsFormat composite_attributes_format() const { + return static_cast(GetField(VT_COMPOSITE_ATTRIBUTES_FORMAT, 0)); + } + int32_t version() const { + return GetField(VT_VERSION, 0); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_NAME) && + verifier.VerifyString(name()) && + VerifyField(verifier, VT_DECOMPOSITION_SUBGRAPH_INDEX, 4) && + VerifyOffset(verifier, VT_COMPOSITE_ATTRIBUTES) && + verifier.VerifyVector(composite_attributes()) && + VerifyField(verifier, VT_COMPOSITE_ATTRIBUTES_FORMAT, 1) && + VerifyField(verifier, VT_VERSION, 4) && + verifier.EndTable(); + } + StableHLOCompositeOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(StableHLOCompositeOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StableHLOCompositeOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct StableHLOCompositeOptionsBuilder { + typedef StableHLOCompositeOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_name(::flatbuffers::Offset<::flatbuffers::String> name) { + fbb_.AddOffset(StableHLOCompositeOptions::VT_NAME, name); + } + void add_decomposition_subgraph_index(int32_t decomposition_subgraph_index) { + fbb_.AddElement(StableHLOCompositeOptions::VT_DECOMPOSITION_SUBGRAPH_INDEX, decomposition_subgraph_index, 0); + } + void add_composite_attributes(::flatbuffers::Offset<::flatbuffers::Vector> composite_attributes) { + fbb_.AddOffset(StableHLOCompositeOptions::VT_COMPOSITE_ATTRIBUTES, composite_attributes); + } + void add_composite_attributes_format(tflite::CustomOptionsFormat composite_attributes_format) { + fbb_.AddElement(StableHLOCompositeOptions::VT_COMPOSITE_ATTRIBUTES_FORMAT, static_cast(composite_attributes_format), 0); + } + void add_version(int32_t version) { + fbb_.AddElement(StableHLOCompositeOptions::VT_VERSION, version, 0); + } + explicit StableHLOCompositeOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateStableHLOCompositeOptions( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::String> name = 0, + int32_t decomposition_subgraph_index = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> composite_attributes = 0, + tflite::CustomOptionsFormat composite_attributes_format = tflite::CustomOptionsFormat_FLEXBUFFERS, + int32_t version = 0) { + StableHLOCompositeOptionsBuilder builder_(_fbb); + builder_.add_version(version); + builder_.add_composite_attributes(composite_attributes); + builder_.add_decomposition_subgraph_index(decomposition_subgraph_index); + builder_.add_name(name); + builder_.add_composite_attributes_format(composite_attributes_format); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateStableHLOCompositeOptionsDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const char *name = nullptr, + int32_t decomposition_subgraph_index = 0, + const std::vector *composite_attributes = nullptr, + tflite::CustomOptionsFormat composite_attributes_format = tflite::CustomOptionsFormat_FLEXBUFFERS, + int32_t version = 0) { + auto name__ = name ? _fbb.CreateString(name) : 0; + auto composite_attributes__ = composite_attributes ? _fbb.CreateVector(*composite_attributes) : 0; + return tflite::CreateStableHLOCompositeOptions( + _fbb, + name__, + decomposition_subgraph_index, + composite_attributes__, + composite_attributes_format, + version); +} + +::flatbuffers::Offset CreateStableHLOCompositeOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StableHLOCompositeOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct StablehloShiftLeftOptionsT : public ::flatbuffers::NativeTable { + typedef StablehloShiftLeftOptions TableType; +}; + +struct StablehloShiftLeftOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef StablehloShiftLeftOptionsT NativeTableType; + typedef StablehloShiftLeftOptionsBuilder Builder; + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + StablehloShiftLeftOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(StablehloShiftLeftOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloShiftLeftOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct StablehloShiftLeftOptionsBuilder { + typedef StablehloShiftLeftOptions Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + explicit StablehloShiftLeftOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateStablehloShiftLeftOptions( + ::flatbuffers::FlatBufferBuilder &_fbb) { + StablehloShiftLeftOptionsBuilder builder_(_fbb); + return builder_.Finish(); +} + +::flatbuffers::Offset CreateStablehloShiftLeftOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloShiftLeftOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct OperatorT : public ::flatbuffers::NativeTable { + typedef Operator TableType; + uint32_t opcode_index = 0; + std::vector inputs{}; + std::vector outputs{}; + tflite::BuiltinOptionsUnion builtin_options{}; + std::vector custom_options{}; + tflite::CustomOptionsFormat custom_options_format = tflite::CustomOptionsFormat_FLEXBUFFERS; + std::vector mutating_variable_inputs{}; + std::vector intermediates{}; + uint64_t large_custom_options_offset = 0; + uint64_t large_custom_options_size = 0; + tflite::BuiltinOptions2Union builtin_options_2{}; + int32_t debug_metadata_index = -1; +}; + +struct Operator FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef OperatorT NativeTableType; + typedef OperatorBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_OPCODE_INDEX = 4, + VT_INPUTS = 6, + VT_OUTPUTS = 8, + VT_BUILTIN_OPTIONS_TYPE = 10, + VT_BUILTIN_OPTIONS = 12, + VT_CUSTOM_OPTIONS = 14, + VT_CUSTOM_OPTIONS_FORMAT = 16, + VT_MUTATING_VARIABLE_INPUTS = 18, + VT_INTERMEDIATES = 20, + VT_LARGE_CUSTOM_OPTIONS_OFFSET = 22, + VT_LARGE_CUSTOM_OPTIONS_SIZE = 24, + VT_BUILTIN_OPTIONS_2_TYPE = 26, + VT_BUILTIN_OPTIONS_2 = 28, + VT_DEBUG_METADATA_INDEX = 30 + }; + uint32_t opcode_index() const { + return GetField(VT_OPCODE_INDEX, 0); + } + const ::flatbuffers::Vector *inputs() const { + return GetPointer *>(VT_INPUTS); + } + const ::flatbuffers::Vector *outputs() const { + return GetPointer *>(VT_OUTPUTS); + } + tflite::BuiltinOptions builtin_options_type() const { + return static_cast(GetField(VT_BUILTIN_OPTIONS_TYPE, 0)); + } + const void *builtin_options() const { + return GetPointer(VT_BUILTIN_OPTIONS); + } + template const T *builtin_options_as() const; + const tflite::Conv2DOptions *builtin_options_as_Conv2DOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_Conv2DOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::DepthwiseConv2DOptions *builtin_options_as_DepthwiseConv2DOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_DepthwiseConv2DOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::ConcatEmbeddingsOptions *builtin_options_as_ConcatEmbeddingsOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_ConcatEmbeddingsOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::LSHProjectionOptions *builtin_options_as_LSHProjectionOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_LSHProjectionOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::Pool2DOptions *builtin_options_as_Pool2DOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_Pool2DOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::SVDFOptions *builtin_options_as_SVDFOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_SVDFOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::RNNOptions *builtin_options_as_RNNOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_RNNOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::FullyConnectedOptions *builtin_options_as_FullyConnectedOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_FullyConnectedOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::SoftmaxOptions *builtin_options_as_SoftmaxOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_SoftmaxOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::ConcatenationOptions *builtin_options_as_ConcatenationOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_ConcatenationOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::AddOptions *builtin_options_as_AddOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_AddOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::L2NormOptions *builtin_options_as_L2NormOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_L2NormOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::LocalResponseNormalizationOptions *builtin_options_as_LocalResponseNormalizationOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_LocalResponseNormalizationOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::LSTMOptions *builtin_options_as_LSTMOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_LSTMOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::ResizeBilinearOptions *builtin_options_as_ResizeBilinearOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_ResizeBilinearOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::CallOptions *builtin_options_as_CallOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_CallOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::ReshapeOptions *builtin_options_as_ReshapeOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_ReshapeOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::SkipGramOptions *builtin_options_as_SkipGramOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_SkipGramOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::SpaceToDepthOptions *builtin_options_as_SpaceToDepthOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_SpaceToDepthOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::EmbeddingLookupSparseOptions *builtin_options_as_EmbeddingLookupSparseOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_EmbeddingLookupSparseOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::MulOptions *builtin_options_as_MulOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_MulOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::PadOptions *builtin_options_as_PadOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_PadOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::GatherOptions *builtin_options_as_GatherOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_GatherOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::BatchToSpaceNDOptions *builtin_options_as_BatchToSpaceNDOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_BatchToSpaceNDOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::SpaceToBatchNDOptions *builtin_options_as_SpaceToBatchNDOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_SpaceToBatchNDOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::TransposeOptions *builtin_options_as_TransposeOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_TransposeOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::ReducerOptions *builtin_options_as_ReducerOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_ReducerOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::SubOptions *builtin_options_as_SubOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_SubOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::DivOptions *builtin_options_as_DivOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_DivOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::SqueezeOptions *builtin_options_as_SqueezeOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_SqueezeOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::SequenceRNNOptions *builtin_options_as_SequenceRNNOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_SequenceRNNOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::StridedSliceOptions *builtin_options_as_StridedSliceOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_StridedSliceOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::ExpOptions *builtin_options_as_ExpOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_ExpOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::TopKV2Options *builtin_options_as_TopKV2Options() const { + return builtin_options_type() == tflite::BuiltinOptions_TopKV2Options ? static_cast(builtin_options()) : nullptr; + } + const tflite::SplitOptions *builtin_options_as_SplitOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_SplitOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::LogSoftmaxOptions *builtin_options_as_LogSoftmaxOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_LogSoftmaxOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::CastOptions *builtin_options_as_CastOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_CastOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::DequantizeOptions *builtin_options_as_DequantizeOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_DequantizeOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::MaximumMinimumOptions *builtin_options_as_MaximumMinimumOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_MaximumMinimumOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::ArgMaxOptions *builtin_options_as_ArgMaxOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_ArgMaxOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::LessOptions *builtin_options_as_LessOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_LessOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::NegOptions *builtin_options_as_NegOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_NegOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::PadV2Options *builtin_options_as_PadV2Options() const { + return builtin_options_type() == tflite::BuiltinOptions_PadV2Options ? static_cast(builtin_options()) : nullptr; + } + const tflite::GreaterOptions *builtin_options_as_GreaterOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_GreaterOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::GreaterEqualOptions *builtin_options_as_GreaterEqualOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_GreaterEqualOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::LessEqualOptions *builtin_options_as_LessEqualOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_LessEqualOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::SelectOptions *builtin_options_as_SelectOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_SelectOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::SliceOptions *builtin_options_as_SliceOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_SliceOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::TransposeConvOptions *builtin_options_as_TransposeConvOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_TransposeConvOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::SparseToDenseOptions *builtin_options_as_SparseToDenseOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_SparseToDenseOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::TileOptions *builtin_options_as_TileOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_TileOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::ExpandDimsOptions *builtin_options_as_ExpandDimsOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_ExpandDimsOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::EqualOptions *builtin_options_as_EqualOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_EqualOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::NotEqualOptions *builtin_options_as_NotEqualOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_NotEqualOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::ShapeOptions *builtin_options_as_ShapeOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_ShapeOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::PowOptions *builtin_options_as_PowOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_PowOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::ArgMinOptions *builtin_options_as_ArgMinOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_ArgMinOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::FakeQuantOptions *builtin_options_as_FakeQuantOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_FakeQuantOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::PackOptions *builtin_options_as_PackOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_PackOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::LogicalOrOptions *builtin_options_as_LogicalOrOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_LogicalOrOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::OneHotOptions *builtin_options_as_OneHotOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_OneHotOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::LogicalAndOptions *builtin_options_as_LogicalAndOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_LogicalAndOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::LogicalNotOptions *builtin_options_as_LogicalNotOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_LogicalNotOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::UnpackOptions *builtin_options_as_UnpackOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_UnpackOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::FloorDivOptions *builtin_options_as_FloorDivOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_FloorDivOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::SquareOptions *builtin_options_as_SquareOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_SquareOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::ZerosLikeOptions *builtin_options_as_ZerosLikeOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_ZerosLikeOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::FillOptions *builtin_options_as_FillOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_FillOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::BidirectionalSequenceLSTMOptions *builtin_options_as_BidirectionalSequenceLSTMOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_BidirectionalSequenceLSTMOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::BidirectionalSequenceRNNOptions *builtin_options_as_BidirectionalSequenceRNNOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_BidirectionalSequenceRNNOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::UnidirectionalSequenceLSTMOptions *builtin_options_as_UnidirectionalSequenceLSTMOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_UnidirectionalSequenceLSTMOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::FloorModOptions *builtin_options_as_FloorModOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_FloorModOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::RangeOptions *builtin_options_as_RangeOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_RangeOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::ResizeNearestNeighborOptions *builtin_options_as_ResizeNearestNeighborOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_ResizeNearestNeighborOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::LeakyReluOptions *builtin_options_as_LeakyReluOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_LeakyReluOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::SquaredDifferenceOptions *builtin_options_as_SquaredDifferenceOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_SquaredDifferenceOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::MirrorPadOptions *builtin_options_as_MirrorPadOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_MirrorPadOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::AbsOptions *builtin_options_as_AbsOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_AbsOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::SplitVOptions *builtin_options_as_SplitVOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_SplitVOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::UniqueOptions *builtin_options_as_UniqueOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_UniqueOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::ReverseV2Options *builtin_options_as_ReverseV2Options() const { + return builtin_options_type() == tflite::BuiltinOptions_ReverseV2Options ? static_cast(builtin_options()) : nullptr; + } + const tflite::AddNOptions *builtin_options_as_AddNOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_AddNOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::GatherNdOptions *builtin_options_as_GatherNdOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_GatherNdOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::CosOptions *builtin_options_as_CosOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_CosOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::WhereOptions *builtin_options_as_WhereOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_WhereOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::RankOptions *builtin_options_as_RankOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_RankOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::ReverseSequenceOptions *builtin_options_as_ReverseSequenceOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_ReverseSequenceOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::MatrixDiagOptions *builtin_options_as_MatrixDiagOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_MatrixDiagOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::QuantizeOptions *builtin_options_as_QuantizeOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_QuantizeOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::MatrixSetDiagOptions *builtin_options_as_MatrixSetDiagOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_MatrixSetDiagOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::HardSwishOptions *builtin_options_as_HardSwishOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_HardSwishOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::IfOptions *builtin_options_as_IfOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_IfOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::WhileOptions *builtin_options_as_WhileOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_WhileOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::DepthToSpaceOptions *builtin_options_as_DepthToSpaceOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_DepthToSpaceOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::NonMaxSuppressionV4Options *builtin_options_as_NonMaxSuppressionV4Options() const { + return builtin_options_type() == tflite::BuiltinOptions_NonMaxSuppressionV4Options ? static_cast(builtin_options()) : nullptr; + } + const tflite::NonMaxSuppressionV5Options *builtin_options_as_NonMaxSuppressionV5Options() const { + return builtin_options_type() == tflite::BuiltinOptions_NonMaxSuppressionV5Options ? static_cast(builtin_options()) : nullptr; + } + const tflite::ScatterNdOptions *builtin_options_as_ScatterNdOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_ScatterNdOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::SelectV2Options *builtin_options_as_SelectV2Options() const { + return builtin_options_type() == tflite::BuiltinOptions_SelectV2Options ? static_cast(builtin_options()) : nullptr; + } + const tflite::DensifyOptions *builtin_options_as_DensifyOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_DensifyOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::SegmentSumOptions *builtin_options_as_SegmentSumOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_SegmentSumOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::BatchMatMulOptions *builtin_options_as_BatchMatMulOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_BatchMatMulOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::CumsumOptions *builtin_options_as_CumsumOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_CumsumOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::CallOnceOptions *builtin_options_as_CallOnceOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_CallOnceOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::BroadcastToOptions *builtin_options_as_BroadcastToOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_BroadcastToOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::Rfft2dOptions *builtin_options_as_Rfft2dOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_Rfft2dOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::Conv3DOptions *builtin_options_as_Conv3DOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_Conv3DOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::HashtableOptions *builtin_options_as_HashtableOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_HashtableOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::HashtableFindOptions *builtin_options_as_HashtableFindOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_HashtableFindOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::HashtableImportOptions *builtin_options_as_HashtableImportOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_HashtableImportOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::HashtableSizeOptions *builtin_options_as_HashtableSizeOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_HashtableSizeOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::VarHandleOptions *builtin_options_as_VarHandleOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_VarHandleOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::ReadVariableOptions *builtin_options_as_ReadVariableOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_ReadVariableOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::AssignVariableOptions *builtin_options_as_AssignVariableOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_AssignVariableOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::RandomOptions *builtin_options_as_RandomOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_RandomOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::BucketizeOptions *builtin_options_as_BucketizeOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_BucketizeOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::GeluOptions *builtin_options_as_GeluOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_GeluOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::DynamicUpdateSliceOptions *builtin_options_as_DynamicUpdateSliceOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_DynamicUpdateSliceOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::UnsortedSegmentProdOptions *builtin_options_as_UnsortedSegmentProdOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_UnsortedSegmentProdOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::UnsortedSegmentMaxOptions *builtin_options_as_UnsortedSegmentMaxOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_UnsortedSegmentMaxOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::UnsortedSegmentMinOptions *builtin_options_as_UnsortedSegmentMinOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_UnsortedSegmentMinOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::UnsortedSegmentSumOptions *builtin_options_as_UnsortedSegmentSumOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_UnsortedSegmentSumOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::ATan2Options *builtin_options_as_ATan2Options() const { + return builtin_options_type() == tflite::BuiltinOptions_ATan2Options ? static_cast(builtin_options()) : nullptr; + } + const tflite::SignOptions *builtin_options_as_SignOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_SignOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::BitcastOptions *builtin_options_as_BitcastOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_BitcastOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::BitwiseXorOptions *builtin_options_as_BitwiseXorOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_BitwiseXorOptions ? static_cast(builtin_options()) : nullptr; + } + const tflite::RightShiftOptions *builtin_options_as_RightShiftOptions() const { + return builtin_options_type() == tflite::BuiltinOptions_RightShiftOptions ? static_cast(builtin_options()) : nullptr; + } + const ::flatbuffers::Vector *custom_options() const { + return GetPointer *>(VT_CUSTOM_OPTIONS); + } + tflite::CustomOptionsFormat custom_options_format() const { + return static_cast(GetField(VT_CUSTOM_OPTIONS_FORMAT, 0)); + } + const ::flatbuffers::Vector *mutating_variable_inputs() const { + return GetPointer *>(VT_MUTATING_VARIABLE_INPUTS); + } + const ::flatbuffers::Vector *intermediates() const { + return GetPointer *>(VT_INTERMEDIATES); + } + uint64_t large_custom_options_offset() const { + return GetField(VT_LARGE_CUSTOM_OPTIONS_OFFSET, 0); + } + uint64_t large_custom_options_size() const { + return GetField(VT_LARGE_CUSTOM_OPTIONS_SIZE, 0); + } + tflite::BuiltinOptions2 builtin_options_2_type() const { + return static_cast(GetField(VT_BUILTIN_OPTIONS_2_TYPE, 0)); + } + const void *builtin_options_2() const { + return GetPointer(VT_BUILTIN_OPTIONS_2); + } + template const T *builtin_options_2_as() const; + const tflite::StablehloConcatenateOptions *builtin_options_2_as_StablehloConcatenateOptions() const { + return builtin_options_2_type() == tflite::BuiltinOptions2_StablehloConcatenateOptions ? static_cast(builtin_options_2()) : nullptr; + } + const tflite::StablehloBroadcastInDimOptions *builtin_options_2_as_StablehloBroadcastInDimOptions() const { + return builtin_options_2_type() == tflite::BuiltinOptions2_StablehloBroadcastInDimOptions ? static_cast(builtin_options_2()) : nullptr; + } + const tflite::StablehloSliceOptions *builtin_options_2_as_StablehloSliceOptions() const { + return builtin_options_2_type() == tflite::BuiltinOptions2_StablehloSliceOptions ? static_cast(builtin_options_2()) : nullptr; + } + const tflite::StablehloConvolutionOptions *builtin_options_2_as_StablehloConvolutionOptions() const { + return builtin_options_2_type() == tflite::BuiltinOptions2_StablehloConvolutionOptions ? static_cast(builtin_options_2()) : nullptr; + } + const tflite::StablehloCustomCallOptions *builtin_options_2_as_StablehloCustomCallOptions() const { + return builtin_options_2_type() == tflite::BuiltinOptions2_StablehloCustomCallOptions ? static_cast(builtin_options_2()) : nullptr; + } + const tflite::StablehloReduceOptions *builtin_options_2_as_StablehloReduceOptions() const { + return builtin_options_2_type() == tflite::BuiltinOptions2_StablehloReduceOptions ? static_cast(builtin_options_2()) : nullptr; + } + const tflite::StablehloScatterOptions *builtin_options_2_as_StablehloScatterOptions() const { + return builtin_options_2_type() == tflite::BuiltinOptions2_StablehloScatterOptions ? static_cast(builtin_options_2()) : nullptr; + } + const tflite::StablehloCompareOptions *builtin_options_2_as_StablehloCompareOptions() const { + return builtin_options_2_type() == tflite::BuiltinOptions2_StablehloCompareOptions ? static_cast(builtin_options_2()) : nullptr; + } + const tflite::StablehloDynamicSliceOptions *builtin_options_2_as_StablehloDynamicSliceOptions() const { + return builtin_options_2_type() == tflite::BuiltinOptions2_StablehloDynamicSliceOptions ? static_cast(builtin_options_2()) : nullptr; + } + const tflite::StablehloPadOptions *builtin_options_2_as_StablehloPadOptions() const { + return builtin_options_2_type() == tflite::BuiltinOptions2_StablehloPadOptions ? static_cast(builtin_options_2()) : nullptr; + } + const tflite::StablehloIotaOptions *builtin_options_2_as_StablehloIotaOptions() const { + return builtin_options_2_type() == tflite::BuiltinOptions2_StablehloIotaOptions ? static_cast(builtin_options_2()) : nullptr; + } + const tflite::StablehloDotGeneralOptions *builtin_options_2_as_StablehloDotGeneralOptions() const { + return builtin_options_2_type() == tflite::BuiltinOptions2_StablehloDotGeneralOptions ? static_cast(builtin_options_2()) : nullptr; + } + const tflite::StablehloReduceWindowOptions *builtin_options_2_as_StablehloReduceWindowOptions() const { + return builtin_options_2_type() == tflite::BuiltinOptions2_StablehloReduceWindowOptions ? static_cast(builtin_options_2()) : nullptr; + } + const tflite::StablehloSortOptions *builtin_options_2_as_StablehloSortOptions() const { + return builtin_options_2_type() == tflite::BuiltinOptions2_StablehloSortOptions ? static_cast(builtin_options_2()) : nullptr; + } + const tflite::StablehloWhileOptions *builtin_options_2_as_StablehloWhileOptions() const { + return builtin_options_2_type() == tflite::BuiltinOptions2_StablehloWhileOptions ? static_cast(builtin_options_2()) : nullptr; + } + const tflite::StablehloGatherOptions *builtin_options_2_as_StablehloGatherOptions() const { + return builtin_options_2_type() == tflite::BuiltinOptions2_StablehloGatherOptions ? static_cast(builtin_options_2()) : nullptr; + } + const tflite::StablehloTransposeOptions *builtin_options_2_as_StablehloTransposeOptions() const { + return builtin_options_2_type() == tflite::BuiltinOptions2_StablehloTransposeOptions ? static_cast(builtin_options_2()) : nullptr; + } + const tflite::DilateOptions *builtin_options_2_as_DilateOptions() const { + return builtin_options_2_type() == tflite::BuiltinOptions2_DilateOptions ? static_cast(builtin_options_2()) : nullptr; + } + const tflite::StablehloRngBitGeneratorOptions *builtin_options_2_as_StablehloRngBitGeneratorOptions() const { + return builtin_options_2_type() == tflite::BuiltinOptions2_StablehloRngBitGeneratorOptions ? static_cast(builtin_options_2()) : nullptr; + } + const tflite::ReduceWindowOptions *builtin_options_2_as_ReduceWindowOptions() const { + return builtin_options_2_type() == tflite::BuiltinOptions2_ReduceWindowOptions ? static_cast(builtin_options_2()) : nullptr; + } + const tflite::StableHLOCompositeOptions *builtin_options_2_as_StableHLOCompositeOptions() const { + return builtin_options_2_type() == tflite::BuiltinOptions2_StableHLOCompositeOptions ? static_cast(builtin_options_2()) : nullptr; + } + const tflite::StablehloShiftLeftOptions *builtin_options_2_as_StablehloShiftLeftOptions() const { + return builtin_options_2_type() == tflite::BuiltinOptions2_StablehloShiftLeftOptions ? static_cast(builtin_options_2()) : nullptr; + } + const tflite::StablehloCaseOptions *builtin_options_2_as_StablehloCaseOptions() const { + return builtin_options_2_type() == tflite::BuiltinOptions2_StablehloCaseOptions ? static_cast(builtin_options_2()) : nullptr; + } + int32_t debug_metadata_index() const { + return GetField(VT_DEBUG_METADATA_INDEX, -1); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_OPCODE_INDEX, 4) && + VerifyOffset(verifier, VT_INPUTS) && + verifier.VerifyVector(inputs()) && + VerifyOffset(verifier, VT_OUTPUTS) && + verifier.VerifyVector(outputs()) && + VerifyField(verifier, VT_BUILTIN_OPTIONS_TYPE, 1) && + VerifyOffset(verifier, VT_BUILTIN_OPTIONS) && + VerifyBuiltinOptions(verifier, builtin_options(), builtin_options_type()) && + VerifyOffset(verifier, VT_CUSTOM_OPTIONS) && + verifier.VerifyVector(custom_options()) && + VerifyField(verifier, VT_CUSTOM_OPTIONS_FORMAT, 1) && + VerifyOffset(verifier, VT_MUTATING_VARIABLE_INPUTS) && + verifier.VerifyVector(mutating_variable_inputs()) && + VerifyOffset(verifier, VT_INTERMEDIATES) && + verifier.VerifyVector(intermediates()) && + VerifyField(verifier, VT_LARGE_CUSTOM_OPTIONS_OFFSET, 8) && + VerifyField(verifier, VT_LARGE_CUSTOM_OPTIONS_SIZE, 8) && + VerifyField(verifier, VT_BUILTIN_OPTIONS_2_TYPE, 1) && + VerifyOffset(verifier, VT_BUILTIN_OPTIONS_2) && + VerifyBuiltinOptions2(verifier, builtin_options_2(), builtin_options_2_type()) && + VerifyField(verifier, VT_DEBUG_METADATA_INDEX, 4) && + verifier.EndTable(); + } + OperatorT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(OperatorT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const OperatorT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +template<> inline const tflite::Conv2DOptions *Operator::builtin_options_as() const { + return builtin_options_as_Conv2DOptions(); +} + +template<> inline const tflite::DepthwiseConv2DOptions *Operator::builtin_options_as() const { + return builtin_options_as_DepthwiseConv2DOptions(); +} + +template<> inline const tflite::ConcatEmbeddingsOptions *Operator::builtin_options_as() const { + return builtin_options_as_ConcatEmbeddingsOptions(); +} + +template<> inline const tflite::LSHProjectionOptions *Operator::builtin_options_as() const { + return builtin_options_as_LSHProjectionOptions(); +} + +template<> inline const tflite::Pool2DOptions *Operator::builtin_options_as() const { + return builtin_options_as_Pool2DOptions(); +} + +template<> inline const tflite::SVDFOptions *Operator::builtin_options_as() const { + return builtin_options_as_SVDFOptions(); +} + +template<> inline const tflite::RNNOptions *Operator::builtin_options_as() const { + return builtin_options_as_RNNOptions(); +} + +template<> inline const tflite::FullyConnectedOptions *Operator::builtin_options_as() const { + return builtin_options_as_FullyConnectedOptions(); +} + +template<> inline const tflite::SoftmaxOptions *Operator::builtin_options_as() const { + return builtin_options_as_SoftmaxOptions(); +} + +template<> inline const tflite::ConcatenationOptions *Operator::builtin_options_as() const { + return builtin_options_as_ConcatenationOptions(); +} + +template<> inline const tflite::AddOptions *Operator::builtin_options_as() const { + return builtin_options_as_AddOptions(); +} + +template<> inline const tflite::L2NormOptions *Operator::builtin_options_as() const { + return builtin_options_as_L2NormOptions(); +} + +template<> inline const tflite::LocalResponseNormalizationOptions *Operator::builtin_options_as() const { + return builtin_options_as_LocalResponseNormalizationOptions(); +} + +template<> inline const tflite::LSTMOptions *Operator::builtin_options_as() const { + return builtin_options_as_LSTMOptions(); +} + +template<> inline const tflite::ResizeBilinearOptions *Operator::builtin_options_as() const { + return builtin_options_as_ResizeBilinearOptions(); +} + +template<> inline const tflite::CallOptions *Operator::builtin_options_as() const { + return builtin_options_as_CallOptions(); +} + +template<> inline const tflite::ReshapeOptions *Operator::builtin_options_as() const { + return builtin_options_as_ReshapeOptions(); +} + +template<> inline const tflite::SkipGramOptions *Operator::builtin_options_as() const { + return builtin_options_as_SkipGramOptions(); +} + +template<> inline const tflite::SpaceToDepthOptions *Operator::builtin_options_as() const { + return builtin_options_as_SpaceToDepthOptions(); +} + +template<> inline const tflite::EmbeddingLookupSparseOptions *Operator::builtin_options_as() const { + return builtin_options_as_EmbeddingLookupSparseOptions(); +} + +template<> inline const tflite::MulOptions *Operator::builtin_options_as() const { + return builtin_options_as_MulOptions(); +} + +template<> inline const tflite::PadOptions *Operator::builtin_options_as() const { + return builtin_options_as_PadOptions(); +} + +template<> inline const tflite::GatherOptions *Operator::builtin_options_as() const { + return builtin_options_as_GatherOptions(); +} + +template<> inline const tflite::BatchToSpaceNDOptions *Operator::builtin_options_as() const { + return builtin_options_as_BatchToSpaceNDOptions(); +} + +template<> inline const tflite::SpaceToBatchNDOptions *Operator::builtin_options_as() const { + return builtin_options_as_SpaceToBatchNDOptions(); +} + +template<> inline const tflite::TransposeOptions *Operator::builtin_options_as() const { + return builtin_options_as_TransposeOptions(); +} + +template<> inline const tflite::ReducerOptions *Operator::builtin_options_as() const { + return builtin_options_as_ReducerOptions(); +} + +template<> inline const tflite::SubOptions *Operator::builtin_options_as() const { + return builtin_options_as_SubOptions(); +} + +template<> inline const tflite::DivOptions *Operator::builtin_options_as() const { + return builtin_options_as_DivOptions(); +} + +template<> inline const tflite::SqueezeOptions *Operator::builtin_options_as() const { + return builtin_options_as_SqueezeOptions(); +} + +template<> inline const tflite::SequenceRNNOptions *Operator::builtin_options_as() const { + return builtin_options_as_SequenceRNNOptions(); +} + +template<> inline const tflite::StridedSliceOptions *Operator::builtin_options_as() const { + return builtin_options_as_StridedSliceOptions(); +} + +template<> inline const tflite::ExpOptions *Operator::builtin_options_as() const { + return builtin_options_as_ExpOptions(); +} + +template<> inline const tflite::TopKV2Options *Operator::builtin_options_as() const { + return builtin_options_as_TopKV2Options(); +} + +template<> inline const tflite::SplitOptions *Operator::builtin_options_as() const { + return builtin_options_as_SplitOptions(); +} + +template<> inline const tflite::LogSoftmaxOptions *Operator::builtin_options_as() const { + return builtin_options_as_LogSoftmaxOptions(); +} + +template<> inline const tflite::CastOptions *Operator::builtin_options_as() const { + return builtin_options_as_CastOptions(); +} + +template<> inline const tflite::DequantizeOptions *Operator::builtin_options_as() const { + return builtin_options_as_DequantizeOptions(); +} + +template<> inline const tflite::MaximumMinimumOptions *Operator::builtin_options_as() const { + return builtin_options_as_MaximumMinimumOptions(); +} + +template<> inline const tflite::ArgMaxOptions *Operator::builtin_options_as() const { + return builtin_options_as_ArgMaxOptions(); +} + +template<> inline const tflite::LessOptions *Operator::builtin_options_as() const { + return builtin_options_as_LessOptions(); +} + +template<> inline const tflite::NegOptions *Operator::builtin_options_as() const { + return builtin_options_as_NegOptions(); +} + +template<> inline const tflite::PadV2Options *Operator::builtin_options_as() const { + return builtin_options_as_PadV2Options(); +} + +template<> inline const tflite::GreaterOptions *Operator::builtin_options_as() const { + return builtin_options_as_GreaterOptions(); +} + +template<> inline const tflite::GreaterEqualOptions *Operator::builtin_options_as() const { + return builtin_options_as_GreaterEqualOptions(); +} + +template<> inline const tflite::LessEqualOptions *Operator::builtin_options_as() const { + return builtin_options_as_LessEqualOptions(); +} + +template<> inline const tflite::SelectOptions *Operator::builtin_options_as() const { + return builtin_options_as_SelectOptions(); +} + +template<> inline const tflite::SliceOptions *Operator::builtin_options_as() const { + return builtin_options_as_SliceOptions(); +} + +template<> inline const tflite::TransposeConvOptions *Operator::builtin_options_as() const { + return builtin_options_as_TransposeConvOptions(); +} + +template<> inline const tflite::SparseToDenseOptions *Operator::builtin_options_as() const { + return builtin_options_as_SparseToDenseOptions(); +} + +template<> inline const tflite::TileOptions *Operator::builtin_options_as() const { + return builtin_options_as_TileOptions(); +} + +template<> inline const tflite::ExpandDimsOptions *Operator::builtin_options_as() const { + return builtin_options_as_ExpandDimsOptions(); +} + +template<> inline const tflite::EqualOptions *Operator::builtin_options_as() const { + return builtin_options_as_EqualOptions(); +} + +template<> inline const tflite::NotEqualOptions *Operator::builtin_options_as() const { + return builtin_options_as_NotEqualOptions(); +} + +template<> inline const tflite::ShapeOptions *Operator::builtin_options_as() const { + return builtin_options_as_ShapeOptions(); +} + +template<> inline const tflite::PowOptions *Operator::builtin_options_as() const { + return builtin_options_as_PowOptions(); +} + +template<> inline const tflite::ArgMinOptions *Operator::builtin_options_as() const { + return builtin_options_as_ArgMinOptions(); +} + +template<> inline const tflite::FakeQuantOptions *Operator::builtin_options_as() const { + return builtin_options_as_FakeQuantOptions(); +} + +template<> inline const tflite::PackOptions *Operator::builtin_options_as() const { + return builtin_options_as_PackOptions(); +} + +template<> inline const tflite::LogicalOrOptions *Operator::builtin_options_as() const { + return builtin_options_as_LogicalOrOptions(); +} + +template<> inline const tflite::OneHotOptions *Operator::builtin_options_as() const { + return builtin_options_as_OneHotOptions(); +} + +template<> inline const tflite::LogicalAndOptions *Operator::builtin_options_as() const { + return builtin_options_as_LogicalAndOptions(); +} + +template<> inline const tflite::LogicalNotOptions *Operator::builtin_options_as() const { + return builtin_options_as_LogicalNotOptions(); +} + +template<> inline const tflite::UnpackOptions *Operator::builtin_options_as() const { + return builtin_options_as_UnpackOptions(); +} + +template<> inline const tflite::FloorDivOptions *Operator::builtin_options_as() const { + return builtin_options_as_FloorDivOptions(); +} + +template<> inline const tflite::SquareOptions *Operator::builtin_options_as() const { + return builtin_options_as_SquareOptions(); +} + +template<> inline const tflite::ZerosLikeOptions *Operator::builtin_options_as() const { + return builtin_options_as_ZerosLikeOptions(); +} + +template<> inline const tflite::FillOptions *Operator::builtin_options_as() const { + return builtin_options_as_FillOptions(); +} + +template<> inline const tflite::BidirectionalSequenceLSTMOptions *Operator::builtin_options_as() const { + return builtin_options_as_BidirectionalSequenceLSTMOptions(); +} + +template<> inline const tflite::BidirectionalSequenceRNNOptions *Operator::builtin_options_as() const { + return builtin_options_as_BidirectionalSequenceRNNOptions(); +} + +template<> inline const tflite::UnidirectionalSequenceLSTMOptions *Operator::builtin_options_as() const { + return builtin_options_as_UnidirectionalSequenceLSTMOptions(); +} + +template<> inline const tflite::FloorModOptions *Operator::builtin_options_as() const { + return builtin_options_as_FloorModOptions(); +} + +template<> inline const tflite::RangeOptions *Operator::builtin_options_as() const { + return builtin_options_as_RangeOptions(); +} + +template<> inline const tflite::ResizeNearestNeighborOptions *Operator::builtin_options_as() const { + return builtin_options_as_ResizeNearestNeighborOptions(); +} + +template<> inline const tflite::LeakyReluOptions *Operator::builtin_options_as() const { + return builtin_options_as_LeakyReluOptions(); +} + +template<> inline const tflite::SquaredDifferenceOptions *Operator::builtin_options_as() const { + return builtin_options_as_SquaredDifferenceOptions(); +} + +template<> inline const tflite::MirrorPadOptions *Operator::builtin_options_as() const { + return builtin_options_as_MirrorPadOptions(); +} + +template<> inline const tflite::AbsOptions *Operator::builtin_options_as() const { + return builtin_options_as_AbsOptions(); +} + +template<> inline const tflite::SplitVOptions *Operator::builtin_options_as() const { + return builtin_options_as_SplitVOptions(); +} + +template<> inline const tflite::UniqueOptions *Operator::builtin_options_as() const { + return builtin_options_as_UniqueOptions(); +} + +template<> inline const tflite::ReverseV2Options *Operator::builtin_options_as() const { + return builtin_options_as_ReverseV2Options(); +} + +template<> inline const tflite::AddNOptions *Operator::builtin_options_as() const { + return builtin_options_as_AddNOptions(); +} + +template<> inline const tflite::GatherNdOptions *Operator::builtin_options_as() const { + return builtin_options_as_GatherNdOptions(); +} + +template<> inline const tflite::CosOptions *Operator::builtin_options_as() const { + return builtin_options_as_CosOptions(); +} + +template<> inline const tflite::WhereOptions *Operator::builtin_options_as() const { + return builtin_options_as_WhereOptions(); +} + +template<> inline const tflite::RankOptions *Operator::builtin_options_as() const { + return builtin_options_as_RankOptions(); +} + +template<> inline const tflite::ReverseSequenceOptions *Operator::builtin_options_as() const { + return builtin_options_as_ReverseSequenceOptions(); +} + +template<> inline const tflite::MatrixDiagOptions *Operator::builtin_options_as() const { + return builtin_options_as_MatrixDiagOptions(); +} + +template<> inline const tflite::QuantizeOptions *Operator::builtin_options_as() const { + return builtin_options_as_QuantizeOptions(); +} + +template<> inline const tflite::MatrixSetDiagOptions *Operator::builtin_options_as() const { + return builtin_options_as_MatrixSetDiagOptions(); +} + +template<> inline const tflite::HardSwishOptions *Operator::builtin_options_as() const { + return builtin_options_as_HardSwishOptions(); +} + +template<> inline const tflite::IfOptions *Operator::builtin_options_as() const { + return builtin_options_as_IfOptions(); +} + +template<> inline const tflite::WhileOptions *Operator::builtin_options_as() const { + return builtin_options_as_WhileOptions(); +} + +template<> inline const tflite::DepthToSpaceOptions *Operator::builtin_options_as() const { + return builtin_options_as_DepthToSpaceOptions(); +} + +template<> inline const tflite::NonMaxSuppressionV4Options *Operator::builtin_options_as() const { + return builtin_options_as_NonMaxSuppressionV4Options(); +} + +template<> inline const tflite::NonMaxSuppressionV5Options *Operator::builtin_options_as() const { + return builtin_options_as_NonMaxSuppressionV5Options(); +} + +template<> inline const tflite::ScatterNdOptions *Operator::builtin_options_as() const { + return builtin_options_as_ScatterNdOptions(); +} + +template<> inline const tflite::SelectV2Options *Operator::builtin_options_as() const { + return builtin_options_as_SelectV2Options(); +} + +template<> inline const tflite::DensifyOptions *Operator::builtin_options_as() const { + return builtin_options_as_DensifyOptions(); +} + +template<> inline const tflite::SegmentSumOptions *Operator::builtin_options_as() const { + return builtin_options_as_SegmentSumOptions(); +} + +template<> inline const tflite::BatchMatMulOptions *Operator::builtin_options_as() const { + return builtin_options_as_BatchMatMulOptions(); +} + +template<> inline const tflite::CumsumOptions *Operator::builtin_options_as() const { + return builtin_options_as_CumsumOptions(); +} + +template<> inline const tflite::CallOnceOptions *Operator::builtin_options_as() const { + return builtin_options_as_CallOnceOptions(); +} + +template<> inline const tflite::BroadcastToOptions *Operator::builtin_options_as() const { + return builtin_options_as_BroadcastToOptions(); +} + +template<> inline const tflite::Rfft2dOptions *Operator::builtin_options_as() const { + return builtin_options_as_Rfft2dOptions(); +} + +template<> inline const tflite::Conv3DOptions *Operator::builtin_options_as() const { + return builtin_options_as_Conv3DOptions(); +} + +template<> inline const tflite::HashtableOptions *Operator::builtin_options_as() const { + return builtin_options_as_HashtableOptions(); +} + +template<> inline const tflite::HashtableFindOptions *Operator::builtin_options_as() const { + return builtin_options_as_HashtableFindOptions(); +} + +template<> inline const tflite::HashtableImportOptions *Operator::builtin_options_as() const { + return builtin_options_as_HashtableImportOptions(); +} + +template<> inline const tflite::HashtableSizeOptions *Operator::builtin_options_as() const { + return builtin_options_as_HashtableSizeOptions(); +} + +template<> inline const tflite::VarHandleOptions *Operator::builtin_options_as() const { + return builtin_options_as_VarHandleOptions(); +} + +template<> inline const tflite::ReadVariableOptions *Operator::builtin_options_as() const { + return builtin_options_as_ReadVariableOptions(); +} + +template<> inline const tflite::AssignVariableOptions *Operator::builtin_options_as() const { + return builtin_options_as_AssignVariableOptions(); +} + +template<> inline const tflite::RandomOptions *Operator::builtin_options_as() const { + return builtin_options_as_RandomOptions(); +} + +template<> inline const tflite::BucketizeOptions *Operator::builtin_options_as() const { + return builtin_options_as_BucketizeOptions(); +} + +template<> inline const tflite::GeluOptions *Operator::builtin_options_as() const { + return builtin_options_as_GeluOptions(); +} + +template<> inline const tflite::DynamicUpdateSliceOptions *Operator::builtin_options_as() const { + return builtin_options_as_DynamicUpdateSliceOptions(); +} + +template<> inline const tflite::UnsortedSegmentProdOptions *Operator::builtin_options_as() const { + return builtin_options_as_UnsortedSegmentProdOptions(); +} + +template<> inline const tflite::UnsortedSegmentMaxOptions *Operator::builtin_options_as() const { + return builtin_options_as_UnsortedSegmentMaxOptions(); +} + +template<> inline const tflite::UnsortedSegmentMinOptions *Operator::builtin_options_as() const { + return builtin_options_as_UnsortedSegmentMinOptions(); +} + +template<> inline const tflite::UnsortedSegmentSumOptions *Operator::builtin_options_as() const { + return builtin_options_as_UnsortedSegmentSumOptions(); +} + +template<> inline const tflite::ATan2Options *Operator::builtin_options_as() const { + return builtin_options_as_ATan2Options(); +} + +template<> inline const tflite::SignOptions *Operator::builtin_options_as() const { + return builtin_options_as_SignOptions(); +} + +template<> inline const tflite::BitcastOptions *Operator::builtin_options_as() const { + return builtin_options_as_BitcastOptions(); +} + +template<> inline const tflite::BitwiseXorOptions *Operator::builtin_options_as() const { + return builtin_options_as_BitwiseXorOptions(); +} + +template<> inline const tflite::RightShiftOptions *Operator::builtin_options_as() const { + return builtin_options_as_RightShiftOptions(); +} + +template<> inline const tflite::StablehloConcatenateOptions *Operator::builtin_options_2_as() const { + return builtin_options_2_as_StablehloConcatenateOptions(); +} + +template<> inline const tflite::StablehloBroadcastInDimOptions *Operator::builtin_options_2_as() const { + return builtin_options_2_as_StablehloBroadcastInDimOptions(); +} + +template<> inline const tflite::StablehloSliceOptions *Operator::builtin_options_2_as() const { + return builtin_options_2_as_StablehloSliceOptions(); +} + +template<> inline const tflite::StablehloConvolutionOptions *Operator::builtin_options_2_as() const { + return builtin_options_2_as_StablehloConvolutionOptions(); +} + +template<> inline const tflite::StablehloCustomCallOptions *Operator::builtin_options_2_as() const { + return builtin_options_2_as_StablehloCustomCallOptions(); +} + +template<> inline const tflite::StablehloReduceOptions *Operator::builtin_options_2_as() const { + return builtin_options_2_as_StablehloReduceOptions(); +} + +template<> inline const tflite::StablehloScatterOptions *Operator::builtin_options_2_as() const { + return builtin_options_2_as_StablehloScatterOptions(); +} + +template<> inline const tflite::StablehloCompareOptions *Operator::builtin_options_2_as() const { + return builtin_options_2_as_StablehloCompareOptions(); +} + +template<> inline const tflite::StablehloDynamicSliceOptions *Operator::builtin_options_2_as() const { + return builtin_options_2_as_StablehloDynamicSliceOptions(); +} + +template<> inline const tflite::StablehloPadOptions *Operator::builtin_options_2_as() const { + return builtin_options_2_as_StablehloPadOptions(); +} + +template<> inline const tflite::StablehloIotaOptions *Operator::builtin_options_2_as() const { + return builtin_options_2_as_StablehloIotaOptions(); +} + +template<> inline const tflite::StablehloDotGeneralOptions *Operator::builtin_options_2_as() const { + return builtin_options_2_as_StablehloDotGeneralOptions(); +} + +template<> inline const tflite::StablehloReduceWindowOptions *Operator::builtin_options_2_as() const { + return builtin_options_2_as_StablehloReduceWindowOptions(); +} + +template<> inline const tflite::StablehloSortOptions *Operator::builtin_options_2_as() const { + return builtin_options_2_as_StablehloSortOptions(); +} + +template<> inline const tflite::StablehloWhileOptions *Operator::builtin_options_2_as() const { + return builtin_options_2_as_StablehloWhileOptions(); +} + +template<> inline const tflite::StablehloGatherOptions *Operator::builtin_options_2_as() const { + return builtin_options_2_as_StablehloGatherOptions(); +} + +template<> inline const tflite::StablehloTransposeOptions *Operator::builtin_options_2_as() const { + return builtin_options_2_as_StablehloTransposeOptions(); +} + +template<> inline const tflite::DilateOptions *Operator::builtin_options_2_as() const { + return builtin_options_2_as_DilateOptions(); +} + +template<> inline const tflite::StablehloRngBitGeneratorOptions *Operator::builtin_options_2_as() const { + return builtin_options_2_as_StablehloRngBitGeneratorOptions(); +} + +template<> inline const tflite::ReduceWindowOptions *Operator::builtin_options_2_as() const { + return builtin_options_2_as_ReduceWindowOptions(); +} + +template<> inline const tflite::StableHLOCompositeOptions *Operator::builtin_options_2_as() const { + return builtin_options_2_as_StableHLOCompositeOptions(); +} + +template<> inline const tflite::StablehloShiftLeftOptions *Operator::builtin_options_2_as() const { + return builtin_options_2_as_StablehloShiftLeftOptions(); +} + +template<> inline const tflite::StablehloCaseOptions *Operator::builtin_options_2_as() const { + return builtin_options_2_as_StablehloCaseOptions(); +} + +struct OperatorBuilder { + typedef Operator Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_opcode_index(uint32_t opcode_index) { + fbb_.AddElement(Operator::VT_OPCODE_INDEX, opcode_index, 0); + } + void add_inputs(::flatbuffers::Offset<::flatbuffers::Vector> inputs) { + fbb_.AddOffset(Operator::VT_INPUTS, inputs); + } + void add_outputs(::flatbuffers::Offset<::flatbuffers::Vector> outputs) { + fbb_.AddOffset(Operator::VT_OUTPUTS, outputs); + } + void add_builtin_options_type(tflite::BuiltinOptions builtin_options_type) { + fbb_.AddElement(Operator::VT_BUILTIN_OPTIONS_TYPE, static_cast(builtin_options_type), 0); + } + void add_builtin_options(::flatbuffers::Offset builtin_options) { + fbb_.AddOffset(Operator::VT_BUILTIN_OPTIONS, builtin_options); + } + void add_custom_options(::flatbuffers::Offset<::flatbuffers::Vector> custom_options) { + fbb_.AddOffset(Operator::VT_CUSTOM_OPTIONS, custom_options); + } + void add_custom_options_format(tflite::CustomOptionsFormat custom_options_format) { + fbb_.AddElement(Operator::VT_CUSTOM_OPTIONS_FORMAT, static_cast(custom_options_format), 0); + } + void add_mutating_variable_inputs(::flatbuffers::Offset<::flatbuffers::Vector> mutating_variable_inputs) { + fbb_.AddOffset(Operator::VT_MUTATING_VARIABLE_INPUTS, mutating_variable_inputs); + } + void add_intermediates(::flatbuffers::Offset<::flatbuffers::Vector> intermediates) { + fbb_.AddOffset(Operator::VT_INTERMEDIATES, intermediates); + } + void add_large_custom_options_offset(uint64_t large_custom_options_offset) { + fbb_.AddElement(Operator::VT_LARGE_CUSTOM_OPTIONS_OFFSET, large_custom_options_offset, 0); + } + void add_large_custom_options_size(uint64_t large_custom_options_size) { + fbb_.AddElement(Operator::VT_LARGE_CUSTOM_OPTIONS_SIZE, large_custom_options_size, 0); + } + void add_builtin_options_2_type(tflite::BuiltinOptions2 builtin_options_2_type) { + fbb_.AddElement(Operator::VT_BUILTIN_OPTIONS_2_TYPE, static_cast(builtin_options_2_type), 0); + } + void add_builtin_options_2(::flatbuffers::Offset builtin_options_2) { + fbb_.AddOffset(Operator::VT_BUILTIN_OPTIONS_2, builtin_options_2); + } + void add_debug_metadata_index(int32_t debug_metadata_index) { + fbb_.AddElement(Operator::VT_DEBUG_METADATA_INDEX, debug_metadata_index, -1); + } + explicit OperatorBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateOperator( + ::flatbuffers::FlatBufferBuilder &_fbb, + uint32_t opcode_index = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> inputs = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> outputs = 0, + tflite::BuiltinOptions builtin_options_type = tflite::BuiltinOptions_NONE, + ::flatbuffers::Offset builtin_options = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> custom_options = 0, + tflite::CustomOptionsFormat custom_options_format = tflite::CustomOptionsFormat_FLEXBUFFERS, + ::flatbuffers::Offset<::flatbuffers::Vector> mutating_variable_inputs = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> intermediates = 0, + uint64_t large_custom_options_offset = 0, + uint64_t large_custom_options_size = 0, + tflite::BuiltinOptions2 builtin_options_2_type = tflite::BuiltinOptions2_NONE, + ::flatbuffers::Offset builtin_options_2 = 0, + int32_t debug_metadata_index = -1) { + OperatorBuilder builder_(_fbb); + builder_.add_large_custom_options_size(large_custom_options_size); + builder_.add_large_custom_options_offset(large_custom_options_offset); + builder_.add_debug_metadata_index(debug_metadata_index); + builder_.add_builtin_options_2(builtin_options_2); + builder_.add_intermediates(intermediates); + builder_.add_mutating_variable_inputs(mutating_variable_inputs); + builder_.add_custom_options(custom_options); + builder_.add_builtin_options(builtin_options); + builder_.add_outputs(outputs); + builder_.add_inputs(inputs); + builder_.add_opcode_index(opcode_index); + builder_.add_builtin_options_2_type(builtin_options_2_type); + builder_.add_custom_options_format(custom_options_format); + builder_.add_builtin_options_type(builtin_options_type); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateOperatorDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + uint32_t opcode_index = 0, + const std::vector *inputs = nullptr, + const std::vector *outputs = nullptr, + tflite::BuiltinOptions builtin_options_type = tflite::BuiltinOptions_NONE, + ::flatbuffers::Offset builtin_options = 0, + const std::vector *custom_options = nullptr, + tflite::CustomOptionsFormat custom_options_format = tflite::CustomOptionsFormat_FLEXBUFFERS, + const std::vector *mutating_variable_inputs = nullptr, + const std::vector *intermediates = nullptr, + uint64_t large_custom_options_offset = 0, + uint64_t large_custom_options_size = 0, + tflite::BuiltinOptions2 builtin_options_2_type = tflite::BuiltinOptions2_NONE, + ::flatbuffers::Offset builtin_options_2 = 0, + int32_t debug_metadata_index = -1) { + auto inputs__ = inputs ? _fbb.CreateVector(*inputs) : 0; + auto outputs__ = outputs ? _fbb.CreateVector(*outputs) : 0; + auto custom_options__ = custom_options ? _fbb.CreateVector(*custom_options) : 0; + auto mutating_variable_inputs__ = mutating_variable_inputs ? _fbb.CreateVector(*mutating_variable_inputs) : 0; + auto intermediates__ = intermediates ? _fbb.CreateVector(*intermediates) : 0; + return tflite::CreateOperator( + _fbb, + opcode_index, + inputs__, + outputs__, + builtin_options_type, + builtin_options, + custom_options__, + custom_options_format, + mutating_variable_inputs__, + intermediates__, + large_custom_options_offset, + large_custom_options_size, + builtin_options_2_type, + builtin_options_2, + debug_metadata_index); +} + +::flatbuffers::Offset CreateOperator(::flatbuffers::FlatBufferBuilder &_fbb, const OperatorT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct SubGraphT : public ::flatbuffers::NativeTable { + typedef SubGraph TableType; + std::vector> tensors{}; + std::vector inputs{}; + std::vector outputs{}; + std::vector> operators{}; + std::string name{}; + int32_t debug_metadata_index = -1; + SubGraphT() = default; + SubGraphT(const SubGraphT &o); + SubGraphT(SubGraphT&&) FLATBUFFERS_NOEXCEPT = default; + SubGraphT &operator=(SubGraphT o) FLATBUFFERS_NOEXCEPT; +}; + +struct SubGraph FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef SubGraphT NativeTableType; + typedef SubGraphBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_TENSORS = 4, + VT_INPUTS = 6, + VT_OUTPUTS = 8, + VT_OPERATORS = 10, + VT_NAME = 12, + VT_DEBUG_METADATA_INDEX = 14 + }; + const ::flatbuffers::Vector<::flatbuffers::Offset> *tensors() const { + return GetPointer> *>(VT_TENSORS); + } + const ::flatbuffers::Vector *inputs() const { + return GetPointer *>(VT_INPUTS); + } + const ::flatbuffers::Vector *outputs() const { + return GetPointer *>(VT_OUTPUTS); + } + const ::flatbuffers::Vector<::flatbuffers::Offset> *operators() const { + return GetPointer> *>(VT_OPERATORS); + } + const ::flatbuffers::String *name() const { + return GetPointer(VT_NAME); + } + int32_t debug_metadata_index() const { + return GetField(VT_DEBUG_METADATA_INDEX, -1); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_TENSORS) && + verifier.VerifyVector(tensors()) && + verifier.VerifyVectorOfTables(tensors()) && + VerifyOffset(verifier, VT_INPUTS) && + verifier.VerifyVector(inputs()) && + VerifyOffset(verifier, VT_OUTPUTS) && + verifier.VerifyVector(outputs()) && + VerifyOffset(verifier, VT_OPERATORS) && + verifier.VerifyVector(operators()) && + verifier.VerifyVectorOfTables(operators()) && + VerifyOffset(verifier, VT_NAME) && + verifier.VerifyString(name()) && + VerifyField(verifier, VT_DEBUG_METADATA_INDEX, 4) && + verifier.EndTable(); + } + SubGraphT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(SubGraphT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SubGraphT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct SubGraphBuilder { + typedef SubGraph Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_tensors(::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset>> tensors) { + fbb_.AddOffset(SubGraph::VT_TENSORS, tensors); + } + void add_inputs(::flatbuffers::Offset<::flatbuffers::Vector> inputs) { + fbb_.AddOffset(SubGraph::VT_INPUTS, inputs); + } + void add_outputs(::flatbuffers::Offset<::flatbuffers::Vector> outputs) { + fbb_.AddOffset(SubGraph::VT_OUTPUTS, outputs); + } + void add_operators(::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset>> operators) { + fbb_.AddOffset(SubGraph::VT_OPERATORS, operators); + } + void add_name(::flatbuffers::Offset<::flatbuffers::String> name) { + fbb_.AddOffset(SubGraph::VT_NAME, name); + } + void add_debug_metadata_index(int32_t debug_metadata_index) { + fbb_.AddElement(SubGraph::VT_DEBUG_METADATA_INDEX, debug_metadata_index, -1); + } + explicit SubGraphBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateSubGraph( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset>> tensors = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> inputs = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> outputs = 0, + ::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset>> operators = 0, + ::flatbuffers::Offset<::flatbuffers::String> name = 0, + int32_t debug_metadata_index = -1) { + SubGraphBuilder builder_(_fbb); + builder_.add_debug_metadata_index(debug_metadata_index); + builder_.add_name(name); + builder_.add_operators(operators); + builder_.add_outputs(outputs); + builder_.add_inputs(inputs); + builder_.add_tensors(tensors); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateSubGraphDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const std::vector<::flatbuffers::Offset> *tensors = nullptr, + const std::vector *inputs = nullptr, + const std::vector *outputs = nullptr, + const std::vector<::flatbuffers::Offset> *operators = nullptr, + const char *name = nullptr, + int32_t debug_metadata_index = -1) { + auto tensors__ = tensors ? _fbb.CreateVector<::flatbuffers::Offset>(*tensors) : 0; + auto inputs__ = inputs ? _fbb.CreateVector(*inputs) : 0; + auto outputs__ = outputs ? _fbb.CreateVector(*outputs) : 0; + auto operators__ = operators ? _fbb.CreateVector<::flatbuffers::Offset>(*operators) : 0; + auto name__ = name ? _fbb.CreateString(name) : 0; + return tflite::CreateSubGraph( + _fbb, + tensors__, + inputs__, + outputs__, + operators__, + name__, + debug_metadata_index); +} + +::flatbuffers::Offset CreateSubGraph(::flatbuffers::FlatBufferBuilder &_fbb, const SubGraphT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct BufferT : public ::flatbuffers::NativeTable { + typedef Buffer TableType; + std::vector data{}; + uint64_t offset = 0; + uint64_t size = 0; +}; + +struct Buffer FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef BufferT NativeTableType; + typedef BufferBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_DATA = 4, + VT_OFFSET = 6, + VT_SIZE = 8 + }; + const ::flatbuffers::Vector *data() const { + return GetPointer *>(VT_DATA); + } + uint64_t offset() const { + return GetField(VT_OFFSET, 0); + } + uint64_t size() const { + return GetField(VT_SIZE, 0); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_DATA) && + verifier.VerifyVector(data()) && + VerifyField(verifier, VT_OFFSET, 8) && + VerifyField(verifier, VT_SIZE, 8) && + verifier.EndTable(); + } + BufferT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(BufferT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const BufferT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct BufferBuilder { + typedef Buffer Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_data(::flatbuffers::Offset<::flatbuffers::Vector> data) { + fbb_.AddOffset(Buffer::VT_DATA, data); + } + void add_offset(uint64_t offset) { + fbb_.AddElement(Buffer::VT_OFFSET, offset, 0); + } + void add_size(uint64_t size) { + fbb_.AddElement(Buffer::VT_SIZE, size, 0); + } + explicit BufferBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateBuffer( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::Vector> data = 0, + uint64_t offset = 0, + uint64_t size = 0) { + BufferBuilder builder_(_fbb); + builder_.add_size(size); + builder_.add_offset(offset); + builder_.add_data(data); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateBufferDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const std::vector *data = nullptr, + uint64_t offset = 0, + uint64_t size = 0) { + if (data) { _fbb.ForceVectorAlignment(data->size(), sizeof(uint8_t), 16); } + auto data__ = data ? _fbb.CreateVector(*data) : 0; + return tflite::CreateBuffer( + _fbb, + data__, + offset, + size); +} + +::flatbuffers::Offset CreateBuffer(::flatbuffers::FlatBufferBuilder &_fbb, const BufferT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct ExternalBufferGroupT : public ::flatbuffers::NativeTable { + typedef ExternalBufferGroup TableType; + std::string name{}; +}; + +struct ExternalBufferGroup FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef ExternalBufferGroupT NativeTableType; + typedef ExternalBufferGroupBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_NAME = 4 + }; + const ::flatbuffers::String *name() const { + return GetPointer(VT_NAME); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_NAME) && + verifier.VerifyString(name()) && + verifier.EndTable(); + } + ExternalBufferGroupT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(ExternalBufferGroupT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ExternalBufferGroupT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct ExternalBufferGroupBuilder { + typedef ExternalBufferGroup Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_name(::flatbuffers::Offset<::flatbuffers::String> name) { + fbb_.AddOffset(ExternalBufferGroup::VT_NAME, name); + } + explicit ExternalBufferGroupBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateExternalBufferGroup( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::String> name = 0) { + ExternalBufferGroupBuilder builder_(_fbb); + builder_.add_name(name); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateExternalBufferGroupDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const char *name = nullptr) { + auto name__ = name ? _fbb.CreateString(name) : 0; + return tflite::CreateExternalBufferGroup( + _fbb, + name__); +} + +::flatbuffers::Offset CreateExternalBufferGroup(::flatbuffers::FlatBufferBuilder &_fbb, const ExternalBufferGroupT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct ExternalBufferT : public ::flatbuffers::NativeTable { + typedef ExternalBuffer TableType; + uint32_t id = 0; + uint32_t group = 0; + uint64_t offset = 0; + uint64_t length = 0; + std::string packing{}; +}; + +struct ExternalBuffer FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef ExternalBufferT NativeTableType; + typedef ExternalBufferBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_ID = 4, + VT_GROUP = 6, + VT_OFFSET = 8, + VT_LENGTH = 10, + VT_PACKING = 12 + }; + uint32_t id() const { + return GetField(VT_ID, 0); + } + uint32_t group() const { + return GetField(VT_GROUP, 0); + } + uint64_t offset() const { + return GetField(VT_OFFSET, 0); + } + uint64_t length() const { + return GetField(VT_LENGTH, 0); + } + const ::flatbuffers::String *packing() const { + return GetPointer(VT_PACKING); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_ID, 4) && + VerifyField(verifier, VT_GROUP, 4) && + VerifyField(verifier, VT_OFFSET, 8) && + VerifyField(verifier, VT_LENGTH, 8) && + VerifyOffset(verifier, VT_PACKING) && + verifier.VerifyString(packing()) && + verifier.EndTable(); + } + ExternalBufferT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(ExternalBufferT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ExternalBufferT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct ExternalBufferBuilder { + typedef ExternalBuffer Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_id(uint32_t id) { + fbb_.AddElement(ExternalBuffer::VT_ID, id, 0); + } + void add_group(uint32_t group) { + fbb_.AddElement(ExternalBuffer::VT_GROUP, group, 0); + } + void add_offset(uint64_t offset) { + fbb_.AddElement(ExternalBuffer::VT_OFFSET, offset, 0); + } + void add_length(uint64_t length) { + fbb_.AddElement(ExternalBuffer::VT_LENGTH, length, 0); + } + void add_packing(::flatbuffers::Offset<::flatbuffers::String> packing) { + fbb_.AddOffset(ExternalBuffer::VT_PACKING, packing); + } + explicit ExternalBufferBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateExternalBuffer( + ::flatbuffers::FlatBufferBuilder &_fbb, + uint32_t id = 0, + uint32_t group = 0, + uint64_t offset = 0, + uint64_t length = 0, + ::flatbuffers::Offset<::flatbuffers::String> packing = 0) { + ExternalBufferBuilder builder_(_fbb); + builder_.add_length(length); + builder_.add_offset(offset); + builder_.add_packing(packing); + builder_.add_group(group); + builder_.add_id(id); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateExternalBufferDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + uint32_t id = 0, + uint32_t group = 0, + uint64_t offset = 0, + uint64_t length = 0, + const char *packing = nullptr) { + auto packing__ = packing ? _fbb.CreateString(packing) : 0; + return tflite::CreateExternalBuffer( + _fbb, + id, + group, + offset, + length, + packing__); +} + +::flatbuffers::Offset CreateExternalBuffer(::flatbuffers::FlatBufferBuilder &_fbb, const ExternalBufferT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct MetadataT : public ::flatbuffers::NativeTable { + typedef Metadata TableType; + std::string name{}; + uint32_t buffer = 0; +}; + +struct Metadata FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef MetadataT NativeTableType; + typedef MetadataBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_NAME = 4, + VT_BUFFER = 6 + }; + const ::flatbuffers::String *name() const { + return GetPointer(VT_NAME); + } + uint32_t buffer() const { + return GetField(VT_BUFFER, 0); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_NAME) && + verifier.VerifyString(name()) && + VerifyField(verifier, VT_BUFFER, 4) && + verifier.EndTable(); + } + MetadataT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(MetadataT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const MetadataT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct MetadataBuilder { + typedef Metadata Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_name(::flatbuffers::Offset<::flatbuffers::String> name) { + fbb_.AddOffset(Metadata::VT_NAME, name); + } + void add_buffer(uint32_t buffer) { + fbb_.AddElement(Metadata::VT_BUFFER, buffer, 0); + } + explicit MetadataBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateMetadata( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::String> name = 0, + uint32_t buffer = 0) { + MetadataBuilder builder_(_fbb); + builder_.add_buffer(buffer); + builder_.add_name(name); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateMetadataDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const char *name = nullptr, + uint32_t buffer = 0) { + auto name__ = name ? _fbb.CreateString(name) : 0; + return tflite::CreateMetadata( + _fbb, + name__, + buffer); +} + +::flatbuffers::Offset CreateMetadata(::flatbuffers::FlatBufferBuilder &_fbb, const MetadataT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct TensorMapT : public ::flatbuffers::NativeTable { + typedef TensorMap TableType; + std::string name{}; + uint32_t tensor_index = 0; +}; + +struct TensorMap FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef TensorMapT NativeTableType; + typedef TensorMapBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_NAME = 4, + VT_TENSOR_INDEX = 6 + }; + const ::flatbuffers::String *name() const { + return GetPointer(VT_NAME); + } + uint32_t tensor_index() const { + return GetField(VT_TENSOR_INDEX, 0); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_NAME) && + verifier.VerifyString(name()) && + VerifyField(verifier, VT_TENSOR_INDEX, 4) && + verifier.EndTable(); + } + TensorMapT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(TensorMapT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const TensorMapT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct TensorMapBuilder { + typedef TensorMap Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_name(::flatbuffers::Offset<::flatbuffers::String> name) { + fbb_.AddOffset(TensorMap::VT_NAME, name); + } + void add_tensor_index(uint32_t tensor_index) { + fbb_.AddElement(TensorMap::VT_TENSOR_INDEX, tensor_index, 0); + } + explicit TensorMapBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateTensorMap( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::String> name = 0, + uint32_t tensor_index = 0) { + TensorMapBuilder builder_(_fbb); + builder_.add_tensor_index(tensor_index); + builder_.add_name(name); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateTensorMapDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const char *name = nullptr, + uint32_t tensor_index = 0) { + auto name__ = name ? _fbb.CreateString(name) : 0; + return tflite::CreateTensorMap( + _fbb, + name__, + tensor_index); +} + +::flatbuffers::Offset CreateTensorMap(::flatbuffers::FlatBufferBuilder &_fbb, const TensorMapT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct SignatureDefT : public ::flatbuffers::NativeTable { + typedef SignatureDef TableType; + std::vector> inputs{}; + std::vector> outputs{}; + std::string signature_key{}; + uint32_t subgraph_index = 0; + SignatureDefT() = default; + SignatureDefT(const SignatureDefT &o); + SignatureDefT(SignatureDefT&&) FLATBUFFERS_NOEXCEPT = default; + SignatureDefT &operator=(SignatureDefT o) FLATBUFFERS_NOEXCEPT; +}; + +struct SignatureDef FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef SignatureDefT NativeTableType; + typedef SignatureDefBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_INPUTS = 4, + VT_OUTPUTS = 6, + VT_SIGNATURE_KEY = 8, + VT_SUBGRAPH_INDEX = 12 + }; + const ::flatbuffers::Vector<::flatbuffers::Offset> *inputs() const { + return GetPointer> *>(VT_INPUTS); + } + const ::flatbuffers::Vector<::flatbuffers::Offset> *outputs() const { + return GetPointer> *>(VT_OUTPUTS); + } + const ::flatbuffers::String *signature_key() const { + return GetPointer(VT_SIGNATURE_KEY); + } + uint32_t subgraph_index() const { + return GetField(VT_SUBGRAPH_INDEX, 0); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_INPUTS) && + verifier.VerifyVector(inputs()) && + verifier.VerifyVectorOfTables(inputs()) && + VerifyOffset(verifier, VT_OUTPUTS) && + verifier.VerifyVector(outputs()) && + verifier.VerifyVectorOfTables(outputs()) && + VerifyOffset(verifier, VT_SIGNATURE_KEY) && + verifier.VerifyString(signature_key()) && + VerifyField(verifier, VT_SUBGRAPH_INDEX, 4) && + verifier.EndTable(); + } + SignatureDefT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(SignatureDefT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SignatureDefT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct SignatureDefBuilder { + typedef SignatureDef Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_inputs(::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset>> inputs) { + fbb_.AddOffset(SignatureDef::VT_INPUTS, inputs); + } + void add_outputs(::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset>> outputs) { + fbb_.AddOffset(SignatureDef::VT_OUTPUTS, outputs); + } + void add_signature_key(::flatbuffers::Offset<::flatbuffers::String> signature_key) { + fbb_.AddOffset(SignatureDef::VT_SIGNATURE_KEY, signature_key); + } + void add_subgraph_index(uint32_t subgraph_index) { + fbb_.AddElement(SignatureDef::VT_SUBGRAPH_INDEX, subgraph_index, 0); + } + explicit SignatureDefBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateSignatureDef( + ::flatbuffers::FlatBufferBuilder &_fbb, + ::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset>> inputs = 0, + ::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset>> outputs = 0, + ::flatbuffers::Offset<::flatbuffers::String> signature_key = 0, + uint32_t subgraph_index = 0) { + SignatureDefBuilder builder_(_fbb); + builder_.add_subgraph_index(subgraph_index); + builder_.add_signature_key(signature_key); + builder_.add_outputs(outputs); + builder_.add_inputs(inputs); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateSignatureDefDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + const std::vector<::flatbuffers::Offset> *inputs = nullptr, + const std::vector<::flatbuffers::Offset> *outputs = nullptr, + const char *signature_key = nullptr, + uint32_t subgraph_index = 0) { + auto inputs__ = inputs ? _fbb.CreateVector<::flatbuffers::Offset>(*inputs) : 0; + auto outputs__ = outputs ? _fbb.CreateVector<::flatbuffers::Offset>(*outputs) : 0; + auto signature_key__ = signature_key ? _fbb.CreateString(signature_key) : 0; + return tflite::CreateSignatureDef( + _fbb, + inputs__, + outputs__, + signature_key__, + subgraph_index); +} + +::flatbuffers::Offset CreateSignatureDef(::flatbuffers::FlatBufferBuilder &_fbb, const SignatureDefT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct ModelT : public ::flatbuffers::NativeTable { + typedef Model TableType; + uint32_t version = 0; + std::vector> operator_codes{}; + std::vector> subgraphs{}; + std::string description{}; + std::vector> buffers{}; + std::vector metadata_buffer{}; + std::vector> metadata{}; + std::vector> signature_defs{}; + std::vector> external_buffer_groups{}; + std::vector> external_buffers{}; + ModelT() = default; + ModelT(const ModelT &o); + ModelT(ModelT&&) FLATBUFFERS_NOEXCEPT = default; + ModelT &operator=(ModelT o) FLATBUFFERS_NOEXCEPT; +}; + +struct Model FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { + typedef ModelT NativeTableType; + typedef ModelBuilder Builder; + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_VERSION = 4, + VT_OPERATOR_CODES = 6, + VT_SUBGRAPHS = 8, + VT_DESCRIPTION = 10, + VT_BUFFERS = 12, + VT_METADATA_BUFFER = 14, + VT_METADATA = 16, + VT_SIGNATURE_DEFS = 18, + VT_EXTERNAL_BUFFER_GROUPS = 20, + VT_EXTERNAL_BUFFERS = 22 + }; + uint32_t version() const { + return GetField(VT_VERSION, 0); + } + const ::flatbuffers::Vector<::flatbuffers::Offset> *operator_codes() const { + return GetPointer> *>(VT_OPERATOR_CODES); + } + const ::flatbuffers::Vector<::flatbuffers::Offset> *subgraphs() const { + return GetPointer> *>(VT_SUBGRAPHS); + } + const ::flatbuffers::String *description() const { + return GetPointer(VT_DESCRIPTION); + } + const ::flatbuffers::Vector<::flatbuffers::Offset> *buffers() const { + return GetPointer> *>(VT_BUFFERS); + } + const ::flatbuffers::Vector *metadata_buffer() const { + return GetPointer *>(VT_METADATA_BUFFER); + } + const ::flatbuffers::Vector<::flatbuffers::Offset> *metadata() const { + return GetPointer> *>(VT_METADATA); + } + const ::flatbuffers::Vector<::flatbuffers::Offset> *signature_defs() const { + return GetPointer> *>(VT_SIGNATURE_DEFS); + } + const ::flatbuffers::Vector<::flatbuffers::Offset> *external_buffer_groups() const { + return GetPointer> *>(VT_EXTERNAL_BUFFER_GROUPS); + } + const ::flatbuffers::Vector<::flatbuffers::Offset> *external_buffers() const { + return GetPointer> *>(VT_EXTERNAL_BUFFERS); + } + template + bool Verify(::flatbuffers::VerifierTemplate &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_VERSION, 4) && + VerifyOffset(verifier, VT_OPERATOR_CODES) && + verifier.VerifyVector(operator_codes()) && + verifier.VerifyVectorOfTables(operator_codes()) && + VerifyOffset(verifier, VT_SUBGRAPHS) && + verifier.VerifyVector(subgraphs()) && + verifier.VerifyVectorOfTables(subgraphs()) && + VerifyOffset(verifier, VT_DESCRIPTION) && + verifier.VerifyString(description()) && + VerifyOffset(verifier, VT_BUFFERS) && + verifier.VerifyVector(buffers()) && + verifier.VerifyVectorOfTables(buffers()) && + VerifyOffset(verifier, VT_METADATA_BUFFER) && + verifier.VerifyVector(metadata_buffer()) && + VerifyOffset(verifier, VT_METADATA) && + verifier.VerifyVector(metadata()) && + verifier.VerifyVectorOfTables(metadata()) && + VerifyOffset(verifier, VT_SIGNATURE_DEFS) && + verifier.VerifyVector(signature_defs()) && + verifier.VerifyVectorOfTables(signature_defs()) && + VerifyOffset(verifier, VT_EXTERNAL_BUFFER_GROUPS) && + verifier.VerifyVector(external_buffer_groups()) && + verifier.VerifyVectorOfTables(external_buffer_groups()) && + VerifyOffset(verifier, VT_EXTERNAL_BUFFERS) && + verifier.VerifyVector(external_buffers()) && + verifier.VerifyVectorOfTables(external_buffers()) && + verifier.EndTable(); + } + ModelT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(ModelT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const; + static ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ModelT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct ModelBuilder { + typedef Model Table; + ::flatbuffers::FlatBufferBuilder &fbb_; + ::flatbuffers::uoffset_t start_; + void add_version(uint32_t version) { + fbb_.AddElement(Model::VT_VERSION, version, 0); + } + void add_operator_codes(::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset>> operator_codes) { + fbb_.AddOffset(Model::VT_OPERATOR_CODES, operator_codes); + } + void add_subgraphs(::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset>> subgraphs) { + fbb_.AddOffset(Model::VT_SUBGRAPHS, subgraphs); + } + void add_description(::flatbuffers::Offset<::flatbuffers::String> description) { + fbb_.AddOffset(Model::VT_DESCRIPTION, description); + } + void add_buffers(::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset>> buffers) { + fbb_.AddOffset(Model::VT_BUFFERS, buffers); + } + void add_metadata_buffer(::flatbuffers::Offset<::flatbuffers::Vector> metadata_buffer) { + fbb_.AddOffset(Model::VT_METADATA_BUFFER, metadata_buffer); + } + void add_metadata(::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset>> metadata) { + fbb_.AddOffset(Model::VT_METADATA, metadata); + } + void add_signature_defs(::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset>> signature_defs) { + fbb_.AddOffset(Model::VT_SIGNATURE_DEFS, signature_defs); + } + void add_external_buffer_groups(::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset>> external_buffer_groups) { + fbb_.AddOffset(Model::VT_EXTERNAL_BUFFER_GROUPS, external_buffer_groups); + } + void add_external_buffers(::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset>> external_buffers) { + fbb_.AddOffset(Model::VT_EXTERNAL_BUFFERS, external_buffers); + } + explicit ModelBuilder(::flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ::flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = ::flatbuffers::Offset(end); + return o; + } +}; + +inline ::flatbuffers::Offset CreateModel( + ::flatbuffers::FlatBufferBuilder &_fbb, + uint32_t version = 0, + ::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset>> operator_codes = 0, + ::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset>> subgraphs = 0, + ::flatbuffers::Offset<::flatbuffers::String> description = 0, + ::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset>> buffers = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> metadata_buffer = 0, + ::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset>> metadata = 0, + ::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset>> signature_defs = 0, + ::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset>> external_buffer_groups = 0, + ::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset>> external_buffers = 0) { + ModelBuilder builder_(_fbb); + builder_.add_external_buffers(external_buffers); + builder_.add_external_buffer_groups(external_buffer_groups); + builder_.add_signature_defs(signature_defs); + builder_.add_metadata(metadata); + builder_.add_metadata_buffer(metadata_buffer); + builder_.add_buffers(buffers); + builder_.add_description(description); + builder_.add_subgraphs(subgraphs); + builder_.add_operator_codes(operator_codes); + builder_.add_version(version); + return builder_.Finish(); +} + +inline ::flatbuffers::Offset CreateModelDirect( + ::flatbuffers::FlatBufferBuilder &_fbb, + uint32_t version = 0, + const std::vector<::flatbuffers::Offset> *operator_codes = nullptr, + const std::vector<::flatbuffers::Offset> *subgraphs = nullptr, + const char *description = nullptr, + const std::vector<::flatbuffers::Offset> *buffers = nullptr, + const std::vector *metadata_buffer = nullptr, + const std::vector<::flatbuffers::Offset> *metadata = nullptr, + const std::vector<::flatbuffers::Offset> *signature_defs = nullptr, + const std::vector<::flatbuffers::Offset> *external_buffer_groups = nullptr, + const std::vector<::flatbuffers::Offset> *external_buffers = nullptr) { + auto operator_codes__ = operator_codes ? _fbb.CreateVector<::flatbuffers::Offset>(*operator_codes) : 0; + auto subgraphs__ = subgraphs ? _fbb.CreateVector<::flatbuffers::Offset>(*subgraphs) : 0; + auto description__ = description ? _fbb.CreateString(description) : 0; + auto buffers__ = buffers ? _fbb.CreateVector<::flatbuffers::Offset>(*buffers) : 0; + auto metadata_buffer__ = metadata_buffer ? _fbb.CreateVector(*metadata_buffer) : 0; + auto metadata__ = metadata ? _fbb.CreateVector<::flatbuffers::Offset>(*metadata) : 0; + auto signature_defs__ = signature_defs ? _fbb.CreateVector<::flatbuffers::Offset>(*signature_defs) : 0; + auto external_buffer_groups__ = external_buffer_groups ? _fbb.CreateVector<::flatbuffers::Offset>(*external_buffer_groups) : 0; + auto external_buffers__ = external_buffers ? _fbb.CreateVector<::flatbuffers::Offset>(*external_buffers) : 0; + return tflite::CreateModel( + _fbb, + version, + operator_codes__, + subgraphs__, + description__, + buffers__, + metadata_buffer__, + metadata__, + signature_defs__, + external_buffer_groups__, + external_buffers__); +} + +::flatbuffers::Offset CreateModel(::flatbuffers::FlatBufferBuilder &_fbb, const ModelT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +inline CustomQuantizationT *CustomQuantization::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new CustomQuantizationT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void CustomQuantization::UnPackTo(CustomQuantizationT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = custom(); if (_e) { _o->custom.resize(_e->size()); std::copy(_e->begin(), _e->end(), _o->custom.begin()); } } +} + +inline ::flatbuffers::Offset CreateCustomQuantization(::flatbuffers::FlatBufferBuilder &_fbb, const CustomQuantizationT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return CustomQuantization::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset CustomQuantization::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const CustomQuantizationT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const CustomQuantizationT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + _fbb.ForceVectorAlignment(_o->custom.size(), sizeof(uint8_t), 16); + auto _custom = _o->custom.size() ? _fbb.CreateVector(_o->custom) : 0; + return tflite::CreateCustomQuantization( + _fbb, + _custom); +} + +inline BlockwiseQuantizationT *BlockwiseQuantization::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new BlockwiseQuantizationT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void BlockwiseQuantization::UnPackTo(BlockwiseQuantizationT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = scales(); _o->scales = _e; } + { auto _e = zero_points(); _o->zero_points = _e; } + { auto _e = block_size(); _o->block_size = _e; } +} + +inline ::flatbuffers::Offset CreateBlockwiseQuantization(::flatbuffers::FlatBufferBuilder &_fbb, const BlockwiseQuantizationT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return BlockwiseQuantization::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset BlockwiseQuantization::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const BlockwiseQuantizationT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const BlockwiseQuantizationT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _scales = _o->scales; + auto _zero_points = _o->zero_points; + auto _block_size = _o->block_size; + return tflite::CreateBlockwiseQuantization( + _fbb, + _scales, + _zero_points, + _block_size); +} + +inline MultiAxisQuantizationT *MultiAxisQuantization::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new MultiAxisQuantizationT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void MultiAxisQuantization::UnPackTo(MultiAxisQuantizationT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = scales(); _o->scales = _e; } + { auto _e = zero_points(); _o->zero_points = _e; } + { auto _e = block_size(); _o->block_size = _e; } + { auto _e = quantized_dimensions(); if (_e) { _o->quantized_dimensions.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->quantized_dimensions[_i] = _e->Get(_i); } } else { _o->quantized_dimensions.resize(0); } } +} + +inline ::flatbuffers::Offset CreateMultiAxisQuantization(::flatbuffers::FlatBufferBuilder &_fbb, const MultiAxisQuantizationT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return MultiAxisQuantization::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset MultiAxisQuantization::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const MultiAxisQuantizationT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const MultiAxisQuantizationT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _scales = _o->scales; + auto _zero_points = _o->zero_points; + auto _block_size = _o->block_size; + auto _quantized_dimensions = _o->quantized_dimensions.size() ? _fbb.CreateVector(_o->quantized_dimensions) : 0; + return tflite::CreateMultiAxisQuantization( + _fbb, + _scales, + _zero_points, + _block_size, + _quantized_dimensions); +} + +inline QuantizationParametersT *QuantizationParameters::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new QuantizationParametersT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void QuantizationParameters::UnPackTo(QuantizationParametersT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = min(); if (_e) { _o->min.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->min[_i] = _e->Get(_i); } } else { _o->min.resize(0); } } + { auto _e = max(); if (_e) { _o->max.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->max[_i] = _e->Get(_i); } } else { _o->max.resize(0); } } + { auto _e = scale(); if (_e) { _o->scale.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->scale[_i] = _e->Get(_i); } } else { _o->scale.resize(0); } } + { auto _e = zero_point(); if (_e) { _o->zero_point.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->zero_point[_i] = _e->Get(_i); } } else { _o->zero_point.resize(0); } } + { auto _e = details_type(); _o->details.type = _e; } + { auto _e = details(); if (_e) _o->details.value = tflite::QuantizationDetailsUnion::UnPack(_e, details_type(), _resolver); } + { auto _e = quantized_dimension(); _o->quantized_dimension = _e; } +} + +inline ::flatbuffers::Offset CreateQuantizationParameters(::flatbuffers::FlatBufferBuilder &_fbb, const QuantizationParametersT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return QuantizationParameters::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset QuantizationParameters::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const QuantizationParametersT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const QuantizationParametersT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _min = _o->min.size() ? _fbb.CreateVector(_o->min) : 0; + auto _max = _o->max.size() ? _fbb.CreateVector(_o->max) : 0; + auto _scale = _o->scale.size() ? _fbb.CreateVector(_o->scale) : 0; + auto _zero_point = _o->zero_point.size() ? _fbb.CreateVector(_o->zero_point) : 0; + auto _details_type = _o->details.type; + auto _details = _o->details.Pack(_fbb); + auto _quantized_dimension = _o->quantized_dimension; + return tflite::CreateQuantizationParameters( + _fbb, + _min, + _max, + _scale, + _zero_point, + _details_type, + _details, + _quantized_dimension); +} + +inline Int32VectorT *Int32Vector::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new Int32VectorT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void Int32Vector::UnPackTo(Int32VectorT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = values(); if (_e) { _o->values.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->values[_i] = _e->Get(_i); } } else { _o->values.resize(0); } } +} + +inline ::flatbuffers::Offset CreateInt32Vector(::flatbuffers::FlatBufferBuilder &_fbb, const Int32VectorT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return Int32Vector::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset Int32Vector::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const Int32VectorT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const Int32VectorT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _values = _o->values.size() ? _fbb.CreateVector(_o->values) : 0; + return tflite::CreateInt32Vector( + _fbb, + _values); +} + +inline Uint16VectorT *Uint16Vector::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new Uint16VectorT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void Uint16Vector::UnPackTo(Uint16VectorT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = values(); if (_e) { _o->values.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->values[_i] = _e->Get(_i); } } else { _o->values.resize(0); } } +} + +inline ::flatbuffers::Offset CreateUint16Vector(::flatbuffers::FlatBufferBuilder &_fbb, const Uint16VectorT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return Uint16Vector::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset Uint16Vector::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const Uint16VectorT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const Uint16VectorT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + _fbb.ForceVectorAlignment(_o->values.size(), sizeof(uint16_t), 4); + auto _values = _o->values.size() ? _fbb.CreateVector(_o->values) : 0; + return tflite::CreateUint16Vector( + _fbb, + _values); +} + +inline Uint8VectorT *Uint8Vector::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new Uint8VectorT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void Uint8Vector::UnPackTo(Uint8VectorT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = values(); if (_e) { _o->values.resize(_e->size()); std::copy(_e->begin(), _e->end(), _o->values.begin()); } } +} + +inline ::flatbuffers::Offset CreateUint8Vector(::flatbuffers::FlatBufferBuilder &_fbb, const Uint8VectorT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return Uint8Vector::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset Uint8Vector::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const Uint8VectorT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const Uint8VectorT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + _fbb.ForceVectorAlignment(_o->values.size(), sizeof(uint8_t), 4); + auto _values = _o->values.size() ? _fbb.CreateVector(_o->values) : 0; + return tflite::CreateUint8Vector( + _fbb, + _values); +} + +inline DimensionMetadataT *DimensionMetadata::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new DimensionMetadataT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void DimensionMetadata::UnPackTo(DimensionMetadataT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = format(); _o->format = _e; } + { auto _e = dense_size(); _o->dense_size = _e; } + { auto _e = array_segments_type(); _o->array_segments.type = _e; } + { auto _e = array_segments(); if (_e) _o->array_segments.value = tflite::SparseIndexVectorUnion::UnPack(_e, array_segments_type(), _resolver); } + { auto _e = array_indices_type(); _o->array_indices.type = _e; } + { auto _e = array_indices(); if (_e) _o->array_indices.value = tflite::SparseIndexVectorUnion::UnPack(_e, array_indices_type(), _resolver); } +} + +inline ::flatbuffers::Offset CreateDimensionMetadata(::flatbuffers::FlatBufferBuilder &_fbb, const DimensionMetadataT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return DimensionMetadata::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset DimensionMetadata::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const DimensionMetadataT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const DimensionMetadataT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _format = _o->format; + auto _dense_size = _o->dense_size; + auto _array_segments_type = _o->array_segments.type; + auto _array_segments = _o->array_segments.Pack(_fbb); + auto _array_indices_type = _o->array_indices.type; + auto _array_indices = _o->array_indices.Pack(_fbb); + return tflite::CreateDimensionMetadata( + _fbb, + _format, + _dense_size, + _array_segments_type, + _array_segments, + _array_indices_type, + _array_indices); +} + +inline SparsityParametersT::SparsityParametersT(const SparsityParametersT &o) + : traversal_order(o.traversal_order), + block_map(o.block_map) { + dim_metadata.reserve(o.dim_metadata.size()); + for (const auto &dim_metadata_ : o.dim_metadata) { dim_metadata.emplace_back((dim_metadata_) ? new tflite::DimensionMetadataT(*dim_metadata_) : nullptr); } +} + +inline SparsityParametersT &SparsityParametersT::operator=(SparsityParametersT o) FLATBUFFERS_NOEXCEPT { + std::swap(traversal_order, o.traversal_order); + std::swap(block_map, o.block_map); + std::swap(dim_metadata, o.dim_metadata); + return *this; +} + +inline SparsityParametersT *SparsityParameters::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new SparsityParametersT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void SparsityParameters::UnPackTo(SparsityParametersT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = traversal_order(); if (_e) { _o->traversal_order.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->traversal_order[_i] = _e->Get(_i); } } else { _o->traversal_order.resize(0); } } + { auto _e = block_map(); if (_e) { _o->block_map.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->block_map[_i] = _e->Get(_i); } } else { _o->block_map.resize(0); } } + { auto _e = dim_metadata(); if (_e) { _o->dim_metadata.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { if(_o->dim_metadata[_i]) { _e->Get(_i)->UnPackTo(_o->dim_metadata[_i].get(), _resolver); } else { _o->dim_metadata[_i] = std::unique_ptr(_e->Get(_i)->UnPack(_resolver)); } } } else { _o->dim_metadata.resize(0); } } +} + +inline ::flatbuffers::Offset CreateSparsityParameters(::flatbuffers::FlatBufferBuilder &_fbb, const SparsityParametersT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return SparsityParameters::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset SparsityParameters::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SparsityParametersT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const SparsityParametersT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _traversal_order = _o->traversal_order.size() ? _fbb.CreateVector(_o->traversal_order) : 0; + auto _block_map = _o->block_map.size() ? _fbb.CreateVector(_o->block_map) : 0; + auto _dim_metadata = _o->dim_metadata.size() ? _fbb.CreateVector<::flatbuffers::Offset> (_o->dim_metadata.size(), [](size_t i, _VectorArgs *__va) { return CreateDimensionMetadata(*__va->__fbb, __va->__o->dim_metadata[i].get(), __va->__rehasher); }, &_va ) : 0; + return tflite::CreateSparsityParameters( + _fbb, + _traversal_order, + _block_map, + _dim_metadata); +} + +inline VariantSubTypeT *VariantSubType::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new VariantSubTypeT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void VariantSubType::UnPackTo(VariantSubTypeT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = shape(); if (_e) { _o->shape.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->shape[_i] = _e->Get(_i); } } else { _o->shape.resize(0); } } + { auto _e = type(); _o->type = _e; } + { auto _e = has_rank(); _o->has_rank = _e; } +} + +inline ::flatbuffers::Offset CreateVariantSubType(::flatbuffers::FlatBufferBuilder &_fbb, const VariantSubTypeT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return VariantSubType::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset VariantSubType::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const VariantSubTypeT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const VariantSubTypeT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _shape = _o->shape.size() ? _fbb.CreateVector(_o->shape) : 0; + auto _type = _o->type; + auto _has_rank = _o->has_rank; + return tflite::CreateVariantSubType( + _fbb, + _shape, + _type, + _has_rank); +} + +inline TensorT::TensorT(const TensorT &o) + : shape(o.shape), + type(o.type), + buffer(o.buffer), + name(o.name), + quantization((o.quantization) ? new tflite::QuantizationParametersT(*o.quantization) : nullptr), + is_variable(o.is_variable), + sparsity((o.sparsity) ? new tflite::SparsityParametersT(*o.sparsity) : nullptr), + shape_signature(o.shape_signature), + has_rank(o.has_rank), + external_buffer(o.external_buffer) { + variant_tensors.reserve(o.variant_tensors.size()); + for (const auto &variant_tensors_ : o.variant_tensors) { variant_tensors.emplace_back((variant_tensors_) ? new tflite::VariantSubTypeT(*variant_tensors_) : nullptr); } +} + +inline TensorT &TensorT::operator=(TensorT o) FLATBUFFERS_NOEXCEPT { + std::swap(shape, o.shape); + std::swap(type, o.type); + std::swap(buffer, o.buffer); + std::swap(name, o.name); + std::swap(quantization, o.quantization); + std::swap(is_variable, o.is_variable); + std::swap(sparsity, o.sparsity); + std::swap(shape_signature, o.shape_signature); + std::swap(has_rank, o.has_rank); + std::swap(variant_tensors, o.variant_tensors); + std::swap(external_buffer, o.external_buffer); + return *this; +} + +inline TensorT *Tensor::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new TensorT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void Tensor::UnPackTo(TensorT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = shape(); if (_e) { _o->shape.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->shape[_i] = _e->Get(_i); } } else { _o->shape.resize(0); } } + { auto _e = type(); _o->type = _e; } + { auto _e = buffer(); _o->buffer = _e; } + { auto _e = name(); if (_e) _o->name = _e->str(); } + { auto _e = quantization(); if (_e) { if(_o->quantization) { _e->UnPackTo(_o->quantization.get(), _resolver); } else { _o->quantization = std::unique_ptr(_e->UnPack(_resolver)); } } else if (_o->quantization) { _o->quantization.reset(); } } + { auto _e = is_variable(); _o->is_variable = _e; } + { auto _e = sparsity(); if (_e) { if(_o->sparsity) { _e->UnPackTo(_o->sparsity.get(), _resolver); } else { _o->sparsity = std::unique_ptr(_e->UnPack(_resolver)); } } else if (_o->sparsity) { _o->sparsity.reset(); } } + { auto _e = shape_signature(); if (_e) { _o->shape_signature.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->shape_signature[_i] = _e->Get(_i); } } else { _o->shape_signature.resize(0); } } + { auto _e = has_rank(); _o->has_rank = _e; } + { auto _e = variant_tensors(); if (_e) { _o->variant_tensors.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { if(_o->variant_tensors[_i]) { _e->Get(_i)->UnPackTo(_o->variant_tensors[_i].get(), _resolver); } else { _o->variant_tensors[_i] = std::unique_ptr(_e->Get(_i)->UnPack(_resolver)); } } } else { _o->variant_tensors.resize(0); } } + { auto _e = external_buffer(); _o->external_buffer = _e; } +} + +inline ::flatbuffers::Offset CreateTensor(::flatbuffers::FlatBufferBuilder &_fbb, const TensorT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return Tensor::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset Tensor::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const TensorT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const TensorT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _shape = _o->shape.size() ? _fbb.CreateVector(_o->shape) : 0; + auto _type = _o->type; + auto _buffer = _o->buffer; + auto _name = _o->name.empty() ? 0 : _fbb.CreateString(_o->name); + auto _quantization = _o->quantization ? CreateQuantizationParameters(_fbb, _o->quantization.get(), _rehasher) : 0; + auto _is_variable = _o->is_variable; + auto _sparsity = _o->sparsity ? CreateSparsityParameters(_fbb, _o->sparsity.get(), _rehasher) : 0; + auto _shape_signature = _o->shape_signature.size() ? _fbb.CreateVector(_o->shape_signature) : 0; + auto _has_rank = _o->has_rank; + auto _variant_tensors = _o->variant_tensors.size() ? _fbb.CreateVector<::flatbuffers::Offset> (_o->variant_tensors.size(), [](size_t i, _VectorArgs *__va) { return CreateVariantSubType(*__va->__fbb, __va->__o->variant_tensors[i].get(), __va->__rehasher); }, &_va ) : 0; + auto _external_buffer = _o->external_buffer; + return tflite::CreateTensor( + _fbb, + _shape, + _type, + _buffer, + _name, + _quantization, + _is_variable, + _sparsity, + _shape_signature, + _has_rank, + _variant_tensors, + _external_buffer); +} + +inline StablehloGatherOptionsT *StablehloGatherOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new StablehloGatherOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void StablehloGatherOptions::UnPackTo(StablehloGatherOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = offset_dims(); if (_e) { _o->offset_dims.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->offset_dims[_i] = _e->Get(_i); } } else { _o->offset_dims.resize(0); } } + { auto _e = collapsed_slice_dims(); if (_e) { _o->collapsed_slice_dims.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->collapsed_slice_dims[_i] = _e->Get(_i); } } else { _o->collapsed_slice_dims.resize(0); } } + { auto _e = start_index_map(); if (_e) { _o->start_index_map.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->start_index_map[_i] = _e->Get(_i); } } else { _o->start_index_map.resize(0); } } + { auto _e = index_vector_dim(); _o->index_vector_dim = _e; } + { auto _e = slice_sizes(); if (_e) { _o->slice_sizes.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->slice_sizes[_i] = _e->Get(_i); } } else { _o->slice_sizes.resize(0); } } + { auto _e = indices_are_sorted(); _o->indices_are_sorted = _e; } +} + +inline ::flatbuffers::Offset CreateStablehloGatherOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloGatherOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return StablehloGatherOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset StablehloGatherOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloGatherOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const StablehloGatherOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _offset_dims = _o->offset_dims.size() ? _fbb.CreateVector(_o->offset_dims) : 0; + auto _collapsed_slice_dims = _o->collapsed_slice_dims.size() ? _fbb.CreateVector(_o->collapsed_slice_dims) : 0; + auto _start_index_map = _o->start_index_map.size() ? _fbb.CreateVector(_o->start_index_map) : 0; + auto _index_vector_dim = _o->index_vector_dim; + auto _slice_sizes = _o->slice_sizes.size() ? _fbb.CreateVector(_o->slice_sizes) : 0; + auto _indices_are_sorted = _o->indices_are_sorted; + return tflite::CreateStablehloGatherOptions( + _fbb, + _offset_dims, + _collapsed_slice_dims, + _start_index_map, + _index_vector_dim, + _slice_sizes, + _indices_are_sorted); +} + +inline StablehloTransposeOptionsT *StablehloTransposeOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new StablehloTransposeOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void StablehloTransposeOptions::UnPackTo(StablehloTransposeOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = permutation(); if (_e) { _o->permutation.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->permutation[_i] = _e->Get(_i); } } else { _o->permutation.resize(0); } } +} + +inline ::flatbuffers::Offset CreateStablehloTransposeOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloTransposeOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return StablehloTransposeOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset StablehloTransposeOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloTransposeOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const StablehloTransposeOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _permutation = _o->permutation.size() ? _fbb.CreateVector(_o->permutation) : 0; + return tflite::CreateStablehloTransposeOptions( + _fbb, + _permutation); +} + +inline StablehloDotGeneralOptionsT *StablehloDotGeneralOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new StablehloDotGeneralOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void StablehloDotGeneralOptions::UnPackTo(StablehloDotGeneralOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = lhs_batching_dimensions(); if (_e) { _o->lhs_batching_dimensions.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->lhs_batching_dimensions[_i] = _e->Get(_i); } } else { _o->lhs_batching_dimensions.resize(0); } } + { auto _e = rhs_batching_dimensions(); if (_e) { _o->rhs_batching_dimensions.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->rhs_batching_dimensions[_i] = _e->Get(_i); } } else { _o->rhs_batching_dimensions.resize(0); } } + { auto _e = lhs_contracting_dimensions(); if (_e) { _o->lhs_contracting_dimensions.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->lhs_contracting_dimensions[_i] = _e->Get(_i); } } else { _o->lhs_contracting_dimensions.resize(0); } } + { auto _e = rhs_contracting_dimensions(); if (_e) { _o->rhs_contracting_dimensions.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->rhs_contracting_dimensions[_i] = _e->Get(_i); } } else { _o->rhs_contracting_dimensions.resize(0); } } + { auto _e = precision_config(); if (_e) { _o->precision_config.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->precision_config[_i] = static_cast(_e->Get(_i)); } } else { _o->precision_config.resize(0); } } +} + +inline ::flatbuffers::Offset CreateStablehloDotGeneralOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloDotGeneralOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return StablehloDotGeneralOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset StablehloDotGeneralOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloDotGeneralOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const StablehloDotGeneralOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _lhs_batching_dimensions = _o->lhs_batching_dimensions.size() ? _fbb.CreateVector(_o->lhs_batching_dimensions) : 0; + auto _rhs_batching_dimensions = _o->rhs_batching_dimensions.size() ? _fbb.CreateVector(_o->rhs_batching_dimensions) : 0; + auto _lhs_contracting_dimensions = _o->lhs_contracting_dimensions.size() ? _fbb.CreateVector(_o->lhs_contracting_dimensions) : 0; + auto _rhs_contracting_dimensions = _o->rhs_contracting_dimensions.size() ? _fbb.CreateVector(_o->rhs_contracting_dimensions) : 0; + auto _precision_config = _o->precision_config.size() ? _fbb.CreateVectorScalarCast(::flatbuffers::data(_o->precision_config), _o->precision_config.size()) : 0; + return tflite::CreateStablehloDotGeneralOptions( + _fbb, + _lhs_batching_dimensions, + _rhs_batching_dimensions, + _lhs_contracting_dimensions, + _rhs_contracting_dimensions, + _precision_config); +} + +inline StablehloReduceWindowOptionsT *StablehloReduceWindowOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new StablehloReduceWindowOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void StablehloReduceWindowOptions::UnPackTo(StablehloReduceWindowOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = window_dimensions(); if (_e) { _o->window_dimensions.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->window_dimensions[_i] = _e->Get(_i); } } else { _o->window_dimensions.resize(0); } } + { auto _e = window_strides(); if (_e) { _o->window_strides.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->window_strides[_i] = _e->Get(_i); } } else { _o->window_strides.resize(0); } } + { auto _e = base_dilations(); if (_e) { _o->base_dilations.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->base_dilations[_i] = _e->Get(_i); } } else { _o->base_dilations.resize(0); } } + { auto _e = window_dilations(); if (_e) { _o->window_dilations.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->window_dilations[_i] = _e->Get(_i); } } else { _o->window_dilations.resize(0); } } + { auto _e = padding(); if (_e) { _o->padding.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->padding[_i] = _e->Get(_i); } } else { _o->padding.resize(0); } } + { auto _e = body_subgraph_index(); _o->body_subgraph_index = _e; } +} + +inline ::flatbuffers::Offset CreateStablehloReduceWindowOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloReduceWindowOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return StablehloReduceWindowOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset StablehloReduceWindowOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloReduceWindowOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const StablehloReduceWindowOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _window_dimensions = _o->window_dimensions.size() ? _fbb.CreateVector(_o->window_dimensions) : 0; + auto _window_strides = _o->window_strides.size() ? _fbb.CreateVector(_o->window_strides) : 0; + auto _base_dilations = _o->base_dilations.size() ? _fbb.CreateVector(_o->base_dilations) : 0; + auto _window_dilations = _o->window_dilations.size() ? _fbb.CreateVector(_o->window_dilations) : 0; + auto _padding = _o->padding.size() ? _fbb.CreateVector(_o->padding) : 0; + auto _body_subgraph_index = _o->body_subgraph_index; + return tflite::CreateStablehloReduceWindowOptions( + _fbb, + _window_dimensions, + _window_strides, + _base_dilations, + _window_dilations, + _padding, + _body_subgraph_index); +} + +inline StablehloWhileOptionsT *StablehloWhileOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new StablehloWhileOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void StablehloWhileOptions::UnPackTo(StablehloWhileOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = cond_subgraph_index(); _o->cond_subgraph_index = _e; } + { auto _e = body_subgraph_index(); _o->body_subgraph_index = _e; } +} + +inline ::flatbuffers::Offset CreateStablehloWhileOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloWhileOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return StablehloWhileOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset StablehloWhileOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloWhileOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const StablehloWhileOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _cond_subgraph_index = _o->cond_subgraph_index; + auto _body_subgraph_index = _o->body_subgraph_index; + return tflite::CreateStablehloWhileOptions( + _fbb, + _cond_subgraph_index, + _body_subgraph_index); +} + +inline StablehloSortOptionsT *StablehloSortOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new StablehloSortOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void StablehloSortOptions::UnPackTo(StablehloSortOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = dimension(); _o->dimension = _e; } + { auto _e = is_stable(); _o->is_stable = _e; } + { auto _e = comparator_subgraph_index(); _o->comparator_subgraph_index = _e; } +} + +inline ::flatbuffers::Offset CreateStablehloSortOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloSortOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return StablehloSortOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset StablehloSortOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloSortOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const StablehloSortOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _dimension = _o->dimension; + auto _is_stable = _o->is_stable; + auto _comparator_subgraph_index = _o->comparator_subgraph_index; + return tflite::CreateStablehloSortOptions( + _fbb, + _dimension, + _is_stable, + _comparator_subgraph_index); +} + +inline StablehloConcatenateOptionsT *StablehloConcatenateOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new StablehloConcatenateOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void StablehloConcatenateOptions::UnPackTo(StablehloConcatenateOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = dimension(); _o->dimension = _e; } +} + +inline ::flatbuffers::Offset CreateStablehloConcatenateOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloConcatenateOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return StablehloConcatenateOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset StablehloConcatenateOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloConcatenateOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const StablehloConcatenateOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _dimension = _o->dimension; + return tflite::CreateStablehloConcatenateOptions( + _fbb, + _dimension); +} + +inline StablehloBroadcastInDimOptionsT *StablehloBroadcastInDimOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new StablehloBroadcastInDimOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void StablehloBroadcastInDimOptions::UnPackTo(StablehloBroadcastInDimOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = broadcast_dimensions(); if (_e) { _o->broadcast_dimensions.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->broadcast_dimensions[_i] = _e->Get(_i); } } else { _o->broadcast_dimensions.resize(0); } } +} + +inline ::flatbuffers::Offset CreateStablehloBroadcastInDimOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloBroadcastInDimOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return StablehloBroadcastInDimOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset StablehloBroadcastInDimOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloBroadcastInDimOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const StablehloBroadcastInDimOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _broadcast_dimensions = _o->broadcast_dimensions.size() ? _fbb.CreateVector(_o->broadcast_dimensions) : 0; + return tflite::CreateStablehloBroadcastInDimOptions( + _fbb, + _broadcast_dimensions); +} + +inline StablehloCompareOptionsT *StablehloCompareOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new StablehloCompareOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void StablehloCompareOptions::UnPackTo(StablehloCompareOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = comparison_direction(); _o->comparison_direction = _e; } + { auto _e = compare_type(); _o->compare_type = _e; } +} + +inline ::flatbuffers::Offset CreateStablehloCompareOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloCompareOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return StablehloCompareOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset StablehloCompareOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloCompareOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const StablehloCompareOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _comparison_direction = _o->comparison_direction; + auto _compare_type = _o->compare_type; + return tflite::CreateStablehloCompareOptions( + _fbb, + _comparison_direction, + _compare_type); +} + +inline StablehloDynamicSliceOptionsT *StablehloDynamicSliceOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new StablehloDynamicSliceOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void StablehloDynamicSliceOptions::UnPackTo(StablehloDynamicSliceOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = slice_sizes(); if (_e) { _o->slice_sizes.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->slice_sizes[_i] = _e->Get(_i); } } else { _o->slice_sizes.resize(0); } } +} + +inline ::flatbuffers::Offset CreateStablehloDynamicSliceOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloDynamicSliceOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return StablehloDynamicSliceOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset StablehloDynamicSliceOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloDynamicSliceOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const StablehloDynamicSliceOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _slice_sizes = _o->slice_sizes.size() ? _fbb.CreateVector(_o->slice_sizes) : 0; + return tflite::CreateStablehloDynamicSliceOptions( + _fbb, + _slice_sizes); +} + +inline StablehloPadOptionsT *StablehloPadOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new StablehloPadOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void StablehloPadOptions::UnPackTo(StablehloPadOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = edge_padding_low(); if (_e) { _o->edge_padding_low.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->edge_padding_low[_i] = _e->Get(_i); } } else { _o->edge_padding_low.resize(0); } } + { auto _e = edge_padding_high(); if (_e) { _o->edge_padding_high.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->edge_padding_high[_i] = _e->Get(_i); } } else { _o->edge_padding_high.resize(0); } } + { auto _e = interior_padding(); if (_e) { _o->interior_padding.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->interior_padding[_i] = _e->Get(_i); } } else { _o->interior_padding.resize(0); } } +} + +inline ::flatbuffers::Offset CreateStablehloPadOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloPadOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return StablehloPadOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset StablehloPadOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloPadOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const StablehloPadOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _edge_padding_low = _o->edge_padding_low.size() ? _fbb.CreateVector(_o->edge_padding_low) : 0; + auto _edge_padding_high = _o->edge_padding_high.size() ? _fbb.CreateVector(_o->edge_padding_high) : 0; + auto _interior_padding = _o->interior_padding.size() ? _fbb.CreateVector(_o->interior_padding) : 0; + return tflite::CreateStablehloPadOptions( + _fbb, + _edge_padding_low, + _edge_padding_high, + _interior_padding); +} + +inline StablehloIotaOptionsT *StablehloIotaOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new StablehloIotaOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void StablehloIotaOptions::UnPackTo(StablehloIotaOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = iota_dimension(); _o->iota_dimension = _e; } +} + +inline ::flatbuffers::Offset CreateStablehloIotaOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloIotaOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return StablehloIotaOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset StablehloIotaOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloIotaOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const StablehloIotaOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _iota_dimension = _o->iota_dimension; + return tflite::CreateStablehloIotaOptions( + _fbb, + _iota_dimension); +} + +inline StablehloCustomCallOptionsT *StablehloCustomCallOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new StablehloCustomCallOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void StablehloCustomCallOptions::UnPackTo(StablehloCustomCallOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = call_target_name(); if (_e) _o->call_target_name = _e->str(); } + { auto _e = has_side_effect(); _o->has_side_effect = _e; } + { auto _e = backend_config(); if (_e) _o->backend_config = _e->str(); } + { auto _e = api_version(); _o->api_version = _e; } + { auto _e = called_computations(); if (_e) { _o->called_computations.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->called_computations[_i] = _e->Get(_i); } } else { _o->called_computations.resize(0); } } + { auto _e = custom_attributes(); if (_e) { _o->custom_attributes.resize(_e->size()); std::copy(_e->begin(), _e->end(), _o->custom_attributes.begin()); } } +} + +inline ::flatbuffers::Offset CreateStablehloCustomCallOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloCustomCallOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return StablehloCustomCallOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset StablehloCustomCallOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloCustomCallOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const StablehloCustomCallOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _call_target_name = _o->call_target_name.empty() ? 0 : _fbb.CreateString(_o->call_target_name); + auto _has_side_effect = _o->has_side_effect; + auto _backend_config = _o->backend_config.empty() ? 0 : _fbb.CreateString(_o->backend_config); + auto _api_version = _o->api_version; + auto _called_computations = _o->called_computations.size() ? _fbb.CreateVector(_o->called_computations) : 0; + auto _custom_attributes = _o->custom_attributes.size() ? _fbb.CreateVector(_o->custom_attributes) : 0; + return tflite::CreateStablehloCustomCallOptions( + _fbb, + _call_target_name, + _has_side_effect, + _backend_config, + _api_version, + _called_computations, + _custom_attributes); +} + +inline StablehloReduceOptionsT *StablehloReduceOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new StablehloReduceOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void StablehloReduceOptions::UnPackTo(StablehloReduceOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = dimensions(); if (_e) { _o->dimensions.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->dimensions[_i] = _e->Get(_i); } } else { _o->dimensions.resize(0); } } + { auto _e = body_subgraph_index(); _o->body_subgraph_index = _e; } +} + +inline ::flatbuffers::Offset CreateStablehloReduceOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloReduceOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return StablehloReduceOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset StablehloReduceOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloReduceOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const StablehloReduceOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _dimensions = _o->dimensions.size() ? _fbb.CreateVector(_o->dimensions) : 0; + auto _body_subgraph_index = _o->body_subgraph_index; + return tflite::CreateStablehloReduceOptions( + _fbb, + _dimensions, + _body_subgraph_index); +} + +inline StablehloSliceOptionsT *StablehloSliceOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new StablehloSliceOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void StablehloSliceOptions::UnPackTo(StablehloSliceOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = start_indices(); if (_e) { _o->start_indices.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->start_indices[_i] = _e->Get(_i); } } else { _o->start_indices.resize(0); } } + { auto _e = limit_indices(); if (_e) { _o->limit_indices.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->limit_indices[_i] = _e->Get(_i); } } else { _o->limit_indices.resize(0); } } + { auto _e = strides(); if (_e) { _o->strides.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->strides[_i] = _e->Get(_i); } } else { _o->strides.resize(0); } } +} + +inline ::flatbuffers::Offset CreateStablehloSliceOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloSliceOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return StablehloSliceOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset StablehloSliceOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloSliceOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const StablehloSliceOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _start_indices = _o->start_indices.size() ? _fbb.CreateVector(_o->start_indices) : 0; + auto _limit_indices = _o->limit_indices.size() ? _fbb.CreateVector(_o->limit_indices) : 0; + auto _strides = _o->strides.size() ? _fbb.CreateVector(_o->strides) : 0; + return tflite::CreateStablehloSliceOptions( + _fbb, + _start_indices, + _limit_indices, + _strides); +} + +inline StablehloConvolutionOptionsT *StablehloConvolutionOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new StablehloConvolutionOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void StablehloConvolutionOptions::UnPackTo(StablehloConvolutionOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = window_strides(); if (_e) { _o->window_strides.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->window_strides[_i] = _e->Get(_i); } } else { _o->window_strides.resize(0); } } + { auto _e = padding(); if (_e) { _o->padding.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->padding[_i] = _e->Get(_i); } } else { _o->padding.resize(0); } } + { auto _e = lhs_dilation(); if (_e) { _o->lhs_dilation.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->lhs_dilation[_i] = _e->Get(_i); } } else { _o->lhs_dilation.resize(0); } } + { auto _e = rhs_dilation(); if (_e) { _o->rhs_dilation.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->rhs_dilation[_i] = _e->Get(_i); } } else { _o->rhs_dilation.resize(0); } } + { auto _e = window_reversal(); if (_e) { _o->window_reversal.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->window_reversal[_i] = _e->Get(_i) != 0; } } else { _o->window_reversal.resize(0); } } + { auto _e = input_batch_dimension(); _o->input_batch_dimension = _e; } + { auto _e = input_feature_dimension(); _o->input_feature_dimension = _e; } + { auto _e = input_spatial_dimensions(); if (_e) { _o->input_spatial_dimensions.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->input_spatial_dimensions[_i] = _e->Get(_i); } } else { _o->input_spatial_dimensions.resize(0); } } + { auto _e = kernel_input_feature_dimension(); _o->kernel_input_feature_dimension = _e; } + { auto _e = kernel_output_feature_dimension(); _o->kernel_output_feature_dimension = _e; } + { auto _e = kernel_spatial_dimensions(); if (_e) { _o->kernel_spatial_dimensions.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->kernel_spatial_dimensions[_i] = _e->Get(_i); } } else { _o->kernel_spatial_dimensions.resize(0); } } + { auto _e = output_batch_dimension(); _o->output_batch_dimension = _e; } + { auto _e = output_feature_dimension(); _o->output_feature_dimension = _e; } + { auto _e = output_spatial_dimensions(); if (_e) { _o->output_spatial_dimensions.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->output_spatial_dimensions[_i] = _e->Get(_i); } } else { _o->output_spatial_dimensions.resize(0); } } + { auto _e = feature_group_count(); _o->feature_group_count = _e; } + { auto _e = batch_group_count(); _o->batch_group_count = _e; } + { auto _e = precision_config(); if (_e) { _o->precision_config.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->precision_config[_i] = static_cast(_e->Get(_i)); } } else { _o->precision_config.resize(0); } } +} + +inline ::flatbuffers::Offset CreateStablehloConvolutionOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloConvolutionOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return StablehloConvolutionOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset StablehloConvolutionOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloConvolutionOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const StablehloConvolutionOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _window_strides = _o->window_strides.size() ? _fbb.CreateVector(_o->window_strides) : 0; + auto _padding = _o->padding.size() ? _fbb.CreateVector(_o->padding) : 0; + auto _lhs_dilation = _o->lhs_dilation.size() ? _fbb.CreateVector(_o->lhs_dilation) : 0; + auto _rhs_dilation = _o->rhs_dilation.size() ? _fbb.CreateVector(_o->rhs_dilation) : 0; + auto _window_reversal = _o->window_reversal.size() ? _fbb.CreateVector(_o->window_reversal) : 0; + auto _input_batch_dimension = _o->input_batch_dimension; + auto _input_feature_dimension = _o->input_feature_dimension; + auto _input_spatial_dimensions = _o->input_spatial_dimensions.size() ? _fbb.CreateVector(_o->input_spatial_dimensions) : 0; + auto _kernel_input_feature_dimension = _o->kernel_input_feature_dimension; + auto _kernel_output_feature_dimension = _o->kernel_output_feature_dimension; + auto _kernel_spatial_dimensions = _o->kernel_spatial_dimensions.size() ? _fbb.CreateVector(_o->kernel_spatial_dimensions) : 0; + auto _output_batch_dimension = _o->output_batch_dimension; + auto _output_feature_dimension = _o->output_feature_dimension; + auto _output_spatial_dimensions = _o->output_spatial_dimensions.size() ? _fbb.CreateVector(_o->output_spatial_dimensions) : 0; + auto _feature_group_count = _o->feature_group_count; + auto _batch_group_count = _o->batch_group_count; + auto _precision_config = _o->precision_config.size() ? _fbb.CreateVectorScalarCast(::flatbuffers::data(_o->precision_config), _o->precision_config.size()) : 0; + return tflite::CreateStablehloConvolutionOptions( + _fbb, + _window_strides, + _padding, + _lhs_dilation, + _rhs_dilation, + _window_reversal, + _input_batch_dimension, + _input_feature_dimension, + _input_spatial_dimensions, + _kernel_input_feature_dimension, + _kernel_output_feature_dimension, + _kernel_spatial_dimensions, + _output_batch_dimension, + _output_feature_dimension, + _output_spatial_dimensions, + _feature_group_count, + _batch_group_count, + _precision_config); +} + +inline StablehloScatterOptionsT *StablehloScatterOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new StablehloScatterOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void StablehloScatterOptions::UnPackTo(StablehloScatterOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = indices_are_sorted(); _o->indices_are_sorted = _e; } + { auto _e = update_window_dims(); if (_e) { _o->update_window_dims.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->update_window_dims[_i] = _e->Get(_i); } } else { _o->update_window_dims.resize(0); } } + { auto _e = inserted_window_dims(); if (_e) { _o->inserted_window_dims.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->inserted_window_dims[_i] = _e->Get(_i); } } else { _o->inserted_window_dims.resize(0); } } + { auto _e = scatter_dims_to_operand_dims(); if (_e) { _o->scatter_dims_to_operand_dims.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->scatter_dims_to_operand_dims[_i] = _e->Get(_i); } } else { _o->scatter_dims_to_operand_dims.resize(0); } } + { auto _e = index_vector_dim(); _o->index_vector_dim = _e; } + { auto _e = unique_indices(); _o->unique_indices = _e; } + { auto _e = update_computation_subgraph_index(); _o->update_computation_subgraph_index = _e; } +} + +inline ::flatbuffers::Offset CreateStablehloScatterOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloScatterOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return StablehloScatterOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset StablehloScatterOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloScatterOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const StablehloScatterOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _indices_are_sorted = _o->indices_are_sorted; + auto _update_window_dims = _o->update_window_dims.size() ? _fbb.CreateVector(_o->update_window_dims) : 0; + auto _inserted_window_dims = _o->inserted_window_dims.size() ? _fbb.CreateVector(_o->inserted_window_dims) : 0; + auto _scatter_dims_to_operand_dims = _o->scatter_dims_to_operand_dims.size() ? _fbb.CreateVector(_o->scatter_dims_to_operand_dims) : 0; + auto _index_vector_dim = _o->index_vector_dim; + auto _unique_indices = _o->unique_indices; + auto _update_computation_subgraph_index = _o->update_computation_subgraph_index; + return tflite::CreateStablehloScatterOptions( + _fbb, + _indices_are_sorted, + _update_window_dims, + _inserted_window_dims, + _scatter_dims_to_operand_dims, + _index_vector_dim, + _unique_indices, + _update_computation_subgraph_index); +} + +inline StablehloCaseOptionsT *StablehloCaseOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new StablehloCaseOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void StablehloCaseOptions::UnPackTo(StablehloCaseOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = branch_subgraph_indices(); if (_e) { _o->branch_subgraph_indices.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->branch_subgraph_indices[_i] = _e->Get(_i); } } else { _o->branch_subgraph_indices.resize(0); } } +} + +inline ::flatbuffers::Offset CreateStablehloCaseOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloCaseOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return StablehloCaseOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset StablehloCaseOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloCaseOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const StablehloCaseOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _branch_subgraph_indices = _o->branch_subgraph_indices.size() ? _fbb.CreateVector(_o->branch_subgraph_indices) : 0; + return tflite::CreateStablehloCaseOptions( + _fbb, + _branch_subgraph_indices); +} + +inline StablehloRngBitGeneratorOptionsT *StablehloRngBitGeneratorOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new StablehloRngBitGeneratorOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void StablehloRngBitGeneratorOptions::UnPackTo(StablehloRngBitGeneratorOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = algorithm(); _o->algorithm = _e; } +} + +inline ::flatbuffers::Offset CreateStablehloRngBitGeneratorOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloRngBitGeneratorOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return StablehloRngBitGeneratorOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset StablehloRngBitGeneratorOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloRngBitGeneratorOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const StablehloRngBitGeneratorOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _algorithm = _o->algorithm; + return tflite::CreateStablehloRngBitGeneratorOptions( + _fbb, + _algorithm); +} + +inline Conv2DOptionsT *Conv2DOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new Conv2DOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void Conv2DOptions::UnPackTo(Conv2DOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = padding(); _o->padding = _e; } + { auto _e = stride_w(); _o->stride_w = _e; } + { auto _e = stride_h(); _o->stride_h = _e; } + { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } + { auto _e = dilation_w_factor(); _o->dilation_w_factor = _e; } + { auto _e = dilation_h_factor(); _o->dilation_h_factor = _e; } + { auto _e = quantized_bias_type(); _o->quantized_bias_type = _e; } +} + +inline ::flatbuffers::Offset CreateConv2DOptions(::flatbuffers::FlatBufferBuilder &_fbb, const Conv2DOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return Conv2DOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset Conv2DOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const Conv2DOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const Conv2DOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _padding = _o->padding; + auto _stride_w = _o->stride_w; + auto _stride_h = _o->stride_h; + auto _fused_activation_function = _o->fused_activation_function; + auto _dilation_w_factor = _o->dilation_w_factor; + auto _dilation_h_factor = _o->dilation_h_factor; + auto _quantized_bias_type = _o->quantized_bias_type; + return tflite::CreateConv2DOptions( + _fbb, + _padding, + _stride_w, + _stride_h, + _fused_activation_function, + _dilation_w_factor, + _dilation_h_factor, + _quantized_bias_type); +} + +inline Conv3DOptionsT *Conv3DOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new Conv3DOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void Conv3DOptions::UnPackTo(Conv3DOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = padding(); _o->padding = _e; } + { auto _e = stride_d(); _o->stride_d = _e; } + { auto _e = stride_w(); _o->stride_w = _e; } + { auto _e = stride_h(); _o->stride_h = _e; } + { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } + { auto _e = dilation_d_factor(); _o->dilation_d_factor = _e; } + { auto _e = dilation_w_factor(); _o->dilation_w_factor = _e; } + { auto _e = dilation_h_factor(); _o->dilation_h_factor = _e; } +} + +inline ::flatbuffers::Offset CreateConv3DOptions(::flatbuffers::FlatBufferBuilder &_fbb, const Conv3DOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return Conv3DOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset Conv3DOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const Conv3DOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const Conv3DOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _padding = _o->padding; + auto _stride_d = _o->stride_d; + auto _stride_w = _o->stride_w; + auto _stride_h = _o->stride_h; + auto _fused_activation_function = _o->fused_activation_function; + auto _dilation_d_factor = _o->dilation_d_factor; + auto _dilation_w_factor = _o->dilation_w_factor; + auto _dilation_h_factor = _o->dilation_h_factor; + return tflite::CreateConv3DOptions( + _fbb, + _padding, + _stride_d, + _stride_w, + _stride_h, + _fused_activation_function, + _dilation_d_factor, + _dilation_w_factor, + _dilation_h_factor); +} + +inline Pool2DOptionsT *Pool2DOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new Pool2DOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void Pool2DOptions::UnPackTo(Pool2DOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = padding(); _o->padding = _e; } + { auto _e = stride_w(); _o->stride_w = _e; } + { auto _e = stride_h(); _o->stride_h = _e; } + { auto _e = filter_width(); _o->filter_width = _e; } + { auto _e = filter_height(); _o->filter_height = _e; } + { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } +} + +inline ::flatbuffers::Offset CreatePool2DOptions(::flatbuffers::FlatBufferBuilder &_fbb, const Pool2DOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return Pool2DOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset Pool2DOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const Pool2DOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const Pool2DOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _padding = _o->padding; + auto _stride_w = _o->stride_w; + auto _stride_h = _o->stride_h; + auto _filter_width = _o->filter_width; + auto _filter_height = _o->filter_height; + auto _fused_activation_function = _o->fused_activation_function; + return tflite::CreatePool2DOptions( + _fbb, + _padding, + _stride_w, + _stride_h, + _filter_width, + _filter_height, + _fused_activation_function); +} + +inline DepthwiseConv2DOptionsT *DepthwiseConv2DOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new DepthwiseConv2DOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void DepthwiseConv2DOptions::UnPackTo(DepthwiseConv2DOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = padding(); _o->padding = _e; } + { auto _e = stride_w(); _o->stride_w = _e; } + { auto _e = stride_h(); _o->stride_h = _e; } + { auto _e = depth_multiplier(); _o->depth_multiplier = _e; } + { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } + { auto _e = dilation_w_factor(); _o->dilation_w_factor = _e; } + { auto _e = dilation_h_factor(); _o->dilation_h_factor = _e; } +} + +inline ::flatbuffers::Offset CreateDepthwiseConv2DOptions(::flatbuffers::FlatBufferBuilder &_fbb, const DepthwiseConv2DOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return DepthwiseConv2DOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset DepthwiseConv2DOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const DepthwiseConv2DOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const DepthwiseConv2DOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _padding = _o->padding; + auto _stride_w = _o->stride_w; + auto _stride_h = _o->stride_h; + auto _depth_multiplier = _o->depth_multiplier; + auto _fused_activation_function = _o->fused_activation_function; + auto _dilation_w_factor = _o->dilation_w_factor; + auto _dilation_h_factor = _o->dilation_h_factor; + return tflite::CreateDepthwiseConv2DOptions( + _fbb, + _padding, + _stride_w, + _stride_h, + _depth_multiplier, + _fused_activation_function, + _dilation_w_factor, + _dilation_h_factor); +} + +inline ConcatEmbeddingsOptionsT *ConcatEmbeddingsOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new ConcatEmbeddingsOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void ConcatEmbeddingsOptions::UnPackTo(ConcatEmbeddingsOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = num_channels(); _o->num_channels = _e; } + { auto _e = num_columns_per_channel(); if (_e) { _o->num_columns_per_channel.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->num_columns_per_channel[_i] = _e->Get(_i); } } else { _o->num_columns_per_channel.resize(0); } } + { auto _e = embedding_dim_per_channel(); if (_e) { _o->embedding_dim_per_channel.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->embedding_dim_per_channel[_i] = _e->Get(_i); } } else { _o->embedding_dim_per_channel.resize(0); } } +} + +inline ::flatbuffers::Offset CreateConcatEmbeddingsOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ConcatEmbeddingsOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return ConcatEmbeddingsOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset ConcatEmbeddingsOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ConcatEmbeddingsOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const ConcatEmbeddingsOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _num_channels = _o->num_channels; + auto _num_columns_per_channel = _o->num_columns_per_channel.size() ? _fbb.CreateVector(_o->num_columns_per_channel) : 0; + auto _embedding_dim_per_channel = _o->embedding_dim_per_channel.size() ? _fbb.CreateVector(_o->embedding_dim_per_channel) : 0; + return tflite::CreateConcatEmbeddingsOptions( + _fbb, + _num_channels, + _num_columns_per_channel, + _embedding_dim_per_channel); +} + +inline LSHProjectionOptionsT *LSHProjectionOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new LSHProjectionOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void LSHProjectionOptions::UnPackTo(LSHProjectionOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = type(); _o->type = _e; } +} + +inline ::flatbuffers::Offset CreateLSHProjectionOptions(::flatbuffers::FlatBufferBuilder &_fbb, const LSHProjectionOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return LSHProjectionOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset LSHProjectionOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const LSHProjectionOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const LSHProjectionOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _type = _o->type; + return tflite::CreateLSHProjectionOptions( + _fbb, + _type); +} + +inline SVDFOptionsT *SVDFOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new SVDFOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void SVDFOptions::UnPackTo(SVDFOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = rank(); _o->rank = _e; } + { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } + { auto _e = asymmetric_quantize_inputs(); _o->asymmetric_quantize_inputs = _e; } +} + +inline ::flatbuffers::Offset CreateSVDFOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SVDFOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return SVDFOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset SVDFOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SVDFOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const SVDFOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _rank = _o->rank; + auto _fused_activation_function = _o->fused_activation_function; + auto _asymmetric_quantize_inputs = _o->asymmetric_quantize_inputs; + return tflite::CreateSVDFOptions( + _fbb, + _rank, + _fused_activation_function, + _asymmetric_quantize_inputs); +} + +inline RNNOptionsT *RNNOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new RNNOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void RNNOptions::UnPackTo(RNNOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } + { auto _e = asymmetric_quantize_inputs(); _o->asymmetric_quantize_inputs = _e; } +} + +inline ::flatbuffers::Offset CreateRNNOptions(::flatbuffers::FlatBufferBuilder &_fbb, const RNNOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return RNNOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset RNNOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const RNNOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const RNNOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _fused_activation_function = _o->fused_activation_function; + auto _asymmetric_quantize_inputs = _o->asymmetric_quantize_inputs; + return tflite::CreateRNNOptions( + _fbb, + _fused_activation_function, + _asymmetric_quantize_inputs); +} + +inline SequenceRNNOptionsT *SequenceRNNOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new SequenceRNNOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void SequenceRNNOptions::UnPackTo(SequenceRNNOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = time_major(); _o->time_major = _e; } + { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } + { auto _e = asymmetric_quantize_inputs(); _o->asymmetric_quantize_inputs = _e; } +} + +inline ::flatbuffers::Offset CreateSequenceRNNOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SequenceRNNOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return SequenceRNNOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset SequenceRNNOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SequenceRNNOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const SequenceRNNOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _time_major = _o->time_major; + auto _fused_activation_function = _o->fused_activation_function; + auto _asymmetric_quantize_inputs = _o->asymmetric_quantize_inputs; + return tflite::CreateSequenceRNNOptions( + _fbb, + _time_major, + _fused_activation_function, + _asymmetric_quantize_inputs); +} + +inline BidirectionalSequenceRNNOptionsT *BidirectionalSequenceRNNOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new BidirectionalSequenceRNNOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void BidirectionalSequenceRNNOptions::UnPackTo(BidirectionalSequenceRNNOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = time_major(); _o->time_major = _e; } + { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } + { auto _e = merge_outputs(); _o->merge_outputs = _e; } + { auto _e = asymmetric_quantize_inputs(); _o->asymmetric_quantize_inputs = _e; } +} + +inline ::flatbuffers::Offset CreateBidirectionalSequenceRNNOptions(::flatbuffers::FlatBufferBuilder &_fbb, const BidirectionalSequenceRNNOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return BidirectionalSequenceRNNOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset BidirectionalSequenceRNNOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const BidirectionalSequenceRNNOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const BidirectionalSequenceRNNOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _time_major = _o->time_major; + auto _fused_activation_function = _o->fused_activation_function; + auto _merge_outputs = _o->merge_outputs; + auto _asymmetric_quantize_inputs = _o->asymmetric_quantize_inputs; + return tflite::CreateBidirectionalSequenceRNNOptions( + _fbb, + _time_major, + _fused_activation_function, + _merge_outputs, + _asymmetric_quantize_inputs); +} + +inline FullyConnectedOptionsT *FullyConnectedOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new FullyConnectedOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void FullyConnectedOptions::UnPackTo(FullyConnectedOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } + { auto _e = weights_format(); _o->weights_format = _e; } + { auto _e = keep_num_dims(); _o->keep_num_dims = _e; } + { auto _e = asymmetric_quantize_inputs(); _o->asymmetric_quantize_inputs = _e; } + { auto _e = quantized_bias_type(); _o->quantized_bias_type = _e; } +} + +inline ::flatbuffers::Offset CreateFullyConnectedOptions(::flatbuffers::FlatBufferBuilder &_fbb, const FullyConnectedOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return FullyConnectedOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset FullyConnectedOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const FullyConnectedOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const FullyConnectedOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _fused_activation_function = _o->fused_activation_function; + auto _weights_format = _o->weights_format; + auto _keep_num_dims = _o->keep_num_dims; + auto _asymmetric_quantize_inputs = _o->asymmetric_quantize_inputs; + auto _quantized_bias_type = _o->quantized_bias_type; + return tflite::CreateFullyConnectedOptions( + _fbb, + _fused_activation_function, + _weights_format, + _keep_num_dims, + _asymmetric_quantize_inputs, + _quantized_bias_type); +} + +inline SoftmaxOptionsT *SoftmaxOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new SoftmaxOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void SoftmaxOptions::UnPackTo(SoftmaxOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = beta(); _o->beta = _e; } +} + +inline ::flatbuffers::Offset CreateSoftmaxOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SoftmaxOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return SoftmaxOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset SoftmaxOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SoftmaxOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const SoftmaxOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _beta = _o->beta; + return tflite::CreateSoftmaxOptions( + _fbb, + _beta); +} + +inline ConcatenationOptionsT *ConcatenationOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new ConcatenationOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void ConcatenationOptions::UnPackTo(ConcatenationOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = axis(); _o->axis = _e; } + { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } +} + +inline ::flatbuffers::Offset CreateConcatenationOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ConcatenationOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return ConcatenationOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset ConcatenationOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ConcatenationOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const ConcatenationOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _axis = _o->axis; + auto _fused_activation_function = _o->fused_activation_function; + return tflite::CreateConcatenationOptions( + _fbb, + _axis, + _fused_activation_function); +} + +inline AddOptionsT *AddOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new AddOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void AddOptions::UnPackTo(AddOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } + { auto _e = pot_scale_int16(); _o->pot_scale_int16 = _e; } +} + +inline ::flatbuffers::Offset CreateAddOptions(::flatbuffers::FlatBufferBuilder &_fbb, const AddOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return AddOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset AddOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const AddOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const AddOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _fused_activation_function = _o->fused_activation_function; + auto _pot_scale_int16 = _o->pot_scale_int16; + return tflite::CreateAddOptions( + _fbb, + _fused_activation_function, + _pot_scale_int16); +} + +inline MulOptionsT *MulOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new MulOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void MulOptions::UnPackTo(MulOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } +} + +inline ::flatbuffers::Offset CreateMulOptions(::flatbuffers::FlatBufferBuilder &_fbb, const MulOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return MulOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset MulOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const MulOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const MulOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _fused_activation_function = _o->fused_activation_function; + return tflite::CreateMulOptions( + _fbb, + _fused_activation_function); +} + +inline L2NormOptionsT *L2NormOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new L2NormOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void L2NormOptions::UnPackTo(L2NormOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } +} + +inline ::flatbuffers::Offset CreateL2NormOptions(::flatbuffers::FlatBufferBuilder &_fbb, const L2NormOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return L2NormOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset L2NormOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const L2NormOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const L2NormOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _fused_activation_function = _o->fused_activation_function; + return tflite::CreateL2NormOptions( + _fbb, + _fused_activation_function); +} + +inline LocalResponseNormalizationOptionsT *LocalResponseNormalizationOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new LocalResponseNormalizationOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void LocalResponseNormalizationOptions::UnPackTo(LocalResponseNormalizationOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = radius(); _o->radius = _e; } + { auto _e = bias(); _o->bias = _e; } + { auto _e = alpha(); _o->alpha = _e; } + { auto _e = beta(); _o->beta = _e; } +} + +inline ::flatbuffers::Offset CreateLocalResponseNormalizationOptions(::flatbuffers::FlatBufferBuilder &_fbb, const LocalResponseNormalizationOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return LocalResponseNormalizationOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset LocalResponseNormalizationOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const LocalResponseNormalizationOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const LocalResponseNormalizationOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _radius = _o->radius; + auto _bias = _o->bias; + auto _alpha = _o->alpha; + auto _beta = _o->beta; + return tflite::CreateLocalResponseNormalizationOptions( + _fbb, + _radius, + _bias, + _alpha, + _beta); +} + +inline LSTMOptionsT *LSTMOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new LSTMOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void LSTMOptions::UnPackTo(LSTMOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } + { auto _e = cell_clip(); _o->cell_clip = _e; } + { auto _e = proj_clip(); _o->proj_clip = _e; } + { auto _e = kernel_type(); _o->kernel_type = _e; } + { auto _e = asymmetric_quantize_inputs(); _o->asymmetric_quantize_inputs = _e; } +} + +inline ::flatbuffers::Offset CreateLSTMOptions(::flatbuffers::FlatBufferBuilder &_fbb, const LSTMOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return LSTMOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset LSTMOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const LSTMOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const LSTMOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _fused_activation_function = _o->fused_activation_function; + auto _cell_clip = _o->cell_clip; + auto _proj_clip = _o->proj_clip; + auto _kernel_type = _o->kernel_type; + auto _asymmetric_quantize_inputs = _o->asymmetric_quantize_inputs; + return tflite::CreateLSTMOptions( + _fbb, + _fused_activation_function, + _cell_clip, + _proj_clip, + _kernel_type, + _asymmetric_quantize_inputs); +} + +inline UnidirectionalSequenceLSTMOptionsT *UnidirectionalSequenceLSTMOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new UnidirectionalSequenceLSTMOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void UnidirectionalSequenceLSTMOptions::UnPackTo(UnidirectionalSequenceLSTMOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } + { auto _e = cell_clip(); _o->cell_clip = _e; } + { auto _e = proj_clip(); _o->proj_clip = _e; } + { auto _e = time_major(); _o->time_major = _e; } + { auto _e = asymmetric_quantize_inputs(); _o->asymmetric_quantize_inputs = _e; } + { auto _e = diagonal_recurrent_tensors(); _o->diagonal_recurrent_tensors = _e; } +} + +inline ::flatbuffers::Offset CreateUnidirectionalSequenceLSTMOptions(::flatbuffers::FlatBufferBuilder &_fbb, const UnidirectionalSequenceLSTMOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return UnidirectionalSequenceLSTMOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset UnidirectionalSequenceLSTMOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const UnidirectionalSequenceLSTMOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const UnidirectionalSequenceLSTMOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _fused_activation_function = _o->fused_activation_function; + auto _cell_clip = _o->cell_clip; + auto _proj_clip = _o->proj_clip; + auto _time_major = _o->time_major; + auto _asymmetric_quantize_inputs = _o->asymmetric_quantize_inputs; + auto _diagonal_recurrent_tensors = _o->diagonal_recurrent_tensors; + return tflite::CreateUnidirectionalSequenceLSTMOptions( + _fbb, + _fused_activation_function, + _cell_clip, + _proj_clip, + _time_major, + _asymmetric_quantize_inputs, + _diagonal_recurrent_tensors); +} + +inline BidirectionalSequenceLSTMOptionsT *BidirectionalSequenceLSTMOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new BidirectionalSequenceLSTMOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void BidirectionalSequenceLSTMOptions::UnPackTo(BidirectionalSequenceLSTMOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } + { auto _e = cell_clip(); _o->cell_clip = _e; } + { auto _e = proj_clip(); _o->proj_clip = _e; } + { auto _e = merge_outputs(); _o->merge_outputs = _e; } + { auto _e = time_major(); _o->time_major = _e; } + { auto _e = asymmetric_quantize_inputs(); _o->asymmetric_quantize_inputs = _e; } +} + +inline ::flatbuffers::Offset CreateBidirectionalSequenceLSTMOptions(::flatbuffers::FlatBufferBuilder &_fbb, const BidirectionalSequenceLSTMOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return BidirectionalSequenceLSTMOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset BidirectionalSequenceLSTMOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const BidirectionalSequenceLSTMOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const BidirectionalSequenceLSTMOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _fused_activation_function = _o->fused_activation_function; + auto _cell_clip = _o->cell_clip; + auto _proj_clip = _o->proj_clip; + auto _merge_outputs = _o->merge_outputs; + auto _time_major = _o->time_major; + auto _asymmetric_quantize_inputs = _o->asymmetric_quantize_inputs; + return tflite::CreateBidirectionalSequenceLSTMOptions( + _fbb, + _fused_activation_function, + _cell_clip, + _proj_clip, + _merge_outputs, + _time_major, + _asymmetric_quantize_inputs); +} + +inline ResizeBilinearOptionsT *ResizeBilinearOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new ResizeBilinearOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void ResizeBilinearOptions::UnPackTo(ResizeBilinearOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = align_corners(); _o->align_corners = _e; } + { auto _e = half_pixel_centers(); _o->half_pixel_centers = _e; } +} + +inline ::flatbuffers::Offset CreateResizeBilinearOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ResizeBilinearOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return ResizeBilinearOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset ResizeBilinearOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ResizeBilinearOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const ResizeBilinearOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _align_corners = _o->align_corners; + auto _half_pixel_centers = _o->half_pixel_centers; + return tflite::CreateResizeBilinearOptions( + _fbb, + _align_corners, + _half_pixel_centers); +} + +inline ResizeNearestNeighborOptionsT *ResizeNearestNeighborOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new ResizeNearestNeighborOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void ResizeNearestNeighborOptions::UnPackTo(ResizeNearestNeighborOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = align_corners(); _o->align_corners = _e; } + { auto _e = half_pixel_centers(); _o->half_pixel_centers = _e; } +} + +inline ::flatbuffers::Offset CreateResizeNearestNeighborOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ResizeNearestNeighborOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return ResizeNearestNeighborOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset ResizeNearestNeighborOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ResizeNearestNeighborOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const ResizeNearestNeighborOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _align_corners = _o->align_corners; + auto _half_pixel_centers = _o->half_pixel_centers; + return tflite::CreateResizeNearestNeighborOptions( + _fbb, + _align_corners, + _half_pixel_centers); +} + +inline CallOptionsT *CallOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new CallOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void CallOptions::UnPackTo(CallOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = subgraph(); _o->subgraph = _e; } +} + +inline ::flatbuffers::Offset CreateCallOptions(::flatbuffers::FlatBufferBuilder &_fbb, const CallOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return CallOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset CallOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const CallOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const CallOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _subgraph = _o->subgraph; + return tflite::CreateCallOptions( + _fbb, + _subgraph); +} + +inline PadOptionsT *PadOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new PadOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void PadOptions::UnPackTo(PadOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreatePadOptions(::flatbuffers::FlatBufferBuilder &_fbb, const PadOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return PadOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset PadOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const PadOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const PadOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreatePadOptions( + _fbb); +} + +inline PadV2OptionsT *PadV2Options::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new PadV2OptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void PadV2Options::UnPackTo(PadV2OptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreatePadV2Options(::flatbuffers::FlatBufferBuilder &_fbb, const PadV2OptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return PadV2Options::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset PadV2Options::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const PadV2OptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const PadV2OptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreatePadV2Options( + _fbb); +} + +inline ReshapeOptionsT *ReshapeOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new ReshapeOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void ReshapeOptions::UnPackTo(ReshapeOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = new_shape(); if (_e) { _o->new_shape.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->new_shape[_i] = _e->Get(_i); } } else { _o->new_shape.resize(0); } } +} + +inline ::flatbuffers::Offset CreateReshapeOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ReshapeOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return ReshapeOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset ReshapeOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ReshapeOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const ReshapeOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _new_shape = _o->new_shape.size() ? _fbb.CreateVector(_o->new_shape) : 0; + return tflite::CreateReshapeOptions( + _fbb, + _new_shape); +} + +inline SpaceToBatchNDOptionsT *SpaceToBatchNDOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new SpaceToBatchNDOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void SpaceToBatchNDOptions::UnPackTo(SpaceToBatchNDOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateSpaceToBatchNDOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SpaceToBatchNDOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return SpaceToBatchNDOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset SpaceToBatchNDOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SpaceToBatchNDOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const SpaceToBatchNDOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateSpaceToBatchNDOptions( + _fbb); +} + +inline BatchToSpaceNDOptionsT *BatchToSpaceNDOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new BatchToSpaceNDOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void BatchToSpaceNDOptions::UnPackTo(BatchToSpaceNDOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateBatchToSpaceNDOptions(::flatbuffers::FlatBufferBuilder &_fbb, const BatchToSpaceNDOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return BatchToSpaceNDOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset BatchToSpaceNDOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const BatchToSpaceNDOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const BatchToSpaceNDOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateBatchToSpaceNDOptions( + _fbb); +} + +inline SkipGramOptionsT *SkipGramOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new SkipGramOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void SkipGramOptions::UnPackTo(SkipGramOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = ngram_size(); _o->ngram_size = _e; } + { auto _e = max_skip_size(); _o->max_skip_size = _e; } + { auto _e = include_all_ngrams(); _o->include_all_ngrams = _e; } +} + +inline ::flatbuffers::Offset CreateSkipGramOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SkipGramOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return SkipGramOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset SkipGramOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SkipGramOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const SkipGramOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _ngram_size = _o->ngram_size; + auto _max_skip_size = _o->max_skip_size; + auto _include_all_ngrams = _o->include_all_ngrams; + return tflite::CreateSkipGramOptions( + _fbb, + _ngram_size, + _max_skip_size, + _include_all_ngrams); +} + +inline SpaceToDepthOptionsT *SpaceToDepthOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new SpaceToDepthOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void SpaceToDepthOptions::UnPackTo(SpaceToDepthOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = block_size(); _o->block_size = _e; } +} + +inline ::flatbuffers::Offset CreateSpaceToDepthOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SpaceToDepthOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return SpaceToDepthOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset SpaceToDepthOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SpaceToDepthOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const SpaceToDepthOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _block_size = _o->block_size; + return tflite::CreateSpaceToDepthOptions( + _fbb, + _block_size); +} + +inline DepthToSpaceOptionsT *DepthToSpaceOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new DepthToSpaceOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void DepthToSpaceOptions::UnPackTo(DepthToSpaceOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = block_size(); _o->block_size = _e; } +} + +inline ::flatbuffers::Offset CreateDepthToSpaceOptions(::flatbuffers::FlatBufferBuilder &_fbb, const DepthToSpaceOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return DepthToSpaceOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset DepthToSpaceOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const DepthToSpaceOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const DepthToSpaceOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _block_size = _o->block_size; + return tflite::CreateDepthToSpaceOptions( + _fbb, + _block_size); +} + +inline SubOptionsT *SubOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new SubOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void SubOptions::UnPackTo(SubOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } + { auto _e = pot_scale_int16(); _o->pot_scale_int16 = _e; } +} + +inline ::flatbuffers::Offset CreateSubOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SubOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return SubOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset SubOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SubOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const SubOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _fused_activation_function = _o->fused_activation_function; + auto _pot_scale_int16 = _o->pot_scale_int16; + return tflite::CreateSubOptions( + _fbb, + _fused_activation_function, + _pot_scale_int16); +} + +inline DivOptionsT *DivOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new DivOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void DivOptions::UnPackTo(DivOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } +} + +inline ::flatbuffers::Offset CreateDivOptions(::flatbuffers::FlatBufferBuilder &_fbb, const DivOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return DivOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset DivOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const DivOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const DivOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _fused_activation_function = _o->fused_activation_function; + return tflite::CreateDivOptions( + _fbb, + _fused_activation_function); +} + +inline TopKV2OptionsT *TopKV2Options::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new TopKV2OptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void TopKV2Options::UnPackTo(TopKV2OptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateTopKV2Options(::flatbuffers::FlatBufferBuilder &_fbb, const TopKV2OptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return TopKV2Options::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset TopKV2Options::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const TopKV2OptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const TopKV2OptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateTopKV2Options( + _fbb); +} + +inline EmbeddingLookupSparseOptionsT *EmbeddingLookupSparseOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new EmbeddingLookupSparseOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void EmbeddingLookupSparseOptions::UnPackTo(EmbeddingLookupSparseOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = combiner(); _o->combiner = _e; } +} + +inline ::flatbuffers::Offset CreateEmbeddingLookupSparseOptions(::flatbuffers::FlatBufferBuilder &_fbb, const EmbeddingLookupSparseOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return EmbeddingLookupSparseOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset EmbeddingLookupSparseOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const EmbeddingLookupSparseOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const EmbeddingLookupSparseOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _combiner = _o->combiner; + return tflite::CreateEmbeddingLookupSparseOptions( + _fbb, + _combiner); +} + +inline GatherOptionsT *GatherOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new GatherOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void GatherOptions::UnPackTo(GatherOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = axis(); _o->axis = _e; } + { auto _e = batch_dims(); _o->batch_dims = _e; } +} + +inline ::flatbuffers::Offset CreateGatherOptions(::flatbuffers::FlatBufferBuilder &_fbb, const GatherOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return GatherOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset GatherOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const GatherOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const GatherOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _axis = _o->axis; + auto _batch_dims = _o->batch_dims; + return tflite::CreateGatherOptions( + _fbb, + _axis, + _batch_dims); +} + +inline TransposeOptionsT *TransposeOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new TransposeOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void TransposeOptions::UnPackTo(TransposeOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateTransposeOptions(::flatbuffers::FlatBufferBuilder &_fbb, const TransposeOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return TransposeOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset TransposeOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const TransposeOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const TransposeOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateTransposeOptions( + _fbb); +} + +inline ExpOptionsT *ExpOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new ExpOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void ExpOptions::UnPackTo(ExpOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateExpOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ExpOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return ExpOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset ExpOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ExpOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const ExpOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateExpOptions( + _fbb); +} + +inline CosOptionsT *CosOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new CosOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void CosOptions::UnPackTo(CosOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateCosOptions(::flatbuffers::FlatBufferBuilder &_fbb, const CosOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return CosOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset CosOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const CosOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const CosOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateCosOptions( + _fbb); +} + +inline ReducerOptionsT *ReducerOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new ReducerOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void ReducerOptions::UnPackTo(ReducerOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = keep_dims(); _o->keep_dims = _e; } +} + +inline ::flatbuffers::Offset CreateReducerOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ReducerOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return ReducerOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset ReducerOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ReducerOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const ReducerOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _keep_dims = _o->keep_dims; + return tflite::CreateReducerOptions( + _fbb, + _keep_dims); +} + +inline SqueezeOptionsT *SqueezeOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new SqueezeOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void SqueezeOptions::UnPackTo(SqueezeOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = squeeze_dims(); if (_e) { _o->squeeze_dims.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->squeeze_dims[_i] = _e->Get(_i); } } else { _o->squeeze_dims.resize(0); } } +} + +inline ::flatbuffers::Offset CreateSqueezeOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SqueezeOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return SqueezeOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset SqueezeOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SqueezeOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const SqueezeOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _squeeze_dims = _o->squeeze_dims.size() ? _fbb.CreateVector(_o->squeeze_dims) : 0; + return tflite::CreateSqueezeOptions( + _fbb, + _squeeze_dims); +} + +inline SplitOptionsT *SplitOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new SplitOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void SplitOptions::UnPackTo(SplitOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = num_splits(); _o->num_splits = _e; } +} + +inline ::flatbuffers::Offset CreateSplitOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SplitOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return SplitOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset SplitOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SplitOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const SplitOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _num_splits = _o->num_splits; + return tflite::CreateSplitOptions( + _fbb, + _num_splits); +} + +inline SplitVOptionsT *SplitVOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new SplitVOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void SplitVOptions::UnPackTo(SplitVOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = num_splits(); _o->num_splits = _e; } +} + +inline ::flatbuffers::Offset CreateSplitVOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SplitVOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return SplitVOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset SplitVOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SplitVOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const SplitVOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _num_splits = _o->num_splits; + return tflite::CreateSplitVOptions( + _fbb, + _num_splits); +} + +inline StridedSliceOptionsT *StridedSliceOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new StridedSliceOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void StridedSliceOptions::UnPackTo(StridedSliceOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = begin_mask(); _o->begin_mask = _e; } + { auto _e = end_mask(); _o->end_mask = _e; } + { auto _e = ellipsis_mask(); _o->ellipsis_mask = _e; } + { auto _e = new_axis_mask(); _o->new_axis_mask = _e; } + { auto _e = shrink_axis_mask(); _o->shrink_axis_mask = _e; } + { auto _e = offset(); _o->offset = _e; } +} + +inline ::flatbuffers::Offset CreateStridedSliceOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StridedSliceOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return StridedSliceOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset StridedSliceOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StridedSliceOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const StridedSliceOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _begin_mask = _o->begin_mask; + auto _end_mask = _o->end_mask; + auto _ellipsis_mask = _o->ellipsis_mask; + auto _new_axis_mask = _o->new_axis_mask; + auto _shrink_axis_mask = _o->shrink_axis_mask; + auto _offset = _o->offset; + return tflite::CreateStridedSliceOptions( + _fbb, + _begin_mask, + _end_mask, + _ellipsis_mask, + _new_axis_mask, + _shrink_axis_mask, + _offset); +} + +inline LogSoftmaxOptionsT *LogSoftmaxOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new LogSoftmaxOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void LogSoftmaxOptions::UnPackTo(LogSoftmaxOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateLogSoftmaxOptions(::flatbuffers::FlatBufferBuilder &_fbb, const LogSoftmaxOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return LogSoftmaxOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset LogSoftmaxOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const LogSoftmaxOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const LogSoftmaxOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateLogSoftmaxOptions( + _fbb); +} + +inline CastOptionsT *CastOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new CastOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void CastOptions::UnPackTo(CastOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = in_data_type(); _o->in_data_type = _e; } + { auto _e = out_data_type(); _o->out_data_type = _e; } +} + +inline ::flatbuffers::Offset CreateCastOptions(::flatbuffers::FlatBufferBuilder &_fbb, const CastOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return CastOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset CastOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const CastOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const CastOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _in_data_type = _o->in_data_type; + auto _out_data_type = _o->out_data_type; + return tflite::CreateCastOptions( + _fbb, + _in_data_type, + _out_data_type); +} + +inline DequantizeOptionsT *DequantizeOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new DequantizeOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void DequantizeOptions::UnPackTo(DequantizeOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateDequantizeOptions(::flatbuffers::FlatBufferBuilder &_fbb, const DequantizeOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return DequantizeOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset DequantizeOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const DequantizeOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const DequantizeOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateDequantizeOptions( + _fbb); +} + +inline MaximumMinimumOptionsT *MaximumMinimumOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new MaximumMinimumOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void MaximumMinimumOptions::UnPackTo(MaximumMinimumOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateMaximumMinimumOptions(::flatbuffers::FlatBufferBuilder &_fbb, const MaximumMinimumOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return MaximumMinimumOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset MaximumMinimumOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const MaximumMinimumOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const MaximumMinimumOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateMaximumMinimumOptions( + _fbb); +} + +inline TileOptionsT *TileOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new TileOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void TileOptions::UnPackTo(TileOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateTileOptions(::flatbuffers::FlatBufferBuilder &_fbb, const TileOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return TileOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset TileOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const TileOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const TileOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateTileOptions( + _fbb); +} + +inline ArgMaxOptionsT *ArgMaxOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new ArgMaxOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void ArgMaxOptions::UnPackTo(ArgMaxOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = output_type(); _o->output_type = _e; } +} + +inline ::flatbuffers::Offset CreateArgMaxOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ArgMaxOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return ArgMaxOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset ArgMaxOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ArgMaxOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const ArgMaxOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _output_type = _o->output_type; + return tflite::CreateArgMaxOptions( + _fbb, + _output_type); +} + +inline ArgMinOptionsT *ArgMinOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new ArgMinOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void ArgMinOptions::UnPackTo(ArgMinOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = output_type(); _o->output_type = _e; } +} + +inline ::flatbuffers::Offset CreateArgMinOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ArgMinOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return ArgMinOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset ArgMinOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ArgMinOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const ArgMinOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _output_type = _o->output_type; + return tflite::CreateArgMinOptions( + _fbb, + _output_type); +} + +inline GreaterOptionsT *GreaterOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new GreaterOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void GreaterOptions::UnPackTo(GreaterOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateGreaterOptions(::flatbuffers::FlatBufferBuilder &_fbb, const GreaterOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return GreaterOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset GreaterOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const GreaterOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const GreaterOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateGreaterOptions( + _fbb); +} + +inline GreaterEqualOptionsT *GreaterEqualOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new GreaterEqualOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void GreaterEqualOptions::UnPackTo(GreaterEqualOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateGreaterEqualOptions(::flatbuffers::FlatBufferBuilder &_fbb, const GreaterEqualOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return GreaterEqualOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset GreaterEqualOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const GreaterEqualOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const GreaterEqualOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateGreaterEqualOptions( + _fbb); +} + +inline LessOptionsT *LessOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new LessOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void LessOptions::UnPackTo(LessOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateLessOptions(::flatbuffers::FlatBufferBuilder &_fbb, const LessOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return LessOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset LessOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const LessOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const LessOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateLessOptions( + _fbb); +} + +inline LessEqualOptionsT *LessEqualOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new LessEqualOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void LessEqualOptions::UnPackTo(LessEqualOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateLessEqualOptions(::flatbuffers::FlatBufferBuilder &_fbb, const LessEqualOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return LessEqualOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset LessEqualOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const LessEqualOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const LessEqualOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateLessEqualOptions( + _fbb); +} + +inline NegOptionsT *NegOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new NegOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void NegOptions::UnPackTo(NegOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateNegOptions(::flatbuffers::FlatBufferBuilder &_fbb, const NegOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return NegOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset NegOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const NegOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const NegOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateNegOptions( + _fbb); +} + +inline SelectOptionsT *SelectOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new SelectOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void SelectOptions::UnPackTo(SelectOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateSelectOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SelectOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return SelectOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset SelectOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SelectOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const SelectOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateSelectOptions( + _fbb); +} + +inline SliceOptionsT *SliceOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new SliceOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void SliceOptions::UnPackTo(SliceOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateSliceOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SliceOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return SliceOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset SliceOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SliceOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const SliceOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateSliceOptions( + _fbb); +} + +inline TransposeConvOptionsT *TransposeConvOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new TransposeConvOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void TransposeConvOptions::UnPackTo(TransposeConvOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = padding(); _o->padding = _e; } + { auto _e = stride_w(); _o->stride_w = _e; } + { auto _e = stride_h(); _o->stride_h = _e; } + { auto _e = fused_activation_function(); _o->fused_activation_function = _e; } + { auto _e = quantized_bias_type(); _o->quantized_bias_type = _e; } +} + +inline ::flatbuffers::Offset CreateTransposeConvOptions(::flatbuffers::FlatBufferBuilder &_fbb, const TransposeConvOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return TransposeConvOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset TransposeConvOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const TransposeConvOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const TransposeConvOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _padding = _o->padding; + auto _stride_w = _o->stride_w; + auto _stride_h = _o->stride_h; + auto _fused_activation_function = _o->fused_activation_function; + auto _quantized_bias_type = _o->quantized_bias_type; + return tflite::CreateTransposeConvOptions( + _fbb, + _padding, + _stride_w, + _stride_h, + _fused_activation_function, + _quantized_bias_type); +} + +inline ExpandDimsOptionsT *ExpandDimsOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new ExpandDimsOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void ExpandDimsOptions::UnPackTo(ExpandDimsOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateExpandDimsOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ExpandDimsOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return ExpandDimsOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset ExpandDimsOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ExpandDimsOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const ExpandDimsOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateExpandDimsOptions( + _fbb); +} + +inline SparseToDenseOptionsT *SparseToDenseOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new SparseToDenseOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void SparseToDenseOptions::UnPackTo(SparseToDenseOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = validate_indices(); _o->validate_indices = _e; } +} + +inline ::flatbuffers::Offset CreateSparseToDenseOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SparseToDenseOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return SparseToDenseOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset SparseToDenseOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SparseToDenseOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const SparseToDenseOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _validate_indices = _o->validate_indices; + return tflite::CreateSparseToDenseOptions( + _fbb, + _validate_indices); +} + +inline EqualOptionsT *EqualOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new EqualOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void EqualOptions::UnPackTo(EqualOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateEqualOptions(::flatbuffers::FlatBufferBuilder &_fbb, const EqualOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return EqualOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset EqualOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const EqualOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const EqualOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateEqualOptions( + _fbb); +} + +inline NotEqualOptionsT *NotEqualOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new NotEqualOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void NotEqualOptions::UnPackTo(NotEqualOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateNotEqualOptions(::flatbuffers::FlatBufferBuilder &_fbb, const NotEqualOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return NotEqualOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset NotEqualOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const NotEqualOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const NotEqualOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateNotEqualOptions( + _fbb); +} + +inline ShapeOptionsT *ShapeOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new ShapeOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void ShapeOptions::UnPackTo(ShapeOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = out_type(); _o->out_type = _e; } +} + +inline ::flatbuffers::Offset CreateShapeOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ShapeOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return ShapeOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset ShapeOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ShapeOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const ShapeOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _out_type = _o->out_type; + return tflite::CreateShapeOptions( + _fbb, + _out_type); +} + +inline RankOptionsT *RankOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new RankOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void RankOptions::UnPackTo(RankOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateRankOptions(::flatbuffers::FlatBufferBuilder &_fbb, const RankOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return RankOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset RankOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const RankOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const RankOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateRankOptions( + _fbb); +} + +inline PowOptionsT *PowOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new PowOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void PowOptions::UnPackTo(PowOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreatePowOptions(::flatbuffers::FlatBufferBuilder &_fbb, const PowOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return PowOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset PowOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const PowOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const PowOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreatePowOptions( + _fbb); +} + +inline FakeQuantOptionsT *FakeQuantOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new FakeQuantOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void FakeQuantOptions::UnPackTo(FakeQuantOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = min(); _o->min = _e; } + { auto _e = max(); _o->max = _e; } + { auto _e = num_bits(); _o->num_bits = _e; } + { auto _e = narrow_range(); _o->narrow_range = _e; } +} + +inline ::flatbuffers::Offset CreateFakeQuantOptions(::flatbuffers::FlatBufferBuilder &_fbb, const FakeQuantOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return FakeQuantOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset FakeQuantOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const FakeQuantOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const FakeQuantOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _min = _o->min; + auto _max = _o->max; + auto _num_bits = _o->num_bits; + auto _narrow_range = _o->narrow_range; + return tflite::CreateFakeQuantOptions( + _fbb, + _min, + _max, + _num_bits, + _narrow_range); +} + +inline PackOptionsT *PackOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new PackOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void PackOptions::UnPackTo(PackOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = values_count(); _o->values_count = _e; } + { auto _e = axis(); _o->axis = _e; } +} + +inline ::flatbuffers::Offset CreatePackOptions(::flatbuffers::FlatBufferBuilder &_fbb, const PackOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return PackOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset PackOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const PackOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const PackOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _values_count = _o->values_count; + auto _axis = _o->axis; + return tflite::CreatePackOptions( + _fbb, + _values_count, + _axis); +} + +inline LogicalOrOptionsT *LogicalOrOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new LogicalOrOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void LogicalOrOptions::UnPackTo(LogicalOrOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateLogicalOrOptions(::flatbuffers::FlatBufferBuilder &_fbb, const LogicalOrOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return LogicalOrOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset LogicalOrOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const LogicalOrOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const LogicalOrOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateLogicalOrOptions( + _fbb); +} + +inline OneHotOptionsT *OneHotOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new OneHotOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void OneHotOptions::UnPackTo(OneHotOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = axis(); _o->axis = _e; } +} + +inline ::flatbuffers::Offset CreateOneHotOptions(::flatbuffers::FlatBufferBuilder &_fbb, const OneHotOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return OneHotOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset OneHotOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const OneHotOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const OneHotOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _axis = _o->axis; + return tflite::CreateOneHotOptions( + _fbb, + _axis); +} + +inline AbsOptionsT *AbsOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new AbsOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void AbsOptions::UnPackTo(AbsOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateAbsOptions(::flatbuffers::FlatBufferBuilder &_fbb, const AbsOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return AbsOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset AbsOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const AbsOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const AbsOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateAbsOptions( + _fbb); +} + +inline HardSwishOptionsT *HardSwishOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new HardSwishOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void HardSwishOptions::UnPackTo(HardSwishOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateHardSwishOptions(::flatbuffers::FlatBufferBuilder &_fbb, const HardSwishOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return HardSwishOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset HardSwishOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const HardSwishOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const HardSwishOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateHardSwishOptions( + _fbb); +} + +inline LogicalAndOptionsT *LogicalAndOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new LogicalAndOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void LogicalAndOptions::UnPackTo(LogicalAndOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateLogicalAndOptions(::flatbuffers::FlatBufferBuilder &_fbb, const LogicalAndOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return LogicalAndOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset LogicalAndOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const LogicalAndOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const LogicalAndOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateLogicalAndOptions( + _fbb); +} + +inline LogicalNotOptionsT *LogicalNotOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new LogicalNotOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void LogicalNotOptions::UnPackTo(LogicalNotOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateLogicalNotOptions(::flatbuffers::FlatBufferBuilder &_fbb, const LogicalNotOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return LogicalNotOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset LogicalNotOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const LogicalNotOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const LogicalNotOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateLogicalNotOptions( + _fbb); +} + +inline UnpackOptionsT *UnpackOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new UnpackOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void UnpackOptions::UnPackTo(UnpackOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = num(); _o->num = _e; } + { auto _e = axis(); _o->axis = _e; } +} + +inline ::flatbuffers::Offset CreateUnpackOptions(::flatbuffers::FlatBufferBuilder &_fbb, const UnpackOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return UnpackOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset UnpackOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const UnpackOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const UnpackOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _num = _o->num; + auto _axis = _o->axis; + return tflite::CreateUnpackOptions( + _fbb, + _num, + _axis); +} + +inline FloorDivOptionsT *FloorDivOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new FloorDivOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void FloorDivOptions::UnPackTo(FloorDivOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateFloorDivOptions(::flatbuffers::FlatBufferBuilder &_fbb, const FloorDivOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return FloorDivOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset FloorDivOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const FloorDivOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const FloorDivOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateFloorDivOptions( + _fbb); +} + +inline SquareOptionsT *SquareOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new SquareOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void SquareOptions::UnPackTo(SquareOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateSquareOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SquareOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return SquareOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset SquareOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SquareOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const SquareOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateSquareOptions( + _fbb); +} + +inline ZerosLikeOptionsT *ZerosLikeOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new ZerosLikeOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void ZerosLikeOptions::UnPackTo(ZerosLikeOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateZerosLikeOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ZerosLikeOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return ZerosLikeOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset ZerosLikeOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ZerosLikeOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const ZerosLikeOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateZerosLikeOptions( + _fbb); +} + +inline FillOptionsT *FillOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new FillOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void FillOptions::UnPackTo(FillOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateFillOptions(::flatbuffers::FlatBufferBuilder &_fbb, const FillOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return FillOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset FillOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const FillOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const FillOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateFillOptions( + _fbb); +} + +inline FloorModOptionsT *FloorModOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new FloorModOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void FloorModOptions::UnPackTo(FloorModOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateFloorModOptions(::flatbuffers::FlatBufferBuilder &_fbb, const FloorModOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return FloorModOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset FloorModOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const FloorModOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const FloorModOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateFloorModOptions( + _fbb); +} + +inline RangeOptionsT *RangeOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new RangeOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void RangeOptions::UnPackTo(RangeOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateRangeOptions(::flatbuffers::FlatBufferBuilder &_fbb, const RangeOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return RangeOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset RangeOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const RangeOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const RangeOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateRangeOptions( + _fbb); +} + +inline LeakyReluOptionsT *LeakyReluOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new LeakyReluOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void LeakyReluOptions::UnPackTo(LeakyReluOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = alpha(); _o->alpha = _e; } +} + +inline ::flatbuffers::Offset CreateLeakyReluOptions(::flatbuffers::FlatBufferBuilder &_fbb, const LeakyReluOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return LeakyReluOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset LeakyReluOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const LeakyReluOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const LeakyReluOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _alpha = _o->alpha; + return tflite::CreateLeakyReluOptions( + _fbb, + _alpha); +} + +inline SquaredDifferenceOptionsT *SquaredDifferenceOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new SquaredDifferenceOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void SquaredDifferenceOptions::UnPackTo(SquaredDifferenceOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateSquaredDifferenceOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SquaredDifferenceOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return SquaredDifferenceOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset SquaredDifferenceOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SquaredDifferenceOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const SquaredDifferenceOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateSquaredDifferenceOptions( + _fbb); +} + +inline MirrorPadOptionsT *MirrorPadOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new MirrorPadOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void MirrorPadOptions::UnPackTo(MirrorPadOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = mode(); _o->mode = _e; } +} + +inline ::flatbuffers::Offset CreateMirrorPadOptions(::flatbuffers::FlatBufferBuilder &_fbb, const MirrorPadOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return MirrorPadOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset MirrorPadOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const MirrorPadOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const MirrorPadOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _mode = _o->mode; + return tflite::CreateMirrorPadOptions( + _fbb, + _mode); +} + +inline UniqueOptionsT *UniqueOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new UniqueOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void UniqueOptions::UnPackTo(UniqueOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = idx_out_type(); _o->idx_out_type = _e; } +} + +inline ::flatbuffers::Offset CreateUniqueOptions(::flatbuffers::FlatBufferBuilder &_fbb, const UniqueOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return UniqueOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset UniqueOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const UniqueOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const UniqueOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _idx_out_type = _o->idx_out_type; + return tflite::CreateUniqueOptions( + _fbb, + _idx_out_type); +} + +inline ReverseV2OptionsT *ReverseV2Options::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new ReverseV2OptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void ReverseV2Options::UnPackTo(ReverseV2OptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateReverseV2Options(::flatbuffers::FlatBufferBuilder &_fbb, const ReverseV2OptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return ReverseV2Options::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset ReverseV2Options::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ReverseV2OptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const ReverseV2OptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateReverseV2Options( + _fbb); +} + +inline AddNOptionsT *AddNOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new AddNOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void AddNOptions::UnPackTo(AddNOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateAddNOptions(::flatbuffers::FlatBufferBuilder &_fbb, const AddNOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return AddNOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset AddNOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const AddNOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const AddNOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateAddNOptions( + _fbb); +} + +inline GatherNdOptionsT *GatherNdOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new GatherNdOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void GatherNdOptions::UnPackTo(GatherNdOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateGatherNdOptions(::flatbuffers::FlatBufferBuilder &_fbb, const GatherNdOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return GatherNdOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset GatherNdOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const GatherNdOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const GatherNdOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateGatherNdOptions( + _fbb); +} + +inline WhereOptionsT *WhereOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new WhereOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void WhereOptions::UnPackTo(WhereOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateWhereOptions(::flatbuffers::FlatBufferBuilder &_fbb, const WhereOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return WhereOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset WhereOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const WhereOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const WhereOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateWhereOptions( + _fbb); +} + +inline ReverseSequenceOptionsT *ReverseSequenceOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new ReverseSequenceOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void ReverseSequenceOptions::UnPackTo(ReverseSequenceOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = seq_dim(); _o->seq_dim = _e; } + { auto _e = batch_dim(); _o->batch_dim = _e; } +} + +inline ::flatbuffers::Offset CreateReverseSequenceOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ReverseSequenceOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return ReverseSequenceOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset ReverseSequenceOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ReverseSequenceOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const ReverseSequenceOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _seq_dim = _o->seq_dim; + auto _batch_dim = _o->batch_dim; + return tflite::CreateReverseSequenceOptions( + _fbb, + _seq_dim, + _batch_dim); +} + +inline MatrixDiagOptionsT *MatrixDiagOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new MatrixDiagOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void MatrixDiagOptions::UnPackTo(MatrixDiagOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateMatrixDiagOptions(::flatbuffers::FlatBufferBuilder &_fbb, const MatrixDiagOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return MatrixDiagOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset MatrixDiagOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const MatrixDiagOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const MatrixDiagOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateMatrixDiagOptions( + _fbb); +} + +inline QuantizeOptionsT *QuantizeOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new QuantizeOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void QuantizeOptions::UnPackTo(QuantizeOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateQuantizeOptions(::flatbuffers::FlatBufferBuilder &_fbb, const QuantizeOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return QuantizeOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset QuantizeOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const QuantizeOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const QuantizeOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateQuantizeOptions( + _fbb); +} + +inline MatrixSetDiagOptionsT *MatrixSetDiagOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new MatrixSetDiagOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void MatrixSetDiagOptions::UnPackTo(MatrixSetDiagOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateMatrixSetDiagOptions(::flatbuffers::FlatBufferBuilder &_fbb, const MatrixSetDiagOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return MatrixSetDiagOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset MatrixSetDiagOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const MatrixSetDiagOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const MatrixSetDiagOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateMatrixSetDiagOptions( + _fbb); +} + +inline IfOptionsT *IfOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new IfOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void IfOptions::UnPackTo(IfOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = then_subgraph_index(); _o->then_subgraph_index = _e; } + { auto _e = else_subgraph_index(); _o->else_subgraph_index = _e; } +} + +inline ::flatbuffers::Offset CreateIfOptions(::flatbuffers::FlatBufferBuilder &_fbb, const IfOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return IfOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset IfOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const IfOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const IfOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _then_subgraph_index = _o->then_subgraph_index; + auto _else_subgraph_index = _o->else_subgraph_index; + return tflite::CreateIfOptions( + _fbb, + _then_subgraph_index, + _else_subgraph_index); +} + +inline CallOnceOptionsT *CallOnceOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new CallOnceOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void CallOnceOptions::UnPackTo(CallOnceOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = init_subgraph_index(); _o->init_subgraph_index = _e; } +} + +inline ::flatbuffers::Offset CreateCallOnceOptions(::flatbuffers::FlatBufferBuilder &_fbb, const CallOnceOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return CallOnceOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset CallOnceOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const CallOnceOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const CallOnceOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _init_subgraph_index = _o->init_subgraph_index; + return tflite::CreateCallOnceOptions( + _fbb, + _init_subgraph_index); +} + +inline WhileOptionsT *WhileOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new WhileOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void WhileOptions::UnPackTo(WhileOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = cond_subgraph_index(); _o->cond_subgraph_index = _e; } + { auto _e = body_subgraph_index(); _o->body_subgraph_index = _e; } +} + +inline ::flatbuffers::Offset CreateWhileOptions(::flatbuffers::FlatBufferBuilder &_fbb, const WhileOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return WhileOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset WhileOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const WhileOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const WhileOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _cond_subgraph_index = _o->cond_subgraph_index; + auto _body_subgraph_index = _o->body_subgraph_index; + return tflite::CreateWhileOptions( + _fbb, + _cond_subgraph_index, + _body_subgraph_index); +} + +inline NonMaxSuppressionV4OptionsT *NonMaxSuppressionV4Options::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new NonMaxSuppressionV4OptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void NonMaxSuppressionV4Options::UnPackTo(NonMaxSuppressionV4OptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateNonMaxSuppressionV4Options(::flatbuffers::FlatBufferBuilder &_fbb, const NonMaxSuppressionV4OptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return NonMaxSuppressionV4Options::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset NonMaxSuppressionV4Options::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const NonMaxSuppressionV4OptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const NonMaxSuppressionV4OptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateNonMaxSuppressionV4Options( + _fbb); +} + +inline NonMaxSuppressionV5OptionsT *NonMaxSuppressionV5Options::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new NonMaxSuppressionV5OptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void NonMaxSuppressionV5Options::UnPackTo(NonMaxSuppressionV5OptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateNonMaxSuppressionV5Options(::flatbuffers::FlatBufferBuilder &_fbb, const NonMaxSuppressionV5OptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return NonMaxSuppressionV5Options::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset NonMaxSuppressionV5Options::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const NonMaxSuppressionV5OptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const NonMaxSuppressionV5OptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateNonMaxSuppressionV5Options( + _fbb); +} + +inline ScatterNdOptionsT *ScatterNdOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new ScatterNdOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void ScatterNdOptions::UnPackTo(ScatterNdOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateScatterNdOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ScatterNdOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return ScatterNdOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset ScatterNdOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ScatterNdOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const ScatterNdOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateScatterNdOptions( + _fbb); +} + +inline SelectV2OptionsT *SelectV2Options::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new SelectV2OptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void SelectV2Options::UnPackTo(SelectV2OptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateSelectV2Options(::flatbuffers::FlatBufferBuilder &_fbb, const SelectV2OptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return SelectV2Options::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset SelectV2Options::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SelectV2OptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const SelectV2OptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateSelectV2Options( + _fbb); +} + +inline DensifyOptionsT *DensifyOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new DensifyOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void DensifyOptions::UnPackTo(DensifyOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateDensifyOptions(::flatbuffers::FlatBufferBuilder &_fbb, const DensifyOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return DensifyOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset DensifyOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const DensifyOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const DensifyOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateDensifyOptions( + _fbb); +} + +inline SegmentSumOptionsT *SegmentSumOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new SegmentSumOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void SegmentSumOptions::UnPackTo(SegmentSumOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateSegmentSumOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SegmentSumOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return SegmentSumOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset SegmentSumOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SegmentSumOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const SegmentSumOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateSegmentSumOptions( + _fbb); +} + +inline BatchMatMulOptionsT *BatchMatMulOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new BatchMatMulOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void BatchMatMulOptions::UnPackTo(BatchMatMulOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = adj_x(); _o->adj_x = _e; } + { auto _e = adj_y(); _o->adj_y = _e; } + { auto _e = asymmetric_quantize_inputs(); _o->asymmetric_quantize_inputs = _e; } +} + +inline ::flatbuffers::Offset CreateBatchMatMulOptions(::flatbuffers::FlatBufferBuilder &_fbb, const BatchMatMulOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return BatchMatMulOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset BatchMatMulOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const BatchMatMulOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const BatchMatMulOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _adj_x = _o->adj_x; + auto _adj_y = _o->adj_y; + auto _asymmetric_quantize_inputs = _o->asymmetric_quantize_inputs; + return tflite::CreateBatchMatMulOptions( + _fbb, + _adj_x, + _adj_y, + _asymmetric_quantize_inputs); +} + +inline CumsumOptionsT *CumsumOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new CumsumOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void CumsumOptions::UnPackTo(CumsumOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = exclusive(); _o->exclusive = _e; } + { auto _e = reverse(); _o->reverse = _e; } +} + +inline ::flatbuffers::Offset CreateCumsumOptions(::flatbuffers::FlatBufferBuilder &_fbb, const CumsumOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return CumsumOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset CumsumOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const CumsumOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const CumsumOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _exclusive = _o->exclusive; + auto _reverse = _o->reverse; + return tflite::CreateCumsumOptions( + _fbb, + _exclusive, + _reverse); +} + +inline BroadcastToOptionsT *BroadcastToOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new BroadcastToOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void BroadcastToOptions::UnPackTo(BroadcastToOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateBroadcastToOptions(::flatbuffers::FlatBufferBuilder &_fbb, const BroadcastToOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return BroadcastToOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset BroadcastToOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const BroadcastToOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const BroadcastToOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateBroadcastToOptions( + _fbb); +} + +inline Rfft2dOptionsT *Rfft2dOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new Rfft2dOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void Rfft2dOptions::UnPackTo(Rfft2dOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateRfft2dOptions(::flatbuffers::FlatBufferBuilder &_fbb, const Rfft2dOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return Rfft2dOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset Rfft2dOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const Rfft2dOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const Rfft2dOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateRfft2dOptions( + _fbb); +} + +inline HashtableOptionsT *HashtableOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new HashtableOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void HashtableOptions::UnPackTo(HashtableOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = table_id(); _o->table_id = _e; } + { auto _e = key_dtype(); _o->key_dtype = _e; } + { auto _e = value_dtype(); _o->value_dtype = _e; } +} + +inline ::flatbuffers::Offset CreateHashtableOptions(::flatbuffers::FlatBufferBuilder &_fbb, const HashtableOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return HashtableOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset HashtableOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const HashtableOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const HashtableOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _table_id = _o->table_id; + auto _key_dtype = _o->key_dtype; + auto _value_dtype = _o->value_dtype; + return tflite::CreateHashtableOptions( + _fbb, + _table_id, + _key_dtype, + _value_dtype); +} + +inline HashtableFindOptionsT *HashtableFindOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new HashtableFindOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void HashtableFindOptions::UnPackTo(HashtableFindOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateHashtableFindOptions(::flatbuffers::FlatBufferBuilder &_fbb, const HashtableFindOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return HashtableFindOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset HashtableFindOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const HashtableFindOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const HashtableFindOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateHashtableFindOptions( + _fbb); +} + +inline HashtableImportOptionsT *HashtableImportOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new HashtableImportOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void HashtableImportOptions::UnPackTo(HashtableImportOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateHashtableImportOptions(::flatbuffers::FlatBufferBuilder &_fbb, const HashtableImportOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return HashtableImportOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset HashtableImportOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const HashtableImportOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const HashtableImportOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateHashtableImportOptions( + _fbb); +} + +inline HashtableSizeOptionsT *HashtableSizeOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new HashtableSizeOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void HashtableSizeOptions::UnPackTo(HashtableSizeOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateHashtableSizeOptions(::flatbuffers::FlatBufferBuilder &_fbb, const HashtableSizeOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return HashtableSizeOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset HashtableSizeOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const HashtableSizeOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const HashtableSizeOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateHashtableSizeOptions( + _fbb); +} + +inline VarHandleOptionsT *VarHandleOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new VarHandleOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void VarHandleOptions::UnPackTo(VarHandleOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = container(); if (_e) _o->container = _e->str(); } + { auto _e = shared_name(); if (_e) _o->shared_name = _e->str(); } +} + +inline ::flatbuffers::Offset CreateVarHandleOptions(::flatbuffers::FlatBufferBuilder &_fbb, const VarHandleOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return VarHandleOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset VarHandleOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const VarHandleOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const VarHandleOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _container = _o->container.empty() ? 0 : _fbb.CreateString(_o->container); + auto _shared_name = _o->shared_name.empty() ? 0 : _fbb.CreateString(_o->shared_name); + return tflite::CreateVarHandleOptions( + _fbb, + _container, + _shared_name); +} + +inline ReadVariableOptionsT *ReadVariableOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new ReadVariableOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void ReadVariableOptions::UnPackTo(ReadVariableOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateReadVariableOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ReadVariableOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return ReadVariableOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset ReadVariableOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ReadVariableOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const ReadVariableOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateReadVariableOptions( + _fbb); +} + +inline AssignVariableOptionsT *AssignVariableOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new AssignVariableOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void AssignVariableOptions::UnPackTo(AssignVariableOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateAssignVariableOptions(::flatbuffers::FlatBufferBuilder &_fbb, const AssignVariableOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return AssignVariableOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset AssignVariableOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const AssignVariableOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const AssignVariableOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateAssignVariableOptions( + _fbb); +} + +inline RandomOptionsT *RandomOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new RandomOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void RandomOptions::UnPackTo(RandomOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = seed(); _o->seed = _e; } + { auto _e = seed2(); _o->seed2 = _e; } +} + +inline ::flatbuffers::Offset CreateRandomOptions(::flatbuffers::FlatBufferBuilder &_fbb, const RandomOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return RandomOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset RandomOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const RandomOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const RandomOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _seed = _o->seed; + auto _seed2 = _o->seed2; + return tflite::CreateRandomOptions( + _fbb, + _seed, + _seed2); +} + +inline BucketizeOptionsT *BucketizeOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new BucketizeOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void BucketizeOptions::UnPackTo(BucketizeOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = boundaries(); if (_e) { _o->boundaries.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->boundaries[_i] = _e->Get(_i); } } else { _o->boundaries.resize(0); } } +} + +inline ::flatbuffers::Offset CreateBucketizeOptions(::flatbuffers::FlatBufferBuilder &_fbb, const BucketizeOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return BucketizeOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset BucketizeOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const BucketizeOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const BucketizeOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _boundaries = _o->boundaries.size() ? _fbb.CreateVector(_o->boundaries) : 0; + return tflite::CreateBucketizeOptions( + _fbb, + _boundaries); +} + +inline GeluOptionsT *GeluOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new GeluOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void GeluOptions::UnPackTo(GeluOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = approximate(); _o->approximate = _e; } +} + +inline ::flatbuffers::Offset CreateGeluOptions(::flatbuffers::FlatBufferBuilder &_fbb, const GeluOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return GeluOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset GeluOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const GeluOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const GeluOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _approximate = _o->approximate; + return tflite::CreateGeluOptions( + _fbb, + _approximate); +} + +inline DynamicUpdateSliceOptionsT *DynamicUpdateSliceOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new DynamicUpdateSliceOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void DynamicUpdateSliceOptions::UnPackTo(DynamicUpdateSliceOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateDynamicUpdateSliceOptions(::flatbuffers::FlatBufferBuilder &_fbb, const DynamicUpdateSliceOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return DynamicUpdateSliceOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset DynamicUpdateSliceOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const DynamicUpdateSliceOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const DynamicUpdateSliceOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateDynamicUpdateSliceOptions( + _fbb); +} + +inline UnsortedSegmentProdOptionsT *UnsortedSegmentProdOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new UnsortedSegmentProdOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void UnsortedSegmentProdOptions::UnPackTo(UnsortedSegmentProdOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateUnsortedSegmentProdOptions(::flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentProdOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return UnsortedSegmentProdOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset UnsortedSegmentProdOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentProdOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const UnsortedSegmentProdOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateUnsortedSegmentProdOptions( + _fbb); +} + +inline UnsortedSegmentMaxOptionsT *UnsortedSegmentMaxOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new UnsortedSegmentMaxOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void UnsortedSegmentMaxOptions::UnPackTo(UnsortedSegmentMaxOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateUnsortedSegmentMaxOptions(::flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentMaxOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return UnsortedSegmentMaxOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset UnsortedSegmentMaxOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentMaxOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const UnsortedSegmentMaxOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateUnsortedSegmentMaxOptions( + _fbb); +} + +inline UnsortedSegmentSumOptionsT *UnsortedSegmentSumOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new UnsortedSegmentSumOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void UnsortedSegmentSumOptions::UnPackTo(UnsortedSegmentSumOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateUnsortedSegmentSumOptions(::flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentSumOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return UnsortedSegmentSumOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset UnsortedSegmentSumOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentSumOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const UnsortedSegmentSumOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateUnsortedSegmentSumOptions( + _fbb); +} + +inline ATan2OptionsT *ATan2Options::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new ATan2OptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void ATan2Options::UnPackTo(ATan2OptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateATan2Options(::flatbuffers::FlatBufferBuilder &_fbb, const ATan2OptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return ATan2Options::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset ATan2Options::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ATan2OptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const ATan2OptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateATan2Options( + _fbb); +} + +inline UnsortedSegmentMinOptionsT *UnsortedSegmentMinOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new UnsortedSegmentMinOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void UnsortedSegmentMinOptions::UnPackTo(UnsortedSegmentMinOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateUnsortedSegmentMinOptions(::flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentMinOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return UnsortedSegmentMinOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset UnsortedSegmentMinOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const UnsortedSegmentMinOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const UnsortedSegmentMinOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateUnsortedSegmentMinOptions( + _fbb); +} + +inline SignOptionsT *SignOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new SignOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void SignOptions::UnPackTo(SignOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateSignOptions(::flatbuffers::FlatBufferBuilder &_fbb, const SignOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return SignOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset SignOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SignOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const SignOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateSignOptions( + _fbb); +} + +inline BitcastOptionsT *BitcastOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new BitcastOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void BitcastOptions::UnPackTo(BitcastOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateBitcastOptions(::flatbuffers::FlatBufferBuilder &_fbb, const BitcastOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return BitcastOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset BitcastOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const BitcastOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const BitcastOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateBitcastOptions( + _fbb); +} + +inline BitwiseXorOptionsT *BitwiseXorOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new BitwiseXorOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void BitwiseXorOptions::UnPackTo(BitwiseXorOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateBitwiseXorOptions(::flatbuffers::FlatBufferBuilder &_fbb, const BitwiseXorOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return BitwiseXorOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset BitwiseXorOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const BitwiseXorOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const BitwiseXorOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateBitwiseXorOptions( + _fbb); +} + +inline RightShiftOptionsT *RightShiftOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new RightShiftOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void RightShiftOptions::UnPackTo(RightShiftOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateRightShiftOptions(::flatbuffers::FlatBufferBuilder &_fbb, const RightShiftOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return RightShiftOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset RightShiftOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const RightShiftOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const RightShiftOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateRightShiftOptions( + _fbb); +} + +inline DilateOptionsT *DilateOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new DilateOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void DilateOptions::UnPackTo(DilateOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateDilateOptions(::flatbuffers::FlatBufferBuilder &_fbb, const DilateOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return DilateOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset DilateOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const DilateOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const DilateOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateDilateOptions( + _fbb); +} + +inline ReduceWindowOptionsT *ReduceWindowOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new ReduceWindowOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void ReduceWindowOptions::UnPackTo(ReduceWindowOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = reduce_function(); _o->reduce_function = _e; } +} + +inline ::flatbuffers::Offset CreateReduceWindowOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ReduceWindowOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return ReduceWindowOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset ReduceWindowOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ReduceWindowOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const ReduceWindowOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _reduce_function = _o->reduce_function; + return tflite::CreateReduceWindowOptions( + _fbb, + _reduce_function); +} + +inline OperatorCodeT *OperatorCode::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new OperatorCodeT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void OperatorCode::UnPackTo(OperatorCodeT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = deprecated_builtin_code(); _o->deprecated_builtin_code = _e; } + { auto _e = custom_code(); if (_e) _o->custom_code = _e->str(); } + { auto _e = version(); _o->version = _e; } + { auto _e = builtin_code(); _o->builtin_code = _e; } +} + +inline ::flatbuffers::Offset CreateOperatorCode(::flatbuffers::FlatBufferBuilder &_fbb, const OperatorCodeT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return OperatorCode::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset OperatorCode::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const OperatorCodeT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const OperatorCodeT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _deprecated_builtin_code = _o->deprecated_builtin_code; + auto _custom_code = _o->custom_code.empty() ? 0 : _fbb.CreateString(_o->custom_code); + auto _version = _o->version; + auto _builtin_code = _o->builtin_code; + return tflite::CreateOperatorCode( + _fbb, + _deprecated_builtin_code, + _custom_code, + _version, + _builtin_code); +} + +inline StableHLOCompositeOptionsT *StableHLOCompositeOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new StableHLOCompositeOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void StableHLOCompositeOptions::UnPackTo(StableHLOCompositeOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = name(); if (_e) _o->name = _e->str(); } + { auto _e = decomposition_subgraph_index(); _o->decomposition_subgraph_index = _e; } + { auto _e = composite_attributes(); if (_e) { _o->composite_attributes.resize(_e->size()); std::copy(_e->begin(), _e->end(), _o->composite_attributes.begin()); } } + { auto _e = composite_attributes_format(); _o->composite_attributes_format = _e; } + { auto _e = version(); _o->version = _e; } +} + +inline ::flatbuffers::Offset CreateStableHLOCompositeOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StableHLOCompositeOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return StableHLOCompositeOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset StableHLOCompositeOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StableHLOCompositeOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const StableHLOCompositeOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _name = _o->name.empty() ? 0 : _fbb.CreateString(_o->name); + auto _decomposition_subgraph_index = _o->decomposition_subgraph_index; + auto _composite_attributes = _o->composite_attributes.size() ? _fbb.CreateVector(_o->composite_attributes) : 0; + auto _composite_attributes_format = _o->composite_attributes_format; + auto _version = _o->version; + return tflite::CreateStableHLOCompositeOptions( + _fbb, + _name, + _decomposition_subgraph_index, + _composite_attributes, + _composite_attributes_format, + _version); +} + +inline StablehloShiftLeftOptionsT *StablehloShiftLeftOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new StablehloShiftLeftOptionsT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void StablehloShiftLeftOptions::UnPackTo(StablehloShiftLeftOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline ::flatbuffers::Offset CreateStablehloShiftLeftOptions(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloShiftLeftOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return StablehloShiftLeftOptions::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset StablehloShiftLeftOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const StablehloShiftLeftOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const StablehloShiftLeftOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return tflite::CreateStablehloShiftLeftOptions( + _fbb); +} + +inline OperatorT *Operator::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new OperatorT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void Operator::UnPackTo(OperatorT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = opcode_index(); _o->opcode_index = _e; } + { auto _e = inputs(); if (_e) { _o->inputs.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->inputs[_i] = _e->Get(_i); } } else { _o->inputs.resize(0); } } + { auto _e = outputs(); if (_e) { _o->outputs.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->outputs[_i] = _e->Get(_i); } } else { _o->outputs.resize(0); } } + { auto _e = builtin_options_type(); _o->builtin_options.type = _e; } + { auto _e = builtin_options(); if (_e) _o->builtin_options.value = tflite::BuiltinOptionsUnion::UnPack(_e, builtin_options_type(), _resolver); } + { auto _e = custom_options(); if (_e) { _o->custom_options.resize(_e->size()); std::copy(_e->begin(), _e->end(), _o->custom_options.begin()); } } + { auto _e = custom_options_format(); _o->custom_options_format = _e; } + { auto _e = mutating_variable_inputs(); if (_e) { _o->mutating_variable_inputs.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->mutating_variable_inputs[_i] = _e->Get(_i) != 0; } } else { _o->mutating_variable_inputs.resize(0); } } + { auto _e = intermediates(); if (_e) { _o->intermediates.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->intermediates[_i] = _e->Get(_i); } } else { _o->intermediates.resize(0); } } + { auto _e = large_custom_options_offset(); _o->large_custom_options_offset = _e; } + { auto _e = large_custom_options_size(); _o->large_custom_options_size = _e; } + { auto _e = builtin_options_2_type(); _o->builtin_options_2.type = _e; } + { auto _e = builtin_options_2(); if (_e) _o->builtin_options_2.value = tflite::BuiltinOptions2Union::UnPack(_e, builtin_options_2_type(), _resolver); } + { auto _e = debug_metadata_index(); _o->debug_metadata_index = _e; } +} + +inline ::flatbuffers::Offset CreateOperator(::flatbuffers::FlatBufferBuilder &_fbb, const OperatorT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return Operator::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset Operator::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const OperatorT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const OperatorT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _opcode_index = _o->opcode_index; + auto _inputs = _o->inputs.size() ? _fbb.CreateVector(_o->inputs) : 0; + auto _outputs = _o->outputs.size() ? _fbb.CreateVector(_o->outputs) : 0; + auto _builtin_options_type = _o->builtin_options.type; + auto _builtin_options = _o->builtin_options.Pack(_fbb); + auto _custom_options = _o->custom_options.size() ? _fbb.CreateVector(_o->custom_options) : 0; + auto _custom_options_format = _o->custom_options_format; + auto _mutating_variable_inputs = _o->mutating_variable_inputs.size() ? _fbb.CreateVector(_o->mutating_variable_inputs) : 0; + auto _intermediates = _o->intermediates.size() ? _fbb.CreateVector(_o->intermediates) : 0; + auto _large_custom_options_offset = _o->large_custom_options_offset; + auto _large_custom_options_size = _o->large_custom_options_size; + auto _builtin_options_2_type = _o->builtin_options_2.type; + auto _builtin_options_2 = _o->builtin_options_2.Pack(_fbb); + auto _debug_metadata_index = _o->debug_metadata_index; + return tflite::CreateOperator( + _fbb, + _opcode_index, + _inputs, + _outputs, + _builtin_options_type, + _builtin_options, + _custom_options, + _custom_options_format, + _mutating_variable_inputs, + _intermediates, + _large_custom_options_offset, + _large_custom_options_size, + _builtin_options_2_type, + _builtin_options_2, + _debug_metadata_index); +} + +inline SubGraphT::SubGraphT(const SubGraphT &o) + : inputs(o.inputs), + outputs(o.outputs), + name(o.name), + debug_metadata_index(o.debug_metadata_index) { + tensors.reserve(o.tensors.size()); + for (const auto &tensors_ : o.tensors) { tensors.emplace_back((tensors_) ? new tflite::TensorT(*tensors_) : nullptr); } + operators.reserve(o.operators.size()); + for (const auto &operators_ : o.operators) { operators.emplace_back((operators_) ? new tflite::OperatorT(*operators_) : nullptr); } +} + +inline SubGraphT &SubGraphT::operator=(SubGraphT o) FLATBUFFERS_NOEXCEPT { + std::swap(tensors, o.tensors); + std::swap(inputs, o.inputs); + std::swap(outputs, o.outputs); + std::swap(operators, o.operators); + std::swap(name, o.name); + std::swap(debug_metadata_index, o.debug_metadata_index); + return *this; +} + +inline SubGraphT *SubGraph::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new SubGraphT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void SubGraph::UnPackTo(SubGraphT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = tensors(); if (_e) { _o->tensors.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { if(_o->tensors[_i]) { _e->Get(_i)->UnPackTo(_o->tensors[_i].get(), _resolver); } else { _o->tensors[_i] = std::unique_ptr(_e->Get(_i)->UnPack(_resolver)); } } } else { _o->tensors.resize(0); } } + { auto _e = inputs(); if (_e) { _o->inputs.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->inputs[_i] = _e->Get(_i); } } else { _o->inputs.resize(0); } } + { auto _e = outputs(); if (_e) { _o->outputs.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->outputs[_i] = _e->Get(_i); } } else { _o->outputs.resize(0); } } + { auto _e = operators(); if (_e) { _o->operators.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { if(_o->operators[_i]) { _e->Get(_i)->UnPackTo(_o->operators[_i].get(), _resolver); } else { _o->operators[_i] = std::unique_ptr(_e->Get(_i)->UnPack(_resolver)); } } } else { _o->operators.resize(0); } } + { auto _e = name(); if (_e) _o->name = _e->str(); } + { auto _e = debug_metadata_index(); _o->debug_metadata_index = _e; } +} + +inline ::flatbuffers::Offset CreateSubGraph(::flatbuffers::FlatBufferBuilder &_fbb, const SubGraphT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return SubGraph::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset SubGraph::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SubGraphT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const SubGraphT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _tensors = _o->tensors.size() ? _fbb.CreateVector<::flatbuffers::Offset> (_o->tensors.size(), [](size_t i, _VectorArgs *__va) { return CreateTensor(*__va->__fbb, __va->__o->tensors[i].get(), __va->__rehasher); }, &_va ) : 0; + auto _inputs = _o->inputs.size() ? _fbb.CreateVector(_o->inputs) : 0; + auto _outputs = _o->outputs.size() ? _fbb.CreateVector(_o->outputs) : 0; + auto _operators = _o->operators.size() ? _fbb.CreateVector<::flatbuffers::Offset> (_o->operators.size(), [](size_t i, _VectorArgs *__va) { return CreateOperator(*__va->__fbb, __va->__o->operators[i].get(), __va->__rehasher); }, &_va ) : 0; + auto _name = _o->name.empty() ? 0 : _fbb.CreateString(_o->name); + auto _debug_metadata_index = _o->debug_metadata_index; + return tflite::CreateSubGraph( + _fbb, + _tensors, + _inputs, + _outputs, + _operators, + _name, + _debug_metadata_index); +} + +inline BufferT *Buffer::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new BufferT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void Buffer::UnPackTo(BufferT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = data(); if (_e) { _o->data.resize(_e->size()); std::copy(_e->begin(), _e->end(), _o->data.begin()); } } + { auto _e = offset(); _o->offset = _e; } + { auto _e = size(); _o->size = _e; } +} + +inline ::flatbuffers::Offset CreateBuffer(::flatbuffers::FlatBufferBuilder &_fbb, const BufferT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return Buffer::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset Buffer::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const BufferT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const BufferT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + _fbb.ForceVectorAlignment(_o->data.size(), sizeof(uint8_t), 16); + auto _data = _o->data.size() ? _fbb.CreateVector(_o->data) : 0; + auto _offset = _o->offset; + auto _size = _o->size; + return tflite::CreateBuffer( + _fbb, + _data, + _offset, + _size); +} + +inline ExternalBufferGroupT *ExternalBufferGroup::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new ExternalBufferGroupT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void ExternalBufferGroup::UnPackTo(ExternalBufferGroupT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = name(); if (_e) _o->name = _e->str(); } +} + +inline ::flatbuffers::Offset CreateExternalBufferGroup(::flatbuffers::FlatBufferBuilder &_fbb, const ExternalBufferGroupT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return ExternalBufferGroup::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset ExternalBufferGroup::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ExternalBufferGroupT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const ExternalBufferGroupT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _name = _o->name.empty() ? 0 : _fbb.CreateString(_o->name); + return tflite::CreateExternalBufferGroup( + _fbb, + _name); +} + +inline ExternalBufferT *ExternalBuffer::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new ExternalBufferT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void ExternalBuffer::UnPackTo(ExternalBufferT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = id(); _o->id = _e; } + { auto _e = group(); _o->group = _e; } + { auto _e = offset(); _o->offset = _e; } + { auto _e = length(); _o->length = _e; } + { auto _e = packing(); if (_e) _o->packing = _e->str(); } +} + +inline ::flatbuffers::Offset CreateExternalBuffer(::flatbuffers::FlatBufferBuilder &_fbb, const ExternalBufferT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return ExternalBuffer::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset ExternalBuffer::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ExternalBufferT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const ExternalBufferT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _id = _o->id; + auto _group = _o->group; + auto _offset = _o->offset; + auto _length = _o->length; + auto _packing = _o->packing.empty() ? 0 : _fbb.CreateString(_o->packing); + return tflite::CreateExternalBuffer( + _fbb, + _id, + _group, + _offset, + _length, + _packing); +} + +inline MetadataT *Metadata::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new MetadataT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void Metadata::UnPackTo(MetadataT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = name(); if (_e) _o->name = _e->str(); } + { auto _e = buffer(); _o->buffer = _e; } +} + +inline ::flatbuffers::Offset CreateMetadata(::flatbuffers::FlatBufferBuilder &_fbb, const MetadataT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return Metadata::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset Metadata::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const MetadataT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const MetadataT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _name = _o->name.empty() ? 0 : _fbb.CreateString(_o->name); + auto _buffer = _o->buffer; + return tflite::CreateMetadata( + _fbb, + _name, + _buffer); +} + +inline TensorMapT *TensorMap::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new TensorMapT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void TensorMap::UnPackTo(TensorMapT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = name(); if (_e) _o->name = _e->str(); } + { auto _e = tensor_index(); _o->tensor_index = _e; } +} + +inline ::flatbuffers::Offset CreateTensorMap(::flatbuffers::FlatBufferBuilder &_fbb, const TensorMapT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return TensorMap::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset TensorMap::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const TensorMapT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const TensorMapT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _name = _o->name.empty() ? 0 : _fbb.CreateString(_o->name); + auto _tensor_index = _o->tensor_index; + return tflite::CreateTensorMap( + _fbb, + _name, + _tensor_index); +} + +inline SignatureDefT::SignatureDefT(const SignatureDefT &o) + : signature_key(o.signature_key), + subgraph_index(o.subgraph_index) { + inputs.reserve(o.inputs.size()); + for (const auto &inputs_ : o.inputs) { inputs.emplace_back((inputs_) ? new tflite::TensorMapT(*inputs_) : nullptr); } + outputs.reserve(o.outputs.size()); + for (const auto &outputs_ : o.outputs) { outputs.emplace_back((outputs_) ? new tflite::TensorMapT(*outputs_) : nullptr); } +} + +inline SignatureDefT &SignatureDefT::operator=(SignatureDefT o) FLATBUFFERS_NOEXCEPT { + std::swap(inputs, o.inputs); + std::swap(outputs, o.outputs); + std::swap(signature_key, o.signature_key); + std::swap(subgraph_index, o.subgraph_index); + return *this; +} + +inline SignatureDefT *SignatureDef::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new SignatureDefT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void SignatureDef::UnPackTo(SignatureDefT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = inputs(); if (_e) { _o->inputs.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { if(_o->inputs[_i]) { _e->Get(_i)->UnPackTo(_o->inputs[_i].get(), _resolver); } else { _o->inputs[_i] = std::unique_ptr(_e->Get(_i)->UnPack(_resolver)); } } } else { _o->inputs.resize(0); } } + { auto _e = outputs(); if (_e) { _o->outputs.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { if(_o->outputs[_i]) { _e->Get(_i)->UnPackTo(_o->outputs[_i].get(), _resolver); } else { _o->outputs[_i] = std::unique_ptr(_e->Get(_i)->UnPack(_resolver)); } } } else { _o->outputs.resize(0); } } + { auto _e = signature_key(); if (_e) _o->signature_key = _e->str(); } + { auto _e = subgraph_index(); _o->subgraph_index = _e; } +} + +inline ::flatbuffers::Offset CreateSignatureDef(::flatbuffers::FlatBufferBuilder &_fbb, const SignatureDefT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return SignatureDef::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset SignatureDef::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SignatureDefT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const SignatureDefT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _inputs = _o->inputs.size() ? _fbb.CreateVector<::flatbuffers::Offset> (_o->inputs.size(), [](size_t i, _VectorArgs *__va) { return CreateTensorMap(*__va->__fbb, __va->__o->inputs[i].get(), __va->__rehasher); }, &_va ) : 0; + auto _outputs = _o->outputs.size() ? _fbb.CreateVector<::flatbuffers::Offset> (_o->outputs.size(), [](size_t i, _VectorArgs *__va) { return CreateTensorMap(*__va->__fbb, __va->__o->outputs[i].get(), __va->__rehasher); }, &_va ) : 0; + auto _signature_key = _o->signature_key.empty() ? 0 : _fbb.CreateString(_o->signature_key); + auto _subgraph_index = _o->subgraph_index; + return tflite::CreateSignatureDef( + _fbb, + _inputs, + _outputs, + _signature_key, + _subgraph_index); +} + +inline ModelT::ModelT(const ModelT &o) + : version(o.version), + description(o.description), + metadata_buffer(o.metadata_buffer) { + operator_codes.reserve(o.operator_codes.size()); + for (const auto &operator_codes_ : o.operator_codes) { operator_codes.emplace_back((operator_codes_) ? new tflite::OperatorCodeT(*operator_codes_) : nullptr); } + subgraphs.reserve(o.subgraphs.size()); + for (const auto &subgraphs_ : o.subgraphs) { subgraphs.emplace_back((subgraphs_) ? new tflite::SubGraphT(*subgraphs_) : nullptr); } + buffers.reserve(o.buffers.size()); + for (const auto &buffers_ : o.buffers) { buffers.emplace_back((buffers_) ? new tflite::BufferT(*buffers_) : nullptr); } + metadata.reserve(o.metadata.size()); + for (const auto &metadata_ : o.metadata) { metadata.emplace_back((metadata_) ? new tflite::MetadataT(*metadata_) : nullptr); } + signature_defs.reserve(o.signature_defs.size()); + for (const auto &signature_defs_ : o.signature_defs) { signature_defs.emplace_back((signature_defs_) ? new tflite::SignatureDefT(*signature_defs_) : nullptr); } + external_buffer_groups.reserve(o.external_buffer_groups.size()); + for (const auto &external_buffer_groups_ : o.external_buffer_groups) { external_buffer_groups.emplace_back((external_buffer_groups_) ? new tflite::ExternalBufferGroupT(*external_buffer_groups_) : nullptr); } + external_buffers.reserve(o.external_buffers.size()); + for (const auto &external_buffers_ : o.external_buffers) { external_buffers.emplace_back((external_buffers_) ? new tflite::ExternalBufferT(*external_buffers_) : nullptr); } +} + +inline ModelT &ModelT::operator=(ModelT o) FLATBUFFERS_NOEXCEPT { + std::swap(version, o.version); + std::swap(operator_codes, o.operator_codes); + std::swap(subgraphs, o.subgraphs); + std::swap(description, o.description); + std::swap(buffers, o.buffers); + std::swap(metadata_buffer, o.metadata_buffer); + std::swap(metadata, o.metadata); + std::swap(signature_defs, o.signature_defs); + std::swap(external_buffer_groups, o.external_buffer_groups); + std::swap(external_buffers, o.external_buffers); + return *this; +} + +inline ModelT *Model::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const { + auto _o = std::unique_ptr(new ModelT()); + UnPackTo(_o.get(), _resolver); + return _o.release(); +} + +inline void Model::UnPackTo(ModelT *_o, const ::flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = version(); _o->version = _e; } + { auto _e = operator_codes(); if (_e) { _o->operator_codes.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { if(_o->operator_codes[_i]) { _e->Get(_i)->UnPackTo(_o->operator_codes[_i].get(), _resolver); } else { _o->operator_codes[_i] = std::unique_ptr(_e->Get(_i)->UnPack(_resolver)); } } } else { _o->operator_codes.resize(0); } } + { auto _e = subgraphs(); if (_e) { _o->subgraphs.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { if(_o->subgraphs[_i]) { _e->Get(_i)->UnPackTo(_o->subgraphs[_i].get(), _resolver); } else { _o->subgraphs[_i] = std::unique_ptr(_e->Get(_i)->UnPack(_resolver)); } } } else { _o->subgraphs.resize(0); } } + { auto _e = description(); if (_e) _o->description = _e->str(); } + { auto _e = buffers(); if (_e) { _o->buffers.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { if(_o->buffers[_i]) { _e->Get(_i)->UnPackTo(_o->buffers[_i].get(), _resolver); } else { _o->buffers[_i] = std::unique_ptr(_e->Get(_i)->UnPack(_resolver)); } } } else { _o->buffers.resize(0); } } + { auto _e = metadata_buffer(); if (_e) { _o->metadata_buffer.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->metadata_buffer[_i] = _e->Get(_i); } } else { _o->metadata_buffer.resize(0); } } + { auto _e = metadata(); if (_e) { _o->metadata.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { if(_o->metadata[_i]) { _e->Get(_i)->UnPackTo(_o->metadata[_i].get(), _resolver); } else { _o->metadata[_i] = std::unique_ptr(_e->Get(_i)->UnPack(_resolver)); } } } else { _o->metadata.resize(0); } } + { auto _e = signature_defs(); if (_e) { _o->signature_defs.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { if(_o->signature_defs[_i]) { _e->Get(_i)->UnPackTo(_o->signature_defs[_i].get(), _resolver); } else { _o->signature_defs[_i] = std::unique_ptr(_e->Get(_i)->UnPack(_resolver)); } } } else { _o->signature_defs.resize(0); } } + { auto _e = external_buffer_groups(); if (_e) { _o->external_buffer_groups.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { if(_o->external_buffer_groups[_i]) { _e->Get(_i)->UnPackTo(_o->external_buffer_groups[_i].get(), _resolver); } else { _o->external_buffer_groups[_i] = std::unique_ptr(_e->Get(_i)->UnPack(_resolver)); } } } else { _o->external_buffer_groups.resize(0); } } + { auto _e = external_buffers(); if (_e) { _o->external_buffers.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { if(_o->external_buffers[_i]) { _e->Get(_i)->UnPackTo(_o->external_buffers[_i].get(), _resolver); } else { _o->external_buffers[_i] = std::unique_ptr(_e->Get(_i)->UnPack(_resolver)); } } } else { _o->external_buffers.resize(0); } } +} + +inline ::flatbuffers::Offset CreateModel(::flatbuffers::FlatBufferBuilder &_fbb, const ModelT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { + return Model::Pack(_fbb, _o, _rehasher); +} + +inline ::flatbuffers::Offset Model::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ModelT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const ModelT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _version = _o->version; + auto _operator_codes = _o->operator_codes.size() ? _fbb.CreateVector<::flatbuffers::Offset> (_o->operator_codes.size(), [](size_t i, _VectorArgs *__va) { return CreateOperatorCode(*__va->__fbb, __va->__o->operator_codes[i].get(), __va->__rehasher); }, &_va ) : 0; + auto _subgraphs = _o->subgraphs.size() ? _fbb.CreateVector<::flatbuffers::Offset> (_o->subgraphs.size(), [](size_t i, _VectorArgs *__va) { return CreateSubGraph(*__va->__fbb, __va->__o->subgraphs[i].get(), __va->__rehasher); }, &_va ) : 0; + auto _description = _o->description.empty() ? 0 : _fbb.CreateString(_o->description); + auto _buffers = _o->buffers.size() ? _fbb.CreateVector<::flatbuffers::Offset> (_o->buffers.size(), [](size_t i, _VectorArgs *__va) { return CreateBuffer(*__va->__fbb, __va->__o->buffers[i].get(), __va->__rehasher); }, &_va ) : 0; + auto _metadata_buffer = _o->metadata_buffer.size() ? _fbb.CreateVector(_o->metadata_buffer) : 0; + auto _metadata = _o->metadata.size() ? _fbb.CreateVector<::flatbuffers::Offset> (_o->metadata.size(), [](size_t i, _VectorArgs *__va) { return CreateMetadata(*__va->__fbb, __va->__o->metadata[i].get(), __va->__rehasher); }, &_va ) : 0; + auto _signature_defs = _o->signature_defs.size() ? _fbb.CreateVector<::flatbuffers::Offset> (_o->signature_defs.size(), [](size_t i, _VectorArgs *__va) { return CreateSignatureDef(*__va->__fbb, __va->__o->signature_defs[i].get(), __va->__rehasher); }, &_va ) : 0; + auto _external_buffer_groups = _o->external_buffer_groups.size() ? _fbb.CreateVector<::flatbuffers::Offset> (_o->external_buffer_groups.size(), [](size_t i, _VectorArgs *__va) { return CreateExternalBufferGroup(*__va->__fbb, __va->__o->external_buffer_groups[i].get(), __va->__rehasher); }, &_va ) : 0; + auto _external_buffers = _o->external_buffers.size() ? _fbb.CreateVector<::flatbuffers::Offset> (_o->external_buffers.size(), [](size_t i, _VectorArgs *__va) { return CreateExternalBuffer(*__va->__fbb, __va->__o->external_buffers[i].get(), __va->__rehasher); }, &_va ) : 0; + return tflite::CreateModel( + _fbb, + _version, + _operator_codes, + _subgraphs, + _description, + _buffers, + _metadata_buffer, + _metadata, + _signature_defs, + _external_buffer_groups, + _external_buffers); +} + +template +inline bool VerifyQuantizationDetails(::flatbuffers::VerifierTemplate &verifier, const void *obj, QuantizationDetails type) { + switch (type) { + case QuantizationDetails_NONE: { + return true; + } + case QuantizationDetails_CustomQuantization: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case QuantizationDetails_BlockwiseQuantization: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case QuantizationDetails_MultiAxisQuantization: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + default: return true; + } +} + +template +inline bool VerifyQuantizationDetailsVector(::flatbuffers::VerifierTemplate &verifier, const ::flatbuffers::Vector<::flatbuffers::Offset> *values, const ::flatbuffers::Vector *types) { + if (!values || !types) return !values && !types; + if (values->size() != types->size()) return false; + for (::flatbuffers::uoffset_t i = 0; i < values->size(); ++i) { + if (!VerifyQuantizationDetails( + verifier, values->Get(i), types->GetEnum(i))) { + return false; + } + } + return true; +} + +inline void *QuantizationDetailsUnion::UnPack(const void *obj, enum QuantizationDetails type, const ::flatbuffers::resolver_function_t *resolver) { + (void)resolver; + switch (type) { + case QuantizationDetails_CustomQuantization: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case QuantizationDetails_BlockwiseQuantization: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case QuantizationDetails_MultiAxisQuantization: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + default: return nullptr; + } +} + +inline ::flatbuffers::Offset QuantizationDetailsUnion::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ::flatbuffers::rehasher_function_t *_rehasher) const { + (void)_rehasher; + switch (type) { + case QuantizationDetails_CustomQuantization: { + auto ptr = reinterpret_cast(value); + return CreateCustomQuantization(_fbb, ptr, _rehasher).Union(); + } + case QuantizationDetails_BlockwiseQuantization: { + auto ptr = reinterpret_cast(value); + return CreateBlockwiseQuantization(_fbb, ptr, _rehasher).Union(); + } + case QuantizationDetails_MultiAxisQuantization: { + auto ptr = reinterpret_cast(value); + return CreateMultiAxisQuantization(_fbb, ptr, _rehasher).Union(); + } + default: return 0; + } +} + +inline QuantizationDetailsUnion::QuantizationDetailsUnion(const QuantizationDetailsUnion &u) : type(u.type), value(nullptr) { + switch (type) { + case QuantizationDetails_CustomQuantization: { + value = new tflite::CustomQuantizationT(*reinterpret_cast(u.value)); + break; + } + case QuantizationDetails_BlockwiseQuantization: { + value = new tflite::BlockwiseQuantizationT(*reinterpret_cast(u.value)); + break; + } + case QuantizationDetails_MultiAxisQuantization: { + value = new tflite::MultiAxisQuantizationT(*reinterpret_cast(u.value)); + break; + } + default: + break; + } +} + +inline void QuantizationDetailsUnion::Reset() { + switch (type) { + case QuantizationDetails_CustomQuantization: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case QuantizationDetails_BlockwiseQuantization: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case QuantizationDetails_MultiAxisQuantization: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + default: break; + } + value = nullptr; + type = QuantizationDetails_NONE; +} + +template +inline bool VerifySparseIndexVector(::flatbuffers::VerifierTemplate &verifier, const void *obj, SparseIndexVector type) { + switch (type) { + case SparseIndexVector_NONE: { + return true; + } + case SparseIndexVector_Int32Vector: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case SparseIndexVector_Uint16Vector: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case SparseIndexVector_Uint8Vector: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + default: return true; + } +} + +template +inline bool VerifySparseIndexVectorVector(::flatbuffers::VerifierTemplate &verifier, const ::flatbuffers::Vector<::flatbuffers::Offset> *values, const ::flatbuffers::Vector *types) { + if (!values || !types) return !values && !types; + if (values->size() != types->size()) return false; + for (::flatbuffers::uoffset_t i = 0; i < values->size(); ++i) { + if (!VerifySparseIndexVector( + verifier, values->Get(i), types->GetEnum(i))) { + return false; + } + } + return true; +} + +inline void *SparseIndexVectorUnion::UnPack(const void *obj, enum SparseIndexVector type, const ::flatbuffers::resolver_function_t *resolver) { + (void)resolver; + switch (type) { + case SparseIndexVector_Int32Vector: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case SparseIndexVector_Uint16Vector: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case SparseIndexVector_Uint8Vector: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + default: return nullptr; + } +} + +inline ::flatbuffers::Offset SparseIndexVectorUnion::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ::flatbuffers::rehasher_function_t *_rehasher) const { + (void)_rehasher; + switch (type) { + case SparseIndexVector_Int32Vector: { + auto ptr = reinterpret_cast(value); + return CreateInt32Vector(_fbb, ptr, _rehasher).Union(); + } + case SparseIndexVector_Uint16Vector: { + auto ptr = reinterpret_cast(value); + return CreateUint16Vector(_fbb, ptr, _rehasher).Union(); + } + case SparseIndexVector_Uint8Vector: { + auto ptr = reinterpret_cast(value); + return CreateUint8Vector(_fbb, ptr, _rehasher).Union(); + } + default: return 0; + } +} + +inline SparseIndexVectorUnion::SparseIndexVectorUnion(const SparseIndexVectorUnion &u) : type(u.type), value(nullptr) { + switch (type) { + case SparseIndexVector_Int32Vector: { + value = new tflite::Int32VectorT(*reinterpret_cast(u.value)); + break; + } + case SparseIndexVector_Uint16Vector: { + value = new tflite::Uint16VectorT(*reinterpret_cast(u.value)); + break; + } + case SparseIndexVector_Uint8Vector: { + value = new tflite::Uint8VectorT(*reinterpret_cast(u.value)); + break; + } + default: + break; + } +} + +inline void SparseIndexVectorUnion::Reset() { + switch (type) { + case SparseIndexVector_Int32Vector: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case SparseIndexVector_Uint16Vector: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case SparseIndexVector_Uint8Vector: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + default: break; + } + value = nullptr; + type = SparseIndexVector_NONE; +} + +template +inline bool VerifyBuiltinOptions(::flatbuffers::VerifierTemplate &verifier, const void *obj, BuiltinOptions type) { + switch (type) { + case BuiltinOptions_NONE: { + return true; + } + case BuiltinOptions_Conv2DOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_DepthwiseConv2DOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_ConcatEmbeddingsOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_LSHProjectionOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_Pool2DOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_SVDFOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_RNNOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_FullyConnectedOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_SoftmaxOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_ConcatenationOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_AddOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_L2NormOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_LocalResponseNormalizationOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_LSTMOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_ResizeBilinearOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_CallOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_ReshapeOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_SkipGramOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_SpaceToDepthOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_EmbeddingLookupSparseOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_MulOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_PadOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_GatherOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_BatchToSpaceNDOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_SpaceToBatchNDOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_TransposeOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_ReducerOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_SubOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_DivOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_SqueezeOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_SequenceRNNOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_StridedSliceOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_ExpOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_TopKV2Options: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_SplitOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_LogSoftmaxOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_CastOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_DequantizeOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_MaximumMinimumOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_ArgMaxOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_LessOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_NegOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_PadV2Options: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_GreaterOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_GreaterEqualOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_LessEqualOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_SelectOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_SliceOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_TransposeConvOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_SparseToDenseOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_TileOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_ExpandDimsOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_EqualOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_NotEqualOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_ShapeOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_PowOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_ArgMinOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_FakeQuantOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_PackOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_LogicalOrOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_OneHotOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_LogicalAndOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_LogicalNotOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_UnpackOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_FloorDivOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_SquareOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_ZerosLikeOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_FillOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_BidirectionalSequenceLSTMOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_BidirectionalSequenceRNNOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_UnidirectionalSequenceLSTMOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_FloorModOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_RangeOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_ResizeNearestNeighborOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_LeakyReluOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_SquaredDifferenceOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_MirrorPadOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_AbsOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_SplitVOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_UniqueOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_ReverseV2Options: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_AddNOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_GatherNdOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_CosOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_WhereOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_RankOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_ReverseSequenceOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_MatrixDiagOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_QuantizeOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_MatrixSetDiagOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_HardSwishOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_IfOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_WhileOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_DepthToSpaceOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_NonMaxSuppressionV4Options: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_NonMaxSuppressionV5Options: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_ScatterNdOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_SelectV2Options: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_DensifyOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_SegmentSumOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_BatchMatMulOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_CumsumOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_CallOnceOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_BroadcastToOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_Rfft2dOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_Conv3DOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_HashtableOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_HashtableFindOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_HashtableImportOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_HashtableSizeOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_VarHandleOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_ReadVariableOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_AssignVariableOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_RandomOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_BucketizeOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_GeluOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_DynamicUpdateSliceOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_UnsortedSegmentProdOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_UnsortedSegmentMaxOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_UnsortedSegmentMinOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_UnsortedSegmentSumOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_ATan2Options: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_SignOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_BitcastOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_BitwiseXorOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions_RightShiftOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + default: return true; + } +} + +template +inline bool VerifyBuiltinOptionsVector(::flatbuffers::VerifierTemplate &verifier, const ::flatbuffers::Vector<::flatbuffers::Offset> *values, const ::flatbuffers::Vector *types) { + if (!values || !types) return !values && !types; + if (values->size() != types->size()) return false; + for (::flatbuffers::uoffset_t i = 0; i < values->size(); ++i) { + if (!VerifyBuiltinOptions( + verifier, values->Get(i), types->GetEnum(i))) { + return false; + } + } + return true; +} + +inline void *BuiltinOptionsUnion::UnPack(const void *obj, enum BuiltinOptions type, const ::flatbuffers::resolver_function_t *resolver) { + (void)resolver; + switch (type) { + case BuiltinOptions_Conv2DOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_DepthwiseConv2DOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_ConcatEmbeddingsOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_LSHProjectionOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_Pool2DOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_SVDFOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_RNNOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_FullyConnectedOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_SoftmaxOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_ConcatenationOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_AddOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_L2NormOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_LocalResponseNormalizationOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_LSTMOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_ResizeBilinearOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_CallOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_ReshapeOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_SkipGramOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_SpaceToDepthOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_EmbeddingLookupSparseOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_MulOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_PadOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_GatherOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_BatchToSpaceNDOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_SpaceToBatchNDOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_TransposeOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_ReducerOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_SubOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_DivOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_SqueezeOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_SequenceRNNOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_StridedSliceOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_ExpOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_TopKV2Options: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_SplitOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_LogSoftmaxOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_CastOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_DequantizeOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_MaximumMinimumOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_ArgMaxOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_LessOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_NegOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_PadV2Options: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_GreaterOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_GreaterEqualOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_LessEqualOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_SelectOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_SliceOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_TransposeConvOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_SparseToDenseOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_TileOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_ExpandDimsOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_EqualOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_NotEqualOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_ShapeOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_PowOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_ArgMinOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_FakeQuantOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_PackOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_LogicalOrOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_OneHotOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_LogicalAndOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_LogicalNotOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_UnpackOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_FloorDivOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_SquareOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_ZerosLikeOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_FillOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_BidirectionalSequenceLSTMOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_BidirectionalSequenceRNNOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_UnidirectionalSequenceLSTMOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_FloorModOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_RangeOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_ResizeNearestNeighborOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_LeakyReluOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_SquaredDifferenceOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_MirrorPadOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_AbsOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_SplitVOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_UniqueOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_ReverseV2Options: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_AddNOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_GatherNdOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_CosOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_WhereOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_RankOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_ReverseSequenceOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_MatrixDiagOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_QuantizeOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_MatrixSetDiagOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_HardSwishOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_IfOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_WhileOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_DepthToSpaceOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_NonMaxSuppressionV4Options: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_NonMaxSuppressionV5Options: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_ScatterNdOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_SelectV2Options: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_DensifyOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_SegmentSumOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_BatchMatMulOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_CumsumOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_CallOnceOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_BroadcastToOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_Rfft2dOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_Conv3DOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_HashtableOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_HashtableFindOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_HashtableImportOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_HashtableSizeOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_VarHandleOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_ReadVariableOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_AssignVariableOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_RandomOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_BucketizeOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_GeluOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_DynamicUpdateSliceOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_UnsortedSegmentProdOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_UnsortedSegmentMaxOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_UnsortedSegmentMinOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_UnsortedSegmentSumOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_ATan2Options: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_SignOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_BitcastOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_BitwiseXorOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions_RightShiftOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + default: return nullptr; + } +} + +inline ::flatbuffers::Offset BuiltinOptionsUnion::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ::flatbuffers::rehasher_function_t *_rehasher) const { + (void)_rehasher; + switch (type) { + case BuiltinOptions_Conv2DOptions: { + auto ptr = reinterpret_cast(value); + return CreateConv2DOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_DepthwiseConv2DOptions: { + auto ptr = reinterpret_cast(value); + return CreateDepthwiseConv2DOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_ConcatEmbeddingsOptions: { + auto ptr = reinterpret_cast(value); + return CreateConcatEmbeddingsOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_LSHProjectionOptions: { + auto ptr = reinterpret_cast(value); + return CreateLSHProjectionOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_Pool2DOptions: { + auto ptr = reinterpret_cast(value); + return CreatePool2DOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_SVDFOptions: { + auto ptr = reinterpret_cast(value); + return CreateSVDFOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_RNNOptions: { + auto ptr = reinterpret_cast(value); + return CreateRNNOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_FullyConnectedOptions: { + auto ptr = reinterpret_cast(value); + return CreateFullyConnectedOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_SoftmaxOptions: { + auto ptr = reinterpret_cast(value); + return CreateSoftmaxOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_ConcatenationOptions: { + auto ptr = reinterpret_cast(value); + return CreateConcatenationOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_AddOptions: { + auto ptr = reinterpret_cast(value); + return CreateAddOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_L2NormOptions: { + auto ptr = reinterpret_cast(value); + return CreateL2NormOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_LocalResponseNormalizationOptions: { + auto ptr = reinterpret_cast(value); + return CreateLocalResponseNormalizationOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_LSTMOptions: { + auto ptr = reinterpret_cast(value); + return CreateLSTMOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_ResizeBilinearOptions: { + auto ptr = reinterpret_cast(value); + return CreateResizeBilinearOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_CallOptions: { + auto ptr = reinterpret_cast(value); + return CreateCallOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_ReshapeOptions: { + auto ptr = reinterpret_cast(value); + return CreateReshapeOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_SkipGramOptions: { + auto ptr = reinterpret_cast(value); + return CreateSkipGramOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_SpaceToDepthOptions: { + auto ptr = reinterpret_cast(value); + return CreateSpaceToDepthOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_EmbeddingLookupSparseOptions: { + auto ptr = reinterpret_cast(value); + return CreateEmbeddingLookupSparseOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_MulOptions: { + auto ptr = reinterpret_cast(value); + return CreateMulOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_PadOptions: { + auto ptr = reinterpret_cast(value); + return CreatePadOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_GatherOptions: { + auto ptr = reinterpret_cast(value); + return CreateGatherOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_BatchToSpaceNDOptions: { + auto ptr = reinterpret_cast(value); + return CreateBatchToSpaceNDOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_SpaceToBatchNDOptions: { + auto ptr = reinterpret_cast(value); + return CreateSpaceToBatchNDOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_TransposeOptions: { + auto ptr = reinterpret_cast(value); + return CreateTransposeOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_ReducerOptions: { + auto ptr = reinterpret_cast(value); + return CreateReducerOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_SubOptions: { + auto ptr = reinterpret_cast(value); + return CreateSubOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_DivOptions: { + auto ptr = reinterpret_cast(value); + return CreateDivOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_SqueezeOptions: { + auto ptr = reinterpret_cast(value); + return CreateSqueezeOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_SequenceRNNOptions: { + auto ptr = reinterpret_cast(value); + return CreateSequenceRNNOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_StridedSliceOptions: { + auto ptr = reinterpret_cast(value); + return CreateStridedSliceOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_ExpOptions: { + auto ptr = reinterpret_cast(value); + return CreateExpOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_TopKV2Options: { + auto ptr = reinterpret_cast(value); + return CreateTopKV2Options(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_SplitOptions: { + auto ptr = reinterpret_cast(value); + return CreateSplitOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_LogSoftmaxOptions: { + auto ptr = reinterpret_cast(value); + return CreateLogSoftmaxOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_CastOptions: { + auto ptr = reinterpret_cast(value); + return CreateCastOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_DequantizeOptions: { + auto ptr = reinterpret_cast(value); + return CreateDequantizeOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_MaximumMinimumOptions: { + auto ptr = reinterpret_cast(value); + return CreateMaximumMinimumOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_ArgMaxOptions: { + auto ptr = reinterpret_cast(value); + return CreateArgMaxOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_LessOptions: { + auto ptr = reinterpret_cast(value); + return CreateLessOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_NegOptions: { + auto ptr = reinterpret_cast(value); + return CreateNegOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_PadV2Options: { + auto ptr = reinterpret_cast(value); + return CreatePadV2Options(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_GreaterOptions: { + auto ptr = reinterpret_cast(value); + return CreateGreaterOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_GreaterEqualOptions: { + auto ptr = reinterpret_cast(value); + return CreateGreaterEqualOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_LessEqualOptions: { + auto ptr = reinterpret_cast(value); + return CreateLessEqualOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_SelectOptions: { + auto ptr = reinterpret_cast(value); + return CreateSelectOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_SliceOptions: { + auto ptr = reinterpret_cast(value); + return CreateSliceOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_TransposeConvOptions: { + auto ptr = reinterpret_cast(value); + return CreateTransposeConvOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_SparseToDenseOptions: { + auto ptr = reinterpret_cast(value); + return CreateSparseToDenseOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_TileOptions: { + auto ptr = reinterpret_cast(value); + return CreateTileOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_ExpandDimsOptions: { + auto ptr = reinterpret_cast(value); + return CreateExpandDimsOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_EqualOptions: { + auto ptr = reinterpret_cast(value); + return CreateEqualOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_NotEqualOptions: { + auto ptr = reinterpret_cast(value); + return CreateNotEqualOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_ShapeOptions: { + auto ptr = reinterpret_cast(value); + return CreateShapeOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_PowOptions: { + auto ptr = reinterpret_cast(value); + return CreatePowOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_ArgMinOptions: { + auto ptr = reinterpret_cast(value); + return CreateArgMinOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_FakeQuantOptions: { + auto ptr = reinterpret_cast(value); + return CreateFakeQuantOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_PackOptions: { + auto ptr = reinterpret_cast(value); + return CreatePackOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_LogicalOrOptions: { + auto ptr = reinterpret_cast(value); + return CreateLogicalOrOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_OneHotOptions: { + auto ptr = reinterpret_cast(value); + return CreateOneHotOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_LogicalAndOptions: { + auto ptr = reinterpret_cast(value); + return CreateLogicalAndOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_LogicalNotOptions: { + auto ptr = reinterpret_cast(value); + return CreateLogicalNotOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_UnpackOptions: { + auto ptr = reinterpret_cast(value); + return CreateUnpackOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_FloorDivOptions: { + auto ptr = reinterpret_cast(value); + return CreateFloorDivOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_SquareOptions: { + auto ptr = reinterpret_cast(value); + return CreateSquareOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_ZerosLikeOptions: { + auto ptr = reinterpret_cast(value); + return CreateZerosLikeOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_FillOptions: { + auto ptr = reinterpret_cast(value); + return CreateFillOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_BidirectionalSequenceLSTMOptions: { + auto ptr = reinterpret_cast(value); + return CreateBidirectionalSequenceLSTMOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_BidirectionalSequenceRNNOptions: { + auto ptr = reinterpret_cast(value); + return CreateBidirectionalSequenceRNNOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_UnidirectionalSequenceLSTMOptions: { + auto ptr = reinterpret_cast(value); + return CreateUnidirectionalSequenceLSTMOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_FloorModOptions: { + auto ptr = reinterpret_cast(value); + return CreateFloorModOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_RangeOptions: { + auto ptr = reinterpret_cast(value); + return CreateRangeOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_ResizeNearestNeighborOptions: { + auto ptr = reinterpret_cast(value); + return CreateResizeNearestNeighborOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_LeakyReluOptions: { + auto ptr = reinterpret_cast(value); + return CreateLeakyReluOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_SquaredDifferenceOptions: { + auto ptr = reinterpret_cast(value); + return CreateSquaredDifferenceOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_MirrorPadOptions: { + auto ptr = reinterpret_cast(value); + return CreateMirrorPadOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_AbsOptions: { + auto ptr = reinterpret_cast(value); + return CreateAbsOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_SplitVOptions: { + auto ptr = reinterpret_cast(value); + return CreateSplitVOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_UniqueOptions: { + auto ptr = reinterpret_cast(value); + return CreateUniqueOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_ReverseV2Options: { + auto ptr = reinterpret_cast(value); + return CreateReverseV2Options(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_AddNOptions: { + auto ptr = reinterpret_cast(value); + return CreateAddNOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_GatherNdOptions: { + auto ptr = reinterpret_cast(value); + return CreateGatherNdOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_CosOptions: { + auto ptr = reinterpret_cast(value); + return CreateCosOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_WhereOptions: { + auto ptr = reinterpret_cast(value); + return CreateWhereOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_RankOptions: { + auto ptr = reinterpret_cast(value); + return CreateRankOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_ReverseSequenceOptions: { + auto ptr = reinterpret_cast(value); + return CreateReverseSequenceOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_MatrixDiagOptions: { + auto ptr = reinterpret_cast(value); + return CreateMatrixDiagOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_QuantizeOptions: { + auto ptr = reinterpret_cast(value); + return CreateQuantizeOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_MatrixSetDiagOptions: { + auto ptr = reinterpret_cast(value); + return CreateMatrixSetDiagOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_HardSwishOptions: { + auto ptr = reinterpret_cast(value); + return CreateHardSwishOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_IfOptions: { + auto ptr = reinterpret_cast(value); + return CreateIfOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_WhileOptions: { + auto ptr = reinterpret_cast(value); + return CreateWhileOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_DepthToSpaceOptions: { + auto ptr = reinterpret_cast(value); + return CreateDepthToSpaceOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_NonMaxSuppressionV4Options: { + auto ptr = reinterpret_cast(value); + return CreateNonMaxSuppressionV4Options(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_NonMaxSuppressionV5Options: { + auto ptr = reinterpret_cast(value); + return CreateNonMaxSuppressionV5Options(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_ScatterNdOptions: { + auto ptr = reinterpret_cast(value); + return CreateScatterNdOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_SelectV2Options: { + auto ptr = reinterpret_cast(value); + return CreateSelectV2Options(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_DensifyOptions: { + auto ptr = reinterpret_cast(value); + return CreateDensifyOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_SegmentSumOptions: { + auto ptr = reinterpret_cast(value); + return CreateSegmentSumOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_BatchMatMulOptions: { + auto ptr = reinterpret_cast(value); + return CreateBatchMatMulOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_CumsumOptions: { + auto ptr = reinterpret_cast(value); + return CreateCumsumOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_CallOnceOptions: { + auto ptr = reinterpret_cast(value); + return CreateCallOnceOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_BroadcastToOptions: { + auto ptr = reinterpret_cast(value); + return CreateBroadcastToOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_Rfft2dOptions: { + auto ptr = reinterpret_cast(value); + return CreateRfft2dOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_Conv3DOptions: { + auto ptr = reinterpret_cast(value); + return CreateConv3DOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_HashtableOptions: { + auto ptr = reinterpret_cast(value); + return CreateHashtableOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_HashtableFindOptions: { + auto ptr = reinterpret_cast(value); + return CreateHashtableFindOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_HashtableImportOptions: { + auto ptr = reinterpret_cast(value); + return CreateHashtableImportOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_HashtableSizeOptions: { + auto ptr = reinterpret_cast(value); + return CreateHashtableSizeOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_VarHandleOptions: { + auto ptr = reinterpret_cast(value); + return CreateVarHandleOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_ReadVariableOptions: { + auto ptr = reinterpret_cast(value); + return CreateReadVariableOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_AssignVariableOptions: { + auto ptr = reinterpret_cast(value); + return CreateAssignVariableOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_RandomOptions: { + auto ptr = reinterpret_cast(value); + return CreateRandomOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_BucketizeOptions: { + auto ptr = reinterpret_cast(value); + return CreateBucketizeOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_GeluOptions: { + auto ptr = reinterpret_cast(value); + return CreateGeluOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_DynamicUpdateSliceOptions: { + auto ptr = reinterpret_cast(value); + return CreateDynamicUpdateSliceOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_UnsortedSegmentProdOptions: { + auto ptr = reinterpret_cast(value); + return CreateUnsortedSegmentProdOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_UnsortedSegmentMaxOptions: { + auto ptr = reinterpret_cast(value); + return CreateUnsortedSegmentMaxOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_UnsortedSegmentMinOptions: { + auto ptr = reinterpret_cast(value); + return CreateUnsortedSegmentMinOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_UnsortedSegmentSumOptions: { + auto ptr = reinterpret_cast(value); + return CreateUnsortedSegmentSumOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_ATan2Options: { + auto ptr = reinterpret_cast(value); + return CreateATan2Options(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_SignOptions: { + auto ptr = reinterpret_cast(value); + return CreateSignOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_BitcastOptions: { + auto ptr = reinterpret_cast(value); + return CreateBitcastOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_BitwiseXorOptions: { + auto ptr = reinterpret_cast(value); + return CreateBitwiseXorOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions_RightShiftOptions: { + auto ptr = reinterpret_cast(value); + return CreateRightShiftOptions(_fbb, ptr, _rehasher).Union(); + } + default: return 0; + } +} + +inline BuiltinOptionsUnion::BuiltinOptionsUnion(const BuiltinOptionsUnion &u) : type(u.type), value(nullptr) { + switch (type) { + case BuiltinOptions_Conv2DOptions: { + value = new tflite::Conv2DOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_DepthwiseConv2DOptions: { + value = new tflite::DepthwiseConv2DOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_ConcatEmbeddingsOptions: { + value = new tflite::ConcatEmbeddingsOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_LSHProjectionOptions: { + value = new tflite::LSHProjectionOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_Pool2DOptions: { + value = new tflite::Pool2DOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_SVDFOptions: { + value = new tflite::SVDFOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_RNNOptions: { + value = new tflite::RNNOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_FullyConnectedOptions: { + value = new tflite::FullyConnectedOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_SoftmaxOptions: { + value = new tflite::SoftmaxOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_ConcatenationOptions: { + value = new tflite::ConcatenationOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_AddOptions: { + value = new tflite::AddOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_L2NormOptions: { + value = new tflite::L2NormOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_LocalResponseNormalizationOptions: { + value = new tflite::LocalResponseNormalizationOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_LSTMOptions: { + value = new tflite::LSTMOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_ResizeBilinearOptions: { + value = new tflite::ResizeBilinearOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_CallOptions: { + value = new tflite::CallOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_ReshapeOptions: { + value = new tflite::ReshapeOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_SkipGramOptions: { + value = new tflite::SkipGramOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_SpaceToDepthOptions: { + value = new tflite::SpaceToDepthOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_EmbeddingLookupSparseOptions: { + value = new tflite::EmbeddingLookupSparseOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_MulOptions: { + value = new tflite::MulOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_PadOptions: { + value = new tflite::PadOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_GatherOptions: { + value = new tflite::GatherOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_BatchToSpaceNDOptions: { + value = new tflite::BatchToSpaceNDOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_SpaceToBatchNDOptions: { + value = new tflite::SpaceToBatchNDOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_TransposeOptions: { + value = new tflite::TransposeOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_ReducerOptions: { + value = new tflite::ReducerOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_SubOptions: { + value = new tflite::SubOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_DivOptions: { + value = new tflite::DivOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_SqueezeOptions: { + value = new tflite::SqueezeOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_SequenceRNNOptions: { + value = new tflite::SequenceRNNOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_StridedSliceOptions: { + value = new tflite::StridedSliceOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_ExpOptions: { + value = new tflite::ExpOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_TopKV2Options: { + value = new tflite::TopKV2OptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_SplitOptions: { + value = new tflite::SplitOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_LogSoftmaxOptions: { + value = new tflite::LogSoftmaxOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_CastOptions: { + value = new tflite::CastOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_DequantizeOptions: { + value = new tflite::DequantizeOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_MaximumMinimumOptions: { + value = new tflite::MaximumMinimumOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_ArgMaxOptions: { + value = new tflite::ArgMaxOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_LessOptions: { + value = new tflite::LessOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_NegOptions: { + value = new tflite::NegOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_PadV2Options: { + value = new tflite::PadV2OptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_GreaterOptions: { + value = new tflite::GreaterOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_GreaterEqualOptions: { + value = new tflite::GreaterEqualOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_LessEqualOptions: { + value = new tflite::LessEqualOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_SelectOptions: { + value = new tflite::SelectOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_SliceOptions: { + value = new tflite::SliceOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_TransposeConvOptions: { + value = new tflite::TransposeConvOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_SparseToDenseOptions: { + value = new tflite::SparseToDenseOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_TileOptions: { + value = new tflite::TileOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_ExpandDimsOptions: { + value = new tflite::ExpandDimsOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_EqualOptions: { + value = new tflite::EqualOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_NotEqualOptions: { + value = new tflite::NotEqualOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_ShapeOptions: { + value = new tflite::ShapeOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_PowOptions: { + value = new tflite::PowOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_ArgMinOptions: { + value = new tflite::ArgMinOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_FakeQuantOptions: { + value = new tflite::FakeQuantOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_PackOptions: { + value = new tflite::PackOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_LogicalOrOptions: { + value = new tflite::LogicalOrOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_OneHotOptions: { + value = new tflite::OneHotOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_LogicalAndOptions: { + value = new tflite::LogicalAndOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_LogicalNotOptions: { + value = new tflite::LogicalNotOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_UnpackOptions: { + value = new tflite::UnpackOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_FloorDivOptions: { + value = new tflite::FloorDivOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_SquareOptions: { + value = new tflite::SquareOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_ZerosLikeOptions: { + value = new tflite::ZerosLikeOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_FillOptions: { + value = new tflite::FillOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_BidirectionalSequenceLSTMOptions: { + value = new tflite::BidirectionalSequenceLSTMOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_BidirectionalSequenceRNNOptions: { + value = new tflite::BidirectionalSequenceRNNOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_UnidirectionalSequenceLSTMOptions: { + value = new tflite::UnidirectionalSequenceLSTMOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_FloorModOptions: { + value = new tflite::FloorModOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_RangeOptions: { + value = new tflite::RangeOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_ResizeNearestNeighborOptions: { + value = new tflite::ResizeNearestNeighborOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_LeakyReluOptions: { + value = new tflite::LeakyReluOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_SquaredDifferenceOptions: { + value = new tflite::SquaredDifferenceOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_MirrorPadOptions: { + value = new tflite::MirrorPadOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_AbsOptions: { + value = new tflite::AbsOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_SplitVOptions: { + value = new tflite::SplitVOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_UniqueOptions: { + value = new tflite::UniqueOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_ReverseV2Options: { + value = new tflite::ReverseV2OptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_AddNOptions: { + value = new tflite::AddNOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_GatherNdOptions: { + value = new tflite::GatherNdOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_CosOptions: { + value = new tflite::CosOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_WhereOptions: { + value = new tflite::WhereOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_RankOptions: { + value = new tflite::RankOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_ReverseSequenceOptions: { + value = new tflite::ReverseSequenceOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_MatrixDiagOptions: { + value = new tflite::MatrixDiagOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_QuantizeOptions: { + value = new tflite::QuantizeOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_MatrixSetDiagOptions: { + value = new tflite::MatrixSetDiagOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_HardSwishOptions: { + value = new tflite::HardSwishOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_IfOptions: { + value = new tflite::IfOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_WhileOptions: { + value = new tflite::WhileOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_DepthToSpaceOptions: { + value = new tflite::DepthToSpaceOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_NonMaxSuppressionV4Options: { + value = new tflite::NonMaxSuppressionV4OptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_NonMaxSuppressionV5Options: { + value = new tflite::NonMaxSuppressionV5OptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_ScatterNdOptions: { + value = new tflite::ScatterNdOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_SelectV2Options: { + value = new tflite::SelectV2OptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_DensifyOptions: { + value = new tflite::DensifyOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_SegmentSumOptions: { + value = new tflite::SegmentSumOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_BatchMatMulOptions: { + value = new tflite::BatchMatMulOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_CumsumOptions: { + value = new tflite::CumsumOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_CallOnceOptions: { + value = new tflite::CallOnceOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_BroadcastToOptions: { + value = new tflite::BroadcastToOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_Rfft2dOptions: { + value = new tflite::Rfft2dOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_Conv3DOptions: { + value = new tflite::Conv3DOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_HashtableOptions: { + value = new tflite::HashtableOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_HashtableFindOptions: { + value = new tflite::HashtableFindOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_HashtableImportOptions: { + value = new tflite::HashtableImportOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_HashtableSizeOptions: { + value = new tflite::HashtableSizeOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_VarHandleOptions: { + value = new tflite::VarHandleOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_ReadVariableOptions: { + value = new tflite::ReadVariableOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_AssignVariableOptions: { + value = new tflite::AssignVariableOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_RandomOptions: { + value = new tflite::RandomOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_BucketizeOptions: { + value = new tflite::BucketizeOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_GeluOptions: { + value = new tflite::GeluOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_DynamicUpdateSliceOptions: { + value = new tflite::DynamicUpdateSliceOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_UnsortedSegmentProdOptions: { + value = new tflite::UnsortedSegmentProdOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_UnsortedSegmentMaxOptions: { + value = new tflite::UnsortedSegmentMaxOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_UnsortedSegmentMinOptions: { + value = new tflite::UnsortedSegmentMinOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_UnsortedSegmentSumOptions: { + value = new tflite::UnsortedSegmentSumOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_ATan2Options: { + value = new tflite::ATan2OptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_SignOptions: { + value = new tflite::SignOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_BitcastOptions: { + value = new tflite::BitcastOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_BitwiseXorOptions: { + value = new tflite::BitwiseXorOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions_RightShiftOptions: { + value = new tflite::RightShiftOptionsT(*reinterpret_cast(u.value)); + break; + } + default: + break; + } +} + +inline void BuiltinOptionsUnion::Reset() { + switch (type) { + case BuiltinOptions_Conv2DOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_DepthwiseConv2DOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_ConcatEmbeddingsOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_LSHProjectionOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_Pool2DOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_SVDFOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_RNNOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_FullyConnectedOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_SoftmaxOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_ConcatenationOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_AddOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_L2NormOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_LocalResponseNormalizationOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_LSTMOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_ResizeBilinearOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_CallOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_ReshapeOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_SkipGramOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_SpaceToDepthOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_EmbeddingLookupSparseOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_MulOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_PadOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_GatherOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_BatchToSpaceNDOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_SpaceToBatchNDOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_TransposeOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_ReducerOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_SubOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_DivOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_SqueezeOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_SequenceRNNOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_StridedSliceOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_ExpOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_TopKV2Options: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_SplitOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_LogSoftmaxOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_CastOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_DequantizeOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_MaximumMinimumOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_ArgMaxOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_LessOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_NegOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_PadV2Options: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_GreaterOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_GreaterEqualOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_LessEqualOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_SelectOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_SliceOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_TransposeConvOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_SparseToDenseOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_TileOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_ExpandDimsOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_EqualOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_NotEqualOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_ShapeOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_PowOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_ArgMinOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_FakeQuantOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_PackOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_LogicalOrOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_OneHotOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_LogicalAndOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_LogicalNotOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_UnpackOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_FloorDivOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_SquareOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_ZerosLikeOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_FillOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_BidirectionalSequenceLSTMOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_BidirectionalSequenceRNNOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_UnidirectionalSequenceLSTMOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_FloorModOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_RangeOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_ResizeNearestNeighborOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_LeakyReluOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_SquaredDifferenceOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_MirrorPadOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_AbsOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_SplitVOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_UniqueOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_ReverseV2Options: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_AddNOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_GatherNdOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_CosOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_WhereOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_RankOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_ReverseSequenceOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_MatrixDiagOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_QuantizeOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_MatrixSetDiagOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_HardSwishOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_IfOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_WhileOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_DepthToSpaceOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_NonMaxSuppressionV4Options: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_NonMaxSuppressionV5Options: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_ScatterNdOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_SelectV2Options: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_DensifyOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_SegmentSumOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_BatchMatMulOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_CumsumOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_CallOnceOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_BroadcastToOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_Rfft2dOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_Conv3DOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_HashtableOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_HashtableFindOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_HashtableImportOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_HashtableSizeOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_VarHandleOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_ReadVariableOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_AssignVariableOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_RandomOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_BucketizeOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_GeluOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_DynamicUpdateSliceOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_UnsortedSegmentProdOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_UnsortedSegmentMaxOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_UnsortedSegmentMinOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_UnsortedSegmentSumOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_ATan2Options: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_SignOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_BitcastOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_BitwiseXorOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions_RightShiftOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + default: break; + } + value = nullptr; + type = BuiltinOptions_NONE; +} + +template +inline bool VerifyBuiltinOptions2(::flatbuffers::VerifierTemplate &verifier, const void *obj, BuiltinOptions2 type) { + switch (type) { + case BuiltinOptions2_NONE: { + return true; + } + case BuiltinOptions2_StablehloConcatenateOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions2_StablehloBroadcastInDimOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions2_StablehloSliceOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions2_StablehloConvolutionOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions2_StablehloCustomCallOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions2_StablehloReduceOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions2_StablehloScatterOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions2_StablehloCompareOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions2_StablehloDynamicSliceOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions2_StablehloPadOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions2_StablehloIotaOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions2_StablehloDotGeneralOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions2_StablehloReduceWindowOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions2_StablehloSortOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions2_StablehloWhileOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions2_StablehloGatherOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions2_StablehloTransposeOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions2_DilateOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions2_StablehloRngBitGeneratorOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions2_ReduceWindowOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions2_StableHLOCompositeOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions2_StablehloShiftLeftOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case BuiltinOptions2_StablehloCaseOptions: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + default: return true; + } +} + +template +inline bool VerifyBuiltinOptions2Vector(::flatbuffers::VerifierTemplate &verifier, const ::flatbuffers::Vector<::flatbuffers::Offset> *values, const ::flatbuffers::Vector *types) { + if (!values || !types) return !values && !types; + if (values->size() != types->size()) return false; + for (::flatbuffers::uoffset_t i = 0; i < values->size(); ++i) { + if (!VerifyBuiltinOptions2( + verifier, values->Get(i), types->GetEnum(i))) { + return false; + } + } + return true; +} + +inline void *BuiltinOptions2Union::UnPack(const void *obj, enum BuiltinOptions2 type, const ::flatbuffers::resolver_function_t *resolver) { + (void)resolver; + switch (type) { + case BuiltinOptions2_StablehloConcatenateOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions2_StablehloBroadcastInDimOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions2_StablehloSliceOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions2_StablehloConvolutionOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions2_StablehloCustomCallOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions2_StablehloReduceOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions2_StablehloScatterOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions2_StablehloCompareOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions2_StablehloDynamicSliceOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions2_StablehloPadOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions2_StablehloIotaOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions2_StablehloDotGeneralOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions2_StablehloReduceWindowOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions2_StablehloSortOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions2_StablehloWhileOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions2_StablehloGatherOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions2_StablehloTransposeOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions2_DilateOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions2_StablehloRngBitGeneratorOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions2_ReduceWindowOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions2_StableHLOCompositeOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions2_StablehloShiftLeftOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case BuiltinOptions2_StablehloCaseOptions: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + default: return nullptr; + } +} + +inline ::flatbuffers::Offset BuiltinOptions2Union::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ::flatbuffers::rehasher_function_t *_rehasher) const { + (void)_rehasher; + switch (type) { + case BuiltinOptions2_StablehloConcatenateOptions: { + auto ptr = reinterpret_cast(value); + return CreateStablehloConcatenateOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions2_StablehloBroadcastInDimOptions: { + auto ptr = reinterpret_cast(value); + return CreateStablehloBroadcastInDimOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions2_StablehloSliceOptions: { + auto ptr = reinterpret_cast(value); + return CreateStablehloSliceOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions2_StablehloConvolutionOptions: { + auto ptr = reinterpret_cast(value); + return CreateStablehloConvolutionOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions2_StablehloCustomCallOptions: { + auto ptr = reinterpret_cast(value); + return CreateStablehloCustomCallOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions2_StablehloReduceOptions: { + auto ptr = reinterpret_cast(value); + return CreateStablehloReduceOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions2_StablehloScatterOptions: { + auto ptr = reinterpret_cast(value); + return CreateStablehloScatterOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions2_StablehloCompareOptions: { + auto ptr = reinterpret_cast(value); + return CreateStablehloCompareOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions2_StablehloDynamicSliceOptions: { + auto ptr = reinterpret_cast(value); + return CreateStablehloDynamicSliceOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions2_StablehloPadOptions: { + auto ptr = reinterpret_cast(value); + return CreateStablehloPadOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions2_StablehloIotaOptions: { + auto ptr = reinterpret_cast(value); + return CreateStablehloIotaOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions2_StablehloDotGeneralOptions: { + auto ptr = reinterpret_cast(value); + return CreateStablehloDotGeneralOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions2_StablehloReduceWindowOptions: { + auto ptr = reinterpret_cast(value); + return CreateStablehloReduceWindowOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions2_StablehloSortOptions: { + auto ptr = reinterpret_cast(value); + return CreateStablehloSortOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions2_StablehloWhileOptions: { + auto ptr = reinterpret_cast(value); + return CreateStablehloWhileOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions2_StablehloGatherOptions: { + auto ptr = reinterpret_cast(value); + return CreateStablehloGatherOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions2_StablehloTransposeOptions: { + auto ptr = reinterpret_cast(value); + return CreateStablehloTransposeOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions2_DilateOptions: { + auto ptr = reinterpret_cast(value); + return CreateDilateOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions2_StablehloRngBitGeneratorOptions: { + auto ptr = reinterpret_cast(value); + return CreateStablehloRngBitGeneratorOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions2_ReduceWindowOptions: { + auto ptr = reinterpret_cast(value); + return CreateReduceWindowOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions2_StableHLOCompositeOptions: { + auto ptr = reinterpret_cast(value); + return CreateStableHLOCompositeOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions2_StablehloShiftLeftOptions: { + auto ptr = reinterpret_cast(value); + return CreateStablehloShiftLeftOptions(_fbb, ptr, _rehasher).Union(); + } + case BuiltinOptions2_StablehloCaseOptions: { + auto ptr = reinterpret_cast(value); + return CreateStablehloCaseOptions(_fbb, ptr, _rehasher).Union(); + } + default: return 0; + } +} + +inline BuiltinOptions2Union::BuiltinOptions2Union(const BuiltinOptions2Union &u) : type(u.type), value(nullptr) { + switch (type) { + case BuiltinOptions2_StablehloConcatenateOptions: { + value = new tflite::StablehloConcatenateOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions2_StablehloBroadcastInDimOptions: { + value = new tflite::StablehloBroadcastInDimOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions2_StablehloSliceOptions: { + value = new tflite::StablehloSliceOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions2_StablehloConvolutionOptions: { + value = new tflite::StablehloConvolutionOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions2_StablehloCustomCallOptions: { + value = new tflite::StablehloCustomCallOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions2_StablehloReduceOptions: { + value = new tflite::StablehloReduceOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions2_StablehloScatterOptions: { + value = new tflite::StablehloScatterOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions2_StablehloCompareOptions: { + value = new tflite::StablehloCompareOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions2_StablehloDynamicSliceOptions: { + value = new tflite::StablehloDynamicSliceOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions2_StablehloPadOptions: { + value = new tflite::StablehloPadOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions2_StablehloIotaOptions: { + value = new tflite::StablehloIotaOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions2_StablehloDotGeneralOptions: { + value = new tflite::StablehloDotGeneralOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions2_StablehloReduceWindowOptions: { + value = new tflite::StablehloReduceWindowOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions2_StablehloSortOptions: { + value = new tflite::StablehloSortOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions2_StablehloWhileOptions: { + value = new tflite::StablehloWhileOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions2_StablehloGatherOptions: { + value = new tflite::StablehloGatherOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions2_StablehloTransposeOptions: { + value = new tflite::StablehloTransposeOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions2_DilateOptions: { + value = new tflite::DilateOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions2_StablehloRngBitGeneratorOptions: { + value = new tflite::StablehloRngBitGeneratorOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions2_ReduceWindowOptions: { + value = new tflite::ReduceWindowOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions2_StableHLOCompositeOptions: { + value = new tflite::StableHLOCompositeOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions2_StablehloShiftLeftOptions: { + value = new tflite::StablehloShiftLeftOptionsT(*reinterpret_cast(u.value)); + break; + } + case BuiltinOptions2_StablehloCaseOptions: { + value = new tflite::StablehloCaseOptionsT(*reinterpret_cast(u.value)); + break; + } + default: + break; + } +} + +inline void BuiltinOptions2Union::Reset() { + switch (type) { + case BuiltinOptions2_StablehloConcatenateOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions2_StablehloBroadcastInDimOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions2_StablehloSliceOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions2_StablehloConvolutionOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions2_StablehloCustomCallOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions2_StablehloReduceOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions2_StablehloScatterOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions2_StablehloCompareOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions2_StablehloDynamicSliceOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions2_StablehloPadOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions2_StablehloIotaOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions2_StablehloDotGeneralOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions2_StablehloReduceWindowOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions2_StablehloSortOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions2_StablehloWhileOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions2_StablehloGatherOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions2_StablehloTransposeOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions2_DilateOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions2_StablehloRngBitGeneratorOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions2_ReduceWindowOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions2_StableHLOCompositeOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions2_StablehloShiftLeftOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case BuiltinOptions2_StablehloCaseOptions: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + default: break; + } + value = nullptr; + type = BuiltinOptions2_NONE; +} + +inline const tflite::Model *GetModel(const void *buf) { + return ::flatbuffers::GetRoot(buf); +} + +inline const tflite::Model *GetSizePrefixedModel(const void *buf) { + return ::flatbuffers::GetSizePrefixedRoot(buf); +} + +inline const char *ModelIdentifier() { + return "TFL3"; +} + +inline bool ModelBufferHasIdentifier(const void *buf) { + return ::flatbuffers::BufferHasIdentifier( + buf, ModelIdentifier()); +} + +inline bool SizePrefixedModelBufferHasIdentifier(const void *buf) { + return ::flatbuffers::BufferHasIdentifier( + buf, ModelIdentifier(), true); +} + +template +inline bool VerifyModelBuffer( + ::flatbuffers::VerifierTemplate &verifier) { + return verifier.template VerifyBuffer(ModelIdentifier()); +} + +template +inline bool VerifySizePrefixedModelBuffer( + ::flatbuffers::VerifierTemplate &verifier) { + return verifier.template VerifySizePrefixedBuffer(ModelIdentifier()); +} + +inline const char *ModelExtension() { + return "tflite"; +} + +inline void FinishModelBuffer( + ::flatbuffers::FlatBufferBuilder &fbb, + ::flatbuffers::Offset root) { + fbb.Finish(root, ModelIdentifier()); +} + +inline void FinishSizePrefixedModelBuffer( + ::flatbuffers::FlatBufferBuilder &fbb, + ::flatbuffers::Offset root) { + fbb.FinishSizePrefixed(root, ModelIdentifier()); +} + +inline std::unique_ptr UnPackModel( + const void *buf, + const ::flatbuffers::resolver_function_t *res = nullptr) { + return std::unique_ptr(GetModel(buf)->UnPack(res)); +} + +inline std::unique_ptr UnPackSizePrefixedModel( + const void *buf, + const ::flatbuffers::resolver_function_t *res = nullptr) { + return std::unique_ptr(GetSizePrefixedModel(buf)->UnPack(res)); +} + +} // namespace tflite + +#endif // FLATBUFFERS_GENERATED_SCHEMA_TFLITE_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_utils.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_utils.cc new file mode 100644 index 0000000000000000000000000000000000000000..e969f074806880bb6d0d4bf02c66887dcabf1ee3 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_utils.cc @@ -0,0 +1,113 @@ +/* Copyright 2020 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/compiler/mlir/lite/schema/schema_utils.h" + +#include +#include +#include +#include + +#include "tensorflow/compiler/mlir/lite/kernels/internal/compatibility_macros.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" + +namespace tflite { + +// The following GetBuiltinCode methods are the utility methods for reading +// builtin operator code, ensuring compatibility issues between v3 and v3a +// schema. Always the maximum value of the two fields always will be the correct +// value as follows: +// +// - Supporting schema version v3 models +// +// The `builtin_code` field is not available in the v3 models. Flatbuffer +// library will feed zero value, which is the default value in the v3a schema. +// The actual builtin operator code value will exist in the +// `deprecated_builtin_code` field. At the same time, it implies that +// `deprecated_builtin_code` >= `builtin_code` and the maximum value of the two +// fields will be same with `deprecated_builtin_code'. +// +// - Supporting builtin operator codes beyonds 127 +// +// New builtin operators, whose operator code is larger than 127, can not be +// assigned to the `deprecated_builtin_code` field. In such cases, the +// value of the `builtin_code` field should be used for the builtin operator +// code. In the case, the maximum value of the two fields will be the value of +// the `builtin_code` as the right value. + +BuiltinOperator GetBuiltinCode(const OperatorCode* op_code) { + // Caller should guarantee that the given argument value is not a nullptr. + TFLITE_DCHECK(op_code != nullptr); + + return std::max( + op_code->builtin_code(), + static_cast(op_code->deprecated_builtin_code())); +} + +BuiltinOperator GetBuiltinCode(const OperatorCodeT* op_code) { + // Caller should guarantee that the given argument value is not a nullptr. + TFLITE_DCHECK(op_code != nullptr); + + return std::max(op_code->builtin_code, static_cast( + op_code->deprecated_builtin_code)); +} + +size_t TensorTypeGetSize(::tflite::TensorType data_type) { + switch (data_type) { + case ::tflite::TensorType_FLOAT32: + static_assert(sizeof(float) == 4, ""); + return 4; + case ::tflite::TensorType_FLOAT16: + static_assert(sizeof(int16_t) == 2, ""); + return 2; + case ::tflite::TensorType_INT32: + static_assert(sizeof(int32_t) == 4, ""); + return 4; + case ::tflite::TensorType_UINT8: + static_assert(sizeof(uint8_t) == 1, ""); + return 1; + case ::tflite::TensorType_INT64: + static_assert(sizeof(int64_t) == 8, ""); + return 8; + case ::tflite::TensorType_BOOL: + return sizeof(bool); + case ::tflite::TensorType_INT16: + static_assert(sizeof(int16_t) == 2, ""); + return 2; + case ::tflite::TensorType_COMPLEX64: + static_assert(sizeof(std::complex) == 8, ""); + return 8; + case ::tflite::TensorType_INT8: + static_assert(sizeof(int8_t) == 1, ""); + return 1; + case ::tflite::TensorType_FLOAT64: + static_assert(sizeof(double) == 8, ""); + return 8; + case ::tflite::TensorType_COMPLEX128: + static_assert(sizeof(std::complex) == 16, ""); + return 16; + case ::tflite::TensorType_UINT64: + static_assert(sizeof(uint64_t) == 8, ""); + return 8; + case ::tflite::TensorType_UINT32: + static_assert(sizeof(uint32_t) == 4, ""); + return 4; + case ::tflite::TensorType_UINT16: + static_assert(sizeof(uint16_t) == 2, ""); + return 2; + default: + return 0; + } +} +} // namespace tflite diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_utils.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..bcf0cf68abef3e83022d2cefede03e3bd1fb9f32 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_utils.h @@ -0,0 +1,40 @@ +/* Copyright 2020 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_SCHEMA_SCHEMA_UTILS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_SCHEMA_SCHEMA_UTILS_H_ + +#include + +#include "flatbuffers/flatbuffers.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" + +namespace tflite { + +// The following methods are introduced to resolve op builtin code shortage +// problem. The new builtin operator will be assigned to the extended builtin +// code field in the flatbuffer schema. Those methods helps to hide builtin code +// details. +BuiltinOperator GetBuiltinCode(const OperatorCode *op_code); + +BuiltinOperator GetBuiltinCode(const OperatorCodeT *op_code); + +// Returns the size of the given TensorType in bytes, or 0 if the TensorType is +// not supported, this function should be aligned with TfLiteTypeGetSize in +// lite/kernels/kernel_util.h. +size_t TensorTypeGetSize(::tflite::TensorType data_type); + +} // namespace tflite + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_SCHEMA_SCHEMA_UTILS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_v0.fbs b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_v0.fbs new file mode 100644 index 0000000000000000000000000000000000000000..1a1702bc199b08914d08554458582ee7dbfb91fa --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_v0.fbs @@ -0,0 +1,247 @@ +// 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. + +namespace tflite; + +// The type of data stored in a tensor. +enum TensorType : byte { + FLOAT32 = 0, + FLOAT16 = 1, + INT32 = 2, + UINT8 = 3, + INT64 = 4, +} + +// Parameters for converting a quantized tensor back to float. Given a +// quantized value q, the corresponding float value f should be: +// f = scale * (q - zero_point) +table QuantizationParameters { + min:[float]; // For importing back into tensorflow. + max:[float]; // For importing back into tensorflow. + scale:[float]; + zero_point:[long]; +} + +table Tensor { + // The tensor shape. The meaning of each entry is operator-specific but + // builtin ops use: [batch size, height, width, number of channels] (That's + // Tensorflow's NHWC). + shape:[int]; + type:TensorType; + // The data_buffer is an opaque container, with the assumption that the + // target device is little-endian. In addition, all builtin operators assume + // the memory is ordered such that if `shape` is [4, 3, 2], then index + // [i, j, k] maps to data_buffer[i*4*3 + j*3 + k]. + data_buffer:[ubyte]; + name:string; // For debugging and importing back into tensorflow. + quantization:QuantizationParameters; // Optional. +} + +// A list of builtin operators. Builtin operators are slightly faster than custom +// ones, but not by much. Moreover, while custom operators accept an opaque +// object containing configuration parameters, builtins have a predetermined +// set of acceptable options. +enum BuiltinOperator : byte { + CUSTOM = 0, + CONVOLUTION = 1, + DEPTHWISE_CONVOLUTION = 2, + CONCAT_EMBEDDINGS = 3, + LSH_PROJECTION = 4, + TANH = 5, + RELU = 6, + AVERAGE_POOL = 7, + MAX_POOL = 8, + L2_POOL = 9, + SIGMOID = 10, + SVDF = 11, + BasicRNN = 12, + RELU6 = 13, + EMBEDDING_LOOKUP = 14, + FULLY_CONNECTED = 15, + HASHTABLE_LOOKUP = 16, + SOFTMAX = 17, + CONCATENATION = 18, + LSTM = 19, + ADD = 20, + L2NORM = 21, + LOCAL_RESPONSE_NORM = 22, + RESIZE_BILINEAR = 23, +} + +// Options for the builtin operators. +union BuiltinOptions { + ConvolutionOptions, + DepthwiseConvolutionOptions, + ConcatEmbeddingsOptions, + LSHProjectionOptions, + PoolOptions, + SVDFOptions, + BasicRNNOptions, + FullyConnectedOptions, + SoftmaxOptions, + ConcatenationOptions, + AddOptions, + L2NormOptions, + LocalResponseNormOptions, + LSTMOptions, + ResizeBilinearOptions, +} + +enum Padding : byte { SAME, VALID } + +enum ActivationFunctionType : byte { + NONE = 0, + RELU = 1, + RELU1 = 2, + RELU6 = 3, + TANH = 4, + SIGN_BIT = 5, +} + +table ConvolutionOptions { + padding:Padding; + stride_w:int; + stride_h:int; + fused_activation_function:ActivationFunctionType; +} + +table PoolOptions { + padding:Padding; + stride_w:int; + stride_h:int; + filter_width:int; + filter_height:int; + fused_activation_function:ActivationFunctionType; +} + +table DepthwiseConvolutionOptions { + padding:Padding; + stride_w:int; + stride_h:int; + depth_multiplier:int; + fused_activation_function:ActivationFunctionType; +} + +table ConcatEmbeddingsOptions { + num_channels:int; + num_columns_per_channel:[int]; + embedding_dim_per_channel:[int]; // This could be inferred from parameters. +} + +enum LSHProjectionType: byte { + UNKNOWN = 0, + SPARSE = 1, + DENSE = 2, +} + +table LSHProjectionOptions { + type: LSHProjectionType; +} + +table SVDFOptions { + rank:int; + fused_activation_function:ActivationFunctionType; +} + +// An implementation of TensorFlow BasicRNNCell. +table BasicRNNOptions { + fused_activation_function:ActivationFunctionType; +} + +// An implementation of TensorFlow fully_connected (a.k.a Dense) layer. +table FullyConnectedOptions { + fused_activation_function:ActivationFunctionType; +} + +table SoftmaxOptions { + beta: float; +} + +// An implementation of TensorFlow concat. +table ConcatenationOptions { + axis:int; + fused_activation_function:ActivationFunctionType; +} + +table AddOptions { + fused_activation_function:ActivationFunctionType; +} + +table L2NormOptions { + fused_activation_function:ActivationFunctionType; +} + +table LocalResponseNormOptions { + radius:int; + bias:float; + alpha:float; + beta:float; +} + +// An implementation of TensorFlow LSTMCell and CoupledInputForgetGateLSTMCell +table LSTMOptions { + fused_activation_function:ActivationFunctionType; + cell_clip: float; // Optional, 0.0 means no clipping + proj_clip: float; // Optional, 0.0 means no clipping +} + +table ResizeBilinearOptions { + new_height:int; + new_width:int; +} + +// An OperatorCode can be an enum value (BuiltinOperator) if the operator is a +// builtin, or a string if the operator is custom. +table OperatorCode { + builtin_code:BuiltinOperator; + custom_code:string; +} + +// An operator takes tensors as inputs and outputs. The type of operation being +// performed is determined by an index into the list of valid OperatorCodes, +// while the specifics of each operations is configured using builtin_options +// or custom_options. +table Operator { + // Index into the operator_codes array. Using an integer here avoids + // complicate map lookups. + opcode_index:int; + + inputs:[int]; + outputs:[int]; + + builtin_options:BuiltinOptions; + custom_options:[ubyte]; +} + +// The root type, defining a model. +table Model { + // A list of all tensors used in this model. + tensors:[Tensor]; + + // Indices of the input tensors. + inputs:[int]; + + // Indices of the output tensors. + outputs:[int]; + + // A list of all operator codes used in this model. This is + // kept in order because operators carry an index into this + // vector. + operator_codes:[OperatorCode]; + + // All operators, in execution order. + operators:[Operator]; +} + +root_type Model; diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_v1.fbs b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_v1.fbs new file mode 100644 index 0000000000000000000000000000000000000000..669bb0ab85b80ae8279c065da18a5375164feb70 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_v1.fbs @@ -0,0 +1,295 @@ +// 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. + +// Revision History +// Version 0: Initial version. +// Version 1: Add subgraphs to schema. + +namespace tflite; + +// The type of data stored in a tensor. +enum TensorType : byte { + FLOAT32 = 0, + FLOAT16 = 1, + INT32 = 2, + UINT8 = 3, + INT64 = 4, + STRING = 5, +} + +// Parameters for converting a quantized tensor back to float. Given a +// quantized value q, the corresponding float value f should be: +// f = scale * (q - zero_point) +table QuantizationParameters { + min:[float]; // For importing back into tensorflow. + max:[float]; // For importing back into tensorflow. + scale:[float]; + zero_point:[long]; +} + +table Tensor { + // The tensor shape. The meaning of each entry is operator-specific but + // builtin ops use: [batch size, height, width, number of channels] (That's + // Tensorflow's NHWC). + shape:[int]; + type:TensorType; + // The data_buffer is an opaque container, with the assumption that the + // target device is little-endian. In addition, all builtin operators assume + // the memory is ordered such that if `shape` is [4, 3, 2], then index + // [i, j, k] maps to data_buffer[i*3*2 + j*3 + k]. + data_buffer:[ubyte]; + name:string; // For debugging and importing back into tensorflow. + quantization:QuantizationParameters; // Optional. +} + +// A list of builtin operators. Builtin operators are slightly faster than custom +// ones, but not by much. Moreover, while custom operators accept an opaque +// object containing configuration parameters, builtins have a predetermined +// set of acceptable options. +enum BuiltinOperator : byte { + CUSTOM = 0, + CONVOLUTION = 1, + DEPTHWISE_CONVOLUTION = 2, + CONCAT_EMBEDDINGS = 3, + LSH_PROJECTION = 4, + TANH = 5, + RELU = 6, + AVERAGE_POOL = 7, + MAX_POOL = 8, + L2_POOL = 9, + SIGMOID = 10, + SVDF = 11, + BasicRNN = 12, + RELU6 = 13, + EMBEDDING_LOOKUP = 14, + FULLY_CONNECTED = 15, + HASHTABLE_LOOKUP = 16, + SOFTMAX = 17, + CONCATENATION = 18, + LSTM = 19, + ADD = 20, + L2NORM = 21, + LOCAL_RESPONSE_NORM = 22, + RESIZE_BILINEAR = 23, + CALL = 24, + RESHAPE = 25, + SKIP_GRAM = 26, + SPACE_TO_DEPTH = 27, +} + +// Options for the builtin operators. +union BuiltinOptions { + ConvolutionOptions, + DepthwiseConvolutionOptions, + ConcatEmbeddingsOptions, + LSHProjectionOptions, + PoolOptions, + SVDFOptions, + BasicRNNOptions, + FullyConnectedOptions, + SoftmaxOptions, + ConcatenationOptions, + AddOptions, + L2NormOptions, + LocalResponseNormOptions, + LSTMOptions, + ResizeBilinearOptions, + CallOptions, + ReshapeOptions, + SkipGramOptions, + SpaceToDepthOptions, +} + +enum Padding : byte { SAME, VALID } + +enum ActivationFunctionType : byte { + NONE = 0, + RELU = 1, + RELU1 = 2, + RELU6 = 3, + TANH = 4, + SIGN_BIT = 5, +} + +table ConvolutionOptions { + padding:Padding; + stride_w:int; + stride_h:int; + fused_activation_function:ActivationFunctionType; +} + +table PoolOptions { + padding:Padding; + stride_w:int; + stride_h:int; + filter_width:int; + filter_height:int; + fused_activation_function:ActivationFunctionType; +} + +table DepthwiseConvolutionOptions { + padding:Padding; + stride_w:int; + stride_h:int; + depth_multiplier:int; + fused_activation_function:ActivationFunctionType; +} + +table ConcatEmbeddingsOptions { + num_channels:int; + num_columns_per_channel:[int]; + embedding_dim_per_channel:[int]; // This could be inferred from parameters. +} + +enum LSHProjectionType: byte { + UNKNOWN = 0, + SPARSE = 1, + DENSE = 2, +} + +table LSHProjectionOptions { + type: LSHProjectionType; +} + +table SVDFOptions { + rank:int; + fused_activation_function:ActivationFunctionType; +} + +// An implementation of TensorFlow BasicRNNCell. +table BasicRNNOptions { + fused_activation_function:ActivationFunctionType; +} + +// An implementation of TensorFlow fully_connected (a.k.a Dense) layer. +table FullyConnectedOptions { + fused_activation_function:ActivationFunctionType; +} + +table SoftmaxOptions { + beta: float; +} + +// An implementation of TensorFlow concat. +table ConcatenationOptions { + axis:int; + fused_activation_function:ActivationFunctionType; +} + +table AddOptions { + fused_activation_function:ActivationFunctionType; +} + +table L2NormOptions { + fused_activation_function:ActivationFunctionType; +} + +table LocalResponseNormOptions { + radius:int; + bias:float; + alpha:float; + beta:float; +} + +// An implementation of TensorFlow LSTMCell and CoupledInputForgetGateLSTMCell +table LSTMOptions { + fused_activation_function:ActivationFunctionType; + cell_clip: float; // Optional, 0.0 means no clipping + proj_clip: float; // Optional, 0.0 means no clipping +} + +table ResizeBilinearOptions { + new_height:int; + new_width:int; +} + +// A call operation options +table CallOptions { + // The subgraph index that needs to be called. + subgraph:int; +} + +table ReshapeOptions { + new_shape:[int]; +} + +table SkipGramOptions { + ngram_size: int; + max_skip_size: int; + include_all_ngrams: bool; +} + +table SpaceToDepthOptions { + block_size: int; +} + +// An OperatorCode can be an enum value (BuiltinOperator) if the operator is a +// builtin, or a string if the operator is custom. +table OperatorCode { + builtin_code:BuiltinOperator; + custom_code:string; +} + +// An operator takes tensors as inputs and outputs. The type of operation being +// performed is determined by an index into the list of valid OperatorCodes, +// while the specifics of each operations is configured using builtin_options +// or custom_options. +table Operator { + // Index into the operator_codes array. Using an integer here avoids + // complicate map lookups. + opcode_index:int; + + inputs:[int]; + outputs:[int]; + + builtin_options:BuiltinOptions; + custom_options:[ubyte]; +} + +// The root type, defining a model. +table SubGraph { + // A list of all tensors used in this model. + tensors:[Tensor]; + + // Indices of the input tensors. + inputs:[int]; + + // Indices of the output tensors. + outputs:[int]; + + // All operators, in execution order. + operators:[Operator]; + + // Name of subgraph (used for debugging). + name:string; +} + +table Model { + // Version of the schema. + version:int; + + // A list of all operator codes used in this model. This is + // kept in order because operators carry an index into this + // vector. + operator_codes:[OperatorCode]; + + // All the subgraphs of the model. The 0th is assumed to be the main + // model. + subgraphs:[SubGraph]; + + // A description of the model. + description:string; +} + +root_type Model; diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_v2.fbs b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_v2.fbs new file mode 100644 index 0000000000000000000000000000000000000000..37d6df5dacac960055ace54b51d80d19f4b46a94 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_v2.fbs @@ -0,0 +1,302 @@ +// 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. + +// Revision History +// Version 0: Initial version. +// Version 1: Add subgraphs to schema. +// Version 2: Rename operators to conform to NN API. + +namespace tflite; + +// The type of data stored in a tensor. +enum TensorType : byte { + FLOAT32 = 0, + FLOAT16 = 1, + INT32 = 2, + UINT8 = 3, + INT64 = 4, + STRING = 5, +} + +// Parameters for converting a quantized tensor back to float. Given a +// quantized value q, the corresponding float value f should be: +// f = scale * (q - zero_point) +table QuantizationParameters { + min:[float]; // For importing back into tensorflow. + max:[float]; // For importing back into tensorflow. + scale:[float]; + zero_point:[long]; +} + +table Tensor { + // The tensor shape. The meaning of each entry is operator-specific but + // builtin ops use: [batch size, height, width, number of channels] (That's + // Tensorflow's NHWC). + shape:[int]; + type:TensorType; + // The data_buffer is an opaque container, with the assumption that the + // target device is little-endian. In addition, all builtin operators assume + // the memory is ordered such that if `shape` is [4, 3, 2], then index + // [i, j, k] maps to data_buffer[i*3*2 + j*3 + k]. + data_buffer:[ubyte]; + name:string; // For debugging and importing back into tensorflow. + quantization:QuantizationParameters; // Optional. +} + +// A list of builtin operators. Builtin operators are slightly faster than custom +// ones, but not by much. Moreover, while custom operators accept an opaque +// object containing configuration parameters, builtins have a predetermined +// set of acceptable options. +enum BuiltinOperator : byte { + ADD = 0, + AVERAGE_POOL_2D = 1, + CONCATENATION = 2, + CONV_2D = 3, + DEPTHWISE_CONV_2D = 4, + // DEPTH_TO_SPACE = 5, + // DEQUANTIZE = 6, + EMBEDDING_LOOKUP = 7, + // FLOOR = 8, + FULLY_CONNECTED = 9, + HASHTABLE_LOOKUP = 10, + L2_NORMALIZATION = 11, + L2_POOL_2D = 12, + LOCAL_RESPONSE_NORMALIZATION = 13, + LOGISTIC = 14, + LSH_PROJECTION = 15, + LSTM = 16, + MAX_POOL_2D = 17, + // MUL = 18, + RELU = 19, + // RELU1=20, + RELU6 = 21, + RESHAPE = 22, + RESIZE_BILINEAR = 23, + RNN = 24, + SOFTMAX = 25, + SPACE_TO_DEPTH = 26, + SVDF = 27, + TANH = 28, + CONCAT_EMBEDDINGS = 29, + SKIP_GRAM = 30, + CALL = 31, + CUSTOM = 32, + +} + +// Options for the builtin operators. +union BuiltinOptions { + Conv2DOptions, + DepthwiseConv2DOptions, + ConcatEmbeddingsOptions, + LSHProjectionOptions, + Pool2DOptions, + SVDFOptions, + RNNOptions, + FullyConnectedOptions, + SoftmaxOptions, + ConcatenationOptions, + AddOptions, + L2NormOptions, + LocalResponseNormalizationOptions, + LSTMOptions, + ResizeBilinearOptions, + CallOptions, + ReshapeOptions, + SkipGramOptions, + SpaceToDepthOptions, +} + +enum Padding : byte { SAME, VALID } + +enum ActivationFunctionType : byte { + NONE = 0, + RELU = 1, + RELU1 = 2, + RELU6 = 3, + TANH = 4, + SIGN_BIT = 5, +} + +table Conv2DOptions { + padding:Padding; + stride_w:int; + stride_h:int; + fused_activation_function:ActivationFunctionType; +} + +table Pool2DOptions { + padding:Padding; + stride_w:int; + stride_h:int; + filter_width:int; + filter_height:int; + fused_activation_function:ActivationFunctionType; +} + +table DepthwiseConv2DOptions { + padding:Padding; + stride_w:int; + stride_h:int; + depth_multiplier:int; + fused_activation_function:ActivationFunctionType; +} + +table ConcatEmbeddingsOptions { + num_channels:int; + num_columns_per_channel:[int]; + embedding_dim_per_channel:[int]; // This could be inferred from parameters. +} + +enum LSHProjectionType: byte { + UNKNOWN = 0, + SPARSE = 1, + DENSE = 2, +} + +table LSHProjectionOptions { + type: LSHProjectionType; +} + +table SVDFOptions { + rank:int; + fused_activation_function:ActivationFunctionType; +} + +// An implementation of TensorFlow RNNCell. +table RNNOptions { + fused_activation_function:ActivationFunctionType; +} + +// An implementation of TensorFlow fully_connected (a.k.a Dense) layer. +table FullyConnectedOptions { + fused_activation_function:ActivationFunctionType; +} + +table SoftmaxOptions { + beta: float; +} + +// An implementation of TensorFlow concat. +table ConcatenationOptions { + axis:int; + fused_activation_function:ActivationFunctionType; +} + +table AddOptions { + fused_activation_function:ActivationFunctionType; +} + +table L2NormOptions { + fused_activation_function:ActivationFunctionType; +} + +table LocalResponseNormalizationOptions { + radius:int; + bias:float; + alpha:float; + beta:float; +} + +// An implementation of TensorFlow LSTMCell and CoupledInputForgetGateLSTMCell +table LSTMOptions { + fused_activation_function:ActivationFunctionType; + cell_clip: float; // Optional, 0.0 means no clipping + proj_clip: float; // Optional, 0.0 means no clipping +} + +table ResizeBilinearOptions { + new_height:int; + new_width:int; +} + +// A call operation options +table CallOptions { + // The subgraph index that needs to be called. + subgraph:int; +} + +table ReshapeOptions { + new_shape:[int]; +} + +table SkipGramOptions { + ngram_size: int; + max_skip_size: int; + include_all_ngrams: bool; +} + +table SpaceToDepthOptions { + block_size: int; +} + +// An OperatorCode can be an enum value (BuiltinOperator) if the operator is a +// builtin, or a string if the operator is custom. +table OperatorCode { + builtin_code:BuiltinOperator; + custom_code:string; +} + +// An operator takes tensors as inputs and outputs. The type of operation being +// performed is determined by an index into the list of valid OperatorCodes, +// while the specifics of each operations is configured using builtin_options +// or custom_options. +table Operator { + // Index into the operator_codes array. Using an integer here avoids + // complicate map lookups. + opcode_index:int; + + inputs:[int]; + outputs:[int]; + + builtin_options:BuiltinOptions; + custom_options:[ubyte]; +} + +// The root type, defining a model. +table SubGraph { + // A list of all tensors used in this model. + tensors:[Tensor]; + + // Indices of the input tensors. + inputs:[int]; + + // Indices of the output tensors. + outputs:[int]; + + // All operators, in execution order. + operators:[Operator]; + + // Name of subgraph (used for debugging). + name:string; +} + +table Model { + // Version of the schema. + version:int; + + // A list of all operator codes used in this model. This is + // kept in order because operators carry an index into this + // vector. + operator_codes:[OperatorCode]; + + // All the subgraphs of the model. The 0th is assumed to be the main + // model. + subgraphs:[SubGraph]; + + // A description of the model. + description:string; +} + +root_type Model; diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_v3.fbs b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_v3.fbs new file mode 100644 index 0000000000000000000000000000000000000000..5b6a3ecc6d6cab3ea5ebd0143c683c034e4d26cf --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_v3.fbs @@ -0,0 +1,326 @@ +// 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. + +// Revision History +// Version 0: Initial version. +// Version 1: Add subgraphs to schema. +// Version 2: Rename operators to conform to NN API. +// Version 3: Move buffer data from Model.Subgraph.Tensors to Model.Buffers. + +namespace tflite; + +// This corresponds to the version (4). +file_identifier "TFL3"; +// File extension of any written files. +file_extension "tflite"; + +// The type of data stored in a tensor. +enum TensorType : byte { + FLOAT32 = 0, + FLOAT16 = 1, + INT32 = 2, + UINT8 = 3, + INT64 = 4, + STRING = 5, +} + +// Parameters for converting a quantized tensor back to float. Given a +// quantized value q, the corresponding float value f should be: +// f = scale * (q - zero_point) +table QuantizationParameters { + min:[float]; // For importing back into tensorflow. + max:[float]; // For importing back into tensorflow. + scale:[float]; + zero_point:[long]; +} + +table Tensor { + // The tensor shape. The meaning of each entry is operator-specific but + // builtin ops use: [batch size, height, width, number of channels] (That's + // Tensorflow's NHWC). + shape:[int]; + type:TensorType; + // An index that refers to the buffers table at the root of the model. Or, + // if there is no data buffer associated (i.e. intermediate results), then + // this is 0 (which refers to an always existent empty buffer). + // + // The data_buffer itself is an opaque container, with the assumption that the + // target device is little-endian. In addition, all builtin operators assume + // the memory is ordered such that if `shape` is [4, 3, 2], then index + // [i, j, k] maps to data_buffer[i*3*2 + j*3 + k]. + buffer:uint; + name:string; // For debugging and importing back into tensorflow. + quantization:QuantizationParameters; // Optional. +} + +// A list of builtin operators. Builtin operators are slightly faster than custom +// ones, but not by much. Moreover, while custom operators accept an opaque +// object containing configuration parameters, builtins have a predetermined +// set of acceptable options. +enum BuiltinOperator : byte { + ADD = 0, + AVERAGE_POOL_2D = 1, + CONCATENATION = 2, + CONV_2D = 3, + DEPTHWISE_CONV_2D = 4, + // DEPTH_TO_SPACE = 5, + // DEQUANTIZE = 6, + EMBEDDING_LOOKUP = 7, + // FLOOR = 8, + FULLY_CONNECTED = 9, + HASHTABLE_LOOKUP = 10, + L2_NORMALIZATION = 11, + L2_POOL_2D = 12, + LOCAL_RESPONSE_NORMALIZATION = 13, + LOGISTIC = 14, + LSH_PROJECTION = 15, + LSTM = 16, + MAX_POOL_2D = 17, + // MUL = 18, + RELU = 19, + // RELU1=20, + RELU6 = 21, + RESHAPE = 22, + RESIZE_BILINEAR = 23, + RNN = 24, + SOFTMAX = 25, + SPACE_TO_DEPTH = 26, + SVDF = 27, + TANH = 28, + // TODO(aselle): Consider rename to CONCATENATE_EMBEDDINGS + CONCAT_EMBEDDINGS = 29, + SKIP_GRAM = 30, + CALL = 31, + CUSTOM = 32, + +} + +// Options for the builtin operators. +union BuiltinOptions { + Conv2DOptions, + DepthwiseConv2DOptions, + ConcatEmbeddingsOptions, + LSHProjectionOptions, + Pool2DOptions, + SVDFOptions, + RNNOptions, + FullyConnectedOptions, + SoftmaxOptions, + ConcatenationOptions, + AddOptions, + L2NormOptions, + LocalResponseNormalizationOptions, + LSTMOptions, + ResizeBilinearOptions, + CallOptions, + ReshapeOptions, + SkipGramOptions, + SpaceToDepthOptions, +} + +enum Padding : byte { SAME, VALID } + +enum ActivationFunctionType : byte { + NONE = 0, + RELU = 1, + RELU1 = 2, + RELU6 = 3, + TANH = 4, + SIGN_BIT = 5, +} + +table Conv2DOptions { + padding:Padding; + stride_w:int; + stride_h:int; + fused_activation_function:ActivationFunctionType; +} + +table Pool2DOptions { + padding:Padding; + stride_w:int; + stride_h:int; + filter_width:int; + filter_height:int; + fused_activation_function:ActivationFunctionType; +} + +table DepthwiseConv2DOptions { + padding:Padding; + stride_w:int; + stride_h:int; + depth_multiplier:int; + fused_activation_function:ActivationFunctionType; +} + +table ConcatEmbeddingsOptions { + num_channels:int; + num_columns_per_channel:[int]; + embedding_dim_per_channel:[int]; // This could be inferred from parameters. +} + +enum LSHProjectionType: byte { + UNKNOWN = 0, + SPARSE = 1, + DENSE = 2, +} + +table LSHProjectionOptions { + type: LSHProjectionType; +} + +table SVDFOptions { + rank:int; + fused_activation_function:ActivationFunctionType; +} + +// An implementation of TensorFlow RNNCell. +table RNNOptions { + fused_activation_function:ActivationFunctionType; +} + +// An implementation of TensorFlow fully_connected (a.k.a Dense) layer. +table FullyConnectedOptions { + fused_activation_function:ActivationFunctionType; +} + +table SoftmaxOptions { + beta: float; +} + +// An implementation of TensorFlow concat. +table ConcatenationOptions { + axis:int; + fused_activation_function:ActivationFunctionType; +} + +table AddOptions { + fused_activation_function:ActivationFunctionType; +} + +table L2NormOptions { + fused_activation_function:ActivationFunctionType; +} + +table LocalResponseNormalizationOptions { + radius:int; + bias:float; + alpha:float; + beta:float; +} + +// An implementation of TensorFlow LSTMCell and CoupledInputForgetGateLSTMCell +table LSTMOptions { + fused_activation_function:ActivationFunctionType; + cell_clip: float; // Optional, 0.0 means no clipping + proj_clip: float; // Optional, 0.0 means no clipping +} + +table ResizeBilinearOptions { + new_height:int; + new_width:int; +} + +// A call operation options +table CallOptions { + // The subgraph index that needs to be called. + subgraph:uint; +} + +table ReshapeOptions { + new_shape:[int]; +} + +table SkipGramOptions { + ngram_size: int; + max_skip_size: int; + include_all_ngrams: bool; +} + +table SpaceToDepthOptions { + block_size: int; +} + +// An OperatorCode can be an enum value (BuiltinOperator) if the operator is a +// builtin, or a string if the operator is custom. +table OperatorCode { + builtin_code:BuiltinOperator; + custom_code:string; +} + +// An operator takes tensors as inputs and outputs. The type of operation being +// performed is determined by an index into the list of valid OperatorCodes, +// while the specifics of each operations is configured using builtin_options +// or custom_options. +table Operator { + // Index into the operator_codes array. Using an integer here avoids + // complicate map lookups. + opcode_index:uint; + + inputs:[int]; + outputs:[int]; + + builtin_options:BuiltinOptions; + custom_options:[ubyte]; +} + +// The root type, defining a model. +table SubGraph { + // A list of all tensors used in this model. + tensors:[Tensor]; + + // Indices of the input tensors. + inputs:[int]; + + // Indices of the output tensors. + outputs:[int]; + + // All operators, in execution order. + operators:[Operator]; + + // Name of subgraph (used for debugging). + name:string; +} + +// Table of raw data buffers (used for constant tensors). Referenced by tensors +// by index. +table Buffer { + data:[ubyte]; +} + +table Model { + // Version of the schema. + version:uint; + + // A list of all operator codes used in this model. This is + // kept in order because operators carry an index into this + // vector. + operator_codes:[OperatorCode]; + + // All the subgraphs of the model. The 0th is assumed to be the main + // model. + subgraphs:[SubGraph]; + + // A description of the model. + description:string; + + // Buffers of the model. + // NOTE: It is required that the first entry in here is always an empty + // buffer. This is so that the default buffer index of zero in Tensor + // will always refer to a valid empty buffer. + buffers:[Buffer]; + +} + +root_type Model; diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_v3a.fbs b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_v3a.fbs new file mode 100644 index 0000000000000000000000000000000000000000..f15a67fbd0ece83daf2712cf2cb03598171b413e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_v3a.fbs @@ -0,0 +1,1109 @@ +// 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. + +// Revision History +// Version 0: Initial version. +// Version 1: Add subgraphs to schema. +// Version 2: Rename operators to conform to NN API. +// Version 3: Move buffer data from Model.Subgraph.Tensors to Model.Buffers. +// Version 3a: Add new builtin op code field. Has backward compatibility with +// version 3. + +namespace tflite; + +// This corresponds to the version. +file_identifier "TFL3"; +// File extension of any written files. +file_extension "tflite"; + +// IMPORTANT: All new members of tables, enums and unions must be added at the +// end to ensure backwards compatibility. + +// The type of data stored in a tensor. +enum TensorType : byte { + FLOAT32 = 0, + FLOAT16 = 1, + INT32 = 2, + UINT8 = 3, + INT64 = 4, + STRING = 5, + BOOL = 6, + INT16 = 7, + COMPLEX64 = 8, + INT8 = 9, + FLOAT64 = 10, + COMPLEX128 = 11, +} + +// Custom quantization parameters for experimenting with new quantization +// techniques. +table CustomQuantization { + custom:[ubyte] (force_align: 16); +} + +// Represents a specific quantization technique's parameters. +union QuantizationDetails { + CustomQuantization, +} + +// Parameters for converting a quantized tensor back to float. +table QuantizationParameters { + // These four parameters are the asymmetric linear quantization parameters. + // Given a quantized value q, the corresponding float value f should be: + // f = scale * (q - zero_point) + // For other quantization types, the QuantizationDetails below is used. + min:[float]; // For importing back into tensorflow. + max:[float]; // For importing back into tensorflow. + scale:[float]; // For dequantizing the tensor's values. + zero_point:[long]; + + // If this is not none, the other quantization parameters (i.e. min, max, + // scale, zero_point fields above) are ignored and the value of the + // QuantizationDetails union should be used. + details:QuantizationDetails; + + // Specifies the dimension of the Tensor's shape that the scales and + // zero_points correspond to. For example, a tensor t, with dims=[4, 3, 2, 1] + // with quantization params: + // scale=[1.0, 2.0, 3.0], zero_point=[1, 2, 3], quantization_dimension=1 + // will be quantized across the second dimension of t. + // t[:, 0, :, :] will have scale[0]=1.0, zero_point[0]=1 + // t[:, 1, :, :] will have scale[1]=2.0, zero_point[0]=2 + // t[:, 2, :, :] will have scale[2]=3.0, zero_point[0]=3 + quantized_dimension:int; +} + +// Sparse tensors. +// We use a modification of the TACO format. +// Reference: http://tensor-compiler.org/kjolstad-oopsla17-tensor-compiler.pdf +// +// To encode a conceptual n-dimensional dense tensor with dims (d0, ..., dn-1), +// potentially with a k-dimensional block (0 <= k <= n) with dims +// (dn, ..., dn+k-1), the format needs to specify: +// 1. In what order to traverse these dimensions. For example, to store a 2-D +// matrix in row major order, the traversal order would be (d0, d1), +// whereas to store it in column major order, the traversal order would be +// (d1, d0). If the 2-D matrix has a 2-D inner block, the traversal order +// could be (d0, d1, d2, d3). +// 2. How each block dimension in (dn, ..., dn+k-1) maps to the original +// tensor dimension in (d0, ..., dn-1). +// 3. In the traversal order defined above, the format (dense vs. sparse) and +// index metadata for each dimension. For a dense dimension, this is just +// the size of that dimension. For a sparse dimension, it's the same as +// the compressed index defined in the Compressed Sparse Row (CSR) format. +// (http://scipy-lectures.org/advanced/scipy_sparse/csr_matrix.html) + +// The storage type for a dimension. Currently we support: +// 1. DENSE: each coordinate in this dimension is stored implicitly. +// 2. SPARSE_CSR: only the coordinates with non-zero elements are stored. The +// compression technique is the same what CSR uses. +// More types like a sparse dimension with a different compression technique +// could be added to the list in the future. +enum DimensionType : byte { + DENSE = 0, + SPARSE_CSR = 1, +} + +table Int32Vector { + values:[int]; +} + +table Uint16Vector { + values:[ushort] (force_align: 4); +} + +table Uint8Vector { + values:[ubyte] (force_align: 4); +} + +// Variable-typed buffer to store the index metadata for a sparse dimension. +// The widest type is Int32 instead of UInt32 because tensor's shape is a int32 +// vector. We don't want the per-dimensional index to overflow that range. +union SparseIndexVector { + Int32Vector, + Uint16Vector, + Uint8Vector +} + +table DimensionMetadata { + // Whether a dimension is dense or sparse. + format:DimensionType; + // Index metadata used for a dimension. + // - If format is DimensionType.DENSE then we use the dense_size field to + // store the size of that dimension. Each index in that dimension is + // stored implicitly. + // - If format is DimensionType.SPARSE_CSR then we use array_segments and + // array_indices to encode that dimension. array_segments represents how + // to segment the indices array, each segment corresponds to one element + // in the previous dimension. array_indices represents the index of the + // non-zero elements within this dimension (as those in the CSR matrix + // format, where the first array is row pointers and the second array is + // column indices). + dense_size:int; + array_segments:SparseIndexVector; + array_indices:SparseIndexVector; +} + +// Parameters to encode a sparse TfLite tensor. +table SparsityParameters { + // The traversal order of the dimensions defined in the `shape` field of the + // conceptual dense tensor. For a n-dimensional tensors with dims (d0, d1, + // ..., dn-1), + // - if not block sparse, the traversal_order is just a permutation of (d0, + // ..., dn-1). For example, a 2-D matrix stored in row-major order would + // have traversal_order = (d0, d1). + // - if block sparse with a k-dimensional block (0 <= k <= n), the + // traversal_order has n + k elements. The first n elements are still a + // permutation of (d0, ..., dn-1). The lask k elements are a permutation + // of (dn, ..., dn+k-1), defining how to traverse a block internally. For + // example, a 2-D matrix with 2-D blocks, both stored in row-major order + // would have traversal_order = (d0, d1, d2, d3). + traversal_order:[int]; + // For an n-dimensional tensor with a k-dimensional block (0 <= k <= n), + // stores how a block dimension in (dn, ..., dn+k-1) maps to the original + // tensor dimension in (d0, ..., dn). + // It's stored in the order of (dn, ..., dn+k-1). + // If not block-sparse, this field is NULL. + block_map:[int]; + // In the traversal order defined above, the metadata needed for + // each dimension to locate the non-zero values in the original dense tensor. + // The size of the dim_metadata array = the size of the traversal_order array + // = n + k. + dim_metadata:[DimensionMetadata]; +} + +table Tensor { + // The tensor shape. The meaning of each entry is operator-specific but + // builtin ops use: [batch size, height, width, number of channels] (That's + // Tensorflow's NHWC). + shape:[int]; + type:TensorType; + // An index that refers to the buffers table at the root of the model. Or, + // if there is no data buffer associated (i.e. intermediate results), then + // this is 0 (which refers to an always existent empty buffer). + // + // The data_buffer itself is an opaque container, with the assumption that the + // target device is little-endian. In addition, all builtin operators assume + // the memory is ordered such that if `shape` is [4, 3, 2], then index + // [i, j, k] maps to data_buffer[i*3*2 + j*2 + k]. + buffer:uint; + name:string; // For debugging and importing back into tensorflow. + quantization:QuantizationParameters; // Optional. + + is_variable:bool = false; + + // Parameters to encode a sparse tensor. See the example in + // tensorflow/lite/testdata/sparse_tensor.json. + sparsity:SparsityParameters; // Optional. + + // Encodes `shape` with unknown dimensions. Unknown dimensions are + // represented with -1. + shape_signature:[int]; // Optional. +} + +// A list of builtin operators. Builtin operators are slightly faster than custom +// ones, but not by much. Moreover, while custom operators accept an opaque +// object containing configuration parameters, builtins have a predetermined +// set of acceptable options. +// LINT.IfChange +enum BuiltinOperator : int32 { + ADD = 0, + AVERAGE_POOL_2D = 1, + CONCATENATION = 2, + CONV_2D = 3, + DEPTHWISE_CONV_2D = 4, + DEPTH_TO_SPACE = 5, + DEQUANTIZE = 6, + EMBEDDING_LOOKUP = 7, + FLOOR = 8, + FULLY_CONNECTED = 9, + HASHTABLE_LOOKUP = 10, + L2_NORMALIZATION = 11, + L2_POOL_2D = 12, + LOCAL_RESPONSE_NORMALIZATION = 13, + LOGISTIC = 14, + LSH_PROJECTION = 15, + LSTM = 16, + MAX_POOL_2D = 17, + MUL = 18, + RELU = 19, + // NOTE(aselle): RELU_N1_TO_1 used to be called RELU1, but it was renamed + // since different model developers use RELU1 in different ways. Never + // create another op called RELU1. + RELU_N1_TO_1 = 20, + RELU6 = 21, + RESHAPE = 22, + RESIZE_BILINEAR = 23, + RNN = 24, + SOFTMAX = 25, + SPACE_TO_DEPTH = 26, + SVDF = 27, + TANH = 28, + CONCAT_EMBEDDINGS = 29, + SKIP_GRAM = 30, + CALL = 31, + CUSTOM = 32, + EMBEDDING_LOOKUP_SPARSE = 33, + PAD = 34, + UNIDIRECTIONAL_SEQUENCE_RNN = 35, + GATHER = 36, + BATCH_TO_SPACE_ND = 37, + SPACE_TO_BATCH_ND = 38, + TRANSPOSE = 39, + MEAN = 40, + SUB = 41, + DIV = 42, + SQUEEZE = 43, + UNIDIRECTIONAL_SEQUENCE_LSTM = 44, + STRIDED_SLICE = 45, + BIDIRECTIONAL_SEQUENCE_RNN = 46, + EXP = 47, + TOPK_V2 = 48, + SPLIT = 49, + LOG_SOFTMAX = 50, + // DELEGATE is a special op type for the operations which are delegated to + // other backends. + // WARNING: Experimental interface, subject to change + DELEGATE = 51, + BIDIRECTIONAL_SEQUENCE_LSTM = 52, + CAST = 53, + PRELU = 54, + MAXIMUM = 55, + ARG_MAX = 56, + MINIMUM = 57, + LESS = 58, + NEG = 59, + PADV2 = 60, + GREATER = 61, + GREATER_EQUAL = 62, + LESS_EQUAL = 63, + SELECT = 64, + SLICE = 65, + SIN = 66, + TRANSPOSE_CONV = 67, + SPARSE_TO_DENSE = 68, + TILE = 69, + EXPAND_DIMS = 70, + EQUAL = 71, + NOT_EQUAL = 72, + LOG = 73, + SUM = 74, + SQRT = 75, + RSQRT = 76, + SHAPE = 77, + POW = 78, + ARG_MIN = 79, + FAKE_QUANT = 80, + REDUCE_PROD = 81, + REDUCE_MAX = 82, + PACK = 83, + LOGICAL_OR = 84, + ONE_HOT = 85, + LOGICAL_AND = 86, + LOGICAL_NOT = 87, + UNPACK = 88, + REDUCE_MIN = 89, + FLOOR_DIV = 90, + REDUCE_ANY = 91, + SQUARE = 92, + ZEROS_LIKE = 93, + FILL = 94, + FLOOR_MOD = 95, + RANGE = 96, + RESIZE_NEAREST_NEIGHBOR = 97, + LEAKY_RELU = 98, + SQUARED_DIFFERENCE = 99, + MIRROR_PAD = 100, + ABS = 101, + SPLIT_V = 102, + UNIQUE = 103, + CEIL = 104, + REVERSE_V2 = 105, + ADD_N = 106, + GATHER_ND = 107, + COS = 108, + WHERE = 109, + RANK = 110, + ELU = 111, + REVERSE_SEQUENCE = 112, + MATRIX_DIAG = 113, + QUANTIZE = 114, + MATRIX_SET_DIAG = 115, + ROUND = 116, + HARD_SWISH = 117, + IF = 118, + WHILE = 119, + NON_MAX_SUPPRESSION_V4 = 120, + NON_MAX_SUPPRESSION_V5 = 121, + SCATTER_ND = 122, + SELECT_V2 = 123, + DENSIFY = 124, + SEGMENT_SUM = 125, + BATCH_MATMUL = 126, + PLACEHOLDER_FOR_GREATER_OP_CODES = 127 +} +// LINT.ThenChange(nnapi_linter/linter.proto) + +// Options for the builtin operators. +union BuiltinOptions { + Conv2DOptions, + DepthwiseConv2DOptions, + ConcatEmbeddingsOptions, + LSHProjectionOptions, + Pool2DOptions, + SVDFOptions, + RNNOptions, + FullyConnectedOptions, + SoftmaxOptions, + ConcatenationOptions, + AddOptions, + L2NormOptions, + LocalResponseNormalizationOptions, + LSTMOptions, + ResizeBilinearOptions, + CallOptions, + ReshapeOptions, + SkipGramOptions, + SpaceToDepthOptions, + EmbeddingLookupSparseOptions, + MulOptions, + PadOptions, + GatherOptions, + BatchToSpaceNDOptions, + SpaceToBatchNDOptions, + TransposeOptions, + ReducerOptions, + SubOptions, + DivOptions, + SqueezeOptions, + SequenceRNNOptions, + StridedSliceOptions, + ExpOptions, + TopKV2Options, + SplitOptions, + LogSoftmaxOptions, + CastOptions, + DequantizeOptions, + MaximumMinimumOptions, + ArgMaxOptions, + LessOptions, + NegOptions, + PadV2Options, + GreaterOptions, + GreaterEqualOptions, + LessEqualOptions, + SelectOptions, + SliceOptions, + TransposeConvOptions, + SparseToDenseOptions, + TileOptions, + ExpandDimsOptions, + EqualOptions, + NotEqualOptions, + ShapeOptions, + PowOptions, + ArgMinOptions, + FakeQuantOptions, + PackOptions, + LogicalOrOptions, + OneHotOptions, + LogicalAndOptions, + LogicalNotOptions, + UnpackOptions, + FloorDivOptions, + SquareOptions, + ZerosLikeOptions, + FillOptions, + BidirectionalSequenceLSTMOptions, + BidirectionalSequenceRNNOptions, + UnidirectionalSequenceLSTMOptions, + FloorModOptions, + RangeOptions, + ResizeNearestNeighborOptions, + LeakyReluOptions, + SquaredDifferenceOptions, + MirrorPadOptions, + AbsOptions, + SplitVOptions, + UniqueOptions, + ReverseV2Options, + AddNOptions, + GatherNdOptions, + CosOptions, + WhereOptions, + RankOptions, + ReverseSequenceOptions, + MatrixDiagOptions, + QuantizeOptions, + MatrixSetDiagOptions, + HardSwishOptions, + IfOptions, + WhileOptions, + DepthToSpaceOptions, + NonMaxSuppressionV4Options, + NonMaxSuppressionV5Options, + ScatterNdOptions, + SelectV2Options, + DensifyOptions, + SegmentSumOptions, + BatchMatMulOptions +} + +enum Padding : byte { SAME, VALID } + +enum ActivationFunctionType : byte { + NONE = 0, + RELU = 1, + RELU_N1_TO_1 = 2, + RELU6 = 3, + TANH = 4, + SIGN_BIT = 5, +} + +table Conv2DOptions { + padding:Padding; + stride_w:int; + stride_h:int; + fused_activation_function:ActivationFunctionType; + dilation_w_factor:int = 1; + dilation_h_factor:int = 1; +} + +table Pool2DOptions { + padding:Padding; + stride_w:int; + stride_h:int; + filter_width:int; + filter_height:int; + fused_activation_function:ActivationFunctionType; +} + +table DepthwiseConv2DOptions { + // Parameters for DepthwiseConv version 1 or above. + padding:Padding; + stride_w:int; + stride_h:int; + // `depth_multiplier` is redundant. It's used by CPU kernels in + // TensorFlow 2.0 or below, but ignored in versions above. + // See comments in lite/c/builtin_op_data.h for more details. + depth_multiplier:int; + fused_activation_function:ActivationFunctionType; + // Parameters for DepthwiseConv version 2 or above. + dilation_w_factor:int = 1; + dilation_h_factor:int = 1; +} + +table ConcatEmbeddingsOptions { + num_channels:int; + num_columns_per_channel:[int]; + embedding_dim_per_channel:[int]; // This could be inferred from parameters. +} + +enum LSHProjectionType: byte { + UNKNOWN = 0, + SPARSE = 1, + DENSE = 2, +} + +table LSHProjectionOptions { + type: LSHProjectionType; +} + +table SVDFOptions { + rank:int; + fused_activation_function:ActivationFunctionType; + // For weights-only quantization, use asymmetric quantization for non + // constant inputs at evaluation time. + asymmetric_quantize_inputs:bool; +} + +// An implementation of TensorFlow RNNCell. +table RNNOptions { + fused_activation_function:ActivationFunctionType; + asymmetric_quantize_inputs:bool; +} + +// An implementation of TensorFlow dynamic_rnn with RNNCell. +table SequenceRNNOptions { + time_major:bool; + fused_activation_function:ActivationFunctionType; + asymmetric_quantize_inputs:bool; +} + +// An implementation of TensorFlow bidrectional_dynamic_rnn with RNNCell. +table BidirectionalSequenceRNNOptions { + time_major:bool; + fused_activation_function:ActivationFunctionType; + merge_outputs: bool; + asymmetric_quantize_inputs:bool; +} + +enum FullyConnectedOptionsWeightsFormat: byte { + DEFAULT = 0, + SHUFFLED4x16INT8 = 1, +} + +// An implementation of TensorFlow fully_connected (a.k.a Dense) layer. +table FullyConnectedOptions { + // Parameters for FullyConnected version 1 or above. + fused_activation_function:ActivationFunctionType; + + // Parameters for FullyConnected version 2 or above. + weights_format:FullyConnectedOptionsWeightsFormat = DEFAULT; + + // Parameters for FullyConnected version 5 or above. + // If set to true, then the number of dimension is preserved. Furthermore, + // all but the last dimension of the input and output shapes will be equal. + keep_num_dims: bool; + + // Parameters for FullyConnected version 7 or above. + // If set to true, then weights-only op will use asymmetric quantization for + // inputs. + asymmetric_quantize_inputs: bool; +} + +table SoftmaxOptions { + beta: float; +} + +// An implementation of TensorFlow concat. +table ConcatenationOptions { + axis:int; + fused_activation_function:ActivationFunctionType; +} + +table AddOptions { + fused_activation_function:ActivationFunctionType; + // Parameters supported by version 4. + pot_scale_int16:bool = true; +} + +table MulOptions { + fused_activation_function:ActivationFunctionType; +} + +table L2NormOptions { + fused_activation_function:ActivationFunctionType; +} + +table LocalResponseNormalizationOptions { + radius:int; + bias:float; + alpha:float; + beta:float; +} + +enum LSTMKernelType : byte { + // Full LSTM kernel which supports peephole and projection. + FULL = 0, + // Basic LSTM kernels. Equivalent to TensorFlow BasicLSTMCell. + BASIC = 1, +} + +// An implementation of TensorFlow LSTMCell and CoupledInputForgetGateLSTMCell +table LSTMOptions { + // Parameters for LSTM version 1 or above. + fused_activation_function:ActivationFunctionType; + cell_clip: float; // Optional, 0.0 means no clipping + proj_clip: float; // Optional, 0.0 means no clipping + + // Parameters for LSTM version 2 or above. + // Basic kernel is only supported in version 2 or above. + kernel_type: LSTMKernelType = FULL; + + // Parameters for LSTM version 4 or above. + asymmetric_quantize_inputs: bool; +} + +// An implementation of TensorFlow dynamic_rnn with LSTMCell. +table UnidirectionalSequenceLSTMOptions { + fused_activation_function:ActivationFunctionType; + cell_clip: float; // Optional, 0.0 means no clipping + proj_clip: float; // Optional, 0.0 means no clipping + + // If true then first dimension is sequence, otherwise batch. + time_major:bool; + + // Parameter for Unidirectional Sequence LSTM version 4. + asymmetric_quantize_inputs:bool; +} + +table BidirectionalSequenceLSTMOptions { + // Parameters supported by version 1: + fused_activation_function:ActivationFunctionType; + cell_clip: float; // Optional, 0.0 means no clipping + proj_clip: float; // Optional, 0.0 means no clipping + + // If true, store the outputs of both directions into the first output. + merge_outputs: bool; + + // Parameters supported by version 2: + // If true then first dimension is sequence, otherwise batch. + // Version 1 implementations assumed time_major to be true, so this default + // value should never change. + time_major: bool = true; + + // Parameters for version 3 or above. + asymmetric_quantize_inputs:bool; +} + +table ResizeBilinearOptions { + new_height: int (deprecated); + new_width: int (deprecated); + align_corners: bool; + half_pixel_centers: bool; +} + +table ResizeNearestNeighborOptions { + align_corners: bool; + half_pixel_centers: bool; +} + +// A call operation options +table CallOptions { + // The subgraph index that needs to be called. + subgraph:uint; +} + +table PadOptions { +} + +table PadV2Options { +} + +table ReshapeOptions { + new_shape:[int]; +} + +table SpaceToBatchNDOptions { +} + +table BatchToSpaceNDOptions { +} + +table SkipGramOptions { + ngram_size: int; + max_skip_size: int; + include_all_ngrams: bool; +} + +table SpaceToDepthOptions { + block_size: int; +} + +table DepthToSpaceOptions { + block_size: int; +} + +table SubOptions { + fused_activation_function:ActivationFunctionType; + // Parameters supported by version 5 + pot_scale_int16:bool = true; +} + +table DivOptions { + fused_activation_function:ActivationFunctionType; +} + +table TopKV2Options { +} + +enum CombinerType : byte { + SUM = 0, + MEAN = 1, + SQRTN = 2, +} + +table EmbeddingLookupSparseOptions { + combiner:CombinerType; +} + +table GatherOptions { + axis: int; +} + +table TransposeOptions { +} + +table ExpOptions { +} + +table CosOptions { +} + +table ReducerOptions { + keep_dims: bool; +} + +table SqueezeOptions { + squeeze_dims:[int]; +} + +table SplitOptions { + num_splits: int; +} + +table SplitVOptions { + num_splits: int; +} + +table StridedSliceOptions { + begin_mask: int; + end_mask: int; + ellipsis_mask: int; + new_axis_mask: int; + shrink_axis_mask: int; +} + +table LogSoftmaxOptions { +} + +table CastOptions { + in_data_type: TensorType; + out_data_type: TensorType; +} + +table DequantizeOptions { +} + +table MaximumMinimumOptions { +} + +table TileOptions { +} + +table ArgMaxOptions { + output_type : TensorType; +} + +table ArgMinOptions { + output_type : TensorType; +} + +table GreaterOptions { +} + +table GreaterEqualOptions { +} + +table LessOptions { +} + +table LessEqualOptions { +} + +table NegOptions { +} + +table SelectOptions { +} + +table SliceOptions { +} + +table TransposeConvOptions { + padding:Padding; + stride_w:int; + stride_h:int; +} + +table ExpandDimsOptions { +} + +table SparseToDenseOptions { + validate_indices:bool; +} + +table EqualOptions { +} + +table NotEqualOptions { +} + +table ShapeOptions { + // Optional output type of the operation (int32 or int64). Defaults to int32. + out_type : TensorType; +} + +table RankOptions { +} + +table PowOptions { +} + +table FakeQuantOptions { + // Parameters supported by version 1: + min:float; + max:float; + num_bits:int; + + // Parameters supported by version 2: + narrow_range:bool; +} + +table PackOptions { + values_count:int; + axis:int; +} + +table LogicalOrOptions { +} + +table OneHotOptions { + axis:int; +} + +table AbsOptions { +} + + +table HardSwishOptions { +} + +table LogicalAndOptions { +} + +table LogicalNotOptions { +} + +table UnpackOptions { + num:int; + axis:int; +} + +table FloorDivOptions { +} + +table SquareOptions { +} + +table ZerosLikeOptions { +} + +table FillOptions { +} + +table FloorModOptions { +} + +table RangeOptions { +} + +table LeakyReluOptions { + alpha:float; +} + +table SquaredDifferenceOptions { +} + +enum MirrorPadMode : byte { + // Doesn't include borders. + REFLECT = 0, + // Includes borders. + SYMMETRIC = 1, +} + +table MirrorPadOptions { + mode:MirrorPadMode; +} + +table UniqueOptions { + idx_out_type:TensorType = INT32; +} + +table ReverseV2Options { +} + +table AddNOptions { +} + +table GatherNdOptions { +} + +table WhereOptions { +} + +table ReverseSequenceOptions { + seq_dim:int; + batch_dim:int = 0; +} + +table MatrixDiagOptions { +} + +table QuantizeOptions { +} + +table MatrixSetDiagOptions { +} + +table IfOptions { + then_subgraph_index:int; + else_subgraph_index:int; +} + +table WhileOptions { + cond_subgraph_index:int; + body_subgraph_index:int; +} + +table NonMaxSuppressionV4Options { +} + +table NonMaxSuppressionV5Options { +} + +table ScatterNdOptions { +} + +table SelectV2Options { +} + +table DensifyOptions { +} + +table SegmentSumOptions { +} + +table BatchMatMulOptions { + adj_x:bool; + adj_y:bool; +} + +// An OperatorCode can be an enum value (BuiltinOperator) if the operator is a +// builtin, or a string if the operator is custom. +table OperatorCode { + // This field is for backward compatibility. This field will be used when + // the value of the extended builtin_code field has less than + // BulitinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES. + deprecated_builtin_code:byte; + custom_code:string; + + // The version of the operator. The version need to be bumped whenever new + // parameters are introduced into an op. + version:int = 1; + + // This field is introduced for resolving op builtin code shortage problem. + // This field will be used when the value of the extended builtin_code field + // is greater than BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES. + builtin_code:BuiltinOperator; +} + +enum CustomOptionsFormat : byte { + FLEXBUFFERS = 0, +} + +// An operator takes tensors as inputs and outputs. The type of operation being +// performed is determined by an index into the list of valid OperatorCodes, +// while the specifics of each operations is configured using builtin_options +// or custom_options. +table Operator { + // Index into the operator_codes array. Using an integer here avoids + // complicate map lookups. + opcode_index:uint; + + // Optional input are indicated by -1. + inputs:[int]; + outputs:[int]; + + builtin_options:BuiltinOptions; + custom_options:[ubyte]; + custom_options_format:CustomOptionsFormat; + + // A list of booleans indicating the input tensors which are being mutated by + // this operator.(e.g. used by RNN and LSTM). + // For example, if the "inputs" array refers to 5 tensors and the second and + // fifth are mutable variables, then this list will contain + // [false, true, false, false, true]. + // + // If the list is empty, no variable is mutated in this operator. + // The list either has the same length as `inputs`, or is empty. + mutating_variable_inputs:[bool]; + + // A list of indices to the subgraph's "tensors" that are internal to an Op. + // Internal tensors are those that do not flow in or out of the operation, + // but instead are part of internal computation. As such, the operation's + // implementation may manage its memory more efficiently. They are needed + // however (i.e. not just an implementation detail) since they are part of the + // computation, which may require relevant metadata such as quantization + // parameters. + intermediates:[int]; +} + +// The root type, defining a subgraph, which typically represents an entire +// model. +table SubGraph { + // A list of all tensors used in this subgraph. + tensors:[Tensor]; + + // Indices of the tensors that are inputs into this subgraph. Note this is + // the list of non-static tensors that feed into the subgraph for inference. + inputs:[int]; + + // Indices of the tensors that are outputs out of this subgraph. Note this is + // the list of output tensors that are considered the product of the + // subgraph's inference. + outputs:[int]; + + // All operators, in execution order. + operators:[Operator]; + + // Name of this subgraph (used for debugging). + name:string; +} + +// Table of raw data buffers (used for constant tensors). Referenced by tensors +// by index. The generous alignment accommodates mmap-friendly data structures. +table Buffer { + data:[ubyte] (force_align: 16); +} + +table Metadata { + // A human readable string to uniquely identify a Metadata. + name:string; + // An index to the buffers table. + buffer:uint; +} + +table Model { + // Version of the schema. + version:uint; + + // A list of all operator codes used in this model. This is + // kept in order because operators carry an index into this + // vector. + operator_codes:[OperatorCode]; + + // All the subgraphs of the model. The 0th is assumed to be the main + // model. + subgraphs:[SubGraph]; + + // A description of the model. + description:string; + + // Buffers of the model. + // Note the 0th entry of this array must be an empty buffer (sentinel). + // This is a convention so that tensors without a buffer can provide 0 as + // their buffer. + buffers:[Buffer]; + + // Metadata about the model. Indirects into the existings buffers list. + // Deprecated, prefer to use metadata field. + metadata_buffer:[int]; + + // Metadata about the model. + metadata:[Metadata]; +} + +root_type Model; diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_v3b.fbs b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_v3b.fbs new file mode 100644 index 0000000000000000000000000000000000000000..4a9df5644573f1729b728dc0b185f1841d4c1bd2 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_v3b.fbs @@ -0,0 +1,1242 @@ +// 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. + +// Revision History +// Version 0: Initial version. +// Version 1: Add subgraphs to schema. +// Version 2: Rename operators to conform to NN API. +// Version 3: Move buffer data from Model.Subgraph.Tensors to Model.Buffers. +// Version 3a: Add new builtin op code field. Has backward compatibility with +// version 3. +// Version 3b: Rename fields in SignatureDef. Has backward compatibility with +// version 3 and 3a. + +namespace tflite; + +// This corresponds to the version. +file_identifier "TFL3"; +// File extension of any written files. +file_extension "tflite"; + +// IMPORTANT: All new members of tables, enums and unions must be added at the +// end to ensure backwards compatibility. + +// The type of data stored in a tensor. +enum TensorType : byte { + FLOAT32 = 0, + FLOAT16 = 1, + INT32 = 2, + UINT8 = 3, + INT64 = 4, + STRING = 5, + BOOL = 6, + INT16 = 7, + COMPLEX64 = 8, + INT8 = 9, + FLOAT64 = 10, + COMPLEX128 = 11, + UINT64 = 12, + // Experimental: Resource and variant types are experimental, that are subject + // to change. Do not implement custom kernels using resource & variant types + // now. + RESOURCE = 13, + VARIANT = 14, + UINT32 = 15, +} + +// Custom quantization parameters for experimenting with new quantization +// techniques. +table CustomQuantization { + custom:[ubyte] (force_align: 16); +} + +// Represents a specific quantization technique's parameters. +union QuantizationDetails { + CustomQuantization, +} + +// Parameters for converting a quantized tensor back to float. +table QuantizationParameters { + // These four parameters are the asymmetric linear quantization parameters. + // Given a quantized value q, the corresponding float value f should be: + // f = scale * (q - zero_point) + // For other quantization types, the QuantizationDetails below is used. + min:[float]; // For importing back into tensorflow. + max:[float]; // For importing back into tensorflow. + scale:[float]; // For dequantizing the tensor's values. + zero_point:[long]; + + // If this is not none, the other quantization parameters (i.e. min, max, + // scale, zero_point fields above) are ignored and the value of the + // QuantizationDetails union should be used. + details:QuantizationDetails; + + // Specifies the dimension of the Tensor's shape that the scales and + // zero_points correspond to. For example, a tensor t, with dims=[4, 3, 2, 1] + // with quantization params: + // scale=[1.0, 2.0, 3.0], zero_point=[1, 2, 3], quantization_dimension=1 + // will be quantized across the second dimension of t. + // t[:, 0, :, :] will have scale[0]=1.0, zero_point[0]=1 + // t[:, 1, :, :] will have scale[1]=2.0, zero_point[0]=2 + // t[:, 2, :, :] will have scale[2]=3.0, zero_point[0]=3 + quantized_dimension:int; +} + +// Sparse tensors. +// We use a modification of the TACO format. +// Reference: http://tensor-compiler.org/kjolstad-oopsla17-tensor-compiler.pdf +// +// To encode a conceptual n-dimensional dense tensor with dims (d0, ..., dn-1), +// potentially with a k-dimensional block (0 <= k <= n) with dims +// (dn, ..., dn+k-1), the format needs to specify: +// 1. In what order to traverse these dimensions. For example, to store a 2-D +// matrix in row major order, the traversal order would be (d0, d1), +// whereas to store it in column major order, the traversal order would be +// (d1, d0). If the 2-D matrix has a 2-D inner block, the traversal order +// could be (d0, d1, d2, d3). +// 2. How each block dimension in (dn, ..., dn+k-1) maps to the original +// tensor dimension in (d0, ..., dn-1). +// 3. In the traversal order defined above, the format (dense vs. sparse) and +// index metadata for each dimension. For a dense dimension, this is just +// the size of that dimension. For a sparse dimension, it's the same as +// the compressed index defined in the Compressed Sparse Row (CSR) format. +// (http://scipy-lectures.org/advanced/scipy_sparse/csr_matrix.html) + +// The storage type for a dimension. Currently we support: +// 1. DENSE: each coordinate in this dimension is stored implicitly. +// 2. SPARSE_CSR: only the coordinates with non-zero elements are stored. The +// compression technique is the same what CSR uses. +// More types like a sparse dimension with a different compression technique +// could be added to the list in the future. +enum DimensionType : byte { + DENSE = 0, + SPARSE_CSR = 1, +} + +table Int32Vector { + values:[int]; +} + +table Uint16Vector { + values:[ushort] (force_align: 4); +} + +table Uint8Vector { + values:[ubyte] (force_align: 4); +} + +// Variable-typed buffer to store the index metadata for a sparse dimension. +// The widest type is Int32 instead of UInt32 because tensor's shape is a int32 +// vector. We don't want the per-dimensional index to overflow that range. +union SparseIndexVector { + Int32Vector, + Uint16Vector, + Uint8Vector +} + +table DimensionMetadata { + // Whether a dimension is dense or sparse. + format:DimensionType; + // Index metadata used for a dimension. + // - If format is DimensionType.DENSE then we use the dense_size field to + // store the size of that dimension. Each index in that dimension is + // stored implicitly. + // - If format is DimensionType.SPARSE_CSR then we use array_segments and + // array_indices to encode that dimension. array_segments represents how + // to segment the indices array, each segment corresponds to one element + // in the previous dimension. array_indices represents the index of the + // non-zero elements within this dimension (as those in the CSR matrix + // format, where the first array is row pointers and the second array is + // column indices). + dense_size:int; + array_segments:SparseIndexVector; + array_indices:SparseIndexVector; +} + +// Parameters to encode a sparse TfLite tensor. +table SparsityParameters { + // The traversal order of the dimensions defined in the `shape` field of the + // conceptual dense tensor. For a n-dimensional tensors with dims (d0, d1, + // ..., dn-1), + // - if not block sparse, the traversal_order is just a permutation of (d0, + // ..., dn-1). For example, a 2-D matrix stored in row-major order would + // have traversal_order = (d0, d1). + // - if block sparse with a k-dimensional block (0 <= k <= n), the + // traversal_order has n + k elements. The first n elements are still a + // permutation of (d0, ..., dn-1). The lask k elements are a permutation + // of (dn, ..., dn+k-1), defining how to traverse a block internally. For + // example, a 2-D matrix with 2-D blocks, both stored in row-major order + // would have traversal_order = (d0, d1, d2, d3). + traversal_order:[int]; + // For an n-dimensional tensor with a k-dimensional block (0 <= k <= n), + // stores how a block dimension in (dn, ..., dn+k-1) maps to the original + // tensor dimension in (d0, ..., dn). + // It's stored in the order of (dn, ..., dn+k-1). + // If not block-sparse, this field is NULL. + block_map:[int]; + // In the traversal order defined above, the metadata needed for + // each dimension to locate the non-zero values in the original dense tensor. + // The size of the dim_metadata array = the size of the traversal_order array + // = n + k. + dim_metadata:[DimensionMetadata]; +} + +table Tensor { + // The tensor shape. The meaning of each entry is operator-specific but + // builtin ops use: [batch size, height, width, number of channels] (That's + // Tensorflow's NHWC). + shape:[int]; + type:TensorType; + // An index that refers to the buffers table at the root of the model. Or, + // if there is no data buffer associated (i.e. intermediate results), then + // this is 0 (which refers to an always existent empty buffer). + // + // The data_buffer itself is an opaque container, with the assumption that the + // target device is little-endian. In addition, all builtin operators assume + // the memory is ordered such that if `shape` is [4, 3, 2], then index + // [i, j, k] maps to data_buffer[i*3*2 + j*2 + k]. + buffer:uint; + name:string; // For debugging and importing back into tensorflow. + quantization:QuantizationParameters; // Optional. + + is_variable:bool = false; + + // Parameters to encode a sparse tensor. See the example in + // tensorflow/lite/testdata/sparse_tensor.json. + sparsity:SparsityParameters; // Optional. + + // Encodes `shape` with unknown dimensions. Unknown dimensions are + // represented with -1. + shape_signature:[int]; // Optional. +} + +// A list of builtin operators. Builtin operators are slightly faster than custom +// ones, but not by much. Moreover, while custom operators accept an opaque +// object containing configuration parameters, builtins have a predetermined +// set of acceptable options. +// LINT.IfChange +enum BuiltinOperator : int32 { + ADD = 0, + AVERAGE_POOL_2D = 1, + CONCATENATION = 2, + CONV_2D = 3, + DEPTHWISE_CONV_2D = 4, + DEPTH_TO_SPACE = 5, + DEQUANTIZE = 6, + EMBEDDING_LOOKUP = 7, + FLOOR = 8, + FULLY_CONNECTED = 9, + HASHTABLE_LOOKUP = 10, + L2_NORMALIZATION = 11, + L2_POOL_2D = 12, + LOCAL_RESPONSE_NORMALIZATION = 13, + LOGISTIC = 14, + LSH_PROJECTION = 15, + LSTM = 16, + MAX_POOL_2D = 17, + MUL = 18, + RELU = 19, + // NOTE(aselle): RELU_N1_TO_1 used to be called RELU1, but it was renamed + // since different model developers use RELU1 in different ways. Never + // create another op called RELU1. + RELU_N1_TO_1 = 20, + RELU6 = 21, + RESHAPE = 22, + RESIZE_BILINEAR = 23, + RNN = 24, + SOFTMAX = 25, + SPACE_TO_DEPTH = 26, + SVDF = 27, + TANH = 28, + CONCAT_EMBEDDINGS = 29, + SKIP_GRAM = 30, + CALL = 31, + CUSTOM = 32, + EMBEDDING_LOOKUP_SPARSE = 33, + PAD = 34, + UNIDIRECTIONAL_SEQUENCE_RNN = 35, + GATHER = 36, + BATCH_TO_SPACE_ND = 37, + SPACE_TO_BATCH_ND = 38, + TRANSPOSE = 39, + MEAN = 40, + SUB = 41, + DIV = 42, + SQUEEZE = 43, + UNIDIRECTIONAL_SEQUENCE_LSTM = 44, + STRIDED_SLICE = 45, + BIDIRECTIONAL_SEQUENCE_RNN = 46, + EXP = 47, + TOPK_V2 = 48, + SPLIT = 49, + LOG_SOFTMAX = 50, + // DELEGATE is a special op type for the operations which are delegated to + // other backends. + // WARNING: Experimental interface, subject to change + DELEGATE = 51, + BIDIRECTIONAL_SEQUENCE_LSTM = 52, + CAST = 53, + PRELU = 54, + MAXIMUM = 55, + ARG_MAX = 56, + MINIMUM = 57, + LESS = 58, + NEG = 59, + PADV2 = 60, + GREATER = 61, + GREATER_EQUAL = 62, + LESS_EQUAL = 63, + SELECT = 64, + SLICE = 65, + SIN = 66, + TRANSPOSE_CONV = 67, + SPARSE_TO_DENSE = 68, + TILE = 69, + EXPAND_DIMS = 70, + EQUAL = 71, + NOT_EQUAL = 72, + LOG = 73, + SUM = 74, + SQRT = 75, + RSQRT = 76, + SHAPE = 77, + POW = 78, + ARG_MIN = 79, + FAKE_QUANT = 80, + REDUCE_PROD = 81, + REDUCE_MAX = 82, + PACK = 83, + LOGICAL_OR = 84, + ONE_HOT = 85, + LOGICAL_AND = 86, + LOGICAL_NOT = 87, + UNPACK = 88, + REDUCE_MIN = 89, + FLOOR_DIV = 90, + REDUCE_ANY = 91, + SQUARE = 92, + ZEROS_LIKE = 93, + FILL = 94, + FLOOR_MOD = 95, + RANGE = 96, + RESIZE_NEAREST_NEIGHBOR = 97, + LEAKY_RELU = 98, + SQUARED_DIFFERENCE = 99, + MIRROR_PAD = 100, + ABS = 101, + SPLIT_V = 102, + UNIQUE = 103, + CEIL = 104, + REVERSE_V2 = 105, + ADD_N = 106, + GATHER_ND = 107, + COS = 108, + WHERE = 109, + RANK = 110, + ELU = 111, + REVERSE_SEQUENCE = 112, + MATRIX_DIAG = 113, + QUANTIZE = 114, + MATRIX_SET_DIAG = 115, + ROUND = 116, + HARD_SWISH = 117, + IF = 118, + WHILE = 119, + NON_MAX_SUPPRESSION_V4 = 120, + NON_MAX_SUPPRESSION_V5 = 121, + SCATTER_ND = 122, + SELECT_V2 = 123, + DENSIFY = 124, + SEGMENT_SUM = 125, + BATCH_MATMUL = 126, + PLACEHOLDER_FOR_GREATER_OP_CODES = 127, + CUMSUM = 128, + CALL_ONCE = 129, + BROADCAST_TO = 130, + RFFT2D = 131, + CONV_3D = 132, + IMAG=133, + REAL=134, + COMPLEX_ABS=135, + HASHTABLE = 136, + HASHTABLE_FIND = 137, + HASHTABLE_IMPORT = 138, + HASHTABLE_SIZE = 139, + REDUCE_ALL = 140, + CONV_3D_TRANSPOSE = 141, + VAR_HANDLE = 142, + READ_VARIABLE = 143, + ASSIGN_VARIABLE = 144, +} +// LINT.ThenChange(nnapi_linter/linter.proto) + +// Options for the builtin operators. +union BuiltinOptions { + Conv2DOptions, + DepthwiseConv2DOptions, + ConcatEmbeddingsOptions, + LSHProjectionOptions, + Pool2DOptions, + SVDFOptions, + RNNOptions, + FullyConnectedOptions, + SoftmaxOptions, + ConcatenationOptions, + AddOptions, + L2NormOptions, + LocalResponseNormalizationOptions, + LSTMOptions, + ResizeBilinearOptions, + CallOptions, + ReshapeOptions, + SkipGramOptions, + SpaceToDepthOptions, + EmbeddingLookupSparseOptions, + MulOptions, + PadOptions, + GatherOptions, + BatchToSpaceNDOptions, + SpaceToBatchNDOptions, + TransposeOptions, + ReducerOptions, + SubOptions, + DivOptions, + SqueezeOptions, + SequenceRNNOptions, + StridedSliceOptions, + ExpOptions, + TopKV2Options, + SplitOptions, + LogSoftmaxOptions, + CastOptions, + DequantizeOptions, + MaximumMinimumOptions, + ArgMaxOptions, + LessOptions, + NegOptions, + PadV2Options, + GreaterOptions, + GreaterEqualOptions, + LessEqualOptions, + SelectOptions, + SliceOptions, + TransposeConvOptions, + SparseToDenseOptions, + TileOptions, + ExpandDimsOptions, + EqualOptions, + NotEqualOptions, + ShapeOptions, + PowOptions, + ArgMinOptions, + FakeQuantOptions, + PackOptions, + LogicalOrOptions, + OneHotOptions, + LogicalAndOptions, + LogicalNotOptions, + UnpackOptions, + FloorDivOptions, + SquareOptions, + ZerosLikeOptions, + FillOptions, + BidirectionalSequenceLSTMOptions, + BidirectionalSequenceRNNOptions, + UnidirectionalSequenceLSTMOptions, + FloorModOptions, + RangeOptions, + ResizeNearestNeighborOptions, + LeakyReluOptions, + SquaredDifferenceOptions, + MirrorPadOptions, + AbsOptions, + SplitVOptions, + UniqueOptions, + ReverseV2Options, + AddNOptions, + GatherNdOptions, + CosOptions, + WhereOptions, + RankOptions, + ReverseSequenceOptions, + MatrixDiagOptions, + QuantizeOptions, + MatrixSetDiagOptions, + HardSwishOptions, + IfOptions, + WhileOptions, + DepthToSpaceOptions, + NonMaxSuppressionV4Options, + NonMaxSuppressionV5Options, + ScatterNdOptions, + SelectV2Options, + DensifyOptions, + SegmentSumOptions, + BatchMatMulOptions, + CumsumOptions, + CallOnceOptions, + BroadcastToOptions, + Rfft2dOptions, + Conv3DOptions, + HashtableOptions, + HashtableFindOptions, + HashtableImportOptions, + HashtableSizeOptions, + VarHandleOptions, + ReadVariableOptions, + AssignVariableOptions, +} + +enum Padding : byte { SAME, VALID } + +enum ActivationFunctionType : byte { + NONE = 0, + RELU = 1, + RELU_N1_TO_1 = 2, + RELU6 = 3, + TANH = 4, + SIGN_BIT = 5, +} + +table Conv2DOptions { + padding:Padding; + stride_w:int; + stride_h:int; + fused_activation_function:ActivationFunctionType; + dilation_w_factor:int = 1; + dilation_h_factor:int = 1; +} + +// Options for both Conv3D and Conv3DTranspose. +table Conv3DOptions { + padding:Padding; + stride_d:int; + stride_w:int; + stride_h:int; + fused_activation_function:ActivationFunctionType; + dilation_d_factor:int = 1; + dilation_w_factor:int = 1; + dilation_h_factor:int = 1; +} + +table Pool2DOptions { + padding:Padding; + stride_w:int; + stride_h:int; + filter_width:int; + filter_height:int; + fused_activation_function:ActivationFunctionType; +} + +table DepthwiseConv2DOptions { + // Parameters for DepthwiseConv version 1 or above. + padding:Padding; + stride_w:int; + stride_h:int; + // `depth_multiplier` is redundant. It's used by CPU kernels in + // TensorFlow 2.0 or below, but ignored in versions above. + // See comments in lite/c/builtin_op_data.h for more details. + depth_multiplier:int; + fused_activation_function:ActivationFunctionType; + // Parameters for DepthwiseConv version 2 or above. + dilation_w_factor:int = 1; + dilation_h_factor:int = 1; +} + +table ConcatEmbeddingsOptions { + num_channels:int; + num_columns_per_channel:[int]; + embedding_dim_per_channel:[int]; // This could be inferred from parameters. +} + +enum LSHProjectionType: byte { + UNKNOWN = 0, + SPARSE = 1, + DENSE = 2, +} + +table LSHProjectionOptions { + type: LSHProjectionType; +} + +table SVDFOptions { + rank:int; + fused_activation_function:ActivationFunctionType; + // For weights-only quantization, use asymmetric quantization for non + // constant inputs at evaluation time. + asymmetric_quantize_inputs:bool; +} + +// An implementation of TensorFlow RNNCell. +table RNNOptions { + fused_activation_function:ActivationFunctionType; + asymmetric_quantize_inputs:bool; +} + +// An implementation of TensorFlow dynamic_rnn with RNNCell. +table SequenceRNNOptions { + time_major:bool; + fused_activation_function:ActivationFunctionType; + asymmetric_quantize_inputs:bool; +} + +// An implementation of TensorFlow bidrectional_dynamic_rnn with RNNCell. +table BidirectionalSequenceRNNOptions { + time_major:bool; + fused_activation_function:ActivationFunctionType; + merge_outputs: bool; + asymmetric_quantize_inputs:bool; +} + +enum FullyConnectedOptionsWeightsFormat: byte { + DEFAULT = 0, + SHUFFLED4x16INT8 = 1, +} + +// An implementation of TensorFlow fully_connected (a.k.a Dense) layer. +table FullyConnectedOptions { + // Parameters for FullyConnected version 1 or above. + fused_activation_function:ActivationFunctionType; + + // Parameters for FullyConnected version 2 or above. + weights_format:FullyConnectedOptionsWeightsFormat = DEFAULT; + + // Parameters for FullyConnected version 5 or above. + // If set to true, then the number of dimension is preserved. Furthermore, + // all but the last dimension of the input and output shapes will be equal. + keep_num_dims: bool; + + // Parameters for FullyConnected version 7 or above. + // If set to true, then weights-only op will use asymmetric quantization for + // inputs. + asymmetric_quantize_inputs: bool; +} + +table SoftmaxOptions { + beta: float; +} + +// An implementation of TensorFlow concat. +table ConcatenationOptions { + axis:int; + fused_activation_function:ActivationFunctionType; +} + +table AddOptions { + fused_activation_function:ActivationFunctionType; + // Parameters supported by version 3. + pot_scale_int16:bool = true; +} + +table MulOptions { + fused_activation_function:ActivationFunctionType; +} + +table L2NormOptions { + // This field is currently ignored in the L2 Norm Op. + fused_activation_function:ActivationFunctionType; +} + +table LocalResponseNormalizationOptions { + radius:int; + bias:float; + alpha:float; + beta:float; +} + +enum LSTMKernelType : byte { + // Full LSTM kernel which supports peephole and projection. + FULL = 0, + // Basic LSTM kernels. Equivalent to TensorFlow BasicLSTMCell. + BASIC = 1, +} + +// An implementation of TensorFlow LSTMCell and CoupledInputForgetGateLSTMCell +table LSTMOptions { + // Parameters for LSTM version 1 or above. + fused_activation_function:ActivationFunctionType; + cell_clip: float; // Optional, 0.0 means no clipping + proj_clip: float; // Optional, 0.0 means no clipping + + // Parameters for LSTM version 2 or above. + // Basic kernel is only supported in version 2 or above. + kernel_type: LSTMKernelType = FULL; + + // Parameters for LSTM version 4 or above. + asymmetric_quantize_inputs: bool; +} + +// An implementation of TensorFlow dynamic_rnn with LSTMCell. +table UnidirectionalSequenceLSTMOptions { + fused_activation_function:ActivationFunctionType; + cell_clip: float; // Optional, 0.0 means no clipping + proj_clip: float; // Optional, 0.0 means no clipping + + // If true then first dimension is sequence, otherwise batch. + time_major:bool; + + // Parameter for Unidirectional Sequence LSTM version 4. + asymmetric_quantize_inputs:bool; +} + +table BidirectionalSequenceLSTMOptions { + // Parameters supported by version 1: + fused_activation_function:ActivationFunctionType; + cell_clip: float; // Optional, 0.0 means no clipping + proj_clip: float; // Optional, 0.0 means no clipping + + // If true, store the outputs of both directions into the first output. + merge_outputs: bool; + + // Parameters supported by version 2: + // If true then first dimension is sequence, otherwise batch. + // Version 1 implementations assumed time_major to be true, so this default + // value should never change. + time_major: bool = true; + + // Parameters for version 3 or above. + asymmetric_quantize_inputs:bool; +} + +table ResizeBilinearOptions { + new_height: int (deprecated); + new_width: int (deprecated); + align_corners: bool; + half_pixel_centers: bool; +} + +table ResizeNearestNeighborOptions { + align_corners: bool; + half_pixel_centers: bool; +} + +// A call operation options +table CallOptions { + // The subgraph index that needs to be called. + subgraph:uint; +} + +table PadOptions { +} + +table PadV2Options { +} + +table ReshapeOptions { + new_shape:[int]; +} + +table SpaceToBatchNDOptions { +} + +table BatchToSpaceNDOptions { +} + +table SkipGramOptions { + ngram_size: int; + max_skip_size: int; + include_all_ngrams: bool; +} + +table SpaceToDepthOptions { + block_size: int; +} + +table DepthToSpaceOptions { + block_size: int; +} + +table SubOptions { + fused_activation_function:ActivationFunctionType; + // Parameters supported by version 5 + pot_scale_int16:bool = true; +} + +table DivOptions { + fused_activation_function:ActivationFunctionType; +} + +table TopKV2Options { +} + +enum CombinerType : byte { + SUM = 0, + MEAN = 1, + SQRTN = 2, +} + +table EmbeddingLookupSparseOptions { + combiner:CombinerType; +} + +table GatherOptions { + axis: int; + // Parameters for Gather version 5 or above. + batch_dims: int = 0; +} + +table TransposeOptions { +} + +table ExpOptions { +} + +table CosOptions { +} + +table ReducerOptions { + keep_dims: bool; +} + +table SqueezeOptions { + squeeze_dims:[int]; +} + +table SplitOptions { + num_splits: int; +} + +table SplitVOptions { + num_splits: int; +} + +table StridedSliceOptions { + begin_mask: int; + end_mask: int; + ellipsis_mask: int; + new_axis_mask: int; + shrink_axis_mask: int; +} + +table LogSoftmaxOptions { +} + +table CastOptions { + in_data_type: TensorType; + out_data_type: TensorType; +} + +table DequantizeOptions { +} + +table MaximumMinimumOptions { +} + +table TileOptions { +} + +table ArgMaxOptions { + output_type : TensorType; +} + +table ArgMinOptions { + output_type : TensorType; +} + +table GreaterOptions { +} + +table GreaterEqualOptions { +} + +table LessOptions { +} + +table LessEqualOptions { +} + +table NegOptions { +} + +table SelectOptions { +} + +table SliceOptions { +} + +table TransposeConvOptions { + padding:Padding; + stride_w:int; + stride_h:int; +} + +table ExpandDimsOptions { +} + +table SparseToDenseOptions { + validate_indices:bool; +} + +table EqualOptions { +} + +table NotEqualOptions { +} + +table ShapeOptions { + // Optional output type of the operation (int32 or int64). Defaults to int32. + out_type : TensorType; +} + +table RankOptions { +} + +table PowOptions { +} + +table FakeQuantOptions { + // Parameters supported by version 1: + min:float; + max:float; + num_bits:int; + + // Parameters supported by version 2: + narrow_range:bool; +} + +table PackOptions { + values_count:int; + axis:int; +} + +table LogicalOrOptions { +} + +table OneHotOptions { + axis:int; +} + +table AbsOptions { +} + + +table HardSwishOptions { +} + +table LogicalAndOptions { +} + +table LogicalNotOptions { +} + +table UnpackOptions { + num:int; + axis:int; +} + +table FloorDivOptions { +} + +table SquareOptions { +} + +table ZerosLikeOptions { +} + +table FillOptions { +} + +table FloorModOptions { +} + +table RangeOptions { +} + +table LeakyReluOptions { + alpha:float; +} + +table SquaredDifferenceOptions { +} + +enum MirrorPadMode : byte { + // Doesn't include borders. + REFLECT = 0, + // Includes borders. + SYMMETRIC = 1, +} + +table MirrorPadOptions { + mode:MirrorPadMode; +} + +table UniqueOptions { + idx_out_type:TensorType = INT32; +} + +table ReverseV2Options { +} + +table AddNOptions { +} + +table GatherNdOptions { +} + +table WhereOptions { +} + +table ReverseSequenceOptions { + seq_dim:int; + batch_dim:int = 0; +} + +table MatrixDiagOptions { +} + +table QuantizeOptions { +} + +table MatrixSetDiagOptions { +} + +table IfOptions { + then_subgraph_index:int; + else_subgraph_index:int; +} + +table CallOnceOptions { + init_subgraph_index:int; +} + +table WhileOptions { + cond_subgraph_index:int; + body_subgraph_index:int; +} + +table NonMaxSuppressionV4Options { +} + +table NonMaxSuppressionV5Options { +} + +table ScatterNdOptions { +} + +table SelectV2Options { +} + +table DensifyOptions { +} + +table SegmentSumOptions { +} + +table BatchMatMulOptions { + adj_x:bool; + adj_y:bool; + // Parameters for BatchMatMul version 4 or above. + // If set to true, then weights-only op will use asymmetric quantization for + // inputs. + asymmetric_quantize_inputs: bool; +} + +table CumsumOptions { + exclusive:bool; + reverse:bool; +} + +table BroadcastToOptions { +} + +table Rfft2dOptions { +} + +table HashtableOptions { + // The identity of hash tables. This identity will be used across different + // subgraphs in the same interpreter instance. + table_id:int; + key_dtype:TensorType; + value_dtype:TensorType; +} + +table HashtableFindOptions { +} + +table HashtableImportOptions { +} + +table HashtableSizeOptions { +} + +table VarHandleOptions { + container:string; + shared_name:string; +} + +table ReadVariableOptions { +} + +table AssignVariableOptions { +} + +// An OperatorCode can be an enum value (BuiltinOperator) if the operator is a +// builtin, or a string if the operator is custom. +table OperatorCode { + // This field is for backward compatibility. This field will be used when + // the value of the extended builtin_code field has less than + // BulitinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES. + deprecated_builtin_code:byte; + custom_code:string; + + // The version of the operator. The version need to be bumped whenever new + // parameters are introduced into an op. + version:int = 1; + + // This field is introduced for resolving op builtin code shortage problem + // (the original BuiltinOperator enum field was represented as a byte). + // This field will be used when the value of the extended builtin_code field + // has greater than BulitinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES. + builtin_code:BuiltinOperator; +} + +enum CustomOptionsFormat : byte { + FLEXBUFFERS = 0, +} + +// An operator takes tensors as inputs and outputs. The type of operation being +// performed is determined by an index into the list of valid OperatorCodes, +// while the specifics of each operations is configured using builtin_options +// or custom_options. +table Operator { + // Index into the operator_codes array. Using an integer here avoids + // complicate map lookups. + opcode_index:uint; + + // Optional input are indicated by -1. + inputs:[int]; + outputs:[int]; + + builtin_options:BuiltinOptions; + custom_options:[ubyte]; + custom_options_format:CustomOptionsFormat; + + // A list of booleans indicating the input tensors which are being mutated by + // this operator.(e.g. used by RNN and LSTM). + // For example, if the "inputs" array refers to 5 tensors and the second and + // fifth are mutable variables, then this list will contain + // [false, true, false, false, true]. + // + // If the list is empty, no variable is mutated in this operator. + // The list either has the same length as `inputs`, or is empty. + mutating_variable_inputs:[bool]; + + // A list of indices to the subgraph's "tensors" that are internal to an Op. + // Internal tensors are those that do not flow in or out of the operation, + // but instead are part of internal computation. As such, the operation's + // implementation may manage its memory more efficiently. They are needed + // however (i.e. not just an implementation detail) since they are part of the + // computation, which may require relevant metadata such as quantization + // parameters. + intermediates:[int]; +} + +// The root type, defining a subgraph, which typically represents an entire +// model. +table SubGraph { + // A list of all tensors used in this subgraph. + tensors:[Tensor]; + + // Indices of the tensors that are inputs into this subgraph. Note this is + // the list of non-static tensors that feed into the subgraph for inference. + inputs:[int]; + + // Indices of the tensors that are outputs out of this subgraph. Note this is + // the list of output tensors that are considered the product of the + // subgraph's inference. + outputs:[int]; + + // All operators, in execution order. + operators:[Operator]; + + // Name of this subgraph (used for debugging). + name:string; +} + +// Table of raw data buffers (used for constant tensors). Referenced by tensors +// by index. The generous alignment accommodates mmap-friendly data structures. +table Buffer { + data:[ubyte] (force_align: 16); +} + +table Metadata { + // A human readable string to uniquely identify a Metadata. + name:string; + // An index to the buffers table. + buffer:uint; +} + +// Map from an alias name of tensor to tensor index in the graph. +// This is used in Signature def. +table TensorMap { + // Represents the alias to use for this tensor. + name:string; + + // The actual tensor index in the primary graph, that 'name' corresponds to. + tensor_index:uint; +} + +// This corresponds to SignatureDef in Tensorflow SavedModel. +// The SignatureDef will be part of the SavedModel provided for conversion. +table SignatureDef { + // Named inputs for this signature. + inputs:[TensorMap]; + + // Named outputs for this signature. + outputs:[TensorMap]; + + // Key value which was in the Tensorflow SavedModel SignatureDef map. + signature_key:string; + + // Model tag, deprecated. + deprecated_tag:string (deprecated); + + // Index of subgraphs that corresponds to the exported method. + subgraph_index:uint; +} + +table Model { + // Version of the schema. + version:uint; + + // A list of all operator codes used in this model. This is + // kept in order because operators carry an index into this + // vector. + operator_codes:[OperatorCode]; + + // All the subgraphs of the model. The 0th is assumed to be the main + // model. + subgraphs:[SubGraph]; + + // A description of the model. + description:string; + + // Buffers of the model. + // Note the 0th entry of this array must be an empty buffer (sentinel). + // This is a convention so that tensors without a buffer can provide 0 as + // their buffer. + buffers:[Buffer]; + + // Metadata about the model. Indirects into the existings buffers list. + // Deprecated, prefer to use metadata field. + metadata_buffer:[int]; + + // Metadata about the model. + metadata:[Metadata]; + + // Optional SignatureDefs for the model. + signature_defs:[SignatureDef]; +} + +root_type Model; diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_v3c.fbs b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_v3c.fbs new file mode 100644 index 0000000000000000000000000000000000000000..3c0ef1f135edf5e059bca26738998d723f879a10 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/schema/schema_v3c.fbs @@ -0,0 +1,1372 @@ +// 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. + +// Revision History +// Version 0: Initial version. +// Version 1: Add subgraphs to schema. +// Version 2: Rename operators to conform to NN API. +// Version 3: Move buffer data from Model.Subgraph.Tensors to Model.Buffers. +// Version 3a: Add new builtin op code field. Has backward compatibility with +// version 3. +// Version 3b: Rename fields in SignatureDef. Has backward compatibility with +// version 3 and 3a. +// Version 3c: Move constant tensor buffers & custom op buffers outside from +// Flatbuffers. Has backward compatibility with version 3, 3a and +// 3b. + +namespace tflite; + +// This corresponds to the version. +file_identifier "TFL3"; +// File extension of any written files. +file_extension "tflite"; + +// IMPORTANT: All new members of tables, enums and unions must be added at the +// end to ensure backwards compatibility. + +// The type of data stored in a tensor. +enum TensorType : byte { + FLOAT32 = 0, + FLOAT16 = 1, + INT32 = 2, + UINT8 = 3, + INT64 = 4, + STRING = 5, + BOOL = 6, + INT16 = 7, + COMPLEX64 = 8, + INT8 = 9, + FLOAT64 = 10, + COMPLEX128 = 11, + UINT64 = 12, + // Experimental: Resource and variant types are experimental, that are subject + // to change. Do not implement custom kernels using resource & variant types + // now. + RESOURCE = 13, + VARIANT = 14, + UINT32 = 15, + UINT16 = 16, + INT4 = 17, +} + +// Custom quantization parameters for experimenting with new quantization +// techniques. +table CustomQuantization { + custom:[ubyte] (force_align: 16); +} + +// Represents a specific quantization technique's parameters. +union QuantizationDetails { + CustomQuantization, +} + +// Parameters for converting a quantized tensor back to float. +table QuantizationParameters { + // These four parameters are the asymmetric linear quantization parameters. + // Given a quantized value q, the corresponding float value f should be: + // f = scale * (q - zero_point) + // For other quantization types, the QuantizationDetails below is used. + min:[float]; // For importing back into tensorflow. + max:[float]; // For importing back into tensorflow. + scale:[float]; // For dequantizing the tensor's values. + zero_point:[long]; + + // If this is not none, the other quantization parameters (i.e. min, max, + // scale, zero_point fields above) are ignored and the value of the + // QuantizationDetails union should be used. + details:QuantizationDetails; + + // Specifies the dimension of the Tensor's shape that the scales and + // zero_points correspond to. For example, a tensor t, with dims=[4, 3, 2, 1] + // with quantization params: + // scale=[1.0, 2.0, 3.0], zero_point=[1, 2, 3], quantization_dimension=1 + // will be quantized across the second dimension of t. + // t[:, 0, :, :] will have scale[0]=1.0, zero_point[0]=1 + // t[:, 1, :, :] will have scale[1]=2.0, zero_point[0]=2 + // t[:, 2, :, :] will have scale[2]=3.0, zero_point[0]=3 + quantized_dimension:int; +} + +// Sparse tensors. +// We use a modification of the TACO format. +// Reference: http://tensor-compiler.org/kjolstad-oopsla17-tensor-compiler.pdf +// +// To encode a conceptual n-dimensional dense tensor with dims (d0, ..., dn-1), +// potentially with a k-dimensional block (0 <= k <= n) with dims +// (dn, ..., dn+k-1), the format needs to specify: +// 1. In what order to traverse these dimensions. For example, to store a 2-D +// matrix in row major order, the traversal order would be (d0, d1), +// whereas to store it in column major order, the traversal order would be +// (d1, d0). If the 2-D matrix has a 2-D inner block, the traversal order +// could be (d0, d1, d2, d3). +// 2. How each block dimension in (dn, ..., dn+k-1) maps to the original +// tensor dimension in (d0, ..., dn-1). +// 3. In the traversal order defined above, the format (dense vs. sparse) and +// index metadata for each dimension. For a dense dimension, this is just +// the size of that dimension. For a sparse dimension, it's the same as +// the compressed index defined in the Compressed Sparse Row (CSR) format. +// (http://scipy-lectures.org/advanced/scipy_sparse/csr_matrix.html) + +// The storage type for a dimension. Currently we support: +// 1. DENSE: each coordinate in this dimension is stored implicitly. +// 2. SPARSE_CSR: only the coordinates with non-zero elements are stored. The +// compression technique is the same what CSR uses. +// More types like a sparse dimension with a different compression technique +// could be added to the list in the future. +enum DimensionType : byte { + DENSE = 0, + SPARSE_CSR = 1, +} + +table Int32Vector { + values:[int]; +} + +table Uint16Vector { + values:[ushort] (force_align: 4); +} + +table Uint8Vector { + values:[ubyte] (force_align: 4); +} + +// Variable-typed buffer to store the index metadata for a sparse dimension. +// The widest type is Int32 instead of UInt32 because tensor's shape is a int32 +// vector. We don't want the per-dimensional index to overflow that range. +union SparseIndexVector { + Int32Vector, + Uint16Vector, + Uint8Vector +} + +table DimensionMetadata { + // Whether a dimension is dense or sparse. + format:DimensionType; + // Index metadata used for a dimension. + // - If format is DimensionType.DENSE then we use the dense_size field to + // store the size of that dimension. Each index in that dimension is + // stored implicitly. + // - If format is DimensionType.SPARSE_CSR then we use array_segments and + // array_indices to encode that dimension. array_segments represents how + // to segment the indices array, each segment corresponds to one element + // in the previous dimension. array_indices represents the index of the + // non-zero elements within this dimension (as those in the CSR matrix + // format, where the first array is row pointers and the second array is + // column indices). + dense_size:int; + array_segments:SparseIndexVector; + array_indices:SparseIndexVector; +} + +// Parameters to encode a sparse TfLite tensor. +table SparsityParameters { + // The traversal order of the dimensions defined in the `shape` field of the + // conceptual dense tensor. For a n-dimensional tensors with dims (d0, d1, + // ..., dn-1), + // - if not block sparse, the traversal_order is just a permutation of (d0, + // ..., dn-1). For example, a 2-D matrix stored in row-major order would + // have traversal_order = (d0, d1). + // - if block sparse with a k-dimensional block (0 <= k <= n), the + // traversal_order has n + k elements. The first n elements are still a + // permutation of (d0, ..., dn-1). The lask k elements are a permutation + // of (dn, ..., dn+k-1), defining how to traverse a block internally. For + // example, a 2-D matrix with 2-D blocks, both stored in row-major order + // would have traversal_order = (d0, d1, d2, d3). + traversal_order:[int]; + // For an n-dimensional tensor with a k-dimensional block (0 <= k <= n), + // stores how a block dimension in (dn, ..., dn+k-1) maps to the original + // tensor dimension in (d0, ..., dn). + // It's stored in the order of (dn, ..., dn+k-1). + // If not block-sparse, this field is NULL. + block_map:[int]; + // In the traversal order defined above, the metadata needed for + // each dimension to locate the non-zero values in the original dense tensor. + // The size of the dim_metadata array = the size of the traversal_order array + // = n + k. + dim_metadata:[DimensionMetadata]; +} + +// The nested tensor type for VARIANT type. +table VariantSubType { + // The tensor shape. + shape:[int]; + type:TensorType; + // If false, the rank or the number of tensor dimensions is unknown. + // If false, "shape" must be []. + has_rank: bool = false; +} + +table Tensor { + // The tensor shape. The meaning of each entry is operator-specific but + // builtin ops use: [batch size, height, width, number of channels] (That's + // Tensorflow's NHWC). + shape:[int]; + type:TensorType; + // An index that refers to the buffers table at the root of the model. Or, + // if there is no data buffer associated (i.e. intermediate results), then + // this is 0 (which refers to an always existent empty buffer). + // + // The data_buffer itself is an opaque container, with the assumption that the + // target device is little-endian. In addition, all builtin operators assume + // the memory is ordered such that if `shape` is [4, 3, 2], then index + // [i, j, k] maps to data_buffer[i*3*2 + j*2 + k]. + buffer:uint; + name:string; // For debugging and importing back into tensorflow. + quantization:QuantizationParameters; // Optional. + + is_variable:bool = false; + + // Parameters to encode a sparse tensor. See the example in + // tensorflow/lite/testdata/sparse_tensor.json. + sparsity:SparsityParameters; // Optional. + + // Encodes `shape` with unknown dimensions. Unknown dimensions are + // represented with -1. + shape_signature:[int]; // Optional. + + // If false, the rank or the number of tensor dimensions is unknown. + // If false, "shape" must be []. + has_rank: bool = false; + + // The nested Tensor types for VARIANT type. This is always empty for + // non-VARIANT types. This is optional because the nested type can be omitted. + // Currently only 1 subtype is supported. The field is defined as an array for + // flexibility of supporting multiple subtypes in the future. + variant_tensors:[VariantSubType]; +} + +// A list of builtin operators. Builtin operators are slightly faster than custom +// ones, but not by much. Moreover, while custom operators accept an opaque +// object containing configuration parameters, builtins have a predetermined +// set of acceptable options. +// LINT.IfChange +enum BuiltinOperator : int32 { + ADD = 0, + AVERAGE_POOL_2D = 1, + CONCATENATION = 2, + CONV_2D = 3, + DEPTHWISE_CONV_2D = 4, + DEPTH_TO_SPACE = 5, + DEQUANTIZE = 6, + EMBEDDING_LOOKUP = 7, + FLOOR = 8, + FULLY_CONNECTED = 9, + HASHTABLE_LOOKUP = 10, + L2_NORMALIZATION = 11, + L2_POOL_2D = 12, + LOCAL_RESPONSE_NORMALIZATION = 13, + LOGISTIC = 14, + LSH_PROJECTION = 15, + LSTM = 16, + MAX_POOL_2D = 17, + MUL = 18, + RELU = 19, + // NOTE(aselle): RELU_N1_TO_1 used to be called RELU1, but it was renamed + // since different model developers use RELU1 in different ways. Never + // create another op called RELU1. + RELU_N1_TO_1 = 20, + RELU6 = 21, + RESHAPE = 22, + RESIZE_BILINEAR = 23, + RNN = 24, + SOFTMAX = 25, + SPACE_TO_DEPTH = 26, + SVDF = 27, + TANH = 28, + CONCAT_EMBEDDINGS = 29, + SKIP_GRAM = 30, + CALL = 31, + CUSTOM = 32, + EMBEDDING_LOOKUP_SPARSE = 33, + PAD = 34, + UNIDIRECTIONAL_SEQUENCE_RNN = 35, + GATHER = 36, + BATCH_TO_SPACE_ND = 37, + SPACE_TO_BATCH_ND = 38, + TRANSPOSE = 39, + MEAN = 40, + SUB = 41, + DIV = 42, + SQUEEZE = 43, + UNIDIRECTIONAL_SEQUENCE_LSTM = 44, + STRIDED_SLICE = 45, + BIDIRECTIONAL_SEQUENCE_RNN = 46, + EXP = 47, + TOPK_V2 = 48, + SPLIT = 49, + LOG_SOFTMAX = 50, + // DELEGATE is a special op type for the operations which are delegated to + // other backends. + // WARNING: Experimental interface, subject to change + DELEGATE = 51, + BIDIRECTIONAL_SEQUENCE_LSTM = 52, + CAST = 53, + PRELU = 54, + MAXIMUM = 55, + ARG_MAX = 56, + MINIMUM = 57, + LESS = 58, + NEG = 59, + PADV2 = 60, + GREATER = 61, + GREATER_EQUAL = 62, + LESS_EQUAL = 63, + SELECT = 64, + SLICE = 65, + SIN = 66, + TRANSPOSE_CONV = 67, + SPARSE_TO_DENSE = 68, + TILE = 69, + EXPAND_DIMS = 70, + EQUAL = 71, + NOT_EQUAL = 72, + LOG = 73, + SUM = 74, + SQRT = 75, + RSQRT = 76, + SHAPE = 77, + POW = 78, + ARG_MIN = 79, + FAKE_QUANT = 80, + REDUCE_PROD = 81, + REDUCE_MAX = 82, + PACK = 83, + LOGICAL_OR = 84, + ONE_HOT = 85, + LOGICAL_AND = 86, + LOGICAL_NOT = 87, + UNPACK = 88, + REDUCE_MIN = 89, + FLOOR_DIV = 90, + REDUCE_ANY = 91, + SQUARE = 92, + ZEROS_LIKE = 93, + FILL = 94, + FLOOR_MOD = 95, + RANGE = 96, + RESIZE_NEAREST_NEIGHBOR = 97, + LEAKY_RELU = 98, + SQUARED_DIFFERENCE = 99, + MIRROR_PAD = 100, + ABS = 101, + SPLIT_V = 102, + UNIQUE = 103, + CEIL = 104, + REVERSE_V2 = 105, + ADD_N = 106, + GATHER_ND = 107, + COS = 108, + WHERE = 109, + RANK = 110, + ELU = 111, + REVERSE_SEQUENCE = 112, + MATRIX_DIAG = 113, + QUANTIZE = 114, + MATRIX_SET_DIAG = 115, + ROUND = 116, + HARD_SWISH = 117, + IF = 118, + WHILE = 119, + NON_MAX_SUPPRESSION_V4 = 120, + NON_MAX_SUPPRESSION_V5 = 121, + SCATTER_ND = 122, + SELECT_V2 = 123, + DENSIFY = 124, + SEGMENT_SUM = 125, + BATCH_MATMUL = 126, + PLACEHOLDER_FOR_GREATER_OP_CODES = 127, + CUMSUM = 128, + CALL_ONCE = 129, + BROADCAST_TO = 130, + RFFT2D = 131, + CONV_3D = 132, + IMAG=133, + REAL=134, + COMPLEX_ABS=135, + HASHTABLE = 136, + HASHTABLE_FIND = 137, + HASHTABLE_IMPORT = 138, + HASHTABLE_SIZE = 139, + REDUCE_ALL = 140, + CONV_3D_TRANSPOSE = 141, + VAR_HANDLE = 142, + READ_VARIABLE = 143, + ASSIGN_VARIABLE = 144, + BROADCAST_ARGS = 145, + RANDOM_STANDARD_NORMAL = 146, + BUCKETIZE = 147, + RANDOM_UNIFORM = 148, + MULTINOMIAL = 149, + GELU = 150, + DYNAMIC_UPDATE_SLICE = 151, + RELU_0_TO_1 = 152, + UNSORTED_SEGMENT_PROD = 153, + UNSORTED_SEGMENT_MAX = 154, + UNSORTED_SEGMENT_SUM = 155, + ATAN2 = 156, + UNSORTED_SEGMENT_MIN = 157, + SIGN = 158, + BITCAST = 159, + BITWISE_XOR = 160, + RIGHT_SHIFT = 161, +} +// LINT.ThenChange(nnapi_linter/linter.proto) + +// Options for the builtin operators. +union BuiltinOptions { + Conv2DOptions, + DepthwiseConv2DOptions, + ConcatEmbeddingsOptions, + LSHProjectionOptions, + Pool2DOptions, + SVDFOptions, + RNNOptions, + FullyConnectedOptions, + SoftmaxOptions, + ConcatenationOptions, + AddOptions, + L2NormOptions, + LocalResponseNormalizationOptions, + LSTMOptions, + ResizeBilinearOptions, + CallOptions, + ReshapeOptions, + SkipGramOptions, + SpaceToDepthOptions, + EmbeddingLookupSparseOptions, + MulOptions, + PadOptions, + GatherOptions, + BatchToSpaceNDOptions, + SpaceToBatchNDOptions, + TransposeOptions, + ReducerOptions, + SubOptions, + DivOptions, + SqueezeOptions, + SequenceRNNOptions, + StridedSliceOptions, + ExpOptions, + TopKV2Options, + SplitOptions, + LogSoftmaxOptions, + CastOptions, + DequantizeOptions, + MaximumMinimumOptions, + ArgMaxOptions, + LessOptions, + NegOptions, + PadV2Options, + GreaterOptions, + GreaterEqualOptions, + LessEqualOptions, + SelectOptions, + SliceOptions, + TransposeConvOptions, + SparseToDenseOptions, + TileOptions, + ExpandDimsOptions, + EqualOptions, + NotEqualOptions, + ShapeOptions, + PowOptions, + ArgMinOptions, + FakeQuantOptions, + PackOptions, + LogicalOrOptions, + OneHotOptions, + LogicalAndOptions, + LogicalNotOptions, + UnpackOptions, + FloorDivOptions, + SquareOptions, + ZerosLikeOptions, + FillOptions, + BidirectionalSequenceLSTMOptions, + BidirectionalSequenceRNNOptions, + UnidirectionalSequenceLSTMOptions, + FloorModOptions, + RangeOptions, + ResizeNearestNeighborOptions, + LeakyReluOptions, + SquaredDifferenceOptions, + MirrorPadOptions, + AbsOptions, + SplitVOptions, + UniqueOptions, + ReverseV2Options, + AddNOptions, + GatherNdOptions, + CosOptions, + WhereOptions, + RankOptions, + ReverseSequenceOptions, + MatrixDiagOptions, + QuantizeOptions, + MatrixSetDiagOptions, + HardSwishOptions, + IfOptions, + WhileOptions, + DepthToSpaceOptions, + NonMaxSuppressionV4Options, + NonMaxSuppressionV5Options, + ScatterNdOptions, + SelectV2Options, + DensifyOptions, + SegmentSumOptions, + BatchMatMulOptions, + CumsumOptions, + CallOnceOptions, + BroadcastToOptions, + Rfft2dOptions, + Conv3DOptions, + HashtableOptions, + HashtableFindOptions, + HashtableImportOptions, + HashtableSizeOptions, + VarHandleOptions, + ReadVariableOptions, + AssignVariableOptions, + RandomOptions, + BucketizeOptions, + GeluOptions, + DynamicUpdateSliceOptions, + UnsortedSegmentProdOptions, + UnsortedSegmentMaxOptions, + UnsortedSegmentMinOptions, + UnsortedSegmentSumOptions, + ATan2Options, + SignOptions, + BitcastOptions, + BitwiseXorOptions, + RightShiftOptions, +} + +// LINT.IfChange +enum Padding : byte { SAME, VALID } +// LINT.ThenChange(//tensorflow/compiler/mlir/lite/ir/tfl_op_enums.td) + +// LINT.IfChange +enum ActivationFunctionType : byte { + NONE = 0, + RELU = 1, + RELU_N1_TO_1 = 2, + RELU6 = 3, + TANH = 4, + SIGN_BIT = 5, +} +// LINT.ThenChange(//tensorflow/compiler/mlir/lite/ir/tfl_op_enums.td) + +table Conv2DOptions { + padding:Padding; + stride_w:int; + stride_h:int; + fused_activation_function:ActivationFunctionType; + dilation_w_factor:int = 1; + dilation_h_factor:int = 1; +} + +// Options for both Conv3D and Conv3DTranspose. +table Conv3DOptions { + padding:Padding; + stride_d:int; + stride_w:int; + stride_h:int; + fused_activation_function:ActivationFunctionType; + dilation_d_factor:int = 1; + dilation_w_factor:int = 1; + dilation_h_factor:int = 1; +} + +table Pool2DOptions { + padding:Padding; + stride_w:int; + stride_h:int; + filter_width:int; + filter_height:int; + fused_activation_function:ActivationFunctionType; +} + +table DepthwiseConv2DOptions { + // Parameters for DepthwiseConv version 1 or above. + padding:Padding; + stride_w:int; + stride_h:int; + // `depth_multiplier` is redundant. It's used by CPU kernels in + // TensorFlow 2.0 or below, but ignored in versions above. + // See comments in lite/c/builtin_op_data.h for more details. + depth_multiplier:int; + fused_activation_function:ActivationFunctionType; + // Parameters for DepthwiseConv version 2 or above. + dilation_w_factor:int = 1; + dilation_h_factor:int = 1; +} + +table ConcatEmbeddingsOptions { + num_channels:int; + num_columns_per_channel:[int]; + embedding_dim_per_channel:[int]; // This could be inferred from parameters. +} + +enum LSHProjectionType: byte { + UNKNOWN = 0, + SPARSE = 1, + DENSE = 2, +} + +table LSHProjectionOptions { + type: LSHProjectionType; +} + +table SVDFOptions { + rank:int; + fused_activation_function:ActivationFunctionType; + // For weights-only quantization, use asymmetric quantization for non + // constant inputs at evaluation time. + asymmetric_quantize_inputs:bool; +} + +// An implementation of TensorFlow RNNCell. +table RNNOptions { + fused_activation_function:ActivationFunctionType; + asymmetric_quantize_inputs:bool; +} + +// An implementation of TensorFlow dynamic_rnn with RNNCell. +table SequenceRNNOptions { + time_major:bool; + fused_activation_function:ActivationFunctionType; + asymmetric_quantize_inputs:bool; +} + +// An implementation of TensorFlow bidrectional_dynamic_rnn with RNNCell. +table BidirectionalSequenceRNNOptions { + time_major:bool; + fused_activation_function:ActivationFunctionType; + merge_outputs: bool; + asymmetric_quantize_inputs:bool; +} + +// LINT.IfChange +enum FullyConnectedOptionsWeightsFormat: byte { + DEFAULT = 0, + SHUFFLED4x16INT8 = 1, +} +// LINT.ThenChange(//tensorflow/compiler/mlir/lite/ir/tfl_op_enums.td) + +// An implementation of TensorFlow fully_connected (a.k.a Dense) layer. +table FullyConnectedOptions { + // Parameters for FullyConnected version 1 or above. + fused_activation_function:ActivationFunctionType; + + // Parameters for FullyConnected version 2 or above. + weights_format:FullyConnectedOptionsWeightsFormat = DEFAULT; + + // Parameters for FullyConnected version 5 or above. + // If set to true, then the number of dimension is preserved. Furthermore, + // all but the last dimension of the input and output shapes will be equal. + keep_num_dims: bool; + + // Parameters for FullyConnected version 7 or above. + // If set to true, then weights-only op will use asymmetric quantization for + // inputs. + asymmetric_quantize_inputs: bool; +} + +table SoftmaxOptions { + beta: float; +} + +// An implementation of TensorFlow concat. +table ConcatenationOptions { + axis:int; + fused_activation_function:ActivationFunctionType; +} + +table AddOptions { + fused_activation_function:ActivationFunctionType; + // Parameters supported by version 3. + pot_scale_int16:bool = true; +} + +table MulOptions { + fused_activation_function:ActivationFunctionType; +} + +table L2NormOptions { + // This field is currently ignored in the L2 Norm Op. + fused_activation_function:ActivationFunctionType; +} + +table LocalResponseNormalizationOptions { + radius:int; + bias:float; + alpha:float; + beta:float; +} + +// LINT.IfChange +enum LSTMKernelType : byte { + // Full LSTM kernel which supports peephole and projection. + FULL = 0, + // Basic LSTM kernels. Equivalent to TensorFlow BasicLSTMCell. + BASIC = 1, +} +// LINT.ThenChange(//tensorflow/compiler/mlir/lite/ir/tfl_op_enums.td) + +// An implementation of TensorFlow LSTMCell and CoupledInputForgetGateLSTMCell +table LSTMOptions { + // Parameters for LSTM version 1 or above. + fused_activation_function:ActivationFunctionType; + cell_clip: float; // Optional, 0.0 means no clipping + proj_clip: float; // Optional, 0.0 means no clipping + + // Parameters for LSTM version 2 or above. + // Basic kernel is only supported in version 2 or above. + kernel_type: LSTMKernelType = FULL; + + // Parameters for LSTM version 4 or above. + asymmetric_quantize_inputs: bool; +} + +// An implementation of TensorFlow dynamic_rnn with LSTMCell. +table UnidirectionalSequenceLSTMOptions { + fused_activation_function:ActivationFunctionType; + cell_clip: float; // Optional, 0.0 means no clipping + proj_clip: float; // Optional, 0.0 means no clipping + + // If true then first dimension is sequence, otherwise batch. + time_major:bool; + + // Parameter for Unidirectional Sequence LSTM version 3. + asymmetric_quantize_inputs:bool; + + // Parameter for unidirectional sequence RNN version 4. + diagonal_recurrent_tensors:bool; +} + +table BidirectionalSequenceLSTMOptions { + // Parameters supported by version 1: + fused_activation_function:ActivationFunctionType; + cell_clip: float; // Optional, 0.0 means no clipping + proj_clip: float; // Optional, 0.0 means no clipping + + // If true, store the outputs of both directions into the first output. + merge_outputs: bool; + + // Parameters supported by version 2: + // If true then first dimension is sequence, otherwise batch. + // Version 1 implementations assumed time_major to be true, so this default + // value should never change. + time_major: bool = true; + + // Parameters for version 3 or above. + asymmetric_quantize_inputs:bool; +} + +table ResizeBilinearOptions { + new_height: int (deprecated); + new_width: int (deprecated); + align_corners: bool; + half_pixel_centers: bool; +} + +table ResizeNearestNeighborOptions { + align_corners: bool; + half_pixel_centers: bool; +} + +// A call operation options +table CallOptions { + // The subgraph index that needs to be called. + subgraph:uint; +} + +table PadOptions { +} + +table PadV2Options { +} + +table ReshapeOptions { + new_shape:[int]; +} + +table SpaceToBatchNDOptions { +} + +table BatchToSpaceNDOptions { +} + +table SkipGramOptions { + ngram_size: int; + max_skip_size: int; + include_all_ngrams: bool; +} + +table SpaceToDepthOptions { + block_size: int; +} + +table DepthToSpaceOptions { + block_size: int; +} + +table SubOptions { + fused_activation_function:ActivationFunctionType; + // Parameters supported by version 5 + pot_scale_int16:bool = true; +} + +table DivOptions { + fused_activation_function:ActivationFunctionType; +} + +table TopKV2Options { +} + +enum CombinerType : byte { + SUM = 0, + MEAN = 1, + SQRTN = 2, +} + +table EmbeddingLookupSparseOptions { + combiner:CombinerType; +} + +table GatherOptions { + axis: int; + // Parameters for Gather version 5 or above. + batch_dims: int = 0; +} + +table TransposeOptions { +} + +table ExpOptions { +} + +table CosOptions { +} + +table ReducerOptions { + keep_dims: bool; +} + +table SqueezeOptions { + squeeze_dims:[int]; +} + +table SplitOptions { + num_splits: int; +} + +table SplitVOptions { + num_splits: int; +} + +table StridedSliceOptions { + begin_mask: int; + end_mask: int; + ellipsis_mask: int; + new_axis_mask: int; + shrink_axis_mask: int; + // If true, then the end tensor is an offset of the begin tensor. + offset: bool; +} + +table LogSoftmaxOptions { +} + +table CastOptions { + in_data_type: TensorType; + out_data_type: TensorType; +} + +table DequantizeOptions { +} + +table MaximumMinimumOptions { +} + +table TileOptions { +} + +table ArgMaxOptions { + output_type : TensorType; +} + +table ArgMinOptions { + output_type : TensorType; +} + +table GreaterOptions { +} + +table GreaterEqualOptions { +} + +table LessOptions { +} + +table LessEqualOptions { +} + +table NegOptions { +} + +table SelectOptions { +} + +table SliceOptions { +} + +table TransposeConvOptions { + // Parameters supported by version 1, 2, 3: + padding:Padding; + stride_w:int; + stride_h:int; + + // Parameters supported by version 4: + fused_activation_function:ActivationFunctionType = NONE; +} + +table ExpandDimsOptions { +} + +table SparseToDenseOptions { + validate_indices:bool; +} + +table EqualOptions { +} + +table NotEqualOptions { +} + +table ShapeOptions { + // Optional output type of the operation (int32 or int64). Defaults to int32. + out_type : TensorType; +} + +table RankOptions { +} + +table PowOptions { +} + +table FakeQuantOptions { + // Parameters supported by version 1: + min:float; + max:float; + num_bits:int; + + // Parameters supported by version 2: + narrow_range:bool; +} + +table PackOptions { + values_count:int; + axis:int; +} + +table LogicalOrOptions { +} + +table OneHotOptions { + axis:int; +} + +table AbsOptions { +} + + +table HardSwishOptions { +} + +table LogicalAndOptions { +} + +table LogicalNotOptions { +} + +table UnpackOptions { + num:int; + axis:int; +} + +table FloorDivOptions { +} + +table SquareOptions { +} + +table ZerosLikeOptions { +} + +table FillOptions { +} + +table FloorModOptions { +} + +table RangeOptions { +} + +table LeakyReluOptions { + alpha:float; +} + +table SquaredDifferenceOptions { +} + +// LINT.IfChange +enum MirrorPadMode : byte { + // Doesn't include borders. + REFLECT = 0, + // Includes borders. + SYMMETRIC = 1, +} +// LINT.ThenChange(//tensorflow/compiler/mlir/lite/ir/tfl_op_enums.td) + +table MirrorPadOptions { + mode:MirrorPadMode; +} + +table UniqueOptions { + idx_out_type:TensorType = INT32; +} + +table ReverseV2Options { +} + +table AddNOptions { +} + +table GatherNdOptions { +} + +table WhereOptions { +} + +table ReverseSequenceOptions { + seq_dim:int; + batch_dim:int = 0; +} + +table MatrixDiagOptions { +} + +table QuantizeOptions { +} + +table MatrixSetDiagOptions { +} + +table IfOptions { + then_subgraph_index:int; + else_subgraph_index:int; +} + +table CallOnceOptions { + init_subgraph_index:int; +} + +table WhileOptions { + cond_subgraph_index:int; + body_subgraph_index:int; +} + +table NonMaxSuppressionV4Options { +} + +table NonMaxSuppressionV5Options { +} + +table ScatterNdOptions { +} + +table SelectV2Options { +} + +table DensifyOptions { +} + +table SegmentSumOptions { +} + +table BatchMatMulOptions { + adj_x:bool; + adj_y:bool; + // Parameters for BatchMatMul version 4 or above. + // If set to true, then weights-only op will use asymmetric quantization for + // inputs. + asymmetric_quantize_inputs: bool; +} + +table CumsumOptions { + exclusive:bool; + reverse:bool; +} + +table BroadcastToOptions { +} + +table Rfft2dOptions { +} + +table HashtableOptions { + // The identity of hash tables. This identity will be used across different + // subgraphs in the same interpreter instance. + table_id:int; + key_dtype:TensorType; + value_dtype:TensorType; +} + +table HashtableFindOptions { +} + +table HashtableImportOptions { +} + +table HashtableSizeOptions { +} + +table VarHandleOptions { + container:string; + shared_name:string; +} + +table ReadVariableOptions { +} + +table AssignVariableOptions { +} + +table RandomOptions { + seed: long; + seed2: long; +} + +table BucketizeOptions { + boundaries: [float]; // The bucket boundaries. +} + +table GeluOptions { + approximate: bool; +} + +table DynamicUpdateSliceOptions { +} + +table UnsortedSegmentProdOptions { +} + +table UnsortedSegmentMaxOptions { +} + +table UnsortedSegmentSumOptions { +} + +table ATan2Options { +} + +table UnsortedSegmentMinOptions{ +} + +table SignOptions { +} + +table BitcastOptions { +} + +table BitwiseXorOptions { +} + +table RightShiftOptions { +} + +// An OperatorCode can be an enum value (BuiltinOperator) if the operator is a +// builtin, or a string if the operator is custom. +table OperatorCode { + // This field is for backward compatibility. This field will be used when + // the value of the extended builtin_code field has less than + // BulitinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES. + deprecated_builtin_code:byte; + custom_code:string; + + // The version of the operator. The version need to be bumped whenever new + // parameters are introduced into an op. + version:int = 1; + + // This field is introduced for resolving op builtin code shortage problem + // (the original BuiltinOperator enum field was represented as a byte). + // This field will be used when the value of the extended builtin_code field + // has greater than BulitinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES. + builtin_code:BuiltinOperator; +} + +enum CustomOptionsFormat : byte { + FLEXBUFFERS = 0, +} + +// An operator takes tensors as inputs and outputs. The type of operation being +// performed is determined by an index into the list of valid OperatorCodes, +// while the specifics of each operations is configured using builtin_options +// or custom_options. +table Operator { + // Index into the operator_codes array. Using an integer here avoids + // complicate map lookups. + opcode_index:uint; + + // Optional input are indicated by -1. + inputs:[int]; + outputs:[int]; + + builtin_options:BuiltinOptions; + custom_options:[ubyte]; + custom_options_format:CustomOptionsFormat; + + // A list of booleans indicating the input tensors which are being mutated by + // this operator.(e.g. used by RNN and LSTM). + // For example, if the "inputs" array refers to 5 tensors and the second and + // fifth are mutable variables, then this list will contain + // [false, true, false, false, true]. + // + // If the list is empty, no variable is mutated in this operator. + // The list either has the same length as `inputs`, or is empty. + mutating_variable_inputs:[bool]; + + // A list of indices to the subgraph's "tensors" that are internal to an Op. + // Internal tensors are those that do not flow in or out of the operation, + // but instead are part of internal computation. As such, the operation's + // implementation may manage its memory more efficiently. They are needed + // however (i.e. not just an implementation detail) since they are part of the + // computation, which may require relevant metadata such as quantization + // parameters. + intermediates:[int]; + + // When an op is using custom_options in a model that is larger than 2GB, then + // we instead use the following attributes to find the buffer location which + // is stored outside of flatbuffers. + large_custom_options_offset: ulong; + large_custom_options_size: ulong; +} + +// The root type, defining a subgraph, which typically represents an entire +// model. +table SubGraph { + // A list of all tensors used in this subgraph. + tensors:[Tensor]; + + // Indices of the tensors that are inputs into this subgraph. Note this is + // the list of non-static tensors that feed into the subgraph for inference. + inputs:[int]; + + // Indices of the tensors that are outputs out of this subgraph. Note this is + // the list of output tensors that are considered the product of the + // subgraph's inference. + outputs:[int]; + + // All operators, in execution order. + operators:[Operator]; + + // Name of this subgraph (used for debugging). + name:string; +} + +// Table of raw data buffers (used for constant tensors). Referenced by tensors +// by index. The generous alignment accommodates mmap-friendly data structures. +table Buffer { + data:[ubyte] (force_align: 16); + + // In a model that is larger than 2GB, then buffers instead uses the following + // attributes to find stored data, which is outside of flatbuffers + // the offset is calculated relative to the beginning of the file and is only + // valid if > 1. + offset: ulong; + size: ulong; +} + +table Metadata { + // A human readable string to uniquely identify a Metadata. + name:string; + // An index to the buffers table. + buffer:uint; +} + +// Map from an alias name of tensor to tensor index in the graph. +// This is used in Signature def. +table TensorMap { + // Represents the alias to use for this tensor. + name:string; + + // The actual tensor index in the primary graph, that 'name' corresponds to. + tensor_index:uint; +} + +// This corresponds to SignatureDef in Tensorflow SavedModel. +// The SignatureDef will be part of the SavedModel provided for conversion. +table SignatureDef { + // Named inputs for this signature. + inputs:[TensorMap]; + + // Named outputs for this signature. + outputs:[TensorMap]; + + // Key value which was in the Tensorflow SavedModel SignatureDef map. + signature_key:string; + + // Model tag, deprecated. + deprecated_tag:string (deprecated); + + // Index of subgraphs that corresponds to the exported method. + subgraph_index:uint; +} + +table Model { + // Version of the schema. + version:uint; + + // A list of all operator codes used in this model. This is + // kept in order because operators carry an index into this + // vector. + operator_codes:[OperatorCode]; + + // All the subgraphs of the model. The 0th is assumed to be the main + // model. + subgraphs:[SubGraph]; + + // A description of the model. + description:string; + + // Buffers of the model. + // Note the 0th entry of this array must be an empty buffer (sentinel). + // This is a convention so that tensors without a buffer can provide 0 as + // their buffer. + buffers:[Buffer]; + + // Metadata about the model. Indirects into the existings buffers list. + // Deprecated, prefer to use metadata field. + metadata_buffer:[int]; + + // Metadata about the model. + metadata:[Metadata]; + + // Optional SignatureDefs for the model. + signature_defs:[SignatureDef]; +} + +root_type Model; diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/sparsity/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/sparsity/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..e5d64aa014c3dc3661ca3211b1640104c80c427e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/sparsity/BUILD @@ -0,0 +1,65 @@ +load("//tensorflow:tensorflow.bzl", "tf_cc_test") +load("//tensorflow/core/platform:rules_cc.bzl", "cc_library") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + default_visibility = [ + ":friends", + "//tensorflow:__pkg__", + ], + licenses = ["notice"], +) + +package_group( + name = "friends", + packages = [ + "//learning/brain/experimental/mlir/...", + "//tensorflow/compiler/mlir/lite/...", + "//tensorflow/lite/...", + ], +) + +cc_library( + name = "sparsify_model", + srcs = [ + "sparsify_model.cc", + ], + hdrs = [ + "sparsify_model.h", + ], + deps = [ + "//tensorflow/compiler/mlir/lite:flatbuffer_translate_lib", + "//tensorflow/compiler/mlir/lite:pass_registry_utils", + "//tensorflow/compiler/mlir/lite:tensorflow_lite_d2s", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs", + "//tensorflow/compiler/mlir/lite/tools/optimize:reduced_precision_metadata", + "//tensorflow/compiler/mlir/tensorflow:error_util", + "//tensorflow/core:protos_all_cc", + "@com_google_absl//absl/log", + "@com_google_absl//absl/status", + "@com_google_absl//absl/strings", + "@flatbuffers", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + ], +) + +tf_cc_test( + name = "sparsify_model_test", + srcs = ["sparsify_model_test.cc"], + data = [ + ":testdata/sparse_tensor.bin", + ], + deps = [ + ":sparsify_model", + "//tensorflow/compiler/mlir/lite/core:absl_error_model_builder", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs", + "//tensorflow/compiler/mlir/lite/tools/optimize:reduced_precision_metadata", + "@com_google_absl//absl/status", + "@com_google_googletest//:gtest_main", + "@flatbuffers", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/sparsity/sparsify_model.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/sparsity/sparsify_model.cc new file mode 100644 index 0000000000000000000000000000000000000000..8576a70969bae1468c6fe8a197f473380e56a1c9 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/sparsity/sparsify_model.cc @@ -0,0 +1,110 @@ +/* Copyright 2020 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/compiler/mlir/lite/sparsity/sparsify_model.h" + +#include +#include + +#include "absl/log/log.h" +#include "absl/status/status.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" +#include "flatbuffers/buffer.h" // from @flatbuffers +#include "flatbuffers/flatbuffer_builder.h" // from @flatbuffers +#include "llvm/ADT/SmallVector.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/Location.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/OwningOpRef.h" // from @llvm-project +#include "mlir/Pass/PassManager.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/flatbuffer_export.h" +#include "tensorflow/compiler/mlir/lite/flatbuffer_import.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" +#include "tensorflow/compiler/mlir/lite/tools/optimize/reduced_precision_metadata.h" +#include "tensorflow/compiler/mlir/lite/transforms/dense_to_sparse_pass.h" +#include "tensorflow/compiler/mlir/lite/transforms/pass_registry_utils.h" +#include "tensorflow/compiler/mlir/tensorflow/utils/error_util.h" +#include "tensorflow/core/framework/types.pb.h" + +namespace mlir { +namespace lite { + +absl::Status SparsifyModel(const tflite::ModelT& input_model, + flatbuffers::FlatBufferBuilder* builder) { + MLIRContext context; + StatusScopedDiagnosticHandler statusHandler(&context, + /*propagate=*/true); + + // Import input_model to a MLIR module + flatbuffers::FlatBufferBuilder input_builder; + flatbuffers::Offset input_model_location = + tflite::Model::Pack(input_builder, &input_model); + tflite::FinishModelBuffer(input_builder, input_model_location); + + std::string serialized_model( + reinterpret_cast(input_builder.GetBufferPointer()), + input_builder.GetSize()); + + OwningOpRef module = tflite::FlatBufferToMlir( + serialized_model, &context, UnknownLoc::get(&context)); + if (!module) { + LOG(ERROR) << "Couldn't import flatbuffer to MLIR."; + return absl::InternalError("Couldn't import flatbuffer to MLIR."); + } + + PassManager pm((*module)->getName(), OpPassManager::Nesting::Implicit); + pm.addPass(TFL::Create()); + + if (failed(pm.run(module.get()))) { + LOG(ERROR) << "Failed to sparsify: " + << statusHandler.ConsumeStatus().message(); + return absl::InternalError(absl::StrCat( + "Failed to sparsify: ", statusHandler.ConsumeStatus().message())); + } + + // Export the results to the builder + std::string result; + tflite::FlatbufferExportOptions options; + options.converter_flags.set_force_select_tf_ops(false); + options.converter_flags.set_enable_select_tf_ops(true); + options.converter_flags.set_allow_custom_ops(true); + + // Copy metadata for Reduced Precision Support from input model if it exists + for (const auto& metadata : input_model.metadata) { + if (metadata->name != tflite::optimize::kTfLiteReducedPrecisionKey) { + continue; + } + + const auto& data = input_model.buffers[metadata->buffer]->data; + options.metadata[metadata->name] = std::string(data.begin(), data.end()); + break; + } + + if (!tflite::MlirToFlatBufferTranslateFunction(module.get(), options, + &result)) { + LOG(ERROR) << "Failed to export MLIR to flatbuffer."; + return absl::InternalError("Failed to export MLIR to flatbuffer."); + } + builder->PushFlatBuffer(reinterpret_cast(result.data()), + result.size()); + + return absl::OkStatus(); +} + +} // namespace lite +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/sparsity/sparsify_model.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/sparsity/sparsify_model.h new file mode 100644 index 0000000000000000000000000000000000000000..04be083820730575ff68d3ade2750d058ef14c1d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/sparsity/sparsify_model.h @@ -0,0 +1,31 @@ +/* Copyright 2020 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_SPARSITY_SPARSIFY_MODEL_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_SPARSITY_SPARSIFY_MODEL_H_ + +#include "absl/status/status.h" +#include "flatbuffers/flatbuffer_builder.h" // from @flatbuffers +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" + +namespace mlir { +namespace lite { + +// Sparsify the `input_model` and write the result to a flatbuffer `builder`. +absl::Status SparsifyModel(const tflite::ModelT& input_model, + flatbuffers::FlatBufferBuilder* builder); +} // namespace lite +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_SPARSITY_SPARSIFY_MODEL_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/sparsity/sparsify_model_test.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/sparsity/sparsify_model_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..bd355421442f792fc9366737775192f54c9f127e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/sparsity/sparsify_model_test.cc @@ -0,0 +1,81 @@ +/* Copyright 2022 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/compiler/mlir/lite/sparsity/sparsify_model.h" + +#include + +#include +#include +#include +#include +#include + +#include +#include +#include "absl/status/status.h" +#include "flatbuffers/flatbuffer_builder.h" // from @flatbuffers +#include "tensorflow/compiler/mlir/lite/core/absl_error_model_builder.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" +#include "tensorflow/compiler/mlir/lite/tools/optimize/reduced_precision_metadata.h" + +namespace mlir { +namespace lite { +namespace { + + +TEST(SparsifyModelTest, MetadataIsAddedToOutputModel) { + std::string expected_key = tflite::optimize::kTfLiteReducedPrecisionKey; + std::string expected_value = "test_data"; + + // Load input model + auto input_fbm = mlir::TFL::FlatBufferModelAbslError::BuildFromFile( + "tensorflow/compiler/mlir/lite/sparsity/testdata/" + "sparse_tensor.bin"); + tflite::ModelT input_model; + input_fbm->GetModel()->UnPackTo(&input_model); + + // Populate input metadata + auto model_metadata_buffer = std::make_unique(); + model_metadata_buffer->data = + std::vector(expected_value.begin(), expected_value.end()); + input_model.buffers.push_back(std::move(model_metadata_buffer)); + auto metadata_t = std::make_unique(); + metadata_t->name = tflite::optimize::kTfLiteReducedPrecisionKey; + metadata_t->buffer = input_model.buffers.size() - 1; + input_model.metadata.push_back(std::move(metadata_t)); + + // Sparsify and create output model + flatbuffers::FlatBufferBuilder output_builder; + ASSERT_TRUE(SparsifyModel(input_model, &output_builder).ok()); + auto output_fbm = mlir::TFL::FlatBufferModelAbslError::BuildFromBuffer( + reinterpret_cast(output_builder.GetCurrentBufferPointer()), + output_builder.GetSize()); + tflite::ModelT output_model; + output_fbm->GetModel()->UnPackTo(&output_model); + + // Extract output metadata + std::map output_metadata; + for (const auto& metadata : output_model.metadata) { + const auto& data = output_model.buffers[metadata->buffer]->data; + output_metadata[metadata->name] = std::string(data.begin(), data.end()); + } + + EXPECT_THAT(output_metadata, + testing::Contains(testing::Pair(expected_key, expected_value))); +} + +} // namespace +} // namespace lite +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/sparsity/testdata/sparse_tensor.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/sparsity/testdata/sparse_tensor.bin new file mode 100644 index 0000000000000000000000000000000000000000..039cef199c50f80908496c6a6135178471bd7254 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/sparsity/testdata/sparse_tensor.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:de10c0ba48fdfeded88bb2206ec108f709fa4a9ae44ba2fd832187635f4529c3 +size 592 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/special_rules.bzl b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/special_rules.bzl new file mode 100644 index 0000000000000000000000000000000000000000..6e80f6e1a5c73e8285932a4cb5319a4f0d3d13f2 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/special_rules.bzl @@ -0,0 +1,21 @@ +"""External versions of build rules that differ outside of Google.""" + +def flex_portable_tensorflow_deps(): + """Returns dependencies for building portable tensorflow in Flex delegate.""" + + return [ + "//third_party/fft2d:fft2d_headers", + "@com_google_absl//absl/log", + "@com_google_absl//absl/log:check", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/strings:str_format", + "@com_google_absl//absl/types:optional", + "@eigen_archive//:eigen3", + "@gemmlowp", + "@icu//:common", + "//third_party/icu/data:conversion_data", + ] + +def tflite_copts_extra(): + """Defines extra compile time flags for tflite_copts(). Currently empty.""" + return [] diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..edcaf7f2858caa636e9a69dc70c21096caf61026 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/BUILD @@ -0,0 +1,916 @@ +load("@llvm-project//mlir:tblgen.bzl", "gentbl_cc_library", "td_library") +load("//tensorflow:tensorflow.bzl", "tf_cc_binary") +load("//tensorflow:tensorflow.default.bzl", "get_compatible_with_portable") +load("//tensorflow/core/platform:rules_cc.bzl", "cc_library") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + default_visibility = [ + "//visibility:public", + ], + licenses = ["notice"], +) + +cc_library( + name = "drop_savedmodel_semantics", + srcs = [ + "transforms/drop_savedmodel_semantics.cc", + ], + hdrs = [ + "transforms/drop_savedmodel_semantics.h", + ], + copts = [ + "-Ithird_party", + ], + deps = [ + ":stablehlo_util", + "//tensorflow/compiler/mlir/tensorflow", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + ], + alwayslink = 1, +) + +cc_library( + name = "stablehlo_util", + srcs = [ + "transforms/stablehlo_util.cc", + ], + hdrs = [ + "transforms/stablehlo_util.h", + ], + copts = [ + "-Ithird_party", + ], + deps = [ + "@llvm-project//llvm:Support", + ], + alwayslink = 1, +) + +cc_library( + name = "hlo_matchers", + srcs = [ + "transforms/hlo_matchers.cc", + ], + hdrs = [ + "transforms/hlo_matchers.h", + ], + copts = [ + "-Ithird_party", + ], + deps = [ + "@llvm-project//llvm:Support", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@llvm-project//mlir:Transforms", + "@xla//xla/mlir_hlo", + ], +) + +cc_library( + name = "smuggle_disallowed_ops", + srcs = [ + "transforms/smuggle_disallowed_ops.cc", + ], + hdrs = [ + "transforms/smuggle_disallowed_ops.h", + ], + copts = [ + "-Ithird_party", + ], + deps = [ + ":stablehlo_util", + "//tensorflow/compiler/mlir/tensorflow", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:TransformUtils", + "@llvm-project//mlir:Transforms", + "@stablehlo//:stablehlo_ops", + ], + alwayslink = 1, +) + +cc_library( + name = "tfl_stablehlo", + srcs = [ + "transforms/tfl_stablehlo_pass.cc", + ], + hdrs = [ + "transforms/tfl_stablehlo_pass.h", + ], + copts = [ + "-Ithird_party", + ], + deps = [ + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/tensorflow:dynamic_shape_utils", + "@flatbuffers", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@stablehlo//:register", + "@stablehlo//:stablehlo_ops", + ], + alwayslink = 1, +) + +cc_library( + name = "transforms", + srcs = [ + "transforms/transforms.cc", + ], + hdrs = [ + "transforms/transforms.h", + ], + copts = [ + "-Ithird_party", + ], + deps = [ + ":drop_savedmodel_semantics", + ":legalize_stablehlo_custom_call_to_composite", + ":legalize_tf_xla_call_module_to_stablehlo_pass", + ":optimize", + ":smuggle_disallowed_ops", + ":stablehlo_fuse_convolution_pass", + ":stablehlo_unfuse_batch_norm_pass", + ":unfold_splat_constant_pass", + "//tensorflow/compiler/mlir/quantization/stablehlo:bridge_passes", + "//tensorflow/compiler/mlir/stablehlo:fold_broadcast_pass", + "//tensorflow/compiler/mlir/stablehlo:fuse_convolution_pass", + "//tensorflow/compiler/mlir/stablehlo:rename_entrypoint_to_main", + "//tensorflow/compiler/mlir/stablehlo:tf_stablehlo", + "//tensorflow/compiler/mlir/stablehlo:unfuse_batch_norm_pass", + "//tensorflow/compiler/mlir/tensorflow/transforms:tensorflow_passes", + "//tensorflow/compiler/mlir/tensorflow/transforms:tf_saved_model_passes", + "//tensorflow/compiler/mlir/tf2xla/transforms:tf_xla_passes", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Transforms", + "@xla//xla/mlir_hlo:mhlo_passes", + ], +) + +cc_library( + name = "check_accepted_ops_pass", + srcs = [ + "transforms/check_accepted_ops_pass.cc", + ], + hdrs = [ + "transforms/check_accepted_ops_pass.h", + ], + copts = [ + "-Ithird_party", + ], + deps = [ + ":stablehlo_util", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Transforms", + ], +) + +cc_library( + name = "op_stat_pass", + srcs = [ + "transforms/op_stat_pass.cc", + ], + hdrs = [ + "transforms/op_stat_pass.h", + ], + copts = [ + "-Ithird_party", + ], + deps = [ + ":stablehlo_util", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/log", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/strings:str_format", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:Transforms", + ], +) + +gentbl_cc_library( + name = "passes_inc_gen", + compatible_with = get_compatible_with_portable(), + tbl_outs = {"transforms/stablehlo_passes.h.inc": [ + "-gen-pass-decls", + "-name=OdmlStablehlo", + ]}, + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "transforms/stablehlo_passes.td", + deps = ["@llvm-project//mlir:PassBaseTdFiles"], +) + +cc_library( + name = "compose_uniform_quantized_type_pass", + srcs = ["transforms/compose_uniform_quantized_type_pass.cc"], + hdrs = ["transforms/stablehlo_passes.h"], + copts = ["-Ithird_party"], + deps = [ + ":passes_inc_gen", + "//tensorflow/compiler/mlir/quantization/common:attrs_and_constraints", + "//tensorflow/compiler/mlir/quantization/common:uniform_quantized_types", + "@com_google_absl//absl/algorithm:container", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@stablehlo//:stablehlo_ops", + ], + alwayslink = 1, +) + +cc_library( + name = "stablehlo_unfuse_batch_norm_pass", + srcs = [ + "transforms/stablehlo_unfuse_batch_norm_pass.cc", + ], + hdrs = [ + "transforms/stablehlo_passes.h", + ], + copts = [ + "-Ithird_party", + ], + deps = [ + ":passes_inc_gen", + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_ops", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:ShapeDialect", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@stablehlo//:stablehlo_ops", + ], + alwayslink = 1, +) + +cc_library( + name = "stablehlo_fuse_convolution_pass", + srcs = [ + "transforms/stablehlo_fuse_convolution_pass.cc", + ], + hdrs = [ + "transforms/stablehlo_passes.h", + ], + copts = [ + "-Ithird_party", + ], + deps = [ + ":passes_inc_gen", + "//tensorflow/compiler/mlir/lite:validators", + "//tensorflow/compiler/mlir/quantization/common:attrs_and_constraints", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:Dialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:ShapeDialect", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@stablehlo//:stablehlo_ops", + ], + alwayslink = 1, +) + +cc_library( + name = "unfold_splat_constant_pass", + srcs = [ + "transforms/unfold_splat_constant_pass.cc", + ], + hdrs = [ + "transforms/stablehlo_passes.h", + ], + copts = [ + "-Ithird_party", + ], + deps = [ + ":passes_inc_gen", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:Transforms", + "@xla//xla/mlir_hlo", + ], + alwayslink = 1, +) + +cc_library( + name = "legalize_tf_xla_call_module_to_stablehlo_pass", + srcs = [ + "transforms/legalize_tf_xla_call_module_to_stablehlo_pass.cc", + ], + hdrs = [ + "transforms/legalize_tf_xla_call_module_to_stablehlo_pass.h", + ], + copts = [ + "-Ithird_party", + ], + deps = [ + "//tensorflow/compiler/mlir/tensorflow", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/strings:string_view", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:ShapeDialect", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@llvm-project//mlir:Transforms", + "@stablehlo//:stablehlo_ops", + "@stablehlo//:stablehlo_serialization", + "@stablehlo//:vhlo_ops", + ], + alwayslink = 1, +) + +cc_library( + name = "legalize_stablehlo_composite_to_tfl_custom", + srcs = [ + "transforms/legalize_stablehlo_composite_to_tfl_custom.cc", + ], + hdrs = [ + "transforms/stablehlo_passes.h", + "transforms/stablehlo_passes.h.inc", + ], + copts = [ + "-Ithird_party", + ], + deps = [ + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "@flatbuffers", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@stablehlo//:stablehlo_ops", + ], + alwayslink = 1, +) + +cc_library( + name = "legalize_stablehlo_to_vhlo_pass", + srcs = [ + "transforms/legalize_stablehlo_to_vhlo.cc", + ], + hdrs = [ + "transforms/stablehlo_passes.h", + "transforms/stablehlo_passes.h.inc", + ], + copts = [ + "-Ithird_party", + ], + deps = [ + ":uniform_quantized_stablehlo_to_tfl_pass", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/lite/core:macros", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:InferTypeOpInterface", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:ReconcileUnrealizedCasts", + "@llvm-project//mlir:ShapeDialect", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TensorDialect", + "@llvm-project//mlir:TransformUtils", + "@llvm-project//mlir:Transforms", + "@stablehlo//:base", + "@stablehlo//:stablehlo_ops", + "@stablehlo//:stablehlo_ops_inc_gen", + "@stablehlo//:stablehlo_pass_inc_gen", + "@stablehlo//:stablehlo_passes", + "@stablehlo//:stablehlo_portable_api", + "@stablehlo//:stablehlo_type_inference", + "@stablehlo//:version", + "@stablehlo//:vhlo_ops", + "@stablehlo//:vhlo_types", + ], + alwayslink = 1, +) + +cc_library( + name = "legalize_stablehlo_custom_call_to_composite", + srcs = [ + "transforms/legalize_stablehlo_custom_call_to_composite.cc", + ], + hdrs = [ + "transforms/stablehlo_passes.h", + "transforms/stablehlo_passes.h.inc", + ], + copts = [ + "-Ithird_party", + ], + deps = [ + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@llvm-project//mlir:Transforms", + "@stablehlo//:stablehlo_ops", + ], + alwayslink = 1, +) + +cc_library( + name = "optimize", + srcs = [ + "transforms/optimize.cc", + ], + hdrs = [ + "transforms/stablehlo_passes.h", + ], + copts = [ + "-Ithird_party", + ], + deps = [ + ":passes_inc_gen", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@xla//xla/mlir_hlo", + ], + alwayslink = 1, +) + +cc_library( + name = "uniform_quantized_stablehlo_to_tfl_pass", + srcs = ["transforms/uniform_quantized_stablehlo_to_tfl_pass.cc"], + hdrs = [ + "transforms/stablehlo_passes.h", + "transforms/stablehlo_passes.h.inc", + ], + copts = ["-Ithird_party"], + deps = [ + ":passes_inc_gen", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/quantization/common:attrs_and_constraints", + "//tensorflow/compiler/mlir/quantization/common:uniform_quantized_types", + "@com_google_absl//absl/algorithm:container", + "@com_google_absl//absl/log:check", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@stablehlo//:base", + "@stablehlo//:stablehlo_ops", + ], + alwayslink = 1, +) + +gentbl_cc_library( + name = "hlo_legalize_tf_inc_gen", + compatible_with = get_compatible_with_portable(), + tbl_outs = {"transforms/generated_legalize_hlo.inc": ["-gen-rewriters"]}, + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "transforms/legalize_hlo_patterns.td", + deps = [ + "//tensorflow/compiler/mlir/tensorflow:tensorflow_ops_td_files", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncTdFiles", + "@xla//xla/mlir_hlo:hlo_ops_td_files", + ], +) + +gentbl_cc_library( + name = "hlo_legalize_tflite_inc_gen", + compatible_with = get_compatible_with_portable(), + tbl_outs = {"transforms/generated_tflite_legalize_hlo.inc": ["-gen-rewriters"]}, + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "transforms/tflite_legalize_hlo_patterns.td", + deps = [ + "//tensorflow/compiler/mlir/lite:tensorflow_lite_ops_td_files", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncTdFiles", + "@xla//xla/mlir_hlo:hlo_ops_td_files", + ], +) + +cc_library( + name = "tfl_legalize_hlo", + srcs = [ + "transforms/tflite_legalize_hlo.cc", + ], + hdrs = [ + "transforms/stablehlo_passes.h", + ], + copts = [ + "-Ithird_party", + ], + deps = [ + ":hlo_legalize_tflite_inc_gen", + ":passes_inc_gen", + ":unfold_splat_constant_pass", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions:case", + "//tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions:conv", + "//tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions:custom_call", + "//tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions:dot_general", + "//tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions:fft", + "//tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions:gather", + "//tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions:get_dimension_size", + "//tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions:if", + "//tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions:iota", + "//tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions:pad", + "//tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions:reduce", + "//tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions:reduce_window", + "//tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions:slice", + "//tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions:sort", + "//tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions:util", + "//tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions:while", + "//tensorflow/compiler/mlir/tensorflow", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@xla//xla/mlir_hlo", + ], + alwayslink = True, +) + +gentbl_cc_library( + name = "prepare_hlo_inc_gen", + compatible_with = get_compatible_with_portable(), + tbl_outs = {"transforms/generated_prepare_hlo.inc": ["-gen-rewriters"]}, + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "transforms/prepare_hlo.td", + deps = [ + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncTdFiles", + "@xla//xla/mlir_hlo:hlo_ops_td_files", + ], +) + +cc_library( + name = "prepare_hlo", + srcs = [ + "transforms/prepare_hlo.cc", + ], + hdrs = [ + "transforms/stablehlo_passes.h", + ], + copts = [ + "-Ithird_party", + ], + deps = [ + ":passes_inc_gen", + ":prepare_hlo_inc_gen", + "//tensorflow/compiler/mlir/lite:tensorflow_lite_ops", + "//tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions:conv", + "//tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions:conv_util", + "//tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions:custom_call", + "//tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions:fft", + "//tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions:pad_util", + "//tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions:reduce_window", + "//tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions:slice", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@xla//xla/mlir_hlo", + ], + alwayslink = True, +) + +cc_library( + name = "tfl_legalize_chlo", + srcs = [ + "transforms/tfl_legalize_chlo.cc", + ], + hdrs = ["transforms/stablehlo_passes.h"], + compatible_with = get_compatible_with_portable(), + copts = [ + "-Ithird_party", + ], + deps = [ + ":passes_inc_gen", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions:gelu", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@stablehlo//:chlo_ops", + "@stablehlo//:stablehlo_ops", + ], + alwayslink = 1, +) + +cc_library( + name = "tf_legalize_hlo", + srcs = [ + "transforms/generated_legalize_hlo.inc", + "transforms/legalize_hlo.cc", + ], + hdrs = [ + "transforms/stablehlo_passes.h", + "transforms/stablehlo_passes.h.inc", + ], + copts = [ + "-Ithird_party", + ], + deps = [ + ":hlo_legalize_tf_inc_gen", + ":hlo_matchers", + ":passes_inc_gen", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions:reduce", + "//tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions:util", + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/compiler/mlir/tensorflow:dynamic_shape_utils", + "//tensorflow/compiler/mlir/tensorflow/transforms:lower_tf_lib", + "//tensorflow/compiler/mlir/tensorflow/transforms:tf_device_pass_inc_gen", + "//tensorflow/core:framework", + "//tensorflow/core:lib", + "@com_google_absl//absl/status", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:Analysis", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:Dialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@llvm-project//mlir:Transforms", + "@stablehlo//:broadcast_utils", + "@stablehlo//:chlo_ops", + "@xla//xla/mlir_hlo", + ], +) + +td_library( + name = "composite_td_files", + srcs = [ + "transforms/composite_avg_pool_patterns.td", + "transforms/composite_utils.td", + ], + compatible_with = get_compatible_with_portable(), + deps = [ + "//tensorflow/compiler/mlir/lite:tensorflow_lite_ops_td_files", + "@llvm-project//mlir:FuncTdFiles", + "@xla//xla/mlir_hlo:hlo_ops_td_files", + ], +) + +cc_library( + name = "composite_utils", + srcs = ["transforms/composite_utils.cc"], + hdrs = ["transforms/composite_utils.h"], + deps = [ + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@xla//xla/mlir_hlo", + ], +) + +cc_library( + name = "lift_callsite_loc_caller", + srcs = ["transforms/torch/lift_callsite_loc_caller_pass.cc"], + copts = ["-Ithird_party"], + deps = [ + ":passes_inc_gen", + ":prepare_hlo", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + ], + alwayslink = True, +) + +cc_library( + name = "legalize_qdq_custom_call", + srcs = ["transforms/torch/legalize_qdq_custom_call_pass.cc"], + copts = ["-Ithird_party"], + deps = [ + ":passes_inc_gen", + ":prepare_hlo", + "@com_google_absl//absl/log", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:AsmParser", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:Support", + "@stablehlo//:stablehlo_ops", + ], + alwayslink = True, +) + +cc_library( + name = "build_stablehlo_composite", + srcs = ["transforms/torch/build_stablehlo_composite_pass.cc"], + copts = ["-Ithird_party"], + deps = [ + ":passes_inc_gen", + ":prepare_hlo", + "@com_google_absl//absl/strings", + "@jsoncpp_git//:jsoncpp", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:Analysis", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + "@stablehlo//:stablehlo_ops", + ], + alwayslink = True, +) + +cc_library( + name = "composite_lowering", + srcs = [ + "transforms/composite_avg_pool.cc", + "transforms/composite_lowering_pass.cc", + ], + hdrs = [ + "transforms/composite_avg_pool.h", + "transforms/stablehlo_passes.h", + ], + copts = [ + "-Ithird_party", + ], + deps = [ + ":composite_lowering_inc_gen", + ":composite_utils", + ":passes_inc_gen", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@xla//xla/mlir_hlo", + ], + alwayslink = True, +) + +cc_library( + name = "optimize_layout", + srcs = [ + "transforms/optimize_layout.cc", + ], + hdrs = ["transforms/stablehlo_passes.h"], + compatible_with = get_compatible_with_portable(), + deps = [ + ":passes_inc_gen", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:DialectUtils", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@stablehlo//:stablehlo_ops", + ], + alwayslink = 1, +) + +gentbl_cc_library( + name = "composite_lowering_inc_gen", + compatible_with = get_compatible_with_portable(), + tbl_outs = {"transforms/generated_composite_lowering.inc": ["-gen-rewriters"]}, + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "transforms/composite_lowering_patterns.td", + deps = [ + ":composite_td_files", + ":composite_utils", + "//tensorflow/compiler/mlir/lite:tensorflow_lite_ops_td_files", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncTdFiles", + "@xla//xla/mlir_hlo:hlo_ops_td_files", + ], +) + +tf_cc_binary( + name = "odml_to_stablehlo", + srcs = [ + "odml_to_stablehlo.cc", + ], + copts = ["-O3"], + deprecation = "odml_to_stablehlo is being deprecated, please use TFlite converter with flag: " + + "converter.target_spec.supported_ops = " + + " [tf.lite.OpsSet.EXPERIMENTAL_STABLEHLO_OPS]", + deps = [ + ":check_accepted_ops_pass", + ":op_stat_pass", + ":stablehlo_util", + ":transforms", + "//tensorflow/cc/saved_model:loader", + "//tensorflow/compiler/mlir:init_mlir", + "//tensorflow/compiler/mlir:passes", + "//tensorflow/compiler/mlir/lite:tf_to_tfl_flatbuffer", + "//tensorflow/compiler/mlir/quantization/tensorflow:quantize_preprocess", + "//tensorflow/compiler/mlir/quantization/tensorflow:tf_quant_ops", + "//tensorflow/compiler/mlir/stablehlo:legalize_tf", + "//tensorflow/compiler/mlir/tensorflow", + "//tensorflow/compiler/mlir/tensorflow:mlir_roundtrip_flags", + "//tensorflow/compiler/mlir/tensorflow/transforms:tensorflow_passes", + "//tensorflow/compiler/mlir/tensorflow/transforms:tf_graph_optimization_pass", + "//tensorflow/compiler/mlir/tf2xla:compile_mlir_util", + "//tensorflow/compiler/mlir/tf2xla/transforms:xla_legalize_tf", + "//tensorflow/core:core_cpu_base", + "//tensorflow/core:lib", + "//tensorflow/core/ir/types:Dialect", + "@com_google_absl//absl/log", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/types:span", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:AllPassesAndDialects", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Parser", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:QuantOps", + "@llvm-project//mlir:RegisterAllDialects", # buildcleaner: keep + "@llvm-project//mlir:RegisterAllPasses", # buildcleaner: keep + "@llvm-project//mlir:Support", + "@stablehlo//:register", + "@xla//xla/mlir_hlo:all_passes", + "@xla//xla/mlir_hlo:hlo_dialect_registration", + ], +) + +tf_cc_binary( + name = "odml-to-stablehlo-opt", + testonly = True, + tags = ["hostonly"], + deps = [ + ":compose_uniform_quantized_type_pass", + ":legalize_stablehlo_composite_to_tfl_custom", + ":legalize_stablehlo_custom_call_to_composite", + ":legalize_stablehlo_to_vhlo_pass", + ":legalize_tf_xla_call_module_to_stablehlo_pass", + ":optimize", + ":passes_inc_gen", + ":prepare_hlo", + ":stablehlo_fuse_convolution_pass", + ":stablehlo_unfuse_batch_norm_pass", + ":tf_legalize_hlo", + ":tfl_legalize_chlo", + ":tfl_legalize_hlo", + ":tfl_stablehlo", + ":uniform_quantized_stablehlo_to_tfl_pass", + "//tensorflow/compiler/mlir:passes", + "//tensorflow/compiler/mlir/lite:litert_mlir_opt_main", + "//tensorflow/compiler/mlir/stablehlo:fold_broadcast_pass", + "//tensorflow/compiler/mlir/stablehlo:fuse_convolution_pass", + "//tensorflow/compiler/mlir/stablehlo:tf_stablehlo", + "//tensorflow/compiler/mlir/stablehlo:unfuse_batch_norm_pass", + ], +) + +exports_files([ + "run_lit.sh", +]) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_converter/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_converter/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..3a2357cd27c7dbd1549b131f5887827f801be4ca --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_converter/BUILD @@ -0,0 +1,97 @@ +load("@llvm-project//mlir:tblgen.bzl", "gentbl_cc_library") +load("@rules_cc//cc:cc_library.bzl", "cc_library") +load("//tensorflow:tensorflow.bzl", "tf_cc_binary") +load("//tensorflow:tensorflow.default.bzl", "get_compatible_with_portable") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + default_visibility = [ + ":friends", + ], +) + +package_group( + name = "friends", + packages = [ + "//tensorflow/compiler/mlir/lite/...", + ], +) + +tf_cc_binary( + name = "odml-converter", + srcs = ["odml_converter_main.cc"], + compatible_with = get_compatible_with_portable(), + visibility = [ + "//tensorflow/compiler/mlir/lite/stablehlo/odml_converter:__subpackages__", + "//third_party/odml/infra:__subpackages__", + ], # Prototype phase. + deps = [ + ":shlo_simplify", + "//tensorflow/compiler/mlir:init_mlir", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/lite/stablehlo:legalize_stablehlo_to_vhlo_pass", + "//tensorflow/compiler/mlir/tensorflow:tensorflow_types", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:MlirOptLib", + "@llvm-project//mlir:Support", + "@stablehlo//:chlo_ops", + "@stablehlo//:stablehlo_ops", + ], +) + +cc_library( + name = "shlo_simplify", + srcs = [ + "transforms/shlo_simplify.cc", + ], + hdrs = ["passes.h"], + compatible_with = get_compatible_with_portable(), + deps = [ + ":folders", + ":passes_inc_gen", + ":shlo_simplify_inc_gen", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@stablehlo//:stablehlo_ops", + ], + alwayslink = 1, +) + +gentbl_cc_library( + name = "shlo_simplify_inc_gen", + compatible_with = get_compatible_with_portable(), + tbl_outs = {"transforms/generated_shlo_simplify.inc": ["-gen-rewriters"]}, + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "transforms/shlo_simplify.td", + deps = ["@stablehlo//:stablehlo_ops_td_files"], +) + +cc_library( + name = "folders", + srcs = ["folders.cc"], + hdrs = ["folders.h"], + compatible_with = get_compatible_with_portable(), + deps = [ + "@llvm-project//llvm:Support", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@stablehlo//:stablehlo_ops", + ], +) + +gentbl_cc_library( + name = "passes_inc_gen", + compatible_with = get_compatible_with_portable(), + tbl_outs = {"passes.h.inc": [ + "-gen-pass-decls", + "-name=ODMLConverter", + ]}, + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "passes.td", + deps = ["@llvm-project//mlir:PassBaseTdFiles"], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_converter/folders.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_converter/folders.cc new file mode 100644 index 0000000000000000000000000000000000000000..606a8223a7851d48c4e12158e8e7c81035df4f60 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_converter/folders.cc @@ -0,0 +1,129 @@ +/* Copyright 2024 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 "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/Support/Casting.h" +#include "mlir/IR/BuiltinAttributeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "stablehlo/dialect/StablehloOps.h" // from @stablehlo + +namespace mlir::odml { + +namespace { + +// Helper class for parsing operands to a foldable operation. +class FoldAdaptor { + public: + // Returns std::nullopt if the operation cannot be folded. + static std::optional Create(Operation* operation) { + auto foldable_opr = [](Value val) -> bool { + return !llvm::isa(val) && + llvm::isa(val.getDefiningOp()); + }; + if (!llvm::all_of(operation->getOperands(), foldable_opr)) { + return std::nullopt; + } + return FoldAdaptor(operation); + } + + // Gets a list of ElementsAttr behind each constant operand. + llvm::SmallVector OperandData() { + llvm::SmallVector res; + res.reserve(operation_->getNumOperands()); + for (auto opr : operation_->getOperands()) { + auto op = llvm::dyn_cast(opr.getDefiningOp()); + res.push_back(op.getValue()); + } + return res; + } + + // Gets a pointer to the operation to be folded. + Operation* Op() { return operation_; } + + private: + explicit FoldAdaptor(Operation* operation) : operation_(operation) {} + Operation* const operation_; +}; + +// APSInt provides operators which APInt does not, so allow for converting +// to APSInt for computation. Only APInts can be directly read from +// element attributes. +static const APFloat& AddSign(const APFloat& v) { return v; } +static APSInt AddSign(const APInt& v) { return APSInt(v); } + +template +static LogicalResult FoldDivOpInternal(stablehlo::DivOp op, + PatternRewriter& rewriter) { + auto adaptor = FoldAdaptor::Create(op); + if (!adaptor.has_value()) { + return failure(); + } + auto const_oprs = adaptor.value().OperandData(); + + const bool lhs_splat = const_oprs[0].isSplat(); + const bool rhs_splat = const_oprs[1].isSplat(); + + auto lhs_vals = const_oprs[0].getValues(); + auto rhs_vals = const_oprs[1].getValues(); + const auto num_results = std::max(lhs_vals.size(), rhs_vals.size()); + std::vector res; + res.reserve(num_results); + + auto lhs_start = lhs_vals.begin(); + auto rhs_start = rhs_vals.begin(); + + for (int i = 0; i < num_results; ++i) { + auto lhs_val = lhs_splat ? *lhs_start : *(lhs_start++); + auto rhs_val = rhs_splat ? *rhs_start : *(rhs_start++); + auto signed_lhs_val = AddSign(lhs_val); + auto signed_rhs_val = AddSign(rhs_val); + if (signed_rhs_val.isZero()) { + return failure(); + } + res.push_back(signed_lhs_val / signed_rhs_val); + } + + auto res_attr = DenseElementsAttr::get( + mlir::cast(const_oprs[0].getType()), res); + rewriter.replaceOpWithNewOp(adaptor.value().Op(), + res_attr); + return success(); +} + +static LogicalResult FoldDivOp(stablehlo::DivOp op, PatternRewriter& rewriter) { + auto etype = op.getType().getElementType(); + if (mlir::isa(etype)) { + return FoldDivOpInternal(op, rewriter); + } + if (mlir::isa(etype)) { + return FoldDivOpInternal(op, rewriter); + } + return failure(); +} +} // namespace + +void PopulateFolderPatterns(RewritePatternSet& patternSet) { + patternSet.add(FoldDivOp); +} + +} // namespace mlir::odml diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_converter/folders.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_converter/folders.h new file mode 100644 index 0000000000000000000000000000000000000000..05b0a02f72f73ec3214489acf0d12185f7edfb6c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_converter/folders.h @@ -0,0 +1,26 @@ +/* Copyright 2024 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_ODML_CONVERTER_FOLDERS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_ODML_CONVERTER_FOLDERS_H_ + +namespace mlir::odml { + +// Populates the pattern set with all folding patterns. These patterns +// are intended to have precedence over any other patterns added to the set. +void PopulateFolderPatterns(RewritePatternSet &patternSet); + +} // namespace mlir::odml + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_ODML_CONVERTER_FOLDERS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_converter/odml_converter_main.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_converter/odml_converter_main.cc new file mode 100644 index 0000000000000000000000000000000000000000..958c13be7cb21a4472dc2ea9301f45f1800214c2 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_converter/odml_converter_main.cc @@ -0,0 +1,50 @@ +/* Copyright 2023 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 "llvm/Support/raw_ostream.h" +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Tools/mlir-opt/MlirOptMain.h" // from @llvm-project +#include "stablehlo/dialect/ChloOps.h" // from @stablehlo +#include "stablehlo/dialect/StablehloOps.h" // from @stablehlo +#include "tensorflow/compiler/mlir/init_mlir.h" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/odml_converter/passes.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_dialect.h" + +const char* art = R"( + ___ ___ __ __ _ ___ _ + / _ \| \| \/ | | / __|___ _ ___ _____ _ _| |_ ___ _ _ +| (_) | |) | |\/| | |__ | (__/ _ \ ' \ V / -_) '_| _/ -_) '_| + \___/|___/|_| |_|____| \___\___/_||_\_/\___|_| \__\___|_| +)"; + +int main(int argc, char* argv[]) { + tensorflow::InitMlir y(&argc, &argv); + llvm::errs() << art << "\n"; + + mlir::odml::registerODMLConverterPasses(); + mlir::odml::registerLegalizeStablehloToVhloPass(); + + mlir::DialectRegistry registry; + registry.insert(); + + return failed( + mlir::MlirOptMain(argc, argv, "ODML Converter Driver\n", registry)); +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_converter/passes.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_converter/passes.h new file mode 100644 index 0000000000000000000000000000000000000000..6ff075d631e9902c4521e2c7fd425131aa71063e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_converter/passes.h @@ -0,0 +1,34 @@ +/* Copyright 2023 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_ODML_CONVERTER_PASSES_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_ODML_CONVERTER_PASSES_H_ + +#include + +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project + +namespace mlir::odml { + +std::unique_ptr> CreateSHLOSimplifyPass(); + +#define GEN_PASS_REGISTRATION +#include "tensorflow/compiler/mlir/lite/stablehlo/odml_converter/passes.h.inc" + +} // namespace mlir::odml + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_ODML_CONVERTER_PASSES_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_converter/passes.td b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_converter/passes.td new file mode 100644 index 0000000000000000000000000000000000000000..aa403468b4cfdf60c02795a0bbe2d89d0004be79 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_converter/passes.td @@ -0,0 +1,28 @@ +/* Copyright 2024 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 "mlir/Pass/PassBase.td" + +def SHLOSimplifyPass: Pass<"shlo-simplify", "ModuleOp"> { + let summary = "Apply internal canonicalizations and foldings."; + let description = [{ + Applies various internally defined patterns. + }]; + + let options = [ + + ]; + let constructor = "CreateSHLOSimplifyPass()"; +} \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_converter/tests/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_converter/tests/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..a8a2e50176a067f63ae00706cd7f9b26ce6ffddf --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_converter/tests/BUILD @@ -0,0 +1,26 @@ +load("//tensorflow:tensorflow.default.bzl", "filegroup") +load("//tensorflow/compiler/mlir:glob_lit_test.bzl", "glob_lit_tests") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + default_visibility = ["//tensorflow/compiler/mlir/lite/stablehlo/odml_converter:__subpackages__"], +) + +glob_lit_tests( + name = "filecheck_tests", + data = [":test_utilities"], + driver = "@llvm-project//mlir:run_lit.sh", + test_file_exts = [ + "mlir", + ], +) + +filegroup( + name = "test_utilities", + testonly = True, + data = [ + "//tensorflow/compiler/mlir/lite/stablehlo/odml_converter:odml-converter", + "@llvm-project//llvm:FileCheck", + "@llvm-project//mlir:run_lit.sh", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_converter/tests/shlo_simplify.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_converter/tests/shlo_simplify.mlir new file mode 100644 index 0000000000000000000000000000000000000000..254a8b744302c4366090443b9b00660d1d4038b2 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_converter/tests/shlo_simplify.mlir @@ -0,0 +1,96 @@ +// RUN: odml-converter --shlo-simplify %s -split-input-file | FileCheck %s + +func.func @foldDiv() -> tensor<2xf32> { + %0 = stablehlo.constant dense<[2.0, 3.0]> : tensor<2xf32> + %1 = stablehlo.constant dense<[4.0, 6.0]> : tensor<2xf32> + %2 = stablehlo.divide %0, %1 : tensor<2xf32> + return %2 : tensor<2xf32> +} + +// CHECK-LABEL: foldDiv +// CHECK: stablehlo.constant dense<5.000000e-01> : tensor<2xf32> + +// ----- + +func.func @foldDivLHSSplat() -> tensor<2xf32> { + %0 = stablehlo.constant dense<2.0> : tensor<2xf32> + %1 = stablehlo.constant dense<[4.0, 6.0]> : tensor<2xf32> + %2 = stablehlo.divide %0, %1 : tensor<2xf32> + return %2 : tensor<2xf32> +} + +// CHECK-LABEL: foldDivLHSSplat +// CHECK: stablehlo.constant dense<[5.000000e-01, 0.333333343]> : tensor<2xf32> + +// ----- + +func.func @foldDivRHSSplat() -> tensor<2xf32> { + %0 = stablehlo.constant dense<[4.0, 6.0]> : tensor<2xf32> + %1 = stablehlo.constant dense<2.0> : tensor<2xf32> + %2 = stablehlo.divide %0, %1 : tensor<2xf32> + return %2 : tensor<2xf32> +} + +// CHECK-LABEL: foldDivRHSSplat +// CHECK: stablehlo.constant dense<[2.000000e+00, 3.000000e+00]> : tensor<2xf32> + +// ----- + +func.func @foldDivBothSplat() -> tensor<2xf32> { + %0 = stablehlo.constant dense<4.0> : tensor<2xf32> + %1 = stablehlo.constant dense<2.0> : tensor<2xf32> + %2 = stablehlo.divide %0, %1 : tensor<2xf32> + return %2 : tensor<2xf32> +} + +// CHECK-LABEL: foldDivBothSplat +// CHECK: stablehlo.constant dense<2.000000e+00> : tensor<2xf32> + +// ----- + +func.func @foldDivF64() -> tensor<2xf64> { + %0 = stablehlo.constant dense<[2.0, 3.0]> : tensor<2xf64> + %1 = stablehlo.constant dense<[4.0, 6.0]> : tensor<2xf64> + %2 = stablehlo.divide %0, %1 : tensor<2xf64> + return %2 : tensor<2xf64> +} + +// CHECK-LABEL: foldDivF64 +// CHECK: stablehlo.constant dense<5.000000e-01> : tensor<2xf64> + +// ----- + +func.func @foldDivI32() -> tensor<2xi32> { + %0 = stablehlo.constant dense<[9, 3]> : tensor<2xi32> + %1 = stablehlo.constant dense<[4, 6]> : tensor<2xi32> + %2 = stablehlo.divide %0, %1 : tensor<2xi32> + return %2 : tensor<2xi32> +} + +// CHECK-LABEL: foldDivI32 +// CHECK: stablehlo.constant dense<[2, 0]> : tensor<2xi32> + +// ----- + +func.func @divideToMulReciprocalSplat(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = stablehlo.constant dense<2.0> : tensor<2xf32> + %2 = stablehlo.divide %arg0, %0 : tensor<2xf32> + return %2 : tensor<2xf32> +} + +// CHECK-LABEL: divideToMulReciprocalSplat +// CHECK: stablehlo.constant dense<5.000000e-01> : tensor<2xf32> +// CHECK: stablehlo.multiply + +// ----- + +func.func @divideToMulReciprocal(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = stablehlo.constant dense<[2.0, 3.0]> : tensor<2xf32> + %2 = stablehlo.divide %arg0, %0 : tensor<2xf32> + return %2 : tensor<2xf32> +} + +// CHECK-LABEL: divideToMulReciprocal +// CHECK: stablehlo.constant dense<[5.000000e-01, 0.333333343]> : tensor<2xf32> +// CHECK: stablehlo.multiply + diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_converter/transforms/shlo_simplify.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_converter/transforms/shlo_simplify.cc new file mode 100644 index 0000000000000000000000000000000000000000..c6c5744fc5d0423ab1ef2d2347ac20e5323db68d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_converter/transforms/shlo_simplify.cc @@ -0,0 +1,60 @@ +/* Copyright 2024 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 "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Support/TypeID.h" // from @llvm-project +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" // from @llvm-project +#include "stablehlo/dialect/StablehloOps.h" // from @stablehlo // IWYU pragma: keep +#include "tensorflow/compiler/mlir/lite/stablehlo/odml_converter/folders.h" + +namespace mlir { +namespace odml { +namespace { + +#define GEN_PASS_DEF_SHLOSIMPLIFYPASS +#include "tensorflow/compiler/mlir/lite/stablehlo/odml_converter/passes.h.inc" +#include "tensorflow/compiler/mlir/lite/stablehlo/odml_converter/transforms/generated_shlo_simplify.inc" + +// Performs misc odml "cleanup" on shlo dialect. This is a functional standin +// for canonicalization and folding which is not offered directly by the +// shlo implementation. +class SHLOSimplifyPass : public impl::SHLOSimplifyPassBase { + public: + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(SHLOSimplifyPass) + + void runOnOperation() override { + ModuleOp module = getOperation(); + RewritePatternSet patterns(&getContext()); + populateWithGenerated(patterns); + PopulateFolderPatterns(patterns); + if (failed(applyPatternsGreedily(module, std::move(patterns)))) { + signalPassFailure(); + } + } +}; + +} // namespace + +std::unique_ptr> CreateSHLOSimplifyPass() { + return std::make_unique(); +} + +} // namespace odml +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_converter/transforms/shlo_simplify.td b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_converter/transforms/shlo_simplify.td new file mode 100644 index 0000000000000000000000000000000000000000..21d6e7c0f62a87e1300f4bf515d51ef7d6d9d42a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_converter/transforms/shlo_simplify.td @@ -0,0 +1,38 @@ +/* Copyright 2024 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 "stablehlo/dialect/StablehloOps.td" +include "mlir/IR/OpBase.td" +include "mlir/IR/BuiltinAttributes.td" +include "mlir/IR/CommonAttrConstraints.td" +include "mlir/IR/CommonTypeConstraints.td" + +def CloneF32ElementsAttrWithOnes + : NativeCodeCall<"DenseElementsAttr::get(llvm::cast($0.getType()), (float)1.0)">; + +def NotConstant : Constraint< + CPred<"llvm::isa($0) || !llvm::isa($0.getDefiningOp())">, + "Is not a constant.">; + +def : Pat<(StableHLO_DivOp $l, + (StableHLO_ConstantOp:$divisor FloatElementsAttr<32>:$cst)), + (StableHLO_MulOp $l, + (StableHLO_DivOp + (StableHLO_ConstantOp (CloneF32ElementsAttrWithOnes $cst)), + $divisor)), + [(NotConstant $l)]>; + + + + diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_to_stablehlo.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_to_stablehlo.cc new file mode 100644 index 0000000000000000000000000000000000000000..13b2e4d904fc3a1797418c4af89930731ad780fa --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/odml_to_stablehlo.cc @@ -0,0 +1,389 @@ +/* Copyright 2021 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 +#include +#include +#include + +#include "absl/log/log.h" +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/match.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/str_split.h" +#include "absl/types/span.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/Path.h" +#include "llvm/Support/SourceMgr.h" +#include "llvm/Support/ToolOutputFile.h" +#include "llvm/Support/raw_ostream.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Dialect/Quant/IR/Quant.h" // from @llvm-project +#include "mlir/IR/AsmState.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/Diagnostics.h" // from @llvm-project +#include "mlir/IR/Location.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/OwningOpRef.h" // from @llvm-project +#include "mlir/InitAllDialects.h" // from @llvm-project +#include "mlir/InitAllPasses.h" // from @llvm-project +#include "mlir/Parser/Parser.h" // from @llvm-project +#include "mlir/Pass/PassManager.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/FileUtilities.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "stablehlo/dialect/Register.h" // from @stablehlo +#include "tensorflow/cc/saved_model/loader.h" +#include "tensorflow/compiler/mlir/init_mlir.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/check_accepted_ops_pass.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/op_stat_pass.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_util.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/transforms.h" +#include "tensorflow/compiler/mlir/lite/tf_to_tfl_flatbuffer.h" +#include "tensorflow/compiler/mlir/quantization/tensorflow/passes/tf_quant_ops.h" +#include "tensorflow/compiler/mlir/quantization/tensorflow/quantize_preprocess.h" +#include "tensorflow/compiler/mlir/stablehlo/transforms/legalize_tf_passes.h" +#include "tensorflow/compiler/mlir/tensorflow/dialect_registration.h" +#include "tensorflow/compiler/mlir/tensorflow/transforms/passes.h" +#include "tensorflow/compiler/mlir/tensorflow/transforms/tf_graph_optimization_pass.h" +#include "tensorflow/compiler/mlir/tensorflow/translate/mlir_roundtrip_flags.h" +#include "tensorflow/compiler/mlir/tf2xla/api/v1/compile_mlir_util.h" +#include "tensorflow/compiler/mlir/tf2xla/transforms/passes.h" +#include "xla/mlir_hlo/mhlo/IR/register.h" +#include "xla/mlir_hlo/mhlo/transforms/passes.h" +#include "xla/tsl/platform/errors.h" +#include "xla/tsl/platform/statusor.h" +#include "tensorflow/core/ir/types/dialect.h" +#include "tensorflow/core/platform/errors.h" +#include "tensorflow/core/platform/status.h" +#include "tensorflow/core/public/session.h" + +// Tool which lowers TensorFlow Graphs to StableHLO graphs. +// +// This tool is used by the ODML Programmability effort to consume input TF +// graphs, and lower them to StableHLO graphs for use by downstream tools. +// +// Input: TF Saved Model or TF MLIR files. +// Output: StableHLO MLIR or TFLite Flatbuffer. +// +// Usage: +// 1) convert the input model to StableHLO MLIR +// odml_to_stablehlo /path/to/model -o=model.mlir +// 2) convert the input model to TFLite Flatbuffer +// odml_to_stablehlo /path/to/model -o=model.tflite --export-type=tflite +// +// TODO(pulkitb): Add more options. +// * Verbose - dump intermediate translations. +// * Choose specific signatures/functions from a saved model. +// * Options for full/partial conversion, Op exceptions list. +// * Option to serialize output to TFL flatbuffer format. + +using llvm::cl::opt; + +// NOLINTNEXTLINE +opt input_model(llvm::cl::Positional, + llvm::cl::desc(""), + llvm::cl::Required); + +// NOLINTNEXTLINE +opt output_path("o", llvm::cl::desc(""), + llvm::cl::Required); + +// NOLINTNEXTLINE +opt export_type("export-type", llvm::cl::desc(""), + llvm::cl::Optional, llvm::cl::init("mlir")); + +// NOLINTNEXTLINE +opt verbose( + "v", llvm::cl::desc("Dump intermediate translations to output dir."), + llvm::cl::Optional, llvm::cl::init(false)); + +// NOLINTNEXTLINE +opt elide_large_elements_attrs( + "e", llvm::cl::desc("Elide large elements attrs."), llvm::cl::Optional, + llvm::cl::init(false)); + +// NOLINTNEXTLINE +opt debug_info( + "debug-info", llvm::cl::desc("Inclide MLIR debug location info in output."), + llvm::cl::Optional, llvm::cl::init(false)); + +// NOLINTNEXTLINE +opt allow_tf("allow-tf", llvm::cl::desc("Allow TF dialect."), + llvm::cl::Optional, llvm::cl::init(false)); + +// NOLINTNEXTLINE +opt skip_checks("skip-checks", + llvm::cl::desc("Skip checking for disallowed ops."), + llvm::cl::Optional, llvm::cl::init(false)); + +// NOLINTNEXTLINE +opt skip_resize( + "skip-resize", + llvm::cl::desc( + "Skip converting tf.ResizeBilinear and tf.ResizeNearestNeighbor ops."), + llvm::cl::Optional, llvm::cl::init(true)); + +// NOLINTNEXTLINE +opt smuggle_disallowed_ops( + "smuggle-disallowed-ops", + llvm::cl::desc("Smuggle disallowed ops via stablehlo.custom_calls."), + llvm::cl::Optional, llvm::cl::init(true)); + +// NOLINTNEXTLINE +opt freeze_tf_graph( + "freeze-tf-graph", + llvm::cl::desc("Freeze TF graph to remove tf.ResourceVariable, etc."), + llvm::cl::Optional, llvm::cl::init(false)); + +// NOLINTNEXTLINE +opt exported_model_signatures( + "exported_model_signatures", llvm::cl::desc("model signature names"), + llvm::cl::Optional, llvm::cl::init("serving_default")); + +namespace mlir { +namespace odml { + +absl::StatusOr> ImportSavedModelOrMLIR( + const std::string& input_path, MLIRContext* context, + llvm::SourceMgr* source_mgr, + std::unique_ptr* saved_model_bundle) { + if (absl::EndsWith(input_path, ".mlir")) { + auto file_or_err = llvm::MemoryBuffer::getFileOrSTDIN(input_path.c_str()); + if (std::error_code error = file_or_err.getError()) { + return absl::InvalidArgumentError( + absl::StrCat("Could not open input file: ", input_path, + ", message=", error.message())); + } + + // Load the MLIR module. + source_mgr->AddNewSourceBuffer(std::move(*file_or_err), llvm::SMLoc()); + return mlir::parseSourceFile(*source_mgr, context); + } + + // TODO(pulkitb): Remove hard-coded tag. + std::unordered_set tags({"serve"}); + std::vector exported_names_in_vector = + absl::StrSplit(exported_model_signatures, ','); + absl::Span exported_names(exported_names_in_vector); + std::vector custom_opdefs; + + tensorflow::GraphImportConfig specs; + specs.upgrade_legacy = true; + + return ImportSavedModel(input_path, /*saved_model_version=*/1, tags, + absl::MakeSpan(custom_opdefs), exported_names, specs, + /*enable_variable_lifting=*/true, context, + saved_model_bundle); +} + +absl::Status ExportModule(mlir::ModuleOp module, + const std::string& output_filename, + bool elide_large_elements_attrs) { + std::string error_msg; + auto output = mlir::openOutputFile(output_filename, &error_msg); + if (output == nullptr) { + llvm::errs() << error_msg << '\n'; + return absl::AbortedError("Unable to write to output path."); + } + + // Export StableHLO MLIR as output + std::string result; + llvm::raw_string_ostream os(result); + OpPrintingFlags printing_flags; + if (debug_info) { + printing_flags.enableDebugInfo(); + } + if (elide_large_elements_attrs) { + printing_flags.elideLargeElementsAttrs(); + } + module.print(os, printing_flags); + os.flush(); + + output->os() << result; + output->keep(); + + return absl::OkStatus(); +} + +absl::Status ConvertTFToStableHLO(ModuleOp tf_module, + const PassPipelineCLParser& pass_pipeline) { + PassManager pm(tf_module.getContext()); + if (failed(applyPassManagerCLOptions(pm))) { + return absl::AbortedError("Failed to apply MLIR pass manager CL options."); + } + + auto error_handler = [&](const Twine& msg) { + emitError(UnknownLoc::get(pm.getContext())) << msg; + return failure(); + }; + if (failed(pass_pipeline.addToPipeline(pm, error_handler))) { + return absl::AbortedError("Failed to add passes to pipeline."); + } + + AddTFToStablehloPasses(pm, skip_resize, smuggle_disallowed_ops); + + if (!skip_checks) { + std::vector optional_accepted_dialects; + if (allow_tf) { + // For the normal case, this should be always FALSE. + // This `allow_tf` logic should be removed after the backend supports tf. + optional_accepted_dialects.push_back("tf"); + } + pm.addPass( + mlir::odml::createCheckAcceptedOpsPass(optional_accepted_dialects)); + } + + mlir::odml::AddStablehloOptimizationPasses(pm); + + if (verbose) { + // Print out a detailed report of non-converted stats. + // Because this pass aborts the pass if there are unconverted ops, + // we need to locate createPrintOpStatsPass after all optimization. + pm.addPass( + mlir::odml::createPrintOpStatsPass(GetAcceptedStableHLODialects())); + } + + if (failed(pm.run(tf_module))) { + return absl::AbortedError("Lowering to StableHLO failed."); + } + + return absl::OkStatus(); +} + +absl::Status RunConverter(const PassPipelineCLParser& pass_pipeline) { + DialectRegistry registry; + registerAllDialects(registry); + RegisterAllTensorFlowDialects(registry); + mhlo::registerAllMhloDialects(registry); + stablehlo::registerAllDialects(registry); + registry.insert(); + mlir::quant::RegisterOps(); + + MLIRContext context(registry); + context.loadAllAvailableDialects(); + + llvm::SourceMgr source_mgr; + SourceMgrDiagnosticHandler sourceMgrHandler(source_mgr, &context); + + auto verbose_dir = llvm::sys::path::parent_path(output_path).str(); + + const char* type_set[] = {"tflite", "mlir"}; + bool found = std::find(std::begin(type_set), std::end(type_set), + export_type) != std::end(type_set); + if (!found) { + return absl::AbortedError("Export type is not supported."); + } + + auto bundle = std::make_unique(); + bundle = nullptr; + + TF_ASSIGN_OR_RETURN( + auto module, + ImportSavedModelOrMLIR(input_model, &context, &source_mgr, &bundle)); + if (verbose) { + TF_RETURN_IF_ERROR(ExportModule(*module, + absl::StrCat(verbose_dir, "/debug_tf.mlir"), + elide_large_elements_attrs)); + } + + std::optional session = std::nullopt; + if (bundle) session = bundle->GetSession(); // NOMUTANTS--it should pass. + + if (freeze_tf_graph) { + if (absl::EndsWith(input_model, ".mlir")) { + LOG(INFO) << "file type is mlir. ignoring freezing TF graph"; + } else { + LOG(INFO) << "--freeze-tf-graph is on. Freezing TF graph..."; + const auto status = tensorflow::quantization::PreprocessAndFreezeGraph( + *module, module->getContext(), session); + if (!status.ok()) { + return absl::AbortedError("Failed to preprocess & freeze TF graph"); + } + } + } + + auto conversion_status = ConvertTFToStableHLO(*module, pass_pipeline); + auto output_export_status = + ExportModule(*module, output_path, elide_large_elements_attrs); + if (!conversion_status.ok()) { + LOG(ERROR) << "TF to StableHLO conversion failed: " + << conversion_status.message(); + + auto debug_export_status = ExportModule( + *module, absl::StrCat(verbose_dir, "/debug_stablehlo.mlir"), + elide_large_elements_attrs); + if (!debug_export_status.ok()) { + LOG(ERROR) << "Failed to export debug_stablehlo.mlir: " + << debug_export_status.message(); + } + + return conversion_status; + } + return output_export_status; +} + +// All MLIR and TF passes are registered here, similar to mlirOptMain. +// +// This is done so users of the binary have the opportunity to experiment with +// the pipeline from the command line by adding additional passes when +// necessary. +// +// Once the pipeline stabilizes, and most models convert successfully this may +// be removed so only pipeline passes run. +void initAllPasses() { + mlir::registerPassManagerCLOptions(); + mlir::registerAsmPrinterCLOptions(); + mlir::registerAllPasses(); + mlir::registerTensorFlowPasses(); + mlir::mhlo::registerAllMhloPasses(); + // These are in compiler/mlir/tf2xla and not part of the above MHLO passes. + mlir::mhlo::registerTfXlaPasses(); + mlir::mhlo::registerLegalizeTFPass(); + tensorflow::RegisterConvertMlirToXlaHloPipelineWithDefaults(); + tensorflow::RegisterGraphOptimizationPasses(); +} + +} // namespace odml +} // namespace mlir + +int main(int argc, char* argv[]) { + tensorflow::InitMlir y(&argc, &argv); + LOG(INFO) << "odml_to_stablehlo is being deprecated, please use " + "TFlite converter with flag: " + "converter.target_spec.supported_ops = " + "[tf.lite.OpsSet.EXPERIMENTAL_STABLEHLO_OPS] "; + + mlir::odml::initAllPasses(); + mlir::PassPipelineCLParser passPipeline("", "Add available compiler passes."); + llvm::cl::ParseCommandLineOptions(argc, argv, "ODML StableHLO Bridge.\n"); + + auto status = mlir::odml::RunConverter(passPipeline); + + if (!status.ok()) { + LOG(ERROR) << status; + return status.raw_code(); + } + return 0; +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..6f9cec9f7acc1506c1870e9cd0f975b24fdf275c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/BUILD @@ -0,0 +1,37 @@ +load("//tensorflow:tensorflow.default.bzl", "filegroup") +load("//tensorflow/compiler/mlir:glob_lit_test.bzl", "glob_lit_tests") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + default_visibility = [ + "//visibility:private", + ], + licenses = ["notice"], +) + +glob_lit_tests( + name = "all_tests", + data = [":test_utilities"], + driver = "//tensorflow/compiler/mlir/lite/stablehlo:run_lit.sh", + size_override = { + "legalize-skip-quantization-ops.mlir": "medium", + }, + test_file_exts = [ + "mlir", + "cc", + ], +) + +# Bundle together all of the test utilities that are used by tests. +filegroup( + name = "test_utilities", + testonly = True, + data = [ + "//tensorflow/compiler/mlir/lite:flatbuffer_translate", + "//tensorflow/compiler/mlir/lite:tf_tfl_translate", + "//tensorflow/compiler/mlir/lite/stablehlo:odml-to-stablehlo-opt", + "//tensorflow/compiler/mlir/lite/stablehlo:odml_to_stablehlo", + "@llvm-project//llvm:FileCheck", + "@llvm-project//mlir:run_lit.sh", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/call_xla_module_to_stablehlo.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/call_xla_module_to_stablehlo.mlir new file mode 100644 index 0000000000000000000000000000000000000000..8c6174c5551ae6f3bc80a001134b5929358473c0 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/call_xla_module_to_stablehlo.mlir @@ -0,0 +1,27 @@ +//RUN: tf_tfl_translate --enable-stablehlo-conversion --input-mlir %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s + + +module attributes {tf.versions = {bad_consumers = [], min_consumer = 0 : i32, producer = 1660 : i32}} { + func.func @main(%arg0: tensor<2x3xi32>) -> tensor<2x3xi32> attributes {tf.entry_function = {control_outputs = "", inputs = "args_tf_0", outputs = "Identity"}} { + %0 = tf_executor.graph { + %outputs, %control = tf_executor.island wraps "tf.Identity"(%arg0) {device = ""} : (tensor<2x3xi32>) -> tensor<2x3xi32> + %outputs_0, %control_1 = tf_executor.island wraps "tf.XlaSharding"(%outputs) {_XlaSharding = "", device = "", sharding = "", unspecified_dims = []} : (tensor<2x3xi32>) -> tensor<2x3xi32> + %outputs_2, %control_3 = tf_executor.island wraps "tf.XlaCallModule"(%outputs_0) {Sout = [#tf_type.shape<2x3>], device = "", dim_args_spec = [], disabled_checks = [], function_list = [], has_token_input_output = false, module = "ML\EFR\01StableHLO_v0.9.0\00\01\17\05\01\03\01\03\05\03\07\07\09\0B\03]?\0B\01)\07\0F\0B+\0B\0F\0B\0B\0B3\0B\0B\0B\0B\0F\0B\0F\0B\13\0B\03\17\0F\13\0B\0B\0B\0F\13\0B\0B\0B\0B\01\05\0B\0F\03\07\17\17\07\02\D7\1F\11\03\05\05\0D\03\09\09\0B\0D\03\0F\03\05\11\05\0F\11\01\00\05\11\05\13\05\15\03\0B\15)\171\193\05;\1B=\05\17\05\19\05\1B\05\1D\1D\1F\01\05\1F\1D#%\05!\17'\A9\01\05#\03\03+\0D\03-/\1D%\1D'#\07\03\035\0D\0379\1D)\1D+\1D-\1D/\01\09\01\02\02)\05\09\0D\09\11\03\05\03\05\1B\04C\05\01\11\01\07\07\03\01\05\03\11\01\13\07\03\05\0B\03\05\1D\05\06!\03\05\05\01\01\07\04\01\03\03\06\03\01\05\01\00f\051\0F\0B\03!\1B\1D[;\05\1F\15\1D\15\1D%)9\13\15\19\11\0F\0B\11builtin\00vhlo\00module\00func_v1\00multiply_v1\00return_v1\00sym_name\00jax.uses_shape_polymorphism\00mhlo.num_partitions\00mhlo.num_replicas\00jit_jax_model\00arg_attrs\00function_type\00res_attrs\00sym_visibility\00x\00jit(jax_model)/jit(main)/mul\00experimental/users/ypang/lite/convert_ulm.py\00mhlo.sharding\00{replicated}\00jax.result_info\00\00main\00public\00", platforms = ["CPU"], version = 8 : i64} : (tensor<2x3xi32>) -> tensor<2x3xi32> + %control_4 = tf_executor.island(%control_3) wraps "tf.NoOp"() {device = ""} : () -> () + %outputs_5, %control_6 = tf_executor.island wraps "tf.PreventGradient"(%outputs_2) {device = "", message = "The jax2tf-converted function does not support gradients. Use `with_gradient` parameter to enable gradients"} : (tensor<2x3xi32>) -> tensor<2x3xi32> + %outputs_7, %control_8 = tf_executor.island wraps "tf.Identity"(%outputs_5) {device = ""} : (tensor<2x3xi32>) -> tensor<2x3xi32> + %outputs_9, %control_10 = tf_executor.island(%control_4) wraps "tf.Identity"(%outputs_7) {device = ""} : (tensor<2x3xi32>) -> tensor<2x3xi32> + tf_executor.fetch %outputs_9 : tensor<2x3xi32> + } + return %0 : tensor<2x3xi32> + } +} + +// CHECK: module attributes +// CHECK-SAME: tfl.metadata = {{{.*}}keep_stablehlo_constant = "true"{{.*}}} +// CHECK-NEXT: func.func @main(%arg0: tensor<2x3xi32>) -> tensor<2x3xi32> attributes {tf.entry_function = {inputs = "args_tf_0", outputs = "Identity"}} { +// CHECK-NEXT: %0 = stablehlo.custom_call @Sharding(%arg0) {mhlo.sharding = ""} : (tensor<2x3xi32>) -> tensor<2x3xi32> +// CHECK-NEXT: %1 = stablehlo.multiply %0, %0 : tensor<2x3xi32> +// CHECK-NEXT: return %1 : tensor<2x3xi32> +// CHECK-NEXT: } +// CHECK-NEXT: } \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/compose-uniform-quantized-type.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/compose-uniform-quantized-type.mlir new file mode 100644 index 0000000000000000000000000000000000000000..787b92cd51df6341c2b7f1118fbf592c9bc03d6c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/compose-uniform-quantized-type.mlir @@ -0,0 +1,542 @@ +// RUN: odml-to-stablehlo-opt --compose-uniform-quantized-type \ +// RUN: --split-input-file --verify-diagnostics %s | FileCheck %s + +module { +// CHECK-LABEL: quantized_conv_op +// CHECK-SAME: %[[ARG:.*]]: tensor<1x3x3x4xf32> + func.func @quantized_conv_op(%arg0: tensor<1x3x3x4xf32>) -> tensor<1x3x3x4xf32> { + %1 = stablehlo.constant dense<1.000000e+03> : tensor<1x1x1x1xf32> // Input inverse scale. + %2 = stablehlo.constant dense<-128> : tensor<1x1x1x1xi8> // Input zero point. + %3 = stablehlo.constant dense<1> : tensor<3x3x4x4xi8> // Quantized filter tensor. + %4 = stablehlo.constant dense<3.000000e+03> : tensor<1x1x1x4xf32> + %5 = stablehlo.constant dense<4.000000e+03> : tensor<1x1x1x1xf32> // Output inverse scale. + %6 = stablehlo.constant dense<127> : tensor<1x1x1x1xi8> // Output zero point. + %7 = call @uniform_quantize_0(%arg0, %1, %2) : (tensor<1x3x3x4xf32>, tensor<1x1x1x1xf32>, tensor<1x1x1x1xi8>) -> tensor<1x3x3x4xi8> + %8 = stablehlo.convert %7 : (tensor<1x3x3x4xi8>) -> tensor<1x3x3x4xf32> + %9 = stablehlo.convert %3 : (tensor<3x3x4x4xi8>) -> tensor<3x3x4x4xf32> + %10 = stablehlo.convolution(%8, %9) dim_numbers = [b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f], window = {pad = [[1, 1], [1, 1]]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64} : (tensor<1x3x3x4xf32>, tensor<3x3x4x4xf32>) -> tensor<1x3x3x4xf32> + %11 = stablehlo.reshape %2 : (tensor<1x1x1x1xi8>) -> tensor<1xi8> + %12 = stablehlo.broadcast_in_dim %11, dims = [0] : (tensor<1xi8>) -> tensor<1x3x3x4xi8> + %13 = stablehlo.convert %12 : (tensor<1x3x3x4xi8>) -> tensor<1x3x3x4xf32> + %14 = stablehlo.convert %3 : (tensor<3x3x4x4xi8>) -> tensor<3x3x4x4xf32> + %15 = stablehlo.convolution(%13, %14) dim_numbers = [b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f], window = {pad = [[1, 1], [1, 1]]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64} : (tensor<1x3x3x4xf32>, tensor<3x3x4x4xf32>) -> tensor<1x3x3x4xf32> + %16 = stablehlo.subtract %10, %15 : tensor<1x3x3x4xf32> + %17 = stablehlo.broadcast_in_dim %4, dims = [0, 1, 2, 3] : (tensor<1x1x1x4xf32>) -> tensor<1x3x3x4xf32> + %18 = stablehlo.multiply %16, %17 : tensor<1x3x3x4xf32> + %19 = call @uniform_quantize_1(%18, %5, %6) : (tensor<1x3x3x4xf32>, tensor<1x1x1x1xf32>, tensor<1x1x1x1xi8>) -> tensor<1x3x3x4xi8> + %20 = call @uniform_dequantize_0(%19, %5, %6) : (tensor<1x3x3x4xi8>, tensor<1x1x1x1xf32>, tensor<1x1x1x1xi8>) -> tensor<1x3x3x4xf32> + return %20 : tensor<1x3x3x4xf32> + } +// CHECK: %[[FILTER:.*]] = stablehlo.constant() <{value = dense<1> : tensor<3x3x4x4xi8>}> : () -> tensor<3x3x4x4x!quant.uniform> +// CHECK: %[[QUANT_ARG:.*]] = stablehlo.uniform_quantize %[[ARG]] : (tensor<1x3x3x4xf32>) -> tensor<1x3x3x4x!quant.uniform> +// CHECK: %[[CONV:.*]] = stablehlo.convolution(%[[QUANT_ARG]], %[[FILTER]]) {{.*}} : (tensor<1x3x3x4x!quant.uniform>, tensor<3x3x4x4x!quant.uniform>) -> tensor<1x3x3x4x!quant.uniform> +// CHECK: %[[DEQUANT:.*]] = stablehlo.uniform_dequantize %[[CONV]] : (tensor<1x3x3x4x!quant.uniform>) -> tensor<1x3x3x4xf32> +// CHECK: return %[[DEQUANT]] : tensor<1x3x3x4xf32> + + // The following uniform_quantize & uniform_dequantize functions do NOT have + // the correct body. Only the type signatures matter for testing. + func.func private @uniform_quantize_0(%arg0: tensor<1x3x3x4xf32>, %arg1: tensor<1x1x1x1xf32>, %arg2: tensor<1x1x1x1xi8>) -> tensor<1x3x3x4xi8> { + %0 = stablehlo.convert %arg0 : (tensor<1x3x3x4xf32>) -> tensor<1x3x3x4xi8> + return %0 : tensor<1x3x3x4xi8> + } +// CHECK: @uniform_quantize_0 + func.func private @uniform_quantize_1(%arg0: tensor<1x3x3x4xf32>, %arg1: tensor<1x1x1x1xf32>, %arg2: tensor<1x1x1x1xi8>) -> tensor<1x3x3x4xi8> { + %0 = stablehlo.convert %arg0 : (tensor<1x3x3x4xf32>) -> tensor<1x3x3x4xi8> + return %0 : tensor<1x3x3x4xi8> + } +// CHECK: @uniform_quantize_1 + func.func private @uniform_dequantize_0(%arg0: tensor<1x3x3x4xi8>, %arg1: tensor<1x1x1x1xf32>, %arg2: tensor<1x1x1x1xi8>) -> tensor<1x3x3x4xf32> { + %0 = stablehlo.convert %arg0 : (tensor<1x3x3x4xi8>) -> tensor<1x3x3x4xf32> + return %0 : tensor<1x3x3x4xf32> + } +// CHECK: @uniform_dequantize_0 +} + +// ----- + +// Tests a variant where there is no stablehlo.convert op in between the +// filter constant and the convolution op. +// +// `filter (f32) -> convolution` +// +// instead of: +// +// `filter (i8) -> convert (i8 -> f32) -> convolution` + +module { +// CHECK-LABEL: quantized_conv_op_with_no_filter_convert +// CHECK-SAME: %[[ARG:.*]]: tensor<1x3x3x4xf32> + func.func @quantized_conv_op_with_no_filter_convert(%arg0: tensor<1x3x3x4xf32>) -> tensor<1x3x3x4xf32> { + %1 = stablehlo.constant dense<1.000000e+03> : tensor<1x1x1x1xf32> // Input inverse scale. + %2 = stablehlo.constant dense<-128> : tensor<1x1x1x1xi8> // Input zero point. + %3 = stablehlo.constant dense<2.000000e+01> : tensor<3x3x4x4xf32> // Quantized filter tensor. + %4 = stablehlo.constant dense<3.000000e+03> : tensor<1x1x1x4xf32> + %5 = stablehlo.constant dense<4.000000e+03> : tensor<1x1x1x1xf32> // Output inverse scale. + %6 = stablehlo.constant dense<127> : tensor<1x1x1x1xi8> // Output zero point. + %7 = call @uniform_quantize_0(%arg0, %1, %2) : (tensor<1x3x3x4xf32>, tensor<1x1x1x1xf32>, tensor<1x1x1x1xi8>) -> tensor<1x3x3x4xi8> + %8 = stablehlo.convert %7 : (tensor<1x3x3x4xi8>) -> tensor<1x3x3x4xf32> + %9 = stablehlo.convolution(%8, %3) dim_numbers = [b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f], window = {pad = [[1, 1], [1, 1]]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64} : (tensor<1x3x3x4xf32>, tensor<3x3x4x4xf32>) -> tensor<1x3x3x4xf32> + %10 = stablehlo.reshape %2 : (tensor<1x1x1x1xi8>) -> tensor<1xi8> + %11 = stablehlo.broadcast_in_dim %10, dims = [0] : (tensor<1xi8>) -> tensor<1x3x3x4xi8> + %12 = stablehlo.convert %11 : (tensor<1x3x3x4xi8>) -> tensor<1x3x3x4xf32> + %13 = stablehlo.convolution(%12, %3) dim_numbers = [b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f], window = {pad = [[1, 1], [1, 1]]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64} : (tensor<1x3x3x4xf32>, tensor<3x3x4x4xf32>) -> tensor<1x3x3x4xf32> + %14 = stablehlo.subtract %9, %13 : tensor<1x3x3x4xf32> + %15 = stablehlo.broadcast_in_dim %4, dims = [0, 1, 2, 3] : (tensor<1x1x1x4xf32>) -> tensor<1x3x3x4xf32> + %16 = stablehlo.multiply %14, %15 : tensor<1x3x3x4xf32> + %17 = call @uniform_quantize_1(%16, %5, %6) : (tensor<1x3x3x4xf32>, tensor<1x1x1x1xf32>, tensor<1x1x1x1xi8>) -> tensor<1x3x3x4xi8> + %18 = call @uniform_dequantize_0(%17, %5, %6) : (tensor<1x3x3x4xi8>, tensor<1x1x1x1xf32>, tensor<1x1x1x1xi8>) -> tensor<1x3x3x4xf32> + return %18 : tensor<1x3x3x4xf32> + } +// CHECK: %[[FILTER:.*]] = stablehlo.constant() <{value = dense<20> : tensor<3x3x4x4xi8>}> : () -> tensor<3x3x4x4x!quant.uniform> +// CHECK: %[[QUANT_ARG:.*]] = stablehlo.uniform_quantize %[[ARG]] : (tensor<1x3x3x4xf32>) -> tensor<1x3x3x4x!quant.uniform> +// CHECK: %[[CONV:.*]] = stablehlo.convolution(%[[QUANT_ARG]], %[[FILTER]]) {{.*}} : (tensor<1x3x3x4x!quant.uniform>, tensor<3x3x4x4x!quant.uniform>) -> tensor<1x3x3x4x!quant.uniform> +// CHECK: %[[DEQUANT:.*]] = stablehlo.uniform_dequantize %[[CONV]] : (tensor<1x3x3x4x!quant.uniform>) -> tensor<1x3x3x4xf32> +// CHECK: return %[[DEQUANT]] : tensor<1x3x3x4xf32> + + // The following uniform_quantize & uniform_dequantize functions do NOT have + // the correct body. Only the type signatures matter for testing. + func.func private @uniform_quantize_0(%arg0: tensor<1x3x3x4xf32>, %arg1: tensor<1x1x1x1xf32>, %arg2: tensor<1x1x1x1xi8>) -> tensor<1x3x3x4xi8> { + %0 = stablehlo.convert %arg0 : (tensor<1x3x3x4xf32>) -> tensor<1x3x3x4xi8> + return %0 : tensor<1x3x3x4xi8> + } +// CHECK: @uniform_quantize_0 + func.func private @uniform_quantize_1(%arg0: tensor<1x3x3x4xf32>, %arg1: tensor<1x1x1x1xf32>, %arg2: tensor<1x1x1x1xi8>) -> tensor<1x3x3x4xi8> { + %0 = stablehlo.convert %arg0 : (tensor<1x3x3x4xf32>) -> tensor<1x3x3x4xi8> + return %0 : tensor<1x3x3x4xi8> + } +// CHECK: @uniform_quantize_1 + func.func private @uniform_dequantize_0(%arg0: tensor<1x3x3x4xi8>, %arg1: tensor<1x1x1x1xf32>, %arg2: tensor<1x1x1x1xi8>) -> tensor<1x3x3x4xf32> { + %0 = stablehlo.convert %arg0 : (tensor<1x3x3x4xi8>) -> tensor<1x3x3x4xf32> + return %0 : tensor<1x3x3x4xf32> + } +// CHECK: @uniform_dequantize_0 +} + +// ----- + +// The pattern should not match when there are no `uniform_quantize` call +// for the input. + +module { +// CHECK-LABEL: conv_no_input_uniform_quantize_call + func.func @conv_no_input_uniform_quantize_call(%arg0: tensor<1x3x3x4xf32>) -> tensor<1x3x3x4xf32> { + %1 = stablehlo.constant dense<1.000000e+03> : tensor<1x1x1x1xf32> // Input inverse scale. + %2 = stablehlo.constant dense<-128> : tensor<1x1x1x1xi8> // Input zero point. + %3 = stablehlo.constant dense<2.000000e+01> : tensor<3x3x4x4xf32> // Quantized filter tensor. + %4 = stablehlo.constant dense<3.000000e+03> : tensor<1x1x1x4xf32> + %5 = stablehlo.constant dense<4.000000e+03> : tensor<1x1x1x1xf32> // Output inverse scale. + %6 = stablehlo.constant dense<127> : tensor<1x1x1x1xi8> // Output zero point. + %10 = stablehlo.convolution(%arg0, %3) dim_numbers = [b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f], window = {pad = [[1, 1], [1, 1]]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64} : (tensor<1x3x3x4xf32>, tensor<3x3x4x4xf32>) -> tensor<1x3x3x4xf32> + %11 = stablehlo.reshape %2 : (tensor<1x1x1x1xi8>) -> tensor<1xi8> + %12 = stablehlo.broadcast_in_dim %11, dims = [0] : (tensor<1xi8>) -> tensor<1x3x3x4xi8> + %13 = stablehlo.convert %12 : (tensor<1x3x3x4xi8>) -> tensor<1x3x3x4xf32> + %15 = stablehlo.convolution(%13, %3) dim_numbers = [b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f], window = {pad = [[1, 1], [1, 1]]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64} : (tensor<1x3x3x4xf32>, tensor<3x3x4x4xf32>) -> tensor<1x3x3x4xf32> + %16 = stablehlo.subtract %10, %15 : tensor<1x3x3x4xf32> + %17 = stablehlo.broadcast_in_dim %4, dims = [0, 1, 2, 3] : (tensor<1x1x1x4xf32>) -> tensor<1x3x3x4xf32> + %18 = stablehlo.multiply %16, %17 : tensor<1x3x3x4xf32> + %19 = call @uniform_quantize_1(%18, %5, %6) : (tensor<1x3x3x4xf32>, tensor<1x1x1x1xf32>, tensor<1x1x1x1xi8>) -> tensor<1x3x3x4xi8> + %20 = call @uniform_dequantize_0(%19, %5, %6) : (tensor<1x3x3x4xi8>, tensor<1x1x1x1xf32>, tensor<1x1x1x1xi8>) -> tensor<1x3x3x4xf32> + return %20 : tensor<1x3x3x4xf32> + } +// CHECK-NOT: stablehlo.uniform_quantize +// CHECK-NOT: stablehlo.uniform_dequantize + + // The following uniform_quantize & uniform_dequantize functions do NOT have + // the correct body. Only the type signatures matter for testing. + func.func private @uniform_quantize_1(%arg0: tensor<1x3x3x4xf32>, %arg1: tensor<1x1x1x1xf32>, %arg2: tensor<1x1x1x1xi8>) -> tensor<1x3x3x4xi8> { + %0 = stablehlo.convert %arg0 : (tensor<1x3x3x4xf32>) -> tensor<1x3x3x4xi8> + return %0 : tensor<1x3x3x4xi8> + } +// CHECK: @uniform_quantize_1 + func.func private @uniform_dequantize_0(%arg0: tensor<1x3x3x4xi8>, %arg1: tensor<1x1x1x1xf32>, %arg2: tensor<1x1x1x1xi8>) -> tensor<1x3x3x4xf32> { + %0 = stablehlo.convert %arg0 : (tensor<1x3x3x4xi8>) -> tensor<1x3x3x4xf32> + return %0 : tensor<1x3x3x4xf32> + } +// CHECK: @uniform_dequantize_0 +} + +// ----- + +module { +// CHECK-LABEL: quantized_dot_general +// CHECK-SAME: %[[ARG:.*]]: tensor<1x4x2xf32> + func.func @quantized_dot_general(%arg0: tensor<1x4x2xf32>) -> tensor<1x4x3xf32> { + %0 = stablehlo.constant dense<3.000000e+00> : tensor<1x1x1xf32> // Input inverse scale. + %1 = stablehlo.constant dense<1> : tensor<1x1x1xi8> // Input zero point. + %2 = stablehlo.constant dense<5> : tensor<2x3xi8> // Quantized filter. + %3 = stablehlo.constant dense<4> : tensor<1x1x3xi32> // Precalculated q2 * z1. + %4 = stablehlo.constant dense<3.000000e+03> : tensor<1x1x3xf32> // Merged scale: s1 * s2. + %5 = stablehlo.constant dense<2.000000e+02> : tensor<1x1x1xf32> // Output inverse scale. + %6 = stablehlo.constant dense<2> : tensor<1x1x1xi8> // Output zero point. + %7 = call @uniform_quantize_0(%arg0, %0, %1) : (tensor<1x4x2xf32>, tensor<1x1x1xf32>, tensor<1x1x1xi8>) -> tensor<1x4x2xi8> + %8 = stablehlo.convert %7 : (tensor<1x4x2xi8>) -> tensor<1x4x2xf32> + %9 = stablehlo.convert %2 : (tensor<2x3xi8>) -> tensor<2x3xf32> + %10 = stablehlo.dot_general %8, %9, contracting_dims = [2] x [0] : (tensor<1x4x2xf32>, tensor<2x3xf32>) -> tensor<1x4x3xf32> + %11 = stablehlo.convert %3 : (tensor<1x1x3xi32>) -> tensor<1x1x3xf32> + %12 = stablehlo.broadcast_in_dim %11, dims = [0, 1, 2] : (tensor<1x1x3xf32>) -> tensor<1x4x3xf32> // Optional + %13 = stablehlo.subtract %10, %12 : tensor<1x4x3xf32> // Precalculated zp_neg. + %14 = stablehlo.broadcast_in_dim %4, dims = [0, 1, 2] : (tensor<1x1x3xf32>) -> tensor<1x4x3xf32> // Optional + %15 = stablehlo.multiply %13, %14 : tensor<1x4x3xf32> // s1 * s2 + %16 = call @uniform_quantize_1(%15, %5, %6) : (tensor<1x4x3xf32>, tensor<1x1x1xf32>, tensor<1x1x1xi8>) -> tensor<1x4x3xi8> + %17 = call @uniform_dequantize_0(%16, %5, %6) : (tensor<1x4x3xi8>, tensor<1x1x1xf32>, tensor<1x1x1xi8>) -> tensor<1x4x3xf32> + return %17 : tensor<1x4x3xf32> + } +// Quantization dimension == 1 because it is the output feature dimension. +// CHECK: %[[FILTER:.*]] = stablehlo.constant() <{value = dense<5> : tensor<2x3xi8>}> : () -> tensor<2x3x!quant.uniform> +// CHECK: %[[QUANT_ARG:.*]] = stablehlo.uniform_quantize %[[ARG]] : (tensor<1x4x2xf32>) -> tensor<1x4x2x!quant.uniform> +// CHECK: %[[CONV:.*]] = stablehlo.dot_general %[[QUANT_ARG]], %[[FILTER]], contracting_dims = [2] x [0] : (tensor<1x4x2x!quant.uniform>, tensor<2x3x!quant.uniform>) -> tensor<1x4x3x!quant.uniform> +// CHECK: %[[DEQUANT:.*]] = stablehlo.uniform_dequantize %[[CONV]] : (tensor<1x4x3x!quant.uniform>) -> tensor<1x4x3xf32> +// CHECK: return %[[DEQUANT]] : tensor<1x4x3xf32> + + // The following uniform_quantize & uniform_dequantize functions do NOT have + // the correct body. Only the type signatures matter for testing. + func.func private @uniform_quantize_0(%arg0: tensor<1x4x2xf32>, %arg1: tensor<1x1x1xf32>, %arg2: tensor<1x1x1xi8>) -> tensor<1x4x2xi8> { + %0 = stablehlo.convert %arg0 : (tensor<1x4x2xf32>) -> tensor<1x4x2xi8> + return %0 : tensor<1x4x2xi8> + } +// CHECK: @uniform_quantize_0 + func.func private @uniform_quantize_1(%arg0: tensor<1x4x3xf32>, %arg1: tensor<1x1x1xf32>, %arg2: tensor<1x1x1xi8>) -> tensor<1x4x3xi8> { + %0 = stablehlo.convert %arg0 : (tensor<1x4x3xf32>) -> tensor<1x4x3xi8> + return %0 : tensor<1x4x3xi8> + } +// CHECK: @uniform_quantize_1 + func.func private @uniform_dequantize_0(%arg0: tensor<1x4x3xi8>, %arg1: tensor<1x1x1xf32>, %arg2: tensor<1x1x1xi8>) -> tensor<1x4x3xf32> { + %0 = stablehlo.convert %arg0 : (tensor<1x4x3xi8>) -> tensor<1x4x3xf32> + return %0 : tensor<1x4x3xf32> + } +// CHECK: @uniform_dequantize_0 +} + +// ----- + +// Tests that when dot_general's filter comes from an f32 constant +// it is cast to i8 after the conversion. + +module { +// CHECK-LABEL: quantized_dot_general_float_filter +// CHECK-SAME: %[[ARG:.*]]: tensor<1x4x2xf32> + func.func @quantized_dot_general_float_filter(%arg0: tensor<1x4x2xf32>) -> tensor<1x4x3xf32> { + %0 = stablehlo.constant dense<3.000000e+00> : tensor<1x1x1xf32> // Input inverse scale. + %1 = stablehlo.constant dense<1> : tensor<1x1x1xi8> // Input zero point. + // Filter, disguised as f32 but the values are actually i8. + %2 = stablehlo.constant dense<5.000000e+00> : tensor<2x3xf32> + %3 = stablehlo.constant dense<4> : tensor<1x1x3xi32> // Precalculated q2 * z1. + %4 = stablehlo.constant dense<3.000000e+03> : tensor<1x1x3xf32> // Merged scale: s1 * s2. + %5 = stablehlo.constant dense<2.000000e+02> : tensor<1x1x1xf32> // Output inverse scale. + %6 = stablehlo.constant dense<2> : tensor<1x1x1xi8> // Output zero point. + %7 = call @uniform_quantize_0(%arg0, %0, %1) : (tensor<1x4x2xf32>, tensor<1x1x1xf32>, tensor<1x1x1xi8>) -> tensor<1x4x2xi8> + %8 = stablehlo.convert %7 : (tensor<1x4x2xi8>) -> tensor<1x4x2xf32> + %9 = stablehlo.dot_general %8, %2, contracting_dims = [2] x [0] : (tensor<1x4x2xf32>, tensor<2x3xf32>) -> tensor<1x4x3xf32> + %10 = stablehlo.convert %3 : (tensor<1x1x3xi32>) -> tensor<1x1x3xf32> + %11 = stablehlo.broadcast_in_dim %10, dims = [0, 1, 2] : (tensor<1x1x3xf32>) -> tensor<1x4x3xf32> // Optional + %12 = stablehlo.subtract %9, %11 : tensor<1x4x3xf32> // Precalculated zp_neg. + %13 = stablehlo.broadcast_in_dim %4, dims = [0, 1, 2] : (tensor<1x1x3xf32>) -> tensor<1x4x3xf32> // Optional + %14 = stablehlo.multiply %12, %13 : tensor<1x4x3xf32> // s1 * s2 + %15 = call @uniform_quantize_1(%14, %5, %6) : (tensor<1x4x3xf32>, tensor<1x1x1xf32>, tensor<1x1x1xi8>) -> tensor<1x4x3xi8> + %16 = call @uniform_dequantize_0(%15, %5, %6) : (tensor<1x4x3xi8>, tensor<1x1x1xf32>, tensor<1x1x1xi8>) -> tensor<1x4x3xf32> + return %16 : tensor<1x4x3xf32> + } +// Quantization dimension == 1 because it is the output feature dimension. +// Quantized filter values (from f32 constant) are cast to i8. +// CHECK: %[[FILTER:.*]] = stablehlo.constant() <{value = dense<5> : tensor<2x3xi8>}> : () -> tensor<2x3x!quant.uniform> +// CHECK: %[[QUANT_ARG:.*]] = stablehlo.uniform_quantize %[[ARG]] : (tensor<1x4x2xf32>) -> tensor<1x4x2x!quant.uniform> +// CHECK: %[[CONV:.*]] = stablehlo.dot_general %[[QUANT_ARG]], %[[FILTER]], contracting_dims = [2] x [0] : (tensor<1x4x2x!quant.uniform>, tensor<2x3x!quant.uniform>) -> tensor<1x4x3x!quant.uniform> +// CHECK: %[[DEQUANT:.*]] = stablehlo.uniform_dequantize %[[CONV]] : (tensor<1x4x3x!quant.uniform>) -> tensor<1x4x3xf32> +// CHECK: return %[[DEQUANT]] : tensor<1x4x3xf32> + + // The following uniform_quantize & uniform_dequantize functions do NOT have + // the correct body. Only the type signatures matter for testing. + func.func private @uniform_quantize_0(%arg0: tensor<1x4x2xf32>, %arg1: tensor<1x1x1xf32>, %arg2: tensor<1x1x1xi8>) -> tensor<1x4x2xi8> { + %0 = stablehlo.convert %arg0 : (tensor<1x4x2xf32>) -> tensor<1x4x2xi8> + return %0 : tensor<1x4x2xi8> + } +// CHECK: @uniform_quantize_0 + func.func private @uniform_quantize_1(%arg0: tensor<1x4x3xf32>, %arg1: tensor<1x1x1xf32>, %arg2: tensor<1x1x1xi8>) -> tensor<1x4x3xi8> { + %0 = stablehlo.convert %arg0 : (tensor<1x4x3xf32>) -> tensor<1x4x3xi8> + return %0 : tensor<1x4x3xi8> + } +// CHECK: @uniform_quantize_1 + func.func private @uniform_dequantize_0(%arg0: tensor<1x4x3xi8>, %arg1: tensor<1x1x1xf32>, %arg2: tensor<1x1x1xi8>) -> tensor<1x4x3xf32> { + %0 = stablehlo.convert %arg0 : (tensor<1x4x3xi8>) -> tensor<1x4x3xf32> + return %0 : tensor<1x4x3xf32> + } +// CHECK: @uniform_dequantize_0 +} + +// ----- + +// Tests that the conversion is successful even when there are no +// broadcast_in_dim ops for the second arguments of the subtract op and +// multiply op. + +module { +// CHECK-LABEL: quantized_dot_general_no_broadcast +// CHECK-SAME: %[[ARG:.*]]: tensor<1x2xf32> + func.func @quantized_dot_general_no_broadcast(%arg0: tensor<1x2xf32>) -> tensor<1x3xf32> { + %0 = stablehlo.constant dense<3.000000e+00> : tensor<1x1xf32> // Input inverse scale. + %1 = stablehlo.constant dense<1> : tensor<1x1xi8> // Input zero point. + %2 = stablehlo.constant dense<5> : tensor<2x3xi8> // Quantized filter. + %3 = stablehlo.constant dense<4> : tensor<1x3xi32> // Precalculated z1 * q2. + %4 = stablehlo.constant dense<3.000000e+03> : tensor<1x3xf32> // Merged scale: s1 * s2. + %5 = stablehlo.constant dense<2.000000e+02> : tensor<1x1xf32> // Output inverse scale. + %6 = stablehlo.constant dense<2> : tensor<1x1xi8> // Output zero point. + %7 = call @uniform_quantize_0(%arg0, %0, %1) : (tensor<1x2xf32>, tensor<1x1xf32>, tensor<1x1xi8>) -> tensor<1x2xi8> + %8 = stablehlo.convert %7 : (tensor<1x2xi8>) -> tensor<1x2xf32> + %9 = stablehlo.convert %2 : (tensor<2x3xi8>) -> tensor<2x3xf32> + %10 = stablehlo.dot_general %8, %9, contracting_dims = [1] x [0] : (tensor<1x2xf32>, tensor<2x3xf32>) -> tensor<1x3xf32> + %11 = stablehlo.convert %3 : (tensor<1x3xi32>) -> tensor<1x3xf32> + %12 = stablehlo.subtract %10, %11 : tensor<1x3xf32> // q1 * q2 - z1 * q2 + %13 = stablehlo.multiply %12, %4 : tensor<1x3xf32> // s1 * s2 + %14 = call @uniform_quantize_1(%13, %5, %6) : (tensor<1x3xf32>, tensor<1x1xf32>, tensor<1x1xi8>) -> tensor<1x3xi8> + %15 = call @uniform_dequantize_0(%14, %5, %6) : (tensor<1x3xi8>, tensor<1x1xf32>, tensor<1x1xi8>) -> tensor<1x3xf32> + return %15 : tensor<1x3xf32> + } +// Quantization dimension == 1 because it is the output feature dimension. +// CHECK: %[[FILTER:.*]] = stablehlo.constant() <{value = dense<5> : tensor<2x3xi8>}> : () -> tensor<2x3x!quant.uniform> +// CHECK: %[[QUANT_ARG:.*]] = stablehlo.uniform_quantize %[[ARG]] : (tensor<1x2xf32>) -> tensor<1x2x!quant.uniform> +// CHECK: %[[CONV:.*]] = stablehlo.dot_general %[[QUANT_ARG]], %[[FILTER]], contracting_dims = [1] x [0] : (tensor<1x2x!quant.uniform>, tensor<2x3x!quant.uniform>) -> tensor<1x3x!quant.uniform> +// CHECK: %[[DEQUANT:.*]] = stablehlo.uniform_dequantize %[[CONV]] : (tensor<1x3x!quant.uniform>) -> tensor<1x3xf32> +// CHECK: return %[[DEQUANT]] : tensor<1x3xf32> + + // The following uniform_quantize & uniform_dequantize functions do NOT have + // the correct body. Only the type signatures matter for testing. + func.func private @uniform_quantize_0(%arg0: tensor<1x2xf32>, %arg1: tensor<1x1xf32>, %arg2: tensor<1x1xi8>) -> tensor<1x2xi8> { + %0 = stablehlo.convert %arg0 : (tensor<1x2xf32>) -> tensor<1x2xi8> + return %0 : tensor<1x2xi8> + } +// CHECK: @uniform_quantize_0 + func.func private @uniform_quantize_1(%arg0: tensor<1x3xf32>, %arg1: tensor<1x1xf32>, %arg2: tensor<1x1xi8>) -> tensor<1x3xi8> { + %0 = stablehlo.convert %arg0 : (tensor<1x3xf32>) -> tensor<1x3xi8> + return %0 : tensor<1x3xi8> + } +// CHECK: @uniform_quantize_1 + func.func private @uniform_dequantize_0(%arg0: tensor<1x3xi8>, %arg1: tensor<1x1xf32>, %arg2: tensor<1x1xi8>) -> tensor<1x3xf32> { + %0 = stablehlo.convert %arg0 : (tensor<1x3xi8>) -> tensor<1x3xf32> + return %0 : tensor<1x3xf32> + } +// CHECK: @uniform_dequantize_0 +} + +// ----- + +// Tests that the conversion doesn't happen when the uniform_quantize function +// outputs a i32 storage type. + +module { +// CHECK-LABEL: quantized_dot_general_uniform_quantize_to_i32 +// CHECK-SAME: %[[ARG:.*]]: tensor<1x2xf32> + func.func @quantized_dot_general_uniform_quantize_to_i32(%arg0: tensor<1x2xf32>) -> tensor<1x3xf32> { + %0 = stablehlo.constant dense<3.000000e+00> : tensor<1x1xf32> // Input inverse scale. + %1 = stablehlo.constant dense<1> : tensor<1x1xi8> // Input zero point. + %2 = stablehlo.constant dense<5> : tensor<2x3xi8> // Quantized filter. + %3 = stablehlo.constant dense<4> : tensor<1x3xi32> // Precalculated z1 * q2. + %4 = stablehlo.constant dense<3.000000e+03> : tensor<1x3xf32> // Merged scale: s1 * s2. + %5 = stablehlo.constant dense<2.000000e+02> : tensor<1x1xf32> // Output inverse scale. + %6 = stablehlo.constant dense<2> : tensor<1x1xi8> // Output zero point. + // This uniform_quantize function is expected to output i8 instead of i32. + %7 = call @uniform_quantize_0(%arg0, %0, %1) : (tensor<1x2xf32>, tensor<1x1xf32>, tensor<1x1xi8>) -> tensor<1x2xi32> + %8 = stablehlo.convert %7 : (tensor<1x2xi32>) -> tensor<1x2xf32> + %9 = stablehlo.convert %2 : (tensor<2x3xi8>) -> tensor<2x3xf32> + %10 = stablehlo.dot_general %8, %9, contracting_dims = [1] x [0] : (tensor<1x2xf32>, tensor<2x3xf32>) -> tensor<1x3xf32> + %11 = stablehlo.convert %3 : (tensor<1x3xi32>) -> tensor<1x3xf32> + %12 = stablehlo.subtract %10, %11 : tensor<1x3xf32> // q1 * q2 - z1 * q2 + %13 = stablehlo.multiply %12, %4 : tensor<1x3xf32> // s1 * s2 + %14 = call @uniform_quantize_1(%13, %5, %6) : (tensor<1x3xf32>, tensor<1x1xf32>, tensor<1x1xi8>) -> tensor<1x3xi8> + %15 = call @uniform_dequantize_0(%14, %5, %6) : (tensor<1x3xi8>, tensor<1x1xf32>, tensor<1x1xi8>) -> tensor<1x3xf32> + return %15 : tensor<1x3xf32> + } +// CHECK-NOT: stablehlo.uniform_quantize +// CHECK-NOT: !quant.uniform +// CHECK: stablehlo.dot_general + + // The following uniform_quantize & uniform_dequantize functions do NOT have + // the correct body. Only the type signatures matter for testing. + func.func private @uniform_quantize_0(%arg0: tensor<1x2xf32>, %arg1: tensor<1x1xf32>, %arg2: tensor<1x1xi8>) -> tensor<1x2xi32> { + %0 = stablehlo.convert %arg0 : (tensor<1x2xf32>) -> tensor<1x2xi32> + return %0 : tensor<1x2xi32> + } +// CHECK: @uniform_quantize_0 + func.func private @uniform_quantize_1(%arg0: tensor<1x3xf32>, %arg1: tensor<1x1xf32>, %arg2: tensor<1x1xi8>) -> tensor<1x3xi8> { + %0 = stablehlo.convert %arg0 : (tensor<1x3xf32>) -> tensor<1x3xi8> + return %0 : tensor<1x3xi8> + } +// CHECK: @uniform_quantize_1 + func.func private @uniform_dequantize_0(%arg0: tensor<1x3xi8>, %arg1: tensor<1x1xf32>, %arg2: tensor<1x1xi8>) -> tensor<1x3xf32> { + %0 = stablehlo.convert %arg0 : (tensor<1x3xi8>) -> tensor<1x3xf32> + return %0 : tensor<1x3xf32> + } +// CHECK: @uniform_dequantize_0 +} + +// ----- + +// Tests that the conversion doesn't happen when the filter tensor is i32. + +module { +// CHECK-LABEL: quantized_dot_general_filter_i32 +// CHECK-SAME: %[[ARG:.*]]: tensor<1x2xf32> + func.func @quantized_dot_general_filter_i32(%arg0: tensor<1x2xf32>) -> tensor<1x3xf32> { + %0 = stablehlo.constant dense<3.000000e+00> : tensor<1x1xf32> // Input inverse scale. + %1 = stablehlo.constant dense<1> : tensor<1x1xi8> // Input zero point. + %2 = stablehlo.constant dense<5> : tensor<2x3xi32> // Quantized filter - the pattern expects i8 but i32 is given. + %3 = stablehlo.constant dense<4> : tensor<1x3xi32> // Precalculated z1 * q2. + %4 = stablehlo.constant dense<3.000000e+03> : tensor<1x3xf32> // Merged scale: s1 * s2. + %5 = stablehlo.constant dense<2.000000e+02> : tensor<1x1xf32> // Output inverse scale. + %6 = stablehlo.constant dense<2> : tensor<1x1xi8> // Output zero point. + %7 = call @uniform_quantize_0(%arg0, %0, %1) : (tensor<1x2xf32>, tensor<1x1xf32>, tensor<1x1xi8>) -> tensor<1x2xi8> + %8 = stablehlo.convert %7 : (tensor<1x2xi8>) -> tensor<1x2xf32> + %9 = stablehlo.convert %2 : (tensor<2x3xi32>) -> tensor<2x3xf32> + %10 = stablehlo.dot_general %8, %9, contracting_dims = [1] x [0] : (tensor<1x2xf32>, tensor<2x3xf32>) -> tensor<1x3xf32> + %11 = stablehlo.convert %3 : (tensor<1x3xi32>) -> tensor<1x3xf32> + %12 = stablehlo.subtract %10, %11 : tensor<1x3xf32> // q1 * q2 - z1 * q2 + %13 = stablehlo.multiply %12, %4 : tensor<1x3xf32> // s1 * s2 + %14 = call @uniform_quantize_1(%13, %5, %6) : (tensor<1x3xf32>, tensor<1x1xf32>, tensor<1x1xi8>) -> tensor<1x3xi8> + %15 = call @uniform_dequantize_0(%14, %5, %6) : (tensor<1x3xi8>, tensor<1x1xf32>, tensor<1x1xi8>) -> tensor<1x3xf32> + return %15 : tensor<1x3xf32> + } +// CHECK-NOT: stablehlo.uniform_quantize +// CHECK-NOT: !quant.uniform +// CHECK: stablehlo.dot_general + + // The following uniform_quantize & uniform_dequantize functions do NOT have + // the correct body. Only the type signatures matter for testing. + func.func private @uniform_quantize_0(%arg0: tensor<1x2xf32>, %arg1: tensor<1x1xf32>, %arg2: tensor<1x1xi8>) -> tensor<1x2xi8> { + %0 = stablehlo.convert %arg0 : (tensor<1x2xf32>) -> tensor<1x2xi8> + return %0 : tensor<1x2xi8> + } +// CHECK: @uniform_quantize_0 + func.func private @uniform_quantize_1(%arg0: tensor<1x3xf32>, %arg1: tensor<1x1xf32>, %arg2: tensor<1x1xi8>) -> tensor<1x3xi8> { + %0 = stablehlo.convert %arg0 : (tensor<1x3xf32>) -> tensor<1x3xi8> + return %0 : tensor<1x3xi8> + } +// CHECK: @uniform_quantize_1 + func.func private @uniform_dequantize_0(%arg0: tensor<1x3xi8>, %arg1: tensor<1x1xf32>, %arg2: tensor<1x1xi8>) -> tensor<1x3xf32> { + %0 = stablehlo.convert %arg0 : (tensor<1x3xi8>) -> tensor<1x3xf32> + return %0 : tensor<1x3xf32> + } +// CHECK: @uniform_dequantize_0 +} + +// ----- + +// Tests that a quantized dot_general op is composed when both operands are +// actiavations. + +// CHECK-LABEL: dot_general_with_two_activations +// CHECK-SAME: %[[ARG_0:.*]]: tensor<8x16x16xf32> +// CHECK-SAME: %[[ARG_1:.*]]: tensor<8x16x4xf32> +module { + func.func @dot_general_with_two_activations(%arg0: tensor<8x16x16xf32>, %arg1: tensor<8x16x4xf32>) -> tensor<8x16x4xf32> { + %1 = stablehlo.constant dense<2.000000e-01> : tensor<1x1x1xf32> // Input 1 inverse scale (1 / s1). + %2 = stablehlo.constant dense<-128> : tensor<1x1x1xi8> // Input 1 zero point (z1). + %3 = stablehlo.constant dense<-128> : tensor<1x1x1xi32> // Input 1 zero point (z1) (upcast & folded into i32). + %4 = stablehlo.constant dense<4.000000e-01> : tensor<1x1x1xf32> // Input 2 inverse scale (1 / s2). + %5 = stablehlo.constant dense<0> : tensor<1x1x1xi8> // Input 2 zero point (z2). + %6 = stablehlo.constant dense<0> : tensor<1x1x1xi32> // Input 2 zero point (z2) (upcast & folded into i32). + %7 = stablehlo.constant dense<5.000000e-01> : tensor<1x1x1xf32> // Output inverse scale (1 / s3). + %8 = stablehlo.constant dense<-5> : tensor<1x1x1xi8> // Output zero point (z3). + %9 = stablehlo.constant dense<1.250000e+01> : tensor<1x1x1xf32> // Merged scale (s1 * s2). + %10 = call @uniform_quantize(%arg0, %1, %2) : (tensor<8x16x16xf32>, tensor<1x1x1xf32>, tensor<1x1x1xi8>) -> tensor<8x16x16xi8> // q1 + %11 = call @uniform_quantize_0(%arg1, %4, %5) : (tensor<8x16x4xf32>, tensor<1x1x1xf32>, tensor<1x1x1xi8>) -> tensor<8x16x4xi8> // q2 + %12 = stablehlo.convert %10 : (tensor<8x16x16xi8>) -> tensor<8x16x16xi32> + %13 = stablehlo.broadcast_in_dim %3, dims = [0, 1, 2] : (tensor<1x1x1xi32>) -> tensor<8x16x16xi32> + %14 = stablehlo.subtract %12, %13 : tensor<8x16x16xi32> // q1 - z1 + %15 = stablehlo.convert %11 : (tensor<8x16x4xi8>) -> tensor<8x16x4xi32> + %16 = stablehlo.broadcast_in_dim %6, dims = [0, 1, 2] : (tensor<1x1x1xi32>) -> tensor<8x16x4xi32> + %17 = stablehlo.subtract %15, %16 : tensor<8x16x4xi32> // q2 - z2 + // Corresponds to einsum expression: b i j, b j d -> b i d + %18 = stablehlo.dot_general %14, %17, batching_dims = [0] x [0], contracting_dims = [2] x [1] : (tensor<8x16x16xi32>, tensor<8x16x4xi32>) -> tensor<8x16x4xi32> + %19 = stablehlo.convert %18 : (tensor<8x16x4xi32>) -> tensor<8x16x4xf32> + %20 = stablehlo.broadcast_in_dim %9, dims = [0, 1, 2] : (tensor<1x1x1xf32>) -> tensor<8x16x4xf32> + %21 = stablehlo.multiply %19, %20 : tensor<8x16x4xf32> // * s1 s2 + %22 = call @uniform_quantize_1(%21, %7, %8) : (tensor<8x16x4xf32>, tensor<1x1x1xf32>, tensor<1x1x1xi8>) -> tensor<8x16x4xi8> + %23 = call @uniform_dequantize(%22, %7, %8) : (tensor<8x16x4xi8>, tensor<1x1x1xf32>, tensor<1x1x1xi8>) -> tensor<8x16x4xf32> + return %23 : tensor<8x16x4xf32> + } +// CHECK: %[[UQ_0:.*]] = stablehlo.uniform_quantize %[[ARG_0]] : (tensor<8x16x16xf32>) -> tensor<8x16x16x!quant.uniform> +// CHECK: %[[UQ_1:.*]] = stablehlo.uniform_quantize %[[ARG_1]] : (tensor<8x16x4xf32>) -> tensor<8x16x4x!quant.uniform> +// CHECK: %[[DOT_GENERAL:.*]] = stablehlo.dot_general %[[UQ_0]], %[[UQ_1]], batching_dims = [0] x [0], contracting_dims = [2] x [1] : (tensor<8x16x16x!quant.uniform>, tensor<8x16x4x!quant.uniform>) -> tensor<8x16x4x!quant.uniform> +// CHECK: %[[DQ_0:.*]] = stablehlo.uniform_dequantize %[[DOT_GENERAL]] : (tensor<8x16x4x!quant.uniform>) -> tensor<8x16x4xf32> +// CHECK: return %[[DQ_0]] + + // The following uniform_quantize & uniform_dequantize functions do NOT have + // the correct body. Only the type signatures matter for testing. + func.func private @uniform_quantize(%arg0: tensor<8x16x16xf32>, %arg1: tensor<1x1x1xf32>, %arg2: tensor<1x1x1xi8>) -> tensor<8x16x16xi8> { + %0 = stablehlo.convert %arg0 : (tensor<8x16x16xf32>) -> tensor<8x16x16xi8> + return %0 : tensor<8x16x16xi8> + } + func.func private @uniform_quantize_0(%arg0: tensor<8x16x4xf32>, %arg1: tensor<1x1x1xf32>, %arg2: tensor<1x1x1xi8>) -> tensor<8x16x4xi8> { + %0 = stablehlo.convert %arg0 : (tensor<8x16x4xf32>) -> tensor<8x16x4xi8> + return %0 : tensor<8x16x4xi8> + } + func.func private @uniform_quantize_1(%arg0: tensor<8x16x4xf32>, %arg1: tensor<1x1x1xf32>, %arg2: tensor<1x1x1xi8>) -> tensor<8x16x4xi8> { + %0 = stablehlo.convert %arg0 : (tensor<8x16x4xf32>) -> tensor<8x16x4xi8> + return %0 : tensor<8x16x4xi8> + } + func.func private @uniform_dequantize(%arg0: tensor<8x16x4xi8>, %arg1: tensor<1x1x1xf32>, %arg2: tensor<1x1x1xi8>) -> tensor<8x16x4xf32> { + %0 = stablehlo.convert %arg0 : (tensor<8x16x4xi8>) -> tensor<8x16x4xf32> + return %0 : tensor<8x16x4xf32> + } +} + +// ----- + +// Tests that a quantized dot_general op is composed when both operands are +// activations, where input zero points are not folded into i32 constants. + +// CHECK-LABEL: dot_general_with_two_activations +// CHECK-SAME: %[[ARG_0:.*]]: tensor<8x16x16xf32> +// CHECK-SAME: %[[ARG_1:.*]]: tensor<8x16x4xf32> +module { + func.func @dot_general_with_two_activations(%arg0: tensor<8x16x16xf32>, %arg1: tensor<8x16x4xf32>) -> tensor<8x16x4xf32> { + %1 = stablehlo.constant dense<2.000000e-01> : tensor<1x1x1xf32> // Input 1 inverse scale (1 / s1). + %2 = stablehlo.constant dense<-128> : tensor<1x1x1xi8> // Input 1 zero point (z1). + %3 = stablehlo.constant dense<4.000000e-01> : tensor<1x1x1xf32> // Input 2 inverse scale (1 / s2). + %4 = stablehlo.constant dense<0> : tensor<1x1x1xi8> // Input 2 zero point (z2). + %5 = stablehlo.constant dense<5.000000e-01> : tensor<1x1x1xf32> // Output inverse scale (1 / s3). + %6 = stablehlo.constant dense<-5> : tensor<1x1x1xi8> // Output zero point (z3). + %7 = stablehlo.constant dense<1.250000e+01> : tensor<1x1x1xf32> // Merged scale (s1 * s2). + %8 = call @uniform_quantize(%arg0, %1, %2) : (tensor<8x16x16xf32>, tensor<1x1x1xf32>, tensor<1x1x1xi8>) -> tensor<8x16x16xi8> // q1 + %9 = call @uniform_quantize_0(%arg1, %3, %4) : (tensor<8x16x4xf32>, tensor<1x1x1xf32>, tensor<1x1x1xi8>) -> tensor<8x16x4xi8> // q2 + %10 = stablehlo.convert %8 : (tensor<8x16x16xi8>) -> tensor<8x16x16xi32> + %11 = stablehlo.convert %2 : (tensor<1x1x1xi8>) -> tensor<1x1x1xi32> + %12 = stablehlo.broadcast_in_dim %11, dims = [0, 1, 2] : (tensor<1x1x1xi32>) -> tensor<8x16x16xi32> + %13 = stablehlo.subtract %10, %12 : tensor<8x16x16xi32> // q1 - z1 + %14 = stablehlo.convert %9 : (tensor<8x16x4xi8>) -> tensor<8x16x4xi32> + %15 = stablehlo.convert %4 : (tensor<1x1x1xi8>) -> tensor<1x1x1xi32> + %16 = stablehlo.broadcast_in_dim %15, dims = [0, 1, 2] : (tensor<1x1x1xi32>) -> tensor<8x16x4xi32> + %17 = stablehlo.subtract %14, %16 : tensor<8x16x4xi32> // q2 - z2 + // Corresponds to einsum expression: b i j, b j d -> b i d + %18 = stablehlo.dot_general %13, %17, batching_dims = [0] x [0], contracting_dims = [2] x [1] : (tensor<8x16x16xi32>, tensor<8x16x4xi32>) -> tensor<8x16x4xi32> + %19 = stablehlo.convert %18 : (tensor<8x16x4xi32>) -> tensor<8x16x4xf32> + %20 = stablehlo.broadcast_in_dim %7, dims = [0, 1, 2] : (tensor<1x1x1xf32>) -> tensor<8x16x4xf32> + %21 = stablehlo.multiply %19, %20 : tensor<8x16x4xf32> // * s1 s2 + %22 = call @uniform_quantize_1(%21, %5, %6) : (tensor<8x16x4xf32>, tensor<1x1x1xf32>, tensor<1x1x1xi8>) -> tensor<8x16x4xi8> + %23 = call @uniform_dequantize(%22, %5, %6) : (tensor<8x16x4xi8>, tensor<1x1x1xf32>, tensor<1x1x1xi8>) -> tensor<8x16x4xf32> + return %23 : tensor<8x16x4xf32> + } +// CHECK: %[[UQ_0:.*]] = stablehlo.uniform_quantize %[[ARG_0]] : (tensor<8x16x16xf32>) -> tensor<8x16x16x!quant.uniform> +// CHECK: %[[UQ_1:.*]] = stablehlo.uniform_quantize %[[ARG_1]] : (tensor<8x16x4xf32>) -> tensor<8x16x4x!quant.uniform> +// CHECK: %[[DOT_GENERAL:.*]] = stablehlo.dot_general %[[UQ_0]], %[[UQ_1]], batching_dims = [0] x [0], contracting_dims = [2] x [1] : (tensor<8x16x16x!quant.uniform>, tensor<8x16x4x!quant.uniform>) -> tensor<8x16x4x!quant.uniform> +// CHECK: %[[DQ_0:.*]] = stablehlo.uniform_dequantize %[[DOT_GENERAL]] : (tensor<8x16x4x!quant.uniform>) -> tensor<8x16x4xf32> +// CHECK: return %[[DQ_0]] + + // The following uniform_quantize & uniform_dequantize functions do NOT have + // the correct body. Only the type signatures matter for testing. + func.func private @uniform_quantize(%arg0: tensor<8x16x16xf32>, %arg1: tensor<1x1x1xf32>, %arg2: tensor<1x1x1xi8>) -> tensor<8x16x16xi8> { + %0 = stablehlo.convert %arg0 : (tensor<8x16x16xf32>) -> tensor<8x16x16xi8> + return %0 : tensor<8x16x16xi8> + } + func.func private @uniform_quantize_0(%arg0: tensor<8x16x4xf32>, %arg1: tensor<1x1x1xf32>, %arg2: tensor<1x1x1xi8>) -> tensor<8x16x4xi8> { + %0 = stablehlo.convert %arg0 : (tensor<8x16x4xf32>) -> tensor<8x16x4xi8> + return %0 : tensor<8x16x4xi8> + } + func.func private @uniform_quantize_1(%arg0: tensor<8x16x4xf32>, %arg1: tensor<1x1x1xf32>, %arg2: tensor<1x1x1xi8>) -> tensor<8x16x4xi8> { + %0 = stablehlo.convert %arg0 : (tensor<8x16x4xf32>) -> tensor<8x16x4xi8> + return %0 : tensor<8x16x4xi8> + } + func.func private @uniform_dequantize(%arg0: tensor<8x16x4xi8>, %arg1: tensor<1x1x1xf32>, %arg2: tensor<1x1x1xi8>) -> tensor<8x16x4xf32> { + %0 = stablehlo.convert %arg0 : (tensor<8x16x4xi8>) -> tensor<8x16x4xf32> + return %0 : tensor<8x16x4xf32> + } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/composite-lowering.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/composite-lowering.mlir new file mode 100644 index 0000000000000000000000000000000000000000..eaf96004e3035bca801eb95bc14de2e0f38e0b05 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/composite-lowering.mlir @@ -0,0 +1,479 @@ +// RUN: odml-to-stablehlo-opt -composite-lowering -verify-diagnostics %s | FileCheck %s + +func.func @hardswish(%arg0: tensor<2xf32>) -> (tensor<*xf32>) { + %0 = mhlo.composite "aten.hardswish.default" %arg0 {decomposition = @XlaCallModule_aten.hardswish.default.impl_0} : (tensor<2xf32>) -> tensor<2xf32> + %1 = "tf.Identity"(%0) {device = ""} : (tensor<2xf32>) -> tensor<*xf32> + %2 = "tf.Identity"(%1) {device = ""} : (tensor<*xf32>) -> tensor<*xf32> + return %2 : tensor<*xf32> +} +func.func private @XlaCallModule_aten.hardswish.default.impl_0(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = mhlo.constant dense<6.000000e+00> : tensor + %1 = "mhlo.broadcast_in_dim"(%0) <{broadcast_dimensions = dense<> : tensor<0xi64>}> : (tensor) -> tensor<2xf32> + %2 = mhlo.constant dense<3.40282347E+38> : tensor + %3 = "mhlo.broadcast_in_dim"(%2) <{broadcast_dimensions = dense<> : tensor<0xi64>}> : (tensor) -> tensor<2xf32> + %4 = mhlo.constant dense<3.000000e+00> : tensor + %5 = "mhlo.broadcast_in_dim"(%4) <{broadcast_dimensions = dense<> : tensor<0xi64>}> : (tensor) -> tensor<2xf32> + %6 = mhlo.constant dense<0.000000e+00> : tensor + %7 = "mhlo.broadcast_in_dim"(%6) <{broadcast_dimensions = dense<> : tensor<0xi64>}> : (tensor) -> tensor<2xf32> + %8 = mhlo.constant dense<-3.40282347E+38> : tensor + %9 = "mhlo.broadcast_in_dim"(%8) <{broadcast_dimensions = dense<> : tensor<0xi64>}> : (tensor) -> tensor<2xf32> + %10 = mhlo.add %arg0, %5 : tensor<2xf32> + %11 = mhlo.clamp %7, %10, %3 : tensor<2xf32> + %12 = mhlo.clamp %9, %11, %1 : tensor<2xf32> + %13 = mhlo.multiply %arg0, %12 : tensor<2xf32> + %14 = mhlo.divide %13, %1 : tensor<2xf32> + return %14 : tensor<2xf32> +} + +// CHECK-LABEL: func.func @hardswish( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xf32>) -> tensor<*xf32> { +// CHECK: %[[VAL_1:.*]] = "tfl.hard_swish"(%[[VAL_0]]) : (tensor<2xf32>) -> tensor<2xf32> +// CHECK: %[[VAL_2:.*]] = "tf.Identity"(%[[VAL_1]]) {device = ""} : (tensor<2xf32>) -> tensor<*xf32> +// CHECK: %[[VAL_3:.*]] = "tf.Identity"(%[[VAL_2]]) {device = ""} : (tensor<*xf32>) -> tensor<*xf32> +// CHECK: return %[[VAL_3]] : tensor<*xf32> +// CHECK: } + +func.func @avg_pool2d_1(%arg0: tensor<1x3x6x6xf32>) -> (tensor<*xf32>) { + %0 = mhlo.composite "aten.avg_pool2d.default" %arg0 {composite_attributes = {ceil_mode = false, count_include_pad = true, divisor_override = "py_None", kernel_size = dense<3> : tensor<2xi64>, padding = dense<0> : tensor<2xi64>, stride = dense<1> : tensor<2xi64>}, decomposition = @XlaCallModule_aten.avg_pool2d.default.impl_0} : (tensor<1x3x6x6xf32>) -> tensor<1x3x4x4xf32> + %1 = "tf.Identity"(%0) {device = ""} : (tensor<1x3x4x4xf32>) -> tensor<*xf32> + %2 = "tf.Identity"(%1) {device = ""} : (tensor<*xf32>) -> tensor<*xf32> + return %2 : tensor<*xf32> +} +func.func private @XlaCallModule_aten.avg_pool2d.default.impl_0(%arg0: tensor<1x3x6x6xf32>) -> tensor<1x3x4x4xf32> { + %0 = mhlo.constant dense<1.000000e+00> : tensor + %1 = "mhlo.broadcast_in_dim"(%0) {broadcast_dimensions = dense<> : tensor<0xi64>} : (tensor) -> tensor<6x6xf32> + %2 = mhlo.constant dense<0.000000e+00> : tensor + %3 = "mhlo.reduce_window"(%arg0, %2) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %7 = mhlo.add %arg1, %arg2 : tensor + mhlo.return %7 : tensor + }) {window_dimensions = dense<[1, 1, 3, 3]> : tensor<4xi64>} : (tensor<1x3x6x6xf32>, tensor) -> tensor<1x3x4x4xf32> + %4 = "mhlo.reduce_window"(%1, %2) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %7 = mhlo.add %arg1, %arg2 : tensor + mhlo.return %7 : tensor + }) {window_dimensions = dense<3> : tensor<2xi64>} : (tensor<6x6xf32>, tensor) -> tensor<4x4xf32> + %5 = "mhlo.broadcast_in_dim"(%4) {broadcast_dimensions = dense<[2, 3]> : tensor<2xi64>} : (tensor<4x4xf32>) -> tensor<1x3x4x4xf32> + %6 = mhlo.divide %3, %5 : tensor<1x3x4x4xf32> + return %6 : tensor<1x3x4x4xf32> +} + +// CHECK-LABEL: func.func @avg_pool2d_1( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x3x6x6xf32>) -> tensor<*xf32> { +// CHECK: %[[VAL_1:.*]] = arith.constant dense<[0, 2, 3, 1]> : tensor<4xi32> +// CHECK: %[[VAL_2:.*]] = "tfl.transpose"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1x3x6x6xf32>, tensor<4xi32>) -> tensor<1x6x6x3xf32> +// CHECK: %[[VAL_3:.*]] = arith.constant dense<0> : tensor<4x2xi32> +// CHECK: %[[VAL_4:.*]] = "tfl.pad"(%[[VAL_2]], %[[VAL_3]]) : (tensor<1x6x6x3xf32>, tensor<4x2xi32>) -> tensor<1x6x6x3xf32> +// CHECK: %[[VAL_5:.*]] = "tfl.average_pool_2d"(%[[VAL_4]]) <{filter_height = 3 : i32, filter_width = 3 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x6x6x3xf32>) -> tensor<1x4x4x3xf32> +// CHECK: %[[VAL_6:.*]] = arith.constant dense<[0, 3, 1, 2]> : tensor<4xi32> +// CHECK: %[[VAL_7:.*]] = "tfl.transpose"(%[[VAL_5]], %[[VAL_6]]) : (tensor<1x4x4x3xf32>, tensor<4xi32>) -> tensor<1x3x4x4xf32> +// CHECK: %[[VAL_8:.*]] = "tf.Identity"(%[[VAL_7]]) {device = ""} : (tensor<1x3x4x4xf32>) -> tensor<*xf32> +// CHECK: %[[VAL_9:.*]] = "tf.Identity"(%[[VAL_8]]) {device = ""} : (tensor<*xf32>) -> tensor<*xf32> +// CHECK: return %[[VAL_9]] : tensor<*xf32> + +func.func @avg_pool2d_2(%arg0: tensor<1x3x6x6xf32>) -> (tensor<*xf32>) { + %0 = mhlo.composite "aten.avg_pool2d.default" %arg0 {composite_attributes = {ceil_mode = false, count_include_pad = false, divisor_override = "py_None", kernel_size = dense<3> : tensor<2xi64>, padding = dense<1> : tensor<2xi64>, stride = dense<1> : tensor<2xi64>}, decomposition = @XlaCallModule_aten.avg_pool2d.default.impl_1} : (tensor<1x3x6x6xf32>) -> tensor<1x3x6x6xf32> + %1 = "tf.Identity"(%0) {device = ""} : (tensor<1x3x6x6xf32>) -> tensor<*xf32> + %2 = "tf.Identity"(%1) {device = ""} : (tensor<*xf32>) -> tensor<*xf32> + return %2 : tensor<*xf32> +} +func.func private @XlaCallModule_aten.avg_pool2d.default.impl_1(%arg0: tensor<1x3x6x6xf32>) -> tensor<1x3x6x6xf32> { + %0 = mhlo.constant dense<[[0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00], [0.000000e+00, 1.000000e+00, 1.000000e+00, 1.000000e+00, 1.000000e+00, 1.000000e+00, 1.000000e+00, 0.000000e+00], [0.000000e+00, 1.000000e+00, 1.000000e+00, 1.000000e+00, 1.000000e+00, 1.000000e+00, 1.000000e+00, 0.000000e+00], [0.000000e+00, 1.000000e+00, 1.000000e+00, 1.000000e+00, 1.000000e+00, 1.000000e+00, 1.000000e+00, 0.000000e+00], [0.000000e+00, 1.000000e+00, 1.000000e+00, 1.000000e+00, 1.000000e+00, 1.000000e+00, 1.000000e+00, 0.000000e+00], [0.000000e+00, 1.000000e+00, 1.000000e+00, 1.000000e+00, 1.000000e+00, 1.000000e+00, 1.000000e+00, 0.000000e+00], [0.000000e+00, 1.000000e+00, 1.000000e+00, 1.000000e+00, 1.000000e+00, 1.000000e+00, 1.000000e+00, 0.000000e+00], [0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00]]> : tensor<8x8xf32> + %1 = mhlo.constant dense<0.000000e+00> : tensor + %2 = "mhlo.pad"(%arg0, %1) {edge_padding_high = dense<[0, 0, 1, 1]> : tensor<4xi64>, edge_padding_low = dense<[0, 0, 1, 1]> : tensor<4xi64>, interior_padding = dense<0> : tensor<4xi64>} : (tensor<1x3x6x6xf32>, tensor) -> tensor<1x3x8x8xf32> + %3 = "mhlo.reduce_window"(%2, %1) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %7 = mhlo.add %arg1, %arg2 : tensor + mhlo.return %7 : tensor + }) {window_dimensions = dense<[1, 1, 3, 3]> : tensor<4xi64>} : (tensor<1x3x8x8xf32>, tensor) -> tensor<1x3x6x6xf32> + %4 = "mhlo.reduce_window"(%0, %1) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %7 = mhlo.add %arg1, %arg2 : tensor + mhlo.return %7 : tensor + }) {window_dimensions = dense<3> : tensor<2xi64>} : (tensor<8x8xf32>, tensor) -> tensor<6x6xf32> + %5 = "mhlo.broadcast_in_dim"(%4) {broadcast_dimensions = dense<[2, 3]> : tensor<2xi64>} : (tensor<6x6xf32>) -> tensor<1x3x6x6xf32> + %6 = mhlo.divide %3, %5 : tensor<1x3x6x6xf32> + return %6 : tensor<1x3x6x6xf32> +} + +// CHECK-LABEL: func.func @avg_pool2d_2( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x3x6x6xf32>) -> tensor<*xf32> { +// CHECK: %[[VAL_1:.*]] = arith.constant dense<[0, 2, 3, 1]> : tensor<4xi32> +// CHECK: %[[VAL_2:.*]] = "tfl.transpose"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1x3x6x6xf32>, tensor<4xi32>) -> tensor<1x6x6x3xf32> +// CHECK: %[[VAL_3:.*]] = "tfl.average_pool_2d"(%[[VAL_2]]) <{filter_height = 3 : i32, filter_width = 3 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x6x6x3xf32>) -> tensor<1x6x6x3xf32> +// CHECK: %[[VAL_4:.*]] = arith.constant dense<[0, 3, 1, 2]> : tensor<4xi32> +// CHECK: %[[VAL_5:.*]] = "tfl.transpose"(%[[VAL_3]], %[[VAL_4]]) : (tensor<1x6x6x3xf32>, tensor<4xi32>) -> tensor<1x3x6x6xf32> +// CHECK: %[[VAL_6:.*]] = "tf.Identity"(%[[VAL_5]]) {device = ""} : (tensor<1x3x6x6xf32>) -> tensor<*xf32> +// CHECK: %[[VAL_7:.*]] = "tf.Identity"(%[[VAL_6]]) {device = ""} : (tensor<*xf32>) -> tensor<*xf32> +// CHECK: return %[[VAL_7]] : tensor<*xf32> + +func.func @avg_pool2d_3(%arg0: tensor<1x1x1x8xf32>) -> (tensor<1x1x1x4xf32>) { + %2 = mhlo.composite "aten.avg_pool2d.default" %arg0 {composite_attributes = {ceil_mode = false, count_include_pad = true, divisor_override = "py_None", kernel_size = dense<[1, 3]> : tensor<2xi64>, padding = dense<[0, 1]> : tensor<2xi64>, stride = dense<[1, 2]> : tensor<2xi64>}, decomposition = @XlaCallModule_aten.avg_pool2d.default.impl_2} : (tensor<1x1x1x8xf32>) -> tensor<1x1x1x4xf32> + return %2 : tensor<1x1x1x4xf32> +} +func.func private @XlaCallModule_aten.avg_pool2d.default.impl_2(%arg0: tensor<1x1x1x8xf32>) -> tensor<1x1x1x4xf32> + +// CHECK-LABEL: avg_pool2d_3 +// CHECK: %cst = arith.constant dense<[0, 2, 3, 1]> : tensor<4xi32> +// CHECK: %0 = "tfl.transpose"(%arg0, %cst) : (tensor<1x1x1x8xf32>, tensor<4xi32>) -> tensor<1x1x8x1xf32> +// CHECK{LITERAL}: %cst_0 = arith.constant dense<[[0, 0], [0, 0], [1, 1], [0, 0]]> : tensor<4x2xi32> +// CHECK: %1 = "tfl.pad"(%0, %cst_0) : (tensor<1x1x8x1xf32>, tensor<4x2xi32>) -> tensor<1x1x10x1xf32> +// CHECK: %2 = "tfl.average_pool_2d"(%1) <{filter_height = 1 : i32, filter_width = 3 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 2 : i32}> : (tensor<1x1x10x1xf32>) -> tensor<1x1x4x1xf32> +// CHECK: %cst_1 = arith.constant dense<[0, 3, 1, 2]> : tensor<4xi32> +// CHECK: %3 = "tfl.transpose"(%2, %cst_1) : (tensor<1x1x4x1xf32>, tensor<4xi32>) -> tensor<1x1x1x4xf32> +// CHECK: return %3 : tensor<1x1x1x4xf32> + +func.func @avg_pool2d_4(%arg0: tensor<1x1x1x9xf32>) -> (tensor<1x1x1x4xf32>) { + %2 = mhlo.composite "aten.avg_pool2d.default" %arg0 {composite_attributes = {ceil_mode = false, count_include_pad = false, divisor_override = "py_None", kernel_size = dense<[1, 3]> : tensor<2xi64>, padding = dense<[0, 0]> : tensor<2xi64>, stride = dense<[1, 2]> : tensor<2xi64>}, decomposition = @XlaCallModule_aten.avg_pool2d.default.impl_3} : (tensor<1x1x1x9xf32>) -> tensor<1x1x1x4xf32> + return %2 : tensor<1x1x1x4xf32> +} +func.func private @XlaCallModule_aten.avg_pool2d.default.impl_3(%arg0: tensor<1x1x1x9xf32>) -> tensor<1x1x1x4xf32> + +// CHECK-LABEL: avg_pool2d_4 +// CHECK: %cst = arith.constant dense<[0, 2, 3, 1]> : tensor<4xi32> +// CHECK: %0 = "tfl.transpose"(%arg0, %cst) : (tensor<1x1x1x9xf32>, tensor<4xi32>) -> tensor<1x1x9x1xf32> +// CHECK: %1 = "tfl.average_pool_2d"(%0) <{filter_height = 1 : i32, filter_width = 3 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 2 : i32}> : (tensor<1x1x9x1xf32>) -> tensor<1x1x4x1xf32> +// CHECK: %cst_0 = arith.constant dense<[0, 3, 1, 2]> : tensor<4xi32> +// CHECK: %2 = "tfl.transpose"(%1, %cst_0) : (tensor<1x1x4x1xf32>, tensor<4xi32>) -> tensor<1x1x1x4xf32> +// CHECK: return %2 : tensor<1x1x1x4xf32> + + +func.func @avg_pool2d_5(%arg0: tensor<1x1x3x3xf32>) -> (tensor<1x1x2x2xf32>) { + %0 = mhlo.composite "aten.avg_pool2d.default" %arg0 {composite_attributes = {ceil_mode = true, count_include_pad = true, divisor_override = "py_None", kernel_size = dense<[2, 2]> : tensor<2xi64>, padding = dense<[0, 0]> : tensor<2xi64>, stride = dense<[2, 2]> : tensor<2xi64>}, decomposition = @XlaCallModule_aten.avg_pool2d.default.impl_4} : (tensor<1x1x3x3xf32>) -> tensor<1x1x2x2xf32> + return %0 : tensor<1x1x2x2xf32> +} +func.func private @XlaCallModule_aten.avg_pool2d.default.impl_4(%arg0: tensor<1x1x3x3xf32>) -> tensor<1x1x2x2xf32> + +// CHECK-LABEL: avg_pool2d_5 +// CHECK: %cst = arith.constant dense<[0, 2, 3, 1]> : tensor<4xi32> +// CHECK: %0 = "tfl.transpose"(%arg0, %cst) : (tensor<1x1x3x3xf32>, tensor<4xi32>) -> tensor<1x3x3x1xf32> +// CHECK{LITERAL}: %cst_0 = arith.constant dense<[[0, 0], [0, 1], [0, 1], [0, 0]]> : tensor<4x2xi32> +// CHECK: %1 = "tfl.pad"(%0, %cst_0) : (tensor<1x3x3x1xf32>, tensor<4x2xi32>) -> tensor<1x4x4x1xf32> +// CHECK: %2 = "tfl.average_pool_2d"(%1) <{filter_height = 2 : i32, filter_width = 2 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 2 : i32, stride_w = 2 : i32}> : (tensor<1x4x4x1xf32>) -> tensor<1x2x2x1xf32> +// CHECK{LITERAL}: %cst_1 = arith.constant dense<[[[[1.000000e+00], [2.000000e+00]], [[2.000000e+00], [4.000000e+00]]]]> : tensor<1x2x2x1xf32> +// CHECK: %3 = tfl.mul %2, %cst_1 {fused_activation_function = "NONE"} : tensor<1x2x2x1xf32> +// CHECK: %cst_2 = arith.constant dense<[0, 3, 1, 2]> : tensor<4xi32> +// CHECK: %4 = "tfl.transpose"(%3, %cst_2) : (tensor<1x2x2x1xf32>, tensor<4xi32>) -> tensor<1x1x2x2xf32> +// CHECK: return %4 : tensor<1x1x2x2xf32> + +func.func @avg_pool2d_6(%arg0: tensor<1x1x1x7xf32>) -> (tensor<1x1x1x2xf32>) { + %0 = mhlo.composite "aten.avg_pool2d.default" %arg0 {composite_attributes = {ceil_mode = true, count_include_pad = true, divisor_override = "py_None", kernel_size = dense<[1, 5]> : tensor<2xi64>, padding = dense<[0, 0]> : tensor<2xi64>, stride = dense<[1, 3]> : tensor<2xi64>}, decomposition = @XlaCallModule_aten.avg_pool2d.default.impl_5} : (tensor<1x1x1x7xf32>) -> tensor<1x1x1x2xf32> + return %0 : tensor<1x1x1x2xf32> +} +func.func private @XlaCallModule_aten.avg_pool2d.default.impl_5(%arg0: tensor<1x1x1x7xf32>) -> tensor<1x1x1x2xf32> + +// CHECK-LABEL: avg_pool2d_6 +// CHECK: %cst = arith.constant dense<[0, 2, 3, 1]> : tensor<4xi32> +// CHECK: %0 = "tfl.transpose"(%arg0, %cst) : (tensor<1x1x1x7xf32>, tensor<4xi32>) -> tensor<1x1x7x1xf32> +// CHECK{LITERAL}: %cst_0 = arith.constant dense<[[0, 0], [0, 0], [0, 1], [0, 0]]> : tensor<4x2xi32> +// CHECK: %1 = "tfl.pad"(%0, %cst_0) : (tensor<1x1x7x1xf32>, tensor<4x2xi32>) -> tensor<1x1x8x1xf32> +// CHECK: %2 = "tfl.average_pool_2d"(%1) <{filter_height = 1 : i32, filter_width = 5 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 3 : i32}> : (tensor<1x1x8x1xf32>) -> tensor<1x1x2x1xf32> +// CHECK{LITERAL}: %cst_1 = arith.constant dense<[[[[1.000000e+00], [1.250000e+00]]]]> : tensor<1x1x2x1xf32> +// CHECK: %3 = tfl.mul %2, %cst_1 {fused_activation_function = "NONE"} : tensor<1x1x2x1xf32> +// CHECK: %cst_2 = arith.constant dense<[0, 3, 1, 2]> : tensor<4xi32> +// CHECK: %4 = "tfl.transpose"(%3, %cst_2) : (tensor<1x1x2x1xf32>, tensor<4xi32>) -> tensor<1x1x1x2xf32> + +func.func @avg_pool2d_7(%arg0: tensor<1x1x1x8xf32>) -> (tensor<1x1x1x5xf32>) { + %0 = mhlo.composite "aten.avg_pool2d.default" %arg0 {composite_attributes = {ceil_mode = true, count_include_pad = true, divisor_override = "py_None", kernel_size = dense<[1, 3]> : tensor<2xi64>, padding = dense<[0, 1]> : tensor<2xi64>, stride = dense<[1, 2]> : tensor<2xi64>}, decomposition = @XlaCallModule_aten.avg_pool2d.default.impl_6} : (tensor<1x1x1x8xf32>) -> tensor<1x1x1x5xf32> + return %0 : tensor<1x1x1x5xf32> +} +func.func private @XlaCallModule_aten.avg_pool2d.default.impl_6(%arg0: tensor<1x1x1x8xf32>) -> tensor<1x1x1x5xf32> + +// CHECK-LABEL: avg_pool2d_7 +// CHECK: %cst = arith.constant dense<[0, 2, 3, 1]> : tensor<4xi32> +// CHECK{LITERAL}: %0 = "tfl.transpose"(%arg0, %cst) : (tensor<1x1x1x8xf32>, tensor<4xi32>) -> tensor<1x1x8x1xf32> +// CHECK{LITERAL}: %cst_0 = arith.constant dense<[[0, 0], [0, 0], [1, 2], [0, 0]]> : tensor<4x2xi32> +// CHECK: %1 = "tfl.pad"(%0, %cst_0) : (tensor<1x1x8x1xf32>, tensor<4x2xi32>) -> tensor<1x1x11x1xf32> +// CHECK: %2 = "tfl.average_pool_2d"(%1) <{filter_height = 1 : i32, filter_width = 3 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 2 : i32}> : (tensor<1x1x11x1xf32>) -> tensor<1x1x5x1xf32> +// CHECK{LITERAL}: %cst_1 = arith.constant dense<[[[[1.000000e+00], [1.000000e+00], [1.000000e+00], [1.000000e+00], [1.500000e+00]]]]> : tensor<1x1x5x1xf32> +// CHECK: %3 = tfl.mul %2, %cst_1 {fused_activation_function = "NONE"} : tensor<1x1x5x1xf32> +// CHECK: %cst_2 = arith.constant dense<[0, 3, 1, 2]> : tensor<4xi32> +// CHECK: %4 = "tfl.transpose"(%3, %cst_2) : (tensor<1x1x5x1xf32>, tensor<4xi32>) -> tensor<1x1x1x5xf32> + +func.func @upsample_bilinear2d(%arg0: tensor<1x64x16x16xf32>) -> (tensor<1x64x32x32xf32>) { + %0 = mhlo.composite "odml.upsample_bilinear2d" %arg0 {composite_attributes = {is_nchw_op = true, align_corners = false, size = dense<32> : tensor<2xi64>}, decomposition = @XlaCallModule_odml.upsample_bilinear2d.impl_21_0} : (tensor<1x64x16x16xf32>) -> tensor<1x64x32x32xf32> + return %0 : tensor<1x64x32x32xf32> +} +func.func private @XlaCallModule_odml.upsample_bilinear2d.impl_21_0(%arg0: tensor<1x64x16x16xf32>) -> tensor<1x64x32x32xf32> { + %0 = mhlo.constant dense<[[0.000000e+00], [2.500000e-01], [7.500000e-01], [2.500000e-01], [7.500000e-01], [2.500000e-01], [7.500000e-01], [2.500000e-01], [7.500000e-01], [2.500000e-01], [7.500000e-01], [2.500000e-01], [7.500000e-01], [2.500000e-01], [7.500000e-01], [2.500000e-01], [7.500000e-01], [2.500000e-01], [7.500000e-01], [2.500000e-01], [7.500000e-01], [2.500000e-01], [7.500000e-01], [2.500000e-01], [7.500000e-01], [2.500000e-01], [7.500000e-01], [2.500000e-01], [7.500000e-01], [2.500000e-01], [7.500000e-01], [2.500000e-01]]> : tensor<32x1xf32> + %1 = mhlo.constant dense<[0.000000e+00, 2.500000e-01, 7.500000e-01, 2.500000e-01, 7.500000e-01, 2.500000e-01, 7.500000e-01, 2.500000e-01, 7.500000e-01, 2.500000e-01, 7.500000e-01, 2.500000e-01, 7.500000e-01, 2.500000e-01, 7.500000e-01, 2.500000e-01, 7.500000e-01, 2.500000e-01, 7.500000e-01, 2.500000e-01, 7.500000e-01, 2.500000e-01, 7.500000e-01, 2.500000e-01, 7.500000e-01, 2.500000e-01, 7.500000e-01, 2.500000e-01, 7.500000e-01, 2.500000e-01, 7.500000e-01, 2.500000e-01]> : tensor<32xf32> + %2 = mhlo.constant dense<[1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 15]> : tensor<32xi64> + %3 = mhlo.constant dense<16> : tensor + %4 = "mhlo.broadcast_in_dim"(%3) <{broadcast_dimensions = dense<> : tensor<0xi64>}> : (tensor) -> tensor<32x32xi64> + %5 = mhlo.constant dense<0> : tensor + %6 = "mhlo.broadcast_in_dim"(%5) <{broadcast_dimensions = dense<> : tensor<0xi64>}> : (tensor) -> tensor<32x32xi64> + %7 = mhlo.constant dense<[0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15]> : tensor<32xi64> + %8 = "mhlo.broadcast_in_dim"(%7) <{broadcast_dimensions = dense<0> : tensor<1xi64>}> : (tensor<32xi64>) -> tensor<32x32xi64> + %9 = mhlo.compare LT, %8, %6 : (tensor<32x32xi64>, tensor<32x32xi64>) -> tensor<32x32xi1> + %10 = mhlo.add %8, %4 : tensor<32x32xi64> + %11 = mhlo.select %9, %10, %8 : tensor<32x32xi1>, tensor<32x32xi64> + %12 = mhlo.reshape %11 : (tensor<32x32xi64>) -> tensor<32x32x1xi64> + %13 = "mhlo.broadcast_in_dim"(%7) <{broadcast_dimensions = dense<1> : tensor<1xi64>}> : (tensor<32xi64>) -> tensor<32x32xi64> + %14 = mhlo.compare LT, %13, %6 : (tensor<32x32xi64>, tensor<32x32xi64>) -> tensor<32x32xi1> + %15 = mhlo.add %13, %4 : tensor<32x32xi64> + %16 = mhlo.select %14, %15, %13 : tensor<32x32xi1>, tensor<32x32xi64> + %17 = mhlo.reshape %16 : (tensor<32x32xi64>) -> tensor<32x32x1xi64> + %18 = "mhlo.concatenate"(%12, %17) <{dimension = 2 : i64}> : (tensor<32x32x1xi64>, tensor<32x32x1xi64>) -> tensor<32x32x2xi64> + %19 = "mhlo.gather"(%arg0, %18) <{dimension_numbers = #mhlo.gather, slice_sizes = dense<[1, 64, 1, 1]> : tensor<4xi64>}> : (tensor<1x64x16x16xf32>, tensor<32x32x2xi64>) -> tensor<1x64x32x32xf32> + %20 = "mhlo.broadcast_in_dim"(%2) <{broadcast_dimensions = dense<1> : tensor<1xi64>}> : (tensor<32xi64>) -> tensor<32x32xi64> + %21 = mhlo.compare LT, %20, %6 : (tensor<32x32xi64>, tensor<32x32xi64>) -> tensor<32x32xi1> + %22 = mhlo.add %20, %4 : tensor<32x32xi64> + %23 = mhlo.select %21, %22, %20 : tensor<32x32xi1>, tensor<32x32xi64> + %24 = mhlo.reshape %23 : (tensor<32x32xi64>) -> tensor<32x32x1xi64> + %25 = "mhlo.concatenate"(%12, %24) <{dimension = 2 : i64}> : (tensor<32x32x1xi64>, tensor<32x32x1xi64>) -> tensor<32x32x2xi64> + %26 = "mhlo.gather"(%arg0, %25) <{dimension_numbers = #mhlo.gather, slice_sizes = dense<[1, 64, 1, 1]> : tensor<4xi64>}> : (tensor<1x64x16x16xf32>, tensor<32x32x2xi64>) -> tensor<1x64x32x32xf32> + %27 = mhlo.subtract %26, %19 : tensor<1x64x32x32xf32> + %28 = "mhlo.broadcast_in_dim"(%1) <{broadcast_dimensions = dense<3> : tensor<1xi64>}> : (tensor<32xf32>) -> tensor<1x64x32x32xf32> + %29 = mhlo.multiply %27, %28 : tensor<1x64x32x32xf32> + %30 = mhlo.add %19, %29 : tensor<1x64x32x32xf32> + %31 = "mhlo.broadcast_in_dim"(%2) <{broadcast_dimensions = dense<0> : tensor<1xi64>}> : (tensor<32xi64>) -> tensor<32x32xi64> + %32 = mhlo.compare LT, %31, %6 : (tensor<32x32xi64>, tensor<32x32xi64>) -> tensor<32x32xi1> + %33 = mhlo.add %31, %4 : tensor<32x32xi64> + %34 = mhlo.select %32, %33, %31 : tensor<32x32xi1>, tensor<32x32xi64> + %35 = mhlo.reshape %34 : (tensor<32x32xi64>) -> tensor<32x32x1xi64> + %36 = "mhlo.concatenate"(%35, %17) <{dimension = 2 : i64}> : (tensor<32x32x1xi64>, tensor<32x32x1xi64>) -> tensor<32x32x2xi64> + %37 = "mhlo.gather"(%arg0, %36) <{dimension_numbers = #mhlo.gather, slice_sizes = dense<[1, 64, 1, 1]> : tensor<4xi64>}> : (tensor<1x64x16x16xf32>, tensor<32x32x2xi64>) -> tensor<1x64x32x32xf32> + %38 = "mhlo.concatenate"(%35, %24) <{dimension = 2 : i64}> : (tensor<32x32x1xi64>, tensor<32x32x1xi64>) -> tensor<32x32x2xi64> + %39 = "mhlo.gather"(%arg0, %38) <{dimension_numbers = #mhlo.gather, slice_sizes = dense<[1, 64, 1, 1]> : tensor<4xi64>}> : (tensor<1x64x16x16xf32>, tensor<32x32x2xi64>) -> tensor<1x64x32x32xf32> + %40 = mhlo.subtract %39, %37 : tensor<1x64x32x32xf32> + %41 = mhlo.multiply %40, %28 : tensor<1x64x32x32xf32> + %42 = mhlo.add %37, %41 : tensor<1x64x32x32xf32> + %43 = mhlo.subtract %42, %30 : tensor<1x64x32x32xf32> + %44 = "mhlo.broadcast_in_dim"(%0) <{broadcast_dimensions = dense<[2, 3]> : tensor<2xi64>}> : (tensor<32x1xf32>) -> tensor<1x64x32x1xf32> + %45 = mhlo.reshape %44 : (tensor<1x64x32x1xf32>) -> tensor<1x64x32xf32> + %46 = "mhlo.broadcast_in_dim"(%45) <{broadcast_dimensions = dense<[0, 1, 2]> : tensor<3xi64>}> : (tensor<1x64x32xf32>) -> tensor<1x64x32x32xf32> + %47 = mhlo.multiply %43, %46 : tensor<1x64x32x32xf32> + %48 = mhlo.add %30, %47 : tensor<1x64x32x32xf32> + return %48 : tensor<1x64x32x32xf32> +} + +// CHECK-LABEL: func.func @upsample_bilinear2d( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x64x16x16xf32>) -> tensor<1x64x32x32xf32> { +// CHECK-DAG: %[[VAL_1:.*]] = arith.constant dense<[0, 2, 3, 1]> : tensor<4xi32> +// CHECK-DAG: %[[VAL_2:.*]] = "tfl.transpose"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1x64x16x16xf32>, tensor<4xi32>) -> tensor<1x16x16x64xf32> +// CHECK-DAG: %[[VAL_3:.*]] = arith.constant dense<32> : tensor<2xi32> +// CHECK-DAG: %[[VAL_4:.*]] = "tfl.resize_bilinear"(%[[VAL_2]], %[[VAL_3]]) <{align_corners = false, half_pixel_centers = true}> : (tensor<1x16x16x64xf32>, tensor<2xi32>) -> tensor<1x32x32x64xf32> +// CHECK-DAG: %[[VAL_5:.*]] = arith.constant dense<[0, 3, 1, 2]> : tensor<4xi32> +// CHECK-DAG: %[[VAL_6:.*]] = "tfl.transpose"(%[[VAL_4]], %[[VAL_5]]) : (tensor<1x32x32x64xf32>, tensor<4xi32>) -> tensor<1x64x32x32xf32> +// CHECK: return %[[VAL_6]] : tensor<1x64x32x32xf32> +// CHECK: } + +func.func private @XlaCallModule_tfl.gelu.impl_0(%arg0: tensor<1x4x4x1xf32>) -> (tensor<1x4x4x1xf32>) +func.func @jax_gelu_approx(%arg0: tensor<1x4x4x1xf32>) -> (tensor<1x4x4x1xf32>) { + %2 = mhlo.composite "tfl.gelu" %arg0 {composite_attributes = {approximate = true}, decomposition = @XlaCallModule_tfl.gelu.impl_0} : (tensor<1x4x4x1xf32>) -> tensor<1x4x4x1xf32> + return %2 : tensor<1x4x4x1xf32> +} + +// CHECK-LABEL: jax_gelu_approx +// CHECK: %0 = "tfl.gelu"(%arg0) <{approximate = true}> : (tensor<1x4x4x1xf32>) -> tensor<1x4x4x1xf32> + +func.func private @XlaCallModule_tfl.gelu.impl_1(%arg0: tensor<1x4x4x1xf32>) -> (tensor<1x4x4x1xf32>) +func.func @jax_gelu(%arg0: tensor<1x4x4x1xf32>) -> (tensor<1x4x4x1xf32>) { + %2 = mhlo.composite "tfl.gelu" %arg0 {composite_attributes = {approximate = false}, decomposition = @XlaCallModule_tfl.gelu.impl_1} : (tensor<1x4x4x1xf32>) -> tensor<1x4x4x1xf32> + return %2 : tensor<1x4x4x1xf32> +} + +// CHECK-LABEL: jax_gelu +// CHECK: %0 = "tfl.gelu"(%arg0) <{approximate = false}> : (tensor<1x4x4x1xf32>) -> tensor<1x4x4x1xf32> + +func.func private @gelu_decomp_1(%arg0: tensor<5x10xf32>) -> tensor<5x10xf32> +func.func @gelu_aten(%arg0: tensor<5x10xf32>) -> (tensor<*xf32>) { + %0 = mhlo.composite "aten.gelu.default" %arg0 {composite_attributes = {approximate = "none"}, decomposition = @gelu_decomp_1} : (tensor<5x10xf32>) -> tensor<5x10xf32> + %1 = "tf.Identity"(%0) {device = ""} : (tensor<5x10xf32>) -> tensor<*xf32> + %2 = "tf.Identity"(%1) {device = ""} : (tensor<*xf32>) -> tensor<*xf32> + return %2 : tensor<*xf32> +} + +// CHECK-LABEL: gelu_aten +// CHECK: %0 = "tfl.gelu"(%arg0) <{approximate = false}> : (tensor<5x10xf32>) -> tensor<5x10xf32> + +func.func private @gelu_decomp_2(%arg0: tensor<5x10xf32>) -> tensor<5x10xf32> +func.func @gelu_aten_approximate(%arg0: tensor<5x10xf32>) -> (tensor<*xf32>) { + %0 = mhlo.composite "aten.gelu.default" %arg0 {composite_attributes = {approximate = "tanh"}, decomposition = @gelu_decomp_2} : (tensor<5x10xf32>) -> tensor<5x10xf32> + %1 = "tf.Identity"(%0) {device = ""} : (tensor<5x10xf32>) -> tensor<*xf32> + %2 = "tf.Identity"(%1) {device = ""} : (tensor<*xf32>) -> tensor<*xf32> + return %2 : tensor<*xf32> +} + +// CHECK-LABEL: gelu_aten_approximate +// CHECK: %0 = "tfl.gelu"(%arg0) <{approximate = true}> : (tensor<5x10xf32>) -> tensor<5x10xf32> + +// CHECK-LABEL func.func @jax_image_resize_nearest +func.func @jax_image_resize_nearest(%arg0: tensor<1x2x2x10xf32>) -> (tensor<1x4x4x10xf32>) { + %1 = mhlo.composite "tfl.resize_nearest_neighbor" %arg0 {composite_attributes = {is_nchw_op = false, size = dense<4> : tensor<2xi64>}, decomposition = @XlaCallModule_tfl.resize_nearest_neighbor.impl_0} : (tensor<1x2x2x10xf32>) -> tensor<1x4x4x10xf32> + return %1 : tensor<1x4x4x10xf32> +} +func.func private @XlaCallModule_tfl.resize_nearest_neighbor.impl_0(%arg0: tensor<1x2x2x10xf32>) -> tensor<1x4x4x10xf32> { + %0 = call @XlaCallModule__resize_0(%arg0) : (tensor<1x2x2x10xf32>) -> tensor<1x4x4x10xf32> + return %0 : tensor<1x4x4x10xf32> +} +func.func private @XlaCallModule__resize_0(%arg0: tensor<1x2x2x10xf32>) -> (tensor<1x4x4x10xf32>) { + %0 = mhlo.constant dense<2> : tensor + %1 = mhlo.constant dense<0> : tensor + %2 = mhlo.constant dense<4.000000e+00> : tensor + %3 = mhlo.constant dense<2.000000e+00> : tensor + %4 = mhlo.constant dense<5.000000e-01> : tensor + %5 = "mhlo.iota"() <{iota_dimension = 0 : i64}> : () -> tensor<4xf32> + %6 = "mhlo.broadcast_in_dim"(%4) <{broadcast_dimensions = dense<> : tensor<0xi64>}> : (tensor) -> tensor<4xf32> + %7 = mhlo.add %5, %6 : tensor<4xf32> + %8 = "mhlo.broadcast_in_dim"(%3) <{broadcast_dimensions = dense<> : tensor<0xi64>}> : (tensor) -> tensor<4xf32> + %9 = mhlo.multiply %7, %8 : tensor<4xf32> + %10 = "mhlo.broadcast_in_dim"(%2) <{broadcast_dimensions = dense<> : tensor<0xi64>}> : (tensor) -> tensor<4xf32> + %11 = mhlo.divide %9, %10 : tensor<4xf32> + %12 = mhlo.floor %11 : tensor<4xf32> + %13 = mhlo.convert %12 : (tensor<4xf32>) -> tensor<4xi32> + %14 = "mhlo.broadcast_in_dim"(%1) <{broadcast_dimensions = dense<> : tensor<0xi64>}> : (tensor) -> tensor<4xi32> + %15 = mhlo.compare LT, %13, %14, SIGNED : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi1> + %16 = "mhlo.broadcast_in_dim"(%0) <{broadcast_dimensions = dense<> : tensor<0xi64>}> : (tensor) -> tensor<4xi32> + %17 = mhlo.add %13, %16 : tensor<4xi32> + %18 = mhlo.select %15, %17, %13 : tensor<4xi1>, tensor<4xi32> + %19 = "mhlo.broadcast_in_dim"(%18) <{broadcast_dimensions = dense<0> : tensor<1xi64>}> : (tensor<4xi32>) -> tensor<4x1xi32> + %20 = "mhlo.gather"(%arg0, %19) <{dimension_numbers = #mhlo.gather, slice_sizes = dense<[1, 1, 2, 10]> : tensor<4xi64>}> : (tensor<1x2x2x10xf32>, tensor<4x1xi32>) -> tensor<1x4x2x10xf32> + %21 = "mhlo.iota"() <{iota_dimension = 0 : i64}> : () -> tensor<4xf32> + %22 = "mhlo.broadcast_in_dim"(%4) <{broadcast_dimensions = dense<> : tensor<0xi64>}> : (tensor) -> tensor<4xf32> + %23 = mhlo.add %21, %22 : tensor<4xf32> + %24 = "mhlo.broadcast_in_dim"(%3) <{broadcast_dimensions = dense<> : tensor<0xi64>}> : (tensor) -> tensor<4xf32> + %25 = mhlo.multiply %23, %24 : tensor<4xf32> + %26 = "mhlo.broadcast_in_dim"(%2) <{broadcast_dimensions = dense<> : tensor<0xi64>}> : (tensor) -> tensor<4xf32> + %27 = mhlo.divide %25, %26 : tensor<4xf32> + %28 = mhlo.floor %27 : tensor<4xf32> + %29 = mhlo.convert %28 : (tensor<4xf32>) -> tensor<4xi32> + %30 = "mhlo.broadcast_in_dim"(%1) <{broadcast_dimensions = dense<> : tensor<0xi64>}> : (tensor) -> tensor<4xi32> + %31 = mhlo.compare LT, %29, %30, SIGNED : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi1> + %32 = "mhlo.broadcast_in_dim"(%0) <{broadcast_dimensions = dense<> : tensor<0xi64>}> : (tensor) -> tensor<4xi32> + %33 = mhlo.add %29, %32 : tensor<4xi32> + %34 = mhlo.select %31, %33, %29 : tensor<4xi1>, tensor<4xi32> + %35 = "mhlo.broadcast_in_dim"(%34) <{broadcast_dimensions = dense<0> : tensor<1xi64>}> : (tensor<4xi32>) -> tensor<4x1xi32> + %36 = "mhlo.gather"(%20, %35) <{dimension_numbers = #mhlo.gather, slice_sizes = dense<[1, 4, 1, 10]> : tensor<4xi64>}> : (tensor<1x4x2x10xf32>, tensor<4x1xi32>) -> tensor<1x4x4x10xf32> + return %36 : tensor<1x4x4x10xf32> +} + +// CHECK: %cst = arith.constant dense<4> : tensor<2xi32> +// CHECK: %0 = "tfl.resize_nearest_neighbor"(%arg0, %cst) <{align_corners = false, half_pixel_centers = true}> : (tensor<1x2x2x10xf32>, tensor<2xi32>) -> tensor<1x4x4x10xf32> +// CHECK: return %0 : tensor<1x4x4x10xf32> + +// CHECK-LABEL func.func @jax_image_resize_bilinear +func.func @jax_image_resize_bilinear(%arg0: tensor<1x44x44x128xf32>) -> (tensor<1x88x88x128xf32> {jax.result_info = ""}) { + %1 = mhlo.composite "odml.upsample_bilinear2d" %arg0 {composite_attributes = {is_nchw_op = false, align_corners = false, size = dense<88> : tensor<2xi64>}, decomposition = @XlaCallModule_tfl.resize_bilinear.impl_0} : (tensor<1x44x44x128xf32>) -> tensor<1x88x88x128xf32> + return %1 : tensor<1x88x88x128xf32> +} +func.func private @XlaCallModule_tfl.resize_bilinear.impl_0(%arg0: tensor<1x44x44x128xf32>) -> tensor<1x88x88x128xf32> { + %0 = call @XlaCallModule__resize_bilinear_0(%arg0) : (tensor<1x44x44x128xf32>) -> tensor<1x88x88x128xf32> + return %0 : tensor<1x88x88x128xf32> +} +func.func private @XlaCallModule__resize_bilinear_0(%arg0: tensor<1x44x44x128xf32>) -> tensor<1x88x88x128xf32> { + %cst = arith.constant dense<88> : tensor<2xi32> + // Because the decomposition desn't matter here, we just use the + // resize_bilinear op to create a correct graph. + %1 = "tfl.resize_bilinear"(%arg0, %cst) <{align_corners = false, half_pixel_centers = true}> : (tensor<1x44x44x128xf32>, tensor<2xi32>) -> tensor<1x88x88x128xf32> + return %1 : tensor<1x88x88x128xf32> +} + +// CHECK: %cst = arith.constant dense<88> : tensor<2xi32> +// CHECK: %0 = "tfl.resize_bilinear"(%arg0, %cst) <{align_corners = false, half_pixel_centers = true}> : (tensor<1x44x44x128xf32>, tensor<2xi32>) -> tensor<1x88x88x128xf32> +// CHECK: return %0 : tensor<1x88x88x128xf32> + +// CHECK-LABEL func.func @jax_image_resize_nearest_nchw +func.func @jax_image_resize_nearest_nchw(%arg0: tensor<4x8x32x32xf32>) -> (tensor<4x8x64x64xf32>) { + %0 = call @XlaCallModule_tfl.resize_nearest_neighbor.impl_1(%arg0) : (tensor<4x8x32x32xf32>) -> tensor<4x8x64x64xf32> + %1 = mhlo.composite "tfl.resize_nearest_neighbor" %arg0 {composite_attributes = {is_nchw_op = true, size = dense<64> : tensor<2xi64>}, decomposition = @XlaCallModule_tfl.resize_nearest_neighbor.impl_1} : (tensor<4x8x32x32xf32>) -> tensor<4x8x64x64xf32> + return %1 : tensor<4x8x64x64xf32> +} +func.func private @XlaCallModule_tfl.resize_nearest_neighbor.impl_1(%arg0: tensor<4x8x32x32xf32>) -> tensor<4x8x64x64xf32> { + %0 = call @XlaCallModule__resize_1(%arg0) : (tensor<4x8x32x32xf32>) -> tensor<4x8x64x64xf32> + return %0 : tensor<4x8x64x64xf32> +} +func.func private @XlaCallModule__resize_1(%arg0: tensor<4x8x32x32xf32>) -> (tensor<4x8x64x64xf32>) { + %0 = mhlo.constant dense<32> : tensor<64xi32> + %1 = mhlo.constant dense<0> : tensor<64xi32> + %2 = mhlo.constant dense<6.400000e+01> : tensor<64xf32> + %3 = mhlo.constant dense<3.200000e+01> : tensor<64xf32> + %4 = mhlo.constant dense<5.000000e-01> : tensor<64xf32> + %5 = "mhlo.iota"() <{iota_dimension = 0 : i64}> : () -> tensor<64xf32> + %6 = mhlo.add %5, %4 : tensor<64xf32> + %7 = mhlo.multiply %6, %3 : tensor<64xf32> + %8 = mhlo.divide %7, %2 : tensor<64xf32> + %9 = mhlo.floor %8 : tensor<64xf32> + %10 = mhlo.convert %9 : (tensor<64xf32>) -> tensor<64xi32> + %11 = mhlo.compare LT, %10, %1, SIGNED : (tensor<64xi32>, tensor<64xi32>) -> tensor<64xi1> + %12 = mhlo.add %10, %0 : tensor<64xi32> + %13 = mhlo.select %11, %12, %10 : tensor<64xi1>, tensor<64xi32> + %14 = mhlo.reshape %13 : (tensor<64xi32>) -> tensor<64x1xi32> + %15 = "mhlo.gather"(%arg0, %14) <{dimension_numbers = #mhlo.gather, slice_sizes = dense<[4, 8, 1, 32]> : tensor<4xi64>}> : (tensor<4x8x32x32xf32>, tensor<64x1xi32>) -> tensor<4x8x64x32xf32> + %16 = "mhlo.gather"(%15, %14) <{dimension_numbers = #mhlo.gather, slice_sizes = dense<[4, 8, 64, 1]> : tensor<4xi64>}> : (tensor<4x8x64x32xf32>, tensor<64x1xi32>) -> tensor<4x8x64x64xf32> + return %16 : tensor<4x8x64x64xf32> +} +// CHECK: %cst = arith.constant dense<[0, 2, 3, 1]> : tensor<4xi32> +// CHECK: %1 = "tfl.transpose"(%arg0, %cst) : (tensor<4x8x32x32xf32>, tensor<4xi32>) -> tensor<4x32x32x8xf32> +// CHECK: %cst_0 = arith.constant dense<64> : tensor<2xi32> +// CHECK: %2 = "tfl.resize_nearest_neighbor"(%1, %cst_0) <{align_corners = false, half_pixel_centers = true}> : (tensor<4x32x32x8xf32>, tensor<2xi32>) -> tensor<4x64x64x8xf32> +// CHECK: %cst_1 = arith.constant dense<[0, 3, 1, 2]> : tensor<4xi32> +// CHECK: %3 = "tfl.transpose"(%2, %cst_1) : (tensor<4x64x64x8xf32>, tensor<4xi32>) -> tensor<4x8x64x64xf32> +// CHECK: return %3 : tensor<4x8x64x64xf32> + + +func.func @embedding_lookup(%arg0: tensor<1xi32>, %arg1: tensor<32000x2048xf32>) -> tensor<1x2048xf32> { + %0 = mhlo.composite "odml.embedding_lookup" %arg0, %arg1 {decomposition = @XlaCallModule_odml.embedding_lookup.impl_0} : (tensor<1xi32>, tensor<32000x2048xf32>) -> tensor<1x2048xf32> + return %0 : tensor<1x2048xf32> +} +func.func private @XlaCallModule_odml.embedding_lookup.impl_0(%arg0: tensor<1xi32>, %arg1: tensor<32000x2048xf32>) -> tensor<1x2048xf32> { + %0 = "mhlo.gather"(%arg1, %arg0) <{dimension_numbers = #mhlo.gather, slice_sizes = dense<[1, 2048]> : tensor<2xi64>}> : (tensor<32000x2048xf32>, tensor<1xi32>) -> tensor<1x2048xf32> + return %0 : tensor<1x2048xf32> + } + +// CHECK-LABEL: func.func @embedding_lookup( +// CHECK-SAME: %[[ARG_0:.*]]: tensor<1xi32>, %[[ARG_1:.*]]: tensor<32000x2048xf32>) -> tensor<1x2048xf32> { +// CHECK: %[[VAL_1:.*]] = "tfl.embedding_lookup"(%[[ARG_0]], %[[ARG_1]]) : (tensor<1xi32>, tensor<32000x2048xf32>) -> tensor<1x2048xf32> +// CHECK: return %[[VAL_1]] : tensor<1x2048xf32> +// CHECK: } + +func.func @embedding_lookup_dynamic(%arg0: tensor<1xi32>, %arg1: tensor<32000x2048xf32>, %arg2: tensor) -> tensor<1x2048xf32> { + %0 = mhlo.composite "odml.embedding_lookup" %arg2, %arg0, %arg1 {decomposition = @XlaCallModule_odml.embedding_lookup.impl_1} : (tensor, tensor<1xi32>, tensor<32000x2048xf32>) -> tensor<1x2048xf32> + return %0 : tensor<1x2048xf32> +} +func.func private @XlaCallModule_odml.embedding_lookup.impl_1(%arg2: tensor, %arg0: tensor<1xi32>, %arg1: tensor<32000x2048xf32>) -> tensor<1x2048xf32> { + %0 = "mhlo.gather"(%arg1, %arg0) <{dimension_numbers = #mhlo.gather, slice_sizes = dense<[1, 2048]> : tensor<2xi64>}> : (tensor<32000x2048xf32>, tensor<1xi32>) -> tensor<1x2048xf32> + return %0 : tensor<1x2048xf32> + } + +// CHECK-LABEL: func.func @embedding_lookup_dynamic( +// CHECK-SAME: %[[ARG_0:.*]]: tensor<1xi32>, %[[ARG_1:.*]]: tensor<32000x2048xf32>, %[[ARG_2:.*]]: tensor) -> tensor<1x2048xf32> { +// CHECK: %[[VAL_1:.*]] = "tfl.embedding_lookup"(%[[ARG_0]], %[[ARG_1]]) : (tensor<1xi32>, tensor<32000x2048xf32>) -> tensor<1x2048xf32> +// CHECK: return %[[VAL_1]] : tensor<1x2048xf32> +// CHECK: } + + +func.func @random_uniform(%arg0: tensor<3xi32>) -> tensor<1x2x3xf32> { + %0 = mhlo.composite "odml.random_uniform" %arg0 {composite_attributes = {seed = 0 : i64, seed2 = 1: i64}, decomposition = @XlaCallModule_odml.random_uniform.impl_0} : (tensor<3xi32>) -> tensor<1x2x3xf32> + return %0 : tensor<1x2x3xf32> +} +func.func private @XlaCallModule_odml.random_uniform.impl_0(%arg0: tensor<3xi32>) -> tensor<1x2x3xf32> { + %0 = mhlo.constant dense<1.000000e+00> : tensor<1x2x3xf32> + return %0 : tensor<1x2x3xf32> +} +// CHECK-LABEL func.func @random_uniform +// CHECK: %0 = "tfl.random_uniform"(%arg0) <{seed = 0 : i64, seed2 = 1 : i64}> : (tensor<3xi32>) -> tensor<1x2x3xf32> +// CHECK: return %0 : tensor<1x2x3xf32> + + +func.func @random_standard_normal(%arg0: tensor<3xi32>) -> tensor<1x2x3xf32> { + %0 = mhlo.composite "odml.random_standard_normal" %arg0 {composite_attributes = {seed = 0 : i64, seed2 = 1: i64}, decomposition = @XlaCallModule_odml.random_standard_normal.impl_0} : (tensor<3xi32>) -> tensor<1x2x3xf32> + return %0 : tensor<1x2x3xf32> +} +func.func private @XlaCallModule_odml.random_standard_normal.impl_0(%arg0: tensor<3xi32>) -> tensor<1x2x3xf32> { + %0 = mhlo.constant dense<1.000000e+00> : tensor<1x2x3xf32> + return %0 : tensor<1x2x3xf32> +} +// CHECK-LABEL func.func @random_standard_normal +// CHECK: %0 = "tfl.random_standard_normal"(%arg0) <{seed = 0 : i64, seed2 = 1 : i64}> : (tensor<3xi32>) -> tensor<1x2x3xf32> +// CHECK: return %0 : tensor<1x2x3xf32> + + +func.func private @XlaCallModule_tfl.unpack.impl_0(%arg0: tensor<1x3x4x1xf32>) -> (tensor<1x4x1xf32>, tensor<1x4x1xf32>, tensor<1x4x1xf32>) +func.func @jax_unstack(%arg0: tensor<1x3x4x1xf32>) -> (tensor<1x4x1xf32>, tensor<1x4x1xf32>, tensor<1x4x1xf32>) { + %0:3 = mhlo.composite "tfl.unpack" %arg0 {composite_attributes = {num = 3 : i32, axis = 1 : i32}, decomposition = @XlaCallModule_tfl.unpack.impl_0} : (tensor<1x3x4x1xf32>) -> (tensor<1x4x1xf32>, tensor<1x4x1xf32>, tensor<1x4x1xf32>) + return %0#0, %0#1, %0#2 : tensor<1x4x1xf32>, tensor<1x4x1xf32>, tensor<1x4x1xf32> +} + +// CHECK-LABEL: jax_unstack +// CHECK: %0:3 = "tfl.unpack"(%arg0) <{axis = 1 : i32, num = 3 : i32}> : (tensor<1x3x4x1xf32>) -> (tensor<1x4x1xf32>, tensor<1x4x1xf32>, tensor<1x4x1xf32>) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/fold_broadcast.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/fold_broadcast.mlir new file mode 100644 index 0000000000000000000000000000000000000000..1a7e8ae313c0921a6ed31e950dd0f3fe9badd7cf --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/fold_broadcast.mlir @@ -0,0 +1,57 @@ +// RUN: odml-to-stablehlo-opt %s -constant-fold-broadcast-pass -cse -verify-diagnostics | FileCheck %s + +// CHECK-LABEL: @foldBroadcastInDimBeforeMulOp_bcast_dim_1D_float +func.func @foldBroadcastInDimBeforeMulOp_bcast_dim_1D_float() -> (tensor<1x1x2x4xf32>) { + // CHECK-DAG: %[[RES:.*]] = mhlo.constant dense<{{\[\[\[\[}}1.000000e+00, 4.000000e+00, 9.000000e+00, 1.600000e+01], [5.000000e+00, 1.200000e+01, 2.100000e+01, 3.200000e+01]]]]> : tensor<1x1x2x4xf32> + %cst0 = mhlo.constant dense<[1.0, 2.0, 3.0, 4.0]> : tensor<4xf32> + %cst1 = mhlo.constant dense<[[[[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0]]]]> : tensor<1x1x2x4xf32> + %0 = "mhlo.broadcast_in_dim"(%cst0) <{broadcast_dimensions = dense<3> : tensor<1xi64>}> : (tensor<4xf32>) -> tensor<1x1x2x4xf32> + %1 = mhlo.multiply %0, %cst1 : tensor<1x1x2x4xf32> + // CHECK: return %[[RES]] : tensor<1x1x2x4xf32> + func.return %1 : tensor<1x1x2x4xf32> +} + +// CHECK-LABEL: @foldBroadcastInDimBeforeMulOp_bcast_dim_2D_float +func.func @foldBroadcastInDimBeforeMulOp_bcast_dim_2D_float() -> (tensor<1x2x2x3xf32>) { + // CHECK-DAG: %[[RES:.*]] = mhlo.constant dense<{{\[\[\[\[}}1.000000e+00, 4.000000e+00, 9.000000e+00], [4.000000e+00, 1.000000e+01, 1.800000e+01]], {{\[\[}}2.800000e+01, 4.000000e+01, 5.400000e+01], [4.000000e+01, 5.500000e+01, 7.200000e+01]]]]> : tensor<1x2x2x3xf32> + %cst0 = mhlo.constant dense<[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]> : tensor<2x3xf32> + %cst1 = mhlo.constant dense<[[[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], [[7.0, 8.0, 9.0], [10.0, 11.0, 12.0]]]]> : tensor<1x2x2x3xf32> + %0 = "mhlo.broadcast_in_dim"(%cst0) <{broadcast_dimensions = dense<[1, 3]> : tensor<2xi64>}> : (tensor<2x3xf32>) -> tensor<1x2x2x3xf32> + %1 = mhlo.multiply %0, %cst1 : tensor<1x2x2x3xf32> + // CHECK: return %[[RES]] : tensor<1x2x2x3xf32> + func.return %1 : tensor<1x2x2x3xf32> +} + +// CHECK-LABEL: @foldBroadcastInDimBeforeMulOp_bcast_dim_1D_int +func.func @foldBroadcastInDimBeforeMulOp_bcast_dim_1D_int() -> (tensor<1x1x2x4xi32>) { + // CHECK-DAG: %[[RES:.*]] = mhlo.constant dense<{{\[\[\[\[}}1, 4, 9, 16], [5, 12, 21, 32]]]]> : tensor<1x1x2x4xi32> + %cst0 = mhlo.constant dense<[1, 2, 3, 4]> : tensor<4xi32> + %cst1 = mhlo.constant dense<[[[[1, 2, 3, 4], [5, 6, 7, 8]]]]> : tensor<1x1x2x4xi32> + %0 = "mhlo.broadcast_in_dim"(%cst0) <{broadcast_dimensions = dense<3> : tensor<1xi64>}> : (tensor<4xi32>) -> tensor<1x1x2x4xi32> + %1 = mhlo.multiply %0, %cst1 : tensor<1x1x2x4xi32> + // CHECK: return %[[RES]] : tensor<1x1x2x4xi32> + func.return %1 : tensor<1x1x2x4xi32> +} + +// CHECK-LABEL: @foldBroadcastInDimBeforeMulOp_bcast_dim_4D_int +func.func @foldBroadcastInDimBeforeMulOp_bcast_dim_4D_int() -> tensor<1x2x1x4xi32> { + // CHECK-DAG: %[[RES:.*]] = mhlo.constant dense<{{\[\[\[\[}}0, 1, 4, 9]], {{\[\[}}0, 1, 4, 9]]]]> : tensor<1x2x1x4xi32> + %0 = mhlo.constant dense<[[[[0, 1, 2, 3]]]]> : tensor<1x1x1x4xi32> + %1 = mhlo.constant dense<[[[[0, 1, 2, 3]], [[0, 1, 2, 3]]]]> : tensor<1x2x1x4xi32> + %2 = "mhlo.broadcast_in_dim"(%0) <{broadcast_dimensions = dense<[0, 1, 2, 3]> : tensor<4xi64>}> : (tensor<1x1x1x4xi32>) -> tensor<1x2x1x4xi32> + %3 = mhlo.multiply %1, %2 : tensor<1x2x1x4xi32> + // CHECK: return %[[RES]] : tensor<1x2x1x4xi32> + return %3 : tensor<1x2x1x4xi32> +} + +// CHECK: @notFoldBroadcastInDimBeforeMulOpWhenArgIsNonConst_bcast_dim_1D_int(%[[ARG:.*]]: tensor<1x1x2x4xi32>) -> tensor<1x1x2x4xi32> +func.func @notFoldBroadcastInDimBeforeMulOpWhenArgIsNonConst_bcast_dim_1D_int(%arg0: tensor<1x1x2x4xi32>) -> (tensor<1x1x2x4xi32>) { + // CHECK-DAG: %[[CONST:.*]] = mhlo.constant dense<{{\[}}1, 2, 3, 4]> : tensor<4xi32> + %cst0 = mhlo.constant dense<[1, 2, 3, 4]> : tensor<4xi32> + // CHECK: %[[BROADCAST:.*]] = "mhlo.broadcast_in_dim"(%[[CONST]]) <{broadcast_dimensions = dense<3> : tensor<1xi64>}> : (tensor<4xi32>) -> tensor<1x1x2x4xi32> + %0 = "mhlo.broadcast_in_dim"(%cst0) <{broadcast_dimensions = dense<3> : tensor<1xi64>}> : (tensor<4xi32>) -> tensor<1x1x2x4xi32> + // CHECK: %[[MUL:.*]] = mhlo.multiply %[[BROADCAST]], %[[ARG]] : tensor<1x1x2x4xi32> + %1 = mhlo.multiply %0, %arg0 : tensor<1x1x2x4xi32> + // CHECK: return %[[MUL]] : tensor<1x1x2x4xi32> + func.return %1 : tensor<1x1x2x4xi32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/fuse_mhlo_convolution.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/fuse_mhlo_convolution.mlir new file mode 100644 index 0000000000000000000000000000000000000000..42c9faf714fff8b01aab847ffb2d7d3d9dd0c042 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/fuse_mhlo_convolution.mlir @@ -0,0 +1,45 @@ +// RUN: odml-to-stablehlo-opt %s -fuse-mhlo-convolution-pass -cse | FileCheck %s + +// CHECK-LABEL: @fuseMulAndConv2D +// CHECK-SAME: %[[INPUT:[^:[:space:]]+]] +func.func @fuseMulAndConv2D(%input: tensor<1x256x256x3xf32>) -> (tensor<1x256x256x2xf32>) { + // CHECK-DAG: %[[FILTER:.+]] = mhlo.constant dense<{{\[\[\[\[}}1.000000e+00, 2.000000e+00], [3.000000e+00, 4.000000e+00], [5.000000e+00, 6.000000e+00]]]]> : tensor<1x1x3x2xf32> + // CHECK-DAG: %[[CST:.+]] = mhlo.constant dense<[1.000000e-01, 2.000000e-01]> : tensor<2xf32> + // CHECK-DAG: %[[CST_BCAST:.+]] = "mhlo.broadcast_in_dim"(%[[CST]]) <{broadcast_dimensions = dense<3> : tensor<1xi64>}> : (tensor<2xf32>) -> tensor<1x1x3x2xf32> + // CHECK-DAG: %[[NEW_FILTER:.+]] = mhlo.multiply %[[CST_BCAST]], %[[FILTER]] : tensor<1x1x3x2xf32> + // CHECK-DAG: %[[RESULT:.+]] = mhlo.convolution(%[[INPUT]], %[[NEW_FILTER]]) dim_numbers = [b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f], window = {stride = [1, 1], pad = {{\[\[}}0, 0], [0, 0]], rhs_dilate = [1, 1]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64} : (tensor<1x256x256x3xf32>, tensor<1x1x3x2xf32>) -> tensor<1x256x256x2xf32> + %filter = mhlo.constant dense<[[[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]]]> : tensor<1x1x3x2xf32> + %cst = mhlo.constant dense<[0.1, 0.2]> : tensor<2xf32> + %0 = mhlo.convolution(%input, %filter) dim_numbers = [b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f], window = {stride = [1, 1], pad = [[0, 0], [0, 0]], rhs_dilate = [1, 1]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64} : (tensor<1x256x256x3xf32>, tensor<1x1x3x2xf32>) -> tensor<1x256x256x2xf32> + %1 = "mhlo.broadcast_in_dim"(%cst) <{broadcast_dimensions = dense<3> : tensor<1xi64>}> : (tensor<2xf32>) -> tensor<1x256x256x2xf32> + %2 = mhlo.multiply %0, %1 : tensor<1x256x256x2xf32> + // CHECK-DAG: return %[[RESULT]] + func.return %2 : tensor<1x256x256x2xf32> +} + +// ----- + +// CHECK-LABEL: @fuseMulAndConv2DDynamic +// CHECK-SAME: %[[INPUT:[^:[:space:]]+]] +func.func @fuseMulAndConv2DDynamic(%input: tensor) -> (tensor) { + // CHECK-DAG: %[[FILTER:.+]] = mhlo.constant dense<{{\[\[\[\[}}1.000000e+00, 2.000000e+00], [3.000000e+00, 4.000000e+00], [5.000000e+00, 6.000000e+00]]]]> : tensor<1x1x3x2xf32> + // CHECK-DAG: %[[CST_0:.+]] = mhlo.constant dense<[1.000000e-01, 2.000000e-01]> : tensor<2xf32> + // CHECK-DAG: %[[CST_1:.+]] = mhlo.constant dense<[3.000000e-01, 4.000000e-01]> : tensor<2xf32> + // CHECK: %[[CST_BCAST:.+]] = "mhlo.broadcast_in_dim"(%[[CST_0]]) <{broadcast_dimensions = dense<3> : tensor<1xi64>}> : (tensor<2xf32>) -> tensor<1x1x3x2xf32> + // CHECK: %[[NEW_FILTER:.+]] = mhlo.multiply %[[CST_BCAST]], %[[FILTER]] : tensor<1x1x3x2xf32> + // CHECK: %[[CONV:.+]] = mhlo.convolution(%[[INPUT]], %[[NEW_FILTER]]) dim_numbers = [b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f], window = {stride = [1, 1], pad = {{\[\[}}0, 0], [0, 0]], rhs_dilate = [1, 1]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64} : (tensor, tensor<1x1x3x2xf32>) -> tensor + // CHECK: %[[SHAPE:.+]] = shape.shape_of %[[CONV]] : tensor -> tensor<4xindex> + // CHECK: %[[DYNAMIC_BCAST:.+]] = "mhlo.dynamic_broadcast_in_dim"(%[[CST_1]], %[[SHAPE]]) <{broadcast_dimensions = dense<3> : tensor<1xi64>}> : (tensor<2xf32>, tensor<4xindex>) -> tensor + // CHECK: %[[ADD:.+]] = mhlo.add %[[CONV]], %[[DYNAMIC_BCAST]] : tensor + %filter = mhlo.constant dense<[[[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]]]> : tensor<1x1x3x2xf32> + %cst_0 = mhlo.constant dense<[0.1, 0.2]> : tensor<2xf32> + %cst_1 = mhlo.constant dense<[0.3, 0.4]> : tensor<2xf32> + %0 = mhlo.convolution(%input, %filter) dim_numbers = [b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f], window = {stride = [1, 1], pad = [[0, 0], [0, 0]], rhs_dilate = [1, 1]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64} : (tensor, tensor<1x1x3x2xf32>) -> tensor + %1 = shape.shape_of %0 : tensor -> tensor<4xindex> + %2 = "mhlo.dynamic_broadcast_in_dim"(%cst_0, %1) <{broadcast_dimensions = dense<3> : tensor<1xi64>}> : (tensor<2xf32>, tensor<4xindex>) -> tensor + %3 = mhlo.multiply %0, %2 : tensor + %4 = "mhlo.dynamic_broadcast_in_dim"(%cst_1, %1) <{broadcast_dimensions = dense<3> : tensor<1xi64>}> : (tensor<2xf32>, tensor<4xindex>) -> tensor + %5 = mhlo.add %3, %4 : tensor + // CHECK-DAG: return %[[ADD]] + func.return %5 : tensor +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/gelu.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/gelu.mlir new file mode 100644 index 0000000000000000000000000000000000000000..cbb22610c6d9e313d268a4f9588d54196b829d04 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/gelu.mlir @@ -0,0 +1,35 @@ +// RUN: odml-to-stablehlo-opt %s -tfl-legalize-chlo -split-input-file + +//| FileCheck %s --dump-input=fail + +module { +func.func @main(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = stablehlo.constant dense<1.000000e+00> : tensor<2xf32> + %1 = stablehlo.constant dense<0.707106769> : tensor<2xf32> + %2 = stablehlo.constant dense<5.000000e-01> : tensor<2xf32> + %3 = stablehlo.multiply %arg0, %2 : tensor<2xf32> + %4 = stablehlo.multiply %arg0, %1 : tensor<2xf32> + %5 = stablehlo.custom_call @mhlo.erf(%4) {mhlo.attributes = {}, mhlo.version = 1 : i64} : (tensor<2xf32>) -> tensor<2xf32> + %6 = stablehlo.add %5, %0 : tensor<2xf32> + %7 = stablehlo.multiply %3, %6 : tensor<2xf32> + return %7 : tensor<2xf32> +} +} + +// CHECK-LABEL: geluWithCustomCallErf + +// ----- + +func.func @geluWithCHLOErf(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = stablehlo.constant dense<1.000000e+00> : tensor<2xf32> + %1 = stablehlo.constant dense<0.707106769> : tensor<2xf32> + %2 = stablehlo.constant dense<5.000000e-01> : tensor<2xf32> + %3 = stablehlo.multiply %arg0, %2 : tensor<2xf32> + %4 = stablehlo.multiply %arg0, %1 : tensor<2xf32> + %5 = chlo.erf %4 : tensor<2xf32> -> tensor<2xf32> + %6 = stablehlo.add %5, %0 : tensor<2xf32> + %7 = stablehlo.multiply %3, %6 : tensor<2xf32> + return %7 : tensor<2xf32> +} + +// CHECK-LABEL: geluWithCHLOErf diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-inplaceupdate.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-inplaceupdate.mlir new file mode 100644 index 0000000000000000000000000000000000000000..3b03f1b4a48de96c7e1c886a57006e57487b9da3 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-inplaceupdate.mlir @@ -0,0 +1,20 @@ +// RUN: odml-to-stablehlo-opt %s -tf-stablehlo | FileCheck %s + +module attributes {tf.versions = {producer = 888 : i32}} { + +func.func @tfInplaceUpdate(%arg0: tensor<2x1x2xf32>) -> tensor<2x1x2xf32> { + %1 = arith.constant dense<1> : tensor<1xi32> + %2 = arith.constant dense<2.0> : tensor<1x1x2xf32> + %3 = "tf.InplaceUpdate"(%arg0, %1, %2) {device = ""} + : (tensor<2x1x2xf32>, tensor<1xi32>, tensor<1x1x2xf32>) -> tensor<2x1x2xf32> + func.return %3 : tensor<2x1x2xf32> +} + +} + +// CHECK-LABEL: @tfInplaceUpdate +// CHECK-DAG: %[[CST0:.*]] = stablehlo.constant dense<1> : tensor +// CHECK-DAG: %[[CST1:.*]] = stablehlo.constant dense<0> : tensor +// CHECK-DAG: %[[CST2:.*]] = stablehlo.constant dense<2.000000e+00> : tensor<1x1x2xf32> +// CHECK: %[[RES:.*]] = stablehlo.dynamic_update_slice %arg0, %[[CST2]], %[[CST0]], %[[CST1]], %[[CST1]] : (tensor<2x1x2xf32>, tensor<1x1x2xf32>, tensor, tensor, tensor) -> tensor<2x1x2xf32> +// CHECK: return %[[RES]] : tensor<2x1x2xf32> diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-skip-partitioned-calls.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-skip-partitioned-calls.mlir new file mode 100644 index 0000000000000000000000000000000000000000..2f6010807e050ffdbb2b81d13ca5e3b0989e6e05 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-skip-partitioned-calls.mlir @@ -0,0 +1,34 @@ +// RUN: odml-to-stablehlo-opt %s --tf-stablehlo=skip-partitioned-calls=true | FileCheck %s --check-prefix=CHECK-SKIP +// RUN: odml-to-stablehlo-opt %s --tf-stablehlo=skip-partitioned-calls=false | FileCheck %s --check-prefix=CHECK-NOSKIP + +module { + func.func @partitioned_call(%arg0: tensor<1x2x2x3xf32>) -> (tensor<1x2x2x3xf32>) { + %0 = "tf.StatefulPartitionedCall"(%arg0) <{ + config = "", config_proto = "", executor_type = "", f = @some_func + }> { + _collective_manager_ids = [], device = "" + } : (tensor<1x2x2x3xf32>) -> tensor<1x2x2x3xf32> + // CHECK-SKIP: tf.StatefulPartitionedCall + // CHECK-NOSKIP: call @some_func + // CHECK-NOSKIP-NOT: tf.StatefulPartitionedCall + %1 = "tf.PartitionedCall"(%0) <{ + config = "", config_proto = "", executor_type = "", f = @some_other_func + }> { + _collective_manager_ids = [], device = "" + } : (tensor<1x2x2x3xf32>) -> tensor<1x2x2x3xf32> + // CHECK-SKIP: tf.PartitionedCall + // CHECK-NOSKIP: call @some_other_func + // CHECK-NOSKIP-NOT: tf.PartitionedCall + func.return %1: tensor<1x2x2x3xf32> + } + + // CHECK-SKIP: func.func private @some_func + func.func private @some_func(%arg0: tensor<1x2x2x3xf32>) -> tensor<1x2x2x3xf32> attributes {tf._noinline = true} { + return %arg0 : tensor<1x2x2x3xf32> + } + + // CHECK-SKIP: func.func private @some_other_func + func.func private @some_other_func(%arg0: tensor<1x2x2x3xf32>) -> tensor<1x2x2x3xf32> attributes {tf._noinline = true} { + return %arg0 : tensor<1x2x2x3xf32> + } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-skip-quantization-ops.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-skip-quantization-ops.mlir new file mode 100644 index 0000000000000000000000000000000000000000..2215ce53420a15c4b9fa9dc2e12421e34e1b528a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-skip-quantization-ops.mlir @@ -0,0 +1,9 @@ +// RUN: odml-to-stablehlo-opt %s --tf-stablehlo=skip-quantization-ops=true | FileCheck %s --check-prefix=CHECK-SKIP +// RUN: odml-to-stablehlo-opt %s --tf-stablehlo=skip-quantization-ops=false | FileCheck %s --check-prefix=CHECK-NOSKIP + +func.func @fake_quant_with_min_max_vars(%arg0: tensor<1x1x28x48xf32>, %arg1: tensor, %arg2: tensor) -> tensor<1x1x28x48xf32> { + %0 = "tf.FakeQuantWithMinMaxVars"(%arg0, %arg1, %arg2) {device = "", narrow_range = true, num_bits = 8 : i64} : (tensor<1x1x28x48xf32>, tensor, tensor) -> tensor<1x1x28x48xf32> + func.return %0 : tensor<1x1x28x48xf32> + // CHECK-SKIP: tf.FakeQuantWithMinMaxVars + // CHECK-NOSKIP-NOT: tf. +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-stablehlo-tfl-composite-quantize-dequantize.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-stablehlo-tfl-composite-quantize-dequantize.mlir new file mode 100644 index 0000000000000000000000000000000000000000..aa8f78c35d04262a3efd36810996dfced6722c23 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-stablehlo-tfl-composite-quantize-dequantize.mlir @@ -0,0 +1,33 @@ +// RUN: odml-to-stablehlo-opt %s -stablehlo-composite-legalize-tfl-custom | FileCheck %s +module { + // CHECK-LABEL: func.func private @test_quantize_and_dequantize + func.func private @test_quantize_and_dequantize(%arg0: tensor<1x4xf32>) -> tensor<1x3xf32> { + %0 = "tfl.pseudo_const"() <{value = dense<1.000000e+00> : tensor<4x3xf32>}> : () -> tensor<4x3xf32> + // CHECK: %1:3 = "tfl.custom"(%0) <{custom_code = "odml.quantize_and_dequantize", custom_option = #tfl}> : (tensor<4x3xf32>) -> (tensor<4x3xf32>, tensor<4x3xf32>, tensor<1x3xf32>) + %1:3 = stablehlo.composite "odml.quantize_and_dequantize" %0 {composite_attributes = {axis = 0 : i64, bits = 4 : i64}, decomposition = @call_module_odml.quantize_and_dequantize.0} : (tensor<4x3xf32>) -> (tensor<4x3xf32>, tensor<4x3xf32>, tensor<1x3xf32>) + %2 = "tfl.batch_matmul"(%arg0, %1#0) <{adj_x = false, adj_y = false, asymmetric_quantize_inputs = false}> : (tensor<1x4xf32>, tensor<4x3xf32>) -> tensor<1x3xf32> + return %2 : tensor<1x3xf32> + } + + func.func private @call_module_odml.quantize_and_dequantize.0(%arg0: tensor<4x3xf32>) -> (tensor<4x3xf32>, tensor<4x3xf32>, tensor<1x3xf32>) { + %0 = "tfl.abs"(%arg0) : (tensor<4x3xf32>) -> tensor<4x3xf32> + %1 = "tfl.pseudo_const"() <{value = dense<0> : tensor<1xi32>}> : () -> tensor<1xi32> + %2 = "tfl.reduce_max"(%0, %1) <{keep_dims = false}> : (tensor<4x3xf32>, tensor<1xi32>) -> tensor<3xf32> + %3 = "tfl.pseudo_const"() <{value = dense<0.142857149> : tensor}> : () -> tensor + %4 = tfl.mul(%2, %3) <{fused_activation_function = "NONE"}> : (tensor<3xf32>, tensor) -> tensor<3xf32> + %5 = "tfl.pseudo_const"() <{value = dense<9.99999993E-9> : tensor}> : () -> tensor + %6 = tfl.add(%4, %5) <{fused_activation_function = "NONE"}> : (tensor<3xf32>, tensor) -> tensor<3xf32> + %7 = "tfl.pseudo_const"() <{value = dense<[1, 3]> : tensor<2xi32>}> : () -> tensor<2xi32> + %8 = "tfl.reshape"(%6, %7) : (tensor<3xf32>, tensor<2xi32>) -> tensor<1x3xf32> + %9 = tfl.div(%arg0, %8) <{fused_activation_function = "NONE"}> : (tensor<4x3xf32>, tensor<1x3xf32>) -> tensor<4x3xf32> + %10 = tfl.sub %9, %9 {fused_activation_function = "NONE"} : tensor<4x3xf32> + %11 = "tfl.round"(%9) : (tensor<4x3xf32>) -> tensor<4x3xf32> + %12 = tfl.add %10, %11 {fused_activation_function = "NONE"} : tensor<4x3xf32> + %13 = "tfl.pseudo_const"() <{value = dense<-8.000000e+00> : tensor}> : () -> tensor + %14 = "tfl.maximum"(%12, %13) : (tensor<4x3xf32>, tensor) -> tensor<4x3xf32> + %15 = "tfl.pseudo_const"() <{value = dense<7.000000e+00> : tensor}> : () -> tensor + %16 = "tfl.minimum"(%14, %15) : (tensor<4x3xf32>, tensor) -> tensor<4x3xf32> + %17 = tfl.mul(%16, %8) <{fused_activation_function = "NONE"}> : (tensor<4x3xf32>, tensor<1x3xf32>) -> tensor<4x3xf32> + return %17, %16, %8 : tensor<4x3xf32>, tensor<4x3xf32>, tensor<1x3xf32> + } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-stablehlo-tfl-composite.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-stablehlo-tfl-composite.mlir new file mode 100644 index 0000000000000000000000000000000000000000..5408459b5f2377c8f4e1161740a76da0a4c669ed --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-stablehlo-tfl-composite.mlir @@ -0,0 +1,32 @@ +// RUN: odml-to-stablehlo-opt %s -stablehlo-composite-legalize-tfl-custom | FileCheck %s + +func.func private @odml.update_kv_cache.impl_0(%arg0: tensor<1x500x4x4xf32>, %arg1: tensor<1x500x4x4xf32>, %arg2: tensor<100xi64>, %arg3: tensor<1x100x4x4xf32>, %arg4: tensor<1x100x4x4xf32>) -> (tensor<1x500x4x4xf32>, tensor<1x500x4x4xf32>) +// CHECK-LABEL: func.func private @test_multiple_kv_caches +func.func private @test_multiple_kv_caches(%arg0: tensor<1x500x4x4xf32>, %arg1: tensor<1x500x4x4xf32>, %arg2: tensor<100xi64>, %arg3: tensor<1x100x4x4xf32>, %arg4: tensor<1x100x4x4xf32>) -> (tensor<1x500x4x4xf32>, tensor<1x500x4x4xf32>) { + // CHECK: %0:2 = "tfl.custom"(%arg2, %arg3, %arg4) <{custom_code = "odml.update_kv_cache", custom_option = #tfl}> : (tensor<100xi64>, tensor<1x100x4x4xf32>, tensor<1x100x4x4xf32>) -> (tensor<1x500x4x4xf32>, tensor<1x500x4x4xf32>) + // CHECK: %1:2 = "tfl.custom"(%arg2, %arg3, %arg4) <{custom_code = "odml.update_kv_cache", custom_option = #tfl}> : (tensor<100xi64>, tensor<1x100x4x4xf32>, tensor<1x100x4x4xf32>) -> (tensor<1x500x4x4xf32>, tensor<1x500x4x4xf32>) + %0:2 = stablehlo.composite "odml.update_kv_cache" %arg0, %arg1, %arg2, %arg3, %arg4 {composite_attributes = {kv_cache_max = 500 : i64}, decomposition = @odml.update_kv_cache.impl_0} : (tensor<1x500x4x4xf32>, tensor<1x500x4x4xf32>, tensor<100xi64>, tensor<1x100x4x4xf32>, tensor<1x100x4x4xf32>) -> (tensor<1x500x4x4xf32>, tensor<1x500x4x4xf32>) + %1:2 = stablehlo.composite "odml.update_kv_cache" %0#0, %0#1, %arg2, %arg3, %arg4 {composite_attributes = {kv_cache_max = 500 : i64}, decomposition = @odml.update_kv_cache.impl_0} : (tensor<1x500x4x4xf32>, tensor<1x500x4x4xf32>, tensor<100xi64>, tensor<1x100x4x4xf32>, tensor<1x100x4x4xf32>) -> (tensor<1x500x4x4xf32>, tensor<1x500x4x4xf32>) + return %1#0, %1#1 : tensor<1x500x4x4xf32>, tensor<1x500x4x4xf32> +} + +// --- + +func.func private @test_odml_detector.detector.impl_0(%arg0: tensor<2xf32>) -> tensor<2xf32> +// CHECK-LABEL: func.func private @test_odml_detector +func.func @test_odml_detector(%arg0: tensor<2xf32>, %arg1: tensor<2xf32>) -> (tensor<2xf32>) { + %0 = tfl.add %arg0, %arg1 {fused_activation_function = "NONE"} : tensor<2xf32> + // CHECK %1 = "tfl.custom"(%0) <{custom_code = "odml.detector", custom_option = #tfl}> : (tensor<2xf32>) -> tensor<2xf32> + %1 = stablehlo.composite "odml.detector" %0 {composite_attributes = {name = "out", working_dir = "/tmp/tst"}, decomposition = @test_odml_detector.detector.impl_0} : (tensor<2xf32>) -> tensor<2xf32> + return %1 : tensor<2xf32> +} + +// --- + +func.func private @test_litert_custom_op.impl_0(%arg0: tensor<2xf32>) -> tensor<2xf32> +// CHECK-LABEL: func.func @test_litert_custom_op +func.func @test_litert_custom_op(%arg0: tensor<2xf32>) -> (tensor<2xf32>) { + // CHECK: "tfl.custom"(%arg0) <{custom_code = "litert_custom_op.my_op" + %0 = stablehlo.composite "litert_custom_op.my_op" %arg0 {composite_attributes = {}, decomposition = @test_litert_custom_op.impl_0} : (tensor<2xf32>) -> tensor<2xf32> + return %0 : tensor<2xf32> +} \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-stablehlo-vhlo.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-stablehlo-vhlo.mlir new file mode 100644 index 0000000000000000000000000000000000000000..b8f0d489ac0c67970bc74c784c92aac8b4410910 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-stablehlo-vhlo.mlir @@ -0,0 +1,128 @@ +// RUN: odml-to-stablehlo-opt %s --stablehlo-legalize-vhlo -reconcile-unrealized-casts -split-input-file | FileCheck %s +// RUN: odml-to-stablehlo-opt --stablehlo-legalize-vhlo -reconcile-unrealized-casts %s | odml-to-stablehlo-opt --vhlo-legalize-stablehlo -reconcile-unrealized-casts > %t.0 +// RUN: odml-to-stablehlo-opt %s > %t.1 +// RUN: diff %t.0 %t.1 + +// CHECK-LABEL: op_tfl +func.func @op_tfl(%arg0 : tensor) -> (tensor) { + // CHECK: %0 = tfl.add %arg0, %arg0 {fused_activation_function = "NONE"} : tensor + %0 = tfl.add %arg0, %arg0 {fused_activation_function = "NONE"} : tensor + return %0 : tensor +} + +// ----- + +// CHECK-LABEL: op_shlo +func.func @op_shlo(%arg0 : tensor) -> (tensor) { + // CHECK: %0 = "vhlo.add_v1"(%arg0, %arg0) : (tensor, tensor) -> tensor + %0 = stablehlo.add %arg0, %arg0 : tensor + return %0 : tensor +} + +// ----- + +// CHECK-LABEL: mixed_shlo_tfl_shlo +func.func @mixed_shlo_tfl_shlo(%arg0 : tensor) -> (tensor) { + // CHECK: %0 = "vhlo.abs_v1"(%arg0) : (tensor) -> tensor + // CHECK-NEXT: %1 = tfl.add %0, %arg0 {fused_activation_function = "NONE"} : tensor + // CHECK-NEXT: %2 = "vhlo.abs_v1"(%1) : (tensor) -> tensor + %0 = stablehlo.abs %arg0 : tensor + %1 = tfl.add %0, %arg0 {fused_activation_function = "NONE"} : tensor + %2 = stablehlo.abs %1 : tensor + return %2 : tensor +} + +// ----- + +// CHECK-LABEL: mixed_tfl_shlo_tfl +func.func @mixed_tfl_shlo_tfl(%arg0 : tensor) -> (tensor) { + %0 = "tfl.abs"(%arg0) {fused_activation_function = "NONE"} : (tensor) -> tensor + // CHECK: %1 = "vhlo.add_v1"(%0, %arg0) : (tensor, tensor) -> tensor + %1 = stablehlo.add %0, %arg0 : tensor + %2 = "tfl.abs"(%1) {fused_activation_function = "NONE"} : (tensor) -> tensor + return %2 : tensor +} + +// ----- + +// CHECK-LABEL: op_with_region +func.func @op_with_region(%arg0: tensor<1x16x16x320xf32>, %arg1: tensor) -> tensor<1x320xf32> { + // CHECK: %0 = "vhlo.reduce_v1"(%arg0, %arg1) <{{.*}}> ({ + // CHECK-NEXT: ^bb0(%arg2: tensor, %arg3: tensor): + // CHECK-NEXT: %1 = "vhlo.add_v1"(%arg2, %arg3) : (tensor, tensor) -> tensor + // CHECK-NEXT: "vhlo.return_v1"(%1) : (tensor) -> () + // CHECK-NEXT: }) : (tensor<1x16x16x320xf32>, tensor) -> tensor<1x320xf32> + %0 = stablehlo.reduce(%arg0 init: %arg1) applies stablehlo.add across dimensions = [1, 2] : (tensor<1x16x16x320xf32>, tensor) -> tensor<1x320xf32> + return %0 : tensor<1x320xf32> +} + +// ----- + +// CHECK-LABEL: op_with_region_mixed_tfl_shlo_tfl +func.func @op_with_region_mixed_tfl_shlo_tfl(%arg0: tensor<7x5xf32>, %arg1 : tensor<5xf32>) -> tensor<5xf32> { + %0 = "stablehlo.reduce"(%arg0, %arg1) ({ + ^bb0(%arg2: tensor<5xf32>, %arg3: tensor<5xf32>): + // CHECK: %1 = "tfl.abs"(%arg2) {fused_activation_function = "NONE"} : (tensor<5xf32>) -> tensor<5xf32> + // CHECK-NEXT: %2 = "vhlo.add_v1"(%1, %arg2) : (tensor<5xf32>, tensor<5xf32>) -> tensor<5xf32> + // CHECK-NEXT: %3 = "tfl.abs"(%2) {fused_activation_function = "NONE"} : (tensor<5xf32>) -> tensor<5xf32> + %1 = "tfl.abs"(%arg2) {fused_activation_function = "NONE"} : (tensor<5xf32>) -> tensor<5xf32> + %2 = stablehlo.add %1, %arg2 : tensor<5xf32> + %3 = "tfl.abs"(%2) {fused_activation_function = "NONE"} : (tensor<5xf32>) -> tensor<5xf32> + "stablehlo.return"(%3) : (tensor<5xf32>) -> () + }) {dimensions = array} : (tensor<7x5xf32>, tensor<5xf32>) -> tensor<5xf32> + func.return %0: tensor<5xf32> +} + +// ----- + +// CHECK-LABEL: op_with_region_mixed_shlo_tfl_shlo +func.func @op_with_region_mixed_shlo_tfl_shlo(%arg0: tensor<7x5xf32>, %arg1 : tensor<5xf32>) -> tensor<5xf32> { + %0 = "stablehlo.reduce"(%arg0, %arg1) ({ + ^bb0(%arg2: tensor<5xf32>, %arg3: tensor<5xf32> ): + // CHECK: %1 = "vhlo.abs_v1"(%arg2) : (tensor<5xf32>) -> tensor<5xf32> + // CHECK-NEXT: %2 = tfl.add %1, %arg2 {fused_activation_function = "NONE"} : tensor<5xf32> + // CHECK-NEXT: %3 = "vhlo.abs_v1"(%2) : (tensor<5xf32>) -> tensor<5xf32> + %1 = stablehlo.abs %arg2 : tensor<5xf32> + %2 = tfl.add %1, %arg2 {fused_activation_function = "NONE"} : tensor<5xf32> + %3 = stablehlo.abs %2 : tensor<5xf32> + "stablehlo.return"(%3) : (tensor<5xf32>) -> () + }) {dimensions = array} : (tensor<7x5xf32>, tensor<5xf32>) -> tensor<5xf32> + func.return %0: tensor<5xf32> +} + +// ----- + +// CHECK-LABEL: op_with_tfl_control_flow +func.func @op_with_tfl_control_flow() -> (tensor<1xf32>, !tfl.control) { + // CHECK: vhlo.constant_v1 + %0 = stablehlo.constant dense<1.000000e+00> : tensor<1xf32> + // CHECK-NEXT: tfl.control_node + %outputs, %control = tfl.control_node { + %1 = "tfl.neg"(%0) : (tensor<1xf32>) -> tensor<1xf32> + "tfl.yield"(%1) : (tensor<1xf32>) -> () + } + return %outputs, %control : tensor<1xf32>, !tfl.control +} + +// ----- + +// CHECK-LABEL: func_with_tfl_attrs +func.func @func_with_tfl_attrs(%arg0: tensor>>, %arg1: tensor>>) -> tensor>> attributes {tf.entry_function = {inputs = "arg0,arg1", outputs = "arg0"}} { + return %arg0 : tensor>> +} + +// ----- + +// There are cases where ODML converter relies on constants not being folded or +// CSE'ed. This test ensures that StableHLO<->ODML conversion does not fold. + +// CHECK-LABEL: mixed_no_constant_folding +func.func @mixed_no_constant_folding() -> (tensor) { + // CHECK: %[[CST0:.+]] = arith.constant dense<0.000000e+00> + // CHECK-NEXT: %[[CST1:.+]] = arith.constant dense<0.000000e+00> + // CHECK-NEXT: "vhlo.add_v1"(%[[CST0]], %[[CST1]]) : (tensor, tensor) -> tensor + %cst_0 = arith.constant dense<0.000000e+00> : tensor + %cst_1 = arith.constant dense<0.000000e+00> : tensor + %0 = stablehlo.add %cst_0, %cst_1 : tensor + return %0 : tensor +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tf.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tf.mlir new file mode 100644 index 0000000000000000000000000000000000000000..b87bc3ecd2ee37bdc7567b9267be3ccc8fcfb01b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tf.mlir @@ -0,0 +1,2551 @@ +// RUN: odml-to-stablehlo-opt --tf-stablehlo \ +// RUN: %s | env FILECHECK_OPTS="" FileCheck %s + +//===----------------------------------------------------------------------===// +// BatchNorm op legalizations. +//===----------------------------------------------------------------------===// + +// ----- + +// fusedBatchNormV2 is almost identical to fusedBatchNormV3 (and uses the same +// code), so only do a couple of basic checks. + +// CHECK-LABEL: fusedBatchNormV2_noTraining +func.func @fusedBatchNormV2_noTraining(%arg0: tensor<8x8x8x8xf32>, %arg1: tensor<8xf32>, %arg2: tensor<8xf32>, %arg3: tensor<8xf32>, %arg4: tensor<8xf32>) -> (tensor<8x8x8x8xf32>) { + // CHECK: "stablehlo.batch_norm_inference"({{.*}}, %arg1, %arg2, %arg3, %arg4) <{epsilon = 1.000000e-03 : f32, feature_index = 3 : i64}> : (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) -> tensor<8x8x8x8xf32> + %0:5 = "tf.FusedBatchNormV2"(%arg0, %arg1, %arg2, %arg3, %arg4) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", epsilon = 0.001 : f32, is_training = false} : (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) -> (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) + func.return %0#0 : tensor<8x8x8x8xf32> +} + +// ----- + +// CHECK-LABEL: fusedBatchNormV2_training +func.func @fusedBatchNormV2_training(%arg0: tensor<8x8x8x8xf32>, %arg1: tensor<8xf32>, %arg2: tensor<8xf32>, %arg3: tensor<8xf32>, %arg4: tensor<8xf32>) -> (tensor<8x8x8x8xf32>) { + // CHECK: %[[OUT:.*]], %[[MEAN:.*]], %[[VAR:.*]] = "stablehlo.batch_norm_training"({{.*}}, %arg1, %arg2) <{epsilon = 1.000000e-03 : f32, feature_index = 3 : i64}> : (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>) -> (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>) + %0:5 = "tf.FusedBatchNormV2"(%arg0, %arg1, %arg2, %arg3, %arg4) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", epsilon = 0.001 : f32, exponential_avg_factor = 1.0 : f32, is_training = true} : (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) -> (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) + func.return %0#0 : tensor<8x8x8x8xf32> +} + +// ----- + +// CHECK-LABEL: fusedBatchNormV3_noTraining +func.func @fusedBatchNormV3_noTraining(%arg0: tensor<8x8x8x8xf32>, %arg1: tensor<8xf32>, %arg2: tensor<8xf32>, %arg3: tensor<8xf32>, %arg4: tensor<8xf32>) -> (tensor<8x8x8x8xf32>) { + // CHECK: "stablehlo.batch_norm_inference"({{.*}}, %arg1, %arg2, %arg3, %arg4) <{epsilon = 1.000000e-03 : f32, feature_index = 3 : i64}> : (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) -> tensor<8x8x8x8xf32> + %0:6 = "tf.FusedBatchNormV3"(%arg0, %arg1, %arg2, %arg3, %arg4) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", epsilon = 0.001 : f32, is_training = false} : (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) -> (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) + func.return %0#0 : tensor<8x8x8x8xf32> +} + +// ----- + +// CHECK-LABEL: fusedBatchNormV3_noTraining_mixedPrecision +// CHECK-SAME: ([[X:%.*]]: tensor<8x8x8x8xbf16>, [[SCALE:%.*]]: tensor<8xf32>, [[OFFSET:%.*]]: tensor<8xf32>, [[MEAN:%.*]]: tensor<8xf32>, [[VARIANCE:%.*]]: tensor<8xf32>) +func.func @fusedBatchNormV3_noTraining_mixedPrecision(%arg0: tensor<8x8x8x8xbf16>, %arg1: tensor<8xf32>, %arg2: tensor<8xf32>, %arg3: tensor<8xf32>, %arg4: tensor<8xf32>) -> (tensor<8x8x8x8xbf16>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<*xf32>) { + // CHECK: [[DUMMY:%.*]] = stablehlo.constant dense<0.000000e+00> : tensor<0xf32> + // CHECK: [[CONVERT_X:%.*]] = stablehlo.convert [[X]] : (tensor<8x8x8x8xbf16>) -> tensor<8x8x8x8xf32> + // CHECK: [[Y:%.*]] = "stablehlo.batch_norm_inference"([[CONVERT_X]], [[SCALE]], [[OFFSET]], [[MEAN]], [[VARIANCE]]) <{epsilon = 1.000000e-03 : f32, feature_index = 3 : i64}> + %0:6 = "tf.FusedBatchNormV3"(%arg0, %arg1, %arg2, %arg3, %arg4) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", epsilon = 0.001 : f32, is_training = false} : (tensor<8x8x8x8xbf16>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) -> (tensor<8x8x8x8xbf16>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<*xf32>) + // CHECK: [[Y_CONVERT:%.*]] = stablehlo.convert [[Y]] : (tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xbf16> + // CHECK: [[DUMMY_CAST:%.*]] = tensor.cast [[DUMMY]] : tensor<0xf32> to tensor<*xf32> + // CHECK: return [[Y_CONVERT]], [[MEAN]], [[VARIANCE]], [[MEAN]], [[VARIANCE]], [[DUMMY_CAST]] + func.return %0#0, %0#1, %0#2, %0#3, %0#4, %0#5 : tensor<8x8x8x8xbf16>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<*xf32> +} + +// ----- + +// CHECK-LABEL: fusedBatchNormV3_training +func.func @fusedBatchNormV3_training(%arg0: tensor<8x8x8x8xf32>, %arg1: tensor<8xf32>, %arg2: tensor<8xf32>, %arg3: tensor<8xf32>, %arg4: tensor<8xf32>) -> (tensor<8x8x8x8xf32>) { + // CHECK: %[[OUT:.*]], %[[MEAN:.*]], %[[VAR:.*]] = "stablehlo.batch_norm_training"({{.*}}, %arg1, %arg2) <{epsilon = 1.000000e-03 : f32, feature_index = 3 : i64}> : (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>) -> (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>) + %0:6 = "tf.FusedBatchNormV3"(%arg0, %arg1, %arg2, %arg3, %arg4) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", epsilon = 0.001 : f32, exponential_avg_factor = 1.0 : f32, is_training = true} : (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) -> (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) + func.return %0#0 : tensor<8x8x8x8xf32> +} + +// ----- + +// CHECK-LABEL: func @fusedBatchNormV3_training_batchVariance +func.func @fusedBatchNormV3_training_batchVariance(%arg0: tensor<8x8x8x8xf32>, %arg1: tensor<8xf32>, %arg2: tensor<8xf32>, %arg3: tensor<8xf32>, %arg4: tensor<8xf32>) -> tensor<8xf32> { + // CHECK: %[[OUT:.*]], %[[MEAN:.*]], %[[VAR:.*]] = "stablehlo.batch_norm_training"({{.*}}, %arg1, %arg2) <{epsilon = 1.000000e-03 : f32, feature_index = 3 : i64}> : (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>) -> (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>) + %0:6 = "tf.FusedBatchNormV3"(%arg0, %arg1, %arg2, %arg3, %arg4) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", epsilon = 0.001 : f32, exponential_avg_factor = 1.0 : f32, is_training = true} : (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) -> (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) + // CHECK: return %[[VAR]] + func.return %0#4 : tensor<8xf32> +} + +// ----- + +// CHECK-LABEL: fusedBatchNormV3_training_exponentialAvgFactor +func.func @fusedBatchNormV3_training_exponentialAvgFactor(%arg0: tensor<8x8x8x8xf32>, %arg1: tensor<8xf32>, %arg2: tensor<8xf32>, %arg3: tensor<8xf32>, %arg4: tensor<8xf32>) -> (tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) { + // CHECK-DAG: %[[ALPHA:.*]] = stablehlo.constant dense<0.199999988> + // CHECK-DAG: %[[BETA:.*]] = stablehlo.constant dense<8.000000e-01> + // CHECK-DAG: %[[FACTOR:.*]] = stablehlo.constant dense<1.00195694> + // CHECK: %[[OUT:.*]], %[[MEAN:.*]], %[[VAR:.*]] = "stablehlo.batch_norm_training"({{.*}}, %arg1, %arg2) <{epsilon = 1.000000e-03 : f32, feature_index = 3 : i64}> : (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>) -> (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>) + %0:6 = "tf.FusedBatchNormV3"(%arg0, %arg1, %arg2, %arg3, %arg4) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", epsilon = 0.001 : f32, exponential_avg_factor = 0.8 : f32, is_training = true} : (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) -> (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) + // CHECK: %[[CORRECTED_VAR:.*]] = stablehlo.multiply %[[VAR]], %[[FACTOR]] + + // CHECK: %[[ALPHA_MUL_OLD_MEAN:.*]] = stablehlo.multiply %arg3, %[[ALPHA]] + // CHECK: %[[BETA_MUL_BATCH_MEAN:.*]] = stablehlo.multiply %[[MEAN]], %[[BETA]] + // CHECK: %[[NEW_BATCH_MEAN:.*]] = stablehlo.add %[[ALPHA_MUL_OLD_MEAN]], %[[BETA_MUL_BATCH_MEAN]] + + // CHECK: %[[ALPHA_MUL_OLD_VAR:.*]] = stablehlo.multiply %arg4, %[[ALPHA]] + // CHECK: %[[BETA_MUL_CORRECTED_VAR:.*]] = stablehlo.multiply %[[CORRECTED_VAR]], %[[BETA]] + // CHECK: %[[NEW_BATCH_VAR:.*]] = stablehlo.add %[[ALPHA_MUL_OLD_VAR]], %[[BETA_MUL_CORRECTED_VAR]] + + // CHECK: return %[[NEW_BATCH_MEAN]], %[[NEW_BATCH_VAR]], %[[MEAN]], %[[VAR]] + func.return %0#1, %0#2, %0#3, %0#4 : tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32> +} + +// ----- + +// CHECK-LABEL: fusedBatchNormV3_training_mixedPrecision +func.func @fusedBatchNormV3_training_mixedPrecision(%arg0: tensor<8x8x8x8xbf16>, %arg1: tensor<8xf32>, %arg2: tensor<8xf32>, %arg3: tensor<8xf32>, %arg4: tensor<8xf32>) -> (tensor<8x8x8x8xbf16>) { + // CHECK: stablehlo.convert %arg0 : (tensor<8x8x8x8xbf16>) -> tensor<8x8x8x8xf32> + %0:6 = "tf.FusedBatchNormV3"(%arg0, %arg1, %arg2, %arg3, %arg4) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", epsilon = 0.001 : f32, exponential_avg_factor = 1.0 : f32, is_training = true} : (tensor<8x8x8x8xbf16>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) -> (tensor<8x8x8x8xbf16>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) + // CHECK: stablehlo.convert {{.*}} : (tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xbf16> + func.return %0#0 : tensor<8x8x8x8xbf16> +} + +// ----- + +// CHECK-LABEL: fusedBatchNormV3_NCHW +func.func @fusedBatchNormV3_NCHW(%arg0: tensor<8x8x8x8xf32>, %arg1: tensor<8xf32>, %arg2: tensor<8xf32>, %arg3: tensor<8xf32>, %arg4: tensor<8xf32>) -> (tensor<8x8x8x8xf32>) { + // CHECK: "stablehlo.batch_norm_training"({{.*}}, %arg1, %arg2) <{epsilon = 1.000000e-03 : f32, feature_index = 1 : i64}> : (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>) -> (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>) + %0:6 = "tf.FusedBatchNormV3"(%arg0, %arg1, %arg2, %arg3, %arg4) {T = "tfdtype$DT_FLOAT", data_format = "NCHW", epsilon = 0.001 : f32, exponential_avg_factor = 1.0 : f32, is_training = true} : (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) -> (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) + func.return %0#0 : tensor<8x8x8x8xf32> +} + +// ----- + +// CHECK-LABEL: fusedBatchNormV3_NDHWC +func.func @fusedBatchNormV3_NDHWC(%arg0: tensor<8x8x8x8x8xf32>, %arg1: tensor<8xf32>, %arg2: tensor<8xf32>, %arg3: tensor<8xf32>, %arg4: tensor<8xf32>) -> (tensor<8x8x8x8x8xf32>) { + // CHECK: feature_index = 4 : i64 + %0:6 = "tf.FusedBatchNormV3"(%arg0, %arg1, %arg2, %arg3, %arg4) {T = "tfdtype$DT_FLOAT", data_format = "NDHWC", epsilon = 0.001 : f32, exponential_avg_factor = 1.0 : f32, is_training = true} : (tensor<8x8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) -> (tensor<8x8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) + func.return %0#0 : tensor<8x8x8x8x8xf32> +} + +// ----- + +// CHECK-LABEL: fusedBatchNormV3_noTraining_dynamic_supported +func.func @fusedBatchNormV3_noTraining_dynamic_supported(%arg0: tensor, %arg1: tensor, %arg2: tensor, %arg3: tensor, %arg4: tensor) -> (tensor) { + // CHECK: "stablehlo.batch_norm_inference"({{.*}}, %arg1, %arg2, %arg3, %arg4) <{epsilon = 1.000000e-03 : f32, feature_index = 1 : i64}> : (tensor, tensor, tensor, tensor, tensor) -> tensor + %0:6 = "tf.FusedBatchNormV3"(%arg0, %arg1, %arg2, %arg3, %arg4) {T = "tfdtype$DT_FLOAT", data_format = "NCHW", epsilon = 0.001 : f32, exponential_avg_factor = 1.0 : f32, is_training = false} : (tensor, tensor, tensor, tensor, tensor) -> (tensor, tensor, tensor, tensor, tensor, tensor) + func.return %0#0 : tensor +} + +// ----- + +// CHECK-LABEL: fusedBatchNormV3_training_dynamic_unsupported1 +func.func @fusedBatchNormV3_training_dynamic_unsupported1(%arg0: tensor, %arg1: tensor, %arg2: tensor, %arg3: tensor, %arg4: tensor) -> (tensor) { + // CHECK: tf.FusedBatchNormV3 + %0:6 = "tf.FusedBatchNormV3"(%arg0, %arg1, %arg2, %arg3, %arg4) {T = "tfdtype$DT_FLOAT", data_format = "NCHW", epsilon = 0.001 : f32, exponential_avg_factor = 1.0 : f32, is_training = true} : (tensor, tensor, tensor, tensor, tensor) -> (tensor, tensor, tensor, tensor, tensor, tensor) + func.return %0#0 : tensor +} + +// ----- + +// CHECK-LABEL: fusedBatchNormV3_training_dynamic_unsupported2 +func.func @fusedBatchNormV3_training_dynamic_unsupported2(%arg0: tensor, %arg1: tensor<6xf32>, %arg2: tensor<6xf32>, %arg3: tensor<6xf32>, %arg4: tensor<6xf32>) -> (tensor) { + // CHECK: tf.FusedBatchNormV3 + %0:6 = "tf.FusedBatchNormV3"(%arg0, %arg1, %arg2, %arg3, %arg4) {T = "tfdtype$DT_FLOAT", data_format = "NCHW", epsilon = 0.001 : f32, exponential_avg_factor = 1.0 : f32, is_training = true} : (tensor, tensor<6xf32>, tensor<6xf32>, tensor<6xf32>, tensor<6xf32>) -> (tensor, tensor<6xf32>, tensor<6xf32>, tensor<6xf32>, tensor<6xf32>, tensor<6xf32>) + func.return %0#0 : tensor +} + +// ----- + +// CHECK-LABEL: fusedBatchNormGrad_noTraining +func.func @fusedBatchNormGrad_noTraining(%arg0: tensor<8x8x8x8xf32>, %arg1: tensor<8x8x8x8xf32>, %arg2: tensor<8xf32>, %arg3: tensor<8xf32>, %arg4: tensor<8xf32>) -> (tensor<8x8x8x8xf32>) { + // CHECK-NEXT: %[[EPS:.*]] = stablehlo.constant dense<1.000000e-03> : tensor<8xf32> + + // CHECK-NEXT: %[[ADD:.*]] = stablehlo.add %arg4, %[[EPS]] : tensor<8xf32> + // CHECK-NEXT: %[[RSQRT:.*]] = stablehlo.rsqrt %[[ADD]] : tensor<8xf32> + + // CHECK-NEXT: %[[MUL2:.*]] = stablehlo.multiply %arg2, %[[RSQRT]] : tensor<8xf32> + // CHECK-NEXT: %[[BCAST_MUL2:.+]] = stablehlo.broadcast_in_dim %[[MUL2]], {{.*}} : (tensor<8xf32>) -> tensor<8x8x8x8xf32> + // CHECK-NEXT: %[[MUL3:.*]] = stablehlo.multiply %arg0, %[[BCAST_MUL2]] : tensor<8x8x8x8xf32> + // CHECK-NEXT: return %[[MUL3]] : tensor<8x8x8x8xf32> + + %0:5 = "tf.FusedBatchNormGrad"(%arg0, %arg1, %arg2, %arg3, %arg4) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", epsilon = 0.001 : f32, is_training = false} : (tensor<8x8x8x8xf32>, tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) -> (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) + func.return %0#0 : tensor<8x8x8x8xf32> +} + +// ----- + +// CHECK-LABEL: fusedBatchNormGrad_Training +func.func @fusedBatchNormGrad_Training(%arg0: tensor<8x8x8x8xf32>, %arg1: tensor<8x8x8x8xf32>, %arg2: tensor<8xf32>, %arg3: tensor<8xf32>, %arg4: tensor<8xf32>) -> (tensor<8x8x8x8xf32>) { + // CHECK-NEXT: %[[GRAD_OPERAND:.*]], %[[GRAD_SCALE:.*]], %[[GRAD_OFFSET:.*]] = "stablehlo.batch_norm_grad"(%arg1, %arg2, %arg3, %arg4, %arg0) {{.*}} + // CHECK-NEXT: return %[[GRAD_OPERAND]] : tensor<8x8x8x8xf32> + + %0:5 = "tf.FusedBatchNormGrad"(%arg0, %arg1, %arg2, %arg3, %arg4) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", epsilon = 0.001 : f32, is_training = true} : (tensor<8x8x8x8xf32>, tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) -> (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) + func.return %0#0 : tensor<8x8x8x8xf32> +} + +// ----- + +// CHECK-LABEL: fusedBatchNormGradV2_noTraining +func.func @fusedBatchNormGradV2_noTraining(%arg0: tensor<8x8x8x8xf32>, %arg1: tensor<8x8x8x8xf32>, %arg2: tensor<8xf32>, %arg3: tensor<8xf32>, %arg4: tensor<8xf32>) -> (tensor<8x8x8x8xf32>) { + // CHECK-NEXT: %[[EPS:.*]] = stablehlo.constant dense<1.000000e-03> : tensor<8xf32> + // CHECK-NEXT: %[[ADD:.*]] = stablehlo.add %arg4, %[[EPS]] : tensor<8xf32> + // CHECK-NEXT: %[[RSQRT:.*]] = stablehlo.rsqrt %[[ADD]] : tensor<8xf32> + // CHECK-NEXT: %[[MUL:.*]] = stablehlo.multiply %arg2, %[[RSQRT]] : tensor<8xf32> + // CHECK-NEXT: %[[BCAST:.*]] = stablehlo.broadcast_in_dim %[[MUL]], dims = [3] : (tensor<8xf32>) -> tensor<8x8x8x8xf32> + // CHECK-NEXT: %[[MUL2:.*]] = stablehlo.multiply %arg0, %[[BCAST]] : tensor<8x8x8x8xf32> + // CHECK-NEXT: return %[[MUL2:.*]] : tensor<8x8x8x8xf32> + + %0:5 = "tf.FusedBatchNormGradV2"(%arg0, %arg1, %arg2, %arg3, %arg4) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", epsilon = 0.001 : f32, is_training = false} : (tensor<8x8x8x8xf32>, tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) -> (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) + func.return %0#0 : tensor<8x8x8x8xf32> +} + +// ----- + +// CHECK-LABEL: fusedBatchNormGradV2_Training +func.func @fusedBatchNormGradV2_Training(%arg0: tensor<8x8x8x8xf32>, %arg1: tensor<8x8x8x8xf32>, %arg2: tensor<8xf32>, %arg3: tensor<8xf32>, %arg4: tensor<8xf32>) -> (tensor<8x8x8x8xf32>) { + // CHECK-NEXT: %[[GRAD_OPERAND:.*]], %[[GRAD_SCALE:.*]], %[[GRAD_OFFSET:.*]] = "stablehlo.batch_norm_grad"(%arg1, %arg2, %arg3, %arg4, %arg0) {{.*}} + // CHECK-NEXT: return %[[GRAD_OPERAND]] : tensor<8x8x8x8xf32> + + %0:5 = "tf.FusedBatchNormGradV2"(%arg0, %arg1, %arg2, %arg3, %arg4) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", epsilon = 0.001 : f32, is_training = true} : (tensor<8x8x8x8xf32>, tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) -> (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) + func.return %0#0 : tensor<8x8x8x8xf32> +} + +// ----- + +// CHECK-LABEL: fusedBatchNormGradV2_noTraining_mixed_precision +func.func @fusedBatchNormGradV2_noTraining_mixed_precision(%arg0: tensor<8x8x8x8xf32>, %arg1: tensor<8x8x8x8xbf16>, %arg2: tensor<8xf32>, %arg3: tensor<8xf32>, %arg4: tensor<8xf32>) -> (tensor<8x8x8x8xbf16>) { + // CHECK-NEST: %[[CST:.*]] = stablehlo.constant dense<1.000000e-03> : tensor<8xf32> + // CHECK-NEST: %[[ADD:.*]] = stablehlo.add %arg4, %[[CST]] : tensor<8xf32> + // CHECK-NEST: %[[RSQRT:.*]] = stablehlo.rsqrt %[[ADD]] : tensor<8xf32> + // CHECK-NEST: %[[MUL:.*]] = stablehlo.multiply %arg2, %[[RSQRT]] : tensor<8xf32> + // CHECK-NEST: %[[BCAST:.*]] = stablehlo.broadcast_in_dim %[[MUL]], dims = [3] : (tensor<8xf32>) -> tensor<8x8x8x8xf32> + // CHECK-NEST: %[[MUL2:.*]] = stablehlo.multiply %arg0, %[[BCAST]] : tensor<8x8x8x8xf32> + // CHECK-NEST: %[[CONVERT:.*]] = stablehlo.convert %[[MUL2]] : (tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xbf16> + // CHECK-NEST: return %[[CONVERT]] : tensor<8x8x8x8xbf16> + + %0:5 = "tf.FusedBatchNormGradV2"(%arg0, %arg1, %arg2, %arg3, %arg4) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", epsilon = 0.001 : f32, is_training = false} : (tensor<8x8x8x8xf32>, tensor<8x8x8x8xbf16>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) -> (tensor<8x8x8x8xbf16>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) + func.return %0#0 : tensor<8x8x8x8xbf16> +} + +// ----- + +// CHECK-LABEL: fusedBatchNormGradV2_Training_mixed_precision +func.func @fusedBatchNormGradV2_Training_mixed_precision(%arg0: tensor<8x8x8x8xf32>, %arg1: tensor<8x8x8x8xbf16>, %arg2: tensor<8xf32>, %arg3: tensor<8xf32>, %arg4: tensor<8xf32>) -> (tensor<8x8x8x8xbf16>) { + // CHECK-NEXT: %[[CONVERT:.*]] = stablehlo.convert %arg1 : (tensor<8x8x8x8xbf16>) -> tensor<8x8x8x8xf32> + // CHECK-NEXT: %[[GRAD_OPERAND:.*]], %[[GRAD_SCALE:.*]], %[[GRAD_OFFSET:.*]] = "stablehlo.batch_norm_grad"(%[[CONVERT]], %arg2, %arg3, %arg4, %arg0) <{epsilon = 1.000000e-03 : f32, feature_index = 3 : i64}> : (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8x8x8x8xf32>) -> (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>) + // CHECK-NEXT: %[[CONVERT:.*]] = stablehlo.convert %[[GRAD_OPERAND]] : (tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xbf16> + // CHECK-NEXT: return %[[CONVERT]] : tensor<8x8x8x8xbf16> + + %0:5 = "tf.FusedBatchNormGradV2"(%arg0, %arg1, %arg2, %arg3, %arg4) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", epsilon = 0.001 : f32, is_training = true} : (tensor<8x8x8x8xf32>, tensor<8x8x8x8xbf16>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) -> (tensor<8x8x8x8xbf16>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) + func.return %0#0 : tensor<8x8x8x8xbf16> +} + +// ----- + +// CHECK-LABEL: fusedBatchNormGradV3_noTraining +func.func @fusedBatchNormGradV3_noTraining(%arg0: tensor<8x8x8x8xf32>, %arg1: tensor<8x8x8x8xf32>, %arg2: tensor<8xf32>, %arg3: tensor<8xf32>, %arg4: tensor<8xf32>, %arg5: tensor<8xf32>) -> (tensor<8x8x8x8xf32>) { +// CHECK-NEXT: %[[EPS:.*]] = stablehlo.constant dense<1.000000e-03> : tensor<8xf32> +// CHECK-NEXT: %[[ADD:.*]] = stablehlo.add %arg4, %[[EPS]] : tensor<8xf32> +// CHECK-NEXT: %[[RSQRT:.*]] = stablehlo.rsqrt %[[ADD]] : tensor<8xf32> +// CHECK-NEXT: %[[MUL:.*]] = stablehlo.multiply %arg2, %[[RSQRT]] : tensor<8xf32> +// CHECK-NEXT: %[[BCAST:.*]] = stablehlo.broadcast_in_dim %[[MUL]], dims = [3] : (tensor<8xf32>) -> tensor<8x8x8x8xf32> +// CHECK-NEXT: %[[MUL2:.*]] = stablehlo.multiply %arg0, %[[BCAST]] : tensor<8x8x8x8xf32> +// CHECK-NEXT: return %[[MUL2]] : tensor<8x8x8x8xf32> + + %0:5 = "tf.FusedBatchNormGradV3"(%arg0, %arg1, %arg2, %arg3, %arg4, %arg5) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", epsilon = 0.001 : f32, is_training = false} : (tensor<8x8x8x8xf32>, tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) -> (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) + func.return %0#0 : tensor<8x8x8x8xf32> +} + +// ----- + +// CHECK-LABEL: fusedBatchNormGradV3_Training +func.func @fusedBatchNormGradV3_Training(%arg0: tensor<8x8x8x8xf32>, %arg1: tensor<8x8x8x8xf32>, %arg2: tensor<8xf32>, %arg3: tensor<8xf32>, %arg4: tensor<8xf32>, %arg5: tensor<8xf32>) -> (tensor<8x8x8x8xf32>, tensor<0xf32>, tensor<*xf32>) { + // CHECK-NEXT: %[[EPS:.*]] = stablehlo.constant dense<0.000000e+00> : tensor<0xf32> + // CHECK-NEXT: %[[GRAD_OPERAND:.*]], %[[GRAD_SCALE:.*]], %[[GRAD_OFFSET:.*]] = "stablehlo.batch_norm_grad"(%arg1, %arg2, %arg3, %arg4, %arg0) <{epsilon = 1.000000e-03 : f32, feature_index = 3 : i64}> : (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8x8x8x8xf32>) -> (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>) + // CHECK-NEXT: %[[CAST:.*]] = tensor.cast %[[EPS]] : tensor<0xf32> to tensor<*xf32> + // CHECK-NEXT: return %[[GRAD_OPERAND]], %[[EPS]], %[[CAST]] : tensor<8x8x8x8xf32>, tensor<0xf32>, tensor<*xf32> + + %0:5 = "tf.FusedBatchNormGradV3"(%arg0, %arg1, %arg2, %arg3, %arg4, %arg5) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", epsilon = 0.001 : f32, is_training = true} : (tensor<8x8x8x8xf32>, tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) -> (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<0xf32>, tensor<*xf32>) + func.return %0#0, %0#3, %0#4 : tensor<8x8x8x8xf32>, tensor<0xf32>, tensor<*xf32> +} + +// ----- + +// CHECK-LABEL: fusedBatchNormGradV3_noTraining_mixed_precision +func.func @fusedBatchNormGradV3_noTraining_mixed_precision(%arg0: tensor<8x8x8x8xf32>, %arg1: tensor<8x8x8x8xbf16>, %arg2: tensor<8xf32>, %arg3: tensor<8xf32>, %arg4: tensor<8xf32>, %arg5: tensor<8xf32>) -> (tensor<8x8x8x8xbf16>) { + // CHECK-NEXT: %[[EPS:.*]] = stablehlo.constant dense<1.000000e-03> : tensor<8xf32> + // CHECK-NEXT: %[[ADD:.*]] = stablehlo.add %arg4, %[[EPS]] : tensor<8xf32> + // CHECK-NEXT: %[[RSQRT:.*]] = stablehlo.rsqrt %[[ADD]] : tensor<8xf32> + // CHECK-NEXT: %[[MUL:.*]] = stablehlo.multiply %arg2, %[[RSQRT]] : tensor<8xf32> + // CHECK-NEXT: %[[BCAST:.*]] = stablehlo.broadcast_in_dim %[[MUL]], dims = [3] : (tensor<8xf32>) -> tensor<8x8x8x8xf32> + // CHECK-NEXT: %[[MUL2:.*]] = stablehlo.multiply %arg0, %[[BCAST]] : tensor<8x8x8x8xf32> + // CHECK-NEXT: %[[CONVERT:.*]] = stablehlo.convert %[[MUL2]] : (tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xbf16> + // CHECK-NEXT: return %[[CONVERT]] : tensor<8x8x8x8xbf16> + + %0:5 = "tf.FusedBatchNormGradV3"(%arg0, %arg1, %arg2, %arg3, %arg4, %arg5) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", epsilon = 0.001 : f32, is_training = false} : (tensor<8x8x8x8xf32>, tensor<8x8x8x8xbf16>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) -> (tensor<8x8x8x8xbf16>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) + func.return %0#0 : tensor<8x8x8x8xbf16> +} + +// ----- + +// CHECK-LABEL: fusedBatchNormGradV3_Training_mixed_precision +func.func @fusedBatchNormGradV3_Training_mixed_precision(%arg0: tensor<8x8x8x8xf32>, %arg1: tensor<8x8x8x8xbf16>, %arg2: tensor<8xf32>, %arg3: tensor<8xf32>, %arg4: tensor<8xf32>, %arg5: tensor<8xf32>) -> (tensor<8x8x8x8xbf16>) { + // CHECK-NEXT: %[[CONVERT:.*]] = stablehlo.convert %arg1 : (tensor<8x8x8x8xbf16>) -> tensor<8x8x8x8xf32> + // CHECK-NEXT: %[[GRAD_OPERAND:.*]], %[[GRAD_SCALE:.*]], %[[GRAD_OFFSET:.*]] = "stablehlo.batch_norm_grad"(%[[CONVERT]], %arg2, %arg3, %arg4, %arg0) <{epsilon = 1.000000e-03 : f32, feature_index = 3 : i64}> : (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8x8x8x8xf32>) -> (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>) + // CHECK-NEXT: %[[CONVERT2:.*]] = stablehlo.convert %[[GRAD_OPERAND]] : (tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xbf16> + // CHECK-NEXT: return %[[CONVERT2]] : tensor<8x8x8x8xbf16> + + %0:5 = "tf.FusedBatchNormGradV3"(%arg0, %arg1, %arg2, %arg3, %arg4, %arg5) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", epsilon = 0.001 : f32, is_training = true} : (tensor<8x8x8x8xf32>, tensor<8x8x8x8xbf16>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) -> (tensor<8x8x8x8xbf16>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) + func.return %0#0 : tensor<8x8x8x8xbf16> +} + +// ----- + +// CHECK-LABEL: fusedBatchNormGradV3_noTraining_NCHW +func.func @fusedBatchNormGradV3_noTraining_NCHW(%arg0: tensor<8x8x8x8xf32>, %arg1: tensor<8x8x8x8xf32>, %arg2: tensor<8xf32>, %arg3: tensor<8xf32>, %arg4: tensor<8xf32>, %arg5: tensor<8xf32>) -> (tensor<8x8x8x8xf32>) { + // CHECK-NEXT: %[[EPS:.*]] = stablehlo.constant dense<1.000000e-03> : tensor<8xf32> + // CHECK-NEXT: %[[ADD:.*]] = stablehlo.add %arg4, %[[EPS]] : tensor<8xf32> + // CHECK-NEXT: %[[RSQRT:.*]] = stablehlo.rsqrt %[[ADD]] : tensor<8xf32> + // CHECK-NEXT: %[[MUL:.*]] = stablehlo.multiply %arg2, %[[RSQRT]] : tensor<8xf32> + // CHECK-NEXT: %[[BCAST:.*]] = stablehlo.broadcast_in_dim %[[MUL]], dims = [1] : (tensor<8xf32>) -> tensor<8x8x8x8xf32> + // CHECK-NEXT: %[[MUL2:.*]] = stablehlo.multiply %arg0, %[[BCAST]] : tensor<8x8x8x8xf32> + // CHECK-NEXT: return %[[MUL2]] : tensor<8x8x8x8xf32> + + %0:5 = "tf.FusedBatchNormGradV3"(%arg0, %arg1, %arg2, %arg3, %arg4, %arg5) {T = "tfdtype$DT_FLOAT", data_format = "NCHW", epsilon = 0.001 : f32, is_training = false} : (tensor<8x8x8x8xf32>, tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) -> (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) + func.return %0#0 : tensor<8x8x8x8xf32> +} + +// ----- + +// CHECK-LABEL: fusedBatchNormGradV3_Training_NCHW +func.func @fusedBatchNormGradV3_Training_NCHW(%arg0: tensor<8x8x8x8xf32>, %arg1: tensor<8x8x8x8xf32>, %arg2: tensor<8xf32>, %arg3: tensor<8xf32>, %arg4: tensor<8xf32>, %arg5: tensor<8xf32>) -> (tensor<8x8x8x8xf32>) { + // CHECK-NEXT: %[[GRAD_OPERAND:.*]], %[[GRAD_SCALE:.*]], %[[GRAD_OFFSET:.*]] = "stablehlo.batch_norm_grad"(%arg1, %arg2, %arg3, %arg4, %arg0) <{epsilon = 1.000000e-03 : f32, feature_index = 1 : i64}> : (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8x8x8x8xf32>) -> (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>) + // CHECK-NEXT: return %[[GRAD_OPERAND]] : tensor<8x8x8x8xf32> + %0:5 = "tf.FusedBatchNormGradV3"(%arg0, %arg1, %arg2, %arg3, %arg4, %arg5) {T = "tfdtype$DT_FLOAT", data_format = "NCHW", epsilon = 0.001 : f32, is_training = true} : (tensor<8x8x8x8xf32>, tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) -> (tensor<8x8x8x8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>, tensor<8xf32>) + func.return %0#0 : tensor<8x8x8x8xf32> +} + +//===----------------------------------------------------------------------===// +// Bias op legalizations. +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: func @biasAdd_default +func.func @biasAdd_default(%arg0: tensor<1x32x10x32xi32>, %arg1: tensor<32xi32>) -> tensor<1x32x10x32xi32> { + // CHECK-NEXT: %[[BCAST:.*]] = stablehlo.broadcast_in_dim %arg1, dims = [3] : (tensor<32xi32>) -> tensor<1x32x10x32xi32> + // CHECK-NEXT: %[[ADD:.*]] = stablehlo.add %arg0, %[[BCAST]] : tensor<1x32x10x32xi32> + // CHECK-NEXT: return %[[ADD]] : tensor<1x32x10x32xi32> + %0 = "tf.BiasAdd"(%arg0, %arg1) {T = "tfdtype$DT_FLOAT"} : (tensor<1x32x10x32xi32>, tensor<32xi32>) -> tensor<1x32x10x32xi32> + func.return %0 : tensor<1x32x10x32xi32> +} + +// ----- + +// CHECK-LABEL: func @biasAdd_NHWC +func.func @biasAdd_NHWC(%arg0: tensor<1x32x10x32xi32>, %arg1: tensor<32xi32>) -> tensor<1x32x10x32xi32> { + // CHECK-NEXT: %[[BCAST:.*]] = stablehlo.broadcast_in_dim %arg1, dims = [3] : (tensor<32xi32>) -> tensor<1x32x10x32xi32> + // CHECK-NEXT: %[[ADD:.*]] = stablehlo.add %arg0, %[[BCAST]] : tensor<1x32x10x32xi32> + // CHECK-NEXT: return %[[ADD]] : tensor<1x32x10x32xi32> + %0 = "tf.BiasAdd"(%arg0, %arg1) {T = "tfdtype$DT_FLOAT", data_format = "NHWC"} : (tensor<1x32x10x32xi32>, tensor<32xi32>) -> tensor<1x32x10x32xi32> + func.return %0 : tensor<1x32x10x32xi32> +} + +// ----- + +// CHECK-LABEL: func @biasAdd_NCHW +func.func @biasAdd_NCHW(%arg0: tensor<1x32x10x32xi32>, %arg1: tensor<32xi32>) -> tensor<1x32x10x32xi32> { + // CHECK-NEXT: %[[BCAST:.*]] = stablehlo.broadcast_in_dim %arg1, dims = [1] : (tensor<32xi32>) -> tensor<1x32x10x32xi32> + // CHECK-NEXT: %[[ADD:.*]] = stablehlo.add %arg0, %[[BCAST]] : tensor<1x32x10x32xi32> + // CHECK-NEXT: return %[[ADD]] : tensor<1x32x10x32xi32> + %0 = "tf.BiasAdd"(%arg0, %arg1) {T = "tfdtype$DT_FLOAT", data_format = "NCHW"} : (tensor<1x32x10x32xi32>, tensor<32xi32>) -> tensor<1x32x10x32xi32> + func.return %0 : tensor<1x32x10x32xi32> +} + +// ----- + +// CHECK-LABEL: func @biasAdd_dynamic +func.func @biasAdd_dynamic(%arg0: tensor, %arg1: tensor) -> tensor { + // CHECK-NEXT: %[[SHAPE:.*]] = shape.shape_of %arg0 : tensor -> tensor<4xindex> + // CHECK-NEXT: %[[BCAST:.*]] = stablehlo.dynamic_broadcast_in_dim %arg1, %[[SHAPE]], dims = [1] : (tensor, tensor<4xindex>) -> tensor + // CHECK-NEXT: %[[ADD:.*]] = stablehlo.add %arg0, %[[BCAST]] : tensor + // CHECK-NEXT: return %[[ADD]] : tensor + %0 = "tf.BiasAdd"(%arg0, %arg1) {data_format = "NCHW"} : (tensor, tensor) -> tensor + func.return %0 : tensor +} + +// ----- + +// CHECK-LABEL: func @biasAdd_partial_dynamic +func.func @biasAdd_partial_dynamic(%arg0: tensor, %arg1: tensor<512xi32>) -> tensor { + // CHECK-NEXT: %[[SHAPE:.*]] = shape.shape_of %arg0 : tensor -> tensor<4xindex> + // CHECK-NEXT: %[[BCAST:.*]] = stablehlo.dynamic_broadcast_in_dim %arg1, %[[SHAPE]], dims = [3] : (tensor<512xi32>, tensor<4xindex>) -> tensor + // CHECK-NEXT: %[[ADD:.*]] = stablehlo.add %arg0, %[[BCAST]] : tensor + // CHECK-NEXT: %[[CAST:.*]] = tensor.cast %[[ADD]] : tensor to tensor + // CHECK-NEXT: return %[[CAST]] : tensor + %0 = "tf.BiasAdd"(%arg0, %arg1) {data_format = "NHWC"} : (tensor, tensor<512xi32>) -> tensor + func.return %0 : tensor +} + + +//===----------------------------------------------------------------------===// +// ClipByValue +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: @clip +func.func @clip(%arg0 : tensor, %arg1 : tensor, %arg2 : tensor) -> tensor { + // CHECK: [[VAL:%.+]] = stablehlo.clamp %arg1, %arg0, %arg2 + + %0 = "tf.ClipByValue"(%arg0, %arg1, %arg2) : (tensor, tensor, tensor) -> tensor + // CHECK: return [[VAL]] + func.return %0 : tensor +} + +// ----- + +// CHECK-LABEL: @clip_dynamic +func.func @clip_dynamic(%arg0 : tensor, %arg1 : tensor, %arg2 : tensor) -> tensor { + // CHECK-DAG: [[CLAMP:%.+]] = stablehlo.clamp %arg1, %arg0, %arg2 + %0 = "tf.ClipByValue"(%arg0, %arg1, %arg2) : (tensor, tensor, tensor) -> tensor + + // CHECK: return [[CLAMP]] + func.return %0 : tensor +} + +// ----- + +// CHECK-LABEL: @clip_static_broadcast +func.func @clip_static_broadcast(%arg0 : tensor<5xf32>, %arg1 : tensor, %arg2 : tensor) -> tensor<5xf32> { + // CHECK-DAG: [[BROADCAST_MIN:%.+]] = stablehlo.broadcast_in_dim %arg1, dims = [] : (tensor) -> tensor<5xf32> + // CHECK-DAG: [[BROADCAST_MAX:%.+]] = stablehlo.broadcast_in_dim %arg2, dims = [] : (tensor) -> tensor<5xf32> + // CHECK-DAG: [[CLAMP:%.+]] = stablehlo.clamp [[BROADCAST_MIN]], %arg0, [[BROADCAST_MAX]] + %0 = "tf.ClipByValue"(%arg0, %arg1, %arg2) : (tensor<5xf32>, tensor, tensor) -> tensor<5xf32> + + // CHECK: return [[CLAMP]] + func.return %0 : tensor<5xf32> +} + + +// CHECK-LABEL: @clip_dynamic_broadcast +func.func @clip_dynamic_broadcast(%arg0 : tensor, %arg1 : tensor, %arg2 : tensor) -> tensor { + // CHECK: [[SHP:%.+]] = shape.shape_of %arg0 + // CHECK: [[SHPIDX:%.+]] = arith.index_cast [[SHP]] : tensor<1xindex> to tensor<1xi32> + // CHECK-DAG: [[BROADCAST_MIN:%.+]] = stablehlo.dynamic_broadcast_in_dim %arg1, [[SHPIDX]], dims = [] : (tensor, tensor<1xi32>) -> tensor + // CHECK-DAG: [[BROADCAST_MAX:%.+]] = stablehlo.dynamic_broadcast_in_dim %arg2, [[SHPIDX]], dims = [] : (tensor, tensor<1xi32>) -> tensor + // CHECK-DAG: [[CLAMP:%.+]] = stablehlo.clamp [[BROADCAST_MIN]], %arg0, [[BROADCAST_MAX]] + %0 = "tf.ClipByValue"(%arg0, %arg1, %arg2) : (tensor, tensor, tensor) -> tensor + + // CHECK: return [[CLAMP]] + func.return %0 : tensor +} + +//===----------------------------------------------------------------------===// +// DiagPart +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: func @diag_part +// CHECK-SAME: %[[ARG:.*]]: tensor<4x3x4x3xf32> +func.func @diag_part(%arg0: tensor<4x3x4x3xf32>) -> tensor<4x3xf32> { + // CHECK-NEXT: %[[CST0:.*]] = stablehlo.constant dense<0.000000e+00> : tensor<12x12xf32> + // CHECK-NEXT: %[[CST1:.*]] = stablehlo.constant dense<0.000000e+00> : tensor + // CHECK-NEXT: %[[RESHAPE:.*]] = stablehlo.reshape %arg0 : (tensor<4x3x4x3xf32>) -> tensor<12x12xf32> + // CHECK-NEXT: %[[IOTA:.*]] = stablehlo.iota dim = 0 : tensor<12xi32> + // CHECK-NEXT: %[[BCAST:.*]] = stablehlo.broadcast_in_dim %[[IOTA]], dims = [0] : (tensor<12xi32>) -> tensor<12x12xi32> + // CHECK-NEXT: %[[IOTA2:.*]] = stablehlo.iota dim = 0 : tensor<12xi32> + // CHECK-NEXT: %[[BCAST2:.*]] = stablehlo.broadcast_in_dim %[[IOTA2]], dims = [1] : (tensor<12xi32>) -> tensor<12x12xi32> + // CHECK-NEXT: %[[CMP:.*]] = stablehlo.compare EQ, %[[BCAST]], %[[BCAST2]], NOTYPE : (tensor<12x12xi32>, tensor<12x12xi32>) -> tensor<12x12xi1> + // CHECK-NEXT: %[[SEL:.*]] = stablehlo.select %[[CMP]], %[[RESHAPE]], %[[CST0]] : tensor<12x12xi1>, tensor<12x12xf32> + // CHECK-NEXT: %[[REDUCE:.*]] = stablehlo.reduce(%[[SEL]] init: %[[CST1]]) applies stablehlo.add across dimensions = [0] : (tensor<12x12xf32>, tensor) -> tensor<12xf32> + // CHECK-NEXT: %[[RESHAPE2:.*]] = stablehlo.reshape %[[REDUCE]] : (tensor<12xf32>) -> tensor<4x3xf32> + // CHECK-NEXT: return %[[RESHAPE2]] : tensor<4x3xf32> + + %0 = "tf.DiagPart"(%arg0) : (tensor<4x3x4x3xf32>) -> tensor<4x3xf32> + func.return %0: tensor<4x3xf32> +} + +//===----------------------------------------------------------------------===// +// MatrixDiagPart +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: func @matrix_diag_part +// CHECK-SAME: %[[ARG:.*]]: tensor<7x140x128xi32> +func.func @matrix_diag_part(%arg0: tensor<7x140x128xi32>) -> tensor<7x22x128xi32> { + // CHECK-NEXT: %[[CST0:.*]] = stablehlo.constant dense<42> : tensor<7x22x128xi32> + // CHECK-NEXT: %[[CST1:.*]] = stablehlo.constant dense<128> : tensor<1x22x128xi32> + // CHECK-NEXT: %[[CST2:.*]] = stablehlo.constant dense<140> : tensor<1x22x128xi32> + // CHECK-NEXT: %[[CST3:.*]] = stablehlo.constant dense<11> : tensor<1x22x128xi32> + // CHECK-NEXT: %[[CST4:.*]] = stablehlo.constant dense<0> : tensor<1x22x128xi32> + // CHECK-NEXT: %[[IOTA0:.*]] = stablehlo.iota dim = 0 : tensor<22xi32> + // CHECK-NEXT: %[[BCAST0:.*]] = stablehlo.broadcast_in_dim %[[IOTA0]], dims = [1] : (tensor<22xi32>) -> tensor<1x22x128xi32> + // CHECK-NEXT: %[[IOTA1:.*]] = stablehlo.iota dim = 0 : tensor<128xi32> + // CHECK-NEXT: %[[BCAST1:.*]] = stablehlo.broadcast_in_dim %[[IOTA1]], dims = [2] : (tensor<128xi32>) -> tensor<1x22x128xi32> + // CHECK-NEXT: %[[SUB0:.*]] = stablehlo.subtract %[[CST3]], %[[BCAST0]] : tensor<1x22x128xi32> + // CHECK-NEXT: %[[NEG0:.*]] = stablehlo.negate %[[SUB0]] : tensor<1x22x128xi32> + // CHECK-NEXT: %[[MIN0:.*]] = stablehlo.minimum %[[SUB0]], %[[CST4]] : tensor<1x22x128xi32> + // CHECK-NEXT: %[[ADD0:.*]] = stablehlo.add %[[MIN0]], %[[CST2]] : tensor<1x22x128xi32> + // CHECK-NEXT: %[[MAX0:.*]] = stablehlo.maximum %[[SUB0]], %[[CST4]] : tensor<1x22x128xi32> + // CHECK-NEXT: %[[SUB1:.*]] = stablehlo.subtract %[[CST1]], %[[MAX0]] : tensor<1x22x128xi32> + // CHECK-NEXT: %[[MIN1:.*]] = stablehlo.minimum %[[ADD0]], %[[SUB1]] : tensor<1x22x128xi32> + // CHECK-NEXT: %[[CMP0:.*]] = stablehlo.compare GE, %[[SUB0]], %[[CST4]] : (tensor<1x22x128xi32>, tensor<1x22x128xi32>) -> tensor<1x22x128xi1> + // CHECK-NEXT: %[[SUB2:.*]] = stablehlo.subtract %[[CST1]], %[[MIN1]] : tensor<1x22x128xi32> + // CHECK-NEXT: %[[SELECT0:.*]] = stablehlo.select %[[CMP0]], %[[SUB2]], %[[CST4]] : tensor<1x22x128xi1>, tensor<1x22x128xi32> + // CHECK-NEXT: %[[MAX1:.*]] = stablehlo.maximum %[[SUB0]], %[[CST4]] : tensor<1x22x128xi32> + // CHECK-NEXT: %[[SUB2:.*]] = stablehlo.subtract %[[MAX1]], %[[SELECT0]] : tensor<1x22x128xi32> + // CHECK-NEXT: %[[MAX2:.*]] = stablehlo.maximum %[[NEG0]], %[[CST4]] : tensor<1x22x128xi32> + // CHECK-NEXT: %[[SUB3:.*]] = stablehlo.subtract %[[MAX2]], %[[SELECT0]] : tensor<1x22x128xi32> + // CHECK-NEXT: %[[ADD1:.*]] = stablehlo.add %[[BCAST1]], %[[SUB2]] : tensor<1x22x128xi32> + // CHECK-NEXT: %[[ADD2:.*]] = stablehlo.add %[[BCAST1]], %[[SUB3]] : tensor<1x22x128xi32> + // CHECK-NEXT: %[[CMP1:.*]] = stablehlo.compare GE, %[[ADD1]], %[[CST4]] : (tensor<1x22x128xi32>, tensor<1x22x128xi32>) -> tensor<1x22x128xi1> + // CHECK-NEXT: %[[CMP2:.*]] = stablehlo.compare LT, %[[ADD1]], %[[CST1]] : (tensor<1x22x128xi32>, tensor<1x22x128xi32>) -> tensor<1x22x128xi1> + // CHECK-NEXT: %[[AND0:.*]] = stablehlo.and %[[CMP1]], %[[CMP2]] : tensor<1x22x128xi1> + // CHECK-NEXT: %[[CMP3:.*]] = stablehlo.compare GE, %[[ADD2]], %[[CST4]] : (tensor<1x22x128xi32>, tensor<1x22x128xi32>) -> tensor<1x22x128xi1> + // CHECK-NEXT: %[[CMP4:.*]] = stablehlo.compare LT, %[[ADD2]], %[[CST2]] : (tensor<1x22x128xi32>, tensor<1x22x128xi32>) -> tensor<1x22x128xi1> + // CHECK-NEXT: %[[AND1:.*]] = stablehlo.and %[[CMP3]], %[[CMP4]] : tensor<1x22x128xi1> + // CHECK-NEXT: %[[AND2:.*]] = stablehlo.and %[[AND0]], %[[AND1]] : tensor<1x22x128xi1> + // CHECK-NEXT: %[[RESHAPE0:.*]] = stablehlo.reshape %[[AND2]] : (tensor<1x22x128xi1>) -> tensor<22x128xi1> + // CHECK-NEXT: %[[CONCAT0:.*]] = stablehlo.concatenate %[[ADD2]], %[[ADD1]], dim = 0 : (tensor<1x22x128xi32>, tensor<1x22x128xi32>) -> tensor<2x22x128xi32> + // CHECK-NEXT: %[[GATHER0:.*]] = "stablehlo.gather"(%arg0, %[[CONCAT0]]) <{dimension_numbers = #{{.*}}, indices_are_sorted = false, slice_sizes = array}> : (tensor<7x140x128xi32>, tensor<2x22x128xi32>) -> tensor<7x22x128xi32> + // CHECK-NEXT: %[[BCAST1:.*]] = stablehlo.broadcast %[[RESHAPE0]], sizes = [7] : (tensor<22x128xi1>) -> tensor<7x22x128xi1> + // CHECK-NEXT: %[[SELECT1:.*]] = stablehlo.select %[[BCAST1]], %[[GATHER0]], %[[CST0]] : tensor<7x22x128xi1>, tensor<7x22x128xi32> + // CHECK-NEXT: return %[[SELECT1]] : tensor<7x22x128xi32> + + %0 = mhlo.constant dense<42> : tensor // padding value + %1 = mhlo.constant dense<[-10, 11]> : tensor<2xi32> // k + %2 = "tf.MatrixDiagPartV3"(%arg0, %1, %0) { + T = i32, align = "RIGHT_LEFT" + } : (tensor<7x140x128xi32>, tensor<2xi32>, tensor) -> tensor<7x22x128xi32> + func.return %2: tensor<7x22x128xi32> +} + +// ----- + +// CHECK-LABEL: func @matrix_diag_part_zero_dim_complex +func.func @matrix_diag_part_zero_dim_complex(%arg0: tensor<4x0xcomplex>) -> tensor<0xcomplex> { + %cst = "tf.Const"() {value = dense<-3> : tensor} : () -> tensor + %cst_0 = "tf.Const"() {value = dense<(0.000000e+00,0.000000e+00)> : tensor>} : () -> tensor> + %0 = "tf.MatrixDiagPartV3"(%arg0, %cst, %cst_0) {align = "RIGHT_LEFT", device = ""} : (tensor<4x0xcomplex>, tensor, tensor>) -> tensor<0xcomplex> + // CHECK: return %{{[0-9]*}} : tensor<0xcomplex> + return %0 : tensor<0xcomplex> +} + +// ----- + +// CHECK-LABEL: func @matrix_diag_part_single_diagonal +func.func @matrix_diag_part_single_diagonal(%arg0: tensor<7x140x128xi32>) -> tensor<7x128xi32> { + // CHECK-NEXT: %[[CST0:.*]] = stablehlo.constant dense<42> : tensor<7x1x128xi32> + // CHECK-NEXT: %[[CST1:.*]] = stablehlo.constant dense<128> : tensor<1x1x128xi32> + // CHECK-NEXT: %[[CST2:.*]] = stablehlo.constant dense<140> : tensor<1x1x128xi32> + // CHECK-NEXT: %[[FALSE:.*]] = stablehlo.constant dense<0> : tensor<1x1x128xi32> + // CHECK-NEXT: %[[IOTA0:.*]] = stablehlo.iota dim = 0 : tensor<128xi32> + // CHECK-NEXT: %[[RESHAPE0:.*]] = stablehlo.reshape %[[IOTA0]] : (tensor<128xi32>) -> tensor<1x1x128xi32> + // CHECK-NEXT: %[[CMP0:.*]] = stablehlo.compare GE, %[[RESHAPE0]], %[[FALSE]] : (tensor<1x1x128xi32>, tensor<1x1x128xi32>) -> tensor<1x1x128xi1> + // CHECK-NEXT: %[[CMP1:.*]] = stablehlo.compare LT, %[[RESHAPE0]], %[[CST1]] : (tensor<1x1x128xi32>, tensor<1x1x128xi32>) -> tensor<1x1x128xi1> + // CHECK-NEXT: %[[AND0:.*]] = stablehlo.and %[[CMP0]], %[[CMP1]] : tensor<1x1x128xi1> + // CHECK-NEXT: %[[CMP2:.*]] = stablehlo.compare GE, %[[RESHAPE0]], %[[FALSE]] : (tensor<1x1x128xi32>, tensor<1x1x128xi32>) -> tensor<1x1x128xi1> + // CHECK-NEXT: %[[CMP3:.*]] = stablehlo.compare LT, %[[RESHAPE0]], %[[CST2]] : (tensor<1x1x128xi32>, tensor<1x1x128xi32>) -> tensor<1x1x128xi1> + // CHECK-NEXT: %[[AND1:.*]] = stablehlo.and %[[CMP2]], %[[CMP3]] : tensor<1x1x128xi1> + // CHECK-NEXT: %[[AND2:.*]] = stablehlo.and %[[AND0]], %[[AND1]] : tensor<1x1x128xi1> + // CHECK-NEXT: %[[RESHAPE1:.*]] = stablehlo.reshape %[[AND2]] : (tensor<1x1x128xi1>) -> tensor<1x128xi1> + // CHECK-NEXT: %[[CONCAT:.*]] = stablehlo.concatenate %[[RESHAPE0]], %[[RESHAPE0]], dim = 0 : (tensor<1x1x128xi32>, tensor<1x1x128xi32>) -> tensor<2x1x128xi32> + // CHECK-NEXT: %[[GATHER:.*]] = "stablehlo.gather"(%arg0, %[[CONCAT]]) <{dimension_numbers = #{{.*}}, indices_are_sorted = false, slice_sizes = array}> : (tensor<7x140x128xi32>, tensor<2x1x128xi32>) -> tensor<7x1x128xi32> + // CHECK-NEXT: %[[BCAST:.*]] = stablehlo.broadcast %[[RESHAPE1]], sizes = [7] : (tensor<1x128xi1>) -> tensor<7x1x128xi1> + // CHECK-NEXT: %[[SELECT0:.*]] = stablehlo.select %[[BCAST]], %[[GATHER]], %[[CST0]] : tensor<7x1x128xi1>, tensor<7x1x128xi32> + // CHECK-NEXT: %[[RESHAPE2:.*]] = stablehlo.reshape %[[SELECT0]] : (tensor<7x1x128xi32>) -> tensor<7x128xi32> + // CHECK-NEXT: return %[[RESHAPE2]] : tensor<7x128xi32> + + %0 = mhlo.constant dense<42> : tensor // padding value + %1 = mhlo.constant dense<0> : tensor<2xi32> // k + %2 = "tf.MatrixDiagPartV3"(%arg0, %1, %0) { + T = i32, align = "RIGHT_LEFT" + } : (tensor<7x140x128xi32>, tensor<2xi32>, tensor) -> tensor<7x128xi32> + func.return %2: tensor<7x128xi32> +} + +// ----- + +// CHECK-LABEL: func @matrix_diag_part_align_ll +func.func @matrix_diag_part_align_ll(%arg0: tensor<7x140x128xi32>) -> tensor<7x22x128xi32> { + // CHECK-NEXT: %[[CST0:.*]] = stablehlo.constant dense<42> : tensor<7x22x128xi32> + // CHECK-NEXT: %[[CST1:.*]] = stablehlo.constant dense<128> : tensor<1x22x128xi32> + // CHECK-NEXT: %[[CST2:.*]] = stablehlo.constant dense<140> : tensor<1x22x128xi32> + // CHECK-NEXT: %[[CST3:.*]] = stablehlo.constant dense<11> : tensor<1x22x128xi32> + // CHECK-NEXT: %[[FALSE:.*]] = stablehlo.constant dense<0> : tensor<1x22x128xi32> + // CHECK-NEXT: %[[IOTA0:.*]] = stablehlo.iota dim = 0 : tensor<22xi32> + // CHECK-NEXT: %[[BCAST0:.*]] = stablehlo.broadcast_in_dim %[[IOTA0]], dims = [1] : (tensor<22xi32>) -> tensor<1x22x128xi32> + // CHECK-NEXT: %[[IOTA1:.*]] = stablehlo.iota dim = 0 : tensor<128xi32> + // CHECK-NEXT: %[[BCAST1:.*]] = stablehlo.broadcast_in_dim %[[IOTA1]], dims = [2] : (tensor<128xi32>) -> tensor<1x22x128xi32> + // CHECK-NEXT: %[[SUB0:.*]] = stablehlo.subtract %[[CST3]], %[[BCAST0]] : tensor<1x22x128xi32> + // CHECK-NEXT: %[[NEG0:.*]] = stablehlo.negate %[[SUB0]] : tensor<1x22x128xi32> + // CHECK-NEXT: %[[MAX0:.*]] = stablehlo.maximum %[[SUB0]], %[[FALSE]] : tensor<1x22x128xi32> + // CHECK-NEXT: %[[SUB1:.*]] = stablehlo.subtract %[[MAX0]], %[[FALSE]] : tensor<1x22x128xi32> + // CHECK-NEXT: %[[MAX1:.*]] = stablehlo.maximum %[[NEG0]], %[[FALSE]] : tensor<1x22x128xi32> + // CHECK-NEXT: %[[SUB2:.*]] = stablehlo.subtract %[[MAX1]], %[[FALSE]] : tensor<1x22x128xi32> + // CHECK-NEXT: %[[ADD0:.*]] = stablehlo.add %[[BCAST1]], %[[SUB1]] : tensor<1x22x128xi32> + // CHECK-NEXT: %[[ADD1:.*]] = stablehlo.add %[[BCAST1]], %[[SUB2]] : tensor<1x22x128xi32> + // CHECK-NEXT: %[[CMP0:.*]] = stablehlo.compare GE, %[[ADD0]], %[[FALSE]] : (tensor<1x22x128xi32>, tensor<1x22x128xi32>) -> tensor<1x22x128xi1> + // CHECK-NEXT: %[[CMP1:.*]] = stablehlo.compare LT, %[[ADD0]], %[[CST1]] : (tensor<1x22x128xi32>, tensor<1x22x128xi32>) -> tensor<1x22x128xi1> + // CHECK-NEXT: %[[AND0:.*]] = stablehlo.and %[[CMP0]], %[[CMP1]] : tensor<1x22x128xi1> + // CHECK-NEXT: %[[CMP2:.*]] = stablehlo.compare GE, %[[ADD1]], %[[FALSE]] : (tensor<1x22x128xi32>, tensor<1x22x128xi32>) -> tensor<1x22x128xi1> + // CHECK-NEXT: %[[CMP3:.*]] = stablehlo.compare LT, %[[ADD1]], %[[CST2]] : (tensor<1x22x128xi32>, tensor<1x22x128xi32>) -> tensor<1x22x128xi1> + // CHECK-NEXT: %[[AND1:.*]] = stablehlo.and %[[CMP2]], %[[CMP3]] : tensor<1x22x128xi1> + // CHECK-NEXT: %[[AND2:.*]] = stablehlo.and %[[AND0]], %[[AND1]] : tensor<1x22x128xi1> + // CHECK-NEXT: %[[RESHAPE0:.*]] = stablehlo.reshape %[[AND2]] : (tensor<1x22x128xi1>) -> tensor<22x128xi1> + // CHECK-NEXT: %[[CONCAT0:.*]] = stablehlo.concatenate %[[ADD1]], %[[ADD0]], dim = 0 : (tensor<1x22x128xi32>, tensor<1x22x128xi32>) -> tensor<2x22x128xi32> + // CHECK-NEXT: %[[GATHER0:.*]] = "stablehlo.gather"(%arg0, %[[CONCAT0]]) <{dimension_numbers = #{{.*}}, indices_are_sorted = false, slice_sizes = array}> : (tensor<7x140x128xi32>, tensor<2x22x128xi32>) -> tensor<7x22x128xi32> + // CHECK-NEXT: %[[BCAST2:.*]] = stablehlo.broadcast %[[RESHAPE0]], sizes = [7] : (tensor<22x128xi1>) -> tensor<7x22x128xi1> + // CHECK-NEXT: %[[SELECT0:.*]] = stablehlo.select %[[BCAST2]], %[[GATHER0]], %[[CST0]] : tensor<7x22x128xi1>, tensor<7x22x128xi32> + // CHECK-NEXT: return %[[SELECT0]] : tensor<7x22x128xi32> + + %0 = mhlo.constant dense<42> : tensor // padding value + %1 = mhlo.constant dense<[-10, 11]> : tensor<2xi32> // k + %2 = "tf.MatrixDiagPartV3"(%arg0, %1, %0) { + T = i32, align = "LEFT_LEFT" + } : (tensor<7x140x128xi32>, tensor<2xi32>, tensor) -> tensor<7x22x128xi32> + func.return %2: tensor<7x22x128xi32> +} + +// ----- + +// CHECK-LABEL: func @matrix_diag_part_align_lr +func.func @matrix_diag_part_align_lr(%arg0: tensor<7x140x128xi32>) -> tensor<7x22x128xi32> { + %0 = mhlo.constant dense<42> : tensor // padding value + %1 = mhlo.constant dense<[-10, 11]> : tensor<2xi32> // k + %2 = "tf.MatrixDiagPartV3"(%arg0, %1, %0) { + T = i32, align = "LEFT_RIGHT" + } : (tensor<7x140x128xi32>, tensor<2xi32>, tensor) -> tensor<7x22x128xi32> + // CHECK: %[[LE:.*]] = stablehlo.compare LE, %{{.*}}, %{{.*}} : (tensor<1x22x128xi32>, tensor<1x22x128xi32>) -> tensor<1x22x128xi1> + // CHECK: %{{.*}} = stablehlo.select %[[LE]], %{{.*}}, %{{.*}} : tensor<1x22x128xi1>, tensor<1x22x128xi32> + func.return %2: tensor<7x22x128xi32> +} + +// ----- + +// CHECK-LABEL: func @matrix_diag_part_align_rl +func.func @matrix_diag_part_align_rl(%arg0: tensor<7x140x128xi32>) -> tensor<7x22x128xi32> { + %0 = mhlo.constant dense<42> : tensor // padding value + %1 = mhlo.constant dense<[-10, 11]> : tensor<2xi32> // k + %2 = "tf.MatrixDiagPartV3"(%arg0, %1, %0) { + T = i32, align = "RIGHT_LEFT" + } : (tensor<7x140x128xi32>, tensor<2xi32>, tensor) -> tensor<7x22x128xi32> + // CHECK: %[[GE:.*]] = stablehlo.compare GE, %{{.*}}, %{{.*}} : (tensor<1x22x128xi32>, tensor<1x22x128xi32>) -> tensor<1x22x128xi1> + // CHECK: %{{.*}} = stablehlo.select %[[GE]], %{{.*}}, %{{.*}} : tensor<1x22x128xi1>, tensor<1x22x128xi32> + + func.return %2: tensor<7x22x128xi32> +} + +// ----- + +// CHECK-LABEL: func @matrix_diag_part_align_rr +func.func @matrix_diag_part_align_rr(%arg0: tensor<7x140x128xi32>) -> tensor<7x22x128xi32> { + %0 = mhlo.constant dense<42> : tensor // padding value + %1 = mhlo.constant dense<[-10, 11]> : tensor<2xi32> // k + %2 = "tf.MatrixDiagPartV3"(%arg0, %1, %0) { + T = i32, align = "RIGHT_RIGHT" + } : (tensor<7x140x128xi32>, tensor<2xi32>, tensor) -> tensor<7x22x128xi32> + // CHECK-NOT: MatrixDiagPartV3 + func.return %2: tensor<7x22x128xi32> +} + +// ----- + +// CHECK-LABEL: func @matrix_diag_part_align_7d +// CHECK: (%arg0: tensor<3x5x7x9x11x13x17xf32>) -> tensor<3x5x7x9x11x4x10xf32> +func.func @matrix_diag_part_align_7d(%arg0: tensor<3x5x7x9x11x13x17xf32>) -> tensor<3x5x7x9x11x4x10xf32> { + %0 = mhlo.constant dense<-1.> : tensor // padding value + %1 = mhlo.constant dense<[-6, -3]> : tensor<2xi32> // k + %2 = "tf.MatrixDiagPartV3"(%arg0, %1, %0) { + T = f32, align = "LEFT_RIGHT" + } : (tensor<3x5x7x9x11x13x17xf32>, tensor<2xi32>, tensor) -> tensor<3x5x7x9x11x4x10xf32> + func.return %2: tensor<3x5x7x9x11x4x10xf32> +} + +//===----------------------------------------------------------------------===// +// Erf +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: func @erf +func.func @erf(%arg0: tensor<2x3xf32>) -> tensor<2x3xf32> { + // CHECK: mhlo.erf(%arg0) {{.*}} : (tensor<2x3xf32>) -> tensor<2x3xf32> + %0 = "tf.Erf"(%arg0) : (tensor<2x3xf32>) -> tensor<2x3xf32> + func.return %0 : tensor<2x3xf32> +} + +//===----------------------------------------------------------------------===// +// Erfc +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: func @erfc +func.func @erfc(%arg0: tensor<2x3xf32>) -> tensor<2x3xf32> { + // CHECK-NOT: tf.Erfc + %0 = "tf.Erfc"(%arg0) : (tensor<2x3xf32>) -> tensor<2x3xf32> + func.return %0 : tensor<2x3xf32> +} + +//===----------------------------------------------------------------------===// +// Einsum. +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: func @einsum +func.func @einsum(%arg0: tensor<2x3xf32>, %arg1: tensor<3x4xf32>) -> tensor<2x4xf32> { + // CHECK: stablehlo.einsum + %0 = "tf.Einsum"(%arg0, %arg1) {equation = "ab,bc->ac"} : (tensor<2x3xf32>, tensor<3x4xf32>) -> tensor<2x4xf32> + func.return %0: tensor<2x4xf32> +} + +// ----- + +// CHECK-LABEL: func @unary_einsum +func.func @unary_einsum(%arg0: tensor<2x3xf32>) -> tensor<2x2xf32> { + // CHECK: stablehlo.constant{{.*}}1.000000e+00 + // CHECK: stablehlo.einsum{{.*}}",ab->aa" + %0 = "tf.Einsum"(%arg0) {equation = "ab->aa"} : (tensor<2x3xf32>) -> tensor<2x2xf32> + func.return %0: tensor<2x2xf32> +} + +//===----------------------------------------------------------------------===// +// FloorDiv and FloorMod. +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: func @floordiv_broadcast_i32 +func.func @floordiv_broadcast_i32(%arg0: tensor<2x3xi32>, %arg1: tensor<3xi32>) -> tensor<2x3xi32> { + // CHECK-NEXT: %[[ONES:.*]] = stablehlo.constant dense<1> : tensor<2x3xi32> + // CHECK-NEXT: %[[ZEROS0:.*]] = stablehlo.constant dense<0> : tensor<3xi32> + // CHECK-NEXT: %[[ZEROS1:.*]] = stablehlo.constant dense<0> : tensor<2x3xi32> + // CHECK-NEXT: %[[BCAST0:.*]] = stablehlo.broadcast_in_dim %arg1, dims = [1] : (tensor<3xi32>) -> tensor<2x3xi32> + // CHECK-NEXT: %[[DIV0:.*]] = stablehlo.divide %arg0, %[[BCAST0]] : tensor<2x3xi32> + // CHECK-NEXT: %[[BCAST1:.*]] = stablehlo.broadcast_in_dim %arg1, dims = [1] : (tensor<3xi32>) -> tensor<2x3xi32> + // CHECK-NEXT: %[[MUL0:.*]] = stablehlo.multiply %[[DIV0]], %[[BCAST1]] : tensor<2x3xi32> + // CHECK-NEXT: %[[CMP0:.*]] = stablehlo.compare NE, %[[MUL0]], %arg0 : (tensor<2x3xi32>, tensor<2x3xi32>) -> tensor<2x3xi1> + // CHECK-NEXT: %[[CMP1:.*]] = stablehlo.compare LT, %arg0, %[[ZEROS1]] : (tensor<2x3xi32>, tensor<2x3xi32>) -> tensor<2x3xi1> + // CHECK-NEXT: %[[CMP2:.*]] = stablehlo.compare LT, %arg1, %[[ZEROS0]] : (tensor<3xi32>, tensor<3xi32>) -> tensor<3xi1> + // CHECK-NEXT: %[[BCAST2:.*]] = stablehlo.broadcast_in_dim %[[CMP2]], dims = [1] : (tensor<3xi1>) -> tensor<2x3xi1> + // CHECK-NEXT: %[[CMP3:.*]] = stablehlo.compare NE, %[[CMP1]], %[[BCAST2]] : (tensor<2x3xi1>, tensor<2x3xi1>) -> tensor<2x3xi1> + // CHECK-NEXT: %[[AND0:.*]] = stablehlo.and %[[CMP0]], %[[CMP3]] : tensor<2x3xi1> + // CHECK-NEXT: %[[SUB0:.*]] = stablehlo.subtract %[[DIV0]], %[[ONES]] : tensor<2x3xi32> + // CHECK-NEXT: %[[SELECT0:.*]] = stablehlo.select %[[AND0]], %[[SUB0]], %[[DIV0]] : tensor<2x3xi1>, tensor<2x3xi32> + // CHECK-NEXT: return %[[SELECT0]] : tensor<2x3xi32> + + %0 = "tf.FloorDiv"(%arg0, %arg1) : (tensor<2x3xi32>, tensor<3xi32>) -> tensor<2x3xi32> + func.return %0: tensor<2x3xi32> +} + +// ----- + +// CHECK-LABEL: func @floordiv_reverse_broadcast_i32 +func.func @floordiv_reverse_broadcast_i32(%arg0: tensor<3xi32>, %arg1: tensor<2x3xi32>) -> tensor<2x3xi32> { + // CHECK-NEXT: %[[ONES:.*]] = stablehlo.constant dense<1> : tensor<2x3xi32> + // CHECK-NEXT: %[[ZEROS0:.*]] = stablehlo.constant dense<0> : tensor<2x3xi32> + // CHECK-NEXT: %[[ZEROS1:.*]] = stablehlo.constant dense<0> : tensor<3xi32> + // CHECK-NEXT: %[[BCAST0:.*]] = stablehlo.broadcast_in_dim %arg0, dims = [1] : (tensor<3xi32>) -> tensor<2x3xi32> + // CHECK-NEXT: %[[DIV0:.*]] = stablehlo.divide %[[BCAST0]], %arg1 : tensor<2x3xi32> + // CHECK-NEXT: %[[MUL0:.*]] = stablehlo.multiply %[[DIV0]], %arg1 : tensor<2x3xi32> + // CHECK-NEXT: %[[BCAST1:.*]] = stablehlo.broadcast_in_dim %arg0, dims = [1] : (tensor<3xi32>) -> tensor<2x3xi32> + // CHECK-NEXT: %[[CMP0:.*]] = stablehlo.compare NE, %[[MUL0]], %[[BCAST1]] : (tensor<2x3xi32>, tensor<2x3xi32>) -> tensor<2x3xi1> + // CHECK-NEXT: %[[CMP1:.*]] = stablehlo.compare LT, %arg0, %[[ZEROS1]] : (tensor<3xi32>, tensor<3xi32>) -> tensor<3xi1> + // CHECK-NEXT: %[[CMP2:.*]] = stablehlo.compare LT, %arg1, %[[ZEROS0]] : (tensor<2x3xi32>, tensor<2x3xi32>) -> tensor<2x3xi1> + // CHECK-NEXT: %[[BCAST2:.*]] = stablehlo.broadcast_in_dim %[[CMP1]], dims = [1] : (tensor<3xi1>) -> tensor<2x3xi1> + // CHECK-NEXT: %[[CMP3:.*]] = stablehlo.compare NE, %[[BCAST2]], %[[CMP2]] : (tensor<2x3xi1>, tensor<2x3xi1>) -> tensor<2x3xi1> + // CHECK-NEXT: %[[AND0:.*]] = stablehlo.and %[[CMP0]], %[[CMP3]] : tensor<2x3xi1> + // CHECK-NEXT: %[[SUB0:.*]] = stablehlo.subtract %[[DIV0]], %[[ONES]] : tensor<2x3xi32> + // CHECK-NEXT: %[[SELECT0:.*]] = stablehlo.select %[[AND0]], %[[SUB0]], %[[DIV0]] : tensor<2x3xi1>, tensor<2x3xi32> + // CHECK-NEXT: return %[[SELECT0]] : tensor<2x3xi32> + + %0 = "tf.FloorDiv"(%arg0, %arg1) : (tensor<3xi32>, tensor<2x3xi32>) -> tensor<2x3xi32> + func.return %0: tensor<2x3xi32> +} + +// ----- + +// CHECK-LABEL: func @floordiv_f32 +func.func @floordiv_f32(%arg0: tensor<2xf32>) -> tensor<2xf32> { + // CHECK-NEXT: %[[DIV:.*]] = stablehlo.divide %arg0, %arg0 + // CHECK-NEXT: %[[FLOOR:.*]] = stablehlo.floor %[[DIV]] + // CHECK-NEXT: return %[[FLOOR]] : tensor<2xf32> + %0 = "tf.FloorDiv"(%arg0, %arg0) : (tensor<2xf32>, tensor<2xf32>) -> tensor<2xf32> + func.return %0: tensor<2xf32> +} + +// ----- + +// CHECK-LABEL: func @floordiv_bf16 +func.func @floordiv_bf16(%arg0: tensor<2xbf16>) -> tensor<2xbf16> { + // CHECK-NEXT: stablehlo.convert + // CHECK-NEXT: stablehlo.convert + // CHECK-NEXT: stablehlo.divide + // CHECK-NEXT: stablehlo.floor + // CHECK-NEXT: stablehlo.convert + // CHECK-NEXT: return + %0 = "tf.FloorDiv"(%arg0, %arg0) : (tensor<2xbf16>, tensor<2xbf16>) -> tensor<2xbf16> + func.return %0: tensor<2xbf16> +} + +// ----- + +// CHECK-LABEL: func @floordiv_f16_broadcast +func.func @floordiv_f16_broadcast(%arg0: tensor<2x3xf16>, %arg1: tensor<3xf16>) -> tensor<2x3xf16> { + // CHECK-NEXT: stablehlo.broadcast_in_dim + // CHECK-NEXT: stablehlo.divide + // CHECK-NEXT: stablehlo.floor + // CHECK-NEXT: return + %0 = "tf.FloorDiv"(%arg0, %arg1) : (tensor<2x3xf16>, tensor<3xf16>) -> tensor<2x3xf16> + func.return %0: tensor<2x3xf16> +} + +// ----- + +// CHECK-LABEL: func @floordiv_dynamic +func.func @floordiv_dynamic(%arg0: tensor, %arg1: tensor) -> tensor { + // CHECK: shape.assuming + // CHECK: stablehlo.dynamic_broadcast_in_dim + // CHECK: stablehlo.divide + // CHECK: shape.assuming + // CHECK: stablehlo.dynamic_broadcast_in_dim + // CHECK: stablehlo.multiply + // CHECK: shape.assuming + // CHECK: stablehlo.dynamic_broadcast_in_dim + // CHECK: stablehlo.compare + // CHECK: shape.assuming + // CHECK: stablehlo.dynamic_broadcast_in_dim + // CHECK: stablehlo.and + // + // CHECK: %[[SELECT:.*]] = stablehlo.select + // CHECK: return %[[SELECT]] + %0 = "tf.FloorDiv"(%arg0, %arg1) : (tensor, tensor) -> tensor + func.return %0: tensor +} + +// ----- + +// CHECK-LABEL: func @floordiv_unsigned +func.func @floordiv_unsigned(%arg0: tensor, %arg1: tensor) -> tensor { + // CHECK: %[[RESULT:.*]] = shape.assuming + // CHECK: %[[BCAST0:.*]] = stablehlo.dynamic_broadcast_in_dim %arg0, + // CHECK: %[[BCAST1:.*]] = stablehlo.dynamic_broadcast_in_dim %arg1, + // CHECK: %[[DIV:.*]] = stablehlo.divide %[[BCAST0]], %[[BCAST1]] + // CHECK: shape.assuming_yield %[[DIV]] + // CHECK: return %[[RESULT]] + %0 = "tf.FloorDiv"(%arg0, %arg1) : (tensor, tensor) -> tensor + func.return %0: tensor +} + +// ----- + +// CHECK-LABEL: func @floordiv_int +func.func @floordiv_int(%arg0: tensor, %arg1: tensor) -> tensor { + // CHECK: shape.assuming + // CHECK: stablehlo.dynamic_broadcast_in_dim + // CHECK: stablehlo.divide + // CHECK: shape.assuming + // CHECK: stablehlo.dynamic_broadcast_in_dim + // CHECK: stablehlo.multiply + // CHECK: shape.assuming + // CHECK: stablehlo.dynamic_broadcast_in_dim + // CHECK: stablehlo.compare + // CHECK: shape.assuming + // CHECK: stablehlo.dynamic_broadcast_in_dim + // CHECK: stablehlo.compare + // CHECK: shape.assuming + // CHECK: stablehlo.dynamic_broadcast_in_dim + // CHECK: stablehlo.and + // + // CHECK: %[[SELECT:.*]] = stablehlo.select + // CHECK: return %[[SELECT]] + %0 = "tf.FloorDiv"(%arg0, %arg1) : (tensor, tensor) -> tensor + func.return %0: tensor +} + +// ----- + +// CHECK-LABEL: func @floormod_broadcast_numerator +func.func @floormod_broadcast_numerator(%arg0: tensor<3xi32>, %arg1: tensor<2x3xi32>) -> tensor<2x3xi32> { + // CHECK: %[[BCAST0:.*]] = stablehlo.broadcast_in_dim %arg0, dims = [1] : (tensor<3xi32>) -> tensor<2x3xi32> + // CHECK: %[[REM:.*]] = stablehlo.remainder %[[BCAST0]], %arg1 : tensor<2x3xi32> + // CHECK: %[[AND:.*]] = stablehlo.and + // CHECK: %[[ADD:.*]] = stablehlo.add + // CHECK: %[[SELECT:.*]] = stablehlo.select %[[AND]], %[[ADD]], %[[REM]] + // CHECK-NEXT: return %[[SELECT]] + %0 = "tf.FloorMod"(%arg0, %arg1) : (tensor<3xi32>, tensor<2x3xi32>) -> tensor<2x3xi32> + func.return %0: tensor<2x3xi32> +} + +// ----- + +// CHECK-LABEL: func @floormod_broadcast_denominator +func.func @floormod_broadcast_denominator(%arg0: tensor<2x3xi32>, %arg1: tensor<3xi32>) -> tensor<2x3xi32> { + // CHECK: %[[BCAST0:.*]] = stablehlo.broadcast_in_dim %arg1, dims = [1] : (tensor<3xi32>) -> tensor<2x3xi32> + // CHECK: %[[REM:.*]] = stablehlo.remainder %arg0, %[[BCAST0]] + // CHECK: %[[AND:.*]] = stablehlo.and + // CHECK: %[[BCAST1:.*]] = stablehlo.broadcast_in_dim %arg1, dims = [1] : (tensor<3xi32>) -> tensor<2x3xi32> + // CHECK: %[[ADD:.*]] = stablehlo.add %[[BCAST1]], %[[REM]] + // CHECK: %[[SELECT:.*]] = stablehlo.select %[[AND]], %[[ADD]], %[[REM]] + // CHECK-NEXT: return %[[SELECT]] + %0 = "tf.FloorMod"(%arg0, %arg1) : (tensor<2x3xi32>, tensor<3xi32>) -> tensor<2x3xi32> + func.return %0: tensor<2x3xi32> +} + +// ----- + +// CHECK-LABEL: func @floormod_unsigned_broadcast_denominator +func.func @floormod_unsigned_broadcast_denominator(%arg0: tensor<2x3xui32>, %arg1: tensor<3xui32>) -> tensor<2x3xui32> { + // CHECK-NEXT: %[[BCAST0:.*]] = stablehlo.broadcast_in_dim %arg1, dims = [1] : (tensor<3xui32>) -> tensor<2x3xui32> + // CHECK-NEXT: %[[REM:.*]] = stablehlo.remainder %arg0, %[[BCAST0]] : tensor<2x3xui32> + // CHECK-NEXT: return %[[REM]] : tensor<2x3xui32> + %0 = "tf.FloorMod"(%arg0, %arg1) : (tensor<2x3xui32>, tensor<3xui32>) -> tensor<2x3xui32> + func.return %0: tensor<2x3xui32> +} + +// ----- + +// CHECK-LABEL: func @floormod_dynamic_broadcast_numerator +func.func @floormod_dynamic_broadcast_numerator_(%arg0: tensor, %arg1: tensor) -> tensor { + // CHECK: %[[REM:.*]] = shape.assuming {{.*}} { + // CHECK: stablehlo.remainder + // CHECK: shape.assuming {{.*}} { + // CHECK: stablehlo.compare + // CHECK: %[[AND:.*]] = shape.assuming {{.*}} { + // CHECK: stablehlo.and + // CHECK: %[[ADD:.*]] = shape.assuming {{.*}} { + // CHECK: stablehlo.add + // CHECK: %[[SELECT:.*]] = stablehlo.select %[[AND]], %[[ADD]], %[[REM]] + // CHECK-NEXT: return %[[SELECT]] + %0 = "tf.FloorMod"(%arg0, %arg1) : (tensor, tensor) -> tensor + func.return %0: tensor +} + +// ----- + +// CHECK-LABEL: func @floormod_dynamic_broadcast_denominator +func.func @floormod_dynamic_broadcast_denominator_(%arg0: tensor, %arg1: tensor) -> tensor { + // CHECK-NOT: tf.FloorMod + // CHECK: %[[REM:.*]] = shape.assuming {{.*}} { + // CHECK: stablehlo.remainder + // CHECK: shape.assuming {{.*}} { + // CHECK: stablehlo.compare + // CHECK: %[[AND:.*]] = shape.assuming {{.*}} { + // CHECK: stablehlo.and + // CHECK: %[[ADD:.*]] = shape.assuming {{.*}} { + // CHECK: stablehlo.add + // CHECK: %[[SELECT:.*]] = stablehlo.select %[[AND]], %[[ADD]], %[[REM]] + // CHECK-NEXT: return %[[SELECT]] + %0 = "tf.FloorMod"(%arg0, %arg1) : (tensor, tensor) -> tensor + func.return %0: tensor +} + +//===----------------------------------------------------------------------===// +// OnesLike +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: @ones_like +// CHECK-SAME: (%[[ARG:.*]]: tensor<2x?xf32>) +func.func @ones_like(%arg0: tensor<2x?xf32>) -> tensor<2x?xf32> { + // CHECK-NEXT: %[[ONES:.*]] = stablehlo.constant dense<1.000000e+00> : tensor + // CHECK-NEXT: %[[SHAPE:.*]] = shape.shape_of %arg0 : tensor<2x?xf32> -> tensor<2xindex> + // CHECK-NEXT: %[[RESULT:.*]] = stablehlo.dynamic_broadcast_in_dim %[[ONES]], %[[SHAPE]], dims = [] : (tensor, tensor<2xindex>) -> tensor<2x?xf32> + // CHECK-NEXT: return %[[RESULT]] : tensor<2x?xf32> + %0 = "tf.OnesLike"(%arg0) : (tensor<2x?xf32>) -> tensor<2x?xf32> + func.return %0 : tensor<2x?xf32> +} + +//===----------------------------------------------------------------------===// +// ZerosLike +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: @zeros_like +// CHECK-SAME: (%[[ARG:.*]]: tensor<2x?xf32>) +func.func @zeros_like(%arg0: tensor<2x?xf32>) -> tensor<2x?xf32> { + // CHECK-NEXT: %[[ZEROS:.*]] = stablehlo.constant dense<0.000000e+00> : tensor + // CHECK-NEXT: %[[SHAPE:.*]] = shape.shape_of %arg0 : tensor<2x?xf32> -> tensor<2xindex> + // CHECK-NEXT: %[[RESULT:.*]] = stablehlo.dynamic_broadcast_in_dim %[[ZEROS]], %[[SHAPE]], dims = [] : (tensor, tensor<2xindex>) -> tensor<2x?xf32> + // CHECK-NEXT: return %[[RESULT]] : tensor<2x?xf32> + %0 = "tf.ZerosLike"(%arg0) : (tensor<2x?xf32>) -> tensor<2x?xf32> + func.return %0 : tensor<2x?xf32> +} + +//===----------------------------------------------------------------------===// +// BroadcastTo. +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: func @broadcast_to +func.func @broadcast_to(%arg0: tensor<16xf32>) -> tensor<16x16x16x16xf32> { + %cst = "tf.Const"() { value = dense<16> : tensor<4xi32> } : () -> tensor<4xi32> + // CHECK: stablehlo.broadcast_in_dim %arg0 + %0 = "tf.BroadcastTo"(%arg0, %cst) : (tensor<16xf32>, tensor<4xi32>) -> tensor<16x16x16x16xf32> + func.return %0 : tensor<16x16x16x16xf32> +} + +//===----------------------------------------------------------------------===// +// Complex op legalizations. +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: func @complex +func.func @complex(%arg0: tensor<3xf32>, %arg1: tensor<3xf32>) -> tensor<3xcomplex> { + // CHECK: stablehlo.complex + %1 = "tf.Complex"(%arg0, %arg1) : (tensor<3xf32>, tensor<3xf32>) -> tensor<3xcomplex> + func.return %1 : tensor<3xcomplex> +} + +// ----- + +// CHECK-LABEL: func @imag +func.func @imag(%arg0: tensor<3xcomplex>) -> tensor<3xf32> { + // CHECK: stablehlo.imag + %1 = "tf.Imag"(%arg0) : (tensor<3xcomplex>) -> tensor<3xf32> + func.return %1 : tensor<3xf32> +} + +// ----- + +// CHECK-LABEL: func @real +func.func @real(%arg0: tensor<3xcomplex>) -> tensor<3xf32> { + // CHECK: stablehlo.real + %1 = "tf.Real"(%arg0) : (tensor<3xcomplex>) -> tensor<3xf32> + func.return %1 : tensor<3xf32> +} + +//===----------------------------------------------------------------------===// +// Concat op legalizations. +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: func @concat_v2 +func.func @concat_v2(%arg0: tensor<3x3xf32>, %arg1: tensor<3x3xf32>) -> tensor<6x3xf32> { + // CHECK: stablehlo.concatenate %arg0, %arg1, dim = 0 + %axis = "tf.Const"() { value = dense<0> : tensor } : () -> tensor + %1 = "tf.ConcatV2"(%arg0, %arg1, %axis) : (tensor<3x3xf32>, tensor<3x3xf32>, tensor) -> tensor<6x3xf32> + func.return %1 : tensor<6x3xf32> +} + +// ----- + +// CHECK-LABEL: func @concat_v2_neg_axis +func.func @concat_v2_neg_axis(%arg0: tensor<3x3xf32>, %arg1: tensor<3x3xf32>) -> tensor<6x3xf32> { + // CHECK: stablehlo.concatenate %arg0, %arg1, dim = 0 + + %axis = "tf.Const"() { value = dense<-2> : tensor } : () -> tensor + %1 = "tf.ConcatV2"(%arg0, %arg1, %axis) : (tensor<3x3xf32>, tensor<3x3xf32>, tensor) -> tensor<6x3xf32> + func.return %1 : tensor<6x3xf32> +} + +// ----- + +// CHECK-LABEL: func @concat_v2_1d_axis +func.func @concat_v2_1d_axis(%arg0: tensor<3x3xf32>, %arg1: tensor<3x3xf32>) -> tensor<3x6xf32> { + // CHECK: stablehlo.concatenate %arg0, %arg1, dim = 1 + + %axis = "tf.Const"() { value = dense<[1]> : tensor<1xi64> } : () -> tensor<1xi64> + %1 = "tf.ConcatV2"(%arg0, %arg1, %axis) : (tensor<3x3xf32>, tensor<3x3xf32>, tensor<1xi64>) -> tensor<3x6xf32> + func.return %1 : tensor<3x6xf32> +} + +// ----- + +// CHECK-LABEL: func @concat_v2_non_const_axis +module attributes {tf.versions = {bad_consumers = [], min_consumer = 12 : i32, producer = 12 : i32}} { +func.func @concat_v2_non_const_axis(%arg0: tensor<3x3xf32>, %arg1: tensor<3x3xf32>, %axis: tensor) -> tensor<3x6xf32> { + // CHECK: "tf.ConcatV2" + %1 = "tf.ConcatV2"(%arg0, %arg1, %axis) : (tensor<3x3xf32>, tensor<3x3xf32>, tensor) -> tensor<3x6xf32> + func.return %1 : tensor<3x6xf32> +} +} + +//===----------------------------------------------------------------------===// +// Pad op legalizations. +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: func @padv2_1D +func.func @padv2_1D(%arg0: tensor<3xf32>, %arg1: tensor) -> tensor<6xf32> { + %padding = "tf.Const"() { value = dense<[[1, 2]]> : tensor<1x2xi64> } : () -> tensor<1x2xi64> + // CHECK: stablehlo.pad %arg0, %arg1, low = [1], high = [2], interior = [0] + %1 = "tf.PadV2"(%arg0, %padding, %arg1) : (tensor<3xf32>, tensor<1x2xi64>, tensor) -> tensor<6xf32> + func.return %1 : tensor<6xf32> +} + +// ----- + +// CHECK-LABEL: func @padv2_2D +func.func @padv2_2D(%arg0: tensor<3x2xf32>, %arg1: tensor) -> tensor<6x9xf32> { + %padding = "tf.Const"() { value = dense<[[1,2],[3,4]]> : tensor<2x2xi64> } : () -> tensor<2x2xi64> + // CHECK: stablehlo.pad %arg0, %arg1, low = [1, 3], high = [2, 4], interior = [0, 0] + %1 = "tf.PadV2"(%arg0, %padding, %arg1) : (tensor<3x2xf32>, tensor<2x2xi64>, tensor) -> tensor<6x9xf32> + func.return %1 : tensor<6x9xf32> +} + +// ----- + +// CHECK-LABEL: func @padv2_i32_paddings +func.func @padv2_i32_paddings(%arg0: tensor<3x2xf32>, %arg1: tensor) -> tensor<6x9xf32> { + %padding = "tf.Const"() { value = dense<[[1,2],[3,4]]> : tensor<2x2xi32> } : () -> tensor<2x2xi32> + // CHECK: stablehlo.pad %arg0, %arg1, low = [1, 3], high = [2, 4], interior = [0, 0] + %1 = "tf.PadV2"(%arg0, %padding, %arg1) : (tensor<3x2xf32>, tensor<2x2xi32>, tensor) -> tensor<6x9xf32> + func.return %1 : tensor<6x9xf32> +} + +// ----- + +// CHECK-LABEL: func @padv2_dynamic +func.func @padv2_dynamic(%arg0: tensor, %arg1: tensor, %arg2: tensor<1x2xi64>) -> tensor { + // CHECK-NEXT: %[[ZEROS:.*]] = stablehlo.constant dense<0> : tensor<1xi64> + // CHECK-NEXT: %[[RESHAPE:.*]] = stablehlo.reshape %arg2 : (tensor<1x2xi64>) -> tensor<2xi64> + // CHECK-NEXT: %[[SLICE0:.*]] = stablehlo.slice %[[RESHAPE]] [0:1] : (tensor<2xi64>) -> tensor<1xi64> + // CHECK-NEXT: %[[SLICE1:.*]] = stablehlo.slice %[[RESHAPE]] [1:2] : (tensor<2xi64>) -> tensor<1xi64> + // CHECK-NEXT: %[[RESULT:.*]] = stablehlo.dynamic_pad %arg0, %arg1, %[[SLICE0]], %[[SLICE1]], %[[ZEROS]] : (tensor, tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + // CHECK-NEXT: return %[[RESULT]] : tensor + + %1 = "tf.PadV2"(%arg0, %arg2, %arg1) : (tensor, tensor<1x2xi64>, tensor) -> tensor + func.return %1 : tensor +} + +//===----------------------------------------------------------------------===// +// Identity op legalizations. +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: func @identity +func.func @identity(%arg0: tensor<1xi32>) -> tensor<1xi32> { + // CHECK-NEXT: return %arg0 : tensor<1xi32> + %0 = "tf.Identity"(%arg0) : (tensor<1xi32>) -> tensor<1xi32> + func.return %0: tensor<1xi32> +} + +// ----- + +// CHECK-LABEL: func @identityN +func.func @identityN(%arg0: tensor<1xi32>, %arg1: tensor<1xf32>) -> (tensor<1xi32>, tensor<1xf32>) { + // CHECK-NEXT: return %arg0, %arg1 : tensor<1xi32>, tensor<1xf32> + %0:2 = "tf.IdentityN"(%arg0, %arg1) : (tensor<1xi32>, tensor<1xf32>) -> (tensor<1xi32>, tensor<1xf32>) + func.return %0#0, %0#1: tensor<1xi32>, tensor<1xf32> +} + +// ----- + +// CHECK-LABEL: func @stopgradient +func.func @stopgradient(%arg0: tensor<1xi32>) -> tensor<1xi32> { + // CHECK-NEXT: return %arg0 : tensor<1xi32> + %0 = "tf.StopGradient"(%arg0) : (tensor<1xi32>) -> tensor<1xi32> + func.return %0: tensor<1xi32> +} + +// ----- + +// CHECK-LABEL: func @preventgradient +func.func @preventgradient(%arg0: tensor<1xi32>) -> tensor<1xi32> { + // CHECK-NEXT: return %arg0 : tensor<1xi32> + %0 = "tf.PreventGradient"(%arg0) {message = "fin gradients"} : (tensor<1xi32>) -> tensor<1xi32> + func.return %0: tensor<1xi32> +} + +// ----- + +// CHECK-LABEL: func @checkNumerics +func.func @checkNumerics(%arg0: tensor<1xf32>) -> tensor<1xf32> { + // CHECK-NEXT: return %arg0 : tensor<1xf32> + %0 = "tf.CheckNumerics"(%arg0) {message = "check numerics"} : (tensor<1xf32>) -> tensor<1xf32> + func.return %0: tensor<1xf32> +} + +//===----------------------------------------------------------------------===// +// InfeedDequeueTuple legalization +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: func @infeed_dequeue_tuple +func.func @infeed_dequeue_tuple() -> (tensor<1x8x4x4xi32>, tensor<1x100x1xf32>) { + // CHECK: [[TOKEN:%.*]] = stablehlo.create_token : !stablehlo.token + // CHECK: [[INFEED:%.*]]:3 = "stablehlo.infeed"([[TOKEN]]) <{infeed_config = ""{{.*}}}> : (!stablehlo.token) -> (tensor<1x8x4x4xi32>, tensor<1x100x1xf32>, !stablehlo.token) + // CHECK: return [[INFEED]]#0, [[INFEED]]#1 + %0:2 = "tf.InfeedDequeueTuple"() : () -> (tensor<1x8x4x4xi32>, tensor<1x100x1xf32>) + func.return %0#0, %0#1 : tensor<1x8x4x4xi32>, tensor<1x100x1xf32> +} + +// ----- + +// CHECK-LABEL: func @infeed_dequeue_tuple_dynamic_error +func.func @infeed_dequeue_tuple_dynamic_error() -> (tensor<3x3xf32>, tensor<4x?xf32>) { + // We expect legalization to fail for dynamic shapes: + // CHECK: [[INFEED:%.*]] = "tf.InfeedDequeueTuple"{{.*}} + %0:2 = "tf.InfeedDequeueTuple"() : () -> (tensor<3x3xf32>, tensor<4x?xf32>) + func.return %0#0, %0#1 : tensor<3x3xf32>, tensor<4x?xf32> +} + +// The following op sharding is used: +// Proto debug string: +// type: TUPLE +// tuple_shardings { +// type: MAXIMAL +// tile_assignment_dimensions: 1 +// tile_assignment_devices: 0 +// } +// Serialized string: +// "\08\02*\08\08\01\1A\01\01\22\01\00" + +// CHECK-LABEL: infeed_dequeue_tuple_sharding +func.func @infeed_dequeue_tuple_sharding() -> tensor<8xi32> { + // CHECK: "stablehlo.infeed" + // An additional sharding is added at the end to account for token result. + // Proto debug string: + // type: TUPLE + // tuple_shardings { + // type: MAXIMAL + // tile_assignment_dimensions: 1 + // tile_assignment_devices: 0 + // } + // tuple_shardings { + // type: MAXIMAL + // tile_assignment_dimensions: 1 + // tile_assignment_devices: 0 + // } + // CHECK-SAME: mhlo.sharding = "\08\02*\08\08\01\1A\01\01\22\01\00*\08\08\01\1A\01\01\22\01\00" + %0 = "tf.InfeedDequeueTuple"() {_XlaSharding = "\08\02*\08\08\01\1A\01\01\22\01\00"} : () -> tensor<8xi32> + func.return %0 : tensor<8xi32> +} + +//===----------------------------------------------------------------------===// +// Nullary op legalizations. +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: @const +func.func @const() -> tensor<2xi32> { + // CHECK: stablehlo.constant dense<0> : tensor<2xi32> + %0 = "tf.Const"() {device = "", name = "", dtype = "tfdtype$DT_INT32", value = dense<0> : tensor<2xi32>} : () -> (tensor<2xi32>) + func.return %0: tensor<2xi32> +} + +// ----- + +// CHECK-LABEL: @const_dynamic_output +func.func @const_dynamic_output() -> tensor<*xi32> { + // CHECK: [[CONST:%.*]] = stablehlo.constant dense<0> : tensor<2xi32> + // CHECK: [[CAST:%.*]] = tensor.cast [[CONST]] : tensor<2xi32> to tensor<*xi32> + %0 = "tf.Const"() {value = dense<0> : tensor<2xi32>} : () -> (tensor<*xi32>) + // CHECK: return [[CAST]] + func.return %0: tensor<*xi32> +} + +// ----- + +// CHECK-LABEL: @opaque_const +func.func @opaque_const() -> tensor>> { + // CHECK-NOT: stablehlo.constant + %0 = "tf.Const"() {device = "", name = "", dtype = "tfdtype$DT_INT32", value = #tf_type : tensor} : () -> tensor>> + func.return %0 : tensor>> +} + +//===----------------------------------------------------------------------===// +// Matmul op legalizations. +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: matmul_notranspose +// CHECK-SAME: (%[[A:.*]]: tensor<5x7xf32>, %[[B:.*]]: tensor<7x11xf32>) +func.func @matmul_notranspose(%a: tensor<5x7xf32>, %b: tensor<7x11xf32>) -> tensor<5x11xf32> { + // CHECK: stablehlo.dot %[[A]], %[[B]] + %0 = "tf.MatMul"(%a, %b) {transpose_a = false, transpose_b = false} : (tensor<5x7xf32>, tensor<7x11xf32>) -> tensor<5x11xf32> + + func.return %0 : tensor<5x11xf32> +} + +// ----- + +// CHECK-LABEL: matmul_transpose_b +// CHECK-SAME: (%[[A:.*]]: tensor<5x7xf32>, %[[B:.*]]: tensor<11x7xf32>) +func.func @matmul_transpose_b(%a: tensor<5x7xf32>, %b: tensor<11x7xf32>) -> tensor<5x11xf32> { + // CHECK: %[[UPDATED_B:.*]] = stablehlo.transpose %[[B]], dims = [1, 0] + // CHECK: stablehlo.dot %[[A]], %[[UPDATED_B]] + %0 = "tf.MatMul"(%a, %b) {transpose_a = false, transpose_b = true} : (tensor<5x7xf32>, tensor<11x7xf32>) -> tensor<5x11xf32> + + func.return %0 : tensor<5x11xf32> +} + +// ----- + +// CHECK-LABEL: matmul_transpose_both +// CHECK-SAME: (%[[A:.*]]: tensor<7x5xf32>, %[[B:.*]]: tensor<11x7xf32>) +func.func @matmul_transpose_both(%a: tensor<7x5xf32>, %b: tensor<11x7xf32>) -> tensor<5x11xf32> { + // CHECK: %[[UPDATED_A:.*]] = stablehlo.transpose %[[A]] + // CHECK: %[[UPDATED_B:.*]] = stablehlo.transpose %[[B]] + // CHECK: stablehlo.dot %[[UPDATED_A]], %[[UPDATED_B]] + %0 = "tf.MatMul"(%a, %b) {transpose_a = true, transpose_b = true} : (tensor<7x5xf32>, tensor<11x7xf32>) -> tensor<5x11xf32> + + func.return %0 : tensor<5x11xf32> +} + +// Verify that MatMul with ranked inputs are lowered to HLO. +// CHECK-LABEL: matmul_ranked +func.func @matmul_ranked(%a: tensor, %b: tensor<7x?xf32>) -> tensor { + // CHECK: stablehlo.dot + %0 = "tf.MatMul"(%a, %b) {transpose_a = false, transpose_b = false} : (tensor, tensor<7x?xf32>) -> tensor + + func.return %0 : tensor +} + +// Verify SparseMatMul is legalized to dot. +// CHECK-LABEL: test_sparse_mat_mul +func.func @test_sparse_mat_mul(%arg0: tensor<3x4xf32>, %arg1: tensor<4x5xf32>) -> tensor<3x5xf32> { + // CHECK: stablehlo.dot + %0 = "tf.SparseMatMul"(%arg0, %arg1) {a_is_sparse = true, b_is_sparse = false, transpose_a = false, transpose_b = false} : (tensor<3x4xf32>, tensor<4x5xf32>) -> tensor<3x5xf32> + func.return %0: tensor<3x5xf32> +} + +// SparseMatMul where one operand needs to be transposed and the other one not. +// +// CHECK-LABEL: @test_sparse_mat_mul_with_transpose + // CHECK-SAME: %[[ARG0:.*]]: tensor<3x4xf32> + // CHECK-SAME: %[[ARG1:.*]]: tensor<5x4xf32> + // CHECK-SAME: -> tensor<3x5xf32> + // CHECK: %[[TRANSPOSE:.*]] = stablehlo.transpose %[[ARG1]] + // CHECK-SAME: dims = [1, 0] + // CHECK-SAME: -> tensor<4x5xf32> + // CHECK: %[[RESULT:.*]] = stablehlo.dot %[[ARG0]], %[[TRANSPOSE]] + // CHECK-SAME: -> tensor<3x5xf32> + // CHECK: return %[[RESULT]] +func.func @test_sparse_mat_mul_with_transpose(%arg0: tensor<3x4xf32>, %arg1: tensor<5x4xf32>) -> tensor<3x5xf32> { + %0 = "tf.SparseMatMul"(%arg0, %arg1) {a_is_sparse = true, b_is_sparse = false, transpose_a = false, transpose_b = true} : (tensor<3x4xf32>, tensor<5x4xf32>) -> tensor<3x5xf32> + func.return %0: tensor<3x5xf32> +} + +// SparseMatMul where one operand needs to be casted and the other one not. +// +// CHECK-LABEL: @test_sparse_mat_mul_with_cast + // CHECK-SAME: %[[ARG0:.*]]: tensor<3x4xf32> + // CHECK-SAME: %[[ARG1:.*]]: tensor<4x5xbf16> + // CHECK-SAME: -> tensor<3x5xf32> + // CHECK: %[[CAST:.*]] = stablehlo.convert %[[ARG1]] + // CHECK-SAME: -> tensor<4x5xf32> + // CHECK: %[[RESULT:.*]] = stablehlo.dot %[[ARG0]], %[[CAST]] + // CHECK-SAME: -> tensor<3x5xf32> + // CHECK: return %[[RESULT]] +func.func @test_sparse_mat_mul_with_cast(%arg0: tensor<3x4xf32>, %arg1: tensor<4x5xbf16>) -> tensor<3x5xf32> { + %0 = "tf.SparseMatMul"(%arg0, %arg1) {a_is_sparse = true, b_is_sparse = false, transpose_a = false, transpose_b = false} : (tensor<3x4xf32>, tensor<4x5xbf16>) -> tensor<3x5xf32> + func.return %0: tensor<3x5xf32> +} + +//===----------------------------------------------------------------------===// +// MaxPool op legalizations. +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: maxpool_valid_padding +// CHECK-SAME: %[[ARG:.*]]: tensor +func.func @maxpool_valid_padding(%arg0: tensor<2x12x20x7xi32>) -> tensor<2x3x5x7xi32> { + // CHECK: %[[INIT:.*]] = stablehlo.constant dense<-2147483648> : tensor + // CHECK: "stablehlo.reduce_window"(%[[ARG]], %[[INIT]]) + // CHECK-SAME: <{window_dimensions = array, window_strides = array}> + // CHECK: stablehlo.maximum + // CHECK: stablehlo.return + + %0 = "tf.MaxPool"(%arg0) {data_format = "NHWC", ksize = [1, 2, 2, 1], padding = "VALID", strides = [1, 4, 4, 1]} : (tensor<2x12x20x7xi32>) -> tensor<2x3x5x7xi32> + func.return %0 : tensor<2x3x5x7xi32> +} + +// ----- + +// CHECK-LABEL: maxpool_same_padding +// CHECK-SAME: %[[ARG:.*]]: tensor +func.func @maxpool_same_padding(%arg0: tensor<2x13x25x7xi32>) -> tensor<2x4x7x7xi32> { + // CHECK: padding = dense<{{\[\[}}0, 0], [0, 1], [1, 1], [0, 0]]> : tensor<4x2xi64> + + %0 = "tf.MaxPool"(%arg0) {data_format = "NHWC", ksize = [1, 2, 3, 1], padding = "SAME", strides = [1, 4, 4, 1]} : (tensor<2x13x25x7xi32>) -> tensor<2x4x7x7xi32> + func.return %0 : tensor<2x4x7x7xi32> +} + +// ----- + +// CHECK-LABEL: maxpool_3d_valid_padding +// CHECK-SAME: %[[ARG:.*]]: tensor +func.func @maxpool_3d_valid_padding(%arg0: tensor<2x8x12x20x7xf32>) -> tensor<2x8x3x5x7xf32> { + // CHECK: %[[INIT:.*]] = stablehlo.constant dense<0xFF800000> : tensor + // CHECK: "stablehlo.reduce_window"(%[[ARG]], %[[INIT]]) + // CHECK-SAME: <{window_dimensions = array, window_strides = array}> + // CHECK: stablehlo.maximum + // CHECK: stablehlo.return + + %0 = "tf.MaxPool3D"(%arg0) {data_format = "NDHWC", ksize = [1, 1, 2, 2, 1], padding = "VALID", strides = [1, 1, 4, 4, 1]} : (tensor<2x8x12x20x7xf32>) -> tensor<2x8x3x5x7xf32> + func.return %0 : tensor<2x8x3x5x7xf32> +} + +// ----- + +// CHECK-LABEL: maxpool_3d_same_padding +// CHECK-SAME: %[[ARG:.*]]: tensor +func.func @maxpool_3d_same_padding(%arg0: tensor<2x8x13x25x7xf32>) -> tensor<2x8x4x7x7xf32> { + // CHECK: padding = dense<{{\[\[}}0, 0], [0, 0], [0, 1], [1, 1], [0, 0]]> : tensor<5x2xi64> + + %0 = "tf.MaxPool3D"(%arg0) {data_format = "NDHWC", ksize = [1, 1, 2, 3, 1], padding = "SAME", strides = [1, 1, 4, 4, 1]} : (tensor<2x8x13x25x7xf32>) -> tensor<2x8x4x7x7xf32> + func.return %0 : tensor<2x8x4x7x7xf32> +} + +// ----- + +// CHECK-LABEL: maxpool_explicit_padding +func.func @maxpool_explicit_padding(%arg0: tensor<2x12x20x7xi32>) -> tensor<2x3x5x7xi32> { + // CHECK: tf.MaxPool + // TODO(b/165938852): need to support explicit padding in max_pool. + + %0 = "tf.MaxPool"(%arg0) {data_format = "NHWC", ksize = [1, 2, 2, 1], padding = "EXPLICIT", strides = [1, 4, 4, 1]} : (tensor<2x12x20x7xi32>) -> tensor<2x3x5x7xi32> + func.return %0 : tensor<2x3x5x7xi32> +} + +//===----------------------------------------------------------------------===// +// MaxPoolGrad op legalizations. +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: @max_pool_grad_valid +// CHECK-SAME: %[[INPUT:.*]]: tensor<10x24x24x64xf32>, %arg1: tensor<10x12x12x64xf32>, %[[GRAD:.*]]: tensor<10x12x12x64xf32> +func.func @max_pool_grad_valid(%orig_input: tensor<10x24x24x64xf32>, %orig_output: tensor<10x12x12x64xf32>, %grad: tensor<10x12x12x64xf32>) -> tensor<10x24x24x64xf32> { + // CHECK: %[[ZERO:.*]] = stablehlo.constant dense<0.000000e+00> : tensor + // CHECK: %[[RESULT:.*]] = "stablehlo.select_and_scatter"(%[[INPUT]], %[[GRAD]], %[[ZERO]]) + // CHECK-SAME: <{window_dimensions = array, window_strides = array}> ({ + // CHECK: ^bb0(%[[VALUE_A:.*]]: tensor, %[[VALUE_B:.*]]: tensor): + // CHECK: %[[SELECT_RESULT:.*]] = stablehlo.compare GE, %[[VALUE_A]], %[[VALUE_B]], NOTYPE : (tensor, tensor) -> tensor + // CHECK: stablehlo.return %[[SELECT_RESULT]] : tensor + // CHECK: }, { + // CHECK: ^bb0(%[[VALUE_A:.*]]: tensor, %[[VALUE_B:.*]]: tensor): + // CHECK: %[[SELECT_RESULT:.*]] = stablehlo.add %[[VALUE_A]], %[[VALUE_B]] : tensor + // CHECK: stablehlo.return %[[SELECT_RESULT]] : tensor + // CHECK: }) : (tensor<10x24x24x64xf32>, tensor<10x12x12x64xf32>, tensor) -> tensor<10x24x24x64xf32> + // CHECK: return %[[RESULT]] : tensor<10x24x24x64xf32> + %result = "tf.MaxPoolGrad"(%orig_input, %orig_output, %grad) { + data_format = "NHWC", + ksize = [1, 2, 2, 1], + padding = "VALID", + strides = [1, 2, 2, 1] + } : (tensor<10x24x24x64xf32>, tensor<10x12x12x64xf32>, tensor<10x12x12x64xf32>) -> tensor<10x24x24x64xf32> + func.return %result : tensor<10x24x24x64xf32> +} + +// ----- + +// CHECK-LABEL: @max_pool_3d_grad_valid +// CHECK-SAME: %[[INPUT:.*]]: tensor<10x8x24x24x64xf32>, %arg1: tensor<10x8x12x12x64xf32>, %[[GRAD:.*]]: tensor<10x8x12x12x64xf32> +func.func @max_pool_3d_grad_valid(%orig_input: tensor<10x8x24x24x64xf32>, %orig_output: tensor<10x8x12x12x64xf32>, %grad: tensor<10x8x12x12x64xf32>) -> tensor<10x8x24x24x64xf32> { + // CHECK: %[[ZERO:.*]] = stablehlo.constant dense<0.000000e+00> : tensor + // CHECK: %[[RESULT:.*]] = "stablehlo.select_and_scatter"(%[[INPUT]], %[[GRAD]], %[[ZERO]]) + // CHECK-SAME: <{window_dimensions = array, window_strides = array}> ({ + // CHECK: ^bb0(%[[VALUE_A:.*]]: tensor, %[[VALUE_B:.*]]: tensor): + // CHECK: %[[SELECT_RESULT:.*]] = stablehlo.compare GE, %[[VALUE_A]], %[[VALUE_B]], NOTYPE : (tensor, tensor) -> tensor + // CHECK: stablehlo.return %[[SELECT_RESULT]] : tensor + // CHECK: }, { + // CHECK: ^bb0(%[[VALUE_A:.*]]: tensor, %[[VALUE_B:.*]]: tensor): + // CHECK: %[[SELECT_RESULT:.*]] = stablehlo.add %[[VALUE_A]], %[[VALUE_B]] : tensor + // CHECK: stablehlo.return %[[SELECT_RESULT]] : tensor + // CHECK: }) : (tensor<10x8x24x24x64xf32>, tensor<10x8x12x12x64xf32>, tensor) -> tensor<10x8x24x24x64xf32> + // CHECK: return %[[RESULT]] : tensor<10x8x24x24x64xf32> + %result = "tf.MaxPool3DGrad"(%orig_input, %orig_output, %grad) {data_format = "NDHWC", ksize = [1, 1, 2, 2, 1], padding = "VALID", strides = [1, 1, 2, 2, 1]} : (tensor<10x8x24x24x64xf32>, tensor<10x8x12x12x64xf32>, tensor<10x8x12x12x64xf32>) -> tensor<10x8x24x24x64xf32> + func.return %result : tensor<10x8x24x24x64xf32> +} + +// ----- + +// CHECK-LABEL: @max_pool_grad_same +func.func @max_pool_grad_same(%orig_input: tensor<2x13x25x7xf32>, %orig_output: tensor<2x4x7x7xf32>, %grad: tensor<2x4x7x7xf32>) -> tensor<2x13x25x7xf32> { + // CHECK: padding = dense<{{\[\[}}0, 0], [0, 1], [1, 1], [0, 0]]> : tensor<4x2xi64> + %result = "tf.MaxPoolGrad"(%orig_input, %orig_output, %grad) { + data_format = "NHWC", + ksize = [1, 2, 3, 1], + padding = "SAME", + strides = [1, 4, 4, 1] + } : (tensor<2x13x25x7xf32>, tensor<2x4x7x7xf32>, tensor<2x4x7x7xf32>) -> tensor<2x13x25x7xf32> + func.return %result : tensor<2x13x25x7xf32> +} + +// ----- + +// CHECK-LABEL: @max_pool_3d_grad_same +func.func @max_pool_3d_grad_same(%orig_input: tensor<2x8x13x25x7xf32>, %orig_output: tensor<2x8x4x7x7xf32>, %grad: tensor<2x8x4x7x7xf32>) -> tensor<2x8x13x25x7xf32> { + // CHECK: padding = dense<{{\[\[}}0, 0], [0, 0], [0, 1], [1, 1], [0, 0]]> : tensor<5x2xi64> + %result = "tf.MaxPool3DGrad"(%orig_input, %orig_output, %grad) {data_format = "NDHWC", ksize = [1, 1, 2, 3, 1], padding = "SAME", strides = [1, 1, 4, 4, 1]} : (tensor<2x8x13x25x7xf32>, tensor<2x8x4x7x7xf32>, tensor<2x8x4x7x7xf32>) -> tensor<2x8x13x25x7xf32> + func.return %result : tensor<2x8x13x25x7xf32> +} + +//===----------------------------------------------------------------------===// +// OneHot op legalizations. +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL:one_hot +func.func @one_hot(%indices: tensor<3xi32>, %on_value: tensor, %off_value: tensor) -> tensor<3x5xf32> { + // CHECK-NEXT: %[[IOTA0:.*]] = stablehlo.iota dim = 0 : tensor<5xi32> + // CHECK-NEXT: %[[BCAST0:.*]] = stablehlo.broadcast_in_dim %[[IOTA0]], dims = [1] : (tensor<5xi32>) -> tensor<3x5xi32> + // CHECK-NEXT: %[[BCAST1:.*]] = stablehlo.broadcast_in_dim %arg0, dims = [0] : (tensor<3xi32>) -> tensor<3x5xi32> + // CHECK-NEXT: %[[CMP0:.*]] = stablehlo.compare EQ, %[[BCAST1]], %[[BCAST0]], NOTYPE : (tensor<3x5xi32>, tensor<3x5xi32>) -> tensor<3x5xi1> + // CHECK-NEXT: %[[BCAST2:.*]] = stablehlo.broadcast %arg1, sizes = [3, 5] : (tensor) -> tensor<3x5xf32> + // CHECK-NEXT: %[[BCAST3:.*]] = stablehlo.broadcast %arg2, sizes = [3, 5] : (tensor) -> tensor<3x5xf32> + // CHECK-NEXT: %[[RESULT:.*]] = stablehlo.select %[[CMP0]], %[[BCAST2]], %[[BCAST3]] : tensor<3x5xi1>, tensor<3x5xf32> + // CHECK-NEXT: return %[[RESULT]] : tensor<3x5xf32> + %depth = "tf.Const"() { value = dense<5> : tensor } : () -> tensor + %result = "tf.OneHot"(%indices, %depth, %on_value, %off_value) {axis = -1 : i64} : (tensor<3xi32>, tensor, tensor, tensor) -> tensor<3x5xf32> + func.return %result : tensor<3x5xf32> +} + +//===----------------------------------------------------------------------===// +// tf.OutfeedEnqueueTuple legalization +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: func @outfeed_enqueue_tuple +// CHECK-SAME: [[VAL_0:%.*]]: tensor<3xi32>, [[VAL_1:%.*]]: tensor<4xf32>) +func.func @outfeed_enqueue_tuple(%data_1: tensor<3xi32>, %data_2: tensor<4xf32>) -> () { + // CHECK: [[TOKEN:%.*]] = stablehlo.create_token : !stablehlo.token + // CHECK: "stablehlo.outfeed"([[VAL_0]], [[VAL_1]], [[TOKEN]]) <{outfeed_config = ""}> : (tensor<3xi32>, tensor<4xf32>, !stablehlo.token) -> !stablehlo.token + "tf.OutfeedEnqueueTuple"(%data_1, %data_2) : (tensor<3xi32>, tensor<4xf32>) -> () + func.return +} + +//===----------------------------------------------------------------------===// +// Pack op legalizations. +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: func @pack +func.func @pack(%arg0: tensor<2xi32>, %arg1: tensor<2xi32>) -> tensor<2x2xi32> { + // CHECK: stablehlo.reshape {{.*}} : (tensor<2xi32>) -> tensor<1x2xi32> + // CHECK: stablehlo.reshape {{.*}} : (tensor<2xi32>) -> tensor<1x2xi32> + // CHECK: stablehlo.concatenate {{.*}}, {{.*}}, dim = 0 + + %0 = "tf.Pack"(%arg0, %arg1) : (tensor<2xi32>, tensor<2xi32>) -> tensor<2x2xi32> + func.return %0 : tensor<2x2xi32> +} + +//===----------------------------------------------------------------------===// +// PartitionedCall op legalization. +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: func @partitioned_call +func.func @partitioned_call(%arg0: tensor) -> tensor { + // CHECK: call @pcall_func(%arg0) : (tensor) -> tensor + %0 = "tf.PartitionedCall"(%arg0) {config = "", config_proto = "", executor_type = "", f = @pcall_func} : (tensor) -> (tensor) + func.return %0 : tensor +} + + +func.func @pcall_func(%arg0: tensor) -> tensor { + func.return %arg0 : tensor +} + +// ----- + +// CHECK-LABEL: func @partitioned_call_multi_input +func.func @partitioned_call_multi_input(%arg0: tensor, %arg1: tensor) -> tensor { + // CHECK: call @pcall_multi_input(%arg0, %arg1) : (tensor, tensor) -> tensor + %0 = "tf.PartitionedCall"(%arg0, %arg1) {config = "", config_proto = "", executor_type = "", f = @pcall_multi_input} : (tensor, tensor) -> (tensor) + func.return %0 : tensor +} + + +func.func @pcall_multi_input(%arg0: tensor, %arg1: tensor) -> tensor { + func.return %arg0 : tensor +} + +// ----- + +// CHECK-LABEL: func @partitioned_call_multi_in_out +func.func @partitioned_call_multi_in_out(%arg0: tensor, %arg1: tensor) -> (tensor, tensor) { + // CHECK: call @pcall_multi_in_out(%arg0, %arg1) : (tensor, tensor) -> (tensor, tensor) + %0, %1 = "tf.PartitionedCall"(%arg0, %arg1) {config = "", config_proto = "", executor_type = "", f = @pcall_multi_in_out} : (tensor, tensor) -> (tensor, tensor) + func.return %0, %1 : tensor, tensor +} + + +func.func @pcall_multi_in_out(%arg0: tensor, %arg1: tensor) -> (tensor, tensor) { + func.return %arg1, %arg0 : tensor, tensor +} + +// CHECK-LABEL: func @unhandled_partitioned_call +func.func @unhandled_partitioned_call(%arg0: tensor<*xi32>, %arg1: tensor<*xi32>) -> (tensor, tensor) { + // The argument types don't match the parameter types for the + // pcall_multi_in_out function. That's fine for a PartitionedCallOp but not + // for a standard CallOp, so this op can't be lowered. + // CHECK: "tf.PartitionedCall" + %0, %1 = "tf.PartitionedCall"(%arg0, %arg1) {config = "", config_proto = "", executor_type = "", f = @pcall_multi_in_out} : (tensor<*xi32>, tensor<*xi32>) -> (tensor, tensor) + func.return %0, %1 : tensor, tensor +} + + +// CHECK-LABEL: func @unhandled_partitioned_call_2 +func.func @unhandled_partitioned_call_2(%arg0: tensor, %arg1: tensor<*xi32>) -> (tensor, tensor) { + // CHECK: "tf.PartitionedCall" + %0, %1 = "tf.PartitionedCall"(%arg0, %arg1) {config = "", config_proto = "", executor_type = "", f = @pcall_multi_in_out} : (tensor, tensor<*xi32>) -> (tensor, tensor) + func.return %0, %1 : tensor, tensor +} + +// ----- + +// CHECK-LABEL: func @no_args_and_results +func.func @no_args_and_results() { + // CHECK: call @callee() : () -> () + // CHECK: call @callee() : () -> () + // CHECK: call @callee() : () -> () + "tf.PartitionedCall"() {config = "", config_proto = "", executor_type = "", f = @callee} : () -> () + "tf.StatefulPartitionedCall"() {config = "", config_proto = "", executor_type = "", f = @callee} : () -> () + "tf.LegacyCall"() {config = "", config_proto = "", executor_type = "", f = @callee} : () -> () + func.return +} + +func.func @callee() { + func.return +} + +//===----------------------------------------------------------------------===// +// ReverseV2 op legalization. +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: @reverse_func_32 +func.func @reverse_func_32(%arg0: tensor<5xi32>) -> tensor<5xi32> { + %axis = "tf.Const"() {value = dense<0> : tensor<1xi32>} : () -> (tensor<1xi32>) + + // CHECK: [[VAL:%.+]] = stablehlo.reverse %arg0, dims = [0] : tensor<5xi32> + %reversed = "tf.ReverseV2"(%arg0, %axis) : (tensor<5xi32>, tensor<1xi32>) -> tensor<5xi32> + + // CHECK: return [[VAL]] : tensor<5xi32> + func.return %reversed : tensor<5xi32> +} + +// ----- + +// CHECK-LABEL: @reverse_func_64 +func.func @reverse_func_64(%arg0: tensor<5xi32>) -> tensor<5xi32> { + %axis = "tf.Const"() {value = dense<0> : tensor<1xi64>} : () -> (tensor<1xi64>) + + // CHECK: [[VAL:%.+]] = stablehlo.reverse %arg0, dims = [0] : tensor<5xi32> + %reversed = "tf.ReverseV2"(%arg0, %axis) : (tensor<5xi32>, tensor<1xi64>) -> tensor<5xi32> + + // CHECK: return [[VAL]] : tensor<5xi32> + func.return %reversed : tensor<5xi32> +} + +// ----- + +// CHECK-LABEL: @reverse_func_neg +func.func @reverse_func_neg(%arg0: tensor<5x5xi32>) -> tensor<5x5xi32> { + %axis = "tf.Const"() {value = dense<[-1]> : tensor<1xi32>} : () -> (tensor<1xi32>) + + // CHECK: [[VAL:%.+]] = stablehlo.reverse %arg0, dims = [1] : tensor<5x5xi32> + %reversed = "tf.ReverseV2"(%arg0, %axis) : (tensor<5x5xi32>, tensor<1xi32>) -> tensor<5x5xi32> + + // CHECK: return [[VAL]] : tensor<5x5xi32> + func.return %reversed : tensor<5x5xi32> +} + +//===----------------------------------------------------------------------===// +// StatefulPartitionedCall op legalization. +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: func @stateful_partitioned_call +// CHECK-SAME: [[ARG:%.+]]: tensor +func.func @stateful_partitioned_call(%arg0: tensor) -> tensor { + // CHECK: call @stateful_pcall_func([[ARG]]) : (tensor) -> tensor + %0 = "tf.StatefulPartitionedCall"(%arg0) {config = "", config_proto = "", executor_type = "", f = @stateful_pcall_func} : (tensor) -> (tensor) + func.return %0 : tensor +} + +func.func @stateful_pcall_func(%arg0: tensor) -> tensor { + func.return %arg0 : tensor +} + +// ----- + +// CHECK-LABEL: func @stateful_partitioned_call_multi_in_out +// CHECK-SAME: ([[ARG0:%.+]]: tensor, [[ARG1:%.+]]: tensor) +func.func @stateful_partitioned_call_multi_in_out(%arg0: tensor, %arg1: tensor) -> (tensor, tensor) { + // CHECK: call @stateful_pcall_multi_in_out([[ARG0]], [[ARG1]]) : (tensor, tensor) -> (tensor, tensor) + %0, %1 = "tf.StatefulPartitionedCall"(%arg0, %arg1) {config = "", config_proto = "", executor_type = "", f = @stateful_pcall_multi_in_out} : (tensor, tensor) -> (tensor, tensor) + func.return %0, %1 : tensor, tensor +} + +func.func @stateful_pcall_multi_in_out(%arg0: tensor, %arg1: tensor) -> (tensor, tensor) { + func.return %arg1, %arg0 : tensor, tensor +} + +//===----------------------------------------------------------------------===// +// Elu op legalizations. +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: func @elu +func.func @elu(%arg0: tensor<1xf32>) -> tensor<1xf32> { + // CHECK-DAG: %[[ZERO:.*]] = stablehlo.constant dense<0.000000e+00> : tensor<1xf32> + // CHECK-DAG: %[[PRED:.*]] = stablehlo.compare GT, %arg0, %[[ZERO]] + // CHECK-DAG: %[[EXP:.*]] = stablehlo.exponential_minus_one %arg0 + // CHECK: %[[RESULT:.*]] = stablehlo.select %[[PRED]], %arg0, %[[EXP]] + // CHECK: return %[[RESULT]] + %0 = "tf.Elu"(%arg0) : (tensor<1xf32>) -> tensor<1xf32> + func.return %0: tensor<1xf32> +} + +// ----- + +// CHECK-LABEL: func @elu_grad +// CHECK-SAME: (%[[GRADIENTS:.*]]: tensor<4x8xf32>, %[[FEATURES:.*]]: tensor) +func.func @elu_grad(%gradients: tensor<4x8xf32>, %features: tensor) -> tensor<4x8xf32> { + // CHECK-DAG: %[[ZERO:.*]] = stablehlo.constant dense<0.000000e+00> : tensor + // CHECK-DAG: %[[ONE:.*]] = stablehlo.constant dense<1.000000e+00> : tensor + // CHECK-DAG: %[[BCAST0:.*]] = stablehlo.dynamic_broadcast_in_dim %[[ZERO]], {{.*}}, dims = [] : (tensor, tensor<2xindex>) -> tensor + // CHECK-DAG: %[[PRED:.*]] = stablehlo.compare GT, %[[FEATURES]], %[[BCAST0]] + // CHECK-DAG: %[[BCAST1:.*]] = stablehlo.dynamic_broadcast_in_dim %[[ONE]], {{.*}}, dims = [] : (tensor, tensor<2xindex>) -> tensor + // CHECK-DAG: %[[ADD1:.*]] = stablehlo.add %[[FEATURES]], %[[BCAST1]] + // CHECK-DAG: %[[MULGRAD:.*]] = stablehlo.multiply %[[GRADIENTS]], %[[ADD1]] : (tensor<4x8xf32>, tensor) -> tensor<4x8xf32> + // CHECK: %[[RESULT:.*]] = stablehlo.select %[[PRED]], %[[GRADIENTS]], %[[MULGRAD]] + // CHECK: return %[[RESULT]] + %2 = "tf.EluGrad"(%gradients, %features) : (tensor<4x8xf32>, tensor) -> tensor<4x8xf32> + func.return %2 : tensor<4x8xf32> +} + +//===----------------------------------------------------------------------===// +// Relu op legalizations. +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: func @relu +func.func @relu(%arg0: tensor<1xi32>) -> tensor<1xi32> { + // CHECK: %[[ZERO:.*]] = stablehlo.constant dense<0> : tensor<1xi32> + // CHECK: stablehlo.maximum %arg0, %[[ZERO]] + %0 = "tf.Relu"(%arg0) : (tensor<1xi32>) -> tensor<1xi32> + func.return %0: tensor<1xi32> +} + +// ----- + +// CHECK-LABEL: func @relu_unsigned +func.func @relu_unsigned(%arg0: tensor) -> tensor { + // CHECK: %[[ZERO:.*]] = stablehlo.constant dense<0> : tensor + // CHECK: %[[BCAST0:.*]] = stablehlo.dynamic_broadcast_in_dim %[[ZERO]], {{.*}}, dims = [] + // CHECK: stablehlo.maximum %arg0, %[[BCAST0]] + %0 = "tf.Relu"(%arg0) : (tensor) -> tensor + func.return %0: tensor +} + +// ----- + +// CHECK-LABEL: func @relu6 +func.func @relu6(%arg0: tensor<1xi32>) -> tensor<1xi32> { + // CHECK-DAG: %[[ZERO:.*]] = stablehlo.constant dense<0> : tensor + // CHECK-DAG: %[[SIX:.*]] = stablehlo.constant dense<6> : tensor + // CHECK: stablehlo.clamp %[[ZERO]], %arg0, %[[SIX]] + %0 = "tf.Relu6"(%arg0) : (tensor<1xi32>) -> tensor<1xi32> + func.return %0: tensor<1xi32> +} + +// ----- + +// CHECK-LABEL: func @relu6_unsigned +func.func @relu6_unsigned(%arg0: tensor) -> tensor { + // CHECK-DAG: %[[ZERO:.*]] = stablehlo.constant dense<0> : tensor + // CHECK-DAG: %[[SIX:.*]] = stablehlo.constant dense<6> : tensor + // CHECK: stablehlo.clamp %[[ZERO]], %arg0, %[[SIX]] + %0 = "tf.Relu6"(%arg0) : (tensor) -> tensor + func.return %0: tensor +} + +// ----- + +// CHECK-LABEL: func @leaky_relu +func.func @leaky_relu(%arg0: tensor<1x4x4x3xf32>) -> tensor<1x4x4x3xf32> attributes {tf.entry_function = {}} { + // CHECK-NEXT: %[[ALPHA:.*]] = stablehlo.constant dense<2.000000e-01> + // CHECK-NEXT: %[[ZERO:.*]] = stablehlo.constant dense<0.000000e+00> + // CHECK-NEXT: %[[LEAKY:.*]] = stablehlo.multiply %arg0, %[[ALPHA]] + // CHECK-NEXT: %[[CMP:.*]] = stablehlo.compare GT, %arg0, %[[ZERO]] + // CHECK-NEXT: %[[RES:.*]] = stablehlo.select %[[CMP]], %arg0, %[[LEAKY]] + // CHECK-NEXT: return %[[RES]] : tensor<1x4x4x3xf32> + %0 = "tf.LeakyRelu"(%arg0) {alpha = 2.000000e-01 : f32, device = ""} : (tensor<1x4x4x3xf32>) -> tensor<1x4x4x3xf32> + func.return %0 : tensor<1x4x4x3xf32> +} + +// ----- + +// CHECK-LABEL: func @leaky_relu_grad +func.func @leaky_relu_grad(%arg0: tensor<1x4x4xf32>, %arg1: tensor<1x4x4xf32>) -> tensor<1x4x4xf32> attributes {tf.entry_function = {}} { + // CHECK-NEXT: %[[ALPHA:.*]] = stablehlo.constant dense<2.000000e-01> + // CHECK-NEXT: %[[ZERO:.*]] = stablehlo.constant dense<0.000000e+00> + // CHECK-NEXT: %[[LEAKYGRAD:.*]] = stablehlo.multiply %[[GRADIENT:.*]], %[[ALPHA]] + // CHECK-NEXT: %[[CMP:.*]] = stablehlo.compare GT, %[[INP:.*]], %[[ZERO]], NOTYPE + // CHECK-NEXT: %[[RES:.*]] = stablehlo.select %[[CMP]], %[[GRADIENT]], %[[LEAKYGRAD]] + // CHECK-NEXT: return %[[RES]] : tensor<1x4x4xf32> + %0 = "tf.LeakyReluGrad"(%arg0, %arg1) {alpha = 2.000000e-01 : f32, device = ""} : (tensor<1x4x4xf32>, tensor<1x4x4xf32>) -> tensor<1x4x4xf32> + func.return %0 : tensor<1x4x4xf32> +} + +// ----- + +// CHECK-LABEL: func @softsign +func.func @softsign(%arg0: tensor<4x10xf32>) -> tensor<4x10xf32> { + // CHECK-NEXT: %[[ONE:.*]] = stablehlo.constant dense<1.000000e+00> + // CHECK-NEXT: %[[ABS:.*]] = stablehlo.abs %{{.*}} + // CHECK-NEXT: %[[ADD:.*]] = stablehlo.add %[[ABS]], %[[ONE]] + // CHECK-NEXT: %[[DIV:.*]] = stablehlo.divide %{{.*}}, %[[ADD]] + // CHECK-NEXT: return %[[DIV]] : tensor<4x10xf32> + %0 = "tf.Softsign"(%arg0) : (tensor<4x10xf32>) -> tensor<4x10xf32> + func.return %0 : tensor<4x10xf32> +} + +// ----- + +// CHECK-LABEL: func @softsign_grad +func.func @softsign_grad(%arg0: tensor<4x10xf32>, %arg1: tensor<4x10xf32>) -> tensor<4x10xf32> { + + // CHECK-NEXT: %[[ONE:.*]] = stablehlo.constant dense<1.000000e+00> + // CHECK-NEXT: %[[ABS:.*]] = stablehlo.abs %{{.*}} : tensor<4x10xf32> + // CHECK-NEXT: %[[BROADCAST_ADD:.*]] = stablehlo.add %[[ABS]], %[[ONE]] + // CHECK-NEXT: %[[MUL:.*]] = stablehlo.multiply %[[BROADCAST_ADD]], %[[BROADCAST_ADD]] + // CHECK-NEXT: %[[BROADCAST_DIV:.*]] = stablehlo.divide %{{.*}}, %[[MUL]] + // CHECK-NEXT: return %[[BROADCAST_DIV]] : tensor<4x10xf32> + %0 = "tf.SoftsignGrad"(%arg0, %arg1) : (tensor<4x10xf32>, tensor<4x10xf32>) -> tensor<4x10xf32> + func.return %0 : tensor<4x10xf32> +} + +//===----------------------------------------------------------------------===// +// Roll op legalizations. +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: func @Roll_0D +func.func @Roll_0D(%arg0: tensor<512xi32>, %shift: tensor) -> tensor<512xi32> { + %axis = "tf.Const"() {value = dense<0> : tensor} : () -> (tensor) + // CHECK: %[[AXIS_SIZE:.*]] = stablehlo.constant dense<512> : tensor + // CHECK: %[[T1:.+]] = stablehlo.remainder %arg1, %[[AXIS_SIZE]] : tensor + // CHECK: %[[T2:.+]] = stablehlo.add %[[T1]], %[[AXIS_SIZE]] : tensor + // CHECK: %[[T3:.+]] = stablehlo.remainder %[[T2]], %[[AXIS_SIZE]] : tensor + // CHECK: %[[CONCAT:.+]] = stablehlo.concatenate %arg0, %arg0, dim = 0 + // CHECK: %[[OFFSET:.+]] = stablehlo.subtract %[[AXIS_SIZE]], %[[T3]] : tensor + // CHECK: stablehlo.dynamic_slice %[[CONCAT]], %[[OFFSET]], sizes = [512] + %0 = "tf.Roll"(%arg0, %shift, %axis) {device = ""} : (tensor<512xi32>, tensor, tensor) -> tensor<512xi32> + func.return %0 : tensor<512xi32> +} + +//===----------------------------------------------------------------------===// +// Select op legalizations. +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: func @select_batch_static +func.func @select_batch_static(%arg0: tensor<2xi1>, %arg1: tensor<2x6x8xi32>, %arg2: tensor<2x6x8xi32>) -> tensor<2x6x8xi32> { + // CHECK: %[[BCAST:.*]] = stablehlo.broadcast_in_dim %arg0, dims = [0] + // CHECK: stablehlo.select %[[BCAST]], %arg1, %arg2 + %0 = "tf.Select"(%arg0, %arg1, %arg2) : (tensor<2xi1>, tensor<2x6x8xi32>, tensor<2x6x8xi32>) -> tensor<2x6x8xi32> + func.return %0: tensor<2x6x8xi32> +} + +// ----- + +// CHECK-LABEL: func @select_batch_static_r1 +func.func @select_batch_static_r1(%arg0: tensor, %arg1: tensor<2x6x8xi32>, %arg2: tensor<2x6x8xi32>) -> tensor<2x6x8xi32> { + // CHECK: stablehlo.select %arg0, %arg1, %arg2 + %0 = "tf.Select"(%arg0, %arg1, %arg2) : (tensor, tensor<2x6x8xi32>, tensor<2x6x8xi32>) -> tensor<2x6x8xi32> + func.return %0: tensor<2x6x8xi32> +} + +// ----- + +// CHECK-LABEL: func @select_batch_static_all_same +func.func @select_batch_static_all_same(%arg0: tensor<2x6x8xi1>, %arg1: tensor<2x6x8xi32>, %arg2: tensor<2x6x8xi32>) -> tensor<2x6x8xi32> { + // CHECK: stablehlo.select %arg0, %arg1, %arg2 + %0 = "tf.Select"(%arg0, %arg1, %arg2) : (tensor<2x6x8xi1>, tensor<2x6x8xi32>, tensor<2x6x8xi32>) -> tensor<2x6x8xi32> + func.return %0: tensor<2x6x8xi32> +} + +// ----- + +// CHECK-LABEL: func @select_batch_dynamic_r1 +func.func @select_batch_dynamic_r1(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor { + // CHECK-NEXT: %[[C1:.*]] = arith.constant 1 : index + // CHECK-NEXT: %[[SHAPE0:.*]] = shape.shape_of %arg0 : tensor -> tensor<1xindex> + // CHECK-NEXT: %[[SHAPE1:.*]] = shape.shape_of %arg1 : tensor -> tensor<3xindex> + // CHECK-NEXT: %[[SHAPE2:.*]] = shape.shape_of %arg2 : tensor -> tensor<3xindex> + // CHECK-NEXT: %[[SHAPEEQ1:.*]] = shape.cstr_eq %[[SHAPE1]], %[[SHAPE2]] : tensor<3xindex>, tensor<3xindex> + // CHECK-NEXT: %[[HEAD:.*]], %[[TAIL:.*]] = "shape.split_at"(%[[SHAPE1]], %[[C1]]) : (tensor<3xindex>, index) -> (tensor<1xindex>, tensor<2xindex>) + // CHECK-NEXT: %[[SHAPEEQ2:.*]] = shape.cstr_eq %[[SHAPE0]], %[[HEAD]] : tensor<1xindex>, tensor<1xindex> + // CHECK-NEXT: %[[SHAPEEQ:.*]] = shape.assuming_all %[[SHAPEEQ1]], %[[SHAPEEQ2]] + // CHECK-NEXT: %[[ASSUMING:.*]] = shape.assuming %[[SHAPEEQ]] -> (tensor) { + // CHECK-NEXT: %[[BCAST:.*]] = stablehlo.dynamic_broadcast_in_dim %arg0, %[[SHAPE1]], dims = [0] + // CHECK-NEXT: %[[SELECT:.*]] = stablehlo.select %[[BCAST]], %arg1, %arg2 : tensor, tensor + // CHECK-NEXT: shape.assuming_yield %[[SELECT]] : tensor + %0 = "tf.Select"(%arg0, %arg1, %arg2) : (tensor, tensor, tensor) -> tensor + func.return %0: tensor +} + +// ----- + +// CHECK-LABEL: func @select_batch_dynamic +func.func @select_batch_dynamic(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor { + // CHECK-NEXT: %[[SHAPE0:.*]] = shape.shape_of %arg0 : tensor -> tensor<3xindex> + // CHECK-NEXT: %[[SHAPE1:.*]] = shape.shape_of %arg1 : tensor -> tensor<3xindex> + // CHECK-NEXT: %[[SHAPE2:.*]] = shape.shape_of %arg2 : tensor -> tensor<3xindex> + // CHECK-NEXT: %[[SHAPEEQ1:.*]] = shape.cstr_eq %[[SHAPE1]], %[[SHAPE2]] : tensor<3xindex>, tensor<3xindex> + // CHECK-NEXT: %[[SHAPEEQ2:.*]] = shape.cstr_eq %[[SHAPE0]], %[[SHAPE1]] : tensor<3xindex>, tensor<3xindex> + // CHECK-NEXT: %[[SHAPEEQ3:.*]] = shape.cstr_eq %[[SHAPE1]], %[[SHAPE2]], %[[SHAPE0]], %[[SHAPE1]] : tensor<3xindex>, tensor<3xindex>, tensor<3xindex>, tensor<3xindex> + // CHECK-NEXT: %[[SHAPEEQ:.*]] = shape.assuming %[[SHAPEEQ3]] + // CHECK-NEXT: %[[SELECT:.*]] = stablehlo.select %arg0, %arg1, %arg2 : tensor, tensor + // CHECK-NEXT: shape.assuming_yield %[[SELECT]] : tensor + %0 = "tf.Select"(%arg0, %arg1, %arg2) : (tensor, tensor, tensor) -> tensor + func.return %0: tensor +} + +// ----- + +// CHECK-LABEL: testSelectInvalidUnranked +func.func @testSelectInvalidUnranked(%arg0: tensor<6x7xi1>, %arg1: tensor<*xf16>, %arg2: tensor<*xf16>) -> tensor<*xf16> { + // CHECK-NEXT: tf.Select + %0 = "tf.Select"(%arg0, %arg1, %arg2) : (tensor<6x7xi1>, tensor<*xf16>, tensor<*xf16>) -> tensor<*xf16> + func.return %0: tensor<*xf16> +} + +// ----- + +// CHECK-LABEL: testSelectThenUnranked +func.func @testSelectThenUnranked(%arg0: tensor<3xi1>, %arg1: tensor<*xf16>, %arg2: tensor<3x2xf16>) -> tensor<*xf16> { + // CHECK-NEXT: tf.Select + %0 = "tf.Select"(%arg0, %arg1, %arg2) : (tensor<3xi1>, tensor<*xf16>, tensor<3x2xf16>) -> tensor<*xf16> + func.return %0: tensor<*xf16> +} + +// ----- + +// CHECK-LABEL: testSelectElseUnranked +func.func @testSelectElseUnranked(%arg0: tensor<3xi1>, %arg1: tensor<3x2xf16>, %arg2: tensor<*xf16>) -> tensor<*xf16> { + // CHECK-NEXT: tf.Select + %0 = "tf.Select"(%arg0, %arg1, %arg2) : (tensor<3xi1>, tensor<3x2xf16>, tensor<*xf16>) -> tensor<*xf16> + func.return %0: tensor<*xf16> +} + +// ----- + +// CHECK-LABEL: func @selectv2_dynamic_ranked +func.func @selectv2_dynamic_ranked(%arg0: tensor<1xi1>, %arg1: tensor<2x?x8xi32>, %arg2: tensor<2x8x8xi32>) -> tensor<2x?x8xi32> { + // CHECK: stablehlo.select + %0 = "tf.SelectV2"(%arg0, %arg1, %arg2) : (tensor<1xi1>, tensor<2x?x8xi32>, tensor<2x8x8xi32>) -> tensor<2x?x8xi32> + func.return %0: tensor<2x?x8xi32> +} + +//===----------------------------------------------------------------------===// +// Fast Fourier Transform op legalization. +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: func @fft_1D +func.func @fft_1D(%arg0: tensor<8xcomplex>) -> tensor<8xcomplex> { + // CHECK: stablehlo.fft %arg0, type = FFT, length = [8] + %0 = "tf.FFT"(%arg0) : (tensor<8xcomplex>) -> tensor<8xcomplex> + func.return %0 : tensor<8xcomplex> +} + +// ----- + +// CHECK-LABEL: func @ifft_1D +func.func @ifft_1D(%arg0: tensor<8xcomplex>) -> tensor<8xcomplex> { + // CHECK: stablehlo.fft %arg0, type = IFFT, length = [8] + %0 = "tf.IFFT"(%arg0) : (tensor<8xcomplex>) -> tensor<8xcomplex> + func.return %0 : tensor<8xcomplex> +} + +// ----- + +// CHECK-LABEL: func @rfft_1D +func.func @rfft_1D(%arg0: tensor<8xf32>) -> tensor<5xcomplex> { + %fftlength = "tf.Const"() {value = dense<[8]> : tensor<1xi32>} : () -> (tensor<1xi32>) + // CHECK: stablehlo.fft %arg0, type = RFFT, length = [8] + %0 = "tf.RFFT"(%arg0, %fftlength) : (tensor<8xf32>, tensor<1xi32>) -> tensor<5xcomplex> + func.return %0 : tensor<5xcomplex> +} + +// ----- + +// CHECK-LABEL: func @rfft_1D_padded +func.func @rfft_1D_padded(%arg0: tensor<7xf32>) -> tensor<5xcomplex> { + %fftlength = "tf.Const"() {value = dense<[8]> : tensor<1xi32>} : () -> (tensor<1xi32>) + // CHECK: %[[PADDED:.*]] = stablehlo.pad %arg0, %{{.*}}, low = [0], high = [1], interior = [0] + // CHECK: stablehlo.fft %[[PADDED]], type = RFFT, length = [8] + %0 = "tf.RFFT"(%arg0, %fftlength) : (tensor<7xf32>, tensor<1xi32>) -> tensor<5xcomplex> + func.return %0 : tensor<5xcomplex> +} + +// ----- + +// CHECK-LABEL: func @rfft_1D_sliced +func.func @rfft_1D_sliced(%arg0: tensor<2x9xf32>) -> tensor<2x5xcomplex> { + %fftlength = "tf.Const"() {value = dense<[8]> : tensor<1xi32>} : () -> (tensor<1xi32>) + // CHECK: %[[SLICED:.*]] = stablehlo.slice %arg0 [0:2, 0:8] + // CHECK: stablehlo.fft %[[SLICED]], type = RFFT, length = [8] + %0 = "tf.RFFT"(%arg0, %fftlength) : (tensor<2x9xf32>, tensor<1xi32>) -> tensor<2x5xcomplex> + func.return %0 : tensor<2x5xcomplex> +} + +// ----- + +// CHECK-LABEL: func @irfft_1D +func.func @irfft_1D(%arg0: tensor<8xcomplex>) -> tensor<8xf32> { + %fftlength = "tf.Const"() {value = dense<[8]> : tensor<1xi32>} : () -> (tensor<1xi32>) + // CHECK: %[[SLICED:.*]] = stablehlo.slice %arg0 [0:5] + // CHECK: stablehlo.fft %[[SLICED]], type = IRFFT, length = [8] + %0 = "tf.IRFFT"(%arg0, %fftlength) : (tensor<8xcomplex>, tensor<1xi32>) -> tensor<8xf32> + func.return %0 : tensor<8xf32> +} + +// ----- + +// CHECK-LABEL: fft_1D_dynamic +func.func @fft_1D_dynamic(%arg0: tensor>) -> tensor<8xcomplex> { + // CHECK: "tf.FFT" + %0 = "tf.FFT"(%arg0) : (tensor>) -> tensor<8xcomplex> + func.return %0 : tensor<8xcomplex> +} + +// ----- + +// CHECK-LABEL: rfft_1D_dynamic +func.func @rfft_1D_dynamic(%arg0: tensor) -> tensor<8xcomplex> { + %fftlength = "tf.Const"() {value = dense<[8]> : tensor<1xi32>} : () -> (tensor<1xi32>) + // CHECK: "tf.RFFT" + %0 = "tf.RFFT"(%arg0, %fftlength) : (tensor, tensor<1xi32>) -> tensor<8xcomplex> + func.return %0 : tensor<8xcomplex> +} + +//===----------------------------------------------------------------------===// +// Shape op legalization. +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: func @shape_1D +func.func @shape_1D(%arg0: tensor) -> tensor<1xi32> { + // CHECK: [[SHAPE:%.+]] = shape.shape_of %arg0 + // CHECK: [[TENSOR:%.+]] = arith.index_cast [[SHAPE]] : tensor<1xindex> to tensor<1xi32> + %0 = "tf.Shape"(%arg0) : (tensor) -> tensor<1xi32> + + // CHECK: return [[TENSOR]] + func.return %0 : tensor<1xi32> +} + +// ----- + +// CHECK-LABEL: func @shape_2D +func.func @shape_2D(%arg0: tensor) -> tensor<2xi32> { + // CHECK: [[SHAPE:%.+]] = shape.shape_of %arg0 + // CHECK: [[TENSOR:%.+]] = arith.index_cast [[SHAPE]] : tensor<2xindex> to tensor<2xi32> + %0 = "tf.Shape"(%arg0) : (tensor) -> tensor<2xi32> + + // CHECK: return [[TENSOR]] + func.return %0 : tensor<2xi32> +} + +// ----- + +// CHECK-LABEL: func @shape_rankless +func.func @shape_rankless(%arg0: tensor<*xf32>) -> tensor { + // CHECK: [[SHAPE:%.+]] = shape.shape_of %arg0 + // CHECK: [[TENSOR:%.+]] = arith.index_cast [[SHAPE]] : tensor to tensor + %0 = "tf.Shape"(%arg0) : (tensor<*xf32>) -> tensor + + // CHECK: return [[TENSOR]] + func.return %0 : tensor +} + +//===----------------------------------------------------------------------===// +// Transpose op legalization. +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: @transpose_noop +func.func @transpose_noop(%arg0: tensor<2x3xf32>) -> tensor<2x3xf32> { + %permutation = "tf.Const"() {value = dense<[0, 1]> : tensor<2xi64>} : () -> (tensor<2xi64>) + // CHECK: return %arg0 + %0 = "tf.Transpose"(%arg0, %permutation) : (tensor<2x3xf32>, tensor<2xi64>) -> tensor<2x3xf32> + func.return %0 : tensor<2x3xf32> +} + +// ----- + +// CHECK-LABEL: @transpose_2d +func.func @transpose_2d(%arg0: tensor<2x3xf32>) -> tensor<3x2xf32> { + %permutation = "tf.Const"() {value = dense<[1, 0]> : tensor<2xi64>} : () -> (tensor<2xi64>) + // CHECK: stablehlo.transpose + %0 = "tf.Transpose"(%arg0, %permutation) : (tensor<2x3xf32>, tensor<2xi64>) -> tensor<3x2xf32> + func.return %0 : tensor<3x2xf32> +} + +// ----- + +// CHECK-LABEL: @transpose_3d_int32 +func.func @transpose_3d_int32(%arg0: tensor<1x2x3xf32>) -> tensor<3x2x1xf32> { + %permutation = "tf.Const"() {value = dense<[2, 1, 0]> : tensor<3xi32>} : () -> (tensor<3xi32>) + // CHECK: stablehlo.transpose + %0 = "tf.Transpose"(%arg0, %permutation) : (tensor<1x2x3xf32>, tensor<3xi32>) -> tensor<3x2x1xf32> + func.return %0 : tensor<3x2x1xf32> +} + +// ----- + +// CHECK-LABEL: @transpose_3d +func.func @transpose_3d(%arg0: tensor<1x2x3xf32>) -> tensor<3x2x1xf32> { + %permutation = "tf.Const"() {value = dense<[2, 1, 0]> : tensor<3xi64>} : () -> (tensor<3xi64>) + // CHECK: stablehlo.transpose + %0 = "tf.Transpose"(%arg0, %permutation) : (tensor<1x2x3xf32>, tensor<3xi64>) -> tensor<3x2x1xf32> + func.return %0 : tensor<3x2x1xf32> +} + +// ----- + +// CHECK-LABEL: @transpose_dynamic_2d +func.func @transpose_dynamic_2d(%arg0: tensor) -> tensor<4x?xf32> { + %permutation = "tf.Const"() {value = dense<[1, 0]> : tensor<2xi64>} : () -> (tensor<2xi64>) + // CHECK: stablehlo.transpose + %0 = "tf.Transpose"(%arg0, %permutation) : (tensor, tensor<2xi64>) -> tensor<4x?xf32> + func.return %0 : tensor<4x?xf32> +} + +//===----------------------------------------------------------------------===// +// Unary op legalizations. +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: @abs +func.func @abs(%arg0: tensor<2xf32>) -> tensor<2xf32> { + // CHECK: stablehlo.abs %arg0 : tensor<2xf32> + %0 = "tf.Abs"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// ----- + +// CHECK-LABEL: func @abs_dynamic +func.func @abs_dynamic(%arg0: tensor) -> tensor { + // CHECK: stablehlo.abs %arg0 : tensor + %0 = "tf.Abs"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// ----- + +// CHECK-LABEL: @acos +func.func @acos(%arg0: tensor<2xf32>) -> tensor<2xf32> { + // CHECK: %[[TEMP_0:.*]] = stablehlo.constant dense<1.000000e+00> : tensor<2xf32> + // CHECK: %[[TEMP_1:.*]] = stablehlo.subtract %[[TEMP_0]], %arg0 : tensor<2xf32> + // CHECK: %[[TEMP_2:.*]] = stablehlo.add %arg0, %[[TEMP_0]] : tensor<2xf32> + // CHECK: %[[TEMP_3:.*]] = stablehlo.multiply %[[TEMP_1]], %[[TEMP_2]] : tensor<2xf32> + // CHECK: %[[TEMP_4:.*]] = stablehlo.sqrt %[[TEMP_3]] : tensor<2xf32> + // CHECK: %[[TEMP_5:.*]] = stablehlo.atan2 %[[TEMP_4]], %arg0 : tensor<2xf32> + // CHECK: return %[[TEMP_5]] : tensor<2xf32> + %0 = "tf.Acos"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// ----- + +// CHECK-LABEL: @acos_complex +func.func @acos_complex(%arg0: tensor<2xcomplex>) -> tensor<2xcomplex> { +// CHECK-NEXT: %[[TEMP_cst:.*]] = stablehlo.constant dense<4.33680869E-19> : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_cst_0:.*]] = stablehlo.constant dense<0.693147182> : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_cst_1:.*]] = stablehlo.constant dense<2.30584283E+20> : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_cst_2:.*]] = stablehlo.constant dense<2.30584274E+12> : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_cst_3:.*]] = stablehlo.constant dense<2.30584285E+30> : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_cst_4:.*]] = stablehlo.constant dense<2.30584287E+18> : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_cst_5:.*]] = stablehlo.constant dense<1.500000e+00> : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_cst_6:.*]] = stablehlo.constant dense<0x7F800000> : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_cst_7:.*]] = stablehlo.constant dense<2.000000e+00> : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_cst_8:.*]] = stablehlo.constant dense<0.000000e+00> : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_cst_9:.*]] = stablehlo.constant dense<1.41421354> : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_cst_10:.*]] = stablehlo.constant dense<5.000000e-01> : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_cst_11:.*]] = stablehlo.constant dense<1.000000e+00> : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_0:.*]] = stablehlo.real %[[TEMP_arg0:.*]] : (tensor<2xcomplex>) -> tensor<2xf32> +// CHECK-NEXT: %[[TEMP_1:.*]] = stablehlo.abs %[[TEMP_0]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_2:.*]] = stablehlo.imag %[[TEMP_arg0:.*]] : (tensor<2xcomplex>) -> tensor<2xf32> +// CHECK-NEXT: %[[TEMP_3:.*]] = stablehlo.abs %[[TEMP_2]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_4:.*]] = stablehlo.maximum %[[TEMP_1]], %[[TEMP_3]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_5:.*]] = stablehlo.compare GE, %[[TEMP_4]], %[[TEMP_cst_5:.*]] : (tensor<2xf32>, tensor<2xf32>) -> tensor<2xi1> +// CHECK-NEXT: %[[TEMP_6:.*]] = stablehlo.compare LE, %[[TEMP_1]], %[[TEMP_cst_11:.*]] : (tensor<2xf32>, tensor<2xf32>) -> tensor<2xi1> +// CHECK-NEXT: %[[TEMP_7:.*]] = stablehlo.add %[[TEMP_1]], %[[TEMP_cst_11:.*]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_8:.*]] = stablehlo.abs %[[TEMP_7]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_9:.*]] = stablehlo.maximum %[[TEMP_8]], %[[TEMP_3]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_10:.*]] = stablehlo.minimum %[[TEMP_8]], %[[TEMP_3]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_11:.*]] = stablehlo.compare EQ, %[[TEMP_9]], %[[TEMP_10]] : (tensor<2xf32>, tensor<2xf32>) -> tensor<2xi1> +// CHECK-NEXT: %[[TEMP_12:.*]] = stablehlo.multiply %[[TEMP_9]], %[[TEMP_cst_4:.*]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_13:.*]] = stablehlo.divide %[[TEMP_10]], %[[TEMP_9]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_14:.*]] = stablehlo.multiply %[[TEMP_13]], %[[TEMP_13]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_15:.*]] = stablehlo.add %[[TEMP_14]], %[[TEMP_cst_11:.*]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_16:.*]] = stablehlo.sqrt %[[TEMP_15]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_17:.*]] = stablehlo.compare EQ, %[[TEMP_16]], %[[TEMP_cst_11:.*]] : (tensor<2xf32>, tensor<2xf32>) -> tensor<2xi1> +// CHECK-NEXT: %[[TEMP_18:.*]] = stablehlo.compare GT, %[[TEMP_14]], %[[TEMP_cst_8:.*]] : (tensor<2xf32>, tensor<2xf32>) -> tensor<2xi1> +// CHECK-NEXT: %[[TEMP_19:.*]] = stablehlo.and %[[TEMP_17]], %[[TEMP_18]] : tensor<2xi1> +// CHECK-NEXT: %[[TEMP_20:.*]] = stablehlo.multiply %[[TEMP_9]], %[[TEMP_14]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_21:.*]] = stablehlo.divide %[[TEMP_20]], %[[TEMP_cst_9:.*]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_22:.*]] = stablehlo.add %[[TEMP_9]], %[[TEMP_21]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_23:.*]] = stablehlo.multiply %[[TEMP_9]], %[[TEMP_16]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_24:.*]] = stablehlo.select %[[TEMP_19]], %[[TEMP_22]], %[[TEMP_23]] : tensor<2xi1>, tensor<2xf32> +// CHECK-NEXT: %[[TEMP_25:.*]] = stablehlo.select %[[TEMP_11]], %[[TEMP_12]], %[[TEMP_24]] : tensor<2xi1>, tensor<2xf32> +// CHECK-NEXT: %[[TEMP_26:.*]] = stablehlo.subtract %[[TEMP_1]], %[[TEMP_cst_11:.*]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_27:.*]] = stablehlo.abs %[[TEMP_26]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_28:.*]] = stablehlo.maximum %[[TEMP_27]], %[[TEMP_3]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_29:.*]] = stablehlo.minimum %[[TEMP_27]], %[[TEMP_3]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_30:.*]] = stablehlo.compare EQ, %[[TEMP_28]], %[[TEMP_29]] : (tensor<2xf32>, tensor<2xf32>) -> tensor<2xi1> +// CHECK-NEXT: %[[TEMP_31:.*]] = stablehlo.multiply %[[TEMP_28]], %[[TEMP_cst_4:.*]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_32:.*]] = stablehlo.divide %[[TEMP_29]], %[[TEMP_28]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_33:.*]] = stablehlo.multiply %[[TEMP_32]], %[[TEMP_32]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_34:.*]] = stablehlo.add %[[TEMP_33]], %[[TEMP_cst_11:.*]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_35:.*]] = stablehlo.sqrt %[[TEMP_34]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_36:.*]] = stablehlo.compare EQ, %[[TEMP_35]], %[[TEMP_cst_11:.*]] : (tensor<2xf32>, tensor<2xf32>) -> tensor<2xi1> +// CHECK-NEXT: %[[TEMP_37:.*]] = stablehlo.compare GT, %[[TEMP_33]], %[[TEMP_cst_8:.*]] : (tensor<2xf32>, tensor<2xf32>) -> tensor<2xi1> +// CHECK-NEXT: %[[TEMP_38:.*]] = stablehlo.and %[[TEMP_36]], %[[TEMP_37]] : tensor<2xi1> +// CHECK-NEXT: %[[TEMP_39:.*]] = stablehlo.multiply %[[TEMP_28]], %[[TEMP_33]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_40:.*]] = stablehlo.divide %[[TEMP_39]], %[[TEMP_cst_9:.*]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_41:.*]] = stablehlo.add %[[TEMP_28]], %[[TEMP_40]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_42:.*]] = stablehlo.multiply %[[TEMP_28]], %[[TEMP_35]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_43:.*]] = stablehlo.select %[[TEMP_38]], %[[TEMP_41]], %[[TEMP_42]] : tensor<2xi1>, tensor<2xf32> +// CHECK-NEXT: %[[TEMP_44:.*]] = stablehlo.select %[[TEMP_30]], %[[TEMP_31]], %[[TEMP_43]] : tensor<2xi1>, tensor<2xf32> +// CHECK-NEXT: %[[TEMP_45:.*]] = stablehlo.add %[[TEMP_25]], %[[TEMP_44]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_46:.*]] = stablehlo.multiply %[[TEMP_45]], %[[TEMP_cst_10:.*]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_47:.*]] = stablehlo.add %[[TEMP_46]], %[[TEMP_1]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_48:.*]] = stablehlo.multiply %[[TEMP_47]], %[[TEMP_cst_10:.*]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_49:.*]] = stablehlo.multiply %[[TEMP_3]], %[[TEMP_3]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_50:.*]] = stablehlo.add %[[TEMP_25]], %[[TEMP_7]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_51:.*]] = stablehlo.divide %[[TEMP_49]], %[[TEMP_50]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_52:.*]] = stablehlo.subtract %[[TEMP_44]], %[[TEMP_26]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_53:.*]] = stablehlo.add %[[TEMP_51]], %[[TEMP_52]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_54:.*]] = stablehlo.multiply %[[TEMP_48]], %[[TEMP_53]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_55:.*]] = stablehlo.sqrt %[[TEMP_54]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_56:.*]] = stablehlo.divide %[[TEMP_48]], %[[TEMP_50]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_57:.*]] = stablehlo.add %[[TEMP_44]], %[[TEMP_26]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_58:.*]] = stablehlo.divide %[[TEMP_48]], %[[TEMP_57]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_59:.*]] = stablehlo.add %[[TEMP_56]], %[[TEMP_58]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_60:.*]] = stablehlo.sqrt %[[TEMP_59]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_61:.*]] = stablehlo.multiply %[[TEMP_3]], %[[TEMP_60]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_62:.*]] = stablehlo.select %[[TEMP_6]], %[[TEMP_55]], %[[TEMP_61]] : tensor<2xi1>, tensor<2xf32> +// CHECK-NEXT: %[[TEMP_63:.*]] = stablehlo.select %[[TEMP_5]], %[[TEMP_3]], %[[TEMP_62]] : tensor<2xi1>, tensor<2xf32> +// CHECK-NEXT: %[[TEMP_64:.*]] = stablehlo.compare LT, %[[TEMP_1]], %[[TEMP_cst_3:.*]] : (tensor<2xf32>, tensor<2xf32>) -> tensor<2xi1> +// CHECK-NEXT: %[[TEMP_65:.*]] = stablehlo.select %[[TEMP_64]], %[[TEMP_cst_2:.*]], %[[TEMP_cst_1:.*]] : tensor<2xi1>, tensor<2xf32> +// CHECK-NEXT: %[[TEMP_66:.*]] = stablehlo.compare GE, %[[TEMP_3]], %[[TEMP_65]] : (tensor<2xf32>, tensor<2xf32>) -> tensor<2xi1> +// CHECK-NEXT: %[[TEMP_67:.*]] = stablehlo.select %[[TEMP_66]], %[[TEMP_3]], %[[TEMP_1]] : tensor<2xi1>, tensor<2xf32> +// CHECK-NEXT: %[[TEMP_68:.*]] = stablehlo.select %[[TEMP_66]], %[[TEMP_65]], %[[TEMP_cst_5:.*]] : tensor<2xi1>, tensor<2xf32> +// CHECK-NEXT: %[[TEMP_69:.*]] = stablehlo.compare GE, %[[TEMP_67]], %[[TEMP_68]] : (tensor<2xf32>, tensor<2xf32>) -> tensor<2xi1> +// CHECK-NEXT: %[[TEMP_70:.*]] = stablehlo.log %[[TEMP_67]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_71:.*]] = stablehlo.add %[[TEMP_70]], %[[TEMP_cst_0:.*]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_72:.*]] = stablehlo.compare EQ, %[[TEMP_3]], %[[TEMP_cst_7:.*]] : (tensor<2xf32>, tensor<2xf32>) -> tensor<2xi1> +// CHECK-NEXT: %[[TEMP_73:.*]] = stablehlo.not %[[TEMP_72]] : tensor<2xi1> +// CHECK-NEXT: %[[TEMP_74:.*]] = stablehlo.and %[[TEMP_66]], %[[TEMP_73]] : tensor<2xi1> +// CHECK-NEXT: %[[TEMP_75:.*]] = stablehlo.divide %[[TEMP_1]], %[[TEMP_3]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_76:.*]] = stablehlo.select %[[TEMP_74]], %[[TEMP_75]], %[[TEMP_cst_8:.*]] : tensor<2xi1>, tensor<2xf32> +// CHECK-NEXT: %[[TEMP_77:.*]] = stablehlo.multiply %[[TEMP_76]], %[[TEMP_76]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_78:.*]] = stablehlo.log_plus_one %[[TEMP_77]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_79:.*]] = stablehlo.multiply %[[TEMP_78]], %[[TEMP_cst_10:.*]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_80:.*]] = stablehlo.add %[[TEMP_71]], %[[TEMP_79]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_81:.*]] = stablehlo.compare LT, %[[TEMP_3]], %cst : (tensor<2xf32>, tensor<2xf32>) -> tensor<2xi1> +// CHECK-NEXT: %[[TEMP_82:.*]] = stablehlo.compare LT, %[[TEMP_1]], %[[TEMP_cst_11:.*]] : (tensor<2xf32>, tensor<2xf32>) -> tensor<2xi1> +// CHECK-NEXT: %[[TEMP_83:.*]] = stablehlo.and %[[TEMP_81]], %[[TEMP_82]] : tensor<2xi1> +// CHECK-NEXT: %[[TEMP_84:.*]] = stablehlo.multiply %[[TEMP_7]], %[[TEMP_26]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_85:.*]] = stablehlo.add %[[TEMP_46]], %[[TEMP_cst_11:.*]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_86:.*]] = stablehlo.divide %[[TEMP_84]], %[[TEMP_85]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_87:.*]] = stablehlo.negate %[[TEMP_86]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_88:.*]] = stablehlo.compare GE, %[[TEMP_1]], %[[TEMP_cst_11:.*]] : (tensor<2xf32>, tensor<2xf32>) -> tensor<2xi1> +// CHECK-NEXT: %[[TEMP_89:.*]] = stablehlo.multiply %[[TEMP_49]], %[[TEMP_cst_10:.*]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_90:.*]] = stablehlo.divide %[[TEMP_89]], %[[TEMP_50]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_91:.*]] = stablehlo.multiply %[[TEMP_57]], %[[TEMP_cst_10:.*]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_92:.*]] = stablehlo.add %[[TEMP_90]], %[[TEMP_91]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_93:.*]] = stablehlo.compare LE, %[[TEMP_46]], %[[TEMP_cst_6:.*]] : (tensor<2xf32>, tensor<2xf32>) -> tensor<2xi1> +// CHECK-NEXT: %[[TEMP_94:.*]] = stablehlo.divide %[[TEMP_89]], %[[TEMP_52]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_95:.*]] = stablehlo.add %[[TEMP_90]], %[[TEMP_94]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_96:.*]] = stablehlo.subtract %[[TEMP_46]], %[[TEMP_cst_11:.*]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_97:.*]] = stablehlo.select %[[TEMP_93]], %[[TEMP_95]], %[[TEMP_96]] : tensor<2xi1>, tensor<2xf32> +// CHECK-NEXT: %[[TEMP_98:.*]] = stablehlo.select %[[TEMP_88]], %[[TEMP_92]], %[[TEMP_97]] : tensor<2xi1>, tensor<2xf32> +// CHECK-NEXT: %[[TEMP_99:.*]] = stablehlo.select %[[TEMP_83]], %[[TEMP_87]], %[[TEMP_98]] : tensor<2xi1>, tensor<2xf32> +// CHECK-NEXT: %[[TEMP_100:.*]] = stablehlo.multiply %[[TEMP_99]], %[[TEMP_85]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_101:.*]] = stablehlo.sqrt %[[TEMP_100]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_102:.*]] = stablehlo.divide %[[TEMP_3]], %[[TEMP_101]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_103:.*]] = stablehlo.add %[[TEMP_99]], %[[TEMP_101]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_104:.*]] = stablehlo.log_plus_one %[[TEMP_103]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_105:.*]] = stablehlo.select %[[TEMP_83]], %[[TEMP_102]], %[[TEMP_104]] : tensor<2xi1>, tensor<2xf32> +// CHECK-NEXT: %[[TEMP_106:.*]] = stablehlo.select %[[TEMP_69]], %[[TEMP_80]], %[[TEMP_105]] : tensor<2xi1>, tensor<2xf32> +// CHECK-NEXT: %[[TEMP_107:.*]] = stablehlo.real %[[TEMP_arg0:.*]] : (tensor<2xcomplex>) -> tensor<2xf32> +// CHECK-NEXT: %[[TEMP_108:.*]] = stablehlo.atan2 %[[TEMP_63]], %[[TEMP_107]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_109:.*]] = stablehlo.imag %[[TEMP_arg0:.*]] : (tensor<2xcomplex>) -> tensor<2xf32> +// CHECK-NEXT: %[[TEMP_110:.*]] = stablehlo.compare LT, %[[TEMP_109]], %[[TEMP_cst_8:.*]] : (tensor<2xf32>, tensor<2xf32>) -> tensor<2xi1> +// CHECK-NEXT: %[[TEMP_111:.*]] = stablehlo.negate %[[TEMP_106]] : tensor<2xf32> +// CHECK-NEXT: %[[TEMP_112:.*]] = stablehlo.select %[[TEMP_110]], %[[TEMP_106]], %[[TEMP_111]] : tensor<2xi1>, tensor<2xf32> +// CHECK-NEXT: %[[TEMP_113:.*]] = stablehlo.complex %[[TEMP_108]], %[[TEMP_112]] : tensor<2xcomplex> +// CHECK-NEXT: return %[[TEMP_113:.*]] : tensor<2xcomplex> + + %0 = "tf.Acos"(%arg0) : (tensor<2xcomplex>) -> tensor<2xcomplex> + func.return %0 : tensor<2xcomplex> +} + +// ----- + +// CHECK-LABEL: @acos_dynamic +func.func @acos_dynamic(%arg0: tensor<*xf32>) -> tensor<*xf32> { + // CHECK: "tf.Acos" + %0 = "tf.Acos"(%arg0) : (tensor<*xf32>) -> tensor<*xf32> + func.return %0 : tensor<*xf32> +} + +// ----- + +// CHECK-LABEL: func @cast_dynamic_i2f +func.func @cast_dynamic_i2f(%arg0: tensor) -> tensor { + // CHECK: stablehlo.convert %arg0 : (tensor) -> tensor + %0 = "tf.Cast"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// ----- + +// CHECK-LABEL: func @cast_i2f +func.func @cast_i2f(%arg0: tensor<2xi32>) -> tensor<2xf32> { + // CHECK: stablehlo.convert %arg0 : (tensor<2xi32>) -> tensor<2xf32> + %0 = "tf.Cast"(%arg0) : (tensor<2xi32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// ----- + +// CHECK-LABEL: func @cast_c2f +func.func @cast_c2f(%arg0: tensor<2xcomplex>) -> tensor<2xf32> { + // CHECK: stablehlo.convert %arg0 : (tensor<2xcomplex>) -> tensor<2xf32> + %0 = "tf.Cast"(%arg0) : (tensor<2xcomplex>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// ----- + +// CHECK-LABEL: @ceil +func.func @ceil(%arg0: tensor<2xf32>) -> tensor<2xf32> { + // CHECK: stablehlo.ceil %arg0 : tensor<2xf32> + %0 = "tf.Ceil"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// ----- + +// CHECK-LABEL: func @ceil_dynamic +func.func @ceil_dynamic(%arg0: tensor) -> tensor { + // CHECK: stablehlo.ceil %arg0 : tensor + %0 = "tf.Ceil"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// ----- + +// CHECK-LABEL: @complex_abs +func.func @complex_abs(%arg0: tensor<2xcomplex>) -> tensor<2xf32> { + // CHECK: stablehlo.abs %arg0 : (tensor<2xcomplex>) -> tensor<2xf32> + %0 = "tf.ComplexAbs"(%arg0) : (tensor<2xcomplex>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// ----- + +// CHECK-LABEL: @cos +func.func @cos(%arg0: tensor<2xf32>) -> tensor<2xf32> { + // CHECK: stablehlo.cosine %arg0 : tensor<2xf32> + %0 = "tf.Cos"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// ----- + +// CHECK-LABEL: @tan +func.func @tan(%arg0: tensor<2xf32>) -> tensor<2xf32> { + // CHECK: stablehlo.tan %arg0 : tensor<2xf32> + %0 = "tf.Tan"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// ----- + +// CHECK-LABEL: func @cos_dynamic +func.func @cos_dynamic(%arg0: tensor) -> tensor { + // CHECK: stablehlo.cosine %arg0 : tensor + %0 = "tf.Cos"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// ----- + +// CHECK-LABEL: @exp +func.func @exp(%arg0: tensor<2xf32>) -> tensor<2xf32> { + // CHECK: stablehlo.exponential %arg0 : tensor<2xf32> + %0 = "tf.Exp"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// ----- + +// CHECK-LABEL: @expm1 +func.func @expm1(%arg0: tensor<2xf32>) -> tensor<2xf32> { + // CHECK: stablehlo.exponential_minus_one %arg0 : tensor<2xf32> + %0 = "tf.Expm1"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// ----- + +// CHECK-LABEL: func @exp_dynamic +func.func @exp_dynamic(%arg0: tensor) -> tensor { + // CHECK: stablehlo.exponential %arg0 : tensor + %0 = "tf.Exp"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// ----- + +// CHECK-LABEL: @floor +func.func @floor(%arg0: tensor<2xf32>) -> tensor<2xf32> { + // CHECK: stablehlo.floor %arg0 : tensor<2xf32> + %0 = "tf.Floor"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// ----- + +// CHECK-LABEL: func @floor_dynamic +func.func @floor_dynamic(%arg0: tensor) -> tensor { + // CHECK: stablehlo.floor %arg0 : tensor + %0 = "tf.Floor"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// ----- + +// CHECK-LABEL: @is_finite +func.func @is_finite(%arg0: tensor<2xf32>) -> tensor<2xi1> { + // CHECK: stablehlo.is_finite %arg0 : (tensor<2xf32>) -> tensor<2xi1> + %0 = "tf.IsFinite"(%arg0) : (tensor<2xf32>) -> tensor<2xi1> + func.return %0 : tensor<2xi1> +} + +// ----- + +// CHECK-LABEL: func @is_finite_dynamic +func.func @is_finite_dynamic(%arg0: tensor) -> tensor { + // CHECK: stablehlo.is_finite %arg0 : (tensor) -> tensor + %0 = "tf.IsFinite"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// ----- + +// CHECK-LABEL: @log +func.func @log(%arg0: tensor<2xf32>) -> tensor<2xf32> { + // CHECK: stablehlo.log %arg0 : tensor<2xf32> + %0 = "tf.Log"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// ----- + +// CHECK-LABEL: func @log_dynamic +func.func @log_dynamic(%arg0: tensor) -> tensor { + // CHECK: stablehlo.log %arg0 : tensor + %0 = "tf.Log"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// ----- + +// CHECK-LABEL: @log1p +func.func @log1p(%arg0: tensor<2xf32>) -> tensor<2xf32> { + // CHECK: stablehlo.log_plus_one %arg0 : tensor<2xf32> + %0 = "tf.Log1p"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// ----- + +// CHECK-LABEL: func @log1p_dynamic +func.func @log1p_dynamic(%arg0: tensor) -> tensor { + // CHECK: stablehlo.log_plus_one %arg0 : tensor + %0 = "tf.Log1p"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// ----- + +// CHECK-LABEL: @neg +func.func @neg(%arg0: tensor<2xf32>) -> tensor<2xf32> { + // CHECK: stablehlo.negate %arg0 : tensor<2xf32> + %0 = "tf.Neg"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// ----- + +// CHECK-LABEL: func @neg_dynamic +func.func @neg_dynamic(%arg0: tensor) -> tensor { + // CHECK: stablehlo.negate %arg0 : tensor + %0 = "tf.Neg"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// ----- + +// CHECK-LABEL: @sigmoid +func.func @sigmoid(%arg0: tensor<2xf32>) -> tensor<2xf32> { + // CHECK: stablehlo.logistic + %0 = "tf.Sigmoid"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// ----- + +// CHECK-LABEL: @sigmoid_complex +func.func @sigmoid_complex(%arg0: tensor<2xcomplex>) -> tensor<2xcomplex> { + // CHECK: stablehlo.logistic + %0 = "tf.Sigmoid"(%arg0) : (tensor<2xcomplex>) -> tensor<2xcomplex> + func.return %0 : tensor<2xcomplex> +} + +// ----- + +// CHECK-LABEL: @xla_scatter +func.func @xla_scatter(%arg0: tensor<2x10xi1>, %arg1: tensor<1xi32>, %arg2: tensor<2x3xi1>) -> tensor<2x10xi1> { + // CHECK: %[[RESULT:.*]] = "stablehlo.scatter"(%arg0, %arg1, %arg2) + // CHECK-SAME: <{indices_are_sorted = true, scatter_dimension_numbers = #stablehlo.scatter, unique_indices = false}> + // CHECK-NEXT: ^bb0(%[[VALUE_A:.*]]: tensor, %[[VALUE_B:.*]]: tensor): + // CHECK-NEXT: %[[IDENTITY:.*]] = func.call @scatter_update(%[[VALUE_A]], %[[VALUE_B]]) : (tensor, tensor) -> tensor + // CHECK-NEXT: stablehlo.return %[[IDENTITY]] : tensor + // CHECK-NEXT: }) : (tensor<2x10xi1>, tensor<1xi32>, tensor<2x3xi1>) -> tensor<2x10xi1> + // CHECK-NEXT: return %[[RESULT]] : tensor<2x10xi1> + %0 = "tf.XlaScatter"(%arg0, %arg1, %arg2) <{dimension_numbers = "\0A\02\00\01\1A\01\01", indices_are_sorted = true, update_computation = @scatter_update}> : (tensor<2x10xi1>, tensor<1xi32>, tensor<2x3xi1>) -> tensor<2x10xi1> + return %0 : tensor<2x10xi1> +} + +func.func private @scatter_update(%arg0: tensor, %arg1: tensor) -> tensor { + return %arg1 : tensor +} \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-add.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-add.mlir new file mode 100644 index 0000000000000000000000000000000000000000..5b7b5cd3b392839aa93b571bb69ef9295142ec44 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-add.mlir @@ -0,0 +1,15 @@ +// RUN: odml-to-stablehlo-opt %s -tfl-parse-stablehlo-ops | FileCheck %s + +module { +func.func @main(%arg0: tensor<2xi32>) -> tensor<2xi32> attributes {tf.entry_function = {inputs = "arg0", outputs = "tfl.custom1"}} { + %0 = "tfl.custom"(%arg0, %arg0) {custom_code = "stablehlo.add", custom_option = #tfl} : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi32> + func.return %0 : tensor<2xi32> +} +} + +// CHECK: module { +// CHECK-NEXT: func @main(%arg0: tensor<2xi32>) -> tensor<2xi32> attributes {tf.entry_function = {inputs = "arg0", outputs = "tfl.custom1"}} { +// CHECK-NEXT: %0 = stablehlo.add %arg0, %arg0 : tensor<2xi32> +// CHECK-NEXT: return %0 : tensor<2xi32> +// CHECK-NEXT: } +// CHECK-NEXT: } diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-broadcast.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-broadcast.mlir new file mode 100644 index 0000000000000000000000000000000000000000..656f9c511f855855fbc29c88b24933aa42c08312 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-broadcast.mlir @@ -0,0 +1,15 @@ +// RUN: odml-to-stablehlo-opt %s -tfl-parse-stablehlo-ops | FileCheck %s + +module { + func.func @main(%arg0: tensor<1x2xi32>) -> tensor<1x2x2xi32> { + %0 = "tfl.custom"(%arg0) {custom_code = "stablehlo.broadcast_in_dim", custom_option = #tfl} : (tensor<1x2xi32>) -> tensor<1x2x2xi32> + func.return %0 : tensor<1x2x2xi32> + } +} + +// CHECK: module { +// CHECK-NEXT: func @main(%arg0: tensor<1x2xi32>) -> tensor<1x2x2xi32> { +// CHECK-NEXT: %0 = stablehlo.broadcast_in_dim %arg0, dims = [1, 2] : (tensor<1x2xi32>) -> tensor<1x2x2xi32> +// CHECK-NEXT: return %0 : tensor<1x2x2xi32> +// CHECK-NEXT: } +// CHECK-NEXT: } diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-clamp.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-clamp.mlir new file mode 100644 index 0000000000000000000000000000000000000000..26ffcfe20401a88b7c04ac723648897134eb9ad8 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-clamp.mlir @@ -0,0 +1,15 @@ +// RUN: odml-to-stablehlo-opt %s -tfl-parse-stablehlo-ops | FileCheck %s + +module { +func.func @main(%arg0: tensor<2xi32>, %arg1: tensor<2xi32>, %arg2: tensor<2xi32>) -> tensor<2xi32> { + %0 = "tfl.custom"(%arg0, %arg1, %arg2) {custom_code = "stablehlo.clamp", custom_option = #tfl} : (tensor<2xi32>, tensor<2xi32>, tensor<2xi32>) -> tensor<2xi32> + func.return %0 : tensor<2xi32> +} +} + +// CHECK: module { +// CHECK-NEXT: func @main(%arg0: tensor<2xi32>, %arg1: tensor<2xi32>, %arg2: tensor<2xi32>) -> tensor<2xi32> { +// CHECK-NEXT: %0 = stablehlo.clamp %arg0, %arg1, %arg2 : tensor<2xi32> +// CHECK-NEXT: return %0 : tensor<2xi32> +// CHECK-NEXT: } +// CHECK-NEXT: } diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-concat.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-concat.mlir new file mode 100644 index 0000000000000000000000000000000000000000..cc93f212759834d56c96bca5d840eaec4c213ed8 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-concat.mlir @@ -0,0 +1,15 @@ +// RUN: odml-to-stablehlo-opt %s -tfl-parse-stablehlo-ops | FileCheck %s + +module { +func.func @main(%arg0: tensor<3x3xf32>, %arg1: tensor<3x3xf32>) -> tensor<6x3xf32> { + %0 = "tfl.custom"(%arg0, %arg1) {custom_code = "stablehlo.concatenate", custom_option = #tfl} : (tensor<3x3xf32>, tensor<3x3xf32>) -> tensor<6x3xf32> + func.return %0 : tensor<6x3xf32> +} +} + +// CHECK: module { +// CHECK-NEXT: func @main(%arg0: tensor<3x3xf32>, %arg1: tensor<3x3xf32>) -> tensor<6x3xf32> { +// CHECK-NEXT: %0 = stablehlo.concatenate %arg0, %arg1, dim = 0 : (tensor<3x3xf32>, tensor<3x3xf32>) -> tensor<6x3xf32> +// CHECK-NEXT: return %0 : tensor<6x3xf32> +// CHECK-NEXT: } +// CHECK-NEXT: } diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-constant.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-constant.mlir new file mode 100644 index 0000000000000000000000000000000000000000..837f35d9d7e3eb3a34db16f68a0e0922ec0ef979 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-constant.mlir @@ -0,0 +1,15 @@ +// RUN: odml-to-stablehlo-opt %s -tfl-parse-stablehlo-ops | FileCheck -dump-input always %s + +module { + func.func @main() -> tensor<1xi64> { + %0 = "tfl.custom"() {custom_code = "stablehlo.constant", custom_option = #tfl} : () -> tensor<1xi64> + func.return %0 : tensor<1xi64> + } +} + +// CHECK: module { +// CHECK-NEXT: func @main() -> tensor<1xi64> { +// CHECK-NEXT: %[[c0:.+]] = stablehlo.constant dense<2> : tensor<1xi64> +// CHECK-NEXT: return %[[c0]] : tensor<1xi64> +// CHECK-NEXT: } +// CHECK-NEXT: } diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-conv.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-conv.mlir new file mode 100644 index 0000000000000000000000000000000000000000..954181c1546c0c71f6c499cb1d39bb03ef9e1447 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-conv.mlir @@ -0,0 +1,15 @@ +// RUN: odml-to-stablehlo-opt %s -tfl-parse-stablehlo-ops | FileCheck -dump-input always %s + +module { + func.func @main(%arg0: tensor<8x8x1x207xf32>, %arg1: tensor<3x3x16x207xf32>) -> tensor<16x8x8x1xf32> { + %0 = "tfl.custom"(%arg0, %arg1) {custom_code = "stablehlo.convolution", custom_option = #tfl} : (tensor<8x8x1x207xf32>, tensor<3x3x16x207xf32>) -> tensor<16x8x8x1xf32> + func.return %0 : tensor<16x8x8x1xf32> + } +} + +// CHECK: module { +// CHECK-NEXT: func.func @main(%arg0: tensor<8x8x1x207xf32>, %arg1: tensor<3x3x16x207xf32>) -> tensor<16x8x8x1xf32> { +// CHECK-NEXT: %0 = stablehlo.convolution(%arg0, %arg1) dim_numbers = [0, 1, b, f]x[0, 1, o, i]->[f, 0, 1, b], window = {stride = [1, 1], pad = {{\[}}[1, 1], [1, 1]], lhs_dilate = [1, 1], rhs_dilate = [1, 1], reverse = [true, false]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64, precision_config = [#stablehlo, #stablehlo]} : (tensor<8x8x1x207xf32>, tensor<3x3x16x207xf32>) -> tensor<16x8x8x1xf32> +// CHECK-NEXT: return %0 : tensor<16x8x8x1xf32> +// CHECK-NEXT: } +// CHECK-NEXT: } diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-max.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-max.mlir new file mode 100644 index 0000000000000000000000000000000000000000..736d17c54aef42ea4e581dfbcef6d1251bdd2a2d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-max.mlir @@ -0,0 +1,15 @@ +// RUN: odml-to-stablehlo-opt %s -tfl-parse-stablehlo-ops | FileCheck %s + +module { +func.func @main(%arg0: tensor<2xi32>) -> tensor<2xi32> attributes {tf.entry_function = {inputs = "arg0", outputs = "tfl.custom1"}} { + %0 = "tfl.custom"(%arg0, %arg0) {custom_code = "stablehlo.maximum", custom_option = #tfl} : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi32> + func.return %0 : tensor<2xi32> +} +} + +// CHECK: module { +// CHECK-NEXT: func @main(%arg0: tensor<2xi32>) -> tensor<2xi32> attributes {tf.entry_function = {inputs = "arg0", outputs = "tfl.custom1"}} { +// CHECK-NEXT: %0 = stablehlo.maximum %arg0, %arg0 : tensor<2xi32> +// CHECK-NEXT: return %0 : tensor<2xi32> +// CHECK-NEXT: } +// CHECK-NEXT: } diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-mul.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-mul.mlir new file mode 100644 index 0000000000000000000000000000000000000000..23e2b4609e448771bbaf77f10b5e1cab78ce1866 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-mul.mlir @@ -0,0 +1,15 @@ +// RUN: odml-to-stablehlo-opt %s -tfl-parse-stablehlo-ops | FileCheck %s + +module { +func.func @main(%arg0: tensor<2xi32>) -> tensor<2xi32> attributes {tf.entry_function = {inputs = "arg0", outputs = "tfl.custom1"}} { + %0 = "tfl.custom"(%arg0, %arg0) {custom_code = "stablehlo.multiply", custom_option = #tfl} : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi32> + func.return %0 : tensor<2xi32> +} +} + +// CHECK: module { +// CHECK-NEXT: func @main(%arg0: tensor<2xi32>) -> tensor<2xi32> attributes {tf.entry_function = {inputs = "arg0", outputs = "tfl.custom1"}} { +// CHECK-NEXT: %0 = stablehlo.multiply %arg0, %arg0 : tensor<2xi32> +// CHECK-NEXT: return %0 : tensor<2xi32> +// CHECK-NEXT: } +// CHECK-NEXT: } diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-pad.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-pad.mlir new file mode 100644 index 0000000000000000000000000000000000000000..b9e308ffddca2bd96c16606fb06c7d83c912c9e9 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-pad.mlir @@ -0,0 +1,16 @@ +// RUN: odml-to-stablehlo-opt %s -tfl-parse-stablehlo-ops | FileCheck -dump-input always %s + +module { + func.func @main(%arg0: tensor<8x128xf32>, %arg1: tensor) -> tensor<11x131xf32> { + %0 = "tfl.custom"(%arg0, %arg1) {custom_code = "stablehlo.pad", custom_option = #tfl} : (tensor<8x128xf32>, tensor) -> tensor<11x131xf32> + func.return %0 : tensor<11x131xf32> + } + } + +// CHECK: module { +// CHECK-NEXT: func @main(%arg0: tensor<8x128xf32>, %arg1: tensor) -> tensor<11x131xf32> { +// CHECK-NEXT: %0 = stablehlo.pad %arg0, %arg1, low = [1, 0], high = [2, 3], interior = [0, 0] : (tensor<8x128xf32>, tensor) -> tensor<11x131xf32> +// CHECK-NEXT: return %0 : tensor<11x131xf32> +// CHECK-NEXT: } +// CHECK-NEXT: } + diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-reshape.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-reshape.mlir new file mode 100644 index 0000000000000000000000000000000000000000..bb13d72e04b29c1b32fcad5b273e0d53591d9823 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-reshape.mlir @@ -0,0 +1,15 @@ +// RUN: odml-to-stablehlo-opt %s -tfl-parse-stablehlo-ops | FileCheck -dump-input always %s + +module { + func.func @main(%arg0: tensor<2xi32>) -> tensor<2xi32> { + %0 = "tfl.custom"(%arg0) {custom_code = "stablehlo.reshape", custom_option = #tfl} : (tensor<2xi32>) -> tensor<2xi32> + func.return %0 : tensor<2xi32> + } +} + +// CHECK: module { +// CHECK-NEXT: func @main(%arg0: tensor<2xi32>) -> tensor<2xi32> { +// CHECK-NEXT: %0 = stablehlo.reshape %arg0 : (tensor<2xi32>) -> tensor<2xi32> +// CHECK-NEXT: return %0 : tensor<2xi32> +// CHECK-NEXT: } +// CHECK-NEXT: } diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-rsqrt.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-rsqrt.mlir new file mode 100644 index 0000000000000000000000000000000000000000..a894603b8f40f302fab4bf242652759f5c98d7f8 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-rsqrt.mlir @@ -0,0 +1,15 @@ +// RUN: odml-to-stablehlo-opt %s -tfl-parse-stablehlo-ops | FileCheck -dump-input always %s + +module { +func.func @main(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "tfl.custom"(%arg0) {custom_code = "stablehlo.rsqrt", custom_option = #tfl} : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} +} + +// CHECK: module +// CHECK-NEXT: func @main(%arg0: tensor<2xf32>) -> tensor<2xf32> { +// CHECK-NEXT: %0 = stablehlo.rsqrt %arg0 : tensor<2xf32> +// CHECK-NEXT: return %0 : tensor<2xf32> +// CHECK-NEXT: } +// CHECK-NEXT: } diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-sub.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-sub.mlir new file mode 100644 index 0000000000000000000000000000000000000000..28a1851ae386eda704fed5c52eedbcdc419eb067 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo-sub.mlir @@ -0,0 +1,15 @@ +// RUN: odml-to-stablehlo-opt %s -tfl-parse-stablehlo-ops | FileCheck %s + +module { +func.func @main(%arg0: tensor<2xi32>) -> tensor<2xi32> attributes {tf.entry_function = {inputs = "arg0", outputs = "tfl.custom1"}} { + %0 = "tfl.custom"(%arg0, %arg0) {custom_code = "stablehlo.subtract", custom_option = #tfl} : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi32> + func.return %0 : tensor<2xi32> +} +} + +// CHECK: module { +// CHECK-NEXT: func @main(%arg0: tensor<2xi32>) -> tensor<2xi32> attributes {tf.entry_function = {inputs = "arg0", outputs = "tfl.custom1"}} { +// CHECK-NEXT: %0 = stablehlo.subtract %arg0, %arg0 : tensor<2xi32> +// CHECK-NEXT: return %0 : tensor<2xi32> +// CHECK-NEXT: } +// CHECK-NEXT: } diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo.mlir new file mode 100644 index 0000000000000000000000000000000000000000..22fc4076c019160844a92f01c82d48296f207e37 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize-tfl-stablehlo.mlir @@ -0,0 +1,17 @@ +// RUN: odml-to-stablehlo-opt %s -tfl-parse-stablehlo-ops | FileCheck %s + +module { +func.func @main(%arg0: tensor<2xi32>) -> tensor<2xi32> attributes {tf.entry_function = {inputs = "arg0", outputs = "tfl.custom1"}} { + %0 = "tfl.custom"(%arg0, %arg0) {custom_code = "stablehlo.add", custom_option = #tfl} : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi32> + %1 = "tfl.custom"(%0, %arg0) {custom_code = "stablehlo.subtract", custom_option = #tfl} : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi32> + func.return %1 : tensor<2xi32> +} +} + +// CHECK: module { +// CHECK-NEXT: func @main(%arg0: tensor<2xi32>) -> tensor<2xi32> attributes {tf.entry_function = {inputs = "arg0", outputs = "tfl.custom1"}} { +// CHECK-NEXT: %0 = stablehlo.add %arg0, %arg0 : tensor<2xi32> +// CHECK-NEXT: %1 = stablehlo.subtract %0, %arg0 : tensor<2xi32> +// CHECK-NEXT: return %1 : tensor<2xi32> +// CHECK-NEXT: } +// CHECK-NEXT: } diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize_hlo.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize_hlo.mlir new file mode 100644 index 0000000000000000000000000000000000000000..bfd4b422e019dad96e965bf69545fc4f240b6d6c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/legalize_hlo.mlir @@ -0,0 +1,5245 @@ +// NOTE: Assertions have been autogenerated by utils/generate-test-checks.py +// RUN: odml-to-stablehlo-opt -tf-legalize-hlo -verify-diagnostics %s | FileCheck %s + +// CHECK-LABEL: func @biasAdd_NHWC( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x32x10x32xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<32xi32>) -> tensor<1x32x10x32xi32> { +// CHECK: %[[VAL_2:.*]] = "tf.AddV2"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1x32x10x32xi32>, tensor<32xi32>) -> tensor<1x32x10x32xi32> +// CHECK: return %[[VAL_2]] : tensor<1x32x10x32xi32> +// CHECK: } +func.func @biasAdd_NHWC(%arg0: tensor<1x32x10x32xi32>, %arg1: tensor<32xi32>) -> tensor<1x32x10x32xi32> { + %0 = "chlo.broadcast_add"(%arg0, %arg1) {broadcast_dimensions = array} : (tensor<1x32x10x32xi32>, tensor<32xi32>) -> tensor<1x32x10x32xi32> + func.return %0 : tensor<1x32x10x32xi32> +} + +// CHECK-LABEL: func @biasAdd_NCHW( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x32x10x32xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<32xi32>) -> tensor<1x32x10x32xi32> { +// CHECK: %[[VAL_2:.*]] = "tf.AddV2"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1x32x10x32xi32>, tensor<32xi32>) -> tensor<1x32x10x32xi32> +// CHECK: return %[[VAL_2]] : tensor<1x32x10x32xi32> +// CHECK: } +func.func @biasAdd_NCHW(%arg0: tensor<1x32x10x32xi32>, %arg1: tensor<32xi32>) -> tensor<1x32x10x32xi32> { + %0 = "chlo.broadcast_add"(%arg0, %arg1) {broadcast_dimensions = array} : (tensor<1x32x10x32xi32>, tensor<32xi32>) -> tensor<1x32x10x32xi32> + func.return %0 : tensor<1x32x10x32xi32> +} + +// CHECK-LABEL: func @biasAdd_dynamic( +// CHECK-SAME: %[[VAL_0:.*]]: tensor, +// CHECK-SAME: %[[VAL_1:.*]]: tensor) -> tensor { +// CHECK: %[[VAL_2:.*]] = "tf.AddV2"(%[[VAL_0]], %[[VAL_1]]) : (tensor, tensor) -> tensor +// CHECK: return %[[VAL_2]] : tensor +// CHECK: } +func.func @biasAdd_dynamic(%arg0: tensor, %arg1: tensor) -> tensor { + %0 = "chlo.broadcast_add"(%arg0, %arg1) {broadcast_dimensions = array} : (tensor, tensor) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @add( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xi32>) -> tensor<2xi32> { +// CHECK: %[[VAL_1:.*]] = "tf.AddV2"(%[[VAL_0]], %[[VAL_0]]) : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi32> +// CHECK: %[[VAL_2:.*]] = "tf.AddV2"(%[[VAL_1]], %[[VAL_0]]) : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi32> +// CHECK: return %[[VAL_2]] : tensor<2xi32> +// CHECK: } +func.func @add(%arg0: tensor<2xi32>) -> tensor<2xi32> { + %0 = mhlo.add %arg0, %arg0 : tensor<2xi32> + %1 = mhlo.add %0, %arg0 : tensor<2xi32> + func.return %1 : tensor<2xi32> +} + +// CHECK-LABEL: func @broadcast_add( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x1xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x1000xf32>) -> (tensor<1x1000xf32>, tensor<1x1000xf32>) { +// CHECK-DAG. %cst = arith.constant dense<[1, 1000]> : tensor<2xi64> +// CHECK: %[[VAL_2:.*]] = "tf.AddV2"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1x1xf32>, tensor<1x1000xf32>) -> tensor<1x1000xf32> +// CHECK: %[[VAL_3:.*]] = "tf.AddV2"(%[[VAL_1]], %[[VAL_0]]) : (tensor<1x1000xf32>, tensor<1x1xf32>) -> tensor<1x1000xf32> +// CHECK: return %[[VAL_2]], %[[VAL_3]] : tensor<1x1000xf32>, tensor<1x1000xf32> +// CHECK: } +func.func @broadcast_add(%arg0: tensor<1x1xf32>, %arg1: tensor<1x1000xf32>) -> (tensor<1x1000xf32>, tensor<1x1000xf32>) { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 1]> : tensor<2xi64>}> : (tensor<1x1xf32>) -> tensor<1x1000xf32> + %1 = mhlo.add %0, %arg1 : tensor<1x1000xf32> + %2 = mhlo.add %arg1, %0 : tensor<1x1000xf32> + func.return %1, %2 : tensor<1x1000xf32>, tensor<1x1000xf32> +} + +// CHECK-LABEL: func @broadcast_add_chlo( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x2xi32>) -> tensor<1x2xi32> { +// CHECK: %[[VAL_2:.*]] = "tf.AddV2"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1xi32>, tensor<1x2xi32>) -> tensor<1x2xi32> +// CHECK: return %[[VAL_2]] : tensor<1x2xi32> +// CHECK: } +func.func @broadcast_add_chlo(%arg0: tensor<1xi32>, %arg1: tensor<1x2xi32>) -> tensor<1x2xi32> { + %0 = "chlo.broadcast_add"(%arg0, %arg1) {broadcast_dimensions = array} : (tensor<1xi32>, tensor<1x2xi32>) -> tensor<1x2xi32> + func.return %0 : tensor<1x2xi32> +} + +// CHECK-LABEL: func @broadcast_multi_dim_add( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<4x1x1xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<4x4x4x4xi32>) -> tensor<4x4x4x4xi32> { +// CHECK: %[[VAL_2:.*]] = "tf.AddV2"(%[[VAL_0]], %[[VAL_1]]) : (tensor<4x1x1xi32>, tensor<4x4x4x4xi32>) -> tensor<4x4x4x4xi32> +// CHECK: return %[[VAL_2]] : tensor<4x4x4x4xi32> +// CHECK: } +func.func @broadcast_multi_dim_add(%arg0: tensor<4x1x1xi32>, %arg1: tensor<4x4x4x4xi32>) -> tensor<4x4x4x4xi32> { + %0 = "chlo.broadcast_add"(%arg0, %arg1) {broadcast_dimensions = array} : (tensor<4x1x1xi32>, tensor<4x4x4x4xi32>) -> tensor<4x4x4x4xi32> + func.return %0 : tensor<4x4x4x4xi32> +} + +// CHECK-LABEL: func @unsupported_broadcast_add +// CHECK: chlo.broadcast_add +func.func @unsupported_broadcast_add(%arg0: tensor<4x1x1xi32>, %arg1: tensor<4x4x4x4xi32>) -> tensor<4x4x4x4xi32> { + %0 = "chlo.broadcast_add"(%arg0, %arg1) {broadcast_dimensions = array} : (tensor<4x1x1xi32>, tensor<4x4x4x4xi32>) -> tensor<4x4x4x4xi32> + func.return %0 : tensor<4x4x4x4xi32> +} + +// CHECK-LABEL: func @div( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xi32>) -> tensor<2xi32> { +// CHECK: %[[VAL_1:.*]] = "tf.Div"(%[[VAL_0]], %[[VAL_0]]) : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi32> +// CHECK: return %[[VAL_1]] : tensor<2xi32> +// CHECK: } +func.func @div(%arg0: tensor<2xi32>) -> tensor<2xi32> { + %0 = mhlo.divide %arg0, %arg0 : tensor<2xi32> + func.return %0 : tensor<2xi32> +} + +// CHECK-LABEL: func @broadcast_div( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x1xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x1000xf32>) -> (tensor<1x1000xf32>, tensor<1x1000xf32>) { +// CHECK-DAG. %cst = arith.constant dense<[1, 1000]> : tensor<2xi64> +// CHECK: %[[VAL_2:.*]] = "tf.Div"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1x1xf32>, tensor<1x1000xf32>) -> tensor<1x1000xf32> +// CHECK: %[[VAL_3:.*]] = "tf.Div"(%[[VAL_1]], %[[VAL_0]]) : (tensor<1x1000xf32>, tensor<1x1xf32>) -> tensor<1x1000xf32> +// CHECK: return %[[VAL_2]], %[[VAL_3]] : tensor<1x1000xf32>, tensor<1x1000xf32> +// CHECK: } +func.func @broadcast_div(%arg0: tensor<1x1xf32>, %arg1: tensor<1x1000xf32>) -> (tensor<1x1000xf32>, tensor<1x1000xf32>) { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 1]> : tensor<2xi64>}> : (tensor<1x1xf32>) -> tensor<1x1000xf32> + %1 = mhlo.divide %0, %arg1 : tensor<1x1000xf32> + %2 = mhlo.divide %arg1, %0 : tensor<1x1000xf32> + func.return %1, %2 : tensor<1x1000xf32>, tensor<1x1000xf32> +} + +// CHECK-LABEL: func @broadcast_div_chlo( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x2xi32>) -> tensor<1x2xi32> { +// CHECK: %[[VAL_2:.*]] = "tf.Div"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1xi32>, tensor<1x2xi32>) -> tensor<1x2xi32> +// CHECK: return %[[VAL_2]] : tensor<1x2xi32> +// CHECK: } +func.func @broadcast_div_chlo(%arg0: tensor<1xi32>, %arg1: tensor<1x2xi32>) -> tensor<1x2xi32> { + %0 = "chlo.broadcast_divide"(%arg0, %arg1) {broadcast_dimensions = array} : (tensor<1xi32>, tensor<1x2xi32>) -> tensor<1x2xi32> + func.return %0 : tensor<1x2xi32> +} + +// CHECK-LABEL: func @shift_left( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<4xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<4xi32>) -> tensor<4xi32> { +// CHECK: %[[VAL_2:.*]] = "tf.LeftShift"(%[[VAL_0]], %[[VAL_1]]) : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi32> +// CHECK: return %[[VAL_2]] : tensor<4xi32> +// CHECK: } +func.func @shift_left(%arg0: tensor<4xi32>, %arg1: tensor<4xi32>) -> tensor<4xi32> { + %0 = mhlo.shift_left %arg0, %arg1 : tensor<4xi32> + func.return %0 : tensor<4xi32> +} + +// CHECK-LABEL: func @broadcast_shift_left( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<4xi32>) -> (tensor<4xi32>, tensor<4xi32>) { +// CHECK-DAG. %cst = arith.constant dense<[4]> : tensor<1xi64> +// CHECK: %[[VAL_2:.*]] = "tf.LeftShift"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1xi32>, tensor<4xi32>) -> tensor<4xi32> +// CHECK: %[[VAL_3:.*]] = "tf.LeftShift"(%[[VAL_1]], %[[VAL_0]]) : (tensor<4xi32>, tensor<1xi32>) -> tensor<4xi32> +// CHECK: return %[[VAL_2]], %[[VAL_3]] : tensor<4xi32>, tensor<4xi32> +// CHECK: } +func.func @broadcast_shift_left(%arg0: tensor<1xi32>, %arg1: tensor<4xi32>) -> (tensor<4xi32>, tensor<4xi32>) { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0]> : tensor<1xi64>}> : (tensor<1xi32>) -> tensor<4xi32> + %1 = mhlo.shift_left %0, %arg1 : tensor<4xi32> + %2 = mhlo.shift_left %arg1, %0 : tensor<4xi32> + func.return %1, %2 : tensor<4xi32>, tensor<4xi32> +} + +// CHECK-LABEL: func @div_dynamic( +// CHECK-SAME: %[[VAL_0:.*]]: tensor, +// CHECK-SAME: %[[VAL_1:.*]]: tensor) -> tensor { +// CHECK: %[[VAL_2:.*]] = "tf.Div"(%[[VAL_0]], %[[VAL_1]]) : (tensor, tensor) -> tensor +// CHECK: return %[[VAL_2]] : tensor +// CHECK: } +func.func @div_dynamic(%arg0: tensor, %arg1: tensor) -> tensor { + %0 = "chlo.broadcast_divide"(%arg0, %arg1) {broadcast_dimensions = array} : (tensor, tensor) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @maximum( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<4xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<4xf32>) -> tensor<4xf32> { +// CHECK: %[[VAL_2:.*]] = "tf.Maximum"(%[[VAL_0]], %[[VAL_1]]) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> +// CHECK: return %[[VAL_2]] : tensor<4xf32> +// CHECK: } +func.func @maximum(%arg0: tensor<4xf32>, %arg1: tensor<4xf32>) -> tensor<4xf32> { + %0 = mhlo.maximum %arg0, %arg1 : tensor<4xf32> + func.return %0 : tensor<4xf32> +} + +// CHECK-LABEL: func @broadcast_maximum( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x1xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x1000xf32>) -> (tensor<1x1000xf32>, tensor<1x1000xf32>) { +// CHECK-DAG. %cst = arith.constant dense<[1, 1000]> : tensor<2xi64> +// CHECK: %[[VAL_2:.*]] = "tf.Maximum"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1x1xf32>, tensor<1x1000xf32>) -> tensor<1x1000xf32> +// CHECK: %[[VAL_3:.*]] = "tf.Maximum"(%[[VAL_1]], %[[VAL_0]]) : (tensor<1x1000xf32>, tensor<1x1xf32>) -> tensor<1x1000xf32> +// CHECK: return %[[VAL_2]], %[[VAL_3]] : tensor<1x1000xf32>, tensor<1x1000xf32> +// CHECK: } +func.func @broadcast_maximum(%arg0: tensor<1x1xf32>, %arg1: tensor<1x1000xf32>) -> (tensor<1x1000xf32>, tensor<1x1000xf32>) { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 1]> : tensor<2xi64>}> : (tensor<1x1xf32>) -> tensor<1x1000xf32> + %1 = mhlo.maximum %0, %arg1 : tensor<1x1000xf32> + %2 = mhlo.maximum %arg1, %0 : tensor<1x1000xf32> + func.return %1, %2 : tensor<1x1000xf32>, tensor<1x1000xf32> +} + +// CHECK-LABEL: func @minimum( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<4xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<4xf32>) -> tensor<4xf32> { +// CHECK: %[[VAL_2:.*]] = "tf.Minimum"(%[[VAL_0]], %[[VAL_1]]) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> +// CHECK: return %[[VAL_2]] : tensor<4xf32> +// CHECK: } +func.func @minimum(%arg0: tensor<4xf32>, %arg1: tensor<4xf32>) -> tensor<4xf32> { + %0 = mhlo.minimum %arg0, %arg1 : tensor<4xf32> + func.return %0 : tensor<4xf32> +} + +// CHECK-LABEL: func @broadcast_minimum( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x1xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x1000xf32>) -> (tensor<1x1000xf32>, tensor<1x1000xf32>) { +// CHECK-DAG. %cst = arith.constant dense<[1, 1000]> : tensor<2xi64> +// CHECK: %[[VAL_2:.*]] = "tf.Minimum"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1x1xf32>, tensor<1x1000xf32>) -> tensor<1x1000xf32> +// CHECK: %[[VAL_3:.*]] = "tf.Minimum"(%[[VAL_1]], %[[VAL_0]]) : (tensor<1x1000xf32>, tensor<1x1xf32>) -> tensor<1x1000xf32> +// CHECK: return %[[VAL_2]], %[[VAL_3]] : tensor<1x1000xf32>, tensor<1x1000xf32> +// CHECK: } +func.func @broadcast_minimum(%arg0: tensor<1x1xf32>, %arg1: tensor<1x1000xf32>) -> (tensor<1x1000xf32>, tensor<1x1000xf32>) { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 1]> : tensor<2xi64>}> : (tensor<1x1xf32>) -> tensor<1x1000xf32> + %1 = mhlo.minimum %0, %arg1 : tensor<1x1000xf32> + %2 = mhlo.minimum %arg1, %0 : tensor<1x1000xf32> + func.return %1, %2 : tensor<1x1000xf32>, tensor<1x1000xf32> +} + +// CHECK-LABEL: func @mul( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xi32>) -> tensor<2xi32> { +// CHECK: %[[VAL_1:.*]] = "tf.Mul"(%[[VAL_0]], %[[VAL_0]]) : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi32> +// CHECK: return %[[VAL_1]] : tensor<2xi32> +// CHECK: } +func.func @mul(%arg0: tensor<2xi32>) -> tensor<2xi32> { + %0 = mhlo.multiply %arg0, %arg0 : tensor<2xi32> + func.return %0 : tensor<2xi32> +} + +// CHECK-LABEL: func @broadcast_mul( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x1xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x1000xf32>) -> (tensor<1x1000xf32>, tensor<1x1000xf32>) { +// CHECK-DAG. %cst = arith.constant dense<[1, 1000]> : tensor<2xi64> +// CHECK: %[[VAL_2:.*]] = "tf.Mul"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1x1xf32>, tensor<1x1000xf32>) -> tensor<1x1000xf32> +// CHECK: %[[VAL_3:.*]] = "tf.Mul"(%[[VAL_1]], %[[VAL_0]]) : (tensor<1x1000xf32>, tensor<1x1xf32>) -> tensor<1x1000xf32> +// CHECK: return %[[VAL_2]], %[[VAL_3]] : tensor<1x1000xf32>, tensor<1x1000xf32> +// CHECK: } +func.func @broadcast_mul(%arg0: tensor<1x1xf32>, %arg1: tensor<1x1000xf32>) -> (tensor<1x1000xf32>, tensor<1x1000xf32>) { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 1]> : tensor<2xi64>}> : (tensor<1x1xf32>) -> tensor<1x1000xf32> + %1 = mhlo.multiply %0, %arg1 : tensor<1x1000xf32> + %2 = mhlo.multiply %arg1, %0 : tensor<1x1000xf32> + func.return %1, %2 : tensor<1x1000xf32>, tensor<1x1000xf32> +} + +// CHECK-LABEL: func @broadcast_mul_chlo( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x2xi32>) -> tensor<1x2xi32> { +// CHECK: %[[VAL_2:.*]] = "tf.Mul"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1xi32>, tensor<1x2xi32>) -> tensor<1x2xi32> +// CHECK: return %[[VAL_2]] : tensor<1x2xi32> +// CHECK: } +func.func @broadcast_mul_chlo(%arg0: tensor<1xi32>, %arg1: tensor<1x2xi32>) -> tensor<1x2xi32> { + %0 = "chlo.broadcast_multiply"(%arg0, %arg1) {broadcast_dimensions = array} : (tensor<1xi32>, tensor<1x2xi32>) -> tensor<1x2xi32> + func.return %0 : tensor<1x2xi32> +} + +// CHECK-LABEL: func @real_div( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xi32>) -> tensor<2xi32> { +// CHECK: %[[VAL_1:.*]] = "tf.Div"(%[[VAL_0]], %[[VAL_0]]) : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi32> +// CHECK: return %[[VAL_1]] : tensor<2xi32> +// CHECK: } +func.func @real_div(%arg0: tensor<2xi32>) -> tensor<2xi32> { + %0 = mhlo.divide %arg0, %arg0 : tensor<2xi32> + func.return %0 : tensor<2xi32> +} + +// CHECK-LABEL: func @broadcast_real_div( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x2xi32>) -> tensor<1x2xi32> { +// CHECK: %[[VAL_2:.*]] = "tf.Div"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1xi32>, tensor<1x2xi32>) -> tensor<1x2xi32> +// CHECK: return %[[VAL_2]] : tensor<1x2xi32> +// CHECK: } +func.func @broadcast_real_div(%arg0: tensor<1xi32>, %arg1: tensor<1x2xi32>) -> tensor<1x2xi32> { + %0 = "chlo.broadcast_divide"(%arg0, %arg1) {broadcast_dimensions = array} : (tensor<1xi32>, tensor<1x2xi32>) -> tensor<1x2xi32> + func.return %0 : tensor<1x2xi32> +} + +// CHECK-LABEL: func @sub( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xi32>) -> tensor<2xi32> { +// CHECK: %[[VAL_1:.*]] = "tf.Sub"(%[[VAL_0]], %[[VAL_0]]) : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi32> +// CHECK: return %[[VAL_1]] : tensor<2xi32> +// CHECK: } +func.func @sub(%arg0: tensor<2xi32>) -> tensor<2xi32> { + %0 = mhlo.subtract %arg0, %arg0 : tensor<2xi32> + func.return %0 : tensor<2xi32> +} + +// CHECK-LABEL: func @broadcast_sub( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x1xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x1000xf32>) -> (tensor<1x1000xf32>, tensor<1x1000xf32>) { +// CHECK-DAG. %cst = arith.constant dense<[1, 1000]> : tensor<2xi64> +// CHECK: %[[VAL_2:.*]] = "tf.Sub"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1x1xf32>, tensor<1x1000xf32>) -> tensor<1x1000xf32> +// CHECK: %[[VAL_3:.*]] = "tf.Sub"(%[[VAL_1]], %[[VAL_0]]) : (tensor<1x1000xf32>, tensor<1x1xf32>) -> tensor<1x1000xf32> +// CHECK: return %[[VAL_2]], %[[VAL_3]] : tensor<1x1000xf32>, tensor<1x1000xf32> +// CHECK: } +func.func @broadcast_sub(%arg0: tensor<1x1xf32>, %arg1: tensor<1x1000xf32>) -> (tensor<1x1000xf32>, tensor<1x1000xf32>) { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 1]> : tensor<2xi64>}> : (tensor<1x1xf32>) -> tensor<1x1000xf32> + %1 = mhlo.subtract %0, %arg1 : tensor<1x1000xf32> + %2 = mhlo.subtract %arg1, %0 : tensor<1x1000xf32> + func.return %1, %2 : tensor<1x1000xf32>, tensor<1x1000xf32> +} + +// CHECK-LABEL: func @broadcast_sub_chlo( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x2xi32>) -> tensor<1x2xi32> { +// CHECK: %[[VAL_2:.*]] = "tf.Sub"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1xi32>, tensor<1x2xi32>) -> tensor<1x2xi32> +// CHECK: return %[[VAL_2]] : tensor<1x2xi32> +// CHECK: } +func.func @broadcast_sub_chlo(%arg0: tensor<1xi32>, %arg1: tensor<1x2xi32>) -> tensor<1x2xi32> { + %0 = "chlo.broadcast_subtract"(%arg0, %arg1) {broadcast_dimensions = array} : (tensor<1xi32>, tensor<1x2xi32>) -> tensor<1x2xi32> + func.return %0 : tensor<1x2xi32> +} + +// CHECK-LABEL: func @broadcast_atan2( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<4xf32>) -> (tensor<4xf32>, tensor<4xf32>) { +// CHECK-DAG. %cst = arith.constant dense<[4]> : tensor<1xi64> +// CHECK: %[[VAL_2:.*]] = "tf.Atan2"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1xf32>, tensor<4xf32>) -> tensor<4xf32> +// CHECK: %[[VAL_3:.*]] = "tf.Atan2"(%[[VAL_1]], %[[VAL_0]]) : (tensor<4xf32>, tensor<1xf32>) -> tensor<4xf32> +// CHECK: return %[[VAL_2]], %[[VAL_3]] : tensor<4xf32>, tensor<4xf32> +// CHECK: } +func.func @broadcast_atan2(%arg0: tensor<1xf32>, %arg1: tensor<4xf32>) -> (tensor<4xf32>, tensor<4xf32>) { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0]> : tensor<1xi64>}> : (tensor<1xf32>) -> tensor<4xf32> + %1 = mhlo.atan2 %0, %arg1 : tensor<4xf32> + %2 = mhlo.atan2 %arg1, %0 : tensor<4xf32> + func.return %1, %2 : tensor<4xf32>, tensor<4xf32> +} + +// CHECK-LABEL: func @shift_right( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<4xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<4xi32>) -> tensor<4xi32> { +// CHECK: %[[VAL_2:.*]] = "tf.RightShift"(%[[VAL_0]], %[[VAL_1]]) : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi32> +// CHECK: return %[[VAL_2]] : tensor<4xi32> +// CHECK: } +func.func @shift_right(%arg0: tensor<4xi32>, %arg1: tensor<4xi32>) -> tensor<4xi32> { + %0 = mhlo.shift_right_arithmetic %arg0, %arg1 : tensor<4xi32> + func.return %0 : tensor<4xi32> +} + +// CHECK-LABEL: func @broadcast_shift_right( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<4xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<2x4xi32>) -> tensor<2x4xi32> { +// CHECK: %[[VAL_2:.*]] = "tf.RightShift"(%[[VAL_0]], %[[VAL_1]]) : (tensor<4xi32>, tensor<2x4xi32>) -> tensor<2x4xi32> +// CHECK: return %[[VAL_2]] : tensor<2x4xi32> +// CHECK: } +func.func @broadcast_shift_right(%arg0: tensor<4xi32>, %arg1: tensor<2x4xi32>) -> tensor<2x4xi32> { + %0 = "chlo.broadcast_shift_right_arithmetic"(%arg0, %arg1) {broadcast_dimensions = array} : (tensor<4xi32>, tensor<2x4xi32>) -> tensor<2x4xi32> + func.return %0 : tensor<2x4xi32> +} + +// CHECK-LABEL: func @and( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xi1>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<2xi1>) -> tensor<2xi1> { +// CHECK: %[[VAL_2:.*]] = "tf.LogicalAnd"(%[[VAL_0]], %[[VAL_1]]) : (tensor<2xi1>, tensor<2xi1>) -> tensor<2xi1> +// CHECK: return %[[VAL_2]] : tensor<2xi1> +// CHECK: } +func.func @and(%arg0: tensor<2xi1>, %arg1: tensor<2xi1>) -> tensor<2xi1> { + %0 = mhlo.and %arg0, %arg1 : tensor<2xi1> + func.return %0 : tensor<2xi1> +} + +// CHECK-LABEL: func @and_broadcast( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1xi1>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x2xi1>) -> tensor<1x2xi1> { +// CHECK: %[[VAL_2:.*]] = "tf.LogicalAnd"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1xi1>, tensor<1x2xi1>) -> tensor<1x2xi1> +// CHECK: return %[[VAL_2]] : tensor<1x2xi1> +// CHECK: } +func.func @and_broadcast(%arg0: tensor<1xi1>, %arg1: tensor<1x2xi1>) -> tensor<1x2xi1> { + %0 = "chlo.broadcast_and"(%arg0, %arg1) {broadcast_dimensions = array} : (tensor<1xi1>, tensor<1x2xi1>) -> tensor<1x2xi1> + func.return %0 : tensor<1x2xi1> +} + +// CHECK-LABEL: func @and_dynamic( +// CHECK-SAME: %[[VAL_0:.*]]: tensor, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1xi1>) -> tensor { +// CHECK: %[[VAL_2:.*]] = "tf.LogicalAnd"(%[[VAL_0]], %[[VAL_1]]) : (tensor, tensor<1xi1>) -> tensor +// CHECK: return %[[VAL_2]] : tensor +// CHECK: } +func.func @and_dynamic(%arg0: tensor, %arg1: tensor<1xi1>) -> tensor { + %0 = "chlo.broadcast_and"(%arg0, %arg1) : (tensor, tensor<1xi1>) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @or( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xi1>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<2xi1>) -> tensor<2xi1> { +// CHECK: %[[VAL_2:.*]] = "tf.LogicalOr"(%[[VAL_0]], %[[VAL_1]]) : (tensor<2xi1>, tensor<2xi1>) -> tensor<2xi1> +// CHECK: return %[[VAL_2]] : tensor<2xi1> +// CHECK: } +func.func @or(%arg0: tensor<2xi1>, %arg1: tensor<2xi1>) -> tensor<2xi1> { + %0 = mhlo.or %arg0, %arg1 : tensor<2xi1> + func.return %0 : tensor<2xi1> +} + +// CHECK-LABEL: func @or_broadcast( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1xi1>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x2xi1>) -> tensor<1x2xi1> { +// CHECK: %[[VAL_2:.*]] = "tf.LogicalOr"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1xi1>, tensor<1x2xi1>) -> tensor<1x2xi1> +// CHECK: return %[[VAL_2]] : tensor<1x2xi1> +// CHECK: } +func.func @or_broadcast(%arg0: tensor<1xi1>, %arg1: tensor<1x2xi1>) -> tensor<1x2xi1> { + %0 = "chlo.broadcast_or"(%arg0, %arg1) {broadcast_dimensions = array} : (tensor<1xi1>, tensor<1x2xi1>) -> tensor<1x2xi1> + func.return %0 : tensor<1x2xi1> +} + +// CHECK-LABEL: func @or_dynamic( +// CHECK-SAME: %[[VAL_0:.*]]: tensor, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1xi1>) -> tensor { +// CHECK: %[[VAL_2:.*]] = "tf.LogicalOr"(%[[VAL_0]], %[[VAL_1]]) : (tensor, tensor<1xi1>) -> tensor +// CHECK: return %[[VAL_2]] : tensor +// CHECK: } +func.func @or_dynamic(%arg0: tensor, %arg1: tensor<1xi1>) -> tensor { + %0 = "chlo.broadcast_or"(%arg0, %arg1) : (tensor, tensor<1xi1>) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @bitwise_or( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<4xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<4xi32>) -> tensor<4xi32> { +// CHECK: %[[VAL_2:.*]] = "tf.BitwiseOr"(%[[VAL_0]], %[[VAL_1]]) : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi32> +// CHECK: return %[[VAL_2]] : tensor<4xi32> +// CHECK: } +func.func @bitwise_or(%arg0: tensor<4xi32>, %arg1: tensor<4xi32>) -> tensor<4xi32> { + %0 = mhlo.or %arg0, %arg1 : tensor<4xi32> + func.return %0 : tensor<4xi32> +} + +// CHECK-LABEL: func @bitwise_or_broadcast( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1xi8>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x4xi8>) -> tensor<1x4xi8> { +// CHECK: %[[VAL_2:.*]] = "tf.BitwiseOr"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1xi8>, tensor<1x4xi8>) -> tensor<1x4xi8> +// CHECK: return %[[VAL_2]] : tensor<1x4xi8> +// CHECK: } +func.func @bitwise_or_broadcast(%arg0: tensor<1xi8>, %arg1: tensor<1x4xi8>) -> tensor<1x4xi8> { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[1]> : tensor<1xi64>}> : (tensor<1xi8>) -> tensor<1x4xi8> + %1 = mhlo.or %0, %arg1 : tensor<1x4xi8> + func.return %1 : tensor<1x4xi8> +} + +// CHECK-LABEL: func @bitwise_or_broadcast_chlo( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1xi8>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x4xi8>) -> tensor<1x4xi8> { +// CHECK: %[[VAL_2:.*]] = "tf.BitwiseOr"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1xi8>, tensor<1x4xi8>) -> tensor<1x4xi8> +// CHECK: return %[[VAL_2]] : tensor<1x4xi8> +// CHECK: } +func.func @bitwise_or_broadcast_chlo(%arg0: tensor<1xi8>, %arg1: tensor<1x4xi8>) -> tensor<1x4xi8> { + %0 = "chlo.broadcast_or"(%arg0, %arg1) {broadcast_dimensions = array} : (tensor<1xi8>, tensor<1x4xi8>) -> tensor<1x4xi8> + func.return %0 : tensor<1x4xi8> +} + +// CHECK-LABEL: func @bitwise_or_dynamic( +// CHECK-SAME: %[[VAL_0:.*]]: tensor, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1xi32>) -> tensor { +// CHECK: %[[VAL_2:.*]] = "tf.BitwiseOr"(%[[VAL_0]], %[[VAL_1]]) : (tensor, tensor<1xi32>) -> tensor +// CHECK: return %[[VAL_2]] : tensor +// CHECK: } +func.func @bitwise_or_dynamic(%arg0: tensor, %arg1: tensor<1xi32>) -> tensor { + %0 = "chlo.broadcast_or"(%arg0, %arg1) : (tensor, tensor<1xi32>) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @bitwise_xor( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<4xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<4xi32>) -> tensor<4xi32> { +// CHECK: %[[VAL_2:.*]] = "tf.BitwiseXor"(%[[VAL_0]], %[[VAL_1]]) : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi32> +// CHECK: return %[[VAL_2]] : tensor<4xi32> +// CHECK: } +func.func @bitwise_xor(%arg0: tensor<4xi32>, %arg1: tensor<4xi32>) -> tensor<4xi32> { + %0 = mhlo.xor %arg0, %arg1 : tensor<4xi32> + func.return %0 : tensor<4xi32> +} + +// CHECK-LABEL: func @bitwise_xor_broadcast( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1xi8>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x4xi8>) -> tensor<1x4xi8> { +// CHECK: %[[VAL_2:.*]] = "tf.BitwiseXor"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1xi8>, tensor<1x4xi8>) -> tensor<1x4xi8> +// CHECK: return %[[VAL_2]] : tensor<1x4xi8> +// CHECK: } +func.func @bitwise_xor_broadcast(%arg0: tensor<1xi8>, %arg1: tensor<1x4xi8>) -> tensor<1x4xi8> { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<1> : tensor<1xi64>}> : (tensor<1xi8>) -> tensor<1x4xi8> + %1 = mhlo.xor %0, %arg1 : tensor<1x4xi8> + func.return %1 : tensor<1x4xi8> +} + +// CHECK-LABEL: func @bitwise_and( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<4xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<4xi32>) -> tensor<4xi32> { +// CHECK: %[[VAL_2:.*]] = "tf.BitwiseAnd"(%[[VAL_0]], %[[VAL_1]]) : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi32> +// CHECK: return %[[VAL_2]] : tensor<4xi32> +// CHECK: } +func.func @bitwise_and(%arg0: tensor<4xi32>, %arg1: tensor<4xi32>) -> tensor<4xi32> { + %0 = mhlo.and %arg0, %arg1 : tensor<4xi32> + func.return %0 : tensor<4xi32> +} + +// CHECK-LABEL: func @bitwise_and_broadcast( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1xi8>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x4xi8>) -> tensor<1x4xi8> { +// CHECK: %[[VAL_2:.*]] = "tf.BitwiseAnd"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1xi8>, tensor<1x4xi8>) -> tensor<1x4xi8> +// CHECK: return %[[VAL_2]] : tensor<1x4xi8> +// CHECK: } +func.func @bitwise_and_broadcast(%arg0: tensor<1xi8>, %arg1: tensor<1x4xi8>) -> tensor<1x4xi8> { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[1]> : tensor<1xi64>}> : (tensor<1xi8>) -> tensor<1x4xi8> + %1 = mhlo.and %0, %arg1 : tensor<1x4xi8> + func.return %1 : tensor<1x4xi8> +} + +// CHECK-LABEL: func @bitwise_and_broadcast_chlo( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1xi8>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x4xi8>) -> tensor<1x4xi8> { +// CHECK: %[[VAL_2:.*]] = "tf.BitwiseAnd"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1xi8>, tensor<1x4xi8>) -> tensor<1x4xi8> +// CHECK: return %[[VAL_2]] : tensor<1x4xi8> +// CHECK: } +func.func @bitwise_and_broadcast_chlo(%arg0: tensor<1xi8>, %arg1: tensor<1x4xi8>) -> tensor<1x4xi8> { + %0 = "chlo.broadcast_and"(%arg0, %arg1) {broadcast_dimensions = array} : (tensor<1xi8>, tensor<1x4xi8>) -> tensor<1x4xi8> + func.return %0 : tensor<1x4xi8> +} + +// CHECK-LABEL: func @bitwise_and_dynamic( +// CHECK-SAME: %[[VAL_0:.*]]: tensor, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1xi32>) -> tensor { +// CHECK: %[[VAL_2:.*]] = "tf.BitwiseAnd"(%[[VAL_0]], %[[VAL_1]]) : (tensor, tensor<1xi32>) -> tensor +// CHECK: return %[[VAL_2]] : tensor +// CHECK: } +func.func @bitwise_and_dynamic(%arg0: tensor, %arg1: tensor<1xi32>) -> tensor { + %0 = "chlo.broadcast_and"(%arg0, %arg1) : (tensor, tensor<1xi32>) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @pow( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xf32>) -> tensor<2xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.Pow"(%[[VAL_0]], %[[VAL_0]]) : (tensor<2xf32>, tensor<2xf32>) -> tensor<2xf32> +// CHECK: return %[[VAL_1]] : tensor<2xf32> +// CHECK: } +func.func @pow(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = mhlo.power %arg0, %arg0 : tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK-LABEL: func @broadcast_pow( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<4xi32>) -> (tensor<4xi32>, tensor<4xi32>) { +// CHECK-DAG. %cst = arith.constant dense<[4]> : tensor<1xi64> +// CHECK: %[[VAL_2:.*]] = "tf.Pow"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1xi32>, tensor<4xi32>) -> tensor<4xi32> +// CHECK: %[[VAL_3:.*]] = "tf.Pow"(%[[VAL_1]], %[[VAL_0]]) : (tensor<4xi32>, tensor<1xi32>) -> tensor<4xi32> +// CHECK: return %[[VAL_2]], %[[VAL_3]] : tensor<4xi32>, tensor<4xi32> +// CHECK: } +func.func @broadcast_pow(%arg0: tensor<1xi32>, %arg1: tensor<4xi32>) -> (tensor<4xi32>, tensor<4xi32>) { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0]> : tensor<1xi64>}> : (tensor<1xi32>) -> tensor<4xi32> + %1 = mhlo.power %0, %arg1 : tensor<4xi32> + %2 = mhlo.power %arg1, %0 : tensor<4xi32> + func.return %1, %2 : tensor<4xi32>, tensor<4xi32> +} + +// CHECK-LABEL: func @pow_dynamic( +// CHECK-SAME: %[[VAL_0:.*]]: tensor) -> tensor { +// CHECK: %[[VAL_1:.*]] = "tf.Pow"(%[[VAL_0]], %[[VAL_0]]) : (tensor, tensor) -> tensor +// CHECK: return %[[VAL_1]] : tensor +// CHECK: } +func.func @pow_dynamic(%arg0: tensor) -> tensor { + %0 = mhlo.power %arg0, %arg0 : tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @floordiv_broadcast_i32( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2x3xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<3xi32>) -> tensor<2x3xi32> { +// CHECK: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<0> : tensor<2x3xi32>}> : () -> tensor<2x3xi32> +// CHECK: %[[VAL_3:.*]] = "tf.Less"(%[[VAL_0]], %[[VAL_2]]) : (tensor<2x3xi32>, tensor<2x3xi32>) -> tensor<2x3xi1> +// CHECK: %[[VAL_4:.*]] = "tf.Const"() <{value = dense<0> : tensor<3xi32>}> : () -> tensor<3xi32> +// CHECK: %[[VAL_5:.*]] = "tf.Less"(%[[VAL_1]], %[[VAL_4]]) : (tensor<3xi32>, tensor<3xi32>) -> tensor<3xi1> +// CHECK: %[[VAL_6:.*]] = "tf.Equal"(%[[VAL_3]], %[[VAL_5]]) <{incompatible_shape_error = true}> : (tensor<2x3xi1>, tensor<3xi1>) -> tensor<2x3xi1> +// CHECK: %[[VAL_7:.*]] = "tf.Div"(%[[VAL_0]], %[[VAL_1]]) : (tensor<2x3xi32>, tensor<3xi32>) -> tensor<2x3xi32> +// CHECK: %[[VAL_8:.*]] = "tf.Abs"(%[[VAL_0]]) : (tensor<2x3xi32>) -> tensor<2x3xi32> +// CHECK: %[[VAL_9:.*]] = "tf.Abs"(%[[VAL_1]]) : (tensor<3xi32>) -> tensor<3xi32> +// CHECK: %[[VAL_10:.*]] = "tf.Const"() <{value = dense<1> : tensor<3xi32>}> : () -> tensor<3xi32> +// CHECK: %[[VAL_11:.*]] = "tf.Sub"(%[[VAL_9]], %[[VAL_10]]) : (tensor<3xi32>, tensor<3xi32>) -> tensor<3xi32> +// CHECK: %[[VAL_12:.*]] = "tf.AddV2"(%[[VAL_8]], %[[VAL_11]]) : (tensor<2x3xi32>, tensor<3xi32>) -> tensor<2x3xi32> +// CHECK: %[[VAL_13:.*]] = "tf.Neg"(%[[VAL_12]]) : (tensor<2x3xi32>) -> tensor<2x3xi32> +// CHECK: %[[VAL_14:.*]] = "tf.Abs"(%[[VAL_1]]) : (tensor<3xi32>) -> tensor<3xi32> +// CHECK: %[[VAL_15:.*]] = "tf.Div"(%[[VAL_13]], %[[VAL_14]]) : (tensor<2x3xi32>, tensor<3xi32>) -> tensor<2x3xi32> +// CHECK: %[[VAL_16:.*]] = "tf.Select"(%[[VAL_6]], %[[VAL_7]], %[[VAL_15]]) : (tensor<2x3xi1>, tensor<2x3xi32>, tensor<2x3xi32>) -> tensor<2x3xi32> +// CHECK: return %[[VAL_16]] : tensor<2x3xi32> +// CHECK: } +func.func @floordiv_broadcast_i32(%arg0: tensor<2x3xi32>, %arg1: tensor<3xi32>) -> tensor<2x3xi32> { + %0 = mhlo.constant dense<0> : tensor<2x3xi32> + %1 = "chlo.broadcast_compare"(%arg0, %0) {comparison_direction = #chlo} : (tensor<2x3xi32>, tensor<2x3xi32>) -> tensor<2x3xi1> + %2 = mhlo.constant dense<0> : tensor<3xi32> + %3 = "chlo.broadcast_compare"(%arg1, %2) {comparison_direction = #chlo} : (tensor<3xi32>, tensor<3xi32>) -> tensor<3xi1> + %4 = "chlo.broadcast_compare"(%1, %3) {broadcast_dimensions = array, comparison_direction = #chlo} : (tensor<2x3xi1>, tensor<3xi1>) -> tensor<2x3xi1> + %5 = "chlo.broadcast_divide"(%arg0, %arg1) {broadcast_dimensions = array} : (tensor<2x3xi32>, tensor<3xi32>) -> tensor<2x3xi32> + %6 = "mhlo.abs"(%arg0) : (tensor<2x3xi32>) -> tensor<2x3xi32> + %7 = "mhlo.abs"(%arg1) : (tensor<3xi32>) -> tensor<3xi32> + %8 = mhlo.constant dense<1> : tensor<3xi32> + %9 = mhlo.subtract %7, %8 : tensor<3xi32> + %10 = "chlo.broadcast_add"(%6, %9) {broadcast_dimensions = array} : (tensor<2x3xi32>, tensor<3xi32>) -> tensor<2x3xi32> + %11 = "mhlo.negate"(%10) : (tensor<2x3xi32>) -> tensor<2x3xi32> + %12 = "mhlo.abs"(%arg1) : (tensor<3xi32>) -> tensor<3xi32> + %13 = "chlo.broadcast_divide"(%11, %12) {broadcast_dimensions = array} : (tensor<2x3xi32>, tensor<3xi32>) -> tensor<2x3xi32> + %14 = "mhlo.select"(%4, %5, %13) : (tensor<2x3xi1>, tensor<2x3xi32>, tensor<2x3xi32>) -> tensor<2x3xi32> + func.return %14 : tensor<2x3xi32> +} + +// CHECK-LABEL: func @floordiv_reverse_broadcast_i32( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<3xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<2x3xi32>) -> tensor<2x3xi32> { +// CHECK: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<0> : tensor<3xi32>}> : () -> tensor<3xi32> +// CHECK: %[[VAL_3:.*]] = "tf.Less"(%[[VAL_0]], %[[VAL_2]]) : (tensor<3xi32>, tensor<3xi32>) -> tensor<3xi1> +// CHECK: %[[VAL_4:.*]] = "tf.Const"() <{value = dense<0> : tensor<2x3xi32>}> : () -> tensor<2x3xi32> +// CHECK: %[[VAL_5:.*]] = "tf.Less"(%[[VAL_1]], %[[VAL_4]]) : (tensor<2x3xi32>, tensor<2x3xi32>) -> tensor<2x3xi1> +// CHECK: %[[VAL_6:.*]] = "tf.Equal"(%[[VAL_3]], %[[VAL_5]]) <{incompatible_shape_error = true}> : (tensor<3xi1>, tensor<2x3xi1>) -> tensor<2x3xi1> +// CHECK: %[[VAL_7:.*]] = "tf.Div"(%[[VAL_0]], %[[VAL_1]]) : (tensor<3xi32>, tensor<2x3xi32>) -> tensor<2x3xi32> +// CHECK: %[[VAL_8:.*]] = "tf.Abs"(%[[VAL_0]]) : (tensor<3xi32>) -> tensor<3xi32> +// CHECK: %[[VAL_9:.*]] = "tf.Abs"(%[[VAL_1]]) : (tensor<2x3xi32>) -> tensor<2x3xi32> +// CHECK: %[[VAL_10:.*]] = "tf.Const"() <{value = dense<1> : tensor<2x3xi32>}> : () -> tensor<2x3xi32> +// CHECK: %[[VAL_11:.*]] = "tf.Sub"(%[[VAL_9]], %[[VAL_10]]) : (tensor<2x3xi32>, tensor<2x3xi32>) -> tensor<2x3xi32> +// CHECK: %[[VAL_12:.*]] = "tf.AddV2"(%[[VAL_8]], %[[VAL_11]]) : (tensor<3xi32>, tensor<2x3xi32>) -> tensor<2x3xi32> +// CHECK: %[[VAL_13:.*]] = "tf.Neg"(%[[VAL_12]]) : (tensor<2x3xi32>) -> tensor<2x3xi32> +// CHECK: %[[VAL_14:.*]] = "tf.Abs"(%[[VAL_1]]) : (tensor<2x3xi32>) -> tensor<2x3xi32> +// CHECK: %[[VAL_15:.*]] = "tf.Div"(%[[VAL_13]], %[[VAL_14]]) : (tensor<2x3xi32>, tensor<2x3xi32>) -> tensor<2x3xi32> +// CHECK: %[[VAL_16:.*]] = "tf.Select"(%[[VAL_6]], %[[VAL_7]], %[[VAL_15]]) : (tensor<2x3xi1>, tensor<2x3xi32>, tensor<2x3xi32>) -> tensor<2x3xi32> +// CHECK: return %[[VAL_16]] : tensor<2x3xi32> +// CHECK: } +func.func @floordiv_reverse_broadcast_i32(%arg0: tensor<3xi32>, %arg1: tensor<2x3xi32>) -> tensor<2x3xi32> { + %0 = mhlo.constant dense<0> : tensor<3xi32> + %1 = "mhlo.compare"(%arg0, %0) {comparison_direction = #mhlo} : (tensor<3xi32>, tensor<3xi32>) -> tensor<3xi1> + %2 = mhlo.constant dense<0> : tensor<2x3xi32> + %3 = "chlo.broadcast_compare"(%arg1, %2) {comparison_direction = #chlo} : (tensor<2x3xi32>, tensor<2x3xi32>) -> tensor<2x3xi1> + %4 = "chlo.broadcast_compare"(%1, %3) {broadcast_dimensions = array, comparison_direction = #chlo} : (tensor<3xi1>, tensor<2x3xi1>) -> tensor<2x3xi1> + %5 = "chlo.broadcast_divide"(%arg0, %arg1) {broadcast_dimensions = array} : (tensor<3xi32>, tensor<2x3xi32>) -> tensor<2x3xi32> + %6 = "mhlo.abs"(%arg0) : (tensor<3xi32>) -> tensor<3xi32> + %7 = "mhlo.abs"(%arg1) : (tensor<2x3xi32>) -> tensor<2x3xi32> + %8 = mhlo.constant dense<1> : tensor<2x3xi32> + %9 = mhlo.subtract %7, %8 : tensor<2x3xi32> + %10 = "chlo.broadcast_add"(%6, %9) {broadcast_dimensions = array} : (tensor<3xi32>, tensor<2x3xi32>) -> tensor<2x3xi32> + %11 = "mhlo.negate"(%10) : (tensor<2x3xi32>) -> tensor<2x3xi32> + %12 = "mhlo.abs"(%arg1) : (tensor<2x3xi32>) -> tensor<2x3xi32> + %13 = mhlo.divide %11, %12 : tensor<2x3xi32> + %14 = "mhlo.select"(%4, %5, %13) : (tensor<2x3xi1>, tensor<2x3xi32>, tensor<2x3xi32>) -> tensor<2x3xi32> + func.return %14 : tensor<2x3xi32> +} + +// CHECK-LABEL: func @floordiv_f32( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xf32>) -> tensor<2xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.Div"(%[[VAL_0]], %[[VAL_0]]) : (tensor<2xf32>, tensor<2xf32>) -> tensor<2xf32> +// CHECK: %[[VAL_2:.*]] = "tf.Div"(%[[VAL_0]], %[[VAL_0]]) : (tensor<2xf32>, tensor<2xf32>) -> tensor<2xf32> +// CHECK: %[[VAL_3:.*]] = "tf.FloorDiv"(%[[VAL_0]], %[[VAL_0]]) : (tensor<2xf32>, tensor<2xf32>) -> tensor<2xf32> +// CHECK: return %[[VAL_3]] : tensor<2xf32> +// CHECK: } +func.func @floordiv_f32(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = mhlo.divide %arg0, %arg0 : tensor<2xf32> + %1 = mhlo.divide %arg0, %arg0 : tensor<2xf32> + %2 = "mhlo.floor"(%1) : (tensor<2xf32>) -> tensor<2xf32> + func.return %2 : tensor<2xf32> +} + +// CHECK-LABEL: func @floordiv_f16_broadcast( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2x3xf16>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<3xf16>) -> tensor<2x3xf16> { +// CHECK: %[[VAL_2:.*]] = "tf.Div"(%[[VAL_0]], %[[VAL_1]]) : (tensor<2x3xf16>, tensor<3xf16>) -> tensor<2x3xf16> +// CHECK: %[[VAL_3:.*]] = "tf.Div"(%[[VAL_0]], %[[VAL_1]]) : (tensor<2x3xf16>, tensor<3xf16>) -> tensor<2x3xf16> +// CHECK: %[[VAL_4:.*]] = "tf.FloorDiv"(%[[VAL_0]], %[[VAL_1]]) : (tensor<2x3xf16>, tensor<3xf16>) -> tensor<2x3xf16> +// CHECK: return %[[VAL_4]] : tensor<2x3xf16> +// CHECK: } +func.func @floordiv_f16_broadcast(%arg0: tensor<2x3xf16>, %arg1: tensor<3xf16>) -> tensor<2x3xf16> { + %0 = "chlo.broadcast_divide"(%arg0, %arg1) {broadcast_dimensions = array} : (tensor<2x3xf16>, tensor<3xf16>) -> tensor<2x3xf16> + %1 = "chlo.broadcast_divide"(%arg0, %arg1) {broadcast_dimensions = array} : (tensor<2x3xf16>, tensor<3xf16>) -> tensor<2x3xf16> + %2 = "mhlo.floor"(%1) : (tensor<2x3xf16>) -> tensor<2x3xf16> + func.return %2 : tensor<2x3xf16> +} + +// CHECK-LABEL: func @equal( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<2xi32>) -> tensor<2xi1> { +// CHECK: %[[VAL_2:.*]] = "tf.Equal"(%[[VAL_0]], %[[VAL_1]]) <{incompatible_shape_error = true}> : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi1> +// CHECK: return %[[VAL_2]] : tensor<2xi1> +// CHECK: } +func.func @equal(%arg0: tensor<2xi32>, %arg1: tensor<2xi32>) -> tensor<2xi1> { + %0 = "mhlo.compare"(%arg0, %arg1) {comparison_direction = #mhlo} : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi1> + func.return %0 : tensor<2xi1> +} + +// CHECK-LABEL: func @equal_dynamic( +// CHECK-SAME: %[[VAL_0:.*]]: tensor, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1xi32>) -> tensor { +// CHECK: %[[VAL_2:.*]] = "tf.Equal"(%[[VAL_0]], %[[VAL_1]]) <{incompatible_shape_error = true}> : (tensor, tensor<1xi32>) -> tensor +// CHECK: return %[[VAL_2]] : tensor +// CHECK: } +func.func @equal_dynamic(%arg0: tensor, %arg1: tensor<1xi32>) -> tensor { + %0 = "chlo.broadcast_compare"(%arg0, %arg1) {comparison_direction = #chlo} : (tensor, tensor<1xi32>) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @equal_broadcast( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x1xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x2xi32>) -> tensor<1x2xi1> { +// CHECK: %[[VAL_2:.*]] = "tf.Equal"(%[[VAL_0]], %[[VAL_1]]) <{incompatible_shape_error = true}> : (tensor<1x1xi32>, tensor<1x2xi32>) -> tensor<1x2xi1> +// CHECK: return %[[VAL_2]] : tensor<1x2xi1> +// CHECK: } +func.func @equal_broadcast(%arg0: tensor<1x1xi32>, %arg1: tensor<1x2xi32>) -> tensor<1x2xi1> { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 1]> : tensor<2xi64>}> : (tensor<1x1xi32>) -> tensor<1x2xi32> + %1 = "mhlo.compare"(%0, %arg1) {comparison_direction = #mhlo} : (tensor<1x2xi32>, tensor<1x2xi32>) -> tensor<1x2xi1> + func.return %1 : tensor<1x2xi1> +} + +// CHECK-LABEL: func @equal_broadcast_chlo( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x2xi32>) -> tensor<1x2xi1> { +// CHECK: %[[VAL_2:.*]] = "tf.Equal"(%[[VAL_0]], %[[VAL_1]]) <{incompatible_shape_error = true}> : (tensor<1xi32>, tensor<1x2xi32>) -> tensor<1x2xi1> +// CHECK: return %[[VAL_2]] : tensor<1x2xi1> +// CHECK: } +func.func @equal_broadcast_chlo(%arg0: tensor<1xi32>, %arg1: tensor<1x2xi32>) -> tensor<1x2xi1> { + %0 = "chlo.broadcast_compare"(%arg0, %arg1) {broadcast_dimensions = array, comparison_direction = #chlo} : (tensor<1xi32>, tensor<1x2xi32>) -> tensor<1x2xi1> + func.return %0 : tensor<1x2xi1> +} + +// CHECK-LABEL: func @equal_broadcast_no_incompatible_shapes_error( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x2xi32>) -> tensor<1x2xi1> { +// CHECK: %[[VAL_2:.*]] = "tf.Equal"(%[[VAL_0]], %[[VAL_1]]) <{incompatible_shape_error = true}> : (tensor<2xi32>, tensor<1x2xi32>) -> tensor<1x2xi1> +// CHECK: return %[[VAL_2]] : tensor<1x2xi1> +// CHECK: } +func.func @equal_broadcast_no_incompatible_shapes_error(%arg0: tensor<2xi32>, %arg1: tensor<1x2xi32>) -> tensor<1x2xi1> { + %0 = "chlo.broadcast_compare"(%arg0, %arg1) {broadcast_dimensions = array, comparison_direction = #chlo} : (tensor<2xi32>, tensor<1x2xi32>) -> tensor<1x2xi1> + func.return %0 : tensor<1x2xi1> +} + +// CHECK-LABEL: func @equal_incompatible_shape_broadcastable( +// CHECK-SAME: %[[VAL_0:.*]]: tensor, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1xi32>) -> tensor { +// CHECK: %[[VAL_2:.*]] = "tf.Equal"(%[[VAL_0]], %[[VAL_1]]) <{incompatible_shape_error = true}> : (tensor, tensor<1xi32>) -> tensor +// CHECK: return %[[VAL_2]] : tensor +// CHECK: } +func.func @equal_incompatible_shape_broadcastable(%arg0: tensor, %arg1: tensor<1xi32>) -> tensor { + %0 = "chlo.broadcast_compare"(%arg0, %arg1) {comparison_direction = #chlo} : (tensor, tensor<1xi32>) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @equal_unsupported_compare_type +func.func @equal_unsupported_compare_type(%arg0: tensor<1xf32>, %arg1: tensor<1x2xf32>) -> tensor<1x2xi1> { + // CHECK: chlo.broadcast_compare + %0 = "chlo.broadcast_compare"(%arg0, %arg1) {broadcast_dimensions = array, compare_type = #chlo, comparison_direction = #chlo} : (tensor<1xf32>, tensor<1x2xf32>) -> tensor<1x2xi1> + func.return %0 : tensor<1x2xi1> +} + +// CHECK-LABEL: func @notequal( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<2xi32>) -> tensor<2xi1> { +// CHECK: %[[VAL_2:.*]] = "tf.NotEqual"(%[[VAL_0]], %[[VAL_1]]) <{incompatible_shape_error = true}> : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi1> +// CHECK: return %[[VAL_2]] : tensor<2xi1> +// CHECK: } +func.func @notequal(%arg0: tensor<2xi32>, %arg1: tensor<2xi32>) -> tensor<2xi1> { + %0 = "mhlo.compare"(%arg0, %arg1) {comparison_direction = #mhlo} : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi1> + func.return %0 : tensor<2xi1> +} + +// CHECK-LABEL: func @notequal_broadcast( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x1xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x2xi32>) -> tensor<1x2xi1> { +// CHECK: %[[VAL_2:.*]] = "tf.NotEqual"(%[[VAL_0]], %[[VAL_1]]) <{incompatible_shape_error = true}> : (tensor<1x1xi32>, tensor<1x2xi32>) -> tensor<1x2xi1> +// CHECK: return %[[VAL_2]] : tensor<1x2xi1> +// CHECK: } +func.func @notequal_broadcast(%arg0: tensor<1x1xi32>, %arg1: tensor<1x2xi32>) -> tensor<1x2xi1> { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 1]> : tensor<2xi64>}> : (tensor<1x1xi32>) -> tensor<1x2xi32> + %1 = "mhlo.compare"(%0, %arg1) {comparison_direction = #mhlo} : (tensor<1x2xi32>, tensor<1x2xi32>) -> tensor<1x2xi1> + return %1 : tensor<1x2xi1> +} + +// CHECK-LABEL: func @notequal_broadcast_chlo( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x2xi32>) -> tensor<1x2xi1> { +// CHECK: %[[VAL_2:.*]] = "tf.NotEqual"(%[[VAL_0]], %[[VAL_1]]) <{incompatible_shape_error = true}> : (tensor<1xi32>, tensor<1x2xi32>) -> tensor<1x2xi1> +// CHECK: return %[[VAL_2]] : tensor<1x2xi1> +// CHECK: } +func.func @notequal_broadcast_chlo(%arg0: tensor<1xi32>, %arg1: tensor<1x2xi32>) -> tensor<1x2xi1> { + %0 = "chlo.broadcast_compare"(%arg0, %arg1) {broadcast_dimensions = array, comparison_direction = #chlo} : (tensor<1xi32>, tensor<1x2xi32>) -> tensor<1x2xi1> + func.return %0 : tensor<1x2xi1> +} + +// CHECK-LABEL: func @notequal_broadcast_no_incompatible_shapes_error( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x2xi32>) -> tensor<1x2xi1> { +// CHECK: %[[VAL_2:.*]] = "tf.NotEqual"(%[[VAL_0]], %[[VAL_1]]) <{incompatible_shape_error = true}> : (tensor<2xi32>, tensor<1x2xi32>) -> tensor<1x2xi1> +// CHECK: return %[[VAL_2]] : tensor<1x2xi1> +// CHECK: } +func.func @notequal_broadcast_no_incompatible_shapes_error(%arg0: tensor<2xi32>, %arg1: tensor<1x2xi32>) -> tensor<1x2xi1> { + %0 = "chlo.broadcast_compare"(%arg0, %arg1) {broadcast_dimensions = array, comparison_direction = #chlo} : (tensor<2xi32>, tensor<1x2xi32>) -> tensor<1x2xi1> + func.return %0 : tensor<1x2xi1> +} + +// CHECK-LABEL: func @notequal_incompatible_shape_broadcastable( +// CHECK-SAME: %[[VAL_0:.*]]: tensor, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1xi32>) -> tensor { +// CHECK: %[[VAL_2:.*]] = "tf.NotEqual"(%[[VAL_0]], %[[VAL_1]]) <{incompatible_shape_error = true}> : (tensor, tensor<1xi32>) -> tensor +// CHECK: return %[[VAL_2]] : tensor +// CHECK: } +func.func @notequal_incompatible_shape_broadcastable(%arg0: tensor, %arg1: tensor<1xi32>) -> tensor { + %0 = "chlo.broadcast_compare"(%arg0, %arg1) {comparison_direction = #chlo} : (tensor, tensor<1xi32>) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @greater( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<2xi32>) -> tensor<2xi1> { +// CHECK: %[[VAL_2:.*]] = "tf.Greater"(%[[VAL_0]], %[[VAL_1]]) : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi1> +// CHECK: return %[[VAL_2]] : tensor<2xi1> +// CHECK: } +func.func @greater(%arg0: tensor<2xi32>, %arg1: tensor<2xi32>) -> tensor<2xi1> { + %0 = "mhlo.compare"(%arg0, %arg1) {comparison_direction = #mhlo} : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi1> + func.return %0 : tensor<2xi1> +} + +// CHECK-LABEL: func @broadcast_greater( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x1xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x2xi32>) -> tensor<1x2xi1> { +// CHECK: %[[VAL_2:.*]] = "tf.Greater"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1x1xi32>, tensor<1x2xi32>) -> tensor<1x2xi1> +// CHECK: return %[[VAL_2]] : tensor<1x2xi1> +// CHECK: } +func.func @broadcast_greater(%arg0: tensor<1x1xi32>, %arg1: tensor<1x2xi32>) -> tensor<1x2xi1> { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 1]> : tensor<2xi64>}> : (tensor<1x1xi32>) -> tensor<1x2xi32> + %1 = "mhlo.compare"(%0, %arg1) {comparison_direction = #mhlo} : (tensor<1x2xi32>, tensor<1x2xi32>) -> tensor<1x2xi1> + func.return %1 : tensor<1x2xi1> +} + +// CHECK-LABEL: func @broadcast_greater_chlo( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x2xi32>) -> tensor<1x2xi1> { +// CHECK: %[[VAL_2:.*]] = "tf.Greater"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1xi32>, tensor<1x2xi32>) -> tensor<1x2xi1> +// CHECK: return %[[VAL_2]] : tensor<1x2xi1> +// CHECK: } +func.func @broadcast_greater_chlo(%arg0: tensor<1xi32>, %arg1: tensor<1x2xi32>) -> tensor<1x2xi1> { + %0 = "chlo.broadcast_compare"(%arg0, %arg1) {broadcast_dimensions = array, comparison_direction = #chlo} : (tensor<1xi32>, tensor<1x2xi32>) -> tensor<1x2xi1> + func.return %0 : tensor<1x2xi1> +} + +// CHECK-LABEL: func @greater_unsupported_compare_type +func.func @greater_unsupported_compare_type(%arg0: tensor<2xf32>, %arg1: tensor<2xf32>) -> tensor<2xi1> { + // CHECK: mhlo.compare + %0 = "mhlo.compare"(%arg0, %arg1) {compare_type = #mhlo, comparison_direction = #mhlo} : (tensor<2xf32>, tensor<2xf32>) -> tensor<2xi1> + func.return %0 : tensor<2xi1> +} + +// CHECK-LABEL: func @greater_equal( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<2xi32>) -> tensor<2xi1> { +// CHECK: %[[VAL_2:.*]] = "tf.GreaterEqual"(%[[VAL_0]], %[[VAL_1]]) : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi1> +// CHECK: return %[[VAL_2]] : tensor<2xi1> +// CHECK: } +func.func @greater_equal(%arg0: tensor<2xi32>, %arg1: tensor<2xi32>) -> tensor<2xi1> { + %0 = "mhlo.compare"(%arg0, %arg1) {comparison_direction = #mhlo} : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi1> + func.return %0 : tensor<2xi1> +} + +// CHECK-LABEL: func @broadcast_greater_equal( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x1xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x2xi32>) -> tensor<1x2xi1> { +// CHECK: %[[VAL_2:.*]] = "tf.GreaterEqual"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1x1xi32>, tensor<1x2xi32>) -> tensor<1x2xi1> +// CHECK: return %[[VAL_2]] : tensor<1x2xi1> +// CHECK: } +func.func @broadcast_greater_equal(%arg0: tensor<1x1xi32>, %arg1: tensor<1x2xi32>) -> tensor<1x2xi1> { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 1]> : tensor<2xi64>}> : (tensor<1x1xi32>) -> tensor<1x2xi32> + %1 = "mhlo.compare"(%0, %arg1) {comparison_direction = #mhlo} : (tensor<1x2xi32>, tensor<1x2xi32>) -> tensor<1x2xi1> + func.return %1 : tensor<1x2xi1> +} + +// CHECK-LABEL: func @broadcast_greater_equal_chlo( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x2xi32>) -> tensor<1x2xi1> { +// CHECK: %[[VAL_2:.*]] = "tf.GreaterEqual"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1xi32>, tensor<1x2xi32>) -> tensor<1x2xi1> +// CHECK: return %[[VAL_2]] : tensor<1x2xi1> +// CHECK: } +func.func @broadcast_greater_equal_chlo(%arg0: tensor<1xi32>, %arg1: tensor<1x2xi32>) -> tensor<1x2xi1> { + %0 = "chlo.broadcast_compare"(%arg0, %arg1) {broadcast_dimensions = array, comparison_direction = #chlo} : (tensor<1xi32>, tensor<1x2xi32>) -> tensor<1x2xi1> + func.return %0 : tensor<1x2xi1> +} + +// CHECK-LABEL: func @less( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<2xi32>) -> tensor<2xi1> { +// CHECK: %[[VAL_2:.*]] = "tf.Less"(%[[VAL_0]], %[[VAL_1]]) : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi1> +// CHECK: return %[[VAL_2]] : tensor<2xi1> +// CHECK: } +func.func @less(%arg0: tensor<2xi32>, %arg1: tensor<2xi32>) -> tensor<2xi1> { + %0 = "mhlo.compare"(%arg0, %arg1) {comparison_direction = #mhlo} : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi1> + func.return %0 : tensor<2xi1> +} + +// CHECK-LABEL: func @broadcast_less( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x1xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x2xi32>) -> tensor<1x2xi1> { +// CHECK: %[[VAL_2:.*]] = "tf.Less"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1x1xi32>, tensor<1x2xi32>) -> tensor<1x2xi1> +// CHECK: return %[[VAL_2]] : tensor<1x2xi1> +// CHECK: } +func.func @broadcast_less(%arg0: tensor<1x1xi32>, %arg1: tensor<1x2xi32>) -> tensor<1x2xi1> { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 1]> : tensor<2xi64>}> : (tensor<1x1xi32>) -> tensor<1x2xi32> + %1 = "mhlo.compare"(%0, %arg1) {comparison_direction = #mhlo} : (tensor<1x2xi32>, tensor<1x2xi32>) -> tensor<1x2xi1> + func.return %1 : tensor<1x2xi1> +} + +// CHECK-LABEL: func @broadcast_less_chlo( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x2xi32>) -> tensor<1x2xi1> { +// CHECK: %[[VAL_2:.*]] = "tf.Less"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1xi32>, tensor<1x2xi32>) -> tensor<1x2xi1> +// CHECK: return %[[VAL_2]] : tensor<1x2xi1> +// CHECK: } +func.func @broadcast_less_chlo(%arg0: tensor<1xi32>, %arg1: tensor<1x2xi32>) -> tensor<1x2xi1> { + %0 = "chlo.broadcast_compare"(%arg0, %arg1) {broadcast_dimensions = array, comparison_direction = #chlo} : (tensor<1xi32>, tensor<1x2xi32>) -> tensor<1x2xi1> + func.return %0 : tensor<1x2xi1> +} + +// CHECK-LABEL: func @less_equal( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<2xi32>) -> tensor<2xi1> { +// CHECK: %[[VAL_2:.*]] = "tf.LessEqual"(%[[VAL_0]], %[[VAL_1]]) : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi1> +// CHECK: return %[[VAL_2]] : tensor<2xi1> +// CHECK: } +func.func @less_equal(%arg0: tensor<2xi32>, %arg1: tensor<2xi32>) -> tensor<2xi1> { + %0 = "mhlo.compare"(%arg0, %arg1) {comparison_direction = #mhlo} : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi1> + func.return %0 : tensor<2xi1> +} + +// CHECK-LABEL: func @broadcast_less_equal( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x1xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x2xi32>) -> tensor<1x2xi1> { +// CHECK: %[[VAL_2:.*]] = "tf.LessEqual"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1x1xi32>, tensor<1x2xi32>) -> tensor<1x2xi1> +// CHECK: return %[[VAL_2]] : tensor<1x2xi1> +// CHECK: } +func.func @broadcast_less_equal(%arg0: tensor<1x1xi32>, %arg1: tensor<1x2xi32>) -> tensor<1x2xi1> { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 1]> : tensor<2xi64>}> : (tensor<1x1xi32>) -> tensor<1x2xi32> + %1 = "mhlo.compare"(%0, %arg1) {comparison_direction = #mhlo} : (tensor<1x2xi32>, tensor<1x2xi32>) -> tensor<1x2xi1> + func.return %1 : tensor<1x2xi1> +} + +// CHECK-LABEL: func @broadcast_less_equal_chlo( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x2xi32>) -> tensor<1x2xi1> { +// CHECK: %[[VAL_2:.*]] = "tf.LessEqual"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1xi32>, tensor<1x2xi32>) -> tensor<1x2xi1> +// CHECK: return %[[VAL_2]] : tensor<1x2xi1> +// CHECK: } +func.func @broadcast_less_equal_chlo(%arg0: tensor<1xi32>, %arg1: tensor<1x2xi32>) -> tensor<1x2xi1> { + %0 = "chlo.broadcast_compare"(%arg0, %arg1) {broadcast_dimensions = array, comparison_direction = #chlo} : (tensor<1xi32>, tensor<1x2xi32>) -> tensor<1x2xi1> + func.return %0 : tensor<1x2xi1> +} + +// CHECK-LABEL: func @concat_v2( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<3x3xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<3x3xf32>) -> tensor<6x3xf32> { +// CHECK: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK: %[[VAL_3:.*]] = "tf.ConcatV2"(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]]) : (tensor<3x3xf32>, tensor<3x3xf32>, tensor) -> tensor<6x3xf32> +// CHECK: return %[[VAL_3]] : tensor<6x3xf32> +// CHECK: } +func.func @concat_v2(%arg0: tensor<3x3xf32>, %arg1: tensor<3x3xf32>) -> tensor<6x3xf32> { + %2 = "mhlo.concatenate"(%arg0, %arg1) <{dimension = 0 : i64}> : (tensor<3x3xf32>, tensor<3x3xf32>) -> tensor<6x3xf32> + func.return %2 : tensor<6x3xf32> +} + +// CHECK-LABEL: func @concat_v2_1d_axis( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<3x3xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<3x3xf32>) -> tensor<3x6xf32> { +// CHECK: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<1> : tensor}> : () -> tensor +// CHECK: %[[VAL_3:.*]] = "tf.ConcatV2"(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]]) : (tensor<3x3xf32>, tensor<3x3xf32>, tensor) -> tensor<3x6xf32> +// CHECK: return %[[VAL_3]] : tensor<3x6xf32> +// CHECK: } +func.func @concat_v2_1d_axis(%arg0: tensor<3x3xf32>, %arg1: tensor<3x3xf32>) -> tensor<3x6xf32> { + %2 = "mhlo.concatenate"(%arg0, %arg1) <{dimension = 1 : i64}> : (tensor<3x3xf32>, tensor<3x3xf32>) -> tensor<3x6xf32> + func.return %2 : tensor<3x6xf32> +} + +// CHECK-LABEL: func @const() -> tensor<2xi32> { +// CHECK: %[[VAL_0:.*]] = "tf.Const"() <{value = dense<0> : tensor<2xi32>}> : () -> tensor<2xi32> +// CHECK: return %[[VAL_0]] : tensor<2xi32> +// CHECK: } +func.func @const() -> tensor<2xi32> { + %0 = mhlo.constant dense<0> : tensor<2xi32> + func.return %0 : tensor<2xi32> +} + +// CHECK-LABEL: func @relu( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1xi32>) -> tensor<1xi32> { +// CHECK: %[[VAL_1:.*]] = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK: %[[VAL_2:.*]] = "tf.Maximum"(%[[VAL_0]], %[[VAL_1]]) : (tensor<1xi32>, tensor) -> tensor<1xi32> +// CHECK: return %[[VAL_2]] : tensor<1xi32> +// CHECK: } +func.func @relu(%arg0: tensor<1xi32>) -> tensor<1xi32> { + %0 = mhlo.constant dense<0> : tensor + %1 = "chlo.broadcast_maximum"(%0, %arg0) {broadcast_dimensions = array} : (tensor, tensor<1xi32>) -> tensor<1xi32> + func.return %1 : tensor<1xi32> +} + +// CHECK-LABEL: func @relu_unranked( +// CHECK-SAME: %[[VAL_0:.*]]: tensor) -> tensor { +// CHECK: %[[VAL_1:.*]] = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK: %[[VAL_2:.*]] = "tf.Maximum"(%[[VAL_0]], %[[VAL_1]]) : (tensor, tensor) -> tensor +// CHECK: return %[[VAL_2]] : tensor +// CHECK: } +func.func @relu_unranked(%arg0: tensor) -> tensor { + %0 = mhlo.constant dense<0> : tensor + %1 = "chlo.broadcast_maximum"(%0, %arg0) {broadcast_dimensions = array} : (tensor, tensor) -> tensor + func.return %1 : tensor +} + +// CHECK-LABEL: func @relu6( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1xi32>) -> tensor<1xi32> { +// CHECK-DAG: %[[VAL_1:.*]] = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<6> : tensor}> : () -> tensor +// CHECK: %[[VAL_3:.*]] = "tf.Minimum"(%[[VAL_0]], %[[VAL_2]]) : (tensor<1xi32>, tensor) -> tensor<1xi32> +// CHECK: %[[VAL_4:.*]] = "tf.Maximum"(%[[VAL_3]], %[[VAL_1]]) : (tensor<1xi32>, tensor) -> tensor<1xi32> +// CHECK: return %[[VAL_4]] : tensor<1xi32> +// CHECK: } +func.func @relu6(%arg0: tensor<1xi32>) -> tensor<1xi32> { + %0 = mhlo.constant dense<0> : tensor + %1 = mhlo.constant dense<6> : tensor + %2 = "chlo.broadcast_minimum"(%arg0, %1) {broadcast_dimensions = array} : (tensor<1xi32>, tensor) -> tensor<1xi32> + %3 = "chlo.broadcast_maximum"(%2, %0) {broadcast_dimensions = array} : (tensor<1xi32>, tensor) -> tensor<1xi32> + func.return %3 : tensor<1xi32> +} + +// CHECK-LABEL: func @relu6_unranked( +// CHECK-SAME: %[[VAL_0:.*]]: tensor) -> tensor { +// CHECK-DAG: %[[VAL_1:.*]] = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<6> : tensor}> : () -> tensor +// CHECK: %[[VAL_3:.*]] = "tf.Minimum"(%[[VAL_0]], %[[VAL_2]]) : (tensor, tensor) -> tensor +// CHECK: %[[VAL_4:.*]] = "tf.Maximum"(%[[VAL_3]], %[[VAL_1]]) : (tensor, tensor) -> tensor +// CHECK: return %[[VAL_4]] : tensor +// CHECK: } +func.func @relu6_unranked(%arg0: tensor) -> tensor { + %0 = mhlo.constant dense<0> : tensor + %1 = mhlo.constant dense<6> : tensor + %2 = "chlo.broadcast_minimum"(%arg0, %1) {broadcast_dimensions = array} : (tensor, tensor) -> tensor + %3 = "chlo.broadcast_maximum"(%2, %0) {broadcast_dimensions = array} : (tensor, tensor) -> tensor + func.return %3 : tensor +} + +// CHECK-LABEL: func @relu_grad( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<4x8xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor) -> tensor<4x8xf32> { +// CHECK: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<0.000000e+00> : tensor}> : () -> tensor +// CHECK: %[[VAL_3:.*]] = "tf.Greater"(%[[VAL_1]], %[[VAL_2]]) : (tensor, tensor) -> tensor +// CHECK: %[[VAL_4:.*]] = "tf.Const"() <{value = dense<0.000000e+00> : tensor<4x8xf32>}> : () -> tensor<4x8xf32> +// CHECK: %[[VAL_5:.*]] = "tf.Select"(%[[VAL_3]], %[[VAL_0]], %[[VAL_4]]) : (tensor, tensor<4x8xf32>, tensor<4x8xf32>) -> tensor<4x8xf32> +// CHECK: return %[[VAL_5]] : tensor<4x8xf32> +// CHECK: } +func.func @relu_grad(%arg0: tensor<4x8xf32>, %arg1: tensor) -> tensor<4x8xf32> { + %0 = mhlo.constant dense<0.000000e+00> : tensor + %1 = "chlo.broadcast_compare"(%arg1, %0) {broadcast_dimensions = array, comparison_direction = #chlo} : (tensor, tensor) -> tensor + %2 = mhlo.constant dense<0.000000e+00> : tensor<4x8xf32> + %3 = "mhlo.select"(%1, %arg0, %2) : (tensor, tensor<4x8xf32>, tensor<4x8xf32>) -> tensor<4x8xf32> + func.return %3 : tensor<4x8xf32> +} + +// CHECK-LABEL: func @select( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xi1>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<2xi32>, +// CHECK-SAME: %[[VAL_2:.*]]: tensor<2xi32>) -> tensor<2xi32> { +// CHECK: %[[VAL_3:.*]] = "tf.Select"(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]]) : (tensor<2xi1>, tensor<2xi32>, tensor<2xi32>) -> tensor<2xi32> +// CHECK: return %[[VAL_3]] : tensor<2xi32> +// CHECK: } +func.func @select(%arg0: tensor<2xi1>, %arg1: tensor<2xi32>, %arg2: tensor<2xi32>) -> tensor<2xi32> { + %0 = "mhlo.select"(%arg0, %arg1, %arg2) : (tensor<2xi1>, tensor<2xi32>, tensor<2xi32>) -> tensor<2xi32> + func.return %0 : tensor<2xi32> +} + +// CHECK-LABEL: func @select_float( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xi1>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<2xf32>, +// CHECK-SAME: %[[VAL_2:.*]]: tensor<2xf32>) -> tensor<2xf32> { +// CHECK: %[[VAL_3:.*]] = "tf.Select"(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]]) : (tensor<2xi1>, tensor<2xf32>, tensor<2xf32>) -> tensor<2xf32> +// CHECK: return %[[VAL_3]] : tensor<2xf32> +// CHECK: } +func.func @select_float(%arg0: tensor<2xi1>, %arg1: tensor<2xf32>, %arg2: tensor<2xf32>) -> tensor<2xf32> { + %0 = "mhlo.select"(%arg0, %arg1, %arg2) : (tensor<2xi1>, tensor<2xf32>, tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK-LABEL: func @select_multidimensional( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<3x2xi1>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<3x2xi32>, +// CHECK-SAME: %[[VAL_2:.*]]: tensor<3x2xi32>) -> tensor<3x2xi32> { +// CHECK: %[[VAL_3:.*]] = "tf.Select"(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]]) : (tensor<3x2xi1>, tensor<3x2xi32>, tensor<3x2xi32>) -> tensor<3x2xi32> +// CHECK: return %[[VAL_3]] : tensor<3x2xi32> +// CHECK: } +func.func @select_multidimensional(%arg0: tensor<3x2xi1>, %arg1: tensor<3x2xi32>, %arg2: tensor<3x2xi32>) -> tensor<3x2xi32> { + %0 = "mhlo.select"(%arg0, %arg1, %arg2) : (tensor<3x2xi1>, tensor<3x2xi32>, tensor<3x2xi32>) -> tensor<3x2xi32> + func.return %0 : tensor<3x2xi32> +} + +// CHECK-LABEL: func @selectv2( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xi1>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<2xi32>, +// CHECK-SAME: %[[VAL_2:.*]]: tensor<2xi32>) -> tensor<2xi32> { +// CHECK: %[[VAL_3:.*]] = "tf.Select"(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]]) : (tensor<2xi1>, tensor<2xi32>, tensor<2xi32>) -> tensor<2xi32> +// CHECK: return %[[VAL_3]] : tensor<2xi32> +// CHECK: } +func.func @selectv2(%arg0: tensor<2xi1>, %arg1: tensor<2xi32>, %arg2: tensor<2xi32>) -> tensor<2xi32> { + %0 = "mhlo.select"(%arg0, %arg1, %arg2) : (tensor<2xi1>, tensor<2xi32>, tensor<2xi32>) -> tensor<2xi32> + func.return %0 : tensor<2xi32> +} + +// CHECK-LABEL: func @selectv2_pred_scalar( +// CHECK-SAME: %[[VAL_0:.*]]: tensor, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<2xi32>, +// CHECK-SAME: %[[VAL_2:.*]]: tensor<2xi32>) -> tensor<2xi32> { +// CHECK: %[[VAL_3:.*]] = "tf.Select"(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]]) : (tensor, tensor<2xi32>, tensor<2xi32>) -> tensor<2xi32> +// CHECK: return %[[VAL_3]] : tensor<2xi32> +// CHECK: } +func.func @selectv2_pred_scalar(%arg0: tensor, %arg1: tensor<2xi32>, %arg2: tensor<2xi32>) -> tensor<2xi32> { + %0 = "mhlo.select"(%arg0, %arg1, %arg2) : (tensor, tensor<2xi32>, tensor<2xi32>) -> tensor<2xi32> + func.return %0 : tensor<2xi32> +} + +// CHECK-LABEL: func @selectv2_broadcasted_operand( +// CHECK-SAME: %[[VAL_0:.*]]: tensor, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x1xi32>, +// CHECK-SAME: %[[VAL_2:.*]]: tensor<1x100xi32>) -> tensor<1x100xi32> { +// CHECK: %[[VAL_3:.*]] = "tf.SelectV2"(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]]) : (tensor, tensor<1x1xi32>, tensor<1x100xi32>) -> tensor<1x100xi32> +// CHECK: return %[[VAL_3]] : tensor<1x100xi32> +// CHECK: } +func.func @selectv2_broadcasted_operand(%arg0: tensor, %arg1: tensor<1x1xi32>, %arg2: tensor<1x100xi32>) -> tensor<1x100xi32> { + %0 = "mhlo.broadcast_in_dim"(%arg1) <{broadcast_dimensions = dense<[0, 1]> : tensor<2xi64>}> : (tensor<1x1xi32>) -> tensor<1x100xi32> + %1 = "mhlo.select"(%arg0, %0, %arg2) : (tensor, tensor<1x100xi32>, tensor<1x100xi32>) -> tensor<1x100xi32> + func.return %1 : tensor<1x100xi32> +} + +// CHECK-LABEL: func @selectv2_broadcasted_condition( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x1xi1>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x100xi32>, +// CHECK-SAME: %[[VAL_2:.*]]: tensor<1x100xi32>) -> tensor<1x100xi32> { +// CHECK: %[[VAL_3:.*]] = "tf.SelectV2"(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]]) : (tensor<1x1xi1>, tensor<1x100xi32>, tensor<1x100xi32>) -> tensor<1x100xi32> +// CHECK: return %[[VAL_3]] : tensor<1x100xi32> +// CHECK: } +func.func @selectv2_broadcasted_condition(%arg0: tensor<1x1xi1>, %arg1: tensor<1x100xi32>, %arg2: tensor<1x100xi32>) -> tensor<1x100xi32> { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 1]> : tensor<2xi64>}> : (tensor<1x1xi1>) -> tensor<1x100xi1> + %1 = "mhlo.select"(%0, %arg1, %arg2) : (tensor<1x100xi1>, tensor<1x100xi32>, tensor<1x100xi32>) -> tensor<1x100xi32> + func.return %1 : tensor<1x100xi32> +} + +// CHECK-LABEL: func @transpose_2d( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2x3xf32>) -> tensor<3x2xf32> { +// CHECK-DAG: %[[VAL_1:.*]] = "tf.Const"() <{value = dense<[1, 0]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK-DAG: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<[1, 0]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK-DAG: %[[VAL_3:.*]] = "tf.Const"() <{value = dense<[1, 0]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK: %[[VAL_4:.*]] = "tf.Transpose"(%[[VAL_0]], %[[VAL_3]]) : (tensor<2x3xf32>, tensor<2xi64>) -> tensor<3x2xf32> +// CHECK: return %[[VAL_4]] : tensor<3x2xf32> +// CHECK: } +func.func @transpose_2d(%arg0: tensor<2x3xf32>) -> tensor<3x2xf32> { + %0 = mhlo.constant dense<[1, 0]> : tensor<2xi64> + %1 = mhlo.constant dense<[1, 0]> : tensor<2xi64> + %2 = "mhlo.transpose"(%arg0) <{permutation = dense<[1, 0]> : tensor<2xi64>}> : (tensor<2x3xf32>) -> tensor<3x2xf32> + func.return %2 : tensor<3x2xf32> +} + +// CHECK-LABEL: func @transpose_3d_int32( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x2x3xf32>) -> tensor<3x2x1xf32> { +// CHECK-DAG: %[[VAL_1:.*]] = "tf.Const"() <{value = dense<[2, 1, 0]> : tensor<3xi32>}> : () -> tensor<3xi32> +// CHECK-DAG: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<[2, 1, 0]> : tensor<3xi64>}> : () -> tensor<3xi64> +// CHECK-DAG: %[[VAL_3:.*]] = "tf.Const"() <{value = dense<[2, 1, 0]> : tensor<3xi64>}> : () -> tensor<3xi64> +// CHECK: %[[VAL_4:.*]] = "tf.Transpose"(%[[VAL_0]], %[[VAL_3]]) : (tensor<1x2x3xf32>, tensor<3xi64>) -> tensor<3x2x1xf32> +// CHECK: return %[[VAL_4]] : tensor<3x2x1xf32> +// CHECK: } +func.func @transpose_3d_int32(%arg0: tensor<1x2x3xf32>) -> tensor<3x2x1xf32> { + %0 = mhlo.constant dense<[2, 1, 0]> : tensor<3xi32> + %1 = mhlo.constant dense<[2, 1, 0]> : tensor<3xi64> + %2 = "mhlo.transpose"(%arg0) <{permutation = dense<[2, 1, 0]> : tensor<3xi64>}> : (tensor<1x2x3xf32>) -> tensor<3x2x1xf32> + func.return %2 : tensor<3x2x1xf32> +} + +// CHECK-LABEL: func @transpose_3d( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x2x3xf32>) -> tensor<3x2x1xf32> { +// CHECK-DAG: %[[VAL_1:.*]] = "tf.Const"() <{value = dense<[2, 1, 0]> : tensor<3xi64>}> : () -> tensor<3xi64> +// CHECK-DAG: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<[2, 1, 0]> : tensor<3xi64>}> : () -> tensor<3xi64> +// CHECK-DAG: %[[VAL_3:.*]] = "tf.Const"() <{value = dense<[2, 1, 0]> : tensor<3xi64>}> : () -> tensor<3xi64> +// CHECK: %[[VAL_4:.*]] = "tf.Transpose"(%[[VAL_0]], %[[VAL_3]]) : (tensor<1x2x3xf32>, tensor<3xi64>) -> tensor<3x2x1xf32> +// CHECK: return %[[VAL_4]] : tensor<3x2x1xf32> +// CHECK: } +func.func @transpose_3d(%arg0: tensor<1x2x3xf32>) -> tensor<3x2x1xf32> { + %0 = mhlo.constant dense<[2, 1, 0]> : tensor<3xi64> + %1 = mhlo.constant dense<[2, 1, 0]> : tensor<3xi64> + %2 = "mhlo.transpose"(%arg0) <{permutation = dense<[2, 1, 0]> : tensor<3xi64>}> : (tensor<1x2x3xf32>) -> tensor<3x2x1xf32> + func.return %2 : tensor<3x2x1xf32> +} + +// CHECK-LABEL: func @transpose_dynamic_2d( +// CHECK-SAME: %[[VAL_0:.*]]: tensor) -> tensor<4x?xf32> { +// CHECK-DAG: %[[VAL_1:.*]] = "tf.Const"() <{value = dense<[1, 0]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK-DAG: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<[1, 0]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK-DAG: %[[VAL_3:.*]] = "tf.Const"() <{value = dense<[1, 0]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK: %[[VAL_4:.*]] = "tf.Transpose"(%[[VAL_0]], %[[VAL_3]]) : (tensor, tensor<2xi64>) -> tensor<4x?xf32> +// CHECK: return %[[VAL_4]] : tensor<4x?xf32> +// CHECK: } +func.func @transpose_dynamic_2d(%arg0: tensor) -> tensor<4x?xf32> { + %0 = mhlo.constant dense<[1, 0]> : tensor<2xi64> + %1 = mhlo.constant dense<[1, 0]> : tensor<2xi64> + %2 = "mhlo.transpose"(%arg0) <{permutation = dense<[1, 0]> : tensor<2xi64>}> : (tensor) -> tensor<4x?xf32> + func.return %2 : tensor<4x?xf32> +} + +// CHECK-LABEL: func @abs( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xf32>) -> tensor<2xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.Abs"(%[[VAL_0]]) : (tensor<2xf32>) -> tensor<2xf32> +// CHECK: return %[[VAL_1]] : tensor<2xf32> +// CHECK: } +func.func @abs(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "mhlo.abs"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK-LABEL: func @abs_dynamic( +// CHECK-SAME: %[[VAL_0:.*]]: tensor) -> tensor { +// CHECK: %[[VAL_1:.*]] = "tf.Abs"(%[[VAL_0]]) : (tensor) -> tensor +// CHECK: return %[[VAL_1]] : tensor +// CHECK: } +func.func @abs_dynamic(%arg0: tensor) -> tensor { + %0 = "mhlo.abs"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @ceil( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xf32>) -> tensor<2xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.Ceil"(%[[VAL_0]]) : (tensor<2xf32>) -> tensor<2xf32> +// CHECK: return %[[VAL_1]] : tensor<2xf32> +// CHECK: } +func.func @ceil(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "mhlo.ceil"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK-LABEL: func @ceil_dynamic( +// CHECK-SAME: %[[VAL_0:.*]]: tensor) -> tensor { +// CHECK: %[[VAL_1:.*]] = "tf.Ceil"(%[[VAL_0]]) : (tensor) -> tensor +// CHECK: return %[[VAL_1]] : tensor +// CHECK: } +func.func @ceil_dynamic(%arg0: tensor) -> tensor { + %0 = "mhlo.ceil"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @complex_abs( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xcomplex>) -> tensor<2xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.ComplexAbs"(%[[VAL_0]]) : (tensor<2xcomplex>) -> tensor<2xf32> +// CHECK: return %[[VAL_1]] : tensor<2xf32> +// CHECK: } +func.func @complex_abs(%arg0: tensor<2xcomplex>) -> tensor<2xf32> { + %0 = "mhlo.abs"(%arg0) : (tensor<2xcomplex>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK-LABEL: func @cos( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xf32>) -> tensor<2xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.Cos"(%[[VAL_0]]) : (tensor<2xf32>) -> tensor<2xf32> +// CHECK: return %[[VAL_1]] : tensor<2xf32> +// CHECK: } +func.func @cos(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "mhlo.cosine"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK-LABEL: func @cos_dynamic( +// CHECK-SAME: %[[VAL_0:.*]]: tensor) -> tensor { +// CHECK: %[[VAL_1:.*]] = "tf.Cos"(%[[VAL_0]]) : (tensor) -> tensor +// CHECK: return %[[VAL_1]] : tensor +// CHECK: } +func.func @cos_dynamic(%arg0: tensor) -> tensor { + %0 = "mhlo.cosine"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @exp( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xf32>) -> tensor<2xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.Exp"(%[[VAL_0]]) : (tensor<2xf32>) -> tensor<2xf32> +// CHECK: return %[[VAL_1]] : tensor<2xf32> +// CHECK: } +func.func @exp(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "mhlo.exponential"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK-LABEL: func @exp_dynamic( +// CHECK-SAME: %[[VAL_0:.*]]: tensor) -> tensor { +// CHECK: %[[VAL_1:.*]] = "tf.Exp"(%[[VAL_0]]) : (tensor) -> tensor +// CHECK: return %[[VAL_1]] : tensor +// CHECK: } +func.func @exp_dynamic(%arg0: tensor) -> tensor { + %0 = "mhlo.exponential"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @floor( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xf32>) -> tensor<2xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.Floor"(%[[VAL_0]]) : (tensor<2xf32>) -> tensor<2xf32> +// CHECK: return %[[VAL_1]] : tensor<2xf32> +// CHECK: } +func.func @floor(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "mhlo.floor"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK-LABEL: func @floor_dynamic( +// CHECK-SAME: %[[VAL_0:.*]]: tensor) -> tensor { +// CHECK: %[[VAL_1:.*]] = "tf.Floor"(%[[VAL_0]]) : (tensor) -> tensor +// CHECK: return %[[VAL_1]] : tensor +// CHECK: } +func.func @floor_dynamic(%arg0: tensor) -> tensor { + %0 = "mhlo.floor"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @is_finite( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xf32>) -> tensor<2xi1> { +// CHECK: %[[VAL_1:.*]] = "tf.IsFinite"(%[[VAL_0]]) : (tensor<2xf32>) -> tensor<2xi1> +// CHECK: return %[[VAL_1]] : tensor<2xi1> +// CHECK: } +func.func @is_finite(%arg0: tensor<2xf32>) -> tensor<2xi1> { + %0 = "mhlo.is_finite"(%arg0) : (tensor<2xf32>) -> tensor<2xi1> + func.return %0 : tensor<2xi1> +} + +// CHECK-LABEL: func @is_finite_dynamic( +// CHECK-SAME: %[[VAL_0:.*]]: tensor) -> tensor { +// CHECK: %[[VAL_1:.*]] = "tf.IsFinite"(%[[VAL_0]]) : (tensor) -> tensor +// CHECK: return %[[VAL_1]] : tensor +// CHECK: } +func.func @is_finite_dynamic(%arg0: tensor) -> tensor { + %0 = "mhlo.is_finite"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @log( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xf32>) -> tensor<2xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.Log"(%[[VAL_0]]) : (tensor<2xf32>) -> tensor<2xf32> +// CHECK: return %[[VAL_1]] : tensor<2xf32> +// CHECK: } +func.func @log(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "mhlo.log"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK-LABEL: func @log_dynamic( +// CHECK-SAME: %[[VAL_0:.*]]: tensor) -> tensor { +// CHECK: %[[VAL_1:.*]] = "tf.Log"(%[[VAL_0]]) : (tensor) -> tensor +// CHECK: return %[[VAL_1]] : tensor +// CHECK: } +func.func @log_dynamic(%arg0: tensor) -> tensor { + %0 = "mhlo.log"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @log1p( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xf32>) -> tensor<2xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.Log1p"(%[[VAL_0]]) : (tensor<2xf32>) -> tensor<2xf32> +// CHECK: return %[[VAL_1]] : tensor<2xf32> +// CHECK: } +func.func @log1p(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "mhlo.log_plus_one"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK-LABEL: func @log1p_dynamic( +// CHECK-SAME: %[[VAL_0:.*]]: tensor) -> tensor { +// CHECK: %[[VAL_1:.*]] = "tf.Log1p"(%[[VAL_0]]) : (tensor) -> tensor +// CHECK: return %[[VAL_1]] : tensor +// CHECK: } +func.func @log1p_dynamic(%arg0: tensor) -> tensor { + %0 = "mhlo.log_plus_one"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @neg( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xf32>) -> tensor<2xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.Neg"(%[[VAL_0]]) : (tensor<2xf32>) -> tensor<2xf32> +// CHECK: return %[[VAL_1]] : tensor<2xf32> +// CHECK: } +func.func @neg(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "mhlo.negate"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK-LABEL: func @neg_dynamic( +// CHECK-SAME: %[[VAL_0:.*]]: tensor) -> tensor { +// CHECK: %[[VAL_1:.*]] = "tf.Neg"(%[[VAL_0]]) : (tensor) -> tensor +// CHECK: return %[[VAL_1]] : tensor +// CHECK: } +func.func @neg_dynamic(%arg0: tensor) -> tensor { + %0 = "mhlo.negate"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @sigmoid( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xf32>) -> tensor<2xf32> { +// CHECK-DAG: %[[VAL_1:.*]] = "tf.Const"() <{value = dense<5.000000e-01> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<2> : tensor<1xi64>}> : () -> tensor<1xi64> +// CHECK-DAG: %[[VAL_3:.*]] = "tf.Const"() <{value = dense<5.000000e-01> : tensor<2xf32>}> : () -> tensor<2xf32> +// CHECK: %[[VAL_4:.*]] = "tf.Mul"(%[[VAL_0]], %[[VAL_3]]) : (tensor<2xf32>, tensor<2xf32>) -> tensor<2xf32> +// CHECK: %[[VAL_5:.*]] = "tf.Tanh"(%[[VAL_4]]) : (tensor<2xf32>) -> tensor<2xf32> +// CHECK: %[[VAL_6:.*]] = "tf.Mul"(%[[VAL_5]], %[[VAL_3]]) : (tensor<2xf32>, tensor<2xf32>) -> tensor<2xf32> +// CHECK: %[[VAL_7:.*]] = "tf.AddV2"(%[[VAL_6]], %[[VAL_3]]) : (tensor<2xf32>, tensor<2xf32>) -> tensor<2xf32> +// CHECK: return %[[VAL_7]] : tensor<2xf32> +// CHECK: } +func.func @sigmoid(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = mhlo.constant dense<5.000000e-01> : tensor + %1 = mhlo.constant dense<2> : tensor<1xi64> + %2 = mhlo.constant dense<5.000000e-01> : tensor<2xf32> + %3 = mhlo.multiply %arg0, %2 : tensor<2xf32> + %4 = "mhlo.tanh"(%3) : (tensor<2xf32>) -> tensor<2xf32> + %5 = mhlo.multiply %4, %2 : tensor<2xf32> + %6 = mhlo.add %5, %2 : tensor<2xf32> + func.return %6 : tensor<2xf32> +} + +// CHECK-LABEL: func @sin( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xf32>) -> tensor<2xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.Sin"(%[[VAL_0]]) : (tensor<2xf32>) -> tensor<2xf32> +// CHECK: return %[[VAL_1]] : tensor<2xf32> +// CHECK: } +func.func @sin(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "mhlo.sine"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK-LABEL: func @sin_dynamic( +// CHECK-SAME: %[[VAL_0:.*]]: tensor) -> tensor { +// CHECK: %[[VAL_1:.*]] = "tf.Sin"(%[[VAL_0]]) : (tensor) -> tensor +// CHECK: return %[[VAL_1]] : tensor +// CHECK: } +func.func @sin_dynamic(%arg0: tensor) -> tensor { + %0 = "mhlo.sine"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @rsqrt( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xf32>) -> tensor<2xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.Rsqrt"(%[[VAL_0]]) : (tensor<2xf32>) -> tensor<2xf32> +// CHECK: return %[[VAL_1]] : tensor<2xf32> +// CHECK: } +func.func @rsqrt(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "mhlo.rsqrt"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK-LABEL: func @rsqrt_dynamic( +// CHECK-SAME: %[[VAL_0:.*]]: tensor) -> tensor { +// CHECK: %[[VAL_1:.*]] = "tf.Rsqrt"(%[[VAL_0]]) : (tensor) -> tensor +// CHECK: return %[[VAL_1]] : tensor +// CHECK: } +func.func @rsqrt_dynamic(%arg0: tensor) -> tensor { + %0 = "mhlo.rsqrt"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @sqrt( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xf32>) -> tensor<2xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.Sqrt"(%[[VAL_0]]) : (tensor<2xf32>) -> tensor<2xf32> +// CHECK: return %[[VAL_1]] : tensor<2xf32> +// CHECK: } +func.func @sqrt(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "mhlo.sqrt"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK-LABEL: func @sqrt_dynamic( +// CHECK-SAME: %[[VAL_0:.*]]: tensor) -> tensor { +// CHECK: %[[VAL_1:.*]] = "tf.Sqrt"(%[[VAL_0]]) : (tensor) -> tensor +// CHECK: return %[[VAL_1]] : tensor +// CHECK: } +func.func @sqrt_dynamic(%arg0: tensor) -> tensor { + %0 = "mhlo.sqrt"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @tanh( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xf32>) -> tensor<2xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.Tanh"(%[[VAL_0]]) : (tensor<2xf32>) -> tensor<2xf32> +// CHECK: return %[[VAL_1]] : tensor<2xf32> +// CHECK: } +func.func @tanh(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "mhlo.tanh"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK-LABEL: func @tanh_dynamic( +// CHECK-SAME: %[[VAL_0:.*]]: tensor) -> tensor { +// CHECK: %[[VAL_1:.*]] = "tf.Tanh"(%[[VAL_0]]) : (tensor) -> tensor +// CHECK: return %[[VAL_1]] : tensor +// CHECK: } +func.func @tanh_dynamic(%arg0: tensor) -> tensor { + %0 = "mhlo.tanh"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @bitcast( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xf32>) -> tensor<2xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.Bitcast"(%[[VAL_0]]) : (tensor<2xf32>) -> tensor<2xf32> +// CHECK: return %[[VAL_1]] : tensor<2xf32> +// CHECK: } +func.func @bitcast(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "mhlo.bitcast_convert"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK-LABEL: func @bitcast_dynamic( +// CHECK-SAME: %[[VAL_0:.*]]: tensor) -> tensor { +// CHECK: %[[VAL_1:.*]] = "tf.Bitcast"(%[[VAL_0]]) : (tensor) -> tensor +// CHECK: return %[[VAL_1]] : tensor +// CHECK: } +func.func @bitcast_dynamic(%arg0: tensor) -> tensor { + %0 = "mhlo.bitcast_convert"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @bitcast_same_widths( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xf32>) -> tensor<2xi32> { +// CHECK: %[[VAL_1:.*]] = "tf.Bitcast"(%[[VAL_0]]) : (tensor<2xf32>) -> tensor<2xi32> +// CHECK: return %[[VAL_1]] : tensor<2xi32> +// CHECK: } +func.func @bitcast_same_widths(%arg0: tensor<2xf32>) -> tensor<2xi32> { + %0 = "mhlo.bitcast_convert"(%arg0) : (tensor<2xf32>) -> tensor<2xi32> + func.return %0 : tensor<2xi32> +} + +// CHECK-LABEL: func @sign( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x2x3x4xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x2x3x4xf32>) -> tensor<1x2x3x4xf32> { +// CHECK: %[[VAL_2:.*]] = "tf.NotEqual"(%[[VAL_0]], %[[VAL_1]]) <{incompatible_shape_error = true}> : (tensor<1x2x3x4xf32>, tensor<1x2x3x4xf32>) -> tensor<1x2x3x4xi1> +// CHECK: %[[VAL_3:.*]] = "tf.Const"() <{value = dense<0.000000e+00> : tensor<1x2x3x4xf32>}> : () -> tensor<1x2x3x4xf32> +// CHECK: %[[VAL_4:.*]] = "tf.NotEqual"(%[[VAL_0]], %[[VAL_1]]) <{incompatible_shape_error = true}> : (tensor<1x2x3x4xf32>, tensor<1x2x3x4xf32>) -> tensor<1x2x3x4xi1> +// CHECK: %[[VAL_5:.*]] = "tf.Const"() <{value = dense<0.000000e+00> : tensor<1x2x3x4xf32>}> : () -> tensor<1x2x3x4xf32> +// CHECK: %[[VAL_6:.*]] = "tf.Sign"(%[[VAL_0]]) : (tensor<1x2x3x4xf32>) -> tensor<1x2x3x4xf32> +// CHECK: %[[VAL_7:.*]] = "tf.Select"(%[[VAL_4]], %[[VAL_5]], %[[VAL_6]]) : (tensor<1x2x3x4xi1>, tensor<1x2x3x4xf32>, tensor<1x2x3x4xf32>) -> tensor<1x2x3x4xf32> +// CHECK: %[[VAL_8:.*]] = "tf.Select"(%[[VAL_2]], %[[VAL_3]], %[[VAL_7]]) : (tensor<1x2x3x4xi1>, tensor<1x2x3x4xf32>, tensor<1x2x3x4xf32>) -> tensor<1x2x3x4xf32> +// CHECK: return %[[VAL_8]] : tensor<1x2x3x4xf32> +// CHECK: } +func.func @sign(%arg0: tensor<1x2x3x4xf32>, %arg1: tensor<1x2x3x4xf32>) -> tensor<1x2x3x4xf32> { + %0 = "mhlo.compare"(%arg0, %arg1) {comparison_direction = #mhlo} : (tensor<1x2x3x4xf32>, tensor<1x2x3x4xf32>) -> tensor<1x2x3x4xi1> + %1 = mhlo.constant dense<0.000000e+00> : tensor<1x2x3x4xf32> + %2 = "mhlo.compare"(%arg0, %arg1) {comparison_direction = #mhlo} : (tensor<1x2x3x4xf32>, tensor<1x2x3x4xf32>) -> tensor<1x2x3x4xi1> + %3 = mhlo.constant dense<0.000000e+00> : tensor<1x2x3x4xf32> + %4 = "mhlo.sign"(%arg0) : (tensor<1x2x3x4xf32>) -> tensor<1x2x3x4xf32> + %5 = "mhlo.select"(%2, %3, %4) : (tensor<1x2x3x4xi1>, tensor<1x2x3x4xf32>, tensor<1x2x3x4xf32>) -> tensor<1x2x3x4xf32> + %6 = "mhlo.select"(%0, %1, %5) : (tensor<1x2x3x4xi1>, tensor<1x2x3x4xf32>, tensor<1x2x3x4xf32>) -> tensor<1x2x3x4xf32> + func.return %6 : tensor<1x2x3x4xf32> +} + +// CHECK-LABEL: func @size_rank_one_i32( +// CHECK-SAME: %[[VAL_0:.*]]: tensor) -> tensor { +// CHECK: %[[VAL_1:.*]] = "tf.Const"() <{value = dense<1> : tensor}> : () -> tensor +// CHECK: return %[[VAL_1]] : tensor +// CHECK: } +func.func @size_rank_one_i32(%arg0: tensor) -> tensor { + %0 = mhlo.constant dense<1> : tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @size_rank_one_i64( +// CHECK-SAME: %[[VAL_0:.*]]: tensor) -> tensor { +// CHECK: %[[VAL_1:.*]] = "tf.Const"() <{value = dense<1> : tensor}> : () -> tensor +// CHECK: return %[[VAL_1]] : tensor +// CHECK: } +func.func @size_rank_one_i64(%arg0: tensor) -> tensor { + %0 = mhlo.constant dense<1> : tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @complex( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<3xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<3xf32>) -> tensor<3xcomplex> { +// CHECK: %[[VAL_2:.*]] = "tf.Complex"(%[[VAL_0]], %[[VAL_1]]) : (tensor<3xf32>, tensor<3xf32>) -> tensor<3xcomplex> +// CHECK: return %[[VAL_2]] : tensor<3xcomplex> +// CHECK: } +func.func @complex(%arg0: tensor<3xf32>, %arg1: tensor<3xf32>) -> tensor<3xcomplex> { + %0 = "mhlo.complex"(%arg0, %arg1) : (tensor<3xf32>, tensor<3xf32>) -> tensor<3xcomplex> + func.return %0 : tensor<3xcomplex> +} + +// CHECK-LABEL: func @convert_i32_f32( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xi32>) -> tensor<2xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.Cast"(%[[VAL_0]]) <{Truncate = false}> : (tensor<2xi32>) -> tensor<2xf32> +// CHECK: return %[[VAL_1]] : tensor<2xf32> +// CHECK: } +func.func @convert_i32_f32(%arg0: tensor<2xi32>) -> tensor<2xf32> { + %0 = "mhlo.convert"(%arg0) : (tensor<2xi32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK-LABEL: func @convert_slice( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x4672xf32>) -> tensor<1x519xf32> { +// CHECK-DAG: %[[VAL_1:.*]] = "tf.Const"() <{value = dense<[0, 4153]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK-DAG: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<[1, 4672]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK-DAG: %[[VAL_3:.*]] = "tf.Const"() <{value = dense<1> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK: %[[VAL_4:.*]] = "tf.StridedSlice"(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]], %[[VAL_3]]) +// CHECK-SAME: <{begin_mask = 0 : i64, ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64}> +// CHECK-SAME: (tensor<1x4672xf32>, tensor<2xi64>, tensor<2xi64>, tensor<2xi64>) -> tensor<1x519xf32> +// CHECK: return %[[VAL_4]] : tensor<1x519xf32> +// CHECK: } +func.func @convert_slice(%arg0: tensor<1x4672xf32>) -> tensor<1x519xf32> { + %0 = "mhlo.slice"(%arg0) <{limit_indices = dense<[1, 4672]> : tensor<2xi64>, start_indices = dense<[0, 4153]> : tensor<2xi64>, strides = dense<1> : tensor<2xi64>}> : (tensor<1x4672xf32>) -> tensor<1x519xf32> + func.return %0 : tensor<1x519xf32> +} + +// CHECK-LABEL: func @reshape( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<4x6xf32>) -> tensor<2x2x6xf32> { +// CHECK: %[[VAL_1:.*]] = arith.constant dense<[2, 2, 6]> : tensor<3xi64> +// CHECK: %[[VAL_2:.*]] = "tf.Reshape"(%[[VAL_0]], %[[VAL_1]]) : (tensor<4x6xf32>, tensor<3xi64>) -> tensor<2x2x6xf32> +// CHECK: return %[[VAL_2]] : tensor<2x2x6xf32> +// CHECK: } +func.func @reshape(%arg0: tensor<4x6xf32>) -> tensor<2x2x6xf32> { + %0 = "mhlo.reshape"(%arg0) : (tensor<4x6xf32>) -> tensor<2x2x6xf32> + func.return %0 : tensor<2x2x6xf32> +} + +// CHECK-LABEL: func @dynamic_reshape( +// CHECK-SAME: %arg0: tensor<1x1x1x?xf32>, +// CHECK-SAME: %arg1: tensor<3xi32>, +// CHECK-SAME: %arg2: tensor<1xi32>) -> tensor { +// CHECK: %0 = "tf.Reshape"(%arg0, %arg1) : (tensor<1x1x1x?xf32>, tensor<3xi32>) -> tensor<1x1x?xf32> +// CHECK: %1 = "tf.Reshape"(%0, %arg2) : (tensor<1x1x?xf32>, tensor<1xi32>) -> tensor +// CHECK: return %1 : tensor +func.func @dynamic_reshape(%arg0: tensor<1x1x1x?xf32>, %arg1: tensor<3xi32>, %arg2: tensor<1xi32>) -> tensor { + %0 = mhlo.dynamic_reshape %arg0, %arg1 : (tensor<1x1x1x?xf32>, tensor<3xi32>) -> tensor<1x1x?xf32> + %1 = mhlo.dynamic_reshape %0, %arg2 : (tensor<1x1x?xf32>, tensor<1xi32>) -> tensor + func.return %1 : tensor +} + +// CHECK-LABEL: func @round_nearest_even( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xf32>) -> tensor<2xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.Round"(%[[VAL_0]]) : (tensor<2xf32>) -> tensor<2xf32> +// CHECK: return %[[VAL_1]] : tensor<2xf32> +// CHECK: } +func.func @round_nearest_even(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "mhlo.round_nearest_even"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + return %0 : tensor<2xf32> +} + +// CHECK-LABEL: func @convert_dot_2d_1d( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x256xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<256xf32>) -> tensor<1xf32> { +// CHECK: %[[VAL_2:.*]] = arith.constant dense<[256, 1]> : tensor<2xi64> +// CHECK: %[[VAL_3:.*]] = "tf.Reshape"(%[[VAL_1]], %[[VAL_2]]) : (tensor<256xf32>, tensor<2xi64>) -> tensor<256x1xf32> +// CHECK: %[[VAL_4:.*]] = "tf.BatchMatMulV3"(%[[VAL_0]], %[[VAL_3]]) <{adj_x = false, adj_y = false, grad_x = false, grad_y = false}> : (tensor<1x256xf32>, tensor<256x1xf32>) -> tensor<1x1xf32> +// CHECK: %[[VAL_5:.*]] = arith.constant dense<1> : tensor<1xi64> +// CHECK: %[[VAL_6:.*]] = "tf.Reshape"(%[[VAL_4]], %[[VAL_5]]) : (tensor<1x1xf32>, tensor<1xi64>) -> tensor<1xf32> +// CHECK: return %[[VAL_6]] : tensor<1xf32> +// CHECK: } +func.func @convert_dot_2d_1d(%arg0: tensor<1x256xf32>, %arg1: tensor<256xf32>) -> tensor<1xf32> { + %0 = "mhlo.dot"(%arg0, %arg1) {precision_config = [#mhlo, #mhlo]} : (tensor<1x256xf32>, tensor<256xf32>) -> tensor<1xf32> + func.return %0 : tensor<1xf32> +} + +// CHECK-LABEL: func @convert_dot_1d_1d( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<256xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<256xf32>) -> tensor { +// CHECK: %[[VAL_2:.*]] = arith.constant dense<[1, 256]> : tensor<2xi64> +// CHECK: %[[VAL_3:.*]] = "tf.Reshape"(%[[VAL_0]], %[[VAL_2]]) : (tensor<256xf32>, tensor<2xi64>) -> tensor<1x256xf32> +// CHECK: %[[VAL_4:.*]] = arith.constant dense<[256, 1]> : tensor<2xi64> +// CHECK: %[[VAL_5:.*]] = "tf.Reshape"(%[[VAL_1]], %[[VAL_4]]) : (tensor<256xf32>, tensor<2xi64>) -> tensor<256x1xf32> +// CHECK: %[[VAL_6:.*]] = "tf.BatchMatMulV3"(%[[VAL_3]], %[[VAL_5]]) <{adj_x = false, adj_y = false, grad_x = false, grad_y = false}> : (tensor<1x256xf32>, tensor<256x1xf32>) -> tensor<1x1xf32> +// CHECK: %[[VAL_7:.*]] = arith.constant dense<> : tensor<0xi64> +// CHECK: %[[VAL_8:.*]] = "tf.Reshape"(%[[VAL_6]], %[[VAL_7]]) : (tensor<1x1xf32>, tensor<0xi64>) -> tensor +// CHECK: return %[[VAL_8]] : tensor +// CHECK: } +func.func @convert_dot_1d_1d(%arg0: tensor<256xf32>, %arg1: tensor<256xf32>) -> tensor { + %0 = "mhlo.dot"(%arg0, %arg1) {precision_config = [#mhlo, #mhlo]} : (tensor<256xf32>, tensor<256xf32>) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @convert_dot_2d_2d( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x256xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<256x1xf32>) -> tensor<1x1xf32> { +// CHECK: %[[VAL_2:.*]] = "tf.BatchMatMulV3"(%[[VAL_0]], %[[VAL_1]]) <{adj_x = false, adj_y = false, grad_x = false, grad_y = false}> : (tensor<1x256xf32>, tensor<256x1xf32>) -> tensor<1x1xf32> +// CHECK: return %[[VAL_2]] : tensor<1x1xf32> +// CHECK: } +func.func @convert_dot_2d_2d(%arg0: tensor<1x256xf32>, %arg1: tensor<256x1xf32>) -> tensor<1x1xf32> { + %0 = "mhlo.dot"(%arg0, %arg1) {precision_config = [#mhlo, #mhlo]} : (tensor<1x256xf32>, tensor<256x1xf32>) -> tensor<1x1xf32> + func.return %0 : tensor<1x1xf32> +} + +// CHECK-LABEL: func @broadcast_in_dim_tf_style( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<8x1x16xf32>) -> tensor<3x8x8x16xf32> { +// CHECK: %[[VAL_1:.*]] = arith.constant dense<[3, 8, 8, 16]> : tensor<4xi64> +// CHECK: %[[VAL_2:.*]] = "tf.BroadcastTo"(%[[VAL_0]], %[[VAL_1]]) : (tensor<8x1x16xf32>, tensor<4xi64>) -> tensor<3x8x8x16xf32> +// CHECK: return %[[VAL_2]] : tensor<3x8x8x16xf32> +// CHECK: } +func.func @broadcast_in_dim_tf_style(%arg0: tensor<8x1x16xf32>) -> tensor<3x8x8x16xf32> { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[1, 2, 3]> : tensor<3xi64>, name = "broadcast.0"}> : (tensor<8x1x16xf32>) -> tensor<3x8x8x16xf32> + func.return %0 : tensor<3x8x8x16xf32> +} + +// CHECK-LABEL: func @broadcast_in_dim_general_case( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<3x1x16xf32>) -> tensor<3x8x8x16xf32> { +// CHECK: %[[VAL_1:.*]] = arith.constant dense<[3, 1, 1, 16]> : tensor<4xi64> +// CHECK: %[[VAL_2:.*]] = "tf.Reshape"(%[[VAL_0]], %[[VAL_1]]) : (tensor<3x1x16xf32>, tensor<4xi64>) -> tensor<3x1x1x16xf32> +// CHECK: %[[VAL_3:.*]] = arith.constant dense<[3, 8, 8, 16]> : tensor<4xi64> +// CHECK: %[[VAL_4:.*]] = "tf.BroadcastTo"(%[[VAL_2]], %[[VAL_3]]) : (tensor<3x1x1x16xf32>, tensor<4xi64>) -> tensor<3x8x8x16xf32> +// CHECK: return %[[VAL_4]] : tensor<3x8x8x16xf32> +// CHECK: } +func.func @broadcast_in_dim_general_case(%arg0: tensor<3x1x16xf32>) -> tensor<3x8x8x16xf32> { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 2, 3]> : tensor<3xi64>, name = "broadcast.0"}> : (tensor<3x1x16xf32>) -> tensor<3x8x8x16xf32> + func.return %0 : tensor<3x8x8x16xf32> +} + +// CHECK-LABEL: func @dynamic_broadcast_in_dim_tf_style( +// CHECK-SAME: %[[ARG_0:.*]]: tensor, +// CHECK-SAME: %[[ARG_1:.*]]: tensor<5xi32>) -> tensor { +// CHECK %[[VAL_0:.*]] = "tf.BroadcastTo"(%[[ARG_0]], %[[ARG_1]]) : (tensor, tensor<5xi32>) -> tensor +// CHECK return %[[VAL_0]] : tensor +func.func @dynamic_broadcast_in_dim_tf_style(%arg0: tensor, %arg1: tensor<5xi32>) -> tensor { + %0 = "mhlo.dynamic_broadcast_in_dim"(%arg0, %arg1) <{broadcast_dimensions = dense<[0, 1, 2, 3, 4]> : tensor<5xi64>}> : (tensor, tensor<5xi32>) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @dynamic_broadcast_in_dim_general_case_expand_back_dims( +// CHECK-SAME: %[[ARG_0:.*]]: tensor, +// CHECK-SAME: %[[ARG_1:.*]]: tensor<4xi32>) -> tensor { +// CHECK %[[CST_0:.*]] = "tf.Const"() <{value = dense<2> : tensor}> : () -> tensor +// CHECK %[[VAL_0:.*]] = "tf.ExpandDims"(%[[ARG_0]], %[[CST_0]]) : (tensor, tensor) -> tensor +// CHECK %[[CST_1:.*]] = "tf.Const"() <{value = dense<3> : tensor}> : () -> tensor +// CHECK %[[VAL_1:.*]] = "tf.ExpandDims"(%[[VAL_0]], %[[CST_1]]) : (tensor, tensor) -> tensor +// CHECK %[[VAL_2:.*]] = "tf.BroadcastTo"(%[[VAL_1]], %[[ARG_1]]) : (tensor, tensor<4xi32>) -> tensor +// CHECK return %[[VAL_2]] : tensor +func.func @dynamic_broadcast_in_dim_general_case_expand_back_dims(%arg0: tensor, %arg1: tensor<4xi32>) -> tensor { + %0 = "mhlo.dynamic_broadcast_in_dim"(%arg0, %arg1) <{broadcast_dimensions = dense<[0, 1]> : tensor<2xi64>}> : (tensor, tensor<4xi32>) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @dynamic_broadcast_in_dim_general_case_expand_middle_dim( +// CHECK-SAME: %[[ARG_0:.*]]: tensor, +// CHECK-SAME: %[[ARG_1:.*]]: tensor<4xi32>) -> tensor { +// CHECK %[[CST_0:.*]] = "tf.Const"() <{value = dense<2> : tensor}> : () -> tensor +// CHECK %[[VAL_0:.*]] = "tf.ExpandDims"(%[[ARG_0]], %[[CST_0]]) : (tensor, tensor) -> tensor +// CHECK %[[VAL_1:.*]] = "tf.BroadcastTo"(%[[VAL_0]], %[[ARG_1]]) : (tensor, tensor<4xi32>) -> tensor +// CHECK return %[[VAL_1]] : tensor +func.func @dynamic_broadcast_in_dim_general_case_expand_middle_dim(%arg0: tensor, %arg1: tensor<4xi32>) -> tensor { + %0 = "mhlo.dynamic_broadcast_in_dim"(%arg0, %arg1) <{broadcast_dimensions = dense<[0, 1, 3]> : tensor<3xi64>}> : (tensor, tensor<4xi32>) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @convert_dot_general( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<3x2x6x5x1xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<3x2x4x6xf32>) -> tensor<3x5x1x4xf32> { +// CHECK: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<[0, 3, 4, 1, 2]> : tensor<5xi64>}> : () -> tensor<5xi64> +// CHECK: %[[VAL_3:.*]] = "tf.Transpose"(%[[VAL_0]], %[[VAL_2]]) : (tensor<3x2x6x5x1xf32>, tensor<5xi64>) -> tensor<3x5x1x2x6xf32> +// CHECK: %[[VAL_4:.*]] = "tf.Const"() <{value = dense<[0, 1, 3, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_5:.*]] = "tf.Transpose"(%[[VAL_1]], %[[VAL_4]]) : (tensor<3x2x4x6xf32>, tensor<4xi64>) -> tensor<3x2x6x4xf32> +// CHECK: %[[VAL_6:.*]] = arith.constant dense<[3, 5, 12]> : tensor<3xi64> +// CHECK: %[[VAL_7:.*]] = "tf.Reshape"(%[[VAL_3]], %[[VAL_6]]) : (tensor<3x5x1x2x6xf32>, tensor<3xi64>) -> tensor<3x5x12xf32> +// CHECK: %[[VAL_8:.*]] = arith.constant dense<[3, 12, 4]> : tensor<3xi64> +// CHECK: %[[VAL_9:.*]] = "tf.Reshape"(%[[VAL_5]], %[[VAL_8]]) : (tensor<3x2x6x4xf32>, tensor<3xi64>) -> tensor<3x12x4xf32> +// CHECK: %[[VAL_10:.*]] = "tf.BatchMatMulV3"(%[[VAL_7]], %[[VAL_9]]) <{adj_x = false, adj_y = false, grad_x = false, grad_y = false}> : (tensor<3x5x12xf32>, tensor<3x12x4xf32>) -> tensor<3x5x4xf32> +// CHECK: %[[VAL_11:.*]] = arith.constant dense<[3, 5, 1, 4]> : tensor<4xi64> +// CHECK: %[[VAL_12:.*]] = "tf.Reshape"(%[[VAL_10]], %[[VAL_11]]) : (tensor<3x5x4xf32>, tensor<4xi64>) -> tensor<3x5x1x4xf32> +// CHECK: return %[[VAL_12]] : tensor<3x5x1x4xf32> +// CHECK: } +func.func @convert_dot_general(%arg0: tensor<3x2x6x5x1xf32>, %arg1: tensor<3x2x4x6xf32>) -> tensor<3x5x1x4xf32> { + %0 = "mhlo.dot_general"(%arg0, %arg1) { + dot_dimension_numbers = #mhlo.dot< + lhs_batching_dimensions = [0], + lhs_contracting_dimensions = [1, 2], + rhs_batching_dimensions = [0], + rhs_contracting_dimensions = [1, 3] + >, + precision_config = [#mhlo, #mhlo] + } : (tensor<3x2x6x5x1xf32>, tensor<3x2x4x6xf32>) -> tensor<3x5x1x4xf32> + func.return %0 : tensor<3x5x1x4xf32> +} + +// CHECK-LABEL: func @quantized_dot_general_not_converted +// CHECK: "mhlo.dot_general" +func.func @quantized_dot_general_not_converted(%arg0: tensor<1x1x512xf32>, %arg1: tensor<512x512x!quant.uniform>) -> tensor<1x1x512xf32> { + %0 = "mhlo.dot_general"(%arg0, %arg1) { + dot_dimension_numbers = #mhlo.dot< + lhs_contracting_dimensions = [2], + rhs_contracting_dimensions = [0] + >} : (tensor<1x1x512xf32>, tensor<512x512x!quant.uniform>) -> tensor<1x1x512xf32> + func.return %0 : tensor<1x1x512xf32> +} + +// CHECK-LABEL: func @convert_dot_general_repeated( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x1x1024xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1024x1024xf32>) -> tensor<1x1x1024xf32> { +// CHECK: %[[VAL_2:.*]] = arith.constant dense<[1, 1024]> : tensor<2xi64> +// CHECK: %[[VAL_3:.*]] = "tf.Reshape"(%[[VAL_0]], %[[VAL_2]]) : {{.*}} -> tensor<1x1024xf32> +// CHECK: %[[VAL_4:.*]] = "tf.BatchMatMulV3"(%[[VAL_3]], %[[VAL_1]]) <{adj_x = false, adj_y = false, grad_x = false, grad_y = false}> : {{.*}} -> tensor<1x1024xf32> +// CHECK: %[[VAL_5:.*]] = arith.constant dense<[1, 1, 1024]> : tensor<3xi64> +// CHECK: %[[VAL_6:.*]] = "tf.Reshape"(%[[VAL_4]], %[[VAL_5]]) : {{.*}} -> tensor<1x1x1024xf32> +// CHECK: return %[[VAL_6]] : tensor<1x1x1024xf32> +// CHECK: } +func.func @convert_dot_general_repeated(%arg0: tensor<1x1x1024xf32>, %arg1: tensor<1024x1024xf32>) -> tensor<1x1x1024xf32> { + %0 = "mhlo.dot_general"(%arg0, %arg1) { + dot_dimension_numbers = #mhlo.dot< + lhs_batching_dimensions = [], + lhs_contracting_dimensions = [2], + rhs_batching_dimensions = [], + rhs_contracting_dimensions = [0] + >, + precision_config = [#mhlo, #mhlo] + } : (tensor<1x1x1024xf32>, tensor<1024x1024xf32>) -> tensor<1x1x1024xf32> + func.return %0 : tensor<1x1x1024xf32> +} + +// CHECK-LABEL: func @convert_dot_general_int8( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<256xi8>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<256x8xi8>) -> tensor<8xi32> { +// CHECK: %[[VAL_2:.*]] = arith.constant dense<[1, 256]> : tensor<2xi64> +// CHECK: %[[VAL_3:.*]] = "tf.Reshape"(%[[VAL_0]], %[[VAL_2]]) : (tensor<256xi8>, tensor<2xi64>) -> tensor<1x256xi8> +// CHECK: %[[VAL_4:.*]] = "tf.BatchMatMulV3"(%[[VAL_3]], %[[VAL_1]]) <{adj_x = false, adj_y = false, grad_x = false, grad_y = false}> : (tensor<1x256xi8>, tensor<256x8xi8>) -> tensor<1x8xi32> +// CHECK: %[[VAL_5:.*]] = arith.constant dense<8> : tensor<1xi64> +// CHECK: %[[VAL_6:.*]] = "tf.Reshape"(%[[VAL_4]], %[[VAL_5]]) : (tensor<1x8xi32>, tensor<1xi64>) -> tensor<8xi32> +// CHECK: return %[[VAL_6]] : tensor<8xi32> +// CHECK: } +func.func @convert_dot_general_int8(%arg0: tensor<256xi8>, %arg1: tensor<256x8xi8>) -> tensor<8xi32> { + %0 = "mhlo.dot_general"(%arg0, %arg1) { + dot_dimension_numbers = #mhlo.dot< + lhs_contracting_dimensions = [0], + rhs_contracting_dimensions = [0]>, + precision_config = [#mhlo, #mhlo] + } : (tensor<256xi8>, tensor<256x8xi8>) -> tensor<8xi32> + func.return %0 : tensor<8xi32> +} + +// CHECK-LABEL: func @convert_dot_general_dynamic_rhs_out_dim( +// CHECK-SAME: %arg0: tensor<4x4x256xf32>, +// CHECK-SAME: %arg1: tensor<4x?x256xf32>) -> tensor<4x4x?xf32> { +// CHECK-DAG: %cst = "tf.Const"() <{value = dense<[0, 2, 1]> : tensor<3xi64>}> : () -> tensor<3xi64> +// CHECK: %0 = "tf.Transpose"(%arg1, %cst) : (tensor<4x?x256xf32>, tensor<3xi64>) -> tensor<4x256x?xf32> +// CHECK: %1 = "tf.Shape"(%arg1) : (tensor<4x?x256xf32>) -> tensor<3xi32> +// CHECK-DAG: %cst_0 = "tf.Const"() <{value = dense<[-1, 0, -1]> : tensor<3xi32>}> : () -> tensor<3xi32> +// CHECK-DAG: %cst_1 = "tf.Const"() <{value = dense<[-1, -1, 0]> : tensor<3xi32>}> : () -> tensor<3xi32> +// CHECK-DAG: %cst_2 = "tf.Const"() <{value = dense<1> : tensor}> : () -> tensor +// CHECK: %2 = "tf.UnsortedSegmentProd"(%1, %cst_0, %cst_2) : (tensor<3xi32>, tensor<3xi32>, tensor) -> tensor<1xi32> +// CHECK: %3 = "tf.UnsortedSegmentProd"(%1, %cst_1, %cst_2) : (tensor<3xi32>, tensor<3xi32>, tensor) -> tensor<1xi32> +// CHECK-DAG: %cst_3 = "tf.Const"() <{value = dense<4> : tensor<1xi32>}> : () -> tensor<1xi32> +// CHECK-DAG: %cst_4 = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK: %4 = "tf.Concat"(%cst_4, %cst_3, %3, %2) : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<3xi32> +// CHECK: %5 = "tf.Reshape"(%0, %4) : (tensor<4x256x?xf32>, tensor<3xi32>) -> tensor<4x256x?xf32> +// CHECK: %6 = "tf.BatchMatMulV3"(%arg0, %5) <{adj_x = false, adj_y = false, grad_x = false, grad_y = false}> : (tensor<4x4x256xf32>, tensor<4x256x?xf32>) -> tensor<4x4x?xf32> +// CHECK: %7 = "tf.Shape"(%arg0) : (tensor<4x4x256xf32>) -> tensor<3xi32> +// CHECK: %8 = "tf.Shape"(%arg1) : (tensor<4x?x256xf32>) -> tensor<3xi32> +// CHECK-DAG: %cst_5 = "tf.Const"() <{value = dense<[0, 1]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK: %9 = "tf.Gather"(%7, %cst_5) <{validate_indices = true}> : (tensor<3xi32>, tensor<2xi64>) -> tensor<2xi32> +// CHECK-DAG: %cst_6 = "tf.Const"() <{value = dense<1> : tensor<1xi64>}> : () -> tensor<1xi64> +// CHECK: %10 = "tf.Gather"(%8, %cst_6) <{validate_indices = true}> : (tensor<3xi32>, tensor<1xi64>) -> tensor<1xi32> +// CHECK-DAG: %cst_7 = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK: %11 = "tf.Concat"(%cst_7, %9, %10) : (tensor, tensor<2xi32>, tensor<1xi32>) -> tensor<3xi32> +// CHECK: %12 = "tf.Reshape"(%6, %11) : (tensor<4x4x?xf32>, tensor<3xi32>) -> tensor<4x4x?xf32> +// CHECK: return %12 : tensor<4x4x?xf32> +// CHECK: } +func.func @convert_dot_general_dynamic_rhs_out_dim(%arg0: tensor<4x4x256xf32>, %arg1: tensor<4x?x256xf32>) -> tensor<4x4x?xf32> { +%0 = "mhlo.dot_general"(%arg0, %arg1) { + dot_dimension_numbers = #mhlo.dot< + lhs_batching_dimensions = [0], + rhs_batching_dimensions = [0], + lhs_contracting_dimensions = [2], + rhs_contracting_dimensions = [2] + >} : (tensor<4x4x256xf32>, tensor<4x?x256xf32>) -> tensor<4x4x?xf32> +func.return %0 : tensor<4x4x?xf32> +} + +// CHECK-LABEL: func @convert_dot_general_dynamic_batch_dim( +// CHECK-SAME: %arg0: tensor<2x?x2x3xf32>, +// CHECK-SAME: %arg1: tensor<2x?x4x3xf32>) -> tensor<2x?x2x4xf32> { +// CHECK-DAG: %cst = "tf.Const"() <{value = dense<[0, 1, 3, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %0 = "tf.Transpose"(%arg1, %cst) : (tensor<2x?x4x3xf32>, tensor<4xi64>) -> tensor<2x?x3x4xf32> +// CHECK: %1 = "tf.Shape"(%arg0) : (tensor<2x?x2x3xf32>) -> tensor<4xi32> +// CHECK-DAG: %cst_0 = "tf.Const"() <{value = dense<[-1, -1, 0, -1]> : tensor<4xi32>}> : () -> tensor<4xi32> +// CHECK-DAG: %cst_1 = "tf.Const"() <{value = dense<[-1, -1, -1, 0]> : tensor<4xi32>}> : () -> tensor<4xi32> +// CHECK-DAG: %cst_2 = "tf.Const"() <{value = dense<1> : tensor}> : () -> tensor +// CHECK: %2 = "tf.UnsortedSegmentProd"(%1, %cst_0, %cst_2) : (tensor<4xi32>, tensor<4xi32>, tensor) -> tensor<1xi32> +// CHECK: %3 = "tf.UnsortedSegmentProd"(%1, %cst_1, %cst_2) : (tensor<4xi32>, tensor<4xi32>, tensor) -> tensor<1xi32> +// CHECK-DAG: %cst_3 = "tf.Const"() <{value = dense<[0, 1]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK: %4 = "tf.Gather"(%1, %cst_3) <{validate_indices = true}> : (tensor<4xi32>, tensor<2xi64>) -> tensor<2xi32> +// CHECK-DAG: %cst_4 = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK: %5 = "tf.Concat"(%cst_4, %4, %2, %3) : (tensor, tensor<2xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<4xi32> +// CHECK: %6 = "tf.Reshape"(%arg0, %5) : (tensor<2x?x2x3xf32>, tensor<4xi32>) -> tensor<2x?x2x3xf32> +// CHECK: %7 = "tf.Shape"(%arg1) : (tensor<2x?x4x3xf32>) -> tensor<4xi32> +// CHECK-DAG: %cst_5 = "tf.Const"() <{value = dense<[-1, -1, 0, -1]> : tensor<4xi32>}> : () -> tensor<4xi32> +// CHECK-DAG: %cst_6 = "tf.Const"() <{value = dense<[-1, -1, -1, 0]> : tensor<4xi32>}> : () -> tensor<4xi32> +// CHECK-DAG: %cst_7 = "tf.Const"() <{value = dense<1> : tensor}> : () -> tensor +// CHECK: %8 = "tf.UnsortedSegmentProd"(%7, %cst_5, %cst_7) : (tensor<4xi32>, tensor<4xi32>, tensor) -> tensor<1xi32> +// CHECK: %9 = "tf.UnsortedSegmentProd"(%7, %cst_6, %cst_7) : (tensor<4xi32>, tensor<4xi32>, tensor) -> tensor<1xi32> +// CHECK-DAG: %cst_8 = "tf.Const"() <{value = dense<[0, 1]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK: %10 = "tf.Gather"(%7, %cst_8) <{validate_indices = true}> : (tensor<4xi32>, tensor<2xi64>) -> tensor<2xi32> +// CHECK-DAG: %cst_9 = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK: %11 = "tf.Concat"(%cst_9, %10, %9, %8) : (tensor, tensor<2xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<4xi32> +// CHECK: %12 = "tf.Reshape"(%0, %11) : (tensor<2x?x3x4xf32>, tensor<4xi32>) -> tensor<2x?x3x4xf32> +// CHECK: %13 = "tf.BatchMatMulV3"(%6, %12) <{adj_x = false, adj_y = false, grad_x = false, grad_y = false}> : (tensor<2x?x2x3xf32>, tensor<2x?x3x4xf32>) -> tensor<2x?x2x4xf32> +// CHECK: %14 = "tf.Shape"(%arg0) : (tensor<2x?x2x3xf32>) -> tensor<4xi32> +// CHECK: %15 = "tf.Shape"(%arg1) : (tensor<2x?x4x3xf32>) -> tensor<4xi32> +// CHECK-DAG: %cst_10 = "tf.Const"() <{value = dense<[0, 1, 2]> : tensor<3xi64>}> : () -> tensor<3xi64> +// CHECK: %16 = "tf.Gather"(%14, %cst_10) <{validate_indices = true}> : (tensor<4xi32>, tensor<3xi64>) -> tensor<3xi32> +// CHECK: %cst_11 = "tf.Const"() <{value = dense<2> : tensor<1xi64>}> : () -> tensor<1xi64> +// CHECK: %17 = "tf.Gather"(%15, %cst_11) <{validate_indices = true}> : (tensor<4xi32>, tensor<1xi64>) -> tensor<1xi32> +// CHECK-DAG: %cst_12 = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK: %18 = "tf.Concat"(%cst_12, %16, %17) : (tensor, tensor<3xi32>, tensor<1xi32>) -> tensor<4xi32> +// CHECK: %19 = "tf.Reshape"(%13, %18) : (tensor<2x?x2x4xf32>, tensor<4xi32>) -> tensor<2x?x2x4xf32> +// CHECK: return %19 : tensor<2x?x2x4xf32> +// CHECK: } +func.func @convert_dot_general_dynamic_batch_dim(%arg0: tensor<2x?x2x3xf32>, %arg1: tensor<2x?x4x3xf32>) -> tensor<2x?x2x4xf32> { +%0 = "mhlo.dot_general"(%arg0, %arg1) { + dot_dimension_numbers = #mhlo.dot< + lhs_batching_dimensions = [0, 1], + rhs_batching_dimensions = [0, 1], + lhs_contracting_dimensions = [3], + rhs_contracting_dimensions = [3] + >} : (tensor<2x?x2x3xf32>, tensor<2x?x4x3xf32>) -> tensor<2x?x2x4xf32> +func.return %0 : tensor<2x?x2x4xf32> +} + +// CHECK-LABEL: func @convert_dot_general_dynamic_lhs_rhs_out_dims( +// CHECK-SAME: %arg0: tensor<2x2x?x3xf32>, +// CHECK-SAME: %arg1: tensor<2x4x?x3xf32>) -> tensor<2x2x?x4x?xf32> { +// CHECK-DAG: %cst = "tf.Const"() <{value = dense<[0, 3, 1, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %0 = "tf.Transpose"(%arg1, %cst) : (tensor<2x4x?x3xf32>, tensor<4xi64>) -> tensor<2x3x4x?xf32> +// CHECK: %1 = "tf.Shape"(%arg0) : (tensor<2x2x?x3xf32>) -> tensor<4xi32> +// CHECK-DAG: %cst_0 = "tf.Const"() <{value = dense<[-1, 0, 0, -1]> : tensor<4xi32>}> : () -> tensor<4xi32> +// CHECK-DAG: %cst_1 = "tf.Const"() <{value = dense<[-1, -1, -1, 0]> : tensor<4xi32>}> : () -> tensor<4xi32> +// CHECK-DAG: %cst_2 = "tf.Const"() <{value = dense<1> : tensor}> : () -> tensor +// CHECK: %2 = "tf.UnsortedSegmentProd"(%1, %cst_0, %cst_2) : (tensor<4xi32>, tensor<4xi32>, tensor) -> tensor<1xi32> +// CHECK: %3 = "tf.UnsortedSegmentProd"(%1, %cst_1, %cst_2) : (tensor<4xi32>, tensor<4xi32>, tensor) -> tensor<1xi32> +// CHECK-DAG: %cst_3 = "tf.Const"() <{value = dense<2> : tensor<1xi32>}> : () -> tensor<1xi32> +// CHECK-DAG: %cst_4 = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK: %4 = "tf.Concat"(%cst_4, %cst_3, %2, %3) : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<3xi32> +// CHECK: %5 = "tf.Reshape"(%arg0, %4) : (tensor<2x2x?x3xf32>, tensor<3xi32>) -> tensor<2x?x3xf32> +// CHECK: %6 = "tf.Shape"(%arg1) : (tensor<2x4x?x3xf32>) -> tensor<4xi32> +// CHECK-DAG: %cst_5 = "tf.Const"() <{value = dense<[-1, 0, 0, -1]> : tensor<4xi32>}> : () -> tensor<4xi32> +// CHECK-DAG: %cst_6 = "tf.Const"() <{value = dense<[-1, -1, -1, 0]> : tensor<4xi32>}> : () -> tensor<4xi32> +// CHECK-DAG: %cst_7 = "tf.Const"() <{value = dense<1> : tensor}> : () -> tensor +// CHECK: %7 = "tf.UnsortedSegmentProd"(%6, %cst_5, %cst_7) : (tensor<4xi32>, tensor<4xi32>, tensor) -> tensor<1xi32> +// CHECK: %8 = "tf.UnsortedSegmentProd"(%6, %cst_6, %cst_7) : (tensor<4xi32>, tensor<4xi32>, tensor) -> tensor<1xi32> +// CHECK-DAG: %cst_8 = "tf.Const"() <{value = dense<2> : tensor<1xi32>}> : () -> tensor<1xi32> +// CHECK-DAG: %cst_9 = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK: %9 = "tf.Concat"(%cst_9, %cst_8, %8, %7) : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<3xi32> +// CHECK: %10 = "tf.Reshape"(%0, %9) : (tensor<2x3x4x?xf32>, tensor<3xi32>) -> tensor<2x3x?xf32> +// CHECK: %11 = "tf.BatchMatMulV3"(%5, %10) <{adj_x = false, adj_y = false, grad_x = false, grad_y = false}> : (tensor<2x?x3xf32>, tensor<2x3x?xf32>) -> tensor<2x?x?xf32> +// CHECK: %12 = "tf.Shape"(%arg0) : (tensor<2x2x?x3xf32>) -> tensor<4xi32> +// CHECK: %13 = "tf.Shape"(%arg1) : (tensor<2x4x?x3xf32>) -> tensor<4xi32> +// CHECK-DAG: %cst_10 = "tf.Const"() <{value = dense<[0, 1, 2]> : tensor<3xi64>}> : () -> tensor<3xi64> +// CHECK: %14 = "tf.Gather"(%12, %cst_10) <{validate_indices = true}> : (tensor<4xi32>, tensor<3xi64>) -> tensor<3xi32> +// CHECK-DAG: %cst_11 = "tf.Const"() <{value = dense<[1, 2]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK: %15 = "tf.Gather"(%13, %cst_11) <{validate_indices = true}> : (tensor<4xi32>, tensor<2xi64>) -> tensor<2xi32> +// CHECK-DAG: %cst_12 = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK: %16 = "tf.Concat"(%cst_12, %14, %15) : (tensor, tensor<3xi32>, tensor<2xi32>) -> tensor<5xi32> +// CHECK: %17 = "tf.Reshape"(%11, %16) : (tensor<2x?x?xf32>, tensor<5xi32>) -> tensor<2x2x?x4x?xf32> +// CHECK: return %17 : tensor<2x2x?x4x?xf32> +// CHECK: } +func.func @convert_dot_general_dynamic_lhs_rhs_out_dims(%arg0: tensor<2x2x?x3xf32>, %arg1: tensor<2x4x?x3xf32>) -> tensor<2x2x?x4x?xf32> { +%0 = "mhlo.dot_general"(%arg0, %arg1) { + dot_dimension_numbers = #mhlo.dot< + lhs_batching_dimensions = [0], + rhs_batching_dimensions = [0], + lhs_contracting_dimensions = [3], + rhs_contracting_dimensions = [3] + >} : (tensor<2x2x?x3xf32>, tensor<2x4x?x3xf32>) -> tensor<2x2x?x4x?xf32> +func.return %0 : tensor<2x2x?x4x?xf32> +} + +// CHECK-LABEL: func @convert_dot_general_dynamic_contracting_dim( +// CHECK-SAME: %arg0: tensor<4x4x?xf32>, +// CHECK-SAME: %arg1: tensor<4x?x256xf32>) -> tensor<4x4x256xf32> { +// CHECK: %0 = "tf.Shape"(%arg0) : (tensor<4x4x?xf32>) -> tensor<3xi32> +// CHECK-DAG: %cst = "tf.Const"() <{value = dense<[-1, 0, -1]> : tensor<3xi32>}> : () -> tensor<3xi32> +// CHECK-DAG: %cst_0 = "tf.Const"() <{value = dense<[-1, -1, 0]> : tensor<3xi32>}> : () -> tensor<3xi32> +// CHECK-DAG: %cst_1 = "tf.Const"() <{value = dense<1> : tensor}> : () -> tensor +// CHECK: %1 = "tf.UnsortedSegmentProd"(%0, %cst, %cst_1) : (tensor<3xi32>, tensor<3xi32>, tensor) -> tensor<1xi32> +// CHECK: %2 = "tf.UnsortedSegmentProd"(%0, %cst_0, %cst_1) : (tensor<3xi32>, tensor<3xi32>, tensor) -> tensor<1xi32> +// CHECK-DAG: %cst_2 = "tf.Const"() <{value = dense<4> : tensor<1xi32>}> : () -> tensor<1xi32> +// CHECK-DAG: %cst_3 = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK: %3 = "tf.Concat"(%cst_3, %cst_2, %1, %2) : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<3xi32> +// CHECK: %4 = "tf.Reshape"(%arg0, %3) : (tensor<4x4x?xf32>, tensor<3xi32>) -> tensor<4x4x?xf32> +// CHECK: %5 = "tf.Shape"(%arg1) : (tensor<4x?x256xf32>) -> tensor<3xi32> +// CHECK-DAG: %cst_4 = "tf.Const"() <{value = dense<[-1, -1, 0]> : tensor<3xi32>}> : () -> tensor<3xi32> +// CHECK-DAG: %cst_5 = "tf.Const"() <{value = dense<[-1, 0, -1]> : tensor<3xi32>}> : () -> tensor<3xi32> +// CHECK-DAG: %cst_6 = "tf.Const"() <{value = dense<1> : tensor}> : () -> tensor +// CHECK: %6 = "tf.UnsortedSegmentProd"(%5, %cst_4, %cst_6) : (tensor<3xi32>, tensor<3xi32>, tensor) -> tensor<1xi32> +// CHECK: %7 = "tf.UnsortedSegmentProd"(%5, %cst_5, %cst_6) : (tensor<3xi32>, tensor<3xi32>, tensor) -> tensor<1xi32> +// CHECK-DAG: %cst_7 = "tf.Const"() <{value = dense<4> : tensor<1xi32>}> : () -> tensor<1xi32> +// CHECK-DAG: %cst_8 = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK: %8 = "tf.Concat"(%cst_8, %cst_7, %7, %6) : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<3xi32> +// CHECK: %9 = "tf.Reshape"(%arg1, %8) : (tensor<4x?x256xf32>, tensor<3xi32>) -> tensor<4x?x256xf32> +// CHECK: %10 = "tf.BatchMatMulV3"(%4, %9) <{adj_x = false, adj_y = false, grad_x = false, grad_y = false}> : (tensor<4x4x?xf32>, tensor<4x?x256xf32>) -> tensor<4x4x256xf32> +// CHECK: return %10 : tensor<4x4x256xf32> +// CHECK: } +func.func @convert_dot_general_dynamic_contracting_dim(%arg0: tensor<4x4x?xf32>, %arg1: tensor<4x?x256xf32>) -> tensor<4x4x256xf32> { +%0 = "mhlo.dot_general"(%arg0, %arg1) { + dot_dimension_numbers = #mhlo.dot< + lhs_batching_dimensions = [0], + rhs_batching_dimensions = [0], + lhs_contracting_dimensions = [2], + rhs_contracting_dimensions = [1] + >} : (tensor<4x4x?xf32>, tensor<4x?x256xf32>) -> tensor<4x4x256xf32> +func.return %0 : tensor<4x4x256xf32> +} + + + +// CHECK-LABEL: func.func @convert_conv1d( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<16x32x256xbf16>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x256x256xbf16>) -> tensor<16x32x256xbf16> { +// CHECK-DAG: %[[VAL_2:.*]] = arith.constant dense<[16, 32, 256, 1]> : tensor<4xi64> +// CHECK: %[[VAL_3:.*]] = "tf.Reshape"(%[[VAL_0]], %[[VAL_2]]) : (tensor<16x32x256xbf16>, tensor<4xi64>) -> tensor<16x32x256x1xbf16> +// CHECK-DAG: %[[VAL_4:.*]] = "tf.Const"() <{value = dense<[0, 1, 3, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_5:.*]] = "tf.Transpose"(%[[VAL_3]], %[[VAL_4]]) : (tensor<16x32x256x1xbf16>, tensor<4xi64>) -> tensor<16x32x1x256xbf16> +// CHECK-DAG: %[[VAL_6:.*]] = arith.constant dense<[1, 256, 256, 1]> : tensor<4xi64> +// CHECK: %[[VAL_7:.*]] = "tf.Reshape"(%[[VAL_1]], %[[VAL_6]]) : (tensor<1x256x256xbf16>, tensor<4xi64>) -> tensor<1x256x256x1xbf16> +// CHECK-DAG: %[[VAL_8:.*]] = "tf.Const"() <{value = dense<[0, 3, 1, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_9:.*]] = "tf.Transpose"(%[[VAL_7]], %[[VAL_8]]) : (tensor<1x256x256x1xbf16>, tensor<4xi64>) -> tensor<1x1x256x256xbf16> +// CHECK: %[[VAL_10:.*]] = "tf.Conv2D"(%[[VAL_5]], %[[VAL_9]]) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "VALID", strides = [1, 1, 1, 1], use_cudnn_on_gpu = true}> : (tensor<16x32x1x256xbf16>, tensor<1x1x256x256xbf16>) -> tensor<16x32x1x256xbf16> +// CHECK: %[[VAL_11:.*]] = "tf.Const"() <{value = dense<[0, 1, 3, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_12:.*]] = "tf.Transpose"(%[[VAL_10]], %[[VAL_11]]) : (tensor<16x32x1x256xbf16>, tensor<4xi64>) -> tensor<16x32x256x1xbf16> +// CHECK: %[[VAL_13:.*]] = arith.constant dense<[16, 32, 256]> : tensor<3xi64> +// CHECK: %[[VAL_14:.*]] = "tf.Reshape"(%[[VAL_12]], %[[VAL_13]]) : (tensor<16x32x256x1xbf16>, tensor<3xi64>) -> tensor<16x32x256xbf16> +// CHECK: return %[[VAL_14]] : tensor<16x32x256xbf16> +// CHECK: } +func.func @convert_conv1d(%arg0: tensor<16x32x256xbf16>, %arg1: tensor<1x256x256xbf16>) -> tensor<16x32x256xbf16> { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, f]x[0, i, o]->[b, 0, f]>, + feature_group_count = 1 : i64, + lhs_dilation = dense<1> : tensor<1xi64>, + padding = dense<0> : tensor<1x2xi64>, + precision_config = [#mhlo, #mhlo], + rhs_dilation = dense<1> : tensor<1xi64>, + window_strides = dense<1> : tensor<1xi64> + } : (tensor<16x32x256xbf16>, tensor<1x256x256xbf16>) -> tensor<16x32x256xbf16> + func.return %0 : tensor<16x32x256xbf16> +} + +// CHECK-LABEL: func.func @convert_conv1d_dynamic_batch( +// CHECK-SAME: %[[VAL_0:.*]]: tensor, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x256x256xbf16>) -> tensor { +// CHECK-DAG: %[[VAL_2:.*]] = arith.constant dense<[-9223372036854775808, 32, 256, 1]> : tensor<4xi64> +// CHECK: %[[VAL_3:.*]] = "tf.Reshape"(%[[VAL_0]], %[[VAL_2]]) : (tensor, tensor<4xi64>) -> tensor +// CHECK-DAG: %[[VAL_4:.*]] = "tf.Const"() <{value = dense<[0, 1, 3, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_5:.*]] = "tf.Transpose"(%[[VAL_3]], %[[VAL_4]]) : (tensor, tensor<4xi64>) -> tensor +// CHECK-DAG: %[[VAL_6:.*]] = arith.constant dense<[1, 256, 256, 1]> : tensor<4xi64> +// CHECK: %[[VAL_7:.*]] = "tf.Reshape"(%[[VAL_1]], %[[VAL_6]]) : (tensor<1x256x256xbf16>, tensor<4xi64>) -> tensor<1x256x256x1xbf16> +// CHECK-DAG: %[[VAL_8:.*]] = "tf.Const"() <{value = dense<[0, 3, 1, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_9:.*]] = "tf.Transpose"(%[[VAL_7]], %[[VAL_8]]) : (tensor<1x256x256x1xbf16>, tensor<4xi64>) -> tensor<1x1x256x256xbf16> +// CHECK: %[[VAL_10:.*]] = "tf.Conv2D"(%[[VAL_5]], %[[VAL_9]]) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "VALID", strides = [1, 1, 1, 1], use_cudnn_on_gpu = true}> : (tensor, tensor<1x1x256x256xbf16>) -> tensor +// CHECK: %[[VAL_11:.*]] = "tf.Const"() <{value = dense<[0, 1, 3, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_12:.*]] = "tf.Transpose"(%[[VAL_10]], %[[VAL_11]]) : (tensor, tensor<4xi64>) -> tensor +// CHECK: %[[VAL_13:.*]] = arith.constant dense<[-9223372036854775808, 32, 256]> : tensor<3xi64> +// CHECK: %[[VAL_14:.*]] = "tf.Reshape"(%[[VAL_12]], %[[VAL_13]]) : (tensor, tensor<3xi64>) -> tensor +// CHECK: return %[[VAL_14]] : tensor +// CHECK: } +func.func @convert_conv1d_dynamic_batch(%arg0: tensor, %arg1: tensor<1x256x256xbf16>) -> tensor { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, f]x[0, i, o]->[b, 0, f]>, + feature_group_count = 1 : i64, + lhs_dilation = dense<1> : tensor<1xi64>, + padding = dense<0> : tensor<1x2xi64>, + precision_config = [#mhlo, #mhlo], + rhs_dilation = dense<1> : tensor<1xi64>, + window_strides = dense<1> : tensor<1xi64> + } : (tensor, tensor<1x256x256xbf16>) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: convert_dynamic_1d_group_conv +func.func private @convert_dynamic_1d_group_conv(%arg1: tensor, %arg2: tensor<768x48x128xf32>) -> (tensor) { + %0 = mhlo.convolution(%arg1, %arg2) + dim_numbers = [b, f, 0]x[o, i, 0]->[b, f, 0], + window = {pad = [[64, 64]]} + {batch_group_count = 1 : i64, feature_group_count = 16 : i64} + : (tensor, tensor<768x48x128xf32>) -> tensor + return %0 : tensor +// CHECK: %cst = arith.constant dense<[-9223372036854775808, 768, 2, 1]> : tensor<4xi64> +// CHECK: %0 = "tf.Reshape"(%arg0, %cst) : (tensor, tensor<4xi64>) -> tensor +// CHECK: %cst_0 = "tf.Const"() <{value = dense<[0, 2, 3, 1]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %1 = "tf.Transpose"(%0, %cst_0) : (tensor, tensor<4xi64>) -> tensor +// CHECK: %cst_1 = arith.constant dense<[768, 48, 128, 1]> : tensor<4xi64> +// CHECK: %2 = "tf.Reshape"(%arg1, %cst_1) : (tensor<768x48x128xf32>, tensor<4xi64>) -> tensor<768x48x128x1xf32> +// CHECK: %cst_2 = "tf.Const"() <{value = dense<[2, 3, 1, 0]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %3 = "tf.Transpose"(%2, %cst_2) : (tensor<768x48x128x1xf32>, tensor<4xi64>) -> tensor<128x1x48x768xf32> +// CHECK: %4 = "tf.Conv2D"(%1, %3) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [0, 0, 64, 64, 0, 0, 0, 0], padding = "EXPLICIT", strides = [1, 1, 1, 1], use_cudnn_on_gpu = true}> : (tensor, tensor<128x1x48x768xf32>) -> tensor +// CHECK: %cst_3 = "tf.Const"() <{value = dense<[0, 3, 1, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %5 = "tf.Transpose"(%4, %cst_3) : (tensor, tensor<4xi64>) -> tensor +// CHECK: %cst_4 = arith.constant dense<[-9223372036854775808, 768, 3]> : tensor<3xi64> +// CHECK: %6 = "tf.Reshape"(%5, %cst_4) : (tensor, tensor<3xi64>) -> tensor +// CHECK: return %6 : tensor +} + +// CHECK-LABEL: func.func @convert_conv1d_no_lhs_dil_rhs_dil_precision_conf( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<16x32x256xbf16>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x256x256xbf16>) -> tensor<16x32x256xbf16> { +// CHECK-DAG: %[[VAL_2:.*]] = arith.constant dense<[16, 32, 256, 1]> : tensor<4xi64> +// CHECK: %[[VAL_3:.*]] = "tf.Reshape"(%[[VAL_0]], %[[VAL_2]]) : (tensor<16x32x256xbf16>, tensor<4xi64>) -> tensor<16x32x256x1xbf16> +// CHECK-DAG: %[[VAL_4:.*]] = "tf.Const"() <{value = dense<[0, 1, 3, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_5:.*]] = "tf.Transpose"(%[[VAL_3]], %[[VAL_4]]) : (tensor<16x32x256x1xbf16>, tensor<4xi64>) -> tensor<16x32x1x256xbf16> +// CHECK-DAG: %[[VAL_6:.*]] = arith.constant dense<[1, 256, 256, 1]> : tensor<4xi64> +// CHECK: %[[VAL_7:.*]] = "tf.Reshape"(%[[VAL_1]], %[[VAL_6]]) : (tensor<1x256x256xbf16>, tensor<4xi64>) -> tensor<1x256x256x1xbf16> +// CHECK-DAG: %[[VAL_8:.*]] = "tf.Const"() <{value = dense<[0, 3, 1, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_9:.*]] = "tf.Transpose"(%[[VAL_7]], %[[VAL_8]]) : (tensor<1x256x256x1xbf16>, tensor<4xi64>) -> tensor<1x1x256x256xbf16> +// CHECK: %[[VAL_10:.*]] = "tf.Conv2D"(%[[VAL_5]], %[[VAL_9]]) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "VALID", strides = [1, 1, 1, 1], use_cudnn_on_gpu = true}> : (tensor<16x32x1x256xbf16>, tensor<1x1x256x256xbf16>) -> tensor<16x32x1x256xbf16> +// CHECK: %[[VAL_11:.*]] = "tf.Const"() <{value = dense<[0, 1, 3, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_12:.*]] = "tf.Transpose"(%[[VAL_10]], %[[VAL_11]]) : (tensor<16x32x1x256xbf16>, tensor<4xi64>) -> tensor<16x32x256x1xbf16> +// CHECK: %[[VAL_13:.*]] = arith.constant dense<[16, 32, 256]> : tensor<3xi64> +// CHECK: %[[VAL_14:.*]] = "tf.Reshape"(%[[VAL_12]], %[[VAL_13]]) : (tensor<16x32x256x1xbf16>, tensor<3xi64>) -> tensor<16x32x256xbf16> +// CHECK: return %[[VAL_14]] : tensor<16x32x256xbf16> +// CHECK: } +func.func @convert_conv1d_no_lhs_dil_rhs_dil_precision_conf(%arg0: tensor<16x32x256xbf16>, %arg1: tensor<1x256x256xbf16>) -> tensor<16x32x256xbf16> { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, f]x[0, i, o]->[b, 0, f]>, + feature_group_count = 1 : i64, + padding = dense<0> : tensor<1x2xi64>, + window_strides = dense<1> : tensor<1xi64> + } : (tensor<16x32x256xbf16>, tensor<1x256x256xbf16>) -> tensor<16x32x256xbf16> + func.return %0 : tensor<16x32x256xbf16> +} + +// CHECK-LABEL: func.func @convert_conv1d_no_lhs_dil_rhs_dil_precision_conf_dynamic_batch( +// CHECK-SAME: %[[VAL_0:.*]]: tensor, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x256x256xbf16>) -> tensor { +// CHECK-DAG: %[[VAL_2:.*]] = arith.constant dense<[-9223372036854775808, 32, 256, 1]> : tensor<4xi64> +// CHECK: %[[VAL_3:.*]] = "tf.Reshape"(%[[VAL_0]], %[[VAL_2]]) : (tensor, tensor<4xi64>) -> tensor +// CHECK-DAG: %[[VAL_4:.*]] = "tf.Const"() <{value = dense<[0, 1, 3, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_5:.*]] = "tf.Transpose"(%[[VAL_3]], %[[VAL_4]]) : (tensor, tensor<4xi64>) -> tensor +// CHECK-DAG: %[[VAL_6:.*]] = arith.constant dense<[1, 256, 256, 1]> : tensor<4xi64> +// CHECK: %[[VAL_7:.*]] = "tf.Reshape"(%[[VAL_1]], %[[VAL_6]]) : (tensor<1x256x256xbf16>, tensor<4xi64>) -> tensor<1x256x256x1xbf16> +// CHECK-DAG: %[[VAL_8:.*]] = "tf.Const"() <{value = dense<[0, 3, 1, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_9:.*]] = "tf.Transpose"(%[[VAL_7]], %[[VAL_8]]) : (tensor<1x256x256x1xbf16>, tensor<4xi64>) -> tensor<1x1x256x256xbf16> +// CHECK: %[[VAL_10:.*]] = "tf.Conv2D"(%[[VAL_5]], %[[VAL_9]]) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "VALID", strides = [1, 1, 1, 1], use_cudnn_on_gpu = true}> : (tensor, tensor<1x1x256x256xbf16>) -> tensor +// CHECK: %[[VAL_11:.*]] = "tf.Const"() <{value = dense<[0, 1, 3, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_12:.*]] = "tf.Transpose"(%[[VAL_10]], %[[VAL_11]]) : (tensor, tensor<4xi64>) -> tensor +// CHECK: %[[VAL_13:.*]] = arith.constant dense<[-9223372036854775808, 32, 256]> : tensor<3xi64> +// CHECK: %[[VAL_14:.*]] = "tf.Reshape"(%[[VAL_12]], %[[VAL_13]]) : (tensor, tensor<3xi64>) -> tensor +// CHECK: return %[[VAL_14]] : tensor +// CHECK: } +func.func @convert_conv1d_no_lhs_dil_rhs_dil_precision_conf_dynamic_batch(%arg0: tensor, %arg1: tensor<1x256x256xbf16>) -> tensor { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, f]x[0, i, o]->[b, 0, f]>, + feature_group_count = 1 : i64, + padding = dense<0> : tensor<1x2xi64>, + window_strides = dense<1> : tensor<1xi64> + } : (tensor, tensor<1x256x256xbf16>) -> tensor + func.return %0 : tensor +} + + + +// CHECK-LABEL: func.func @convert_conv1d_non_canonical_dimension_numbers( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<32x16x256xbf16>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<256x1x256xbf16>) -> tensor<256x16x32xbf16> { +// CHECK-DAG: %[[VAL_2:.*]] = arith.constant dense<[32, 16, 256, 1]> : tensor<4xi64> +// CHECK: %[[VAL_3:.*]] = "tf.Reshape"(%[[VAL_0]], %[[VAL_2]]) : (tensor<32x16x256xbf16>, tensor<4xi64>) -> tensor<32x16x256x1xbf16> +// CHECK-DAG: %[[VAL_4:.*]] = "tf.Const"() <{value = dense<[1, 0, 3, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_5:.*]] = "tf.Transpose"(%[[VAL_3]], %[[VAL_4]]) : (tensor<32x16x256x1xbf16>, tensor<4xi64>) -> tensor<16x32x1x256xbf16> +// CHECK: %[[VAL_6:.*]] = arith.constant dense<[256, 1, 256, 1]> : tensor<4xi64> +// CHECK: %[[VAL_7:.*]] = "tf.Reshape"(%[[VAL_1]], %[[VAL_6]]) : (tensor<256x1x256xbf16>, tensor<4xi64>) -> tensor<256x1x256x1xbf16> +// CHECK-DAG: %[[VAL_8:.*]] = "tf.Const"() <{value = dense<[1, 3, 2, 0]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_9:.*]] = "tf.Transpose"(%[[VAL_7]], %[[VAL_8]]) : (tensor<256x1x256x1xbf16>, tensor<4xi64>) -> tensor<1x1x256x256xbf16> +// CHECK: %[[VAL_10:.*]] = "tf.Conv2D"(%[[VAL_5]], %[[VAL_9]]) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "VALID", strides = [1, 1, 1, 1], use_cudnn_on_gpu = true}> : (tensor<16x32x1x256xbf16>, tensor<1x1x256x256xbf16>) -> tensor<16x32x1x256xbf16> +// CHECK-DAG: %[[VAL_11:.*]] = "tf.Const"() <{value = dense<[3, 0, 1, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_12:.*]] = "tf.Transpose"(%[[VAL_10]], %[[VAL_11]]) : (tensor<16x32x1x256xbf16>, tensor<4xi64>) -> tensor<256x16x32x1xbf16> +// CHECK: %[[VAL_13:.*]] = arith.constant dense<[256, 16, 32]> : tensor<3xi64> +// CHECK: %[[VAL_14:.*]] = "tf.Reshape"(%[[VAL_12]], %[[VAL_13]]) : (tensor<256x16x32x1xbf16>, tensor<3xi64>) -> tensor<256x16x32xbf16> +// CHECK: return %[[VAL_14]] : tensor<256x16x32xbf16> +// CHECK: } +func.func @convert_conv1d_non_canonical_dimension_numbers(%arg0: tensor<32x16x256xbf16>, %arg1: tensor<256x1x256xbf16>) -> tensor<256x16x32xbf16> { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[0, b, f]x[o, 0, i]->[f, b, 0]>, + feature_group_count = 1 : i64, + lhs_dilation = dense<1> : tensor<1xi64>, + padding = dense<0> : tensor<1x2xi64>, + precision_config = [#mhlo, #mhlo], + rhs_dilation = dense<1> : tensor<1xi64>, + window_strides = dense<1> : tensor<1xi64> + } : (tensor<32x16x256xbf16>, tensor<256x1x256xbf16>) -> tensor<256x16x32xbf16> + func.return %0 : tensor<256x16x32xbf16> +} + +// CHECK-LABEL: func.func @no_convert_conv1d_dynamic( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<16x?x256xbf16>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x256x256xbf16>) -> tensor<16x?x256xbf16> { +// CHECK: %[[VAL_2:.*]] = mhlo.convolution(%[[VAL_0]], %[[VAL_1]]) dim_numbers = [b, 0, f]x[0, i, o]->[b, 0, f], window = {stride = [1], pad = {{\[\[}}0, 0]], lhs_dilate = [1], rhs_dilate = [1]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64, precision_config = [#mhlo, #mhlo]} : (tensor<16x?x256xbf16>, tensor<1x256x256xbf16>) -> tensor<16x?x256xbf16> +// CHECK: return %[[VAL_2]] : tensor<16x?x256xbf16> +// CHECK: } +func.func @no_convert_conv1d_dynamic(%arg0: tensor<16x?x256xbf16>, %arg1: tensor<1x256x256xbf16>) -> tensor<16x?x256xbf16> { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, f]x[0, i, o]->[b, 0, f]>, + feature_group_count = 1 : i64, + lhs_dilation = dense<1> : tensor<1xi64>, + padding = dense<0> : tensor<1x2xi64>, + precision_config = [#mhlo, #mhlo], + rhs_dilation = dense<1> : tensor<1xi64>, + window_strides = dense<1> : tensor<1xi64> + } : (tensor<16x?x256xbf16>, tensor<1x256x256xbf16>) -> tensor<16x?x256xbf16> + func.return %0 : tensor<16x?x256xbf16> +} + +// CHECK-LABEL: func.func @convert_conv1d_feature_group_gt_1( +// CHECK: %cst = arith.constant dense<[16, 32, 256, 1]> : tensor<4xi64> +// CHECK: %0 = "tf.Reshape"(%arg0, %cst) : (tensor<16x32x256xbf16>, tensor<4xi64>) -> tensor<16x32x256x1xbf16> +// CHECK: %cst_0 = "tf.Const"() <{value = dense<[0, 1, 3, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %1 = "tf.Transpose"(%0, %cst_0) : (tensor<16x32x256x1xbf16>, tensor<4xi64>) -> tensor<16x32x1x256xbf16> +// CHECK: %cst_1 = arith.constant dense<[1, 128, 128, 1]> : tensor<4xi64> +// CHECK: %2 = "tf.Reshape"(%arg1, %cst_1) : (tensor<1x128x128xbf16>, tensor<4xi64>) -> tensor<1x128x128x1xbf16> +// CHECK: %cst_2 = "tf.Const"() <{value = dense<[0, 3, 1, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %3 = "tf.Transpose"(%2, %cst_2) : (tensor<1x128x128x1xbf16>, tensor<4xi64>) -> tensor<1x1x128x128xbf16> +// CHECK: %4 = "tf.Conv2D"(%1, %3) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "VALID", strides = [1, 1, 1, 1], use_cudnn_on_gpu = true}> : (tensor<16x32x1x256xbf16>, tensor<1x1x128x128xbf16>) -> tensor<16x32x1x128xbf16> +// CHECK: %cst_3 = "tf.Const"() <{value = dense<[0, 1, 3, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %5 = "tf.Transpose"(%4, %cst_3) : (tensor<16x32x1x128xbf16>, tensor<4xi64>) -> tensor<16x32x128x1xbf16> +// CHECK: %cst_4 = arith.constant dense<[16, 32, 128]> : tensor<3xi64> +// CHECK: %6 = "tf.Reshape"(%5, %cst_4) : (tensor<16x32x128x1xbf16>, tensor<3xi64>) -> tensor<16x32x128xbf16> +// CHECK: return %6 : tensor<16x32x128xbf16> +func.func @convert_conv1d_feature_group_gt_1(%arg0: tensor<16x32x256xbf16>, %arg1: tensor<1x128x128xbf16>) -> tensor<16x32x128xbf16> { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, f]x[0, i, o]->[b, 0, f]>, + feature_group_count = 2 : i64, + lhs_dilation = dense<1> : tensor<1xi64>, + padding = dense<0> : tensor<1x2xi64>, + precision_config = [#mhlo, #mhlo], + rhs_dilation = dense<1> : tensor<1xi64>, + window_strides = dense<1> : tensor<1xi64> + } : (tensor<16x32x256xbf16>, tensor<1x128x128xbf16>) -> tensor<16x32x128xbf16> + func.return %0 : tensor<16x32x128xbf16> +} + +// CHECK-LABEL: func.func @convert_conv1d_missing_windows_strides_fallback( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<16x32x256xbf16>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x256x256xbf16>) -> tensor<16x32x256xbf16> { +// CHECK-DAG: %[[VAL_2:.*]] = arith.constant dense<[16, 32, 256, 1]> : tensor<4xi64> +// CHECK: %[[VAL_3:.*]] = "tf.Reshape"(%[[VAL_0]], %[[VAL_2]]) : (tensor<16x32x256xbf16>, tensor<4xi64>) -> tensor<16x32x256x1xbf16> +// CHECK-DAG: %[[VAL_4:.*]] = "tf.Const"() <{value = dense<[0, 1, 3, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_5:.*]] = "tf.Transpose"(%[[VAL_3]], %[[VAL_4]]) : (tensor<16x32x256x1xbf16>, tensor<4xi64>) -> tensor<16x32x1x256xbf16> +// CHECK-DAG: %[[VAL_6:.*]] = arith.constant dense<[1, 256, 256, 1]> : tensor<4xi64> +// CHECK: %[[VAL_7:.*]] = "tf.Reshape"(%[[VAL_1]], %[[VAL_6]]) : (tensor<1x256x256xbf16>, tensor<4xi64>) -> tensor<1x256x256x1xbf16> +// CHECK-DAG: %[[VAL_8:.*]] = "tf.Const"() <{value = dense<[0, 3, 1, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_9:.*]] = "tf.Transpose"(%[[VAL_7]], %[[VAL_8]]) : (tensor<1x256x256x1xbf16>, tensor<4xi64>) -> tensor<1x1x256x256xbf16> +// CHECK: %[[VAL_10:.*]] = "tf.Conv2D"(%[[VAL_5]], %[[VAL_9]]) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "VALID", strides = [1, 1, 1, 1], use_cudnn_on_gpu = true}> : (tensor<16x32x1x256xbf16>, tensor<1x1x256x256xbf16>) -> tensor<16x32x1x256xbf16> +// CHECK: %[[VAL_11:.*]] = "tf.Const"() <{value = dense<[0, 1, 3, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_12:.*]] = "tf.Transpose"(%[[VAL_10]], %[[VAL_11]]) : (tensor<16x32x1x256xbf16>, tensor<4xi64>) -> tensor<16x32x256x1xbf16> +// CHECK: %[[VAL_13:.*]] = arith.constant dense<[16, 32, 256]> : tensor<3xi64> +// CHECK: %[[VAL_14:.*]] = "tf.Reshape"(%[[VAL_12]], %[[VAL_13]]) : (tensor<16x32x256x1xbf16>, tensor<3xi64>) -> tensor<16x32x256xbf16> +// CHECK: return %[[VAL_14]] : tensor<16x32x256xbf16> +// CHECK: } +func.func @convert_conv1d_missing_windows_strides_fallback(%arg0: tensor<16x32x256xbf16>, %arg1: tensor<1x256x256xbf16>) -> tensor<16x32x256xbf16> { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, f]x[0, i, o]->[b, 0, f]>, + feature_group_count = 1 : i64, + lhs_dilation = dense<1> : tensor<1xi64>, + padding = dense<0> : tensor<1x2xi64>, + precision_config = [#mhlo, #mhlo], + rhs_dilation = dense<1> : tensor<1xi64> + } : (tensor<16x32x256xbf16>, tensor<1x256x256xbf16>) -> tensor<16x32x256xbf16> + func.return %0 : tensor<16x32x256xbf16> +} + +// CHECK-LABEL: func.func @convert_conv1d_missing_windows_strides_fallback_2( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x64x64x4xbf16>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<3x3x4x320xbf16>) -> tensor<1x62x62x320xbf16> { +// CHECK: %[[VAL_2:.*]] = "tf.Conv2D"(%[[VAL_0]], %[[VAL_1]]) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "VALID", strides = [1, 1, 1, 1], use_cudnn_on_gpu = true}> : (tensor<1x64x64x4xbf16>, tensor<3x3x4x320xbf16>) -> tensor<1x62x62x320xbf16> +// CHECK: return %[[VAL_2]] : tensor<1x62x62x320xbf16> +// CHECK: } +func.func @convert_conv1d_missing_windows_strides_fallback_2(%arg0: tensor<1x64x64x4xbf16>, %arg1: tensor<3x3x4x320xbf16>) -> tensor<1x62x62x320xbf16> { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f]>, + feature_group_count = 1 : i64, + lhs_dilation = dense<[1, 1]> : tensor<2xi64>, + padding = dense<0> : tensor<2x2xi64>, + precision_config = [#mhlo, #mhlo], + rhs_dilation = dense<[1, 1]> : tensor<2xi64> + } : (tensor<1x64x64x4xbf16>, tensor<3x3x4x320xbf16>) -> tensor<1x62x62x320xbf16> + func.return %0 : tensor<1x62x62x320xbf16> +} + +// CHECK-LABEL: func @convert_conv2d( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x8x8x207xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<3x3x207x16xf32>) -> tensor<1x8x8x16xf32> { +// CHECK: %[[VAL_2:.*]] = "tf.Conv2D"(%[[VAL_0]], %[[VAL_1]]) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "SAME", strides = [1, 1, 1, 1], use_cudnn_on_gpu = true}> : (tensor<1x8x8x207xf32>, tensor<3x3x207x16xf32>) -> tensor<1x8x8x16xf32> +// CHECK: return %[[VAL_2]] : tensor<1x8x8x16xf32> +// CHECK: } +func.func @convert_conv2d(%arg0: tensor<1x8x8x207xf32>, %arg1: tensor<3x3x207x16xf32>) -> tensor<1x8x8x16xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) {batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv, feature_group_count = 1 : i64, lhs_dilation = dense<1> : tensor<2xi64>, padding = dense<1> : tensor<2x2xi64>, precision_config = [#mhlo, #mhlo], rhs_dilation = dense<1> : tensor<2xi64>, window_strides = dense<1> : tensor<2xi64>} : + (tensor<1x8x8x207xf32>, tensor<3x3x207x16xf32>) -> tensor<1x8x8x16xf32> + func.return %0 : tensor<1x8x8x16xf32> +} + +// CHECK-LABEL: func @convert_group_conv2d( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x14x14x2240xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<3x3x112x2240xf32>) -> tensor<1x7x7x2240xf32> { +// CHECK: %[[VAL_2:.*]] = "tf.Conv2D"(%[[VAL_0]], %[[VAL_1]]) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [0, 0, 1, 1, 1, 1, 0, 0], padding = "EXPLICIT", strides = [1, 2, 2, 1], use_cudnn_on_gpu = true}> : (tensor<1x14x14x2240xf32>, tensor<3x3x112x2240xf32>) -> tensor<1x7x7x2240xf32> +// CHECk: return %[[VAL_2]] : tensor<1x7x7x2240xf32> +// CHECK: } +func.func @convert_group_conv2d(%arg0: tensor<1x14x14x2240xf32>, %arg1: tensor<3x3x112x2240xf32>) -> tensor<1x7x7x2240xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) {batch_group_count = 1 :i64, + dimension_numbers = #mhlo.conv<[b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f]>, + feature_group_count = 20 : i64, lhs_dilation = dense<1> : tensor<2xi64>, padding = dense<1> : tensor<2x2xi64>, precision_config = [#mhlo, #mhlo], rhs_dilation = dense<1> : tensor<2xi64>, window_reversal = dense : tensor<2xi1>, window_strides = dense<2> : tensor<2xi64>} : + (tensor<1x14x14x2240xf32>, tensor<3x3x112x2240xf32>) -> tensor<1x7x7x2240xf32> + func.return %0 : tensor<1x7x7x2240xf32> +} + +// CHECK-LABEL: func.func @convert_transpose_conv_with_transpose( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x256x64x64xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<2x2x64x256xf32>) -> tensor<1x64x128x128xf32> { +// CHECK: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<[0, 2, 3, 1]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_3:.*]] = "tf.Transpose"(%[[VAL_0:.*]], %[[VAL_2:.*]]) : (tensor<1x256x64x64xf32>, tensor<4xi64>) -> tensor<1x64x64x256xf32> +// CHECK: %[[VAL_4:.*]] = "tf.Const"() <{value = dense<[0, 1]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK: %[[VAL_5:.*]] = "tf.ReverseV2"(%[[VAL_1:.*]], %[[VAL_4:.*]]) : (tensor<2x2x64x256xf32>, tensor<2xi64>) -> tensor<2x2x64x256xf32> +// CHECK: %[[VAL_6:.*]] = "tf.Const"() <{value = dense<[1, 128, 128, 64]> : tensor<4xi32>}> : () -> tensor<4xi32> +// CHECK: %[[VAL_7:.*]] = "tf.Conv2DBackpropInput"(%[[VAL_6:.*]], %[[VAL_5:.*]], %[[VAL_3:.*]]) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "VALID", strides = [1, 2, 2, 1], use_cudnn_on_gpu = true}> : (tensor<4xi32>, tensor<2x2x64x256xf32>, tensor<1x64x64x256xf32>) -> tensor<1x128x128x64xf32> +// CHECK: %[[VAL_8:.*]] = "tf.Const"() <{value = dense<[0, 3, 1, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_9:.*]] = "tf.Transpose"(%[[VAL_7:.*]], %[[VAL_8:.*]]) : (tensor<1x128x128x64xf32>, tensor<4xi64>) -> tensor<1x64x128x128xf32> +// CHECK: return %[[VAL_9:.*]] : tensor<1x64x128x128xf32> +// CHECK: } + +func.func @convert_transpose_conv_with_transpose(%arg0: tensor<1x256x64x64xf32>, %arg1: tensor<2x2x64x256xf32>) -> tensor<1x64x128x128xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) {batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, f, 0, 1]x[0, 1, o, i]->[b, f, 0, 1]>, + feature_group_count = 1 : i64, lhs_dilation = dense<2> : tensor<2xi64>, padding = dense<1> : tensor<2x2xi64>, precision_config = [#mhlo, #mhlo], rhs_dilation = dense<1> : tensor<2xi64>, window_reversal = dense : tensor<2xi1>, window_strides = dense<1> : tensor<2xi64>} : + (tensor<1x256x64x64xf32>, tensor<2x2x64x256xf32>) -> tensor<1x64x128x128xf32> + func.return %0 : tensor<1x64x128x128xf32> +} + +// CHECK-LABEL: func.func @convert_transpose_conv_with_transpose2( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<64x64x1x256xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<2x2x64x256xf32>) -> tensor<128x128x1x64xf32> { +// CHECK: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<[2, 0, 1, 3]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_3:.*]] = "tf.Transpose"(%[[VAL_0:.*]], %[[VAL_2:.*]]) : (tensor<64x64x1x256xf32>, tensor<4xi64>) -> tensor<1x64x64x256xf32> +// CHECK: %[[VAL_4:.*]] = "tf.Const"() <{value = dense<[0, 1]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK: %[[VAL_5:.*]] = "tf.ReverseV2"(%[[VAL_1:.*]], %[[VAL_4:.*]]) : (tensor<2x2x64x256xf32>, tensor<2xi64>) -> tensor<2x2x64x256xf32> +// CHECK: %[[VAL_6:.*]] = "tf.Const"() <{value = dense<[1, 128, 128, 64]> : tensor<4xi32>}> : () -> tensor<4xi32> +// CHECK: %[[VAL_7:.*]] = "tf.Conv2DBackpropInput"(%[[VAL_6:.*]], %[[VAL_5:.*]], %[[VAL_3:.*]]) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "VALID", strides = [1, 2, 2, 1], use_cudnn_on_gpu = true}> : (tensor<4xi32>, tensor<2x2x64x256xf32>, tensor<1x64x64x256xf32>) -> tensor<1x128x128x64xf32> +// CHECK: %[[VAL_8:.*]] = "tf.Const"() <{value = dense<[1, 2, 0, 3]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_9:.*]] = "tf.Transpose"(%[[VAL_7:.*]], %[[VAL_8:.*]]) : (tensor<1x128x128x64xf32>, tensor<4xi64>) -> tensor<128x128x1x64xf32> +// CHECK: return %[[VAL_9:.*]] : tensor<128x128x1x64xf32> +// CHECK: } + +func.func @convert_transpose_conv_with_transpose2(%arg0: tensor<64x64x1x256xf32>, %arg1: tensor<2x2x64x256xf32>) -> tensor<128x128x1x64xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) {batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[0, 1, b, f]x[0, 1, o, i]->[0, 1, b, f]>, + feature_group_count = 1 : i64, lhs_dilation = dense<2> : tensor<2xi64>, padding = dense<1> : tensor<2x2xi64>, precision_config = [#mhlo, #mhlo], rhs_dilation = dense<1> : tensor<2xi64>, window_reversal = dense : tensor<2xi1>, window_strides = dense<1> : tensor<2xi64>} : + (tensor<64x64x1x256xf32>, tensor<2x2x64x256xf32>) -> tensor<128x128x1x64xf32> + func.return %0 : tensor<128x128x1x64xf32> +} + + +// CHECK-LABEL: func @convert_conv2d_dynamic_batch( +// CHECK-SAME: %[[VAL_0:.*]]: tensor, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<3x3x207x16xf32>) -> tensor { +// CHECK: %[[VAL_2:.*]] = "tf.Conv2D"(%[[VAL_0]], %[[VAL_1]]) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "SAME", strides = [1, 1, 1, 1], use_cudnn_on_gpu = true}> : (tensor, tensor<3x3x207x16xf32>) -> tensor +// CHECK: return %[[VAL_2]] : tensor +// CHECK: } +func.func @convert_conv2d_dynamic_batch(%arg0: tensor, %arg1: tensor<3x3x207x16xf32>) -> tensor { + %0 = "mhlo.convolution"(%arg0, %arg1) {batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv, feature_group_count = 1 : i64, lhs_dilation = dense<1> : tensor<2xi64>, padding = dense<1> : tensor<2x2xi64>, precision_config = [#mhlo, #mhlo], rhs_dilation = dense<1> : tensor<2xi64>, window_strides = dense<1> : tensor<2xi64>} : + (tensor, tensor<3x3x207x16xf32>) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @convert_conv2d_no_padding( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x6x6x207xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<3x3x207x16xf32>) -> tensor<1x4x4x16xf32> { +// CHECK: %[[VAL_2:.*]] = "tf.Conv2D"(%[[VAL_0]], %[[VAL_1]]) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "VALID", strides = [1, 1, 1, 1], use_cudnn_on_gpu = true}> : (tensor<1x6x6x207xf32>, tensor<3x3x207x16xf32>) -> tensor<1x4x4x16xf32> +// CHECK: return %[[VAL_2]] : tensor<1x4x4x16xf32> +// CHECK: } +func.func @convert_conv2d_no_padding(%arg0: tensor<1x6x6x207xf32>, %arg1: tensor<3x3x207x16xf32>) -> tensor<1x4x4x16xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) {batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv, feature_group_count = 1 : i64, lhs_dilation = dense<1> : tensor<2xi64>, precision_config = [#mhlo, #mhlo], rhs_dilation = dense<1> : tensor<2xi64>, window_strides = dense<1> : tensor<2xi64>} : + (tensor<1x6x6x207xf32>, tensor<3x3x207x16xf32>) -> tensor<1x4x4x16xf32> + func.return %0 : tensor<1x4x4x16xf32> +} + +// CHECK-LABEL: func @convert_conv2d_no_rhs_dilation( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x8x8x207xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<3x3x207x16xf32>) -> tensor<1x8x8x16xf32> { +// CHECK: %[[VAL_2:.*]] = "tf.Conv2D"(%[[VAL_0]], %[[VAL_1]]) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "SAME", strides = [1, 1, 1, 1], use_cudnn_on_gpu = true}> : (tensor<1x8x8x207xf32>, tensor<3x3x207x16xf32>) -> tensor<1x8x8x16xf32> +// CHECK: return %[[VAL_2]] : tensor<1x8x8x16xf32> +// CHECK: } +func.func @convert_conv2d_no_rhs_dilation(%arg0: tensor<1x8x8x207xf32>, %arg1: tensor<3x3x207x16xf32>) -> tensor<1x8x8x16xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) {batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv, feature_group_count = 1 : i64, lhs_dilation = dense<1> : tensor<2xi64>, padding = dense<1> : tensor<2x2xi64>, precision_config = [#mhlo, #mhlo], window_strides = dense<1> : tensor<2xi64>} : + (tensor<1x8x8x207xf32>, tensor<3x3x207x16xf32>) -> tensor<1x8x8x16xf32> + func.return %0 : tensor<1x8x8x16xf32> +} + +// CHECK-LABEL: func @convert_conv2d_no_window_strides( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x8x8x207xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<3x3x207x16xf32>) -> tensor<1x8x8x16xf32> { +// CHECK: %[[VAL_2:.*]] = "tf.Conv2D"(%[[VAL_0]], %[[VAL_1]]) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "SAME", strides = [1, 1, 1, 1], use_cudnn_on_gpu = true}> : (tensor<1x8x8x207xf32>, tensor<3x3x207x16xf32>) -> tensor<1x8x8x16xf32> +// CHECK: return %[[VAL_2]] : tensor<1x8x8x16xf32> +// CHECK: } +func.func @convert_conv2d_no_window_strides(%arg0: tensor<1x8x8x207xf32>, %arg1: tensor<3x3x207x16xf32>) -> tensor<1x8x8x16xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) {batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv, feature_group_count = 1 : i64, lhs_dilation = dense<1> : tensor<2xi64>, padding = dense<1> : tensor<2x2xi64>, precision_config = [#mhlo, #mhlo], rhs_dilation = dense<1> : tensor<2xi64>} : + (tensor<1x8x8x207xf32>, tensor<3x3x207x16xf32>) -> tensor<1x8x8x16xf32> + func.return %0 : tensor<1x8x8x16xf32> +} + +// CHECK-LABEL: func @convert_conv2d_no_lhs_dilation( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x8x8x207xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<3x3x207x16xf32>) -> tensor<1x8x8x16xf32> { +// CHECK: %[[VAL_2:.*]] = "tf.Conv2D"(%[[VAL_0]], %[[VAL_1]]) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "SAME", strides = [1, 1, 1, 1], use_cudnn_on_gpu = true}> : (tensor<1x8x8x207xf32>, tensor<3x3x207x16xf32>) -> tensor<1x8x8x16xf32> +// CHECK: return %[[VAL_2]] : tensor<1x8x8x16xf32> +// CHECK: } +func.func @convert_conv2d_no_lhs_dilation(%arg0: tensor<1x8x8x207xf32>, %arg1: tensor<3x3x207x16xf32>) -> tensor<1x8x8x16xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) {batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv, feature_group_count = 1 : i64, padding = dense<1> : tensor<2x2xi64>, precision_config = [#mhlo, #mhlo], rhs_dilation = dense<1> : tensor<2xi64>, window_strides = dense<1> : tensor<2xi64>} : + (tensor<1x8x8x207xf32>, tensor<3x3x207x16xf32>) -> tensor<1x8x8x16xf32> + func.return %0 : tensor<1x8x8x16xf32> +} + +// CHECK-LABEL: func @convert_conv2d_with_transpose( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<8x8x1x207xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<3x3x16x207xf32>) -> tensor<16x8x8x1xf32> { +// CHECK: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<[2, 0, 1, 3]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_3:.*]] = "tf.Transpose"(%[[VAL_0]], %[[VAL_2]]) : (tensor<8x8x1x207xf32>, tensor<4xi64>) -> tensor<1x8x8x207xf32> +// CHECK: %[[VAL_4:.*]] = "tf.Const"() <{value = dense<[0, 1, 3, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_5:.*]] = "tf.Transpose"(%[[VAL_1]], %[[VAL_4]]) : (tensor<3x3x16x207xf32>, tensor<4xi64>) -> tensor<3x3x207x16xf32> +// CHECK: %[[VAL_6:.*]] = "tf.Conv2D"(%[[VAL_3]], %[[VAL_5]]) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "SAME", strides = [1, 1, 1, 1], use_cudnn_on_gpu = true}> : (tensor<1x8x8x207xf32>, tensor<3x3x207x16xf32>) -> tensor<1x8x8x16xf32> +// CHECK: %[[VAL_7:.*]] = "tf.Const"() <{value = dense<[3, 1, 2, 0]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_8:.*]] = "tf.Transpose"(%[[VAL_6]], %[[VAL_7]]) : (tensor<1x8x8x16xf32>, tensor<4xi64>) -> tensor<16x8x8x1xf32> +// CHECK: return %[[VAL_8]] : tensor<16x8x8x1xf32> +// CHECK: } +func.func @convert_conv2d_with_transpose(%arg0: tensor<8x8x1x207xf32>, %arg1: tensor<3x3x16x207xf32>) -> tensor<16x8x8x1xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) {batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv, feature_group_count = 1 : i64, lhs_dilation = dense<1> : tensor<2xi64>, padding = dense<1> : tensor<2x2xi64>, precision_config = [#mhlo, #mhlo], rhs_dilation = dense<1> : tensor<2xi64>, window_strides = dense<1> : tensor<2xi64>} : + (tensor<8x8x1x207xf32>, tensor<3x3x16x207xf32>) -> tensor<16x8x8x1xf32> + func.return %0 : tensor<16x8x8x1xf32> +} + +// CHECK-LABEL: func @convert_conv2d_with_transpose_dynamic_batch( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<8x8x?x207xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<3x3x16x207xf32>) -> tensor<16x8x8x?xf32> { +// CHECK: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<[2, 0, 1, 3]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_3:.*]] = "tf.Transpose"(%[[VAL_0]], %[[VAL_2]]) : (tensor<8x8x?x207xf32>, tensor<4xi64>) -> tensor +// CHECK: %[[VAL_4:.*]] = "tf.Const"() <{value = dense<[0, 1, 3, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_5:.*]] = "tf.Transpose"(%[[VAL_1]], %[[VAL_4]]) : (tensor<3x3x16x207xf32>, tensor<4xi64>) -> tensor<3x3x207x16xf32> +// CHECK: %[[VAL_6:.*]] = "tf.Conv2D"(%[[VAL_3]], %[[VAL_5]]) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "SAME", strides = [1, 1, 1, 1], use_cudnn_on_gpu = true}> : (tensor, tensor<3x3x207x16xf32>) -> tensor +// CHECK: %[[VAL_7:.*]] = "tf.Const"() <{value = dense<[3, 1, 2, 0]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_8:.*]] = "tf.Transpose"(%[[VAL_6]], %[[VAL_7]]) : (tensor, tensor<4xi64>) -> tensor<16x8x8x?xf32> +// CHECK: return %[[VAL_8]] : tensor<16x8x8x?xf32> +// CHECK: } +func.func @convert_conv2d_with_transpose_dynamic_batch(%arg0: tensor<8x8x?x207xf32>, %arg1: tensor<3x3x16x207xf32>) -> tensor<16x8x8x?xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) {batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv, feature_group_count = 1 : i64, lhs_dilation = dense<1> : tensor<2xi64>, padding = dense<1> : tensor<2x2xi64>, precision_config = [#mhlo, #mhlo], rhs_dilation = dense<1> : tensor<2xi64>, window_strides = dense<1> : tensor<2xi64>} : + (tensor<8x8x?x207xf32>, tensor<3x3x16x207xf32>) -> tensor<16x8x8x?xf32> + func.return %0 : tensor<16x8x8x?xf32> +} + +// CHECK-LABEL: func @convert_conv2d_explicit_padding( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<64x8x8x8xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<8x8x8x64xf32>) -> tensor<64x3x3x64xf32> { +// CHECK: %[[VAL_2:.*]] = "tf.Conv2D"(%[[VAL_0]], %[[VAL_1]]) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [0, 0, 1, 1, 1, 1, 0, 0], padding = "EXPLICIT", strides = [1, 1, 1, 1], use_cudnn_on_gpu = true}> : (tensor<64x8x8x8xf32>, tensor<8x8x8x64xf32>) -> tensor<64x3x3x64xf32> +// CHECK: return %[[VAL_2]] : tensor<64x3x3x64xf32> +// CHECK: } +func.func @convert_conv2d_explicit_padding(%arg0: tensor<64x8x8x8xf32>, %arg1: tensor<8x8x8x64xf32>) -> tensor<64x3x3x64xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) {batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv, feature_group_count = 1 : i64, lhs_dilation = dense<1> : tensor<2xi64>, padding = dense<1> : tensor<2x2xi64>, + precision_config = [#mhlo, #mhlo], rhs_dilation = dense<1> : tensor<2xi64>, window_strides = dense<1> : tensor<2xi64> + } : (tensor<64x8x8x8xf32>, tensor<8x8x8x64xf32>) -> tensor<64x3x3x64xf32> + func.return %0 : tensor<64x3x3x64xf32> +} + +// CHECK-LABEL: func @convert_conv2d_explicit_padding_dynamic_batch( +// CHECK-SAME: %[[VAL_0:.*]]: tensor, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<8x8x8x64xf32>) -> tensor { +// CHECK: %[[VAL_2:.*]] = "tf.Conv2D"(%[[VAL_0]], %[[VAL_1]]) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [0, 0, 1, 1, 1, 1, 0, 0], padding = "EXPLICIT", strides = [1, 1, 1, 1], use_cudnn_on_gpu = true}> : (tensor, tensor<8x8x8x64xf32>) -> tensor +// CHECK: return %[[VAL_2]] : tensor +// CHECK: } +func.func @convert_conv2d_explicit_padding_dynamic_batch(%arg0: tensor, %arg1: tensor<8x8x8x64xf32>) -> tensor { + %0 = "mhlo.convolution"(%arg0, %arg1) {batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv, feature_group_count = 1 : i64, lhs_dilation = dense<1> : tensor<2xi64>, padding = dense<1> : tensor<2x2xi64>, + precision_config = [#mhlo, #mhlo], rhs_dilation = dense<1> : tensor<2xi64>, window_strides = dense<1> : tensor<2xi64> + } : (tensor, tensor<8x8x8x64xf32>) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @convert_conv2d_negative_explicit_padding( +// CHECK-SAME: %[[ARG0:.*]]: tensor<128x7x9x64xf32>, +// CHECK-SAME: %[[ARG1:.*]]: tensor<3x2x64x4xf32>) -> tensor<128x4x3x4xf32> { +// CHECK-DAG: %[[START:.*]] = "tf.Const"() <{value = dense<[0, 0, 5, 0]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK-DAG: %[[SIZE:.*]] = "tf.Const"() <{value = dense<[128, 5, 4, 64]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[SLICED_ARG0:.*]] = "tf.Slice"(%[[ARG0]], %[[START]], %[[SIZE]]) +// CHECK-SAME: (tensor<128x7x9x64xf32>, tensor<4xi64>, tensor<4xi64>) -> tensor<128x5x4x64xf32> +// CHECK: %[[CONV:.*]] = "tf.Conv2D"(%[[SLICED_ARG0]], %[[ARG1]]) +// CHECK-SAME: explicit_paddings = [0, 0, 4, 0, 0, 2, 0, 0] +// CHECK-SAME: (tensor<128x5x4x64xf32>, tensor<3x2x64x4xf32>) -> tensor<128x4x3x4xf32> +// CHECK: return %[[CONV]] : tensor<128x4x3x4xf32> +// CHECK: } +func.func @convert_conv2d_negative_explicit_padding(%arg0: tensor<128x7x9x64xf32>, %arg1: tensor<3x2x64x4xf32>) -> tensor<128x4x3x4xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) {batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv, feature_group_count = 1 : i64, lhs_dilation = dense<1> : tensor<2xi64>, padding = dense<[[4, -2], [-5, 2]]> : tensor<2x2xi64>, + precision_config = [#mhlo, #mhlo], rhs_dilation = dense<1> : tensor<2xi64>, window_strides = dense<2> : tensor<2xi64> + } : (tensor<128x7x9x64xf32>, tensor<3x2x64x4xf32>) -> tensor<128x4x3x4xf32> + func.return %0 : tensor<128x4x3x4xf32> +} + +// CHECK-LABEL: func @convert_conv2d_negative_explicit_padding_dynamic_batch( +// CHECK-SAME: %[[ARG0:.*]]: tensor, +// CHECK-SAME: %[[ARG1:.*]]: tensor<3x2x64x4xf32>) -> tensor { +// CHECK-DAG: %[[START:.*]] = "tf.Const"() <{value = dense<[0, 0, 5, 0]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK-DAG: %[[SIZE:.*]] = "tf.Const"() <{value = dense<[-9223372036854775808, 5, 4, 64]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[SLICED_ARG0:.*]] = "tf.Slice"(%[[ARG0]], %[[START]], %[[SIZE]]) +// CHECK-SAME: (tensor, tensor<4xi64>, tensor<4xi64>) -> tensor +// CHECK: %[[CONV:.*]] = "tf.Conv2D"(%[[SLICED_ARG0]], %[[ARG1]]) +// CHECK-SAME: explicit_paddings = [0, 0, 4, 0, 0, 2, 0, 0] +// CHECK-SAME: (tensor, tensor<3x2x64x4xf32>) -> tensor +// CHECK: return %[[CONV]] : tensor +// CHECK: } +func.func @convert_conv2d_negative_explicit_padding_dynamic_batch(%arg0: tensor, %arg1: tensor<3x2x64x4xf32>) -> tensor { + %0 = "mhlo.convolution"(%arg0, %arg1) {batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv, feature_group_count = 1 : i64, lhs_dilation = dense<1> : tensor<2xi64>, padding = dense<[[4, -2], [-5, 2]]> : tensor<2x2xi64>, + precision_config = [#mhlo, #mhlo], rhs_dilation = dense<1> : tensor<2xi64>, window_strides = dense<2> : tensor<2xi64> + } : (tensor, tensor<3x2x64x4xf32>) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @convert_depthwise_conv2d( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x8x8x207xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<3x3x1x3312xf32>) -> tensor<1x8x8x3312xf32> { +// CHECK: %[[CST:.*]] = arith.constant dense<[3, 3, 207, 16]> : tensor<4xi64> +// CHECK: %[[VAL_2:.*]] = "tf.Reshape"(%[[VAL_1]], %[[CST]]) : (tensor<3x3x1x3312xf32>, tensor<4xi64>) -> tensor<3x3x207x16xf32> +// CHECK: %[[VAL_3:.*]] = "tf.DepthwiseConv2dNative"(%[[VAL_0]], %[[VAL_2]]) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "SAME", strides = [1, 1, 1, 1]}> : (tensor<1x8x8x207xf32>, tensor<3x3x207x16xf32>) -> tensor<1x8x8x3312xf32> +// CHECK: return %[[VAL_3]] : tensor<1x8x8x3312xf32> +// CHECK: } +func.func @convert_depthwise_conv2d(%arg0: tensor<1x8x8x207xf32>, %arg1: tensor<3x3x1x3312xf32>) -> tensor<1x8x8x3312xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) {batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv, feature_group_count = 207 : i64, lhs_dilation = dense<1> : tensor<2xi64>, padding = dense<1> : tensor<2x2xi64>, precision_config = [#mhlo, #mhlo], rhs_dilation = dense<1> : tensor<2xi64>, window_strides = dense<1> : tensor<2xi64>} : + (tensor<1x8x8x207xf32>, tensor<3x3x1x3312xf32>) -> tensor<1x8x8x3312xf32> + func.return %0 : tensor<1x8x8x3312xf32> +} + +// CHECK-LABEL: func @convert_depthwise_transposed_conv2d +func.func @convert_depthwise_transposed_conv2d(%arg0: tensor<1x2x20x20xf32>, %arg1: tensor<8x8x2x1xf32>) -> (tensor<1x2x80x80xf32>) { + %0 = mhlo.convolution(%arg0, %arg1) dim_numbers = [b, f, 0, 1]x[0, 1, o, i]->[b, f, 0, 1], window = {pad = [[5, 5], [5, 5]], lhs_dilate = [4, 4]} {batch_group_count = 1 : i64, feature_group_count = 2 : i64} : (tensor<1x2x20x20xf32>, tensor<8x8x2x1xf32>) -> tensor<1x2x80x80xf32> + return %0 : tensor<1x2x80x80xf32> + // CHECK: %cst = "tf.Const"() <{value = dense<0> : tensor<4xi64>}> : () -> tensor<4xi64> + // CHECK-DAG: %cst_0 = "tf.Const"() <{value = dense<[1, 1, 20, 20]> : tensor<4xi64>}> : () -> tensor<4xi64> + // CHECK: %cst_1 = "tf.Const"() <{value = dense<1> : tensor<4xi64>}> : () -> tensor<4xi64> + // CHECK: %0 = "tf.StridedSlice"(%arg0, %cst, %cst_0, %cst_1) <{begin_mask = 0 : i64, ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64}> : (tensor<1x2x20x20xf32>, tensor<4xi64>, tensor<4xi64>, tensor<4xi64>) -> tensor<1x1x20x20xf32> + // CHECK: %cst_2 = "tf.Const"() <{value = dense<0> : tensor<4xi64>}> : () -> tensor<4xi64> + // CHECK-DAG: %cst_3 = "tf.Const"() <{value = dense<[8, 8, 1, 1]> : tensor<4xi64>}> : () -> tensor<4xi64> + // CHECK: %cst_4 = "tf.Const"() <{value = dense<1> : tensor<4xi64>}> : () -> tensor<4xi64> + // CHECK: %1 = "tf.StridedSlice"(%arg1, %cst_2, %cst_3, %cst_4) <{begin_mask = 0 : i64, ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64}> : (tensor<8x8x2x1xf32>, tensor<4xi64>, tensor<4xi64>, tensor<4xi64>) -> tensor<8x8x1x1xf32> + // CHECK: %cst_5 = "tf.Const"() <{value = dense<[0, 2, 3, 1]> : tensor<4xi64>}> : () -> tensor<4xi64> + // CHECK: %2 = "tf.Transpose"(%0, %cst_5) : (tensor<1x1x20x20xf32>, tensor<4xi64>) -> tensor<1x20x20x1xf32> + // CHECK: %cst_6 = "tf.Const"() <{value = dense<[0, 1]> : tensor<2xi64>}> : () -> tensor<2xi64> + // CHECK: %3 = "tf.ReverseV2"(%1, %cst_6) : (tensor<8x8x1x1xf32>, tensor<2xi64>) -> tensor<8x8x1x1xf32> + // CHECK: %cst_7 = "tf.Const"() <{value = dense<[1, 80, 80, 1]> : tensor<4xi32>}> : () -> tensor<4xi32> + // CHECK: %4 = "tf.Conv2DBackpropInput"(%cst_7, %3, %2) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "SAME", strides = [1, 4, 4, 1], use_cudnn_on_gpu = true}> : (tensor<4xi32>, tensor<8x8x1x1xf32>, tensor<1x20x20x1xf32>) -> tensor<1x80x80x1xf32> + // CHECK: %cst_8 = "tf.Const"() <{value = dense<[0, 3, 1, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> + // CHECK: %5 = "tf.Transpose"(%4, %cst_8) : (tensor<1x80x80x1xf32>, tensor<4xi64>) -> tensor<1x1x80x80xf32> + // CHECK: %cst_9 = "tf.Const"() <{value = dense<[0, 1, 0, 0]> : tensor<4xi64>}> : () -> tensor<4xi64> + // CHECK-DAG: %cst_10 = "tf.Const"() <{value = dense<[1, 2, 20, 20]> : tensor<4xi64>}> : () -> tensor<4xi64> + // CHECK: %cst_11 = "tf.Const"() <{value = dense<1> : tensor<4xi64>}> : () -> tensor<4xi64> + // CHECK: %6 = "tf.StridedSlice"(%arg0, %cst_9, %cst_10, %cst_11) <{begin_mask = 0 : i64, ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64}> : (tensor<1x2x20x20xf32>, tensor<4xi64>, tensor<4xi64>, tensor<4xi64>) -> tensor<1x1x20x20xf32> + // CHECK: %cst_12 = "tf.Const"() <{value = dense<[0, 0, 1, 0]> : tensor<4xi64>}> : () -> tensor<4xi64> + // CHECK-DAG: %cst_13 = "tf.Const"() <{value = dense<[8, 8, 2, 1]> : tensor<4xi64>}> : () -> tensor<4xi64> + // CHECK: %cst_14 = "tf.Const"() <{value = dense<1> : tensor<4xi64>}> : () -> tensor<4xi64> + // CHECK: %7 = "tf.StridedSlice"(%arg1, %cst_12, %cst_13, %cst_14) <{begin_mask = 0 : i64, ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64}> : (tensor<8x8x2x1xf32>, tensor<4xi64>, tensor<4xi64>, tensor<4xi64>) -> tensor<8x8x1x1xf32> + // CHECK: %cst_15 = "tf.Const"() <{value = dense<[0, 2, 3, 1]> : tensor<4xi64>}> : () -> tensor<4xi64> + // CHECK: %8 = "tf.Transpose"(%6, %cst_15) : (tensor<1x1x20x20xf32>, tensor<4xi64>) -> tensor<1x20x20x1xf32> + // CHECK: %cst_16 = "tf.Const"() <{value = dense<[0, 1]> : tensor<2xi64>}> : () -> tensor<2xi64> + // CHECK: %9 = "tf.ReverseV2"(%7, %cst_16) : (tensor<8x8x1x1xf32>, tensor<2xi64>) -> tensor<8x8x1x1xf32> + // CHECK: %cst_17 = "tf.Const"() <{value = dense<[1, 80, 80, 1]> : tensor<4xi32>}> : () -> tensor<4xi32> + // CHECK: %10 = "tf.Conv2DBackpropInput"(%cst_17, %9, %8) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "SAME", strides = [1, 4, 4, 1], use_cudnn_on_gpu = true}> : (tensor<4xi32>, tensor<8x8x1x1xf32>, tensor<1x20x20x1xf32>) -> tensor<1x80x80x1xf32> + // CHECK: %cst_18 = "tf.Const"() <{value = dense<[0, 3, 1, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> + // CHECK: %11 = "tf.Transpose"(%10, %cst_18) : (tensor<1x80x80x1xf32>, tensor<4xi64>) -> tensor<1x1x80x80xf32> + // CHECK: %cst_19 = "tf.Const"() <{value = dense<1> : tensor}> : () -> tensor + // CHECK: %12 = "tf.ConcatV2"(%5, %11, %cst_19) : (tensor<1x1x80x80xf32>, tensor<1x1x80x80xf32>, tensor) -> tensor<1x2x80x80xf32> + // CHECK: return %12 : tensor<1x2x80x80xf32> +} + +// CHECK-LABEL: func @convert_conv2d_to_resize( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x56x624x16xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x257x16x1xf32>) -> tensor<1x56x904x16xf32> { +// CHECK-DAG: %[[SIZE:.*]] = "tf.Const"() <{value = dense<[56, 904]> : tensor<2xi32>}> : () -> tensor<2xi32> +// CHECK: %[[VAL_2:.*]] = "tf.ResizeBilinear"(%[[VAL_0]], %[[SIZE]]) <{align_corners = true, half_pixel_centers = false}> : (tensor<1x56x624x16xf32>, tensor<2xi32>) -> tensor<1x56x904x16xf32> +// CHECK: return %[[VAL_2]] : tensor<1x56x904x16xf32> +// CHECK: } +func.func @convert_conv2d_to_resize(%arg0: tensor<1x56x624x16xf32>, %arg1: tensor<1x257x16x1xf32>) -> tensor<1x56x904x16xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) {batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, 1, f]x[0, 1, o, i]->[b, 0, 1, f]>, + feature_group_count = 16 : i64, + lhs_dilation = dense<[1, 129]> : tensor<2xi64>, + padding = dense<[[0, 0], [128, 128]]> : tensor<2x2xi64>, + precision_config = [#mhlo, #mhlo], + rhs_dilation = dense<1> : tensor<2xi64>, + window_strides = dense<[1, 89]> : tensor<2xi64>} : (tensor<1x56x624x16xf32>, tensor<1x257x16x1xf32>) -> tensor<1x56x904x16xf32> + func.return %0 : tensor<1x56x904x16xf32> +} + +// CHECK-LABEL: func @convert_conv2d_resize_perferred( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x56x1248x16xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<3x1x16x1xf32>) -> tensor<1x111x1248x16xf32> { +// CHECK-DAG: %[[SIZE:.*]] = "tf.Const"() <{value = dense<[111, 1248]> : tensor<2xi32>}> : () -> tensor<2xi32> +// CHECK: %[[VAL_2:.*]] = "tf.ResizeBilinear"(%[[VAL_0]], %[[SIZE]]) <{align_corners = true, half_pixel_centers = false}> : (tensor<1x56x1248x16xf32>, tensor<2xi32>) -> tensor<1x111x1248x16xf32> +// CHECK: return %[[VAL_2]] : tensor<1x111x1248x16xf32> +// CHECK: } +func.func @convert_conv2d_resize_perferred(%arg0: tensor<1x56x1248x16xf32>, %arg1: tensor<3x1x16x1xf32>) -> tensor<1x111x1248x16xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) {batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, 1, f]x[0, 1, o, i]->[b, 0, 1, f]>, + feature_group_count = 16 : i64, + lhs_dilation = dense<[2, 1]> : tensor<2xi64>, + padding = dense<[[1, 1], [0, 0]]> : tensor<2x2xi64>, + precision_config = [#mhlo, #mhlo], + rhs_dilation = dense<[1, 1]> : tensor<2xi64>, + window_strides = dense<[1, 1]> : tensor<2xi64>} : (tensor<1x56x1248x16xf32>, tensor<3x1x16x1xf32>) -> tensor<1x111x1248x16xf32> + func.return %0 : tensor<1x111x1248x16xf32> +} + +// CHECK-LABEL: func @convert_conv2d_back_prop_input_same_pad( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x256x256x2xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<4x4x2x2xf32>) -> tensor<1x512x512x2xf32> { +// CHECK: %[[VAL_3:.*]] = "tf.Const"() <{value = dense<[0, 1]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK: %[[VAL_4:.*]] = "tf.ReverseV2"(%[[VAL_1]], %[[VAL_3]]) : (tensor<4x4x2x2xf32>, tensor<2xi64>) -> tensor<4x4x2x2xf32> +// CHECK: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<[1, 512, 512, 2]> : tensor<4xi32>}> : () -> tensor<4xi32> +// CHECK: %[[VAL_5:.*]] = "tf.Conv2DBackpropInput"(%[[VAL_2]], %[[VAL_4]], %[[VAL_0]]) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "SAME", strides = [1, 2, 2, 1], use_cudnn_on_gpu = true}> : (tensor<4xi32>, tensor<4x4x2x2xf32>, tensor<1x256x256x2xf32>) -> tensor<1x512x512x2xf32> +// CHECK: return %[[VAL_5]] : tensor<1x512x512x2xf32> +// CHECK: } +func.func @convert_conv2d_back_prop_input_same_pad(%arg0: tensor<1x256x256x2xf32>, %arg1: tensor<4x4x2x2xf32>) -> tensor<1x512x512x2xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) {batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, 1, f]x[0, 1, o, i]->[b, 0, 1, f]>, feature_group_count = 1 : i64, lhs_dilation = dense<2> : tensor<2xi64>, padding = dense<[[2, 2], [2, 2]]> : tensor<2x2xi64>, + precision_config = [#mhlo, #mhlo], rhs_dilation = dense<1> : tensor<2xi64>, window_strides = dense<1> : tensor<2xi64> + } : (tensor<1x256x256x2xf32>, tensor<4x4x2x2xf32>) -> tensor<1x512x512x2xf32> + func.return %0 : tensor<1x512x512x2xf32> +} + +// CHECK-LABEL: func @convert_conv2d_back_prop_input_negative_pad( +// CHECK-NOT: "tf.Conv2DBackpropInput" +func.func @convert_conv2d_back_prop_input_negative_pad(%arg0: tensor<1x256x256x2xf32>, %arg1: tensor<4x4x2x2xf32>) -> tensor<1x504x504x2xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) {batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, 1, f]x[0, 1, o, i]->[b, 0, 1, f]>, feature_group_count = 1 : i64, lhs_dilation = dense<2> : tensor<2xi64>, padding = dense<[[-2, -2], [-2, -2]]> : tensor<2x2xi64>, + precision_config = [#mhlo, #mhlo], rhs_dilation = dense<1> : tensor<2xi64>, window_strides = dense<1> : tensor<2xi64> + } : (tensor<1x256x256x2xf32>, tensor<4x4x2x2xf32>) -> tensor<1x504x504x2xf32> + func.return %0 : tensor<1x504x504x2xf32> +} + + +// CHECK-LABEL: func @convert_conv2d_back_prop_input( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<8x4x4x32xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<3x3x64x32xf32>) -> tensor<8x8x8x64xf32> { +// CHECK: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<[0, 1]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK: %[[VAL_3:.*]] = "tf.ReverseV2"(%[[VAL_1]], %[[VAL_2]]) : (tensor<3x3x64x32xf32>, tensor<2xi64>) -> tensor<3x3x64x32xf32> +// CHECK: %[[VAL_4:.*]] = "tf.Const"() <{value = dense<[8, 8, 8, 64]> : tensor<4xi32>}> : () -> tensor<4xi32> +// CHECK: %[[VAL_5:.*]] = "tf.Conv2DBackpropInput"(%[[VAL_4]], %[[VAL_3]], %[[VAL_0]]) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "VALID", strides = [1, 2, 2, 1], use_cudnn_on_gpu = true}> : (tensor<4xi32>, tensor<3x3x64x32xf32>, tensor<8x4x4x32xf32>) -> tensor<8x8x8x64xf32> +// CHECK: return %[[VAL_5]] : tensor<8x8x8x64xf32> +// CHECK: } +func.func @convert_conv2d_back_prop_input(%arg0: tensor<8x4x4x32xf32>, %arg1: tensor<3x3x64x32xf32>) -> tensor<8x8x8x64xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) {batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv, feature_group_count = 1 : i64, lhs_dilation = dense<2> : tensor<2xi64>, padding = dense<[[2, 1], [2, 1]]> : tensor<2x2xi64>, + precision_config = [#mhlo, #mhlo], rhs_dilation = dense<1> : tensor<2xi64>, window_strides = dense<1> : tensor<2xi64> + } : (tensor<8x4x4x32xf32>, tensor<3x3x64x32xf32>) -> tensor<8x8x8x64xf32> + func.return %0 : tensor<8x8x8x64xf32> +} + +// CHECK-LABEL: func @convert_conv2d_back_prop_input_transpose_filter( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<8x4x4x32xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<3x3x32x64xf32>) -> tensor<8x8x8x64xf32> { +// CHECK-DAG: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<[0, 1]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK-DAG: %[[VAL_3:.*]] = "tf.Const"() <{value = dense<[0, 1, 3, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK-DAG: %[[VAL_4:.*]] = "tf.Transpose"(%[[VAL_1]], %[[VAL_3]]) : (tensor<3x3x32x64xf32>, tensor<4xi64>) -> tensor<3x3x64x32xf32> +// CHECK: %[[VAL_5:.*]] = "tf.ReverseV2"(%[[VAL_4]], %[[VAL_2]]) : (tensor<3x3x64x32xf32>, tensor<2xi64>) -> tensor<3x3x64x32xf32> +// CHECK: %[[VAL_6:.*]] = "tf.Const"() <{value = dense<[8, 8, 8, 64]> : tensor<4xi32>}> : () -> tensor<4xi32> +// CHECK: %[[VAL_7:.*]] = "tf.Conv2DBackpropInput"(%[[VAL_6]], %[[VAL_5]], %[[VAL_0]]) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "VALID", strides = [1, 2, 2, 1], use_cudnn_on_gpu = true}> : (tensor<4xi32>, tensor<3x3x64x32xf32>, tensor<8x4x4x32xf32>) -> tensor<8x8x8x64xf32> +// CHECK: return %[[VAL_7]] : tensor<8x8x8x64xf32> +// CHECK: } +func.func @convert_conv2d_back_prop_input_transpose_filter(%arg0: tensor<8x4x4x32xf32>, %arg1: tensor<3x3x32x64xf32>) -> tensor<8x8x8x64xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) {batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv, feature_group_count = 1 : i64, lhs_dilation = dense<2> : tensor<2xi64>, padding = dense<[[2, 1], [2, 1]]> : tensor<2x2xi64>, + precision_config = [#mhlo, #mhlo], rhs_dilation = dense<1> : tensor<2xi64>, window_strides = dense<1> : tensor<2xi64> + } : (tensor<8x4x4x32xf32>, tensor<3x3x32x64xf32>) -> tensor<8x8x8x64xf32> + func.return %0 : tensor<8x8x8x64xf32> +} + +// CHECK-LABEL: func @convert_conv2d_valid_padding( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x8x8x207xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<3x3x207x16xf32>) -> tensor<1x6x6x16xf32> { +// CHECK: %[[VAL_2:.*]] = "tf.Conv2D"(%[[VAL_0]], %[[VAL_1]]) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "VALID", strides = [1, 1, 1, 1], use_cudnn_on_gpu = true}> : (tensor<1x8x8x207xf32>, tensor<3x3x207x16xf32>) -> tensor<1x6x6x16xf32> +// CHECK: return %[[VAL_2]] : tensor<1x6x6x16xf32> +// CHECK: } +func.func @convert_conv2d_valid_padding(%arg0: tensor<1x8x8x207xf32>, %arg1: tensor<3x3x207x16xf32>) -> tensor<1x6x6x16xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) {batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv, feature_group_count = 1 : i64, lhs_dilation = dense<1> : tensor<2xi64>, padding = dense<0> : tensor<2x2xi64>, precision_config = [#mhlo, #mhlo], rhs_dilation = dense<1> : tensor<2xi64>, window_strides = dense<1> : tensor<2xi64>} : + (tensor<1x8x8x207xf32>, tensor<3x3x207x16xf32>) -> tensor<1x6x6x16xf32> + func.return %0 : tensor<1x6x6x16xf32> +} + +// CHECK-LABEL: func @convert_conv2d_valid_padding_dynamic_batch( +// CHECK-SAME: %[[VAL_0:.*]]: tensor, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<3x3x207x16xf32>) -> tensor { +// CHECK: %[[VAL_2:.*]] = "tf.Conv2D"(%[[VAL_0]], %[[VAL_1]]) <{data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "VALID", strides = [1, 1, 1, 1], use_cudnn_on_gpu = true}> : (tensor, tensor<3x3x207x16xf32>) -> tensor +// CHECK: return %[[VAL_2]] : tensor +// CHECK: } +func.func @convert_conv2d_valid_padding_dynamic_batch(%arg0: tensor, %arg1: tensor<3x3x207x16xf32>) -> tensor { + %0 = "mhlo.convolution"(%arg0, %arg1) {batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv, feature_group_count = 1 : i64, lhs_dilation = dense<1> : tensor<2xi64>, padding = dense<0> : tensor<2x2xi64>, precision_config = [#mhlo, #mhlo], rhs_dilation = dense<1> : tensor<2xi64>, window_strides = dense<1> : tensor<2xi64>} : + (tensor, tensor<3x3x207x16xf32>) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @convert_reduce_to_prod( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x256xf32>) -> tensor<1xf32> { +// CHECK-DAG: %[[VAL_1:.*]] = "tf.Const"() <{value = dense<1.000000e+00> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<1> : tensor<1xi64>}> : () -> tensor<1xi64> +// CHECK: %[[VAL_3:.*]] = "tf.Prod"(%[[VAL_0]], %[[VAL_2]]) <{keep_dims = false}> : (tensor<1x256xf32>, tensor<1xi64>) -> tensor<1xf32> +// CHECK: return %[[VAL_3]] : tensor<1xf32> +// CHECK: } +func.func @convert_reduce_to_prod(%arg0: tensor<1x256xf32>) -> tensor<1xf32> { + %0 = mhlo.constant dense<1.000000e+00> : tensor + %1 = "mhlo.reduce"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %2 = mhlo.multiply %arg1, %arg2 : tensor + "mhlo.return"(%2) : (tensor) -> () + }) {dimensions = dense<1> : tensor<1xi64>} : (tensor<1x256xf32>, tensor) -> tensor<1xf32> + func.return %1 : tensor<1xf32> +} + +// CHECK-LABEL: func @convert_reduce_to_sum( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x256xf32>) -> tensor<1xf32> { +// CHECK-DAG: %[[VAL_1:.*]] = "tf.Const"() <{value = dense<0.000000e+00> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<1> : tensor<1xi64>}> : () -> tensor<1xi64> +// CHECK: %[[VAL_3:.*]] = "tf.Sum"(%[[VAL_0]], %[[VAL_2]]) <{keep_dims = false}> : (tensor<1x256xf32>, tensor<1xi64>) -> tensor<1xf32> +// CHECK: return %[[VAL_3]] : tensor<1xf32> +// CHECK: } +func.func @convert_reduce_to_sum(%arg0: tensor<1x256xf32>) -> tensor<1xf32> { + %0 = mhlo.constant dense<0.000000e+00> : tensor + %1 = "mhlo.reduce"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %2 = mhlo.add %arg1, %arg2 : tensor + "mhlo.return"(%2) : (tensor) -> () + }) {dimensions = dense<1> : tensor<1xi64>} : (tensor<1x256xf32>, tensor) -> tensor<1xf32> + func.return %1 : tensor<1xf32> +} + +// CHECK-LABEL: func @convert_reduce_to_prod_non_constant_init( +// CHECK-SAME: %[[ARG_0:.*]]: tensor<1x256xf32>, +// CHECK-SAME: %[[ARG_1:.*]]: tensor) -> tensor<1xf32> { +// CHECK-DAG: %[[VAL_0:.*]] = "tf.Const"() <{value = dense<1> : tensor<1xi64>}> : () -> tensor<1xi64> +// CHECK: %[[VAL_1:.*]] = "tf.Prod"(%[[ARG_0]], %[[VAL_0]]) <{keep_dims = false}> : (tensor<1x256xf32>, tensor<1xi64>) -> tensor<1xf32> +// CHECK: %[[VAL_2:.*]] = "tf.Mul"(%[[VAL_1]], %[[ARG_1]]) : (tensor<1xf32>, tensor) -> tensor<1xf32> +// CHECK: return %[[VAL_2]] : tensor<1xf32> +// CHECK: } +func.func @convert_reduce_to_prod_non_constant_init(%arg0: tensor<1x256xf32>, %arg1: tensor) -> tensor<1xf32> { + %1 = "mhlo.reduce"(%arg0, %arg1) ({ + ^bb0(%arg2: tensor, %arg3: tensor): + %2 = mhlo.multiply %arg2, %arg3 : tensor + "mhlo.return"(%2) : (tensor) -> () + }) {dimensions = dense<1> : tensor<1xi64>} : (tensor<1x256xf32>, tensor) -> tensor<1xf32> + func.return %1 : tensor<1xf32> +} + + +// CHECK-LABEL: func @convert_reduce_to_sum_non_constant_init( +// CHECK-SAME: %[[ARG_0:.*]]: tensor<1x256xf32>, +// CHECK-SAME: %[[ARG_1:.*]]: tensor) -> tensor<1xf32> { +// CHECK-DAG: %[[VAL_0:.*]] = "tf.Const"() <{value = dense<1> : tensor<1xi64>}> : () -> tensor<1xi64> +// CHECK: %[[VAL_1:.*]] = "tf.Sum"(%[[ARG_0]], %[[VAL_0]]) <{keep_dims = false}> : (tensor<1x256xf32>, tensor<1xi64>) -> tensor<1xf32> +// CHECK: %[[VAL_2:.*]] = "tf.Add"(%[[VAL_1]], %[[ARG_1]]) : (tensor<1xf32>, tensor) -> tensor<1xf32> +// CHECK: return %[[VAL_2]] : tensor<1xf32> +// CHECK: } +func.func @convert_reduce_to_sum_non_constant_init(%arg0: tensor<1x256xf32>, %arg1: tensor) -> tensor<1xf32> { + %1 = "mhlo.reduce"(%arg0, %arg1) ({ + ^bb0(%arg2: tensor, %arg3: tensor): + %2 = mhlo.add %arg2, %arg3 : tensor + "mhlo.return"(%2) : (tensor) -> () + }) {dimensions = dense<1> : tensor<1xi64>} : (tensor<1x256xf32>, tensor) -> tensor<1xf32> + func.return %1 : tensor<1xf32> +} + +// CHECK-LABEL: func @convert_int_reduce_to_prod( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x256xi32>) -> tensor<1xi32> { +// CHECK-DAG: %[[VAL_1:.*]] = "tf.Const"() <{value = dense<1> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<1> : tensor<1xi64>}> : () -> tensor<1xi64> +// CHECK: %[[VAL_3:.*]] = "tf.Prod"(%[[VAL_0]], %[[VAL_2]]) <{keep_dims = false}> : (tensor<1x256xi32>, tensor<1xi64>) -> tensor<1xi32> +// CHECK: return %[[VAL_3]] : tensor<1xi32> +// CHECK: } +func.func @convert_int_reduce_to_prod(%arg0: tensor<1x256xi32>) -> tensor<1xi32> { + %0 = mhlo.constant dense<1> : tensor + %1 = "mhlo.reduce"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %2 = mhlo.multiply %arg1, %arg2 : tensor + "mhlo.return"(%2) : (tensor) -> () + }) {dimensions = dense<1> : tensor<1xi64>} : (tensor<1x256xi32>, tensor) -> tensor<1xi32> + func.return %1 : tensor<1xi32> +} + + +// CHECK-LABEL: func @convert_int_reduce_to_sum( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x256xi32>) -> tensor<1xi32> { +// CHECK-DAG: %[[VAL_1:.*]] = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<1> : tensor<1xi64>}> : () -> tensor<1xi64> +// CHECK: %[[VAL_3:.*]] = "tf.Sum"(%[[VAL_0]], %[[VAL_2]]) <{keep_dims = false}> : (tensor<1x256xi32>, tensor<1xi64>) -> tensor<1xi32> +// CHECK: return %[[VAL_3]] : tensor<1xi32> +// CHECK: } +func.func @convert_int_reduce_to_sum(%arg0: tensor<1x256xi32>) -> tensor<1xi32> { + %0 = mhlo.constant dense<0> : tensor + %1 = "mhlo.reduce"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %2 = mhlo.add %arg1, %arg2 : tensor + "mhlo.return"(%2) : (tensor) -> () + }) {dimensions = dense<1> : tensor<1xi64>} : (tensor<1x256xi32>, tensor) -> tensor<1xi32> + func.return %1 : tensor<1xi32> +} + +// CHECK-LABEL: func @convert_reduce_to_max( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x256xf32>) -> tensor<1xf32> { +// CHECK-DAG: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<1> : tensor<1xi64>}> : () -> tensor<1xi64> +// CHECK: %[[VAL_3:.*]] = "tf.Max"(%[[VAL_0]], %[[VAL_2]]) <{keep_dims = false}> : (tensor<1x256xf32>, tensor<1xi64>) -> tensor<1xf32> +// CHECK: return %[[VAL_3]] : tensor<1xf32> +// CHECK: } +func.func @convert_reduce_to_max(%arg0: tensor<1x256xf32>) -> tensor<1xf32> { + // "0xFF800000" represents -INF for f32. + %0 = mhlo.constant dense<0xFF800000> : tensor + %1 = "mhlo.reduce"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %2 = mhlo.maximum %arg1, %arg2 : tensor + "mhlo.return"(%2) : (tensor) -> () + }) {dimensions = dense<1> : tensor<1xi64>} : (tensor<1x256xf32>, tensor) -> tensor<1xf32> + func.return %1 : tensor<1xf32> +} + +// CHECK-LABEL: func @convert_reduce_to_max_int( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x4xi32>) -> tensor<1xi32> { +// CHECK-DAG: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<1> : tensor<1xi64>}> : () -> tensor<1xi64> +// CHECK: %[[VAL_3:.*]] = "tf.Max"(%[[VAL_0]], %[[VAL_2]]) <{keep_dims = false}> : (tensor<1x4xi32>, tensor<1xi64>) -> tensor<1xi32> +// CHECK: return %[[VAL_3]] : tensor<1xi32> +func.func @convert_reduce_to_max_int(%arg0: tensor<1x4xi32>) -> tensor<1xi32> { + // -2147483648 is MIN for INT32 + %0 = mhlo.constant dense<-2147483648> : tensor + %1 = mhlo.reduce(%arg0 init: %0) across dimensions = [1] : (tensor<1x4xi32>, tensor) -> tensor<1xi32> + reducer(%arg2: tensor, %arg3: tensor) { + %892 = mhlo.maximum %arg2, %arg3 : tensor + "mhlo.return"(%892) : (tensor) -> () + } + func.return %1 : tensor<1xi32> +} + +// CHECK-LABEL: func @convert_reduce_to_min( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x256xf32>) -> tensor<1xf32> { +// CHECK-DAG: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<1> : tensor<1xi64>}> : () -> tensor<1xi64> +// CHECK: %[[VAL_3:.*]] = "tf.Min"(%[[VAL_0]], %[[VAL_2]]) <{keep_dims = false}> : (tensor<1x256xf32>, tensor<1xi64>) -> tensor<1xf32> +// CHECK: return %[[VAL_3]] : tensor<1xf32> +// CHECK: } +func.func @convert_reduce_to_min(%arg0: tensor<1x256xf32>) -> tensor<1xf32> { + // "0x7F800000" represents INF for f32. + %0 = mhlo.constant dense<0x7F800000> : tensor + %1 = "mhlo.reduce"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %2 = mhlo.minimum %arg1, %arg2 : tensor + "mhlo.return"(%2) : (tensor) -> () + }) {dimensions = dense<1> : tensor<1xi64>} : (tensor<1x256xf32>, tensor) -> tensor<1xf32> + func.return %1 : tensor<1xf32> +} + +// CHECK-LABEL: func @convert_reduce_to_min_int( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x4xi32>) -> tensor<1xi32> { +// CHECK-DAG: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<1> : tensor<1xi64>}> : () -> tensor<1xi64> +// CHECK: %[[VAL_3:.*]] = "tf.Min"(%[[VAL_0]], %[[VAL_2]]) <{keep_dims = false}> : (tensor<1x4xi32>, tensor<1xi64>) -> tensor<1xi32> +// CHECK: return %[[VAL_3]] : tensor<1xi32> +func.func @convert_reduce_to_min_int(%arg0: tensor<1x4xi32>) -> tensor<1xi32> { + // 2147483647 is MAX for INT32 + %0 = mhlo.constant dense<2147483647> : tensor + %1 = mhlo.reduce(%arg0 init: %0) across dimensions = [1] : (tensor<1x4xi32>, tensor) -> tensor<1xi32> + reducer(%arg2: tensor, %arg3: tensor) { + %892 = mhlo.minimum %arg2, %arg3 : tensor + "mhlo.return"(%892) : (tensor) -> () + } + func.return %1 : tensor<1xi32> +} + +// CHECK-LABEL: func @convert_iota_1d() -> tensor<123xf32> { +// CHECK-DAG: %[[VAL_0:.*]] = "tf.Const"() <{value = dense<0.000000e+00> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_1:.*]] = "tf.Const"() <{value = dense<1.230000e+02> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<1.000000e+00> : tensor}> : () -> tensor +// CHECK: %[[VAL_3:.*]] = "tf.Range"(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]]) : (tensor, tensor, tensor) -> tensor<123xf32> +// CHECK: return %[[VAL_3]] : tensor<123xf32> +// CHECK: } +func.func @convert_iota_1d() -> tensor<123xf32> { + %0 = "mhlo.iota"() <{ iota_dimension = 0 : i64 }> : () -> tensor<123xf32> + func.return %0 : tensor<123xf32> +} + +// CHECK-LABEL: func @convert_iota_3d() -> tensor<5x7x9xi32> { +// CHECK-DAG: %[[VAL_0:.*]] = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_1:.*]] = "tf.Const"() <{value = dense<7> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<1> : tensor}> : () -> tensor +// CHECK: %[[VAL_3:.*]] = "tf.Range"(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]]) : (tensor, tensor, tensor) -> tensor<7xi32> +// CHECK: %[[VAL_4:.*]] = "tf.Const"() <{value = dense<[1, 7, 1]> : tensor<3xi64>}> : () -> tensor<3xi64> +// CHECK: %[[VAL_5:.*]] = "tf.Reshape"(%[[VAL_3]], %[[VAL_4]]) : (tensor<7xi32>, tensor<3xi64>) -> tensor<1x7x1xi32> +// CHECK: %[[VAL_6:.*]] = "tf.Const"() <{value = dense<[5, 7, 9]> : tensor<3xi64>}> : () -> tensor<3xi64> +// CHECK: %[[VAL_7:.*]] = "tf.BroadcastTo"(%[[VAL_5]], %[[VAL_6]]) : (tensor<1x7x1xi32>, tensor<3xi64>) -> tensor<5x7x9xi32> +// CHECK: return %[[VAL_7]] : tensor<5x7x9xi32> +// CHECK: } +func.func @convert_iota_3d() -> tensor<5x7x9xi32> { + %0 = "mhlo.iota"() <{ iota_dimension = 1 : i64 }> : () -> tensor<5x7x9xi32> + func.return %0 : tensor<5x7x9xi32> +} + +// CHECK-LABEL: func @convert_iota_ui64() -> tensor<123xui64> { +// CHECK-DAG: %[[VAL_0:.*]] = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_1:.*]] = "tf.Const"() <{value = dense<123> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<1> : tensor}> : () -> tensor +// CHECK: %[[VAL_3:.*]] = "tf.Range"(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]]) : (tensor, tensor, tensor) -> tensor<123xui64> +// CHECK: return %[[VAL_3]] : tensor<123xui64> +// CHECK: } +func.func @convert_iota_ui64() -> tensor<123xui64> { + %0 = "mhlo.iota"() <{ iota_dimension = 0 : i64 }> : () -> tensor<123xui64> + func.return %0 : tensor<123xui64> +} + +// CHECK-LABEL: func @no_convert_iota_ui8 +func.func @no_convert_iota_ui8() -> tensor<123xui8> { + // CHECK: "mhlo.iota" + %0 = "mhlo.iota"() <{ iota_dimension = 0 : i64 }> : () -> tensor<123xui8> + func.return %0 : tensor<123xui8> +} + +// CHECK-LABEL: func @convert_avgpool_valid( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<4x16x16x8xf32>) -> tensor<4x7x7x8xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.AvgPool"(%[[VAL_0]]) <{data_format = "NHWC", ksize = [1, 3, 3, 1], padding = "VALID", strides = [1, 2, 2, 1]}> : (tensor<4x16x16x8xf32>) -> tensor<4x7x7x8xf32> +// CHECK: return %[[VAL_1]] : tensor<4x7x7x8xf32> +// CHECK: } +func.func @convert_avgpool_valid(%arg0: tensor<4x16x16x8xf32>) -> tensor<4x7x7x8xf32> { + %0 = mhlo.constant dense<0.0> : tensor + %1 = mhlo.constant dense<9.0> : tensor<4x7x7x8xf32> + %2 = "mhlo.reduce_window"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %5 = mhlo.add %arg1, %arg2 : tensor + "mhlo.return"(%5) : (tensor) -> () + }) { + base_dilations = dense<1> : tensor<4xi64>, + padding = dense<0> : tensor<4x2xi64>, + window_dilations = dense<1> : tensor<4xi64>, + window_dimensions = dense<[1, 3, 3, 1]> : tensor<4xi64>, + window_strides = dense<[1, 2, 2, 1]> : tensor<4xi64>} : (tensor<4x16x16x8xf32>, tensor) -> tensor<4x7x7x8xf32> + %3 = mhlo.divide %2, %1 : tensor<4x7x7x8xf32> + func.return %3 : tensor<4x7x7x8xf32> +} + +// CHECK-LABEL: func @convert_avgpool_valid_broadcasted_divisor( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<4x16x16x8xf32>) -> tensor<4x7x7x8xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.AvgPool"(%[[VAL_0]]) <{data_format = "NHWC", ksize = [1, 3, 3, 1], padding = "VALID", strides = [1, 2, 2, 1]}> : (tensor<4x16x16x8xf32>) -> tensor<4x7x7x8xf32> +// CHECK: return %[[VAL_1]] : tensor<4x7x7x8xf32> +// CHECK: } +func.func @convert_avgpool_valid_broadcasted_divisor(%arg0: tensor<4x16x16x8xf32>) -> tensor<4x7x7x8xf32> { + %0 = mhlo.constant dense<0.0> : tensor + %1 = mhlo.constant dense<9.0> : tensor + %2 = "mhlo.broadcast_in_dim"(%1) <{broadcast_dimensions = dense<> : tensor<0xi64>}> : (tensor) -> tensor<4x7x7x8xf32> + %3 = "mhlo.reduce_window"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %5 = mhlo.add %arg1, %arg2 : tensor + "mhlo.return"(%5) : (tensor) -> () + }) { + base_dilations = dense<1> : tensor<4xi64>, + padding = dense<0> : tensor<4x2xi64>, + window_dilations = dense<1> : tensor<4xi64>, + window_dimensions = dense<[1, 3, 3, 1]> : tensor<4xi64>, + window_strides = dense<[1, 2, 2, 1]> : tensor<4xi64>} : (tensor<4x16x16x8xf32>, tensor) -> tensor<4x7x7x8xf32> + %4 = mhlo.divide %3, %2 : tensor<4x7x7x8xf32> + func.return %4 : tensor<4x7x7x8xf32> +} + +// CHECK-LABEL: func @convert_avgpool_valid_channel_first( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<4x3x16x16xf32>) -> tensor<4x3x7x7xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.AvgPool"(%[[VAL_0]]) <{data_format = "NCHW", ksize = [1, 1, 3, 3], padding = "VALID", strides = [1, 1, 2, 2]}> : (tensor<4x3x16x16xf32>) -> tensor<4x3x7x7xf32> +// CHECK: return %[[VAL_1]] : tensor<4x3x7x7xf32> +// CHECK: } +func.func @convert_avgpool_valid_channel_first(%arg0: tensor<4x3x16x16xf32>) -> tensor<4x3x7x7xf32> { + %0 = mhlo.constant dense<9.0> : tensor<4x3x7x7xf32> + %1 = mhlo.constant dense<0.0> : tensor + %2 = "mhlo.reduce_window"(%arg0, %1) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %4 = mhlo.add %arg1, %arg2 : tensor + mhlo.return %4 : tensor + }) { + base_dilations = dense<1> : tensor<4xi64>, + padding = dense<0> : tensor<4x2xi64>, + window_dilations = dense<1> : tensor<4xi64>, + window_dimensions = dense<[1, 1, 3, 3]> : tensor<4xi64>, + window_strides = dense<[1, 1, 2, 2]> : tensor<4xi64>} : (tensor<4x3x16x16xf32>, tensor) -> tensor<4x3x7x7xf32> + %3 = mhlo.divide %2, %0 : tensor<4x3x7x7xf32> + func.return %3 : tensor<4x3x7x7xf32> +} + +// CHECK-LABEL: func @convert_avgpool_valid_rw( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<4x16x16x8xf32>) -> tensor<4x7x7x8xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.AvgPool"(%[[VAL_0]]) <{data_format = "NHWC", ksize = [1, 3, 3, 1], padding = "VALID", strides = [1, 2, 2, 1]}> : (tensor<4x16x16x8xf32>) -> tensor<4x7x7x8xf32> +// CHECK: return %[[VAL_1]] : tensor<4x7x7x8xf32> +// CHECK: } +func.func @convert_avgpool_valid_rw(%arg0: tensor<4x16x16x8xf32>) -> tensor<4x7x7x8xf32> { + %0 = mhlo.constant dense<1.0> : tensor<4x16x16x8xf32> + %1 = mhlo.constant dense<0.0> : tensor + %2 = "mhlo.reduce_window"(%arg0, %1) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %6 = mhlo.add %arg1, %arg2 : tensor + "mhlo.return"(%6) : (tensor) -> () + }) { + base_dilations = dense<1> : tensor<4xi64>, + padding = dense<[[0, 0], [0, 0], [0, 0], [0, 0]]> : tensor<4x2xi64>, + window_dilations = dense<1> : tensor<4xi64>, + window_dimensions = dense<[1, 3, 3, 1]> : tensor<4xi64>, + window_strides = dense<[1, 2, 2, 1]> : tensor<4xi64>} : (tensor<4x16x16x8xf32>, tensor) -> tensor<4x7x7x8xf32> + %3 = "mhlo.reduce_window"(%0, %1) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %6 = mhlo.add %arg1, %arg2 : tensor + "mhlo.return"(%6) : (tensor) -> () + }) { + base_dilations = dense<1> : tensor<4xi64>, + padding = dense<[[0, 0], [0, 0], [0, 0], [0, 0]]> : tensor<4x2xi64>, + window_dilations = dense<1> : tensor<4xi64>, + window_dimensions = dense<[1, 3, 3, 1]> : tensor<4xi64>, + window_strides = dense<[1, 2, 2, 1]> : tensor<4xi64>} : (tensor<4x16x16x8xf32>, tensor) -> tensor<4x7x7x8xf32> + %4 = mhlo.divide %2, %3 : tensor<4x7x7x8xf32> + func.return %4 : tensor<4x7x7x8xf32> +} + +// CHECK-LABEL: func @convert_avgpool_valid_rw_broadcasted_const_lhs( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<4x16x16x8xf32>) -> tensor<4x7x7x8xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.AvgPool"(%[[VAL_0]]) <{data_format = "NHWC", ksize = [1, 3, 3, 1], padding = "VALID", strides = [1, 2, 2, 1]}> : (tensor<4x16x16x8xf32>) -> tensor<4x7x7x8xf32> +// CHECK: return %[[VAL_1]] : tensor<4x7x7x8xf32> +// CHECK: } +func.func @convert_avgpool_valid_rw_broadcasted_const_lhs(%arg0: tensor<4x16x16x8xf32>) -> tensor<4x7x7x8xf32> { + %0 = mhlo.constant dense<1.0> : tensor + %1 = "mhlo.broadcast_in_dim"(%0) <{broadcast_dimensions = dense<> : tensor<0xi64>}> : (tensor) -> tensor<4x16x16x8xf32> + %2 = mhlo.constant dense<0.0> : tensor + %3 = "mhlo.reduce_window"(%arg0, %2) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %6 = mhlo.add %arg1, %arg2 : tensor + "mhlo.return"(%6) : (tensor) -> () + }) { + base_dilations = dense<1> : tensor<4xi64>, + padding = dense<[[0, 0], [0, 0], [0, 0], [0, 0]]> : tensor<4x2xi64>, + window_dilations = dense<1> : tensor<4xi64>, + window_dimensions = dense<[1, 3, 3, 1]> : tensor<4xi64>, + window_strides = dense<[1, 2, 2, 1]> : tensor<4xi64>} : (tensor<4x16x16x8xf32>, tensor) -> tensor<4x7x7x8xf32> + %4 = "mhlo.reduce_window"(%1, %2) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %6 = mhlo.add %arg1, %arg2 : tensor + "mhlo.return"(%6) : (tensor) -> () + }) { + base_dilations = dense<1> : tensor<4xi64>, + padding = dense<[[0, 0], [0, 0], [0, 0], [0, 0]]> : tensor<4x2xi64>, + window_dilations = dense<1> : tensor<4xi64>, + window_dimensions = dense<[1, 3, 3, 1]> : tensor<4xi64>, + window_strides = dense<[1, 2, 2, 1]> : tensor<4xi64>} : (tensor<4x16x16x8xf32>, tensor) -> tensor<4x7x7x8xf32> + %5 = mhlo.divide %3, %4 : tensor<4x7x7x8xf32> + func.return %5 : tensor<4x7x7x8xf32> +} + +// CHECK-LABEL: func @convert_avgpool_valid_3d( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<4x16x16x16x8xf32>) -> tensor<4x7x7x7x8xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.AvgPool3D"(%[[VAL_0]]) <{data_format = "NDHWC", ksize = [1, 3, 3, 3, 1], padding = "VALID", strides = [1, 2, 2, 2, 1]}> : (tensor<4x16x16x16x8xf32>) -> tensor<4x7x7x7x8xf32> +// CHECK: return %[[VAL_1]] : tensor<4x7x7x7x8xf32> +// CHECK: } +func.func @convert_avgpool_valid_3d(%arg0: tensor<4x16x16x16x8xf32>) -> tensor<4x7x7x7x8xf32> { + %0 = mhlo.constant dense<0.0> : tensor + %1 = mhlo.constant dense<27.0> : tensor<4x7x7x7x8xf32> + %2 = "mhlo.reduce_window"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %5 = mhlo.add %arg1, %arg2 : tensor + "mhlo.return"(%5) : (tensor) -> () + }) { + base_dilations = dense<1> : tensor<5xi64>, + padding = dense<0> : tensor<5x2xi64>, + window_dilations = dense<1> : tensor<5xi64>, + window_dimensions = dense<[1, 3, 3, 3, 1]> : tensor<5xi64>, + window_strides = dense<[1, 2, 2, 2, 1]> : tensor<5xi64>} : (tensor<4x16x16x16x8xf32>, tensor) -> tensor<4x7x7x7x8xf32> + %3 = mhlo.divide %2, %1 : tensor<4x7x7x7x8xf32> + func.return %3 : tensor<4x7x7x7x8xf32> +} + +// CHECK-LABEL: func @convert_avgpool_valid_3d_channel_first( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<4x3x16x16x16xf32>) -> tensor<4x3x7x7x7xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.AvgPool3D"(%[[VAL_0]]) <{data_format = "NCDHW", ksize = [1, 1, 3, 3, 3], padding = "VALID", strides = [1, 1, 2, 2, 2]}> : (tensor<4x3x16x16x16xf32>) -> tensor<4x3x7x7x7xf32> +// CHECK: return %[[VAL_1]] : tensor<4x3x7x7x7xf32> +// CHECK: } +func.func @convert_avgpool_valid_3d_channel_first(%arg0: tensor<4x3x16x16x16xf32>) -> tensor<4x3x7x7x7xf32> { + %0 = mhlo.constant dense<27.0> : tensor<4x3x7x7x7xf32> + %1 = mhlo.constant dense<0.0> : tensor + %2 = "mhlo.reduce_window"(%arg0, %1) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %4 = mhlo.add %arg1, %arg2 : tensor + mhlo.return %4 : tensor + }) { + base_dilations = dense<1> : tensor<5xi64>, + padding = dense<0> : tensor<5x2xi64>, + window_dilations = dense<1> : tensor<5xi64>, + window_dimensions = dense<[1, 1, 3, 3, 3]> : tensor<5xi64>, + window_strides = dense<[1, 1, 2, 2, 2]> : tensor<5xi64>} : (tensor<4x3x16x16x16xf32>, tensor) -> tensor<4x3x7x7x7xf32> + %3 = mhlo.divide %2, %0 : tensor<4x3x7x7x7xf32> + func.return %3 : tensor<4x3x7x7x7xf32> +} + +// CHECK-LABEL: func @convert_avgpool_same( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<4x16x16x8xf32>) -> tensor<4x8x8x8xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.AvgPool"(%[[VAL_0]]) <{data_format = "NHWC", ksize = [1, 3, 3, 1], padding = "SAME", strides = [1, 2, 2, 1]}> : (tensor<4x16x16x8xf32>) -> tensor<4x8x8x8xf32> +// CHECK: return %[[VAL_1]] : tensor<4x8x8x8xf32> +// CHECK: } +func.func @convert_avgpool_same(%arg0: tensor<4x16x16x8xf32>) -> tensor<4x8x8x8xf32> { + %0 = mhlo.constant dense<1.0> : tensor<4x16x16x8xf32> + %1 = mhlo.constant dense<0.0> : tensor + %2 = "mhlo.reduce_window"(%arg0, %1) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %6 = mhlo.add %arg1, %arg2 : tensor + "mhlo.return"(%6) : (tensor) -> () + }) { + base_dilations = dense<1> : tensor<4xi64>, + padding = dense<[[0, 0], [0, 1], [0, 1], [0, 0]]> : tensor<4x2xi64>, + window_dilations = dense<1> : tensor<4xi64>, + window_dimensions = dense<[1, 3, 3, 1]> : tensor<4xi64>, + window_strides = dense<[1, 2, 2, 1]> : tensor<4xi64>} : (tensor<4x16x16x8xf32>, tensor) -> tensor<4x8x8x8xf32> + %3 = "mhlo.reduce_window"(%0, %1) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %6 = mhlo.add %arg1, %arg2 : tensor + "mhlo.return"(%6) : (tensor) -> () + }) { + base_dilations = dense<1> : tensor<4xi64>, + padding = dense<[[0, 0], [0, 1], [0, 1], [0, 0]]> : tensor<4x2xi64>, + window_dilations = dense<1> : tensor<4xi64>, + window_dimensions = dense<[1, 3, 3, 1]> : tensor<4xi64>, + window_strides = dense<[1, 2, 2, 1]> : tensor<4xi64>} : (tensor<4x16x16x8xf32>, tensor) -> tensor<4x8x8x8xf32> + %4 = mhlo.divide %2, %3 : tensor<4x8x8x8xf32> + func.return %4 : tensor<4x8x8x8xf32> +} + +// CHECK-LABEL: func @convert_avgpool_reshape_broadcast( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<4x16x16x8xf32>) -> tensor<4x8x8x8xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.AvgPool"(%[[VAL_0]]) <{data_format = "NHWC", ksize = [1, 3, 3, 1], padding = "SAME", strides = [1, 2, 2, 1]}> : (tensor<4x16x16x8xf32>) -> tensor<4x8x8x8xf32> +// CHECK: return %[[VAL_1]] : tensor<4x8x8x8xf32> +// CHECK: } +func.func @convert_avgpool_reshape_broadcast(%arg0: tensor<4x16x16x8xf32>) -> tensor<4x8x8x8xf32> { + %0 = mhlo.constant dense<1.000000e+00> : tensor<1x16x16x1xf32> + %1 = mhlo.constant dense<0.000000e+00> : tensor + %2 = "mhlo.reduce_window"(%arg0, %1) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %7 = mhlo.add %arg1, %arg2 : tensor + mhlo.return %7 : tensor + }) {base_dilations = dense<1> : tensor<4xi64>, padding = dense<[[0, 0], [0, 1], [0, 1], [0, 0]]> : tensor<4x2xi64>, window_dilations = dense<1> : tensor<4xi64>, window_dimensions = dense<[1, 3, 3, 1]> : tensor<4xi64>, window_strides = dense<[1, 2, 2, 1]> : tensor<4xi64>} : (tensor<4x16x16x8xf32>, tensor) -> tensor<4x8x8x8xf32> + %3 = "mhlo.reduce_window"(%0, %1) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %7 = mhlo.add %arg1, %arg2 : tensor + mhlo.return %7 : tensor + }) {base_dilations = dense<1> : tensor<4xi64>, padding = dense<[[0, 0], [0, 1], [0, 1], [0, 0]]> : tensor<4x2xi64>, window_dilations = dense<1> : tensor<4xi64>, window_dimensions = dense<[1, 3, 3, 1]> : tensor<4xi64>, window_strides = dense<[1, 2, 2, 1]> : tensor<4xi64>} : (tensor<1x16x16x1xf32>, tensor) -> tensor<1x8x8x1xf32> + %4 = mhlo.reshape %3 : (tensor<1x8x8x1xf32>) -> tensor<8x8xf32> + %5 = "mhlo.broadcast_in_dim"(%4) <{broadcast_dimensions = dense<[1, 2]> : tensor<2xi64>}> : (tensor<8x8xf32>) -> tensor<4x8x8x8xf32> + %6 = mhlo.divide %2, %5 : tensor<4x8x8x8xf32> + return %6 : tensor<4x8x8x8xf32> +} + +// CHECK-LABEL: func @convert_maxpool_valid( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<4x16x16x8xf32>) -> tensor<4x7x7x8xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.MaxPool"(%[[VAL_0]]) <{data_format = "NHWC", explicit_paddings = [], ksize = [1, 3, 3, 1], padding = "VALID", strides = [1, 2, 2, 1]}> : (tensor<4x16x16x8xf32>) -> tensor<4x7x7x8xf32> +// CHECK: return %[[VAL_1]] : tensor<4x7x7x8xf32> +// CHECK: } +func.func @convert_maxpool_valid(%arg0: tensor<4x16x16x8xf32>) -> tensor<4x7x7x8xf32> { + // "0xFF800000" represents -INF for f32. + %0 = mhlo.constant dense<0xFF800000> : tensor + %1 = "mhlo.reduce_window"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %5 = mhlo.maximum %arg1, %arg2 : tensor + "mhlo.return"(%5) : (tensor) -> () + }) { + base_dilations = dense<1> : tensor<4xi64>, + padding = dense<0> : tensor<4x2xi64>, + window_dilations = dense<1> : tensor<4xi64>, + window_dimensions = dense<[1, 3, 3, 1]> : tensor<4xi64>, + window_strides = dense<[1, 2, 2, 1]> : tensor<4xi64>} : (tensor<4x16x16x8xf32>, tensor) -> tensor<4x7x7x8xf32> + func.return %1 : tensor<4x7x7x8xf32> +} + +// CHECK-LABEL: func @convert_maxpool_valid_channel_first( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<4x3x16x16xf32>) -> tensor<4x3x7x7xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.MaxPool"(%[[VAL_0]]) <{data_format = "NCHW", explicit_paddings = [], ksize = [1, 1, 3, 3], padding = "VALID", strides = [1, 1, 2, 2]}> : (tensor<4x3x16x16xf32>) -> tensor<4x3x7x7xf32> +// CHECK: return %[[VAL_1]] : tensor<4x3x7x7xf32> +// CHECK: } +func.func @convert_maxpool_valid_channel_first(%arg0: tensor<4x3x16x16xf32>) -> tensor<4x3x7x7xf32> { + // "0xFF800000" represents -INF for f32. + %0 = mhlo.constant dense<0xFF800000> : tensor + %1 = "mhlo.reduce_window"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %2 = mhlo.maximum %arg1, %arg2 : tensor + mhlo.return %2 : tensor + }) { + base_dilations = dense<1> : tensor<4xi64>, + padding = dense<0> : tensor<4x2xi64>, + window_dilations = dense<1> : tensor<4xi64>, + window_dimensions = dense<[1, 1, 3, 3]> : tensor<4xi64>, + window_strides = dense<[1, 1, 2, 2]> : tensor<4xi64>} : (tensor<4x3x16x16xf32>, tensor) -> tensor<4x3x7x7xf32> + func.return %1 : tensor<4x3x7x7xf32> +} + +// CHECK-LABEL: func @convert_maxpool_valid_3d( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<4x16x16x16x8xf32>) -> tensor<4x7x7x7x8xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.MaxPool3D"(%[[VAL_0]]) <{data_format = "NDHWC", ksize = [1, 3, 3, 3, 1], padding = "VALID", strides = [1, 2, 2, 2, 1]}> : (tensor<4x16x16x16x8xf32>) -> tensor<4x7x7x7x8xf32> +// CHECK: return %[[VAL_1]] : tensor<4x7x7x7x8xf32> +// CHECK: } +func.func @convert_maxpool_valid_3d(%arg0: tensor<4x16x16x16x8xf32>) -> tensor<4x7x7x7x8xf32> { + // "0xFF800000" represents -INF for f32. + %0 = mhlo.constant dense<0xFF800000> : tensor + %1 = "mhlo.reduce_window"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %5 = mhlo.maximum %arg1, %arg2 : tensor + "mhlo.return"(%5) : (tensor) -> () + }) { + base_dilations = dense<1> : tensor<5xi64>, + padding = dense<0> : tensor<5x2xi64>, + window_dilations = dense<1> : tensor<5xi64>, + window_dimensions = dense<[1, 3, 3, 3, 1]> : tensor<5xi64>, + window_strides = dense<[1, 2, 2, 2, 1]> : tensor<5xi64>} : (tensor<4x16x16x16x8xf32>, tensor) -> tensor<4x7x7x7x8xf32> + func.return %1 : tensor<4x7x7x7x8xf32> +} + +// CHECK-LABEL: func @convert_maxpool_valid_3d_channel_first( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<4x3x16x16x16xf32>) -> tensor<4x3x7x7x7xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.MaxPool3D"(%[[VAL_0]]) <{data_format = "NCDHW", ksize = [1, 1, 3, 3, 3], padding = "VALID", strides = [1, 1, 2, 2, 2]}> : (tensor<4x3x16x16x16xf32>) -> tensor<4x3x7x7x7xf32> +// CHECK: return %[[VAL_1]] : tensor<4x3x7x7x7xf32> +// CHECK: } +func.func @convert_maxpool_valid_3d_channel_first(%arg0: tensor<4x3x16x16x16xf32>) -> tensor<4x3x7x7x7xf32> { + // "0xFF800000" represents -INF for f32. + %0 = mhlo.constant dense<0xFF800000> : tensor + %1 = "mhlo.reduce_window"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %2 = mhlo.maximum %arg1, %arg2 : tensor + mhlo.return %2 : tensor + }) { + base_dilations = dense<1> : tensor<5xi64>, + padding = dense<0> : tensor<5x2xi64>, + window_dilations = dense<1> : tensor<5xi64>, + window_dimensions = dense<[1, 1, 3, 3, 3]> : tensor<5xi64>, + window_strides = dense<[1, 1, 2, 2, 2]> : tensor<5xi64>} : (tensor<4x3x16x16x16xf32>, tensor) -> tensor<4x3x7x7x7xf32> + func.return %1 : tensor<4x3x7x7x7xf32> +} + +// CHECK-LABEL: func @convert_maxpool_same( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<4x16x16x8xf32>) -> tensor<4x8x8x8xf32> { +// CHECK: %[[VAL_1:.*]] = "tf.MaxPool"(%[[VAL_0]]) <{data_format = "NHWC", explicit_paddings = [], ksize = [1, 3, 3, 1], padding = "SAME", strides = [1, 2, 2, 1]}> : (tensor<4x16x16x8xf32>) -> tensor<4x8x8x8xf32> +// CHECK: return %[[VAL_1]] : tensor<4x8x8x8xf32> +// CHECK: } +func.func @convert_maxpool_same(%arg0: tensor<4x16x16x8xf32>) -> tensor<4x8x8x8xf32> { + // "0xFF800000" represents -INF for f32. + %0 = mhlo.constant dense<0xFF800000> : tensor + %1 = "mhlo.reduce_window"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %6 = mhlo.maximum %arg1, %arg2 : tensor + "mhlo.return"(%6) : (tensor) -> () + }) { + base_dilations = dense<1> : tensor<4xi64>, + padding = dense<[[0, 0], [0, 1], [0, 1], [0, 0]]> : tensor<4x2xi64>, + window_dilations = dense<1> : tensor<4xi64>, + window_dimensions = dense<[1, 3, 3, 1]> : tensor<4xi64>, + window_strides = dense<[1, 2, 2, 1]> : tensor<4xi64>} : (tensor<4x16x16x8xf32>, tensor) -> tensor<4x8x8x8xf32> + func.return %1 : tensor<4x8x8x8xf32> +} + +// CHECK-LABEL: func @convert_pad( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<8x128xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor) -> tensor<11x131xf32> { +// CHECK: %[[VAL_2:.*]] = arith.constant dense<{{\[\[}}1, 2], [0, 3]]> : tensor<2x2xi64> +// CHECK: %[[VAL_3:.*]] = "tf.PadV2"(%[[VAL_0]], %[[VAL_2]], %[[VAL_1]]) : (tensor<8x128xf32>, tensor<2x2xi64>, tensor) -> tensor<11x131xf32> +// CHECK: return %[[VAL_3]] : tensor<11x131xf32> +// CHECK: } +func.func @convert_pad(%arg0: tensor<8x128xf32>, %arg1: tensor) -> tensor<11x131xf32> { + %0 = "mhlo.pad"(%arg0, %arg1) { + edge_padding_low = dense<[1, 0]> : tensor<2xi64>, + edge_padding_high = dense<[2, 3]> : tensor<2xi64>, + interior_padding = dense<0> : tensor<2xi64> + } : (tensor<8x128xf32>, tensor) -> tensor<11x131xf32> + func.return %0 : tensor<11x131xf32> +} + +// CHECK-LABEL: func @convert_pad_negative_amount( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<8x128xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor) -> tensor<7x128xf32> { +// CHECK: %[[VAL_2:.*]] = arith.constant dense<{{\[\[}}0, 0], [0, 1]]> : tensor<2x2xi64> +// CHECK: %[[VAL_3:.*]] = "tf.PadV2"(%[[VAL_0]], %[[VAL_2]], %[[VAL_1]]) : (tensor<8x128xf32>, tensor<2x2xi64>, tensor) -> tensor<8x129xf32> +// CHECK: %[[VAL_4:.*]] = arith.constant dense<[0, 1]> : tensor<2xi64> +// CHECK: %[[VAL_5:.*]] = arith.constant dense<[7, 128]> : tensor<2xi64> +// CHECK: %[[VAL_6:.*]] = "tf.Slice"(%[[VAL_3]], %[[VAL_4]], %[[VAL_5]]) : (tensor<8x129xf32>, tensor<2xi64>, tensor<2xi64>) -> tensor<7x128xf32> +// CHECK: return %[[VAL_6]] : tensor<7x128xf32> +// CHECK: } +func.func @convert_pad_negative_amount(%arg0: tensor<8x128xf32>, %arg1: tensor) -> tensor<7x128xf32> { + %0 = "mhlo.pad"(%arg0, %arg1) { + edge_padding_low = dense<[0, -1]> : tensor<2xi64>, + edge_padding_high = dense<[-1, 1]> : tensor<2xi64>, + interior_padding = dense<0> : tensor<2xi64> + } : (tensor<8x128xf32>, tensor) -> tensor<7x128xf32> + func.return %0 : tensor<7x128xf32> +} + +// CHECK-LABEL: func @convert_round( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<8x128xbf16>) -> tensor<8x128xbf16> +// CHECK: %[[VAL_1:.*]] = "tf.Round"(%[[VAL_0]]) : (tensor<8x128xbf16>) -> tensor<8x128xbf16> +// CHECK: return %[[VAL_1]] +// CHECK: } +func.func @convert_round(%arg0: tensor<8x128xbf16>) -> tensor<8x128xbf16> { + %0 = mhlo.constant dense<2.000000e+00> : tensor<8x128xbf16> + %1 = mhlo.constant dense<5.000000e-01> : tensor<8x128xbf16> + %2 = mhlo.constant dense<1.000000e+00> : tensor<8x128xbf16> + %3 = "mhlo.floor"(%arg0) : (tensor<8x128xbf16>) -> tensor<8x128xbf16> + %4 = mhlo.subtract %arg0, %3 : tensor<8x128xbf16> + %5 = "mhlo.compare"(%4, %1) {comparison_direction = #mhlo} : (tensor<8x128xbf16>, tensor<8x128xbf16>) -> tensor<8x128xi1> + %6 = "mhlo.compare"(%4, %1) {comparison_direction = #mhlo} : (tensor<8x128xbf16>, tensor<8x128xbf16>) -> tensor<8x128xi1> + %7 = mhlo.multiply %arg0, %1 : tensor<8x128xbf16> + %8 = "mhlo.floor"(%7) : (tensor<8x128xbf16>) -> tensor<8x128xbf16> + %9 = mhlo.multiply %8, %0 : tensor<8x128xbf16> + %10 = mhlo.subtract %3, %9 : tensor<8x128xbf16> + %11 = "mhlo.compare"(%10, %2) {comparison_direction = #mhlo} : (tensor<8x128xbf16>, tensor<8x128xbf16>) -> tensor<8x128xi1> + %12 = mhlo.and %6, %11 : tensor<8x128xi1> + %13 = mhlo.or %5, %12 : tensor<8x128xi1> + %14 = mhlo.add %3, %2 : tensor<8x128xbf16> + %15 = "mhlo.select"(%13, %14, %3) : (tensor<8x128xi1>, tensor<8x128xbf16>, tensor<8x128xbf16>) -> tensor<8x128xbf16> + func.return %15 : tensor<8x128xbf16> +} + +// CHECK-LABEL: func @convert_floor_mod_float +// CHECK: %[[RESULT:.*]] = "tf.FloorMod"(%arg0, %arg1) : (tensor<192x8xbf16>, tensor<192x8xbf16>) -> tensor<192x8xbf16> +// CHECK: return %[[RESULT]] +// CHECK: } +func.func @convert_floor_mod_float(%arg0: tensor<192x8xbf16>, %arg1: tensor<192x8xbf16>) -> tensor<192x8xbf16> { + %0 = mhlo.constant dense<0.000000e+00> : tensor<192x8xbf16> + %1 = mhlo.remainder %arg0, %arg1 : tensor<192x8xbf16> + %2 = "mhlo.compare"(%1, %0) {comparison_direction = #mhlo} : (tensor<192x8xbf16>, tensor<192x8xbf16>) -> tensor<192x8xi1> + %3 = "mhlo.compare"(%arg1, %0) {comparison_direction = #mhlo} : (tensor<192x8xbf16>, tensor<192x8xbf16>) -> tensor<192x8xi1> + %4 = "mhlo.compare"(%2, %3) {comparison_direction = #mhlo} : (tensor<192x8xi1>, tensor<192x8xi1>) -> tensor<192x8xi1> + %5 = "mhlo.compare"(%1, %0) {comparison_direction = #mhlo} : (tensor<192x8xbf16>, tensor<192x8xbf16>) -> tensor<192x8xi1> + %6 = mhlo.and %4, %5 : tensor<192x8xi1> + %7 = mhlo.add %1, %arg1 : tensor<192x8xbf16> + %8 = "mhlo.select"(%6, %7, %1) : (tensor<192x8xi1>, tensor<192x8xbf16>, tensor<192x8xbf16>) -> tensor<192x8xbf16> + func.return %8 : tensor<192x8xbf16> +} + +// CHECK-LABEL: func @convert_floor_mod_int +// CHECK: %[[RESULT:.*]] = "tf.FloorMod"(%arg0, %arg1) : (tensor<192x8xi32>, tensor<192x8xi32>) -> tensor<192x8xi32> +// CHECK: return %[[RESULT]] +// CHECK: } +func.func @convert_floor_mod_int(%arg0: tensor<192x8xi32>, %arg1: tensor<192x8xi32>) -> tensor<192x8xi32> { + %0 = mhlo.constant dense<0> : tensor<192x8xi32> + %1 = mhlo.remainder %arg0, %arg1 : tensor<192x8xi32> + %2 = "mhlo.compare"(%1, %0) {comparison_direction = #mhlo} : (tensor<192x8xi32>, tensor<192x8xi32>) -> tensor<192x8xi1> + %3 = "mhlo.compare"(%arg1, %0) {comparison_direction = #mhlo} : (tensor<192x8xi32>, tensor<192x8xi32>) -> tensor<192x8xi1> + %4 = "mhlo.compare"(%2, %3) {comparison_direction = #mhlo} : (tensor<192x8xi1>, tensor<192x8xi1>) -> tensor<192x8xi1> + %5 = "mhlo.compare"(%1, %0) {comparison_direction = #mhlo} : (tensor<192x8xi32>, tensor<192x8xi32>) -> tensor<192x8xi1> + %6 = mhlo.and %4, %5 : tensor<192x8xi1> + %7 = mhlo.add %1, %arg1 : tensor<192x8xi32> + %8 = "mhlo.select"(%6, %7, %1) : (tensor<192x8xi1>, tensor<192x8xi32>, tensor<192x8xi32>) -> tensor<192x8xi32> + func.return %8 : tensor<192x8xi32> +} + +// CHECK-LABEL: func @convert_floor_mod_float_cst +// CHECK-DAG: %[[CST1:.*]] = "tf.Const"() <{value = dense<2.000000e+00> : tensor<192x8xbf16>}> : () -> tensor<192x8xbf16> +// CHECK-DAG: %[[CST2:.*]] = "tf.Const"() <{value = dense<2.000000e+00> : tensor<192x8xbf16>}> : () -> tensor<192x8xbf16> +// CHECK: %[[RESULT:.*]] = "tf.FloorMod"(%arg0, %[[CST2]]) : (tensor<192x8xbf16>, tensor<192x8xbf16>) -> tensor<192x8xbf16> +// CHECK: return %[[RESULT]] : tensor<192x8xbf16> +// CHECK: } +func.func @convert_floor_mod_float_cst(%arg0: tensor<192x8xbf16>) -> tensor<192x8xbf16> { + %0 = mhlo.constant dense<0.000000e+00> : tensor<192x8xbf16> + %1 = mhlo.constant dense<2.000000e+00> : tensor<192x8xbf16> + %2 = mhlo.remainder %arg0, %1 : tensor<192x8xbf16> + %3 = "mhlo.compare"(%2, %0) {comparison_direction = #mhlo} : (tensor<192x8xbf16>, tensor<192x8xbf16>) -> tensor<192x8xi1> + %4 = "mhlo.compare"(%2, %0) {comparison_direction = #mhlo} : (tensor<192x8xbf16>, tensor<192x8xbf16>) -> tensor<192x8xi1> + %5 = mhlo.and %3, %4 : tensor<192x8xi1> + %6 = mhlo.add %2, %1 : tensor<192x8xbf16> + %7 = "mhlo.select"(%5, %6, %2) : (tensor<192x8xi1>, tensor<192x8xbf16>, tensor<192x8xbf16>) -> tensor<192x8xbf16> + func.return %7 : tensor<192x8xbf16> +} + +// CHECK-LABEL: func @convert_floor_mod_int_cst +// CHECK-DAG: %[[CST1:.*]] = "tf.Const"() <{value = dense<2> : tensor<192x8xi32>}> : () -> tensor<192x8xi32> +// CHECK-DAG: %[[CST2:.*]] = "tf.Const"() <{value = dense<2> : tensor<192x8xi32>}> : () -> tensor<192x8xi32> +// CHECK: %[[RESULT:.*]] = "tf.FloorMod"(%arg0, %[[CST2]]) : (tensor<192x8xi32>, tensor<192x8xi32>) -> tensor<192x8xi32> +// CHECK: return %[[RESULT]] : tensor<192x8xi32> +// CHECK: } +func.func @convert_floor_mod_int_cst(%arg0: tensor<192x8xi32>) -> tensor<192x8xi32> { + %0 = mhlo.constant dense<0> : tensor<192x8xi32> + %1 = mhlo.constant dense<2> : tensor<192x8xi32> + %2 = mhlo.remainder %arg0, %1 : tensor<192x8xi32> + %3 = "mhlo.compare"(%2, %0) {comparison_direction = #mhlo} : (tensor<192x8xi32>, tensor<192x8xi32>) -> tensor<192x8xi1> + %4 = "mhlo.compare"(%2, %0) {comparison_direction = #mhlo} : (tensor<192x8xi32>, tensor<192x8xi32>) -> tensor<192x8xi1> + %5 = mhlo.and %3, %4 : tensor<192x8xi1> + %6 = mhlo.add %2, %1 : tensor<192x8xi32> + %7 = "mhlo.select"(%5, %6, %2) : (tensor<192x8xi1>, tensor<192x8xi32>, tensor<192x8xi32>) -> tensor<192x8xi32> + func.return %7 : tensor<192x8xi32> +} + +// CHECK-LABEL: func @convert_floor_mod_bfloat +// CHECK: %[[RESULT:.*]] = "tf.FloorMod"(%arg0, %arg1) : (tensor<10x10xbf16>, tensor<10x10xbf16>) -> tensor<10x10xbf16> +// CHECK: return %[[RESULT]] +// CHECK: } +func.func @convert_floor_mod_bfloat(%arg0: tensor<10x10xbf16>, %arg1: tensor<10x10xbf16>) -> tensor<10x10xbf16> { + %0 = mhlo.constant dense<0.000000e+00> : tensor<10x10xbf16> + %1 = mhlo.remainder %arg0, %arg1 : tensor<10x10xbf16> + %2 = mhlo.compare NE, %1, %0, FLOAT : (tensor<10x10xbf16>, tensor<10x10xbf16>) -> tensor<10x10xi1> + %3 = mhlo.compare LT, %1, %0, FLOAT : (tensor<10x10xbf16>, tensor<10x10xbf16>) -> tensor<10x10xi1> + %4 = mhlo.compare LT, %arg1, %0, FLOAT : (tensor<10x10xbf16>, tensor<10x10xbf16>) -> tensor<10x10xi1> + %5 = mhlo.compare NE, %3, %4, UNSIGNED : (tensor<10x10xi1>, tensor<10x10xi1>) -> tensor<10x10xi1> + %6 = mhlo.and %5, %2 : tensor<10x10xi1> + %7 = mhlo.add %1, %arg1 : tensor<10x10xbf16> + %8 = mhlo.select %6, %7, %1 : tensor<10x10xi1>, tensor<10x10xbf16> + return %8 : tensor<10x10xbf16> +} + +// CHECK-LABEL: func @convert_floor_div +// CHECK: %[[RESULT:.*]] = "tf.FloorDiv"(%arg0, %arg1) : (tensor<10x10xbf16>, tensor<10x10xbf16>) -> tensor<10x10xbf16> +// CHECK: return %[[RESULT]] +// CHECK: } +func.func @convert_floor_div(%arg0: tensor<10x10xbf16>, %arg1: tensor<10x10xbf16>) -> tensor<10x10xbf16> { + %0 = mhlo.constant dense<0.000000e+00> : tensor<10x10xbf16> + %1 = mhlo.constant dense<-1.000000e+00> : tensor<10x10xbf16> + %2 = mhlo.remainder %arg0, %arg1 : tensor<10x10xbf16> + %3 = "mhlo.compare"(%2, %0) {comparison_direction = #mhlo} : (tensor<10x10xbf16>, tensor<10x10xbf16>) -> tensor<10x10xi1> + %4 = "mhlo.sign"(%arg1) : (tensor<10x10xbf16>) -> tensor<10x10xbf16> + %5 = "mhlo.sign"(%2) : (tensor<10x10xbf16>) -> tensor<10x10xbf16> + %6 = "mhlo.compare"(%4, %5) {comparison_direction = #mhlo} : (tensor<10x10xbf16>, tensor<10x10xbf16>) -> tensor<10x10xi1> + %7 = mhlo.and %3, %6 : tensor<10x10xi1> + %8 = mhlo.subtract %arg0, %2 : tensor<10x10xbf16> + %9 = mhlo.divide %8, %arg1 : tensor<10x10xbf16> + %10 = mhlo.add %9, %1 : tensor<10x10xbf16> + %11 = "mhlo.select"(%7, %10, %9) : (tensor<10x10xi1>, tensor<10x10xbf16>, tensor<10x10xbf16>) -> tensor<10x10xbf16> + %12 = "mhlo.round_nearest_afz"(%11) : (tensor<10x10xbf16>) -> tensor<10x10xbf16> + %13 = "mhlo.tuple"(%12) : (tensor<10x10xbf16>) -> tuple> + func.return %12 : tensor<10x10xbf16> +} + +// CHECK-LABEL: func @convert_floor_div_cst +// CHECK: %[[CST2:.*]] = "tf.Const"() <{value = dense<2.000000e+00> : tensor<10x10xbf16>}> : () -> tensor<10x10xbf16> +// CHECK: %[[RESULT:.*]] = "tf.FloorDiv"(%arg0, %[[CST2]]) : (tensor<10x10xbf16>, tensor<10x10xbf16>) -> tensor<10x10xbf16> +// CHECK: return %[[RESULT]] +// CHECK: } +func.func @convert_floor_div_cst(%arg0: tensor<10x10xbf16>) -> tensor<10x10xbf16> { + %0 = mhlo.constant dense<2.000000e+00> : tensor<10x10xbf16> + %1 = mhlo.constant dense<0.000000e+00> : tensor<10x10xbf16> + %2 = mhlo.constant dense<1.000000e+00> : tensor<10x10xbf16> + %3 = mhlo.constant dense<5.000000e-01> : tensor<10x10xbf16> + %4 = mhlo.constant dense<-1.000000e+00> : tensor<10x10xbf16> + %5 = mhlo.remainder %arg0, %0 : tensor<10x10xbf16> + %6 = "mhlo.compare"(%5, %1) {comparison_direction = #mhlo} : (tensor<10x10xbf16>, tensor<10x10xbf16>) -> tensor<10x10xi1> + %7 = "mhlo.sign"(%5) : (tensor<10x10xbf16>) -> tensor<10x10xbf16> + %8 = "mhlo.compare"(%2, %7) {comparison_direction = #mhlo} : (tensor<10x10xbf16>, tensor<10x10xbf16>) -> tensor<10x10xi1> + %9 = mhlo.and %6, %8 : tensor<10x10xi1> + %10 = mhlo.subtract %arg0, %5 : tensor<10x10xbf16> + %11 = mhlo.multiply %10, %3 : tensor<10x10xbf16> + %12 = mhlo.add %11, %4 : tensor<10x10xbf16> + %13 = "mhlo.select"(%9, %12, %11) : (tensor<10x10xi1>, tensor<10x10xbf16>, tensor<10x10xbf16>) -> tensor<10x10xbf16> + %14 = "mhlo.round_nearest_afz"(%13) : (tensor<10x10xbf16>) -> tensor<10x10xbf16> + %15 = "mhlo.tuple"(%14) : (tensor<10x10xbf16>) -> tuple> + func.return %14 : tensor<10x10xbf16> +} + +// CHECK-LABEL: func @convert_floor_div_cst2 +// CHECK: %[[CST2:.*]] = "tf.Const"() <{value = dense<2.000000e+00> : tensor<10x10xbf16>}> : () -> tensor<10x10xbf16> +// CHECK: %[[RESULT:.*]] = "tf.FloorDiv"(%arg0, %[[CST2]]) : (tensor<10x10xbf16>, tensor<10x10xbf16>) -> tensor<10x10xbf16> +// CHECK: return %[[RESULT]] +// CHECK: } +func.func @convert_floor_div_cst2(%arg0: tensor<10x10xbf16>) -> tensor<10x10xbf16> { + %0 = mhlo.constant dense<1.000000e+00> : tensor<10x10xbf16> + %1 = mhlo.constant dense<2.000000e+00> : tensor<10x10xbf16> + %2 = mhlo.constant dense<0.000000e+00> : tensor<10x10xbf16> + %3 = mhlo.constant dense<-1.000000e+00> : tensor<10x10xbf16> + %4 = mhlo.remainder %arg0, %1 : tensor<10x10xbf16> + %5 = "mhlo.compare"(%4, %2) {comparison_direction = #mhlo} : (tensor<10x10xbf16>, tensor<10x10xbf16>) -> tensor<10x10xi1> + %6 = "mhlo.sign"(%4) : (tensor<10x10xbf16>) -> tensor<10x10xbf16> + %7 = "mhlo.compare"(%0, %6) {comparison_direction = #mhlo} : (tensor<10x10xbf16>, tensor<10x10xbf16>) -> tensor<10x10xi1> + %8 = mhlo.and %5, %7 : tensor<10x10xi1> + %9 = mhlo.subtract %arg0, %4 : tensor<10x10xbf16> + %10 = mhlo.divide %9, %1 : tensor<10x10xbf16> + %11 = mhlo.add %10, %3 : tensor<10x10xbf16> + %12 = "mhlo.select"(%8, %11, %10) : (tensor<10x10xi1>, tensor<10x10xbf16>, tensor<10x10xbf16>) -> tensor<10x10xbf16> + %13 = "mhlo.round_nearest_afz"(%12) : (tensor<10x10xbf16>) -> tensor<10x10xbf16> + %14 = "mhlo.tuple"(%13) : (tensor<10x10xbf16>) -> tuple> + func.return %13 : tensor<10x10xbf16> +} + +// CHECK-LABEL: func @convert_floor_div_broadcast_cst +// CHECK: %[[BCST:.*]] = "tf.BroadcastTo"{{.*}} : (tensor<8xf32>, tensor<2xi64>) -> tensor<10x8xf32> +// CHECK: %[[RESULT:.*]] = "tf.FloorDiv"(%arg0, %[[BCST]]) : (tensor<10x8xf32>, tensor<10x8xf32>) -> tensor<10x8xf32> +// CHECK: return %[[RESULT]] +// CHECK: } +func.func @convert_floor_div_broadcast_cst(%arg0: tensor<10x8xf32>) -> tensor<10x8xf32> { + %0 = mhlo.constant dense<1.000000e+00> : tensor<10x8xf32> + %1 = mhlo.constant dense<[1.000000e+00, 2.000000e+00, 4.000000e+00, 8.000000e+00, 1.600000e+01, 3.200000e+01, 6.400000e+01, 1.280000e+02]> : tensor<8xf32> + %2 = mhlo.constant dense<0.000000e+00> : tensor<10x8xf32> + %3 = mhlo.constant dense<-1.000000e+00> : tensor<10x8xf32> + %5 = "mhlo.broadcast_in_dim"(%1) <{broadcast_dimensions = dense<1> : tensor<1xi64>}> : (tensor<8xf32>) -> tensor<10x8xf32> + %6 = mhlo.remainder %arg0, %5 : tensor<10x8xf32> + %7 = "mhlo.compare"(%6, %2) {comparison_direction = #mhlo} : (tensor<10x8xf32>, tensor<10x8xf32>) -> tensor<10x8xi1> + %8 = "mhlo.sign"(%6) : (tensor<10x8xf32>) -> tensor<10x8xf32> + %9 = "mhlo.compare"(%0, %8) {comparison_direction = #mhlo} : (tensor<10x8xf32>, tensor<10x8xf32>) -> tensor<10x8xi1> + %10 = mhlo.and %7, %9 : tensor<10x8xi1> + %11 = mhlo.subtract %arg0, %6 : tensor<10x8xf32> + %12 = mhlo.divide %11, %5 : tensor<10x8xf32> + %13 = mhlo.add %12, %3 : tensor<10x8xf32> + %14 = "mhlo.select"(%10, %13, %12) : (tensor<10x8xi1>, tensor<10x8xf32>, tensor<10x8xf32>) -> tensor<10x8xf32> + %15 = "mhlo.round_nearest_afz"(%14) : (tensor<10x8xf32>) -> tensor<10x8xf32> + %16 = "mhlo.tuple"(%15) : (tensor<10x8xf32>) -> tuple> + func.return %15 : tensor<10x8xf32> +} + + +// CHECK-LABEL: func @convert_gather( +// CHECK-SAME: %[[ARG_0:.*]]: tensor<147456xf16>, +// CHECK-SAME: %[[ARG_1:.*]]: tensor<192x256x1xi32>) +// CHECK: %[[VAL_0:.*]] = "tf.GatherNd"(%[[ARG_0]], %[[ARG_1]]) <{bad_indices_policy = ""}> : {{.*}} -> tensor<192x256xf16> +// CHECK: return %[[VAL_0]] +// CHECK: } +func.func @convert_gather(%arg0: tensor<147456xf16>, %arg1: tensor<192x256x1xi32>) -> tensor<192x256xf16> { + %0 = "mhlo.gather"(%arg0, %arg1) { + dimension_numbers = #mhlo.gather< + collapsed_slice_dims = [0], + index_vector_dim = 2, + start_index_map = [0], + >, + indices_are_sorted = false, + slice_sizes = dense<1> : tensor<1xi64> + } : (tensor<147456xf16>, tensor<192x256x1xi32>) -> tensor<192x256xf16> + func.return %0 : tensor<192x256xf16> +} + +// CHECK-LABEL: func @convert_gather_with_ui32indices( +// CHECK-SAME: %[[ARG_0:.*]]: tensor<147456xf16>, +// CHECK-SAME: %[[ARG_1:.*]]: tensor<192x256x1xui32>) +// CHECK: %[[INDICES:.*]] = "tf.Cast"(%arg1) <{Truncate = false}> : (tensor<192x256x1xui32>) -> tensor<192x256x1xi64> +// CHECK: %[[VAL_0:.*]] = "tf.GatherNd"(%[[ARG_0]], %[[INDICES]]) <{bad_indices_policy = ""}> : {{.*}} -> tensor<192x256xf16> +// CHECK: return %[[VAL_0]] +// CHECK: } +func.func @convert_gather_with_ui32indices(%arg0: tensor<147456xf16>, %arg1: tensor<192x256x1xui32>) -> tensor<192x256xf16> { + %0 = "mhlo.gather"(%arg0, %arg1) { + dimension_numbers = #mhlo.gather< + collapsed_slice_dims = [0], + index_vector_dim = 2, + start_index_map = [0], + >, + indices_are_sorted = false, + slice_sizes = dense<1> : tensor<1xi64> + } : (tensor<147456xf16>, tensor<192x256x1xui32>) -> tensor<192x256xf16> + func.return %0 : tensor<192x256xf16> +} + +// CHECK-LABEL: func @convert_gather_nd( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<98x128xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<4x64xi32>) +// CHECK: %[[VAL_2:.*]] = arith.constant dense<[4, 64, 1]> : tensor<3xi64> +// CHECK: %[[VAL_3:.*]] = "tf.Reshape"(%[[VAL_1]], %[[VAL_2]]) : {{.*}} -> tensor<4x64x1xi32> +// CHECK: %[[VAL_4:.*]] = "tf.GatherNd"(%[[VAL_0]], %[[VAL_3]]) <{bad_indices_policy = ""}> : {{.*}} -> tensor<4x64x128xf32> +// CHECK: return %[[VAL_4]] +// CHECK: } +func.func @convert_gather_nd(%arg0: tensor<98x128xf32>, %arg1: tensor<4x64xi32>) -> tensor<4x64x128xf32> { + %0 = "mhlo.gather"(%arg0, %arg1) { + dimension_numbers = #mhlo.gather< + collapsed_slice_dims = [0], + index_vector_dim = 2, + offset_dims = [2], + start_index_map = [0], + >, + indices_are_sorted = false, + slice_sizes = dense<[1, 128]> : tensor<2xi64> + } : (tensor<98x128xf32>, tensor<4x64xi32>) -> tensor<4x64x128xf32> + func.return %0 : tensor<4x64x128xf32> +} + +// CHECK-LABEL: func @convert_gather_transpose( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<128x256xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<4x1xi32>) -> tensor<4x128xf32> { +// CHECK: %[[VAL_2:.*]] = "tf.Const"{{.*}}value = dense<[1, 0]> : tensor<2xi64> +// CHECK: %[[VAL_3:.*]] = "tf.Transpose"(%[[VAL_0]], %[[VAL_2]]) : {{.*}} -> tensor<256x128xf32> +// CHECK: %[[VAL_4:.*]] = "tf.GatherNd"(%[[VAL_3]], %[[VAL_1]]) <{bad_indices_policy = ""}> : {{.*}} -> tensor<4x128xf32> +// CHECK: return %[[VAL_4]] +// CHECK: } +// Test the case when start_index_map isn't an iota what requires a transpose to +// convert it to tf.GatherNd. +func.func @convert_gather_transpose(%arg0: tensor<128x256xf32>, %arg1: tensor<4x1xi32>) -> tensor<4x128xf32> { + %0 = "mhlo.gather"(%arg0, %arg1) { + dimension_numbers = #mhlo.gather< + collapsed_slice_dims = [1], + index_vector_dim = 1, + offset_dims = [1], + start_index_map = [1], + >, + indices_are_sorted = false, + slice_sizes = dense<[128, 1]> : tensor<2xi64> + } : (tensor<128x256xf32>, tensor<4x1xi32>) -> tensor<4x128xf32> + func.return %0 : tensor<4x128xf32> +} + +// CHECK-LABEL: func @convert_gather_offset( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x20xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1x1xi32>) -> tensor<1x1xi32> { +// CHECK: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<[1, 0]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK: %[[VAL_3:.*]] = "tf.Transpose"(%[[VAL_0]], %[[VAL_2]]) : (tensor<1x20xi32>, tensor<2xi64>) -> tensor<20x1xi32> +// CHECK: %[[VAL_4:.*]] = "tf.GatherNd"(%[[VAL_3]], %[[VAL_1]]) <{bad_indices_policy = ""}> : (tensor<20x1xi32>, tensor<1x1xi32>) -> tensor<1x1xi32> +// CHECK: %[[VAL_5:.*]] = "tf.Const"() <{value = dense<[1, 0]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK: %[[VAL_6:.*]] = "tf.Transpose"(%[[VAL_4]], %[[VAL_5]]) : (tensor<1x1xi32>, tensor<2xi64>) -> tensor<1x1xi32> +// CHECK: return %[[VAL_6]] : tensor<1x1xi32> +// CHECK: } +func.func @convert_gather_offset(%arg0: tensor<1x20xi32>, %arg1: tensor<1x1xi32>) -> tensor<1x1xi32> { + %0 = "mhlo.gather"(%arg0, %arg1) { + dimension_numbers = #mhlo.gather< + collapsed_slice_dims = [1], + index_vector_dim = 1, + offset_dims = [0], + start_index_map = [1], + >, + indices_are_sorted = false, + slice_sizes = dense<1> : tensor<2xi64> + } : (tensor<1x20xi32>, tensor<1x1xi32>) -> tensor<1x1xi32> + func.return %0 : tensor<1x1xi32> +} + +// CHECK-LABEL: func @convert_gather_batching_dims( +// CHECK-SAME: %[[ARG_0:.*]]: tensor<2x3x128xf32>, +// CHECK-SAME: %[[ARG_1:.*]]: tensor<3x128x2x1xi32>) +// CHECK-DAG: %[[CST:.*]] = arith.constant dense<[6, 128]> : tensor<2xi64> +// CHECK: %[[VAL_0:.*]] = "tf.Reshape"(%[[ARG_0]], %[[CST]]) : (tensor<2x3x128xf32>, tensor<2xi64>) -> tensor<6x128xf32> +// CHECK-DAG: %[[CST_0:.*]] = "tf.Const"() <{value = dense<[2, 0, 1, 3]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_1:.*]] = "tf.Transpose"(%[[ARG_1]], %[[CST_0]]) : (tensor<3x128x2x1xi32>, tensor<4xi64>) -> tensor<2x3x128x1xi32> +// CHECK-DAG: %[[CST_1:.*]] = arith.constant dense<[6, 128, 1]> : tensor<3xi64> +// CHECK: %[[VAL_2:.*]] = "tf.Reshape"(%[[VAL_1]], %[[CST_1]]) : (tensor<2x3x128x1xi32>, tensor<3xi64>) -> tensor<6x128x1xi32> +// CHECK-DAG: %[[CST_2:.*]] = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK-DAG: %[[CST_3:.*]] = "tf.Const"() <{value = dense<6> : tensor}> : () -> tensor +// CHECK-DAG: %[[CST_4:.*]] = "tf.Const"() <{value = dense<1> : tensor}> : () -> tensor +// CHECK: %[[VAL_3:.*]] = "tf.Range"(%[[CST_2]], %[[CST_3]], %[[CST_4]]) : (tensor, tensor, tensor) -> tensor<6xi32> +// CHECK-DAG: %[[CST_5:.*]] = "tf.Const"() <{value = dense<[6, 1, 1]> : tensor<3xi64>}> : () -> tensor<3xi64> +// CHECK: %[[VAL_4:.*]] = "tf.Reshape"(%[[VAL_3]], %[[CST_5]]) : (tensor<6xi32>, tensor<3xi64>) -> tensor<6x1x1xi32> +// CHECK-DAG: %[[CST_6:.*]] = "tf.Const"() <{value = dense<[6, 128, 1]> : tensor<3xi64>}> : () -> tensor<3xi64> +// CHECK: %[[VAL_5:.*]] = "tf.BroadcastTo"(%[[VAL_4]], %[[CST_6]]) : (tensor<6x1x1xi32>, tensor<3xi64>) -> tensor<6x128x1xi32> +// CHECK-DAG: %[[CST_7:.*]] = "tf.Const"() <{value = dense<2> : tensor}> : () -> tensor +// CHECK: %[[VAL_6:.*]] = "tf.ConcatV2"(%[[VAL_5]], %[[VAL_2]], %[[CST_7]]) : (tensor<6x128x1xi32>, tensor<6x128x1xi32>, tensor) -> tensor<6x128x2xi32> +// CHECK: %[[VAL_7:.*]] = "tf.GatherNd"(%[[VAL_0]], %[[VAL_6]]) <{bad_indices_policy = ""}> : {{.*}} -> tensor<6x128xf32> +// CHECK-DAG: %[[CST_8:.*]] = arith.constant dense<[2, 3, 128]> : tensor<3xi64> +// CHECK: %[[VAL_8:.*]] = "tf.Reshape"(%[[VAL_7]], %[[CST_8]]) : (tensor<6x128xf32>, tensor<3xi64>) -> tensor<2x3x128xf32> +// CHECK-DAG: %[[CST_9:.*]] = "tf.Const"() <{value = dense<[1, 2, 0]> : tensor<3xi64>}> : () -> tensor<3xi64> +// CHECK: %[[VAL_9:.*]] = "tf.Transpose"(%[[VAL_8]], %[[CST_9]]) : (tensor<2x3x128xf32>, tensor<3xi64>) -> tensor<3x128x2xf32> +// CHECK: return %[[VAL_9]] +// CHECK: } +func.func @convert_gather_batching_dims(%arg0: tensor<2x3x128xf32>, %arg1: tensor<3x128x2x1xi32>) -> tensor<3x128x2xf32> { + %0 = "mhlo.gather"(%arg0, %arg1) { + dimension_numbers = #mhlo.gather< + index_vector_dim = 3, + start_index_map = [2], + operand_batching_dims = [0, 1], + start_indices_batching_dims = [2, 0], + collapsed_slice_dims = [2], + >, + indices_are_sorted = false, + slice_sizes = dense<1> : tensor<3xi64> + } : (tensor<2x3x128xf32>, tensor<3x128x2x1xi32>) -> tensor<3x128x2xf32> + func.return %0 : tensor<3x128x2xf32> +} + +// CHECK-LABEL: func @convert_gather_non_collapsed_index_dim( +// CHECK-SAME: %[[ARG_0:.*]]: tensor<10x5xi32>, +// CHECK-SAME: %[[ARG_1:.*]]: tensor<2x1xi32>) -> tensor<2x1x5xi32> { +// CHECK: %[[VAL_0:.*]] = "tf.GatherNd"(%[[ARG_0]], %[[ARG_1]]) <{bad_indices_policy = ""}> : (tensor<10x5xi32>, tensor<2x1xi32>) -> tensor<2x5xi32> +// CHECK-DAG: %[[CST:.*]] = arith.constant dense<[2, 1, 5]> : tensor<3xi64> +// CHECK: %[[VAL_1:.*]] = "tf.Reshape"(%[[VAL_0]], %[[CST]]) : (tensor<2x5xi32>, tensor<3xi64>) -> tensor<2x1x5xi32> +// CHECK: return %[[VAL_1]] : tensor<2x1x5xi32> +// CHECK: } +func.func @convert_gather_non_collapsed_index_dim(%arg0: tensor<10x5xi32>, %arg1: tensor<2x1xi32>) -> tensor<2x1x5xi32> { + %0 = "mhlo.gather"(%arg0, %arg1) { + dimension_numbers = #mhlo.gather< + index_vector_dim = 1, + offset_dims = [1, 2], + start_index_map = [0], + >, + indices_are_sorted = false, + slice_sizes = dense<[1, 5]> : tensor<2xi64> + } : (tensor<10x5xi32>, tensor<2x1xi32>) -> tensor<2x1x5xi32> + func.return %0 : tensor<2x1x5xi32> +} + +// CHECK-LABEL: func @convert_gather_indexed_dimension_slice( +// CHECK-SAME: %[[ARG_0:.*]]: tensor<4x5x6xi32>, +// CHECK-SAME: %[[ARG_1:.*]]: tensor<2x2xi32>) -> tensor<2x1x5x6xi32> { +// CHECK-DAG: %[[CST:.*]] = "tf.Const"() <{value = dense<[0, 2, 1]> : tensor<3xi64>}> : () -> tensor<3xi64> +// CHECK: %[[VAL_0:.*]] = "tf.Transpose"(%[[ARG_0]], %[[CST]]) : (tensor<4x5x6xi32>, tensor<3xi64>) -> tensor<4x6x5xi32> +// CHECK-DAG: %[[CST_0:.*]] = arith.constant dense<[2, 1, 2]> : tensor<3xi64> +// CHECK: %[[VAL_1:.*]] = "tf.Reshape"(%[[ARG_1]], %[[CST_0]]) : (tensor<2x2xi32>, tensor<3xi64>) -> tensor<2x1x2xi32> +// CHECK-DAG: %[[CST_1:.*]] = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK-DAG: %[[CST_2:.*]] = "tf.Const"() <{value = dense<6> : tensor}> : () -> tensor +// CHECK-DAG: %[[CST_3:.*]] = "tf.Const"() <{value = dense<1> : tensor}> : () -> tensor +// CHECK: %[[VAL_2:.*]] = "tf.Range"(%[[CST_1]], %[[CST_2]], %[[CST_3]]) : (tensor, tensor, tensor) -> tensor<6xi32> +// CHECK-DAG: %[[CST_4:.*]] = "tf.Const"() <{value = dense<[1, 6, 1]> : tensor<3xi64>}> : () -> tensor<3xi64> +// CHECK: %[[VAL_3:.*]] = "tf.Reshape"(%[[VAL_2]], %[[CST_4]]) : (tensor<6xi32>, tensor<3xi64>) -> tensor<1x6x1xi32> +// CHECK-DAG: %[[CST_5:.*]] = "tf.Const"() <{value = dense<[1, 6, 1]> : tensor<3xi64>}> : () -> tensor<3xi64> +// CHECK: %[[VAL_4:.*]] = "tf.BroadcastTo"(%[[VAL_3]], %[[CST_5]]) : (tensor<1x6x1xi32>, tensor<3xi64>) -> tensor<1x6x1xi32> +// CHECK-DAG: %[[CST_6:.*]] = arith.constant dense<0> : tensor +// CHECK-DAG: %[[CST_7:.*]] = arith.constant +// CHECK-SAME{LITERAL: dense<[[0, 0], [0, 0], [1, 0]]> : tensor<3x2xi64> +// CHECK: %[[VAL_5:.*]] = "tf.PadV2"(%[[VAL_4]], %[[CST_7]], %[[CST_6]]) : (tensor<1x6x1xi32>, tensor<3x2xi64>, tensor) -> tensor<1x6x2xi32> +// CHECK: %[[VAL_6:.*]] = "tf.Add"(%[[VAL_1]], %[[VAL_5]]) : (tensor<2x1x2xi32>, tensor<1x6x2xi32>) -> tensor<2x6x2xi32> +// CHECK: %[[VAL_7:.*]] = "tf.GatherNd"(%[[VAL_0]], %[[VAL_6]]) <{bad_indices_policy = ""}> : (tensor<4x6x5xi32>, tensor<2x6x2xi32>) -> tensor<2x6x5xi32> +// CHECK-DAG: %[[CST_8:.*]] = arith.constant dense<[2, 1, 6, 5]> : tensor<4xi64> +// CHECK: %[[VAL_8:.*]] = "tf.Reshape"(%[[VAL_7]], %[[CST_8]]) : (tensor<2x6x5xi32>, tensor<4xi64>) -> tensor<2x1x6x5xi32> +// CHECK-DAG: %[[CST_9:.*]] = "tf.Const"() <{value = dense<[0, 1, 3, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_9:.*]] = "tf.Transpose"(%[[VAL_8]], %[[CST_9]]) : (tensor<2x1x6x5xi32>, tensor<4xi64>) -> tensor<2x1x5x6xi32> +// CHECK: return %[[VAL_9]] : tensor<2x1x5x6xi32> +// CHECK: } +func.func @convert_gather_indexed_dimension_slice(%arg0: tensor<4x5x6xi32>, %arg1: tensor<2x2xi32>) -> tensor<2x1x5x6xi32> { + %0 = "mhlo.gather"(%arg0, %arg1) { + dimension_numbers = #mhlo.gather< + index_vector_dim = 1, + offset_dims = [1, 2, 3], + start_index_map = [0, 2], + >, + indices_are_sorted = false, + slice_sizes = dense<[1, 5, 6]> : tensor<3xi64> + } : (tensor<4x5x6xi32>, tensor<2x2xi32>) -> tensor<2x1x5x6xi32> + func.return %0 : tensor<2x1x5x6xi32> +} + +// CHECK-LABEL: func @convert_gather_to_slice_batch_size_1( +// CHECK-SAME: %[[ARG_0:.*]]: tensor<1x2944xi32>, +// CHECK-SAME: %[[ARG_1:.*]]: tensor<1x2xi32>) +// CHECK-DAG: %[[CST:.*]] = "tf.Const"() <{value = dense<[0, 1440]> : tensor<2xi32>}> : () -> tensor<2xi32> +// CHECK-DAG: %[[CST_0:.*]] = "tf.Const"() <{value = dense<0> : tensor<2xi32>}> : () -> tensor<2xi32> +// CHECK: %[[VAL_0:.*]] = "tf.Maximum"(%[[ARG_1]], %[[CST_0:.*]]) : (tensor<1x2xi32>, tensor<2xi32>) -> tensor<1x2xi32> +// CHECK: %[[VAL_1:.*]] = "tf.Minimum"(%[[VAL_0]], %[[CST]]) : (tensor<1x2xi32>, tensor<2xi32>) -> tensor<1x2xi32> +// CHECK-DAG: %[[CST_1:.*]] = "tf.Const"() <{value = dense<[1, 1504]> : tensor<2xi32>}> : () -> tensor<2xi32> +// CHECK: %[[VAL_2:.*]] = "tf.Squeeze"(%[[VAL_1]]) <{squeeze_dims = [0]}> : (tensor<1x2xi32>) -> tensor<2xi32> +// CHECK: %[[VAL_3:.*]] = "tf.Slice"(%[[ARG_0]], %[[VAL_2]], %[[CST_1]]) : (tensor<1x2944xi32>, tensor<2xi32>, tensor<2xi32>) -> tensor<1x1504xi32> +// CHECK: return %[[VAL_3]] +// CHECK: } +func.func @convert_gather_to_slice_batch_size_1(%arg0: tensor<1x2944xi32>, %arg1: tensor<1x2xi32>) -> tensor<1x1504xi32> { + %0 = "mhlo.gather"(%arg0, %arg1) { + dimension_numbers = #mhlo.gather< + offset_dims = [1], + collapsed_slice_dims = [0], + start_index_map = [0, 1], + index_vector_dim = 1, + >, + indices_are_sorted = true, + slice_sizes = dense<[1, 1504]> : tensor<2xi64> + } : (tensor<1x2944xi32>, tensor<1x2xi32>) -> tensor<1x1504xi32> + func.return %0 : tensor<1x1504xi32> +} + +// CHECK-LABEL: func @convert_gather_slice_dynamic_indices( +// CHECK-SAME: %[[ARG_0:.*]]: tensor<256000x1024xi8>, +// CHECK-SAME: %[[ARG_1:.*]]: tensor) -> tensor { +// CHECK: %[[VAL_0:.*]] = "tf.GatherNd"(%[[ARG_0]], %[[ARG_1]]) <{bad_indices_policy = ""}> : (tensor<256000x1024xi8>, tensor) -> tensor +// CHECK: return %[[VAL_0]] : tensor +// CHECK: } +func.func @convert_gather_slice_dynamic_indices(%arg0: tensor<256000x1024xi8>, %arg1: tensor) -> tensor { + %0 = "mhlo.gather"(%arg0, %arg1) { + dimension_numbers = #mhlo.gather< + offset_dims = [2], + collapsed_slice_dims = [0], + start_index_map = [0], + index_vector_dim = 2 + >, + slice_sizes = dense<[1, 1024]> : tensor<2xi64> + } : (tensor<256000x1024xi8>, tensor) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @convert_gather_scalar_dynamic_indices( +// CHECK-SAME: %[[ARG_0:.*]]: tensor<256000xf32>, +// CHECK-SAME: %[[ARG_1:.*]]: tensor) -> tensor { +// CHECK: %[[VAL_0:.*]] = "tf.GatherNd"(%[[ARG_0]], %[[ARG_1]]) <{bad_indices_policy = ""}> : (tensor<256000xf32>, tensor) -> tensor +// CHECK: return %[[VAL_0]] : tensor +// CHECK: } +func.func @convert_gather_scalar_dynamic_indices(%arg0: tensor<256000xf32>, %arg1: tensor) -> tensor { + %0 = "mhlo.gather"(%arg0, %arg1) { + dimension_numbers = #mhlo.gather< + collapsed_slice_dims = [0], + start_index_map = [0], + index_vector_dim = 2 + >, + slice_sizes = dense<1> : tensor<1xi64> + } : (tensor<256000xf32>, tensor) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @convert_gather_to_slice( +// CHECK-SAME: %[[ARG_0:.*]]: tensor<3x2944xi32>, +// CHECK-SAME: %[[ARG_1:.*]]: tensor<3x2xi32>) +// CHECK-DAG: %[[CST:.*]] = "tf.Const"() <{value = dense<[2, 1440]> : tensor<2xi32>}> : () -> tensor<2xi32> +// CHECK-DAG: %[[CST_0:.*]] = "tf.Const"() <{value = dense<0> : tensor<2xi32>}> : () -> tensor<2xi32> +// CHECK: %[[VAL_0:.*]] = "tf.Maximum"(%[[ARG_1]], %[[CST_0]]) : (tensor<3x2xi32>, tensor<2xi32>) -> tensor<3x2xi32> +// CHECK: %[[VAL_1:.*]] = "tf.Minimum"(%[[VAL_0]], %[[CST]]) : (tensor<3x2xi32>, tensor<2xi32>) -> tensor<3x2xi32> +// CHECK-DAG: %[[CST_1:.*]] = "tf.Const"() <{value = dense<[1, 1504]> : tensor<2xi32>}> : () -> tensor<2xi32> +// CHECK-DAG: %[[CST_2:.*]] = "tf.Const"() <{value = dense<0> : tensor<2xi32>}> : () -> tensor<2xi32> +// CHECK-DAG: %[[CST_3:.*]] = "tf.Const"() <{value = dense<[1, 2]> : tensor<2xi32>}> : () -> tensor<2xi32> +// CHECK: %[[VAL_2:.*]] = "tf.Slice"(%[[VAL_1]], %[[CST_2]], %[[CST_3]]) : (tensor<3x2xi32>, tensor<2xi32>, tensor<2xi32>) -> tensor<1x2xi32> +// CHECK: %[[VAL_3:.*]] = "tf.Squeeze"(%[[VAL_2]]) <{squeeze_dims = [0]}> : (tensor<1x2xi32>) -> tensor<2xi32> +// CHECK: %[[VAL_4:.*]] = "tf.Slice"(%[[ARG_0]], %[[VAL_3]], %[[CST_1]]) : (tensor<3x2944xi32>, tensor<2xi32>, tensor<2xi32>) -> tensor<1x1504xi32> +// CHECK-DAG: %[[CST_4:.*]] = "tf.Const"() <{value = dense<[1, 0]> : tensor<2xi32>}> : () -> tensor<2xi32> +// CHECK-DAG: %[[CST_5:.*]] = "tf.Const"() <{value = dense<[1, 2]> : tensor<2xi32>}> : () -> tensor<2xi32> +// CHECK: %[[VAL_5:.*]] = "tf.Slice"(%[[VAL_1]], %[[CST_4]], %[[CST_5]]) : (tensor<3x2xi32>, tensor<2xi32>, tensor<2xi32>) -> tensor<1x2xi32> +// CHECK: %[[VAL_6:.*]] = "tf.Squeeze"(%[[VAL_5]]) <{squeeze_dims = [0]}> : (tensor<1x2xi32>) -> tensor<2xi32> +// CHECK: %[[VAL_7:.*]] = "tf.Slice"(%[[ARG_0]], %[[VAL_6]], %[[CST_1]]) : (tensor<3x2944xi32>, tensor<2xi32>, tensor<2xi32>) -> tensor<1x1504xi32> +// CHECK-DAG: %[[CST_6:.*]] = "tf.Const"() <{value = dense<[2, 0]> : tensor<2xi32>}> : () -> tensor<2xi32> +// CHECK-DAG: %[[CST_7:.*]] = "tf.Const"() <{value = dense<[1, 2]> : tensor<2xi32>}> : () -> tensor<2xi32> +// CHECK: %[[VAL_8:.*]] = "tf.Slice"(%[[VAL_1]], %[[CST_6]], %[[CST_7]]) : (tensor<3x2xi32>, tensor<2xi32>, tensor<2xi32>) -> tensor<1x2xi32> +// CHECK: %[[VAL_9:.*]] = "tf.Squeeze"(%[[VAL_8]]) <{squeeze_dims = [0]}> : (tensor<1x2xi32>) -> tensor<2xi32> +// CHECK: %[[VAL_10:.*]] = "tf.Slice"(%[[ARG_0]], %[[VAL_9]], %[[CST_1]]) : (tensor<3x2944xi32>, tensor<2xi32>, tensor<2xi32>) -> tensor<1x1504xi32> +// CHECK-DAG: %[[CST_8:.*]] = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK: %[[VAL_11:.*]] = "tf.ConcatV2"(%[[VAL_4]], %[[VAL_7]], %[[VAL_10]], %[[CST_8]]) : (tensor<1x1504xi32>, tensor<1x1504xi32>, tensor<1x1504xi32>, tensor) -> tensor<3x1504xi32> +// CHECK: return %[[VAL_11]] +// CHECK: } +func.func @convert_gather_to_slice(%arg0: tensor<3x2944xi32>, %arg1: tensor<3x2xi32>) -> tensor<3x1504xi32> { + %0 = "mhlo.gather"(%arg0, %arg1) { + dimension_numbers = #mhlo.gather< + offset_dims = [1], + collapsed_slice_dims = [0], + start_index_map = [0, 1], + index_vector_dim = 1, + >, + indices_are_sorted = true, + slice_sizes = dense<[1, 1504]> : tensor<2xi64> + } : (tensor<3x2944xi32>, tensor<3x2xi32>) -> tensor<3x1504xi32> + func.return %0 : tensor<3x1504xi32> +} + +// CHECK-LABEL: func @convert_gather_to_slice_dynamic_error +func.func @convert_gather_to_slice_dynamic_error(%arg0: tensor<3x?xi32>, %arg1: tensor<3x2xi32>) -> tensor<3x1504xi32> { + // expected-error @+1 {{Dynamic shaped operand is not supported.}} + %0 = "mhlo.gather"(%arg0, %arg1) { + dimension_numbers = #mhlo.gather< + offset_dims = [1], + collapsed_slice_dims = [0], + start_index_map = [0, 1], + index_vector_dim = 1, + >, + indices_are_sorted = true, + slice_sizes = dense<[1, 1504]> : tensor<2xi64> + } : (tensor<3x?xi32>, tensor<3x2xi32>) -> tensor<3x1504xi32> + func.return %0 : tensor<3x1504xi32> +} + +// CHECK-LABEL: func @convert_dynamic_slice( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<7x3xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor, +// CHECK-SAME: %[[VAL_2:.*]]: tensor) -> tensor<4x2xf32> { +// CHECK: %[[VAL_3:.*]] = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK: %[[VAL_4:.*]] = "tf.Cast"(%[[VAL_1]]) <{Truncate = false}> : (tensor) -> tensor +// CHECK: %[[VAL_5:.*]] = "tf.Const"() <{value = dense<3> : tensor}> : () -> tensor +// CHECK: %[[VAL_6:.*]] = "tf.Minimum"(%[[VAL_4]], %[[VAL_5]]) : (tensor, tensor) -> tensor +// CHECK: %[[VAL_7:.*]] = "tf.Maximum"(%[[VAL_6]], %[[VAL_3]]) : (tensor, tensor) -> tensor +// CHECK: %[[VAL_8:.*]] = "tf.Cast"(%[[VAL_2]]) <{Truncate = false}> : (tensor) -> tensor +// CHECK: %[[VAL_9:.*]] = "tf.Const"() <{value = dense<1> : tensor}> : () -> tensor +// CHECK: %[[VAL_10:.*]] = "tf.Minimum"(%[[VAL_8]], %[[VAL_9]]) : (tensor, tensor) -> tensor +// CHECK: %[[VAL_11:.*]] = "tf.Maximum"(%[[VAL_10]], %[[VAL_3]]) : (tensor, tensor) -> tensor +// CHECK: %[[VAL_12:.*]] = "tf.Pack"(%[[VAL_7]], %[[VAL_11]]) <{axis = 0 : i64}> : (tensor, tensor) -> tensor<2xi32> +// CHECK: %[[VAL_13:.*]] = "tf.Const"() <{value = dense<[4, 2]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK: %[[VAL_14:.*]] = "tf.Slice"(%[[VAL_0]], %[[VAL_12]], %[[VAL_13]]) : (tensor<7x3xf32>, tensor<2xi32>, tensor<2xi64>) -> tensor<4x2xf32> +// CHECK: return %[[VAL_14]] : tensor<4x2xf32> +// CHECK: } +func.func @convert_dynamic_slice(%arg0: tensor<7x3xf32>, %arg1: tensor, %arg2: tensor) -> tensor<4x2xf32> { + %0 = "mhlo.dynamic_slice"(%arg0, %arg1, %arg2) <{slice_sizes = dense<[4, 2]> : tensor<2xi64>}> : (tensor<7x3xf32>, tensor, tensor) -> tensor<4x2xf32> + func.return %0 : tensor<4x2xf32> +} + +// CHECK-LABEL: func @convert_dynamic_slice_ui32( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<7x3xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor, +// CHECK-SAME: %[[VAL_2:.*]]: tensor) -> tensor<4x2xf32> { +// CHECK: %[[VAL_3:.*]] = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK: %[[VAL_4:.*]] = "tf.Cast"(%[[VAL_1]]) <{Truncate = false}> : (tensor) -> tensor +// CHECK: %[[VAL_5:.*]] = "tf.Const"() <{value = dense<3> : tensor}> : () -> tensor +// CHECK: %[[VAL_6:.*]] = "tf.Minimum"(%[[VAL_4]], %[[VAL_5]]) : (tensor, tensor) -> tensor +// CHECK: %[[VAL_7:.*]] = "tf.Maximum"(%[[VAL_6]], %[[VAL_3]]) : (tensor, tensor) -> tensor +// CHECK: %[[VAL_8:.*]] = "tf.Cast"(%[[VAL_2]]) <{Truncate = false}> : (tensor) -> tensor +// CHECK: %[[VAL_9:.*]] = "tf.Const"() <{value = dense<1> : tensor}> : () -> tensor +// CHECK: %[[VAL_10:.*]] = "tf.Minimum"(%[[VAL_8]], %[[VAL_9]]) : (tensor, tensor) -> tensor +// CHECK: %[[VAL_11:.*]] = "tf.Maximum"(%[[VAL_10]], %[[VAL_3]]) : (tensor, tensor) -> tensor +// CHECK: %[[VAL_12:.*]] = "tf.Pack"(%[[VAL_7]], %[[VAL_11]]) <{axis = 0 : i64}> : (tensor, tensor) -> tensor<2xi32> +// CHECK: %[[VAL_13:.*]] = "tf.Const"() <{value = dense<[4, 2]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK: %[[VAL_14:.*]] = "tf.Slice"(%[[VAL_0]], %[[VAL_12]], %[[VAL_13]]) : (tensor<7x3xf32>, tensor<2xi32>, tensor<2xi64>) -> tensor<4x2xf32> +// CHECK: return %[[VAL_14]] : tensor<4x2xf32> +// CHECK: } +func.func @convert_dynamic_slice_ui32(%arg0: tensor<7x3xf32>, %arg1: tensor, %arg2: tensor) -> tensor<4x2xf32> { + %0 = "mhlo.dynamic_slice"(%arg0, %arg1, %arg2) <{slice_sizes = dense<[4, 2]> : tensor<2xi64>}> : (tensor<7x3xf32>, tensor, tensor) -> tensor<4x2xf32> + func.return %0 : tensor<4x2xf32> +} + +// CHECK-LABEL: func.func @convert_scatter_update( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<20x6xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<4xi32>, +// CHECK-SAME: %[[VAL_2:.*]]: tensor<4x6xf32>) -> tensor<20x6xf32> { +// CHECK: %[[VAL_3:.*]] = "mhlo.scatter"(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]]) <{ +// CHECK-SAME: indices_are_sorted = false, scatter_dimension_numbers = #mhlo.scatter, unique_indices = false +// CHECK-SAME: }> ({ +// CHECK: ^bb0(%[[VAL_4:.*]]: tensor, %[[VAL_5:.*]]: tensor): +// CHECK: mhlo.return %[[VAL_5]] : tensor +// CHECK: }) : (tensor<20x6xf32>, tensor<4xi32>, tensor<4x6xf32>) -> tensor<20x6xf32> +// CHECK: return %[[VAL_3]] : tensor<20x6xf32> +// CHECK: } +func.func @convert_scatter_update(%arg0: tensor<20x6xf32>, %arg1: tensor<4xi32>, %arg2: tensor<4x6xf32>) -> tensor<20x6xf32> { + %0 = "mhlo.scatter"(%arg0, %arg1, %arg2) ({ + ^bb0(%arg3: tensor, %arg4: tensor): + "mhlo.return"(%arg4) : (tensor) -> () + }) { + scatter_dimension_numbers = #mhlo.scatter< + update_window_dims = [1], + inserted_window_dims = [0], + scatter_dims_to_operand_dims = [0], + index_vector_dim = 1 + >, + indices_are_sorted = false, + unique_indices = false} : (tensor<20x6xf32>, tensor<4xi32>, tensor<4x6xf32>) -> tensor<20x6xf32> + func.return %0 : tensor<20x6xf32> +} + +// CHECK-LABEL: func.func @convert_scatter_update_with_non_trailing_update_window_dims( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<5x10xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<3x1xi32>, +// CHECK-SAME: %[[VAL_2:.*]]: tensor<10x3xf32>) -> tensor<5x10xf32> { +// CHECK: %[[VAL_3:.*]] = "mhlo.scatter"(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]]) <{ +// CHECK-SAME: indices_are_sorted = false, scatter_dimension_numbers = #mhlo.scatter, unique_indices = false +// CHECK-SAME: }> ({ +// CHECK: ^bb0(%[[VAL_4:.*]]: tensor, %[[VAL_5:.*]]: tensor): +// CHECK: mhlo.return %[[VAL_5]] : tensor +// CHECK: }) : (tensor<5x10xf32>, tensor<3x1xi32>, tensor<10x3xf32>) -> tensor<5x10xf32> +// CHECK: return %[[VAL_3]] : tensor<5x10xf32> +// CHECK: } +func.func @convert_scatter_update_with_non_trailing_update_window_dims( + %arg0: tensor<5x10xf32>, + %arg1: tensor<3x1xi32>, + %arg2: tensor<10x3xf32>) -> tensor<5x10xf32> +{ + %0 = "mhlo.scatter"(%arg0, %arg1, %arg2) ({ + ^bb0(%arg3: tensor, %arg4: tensor): + "mhlo.return"(%arg4) : (tensor) -> () + }) { + scatter_dimension_numbers = #mhlo.scatter< + update_window_dims = [0], + inserted_window_dims = [0], + scatter_dims_to_operand_dims = [0], + index_vector_dim = 1 + >, + indices_are_sorted = false, + unique_indices = false} : (tensor<5x10xf32>, tensor<3x1xi32>, tensor<10x3xf32>) -> tensor<5x10xf32> + func.return %0 : tensor<5x10xf32> +} + +// CHECK-LABEL: func.func @convert_scatter_update_to_non_trailing_operand_dimensions( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<5x4x3x7xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<2x2xi32>, +// CHECK-SAME: %[[VAL_2:.*]]: tensor<2x5x3xf32>) -> tensor<5x4x3x7xf32> { +// CHECK: %[[VAL_3:.*]] = "mhlo.scatter"(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]]) <{ +// CHECK-SAME: indices_are_sorted = false, scatter_dimension_numbers = #mhlo.scatter, unique_indices = false +// CHECK-SAME: }> ({ +// CHECK: ^bb0(%[[VAL_4:.*]]: tensor, %[[VAL_5:.*]]: tensor): +// CHECK: mhlo.return %[[VAL_5]] : tensor +// CHECK: }) : (tensor<5x4x3x7xf32>, tensor<2x2xi32>, tensor<2x5x3xf32>) -> tensor<5x4x3x7xf32> +// CHECK: return %[[VAL_3]] : tensor<5x4x3x7xf32> +// CHECK: } +func.func @convert_scatter_update_to_non_trailing_operand_dimensions( + %arg0: tensor<5x4x3x7xf32>, + %arg1: tensor<2x2xi32>, + %arg2: tensor<2x5x3xf32>) -> tensor<5x4x3x7xf32> +{ + %0 = "mhlo.scatter"(%arg0, %arg1, %arg2) ({ + ^bb0(%arg3: tensor, %arg4: tensor): + "mhlo.return"(%arg4) : (tensor) -> () + }) { + scatter_dimension_numbers = #mhlo.scatter< + update_window_dims = [1, 2], + inserted_window_dims = [1, 3], + scatter_dims_to_operand_dims = [1, 3], + index_vector_dim = 1>, + indices_are_sorted = false, + unique_indices = false} : (tensor<5x4x3x7xf32>, tensor<2x2xi32>, tensor<2x5x3xf32>) -> tensor<5x4x3x7xf32> + func.return %0 : tensor<5x4x3x7xf32> +} + +// CHECK-LABEL: func.func @convert_scatter_update_reshape_indices_and_updates( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<16x1504xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<1xi32>, +// CHECK-SAME: %[[VAL_2:.*]]: tensor<16xf32>) -> tensor<16x1504xf32> { +// CHECK: %[[VAL_3:.*]] = "mhlo.scatter"(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]]) <{ +// CHECK-SAME: indices_are_sorted = true, scatter_dimension_numbers = #mhlo.scatter, unique_indices = true +// CHECK-SAME: }> ({ +// CHECK: ^bb0(%[[VAL_4:.*]]: tensor, %[[VAL_5:.*]]: tensor): +// CHECK: mhlo.return %[[VAL_5]] : tensor +// CHECK: }) : (tensor<16x1504xf32>, tensor<1xi32>, tensor<16xf32>) -> tensor<16x1504xf32> +// CHECK: return %[[VAL_3]] : tensor<16x1504xf32> +// CHECK: } +func.func @convert_scatter_update_reshape_indices_and_updates( + %arg0: tensor<16x1504xf32>, + %arg1: tensor<1xi32>, + %arg2: tensor<16xf32>) -> tensor<16x1504xf32> +{ + %0 = "mhlo.scatter"(%arg0, %arg1, %arg2) ({ +^bb0(%arg3: tensor, %arg4: tensor): + "mhlo.return"(%arg4) : (tensor) -> () +}) { + indices_are_sorted = true, + scatter_dimension_numbers = #mhlo.scatter< + update_window_dims = [0], + inserted_window_dims = [1], + scatter_dims_to_operand_dims = [1]>, + unique_indices = true} : (tensor<16x1504xf32>, tensor<1xi32>, tensor<16xf32>) -> tensor<16x1504xf32> + func.return %0 : tensor<16x1504xf32> +} + +// CHECK-LABEL: func.func @convert_scatter_add( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<20x6xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<4x1xi32>, +// CHECK-SAME: %[[VAL_2:.*]]: tensor<4x6xf32>) -> tensor<20x6xf32> { +// CHECK: %[[VAL_6:.*]] = "mhlo.scatter"(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]]) <{ +// CHECK-SAME: indices_are_sorted = false, scatter_dimension_numbers = #mhlo.scatter, unique_indices = false +// CHECK-SAME: }> ({ +// CHECK: ^bb0(%[[VAL_3:.*]]: tensor, %[[VAL_4:.*]]: tensor): +// CHECK: %[[VAL_5:.*]] = mhlo.add %[[VAL_3]], %[[VAL_4]] : tensor +// CHECK: mhlo.return %[[VAL_5]] : tensor +// CHECK: }) : (tensor<20x6xf32>, tensor<4x1xi32>, tensor<4x6xf32>) -> tensor<20x6xf32> +// CHECK: return %[[VAL_6]] : tensor<20x6xf32> +// CHECK: } +func.func @convert_scatter_add(%arg0: tensor<20x6xf32>, %arg1: tensor<4x1xi32>, %arg2: tensor<4x6xf32>) -> tensor<20x6xf32> { + %0 = "mhlo.scatter"(%arg0, %arg1, %arg2) ({ + ^bb0(%arg3: tensor, %arg4: tensor): + %2 = mhlo.add %arg3, %arg4 : tensor + "mhlo.return"(%2) : (tensor) -> () + }) { + scatter_dimension_numbers = #mhlo.scatter< + update_window_dims = [1], + inserted_window_dims = [0], + scatter_dims_to_operand_dims = [0], + index_vector_dim = 1, + >, + indices_are_sorted = false, + unique_indices = false} : (tensor<20x6xf32>, tensor<4x1xi32>, tensor<4x6xf32>) -> tensor<20x6xf32> + func.return %0 : tensor<20x6xf32> +} + +// CHECK-LABEL: func.func @convert_scatter_max( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<20x6xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<4x1xi32>, +// CHECK-SAME: %[[VAL_2:.*]]: tensor<4x6xf32>) -> tensor<20x6xf32> { +// CHECK: %[[VAL_6:.*]] = "mhlo.scatter"(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]]) <{ +// CHECK-SAME: indices_are_sorted = false, scatter_dimension_numbers = #mhlo.scatter, unique_indices = false +// CHECK-SAME: }> ({ +// CHECK: ^bb0(%[[VAL_3:.*]]: tensor, %[[VAL_4:.*]]: tensor): +// CHECK: %[[VAL_5:.*]] = mhlo.maximum %[[VAL_3]], %[[VAL_4]] : tensor +// CHECK: mhlo.return %[[VAL_5]] : tensor +// CHECK: }) : (tensor<20x6xf32>, tensor<4x1xi32>, tensor<4x6xf32>) -> tensor<20x6xf32> +// CHECK: return %[[VAL_6]] : tensor<20x6xf32> +// CHECK: } +func.func @convert_scatter_max(%arg0: tensor<20x6xf32>, %arg1: tensor<4x1xi32>, %arg2: tensor<4x6xf32>) -> tensor<20x6xf32> { + %0 = "mhlo.scatter"(%arg0, %arg1, %arg2) ({ + ^bb0(%arg3: tensor, %arg4: tensor): + %2 = mhlo.maximum %arg3, %arg4 : tensor + "mhlo.return"(%2) : (tensor) -> () + }) { + scatter_dimension_numbers = #mhlo.scatter< + update_window_dims = [1], + inserted_window_dims = [0], + scatter_dims_to_operand_dims = [0], + index_vector_dim = 1, + >, + indices_are_sorted = false, + unique_indices = false} : (tensor<20x6xf32>, tensor<4x1xi32>, tensor<4x6xf32>) -> tensor<20x6xf32> + func.return %0 : tensor<20x6xf32> +} + +// CHECK-LABEL: func.func @convert_scatter_min( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<20x6xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<4x1xi32>, +// CHECK-SAME: %[[VAL_2:.*]]: tensor<4x6xf32>) -> tensor<20x6xf32> { +// CHECK: %[[VAL_6:.*]] = "mhlo.scatter"(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]]) <{ +// CHECK-SAME: indices_are_sorted = false, scatter_dimension_numbers = #mhlo.scatter, unique_indices = false +// CHECK-SAME: }> ({ +// CHECK: ^bb0(%[[VAL_3:.*]]: tensor, %[[VAL_4:.*]]: tensor): +// CHECK: %[[VAL_5:.*]] = mhlo.minimum %[[VAL_3]], %[[VAL_4]] : tensor +// CHECK: mhlo.return %[[VAL_5]] : tensor +// CHECK: }) : (tensor<20x6xf32>, tensor<4x1xi32>, tensor<4x6xf32>) -> tensor<20x6xf32> +// CHECK: return %[[VAL_6]] : tensor<20x6xf32> +// CHECK: } +func.func @convert_scatter_min(%arg0: tensor<20x6xf32>, %arg1: tensor<4x1xi32>, %arg2: tensor<4x6xf32>) -> tensor<20x6xf32> { + %0 = "mhlo.scatter"(%arg0, %arg1, %arg2) ({ + ^bb0(%arg3: tensor, %arg4: tensor): + %2 = mhlo.minimum %arg3, %arg4 : tensor + "mhlo.return"(%2) : (tensor) -> () + }) { + scatter_dimension_numbers = #mhlo.scatter< + update_window_dims = [1], + inserted_window_dims = [0], + scatter_dims_to_operand_dims = [0], + index_vector_dim = 1, + >, + indices_are_sorted = false, + unique_indices = false} : (tensor<20x6xf32>, tensor<4x1xi32>, tensor<4x6xf32>) -> tensor<20x6xf32> + func.return %0 : tensor<20x6xf32> +} + +// CHECK-LABEL: func.func @convert_scatter_sub( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<20x6xf32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<4x1xi32>, +// CHECK-SAME: %[[VAL_2:.*]]: tensor<4x6xf32>) -> tensor<20x6xf32> { +// CHECK: %[[VAL_6:.*]] = "mhlo.scatter"(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]]) <{ +// CHECK-SAME: indices_are_sorted = false, scatter_dimension_numbers = #mhlo.scatter, unique_indices = false +// CHECK-SAME: }> ({ +// CHECK: ^bb0(%[[VAL_3:.*]]: tensor, %[[VAL_4:.*]]: tensor): +// CHECK: %[[VAL_5:.*]] = mhlo.subtract %[[VAL_3]], %[[VAL_4]] : tensor +// CHECK: mhlo.return %[[VAL_5]] : tensor +// CHECK: }) : (tensor<20x6xf32>, tensor<4x1xi32>, tensor<4x6xf32>) -> tensor<20x6xf32> +// CHECK: return %[[VAL_6]] : tensor<20x6xf32> +// CHECK: } +func.func @convert_scatter_sub(%arg0: tensor<20x6xf32>, %arg1: tensor<4x1xi32>, %arg2: tensor<4x6xf32>) -> tensor<20x6xf32> { + %0 = "mhlo.scatter"(%arg0, %arg1, %arg2) ({ + ^bb0(%arg3: tensor, %arg4: tensor): + %2 = mhlo.subtract %arg3, %arg4 : tensor + "mhlo.return"(%2) : (tensor) -> () + }) { + scatter_dimension_numbers = #mhlo.scatter< + update_window_dims = [1], + inserted_window_dims = [0], + scatter_dims_to_operand_dims = [0], + index_vector_dim = 1, + >, + indices_are_sorted = false, + unique_indices = false} : (tensor<20x6xf32>, tensor<4x1xi32>, tensor<4x6xf32>) -> tensor<20x6xf32> + func.return %0 : tensor<20x6xf32> +} + +// CHECK-LABEL: func @convert_pytorch_argmax +func.func @convert_pytorch_argmax(%arg0: tensor<1x9xi32>) -> tensor<1xi32> { + %0 = mhlo.constant dense<0> : tensor + %1 = mhlo.constant dense<-2147483648> : tensor + %2 = "mhlo.iota"() <{iota_dimension = 0 : i64}> : () -> tensor<9xi32> + %3 = mhlo.reshape %2 : (tensor<9xi32>) -> tensor<1x9xi32> + %4:2 = mhlo.reduce(%arg0 init: %1), (%3 init: %0) across dimensions = [1] : (tensor<1x9xi32>, tensor<1x9xi32>, tensor, tensor) -> (tensor<1xi32>, tensor<1xi32>) + reducer(%arg1: tensor, %arg3: tensor) (%arg2: tensor, %arg4: tensor) { + %6 = mhlo.compare GE, %arg1, %arg3 : (tensor, tensor) -> tensor + %7 = mhlo.select %6, %arg1, %arg3 : tensor, tensor + %8 = mhlo.compare EQ, %arg1, %arg3 : (tensor, tensor) -> tensor + %9 = mhlo.minimum %arg2, %arg4 : tensor + %10 = mhlo.select %6, %arg2, %arg4 : tensor, tensor + %11 = mhlo.select %8, %9, %10 : tensor, tensor + mhlo.return %7, %11 : tensor, tensor + } + func.return %4#1 : tensor<1xi32> + + // CHECK-DAG: %cst = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor + // CHECK: %cst_0 = "tf.Const"() <{value = dense<-2147483648> : tensor}> : () -> tensor + // CHECK-DAG: %cst_1 = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor + // CHECK-DAG: %cst_2 = "tf.Const"() <{value = dense<9> : tensor}> : () -> tensor + // CHECK: %cst_3 = "tf.Const"() <{value = dense<1> : tensor}> : () -> tensor + // CHECK: %0 = "tf.Range"(%cst_1, %cst_2, %cst_3) : (tensor, tensor, tensor) -> tensor<9xi32> + // CHECK: %cst_4 = arith.constant dense<[1, 9]> : tensor<2xi64> + // CHECK: %1 = "tf.Reshape"(%0, %cst_4) : (tensor<9xi32>, tensor<2xi64>) -> tensor<1x9xi32> + // CHECK: %cst_5 = arith.constant dense<1> : tensor<1xi32> + // CHECK: %2 = "tf.Max"(%arg0, %cst_5) <{keep_dims = false}> : (tensor<1x9xi32>, tensor<1xi32>) -> tensor<1xi32> + // CHECK: %3 = "tf.ArgMax"(%arg0, %cst_5) : (tensor<1x9xi32>, tensor<1xi32>) -> tensor<1xi32> + // CHECK: return %3 : tensor<1xi32> +} + +// CHECK-LABEL: func @convert_argmax( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<4x32x256xf32>) -> (tensor<4x32xf32>, tensor<4x32xi32>) { +// CHECK: %[[VAL_9:.*]] = arith.constant dense<2> : tensor<1xi32> +// CHECK: %[[VAL_10:.*]] = "tf.Max"(%[[VAL_0]], %[[VAL_9]]) <{keep_dims = false}> : {{.*}} -> tensor<4x32xf32> +// CHECK: %[[VAL_11:.*]] = "tf.ArgMax"(%[[VAL_0]], %[[VAL_9]]) : {{.*}} -> tensor<4x32xi32> +// CHECK: return %[[VAL_10]], %[[VAL_11]] +// CHECK: } +func.func @convert_argmax(%arg0: tensor<4x32x256xf32>) -> (tensor<4x32xf32>, tensor<4x32xi32>) { + %0 = mhlo.constant dense<0xFF800000> : tensor + %1 = mhlo.constant dense<0> : tensor + %2 = "mhlo.iota"() <{iota_dimension = 0 : i64}> : () -> tensor<256xi32> + %3 = "mhlo.broadcast_in_dim"(%2) <{broadcast_dimensions = dense<2> : tensor<1xi64>}> : (tensor<256xi32>) -> tensor<4x32x256xi32> + %4:2 = "mhlo.reduce"(%arg0, %3, %0, %1) ({ + ^bb0(%arg1: tensor, %arg2: tensor, %arg3: tensor, %arg4: tensor): + %7 = "mhlo.compare"(%arg1, %arg3) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %8 = "mhlo.compare"(%arg1, %arg1) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %9 = mhlo.or %7, %8 : tensor + %10 = "mhlo.select"(%9, %arg1, %arg3) : (tensor, tensor, tensor) -> tensor + %11 = "mhlo.compare"(%arg1, %arg3) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %12 = "mhlo.compare"(%arg2, %arg4) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %13 = mhlo.and %11, %12 : tensor + %14 = mhlo.or %9, %13 : tensor + %15 = "mhlo.select"(%14, %arg2, %arg4) : (tensor, tensor, tensor) -> tensor + "mhlo.return"(%10, %15) : (tensor, tensor) -> () + }) {dimensions = dense<2> : tensor<1xi64>} : (tensor<4x32x256xf32>, tensor<4x32x256xi32>, tensor, tensor) -> (tensor<4x32xf32>, tensor<4x32xi32>) + func.return %4#0, %4#1 : tensor<4x32xf32>, tensor<4x32xi32> +} + +// CHECK-LABEL: func @convert_argmax_constant( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2x2x4xf32>) -> (tensor<2x2xf32>, tensor<2x2xi32>) { +// CHECK-DAG: %[[VAL_1:.*]] = "tf.Const"() <{value = dense<0xFF800000> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_3:.*]] = "tf.Const"() <{value = dense<{{\[\[}}[0, 1, 2, 3], [0, 1, 2, 3]], {{\[\[}}0, 1, 2, 3], [0, 1, 2, 3]]]> : tensor<2x2x4xi32>}> : () -> tensor<2x2x4xi32> +// CHECK-DAG: %[[VAL_4:.*]] = arith.constant dense<2> : tensor<1xi32> +// CHECK: %[[VAL_5:.*]] = "tf.Max"(%[[VAL_0]], %[[VAL_4]]) <{keep_dims = false}> : (tensor<2x2x4xf32>, tensor<1xi32>) -> tensor<2x2xf32> +// CHECK: %[[VAL_6:.*]] = "tf.ArgMax"(%[[VAL_0]], %[[VAL_4]]) : (tensor<2x2x4xf32>, tensor<1xi32>) -> tensor<2x2xi32> +// CHECK: return %[[VAL_5]], %[[VAL_6]] : tensor<2x2xf32>, tensor<2x2xi32> +// CHECK: } +func.func @convert_argmax_constant(%arg0: tensor<2x2x4xf32>) -> (tensor<2x2xf32>, tensor<2x2xi32>) { + %0 = mhlo.constant dense<0xFF800000> : tensor + %1 = mhlo.constant dense<0> : tensor + %3 = mhlo.constant dense<[[[0, 1, 2, 3], [0, 1, 2, 3]], [[0, 1, 2, 3], [0, 1, 2, 3]]]> : tensor<2x2x4xi32> + %4:2 = "mhlo.reduce"(%arg0, %3, %0, %1) ({ + ^bb0(%arg1: tensor, %arg2: tensor, %arg3: tensor, %arg4: tensor): + %7 = "mhlo.compare"(%arg1, %arg3) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %8 = "mhlo.compare"(%arg1, %arg1) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %9 = mhlo.or %7, %8 : tensor + %10 = "mhlo.select"(%9, %arg1, %arg3) : (tensor, tensor, tensor) -> tensor + %11 = "mhlo.compare"(%arg1, %arg3) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %12 = "mhlo.compare"(%arg2, %arg4) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %13 = mhlo.and %11, %12 : tensor + %14 = mhlo.or %9, %13 : tensor + %15 = "mhlo.select"(%14, %arg2, %arg4) : (tensor, tensor, tensor) -> tensor + "mhlo.return"(%10, %15) : (tensor, tensor) -> () + }) {dimensions = dense<2> : tensor<1xi64>} : (tensor<2x2x4xf32>, tensor<2x2x4xi32>, tensor, tensor) -> (tensor<2x2xf32>, tensor<2x2xi32>) + func.return %4#0, %4#1 : tensor<2x2xf32>, tensor<2x2xi32> +} + +// CHECK-LABEL: func @convert_argmax_constant_non_z_axis( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<4x4xf32>) -> (tensor<4xf32>, tensor<4xi32>) { +// CHECK-DAG: %[[VAL_1:.*]] = "tf.Const"() <{value = dense<0xFF800000> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_3:.*]] = "tf.Const"() <{value = dense<{{\[\[}}0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]]> : tensor<4x4xi32>}> : () -> tensor<4x4xi32> +// CHECK-DAG: %[[VAL_4:.*]] = arith.constant dense<0> : tensor<1xi32> +// CHECK: %[[VAL_5:.*]] = "tf.Max"(%[[VAL_0]], %[[VAL_4]]) <{keep_dims = false}> : (tensor<4x4xf32>, tensor<1xi32>) -> tensor<4xf32> +// CHECK: %[[VAL_6:.*]] = "tf.ArgMax"(%[[VAL_0]], %[[VAL_4]]) : (tensor<4x4xf32>, tensor<1xi32>) -> tensor<4xi32> +// CHECK: return %[[VAL_5]], %[[VAL_6]] : tensor<4xf32>, tensor<4xi32> +// CHECK: } +func.func @convert_argmax_constant_non_z_axis(%arg0: tensor<4x4xf32>) -> (tensor<4xf32>, tensor<4xi32>) { + %0 = mhlo.constant dense<0xFF800000> : tensor + %1 = mhlo.constant dense<0> : tensor + %3 = mhlo.constant dense<[[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]]> : tensor<4x4xi32> + %4:2 = "mhlo.reduce"(%arg0, %3, %0, %1) ({ + ^bb0(%arg1: tensor, %arg2: tensor, %arg3: tensor, %arg4: tensor): + %7 = "mhlo.compare"(%arg1, %arg3) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %8 = "mhlo.compare"(%arg1, %arg1) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %9 = mhlo.or %7, %8 : tensor + %10 = "mhlo.select"(%9, %arg1, %arg3) : (tensor, tensor, tensor) -> tensor + %11 = "mhlo.compare"(%arg1, %arg3) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %12 = "mhlo.compare"(%arg2, %arg4) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %13 = mhlo.and %11, %12 : tensor + %14 = mhlo.or %9, %13 : tensor + %15 = "mhlo.select"(%14, %arg2, %arg4) : (tensor, tensor, tensor) -> tensor + "mhlo.return"(%10, %15) : (tensor, tensor) -> () + }) {dimensions = dense<0> : tensor<1xi64>} : (tensor<4x4xf32>, tensor<4x4xi32>, tensor, tensor) -> (tensor<4xf32>, tensor<4xi32>) + func.return %4#0, %4#1 : tensor<4xf32>, tensor<4xi32> +} + +// CHECK-LABEL: func.func @convert_argmax_bool( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xi1>) -> tensor { +// CHECK-DAG: %[[VAL_1:.*]] = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<2> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_3:.*]] = "tf.Const"() <{value = dense<1> : tensor}> : () -> tensor +// CHECK: %[[VAL_4:.*]] = "tf.Range"(%[[VAL_1]], %[[VAL_2]], %[[VAL_3]]) : (tensor, tensor, tensor) -> tensor<2xi32> +// CHECK-DAG: %[[VAL_5:.*]] = "tf.Const"() <{value = dense : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_6:.*]] = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_7:.*]] = arith.constant dense<0> : tensor<1xi32> +// CHECK: %[[VAL_8:.*]] = "tf.Any"(%[[VAL_0]], %[[VAL_7]]) <{keep_dims = false}> : (tensor<2xi1>, tensor<1xi32>) -> tensor +// CHECK: %[[VAL_9:.*]] = "tf.ArgMax"(%[[VAL_0]], %[[VAL_7]]) : (tensor<2xi1>, tensor<1xi32>) -> tensor +// CHECK: return %[[VAL_9]] : tensor +// CHECK: } + +func.func @convert_argmax_bool(%arg0: tensor<2xi1>) -> tensor { + %0 = "mhlo.iota"() <{iota_dimension = 0 : i64}> : () -> tensor<2xi32> + %1 = mhlo.constant dense : tensor + %2 = mhlo.constant dense<0> : tensor + %3:2 = mhlo.reduce(%arg0 init: %1), (%0 init: %2) across dimensions = [0] : (tensor<2xi1>, tensor<2xi32>, tensor, tensor) -> (tensor, tensor) + reducer(%arg1: tensor, %arg3: tensor) (%arg2: tensor, %arg4: tensor) { + %4 = mhlo.compare GT, %arg1, %arg3 : (tensor, tensor) -> tensor + %5 = mhlo.select %4, %arg1, %arg3 : tensor, tensor + %6 = mhlo.compare EQ, %arg1, %arg3 : (tensor, tensor) -> tensor + %7 = mhlo.compare LT, %arg2, %arg4 : (tensor, tensor) -> tensor + %8 = mhlo.and %6, %7 : tensor + %9 = mhlo.or %4, %8 : tensor + %10 = mhlo.select %9, %arg2, %arg4 : tensor, tensor + mhlo.return %5, %10 : tensor, tensor + } + return %3#1 : tensor +} + +// CHECK-LABEL: func @convert_argmin( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<4x32x256xf32>) -> (tensor<4x32xf32>, tensor<4x32xi32>) { +// CHECK: %[[VAL_9:.*]] = arith.constant dense<2> : tensor<1xi32> +// CHECK: %[[VAL_10:.*]] = "tf.Min"(%[[VAL_0]], %[[VAL_9]]) <{keep_dims = false}> : {{.*}} -> tensor<4x32xf32> +// CHECK: %[[VAL_11:.*]] = "tf.ArgMin"(%[[VAL_0]], %[[VAL_9]]) : {{.*}} -> tensor<4x32xi32> +// CHECK: return %[[VAL_10]], %[[VAL_11]] +// CHECK: } +func.func @convert_argmin(%arg0: tensor<4x32x256xf32>) -> (tensor<4x32xf32>, tensor<4x32xi32>) { + %0 = mhlo.constant dense<0x7F800000> : tensor + %1 = mhlo.constant dense<0> : tensor + %2 = "mhlo.iota"() <{iota_dimension = 0 : i64}> : () -> tensor<256xi32> + %3 = "mhlo.broadcast_in_dim"(%2) <{broadcast_dimensions = dense<2> : tensor<1xi64>}> : (tensor<256xi32>) -> tensor<4x32x256xi32> + %4:2 = "mhlo.reduce"(%arg0, %3, %0, %1) ({ + ^bb0(%arg1: tensor, %arg2: tensor, %arg3: tensor, %arg4: tensor): + %7 = "mhlo.compare"(%arg1, %arg3) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %8 = "mhlo.compare"(%arg1, %arg1) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %9 = mhlo.or %7, %8 : tensor + %10 = "mhlo.select"(%9, %arg1, %arg3) : (tensor, tensor, tensor) -> tensor + %11 = "mhlo.compare"(%arg1, %arg3) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %12 = "mhlo.compare"(%arg2, %arg4) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %13 = mhlo.and %11, %12 : tensor + %14 = mhlo.or %9, %13 : tensor + %15 = "mhlo.select"(%14, %arg2, %arg4) : (tensor, tensor, tensor) -> tensor + "mhlo.return"(%10, %15) : (tensor, tensor) -> () + }) {dimensions = dense<2> : tensor<1xi64>} : (tensor<4x32x256xf32>, tensor<4x32x256xi32>, tensor, tensor) -> (tensor<4x32xf32>, tensor<4x32xi32>) + func.return %4#0, %4#1 : tensor<4x32xf32>, tensor<4x32xi32> +} + +// CHECK-LABEL: func @convert_argmin_i16( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xi16>) -> (tensor, tensor) { +// CHECK: %[[VAL_9:.*]] = arith.constant dense<0> : tensor<1xi32> +// CHECK: %[[VAL_10:.*]] = "tf.Min"(%[[VAL_0]], %[[VAL_9]]) <{keep_dims = false}> : {{.*}} -> tensor +// CHECK: %[[VAL_11:.*]] = "tf.ArgMin"(%[[VAL_0]], %[[VAL_9]]) : {{.*}} -> tensor +// CHECK: return %[[VAL_10]], %[[VAL_11]] +// CHECK: } +func.func @convert_argmin_i16(%arg0: tensor<2xi16>) -> (tensor, tensor) { + %0 = mhlo.constant dense : tensor + %1 = "mhlo.iota"() <{iota_dimension = 0 : i64}> : () -> tensor<2xi32> + %2 = mhlo.constant dense<32767> : tensor + %3 = mhlo.constant dense<0> : tensor + %4:2 = "mhlo.reduce"(%arg0, %1, %2, %3) ({ + ^bb0(%arg1: tensor, %arg2: tensor, %arg3: tensor, %arg4: tensor): + %11 = mhlo.constant dense : tensor + %12 = "mhlo.compare"(%arg1, %arg3) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %13 = "mhlo.select"(%12, %arg1, %arg3) : (tensor, tensor, tensor) -> tensor + %14 = "mhlo.compare"(%arg1, %arg3) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %15 = "mhlo.compare"(%arg2, %arg4) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %16 = mhlo.and %14, %15 : tensor + %17 = mhlo.or %12, %16 : tensor + %18 = "mhlo.select"(%17, %arg2, %arg4) : (tensor, tensor, tensor) -> tensor + "mhlo.return"(%13, %18) : (tensor, tensor) -> () + }) {dimensions = dense<0> : tensor<1xi64>} : (tensor<2xi16>, tensor<2xi32>, tensor, tensor) -> (tensor, tensor) + func.return %4#0, %4#1 : tensor, tensor +} + + +// CHECK-LABEL: func @convert_argmin_constant( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2x2x4xf32>) -> (tensor<2x2xf32>, tensor<2x2xi32>) { +// CHECK-DAG: %[[VAL_1:.*]] = "tf.Const"() <{value = dense<0x7F800000> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_3:.*]] = "tf.Const"() <{value = dense<{{\[\[}}[0, 1, 2, 3], [0, 1, 2, 3]], {{\[\[}}0, 1, 2, 3], [0, 1, 2, 3]]]> : tensor<2x2x4xi32>}> : () -> tensor<2x2x4xi32> +// CHECK-DAG: %[[VAL_4:.*]] = arith.constant dense<2> : tensor<1xi32> +// CHECK: %[[VAL_5:.*]] = "tf.Min"(%[[VAL_0]], %[[VAL_4]]) <{keep_dims = false}> : (tensor<2x2x4xf32>, tensor<1xi32>) -> tensor<2x2xf32> +// CHECK: %[[VAL_6:.*]] = "tf.ArgMin"(%[[VAL_0]], %[[VAL_4]]) : (tensor<2x2x4xf32>, tensor<1xi32>) -> tensor<2x2xi32> +// CHECK: return %[[VAL_5]], %[[VAL_6]] : tensor<2x2xf32>, tensor<2x2xi32> +// CHECK: } +func.func @convert_argmin_constant(%arg0: tensor<2x2x4xf32>) -> (tensor<2x2xf32>, tensor<2x2xi32>) { + %0 = mhlo.constant dense<0x7F800000> : tensor + %1 = mhlo.constant dense<0> : tensor + %3 = mhlo.constant dense<[[[0, 1, 2, 3], [0, 1, 2, 3]], [[0, 1, 2, 3], [0, 1, 2, 3]]]> : tensor<2x2x4xi32> + %4:2 = "mhlo.reduce"(%arg0, %3, %0, %1) ({ + ^bb0(%arg1: tensor, %arg2: tensor, %arg3: tensor, %arg4: tensor): + %7 = "mhlo.compare"(%arg1, %arg3) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %8 = "mhlo.compare"(%arg1, %arg1) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %9 = mhlo.or %7, %8 : tensor + %10 = "mhlo.select"(%9, %arg1, %arg3) : (tensor, tensor, tensor) -> tensor + %11 = "mhlo.compare"(%arg1, %arg3) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %12 = "mhlo.compare"(%arg2, %arg4) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %13 = mhlo.and %11, %12 : tensor + %14 = mhlo.or %9, %13 : tensor + %15 = "mhlo.select"(%14, %arg2, %arg4) : (tensor, tensor, tensor) -> tensor + "mhlo.return"(%10, %15) : (tensor, tensor) -> () + }) {dimensions = dense<2> : tensor<1xi64>} : (tensor<2x2x4xf32>, tensor<2x2x4xi32>, tensor, tensor) -> (tensor<2x2xf32>, tensor<2x2xi32>) + func.return %4#0, %4#1 : tensor<2x2xf32>, tensor<2x2xi32> +} + +// CHECK-LABEL: func.func @convert_argmin_bool( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<2xi1>) -> tensor { +// CHECK-DAG: %[[VAL_1:.*]] = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<2> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_3:.*]] = "tf.Const"() <{value = dense<1> : tensor}> : () -> tensor +// CHECK: %[[VAL_4:.*]] = "tf.Range"(%[[VAL_1]], %[[VAL_2]], %[[VAL_3]]) : (tensor, tensor, tensor) -> tensor<2xi32> +// CHECK-DAG: %[[VAL_5:.*]] = "tf.Const"() <{value = dense : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_6:.*]] = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_7:.*]] = arith.constant dense<0> : tensor<1xi32> +// CHECK: %[[VAL_8:.*]] = "tf.All"(%[[VAL_0]], %[[VAL_7]]) <{keep_dims = false}> : (tensor<2xi1>, tensor<1xi32>) -> tensor +// CHECK: %[[VAL_9:.*]] = "tf.ArgMin"(%[[VAL_0]], %[[VAL_7]]) : (tensor<2xi1>, tensor<1xi32>) -> tensor +// CHECK: return %[[VAL_9]] : tensor +// CHECK: } +func.func @convert_argmin_bool(%arg0: tensor<2xi1>) -> tensor { + %0 = "mhlo.iota"() <{iota_dimension = 0 : i64}> : () -> tensor<2xi32> + %1 = mhlo.constant dense : tensor + %2 = mhlo.constant dense<0> : tensor + %3:2 = mhlo.reduce(%arg0 init: %1), (%0 init: %2) across dimensions = [0] : (tensor<2xi1>, tensor<2xi32>, tensor, tensor) -> (tensor, tensor) + reducer(%arg1: tensor, %arg3: tensor) (%arg2: tensor, %arg4: tensor) { + %4 = mhlo.compare LT, %arg1, %arg3 : (tensor, tensor) -> tensor + %5 = mhlo.select %4, %arg1, %arg3 : tensor, tensor + %6 = mhlo.compare EQ, %arg1, %arg3 : (tensor, tensor) -> tensor + %7 = mhlo.compare LT, %arg2, %arg4 : (tensor, tensor) -> tensor + %8 = mhlo.and %6, %7 : tensor + %9 = mhlo.or %4, %8 : tensor + %10 = mhlo.select %9, %arg2, %arg4 : tensor, tensor + mhlo.return %5, %10 : tensor, tensor + } + return %3#1 : tensor +} + +// CHECK-LABEL: func @convert_argmax_with_reshaped_iota( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<1x32x1xf32>) -> (tensor<1x1xf32>, tensor<1x1xi32>) { +// CHECK-DAG: %[[VAL_1:.*]] = "tf.Const"() <{value = dense<0xFF800000> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_3:.*]] = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_4:.*]] = "tf.Const"() <{value = dense<32> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_5:.*]] = "tf.Const"() <{value = dense<1> : tensor}> : () -> tensor +// CHECK: %[[VAL_6:.*]] = "tf.Range"(%[[VAL_3]], %[[VAL_4]], %[[VAL_5]]) : (tensor, tensor, tensor) -> tensor<32xi32> +// CHECK-DAG: %[[VAL_7:.*]] = arith.constant dense<[1, 32, 1]> : tensor<3xi64> +// CHECK: %[[VAL_8:.*]] = "tf.Reshape"(%[[VAL_6]], %[[VAL_7]]) : (tensor<32xi32>, tensor<3xi64>) -> tensor<1x32x1xi32> +// CHECK-DAG: %[[VAL_9:.*]] = arith.constant dense<1> : tensor<1xi32> +// CHECK: %[[VAL_10:.*]] = "tf.Max"(%[[VAL_0]], %[[VAL_9]]) <{keep_dims = false}> : (tensor<1x32x1xf32>, tensor<1xi32>) -> tensor<1x1xf32> +// CHECK: %[[VAL_11:.*]] = "tf.ArgMax"(%[[VAL_0]], %[[VAL_9]]) : (tensor<1x32x1xf32>, tensor<1xi32>) -> tensor<1x1xi32> +// CHECK: return %[[VAL_10]], %[[VAL_11]] : tensor<1x1xf32>, tensor<1x1xi32> +// CHECK: } +func.func @convert_argmax_with_reshaped_iota(%arg0: tensor<1x32x1xf32>) -> (tensor<1x1xf32>, tensor<1x1xi32>) { + %0 = mhlo.constant dense<0xFF800000> : tensor + %1 = mhlo.constant dense<0> : tensor + %2 = "mhlo.iota"() <{iota_dimension = 0 : i64}> : () -> tensor<32xi32> + %3 = "mhlo.reshape"(%2) : (tensor<32xi32>) -> tensor<1x32x1xi32> + %4:2 = mhlo.reduce(%arg0 init: %0), (%3 init: %1) across dimensions = [1] : (tensor<1x32x1xf32>, tensor<1x32x1xi32>, tensor, tensor) -> (tensor<1x1xf32>, tensor<1x1xi32>) + reducer(%arg1: tensor, %arg3: tensor) (%arg2: tensor, %arg4: tensor) { + %5 = "mhlo.compare"(%arg1, %arg3) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %6 = "mhlo.compare"(%arg1, %arg1) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %7 = mhlo.or %5, %6 : tensor + %8 = "mhlo.select"(%7, %arg1, %arg3) : (tensor, tensor, tensor) -> tensor + %9 = "mhlo.compare"(%arg1, %arg3) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %10 = "mhlo.compare"(%arg2, %arg4) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %11 = mhlo.and %9, %10 : tensor + %12 = mhlo.or %7, %11 : tensor + %13 = "mhlo.select"(%12, %arg2, %arg4) : (tensor, tensor, tensor) -> tensor + "mhlo.return"(%8, %13) : (tensor, tensor) -> () + } + func.return %4#0, %4#1 : tensor<1x1xf32>, tensor<1x1xi32> +} + +// CHECK-LABEL: func @convert_not( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<5x3x1xi1>) -> tensor<5x3x1xi1> { +// CHECK: %[[VAL_1:.*]] = "tf.LogicalNot"(%[[VAL_0]]) : {{.*}} -> tensor<5x3x1xi1> +// CHECK: return %[[VAL_1]] : tensor<5x3x1xi1> +// CHECK: } +func.func @convert_not(%arg0: tensor<5x3x1xi1>) -> tensor<5x3x1xi1> { + %0 = "mhlo.not"(%arg0): (tensor<5x3x1xi1>) -> (tensor<5x3x1xi1>) + func.return %0 : tensor<5x3x1xi1> +} + +// CHECK-LABEL: func @convert_not_i8( +// CHECK-SAME: %[[ARG:.*]]: tensor<7x9x11xi8>) -> tensor<7x9x11xi8> { +// CHECK: %[[CST:.*]] = "tf.Const"() <{value = dense<-1> : tensor}> : () -> tensor +// CHECK: %[[RES:.*]] = "tf.BitwiseXor"(%[[ARG]], %[[CST]]) : (tensor<7x9x11xi8>, tensor) -> tensor<7x9x11xi8> +// CHECK: return %[[RES]] : tensor<7x9x11xi8> +// CHECK: } +func.func @convert_not_i8(%arg0: tensor<7x9x11xi8>) -> tensor<7x9x11xi8> { + %0 = "mhlo.not"(%arg0): (tensor<7x9x11xi8>) -> (tensor<7x9x11xi8>) + func.return %0 : tensor<7x9x11xi8> +} + +// CHECK-LABEL: func @convert_not_i16( +// CHECK-SAME: %[[ARG:.*]]: tensor<7x9x11xi16>) -> tensor<7x9x11xi16> { +// CHECK: %[[CST:.*]] = "tf.Const"() <{value = dense<-1> : tensor}> : () -> tensor +// CHECK: %[[RES:.*]] = "tf.BitwiseXor"(%[[ARG]], %[[CST]]) : (tensor<7x9x11xi16>, tensor) -> tensor<7x9x11xi16> +// CHECK: return %[[RES]] : tensor<7x9x11xi16> +// CHECK: } +func.func @convert_not_i16(%arg0: tensor<7x9x11xi16>) -> tensor<7x9x11xi16> { + %0 = "mhlo.not"(%arg0): (tensor<7x9x11xi16>) -> (tensor<7x9x11xi16>) + func.return %0 : tensor<7x9x11xi16> +} + +// CHECK-LABEL: func @convert_not_i32( +// CHECK-SAME: %[[ARG:.*]]: tensor<7x9x11xi32>) -> tensor<7x9x11xi32> { +// CHECK: %[[CST:.*]] = "tf.Const"() <{value = dense<-1> : tensor}> : () -> tensor +// CHECK: %[[RES:.*]] = "tf.BitwiseXor"(%[[ARG]], %[[CST]]) : (tensor<7x9x11xi32>, tensor) -> tensor<7x9x11xi32> +// CHECK: return %[[RES]] : tensor<7x9x11xi32> +// CHECK: } +func.func @convert_not_i32(%arg0: tensor<7x9x11xi32>) -> tensor<7x9x11xi32> { + %0 = "mhlo.not"(%arg0): (tensor<7x9x11xi32>) -> (tensor<7x9x11xi32>) + func.return %0 : tensor<7x9x11xi32> +} + +// CHECK-LABEL: func @convert_not_i64( +// CHECK-SAME: %[[ARG:.*]]: tensor<7x9x11xi64>) -> tensor<7x9x11xi64> { +// CHECK: %[[CST:.*]] = "tf.Const"() <{value = dense<-1> : tensor}> : () -> tensor +// CHECK: %[[RES:.*]] = "tf.BitwiseXor"(%[[ARG]], %[[CST]]) : (tensor<7x9x11xi64>, tensor) -> tensor<7x9x11xi64> +// CHECK: return %[[RES]] : tensor<7x9x11xi64> +// CHECK: } +func.func @convert_not_i64(%arg0: tensor<7x9x11xi64>) -> tensor<7x9x11xi64> { + %0 = "mhlo.not"(%arg0): (tensor<7x9x11xi64>) -> (tensor<7x9x11xi64>) + func.return %0 : tensor<7x9x11xi64> +} + +// CHECK-LABEL: func @convert_not_ui8( +// CHECK-SAME: %[[ARG:.*]]: tensor<7x9x11xui8>) -> tensor<7x9x11xui8> { +// CHECK: %[[CST:.*]] = "tf.Const"() <{value = dense<255> : tensor}> : () -> tensor +// CHECK: %[[RES:.*]] = "tf.BitwiseXor"(%[[ARG]], %[[CST]]) : (tensor<7x9x11xui8>, tensor) -> tensor<7x9x11xui8> +// CHECK: return %[[RES]] : tensor<7x9x11xui8> +// CHECK: } +func.func @convert_not_ui8(%arg0: tensor<7x9x11xui8>) -> tensor<7x9x11xui8> { + %0 = "mhlo.not"(%arg0): (tensor<7x9x11xui8>) -> (tensor<7x9x11xui8>) + func.return %0 : tensor<7x9x11xui8> +} + +// CHECK-LABEL: func @convert_not_ui16( +// CHECK-SAME: %[[ARG:.*]]: tensor<7x9x11xui16>) -> tensor<7x9x11xui16> { +// CHECK: %[[CST:.*]] = "tf.Const"() <{value = dense<65535> : tensor}> : () -> tensor +// CHECK: %[[RES:.*]] = "tf.BitwiseXor"(%[[ARG]], %[[CST]]) : (tensor<7x9x11xui16>, tensor) -> tensor<7x9x11xui16> +// CHECK: return %[[RES]] : tensor<7x9x11xui16> +// CHECK: } +func.func @convert_not_ui16(%arg0: tensor<7x9x11xui16>) -> tensor<7x9x11xui16> { + %0 = "mhlo.not"(%arg0): (tensor<7x9x11xui16>) -> (tensor<7x9x11xui16>) + func.return %0 : tensor<7x9x11xui16> +} + +// CHECK-LABEL: func @convert_not_ui32( +// CHECK-SAME: %[[ARG:.*]]: tensor<7x9x11xui32>) -> tensor<7x9x11xui32> { +// CHECK: %[[CST:.*]] = "tf.Const"() <{value = dense<4294967295> : tensor}> : () -> tensor +// CHECK: %[[RES:.*]] = "tf.BitwiseXor"(%[[ARG]], %[[CST]]) : (tensor<7x9x11xui32>, tensor) -> tensor<7x9x11xui32> +// CHECK: return %[[RES]] : tensor<7x9x11xui32> +// CHECK: } +func.func @convert_not_ui32(%arg0: tensor<7x9x11xui32>) -> tensor<7x9x11xui32> { + %0 = "mhlo.not"(%arg0): (tensor<7x9x11xui32>) -> (tensor<7x9x11xui32>) + func.return %0 : tensor<7x9x11xui32> +} + +// CHECK-LABEL: func @convert_not_ui64( +// CHECK-SAME: %[[ARG:.*]]: tensor<7x9x11xui64>) -> tensor<7x9x11xui64> { +// CHECK: %[[CST:.*]] = "tf.Const"() <{value = dense<18446744073709551615> : tensor}> : () -> tensor +// CHECK: %[[RES:.*]] = "tf.BitwiseXor"(%[[ARG]], %[[CST]]) : (tensor<7x9x11xui64>, tensor) -> tensor<7x9x11xui64> +// CHECK: return %[[RES]] : tensor<7x9x11xui64> +// CHECK: } +func.func @convert_not_ui64(%arg0: tensor<7x9x11xui64>) -> tensor<7x9x11xui64> { + %0 = "mhlo.not"(%arg0): (tensor<7x9x11xui64>) -> (tensor<7x9x11xui64>) + func.return %0 : tensor<7x9x11xui64> +} + +// ----- + +// CHECK-LABEL: func @while_with_variadic() -> (tensor, tensor, tensor) { +// CHECK-DAG: %[[CST_0:.*]] = arith.constant dense<1> : tensor +// CHECK-DAG: %[[CST_1:.*]] = arith.constant dense<0> : tensor +// CHECK-DAG: %[[CST_2:.*]] = arith.constant dense<1000> : tensor +// CHECK: %[[WHILEREGION_0:.*]]:3 = "tf.WhileRegion"(%[[CST_1]], %[[CST_0]], %[[CST_2]]) <{is_stateless = false, parallel_iterations = 10 : i64}> ({ +// CHECK: ^bb0(%arg0: tensor, %arg1: tensor, %arg2: tensor): +// CHECK: %[[LESS_0:.*]] = "tf.Less"(%arg0, %arg2) : (tensor, tensor) -> tensor +// CHECK: "tf.Yield"(%[[LESS_0]]) : (tensor) -> () +// CHECK: }, { +// CHECK: ^bb0(%arg0: tensor, %arg1: tensor, %arg2: tensor): +// CHECK: %[[ADDV2_0:.*]] = "tf.AddV2"(%arg0, %arg1) : (tensor, tensor) -> tensor +// CHECK: "tf.Yield"(%[[ADDV2_0]], %arg1, %arg2) : (tensor, tensor, tensor) -> () +// CHECK: }) : (tensor, tensor, tensor) -> (tensor, tensor, tensor) +// CHECK: return %[[WHILEREGION_0]]#0, %[[WHILEREGION_0]]#1, %[[WHILEREGION_0]]#2 : tensor, tensor, tensor +// CHECK: } +func.func @while_with_variadic() -> (tensor, tensor, tensor) { + %cst = arith.constant dense<1> : tensor + %cst_0 = arith.constant dense<0> : tensor + %cst_1 = arith.constant dense<1000> : tensor + %0:3 = "mhlo.while"(%cst_0, %cst, %cst_1) ({ + ^bb0(%arg0: tensor, %arg1: tensor, %arg2: tensor): + %1 = "mhlo.compare"(%arg0, %arg2) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + "mhlo.return"(%1) : (tensor) -> () + }, { + ^bb0(%arg0: tensor, %arg1: tensor, %arg2: tensor): + %1 = mhlo.add %arg0, %arg1 : tensor + "mhlo.return"(%1, %arg1, %arg2) : (tensor, tensor, tensor) -> () + }) : (tensor, tensor, tensor) -> (tensor, tensor, tensor) + func.return %0#0, %0#1, %0#2 : tensor, tensor, tensor +} + +// ----- + +// CHECK-LABEL: func @while_with_reduce(%arg0: tensor<1x256xf32>, %arg1: tensor<1xf32>) -> (tensor, tensor, tensor, tensor<1xf32>) { +// CHECK-DAG: %[[CST_0:.*]] = arith.constant dense<1> : tensor +// CHECK-DAG: %[[CST_1:.*]] = arith.constant dense<0> : tensor +// CHECK-DAG: %[[CST_2:.*]] = arith.constant dense<1000> : tensor +// CHECK: %[[WHILEREGION_0:.*]]:5 = "tf.WhileRegion"(%[[CST_1]], %[[CST_0]], %[[CST_2]], %arg0, %arg1) <{is_stateless = false, parallel_iterations = 10 : i64}> ({ +// CHECK: ^bb0(%arg2: tensor, %arg3: tensor, %arg4: tensor, %arg5: tensor<1x256xf32>, %arg6: tensor<1xf32>): +// CHECK: %[[LESS_0:.*]] = "tf.Less"(%arg2, %arg4) : (tensor, tensor) -> tensor +// CHECK: "tf.Yield"(%[[LESS_0]]) : (tensor) -> () +// CHECK: }, { +// CHECK: ^bb0(%arg2: tensor, %arg3: tensor, %arg4: tensor, %arg5: tensor<1x256xf32>, %arg6: tensor<1xf32>): +// CHECK: %[[ADDV2_0:.*]] = "tf.AddV2"(%arg2, %arg3) : (tensor, tensor) -> tensor +// CHECK-DAG: %[[CONST_0:.*]] = "tf.Const"() <{value = dense<0.000000e+00> : tensor}> : () -> tensor +// CHECK-DAG: %[[CONST_1:.*]] = "tf.Const"() <{value = dense<1> : tensor<1xi64>}> : () -> tensor<1xi64> +// CHECK: %[[SUM_0:.*]] = "tf.Sum"(%arg5, %[[CONST_1]]) <{keep_dims = false}> : (tensor<1x256xf32>, tensor<1xi64>) -> tensor<1xf32> +// CHECK: %[[ADDV2_1:.*]] = "tf.AddV2"(%[[SUM_0]], %arg6) : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> +// CHECK: "tf.Yield"(%[[ADDV2_0]], %arg3, %arg4, %arg5, %[[ADDV2_1]]) : (tensor, tensor, tensor, tensor<1x256xf32>, tensor<1xf32>) -> () +// CHECK: }) : (tensor, tensor, tensor, tensor<1x256xf32>, tensor<1xf32>) -> (tensor, tensor, tensor, tensor<1x256xf32>, tensor<1xf32>) +// CHECK: return %[[WHILEREGION_0]]#0, %[[WHILEREGION_0]]#1, %[[WHILEREGION_0]]#2, %[[WHILEREGION_0]]#4 : tensor, tensor, tensor, tensor<1xf32> +// CHECK: } +func.func @while_with_reduce(%arg0: tensor<1x256xf32>, %arg1: tensor<1xf32>) -> (tensor, tensor, tensor, tensor<1xf32>) { + %cst = arith.constant dense<1> : tensor + %cst_0 = arith.constant dense<0> : tensor + %cst_1 = arith.constant dense<1000> : tensor + %0:5 = "mhlo.while"(%cst_0, %cst, %cst_1, %arg0 , %arg1) ({ + ^bb0(%arg2: tensor, %arg3: tensor, %arg4: tensor, %arg5: tensor<1x256xf32>, %arg6: tensor<1xf32>): + %1 = "mhlo.compare"(%arg2, %arg4) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + "mhlo.return"(%1) : (tensor) -> () + }, { + ^bb0(%arg2: tensor, %arg3: tensor, %arg4: tensor, %arg5: tensor<1x256xf32>, %arg6: tensor<1xf32>): + %1 = mhlo.add %arg2, %arg3 : tensor + %2 = mhlo.constant dense<0.000000e+00> : tensor + %3 = "mhlo.reduce"(%arg5, %2) ({ + ^bb0(%arg7: tensor, %arg8: tensor): + %4 = mhlo.add %arg7, %arg8 : tensor + "mhlo.return"(%4) : (tensor) -> () + }) {dimensions = dense<1> : tensor<1xi64>} : (tensor<1x256xf32>, tensor) -> tensor<1xf32> + %5 = mhlo.add %3, %arg6 : tensor<1xf32> + "mhlo.return"(%1, %arg3, %arg4, %arg5, %5) : (tensor, tensor, tensor, tensor<1x256xf32>, tensor<1xf32>) -> () + }) : (tensor, tensor, tensor, tensor<1x256xf32>, tensor<1xf32>) -> (tensor, tensor, tensor, tensor<1x256xf32>, tensor<1xf32>) + func.return %0#0, %0#1, %0#2, %0#4: tensor, tensor, tensor, tensor<1xf32> +} + +// ----- + +// CHECK-LABEL: func @if +// CHECK-DAG: %[[CST_0:.*]] = arith.constant dense<0> : tensor +// CHECK-DAG: %[[CST_1:.*]] = arith.constant dense<1000> : tensor +// CHECK: %[[RES:.*]] = "tf.IfRegion"(%arg0) <{is_stateless = false}> ({ +// CHECK: "tf.Yield"(%[[CST_0]]) : (tensor) -> () +// CHECK: }, { +// CHECK: "tf.Yield"(%[[CST_1]]) : (tensor) -> () +// CHECK: }) : (tensor) -> tensor +// CHECK: return %[[RES]] +func.func @if(%arg0: tensor) -> (tensor) { + %cst_0 = arith.constant dense<0> : tensor + %cst_1 = arith.constant dense<1000> : tensor + %1 = "mhlo.if"(%arg0) ({ + "mhlo.return"(%cst_0) : (tensor) -> () + }, { + "mhlo.return"(%cst_1) : (tensor) -> () + }) : (tensor) -> tensor + func.return %1: tensor +} + +// CHECK-LABEL: func @convert_dynamic_update_slice( +// CHECK-SAME: %[[VAL_0:[a-z0-9]*]]: tensor<28x1x100xf32>, +// CHECK-SAME: %[[VAL_1:[a-z0-9]*]]: tensor<1x1x100xf32>, +// CHECK-SAME: %[[VAL_2:[a-z0-9]*]]: tensor, +// CHECK-SAME: %[[VAL_3:[a-z0-9]*]]: tensor, +// CHECK-SAME: %[[VAL_4:[a-z0-9]*]]: tensor) -> tensor<28x1x100xf32> { +// CHECK: %0 = "tf.Pack"(%arg2, %arg3, %arg4) <{axis = 0 : i64}> : (tensor, tensor, tensor) -> tensor<3xi32> +// CHECK: %1 = "tf.XlaDynamicUpdateSlice"(%arg0, %arg1, %0) : (tensor<28x1x100xf32>, tensor<1x1x100xf32>, tensor<3xi32>) -> tensor<28x1x100xf32> +// CHECK: return %1 : tensor<28x1x100xf32> +func.func @convert_dynamic_update_slice(%arg0: tensor<28x1x100xf32>, %arg1: tensor<1x1x100xf32>, %arg2: tensor, %arg3: tensor, %arg4: tensor) -> tensor<28x1x100xf32> { + %0 = "mhlo.dynamic_update_slice"(%arg0, %arg1, %arg2, %arg3, %arg4) : (tensor<28x1x100xf32>, tensor<1x1x100xf32>, tensor, tensor, tensor) -> tensor<28x1x100xf32> + func.return %0 : tensor<28x1x100xf32> +} + +// CHECK-LABEL: func @dynamic_update_slice_inputs_have_dynamic_dim( +// CHECK-SAME: %arg0: tensor, +// CHECK-SAME: %arg1: tensor, +// CHECK-SAME: %arg2: tensor, +// CHECK-SAME: %arg3: tensor) -> tensor { +// CHECK: %0 = "tf.Pack"(%arg2, %arg3) <{axis = 0 : i64}> : (tensor, tensor) -> tensor<2xi32> +// CHECK: %1 = "tf.XlaDynamicUpdateSlice"(%arg0, %arg1, %0) : (tensor, tensor, tensor<2xi32>) -> tensor +// CHECK: return %1 : tensor +// CHECK: } +func.func @dynamic_update_slice_inputs_have_dynamic_dim(%arg0: tensor, %arg1: tensor, %arg2: tensor, %arg3: tensor) -> tensor { + %0 = mhlo.dynamic_update_slice %arg0, %arg1, %arg2, %arg3 : (tensor, tensor, tensor, tensor) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @dynamic_update_slice_operand_has_dynamic_dim( +// CHECK-SAME: %arg0: tensor<1x?x256xf32>, +// CHECK-SAME: %arg1: tensor<1x1x256xf32>, +// CHECK-SAME: %arg2: tensor, +// CHECK-SAME: %arg3: tensor, +// CHECK-SAME: %arg4: tensor) -> tensor<1x?x256xf32> { +// CHECK: %0 = "tf.Pack"(%arg2, %arg3, %arg4) <{axis = 0 : i64}> : (tensor, tensor, tensor) -> tensor<3xi32> +// CHECK: %1 = "tf.XlaDynamicUpdateSlice"(%arg0, %arg1, %0) : (tensor<1x?x256xf32>, tensor<1x1x256xf32>, tensor<3xi32>) -> tensor<1x?x256xf32> +// CHECK: return %1 : tensor<1x?x256xf32> +// CHECK: } +func.func @dynamic_update_slice_operand_has_dynamic_dim(%arg0: tensor<1x?x256xf32>, %arg1: tensor<1x1x256xf32>, %arg2: tensor, %arg3: tensor, %arg4: tensor) -> tensor<1x?x256xf32> { + %0 = mhlo.dynamic_update_slice %arg0, %arg1, %arg2, %arg3, %arg4 : (tensor<1x?x256xf32>, tensor<1x1x256xf32>, tensor, tensor, tensor) -> tensor<1x?x256xf32> + func.return %0 : tensor<1x?x256xf32> +} + +// CHECK-LABEL: func @convert_reduce_to_all( +// CHECK-SAME: %[[ARG_0:.*]]: tensor<1x2x3x4x5xi1>, +// CHECK-SAME: %[[ARG_1:.*]]: tensor<2xi64>) -> tensor<2x4x5xi1> { +// CHECK-DAG: %[[TRUE_CST:.*]] = "tf.Const"() <{value = dense : tensor}> : () -> tensor +// CHECK-DAG: %[[DIMENSIONS:.*]] = "tf.Const"() <{value = dense<[0, 2]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK: %[[VAL_0:.*]] = "tf.All"(%[[ARG_0]], %[[DIMENSIONS]]) <{keep_dims = false}> : (tensor<1x2x3x4x5xi1>, tensor<2xi64>) -> tensor<2x4x5xi1> +// CHECK: return %[[VAL_0:.*]] : tensor<2x4x5xi1> +// CHECK: } +func.func @convert_reduce_to_all(%arg0: tensor<1x2x3x4x5xi1>, %arg1: tensor<2xi64>) -> tensor<2x4x5xi1> { + %0 = mhlo.constant dense : tensor + %1 = "mhlo.reduce"(%arg0, %0) ({ + ^bb0(%arg2: tensor, %arg3: tensor): + %2 = mhlo.and %arg2, %arg3 : tensor + "mhlo.return"(%2) : (tensor) -> () + }) {dimensions = dense<[0, 2]> : tensor<2xi64>} : (tensor<1x2x3x4x5xi1>, tensor) -> tensor<2x4x5xi1> + func.return %1: tensor<2x4x5xi1> +} + +// CHECK-LABEL: func @convert_reduce_to_all_non_constant_init( +// CHECK-SAME: %[[ARG_0:.*]]: tensor, +// CHECK-SAME: %[[ARG_1:.*]]: tensor<1x2x3x4x5xi1>, +// CHECK-SAME: %[[ARG_2:.*]]: tensor<2xi64>) -> tensor<2x4x5xi1> { +// CHECK-DAG: %[[DIMENSIONS:.*]] = "tf.Const"() <{value = dense<[0, 2]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK: %[[VAL_0:.*]] = "tf.All"(%[[ARG_1]], %[[DIMENSIONS]]) <{keep_dims = false}> : (tensor<1x2x3x4x5xi1>, tensor<2xi64>) -> tensor<2x4x5xi1> +// CHECK: %[[VAL_1:.*]] = "tf.LogicalAnd"(%[[VAL_0]], %[[ARG_0]]) : (tensor<2x4x5xi1>, tensor) -> tensor<2x4x5xi1> +// CHECK: return %[[VAL_1:.*]] : tensor<2x4x5xi1> +// CHECK: } +func.func @convert_reduce_to_all_non_constant_init(%arg0: tensor, %arg1: tensor<1x2x3x4x5xi1>, %arg2: tensor<2xi64>) -> tensor<2x4x5xi1> { + %0 = "mhlo.reduce"(%arg1, %arg0) ({ + ^bb0(%arg3: tensor, %arg4: tensor): + %1 = mhlo.and %arg3, %arg4 : tensor + "mhlo.return"(%1) : (tensor) -> () + }) {dimensions = dense<[0, 2]> : tensor<2xi64>} : (tensor<1x2x3x4x5xi1>, tensor) -> tensor<2x4x5xi1> + func.return %0: tensor<2x4x5xi1> +} + +// CHECK-LABEL: func @convert_reduce_to_any( +// CHECK-SAME: %[[ARG_0:.*]]: tensor<1x2x3x4x5xi1>, +// CHECK-SAME: %[[ARG_1:.*]]: tensor<2xi64>) -> tensor<2x4x5xi1> { +// CHECK-DAG: %[[FALSE_CST:.*]] = "tf.Const"() <{value = dense : tensor}> : () -> tensor +// CHECK-DAG: %[[DIMENSIONS:.*]] = "tf.Const"() <{value = dense<[0, 2]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK: %[[VAL_0:.*]] = "tf.Any"(%[[ARG_0]], %[[DIMENSIONS]]) <{keep_dims = false}> : (tensor<1x2x3x4x5xi1>, tensor<2xi64>) -> tensor<2x4x5xi1> +// CHECK: return %[[VAL_0:.*]] : tensor<2x4x5xi1> +// CHECK: } +func.func @convert_reduce_to_any(%arg0: tensor<1x2x3x4x5xi1>, %arg1: tensor<2xi64>) -> tensor<2x4x5xi1> { + %0 = mhlo.constant dense : tensor + %1 = "mhlo.reduce"(%arg0, %0) ({ + ^bb0(%arg2: tensor, %arg3: tensor): + %2 = mhlo.or %arg2, %arg3 : tensor + "mhlo.return"(%2) : (tensor) -> () + }) {dimensions = dense<[0, 2]> : tensor<2xi64>} : (tensor<1x2x3x4x5xi1>, tensor) -> tensor<2x4x5xi1> + func.return %1: tensor<2x4x5xi1> +} + +// CHECK-LABEL: func @convert_reduce_to_any_non_constant_init( +// CHECK-SAME: %[[ARG_0:.*]]: tensor, +// CHECK-SAME: %[[ARG_1:.*]]: tensor<1x2x3x4x5xi1>, +// CHECK-SAME: %[[ARG_2:.*]]: tensor<2xi64>) -> tensor<2x4x5xi1> { +// CHECK-DAG: %[[DIMENSIONS:.*]] = "tf.Const"() <{value = dense<[0, 2]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK: %[[VAL_0:.*]] = "tf.Any"(%[[ARG_1]], %[[DIMENSIONS]]) <{keep_dims = false}> : (tensor<1x2x3x4x5xi1>, tensor<2xi64>) -> tensor<2x4x5xi1> +// CHECK: %[[VAL_1:.*]] = "tf.LogicalOr"(%[[VAL_0]], %[[ARG_0]]) : (tensor<2x4x5xi1>, tensor) -> tensor<2x4x5xi1> +// CHECK: return %[[VAL_1:.*]] : tensor<2x4x5xi1> +// CHECK: } +func.func @convert_reduce_to_any_non_constant_init(%arg0: tensor, %arg1: tensor<1x2x3x4x5xi1>, %arg2: tensor<2xi64>) -> tensor<2x4x5xi1> { + %0 = "mhlo.reduce"(%arg1, %arg0) ({ + ^bb0(%arg3: tensor, %arg4: tensor): + %1 = mhlo.or %arg3, %arg4 : tensor + "mhlo.return"(%1) : (tensor) -> () + }) {dimensions = dense<[0, 2]> : tensor<2xi64>} : (tensor<1x2x3x4x5xi1>, tensor) -> tensor<2x4x5xi1> + func.return %0: tensor<2x4x5xi1> +} + +// CHECK-LABEL: func @convert_sort_to_topk_iota_broadcast( +// CHECK-SAME: %[[ARG_0:.*]]: tensor<3x6xf32>) -> (tensor<3x6xf32>, tensor<3x6xi32>) { +// CHECK-DAG: %[[VAL_0:.*]] = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_1:.*]] = "tf.Const"() <{value = dense<6> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<1> : tensor}> : () -> tensor +// CHECK: %[[VAL_3:.*]] = "tf.Range"(%cst, %cst_0, %cst_1) : (tensor, tensor, tensor) -> tensor<6xi32> +// CHECK: %[[VAL_4:.*]] = arith.constant dense<[3, 6]> : tensor<2xi64> +// CHECK: %[[VAL_5:.*]] = "tf.BroadcastTo"(%0, %cst_2) : (tensor<6xi32>, tensor<2xi64>) -> tensor<3x6xi32> +// CHECK: %[[K:.*]] = "tf.Const"() <{value = dense<6> : tensor}> : () -> tensor +// CHECK: %[[VALUES:.*]], %[[INDICES:.*]] = "tf.TopKV2"(%[[ARG_0]], %[[K]]) <{sorted = true}> : (tensor<3x6xf32>, tensor) -> (tensor<3x6xf32>, tensor<3x6xi32>) +// CHECK: return %[[VALUES]], %[[INDICES]] : tensor<3x6xf32>, tensor<3x6xi32> +// CHECK: } +func.func @convert_sort_to_topk_iota_broadcast(%arg0: tensor<3x6xf32>) -> (tensor<3x6xf32>, tensor<3x6xi32>) { + %0 = "mhlo.iota"() <{ iota_dimension = 0 : i64 }> : () -> tensor<6xi32> + %1 = "mhlo.broadcast_in_dim"(%0) <{broadcast_dimensions = dense<[1]> : tensor<1xi64>, name = "broadcast.0"}> : (tensor<6xi32>) -> tensor<3x6xi32> + %2:2 = "mhlo.sort"(%arg0, %1) ({ + ^bb0(%arg1: tensor, %arg2: tensor, %arg3: tensor, %arg4: tensor): + %3 = "mhlo.compare"(%arg1, %arg2) {compare_type = #mhlo, comparison_direction = #mhlo} : (tensor, tensor) -> tensor + "mhlo.return"(%3) : (tensor) -> () + }) {dimension = 1 : i64, is_stable = true} : (tensor<3x6xf32>, tensor<3x6xi32>) -> (tensor<3x6xf32>, tensor<3x6xi32>) + func.return %2#0, %2#1 : tensor<3x6xf32>, tensor<3x6xi32> +} + +// CHECK-LABEL: func @convert_sort_to_topk_iotacst_broadcast( +// CHECK-SAME: %[[ARG_0:.*]]: tensor<3x6xf32>) -> (tensor<3x6xf32>, tensor<3x6xi32>) { +// CHECK-DAG: %[[VAL_0:.*]] = "tf.Const"() <{value = dense<[0, 1, 2, 3, 4, 5]> : tensor<6xi32>}> : () -> tensor<6xi32> +// CHECK-DAG: %[[VAL_1:.*]] = arith.constant dense<[3, 6]> : tensor<2xi64> +// CHECK: %[[VAL_2:.*]] = "tf.BroadcastTo"(%cst, %cst_0) : (tensor<6xi32>, tensor<2xi64>) -> tensor<3x6xi32> +// CHECK: %[[K:.*]] = "tf.Const"() <{value = dense<6> : tensor}> : () -> tensor +// CHECK: %[[VALUES:.*]], %[[INDICES:.*]] = "tf.TopKV2"(%[[ARG_0]], %[[K]]) <{sorted = true}> : (tensor<3x6xf32>, tensor) -> (tensor<3x6xf32>, tensor<3x6xi32>) +// CHECK: return %[[VALUES]], %[[INDICES]] : tensor<3x6xf32>, tensor<3x6xi32> +// CHECK: } +func.func @convert_sort_to_topk_iotacst_broadcast(%arg0: tensor<3x6xf32>) -> (tensor<3x6xf32>, tensor<3x6xi32>) { + %0 = mhlo.constant dense<[0, 1, 2, 3, 4, 5]> : tensor<6xi32> + %1 = "mhlo.broadcast_in_dim"(%0) <{broadcast_dimensions = dense<[1]> : tensor<1xi64>, name = "broadcast.0"}> : (tensor<6xi32>) -> tensor<3x6xi32> + %2:2 = "mhlo.sort"(%arg0, %1) ({ + ^bb0(%arg1: tensor, %arg2: tensor, %arg3: tensor, %arg4: tensor): + %3 = "mhlo.compare"(%arg1, %arg2) {compare_type = #mhlo, comparison_direction = #mhlo} : (tensor, tensor) -> tensor + "mhlo.return"(%3) : (tensor) -> () + }) {dimension = 1 : i64, is_stable = true} : (tensor<3x6xf32>, tensor<3x6xi32>) -> (tensor<3x6xf32>, tensor<3x6xi32>) + func.return %2#0, %2#1 : tensor<3x6xf32>, tensor<3x6xi32> +} + +// CHECK-LABEL: func @convert_sort_to_topk_const( +// CHECK-SAME: %[[ARG_0:.*]]: tensor<3x6xf32>) -> (tensor<3x6xf32>, tensor<3x6xi32>) { +// CHECK-DAG: %[[VAL_0:.*]] = "tf.Const"() <{value = dense<{{.*}}> : tensor<3x6xi32>}> : () -> tensor<3x6xi32> +// CHECK-DAG: %[[K:.*]] = "tf.Const"() <{value = dense<6> : tensor}> : () -> tensor +// CHECK: %[[VALUES:.*]], %[[INDICES:.*]] = "tf.TopKV2"(%[[ARG_0]], %[[K]]) <{sorted = true}> : (tensor<3x6xf32>, tensor) -> (tensor<3x6xf32>, tensor<3x6xi32>) +// CHECK: return %[[VALUES]], %[[INDICES]] : tensor<3x6xf32>, tensor<3x6xi32> +// CHECK: } +func.func @convert_sort_to_topk_const(%arg0: tensor<3x6xf32>) -> (tensor<3x6xf32>, tensor<3x6xi32>) { + %0 = mhlo.constant dense<[[0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5]]> : tensor<3x6xi32> + %1:2 = "mhlo.sort"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor, %arg3: tensor, %arg4: tensor): + %3 = "mhlo.compare"(%arg1, %arg2) {compare_type = #mhlo, comparison_direction = #mhlo} : (tensor, tensor) -> tensor + "mhlo.return"(%3) : (tensor) -> () + }) {dimension = 1 : i64, is_stable = true} : (tensor<3x6xf32>, tensor<3x6xi32>) -> (tensor<3x6xf32>, tensor<3x6xi32>) + func.return %1#0, %1#1 : tensor<3x6xf32>, tensor<3x6xi32> +} + +// CHECK-LABEL: func @not_convert_sort_to_topk +// CHECK-NOT: "tf.TopKV2" +func.func @not_convert_sort_to_topk(%arg0: tensor<3x6xf32>) -> (tensor<3x6xf32>, tensor<3x6xi32>) { + %0 = mhlo.constant dense<[[0, 1, 2, 3, 4, 4], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5]]> : tensor<3x6xi32> + %1:2 = "mhlo.sort"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor, %arg3: tensor, %arg4: tensor): + %4 = "mhlo.compare"(%arg1, %arg2) {compare_type = #mhlo, comparison_direction = #mhlo} : (tensor, tensor) -> tensor + "mhlo.return"(%4) : (tensor) -> () + }) {dimension = 1 : i64, is_stable = true} : (tensor<3x6xf32>, tensor<3x6xi32>) -> (tensor<3x6xf32>, tensor<3x6xi32>) + func.return %1#0, %1#1 : tensor<3x6xf32>, tensor<3x6xi32> +} + +// CHECK-LABEL: func @convert_remainder_for_int32( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<10x8xi32>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<10x8xi32>) -> tensor<10x8xi32> { +// CHECK: %[[VAL_2:.*]] = "tf.Mod"(%[[VAL_0]], %[[VAL_1]]) : (tensor<10x8xi32>, tensor<10x8xi32>) -> tensor<10x8xi32> +// CHECK: return %[[VAL_2]] : tensor<10x8xi32> +// CHECK: } +func.func @convert_remainder_for_int32(%arg0: tensor<10x8xi32>, %arg1: tensor<10x8xi32>) -> tensor<10x8xi32> { + %0 = mhlo.remainder %arg0, %arg1 : tensor<10x8xi32> + func.return %0 : tensor<10x8xi32> +} + +// CHECK-LABEL: func @convert_remainder_for_int64( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<10x8xi64>, +// CHECK-SAME: %[[VAL_1:.*]]: tensor<10x8xi64>) -> tensor<10x8xi64> { +// CHECK: %[[VAL_2:.*]] = "tf.Mod"(%[[VAL_0]], %[[VAL_1]]) : (tensor<10x8xi64>, tensor<10x8xi64>) -> tensor<10x8xi64> +// CHECK: return %[[VAL_2]] : tensor<10x8xi64> +// CHECK: } +func.func @convert_remainder_for_int64(%arg0: tensor<10x8xi64>, %arg1: tensor<10x8xi64>) -> tensor<10x8xi64> { + %0 = mhlo.remainder %arg0, %arg1 : tensor<10x8xi64> + func.return %0 : tensor<10x8xi64> +} + +// CHECK-LABEL: func @not_convert_remainder_for_int16( +// CHECK-NOT: "tf.Mod" +func.func @not_convert_remainder_for_int16(%arg0: tensor<10x8xi16>, %arg1: tensor<10x8xi16>) -> tensor<10x8xi16> { + %0 = mhlo.remainder %arg0, %arg1 : tensor<10x8xi16> + func.return %0 : tensor<10x8xi16> +} + +// CHECK-LABEL: func @not_convert_remainder_for_uint16( +// CHECK-NOT: "tf.Mod" +func.func @not_convert_remainder_for_uint16(%arg0: tensor<10x8xui16>, %arg1: tensor<10x8xui16>) -> tensor<10x8xui16> { + %0 = mhlo.remainder %arg0, %arg1 : tensor<10x8xui16> + func.return %0 : tensor<10x8xui16> +} + +// CHECK-LABEL: func @not_convert_remainder_for_uint32( +// CHECK-NOT: "tf.Mod" +func.func @not_convert_remainder_for_uint32(%arg0: tensor<10x8xui32>, %arg1: tensor<10x8xui32>) -> tensor<10x8xui32> { + %0 = mhlo.remainder %arg0, %arg1 : tensor<10x8xui32> + func.return %0 : tensor<10x8xui32> +} + +// CHECK-LABEL: func @not_convert_remainder_for_uint64( +// CHECK-NOT: "tf.Mod" +func.func @not_convert_remainder_for_uint64(%arg0: tensor<10x8xui64>, %arg1: tensor<10x8xui64>) -> tensor<10x8xui64> { + %0 = mhlo.remainder %arg0, %arg1 : tensor<10x8xui64> + func.return %0 : tensor<10x8xui64> +} + +// CHECK-LABEL: func @convert_population_count_i32( +// CHECK-SAME: %[[ARG_0:.*]]: tensor<8xi32> +// CHECK: %[[POP_CNT:.*]] = "tf.PopulationCount"(%[[ARG_0]]) : (tensor<8xi32>) -> tensor<8xui8> +// CHECK: %[[RES:.*]] = "tf.Cast"(%[[POP_CNT]]) <{Truncate = false}> : (tensor<8xui8>) -> tensor<8xi32> +// CHECK: return %[[RES]] +// CHECK: } +func.func @convert_population_count_i32(%arg0: tensor<8xi32>) -> tensor<8xi32> { + %0 = "mhlo.popcnt"(%arg0) : (tensor<8xi32>) -> tensor<8xi32> + func.return %0 : tensor<8xi32> +} + +// CHECK-LABEL: func @convert_population_count_ui8( +// CHECK-SAME: %[[ARG_0:.*]]: tensor<8xui8> +// CHECK: %[[POP_CNT:.*]] = "tf.PopulationCount"(%[[ARG_0]]) : (tensor<8xui8>) -> tensor<8xui8> +// CHECK: return %[[POP_CNT]] +// CHECK: } +func.func @convert_population_count_ui8(%arg0: tensor<8xui8>) -> tensor<8xui8> { + %0 = "mhlo.popcnt"(%arg0) : (tensor<8xui8>) -> tensor<8xui8> + func.return %0 : tensor<8xui8> +} + +// CHECK-LABEL: func @torch_index_select( +// CHECK: %[[AXIS:.+]] = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK: %[[RES:.+]] = "tf.GatherV2"(%arg0, %arg1, %[[AXIS]]) <{batch_dims = 0 : i64}> +// CHECK: return %[[RES]] + +func.func @torch_index_select(%arg0: tensor<2x1xf32>, %arg1: tensor<2xi32>) -> tensor<2x1xf32> { + %0 = "mhlo.torch_index_select"(%arg0, %arg1) { + batch_dims = 0 : i64, dim = 0 : i64 + } : (tensor<2x1xf32>, tensor<2xi32>) -> tensor<2x1xf32> + func.return %0 : tensor<2x1xf32> +} + +// CHECK-LABEL: func @lowered_cumsum( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<4x12xf32>) -> tensor<4x12xf32> { +// CHECK-DAG: %[[VAL_1:.*]] = "tf.Const"() <{value = dense<0.000000e+00> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK: %[[VAL_3:.*]] = "tf.Cumsum"(%[[VAL_0]], %[[VAL_2]]) <{exclusive = false, reverse = false}> : (tensor<4x12xf32>, tensor) -> tensor<4x12xf32> +// CHECK: return %[[VAL_3]] : tensor<4x12xf32> +// CHECK: } +func.func @lowered_cumsum(%arg0: tensor<4x12xf32>) -> tensor<4x12xf32> { + %0 = mhlo.constant dense<0.000000e+00> : tensor + %1 = "mhlo.reduce_window"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %2 = mhlo.add %arg1, %arg2 : tensor + "mhlo.return"(%2) : (tensor) -> () + }) {base_dilations = dense<1> : tensor<2xi64>, padding = dense<[[3, 0], [0, 0]]> : tensor<2x2xi64>, window_dilations = dense<1> : tensor<2xi64>, window_dimensions = dense<[4, 1]> : tensor<2xi64>, window_strides = dense<1> : tensor<2xi64>} : (tensor<4x12xf32>, tensor) -> tensor<4x12xf32> + func.return %1 : tensor<4x12xf32> +} + +// CHECK-LABEL: func @lowered_cumsum_trivial_attrs( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<4x12xf32>) -> tensor<4x12xf32> { +// CHECK-DAG: %[[VAL_1:.*]] = "tf.Const"() <{value = dense<0.000000e+00> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK: %[[VAL_3:.*]] = "tf.Cumsum"(%[[VAL_0]], %[[VAL_2]]) <{exclusive = false, reverse = false}> : (tensor<4x12xf32>, tensor) -> tensor<4x12xf32> +// CHECK: return %[[VAL_3]] : tensor<4x12xf32> +// CHECK: } +func.func @lowered_cumsum_trivial_attrs(%arg0: tensor<4x12xf32>) -> tensor<4x12xf32> { + %0 = mhlo.constant dense<0.000000e+00> : tensor + %1 = "mhlo.reduce_window"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %2 = mhlo.add %arg1, %arg2 : tensor + "mhlo.return"(%2) : (tensor) -> () + }) {padding = dense<[[3, 0], [0, 0]]> : tensor<2x2xi64>, window_dimensions = dense<[4, 1]> : tensor<2xi64>} : (tensor<4x12xf32>, tensor) -> tensor<4x12xf32> + func.return %1 : tensor<4x12xf32> +} + +// CHECK-LABEL: func @lowered_cumprod( +// CHECK-SAME: %[[VAL_0:.*]]: tensor<4x12xf32>) -> tensor<4x12xf32> { +// CHECK-DAG: %[[VAL_1:.*]] = "tf.Const"() <{value = dense<1.000000e+00> : tensor}> : () -> tensor +// CHECK-DAG: %[[VAL_2:.*]] = "tf.Const"() <{value = dense<1> : tensor}> : () -> tensor +// CHECK: %[[VAL_3:.*]] = "tf.Cumprod"(%[[VAL_0]], %[[VAL_2]]) <{exclusive = false, reverse = false}> : (tensor<4x12xf32>, tensor) -> tensor<4x12xf32> +// CHECK: return %[[VAL_3]] : tensor<4x12xf32> +// CHECK: } +func.func @lowered_cumprod(%arg0: tensor<4x12xf32>) -> tensor<4x12xf32> { + %0 = mhlo.constant dense<1.000000e+00> : tensor + %1 = "mhlo.reduce_window"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %2 = mhlo.multiply %arg1, %arg2 : tensor + "mhlo.return"(%2) : (tensor) -> () + }) {base_dilations = dense<1> : tensor<2xi64>, padding = dense<[[0, 0], [11, 0]]> : tensor<2x2xi64>, window_dilations = dense<1> : tensor<2xi64>, window_dimensions = dense<[1, 12]> : tensor<2xi64>, window_strides = dense<1> : tensor<2xi64>} : (tensor<4x12xf32>, tensor) -> tensor<4x12xf32> + func.return %1 : tensor<4x12xf32> +} + +// CHECK-LABEL: reduce_window_trivial_window_dims +// CHECK-NOT: "tf.Cumprod" +func.func @reduce_window_trivial_window_dims(%arg0: tensor<4x12xf32>) -> tensor<4x12xf32> { + %0 = mhlo.constant dense<1.000000e+00> : tensor + %1 = "mhlo.reduce_window"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %2 = mhlo.multiply %arg1, %arg2 : tensor + "mhlo.return"(%2) : (tensor) -> () + }) {padding = dense<0> : tensor<2x2xi64>, window_dimensions = dense<1> : tensor<2xi64>} : (tensor<4x12xf32>, tensor) -> tensor<4x12xf32> + func.return %1 : tensor<4x12xf32> +} + +// CHECK-LABEL: func @const_quant +// CHECK-NOT: "tf.Const" +func.func @const_quant() -> tensor<512x1x!quant.uniform> { + %0 = mhlo.constant() {value = dense<0> : tensor<512x1xi8>} : () -> tensor<512x1x!quant.uniform> + func.return %0 : tensor<512x1x!quant.uniform> +} + +// CHECK-LABEL: func @convert_dot_quant_type( +// CHECK-NOT: "tf.BatchMatMulV3" +func.func @convert_dot_quant_type(%arg0: tensor<1x256xf32>, %arg1: tensor<256x!quant.uniform>) -> tensor<1xf32> { + %0 = "mhlo.dot"(%arg0, %arg1) {precision_config = [#mhlo, #mhlo]} : (tensor<1x256xf32>, tensor<256x!quant.uniform>) -> tensor<1xf32> + func.return %0 : tensor<1xf32> +} + +// CHECK-LABEL: func @get_dimension_size( +// CHECK-SAME: %[[ARG_0:.*]]: tensor<4x256x?xf32>) -> tensor { +// CHECK %[[CST_0:.*]] = "tf.Const"() <{value = dense<256> : tensor}> : () -> tensor +// CHECK return %[[CST_0]] : tensor +func.func @get_dimension_size(%arg0: tensor<4x256x?xf32>) -> tensor { + %0 = "mhlo.get_dimension_size"(%arg0) <{dimension = 1 : i64}> : (tensor<4x256x?xf32>) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @get_dimension_size_dynamic( +// CHECK-SAME: %[[ARG_0:.*]]: tensor<4x256x?xf32>) -> tensor { +// CHECK %[[VAL_0:.*]] = "tf.Shape"(%[[ARG_0]]) : (tensor<4x256x?xf32>) -> tensor<3xi32> +// CHECK %[[CST_0:.*]] = "tf.Const"() <{value = dense<1> : tensor<1xi32>}> : () -> tensor<1xi32> +// CHECK %[[CST_1:.*]] = "tf.Const"() <{value = dense<2> : tensor<1xi64>}> : () -> tensor<1xi64> +// CHECK %[[VAL_1:.*]] = "tf.Slice"(%[[VAL_0]], %[[CST_1]], %[[CST_0]]) : (tensor<3xi32>, tensor<1xi64>, tensor<1xi32>) -> tensor<1xi32> +// CHECK %[[VAL_2:.*]] = "tf.Squeeze"(%[[VAL_1]]) <{squeeze_dims = [0]}> : (tensor<1xi32>) -> tensor +// CHECK return %[[VAL_2]] : tensor +func.func @get_dimension_size_dynamic(%arg0: tensor<4x256x?xf32>) -> tensor { + %0 = "mhlo.get_dimension_size"(%arg0) <{dimension = 2 : i64}> : (tensor<4x256x?xf32>) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @dynamic_iota_i32_1d( +// CHECK-SAME: %[[ARG_0:.*]]: tensor<1xi32>) -> tensor { +// CHECK-DAG: %[[CST_0:.*]] = "tf.Const"() <{value = dense<> : tensor<0xi32>}> : () -> tensor<0xi32> +// CHECK: %[[VAL_0:.*]] = "tf.Reshape"(%arg0, %[[CST_0]]) : (tensor<1xi32>, tensor<0xi32>) -> tensor +// CHECK-DAG: %[[CST_1:.*]] = "tf.Const"() <{value = dense<0> : tensor}> : () -> tensor +// CHECK-DAG: %[[CST_2:.*]] = "tf.Const"() <{value = dense<1> : tensor}> : () -> tensor +// CHECK: %[[VAL_1:.*]] = "tf.Range"(%[[CST_1]], %[[VAL_0]], %[[CST_2]]) : (tensor, tensor, tensor) -> tensor +// CHECK: return %[[VAL_1]] : tensor +func.func @dynamic_iota_i32_1d(%arg0: tensor<1xi32>) -> tensor { + %0 = "mhlo.dynamic_iota"(%arg0) <{iota_dimension = 0 : i64}> : (tensor<1xi32>) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @dynamic_iota_f32_1d( +// CHECK-SAME: %[[ARG_0:.*]]: tensor<1xi32>) -> tensor { +// CHECK: %[[VAL_0:.*]] = "tf.Cast"(%arg0) <{Truncate = false}> : (tensor<1xi32>) -> tensor<1xf32> +// CHECK-DAG: %[[CST_0:.*]] = "tf.Const"() <{value = dense<> : tensor<0xi32>}> : () -> tensor<0xi32> +// CHECK: %[[VAL_1:.*]] = "tf.Reshape"(%[[VAL_0]], %[[CST_0]]) : (tensor<1xf32>, tensor<0xi32>) -> tensor +// CHECK-DAG: %[[CST_1:.*]] = "tf.Const"() <{value = dense<0.000000e+00> : tensor}> : () -> tensor +// CHECK-DAG: %[[CST_2:.*]] = "tf.Const"() <{value = dense<1.000000e+00> : tensor}> : () -> tensor +// CHECK: %[[VAL_2:.*]] = "tf.Range"(%[[CST_1]], %[[VAL_1]], %[[CST_2]]) : (tensor, tensor, tensor) -> tensor +// CHECK: return %[[VAL_2]] : tensor +func.func @dynamic_iota_f32_1d(%arg0: tensor<1xi32>) -> tensor { + %0 = "mhlo.dynamic_iota"(%arg0) <{iota_dimension = 0 : i64}> : (tensor<1xi32>) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: func @real_dynamic_slice_strides_equal_to_1_signed( +// CHECK-SAME: %arg0: tensor<1x?x4x256xf32>, +// CHECK-SAME: %arg1: tensor<4xi32>, +// CHECK-SAME: %arg2: tensor<4xi32>) -> tensor<1x?x4x128xf32> { +// CHECK: %cst = "tf.Const"() <{value = dense<1> : tensor<4xi32>}> : () -> tensor<4xi32> +// CHECK: %0 = "tf.StridedSlice"(%arg0, %arg1, %arg2, %cst) <{begin_mask = 0 : i64, ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64}> : (tensor<1x?x4x256xf32>, tensor<4xi32>, tensor<4xi32>, tensor<4xi32>) -> tensor<1x?x4x128xf32> +// CHECK: return %0 : tensor<1x?x4x128xf32> +func.func @real_dynamic_slice_strides_equal_to_1_signed(%arg0: tensor<1x?x4x256xf32>, %arg1: tensor<4xi32>, %arg2: tensor<4xi32>) -> tensor<1x?x4x128xf32> { +%cst = mhlo.constant dense<1> : tensor<4xi32> +%0 = mhlo.real_dynamic_slice %arg0, %arg1, %arg2, %cst : (tensor<1x?x4x256xf32>, tensor<4xi32>, tensor<4xi32>, tensor<4xi32>) -> tensor<1x?x4x128xf32> +func.return %0 : tensor<1x?x4x128xf32> +} + +// CHECK-LABEL: func @real_dynamic_slice_strides_not_equal_to_1( +// CHECK-SAME: %arg0: tensor<1x?x2x4xf32>, +// CHECK-SAME: %arg1: tensor<4xi32>, +// CHECK-SAME: %arg2: tensor<4xi32>) -> tensor<1x?x1x2xf32> { +// CHECK %cst = "tf.Const"() <{value = dense<2> : tensor<4xi32>}> : () -> tensor<4xi32> +// CHECK %0 = "tf.StridedSlice"(%arg0, %arg1, %arg2, %cst) <{begin_mask = 0 : i64, ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64}> : (tensor<1x?x2x4xf32>, tensor<4xi32>, tensor<4xi32>, tensor<4xi32>) -> tensor<1x?x1x2xf32> +// CHECK return %0 : tensor<1x?x1x2xf32> +func.func @real_dynamic_slice_strides_not_equal_to_1(%arg0: tensor<1x?x2x4xf32>, %arg1: tensor<4xi32>, %arg2: tensor<4xi32>) -> tensor<1x?x1x2xf32> { +%cst = mhlo.constant dense<2> : tensor<4xi32> +%0 = mhlo.real_dynamic_slice %arg0, %arg1, %arg2, %cst : (tensor<1x?x2x4xf32>, tensor<4xi32>, tensor<4xi32>, tensor<4xi32>) -> tensor<1x?x1x2xf32> +func.return %0 : tensor<1x?x1x2xf32> +} + +// CHECK-LABEL: func @remove_shape_assertion_custom_call +// CHECK-NOT: "mhlo.custom_call" +func.func @remove_shape_assertion_custom_call(%arg1: tensor) -> tensor { + %0 = mhlo.constant dense<3> : tensor + %1 = "mhlo.get_dimension_size"(%arg1) <{dimension = 0 : i64}> : (tensor) -> tensor + %ok = mhlo.compare EQ, %1, %0, SIGNED : (tensor, tensor) -> tensor + mhlo.custom_call @shape_assertion(%ok) { + error_message = "The error message", + has_side_effect = true + } : (tensor) -> () + return %1 : tensor +} + +// CHECK-LABEL: func @convert_approx_top_k_custom_call( +// CHECK-SAME: %[[ARG_0:.*]]: tensor<1x4xf32>, +// CHECK-SAME: %[[ARG_1:.*]]: tensor<1x4xi32>, +// CHECK-SAME: %[[ARG_2:.*]]: tensor, +// CHECK-SAME: %[[ARG_3:.*]]: tensor) -> (tensor<1x4xf32>, tensor<1x4xi32>) { +// CHECK: %[[VALUES:.*]], %[[INDICES:.*]] = "tf.ApproxTopK"(%[[ARG_0]]) <{aggregate_to_topk = true, is_max_k = true, k = 4 : i64, recall_target = 8.500000e-01 : f32, reduction_dimension = 1 : i64, reduction_input_size_override = -1 : i64}> : (tensor<1x4xf32>) -> (tensor<1x4xf32>, tensor<1x4xi32>) +// CHECK: return %[[VALUES]], %[[INDICES]] : tensor<1x4xf32>, tensor<1x4xi32> +// CHECK: } +func.func @convert_approx_top_k_custom_call(%arg0: tensor<1x4xf32>, %arg1: tensor<1x4xi32>, %arg2: tensor, %arg3: tensor) -> (tensor<1x4xf32>, tensor<1x4xi32>) { + %0:2 = mhlo.custom_call @ApproxTopK(%arg0, %arg1, %arg2, %arg3) { + api_version = 4 : i32, + called_computations = [@top_k_gt_f32_comparator], + backend_config = { + aggregate_to_topk = true, + is_fallback = true, + recall_target = 8.500000e-01 : f32, + reduction_dim = 1 : i64, + reduction_input_size_override = -1 : i64, + top_k = 4 : i64} + } : (tensor<1x4xf32>, tensor<1x4xi32>, tensor, tensor) -> (tensor<1x4xf32>, tensor<1x4xi32>) + func.return %0#0, %0#1 : tensor<1x4xf32>, tensor<1x4xi32> +} + +func.func @top_k_gt_f32_comparator(%arg0: tensor, %arg1: tensor, %arg2: tensor, %arg3: tensor) -> tensor { + %0 = mhlo.compare GT, %arg0, %arg1 : (tensor, tensor) -> tensor + func.return %0 : tensor +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/odml-to-stablehlo-allow-tf.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/odml-to-stablehlo-allow-tf.mlir new file mode 100644 index 0000000000000000000000000000000000000000..c098d59eda1373fbe71c5394a80bbd1b301ad871 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/odml-to-stablehlo-allow-tf.mlir @@ -0,0 +1,10 @@ +// RUN: odml_to_stablehlo %s --allow-tf=false -o /tmp/temp.mlir; [ -f /tmp/temp.mlir ]; [ -f /tmp/debug_stablehlo.mlir ] +// RUN: odml_to_stablehlo %s --allow-tf=true -o /tmp/temp2.mlir; [ -f /tmp/temp2.mlir ] + +module attributes {tf.versions = {bad_consumers = [], min_consumer = 12 : i32, producer = 975 : i32}, tf_saved_model.semantics} { + func.func @serving_default(%arg0: tensor<1x20x20x28xf32> {tf_saved_model.index_path = ["a"]}) -> (tensor<1x40x40x28xf32> {tf_saved_model.index_path = ["b"]}) attributes {tf.entry_function = {control_outputs = "", inputs = "c:0", outputs = "d:0"}, tf_saved_model.exported_names = ["serving_default"]} { + %c = stablehlo.constant dense<40> : tensor<2xi32> + %0 = "tf.UnconvertedOp"(%arg0, %c) {align_corners = false, half_pixel_centers = false} : (tensor<1x20x20x28xf32>, tensor<2xi32>) -> tensor<1x40x40x28xf32> + func.return %0 : tensor<1x40x40x28xf32> + } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/odml-to-stablehlo-smuggle-resize.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/odml-to-stablehlo-smuggle-resize.mlir new file mode 100644 index 0000000000000000000000000000000000000000..5e2c40633f075fa8b9d473633c3b28c3e00fd27c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/odml-to-stablehlo-smuggle-resize.mlir @@ -0,0 +1,15 @@ +// RUN: odml_to_stablehlo %s -skip-resize -smuggle-disallowed-ops -o - | FileCheck %s +// RUN: odml-to-stablehlo-opt %s --smuggle-disallowed-ops-pass | FileCheck %s --check-prefix=CHECK-OPT + +// CHECK-LABEL: @main +module attributes {tf.versions = {bad_consumers = [], min_consumer = 12 : i32, producer = 975 : i32}, tf_saved_model.semantics} { + func.func @serving_default(%arg0: tensor<1x32x32x128xf32> {tf_saved_model.index_path = ["a"]}) -> (tensor<1x64x64x128xf32> {tf_saved_model.index_path = ["b"]}) attributes {tf.entry_function = {control_outputs = "", inputs = "c:0", outputs = "d:0"}, tf_saved_model.exported_names = ["serving_default"]} { + %0 = "tf.Const"() {value = dense<[56, 904]> : tensor<2xi32>} : () -> tensor<2xi32> + // CHECK: %{{.*}} = stablehlo.custom_call @tf.ResizeBilinear(%arg0, %{{.*}}) {align_corners = false, device = "", half_pixel_centers = true} : (tensor<1x32x32x128xf32>, tensor<2xi32>) -> tensor<1x64x64x128xf32> + // CHECK-OPT: %{{.*}} = stablehlo.custom_call @tf.ResizeBilinear(%arg0, %cst) {align_corners = false, device = "", half_pixel_centers = true} : (tensor<1x32x32x128xf32>, tensor<2xi32>) -> tensor<1x64x64x128xf32> + %1 = "tf.ResizeBilinear"(%arg0, %0) { + align_corners = false, device = "", half_pixel_centers = true + } : (tensor<1x32x32x128xf32>, tensor<2xi32>) -> tensor<1x64x64x128xf32> + func.return %1 : tensor<1x64x64x128xf32> + } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/optimize.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/optimize.mlir new file mode 100644 index 0000000000000000000000000000000000000000..dcf36b796700dce59fced3bc7703ec6da4bf48e7 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/optimize.mlir @@ -0,0 +1,514 @@ +// RUN: odml-to-stablehlo-opt %s -split-input-file -mhlo-optimize | FileCheck %s + +// CHECK-LABEL: testDotToDotGeneralVectorVector +func.func @testDotToDotGeneralVectorVector(%arg0: tensor<3072xf32>, %arg1: tensor<3072xf32>) -> tensor { + %0 = "mhlo.dot"(%arg0, %arg1) : (tensor<3072xf32>, tensor<3072xf32>) -> tensor + func.return %0 : tensor + +// CHECK: %[[RES:.*]] = "mhlo.dot_general"(%arg0, %arg1) <{ +// CHECK-SAME: dot_dimension_numbers = #mhlo.dot< +// CHECK-SAME: lhs_contracting_dimensions = [0], +// CHECK-SAME: rhs_contracting_dimensions = [0] +// CHECK-SAME: >}> : (tensor<3072xf32>, tensor<3072xf32>) -> tensor +// CHECK: return %[[RES]] : tensor +} + +// ----- + +// CHECK-LABEL: testDotToDotGeneralVectorMatrix +func.func @testDotToDotGeneralVectorMatrix(%arg0: tensor<3072xf32>, %arg1: tensor<3072x512xf32>) -> tensor<512xf32> { + %0 = "mhlo.dot"(%arg0, %arg1) : (tensor<3072xf32>, tensor<3072x512xf32>) -> tensor<512xf32> + func.return %0 : tensor<512xf32> + +// CHECK: %[[RES:.*]] = "mhlo.dot_general"(%arg0, %arg1) <{ +// CHECK-SAME: dot_dimension_numbers = #mhlo.dot< +// CHECK-SAME: lhs_contracting_dimensions = [0], +// CHECK-SAME: rhs_contracting_dimensions = [0] +// CHECK-SAME: >}> : (tensor<3072xf32>, tensor<3072x512xf32>) -> tensor<512xf32> +// CHECK: return %[[RES]] : tensor<512xf32> +} + +// ----- + +// CHECK-LABEL: testDotToDotGeneralMatrixVector +func.func @testDotToDotGeneralMatrixVector(%arg0: tensor<2x3072xf32>, %arg1: tensor<3072xf32>) -> tensor<2xf32> { + %0 = "mhlo.dot"(%arg0, %arg1) : (tensor<2x3072xf32>, tensor<3072xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> + +// CHECK: %[[RES:.*]] = "mhlo.dot_general"(%arg0, %arg1) <{ +// CHECK-SAME: dot_dimension_numbers = #mhlo.dot< +// CHECK-SAME: lhs_contracting_dimensions = [1], +// CHECK-SAME: rhs_contracting_dimensions = [0] +// CHECK-SAME: >}> : (tensor<2x3072xf32>, tensor<3072xf32>) -> tensor<2xf32> +// CHECK: return %[[RES]] : tensor<2xf32> +} + +// ----- + +// CHECK-LABEL: testDotToDotGeneralMatrixMatrix +func.func @testDotToDotGeneralMatrixMatrix(%arg0: tensor<2x3072xf32>, %arg1: tensor<3072x512xf32>) -> tensor<2x512xf32> { + %0 = "mhlo.dot"(%arg0, %arg1) : (tensor<2x3072xf32>, tensor<3072x512xf32>) -> tensor<2x512xf32> + func.return %0 : tensor<2x512xf32> + +// CHECK: %[[RES:.*]] = "mhlo.dot_general"(%arg0, %arg1) <{ +// CHECK-SAME: dot_dimension_numbers = #mhlo.dot< +// CHECK-SAME: lhs_contracting_dimensions = [1], +// CHECK-SAME: rhs_contracting_dimensions = [0] +// CHECK-SAME: >}> : (tensor<2x3072xf32>, tensor<3072x512xf32>) -> tensor<2x512xf32> +// CHECK: return %[[RES]] : tensor<2x512xf32> +} + +// ----- + +// CHECK-LABEL: testRemoveReshapeAroundDotGeneral +func.func @testRemoveReshapeAroundDotGeneral(%arg0: tensor<3x72x1x2048xf32>, %arg1: tensor<3x2048x512xf32>) -> tensor<3x72x1x512xf32> { + %0 = "mhlo.reshape"(%arg0) : (tensor<3x72x1x2048xf32>) -> tensor<3x72x2048xf32> + %1 = "mhlo.dot_general"(%0, %arg1) { + dot_dimension_numbers = #mhlo.dot< + lhs_batching_dimensions = [0], + rhs_batching_dimensions = [0], + lhs_contracting_dimensions = [2], + rhs_contracting_dimensions = [1] + >} : (tensor<3x72x2048xf32>, tensor<3x2048x512xf32>) -> tensor<3x72x512xf32> + %2 = "mhlo.reshape"(%1) : (tensor<3x72x512xf32>) -> tensor<3x72x1x512xf32> + func.return %2 : tensor<3x72x1x512xf32> + +// CHECK: %[[RES:.*]] = "mhlo.dot_general"(%arg0, %arg1) <{ +// CHECK-SAME: dot_dimension_numbers = #mhlo.dot< +// CHECK-SAME: lhs_batching_dimensions = [0], +// CHECK-SAME: rhs_batching_dimensions = [0], +// CHECK-SAME: lhs_contracting_dimensions = [3], +// CHECK-SAME: rhs_contracting_dimensions = [1] +// CHECK-SAME: >}> : (tensor<3x72x1x2048xf32>, tensor<3x2048x512xf32>) -> tensor<3x72x1x512xf32> +// CHECK: return %[[RES]] : tensor<3x72x1x512xf32> +} + +// ----- + +// CHECK-LABEL: testRemoveReshapeAroundDot +func.func @testRemoveReshapeAroundDot(%arg0: tensor<1x1x512xf32>, %arg1: tensor<512x13x!quant.uniform>) -> tensor<1x1x13xf32> { + %0 = "mhlo.reshape"(%arg0) : (tensor<1x1x512xf32>) -> tensor<1x512xf32> + %1 = "mhlo.dot"(%0, %arg1) : (tensor<1x512xf32>, tensor<512x13x!quant.uniform>) -> tensor<1x13xf32> + %2 = "mhlo.reshape"(%1) : (tensor<1x13xf32>) -> tensor<1x1x13xf32> + func.return %2 : tensor<1x1x13xf32> + +// CHECK: %[[RES:.*]] = "mhlo.dot_general"(%arg0, %arg1) <{ +// CHECK-SAME: dot_dimension_numbers = #mhlo.dot< +// CHECK-SAME: lhs_contracting_dimensions = [2], +// CHECK-SAME: rhs_contracting_dimensions = [0] +// CHECK-SAME: >}> : (tensor<1x1x512xf32>, tensor<512x13x!quant.uniform>) -> tensor<1x1x13xf32> +// CHECK: return %[[RES]] : tensor<1x1x13xf32> +} + +// ----- + +// CHECK-LABEL: testTwoConsecutivePads +func.func @testTwoConsecutivePads(%arg0: tensor<10x10x10xf32>) -> (tensor<12x12x12xf32>) { + %0 = mhlo.constant dense<0.000000e+00> : tensor + %1 = "mhlo.pad"(%arg0, %0) <{edge_padding_high = dense<0> : tensor<3xi64>, edge_padding_low = dense<1> : tensor<3xi64>, interior_padding = dense<0> : tensor<3xi64>}> : (tensor<10x10x10xf32>, tensor) -> tensor<11x11x11xf32> + %2 = mhlo.constant dense<0.000000e+00> : tensor + %3 = "mhlo.pad"(%1, %2) <{edge_padding_high = dense<1> : tensor<3xi64>, edge_padding_low = dense<0> : tensor<3xi64>, interior_padding = dense<0> : tensor<3xi64>}> : (tensor<11x11x11xf32>, tensor) -> tensor<12x12x12xf32> + return %3 : tensor<12x12x12xf32> +// CHECK: %[[RES:.*]] = "mhlo.pad"(%arg0, %0) <{ +// CHECK-SAME: edge_padding_high = dense<1> : tensor<3xi64>, +// CHECK-SAME: edge_padding_low = dense<1> : tensor<3xi64>, +// CHECK-SAME: interior_padding = dense<0> : tensor<3xi64> +// CHECK-SAME: }> : (tensor<10x10x10xf32>, tensor) -> tensor<12x12x12xf32> +// CHECK: return %[[RES]] : tensor<12x12x12xf32> +} + +// ----- + +// CHECK-LABEL: testTwoConsecutivePadsNegativeLowPad +func.func @testTwoConsecutivePadsNegativeLowPad(%arg0: tensor<10x10x10xf32>) -> (tensor<10x10x10xf32>) { + %0 = mhlo.constant dense<0.000000e+00> : tensor + %1 = "mhlo.pad"(%arg0, %0) <{edge_padding_high = dense<0> : tensor<3xi64>, edge_padding_low = dense<-1> : tensor<3xi64>, interior_padding = dense<0> : tensor<3xi64>}> : (tensor<10x10x10xf32>, tensor) -> tensor<9x9x9xf32> + %2 = mhlo.constant dense<0.000000e+00> : tensor + %3 = "mhlo.pad"(%1, %2) <{edge_padding_high = dense<1> : tensor<3xi64>, edge_padding_low = dense<0> : tensor<3xi64>, interior_padding = dense<0> : tensor<3xi64>}> : (tensor<9x9x9xf32>, tensor) -> tensor<10x10x10xf32> + return %3 : tensor<10x10x10xf32> + +// CHECK: %[[RES:.*]] = "mhlo.pad"(%arg0, %0) <{ +// CHECK-SAME: edge_padding_high = dense<1> : tensor<3xi64>, +// CHECK-SAME: edge_padding_low = dense<-1> : tensor<3xi64>, +// CHECK-SAME: interior_padding = dense<0> : tensor<3xi64> +// CHECK-SAME: }> : (tensor<10x10x10xf32>, tensor) -> tensor<10x10x10xf32> +// CHECK: return %[[RES]] : tensor<10x10x10xf32> +} + +// ----- + +// CHECK-LABEL: testTwoConsecutivePadsTwoNegativeHighPad +func.func @testTwoConsecutivePadsTwoNegativeHighPad(%arg0: tensor<10x10x10xf32>) -> (tensor<9x9x9xf32>) { + %0 = mhlo.constant dense<0.000000e+00> : tensor + %1 = "mhlo.pad"(%arg0, %0) <{edge_padding_high = dense<-1> : tensor<3xi64>, edge_padding_low = dense<1> : tensor<3xi64>, interior_padding = dense<0> : tensor<3xi64>}> : (tensor<10x10x10xf32>, tensor) -> tensor<10x10x10xf32> + %2 = mhlo.constant dense<0.000000e+00> : tensor + %3 = "mhlo.pad"(%1, %2) <{edge_padding_high = dense<-1> : tensor<3xi64>, edge_padding_low = dense<0> : tensor<3xi64>, interior_padding = dense<0> : tensor<3xi64>}> : (tensor<10x10x10xf32>, tensor) -> tensor<9x9x9xf32> + return %3 : tensor<9x9x9xf32> + +// CHECK: %[[RES:.*]] = "mhlo.pad"(%arg0, %0) <{ +// CHECK-SAME: edge_padding_high = dense<-2> : tensor<3xi64>, +// CHECK-SAME: edge_padding_low = dense<1> : tensor<3xi64>, +// CHECK-SAME: interior_padding = dense<0> : tensor<3xi64> +// CHECK-SAME: }> : (tensor<10x10x10xf32>, tensor) -> tensor<9x9x9xf32> +// CHECK: return %[[RES]] : tensor<9x9x9xf32> +} + +// ----- + +// CHECK-LABEL: testTwoConsecutivePadsPositiveNegativeHighPad +func.func @testTwoConsecutivePadsPositiveNegativeHighPad(%arg0: tensor<10x10x10xf32>) -> (tensor<11x11x11xf32>) { + %0 = mhlo.constant dense<0.000000e+00> : tensor + %1 = "mhlo.pad"(%arg0, %0) <{edge_padding_high = dense<1> : tensor<3xi64>, edge_padding_low = dense<1> : tensor<3xi64>, interior_padding = dense<0> : tensor<3xi64>}> : (tensor<10x10x10xf32>, tensor) -> tensor<12x12x12xf32> + %2 = mhlo.constant dense<0.000000e+00> : tensor + %3 = "mhlo.pad"(%1, %2) <{edge_padding_high = dense<-1> : tensor<3xi64>, edge_padding_low = dense<0> : tensor<3xi64>, interior_padding = dense<0> : tensor<3xi64>}> : (tensor<12x12x12xf32>, tensor) -> tensor<11x11x11xf32> + return %3 : tensor<11x11x11xf32> + +// CHECK: %[[RES:.*]] = "mhlo.pad"(%arg0, %0) <{ +// CHECK-SAME: edge_padding_high = dense<0> : tensor<3xi64>, +// CHECK-SAME: edge_padding_low = dense<1> : tensor<3xi64>, +// CHECK-SAME: interior_padding = dense<0> : tensor<3xi64> +// CHECK-SAME: }> : (tensor<10x10x10xf32>, tensor) -> tensor<11x11x11xf32> +// CHECK: return %[[RES]] : tensor<11x11x11xf32> +} + +// ----- + +// CHECK-LABEL: testTwoConsecutivePadsNegativePositiveHighPad +func.func @testTwoConsecutivePadsNegativePositiveHighPad(%arg0: tensor<10x10x10xf32>) -> (tensor<11x11x11xf32>) { + %0 = mhlo.constant dense<0.000000e+00> : tensor + %1 = "mhlo.pad"(%arg0, %0) <{edge_padding_high = dense<-1> : tensor<3xi64>, edge_padding_low = dense<1> : tensor<3xi64>, interior_padding = dense<0> : tensor<3xi64>}> : (tensor<10x10x10xf32>, tensor) -> tensor<10x10x10xf32> + %2 = mhlo.constant dense<0.000000e+00> : tensor + %3 = "mhlo.pad"(%1, %2) <{edge_padding_high = dense<1> : tensor<3xi64>, edge_padding_low = dense<0> : tensor<3xi64>, interior_padding = dense<0> : tensor<3xi64>}> : (tensor<10x10x10xf32>, tensor) -> tensor<11x11x11xf32> + return %3 : tensor<11x11x11xf32> + +// CHECK: "mhlo.pad"(%arg0, %0) <{ +// CHECK-SAME: edge_padding_high = dense<-1> : tensor<3xi64>, +// CHECK-SAME: edge_padding_low = dense<1> : tensor<3xi64>, +// CHECK-SAME: interior_padding = dense<0> : tensor<3xi64> +// CHECK-SAME: }> : (tensor<10x10x10xf32>, tensor) -> tensor<10x10x10xf32> + +// CHECK: "mhlo.pad"(%1, %0) <{ +// CHECK-SAME: edge_padding_high = dense<1> : tensor<3xi64>, +// CHECK-SAME: edge_padding_low = dense<0> : tensor<3xi64>, +// CHECK-SAME: interior_padding = dense<0> : tensor<3xi64> +// CHECK-SAME: }> : (tensor<10x10x10xf32>, tensor) -> tensor<11x11x11xf32> +} + +// ----- + +// CHECK-LABEL: testTwoConsecutivePadsDifferentPadVal +func.func @testTwoConsecutivePadsDifferentPadVal(%arg0: tensor<10x10x10xf32>) -> (tensor<14x14x14xf32>) { + %0 = mhlo.constant dense<1.000000e+00> : tensor + %1 = "mhlo.pad"(%arg0, %0) <{edge_padding_high = dense<1> : tensor<3xi64>, edge_padding_low = dense<1> : tensor<3xi64>, interior_padding = dense<0> : tensor<3xi64>}> : (tensor<10x10x10xf32>, tensor) -> tensor<12x12x12xf32> + %2 = mhlo.constant dense<0.000000e+00> : tensor + %3 = "mhlo.pad"(%1, %2) <{edge_padding_high = dense<1> : tensor<3xi64>, edge_padding_low = dense<1> : tensor<3xi64>, interior_padding = dense<0> : tensor<3xi64>}> : (tensor<12x12x12xf32>, tensor) -> tensor<14x14x14xf32> + return %3 : tensor<14x14x14xf32> + +// CHECK: "mhlo.pad"(%arg0, %1) <{ +// CHECK-SAME: edge_padding_high = dense<1> : tensor<3xi64>, +// CHECK-SAME: edge_padding_low = dense<1> : tensor<3xi64>, +// CHECK-SAME: interior_padding = dense<0> : tensor<3xi64> +// CHECK-SAME: }> : (tensor<10x10x10xf32>, tensor) -> tensor<12x12x12xf32> + +// CHECK: "mhlo.pad"(%2, %0) <{ +// CHECK-SAME: edge_padding_high = dense<1> : tensor<3xi64>, +// CHECK-SAME: edge_padding_low = dense<1> : tensor<3xi64>, +// CHECK-SAME: interior_padding = dense<0> : tensor<3xi64> +// CHECK-SAME: }> : (tensor<12x12x12xf32>, tensor) -> tensor<14x14x14xf32> +} + +// ----- + +// CHECK-LABEL: testTwoConsecutivePadsDifferentUsers +func.func @testTwoConsecutivePadsDifferentUsers(%arg0: tensor<10x10x10xf32>) -> (tensor<13x13x13xf32>, tensor<12x12x12xf32>) { + %0 = mhlo.constant dense<0.000000e+00> : tensor + %1 = "mhlo.pad"(%arg0, %0) <{edge_padding_high = dense<1> : tensor<3xi64>, edge_padding_low = dense<1> : tensor<3xi64>, interior_padding = dense<0> : tensor<3xi64>}> : (tensor<10x10x10xf32>, tensor) -> tensor<12x12x12xf32> + %2 = mhlo.exponential %1 : tensor<12x12x12xf32> + %3 = mhlo.constant dense<0.000000e+00> : tensor + %4 = "mhlo.pad"(%1, %3) <{edge_padding_high = dense<1> : tensor<3xi64>, edge_padding_low = dense<0> : tensor<3xi64>, interior_padding = dense<0> : tensor<3xi64>}> : (tensor<12x12x12xf32>, tensor) -> tensor<13x13x13xf32> + return %4, %2 : tensor<13x13x13xf32>, tensor<12x12x12xf32> + +// CHECK: "mhlo.pad"(%arg0, %0) <{ +// CHECK-SAME: edge_padding_high = dense<1> : tensor<3xi64>, +// CHECK-SAME: edge_padding_low = dense<1> : tensor<3xi64>, +// CHECK-SAME: interior_padding = dense<0> : tensor<3xi64> +// CHECK-SAME: }> : (tensor<10x10x10xf32>, tensor) -> tensor<12x12x12xf32> + +// CHECK: "mhlo.pad"(%1, %0) <{ +// CHECK-SAME: edge_padding_high = dense<1> : tensor<3xi64>, +// CHECK-SAME: edge_padding_low = dense<0> : tensor<3xi64>, +// CHECK-SAME: interior_padding = dense<0> : tensor<3xi64> +// CHECK-SAME: }> : (tensor<12x12x12xf32>, tensor) -> tensor<13x13x13xf32> +} + +// ----- + +// CHECK-LABEL: testTwoConsecutivePadsMultipleDownstreamUsers + func.func @testTwoConsecutivePadsMultipleDownstreamUsers(%arg0: tensor<10x10x10xf32>) -> (tensor<13x13x13xf32>, tensor<13x13x13xf32>) { + %0 = mhlo.constant dense<0.000000e+00> : tensor + %1 = "mhlo.pad"(%arg0, %0) <{edge_padding_high = dense<1> : tensor<3xi64>, edge_padding_low = dense<1> : tensor<3xi64>, interior_padding = dense<0> : tensor<3xi64>}> : (tensor<10x10x10xf32>, tensor) -> tensor<12x12x12xf32> + %2 = mhlo.constant dense<0.000000e+00> : tensor + %3 = "mhlo.pad"(%1, %2) <{edge_padding_high = dense<1> : tensor<3xi64>, edge_padding_low = dense<0> : tensor<3xi64>, interior_padding = dense<0> : tensor<3xi64>}> : (tensor<12x12x12xf32>, tensor) -> tensor<13x13x13xf32> + %4 = mhlo.exponential %3 : tensor<13x13x13xf32> + %5 = mhlo.tanh %3 : tensor<13x13x13xf32> + return %4, %5 : tensor<13x13x13xf32>, tensor<13x13x13xf32> + +// CHECK: "mhlo.pad"(%arg0, %0) <{ +// CHECK-SAME: edge_padding_high = dense<2> : tensor<3xi64>, +// CHECK-SAME: edge_padding_low = dense<1> : tensor<3xi64>, +// CHECK-SAME: interior_padding = dense<0> : tensor<3xi64> +// CHECK-SAME: }> : (tensor<10x10x10xf32>, tensor) -> tensor<13x13x13xf32> + +// CHECK: mhlo.exponential %1 : tensor<13x13x13xf32> +// CHECK: mhlo.tanh %1 : tensor<13x13x13xf32> +// CHECK: return %2, %3 : tensor<13x13x13xf32>, tensor<13x13x13xf32> +} + +// ----- + +// CHECK-LABEL: testLiftDotConcatLHSSimple +func.func @testLiftDotConcatLHSSimple(%arg0: tensor<1x1x512xf32>, %arg1: tensor<2x1x512xf32>, %arg2: tensor<3x1x512xf32>, %arg3: tensor<512x13xf32>) -> tensor<6x1x13xf32> { + %0 = "mhlo.dot_general"(%arg0, %arg3) { + dot_dimension_numbers = #mhlo.dot< + lhs_contracting_dimensions = [2], + rhs_contracting_dimensions = [0] + >} : (tensor<1x1x512xf32>, tensor<512x13xf32>) -> tensor<1x1x13xf32> + %1 = "mhlo.dot_general"(%arg1, %arg3) { + dot_dimension_numbers = #mhlo.dot< + lhs_contracting_dimensions = [2], + rhs_contracting_dimensions = [0] + >} : (tensor<2x1x512xf32>, tensor<512x13xf32>) -> tensor<2x1x13xf32> + %2 = "mhlo.dot_general"(%arg2, %arg3) { + dot_dimension_numbers = #mhlo.dot< + lhs_contracting_dimensions = [2], + rhs_contracting_dimensions = [0] + >} : (tensor<3x1x512xf32>, tensor<512x13xf32>) -> tensor<3x1x13xf32> + %r = "mhlo.concatenate"(%0, %1, %2) <{dimension = 0 : i64}> : (tensor<1x1x13xf32>, tensor<2x1x13xf32>, tensor<3x1x13xf32>) -> tensor<6x1x13xf32> + func.return %r : tensor<6x1x13xf32> + +// CHECK: %[[R0:.*]] = "mhlo.concatenate"(%arg0, %arg1, %arg2) <{dimension = 0 : i64}> : (tensor<1x1x512xf32>, tensor<2x1x512xf32>, tensor<3x1x512xf32>) -> tensor<6x1x512xf32> +// CHECK: %[[R1:.*]] = "mhlo.dot_general"(%[[R0]], %arg3) <{ +// CHECK-SAME: dot_dimension_numbers = #mhlo.dot< +// CHECK-SAME: lhs_contracting_dimensions = [2], +// CHECK-SAME: rhs_contracting_dimensions = [0] +// CHECK-SAME: >}> : (tensor<6x1x512xf32>, tensor<512x13xf32>) -> tensor<6x1x13xf32> +// CHECK: return %[[R1]] : tensor<6x1x13xf32> +} + +// ----- + +// CHECK-LABEL: testLiftDotConcatLHSComplex +func.func @testLiftDotConcatLHSComplex(%arg0: tensor<1x9x2x3x8x4x10xf32>, %arg1: tensor<1x9x2x3x8x100x10xf32>, %arg2: tensor<9x2x1x5x10x5x8x7xf32>) -> tensor<1x2x3x104x5x5x7xf32> { + %0 = "mhlo.dot_general"(%arg0, %arg2) { + dot_dimension_numbers = #mhlo.dot< + lhs_batching_dimensions = [0, 2], + rhs_batching_dimensions = [2, 1], + lhs_contracting_dimensions = [4, 1, 6], + rhs_contracting_dimensions = [6, 0, 4] + >} : (tensor<1x9x2x3x8x4x10xf32>, tensor<9x2x1x5x10x5x8x7xf32>) -> tensor<1x2x3x4x5x5x7xf32> + %1 = "mhlo.dot_general"(%arg1, %arg2) { + dot_dimension_numbers = #mhlo.dot< + lhs_batching_dimensions = [0, 2], + rhs_batching_dimensions = [2, 1], + lhs_contracting_dimensions = [4, 1, 6], + rhs_contracting_dimensions = [6, 0, 4] + >} : (tensor<1x9x2x3x8x100x10xf32>, tensor<9x2x1x5x10x5x8x7xf32>) -> tensor<1x2x3x100x5x5x7xf32> + %r = "mhlo.concatenate"(%0, %1) <{dimension = 3 : i64}> : (tensor<1x2x3x4x5x5x7xf32>, tensor<1x2x3x100x5x5x7xf32>) -> tensor<1x2x3x104x5x5x7xf32> + func.return %r : tensor<1x2x3x104x5x5x7xf32> + +// CHECK: %[[R0:.*]] = "mhlo.concatenate"(%arg0, %arg1) <{dimension = 5 : i64}> : (tensor<1x9x2x3x8x4x10xf32>, tensor<1x9x2x3x8x100x10xf32>) -> tensor<1x9x2x3x8x104x10xf32> +// CHECK: %[[R1:.*]] = "mhlo.dot_general"(%[[R0]], %arg2) <{ +// CHECK-SAME: dot_dimension_numbers = #mhlo.dot< +// CHECK-SAME: lhs_batching_dimensions = [0, 2], +// CHECK-SAME: rhs_batching_dimensions = [2, 1], +// CHECK-SAME: lhs_contracting_dimensions = [4, 1, 6], +// CHECK-SAME: rhs_contracting_dimensions = [6, 0, 4] +// CHECK-SAME: >}> : (tensor<1x9x2x3x8x104x10xf32>, tensor<9x2x1x5x10x5x8x7xf32>) -> tensor<1x2x3x104x5x5x7xf32> +// CHECK: return %[[R1]] : tensor<1x2x3x104x5x5x7xf32> +} + +// ----- + +// CHECK-LABEL: testLiftDotConcatLHSAndRHS +func.func @testLiftDotConcatLHSAndRHS(%arg0: tensor<1x72x128xf32>, %arg1: tensor<1x128x72xf32>, %arg2: tensor<1x72x128xf32>, %arg3: tensor<1x128x72xf32>, %arg4: tensor<1x72x128xf32>, %arg5: tensor<1x128x72xf32>, %arg6: tensor<1x72x128xf32>, %arg7: tensor<1x128x72xf32>) -> tensor<4x72x72xf32> { + %0 = "mhlo.dot_general"(%arg0, %arg1) { + dot_dimension_numbers = #mhlo.dot< + lhs_batching_dimensions = [0], + rhs_batching_dimensions = [0], + lhs_contracting_dimensions = [2], + rhs_contracting_dimensions = [1] + >} : (tensor<1x72x128xf32>, tensor<1x128x72xf32>) -> tensor<1x72x72xf32> + %1 = "mhlo.dot_general"(%arg2, %arg3) { + dot_dimension_numbers = #mhlo.dot< + lhs_batching_dimensions = [0], + rhs_batching_dimensions = [0], + lhs_contracting_dimensions = [2], + rhs_contracting_dimensions = [1] + >} : (tensor<1x72x128xf32>, tensor<1x128x72xf32>) -> tensor<1x72x72xf32> + %2 = "mhlo.dot_general"(%arg4, %arg5) { + dot_dimension_numbers = #mhlo.dot< + lhs_batching_dimensions = [0], + rhs_batching_dimensions = [0], + lhs_contracting_dimensions = [2], + rhs_contracting_dimensions = [1] + >} : (tensor<1x72x128xf32>, tensor<1x128x72xf32>) -> tensor<1x72x72xf32> + %3 = "mhlo.dot_general"(%arg6, %arg7) { + dot_dimension_numbers = #mhlo.dot< + lhs_batching_dimensions = [0], + rhs_batching_dimensions = [0], + lhs_contracting_dimensions = [2], + rhs_contracting_dimensions = [1] + >} : (tensor<1x72x128xf32>, tensor<1x128x72xf32>) -> tensor<1x72x72xf32> + %4 = "mhlo.concatenate"(%0, %1, %2, %3) <{dimension = 0 : i64}> : (tensor<1x72x72xf32>, tensor<1x72x72xf32>, tensor<1x72x72xf32>, tensor<1x72x72xf32>) -> tensor<4x72x72xf32> + func.return %4 : tensor<4x72x72xf32> + +// CHECK: %[[R0:.*]] = "mhlo.concatenate"(%arg0, %arg2, %arg4, %arg6) <{dimension = 0 : i64}> : (tensor<1x72x128xf32>, tensor<1x72x128xf32>, tensor<1x72x128xf32>, tensor<1x72x128xf32>) -> tensor<4x72x128xf32> +// CHECK: %[[R1:.*]] = "mhlo.concatenate"(%arg1, %arg3, %arg5, %arg7) <{dimension = 0 : i64}> : (tensor<1x128x72xf32>, tensor<1x128x72xf32>, tensor<1x128x72xf32>, tensor<1x128x72xf32>) -> tensor<4x128x72xf32> +// CHECK: %[[R2:.*]] = "mhlo.dot_general"(%[[R0]], %[[R1]]) <{ +// CHECK-SAME: dot_dimension_numbers = #mhlo.dot< +// CHECK-SAME: lhs_batching_dimensions = [0], +// CHECK-SAME: rhs_batching_dimensions = [0], +// CHECK-SAME: lhs_contracting_dimensions = [2], +// CHECK-SAME: rhs_contracting_dimensions = [1] +// CHECK-SAME: >}> : (tensor<4x72x128xf32>, tensor<4x128x72xf32>) -> tensor<4x72x72xf32> +// CHECK: return %[[R2]] : tensor<4x72x72xf32> +} + +// ----- + +// CHECK-LABEL: testSliceConcat +func.func @testSliceConcat(%arg0: tensor<3x1x512xf32>) -> tensor<3x1x512xf32> { + %0 = "mhlo.slice"(%arg0) <{limit_indices = dense<[1, 1, 512]> : tensor<3xi64>, start_indices = dense<[0, 0, 0]> : tensor<3xi64>, strides = dense<1> : tensor<3xi64>}> : (tensor<3x1x512xf32>) -> tensor<1x1x512xf32> + %1 = "mhlo.slice"(%arg0) <{limit_indices = dense<[2, 1, 512]> : tensor<3xi64>, start_indices = dense<[1, 0, 0]> : tensor<3xi64>, strides = dense<1> : tensor<3xi64>}> : (tensor<3x1x512xf32>) -> tensor<1x1x512xf32> + %2 = "mhlo.slice"(%arg0) <{limit_indices = dense<[3, 1, 512]> : tensor<3xi64>, start_indices = dense<[2, 0, 0]> : tensor<3xi64>, strides = dense<1> : tensor<3xi64>}> : (tensor<3x1x512xf32>) -> tensor<1x1x512xf32> + %r = "mhlo.concatenate"(%0, %1, %2) <{dimension = 0 : i64}> : (tensor<1x1x512xf32>, tensor<1x1x512xf32>, tensor<1x1x512xf32>) -> tensor<3x1x512xf32> + func.return %r : tensor<3x1x512xf32> + +// CHECK: return %arg0 : tensor<3x1x512xf32> +} + +// ----- + +// CHECK-LABEL: testConvertReshapeDotRhsToBatchedDot +func.func @testConvertReshapeDotRhsToBatchedDot(%arg0: tensor<1x72x72xf32>, %arg1: tensor<1x72x128xf32>) -> tensor<1x72x128xf32> { + %0 = mhlo.reshape %arg1 : (tensor<1x72x128xf32>) -> tensor<72x128xf32> + %1 = "mhlo.dot_general"(%arg0, %0) { + dot_dimension_numbers = #mhlo.dot< + lhs_contracting_dimensions = [2], + rhs_contracting_dimensions = [0] + >} : (tensor<1x72x72xf32>, tensor<72x128xf32>) -> tensor<1x72x128xf32> + func.return %1 : tensor<1x72x128xf32> + +// CHECK: %[[R:.*]] = "mhlo.dot_general"(%arg0, %arg1) <{ +// CHECK-SAME: dot_dimension_numbers = #mhlo.dot< +// CHECK-SAME: lhs_batching_dimensions = [0], +// CHECK-SAME: rhs_batching_dimensions = [0], +// CHECK-SAME: lhs_contracting_dimensions = [2], +// CHECK-SAME: rhs_contracting_dimensions = [1] +// CHECK-SAME: >}> : (tensor<1x72x72xf32>, tensor<1x72x128xf32>) -> tensor<1x72x128xf32> +// CHECK: return %[[R]] : tensor<1x72x128xf32> +} + +// ----- + +// CHECK-LABEL: broadcast_reshape_one_non_unit_dimnsion +func.func @broadcast_reshape_one_non_unit_dimnsion(%arg0: tensor<1x1x1x63xf32>) -> tensor<32x1x63xf32> { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 1, 2, 3]> : tensor<4xi64>}> : (tensor<1x1x1x63xf32>) -> tensor<1x32x1x63xf32> + %1 = mhlo.reshape %0 : (tensor<1x32x1x63xf32>) -> tensor<32x1x63xf32> + return %1 : tensor<32x1x63xf32> +} + +// CHECK: %0 = mhlo.reshape %arg0 : (tensor<1x1x1x63xf32>) -> tensor<63xf32> +// CHECK: %1 = "mhlo.broadcast_in_dim"(%0) <{broadcast_dimensions = dense<2> : tensor<1xi64>}> : (tensor<63xf32>) -> tensor<32x1x63xf32> +// CHECK: return %1 : tensor<32x1x63xf32> + +// ----- + +// CHECK-LABEL: broadcast_reshape_one_non_unit_dimnsion_trailing_zeros +func.func @broadcast_reshape_one_non_unit_dimnsion_trailing_zeros(%arg0: tensor<63x1x1x1xf32>) -> tensor<63x1x2xf32> { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 1, 2, 3]> : tensor<4xi64>}> : (tensor<63x1x1x1xf32>) -> tensor<63x1x1x2xf32> + %1 = mhlo.reshape %0 : (tensor<63x1x1x2xf32>) -> tensor<63x1x2xf32> + return %1 : tensor<63x1x2xf32> +} + +// CHECK: %0 = mhlo.reshape %arg0 : (tensor<63x1x1x1xf32>) -> tensor<63xf32> +// CHECK: %1 = "mhlo.broadcast_in_dim"(%0) <{broadcast_dimensions = dense<0> : tensor<1xi64>}> : (tensor<63xf32>) -> tensor<63x1x2xf32> +// CHECK: return %1 : tensor<63x1x2xf32> + +// ----- + +// CHECK-LABEL: broadcast_reshape_multiple_non_unit_dimension +func.func @broadcast_reshape_multiple_non_unit_dimension(%arg0: tensor<1x2x1x63xf32>) -> tensor<2x3x63xf32> { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 1, 2, 3]> : tensor<4xi64>}> : (tensor<1x2x1x63xf32>) -> tensor<1x2x3x63xf32> + %1 = mhlo.reshape %0 : (tensor<1x2x3x63xf32>) -> tensor<2x3x63xf32> + return %1 : tensor<2x3x63xf32> +} + +// CHECK: %0 = mhlo.reshape %arg0 : (tensor<1x2x1x63xf32>) -> tensor<2x63xf32> +// CHECK: %1 = "mhlo.broadcast_in_dim"(%0) <{broadcast_dimensions = dense<[0, 2]> : tensor<2xi64>}> : (tensor<2x63xf32>) -> tensor<2x3x63xf32> +// CHECK: return %1 : tensor<2x3x63xf32> + +// ----- + +// CHECK-LABEL: broadcast_reshape_multiple_non_unit_dimension_unsorted_broadcast_dims +func.func @broadcast_reshape_multiple_non_unit_dimension_unsorted_broadcast_dims(%arg0: tensor<1x2x1x63xf32>) -> tensor<3x2x63xf32> { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 2, 1, 3]> : tensor<4xi64>}> : (tensor<1x2x1x63xf32>) -> tensor<3x1x2x63xf32> + %1 = mhlo.reshape %0 : (tensor<3x1x2x63xf32>) -> tensor<3x2x63xf32> + return %1 : tensor<3x2x63xf32> +} + +// CHECK: %0 = mhlo.reshape %arg0 : (tensor<1x2x1x63xf32>) -> tensor<2x63xf32> +// CHECK: %1 = "mhlo.broadcast_in_dim"(%0) <{broadcast_dimensions = dense<[1, 2]> : tensor<2xi64>}> : (tensor<2x63xf32>) -> tensor<3x2x63xf32> +// CHECK: return %1 : tensor<3x2x63xf32> + +// ----- + +// CHECK-LABEL: broadcast_reshape_broadcast_increases_rank +func.func @broadcast_reshape_broadcast_increases_rank(%arg0: tensor<1x2x1x63xf32>) -> tensor<2x3x63xf32> { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 1, 2, 4]> : tensor<4xi64>}> : (tensor<1x2x1x63xf32>) -> tensor<1x2x3x1x63xf32> + %1 = mhlo.reshape %0 : (tensor<1x2x3x1x63xf32>) -> tensor<2x3x63xf32> + return %1 : tensor<2x3x63xf32> +} + +// CHECK: %0 = mhlo.reshape %arg0 : (tensor<1x2x1x63xf32>) -> tensor<2x63xf32> +// CHECK: %1 = "mhlo.broadcast_in_dim"(%0) <{broadcast_dimensions = dense<[0, 2]> : tensor<2xi64>}> : (tensor<2x63xf32>) -> tensor<2x3x63xf32> +// CHECK: return %1 : tensor<2x3x63xf32> + +// ----- + +// CHECK-LABEL: broadcast_reshape_not_same_non_unit_dims +func.func @broadcast_reshape_not_same_non_unit_dims(%arg0: tensor<63x1x1x1xf32>) -> tensor<2x1x63xf32> { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 1, 2, 3]> : tensor<4xi64>}> : (tensor<63x1x1x1xf32>) -> tensor<63x1x1x2xf32> + %1 = mhlo.reshape %0 : (tensor<63x1x1x2xf32>) -> tensor<2x1x63xf32> + return %1 : tensor<2x1x63xf32> +} + +// CHECK: %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 1, 2, 3]> : tensor<4xi64>}> : (tensor<63x1x1x1xf32>) -> tensor<63x1x1x2xf32> +// CHECK: %1 = mhlo.reshape %0 : (tensor<63x1x1x2xf32>) -> tensor<2x1x63xf32> +// CHECK: return %1 : tensor<2x1x63xf32> + +// ----- + +// CHECK-LABEL: broadcast_reshape_multi_use +func.func @broadcast_reshape_multi_use(%arg0: tensor<1x1x1x63xf32>) -> (tensor<32x1x63xf32>, tensor<1x32x1x63xf32>) { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 1, 2, 3]> : tensor<4xi64>}> : (tensor<1x1x1x63xf32>) -> tensor<1x32x1x63xf32> + %1 = mhlo.reshape %0 : (tensor<1x32x1x63xf32>) -> tensor<32x1x63xf32> + return %1, %0 : tensor<32x1x63xf32>, tensor<1x32x1x63xf32> +} + +// CHECK: %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 1, 2, 3]> : tensor<4xi64>}> : (tensor<1x1x1x63xf32>) -> tensor<1x32x1x63xf32> +// CHECK: %1 = mhlo.reshape %0 : (tensor<1x32x1x63xf32>) -> tensor<32x1x63xf32> + +// ----- + +// CHECK-LABEL: broadcast_reshape_rank_increase +func.func @broadcast_reshape_rank_increase(%arg0: tensor<1x1x1x63xf32>) -> tensor<32x1x1x1x1x63xf32> { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 1, 2, 3]> : tensor<4xi64>}> : (tensor<1x1x1x63xf32>) -> tensor<1x32x1x63xf32> + %1 = mhlo.reshape %0 : (tensor<1x32x1x63xf32>) -> tensor<32x1x1x1x1x63xf32> + return %1 : tensor<32x1x1x1x1x63xf32> +} + +// CHECK: %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 1, 2, 3]> : tensor<4xi64>}> : (tensor<1x1x1x63xf32>) -> tensor<1x32x1x63xf32> +// CHECK: %1 = mhlo.reshape %0 : (tensor<1x32x1x63xf32>) -> tensor<32x1x1x1x1x63xf32> + + diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/optimize_layout.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/optimize_layout.mlir new file mode 100644 index 0000000000000000000000000000000000000000..e1cf3bada1ece57d040d32576a0e1d83ec208706 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/optimize_layout.mlir @@ -0,0 +1,50 @@ +// RUN: odml-to-stablehlo-opt %s --transpose-commute-ops | FileCheck %s +// CHECK-LABEL: func.func @commute_transpose_pad( +// CHECK-SAME: %[[INPUT:.*]]: tensor<1x112x112x64xf32>, +// CHECK-SAME: %[[PAD_VAL:.*]]: tensor) -> tensor<1x64x114x114xf32> { +// CHECK: %[[PAD:.*]] = stablehlo.pad %[[INPUT]], %[[PAD_VAL]], +// CHECK: low = [0, 1, 1, 0], high = [0, 1, 1, 0], interior = [0, 0, 0, 0] +// CHECK: : (tensor<1x112x112x64xf32>, tensor) -> tensor<1x114x114x64xf32> +// CHECK: %[[TPOS:.*]] = stablehlo.transpose %[[PAD]], dims = [0, 3, 1, 2] +// CHECK: : (tensor<1x114x114x64xf32>) -> tensor<1x64x114x114xf32> +// CHECK: return %[[TPOS]] : tensor<1x64x114x114xf32> + +func.func @commute_transpose_pad( + %arg0: tensor<1x112x112x64xf32>, %padding_val: tensor) + -> tensor<1x64x114x114xf32> { + %tspos = stablehlo.transpose %arg0, dims = [0, 3, 1, 2] + : (tensor<1x112x112x64xf32>) -> tensor<1x64x112x112xf32> + %ret = stablehlo.pad %tspos, %padding_val, + low = [0, 0, 1, 1], high = [0, 0, 1, 1], interior = [0, 0, 0, 0] + : (tensor<1x64x112x112xf32>, tensor) -> tensor<1x64x114x114xf32> + return %ret :tensor<1x64x114x114xf32> +} + +// ----- +// CHECK-LABEL: func.func @commute_transpose_reduce_window( +// CHECK-SAME: %[[INPUT:.*]]: tensor<1x114x114x64xf32>, +// CHECK-SAME: %[[PAD_VAL:.*]]: tensor) -> tensor<1x64x56x56xf32> { +// CHECK: %[[REDUCE:.*]] = "stablehlo.reduce_window"(%[[INPUT]], %[[PAD_VAL]]) +// CHECK: <{window_dimensions = array, +// CHECK: window_strides = array}> ({ +// CHECK: ^bb0(%[[ARG0:.*]]: tensor, %[[ARG1:.*]]: tensor): +// CHECK: %[[MAX:.*]] = stablehlo.maximum %[[ARG0]], %[[ARG1]] : tensor +// CHECK: stablehlo.return %[[MAX]] : tensor +// CHECK: }) : (tensor<1x114x114x64xf32>, tensor) -> tensor<1x56x56x64xf32> +// CHECK: %[[TPOS:.*]] = stablehlo.transpose %[[REDUCE]], dims = [0, 3, 1, 2] +// CHECK: : (tensor<1x56x56x64xf32>) -> tensor<1x64x56x56xf32> +// CHECK: return %[[TPOS]] : tensor<1x64x56x56xf32> + +func.func @commute_transpose_reduce_window( + %input: tensor<1x114x114x64xf32>, + %cst: tensor) -> tensor<1x64x56x56xf32> { + %tpos = stablehlo.transpose %input, dims = [0, 3, 1, 2] + : (tensor<1x114x114x64xf32>) -> tensor<1x64x114x114xf32> + %ret = "stablehlo.reduce_window"(%tpos, %cst) + <{window_dimensions = array, window_strides = array}> ({ + ^bb0(%arg0: tensor, %arg1: tensor): + %max = stablehlo.maximum %arg0, %arg1 : tensor + stablehlo.return %max: tensor + }) : (tensor<1x64x114x114xf32>, tensor) -> tensor<1x64x56x56xf32> + return %ret : tensor<1x64x56x56xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/prepare_hlo.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/prepare_hlo.mlir new file mode 100644 index 0000000000000000000000000000000000000000..483680a99d5243e6f0084fcab6e5e9514cbc6a0d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/prepare_hlo.mlir @@ -0,0 +1,895 @@ +// RUN: odml-to-stablehlo-opt %s -prepare-hlo -split-input-file | FileCheck %s --dump-input=fail + +// Just assert that pass is properly registered. +func.func @main(%arg0: tensor) -> tensor { + return %arg0: tensor +} +// CHECK-LABEL: main + +// ----- + +//===----------------------------------------------------------------------===// +// mhlo.convolution +//===----------------------------------------------------------------------===// + +// 2D +//=-- + +// CHECK-LABEL: transpose_conv2d_same_padding_nchw_ihwo +func.func @transpose_conv2d_same_padding_nchw_ihwo(%input: tensor<1x2x256x256xf32>, %filter:tensor<2x2x4x4xf32>) -> tensor<1x2x512x512xf32> { + %1 = mhlo.convolution(%input, %filter) + dim_numbers = [b, f, 0, 1]x[i, o, 0, 1]->[b, f, 0, 1], + window = {pad = [[2, 2], [2, 2]], lhs_dilate = [2, 2]} + {batch_group_count = 1 : i64, feature_group_count = 1 : i64} + : (tensor<1x2x256x256xf32>, tensor<2x2x4x4xf32>) -> tensor<1x2x512x512xf32> + func.return %1 : tensor<1x2x512x512xf32> +} + +// CHECK: %[[TRANSPOSED_INPUT:.*]] = "mhlo.transpose"(%arg0) +// CHECK-SAME: permutation +// CHECK-SAME: [0, 2, 3, 1] +// CHECK: %[[TRANSPOSED_KERNEL:.*]] = "mhlo.transpose"(%arg1) +// CHECK-SAME: permutation +// CHECK-SAME: [1, 2, 3, 0] +// CHECK: %[[CONV_OUT:.*]] = mhlo.convolution(%[[TRANSPOSED_INPUT]], %[[TRANSPOSED_KERNEL]]) +// CHECK-SAME: [b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f] +// CHECK: "mhlo.transpose"(%[[CONV_OUT]]) +// CHECK-SAME: permutation +// CHECK-SAME: [0, 3, 1, 2] + +// ----- + +// CHECK-LABEL: transpose_conv2d_same_padding_nchw_oihw +func.func @transpose_conv2d_same_padding_nchw_oihw(%input: tensor<1x2x256x256xf32>, %filter:tensor<2x2x4x4xf32>) -> tensor<1x2x512x512xf32> { + %0 = mhlo.convolution(%input, %filter) + dim_numbers = [b, f, 0, 1]x[o, i, 0, 1]->[b, f, 0, 1], + window = {pad = [[2, 2], [2, 2]], lhs_dilate = [2, 2]} { + batch_group_count = 1 : i64, + feature_group_count = 1 : i64 + } : (tensor<1x2x256x256xf32>, tensor<2x2x4x4xf32>) -> tensor<1x2x512x512xf32> + func.return %0 : tensor<1x2x512x512xf32> +} + +// CHECK: %[[TRANSPOSED_INPUT:.*]] = "mhlo.transpose"(%arg0) +// CHECK-SAME: permutation +// CHECK-SAME: [0, 2, 3, 1] +// CHECK: %[[TRANSPOSED_KERNEL:.*]] = "mhlo.transpose"(%arg1) +// CHECK-SAME: permutation +// CHECK-SAME: [0, 2, 3, 1] +// CHECK: %[[CONV_OUT:.*]] = mhlo.convolution(%[[TRANSPOSED_INPUT]], %[[TRANSPOSED_KERNEL]]) +// CHECK-SAME: [b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f] +// CHECK: "mhlo.transpose"(%[[CONV_OUT]]) +// CHECK-SAME: permutation +// CHECK-SAME: [0, 3, 1, 2] + +// ----- + +// CHECK-LABEL: depthwise_transpose_conv2d_same_padding_nchw_hwoi +func.func @depthwise_transpose_conv2d_same_padding_nchw_hwoi(%input: tensor<1x2x20x20xf32>, %filter:tensor<8x8x2x1xf32>) -> tensor<1x2x80x80xf32> { + %1 = mhlo.convolution(%input, %filter) + dim_numbers = [b, f, 0, 1]x[0, 1, o, i]->[b, f, 0, 1], + window = {pad = [[5, 5], [5, 5]], lhs_dilate = [4, 4]} + {batch_group_count = 1 : i64, feature_group_count = 2 : i64} + : (tensor<1x2x20x20xf32>, tensor<8x8x2x1xf32>) -> tensor<1x2x80x80xf32> + func.return %1 : tensor<1x2x80x80xf32> + + // CHECK: %0 = "mhlo.transpose"(%arg0) <{permutation = dense<[0, 2, 3, 1]> : tensor<4xi64>}> : (tensor<1x2x20x20xf32>) -> tensor<1x20x20x2xf32> + // CHECK: %1 = "mhlo.transpose"(%arg1) <{permutation = dense<[2, 0, 1, 3]> : tensor<4xi64>}> : (tensor<8x8x2x1xf32>) -> tensor<2x8x8x1xf32> + // CHECK: %2 = "mhlo.slice"(%0) <{limit_indices = dense<[1, 20, 20, 1]> : tensor<4xi64>, start_indices = dense<0> : tensor<4xi64>, strides = dense<1> : tensor<4xi64>}> : (tensor<1x20x20x2xf32>) -> tensor<1x20x20x1xf32> + // CHECK: %3 = "mhlo.slice"(%1) <{limit_indices = dense<[1, 8, 8, 1]> : tensor<4xi64>, start_indices = dense<0> : tensor<4xi64>, strides = dense<1> : tensor<4xi64>}> : (tensor<2x8x8x1xf32>) -> tensor<1x8x8x1xf32> + // CHECK: %4 = mhlo.convolution(%2, %3) dim_numbers = [b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f], window = {pad = {{\[\[}}5, 5], [5, 5]], lhs_dilate = [4, 4]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64} : (tensor<1x20x20x1xf32>, tensor<1x8x8x1xf32>) -> tensor<1x80x80x1xf32> + // CHECK: %5 = "mhlo.slice"(%0) <{limit_indices = dense<[1, 20, 20, 2]> : tensor<4xi64>, start_indices = dense<[0, 0, 0, 1]> : tensor<4xi64>, strides = dense<1> : tensor<4xi64>}> : (tensor<1x20x20x2xf32>) -> tensor<1x20x20x1xf32> + // CHECK: %6 = "mhlo.slice"(%1) <{limit_indices = dense<[2, 8, 8, 1]> : tensor<4xi64>, start_indices = dense<[1, 0, 0, 0]> : tensor<4xi64>, strides = dense<1> : tensor<4xi64>}> : (tensor<2x8x8x1xf32>) -> tensor<1x8x8x1xf32> + // CHECK: %7 = mhlo.convolution(%5, %6) dim_numbers = [b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f], window = {pad = {{\[\[}}5, 5], [5, 5]], lhs_dilate = [4, 4]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64} : (tensor<1x20x20x1xf32>, tensor<1x8x8x1xf32>) -> tensor<1x80x80x1xf32> + // CHECK: %8 = "mhlo.concatenate"(%4, %7) <{dimension = 3 : i64}> : (tensor<1x80x80x1xf32>, tensor<1x80x80x1xf32>) -> tensor<1x80x80x2xf32> + // CHECK: %9 = "mhlo.transpose"(%8) <{permutation = dense<[0, 3, 1, 2]> : tensor<4xi64>}> : (tensor<1x80x80x2xf32>) -> tensor<1x2x80x80xf32> + // CHECK: return %9 : tensor<1x2x80x80xf32> +} + +// ----- + +// CHECK-LABEL: conv2d_nhwc_ohwi_nhwc +func.func @conv2d_nhwc_ohwi_nhwc(%input: tensor<1x256x256x3xf32>, %filter: tensor<2x1x1x3xf32>) -> tensor<1x256x256x2xf32> { + %0 = mhlo.convolution(%input, %filter) + dim_numbers = [b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f], + window = {stride = [1, 1], pad = [[0, 0], [0, 0]]} { + batch_group_count = 1 : i64, + feature_group_count = 1 : i64, + window_strides = dense<1> : tensor<2xi64>, + padding = dense<0> : tensor<2x2xi64>, + rhs_dilation = dense<[1, 1]> : tensor<2xi64>, + lhs_dilation = dense<[1, 1]> : tensor<2xi64> + } : (tensor<1x256x256x3xf32>, tensor<2x1x1x3xf32>) -> tensor<1x256x256x2xf32> + func.return %0 : tensor<1x256x256x2xf32> +} + +// CHECK-NOT: transpose +// CHECK: [b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f] +// CHECK-NOT: transpose + +// ----- + +// CHECK-LABEL: conv2d_nhwc_ohwi_nhwc_dynamic +func.func @conv2d_nhwc_ohwi_nhwc_dynamic(%input: tensor, %filter: tensor<2x1x1x3xf32>) -> tensor { + %0 = mhlo.convolution(%input, %filter) + dim_numbers = [b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f], + window = {stride = [1, 1], pad = [[0, 0], [0, 0]]} { + batch_group_count = 1 : i64, + feature_group_count = 1 : i64, + window_strides = dense<1> : tensor<2xi64>, + padding = dense<0> : tensor<2x2xi64>, + rhs_dilation = dense<[1, 1]> : tensor<2xi64>, + lhs_dilation = dense<[1, 1]> : tensor<2xi64> + } : (tensor, tensor<2x1x1x3xf32>) -> tensor + func.return %0 : tensor +} + +// CHECK-NOT: transpose +// CHECK: [b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f] +// CHECK-NOT: transpose + +// ----- + +// CHECK-LABEL: conv2d_nchw_ohwi_nhwc +func.func @conv2d_nchw_ohwi_nhwc(%input: tensor, %filter: tensor<2x1x1x3xf32>) -> tensor { + %0 = mhlo.convolution(%input, %filter) + dim_numbers = [b, f, 0, 1]x[o, 0, 1, i]->[b, 0, 1, f], + window = {stride = [1, 1], pad = [[0, 0], [0, 0]]} { + batch_group_count = 1 : i64, + feature_group_count = 1 : i64 + } : (tensor, tensor<2x1x1x3xf32>) -> tensor + func.return %0 : tensor +} + +// CHECK: %[[TRANSPOSED_INPUT:.*]] = "mhlo.transpose"(%arg0) +// CHECK-SAME: permutation +// CHECK-SAME: [0, 2, 3, 1] +// CHECK: mhlo.convolution(%[[TRANSPOSED_INPUT]], %arg1) +// CHECK-SAME: [b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f] +// CHECK-NOT: transpose + +// ----- + +// CHECK-LABEL: conv2d_nchw_ohwi_nhwc_dynamic_batch +func.func @conv2d_nchw_ohwi_nhwc_dynamic_batch(%input: tensor, %filter: tensor<2x1x1x3xf32>) -> tensor { + %0 = mhlo.convolution(%input, %filter) + dim_numbers = [b, f, 0, 1]x[o, 0, 1, i]->[b, 0, 1, f], + window = {stride = [1, 1], pad = [[0, 0], [0, 0]]} { + batch_group_count = 1 : i64, + feature_group_count = 1 : i64 + } : (tensor, tensor<2x1x1x3xf32>) -> tensor + func.return %0 : tensor +} + +// CHECK: %[[TRANSPOSED_INPUT:.*]] = "mhlo.transpose"(%arg0) +// CHECK-SAME: permutation +// CHECK-SAME: [0, 2, 3, 1] +// CHECK: mhlo.convolution(%[[TRANSPOSED_INPUT]], %arg1) +// CHECK-SAME: [b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f] +// CHECK-NOT: transpose + +// ----- + +// CHECK-LABEL: conv2d_nhwc_hwio_nhwc +func.func @conv2d_nhwc_hwio_nhwc(%input: tensor<1x256x256x3xf32>, %filter: tensor<1x1x3x2xf32>) -> tensor<1x256x256x2xf32> { + %0 = mhlo.convolution(%input, %filter) + dim_numbers = [b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f], + window = {stride = [1, 1], pad = [[0, 0], [0, 0]]} { + batch_group_count = 1 : i64, + feature_group_count = 1 : i64 + } : (tensor<1x256x256x3xf32>, tensor<1x1x3x2xf32>) -> tensor<1x256x256x2xf32> + func.return %0 : tensor<1x256x256x2xf32> +} + +// CHECK: %[[TRANSPOSED_KERNEL:.*]] = "mhlo.transpose"(%arg1) +// CHECK-SAME: permutation +// CHECK-SAME: [3, 0, 1, 2] +// CHECK: mhlo.convolution(%arg0, %[[TRANSPOSED_KERNEL]]) +// CHECK-SAME: [b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f] +// CHECK-NOT: transpose + +// ----- + +// CHECK-LABEL: conv2d_nhwc_ohwi_nchw +func.func @conv2d_nhwc_ohwi_nchw(%input: tensor<1x256x256x3xf32>, %filter: tensor<2x1x1x3xf32>) -> tensor<1x2x256x256xf32> { + %0 = mhlo.convolution(%input, %filter) + dim_numbers = [b, 0, 1, f]x[o, 0, 1, i]->[b, f, 0, 1], + window = {stride = [1, 1], pad = [[0, 0], [0, 0]]} { + batch_group_count = 1 : i64, + feature_group_count = 1 : i64 + } : (tensor<1x256x256x3xf32>, tensor<2x1x1x3xf32>) -> tensor<1x2x256x256xf32> + func.return %0 : tensor<1x2x256x256xf32> +} + +// CHECK-NOT: transpose +// CHECK: %[[CONV_OUT:.*]] = mhlo.convolution +// CHECK-SAME: [b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f] +// CHECK: "mhlo.transpose"(%[[CONV_OUT]]) +// CHECK-SAME: permutation +// CHECK-SAME: [0, 3, 1, 2] + +// ----- + +// CHECK-LABEL: conv2d_nchw_oihw_nchw +func.func @conv2d_nchw_oihw_nchw(%input: tensor<1x3x256x256xf32>, %filter: tensor<2x3x1x1xf32>) -> tensor<1x2x256x256xf32> { + %0 = mhlo.convolution(%input, %filter) + dim_numbers = [b, f, 0, 1]x[o, i, 0, 1]->[b, f, 0, 1], + window = {stride = [1, 1], pad = [[0, 0], [0, 0]]} { + batch_group_count = 1 : i64, + feature_group_count = 1 : i64 + } : (tensor<1x3x256x256xf32>, tensor<2x3x1x1xf32>) -> tensor<1x2x256x256xf32> + func.return %0 : tensor<1x2x256x256xf32> +} + +// CHECK: %[[TRANSPOSED_INPUT:.*]] = "mhlo.transpose"(%arg0) +// CHECK-SAME: permutation +// CHECK-SAME: [0, 2, 3, 1] +// CHECK: %[[TRANSPOSED_KERNEL:.*]] = "mhlo.transpose"(%arg1) +// CHECK-SAME: permutation +// CHECK-SAME: [0, 2, 3, 1] +// CHECK: %[[CONV_OUT:.*]] = mhlo.convolution(%[[TRANSPOSED_INPUT]], %[[TRANSPOSED_KERNEL]]) +// CHECK-SAME: [b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f] +// CHECK: "mhlo.transpose"(%[[CONV_OUT]]) +// CHECK-SAME: permutation +// CHECK-SAME: [0, 3, 1, 2] + +// ----- + +// CHECK-LABEL: conv2d_nhwc_ohwi_nhwc_padded +func.func @conv2d_nhwc_ohwi_nhwc_padded(%input: tensor<1x254x254x3xf32>, %filter: tensor<2x1x1x3xf32>) -> tensor<1x256x256x2xf32> { + %0 = "mhlo.convolution"(%input, %filter) { + dimension_numbers = #mhlo.conv<[b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f]>, + batch_group_count = 1 : i64, + feature_group_count = 1 : i64, + window_strides = dense<1> : tensor<2xi64>, + padding = dense<1> : tensor<2x2xi64>, + rhs_dilation = dense<[1, 1]> : tensor<2xi64>, + lhs_dilation = dense<[1, 1]> : tensor<2xi64> + } : (tensor<1x254x254x3xf32>, tensor<2x1x1x3xf32>) -> tensor<1x256x256x2xf32> + func.return %0 : tensor<1x256x256x2xf32> +} + +// CHECK: %[[PADDED_LHS:.*]] = "mhlo.pad" +// CHECK-SAME: edge_padding_high = dense<[0, 1, 1, 0]> +// CHECK-SAME: edge_padding_low = dense<[0, 1, 1, 0]> +// CHECK-SAME: interior_padding = dense<0> +// CHECK: mhlo.convolution(%[[PADDED_LHS]] +// CHECK-SAME: pad +// CHECK-SAME: [0, 0], [0, 0] +// CHECK-SAME: (tensor<1x256x256x3xf32>, tensor<2x1x1x3xf32>) -> tensor<1x256x256x2xf32> + +// ----- + +// CHECK-LABEL: conv2d_nhwc_ohwi_nhwc_asymmetric_padded +func.func @conv2d_nhwc_ohwi_nhwc_asymmetric_padded(%input: tensor<1x255x255x3xf32>, %filter: tensor<2x1x1x3xf32>) -> tensor<1x256x256x2xf32> { + %0 = "mhlo.convolution"(%input, %filter) { + dimension_numbers = #mhlo.conv<[b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f]>, + batch_group_count = 1 : i64, + feature_group_count = 1 : i64, + window_strides = dense<1> : tensor<2xi64>, + padding = dense<[[0, 1], [0, 1]]> : tensor<2x2xi64>, + rhs_dilation = dense<[1, 1]> : tensor<2xi64>, + lhs_dilation = dense<[1, 1]> : tensor<2xi64> + } : (tensor<1x255x255x3xf32>, tensor<2x1x1x3xf32>) -> tensor<1x256x256x2xf32> + func.return %0 : tensor<1x256x256x2xf32> +} + +// CHECK: %[[PADDED_LHS:.*]] = "mhlo.pad" +// CHECK-SAME: edge_padding_high = dense<[0, 1, 1, 0]> +// CHECK-SAME: edge_padding_low = dense<0> +// CHECK-SAME: interior_padding = dense<0> +// CHECK: mhlo.convolution(%[[PADDED_LHS]] +// CHECK-SAME: pad +// CHECK-SAME: [0, 0], [0, 0] +// CHECK-SAME: (tensor<1x256x256x3xf32>, tensor<2x1x1x3xf32>) -> tensor<1x256x256x2xf32> + + +// ----- + +// CHECK-LABEL: conv2d_nchw_ohwi_nhwc_padded +func.func @conv2d_nchw_ohwi_nhwc_padded(%input: tensor<1x3x253x249xf32>, %filter: tensor<2x1x1x3xf32>) -> tensor<1x256x256x2xf32> { + %0 = mhlo.convolution(%input, %filter) + dim_numbers = [b, f, 0, 1]x[o, 0, 1, i]->[b, 0, 1, f], + window = {stride = [1, 1], pad = [[1, 2], [3, 4]]} { + batch_group_count = 1 : i64, + feature_group_count = 1 : i64 + } : (tensor<1x3x253x249xf32>, tensor<2x1x1x3xf32>) -> tensor<1x256x256x2xf32> + func.return %0 : tensor<1x256x256x2xf32> +} + +// Want to ensure that we transpose before padding input (which this test does implicitly). + +// CHECK: %[[PADDED_LHS:.*]] = "mhlo.pad" +// CHECK-SAME: edge_padding_high = dense<[0, 2, 4, 0]> +// CHECK-SAME: edge_padding_low = dense<[0, 1, 3, 0]> +// CHECK-SAME: interior_padding = dense<0> +// CHECK: mhlo.convolution(%[[PADDED_LHS]], %arg1) +// CHECK-SAME: pad +// CHECK-SAME: [0, 0], [0, 0] +// CHECK-SAME: (tensor<1x256x256x3xf32>, tensor<2x1x1x3xf32>) -> tensor<1x256x256x2xf32> + +// ----- + +// CHECK-LABEL: conv2d_nchw_ohwi_nhwc_padded_dilated_lhs +func.func @conv2d_nchw_ohwi_nhwc_padded_dilated_lhs(%input: tensor<1x64x64x256xf32>, %filter: tensor<64x2x2x256xf32>) -> tensor<1x128x128x64xf32> { + %0 = "mhlo.convolution"(%input, %filter) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f]>, + feature_group_count = 1 : i64, + lhs_dilation = dense<2> : tensor<2xi64>, + padding = dense<1> : tensor<2x2xi64>, + precision_config = [#mhlo, #mhlo], + rhs_dilation = dense<1> : tensor<2xi64>, + window_reversal = dense : tensor<2xi1>, + window_strides = dense<1> : tensor<2xi64>} : + (tensor<1x64x64x256xf32>, tensor<64x2x2x256xf32>) -> tensor<1x128x128x64xf32> + func.return %0 : tensor<1x128x128x64xf32> +} + +// CHECK-NOT: mhlo.pad +// CHECK: mhlo.convolution +// CHECK-SAME: pad +// CHECK-SAME: [1, 1], [1, 1] +// CHECK-SAME: lhs_dilate = [2, 2] + +// ----- + +// CHECK-LABEL: depthwise_conv2d_nhwc_ohwi_nhwc +func.func @depthwise_conv2d_nhwc_ohwi_nhwc(%arg0: tensor<1x10x10x207xf32>, %arg1: tensor<3312x3x3x1xf32>) -> tensor<1x8x8x3312xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f]>, + feature_group_count = 207 : i64, + lhs_dilation = dense<1> : tensor<2xi64>, + padding = dense<0> : tensor<2x2xi64>, + precision_config = [#mhlo, #mhlo], + rhs_dilation = dense<1> : tensor<2xi64>, + window_strides = dense<1> : tensor<2xi64> + } : (tensor<1x10x10x207xf32>, tensor<3312x3x3x1xf32>) -> tensor<1x8x8x3312xf32> + func.return %0 : tensor<1x8x8x3312xf32> +} + +// CHECK: %[[TRANSPOSED_KERNEL:.*]] = "mhlo.transpose"(%arg1) +// CHECK-SAME: permutation +// CHECK-SAME: [3, 1, 2, 0] +// CHECK: mhlo.convolution(%arg0, %[[TRANSPOSED_KERNEL]]) +// CHECK-SAME: [b, 0, 1, f]x[i, 0, 1, o]->[b, 0, 1, f] +// CHECK-NOT: transpose + +// ----- + + +// CHECK-LABEL: depthwise_conv2d_nchw_ihwo_nhwc +func.func @depthwise_conv2d_nchw_ihwo_nhwc(%arg0: tensor<1x207x10x10xf32>, %arg1: tensor<1x3x3x3312xf32>) -> tensor<1x8x8x3312xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, f, 0, 1]x[i, 0, 1, o]->[b, 0, 1, f]>, + feature_group_count = 207 : i64, + lhs_dilation = dense<1> : tensor<2xi64>, + padding = dense<0> : tensor<2x2xi64>, + precision_config = [#mhlo, #mhlo], + rhs_dilation = dense<1> : tensor<2xi64>, + window_strides = dense<1> : tensor<2xi64> + } : (tensor<1x207x10x10xf32>, tensor<1x3x3x3312xf32>) -> tensor<1x8x8x3312xf32> + func.return %0 : tensor<1x8x8x3312xf32> +} + +// CHECK: %[[TRANSPOSED_INPUT:.*]] = "mhlo.transpose"(%arg0) +// CHECK-SAME: permutation +// CHECK-SAME: [0, 2, 3, 1] +// CHECK: mhlo.convolution(%[[TRANSPOSED_INPUT]], %arg1) +// CHECK-SAME: [b, 0, 1, f]x[i, 0, 1, o]->[b, 0, 1, f] +// CHECK-NOT: transpose + +// ----- + +// CHECK-LABEL: depthwise_conv2d_nchw_ihwo_nhwc_padded +func.func @depthwise_conv2d_nchw_ihwo_nhwc_padded(%arg0: tensor<1x207x8x8xf32>, %arg1: tensor<1x3x3x3312xf32>) -> tensor<1x8x8x3312xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, f, 0, 1]x[i, 0, 1, o]->[b, 0, 1, f]>, + feature_group_count = 207 : i64, + lhs_dilation = dense<1> : tensor<2xi64>, + padding = dense<1> : tensor<2x2xi64>, + precision_config = [#mhlo, #mhlo], + rhs_dilation = dense<1> : tensor<2xi64>, + window_strides = dense<1> : tensor<2xi64> + } : (tensor<1x207x8x8xf32>, tensor<1x3x3x3312xf32>) -> tensor<1x8x8x3312xf32> + func.return %0 : tensor<1x8x8x3312xf32> +} + +// CHECK: %[[TRANSPOSED_INPUT:.*]] = "mhlo.transpose"(%arg0) +// CHECK-SAME: permutation +// CHECK-SAME: [0, 2, 3, 1] +// CHECK: %[[PADDED_LHS:.*]] = "mhlo.pad"(%[[TRANSPOSED_INPUT]] +// CHECK-SAME: edge_padding_high = dense<[0, 1, 1, 0]> +// CHECK-SAME: edge_padding_low = dense<[0, 1, 1, 0]> +// CHECK-SAME: interior_padding = dense<0> +// CHECK: mhlo.convolution(%[[PADDED_LHS]], %arg1) +// CHECK-SAME: [b, 0, 1, f]x[i, 0, 1, o]->[b, 0, 1, f] +// CHECK-SAME: pad = +// CHECK-SAME: [0, 0], [0, 0] + +// ----- + +// 3D +//=-- + +// CHECK-LABEL: conv3d_ndhwc_dhwio_ndhwc +func.func @conv3d_ndhwc_dhwio_ndhwc(%arg0: tensor<1x8x8x32x207xf32>, %arg1: tensor<3x3x32x207x16xf32>) -> tensor<1x6x6x1x16xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, 1, 2, f]x[0, 1, 2, i, o]->[b, 0, 1, 2, f]>, + feature_group_count = 1 : i64} : + (tensor<1x8x8x32x207xf32>, tensor<3x3x32x207x16xf32>) -> tensor<1x6x6x1x16xf32> + func.return %0 : tensor<1x6x6x1x16xf32> +} + +// CHECK-NOT: transpose +// CHECK: mhlo.convolution +// CHECK-SAME: [b, 0, 1, 2, f]x[0, 1, 2, i, o]->[b, 0, 1, 2, f] +// CHECK-NOT: transpose + +// ----- + +// CHECK-LABEL: conv3d_ncdhw_dhwio_ndhwc +func.func @conv3d_ncdhw_dhwio_ndhwc(%arg0: tensor<1x207x8x8x32xf32>, %arg1: tensor<3x3x32x207x16xf32>) -> tensor<1x6x6x1x16xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, f, 0, 1, 2]x[0, 1, 2, i, o]->[b, 0, 1, 2, f]>, + feature_group_count = 1 : i64} : + (tensor<1x207x8x8x32xf32>, tensor<3x3x32x207x16xf32>) -> tensor<1x6x6x1x16xf32> + func.return %0 : tensor<1x6x6x1x16xf32> +} + +// CHECK: %[[TRANSPOSED_INPUT:.*]] = "mhlo.transpose"(%arg0) +// CHECK-SAME: permutation +// CHECK-SAME: [0, 2, 3, 4, 1] +// CHECK: mhlo.convolution(%[[TRANSPOSED_INPUT]], %arg1) +// CHECK-SAME: [b, 0, 1, 2, f]x[0, 1, 2, i, o]->[b, 0, 1, 2, f] +// CHECK-NOT: transpose + +// ----- + +// CHECK-LABEL: conv3d_ndhwc_odhwi_ndhwc +func.func @conv3d_ndhwc_odhwi_ndhwc(%arg0: tensor<1x8x8x32x207xf32>, %arg1: tensor<16x3x3x32x207xf32>) -> tensor<1x6x6x1x16xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, 1, 2, f]x[o, 0, 1, 2, i]->[b, 0, 1, 2, f]>, + feature_group_count = 1 : i64} : + (tensor<1x8x8x32x207xf32>, tensor<16x3x3x32x207xf32>) -> tensor<1x6x6x1x16xf32> + func.return %0 : tensor<1x6x6x1x16xf32> +} + +// CHECK: %[[TRANSPOSED_KERNEL:.*]] = "mhlo.transpose"(%arg1) +// CHECK-SAME: permutation +// CHECK-SAME: [1, 2, 3, 4, 0] +// CHECK: mhlo.convolution(%arg0, %[[TRANSPOSED_KERNEL]]) +// CHECK-SAME: [b, 0, 1, 2, f]x[0, 1, 2, i, o]->[b, 0, 1, 2, f] +// CHECK-NOT: transpose + +// ----- + +// CHECK-LABEL: conv3d_ndhwc_dhwio_ncdhw +func.func @conv3d_ndhwc_dhwio_ncdhw(%arg0: tensor<1x8x8x32x207xf32>, %arg1: tensor<3x3x32x207x16xf32>) -> tensor<1x16x6x6x1xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, 1, 2, f]x[0, 1, 2, i, o]->[b, f, 0, 1, 2]>, + feature_group_count = 1 : i64} : + (tensor<1x8x8x32x207xf32>, tensor<3x3x32x207x16xf32>) -> tensor<1x16x6x6x1xf32> + func.return %0 : tensor<1x16x6x6x1xf32> +} + +// CHECK-NOT: transpose +// CHECK: %[[CONV_OUT:.*]] = mhlo.convolution +// CHECK-SAME: [b, 0, 1, 2, f]x[0, 1, 2, i, o]->[b, 0, 1, 2, f] +// CHECK: "mhlo.transpose"(%[[CONV_OUT]]) +// CHECK-SAME: permutation +// CHECK-SAME: [0, 4, 1, 2, 3] + +// ----- + +// CHECK-LABEL: conv3d_ndhwc_dhwio_ndhwc_padded +func.func @conv3d_ndhwc_dhwio_ndhwc_padded(%arg0: tensor<1x6x6x30x207xf32>, %arg1: tensor<3x3x32x207x16xf32>) -> tensor<1x6x6x1x16xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, 1, 2, f]x[0, 1, 2, i, o]->[b, 0, 1, 2, f]>, + feature_group_count = 1 : i64, + padding = dense<1> : tensor<3x2xi64>} : + (tensor<1x6x6x30x207xf32>, tensor<3x3x32x207x16xf32>) -> tensor<1x6x6x1x16xf32> + func.return %0 : tensor<1x6x6x1x16xf32> +} + +// CHECK: %[[PADDED_LHS:.*]] = "mhlo.pad" +// CHECK-SAME: edge_padding_high = dense<[0, 1, 1, 1, 0]> +// CHECK-SAME: edge_padding_low = dense<[0, 1, 1, 1, 0]> +// CHECK-SAME: interior_padding = dense<0> +// CHECK: mhlo.convolution(%[[PADDED_LHS]], %arg1) +// CHECK-SAME: pad = +// CHECK-SAME: [0, 0], [0, 0], [0, 0] +// CHECK-SAME: (tensor<1x8x8x32x207xf32>, tensor<3x3x32x207x16xf32>) -> tensor<1x6x6x1x16xf32> + +// ----- + +// CHECK-LABEL: conv3d_ncdhw_dhwio_ndhwc_padded +func.func @conv3d_ncdhw_dhwio_ndhwc_padded(%arg0: tensor<1x207x6x6x30xf32>, %arg1: tensor<3x3x32x207x16xf32>) -> tensor<1x6x6x1x16xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, f, 0, 1, 2]x[0, 1, 2, i, o]->[b, 0, 1, 2, f]>, + feature_group_count = 1 : i64, + padding = dense<1> : tensor<3x2xi64>} : + (tensor<1x207x6x6x30xf32>, tensor<3x3x32x207x16xf32>) -> tensor<1x6x6x1x16xf32> + func.return %0 : tensor<1x6x6x1x16xf32> +} + +// CHECK: %[[PADDED_LHS:.*]] = "mhlo.pad" +// CHECK-SAME: edge_padding_high = dense<[0, 1, 1, 1, 0]> +// CHECK-SAME: edge_padding_low = dense<[0, 1, 1, 1, 0]> +// CHECK-SAME: interior_padding = dense<0> +// CHECK: mhlo.convolution(%[[PADDED_LHS]], %arg1) +// CHECK-SAME: pad = +// CHECK-SAME: [0, 0], [0, 0], [0, 0] +// CHECK-SAME: (tensor<1x8x8x32x207xf32>, tensor<3x3x32x207x16xf32>) -> tensor<1x6x6x1x16xf32> + +// ----- + +// 1D +//=-- + +// CHECK-LABEL: conv1d_nsc_osi_nsc +func.func @conv1d_nsc_osi_nsc(%arg0: tensor<16x32x256xf32>, %arg1: tensor<256x1x256xf32>) -> tensor<16x32x256xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, f]x[o, 0, i]->[b, 0, f]>, + feature_group_count = 1 : i64, + lhs_dilation = dense<1> : tensor<1xi64>, + padding = dense<0> : tensor<1x2xi64>, + precision_config = [#mhlo, #mhlo], + rhs_dilation = dense<1> : tensor<1xi64>, + window_strides = dense<1> : tensor<1xi64> + } : (tensor<16x32x256xf32>, tensor<256x1x256xf32>) -> tensor<16x32x256xf32> + func.return %0 : tensor<16x32x256xf32> +} + +// CHECK: %[[RESHAPED_LHS:.*]] = "tfl.expand_dims"(%arg0 +// CHECK: %[[RESHAPED_RHS:.*]] = "tfl.expand_dims"(%arg1 +// CHECK: %[[CONV_OUT:.*]] = mhlo.convolution(%[[RESHAPED_LHS]], %[[RESHAPED_RHS]]) dim_numbers = [b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f] +// CHECK: "tfl.squeeze"(%[[CONV_OUT]] + +// ----- + +// CHECK-LABEL: conv1d_nsc_sio_nsc +func.func @conv1d_nsc_sio_nsc(%arg0: tensor<16x32x256xf32>, %arg1: tensor<1x256x256xf32>) -> tensor<16x32x256xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, f]x[0, i, o]->[b, 0, f]>, + feature_group_count = 1 : i64, + lhs_dilation = dense<1> : tensor<1xi64>, + padding = dense<0> : tensor<1x2xi64>, + precision_config = [#mhlo, #mhlo], + rhs_dilation = dense<1> : tensor<1xi64>, + window_strides = dense<1> : tensor<1xi64> + } : (tensor<16x32x256xf32>, tensor<1x256x256xf32>) -> tensor<16x32x256xf32> + func.return %0 : tensor<16x32x256xf32> +} + +// CHECK: %[[RESHAPED_LHS:.*]] = "tfl.expand_dims"(%arg0 +// CHECK: %[[RESHAPED_RHS:.*]] = "tfl.expand_dims"(%arg1 +// CHECK: %[[TPOSED_RHS:.*]] = "mhlo.transpose"(%[[RESHAPED_RHS]]) <{permutation = dense<[3, 0, 1, 2]> : tensor<4xi64>}> : (tensor<1x1x256x256xf32>) -> tensor<256x1x1x256xf32> +// CHECK: %[[CONV_OUT:.*]] = mhlo.convolution(%[[RESHAPED_LHS]], %[[TPOSED_RHS]]) dim_numbers = [b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f] +// CHECK: "tfl.squeeze"(%[[CONV_OUT]] + +// ----- + +// CHECK-LABEL: conv1d_ncs_osi_nsc_padded +func.func @conv1d_ncs_osi_nsc_padded(%arg0: tensor<16x256x30xf32>, %arg1: tensor<256x1x256xf32>) -> tensor<16x32x256xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, f, 0]x[o, 0, i]->[b, 0, f]>, + feature_group_count = 1 : i64, + padding = dense<1> : tensor<1x2xi64> + } : (tensor<16x256x30xf32>, tensor<256x1x256xf32>) -> tensor<16x32x256xf32> + func.return %0 : tensor<16x32x256xf32> +} + +// CHECK: %[[RESHAPED_LHS:.*]] = "tfl.expand_dims"(%arg0{{.*}}-> tensor<16x256x30x1xf32> +// CHECK: %[[RESHAPED_RHS:.*]] = "tfl.expand_dims"(%arg1{{.*}}-> tensor<256x1x1x256xf32> +// CHECK: %[[TPOSED_LHS:.*]] = "mhlo.transpose"(%[[RESHAPED_LHS]]) <{permutation = dense<[0, 2, 3, 1]> : tensor<4xi64>}> : (tensor<16x256x30x1xf32>) -> tensor<16x30x1x256xf32> +// CHECK: %[[PADDED_LHS:.*]] = "mhlo.pad"(%[[TPOSED_LHS]], %cst) <{edge_padding_high = dense<[0, 1, 0, 0]> : tensor<4xi64>, edge_padding_low = dense<[0, 1, 0, 0]> : tensor<4xi64>, interior_padding = dense<0> : tensor<4xi64>}> : (tensor<16x30x1x256xf32>, tensor) -> tensor<16x32x1x256xf32> +// CHECK: %[[CONV_OUT:.*]] = mhlo.convolution(%[[PADDED_LHS]], %[[RESHAPED_RHS]]) dim_numbers = [b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f] +// CHECK: "tfl.squeeze"(%[[CONV_OUT]]{{.*}}-> tensor<16x32x256xf32> + +// ----- + +// CHECK-LABEL: conv1d_ncs_osi_nsc_padded_dynamic +func.func @conv1d_ncs_osi_nsc_padded_dynamic(%arg0: tensor, %arg1: tensor<256x1x256xf32>) -> tensor { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, f, 0]x[o, 0, i]->[b, 0, f]>, + feature_group_count = 1 : i64, + padding = dense<1> : tensor<1x2xi64> + } : (tensor, tensor<256x1x256xf32>) -> tensor + func.return %0 : tensor +} + +// CHECK: %[[RESHAPED_LHS:.*]] = "tfl.expand_dims"(%arg0{{.*}}-> tensor +// CHECK: %[[RESHAPED_RHS:.*]] = "tfl.expand_dims"(%arg1{{.*}}-> tensor<256x1x1x256xf32> +// CHECK: %[[TPOSED_LHS:.*]] = "mhlo.transpose"(%[[RESHAPED_LHS]]) <{permutation = dense<[0, 2, 3, 1]> : tensor<4xi64>}> : (tensor) -> tensor +// CHECK: %[[PADDED_LHS:.*]] = "mhlo.pad"(%[[TPOSED_LHS]], %cst) <{edge_padding_high = dense<[0, 1, 0, 0]> : tensor<4xi64>, edge_padding_low = dense<[0, 1, 0, 0]> : tensor<4xi64>, interior_padding = dense<0> : tensor<4xi64>}> : (tensor, tensor) -> tensor +// CHECK: %[[CONV_OUT:.*]] = mhlo.convolution(%[[PADDED_LHS]], %[[RESHAPED_RHS]]) dim_numbers = [b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f] +// CHECK: "tfl.squeeze"(%[[CONV_OUT]]{{.*}}-> tensor + +// ----- + +//===----------------------------------------------------------------------===// +// mhlo.pad +//===----------------------------------------------------------------------===// + +// CHECK-LABEL: pad_2d +func.func @pad_2d(%arg0: tensor<3x3xf32>, %arg1: tensor) -> tensor<4x3xf32> { + %0 = "mhlo.pad"(%arg0, %arg1) { + edge_padding_low = dense<[0, -1]> : tensor<2xi64>, + edge_padding_high = dense<[1, 1]> : tensor<2xi64>, + interior_padding = dense<0> : tensor<2xi64> + } : (tensor<3x3xf32>, tensor) -> tensor<4x3xf32> + func.return %0 : tensor<4x3xf32> +} + +// CHECK: mhlo.slice +// CHECK-SAME: limit_indices = dense<3> +// CHECK-SAME: start_indices = dense<[0, 1]> +// CHECK-SAME: (tensor<3x3xf32>) -> tensor<3x2xf32> +// CHECK: mhlo.pad +// CHECK-SAME: edge_padding_high = dense<1> +// CHECK-SAME: edge_padding_low = dense<0> +// CHECK-SAME: (tensor<3x2xf32>, tensor) -> tensor<4x3xf32> + +// ----- + +// CHECK-LABEL: pad_2d_negative +func.func @pad_2d_negative(%arg0: tensor<3x3xf32>, %arg1: tensor) -> tensor<1x2xf32> { + %0 = "mhlo.pad"(%arg0, %arg1) { + edge_padding_low = dense<[-1, -1]> : tensor<2xi64>, + edge_padding_high = dense<[-1, 0]> : tensor<2xi64>, + interior_padding = dense<0> : tensor<2xi64> + } : (tensor<3x3xf32>, tensor) -> tensor<1x2xf32> + func.return %0 : tensor<1x2xf32> +} + +// CHECK: mhlo.slice +// CHECK-SAME: limit_indices = dense<[2, 3]> +// CHECK-SAME: start_indices = dense<1> +// CHECK-SAME: (tensor<3x3xf32>) -> tensor<1x2xf32> +// CHECK-NOT: mhlo.pad + +// ----- + +// CHECK-LABEL: pad_3d_mixed +func.func @pad_3d_mixed(%arg0: tensor<3x3x3xf32>, %arg1: tensor) -> tensor<3x3x3xf32> { + %0 = "mhlo.pad"(%arg0, %arg1) { + edge_padding_low = dense<[-1, 1, 0]> : tensor<3xi64>, + edge_padding_high = dense<[1, -1, 0]> : tensor<3xi64>, + interior_padding = dense<0> : tensor<3xi64> + } : (tensor<3x3x3xf32>, tensor) -> tensor<3x3x3xf32> + func.return %0 : tensor<3x3x3xf32> +} + +// CHECK: mhlo.slice +// CHECK-SAME: limit_indices = dense<[3, 2, 3]> +// CHECK-SAME: start_indices = dense<[1, 0, 0]> +// CHECK-SAME: (tensor<3x3x3xf32>) -> tensor<2x2x3xf32> +// CHECK: mhlo.pad +// CHECK-SAME: edge_padding_high = dense<[1, 0, 0]> +// CHECK-SAME: edge_padding_low = dense<[0, 1, 0]> +// CHECK-SAME: (tensor<2x2x3xf32>, tensor) -> tensor<3x3x3xf32> + +// ----- + +//===----------------------------------------------------------------------===// +// mhlo.reduce_window +//===----------------------------------------------------------------------===// + +// CHECK-LABEL: reduce_window_valid_channel_first +func.func @reduce_window_valid_channel_first(%arg0: tensor<4x3x16x16xf32>) -> tensor<4x3x7x7xf32> { + // "0xFF800000" represents -INF for f32. + %0 = mhlo.constant dense<0xFF800000> : tensor + %1 = "mhlo.reduce_window"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %2 = mhlo.maximum %arg1, %arg2 : tensor + mhlo.return %2 : tensor + }) { + base_dilations = dense<1> : tensor<4xi64>, + padding = dense<0> : tensor<4x2xi64>, + window_dilations = dense<1> : tensor<4xi64>, + window_dimensions = dense<[1, 1, 3, 3]> : tensor<4xi64>, + window_strides = dense<[1, 1, 2, 2]> : tensor<4xi64>} : (tensor<4x3x16x16xf32>, tensor) -> tensor<4x3x7x7xf32> + func.return %1 : tensor<4x3x7x7xf32> +} + +// CHECK: %[[INIT_CST:.*]] = mhlo.constant dense<0xFF800000> : tensor +// CHECK: %[[TPOSE_IN:.*]] = "mhlo.transpose"(%arg0) <{permutation = dense<[0, 2, 3, 1]> : tensor<4xi64>}> : (tensor<4x3x16x16xf32>) -> tensor<4x16x16x3xf32> +// CHECK: %[[RW:.*]] = "mhlo.reduce_window"(%[[TPOSE_IN]], %[[INIT_CST]]) +// CHECK-SAME: window_dimensions = dense<[1, 3, 3, 1]> +// CHECK-SAME: window_strides = dense<[1, 2, 2, 1]> +// CHECK: %3 = "mhlo.transpose"(%[[RW]]) <{permutation = dense<[0, 3, 1, 2]> : tensor<4xi64>}> : (tensor<4x7x7x3xf32>) -> tensor<4x3x7x7xf32> + +// ----- + +// CHECK-LABEL: reduce_window_same_channel_first +func.func @reduce_window_same_channel_first(%arg0: tensor<4x3x16x16xf32>) -> tensor<4x3x8x8xf32> { + // "0xFF800000" represents -INF for f32. + %0 = mhlo.constant dense<0xFF800000> : tensor + %1 = "mhlo.reduce_window"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %6 = mhlo.maximum %arg1, %arg2 : tensor + "mhlo.return"(%6) : (tensor) -> () + }) { + base_dilations = dense<1> : tensor<4xi64>, + padding = dense<[[0, 0], [0, 0], [0, 1], [0, 1]]> : tensor<4x2xi64>, + window_dilations = dense<1> : tensor<4xi64>, + window_dimensions = dense<[1, 1, 3, 3]> : tensor<4xi64>, + window_strides = dense<[1, 1, 2, 2]> : tensor<4xi64>} : (tensor<4x3x16x16xf32>, tensor) -> tensor<4x3x8x8xf32> + func.return %1 : tensor<4x3x8x8xf32> +} + +// CHECK: %[[INIT_CST:.*]] = mhlo.constant dense<0xFF800000> : tensor +// CHECK: %[[TPOSE_IN:.*]] = "mhlo.transpose"(%arg0) <{permutation = dense<[0, 2, 3, 1]> : tensor<4xi64>}> : (tensor<4x3x16x16xf32>) -> tensor<4x16x16x3xf32> +// CHECK: %[[RW:.*]] = "mhlo.reduce_window"(%[[TPOSE_IN]], %[[INIT_CST]]) +// CHECK-SAME: padding +// CHECK-SAME: [0, 0], [0, 1], [0, 1], [0, 0] +// CHECK-SAME: window_dimensions = dense<[1, 3, 3, 1]> +// CHECK-SAME: window_strides = dense<[1, 2, 2, 1]> +// CHECK: %3 = "mhlo.transpose"(%[[RW]]) <{permutation = dense<[0, 3, 1, 2]> : tensor<4xi64>}> : (tensor<4x8x8x3xf32>) -> tensor<4x3x8x8xf32> + +// ----- + +//===----------------------------------------------------------------------===// +// mhlo.dynamic_slice +//===----------------------------------------------------------------------===// + +// CHECK-LABEL: dynamic_slice +func.func @dynamic_slice(%arg0: tensor<7x3xf32>, %arg1: tensor, %arg2: tensor) -> tensor<4x2xf32> { + %0 = "mhlo.dynamic_slice"(%arg0, %arg1, %arg2) <{slice_sizes = dense<[4, 2]> : tensor<2xi64>}> : (tensor<7x3xf32>, tensor, tensor) -> tensor<4x2xf32> + func.return %0 : tensor<4x2xf32> +} + +// CHECK: mhlo.dynamic_slice +// CHECK-SAME: (tensor<7x3xf32>, tensor, tensor) -> tensor<4x2xf32> + +// ----- + +// CHECK-LABEL: dynamic_slice_ui32 +func.func @dynamic_slice_ui32(%arg0: tensor<7x3xf32>, %arg1: tensor, %arg2: tensor) -> tensor<4x2xf32> { + %0 = "mhlo.dynamic_slice"(%arg0, %arg1, %arg2) <{slice_sizes = dense<[4, 2]> : tensor<2xi64>}> : (tensor<7x3xf32>, tensor, tensor) -> tensor<4x2xf32> + func.return %0 : tensor<4x2xf32> +} + +// CHECK: mhlo.dynamic_slice +// CHECK-SAME: (tensor<7x3xf32>, tensor, tensor) -> tensor<4x2xf32> + +// CHECK-LABEL: dynamic_slice_ui64 +func.func @dynamic_slice_ui64(%arg0: tensor<7x3xf32>, %arg1: tensor, %arg2: tensor) -> tensor<4x2xf32> { + %0 = "mhlo.dynamic_slice"(%arg0, %arg1, %arg2) <{slice_sizes = dense<[4, 2]> : tensor<2xi64>}> : (tensor<7x3xf32>, tensor, tensor) -> tensor<4x2xf32> + func.return %0 : tensor<4x2xf32> +} + +// CHECK: mhlo.dynamic_slice +// CHECK-SAME: (tensor<7x3xf32>, tensor, tensor) -> tensor<4x2xf32> + +// ----- + +// CHECK-LABEL: dynamic_slice_i64 +func.func @dynamic_slice_i64(%arg0: tensor<7x3xf32>, %arg1: tensor, %arg2: tensor) -> tensor<4x2xf32> { + %0 = "mhlo.dynamic_slice"(%arg0, %arg1, %arg2) <{slice_sizes = dense<[4, 2]> : tensor<2xi64>}> : (tensor<7x3xf32>, tensor, tensor) -> tensor<4x2xf32> + func.return %0 : tensor<4x2xf32> +} + +// CHECK: mhlo.dynamic_slice +// CHECK-SAME: (tensor<7x3xf32>, tensor, tensor) -> tensor<4x2xf32> + +//===----------------------------------------------------------------------===// +// mhlo.custom_call +//===----------------------------------------------------------------------===// + +// ----- + +// CHECK-LABEL: @shape_assertion_custom_call +func.func @shape_assertion_custom_call(%arg1: tensor) -> tensor { + %0 = mhlo.constant dense<3> : tensor + %1 = "mhlo.get_dimension_size"(%arg1) <{dimension = 0 : i64}> : (tensor) -> tensor + %ok = mhlo.compare EQ, %1, %0, SIGNED : (tensor, tensor) -> tensor + mhlo.custom_call @shape_assertion(%ok) { + error_message = "The error message", + has_side_effect = true + } : (tensor) -> () + return %1 : tensor +} + +// CHECK-NOT: mhlo.custom_call + +//===----------------------------------------------------------------------===// +// mhlo.fft +//===----------------------------------------------------------------------===// + +// CHECK-LABEL: rfft_2d +func.func @rfft_2d(%arg0: tensor<1x512xf32>) -> tensor<1x257xcomplex> { + %0 = "mhlo.fft"(%arg0) <{fft_length = dense<512> : tensor<1xi64>, fft_type = #mhlo}> : (tensor<1x512xf32>) -> tensor<1x257xcomplex> + func.return %0 : tensor<1x257xcomplex> +} + +// CHECK: %0 = "mhlo.fft"(%arg0) <{fft_length = dense<[1, 512]> : tensor<2xi64>, fft_type = #mhlo}> : (tensor<1x512xf32>) -> tensor<1x257xcomplex> +// CHECK: return %0 : tensor<1x257xcomplex> + +// ----- + +// CHECK-LABEL: @fft +func.func @fft(%arg0: tensor<3x9xcomplex>) -> tensor<3x9xcomplex> { + %0 = "mhlo.fft"(%arg0) <{ fft_length = dense<9> : tensor<1xi64>, fft_type = #mhlo }> : (tensor<3x9xcomplex>) -> tensor<3x9xcomplex> + func.return %0 : tensor<3x9xcomplex> +} + +// CHECK: %0 = "mhlo.fft"(%arg0) <{fft_length = dense<9> : tensor<1xi64>, fft_type = #mhlo}> : (tensor<3x9xcomplex>) -> tensor<3x9xcomplex> +// CHECK: return %0 : tensor<3x9xcomplex> + +// ----- + +// CHECK-LABEL: @mhlo_nd_fft +func.func @mhlo_nd_fft(%arg0: tensor<2x3x345x256xf32>) -> tensor<2x3x345x129xcomplex> { + %43 = "mhlo.fft"(%arg0) <{fft_length = dense<256> : tensor<1xi64>, fft_type = #mhlo}> : (tensor<2x3x345x256xf32>) -> tensor<2x3x345x129xcomplex> + return %43 : tensor<2x3x345x129xcomplex> +} + +// CHECK: %0 = mhlo.reshape %arg0 : (tensor<2x3x345x256xf32>) -> tensor<2x3x345x1x256xf32> +// CHECK: %1 = "mhlo.fft"(%0) <{fft_length = dense<[1, 256]> : tensor<2xi64>, fft_type = #mhlo}> : (tensor<2x3x345x1x256xf32>) -> tensor<2x3x345x1x129xcomplex> +// CHECK: %2 = mhlo.reshape %1 : (tensor<2x3x345x1x129xcomplex>) -> tensor<2x3x345x129xcomplex> +// CHECK: return %2 : tensor<2x3x345x129xcomplex> + +// ----- + +// CHECK-LABEL: @mhlo_dynamic_fft_1 +func.func @mhlo_dynamic_fft_1(%arg0: tensor) -> tensor> { + %0 = "mhlo.fft"(%arg0) <{fft_length = dense<2560> : tensor<1xi64>, fft_type = #mhlo}> : (tensor) -> tensor> + return %0 : tensor> + // CHECK: %4 = "mhlo.get_dimension_size"(%arg0) <{dimension = 0 : i64}> : (tensor) -> tensor + // CHECK: %5 = mhlo.reshape %4 : (tensor) -> tensor<1xi32> + // CHECK: %6 = "mhlo.concatenate"(%5, %3, %2, %1) <{dimension = 0 : i64}> : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<4xi32> + // CHECK: %7 = mhlo.dynamic_reshape %arg0, %6 : (tensor, tensor<4xi32>) -> tensor + // CHECK: %8 = "mhlo.fft"(%7) <{fft_length = dense<[1, 2560]> : tensor<2xi64>, fft_type = #mhlo}> : (tensor) -> tensor> + // CHECK: %9 = "mhlo.get_dimension_size"(%8) <{dimension = 0 : i64}> : (tensor>) -> tensor + // CHECK: %10 = mhlo.reshape %9 : (tensor) -> tensor<1xi32> + // CHECK: %11 = "mhlo.concatenate"(%10, %3, %0) <{dimension = 0 : i64}> : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<3xi32> + // CHECK: %12 = mhlo.dynamic_reshape %8, %11 : (tensor>, tensor<3xi32>) -> tensor> + // CHECK: return %12 : tensor> +} + +// ----- + +// CHECK-LABEL: @mhlo_dynamic_fft_2 +func.func @mhlo_dynamic_fft_2(%arg0: tensor) -> tensor> { + %0 = "mhlo.fft"(%arg0) <{fft_length = dense<2560> : tensor<1xi64>, fft_type = #mhlo}> : (tensor) -> tensor> + return %0 : tensor> + // CHECK: %3 = "mhlo.get_dimension_size"(%arg0) <{dimension = 0 : i64}> : (tensor) -> tensor + // CHECK: %4 = mhlo.reshape %3 : (tensor) -> tensor<1xi32> + // CHECK: %5 = "mhlo.get_dimension_size"(%arg0) <{dimension = 1 : i64}> : (tensor) -> tensor + // CHECK: %6 = mhlo.reshape %5 : (tensor) -> tensor<1xi32> + // CHECK: %7 = "mhlo.concatenate"(%4, %6, %2, %1) <{dimension = 0 : i64}> : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<4xi32> + // CHECK: %8 = mhlo.dynamic_reshape %arg0, %7 : (tensor, tensor<4xi32>) -> tensor + // CHECK: %9 = "mhlo.fft"(%8) <{fft_length = dense<[1, 2560]> : tensor<2xi64>, fft_type = #mhlo}> : (tensor) -> tensor> + // CHECK: %10 = "mhlo.get_dimension_size"(%9) <{dimension = 0 : i64}> : (tensor>) -> tensor + // CHECK: %11 = mhlo.reshape %10 : (tensor) -> tensor<1xi32> + // CHECK: %12 = "mhlo.get_dimension_size"(%9) <{dimension = 1 : i64}> : (tensor>) -> tensor + // CHECK: %13 = mhlo.reshape %12 : (tensor) -> tensor<1xi32> + // CHECK: %14 = "mhlo.concatenate"(%11, %13, %0) <{dimension = 0 : i64}> : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<3xi32> + // CHECK: %15 = mhlo.dynamic_reshape %9, %14 : (tensor>, tensor<3xi32>) -> tensor> + // CHECK: return %15 : tensor> +} + +// ----- + +// CHECK-LABEL: @mhlo_dynamic_fft_2_neg +func.func @mhlo_dynamic_fft_2_neg(%arg0: tensor) -> tensor> { + %0 = "mhlo.fft"(%arg0) <{fft_length = dense<2560> : tensor<1xi64>, fft_type = #mhlo}> : (tensor) -> tensor> + return %0 : tensor> + // CHECK: %0 = "mhlo.fft"(%arg0) <{fft_length = dense<2560> : tensor<1xi64>, fft_type = #mhlo}> : (tensor) -> tensor> + // CHECK: return %0 : tensor> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/stablehlo-custom-call-legalize-composite.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/stablehlo-custom-call-legalize-composite.mlir new file mode 100644 index 0000000000000000000000000000000000000000..0c1242a3eccd702439bd7faf3fd76ba0d1e79de6 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/stablehlo-custom-call-legalize-composite.mlir @@ -0,0 +1,18 @@ +// RUN: odml-to-stablehlo-opt %s -stablehlo-custom-call-legalize-composite | FileCheck %s + +// CHECK-LABEL: module +module { + // CHECK-LABEL: @main + func.func @main(%arg0: tensor<1xf32>, %arg1: tensor<2xf32>) { + // CHECK: stablehlo.custom_call @foo + stablehlo.custom_call @foo() : () -> () + // CHECK-NOT: stablehlo.custom_call + // CHECK: stablehlo.composite "odml.foo" %arg0, %arg1 {composite_attributes = {bar = 500 : i64}, decomposition = @foo.impl} : (tensor<1xf32>, tensor<2xf32>) -> (tensor<2xf32>, tensor<1xf32>) + %1:2 = stablehlo.custom_call @stablehlo.composite(%arg0, %arg1) {called_computations = [@foo.impl], composite.backend_config = {attributes = {bar = 500 : i64}, name = "odml.foo"}} : (tensor<1xf32>, tensor<2xf32>) -> (tensor<2xf32>, tensor<1xf32>) + return + } + // CHECK-LABEL: func private @foo.impl + func.func private @foo.impl(%arg0: tensor<1xf32>, %arg1: tensor<2xf32>) -> (tensor<2xf32>, tensor<1xf32>) { + return %arg1, %arg0 : tensor<2xf32>, tensor<1xf32> + } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/stablehlo_fuse_convolution.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/stablehlo_fuse_convolution.mlir new file mode 100644 index 0000000000000000000000000000000000000000..64c715efd81a9035973139c20a1b48bc7c9df580 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/stablehlo_fuse_convolution.mlir @@ -0,0 +1,45 @@ +// RUN: odml-to-stablehlo-opt %s -stablehlo-fuse-convolution -cse | FileCheck %s + +// CHECK-LABEL: @fuseMulAndConv2D +// CHECK-SAME: %[[INPUT:[^:[:space:]]+]] +func.func @fuseMulAndConv2D(%input: tensor<1x256x256x3xf32>) -> (tensor<1x256x256x2xf32>) { + // CHECK-DAG: %[[FILTER:.+]] = stablehlo.constant dense<{{\[\[\[\[}}1.000000e+00, 2.000000e+00], [3.000000e+00, 4.000000e+00], [5.000000e+00, 6.000000e+00]]]]> : tensor<1x1x3x2xf32> + // CHECK-DAG: %[[CST:.+]] = stablehlo.constant dense<[1.000000e-01, 2.000000e-01]> : tensor<2xf32> + // CHECK-DAG: %[[CST_BCAST:.+]] = stablehlo.broadcast_in_dim %[[CST]], dims = [3] : (tensor<2xf32>) -> tensor<1x1x3x2xf32> + // CHECK-DAG: %[[NEW_FILTER:.+]] = stablehlo.multiply %[[FILTER]], %[[CST_BCAST]] : tensor<1x1x3x2xf32> + // CHECK-DAG: %[[RESULT:.+]] = stablehlo.convolution(%[[INPUT]], %[[NEW_FILTER]]) dim_numbers = [b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f], window = {stride = [1, 1], pad = {{\[\[}}0, 0], [0, 0]], rhs_dilate = [1, 1]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64} : (tensor<1x256x256x3xf32>, tensor<1x1x3x2xf32>) -> tensor<1x256x256x2xf32> + %filter = stablehlo.constant dense<[[[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]]]> : tensor<1x1x3x2xf32> + %cst = stablehlo.constant dense<[0.1, 0.2]> : tensor<2xf32> + %0 = stablehlo.convolution(%input, %filter) dim_numbers = [b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f], window = {stride = [1, 1], pad = [[0, 0], [0, 0]], rhs_dilate = [1, 1]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64} : (tensor<1x256x256x3xf32>, tensor<1x1x3x2xf32>) -> tensor<1x256x256x2xf32> + %1 = stablehlo.broadcast_in_dim %cst, dims = [3] : (tensor<2xf32>) -> tensor<1x256x256x2xf32> + %2 = stablehlo.multiply %0, %1 : tensor<1x256x256x2xf32> + // CHECK-DAG: return %[[RESULT]] + func.return %2 : tensor<1x256x256x2xf32> +} + +// ----- + +// CHECK-LABEL: @fuseMulAndConv2DDynamic +// CHECK-SAME: %[[INPUT:[^:[:space:]]+]] +func.func @fuseMulAndConv2DDynamic(%input: tensor) -> (tensor) { + // CHECK-DAG: %[[FILTER:.+]] = stablehlo.constant dense<{{\[\[\[\[}}1.000000e+00, 2.000000e+00], [3.000000e+00, 4.000000e+00], [5.000000e+00, 6.000000e+00]]]]> : tensor<1x1x3x2xf32> + // CHECK-DAG: %[[CST_0:.+]] = stablehlo.constant dense<[1.000000e-01, 2.000000e-01]> : tensor<2xf32> + // CHECK-DAG: %[[CST_1:.+]] = stablehlo.constant dense<[3.000000e-01, 4.000000e-01]> : tensor<2xf32> + // CHECK: %[[CST_BCAST:.+]] = stablehlo.broadcast_in_dim %[[CST_0]], dims = [3] : (tensor<2xf32>) -> tensor<1x1x3x2xf32> + // CHECK: %[[NEW_FILTER:.+]] = stablehlo.multiply %[[FILTER]], %[[CST_BCAST]] : tensor<1x1x3x2xf32> + // CHECK: %[[CONV:.+]] = stablehlo.convolution(%[[INPUT]], %[[NEW_FILTER]]) dim_numbers = [b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f], window = {stride = [1, 1], pad = {{\[\[}}0, 0], [0, 0]], rhs_dilate = [1, 1]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64} : (tensor, tensor<1x1x3x2xf32>) -> tensor + // CHECK: %[[SHAPE:.+]] = shape.shape_of %[[CONV]] : tensor -> tensor<4xindex> + // CHECK: %[[DYNAMIC_BCAST:.+]] = stablehlo.dynamic_broadcast_in_dim %[[CST_1]], %[[SHAPE]], dims = [3] : (tensor<2xf32>, tensor<4xindex>) -> tensor + // CHECK: %[[ADD:.+]] = stablehlo.add %[[CONV]], %[[DYNAMIC_BCAST]] : tensor + %filter = stablehlo.constant dense<[[[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]]]> : tensor<1x1x3x2xf32> + %cst_0 = stablehlo.constant dense<[0.1, 0.2]> : tensor<2xf32> + %cst_1 = stablehlo.constant dense<[0.3, 0.4]> : tensor<2xf32> + %0 = stablehlo.convolution(%input, %filter) dim_numbers = [b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f], window = {stride = [1, 1], pad = [[0, 0], [0, 0]], rhs_dilate = [1, 1]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64} : (tensor, tensor<1x1x3x2xf32>) -> tensor + %1 = shape.shape_of %0 : tensor -> tensor<4xindex> + %2 = stablehlo.dynamic_broadcast_in_dim %cst_0, %1, dims = [3] : (tensor<2xf32>, tensor<4xindex>) -> tensor + %3 = stablehlo.multiply %0, %2 : tensor + %4 = stablehlo.dynamic_broadcast_in_dim %cst_1, %1, dims = [3] : (tensor<2xf32>, tensor<4xindex>) -> tensor + %5 = stablehlo.add %3, %4 : tensor + // CHECK-DAG: return %[[ADD]] + func.return %5 : tensor +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/stablehlo_unfuse_batch_norm.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/stablehlo_unfuse_batch_norm.mlir new file mode 100644 index 0000000000000000000000000000000000000000..e19692b534f6c2161ddc229ecf0ea9338fc99a5e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/stablehlo_unfuse_batch_norm.mlir @@ -0,0 +1,172 @@ +// RUN: odml-to-stablehlo-opt %s -stablehlo-unfuse-batch-norm -cse -verify-diagnostics | FileCheck %s + +// CHECK-LABEL: @batchNormInference_2D_inner_features +// CHECK-SAME: %[[X:[^:[:space:]]+]] +// CHECK-SAME: %[[SCALE:[^:[:space:]]+]] +// CHECK-SAME: %[[OFFSET:[^:[:space:]]+]] +// CHECK-SAME: %[[MEAN:[^:[:space:]]+]] +// CHECK-SAME: %[[VARIANCE:[^:[:space:]]+]] +func.func @batchNormInference_2D_inner_features( + %x: tensor<4x256xf32>, %scale: tensor<256xf32>, %offset: tensor<256xf32>, + %mean: tensor<256xf32>, %variance: tensor<256xf32>) + -> (tensor<4x256xf32>) { + // CHECK: %[[CST:.+]] = stablehlo.constant dense<1.001000e-05> : tensor + // CHECK-NEXT: %[[EPS_BCAST:.+]] = stablehlo.broadcast_in_dim %[[CST]], dims = [] : (tensor) -> tensor<256xf32> + // CHECK-DAG: %[[VARIANCE_EPS:.+]] = stablehlo.add %[[VARIANCE]], %[[EPS_BCAST]] : tensor<256xf32> + // CHECK-DAG: %[[VARIANCE_EPS_RSQRT:.+]] = stablehlo.rsqrt %[[VARIANCE_EPS]] : tensor<256xf32> + // CHECK-DAG: %[[MULTIPLIER:.+]] = stablehlo.multiply %[[VARIANCE_EPS_RSQRT]], %[[SCALE]] : tensor<256xf32> + // CHECK-DAG: %[[MUL_MEAN:.+]] = stablehlo.multiply %[[MULTIPLIER]], %[[MEAN]] : tensor<256xf32> + // CHECK-DAG: %[[RHS:.+]] = stablehlo.subtract %[[OFFSET]], %[[MUL_MEAN]] : tensor<256xf32> + // CHECK-DAG: %[[MULTIPLIER_BCAST:.+]] = stablehlo.broadcast_in_dim %[[MULTIPLIER]], dims = [1] : (tensor<256xf32>) -> tensor<4x256xf32> + // CHECK-DAG: %[[X_NORMED:.+]] = stablehlo.multiply %[[X]], %[[MULTIPLIER_BCAST]] : tensor<4x256xf32> + // CHECK-DAG: %[[RHS_BCAST:.+]] = stablehlo.broadcast_in_dim %[[RHS]], dims = [1] : (tensor<256xf32>) -> tensor<4x256xf32> + // CHECK-DAG: %[[RESULT:.+]] = stablehlo.add %[[X_NORMED]], %[[RHS_BCAST]] : tensor<4x256xf32> + %0 = "stablehlo.batch_norm_inference"(%x, %scale, %offset, %mean, %variance) + {epsilon = 1.001000e-05 : f32, feature_index = 1 : i64} : + (tensor<4x256xf32>, tensor<256xf32>, tensor<256xf32>, tensor<256xf32>, + tensor<256xf32>) -> tensor<4x256xf32> + // CHECK-DAG: return %[[RESULT]] + func.return %0 : tensor<4x256xf32> +} + +// ----- + +// CHECK-LABEL: @batchNormInference_4D_middle_features +// CHECK-SAME: %[[X:[^:[:space:]]+]] +// CHECK-SAME: %[[SCALE:[^:[:space:]]+]] +// CHECK-SAME: %[[OFFSET:[^:[:space:]]+]] +// CHECK-SAME: %[[MEAN:[^:[:space:]]+]] +// CHECK-SAME: %[[VARIANCE:[^:[:space:]]+]] +func.func @batchNormInference_4D_middle_features( + %x: tensor<3x4x256x6xf32>, %scale: tensor<256xf32>, %offset: tensor<256xf32>, + %mean: tensor<256xf32>, %variance: tensor<256xf32>) + -> (tensor<3x4x256x6xf32>) { + // CHECK: %[[CST:.+]] = stablehlo.constant dense<1.001000e-05> : tensor + // CHECK-NEXT: %[[EPS_BCAST:.+]] = stablehlo.broadcast_in_dim %[[CST]], dims = [] : (tensor) -> tensor<256xf32> + // CHECK-DAG: %[[VARIANCE_EPS:.+]] = stablehlo.add %[[VARIANCE]], %[[EPS_BCAST]] : tensor<256xf32> + // CHECK-DAG: %[[VARIANCE_EPS_RSQRT:.+]] = stablehlo.rsqrt %[[VARIANCE_EPS]] : tensor<256xf32> + // CHECK-DAG: %[[MULTIPLIER:.+]] = stablehlo.multiply %[[VARIANCE_EPS_RSQRT]], %[[SCALE]] : tensor<256xf32> + // CHECK-DAG: %[[MUL_MEAN:.+]] = stablehlo.multiply %[[MULTIPLIER]], %[[MEAN]] : tensor<256xf32> + // CHECK-DAG: %[[RHS:.+]] = stablehlo.subtract %[[OFFSET]], %[[MUL_MEAN]] : tensor<256xf32> + // CHECK-DAG: %[[MULTIPLIER_BCAST:.+]] = stablehlo.broadcast_in_dim %[[MULTIPLIER]], dims = [2] : (tensor<256xf32>) -> tensor<3x4x256x6xf32> + // CHECK-DAG: %[[RHS_BCAST:.+]] = stablehlo.broadcast_in_dim %[[RHS]], dims = [2] : (tensor<256xf32>) -> tensor<3x4x256x6xf32> + %0 = "stablehlo.batch_norm_inference"(%x, %scale, %offset, %mean, %variance) + {epsilon = 1.001000e-05 : f32, feature_index = 2 : i64} : + (tensor<3x4x256x6xf32>, tensor<256xf32>, tensor<256xf32>, tensor<256xf32>, + tensor<256xf32>) -> tensor<3x4x256x6xf32> + func.return %0 : tensor<3x4x256x6xf32> +} + +// ----- + +// CHECK-LABEL: @batchNormInference_dynamic_shape +// Validate that dynamic shapes are handled properly. +// CHECK-SAME: %[[X:[^:[:space:]]+]] +// CHECK-SAME: %[[SCALE:[^:[:space:]]+]] +// CHECK-SAME: %[[OFFSET:[^:[:space:]]+]] +// CHECK-SAME: %[[MEAN:[^:[:space:]]+]] +// CHECK-SAME: %[[VARIANCE:[^:[:space:]]+]] +func.func @batchNormInference_dynamic_shape( + %x: tensor, %scale: tensor, %offset: tensor, + %mean: tensor, %variance: tensor) + -> tensor { + // CHECK-DAG: %[[EPS:.+]] = stablehlo.constant dense<1.000000e-03> : tensor + // CHECK-DAG: %[[VAR_SHAPE:.+]] = shape.shape_of %[[VARIANCE]] : tensor -> tensor<1xindex> + // CHECK-DAG: %[[EPS_BCAST:.+]] = stablehlo.dynamic_broadcast_in_dim %[[EPS]], %[[VAR_SHAPE]], dims = [] : (tensor, tensor<1xindex>) -> tensor + // CHECK-DAG: %[[VARIANCE_EPS:.+]] = stablehlo.add %[[VARIANCE]], %[[EPS_BCAST]] : tensor + // CHECK-DAG: %[[R_STDDEV:.+]] = stablehlo.rsqrt %[[VARIANCE_EPS]] : tensor + // CHECK-DAG: %[[MULTIPLIER:.+]] = stablehlo.multiply %[[R_STDDEV]], %[[SCALE]] : tensor + // CHECK-DAG: %[[MUL_MEAN:.+]] = stablehlo.multiply %[[MULTIPLIER]], %[[MEAN]] : tensor + // CHECK-DAG: %[[RHS:.+]] = stablehlo.subtract %[[OFFSET]], %[[MUL_MEAN]] : tensor + // CHECK-DAG: %[[X_SHAPE:.+]] = shape.shape_of %[[X]] : tensor -> tensor<4xindex> + // CHECK-DAG: %[[MULTIPLIER_BCAST:.+]] = stablehlo.dynamic_broadcast_in_dim %[[MULTIPLIER]], %[[X_SHAPE]], dims = [1] : (tensor, tensor<4xindex>) -> tensor + // CHECK-DAG: %[[X_NORMED:.+]] = stablehlo.multiply %[[X]], %[[MULTIPLIER_BCAST]] : tensor + // CHECK-DAG: %[[RHS_BCAST:.+]] = stablehlo.dynamic_broadcast_in_dim %[[RHS]], %[[X_SHAPE]], dims = [1] : (tensor, tensor<4xindex>) -> tensor + // CHECK-DAG: %[[RESULT:.+]] = stablehlo.add %[[X_NORMED]], %[[RHS_BCAST]] : tensor + %0 = "stablehlo.batch_norm_inference"(%x, %scale, %offset, %mean, %variance) + {epsilon = 0.001 : f32, feature_index = 1 : i64} : + (tensor, tensor, tensor, tensor, + tensor) -> tensor + func.return %0 : tensor +} + +// ----- + +// CHECK-LABEL: @batchNormInference_f64 +// Validate that epsilon is properly promoted to f64 +// CHECK: %[[CST:.+]] = stablehlo.constant dense<1.000000e+00> : tensor +// CHECK-NEXT: [[EPS_BCAST:.+]] = stablehlo.broadcast_in_dim %[[CST]], dims = [] : (tensor) -> tensor<256xf64> +func.func @batchNormInference_f64( + %x: tensor<4x256xf64>, %scale: tensor<256xf64>, %offset: tensor<256xf64>, + %mean: tensor<256xf64>, %variance: tensor<256xf64>) + -> (tensor<4x256xf64>) { + %0 = "stablehlo.batch_norm_inference"(%x, %scale, %offset, %mean, %variance) + {epsilon = 1.0 : f32, feature_index = 1 : i64} : + (tensor<4x256xf64>, tensor<256xf64>, tensor<256xf64>, tensor<256xf64>, + tensor<256xf64>) -> tensor<4x256xf64> + func.return %0 : tensor<4x256xf64> +} + +// ----- + +// CHECK-LABEL: @batchNormInference_f16 +// Validate that epsilon is properly down to f16 +// CHECK: %[[EPS:.+]] = stablehlo.constant dense<1.000000e+00> : tensor +// CHECK-NEXT: %[[EPS_BCAST:.+]] = stablehlo.broadcast_in_dim %[[EPS]], dims = [] : (tensor) -> tensor<256xf16> +func.func @batchNormInference_f16( + %x: tensor<4x256xf16>, %scale: tensor<256xf16>, %offset: tensor<256xf16>, + %mean: tensor<256xf16>, %variance: tensor<256xf16>) + -> (tensor<4x256xf16>) { + %0 = "stablehlo.batch_norm_inference"(%x, %scale, %offset, %mean, %variance) + {epsilon = 1.0 : f32, feature_index = 1 : i64} : + (tensor<4x256xf16>, tensor<256xf16>, tensor<256xf16>, tensor<256xf16>, + tensor<256xf16>) -> tensor<4x256xf16> + func.return %0 : tensor<4x256xf16> +} + +// ----- + +// Validate that epsilon is overflow +func.func @batchNormInference_f16_overflow( + %x: tensor<4x256xf16>, %scale: tensor<256xf16>, %offset: tensor<256xf16>, + %mean: tensor<256xf16>, %variance: tensor<256xf16>) + -> (tensor<4x256xf16>) { + // expected-warning @+1 {{Could not convert batch_norm epsilon to target fp type: opStatus = 24}} + %0 = "stablehlo.batch_norm_inference"(%x, %scale, %offset, %mean, %variance) + {epsilon = 0.00000001 : f32, feature_index = 1 : i64} : + (tensor<4x256xf16>, tensor<256xf16>, tensor<256xf16>, tensor<256xf16>, + tensor<256xf16>) -> tensor<4x256xf16> + func.return %0 : tensor<4x256xf16> +} + +// ----- + +// CHECK-LABEL: @batchNormTraining_4D_middle_features +// CHECK-SAME: %[[X:[^:[:space:]]+]] +// CHECK-SAME: %[[SCALE:[^:[:space:]]+]] +// CHECK-SAME: %[[OFFSET:[^:[:space:]]+]] +func.func @batchNormTraining_4D_middle_features( + %x: tensor<3x4x256x6xf32>, %scale: tensor<256xf32>, %offset: tensor<256xf32>) + -> (tensor<3x4x256x6xf32>) { + // CHECK: %[[CST:.+]] = stablehlo.constant dense<1.000000e+00> : tensor + // CHECK-DAG: %[[CST_AXIS:.+]] = "tf.Const"() <{value = dense<[0, 1, 3]> : tensor<3xi32>}> : () -> tensor<3xi32> + // CHECK-DAG: %[[X_SHAPE:.+]] = shape.shape_of %[[X]] : tensor<3x4x256x6xf32> -> tensor<4xindex> + // CHECK-DAG: %[[MEAN:.+]] = "tf.Mean"(%arg0, %[[CST_AXIS]]) <{keep_dims = false}> : (tensor<3x4x256x6xf32>, tensor<3xi32>) -> tensor<256xf32> + // CHECK-DAG: %[[MEAN_BCAST:.+]] = stablehlo.dynamic_broadcast_in_dim %[[MEAN]], %[[X_SHAPE]], dims = [2] : (tensor<256xf32>, tensor<4xindex>) -> tensor<3x4x256x6xf32> + // CHECK-DAG: %[[SQ_DIFF:.+]] = "tf.SquaredDifference"(%arg0, %[[MEAN_BCAST]]) : (tensor<3x4x256x6xf32>, tensor<3x4x256x6xf32>) -> tensor<3x4x256x6xf32> + // CHECK-DAG: %[[VARIANCE:.+]] = "tf.Mean"(%[[SQ_DIFF]], %[[CST_AXIS]]) <{keep_dims = false}> : (tensor<3x4x256x6xf32>, tensor<3xi32>) -> tensor<256xf32> + // CHECK-DAG: %[[EPS:.+]] = stablehlo.broadcast_in_dim %[[CST]], dims = [] : (tensor) -> tensor<256xf32> + // CHECK-DAG: %[[VARIANCE_EPS:.+]] = stablehlo.add %[[VARIANCE]], %[[EPS]] : tensor<256xf32> + // CHECK-DAG: %[[VARIANCE_EPS_RSQRT:.+]] = stablehlo.rsqrt %[[VARIANCE_EPS]] : tensor<256xf32> + // CHECK-DAG: %[[MULTIPLIER:.+]] = stablehlo.multiply %[[VARIANCE_EPS_RSQRT]], %[[SCALE]] : tensor<256xf32> + // CHECK-DAG: %[[MUL_MEAN:.+]] = stablehlo.multiply %[[MULTIPLIER]], %[[MEAN]] : tensor<256xf32> + // CHECK-DAG: %[[RHS:.+]] = stablehlo.subtract %[[OFFSET]], %[[MUL_MEAN]] : tensor<256xf32> + // CHECK-DAG: %[[MULTIPLIER_BCAST:.+]] = stablehlo.broadcast_in_dim %[[MULTIPLIER]], dims = [2] : (tensor<256xf32>) -> tensor<3x4x256x6xf32> + // CHECK-DAG: %[[X_NORMED:.+]] = stablehlo.multiply %[[X]], %[[MULTIPLIER_BCAST]] : tensor<3x4x256x6xf32> + // CHECK-DAG: %[[RHS_BCAST:.+]] = stablehlo.broadcast_in_dim %[[RHS]], dims = [2] : (tensor<256xf32>) -> tensor<3x4x256x6xf32> + // CHECK-DAG: %[[RESULT:.+]] = stablehlo.add %[[X_NORMED]], %[[RHS_BCAST]] : tensor<3x4x256x6xf32> + %0:3 = "stablehlo.batch_norm_training"(%x, %scale, %offset) + {epsilon = 1.0 : f32, feature_index = 2 : i64} : + (tensor<3x4x256x6xf32>, tensor<256xf32>, tensor<256xf32>) -> (tensor<3x4x256x6xf32>, tensor<256xf32>, tensor<256xf32>) + func.return %0 : tensor<3x4x256x6xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tf-tfl-translate-serialize-stablehlo-clamp.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tf-tfl-translate-serialize-stablehlo-clamp.mlir new file mode 100644 index 0000000000000000000000000000000000000000..9d3eda5007a6110ae7b4ebf2760f2b514abab095 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tf-tfl-translate-serialize-stablehlo-clamp.mlir @@ -0,0 +1,8 @@ +//RUN: tf_tfl_translate --enable-stablehlo-conversion --input-mlir %s -o /tmp/temp.stablehlo; [ -f /tmp/temp.stablehlo ] + +module { +func.func @main(%arg0 : tensor<5xf32>, %arg1 : tensor, %arg2 : tensor) -> tensor<5xf32> { + %0 = "tf.ClipByValue"(%arg0, %arg1, %arg2) : (tensor<5xf32>, tensor, tensor) -> tensor<5xf32> + func.return %0 : tensor<5xf32> +} +} \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tf-tfl-translate-serialize-stablehlo-concat.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tf-tfl-translate-serialize-stablehlo-concat.mlir new file mode 100644 index 0000000000000000000000000000000000000000..30a4d4ffe8d1ff4b91cfe32fdb5392c13b909574 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tf-tfl-translate-serialize-stablehlo-concat.mlir @@ -0,0 +1,9 @@ +//RUN: tf_tfl_translate --enable-stablehlo-conversion --input-mlir %s -o /tmp/temp.stablehlo; [ -f /tmp/temp.stablehlo ] + +module { +func.func @main(%arg0: tensor<3x3xf32>, %arg1: tensor<3x3xf32>) -> tensor<6x3xf32> { + %axis = "tf.Const"() { value = dense<0> : tensor } : () -> tensor + %1 = "tf.ConcatV2"(%arg0, %arg1, %axis) : (tensor<3x3xf32>, tensor<3x3xf32>, tensor) -> tensor<6x3xf32> + func.return %1 : tensor<6x3xf32> +} +} \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tf-tfl-translate-serialize-stablehlo-conv.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tf-tfl-translate-serialize-stablehlo-conv.mlir new file mode 100644 index 0000000000000000000000000000000000000000..a7f55c25cece6560041ef28bfb502161b0f05209 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tf-tfl-translate-serialize-stablehlo-conv.mlir @@ -0,0 +1,8 @@ +//RUN: tf_tfl_translate --enable-stablehlo-conversion --input-mlir %s -o /tmp/temp.stablehlo; [ -f /tmp/temp.stablehlo ] + +module { +func.func @main(%arg0: tensor<4x68x68x3xf32>, %arg1: tensor<5x5x3x8xf32>) -> tensor<4x64x64x8xf32> { + %0 = "tf.Conv2D"(%arg0, %arg1) {padding = "VALID", strides = [1, 1, 1, 1]} : (tensor<4x68x68x3xf32>, tensor<5x5x3x8xf32>) -> tensor<4x64x64x8xf32> + func.return %0 : tensor<4x64x64x8xf32> +} +} \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tf-tfl-translate-serialize-stablehlo-division.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tf-tfl-translate-serialize-stablehlo-division.mlir new file mode 100644 index 0000000000000000000000000000000000000000..f270c7a31d67a49ebf2104fa9901fddb24a9f066 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tf-tfl-translate-serialize-stablehlo-division.mlir @@ -0,0 +1,9 @@ +//RUN: tf_tfl_translate --enable-stablehlo-conversion --input-mlir %s -o /tmp/temp.stablehlo; [ -f /tmp/temp.stablehlo ] + + +module { +func.func @main(%arg0: tensor<2xi32>) -> tensor<2xi32> { + %0 = "tf.Div"(%arg0, %arg0) : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi32> + func.return %0 : tensor<2xi32> +} +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tf-tfl-translate-serialize-stablehlo-logistic.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tf-tfl-translate-serialize-stablehlo-logistic.mlir new file mode 100644 index 0000000000000000000000000000000000000000..0ba5de35d7d5383cff13eaadb57f169a9d6fbe49 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tf-tfl-translate-serialize-stablehlo-logistic.mlir @@ -0,0 +1,9 @@ +//RUN: tf_tfl_translate --enable-stablehlo-conversion --input-mlir %s -o /tmp/temp.stablehlo; [ -f /tmp/temp.stablehlo ] + + +module { +func.func @main(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "tf.Sigmoid"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tf-tfl-translate-serialize-stablehlo-multiply.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tf-tfl-translate-serialize-stablehlo-multiply.mlir new file mode 100644 index 0000000000000000000000000000000000000000..1f86c337bd28d834a9140cbab7974eeaffa61099 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tf-tfl-translate-serialize-stablehlo-multiply.mlir @@ -0,0 +1,9 @@ +//RUN: tf_tfl_translate --enable-stablehlo-conversion --input-mlir %s -o /tmp/temp.stablehlo; [ -f /tmp/temp.stablehlo ] + + +module { +func.func @main(%arg0: tensor<2xi32>) -> tensor<2xi32> { + %0 = "tf.Mul"(%arg0, %arg0) : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi32> + func.return %0 : tensor<2xi32> +} +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tf-tfl-translate-serialize-stablehlo-resize-bilinear.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tf-tfl-translate-serialize-stablehlo-resize-bilinear.mlir new file mode 100644 index 0000000000000000000000000000000000000000..c769fa2ceb019ded0742b9e17e8b5877295959b2 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tf-tfl-translate-serialize-stablehlo-resize-bilinear.mlir @@ -0,0 +1,9 @@ +//RUN: tf_tfl_translate --enable-stablehlo-conversion --input-mlir %s -o /tmp/temp.stablehlo; [ -f /tmp/temp.stablehlo ] + + +module { +func.func @main(%arg0: tensor<21x32x32x128xf32>, %arg1: tensor<2xi32>) -> tensor<1x64x64x128xf32> { + %0 = "tf.ResizeBilinear"(%arg0, %arg1) {align_corners = false, device = "", half_pixel_centers = true} : (tensor<21x32x32x128xf32>, tensor<2xi32>) -> tensor<1x64x64x128xf32> + func.return %0 : tensor<1x64x64x128xf32> +} +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tf-tfl-translate-serialize-stablehlo.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tf-tfl-translate-serialize-stablehlo.mlir new file mode 100644 index 0000000000000000000000000000000000000000..b7617a1042327c6e862d0cbd00f13a3c11b3d293 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tf-tfl-translate-serialize-stablehlo.mlir @@ -0,0 +1,22 @@ +//RUN: tf_tfl_translate --enable-stablehlo-conversion --input-mlir %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s + + +module { +func.func @tfInplaceUpdate(%arg0: tensor<2x1x2xf32>) -> tensor<2x1x2xf32> { + %1 = arith.constant dense<1> : tensor<1xi32> + %2 = arith.constant dense<2.0> : tensor<1x1x2xf32> + %3 = "tf.InplaceUpdate"(%arg0, %1, %2) {device = ""} + : (tensor<2x1x2xf32>, tensor<1xi32>, tensor<1x1x2xf32>) -> tensor<2x1x2xf32> + func.return %3 : tensor<2x1x2xf32> +} +} + +//CHECK: module attributes +//CHECK-SAME: keep_stablehlo_constant = "true" +//CHECK-NEXT: func.func @main(%arg0: tensor<2x1x2xf32>) -> tensor<2x1x2xf32> attributes {tf.entry_function = {inputs = "arg0", outputs = "vhlo.dynamic_update_slice_v1"}} { +//CHECK-DAG: %[[c0:.+]] = stablehlo.constant dense<2.000000e+00> : tensor<1x1x2xf32> +//CHECK-DAG: %[[c1:.+]] = stablehlo.constant dense<1> : tensor +//CHECK-DAG: %[[c2:.+]] = stablehlo.constant dense<0> : tensor +//CHECK-NEXT: %[[c3:.+]] = stablehlo.dynamic_update_slice %arg0, %[[c0]], %[[c1]], %[[c2]], %[[c2]] : (tensor<2x1x2xf32>, tensor<1x1x2xf32>, tensor, tensor, tensor) -> tensor<2x1x2xf32> +//CHECK-NEXT: return %[[c3]] : tensor<2x1x2xf32> +//CHECK-NEXT: } \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tf-tfl-translate-tf-quantize.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tf-tfl-translate-tf-quantize.mlir new file mode 100644 index 0000000000000000000000000000000000000000..ec517a104e52100e36cab8548aa76b6d0ffdeefb --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tf-tfl-translate-tf-quantize.mlir @@ -0,0 +1,22 @@ +//RUN: tf_tfl_translate --post-training-quantization --enable-stablehlo-conversion --input-mlir --output-mlir %s -o - | FileCheck %s + + +module { +func.func @tfInplaceUpdate(%arg0: tensor<2x1x2xf32>) -> tensor<2x1x2xf32> { + %1 = arith.constant dense<1> : tensor<1xi32> + %2 = arith.constant dense<2.0> : tensor<1x1x2xf32> + %3 = "tf.InplaceUpdate"(%arg0, %1, %2) {device = ""} + : (tensor<2x1x2xf32>, tensor<1xi32>, tensor<1x1x2xf32>) -> tensor<2x1x2xf32> + func.return %3 : tensor<2x1x2xf32> +} +} + +//CHECK: module { +//CHECK-NEXT: func.func @main(%arg0: tensor<2x1x2xf32>) -> tensor<2x1x2xf32> { +//CHECK-DAG: %[[c0:.+]] = stablehlo.constant dense<2.000000e+00> : tensor<1x1x2xf32> +//CHECK-DAG: %[[c1:.+]] = stablehlo.constant dense<1> : tensor +//CHECK-DAG: %[[c2:.+]] = stablehlo.constant dense<0> : tensor +//CHECK-NEXT: %[[c3:.+]] = stablehlo.dynamic_update_slice %arg0, %[[c0]], %[[c1]], %[[c2]], %[[c2]] : (tensor<2x1x2xf32>, tensor<1x1x2xf32>, tensor, tensor, tensor) -> tensor<2x1x2xf32> +//CHECK-NEXT: return %[[c3:.+]] : tensor<2x1x2xf32> +//CHECK-NEXT: } +//CHECK-NEXT:} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tfl_legalize_chlo.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tfl_legalize_chlo.mlir new file mode 100644 index 0000000000000000000000000000000000000000..7b1b61045a2dc289076952d3cf75dc2c58b21115 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tfl_legalize_chlo.mlir @@ -0,0 +1,45 @@ +// RUN: odml-to-stablehlo-opt %s -tfl-legalize-chlo -split-input-file | FileCheck %s --dump-input=fail + +// Just assert that pass is properly registered. +func.func @main(%arg0: tensor) -> tensor { + return %arg0: tensor +} +// CHECK-LABEL: main + +// ----- + +func.func @geluWithCustomCallErf(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = stablehlo.constant dense<1.000000e+00> : tensor<2xf32> + %1 = stablehlo.constant dense<0.707106769> : tensor<2xf32> + %2 = stablehlo.constant dense<5.000000e-01> : tensor<2xf32> + %3 = stablehlo.multiply %arg0, %2 : tensor<2xf32> + %4 = stablehlo.multiply %arg0, %1 : tensor<2xf32> + %5 = stablehlo.custom_call @mhlo.erf(%4) {mhlo.attributes = {}, mhlo.version = 1 : i64} : (tensor<2xf32>) -> tensor<2xf32> + %6 = stablehlo.add %5, %0 : tensor<2xf32> + %7 = stablehlo.multiply %3, %6 : tensor<2xf32> + return %7 : tensor<2xf32> +} + +// CHECK-LABEL: geluWithCustomCallErf +// CHECK: "tfl.gelu" +// CHECK-NOT: stablehlo +// CHECK-NOT: chlo + +// ----- + +func.func @geluWithCHLOErf(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = stablehlo.constant dense<1.000000e+00> : tensor<2xf32> + %1 = stablehlo.constant dense<0.707106769> : tensor<2xf32> + %2 = stablehlo.constant dense<5.000000e-01> : tensor<2xf32> + %3 = stablehlo.multiply %arg0, %2 : tensor<2xf32> + %4 = stablehlo.multiply %arg0, %1 : tensor<2xf32> + %5 = chlo.erf %4 : tensor<2xf32> -> tensor<2xf32> + %6 = stablehlo.add %5, %0 : tensor<2xf32> + %7 = stablehlo.multiply %3, %6 : tensor<2xf32> + return %7 : tensor<2xf32> +} + +// CHECK-LABEL: geluWithCHLOErf +// CHECK: "tfl.gelu" +// CHECK-NOT: stablehlo +// CHECK-NOT: chlo diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tfl_legalize_hlo.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tfl_legalize_hlo.mlir new file mode 100644 index 0000000000000000000000000000000000000000..c1be46df3e5af24d2912ffd9951326eb95a2bc26 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tfl_legalize_hlo.mlir @@ -0,0 +1,3932 @@ +// COM: This file is there to check that the `tfl-legalize-hlo` pass exists in `odml-to-stablehlo-opt`. + +// RUN: odml-to-stablehlo-opt %s -tfl-legalize-hlo -split-input-file | FileCheck %s --dump-input=fail + +// CHECK-LABEL: main +func.func @main(%arg0: tensor<5x7xf32>) -> tensor<5x7xf32> { + func.return %arg0: tensor<5x7xf32> +} + +// CHECK: return %arg0 : tensor<5x7xf32> + +// ----- + +//===----------------------------------------------------------------------===// +// mhlo.transpose +//===----------------------------------------------------------------------===// + +// CHECK-LABEL: transpose_2d +func.func @transpose_2d(%arg0: tensor<2x3xf32>) -> tensor<3x2xf32> { + %0 = "mhlo.transpose"(%arg0) <{permutation = dense<[1, 0]> : tensor<2xi64>}> : (tensor<2x3xf32>) -> tensor<3x2xf32> + func.return %0 : tensor<3x2xf32> +} + +// CHECK-NEXT: %0 = "tfl.pseudo_const"() <{value = dense<[1, 0]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK-NEXT: %1 = "tfl.cast"(%0) : (tensor<2xi64>) -> tensor<2xi32> +// CHECK-NEXT: %2 = "tfl.transpose"(%arg0, %1) : (tensor<2x3xf32>, tensor<2xi32>) -> tensor<3x2xf32> +// CHECK-NEXT: return %2 : tensor<3x2xf32> + +// ----- + +// CHECK-LABEL: transpose_3d +func.func @transpose_3d(%arg0: tensor<1x2x3xf32>) -> tensor<3x2x1xf32> { + %0 = "mhlo.transpose"(%arg0) <{permutation = dense<[2, 1, 0]> : tensor<3xi64>}> : (tensor<1x2x3xf32>) -> tensor<3x2x1xf32> + func.return %0 : tensor<3x2x1xf32> +} + +// CHECK-NEXT: %0 = "tfl.pseudo_const"() <{value = dense<[2, 1, 0]> : tensor<3xi64>}> : () -> tensor<3xi64> +// CHECK-NEXT: %1 = "tfl.cast"(%0) : (tensor<3xi64>) -> tensor<3xi32> +// CHECK-NEXT: %2 = "tfl.transpose"(%arg0, %1) : (tensor<1x2x3xf32>, tensor<3xi32>) -> tensor<3x2x1xf32> +// CHECK-NEXT: return %2 : tensor<3x2x1xf32> + +// ----- + +// CHECK-LABEL: transpose_dynamic_2d +func.func @transpose_dynamic_2d(%arg0: tensor) -> tensor<4x?xf32> { + %0 = "mhlo.transpose"(%arg0) <{permutation = dense<[1, 0]> : tensor<2xi64>}> : (tensor) -> tensor<4x?xf32> + func.return %0 : tensor<4x?xf32> +} + +// CHECK-NEXT: %0 = "tfl.pseudo_const"() <{value = dense<[1, 0]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK-NEXT: %1 = "tfl.cast"(%0) : (tensor<2xi64>) -> tensor<2xi32> +// CHECK-NEXT: %2 = "tfl.transpose"(%arg0, %1) : (tensor, tensor<2xi32>) -> tensor<4x?xf32> +// CHECK-NEXT: return %2 : tensor<4x?xf32> + +// ----- + +//===----------------------------------------------------------------------===// +// mhlo.dot_general +//===----------------------------------------------------------------------===// + +// CHECK-LABEL: dot_general_transposed_contracting_dims_3d +func.func @dot_general_transposed_contracting_dims_3d(%arg0: tensor<2x8x256x1792xf32>, %arg1: tensor<1x2x128x8x256xf32>) -> (tensor<1x128x1792xf32>){ + %4 = "mhlo.dot_general"(%arg1, %arg0) + <{dot_dimension_numbers = #mhlo.dot}>: (tensor<1x2x128x8x256xf32>, + tensor<2x8x256x1792xf32>) -> tensor<1x128x1792xf32> + return %4 : tensor<1x128x1792xf32> + // CHECK %0 = "tfl.pseudo_const"() <{value = dense<[0, 2, 1, 3, 4]> : tensor<5xi64>}> : () -> tensor<5xi64> + // CHECK %1 = "tfl.cast"(%0) : (tensor<5xi64>) -> tensor<5xi32> + // CHECK %2 = "tfl.transpose"(%arg1, %1) : (tensor<1x2x128x8x256xf32>, tensor<5xi32>) -> tensor<1x128x2x8x256xf32> + // CHECK %cst = arith.constant dense<[128, 4096]> : tensor<2xi64> + // CHECK %3 = "tfl.cast"(%cst) : (tensor<2xi64>) -> tensor<2xi32> + // CHECK %4 = "tfl.reshape"(%2, %3) : (tensor<1x128x2x8x256xf32>, tensor<2xi32>) -> tensor<128x4096xf32> + // CHECK %cst_0 = arith.constant dense<[4096, 1792]> : tensor<2xi64> + // CHECK %5 = "tfl.cast"(%cst_0) : (tensor<2xi64>) -> tensor<2xi32> + // CHECK %6 = "tfl.reshape"(%arg0, %5) : (tensor<2x8x256x1792xf32>, tensor<2xi32>) -> tensor<4096x1792xf32> + // CHECK %7 = "tfl.batch_matmul"(%4, %6) <{adj_x = false, adj_y = false, asymmetric_quantize_inputs = false}> : (tensor<128x4096xf32>, tensor<4096x1792xf32>) -> tensor<128x1792xf32> + // CHECK %cst_1 = arith.constant dense<[1, 128, 1792]> : tensor<3xi64> + // CHECK %8 = "tfl.cast"(%cst_1) : (tensor<3xi64>) -> tensor<3xi32> + // CHECK %9 = "tfl.reshape"(%7, %8) : (tensor<128x1792xf32>, tensor<3xi32>) -> tensor<1x128x1792xf32> + // CHECK return %9 : tensor<1x128x1792xf32> +} + +// ----- + +// CHECK-LABEL: dot_general_transposed_contracting_dims +func.func @dot_general_transposed_contracting_dims(%arg0: tensor<64x8x8xf32>, %arg1: tensor<1x10x8x8xf32>) -> (tensor<1x10x64xf32>){ + %4 = "mhlo.dot_general"(%arg1, %arg0) + <{dot_dimension_numbers = #mhlo.dot}> + : (tensor<1x10x8x8xf32>, tensor<64x8x8xf32>) -> tensor<1x10x64xf32> + return %4 : tensor<1x10x64xf32> + // CHECK %0 = "tfl.pseudo_const"() <{value = dense<[1, 2, 0]> : tensor<3xi64>}> : () -> tensor<3xi64> + // CHECK %1 = "tfl.cast"(%0) : (tensor<3xi64>) -> tensor<3xi32> + // CHECK %2 = "tfl.transpose"(%arg0, %1) : (tensor<64x8x8xf32>, tensor<3xi32>) -> tensor<8x8x64xf32> + // CHECK %cst = arith.constant dense<[10, 64]> : tensor<2xi64> + // CHECK %3 = "tfl.cast"(%cst) : (tensor<2xi64>) -> tensor<2xi32> + // CHECK %4 = "tfl.reshape"(%arg1, %3) : (tensor<1x10x8x8xf32>, tensor<2xi32>) -> tensor<10x64xf32> + // CHECK %cst_0 = arith.constant dense<64> : tensor<2xi64> + // CHECK %5 = "tfl.cast"(%cst_0) : (tensor<2xi64>) -> tensor<2xi32> + // CHECK %6 = "tfl.reshape"(%2, %5) : (tensor<8x8x64xf32>, tensor<2xi32>) -> tensor<64x64xf32> + // CHECK %7 = "tfl.batch_matmul"(%4, %6) <{adj_x = false, adj_y = false, asymmetric_quantize_inputs = false}> : (tensor<10x64xf32>, tensor<64x64xf32>) -> tensor<10x64xf32> + // CHECK %cst_1 = arith.constant dense<[1, 10, 64]> : tensor<3xi64> + // CHECK %8 = "tfl.cast"(%cst_1) : (tensor<3xi64>) -> tensor<3xi32> + // CHECK %9 = "tfl.reshape"(%7, %8) : (tensor<10x64xf32>, tensor<3xi32>) -> tensor<1x10x64xf32> + // CHECK return %9 : tensor<1x10x64xf32> +} + +// ----- + +// CHECK-LABEL: dot_general_non_transposed_contracting_dims +func.func @dot_general_non_transposed_contracting_dims(%arg0: tensor<8x256x1792xf32>, %arg1: tensor<1x128x8x256xf32>) -> (tensor<1x128x1792xf32>){ + %4 = "mhlo.dot_general"(%arg1, %arg0) + <{dot_dimension_numbers = #mhlo.dot}> + : (tensor<1x128x8x256xf32>, tensor<8x256x1792xf32>) -> tensor<1x128x1792xf32> + return %4 : tensor<1x128x1792xf32> + // CHECK %cst = arith.constant dense<[128, 2048]> : tensor<2xi64> + // CHECK %0 = "tfl.cast"(%cst) : (tensor<2xi64>) -> tensor<2xi32> + // CHECK %1 = "tfl.reshape"(%arg1, %0) : (tensor<1x128x8x256xf32>, tensor<2xi32>) -> tensor<128x2048xf32> + // CHECK %cst_0 = arith.constant dense<[2048, 1792]> : tensor<2xi64> + // CHECK %2 = "tfl.cast"(%cst_0) : (tensor<2xi64>) -> tensor<2xi32> + // CHECK %3 = "tfl.reshape"(%arg0, %2) : (tensor<8x256x1792xf32>, tensor<2xi32>) -> tensor<2048x1792xf32> + // CHECK %4 = "tfl.batch_matmul"(%1, %3) <{adj_x = false, adj_y = false, asymmetric_quantize_inputs = false}> : (tensor<128x2048xf32>, tensor<2048x1792xf32>) -> tensor<128x1792xf32> + // CHECK %cst_1 = arith.constant dense<[1, 128, 1792]> : tensor<3xi64> + // CHECK %5 = "tfl.cast"(%cst_1) : (tensor<3xi64>) -> tensor<3xi32> + // CHECK %6 = "tfl.reshape"(%4, %5) : (tensor<128x1792xf32>, tensor<3xi32>) -> tensor<1x128x1792xf32> + // CHECK return %6 : tensor<1x128x1792xf32> +} + +// ----- + +// CHECK-LABEL: dot_general +func.func @dot_general(%arg0: tensor<3x2x6x5x1xf32>, %arg1: tensor<3x2x4x6xf32>) -> tensor<3x5x1x4xf32> { + %0 = "mhlo.dot_general"(%arg0, %arg1) { + dot_dimension_numbers = #mhlo.dot< + lhs_batching_dimensions = [0], + lhs_contracting_dimensions = [1, 2], + rhs_batching_dimensions = [0], + rhs_contracting_dimensions = [1, 3] + >, + precision_config = [#mhlo, #mhlo] + } : (tensor<3x2x6x5x1xf32>, tensor<3x2x4x6xf32>) -> tensor<3x5x1x4xf32> + func.return %0 : tensor<3x5x1x4xf32> +} + +// CHECK: %[[TRANSPOSED_0:.*]] = "tfl.transpose" +// CHECK: %[[TRANSPOSED_1:.*]] = "tfl.transpose" +// CHECK: %[[RESHAPED_0:.*]] = "tfl.reshape"(%[[TRANSPOSED_0]] +// CHECK: %[[RESHAPED_1:.*]] = "tfl.reshape"(%[[TRANSPOSED_1]] +// CHECK: %[[BMM_0:.*]] = "tfl.batch_matmul"(%[[RESHAPED_0]], %[[RESHAPED_1]]) <{adj_x = false, adj_y = false, asymmetric_quantize_inputs = false}> : (tensor<3x5x12xf32>, tensor<3x12x4xf32>) -> tensor<3x5x4xf32> +// CHECK: %[[RESHAPED_BMM:.*]] = "tfl.reshape"(%[[BMM_0]] +// CHECK: return %[[RESHAPED_BMM]] : tensor<3x5x1x4xf32> + +// ----- + +// CHECK-LABEL: dot_general_repeated +func.func @dot_general_repeated(%arg0: tensor<1x1x1024xf32>, %arg1: tensor<1024x1024xf32>) -> tensor<1x1x1024xf32> { + %0 = "mhlo.dot_general"(%arg0, %arg1) { + dot_dimension_numbers = #mhlo.dot< + lhs_batching_dimensions = [], + lhs_contracting_dimensions = [2], + rhs_batching_dimensions = [], + rhs_contracting_dimensions = [0] + >, + precision_config = [#mhlo, #mhlo] + } : (tensor<1x1x1024xf32>, tensor<1024x1024xf32>) -> tensor<1x1x1024xf32> + func.return %0 : tensor<1x1x1024xf32> +} + +// CHECK: %[[RESHAPED_0:.*]] = "tfl.reshape"(%arg0 +// CHECK: %[[BMM_0:.*]] = "tfl.batch_matmul"(%[[RESHAPED_0]], %arg1) <{adj_x = false, adj_y = false, asymmetric_quantize_inputs = false}> : {{.*}} -> tensor<1x1024xf32> +// CHECK: %[[RESHAPED_BMM:.*]] = "tfl.reshape"(%[[BMM_0]] +// CHECK: return %[[RESHAPED_BMM]] : tensor<1x1x1024xf32> + +// ----- + +// CHECK-LABEL: dot_general_int8 +func.func @dot_general_int8(%arg0: tensor<256xi8>, %arg1: tensor<256x8xi8>) -> tensor<8xi32> { + %0 = "mhlo.dot_general"(%arg0, %arg1) { + dot_dimension_numbers = #mhlo.dot< + lhs_contracting_dimensions = [0], + rhs_contracting_dimensions = [0]>, + precision_config = [#mhlo, #mhlo] + } : (tensor<256xi8>, tensor<256x8xi8>) -> tensor<8xi32> + func.return %0 : tensor<8xi32> +} + +// CHECK: %[[RESHAPED_0:.*]] = "tfl.reshape"(%arg0 +// CHECK: %[[BMM_0:.*]] = "tfl.batch_matmul"(%[[RESHAPED_0]], %arg1) <{adj_x = false, adj_y = false, asymmetric_quantize_inputs = false}> : {{.*}} -> tensor<1x8xi32> +// CHECK: %[[RESHAPED_BMM:.*]] = "tfl.reshape"(%[[BMM_0]] +// CHECK: return %[[RESHAPED_BMM]] : tensor<8xi32> + +// ----- + +// CHECK-LABEL: dot_general_dynamic_rhs_out_dim +func.func @dot_general_dynamic_rhs_out_dim(%arg0: tensor<4x4x256xf32>, %arg1: tensor<4x?x256xf32>) -> tensor<4x4x?xf32> { + %0 = "mhlo.dot_general"(%arg0, %arg1) { + dot_dimension_numbers = #mhlo.dot< + lhs_batching_dimensions = [0], + rhs_batching_dimensions = [0], + lhs_contracting_dimensions = [2], + rhs_contracting_dimensions = [2] + >} : (tensor<4x4x256xf32>, tensor<4x?x256xf32>) -> tensor<4x4x?xf32> + func.return %0 : tensor<4x4x?xf32> +} + +// CHECK: %0 = "tfl.pseudo_const"() <{value = dense<[0, 2, 1]> : tensor<3xi64>}> : () -> tensor<3xi64> +// CHECK: %1 = "tfl.cast"(%0) : (tensor<3xi64>) -> tensor<3xi32> +// CHECK: %2 = "tfl.transpose"(%arg1, %1) : (tensor<4x?x256xf32>, tensor<3xi32>) -> tensor<4x256x?xf32> +// CHECK: %3 = "tfl.shape"(%arg1) : (tensor<4x?x256xf32>) -> tensor<3xi32> +// CHECK-DAG: %4 = "tfl.pseudo_const"() <{value = dense<[-1, 0, -1]> : tensor<3xi32>}> : () -> tensor<3xi32> +// CHECK-DAG: %5 = "tfl.pseudo_const"() <{value = dense<[-1, -1, 0]> : tensor<3xi32>}> : () -> tensor<3xi32> +// CHECK-DAG: %6 = "tfl.pseudo_const"() <{value = dense<1> : tensor}> : () -> tensor +// CHECK: %7 = "tfl.unsorted_segment_prod"(%3, %4, %6) : (tensor<3xi32>, tensor<3xi32>, tensor) -> tensor<1xi32> +// CHECK: %8 = "tfl.unsorted_segment_prod"(%3, %5, %6) : (tensor<3xi32>, tensor<3xi32>, tensor) -> tensor<1xi32> +// CHECK: %9 = "tfl.pseudo_const"() <{value = dense<4> : tensor<1xi32>}> : () -> tensor<1xi32> +// CHECK: %10 = "tfl.concatenation"(%9, %8, %7) <{axis = 0 : i32, fused_activation_function = "NONE"}> : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<3xi32> +// CHECK: %11 = "tfl.cast"(%10) : (tensor<3xi32>) -> tensor<3xi32> +// CHECK: %12 = "tfl.reshape"(%2, %11) : (tensor<4x256x?xf32>, tensor<3xi32>) -> tensor<4x256x?xf32> +// CHECK: %13 = "tfl.batch_matmul"(%arg0, %12) <{adj_x = false, adj_y = false, asymmetric_quantize_inputs = false}> : (tensor<4x4x256xf32>, tensor<4x256x?xf32>) -> tensor<4x4x?xf32> +// CHECK: %14 = "tfl.shape"(%arg0) : (tensor<4x4x256xf32>) -> tensor<3xi32> +// CHECK: %15 = "tfl.shape"(%arg1) : (tensor<4x?x256xf32>) -> tensor<3xi32> +// CHECK: %16 = "tfl.pseudo_const"() <{value = dense<[0, 1]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK: %17 = "tfl.gather"(%14, %16) <{axis = 0 : i32, batch_dims = 0 : i32}> : (tensor<3xi32>, tensor<2xi64>) -> tensor<2xi32> +// CHECK: %18 = "tfl.pseudo_const"() <{value = dense<1> : tensor<1xi64>}> : () -> tensor<1xi64> +// CHECK: %19 = "tfl.gather"(%15, %18) <{axis = 0 : i32, batch_dims = 0 : i32}> : (tensor<3xi32>, tensor<1xi64>) -> tensor<1xi32> +// CHECK: %20 = "tfl.concatenation"(%17, %19) <{axis = 0 : i32, fused_activation_function = "NONE"}> : (tensor<2xi32>, tensor<1xi32>) -> tensor<3xi32> +// CHECK: %21 = "tfl.cast"(%20) : (tensor<3xi32>) -> tensor<3xi32> +// CHECK: %22 = "tfl.reshape"(%13, %21) : (tensor<4x4x?xf32>, tensor<3xi32>) -> tensor<4x4x?xf32> +// CHECK: return %22 : tensor<4x4x?xf32> + +// ----- + +// CHECK-LABEL: dot_general_dynamic_batch_dim +func.func @dot_general_dynamic_batch_dim(%arg0: tensor<2x?x2x3xf32>, %arg1: tensor<2x?x4x3xf32>) -> tensor<2x?x2x4xf32> { + %0 = "mhlo.dot_general"(%arg0, %arg1) { + dot_dimension_numbers = #mhlo.dot< + lhs_batching_dimensions = [0, 1], + rhs_batching_dimensions = [0, 1], + lhs_contracting_dimensions = [3], + rhs_contracting_dimensions = [3] + >} : (tensor<2x?x2x3xf32>, tensor<2x?x4x3xf32>) -> tensor<2x?x2x4xf32> + func.return %0 : tensor<2x?x2x4xf32> +} + +// CHECK: %0 = "tfl.pseudo_const"() <{value = dense<[0, 1, 3, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %1 = "tfl.cast"(%0) : (tensor<4xi64>) -> tensor<4xi32> +// CHECK: %2 = "tfl.transpose"(%arg1, %1) : (tensor<2x?x4x3xf32>, tensor<4xi32>) -> tensor<2x?x3x4xf32> +// CHECK: %3 = "tfl.shape"(%arg0) : (tensor<2x?x2x3xf32>) -> tensor<4xi32> +// CHECK-DAG: %4 = "tfl.pseudo_const"() <{value = dense<[-1, -1, 0, -1]> : tensor<4xi32>}> : () -> tensor<4xi32> +// CHECK-DAG: %5 = "tfl.pseudo_const"() <{value = dense<[-1, -1, -1, 0]> : tensor<4xi32>}> : () -> tensor<4xi32> +// CHECK-DAG: %6 = "tfl.pseudo_const"() <{value = dense<1> : tensor}> : () -> tensor +// CHECK: %7 = "tfl.unsorted_segment_prod"(%3, %4, %6) : (tensor<4xi32>, tensor<4xi32>, tensor) -> tensor<1xi32> +// CHECK: %8 = "tfl.unsorted_segment_prod"(%3, %5, %6) : (tensor<4xi32>, tensor<4xi32>, tensor) -> tensor<1xi32> +// CHECK: %9 = "tfl.pseudo_const"() <{value = dense<[0, 1]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK: %10 = "tfl.gather"(%3, %9) <{axis = 0 : i32, batch_dims = 0 : i32}> : (tensor<4xi32>, tensor<2xi64>) -> tensor<2xi32> +// CHECK: %11 = "tfl.concatenation"(%10, %7, %8) <{axis = 0 : i32, fused_activation_function = "NONE"}> : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<4xi32> +// CHECK: %12 = "tfl.cast"(%11) : (tensor<4xi32>) -> tensor<4xi32> +// CHECK: %13 = "tfl.reshape"(%arg0, %12) : (tensor<2x?x2x3xf32>, tensor<4xi32>) -> tensor<2x?x2x3xf32> +// CHECK: %14 = "tfl.shape"(%arg1) : (tensor<2x?x4x3xf32>) -> tensor<4xi32> +// CHECK-DAG: %15 = "tfl.pseudo_const"() <{value = dense<[-1, -1, 0, -1]> : tensor<4xi32>}> : () -> tensor<4xi32> +// CHECK-DAG: %16 = "tfl.pseudo_const"() <{value = dense<[-1, -1, -1, 0]> : tensor<4xi32>}> : () -> tensor<4xi32> +// CHECK-DAG: %17 = "tfl.pseudo_const"() <{value = dense<1> : tensor}> : () -> tensor +// CHECK: %18 = "tfl.unsorted_segment_prod"(%14, %15, %17) : (tensor<4xi32>, tensor<4xi32>, tensor) -> tensor<1xi32> +// CHECK: %19 = "tfl.unsorted_segment_prod"(%14, %16, %17) : (tensor<4xi32>, tensor<4xi32>, tensor) -> tensor<1xi32> +// CHECK: %20 = "tfl.pseudo_const"() <{value = dense<[0, 1]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK: %21 = "tfl.gather"(%14, %20) <{axis = 0 : i32, batch_dims = 0 : i32}> : (tensor<4xi32>, tensor<2xi64>) -> tensor<2xi32> +// CHECK: %22 = "tfl.concatenation"(%21, %19, %18) <{axis = 0 : i32, fused_activation_function = "NONE"}> : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<4xi32> +// CHECK: %23 = "tfl.cast"(%22) : (tensor<4xi32>) -> tensor<4xi32> +// CHECK: %24 = "tfl.reshape"(%2, %23) : (tensor<2x?x3x4xf32>, tensor<4xi32>) -> tensor<2x?x3x4xf32> +// CHECK: %25 = "tfl.batch_matmul"(%13, %24) <{adj_x = false, adj_y = false, asymmetric_quantize_inputs = false}> : (tensor<2x?x2x3xf32>, tensor<2x?x3x4xf32>) -> tensor<2x?x2x4xf32> +// CHECK: %26 = "tfl.shape"(%arg0) : (tensor<2x?x2x3xf32>) -> tensor<4xi32> +// CHECK: %27 = "tfl.shape"(%arg1) : (tensor<2x?x4x3xf32>) -> tensor<4xi32> +// CHECK: %28 = "tfl.pseudo_const"() <{value = dense<[0, 1, 2]> : tensor<3xi64>}> : () -> tensor<3xi64> +// CHECK: %29 = "tfl.gather"(%26, %28) <{axis = 0 : i32, batch_dims = 0 : i32}> : (tensor<4xi32>, tensor<3xi64>) -> tensor<3xi32> +// CHECK: %30 = "tfl.pseudo_const"() <{value = dense<2> : tensor<1xi64>}> : () -> tensor<1xi64> +// CHECK: %31 = "tfl.gather"(%27, %30) <{axis = 0 : i32, batch_dims = 0 : i32}> : (tensor<4xi32>, tensor<1xi64>) -> tensor<1xi32> +// CHECK: %32 = "tfl.concatenation"(%29, %31) <{axis = 0 : i32, fused_activation_function = "NONE"}> : (tensor<3xi32>, tensor<1xi32>) -> tensor<4xi32> +// CHECK: %33 = "tfl.cast"(%32) : (tensor<4xi32>) -> tensor<4xi32> +// CHECK: %34 = "tfl.reshape"(%25, %33) : (tensor<2x?x2x4xf32>, tensor<4xi32>) -> tensor<2x?x2x4xf32> +// CHECK: return %34 : tensor<2x?x2x4xf32> + +// ----- + +// CHECK-LABEL: dot_general_dynamic_lhs_rhs_out_dims +func.func @dot_general_dynamic_lhs_rhs_out_dims(%arg0: tensor<2x2x?x3xf32>, %arg1: tensor<2x4x?x3xf32>) -> tensor<2x2x?x4x?xf32> { + %0 = "mhlo.dot_general"(%arg0, %arg1) { + dot_dimension_numbers = #mhlo.dot< + lhs_batching_dimensions = [0], + rhs_batching_dimensions = [0], + lhs_contracting_dimensions = [3], + rhs_contracting_dimensions = [3] + >} : (tensor<2x2x?x3xf32>, tensor<2x4x?x3xf32>) -> tensor<2x2x?x4x?xf32> + func.return %0 : tensor<2x2x?x4x?xf32> +} + +// CHECK: %0 = "tfl.pseudo_const"() <{value = dense<[0, 3, 1, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %1 = "tfl.cast"(%0) : (tensor<4xi64>) -> tensor<4xi32> +// CHECK: %2 = "tfl.transpose"(%arg1, %1) : (tensor<2x4x?x3xf32>, tensor<4xi32>) -> tensor<2x3x4x?xf32> +// CHECK: %3 = "tfl.shape"(%arg0) : (tensor<2x2x?x3xf32>) -> tensor<4xi32> +// CHECK-DAG: %4 = "tfl.pseudo_const"() <{value = dense<[-1, 0, 0, -1]> : tensor<4xi32>}> : () -> tensor<4xi32> +// CHECK-DAG: %5 = "tfl.pseudo_const"() <{value = dense<[-1, -1, -1, 0]> : tensor<4xi32>}> : () -> tensor<4xi32> +// CHECK-DAG: %6 = "tfl.pseudo_const"() <{value = dense<1> : tensor}> : () -> tensor +// CHECK: %7 = "tfl.unsorted_segment_prod"(%3, %4, %6) : (tensor<4xi32>, tensor<4xi32>, tensor) -> tensor<1xi32> +// CHECK: %8 = "tfl.unsorted_segment_prod"(%3, %5, %6) : (tensor<4xi32>, tensor<4xi32>, tensor) -> tensor<1xi32> +// CHECK: %9 = "tfl.pseudo_const"() <{value = dense<2> : tensor<1xi32>}> : () -> tensor<1xi32> +// CHECK: %10 = "tfl.concatenation"(%9, %7, %8) <{axis = 0 : i32, fused_activation_function = "NONE"}> : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<3xi32> +// CHECK: %11 = "tfl.cast"(%10) : (tensor<3xi32>) -> tensor<3xi32> +// CHECK: %12 = "tfl.reshape"(%arg0, %11) : (tensor<2x2x?x3xf32>, tensor<3xi32>) -> tensor<2x?x3xf32> +// CHECK: %13 = "tfl.shape"(%arg1) : (tensor<2x4x?x3xf32>) -> tensor<4xi32> +// CHECK-DAG: %14 = "tfl.pseudo_const"() <{value = dense<[-1, 0, 0, -1]> : tensor<4xi32>}> : () -> tensor<4xi32> +// CHECK-DAG: %15 = "tfl.pseudo_const"() <{value = dense<[-1, -1, -1, 0]> : tensor<4xi32>}> : () -> tensor<4xi32> +// CHECK-DAG: %16 = "tfl.pseudo_const"() <{value = dense<1> : tensor}> : () -> tensor +// CHECK: %17 = "tfl.unsorted_segment_prod"(%13, %14, %16) : (tensor<4xi32>, tensor<4xi32>, tensor) -> tensor<1xi32> +// CHECK: %18 = "tfl.unsorted_segment_prod"(%13, %15, %16) : (tensor<4xi32>, tensor<4xi32>, tensor) -> tensor<1xi32> +// CHECK: %19 = "tfl.pseudo_const"() <{value = dense<2> : tensor<1xi32>}> : () -> tensor<1xi32> +// CHECK: %20 = "tfl.concatenation"(%19, %18, %17) <{axis = 0 : i32, fused_activation_function = "NONE"}> : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<3xi32> +// CHECK: %21 = "tfl.cast"(%20) : (tensor<3xi32>) -> tensor<3xi32> +// CHECK: %22 = "tfl.reshape"(%2, %21) : (tensor<2x3x4x?xf32>, tensor<3xi32>) -> tensor<2x3x?xf32> +// CHECK: %23 = "tfl.batch_matmul"(%12, %22) <{adj_x = false, adj_y = false, asymmetric_quantize_inputs = false}> : (tensor<2x?x3xf32>, tensor<2x3x?xf32>) -> tensor<2x?x?xf32> +// CHECK: %24 = "tfl.shape"(%arg0) : (tensor<2x2x?x3xf32>) -> tensor<4xi32> +// CHECK: %25 = "tfl.shape"(%arg1) : (tensor<2x4x?x3xf32>) -> tensor<4xi32> +// CHECK: %26 = "tfl.pseudo_const"() <{value = dense<[0, 1, 2]> : tensor<3xi64>}> : () -> tensor<3xi64> +// CHECK: %27 = "tfl.gather"(%24, %26) <{axis = 0 : i32, batch_dims = 0 : i32}> : (tensor<4xi32>, tensor<3xi64>) -> tensor<3xi32> +// CHECK: %28 = "tfl.pseudo_const"() <{value = dense<[1, 2]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK: %29 = "tfl.gather"(%25, %28) <{axis = 0 : i32, batch_dims = 0 : i32}> : (tensor<4xi32>, tensor<2xi64>) -> tensor<2xi32> +// CHECK: %30 = "tfl.concatenation"(%27, %29) <{axis = 0 : i32, fused_activation_function = "NONE"}> : (tensor<3xi32>, tensor<2xi32>) -> tensor<5xi32> +// CHECK: %31 = "tfl.cast"(%30) : (tensor<5xi32>) -> tensor<5xi32> +// CHECK: %32 = "tfl.reshape"(%23, %31) : (tensor<2x?x?xf32>, tensor<5xi32>) -> tensor<2x2x?x4x?xf32> +// CHECK: return %32 : tensor<2x2x?x4x?xf32 + +// ----- + +// CHECK-LABEL: dot_general_dynamic_contracting_dim +func.func @dot_general_dynamic_contracting_dim(%arg0: tensor<4x4x?xf32>, %arg1: tensor<4x?x256xf32>) -> tensor<4x4x256xf32> { + %0 = "mhlo.dot_general"(%arg0, %arg1) { + dot_dimension_numbers = #mhlo.dot< + lhs_batching_dimensions = [0], + rhs_batching_dimensions = [0], + lhs_contracting_dimensions = [2], + rhs_contracting_dimensions = [1] + >} : (tensor<4x4x?xf32>, tensor<4x?x256xf32>) -> tensor<4x4x256xf32> + func.return %0 : tensor<4x4x256xf32> +} + +// CHECK: %0 = "tfl.shape"(%arg0) : (tensor<4x4x?xf32>) -> tensor<3xi32> +// CHECK-DAG: %1 = "tfl.pseudo_const"() <{value = dense<[-1, 0, -1]> : tensor<3xi32>}> : () -> tensor<3xi32> +// CHECK-DAG: %2 = "tfl.pseudo_const"() <{value = dense<[-1, -1, 0]> : tensor<3xi32>}> : () -> tensor<3xi32> +// CHECK-DAG: %3 = "tfl.pseudo_const"() <{value = dense<1> : tensor}> : () -> tensor +// CHECK: %4 = "tfl.unsorted_segment_prod"(%0, %1, %3) : (tensor<3xi32>, tensor<3xi32>, tensor) -> tensor<1xi32> +// CHECK: %5 = "tfl.unsorted_segment_prod"(%0, %2, %3) : (tensor<3xi32>, tensor<3xi32>, tensor) -> tensor<1xi32> +// CHECK: %6 = "tfl.pseudo_const"() <{value = dense<4> : tensor<1xi32>}> : () -> tensor<1xi32> +// CHECK: %7 = "tfl.concatenation"(%6, %4, %5) <{axis = 0 : i32, fused_activation_function = "NONE"}> : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<3xi32> +// CHECK: %8 = "tfl.cast"(%7) : (tensor<3xi32>) -> tensor<3xi32> +// CHECK: %9 = "tfl.reshape"(%arg0, %8) : (tensor<4x4x?xf32>, tensor<3xi32>) -> tensor<4x4x?xf32> +// CHECK: %10 = "tfl.shape"(%arg1) : (tensor<4x?x256xf32>) -> tensor<3xi32> +// CHECK-DAG: %11 = "tfl.pseudo_const"() <{value = dense<[-1, -1, 0]> : tensor<3xi32>}> : () -> tensor<3xi32> +// CHECK-DAG: %12 = "tfl.pseudo_const"() <{value = dense<[-1, 0, -1]> : tensor<3xi32>}> : () -> tensor<3xi32> +// CHECK-DAG: %13 = "tfl.pseudo_const"() <{value = dense<1> : tensor}> : () -> tensor +// CHECK: %14 = "tfl.unsorted_segment_prod"(%10, %11, %13) : (tensor<3xi32>, tensor<3xi32>, tensor) -> tensor<1xi32> +// CHECK: %15 = "tfl.unsorted_segment_prod"(%10, %12, %13) : (tensor<3xi32>, tensor<3xi32>, tensor) -> tensor<1xi32> +// CHECK: %16 = "tfl.pseudo_const"() <{value = dense<4> : tensor<1xi32>}> : () -> tensor<1xi32> +// CHECK: %17 = "tfl.concatenation"(%16, %15, %14) <{axis = 0 : i32, fused_activation_function = "NONE"}> : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<3xi32> +// CHECK: %18 = "tfl.cast"(%17) : (tensor<3xi32>) -> tensor<3xi32> +// CHECK: %19 = "tfl.reshape"(%arg1, %18) : (tensor<4x?x256xf32>, tensor<3xi32>) -> tensor<4x?x256xf32> +// CHECK: %20 = "tfl.batch_matmul"(%9, %19) <{adj_x = false, adj_y = false, asymmetric_quantize_inputs = false}> : (tensor<4x4x?xf32>, tensor<4x?x256xf32>) -> tensor<4x4x256xf32> +// CHECK: return %20 : tensor<4x4x256xf32> + +// ----- + +//===----------------------------------------------------------------------===// +// mhlo.reduce +//===----------------------------------------------------------------------===// + +// CHECK-LABEL: convert_reduce_maximum_to_reduce_any +func.func @convert_reduce_maximum_to_reduce_any(%arg0: tensor<128x1x1024xi1>) -> tensor<128x1xi1> { + %0 = mhlo.constant dense : tensor + %1271 = mhlo.reduce(%arg0 init: %0) applies mhlo.maximum across dimensions = [2] : (tensor<128x1x1024xi1>, tensor) -> tensor<128x1xi1> + func.return %1271 : tensor<128x1xi1> +} + +// CHECK: %[[CST:.*]] = arith.constant dense<2> : tensor<1xi32> +// CHECK: "tfl.reduce_any"(%arg0, %[[CST]]) <{keep_dims = false}> : (tensor<128x1x1024xi1>, tensor<1xi32>) -> tensor<128x1xi1> + +// ----- + +// CHECK-LABEL: argmax +func.func @argmax(%arg0: tensor<4x32x256xf32>) -> (tensor<4x32xf32>, tensor<4x32xi32>) { + %0 = mhlo.constant dense<0xFF800000> : tensor + %1 = mhlo.constant dense<0> : tensor + %2 = "mhlo.iota"() <{iota_dimension = 0 : i64}> : () -> tensor<256xi32> + %3 = "mhlo.broadcast_in_dim"(%2) <{broadcast_dimensions = dense<2> : tensor<1xi64>}> : (tensor<256xi32>) -> tensor<4x32x256xi32> + %4:2 = "mhlo.reduce"(%arg0, %3, %0, %1) ({ + ^bb0(%arg1: tensor, %arg2: tensor, %arg3: tensor, %arg4: tensor): + %7 = "mhlo.compare"(%arg1, %arg3) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %8 = "mhlo.compare"(%arg1, %arg1) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %9 = mhlo.or %7, %8 : tensor + %10 = "mhlo.select"(%9, %arg1, %arg3) : (tensor, tensor, tensor) -> tensor + %11 = "mhlo.compare"(%arg1, %arg3) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %12 = "mhlo.compare"(%arg2, %arg4) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %13 = mhlo.and %11, %12 : tensor + %14 = mhlo.or %9, %13 : tensor + %15 = "mhlo.select"(%14, %arg2, %arg4) : (tensor, tensor, tensor) -> tensor + "mhlo.return"(%10, %15) : (tensor, tensor) -> () + }) {dimensions = dense<2> : tensor<1xi64>} : (tensor<4x32x256xf32>, tensor<4x32x256xi32>, tensor, tensor) -> (tensor<4x32xf32>, tensor<4x32xi32>) + func.return %4#0, %4#1 : tensor<4x32xf32>, tensor<4x32xi32> +} + +// CHECK: %[[CST:.*]] = arith.constant dense<2> : tensor<1xi32> +// CHECK: %[[REDUCE:.*]] = "tfl.reduce_max"(%arg0, %[[CST]]) <{keep_dims = false}> : (tensor<4x32x256xf32>, tensor<1xi32>) -> tensor<4x32xf32> +// CHECK: %[[ARG:.*]] = "tfl.arg_max"(%arg0, %[[CST]]) : (tensor<4x32x256xf32>, tensor<1xi32>) -> tensor<4x32xi32> +// CHECK: return %[[REDUCE]], %[[ARG]] : tensor<4x32xf32>, tensor<4x32xi32> + +// ----- + +// CHECK-LABEL: argmax_constant +func.func @argmax_constant(%arg0: tensor<2x2x4xf32>) -> (tensor<2x2xf32>, tensor<2x2xi32>) { + %0 = mhlo.constant dense<0xFF800000> : tensor + %1 = mhlo.constant dense<0> : tensor + %3 = mhlo.constant dense<[[[0, 1, 2, 3], [0, 1, 2, 3]], [[0, 1, 2, 3], [0, 1, 2, 3]]]> : tensor<2x2x4xi32> + %4:2 = "mhlo.reduce"(%arg0, %3, %0, %1) ({ + ^bb0(%arg1: tensor, %arg2: tensor, %arg3: tensor, %arg4: tensor): + %7 = "mhlo.compare"(%arg1, %arg3) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %8 = "mhlo.compare"(%arg1, %arg1) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %9 = mhlo.or %7, %8 : tensor + %10 = "mhlo.select"(%9, %arg1, %arg3) : (tensor, tensor, tensor) -> tensor + %11 = "mhlo.compare"(%arg1, %arg3) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %12 = "mhlo.compare"(%arg2, %arg4) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %13 = mhlo.and %11, %12 : tensor + %14 = mhlo.or %9, %13 : tensor + %15 = "mhlo.select"(%14, %arg2, %arg4) : (tensor, tensor, tensor) -> tensor + "mhlo.return"(%10, %15) : (tensor, tensor) -> () + }) {dimensions = dense<2> : tensor<1xi64>} : (tensor<2x2x4xf32>, tensor<2x2x4xi32>, tensor, tensor) -> (tensor<2x2xf32>, tensor<2x2xi32>) + func.return %4#0, %4#1 : tensor<2x2xf32>, tensor<2x2xi32> +} + +// CHECK: %[[CST:.*]] = arith.constant dense<2> : tensor<1xi32> +// CHECK: %[[REDUCE:.*]] = "tfl.reduce_max"(%arg0, %[[CST]]) <{keep_dims = false}> : (tensor<2x2x4xf32>, tensor<1xi32>) -> tensor<2x2xf32> +// CHECK: %[[ARG_MAX:.*]] = "tfl.arg_max"(%arg0, %[[CST]]) : (tensor<2x2x4xf32>, tensor<1xi32>) -> tensor<2x2xi32> +// CHECK: return %[[REDUCE:.*]], %[[ARG_MAX]] : tensor<2x2xf32>, tensor<2x2xi32> + +// ----- + +// CHECK-LABEL: argmax_constant_non_z_axis +func.func @argmax_constant_non_z_axis(%arg0: tensor<4x4xf32>) -> (tensor<4xf32>, tensor<4xi32>) { + %0 = mhlo.constant dense<0xFF800000> : tensor + %1 = mhlo.constant dense<0> : tensor + %3 = mhlo.constant dense<[[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]]> : tensor<4x4xi32> + %4:2 = "mhlo.reduce"(%arg0, %3, %0, %1) ({ + ^bb0(%arg1: tensor, %arg2: tensor, %arg3: tensor, %arg4: tensor): + %7 = "mhlo.compare"(%arg1, %arg3) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %8 = "mhlo.compare"(%arg1, %arg1) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %9 = mhlo.or %7, %8 : tensor + %10 = "mhlo.select"(%9, %arg1, %arg3) : (tensor, tensor, tensor) -> tensor + %11 = "mhlo.compare"(%arg1, %arg3) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %12 = "mhlo.compare"(%arg2, %arg4) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %13 = mhlo.and %11, %12 : tensor + %14 = mhlo.or %9, %13 : tensor + %15 = "mhlo.select"(%14, %arg2, %arg4) : (tensor, tensor, tensor) -> tensor + "mhlo.return"(%10, %15) : (tensor, tensor) -> () + }) {dimensions = dense<0> : tensor<1xi64>} : (tensor<4x4xf32>, tensor<4x4xi32>, tensor, tensor) -> (tensor<4xf32>, tensor<4xi32>) + func.return %4#0, %4#1 : tensor<4xf32>, tensor<4xi32> +} + +// CHECK: %[[CST:.*]] = arith.constant dense<0> : tensor<1xi32> +// CHECK: %[[REDUCE:.*]] = "tfl.reduce_max"(%arg0, %[[CST]]) <{keep_dims = false}> : (tensor<4x4xf32>, tensor<1xi32>) -> tensor<4xf32> +// CHECK: %[[ARG_MAX:.*]] = "tfl.arg_max"(%arg0, %[[CST]]) : (tensor<4x4xf32>, tensor<1xi32>) -> tensor<4xi32> +// CHECK: return %[[REDUCE]], %[[ARG_MAX]] : tensor<4xf32>, tensor<4xi32> + +// ----- + +// CHECK-LABEL: argmax_bool +func.func @argmax_bool(%arg0: tensor<2xi1>) -> tensor { + %0 = "mhlo.iota"() <{iota_dimension = 0 : i64}> : () -> tensor<2xi32> + %1 = mhlo.constant dense : tensor + %2 = mhlo.constant dense<0> : tensor + %3:2 = mhlo.reduce(%arg0 init: %1), (%0 init: %2) across dimensions = [0] : (tensor<2xi1>, tensor<2xi32>, tensor, tensor) -> (tensor, tensor) + reducer(%arg1: tensor, %arg3: tensor) (%arg2: tensor, %arg4: tensor) { + %4 = mhlo.compare GT, %arg1, %arg3 : (tensor, tensor) -> tensor + %5 = mhlo.select %4, %arg1, %arg3 : tensor, tensor + %6 = mhlo.compare EQ, %arg1, %arg3 : (tensor, tensor) -> tensor + %7 = mhlo.compare LT, %arg2, %arg4 : (tensor, tensor) -> tensor + %8 = mhlo.and %6, %7 : tensor + %9 = mhlo.or %4, %8 : tensor + %10 = mhlo.select %9, %arg2, %arg4 : tensor, tensor + mhlo.return %5, %10 : tensor, tensor + } + return %3#1 : tensor +} + +// CHECK: %[[CST:.*]] = arith.constant dense<0> : tensor<1xi32> +// CHECK: "tfl.reduce_any"(%arg0, %[[CST]]) <{keep_dims = false}> : (tensor<2xi1>, tensor<1xi32>) -> tensor + +// ----- + +// CHECK-LABEL: argmin +func.func @argmin(%arg0: tensor<4x32x256xf32>) -> (tensor<4x32xf32>, tensor<4x32xi32>) { + %0 = mhlo.constant dense<0x7F800000> : tensor + %1 = mhlo.constant dense<0> : tensor + %2 = "mhlo.iota"() <{iota_dimension = 0 : i64}> : () -> tensor<256xi32> + %3 = "mhlo.broadcast_in_dim"(%2) <{broadcast_dimensions = dense<2> : tensor<1xi64>}> : (tensor<256xi32>) -> tensor<4x32x256xi32> + %4:2 = "mhlo.reduce"(%arg0, %3, %0, %1) ({ + ^bb0(%arg1: tensor, %arg2: tensor, %arg3: tensor, %arg4: tensor): + %7 = "mhlo.compare"(%arg1, %arg3) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %8 = "mhlo.compare"(%arg1, %arg1) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %9 = mhlo.or %7, %8 : tensor + %10 = "mhlo.select"(%9, %arg1, %arg3) : (tensor, tensor, tensor) -> tensor + %11 = "mhlo.compare"(%arg1, %arg3) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %12 = "mhlo.compare"(%arg2, %arg4) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %13 = mhlo.and %11, %12 : tensor + %14 = mhlo.or %9, %13 : tensor + %15 = "mhlo.select"(%14, %arg2, %arg4) : (tensor, tensor, tensor) -> tensor + "mhlo.return"(%10, %15) : (tensor, tensor) -> () + }) {dimensions = dense<2> : tensor<1xi64>} : (tensor<4x32x256xf32>, tensor<4x32x256xi32>, tensor, tensor) -> (tensor<4x32xf32>, tensor<4x32xi32>) + func.return %4#0, %4#1 : tensor<4x32xf32>, tensor<4x32xi32> +} + +// CHECK: %[[CST:.*]] = arith.constant dense<2> : tensor<1xi32> +// CHECK: %[[REDUCE:.*]] = "tfl.reduce_min"(%arg0, %[[CST]]) <{keep_dims = false}> : (tensor<4x32x256xf32>, tensor<1xi32>) -> tensor<4x32xf32> +// CHECK: %[[ARG:.*]] = "tfl.arg_min"(%arg0, %[[CST]]) : (tensor<4x32x256xf32>, tensor<1xi32>) -> tensor<4x32xi32> +// CHECK: return %[[REDUCE]], %[[ARG]] : tensor<4x32xf32>, tensor<4x32xi32> + +// ----- + +// CHECK-LABEL: argmin_i16 +func.func @argmin_i16(%arg0: tensor<2xi16>) -> (tensor, tensor) { + %0 = mhlo.constant dense : tensor + %1 = "mhlo.iota"() <{iota_dimension = 0 : i64}> : () -> tensor<2xi32> + %2 = mhlo.constant dense<32767> : tensor + %3 = mhlo.constant dense<0> : tensor + %4:2 = "mhlo.reduce"(%arg0, %1, %2, %3) ({ + ^bb0(%arg1: tensor, %arg2: tensor, %arg3: tensor, %arg4: tensor): + %11 = mhlo.constant dense : tensor + %12 = "mhlo.compare"(%arg1, %arg3) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %13 = "mhlo.select"(%12, %arg1, %arg3) : (tensor, tensor, tensor) -> tensor + %14 = "mhlo.compare"(%arg1, %arg3) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %15 = "mhlo.compare"(%arg2, %arg4) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %16 = mhlo.and %14, %15 : tensor + %17 = mhlo.or %12, %16 : tensor + %18 = "mhlo.select"(%17, %arg2, %arg4) : (tensor, tensor, tensor) -> tensor + "mhlo.return"(%13, %18) : (tensor, tensor) -> () + }) {dimensions = dense<0> : tensor<1xi64>} : (tensor<2xi16>, tensor<2xi32>, tensor, tensor) -> (tensor, tensor) + func.return %4#0, %4#1 : tensor, tensor +} + +// CHECK: %[[CST:.*]] = arith.constant dense<0> : tensor<1xi32> +// CHECK: %[[REDUCE:.*]] = "tfl.reduce_min"(%arg0, %[[CST]]) <{keep_dims = false}> : (tensor<2xi16>, tensor<1xi32>) -> tensor +// CHECK: %[[ARG:.*]] = "tfl.arg_min"(%arg0, %[[CST]]) : (tensor<2xi16>, tensor<1xi32>) -> tensor +// CHECK: return %[[REDUCE]], %[[ARG]] : tensor, tensor + +// ----- + +// CHECK-LABEL: argmin_constant +func.func @argmin_constant(%arg0: tensor<2x2x4xf32>) -> (tensor<2x2xf32>, tensor<2x2xi32>) { + %0 = mhlo.constant dense<0x7F800000> : tensor + %1 = mhlo.constant dense<0> : tensor + %3 = mhlo.constant dense<[[[0, 1, 2, 3], [0, 1, 2, 3]], [[0, 1, 2, 3], [0, 1, 2, 3]]]> : tensor<2x2x4xi32> + %4:2 = "mhlo.reduce"(%arg0, %3, %0, %1) ({ + ^bb0(%arg1: tensor, %arg2: tensor, %arg3: tensor, %arg4: tensor): + %7 = "mhlo.compare"(%arg1, %arg3) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %8 = "mhlo.compare"(%arg1, %arg1) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %9 = mhlo.or %7, %8 : tensor + %10 = "mhlo.select"(%9, %arg1, %arg3) : (tensor, tensor, tensor) -> tensor + %11 = "mhlo.compare"(%arg1, %arg3) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %12 = "mhlo.compare"(%arg2, %arg4) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %13 = mhlo.and %11, %12 : tensor + %14 = mhlo.or %9, %13 : tensor + %15 = "mhlo.select"(%14, %arg2, %arg4) : (tensor, tensor, tensor) -> tensor + "mhlo.return"(%10, %15) : (tensor, tensor) -> () + }) {dimensions = dense<2> : tensor<1xi64>} : (tensor<2x2x4xf32>, tensor<2x2x4xi32>, tensor, tensor) -> (tensor<2x2xf32>, tensor<2x2xi32>) + func.return %4#0, %4#1 : tensor<2x2xf32>, tensor<2x2xi32> +} + +// CHECK: %[[CST:.*]] = arith.constant dense<2> : tensor<1xi32> +// CHECK: %[[REDUCE:.*]] = "tfl.reduce_min"(%arg0, %[[CST]]) <{keep_dims = false}> : (tensor<2x2x4xf32>, tensor<1xi32>) -> tensor<2x2xf32> +// CHECK: %[[ARG_MIN:.*]] = "tfl.arg_min"(%arg0, %[[CST]]) : (tensor<2x2x4xf32>, tensor<1xi32>) -> tensor<2x2xi32> +// CHECK: return %[[REDUCE]], %[[ARG_MIN]] : tensor<2x2xf32>, tensor<2x2xi32> + +// ----- + +// CHECK-LABEL: argmin_bool +func.func @argmin_bool(%arg0: tensor<2xi1>) -> tensor { + %0 = "mhlo.iota"() <{iota_dimension = 0 : i64}> : () -> tensor<2xi32> + %1 = mhlo.constant dense : tensor + %2 = mhlo.constant dense<0> : tensor + %3:2 = mhlo.reduce(%arg0 init: %1), (%0 init: %2) across dimensions = [0] : (tensor<2xi1>, tensor<2xi32>, tensor, tensor) -> (tensor, tensor) + reducer(%arg1: tensor, %arg3: tensor) (%arg2: tensor, %arg4: tensor) { + %4 = mhlo.compare LT, %arg1, %arg3 : (tensor, tensor) -> tensor + %5 = mhlo.select %4, %arg1, %arg3 : tensor, tensor + %6 = mhlo.compare EQ, %arg1, %arg3 : (tensor, tensor) -> tensor + %7 = mhlo.compare LT, %arg2, %arg4 : (tensor, tensor) -> tensor + %8 = mhlo.and %6, %7 : tensor + %9 = mhlo.or %4, %8 : tensor + %10 = mhlo.select %9, %arg2, %arg4 : tensor, tensor + mhlo.return %5, %10 : tensor, tensor + } + return %3#1 : tensor +} + +// CHECK: %[[CST:.*]] = arith.constant dense<0> : tensor<1xi32> +// CHECK: %[[ARG_MIN:.*]] = "tfl.arg_min"(%arg0, %[[CST]]) : (tensor<2xi1>, tensor<1xi32>) -> tensor +// CHECK: return %[[ARG_MIN]] : tensor + +// ----- + +// CHECK-LABEL: argmax_with_reshaped_iota +func.func @argmax_with_reshaped_iota(%arg0: tensor<1x32x1xf32>) -> (tensor<1x1xf32>, tensor<1x1xi32>) { + %0 = mhlo.constant dense<0xFF800000> : tensor + %1 = mhlo.constant dense<0> : tensor + %2 = "mhlo.iota"() <{iota_dimension = 0 : i64}> : () -> tensor<32xi32> + %3 = "mhlo.reshape"(%2) : (tensor<32xi32>) -> tensor<1x32x1xi32> + %4:2 = mhlo.reduce(%arg0 init: %0), (%3 init: %1) across dimensions = [1] : (tensor<1x32x1xf32>, tensor<1x32x1xi32>, tensor, tensor) -> (tensor<1x1xf32>, tensor<1x1xi32>) + reducer(%arg1: tensor, %arg3: tensor) (%arg2: tensor, %arg4: tensor) { + %5 = "mhlo.compare"(%arg1, %arg3) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %6 = "mhlo.compare"(%arg1, %arg1) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %7 = mhlo.or %5, %6 : tensor + %8 = "mhlo.select"(%7, %arg1, %arg3) : (tensor, tensor, tensor) -> tensor + %9 = "mhlo.compare"(%arg1, %arg3) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %10 = "mhlo.compare"(%arg2, %arg4) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + %11 = mhlo.and %9, %10 : tensor + %12 = mhlo.or %7, %11 : tensor + %13 = "mhlo.select"(%12, %arg2, %arg4) : (tensor, tensor, tensor) -> tensor + "mhlo.return"(%8, %13) : (tensor, tensor) -> () + } + func.return %4#0, %4#1 : tensor<1x1xf32>, tensor<1x1xi32> +} + +// CHECK: %[[CST:.*]] = arith.constant dense<1> : tensor<1xi32> +// CHECK: %[[REDUCE:.*]] = "tfl.reduce_max"(%arg0, %[[CST]]) <{keep_dims = false}> : (tensor<1x32x1xf32>, tensor<1xi32>) -> tensor<1x1xf32> +// CHECK: %[[ARG:.*]] = "tfl.arg_max"(%arg0, %[[CST]]) : (tensor<1x32x1xf32>, tensor<1xi32>) -> tensor<1x1xi32> +// CHECK: return %[[REDUCE]], %[[ARG]] : tensor<1x1xf32>, tensor<1x1xi32> + +// ----- + +// CHECK-LABEL: pytorch_argmax +func.func @pytorch_argmax(%arg0: tensor<1x9xi32>) -> tensor<1xi32> { + %0 = mhlo.constant dense<0> : tensor + %1 = mhlo.constant dense<-2147483648> : tensor + %2 = "mhlo.iota"() <{iota_dimension = 0 : i64}> : () -> tensor<9xi32> + %3 = mhlo.reshape %2 : (tensor<9xi32>) -> tensor<1x9xi32> + %4:2 = mhlo.reduce(%arg0 init: %1), (%3 init: %0) across dimensions = [1] : (tensor<1x9xi32>, tensor<1x9xi32>, tensor, tensor) -> (tensor<1xi32>, tensor<1xi32>) + reducer(%arg1: tensor, %arg3: tensor) (%arg2: tensor, %arg4: tensor) { + %6 = mhlo.compare GE, %arg1, %arg3 : (tensor, tensor) -> tensor + %7 = mhlo.select %6, %arg1, %arg3 : tensor, tensor + %8 = mhlo.compare EQ, %arg1, %arg3 : (tensor, tensor) -> tensor + %9 = mhlo.minimum %arg2, %arg4 : tensor + %10 = mhlo.select %6, %arg2, %arg4 : tensor, tensor + %11 = mhlo.select %8, %9, %10 : tensor, tensor + mhlo.return %7, %11 : tensor, tensor + } + func.return %4#1 : tensor<1xi32> +} + +// CHECK: %[[CST:.*]] = arith.constant dense<1> : tensor<1xi32> +// CHECK: %[[ARG:.*]] = "tfl.arg_max"(%arg0, %[[CST]]) : (tensor<1x9xi32>, tensor<1xi32>) -> tensor<1xi32> +// CHECK: return %[[ARG]] : tensor<1xi32> + +// ----- + +//===----------------------------------------------------------------------===// +// mhlo.cbrt +//===----------------------------------------------------------------------===// + +// CHECK-LABEL: cbrt_f32 +func.func @cbrt_f32(%arg0: tensor<1x32x1xf32>) -> tensor<1x32x1xf32> { + %0 = "mhlo.cbrt"(%arg0) : (tensor<1x32x1xf32>) -> tensor<1x32x1xf32> + func.return %0 : tensor<1x32x1xf32> +} + +// CHECK-DAG: %cst = arith.constant dense<1.000000e+00> : tensor +// CHECK-DAG: %cst_0 = arith.constant dense<3.000000e+00> : tensor +// CHECK: %0 = tfl.div %cst, %cst_0 {fused_activation_function = "NONE"} : tensor +// CHECK: %1 = tfl.pow(%arg0, %0) : (tensor<1x32x1xf32>, tensor) -> tensor<1x32x1xf32> +// CHECK: return %1 : tensor<1x32x1xf32> + +// ----- + +// CHECK-LABEL: cbrt_f64 +func.func @cbrt_f64(%arg0: tensor<1x32x1xf64>) -> tensor<1x32x1xf64> { + %0 = "mhlo.cbrt"(%arg0) : (tensor<1x32x1xf64>) -> tensor<1x32x1xf64> + func.return %0 : tensor<1x32x1xf64> +} + +// CHECK-NOT: tfl + +// ----- + +//===----------------------------------------------------------------------===// +// mhlo.(dynamic)reshape +//===----------------------------------------------------------------------===// + +// CHECK-LABEL: reshape +func.func @reshape(%arg0: tensor<2x3xf32>) -> tensor<3x2xf32> { + %0 = "mhlo.reshape"(%arg0) : (tensor<2x3xf32>) -> tensor<3x2xf32> + func.return %0 : tensor<3x2xf32> +} + +// CHECK: %cst = arith.constant dense<[3, 2]> : tensor<2xi64> +// CHECK: %0 = "tfl.cast"(%cst) : (tensor<2xi64>) -> tensor<2xi32> +// CHECK: %1 = "tfl.reshape"(%arg0, %0) : (tensor<2x3xf32>, tensor<2xi32>) -> tensor<3x2xf32> + +// ----- + +// CHECK-LABEL: dynamic_reshape_i32 +func.func @dynamic_reshape_i32(%arg0: tensor<2x3xf32>, %arg1: tensor<2xi32>) -> tensor { + %0 = "mhlo.dynamic_reshape"(%arg0, %arg1) : (tensor<2x3xf32>, tensor<2xi32>) -> tensor + func.return %0 : tensor +} + +// CHECK: %0 = "tfl.cast"(%arg1) : (tensor<2xi32>) -> tensor<2xi32> +// CHECK: %1 = "tfl.reshape"(%arg0, %0) : (tensor<2x3xf32>, tensor<2xi32>) -> tensor + +// ----- + +// CHECK-LABEL: dynamic_reshape_i64 +func.func @dynamic_reshape_i64(%arg0: tensor<2x3xf32>, %arg1: tensor<2xi64>) -> tensor { + %0 = "mhlo.dynamic_reshape"(%arg0, %arg1) : (tensor<2x3xf32>, tensor<2xi64>) -> tensor + func.return %0 : tensor +} + +// CHECK: %0 = "tfl.cast"(%arg1) : (tensor<2xi64>) -> tensor<2xi32> +// CHECK: %1 = "tfl.reshape"(%arg0, %0) : (tensor<2x3xf32>, tensor<2xi32>) -> tensor + +// ----- + +//===----------------------------------------------------------------------===// +// mhlo binary bit-wise ops +//===----------------------------------------------------------------------===// + +// CHECK-LABEL: logical_and +func.func @logical_and(%arg0: tensor<2xi1>, %arg1: tensor<2xi1>) -> tensor<2xi1> { + %0 = mhlo.and %arg0, %arg1 : tensor<2xi1> + func.return %0 : tensor<2xi1> +} + +// CHECK: tfl.logical_and +// CHECK-NOT: mhlo + +// ----- + +// CHECK-LABEL: bitwise_and +func.func @bitwise_and(%arg0: tensor<4xi32>, %arg1: tensor<4xi32>) -> tensor<4xi32> { + %0 = mhlo.and %arg0, %arg1 : tensor<4xi32> + func.return %0 : tensor<4xi32> +} + +// CHECK: mhlo.and +// CHECK-NOT: tfl + +// ----- + +// CHECK-LABEL: logical_or +func.func @logical_or(%arg0: tensor<2xi1>, %arg1: tensor<2xi1>) -> tensor<2xi1> { + %0 = mhlo.or %arg0, %arg1 : tensor<2xi1> + func.return %0 : tensor<2xi1> +} + +// CHECK: tfl.logical_or +// CHECK-NOT: mhlo + +// ----- + +// CHECK-LABEL: bitwise_or +func.func @bitwise_or(%arg0: tensor<4xi32>, %arg1: tensor<4xi32>) -> tensor<4xi32> { + %0 = mhlo.or %arg0, %arg1 : tensor<4xi32> + func.return %0 : tensor<4xi32> +} + +// CHECK: mhlo.or +// CHECK-NOT: tfl + +// ----- + +// CHECK-LABEL: logical_xor +func.func @logical_xor(%arg0: tensor<4xi1>, %arg1: tensor<4xi1>) -> tensor<4xi1> { + %0 = mhlo.xor %arg0, %arg1 : tensor<4xi1> + func.return %0 : tensor<4xi1> +} + +// ----- + +//===----------------------------------------------------------------------===// +// mhlo.convolution +//===----------------------------------------------------------------------===// + +// +// 2D +//=--- + +// CHECK-LABEL: transpose_conv2d_valid_padding_odd +func.func @transpose_conv2d_valid_padding_odd(%arg0: tensor<1x200x198x4xf32>, %arg1: tensor<4x4x4x4xf32>) -> tensor<1x402x398x4xf32> { + %0 = mhlo.convolution(%arg0, %arg1) + dim_numbers = [b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f], + window = {pad = [[3, 3], [3, 3]],lhs_dilate = [2, 2]} + {batch_group_count = 1 : i64, feature_group_count = 1 : i64} + : (tensor<1x200x198x4xf32>, tensor<4x4x4x4xf32>) -> tensor<1x402x398x4xf32> + func.return %0 : tensor<1x402x398x4xf32> + // CHECK %cst = arith.constant dense<0.000000e+00> : tensor<4xf32> + // CHECK %cst_0 = arith.constant dense<[1, 2]> : tensor<2xi32> + // CHECK %0 = "tfl.reverse_v2"(%arg1, %cst_0) : (tensor<4x4x4x4xf32>, tensor<2xi32>) -> tensor<4x4x4x4xf32> + // CHECK %cst_1 = arith.constant dense<[1, 402, 398, 4]> : tensor<4xi32> + // CHECK %1 = "tfl.transpose_conv"(%cst_1, %0, %arg0, %cst) <{fused_activation_function = "NONE", padding = "VALID", stride_h = 2 : i32, stride_w = 2 : i32}> : (tensor<4xi32>, tensor<4x4x4x4xf32>, tensor<1x200x198x4xf32>, tensor<4xf32>) -> tensor<1x402x398x4xf32> + // CHECK return %1 : tensor<1x402x398x4xf32> +} + +// CHECK-LABEL: transpose_conv2d_same_padding +func.func @transpose_conv2d_same_padding(%input: tensor<1x256x256x2xf32>, %filter:tensor<2x4x4x2xf32>) -> tensor<1x512x512x2xf32> { + %0 = mhlo.convolution(%input, %filter) + dim_numbers = [b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f], + window = {pad = [[2, 2], [2, 2]], lhs_dilate = [2, 2]} + {batch_group_count = 1 : i64, feature_group_count = 1 : i64} + : (tensor<1x256x256x2xf32>, tensor<2x4x4x2xf32>) -> tensor<1x512x512x2xf32> + func.return %0 : tensor<1x512x512x2xf32> + // CHECK %cst = arith.constant dense<0.000000e+00> : tensor<2xf32> + // CHECK %cst_0 = arith.constant dense<[1, 2]> : tensor<2xi32> + // CHECK %0 = "tfl.reverse_v2"(%arg1, %cst_0) : (tensor<2x4x4x2xf32>, tensor<2xi32>) -> tensor<2x4x4x2xf32> + // CHECK %1 = "tfl.pseudo_const"() <{value = dense<[1, 512, 512, 2]> : tensor<4xi32>}> : () -> tensor<4xi32> + // CHECK %2 = "tfl.transpose_conv"(%1, %0, %arg0, %cst) <{fused_activation_function = "NONE", padding = "SAME", stride_h = 2 : i32, stride_w = 2 : i32}> : (tensor<4xi32>, tensor<2x4x4x2xf32>, tensor<1x256x256x2xf32>, tensor<2xf32>) -> tensor<1x512x512x2xf32> + // CHECK return %2 : tensor<1x512x512x2xf32> +} + +// ----- + +// CHECK-LABEL: transpose_conv2d_valid_padding +func.func @transpose_conv2d_valid_padding(%input: tensor<1x256x256x2xf32>, %filter:tensor<2x4x4x2xf32>) -> tensor<1x514x514x2xf32> { + %0 = mhlo.convolution(%input, %filter) + dim_numbers = [b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f], + window = {pad = [[3, 3], [3, 3]], lhs_dilate = [2, 2]} + {batch_group_count = 1 : i64, feature_group_count = 1 : i64} + : (tensor<1x256x256x2xf32>, tensor<2x4x4x2xf32>) -> tensor<1x514x514x2xf32> + func.return %0 : tensor<1x514x514x2xf32> + // CHECK %cst = arith.constant dense<0.000000e+00> : tensor<2xf32> + // CHECK %cst_0 = arith.constant dense<[1, 2]> : tensor<2xi32> + // CHECK %0 = "tfl.reverse_v2"(%arg1, %cst_0) : (tensor<2x4x4x2xf32>, tensor<2xi32>) -> tensor<2x4x4x2xf32> + // CHECK %1 = "tfl.pseudo_const"() <{value = dense<[1, 514, 514, 2]> : tensor<4xi32>}> : () -> tensor<4xi32> + // CHECK %2 = "tfl.transpose_conv"(%1, %0, %arg0, %cst) <{fused_activation_function = "NONE", padding = "VALID", stride_h = 2 : i32, stride_w = 2 : i32}> : (tensor<4xi32>, tensor<2x4x4x2xf32>, tensor<1x256x256x2xf32>, tensor<2xf32>) -> tensor<1x514x514x2xf32> + // CHECK return %2 : tensor<1x514x514x2xf32> +} + +// ----- + +// CHECK-LABEL: transpose_conv2d_valid_padding_equal_strides +func.func @transpose_conv2d_valid_padding_equal_strides(%arg0: tensor<1x200x198x3xf32>, %arg1: tensor<3x3x3x3xf32>) -> tensor<1x401x397x3xf32> { + %0 = mhlo.convolution(%arg0, %arg1) + dim_numbers = [b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f], + window = {pad = [[2, 2], [2, 2]], lhs_dilate = [2, 2]} + {batch_group_count = 1 : i64, feature_group_count = 1 : i64} + : (tensor<1x200x198x3xf32>, tensor<3x3x3x3xf32>) -> tensor<1x401x397x3xf32> + func.return %0 : tensor<1x401x397x3xf32> + // CHECK %cst = arith.constant dense<0.000000e+00> : tensor<3xf32> + // CHECK %cst_0 = arith.constant dense<[1, 2]> : tensor<2xi32> + // CHECK %0 = "tfl.reverse_v2"(%arg1, %cst_0) : (tensor<3x3x3x3xf32>, tensor<2xi32>) -> tensor<3x3x3x3xf32> + // CHECK %cst_1 = arith.constant dense<[1, 401, 397, 3]> : tensor<4xi32> + // CHECK %1 = "tfl.transpose_conv"(%cst_1, %0, %arg0, %cst) <{fused_activation_function = "NONE", padding = "VALID", stride_h = 2 : i32, stride_w = 2 : i32}> : (tensor<4xi32>, tensor<3x3x3x3xf32>, tensor<1x200x198x3xf32>, tensor<3xf32>) -> tensor<1x401x397x3xf32> + // CHECK return %1 : tensor<1x401x397x3xf32> +} + +// ----- + +// CHECK-LABEL: conv2d_nhwc_ohwi_nhwc +func.func @conv2d_nhwc_ohwi_nhwc(%input: tensor<1x256x256x3xf32>, %filter: tensor<2x1x1x3xf32>) -> tensor<1x256x256x2xf32> { + %0 = mhlo.convolution(%input, %filter) + dim_numbers = [b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f], + window = {stride = [1, 1], pad = [[0, 0], [0, 0]]} { + batch_group_count = 1 : i64, + feature_group_count = 1 : i64, + window_strides = dense<1> : tensor<2xi64>, + padding = dense<0> : tensor<2x2xi64>, + rhs_dilation = dense<[1, 1]> : tensor<2xi64>, + lhs_dilation = dense<[1, 1]> : tensor<2xi64> + } : (tensor<1x256x256x3xf32>, tensor<2x1x1x3xf32>) -> tensor<1x256x256x2xf32> + func.return %0 : tensor<1x256x256x2xf32> +} + +// CHECK: "tfl.conv_2d"(%arg0, %arg1, %cst) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x256x256x3xf32>, tensor<2x1x1x3xf32>, tensor<2xf32>) -> tensor<1x256x256x2xf32> + +// ----- + +// CHECK-LABEL: conv2d_nhwc_ohwi_nhwc_no_strides +func.func @conv2d_nhwc_ohwi_nhwc_no_strides(%arg0: tensor<1x64x64x4xf32>, %arg1: tensor<320x3x3x4xf32>) -> tensor<1x62x62x320xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f]>, + feature_group_count = 1 : i64, + lhs_dilation = dense<[1, 1]> : tensor<2xi64>, + padding = dense<0> : tensor<2x2xi64>, + precision_config = [#mhlo, #mhlo], + rhs_dilation = dense<[1, 1]> : tensor<2xi64> + } : (tensor<1x64x64x4xf32>, tensor<320x3x3x4xf32>) -> tensor<1x62x62x320xf32> + func.return %0 : tensor<1x62x62x320xf32> +} + +// CHECK: "tfl.conv_2d"(%arg0, %arg1, %cst) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x64x64x4xf32>, tensor<320x3x3x4xf32>, tensor<320xf32>) -> tensor<1x62x62x320xf32> + +// ----- + +// CHECK-LABEL: conv2d_nhwc_ohwi_nhwc_no_padding +func.func @conv2d_nhwc_ohwi_nhwc_no_padding(%arg0: tensor<1x6x6x207xf32>, %arg1: tensor<16x3x3x207xf32>) -> tensor<1x4x4x16xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f]>, + feature_group_count = 1 : i64, + lhs_dilation = dense<1> : tensor<2xi64>, + precision_config = [#mhlo, #mhlo], + rhs_dilation = dense<1> : tensor<2xi64>, + window_strides = dense<1> : tensor<2xi64> + } : (tensor<1x6x6x207xf32>, tensor<16x3x3x207xf32>) -> tensor<1x4x4x16xf32> + func.return %0 : tensor<1x4x4x16xf32> +} + +// CHECK: "tfl.conv_2d"(%arg0, %arg1, %cst) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x6x6x207xf32>, tensor<16x3x3x207xf32>, tensor<16xf32>) -> tensor<1x4x4x16xf32> + +// ----- + +// CHECK-LABEL: conv2d_nhwc_ohwi_nhwc_no_lhs_dilation +func.func @conv2d_nhwc_ohwi_nhwc_no_lhs_dilation(%arg0: tensor<1x6x6x207xf32>, %arg1: tensor<16x3x3x207xf32>) -> tensor<1x4x4x16xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f]>, + feature_group_count = 1 : i64, + padding = dense<0> : tensor<2x2xi64>, + precision_config = [#mhlo, #mhlo], + rhs_dilation = dense<1> : tensor<2xi64>, + window_strides = dense<1> : tensor<2xi64> + } : (tensor<1x6x6x207xf32>, tensor<16x3x3x207xf32>) -> tensor<1x4x4x16xf32> + func.return %0 : tensor<1x4x4x16xf32> +} + +// CHECK: "tfl.conv_2d"(%arg0, %arg1, %cst) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x6x6x207xf32>, tensor<16x3x3x207xf32>, tensor<16xf32>) -> tensor<1x4x4x16xf32> + +// ----- + +// CHECK-LABEL: conv2d_nhwc_ohwi_nhwc_no_rhs_dilation +func.func @conv2d_nhwc_ohwi_nhwc_no_rhs_dilation(%arg0: tensor<1x6x6x207xf32>, %arg1: tensor<16x3x3x207xf32>) -> tensor<1x4x4x16xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f]>, + feature_group_count = 1 : i64, + padding = dense<0> : tensor<2x2xi64>, + precision_config = [#mhlo, #mhlo], + lhs_dilation = dense<1> : tensor<2xi64>, + window_strides = dense<1> : tensor<2xi64> + } : (tensor<1x6x6x207xf32>, tensor<16x3x3x207xf32>) -> tensor<1x4x4x16xf32> + func.return %0 : tensor<1x4x4x16xf32> +} + +// CHECK: "tfl.conv_2d"(%arg0, %arg1, %cst) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x6x6x207xf32>, tensor<16x3x3x207xf32>, tensor<16xf32>) -> tensor<1x4x4x16xf32> + +// ----- + +// CHECK-LABEL: conv2d_nhwc_ohwi_nhwc_no_strides +func.func @conv2d_nhwc_ohwi_nhwc_no_strides(%arg0: tensor<1x6x6x207xf32>, %arg1: tensor<16x3x3x207xf32>) -> tensor<1x4x4x16xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f]>, + feature_group_count = 1 : i64, + padding = dense<0> : tensor<2x2xi64>, + precision_config = [#mhlo, #mhlo], + lhs_dilation = dense<1> : tensor<2xi64>, + rhs_dilation = dense<1> : tensor<2xi64> + } : (tensor<1x6x6x207xf32>, tensor<16x3x3x207xf32>) -> tensor<1x4x4x16xf32> + func.return %0 : tensor<1x4x4x16xf32> +} + +// CHECK: "tfl.conv_2d"(%arg0, %arg1, %cst) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x6x6x207xf32>, tensor<16x3x3x207xf32>, tensor<16xf32>) -> tensor<1x4x4x16xf32> + +// ----- + +// CHECK-LABEL: conv2d_nhwc_ohwi_nhwc_rhs_dilated +func.func @conv2d_nhwc_ohwi_nhwc_rhs_dilated(%input: tensor<1x256x256x3xf32>, %filter: tensor<2x5x5x3xf32>) -> tensor<1x248x248x2xf32> { + %0 = mhlo.convolution(%input, %filter) + dim_numbers = [b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f], + window = {stride = [1, 1], pad = [[0, 0], [0, 0]]} { + batch_group_count = 1 : i64, + feature_group_count = 1 : i64, + window_strides = dense<1> : tensor<2xi64>, + padding = dense<0> : tensor<2x2xi64>, + rhs_dilation = dense<[2, 2]> : tensor<2xi64>, + lhs_dilation = dense<[1, 1]> : tensor<2xi64> + } : (tensor<1x256x256x3xf32>, tensor<2x5x5x3xf32>) -> tensor<1x248x248x2xf32> + func.return %0 : tensor<1x248x248x2xf32> +} + +// CHECK: "tfl.conv_2d"(%arg0, %arg1, %cst) <{dilation_h_factor = 2 : i32, dilation_w_factor = 2 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x256x256x3xf32>, tensor<2x5x5x3xf32>, tensor<2xf32>) -> tensor<1x248x248x2xf32> + +// ----- + +// CHECK-LABEL: conv2d_nhwc_ohwi_nhwc_with_strides +func.func @conv2d_nhwc_ohwi_nhwc_with_strides(%input: tensor<1x115x115x96xf32>, %filter: tensor<96x7x7x96xf32>) -> tensor<1x55x55x96xf32> { + %0 = mhlo.convolution(%input, %filter) + dim_numbers = [b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f], + window = {stride = [2, 2], pad = [[0, 0], [0, 0]]} { + batch_group_count = 1 : i64, + feature_group_count = 1 : i64, + window_strides = dense<2> : tensor<2xi64>, + padding = dense<0> : tensor<2x2xi64>, + rhs_dilation = dense<[1, 1]> : tensor<2xi64>, + lhs_dilation = dense<[1, 1]> : tensor<2xi64> + } : (tensor<1x115x115x96xf32>, tensor<96x7x7x96xf32>) -> tensor<1x55x55x96xf32> + func.return %0 : tensor<1x55x55x96xf32> +} + +// CHECK: "tfl.conv_2d"(%arg0, %arg1, %cst) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 2 : i32, stride_w = 2 : i32}> : (tensor<1x115x115x96xf32>, tensor<96x7x7x96xf32>, tensor<96xf32>) -> tensor<1x55x55x96xf32> + +// ----- + +// CHECK-LABEL: conv2d_nhwc_ohwi_nhwc_grouped_features +func.func @conv2d_nhwc_ohwi_nhwc_grouped_features(%input: tensor<1x256x256x6xf32>, %filter: tensor<2x1x1x3xf32>) -> tensor<1x256x256x2xf32> { + %0 = mhlo.convolution(%input, %filter) + dim_numbers = [b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f], + window = {stride = [1, 1], pad = [[0, 0], [0, 0]]} { + batch_group_count = 1 : i64, + feature_group_count = 2 : i64, + window_strides = dense<1> : tensor<2xi64>, + padding = dense<0> : tensor<2x2xi64>, + rhs_dilation = dense<[1, 1]> : tensor<2xi64>, + lhs_dilation = dense<[1, 1]> : tensor<2xi64> + } : (tensor<1x256x256x6xf32>, tensor<2x1x1x3xf32>) -> tensor<1x256x256x2xf32> + func.return %0 : tensor<1x256x256x2xf32> +} + +// CHECK: "tfl.conv_2d"(%arg0, %arg1, %cst) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x256x256x6xf32>, tensor<2x1x1x3xf32>, tensor<2xf32>) -> tensor<1x256x256x2xf32> + +// ----- + +// CHECK-LABEL: conv2d_nhwc_ohwi_nhwc_dynamic_batch +func.func @conv2d_nhwc_ohwi_nhwc_dynamic_batch(%input: tensor, %filter: tensor<2x1x1x3xf32>) -> tensor { + %0 = mhlo.convolution(%input, %filter) + dim_numbers = [b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f], + window = {stride = [1, 1], pad = [[0, 0], [0, 0]]} { + batch_group_count = 1 : i64, + feature_group_count = 1 : i64 + } : (tensor, tensor<2x1x1x3xf32>) -> tensor + func.return %0 : tensor +} + +// CHECK: "tfl.conv_2d"(%arg0, %arg1, %cst) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor, tensor<2x1x1x3xf32>, tensor<2xf32>) -> tensor + +// ----- + +// CHECK-LABEL: depthwise_conv2d_nhwc_ihwo_nhwc +func.func @depthwise_conv2d_nhwc_ihwo_nhwc(%arg0: tensor<1x10x10x207xf32>, %arg1: tensor<1x3x3x207xf32>) -> tensor<1x8x8x207xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, 1, f]x[i, 0, 1, o]->[b, 0, 1, f]>, + feature_group_count = 207 : i64, + lhs_dilation = dense<1> : tensor<2xi64>, + padding = dense<0> : tensor<2x2xi64>, + precision_config = [#mhlo, #mhlo], + rhs_dilation = dense<1> : tensor<2xi64>, + window_strides = dense<1> : tensor<2xi64> + } : (tensor<1x10x10x207xf32>, tensor<1x3x3x207xf32>) -> tensor<1x8x8x207xf32> + func.return %0 : tensor<1x8x8x207xf32> +} + +// CHECK: %0 = "tfl.depthwise_conv_2d"(%arg0, %arg1, %cst) <{depth_multiplier = 1 : i32, dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x10x10x207xf32>, tensor<1x3x3x207xf32>, tensor<207xf32>) -> tensor<1x8x8x207xf32> +// CHECK: return %0 + +// ----- + +// CHECK-LABEL: depthwise_conv2d_nhwc_ihwo_nhwc_dynamic +func.func @depthwise_conv2d_nhwc_ihwo_nhwc_dynamic(%arg0: tensor, %arg1: tensor<1x3x3x207xf32>) -> tensor { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, 1, f]x[i, 0, 1, o]->[b, 0, 1, f]>, + feature_group_count = 207 : i64, + lhs_dilation = dense<1> : tensor<2xi64>, + padding = dense<0> : tensor<2x2xi64>, + precision_config = [#mhlo, #mhlo], + rhs_dilation = dense<1> : tensor<2xi64>, + window_strides = dense<1> : tensor<2xi64> + } : (tensor, tensor<1x3x3x207xf32>) -> tensor + func.return %0 : tensor +} + +// CHECK: %0 = "tfl.depthwise_conv_2d"(%arg0, %arg1, %cst) <{depth_multiplier = 1 : i32, dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor, tensor<1x3x3x207xf32>, tensor<207xf32>) -> tensor +// CHECK: return %0 + +// ----- + +// CHECK-LABEL: depthwise_conv2d_nhwc_ihwo_nhwc_strided +func.func @depthwise_conv2d_nhwc_ihwo_nhwc_strided(%arg0: tensor<1x10x10x207xf32>, %arg1: tensor<1x3x3x207xf32>) -> tensor<1x4x4x207xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, 1, f]x[i, 0, 1, o]->[b, 0, 1, f]>, + feature_group_count = 207 : i64, + lhs_dilation = dense<1> : tensor<2xi64>, + padding = dense<0> : tensor<2x2xi64>, + precision_config = [#mhlo, #mhlo], + rhs_dilation = dense<1> : tensor<2xi64>, + window_strides = dense<2> : tensor<2xi64> + } : (tensor<1x10x10x207xf32>, tensor<1x3x3x207xf32>) -> tensor<1x4x4x207xf32> + func.return %0 : tensor<1x4x4x207xf32> +} + +// CHECK: %0 = "tfl.depthwise_conv_2d"(%arg0, %arg1, %cst) <{depth_multiplier = 1 : i32, dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 2 : i32, stride_w = 2 : i32}> : (tensor<1x10x10x207xf32>, tensor<1x3x3x207xf32>, tensor<207xf32>) -> tensor<1x4x4x207xf32> +// CHECK: return %0 + +// ----- + +// CHECK-LABEL: depthwise_conv2d_nhwc_ihwo_nhwc_dilated +func.func @depthwise_conv2d_nhwc_ihwo_nhwc_dilated(%arg0: tensor<1x10x10x207xf32>, %arg1: tensor<1x3x3x207xf32>) -> tensor<1x6x6x207xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, 1, f]x[i, 0, 1, o]->[b, 0, 1, f]>, + feature_group_count = 207 : i64, + lhs_dilation = dense<1> : tensor<2xi64>, + padding = dense<0> : tensor<2x2xi64>, + precision_config = [#mhlo, #mhlo], + rhs_dilation = dense<2> : tensor<2xi64>, + window_strides = dense<1> : tensor<2xi64> + } : (tensor<1x10x10x207xf32>, tensor<1x3x3x207xf32>) -> tensor<1x6x6x207xf32> + func.return %0 : tensor<1x6x6x207xf32> +} + +// CHECK: %0 = "tfl.depthwise_conv_2d"(%arg0, %arg1, %cst) <{depth_multiplier = 1 : i32, dilation_h_factor = 2 : i32, dilation_w_factor = 2 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x10x10x207xf32>, tensor<1x3x3x207xf32>, tensor<207xf32>) -> tensor<1x6x6x207xf32> +// CHECK: return %0 + +// ----- + +// CHECK-LABEL: depthwise_conv2d_nhwc_ihwo_nhwc_non_trivial_depth_multiplier +func.func @depthwise_conv2d_nhwc_ihwo_nhwc_non_trivial_depth_multiplier(%arg0: tensor<1x10x10x207xf32>, %arg1: tensor<1x3x3x3519xf32>) -> tensor<1x6x6x3519xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, 1, f]x[i, 0, 1, o]->[b, 0, 1, f]>, + feature_group_count = 207 : i64, + lhs_dilation = dense<1> : tensor<2xi64>, + padding = dense<0> : tensor<2x2xi64>, + precision_config = [#mhlo, #mhlo], + rhs_dilation = dense<2> : tensor<2xi64>, + window_strides = dense<1> : tensor<2xi64> + } : (tensor<1x10x10x207xf32>, tensor<1x3x3x3519xf32>) -> tensor<1x6x6x3519xf32> + func.return %0 : tensor<1x6x6x3519xf32> +} + +// CHECK: %0 = "tfl.depthwise_conv_2d"(%arg0, %arg1, %cst) <{depth_multiplier = 17 : i32, dilation_h_factor = 2 : i32, dilation_w_factor = 2 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x10x10x207xf32>, tensor<1x3x3x3519xf32>, tensor<3519xf32>) -> tensor<1x6x6x3519xf32> +// CHECK: return %0 + +// ----- + +// CHECK-LABEL: conv2d_resize_perferred_nhwc_hwoi_nhwc +func.func @conv2d_resize_perferred_nhwc_hwoi_nhwc(%arg0: tensor<1x56x1248x16xf32>, %arg1: tensor<16x3x1x1xf32>) -> tensor<1x111x1248x16xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f]>, + feature_group_count = 16 : i64, + lhs_dilation = dense<[2, 1]> : tensor<2xi64>, + padding = dense<[[1, 1], [0, 0]]> : tensor<2x2xi64>, + precision_config = [#mhlo, #mhlo], + rhs_dilation = dense<[1, 1]> : tensor<2xi64>, + window_strides = dense<[1, 1]> : tensor<2xi64> + } : (tensor<1x56x1248x16xf32>, tensor<16x3x1x1xf32>) -> tensor<1x111x1248x16xf32> + func.return %0 : tensor<1x111x1248x16xf32> + // CHECK %0 = "tfl.pseudo_const"() <{value = dense<[111, 1248]> : tensor<2xi32>}> : () -> tensor<2xi32> + // CHECK %1 = "tfl.resize_bilinear"(%arg0, %0) <{align_corners = false, half_pixel_centers = false}> : (tensor<1x56x1248x16xf32>, tensor<2xi32>) -> tensor<1x111x1248x16xf32> + // CHECK return %1 : tensor<1x111x1248x16xf32> +} + +// ----- + +// CHECK-LABEL: conv2d_to_resize_nhwc_hwoi_nhwc +func.func @conv2d_to_resize_nhwc_hwoi_nhwc(%arg0: tensor<1x56x624x16xf32>, %arg1: tensor<16x1x257x1xf32>) -> tensor<1x56x904x16xf32> { + %0 = "mhlo.convolution"(%arg0, %arg1) { + batch_group_count = 1 : i64, + dimension_numbers = #mhlo.conv<[b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f]>, + feature_group_count = 16 : i64, + lhs_dilation = dense<[1, 129]> : tensor<2xi64>, + padding = dense<[[0, 0], [128, 128]]> : tensor<2x2xi64>, + precision_config = [#mhlo, #mhlo], + rhs_dilation = dense<1> : tensor<2xi64>, + window_strides = dense<[1, 89]> : tensor<2xi64> + } : (tensor<1x56x624x16xf32>, tensor<16x1x257x1xf32>) -> tensor<1x56x904x16xf32> + func.return %0 : tensor<1x56x904x16xf32> + // CHECK %0 = "tfl.pseudo_const"() <{value = dense<[56, 904]> : tensor<2xi32>}> : () -> tensor<2xi32> + // CHECK %1 = "tfl.resize_bilinear"(%arg0, %0) <{align_corners = true, half_pixel_centers = false}> : (tensor<1x56x624x16xf32>, tensor<2xi32>) -> tensor<1x56x904x16xf32> + // CHECK return %1 : tensor<1x56x904x16xf32> +} + +// ----- + +// +// 3D +//=--- + +// CHECK-LABEL: conv3d_ndhwc_dhwio_ndhwc +func.func @conv3d_ndhwc_dhwio_ndhwc(%input: tensor<1x256x256x256x3xf32>, %filter: tensor<1x1x1x3x2xf32>) -> tensor<1x256x256x256x2xf32> { + %0 = "mhlo.convolution"(%input, %filter) { + dimension_numbers = #mhlo.conv<[b, 0, 1, 2, f]x[0, 1, 2, i, o]->[b, 0, 1, 2, f]>, + batch_group_count = 1 : i64, + feature_group_count = 1 : i64, + window_strides = dense<1> : tensor<3xi64>, + padding = dense<0> : tensor<3x2xi64>, + rhs_dilation = dense<1> : tensor<3xi64>, + lhs_dilation = dense<1> : tensor<3xi64> + } : (tensor<1x256x256x256x3xf32>, tensor<1x1x1x3x2xf32>) -> tensor<1x256x256x256x2xf32> + func.return %0 : tensor<1x256x256x256x2xf32> +} + +// CHECK: "tfl.conv_3d"(%arg0, %arg1, %cst) <{dilation_d_factor = 1 : i32, dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_d = 1 : i32, stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x256x256x256x3xf32>, tensor<1x1x1x3x2xf32>, tensor<2xf32>) -> tensor<1x256x256x256x2xf32> + +// ----- + +// CHECK-LABEL: conv3d_ndhwc_dhwio_ndhwc_no_strides +func.func @conv3d_ndhwc_dhwio_ndhwc_no_strides(%input: tensor<1x256x256x256x3xf32>, %filter: tensor<1x1x1x3x2xf32>) -> tensor<1x256x256x256x2xf32> { + %0 = "mhlo.convolution"(%input, %filter) { + dimension_numbers = #mhlo.conv<[b, 0, 1, 2, f]x[0, 1, 2, i, o]->[b, 0, 1, 2, f]>, + batch_group_count = 1 : i64, + feature_group_count = 1 : i64, + padding = dense<0> : tensor<3x2xi64>, + rhs_dilation = dense<1> : tensor<3xi64>, + lhs_dilation = dense<1> : tensor<3xi64> + } : (tensor<1x256x256x256x3xf32>, tensor<1x1x1x3x2xf32>) -> tensor<1x256x256x256x2xf32> + func.return %0 : tensor<1x256x256x256x2xf32> +} + +// CHECK: "tfl.conv_3d"(%arg0, %arg1, %cst) <{dilation_d_factor = 1 : i32, dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_d = 1 : i32, stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x256x256x256x3xf32>, tensor<1x1x1x3x2xf32>, tensor<2xf32>) -> tensor<1x256x256x256x2xf32> + +// ----- + +// CHECK-LABEL: conv3d_ndhwc_dhwio_ndhwc_no_pad +func.func @conv3d_ndhwc_dhwio_ndhwc_no_pad(%input: tensor<1x256x256x256x3xf32>, %filter: tensor<1x1x1x3x2xf32>) -> tensor<1x256x256x256x2xf32> { + %0 = "mhlo.convolution"(%input, %filter) { + dimension_numbers = #mhlo.conv<[b, 0, 1, 2, f]x[0, 1, 2, i, o]->[b, 0, 1, 2, f]>, + batch_group_count = 1 : i64, + feature_group_count = 1 : i64, + window_strides = dense<1> : tensor<3xi64>, + rhs_dilation = dense<1> : tensor<3xi64>, + lhs_dilation = dense<1> : tensor<3xi64> + } : (tensor<1x256x256x256x3xf32>, tensor<1x1x1x3x2xf32>) -> tensor<1x256x256x256x2xf32> + func.return %0 : tensor<1x256x256x256x2xf32> +} + +// CHECK: "tfl.conv_3d"(%arg0, %arg1, %cst) <{dilation_d_factor = 1 : i32, dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_d = 1 : i32, stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x256x256x256x3xf32>, tensor<1x1x1x3x2xf32>, tensor<2xf32>) -> tensor<1x256x256x256x2xf32> + +// ----- + +// CHECK-LABEL: conv3d_ndhwc_dhwio_ndhwc_no_lhs_dilation +func.func @conv3d_ndhwc_dhwio_ndhwc_no_lhs_dilation(%input: tensor<1x256x256x256x3xf32>, %filter: tensor<1x1x1x3x2xf32>) -> tensor<1x256x256x256x2xf32> { + %0 = "mhlo.convolution"(%input, %filter) { + dimension_numbers = #mhlo.conv<[b, 0, 1, 2, f]x[0, 1, 2, i, o]->[b, 0, 1, 2, f]>, + batch_group_count = 1 : i64, + feature_group_count = 1 : i64, + window_strides = dense<1> : tensor<3xi64>, + padding = dense<0> : tensor<3x2xi64>, + rhs_dilation = dense<1> : tensor<3xi64> + } : (tensor<1x256x256x256x3xf32>, tensor<1x1x1x3x2xf32>) -> tensor<1x256x256x256x2xf32> + func.return %0 : tensor<1x256x256x256x2xf32> +} + +// CHECK: "tfl.conv_3d"(%arg0, %arg1, %cst) <{dilation_d_factor = 1 : i32, dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_d = 1 : i32, stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x256x256x256x3xf32>, tensor<1x1x1x3x2xf32>, tensor<2xf32>) -> tensor<1x256x256x256x2xf32> + +// ----- + +// CHECK-LABEL: conv3d_ndhwc_dhwio_ndhwc_no_rhs_dilation +func.func @conv3d_ndhwc_dhwio_ndhwc_no_rhs_dilation(%input: tensor<1x256x256x256x3xf32>, %filter: tensor<1x1x1x3x2xf32>) -> tensor<1x256x256x256x2xf32> { + %0 = "mhlo.convolution"(%input, %filter) { + dimension_numbers = #mhlo.conv<[b, 0, 1, 2, f]x[0, 1, 2, i, o]->[b, 0, 1, 2, f]>, + batch_group_count = 1 : i64, + feature_group_count = 1 : i64, + window_strides = dense<1> : tensor<3xi64>, + padding = dense<0> : tensor<3x2xi64>, + lhs_dilation = dense<1> : tensor<3xi64> + } : (tensor<1x256x256x256x3xf32>, tensor<1x1x1x3x2xf32>) -> tensor<1x256x256x256x2xf32> + func.return %0 : tensor<1x256x256x256x2xf32> +} + +// CHECK: "tfl.conv_3d"(%arg0, %arg1, %cst) <{dilation_d_factor = 1 : i32, dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_d = 1 : i32, stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x256x256x256x3xf32>, tensor<1x1x1x3x2xf32>, tensor<2xf32>) -> tensor<1x256x256x256x2xf32> + +// ----- + +// CHECK-LABEL: conv3d_ndhwc_dhwio_ndhwc_rhs_dilated +func.func @conv3d_ndhwc_dhwio_ndhwc_rhs_dilated(%input: tensor<1x256x256x256x3xf32>, %filter: tensor<5x5x5x3x2xf32>) -> tensor<1x248x248x248x2xf32> { + %0 = "mhlo.convolution"(%input, %filter) { + dimension_numbers = #mhlo.conv<[b, 0, 1, 2, f]x[0, 1, 2, i, o]->[b, 0, 1, 2, f]>, + batch_group_count = 1 : i64, + feature_group_count = 1 : i64, + window_strides = dense<1> : tensor<3xi64>, + padding = dense<0> : tensor<3x2xi64>, + rhs_dilation = dense<2> : tensor<3xi64>, + lhs_dilation = dense<1> : tensor<3xi64> + } : (tensor<1x256x256x256x3xf32>, tensor<5x5x5x3x2xf32>) -> tensor<1x248x248x248x2xf32> + func.return %0 : tensor<1x248x248x248x2xf32> +} + +// CHECK: "tfl.conv_3d"(%arg0, %arg1, %cst) <{dilation_d_factor = 2 : i32, dilation_h_factor = 2 : i32, dilation_w_factor = 2 : i32, fused_activation_function = "NONE", padding = "VALID", stride_d = 1 : i32, stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x256x256x256x3xf32>, tensor<5x5x5x3x2xf32>, tensor<2xf32>) -> tensor<1x248x248x248x2xf32> + +// ----- + +// CHECK-LABEL: conv3d_ndhwc_dhwio_ndhwc_strided +func.func @conv3d_ndhwc_dhwio_ndhwc_strided(%input: tensor<1x256x256x256x3xf32>, %filter: tensor<1x1x1x3x2xf32>) -> tensor<1x52x52x52x2xf32> { + %0 = "mhlo.convolution"(%input, %filter) { + dimension_numbers = #mhlo.conv<[b, 0, 1, 2, f]x[0, 1, 2, i, o]->[b, 0, 1, 2, f]>, + batch_group_count = 1 : i64, + feature_group_count = 1 : i64, + window_strides = dense<5> : tensor<3xi64>, + padding = dense<0> : tensor<3x2xi64>, + rhs_dilation = dense<1> : tensor<3xi64>, + lhs_dilation = dense<1> : tensor<3xi64> + } : (tensor<1x256x256x256x3xf32>, tensor<1x1x1x3x2xf32>) -> tensor<1x52x52x52x2xf32> + func.return %0 : tensor<1x52x52x52x2xf32> +} + +// CHECK: "tfl.conv_3d"(%arg0, %arg1, %cst) <{dilation_d_factor = 1 : i32, dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_d = 5 : i32, stride_h = 5 : i32, stride_w = 5 : i32}> : (tensor<1x256x256x256x3xf32>, tensor<1x1x1x3x2xf32>, tensor<2xf32>) -> tensor<1x52x52x52x2xf32> + +// ----- + +// CHECK-LABEL: conv3d_ndhwc_dhwio_ndhwc_grouped_features +func.func @conv3d_ndhwc_dhwio_ndhwc_grouped_features(%input: tensor<1x256x256x256x6xf32>, %filter: tensor<1x1x1x2x6xf32>) -> tensor<1x256x256x256x6xf32> { + %0 = "mhlo.convolution"(%input, %filter) { + dimension_numbers = #mhlo.conv<[b, 0, 1, 2, f]x[0, 1, 2, i, o]->[b, 0, 1, 2, f]>, + batch_group_count = 1 : i64, + feature_group_count = 3 : i64, + window_strides = dense<1> : tensor<3xi64>, + padding = dense<0> : tensor<3x2xi64>, + rhs_dilation = dense<1> : tensor<3xi64>, + lhs_dilation = dense<1> : tensor<3xi64> + } : (tensor<1x256x256x256x6xf32>, tensor<1x1x1x2x6xf32>) -> tensor<1x256x256x256x6xf32> + func.return %0 : tensor<1x256x256x256x6xf32> +} + + +// Unlike TF, tfl.conv_3d requires kernel_dims[3] == input_dims[4] + +// CHECK-NOT: tfl +// CHECK: mhlo.convolution + +// ----- + +//===----------------------------------------------------------------------===// +// mhlo.pad +//===----------------------------------------------------------------------===// + +func.func @pad_cst_zero_vals(%arg0: tensor<8x128xf32>) -> tensor<11x131xf32> { + %pad_val = mhlo.constant dense<0.0> : tensor + %0 = "mhlo.pad"(%arg0, %pad_val) { + edge_padding_low = dense<[1, 0]> : tensor<2xi64>, + edge_padding_high = dense<[2, 3]> : tensor<2xi64>, + interior_padding = dense<0> : tensor<2xi64> + } : (tensor<8x128xf32>, tensor) -> tensor<11x131xf32> + func.return %0 : tensor<11x131xf32> +} + +// CHECK: %[[PADDINGS:.*]] = arith.constant{{.*}}[1, 2], [0, 3] +// CHECK: "tfl.pad"(%arg0, %[[PADDINGS]]) : (tensor<8x128xf32>, tensor<2x2xi64>) -> tensor<11x131xf32> + +// ----- + +func.func @pad(%arg0: tensor<8x128xf32>, %arg1: tensor) -> tensor<11x131xf32> { + %0 = "mhlo.pad"(%arg0, %arg1) { + edge_padding_low = dense<[1, 0]> : tensor<2xi64>, + edge_padding_high = dense<[2, 3]> : tensor<2xi64>, + interior_padding = dense<0> : tensor<2xi64> + } : (tensor<8x128xf32>, tensor) -> tensor<11x131xf32> + func.return %0 : tensor<11x131xf32> +} + +// CHECK: %[[PADDINGS:.*]] = arith.constant +// CHECK-SAME: [1, 2], [0, 3] +// CHECK-SAME: tensor<2x2xi64> +// CHECK: "tfl.padv2"(%arg0, %[[PADDINGS]], %arg1) : (tensor<8x128xf32>, tensor<2x2xi64>, tensor) -> tensor<11x131xf32> + +//===------------------------------------------------------------------------=== +// mhlo.reduce { mhlo.multiply } +//===------------------------------------------------------------------------=== + +// CHECK-LABEL: int_reduce_to_prod( +func.func @int_reduce_to_prod(%arg0: tensor<1x256xi32>) -> tensor<1xi32> { + %0 = mhlo.constant dense<1> : tensor + %1 = "mhlo.reduce"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %2 = mhlo.multiply %arg1, %arg2 : tensor + "mhlo.return"(%2) : (tensor) -> () + }) {dimensions = dense<1> : tensor<1xi64>} : (tensor<1x256xi32>, tensor) -> tensor<1xi32> + func.return %1 : tensor<1xi32> +} + +// CHECK: %[[CST:.*]] = arith.constant dense<1> : tensor<1xi32> +// CHECK: "tfl.reduce_prod"(%arg0, %[[CST]]) <{keep_dims = false}> : (tensor<1x256xi32>, tensor<1xi32>) -> tensor<1xi32> + +// ----- + +// CHECK-LABEL: reduce_to_prod +func.func @reduce_to_prod(%arg0: tensor<1x256xf32>) -> tensor<1xf32> { + %0 = mhlo.constant dense<1.000000e+00> : tensor + %1 = "mhlo.reduce"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %2 = mhlo.multiply %arg1, %arg2 : tensor + "mhlo.return"(%2) : (tensor) -> () + }) {dimensions = dense<1> : tensor<1xi64>} : (tensor<1x256xf32>, tensor) -> tensor<1xf32> + func.return %1 : tensor<1xf32> +} + +// CHECK: %[[CST:.*]] = arith.constant dense<1> : tensor<1xi32> +// CHECK: "tfl.reduce_prod"(%arg0, %[[CST]]) <{keep_dims = false}> : (tensor<1x256xf32>, tensor<1xi32>) -> tensor<1xf32> + +// ----- + +// CHECK-LABEL: reduce_to_prod_non_constant_init +func.func @reduce_to_prod_non_constant_init(%arg0: tensor<1x256xf32>, %arg1: tensor) -> tensor<1xf32> { + %1 = "mhlo.reduce"(%arg0, %arg1) ({ + ^bb0(%arg2: tensor, %arg3: tensor): + %2 = mhlo.multiply %arg2, %arg3 : tensor + "mhlo.return"(%2) : (tensor) -> () + }) {dimensions = dense<1> : tensor<1xi64>} : (tensor<1x256xf32>, tensor) -> tensor<1xf32> + func.return %1 : tensor<1xf32> +} + +// CHECK: %[[CST:.*]] = arith.constant dense<1> : tensor<1xi32> +// CHECK: %0 = "tfl.reduce_prod"(%arg0, %[[CST]]) <{keep_dims = false}> : (tensor<1x256xf32>, tensor<1xi32>) -> tensor<1xf32> +// CHECK: %1 = tfl.mul(%0, %arg1) <{fused_activation_function = "NONE"}> : (tensor<1xf32>, tensor) -> tensor<1xf32> + +// ----- + +//===------------------------------------------------------------------------=== +// mhlo.reduce { mhlo.add } +//===------------------------------------------------------------------------=== + +// CHECK-LABEL: reduce_to_sum +func.func @reduce_to_sum(%arg0: tensor<1x256xf32>) -> tensor<1xf32> { + %0 = mhlo.constant dense<0.000000e+00> : tensor + %1 = "mhlo.reduce"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %2 = mhlo.add %arg1, %arg2 : tensor + "mhlo.return"(%2) : (tensor) -> () + }) {dimensions = dense<1> : tensor<1xi64>} : (tensor<1x256xf32>, tensor) -> tensor<1xf32> + func.return %1 : tensor<1xf32> +} + +// CHECK: %[[CST:.*]] = arith.constant dense<1> : tensor<1xi32> +// CHECK: "tfl.sum"(%arg0, %[[CST]]) <{keep_dims = false}> : (tensor<1x256xf32>, tensor<1xi32>) -> tensor<1xf32> + +// ----- + +// CHECK-LABEL: reduce_to_sum_non_constant_init +func.func @reduce_to_sum_non_constant_init(%arg0: tensor<1x256xf32>, %arg1: tensor) -> tensor<1xf32> { + %1 = "mhlo.reduce"(%arg0, %arg1) ({ + ^bb0(%arg2: tensor, %arg3: tensor): + %2 = mhlo.add %arg2, %arg3 : tensor + "mhlo.return"(%2) : (tensor) -> () + }) {dimensions = dense<1> : tensor<1xi64>} : (tensor<1x256xf32>, tensor) -> tensor<1xf32> + func.return %1 : tensor<1xf32> +} + +// CHECK: %[[CST:.*]] = arith.constant dense<1> : tensor<1xi32> +// CHECK: %0 = "tfl.sum"(%arg0, %[[CST]]) <{keep_dims = false}> : (tensor<1x256xf32>, tensor<1xi32>) -> tensor<1xf32> +// CHECK: %1 = tfl.add(%0, %arg1) <{fused_activation_function = "NONE"}> : (tensor<1xf32>, tensor) -> tensor<1xf32> + +// ----- + +// CHECK-LABEL: int_reduce_to_sum +func.func @int_reduce_to_sum(%arg0: tensor<1x256xi32>) -> tensor<1xi32> { + %0 = mhlo.constant dense<0> : tensor + %1 = "mhlo.reduce"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %2 = mhlo.add %arg1, %arg2 : tensor + "mhlo.return"(%2) : (tensor) -> () + }) {dimensions = dense<1> : tensor<1xi64>} : (tensor<1x256xi32>, tensor) -> tensor<1xi32> + func.return %1 : tensor<1xi32> +} + +// CHECK: %[[CST:.*]] = arith.constant dense<1> : tensor<1xi32> +// CHECK: "tfl.sum"(%arg0, %[[CST]]) <{keep_dims = false}> : (tensor<1x256xi32>, tensor<1xi32>) -> tensor<1xi32> + +// ----- + +//===------------------------------------------------------------------------=== +// mhlo.reduce { mhlo.max } +//===------------------------------------------------------------------------=== + +// CHECK-LABEL: reduce_to_max +func.func @reduce_to_max(%arg0: tensor<1x256xf32>) -> tensor<1xf32> { + // "0xFF800000" represents -INF for f32. + %0 = mhlo.constant dense<0xFF800000> : tensor + %1 = "mhlo.reduce"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %2 = mhlo.maximum %arg1, %arg2 : tensor + "mhlo.return"(%2) : (tensor) -> () + }) {dimensions = dense<1> : tensor<1xi64>} : (tensor<1x256xf32>, tensor) -> tensor<1xf32> + func.return %1 : tensor<1xf32> +} + +// CHECK: %[[CST:.*]] = arith.constant dense<1> : tensor<1xi32> +// CHECK: "tfl.reduce_max"(%arg0, %[[CST]]) <{keep_dims = false}> : (tensor<1x256xf32>, tensor<1xi32>) -> tensor<1xf32> + +// ----- + +// CHECK-LABEL: reduce_to_max_int +func.func @reduce_to_max_int(%arg0: tensor<1x4xi32>) -> tensor<1xi32> { + // -2147483648 is MIN for INT32 + %0 = mhlo.constant dense<-2147483648> : tensor + %1 = mhlo.reduce(%arg0 init: %0) across dimensions = [1] : (tensor<1x4xi32>, tensor) -> tensor<1xi32> + reducer(%arg2: tensor, %arg3: tensor) { + %892 = mhlo.maximum %arg2, %arg3 : tensor + "mhlo.return"(%892) : (tensor) -> () + } + func.return %1 : tensor<1xi32> +} + +// CHECK: %[[CST:.*]] = arith.constant dense<1> : tensor<1xi32> +// CHECK: "tfl.reduce_max"(%arg0, %[[CST]]) <{keep_dims = false}> : (tensor<1x4xi32>, tensor<1xi32>) -> tensor<1xi32> + +// ----- + +//===------------------------------------------------------------------------=== +// mhlo.reduce { mhlo.min } +//===------------------------------------------------------------------------=== + +// CHECK-LABEL: reduce_to_min +func.func @reduce_to_min(%arg0: tensor<1x256xf32>) -> tensor<1xf32> { + // "0x7F800000" represents INF for f32. + %0 = mhlo.constant dense<0x7F800000> : tensor + %1 = "mhlo.reduce"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %2 = mhlo.minimum %arg1, %arg2 : tensor + "mhlo.return"(%2) : (tensor) -> () + }) {dimensions = dense<1> : tensor<1xi64>} : (tensor<1x256xf32>, tensor) -> tensor<1xf32> + func.return %1 : tensor<1xf32> +} + +// CHECK: %[[CST:.*]] = arith.constant dense<1> : tensor<1xi32> +// CHECK: "tfl.reduce_min"(%arg0, %[[CST]]) <{keep_dims = false}> : (tensor<1x256xf32>, tensor<1xi32>) -> tensor<1xf32> + +// ----- + +// CHECK-LABEL: reduce_to_min_int +func.func @reduce_to_min_int(%arg0: tensor<1x4xi32>) -> tensor<1xi32> { + // 2147483647 is MAX for INT32 + %0 = mhlo.constant dense<2147483647> : tensor + %1 = mhlo.reduce(%arg0 init: %0) across dimensions = [1] : (tensor<1x4xi32>, tensor) -> tensor<1xi32> + reducer(%arg2: tensor, %arg3: tensor) { + %892 = mhlo.minimum %arg2, %arg3 : tensor + "mhlo.return"(%892) : (tensor) -> () + } + func.return %1 : tensor<1xi32> +} + +// CHECK: %[[CST:.*]] = arith.constant dense<1> : tensor<1xi32> +// CHECK: "tfl.reduce_min"(%arg0, %[[CST]]) <{keep_dims = false}> : (tensor<1x4xi32>, tensor<1xi32>) -> tensor<1xi32> + +// ----- + +//===------------------------------------------------------------------------=== +// mhlo.reduce { mhlo.and } +//===------------------------------------------------------------------------=== + +// CHECK-LABEL: reduce_to_all +func.func @reduce_to_all(%arg0: tensor<1x2x3x4x5xi1>, %arg1: tensor<2xi64>) -> tensor<2x4x5xi1> { + %0 = mhlo.constant dense : tensor + %1 = "mhlo.reduce"(%arg0, %0) ({ + ^bb0(%arg2: tensor, %arg3: tensor): + %2 = mhlo.and %arg2, %arg3 : tensor + "mhlo.return"(%2) : (tensor) -> () + }) {dimensions = dense<[0, 2]> : tensor<2xi64>} : (tensor<1x2x3x4x5xi1>, tensor) -> tensor<2x4x5xi1> + func.return %1: tensor<2x4x5xi1> +} + +// CHECK: %[[CST:.*]] = arith.constant dense<[0, 2]> : tensor<2xi32> +// CHECK: "tfl.reduce_all"(%arg0, %[[CST]]) <{keep_dims = false}> : (tensor<1x2x3x4x5xi1>, tensor<2xi32>) -> tensor<2x4x5xi1> + +// ----- + +// CHECK-LABEL: reduce_to_all_non_constant_init +func.func @reduce_to_all_non_constant_init(%arg0: tensor, %arg1: tensor<1x2x3x4x5xi1>, %arg2: tensor<2xi64>) -> tensor<2x4x5xi1> { + %0 = "mhlo.reduce"(%arg1, %arg0) ({ + ^bb0(%arg3: tensor, %arg4: tensor): + %1 = mhlo.and %arg3, %arg4 : tensor + "mhlo.return"(%1) : (tensor) -> () + }) {dimensions = dense<[0, 2]> : tensor<2xi64>} : (tensor<1x2x3x4x5xi1>, tensor) -> tensor<2x4x5xi1> + func.return %0: tensor<2x4x5xi1> +} + +// CHECK: %[[CST:.*]] = arith.constant dense<[0, 2]> : tensor<2xi32> +// CHECK: %[[REDUCE:.*]] = "tfl.reduce_all"(%arg1, %[[CST]]) <{keep_dims = false}> : (tensor<1x2x3x4x5xi1>, tensor<2xi32>) -> tensor<2x4x5xi1> +// CHECK: tfl.logical_and(%[[REDUCE]], %arg0) : (tensor<2x4x5xi1>, tensor) -> tensor<2x4x5xi1> + +// ----- + +//===------------------------------------------------------------------------=== +// mhlo.reduce { mhlo.or } +//===------------------------------------------------------------------------=== + +// CHECK-LABEL: reduce_to_any +func.func @reduce_to_any(%arg0: tensor<1x2x3x4x5xi1>, %arg1: tensor<2xi64>) -> tensor<2x4x5xi1> { + %0 = mhlo.constant dense : tensor + %1 = "mhlo.reduce"(%arg0, %0) ({ + ^bb0(%arg2: tensor, %arg3: tensor): + %2 = mhlo.or %arg2, %arg3 : tensor + "mhlo.return"(%2) : (tensor) -> () + }) {dimensions = dense<[0, 2]> : tensor<2xi64>} : (tensor<1x2x3x4x5xi1>, tensor) -> tensor<2x4x5xi1> + func.return %1: tensor<2x4x5xi1> +} + +// CHECK: %[[CST:.*]] = arith.constant dense<[0, 2]> : tensor<2xi32> +// CHECK: "tfl.reduce_any"(%arg0, %[[CST]]) <{keep_dims = false}> : (tensor<1x2x3x4x5xi1>, tensor<2xi32>) -> tensor<2x4x5xi1> + +// ----- + +// CHECK-LABEL: reduce_to_any_non_constant_init +func.func @reduce_to_any_non_constant_init(%arg0: tensor, %arg1: tensor<1x2x3x4x5xi1>, %arg2: tensor<2xi64>) -> tensor<2x4x5xi1> { + %0 = "mhlo.reduce"(%arg1, %arg0) ({ + ^bb0(%arg3: tensor, %arg4: tensor): + %1 = mhlo.or %arg3, %arg4 : tensor + "mhlo.return"(%1) : (tensor) -> () + }) {dimensions = dense<[0, 2]> : tensor<2xi64>} : (tensor<1x2x3x4x5xi1>, tensor) -> tensor<2x4x5xi1> + func.return %0: tensor<2x4x5xi1> +} + +// CHECK: %[[CST:.*]] = arith.constant dense<[0, 2]> : tensor<2xi32> +// CHECK: %[[REDUCE:.*]] = "tfl.reduce_any"(%arg1, %[[CST]]) <{keep_dims = false}> : (tensor<1x2x3x4x5xi1>, tensor<2xi32>) -> tensor<2x4x5xi1> +// CHECK: tfl.logical_or(%[[REDUCE]], %arg0) : (tensor<2x4x5xi1>, tensor) -> tensor<2x4x5xi1> + +// ----- + +//===------------------------------------------------------------------------=== +// mhlo.gather +//===------------------------------------------------------------------------=== + +// CHECK-LABEL: gather_to_slice +func.func @gather_to_slice(%arg0: tensor<3x2944xi32>, %arg1: tensor<3x2xi32>) -> tensor<3x1504xi32> { + %0 = "mhlo.gather"(%arg0, %arg1) { + dimension_numbers = #mhlo.gather< + offset_dims = [1], + collapsed_slice_dims = [0], + start_index_map = [0, 1], + index_vector_dim = 1, + >, + indices_are_sorted = true, + slice_sizes = dense<[1, 1504]> : tensor<2xi64> + } : (tensor<3x2944xi32>, tensor<3x2xi32>) -> tensor<3x1504xi32> + func.return %0 : tensor<3x1504xi32> +} + +// CHECK-DAG: %[[CST:.*]] = arith.constant dense<[2, 1440]> : tensor<2xi32> +// CHECK-DAG: %[[CST_0:.*]] = arith.constant dense<0> : tensor<2xi32> +// CHECK: %[[VAL_0:.*]] = "tfl.maximum"(%arg1, %[[CST_0]]) : (tensor<3x2xi32>, tensor<2xi32>) -> tensor<3x2xi32> +// CHECK: %[[VAL_1:.*]] = "tfl.minimum"(%[[VAL_0]], %[[CST]]) : (tensor<3x2xi32>, tensor<2xi32>) -> tensor<3x2xi32> +// CHECK-DAG: %[[CST_1:.*]] = arith.constant dense<[1, 1504]> : tensor<2xi32> +// CHECK-DAG: %[[CST_2:.*]] = arith.constant dense<0> : tensor<2xi32> +// CHECK-DAG: %[[CST_3:.*]] = arith.constant dense<[1, 2]> : tensor<2xi32> +// CHECK: %[[VAL_2:.*]] = "tfl.slice"(%[[VAL_1]], %[[CST_2]], %[[CST_3]]) : (tensor<3x2xi32>, tensor<2xi32>, tensor<2xi32>) -> tensor<1x2xi32> +// CHECK: %[[VAL_3:.*]] = "tfl.squeeze"(%[[VAL_2]]) <{squeeze_dims = [0]}> : (tensor<1x2xi32>) -> tensor<2xi32> +// CHECK: %[[VAL_4:.*]] = "tfl.slice"(%arg0, %[[VAL_3]], %[[CST_1]]) : (tensor<3x2944xi32>, tensor<2xi32>, tensor<2xi32>) -> tensor<1x1504xi32> +// CHECK-DAG: %[[CST_4:.*]] = arith.constant dense<[1, 0]> : tensor<2xi32> +// CHECK-DAG: %[[CST_5:.*]] = arith.constant dense<[1, 2]> : tensor<2xi32> +// CHECK: %[[VAL_5:.*]] = "tfl.slice"(%[[VAL_1]], %[[CST_4]], %[[CST_5]]) : (tensor<3x2xi32>, tensor<2xi32>, tensor<2xi32>) -> tensor<1x2xi32> +// CHECK: %[[VAL_6:.*]] = "tfl.squeeze"(%[[VAL_5]]) <{squeeze_dims = [0]}> : (tensor<1x2xi32>) -> tensor<2xi32> +// CHECK: %[[VAL_7:.*]] = "tfl.slice"(%arg0, %[[VAL_6]], %[[CST_1]]) : (tensor<3x2944xi32>, tensor<2xi32>, tensor<2xi32>) -> tensor<1x1504xi32> +// CHECK-DAG: %[[CST_6:.*]] = arith.constant dense<[2, 0]> : tensor<2xi32> +// CHECK-DAG: %[[CST_7:.*]] = arith.constant dense<[1, 2]> : tensor<2xi32> +// CHECK: %[[VAL_8:.*]] = "tfl.slice"(%[[VAL_1]], %[[CST_6]], %[[CST_7]]) : (tensor<3x2xi32>, tensor<2xi32>, tensor<2xi32>) -> tensor<1x2xi32> +// CHECK: %[[VAL_9:.*]] = "tfl.squeeze"(%[[VAL_8]]) <{squeeze_dims = [0]}> : (tensor<1x2xi32>) -> tensor<2xi32> +// CHECK: %[[VAL_10:.*]] = "tfl.slice"(%arg0, %[[VAL_9]], %[[CST_1]]) : (tensor<3x2944xi32>, tensor<2xi32>, tensor<2xi32>) -> tensor<1x1504xi32> +// CHECK: %[[VAL_11:.*]] = "tfl.concatenation"(%[[VAL_4]], %[[VAL_7]], %[[VAL_10]]) <{axis = 0 : i32, fused_activation_function = "NONE"}> : (tensor<1x1504xi32>, tensor<1x1504xi32>, tensor<1x1504xi32>) -> tensor<3x1504xi32> + +// ----- + +// CHECK-LABEL: gather +func.func @gather(%arg0: tensor<147456xf16>, %arg1: tensor<192x256x1xi32>) -> tensor<192x256xf16> { + %0 = "mhlo.gather"(%arg0, %arg1) { + dimension_numbers = #mhlo.gather< + collapsed_slice_dims = [0], + index_vector_dim = 2, + start_index_map = [0], + >, + indices_are_sorted = false, + slice_sizes = dense<1> : tensor<1xi64> + } : (tensor<147456xf16>, tensor<192x256x1xi32>) -> tensor<192x256xf16> + func.return %0 : tensor<192x256xf16> +} + +// ----- + +// CHECK-LABEL: gather_with_ui32indices +func.func @gather_with_ui32indices(%arg0: tensor<147456xf16>, %arg1: tensor<192x256x1xui32>) -> tensor<192x256xf16> { + %0 = "mhlo.gather"(%arg0, %arg1) { + dimension_numbers = #mhlo.gather< + collapsed_slice_dims = [0], + index_vector_dim = 2, + start_index_map = [0], + >, + indices_are_sorted = false, + slice_sizes = dense<1> : tensor<1xi64> + } : (tensor<147456xf16>, tensor<192x256x1xui32>) -> tensor<192x256xf16> + func.return %0 : tensor<192x256xf16> +} + +// CHECK: %0 = "tfl.cast"(%arg1) : (tensor<192x256x1xui32>) -> tensor<192x256x1xi64> + +// ----- + +// CHECK-LABEL: gather_nd +func.func @gather_nd(%arg0: tensor<98x128xf32>, %arg1: tensor<4x64xi32>) -> tensor<4x64x128xf32> { + %0 = "mhlo.gather"(%arg0, %arg1) { + dimension_numbers = #mhlo.gather< + collapsed_slice_dims = [0], + index_vector_dim = 2, + offset_dims = [2], + start_index_map = [0], + >, + indices_are_sorted = false, + slice_sizes = dense<[1, 128]> : tensor<2xi64> + } : (tensor<98x128xf32>, tensor<4x64xi32>) -> tensor<4x64x128xf32> + func.return %0 : tensor<4x64x128xf32> +} + +// CHECK: %[[VAL_0:.*]] = "tfl.reshape"(%arg1, %0) : (tensor<4x64xi32>, tensor<3xi32>) -> tensor<4x64x1xi32 +// CHECK: %[[VAL_1:.*]] = "tfl.gather_nd"(%arg0, %[[VAL_0]]) : (tensor<98x128xf32>, tensor<4x64x1xi32>) -> tensor<4x64x128xf32> + +// ----- + +// CHECK-LABEL: gather_transpose +func.func @gather_transpose(%arg0: tensor<128x256xf32>, %arg1: tensor<4x1xi32>) -> tensor<4x128xf32> { + %0 = "mhlo.gather"(%arg0, %arg1) { + dimension_numbers = #mhlo.gather< + collapsed_slice_dims = [1], + index_vector_dim = 1, + offset_dims = [1], + start_index_map = [1], + >, + indices_are_sorted = false, + slice_sizes = dense<[128, 1]> : tensor<2xi64> + } : (tensor<128x256xf32>, tensor<4x1xi32>) -> tensor<4x128xf32> + func.return %0 : tensor<4x128xf32> +} + +// CHECK: %[[VAL_0:.*]] = "tfl.pseudo_const"() <{value = dense<[1, 0]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK: %[[VAL_1:.*]] = "tfl.cast"(%[[VAL_0]]) : (tensor<2xi64>) -> tensor<2xi32> +// CHECK: %[[VAL_2:.*]] = "tfl.transpose"(%arg0, %[[VAL_1]]) : (tensor<128x256xf32>, tensor<2xi32>) -> tensor<256x128xf32> +// CHECK: %[[VAL_3:.*]] = "tfl.gather_nd"(%[[VAL_2]], %arg1) : (tensor<256x128xf32>, tensor<4x1xi32>) -> tensor<4x128xf32> + +// ----- + +// CHECK-LABEL: gather_offset +func.func @gather_offset(%arg0: tensor<1x20xi32>, %arg1: tensor<1x1xi32>) -> tensor<1x1xi32> { + %0 = "mhlo.gather"(%arg0, %arg1) { + dimension_numbers = #mhlo.gather< + collapsed_slice_dims = [1], + index_vector_dim = 1, + offset_dims = [0], + start_index_map = [1], + >, + indices_are_sorted = false, + slice_sizes = dense<1> : tensor<2xi64> + } : (tensor<1x20xi32>, tensor<1x1xi32>) -> tensor<1x1xi32> + func.return %0 : tensor<1x1xi32> +} + +// CHECK: %[[VAL_0:.*]] = "tfl.pseudo_const"() <{value = dense<[1, 0]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK: %[[VAL_1:.*]] = "tfl.cast"(%[[VAL_0]]) : (tensor<2xi64>) -> tensor<2xi32> +// CHECK: %[[VAL_2:.*]] = "tfl.transpose"(%arg0, %[[VAL_1]]) : (tensor<1x20xi32>, tensor<2xi32>) -> tensor<20x1xi32> +// CHECK: %[[VAL_3:.*]] = "tfl.gather_nd"(%[[VAL_2]], %arg1) : (tensor<20x1xi32>, tensor<1x1xi32>) -> tensor<1x1xi32> +// CHECK: %[[VAL_4:.*]] = "tfl.pseudo_const"() <{value = dense<[1, 0]> : tensor<2xi64>}> : () -> tensor<2xi64> +// CHECK: %[[VAL_5:.*]] = "tfl.cast"(%[[VAL_4]]) : (tensor<2xi64>) -> tensor<2xi32> +// CHECK: %[[VAL_6:.*]] = "tfl.transpose"(%[[VAL_3]], %[[VAL_5]]) : (tensor<1x1xi32>, tensor<2xi32>) -> tensor<1x1xi32> + +// ----- + + +// CHECK-LABEL: gather_batching_dims +func.func @gather_batching_dims(%arg0: tensor<2x3x128xf32>, %arg1: tensor<3x128x2x1xi32>) -> tensor<3x128x2xf32> { + %0 = "mhlo.gather"(%arg0, %arg1) { + dimension_numbers = #mhlo.gather< + index_vector_dim = 3, + start_index_map = [2], + operand_batching_dims = [0, 1], + start_indices_batching_dims = [2, 0], + collapsed_slice_dims = [2], + >, + indices_are_sorted = false, + slice_sizes = dense<1> : tensor<3xi64> + } : (tensor<2x3x128xf32>, tensor<3x128x2x1xi32>) -> tensor<3x128x2xf32> + func.return %0 : tensor<3x128x2xf32> +} + +// CHECK-DAG: %[[CST:.*]] = arith.constant dense<[6, 128]> : tensor<2xi64> +// CHECK: %[[VAL_0:.*]] = "tfl.cast"(%[[CST]]) : (tensor<2xi64>) -> tensor<2xi32> +// CHECK: %[[VAL_1:.*]] = "tfl.reshape"(%arg0, %[[VAL_0]]) : (tensor<2x3x128xf32>, tensor<2xi32>) -> tensor<6x128xf32> +// CHECK-DAG: %[[VAL_2:.*]] = "tfl.pseudo_const"() <{value = dense<[2, 0, 1, 3]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_3:.*]] = "tfl.cast"(%[[VAL_2]]) : (tensor<4xi64>) -> tensor<4xi32> +// CHECK: %[[VAL_4:.*]] = "tfl.transpose"(%arg1, %[[VAL_3]]) : (tensor<3x128x2x1xi32>, tensor<4xi32>) -> tensor<2x3x128x1xi32> +// CHECK-DAG: %[[CST_0:.*]] = arith.constant dense<[6, 128, 1]> : tensor<3xi64> +// CHECK: %[[VAL_5:.*]] = "tfl.cast"(%[[CST_0]]) : (tensor<3xi64>) -> tensor<3xi32> +// CHECK: %[[VAL_6:.*]] = "tfl.reshape"(%[[VAL_4]], %[[VAL_5]]) : (tensor<2x3x128x1xi32>, tensor<3xi32>) -> tensor<6x128x1xi32> +// CHECK-DAG: %[[CST_1:.*]] = arith.constant dense<0> : tensor +// CHECK-DAG: %[[CST_2:.*]] = arith.constant dense<6> : tensor +// CHECK-DAG: %[[CST_3:.*]] = arith.constant dense<1> : tensor +// CHECK: %[[VAL_7:.*]] = "tfl.range"(%[[CST_1]], %[[CST_2]], %[[CST_3]]) : (tensor, tensor, tensor) -> tensor<6xi32> +// CHECK-DAG: %[[CST_4:.*]] = arith.constant dense<[6, 1, 1]> : tensor<3xi64> +// CHECK: %[[VAL_8:.*]] = "tfl.cast"(%[[CST_4]]) : (tensor<3xi64>) -> tensor<3xi32> +// CHECK: %[[VAL_9:.*]] = "tfl.reshape"(%[[VAL_7]], %[[VAL_8]]) : (tensor<6xi32>, tensor<3xi32>) -> tensor<6x1x1xi32> +// CHECK-DAG: %[[CST_5:.*]] = arith.constant dense<[6, 128, 1]> : tensor<3xi64> +// CHECK: %[[VAL_10:.*]] = "tfl.broadcast_to"(%[[VAL_9]], %[[CST_5]]) : (tensor<6x1x1xi32>, tensor<3xi64>) -> tensor<6x128x1xi32> +// CHECK: %[[VAL_11:.*]] = "tfl.concatenation"(%[[VAL_10]], %[[VAL_6]]) <{axis = 2 : i32, fused_activation_function = "NONE"}> : (tensor<6x128x1xi32>, tensor<6x128x1xi32>) -> tensor<6x128x2xi32> +// CHECK: %[[VAL_12:.*]] = "tfl.gather_nd"(%[[VAL_1]], %[[VAL_11]]) : (tensor<6x128xf32>, tensor<6x128x2xi32>) -> tensor<6x128xf32> +// CHECK-DAG: %[[CST_6:.*]] = arith.constant dense<[2, 3, 128]> : tensor<3xi64> +// CHECK: %[[VAL_13:.*]] = "tfl.cast"(%[[CST_6]]) : (tensor<3xi64>) -> tensor<3xi32> +// CHECK: %[[VAL_14:.*]] = "tfl.reshape"(%[[VAL_12]], %[[VAL_13]]) : (tensor<6x128xf32>, tensor<3xi32>) -> tensor<2x3x128xf32> +// CHECK: %[[VAL_15:.*]] = "tfl.pseudo_const"() <{value = dense<[1, 2, 0]> : tensor<3xi64>}> : () -> tensor<3xi64> +// CHECK: %[[VAL_16:.*]] = "tfl.cast"(%[[VAL_15]]) : (tensor<3xi64>) -> tensor<3xi32> +// CHECK: %[[VAL_17:.*]] = "tfl.transpose"(%[[VAL_14]], %[[VAL_16]]) : (tensor<2x3x128xf32>, tensor<3xi32>) -> tensor<3x128x2xf32> + +// ----- + +// CHECK-LABEL: convert_gather_non_collapsed_index_dim +func.func @convert_gather_non_collapsed_index_dim(%arg0: tensor<10x5xi32>, %arg1: tensor<2x1xi32>) -> tensor<2x1x5xi32> { + %0 = "mhlo.gather"(%arg0, %arg1) { + dimension_numbers = #mhlo.gather< + index_vector_dim = 1, + offset_dims = [1, 2], + start_index_map = [0], + >, + indices_are_sorted = false, + slice_sizes = dense<[1, 5]> : tensor<2xi64> + } : (tensor<10x5xi32>, tensor<2x1xi32>) -> tensor<2x1x5xi32> + func.return %0 : tensor<2x1x5xi32> +} + +// CHECK: %[[VAL_0:.*]] = "tfl.gather_nd"(%arg0, %arg1) : (tensor<10x5xi32>, tensor<2x1xi32>) -> tensor<2x5xi32 +// CHECK-DAG: %[[CST:.*]] = arith.constant dense<[2, 1, 5]> : tensor<3xi64> +// CHECK: %[[VAL_1:.*]] = "tfl.cast"(%[[CST]]) : (tensor<3xi64>) -> tensor<3xi32> +// CHECK: %[[VAL_2:.*]] = "tfl.reshape"(%[[VAL_0]], %[[VAL_1]]) : (tensor<2x5xi32>, tensor<3xi32>) -> tensor<2x1x5xi32> + +// ----- + +// CHECK-LABEL: convert_gather_indexed_dimension_slice +func.func @convert_gather_indexed_dimension_slice(%arg0: tensor<4x5x6xi32>, %arg1: tensor<2x2xi32>) -> tensor<2x1x5x6xi32> { + %0 = "mhlo.gather"(%arg0, %arg1) { + dimension_numbers = #mhlo.gather< + index_vector_dim = 1, + offset_dims = [1, 2, 3], + start_index_map = [0, 2], + >, + indices_are_sorted = false, + slice_sizes = dense<[1, 5, 6]> : tensor<3xi64> + } : (tensor<4x5x6xi32>, tensor<2x2xi32>) -> tensor<2x1x5x6xi32> + func.return %0 : tensor<2x1x5x6xi32> +} + +// CHECK: %[[VAL_0:.*]] = "tfl.pseudo_const"() <{value = dense<[0, 2, 1]> : tensor<3xi64>}> : () -> tensor<3xi64> +// CHECK: %[[VAL_1:.*]] = "tfl.cast"(%[[VAL_0]]) : (tensor<3xi64>) -> tensor<3xi32> +// CHECK: %[[VAL_2:.*]] = "tfl.transpose"(%arg0, %[[VAL_1]]) : (tensor<4x5x6xi32>, tensor<3xi32>) -> tensor<4x6x5xi32> +// CHECK-DAG: %[[CST:.*]] = arith.constant dense<[2, 1, 2]> : tensor<3xi64> +// CHECK: %[[VAL_3:.*]] = "tfl.cast"(%[[CST]]) : (tensor<3xi64>) -> tensor<3xi32> +// CHECK: %[[VAL_4:.*]] = "tfl.reshape"(%arg1, %[[VAL_3]]) : (tensor<2x2xi32>, tensor<3xi32>) -> tensor<2x1x2xi32> +// CHECK-DAG: %[[CST_0:.*]] = arith.constant dense<0> : tensor +// CHECK-DAG: %[[CST_1:.*]] = arith.constant dense<6> : tensor +// CHECK-DAG: %[[CST_2:.*]] = arith.constant dense<1> : tensor +// CHECK: %[[VAL_5:.*]] = "tfl.range"(%[[CST_0]], %[[CST_1]], %[[CST_2]]) : (tensor, tensor, tensor) -> tensor<6xi32> +// CHECK-DAG: %[[CST_3:.*]] = arith.constant dense<[1, 6, 1]> : tensor<3xi64> +// CHECK: %[[VAL_6:.*]] = "tfl.cast"(%[[CST_3]]) : (tensor<3xi64>) -> tensor<3xi32> +// CHECK: %[[VAL_7:.*]] = "tfl.reshape"(%[[VAL_5]], %[[VAL_6]]) : (tensor<6xi32>, tensor<3xi32>) -> tensor<1x6x1xi32> +// CHECK-DAG: %[[CST_4:.*]] = arith.constant dense<[1, 6, 1]> : tensor<3xi64> +// CHECK: %[[VAL_8:.*]] = "tfl.broadcast_to"(%[[VAL_7]], %[[CST_4]]) : (tensor<1x6x1xi32>, tensor<3xi64>) -> tensor<1x6x1xi32> +// CHECK-DAG: %[[CST_5:.*]] = arith.constant dense<0> : tensor +// CHECK-DAG: %[[CST_6:.*]] = arith.constant +// CHECK-SAME{LITERAL}: dense<[[0, 0], [0, 0], [1, 0]]> : tensor<3x2xi64> +// CHECK: %[[VAL_9:.*]] = "tfl.pad"(%[[VAL_8]], %[[CST_6]]) : (tensor<1x6x1xi32>, tensor<3x2xi64>) -> tensor<1x6x2xi32> +// CHECK: %[[VAL_10:.*]] = tfl.add(%[[VAL_4]], %[[VAL_9]]) <{fused_activation_function = "NONE"}> : (tensor<2x1x2xi32>, tensor<1x6x2xi32>) -> tensor<2x6x2xi32> +// CHECK: %[[VAL_11:.*]] = "tfl.gather_nd"(%[[VAL_2]], %[[VAL_10]]) : (tensor<4x6x5xi32>, tensor<2x6x2xi32>) -> tensor<2x6x5xi32> +// CHECK-DAG: %[[CST_7:.*]] = arith.constant dense<[2, 1, 6, 5]> : tensor<4xi64> +// CHECK: %[[VAL_12:.*]] = "tfl.cast"(%[[CST_7]]) : (tensor<4xi64>) -> tensor<4xi32> +// CHECK: %[[VAL_13:.*]] = "tfl.reshape"(%[[VAL_11]], %[[VAL_12]]) : (tensor<2x6x5xi32>, tensor<4xi32>) -> tensor<2x1x6x5xi32> +// CHECK: %[[VAL_14:.*]] = "tfl.pseudo_const"() <{value = dense<[0, 1, 3, 2]> : tensor<4xi64>}> : () -> tensor<4xi64> +// CHECK: %[[VAL_15:.*]] = "tfl.cast"(%[[VAL_14]]) : (tensor<4xi64>) -> tensor<4xi32> +// CHECK: %[[VAL_16:.*]] = "tfl.transpose"(%[[VAL_13]], %[[VAL_15]]) : (tensor<2x1x6x5xi32>, tensor<4xi32>) -> tensor<2x1x5x6xi32> + +// ----- + +// CHECK-LABEL: gather_to_slice_batch_size_1 +func.func @gather_to_slice_batch_size_1(%arg0: tensor<1x2944xi32>, %arg1: tensor<1x2xi32>) -> tensor<1x1504xi32> { + %0 = "mhlo.gather"(%arg0, %arg1) { + dimension_numbers = #mhlo.gather< + offset_dims = [1], + collapsed_slice_dims = [0], + start_index_map = [0, 1], + index_vector_dim = 1, + >, + indices_are_sorted = true, + slice_sizes = dense<[1, 1504]> : tensor<2xi64> + } : (tensor<1x2944xi32>, tensor<1x2xi32>) -> tensor<1x1504xi32> + func.return %0 : tensor<1x1504xi32> +} + +// CHECK-DAG: %[[CST:.*]] = arith.constant dense<[0, 1440]> : tensor<2xi32> +// CHECK-DAG: %[[CST_0:.*]] = arith.constant dense<0> : tensor<2xi32> +// CHECK: %[[VAL_0:.*]] = "tfl.maximum"(%arg1, %[[CST_0]]) : (tensor<1x2xi32>, tensor<2xi32>) -> tensor<1x2xi32> +// CHECK: %[[VAL_1:.*]] = "tfl.minimum"(%[[VAL_0]], %[[CST]]) : (tensor<1x2xi32>, tensor<2xi32>) -> tensor<1x2xi32> +// CHECK-DAG: %[[CST_1:.*]] = arith.constant dense<[1, 1504]> : tensor<2xi32> +// CHECK: %[[VAL_2:.*]] = "tfl.squeeze"(%[[VAL_1]]) <{squeeze_dims = [0]}> : (tensor<1x2xi32>) -> tensor<2xi32> +// CHECK: %[[VAL_3:.*]] = "tfl.slice"(%arg0, %[[VAL_2]], %[[CST_1]]) : (tensor<1x2944xi32>, tensor<2xi32>, tensor<2xi32>) -> tensor<1x1504xi32> + +// ----- + +// CHECK-LABEL: gather_slice_dynamic_indices +func.func @gather_slice_dynamic_indices(%arg0: tensor<256000x1024xi8>, %arg1: tensor) -> tensor { + %0 = "mhlo.gather"(%arg0, %arg1) { + dimension_numbers = #mhlo.gather< + offset_dims = [2], + collapsed_slice_dims = [0], + start_index_map = [0], + index_vector_dim = 2 + >, + slice_sizes = dense<[1, 1024]> : tensor<2xi64> + } : (tensor<256000x1024xi8>, tensor) -> tensor + func.return %0 : tensor +} + +// CHECK: %0 = "tfl.gather_nd"(%arg0, %arg1) : (tensor<256000x1024xi8>, tensor) -> tensor + +// ----- + +// CHECK-LABEL: gather_scalar_dynamic_indices +func.func @gather_scalar_dynamic_indices(%arg0: tensor<256000xf32>, %arg1: tensor) -> tensor { + %0 = "mhlo.gather"(%arg0, %arg1) { + dimension_numbers = #mhlo.gather< + collapsed_slice_dims = [0], + start_index_map = [0], + index_vector_dim = 2 + >, + slice_sizes = dense<1> : tensor<1xi64> + } : (tensor<256000xf32>, tensor) -> tensor + func.return %0 : tensor +} + +// CHECK: %0 = "tfl.gather_nd"(%arg0, %arg1) : (tensor<256000xf32>, tensor) -> tensor + +// ----- + +//===------------------------------------------------------------------------=== +// mhlo.reduce_window -> avg pool +//===------------------------------------------------------------------------=== + +// CHECK-LABEL: avgpool_same_channel_first +func.func @avgpool_same_channel_first(%arg0: tensor<4x3x16x16xf32>) -> tensor<4x3x8x8xf32> { + %0 = mhlo.constant dense<1.000000e+00> : tensor<4x16x16x3xf32> + %1 = mhlo.constant dense<0.000000e+00> : tensor + %2 = "mhlo.transpose"(%arg0) <{permutation = dense<[0, 2, 3, 1]> : tensor<4xi64>}> : (tensor<4x3x16x16xf32>) -> tensor<4x16x16x3xf32> + %3 = "mhlo.reduce_window"(%2, %1) <{base_dilations = dense<1> : tensor<4xi64>, padding = dense<[[0, 0], [0, 1], [0, 1], [0, 0]]> : tensor<4x2xi64>, window_dilations = dense<1> : tensor<4xi64>, window_dimensions = dense<[1, 3, 3, 1]> : tensor<4xi64>, window_strides = dense<[1, 2, 2, 1]> : tensor<4xi64>}> ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %8 = mhlo.add %arg1, %arg2 : tensor + mhlo.return %8 : tensor + }) : (tensor<4x16x16x3xf32>, tensor) -> tensor<4x8x8x3xf32> + %4 = "mhlo.transpose"(%3) <{permutation = dense<[0, 3, 1, 2]> : tensor<4xi64>}> : (tensor<4x8x8x3xf32>) -> tensor<4x3x8x8xf32> + %5 = "mhlo.reduce_window"(%0, %1) <{base_dilations = dense<1> : tensor<4xi64>, padding = dense<[[0, 0], [0, 1], [0, 1], [0, 0]]> : tensor<4x2xi64>, window_dilations = dense<1> : tensor<4xi64>, window_dimensions = dense<[1, 3, 3, 1]> : tensor<4xi64>, window_strides = dense<[1, 2, 2, 1]> : tensor<4xi64>}> ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %8 = mhlo.add %arg1, %arg2 : tensor + mhlo.return %8 : tensor + }) : (tensor<4x16x16x3xf32>, tensor) -> tensor<4x8x8x3xf32> + %6 = "mhlo.transpose"(%5) <{permutation = dense<[0, 3, 1, 2]> : tensor<4xi64>}> : (tensor<4x8x8x3xf32>) -> tensor<4x3x8x8xf32> + %7 = mhlo.divide %4, %6 : tensor<4x3x8x8xf32> + return %7 : tensor<4x3x8x8xf32> +} + +// CHECK: %[[TPOSED_ARG0:.*]] = "tfl.transpose"(%arg0 +// CHECK-SAME: (tensor<4x3x16x16xf32>, tensor<4xi32>) -> tensor<4x16x16x3xf32> +// CHECK: %[[POOL_OUT:.*]] = "tfl.average_pool_2d"(%[[TPOSED_ARG0]]) <{filter_height = 3 : i32, filter_width = 3 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 2 : i32, stride_w = 2 : i32}> : (tensor<4x16x16x3xf32>) -> tensor<4x8x8x3xf32> +// CHECK: %[[TPOSED_OUT:.*]] = "tfl.transpose"(%[[POOL_OUT]] +// CHECK-SAME: (tensor<4x8x8x3xf32>, tensor<4xi32>) -> tensor<4x3x8x8xf32> +// CHECK: return %[[TPOSED_OUT]] + +// ----- + +// CHECK-LABEL: avgpool_valid_channel_first +func.func @avgpool_valid_channel_first(%arg0: tensor<4x3x16x16xf32>) -> tensor<4x3x7x7xf32> { + %0 = mhlo.constant dense<9.000000e+00> : tensor<4x3x7x7xf32> + %1 = mhlo.constant dense<0.000000e+00> : tensor + %2 = "mhlo.transpose"(%arg0) <{permutation = dense<[0, 2, 3, 1]> : tensor<4xi64>}> : (tensor<4x3x16x16xf32>) -> tensor<4x16x16x3xf32> + %3 = "mhlo.reduce_window"(%2, %1) <{base_dilations = dense<1> : tensor<4xi64>, padding = dense<0> : tensor<4x2xi64>, window_dilations = dense<1> : tensor<4xi64>, window_dimensions = dense<[1, 3, 3, 1]> : tensor<4xi64>, window_strides = dense<[1, 2, 2, 1]> : tensor<4xi64>}> ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %6 = mhlo.add %arg1, %arg2 : tensor + mhlo.return %6 : tensor + }) : (tensor<4x16x16x3xf32>, tensor) -> tensor<4x7x7x3xf32> + %4 = "mhlo.transpose"(%3) <{permutation = dense<[0, 3, 1, 2]> : tensor<4xi64>}> : (tensor<4x7x7x3xf32>) -> tensor<4x3x7x7xf32> + %5 = mhlo.divide %4, %0 : tensor<4x3x7x7xf32> + return %5 : tensor<4x3x7x7xf32> +} + +// CHECK: %[[TPOSED_ARG0:.*]] = "tfl.transpose"(%arg0 +// CHECK-SAME: (tensor<4x3x16x16xf32>, tensor<4xi32>) -> tensor<4x16x16x3xf32> +// CHECK: %[[POOL_OUT:.*]] = "tfl.average_pool_2d"(%[[TPOSED_ARG0]]) <{filter_height = 3 : i32, filter_width = 3 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 2 : i32, stride_w = 2 : i32}> : (tensor<4x16x16x3xf32>) -> tensor<4x7x7x3xf32> +// CHECK: %[[TPOSED_OUT:.*]] = "tfl.transpose"(%[[POOL_OUT]] +// CHECK-SAME: (tensor<4x7x7x3xf32>, tensor<4xi32>) -> tensor<4x3x7x7xf32> +// CHECK: return %[[TPOSED_OUT]] + +// ----- + +func.func @avgpool_valid(%arg0: tensor<4x16x16x8xf32>) -> tensor<4x7x7x8xf32> { + %0 = mhlo.constant dense<0.0> : tensor + %1 = mhlo.constant dense<9.0> : tensor<4x7x7x8xf32> + %2 = "mhlo.reduce_window"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %5 = mhlo.add %arg1, %arg2 : tensor + "mhlo.return"(%5) : (tensor) -> () + }) { + base_dilations = dense<1> : tensor<4xi64>, + padding = dense<0> : tensor<4x2xi64>, + window_dilations = dense<1> : tensor<4xi64>, + window_dimensions = dense<[1, 3, 3, 1]> : tensor<4xi64>, + window_strides = dense<[1, 2, 2, 1]> : tensor<4xi64>} : (tensor<4x16x16x8xf32>, tensor) -> tensor<4x7x7x8xf32> + %3 = mhlo.divide %2, %1 : tensor<4x7x7x8xf32> + func.return %3 : tensor<4x7x7x8xf32> +} + +// CHECK: %[[POOL_OUT:.*]] = "tfl.average_pool_2d"(%arg0) <{filter_height = 3 : i32, filter_width = 3 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 2 : i32, stride_w = 2 : i32}> : (tensor<4x16x16x8xf32>) -> tensor<4x7x7x8xf32> +// CHECK: return %[[POOL_OUT]] + +// ----- + +// CHECK-LABEL: avgpool_valid_broadcasted_divisor +func.func @avgpool_valid_broadcasted_divisor(%arg0: tensor<4x16x16x8xf32>) -> tensor<4x7x7x8xf32> { + %0 = mhlo.constant dense<0.0> : tensor + %1 = mhlo.constant dense<9.0> : tensor + %2 = "mhlo.broadcast_in_dim"(%1) <{broadcast_dimensions = dense<> : tensor<0xi64>}> : (tensor) -> tensor<4x7x7x8xf32> + %3 = "mhlo.reduce_window"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %5 = mhlo.add %arg1, %arg2 : tensor + "mhlo.return"(%5) : (tensor) -> () + }) { + base_dilations = dense<1> : tensor<4xi64>, + padding = dense<0> : tensor<4x2xi64>, + window_dilations = dense<1> : tensor<4xi64>, + window_dimensions = dense<[1, 3, 3, 1]> : tensor<4xi64>, + window_strides = dense<[1, 2, 2, 1]> : tensor<4xi64>} : (tensor<4x16x16x8xf32>, tensor) -> tensor<4x7x7x8xf32> + %4 = mhlo.divide %3, %2 : tensor<4x7x7x8xf32> + func.return %4 : tensor<4x7x7x8xf32> +} + +// CHECK: %[[POOL_OUT:.*]] = "tfl.average_pool_2d"(%arg0) <{filter_height = 3 : i32, filter_width = 3 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 2 : i32, stride_w = 2 : i32}> : (tensor<4x16x16x8xf32>) -> tensor<4x7x7x8xf32> +// CHECK: return %[[POOL_OUT]] + +// ----- + +// CHECK-LABEL: avgpool_valid_rw +func.func @avgpool_valid_rw(%arg0: tensor<4x16x16x8xf32>) -> tensor<4x7x7x8xf32> { + %0 = mhlo.constant dense<1.0> : tensor<4x16x16x8xf32> + %1 = mhlo.constant dense<0.0> : tensor + %2 = "mhlo.reduce_window"(%arg0, %1) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %6 = mhlo.add %arg1, %arg2 : tensor + "mhlo.return"(%6) : (tensor) -> () + }) { + base_dilations = dense<1> : tensor<4xi64>, + padding = dense<[[0, 0], [0, 0], [0, 0], [0, 0]]> : tensor<4x2xi64>, + window_dilations = dense<1> : tensor<4xi64>, + window_dimensions = dense<[1, 3, 3, 1]> : tensor<4xi64>, + window_strides = dense<[1, 2, 2, 1]> : tensor<4xi64>} : (tensor<4x16x16x8xf32>, tensor) -> tensor<4x7x7x8xf32> + %3 = "mhlo.reduce_window"(%0, %1) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %6 = mhlo.add %arg1, %arg2 : tensor + "mhlo.return"(%6) : (tensor) -> () + }) { + base_dilations = dense<1> : tensor<4xi64>, + padding = dense<[[0, 0], [0, 0], [0, 0], [0, 0]]> : tensor<4x2xi64>, + window_dilations = dense<1> : tensor<4xi64>, + window_dimensions = dense<[1, 3, 3, 1]> : tensor<4xi64>, + window_strides = dense<[1, 2, 2, 1]> : tensor<4xi64>} : (tensor<4x16x16x8xf32>, tensor) -> tensor<4x7x7x8xf32> + %4 = mhlo.divide %2, %3 : tensor<4x7x7x8xf32> + func.return %4 : tensor<4x7x7x8xf32> +} + +// CHECK: %[[POOL_OUT:.*]] = "tfl.average_pool_2d"(%arg0) <{filter_height = 3 : i32, filter_width = 3 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 2 : i32, stride_w = 2 : i32}> : (tensor<4x16x16x8xf32>) -> tensor<4x7x7x8xf32> +// CHECK: return %[[POOL_OUT]] + +// ----- + +// CHECK-LABEL: avgpool_valid_rw_broadcasted_const_lhs +func.func @avgpool_valid_rw_broadcasted_const_lhs(%arg0: tensor<4x16x16x8xf32>) -> tensor<4x7x7x8xf32> { + %0 = mhlo.constant dense<1.0> : tensor + %1 = "mhlo.broadcast_in_dim"(%0) <{broadcast_dimensions = dense<> : tensor<0xi64>}> : (tensor) -> tensor<4x16x16x8xf32> + %2 = mhlo.constant dense<0.0> : tensor + %3 = "mhlo.reduce_window"(%arg0, %2) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %6 = mhlo.add %arg1, %arg2 : tensor + "mhlo.return"(%6) : (tensor) -> () + }) { + base_dilations = dense<1> : tensor<4xi64>, + padding = dense<[[0, 0], [0, 0], [0, 0], [0, 0]]> : tensor<4x2xi64>, + window_dilations = dense<1> : tensor<4xi64>, + window_dimensions = dense<[1, 3, 3, 1]> : tensor<4xi64>, + window_strides = dense<[1, 2, 2, 1]> : tensor<4xi64>} : (tensor<4x16x16x8xf32>, tensor) -> tensor<4x7x7x8xf32> + %4 = "mhlo.reduce_window"(%1, %2) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %6 = mhlo.add %arg1, %arg2 : tensor + "mhlo.return"(%6) : (tensor) -> () + }) { + base_dilations = dense<1> : tensor<4xi64>, + padding = dense<[[0, 0], [0, 0], [0, 0], [0, 0]]> : tensor<4x2xi64>, + window_dilations = dense<1> : tensor<4xi64>, + window_dimensions = dense<[1, 3, 3, 1]> : tensor<4xi64>, + window_strides = dense<[1, 2, 2, 1]> : tensor<4xi64>} : (tensor<4x16x16x8xf32>, tensor) -> tensor<4x7x7x8xf32> + %5 = mhlo.divide %3, %4 : tensor<4x7x7x8xf32> + func.return %5 : tensor<4x7x7x8xf32> +} + +// CHECK: %[[POOL_OUT:.*]] = "tfl.average_pool_2d"(%arg0) <{filter_height = 3 : i32, filter_width = 3 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 2 : i32, stride_w = 2 : i32}> : (tensor<4x16x16x8xf32>) -> tensor<4x7x7x8xf32> +// CHECK: return %[[POOL_OUT]] + +// ----- + +// CHECK-LABEL: avgpool_same +func.func @avgpool_same(%arg0: tensor<4x16x16x8xf32>) -> tensor<4x8x8x8xf32> { + %0 = mhlo.constant dense<1.0> : tensor<4x16x16x8xf32> + %1 = mhlo.constant dense<0.0> : tensor + %2 = "mhlo.reduce_window"(%arg0, %1) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %6 = mhlo.add %arg1, %arg2 : tensor + "mhlo.return"(%6) : (tensor) -> () + }) { + base_dilations = dense<1> : tensor<4xi64>, + padding = dense<[[0, 0], [0, 1], [0, 1], [0, 0]]> : tensor<4x2xi64>, + window_dilations = dense<1> : tensor<4xi64>, + window_dimensions = dense<[1, 3, 3, 1]> : tensor<4xi64>, + window_strides = dense<[1, 2, 2, 1]> : tensor<4xi64>} : (tensor<4x16x16x8xf32>, tensor) -> tensor<4x8x8x8xf32> + %3 = "mhlo.reduce_window"(%0, %1) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %6 = mhlo.add %arg1, %arg2 : tensor + "mhlo.return"(%6) : (tensor) -> () + }) { + base_dilations = dense<1> : tensor<4xi64>, + padding = dense<[[0, 0], [0, 1], [0, 1], [0, 0]]> : tensor<4x2xi64>, + window_dilations = dense<1> : tensor<4xi64>, + window_dimensions = dense<[1, 3, 3, 1]> : tensor<4xi64>, + window_strides = dense<[1, 2, 2, 1]> : tensor<4xi64>} : (tensor<4x16x16x8xf32>, tensor) -> tensor<4x8x8x8xf32> + %4 = mhlo.divide %2, %3 : tensor<4x8x8x8xf32> + func.return %4 : tensor<4x8x8x8xf32> +} + +// CHECK: %[[POOL_OUT:.*]] = "tfl.average_pool_2d"(%arg0) <{filter_height = 3 : i32, filter_width = 3 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 2 : i32, stride_w = 2 : i32}> : (tensor<4x16x16x8xf32>) -> tensor<4x8x8x8xf32> +// CHECK: return %[[POOL_OUT]] + +// ----- + +// CHECK-LABEL: avgpool_reshape_broadcast +func.func @avgpool_reshape_broadcast(%arg0: tensor<4x16x16x8xf32>) -> tensor<4x8x8x8xf32> { + %0 = mhlo.constant dense<1.000000e+00> : tensor<1x16x16x1xf32> + %1 = mhlo.constant dense<0.000000e+00> : tensor + %2 = "mhlo.reduce_window"(%arg0, %1) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %7 = mhlo.add %arg1, %arg2 : tensor + mhlo.return %7 : tensor + }) {base_dilations = dense<1> : tensor<4xi64>, padding = dense<[[0, 0], [0, 1], [0, 1], [0, 0]]> : tensor<4x2xi64>, window_dilations = dense<1> : tensor<4xi64>, window_dimensions = dense<[1, 3, 3, 1]> : tensor<4xi64>, window_strides = dense<[1, 2, 2, 1]> : tensor<4xi64>} : (tensor<4x16x16x8xf32>, tensor) -> tensor<4x8x8x8xf32> + %3 = "mhlo.reduce_window"(%0, %1) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %7 = mhlo.add %arg1, %arg2 : tensor + mhlo.return %7 : tensor + }) {base_dilations = dense<1> : tensor<4xi64>, padding = dense<[[0, 0], [0, 1], [0, 1], [0, 0]]> : tensor<4x2xi64>, window_dilations = dense<1> : tensor<4xi64>, window_dimensions = dense<[1, 3, 3, 1]> : tensor<4xi64>, window_strides = dense<[1, 2, 2, 1]> : tensor<4xi64>} : (tensor<1x16x16x1xf32>, tensor) -> tensor<1x8x8x1xf32> + %4 = mhlo.reshape %3 : (tensor<1x8x8x1xf32>) -> tensor<8x8xf32> + %5 = "mhlo.broadcast_in_dim"(%4) <{broadcast_dimensions = dense<[1, 2]> : tensor<2xi64>}> : (tensor<8x8xf32>) -> tensor<4x8x8x8xf32> + %6 = mhlo.divide %2, %5 : tensor<4x8x8x8xf32> + return %6 : tensor<4x8x8x8xf32> +} + +// CHECK: %[[POOL_OUT:.*]] = "tfl.average_pool_2d"(%arg0) <{filter_height = 3 : i32, filter_width = 3 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 2 : i32, stride_w = 2 : i32}> : (tensor<4x16x16x8xf32>) -> tensor<4x8x8x8xf32> +// CHECK: return %[[POOL_OUT]] + +// ----- + +//===------------------------------------------------------------------------=== +// mhlo.reduce_window -> max pool +//===------------------------------------------------------------------------=== + +// CHECK-LABEL: maxpool_same +func.func @maxpool_same(%arg0: tensor<4x16x16x8xf32>) -> tensor<4x8x8x8xf32> { + // "0xFF800000" represents -INF for f32. + %0 = mhlo.constant dense<0xFF800000> : tensor + %1 = "mhlo.reduce_window"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %6 = mhlo.maximum %arg1, %arg2 : tensor + "mhlo.return"(%6) : (tensor) -> () + }) { + base_dilations = dense<1> : tensor<4xi64>, + padding = dense<[[0, 0], [0, 1], [0, 1], [0, 0]]> : tensor<4x2xi64>, + window_dilations = dense<1> : tensor<4xi64>, + window_dimensions = dense<[1, 3, 3, 1]> : tensor<4xi64>, + window_strides = dense<[1, 2, 2, 1]> : tensor<4xi64>} : (tensor<4x16x16x8xf32>, tensor) -> tensor<4x8x8x8xf32> + func.return %1 : tensor<4x8x8x8xf32> +} + +// CHECK: "tfl.max_pool_2d"(%arg0) <{filter_height = 3 : i32, filter_width = 3 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 2 : i32, stride_w = 2 : i32}> : (tensor<4x16x16x8xf32>) -> tensor<4x8x8x8xf32> + +// ----- + +// CHECK-LABEL: maxpool_explicit +func.func @maxpool_explicit(%arg0: tensor<1x112x112x64xf32>) -> tensor<1x56x56x64xf32> { + // "0xFF800000" represents -INF for f32. + %0 = mhlo.constant dense<0xFF800000> : tensor + %1 = "mhlo.reduce_window"(%arg0, %0)({ + ^bb0(%arg1: tensor, %arg2: tensor): + %2 = mhlo.maximum %arg1, %arg2 : (tensor, tensor) -> tensor + mhlo.return %2 : tensor + }) { + padding = dense<[[0, 0], [1, 1], [1, 1], [0, 0]]> : tensor<4x2xi64>, + window_dimensions = dense<[1, 3, 3, 1]> : tensor<4xi64>, + window_strides = dense<[1, 2, 2, 1]> : tensor<4xi64> } : (tensor<1x112x112x64xf32>, tensor) -> tensor<1x56x56x64xf32> + func.return %1 : tensor<1x56x56x64xf32> +} + +// CHECK: %[[PADDING_CONST:.*]] = arith.constant dense<0xFF800000> : tensor +// CHECK: %[[PADDING:.*]] = arith.constant dense<{{\[\[}}0, 0], [1, 1], [1, 1], [0, 0]]> : tensor<4x2xi64> +// CHECK: %[[PAD_V2_OUT:.*]] = "tfl.padv2"(%arg0, %[[PADDING]], %[[PADDING_CONST]]) : (tensor<1x112x112x64xf32>, tensor<4x2xi64>, tensor) -> tensor<1x114x114x64xf32> +// CHECK: "tfl.max_pool_2d"(%[[PAD_V2_OUT]]) <{filter_height = 3 : i32, filter_width = 3 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 2 : i32, stride_w = 2 : i32}> : (tensor<1x114x114x64xf32>) -> tensor<1x56x56x64xf32> + + +// ----- + +// CHECK-LABEL: maxpool_valid +func.func @maxpool_valid(%arg0: tensor<4x16x16x8xf32>) -> tensor<4x7x7x8xf32> { + // "0xFF800000" represents -INF for f32. + %0 = mhlo.constant dense<0xFF800000> : tensor + %1 = "mhlo.reduce_window"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %6 = mhlo.maximum %arg1, %arg2 : tensor + "mhlo.return"(%6) : (tensor) -> () + }) { + base_dilations = dense<1> : tensor<4xi64>, + padding = dense<[[0, 0], [0, 0], [0, 0], [0, 0]]> : tensor<4x2xi64>, + window_dilations = dense<1> : tensor<4xi64>, + window_dimensions = dense<[1, 3, 3, 1]> : tensor<4xi64>, + window_strides = dense<[1, 2, 2, 1]> : tensor<4xi64>} : (tensor<4x16x16x8xf32>, tensor) -> tensor<4x7x7x8xf32> + func.return %1 : tensor<4x7x7x8xf32> +} + +// CHECK: "tfl.max_pool_2d"(%arg0) <{filter_height = 3 : i32, filter_width = 3 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 2 : i32, stride_w = 2 : i32}> : (tensor<4x16x16x8xf32>) -> tensor<4x7x7x8xf32> + +// ----- + +// CHECK-LABEL: maxpool_valid_channel_first +func.func @maxpool_valid_channel_first(%arg0: tensor<4x3x16x16xf32>) -> tensor<4x3x7x7xf32> { + // "0xFF800000" represents -INF for f32. + %0 = mhlo.constant dense<0xFF800000> : tensor + %1 = "mhlo.transpose"(%arg0) <{permutation = dense<[0, 2, 3, 1]> : tensor<4xi64>}> : (tensor<4x3x16x16xf32>) -> tensor<4x16x16x3xf32> + %2 = "mhlo.reduce_window"(%1, %0) <{base_dilations = dense<1> : tensor<4xi64>, padding = dense<0> : tensor<4x2xi64>, window_dilations = dense<1> : tensor<4xi64>, window_dimensions = dense<[1, 3, 3, 1]> : tensor<4xi64>, window_strides = dense<[1, 2, 2, 1]> : tensor<4xi64>}> ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %4 = mhlo.maximum %arg1, %arg2 : tensor + mhlo.return %4 : tensor + }) : (tensor<4x16x16x3xf32>, tensor) -> tensor<4x7x7x3xf32> + %3 = "mhlo.transpose"(%2) <{permutation = dense<[0, 3, 1, 2]> : tensor<4xi64>}> : (tensor<4x7x7x3xf32>) -> tensor<4x3x7x7xf32> + return %3 : tensor<4x3x7x7xf32> +} + +// CHECK: %[[TPOSED_ARG0:.*]] = "tfl.transpose"(%arg0 +// CHECK: "tfl.max_pool_2d"(%[[TPOSED_ARG0]]) <{filter_height = 3 : i32, filter_width = 3 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 2 : i32, stride_w = 2 : i32}> : (tensor<4x16x16x3xf32>) -> tensor<4x7x7x3xf32> +// CHECK: return +// CHECK-SAME: tensor<4x3x7x7xf32> + +// ----- + +// CHECK-LABEL: maxpool_same_channel_first +func.func @maxpool_same_channel_first(%arg0: tensor<4x3x16x16xf32>) -> tensor<4x3x8x8xf32> { + // "0xFF800000" represents -INF for f32. + %0 = mhlo.constant dense<0xFF800000> : tensor + %1 = "mhlo.transpose"(%arg0) <{permutation = dense<[0, 2, 3, 1]> : tensor<4xi64>}> : (tensor<4x3x16x16xf32>) -> tensor<4x16x16x3xf32> + %2 = "mhlo.reduce_window"(%1, %0) <{base_dilations = dense<1> : tensor<4xi64>, padding = dense<[[0, 0], [0, 1], [0, 1], [0, 0]]> : tensor<4x2xi64>, window_dilations = dense<1> : tensor<4xi64>, window_dimensions = dense<[1, 3, 3, 1]> : tensor<4xi64>, window_strides = dense<[1, 2, 2, 1]> : tensor<4xi64>}> ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %4 = mhlo.maximum %arg1, %arg2 : tensor + mhlo.return %4 : tensor + }) : (tensor<4x16x16x3xf32>, tensor) -> tensor<4x8x8x3xf32> + %3 = "mhlo.transpose"(%2) <{permutation = dense<[0, 3, 1, 2]> : tensor<4xi64>}> : (tensor<4x8x8x3xf32>) -> tensor<4x3x8x8xf32> + return %3 : tensor<4x3x8x8xf32> +} + +// CHECK: %[[TPOSED_ARG0:.*]] = "tfl.transpose"(%arg0 +// CHECK: "tfl.max_pool_2d"(%[[TPOSED_ARG0]]) <{filter_height = 3 : i32, filter_width = 3 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 2 : i32, stride_w = 2 : i32}> : (tensor<4x16x16x3xf32>) -> tensor<4x8x8x3xf32> +// CHECK: return +// CHECK-SAME: tensor<4x3x8x8xf32> + +// ----- + +//===------------------------------------------------------------------------=== +// mhlo.reduce_window -> tfl.cumsum +//===------------------------------------------------------------------------=== + +// CHECK-LABEL: reduce_window_sum +func.func @reduce_window_sum(%arg0: tensor<4x12xf32>) -> tensor<4x12xf32> { + %0 = mhlo.constant dense<0.000000e+00> : tensor + %1 = "mhlo.reduce_window"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor): + %2 = mhlo.add %arg1, %arg2 : tensor + "mhlo.return"(%2) : (tensor) -> () + }) {base_dilations = dense<1> : tensor<2xi64>, padding = dense<[[3, 0], [0, 0]]> : tensor<2x2xi64>, window_dilations = dense<1> : tensor<2xi64>, window_dimensions = dense<[4, 1]> : tensor<2xi64>, window_strides = dense<1> : tensor<2xi64>} : (tensor<4x12xf32>, tensor) -> tensor<4x12xf32> + func.return %1 : tensor<4x12xf32> +} + +// CHECK: %[[AXIS:.*]] = arith.constant dense<0> : tensor +// CHECK: "tfl.cumsum"(%arg0, %[[AXIS]]) <{exclusive = false, reverse = false}> : (tensor<4x12xf32>, tensor) -> tensor<4x12xf32> + +// ----- + +//===----------------------------------------------------------------------===// +// mhlo.slice +//===----------------------------------------------------------------------===// + +// CHECK-LABEL: slice +func.func @slice(%arg0: tensor<1x4672xf32>) -> tensor<1x519xf32> { + %0 = "mhlo.slice"(%arg0) <{limit_indices = dense<[1, 4672]> : tensor<2xi64>, start_indices = dense<[0, 4153]> : tensor<2xi64>, strides = dense<1> : tensor<2xi64>}> : (tensor<1x4672xf32>) -> tensor<1x519xf32> + func.return %0 : tensor<1x519xf32> +} + +// CHECK: %[[CST:.*]] = arith.constant dense<[0, 4153]> : tensor<2xi32> +// CHECK: %[[CST_0:.*]] = arith.constant dense<[1, 4672]> : tensor<2xi32> +// CHECK: %[[CST_1:.*]] = arith.constant dense<1> : tensor<2xi32> +// CHECK: %[[VAL_3:.*]] = "tfl.strided_slice"(%arg0, %[[CST]], %[[CST_0]], %[[CST_1]]) <{begin_mask = 0 : i32, ellipsis_mask = 0 : i32, end_mask = 0 : i32, new_axis_mask = 0 : i32, offset = false, shrink_axis_mask = 0 : i32}> : (tensor<1x4672xf32>, tensor<2xi32>, tensor<2xi32>, tensor<2xi32>) -> tensor<1x519xf32> + +// ----- + +// CHECK-LABEL: rank5_slice +func.func @rank5_slice(%arg0: tensor<16x192x48x1x2xf32>) -> tensor<16x192x48x1x1xf32> { + // CHECK: %[[CST:.*]] = arith.constant dense<0> : tensor<5xi32> + // CHECK: %[[CST_0:.*]] = arith.constant dense<[16, 192, 48, 1, 1]> : tensor<5xi32> + // CHECK: %[[CST_1:.*]] = arith.constant dense<1> : tensor<5xi32> + // CHECK: "tfl.strided_slice"(%arg0, %[[CST]], %[[CST_0]], %[[CST_1]]) + %0 = "mhlo.slice"(%arg0) { + start_indices = dense<0> : tensor<5xi64>, + limit_indices = dense<[16, 192, 48, 1, 1]> : tensor<5xi64>, + strides = dense<1> : tensor<5xi64> + } : (tensor<16x192x48x1x2xf32>) -> tensor<16x192x48x1x1xf32> + return %0 : tensor<16x192x48x1x1xf32> +} + +// ----- + +// CHECK-LABEL: dynamic_slice_mhlo +func.func @dynamic_slice_mhlo(%arg0: tensor) -> tensor { + // CHECK: %[[CST:.*]] = arith.constant dense<0> : tensor<2xi32> + // CHECK: %[[CST_0:.*]] = arith.constant dense<1> : tensor<2xi32> + // CHECK: %[[CST_1:.*]] = arith.constant dense<1> : tensor<2xi32> + // CHECK: "tfl.strided_slice"(%arg0, %[[CST]], %[[CST_0]], %[[CST_1]]) + %0 = "mhlo.slice"(%arg0) { + start_indices = dense<0> : tensor<2xi64>, + limit_indices = dense<1> : tensor<2xi64>, + strides = dense<1> : tensor<2xi64> + } : (tensor) -> tensor + return %0 : tensor +} + +// ----- + +// CHECK-LABEL: rank6_slice_collapse +func.func @rank6_slice_collapse(%arg0: tensor<1x16x192x48x1x2xf32>) -> tensor<1x16x192x48x1x1xf32> { + // CHECK: %[[RESHAPE_IN:.*]] = "tfl.reshape"(%arg0, %{{.*}}) : (tensor<1x16x192x48x1x2xf32>, tensor<5xi32>) -> tensor<16x192x48x1x2xf32> + // CHECK: %[[SLICE:.*]] = "tfl.strided_slice"(%[[RESHAPE_IN]], %{{.*}}, %{{.*}}, %{{.*}}) {{.*}} (tensor<16x192x48x1x2xf32>, tensor<5xi32>, tensor<5xi32>, tensor<5xi32>) -> tensor<16x192x48x1x1xf32> + // CHECK: %[[RESHAPE_OUT:.*]] = "tfl.reshape"(%[[SLICE]], %{{.*}}) : (tensor<16x192x48x1x1xf32>, tensor<6xi32>) -> tensor<1x16x192x48x1x1xf32> + %cst = arith.constant dense<0> : tensor<6xi32> + %cst_0 = arith.constant dense<[1, 16, 192, 48, 1, 1]> : tensor<6xi32> + %cst_1 = arith.constant dense<1> : tensor<6xi32> + %0 = "tfl.strided_slice"(%arg0, %cst, %cst_0, %cst_1) <{begin_mask = 0 : i32, ellipsis_mask = 0 : i32, end_mask = 0 : i32, new_axis_mask = 0 : i32, offset = false, shrink_axis_mask = 0 : i32}> : (tensor<1x16x192x48x1x2xf32>, tensor<6xi32>, tensor<6xi32>, tensor<6xi32>) -> tensor<1x16x192x48x1x1xf32> + return %0 : tensor<1x16x192x48x1x1xf32> +} + +// ----- + +//===----------------------------------------------------------------------===// +// mhlo.real_dynamic_slice +//===----------------------------------------------------------------------===// + +func.func @real_dynamic_slice(%arg0: tensor<1x?x4x256xf32>, %arg1: tensor<4xi32>, %arg2: tensor<4xi32>) -> tensor<1x?x4x128xf32> { + %cst = mhlo.constant dense<1> : tensor<4xi32> + %0 = mhlo.real_dynamic_slice %arg0, %arg1, %arg2, %cst : (tensor<1x?x4x256xf32>, tensor<4xi32>, tensor<4xi32>, tensor<4xi32>) -> tensor<1x?x4x128xf32> + func.return %0 : tensor<1x?x4x128xf32> +} + +// CHECK: "tfl.strided_slice"(%arg0, %{{.*}}, %{{.*}}, %{{.*}}) <{begin_mask = 0 : i32, ellipsis_mask = 0 : i32, end_mask = 0 : i32, new_axis_mask = 0 : i32, offset = false, shrink_axis_mask = 0 : i32}> : (tensor<1x?x4x256xf32>, tensor<4xi32>, tensor<4xi32>, tensor<4xi32>) -> tensor<1x?x4x128xf32> + +// ----- + +//===----------------------------------------------------------------------===// +// mhlo.sort +//===----------------------------------------------------------------------===// + +// CHECK-LABEL: sort_to_topk_iota_broadcast +func.func @sort_to_topk_iota_broadcast(%arg0: tensor<3x6xf32>) -> (tensor<3x6xf32>, tensor<3x6xi32>) { + %0 = "mhlo.iota"() <{ iota_dimension = 0 : i64 }> : () -> tensor<6xi32> + %1 = "mhlo.broadcast_in_dim"(%0) <{broadcast_dimensions = dense<[1]> : tensor<1xi64>, name = "broadcast.0"}> : (tensor<6xi32>) -> tensor<3x6xi32> + %2:2 = "mhlo.sort"(%arg0, %1) ({ + ^bb0(%arg1: tensor, %arg2: tensor, %arg3: tensor, %arg4: tensor): + %3 = "mhlo.compare"(%arg1, %arg2) {compare_type = #mhlo, comparison_direction = #mhlo} : (tensor, tensor) -> tensor + "mhlo.return"(%3) : (tensor) -> () + }) {dimension = 1 : i64, is_stable = true} : (tensor<3x6xf32>, tensor<3x6xi32>) -> (tensor<3x6xf32>, tensor<3x6xi32>) + func.return %2#0, %2#1 : tensor<3x6xf32>, tensor<3x6xi32> +} + +// CHECK: arith.constant dense<6> : tensor +// CHECK: %[[CST:.*]] = arith.constant dense<6> : tensor +// CHECK: %values, %indices = "tfl.topk_v2"(%arg0, %[[CST]]) : (tensor<3x6xf32>, tensor) -> (tensor<3x6xf32>, tensor<3x6xi32>) + +// ----- + +// CHECK-LABEL: sort_to_topk_iota_cst_broadcast +func.func @sort_to_topk_iota_cst_broadcast(%arg0: tensor<3x6xf32>) -> (tensor<3x6xf32>, tensor<3x6xi32>) { + %0 = mhlo.constant dense<[0, 1, 2, 3, 4, 5]> : tensor<6xi32> + %1 = "mhlo.broadcast_in_dim"(%0) <{broadcast_dimensions = dense<[1]> : tensor<1xi64>, name = "broadcast.0"}> : (tensor<6xi32>) -> tensor<3x6xi32> + %2:2 = "mhlo.sort"(%arg0, %1) ({ + ^bb0(%arg1: tensor, %arg2: tensor, %arg3: tensor, %arg4: tensor): + %3 = "mhlo.compare"(%arg1, %arg2) {compare_type = #mhlo, comparison_direction = #mhlo} : (tensor, tensor) -> tensor + "mhlo.return"(%3) : (tensor) -> () + }) {dimension = 1 : i64, is_stable = true} : (tensor<3x6xf32>, tensor<3x6xi32>) -> (tensor<3x6xf32>, tensor<3x6xi32>) + func.return %2#0, %2#1 : tensor<3x6xf32>, tensor<3x6xi32> +} + +// CHECK: %[[CST:.*]] = arith.constant dense<6> : tensor +// CHECK: %values, %indices = "tfl.topk_v2"(%arg0, %[[CST]]) : (tensor<3x6xf32>, tensor) -> (tensor<3x6xf32>, tensor<3x6xi32>) + +// ----- + +// CHECK-LABEL: sort_to_topk_const +func.func @sort_to_topk_const(%arg0: tensor<3x6xf32>) -> (tensor<3x6xf32>, tensor<3x6xi32>) { + %0 = mhlo.constant dense<[[0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5]]> : tensor<3x6xi32> + %1:2 = "mhlo.sort"(%arg0, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor, %arg3: tensor, %arg4: tensor): + %3 = "mhlo.compare"(%arg1, %arg2) {compare_type = #mhlo, comparison_direction = #mhlo} : (tensor, tensor) -> tensor + "mhlo.return"(%3) : (tensor) -> () + }) {dimension = 1 : i64, is_stable = true} : (tensor<3x6xf32>, tensor<3x6xi32>) -> (tensor<3x6xf32>, tensor<3x6xi32>) + func.return %1#0, %1#1 : tensor<3x6xf32>, tensor<3x6xi32> +} + +// CHECK: %[[CST:.*]] = arith.constant dense<6> : tensor +// CHECK: %values, %indices = "tfl.topk_v2"(%arg0, %[[CST]]) : (tensor<3x6xf32>, tensor) -> (tensor<3x6xf32>, tensor<3x6xi32> + +// ----- + +//===----------------------------------------------------------------------===// +// mhlo.iota +//===----------------------------------------------------------------------===// + +// CHECK-LABEL: iota_1d +func.func @iota_1d() -> tensor<123xf32> { + %0 = "mhlo.iota"() <{ iota_dimension = 0 : i64 }> : () -> tensor<123xf32> + func.return %0 : tensor<123xf32> +} + +// CHECK-DAG: %[[CST_1:.*]] = arith.constant dense<0.000000e+00> : tensor +// CHECK-DAG: %[[CST_2:.*]] = arith.constant dense<1.230000e+02> : tensor +// CHECK-DAG: %[[CST_3:.*]] = arith.constant dense<1.000000e+00> : tensor +// CHECK: "tfl.range"(%[[CST_1]], %[[CST_2]], %[[CST_3]]) : (tensor, tensor, tensor) -> tensor<123xf32> + +// ----- + +// CHECK-LABEL: iota_i64 +func.func @iota_i64() -> tensor<123xi64> { + %0 = "mhlo.iota"() <{ iota_dimension = 0 : i64 }> : () -> tensor<123xi64> + func.return %0 : tensor<123xi64> +} + +// CHECK-DAG: %[[CST_1:.*]] = arith.constant dense<0> : tensor +// CHECK-DAG: %[[CST_2:.*]] = arith.constant dense<123> : tensor +// CHECK-DAG: %[[CST_3:.*]] = arith.constant dense<1> : tensor +// CHECK: "tfl.range"(%[[CST_1]], %[[CST_2]], %[[CST_3]]) : (tensor, tensor, tensor) -> tensor<123xi64> + +// ----- + +// CHECK-LABEL: iota_i32 +func.func @iota_i32() -> tensor<123xi32> { + %0 = "mhlo.iota"() <{ iota_dimension = 0 : i64 }> : () -> tensor<123xi32> + func.return %0 : tensor<123xi32> +} + +// CHECK-DAG: %[[CST_1:.*]] = arith.constant dense<0> : tensor +// CHECK-DAG: %[[CST_2:.*]] = arith.constant dense<123> : tensor +// CHECK-DAG: %[[CST_3:.*]] = arith.constant dense<1> : tensor +// CHECK: "tfl.range"(%[[CST_1]], %[[CST_2]], %[[CST_3]]) : (tensor, tensor, tensor) -> tensor<123xi32> + +// ----- + +// CHECK-LABEL: iota_3d +func.func @iota_3d() -> tensor<5x7x9xi32> { + %0 = "mhlo.iota"() <{ iota_dimension = 1 : i64 }> : () -> tensor<5x7x9xi32> + func.return %0 : tensor<5x7x9xi32> +} + +// CHECK-DAG: %[[CST_1:.*]] = arith.constant dense<0> : tensor +// CHECK-DAG: %[[CST_2:.*]] = arith.constant dense<7> : tensor +// CHECK-DAG: %[[CST_3:.*]] = arith.constant dense<1> : tensor +// CHECK: %[[RANGE:.*]] = "tfl.range"(%[[CST_1]], %[[CST_2]], %[[CST_3]]) : (tensor, tensor, tensor) -> tensor<7xi32> +// CHECK: %[[CST_4:.*]] = arith.constant dense<[1, 7, 1]> : tensor<3xi64> +// CHECK: %[[CAST:.*]] = "tfl.cast"(%[[CST_4]]) : (tensor<3xi64>) -> tensor<3xi32> +// CHECK: %[[RESHAPE:.*]] = "tfl.reshape"(%[[RANGE]], %[[CAST]]) : (tensor<7xi32>, tensor<3xi32>) -> tensor<1x7x1xi32> +// CHECK: %[[CST_5:.*]] = arith.constant dense<[5, 7, 9]> : tensor<3xi64> +// CHECK: "tfl.broadcast_to"(%[[RESHAPE]], %[[CST_5]]) : (tensor<1x7x1xi32>, tensor<3xi64>) -> tensor<5x7x9xi32> + +// ----- + +// CHECK-LABEL: dynamic_iota_i32_1d +func.func @dynamic_iota_i32_1d(%arg0: tensor<1xi32>) -> tensor { + %0 = "mhlo.dynamic_iota"(%arg0) <{iota_dimension = 0 : i64}> : (tensor<1xi32>) -> tensor + func.return %0 : tensor +} + +// CHECK-DAG: %[[CST:.*]] = arith.constant dense<0> : tensor +// CHECK-DAG: %[[CST_0:.*]] = arith.constant dense<1> : tensor +// CHECK-DAG: %[[CST_1:.*]] = arith.constant dense<> : tensor<0xi32> +// CHECK: %0 = "tfl.reshape"(%arg0, %[[CST_1]]) : (tensor<1xi32>, tensor<0xi32>) -> tensor +// CHECK: %1 = "tfl.range"(%[[CST]], %0, %[[CST_0]]) : (tensor, tensor, tensor) -> tensor + +// ----- + +// CHECK-LABEL: dynamic_iota_f32_1d +func.func @dynamic_iota_f32_1d(%arg0: tensor<1xi32>) -> tensor { + %0 = "mhlo.dynamic_iota"(%arg0) <{iota_dimension = 0 : i64}> : (tensor<1xi32>) -> tensor + func.return %0 : tensor +} + +// CHECK-DAG: %[[CST:.*]] = arith.constant dense<0.000000e+00> : tensor +// CHECK-DAG: %[[CST_0:.*]] = arith.constant dense<1.000000e+00> : tensor +// CHECK: %0 = "tfl.cast"(%arg0) : (tensor<1xi32>) -> tensor<1xf32> +// CHECK: %[[CST_1:.*]] = arith.constant dense<> : tensor<0xi32> +// CHECK: %1 = "tfl.reshape"(%0, %[[CST_1]]) : (tensor<1xf32>, tensor<0xi32>) -> tensor +// CHECK: %2 = "tfl.range"(%[[CST]], %1, %[[CST_0]]) : (tensor, tensor, tensor) -> tensor + +// ----- + +//===----------------------------------------------------------------------===// +// mhlo.dynamic_slice +//===----------------------------------------------------------------------===// + +// CHECK-LABEL: dynamic_slice +func.func @dynamic_slice(%arg0: tensor<7x3xf32>, %arg1: tensor, %arg2: tensor) -> tensor<4x2xf32> { + %0 = "mhlo.dynamic_slice"(%arg0, %arg1, %arg2) <{slice_sizes = dense<[4, 2]> : tensor<2xi64>}> : (tensor<7x3xf32>, tensor, tensor) -> tensor<4x2xf32> + func.return %0 : tensor<4x2xf32> +} + +// CHECK-DAG: %[[CST_IS_0:.*]] = arith.constant dense<0> : tensor +// CHECK-DAG: %[[CST_IS_3:.*]] = arith.constant dense<3> : tensor +// CHECK: %[[MAX_1:.*]] = "tfl.maximum"(%[[CST_IS_0]], %arg1) : (tensor, tensor) -> tensor +// CHECK: %[[MIN_1:.*]] = "tfl.minimum"(%[[CST_IS_3]], %[[MAX_1]]) : (tensor, tensor) -> tensor +// CHECK: %[[CST_IS_1:.*]] = arith.constant dense<1> : tensor +// CHECK: %[[MAX_2:.*]] = "tfl.maximum"(%[[CST_IS_0]], %arg2) : (tensor, tensor) -> tensor +// CHECK: %[[MIN_2:.*]] = "tfl.minimum"(%[[CST_IS_1]], %[[MAX_2]]) : (tensor, tensor) -> tensor +// CHECK: %[[PACK:.*]] = "tfl.pack"(%[[MIN_1]], %[[MIN_2]]) <{axis = 0 : i32, values_count = 2 : i32}> : (tensor, tensor) -> tensor<2xi32> +// CHECK: %[[SLICE_SIZE:.*]] = arith.constant dense<[4, 2]> : tensor<2xi64> +// CHECK: "tfl.slice"(%arg0, %[[PACK]], %[[SLICE_SIZE]]) : (tensor<7x3xf32>, tensor<2xi32>, tensor<2xi64>) -> tensor<4x2xf32> + +// ----- + +// CHECK-LABEL: dynamic_slice_i64 +func.func @dynamic_slice_i64(%arg0: tensor<7x3xf32>, %arg1: tensor, %arg2: tensor) -> tensor<4x2xf32> { + %0 = "mhlo.dynamic_slice"(%arg0, %arg1, %arg2) <{slice_sizes = dense<[4, 2]> : tensor<2xi64>}> : (tensor<7x3xf32>, tensor, tensor) -> tensor<4x2xf32> + func.return %0 : tensor<4x2xf32> +} + +// CHECK-DAG: %[[CST_IS_0:.*]] = arith.constant dense<0> : tensor +// CHECK-DAG: %[[CST_IS_3:.*]] = arith.constant dense<3> : tensor +// CHECK: %[[MAX_1:.*]] = "tfl.maximum"(%[[CST_IS_0]], %arg1) : (tensor, tensor) -> tensor +// CHECK: %[[MIN_1:.*]] = "tfl.minimum"(%[[CST_IS_3]], %[[MAX_1]]) : (tensor, tensor) -> tensor +// CHECK: %[[CST_IS_1:.*]] = arith.constant dense<1> : tensor +// CHECK: %[[MAX_2:.*]] = "tfl.maximum"(%[[CST_IS_0]], %arg2) : (tensor, tensor) -> tensor +// CHECK: %[[MIN_2:.*]] = "tfl.minimum"(%[[CST_IS_1]], %[[MAX_2]]) : (tensor, tensor) -> tensor +// CHECK: %[[PACK:.*]] = "tfl.pack"(%[[MIN_1]], %[[MIN_2]]) <{axis = 0 : i32, values_count = 2 : i32}> : (tensor, tensor) -> tensor<2xi64> +// CHECK: %[[SLICE_SIZE:.*]] = arith.constant dense<[4, 2]> : tensor<2xi64> +// CHECK: "tfl.slice"(%arg0, %[[PACK]], %[[SLICE_SIZE]]) : (tensor<7x3xf32>, tensor<2xi64>, tensor<2xi64>) -> tensor<4x2xf32> + +// ----- + +// CHECK-LABEL: dynamic_slice_splat_sizes +func.func @dynamic_slice_splat_sizes(%arg0: tensor<7x3xf32>, %arg1: tensor, %arg2: tensor) -> tensor<2x2xf32> { + %0 = "mhlo.dynamic_slice"(%arg0, %arg1, %arg2) <{slice_sizes = dense<2> : tensor<2xi64>}> : (tensor<7x3xf32>, tensor, tensor) -> tensor<2x2xf32> + func.return %0 : tensor<2x2xf32> +} + +// CHECK-DAG: %[[CST_IS_0:.*]] = arith.constant dense<0> : tensor +// CHECK-DAG: %[[CST_IS_5:.*]] = arith.constant dense<5> : tensor +// CHECK: %[[MAX_1:.*]] = "tfl.maximum"(%[[CST_IS_0]], %arg1) : (tensor, tensor) -> tensor +// CHECK: %[[MIN_1:.*]] = "tfl.minimum"(%[[CST_IS_5]], %[[MAX_1]]) : (tensor, tensor) -> tensor +// CHECK: %[[CST_IS_1:.*]] = arith.constant dense<1> : tensor +// CHECK: %[[MAX_2:.*]] = "tfl.maximum"(%[[CST_IS_0]], %arg2) : (tensor, tensor) -> tensor +// CHECK: %[[MIN_2:.*]] = "tfl.minimum"(%[[CST_IS_1]], %[[MAX_2]]) : (tensor, tensor) -> tensor +// CHECK: %[[PACK:.*]] = "tfl.pack"(%[[MIN_1]], %[[MIN_2]]) <{axis = 0 : i32, values_count = 2 : i32}> : (tensor, tensor) -> tensor<2xi32> +// CHECK: %[[SLICE_SIZE:.*]] = arith.constant dense<2> : tensor<2xi64> +// CHECK: "tfl.slice"(%arg0, %[[PACK]], %[[SLICE_SIZE]]) : (tensor<7x3xf32>, tensor<2xi32>, tensor<2xi64>) -> tensor<2x2xf32> + +// ----- + +//===----------------------------------------------------------------------===// +// mhlo.dynamic_update_slice +//===----------------------------------------------------------------------===// + +// CHECK-LABEL: dynamic_update_slice +func.func @dynamic_update_slice(%arg0: tensor<28x1x100xf32>, %arg1: tensor<1x1x100xf32>, %arg2: tensor, %arg3: tensor, %arg4: tensor) -> tensor<28x1x100xf32> { + %0 = "mhlo.dynamic_update_slice"(%arg0, %arg1, %arg2, %arg3, %arg4) : (tensor<28x1x100xf32>, tensor<1x1x100xf32>, tensor, tensor, tensor) -> tensor<28x1x100xf32> + func.return %0 : tensor<28x1x100xf32> +} + +// CHECK: %0 = "tfl.pack"(%arg2, %arg3, %arg4) <{axis = 0 : i32, values_count = 3 : i32}> : (tensor, tensor, tensor) -> tensor<3xi32> +// CHECK: %1 = "tfl.dynamic_update_slice"(%arg0, %arg1, %0) : (tensor<28x1x100xf32>, tensor<1x1x100xf32>, tensor<3xi32>) -> tensor<28x1x100xf32> + +// ----- + +// CHECK-LABEL: dynamic_update_slice_inputs_have_dynamic_dim +func.func @dynamic_update_slice_inputs_have_dynamic_dim(%arg0: tensor, %arg1: tensor, %arg2: tensor, %arg3: tensor) -> tensor { + %0 = mhlo.dynamic_update_slice %arg0, %arg1, %arg2, %arg3 : (tensor, tensor, tensor, tensor) -> tensor + func.return %0 : tensor +} + +// CHECK: %0 = "tfl.pack"(%arg2, %arg3) <{axis = 0 : i32, values_count = 2 : i32}> : (tensor, tensor) -> tensor<2xi32> +// CHECK: %1 = "tfl.dynamic_update_slice"(%arg0, %arg1, %0) : (tensor, tensor, tensor<2xi32>) -> tensor + +// ----- + +// CHECK-LABEL: dynamic_update_slice_operand_has_dynamic_dim +func.func @dynamic_update_slice_operand_has_dynamic_dim(%arg0: tensor<1x?x256xf32>, %arg1: tensor<1x1x256xf32>, %arg2: tensor, %arg3: tensor, %arg4: tensor) -> tensor<1x?x256xf32> { + %0 = mhlo.dynamic_update_slice %arg0, %arg1, %arg2, %arg3, %arg4 : (tensor<1x?x256xf32>, tensor<1x1x256xf32>, tensor, tensor, tensor) -> tensor<1x?x256xf32> + func.return %0 : tensor<1x?x256xf32> +} + +// CHECK: %0 = "tfl.pack"(%arg2, %arg3, %arg4) <{axis = 0 : i32, values_count = 3 : i32}> : (tensor, tensor, tensor) -> tensor<3xi32> +// CHECK: %1 = "tfl.dynamic_update_slice"(%arg0, %arg1, %0) : (tensor<1x?x256xf32>, tensor<1x1x256xf32>, tensor<3xi32>) -> tensor<1x?x256xf32> + +// ----- + +//===----------------------------------------------------------------------===// +// rounding +//===----------------------------------------------------------------------===// + +// CHECK-LABEL: @nearest_even +func.func @nearest_even(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "mhlo.round_nearest_even"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + return %0 : tensor<2xf32> +} + +// CHECK: %0 = "tfl.round"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + +// ----- + +// CHECK-LABEL: @round +func.func @round(%arg0: tensor<8x128xf32>) -> tensor<8x128xf32> { + %0 = mhlo.constant dense<2.000000e+00> : tensor<8x128xf32> + %1 = mhlo.constant dense<5.000000e-01> : tensor<8x128xf32> + %2 = mhlo.constant dense<1.000000e+00> : tensor<8x128xf32> + %3 = "mhlo.floor"(%arg0) : (tensor<8x128xf32>) -> tensor<8x128xf32> + %4 = mhlo.subtract %arg0, %3 : tensor<8x128xf32> + %5 = "mhlo.compare"(%4, %1) {comparison_direction = #mhlo} : (tensor<8x128xf32>, tensor<8x128xf32>) -> tensor<8x128xi1> + %6 = "mhlo.compare"(%4, %1) {comparison_direction = #mhlo} : (tensor<8x128xf32>, tensor<8x128xf32>) -> tensor<8x128xi1> + %7 = mhlo.multiply %arg0, %1 : tensor<8x128xf32> + %8 = "mhlo.floor"(%7) : (tensor<8x128xf32>) -> tensor<8x128xf32> + %9 = mhlo.multiply %8, %0 : tensor<8x128xf32> + %10 = mhlo.subtract %3, %9 : tensor<8x128xf32> + %11 = "mhlo.compare"(%10, %2) {comparison_direction = #mhlo} : (tensor<8x128xf32>, tensor<8x128xf32>) -> tensor<8x128xi1> + %12 = mhlo.and %6, %11 : tensor<8x128xi1> + %13 = mhlo.or %5, %12 : tensor<8x128xi1> + %14 = mhlo.add %3, %2 : tensor<8x128xf32> + %15 = "mhlo.select"(%13, %14, %3) : (tensor<8x128xi1>, tensor<8x128xf32>, tensor<8x128xf32>) -> tensor<8x128xf32> + func.return %15 : tensor<8x128xf32> +} + +// CHECK: "tfl.round"(%arg0) : (tensor<8x128xf32>) -> tensor<8x128xf32> + +// ----- + +// CHECK-LABEL: floor_mod_float +func.func @floor_mod_float(%arg0: tensor<192x8xf32>, %arg1: tensor<192x8xf32>) -> tensor<192x8xf32> { + %0 = mhlo.constant dense<0.000000e+00> : tensor<192x8xf32> + %1 = mhlo.remainder %arg0, %arg1 : tensor<192x8xf32> + %2 = "mhlo.compare"(%1, %0) {comparison_direction = #mhlo} : (tensor<192x8xf32>, tensor<192x8xf32>) -> tensor<192x8xi1> + %3 = "mhlo.compare"(%arg1, %0) {comparison_direction = #mhlo} : (tensor<192x8xf32>, tensor<192x8xf32>) -> tensor<192x8xi1> + %4 = "mhlo.compare"(%2, %3) {comparison_direction = #mhlo} : (tensor<192x8xi1>, tensor<192x8xi1>) -> tensor<192x8xi1> + %5 = "mhlo.compare"(%1, %0) {comparison_direction = #mhlo} : (tensor<192x8xf32>, tensor<192x8xf32>) -> tensor<192x8xi1> + %6 = mhlo.and %4, %5 : tensor<192x8xi1> + %7 = mhlo.add %1, %arg1 : tensor<192x8xf32> + %8 = "mhlo.select"(%6, %7, %1) : (tensor<192x8xi1>, tensor<192x8xf32>, tensor<192x8xf32>) -> tensor<192x8xf32> + func.return %8 : tensor<192x8xf32> +} + +// CHECK: "tfl.floor_mod"(%arg0, %arg1) : (tensor<192x8xf32>, tensor<192x8xf32>) -> tensor<192x8xf32> + +// ----- + +// CHECK-LABEL: floor_mod_int +func.func @floor_mod_int(%arg0: tensor<192x8xi32>, %arg1: tensor<192x8xi32>) -> tensor<192x8xi32> { + %0 = mhlo.constant dense<0> : tensor<192x8xi32> + %1 = mhlo.remainder %arg0, %arg1 : tensor<192x8xi32> + %2 = "mhlo.compare"(%1, %0) {comparison_direction = #mhlo} : (tensor<192x8xi32>, tensor<192x8xi32>) -> tensor<192x8xi1> + %3 = "mhlo.compare"(%arg1, %0) {comparison_direction = #mhlo} : (tensor<192x8xi32>, tensor<192x8xi32>) -> tensor<192x8xi1> + %4 = "mhlo.compare"(%2, %3) {comparison_direction = #mhlo} : (tensor<192x8xi1>, tensor<192x8xi1>) -> tensor<192x8xi1> + %5 = "mhlo.compare"(%1, %0) {comparison_direction = #mhlo} : (tensor<192x8xi32>, tensor<192x8xi32>) -> tensor<192x8xi1> + %6 = mhlo.and %4, %5 : tensor<192x8xi1> + %7 = mhlo.add %1, %arg1 : tensor<192x8xi32> + %8 = "mhlo.select"(%6, %7, %1) : (tensor<192x8xi1>, tensor<192x8xi32>, tensor<192x8xi32>) -> tensor<192x8xi32> + func.return %8 : tensor<192x8xi32> +} + +// CHECK: "tfl.floor_mod"(%arg0, %arg1) : (tensor<192x8xi32>, tensor<192x8xi32>) -> tensor<192x8xi32> + +// ----- + +// CHECK-LABEL: floor_mod_float_cst +func.func @floor_mod_float_cst(%arg0: tensor<192x8xf32>) -> tensor<192x8xf32> { + %0 = mhlo.constant dense<0.000000e+00> : tensor<192x8xf32> + %1 = mhlo.constant dense<2.000000e+00> : tensor<192x8xf32> + %2 = mhlo.remainder %arg0, %1 : tensor<192x8xf32> + %3 = "mhlo.compare"(%2, %0) {comparison_direction = #mhlo} : (tensor<192x8xf32>, tensor<192x8xf32>) -> tensor<192x8xi1> + %4 = "mhlo.compare"(%2, %0) {comparison_direction = #mhlo} : (tensor<192x8xf32>, tensor<192x8xf32>) -> tensor<192x8xi1> + %5 = mhlo.and %3, %4 : tensor<192x8xi1> + %6 = mhlo.add %2, %1 : tensor<192x8xf32> + %7 = "mhlo.select"(%5, %6, %2) : (tensor<192x8xi1>, tensor<192x8xf32>, tensor<192x8xf32>) -> tensor<192x8xf32> + func.return %7 : tensor<192x8xf32> +} + +// CHECK: %[[CST:.*]] = arith.constant dense<2.000000e+00> : tensor<192x8xf32> +// CHECK: "tfl.floor_mod"(%arg0, %[[CST]]) : (tensor<192x8xf32>, tensor<192x8xf32>) -> tensor<192x8xf32> + +// ----- + +// CHECK-LABEL: floor_mod_int_cst +func.func @floor_mod_int_cst(%arg0: tensor<192x8xi32>) -> tensor<192x8xi32> { + %0 = mhlo.constant dense<0> : tensor<192x8xi32> + %1 = mhlo.constant dense<2> : tensor<192x8xi32> + %2 = mhlo.remainder %arg0, %1 : tensor<192x8xi32> + %3 = "mhlo.compare"(%2, %0) {comparison_direction = #mhlo} : (tensor<192x8xi32>, tensor<192x8xi32>) -> tensor<192x8xi1> + %4 = "mhlo.compare"(%2, %0) {comparison_direction = #mhlo} : (tensor<192x8xi32>, tensor<192x8xi32>) -> tensor<192x8xi1> + %5 = mhlo.and %3, %4 : tensor<192x8xi1> + %6 = mhlo.add %2, %1 : tensor<192x8xi32> + %7 = "mhlo.select"(%5, %6, %2) : (tensor<192x8xi1>, tensor<192x8xi32>, tensor<192x8xi32>) -> tensor<192x8xi32> + func.return %7 : tensor<192x8xi32> +} + +// CHECK: %[[CST:.*]] = arith.constant dense<2> : tensor<192x8xi32> +// CHECK: "tfl.floor_mod"(%arg0, %[[CST]]) : (tensor<192x8xi32>, tensor<192x8xi32>) -> tensor<192x8xi32> + +// ----- + +// CHECK-LABEL: floor_div +func.func @floor_div(%arg0: tensor<10x10xf32>, %arg1: tensor<10x10xf32>) -> tensor<10x10xf32> { + %0 = mhlo.constant dense<0.000000e+00> : tensor<10x10xf32> + %1 = mhlo.constant dense<-1.000000e+00> : tensor<10x10xf32> + %2 = mhlo.remainder %arg0, %arg1 : tensor<10x10xf32> + %3 = "mhlo.compare"(%2, %0) {comparison_direction = #mhlo} : (tensor<10x10xf32>, tensor<10x10xf32>) -> tensor<10x10xi1> + %4 = "mhlo.sign"(%arg1) : (tensor<10x10xf32>) -> tensor<10x10xf32> + %5 = "mhlo.sign"(%2) : (tensor<10x10xf32>) -> tensor<10x10xf32> + %6 = "mhlo.compare"(%4, %5) {comparison_direction = #mhlo} : (tensor<10x10xf32>, tensor<10x10xf32>) -> tensor<10x10xi1> + %7 = mhlo.and %3, %6 : tensor<10x10xi1> + %8 = mhlo.subtract %arg0, %2 : tensor<10x10xf32> + %9 = mhlo.divide %8, %arg1 : tensor<10x10xf32> + %10 = mhlo.add %9, %1 : tensor<10x10xf32> + %11 = "mhlo.select"(%7, %10, %9) : (tensor<10x10xi1>, tensor<10x10xf32>, tensor<10x10xf32>) -> tensor<10x10xf32> + %12 = "mhlo.round_nearest_afz"(%11) : (tensor<10x10xf32>) -> tensor<10x10xf32> + %13 = "mhlo.tuple"(%12) : (tensor<10x10xf32>) -> tuple> + func.return %12 : tensor<10x10xf32> +} + +// CHECK: tfl.floor_div %arg0, %arg1 : tensor<10x10xf32 + +// ----- + +// CHECK-LABEL: floor_div_cst +func.func @floor_div_cst(%arg0: tensor<10x10xf32>) -> tensor<10x10xf32> { + %0 = mhlo.constant dense<2.000000e+00> : tensor<10x10xf32> + %1 = mhlo.constant dense<0.000000e+00> : tensor<10x10xf32> + %2 = mhlo.constant dense<1.000000e+00> : tensor<10x10xf32> + %3 = mhlo.constant dense<5.000000e-01> : tensor<10x10xf32> + %4 = mhlo.constant dense<-1.000000e+00> : tensor<10x10xf32> + %5 = mhlo.remainder %arg0, %0 : tensor<10x10xf32> + %6 = "mhlo.compare"(%5, %1) {comparison_direction = #mhlo} : (tensor<10x10xf32>, tensor<10x10xf32>) -> tensor<10x10xi1> + %7 = "mhlo.sign"(%5) : (tensor<10x10xf32>) -> tensor<10x10xf32> + %8 = "mhlo.compare"(%2, %7) {comparison_direction = #mhlo} : (tensor<10x10xf32>, tensor<10x10xf32>) -> tensor<10x10xi1> + %9 = mhlo.and %6, %8 : tensor<10x10xi1> + %10 = mhlo.subtract %arg0, %5 : tensor<10x10xf32> + %11 = mhlo.multiply %10, %3 : tensor<10x10xf32> + %12 = mhlo.add %11, %4 : tensor<10x10xf32> + %13 = "mhlo.select"(%9, %12, %11) : (tensor<10x10xi1>, tensor<10x10xf32>, tensor<10x10xf32>) -> tensor<10x10xf32> + %14 = "mhlo.round_nearest_afz"(%13) : (tensor<10x10xf32>) -> tensor<10x10xf32> + %15 = "mhlo.tuple"(%14) : (tensor<10x10xf32>) -> tuple> + func.return %14 : tensor<10x10xf32> +} + +// CHECK: %[[CST:.*]] = arith.constant dense<2.000000e+00> : tensor<10x10xf32> +// CHECK: tfl.floor_div %arg0, %[[CST]] : tensor<10x10xf32> + +// ----- + +// CHECK-LABEL: floor_div_cst2 +func.func @floor_div_cst2(%arg0: tensor<10x10xf32>) -> tensor<10x10xf32> { + %0 = mhlo.constant dense<1.000000e+00> : tensor<10x10xf32> + %1 = mhlo.constant dense<2.000000e+00> : tensor<10x10xf32> + %2 = mhlo.constant dense<0.000000e+00> : tensor<10x10xf32> + %3 = mhlo.constant dense<-1.000000e+00> : tensor<10x10xf32> + %4 = mhlo.remainder %arg0, %1 : tensor<10x10xf32> + %5 = "mhlo.compare"(%4, %2) {comparison_direction = #mhlo} : (tensor<10x10xf32>, tensor<10x10xf32>) -> tensor<10x10xi1> + %6 = "mhlo.sign"(%4) : (tensor<10x10xf32>) -> tensor<10x10xf32> + %7 = "mhlo.compare"(%0, %6) {comparison_direction = #mhlo} : (tensor<10x10xf32>, tensor<10x10xf32>) -> tensor<10x10xi1> + %8 = mhlo.and %5, %7 : tensor<10x10xi1> + %9 = mhlo.subtract %arg0, %4 : tensor<10x10xf32> + %10 = mhlo.divide %9, %1 : tensor<10x10xf32> + %11 = mhlo.add %10, %3 : tensor<10x10xf32> + %12 = "mhlo.select"(%8, %11, %10) : (tensor<10x10xi1>, tensor<10x10xf32>, tensor<10x10xf32>) -> tensor<10x10xf32> + %13 = "mhlo.round_nearest_afz"(%12) : (tensor<10x10xf32>) -> tensor<10x10xf32> + %14 = "mhlo.tuple"(%13) : (tensor<10x10xf32>) -> tuple> + func.return %13 : tensor<10x10xf32> +} + +// CHECK: %[[CST:.*]] = arith.constant dense<2.000000e+00> : tensor<10x10xf32> +// CHECK: tfl.floor_div %arg0, %[[CST]] : tensor<10x10xf32> + +// ----- + +// CHECK-LABEL: floor_div_broadcast_cst +func.func @floor_div_broadcast_cst(%arg0: tensor<10x8xf32>) -> tensor<10x8xf32> { + %0 = mhlo.constant dense<1.000000e+00> : tensor<10x8xf32> + %1 = mhlo.constant dense<[1.000000e+00, 2.000000e+00, 4.000000e+00, 8.000000e+00, 1.600000e+01, 3.200000e+01, 6.400000e+01, 1.280000e+02]> : tensor<8xf32> + %2 = mhlo.constant dense<0.000000e+00> : tensor<10x8xf32> + %3 = mhlo.constant dense<-1.000000e+00> : tensor<10x8xf32> + %5 = "mhlo.broadcast_in_dim"(%1) <{broadcast_dimensions = dense<1> : tensor<1xi64>}> : (tensor<8xf32>) -> tensor<10x8xf32> + %6 = mhlo.remainder %arg0, %5 : tensor<10x8xf32> + %7 = "mhlo.compare"(%6, %2) {comparison_direction = #mhlo} : (tensor<10x8xf32>, tensor<10x8xf32>) -> tensor<10x8xi1> + %8 = "mhlo.sign"(%6) : (tensor<10x8xf32>) -> tensor<10x8xf32> + %9 = "mhlo.compare"(%0, %8) {comparison_direction = #mhlo} : (tensor<10x8xf32>, tensor<10x8xf32>) -> tensor<10x8xi1> + %10 = mhlo.and %7, %9 : tensor<10x8xi1> + %11 = mhlo.subtract %arg0, %6 : tensor<10x8xf32> + %12 = mhlo.divide %11, %5 : tensor<10x8xf32> + %13 = mhlo.add %12, %3 : tensor<10x8xf32> + %14 = "mhlo.select"(%10, %13, %12) : (tensor<10x8xi1>, tensor<10x8xf32>, tensor<10x8xf32>) -> tensor<10x8xf32> + %15 = "mhlo.round_nearest_afz"(%14) : (tensor<10x8xf32>) -> tensor<10x8xf32> + %16 = "mhlo.tuple"(%15) : (tensor<10x8xf32>) -> tuple> + func.return %15 : tensor<10x8xf32> +} + +// CHECK: %[[BCAST:.*]] = "tfl.broadcast_to" +// CHECK: tfl.floor_div %arg0, %[[BCAST]] : tensor<10x8xf32> + +// ----- + +// CHECK-LABEL: floor_div_cst3 +func.func @floor_div_cst3(%arg0: tensor<12xf32>) -> tensor<12xf32> { + %0 = mhlo.constant dense<2.000000e+00> : tensor + %1 = "mhlo.broadcast_in_dim"(%0) <{broadcast_dimensions = dense<> : tensor<0xi64>}> : (tensor) -> tensor<12xf32> + %2 = mhlo.constant dense<0.000000e+00> : tensor + %3 = "mhlo.broadcast_in_dim"(%2) <{broadcast_dimensions = dense<> : tensor<0xi64>}> : (tensor) -> tensor<12xf32> + %4 = mhlo.constant dense<1.000000e+00> : tensor + %5 = "mhlo.broadcast_in_dim"(%4) <{broadcast_dimensions = dense<> : tensor<0xi64>}> : (tensor) -> tensor<12xf32> + %8 = mhlo.remainder %arg0, %1 : tensor<12xf32> + %9 = mhlo.subtract %arg0, %8 : tensor<12xf32> + %10 = mhlo.divide %9, %1 : tensor<12xf32> + %11 = mhlo.compare NE, %8, %3, FLOAT : (tensor<12xf32>, tensor<12xf32>) -> tensor<12xi1> + %12 = mhlo.sign %8 : tensor<12xf32> + %13 = mhlo.compare NE, %5, %12, FLOAT : (tensor<12xf32>, tensor<12xf32>) -> tensor<12xi1> + %14 = mhlo.and %11, %13 : tensor<12xi1> + %15 = mhlo.subtract %10, %5 : tensor<12xf32> + %16 = mhlo.select %14, %15, %10 : tensor<12xi1>, tensor<12xf32> + %17 = mhlo.round_nearest_afz %16 : tensor<12xf32> + return %17 : tensor<12xf32> +} + +// CHECK: %[[CST:.*]] = arith.constant dense<2.000000e+00> : tensor<12xf32> +// CHECK: %[[FLOOR_DIV:.*]] = tfl.floor_div %arg0, %[[CST]] : tensor<12xf32> +// CHECK: return %[[FLOOR_DIV]] : tensor<12xf32> + +// ----- + +//===----------------------------------------------------------------------===// +// unary elementwise +//===----------------------------------------------------------------------===// + +// CHECK-LABEL: convert_i32_f32 +func.func @convert_i32_f32(%arg0: tensor<2xi32>) -> tensor<2xf32> { + %0 = "mhlo.convert"(%arg0) : (tensor<2xi32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK: tfl.cast + +// ----- + +// CHECK-LABEL: abs +func.func @abs(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "mhlo.abs"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK: tfl.abs + +// ----- + +// CHECK-LABEL: abs_dynamic +func.func @abs_dynamic(%arg0: tensor) -> tensor { + %0 = "mhlo.abs"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// CHECK: tfl.abs + +// ----- + +// CHECK-LABEL: ceil +func.func @ceil(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "mhlo.ceil"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK: tfl.ceil + +// ----- + +// CHECK-LABEL: ceil_dynamic +func.func @ceil_dynamic(%arg0: tensor) -> tensor { + %0 = "mhlo.ceil"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// CHECK: tfl.ceil + +// ----- + +// CHECK-LABEL: complex_abs +func.func @complex_abs(%arg0: tensor<2xcomplex>) -> tensor<2xf32> { + %0 = "mhlo.abs"(%arg0) : (tensor<2xcomplex>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK: %0 = "tfl.complex_abs"(%arg0) : (tensor<2xcomplex>) -> tensor<2xf32> +// CHECK: return %0 : tensor<2xf32> +// ----- + +func.func @is_finite(%arg0: tensor<2xf32>) -> tensor<2xi1> { + %0 = "mhlo.is_finite"(%arg0) : (tensor<2xf32>) -> tensor<2xi1> + func.return %0 : tensor<2xi1> +} + +// CHECK: %0 = tfl.sub %arg0, %arg0 {fused_activation_function = "NONE"} : tensor<2xf32> +// CHECK: %cst = arith.constant dense<0.000000e+00> : tensor +// CHECK: %1 = "tfl.equal"(%0, %cst) : (tensor<2xf32>, tensor) -> tensor<2xi1> +// CHECK: return %1 : tensor<2xi1> + +// ----- + +func.func @is_finite_dynamic(%arg0: tensor) -> tensor { + %0 = "mhlo.is_finite"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// CHECK: %0 = tfl.sub %arg0, %arg0 {fused_activation_function = "NONE"} : tensor +// CHECK: %cst = arith.constant dense<0.000000e+00> : tensor +// CHECK: %1 = "tfl.equal"(%0, %cst) : (tensor, tensor) -> tensor + +// ----- + +// CHECK-LABEL: cos +func.func @cos(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "mhlo.cosine"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK: tfl.cos + +// ----- + +// CHECK-LABEL: cos_dynamic +func.func @cos_dynamic(%arg0: tensor) -> tensor { + %0 = "mhlo.cosine"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// CHECK: tfl.cos + +// ----- + +// CHECK-LABEL: logistic +func.func @logistic(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "mhlo.logistic"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK: tfl.logistic + +// ----- + +// CHECK-LABEL: exp +func.func @exp(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "mhlo.exponential"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK: tfl.exp + +// ----- + +// CHECK-LABEL: exp_dynamic +func.func @exp_dynamic(%arg0: tensor) -> tensor { + %0 = "mhlo.exponential"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// CHECK: tfl.exp + +// ----- + +// CHECK-LABEL: expm1 +func.func @expm1(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "mhlo.exponential_minus_one"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK: %0 = "tfl.exp"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> +// CHECK: %cst = arith.constant dense<1.000000e+00> : tensor +// CHECK: %1 = tfl.sub(%0, %cst) <{fused_activation_function = "NONE"}> : (tensor<2xf32>, tensor) -> tensor<2xf32> + +// ----- + +// CHECK-LABEL: floor +func.func @floor(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "mhlo.floor"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK: tfl.floor + +// ----- + +// CHECK-LABEL: floor_dynamic +func.func @floor_dynamic(%arg0: tensor) -> tensor { + %0 = "mhlo.floor"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// CHECK: tfl.floor + +// ----- + +// CHECK-LABEL: log +func.func @log(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "mhlo.log"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK: tfl.log + +// ----- + +// CHECK-LABEL: log_dynamic +func.func @log_dynamic(%arg0: tensor) -> tensor { + %0 = "mhlo.log"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// CHECK: tfl.log + +// ----- + +// CHECK-LABEL: log1p +func.func @log1p(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "mhlo.log_plus_one"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK: %cst = arith.constant dense<1.000000e+00> : tensor +// CHECK: %0 = tfl.add(%arg0, %cst) <{fused_activation_function = "NONE"}> : (tensor<2xf32>, tensor) -> tensor<2xf32> +// CHECK: %1 = "tfl.log"(%0) : (tensor<2xf32>) -> tensor<2xf32> + +// ----- + +// CHECK-LABEL: log1p_dynamic +func.func @log1p_dynamic(%arg0: tensor) -> tensor { + %0 = "mhlo.log_plus_one"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// CHECK: %cst = arith.constant dense<1.000000e+00> : tensor +// CHECK: %0 = tfl.add(%arg0, %cst) <{fused_activation_function = "NONE"}> : (tensor, tensor) -> tensor +// CHECK: %1 = "tfl.log"(%0) : (tensor) -> tensor + +// ----- + +// CHECK-LABEL: neg +func.func @neg(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "mhlo.negate"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK: tfl.neg + +// ----- + +// CHECK-LABEL: neg_dynamic +func.func @neg_dynamic(%arg0: tensor) -> tensor { + %0 = "mhlo.negate"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// CHECK: tfl.neg + +// ----- + +// CHECK-LABEL: sin +func.func @sin(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "mhlo.sine"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK: tfl.sin + +// ----- + +// CHECK-LABEL: sin_dynamic +func.func @sin_dynamic(%arg0: tensor) -> tensor { + %0 = "mhlo.sine"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// CHECK: tfl.sin + +// ----- + +// CHECK-LABEL: rsqrt +func.func @rsqrt(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "mhlo.rsqrt"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK: tfl.rsqrt + +// ----- + +// CHECK-LABEL: rsqrt_dynamic +func.func @rsqrt_dynamic(%arg0: tensor) -> tensor { + %0 = "mhlo.rsqrt"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// CHECK: tfl.rsqrt + +// ----- + +// CHECK-LABEL: @sqrt +func.func @sqrt(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "mhlo.sqrt"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK: tfl.sqrt + +// ----- + +// CHECK-LABEL: sqrt_dynamic +func.func @sqrt_dynamic(%arg0: tensor) -> tensor { + %0 = "mhlo.sqrt"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// CHECK: tfl.sqrt + +// ----- + +// CHECK-LABEL: tanh +func.func @tanh(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "mhlo.tanh"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK: tfl.tanh + +// ----- + +// CHECK-LABEL: tanh_dynamic +func.func @tanh_dynamic(%arg0: tensor) -> tensor { + %0 = "mhlo.tanh"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// CHECK: tfl.tanh + +// ----- + +// CHECK-LABEL: bitcast +func.func @bitcast(%arg0: tensor<2xf32>) -> tensor<2xf32> { + %0 = "mhlo.bitcast_convert"(%arg0) : (tensor<2xf32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK: tfl.bitcast + +// ----- + +// CHECK-LABEL: bitcast_dynamic +func.func @bitcast_dynamic(%arg0: tensor) -> tensor { + %0 = "mhlo.bitcast_convert"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +} + +// CHECK: tfl.bitcast + +// ----- + +// CHECK-LABEL: bitcast_same_widths +func.func @bitcast_same_widths(%arg0: tensor<2xf32>) -> tensor<2xi32> { + %0 = "mhlo.bitcast_convert"(%arg0) : (tensor<2xf32>) -> tensor<2xi32> + func.return %0 : tensor<2xi32> +} + +// CHECK: tfl.bitcast + +// ----- + +//===----------------------------------------------------------------------===// +// logical and bitwise ops +//===----------------------------------------------------------------------===// + +// CHECK-LABEL: not +func.func @not(%arg0: tensor<5x3x1xi1>) -> tensor<5x3x1xi1> { + %0 = "mhlo.not"(%arg0): (tensor<5x3x1xi1>) -> (tensor<5x3x1xi1>) + func.return %0 : tensor<5x3x1xi1> +} + +// CHECK: %0 = "tfl.logical_not"(%arg0) : (tensor<5x3x1xi1>) -> tensor<5x3x1xi1> + +// ----- + +// CHECK-LABEL: not_i8 +func.func @not_i8(%arg0: tensor<7x9x11xi8>) -> tensor<7x9x11xi8> { + %0 = "mhlo.not"(%arg0): (tensor<7x9x11xi8>) -> (tensor<7x9x11xi8>) + func.return %0 : tensor<7x9x11xi8> +} + +// CHECK: %cst = arith.constant dense<-1> : tensor +// CHECK: %0 = "tfl.bitwise_xor"(%arg0, %cst) : (tensor<7x9x11xi8>, tensor) -> tensor<7x9x11xi8> + +// ----- + +// CHECK-LABEL: not_i16 +func.func @not_i16(%arg0: tensor<7x9x11xi16>) -> tensor<7x9x11xi16> { + %0 = "mhlo.not"(%arg0): (tensor<7x9x11xi16>) -> (tensor<7x9x11xi16>) + func.return %0 : tensor<7x9x11xi16> +} + +// CHECK: %cst = arith.constant dense<-1> : tensor +// CHECK: %0 = "tfl.bitwise_xor"(%arg0, %cst) : (tensor<7x9x11xi16>, tensor) -> tensor<7x9x11xi16> + +// ----- + +// CHECK-LABEL: not_i32 +func.func @not_i32(%arg0: tensor<7x9x11xi32>) -> tensor<7x9x11xi32> { + %0 = "mhlo.not"(%arg0): (tensor<7x9x11xi32>) -> (tensor<7x9x11xi32>) + func.return %0 : tensor<7x9x11xi32> +} + +// CHECK: %cst = arith.constant dense<-1> : tensor +// CHECK: %0 = "tfl.bitwise_xor"(%arg0, %cst) : (tensor<7x9x11xi32>, tensor) -> tensor<7x9x11xi32> + +// ----- + +// CHECK-LABEL: not_ui8 +func.func @not_ui8(%arg0: tensor<7x9x11xui8>) -> tensor<7x9x11xui8> { + %0 = "mhlo.not"(%arg0): (tensor<7x9x11xui8>) -> (tensor<7x9x11xui8>) + func.return %0 : tensor<7x9x11xui8> +} + +// CHECK: %[[CST:.*]] = "tfl.pseudo_const"() <{value = dense<255> : tensor}> : () -> tensor +// CHECK: %{{.*}} = "tfl.bitwise_xor"(%arg0, %[[CST]]) : (tensor<7x9x11xui8>, tensor) -> tensor<7x9x11xui8> + +// ----- + +// CHECK-LABEL: not_ui16 +func.func @not_ui16(%arg0: tensor<7x9x11xui16>) -> tensor<7x9x11xui16> { + %0 = "mhlo.not"(%arg0): (tensor<7x9x11xui16>) -> (tensor<7x9x11xui16>) + func.return %0 : tensor<7x9x11xui16> +} + +// CHECK: %[[CST:.*]] = "tfl.pseudo_const"() <{value = dense<65535> : tensor}> : () -> tensor +// CHECK: %{{.*}} = "tfl.bitwise_xor"(%arg0, %[[CST]]) : (tensor<7x9x11xui16>, tensor) -> tensor<7x9x11xui16> + +// ----- + +// CHECK-LABEL: not_ui32 +func.func @not_ui32(%arg0: tensor<7x9x11xui32>) -> tensor<7x9x11xui32> { + %0 = "mhlo.not"(%arg0): (tensor<7x9x11xui32>) -> (tensor<7x9x11xui32>) + func.return %0 : tensor<7x9x11xui32> +} + +// CHECK: %[[CST:.*]] = "tfl.pseudo_const"() <{value = dense<4294967295> : tensor}> : () -> tensor +// CHECK: %{{.*}} = "tfl.bitwise_xor"(%arg0, %[[CST]]) : (tensor<7x9x11xui32>, tensor) -> tensor<7x9x11xui32> + +// ----- + +//===----------------------------------------------------------------------===// +// binary ops +//===----------------------------------------------------------------------===// + +// CHECK-LABEL: remainder +func.func @remainder(%arg0: tensor<10x8xi32>, %arg1: tensor<10x8xi32>) -> tensor<10x8xi32> { + %0 = mhlo.remainder %arg0, %arg1 : tensor<10x8xi32> + func.return %0 : tensor<10x8xi32> +} + +// CHECK: %0 = "tfl.floor_mod"(%arg0, %arg1) : (tensor<10x8xi32>, tensor<10x8xi32>) -> tensor<10x8xi32> + +// ----- + +// CHECK-LABEL: shift_right_arith +func.func @shift_right_arith(%arg0: tensor<4xi32>, %arg1: tensor<4xi32>) -> tensor<4xi32> { + %0 = mhlo.shift_right_arithmetic %arg0, %arg1 : tensor<4xi32> + func.return %0 : tensor<4xi32> +} + +// CHECK: %0 = "tfl.right_shift"(%arg0, %arg1) : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi32> + +// ----- + +// CHECK-LABEL: shift_right_logical +func.func @shift_right_logical(%arg0: tensor<4xi32>, %arg1: tensor<4xi32>) -> tensor<4xi32> { + %0 = mhlo.shift_right_logical %arg0, %arg1 : tensor<4xi32> + func.return %0 : tensor<4xi32> +} + +// CHECK: %0 = "tfl.right_shift"(%arg0, %arg1) : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi32> + +// ----- + +//===----------------------------------------------------------------------===// +// mhlo.compare +//===----------------------------------------------------------------------===// + +// CHECK-LABEL: greater_unsupported_compare_type +func.func @greater_unsupported_compare_type(%arg0: tensor<2xf32>, %arg1: tensor<2xf32>) -> tensor<2xi1> { + %0 = "mhlo.compare"(%arg0, %arg1) {compare_type = #mhlo, comparison_direction = #mhlo} : (tensor<2xf32>, tensor<2xf32>) -> tensor<2xi1> + func.return %0 : tensor<2xi1> +} + +// CHECK-NOT: tfl +// CHECK: mhlo.compare + +// ----- + +// CHECK-LABEL: equal +func.func @equal(%arg0: tensor<2xi32>, %arg1: tensor<2xi32>) -> tensor<2xi1> { + %0 = "mhlo.compare"(%arg0, %arg1) {comparison_direction = #mhlo} : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi1> + func.return %0 : tensor<2xi1> +} + +// CHECK: tfl.equal + +// ----- + +// CHECK-LABEL: notequal +func.func @notequal(%arg0: tensor<2xi32>, %arg1: tensor<2xi32>) -> tensor<2xi1> { + %0 = "mhlo.compare"(%arg0, %arg1) {comparison_direction = #mhlo} : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi1> + func.return %0 : tensor<2xi1> +} + +// CHECK: tfl.not_equal + +// ----- + +// CHECK-LABEL: greater +func.func @greater(%arg0: tensor<2xi32>, %arg1: tensor<2xi32>) -> tensor<2xi1> { + %0 = "mhlo.compare"(%arg0, %arg1) {comparison_direction = #mhlo} : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi1> + func.return %0 : tensor<2xi1> +} + +// CHECK: tfl.greater + +// ----- + +// CHECK-LABEL: greater_equal +func.func @greater_equal(%arg0: tensor<2xi32>, %arg1: tensor<2xi32>) -> tensor<2xi1> { + %0 = "mhlo.compare"(%arg0, %arg1) {comparison_direction = #mhlo} : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi1> + func.return %0 : tensor<2xi1> +} + +// CHECK: tfl.greater_equal + +// ----- + +// CHECK-LABEL: less +func.func @less(%arg0: tensor<2xi32>, %arg1: tensor<2xi32>) -> tensor<2xi1> { + %0 = "mhlo.compare"(%arg0, %arg1) {comparison_direction = #mhlo} : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi1> + func.return %0 : tensor<2xi1> +} + +// CHECK: tfl.less + +// ----- + +// CHECK-LABEL: less_equal +func.func @less_equal(%arg0: tensor<2xi32>, %arg1: tensor<2xi32>) -> tensor<2xi1> { + %0 = "mhlo.compare"(%arg0, %arg1) {comparison_direction = #mhlo} : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi1> + func.return %0 : tensor<2xi1> +} + +// CHECK: tfl.less_equal + +// ----- + +//===----------------------------------------------------------------------===// +// mhlo binary element-wise ops +//===----------------------------------------------------------------------===// + +// CHECK-LABEL: add +func.func @add(%arg0: tensor<2xi32>) -> tensor<2xi32> { + %0 = mhlo.add %arg0, %arg0 : tensor<2xi32> + func.return %0 : tensor<2xi32> +} + +// CHECK: tfl.add +// CHECK-NOT: mhlo + +// ----- + +// CHECK-LABEL: maximum +func.func @maximum(%arg0: tensor<4xf32>, %arg1: tensor<4xf32>) -> tensor<4xf32> { + %0 = mhlo.maximum %arg0, %arg1 : tensor<4xf32> + func.return %0 : tensor<4xf32> +} + +// CHECK: "tfl.maximum"(%arg0, %arg1) +// CHECK-NOT: mhlo + +// ----- + +// CHECK-LABEL: minimum +func.func @minimum(%arg0: tensor<4xf32>, %arg1: tensor<4xf32>) -> tensor<4xf32> { + %0 = mhlo.minimum %arg0, %arg1 : tensor<4xf32> + func.return %0 : tensor<4xf32> +} + +// CHECK: "tfl.minimum"(%arg0, %arg1) +// CHECK-NOT: mhlo + +// ----- + +// CHECK-LABEL: mul +func.func @mul(%arg0: tensor<2xi32>) -> tensor<2xi32> { + %0 = mhlo.multiply %arg0, %arg0 : tensor<2xi32> + func.return %0 : tensor<2xi32> +} + +// CHECK: tfl.mul %arg0, %arg0 +// CHECK-NOT: mhlo + +// ----- + +// CHECK-LABEL: pow +func.func @pow(%arg0: tensor<4xf32>) -> tensor<4xf32> { + %0 = mhlo.power %arg0, %arg0 : tensor<4xf32> + func.return %0 : tensor<4xf32> +} + +// CHECK: tfl.pow +// CHECK-NOT: mhlo + +// ----- + +// CHECK-LABEL: sub +func.func @sub(%arg0: tensor<2xi32>) -> tensor<2xi32> { + %0 = mhlo.subtract %arg0, %arg0 : tensor<2xi32> + func.return %0 : tensor<2xi32> +} + +// CHECK: tfl.sub %arg0, %arg0 +// CHECK-NOT: mhlo + +// ----- + +// CHECK-LABEL: div +func.func @div(%arg0: tensor<2xi32>) -> tensor<2xi32> { + %0 = mhlo.divide %arg0, %arg0 : tensor<2xi32> + func.return %0 : tensor<2xi32> +} + +// CHECK: tfl.div %arg0, %arg0 +// CHECK-NOT: mhlo + +// ----- + +// CHECK-LABEL: atan2 +func.func @atan2(%arg0: tensor<4xf32>) -> tensor<4xf32> { + %0 = mhlo.atan2 %arg0, %arg0 : tensor<4xf32> + func.return %0 : tensor<4xf32> +} + +// CHECK: "tfl.atan2"(%arg0, %arg0) +// CHECK-NOT: mhlo + +// ----- + +// CHECK-LABEL: broadcast_add +func.func @broadcast_add(%arg0: tensor<1x1xf32>, %arg1: tensor<1x1000xf32>) -> (tensor<1x1000xf32>, tensor<1x1000xf32>) { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 1]> : tensor<2xi64>}> : (tensor<1x1xf32>) -> tensor<1x1000xf32> + %1 = mhlo.add %0, %arg1 : tensor<1x1000xf32> + %2 = mhlo.add %arg1, %0 : tensor<1x1000xf32> + func.return %1, %2 : tensor<1x1000xf32>, tensor<1x1000xf32> +} + +// CHECK: tfl.add(%arg0, %arg1) <{fused_activation_function = "NONE"}> : (tensor<1x1xf32>, tensor<1x1000xf32>) -> tensor<1x1000xf32> +// CHECK: tfl.add(%arg1, %arg0) <{fused_activation_function = "NONE"}> : (tensor<1x1000xf32>, tensor<1x1xf32>) -> tensor<1x1000xf32> + +// ----- + +// CHECK-LABEL: broadcast_add_cst +func.func @broadcast_add_cst(%arg1: tensor<1x1000xf32>) -> (tensor<1x1000xf32>, tensor<1x1000xf32>) { + %cst = mhlo.constant dense<1.200000e+01> : tensor + %0 = "mhlo.broadcast_in_dim"(%cst) <{broadcast_dimensions = dense<> : tensor<0xi64>}> : (tensor) -> tensor<1x1000xf32> + %1 = mhlo.add %0, %arg1 : tensor<1x1000xf32> + %2 = mhlo.add %arg1, %0 : tensor<1x1000xf32> + func.return %1, %2 : tensor<1x1000xf32>, tensor<1x1000xf32> +} + +// CHECK: tfl.add{{.*}}(tensor<1x1000xf32>, tensor) -> tensor<1x1000xf32> + +// ----- + +// CHECK-LABEL: broadcast_div +func.func @broadcast_div(%arg0: tensor<1x1xf32>, %arg1: tensor<1x1000xf32>) -> (tensor<1x1000xf32>, tensor<1x1000xf32>) { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 1]> : tensor<2xi64>}> : (tensor<1x1xf32>) -> tensor<1x1000xf32> + %1 = mhlo.divide %0, %arg1 : tensor<1x1000xf32> + %2 = mhlo.divide %arg1, %0 : tensor<1x1000xf32> + func.return %1, %2 : tensor<1x1000xf32>, tensor<1x1000xf32> +} + +// CHECK: tfl.div(%arg0, %arg1) <{fused_activation_function = "NONE"}> : (tensor<1x1xf32>, tensor<1x1000xf32>) -> tensor<1x1000xf32> +// CHECK: tfl.div(%arg1, %arg0) <{fused_activation_function = "NONE"}> : (tensor<1x1000xf32>, tensor<1x1xf32>) -> tensor<1x1000xf32> + +// ----- + +// CHECK-LABEL: broadcast_maximum +func.func @broadcast_maximum(%arg0: tensor<1x1xf32>, %arg1: tensor<1x1000xf32>) -> (tensor<1x1000xf32>, tensor<1x1000xf32>) { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 1]> : tensor<2xi64>}> : (tensor<1x1xf32>) -> tensor<1x1000xf32> + %1 = mhlo.maximum %0, %arg1 : tensor<1x1000xf32> + %2 = mhlo.maximum %arg1, %0 : tensor<1x1000xf32> + func.return %1, %2 : tensor<1x1000xf32>, tensor<1x1000xf32> +} + +// CHECK: "tfl.maximum"(%arg0, %arg1) : (tensor<1x1xf32>, tensor<1x1000xf32>) -> tensor<1x1000xf32> +// CHECK: "tfl.maximum"(%arg1, %arg0) : (tensor<1x1000xf32>, tensor<1x1xf32>) -> tensor<1x1000xf32> + +// ----- + +// CHECK-LABEL: broadcast_minimum +func.func @broadcast_minimum(%arg0: tensor<1x1xf32>, %arg1: tensor<1x1000xf32>) -> (tensor<1x1000xf32>, tensor<1x1000xf32>) { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 1]> : tensor<2xi64>}> : (tensor<1x1xf32>) -> tensor<1x1000xf32> + %1 = mhlo.minimum %0, %arg1 : tensor<1x1000xf32> + %2 = mhlo.minimum %arg1, %0 : tensor<1x1000xf32> + func.return %1, %2 : tensor<1x1000xf32>, tensor<1x1000xf32> +} + +// CHECK: "tfl.minimum"(%arg0, %arg1) : (tensor<1x1xf32>, tensor<1x1000xf32>) -> tensor<1x1000xf32> +// CHECK: "tfl.minimum"(%arg1, %arg0) : (tensor<1x1000xf32>, tensor<1x1xf32>) -> tensor<1x1000xf32> + +// ----- + +// CHECK-LABEL: broadcast_mul +func.func @broadcast_mul(%arg0: tensor<1x1xf32>, %arg1: tensor<1x1000xf32>) -> (tensor<1x1000xf32>, tensor<1x1000xf32>) { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 1]> : tensor<2xi64>}> : (tensor<1x1xf32>) -> tensor<1x1000xf32> + %1 = mhlo.multiply %0, %arg1 : tensor<1x1000xf32> + %2 = mhlo.multiply %arg1, %0 : tensor<1x1000xf32> + func.return %1, %2 : tensor<1x1000xf32>, tensor<1x1000xf32> +} + +// CHECK: tfl.mul(%arg0, %arg1) <{fused_activation_function = "NONE"}> : (tensor<1x1xf32>, tensor<1x1000xf32>) -> tensor<1x1000xf32> +// CHECK: tfl.mul(%arg1, %arg0) <{fused_activation_function = "NONE"}> : (tensor<1x1000xf32>, tensor<1x1xf32>) -> tensor<1x1000xf32> + +// ----- + +// CHECK-LABEL: broadcast_sub +func.func @broadcast_sub(%arg0: tensor<1x1xf32>, %arg1: tensor<1x1000xf32>) -> (tensor<1x1000xf32>, tensor<1x1000xf32>) { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 1]> : tensor<2xi64>}> : (tensor<1x1xf32>) -> tensor<1x1000xf32> + %1 = mhlo.subtract %0, %arg1 : tensor<1x1000xf32> + %2 = mhlo.subtract %arg1, %0 : tensor<1x1000xf32> + func.return %1, %2 : tensor<1x1000xf32>, tensor<1x1000xf32> +} + +// CHECK: tfl.sub(%arg0, %arg1) <{fused_activation_function = "NONE"}> : (tensor<1x1xf32>, tensor<1x1000xf32>) -> tensor<1x1000xf32> +// CHECK: tfl.sub(%arg1, %arg0) <{fused_activation_function = "NONE"}> : (tensor<1x1000xf32>, tensor<1x1xf32>) -> tensor<1x1000xf32> + +// ----- + +// CHECK-LABEL: broadcast_pow +func.func @broadcast_pow(%arg0: tensor<1xi32>, %arg1: tensor<4xi32>) -> (tensor<4xi32>, tensor<4xi32>) { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0]> : tensor<1xi64>}> : (tensor<1xi32>) -> tensor<4xi32> + %1 = mhlo.power %0, %arg1 : tensor<4xi32> + %2 = mhlo.power %arg1, %0 : tensor<4xi32> + func.return %1, %2 : tensor<4xi32>, tensor<4xi32> +} + +// CHECK: tfl.pow(%arg0, %arg1) : (tensor<1xi32>, tensor<4xi32>) -> tensor<4xi32> +// CHECK: tfl.pow(%arg1, %arg0) : (tensor<4xi32>, tensor<1xi32>) -> tensor<4xi32> + +// ----- + +//===----------------------------------------------------------------------===// +// mhlo ternary ops +//===----------------------------------------------------------------------===// + +// CHECK-LABEL: clamp +func.func @clamp(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor { + %0 = "mhlo.clamp"(%arg0, %arg1, %arg2) : (tensor, tensor, tensor) -> tensor + func.return %0 : tensor +} + +// CHECK-NEXT: %0 = "tfl.minimum"(%arg1, %arg2) +// CHECK-NEXT: %1 = "tfl.maximum"(%0, %arg0) +// CHECK-NEXT: return %1 : tensor + +// ----- + +// CHECK-LABEL: select +func.func @select(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor { + %0 = "mhlo.select"(%arg0, %arg1, %arg2) : (tensor, tensor, tensor) -> tensor + func.return %0 : tensor +} + +// CHECK: "tfl.select"(%arg0, %arg1, %arg2) +// CHECK-NOT: mhlo + +// ----- + +//===----------------------------------------------------------------------===// +// mhlo.while +//===----------------------------------------------------------------------===// + +// CHECK-LABEL: while_with_variadic +func.func @while_with_variadic() -> (tensor, tensor, tensor) { + %cst = arith.constant dense<1> : tensor + %cst_0 = arith.constant dense<0> : tensor + %cst_1 = arith.constant dense<1000> : tensor + %0:3 = "mhlo.while"(%cst_0, %cst, %cst_1) ({ + ^bb0(%arg0: tensor, %arg1: tensor, %arg2: tensor): + %1 = "mhlo.compare"(%arg0, %arg2) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + "mhlo.return"(%1) : (tensor) -> () + }, { + ^bb0(%arg0: tensor, %arg1: tensor, %arg2: tensor): + %1 = mhlo.add %arg0, %arg1 : tensor + "mhlo.return"(%1, %arg1, %arg2) : (tensor, tensor, tensor) -> () + }) : (tensor, tensor, tensor) -> (tensor, tensor, tensor) + func.return %0#0, %0#1, %0#2 : tensor, tensor, tensor +} + + +// CHECK-DAG: %cst = arith.constant dense<1> : tensor +// CHECK-DAG: %cst_0 = arith.constant dense<0> : tensor +// CHECK-DAG: %cst_1 = arith.constant dense<1000> : tensor +// CHECK: %0:3 = "tfl.while"(%cst_0, %cst, %cst_1) <{is_stateless = false}> ({ +// CHECK: ^bb0(%arg0: tensor, %arg1: tensor, %arg2: tensor): +// CHECK: %1 = tfl.less(%arg0, %arg2) : (tensor, tensor) -> tensor +// CHECK: "tfl.yield"(%1) : (tensor) -> () +// CHECK: }, { +// CHECK: ^bb0(%arg0: tensor, %arg1: tensor, %arg2: tensor): +// CHECK: %1 = tfl.add %arg0, %arg1 {fused_activation_function = "NONE"} : tensor +// CHECK: "tfl.yield"(%1, %arg1, %arg2) : (tensor, tensor, tensor) -> () +// CHECK: }) : (tensor, tensor, tensor) -> (tensor, tensor, tensor) + +// ----- + +// CHECK-LABEL: while_with_reduce +func.func @while_with_reduce(%arg0: tensor<1x256xf32>, %arg1: tensor<1xf32>) -> (tensor, tensor, tensor, tensor<1xf32>) { + %cst = arith.constant dense<1> : tensor + %cst_0 = arith.constant dense<0> : tensor + %cst_1 = arith.constant dense<1000> : tensor + %0:5 = "mhlo.while"(%cst_0, %cst, %cst_1, %arg0 , %arg1) ({ + ^bb0(%arg2: tensor, %arg3: tensor, %arg4: tensor, %arg5: tensor<1x256xf32>, %arg6: tensor<1xf32>): + %1 = "mhlo.compare"(%arg2, %arg4) {comparison_direction = #mhlo} : (tensor, tensor) -> tensor + "mhlo.return"(%1) : (tensor) -> () + }, { + ^bb0(%arg2: tensor, %arg3: tensor, %arg4: tensor, %arg5: tensor<1x256xf32>, %arg6: tensor<1xf32>): + %1 = mhlo.add %arg2, %arg3 : tensor + %2 = mhlo.constant dense<0.000000e+00> : tensor + %3 = "mhlo.reduce"(%arg5, %2) ({ + ^bb0(%arg7: tensor, %arg8: tensor): + %4 = mhlo.add %arg7, %arg8 : tensor + "mhlo.return"(%4) : (tensor) -> () + }) {dimensions = dense<1> : tensor<1xi64>} : (tensor<1x256xf32>, tensor) -> tensor<1xf32> + %5 = mhlo.add %3, %arg6 : tensor<1xf32> + "mhlo.return"(%1, %arg3, %arg4, %arg5, %5) : (tensor, tensor, tensor, tensor<1x256xf32>, tensor<1xf32>) -> () + }) : (tensor, tensor, tensor, tensor<1x256xf32>, tensor<1xf32>) -> (tensor, tensor, tensor, tensor<1x256xf32>, tensor<1xf32>) + func.return %0#0, %0#1, %0#2, %0#4: tensor, tensor, tensor, tensor<1xf32> +} + +// CHECK-DAG: %cst = arith.constant dense<0.000000e+00> : tensor +// CHECK-DAG: %cst_0 = arith.constant dense<1> : tensor +// CHECK-DAG: %cst_1 = arith.constant dense<0> : tensor +// CHECK-DAG: %cst_2 = arith.constant dense<1000> : tensor +// CHECK: %0:5 = "tfl.while"(%cst_1, %cst_0, %cst_2, %arg0, %arg1) <{is_stateless = false}> ({ +// CHECK: ^bb0(%arg2: tensor, %arg3: tensor, %arg4: tensor, %arg5: tensor<1x256xf32>, %arg6: tensor<1xf32>): +// CHECK: %1 = tfl.less(%arg2, %arg4) : (tensor, tensor) -> tensor +// CHECK: "tfl.yield"(%1) : (tensor) -> () +// CHECK: ^bb0(%arg2: tensor, %arg3: tensor, %arg4: tensor, %arg5: tensor<1x256xf32>, %arg6: tensor<1xf32>): +// CHECK: %1 = tfl.add %arg2, %arg3 {fused_activation_function = "NONE"} : tensor +// CHECK: %cst_3 = arith.constant dense<1> : tensor<1xi32> +// CHECK: %2 = "tfl.sum"(%arg5, %cst_3) <{keep_dims = false}> : (tensor<1x256xf32>, tensor<1xi32>) -> tensor<1xf32> +// CHECK: %3 = tfl.add %2, %arg6 {fused_activation_function = "NONE"} : tensor<1xf32> +// CHECK: "tfl.yield"(%1, %arg3, %arg4, %arg5, %3) : (tensor, tensor, tensor, tensor<1x256xf32>, tensor<1xf32>) -> () +// CHECK: }) : (tensor, tensor, tensor, tensor<1x256xf32>, tensor<1xf32>) -> (tensor, tensor, tensor, tensor<1x256xf32>, tensor<1xf32>) + +// ----- + +//===----------------------------------------------------------------------===// +// data movement and shaping +//===----------------------------------------------------------------------===// + +// CHECK-LABEL: func @get_dimension_size( +func.func @get_dimension_size(%arg0: tensor<4x256x?xf32>) -> tensor { + %0 = "mhlo.get_dimension_size"(%arg0) <{dimension = 1 : i64}> : (tensor<4x256x?xf32>) -> tensor + func.return %0 : tensor +} + +// This gets folded. +// CHECK: %cst = arith.constant dense<256> : tensor + +// ----- + +// CHECK-LABEL: reverse +func.func @reverse(%arg0: tensor<3x2xi32>) -> tensor<3x2xi32> { + %0 = "mhlo.reverse"(%arg0) <{dimensions = dense<0> : tensor<1xi64>}> : (tensor<3x2xi32>) -> tensor<3x2xi32> + func.return %0 : tensor<3x2xi32> +} + +// CHECK: %cst = arith.constant dense<0> : tensor<1xi64> +// CHECK: %0 = "tfl.cast"(%cst) : (tensor<1xi64>) -> tensor<1xi32> +// CHECK: %1 = "tfl.reverse_v2"(%arg0, %0) : (tensor<3x2xi32>, tensor<1xi32>) -> tensor<3x2xi32> +// CHECK: return %1 : tensor<3x2xi32> + +// ----- + +// CHECK-LABEL: concat +func.func @concat(%arg0: tensor<3x3xf32>, %arg1: tensor<3x3xf32>) -> tensor<6x3xf32> { + %0 = "mhlo.concatenate"(%arg0, %arg1) <{dimension = 0 : i64}> : (tensor<3x3xf32>, tensor<3x3xf32>) -> tensor<6x3xf32> + func.return %0 : tensor<6x3xf32> +} + +// CHECK: %0 = "tfl.concatenation"(%arg0, %arg1) <{axis = 0 : i32, fused_activation_function = "NONE"}> : (tensor<3x3xf32>, tensor<3x3xf32>) -> tensor<6x3xf32> + +// ----- + +// CHECK-LABEL: broadcast_in_dim_tfl_style +func.func @broadcast_in_dim_tfl_style(%arg0: tensor<8x1x16xf32>) -> tensor<3x8x8x16xf32> { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[1, 2, 3]> : tensor<3xi64>, name = "broadcast.0"}> : (tensor<8x1x16xf32>) -> tensor<3x8x8x16xf32> + func.return %0 : tensor<3x8x8x16xf32> +} + +// CHECK: %cst = arith.constant dense<[3, 8, 8, 16]> : tensor<4xi64> +// CHECK: %0 = "tfl.broadcast_to"(%arg0, %cst) : (tensor<8x1x16xf32>, tensor<4xi64>) -> tensor<3x8x8x16xf32> + +// ----- + +// CHECK-LABEL: broadcast_in_dim_general_case +func.func @broadcast_in_dim_general_case(%arg0: tensor<3x1x16xf32>) -> tensor<3x8x8x16xf32> { + %0 = "mhlo.broadcast_in_dim"(%arg0) <{broadcast_dimensions = dense<[0, 2, 3]> : tensor<3xi64>, name = "broadcast.0"}> : (tensor<3x1x16xf32>) -> tensor<3x8x8x16xf32> + func.return %0 : tensor<3x8x8x16xf32> +} + +// CHECK: %cst = arith.constant dense<[3, 1, 1, 16]> : tensor<4xi32> +// CHECK: %0 = "tfl.reshape"(%arg0, %cst) : (tensor<3x1x16xf32>, tensor<4xi32>) -> tensor<3x1x1x16xf32> +// CHECK: %cst_0 = arith.constant dense<[3, 8, 8, 16]> : tensor<4xi64> +// CHECK: %1 = "tfl.broadcast_to"(%0, %cst_0) : (tensor<3x1x1x16xf32>, tensor<4xi64>) -> tensor<3x8x8x16xf32> + +// ----- + +// CHECK-LABEL: dynamic_broadcast_in_dim_tf_style +func.func @dynamic_broadcast_in_dim_tf_style(%arg0: tensor, %arg1: tensor<5xi32>) -> tensor { + %0 = "mhlo.dynamic_broadcast_in_dim"(%arg0, %arg1) <{broadcast_dimensions = dense<[0, 1, 2, 3, 4]> : tensor<5xi64>}> : (tensor, tensor<5xi32>) -> tensor + func.return %0 : tensor +} + +// CHECK: %0 = "tfl.broadcast_to"(%arg0, %arg1) : (tensor, tensor<5xi32>) -> tensor + +// ----- + +// CHECK-LABEL: dynamic_broadcast_in_dim_general_case_expand_back_dims +func.func @dynamic_broadcast_in_dim_general_case_expand_back_dims(%arg0: tensor, %arg1: tensor<4xi32>) -> tensor { + %0 = "mhlo.dynamic_broadcast_in_dim"(%arg0, %arg1) <{broadcast_dimensions = dense<[0, 1]> : tensor<2xi64>}> : (tensor, tensor<4xi32>) -> tensor + func.return %0 : tensor +} + +// CHECK: %cst = arith.constant dense<2> : tensor +// CHECK: %0 = "tfl.expand_dims"(%arg0, %cst) : (tensor, tensor) -> tensor +// CHECK: %cst_0 = arith.constant dense<3> : tensor +// CHECK: %1 = "tfl.expand_dims"(%0, %cst_0) : (tensor, tensor) -> tensor +// CHECK: %2 = "tfl.broadcast_to"(%1, %arg1) : (tensor, tensor<4xi32>) -> tensor + + +// ----- + +//===----------------------------------------------------------------------===// +// mhlo.case +//===----------------------------------------------------------------------===// + +// CHECK-LABEL: case_func +func.func @case_func(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> (tensor) { + %0 = "mhlo.case"(%arg0) ({ + %2 = mhlo.add %arg1, %arg2 : tensor + "mhlo.return"(%2) : (tensor) -> () + }, { + %2 = mhlo.multiply %arg1, %arg1 : tensor + "mhlo.return"(%2) : (tensor) -> () + }) : (tensor) -> tensor + func.return %0: tensor +} + +// CHECK: %[[CST:.*]] = arith.constant dense<0> : tensor +// CHECK: %[[PRED:.*]] = tfl.not_equal(%arg0, %[[CST]]) : (tensor, tensor) -> tensor +// CHECK: %[[IF:.*]] = "tfl.if"(%[[PRED]]) ({ +// CHECK: %[[MUL:.*]] = tfl.mul %arg1, %arg1 {fused_activation_function = "NONE"} : tensor +// CHECK: "tfl.yield"(%[[MUL]]) : (tensor) -> () +// CHECK: }, { +// CHECK: %[[ADD:.*]] = tfl.add %arg1, %arg2 {fused_activation_function = "NONE"} : tensor +// CHECK: "tfl.yield"(%[[ADD]]) : (tensor) -> () +// CHECK: }) : (tensor) -> tensor +// CHECK: return %[[IF]] : tensor + +// ----- + +//===----------------------------------------------------------------------===// +// mhlo.if +//===----------------------------------------------------------------------===// + +// CHECK-LABEL: if_label +func.func @if_label(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> (tensor) { + %0 = mhlo.add %arg1, %arg2 : tensor + %1 = "mhlo.if"(%arg0) ({ + "mhlo.return"(%0) : (tensor) -> () + }, { + %2 = mhlo.multiply %arg1, %arg1 : tensor + "mhlo.return"(%2) : (tensor) -> () + }) : (tensor) -> tensor + func.return %1: tensor +} + +// CHECK: %0 = tfl.add %arg1, %arg2 {fused_activation_function = "NONE"} : tensor +// CHECK: %1 = "tfl.if"(%arg0) ({ +// CHECK: "tfl.yield"(%0) : (tensor) -> () +// CHECK: }, { +// CHECK: %2 = tfl.mul %arg1, %arg1 {fused_activation_function = "NONE"} : tensor +// CHECK: "tfl.yield"(%2) : (tensor) -> () +// CHECK: }) : (tensor) -> tensor +// CHECK: return %1 : tensor + +// ----- + +//===----------------------------------------------------------------------===// +// mhlo.fft +//===----------------------------------------------------------------------===// + +// CHECK-LABEL: legalize_fft_mhlo_fft_type_rfft +func.func @legalize_fft_mhlo_fft_type_rfft(%arg0 : tensor<1x1x512xf32>) -> tensor<1x1x257xcomplex> { + %270 = "mhlo.fft"(%arg0) <{fft_length = dense<[1, 512]> : tensor<2xi64>, fft_type = #mhlo}> : (tensor<1x1x512xf32>) -> tensor<1x1x257xcomplex> + return %270: tensor<1x1x257xcomplex> +} + +// CHECK: %cst = arith.constant dense<[1, 512]> : tensor<2xi32> +// CHECK: %0 = "tfl.rfft2d"(%arg0, %cst) : (tensor<1x1x512xf32>, tensor<2xi32>) -> tensor<1x1x257xcomplex> +// CHECK: return %0 : tensor<1x1x257xcomplex> + +// ----- + +// CHECK-LABEL: @fft +func.func @fft(%arg0: tensor<3x9xcomplex>) -> tensor<3x9xcomplex> { + %0 = "mhlo.fft"(%arg0) <{ fft_length = dense<9> : tensor<1xi64>, fft_type = #mhlo }> : (tensor<3x9xcomplex>) -> tensor<3x9xcomplex> + func.return %0 : tensor<3x9xcomplex> +} + +// CHECK: %0 = "mhlo.fft"(%arg0) <{fft_length = dense<9> : tensor<1xi64>, fft_type = #mhlo}> : (tensor<3x9xcomplex>) -> tensor<3x9xcomplex> +// CHECK: return %0 : tensor<3x9xcomplex> + +// ----- + +// CHECK-LABEL: @mhlo_nd_fft +func.func @mhlo_nd_fft(%arg0: tensor<2x3x345x1x256xf32>) -> tensor<2x3x345x1x129xcomplex> { + %0 = "mhlo.fft"(%arg0) <{fft_length = dense<[1, 256]> : tensor<2xi64>, fft_type = #mhlo}> : (tensor<2x3x345x1x256xf32>) -> tensor<2x3x345x1x129xcomplex> + func.return %0 : tensor<2x3x345x1x129xcomplex> +} + +// CHECK: %cst = arith.constant dense<[1, 256]> : tensor<2xi32> +// CHECK: %0 = "tfl.rfft2d"(%arg0, %cst) : (tensor<2x3x345x1x256xf32>, tensor<2xi32>) -> tensor<2x3x345x1x129xcomplex> +// CHECK: return %0 : tensor<2x3x345x1x129xcomplex> + +// ----- + +// CHECK-LABEL: @mhlo_nd_fft_1 +func.func @mhlo_nd_fft_1(%arg0: tensor<2x3x345x4x256xf32>) -> tensor<2x3x345x4x129xcomplex> { + %43 = "mhlo.fft"(%arg0) <{fft_length = dense<[4, 256]> : tensor<2xi64>, fft_type = #mhlo}> : (tensor<2x3x345x4x256xf32>) -> tensor<2x3x345x4x129xcomplex> + return %43 : tensor<2x3x345x4x129xcomplex> +} + +// CHECK: %cst = arith.constant dense<[4, 256]> : tensor<2xi32> +// CHECK: %0 = "tfl.rfft2d"(%arg0, %cst) : (tensor<2x3x345x4x256xf32>, tensor<2xi32>) -> tensor<2x3x345x4x129xcomplex> +// CHECK: return %0 : tensor<2x3x345x4x129xcomplex> + +// ----- + +// CHECK-LABEL: @mhlo_dynamic_fft_1 +func.func @mhlo_dynamic_fft_1(%arg0: tensor) -> tensor> { + %0 = "mhlo.fft"(%arg0) <{fft_length = dense<[1, 2560]> : tensor<2xi64>, fft_type = #mhlo}> : (tensor) -> tensor> + return %0 : tensor> + // CHECK: %cst = arith.constant dense<[1, 2560]> : tensor<2xi32> + // CHECK: %0 = "tfl.rfft2d"(%arg0, %cst) : (tensor, tensor<2xi32>) -> tensor> + // CHECK: return %0 : tensor> +} + +// ----- + +// CHECK-LABEL: @mhlo_dynamic_fft_2 +func.func @mhlo_dynamic_fft_2(%arg0: tensor) -> tensor> { + %9 = "mhlo.fft"(%arg0) <{fft_length = dense<[1, 2560]> : tensor<2xi64>, fft_type = #mhlo}> : (tensor) -> tensor> + return %9 : tensor> + // CHECK: %cst = arith.constant dense<[1, 2560]> : tensor<2xi32> + // CHECK: %0 = "tfl.rfft2d"(%arg0, %cst) : (tensor, tensor<2xi32>) -> tensor> + // CHECK: return %0 : tensor> +} + +//===----------------------------------------------------------------------===// +// mhlo.imag +//===----------------------------------------------------------------------===// + +func.func @mhlo_imag_test1(%arg0: tensor<1x2049xcomplex>) -> tensor<1x2049xf32> { + %339 = mhlo.imag %arg0 : (tensor<1x2049xcomplex>) -> tensor<1x2049xf32> + return %339 : tensor<1x2049xf32> + // CHECK: %0 = "tfl.imag"(%arg0) : (tensor<1x2049xcomplex>) -> tensor<1x2049xf32> + // CHECK: return %0 : tensor<1x2049xf32> +} + +func.func @mhlo_imag_test_neg(%arg0: tensor<1x2049xf32>) -> tensor<1x2049xf32> { + %339 = mhlo.imag %arg0 : (tensor<1x2049xf32>) -> tensor<1x2049xf32> + return %339 : tensor<1x2049xf32> + // CHECK: %0 = mhlo.imag %arg0 : tensor<1x2049xf32> + // CHECK: return %0 : tensor<1x2049xf32> +} + +// ----- + +//===----------------------------------------------------------------------===// +// mhlo.real +//===----------------------------------------------------------------------===// + +func.func @mhlo_real_test1(%arg0: tensor<1x2049xcomplex>) -> tensor<1x2049xf32> { + %339 = mhlo.real %arg0 : (tensor<1x2049xcomplex>) -> tensor<1x2049xf32> + return %339 : tensor<1x2049xf32> + // CHECK: %0 = "tfl.real"(%arg0) : (tensor<1x2049xcomplex>) -> tensor<1x2049xf32> + // CHECK: return %0 : tensor<1x2049xf32> +} + +func.func @mhlo_real_test_neg(%arg0: tensor<1x2049xf32>) -> tensor<1x2049xf32> { + %339 = mhlo.real %arg0 : (tensor<1x2049xf32>) -> tensor<1x2049xf32> + return %339 : tensor<1x2049xf32> + // CHECK: %0 = mhlo.real %arg0 : tensor<1x2049xf32> + // CHECK: return %0 : tensor<1x2049xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tfl_legalize_hlo_custom_call.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tfl_legalize_hlo_custom_call.mlir new file mode 100644 index 0000000000000000000000000000000000000000..52c33c4dcf8cd9417063fadd7323ba42119e5caa --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/tfl_legalize_hlo_custom_call.mlir @@ -0,0 +1,54 @@ +// RUN: odml-to-stablehlo-opt %s -tfl-legalize-hlo -split-input-file | FileCheck %s --dump-input=fail + +// CHECK-LABEL: mhlo_custom_call_test__legalize_string_backend_config +func.func @mhlo_custom_call_test__legalize_string_backend_config(%arg0: tensor<1x4xf32>) -> tensor<1x8xf32> { + %0 = mhlo.custom_call @custom_call.my_custom_op(%arg0) { + api_version = 1 : i32, + backend_config = "this_is_a_test_string" + } : (tensor<1x4xf32>) -> (tensor<1x8xf32>) + func.return %0 : tensor<1x8xf32> + + // CHECK: %0 = "tfl.custom"(%arg0) <{ + // CHECK-SAME: custom_code = "custom_call.my_custom_op", + // CHECK-SAME: custom_option = #tfl + // CHECK-SAME: }> : (tensor<1x4xf32>) -> tensor<1x8xf32> +} + +// CHECK-LABEL: mhlo_custom_call_test__dont_legalize_dict_backend_config +func.func @mhlo_custom_call_test__dont_legalize_dict_backend_config(%arg0: tensor<1x4xf32>) -> tensor<1x8xf32> { + %0 = mhlo.custom_call @custom_call.my_custom_op(%arg0) { + api_version = 4 : i32, + backend_config = {foo = "bar"} + } : (tensor<1x4xf32>) -> (tensor<1x8xf32>) + func.return %0 : tensor<1x8xf32> + + // CHECK: %0 = mhlo.custom_call @custom_call.my_custom_op(%arg0) { + // CHECK-SAME: api_version = 4 : i32, + // CHECK-SAME: backend_config = {foo = "bar"} + // CHECK-SAME: } : (tensor<1x4xf32>) -> tensor<1x8xf32> +} + +// CHECK-LABEL: mhlo_custom_call_test__api_version_4 +func.func @mhlo_custom_call_test__api_version_4(%arg0: tensor<1x4xf32>) -> tensor<1x8xf32> { + %0 = mhlo.custom_call @custom_call.my_custom_op(%arg0) { + api_version = 4 : i32 + } : (tensor<1x4xf32>) -> (tensor<1x8xf32>) + func.return %0 : tensor<1x8xf32> + + // CHECK: %0 = "tfl.custom"(%arg0) <{ + // CHECK-SAME: custom_code = "custom_call.my_custom_op", + // CHECK-SAME: custom_option = #tfl + // CHECK-SAME: }> : (tensor<1x4xf32>) -> tensor<1x8xf32> +} + +// CHECK-LABEL: mhlo_custom_call_does_not_legalize_tf_function +func.func @mhlo_custom_call_does_not_legalize_tf_function(%arg0: tensor<1x4xf32>) -> tensor<1x8xf32> { + %0 = mhlo.custom_call @tf.ResizeBilinear(%arg0) { + backend_config = "this_is_a_test_string" + } : (tensor<1x4xf32>) -> (tensor<1x8xf32>) + func.return %0 : tensor<1x8xf32> + + // CHECK: %0 = mhlo.custom_call @tf.ResizeBilinear(%arg0) { + // CHECK-SAME: backend_config = "this_is_a_test_string" + // CHECK-SAME: } : (tensor<1x4xf32>) -> tensor<1x8xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/torch-build-stablehlo-composite.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/torch-build-stablehlo-composite.mlir new file mode 100644 index 0000000000000000000000000000000000000000..e6c6dfb6d3914df978b50ce3b76d0d2faf39e5af --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/torch-build-stablehlo-composite.mlir @@ -0,0 +1,32 @@ +// RUN: odml-to-stablehlo-opt %s -build-stablehlo-composite -cse -canonicalize -cse | FileCheck %s --dump-input=fail + +module { + func.func public @build_nested_composite(%arg0: tensor<2x2xf32>) -> tensor<2x2xf32> { + %c = stablehlo.constant dense<1> : tensor + %c_0 = stablehlo.constant dense<2> : tensor + %0 = stablehlo.custom_call @mark_tensor(%arg0) {backend_config = "{\22name\22: \22test.outer\22, \22pos\22: 0, \22id\22: \22f8cf5dba52ff4995b9f3810647aa69e2\22, \22is_input\22: true, \22attr\22: null}"} : (tensor<2x2xf32>) -> tensor<2x2xf32> + %1 = stablehlo.multiply %c_0, %c : tensor + %2 = stablehlo.convert %1 : (tensor) -> tensor + %3 = stablehlo.broadcast_in_dim %2, dims = [] : (tensor) -> tensor<2x2xf32> + %4 = stablehlo.add %0, %3 : tensor<2x2xf32> + %5 = stablehlo.custom_call @mark_tensor(%4) {backend_config = "{\22name\22: \22test.inner\22, \22pos\22: 0, \22id\22: \22709cea3466314458963f3262d1deb27e\22, \22is_input\22: true, \22attr\22: null}"} : (tensor<2x2xf32>) -> tensor<2x2xf32> + %6 = stablehlo.multiply %c, %c : tensor + %7 = stablehlo.convert %6 : (tensor) -> tensor + %8 = stablehlo.broadcast_in_dim %7, dims = [] : (tensor) -> tensor<2x2xf32> + %9 = stablehlo.add %5, %8 : tensor<2x2xf32> + %10 = stablehlo.custom_call @mark_tensor(%9) {backend_config = "{\22name\22: \22test.inner\22, \22pos\22: 0, \22id\22: \22709cea3466314458963f3262d1deb27e\22, \22is_input\22: false, \22attr\22: null}"} : (tensor<2x2xf32>) -> tensor<2x2xf32> + %11 = stablehlo.custom_call @mark_tensor(%10) {backend_config = "{\22name\22: \22test.outer\22, \22pos\22: 0, \22id\22: \22f8cf5dba52ff4995b9f3810647aa69e2\22, \22is_input\22: false, \22attr\22: null}"} : (tensor<2x2xf32>) -> tensor<2x2xf32> + return %11 : tensor<2x2xf32> + } +} + +// CHECK-LABEL: build_nested_composite +// CHECK: stablehlo.composite "test.outer" +// CHECK: return +// CHECK: func.func private @test.inner.impl +// CHECK: stablehlo.add %arg0, +// CHECK: return +// CHECK: func.func private @test.outer.impl +// CHECK: stablehlo.add %arg0, +// CHECK: stablehlo.composite "test.inner" +// CHECK: return \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/unfold_splat_constant_pass.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/unfold_splat_constant_pass.mlir new file mode 100644 index 0000000000000000000000000000000000000000..6e4dee81b97eb14a6b9ac632188cf76395243a58 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/unfold_splat_constant_pass.mlir @@ -0,0 +1,29 @@ +// RUN: odml-to-stablehlo-opt %s -unfold-splat-constant-pass -cse -verify-diagnostics | FileCheck %s + +// CHECK-LABEL: @unfold_splat_constant_float +func.func @unfold_splat_constant_float() -> tensor<1x750xf32> { + %cst = mhlo.constant dense<7.680000e+02> : tensor<1x750xf32> + func.return %cst : tensor<1x750xf32> + + // CHECK-DAG: %0 = mhlo.constant dense<7.680000e+02> : tensor + // CHECK: %1 = "mhlo.broadcast_in_dim"(%0) <{broadcast_dimensions = dense<> : tensor<0xi64>}> : (tensor) -> tensor<1x750xf32> + // CHECK: return %1 : tensor<1x750xf32> +} + +// CHECK-LABEL: @unfold_splat_constant_integer +func.func @unfold_splat_constant_integer() -> tensor<1x750xi32> { + %cst = mhlo.constant dense<1> : tensor<1x750xi32> + func.return %cst : tensor<1x750xi32> + + // CHECK-DAG: %0 = mhlo.constant dense<1> : tensor + // CHECK: %1 = "mhlo.broadcast_in_dim"(%0) <{broadcast_dimensions = dense<> : tensor<0xi64>}> : (tensor) -> tensor<1x750xi32> + // CHECK: return %1 : tensor<1x750xi32> +} + +// CHECK-LABEL: @splat_scalar_no_change +func.func @splat_scalar_no_change() -> (tensor, tensor) { + // CHECK-NOT: mhlo.broadcast_in_dim + %cst0 = mhlo.constant dense<0.000000e+00> : tensor + %cst1 = mhlo.constant dense<0> : tensor + func.return %cst0, %cst1 : tensor, tensor +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/unfuse_mhlo_batch_norm.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/unfuse_mhlo_batch_norm.mlir new file mode 100644 index 0000000000000000000000000000000000000000..1f7657362fcffbcc56269231ec0403e39a53f2e5 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/unfuse_mhlo_batch_norm.mlir @@ -0,0 +1,155 @@ +// RUN: odml-to-stablehlo-opt %s -unfuse-mhlo-batch-norm-pass -cse -verify-diagnostics | FileCheck %s + +// CHECK-LABEL: @batchNormInference_2D_inner_features +// CHECK-SAME: %[[X:[^:[:space:]]+]] +// CHECK-SAME: %[[SCALE:[^:[:space:]]+]] +// CHECK-SAME: %[[OFFSET:[^:[:space:]]+]] +// CHECK-SAME: %[[MEAN:[^:[:space:]]+]] +// CHECK-SAME: %[[VARIANCE:[^:[:space:]]+]] +func.func @batchNormInference_2D_inner_features( + %x: tensor<4x256xf32>, %scale: tensor<256xf32>, %offset: tensor<256xf32>, + %mean: tensor<256xf32>, %variance: tensor<256xf32>) + -> (tensor<4x256xf32>) { + // CHECK-DAG: %[[EPS_BCAST:.+]] = mhlo.constant dense<1.001000e-05> : tensor<256xf32> + // CHECK-DAG: %[[VARIANCE_EPS:.+]] = mhlo.add %[[VARIANCE]], %[[EPS_BCAST]] : tensor<256xf32> + // CHECK-DAG: %[[VARIANCE_EPS_RSQRT:.+]] = mhlo.rsqrt %[[VARIANCE_EPS]] : tensor<256xf32> + // CHECK-DAG: %[[MULTIPLIER:.+]] = mhlo.multiply %[[VARIANCE_EPS_RSQRT]], %[[SCALE]] : tensor<256xf32> + // CHECK-DAG: %[[MUL_MEAN:.+]] = mhlo.multiply %[[MULTIPLIER]], %[[MEAN]] : tensor<256xf32> + // CHECK-DAG: %[[RHS:.+]] = mhlo.subtract %[[OFFSET]], %[[MUL_MEAN]] : tensor<256xf32> + // CHECK-DAG: %[[MULTIPLIER_BCAST:.+]] = "mhlo.broadcast_in_dim"(%[[MULTIPLIER]]) <{broadcast_dimensions = dense<1> : tensor<1xi64>}> : (tensor<256xf32>) -> tensor<4x256xf32> + // CHECK-DAG: %[[X_NORMED:.+]] = mhlo.multiply %[[X]], %[[MULTIPLIER_BCAST]] : tensor<4x256xf32> + // CHECK-DAG: %[[RHS_BCAST:.+]] = "mhlo.broadcast_in_dim"(%[[RHS]]) <{broadcast_dimensions = dense<1> : tensor<1xi64>}> : (tensor<256xf32>) -> tensor<4x256xf32> + // CHECK-DAG: %[[RESULT:.+]] = mhlo.add %[[X_NORMED]], %[[RHS_BCAST]] : tensor<4x256xf32> + %0 = "mhlo.batch_norm_inference"(%x, %scale, %offset, %mean, %variance) + {epsilon = 1.001000e-05 : f32, feature_index = 1 : i64} : + (tensor<4x256xf32>, tensor<256xf32>, tensor<256xf32>, tensor<256xf32>, + tensor<256xf32>) -> tensor<4x256xf32> + // CHECK-DAG: return %[[RESULT]] + func.return %0 : tensor<4x256xf32> +} + +// CHECK-LABEL: @batchNormInference_4D_middle_features +// CHECK-SAME: %[[X:[^:[:space:]]+]] +// CHECK-SAME: %[[SCALE:[^:[:space:]]+]] +// CHECK-SAME: %[[OFFSET:[^:[:space:]]+]] +// CHECK-SAME: %[[MEAN:[^:[:space:]]+]] +// CHECK-SAME: %[[VARIANCE:[^:[:space:]]+]] +func.func @batchNormInference_4D_middle_features( + %x: tensor<3x4x256x6xf32>, %scale: tensor<256xf32>, %offset: tensor<256xf32>, + %mean: tensor<256xf32>, %variance: tensor<256xf32>) + -> (tensor<3x4x256x6xf32>) { + // CHECK-DAG: %[[EPS_BCAST:.+]] = mhlo.constant dense<1.001000e-05> : tensor<256xf32> + // CHECK-DAG: %[[VARIANCE_EPS:.+]] = mhlo.add %[[VARIANCE]], %[[EPS_BCAST]] : tensor<256xf32> + // CHECK-DAG: %[[VARIANCE_EPS_RSQRT:.+]] = mhlo.rsqrt %[[VARIANCE_EPS]] : tensor<256xf32> + // CHECK-DAG: %[[MULTIPLIER:.+]] = mhlo.multiply %[[VARIANCE_EPS_RSQRT]], %[[SCALE]] : tensor<256xf32> + // CHECK-DAG: %[[MUL_MEAN:.+]] = mhlo.multiply %[[MULTIPLIER]], %[[MEAN]] : tensor<256xf32> + // CHECK-DAG: %[[RHS:.+]] = mhlo.subtract %[[OFFSET]], %[[MUL_MEAN]] : tensor<256xf32> + // CHECK-DAG: %[[MULTIPLIER_BCAST:.+]] = "mhlo.broadcast_in_dim"(%[[MULTIPLIER]]) <{broadcast_dimensions = dense<2> : tensor<1xi64>}> : (tensor<256xf32>) -> tensor<3x4x256x6xf32> + // CHECK-DAG: %[[RHS_BCAST:.+]] = "mhlo.broadcast_in_dim"(%[[RHS]]) <{broadcast_dimensions = dense<2> : tensor<1xi64>}> : (tensor<256xf32>) -> tensor<3x4x256x6xf32> + %0 = "mhlo.batch_norm_inference"(%x, %scale, %offset, %mean, %variance) + {epsilon = 1.001000e-05 : f32, feature_index = 2 : i64} : + (tensor<3x4x256x6xf32>, tensor<256xf32>, tensor<256xf32>, tensor<256xf32>, + tensor<256xf32>) -> tensor<3x4x256x6xf32> + func.return %0 : tensor<3x4x256x6xf32> +} + +// CHECK-LABEL: @batchNormInference_dynamic_shape +// Validate that dynamic shapes are handled properly. +// CHECK-SAME: %[[X:[^:[:space:]]+]] +// CHECK-SAME: %[[SCALE:[^:[:space:]]+]] +// CHECK-SAME: %[[OFFSET:[^:[:space:]]+]] +// CHECK-SAME: %[[MEAN:[^:[:space:]]+]] +// CHECK-SAME: %[[VARIANCE:[^:[:space:]]+]] +func.func @batchNormInference_dynamic_shape( + %x: tensor, %scale: tensor, %offset: tensor, + %mean: tensor, %variance: tensor) + -> tensor { + // CHECK-DAG: %[[EPS:.+]] = mhlo.constant dense<1.000000e-03> : tensor + // CHECK-DAG: %[[VAR_SHAPE:.+]] = shape.shape_of %[[VARIANCE]] : tensor -> tensor<1xindex> + // CHECK-DAG: %[[EPS_BCAST:.+]] = "mhlo.dynamic_broadcast_in_dim"(%[[EPS]], %[[VAR_SHAPE]]) <{broadcast_dimensions = dense<> : tensor<0xi64>}> : (tensor, tensor<1xindex>) -> tensor + // CHECK-DAG: %[[VARIANCE_EPS:.+]] = mhlo.add %[[VARIANCE]], %[[EPS_BCAST]] : tensor + // CHECK-DAG: %[[R_STDDEV:.+]] = mhlo.rsqrt %[[VARIANCE_EPS]] : tensor + // CHECK-DAG: %[[MULTIPLIER:.+]] = mhlo.multiply %[[R_STDDEV]], %[[SCALE]] : tensor + // CHECK-DAG: %[[MUL_MEAN:.+]] = mhlo.multiply %[[MULTIPLIER]], %[[MEAN]] : tensor + // CHECK-DAG: %[[RHS:.+]] = mhlo.subtract %[[OFFSET]], %[[MUL_MEAN]] : tensor + // CHECK-DAG: %[[X_SHAPE:.+]] = shape.shape_of %[[X]] : tensor -> tensor<4xindex> + // CHECK-DAG: %[[MULTIPLIER_BCAST:.+]] = "mhlo.dynamic_broadcast_in_dim"(%[[MULTIPLIER]], %[[X_SHAPE]]) <{broadcast_dimensions = dense<1> : tensor<1xi64>}> : (tensor, tensor<4xindex>) -> tensor + // CHECK-DAG: %[[X_NORMED:.+]] = mhlo.multiply %[[X]], %[[MULTIPLIER_BCAST]] : tensor + // CHECK-DAG: %[[RHS_BCAST:.+]] = "mhlo.dynamic_broadcast_in_dim"(%[[RHS]], %[[X_SHAPE]]) <{broadcast_dimensions = dense<1> : tensor<1xi64>}> : (tensor, tensor<4xindex>) -> tensor + // CHECK-DAG: %[[RESULT:.+]] = mhlo.add %[[X_NORMED]], %[[RHS_BCAST]] : tensor + %0 = "mhlo.batch_norm_inference"(%x, %scale, %offset, %mean, %variance) + {epsilon = 0.001 : f32, feature_index = 1 : i64} : + (tensor, tensor, tensor, tensor, + tensor) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: @batchNormInference_f64 +// Validate that epsilon is properly promoted to f64 +// CHECK-DAG: %[[EPS:.+]] = mhlo.constant dense<1.000000e+00> : tensor<256xf64> +func.func @batchNormInference_f64( + %x: tensor<4x256xf64>, %scale: tensor<256xf64>, %offset: tensor<256xf64>, + %mean: tensor<256xf64>, %variance: tensor<256xf64>) + -> (tensor<4x256xf64>) { + %0 = "mhlo.batch_norm_inference"(%x, %scale, %offset, %mean, %variance) + {epsilon = 1.0 : f32, feature_index = 1 : i64} : + (tensor<4x256xf64>, tensor<256xf64>, tensor<256xf64>, tensor<256xf64>, + tensor<256xf64>) -> tensor<4x256xf64> + func.return %0 : tensor<4x256xf64> +} + +// CHECK-LABEL: @batchNormInference_f16 +// Validate that epsilon is properly down to f16 +// CHECK-DAG: %[[EPS:.+]] = mhlo.constant dense<1.000000e+00> : tensor<256xf16> +func.func @batchNormInference_f16( + %x: tensor<4x256xf16>, %scale: tensor<256xf16>, %offset: tensor<256xf16>, + %mean: tensor<256xf16>, %variance: tensor<256xf16>) + -> (tensor<4x256xf16>) { + %0 = "mhlo.batch_norm_inference"(%x, %scale, %offset, %mean, %variance) + {epsilon = 1.0 : f32, feature_index = 1 : i64} : + (tensor<4x256xf16>, tensor<256xf16>, tensor<256xf16>, tensor<256xf16>, + tensor<256xf16>) -> tensor<4x256xf16> + func.return %0 : tensor<4x256xf16> +} + +// Validate that epsilon is overflow +func.func @batchNormInference_f16_overflow( + %x: tensor<4x256xf16>, %scale: tensor<256xf16>, %offset: tensor<256xf16>, + %mean: tensor<256xf16>, %variance: tensor<256xf16>) + -> (tensor<4x256xf16>) { + // expected-warning @+1 {{Could not convert batch_norm epsilon to target fp type: opStatus = 24}} + %0 = "mhlo.batch_norm_inference"(%x, %scale, %offset, %mean, %variance) + {epsilon = 0.00000001 : f32, feature_index = 1 : i64} : + (tensor<4x256xf16>, tensor<256xf16>, tensor<256xf16>, tensor<256xf16>, + tensor<256xf16>) -> tensor<4x256xf16> + func.return %0 : tensor<4x256xf16> +} + +// CHECK-LABEL: @batchNormTraining_4D_middle_features +// CHECK-SAME: %[[X:[^:[:space:]]+]] +// CHECK-SAME: %[[SCALE:[^:[:space:]]+]] +// CHECK-SAME: %[[OFFSET:[^:[:space:]]+]] +func.func @batchNormTraining_4D_middle_features( + %x: tensor<3x4x256x6xf32>, %scale: tensor<256xf32>, %offset: tensor<256xf32>) + -> (tensor<3x4x256x6xf32>) { + // CHECK-DAG: %[[CST_AXIS:.+]] = "tf.Const"() <{value = dense<[0, 1, 3]> : tensor<3xi32>}> : () -> tensor<3xi32> + // CHECK-DAG: %[[X_SHAPE:.+]] = shape.shape_of %[[X]] : tensor<3x4x256x6xf32> -> tensor<4xindex> + // CHECK-DAG: %[[EPS:.+]] = mhlo.constant dense<1.000000e+00> : tensor<256xf32> + // CHECK-DAG: %[[MEAN:.+]] = "tf.Mean"(%arg0, %[[CST_AXIS]]) <{keep_dims = false}> : (tensor<3x4x256x6xf32>, tensor<3xi32>) -> tensor<256xf32> + // CHECK-DAG: %[[MEAN_BCAST:.+]] = "mhlo.dynamic_broadcast_in_dim"(%[[MEAN]], %[[X_SHAPE]]) <{broadcast_dimensions = dense<2> : tensor<1xi64>}> : (tensor<256xf32>, tensor<4xindex>) -> tensor<3x4x256x6xf32> + // CHECK-DAG: %[[SQ_DIFF:.+]] = "tf.SquaredDifference"(%arg0, %[[MEAN_BCAST]]) : (tensor<3x4x256x6xf32>, tensor<3x4x256x6xf32>) -> tensor<3x4x256x6xf32> + // CHECK-DAG: %[[VARIANCE:.+]] = "tf.Mean"(%[[SQ_DIFF]], %[[CST_AXIS]]) <{keep_dims = false}> : (tensor<3x4x256x6xf32>, tensor<3xi32>) -> tensor<256xf32> + // CHECK-DAG: %[[VARIANCE_EPS:.+]] = mhlo.add %[[VARIANCE]], %[[EPS]] : tensor<256xf32> + // CHECK-DAG: %[[VARIANCE_EPS_RSQRT:.+]] = mhlo.rsqrt %[[VARIANCE_EPS]] : tensor<256xf32> + // CHECK-DAG: %[[MULTIPLIER:.+]] = mhlo.multiply %[[VARIANCE_EPS_RSQRT]], %[[SCALE]] : tensor<256xf32> + // CHECK-DAG: %[[MUL_MEAN:.+]] = mhlo.multiply %[[MULTIPLIER]], %[[MEAN]] : tensor<256xf32> + // CHECK-DAG: %[[RHS:.+]] = mhlo.subtract %[[OFFSET]], %[[MUL_MEAN]] : tensor<256xf32> + // CHECK-DAG: %[[MULTIPLIER_BCAST:.+]] = "mhlo.broadcast_in_dim"(%[[MULTIPLIER]]) <{broadcast_dimensions = dense<2> : tensor<1xi64>}> : (tensor<256xf32>) -> tensor<3x4x256x6xf32> + // CHECK-DAG: %[[X_NORMED:.+]] = mhlo.multiply %[[X]], %[[MULTIPLIER_BCAST]] : tensor<3x4x256x6xf32> + // CHECK-DAG: %[[RHS_BCAST:.+]] = "mhlo.broadcast_in_dim"(%[[RHS]]) <{broadcast_dimensions = dense<2> : tensor<1xi64>}> : (tensor<256xf32>) -> tensor<3x4x256x6xf32> + // CHECK-DAG: %[[RESULT:.+]] = mhlo.add %[[X_NORMED]], %[[RHS_BCAST]] : tensor<3x4x256x6xf32> + %0:3 = "mhlo.batch_norm_training"(%x, %scale, %offset) + {epsilon = 1.0 : f32, feature_index = 2 : i64} : + (tensor<3x4x256x6xf32>, tensor<256xf32>, tensor<256xf32>) -> (tensor<3x4x256x6xf32>, tensor<256xf32>, tensor<256xf32>) + func.return %0 : tensor<3x4x256x6xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/uniform-quantized-stablehlo-to-tfl.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/uniform-quantized-stablehlo-to-tfl.mlir new file mode 100644 index 0000000000000000000000000000000000000000..4f70f0fe71fa1735de1d99dc18b290b918ef76f7 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/tests/uniform-quantized-stablehlo-to-tfl.mlir @@ -0,0 +1,1622 @@ +// RUN: odml-to-stablehlo-opt --uniform-quantized-stablehlo-to-tfl \ +// RUN: --split-input-file --verify-diagnostics %s | FileCheck %s + +// ============================================================================ +// The following functions tests example quantization patterns outputted from +// JAX Quantizer. JAX Quantizer should output integer types, which are +// composed into `UniformQuantized{|PerAxis}Type` via +// `compose_uniform_quantized_type_pass.cc`. +// ============================================================================ + +func.func @uniform_quantize_op(%arg: tensor<2x2xf32>) -> tensor<2x2x!quant.uniform> { + %0 = stablehlo.uniform_quantize %arg : (tensor<2x2xf32>) -> tensor<2x2x!quant.uniform> + return %0 : tensor<2x2x!quant.uniform> +} +// CHECK-LABEL: uniform_quantize_op +// CHECK: %[[QUANT:.+]] = "tfl.quantize"({{.*}}) <{qtype = tensor<2x2x!quant.uniform>}> : (tensor<2x2xf32>) -> tensor<2x2x!quant.uniform> +// CHECK: return %[[QUANT]] + +// ----- + +// Tests that the pattern doesn't match when the input tensor's type is a +// quantized type. + +func.func @uniform_quantize_op_quantized_input(%arg: tensor<2x2x!quant.uniform>) -> tensor<2x2x!quant.uniform> { + %0 = stablehlo.uniform_quantize %arg : (tensor<2x2x!quant.uniform>) -> tensor<2x2x!quant.uniform> + return %0 : tensor<2x2x!quant.uniform> +} +// CHECK-LABEL: uniform_quantize_op_quantized_input +// CHECK: stablehlo.uniform_quantize +// CHECK-NOT: tfl.quantize + +// ----- + +// Tests that the pattern doesn't match when the output tensor's storage type +// is ui16. ui16 storage type for quantized type is not compatible with +// `tfl.quantize`. + +func.func @uniform_quantize_op_uint16_output(%arg: tensor<2x2xf32>) -> tensor<2x2x!quant.uniform> { + %0 = stablehlo.uniform_quantize %arg : (tensor<2x2xf32>) -> tensor<2x2x!quant.uniform> + return %0 : tensor<2x2x!quant.uniform> +} +// CHECK-LABEL: uniform_quantize_op_uint16_output +// CHECK: stablehlo.uniform_quantize +// CHECK-NOT: tfl.quantize + +// ----- + +// Tests that the pattern doesn't match when the output tensor's storage type +// is i32. i32 storage type for quantized type is not compatible with +// `tfl.quantize`. + +func.func @uniform_quantize_op_i32_output(%arg: tensor<2x2xf32>) -> tensor<2x2x!quant.uniform> { + %0 = stablehlo.uniform_quantize %arg : (tensor<2x2xf32>) -> tensor<2x2x!quant.uniform> + return %0 : tensor<2x2x!quant.uniform> +} +// CHECK-LABEL: uniform_quantize_op_i32_output +// CHECK: stablehlo.uniform_quantize +// CHECK-NOT: tfl.quantize + +// ----- + +func.func @uniform_dequantize_op(%arg: tensor<2x2x!quant.uniform>) -> tensor<2x2xf32> { + %0 = stablehlo.uniform_dequantize %arg : (tensor<2x2x!quant.uniform>) -> tensor<2x2xf32> + return %0 : tensor<2x2xf32> +} +// CHECK-LABEL: uniform_dequantize_op +// CHECK: %[[DEQUANT:.+]] = "tfl.dequantize"({{.*}}) : (tensor<2x2x!quant.uniform>) -> tensor<2x2xf32> +// CHECK: return %[[DEQUANT]] + +// ----- + +// Tests that the pattern doesn't match when the input quantized tensor's +// storage type is ui16. ui16 storage type is not compatible with +// `tfl.dequantize`. + +func.func @uniform_dequantize_op_ui16_storage_input(%arg: tensor<2x2x!quant.uniform>) -> tensor<2x2xf32> { + %0 = stablehlo.uniform_dequantize %arg : (tensor<2x2x!quant.uniform>) -> tensor<2x2xf32> + return %0 : tensor<2x2xf32> +} +// CHECK-LABEL: uniform_dequantize_op_ui16_storage_input +// CHECK: stablehlo.uniform_dequantize +// CHECK-NOT: tfl.dequantize + +// ----- + +// Tests that the pattern doesn't match when the input quantized tensor's +// storage type is i32. i32 storage type is not compatible with +// `tfl.dequantize`. + +func.func @uniform_dequantize_op_i32_storage_input(%arg: tensor<2x2x!quant.uniform>) -> tensor<2x2xf32> { + %0 = stablehlo.uniform_dequantize %arg : (tensor<2x2x!quant.uniform>) -> tensor<2x2xf32> + return %0 : tensor<2x2xf32> +} +// CHECK-LABEL: uniform_dequantize_op_i32_storage_input +// CHECK: stablehlo.uniform_dequantize +// CHECK-NOT: tfl.dequantize + +// ----- + +// Tests that the pattern doesn't match when the input quantized tensor's +// storage type is i32. i32 storage type is not compatible with +// `tfl.dequantize`. + +func.func @uniform_dequantize_op_return_f64(%arg: tensor<2x2x!quant.uniform>) -> tensor<2x2xf64> { + %0 = stablehlo.uniform_dequantize %arg : (tensor<2x2x!quant.uniform>) -> tensor<2x2xf64> + return %0 : tensor<2x2xf64> +} +// CHECK-LABEL: uniform_dequantize_op_return_f64 +// CHECK: stablehlo.uniform_dequantize +// CHECK-NOT: tfl.dequantize + +// ----- + +func.func @convolution_upstream_same_padding_srq(%arg0: tensor<1x3x3x4x!quant.uniform>) -> tensor<1x3x3x2x!quant.uniform> { + %0 = stablehlo.constant() {value = dense<3> : tensor<3x3x4x2xi8>} : () -> tensor<3x3x4x2x!quant.uniform> + %1 = stablehlo.convolution(%arg0, %0) dim_numbers = [b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f], window = {pad = [[1, 1], [1, 1]]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64} : (tensor<1x3x3x4x!quant.uniform>, tensor<3x3x4x2x!quant.uniform>) -> tensor<1x3x3x2x!quant.uniform> + return %1 : tensor<1x3x3x2x!quant.uniform> +} +// Note that the quantized dimension is 0, and the shape has been transposed +// to (2, 3, 3, 4). +// CHECK-LABEL: convolution_upstream_same_padding_srq +// CHECK-SAME: %[[ARG:.+]]: tensor<1x3x3x4x!quant.uniform> +// CHECK-DAG: %[[QCONST_0:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<2x3x3x4x!quant.uniform:f32:0, {2.000000e+02,3.000000e+03}>>, value = dense<3> : tensor<2x3x3x4xi8>}> : () -> tensor<2x3x3x4x!quant.uniform:f32:0, {2.000000e+02,3.000000e+03}>> +// CHECK-DAG: %[[QCONST_1:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<2x!quant.uniform>, value = dense<0> : tensor<2xi32>}> : () -> tensor<2x!quant.uniform> +// CHECK: %[[CONV2D:.+]] = "tfl.conv_2d"(%[[ARG]], %[[QCONST_0]], %[[QCONST_1]]) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x3x3x4x!quant.uniform>, tensor<2x3x3x4x!quant.uniform:f32:0, {2.000000e+02,3.000000e+03}>>, tensor<2x!quant.uniform>) -> tensor<1x3x3x2x!quant.uniform> +// CHECK: return %[[CONV2D]] : tensor<1x3x3x2x!quant.uniform> + +// ----- + +func.func @convolution_upstream_srq_non_const_filter(%arg0: tensor<1x3x3x4x!quant.uniform>, %arg1: tensor<3x3x4x2x!quant.uniform>) -> tensor<1x3x3x2x!quant.uniform> { + %0 = stablehlo.convolution(%arg0, %arg1) dim_numbers = [b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f], window = {pad = [[1, 1], [1, 1]]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64} : (tensor<1x3x3x4x!quant.uniform>, tensor<3x3x4x2x!quant.uniform>) -> tensor<1x3x3x2x!quant.uniform> + return %0 : tensor<1x3x3x2x!quant.uniform> +} +// Confirm that the `stablehlo.convolution` is not converted to `tfl.conv_2d`. +// CHECK-LABEL: convolution_upstream_srq_non_const_filter +// CHECK-SAME: %[[ARG:.+]]: tensor<1x3x3x4x!quant.uniform> +// CHECK: stablehlo.convolution +// CHECK-NOT: tfl.conv_2d + +// ----- + +// Tests that if the window padding contains values of 0, tfl.pad op is not +// created and the `padding` attribute is set as "VALID". + +func.func @convolution_upstream_srq_valid_padding(%arg0: tensor<1x3x3x4x!quant.uniform>) -> tensor<1x1x1x2x!quant.uniform> { + %0 = stablehlo.constant() {value = dense<3> : tensor<3x3x4x2xi8>} : () -> tensor<3x3x4x2x!quant.uniform> + %1 = stablehlo.convolution(%arg0, %0) dim_numbers = [b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f], window = {pad = [[0, 0], [0, 0]]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64} : (tensor<1x3x3x4x!quant.uniform>, tensor<3x3x4x2x!quant.uniform>) -> tensor<1x1x1x2x!quant.uniform> + return %1 : tensor<1x1x1x2x!quant.uniform> +} +// CHECK-LABEL: convolution_upstream_srq_valid_padding +// CHECK-SAME: %[[ARG:.+]]: tensor<1x3x3x4x!quant.uniform> +// CHECK: %[[QCONST_0:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<2x3x3x4x!quant.uniform:f32:0, {2.000000e+02,3.000000e+03}>>, value = dense<3> : tensor<2x3x3x4xi8>}> : () -> tensor<2x3x3x4x!quant.uniform:f32:0, {2.000000e+02,3.000000e+03}>> +// CHECK: %[[QCONST_1:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<2x!quant.uniform>, value = dense<0> : tensor<2xi32>}> : () -> tensor<2x!quant.uniform> +// CHECK-NOT: tfl.pad +// CHECK: %[[CONV2D:.+]] = "tfl.conv_2d"(%[[ARG]], %[[QCONST_0]], %[[QCONST_1]]) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x3x3x4x!quant.uniform>, tensor<2x3x3x4x!quant.uniform:f32:0, {2.000000e+02,3.000000e+03}>>, tensor<2x!quant.uniform>) -> tensor<1x1x1x2x!quant.uniform> +// CHECK: return %[[CONV2D]] : tensor<1x1x1x2x!quant.uniform> + +// ----- + +// Tests that if the window padding value is missing, tfl.pad op is not +// created and the `padding` attribute is set as "VALID". + +func.func @convolution_upstream_srq_valid_padding(%arg0: tensor<1x3x3x4x!quant.uniform>) -> tensor<1x1x1x2x!quant.uniform> { + %0 = stablehlo.constant() {value = dense<3> : tensor<3x3x4x2xi8>} : () -> tensor<3x3x4x2x!quant.uniform> + // The `window` attribute is empty. + %1 = stablehlo.convolution(%arg0, %0) dim_numbers = [b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f], window = {} {batch_group_count = 1 : i64, feature_group_count = 1 : i64} : (tensor<1x3x3x4x!quant.uniform>, tensor<3x3x4x2x!quant.uniform>) -> tensor<1x1x1x2x!quant.uniform> + return %1 : tensor<1x1x1x2x!quant.uniform> +} +// CHECK-LABEL: convolution_upstream_srq_valid_padding +// CHECK-SAME: %[[ARG:.+]]: tensor<1x3x3x4x!quant.uniform> +// CHECK: %[[QCONST_0:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<2x3x3x4x!quant.uniform:f32:0, {2.000000e+02,3.000000e+03}>>, value = dense<3> : tensor<2x3x3x4xi8>}> : () -> tensor<2x3x3x4x!quant.uniform:f32:0, {2.000000e+02,3.000000e+03}>> +// CHECK: %[[QCONST_1:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<2x!quant.uniform>, value = dense<0> : tensor<2xi32>}> : () -> tensor<2x!quant.uniform> +// CHECK: %[[CONV2D:.+]] = "tfl.conv_2d"(%[[ARG]], %[[QCONST_0]], %[[QCONST_1]]) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x3x3x4x!quant.uniform>, tensor<2x3x3x4x!quant.uniform:f32:0, {2.000000e+02,3.000000e+03}>>, tensor<2x!quant.uniform>) -> tensor<1x1x1x2x!quant.uniform> +// CHECK: return %[[CONV2D]] : tensor<1x1x1x2x!quant.uniform> + +// ----- + +// Tests that if the window stride value is explicitly set, the attribute +// value is transferred to tfl.conv_2d's stridw_h and stride_w values. + +func.func @convolution_upstream_srq_strides(%arg0: tensor<1x3x3x4x!quant.uniform>) -> tensor<1x3x2x2x!quant.uniform> { + %0 = stablehlo.constant() {value = dense<3> : tensor<3x3x4x2xi8>} : () -> tensor<3x3x4x2x!quant.uniform> + // The stride value is explicitly set to [1, 2]. + %1 = stablehlo.convolution(%arg0, %0) dim_numbers = [b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f], window = {stride = [1, 2], pad = [[1, 1], [1, 1]]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64} : (tensor<1x3x3x4x!quant.uniform>, tensor<3x3x4x2x!quant.uniform>) -> tensor<1x3x2x2x!quant.uniform> + return %1 : tensor<1x3x2x2x!quant.uniform> +} +// CHECK-LABEL: convolution_upstream_srq_strides +// CHECK-SAME: %[[ARG:.+]]: tensor<1x3x3x4x!quant.uniform> +// CHECK-DAG: %[[QCONST_0:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<2x3x3x4x!quant.uniform:f32:0, {2.000000e+02,3.000000e+03}>>, value = dense<3> : tensor<2x3x3x4xi8>}> : () -> tensor<2x3x3x4x!quant.uniform:f32:0, {2.000000e+02,3.000000e+03}>> +// CHECK-DAG: %[[QCONST_1:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<2x!quant.uniform>, value = dense<0> : tensor<2xi32>}> : () -> tensor<2x!quant.uniform> +// Tests that the stride_w is set to 2. +// CHECK: %[[CONV2D:.+]] = "tfl.conv_2d"(%[[ARG]], %[[QCONST_0]], %[[QCONST_1]]) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 2 : i32}> : (tensor<1x3x3x4x!quant.uniform>, tensor<2x3x3x4x!quant.uniform:f32:0, {2.000000e+02,3.000000e+03}>>, tensor<2x!quant.uniform>) -> tensor<1x3x2x2x!quant.uniform> +// CHECK: return %[[CONV2D]] : tensor<1x3x2x2x!quant.uniform> + +// ----- + +// Tests static range quantized dot_general with asymmetric quantized input. + +func.func @dot_general_upstream_srq_asym_input(%arg0: tensor<1x2x3x4x!quant.uniform>) -> tensor<1x2x3x5x!quant.uniform> { + %0 = stablehlo.constant() {value = dense<1> : tensor<1x2x4x5xi8>} : () -> tensor<1x2x4x5x!quant.uniform> + %1 = "stablehlo.dot_general"(%arg0, %0) { + dot_dimension_numbers = #stablehlo.dot< + lhs_batching_dimensions = [0, 1], + rhs_batching_dimensions = [0, 1], + lhs_contracting_dimensions = [3], + rhs_contracting_dimensions = [2]>, + precision_config = [#stablehlo, #stablehlo] + } : (tensor<1x2x3x4x!quant.uniform>, tensor<1x2x4x5x!quant.uniform>) -> tensor<1x2x3x5x!quant.uniform> + return %1 : tensor<1x2x3x5x!quant.uniform> +} +// CHECK-LABEL: dot_general_upstream_srq_asym_input +// CHECK-SAME: %[[ARG:.+]]: tensor<1x2x3x4x!quant.uniform> +// CHECK: %[[QCONST_0:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<1x2x4x5x!quant.uniform>, value = dense<1> : tensor<1x2x4x5xi8>}> : () -> tensor<1x2x4x5x!quant.uniform> +// CHECK: %[[BMM:.+]] = "tfl.batch_matmul"(%[[ARG]], %[[QCONST_0]]) <{adj_x = false, adj_y = false}> : (tensor<1x2x3x4x!quant.uniform>, tensor<1x2x4x5x!quant.uniform>) -> tensor<1x2x3x5x!quant.uniform> + +// ----- + +// Tests static range quantized dot_general with symmetric quantized input. + +func.func @dot_general_upstream_srq_sym_input(%arg0: tensor<1x2x3x4x!quant.uniform>) -> tensor<1x2x3x5x!quant.uniform> { + %0 = stablehlo.constant() {value = dense<1> : tensor<1x2x4x5xi8>} : () -> tensor<1x2x4x5x!quant.uniform> + %1 = "stablehlo.dot_general"(%arg0, %0) { + dot_dimension_numbers = #stablehlo.dot< + lhs_batching_dimensions = [0, 1], + rhs_batching_dimensions = [0, 1], + lhs_contracting_dimensions = [3], + rhs_contracting_dimensions = [2] + >, + precision_config = [#stablehlo, #stablehlo] + } : (tensor<1x2x3x4x!quant.uniform>, tensor<1x2x4x5x!quant.uniform>) -> tensor<1x2x3x5x!quant.uniform> + return %1 : tensor<1x2x3x5x!quant.uniform> +} +// CHECK-LABEL: dot_general_upstream_srq_sym_input +// CHECK-SAME: %[[ARG:.+]]: tensor<1x2x3x4x!quant.uniform> +// CHECK: %[[QCONST_0:.+]] = "tfl.pseudo_qconst"() +// CHECK: "tfl.batch_matmul"(%[[ARG]], %[[QCONST_0]]) <{adj_x = false, adj_y = false}> + +// ----- + +// Tests static range quantized dot_general with activation as RHS + +func.func @dot_general_upstream_srq_activation_rhs(%arg0: tensor<1x2x3x4x!quant.uniform>, %arg1: tensor<1x2x4x5x!quant.uniform>) -> tensor<1x2x3x5x!quant.uniform> { + %0 = "stablehlo.dot_general"(%arg0, %arg1) { + dot_dimension_numbers = #stablehlo.dot< + lhs_batching_dimensions = [0, 1], + rhs_batching_dimensions = [0, 1], + lhs_contracting_dimensions = [3], + rhs_contracting_dimensions = [2] + >, + precision_config = [#stablehlo, #stablehlo] + } : (tensor<1x2x3x4x!quant.uniform>, tensor<1x2x4x5x!quant.uniform>) -> tensor<1x2x3x5x!quant.uniform> + return %0 : tensor<1x2x3x5x!quant.uniform> +} +// CHECK-LABEL: dot_general_upstream_srq_activation_rhs +// CHECK: "tfl.batch_matmul"(%arg0, %arg1) <{adj_x = false, adj_y = false}> : (tensor<1x2x3x4x!quant.uniform>, tensor<1x2x4x5x!quant.uniform>) -> tensor<1x2x3x5x!quant.uniform> + +// ----- + +// Tests static range quantized dot_general with adj_x + +// CHECK-LABEL: dot_general_upstream_srq_adj_x +func.func @dot_general_upstream_srq_adj_x(%arg0: tensor<1x2x4x3x!quant.uniform>) -> tensor<1x2x3x5x!quant.uniform> { + %0 = stablehlo.constant() {value = dense<1> : tensor<1x2x4x5xi8>} : () -> tensor<1x2x4x5x!quant.uniform> + %1 = "stablehlo.dot_general"(%arg0, %0) { + dot_dimension_numbers = #stablehlo.dot< + lhs_batching_dimensions = [0, 1], + rhs_batching_dimensions = [0, 1], + // implicit transpose of lhs + lhs_contracting_dimensions = [2], + rhs_contracting_dimensions = [2] + >, + precision_config = [#stablehlo, #stablehlo] + } : (tensor<1x2x4x3x!quant.uniform>, tensor<1x2x4x5x!quant.uniform>) -> tensor<1x2x3x5x!quant.uniform> + return %1 : tensor<1x2x3x5x!quant.uniform> +} +// CHECK-SAME: %[[ARG:.+]]: tensor<1x2x4x3x!quant.uniform> +// CHECK: %[[QCONST_0:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<1x2x4x5x!quant.uniform>, value = dense<1> : tensor<1x2x4x5xi8>}> : () -> tensor<1x2x4x5x!quant.uniform> +// CHECK: "tfl.batch_matmul"(%[[ARG]], %[[QCONST_0]]) <{adj_x = true, adj_y = false}> + +// ----- + +// Tests static range quantized dot_general with adj_y + +func.func @dot_general_upstream_srq_adj_y(%arg0: tensor<1x2x3x4x!quant.uniform>) -> tensor<1x2x3x5x!quant.uniform> { + %0 = stablehlo.constant() {value = dense<1> : tensor<1x2x5x4xi8>} : () -> tensor<1x2x5x4x!quant.uniform> + %1 = "stablehlo.dot_general"(%arg0, %0) { + dot_dimension_numbers = #stablehlo.dot< + lhs_batching_dimensions = [0, 1], + rhs_batching_dimensions = [0, 1], + lhs_contracting_dimensions = [3], + // implicit transpose of rhs + rhs_contracting_dimensions = [3] + >, + precision_config = [#stablehlo, #stablehlo] + } : (tensor<1x2x3x4x!quant.uniform>, tensor<1x2x5x4x!quant.uniform>) -> tensor<1x2x3x5x!quant.uniform> + return %1 : tensor<1x2x3x5x!quant.uniform> +} +// CHECK-LABEL: dot_general_upstream_srq_adj_y +// CHECK-SAME: %[[ARG:.+]]: tensor<1x2x3x4x!quant.uniform> +// CHECK: %[[QCONST_0:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<1x2x5x4x!quant.uniform>, value = dense<1> : tensor<1x2x5x4xi8>}> : () -> tensor<1x2x5x4x!quant.uniform> +// CHECK: "tfl.batch_matmul"(%[[ARG]], %[[QCONST_0]]) <{adj_x = false, adj_y = true}> + +// ----- + +// Tests static range quantized dot_general with wrong batch dims + +func.func @dot_general_upstream_srq_too_many_batches(%arg0: tensor<1x1x1x2x3x4x!quant.uniform>) -> tensor<1x1x1x2x3x5x!quant.uniform> { + %0 = stablehlo.constant() {value = dense<1> : tensor<1x1x1x2x4x5xi8>} : () -> tensor<1x1x1x2x4x5x!quant.uniform> + %1 = "stablehlo.dot_general"(%arg0, %0) { + dot_dimension_numbers = #stablehlo.dot< + lhs_batching_dimensions = [0, 1, 2, 3], + rhs_batching_dimensions = [0, 1, 2, 3], + lhs_contracting_dimensions = [5], + rhs_contracting_dimensions = [4] + >, + precision_config = [#stablehlo, #stablehlo] + } : (tensor<1x1x1x2x3x4x!quant.uniform>, tensor<1x1x1x2x4x5x!quant.uniform>) -> tensor<1x1x1x2x3x5x!quant.uniform> + return %1 : tensor<1x1x1x2x3x5x!quant.uniform> +} +// Only support size(batching_dimensions) <= 3 +// CHECK-LABEL: dot_general_upstream_srq_too_many_batches +// CHECK: stablehlo.dot_general +// CHECK-NOT: tfl.batch_matmul + +// ----- + +// Tests static range quantized dot_general with too many contracting dimension + +func.func @dot_general_upstream_srq_too_many_contractions(%arg0: tensor<1x2x3x4x4x!quant.uniform>) -> tensor<1x2x3x5x!quant.uniform> { + %0 = stablehlo.constant() {value = dense<1> : tensor<1x2x4x4x5xi8>} : () -> tensor<1x2x4x4x5x!quant.uniform> + %1 = "stablehlo.dot_general"(%arg0, %0) { + dot_dimension_numbers = #stablehlo.dot< + lhs_batching_dimensions = [0, 1], + rhs_batching_dimensions = [0, 1], + lhs_contracting_dimensions = [3, 4], + rhs_contracting_dimensions = [2, 3] + >, + precision_config = [#stablehlo, #stablehlo] + } : (tensor<1x2x3x4x4x!quant.uniform>, tensor<1x2x4x4x5x!quant.uniform>) -> tensor<1x2x3x5x!quant.uniform> + return %1 : tensor<1x2x3x5x!quant.uniform> +} +// Only support size(contracting_dimensions) == 1 +// CHECK-LABEL: dot_general_upstream_srq_too_many_contractions +// CHECK: stablehlo.dot_general +// CHECK-NOT: tfl.batch_matmul + +// ----- + +// Tests static range quantized dot_general with unsupported contracting dim + +func.func @dot_general_upstream_srq_wrong_contracting(%arg0: tensor<1x2x3x4x!quant.uniform>) -> tensor<1x4x3x5x!quant.uniform> { + %0 = stablehlo.constant() {value = dense<1> : tensor<1x2x4x5xi8>} : () -> tensor<1x2x4x5x!quant.uniform> + %1 = "stablehlo.dot_general"(%arg0, %0) { + dot_dimension_numbers = #stablehlo.dot< + lhs_batching_dimensions = [0, 3], + rhs_batching_dimensions = [0, 2], + lhs_contracting_dimensions = [1], + rhs_contracting_dimensions = [1] + >, + precision_config = [#stablehlo, #stablehlo] + } : (tensor<1x2x3x4x!quant.uniform>, tensor<1x2x4x5x!quant.uniform>) -> tensor<1x4x3x5x!quant.uniform> + return %1 : tensor<1x4x3x5x!quant.uniform> +} +// Contracting dimension must be the last two dimension +// CHECK-LABEL: dot_general_upstream_srq_wrong_contracting +// CHECK: stablehlo.dot_general +// CHECK-NOT: tfl.batch_matmul + +// ----- + +// Tests static range quantized dot_general with float operands + +// CHECK-LABEL: dot_general_upstream_srq_float_operands +func.func @dot_general_upstream_srq_float_operands(%arg0: tensor<1x2x3x4xf32>, %arg1: tensor<1x2x4x5xf32>) -> tensor<1x2x3x5xf32> { + %0 = "stablehlo.dot_general"(%arg0, %arg1) { + dot_dimension_numbers = #stablehlo.dot< + lhs_batching_dimensions = [0, 1], + rhs_batching_dimensions = [0, 1], + lhs_contracting_dimensions = [3], + rhs_contracting_dimensions = [2] + >, + precision_config = [#stablehlo, #stablehlo] + } : (tensor<1x2x3x4xf32>, tensor<1x2x4x5xf32>) -> tensor<1x2x3x5xf32> + return %0 : tensor<1x2x3x5xf32> +} +// Do nothing for float operands +// CHECK: stablehlo.dot_general +// CHECK-NOT: tfl.batch_matmul + +// ----- + +// Tests static range quantized dot_general with asymmetric weight (rhs). + +// CHECK-LABEL: dot_general_upstream_srq_asym_weight +func.func @dot_general_upstream_srq_asym_weight(%arg0: tensor<1x2x3x4x!quant.uniform>) -> tensor<1x2x3x5x!quant.uniform> { + %0 = stablehlo.constant() {value = dense<1> : tensor<1x2x4x5xi8>} : () -> tensor<1x2x4x5x!quant.uniform> + %1 = "stablehlo.dot_general"(%arg0, %0) {dot_dimension_numbers = #stablehlo.dot, precision_config = [#stablehlo, #stablehlo]} : (tensor<1x2x3x4x!quant.uniform>, tensor<1x2x4x5x!quant.uniform>) -> tensor<1x2x3x5x!quant.uniform> + return %1 : tensor<1x2x3x5x!quant.uniform> +} +// CHECK-SAME: %[[ARG:.+]]: tensor<1x2x3x4x!quant.uniform> +// CHECK: %[[QCONST_0:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<1x2x4x5x!quant.uniform>, value = dense<1> : tensor<1x2x4x5xi8>}> : () -> tensor<1x2x4x5x!quant.uniform> +// CHECK: %[[BMM:.+]] = "tfl.batch_matmul"(%[[ARG]], %[[QCONST_0]]) <{adj_x = false, adj_y = false}> : (tensor<1x2x3x4x!quant.uniform>, tensor<1x2x4x5x!quant.uniform>) -> tensor<1x2x3x5x!quant.uniform> + +// ----- + +// Tests that when the weight tensor for `stablehlo.dot_general` is per-axis +// quantized, it is converted to `tfl.fully_connected` op. + +// CHECK-LABEL: dot_general_upstream_srq_per_axis_quantized_filter +func.func @dot_general_upstream_srq_per_axis_quantized_filter(%arg0: tensor<1x3x!quant.uniform>) -> tensor<1x2x!quant.uniform> { + %0 = stablehlo.constant() {value = dense<1> : tensor<3x2xi8>} : () -> tensor<3x2x!quant.uniform> + %1 = stablehlo.dot_general %arg0, %0, contracting_dims = [1] x [0] : (tensor<1x3x!quant.uniform>, tensor<3x2x!quant.uniform>) -> tensor<1x2x!quant.uniform> + return %1 : tensor<1x2x!quant.uniform> +} +// CHECK-SAME: %[[ARG_0:.+]]: tensor<1x3x!quant.uniform> +// Weight tensor is transposed, as tfl.fully_connected accepts a [o, i] matrix. +// CHECK-DAG: %[[QCONST_0:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<2x3x!quant.uniform:f32:0, {2.000000e+02,3.000000e+03}>>, value = dense<1> : tensor<2x3xi8>}> : () -> tensor<2x3x!quant.uniform:f32:0, {2.000000e+02,3.000000e+03}>> +// CHECK-DAG: %[[QCONST_1:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<2x!quant.uniform>, value = dense<0> : tensor<2xi32>}> : () -> tensor<2x!quant.uniform> +// Bias tensor's scale is input scale * filter scale. +// CHECK: %[[FC:.+]] = "tfl.fully_connected"(%[[ARG_0]], %[[QCONST_0]], %[[QCONST_1]]) <{fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"}> : (tensor<1x3x!quant.uniform>, tensor<2x3x!quant.uniform:f32:0, {2.000000e+02,3.000000e+03}>>, tensor<2x!quant.uniform>) -> tensor<1x2x!quant.uniform> +// CHECK-NEXT: return %[[FC]] : tensor<1x2x!quant.uniform> + +// ----- + +// Tests that when the weight tensor for `stablehlo.dot_general` is per-axis +// quantized but has a batch dimension, it is not converted. + +// CHECK-LABEL: dot_general_upstream_srq_per_axis_quantized_filter_with_batch_dim +func.func @dot_general_upstream_srq_per_axis_quantized_filter_with_batch_dim(%arg0: tensor<1x1x3x!quant.uniform>) -> tensor<1x1x2x!quant.uniform> { + %0 = stablehlo.constant() {value = dense<1> : tensor<1x3x2xi8>} : () -> tensor<1x3x2x!quant.uniform> + %1 = stablehlo.dot_general %arg0, %0, batching_dims = [0] x [0], contracting_dims = [2] x [1] : (tensor<1x1x3x!quant.uniform>, tensor<1x3x2x!quant.uniform>) -> tensor<1x1x2x!quant.uniform> + return %1 : tensor<1x1x2x!quant.uniform> +} +// Nothing changes. +// CHECK: stablehlo.dot_general +// CHECK-NOT: tfl.fully_connected +// CHECK-NOT: tfl.batch_matmul + +// ----- + +// Tests that when the weight tensor for `stablehlo.dot_general` is per-axis +// quantized but has a batch dim > 1, it is not converted. + +// CHECK-LABEL: dot_general_upstream_srq_per_axis_quantized_filter_multibatch +func.func @dot_general_upstream_srq_per_axis_quantized_filter_multibatch(%arg0: tensor<3x1x3x!quant.uniform>) -> tensor<3x1x2x!quant.uniform> { + %0 = stablehlo.constant() {value = dense<1> : tensor<3x3x2xi8>} : () -> tensor<3x3x2x!quant.uniform> + %1 = stablehlo.dot_general %arg0, %0, batching_dims = [0] x [0], contracting_dims = [2] x [1] : (tensor<3x1x3x!quant.uniform>, tensor<3x3x2x!quant.uniform>) -> tensor<3x1x2x!quant.uniform> + return %1 : tensor<3x1x2x!quant.uniform> +} +// Nothing changes. +// CHECK: stablehlo.dot_general +// CHECK-NOT: tfl.fully_connected +// CHECK-NOT: tfl.batch_matmul + +// ----- + +// Tests that when the weight tensor for `stablehlo.dot_general` is per-axis +// quantized but has more than one contracting dimension, it is not converted. + +// CHECK-LABEL: dot_general_upstream_srq_per_axis_quantized_filter_with_multiple_contracting_dims +func.func @dot_general_upstream_srq_per_axis_quantized_filter_with_multiple_contracting_dims(%arg0: tensor<1x2x3x!quant.uniform>) -> tensor<1x1x!quant.uniform> { + %0 = stablehlo.constant() {value = dense<1> : tensor<1x3x2xi8>} : () -> tensor<1x3x2x!quant.uniform> + %1 = stablehlo.dot_general %arg0, %0, contracting_dims = [1, 2] x [2, 1] : (tensor<1x2x3x!quant.uniform>, tensor<1x3x2x!quant.uniform>) -> tensor<1x1x!quant.uniform> + return %1 : tensor<1x1x!quant.uniform> +} +// Nothing changes. +// CHECK: stablehlo.dot_general +// CHECK-NOT: tfl.fully_connected +// CHECK-NOT: tfl.batch_matmul + +// ----- + +// ============================================================================ +// The following functions tests example quantization patterns outputted from +// StableHLO Quantizer. These patterns should be legalized early directly +// to fused tflite ops. +// ============================================================================ + +// Tests that a simple per-channel quantized `stablehlo.dot_general` is properly +// lowered to fused `tfl.fully_connected`. +// This case covers for the following quantization patterns because +// activation clipping ranges take affect in scale and zp of the final +// `stablehlo.uniform_quantize`. See more details in b/319168201. +// * dot_general_fn +// * dot_general_with_relu_fn +// * dot_general_with_relu6_fn + +func.func @dot_general_srq(%arg0: tensor<1x1024x!quant.uniform>) -> (tensor<1x3x!quant.uniform>) { + %0 = stablehlo.constant() {value = dense<1> : tensor<1024x3xi8>} : () -> tensor<1024x3x!quant.uniform:f32:1, {2.000000e+0, 2.000000e+0, 2.000000e+0}>> + %1 = stablehlo.dot_general %arg0, %0, contracting_dims = [1] x [0] : (tensor<1x1024x!quant.uniform>, tensor<1024x3x!quant.uniform:f32:1, {2.000000e+0, 2.000000e+0, 2.000000e+0}>>) -> tensor<1x3x!quant.uniform> + %2 = stablehlo.uniform_quantize %1 : (tensor<1x3x!quant.uniform>) -> tensor<1x3x!quant.uniform> + return %2 : tensor<1x3x!quant.uniform> +} +// CHECK-LABEL: dot_general_srq +// CHECK-SAME: (%[[ARG_1:.+]]: tensor<1x1024x!quant.uniform) -> tensor<1x3x!quant.uniform> +// CHECK-NOT: stablehlo.dot_general +// CHECK: %[[QCONST_0:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<3x1024x!quant.uniform:f32:0, {2.000000e+00,2.000000e+00,2.000000e+00}>>, value = dense<1> : tensor<3x1024xi8>}> : () -> tensor<3x1024x!quant.uniform:f32:0, {2.000000e+00,2.000000e+00,2.000000e+00}>> +// CHECK: %[[QCONST_1:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<3x!quant.uniform>, value = dense<0> : tensor<3xi32>}> : () -> tensor<3x!quant.uniform> +// CHECK: %[[FULLY_CONNECTED:.+]] = "tfl.fully_connected"(%[[ARG_1]], %[[QCONST_0]], %[[QCONST_1]]) <{fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"}> : (tensor<1x1024x!quant.uniform>, tensor<3x1024x!quant.uniform:f32:0, {2.000000e+00,2.000000e+00,2.000000e+00}>>, tensor<3x!quant.uniform>) -> tensor<1x3x!quant.uniform> +// CHECK-NOT: tfl.batch_matmul +// CHECK: return %[[FULLY_CONNECTED]] + +// ----- + +// Tests that a fused per-channel quantized `stablehlo.dot_general` is properly +// lowered to fused `tfl.fully_connected`. +// TODO: b/309896242 - Add more support for dynamic bias fusion cases. + +func.func @dot_general_with_bias_same_shape_srq(%arg0: tensor<1x1024x!quant.uniform>) -> (tensor<1x3x!quant.uniform>) { + %0 = stablehlo.constant() {value = dense<1> : tensor<1024x3xi8>} : () -> tensor<1024x3x!quant.uniform:f32:1, {2.000000e+0, 2.000000e+0, 2.000000e+0}>> + %1 = stablehlo.constant() {value = dense<2> : tensor<1x3xi32>} : () -> tensor<1x3x!quant.uniform> + %2 = stablehlo.dot_general %arg0, %0, contracting_dims = [1] x [0] : (tensor<1x1024x!quant.uniform>, tensor<1024x3x!quant.uniform:f32:1, {2.000000e+0, 2.000000e+0, 2.000000e+0}>>) -> tensor<1x3x!quant.uniform> + %3 = stablehlo.add %2, %1 : tensor<1x3x!quant.uniform> + %4 = stablehlo.uniform_quantize %3 : (tensor<1x3x!quant.uniform>) -> tensor<1x3x!quant.uniform> + return %4 : tensor<1x3x!quant.uniform> +} +// CHECK-LABEL: dot_general_with_bias_same_shape +// CHECK-SAME: (%[[ARG_0:.+]]: tensor<1x1024x!quant.uniform>) -> tensor<1x3x!quant.uniform> +// CHECK-DAG: %[[QCONST_0:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<3x1024x!quant.uniform:f32:0, {2.000000e+00,2.000000e+00,2.000000e+00}>>, value = dense<1> : tensor<3x1024xi8>}> : () -> tensor<3x1024x!quant.uniform:f32:0, {2.000000e+00,2.000000e+00,2.000000e+00}>> +// CHECK-DAG: %[[QCONST_1:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<3x!quant.uniform>, value = dense<2> : tensor<1x3xi32>}> : () -> tensor<3x!quant.uniform> +// CHECK: %[[FULLY_CONNECTED:.+]] = "tfl.fully_connected"(%[[ARG_0]], %[[QCONST_0]], %[[QCONST_1]]) <{fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"}> : (tensor<1x1024x!quant.uniform>, tensor<3x1024x!quant.uniform:f32:0, {2.000000e+00,2.000000e+00,2.000000e+00}>>, tensor<3x!quant.uniform>) -> tensor<1x3x!quant.uniform> +// CHECK: return %[[FULLY_CONNECTED]] + +// ----- + +// Tests that when the weight tensor for `stablehlo.dot_general` has a +// `stablehlo.constant` -> `stablehlo.transpose` pattern, the +// `stablehlo.constant` is directly transformed to `tfl.pseudo_qconst`, which +// becomes the rhs of `tfl.fully_connected`. This is because +// `tfl.fully_connected` accepts a [o, i] format for rhs, which +// `stablehlo.constant` op already has before the transpose. + +// CHECK-LABEL: dot_general_srq_constant_transpose_rhs +func.func @dot_general_srq_constant_transpose_rhs(%arg0: tensor<1x3x!quant.uniform>) -> tensor<1x2x!quant.uniform> { + %0 = stablehlo.constant() {value = dense<1> : tensor<2x3xi8>} : () -> tensor<2x3x!quant.uniform> + %1 = stablehlo.transpose %0, dims = [1, 0] : (tensor<2x3x!quant.uniform>) -> tensor<3x2x!quant.uniform> + %2 = stablehlo.dot_general %arg0, %1, contracting_dims = [1] x [0] : (tensor<1x3x!quant.uniform>, tensor<3x2x!quant.uniform>) -> tensor<1x2x!quant.uniform> + %3 = stablehlo.uniform_quantize %2 : (tensor<1x2x!quant.uniform>) -> tensor<1x2x!quant.uniform> + return %3 : tensor<1x2x!quant.uniform> +} +// CHECK-SAME: %[[ARG:.+]]: tensor<1x3x!quant.uniform> + +// Checks that the `tfl.pseudo_qconst` corresponding to the `stablehlo.constant` +// has the same shape. +// CHECK: %[[QCONST_0:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<2x3x!quant.uniform>, value = dense<1> : tensor<2x3xi8>}> : () -> tensor<2x3x!quant.uniform> +// CHECK: %[[QCONST_1:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<2x!quant.uniform>, value = dense<0> : tensor<2xi32>}> : () -> tensor<2x!quant.uniform> +// CHECK: %[[FULLY_CONNECTED:.+]] = "tfl.fully_connected"(%[[ARG]], %[[QCONST_0]], %[[QCONST_1]]) <{fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"}> : (tensor<1x3x!quant.uniform>, tensor<2x3x!quant.uniform>, tensor<2x!quant.uniform>) -> tensor<1x2x!quant.uniform> + +// Also checks that the i32 -> i8 uniform quantize is absorbed into +// `tfl.fully_connected`. +// CHECK: return %[[FULLY_CONNECTED]] : tensor<1x2x!quant.uniform> + +// ----- + +// Tests that when the weight tensor for `stablehlo.dot_general` is coming from +// `stablehlo.transpose` and its operand is not a `stablehlo.constant` +// (e.g. argument), the conversion to `tfl.fully_connected` doesn't happen. + +// CHECK-LABEL: dot_general_srq_arg_transpose_rhs +func.func @dot_general_srq_arg_transpose_rhs(%arg0: tensor<1x3x!quant.uniform>, %arg1: tensor<2x3x!quant.uniform>) -> tensor<1x2x!quant.uniform> { + %1 = stablehlo.transpose %arg1, dims = [1, 0] : (tensor<2x3x!quant.uniform>) -> tensor<3x2x!quant.uniform> + %2 = stablehlo.dot_general %arg0, %1, contracting_dims = [1] x [0] : (tensor<1x3x!quant.uniform>, tensor<3x2x!quant.uniform>) -> tensor<1x2x!quant.uniform> + %3 = stablehlo.uniform_quantize %2 : (tensor<1x2x!quant.uniform>) -> tensor<1x2x!quant.uniform> + return %3 : tensor<1x2x!quant.uniform> +} +// Checks that the `stablehlo.dot_general` is not converted to +// `tfl.fully_connected`. Also notice that the `stablehlo.transpose` and +// `stablehlo.uniform_quantize` are converted separately. + +// CHECK: tfl.transpose +// CHECK: stablehlo.dot_general +// CHECK-NOT: tfl.fully_connected +// CHECK: tfl.quantize + +// ----- + +// Tests static range quantized dot_general with qi32 -> qi8 requantization is +// properly lowered to `tfl.batch_matmul`. + +func.func @dot_general_srq_to_batch_matmul(%arg0: tensor<1x2x3x4x!quant.uniform>, %arg1: tensor<1x2x4x5x!quant.uniform>) -> tensor<1x2x3x5x!quant.uniform> { + %0 = "stablehlo.dot_general"(%arg0, %arg1) { + dot_dimension_numbers = #stablehlo.dot< + lhs_batching_dimensions = [0, 1], + rhs_batching_dimensions = [0, 1], + lhs_contracting_dimensions = [3], + rhs_contracting_dimensions = [2] + >, + precision_config = [#stablehlo, #stablehlo] + } : (tensor<1x2x3x4x!quant.uniform>, tensor<1x2x4x5x!quant.uniform>) -> tensor<1x2x3x5x!quant.uniform> + %1 = stablehlo.uniform_quantize %0 : (tensor<1x2x3x5x!quant.uniform>) -> tensor<1x2x3x5x!quant.uniform> + return %1 : tensor<1x2x3x5x!quant.uniform> +} + +// CHECK-LABEL: dot_general_srq_to_batch_matmul +// CHECK-SAME: (%[[ARG_0:.+]]: tensor<1x2x3x4x!quant.uniform>, %[[ARG_1:.+]]: tensor<1x2x4x5x!quant.uniform>) -> tensor<1x2x3x5x!quant.uniform> +// CHECK: %[[BMM:.+]] = "tfl.batch_matmul"(%[[ARG_0]], %[[ARG_1]]) <{adj_x = false, adj_y = false}> : (tensor<1x2x3x4x!quant.uniform>, tensor<1x2x4x5x!quant.uniform>) -> tensor<1x2x3x5x!quant.uniform> +// CHECK-NOT: stablehlo.dot_general +// CHECK-NOT: stablehlo.uniform_quantize +// CHECK-NOT: tfl.fully_connected +// CHECK-NOT: tfl.quantize +// CHECK: return %[[BMM]] + +// ----- + +// Tests static range quantized dot_general with qi32 -> qi8 requantization is +// not converted to `tfl.batch_matmul` when there are multiple use of the +// intermediate result. + +func.func @dot_general_srq_multiple_use_of_intermediate_result(%arg0: tensor<1x2x3x4x!quant.uniform>, %arg1: tensor<1x2x4x5x!quant.uniform>) -> tensor<1x2x3x5x!quant.uniform> { + %0 = "stablehlo.dot_general"(%arg0, %arg1) { + dot_dimension_numbers = #stablehlo.dot< + lhs_batching_dimensions = [0, 1], + rhs_batching_dimensions = [0, 1], + lhs_contracting_dimensions = [3], + rhs_contracting_dimensions = [2] + >, + precision_config = [#stablehlo, #stablehlo] + } : (tensor<1x2x3x4x!quant.uniform>, tensor<1x2x4x5x!quant.uniform>) -> tensor<1x2x3x5x!quant.uniform> + %1 = stablehlo.uniform_quantize %0 : (tensor<1x2x3x5x!quant.uniform>) -> tensor<1x2x3x5x!quant.uniform> + %2 = stablehlo.uniform_quantize %0 : (tensor<1x2x3x5x!quant.uniform>) -> tensor<1x2x3x5x!quant.uniform> + %3 = stablehlo.add %1, %2 : tensor<1x2x3x5x!quant.uniform> + return %3 : tensor<1x2x3x5x!quant.uniform> +} + +// CHECK-LABEL: dot_general_srq_multiple_use_of_intermediate_result +// CHECK-NOT: tfl.fully_connected +// CHECK-NOT: tfl.batch_matmul +// CHECK: stablehlo.dot_general + +// ----- + +// Tests that a simple per-channel quantized `stablehlo.convolution` is properly +// lowered to fused `tfl.conv_2d`. +// This case covers for the following quantization patterns because +// activation clipping ranges take affect in scale and zp of the final +// `stablehlo.uniform_quantize`. See more details in b/319168201. +// * conv_fn +// * conv_with_relu_fn +// * conv_with_relu6_fn + +func.func @conv_srq(%arg0: tensor<1x5x5x2x!quant.uniform>) -> (tensor<1x4x4x4x!quant.uniform>) { + %0 = stablehlo.constant() {value = dense<3> : tensor<4x4x2x4xi8>} : () -> tensor<4x4x2x4x!quant.uniform> + %1 = stablehlo.convolution(%arg0, %0) dim_numbers = [b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f], window = {pad = [[1, 1], [1, 1]]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64} : (tensor<1x5x5x2x!quant.uniform>, tensor<4x4x2x4x!quant.uniform>) -> tensor<1x4x4x4x!quant.uniform> + %2 = stablehlo.uniform_quantize %1 : (tensor<1x4x4x4x!quant.uniform>) -> tensor<1x4x4x4x!quant.uniform> + return %2 : tensor<1x4x4x4x!quant.uniform> +} +// CHECK-LABEL: func.func @conv_srq +// CHECK-SAME: (%[[ARG_0:.+]]: tensor<1x5x5x2x!quant.uniform>) -> tensor<1x4x4x4x!quant.uniform> +// CHECK-DAG: %[[CONST_0:.+]] = "tfl.pseudo_const"() <{value = dense<{{\[\[0, 0\], \[1, 1\], \[1, 1\], \[0, 0\]\]}}> : tensor<4x2xi32>}> : () -> tensor<4x2xi32> +// CHECK-DAG: %[[QCONST_0:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<4x4x4x2x!quant.uniform:f32:0, {3.000000e+00,3.000000e+00,3.000000e+00,3.000000e+00}>>, value = dense<3> : tensor<4x4x4x2xi8>}> : () -> tensor<4x4x4x2x!quant.uniform:f32:0, {3.000000e+00,3.000000e+00,3.000000e+00,3.000000e+00}>> +// CHECK-DAG: %[[QCONST_1:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<4x!quant.uniform>, value = dense<0> : tensor<4xi32>}> : () -> tensor<4x!quant.uniform> +// CHECK: %[[PAD:.+]] = "tfl.pad"(%[[ARG_0]], %[[CONST_0]]) : (tensor<1x5x5x2x!quant.uniform>, tensor<4x2xi32>) -> tensor<1x7x7x2x!quant.uniform> +// CHECK: %[[CONV_2D:.+]] = "tfl.conv_2d"(%[[PAD]], %[[QCONST_0]], %[[QCONST_1]]) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x7x7x2x!quant.uniform>, tensor<4x4x4x2x!quant.uniform:f32:0, {3.000000e+00,3.000000e+00,3.000000e+00,3.000000e+00}>>, tensor<4x!quant.uniform>) -> tensor<1x4x4x4x!quant.uniform> +// CHECK: return %[[CONV_2D]] + +func.func @conv_same_padding_srq(%arg0: tensor<1x32x32x3x!quant.uniform>) -> (tensor<1x32x32x2x!quant.uniform>) { + %0 = stablehlo.constant() {value = dense<3> : tensor<3x3x3x2xi8>} : () -> tensor<3x3x3x2x!quant.uniform> + %1 = stablehlo.convolution(%arg0, %0) dim_numbers = [b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f], window = {pad = [[1, 1], [1, 1]]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64} : (tensor<1x32x32x3x!quant.uniform>, tensor<3x3x3x2x!quant.uniform>) -> tensor<1x32x32x2x!quant.uniform> + %2 = stablehlo.uniform_quantize %1 : (tensor<1x32x32x2x!quant.uniform>) -> tensor<1x32x32x2x!quant.uniform> + return %2 : tensor<1x32x32x2x!quant.uniform> +} +// CHECK-LABEL: func.func @conv_same_padding_srq +// CHECK-SAME: (%[[ARG_0:.+]]: tensor<1x32x32x3x!quant.uniform>) -> tensor<1x32x32x2x!quant.uniform> +// CHECK-DAG: %[[QCONST_0:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<2x3x3x3x!quant.uniform:f32:0, {3.000000e+00,3.000000e+00}>>, value = dense<3> : tensor<2x3x3x3xi8>}> : () -> tensor<2x3x3x3x!quant.uniform:f32:0, {3.000000e+00,3.000000e+00}>> +// CHECK-DAG: %[[QCONST_1:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<2x!quant.uniform>, value = dense<0> : tensor<2xi32>}> : () -> tensor<2x!quant.uniform> +// CHECK: %[[CONV_2D:.+]] = "tfl.conv_2d"(%[[ARG_0]], %[[QCONST_0]], %[[QCONST_1]]) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x32x32x3x!quant.uniform>, tensor<2x3x3x3x!quant.uniform:f32:0, {3.000000e+00,3.000000e+00}>>, tensor<2x!quant.uniform>) -> tensor<1x32x32x2x!quant.uniform> +// CHECK: return %[[CONV_2D]] : tensor<1x32x32x2x!quant.uniform> + +// ----- + +func.func @conv_same_padding_srq_non_unit_strides(%arg0: tensor<1x32x32x3x!quant.uniform>) -> (tensor<1x16x16x2x!quant.uniform>) { + %0 = stablehlo.constant() {value = dense<3> : tensor<3x3x3x2xi8>} : () -> tensor<3x3x3x2x!quant.uniform> + %1 = stablehlo.convolution(%arg0, %0) dim_numbers = [b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f], window = {pad = [[1, 1], [1, 1]]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64, window_strides = array} : (tensor<1x32x32x3x!quant.uniform>, tensor<3x3x3x2x!quant.uniform>) -> tensor<1x16x16x2x!quant.uniform> + %2 = stablehlo.uniform_quantize %1 : (tensor<1x16x16x2x!quant.uniform>) -> tensor<1x16x16x2x!quant.uniform> + return %2 : tensor<1x16x16x2x!quant.uniform> +} +// CHECK-LABEL: func.func @conv_same_padding_srq_non_unit_strides +// CHECK-SAME: (%[[ARG_0:.+]]: tensor<1x32x32x3x!quant.uniform>) -> tensor<1x16x16x2x!quant.uniform> +// CHECK-DAG: %[[QCONST_0:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<2x3x3x3x!quant.uniform:f32:0, {3.000000e+00,3.000000e+00}>>, value = dense<3> : tensor<2x3x3x3xi8>}> : () -> tensor<2x3x3x3x!quant.uniform:f32:0, {3.000000e+00,3.000000e+00}>> +// CHECK-DAG: %[[QCONST_1:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<2x!quant.uniform>, value = dense<0> : tensor<2xi32>}> : () -> tensor<2x!quant.uniform> +// CHECK: %[[CONV_2D:.+]] = "tfl.conv_2d"(%[[ARG_0]], %[[QCONST_0]], %[[QCONST_1]]) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 2 : i32, stride_w = 2 : i32}> : (tensor<1x32x32x3x!quant.uniform>, tensor<2x3x3x3x!quant.uniform:f32:0, {3.000000e+00,3.000000e+00}>>, tensor<2x!quant.uniform>) -> tensor<1x16x16x2x!quant.uniform> +// CHECK: return %[[CONV_2D]] : tensor<1x16x16x2x!quant.uniform> + +// ----- + +func.func @conv_srq_transpose_conv(%arg0: tensor<1x5x5x2x!quant.uniform>) -> (tensor<1x14x14x4x!quant.uniform>) { + %0 = stablehlo.constant() {value = dense<3> : tensor<2x2x2x4xi8>} : () -> tensor<2x2x2x4x!quant.uniform> + %1 = stablehlo.convolution(%arg0, %0) dim_numbers = [b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f], window = {pad = [[1, 1], [1, 1]]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64, window_strides = array, lhs_dilation = array} : (tensor<1x5x5x2x!quant.uniform>, tensor<2x2x2x4x!quant.uniform>) -> tensor<1x14x14x4x!quant.uniform> + %2 = stablehlo.uniform_quantize %1 : (tensor<1x14x14x4x!quant.uniform>) -> tensor<1x14x14x4x!quant.uniform> + return %2 : tensor<1x14x14x4x!quant.uniform> +} +// CHECK-LABEL: func.func @conv_srq_transpose_conv +// CHECK-SAME: (%[[ARG_0:.+]]: tensor<1x5x5x2x!quant.uniform>) -> tensor<1x14x14x4x!quant.uniform> +// CHECK-DAG: %[[CONST_0:.+]] = arith.constant dense<[1, 14, 14, 4]> : tensor<4xi32> +// CHECK-DAG: %[[CONST_1:.*]] = "tfl.pseudo_const"() <{value = dense<{{\[\[0, 0\], \[1, 1\], \[1, 1\], \[0, 0\]\]}}> : tensor<4x2xi32>}> : () -> tensor<4x2xi32> +// CHECK-DAG: %[[QCONST_0:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<4x2x2x2x!quant.uniform:f32:0, {3.000000e+00,3.000000e+00,3.000000e+00,3.000000e+00}>>, value = dense<3> : tensor<4x2x2x2xi8>}> : () -> tensor<4x2x2x2x!quant.uniform:f32:0, {3.000000e+00,3.000000e+00,3.000000e+00,3.000000e+00}>> +// CHECK-DAG: %[[QCONST_1:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<4x!quant.uniform>, value = dense<0> : tensor<4xi32>}> : () -> tensor<4x!quant.uniform> +// CHECK: %[[PAD:.+]] = "tfl.pad"(%[[ARG_0]], %[[CONST_1]]) : (tensor<1x5x5x2x!quant.uniform>, tensor<4x2xi32>) -> tensor<1x7x7x2x!quant.uniform> +// CHECK: %[[TRANSPOSE_CONV_2D:.+]] = "tfl.transpose_conv"(%[[CONST_0]], %[[QCONST_0]], %[[PAD]], %[[QCONST_1]]) <{fused_activation_function = "NONE", padding = "VALID", stride_h = 4 : i32, stride_w = 4 : i32}> : (tensor<4xi32>, tensor<4x2x2x2x!quant.uniform:f32:0, {3.000000e+00,3.000000e+00,3.000000e+00,3.000000e+00}>>, tensor<1x7x7x2x!quant.uniform>, tensor<4x!quant.uniform>) -> tensor<1x14x14x4x!quant.uniform> +// CHECK: return %[[TRANSPOSE_CONV_2D]] + +// ----- + +// Tests that a fused per-channel quantized `stablehlo.convolution` is properly +// lowered to fused `tfl.conv_2d`. +// This case covers for the following quantization patterns because +// activation clipping ranges take affect in scale and zp of the final +// `stablehlo.uniform_quantize`. See more details in b/319168201. +// * conv_with_bias_fn +// * conv_with_bias_and_relu_fn +// * conv_with_bias_and_relu6_fn + +func.func @conv_with_bias_and_relu_srq(%arg0: tensor<1x5x5x2x!quant.uniform>) -> (tensor<1x4x4x4x!quant.uniform>) { + %0 = stablehlo.constant() {value = dense<5> : tensor<1x1x1x4xi32>} : () -> tensor<1x1x1x4x!quant.uniform> + %1 = stablehlo.constant() {value = dense<3> : tensor<4x4x2x4xi8>} : () -> tensor<4x4x2x4x!quant.uniform> + %2 = stablehlo.broadcast_in_dim %0, dims = [0, 1, 2, 3] : (tensor<1x1x1x4x!quant.uniform>) -> tensor<1x4x4x4x!quant.uniform> + %3 = stablehlo.convolution(%arg0, %1) dim_numbers = [b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f], window = {pad = [[1, 1], [1, 1]]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64} : (tensor<1x5x5x2x!quant.uniform>, tensor<4x4x2x4x!quant.uniform>) -> tensor<1x4x4x4x!quant.uniform> + %4 = stablehlo.add %3, %2 : tensor<1x4x4x4x!quant.uniform> + %5 = stablehlo.uniform_quantize %4 : (tensor<1x4x4x4x!quant.uniform>) -> tensor<1x4x4x4x!quant.uniform> + return %5 : tensor<1x4x4x4x!quant.uniform> + } +// CHECK-LABEL: func.func @conv_with_bias_and_relu_srq +// CHECK-SAME: (%[[ARG_0:.+]]: tensor<1x5x5x2x!quant.uniform>) -> tensor<1x4x4x4x!quant.uniform> +// CHECK-DAG: %[[CONST_0:.+]] = "tfl.pseudo_const"() <{value = dense<{{\[\[0, 0\], \[1, 1\], \[1, 1\], \[0, 0\]\]}}> : tensor<4x2xi32>}> : () -> tensor<4x2xi32> +// CHECK-DAG: %[[QCONST_0:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<4x4x4x2x!quant.uniform:f32:0, {3.000000e+00,3.000000e+00,3.000000e+00,3.000000e+00}>>, value = dense<3> : tensor<4x4x4x2xi8>}> : () -> tensor<4x4x4x2x!quant.uniform:f32:0, {3.000000e+00,3.000000e+00,3.000000e+00,3.000000e+00}>> +// CHECK-DAG: %[[QCONST_1:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<4x!quant.uniform>, value = dense<5> : tensor<1x1x1x4xi32>}> : () -> tensor<4x!quant.uniform> +// CHECK: %[[PAD:.+]] = "tfl.pad"(%[[ARG_0]], %[[CONST_0]]) : (tensor<1x5x5x2x!quant.uniform>, tensor<4x2xi32>) -> tensor<1x7x7x2x!quant.uniform> +// CHECK: %[[CONV_2D:.+]] = "tfl.conv_2d"(%[[PAD]], %[[QCONST_0]], %[[QCONST_1]]) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x7x7x2x!quant.uniform>, tensor<4x4x4x2x!quant.uniform:f32:0, {3.000000e+00,3.000000e+00,3.000000e+00,3.000000e+00}>>, tensor<4x!quant.uniform>) -> tensor<1x4x4x4x!quant.uniform> +// CHECK: return %[[CONV_2D]] + +func.func @conv_with_bias_same_padding_srq(%arg0: tensor<1x32x32x3x!quant.uniform>) -> (tensor<1x32x32x2x!quant.uniform>) { + %0 = stablehlo.constant() {value = dense<3> : tensor<3x3x3x2xi8>} : () -> tensor<3x3x3x2x!quant.uniform> + %1 = stablehlo.constant() {value = dense<5> : tensor<1x1x1x2xi32>} : () -> tensor<1x1x1x2x!quant.uniform> + %2 = stablehlo.broadcast_in_dim %1, dims = [0, 1, 2, 3] : (tensor<1x1x1x2x!quant.uniform>) -> tensor<1x32x32x2x!quant.uniform> + %3 = stablehlo.convolution(%arg0, %0) dim_numbers = [b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f], window = {pad = [[1, 1], [1, 1]]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64} : (tensor<1x32x32x3x!quant.uniform>, tensor<3x3x3x2x!quant.uniform>) -> tensor<1x32x32x2x!quant.uniform> + %4 = stablehlo.add %3, %2 : tensor<1x32x32x2x!quant.uniform> + %5 = stablehlo.uniform_quantize %4 : (tensor<1x32x32x2x!quant.uniform>) -> tensor<1x32x32x2x!quant.uniform> + return %5 : tensor<1x32x32x2x!quant.uniform> +} +// CHECK-LABEL: func.func @conv_with_bias_same_padding_srq +// CHECK-SAME: (%[[ARG_0:.+]]: tensor<1x32x32x3x!quant.uniform>) -> tensor<1x32x32x2x!quant.uniform> +// CHECK-DAG: %[[QCONST_0:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<2x3x3x3x!quant.uniform:f32:0, {3.000000e+00,3.000000e+00}>>, value = dense<3> : tensor<2x3x3x3xi8>}> : () -> tensor<2x3x3x3x!quant.uniform:f32:0, {3.000000e+00,3.000000e+00}>> +// CHECK-DAG: %[[QCONST_1:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<2x!quant.uniform>, value = dense<5> : tensor<1x1x1x2xi32>}> : () -> tensor<2x!quant.uniform> +// CHECK: %[[CONV_2D:.+]] = "tfl.conv_2d"(%[[ARG_0]], %[[QCONST_0]], %[[QCONST_1]]) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x32x32x3x!quant.uniform>, tensor<2x3x3x3x!quant.uniform:f32:0, {3.000000e+00,3.000000e+00}>>, tensor<2x!quant.uniform>) -> tensor<1x32x32x2x!quant.uniform> +// CHECK: return %[[CONV_2D]] + +func.func @conv_with_bias_same_padding_srq_depthwise(%arg0: tensor<1x4x5x3x!quant.uniform>) -> (tensor<1x5x6x3x!quant.uniform>) { + %0 = stablehlo.constant() {value = dense<3> : tensor<2x2x1x3xi8>} : () -> tensor<2x2x1x3x!quant.uniform> + %1 = stablehlo.constant() {value = dense<5> : tensor<1x1x1x3xi32>} : () -> tensor<1x1x1x3x!quant.uniform> + %2 = stablehlo.broadcast_in_dim %1, dims = [0, 1, 2, 3] : (tensor<1x1x1x3x!quant.uniform>) -> tensor<1x5x6x3x!quant.uniform> + %3 = stablehlo.convolution(%arg0, %0) dim_numbers = [b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f], window = {pad = [[1, 1], [1, 1]]} {batch_group_count = 1 : i64, feature_group_count = 3 : i64} : (tensor<1x4x5x3x!quant.uniform>, tensor<2x2x1x3x!quant.uniform>) -> tensor<1x5x6x3x!quant.uniform> + %4 = stablehlo.add %3, %2 : tensor<1x5x6x3x!quant.uniform> + %5 = stablehlo.uniform_quantize %4 : (tensor<1x5x6x3x!quant.uniform>) -> tensor<1x5x6x3x!quant.uniform> + return %5 : tensor<1x5x6x3x!quant.uniform> +} +// CHECK-LABEL: func.func @conv_with_bias_same_padding_srq_depthwise +// CHECK-SAME: (%[[ARG_0:.+]]: tensor<1x4x5x3x!quant.uniform>) -> tensor<1x5x6x3x!quant.uniform> +// CHECK-DAG: %[[CONST_0:.+]] = "tfl.pseudo_const"() <{value = dense<{{\[\[0, 0\], \[1, 1\], \[1, 1\], \[0, 0\]\]}}> : tensor<4x2xi32>}> : () -> tensor<4x2xi32> +// CHECK-DAG: %[[QCONST_0:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<1x2x2x3x!quant.uniform:f32:3, {3.000000e+00,3.000000e+00,3.000000e+00}>>, value = dense<3> : tensor<1x2x2x3xi8>}> : () -> tensor<1x2x2x3x!quant.uniform:f32:3, {3.000000e+00,3.000000e+00,3.000000e+00}>> +// CHECK-DAG: %[[QCONST_1:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<3x!quant.uniform>, value = dense<5> : tensor<1x1x1x3xi32>}> : () -> tensor<3x!quant.uniform> +// CHECK: %[[PAD:.+]] = "tfl.pad"(%[[ARG_0]], %[[CONST_0]]) : (tensor<1x4x5x3x!quant.uniform>, tensor<4x2xi32>) -> tensor<1x6x7x3x!quant.uniform> +// CHECK: %[[DEPTHWISE_CONV_2D:.+]] = "tfl.depthwise_conv_2d"(%[[PAD]], %[[QCONST_0]], %[[QCONST_1]]) <{depth_multiplier = 1 : i32, dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x6x7x3x!quant.uniform>, tensor<1x2x2x3x!quant.uniform:f32:3, {3.000000e+00,3.000000e+00,3.000000e+00}>>, tensor<3x!quant.uniform>) -> tensor<1x5x6x3x!quant.uniform> +// CHECK: return %[[DEPTHWISE_CONV_2D]] + +// ----- + +// Tests that a quantized `stablehlo.transpose` is converted to `tfl.transpose`. + +func.func @transpose( + %arg0: tensor<2x3x4x!quant.uniform> + ) -> tensor<4x3x2x!quant.uniform> { + %0 = stablehlo.transpose %arg0, dims = [2, 1, 0] : (tensor<2x3x4x!quant.uniform>) -> tensor<4x3x2x!quant.uniform> + return %0 : tensor<4x3x2x!quant.uniform> +} +// CHECK-LABEL: transpose +// CHECK-SAME: %[[ARG0:.+]]: tensor<2x3x4x!quant.uniform> +// CHECK-NOT: stablehlo.transpose +// CHECK: %[[CST:.+]] = arith.constant dense<[2, 1, 0]> : tensor<3xi32> +// CHECK: %[[TRANSPOSE:.+]] = "tfl.transpose"(%[[ARG0]], %[[CST]]) : (tensor<2x3x4x!quant.uniform>, tensor<3xi32>) -> tensor<4x3x2x!quant.uniform> +// CHECK: return %[[TRANSPOSE]] + +// ----- + +// Tests that a float `stablehlo.transpose` is not converted to `tfl.transpose`. + +func.func @transpose_float(%arg0: tensor<2x3x4xf32>) -> tensor<4x3x2xf32> { + %0 = stablehlo.transpose %arg0, dims = [2, 1, 0] : (tensor<2x3x4xf32>) -> tensor<4x3x2xf32> + return %0 : tensor<4x3x2xf32> +} +// CHECK-LABEL: transpose_float +// CHECK-NOT: tfl.transpose +// CHECK: stablehlo.transpose + +// ----- + +// Tests that a quantized `stablehlo.reshape` is converted to `tfl.reshape`. + +func.func @reshape( + %arg0: tensor<2x3x4x!quant.uniform> + ) -> tensor<6x4x!quant.uniform> { + %0 = stablehlo.reshape %arg0 : (tensor<2x3x4x!quant.uniform>) -> tensor<6x4x!quant.uniform> + return %0 : tensor<6x4x!quant.uniform> +} +// CHECK-LABEL: reshape +// CHECK-SAME: %[[ARG0:.+]]: tensor<2x3x4x!quant.uniform> +// CHECK-NOT: stablehlo.reshape +// CHECK: %[[CST:.+]] = arith.constant dense<[6, 4]> : tensor<2xi32> +// CHECK: %[[RESHAPE:.+]] = "tfl.reshape"(%[[ARG0]], %[[CST]]) : (tensor<2x3x4x!quant.uniform>, tensor<2xi32>) -> tensor<6x4x!quant.uniform> +// CHECK: return %[[RESHAPE]] + +// ----- + +// Tests that a float `stablehlo.reshape` is not converted to `tfl.reshape`. + +func.func @reshape_float(%arg0: tensor<2x3x4xf32>) -> tensor<6x4xf32> { + %0 = stablehlo.reshape %arg0 : (tensor<2x3x4xf32>) -> tensor<6x4xf32> + return %0 : tensor<6x4xf32> +} +// CHECK-LABEL: reshape_float +// CHECK-NOT: tfl.reshape +// CHECK: stablehlo.reshape + +// ----- + +// Tests that a quantized `stablehlo.select` is converted to `tfl.select_v2`. + +func.func @select( + %arg0: tensor<1x3xi1>, + %arg1: tensor<1x3x!quant.uniform>, + %arg2: tensor<1x3x!quant.uniform> + ) -> tensor<1x3x!quant.uniform> { + %0 = "stablehlo.select"(%arg0, %arg1, %arg2) : ( + tensor<1x3xi1>, + tensor<1x3x!quant.uniform>, + tensor<1x3x!quant.uniform> + ) -> tensor<1x3x!quant.uniform> + return %0 : tensor<1x3x!quant.uniform> +} +// CHECK-LABEL: select +// CHECK-SAME: %[[ARG0:.+]]: tensor<1x3xi1>, %[[ARG1:.+]]: tensor<1x3x!quant.uniform>, %[[ARG2:.+]]: tensor<1x3x!quant.uniform> +// CHECK-NOT: stablehlo.select +// CHECK: %[[SELECT:.+]] = "tfl.select_v2"(%[[ARG0]], %[[ARG1]], %[[ARG2]]) : (tensor<1x3xi1>, tensor<1x3x!quant.uniform>, tensor<1x3x!quant.uniform>) -> tensor<1x3x!quant.uniform> +// CHECK: return %[[SELECT]] + +// ----- + +// Tests that a float `stablehlo.select` is not converted to `tfl.select_v2`. + +func.func @select_float(%arg0: tensor<1x3xi1>, %arg1: tensor<1x3xf32>, %arg2: tensor<1x3xf32>) -> tensor<1x3xf32> { + %0 = "stablehlo.select"(%arg0, %arg1, %arg2) : (tensor<1x3xi1>, tensor<1x3xf32>, tensor<1x3xf32>) -> tensor<1x3xf32> + return %0 : tensor<1x3xf32> +} +// CHECK-LABEL: select_float +// CHECK-NOT: tfl.select_v2 +// CHECK: stablehlo.select + +// ----- + +// Tests that a quantized `stablehlo.concatenate` is converted to +// `tfl.concatenation`. + +func.func @concatenate( + %arg0: tensor<3x2x!quant.uniform>, + %arg1: tensor<1x2x!quant.uniform> + ) -> tensor<4x2x!quant.uniform> { + %0 = "stablehlo.concatenate"(%arg0, %arg1) {dimension = 0 : i64} : ( + tensor<3x2x!quant.uniform>, + tensor<1x2x!quant.uniform> + ) -> tensor<4x2x!quant.uniform> + return %0 : tensor<4x2x!quant.uniform> +} +// CHECK-LABEL: concatenate +// CHECK-SAME: %[[ARG0:.+]]: tensor<3x2x!quant.uniform>, %[[ARG1:.+]]: tensor<1x2x!quant.uniform> +// CHECK-NOT: stablehlo.concatenate +// CHECK: %[[CONCAT:.+]] = "tfl.concatenation"(%arg0, %arg1) <{axis = 0 : i32, fused_activation_function = "NONE"}> : (tensor<3x2x!quant.uniform>, tensor<1x2x!quant.uniform>) -> tensor<4x2x!quant.uniform> +// CHECK: return %[[CONCAT]] + +// ----- + +// Tests that a float `stablehlo.concatenate` is not converted to +// `tfl.concatenation`. + +func.func @concatenate_float(%arg0: tensor<3x2xf32>, %arg1: tensor<1x2xf32>) -> tensor<4x2xf32> { + %0 = "stablehlo.concatenate"(%arg0, %arg1) {dimension = 0 : i64} : (tensor<3x2xf32>, tensor<1x2xf32>) -> tensor<4x2xf32> + return %0 : tensor<4x2xf32> +} +// CHECK-LABEL: concatenate_float +// CHECK-NOT: tfl.concatenation +// CHECK: stablehlo.concatenate + +// ----- + +// Tests that a quantized `stablehlo.pad` without interior padding is +// converted to `tfl.padv2`. + +func.func @pad_without_interior_padding( + %arg0: tensor<2x3x!quant.uniform>, + %arg1: tensor> + ) -> tensor<4x5x!quant.uniform> { + %0 = stablehlo.pad %arg0, %arg1, low = [0, 1], high = [2, 1], interior = [0, 0] : ( + tensor<2x3x!quant.uniform>, + tensor> + ) -> tensor<4x5x!quant.uniform> + return %0 : tensor<4x5x!quant.uniform> +} +// CHECK-LABEL: pad_without_interior_padding +// CHECK-SAME: %[[ARG0:.+]]: tensor<2x3x!quant.uniform> +// CHECK-SAME: %[[ARG1:.+]]: tensor> +// CHECK: %[[PADDING:.+]] = arith.constant +// CHECK{LITERAL}: dense<[[0, 2], [1, 1]]> : tensor<2x2xi32> +// CHECK: %[[PAD:.+]] = "tfl.padv2"(%[[ARG0]], %[[PADDING]], %[[ARG1]]) : (tensor<2x3x!quant.uniform>, tensor<2x2xi32>, tensor>) -> tensor<4x5x!quant.uniform> +// CHECK: return %[[PAD]] + +// ----- + +// Tests that a quantized `stablehlo.pad` with interior padding is converted to +// `tfl.dilate` and `tfl.padv2`. + +func.func @pad_with_interior_padding( + %arg0: tensor<2x3x!quant.uniform>, + %arg1: tensor> + ) -> tensor<5x9x!quant.uniform> { + %0 = stablehlo.pad %arg0, %arg1, low = [0, 1], high = [2, 1], interior = [1, 2] : ( + tensor<2x3x!quant.uniform>, + tensor> + ) -> tensor<5x9x!quant.uniform> + return %0 : tensor<5x9x!quant.uniform> +} +// CHECK-LABEL: pad_with_interior_padding +// CHECK-SAME: %[[ARG0:.+]]: tensor<2x3x!quant.uniform> +// CHECK-SAME: %[[ARG1:.+]]: tensor> +// CHECK: %[[PADDING:.+]] = arith.constant +// CHECK{LITERAL}: dense<[[0, 2], [1, 1]]> : tensor<2x2xi32> +// CHECK: %[[INTERIOR:.+]] = arith.constant +// CHECK{LITERAL}: dense<[1, 2]> : tensor<2xi32> +// CHECK: %[[DILATE:.+]] = "tfl.dilate"(%[[ARG0]], %[[INTERIOR]], %[[ARG1]]) : (tensor<2x3x!quant.uniform>, tensor<2xi32>, tensor>) -> tensor<3x7x!quant.uniform> +// CHECK: %[[PAD:.+]] = "tfl.padv2"(%[[DILATE]], %[[PADDING]], %[[ARG1]]) : (tensor<3x7x!quant.uniform>, tensor<2x2xi32>, tensor>) -> tensor<5x9x!quant.uniform> +// CHECK: return %[[PAD]] + +// ----- + +// Tests that a float `stablehlo.pad` is not converted to `tfl.padv2`. + +func.func @pad_float(%arg0: tensor<2x3xf32>, %arg1: tensor) -> tensor<4x5xf32> { + %0 = stablehlo.pad %arg0, %arg1, low = [0, 1], high = [2, 1], interior = [0, 0] : (tensor<2x3xf32>, tensor) -> tensor<4x5xf32> + return %0 : tensor<4x5xf32> +} +// CHECK-LABEL: pad_float +// CHECK-NOT: tfl.padv2 +// CHECK: stablehlo.pad + +// ----- + +// Tests that a quantized `stablehlo.slice` is converted to +// `tfl.slice` when stride is 1. + +func.func @slice( + %arg0: tensor<3x4x!quant.uniform> + ) -> tensor<2x2x!quant.uniform> { + %0 = "stablehlo.slice"(%arg0) { + start_indices = array, + limit_indices = array, + strides = array + } : ( + tensor<3x4x!quant.uniform> + ) -> tensor<2x2x!quant.uniform> + return %0 : tensor<2x2x!quant.uniform> +} +// CHECK-LABEL: slice +// CHECK-SAME: %[[ARG0:.+]]: tensor<3x4x!quant.uniform> +// CHECK-DAG: %[[START:.+]] = arith.constant dense<{{\[1, 2\]}}> : tensor<2xi32> +// CHECK-DAG: %[[SIZE:.+]] = arith.constant dense<2> : tensor<2xi32> +// CHECK: %[[SLICE:.+]] = "tfl.slice"(%[[ARG0]], %[[START]], %[[SIZE]]) : (tensor<3x4x!quant.uniform>, tensor<2xi32>, tensor<2xi32>) -> tensor<2x2x!quant.uniform> +// CHECK: return %[[SLICE]] + +// ----- + +// Tests that a quantized `stablehlo.slice` is converted to `tfl.strided_slice` +// when stride is not 1. + +func.func @strided_slice( + %arg0: tensor<3x6x!quant.uniform> + ) -> tensor<2x2x!quant.uniform> { + %0 = "stablehlo.slice"(%arg0) { + start_indices = array, + limit_indices = array, + strides = array + } : ( + tensor<3x6x!quant.uniform> + ) -> tensor<2x2x!quant.uniform> + return %0 : tensor<2x2x!quant.uniform> +} +// CHECK-LABEL: strided_slice +// CHECK-SAME: %[[ARG0:.+]]: tensor<3x6x!quant.uniform> +// CHECK: %[[START:.+]] = arith.constant +// CHECK{LITERAL}: dense<[0, 2]> : tensor<2xi32> +// CHECK: %[[SIZE:.+]] = arith.constant +// CHECK{LITERAL}: dense<[3, 4]> : tensor<2xi32> +// CHECK: %[[STRIDE:.+]] = arith.constant +// CHECK{LITERAL}: dense<[2, 3]> : tensor<2xi32> +// CHECK: %[[SLICE:.+]] = "tfl.strided_slice"(%[[ARG0]], %[[START]], %[[SIZE]], %[[STRIDE]]) <{begin_mask = 0 : i32, ellipsis_mask = 0 : i32, end_mask = 0 : i32, new_axis_mask = 0 : i32, offset = false, shrink_axis_mask = 0 : i32}> : (tensor<3x6x!quant.uniform>, tensor<2xi32>, tensor<2xi32>, tensor<2xi32>) -> tensor<2x2x!quant.uniform> +// CHECK: return %[[SLICE]] + +// ----- + +// Tests that a float `stablehlo.slice` is not converted to `tfl.slice`. + +func.func @slice_float(%arg0: tensor<3x4xf32>) -> tensor<2x2xf32> { + %0 = "stablehlo.slice"(%arg0) { + start_indices = array, + limit_indices = array, + strides = array + } : (tensor<3x4xf32>) -> tensor<2x2xf32> + return %0 : tensor<2x2xf32> +} +// CHECK-LABEL: slice_float +// CHECK-NOT: tfl.slice +// CHECK-NOT: tfl.strided_slice +// CHECK: stablehlo.slice + +// ----- + +// Tests that a quantized `stablehlo.broadcast_in_dim` is converted to +// `tfl.broadcast_to`. + +func.func @broadcast_in_dim( + %arg0: tensor<1x2x!quant.uniform> + ) -> tensor<3x2x!quant.uniform> { + %0 = "stablehlo.broadcast_in_dim"(%arg0) { + broadcast_dimensions = array + } : (tensor<1x2x!quant.uniform>) -> tensor<3x2x!quant.uniform> + return %0 : tensor<3x2x!quant.uniform> +} +// CHECK-LABEL: broadcast_in_dim +// CHECK-SAME: %[[ARG0:.+]]: tensor<1x2x!quant.uniform> +// CHECK: %[[SHAPE:.+]] = arith.constant +// CHECK{LITERAL}: dense<[3, 2]> : tensor<2xi32> +// CHECK: %[[BROADCAST:.+]] = "tfl.broadcast_to"(%[[ARG0]], %[[SHAPE]]) : (tensor<1x2x!quant.uniform>, tensor<2xi32>) -> tensor<3x2x!quant.uniform> +// CHECK: return %[[BROADCAST]] + +// ----- + +// Tests that a quantized `stablehlo.broadcast_in_dim` is converted to +// `tfl.transpose` and `tfl.broadcast_to` when `broadcast_dimensions` is not in +// ascending order. + +func.func @broadcast_in_dim_with_transpose( + %arg0: tensor<1x2x!quant.uniform> + ) -> tensor<2x3x!quant.uniform> { + %0 = "stablehlo.broadcast_in_dim"(%arg0) { + broadcast_dimensions = array + } : (tensor<1x2x!quant.uniform>) -> tensor<2x3x!quant.uniform> + return %0 : tensor<2x3x!quant.uniform> +} +// CHECK-LABEL: broadcast_in_dim_with_transpose +// CHECK-SAME: %[[ARG0:.+]]: tensor<1x2x!quant.uniform> +// CHECK: %[[BROADCAST_DIM:.+]] = arith.constant +// CHECK{LITERAL}: dense<[2, 3]> : tensor<2xi32> +// CHECK: %[[PERM:.+]] = arith.constant +// CHECK{LITERAL}: dense<[1, 0]> : tensor<2xi32> +// CHECK: %[[TRANSPOSE:.+]] = "tfl.transpose"(%[[ARG0]], %[[PERM]]) : (tensor<1x2x!quant.uniform>, tensor<2xi32>) -> tensor<2x1x!quant.uniform> +// CHECK: %[[BROADCAST:.+]] = "tfl.broadcast_to"(%[[TRANSPOSE]], %[[BROADCAST_DIM]]) : (tensor<2x1x!quant.uniform>, tensor<2xi32>) -> tensor<2x3x!quant.uniform> +// CHECK: return %[[BROADCAST]] + +// ----- + +// Tests that a quantized `stablehlo.broadcast_in_dim` is converted to +// tfl.expand_dims and `tfl.broadcast_to` when input rank is smaller than output +// rank. + +func.func @broadcast_in_dim_with_expand( + %arg0: tensor<1x2x!quant.uniform> + ) -> tensor<3x2x1x1x!quant.uniform> { + %0 = "stablehlo.broadcast_in_dim"(%arg0) { + broadcast_dimensions = array + } : (tensor<1x2x!quant.uniform>) -> tensor<3x2x1x1x!quant.uniform> + return %0 : tensor<3x2x1x1x!quant.uniform> +} +// CHECK-LABEL: broadcast_in_dim_with_expand +// CHECK-SAME: %[[ARG0:.+]]: tensor<1x2x!quant.uniform> +// CHECK-DAG: %[[BROADCAST_DIM:.+]] = arith.constant dense<{{\[3, 2, 1, 1\]}}> : tensor<4xi32> +// CHECK-DAG: %[[EXPAND_DIM1:.+]] = arith.constant dense<3> : tensor<1xi32> +// CHECK-DAG: %[[EXPAND_DIM0:.+]] = arith.constant dense<2> : tensor<1xi32> +// CHECK: %[[EXPAND0:.+]] = "tfl.expand_dims"(%[[ARG0]], %[[EXPAND_DIM0]]) : (tensor<1x2x!quant.uniform>, tensor<1xi32>) -> tensor<1x2x1x!quant.uniform> +// CHECK: %[[EXPAND1:.+]] = "tfl.expand_dims"(%[[EXPAND0]], %[[EXPAND_DIM1]]) : (tensor<1x2x1x!quant.uniform>, tensor<1xi32>) -> tensor<1x2x1x1x!quant.uniform> +// CHECK: %[[BROADCAST:.+]] = "tfl.broadcast_to"(%[[EXPAND1]], %[[BROADCAST_DIM]]) : (tensor<1x2x1x1x!quant.uniform>, tensor<4xi32>) -> tensor<3x2x1x1x!quant.uniform> +// CHECK: return %[[BROADCAST]] + +// ----- + +// Tests that a quantized `stablehlo.broadcast_in_dim` is converted to +// `tfl.transpose`, `tfl.expand_dims` and `tfl.broadcast_to` when +// `broadcast_dimensions` is not in ascending order and input rank is smaller +// than output rank. + +func.func @broadcast_in_dim_with_transpose_and_expand( + %arg0: tensor<2x3x4x!quant.uniform> + ) -> tensor<3x2x1x1x4x!quant.uniform> { + %0 = "stablehlo.broadcast_in_dim"(%arg0) { + broadcast_dimensions = array + } : (tensor<2x3x4x!quant.uniform>) -> tensor<3x2x1x1x4x!quant.uniform> + return %0 : tensor<3x2x1x1x4x!quant.uniform> +} +// CHECK-LABEL: broadcast_in_dim_with_transpose_and_expand +// CHECK-SAME: %[[ARG0:.+]]: tensor<2x3x4x!quant.uniform> +// CHECK-DAG: %[[BROADCAST_DIM:.+]] = arith.constant dense<{{\[3, 2, 1, 1, 4\]}}> : tensor<5xi32> +// CHECK-DAG: %[[EXPAND_DIM1:.+]] = arith.constant dense<3> : tensor<1xi32> +// CHECK-DAG: %[[EXPAND_DIM0:.+]] = arith.constant dense<2> : tensor<1xi32> +// CHECK-DAG: %[[PERM:.+]] = arith.constant dense<{{\[1, 0, 2\]}}> : tensor<3xi32> +// CHECK: %[[TRANSPOSE:.+]] = "tfl.transpose"(%[[ARG0]], %[[PERM]]) : (tensor<2x3x4x!quant.uniform>, tensor<3xi32>) -> tensor<3x2x4x!quant.uniform> +// CHECK: %[[EXPAND0:.+]] = "tfl.expand_dims"(%[[TRANSPOSE]], %[[EXPAND_DIM0]]) : (tensor<3x2x4x!quant.uniform>, tensor<1xi32>) -> tensor<3x2x1x4x!quant.uniform> +// CHECK: %[[EXPAND1:.+]] = "tfl.expand_dims"(%[[EXPAND0]], %[[EXPAND_DIM1]]) : (tensor<3x2x1x4x!quant.uniform>, tensor<1xi32>) -> tensor<3x2x1x1x4x!quant.uniform> +// CHECK: %[[BROADCAST:.+]] = "tfl.broadcast_to"(%[[EXPAND1]], %[[BROADCAST_DIM]]) : (tensor<3x2x1x1x4x!quant.uniform>, tensor<5xi32>) -> tensor<3x2x1x1x4x!quant.uniform> +// CHECK: return %[[BROADCAST]] + +// ----- + +// Tests that a float `stablehlo.broadcast_in_dim` is not converted to +// `tfl.broadcast_to`. + +func.func @broadcast_in_dim_float(%arg0: tensor<1x2xf32>) -> tensor<3x2xf32> { + %0 = "stablehlo.broadcast_in_dim"(%arg0) { + broadcast_dimensions = array + } : (tensor<1x2xf32>) -> tensor<3x2xf32> + return %0 : tensor<3x2xf32> +} +// CHECK-LABEL: broadcast_in_dim_float +// CHECK-NOT: tfl.broadcast_to +// CHECK-NOT: tfl.transpose +// CHECK-NOT: tfl.expand_dims +// CHECK: stablehlo.broadcast_in_dim + +// ----- + +// Tests that a quantized `stablehlo.reduce_window` with max is converted to +// `tfl.max_pool_2d`. + +func.func @reduce_window_with_max( + %arg0: tensor<2x9x10x3x!quant.uniform>, + %arg1: tensor> +) -> tensor<2x4x3x3x!quant.uniform> { + %0 = "stablehlo.reduce_window"(%arg0, %arg1) ({ + ^bb0(%arg2: tensor>, %arg3: tensor>): + %1 = stablehlo.maximum %arg2, %arg3 : tensor> + stablehlo.return %1 : tensor> + }) {window_dimensions = array, window_strides = array} : (tensor<2x9x10x3x!quant.uniform>, tensor>) -> tensor<2x4x3x3x!quant.uniform> + return %0 : tensor<2x4x3x3x!quant.uniform> +} + +// CHECK-LABEL: reduce_window_with_max +// CHECK-SAME: %[[ARG0:.*]]: tensor<2x9x10x3x!quant.uniform> +// CHECK-SAME: %[[ARG1:.*]]: tensor> +// CHECK: %[[MAX_POOL:.*]] = "tfl.max_pool_2d"(%[[ARG0]]) +// CHECK-SAME: {filter_height = 3 : i32, filter_width = 4 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 2 : i32, stride_w = 3 : i32} +// CHECK-SAME: (tensor<2x9x10x3x!quant.uniform>) -> tensor<2x4x3x3x!quant.uniform> +// CHECK: return %[[MAX_POOL]] + +// ----- + +// Tests that a quantized `stablehlo.reduce_window `with max whose rank is not 4 +// is not converted to `tfl.max_pool_2d`. + +func.func @reduce_window_not_4d( + %arg0: tensor<3x2x9x10x3x!quant.uniform>, + %arg1: tensor> +) -> tensor<3x2x4x3x3x!quant.uniform> { + %0 = "stablehlo.reduce_window"(%arg0, %arg1) ({ + ^bb0(%arg2: tensor>, %arg3: tensor>): + %1 = stablehlo.maximum %arg2, %arg3 : tensor> + stablehlo.return %1 : tensor> + }) {window_dimensions = array, window_strides = array} : (tensor<3x2x9x10x3x!quant.uniform>, tensor>) -> tensor<3x2x4x3x3x!quant.uniform> + return %0 : tensor<3x2x4x3x3x!quant.uniform> +} + +// CHECK-LABEL: reduce_window_not_4d +// CHECK: stablehlo.reduce_window +// CHECK-NOT: tfl.max_pool_2d + +// ----- + +// Tests that a quantized `stablehlo.reduce_window` with max that takes multiple +// inputs is not converted to `tfl.max_pool_2d`. + +func.func @reduce_window_not_binary( + %arg0: tensor<3x2x9x10x3x!quant.uniform>, + %arg1: tensor<3x2x9x10x3x!quant.uniform>, + %arg2: tensor>, + %arg3: tensor> +) -> tensor<3x2x4x3x3x!quant.uniform> { + %0, %1 = "stablehlo.reduce_window"(%arg0, %arg1, %arg2, %arg3) ({ + ^bb0(%arg4: tensor>, %arg5: tensor>, %arg6: tensor>, %arg7: tensor>): + %2 = stablehlo.maximum %arg4, %arg5 : tensor> + %3 = stablehlo.maximum %arg6, %arg7 : tensor> + stablehlo.return %2, %3 : tensor>, tensor> + }) {window_dimensions = array, window_strides = array} : (tensor<3x2x9x10x3x!quant.uniform>, tensor<3x2x9x10x3x!quant.uniform>, tensor>, tensor>) -> (tensor<3x2x4x3x3x!quant.uniform>, tensor<3x2x4x3x3x!quant.uniform>) + return %0 : tensor<3x2x4x3x3x!quant.uniform> +} + +// CHECK-LABEL: reduce_window_not_binary +// CHECK: stablehlo.reduce_window +// CHECK-NOT: tfl.max_pool_2d + +// ----- + +// Tests that a float `stablehlo.reduce_window` with max is not converted to +// `tfl.max_pool_2d`. + +func.func @reduce_window_with_max_float( + %arg0: tensor<2x9x10x3xf32>, + %arg1: tensor +) -> tensor<2x4x3x3xf32> { + %0 = "stablehlo.reduce_window"(%arg0, %arg1) ({ + ^bb0(%arg2: tensor, %arg3: tensor): + %1 = stablehlo.maximum %arg2, %arg3 : tensor + stablehlo.return %1 : tensor + }) {window_dimensions = array, window_strides = array} : (tensor<2x9x10x3xf32>, tensor) -> tensor<2x4x3x3xf32> + return %0 : tensor<2x4x3x3xf32> +} + +// CHECK-LABEL: reduce_window_with_max_float +// CHECK: stablehlo.reduce_window +// CHECK-NOT: tfl.max_pool_2d + +// ----- + +// Tests that a quantized `stablehlo.dynamic_reshape` is converted to +// `tfl.reshape`. + +func.func @dynamic_reshape( + %arg0: tensor>, + %arg1: tensor<2xi32> + ) -> tensor> { + %0 = "stablehlo.dynamic_reshape"(%arg0, %arg1) : ( + tensor>, tensor<2xi32> + ) -> tensor> + return %0 : tensor> +} + +// CHECK-LABEL: func @dynamic_reshape +// CHECK-SAME: %[[ARG0:.+]]: tensor> +// CHECK-SAME: %[[ARG1:.+]]: tensor<2xi32> +// CHECK: %[[RESHAPE:.+]] = "tfl.reshape"(%[[ARG0]], %[[ARG1]]) : (tensor>, tensor<2xi32>) -> tensor> +// CHECK: return %[[RESHAPE]] +// CHECK-NOT: stablehlo.dynamic_reshape + +// ----- + +// Tests that a float `stablehlo.dynamic_reshape` is not converted to +// `tfl.reshape`. + +func.func @dynamic_reshape_float(%arg0: tensor, %arg1: tensor<2xi32>) -> tensor { + %0 = "stablehlo.dynamic_reshape"(%arg0, %arg1) : (tensor, tensor<2xi32>) -> tensor + return %0 : tensor +} + +// CHECK-LABEL: func @dynamic_reshape_float +// CHECK: stablehlo.dynamic_reshape +// CHECK-NOT: tfl.reshape + +// ----- + +// Tests that a quantized `stablehlo.gather` is converted to tfl.gather_nd. + +func.func @gather( + %arg0: tensor<3x4x2x2x!quant.uniform>, + %arg1: tensor<2x3x2xi64> + ) -> tensor<2x3x2x2x!quant.uniform> { + %0 = "stablehlo.gather"(%arg0, %arg1) { + dimension_numbers = #stablehlo.gather< + offset_dims = [2, 3], + collapsed_slice_dims = [0, 1], + start_index_map = [0, 1], + index_vector_dim = 2>, + slice_sizes = array, + indices_are_sorted = false + } : ( + tensor<3x4x2x2x!quant.uniform>, + tensor<2x3x2xi64> + ) -> tensor<2x3x2x2x!quant.uniform> + return %0 : tensor<2x3x2x2x!quant.uniform> +} + +// CHECK-LABEL: func @gather +// CHECK-SAME: %[[ARG_0:.+]]: tensor<3x4x2x2x!quant.uniform>, %[[ARG_1:.+]]: tensor<2x3x2xi64> +// CHECK: %[[GATHER:.+]] = "tfl.gather_nd"(%[[ARG_0]], %[[ARG_1]]) +// CHECK-SAME: (tensor<3x4x2x2x!quant.uniform>, tensor<2x3x2xi64>) -> tensor<2x3x2x2x!quant.uniform> +// CHECK: return %[[GATHER]] + +// ----- + +// Tests that a quantized `stablehlo.gather` with unsorted start_index_map is +// not converted to `tfl.gather_nd` (condition 1 is not satisfied). + +func.func @gather_start_index_map_not_sorted( + %arg0: tensor<3x4x2x2x!quant.uniform>, + %arg1: tensor<2x3x2xi64> + ) -> tensor<2x3x2x2x!quant.uniform> { + %0 = "stablehlo.gather"(%arg0, %arg1) { + dimension_numbers = #stablehlo.gather< + offset_dims = [2, 3], + collapsed_slice_dims = [0, 1], + start_index_map = [1, 0], + index_vector_dim = 2>, + slice_sizes = array, + indices_are_sorted = false + } : ( + tensor<3x4x2x2x!quant.uniform>, + tensor<2x3x2xi64> + ) -> tensor<2x3x2x2x!quant.uniform> + return %0 : tensor<2x3x2x2x!quant.uniform> +} + +// CHECK-LABEL: func @gather_start_index_map_not_sorted +// CHECK: stablehlo.gather +// CHECK-NOT: tfl.gather_nd +// CHECK-NOT: tfl.gather + +// ----- + +// Tests that a quantized `stablehlo.gather` is not converted to tfl.gather_nd +// when index_vector_dim is not the last dimension of start_indices (condition 2 +// is not satisfied). + +func.func @gather_start_index_vector_dim_not_at_last( + %arg0: tensor<3x4x2x2x!quant.uniform>, + %arg1: tensor<2x3x2xi64> + ) -> tensor<3x2x2x2x!quant.uniform> { + %0 = "stablehlo.gather"(%arg0, %arg1) { + dimension_numbers = #stablehlo.gather< + offset_dims = [2, 3], + collapsed_slice_dims = [0, 1], + start_index_map = [0, 1], + index_vector_dim = 0>, + slice_sizes = array, + indices_are_sorted = false + } : ( + tensor<3x4x2x2x!quant.uniform>, + tensor<2x3x2xi64> + ) -> tensor<3x2x2x2x!quant.uniform> + return %0 : tensor<3x2x2x2x!quant.uniform> +} + +// CHECK-LABEL: func @gather_start_index_vector_dim_not_at_last +// CHECK: stablehlo.gather +// CHECK-NOT: tfl.gather_nd +// CHECK-NOT: tfl.gather + +// ----- + +// Tests that a quantized `stablehlo.gather` is not converted to tfl.gather_nd +// when offset_dims are not the last dimensions of the output (condition 3 is +// not satisfied). + +func.func @gather_offset_dims_not_at_last( + %arg0: tensor<3x4x2x2x!quant.uniform>, + %arg1: tensor<2x3x2xi64> + ) -> tensor<2x2x2x3x!quant.uniform> { + %0 = "stablehlo.gather"(%arg0, %arg1) { + dimension_numbers = #stablehlo.gather< + offset_dims = [0, 1], + collapsed_slice_dims = [0, 1], + start_index_map = [0, 1], + index_vector_dim = 2>, + slice_sizes = array, + indices_are_sorted = false + } : ( + tensor<3x4x2x2x!quant.uniform>, + tensor<2x3x2xi64> + ) -> tensor<2x2x2x3x!quant.uniform> + return %0 : tensor<2x2x2x3x!quant.uniform> +} + +// CHECK-LABEL: func @gather_offset_dims_not_at_last +// CHECK: stablehlo.gather +// CHECK-NOT: tfl.gather_nd +// CHECK-NOT: tfl.gather + +// ----- + +// Tests that a quantized `stablehlo.gather` is not converted to tfl.gather_nd +// when shape of slice is not same with shape of offset (condition 4 is not +// satisfied). + +func.func @gather_different_slice_and_offset( + %arg0: tensor<3x4x2x3x!quant.uniform>, + %arg1: tensor<2x3x2xi64> + ) -> tensor<2x3x2x2x!quant.uniform> { + %0 = "stablehlo.gather"(%arg0, %arg1) { + dimension_numbers = #stablehlo.gather< + offset_dims = [2, 3], + collapsed_slice_dims = [0, 1], + start_index_map = [0, 1], + index_vector_dim = 2>, + slice_sizes = array, + indices_are_sorted = false + } : ( + tensor<3x4x2x3x!quant.uniform>, + tensor<2x3x2xi64> + ) -> tensor<2x3x2x2x!quant.uniform> + return %0 : tensor<2x3x2x2x!quant.uniform> +} + +// CHECK-LABEL: func @gather_different_slice_and_offset +// CHECK: stablehlo.gather +// CHECK-NOT: tfl.gather_nd +// CHECK-NOT: tfl.gather + +// ----- + +// Tests that a float `stablehlo.gather` is not converted to `tfl.gather_nd`. + +func.func @gather_float(%arg0: tensor<3x4x2x2xf32>, %arg1: tensor<2x3x2xi64>) -> tensor<2x3x2x2xf32> { + %0 = "stablehlo.gather"(%arg0, %arg1) { + dimension_numbers = #stablehlo.gather< + offset_dims = [2, 3], + collapsed_slice_dims = [0, 1], + start_index_map = [0, 1], + index_vector_dim = 2>, + slice_sizes = array, + indices_are_sorted = false + } : (tensor<3x4x2x2xf32>, tensor<2x3x2xi64>) -> tensor<2x3x2x2xf32> + return %0 : tensor<2x3x2x2xf32> +} + +// CHECK-LABEL: func @gather_float +// CHECK: stablehlo.gather +// CHECK-NOT: tfl.gather_nd +// CHECK-NOT: tfl.gather + +// ----- + +// Tests that a quantized `stablehlo.dynamic_slice` is converted to `tfl.slice`. + +// CHECK-LABEL: func @dynamic_slice +// CHECK-SAME: %[[ARG0:.+]]: tensor<4x4x!quant.uniform>, %[[ARG1:.+]]: tensor, %[[ARG2:.+]]: tensor +func.func @dynamic_slice( + %arg0: tensor<4x4x!quant.uniform>, + %arg1: tensor, + %arg2: tensor + ) -> tensor<2x1x!quant.uniform> { + %0 = "stablehlo.dynamic_slice"(%arg0, %arg1, %arg2) { + slice_sizes = array + } : ( + tensor<4x4x!quant.uniform>, tensor, + tensor + ) -> tensor<2x1x!quant.uniform> + return %0 : tensor<2x1x!quant.uniform> +} + + +// CHECK-DAG: %[[SLICE_SIZE:.+]] = arith.constant dense<[2, 1]> : tensor<2xi64> +// CHECK-DAG: %[[ZERO:.+]] = arith.constant dense<0> : tensor<1xi64> +// CHECK-DAG: %[[MAX1:.+]] = arith.constant dense<2> : tensor<1xi64> +// CHECK-DAG: %[[MAX2:.+]] = arith.constant dense<3> : tensor<1xi64> +// CHECK: %[[BITCAST1:.+]] = "tfl.bitcast"(%[[ARG1]]) : (tensor) -> tensor<1xi64> +// CHECK: %[[MIN1:.+]] = "tfl.minimum"(%[[BITCAST1]], %[[MAX1]]) : (tensor<1xi64>, tensor<1xi64>) -> tensor<1xi64> +// CHECK: %[[BITCAST2:.+]] = "tfl.bitcast"(%[[ARG2]]) : (tensor) -> tensor<1xi64> +// CHECK: %[[MIN2:.+]] = "tfl.minimum"(%[[BITCAST2]], %[[MAX2]]) : (tensor<1xi64>, tensor<1xi64>) -> tensor<1xi64> +// CHECK: %[[CONCAT:.+]] = "tfl.concatenation"(%[[MIN1]], %[[MIN2]]) <{axis = 0 : i32, fused_activation_function = "NONE"}> : (tensor<1xi64>, tensor<1xi64>) -> tensor<2xi64> +// CHECK: %[[MAX:.+]] = "tfl.maximum"(%[[CONCAT]], %[[ZERO]]) : (tensor<2xi64>, tensor<1xi64>) -> tensor<2xi64> +// CHECK: %[[SLICE:.+]] = "tfl.slice"(%[[ARG0]], %[[MAX]], %[[SLICE_SIZE]]) +// CHECK-SAME: (tensor<4x4x!quant.uniform>, tensor<2xi64>, tensor<2xi64>) -> tensor<2x1x!quant.uniform> + +// ----- + +// Tests that a float `stablehlo.dynamic_slice` is not converted to `tfl.slice`. + +func.func @dynamic_slice_float(%arg0: tensor<4x4xf32>, %arg1: tensor, %arg2: tensor) -> tensor<2x1xf32> { + %0 = "stablehlo.dynamic_slice"(%arg0, %arg1, %arg2) { + slice_sizes = array + } : (tensor<4x4xf32>, tensor, tensor) -> tensor<2x1xf32> + return %0 : tensor<2x1xf32> +} + +// CHECK-LABEL: func @dynamic_slice_float +// CHECK: stablehlo.dynamic_slice +// CHECK-NOT: tfl.bitcast +// CHECK-NOT: tfl.minimum +// CHECK-NOT: tfl.maximum +// CHECK-NOT: tfl.slice + +// ----- + +// Tests that `stablehlo.add` with both operands int8 UniformQuantizedType is +// properly converted into `tfl.add`. + +func.func @add(%arg0: tensor<1x3x!quant.uniform>, %arg1: tensor<1x3x!quant.uniform>) -> tensor<1x3x!quant.uniform> { + %0 = stablehlo.add %arg0, %arg1 : (tensor<1x3x!quant.uniform>, tensor<1x3x!quant.uniform>) -> tensor<1x3x!quant.uniform> + return %0 : tensor<1x3x!quant.uniform> +} + +// CHECK-LABEL: func @add +// CHECK: %[[ADD:.+]] = tfl.add(%arg0, %arg1) <{fused_activation_function = "NONE"}> : (tensor<1x3x!quant.uniform>, tensor<1x3x!quant.uniform>) -> tensor<1x3x!quant.uniform> +// CHECK: return %[[ADD]] + +// ----- + +// Tests that `stablehlo.add` with int32 UniformQuantizedPerAxisTypes is +// not converted. + +func.func @add_i32(%arg0: tensor<1x3x!quant.uniform>, %arg1: tensor<1x3x!quant.uniform>) -> tensor<1x3x!quant.uniform> { + %0 = stablehlo.add %arg0, %arg1 : (tensor<1x3x!quant.uniform>, tensor<1x3x!quant.uniform>) -> tensor<1x3x!quant.uniform> + return %0 : tensor<1x3x!quant.uniform> +} + +// CHECK-LABEL: func @add_i32 +// CHECK: stablehlo.add +// CHECK-NOT: tfl.add + +// ----- + +// Tests that a quantized `stablehlo.constant` is converted into `tfl.qconst`. + +// CHECK-LABEL: func @quantized_constant +func.func @quantized_constant() -> tensor<1x2x4x5x!quant.uniform> { + %0 = stablehlo.constant() {value = dense<1> : tensor<1x2x4x5xi8>} : () -> tensor<1x2x4x5x!quant.uniform> + return %0 : tensor<1x2x4x5x!quant.uniform> +} + +// CHECK: %[[QCONST:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<1x2x4x5x!quant.uniform>, value = dense<1> : tensor<1x2x4x5xi8>}> +// CHECK-SAME: () -> tensor<1x2x4x5x!quant.uniform> +// CHECK: return %[[QCONST]] + +// ----- + +// Tests that a float `stablehlo.constant` is not converted into `tfl.qconst`. + +// CHECK-LABEL: func @float_constant +func.func @float_constant() -> tensor<1x2x4x5xf32> { + %0 = stablehlo.constant() {value = dense<1.0> : tensor<1x2x4x5xf32>} : () -> tensor<1x2x4x5xf32> + return %0 : tensor<1x2x4x5xf32> +} + +// CHECK: stablehlo.constant +// CHECK-NOT: tfl.pseudo_qconst +// CHECK-NOT: tfl.pseudo_const +// CHECK-NOT: arith.constant + +// ----- + +// Tests that a hybrid quantized dot_general is splitted into dequantize and float +// dot_general. + +// CHECK-LABEL: func @dot_general_hybrid +// CHECK-SAME: %[[ARG0:.+]]: tensor<1x2x3x4xf32> +func.func @dot_general_hybrid(%arg0: tensor<1x2x3x4xf32>) -> tensor<1x2x3x5xf32> { + %0 = stablehlo.constant() {value = dense<1> : tensor<1x2x4x5xi8>} : () -> tensor<1x2x4x5x!quant.uniform> + %1 = "stablehlo.dot_general"(%arg0, %0) { + dot_dimension_numbers = #stablehlo.dot< + lhs_batching_dimensions = [0, 1], + rhs_batching_dimensions = [0, 1], + lhs_contracting_dimensions = [3], + rhs_contracting_dimensions = [2]>, + precision_config = [#stablehlo, #stablehlo] + } : (tensor<1x2x3x4xf32>, tensor<1x2x4x5x!quant.uniform>) -> tensor<1x2x3x5xf32> + return %1 : tensor<1x2x3x5xf32> +} + +// CHECK: %[[WEIGHT:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<1x2x4x5x!quant.uniform>, value = dense<1> : tensor<1x2x4x5xi8>}> +// CHECK: %[[DQ:.+]] = "tfl.dequantize"(%[[WEIGHT]]) : (tensor<1x2x4x5x!quant.uniform>) -> tensor<1x2x4x5xf32> +// CHECK: %[[DOT:.+]] = stablehlo.dot_general %[[ARG0]], %[[DQ]], batching_dims = [0, 1] x [0, 1], contracting_dims = [3] x [2], precision = [DEFAULT, DEFAULT] : (tensor<1x2x3x4xf32>, tensor<1x2x4x5xf32>) -> tensor<1x2x3x5xf32> +// CHECK: return %[[DOT]] + +// ----- + +// Tests that a hybrid per-channel quantized convolution for tfl.conv_2d is +// splitted into dequantize and float stablehlo.convolution. + +// CHECK-LABEL: func @convolution_hybrid_per_channel +// CHECK-SAME: %[[ARG0:.+]]: tensor<1x3x3x4xf32> +func.func @convolution_hybrid_per_channel(%arg0: tensor<1x3x3x4xf32>) -> tensor<1x3x3x2xf32> { + %0 = stablehlo.constant() {value = dense<3> : tensor<3x3x4x2xi8>} : () -> tensor<3x3x4x2x!quant.uniform> + %1 = stablehlo.convolution(%arg0, %0) dim_numbers = [b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f], window = {pad = [[1, 1], [1, 1]]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64} : (tensor<1x3x3x4xf32>, tensor<3x3x4x2x!quant.uniform>) -> tensor<1x3x3x2xf32> + return %1 : tensor<1x3x3x2xf32> +} + +// CHECK: %[[WEIGHT:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<2x3x3x4x!quant.uniform:f32:0, {2.000000e+02,3.000000e+03}>>, value = dense<3> : tensor<2x3x3x4xi8>}> +// CHECK: %[[DQ:.+]] = "tfl.dequantize"(%[[WEIGHT]]) : (tensor<2x3x3x4x!quant.uniform:f32:0, {2.000000e+02,3.000000e+03}>>) -> tensor<2x3x3x4xf32> +// CHECK: %[[CONV:.+]] = stablehlo.convolution(%[[ARG0]], %[[DQ]]) +// CHECK{LITERAL}: dim_numbers = [b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f], window = {pad = [[1, 1], [1, 1]]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64} +// CHECK-SAME: (tensor<1x3x3x4xf32>, tensor<2x3x3x4xf32>) -> tensor<1x3x3x2xf32> +// CHECK: return %[[CONV]] + +// ----- + +// Tests that a hybrid per-tensor quantized convolution for tfl.conv_2d is +// splitted into dequantize and float stablehlo.convolution. + +// CHECK-LABEL: func @convolution_hybrid_per_tensor +// CHECK-SAME: %[[ARG0:.+]]: tensor<1x3x3x4xf32> +func.func @convolution_hybrid_per_tensor(%arg0: tensor<1x3x3x4xf32>) -> tensor<1x3x3x2xf32> { + %0 = stablehlo.constant() {value = dense<3> : tensor<3x3x4x2xi8>} : () -> tensor<3x3x4x2x!quant.uniform> + %1 = stablehlo.convolution(%arg0, %0) dim_numbers = [b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f], window = {pad = [[1, 1], [1, 1]]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64} : (tensor<1x3x3x4xf32>, tensor<3x3x4x2x!quant.uniform>) -> tensor<1x3x3x2xf32> + return %1 : tensor<1x3x3x2xf32> +} + +// CHECK: %[[WEIGHT:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<2x3x3x4x!quant.uniform>, value = dense<3> : tensor<2x3x3x4xi8>}> +// CHECK: %[[DQ:.+]] = "tfl.dequantize"(%[[WEIGHT]]) : (tensor<2x3x3x4x!quant.uniform>) -> tensor<2x3x3x4xf32> +// CHECK: %[[CONV:.+]] = stablehlo.convolution(%[[ARG0]], %[[DQ]]) +// CHECK{LITERAL}: dim_numbers = [b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f], window = {pad = [[1, 1], [1, 1]]} {batch_group_count = 1 : i64, feature_group_count = 1 : i64} +// CHECK-SAME: (tensor<1x3x3x4xf32>, tensor<2x3x3x4xf32>) -> tensor<1x3x3x2xf32> +// CHECK: return %[[CONV]] + +// ----- + +// Tests that a hybrid per-channel quantized convolution for tfl.depthwise_conv +// is splitted into dequantize and float stablehlo.convolution. + +// CHECK-LABEL: func @depthwise_convolution_hybrid_per_channel +// CHECK-SAME: %[[ARG0:.+]]: tensor<1x3x3x4xf32> +func.func @depthwise_convolution_hybrid_per_channel(%arg0: tensor<1x3x3x4xf32>) -> tensor<1x3x3x4xf32> { + %0 = stablehlo.constant() {value = dense<3> : tensor<3x3x1x4xi8>} : () -> tensor<3x3x1x4x!quant.uniform> + %1 = stablehlo.convolution(%arg0, %0) dim_numbers = [b, 0, 1, f]x[0, 1, i, o]->[b, 0, 1, f], window = {pad = [[1, 1], [1, 1]]} {batch_group_count = 1 : i64, feature_group_count = 4 : i64} : (tensor<1x3x3x4xf32>, tensor<3x3x1x4x!quant.uniform>) -> tensor<1x3x3x4xf32> + return %1 : tensor<1x3x3x4xf32> +} + +// CHECK: %[[WEIGHT:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<1x3x3x4x!quant.uniform:f32:3, {2.000000e+02,3.000000e+03,2.000000e+02,3.000000e+03}>>, value = dense<3> : tensor<1x3x3x4xi8>}> +// CHECK: %[[DQ:.+]] = "tfl.dequantize"(%[[WEIGHT]]) : (tensor<1x3x3x4x!quant.uniform:f32:3, {2.000000e+02,3.000000e+03,2.000000e+02,3.000000e+03}>>) -> tensor<1x3x3x4xf32> +// CHECK: %[[CONV:.+]] = stablehlo.convolution(%[[ARG0]], %[[DQ]]) +// CHECK{LITERAL}: dim_numbers = [b, 0, 1, f]x[i, 0, 1, o]->[b, 0, 1, f], window = {pad = [[1, 1], [1, 1]]} {batch_group_count = 1 : i64, feature_group_count = 4 : i64} +// CHECK-SAME: (tensor<1x3x3x4xf32>, tensor<1x3x3x4xf32>) -> tensor<1x3x3x4xf32> +// CHECK: return %[[CONV]] diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/check_accepted_ops_pass.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/check_accepted_ops_pass.cc new file mode 100644 index 0000000000000000000000000000000000000000..ac99982553347bb8340fae393177b982737eebee --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/check_accepted_ops_pass.cc @@ -0,0 +1,77 @@ +/* Copyright 2021 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/compiler/mlir/lite/stablehlo/transforms/check_accepted_ops_pass.h" + +#include +#include +#include + +#include "mlir/IR/Dialect.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_util.h" + +namespace mlir { +namespace odml { + +// Define a pass class for checking if non-accepted dialect ops exist or not. +namespace { +class CheckAcceptedOpsPass + : public PassWrapper> { + public: + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(CheckAcceptedOpsPass) + + explicit CheckAcceptedOpsPass( + const std::vector &optional_accepted_dialects) + : accepted_dialects_(GetAcceptedStableHLODialects()), + optional_accepted_dialects_(optional_accepted_dialects) {} + + // Check if TF dialect ops exist over the module. + void runOnOperation() override; + + private: + std::vector accepted_dialects_; + std::vector optional_accepted_dialects_; +}; +} // namespace + +void CheckAcceptedOpsPass::runOnOperation() { + getOperation()->walk([&](Operation *op) { + auto dialect_name = op->getDialect()->getNamespace(); + auto op_name = op->getName().stripDialect(); + if (IsAcceptedOp(dialect_name, op_name, accepted_dialects_)) { + // If given op is in the `accepted_dialects_`, it's ok. + } else if (IsAcceptedOp(dialect_name, op_name, + optional_accepted_dialects_)) { + // If the given op is in the `optional_accepted_dialects_`, let's warn it. + op->emitWarning() << op->getName().getStringRef() << " op is temporarily " + << "accepted, but it should be removed in the end."; + } else { + // The other ops are not accepted. + return signalPassFailure(); + } + }); +} + +} // namespace odml +} // namespace mlir + +std::unique_ptr mlir::odml::createCheckAcceptedOpsPass( + const std::vector &optional_accepted_dialects) { + return std::make_unique( + optional_accepted_dialects); +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/check_accepted_ops_pass.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/check_accepted_ops_pass.h new file mode 100644 index 0000000000000000000000000000000000000000..89ab7179dac4b6e3a25215efee837f2927f2bf8d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/check_accepted_ops_pass.h @@ -0,0 +1,37 @@ +/* Copyright 2021 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_CHECK_DIALECTS_PASS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_CHECK_DIALECTS_PASS_H_ + +#include +#include +#include + +#include "mlir/Pass/Pass.h" // from @llvm-project + +namespace mlir { +namespace odml { + +// Creates a pass which checks if there exists allowed dialect ops only or not. +// Based on the list of dialect and op names, it signals failure or not. +// If some ops are in the `optional_accepted_dialects`, then it warns them. +std::unique_ptr createCheckAcceptedOpsPass( + const std::vector &optional_accepted_dialects = {}); + +} // namespace odml +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_CHECK_DIALECTS_PASS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/compose_uniform_quantized_type_pass.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/compose_uniform_quantized_type_pass.cc new file mode 100644 index 0000000000000000000000000000000000000000..d8478db55b5efad4e99af4880ca650816101c0c0 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/compose_uniform_quantized_type_pass.cc @@ -0,0 +1,1638 @@ +/* Copyright 2023 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 "absl/algorithm/container.h" +#include "llvm/ADT/Sequence.h" +#include "llvm/Support/Debug.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Dialect/Quant/IR/Quant.h" // from @llvm-project // NOLINT: Required to register quantization dialect. +#include "mlir/Dialect/Quant/IR/QuantTypes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/Location.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" // from @llvm-project +#include "stablehlo/dialect/StablehloOps.h" // from @stablehlo +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h" +#include "tensorflow/compiler/mlir/quantization/common/attrs_and_constraints.h" +#include "tensorflow/compiler/mlir/quantization/common/uniform_quantized_types.h" + +#define DEBUG_TYPE "stablehlo-compose-uniform-quantized-type" + +namespace mlir { +namespace odml { +namespace { + +using ::mlir::quant::CreateI8F32UniformQuantizedPerAxisType; +using ::mlir::quant::CreateI8F32UniformQuantizedType; +using ::mlir::quant::TryCast; +using ::mlir::quant::UniformQuantizedPerAxisType; +using ::mlir::quant::UniformQuantizedType; + +#define GEN_PASS_DEF_COMPOSEUNIFORMQUANTIZEDTYPEPASS +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h.inc" + +// These strings are used to identify the uniform_quantize / uniform_dequantize +// functions. +constexpr StringRef kUniformQuantizeFunctionNameSubstring = "uniform_quantize"; +constexpr StringRef kUniformDequantizeFunctionNameSubstring = + "uniform_dequantize"; + +class ComposeUniformQuantizedTypePass + : public impl::ComposeUniformQuantizedTypePassBase< + ComposeUniformQuantizedTypePass> { + private: + void runOnOperation() override; +}; + +// Tests whether a `stablehlo::ConvertOp` is a i8 -> f32 cast. +bool IsI8ToF32Cast(stablehlo::ConvertOp convert_op) { + const bool is_i8_operand = + convert_op.getOperand().getType().getElementType().isInteger(/*width=*/8); + const bool is_f32_result = + mlir::isa(convert_op.getResult().getType().getElementType()); + return is_i8_operand && is_f32_result; +} + +// Tests whether a `stablehlo::ConvertOp` is a i8 -> i32 cast. +bool IsI8ToI32Cast(stablehlo::ConvertOp convert_op) { + const bool is_i8_operand = + convert_op.getOperand().getType().getElementType().isInteger(/*width=*/8); + const bool is_i32_result = + convert_op.getResult().getType().getElementType().isInteger(/*width=*/32); + return is_i8_operand && is_i32_result; +} + +// Tests whether a `stablehlo::ConvertOp` is a i32 -> f32 cast. +bool IsI32ToF32Cast(stablehlo::ConvertOp convert_op) { + const bool is_i32_operand = + convert_op.getOperand().getType().getElementType().isInteger( + /*width=*/32); + const bool is_f32_result = + mlir::isa(convert_op.getResult().getType().getElementType()); + return is_i32_operand && is_f32_result; +} + +// Matches the zero points operand for the uniform_quantize and +// uniform_dequantize functions. Returns `failure()` if it doesn't match. +LogicalResult MatchZeroPointsOperand(Value zero_points) { + if (!zero_points) { + LLVM_DEBUG(llvm::dbgs() << "Zero point value is empty.\n"); + return failure(); + } + + auto zero_points_type = + mlir::dyn_cast_or_null(zero_points.getType()); + if (!zero_points_type) { + LLVM_DEBUG(llvm::dbgs() << "Zero point value should be a tensor type. Got: " + << zero_points_type << ".\n"); + return failure(); + } + + if (Type zero_points_element_type = zero_points_type.getElementType(); + !mlir::isa(zero_points_element_type)) { + LLVM_DEBUG(llvm::dbgs() << "Zero point should be an integer type. Got: " + << zero_points_element_type << ".\n"); + return failure(); + } + + if (zero_points_type.getNumElements() != 1) { + LLVM_DEBUG(llvm::dbgs() + << "Zero point should contain a single element. Has: " + << zero_points_type.getNumElements() << ".\n"); + return failure(); + } + + auto zero_point_constant_op = + dyn_cast_or_null(zero_points.getDefiningOp()); + if (!zero_point_constant_op) { + LLVM_DEBUG(llvm::dbgs() + << "Zero point should be a constant. Instead it is defined by: " + << zero_point_constant_op << ".\n"); + return failure(); + } + + return success(); +} + +// Matches the inverse scales operand for the uniform_quantize and +// uniform_dequantize functions. Returns `failure()` if it doesn't match. +LogicalResult MatchInverseScalesOperand(Value inverse_scales) { + if (!inverse_scales) { + LLVM_DEBUG(llvm::dbgs() << "Inverse scales value is empty.\n"); + return failure(); + } + + auto inverse_scales_type = + mlir::dyn_cast_or_null(inverse_scales.getType()); + if (!inverse_scales_type) { + LLVM_DEBUG(llvm::dbgs() << "Inverse scales should be a tensor type. Got: " + << inverse_scales_type << ".\n"); + return failure(); + } + + if (Type inverse_scales_element_type = inverse_scales_type.getElementType(); + !mlir::isa(inverse_scales_element_type)) { + LLVM_DEBUG(llvm::dbgs() + << "Inverse scales element should be a float type. Got: " + << inverse_scales_element_type << ".\n"); + return failure(); + } + + if (inverse_scales_type.getNumElements() != 1) { + LLVM_DEBUG(llvm::dbgs() + << "Inverse scales should contain only one element. Has: " + << inverse_scales_type.getNumElements() << ".\n"); + return failure(); + } + + auto inverse_scale_constant_op = + dyn_cast_or_null(inverse_scales.getDefiningOp()); + if (!inverse_scale_constant_op) { + llvm::dbgs() + << "Inverse scales should be a constant. Instead, it was defined by: " + << inverse_scales.getDefiningOp() << ".\n"; + return failure(); + } + + return success(); +} + +// Matches the following pattern that represents uniform quantization: +// +// `call @uniform_quantize(%input, %inverse_scale, %zero_point)` +// +// Provides helper functions to access the operands and the callee. +class UniformQuantizeFunctionCallPattern { + public: + // Returns Failure if it doesn't match. Returns the "wrapper" for the uniform + // dequantization function call pattern when matched. + static FailureOr Match( + func::CallOp call_op) { + if (!call_op.getCallee().contains(kUniformQuantizeFunctionNameSubstring)) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match @uniform_quantize function call pattern. " + "The name doesn't contain uniform_quantize.\n"); + return failure(); + } + + Value input_value = call_op.getOperand(0); + if (!input_value) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match @uniform_quantize function call pattern. " + "Input value is empty.\n"); + return failure(); + } + + auto input_value_type = + mlir::dyn_cast_or_null(input_value.getType()); + if (!input_value_type) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match @uniform_quantize function call pattern. " + "Input value's type must be a TensorType.\n"); + return failure(); + } + + if (Type input_element_type = input_value_type.getElementType(); + !mlir::isa(input_element_type)) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match @uniform_quantize function call pattern. " + "Input value's element type must be a float. Got: " + << input_element_type << ".\n"); + return failure(); + } + + if (failed(MatchInverseScalesOperand(call_op.getOperand(1)))) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match the inverse scales operand of " + "the @uniform_quantize call pattern.\n"); + return failure(); + } + + if (failed(MatchZeroPointsOperand(call_op.getOperand(2)))) { + LLVM_DEBUG(llvm::dbgs() << "Failed to match the zero point operand of " + "the @uniform_quantize call pattern.\n"); + return failure(); + } + + return UniformQuantizeFunctionCallPattern(call_op); + } + + Value GetInputValue() { return call_op_.getOperand(0); } + + Value GetInverseScalesValue() { return call_op_.getOperand(1); } + + Value GetZeroPointsValue() { return call_op_.getOperand(2); } + + stablehlo::ConstantOp GetZeroPointsConstantOp() { + return cast(GetZeroPointsValue().getDefiningOp()); + } + + stablehlo::ConstantOp GetInverseScalesConstantOp() { + return cast(GetInverseScalesValue().getDefiningOp()); + } + + ElementsAttr GetZeroPointsValueAttr() { + return GetZeroPointsConstantOp().getValue(); + } + + ElementsAttr GetInverseScalesValueAttr() { + return GetInverseScalesConstantOp().getValue(); + } + + func::CallOp GetCallOp() { return call_op_; } + + FlatSymbolRefAttr GetFunction() { return call_op_.getCalleeAttr(); } + + private: + explicit UniformQuantizeFunctionCallPattern(func::CallOp call_op) + : call_op_(call_op) {} + + func::CallOp call_op_; +}; + +// Matches the following pattern that represents uniform dequantization. +// +// `call @uniform_dequantize(%input, %zero_point, %inverse_scale)` +// +// Provides helper functions to access the operands and the callee. +class UniformDequantizeFunctionCallPattern { + public: + // Returns Failure if it doesn't match. Returns the "wrapper" for the uniform + // dequantization function call pattern when matched. + static FailureOr Match( + func::CallOp call_op) { + if (!call_op.getCallee().contains( + kUniformDequantizeFunctionNameSubstring)) { + llvm::dbgs() << "Failed to match uniformDequantizeCallOp - name doesn't " + "contain uniform_quantize.\n"; + return failure(); + } + + Value input_value = call_op.getOperand(0); + if (!input_value) { + LLVM_DEBUG(llvm::dbgs() << "Failed to match @uniform_dequantize call " + "pattern. Input value is empty.\n"); + return failure(); + } + + auto input_value_type = + mlir::dyn_cast_or_null(input_value.getType()); + if (!input_value_type) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match @uniform_dequantize call pattern. Input " + "value's type must be a TensorType. Got:" + << input_value_type << ".\n"); + return failure(); + } + + if (Type input_element_type = input_value_type.getElementType(); + !mlir::isa(input_element_type)) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match @uniform_dequantize call pattern. Input " + "value's element type must be integer. Got: " + << input_element_type << ".\n"); + return failure(); + } + + if (failed(MatchInverseScalesOperand(call_op.getOperand(1)))) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match the inverse scales operand of " + "the @uniform_dequantize call pattern.\n"); + return failure(); + } + + if (failed(MatchZeroPointsOperand(call_op.getOperand(2)))) { + LLVM_DEBUG(llvm::dbgs() << "Failed to match the zero point operand of " + "the @uniform_dequantize call pattern.\n"); + return failure(); + } + + return UniformDequantizeFunctionCallPattern(call_op); + } + + Value GetInputValue() { return call_op_.getOperand(0); } + + Value GetInverseScalesValue() { return call_op_.getOperand(1); } + + Value GetZeroPointsValue() { return call_op_.getOperand(2); } + + stablehlo::ConstantOp GetZeroPointsConstantOp() { + return cast(GetZeroPointsValue().getDefiningOp()); + } + + stablehlo::ConstantOp GetInverseScalesConstantOp() { + return cast(GetInverseScalesValue().getDefiningOp()); + } + + ElementsAttr GetZeroPointsValueAttr() { + return GetZeroPointsConstantOp().getValue(); + } + + ElementsAttr GetInverseScalesValueAttr() { + return GetInverseScalesConstantOp().getValue(); + } + + func::CallOp GetCallOp() { return call_op_; } + + FlatSymbolRefAttr GetFunction() { return call_op_.getCalleeAttr(); } + + private: + explicit UniformDequantizeFunctionCallPattern(func::CallOp call_op) + : call_op_(call_op) {} + + func::CallOp call_op_; +}; + +// Matches the pattern for quantized convolution op and rewrites it to use +// uniform quantized types. +// +// Currently assumes asymmetric per-tensor quantization for activations and +// symmetric per-channel quantization for filters. +// +// This pattern represents the following derived equation, where: +// * rn = real (expressed) value for tensor n +// * qn = quantized value for tensor n +// * sn = scale for tensor n +// * zn = zero point for tensor n +// +// r3 = r1 * r2 +// = s1 (q1 - z1) * s2 (q2 - z2) +// = s1 s2 (q1 q2 - q1 z2 - q2 z1 + z1 z2) +// +// * z2 is zero, because it assumes symmetric quantization for the filter: +// +// = s1 s2 (q1 q2 - q2 z1) +// +// In StableHLO text representation, the pattern is as the following +// (simplified): +// +// ``` +// %0 = // Input tensor r1. +// %1 = stablehlo.constant // Input inverse scale 1 / s1. +// %2 = stablehlo.constant // Input zero point z1. +// %3 = call @uniform_quantize(%0, %1, %2) // Quantize input (q1). +// %4 = stablehlo.convert %3 // i8 -> f32 cast trick for input. +// %5 = stablehlo.constant // Quantized filter tensor q2. +// %6 = stablehlo.convert %5 // Optional: i8 -> f32 cast trick for filter. +// %7 = stablehlo.convolution(%4, %6) // q1 * q2 (disguised in f32). +// %8 = stablehlo.reshape %2 // z1 +// %9 = stablehlo.broadcast_in_dim %8 +// %10 = stablehlo.convert %9 // i8 -> f32 cast trick for z1. +// %11 = stablehlo.convert %5 // i8 -> f32 cast trick for filter. +// %12 = stablehlo.convolution(%10, %11) // q2 * z1 +// %13 = stablehlo.subtract %7, %12 // q1 * q2 - q2 * z1 +// %14 = stablehlo.constant // Merged scale s1 * s2, precalculated. +// %15 = stablehlo.broadcast_in_dim %14 +// %16 = stablehlo.multiply %13 %15 // r3 = s1 s2 (q1 q2 - q2 z1) +// +// The following quant -> dequant pattern is a no-op, but is required to +// retrieve the quantization parameters for the output tensor. +// +// %17 = stablehlo.constant // Output inverse scale 1 / s3. +// %18 = stablehlo.constant // Output zero point z3. +// %19 = call @uniform_quantize_0(%16, %17, %18) // r3 -> q3 +// %20 = call @uniform_dequantize(%19, %17, %18) // q3 -> r3 +// ``` +// +// The rewritten pattern looks like: +// +// ``` +// %1 = stablehlo.uniform_quantize %0 // Input f32 -> uniform quantized type. +// %2 = stablehlo.constant // Filter uniform quantized type. +// %3 = stablehlo.convolution(%1, %2) // In uniform quantized type. +// %4 = stablehlo.uniform_dequantize %3 // Dequantize the output. +// ``` +class ComposeUniformQuantizedConvolutionOp + : public OpRewritePattern { + public: + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(stablehlo::ConvolutionOp op, + PatternRewriter& rewriter) const final { + if (match(op).failed()) { + return failure(); + } + rewrite(op, rewriter); + return success(); + } + + private: + LogicalResult match(stablehlo::ConvolutionOp op) const { + // Verify operands' types. + for (Type operand_type : op.getOperandTypes()) { + if (Type element_type = + mlir::cast(operand_type).getElementType(); + !mlir::isa(element_type)) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match. The operand type must be a float. Got: " + << element_type << ".\n"); + return failure(); + } + } + + // Match the subgraph for the input operand. + auto input_i8_to_f32_convert_op = dyn_cast_or_null( + op.getOperand(0).getDefiningOp()); + if (!input_i8_to_f32_convert_op) { + LLVM_DEBUG(llvm::dbgs() << "Failed to match. Input is not defined by a " + "stablehlo::ConvertOp.\n"); + return failure(); + } + + if (!IsI8ToF32Cast(input_i8_to_f32_convert_op)) { + LLVM_DEBUG( + llvm::dbgs() + << "Failed to match. The ConvertOp is not an i8->f32 type cast.\n"); + return failure(); + } + + auto uniform_quantize_call_op = dyn_cast_or_null( + input_i8_to_f32_convert_op.getOperand().getDefiningOp()); + if (!uniform_quantize_call_op) { + LLVM_DEBUG(llvm::dbgs() << "Failed to match: Input is not quantized by a " + "uniform_quantize function.\n"); + return failure(); + } + + auto uniform_quantize_call_pattern_for_input = + UniformQuantizeFunctionCallPattern::Match(uniform_quantize_call_op); + if (failed(uniform_quantize_call_pattern_for_input)) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match uniform_quantized call op for input.\n"); + return failure(); + } + + // Match the subgraph that receives the convolution output. + Value conv_output_value = op.getResult(); + if (auto output_element_type = + mlir::cast(conv_output_value.getType()) + .getElementType(); + !mlir::isa(output_element_type)) { + LLVM_DEBUG( + llvm::dbgs() + << "Failed to match. Output type is expected to be a float. Got: " + << output_element_type << ".\n"); + return failure(); + } + + if (!op->hasOneUse()) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match op - doesn't have a single use.\n"); + return failure(); + } + + auto subtract_op = dyn_cast_or_null( + *conv_output_value.user_begin()); + if (!subtract_op) { + LLVM_DEBUG(llvm::dbgs() << "Failed to match subtract_op.\n"); + return failure(); + } + if (!subtract_op->hasOneUse()) { + llvm::dbgs() << "Failed to match op - doesn't have a single use.\n"; + return failure(); + } + + // This convolution represents the "q2 * z1" term, which is subtraced from + // the first convolution result. + auto other_conv_op = dyn_cast_or_null( + subtract_op.getOperand(1).getDefiningOp()); + if (!other_conv_op) { + LLVM_DEBUG(llvm::dbgs() << "Failed to match other_conv_op.\n"); + return failure(); + } + + // The filter of the convolution may have a `ConvertOp` after the constant + // op. + if (!isa( + other_conv_op.getOperand(1).getDefiningOp()) && + !isa( + other_conv_op.getOperand(1).getDefiningOp())) { + LLVM_DEBUG(llvm::dbgs() << "Failed to match filter of other_conv_op.\n"); + return failure(); + } + + auto other_zp_i8_to_f32_convert_op = dyn_cast_or_null( + other_conv_op.getOperand(0).getDefiningOp()); + if (!other_zp_i8_to_f32_convert_op) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match other_zp_i8_to_f32_convert_op.\n"); + return failure(); + } + + if (!(mlir::isa(other_zp_i8_to_f32_convert_op.getResult() + .getType() + .getElementType()) && + mlir::isa(other_zp_i8_to_f32_convert_op.getOperand() + .getType() + .getElementType()))) { + LLVM_DEBUG( + llvm::dbgs() + << "Failed to match. The ConvertOp is not an i8->f32 type cast.\n"); + return failure(); + } + + auto other_zp_broadcast_in_dim_op = + dyn_cast_or_null( + other_zp_i8_to_f32_convert_op.getOperand().getDefiningOp()); + if (!other_zp_broadcast_in_dim_op) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match other_zp_broadcast_in_dim_op.\n"); + return failure(); + } + + auto other_zp_reshape_op = dyn_cast_or_null( + other_zp_broadcast_in_dim_op.getOperand().getDefiningOp()); + if (!other_zp_reshape_op) { + LLVM_DEBUG(llvm::dbgs() << "Failed to match other_zp_reshape_op.\n"); + return failure(); + } + + auto other_input_zero_points_constant_op = + dyn_cast_or_null( + other_zp_reshape_op.getOperand().getDefiningOp()); + if (!other_input_zero_points_constant_op) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match other_input_zero_points_constant_op.\n"); + return failure(); + } + + auto combined_scale_multiply_op = dyn_cast_or_null( + *subtract_op.getResult().user_begin()); + if (!combined_scale_multiply_op) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match combined_scale_multiply_op.\n"); + return failure(); + } + if (!combined_scale_multiply_op->hasOneUse()) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match op - doesn't have a single use.\n"); + return failure(); + } + + auto scale_combined_broadcast_in_dim_op = + dyn_cast_or_null( + combined_scale_multiply_op.getOperand(1).getDefiningOp()); + if (!scale_combined_broadcast_in_dim_op) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match scale_combined_broadcast_in_dim_op.\n"); + return failure(); + } + + // s1 * s2 + auto combined_scale_constant_op = dyn_cast_or_null( + scale_combined_broadcast_in_dim_op.getOperand().getDefiningOp()); + if (!combined_scale_constant_op) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match combined_scale_constant_op.\n"); + return failure(); + } + + // Quantize -> Dequantize following r3. + auto output_uniform_quantize_call_op = dyn_cast_or_null( + *combined_scale_multiply_op.getResult().user_begin()); + if (!output_uniform_quantize_call_op->hasOneUse()) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match op - doesn't have a single use.\n"); + return failure(); + } + + if (failed(UniformQuantizeFunctionCallPattern::Match( + output_uniform_quantize_call_op))) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match op output_uniform_quantize_call_op\n"); + return failure(); + } + + auto output_uniform_dequantize_call_op = dyn_cast_or_null( + *output_uniform_quantize_call_op.getResult(0).user_begin()); + if (!output_uniform_dequantize_call_op) { + return failure(); + } + if (failed(UniformDequantizeFunctionCallPattern::Match( + output_uniform_dequantize_call_op))) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match output_uniform_dequantize_call_op.\n"); + return failure(); + } + + // The filter of the convolution may have a `ConvertOp` after the constant + // op. + Operation* filter_op = op.getOperand(1).getDefiningOp(); + if (!isa(filter_op) && + !(isa(filter_op) && + isa( + filter_op->getOperand(0).getDefiningOp()))) { + LLVM_DEBUG(llvm::dbgs() << "Failed to match filter_constant_op.\n"); + return failure(); + } + + return success(); + } + + void rewrite(stablehlo::ConvolutionOp op, PatternRewriter& rewriter) const { + // Rewrite `call @uniform_quantize` -> `stablehlo.uniform_quantize`. + auto input_i8_to_f32_convert_op = + cast(op.getOperand(0).getDefiningOp()); + auto uniform_quantize_call_op = cast( + input_i8_to_f32_convert_op.getOperand().getDefiningOp()); + + auto uniform_quantize_call_pattern_for_input = + *UniformQuantizeFunctionCallPattern::Match(uniform_quantize_call_op); + const double input_inverse_scales_value = + uniform_quantize_call_pattern_for_input.GetInverseScalesValueAttr() + .getSplatValue() + .convertToFloat(); + const double input_scale_value = 1.0 / input_inverse_scales_value; + const int64_t input_zero_point_value = + uniform_quantize_call_pattern_for_input.GetZeroPointsValueAttr() + .getSplatValue() + .getSExtValue(); + + Value input_value = uniform_quantize_call_pattern_for_input.GetInputValue(); + UniformQuantizedType input_quantized_element_type = + CreateI8F32UniformQuantizedType( + uniform_quantize_call_op.getLoc(), *rewriter.getContext(), + input_scale_value, input_zero_point_value); + auto input_uniform_quantize_op = stablehlo::UniformQuantizeOp::create( + rewriter, uniform_quantize_call_op.getLoc(), + /*result=*/ + mlir::cast(input_value.getType()) + .clone(input_quantized_element_type), + /*operand=*/input_value); + + rewriter.replaceAllUsesWith(input_i8_to_f32_convert_op.getResult(), + input_uniform_quantize_op.getResult()); + + // Rewrite filter constant. + Operation* filter_op = op.getOperand(1).getDefiningOp(); + + // Retrieve the i8 filter values. + DenseElementsAttr filter_i8_value_attr = nullptr; + if (auto filter_constant_op = + dyn_cast_or_null(filter_op); + filter_constant_op) { + // This is i8 values disguised as f32 (due to the upcast trick). Simply + // cast them to i8. + ElementsAttr filter_value = filter_constant_op.getValue(); + filter_i8_value_attr = + mlir::cast(filter_value) + .mapValues(rewriter.getI8Type(), [](const APFloat& val) -> APInt { + APSInt convertedInt(/*BitWidth=*/8, /*isUnsigned=*/false); + bool ignored; + val.convertToInteger(convertedInt, APFloat::rmTowardZero, + &ignored); + return convertedInt; + }); + } else if (isa(filter_op) && + isa( + filter_op->getOperand(0).getDefiningOp())) { + filter_i8_value_attr = mlir::cast( + cast(filter_op->getOperand(0).getDefiningOp()) + .getValue()); + } + + // Create Uniform Quantized constant for the filter. + auto subtract_op = + cast(*op.getResult().user_begin()); + auto other_conv_op = cast( + subtract_op.getOperand(1).getDefiningOp()); + auto combined_scale_multiply_op = + cast(*subtract_op.getResult().user_begin()); + + auto scale_combined_broadcast_in_dim_op = cast( + combined_scale_multiply_op.getOperand(1).getDefiningOp()); + auto combined_scale_constant_op = cast( + scale_combined_broadcast_in_dim_op.getOperand().getDefiningOp()); + + SmallVector filter_scale_values; + for (const auto combined_scale_value : + mlir::cast(combined_scale_constant_op.getValue()) + .getValues()) { + // UniformQuantizedPerAxisType requires scales to have double dtype. + const double filter_scale_value = static_cast( + combined_scale_value * input_inverse_scales_value); + filter_scale_values.emplace_back(filter_scale_value); + } + + // Assumes it is symmetric. + SmallVector filter_zero_point_values( + /*Size=*/filter_scale_values.size(), /*Value=*/0); + + // Use quantization dimension = 3 that corresponds to the output channel + // dimension, assuming the filter format is `[0, 1, i, o]`. + // TODO: b/291029962 - Lift the assumption above and retrieve the + // quantization dimension from the `dimension_numbers` attribute. + UniformQuantizedPerAxisType filter_quantized_element_type = + CreateI8F32UniformQuantizedPerAxisType( + filter_op->getLoc(), *rewriter.getContext(), filter_scale_values, + filter_zero_point_values, + /*quantization_dimension=*/3); + + // Create a new constant op for the filter in i8. + auto quantized_filter_constant_op = stablehlo::ConstantOp::create( + rewriter, filter_op->getLoc(), + /*output=*/ + filter_i8_value_attr.getType().clone(filter_quantized_element_type), + /*value=*/filter_i8_value_attr); + + // Replace filter uses with uniform quantized filter. + rewriter.replaceAllUsesWith(filter_op->getResult(0), + quantized_filter_constant_op.getResult()); + + // Replace conv op with a new convolution op that has quantized output type. + // Quantize -> Dequantize following r3. + auto output_uniform_quantize_call_op = cast( + *combined_scale_multiply_op.getResult().user_begin()); + + auto output_uniform_quantize_call_pattern = + *UniformQuantizeFunctionCallPattern::Match( + output_uniform_quantize_call_op); + + const int output_zero_point_value = + output_uniform_quantize_call_pattern.GetZeroPointsValueAttr() + .getSplatValue() + .getSExtValue(); + const double output_inverse_scale_value = + output_uniform_quantize_call_pattern.GetInverseScalesValueAttr() + .getSplatValue() + .convertToFloat(); + + UniformQuantizedType output_uniform_quantized_type = + CreateI8F32UniformQuantizedType( + output_uniform_quantize_call_op.getLoc(), *rewriter.getContext(), + /*scale=*/1.0 / output_inverse_scale_value, + output_zero_point_value); + + Value conv_output_value = op.getResult(); + auto output_uniform_quantized_tensor_type = RankedTensorType::getChecked( + rewriter.getUnknownLoc(), + /*shape=*/ + mlir::cast(conv_output_value.getType()).getShape(), + output_uniform_quantized_type); + + SmallVector new_conv_output_types = { + output_uniform_quantized_tensor_type}; + auto new_conv_op_with_output_type = stablehlo::ConvolutionOp::create( + rewriter, op.getLoc(), new_conv_output_types, op.getOperands(), + op->getAttrs()); + + rewriter.replaceAllUsesWith(op.getResult(), + new_conv_op_with_output_type.getResult()); + + auto new_output_dequant_op = stablehlo::UniformDequantizeOp::create( + rewriter, rewriter.getUnknownLoc(), + /*operand=*/new_conv_op_with_output_type); + + auto output_uniform_dequantize_call_op = cast( + *output_uniform_quantize_call_op.getResult(0).user_begin()); + + rewriter.replaceAllUsesWith(output_uniform_dequantize_call_op.getResult(0), + new_output_dequant_op.getResult()); + + // Erase unused ops in the reverse order. + rewriter.eraseOp(output_uniform_dequantize_call_op); + rewriter.eraseOp(output_uniform_quantize_call_op); + rewriter.eraseOp(combined_scale_multiply_op); + rewriter.eraseOp(subtract_op); + rewriter.eraseOp(other_conv_op); + rewriter.eraseOp(op); + rewriter.eraseOp(input_i8_to_f32_convert_op); + rewriter.eraseOp(uniform_quantize_call_op); + } +}; + +// Matches the pattern for quantized dot_general op and rewrites it to use +// uniform quantized types. +// +// Currently assumes asymmetric per-tensor quantization for activations and +// symmetric per-channel quantization for filters. +// +// This pattern represents the following derived equation, where: +// * rn = real (expressed) value for tensor n +// * qn = quantized value for tensor n +// * sn = scale for tensor n +// * zn = zero point for tensor n +// +// r3 = r1 * r2 +// = s1 (q1 - z1) * s2 (q2 - z2) +// = s1 s2 (q1 q2 - q1 z2 - q2 z1 + z1 z2) +// +// * z2 is zero, because it assumes symmetric quantization for the filter: +// +// = s1 s2 (q1 q2 - q2 z1) +// +// In StableHLO text representation, the pattern is as the following +// (simplified): +// +// ``` +// %0 = // Input tensor r1. +// %1 = stablehlo.constant // Input inverse scale 1 / s1. +// %2 = stablehlo.constant // Input zero point z1. +// %3 = stablehlo.constant // Quantized filter tensor q2. +// %4 = stablehlo.constant // Precalculated q2 * z1 +// %5 = stablehlo.constant // Merged scale s1 * s2, precalculated. +// %6 = call @uniform_quantize(%0, %1, %2) // Quantize input (q1). +// %7 = stablehlo.convert %6 // i8 -> f32 cast trick for input. +// %8 = stablehlo.convert %3 // i8 -> f32 cast trick for filter, optional. +// %9 = stablehlo.dot_general(%7, %8) // q1 * q2 (disguised in f32). +// %10 = stablehlo.convert %4 // i32 -> f32 cast for q2 * z1. +// %11 = stablehlo.broadcast_in_dim %10 // Optional. +// %12 = stablehlo.subtract %9, %11 // q1 * q2 - q2 * z1 +// %13 = stablehlo.broadcast_in_dim %5 // Optional. +// %14 = stablehlo.multiply %12 %13 // r3 = s1 s2 (q1 q2 - q2 z1) +// +// The following quant -> dequant pattern is a no-op, but is required to +// retrieve the quantization parameters for the output tensor. +// +// %15 = stablehlo.constant // Output inverse scale 1 / s3. +// %16 = stablehlo.constant // Output zero point z3. +// %17 = call @uniform_quantize_0(%15, %15, %16) // r3 -> q3 +// %18 = call @uniform_dequantize(%17, %15, %16) // q3 -> r3 +// ``` +// +// The rewritten pattern looks like: +// +// ``` +// %1 = stablehlo.uniform_quantize %0 // Input f32 -> uniform quantized type. +// %2 = stablehlo.constant // Filter uniform quantized type. +// %3 = stablehlo.dot_general(%1, %2) // In uniform quantized type. +// %4 = stablehlo.uniform_dequantize %3 // Dequantize the output. +// ``` +// +// Note that the i8->f32 cast trick for the filter (%8) is optional. When the +// cast isn't present, the filter constant (%3) should be i8 quantized values +// disguised in f32. +class ComposeUniformQuantizedDotGeneralOp + : public OpRewritePattern { + public: + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(stablehlo::DotGeneralOp op, + PatternRewriter& rewriter) const final { + if (match(op).failed()) { + return failure(); + } + rewrite(op, rewriter); + return success(); + } + + private: + LogicalResult match(stablehlo::DotGeneralOp op) const { + auto input_i8_to_f32_convert_op = + TryCast(op.getOperand(0).getDefiningOp(), + /*name=*/"input_i8_to_f32_convert_op"); + if (failed(input_i8_to_f32_convert_op)) return failure(); + + if (!IsI8ToF32Cast(*input_i8_to_f32_convert_op)) { + LLVM_DEBUG(llvm::dbgs() << "Failed to match input_i8_to_f32_convert_op. " + "It should be a i8->f32 cast.\n"); + return failure(); + } + + if (failed(MatchFilter(op.getOperand(1)))) return failure(); + + auto input_quantize_call_op = TryCast( + input_i8_to_f32_convert_op->getOperand().getDefiningOp(), + /*name=*/"input_quantize_call_op"); + if (failed(input_quantize_call_op)) return failure(); + + // Uniform quantization pattern found for the input DotGeneralOp input. + auto input_uniform_quantize_call_pattern = + UniformQuantizeFunctionCallPattern::Match(*input_quantize_call_op); + if (failed(input_uniform_quantize_call_pattern)) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match input uniform quantize call pattern.\n"); + return failure(); + } + + auto subtract_op = TryCast( + *op.getResult().user_begin(), /*name=*/"subtract_op"); + if (failed(subtract_op)) return failure(); + + Value subtract_op_second_operand = subtract_op->getOperand(1); + if (auto broadcast_in_dim_op = TryCast( + subtract_op_second_operand.getDefiningOp(), + /*name=*/"broadcast_in_dim_for_subtract_op_operand"); + succeeded(broadcast_in_dim_op)) { + subtract_op_second_operand = broadcast_in_dim_op->getOperand(); + } + + auto input_zp_filter_prod_convert_op = TryCast( + subtract_op_second_operand.getDefiningOp(), + /*name=*/"input_zp_filter_prod_convert_op"); + if (failed(input_zp_filter_prod_convert_op)) return failure(); + + if (!IsI32ToF32Cast(*input_zp_filter_prod_convert_op)) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match input_zp_filter_prod_convert_op. " + "It should be a i32->f32 cast.\n"); + return failure(); + } + + // z1 * q2 + auto input_zp_filter_prod_constant_op = TryCast( + input_zp_filter_prod_convert_op->getOperand().getDefiningOp(), + /*name=*/"input_zp_filter_prod_constant_op"); + if (failed(input_zp_filter_prod_constant_op)) return failure(); + + auto multiply_op = TryCast( + *subtract_op->getResult().user_begin(), /*name=*/"multiply_op"); + if (failed(multiply_op)) return failure(); + + Value multiply_op_second_operand = multiply_op->getOperand(1); + if (auto broadcast_in_dim_op = TryCast( + multiply_op_second_operand.getDefiningOp(), + /*name=*/"broadcast_in_dim_for_multiply_op_operand"); + succeeded(broadcast_in_dim_op)) { + multiply_op_second_operand = broadcast_in_dim_op->getOperand(); + } + + auto merged_scale_constant_op = TryCast( + multiply_op_second_operand.getDefiningOp(), + /*name=*/"merged_scale_constant_op"); + if (failed(merged_scale_constant_op)) return failure(); + + auto output_uniform_quantize_call_op = + TryCast(*multiply_op->getResult().user_begin(), + /*name=*/"output_quantize_call_op"); + if (failed(output_uniform_quantize_call_op)) return failure(); + + auto output_uniform_quantize_call_pattern = + UniformQuantizeFunctionCallPattern::Match( + *output_uniform_quantize_call_op); + if (failed(output_uniform_quantize_call_pattern)) { + llvm::dbgs() << "Failed match uniform quantize call pattern.\n"; + return failure(); + } + + auto output_uniform_dequantize_call_op = TryCast( + *output_uniform_quantize_call_op->getResult(0).user_begin(), + /*name=*/"output_uniform_dequantize_call_op"); + if (failed(output_uniform_dequantize_call_op)) return failure(); + + auto output_uniform_dequantize_call_pattern = + UniformDequantizeFunctionCallPattern::Match( + *output_uniform_dequantize_call_op); + if (failed(output_uniform_dequantize_call_pattern)) { + llvm::dbgs() << "Failed to match output uniform quantize call pattern.\n"; + return failure(); + } + + return success(); + } + + void rewrite(stablehlo::DotGeneralOp op, PatternRewriter& rewriter) const { + // Build uniform quantized type for input. + auto input_i8_to_f32_convert_op = + cast(op.getOperand(0).getDefiningOp()); + auto input_uniform_quantize_call_op = cast( + input_i8_to_f32_convert_op.getOperand().getDefiningOp()); + auto input_uniform_quantize_call_pattern = + UniformQuantizeFunctionCallPattern::Match( + input_uniform_quantize_call_op); + + const float input_inverse_scale_value = + input_uniform_quantize_call_pattern->GetInverseScalesValueAttr() + .getSplatValue() + .convertToFloat(); + const float input_scale_value = 1.0 / input_inverse_scale_value; + + const int8_t input_zero_point_value = + input_uniform_quantize_call_pattern->GetZeroPointsValueAttr() + .getSplatValue() + .getSExtValue(); + + const UniformQuantizedType input_uniform_quantized_type = + CreateI8F32UniformQuantizedType( + input_uniform_quantize_call_op.getLoc(), *rewriter.getContext(), + input_scale_value, input_zero_point_value); + + Value input_value = input_uniform_quantize_call_pattern->GetInputValue(); + auto input_uniform_quantize_op = stablehlo::UniformQuantizeOp::create( + rewriter, input_i8_to_f32_convert_op.getLoc(), + /*result=*/ + mlir::cast(input_value.getType()) + .clone(input_uniform_quantized_type), + /*operand=*/input_value); + + rewriter.replaceAllUsesWith(input_i8_to_f32_convert_op.getResult(), + input_uniform_quantize_op.getResult()); + + // Build uniform quantized type for filter. + Value filter_value = op.getOperand(1); + stablehlo::ConstantOp filter_constant_op = + GetFilterConstantOp(filter_value); + auto filter_value_attr = + mlir::cast(filter_constant_op.getValue()); + if (filter_value_attr.getElementType().isF32()) { + // This is i8 values disguised as f32 (due to the upcast trick). Simply + // cast them to i8. + filter_value_attr = + mlir::cast(filter_value_attr) + .mapValues(rewriter.getI8Type(), [](const APFloat& val) -> APInt { + APSInt converted_int(/*BitWidth=*/8, /*isUnsigned=*/false); + bool ignored; + val.convertToInteger(converted_int, APFloat::rmTowardZero, + &ignored); + return converted_int; + }); + } + + auto subtract_op = + cast(*op.getResult().user_begin()); + + Value subtract_op_second_operand = subtract_op.getOperand(1); + if (auto broadcast_in_dim_op = + dyn_cast_or_null( + subtract_op_second_operand.getDefiningOp()); + broadcast_in_dim_op) { + // Ignore BroadcastInDimOp - it is optional. + subtract_op_second_operand = broadcast_in_dim_op.getOperand(); + } + + auto multiply_op = + cast(*subtract_op.getResult().user_begin()); + + Value multiply_op_second_operand = multiply_op.getOperand(1); + if (auto broadcast_in_dim_op = + dyn_cast_or_null( + multiply_op_second_operand.getDefiningOp()); + broadcast_in_dim_op) { + // Ignore BroadcastInDimOp - it is optional. + multiply_op_second_operand = broadcast_in_dim_op.getOperand(); + } + + // s1 * s2 + auto merged_scale_constant_op = + cast(multiply_op_second_operand.getDefiningOp()); + SmallVector filter_scale_values; + for (const auto merged_scale : + mlir::cast(merged_scale_constant_op.getValue()) + .getValues()) { + // (s1 * s2) * (1 / s1) = s2 + // UniformQuantizedPerAxisType requires scales to have double dtype. + filter_scale_values.push_back( + static_cast(merged_scale * input_inverse_scale_value)); + } + + SmallVector filter_zero_point_values( + /*Size=*/filter_scale_values.size(), /*Value=*/0); + + const int quantization_dimension = GetFilterQuantizationDimension( + op.getDotDimensionNumbers(), + mlir::cast(filter_value_attr.getType()).getRank()); + const UniformQuantizedPerAxisType filter_uniform_quantized_type = + CreateI8F32UniformQuantizedPerAxisType( + filter_constant_op.getLoc(), *rewriter.getContext(), + filter_scale_values, filter_zero_point_values, + quantization_dimension); + + // Create a new constant op for the filter in i8. + auto quantized_filter_constant_op = stablehlo::ConstantOp::create( + rewriter, filter_constant_op.getLoc(), + /*output=*/ + mlir::cast(filter_constant_op.getResult().getType()) + .clone(filter_uniform_quantized_type), + /*value=*/filter_value_attr); + + rewriter.replaceAllUsesWith(filter_value, + quantized_filter_constant_op.getResult()); + + // Recreate stablehlo::DotGeneralOp with a uniform quantized output type. + auto output_uniform_quantize_call_op = + cast(*multiply_op.getResult().user_begin()); + + auto output_uniform_quantize_call_pattern = + UniformQuantizeFunctionCallPattern::Match( + output_uniform_quantize_call_op); + + auto output_uniform_dequantize_call_op = cast( + *output_uniform_quantize_call_op.getResult(0).user_begin()); + + auto output_uniform_dequantize_call_pattern = + UniformDequantizeFunctionCallPattern::Match( + output_uniform_dequantize_call_op); + + const auto inverse_output_scale_value = + output_uniform_quantize_call_pattern->GetInverseScalesValueAttr() + .getSplatValue() + .convertToFloat(); + const float output_scale_value = 1.0 / inverse_output_scale_value; + + const int64_t output_zero_point_value = + output_uniform_quantize_call_pattern->GetZeroPointsValueAttr() + .getSplatValue() + .getSExtValue(); + + const UniformQuantizedType output_uniform_quantized_type = + CreateI8F32UniformQuantizedType( + output_uniform_quantize_call_op.getLoc(), *rewriter.getContext(), + output_scale_value, output_zero_point_value); + + auto new_dot_general_op = stablehlo::DotGeneralOp::create( + rewriter, op.getLoc(), /*resultType0=*/ + mlir::cast(op.getResult().getType()) + .clone(output_uniform_quantized_type), + /*lhs=*/op.getLhs(), /*rhs=*/op.getRhs(), + /*dot_dimension_numbers=*/op.getDotDimensionNumbers(), + /*precision_config=*/op.getPrecisionConfigAttr(), + /*algorithm=*/op.getAlgorithmAttr()); + + rewriter.replaceAllUsesWith(op.getResult(), new_dot_general_op.getResult()); + + auto new_output_dequant_op = stablehlo::UniformDequantizeOp::create( + rewriter, output_uniform_dequantize_call_op.getLoc(), + /*operand=*/new_dot_general_op); + + rewriter.replaceAllUsesWith(output_uniform_dequantize_call_op.getResult(0), + new_output_dequant_op.getResult()); + + // Erase unused ops after the transformation. + rewriter.eraseOp(output_uniform_dequantize_call_pattern->GetCallOp()); + rewriter.eraseOp(output_uniform_quantize_call_pattern->GetCallOp()); + rewriter.eraseOp(multiply_op); + rewriter.eraseOp(subtract_op); + rewriter.eraseOp(input_i8_to_f32_convert_op); + rewriter.eraseOp(input_uniform_quantize_call_pattern->GetCallOp()); + } + + // Quantization dimension corresponds to the output feature dimension, i.e. + // not contracted nor batched. + int64_t GetFilterQuantizationDimension( + const stablehlo::DotDimensionNumbersAttr& dot_dimension_numbers, + const int64_t filter_rank) const { + // Register all dimensions as candidates. + auto seq_range = llvm::seq(int64_t{0}, filter_rank); + SmallVector quantization_dimension_candidates(seq_range.begin(), + seq_range.end()); + + // Erase all contracting dimensions from the candidates. + for (const int64_t contracting_dim : + dot_dimension_numbers.getRhsContractingDimensions()) { + auto contracting_dim_itr = + absl::c_find(quantization_dimension_candidates, contracting_dim); + quantization_dimension_candidates.erase(contracting_dim_itr); + } + + // Erase all batching dimensions from the candidates. + for (const int64_t batching_dim : + dot_dimension_numbers.getRhsBatchingDimensions()) { + auto batching_dim_itr = + absl::c_find(quantization_dimension_candidates, batching_dim); + quantization_dimension_candidates.erase(batching_dim_itr); + } + + return quantization_dimension_candidates[0]; + } + + private: + // Returns the filter constant op. The resulting constant's element type is + // either i8 (when i8->f32 cast is present) or f32. + stablehlo::ConstantOp GetFilterConstantOp(Value filter_value) const { + Operation* filter_op = filter_value.getDefiningOp(); + + auto f32_filter_constant_op = dyn_cast(filter_op); + if (f32_filter_constant_op) { + return f32_filter_constant_op; + } else { + // Build uniform quantized type for filter. + auto filter_i8_to_f32_convert_op = cast(filter_op); + + return cast( + filter_i8_to_f32_convert_op.getOperand().getDefiningOp()); + } + } + + LogicalResult MatchFilter(Value filter_value) const { + auto filter_constant_op = TryCast( + filter_value.getDefiningOp(), /*name=*/"float_filter_constant_op"); + if (succeeded(filter_constant_op) && + filter_constant_op->getResult().getType().getElementType().isF32()) { + return success(); + } + + auto filter_i8_to_f32_convert_op = + TryCast(filter_value.getDefiningOp(), + /*name=*/"filter_i8_to_f32_convert_op"); + if (failed(filter_i8_to_f32_convert_op)) return failure(); + + if (!IsI8ToF32Cast(*filter_i8_to_f32_convert_op)) { + LLVM_DEBUG(llvm::dbgs() << "Failed to match filter_i8_to_f32_convert_op. " + "It should be a i8->f32 cast.\n"); + return failure(); + } + + return TryCast( + filter_i8_to_f32_convert_op->getOperand().getDefiningOp(), + /*name=*/"filter_constant_op"); + } +}; + +// Matches the pattern for quantized dot_general op and rewrites it to use +// uniform quantized types when both operands are activations. +// +// Currently assumes asymmetric per-tensor quantization for both activations. +// +// This pattern represents the following derived equation, where: +// * rn = real (expressed) value for tensor n +// * qn = quantized value for tensor n +// * sn = scale for tensor n +// * zn = zero point for tensor n +// +// r3 = r1 * r2 +// = s1 (q1 - z1) * s2 (q2 - z2) +// = s1 s2 (q1 - z1) * (q2 - z2) +// +// Unlike `ComposeUniformQuantizedDotGeneralOp`, the pattern assumes that the +// term "(q1 - z1) * (q2 - z2)" is not expanded. This is done to reduce +// unnecessary op count. Also, the subtractions "(q - z)" are performed in i32 +// to avoid underflows. +// +// In StableHLO text representation, the pattern is as the following +// (simplified): +// +// ``` +// %0 = // Input tensor r1. +// %1 = // Input tensor r2. +// %2 = stablehlo.constant // Input 1 inverse scale 1 / s1. +// %3 = stablehlo.constant // Input 1 zero point z1 (i8). +// %4 = stablehlo.constant // Input 1 zero point z1 (i32). +// %5 = stablehlo.constant // Input 2 inverse scale 1 / s2. +// %6 = stablehlo.constant // Input 2 zero point z2 (i8). +// %7 = stablehlo.constant // Input 2 zero point z2 (i32). +// %8 = stablehlo.constant // Input 3 inverse scale 1 / s3. +// %9 = stablehlo.constant // Input 3 zero point z3. +// %10 = stablehlo.constant // s1 * s2. +// %11 = call @uniform_quantize(%0, %2, %3) // Quantize input (q1). +// %12 = call @uniform_quantize_0(%1, %5, %6) // Quantize input (q2). +// %13 = stablehlo.convert %11 // i8->i32 cast for q1. +// %14 = stablehlo.convert %3 // [Optional] i8->i32 cast for z1. +// %15 = stablehlo.broadcast_in_dim %14 // Operand = %4 if no `convert` above. +// %16 = stablehlo.subtract %13, %15 // q1 - z1 +// %17 = stablehlo.convert %12 // i8->i32 cast for q2. +// %18 = stablehlo.convert %6 // [Optional] i8->i32 cast for z2. +// %19 = stablehlo.broadcast_in_dim %18 // Operand = %7 if no `convert` above. +// %20 = stablehlo.subtract %17, %19 // q2 - z2 +// %21 = stablehlo.dot_general(%16, %20) // (q1 - z1) * (q2 - z2). +// %22 = stablehlo.convert %21 // i32 -> f32 cast. +// %23 = stablehlo.broadcast_in_dim %10 +// %24 = stablehlo.multiply %22 %23 // * s1 s2 +// +// The following quant -> dequant pattern is a no-op, but is required to +// retrieve the quantization parameters for the output tensor. +// +// %25 = call @uniform_quantize_1(%24, %8, %9) // r3 -> q3 +// %26 = call @uniform_dequantize(%25, %8, %9) // q3 -> r3 +// ``` +// +// The rewritten pattern looks like: +// +// ``` +// %2 = stablehlo.uniform_quantize %0 // Input 1 f32->uniform quantized type. +// %3 = stablehlo.uniform_quantize %1 // Input 2 f32->uniform quantized type. +// %4 = stablehlo.dot_general(%2, %3) // In uniform quantized type. +// %5 = stablehlo.uniform_dequantize %4 // Dequantize the output. +// ``` +class ComposeUniformQuantizedDotGeneralOpWithTwoQuantizedActivations + : public OpRewritePattern { + public: + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(stablehlo::DotGeneralOp op, + PatternRewriter& rewriter) const final { + if (match(op).failed()) { + return failure(); + } + rewrite(op, rewriter); + return success(); + } + + private: + LogicalResult match(stablehlo::DotGeneralOp op) const { + // q1 - z1 + if (failed(MatchQuantizedOperand(op.getOperand(0)))) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match quantized operand pattern for LHS.\n"); + return failure(); + } + + // q2 - z2 + if (failed(MatchQuantizedOperand(op.getOperand(1)))) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match quantized operand pattern for RHS.\n"); + return failure(); + } + + // Go downstream from `op`. + // * s1 s2 + auto output_i32_to_f32_convert_op = TryCast( + *op.getResult().user_begin(), /*name=*/"output_i32_to_f32_convert_op"); + if (failed(output_i32_to_f32_convert_op)) return failure(); + + auto combined_scale_multiply_op = TryCast( + *output_i32_to_f32_convert_op->getResult().user_begin(), + /*name=*/"combined_scale_multiply_op"); + if (failed(combined_scale_multiply_op)) return failure(); + + // call @uniform_quantize() + auto output_uniform_quantize_call_op = TryCast( + *combined_scale_multiply_op->getResult().user_begin(), + /*name=*/"output_quantize_call_op"); + if (failed(output_uniform_quantize_call_op)) return failure(); + + auto output_uniform_quantize_call_pattern = + UniformQuantizeFunctionCallPattern::Match( + *output_uniform_quantize_call_op); + if (failed(output_uniform_quantize_call_pattern)) { + llvm::dbgs() << "Failed match uniform quantize call pattern.\n"; + return failure(); + } + + // call @uniform_dequantize() + auto output_uniform_dequantize_call_op = TryCast( + *output_uniform_quantize_call_op->getResult(0).user_begin(), + /*name=*/"output_uniform_dequantize_call_op"); + if (failed(output_uniform_dequantize_call_op)) return failure(); + + auto output_uniform_dequantize_call_pattern = + UniformDequantizeFunctionCallPattern::Match( + *output_uniform_dequantize_call_op); + if (failed(output_uniform_dequantize_call_pattern)) { + llvm::dbgs() << "Failed to match output uniform quantize call pattern.\n"; + return failure(); + } + + return success(); + } + + void rewrite(stablehlo::DotGeneralOp op, PatternRewriter& rewriter) const { + // Build uniform quantized type for input 1 (lhs). + auto input1_zero_point_subtract_op = + cast(op.getOperand(0).getDefiningOp()); + auto input1_i8_to_i32_convert_op = cast( + input1_zero_point_subtract_op.getOperand(0).getDefiningOp()); + auto input1_uniform_quantize_call_op = cast( + input1_i8_to_i32_convert_op.getOperand().getDefiningOp()); + auto input1_uniform_quantize_call_pattern = + UniformQuantizeFunctionCallPattern::Match( + input1_uniform_quantize_call_op); + + const float input1_inverse_scale_value = + input1_uniform_quantize_call_pattern->GetInverseScalesValueAttr() + .getSplatValue() + .convertToFloat(); + const float input1_scale_value = 1.0 / input1_inverse_scale_value; + + const int8_t input1_zero_point_value = + input1_uniform_quantize_call_pattern->GetZeroPointsValueAttr() + .getSplatValue() + .getSExtValue(); + + const UniformQuantizedType input1_uniform_quantized_type = + CreateI8F32UniformQuantizedType( + input1_uniform_quantize_call_op.getLoc(), *rewriter.getContext(), + input1_scale_value, input1_zero_point_value); + + Value input1_value = input1_uniform_quantize_call_pattern->GetInputValue(); + auto input1_uniform_quantize_op = stablehlo::UniformQuantizeOp::create( + rewriter, input1_uniform_quantize_call_op.getLoc(), + /*result=*/ + mlir::cast(input1_value.getType()) + .clone(input1_uniform_quantized_type), + /*operand=*/input1_value); + + rewriter.replaceAllUsesWith(input1_zero_point_subtract_op.getResult(), + input1_uniform_quantize_op.getResult()); + + // Build uniform quantized type for input 2 (rhs). + auto input2_zero_point_subtract_op = + cast(op.getOperand(1).getDefiningOp()); + auto input2_i8_to_i32_convert_op = cast( + input2_zero_point_subtract_op.getOperand(0).getDefiningOp()); + auto input2_uniform_quantize_call_op = cast( + input2_i8_to_i32_convert_op.getOperand().getDefiningOp()); + auto input2_uniform_quantize_call_pattern = + UniformQuantizeFunctionCallPattern::Match( + input2_uniform_quantize_call_op); + + const float input2_inverse_scale_value = + input2_uniform_quantize_call_pattern->GetInverseScalesValueAttr() + .getSplatValue() + .convertToFloat(); + const float input2_scale_value = 1.0 / input2_inverse_scale_value; + + const int8_t input2_zero_point_value = + input2_uniform_quantize_call_pattern->GetZeroPointsValueAttr() + .getSplatValue() + .getSExtValue(); + + const UniformQuantizedType input2_uniform_quantized_type = + CreateI8F32UniformQuantizedType( + input2_uniform_quantize_call_op.getLoc(), *rewriter.getContext(), + input2_scale_value, input2_zero_point_value); + + Value input2_value = input2_uniform_quantize_call_pattern->GetInputValue(); + auto input2_uniform_quantize_op = stablehlo::UniformQuantizeOp::create( + rewriter, input2_uniform_quantize_call_op.getLoc(), + /*result=*/ + mlir::cast(input2_value.getType()) + .clone(input2_uniform_quantized_type), + /*operand=*/input2_value); + + rewriter.replaceAllUsesWith(input2_zero_point_subtract_op.getResult(), + input2_uniform_quantize_op.getResult()); + + // Recreate stablehlo::DotGeneralOp with a uniform quantized output type. + // * s1 s2 + auto output_i32_to_f32_convert_op = + cast(*op.getResult().user_begin()); + auto combined_scale_multiply_op = cast( + *output_i32_to_f32_convert_op.getResult().user_begin()); + + // call @uniform_quantize() + auto output_uniform_quantize_call_op = cast( + *combined_scale_multiply_op.getResult().user_begin()); + + auto output_uniform_quantize_call_pattern = + UniformQuantizeFunctionCallPattern::Match( + output_uniform_quantize_call_op); + + // call @uniform_dequantize() + auto output_uniform_dequantize_call_op = cast( + *output_uniform_quantize_call_op.getResult(0).user_begin()); + + auto output_uniform_dequantize_call_pattern = + UniformDequantizeFunctionCallPattern::Match( + output_uniform_dequantize_call_op); + + const auto inverse_output_scale_value = + output_uniform_quantize_call_pattern->GetInverseScalesValueAttr() + .getSplatValue() + .convertToFloat(); + const float output_scale_value = 1.0 / inverse_output_scale_value; + + const int64_t output_zero_point_value = + output_uniform_quantize_call_pattern->GetZeroPointsValueAttr() + .getSplatValue() + .getSExtValue(); + + const UniformQuantizedType output_uniform_quantized_type = + CreateI8F32UniformQuantizedType( + output_uniform_quantize_call_op.getLoc(), *rewriter.getContext(), + output_scale_value, output_zero_point_value); + + auto new_dot_general_op = stablehlo::DotGeneralOp::create( + rewriter, op.getLoc(), /*resultType0=*/ + mlir::cast(op.getResult().getType()) + .clone(output_uniform_quantized_type), + /*lhs=*/op.getLhs(), /*rhs=*/op.getRhs(), + /*dot_dimension_numbers=*/op.getDotDimensionNumbers(), + /*precision_config=*/op.getPrecisionConfigAttr(), + /*algorithm=*/op.getAlgorithmAttr()); + + rewriter.replaceAllUsesWith(op.getResult(), new_dot_general_op.getResult()); + + auto new_output_dequant_op = stablehlo::UniformDequantizeOp::create( + rewriter, output_uniform_dequantize_call_op.getLoc(), + /*operand=*/new_dot_general_op); + + rewriter.replaceAllUsesWith(output_uniform_dequantize_call_op.getResult(0), + new_output_dequant_op.getResult()); + + // Erase unused ops after the transformation. + rewriter.eraseOp(output_uniform_dequantize_call_pattern->GetCallOp()); + rewriter.eraseOp(output_uniform_quantize_call_pattern->GetCallOp()); + rewriter.eraseOp(combined_scale_multiply_op); + rewriter.eraseOp(output_i32_to_f32_convert_op); + + rewriter.eraseOp(input1_zero_point_subtract_op); + rewriter.eraseOp(input1_i8_to_i32_convert_op); + rewriter.eraseOp(input1_uniform_quantize_call_pattern->GetCallOp()); + + rewriter.eraseOp(input2_zero_point_subtract_op); + rewriter.eraseOp(input2_i8_to_i32_convert_op); + rewriter.eraseOp(input2_uniform_quantize_call_pattern->GetCallOp()); + } + + private: + // Determines whether the `operand` is a result of quantized operand pattern, + // represented as `(q - z)` where `q` is the quantized value and `z` is the + // zero point. Returns `success()` iff `operand` matches the pattern, + // `failure()` otherwise. + LogicalResult MatchQuantizedOperand(Value operand) const { + // q - z + auto input_zero_point_subtract_op = + TryCast(operand.getDefiningOp(), + /*name=*/"input_zero_point_subtract_op"); + if (failed(input_zero_point_subtract_op)) return failure(); + + // z + auto input_zero_point_broadcast_in_dim_op = + TryCast( + input_zero_point_subtract_op->getOperand(1).getDefiningOp(), + /*name=*/"input_zero_point_broadcast_in_dim_op"); + if (failed(input_zero_point_broadcast_in_dim_op)) return failure(); + + Value input_zero_point_constant_value = + input_zero_point_broadcast_in_dim_op->getOperand(); + // Match optional i8->i32 conversion for z. + if (auto input_zero_point_i8_to_i32_convert_op = + TryCast( + input_zero_point_constant_value.getDefiningOp(), + /*name=*/"input_zero_point_i8_to_i32_convert_op"); + succeeded(input_zero_point_i8_to_i32_convert_op)) { + if (!IsI8ToI32Cast(*input_zero_point_i8_to_i32_convert_op)) { + return failure(); + } + input_zero_point_constant_value = + input_zero_point_i8_to_i32_convert_op->getOperand(); + } + + auto input_zero_point_constant_op = TryCast( + input_zero_point_constant_value.getDefiningOp(), + /*name=*/"input_zero_point_constant_op"); + if (failed(input_zero_point_constant_op)) return failure(); + + // q + auto input_i8_to_i32_convert_op = TryCast( + input_zero_point_subtract_op->getOperand(0).getDefiningOp(), + /*name=*/"input_i8_to_i32_convert_op"); + if (failed(input_i8_to_i32_convert_op)) return failure(); + + if (!IsI8ToI32Cast(*input_i8_to_i32_convert_op)) { + LLVM_DEBUG( + llvm::dbgs() + << "Failed to match. The ConvertOp is not an i8->i32 type cast.\n"); + return failure(); + } + + auto input_uniform_quantize_call_op = TryCast( + input_i8_to_i32_convert_op->getOperand().getDefiningOp(), + /*name=*/"input_uniform_quantize_call_op"); + if (failed(input_uniform_quantize_call_op)) return failure(); + + auto input_uniform_quantize_call_pattern = + UniformQuantizeFunctionCallPattern::Match( + *input_uniform_quantize_call_op); + if (failed(input_uniform_quantize_call_pattern)) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match input uniform quantize call pattern.\n"); + return failure(); + } + + return success(); + } +}; + +void ComposeUniformQuantizedTypePass::runOnOperation() { + ModuleOp module_op = getOperation(); + MLIRContext& ctx = getContext(); + + RewritePatternSet patterns(&ctx); + patterns.add( + &ctx); + + if (failed(applyPatternsGreedily(module_op, std::move(patterns)))) { + module_op.emitError() + << "Failed to compose stablehlo uniform quantized types."; + signalPassFailure(); + } +} + +} // namespace + +std::unique_ptr> +CreateComposeUniformQuantizedTypePass() { + return std::make_unique(); +} + +static PassRegistration pass; + +} // namespace odml +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_avg_pool.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_avg_pool.cc new file mode 100644 index 0000000000000000000000000000000000000000..4eb9b21ec19243d7f102773d5cb9dc6592e0f931 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_avg_pool.cc @@ -0,0 +1,262 @@ +/* Copyright 2024 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/compiler/mlir/lite/stablehlo/transforms/composite_avg_pool.h" + +#include +#include +#include +#include +#include +#include + +#include "llvm/ADT/STLExtras.h" +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_utils.h" +#include "tensorflow/compiler/mlir/lite/transforms/passes.h" // IWYU pragma: keep +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" // IWYU pragma: keep + +namespace mlir { +namespace odml { + +using mhlo::CompositeOp; + +// Struct for holding composite attrs for torch average pool as CC types. +struct TorchAvgPoolData { + int n; // Batch. + int c; // Channels. + + int h_in; // Input height. + int w_in; // Input width. + + int h_out; // Output height. + int w_out; // Output width. + + int kh; // Kernel height. + int kw; // Kernel width. + + int ph; // Padding on "height" dimension (both sides). + int pw; // Padding on "width" dimension (both sides). + + int sh; // Stride on "height" dimension. + int sw; // Stride on "width" dimension. + + bool ceil_mode; // Rounding strategy (ceil or floor). +}; + +// Rounds the dimension based on the ceil mode. +int RoundDim(float dim, bool ceil_mode) { + if (ceil_mode) { + return std::ceil(dim); + } + return std::floor(dim); +} + +// For H or W, calculate the output dimension for average pool. +int CalculateSpatialOutDim(int in, int k, int p, int s, bool ceil_mode) { + const float effective_size = in - k + (2 * p); + int out = RoundDim(effective_size / (float)s, ceil_mode) + 1; + // Only possible if rounder is ceil. + if ((out - 1) * s >= in + p) { + out -= 1; + } + return out; +} + +// Builds a `TorchAvgPoolData` from composite op. +TorchAvgPoolData GetTorchAvgPoolData(CompositeOp op) { + auto composite_attrs = op.getCompositeAttributes(); + TorchAvgPoolData data; + + auto op_type = mlir::cast(op.getOperand(0).getType()); + + data.n = op_type.getShape()[0]; + data.c = op_type.getShape()[1]; + data.h_in = op_type.getShape()[2]; + data.w_in = op_type.getShape()[3]; + + std::vector kernel_size; + GetI32VectorFromDenseI64CompositeAttr(composite_attrs, "kernel_size", + &kernel_size); + data.kh = kernel_size[0]; + data.kw = kernel_size[1]; + + std::vector padding; + GetI32VectorFromDenseI64CompositeAttr(composite_attrs, "padding", &padding); + data.ph = padding[0]; + data.pw = padding[1]; + + std::vector strides; + GetI32VectorFromDenseI64CompositeAttr(composite_attrs, "stride", &strides); + data.sh = strides[0]; + data.sw = strides[1]; + + data.ceil_mode = + GetBoolFromCompositeAttr(composite_attrs, "ceil_mode").value(); + + data.h_out = CalculateSpatialOutDim(data.h_in, data.kh, data.ph, data.sh, + data.ceil_mode); + data.w_out = CalculateSpatialOutDim(data.w_in, data.kw, data.pw, data.sw, + data.ceil_mode); + + return data; +} + +// Determines the true number of present elements in the given window +// in input tensor with pytorch rounding behavior. +int ActualNumElementsInKernel(int k_row_start, int k_col_start, + const TorchAvgPoolData& pool) { + int res = 0; + for (int k_col = 0; k_col < pool.kw; ++k_col) { + for (int k_row = 0; k_row < pool.kh; ++k_row) { + const int target_col = k_col + k_col_start; + const int target_row = k_row + k_row_start; + + const bool row_in_bound = target_row < pool.h_in + (2 * pool.ph); + const bool col_in_bound = target_col < pool.w_in + (2 * pool.pw); + const bool is_counted_in_original_input = row_in_bound && col_in_bound; + + res += is_counted_in_original_input; + } + } + return res; +} + +// Gets a matrix which corrects the overcounting of divisors when casting a +// average pool with ceil mode true as one with ceil mode false on a padded +// tensor. +DenseFPElementsAttr GetCorrectionMatrix(Builder& builder, CompositeOp op) { + const TorchAvgPoolData pool = GetTorchAvgPoolData(op); + + llvm::SmallVector nhwc_out_shape(4); + nhwc_out_shape[0] = 1; // Broadcast batch. + nhwc_out_shape[1] = pool.h_out; + nhwc_out_shape[2] = pool.w_out; + nhwc_out_shape[3] = 1; // Broadcast channels. + + auto out_shaped_type = + RankedTensorType::get(nhwc_out_shape, builder.getF32Type()); + auto get_flat_ind = [&](int row, int col) -> size_t { + return row * pool.w_out + col; + }; + + std::vector correction_data(out_shaped_type.getNumElements(), 1.0); + + const float kern_size = pool.kh * pool.kw; + + // LEMMA 1: Changing the rounding mode from floor to ceil will increase an + // output dimension by at most 1 (see `ComputeSpatialOutDim`). This is because + // for any `x`, `ceil(x) - floor(x) <= 1`. + + // Consider that we pad the input of a average pool with floor rounding to the + // appropriate size and switch the rounding mode to ceil. When computing the + // average of a given window, the elements which exist in the newly padded + // zones will be counted as present elements. Therefore in some windows we + // will overcount the divisors. + // Following from (LEMMA 1) the only windows which contain overcounted + // divisors are the ones on the outside right and bottom edge. We can iterate + // over these windows and multiply the corresponding out element by + // `kernel_size / X` where `X` is the number of elements in the padded input + // tensor not in the newly padded zone. This corrects the overcounting of + // divisors resulting in an equivalant computation. + { + const int right_col = pool.w_out - 1; + const int k_col_start = right_col * pool.sw; + for (int row = 0; row < pool.h_out; ++row) { + const int k_row_start = row * pool.sh; + const int correct_divisor = + ActualNumElementsInKernel(k_row_start, k_col_start, pool); + const size_t flat_ind = get_flat_ind(row, right_col); + correction_data[flat_ind] = kern_size / correct_divisor; + } + } + + { + const int bottom_row = pool.h_out - 1; + const int k_row_start = bottom_row * pool.sh; + for (int col = 0; col < pool.w_out; ++col) { + const int k_col_start = col * pool.sw; + const int correct_divisor = + ActualNumElementsInKernel(k_row_start, k_col_start, pool); + const size_t flat_ind = get_flat_ind(bottom_row, col); + correction_data[flat_ind] = kern_size / correct_divisor; + } + } + + return DenseFPElementsAttr::get(out_shaped_type, correction_data); +} + +std::array GetPadOpPaddingValues(const TorchAvgPoolData& pool) { + int pad_bottom = pool.ph; + int pad_right = pool.pw; + + if (pool.ceil_mode) { + const int remaining_bottom = pool.h_in - ((pool.h_out - 1) * pool.sh); + const int ceil_pad_bottom = pool.kh - remaining_bottom; + pad_bottom = ceil_pad_bottom - pool.ph; + + const int remaining_right = pool.w_in - ((pool.w_out - 1) * pool.sw); + const int ceil_pad_right = pool.kw - remaining_right; + pad_right = ceil_pad_right - pool.pw; + } + + return {pool.ph, pad_bottom, pool.pw, pad_right}; +} + +DenseIntElementsAttr GetPadOpAttr(Builder& builder, CompositeOp op) { + const TorchAvgPoolData pool = GetTorchAvgPoolData(op); + + const auto values = GetPadOpPaddingValues(pool); + + llvm::SmallVector padding_vec(8, 0); // NHWC + for (auto [ind, val] : llvm::enumerate(values)) { + padding_vec[ind + 2] = val; + } + + auto padding_shape = RankedTensorType::get({4, 2}, builder.getI32Type()); + return DenseIntElementsAttr::get(padding_shape, padding_vec); +} + +ShapedType GetPadOpType(CompositeOp op) { + const TorchAvgPoolData pool = GetTorchAvgPoolData(op); + + const auto padding_values = GetPadOpPaddingValues(pool); + const int64_t h = pool.h_in + padding_values[0] + padding_values[1]; + const int64_t w = pool.w_in + padding_values[2] + padding_values[3]; + llvm::SmallVector shape = {pool.n, h, w, pool.c}; + + auto op_type = mlir::cast(op->getResult(0).getType()); + return RankedTensorType::get(shape, op_type.getElementType()); +} + +StringAttr GetAvgPoolOpPadAttr(Builder& builder, CompositeOp op) { + const TorchAvgPoolData pool = GetTorchAvgPoolData(op); + + if (pool.ph == 0 && pool.pw == 0) { + return builder.getStringAttr("VALID"); + } + if (pool.h_out == pool.h_in && pool.w_out == pool.w_in) { + return builder.getStringAttr("SAME"); + } + return builder.getStringAttr("CUSTOM"); +} + +} // namespace odml +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_avg_pool.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_avg_pool.h new file mode 100644 index 0000000000000000000000000000000000000000..d861caa3b5cefd9e3dc965c2755976ad37b41ab4 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_avg_pool.h @@ -0,0 +1,55 @@ +/* Copyright 2024 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_COMPOSITE_AVG_POOL_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_COMPOSITE_AVG_POOL_H_ + +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/transforms/passes.h" // IWYU pragma: keep +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" // IWYU pragma: keep + +namespace mlir { +namespace odml { + +// Given a Composite op that wraps a core.aten.avg_pool2d, returns the padding +// configuration required for the `tfl.pad` if the padding part of the op is +// to be done before average pooling. +DenseIntElementsAttr GetPadOpAttr(Builder& builder, mhlo::CompositeOp op); + +// Given a Composite op that wraps a core.aten.avg_pool2d, and assuming that +// the padding part is extracted into a tfl.pad op prior to a +// tfl.average_pool_2d, this function finds the return type of the needed +// tfl.pad . +ShapedType GetPadOpType(mhlo::CompositeOp op); + +// Given a Composite op that wraps a core.aten.avg_pool2d, finds the padding +// attribute to be passed to the a tfl.average_pool_2d that can fully replace +// this composite (here, padding is done directly by the tfl.average_pool_2d as +// opposed to being extracted into a separate tfl.pad). +StringAttr GetAvgPoolOpPadAttr(Builder& builder, mhlo::CompositeOp op); + +// Get dense attr for a matrix that corrects the over counting of divisors when +// casting an average pool with ceil mode on in terms of average pool with it +// off. +DenseFPElementsAttr GetCorrectionMatrix(Builder& builder, mhlo::CompositeOp op); + +} // namespace odml +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_COMPOSITE_AVG_POOL_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_avg_pool_patterns.td b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_avg_pool_patterns.td new file mode 100644 index 0000000000000000000000000000000000000000..5c721ec825ef3f0f007f7e36544591b0a93b3ae9 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_avg_pool_patterns.td @@ -0,0 +1,132 @@ +/* Copyright 2024 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 "mlir/IR/PatternBase.td" +include "tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_utils.td" +include "mhlo/IR/hlo_ops.td" + + +// See the function doc in the header file. +def GetPadOpType : NativeCodeCall< + "GetPadOpType((*$0.begin()).getDefiningOp())">; + +// See the function doc in the header file. +def GetAvgPoolOpPadAttr: + NativeCodeCall<"GetAvgPoolOpPadAttr($_builder, (*$0.begin()).getDefiningOp())">; + +// Returns true if the provided padding in the composite op can *not* be +// satisfied by SAME or VALID tensorflow padding. +def HasCustomPadding: + Constraint()) == $_builder.getStringAttr(\"CUSTOM\")">>; + +// Returns true if the provided padding in the composite op can be satisfied +// by SAME or VALID tensorflow padding. +def HasSameOrValidPadding: Constraint>; + +// See the function doc in the header file. +def GetPadOpAttr: NativeCodeCall<"GetPadOpAttr($_builder, (*$0.begin()).getDefiningOp())">; + +// See the function doc in the header file. +def GetPaddingArrayCeilMode: NativeCodeCall<"GetPaddingArrayCeilMode($_builder, (*$0.begin()).getDefiningOp())">; + +class CreateStringAttr : NativeCodeCall< + "$_builder.getStringAttr("# values #")">; + +// Get dense attr for a matrix that corrects the over counting of dividends when casting an average pool with ceil mode on in terms of average pool with it off. +def GetCorrectionMatrix: NativeCodeCall<"GetCorrectionMatrix($_builder, (*$0.begin()).getDefiningOp())">; + +// Replaces an aten.avg_pool2d with a (T -> tfl.average_pool_2d -> T). +// Constraints are added on the attributes of the aten.avg_pool2d to ensure only +// ops that match the behaviour of tfl.average_pool_2d are directly lowered. +def LegalizeAvgPool2dComposite: Pat< + (MHLO_CompositeOp:$old_val + (variadic $a_input), + ConstantStrAttr, $attrs, $_, $_), + (TFL_TransposeOp + (TFL_AveragePool2DOp + /*input*/ (TFL_TransposeOp $a_input, + (Arith_ConstantOp + ConstantAttr,"{0, 2, 3, 1}">)), + /*filter_height*/(GetI32At<0> (GetAsVectorAttr<"kernel_size"> $attrs)), + /*filter_width*/(GetI32At<1> (GetAsVectorAttr<"kernel_size"> $attrs)), + /*padding*/(GetAvgPoolOpPadAttr $old_val), + /*stride_h*/(GetI32At<0> (GetAsVectorAttr<"stride"> $attrs)), + /*stride_w*/(GetI32At<1> (GetAsVectorAttr<"stride"> $attrs)), + /*fused_activation_function*/TFL_AF_None, + (returnType (GetNhwcReturnTypeFromNchw $old_val))), + (Arith_ConstantOp + ConstantAttr,"{0, 3, 1, 2}">)), + [(IsBoolCompositeAttribute<"ceil_mode", "false"> $attrs), + (IsBoolCompositeAttribute<"count_include_pad", "false"> $attrs), + (IsStrCompositeAttribute<"divisor_override", "py_None"> $attrs), + (HasSameOrValidPadding $old_val)]>; + +// Replaces an aten.avg_pool2d with (T -> tfl.pad -> tfl.average_pool_2d -> T). +def LegalizeAvgPool2dWithPadComposite: Pat< + (MHLO_CompositeOp:$old_val + (variadic $a_input), + ConstantStrAttr, $attrs, $_, $_), + (TFL_TransposeOp + (TFL_AveragePool2DOp:$padded_value + /*input*/ (TFL_PadOp + (TFL_TransposeOp $a_input, + (Arith_ConstantOp + ConstantAttr,"{0, 2, 3, 1}">)), + (Arith_ConstantOp + (GetPadOpAttr $old_val)), + (returnType (GetPadOpType $old_val))), + /*filter_height*/(GetI32At<0> (GetAsVectorAttr<"kernel_size"> $attrs)), + /*filter_width*/(GetI32At<1> (GetAsVectorAttr<"kernel_size"> $attrs)), + /*padding*/TFL_PAD_Valid, + /*stride_h*/(GetI32At<0> (GetAsVectorAttr<"stride"> $attrs)), + /*stride_w*/(GetI32At<1> (GetAsVectorAttr<"stride"> $attrs)), + /*fused_activation_function*/TFL_AF_None, + (returnType (GetNhwcReturnTypeFromNchw $old_val))), + (Arith_ConstantOp + ConstantAttr,"{0, 3, 1, 2}">)), + [(IsBoolCompositeAttribute<"ceil_mode", "false"> $attrs), + (IsStrCompositeAttribute<"divisor_override", "py_None"> $attrs), + (IsBoolCompositeAttribute<"count_include_pad", "true"> $attrs)]>; + +// Replaces aten.avg_pool2d with ceil mode with (T -> tfl.pad -> tfl.average_pool_2d -> mul -> T). Multiplies by a constant +// which corrects the overcounting of divisors that would occur if doing this computation on a padded tensor with ceil mode off. +def LegalizeAvgPoolCeilModeTrue: Pat< + (MHLO_CompositeOp:$old_val + (variadic $a_input), + ConstantStrAttr, $attrs, $_, $_), + (TFL_TransposeOp + (TFL_MulOp + (TFL_AveragePool2DOp:$padded_value + (TFL_PadOp + (TFL_TransposeOp $a_input, + (Arith_ConstantOp + ConstantAttr,"{0, 2, 3, 1}">)), + (Arith_ConstantOp + (GetPadOpAttr $old_val)), + (returnType (GetPadOpType $old_val))), + (GetI32At<0> (GetAsVectorAttr<"kernel_size"> $attrs)), + (GetI32At<1> (GetAsVectorAttr<"kernel_size"> $attrs)), + TFL_PAD_Valid, + (GetI32At<0> (GetAsVectorAttr<"stride"> $attrs)), + (GetI32At<1> (GetAsVectorAttr<"stride"> $attrs)), + TFL_AF_None, + (returnType (GetNhwcReturnTypeFromNchw $old_val))), + (Arith_ConstantOp (GetCorrectionMatrix $old_val)), + TFL_AF_None), + (Arith_ConstantOp + ConstantAttr,"{0, 3, 1, 2}">)), + [(IsBoolCompositeAttribute<"ceil_mode", "true"> $attrs), + (IsStrCompositeAttribute<"divisor_override", "py_None"> $attrs), + (IsBoolCompositeAttribute<"count_include_pad", "true"> $attrs)]>; \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_lowering_pass.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_lowering_pass.cc new file mode 100644 index 0000000000000000000000000000000000000000..654b0bec144f836e9b45a1af9b805a72c52ddbc1 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_lowering_pass.cc @@ -0,0 +1,81 @@ +/* Copyright 2024 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 "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Support/TypeID.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_avg_pool.h" // IWYU pragma: keep +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_utils.h" // IWYU pragma: keep +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h" +#include "tensorflow/compiler/mlir/lite/transforms/passes.h" // IWYU pragma: keep +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" // IWYU pragma: keep + +namespace mlir { +namespace odml { + +namespace { + +// This file is generated from `passes.td` and provides the implementation base +// class. +#define GEN_PASS_DEF_COMPOSITELOWERINGPASS +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h.inc" + +class CompositeLoweringPass + : public impl::CompositeLoweringPassBase { + public: + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(CompositeLoweringPass); + + void runOnOperation() override; +}; + +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/generated_composite_lowering.inc" + +void CompositeLoweringPass::runOnOperation() { + RewritePatternSet patterns(&getContext()); + + populateWithGenerated(patterns); + + ConversionTarget target(getContext()); + target.addLegalDialect(); + target.addLegalDialect(); + + if (failed(applyPartialConversion(getOperation(), target, + std::move(patterns)))) { + getOperation().emitError("Composite lowering pass failed."); + signalPassFailure(); + } +} + +} // namespace + +// Creates an instance of the pass. +std::unique_ptr> CreateCompositeLoweringPass() { + return std::make_unique(); +} + +// Registers the pass implementation +static PassRegistration pass; + +} // namespace odml +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_lowering_pass.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_lowering_pass.h new file mode 100644 index 0000000000000000000000000000000000000000..1403a22a462693875743d35b0e50d5fc0a204b2f --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_lowering_pass.h @@ -0,0 +1,27 @@ +/* Copyright 2024 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_COMPOSITE_LOWERING_PASS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_COMPOSITE_LOWERING_PASS_H_ + +namespace mlir { +namespace odml { + +std::unique_ptr CreateCompositeLoweringPass(); + +} // namespace odml +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_COMPOSITE_LOWERING_PASS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_lowering_patterns.td b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_lowering_patterns.td new file mode 100644 index 0000000000000000000000000000000000000000..910df0ea045af610bbe46b001fa82f5efb77ccc8 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_lowering_patterns.td @@ -0,0 +1,313 @@ +/* Copyright 2024 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. +==============================================================================*/ + +// Pattern definition file for direct lowering of mhlo composites to tflite ops. +include "mlir/IR/OpBase.td" +include "mlir/Dialect/Arith/IR/ArithOps.td" +include "mlir/Dialect/Func/IR/FuncOps.td" +include "mhlo/IR/hlo_ops.td" +include "tensorflow/compiler/mlir/lite/ir/tfl_ops.td" +include "tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_utils.td" +include "tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_avg_pool_patterns.td" + +def LegalizeHardSwishComposite: Pat< + (MHLO_CompositeOp:$old_val + (variadic $input), + ConstantStrAttr, $_, $_, $_), + (TFL_HardSwishOp $input)>; + +def IsNchwLayoutOp: Constraint($0.get(\"is_nchw_op\")) " + "== mlir::BoolAttr::get($_builder.getContext(), true)">>; + +def IsNhwcLayoutOp: Constraint>; + +class HasRankN : Constraint< + CPred<"llvm::cast($0.getType()).hasRank() && " + "llvm::cast($0.getType()).getRank() == " # n>>; + +class HasRankAtLeast : Constraint< + CPred<"llvm::cast($0.getType()).hasRank() && " + "llvm::cast($0.getType()).getRank() >= " # n>>; + +def I32ElementsVal : Constraint($0.getType()).getElementType().isInteger(32)">, + "32 bit integer tensor">; + +class NegatedCompositeBoolAttribute: NativeCodeCall< + "BoolAttr::get($_builder.getContext(), !llvm::cast($0.get(\"" # attr_name # "\")).getValue())">; + +// TODO(b/343278954): Move the creation of transposes to a separate prepare pass +// to avoid creating multiple pattern-rewrite rules for the same composite op. +def LegalizeTorchUpsampleBlinear2dComposite: Pat< + (MHLO_CompositeOp:$old_val + (variadic $input), + ConstantStrAttr, $attrs, $_, $_), + (TFL_TransposeOp + (TFL_ResizeBilinearOp + (TFL_TransposeOp $input, + (Arith_ConstantOp + ConstantAttr,"{0, 2, 3, 1}">)), + (Arith_ConstantOp:$output_size (GetI32DenseAttr (GetAsVectorAttr<"size"> $attrs))), + (GetCompositeAttributeAs<"align_corners", "BoolAttr"> $attrs), + (/*half_pixel_centers=*/NegatedCompositeBoolAttribute<"align_corners"> $attrs), + (returnType (GetNhwcReturnTypeFromNchw $old_val))), + (Arith_ConstantOp + ConstantAttr,"{0, 3, 1, 2}">)), + [(IsNchwLayoutOp $attrs)]>; + +// Pattern to lower a stablehlo.composite of `jax` image resize fuctions in +// `bilinear` mode and with NHWC inputs to a tflite.resize_nearest_neighbor op. +def LegalizeNhwcResizeBilinear2dComposite: Pat< + (MHLO_CompositeOp:$old_val + (variadic $input), + ConstantStrAttr, $attrs, $_, $_), + (TFL_ResizeBilinearOp + $input, + (Arith_ConstantOp:$output_size (GetI32DenseAttr (GetAsVectorAttr<"size"> $attrs))), + (GetCompositeAttributeAs<"align_corners", "BoolAttr"> $attrs), + (/*half_pixel_centers=*/NegatedCompositeBoolAttribute<"align_corners"> $attrs)), + [(IsNhwcLayoutOp $attrs)]>; + +// pattern to lower a stablehlo.composite of `jax` and `pytorch` image resize fuctions +// in `nearest` mode and with NHWC inputs to a tflite.resize_nearest_neighbor op. +def LegalizeNhwcResizeNearestNeighbor2dComposite: Pat< + (MHLO_CompositeOp:$old_val + (variadic $input), + ConstantStrAttr, $attrs, $_, $_), + (TFL_ResizeNearestNeighborOp + $input, + (Arith_ConstantOp:$output_size (GetI32DenseAttr (GetAsVectorAttr<"size"> $attrs))), + /*align_corners=*/ConstBoolAttrFalse, + /*half_pixel_centers=*/ConstBoolAttrTrue), + [(IsNhwcLayoutOp $attrs)]>; + +// pattern to lower a stablehlo.composite of `jax` and `pytorch` image resize fuctions +// in `nearest`mode and with NCHW inputs to a tflite.resize_nearest_neighbor op. +// TODO(b/343278954): Move the creation of transposes to a separate prepare pass +// to avoid creating multiple pattern-rewrite rules for the same composite op. +def LegalizeNchwResizeNearestNeighbor2dComposite: Pat< + (MHLO_CompositeOp:$old_val + (variadic $input), + ConstantStrAttr, $attrs, $_, $_), + (TFL_TransposeOp + (TFL_ResizeNearestNeighborOp + (TFL_TransposeOp $input, + (Arith_ConstantOp + ConstantAttr,"{0, 2, 3, 1}">)), + (Arith_ConstantOp:$output_size (GetI32DenseAttr (GetAsVectorAttr<"size"> $attrs))), + /*align_corners=*/ConstBoolAttrFalse, + /*half_pixel_centers=*/ConstBoolAttrTrue, + (returnType (GetNhwcReturnTypeFromNchw $old_val))), + (Arith_ConstantOp + ConstantAttr,"{0, 3, 1, 2}">)), + [(IsNchwLayoutOp $attrs)]>; + +def LegalizeCompositeExactAtenGELU : Pat< + (MHLO_CompositeOp:$composite + (variadic $inputs), + ConstantStrAttr, $attrs, $_, $_), + (TFL_GeluOp $inputs, ConstBoolAttrFalse), + [(IsStrCompositeAttribute<"approximate", "none"> $attrs)]>; + +def LegalizeCompositeApproximateAtenGELU : Pat< + (MHLO_CompositeOp:$composite + (variadic $inputs), + ConstantStrAttr, $attrs, $_, $_), + (TFL_GeluOp $inputs, ConstBoolAttrTrue), + [(IsStrCompositeAttribute<"approximate", "tanh"> $attrs)]>; + +def LegalizeCompositeGELU : Pat< + (MHLO_CompositeOp:$composite + (variadic $inputs), + ConstantStrAttr, $attrs, $_, $_), + (TFL_GeluOp $inputs, + (GetCompositeAttributeAs<"approximate", "BoolAttr"> $attrs))>; + +def LegalizeCompositeGELUDynamicShaped : Pat< + (MHLO_CompositeOp:$composite + (variadic $_, $inputs), + ConstantStrAttr, $attrs, $_, $_), + (TFL_GeluOp $inputs, + (GetCompositeAttributeAs<"approximate", "BoolAttr"> $attrs))>; + +def LegalizeCompositeGELUDynamicShaped2 : Pat< + (MHLO_CompositeOp:$composite + (variadic $_, $_, $inputs), + ConstantStrAttr, $attrs, $_, $_), + (TFL_GeluOp $inputs, + (GetCompositeAttributeAs<"approximate", "BoolAttr"> $attrs))>; + +def LegalizeCompositeGELUDynamicShaped3 : Pat< + (MHLO_CompositeOp:$composite + (variadic $_, $_, $_, $inputs), + ConstantStrAttr, $attrs, $_, $_), + (TFL_GeluOp $inputs, + (GetCompositeAttributeAs<"approximate", "BoolAttr"> $attrs))>; + +def LegalizeCompositeOdmlEmbeddingLookup : Pat< + (MHLO_CompositeOp:$composite + (variadic $indices, $table), + ConstantStrAttr, $attrs, $_, $_), + (TFL_EmbeddingLookupOp $indices, $table), + [(HasRankN<1> $indices), + (I32ElementsVal $indices), + (HasRankAtLeast<2> $table)]>; + +def LegalizeCompositeOdmlEmbeddingLookupDynamicShaped : Pat< + (MHLO_CompositeOp:$composite + (variadic $_, $_, $_, $indices, $table), + ConstantStrAttr, $attrs, $_, $_), + (TFL_EmbeddingLookupOp $indices, $table), + [(HasRankN<1> $indices), + (I32ElementsVal $indices), + (HasRankAtLeast<2> $table)]>; + +def LegalizeCompositeOdmlEmbeddingLookupDynamicShaped2 : Pat< + (MHLO_CompositeOp:$composite + (variadic $_, $_, $indices, $table), + ConstantStrAttr, $attrs, $_, $_), + (TFL_EmbeddingLookupOp $indices, $table), + [(HasRankN<1> $indices), + (I32ElementsVal $indices), + (HasRankAtLeast<2> $table)]>; + +def LegalizeCompositeOdmlEmbeddingLookupDynamicShaped3 : Pat< + (MHLO_CompositeOp:$composite + (variadic $_, $indices, $table), + ConstantStrAttr, $attrs, $_, $_), + (TFL_EmbeddingLookupOp $indices, $table), + [(HasRankN<1> $indices), + (I32ElementsVal $indices), + (HasRankAtLeast<2> $table)]>; + +def LegalizeCompositeOdmlRandomUniform : Pat< + (MHLO_CompositeOp:$composite + (variadic $shape), + ConstantStrAttr, $attrs, $_, $_), + (TFL_RandomUniformOp $shape, + (GetCompositeAttributeAs<"seed", "IntegerAttr"> $attrs), + (GetCompositeAttributeAs<"seed2", "IntegerAttr"> $attrs))>; + +def LegalizeCompositeOdmlRandomStandardNormal : Pat< + (MHLO_CompositeOp:$composite + (variadic $shape), + ConstantStrAttr, $attrs, $_, $_), + (TFL_RandomStandardNormalOp $shape, + (GetCompositeAttributeAs<"seed", "IntegerAttr"> $attrs), + (GetCompositeAttributeAs<"seed2", "IntegerAttr"> $attrs))>; + +def LegalizeCompositeUnpack : Pat< + (MHLO_CompositeOp:$composite + (variadic $inputs), + ConstantStrAttr, $attrs, $_, $_), + (TFL_UnpackOp $inputs, + (GetCompositeAttributeAs<"num", "IntegerAttr"> $attrs), + (GetCompositeAttributeAs<"axis", "IntegerAttr"> $attrs))>; + +def LegalizeCompositeUnpack1 : Pat< + (MHLO_CompositeOp:$composite + (variadic $_, $inputs), + ConstantStrAttr, $attrs, $_, $_), + (TFL_UnpackOp $inputs, + (GetCompositeAttributeAs<"num", "IntegerAttr"> $attrs), + (GetCompositeAttributeAs<"axis", "IntegerAttr"> $attrs))>; + +def LegalizeCompositePack4Elements : Pat< + (MHLO_CompositeOp:$composite + // TD not able to represent variadic of variadic now. + // Move to C++ matcher to support more cases. + (variadic $i0, $i1, $i2, $i3), + ConstantStrAttr, $attrs, $_, $_), + (TFL_PackOp (variadic $i0, $i1, $i2, $i3), + (GetCompositeAttributeAs<"values_count", "IntegerAttr"> $attrs), + (GetCompositeAttributeAs<"axis", "IntegerAttr"> $attrs))>; + +def LegalizeCompositePack4Elements1 : Pat< + (MHLO_CompositeOp:$composite + // TD not able to represent variadic of variadic now. + // Move to C++ matcher to support more cases. + (variadic $_, $i0, $i1, $i2, $i3), + ConstantStrAttr, $attrs, $_, $_), + (TFL_PackOp (variadic $i0, $i1, $i2, $i3), + (GetCompositeAttributeAs<"values_count", "IntegerAttr"> $attrs), + (GetCompositeAttributeAs<"axis", "IntegerAttr"> $attrs))>; + +def LegalizeDynamicSlice : Pat< + (MHLO_CompositeOp:$composite + (variadic $input, $begin, $size), + ConstantStrAttr, $attrs, $_, $_), + (TFL_SliceOp $input, $begin, $size)>; + +def LegalizeDynamicSlice_1 : Pat< + (MHLO_CompositeOp:$composite + (variadic $_, $input, $begin, $size), + ConstantStrAttr, $attrs, $_, $_), + (TFL_SliceOp $input, $begin, $size)>; + +def LegalizeBMM4dAdjY : Pat< + (MHLO_CompositeOp:$composite + (variadic $lhs, $rhs), + ConstantStrAttr, $attrs, $_, $_), + (TFL_BatchMatMulOp $lhs, $rhs, ConstBoolAttrFalse, ConstBoolAttrTrue, + ConstBoolAttrFalse)>; + +// For dynamic shaped lhs and rhs. +def LegalizeBMM4dAdjY_1 : Pat< + (MHLO_CompositeOp:$composite + (variadic $_, $lhs, $rhs), + ConstantStrAttr, $attrs, $_, $_), + (TFL_BatchMatMulOp $lhs, $rhs, ConstBoolAttrFalse, ConstBoolAttrTrue, + ConstBoolAttrFalse)>; + +def LegalizeBMM4dAdjY_2 : Pat< + (MHLO_CompositeOp:$composite + (variadic $_, $_, $lhs, $rhs), + ConstantStrAttr, $attrs, $_, $_), + (TFL_BatchMatMulOp $lhs, $rhs, ConstBoolAttrFalse, ConstBoolAttrTrue, + ConstBoolAttrFalse)>; + +def LegalizeCompositeMean : Pat< + (MHLO_CompositeOp:$composite + (variadic $input), + ConstantStrAttr, $attrs, $_, $_), + (TFL_MeanOp $input, + (Arith_ConstantOp (GetI32DenseAttr (GetAsVectorAttr<"axis"> $attrs))), + (GetCompositeAttributeAs<"keep_dims", "BoolAttr"> $attrs))>; + +def LegalizeCompositeMeanDynamicShaped : Pat< + (MHLO_CompositeOp:$composite + (variadic $_, $input), + ConstantStrAttr, $attrs, $_, $_), + (TFL_MeanOp $input, + (Arith_ConstantOp (GetI32DenseAttr (GetAsVectorAttr<"axis"> $attrs))), + (GetCompositeAttributeAs<"keep_dims", "BoolAttr"> $attrs))>; + +def LegalizeCompositeMeanDynamicShaped2 : Pat< + (MHLO_CompositeOp:$composite + (variadic $_, $_, $input), + ConstantStrAttr, $attrs, $_, $_), + (TFL_MeanOp $input, + (Arith_ConstantOp (GetI32DenseAttr (GetAsVectorAttr<"axis"> $attrs))), + (GetCompositeAttributeAs<"keep_dims", "BoolAttr"> $attrs))>; + +def LegalizeCompositeMeanDynamicShaped3 : Pat< + (MHLO_CompositeOp:$composite + (variadic $_, $_, $_, $input), + ConstantStrAttr, $attrs, $_, $_), + (TFL_MeanOp $input, + (Arith_ConstantOp (GetI32DenseAttr (GetAsVectorAttr<"axis"> $attrs))), + (GetCompositeAttributeAs<"keep_dims", "BoolAttr"> $attrs))>; + diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_utils.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_utils.cc new file mode 100644 index 0000000000000000000000000000000000000000..67d1a5637d739cd20c22c56cd29e176802184c12 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_utils.cc @@ -0,0 +1,94 @@ +/* Copyright 2024 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/compiler/mlir/lite/stablehlo/transforms/composite_utils.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "llvm/ADT/StringRef.h" +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project + +namespace mlir { +namespace odml { + +DenseIntElementsAttr DenseI64AttrToI32Attr( + const DenseIntElementsAttr& dense_attr, PatternRewriter& builder) { + std::vector ret(dense_attr.getNumElements()); + auto range = dense_attr.getValues(); + std::transform(range.begin(), range.end(), ret.begin(), + [](int64_t attr) { return static_cast(attr); }); + return DenseIntElementsAttr::get( + RankedTensorType::get(ret.size(), builder.getIntegerType(32)), ret); +} + +bool DenseI64AttrToI32Vector(const DenseIntElementsAttr& dense_attr, + std::vector* out_vec) { + std::vector ret(dense_attr.getNumElements()); + auto range = dense_attr.getValues(); + std::transform(range.begin(), range.end(), ret.begin(), + [](int64_t attr) { return static_cast(attr); }); + *out_vec = std::move(ret); + return true; +} + +bool GetI32VectorFromDenseI64CompositeAttr( + const DictionaryAttr& composite_attrs, const std::string& attr_name, + std::vector* out_vec) { + DenseIntElementsAttr attr; + if (!EnsureAttribute(composite_attrs, attr_name, + &attr)) { + return false; + } + + return DenseI64AttrToI32Vector(attr, out_vec); +} + +std::optional GetBoolFromCompositeAttr( + const DictionaryAttr& composite_attrs, llvm::StringRef attr_name) { + auto attr = composite_attrs.get(attr_name); + if (!attr) return std::nullopt; + if (auto bool_attr = mlir::dyn_cast_or_null(attr)) { + return bool_attr.getValue(); + } + return std::nullopt; +} + +ShapedType GetNhwcReturnTypeFromNchw(Operation* old_op) { + auto composite_result_shape = + mlir::cast(old_op->getResults().front().getType()).getShape(); + std::array output_shape; + // NHWC <- NCHW + output_shape[0] = composite_result_shape[0]; + output_shape[1] = composite_result_shape[2]; + output_shape[2] = composite_result_shape[3]; + output_shape[3] = composite_result_shape[1]; + + auto input_type = mlir::cast(old_op->getOperand(0).getType()); + + return RankedTensorType::get(output_shape, input_type.getElementType()); +} +} // namespace odml +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_utils.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..a4eab4dd6a34416ba7bf6e84e027a327a23a9399 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_utils.h @@ -0,0 +1,84 @@ +/* Copyright 2024 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_COMPOSITE_UTILS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_COMPOSITE_UTILS_H_ + +#include +#include +#include +#include + +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/transforms/passes.h" // IWYU pragma: keep +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" // IWYU pragma: keep + +namespace mlir { +namespace odml { + +// Ensure an attribute named attr_name exists and it is of type AttrType. +// If so, sets the `out_attr` pointer to point to the casted attribute. +template +bool EnsureAttribute(const DictionaryAttr& composite_attributes, + const std::string& attr_name, AttrType* out_attr) { + Attribute attr = composite_attributes.get(attr_name); + if (!mlir::isa_and_nonnull(attr)) { + return false; + } + if (AttrType content = mlir::dyn_cast(attr)) { + *out_attr = content; + return true; + } else { + return false; + } +} + +// Changes a DenseIntElementsAttr **containing I64** elements to an I32 Vector. +bool DenseI64AttrToI32Vector(const DenseIntElementsAttr& dense_attr, + std::vector* out_vec); + +// Gets boolean from composite attrs if it exists. +std::optional GetBoolFromCompositeAttr( + const DictionaryAttr& composite_attrs, llvm::StringRef attr_name); + +// Given a DictionaryAttr, checks if it has a DenseIntElementsAttr attribute +// with the name attr_name. If so, extracts its values and stores as a vector +// of int32_t elements. +// Note: This assumes the DenseIntElementsAttr has its values stored as int64_t. +bool GetI32VectorFromDenseI64CompositeAttr( + const DictionaryAttr& composite_attrs, const std::string& attr_name, + std::vector* out_vec); + +// Get a DenseIntElementsAttr of type I64 and convert it to an I32 attribute. +DenseIntElementsAttr DenseI64AttrToI32Attr( + const DenseIntElementsAttr& dense_attr, PatternRewriter& builder); + +// Returns a NHWC shaped type from an NCHW shaped type op. +// For example- Given a Composite op that wraps a core.aten.avg_pool2d, this +// returns the return type of the tfl.average_pool_2d emitted. Note that the +// aten.avg_pool2d works with the NCHW layout while tfl.average_pool_2d assumes +// NHWC. +ShapedType GetNhwcReturnTypeFromNchw(Operation* old_op); + +} // namespace odml + +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_COMPOSITE_UTILS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_utils.td b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_utils.td new file mode 100644 index 0000000000000000000000000000000000000000..c0a9894e8bbdb2268ab7f6252257c8ede43bd669 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/composite_utils.td @@ -0,0 +1,63 @@ +/* Copyright 2024 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. +==============================================================================*/ + +#ifndef COMPOSITE_UTILS_TD +#define COMPOSITE_UTILS_TD + +include "mlir/IR/PatternBase.td" + +// See the function doc in the header file. +def GetNhwcReturnTypeFromNchw: NativeCodeCall< + "GetNhwcReturnTypeFromNchw((*$0.begin()).getDefiningOp())">; + +// When given a DenseIntElementsAttr containing I64 elements, this extracts +// one I32IntegerAttr from the given index. +class GetI32At: NativeCodeCall< + "$_builder.getI32IntegerAttr(static_cast(*($0.getValues().begin() + " # index #")))">; + +def GetI32DenseAttr: NativeCodeCall< + "DenseI64AttrToI32Attr($0, $_builder)">; + +// Receives a composite DictionaryAttr and returns the value of the Attribute +// with the key `attr_name` as the type provided by `attr_type`. +class GetCompositeAttributeAs: + NativeCodeCall<"llvm::dyn_cast<" # attr_type # ">($0.get(\"" # attr_name # "\"))">; + +// Receives a composite DictionaryAttr and returns the value of the Attribute +// with the key `attr_name` as a DenseIntElementsAttr. +class GetAsVectorAttr: + GetCompositeAttributeAs; + +class IsBoolAttrEqual : Constraint>; + +// Receives a composite DictionaryAttr as an argument and checks if one of the +// its attributes (with the name `attr_name`) is of type `attribute` and has +// the value `val`. +class IsCompositeAttribute: + Constraint>; + +// Receives a composite DictionaryAttr as an argument and checks if has a +// BoolAttr with the name `attr_name` and value `val`. +class IsBoolCompositeAttribute : + IsCompositeAttribute; + +// Receives a composite DictionaryAttr as an argument and checks if has a +// StrAttr with the name `attr_name` and value `val`. +class IsStrCompositeAttribute : + IsCompositeAttribute; + +#endif diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/drop_savedmodel_semantics.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/drop_savedmodel_semantics.cc new file mode 100644 index 0000000000000000000000000000000000000000..32c42fe329bf90e16fda127806a1380e80cedc4c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/drop_savedmodel_semantics.cc @@ -0,0 +1,83 @@ +/* Copyright 2021 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/compiler/mlir/lite/stablehlo/transforms/drop_savedmodel_semantics.h" + +#include + +#include "llvm/ADT/DenseSet.h" +#include "llvm/ADT/StringRef.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/SymbolTable.h" // from @llvm-project +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_saved_model.h" + +namespace mlir { +namespace odml { +namespace { + +using ::mlir::tf_saved_model::kTfSavedModelExportedNamesAttr; +using ::mlir::tf_saved_model::kTfSavedModelIndexPathAttr; + +} // namespace + +class DropSavedModelSemanticsPass + : public PassWrapper> { + public: + StringRef getArgument() const final { return "drop-savedmodel-semantics"; } + StringRef getDescription() const final { + return "Drops all tf_saved_model attributes"; + } + + // NOTE: The implementation is mostly copypasted from + // third_party/tensorflow/compiler/mlir/tfrt/transforms/lower_saved_model.cc + // with the original code trimmed and adapted as needed. + void runOnOperation() override { + auto module = getOperation(); + if (!tf_saved_model::HasTfSavedModelSemantics(module)) return; + + // Clean up functions from tf_saved_model attributes. + OpBuilder builder(module); + auto bound_input = builder.getStringAttr("tf_saved_model.bound_input"); + auto exported_names = builder.getStringAttr(kTfSavedModelExportedNamesAttr); + auto index_path = builder.getStringAttr(kTfSavedModelIndexPathAttr); + module.walk([&](func::FuncOp func) { + func->removeAttr(exported_names); + for (unsigned i = 0, e = func.getNumArguments(); i != e; ++i) { + if (func.getArgAttrOfType(i, bound_input)) { + func.removeArgAttr(i, bound_input); + } + func.removeArgAttr(i, index_path); + } + for (unsigned i = 0, e = func.getNumResults(); i != e; ++i) { + func.removeResultAttr(i, bound_input); + func.removeResultAttr(i, index_path); + } + }); + + // Clean up modules from tf_saved_model attributes. + module->removeAttr("tf_saved_model.semantics"); + } +}; + +std::unique_ptr CreateDropSavedModelSemanticsPass() { + return std::make_unique(); +} + +static PassRegistration pass; + +} // namespace odml +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/drop_savedmodel_semantics.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/drop_savedmodel_semantics.h new file mode 100644 index 0000000000000000000000000000000000000000..270ed4d9e2b5338ed5ff32db50c29acad5c6d468 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/drop_savedmodel_semantics.h @@ -0,0 +1,31 @@ +/* Copyright 2021 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_DROP_SAVEDMODEL_SEMANTICS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_DROP_SAVEDMODEL_SEMANTICS_H_ + +#include + +#include "mlir/Pass/Pass.h" // from @llvm-project + +namespace mlir { +namespace odml { + +std::unique_ptr CreateDropSavedModelSemanticsPass(); + +} // namespace odml +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_DROP_SAVEDMODEL_SEMANTICS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/hlo_matchers.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/hlo_matchers.cc new file mode 100644 index 0000000000000000000000000000000000000000..69be529e05d37a0a20926b60f205b3d474eef873 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/hlo_matchers.cc @@ -0,0 +1,315 @@ +/* Copyright 2024 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/compiler/mlir/lite/stablehlo/transforms/hlo_matchers.h" + +#include +#include +#include +#include +#include +#include + +#include "llvm/ADT/APInt.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/Matchers.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir { +namespace odml { + +namespace { + +// Facilitate access to 1-d backing data for a tensor so that values in a 1-d +// slice of the tensor can be accessed as if part of an ArrayView. +class StridedArrayViewBase { + protected: + StridedArrayViewBase(ArrayRef shape, ArrayRef index, + int64_t axis) { + assert(shape.size() == index.size()); + assert(axis < shape.size()); + assert(axis >= 0); + assert(index[axis] == 0); + offset_ = IndexToOffset(shape, index); + stride_ = StrideForAxis(shape, axis); + size_ = shape[axis]; + } + + // Returns the size of the 1-d slice across the tensor. + int64_t size() const { return size_; } + + // Calculates the next index in a tensor excluding a specified axis. + // + // Returns the next index where one exists. + // If there is no valid next index, returns `std::nullopt`. + // + // `index` should have the same size as `shape`. + // Each value `dim` in `index` should be in [0, shape[dim]). + static std::optional> NextTensorIndex( + SmallVector index, ArrayRef shape, int64_t fixed_axis) { +#ifndef NDEBUG + assert(shape.size() == index.size()); + assert(fixed_axis < shape.size()); + assert(fixed_axis >= 0); + assert(index[fixed_axis] == 0); + for (size_t i = 0; i < shape.size(); ++i) { + assert(index[i] < shape[i]); + assert(index[i] >= 0); + } +#endif // NDEBUG + for (int64_t dim = shape.size() - 1; dim >= 0; --dim) { + if (dim == fixed_axis) continue; + ++index[dim]; + if (index[dim] < shape[dim]) return std::move(index); + index[dim] = 0; + } + return std::nullopt; + } + + protected: + // Calculates how many values to skip across a 1-D contiguous array that holds + // backing data for a given shape to access the value at a given index along a + // StridedArrayView across a higher dimensional shape. + // + // The index `i` must be in [0, shape[axis])` where `shape` is the shape + // of the tensor and `axis` is the axis along the tensor that the + // StridedArrayView indexes along. + int64_t OffsetForIndex(int64_t i) const { return offset_ + i * stride_; } + + private: + // Calculates how many values to skip across a 1-D contiguous array that holds + // backing data for a given shape to access the next value along a given axis. + // + // `axis` should be a valid dimension in `shape`. + static int64_t StrideForAxis(ArrayRef shape, int64_t axis) { + int64_t stride = 1; // Start with the trailing dimension. + for (int64_t dim = shape.size() - 1; dim > axis; --dim) { + stride *= shape[dim]; + } + return stride; + } + + // Calculates how many values to skip across a 1-D contiguous array that holds + // backing data for a given shape to access data at a specified index. + // + // `index` should have the same size as `shape`. + // Each value `dim` in `index` should be in [0, shape[dim]). + static int64_t IndexToOffset(ArrayRef shape, + ArrayRef index) { +#ifndef NDEBUG + assert(shape.size() == index.size()); + for (size_t i = 0; i < shape.size(); ++i) { + assert(index[i] < shape[i]); + assert(index[i] >= 0); + } +#endif // NDEBUG + int64_t offset = 0; + int64_t stride = 1; + for (int64_t dim = shape.size() - 1; dim >= 0; --dim) { + offset += index[dim] * stride; + stride *= shape[dim]; + } + return offset; + } + + int64_t offset_; + int64_t stride_; + int64_t size_; +}; + +template +class StridedArrayView; // Class requires specialization. + +// Wraps a DenseIntElementsAttr that holds backing data for a tensor so that +// int64_t values in a 1-d slice of the tensor can be accessed as if part of an +// ArrayView. +template <> +class StridedArrayView : StridedArrayViewBase { + public: + StridedArrayView(const DenseIntElementsAttr& data, ArrayRef shape, + ArrayRef index, int64_t axis) + : StridedArrayViewBase(shape, index, axis), data_(data) { + int64_t element_count = 1; + for (int64_t i = 0, e = shape.size(); i < e; ++i) { + element_count *= shape[i]; + } + assert(data.getNumElements() == element_count); + } + + using StridedArrayViewBase::NextTensorIndex; + using StridedArrayViewBase::size; + + int64_t operator[](int64_t i) const { + return data_.getValues()[OffsetForIndex(i)].getSExtValue(); + } + + private: + const DenseIntElementsAttr& data_; +}; + +// It matches %iota generated from the following mlir codes: +// +// %iota_r1 = "mhlo.iota"(){iota_dimension = 0} :() -> tensor +// %iota = "mhlo.broadcast_in_dim(%iota_r1){ +// broadcast_dimensions = dense<[$dimensions[0]]>}, +// +// where %dimensions is of size 1. It ususally comes from an IotaOp that is +// folded to IotaOp (rank1) + BroadCastInDimOp. +bool MatchIotaBroadCastInDim(DenseIntElementsAttr dimensions, Value iota) { + auto iota_broadcast = + dyn_cast_or_null(iota.getDefiningOp()); + if (!iota_broadcast || iota_broadcast.getBroadcastDimensions() != dimensions) + return false; + if (!isa_and_nonnull( + iota_broadcast.getOperand().getDefiningOp())) + return false; + return true; +} + +// Matches %iota generated from the following mlir codes (rank 2 example): +// +// %iota = mhlo.constant dense<[[0, 1, 2, ..., L], +// [0, 1, 2, ..., L] +// ... +// [0, 1, 2, ..., L]]>, +// where $dimensions is of size 1. +// +// StridedArrayViews are used to check the iota property across the constant +// data so that the iota dimension does not need to be the (inner) z-dimension. +bool MatchIotaConst(DenseIntElementsAttr dimensions, Value iota) { + DenseIntElementsAttr iota_const_attr; + if (!matchPattern(iota, m_Constant(&iota_const_attr))) return false; + + auto iota_type = iota_const_attr.getType(); + auto iota_shape = iota_type.getShape(); + auto reduce_dim = (*dimensions.value_begin()).getSExtValue(); + if (reduce_dim < 0) reduce_dim += iota_type.getRank(); + + auto index = + std::optional>(std::in_place, iota_type.getRank()); + while (index.has_value()) { + StridedArrayView array_view( + iota_const_attr, iota_shape, *index, reduce_dim); + for (int64_t i = 0; i < array_view.size(); ++i) { + if (array_view[i] != i) return false; + } + index = StridedArrayView::NextTensorIndex( + std::move(*index), iota_shape, reduce_dim); + } + + return true; +} + +// Matches %iota generated from the following code (rank 3 example): +// +// %iota_r1 = "mhlo.iota"(){iota_dimension = 0 : i32} : () -> tensor<44xi32> +// %iota = "mhlo.reshape"(%iota_r1): (tensor<44xi32>) -> tensor<1x1x44xi32> +// +// Where $dimensions is of size 1 and $dimensions[0] = 2. +// +// In general matches a 1-D Iota with multiple dimensions of size 1 added +// through a reshape. +bool MatchReshapedIota(DenseIntElementsAttr dimensions, Value iota) { + if (dimensions.getNumElements() != 1) return false; + auto reshape_op = dyn_cast_or_null(iota.getDefiningOp()); + if (!reshape_op) return false; + auto operand_type = + mlir::dyn_cast(reshape_op.getOperand().getType()); + if (!operand_type || !operand_type.hasStaticShape()) return false; + auto reshape_type = mlir::cast(reshape_op.getType()); + + // Reshape can take a 1-D iota input and add extra dims of size one. + if (operand_type.getRank() != 1) return false; + if (!dyn_cast_or_null(reshape_op.getOperand().getDefiningOp())) + return false; + + int64_t iota_dim = (*dimensions.value_begin()).getSExtValue(); + for (int64_t i = 0, e = reshape_type.getRank(); i < e; ++i) { + if (i == iota_dim) { + if (reshape_type.getDimSize(i) != operand_type.getDimSize(0)) + return false; + } else if (reshape_type.getDimSize(i) != 1) { + return false; + } + } + return true; +} + +// It returns "true" when Value $iota is obtained from the following mlir code: +// +// $iota = "mhlo.iota"(){iota_dimension = $dimensions[0]}, +// +// where $dimensions must have size 1 and iota can have rank>=1. +// It usually used for matching rank 1 iota since the iotaOp will be folded to +// IotaOp + BroadCastInDimOp except for the case when result shape is rank 1. +bool MatchSingleIota(DenseIntElementsAttr dimensions, Value iota) { + auto iota_op = dyn_cast_or_null(iota.getDefiningOp()); + if (!iota_op || dimensions.getNumElements() != 1) return false; + auto dim = *dimensions.value_begin(); + return dim == iota_op.getIotaDimension(); +} + +// It matches %iota generated from the following mlir codes: +// +// %iota_r1 = mhlo.constant dense<[0, 1, 2, ..., L]> +// %iota = "mhlo.broadcast_in_dim(%iota_r1){ +// broadcast_dimensions = dense<[$dimensions[0]]>}, +// +// where $dimensions is of size 1. It ususally comes from an IotaOp that is +// folded to ConstOp (folded rank1 iota) + BroadCastInDimOp. +bool MatchConstIotaBroadCastInDim(DenseIntElementsAttr dimensions, Value iota) { + if (dimensions.getNumElements() != 1) return false; + auto iota_broadcast = + dyn_cast_or_null(iota.getDefiningOp()); + if (!iota_broadcast || iota_broadcast.getBroadcastDimensions() != dimensions) + return false; + DenseElementsAttr range_const; + if (!matchPattern(iota_broadcast.getOperand(), m_Constant(&range_const))) + return false; + int index = 0; + for (auto value : range_const.getValues()) { + if (value != index++) return false; + } + return true; +} +} // namespace + +// The following 5 different forms of mhlo::iota will be matched: +// 1. IotaOp. +// 2. IotaOp + BroadCastInDim. +// 3. IotaOp + Reshape. +// 4. Constant (folded Iota) + BroadCastInDim. +// 5. Constant (folded result). +// Moreover, the dimensions has to match the iota_dimension. +bool MatchIota(DenseIntElementsAttr dimensions, Value iota) { + return MatchSingleIota(dimensions, iota) || + MatchIotaBroadCastInDim(dimensions, iota) || + MatchReshapedIota(dimensions, iota) || + MatchConstIotaBroadCastInDim(dimensions, iota) || + MatchIotaConst(dimensions, iota); +} +} // namespace odml +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/hlo_matchers.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/hlo_matchers.h new file mode 100644 index 0000000000000000000000000000000000000000..2eba8e32c8b09f013aea3c410be9c6f6da25fb2f --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/hlo_matchers.h @@ -0,0 +1,35 @@ +/* Copyright 2024 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_HLO_MATCHERS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_HLO_MATCHERS_H_ + +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project + +namespace mlir { +namespace odml { +// The following 5 different forms of mhlo::iota will be matched: +// 1. IotaOp. +// 2. IotaOp + BroadCastInDim. +// 3. IotaOp + Reshape. +// 4. Constant (folded Iota) + BroadCastInDim. +// 5. Constant (folded result). +// Moreover, the dimensions has to match the iota_dimension. +bool MatchIota(DenseIntElementsAttr dimensions, Value iota); +} // namespace odml +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_HLO_MATCHERS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo.cc new file mode 100644 index 0000000000000000000000000000000000000000..46599616e957e0db01d7ba903c27c736ce5338f1 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo.cc @@ -0,0 +1,3926 @@ +/* Copyright 2023 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. +==============================================================================*/ + +// This file implements logic for legalizing HLO to TensorFlow. + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "absl/status/status.h" +#include "llvm/ADT/APFloat.h" +#include "llvm/ADT/APInt.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/Sequence.h" +#include "llvm/ADT/SmallSet.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/ilist.h" +#include "llvm/Support/Casting.h" +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Dialect/Quant/IR/QuantTypes.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/Diagnostics.h" // from @llvm-project +#include "mlir/IR/ImplicitLocOpBuilder.h" // from @llvm-project +#include "mlir/IR/Location.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/Matchers.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/Region.h" // from @llvm-project +#include "mlir/IR/SymbolTable.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/IR/ValueRange.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "stablehlo/dialect/BroadcastUtils.h" // from @stablehlo +#include "stablehlo/dialect/ChloOps.h" // from @stablehlo +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/hlo_matchers.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/reduce.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/util.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h" +#include "tensorflow/compiler/mlir/lite/utils/utils.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_dialect.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" +#include "tensorflow/core/framework/kernel_shape_util.h" +#include "tensorflow/core/lib/math/math_util.h" +#include "tensorflow/core/platform/status.h" +#include "tensorflow/core/util/padding.h" + +namespace mlir { +namespace odml { +namespace { + +#define DEBUG_TYPE "tf-legalize-hlo" + +#define GEN_PASS_DEF_LEGALIZEHLOTOTFPASS +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h.inc" + +class LegalizeHloToTf : public impl::LegalizeHloToTfPassBase { + /// Performs the legalization to the TF dialect. + void runOnOperation() override; +}; + +using mhlo::DotDimensionNumbersAttr; + +// Replaces `region`'s terminator to TF::Yield. +void ReplaceReturnOp(Region& region, PatternRewriter& rewriter) { + OpBuilder::InsertionGuard guard(rewriter); + + for (auto& block : region.getBlocks()) { + Operation* terminator = block.getTerminator(); + auto return_op = llvm::dyn_cast_or_null(terminator); + if (return_op == nullptr) continue; + + rewriter.setInsertionPoint(return_op); + rewriter.replaceOpWithNewOp(return_op, + return_op->getOperands()); + } +} + +// If `value` is a splat constant, returns a success and set `splat_value` +// to the splate constant value. +// `SplatValueType` can be `APInt` or `APFloat`. +template +LogicalResult GetConstantSplatValue(Value value, SplatValueType& splat_value) { + DenseElementsAttr attr; + if (!matchPattern(value, m_Constant(&attr)) || !attr.isSplat()) { + return failure(); + } + + splat_value = attr.getSplatValue(); + return success(); +} + +// Checks for attributes with no value and sets the default value according to +// the convolution op definition. +void SetDefaultConvAttributes(mhlo::ConvolutionOp& conv_op, + ConversionPatternRewriter& rewriter) { + auto dnums = conv_op.getDimensionNumbers(); + int32_t input_spatial_dims = + static_cast(dnums.getInputSpatialDimensions().size()); + if (!conv_op.getWindowStrides().has_value()) { + conv_op.setWindowStridesAttr( + rewriter.getI64TensorAttr(std::vector(input_spatial_dims, 1))); + } + if (!conv_op.getPadding().has_value()) { + conv_op.setPaddingAttr(DenseIntElementsAttr::get( + RankedTensorType::get({input_spatial_dims, 2}, rewriter.getI64Type()), + SmallVector(input_spatial_dims * 2, 0))); + } + if (!conv_op.getLhsDilation().has_value()) { + conv_op.setLhsDilationAttr( + rewriter.getI64TensorAttr(std::vector(input_spatial_dims, 1))); + } + if (!conv_op.getRhsDilation().has_value()) { + conv_op.setRhsDilationAttr( + rewriter.getI64TensorAttr(std::vector(input_spatial_dims, 1))); + } +} + +template +class ConvertNdConvOp : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::ConvolutionOp conv_op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final { + SetDefaultConvAttributes(conv_op, rewriter); + if (!IsSupportedConvOp(conv_op)) { + return failure(); + } + + // tf Convolution doesn't support quantized type or 8-bit integer type. + Type rhs_element_type = conv_op.getRhs().getType().getElementType(); + if (mlir::isa(rhs_element_type) || + rhs_element_type.isInteger(8)) { + return failure(); + } + + // Constructs strides array. + // For example, [2, 3] -> [1, 2, 3, 1]. + SmallVector strides({1}); + for (const auto v : + conv_op.getWindowStrides().value().getValues()) { + strides.emplace_back(v); + } + strides.emplace_back(1); + + // Constructs dilation array. + SmallVector dilation; + if (auto rhs_dilation = conv_op.getRhsDilation()) { + // For example, [2, 3] -> [1, 2, 3, 1]. + dilation.emplace_back(1); + dilation.append(rhs_dilation.value().getValues().begin(), + rhs_dilation.value().getValues().end()); + dilation.emplace_back(1); + } + + mhlo::ConvDimensionNumbersAttr dnums = conv_op.getDimensionNumbers(); + const int input_feature_dimension = dnums.getInputFeatureDimension(); + const int kernel_input_feature_dimension = + dnums.getKernelInputFeatureDimension(); + const int input_channels = + mlir::cast(conv_op.getLhs().getType()) + .getDimSize(input_feature_dimension); + const int kernel_input_channels = + mlir::cast(conv_op.getRhs().getType()) + .getDimSize(kernel_input_feature_dimension); + int feature_group_count = conv_op.getFeatureGroupCount(); + + // check if group count is valid + if (feature_group_count != input_channels / kernel_input_channels || + input_channels % kernel_input_channels != 0) { + return failure(); + } + + const bool is_depthwise_conv = input_channels == feature_group_count; + std::string padding; + SmallVector explicit_padding; + if (conv_op.getPadding().value().isSplat() && + conv_op.getPadding()->getSplatValue() == 0) { + padding = "VALID"; + } else { + SmallVector padding_array; + for (const auto v : conv_op.getPadding().value().getValues()) { + padding_array.emplace_back(v); + } + + if (IsSamePadding(conv_op, strides, dilation, padding_array)) { + // Check if padding is "SAME". + padding = "SAME"; + } else { + padding = "EXPLICIT"; + explicit_padding.push_back(0); + explicit_padding.push_back(0); + explicit_padding.append(padding_array); + explicit_padding.push_back(0); + explicit_padding.push_back(0); + } + } + + CreateConvOp(conv_op, strides, padding, explicit_padding, dilation, + is_depthwise_conv, input_channels, rewriter); + + return success(); + }; + + static bool IsSupportedConvOp(mhlo::ConvolutionOp conv_op) { + if (!mlir::cast(conv_op.getRhs().getType()).hasStaticShape()) { + return false; + } + if (!mlir::cast(conv_op.getLhs().getType()).hasStaticShape() && + !mlir::cast(conv_op.getType()).hasStaticShape()) { + auto dnums = conv_op.getDimensionNumbers(); + auto lhs_type = mlir::cast(conv_op.getLhs().getType()); + auto out_type = mlir::cast(conv_op.getType()); + int64_t input_batch_dim = dnums.getInputBatchDimension(); + int64_t out_batch_dim = dnums.getOutputBatchDimension(); + for (size_t i = 0; i < lhs_type.getRank(); ++i) { + // is this upcast of size_t or downcast + if ((i != input_batch_dim && lhs_type.isDynamicDim(i)) || + (i != out_batch_dim && out_type.isDynamicDim(i))) { + return false; + } + } + } + + // All ones in "lhs_dilation" means this "mhlo.conv" op should be + // converted to "tf.Conv2D" or "tf.DepthwiseConv2dNativeOp". + auto lhs_dilation = conv_op.getLhsDilation().value(); + if (!lhs_dilation.isSplat() || lhs_dilation.getSplatValue() != 1) + return false; + + if (mlir::cast(conv_op.getWindowStrides().value().getType()) + .getRank() != 1) + return false; + + auto spatial_dims = + conv_op.getDimensionNumbers().getInputSpatialDimensions().size(); + if (spatial_dims != num_spatial_dims) return false; + + return true; + } + + private: + bool IsSamePadding(mhlo::ConvolutionOp conv_op, ArrayRef strides, + ArrayRef dilation, + ArrayRef padding_array) const { + mhlo::ConvDimensionNumbersAttr dnums = conv_op.getDimensionNumbers(); + auto input_spatial_dim = dnums.getInputSpatialDimensions(); + auto kernel_spatial_dim = dnums.getKernelSpatialDimensions(); + for (auto i : llvm::seq(0, num_spatial_dims)) { + int dim = i + 1; + int64_t output_size; + int64_t pad_low_int64; + int64_t pad_high_int64; + absl::Status status = tensorflow::GetWindowedOutputSizeVerbose( + mlir::cast(conv_op.getLhs().getType()) + .getDimSize(input_spatial_dim[i]), + mlir::cast(conv_op.getRhs().getType()) + .getDimSize(kernel_spatial_dim[i]), + dilation[dim], strides[dim], tensorflow::Padding::SAME, &output_size, + &pad_low_int64, &pad_high_int64); + if (!status.ok()) return false; + if (padding_array[2 * i] != pad_low_int64 || + padding_array[2 * i + 1] != pad_high_int64) + return false; + } + + return true; + } + + // Slices the input `value` if there are negative padding values in + // `explicit_padding`. + Value SliceNegativePadding(Value value, ArrayRef explicit_padding, + ConversionPatternRewriter& rewriter) const { + // If no padding is negative return the input as is. + if (llvm::all_of(explicit_padding, [](int64_t pad) { return pad >= 0; })) { + return value; + } + + auto input_type = mlir::cast(value.getType()); + auto input_shape = input_type.getShape(); + + llvm::SmallVector start; + llvm::SmallVector size; + start.reserve(explicit_padding.size() / 2); + size.reserve(explicit_padding.size() / 2); + for (int i = 0, e = explicit_padding.size() / 2; i < e; ++i) { + int64_t pre_padding = explicit_padding[2 * i]; + int64_t post_padding = explicit_padding[2 * i + 1]; + int64_t pre_slice = pre_padding < 0 ? -pre_padding : 0; + int64_t post_slice = post_padding < 0 ? -post_padding : 0; + start.push_back(pre_slice); + size.push_back(input_shape[i] - pre_slice - post_slice); + } + + auto start_attr = TF::ConstOp::create( + rewriter, value.getLoc(), + DenseIntElementsAttr::get( + RankedTensorType::get({static_cast(start.size())}, + rewriter.getI64Type()), + start)); + auto size_attr = TF::ConstOp::create( + rewriter, value.getLoc(), + DenseIntElementsAttr::get( + RankedTensorType::get({static_cast(size.size())}, + rewriter.getI64Type()), + size)); + auto output_type = RankedTensorType::get(size, input_type.getElementType()); + + return TF::SliceOp::create(rewriter, value.getLoc(), output_type, value, + start_attr, size_attr); + } + + void CreateConvOp(mhlo::ConvolutionOp conv_op, ArrayRef strides, + StringRef padding, ArrayRef explicit_padding, + ArrayRef dilation, bool is_depthwise_conv, + int input_channels, + ConversionPatternRewriter& rewriter) const { + mhlo::ConvDimensionNumbersAttr dnums = conv_op.getDimensionNumbers(); + // Transposes lhs and rhs if their formats are not NHWC. + Value lhs = InsertTranspose( + conv_op.getLhs(), dnums.getInputBatchDimension(), + dnums.getInputFeatureDimension(), dnums.getInputSpatialDimensions(), + /*default_batch_dim=*/0, /*default_feature_dim=*/num_spatial_dims + 1, + /*default_spatial_dim_start=*/1, num_spatial_dims, rewriter); + + Value rhs = InsertTranspose( + conv_op.getRhs(), dnums.getKernelInputFeatureDimension(), + dnums.getKernelOutputFeatureDimension(), + dnums.getKernelSpatialDimensions(), + /*default_batch_dim=*/num_spatial_dims, + /*default_feature_dim=*/num_spatial_dims + 1, + /*default_spatial_dim_start=*/0, num_spatial_dims, rewriter); + + // Emulate negative padding with a slice and remove negative values from the + // padding vector. + Value sliced_lhs = SliceNegativePadding(lhs, explicit_padding, rewriter); + auto new_padding = + llvm::to_vector<(num_spatial_dims * 2) + 4>(llvm::map_range( + explicit_padding, [](int64_t dim) { return dim > 0 ? dim : 0; })); + + // Add an TF.PadOp before the LHS if there is explicit_padding on a 3D + // Convolution. This is needed because TF.Conv3DOp doesn't support EXPLICIT. + if (padding == "EXPLICIT" && num_spatial_dims == 3) { + auto lhs_type = + mlir::dyn_cast(conv_op.getLhs().getType()); + RankedTensorType padding_attr_type = mlir::RankedTensorType::get( + {lhs_type.getRank(), 2}, rewriter.getIntegerType(64)); + auto padding_const = TF::ConstOp::create( + rewriter, conv_op->getLoc(), + mlir::DenseElementsAttr::get(padding_attr_type, + ArrayRef(new_padding))); + // Add Pad op. + auto pad_output_type = UnrankedTensorType::get(lhs_type.getElementType()); + sliced_lhs = + TF::PadOp::create(rewriter, conv_op->getLoc(), pad_output_type, + sliced_lhs, padding_const); + padding = "VALID"; + } + + auto conv_output_type = mlir::cast(conv_op.getType()); + DenseIntElementsAttr permutation; + const bool need_transpose_output = NeedsReformatTypeAndPermutation( + dnums.getOutputBatchDimension(), dnums.getOutputFeatureDimension(), + dnums.getOutputSpatialDimensions().front(), + /*default_batch_dim=*/0, /*default_feature_dim=*/num_spatial_dims + 1, + /*default_spatial_dim_start=*/1); + if (need_transpose_output) { + std::pair(conv_output_type, + permutation) = + GetReformatTypeAndPermutation( + dnums.getOutputBatchDimension(), + dnums.getOutputFeatureDimension(), + dnums.getOutputSpatialDimensions().front(), + /*default_batch_dim=*/0, + /*default_feature_dim=*/num_spatial_dims + 1, + /*default_spatial_dim_start=*/1, num_spatial_dims, + conv_output_type, rewriter); + } + Value output; + if (is_depthwise_conv && num_spatial_dims == 2) { + // Reshapes filter format to [filter_height, filter_width, in_channels, + // channel_multiplier] from HLO's [filter_height, filter_width, 1, + // in_channels * channel_multiplier] format. + auto filter_type = mlir::cast(rhs.getType()); + llvm::ArrayRef hlo_filter_shape = filter_type.getShape(); + llvm::SmallVector tf_filter_shape(hlo_filter_shape.begin(), + hlo_filter_shape.end()); + tf_filter_shape[2] = input_channels; + tf_filter_shape[3] = hlo_filter_shape.back() / input_channels; + auto reshaped_filter = mhlo::ReshapeOp::create( + rewriter, rhs.getLoc(), + RankedTensorType::get(tf_filter_shape, filter_type.getElementType()), + rhs); + + output = TF::DepthwiseConv2dNativeOp::create( + rewriter, conv_op.getLoc(), conv_output_type, sliced_lhs, + reshaped_filter, rewriter.getI64ArrayAttr(strides), + /*padding=*/rewriter.getStringAttr(padding), + /*explicit_paddings=*/rewriter.getI64ArrayAttr(new_padding), + /*data_format=*/rewriter.getStringAttr("NHWC"), + /*dilations=*/rewriter.getI64ArrayAttr(dilation)); + } else if (num_spatial_dims == 3) { + output = TF::Conv3DOp::create( + rewriter, conv_op.getLoc(), conv_output_type, sliced_lhs, rhs, + rewriter.getI64ArrayAttr(strides), + /*padding=*/rewriter.getStringAttr(padding), + /*data_format=*/rewriter.getStringAttr("NDHWC"), + /*dilations=*/rewriter.getI64ArrayAttr(dilation)); + } else { + output = TF::Conv2DOp::create( + rewriter, conv_op.getLoc(), conv_output_type, sliced_lhs, rhs, + rewriter.getI64ArrayAttr(strides), + /*use_cudnn_on_gpu=*/rewriter.getBoolAttr(true), + /*padding=*/rewriter.getStringAttr(padding), + /*explicit_paddings=*/rewriter.getI64ArrayAttr(new_padding), + /*data_format=*/rewriter.getStringAttr("NHWC"), + /*dilations=*/rewriter.getI64ArrayAttr(dilation)); + } + + if (need_transpose_output) { + // Converts from "NHWC" format back to the original output format. + std::pair(conv_output_type, + permutation) = + GetReformatTypeAndPermutation( + /*batch_dim=*/0, /*feature_dim=*/num_spatial_dims + 1, + /*spatial_dim_start=*/1, dnums.getOutputBatchDimension(), + dnums.getOutputFeatureDimension(), + *dnums.getOutputSpatialDimensions().begin(), num_spatial_dims, + conv_output_type, rewriter); + output = mhlo::TransposeOp::create( + rewriter, conv_op.getLoc(), conv_op.getType(), output, permutation); + } + rewriter.replaceOp(conv_op, {output}); + } +}; + +// Convert a 1-D convolution into a 2-D convolution (which TF supports) so that +// it can be rewritten by the pattern `Convert2DConvOp`. +class Convert1DConvOp : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::ConvolutionOp conv_op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final { + SetDefaultConvAttributes(conv_op, rewriter); + // Check that input is a supported 1d convolution. + if (!ConvertNdConvOp<1>::IsSupportedConvOp(conv_op) || + conv_op->getNumResults() != 1) + return rewriter.notifyMatchFailure(conv_op, "unsupported conv op."); + + const mhlo::ConvDimensionNumbersAttr dnums = conv_op.getDimensionNumbers(); + + // Group convolution is not supported yet. + const int64_t input_feature_dimension = dnums.getInputFeatureDimension(); + const int64_t input_channels = + mlir::cast(conv_op.getLhs().getType()) + .getDimSize(input_feature_dimension); + const int kernel_input_feature_dimension = + dnums.getKernelInputFeatureDimension(); + const int kernel_input_channels = + mlir::cast(conv_op.getRhs().getType()) + .getDimSize(kernel_input_feature_dimension); + const int64_t feature_group_count = conv_op.getFeatureGroupCount(); + if (feature_group_count != input_channels / kernel_input_channels || + input_channels % kernel_input_channels != 0) + return failure(); + + // + // Transpose and reshape the input and kernel + // + + // Reshape input image to add a new spatial dimension. + auto image_type = mlir::cast(conv_op.getLhs().getType()); + SmallVector image_2d_shape(image_type.getShape().begin(), + image_type.getShape().end()); + image_2d_shape.push_back(1); + auto image_2d_type = + RankedTensorType::get(image_2d_shape, image_type.getElementType()); + auto loc = conv_op.getLoc(); + auto image_2d_op = mhlo::ReshapeOp::create(rewriter, conv_op.getLoc(), + image_2d_type, conv_op.getLhs()); + + // Transpose image to get it into NWHC form (where H is the added dim). + SmallVector image_permutation = { + dnums.getInputBatchDimension(), dnums.getInputSpatialDimensions()[0], + 3, // The trailing dim that we added. + dnums.getInputFeatureDimension()}; + auto image_permutation_and_shape = GetPermutationAndTransposedShape( + image_permutation, image_2d_type, rewriter); + auto transposed_image_2d_op = mhlo::TransposeOp::create( + rewriter, loc, image_permutation_and_shape.shape, + image_2d_op->getResult(0), image_permutation_and_shape.permutation); + + // Reshape kernel to add a new spatial dimension. + auto kernel_type = mlir::cast(conv_op.getRhs().getType()); + SmallVector kernel_2d_shape; + for (int64_t dim : kernel_type.getShape()) { + kernel_2d_shape.push_back(dim); + } + kernel_2d_shape.push_back(1); + auto kernel_2d_type = + RankedTensorType::get(kernel_2d_shape, kernel_type.getElementType()); + auto kernel_2d_op = mhlo::ReshapeOp::create(rewriter, loc, kernel_2d_type, + conv_op.getRhs()); + + // Transpose kernel to get it into WHIO form (where H is the added dim). + SmallVector kernel_permutation = { + dnums.getKernelSpatialDimensions()[0], + 3, // The trailing dim that we added. + dnums.getKernelInputFeatureDimension(), + dnums.getKernelOutputFeatureDimension()}; + auto kernel_permutation_and_shape = GetPermutationAndTransposedShape( + kernel_permutation, kernel_2d_type, rewriter); + auto transposed_kernel_2d_op = mhlo::TransposeOp::create( + rewriter, loc, kernel_permutation_and_shape.shape, + kernel_2d_op->getResult(0), kernel_permutation_and_shape.permutation); + + // + // Create 2d equivalents for 1d convolution attributes. + // + + // Window Strides + SmallVector window_strides_2d_array; + for (const auto v : conv_op.getWindowStrides()->getValues()) { + window_strides_2d_array.emplace_back(v); + } + window_strides_2d_array.push_back(1); + auto window_strides_2d = DenseIntElementsAttr::get( + RankedTensorType::get({2}, rewriter.getI64Type()), + window_strides_2d_array); + + // Padding + SmallVector padding_2d_array; + for (const auto v : conv_op.getPadding().value().getValues()) { + padding_2d_array.emplace_back(v); + } + // The newly added spatial dimension requires zero left and right padding. + padding_2d_array.push_back(0); + padding_2d_array.push_back(0); + auto padding_2d = DenseIntElementsAttr::get( + RankedTensorType::get({2, 2}, rewriter.getI64Type()), padding_2d_array); + + // LHS dilation + SmallVector lhs_dilation_array_2d; + for (const auto v : conv_op.getLhsDilation().value().getValues()) { + lhs_dilation_array_2d.emplace_back(v); + } + lhs_dilation_array_2d.push_back(1); + auto lhs_dilation_2d = DenseIntElementsAttr::get( + RankedTensorType::get({2}, rewriter.getI64Type()), + lhs_dilation_array_2d); + + // RHS dilation + SmallVector rhs_dilation_array_2d; + for (const auto v : conv_op.getRhsDilation().value().getValues()) { + rhs_dilation_array_2d.emplace_back(v); + } + rhs_dilation_array_2d.push_back(1); + auto rhs_dilation_2d = DenseIntElementsAttr::get( + RankedTensorType::get({2}, rewriter.getI64Type()), + rhs_dilation_array_2d); + + // Window reversal is unsupported. + if (conv_op.getWindowReversal().has_value() && + conv_op.getWindowReversal()->getValues()[0] == true) + return failure(); + auto window_reversal_2d = DenseIntElementsAttr::get( + RankedTensorType::get({2}, rewriter.getI64Type()), + SmallVector({0, 0})); + + // Dimension numbers reflect the form of the 2d conv op NWHC * WHIO -> NWHC + auto dnums_2d = + mhlo::ConvDimensionNumbersAttr::get(rewriter.getContext(), + /*inputBatchDimension=*/0, + /*inputFeatureDimension=*/3, + /*inputSpatialDimensions=*/{1, 2}, + /*kernelInputDimension=*/2, + /*kernelOutputDimension=*/3, + /*kernelSpatialDimensions=*/{0, 1}, + /*outputBatchDimension=*/0, + /*outputFeatureDimension=*/3, + /*outputSpatialDimensions=*/{1, 2}); + // + // Generate a 2-D convolution + // + + // Determine the 2-D convolution output shape. + auto output_type = mlir::cast(conv_op->getResult(0).getType()); + SmallVector output_2d_shape; + for (int64_t dim : output_type.getShape()) { + output_2d_shape.push_back(dim); + } + output_2d_shape.push_back(1); + auto output_2d_type = + RankedTensorType::get(output_2d_shape, output_type.getElementType()); + SmallVector output_permutation = { + dnums.getOutputBatchDimension(), dnums.getOutputSpatialDimensions()[0], + 3, // The trailing dim that we added. + dnums.getOutputFeatureDimension()}; + auto transposed_output_2d_shape = + GetPermutationAndTransposedShape(output_permutation, output_2d_type, + rewriter) + .shape; + + auto conv2d_op = mhlo::ConvolutionOp::create( + rewriter, loc, transposed_output_2d_shape, + transposed_image_2d_op.getResult(), transposed_kernel_2d_op.getResult(), + window_strides_2d, padding_2d, lhs_dilation_2d, rhs_dilation_2d, + window_reversal_2d, dnums_2d, conv_op.getFeatureGroupCount(), + conv_op.getBatchGroupCount(), conv_op.getPrecisionConfigAttr()); + + OpResult conv2d_output = conv2d_op->getResult(0); + auto conv2d_output_type = mlir::cast(conv2d_output.getType()); + + // + // Transpose and reshape the output + // + + // Since output is in NWHC form we need to undo the permutation we have + // affectively applied. + auto output_permutation_and_shape = GetInversePermutationAndShape( + output_permutation, conv2d_output_type, rewriter); + auto transposed_output_2d_op = mhlo::TransposeOp::create( + rewriter, loc, output_permutation_and_shape.shape, conv2d_output, + output_permutation_and_shape.permutation); + + // Drop the trailing spatial dimension from the output. + rewriter.replaceOpWithNewOp( + conv_op, output_type, transposed_output_2d_op.getResult()); + return success(); + } +}; + +using Convert2DConvOp = ConvertNdConvOp<2>; +using Convert3DConvOp = ConvertNdConvOp<3>; + +// Utility function to check for supported non-trivial convolutions with +// lhs_dilation>1 and window_strides=1. +LogicalResult IsSupportedNonTrivialConvOp(mhlo::ConvolutionOp conv_op, + ConversionPatternRewriter& rewriter) { + if (!mlir::cast(conv_op.getLhs().getType()).hasStaticShape() || + !mlir::cast(conv_op.getRhs().getType()).hasStaticShape() || + !mlir::cast(conv_op.getType()).hasStaticShape()) + return rewriter.notifyMatchFailure(conv_op, "requires static shape"); + mhlo::ConvDimensionNumbersAttr dnums = conv_op.getDimensionNumbers(); + + auto lhs_dilation = conv_op.getLhsDilation().value(); + if (lhs_dilation.isSplat() && lhs_dilation.getSplatValue() == 1) + return rewriter.notifyMatchFailure(conv_op, + "requires non-trivial lhs_dilation"); + + if (mlir::cast(conv_op.getWindowStrides().value().getType()) + .getRank() != 1) + return rewriter.notifyMatchFailure( + conv_op, "requires window_strides to equal to one"); + + int num_spatial_dims = dnums.getInputSpatialDimensions().size(); + if (num_spatial_dims != 2) + return rewriter.notifyMatchFailure(conv_op, "doesn't support more than 2D"); + + if (llvm::any_of(conv_op.getPadding().value().getValues(), + [](int64_t v) { return v < 0; })) { + return rewriter.notifyMatchFailure(conv_op, + "doesn't support negative pads"); + } + + return success(); +} + +DenseIntElementsAttr GetI64ElementsAttr(ArrayRef values, + Builder* builder) { + RankedTensorType ty = RankedTensorType::get( + {static_cast(values.size())}, builder->getIntegerType(64)); + return DenseIntElementsAttr::get(ty, values); +} + +class ConvertToResizeBilinearOpOrDepthwiseTransposedConvOp + : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + LogicalResult matchAndRewrite( + mhlo::ConvolutionOp conv_op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final { + SetDefaultConvAttributes(conv_op, rewriter); + + // IsSupportedNonTrivialConvOp checks for supported lhs_dilation values. + if (IsSupportedNonTrivialConvOp(conv_op, rewriter).failed()) { + return rewriter.notifyMatchFailure( + conv_op, + "doesn't support conversion to ResizeBilinearOp or " + "ConvBackpropInputOp"); + } + + // tf.ResizeBilinearOp is perferred than tf.Conv2DBackpropInputOp since + // the former has better portability, especially in inference use cases. + bool align_corners; + llvm::SmallVector output_sizes; + if (MatchToResizeBilinearOp(conv_op, align_corners, output_sizes, rewriter) + .succeeded()) { + CreateResizeBilinearOp(conv_op, output_sizes, align_corners, rewriter); + return success(); + } + + // These checks narrow down the support to depthwise transpose conv2d. + mhlo::ConvDimensionNumbersAttr dnums = conv_op.getDimensionNumbers(); + const int input_feature_dimension = dnums.getInputFeatureDimension(); + const int input_channels = + mlir::cast(conv_op.getLhs().getType()) + .getDimSize(input_feature_dimension); + int feature_group_count = conv_op.getFeatureGroupCount(); + const int kernel_input_feature_dimension = + dnums.getKernelInputFeatureDimension(); + const int kernel_input_channels = + mlir::cast(conv_op.getRhs().getType()) + .getDimSize(kernel_input_feature_dimension); + const int kernel_output_feature_dimension = + dnums.getKernelOutputFeatureDimension(); + const int kernel_output_channels = + mlir::cast(conv_op.getRhs().getType()) + .getDimSize(kernel_output_feature_dimension); + + // To support a depthwise convolution, we need- + // 1. feature_group_count != 1 (except when input_channels==1) + // 2. feature_group_count == input_channels + // 3. kernel_input_channels == 1 + // 4. kernel_output_channels % kernel_input_channels == 0 + if (feature_group_count == 1) { + return rewriter.notifyMatchFailure(conv_op, + "Not a depthwise convolution"); + } + + if (input_channels != feature_group_count) { + return rewriter.notifyMatchFailure( + conv_op, "Not a detphwise transposed convolution"); + } + + if ((kernel_output_channels % feature_group_count != 0) || + (kernel_input_channels != 1)) { + return rewriter.notifyMatchFailure( + conv_op, "Not a supported detphwise transposed convolution"); + } + + // This needs to be checked because the TFLite runtime generated incorrect + // results for depthwise transpose convolutions with non-1 channel + // multiplier. + if ((kernel_output_channels / feature_group_count) != 1) { + return rewriter.notifyMatchFailure( + conv_op, + "Unsupported detphwise transpose convolution with non-1 channel " + "multiplier"); + } + + // Slicing with dynamic offsets (helper method advised) + auto create_slice = [&](mlir::Value tensor, int depth_idx, int channel_idx, + bool is_kernel = false) -> mlir::Value { + std::vector tensor_shape = + mlir::cast(tensor.getType()).getShape().vec(); + + // Calculate offsets based on depth_idx, channel_idx and tensor_shape + std::vector start_indices(tensor_shape.size(), 0); + std::vector limit_indices = tensor_shape; + const std::vector strides(tensor_shape.size(), 1); + start_indices[channel_idx] = depth_idx; + if (is_kernel) { + // kernel can have a channel_multiplier that needs to be accounted for + limit_indices[channel_idx] = + depth_idx + (kernel_output_channels / feature_group_count); + } else { + limit_indices[channel_idx] = depth_idx + 1; + } + return mhlo::SliceOp::create(rewriter, conv_op.getLoc(), tensor, + GetI64ElementsAttr(start_indices, &rewriter), + GetI64ElementsAttr(limit_indices, &rewriter), + GetI64ElementsAttr(strides, &rewriter)); + }; + + // Storage for smaller convolution results + std::vector conv_results; + + // Iterative Slicing and Convolutions + for (int i = 0; i < feature_group_count; ++i) { + auto sliced_input = + create_slice(conv_op.getLhs(), i, input_feature_dimension); + auto sliced_kernel = create_slice(conv_op.getRhs(), i, + kernel_output_feature_dimension, true); + + // Calculate convolution output_type based on sliced_input and + // sliced_kernel + auto output_type = + mlir::cast(conv_op->getResult(0).getType()); + std::vector new_output_shape = output_type.getShape().vec(); + new_output_shape[dnums.getOutputFeatureDimension()] /= + feature_group_count; + auto new_output_type = + RankedTensorType::get(new_output_shape, output_type.getElementType()); + + // Create a Smaller Convolution (Ensure compatibility) + auto conv_result = mhlo::ConvolutionOp::create( + rewriter, conv_op.getLoc(), new_output_type, sliced_input, + sliced_kernel, conv_op.getWindowStridesAttr(), + conv_op.getPaddingAttr(), conv_op.getLhsDilationAttr(), + conv_op.getRhsDilationAttr(), conv_op.getWindowReversalAttr(), + conv_op.getDimensionNumbers(), 1, 1, + conv_op.getPrecisionConfigAttr()); + + conv_results.push_back(conv_result); + } + + auto final_output = mhlo::ConcatenateOp::create( + rewriter, conv_op.getLoc(), conv_results, + rewriter.getI64IntegerAttr(dnums.getOutputFeatureDimension())); + rewriter.replaceOp(conv_op, final_output.getResult()); + return mlir::success(); + } + + private: + void CreateResizeBilinearOp(mhlo::ConvolutionOp conv_op, + llvm::ArrayRef output_sizes, + bool align_corners, + ConversionPatternRewriter& rewriter) const { + Value output_sizes_attr = TF::ConstOp::create( + rewriter, conv_op.getLoc(), + DenseIntElementsAttr::get( + RankedTensorType::get({static_cast(output_sizes.size())}, + rewriter.getI32Type()), + output_sizes)); + // The value of half_pixel_centers couldn't be inferred from the IR and XLA + // only support half_pixel_centers=True as in 01/11/2022. Here + // half_pixel_centers=False is hardcoded. + Value output = TF::ResizeBilinearOp::create( + rewriter, conv_op.getLoc(), conv_op.getType(), conv_op.getLhs(), + output_sizes_attr, + /*align_corners=*/rewriter.getBoolAttr(align_corners), + /*half_pixel_centers=*/rewriter.getBoolAttr(false)); + rewriter.replaceOp(conv_op, {output}); + } + + LogicalResult MatchToResizeBilinearOp( + mhlo::ConvolutionOp conv_op, bool& align_corners, + llvm::SmallVector& output_sizes, + ConversionPatternRewriter& rewriter) const { + mhlo::ConvDimensionNumbersAttr dnums = conv_op.getDimensionNumbers(); + + int feature_group_count = conv_op.getFeatureGroupCount(); + const int input_feature_dimension = dnums.getInputFeatureDimension(); + const int input_channels = + mlir::cast(conv_op.getLhs().getType()) + .getDimSize(input_feature_dimension); + + // Check for Group Convolution parameters + if (feature_group_count != 1 && feature_group_count != input_channels) { + // Group convolution is not supported yet. + return rewriter.notifyMatchFailure(conv_op, + "doesn't support group convolution"); + } + + auto input_spatial_dimensions = dnums.getInputSpatialDimensions(); + auto kernel_spatial_dimensions = dnums.getKernelSpatialDimensions(); + auto output_spatial_dimensions = dnums.getOutputSpatialDimensions(); + if (input_spatial_dimensions.size() != 2 || + output_spatial_dimensions.size() != 2 || + kernel_spatial_dimensions.size() != 2 || + input_spatial_dimensions[0] != output_spatial_dimensions[0] || + input_spatial_dimensions[1] != output_spatial_dimensions[1]) + return rewriter.notifyMatchFailure( + conv_op, "can only be converted to 2D resize op"); + + auto lhs_dilation = conv_op.getLhsDilation().value(); + auto rhs_dilation = conv_op.getRhsDilation().value(); + auto window_strides = conv_op.getWindowStrides().value(); + auto padding = conv_op.getPadding().value(); + if (lhs_dilation.getNumElements() != 2 || !rhs_dilation.isSplat() || + rhs_dilation.getSplatValue() != 1 || + window_strides.getNumElements() != 2 || padding.getNumElements() != 4) + return rewriter.notifyMatchFailure( + conv_op, "resize op requires [2] dilations and [2,2] padding"); + auto lhs_dilation_values = lhs_dilation.getValues(); + auto window_strides_values = window_strides.getValues(); + auto padding_values = padding.getValues(); + + // Cast the dimension sizes to int. + auto lhs_type = mlir::cast(conv_op.getLhs().getType()); + llvm::SmallVector input_sizes = { + static_cast(lhs_type.getDimSize(input_spatial_dimensions[0])), + static_cast(lhs_type.getDimSize(input_spatial_dimensions[1]))}; + output_sizes = {static_cast(conv_op.getType().getDimSize( + output_spatial_dimensions[0])), + static_cast(conv_op.getType().getDimSize( + output_spatial_dimensions[1]))}; + + // This is based on method in compiler/tf2xla/kernels/image_resize_ops.cc + auto can_convert_to_bilinear = [](bool align_corners, int64_t dilation, + int64_t padding, int64_t stride, + int64_t input_spatial, + int64_t output_spatial) { + int64_t input_spatial_size = + align_corners ? input_spatial - 1 : input_spatial; + int64_t output_spatial_size = + align_corners ? output_spatial - 1 : output_spatial; + + int64_t gcd = + tensorflow::MathUtil::GCD(static_cast(input_spatial_size), + static_cast(output_spatial_size)); + if ((input_spatial_size % gcd != 0) || + (input_spatial_size / gcd != stride) || (dilation - 1 != padding)) { + return false; + } + + return true; + }; + + // Only of the lhs_dilation must be 1, then the non-1 dimension is the + // resize dimension. + if (lhs_dilation_values[0] != 1 && lhs_dilation_values[1] == 1) { + if (can_convert_to_bilinear( + /*align_corners=*/true, lhs_dilation_values[0], padding_values[0], + window_strides_values[0], input_sizes[0], output_sizes[0])) { + align_corners = true; + return success(); + } + if (can_convert_to_bilinear( + /*align_corners=*/false, lhs_dilation_values[0], + padding_values[0], window_strides_values[0], input_sizes[0], + output_sizes[0])) { + align_corners = false; + return success(); + } + } + + if (lhs_dilation_values[0] == 1 && lhs_dilation_values[1] != 1) { + if (can_convert_to_bilinear( + /*align_corners=*/true, lhs_dilation_values[1], padding_values[2], + window_strides_values[1], input_sizes[1], output_sizes[1])) { + align_corners = true; + return success(); + } + if (can_convert_to_bilinear( + /*align_corners=*/false, lhs_dilation_values[1], + padding_values[2], window_strides_values[1], input_sizes[1], + output_sizes[1])) { + align_corners = false; + return success(); + } + } + + return rewriter.notifyMatchFailure(conv_op, + "can not be converted to resize op"); + } +}; + +class ConvertNonTrivialConvOp + : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + // TODO(b/302150407): we should move to use direct legalization to TFlite + // instead going through TF in the future + LogicalResult matchAndRewrite( + mhlo::ConvolutionOp conv_op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final { + SetDefaultConvAttributes(conv_op, rewriter); + if (IsSupportedNonTrivialConvOp(conv_op, rewriter).failed()) { + return rewriter.notifyMatchFailure( + conv_op, "doesn't support to convert to ConvBackpropInputOp"); + } + + int feature_group_count = conv_op.getFeatureGroupCount(); + // For depthwise and group convolutions, feature_group_count != 1 + if (feature_group_count != 1) { + // Depthwise or Group convolution is not supported yet. + return rewriter.notifyMatchFailure( + conv_op, "group or depthwise convolution is not supported"); + } + + // Constructs strides array from lhs_dilation. + // For example, [2, 3] -> [1, 2, 3, 1]. + SmallVector strides({1}); + strides.append( + conv_op.getLhsDilation().value().getValues().begin(), + conv_op.getLhsDilation().value().getValues().end()); + strides.emplace_back(1); + + // Constructs dilation array. + SmallVector dilation; + if (auto rhs_dilation = conv_op.getRhsDilation()) { + // For example, [2, 3] -> [1, 2, 3, 1]. + dilation.emplace_back(1); + dilation.append(rhs_dilation.value().getValues().begin(), + rhs_dilation.value().getValues().end()); + dilation.emplace_back(1); + } else { + // Default value + dilation = {1, 1, 1, 1}; + } + + mhlo::ConvDimensionNumbersAttr dnums = conv_op.getDimensionNumbers(); + auto input_spatial_dims = dnums.getInputSpatialDimensions(); + int num_spatial_dims = input_spatial_dims.size(); + + std::string padding; + SmallVector padding_array{0, 0}; + SmallVector padding_attr_value( + conv_op.getPadding().value().getValues().begin(), + conv_op.getPadding().value().getValues().end()); + padding_array.append(padding_attr_value); + padding_array.push_back(0); + padding_array.push_back(0); + + if (IsValidPaddingForTransposedConv(conv_op, num_spatial_dims, strides, + padding_array)) { + padding = "VALID"; + } else { + if (!IsSamePaddingForTransposedConv(conv_op, num_spatial_dims, strides)) { + return rewriter.notifyMatchFailure( + conv_op, "requires padding to be SAME or VALID"); + } + padding = "SAME"; + } + auto conv_input = InsertTranspose( + conv_op.getLhs(), dnums.getInputBatchDimension(), + dnums.getInputFeatureDimension(), dnums.getInputSpatialDimensions(), + /*default_batch_dim=*/0, + /*default_feature_dim=*/num_spatial_dims + 1, + /*default_spatial_dim_start=*/1, num_spatial_dims, rewriter); + + // Mirror the filter in the spatial dimensions. + mlir::Value reverse_filter_in = conv_op.getRhs(); + // If the kernel is with format anythoing other than HWOI, we + // transpose it to [0,1,o,i] as the TF->TFL pass anticipates this and the + // kernel format information will be lost once we legalize to TF + if (!isKernelFormatHWOI(dnums)) { + SmallVector permutation; + for (int64_t dim : dnums.getKernelSpatialDimensions()) { + permutation.push_back(dim); + } + permutation.push_back(dnums.getKernelOutputFeatureDimension()); + permutation.push_back(dnums.getKernelInputFeatureDimension()); + + auto filter_transposed = mhlo::TransposeOp::create( + rewriter, conv_op.getLoc(), conv_op.getRhs(), + DenseIntElementsAttr::get( + RankedTensorType::get({static_cast(permutation.size())}, + rewriter.getI64Type()), + permutation)); + reverse_filter_in = filter_transposed; + } + + // Lets hard-code the reverse indexes to be {0, 1} as the expectation is + // that the kernel is always in HWOI format, with the above code. + mhlo::ReverseOp filter = + mhlo::ReverseOp::create(rewriter, conv_op.getLoc(), reverse_filter_in, + rewriter.getI64TensorAttr({0, 1})); + + // if output is not in [b, 0, 1, f] format, insert transpose to go back + if (dnums.getOutputBatchDimension() != 0 || + dnums.getOutputFeatureDimension() != num_spatial_dims + 1) { + std::vector transpose_order; + transpose_order.resize(num_spatial_dims + 2); + // the output always in b, 0, 1, f format + transpose_order[dnums.getOutputBatchDimension()] = 0; + transpose_order[dnums.getOutputFeatureDimension()] = num_spatial_dims + 1; + for (size_t i = 0; i < num_spatial_dims; ++i) { + transpose_order[dnums.getOutputSpatialDimensions().data()[i]] = i + 1; + } + auto output_shape = + mlir::cast(conv_op.getResult().getType()) + .getShape(); + SmallVector transposed_output_shape = { + output_shape[dnums.getOutputBatchDimension()], + output_shape[dnums.getOutputSpatialDimensions().data()[0]], + output_shape[dnums.getOutputSpatialDimensions().data()[1]], + output_shape[dnums.getOutputFeatureDimension()]}; + // Converts int64_t to int32_t. + SmallVector transposed_output_shape_i32; + for (int64_t dim : transposed_output_shape) { + transposed_output_shape_i32.push_back(dim); + } + auto output_type = RankedTensorType::get( + transposed_output_shape, + mlir::cast(conv_op.getRhs().getType()).getElementType()); + auto output_sizes = TF::ConstOp::create( + rewriter, conv_op.getLoc(), + DenseIntElementsAttr::get( + RankedTensorType::get( + {static_cast(transposed_output_shape_i32.size())}, + rewriter.getI32Type()), + transposed_output_shape_i32)); + auto new_conv = TF::Conv2DBackpropInputOp::create( + rewriter, conv_op.getLoc(), output_type, output_sizes, filter, + conv_input, rewriter.getI64ArrayAttr(strides), + /*use_cudnn_on_gpu=*/rewriter.getBoolAttr(true), + /*padding=*/rewriter.getStringAttr(padding), + /*explicit_paddings=*/rewriter.getI64ArrayAttr({}), + /*data_format=*/rewriter.getStringAttr("NHWC"), + /*dilations=*/rewriter.getI64ArrayAttr(dilation)); + auto output_transpose = mhlo::TransposeOp::create( + rewriter, conv_op.getLoc(), new_conv.getResult(), + rewriter.getI64TensorAttr(transpose_order)); + conv_op->replaceAllUsesWith(output_transpose); + rewriter.eraseOp(conv_op); + } else { + SmallVector output_shape_i32; + for (int64_t dim : + mlir::cast(conv_op.getResult().getType()) + .getShape()) { + output_shape_i32.push_back(dim); + } + auto output_sizes = TF::ConstOp::create( + rewriter, conv_op.getLoc(), + DenseIntElementsAttr::get( + RankedTensorType::get( + {static_cast(output_shape_i32.size())}, + rewriter.getI32Type()), + output_shape_i32)); + rewriter.replaceOpWithNewOp( + conv_op, conv_op.getType(), output_sizes, filter, conv_input, + rewriter.getI64ArrayAttr(strides), + /*use_cudnn_on_gpu=*/rewriter.getBoolAttr(true), + /*padding=*/rewriter.getStringAttr(padding), + /*explicit_paddings=*/rewriter.getI64ArrayAttr({}), + /*data_format=*/rewriter.getStringAttr("NHWC"), + /*dilations=*/rewriter.getI64ArrayAttr(dilation)); + } + return success(); + }; + + private: + // Utility function to check if the padding on the mhlo.convolution op + // equals to the padding needed on a transpose_conv for the same + // inputs/outputs, to achieve the effect of VALID padding. + bool IsValidPaddingForTransposedConv(mhlo::ConvolutionOp conv_op, + size_t num_spatial_dims, + ArrayRef strides, + ArrayRef padding) const { + auto dnums = conv_op.getDimensionNumbers(); + // The newly added spatial dimension requires zero left and right padding. + ArrayRef input_spatial_dims = dnums.getInputSpatialDimensions(); + ArrayRef kernel_spatial_dims = dnums.getKernelSpatialDimensions(); + ArrayRef output_spatial_dims = dnums.getOutputSpatialDimensions(); + + for (size_t i = 1; i <= num_spatial_dims; ++i) { + int64_t stride = strides[i]; + int64_t input_size = mlir::cast(conv_op.getLhs().getType()) + .getDimSize(input_spatial_dims[i - 1]); + int64_t kernel_size = mlir::cast(conv_op.getRhs().getType()) + .getDimSize(kernel_spatial_dims[i - 1]); + int64_t output_size = mlir::cast(conv_op.getType()) + .getDimSize(output_spatial_dims[i - 1]); + + // stablehlo.convolution op needs explicit padding to be set to model any + // Transposed-Convolution in JAX/PT. Checking to see if- + // 1. Pre set padding matches to the desired padding + // 2. Output size respects the `VALID` padding scenario + if ((padding[2 * i] == padding[2 * i + 1]) && + (((kernel_size - 1) != padding[2 * i]) || + (output_size != (stride * (input_size - 1)) + kernel_size))) { + // padding[2 * i] == padding[2 * i + 1] means equal padding is applied + // on both sides of a spatial dimension. + // This happens when kernel_dim >= stride + return false; + } else if ((padding[2 * i] != padding[2 * i + 1]) && + (((kernel_size - 1) != padding[2 * i]) || + ((stride - 1) != padding[2 * i + 1]) || + (output_size != (stride * input_size)))) { + return false; + } + } + + return true; + } + + bool IsSamePaddingForTransposedConv(mhlo::ConvolutionOp conv_op, + size_t num_spatial_dims, + ArrayRef strides) const { + auto dnums = conv_op.getDimensionNumbers(); + SmallVector padding( + conv_op.getPadding().value().getValues().begin(), + conv_op.getPadding().value().getValues().end()); + // The newly added spatial dimension requires zero left and right padding. + ArrayRef input_spatial_dims = dnums.getInputSpatialDimensions(); + ArrayRef output_spatial_dims = dnums.getOutputSpatialDimensions(); + for (size_t i = 0; i < num_spatial_dims; ++i) { + // In some cases the total padding is odd, so we have 1 leftover, which is + // why below we check pad_delta > 1. + int64_t pad_delta = std::abs(padding[2 * i] - padding[2 * i + 1]); + if (pad_delta > 1) { + return false; + } + int64_t stride = strides[i + 1]; + int64_t input_size = mlir::cast(conv_op.getLhs().getType()) + .getDimSize(input_spatial_dims[i]); + int64_t output_size = mlir::cast(conv_op.getType()) + .getDimSize(output_spatial_dims[i]); + // The reason for the below check is as follows: + // When computing the output, we have the following relation between + // o - output dim size, i - input dim size, s - stride, P - total pads + // o = (i-k+1) + (s-1)(i-1) + P + // Where the first term is the kernel applications on the input, + // the second term is the additional applications from the stride + // and P is a term that captures the total padding. After expanding we get + // o = si + k - s + 2 + P + // Here JAX sets P to cancel k-s+2, leading to the expression below + if (output_size != input_size * stride) { + return false; + } + } + return true; + } + + bool isKernelFormatHWOI(mhlo::ConvDimensionNumbersAttr dnums) const { + int64_t num_spatial_dims = dnums.getKernelSpatialDimensions().size(); + return dnums.getKernelInputFeatureDimension() == num_spatial_dims + 1 && + dnums.getKernelOutputFeatureDimension() == num_spatial_dims; + } +}; + +class ConvertSliceOp : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::SliceOp slice_op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final { + auto begin = TF::ConstOp::create(rewriter, slice_op.getLoc(), + slice_op.getStartIndices()); + auto end = TF::ConstOp::create(rewriter, slice_op.getLoc(), + slice_op.getLimitIndices()); + auto strides = + TF::ConstOp::create(rewriter, slice_op.getLoc(), slice_op.getStrides()); + rewriter.replaceOpWithNewOp( + slice_op, slice_op.getType(), slice_op.getOperand(), begin, end, + strides); + return success(); + } +}; + +class ConvertDynamicSliceOp : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::DynamicSliceOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final { + ShapedType input_type = mlir::cast(op.getOperand().getType()); + if (!input_type.hasStaticShape()) return failure(); + Type start_indices_element_type = + mlir::cast(op.getStartIndices().front().getType()) + .getElementType(); + + // The mhlo dynamic_slice's start_indices can be either signed/unsigned + // int32/int64. However, TF only takes in either i32 or i64 types for begin, + // so we will always put a cast. + Type signed_start_indices_element_type; + if (start_indices_element_type.isInteger(32)) { + signed_start_indices_element_type = rewriter.getI32Type(); + } else { + signed_start_indices_element_type = rewriter.getI64Type(); + } + + // Clamp indices to [0, input_size - output_size] + llvm::SmallVector start_indices_vector; + start_indices_vector.reserve(op.getStartIndices().size()); + Value clamp_min = TF::ConstOp::create( + rewriter, op.getLoc(), + rewriter.getIntegerAttr(signed_start_indices_element_type, 0)); + for (uint64_t i = 0, e = op.getStartIndices().size(); i < e; ++i) { + // Always put a cast there. + auto start = op.getStartIndices()[i]; + auto cast_type = mlir::cast(start.getType()) + .clone(signed_start_indices_element_type); + auto cast_op = + TF::CastOp::create(rewriter, op.getLoc(), cast_type, start); + Value clamp_max = TF::ConstOp::create( + rewriter, op.getLoc(), + rewriter.getIntegerAttr( + signed_start_indices_element_type, + input_type.getShape()[i] - + op.getSliceSizes().getValues()[i])); + Value clamped_index = mhlo::ClampOp::create( + rewriter, op.getLoc(), cast_type, clamp_min, cast_op, clamp_max); + start_indices_vector.push_back(clamped_index); + } + + // Pack individual start indices to start indices tensor. + Type start_indices_type = RankedTensorType::get( + {static_cast(start_indices_vector.size())}, + signed_start_indices_element_type); + Value start_indices_op = + TF::PackOp::create(rewriter, op.getLoc(), start_indices_type, + ValueRange(start_indices_vector)); + + Value slice_sices_op = + TF::ConstOp::create(rewriter, op.getLoc(), op.getSliceSizes()); + rewriter.replaceOpWithNewOp(op, op.getType(), op.getOperand(), + start_indices_op, slice_sices_op); + return success(); + }; +}; + +// Appends all elements in `range` to `values`. +template +void Append(llvm::SmallVectorImpl& values, Range&& range) { + values.insert(values.end(), range.begin(), range.end()); +} + +// Appends all elements in `range` to `values`. +template +void Append(llvm::SmallVectorImpl& values, Range&& range, + RangeTs&&... ranges) { + values.insert(values.end(), range.begin(), range.end()); + Append(values, ranges...); +} + +// Returns the number of elements in `range`. +template +size_t Size(Range&& range) { + return range.size(); +} + +// Returns the total number of elements in a variadic number of `ranges`. +template +size_t Size(Range&& range, RangeTs&&... ranges) { + return range.size() + Size(std::forward(ranges)...); +} + +// Concats all elements in `ranges` and returns a small vector as a result. +template +llvm::SmallVector Concat(RangeTs&&... ranges) { + llvm::SmallVector results; + results.reserve(Size(std::forward(ranges)...)); + Append(results, std::forward(ranges)...); + return results; +} + +// A struct to hold axes and sizes for a set of dimensions. +struct DimensionVector { + llvm::ArrayRef AxesArray() const { return axes; } + llvm::ArrayRef SizesArray() const { return sizes; } + + llvm::SmallVector axes; + llvm::SmallVector sizes; +}; + +// Create a tensor that is reshaped from input. +Value BuildReshapeOp(ImplicitLocOpBuilder& builder, + ConversionPatternRewriter& rewriter, Value input, + ArrayRef shape, Type idx_type, + Type element_type) { + Value shape_cst = BuildIntArrayConstOp(builder, rewriter, shape, idx_type); + Value reshaped_input = TF::ReshapeOp::create( + builder, RankedTensorType::get(shape, element_type), input, shape_cst); + return reshaped_input; +} + +// Create a tensor which is equal to input[begin: begin + size]. +Value BuildSliceOp(ImplicitLocOpBuilder& builder, + ConversionPatternRewriter& rewriter, Value input, + Value begin, ArrayRef shape, Type idx_type, + Type element_type) { + Value shape_cst = BuildIntArrayConstOp(builder, rewriter, shape, idx_type); + Value slice_result = + TF::SliceOp::create(builder, RankedTensorType::get(shape, element_type), + input, begin, shape_cst); + return slice_result; +} + +class ConvertDynamicUpdateSliceOp + : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::DynamicUpdateSliceOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final { + ShapedType operand_type = mlir::cast(op.getOperand().getType()); + ShapedType update_type = + mlir::dyn_cast_or_null(op.getUpdate().getType()); + ShapedType start_indices_type = mlir::dyn_cast_or_null( + op.getStartIndices().front().getType()); + if (update_type == nullptr || start_indices_type == nullptr) + return rewriter.notifyMatchFailure( + op, "update and start_indices should have ShapedType"); + + Type idx_type = start_indices_type.getElementType(); + int64_t shape_dim = operand_type.getRank(); + llvm::SmallVector start_indices_vector; + Append(start_indices_vector, op.getStartIndices()); + auto shape_tensor_type = RankedTensorType::get({shape_dim}, idx_type); + Value start_indices_tensor = TF::PackOp::create( + rewriter, op.getLoc(), shape_tensor_type, start_indices_vector); + rewriter.replaceOpWithNewOp( + op, op.getType(), op.getOperand(), op.getUpdate(), + start_indices_tensor); + return success(); + }; +}; + +template +bool MatchTopKComparator(Region& comparator) { + if (!comparator.hasOneBlock()) return false; + Block& comparator_blk = comparator.front(); + using OpListType = llvm::iplist; + OpListType& operations = comparator_blk.getOperations(); + if (operations.size() != 2) return false; + auto compare_op = dyn_cast_or_null(&operations.front()); + auto return_op = dyn_cast_or_null(&operations.back()); + if (!compare_op || !return_op) return false; + // TODO(xuanyuanluo): Support mhlo::ComparisonDirection::LT direction. + if (compare_op.getComparisonDirection() != mhlo::ComparisonDirection::GT) { + return false; + } + if (compare_op.getOperands()[0] != comparator_blk.getArgument(0) || + compare_op.getOperands()[1] != comparator_blk.getArgument(1)) { + return false; + } + return return_op.getOperands().front() == compare_op.getResult(); +} + +// In general, we convert the following form of sort to tf.TopK: +// +// %result = "mhlo.sort" (%keys, %indices) ({ +// ^bb0(%key_0, %key_1, %index_0, %index_1): +// %1 = "mhlo.compare"(%key_0, %key_1) {mhlo::ComparisonDirection::GT} +// -> tensor +// }), +// +// where the indices is obtained by an IotaOp (maybe folded). +class ConvertSortToTfTopk : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::SortOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final { + if (op->getOperands().size() != 2) + return rewriter.notifyMatchFailure( + op, "only match for the case where operands is of size 2"); + auto keys = op.getInputs()[0]; + auto indices = op.getInputs()[1]; + auto keys_ty = mlir::dyn_cast_or_null(keys.getType()); + auto indices_ty = mlir::dyn_cast_or_null(indices.getType()); + if (!keys_ty || !keys_ty.hasStaticShape() || + !keys_ty.getElementType().isIntOrFloat()) + return rewriter.notifyMatchFailure( + op, + "only match for the case where the first operand has a static " + "int/float shapeType"); + if (!indices_ty || !indices_ty.hasStaticShape() || + !indices_ty.getElementType().isInteger(32)) + return rewriter.notifyMatchFailure( + op, + "only match for the case where the second operand an I32 shapeType"); + auto sort_dim = op.getDimension(); + auto k = indices_ty.getDimSize(sort_dim); + auto rank = keys_ty.getRank(); + if (sort_dim != rank - 1 || k < 1) + return rewriter.notifyMatchFailure( + op, "only match for sort dim = rank - 1 and DimSize >= 1"); + + // In the following, we'll check indices is obtained by a iota. + auto sort_dim_attr = DenseIntElementsAttr::get( + RankedTensorType::get({1}, rewriter.getI64Type()), {sort_dim}); + if (!MatchIota(sort_dim_attr, indices)) + return rewriter.notifyMatchFailure( + op, "the second operand is supposed to be obtained from IOTA"); + if (!MatchTopKComparator(op.getComparator())) + return rewriter.notifyMatchFailure(op, "only match for GT comparator"); + ImplicitLocOpBuilder builder(op.getLoc(), rewriter); + Value k_cst = BuildIntConstOp(builder, rewriter, k, rewriter.getI32Type()); + rewriter.replaceOpWithNewOp(op, keys.getType(), + indices.getType(), keys, k_cst); + return success(); + }; +}; + +// A struct to hold information about dimensions of dot_general operands. +class DotDimensionsInfo { + public: + DotDimensionsInfo(ShapedType type, ArrayRef batch_dimensions, + ArrayRef contracting_dimensions) { + const int64_t rank = type.getRank(); + for (const int64_t dim : batch_dimensions) { + batch_dimensions_.axes.push_back(dim); + batch_dimensions_.sizes.push_back(type.getDimSize(dim)); + } + + for (const int64_t dim : contracting_dimensions) { + contracting_dimensions_.axes.push_back(dim); + contracting_dimensions_.sizes.push_back(type.getDimSize(dim)); + } + + for (int64_t dim = 0; dim < rank; ++dim) { + if (llvm::count(contracting_dimensions_.axes, dim) > 0 || + llvm::count(batch_dimensions_.axes, dim) > 0) { + continue; + } + out_dimensions_.axes.push_back(dim); + out_dimensions_.sizes.push_back(type.getDimSize(dim)); + } + } + + const DimensionVector& batch_dimensions() const { return batch_dimensions_; } + const DimensionVector& contracting_dimensions() const { + return contracting_dimensions_; + } + // Out dimensions are any dimensions that are neither batch nor contracting + // dimensions, hence will be propagated to output shape. + const DimensionVector& out_dimensions() const { return out_dimensions_; } + + // Returns the total dimension size after flattening all contracting + // dimensions. + int64_t FlattenedContractingDimensionSize() const { + if (ShapedType::isDynamicShape(contracting_dimensions_.sizes)) { + return ShapedType::kDynamic; + } + return std::accumulate(contracting_dimensions_.sizes.begin(), + contracting_dimensions_.sizes.end(), 1, + std::multiplies()); + } + + // Returns the total dimension size after flattening all out dimensions. + int64_t FlattenedOutDimensionSize() const { + if (ShapedType::isDynamicShape(out_dimensions_.sizes)) { + return ShapedType::kDynamic; + } + return std::accumulate(out_dimensions_.sizes.begin(), + out_dimensions_.sizes.end(), 1, + std::multiplies()); + } + + private: + DimensionVector batch_dimensions_; + DimensionVector contracting_dimensions_; + // Out dimensions are any dimensions that are neither batch nor contracting + // dimensions, hence will be propagated to output shape. + DimensionVector out_dimensions_; +}; + +// Calculates the flattened shapes for dynamic shaped operands in +// mhlo.dot_general: +// 1. flattened_out_dim = UnsortedSegmentProdOp(operand_shape, out_axes) +// 2. flattened_contracting_dim = UnsortedSegmentProdOp(operand_shape, +// contracting_axes) +// 3. batch_dimensions = Gather(operand_shape, batch_axes) +// 4. flattened_shape = Concat(batch_dimensions, flattened_out_dim, +// flattened_contracting_dim) +// The flattened shape for LHS +// is like [batch_dimensions, flattened_out_dimension, +// flattened_contracting_dimension] and [batch_dimensions, +// flattened_contracting_dimension, flattened_out_dimension] for RHS. +Value BuildDotOperandFlattenedShapeOp(Value operand, + DotDimensionsInfo dot_dimensions_info, + ImplicitLocOpBuilder& builder, + bool is_lhs) { + auto operand_type = mlir::cast(operand.getType()); + BoolAttr true_attr = builder.getBoolAttr(true); + auto operand_shape = TF::ShapeOp::create(builder, operand, true_attr); + const int64_t operand_rank = operand_type.getRank(); + // Compute flattened out dimension and contracting dimension using + // TF::UnsortedSegmentProdOp. + llvm::SmallVector flattened_out_segids = + llvm::SmallVector(operand_rank, static_cast(-1)); + for (int64_t i : dot_dimensions_info.out_dimensions().AxesArray()) { + flattened_out_segids[i] = 0; + } + llvm::SmallVector flattened_contracting_segids = + llvm::SmallVector(operand_rank, static_cast(-1)); + for (int64_t i : dot_dimensions_info.contracting_dimensions().AxesArray()) { + flattened_contracting_segids[i] = 0; + } + auto seg_prod_result_type = + RankedTensorType::get(static_cast(1), builder.getI32Type()); + auto out_segids_cst = TF::ConstOp::create( + builder, builder.getI32TensorAttr(flattened_out_segids)); + auto contracting_segids_cst = TF::ConstOp::create( + builder, builder.getI32TensorAttr(flattened_contracting_segids)); + auto num_segids_tensor = + TF::ConstOp::create(builder, builder.getI32IntegerAttr(1)); + auto flattened_out_dims = TF::UnsortedSegmentProdOp::create( + builder, seg_prod_result_type, operand_shape, out_segids_cst, + num_segids_tensor); + auto flattened_contracting_dims = TF::UnsortedSegmentProdOp::create( + builder, seg_prod_result_type, operand_shape, contracting_segids_cst, + num_segids_tensor); + llvm::SmallVector flattend_shape_values; + // Gather the batch dimensions. + if (!dot_dimensions_info.batch_dimensions().AxesArray().empty()) { + if (ShapedType::isDynamicShape( + dot_dimensions_info.batch_dimensions().SizesArray())) { + auto batch_axes_tensor = TF::ConstOp::create( + builder, builder.getI64TensorAttr( + dot_dimensions_info.batch_dimensions().AxesArray())); + auto batch_dims = TF::GatherOp::create( + builder, + RankedTensorType::get( + {static_cast( + dot_dimensions_info.batch_dimensions().AxesArray().size())}, + builder.getIntegerType(32)), + operand_shape, batch_axes_tensor, true_attr); + flattend_shape_values.push_back(batch_dims); + } else { + llvm::SmallVector batch_i32_vec; + for (int64_t element : + dot_dimensions_info.batch_dimensions().SizesArray()) { + batch_i32_vec.push_back(static_cast(element)); + } + auto batch_dims = + TF::ConstOp::create(builder, builder.getI32TensorAttr(batch_i32_vec)); + flattend_shape_values.push_back(batch_dims); + } + } + flattend_shape_values.push_back( + (is_lhs ? flattened_out_dims : flattened_contracting_dims)); + flattend_shape_values.push_back( + (is_lhs ? flattened_contracting_dims : flattened_out_dims)); + + auto concat_result_type = RankedTensorType::get( + {static_cast( + dot_dimensions_info.batch_dimensions().AxesArray().size()) + + 2}, + builder.getIntegerType(32)); + // Concatenate the batch dimensions, flattened out dimension and flattened + // contracting dimension. + return TF::ConcatOp::create( + builder, concat_result_type, + TF::ConstOp::create(builder, builder.getI32IntegerAttr(0)), + flattend_shape_values); +} + +Value ConvertDot(PatternRewriter& rewriter, Value lhs, Value rhs, + DotDimensionNumbersAttr dot_dimension_numbers, + ShapedType result_type, mlir::Location loc) { + auto lhs_type = mlir::cast(lhs.getType()); + auto rhs_type = mlir::cast(rhs.getType()); + const int lhs_rank = lhs_type.getRank(); + const int rhs_rank = rhs_type.getRank(); + ImplicitLocOpBuilder builder(loc, rewriter); + + // Collects lhs and rhs dimensions information. + DotDimensionsInfo lhs_dot_dimensions_info( + lhs_type, dot_dimension_numbers.getLhsBatchingDimensions(), + dot_dimension_numbers.getLhsContractingDimensions()); + DotDimensionsInfo rhs_dot_dimensions_info( + rhs_type, dot_dimension_numbers.getRhsBatchingDimensions(), + dot_dimension_numbers.getRhsContractingDimensions()); + + // Transposes lhs shape to be in the order of {batch_dimensions, + // out_dimensions, contracting dimensions}. + llvm::SmallVector lhs_permutation = Concat( + lhs_dot_dimensions_info.batch_dimensions().AxesArray(), + lhs_dot_dimensions_info.out_dimensions().AxesArray(), + lhs_dot_dimensions_info.contracting_dimensions().AxesArray()); + llvm::SmallVector lhs_transposed_shape = Concat( + lhs_dot_dimensions_info.batch_dimensions().SizesArray(), + lhs_dot_dimensions_info.out_dimensions().SizesArray(), + lhs_dot_dimensions_info.contracting_dimensions().SizesArray()); + auto lhs_transposed = mhlo::TransposeOp::create( + rewriter, loc, + RankedTensorType::get(lhs_transposed_shape, lhs_type.getElementType()), + lhs, + DenseIntElementsAttr::get( + RankedTensorType::get({lhs_rank}, rewriter.getI64Type()), + lhs_permutation)); + + // Transposes rhs shape to be in the order of {batch_dimensions, contracting + // dimensions, out_dimensions}. + llvm::SmallVector rhs_permutation = Concat( + rhs_dot_dimensions_info.batch_dimensions().AxesArray(), + rhs_dot_dimensions_info.contracting_dimensions().AxesArray(), + rhs_dot_dimensions_info.out_dimensions().AxesArray()); + llvm::SmallVector rhs_transposed_shape = Concat( + rhs_dot_dimensions_info.batch_dimensions().SizesArray(), + rhs_dot_dimensions_info.contracting_dimensions().SizesArray(), + rhs_dot_dimensions_info.out_dimensions().SizesArray()); + auto rhs_transposed = mhlo::TransposeOp::create( + rewriter, loc, + RankedTensorType::get(rhs_transposed_shape, rhs_type.getElementType()), + rhs, + DenseIntElementsAttr::get( + RankedTensorType::get({rhs_rank}, rewriter.getI64Type()), + rhs_permutation)); + + // Reshapes lhs to flatten out_dimensions and contracting_dimensions. + llvm::SmallVector lhs_flattened_shape = Concat( + lhs_dot_dimensions_info.batch_dimensions().SizesArray(), + llvm::ArrayRef{ + lhs_dot_dimensions_info.FlattenedOutDimensionSize()}, + llvm::ArrayRef{ + lhs_dot_dimensions_info.FlattenedContractingDimensionSize()}); + Value lhs_flattend; + if (lhs_type.hasStaticShape()) { + lhs_flattend = mhlo::ReshapeOp::create( + rewriter, loc, + RankedTensorType::get(lhs_flattened_shape, lhs_type.getElementType()), + lhs_transposed.getResult()); + } else { + auto lhs_flattend_shape_op = BuildDotOperandFlattenedShapeOp( + lhs, lhs_dot_dimensions_info, builder, /*is_lhs=*/true); + lhs_flattend = mhlo::DynamicReshapeOp::create( + rewriter, loc, + RankedTensorType::get(lhs_flattened_shape, lhs_type.getElementType()), + lhs_transposed, lhs_flattend_shape_op); + } + + // Reshapes rhs to flatten out_dimensions and contracting_dimensions. + llvm::SmallVector rhs_flattened_shape = Concat( + rhs_dot_dimensions_info.batch_dimensions().SizesArray(), + llvm::ArrayRef{ + rhs_dot_dimensions_info.FlattenedContractingDimensionSize()}, + llvm::ArrayRef{ + rhs_dot_dimensions_info.FlattenedOutDimensionSize()}); + Value rhs_flattend; + if (rhs_type.hasStaticShape()) { + rhs_flattend = mhlo::ReshapeOp::create( + rewriter, loc, + RankedTensorType::get(rhs_flattened_shape, rhs_type.getElementType()), + rhs_transposed.getResult()); + } else { + auto rhs_flattend_shape_op = BuildDotOperandFlattenedShapeOp( + rhs, rhs_dot_dimensions_info, builder, /*is_lhs=*/false); + rhs_flattend = mhlo::DynamicReshapeOp::create( + rewriter, loc, + RankedTensorType::get(rhs_flattened_shape, rhs_type.getElementType()), + rhs_transposed, rhs_flattend_shape_op); + } + + // Creates matmul op of `lhs_flattend` and `rhs_flattend`. + llvm::SmallVector matmul_shape = + Concat(lhs_dot_dimensions_info.batch_dimensions().SizesArray(), + llvm::ArrayRef{ + lhs_dot_dimensions_info.FlattenedOutDimensionSize()}, + llvm::ArrayRef{ + rhs_dot_dimensions_info.FlattenedOutDimensionSize()}); + auto matmul = TF::BatchMatMulV3Op::create( + rewriter, loc, + RankedTensorType::get(matmul_shape, result_type.getElementType()), + lhs_flattend, rhs_flattend); + + if (result_type.hasStaticShape()) { + auto reshaped = + mhlo::ReshapeOp::create(rewriter, loc, result_type, matmul.getResult()); + return reshaped.getResult(); + } + + // Reshape for dynamic shaped operands. The result shape is + // [lhs_batch_dimensions, lhs_out_dimensions, rhs_out_dimensions]. + BoolAttr true_attr = rewriter.getBoolAttr(true); + auto lhs_shape = TF::ShapeOp::create(rewriter, loc, lhs, true_attr); + auto rhs_shape = TF::ShapeOp::create(rewriter, loc, rhs, true_attr); + llvm::SmallVector lhs_batch_and_out = + Concat(lhs_dot_dimensions_info.batch_dimensions().AxesArray(), + lhs_dot_dimensions_info.out_dimensions().AxesArray()); + auto lhs_batch_and_out_cst = TF::ConstOp::create( + rewriter, loc, rewriter.getI64TensorAttr(lhs_batch_and_out)); + auto lhs_batch_and_out_dims = TF::GatherOp::create( + rewriter, loc, + RankedTensorType::get({static_cast(lhs_batch_and_out.size())}, + rewriter.getIntegerType(32)), + lhs_shape, lhs_batch_and_out_cst, true_attr); + auto rhs_out_cst = TF::ConstOp::create( + rewriter, loc, + rewriter.getI64TensorAttr( + rhs_dot_dimensions_info.out_dimensions().AxesArray())); + auto rhs_out_dims = TF::GatherOp::create( + rewriter, loc, + RankedTensorType::get( + {static_cast( + rhs_dot_dimensions_info.out_dimensions().AxesArray().size())}, + rewriter.getIntegerType(32)), + rhs_shape, rhs_out_cst, true_attr); + auto result_shape_type = RankedTensorType::get( + {static_cast( + lhs_dot_dimensions_info.batch_dimensions().AxesArray().size() + + lhs_dot_dimensions_info.out_dimensions().AxesArray().size() + + rhs_dot_dimensions_info.out_dimensions().AxesArray().size())}, + rewriter.getIntegerType(32)); + auto result_shape = TF::ConcatOp::create( + rewriter, loc, result_shape_type, + TF::ConstOp::create(rewriter, loc, rewriter.getI32IntegerAttr(0)), + ValueRange{lhs_batch_and_out_dims, rhs_out_dims}); + + auto reshaped = mhlo::DynamicReshapeOp::create( + rewriter, loc, result_type, matmul.getResult(), result_shape); + return reshaped.getResult(); +} + +// Converts mhlo.dot to tf.MatMul. Reshape ops will be inserted when +// necessary. +Value ConvertDotOp(PatternRewriter& rewriter, Operation* old_op) { + auto dot_op = cast(old_op); + auto lhs_rank = mlir::cast(dot_op.getLhs().getType()).getRank(); + auto dot_dimension_numbers = + DotDimensionNumbersAttr::get(rewriter.getContext(), + /*lhs_batching_dimensions=*/{}, + /*rhs_batching_dimensions=*/{}, + /*lhs_contracting_dimensions=*/ + {lhs_rank == 1 ? 0 : 1}, + /*rhs_contracting_dimensions=*/{0}); + return ConvertDot( + rewriter, dot_op.getLhs(), dot_op.getRhs(), dot_dimension_numbers, + mlir::cast(dot_op.getResult().getType()), dot_op.getLoc()); +} + +// Converts mhlo.dot to tf.BatchMatMul. Reshape or Transpose ops will also be +// inserted to convert to well-formed matrix multiply. +Value ConvertDotGeneralOp(PatternRewriter& rewriter, Operation* old_op) { + auto dot_general_op = cast(old_op); + return ConvertDot( + rewriter, dot_general_op.getLhs(), dot_general_op.getRhs(), + dot_general_op.getDotDimensionNumbers(), + mlir::cast(dot_general_op.getResult().getType()), + dot_general_op.getLoc()); +} + +// Replace BinaryOp with a combination of TfBinaryOp and TfReduceOp if the +// init value doesn't match the expectation of TfReduceOp. +template +LogicalResult rewriteNonMatchInitValue(mhlo::ReduceOp reduce_op, Value input, + TF::ConstOp reduction_indices, + ConversionPatternRewriter& rewriter) { + Value reduce_result = + TfReduceOp::create(rewriter, reduce_op.getLoc(), reduce_op.getType(0), + input, reduction_indices, + /*keep_dims=*/rewriter.getBoolAttr(false)); + rewriter.replaceOpWithNewOp(reduce_op, reduce_op.getType(0), + reduce_result, + reduce_op.getInitValues()[0]); + return success(); +} + +// Cannot replace BinaryOp if the init value doesn't match the expectation of +// TfReduceOp and there is no corresponding TfBinaryOp. +template <> +LogicalResult rewriteNonMatchInitValue( + mhlo::ReduceOp reduce_op, Value input, TF::ConstOp reduction_indices, + ConversionPatternRewriter& rewriter) { + return failure(); +} + +template <> +LogicalResult rewriteNonMatchInitValue( + mhlo::ReduceOp reduce_op, Value input, TF::ConstOp reduction_indices, + ConversionPatternRewriter& rewriter) { + return failure(); +} + +// Converts a mhlo.reduce op with a mlho binary operation into a tensorflow +// reduction operation. If the initial value can be ignored, then convert it +// into a single TfReduceOp. Otherwise, convert it into a TfReduceOp followed by +// a TfBinaryOp. +// For example: +// 1) A mhlo::ReduceOp on value `x` with a mhlo::AndOp and a constant initial +// value `true` is converted to a TF::Any on value `x`. +// 2) A mhlo::ReduceOp on value `x` with a mhlo::AndOp with a non-constant +// initial value `y` is converted to a TF::Any on value `x`, followed by a +// TF::And with initial value `y`. +template +class ConvertReduceOpToTfOp : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::ReduceOp reduce_op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final { + if (failed(MatchReduceOpOperand(reduce_op))) return failure(); + + if (failed(MatchBinaryReduceFunction(reduce_op.getBody()))) + return failure(); + + auto operand = reduce_op.getInputs()[0]; + + // Get reduction dimension. + DenseIntElementsAttr dimension = reduce_op.getDimensions(); + SmallVector reduce_dims; + for (const int64_t& dim : dimension.getValues()) { + reduce_dims.emplace_back(dim); + } + auto dim_type = RankedTensorType::get( + {static_cast(reduce_dims.size())}, rewriter.getI64Type()); + auto reduction_indices = + TF::ConstOp::create(rewriter, reduce_op.getLoc(), dim_type, + rewriter.getI64TensorAttr(reduce_dims)); + + // In `MatchReduceOpOperand` function, we already match that the + // "mhlo::ReduceOp" only has one operand, one init_value and one result. + + // If the init value matches with the init value expected for the target + // TfReduceOp, then replace the BinaryOp with a TfReduceOp. Otherwise, + // replace the BinaryOp with a TfBinaryOp and a TfReduceOp. + if (succeeded(MatchInitValue(reduce_op.getInitValues()[0]))) { + rewriter.replaceOpWithNewOp( + reduce_op, reduce_op.getType(0), operand, reduction_indices, + /*keep_dim=*/rewriter.getBoolAttr(false)); + return success(); + } + return rewriteNonMatchInitValue( + reduce_op, operand, reduction_indices, rewriter); + } + + private: + // Checks that the init value matches with the init value expected for the + // target TfReduceOp. + virtual LogicalResult MatchInitValue(Value init_value) const = 0; + + // This function tries to match that the "mhlo::ReduceOp" only has one + // operand, one init_value and one result. + LogicalResult MatchReduceOpOperand(mhlo::ReduceOp reduce_op) const { + if (reduce_op.getInputs().size() != 1 || + reduce_op.getInitValues().size() != 1 || + reduce_op.getResults().size() != 1) + return failure(); + + if (!mlir::isa(reduce_op.getInputs()[0].getType())) + return failure(); + if (!mlir::isa(reduce_op.getType(0))) return failure(); + return success(); + } +}; + +class ConvertReduceOpToTfProd + : public ConvertReduceOpToTfOp { + public: + using ConvertReduceOpToTfOp::ConvertReduceOpToTfOp; + + LogicalResult MatchInitValue(Value init_value) const override { + auto type = mlir::cast(init_value.getType()).getElementType(); + if (mlir::isa(type)) { + float const_value; + if (failed(GetConstantSplatValue(init_value, const_value)) || + const_value != 1.0) + return failure(); + } else if (mlir::isa(type) && type.isSignlessInteger()) { + int32_t const_value; + if (failed(GetConstantSplatValue(init_value, const_value)) || + const_value != 1) + return failure(); + } else { + return failure(); + } + + return success(); + } +}; + +class ConvertReduceOpToTfSum + : public ConvertReduceOpToTfOp { + public: + using ConvertReduceOpToTfOp::ConvertReduceOpToTfOp; + + LogicalResult MatchInitValue(Value init_value) const override { + auto type = mlir::cast(init_value.getType()).getElementType(); + if (mlir::isa(type)) { + APFloat const_value(.0); + if (failed(GetConstantSplatValue(init_value, const_value)) || + !const_value.isZero()) + return failure(); + } else if (mlir::isa(type) && type.isSignlessInteger()) { + APInt const_value; + if (failed(GetConstantSplatValue(init_value, const_value)) || + !const_value.isZero()) + return failure(); + } else { + return failure(); + } + + return success(); + } +}; + +class ConvertReduceOpToTfMax + : public ConvertReduceOpToTfOp { + public: + using ConvertReduceOpToTfOp::ConvertReduceOpToTfOp; + + LogicalResult MatchInitValue(Value init_value) const override { + auto type = mlir::cast(init_value.getType()).getElementType(); + if (mlir::isa(type)) { + APFloat const_value(.0); + if (failed(GetConstantSplatValue(init_value, const_value)) || + !TFL::IsNegInfiniteValue(const_value)) + return failure(); + } else if (mlir::isa(type) && type.isSignlessInteger()) { + APInt const_value; + if (failed(GetConstantSplatValue(init_value, const_value)) || + !const_value.isMinSignedValue()) + return failure(); + } else { + return failure(); + } + return success(); + } +}; + +class ConvertReduceOpToTfMin + : public ConvertReduceOpToTfOp { + public: + using ConvertReduceOpToTfOp::ConvertReduceOpToTfOp; + + LogicalResult MatchInitValue(Value init_value) const override { + auto type = mlir::cast(init_value.getType()).getElementType(); + + if (mlir::isa(type)) { + APFloat const_value(.0); + if (failed(GetConstantSplatValue(init_value, const_value)) || + !TFL::IsPosInfiniteValue(const_value)) + return failure(); + } else if (mlir::isa(type) && type.isSignlessInteger()) { + APInt const_value; + if (failed(GetConstantSplatValue(init_value, const_value)) || + !const_value.isMaxSignedValue()) + return failure(); + } else { + return failure(); + } + return success(); + } +}; + +class ConvertReduceOpToTfAll + : public ConvertReduceOpToTfOp { + public: + using ConvertReduceOpToTfOp::ConvertReduceOpToTfOp; + + LogicalResult MatchInitValue(Value init_value) const override { + DenseIntElementsAttr init_attr; + if (!matchPattern(init_value, m_Constant(&init_attr)) || + !init_attr.getType().getElementType().isInteger(1) || + !init_attr.isSplat() || !init_attr.getSplatValue().getValue()) + return failure(); + return success(); + } +}; + +class ConvertReduceOpToTfAny + : public ConvertReduceOpToTfOp { + public: + using ConvertReduceOpToTfOp::ConvertReduceOpToTfOp; + + LogicalResult MatchInitValue(Value init_value) const override { + DenseIntElementsAttr init_attr; + if (!matchPattern(init_value, m_Constant(&init_attr)) || + !init_attr.getType().getElementType().isInteger(1) || + !init_attr.isSplat() || init_attr.getSplatValue().getValue()) + return failure(); + return success(); + } +}; + +class ConvertIotaOpToTfRange : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::IotaOp iota_op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final { + RankedTensorType type = + mlir::dyn_cast_or_null(iota_op.getType()); + // TF::RangeOp doesn't support UI16 and UI8. + if (!type || type.getElementType().isUnsignedInteger(16) || + type.getElementType().isUnsignedInteger(8)) + return failure(); + + const uint64_t dimension = iota_op.getIotaDimension(); + Type element_type = type.getElementType(); + Attribute start, limit, delta; + if (mlir::isa(element_type)) { + start = rewriter.getFloatAttr(element_type, 0.0); + limit = rewriter.getFloatAttr(element_type, type.getShape()[dimension]); + delta = rewriter.getFloatAttr(element_type, 1.0); + } else if (mlir::isa(element_type)) { + start = rewriter.getIntegerAttr(element_type, 0); + limit = rewriter.getIntegerAttr(element_type, type.getShape()[dimension]); + delta = rewriter.getIntegerAttr(element_type, 1); + } else { + return failure(); + } + + auto range_type = + RankedTensorType::get({type.getShape()[dimension]}, element_type); + Value start_op = TF::ConstOp::create(rewriter, iota_op.getLoc(), start); + Value limit_op = TF::ConstOp::create(rewriter, iota_op.getLoc(), limit); + Value delta_op = TF::ConstOp::create(rewriter, iota_op.getLoc(), delta); + Value result = TF::RangeOp::create(rewriter, iota_op.getLoc(), range_type, + start_op, limit_op, delta_op); + + if (type.getRank() > 1) { + std::vector reshape_shape(type.getRank(), 1); + reshape_shape[iota_op.getIotaDimension()] = type.getShape()[dimension]; + auto reshape_type = RankedTensorType::get(reshape_shape, element_type); + Value reshape_shape_op = TF::ConstOp::create( + rewriter, iota_op.getLoc(), rewriter.getI64TensorAttr(reshape_shape)); + result = TF::ReshapeOp::create(rewriter, iota_op.getLoc(), reshape_type, + result, reshape_shape_op); + + Value broadcast_shape_op = + TF::ConstOp::create(rewriter, iota_op.getLoc(), + rewriter.getI64TensorAttr(type.getShape())); + result = TF::BroadcastToOp::create(rewriter, iota_op.getLoc(), type, + result, broadcast_shape_op); + } + + rewriter.replaceOp(iota_op, result); + return success(); + } +}; + +// A helper function for ConvertMaxPoolOp and ConvertAvgPoolOp. Returns true +// if the given ReduceWindowOp is a spatial pooling without dilation. If returns +// true, also outputs the window strides and the TF padding mode ("VALID" or +// "SAME"). +bool IsSpatialPoolingWithoutDilation( + mhlo::ReduceWindowOp rw, llvm::SmallVectorImpl* window_strides, + std::string* padding_mode, std::string* data_format) { + // tf.max_pool or tf.avg_pool need at least 3 dimensions (batch, spatial, + // channel). + const uint64_t rank = rw.getWindowDimensions().size(); + if (rank <= 3 || rank > 5) return false; + + if (rw.getWindowStrides().has_value()) { + window_strides->insert(window_strides->end(), + rw.getWindowStrides()->getValues().begin(), + rw.getWindowStrides()->getValues().end()); + } else { + window_strides->resize(rank, 1); + } + + llvm::SmallVector padding; + if (rw.getPadding().has_value()) { + padding.insert(padding.begin(), + rw.getPadding()->getValues().begin(), + rw.getPadding()->getValues().end()); + } else { + padding.resize(2 * rank, 0); + } + + // Check that we don't do any reduction along the batch and channel + // dimensions. + auto verify_batch_channel_dims = [&rw, &window_strides, &padding]( + uint64_t batch_dim, + uint64_t channel_dim) { + return rw.getWindowDimensions().getValues()[batch_dim] == 1 && + rw.getWindowDimensions().getValues()[channel_dim] == 1 && + (*window_strides)[batch_dim] == 1 && + (*window_strides)[channel_dim] == 1 && padding[2 * batch_dim] == 0 && + padding[2 * batch_dim + 1] == 0 && padding[2 * channel_dim] == 0 && + padding[2 * channel_dim + 1] == 0; + }; + + bool is_pool2d = rank == 4; + if (verify_batch_channel_dims(0, rank - 1)) { + *data_format = is_pool2d ? "NHWC" : "NDHWC"; + } else if (verify_batch_channel_dims(0, 1)) { + *data_format = is_pool2d ? "NCHW" : "NCDHW"; + } else { + return false; + } + + if (rw.getWindowDilations().has_value() && + !(rw.getWindowDilations()->isSplat() && + rw.getWindowDilations()->getSplatValue() == 1)) + return false; + + if (rw.getBaseDilations().has_value() && + !(rw.getBaseDilations()->isSplat() && + rw.getBaseDilations()->getSplatValue() == 1)) + return false; + + if (llvm::all_of(padding, [](int64_t i) { return i == 0; })) { + *padding_mode = "VALID"; + return true; + } + + // Check that the individual padding values are corresponding to SAME + // padding from TensorFlow. + auto operand_type = + mlir::dyn_cast(rw.getInputs()[0].getType()); + RankedTensorType output_type = + mlir::dyn_cast(rw.getResult(0).getType()); + if (!operand_type || !output_type) return false; + + for (uint64_t i = 1; i < rank - 1; ++i) { + int64_t padding_size = + (output_type.getShape()[i] - 1) * (*window_strides)[i] + + rw.getWindowDimensions().getValues()[i] - + operand_type.getShape()[i]; + if (padding[2 * i] != tensorflow::MathUtil::FloorOfRatio( + padding_size, static_cast(2)) || + padding[2 * i + 1] != tensorflow::MathUtil::CeilOfRatio( + padding_size, static_cast(2))) + return false; + } + + *padding_mode = "SAME"; + return true; +} + +// Convert a reduce_window operation into a cumulative operation where possible +// for a given binary operation. +template +class ConvertLoweredCumOp : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + virtual bool IsInitValue(const DenseElementsAttr& attr) const = 0; + + LogicalResult matchAndRewrite( + mhlo::ReduceWindowOp rw, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final { + if (rw.getNumResults() != 1 || rw.getInputs().size() != 1 || + rw.getInitValues().size() != 1) + return failure(); + + if (failed(MatchBinaryReduceFunction(rw.getBody()))) + return failure(); + + // Ensure that initial_values are as expected. + auto const_op = llvm::dyn_cast_or_null( + rw.getInitValues()[0].getDefiningOp()); + if (!const_op) return failure(); + auto const_op_dense_value = + mlir::cast(const_op.getValue()); + if (!const_op_dense_value || !IsInitValue(const_op_dense_value)) { + return failure(); + } + + auto operand_type = mlir::cast(rw.getInputs()[0].getType()); + + // For a cumulative op, require a tensor of 1s for each dimension in + // operand. + auto is_splat_int64_ones = + [&rewriter, + &operand_type](const std::optional& attr) { + // According to the definition, the default value of these attributes + // are all ones when unspecified. + if (!attr.has_value()) return true; + if (attr->getType().getShape()[0] != operand_type.getRank()) + return false; + if (!attr->isSplat()) return false; + if (attr->getElementType() != rewriter.getIntegerType(64)) + return false; + if (attr->getSplatValue().getSExtValue() != 1) return false; + return true; + }; + if (!is_splat_int64_ones(rw.getBaseDilations()) || + !is_splat_int64_ones(rw.getWindowDilations()) || + !is_splat_int64_ones(rw.getWindowStrides())) + return failure(); + + // Determine which axis is being used for the cumulative operation. + // + // For a cumulative op, window_dimensions should be of the form: + // dense<[1, 1, N, 1]> + // where N is the same as the size of the corresponding input dimension + // and there is a 1-entry for each input dimension not being operated + // over. + const auto& window_dimensions = rw.getWindowDimensions(); + if (window_dimensions.size() != operand_type.getRank()) return failure(); + int64_t cumulative_axis = -1; + for (int64_t i = 0, e = window_dimensions.size(); i < e; ++i) { + int64_t window_dimension = window_dimensions.getValues()[i]; + if (window_dimension == 1) continue; + // Cumulative axis already set. + if (cumulative_axis != -1) return failure(); + // Potential cumulative axis is not the right size. + if (window_dimension != operand_type.getShape()[i]) return failure(); + cumulative_axis = i; + } + + if (cumulative_axis == -1) { + return rewriter.notifyMatchFailure(rw, "no reduced dimension is found."); + } + + // For a cumulative op, padding (expressed as a list of left-padding and + // right-padding pairs) should be of the form: + // dense<[[0, 0], [0, 0], [N-1, 0], [0, 0]]> + // where N is the size of the input dimension being operated over. + if (!rw.getPadding()) return failure(); + const auto& padding = rw.getPadding()->getValues(); + if (padding.size() != operand_type.getRank() * 2) return failure(); + int64_t padding_value = operand_type.getShape()[cumulative_axis] - 1; + for (int64_t dim = 0; dim < operand_type.getRank(); ++dim) { + int64_t left_padding = padding[2 * dim]; + int64_t right_padding = padding[2 * dim + 1]; + if (dim == cumulative_axis) { + if (left_padding != padding_value) return failure(); + } else { + if (left_padding != 0) return failure(); + } + if (right_padding != 0) return failure(); + } + + auto axis = TF::ConstOp::create( + rewriter, rw->getLoc(), + rewriter.getIntegerAttr(rewriter.getIntegerType(64), cumulative_axis)); + + rewriter.replaceOpWithNewOp(rw, rw.getType(0), rw.getInputs()[0], + axis, /* exclusive */ false, + /* reverse */ false); + return success(); + } +}; + +class ConvertLoweredCumSumOp + : public ConvertLoweredCumOp { + using ConvertLoweredCumOp::ConvertLoweredCumOp; + bool IsInitValue(const DenseElementsAttr& attr) const override { + auto element_type = attr.getType().getElementType(); + if (attr.getNumElements() != 1 || !element_type.isIntOrFloat()) + return false; + if (mlir::isa(element_type)) { + auto value = *attr.value_begin(); + return value.isZero(); + } + auto value = *attr.value_begin(); + return value.isZero(); + } +}; + +class ConvertLoweredCumProdOp + : public ConvertLoweredCumOp { + using ConvertLoweredCumOp::ConvertLoweredCumOp; + bool IsInitValue(const DenseElementsAttr& attr) const override { + auto element_type = attr.getType().getElementType(); + if (attr.getNumElements() != 1 || !element_type.isIntOrFloat()) + return false; + if (mlir::isa(element_type)) { + auto value = *attr.value_begin(); + return value.isExactlyValue(1.0); + } + auto value = *attr.value_begin(); + return value.getSExtValue() == 1; + } +}; + +// Maps the following representations of AvgPool in MHLO into a tf.AvgPool{3D} +// operation when they cleanly map to 2D or 3D average pool with VALID or SAME +// padding: +// * div(reduce_sum_window(x), constant(sizeof(window))) +// * div(reduce_sum_window(x), reduce_sum_window(constant(1))) +class ConvertAvgPoolOp : public OpConversionPattern { + public: + explicit ConvertAvgPoolOp(MLIRContext* context) + : OpConversionPattern(context, /*benefit=*/10) {} + + LogicalResult matchAndRewrite( + mhlo::DivOp div_op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final { + auto rw = + dyn_cast_or_null(div_op.getLhs().getDefiningOp()); + if (!rw || rw->getNumResults() != 1) return failure(); + + // Check that the reduce-window is a sum-reduce-window. + if (failed(MatchBinaryReduceFunction(rw.getBody()))) + return failure(); + + // Check that this is a floating point reduce window with a rank of 4 or 5. + const RankedTensorType rw_type = + mlir::dyn_cast(rw.getResult(0).getType()); + if (!rw_type || !mlir::isa(rw_type.getElementType()) || + rw_type.getRank() <= 3 || rw_type.getRank() > 5) + return failure(); + + // Check that the Div op doesn't do broadcasting on the output of the reduce + // window. + if (div_op.getType() != rw_type) return failure(); + + // If the init value isn't zero then it can't be an average pool. + if (!isFloatZero(rw.getInitValues()[0])) return failure(); + + llvm::SmallVector window_strides; + std::string padding_mode, data_format; + if (!IsSpatialPoolingWithoutDilation(rw, &window_strides, &padding_mode, + &data_format)) { + return rewriter.notifyMatchFailure( + div_op, "not the root of spatial pooling without dilation"); + } + + DenseFPElementsAttr divisor; + auto div_rhs = recursivelyWalkUp(div_op.getRhs()); + if (matchPattern(div_rhs, m_Constant(&divisor))) { + // If the divisor is a constant then check that it matches with the number + // of elements inside the window what is required for a VALID AvgPool. + if (!divisor.isSplat()) return failure(); + int64_t window_size = 1; + for (int64_t w : rw.getWindowDimensions().getValues()) { + window_size *= w; + } + if (!divisor.getSplatValue().isExactlyValue(window_size)) + return failure(); + + if (padding_mode != "VALID") { + return failure(); + } + + return replaceWithAvgPool( + div_op, rw.getInputs()[0], + llvm::to_vector<4>(rw.getWindowDimensions().getValues()), + window_strides, "VALID", data_format, rewriter); + } + + Value actual_divisor = + recursivelyWalkUp( + div_op.getRhs()); + auto rw_rhs = + dyn_cast_or_null(actual_divisor.getDefiningOp()); + if (rw_rhs && rw_rhs.getNumResults() == 1) { + // Check that RHS is a sum-reduce-window. + if (failed(MatchBinaryReduceFunction(rw_rhs.getBody()))) + return failure(); + + // Check that the RHS is a reduce_window over a constant 1 operand with 0 + // as the init value. + DenseFPElementsAttr rhs_operand; + auto rw_rhs_operand = + recursivelyWalkUp(rw_rhs.getInputs()[0]); + if (!isFloatZero(rw_rhs.getInitValues()[0]) || + !matchPattern(rw_rhs_operand, m_Constant(&rhs_operand)) || + !rhs_operand.isSplat() || + !rhs_operand.getSplatValue().isExactlyValue(1.0)) + return failure(); + + // Check that the two reduce window have the same window configuration. + if (rw.getWindowDimensions() != rw_rhs.getWindowDimensions() || + rw.getWindowStrides() != rw_rhs.getWindowStrides() || + rw.getWindowDilations() != rw_rhs.getWindowDilations() || + rw.getBaseDilations() != rw_rhs.getBaseDilations() || + rw.getPadding() != rw_rhs.getPadding()) + return failure(); + + return replaceWithAvgPool( + div_op, rw.getInputs()[0], + llvm::to_vector<4>(rw.getWindowDimensions().getValues()), + window_strides, padding_mode, data_format, rewriter); + } + + return failure(); + } + + private: + bool isFloatZero(Value value) const { + DenseFPElementsAttr initial_value; + return matchPattern(value, m_Constant(&initial_value)) && + initial_value.getNumElements() == 1 && + initial_value.getValues()[0].isZero(); + } + + LogicalResult replaceWithAvgPool(mhlo::DivOp op, Value input, + llvm::ArrayRef ksizes, + llvm::ArrayRef kstrides, + llvm::StringRef padding, + llvm::StringRef data_format, + ConversionPatternRewriter& rewriter) const { + if (ksizes.size() == 4) { + rewriter.replaceOpWithNewOp( + op, op.getType(), input, rewriter.getI64ArrayAttr(ksizes), + rewriter.getI64ArrayAttr(kstrides), rewriter.getStringAttr(padding), + rewriter.getStringAttr(data_format)); + return success(); + } else if (ksizes.size() == 5) { + rewriter.replaceOpWithNewOp( + op, op.getType(), input, rewriter.getI64ArrayAttr(ksizes), + rewriter.getI64ArrayAttr(kstrides), rewriter.getStringAttr(padding), + rewriter.getStringAttr(data_format)); + return success(); + } + return failure(); + } + + // Walks up the op and ignore all precedding ops of type Tys. + // Returns the first producer op whose type is not in Tys. + template + Value recursivelyWalkUp(Value op) const { + while (llvm::isa_and_nonnull(op.getDefiningOp())) { + Operation* producer = op.getDefiningOp(); + op = producer->getOperand(/*idx=*/0); + } + + return op; + } +}; + +class ConvertMaxPoolOp : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::ReduceWindowOp rw, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final { + // Check that the reduce-window is a max-reduce-window. + if (failed(MatchBinaryReduceFunction(rw.getBody()))) + return failure(); + + // Check that this is a floating point reduce window with a rank of 4 or 5. + const RankedTensorType rw_type = + mlir::dyn_cast(rw.getResult(0).getType()); + if (!rw_type || !mlir::isa(rw_type.getElementType()) || + rw_type.getRank() <= 3 || rw_type.getRank() > 5) + return failure(); + + if (!isFloatMinusInfinity(rw.getInitValues()[0])) { + return failure(); + } + + llvm::SmallVector window_strides; + std::string padding_mode, data_format; + if (!IsSpatialPoolingWithoutDilation(rw, &window_strides, &padding_mode, + &data_format)) { + return rewriter.notifyMatchFailure( + rw, "not the root of spatial pooling without dilation"); + } + + return replaceWithMaxPool( + rw, rw.getInputs()[0], + llvm::to_vector<4>(rw.getWindowDimensions().getValues()), + window_strides, padding_mode, data_format, rewriter); + } + + private: + bool isFloatMinusInfinity(Value value) const { + DenseFPElementsAttr float_value; + if (!matchPattern(value, m_Constant(&float_value))) { + return false; + } + + if (float_value.getNumElements() != 1) { + return false; + } + + APFloat element = float_value.getValues()[0]; + return TFL::IsNegInfiniteValue(element); + } + + LogicalResult replaceWithMaxPool(mhlo::ReduceWindowOp op, Value input, + llvm::ArrayRef ksizes, + llvm::ArrayRef kstrides, + llvm::StringRef padding, + llvm::StringRef data_format, + ConversionPatternRewriter& rewriter) const { + if (ksizes.size() == 4) { + rewriter.replaceOpWithNewOp( + op, op.getType(0), input, rewriter.getI64ArrayAttr(ksizes), + rewriter.getI64ArrayAttr(kstrides), rewriter.getStringAttr(padding), + /*explicit_paddings=*/rewriter.getI64ArrayAttr({}), + rewriter.getStringAttr(data_format)); + return success(); + } else if (ksizes.size() == 5) { + rewriter.replaceOpWithNewOp( + op, op.getType(0), input, rewriter.getI64ArrayAttr(ksizes), + rewriter.getI64ArrayAttr(kstrides), rewriter.getStringAttr(padding), + rewriter.getStringAttr(data_format)); + return success(); + } + return failure(); + } +}; + +// Returns the shape of the given value in a Constant Op. +arith::ConstantOp ShapeToConst(PatternRewriter& rewriter, Value value) { + ArrayRef shape = mlir::cast(value.getType()).getShape(); + auto attr_type = RankedTensorType::get({static_cast(shape.size())}, + rewriter.getIntegerType(64)); + auto attr = DenseElementsAttr::get(attr_type, shape); + return arith::ConstantOp::create(rewriter, value.getLoc(), attr_type, attr); +} + +bool IsSign(APInt a, APInt sign) { + if (a.isZero()) return a == sign; + if (a.isNegative()) return sign == -1; + return sign == 1; +} + +bool IsSign(APFloat a, APFloat sign) { + if (a.isNaN() || a.isZero()) return a == sign; + if (a.isNegative()) return sign.isExactlyValue(-1.0); + return sign.isExactlyValue(1.0); +} + +bool IsDenseSplatIntAttr(ElementsAttr float_or_int) { + return mlir::isa(float_or_int) && + mlir::isa(float_or_int); +} + +bool IsDenseSplatFloatAttr(ElementsAttr float_or_int) { + return mlir::isa(float_or_int) && + mlir::isa(float_or_int); +} + +bool ValueIsReciprocal(ElementsAttr float_or_int, ElementsAttr rhs) { + if (IsDenseSplatFloatAttr(float_or_int) && + IsDenseSplatFloatAttr(float_or_int)) { + return (mlir::cast(float_or_int) + .getSplatValue() * + mlir::cast(rhs).getSplatValue()) + .isExactlyValue(1.0); + } else if (IsDenseSplatIntAttr(float_or_int) && + IsDenseSplatIntAttr(float_or_int)) { + return (mlir::cast(float_or_int).getSplatValue() * + mlir::cast(rhs).getSplatValue()) == 1; + } + return false; +} + +bool ValueEquals(ElementsAttr float_or_int, double rhs) { + if (IsDenseSplatFloatAttr(float_or_int)) { + return mlir::cast(float_or_int) + .getSplatValue() + .isExactlyValue(rhs); + } else if (IsDenseSplatIntAttr(float_or_int)) { + return mlir::cast(float_or_int).getSplatValue() == + static_cast(rhs); + } + return false; +} + +bool ValueGreaterThanZero(ElementsAttr float_or_int) { + if (IsDenseSplatIntAttr(float_or_int)) { + auto value = + mlir::cast(float_or_int).getSplatValue(); + return !value.isNegative() && !value.isZero(); + } else if (IsDenseSplatFloatAttr(float_or_int)) { + auto value = + mlir::cast(float_or_int).getSplatValue(); + return !value.isNaN() && !value.isNegative() && !value.isZero(); + } + return false; +} + +// Returns whether the splat constant is the sign of the int or float Tensor. +bool TensorIsSign(PatternRewriter& rewriter, ElementsAttr float_or_int, + ElementsAttr sgn_cst) { + auto sgn_splat = llvm::dyn_cast(sgn_cst); + if (!sgn_splat) return false; + + auto splat = dyn_cast(float_or_int); + if (auto float_spl = llvm::dyn_cast_if_present(splat), + sgn_cst_spl = llvm::dyn_cast_if_present(sgn_splat); + float_spl && sgn_cst_spl) { + return IsSign(float_spl.getValue(), sgn_cst_spl.getValue()); + } + if (auto int_spl = llvm::dyn_cast_if_present(splat), + sgn_cst_spl = llvm::dyn_cast_if_present(sgn_splat); + int_spl && sgn_cst_spl) { + return IsSign(int_spl.getValue(), sgn_cst_spl.getValue()); + } + if (mlir::isa(float_or_int)) { + auto sgn_splat_value = sgn_splat.getSplatValue(); + return llvm::all_of(float_or_int.getValues(), [&](APFloat value) { + return IsSign(value, sgn_splat_value); + }); + } + if (mlir::isa(float_or_int)) { + auto sgn_splat_value = sgn_splat.getSplatValue(); + return llvm::all_of(float_or_int.getValues(), [&](APInt value) { + return IsSign(value, sgn_splat_value); + }); + } + return false; +} + +bool SameTypeOrDefaultCompare(mhlo::ComparisonTypeAttr comparison_type_attr, + ElementsAttr cst) { + if (!comparison_type_attr) return true; + auto comparison_type_attr_value = comparison_type_attr.getValue(); + if (comparison_type_attr_value == mhlo::ComparisonType::FLOAT && + IsDenseSplatFloatAttr(cst)) { + return true; + } + if ((comparison_type_attr_value == mhlo::ComparisonType::SIGNED || + comparison_type_attr_value == mhlo::ComparisonType::UNSIGNED) && + IsDenseSplatIntAttr(cst)) { + return true; + } + return false; +} + +// Tries to convert an mhlo::GatherOp into a TF::GatherNdOp (or TF::SliceOp). +// +// Consider the following example: +// operand_shape = [B1, I1, O1, B2, I2, O2] +// operand_batching_dims = [0, 3] +// +// start_indices_shape = [B2, B3, B1, 2] +// start_indices_batching_dims = [3, 0] +// index_vector_dim = 3 +// start_index_map = [4, 1] +// +// offset_dims: [2, 4] +// slice_sizes = [1, 1, O1, 1, 1, O2] +// collapsed_slice_dims = [1, 4] +// result_shape = [B2, B3, O1, B3, O2] +// +// To implement this with a tf.GatherNd, we canonicalize the operand s.t. the +// operand batching dimensions are flattened into the leading dimensions, +// followed by the indexed dimensions in order: +// canonical_operand_shape = [B1 * B2, I2, I1, O1, O2] +// +// We canonicalize the start indices so the start indices batching dimensions +// are flattened (in order) into a leading dimension. In addition, we add iota +// indices to appropriately offset into the flattened operand batching +// dimension: +// canonical_start_indices_shape = [B1 * B2, B3, 3] +// (index_vector_dim is expanded to included indices for the operand +// batching dimensions) +// +// The result of tf.GatherNd(canonical_operand, canonical_start_indices) has the +// following shape: +// canonical_result_shape = [B1 * B2, B3, O1, O2] +// +// The canonical result is unflattened and transpose as needed to get back to +// the original result shape. +class ConvertGatherOp : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::GatherOp gather_op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final { + // First see if we can convert the gather to a tf.Slice. + if (succeeded(ConvertGatherOpToSlice(gather_op, rewriter))) { + return success(); + } + + Value operand = gather_op.getOperand(); + Value start_indices = gather_op.getStartIndices(); + + // Can only convert with static shaped gather. + ShapedType operand_type = mlir::cast(operand.getType()); + ShapedType start_indices_type = + mlir::cast(start_indices.getType()); + ShapedType result_type = + mlir::cast(gather_op.getResult().getType()); + if (!operand_type.hasStaticShape()) { + gather_op.emitOpError() << "Dynamic shaped operand is not supported."; + return failure(); + } + + llvm::ArrayRef operand_batching_dims = + gather_op.getDimensionNumbers().getOperandBatchingDims(); + llvm::ArrayRef start_indices_batching_dims = + gather_op.getDimensionNumbers().getStartIndicesBatchingDims(); + llvm::ArrayRef start_index_map = + gather_op.getDimensionNumbers().getStartIndexMap(); + llvm::ArrayRef collapsed_slice_dims = + gather_op.getDimensionNumbers().getCollapsedSliceDims(); + if (!start_indices_type.hasStaticShape() || !result_type.hasStaticShape()) { + // Dynamic dimensions aren't supported in certain cases that require + // reshaping the indices or result. + if (!start_indices_batching_dims.empty()) { + gather_op.emitOpError() + << "Dynamic shaped start indices aren't supported when there are " + "batching dimensions."; + } + + // Verify that start_index_map and collapsed_slice_dims contains the same + // values. + if (start_index_map.size() != collapsed_slice_dims.size()) { + return rewriter.notifyMatchFailure( + gather_op, + "different size for start index map and collapsed slice dims"); + } + for (auto c : collapsed_slice_dims) { + if (llvm::count(start_index_map, c) == 0) { + return rewriter.notifyMatchFailure( + gather_op, + "collapsed slice dim isn't present in start index map"); + } + } + } + + // Normalize start_indices so index_vector_dim == start_indices.rank() - 1. + int64_t index_vector_dim = + gather_op.getDimensionNumbers().getIndexVectorDim(); + if (failed(NormalizeIndexVector(gather_op, start_indices, + start_indices_type, index_vector_dim, + rewriter))) { + return failure(); + } + start_indices_type = mlir::cast(start_indices.getType()); + + // Verify that slice_sizes is 1 for the batching dimensions and the full + // shape for non-indexed dimensions. + auto slice_sizes = gather_op.getSliceSizes(); + llvm::SmallVector slice_sizes_vector; + slice_sizes_vector.reserve(slice_sizes.size()); + for (int64_t s : slice_sizes.getValues()) { + slice_sizes_vector.push_back(s); + } + for (int i = 0; i < slice_sizes_vector.size(); ++i) { + int s = slice_sizes_vector[i]; + if (llvm::count(operand_batching_dims, i)) { + if (s != 1) { + return rewriter.notifyMatchFailure(gather_op, + "unsupported slice sizes"); + } + } else if (llvm::count(start_index_map, i) == 0) { + if (s != operand_type.getShape()[i]) { + return rewriter.notifyMatchFailure(gather_op, + "unsupported slice sizes"); + } + } + } + + // Canonicalize the operand and start indices. + auto canonical_operand = + CanonicalizeOperand(gather_op, operand, operand_type, + operand_batching_dims, start_index_map, rewriter); + auto canonical_operand_type = + mlir::cast(canonical_operand.getType()); + + auto canonical_start_indices = + CanonicalizeStartIndices(gather_op, start_indices, start_indices_type, + start_indices_batching_dims, start_index_map, + slice_sizes_vector, rewriter); + auto canonical_start_indices_type = + mlir::cast(canonical_start_indices.getType()); + + TF::CastOp cast_op = nullptr; + if (canonical_start_indices_type.getElementType().isUnsignedInteger(32)) { + cast_op = TF::CastOp::create( + rewriter, gather_op->getLoc(), + RankedTensorType::get(canonical_start_indices_type.getShape(), + rewriter.getI64Type()), + canonical_start_indices); + } + + llvm::SmallVector canonical_result_shape; + for (int64_t i = 0; i < canonical_start_indices_type.getRank() - 1; ++i) { + canonical_result_shape.push_back( + canonical_start_indices_type.getDimSize(i)); + } + for (int64_t i = canonical_start_indices_type.getDimSize( + canonical_start_indices_type.getRank() - 1); + i < canonical_operand_type.getRank(); ++i) { + canonical_result_shape.push_back(canonical_operand_type.getDimSize(i)); + } + + auto canonical_result_type = RankedTensorType::get( + canonical_result_shape, result_type.getElementType()); + auto canonical_result = TF::GatherNdOp::create( + rewriter, gather_op->getLoc(), canonical_result_type, canonical_operand, + cast_op ? cast_op.getResult() : canonical_start_indices); + + auto offset_dims = gather_op.getDimensionNumbers().getOffsetDims(); + auto final_result = UncanonicalizeResult( + gather_op, canonical_result, canonical_result_type, result_type, + offset_dims, operand_batching_dims, start_indices_batching_dims, + start_index_map, slice_sizes_vector, collapsed_slice_dims, rewriter); + + rewriter.replaceOp(gather_op, final_result); + return success(); + } + + // Convert gather op to tf.slice and tf.concat + LogicalResult ConvertGatherOpToSlice( + mhlo::GatherOp gather_op, ConversionPatternRewriter& rewriter) const { + Value operand = gather_op.getOperand(); + Value start_indices = gather_op.getStartIndices(); + static const int rank_two = 2; + // This converts a gather op to multiple slice ops, cap the number of slice + // ops allowed. + static const int max_batch_size = 50; + + // Can only convert with static shaped gather. + ShapedType operand_type = mlir::cast(operand.getType()); + ShapedType start_indices_type = + mlir::cast(start_indices.getType()); + ShapedType result_type = + mlir::cast(gather_op.getResult().getType()); + if (!operand_type.hasStaticShape() || + !start_indices_type.hasStaticShape() || !result_type.hasStaticShape()) { + return rewriter.notifyMatchFailure( + gather_op, + "Dynamic shaped inputs are not supported when legalizing mhlo.gather " + "op to tf.slice."); + } + + auto start_index_map = gather_op.getDimensionNumbers().getStartIndexMap(); + auto collapsed_slice_dims = + gather_op.getDimensionNumbers().getCollapsedSliceDims(); + auto offset_dims = gather_op.getDimensionNumbers().getOffsetDims(); + auto slice_sizes = gather_op.getSliceSizes(); + llvm::SmallVector slice_sizes_vector; + slice_sizes_vector.reserve(slice_sizes.size()); + for (int64_t s : slice_sizes.getValues()) { + slice_sizes_vector.push_back(s); + } + + llvm::SmallVector batch_dims; + // Offset dims are guaranteed to be sorted. + int offset_index = 0; + for (int64_t i = 0; i < result_type.getRank(); ++i) { + if (offset_index >= offset_dims.size() || + offset_dims[offset_index] != i) { + batch_dims.push_back(i); + } else { + ++offset_index; + } + } + // Here we only support gather with one batch dim and the batch dim is 0. + if (batch_dims.size() != 1 || batch_dims[0] != 0) { + return failure(); + } + int64_t batch_dim = batch_dims[0]; + // Batch dim in operand and start indices should match. + if (operand_type.getDimSize(batch_dim) > max_batch_size || + operand_type.getRank() != rank_two || + start_indices_type.getRank() != rank_two || + operand_type.getDimSize(batch_dim) != + start_indices_type.getDimSize(batch_dim) || + slice_sizes_vector[batch_dim] != 1) { + return failure(); + } + // Here we only support the case where [0, 1] in start_indices maps to + // operand[0, 1] + for (int64_t i = 0; i < start_index_map.size(); i++) { + if (start_index_map[i] != i) { + return failure(); + } + } + // Collapsed slice dims should contain the batch dim. + if (collapsed_slice_dims.size() != start_index_map.size() - 1 || + collapsed_slice_dims.size() != 1 || collapsed_slice_dims[0] != 0) { + return failure(); + } + + // Normalize start_indices so index_vector_dim == start_indices.rank() - 1. + int64_t index_vector_dim = + gather_op.getDimensionNumbers().getIndexVectorDim(); + if (failed(NormalizeIndexVector(gather_op, start_indices, + start_indices_type, index_vector_dim, + rewriter))) { + return failure(); + } + + ImplicitLocOpBuilder builder(gather_op.getLoc(), rewriter); + // Clamp the start indices to ensure it is in bounds. + auto max_start_indices = BuildIntArrayConstOp( + builder, rewriter, + llvm::SmallVector( + {operand_type.getDimSize(0) - slice_sizes_vector[0], + operand_type.getDimSize(1) - slice_sizes_vector[1]}), + start_indices_type.getElementType()); + auto min_start_indices = BuildIntArrayConstOp( + builder, rewriter, llvm::SmallVector({0, 0}), + start_indices_type.getElementType()); + auto start_indices_max_op = TF::MaximumOp::create( + rewriter, gather_op.getLoc(), start_indices, min_start_indices); + auto clamped_start_indices_op = TF::MinimumOp::create( + rewriter, gather_op.getLoc(), start_indices_max_op, max_start_indices); + + int64_t batch_size = start_indices_type.getDimSize(batch_dim); + auto slice_size = BuildIntArrayConstOp( + builder, rewriter, slice_sizes_vector, rewriter.getI32Type()); + if (batch_size == 1) { + auto squeeze_op = TF::SqueezeOp::create( + rewriter, gather_op.getLoc(), + RankedTensorType::get({rank_two}, + start_indices_type.getElementType()), + clamped_start_indices_op, + rewriter.getI64ArrayAttr(llvm::ArrayRef({batch_dim}))); + auto slice_op = + TF::SliceOp::create(rewriter, gather_op.getLoc(), gather_op.getType(), + operand, squeeze_op, slice_size); + rewriter.replaceOp(gather_op, slice_op); + return mlir::success(); + } + + llvm::SmallVector slices; + slices.reserve(batch_size); + for (int64_t i = 0; i < batch_size; ++i) { + auto zero = BuildIntArrayConstOp(builder, rewriter, + llvm::SmallVector({i, 0}), + rewriter.getI32Type()); + auto two = BuildIntArrayConstOp(builder, rewriter, + llvm::SmallVector({1, 2}), + rewriter.getI32Type()); + auto begin = TF::SliceOp::create( + rewriter, gather_op.getLoc(), + RankedTensorType::get({1, 2}, start_indices_type.getElementType()), + clamped_start_indices_op, zero, two); + auto squeeze_op = TF::SqueezeOp::create( + rewriter, gather_op.getLoc(), + RankedTensorType::get({rank_two}, + start_indices_type.getElementType()), + begin, + rewriter.getI64ArrayAttr(llvm::ArrayRef({batch_dim}))); + auto slice_op = TF::SliceOp::create( + rewriter, gather_op.getLoc(), + RankedTensorType::get({1, slice_sizes_vector[1]}, + operand_type.getElementType()), + operand, squeeze_op, slice_size); + slices.push_back(slice_op); + } + auto scalar_type = RankedTensorType::get({}, rewriter.getI32Type()); + auto zero_scalar = TF::ConstOp::create( + rewriter, gather_op.getLoc(), + DenseIntElementsAttr::get(scalar_type, static_cast(0))); + auto concat_op = TF::ConcatV2Op::create(rewriter, gather_op.getLoc(), + result_type, slices, zero_scalar); + rewriter.replaceOp(gather_op, concat_op); + return mlir::success(); + } + + private: + // Transform the canonicalized result produced by tf.GatherNd with the + // canonicalized operand and start indices back into the original result. + // The canonicalized result will have the start indices batching dimensions + // flattened as leading dimension, and the offset dimensions as trailing + // dimensions. To transform back, we: + // - Unflatten the start indices batching dimensions. + // - Introduce trivial index dimensions that aren't in `collapsed_slice_dims`. + // - Transpose dimensions back based on `offset_dims` and + // `start_indices_batching_dims`. + Value UncanonicalizeResult(mhlo::GatherOp gather_op, Value canonical_result, + ShapedType canonical_result_type, + ShapedType original_result_type, + ArrayRef offset_dims, + ArrayRef operand_batching_dims, + ArrayRef start_indices_batching_dims, + ArrayRef start_index_map, + ArrayRef slice_sizes, + ArrayRef collapsed_slice_dims, + ConversionPatternRewriter& rewriter) const { + // For those dims NOT inside the original_offset_dims are considered "batch + // dims". + std::vector batch_dims; + // Offset dims are guaranteed to be sorted. + int offset_index = 0; + for (int64_t i = 0; i < original_result_type.getRank(); ++i) { + if (offset_index >= offset_dims.size() || + offset_dims[offset_index] != i) { + batch_dims.push_back(i); + } else { + ++offset_index; + } + } + + // Determine the canonical shape after unflattening the start indices + // batching dimensions (if they exist) and introducing any trivial index + // dimensions that weren't collapsed. Also compute the permutation to + // transform the original shape to the unflattened canonical shape. + llvm::SmallVector permutation_to_canonical; + llvm::SmallVector unflattened_shape; + for (int64_t i : start_indices_batching_dims) { + int64_t dim = batch_dims[i]; + permutation_to_canonical.push_back(dim); + unflattened_shape.push_back(original_result_type.getDimSize(dim)); + } + for (int64_t i = 0; i < batch_dims.size(); ++i) { + if (llvm::count(start_indices_batching_dims, i) == 0) { + int64_t dim = batch_dims[i]; + permutation_to_canonical.push_back(dim); + unflattened_shape.push_back(original_result_type.getDimSize(dim)); + } + } + // The remaining dimensions are the offset dims. We expect non-collapsed + // indexed dimensions first, followed by the rest of the operand dimensions. + llvm::SmallVector operand_dim_to_offset_dim_map(slice_sizes.size(), + -1); + int offset_dim_index = 0; + llvm::SmallVector remaining_operand_dims; + for (int64_t operand_dim = 0; operand_dim < slice_sizes.size(); + ++operand_dim) { + if (llvm::count(collapsed_slice_dims, operand_dim) || + llvm::count(operand_batching_dims, operand_dim)) { + continue; + } else { + if (llvm::count(start_index_map, operand_dim) == 0) { + remaining_operand_dims.push_back(operand_dim); + } + operand_dim_to_offset_dim_map[operand_dim] = + offset_dims[offset_dim_index++]; + } + } + for (int64_t s : start_index_map) { + if (llvm::count(collapsed_slice_dims, s) == 0) { + int64_t dim = operand_dim_to_offset_dim_map[s]; + permutation_to_canonical.push_back(dim); + unflattened_shape.push_back(original_result_type.getDimSize(dim)); + } + } + for (int64_t operand_dim : remaining_operand_dims) { + int64_t dim = operand_dim_to_offset_dim_map[operand_dim]; + permutation_to_canonical.push_back(dim); + unflattened_shape.push_back(original_result_type.getDimSize(dim)); + } + + // Reshape the result to unflatten the batching dimensions and add back any + // non-collapsed indexed dimensions. The caller should ensure that a + // reshape is not needed if the result has dynamic dimensions. + if (canonical_result_type.hasStaticShape()) { + auto unflattened_result_type = RankedTensorType::get( + unflattened_shape, original_result_type.getElementType()); + canonical_result = + mhlo::ReshapeOp::create(rewriter, gather_op.getLoc(), + unflattened_result_type, canonical_result); + } + // Transpose back to the original result shape. + return mhlo::TransposeOp::create( + rewriter, gather_op.getLoc(), original_result_type, canonical_result, + rewriter.getI64TensorAttr( + GetInversePermutationArray(permutation_to_canonical))); + } + + // Canonicalize `operand` to handle operand batching dimensions and non-iota + // start index map, so it can be used by tf.GatherNd: + // - Transpose so that the leading dimensions are the operand batching + // dimensions followed by the indexed dimensions (in order). + // - Flatten the batching dimensions. + Value CanonicalizeOperand(mhlo::GatherOp gather_op, Value operand, + ShapedType operand_type, + ArrayRef operand_batching_dims, + ArrayRef start_index_map, + ConversionPatternRewriter& rewriter) const { + int batch_size = 1; + llvm::SmallVector permutation; + llvm::SmallVector transposed_shape; + llvm::SmallVector flattened_shape; + // First add the batching dimensions. + for (int64_t batch_dim : operand_batching_dims) { + permutation.push_back(batch_dim); + transposed_shape.push_back(operand_type.getDimSize(batch_dim)); + batch_size *= operand_type.getDimSize(batch_dim); + } + if (!operand_batching_dims.empty()) { + flattened_shape.push_back(batch_size); + } + // Add the indexed dimensions. + for (int64_t s : start_index_map) { + permutation.push_back(s); + transposed_shape.push_back(operand_type.getDimSize(s)); + flattened_shape.push_back(operand_type.getDimSize(s)); + } + // Finally, add the remaining dimensions. + for (int64_t i = 0; i < operand_type.getRank(); i++) { + if (llvm::count(operand_batching_dims, i) == 0 && + llvm::count(start_index_map, i) == 0) { + permutation.push_back(i); + transposed_shape.push_back(operand_type.getDimSize(i)); + flattened_shape.push_back(operand_type.getDimSize(i)); + } + } + + // Transpose the dimensions and flatten the batching dimensions. + RankedTensorType transposed_type = + RankedTensorType::get(transposed_shape, operand_type.getElementType()); + auto transposed_operand = mhlo::TransposeOp::create( + rewriter, gather_op.getLoc(), transposed_type, operand, + rewriter.getI64TensorAttr(permutation)); + auto flattened_type = + RankedTensorType::get(flattened_shape, operand_type.getElementType()); + auto flattened_operand = mhlo::ReshapeOp::create( + rewriter, gather_op.getLoc(), flattened_type, transposed_operand); + return flattened_operand; + } + + // Canonicalize `start_indices` to handle start indices batching dimensions so + // it can be used by tf.GatherNd: + // - Transpose so that the batching dimensions are the leading dimensions. + // - Flatten the batching dimensions if they exist. + // - For each indexed dimension with non-trivial slicing, introduce a new + // dimension, and broadcast and add iota values to the indices. + // - Add iota index values for the operand batching dimensions. + Value CanonicalizeStartIndices(mhlo::GatherOp gather_op, Value start_indices, + ShapedType start_indices_type, + ArrayRef start_indices_batching_dims, + ArrayRef start_index_map, + ArrayRef slice_sizes, + ConversionPatternRewriter& rewriter) const { + int batch_size = 1; + llvm::SmallVector permutation; + llvm::SmallVector transposed_shape; + llvm::SmallVector reshaped_shape; + + // First add the batching dimensions. + for (int64_t batch_dim : start_indices_batching_dims) { + permutation.push_back(batch_dim); + transposed_shape.push_back(start_indices_type.getDimSize(batch_dim)); + batch_size *= start_indices_type.getDimSize(batch_dim); + } + if (!start_indices_batching_dims.empty()) { + reshaped_shape.push_back(batch_size); + } + + // Add remaining dimensions before the final index vector dim. + for (int64_t dim = 0; dim < start_indices_type.getRank() - 1; dim++) { + if (llvm::count(start_indices_batching_dims, dim) == 0) { + permutation.push_back(dim); + transposed_shape.push_back(start_indices_type.getDimSize(dim)); + reshaped_shape.push_back(start_indices_type.getDimSize(dim)); + } + } + + // Introduce new dimensions associated with each indexed operand dimension + // that is taking a non-trivial slice. We will broadcast and add iota values + // after reshaping. See comment below for more details. + int64_t first_non_trivial_sliced_dim = reshaped_shape.size(); + for (int64_t operand_dim : start_index_map) { + if (slice_sizes[operand_dim] > 1) { + reshaped_shape.push_back(1); + } + } + + // Add the index vector dimension. + int64_t index_vector_size = + start_indices_type.getDimSize(start_indices_type.getRank() - 1); + permutation.push_back(permutation.size()); + transposed_shape.push_back(index_vector_size); + reshaped_shape.push_back(index_vector_size); + + // Transpose the dimensions and flatten the batching dimensions. + auto transposed_start_indices = mhlo::TransposeOp::create( + rewriter, gather_op.getLoc(), + RankedTensorType::get(transposed_shape, + start_indices_type.getElementType()), + start_indices, rewriter.getI64TensorAttr(permutation)); + start_indices = mhlo::ReshapeOp::create( + rewriter, gather_op.getLoc(), + RankedTensorType::get(reshaped_shape, + start_indices_type.getElementType()), + transposed_start_indices); + + // Because tf.GatherNd does not support non-trivial slicing on indexed + // dimensions, we introduce new dimensions in start_indices and broadcast + // and add iota values to the indices. For example: + // + // operand_shape = [10, 10, 10] + // start_indices_original_shape = [1, 3] + // start_index_map = [0, 1, 2] + // slice_sizes = [1, 5, 1] + // + // We then transform the start indices by broadcasting the shape to + // [1, 5, 3], and adding the iota tensor with the following values: + // + // [[[ 0 0 0 ] + // [ 0 1 0 ] + // [ 0 2 0 ] + // [ 0 3 0 ] + // [ 0 4 0 ]]] + // + // This allows us to take trivial slices when indexing into operand + // dimension 1. + llvm::SmallVector start_indices_shape = reshaped_shape; + int64_t non_trivial_sliced_dim = first_non_trivial_sliced_dim; + for (int i = 0; i < start_index_map.size(); ++i) { + int64_t operand_dim = start_index_map[i]; + if (slice_sizes[operand_dim] == 1) { + continue; + } + // Create iota values along the sliced dimension. + llvm::SmallVector offsets_shape(start_indices_shape.size(), 1); + offsets_shape[non_trivial_sliced_dim] = slice_sizes[operand_dim]; + start_indices_shape[non_trivial_sliced_dim] = slice_sizes[operand_dim]; + auto offsets = mhlo::IotaOp::create( + rewriter, gather_op.getLoc(), + RankedTensorType::get(offsets_shape, + start_indices_type.getElementType()), + rewriter.getI64IntegerAttr(non_trivial_sliced_dim)); + non_trivial_sliced_dim++; + + // Pad with 0s on the other operand dimensions. + Value zero = arith::ConstantOp::create( + rewriter, gather_op.getLoc(), + rewriter.getZeroAttr( + RankedTensorType::get({}, start_indices_type.getElementType()))); + int rank = offsets_shape.size(); + llvm::SmallVector padding_low(rank, 0); + llvm::SmallVector padding_high(rank, 0); + llvm::SmallVector padding_interior(rank, 0); + padding_low.back() = i; + padding_high.back() = start_indices_shape.back() - i - 1; + auto padded_offsets = + mhlo::PadOp::create(rewriter, gather_op.getLoc(), offsets, zero, + GetI64ElementsAttr(padding_low, &rewriter), + GetI64ElementsAttr(padding_high, &rewriter), + GetI64ElementsAttr(padding_interior, &rewriter)); + + // Add the padded offsets to the start indices (with broadcasting). + start_indices = TF::AddOp::create(rewriter, gather_op.getLoc(), + start_indices, padded_offsets); + } + + if (!start_indices_batching_dims.empty()) { + // Concat iota values for indexing into the batching dimensions of the + // operand. + llvm::SmallVector offsets_shape = start_indices_shape; + offsets_shape.back() = 1; + auto offsets = mhlo::IotaOp::create( + rewriter, gather_op.getLoc(), + RankedTensorType::get(offsets_shape, + start_indices_type.getElementType()), + rewriter.getI64IntegerAttr(0)); + + start_indices_shape.back()++; + start_indices = mhlo::ConcatenateOp::create( + rewriter, gather_op.getLoc(), + RankedTensorType::get(start_indices_shape, + start_indices_type.getElementType()), + ValueRange{offsets, start_indices}, + rewriter.getI32IntegerAttr(start_indices_shape.size() - 1)); + } + + return start_indices; + } +}; + +class ConvertWhileOp : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::WhileOp while_op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final { + // HLO WhileOp should have two regions: cond and body. + if (while_op->getNumRegions() != 2) return failure(); + + // This rule doesn't support mhlo::WhileOp with tuple inputs. + for (auto type : while_op->getOperandTypes()) { + if (mlir::isa(type)) return failure(); + } + + // Creates a TF::WhileRegionOp to replace the mhlo::WhileOp. HLO WhileOp + // currently doesn't support stateless and shape invariant, so these + // parameters are set to the default values. + auto new_while = TF::WhileRegionOp::create( + rewriter, while_op.getLoc(), while_op->getResultTypes(), + while_op->getOperands(), + /*parallel_iterations=*/10, + /*is_stateless=*/false, /*shape_invariant=*/false); + new_while.getCond().takeBody(while_op.getCond()); + new_while.getBody().takeBody(while_op.getBody()); + ReplaceReturnOp(new_while.getCond(), rewriter); + ReplaceReturnOp(new_while.getBody(), rewriter); + rewriter.replaceOp(while_op, new_while.getResults()); + return success(); + } +}; + +class ConvertIfOp : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::IfOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final { + // HLO IfOp currently doesn't support stateless + auto new_op = TF::IfRegionOp::create( + rewriter, op.getLoc(), op->getResultTypes(), op.getPred(), + /*is_stateless=*/false, /*_then_func_name=*/nullptr, + /*_else_func_name=*/nullptr); + new_op.getThenBranch().takeBody(op.getTrueBranch()); + new_op.getElseBranch().takeBody(op.getFalseBranch()); + ReplaceReturnOp(new_op.getThenBranch(), rewriter); + ReplaceReturnOp(new_op.getElseBranch(), rewriter); + rewriter.replaceOp(op, new_op.getResults()); + return success(); + } +}; + +// Converts mhlo.pad to tf.PadV2 +Value ConvertPadOp(PatternRewriter& rewriter, Operation* old_op) { + auto pad_op = cast(old_op); + mlir::Location loc = pad_op.getLoc(); + + // Calculates non-negative padding amount and slice begins/sizes. + llvm::SmallVector padding; + llvm::SmallVector pad_output_shape; + + bool has_negative_padding_amount = false; + llvm::SmallVector slice_begins; + llvm::SmallVector slice_sizes; + + for (auto p : llvm::zip(pad_op.getOperand().getType().getShape(), + pad_op.getEdgePaddingLow().getValues(), + pad_op.getEdgePaddingHigh().getValues())) { + const int64_t input_dim_size = std::get<0>(p); + int64_t pad_output_dim_size = input_dim_size; + + const int64_t pad_low = std::get<1>(p).getSExtValue(); + if (pad_low < 0) { + has_negative_padding_amount = true; + padding.push_back(0); + } else { + padding.push_back(pad_low); + pad_output_dim_size += pad_low; + } + + const int64_t pad_high = std::get<2>(p).getSExtValue(); + if (pad_high < 0) { + has_negative_padding_amount = true; + padding.push_back(0); + } else { + padding.push_back(pad_high); + pad_output_dim_size += pad_high; + } + + pad_output_shape.push_back(pad_output_dim_size); + + slice_begins.push_back(pad_low < 0 ? -pad_low : 0); + slice_sizes.push_back(input_dim_size + pad_low + pad_high); + } + + // Convert to PadV2. + auto padding_attr_type = RankedTensorType::get( + {pad_op.getEdgePaddingLow().size(), 2}, rewriter.getI64Type()); + auto padding_attr = DenseIntElementsAttr::get(padding_attr_type, padding); + auto padding_amount_const_op = + arith::ConstantOp::create(rewriter, loc, padding_attr_type, padding_attr); + auto new_pad_op = TF::PadV2Op::create( + rewriter, loc, pad_op.getType().clone(pad_output_shape), + pad_op.getOperand(), padding_amount_const_op, pad_op.getPaddingValue()); + if (!has_negative_padding_amount) { + return new_pad_op; + } + + // Convert negative padding amount into slice. + auto slice_attr_type = RankedTensorType::get( + {pad_op.getEdgePaddingLow().size()}, rewriter.getI64Type()); + auto slice_begins_const_op = arith::ConstantOp::create( + rewriter, loc, slice_attr_type, + DenseIntElementsAttr::get(slice_attr_type, slice_begins)); + auto slice_sizes_const_op = arith::ConstantOp::create( + rewriter, loc, slice_attr_type, + DenseIntElementsAttr::get(slice_attr_type, slice_sizes)); + return TF::SliceOp::create(rewriter, loc, pad_op.getType(), new_pad_op, + slice_begins_const_op, slice_sizes_const_op); +} + +class ConvertPopulationCountOp + : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::PopulationCountOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final { + auto output_type = op.getType().clone( + rewriter.getIntegerType(/*width=*/8, /*isSigned=*/false)); + auto pop_cnt = TF::PopulationCountOp::create(rewriter, op.getLoc(), + output_type, op.getOperand()); + auto cast_or_pop_cnt = + rewriter.createOrFold(op.getLoc(), op.getType(), pop_cnt); + rewriter.replaceOp(op, {cast_or_pop_cnt}); + return success(); + } +}; + +class ConvertCustomCallWithApproxTopK + : public mlir::OpConversionPattern { + public: + explicit ConvertCustomCallWithApproxTopK(MLIRContext* context, + mlir::ModuleOp* module_op) + : OpConversionPattern(context, /*benefit=*/0), + module_op_(module_op) {} + + mlir::LogicalResult matchAndRewrite( + mhlo::CustomCallOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final { + if (op.getCallTargetName() != "ApproxTopK") { + return mlir::failure(); + } + auto is_supported_attr_name = [](NamedAttribute attr) { + auto name = attr.getName(); + return name == "call_target_name" || name == "backend_config" || + name == "api_version" || name == "called_computations"; + }; + for (const auto& attr : op->getAttrs()) { + if (!is_supported_attr_name(attr)) { + return op.emitOpError() + << attr.getName().getValue() + << " is not a supported attribute for ApproxTopK"; + } + } + auto backend_config = + mlir::dyn_cast_or_null(op.getBackendConfigAttr()); + if (!backend_config) { + return op.emitOpError() << "Missing backend_config attribute"; + } + + for (const auto& attr : backend_config) { + auto name = attr.getName(); + if (!(name == "top_k" || name == "reduction_dim" || + name == "recall_target" || name == "aggregate_to_topk" || + name == "reduction_input_size_override" || name == "is_fallback")) { + return op.emitOpError() + << name.getValue() << " is not a supported backend_config" + << " attribute for ApproxTopK"; + } + } + + auto check_i64_attr = + [&](const std::string& attr_name) -> mlir::LogicalResult { + if (!backend_config.contains(attr_name)) { + return op.emitOpError() + << "Missing " << attr_name << " attribute in backend_config"; + } + auto attr = backend_config.getAs(attr_name); + if (!attr || !attr.getType().isInteger(64)) { + return op.emitOpError() + << attr_name + << " attribute in backend_config must be of i64 type"; + } + return success(); + }; + auto check_f32_attr = + [&](const std::string& attr_name) -> mlir::LogicalResult { + if (!backend_config.contains(attr_name)) { + return op.emitOpError() + << "Missing " << attr_name << " attribute in backend_config"; + } + auto attr = backend_config.getAs(attr_name); + if (!attr || !attr.getType().isF32()) { + return op.emitOpError() + << attr_name + << " attribute in backend_config must be of f32 type"; + } + return success(); + }; + auto check_bool_attr = + [&](const std::string& attr_name) -> mlir::LogicalResult { + if (!backend_config.contains(attr_name)) { + return op.emitOpError() + << "Missing " << attr_name << " attribute in backend_config"; + } + auto attr = backend_config.getAs(attr_name); + if (!attr) { + return op.emitOpError() + << attr_name + << " attribute in backend_config must be of bool type"; + } + return success(); + }; + if (failed(check_i64_attr("top_k"))) return failure(); + if (failed(check_i64_attr("reduction_dim"))) return failure(); + if (failed(check_f32_attr("recall_target"))) return failure(); + if (failed(check_bool_attr("aggregate_to_topk"))) return failure(); + if (failed(check_i64_attr("reduction_input_size_override"))) { + return failure(); + } + bool has_is_fallback = backend_config.contains("is_fallback"); + if (has_is_fallback && !backend_config.getAs("is_fallback")) { + return op.emitOpError() + << "is_fallback attribute in backend_config must be of bool type"; + } + + auto top_k_attr = backend_config.getAs("top_k"); + auto reduction_dim_attr = + backend_config.getAs("reduction_dim"); + auto recall_target_attr = backend_config.getAs("recall_target"); + auto aggregate_to_topk_attr = + backend_config.getAs("aggregate_to_topk"); + auto reduction_input_size_override_attr = + backend_config.getAs("reduction_input_size_override"); + if (op.getInputs().size() % 2 != 0) { + return op.emitOpError() << "ApproxTopK takes an even number of operands."; + } + + auto called_computations = op.getCalledComputations(); + if (called_computations.size() != 1) { + return op.emitOpError() + << "ApproxTopK takes exactly 1 called_computation."; + } + mlir::func::FuncOp callee = module_op_->lookupSymbol( + mlir::cast(op.getCalledComputations()[0])); + mlir::FunctionType callee_type = callee.getFunctionType(); + SmallVector expected_callee_input_types; + auto num_inputs = op.getInputs().size() / 2; + for (unsigned i = 0; i < num_inputs; ++i) { + auto input_type = + mlir::dyn_cast(op.getOperand(i).getType()); + auto scalar = RankedTensorType::get({}, input_type.getElementType()); + expected_callee_input_types.push_back(scalar); + expected_callee_input_types.push_back(scalar); + } + FunctionType expected_callee_type = mlir::FunctionType::get( + op->getContext(), expected_callee_input_types, + RankedTensorType::get({}, IntegerType::get(op->getContext(), 1))); + if (callee_type != expected_callee_type) { + return op.emitOpError() + << "called_computation type does not match the expected type. Got " + << callee_type << " expected " << expected_callee_type; + } + if (!MatchTopKComparator(callee.getBody())) { + return op.emitOpError() << "only match for GT comparator"; + } + auto is_max_k = rewriter.getBoolAttr(true); + + auto approx_top_k = TF::ApproxTopKOp::create( + rewriter, op.getLoc(), op->getResultTypes(), op.getInputs()[0], + top_k_attr, reduction_dim_attr, recall_target_attr, is_max_k, + reduction_input_size_override_attr, aggregate_to_topk_attr); + + rewriter.replaceOp(op, approx_top_k.getResults()); + return mlir::success(); + } + + private: + mlir::ModuleOp* module_op_; +}; + +// Removes the `mhlo.custom_call @shape_assertion` custom call which represents +// an assertion that the first operand (`assert_what`) evaluates to `true`. +// This is a temporary workaround for unblocking dynamic model conversion +// because starting from version 7, in presence of shape polymorphism JAX will +// emit stablehlo.custom_call @shape_assertion to verify at compile time that +// the code is used with compatible actual shapes. +// TFLite runtime kernels support shape checking and shape inference to some +// extent, it is okay to remove the shape assertion in most scenarios. However +// this is not always the case, JAX may trace the program differently based on +// the shape polymorphism specification, for example, if the program contains +// a conditional on "x.shape[0] % 2 == 0" that conditional would evaluate to +// True with x specified as (2*b, ...) and False otherwise. We can revisit +// this when need arises. See b/295316438 for details. +class RemoveCustomCallWithShapeAssertion + : public OpConversionPattern { + public: + explicit RemoveCustomCallWithShapeAssertion(MLIRContext* context) + : OpConversionPattern(context, /*benefit=*/0) {} + + LogicalResult matchAndRewrite( + mhlo::CustomCallOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final { + if (op.getCallTargetName() != "shape_assertion") { + return mlir::failure(); + } + rewriter.eraseOp(op); + return success(); + } +}; + +// Converts a MHLO::GetDimensionSizeOp to TF ops. +class ConvertGetDimensionSizeOp + : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::GetDimensionSizeOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final { + ImplicitLocOpBuilder builder(op.getLoc(), rewriter); + Value shape_op = TF::ShapeOp::create(rewriter, op.getLoc(), op.getOperand(), + rewriter.getBoolAttr(true)); + Value size = + BuildIntArrayConstOp(builder, rewriter, llvm::SmallVector({1}), + rewriter.getI32Type()); + Value begin = BuildIntArrayConstOp( + builder, rewriter, + llvm::SmallVector({static_cast(op.getDimension())}), + rewriter.getI64Type()); + Value slice_op = TF::SliceOp::create( + rewriter, op.getLoc(), + RankedTensorType::get({static_cast(1)}, + op.getType().getElementType()), + shape_op, begin, size); + Value squeeze_op = TF::SqueezeOp::create( + rewriter, op.getLoc(), op.getType(), slice_op, + rewriter.getI64ArrayAttr(llvm::ArrayRef({0}))); + rewriter.replaceOp(op, {squeeze_op}); + return success(); + } +}; + +class ConvertRealDynamicSliceOp + : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::RealDynamicSliceOp real_dynamic_slice_op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final { + auto start_indices_type = mlir::cast( + real_dynamic_slice_op.getStartIndices().getType()); + auto end_indices_type = mlir::cast( + real_dynamic_slice_op.getLimitIndices().getType()); + + if (start_indices_type.getNumDynamicDims() != 0 || + end_indices_type.getNumDynamicDims() != 0) { + return rewriter.notifyMatchFailure( + real_dynamic_slice_op, + "Start indices and limit indices must not have dynamic dimensions."); + } + rewriter.replaceOpWithNewOp( + real_dynamic_slice_op, real_dynamic_slice_op.getType(), + real_dynamic_slice_op.getOperand(), + real_dynamic_slice_op.getStartIndices(), + real_dynamic_slice_op.getLimitIndices(), + real_dynamic_slice_op.getStrides()); + return success(); + }; +}; + +class ConvertDynamicIotaOp : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::DynamicIotaOp dynamic_iota_op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final { + RankedTensorType type = + mlir::dyn_cast_or_null(dynamic_iota_op.getType()); + if (!type || type.getElementType().isUnsignedInteger(64)) { + return rewriter.notifyMatchFailure(dynamic_iota_op, + "TF::RangeOp doesn't support UI64"); + } + // Only support 1D for now. + if (type.getRank() > 1 || dynamic_iota_op.getIotaDimension() != 0) { + return rewriter.notifyMatchFailure( + dynamic_iota_op, [&](::mlir::Diagnostic& diag) { + diag << "Only 1D DynamicIotaOp with iota dimension 0 is supported"; + }); + } + + const uint64_t dimension = dynamic_iota_op.getIotaDimension(); + Type element_type = type.getElementType(); + Attribute start, delta; + if (mlir::isa(element_type)) { + start = rewriter.getFloatAttr(element_type, 0.0); + delta = rewriter.getFloatAttr(element_type, 1.0); + } else if (mlir::isa(element_type)) { + start = rewriter.getIntegerAttr(element_type, 0); + delta = rewriter.getIntegerAttr(element_type, 1); + } else { + return failure(); + } + auto output_shape = dynamic_iota_op.getOperand(); + if (mlir::isa(element_type)) { + auto cast_type = + mlir::cast(output_shape.getType()).clone(element_type); + output_shape = TF::CastOp::create(rewriter, dynamic_iota_op.getLoc(), + cast_type, output_shape); + } + DenseIntElementsAttr scalar_attr = DenseIntElementsAttr::get( + RankedTensorType::get({0}, rewriter.getI32Type()), + llvm::ArrayRef({})); + auto scalar_shape = + TF::ConstOp::create(rewriter, dynamic_iota_op.getLoc(), scalar_attr); + auto limit_scalar = TF::ReshapeOp::create( + rewriter, dynamic_iota_op.getLoc(), + RankedTensorType::get({}, element_type), output_shape, scalar_shape); + auto range_type = + RankedTensorType::get({type.getShape()[dimension]}, element_type); + Value start_op = + TF::ConstOp::create(rewriter, dynamic_iota_op.getLoc(), start); + Value delta_op = + TF::ConstOp::create(rewriter, dynamic_iota_op.getLoc(), delta); + Value range_op = + TF::RangeOp::create(rewriter, dynamic_iota_op.getLoc(), range_type, + start_op, limit_scalar, delta_op); + rewriter.replaceOp(dynamic_iota_op, range_op); + return success(); + } +}; +// Returns true if broadcast_dimensions obey Tensorflow convention, as in new +// dimensions are added as prefix. +bool IsTFStyleBroadcast(DenseIntElementsAttr broadcast_dimensions, + Value output) { + // broadcast_dimensions is an increasing list by definition, thus it suffices + // to check the first element. + int64_t input_rank = broadcast_dimensions.getNumElements(); + int64_t output_rank = mlir::cast(output.getType()).getRank(); + return input_rank == 0 || + (broadcast_dimensions.getValues()[0].getSExtValue() == + output_rank - input_rank); +} + +// Returns true if the operation producing the provided result (`op_result`) +// is within an op region of an operation of type `ParentType`. +template +bool IsWithinOpRegion(mlir::OpResult op_result) { + mlir::Operation* parent_op = op_result.getDefiningOp()->getParentOp(); + + if (llvm::dyn_cast(parent_op)) { + return true; + } + return false; +} + +// Returns the intermediate shape that input tensor should be reshaped to during +// legalization of BroadcastInDimOp. +arith::ConstantOp ExpandedShape(PatternRewriter& rewriter, Value input, + DenseIntElementsAttr broadcast_dimensions, + Value output) { + // Initialize expanded shape with output rank and dimensions of 1. + SmallVector expanded_shape( + mlir::cast(output.getType()).getRank(), + /*Value=*/rewriter.getI64IntegerAttr(1)); + + // Set dimension sizes specified by broadcast_dimensions. + ArrayRef input_shape = + mlir::cast(input.getType()).getShape(); + for (auto x : llvm::enumerate(broadcast_dimensions)) { + expanded_shape[x.value().getSExtValue()] = + rewriter.getI64IntegerAttr(input_shape[x.index()]); + } + + // Create the expanded type wrapped in a arith::ConstantOp. + auto attr_type = + RankedTensorType::get({static_cast(expanded_shape.size())}, + rewriter.getIntegerType(64)); + auto attr = DenseElementsAttr::get(attr_type, expanded_shape); + return arith::ConstantOp::create(rewriter, output.getLoc(), attr_type, attr); +} + +Value ExpandedDynamicShape(PatternRewriter& rewriter, Value input, + DenseIntElementsAttr broadcast_dimensions, + Value output) { + assert(mlir::cast(output.getType()) && + "output type must be of ShapedType"); + int64_t output_rank = mlir::cast(output.getType()).getRank(); + llvm::SmallVector expanded_dimensions; + llvm::SmallSet broadcast_dimensions_values; + for (auto x : llvm::enumerate(broadcast_dimensions)) { + broadcast_dimensions_values.insert(x.value().getSExtValue()); + } + for (int64_t i = 0; i < output_rank; i++) { + if (!broadcast_dimensions_values.contains(i)) { + expanded_dimensions.push_back(i); + } + } + Value expanded_input = input; + for (int64_t i : expanded_dimensions) { + auto index_attr = DenseIntElementsAttr::get( + RankedTensorType::get({}, rewriter.getI64Type()), {i}); + Value index = TF::ConstOp::create(rewriter, output.getLoc(), index_attr); + expanded_input = TF::ExpandDimsOp::create(rewriter, output.getLoc(), + expanded_input, index); + } + return expanded_input; +} + +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/generated_legalize_hlo.inc" + +/// Performs the lowering to TF dialect. +void LegalizeHloToTf::runOnOperation() { + MLIRContext& context = getContext(); + mlir::ModuleOp module = getOperation(); + + RewritePatternSet patterns(&getContext()); + // Conversion patterns for custom calls. + patterns.add(&context); + patterns.add(&context, &module); + PopulateLegalizeHloToTfPatterns(&patterns, &context); + + ConversionTarget target(context); + target.addLegalDialect(); + target.addLegalOp(); + target.addLegalOp(); + if (failed(applyPartialConversion(getOperation(), target, + std::move(patterns)))) { + getOperation().emitError("mhlo to TF legalization failed."); + signalPassFailure(); + } +} + +} // end namespace + +void PopulateLegalizeHloToTfPatterns(RewritePatternSet* patterns, + MLIRContext* context) { + // Add mhlo::GatherOp canonicalization patterns first before the complicated + // ConvertGatherOp legalization pattern. + mhlo::GatherOp::getCanonicalizationPatterns(*patterns, context); + + patterns + ->add(context); + + PopulateReduceArgMinMaxTFPatterns(context, *patterns); + + populateWithGenerated(*patterns); +} + +std::unique_ptr> CreateLegalizeHloToTfPass() { + return std::make_unique(); +} + +static PassRegistration pass; + +} // end namespace odml +} // end namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..180207ec40a77137da8a14731d683a668e156095 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/BUILD @@ -0,0 +1,368 @@ +# Groups the implementations of the HLO to TF operation conversions. +load("//tensorflow/core/platform:rules_cc.bzl", "cc_library") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + default_visibility = [ + "//visibility:public", + ], + licenses = ["notice"], +) + +cc_library( + name = "util", + srcs = [ + "util.cc", + ], + hdrs = [ + "util.h", + ], + deps = [ + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/tensorflow", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@xla//xla/mlir_hlo", + ], +) + +cc_library( + name = "scatter", + srcs = [ + "scatter.cc", + ], + hdrs = [ + "scatter.h", + ], + deps = [ + ":util", + "//tensorflow/compiler/mlir/tensorflow", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@xla//xla/mlir_hlo", + ], +) + +cc_library( + name = "reduce", + srcs = [ + "reduce.cc", + ], + hdrs = [ + "reduce.h", + ], + deps = [ + ":util", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/lite/stablehlo:hlo_matchers", + "//tensorflow/compiler/mlir/tensorflow", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@xla//xla/mlir_hlo", + ], +) + +cc_library( + name = "dot_general", + srcs = [ + "dot_general.cc", + ], + hdrs = ["dot_general.h"], + deps = [ + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@xla//xla/mlir_hlo", + ], +) + +cc_library( + name = "custom_call", + srcs = [ + "custom_call.cc", + ], + hdrs = [ + "custom_call.h", + ], + deps = [ + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "@llvm-project//mlir:AsmParser", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@xla//xla/mlir_hlo", + ], +) + +cc_library( + name = "gelu", + srcs = ["gelu.cc"], + hdrs = ["gelu.h"], + deps = [ + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:FuncDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@stablehlo//:chlo_ops", + "@stablehlo//:stablehlo_ops", + ], +) + +cc_library( + name = "conv", + srcs = ["conv.cc"], + hdrs = ["conv.h"], + deps = [ + ":conv_util", + ":op_util_common", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@xla//xla/mlir_hlo", + ], +) + +cc_library( + name = "conv_util", + srcs = ["conv_util.cc"], + hdrs = ["conv_util.h"], + deps = [ + ":op_util_common", + "//tensorflow/core/lib/math:math_util", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@xla//xla/mlir_hlo", + ], +) + +cc_library( + name = "pad", + srcs = ["pad.cc"], + hdrs = ["pad.h"], + deps = [ + ":op_util_common", + ":pad_util", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@xla//xla/mlir_hlo", + ], +) + +cc_library( + name = "pad_util", + srcs = ["pad_util.cc"], + hdrs = ["pad_util.h"], + deps = [ + ":op_util_common", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@xla//xla/mlir_hlo", + ], +) + +cc_library( + name = "gather", + srcs = ["gather.cc"], + hdrs = ["gather.h"], + deps = [ + ":util", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@xla//xla/mlir_hlo", + ], +) + +cc_library( + name = "reduce_window", + srcs = ["reduce_window.cc"], + hdrs = ["reduce_window.h"], + deps = [ + ":op_util_common", + ":reduce_window_util", + ":util", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@xla//xla/mlir_hlo", + ], +) + +cc_library( + name = "op_util_common", + srcs = ["op_util_common.cc"], + hdrs = ["op_util_common.h"], + deps = [ + "@llvm-project//llvm:Support", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + ], +) + +cc_library( + name = "reduce_window_util", + srcs = ["reduce_window_util.cc"], + hdrs = ["reduce_window_util.h"], + deps = [ + ":op_util_common", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@xla//xla/mlir_hlo", + ], +) + +cc_library( + name = "slice", + srcs = ["slice.cc"], + hdrs = ["slice.h"], + deps = [ + ":op_util_common", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@xla//xla/mlir_hlo", + ], +) + +cc_library( + name = "sort", + srcs = ["sort.cc"], + hdrs = ["sort.h"], + deps = [ + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/lite/stablehlo:hlo_matchers", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@xla//xla/mlir_hlo", + ], +) + +cc_library( + name = "iota", + srcs = ["iota.cc"], + hdrs = ["iota.h"], + deps = [ + ":op_util_common", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@xla//xla/mlir_hlo", + ], +) + +cc_library( + name = "while", + srcs = ["while.cc"], + hdrs = ["while.h"], + deps = [ + ":op_util_common", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/tensorflow", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@xla//xla/mlir_hlo", + ], +) + +cc_library( + name = "get_dimension_size", + srcs = ["get_dimension_size.cc"], + hdrs = ["get_dimension_size.h"], + deps = [ + ":util", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@xla//xla/mlir_hlo", + ], +) + +cc_library( + name = "case", + srcs = ["case.cc"], + hdrs = ["case.h"], + deps = [ + ":util", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@xla//xla/mlir_hlo", + ], +) + +cc_library( + name = "if", + srcs = ["if.cc"], + hdrs = ["if.h"], + deps = [ + ":util", + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@xla//xla/mlir_hlo", + ], +) + +cc_library( + name = "fft", + srcs = ["fft.cc"], + hdrs = ["fft.h"], + deps = [ + "//tensorflow/compiler/mlir/lite:tensorflow_lite", + "//tensorflow/compiler/mlir/tensorflow:dynamic_shape_utils", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:ArithDialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:TransformUtils", + "@xla//xla/mlir_hlo", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/case.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/case.cc new file mode 100644 index 0000000000000000000000000000000000000000..49b5c55c9b87aceeb0f2b083508854f41e805c29 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/case.cc @@ -0,0 +1,100 @@ +/* Copyright 2025 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/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/case.h" + +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/util.h" +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir::odml { +namespace { + +// Legalizes mhlo.case op to tfl.if op. +// This pattern only supports mhlo.case ops with exactly two branches. +class LegalizeCaseOp : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::CaseOp case_op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final { + // mhlo.case can have N branches, but tfl.if only supports two. + if (case_op.getBranches().size() != 2) { + return rewriter.notifyMatchFailure( + case_op, "can only convert mhlo.case with 2 branches"); + } + + // `mhlo.case` takes an index, `tfl.if` takes a boolean predicate. + // For a 2-branch `mhlo.case` (branch 0 and branch 1), we need to map + // the index to a boolean. + // According to the mhlo.case spec, an out-of-bounds index defaults to the + // index of the last branch, which is 1 in this case. + // So, index 0 maps to branch 0, and any other index (1, or out of bounds) + // maps to branch 1. + // This can be expressed as a predicate `index != 0` for branch 1. + + auto loc = case_op->getLoc(); + auto index = case_op.getIndex(); + auto index_type = mlir::cast(index.getType()); + + // Create a constant tensor of the same shape as the index, filled with + // zeros. + auto const_zero = arith::ConstantOp::create( + rewriter, loc, rewriter.getZeroAttr(index_type)); + + // Create the predicate `index != 0`. + auto pred_type = index_type.clone(rewriter.getI1Type()); + auto pred = mhlo::CompareOp::create( + rewriter, loc, pred_type, index, const_zero, + mhlo::ComparisonDirectionAttr::get(rewriter.getContext(), + mhlo::ComparisonDirection::NE), + mhlo::ComparisonTypeAttr{}); // Default comparison type is fine for + // integers. + + // Create the tfl.if op. + auto tfl_if = + TFL::IfOp::create(rewriter, loc, case_op.getResultTypes(), pred); + + // Branch 1 of mhlo.case becomes the `then_region` of tfl.if. + tfl_if.getThenRegion().takeBody(case_op.getBranches()[1]); + ReplaceTerminatorWithYield(tfl_if.getThenRegion(), rewriter); + + // Branch 0 of mhlo.case becomes the `else_region` of tfl.if. + tfl_if.getElseRegion().takeBody(case_op.getBranches()[0]); + ReplaceTerminatorWithYield(tfl_if.getElseRegion(), rewriter); + + rewriter.replaceOp(case_op, tfl_if.getResults()); + return success(); + } +}; + +} // namespace + +void PopulateCasePatterns(MLIRContext* context, RewritePatternSet& patterns, + ConversionTarget& target) { + patterns.add(context); + // Mark mhlo.case as dynamically legal: it's legal if it does NOT have + // exactly 2 branches, as those are the ones we want to convert. + target.addDynamicallyLegalOp( + [](mhlo::CaseOp op) { return op.getBranches().size() != 2; }); +} + +} // namespace mlir::odml diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/case.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/case.h new file mode 100644 index 0000000000000000000000000000000000000000..72ae1e21195cc1725209b262109c73d03d15abf6 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/case.h @@ -0,0 +1,31 @@ +/* Copyright 2025 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_CASE_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_CASE_H_ + +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project + +namespace mlir { +namespace odml { + +void PopulateCasePatterns(MLIRContext* context, RewritePatternSet& patterns, + ConversionTarget& target); + +} // namespace odml +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_CASE_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/conv.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/conv.cc new file mode 100644 index 0000000000000000000000000000000000000000..f0fb29260e3f10c1d9d35b9d695c995c9ac9d5ba --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/conv.cc @@ -0,0 +1,771 @@ +/* Copyright 2024 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/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/conv.h" + +#include +#include +#include +#include + +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallVector.h" +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/Location.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" // IWYU pragma: keep +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/conv_util.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/op_util_common.h" +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir::odml { + +namespace { + +using ::llvm::ArrayRef; + +//===----------------------------------------------------------------------===// +// support/legality checking +//===----------------------------------------------------------------------===// + +bool IsShapeFullyStatic(ArrayRef shape) { + return llvm::all_of(shape, [](int64_t d) { return d >= 0; }); +} + +bool NonBatchDimsFullyStatic(ArrayRef shape) { + return IsShapeFullyStatic(shape.drop_front()); +} + +bool AreShapesFullyStatic(const ConvView& data) { + return IsShapeFullyStatic(data.InputShape()) && + IsShapeFullyStatic(data.KernelShape()) && + IsShapeFullyStatic(data.OutputShape()); +} + +bool InputOutputNonBatchDimsFullyStatic(const ConvView& data) { + return NonBatchDimsFullyStatic(data.InputShape()) && + IsShapeFullyStatic(data.KernelShape()) && + NonBatchDimsFullyStatic(data.OutputShape()); +} + +bool IsPaddingSupported(const ConvView& data) { + return llvm::all_of(data.Padding(), [](const DimPadding& p) { + return p.Hi() == 0 && p.Lo() == 0; + }); +} + +bool IsInputDilationSupported(const ConvView& data) { + return llvm::all_of(data.InputDilations(), [](int64_t v) { return v == 1; }); +} + +bool IsBatchGroupSupported(const ConvView& data) { + return data.BatchGroupCount() == 1; +} + +bool IsWindowReversalSupported(const ConvView& data) { + return llvm::all_of(data.WindowReversal(), [](bool b) { return !b; }); +} + +// Determines if it is OK to leave given mhlo.convolution in the mhlo dialect. +// Used externally to setup a ConversionTarget with dynamically legal +// mhlo.convolution. Doubles as matching predicate during legalization. +bool IsConvLegal(mhlo::ConvolutionOp op) { + const ConvView data(op); + + const bool supported_conv_type = IsStandardConv(data) || + IsDepthwiseConv(data) || + IsSupportedNonTrivialConv(data); + + const bool is_non_supported_trivial_conv = + (!IsSupportedNonTrivialConv(data) && + (!IsPaddingSupported(data) || !IsInputDilationSupported(data))); + + return !supported_conv_type || !IsBatchGroupSupported(data) || + !IsTFLNativeLayout(data) || is_non_supported_trivial_conv || + !IsWindowReversalSupported(data); +} + +//===----------------------------------------------------------------------===// +// mhlo.convolution -> tfl +//===----------------------------------------------------------------------===// + +// Bias is a zero tensor of shape [output_channels]. +arith::ConstantOp BuildEmptyBias(OpBuilder& b, Location loc, + const ConvView& data) { + auto bias_type = RankedTensorType::get( + {data.OutputLayout().SpecialDim2(data.OutputShape())}, + data.ElementType()); + auto bias_const_data = b.getZeroAttr(bias_type); + return arith::ConstantOp::create(b, loc, bias_const_data); +} + +class LegalizeConv2D : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + LogicalResult matchAndRewrite( + mhlo::ConvolutionOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final; +}; + +LogicalResult LegalizeConv2D::matchAndRewrite( + mhlo::ConvolutionOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const { + // Parse mhlo.convolution attrs into cc types. + const ConvView data(op); + + if (IsConvLegal(op) || !IsStandardConv(data) || + data.InputLayout().Rank() != 4) { + return failure(); + } + + // + // dilations + //===------- + + const auto& kernel_dilations = data.KernelDilations(); + auto tfl_h_dilation = rewriter.getI32IntegerAttr(kernel_dilations[0]); + auto tfl_w_dilation = rewriter.getI32IntegerAttr(kernel_dilations[1]); + + // + // strides + //===----- + + const auto& window_strides = data.Strides(); + auto tfl_h_stride = rewriter.getI32IntegerAttr(window_strides[0]); + auto tfl_w_stride = rewriter.getI32IntegerAttr(window_strides[1]); + + // + // padding + //===----- + + // Explicit and same padding should be handeled in upstream "prepare" phase. + // Same padding will be fused in downstream "optimize" phase on tfl dialect. + auto tfl_padding = rewriter.getStringAttr("VALID"); + + // + // build tfl + //===------- + + auto bias = BuildEmptyBias(rewriter, op->getLoc(), data); + + auto tfl_faf_none = rewriter.getStringAttr("NONE"); + + rewriter.replaceOpWithNewOp( + op, op.getResult().getType(), op.getLhs(), op.getRhs(), bias, + tfl_h_dilation, tfl_w_dilation, tfl_faf_none, tfl_padding, tfl_h_stride, + tfl_w_stride); + + return success(); +} + +class LegalizeConvDepthwise : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + LogicalResult matchAndRewrite( + mhlo::ConvolutionOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final; +}; + +LogicalResult LegalizeConvDepthwise::matchAndRewrite( + mhlo::ConvolutionOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const { + // Parse mhlo.convolution attrs into cc types. + const ConvView data(op); + + if (IsConvLegal(op) || !IsDepthwiseConv(data)) { + return failure(); + } + + // + // dilations + //===------- + + const auto& kernel_dilations = data.KernelDilations(); + auto tfl_h_dilation = rewriter.getI32IntegerAttr(kernel_dilations[0]); + auto tfl_w_dilation = rewriter.getI32IntegerAttr(kernel_dilations[1]); + + // + // strides + //===----- + + const auto& window_strides = data.Strides(); + auto tfl_h_stride = rewriter.getI32IntegerAttr(window_strides[0]); + auto tfl_w_stride = rewriter.getI32IntegerAttr(window_strides[1]); + + // + // padding + //===----- + + // Explicit and same padding should be handeled in upstream "prepare" phase. + // Same padding will be fused in downstream "optimize" phase on tfl dialect. + auto tfl_padding = rewriter.getStringAttr("VALID"); + + // + // depth multiplier + //===----- + + const int64_t out_channels = + data.OutputLayout().SpecialDim2(data.OutputShape()); + const int64_t in_channels = data.InputLayout().SpecialDim2(data.InputShape()); + const int32_t depth_multiplier = out_channels / in_channels; + auto depth_multipler_attr = rewriter.getI32IntegerAttr(depth_multiplier); + + // + // build tfl + //===------- + + auto bias = BuildEmptyBias(rewriter, op->getLoc(), data); + + auto tfl_faf_none = rewriter.getStringAttr("NONE"); + + rewriter.replaceOpWithNewOp( + op, op.getResult().getType(), op.getLhs(), op.getRhs(), bias, + tfl_h_dilation, tfl_w_dilation, tfl_faf_none, tfl_padding, tfl_h_stride, + tfl_w_stride, depth_multipler_attr); + + return success(); +} + +class LegalizeConv3D : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + LogicalResult matchAndRewrite( + mhlo::ConvolutionOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final; +}; + +LogicalResult LegalizeConv3D::matchAndRewrite( + mhlo::ConvolutionOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const { + // Parse mhlo.convolution attrs into cc types. + const ConvView data(op); + + if (IsConvLegal(op) || !IsStandardConv(data) || + data.InputLayout().Rank() != 5) { + return failure(); + } + + // + // dilations + //===------- + + const auto& kernel_dilations = data.KernelDilations(); + auto tfl_d_dilation = rewriter.getI32IntegerAttr(kernel_dilations[0]); + auto tfl_h_dilation = rewriter.getI32IntegerAttr(kernel_dilations[1]); + auto tfl_w_dilation = rewriter.getI32IntegerAttr(kernel_dilations[2]); + + // + // strides + //===----- + + const auto& window_strides = data.Strides(); + auto tfl_d_stride = rewriter.getI32IntegerAttr(window_strides[0]); + auto tfl_h_stride = rewriter.getI32IntegerAttr(window_strides[1]); + auto tfl_w_stride = rewriter.getI32IntegerAttr(window_strides[2]); + + // + // padding + //===----- + + // Explicit and same padding should be handeled in upstream "prepare" phase. + // Same padding will be fused in downstream "optimize" phase on tfl dialect. + auto tfl_padding = rewriter.getStringAttr("VALID"); + + // + // build tfl + //===------- + + auto bias = BuildEmptyBias(rewriter, op->getLoc(), data); + + auto tfl_faf_none = rewriter.getStringAttr("NONE"); + + rewriter.replaceOpWithNewOp( + op, op.getResult().getType(), op.getLhs(), op.getRhs(), bias, + tfl_d_dilation, tfl_h_dilation, tfl_w_dilation, tfl_faf_none, tfl_padding, + tfl_d_stride, tfl_h_stride, tfl_w_stride); + + return success(); +} + +//===----------------------------------------------------------------------===// +// mhlo.convolution -> TFL::ResizeBilinearOp +//===----------------------------------------------------------------------===// + +// Convert a 2d mhlo.convolution op to a tfl.resize_bilinear +class ConvertNonTrivialConvToResizeBilinearOp + : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + LogicalResult matchAndRewrite( + mhlo::ConvolutionOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final; +}; + +LogicalResult ConvertNonTrivialConvToResizeBilinearOp::matchAndRewrite( + mhlo::ConvolutionOp conv_op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const { + const ConvView data(conv_op); + bool align_corners; + if (!MatchWithResizeBilinearOp(data, align_corners)) { + return rewriter.notifyMatchFailure( + conv_op, "op does not match with resize_bilinear op"); + } + + // The output size attribute is an array of 32bit values. + SmallVector output_shape_i32; + for (int64_t spatial_dim : data.InputLayout().Spatials()) { + output_shape_i32.push_back( + static_cast(data.OutputShape()[spatial_dim])); + } + Value output_sizes_attr = mlir::arith::ConstantOp::create( + rewriter, conv_op.getLoc(), rewriter.getI32TensorAttr(output_shape_i32)); + // The value of half_pixel_centers couldn't be inferred from the IR and XLA + // only support half_pixel_centers=True as in 01/11/2022. Here + // half_pixel_centers=False is hardcoded. + rewriter.replaceOpWithNewOp( + conv_op, conv_op.getType(), conv_op.getLhs(), output_sizes_attr, + /*align_corners=*/rewriter.getBoolAttr(align_corners), + /*half_pixel_centers=*/rewriter.getBoolAttr(false)); + + return success(); +} + +//===----------------------------------------------------------------------===// +// mhlo.convolution -> TFL::TransposeConv2dOp +//===----------------------------------------------------------------------===// + +// Convert a 2d mhlo.convolution op to a tfl.transpose_conv2d +class ConvertNonTrivialConvToTransposeConvOp + : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + LogicalResult matchAndRewrite( + mhlo::ConvolutionOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final; +}; + +LogicalResult ConvertNonTrivialConvToTransposeConvOp::matchAndRewrite( + mhlo::ConvolutionOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const { + const ConvView data(op); + + // + // Test if the op is a supported non-trivial convolution. + //===----- + + if (!IsSupportedNonTrivialConv(data)) { + return rewriter.notifyMatchFailure(op, "Not a non-trivial convolution."); + } + + // For depthwise and group convolutions, feature_group_count != 1 + if (op.getFeatureGroupCount() != 1) { + // Depthwise or Group convolution is not supported yet. + return rewriter.notifyMatchFailure( + op, "group or depthwise convolution is not supported"); + } + + // + // strides + //===----- + + // TFL::TravsposeConv2D applies strides on LHS. strides == lhs_dilation + auto strides = data.InputDilations(); + auto tfl_h_stride = rewriter.getI32IntegerAttr(strides[0]); + auto tfl_w_stride = rewriter.getI32IntegerAttr(strides[1]); + + // + // padding + //===----- + + std::string padding; + SmallVector padding_array; + for (auto& padding : data.Padding()) { + padding_array.push_back(padding.Lo()); + padding_array.push_back(padding.Hi()); + } + + if (IsTransposeConvPaddingValid(op, /*num_spatial_dims*/ 2, strides, + padding_array)) { + padding = "VALID"; + } else if (IsTransposeConvPaddingSame(op, /*num_spatial_dims*/ 2, strides, + padding_array)) { + padding = "SAME"; + } else { + return rewriter.notifyMatchFailure(op, + "requires padding to be SAME or VALID"); + } + + // + // build tfl op + //===------- + + auto bias = BuildEmptyBias(rewriter, op->getLoc(), data); + auto tfl_faf_none = rewriter.getStringAttr("NONE"); + + // Need to reverse the kernel data inorder to run TFL::TransposeConv2d + // The axis along which to reverse. In this case, we want to mirror the + // kernel's spatial dimensions. + SmallVector kernel_spatial_dims_i32( + data.KernelLayout().Spatials().begin(), + data.KernelLayout().Spatials().end()); + Value axis = arith::ConstantOp::create( + rewriter, op.getLoc(), + rewriter.getI32TensorAttr(kernel_spatial_dims_i32)); + + // Create the tfl::ReverseV2Op + auto filter = TFL::ReverseV2Op::create( + rewriter, op.getLoc(), op.getRhs().getType(), op.getRhs(), axis); + + // Calculate the output size and shape for TFL::TransposeConv2dOp + SmallVector output_shape_i32(data.OutputShape().begin(), + data.OutputShape().end()); + + auto output_sizes = arith::ConstantOp::create( + rewriter, op.getLoc(), rewriter.getI32TensorAttr(output_shape_i32)); + + rewriter.replaceOpWithNewOp( + op, op.getResult().getType(), /*output_shape=*/output_sizes, + /*filter=*/filter, /*input=*/op.getLhs(), /*bias=*/bias, + /*padding=*/rewriter.getStringAttr(padding), + /*stride_h=*/tfl_h_stride, /*stride_w=*/tfl_w_stride, + /*fused_activation_function=*/tfl_faf_none); + + return success(); +} + +//===----------------------------------------------------------------------===// + +class SliceDepthwiseTransposedConvolution + : public OpRewritePattern { + public: + using OpRewritePattern::OpRewritePattern; + LogicalResult matchAndRewrite(mhlo::ConvolutionOp op, + PatternRewriter& rewriter) const final; +}; + +// Pattern rewriter to match a depthwise transposed convolution and rewrite it +// to depth-times slices of input and filter to perform the transposed +// convolution on individual slices of tensors and concatenate the results of. +// the convolutions. This is a. workaround because the TFLite runtime doesn't +// support depthwise-transposed-conv op natively. +LogicalResult SliceDepthwiseTransposedConvolution::matchAndRewrite( + mhlo::ConvolutionOp conv_op, PatternRewriter& rewriter) const { + const ConvView data(conv_op); + + // + // Test if the op is a supported non-trivial convolution. + //===----- + if (!IsSupportedNonTrivialConv(data)) { + return rewriter.notifyMatchFailure(conv_op, + "Not a non-trivial convolution."); + } + + // These checks narrow down the support to depthwise transpose conv2d. + mhlo::ConvDimensionNumbersAttr dnums = conv_op.getDimensionNumbers(); + const int64_t input_feature_dimension = dnums.getInputFeatureDimension(); + const int64_t input_channels = + mlir::cast(conv_op.getLhs().getType()) + .getDimSize(input_feature_dimension); + const int64_t feature_group_count = conv_op.getFeatureGroupCount(); + const int64_t kernel_input_feature_dimension = + dnums.getKernelInputFeatureDimension(); + const int64_t kernel_input_channels = + mlir::cast(conv_op.getRhs().getType()) + .getDimSize(kernel_input_feature_dimension); + const int64_t kernel_output_feature_dimension = + dnums.getKernelOutputFeatureDimension(); + const int64_t kernel_output_channels = + mlir::cast(conv_op.getRhs().getType()) + .getDimSize(kernel_output_feature_dimension); + + // To support a depthwise convolution, we need- + // 1. feature_group_count != 1 (except when input_channels==1) + // 2. feature_group_count == input_channels + // 3. kernel_input_channels == 1 + // 4. kernel_output_channels % kernel_input_channels == 0 + if (feature_group_count == 1) { + return rewriter.notifyMatchFailure(conv_op, "Not a depthwise convolution"); + } + + if (input_channels != feature_group_count) { + return rewriter.notifyMatchFailure( + conv_op, "Not a detphwise transposed convolution"); + } + + if (MatchWithResizeBilinearOp(data)) { + return rewriter.notifyMatchFailure( + conv_op, "Op will be legalized to ResizeBilinearOp"); + } + + if ((kernel_output_channels % feature_group_count != 0) || + (kernel_input_channels != 1)) { + return rewriter.notifyMatchFailure( + conv_op, "Not a supported detphwise transposed convolution"); + } + + // This needs to be checked because the TFLite runtime generated incorrect + // results for depthwise transpose convolutions with non-1 channel + // multiplier. + if ((kernel_output_channels / feature_group_count) != 1) { + return rewriter.notifyMatchFailure( + conv_op, + "Unsupported detphwise transpose convolution with non-1 channel " + "multiplier"); + } + + // Slicing with dynamic offsets (helper method advised) + auto create_slice = [&](mlir::Value tensor, int64_t depth_idx, + int64_t channel_idx, + bool is_kernel = false) -> mlir::Value { + auto tensor_shape = + mlir::cast(tensor.getType()).getShape().vec(); + + // Calculate offsets based on depth_idx, channel_idx and tensor_shape + llvm::SmallVector start_indices(tensor_shape.size(), 0); + auto limit_indices = tensor_shape; + const llvm::SmallVector strides(tensor_shape.size(), 1); + start_indices[channel_idx] = depth_idx; + if (is_kernel) { + // kernel can have a channel_multiplier that needs to be accounted for + limit_indices[channel_idx] = + depth_idx + (kernel_output_channels / feature_group_count); + } else { + limit_indices[channel_idx] = depth_idx + 1; + } + return mhlo::SliceOp::create(rewriter, conv_op.getLoc(), tensor, + rewriter.getI64TensorAttr(start_indices), + rewriter.getI64TensorAttr(limit_indices), + rewriter.getI64TensorAttr(strides)); + }; + + // Storage for smaller convolution results + llvm::SmallVector conv_results; + + // Iterative Slicing and Convolutions + for (int i = 0; i < feature_group_count; ++i) { + auto sliced_input = + create_slice(conv_op.getLhs(), i, input_feature_dimension); + auto sliced_kernel = create_slice(conv_op.getRhs(), i, + kernel_output_feature_dimension, true); + + // Calculate convolution output_type based on sliced_input and + // sliced_kernel + auto output_type = mlir::cast(conv_op->getResult(0).getType()); + auto new_output_shape = output_type.getShape().vec(); + new_output_shape[dnums.getOutputFeatureDimension()] /= feature_group_count; + auto new_output_type = + RankedTensorType::get(new_output_shape, output_type.getElementType()); + + // Create a Smaller Convolution (Ensure compatibility) + auto conv_result = mhlo::ConvolutionOp::create( + rewriter, conv_op.getLoc(), new_output_type, sliced_input, + sliced_kernel, conv_op.getWindowStridesAttr(), conv_op.getPaddingAttr(), + conv_op.getLhsDilationAttr(), conv_op.getRhsDilationAttr(), + conv_op.getWindowReversalAttr(), conv_op.getDimensionNumbers(), + /*feature_group_count*/ 1, /*batch_group_count*/ 1, + conv_op.getPrecisionConfigAttr()); + + conv_results.push_back(conv_result); + } + + auto final_output = mhlo::ConcatenateOp::create( + rewriter, conv_op.getLoc(), conv_results, + rewriter.getI64IntegerAttr(dnums.getOutputFeatureDimension())); + rewriter.replaceOp(conv_op, final_output.getResult()); + return success(); +} + +//===----------------------------------------------------------------------===// + +// Convert a 1-D convolution into a 2-D convolution (which TFL supports) so that +// it can be rewritten by the pattern `Convert2DConvOp`. +class Conv1DToConv2D : public OpRewritePattern { + public: + using OpRewritePattern::OpRewritePattern; + LogicalResult matchAndRewrite(mhlo::ConvolutionOp op, + PatternRewriter& rewriter) const final; +}; + +arith::ConstantOp ShapeToConst(PatternRewriter& rewriter, + ArrayRef shape, Location loc) { + auto attr_type = RankedTensorType::get({static_cast(shape.size())}, + rewriter.getIntegerType(32)); + auto casted_shape = llvm::map_range(shape, [](auto i64) -> int32_t { + return (i64 < 0) ? -1 : static_cast(i64); + }); + auto attr = + DenseIntElementsAttr::get(attr_type, llvm::to_vector(casted_shape)); + return arith::ConstantOp::create(rewriter, loc, attr_type, attr); +} + +std::tuple, int64_t, Layout> InsertTrivialSpatialDim( + const Layout& layout, ArrayRef shape) { + // Make new Layout with extra spatial dimension. + const int64_t last_spatial = layout.Spatials()[layout.Rank() - 3]; + const int64_t new_dim1 = (layout.SpecialDim1() > last_spatial) + ? layout.SpecialDim1() + 1 + : layout.SpecialDim1(); + const int64_t new_dim2 = (layout.SpecialDim2() > last_spatial) + ? layout.SpecialDim2() + 1 + : layout.SpecialDim2(); + + llvm::SmallVector new_spatials(layout.Spatials()); + const int64_t new_last_spatial = new_spatials.back() + 1; + new_spatials.push_back(new_last_spatial); + + // Get new shape. + llvm::SmallVector new_shape(shape.size() + 1, 1); + new_shape[new_dim1] = layout.SpecialDim1(shape); + new_shape[new_dim2] = layout.SpecialDim2(shape); + for (auto new_spatial : new_spatials) { + if (new_spatial == new_last_spatial) { + continue; + } + new_shape[new_spatial] = shape[new_spatial]; + } + return std::tuple(new_shape, new_last_spatial, + Layout(new_dim1, new_dim2, new_spatials)); +} + +LogicalResult Conv1DToConv2D::matchAndRewrite(mhlo::ConvolutionOp op, + PatternRewriter& rewriter) const { + const ConvView view(op); + + if (view.InputLayout().Rank() != 3) { + return rewriter.notifyMatchFailure(op, "Not 1D conv."); + } + + if (!IsWindowReversalSupported(view)) { + return rewriter.notifyMatchFailure(op, "Expects window reversal trivial."); + } + + if (!view.InputLayout().AreSpatialsIota() || + !view.KernelLayout().AreSpatialsIota() || + !view.OutputLayout().AreSpatialsIota()) { + return rewriter.notifyMatchFailure(op, + "Expects well formed spatials dims."); + } + + // + // Transpose and reshape the input and kernel + //=----- + + // Add new trivial spatial dimension to input (LHS). + auto [lhs_new_shape, lhs_new_expnaded_dim, lhs_new_layout] = + InsertTrivialSpatialDim(view.InputLayout(), view.InputShape()); + auto lhs_new_type = op.getLhs().getType().clone(lhs_new_shape); + auto new_lhs = TFL::ExpandDimsOp::create( + rewriter, op.getLoc(), lhs_new_type, op.getLhs(), + ShapeToConst(rewriter, {lhs_new_expnaded_dim}, op.getLoc())); + + // Add new trivial spatial dimension to kernel. + auto [rhs_new_shape, rhs_new_expnaded_dim, rhs_new_layout] = + InsertTrivialSpatialDim(view.KernelLayout(), view.KernelShape()); + auto rhs_new_type = op.getRhs().getType().clone(rhs_new_shape); + auto new_rhs = TFL::ExpandDimsOp::create( + rewriter, op.getLoc(), rhs_new_type, op.getRhs(), + ShapeToConst(rewriter, {rhs_new_expnaded_dim}, op.getLoc())); + + // Add new trivial spatial dimension to output (insert reshape later). + auto [out_new_shape, out_new_expnaded_dim, out_new_layout] = + InsertTrivialSpatialDim(view.OutputLayout(), view.OutputShape()); + auto out_new_type = op.getResult().getType().clone(out_new_shape); + + // + // Create 2d equivalents for 1d convolution attributes. + //=----- + + // Window Strides + llvm::SmallVector strides_2d; + strides_2d.push_back(view.Strides()[0]); + strides_2d.push_back(1); + auto strides_2d_attr = DenseIntElementsAttr::get( + RankedTensorType::get({2}, rewriter.getI64Type()), strides_2d); + + // Padding + SmallVector padding_2d; + const auto& dim_pad = view.Padding()[0]; + padding_2d.push_back(dim_pad.Lo()); + padding_2d.push_back(dim_pad.Hi()); + padding_2d.push_back(0); + padding_2d.push_back(0); + auto padding_2d_attr = DenseIntElementsAttr::get( + RankedTensorType::get({2, 2}, rewriter.getI64Type()), padding_2d); + + // LHS dilation + SmallVector lhs_dilation_2d; + lhs_dilation_2d.push_back(view.InputDilations()[0]); + lhs_dilation_2d.push_back(1); + auto lhs_dilation_2d_attr = DenseIntElementsAttr::get( + RankedTensorType::get({2}, rewriter.getI64Type()), lhs_dilation_2d); + + // RHS dilation + SmallVector rhs_dilation_2d; + rhs_dilation_2d.push_back(view.KernelDilations()[0]); + rhs_dilation_2d.push_back(1); + auto rhs_dilation_2d_attr = DenseIntElementsAttr::get( + RankedTensorType::get({2}, rewriter.getI64Type()), rhs_dilation_2d); + + auto window_reversal_2d_attr = DenseIntElementsAttr::get( + RankedTensorType::get({2}, rewriter.getIntegerType(1)), + SmallVector({false, false})); + + // New dnums. + auto dnums_2d = mhlo::ConvDimensionNumbersAttr::get( + rewriter.getContext(), lhs_new_layout.SpecialDim1(), + lhs_new_layout.SpecialDim2(), lhs_new_layout.Spatials(), + rhs_new_layout.SpecialDim1(), rhs_new_layout.SpecialDim2(), + rhs_new_layout.Spatials(), out_new_layout.SpecialDim1(), + out_new_layout.SpecialDim2(), out_new_layout.Spatials()); + + // + // Build 2-D convolution with reshaped output. + //=----- + + auto conv2d_op = mhlo::ConvolutionOp::create( + rewriter, op.getLoc(), out_new_type, new_lhs, new_rhs, strides_2d_attr, + padding_2d_attr, lhs_dilation_2d_attr, rhs_dilation_2d_attr, + window_reversal_2d_attr, dnums_2d, op.getFeatureGroupCount(), + op.getBatchGroupCount(), op.getPrecisionConfigAttr()); + + auto new_out_type = op.getResult().getType(); + auto squeeze_dim = rewriter.getI64ArrayAttr({out_new_expnaded_dim}); + rewriter.replaceOpWithNewOp( + op, new_out_type, conv2d_op.getResult(), squeeze_dim); + + return success(); +} + +} // namespace + +void PopulateLegalizeConvPatterns(MLIRContext* ctx, RewritePatternSet& patterns, + ConversionTarget& target) { + patterns.add(ctx); + target.addDynamicallyLegalOp(IsConvLegal); +} + +void PopulatePrepareConvPatterns(MLIRContext* ctx, + RewritePatternSet& patterns) { + patterns.add(ctx); +} +} // namespace mlir::odml diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/conv.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/conv.h new file mode 100644 index 0000000000000000000000000000000000000000..75c4e10786914c448f8cfbbc3927212234bf84d4 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/conv.h @@ -0,0 +1,59 @@ +/* Copyright 2024 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_CONV_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_CONV_H_ + +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project + +namespace mlir::odml { + +// Prepares mhlo.convolutions and legalizes to the corresponding tfl op. +// +// Note: "tfl-native" layouts are as follows: +// 2D : [b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f] +// 3D : [b, 0, 1, 2, f]x[0, 1, 2, i, o]->[b, 0, 1, 2, f] +// 2D (depthwise) : [b, 0, 1, f]x[i, 0, 1, o]->[b, 0, 1, f] +// +// Matches: mhlo.convolution +// layout: any (will transpose to tfl-native) +// padding: any (will pull into explicit pad_op) +// lhs_dilations: trivial (all 1) +// rhs_dilations: any +// strides: any +// feature_group: see decision tree below +// batch_group: trivial (1) +// reversal: trivial (all False) +// shape: static, rank 4 or 5 +// +// This pattern emits TFL convs based on the following decision tree: +// if lhs_dilations are trivial && kernel_out_features == output_features +// if feature_group == 1: +// if rank == 5: tfl.conv_3D +// if rank == 4: tfl.conv_2D +// else if input_features == feature_group: +// if rank == 4: tfl.depthwise_conv TODO: b/352954597 - Add support. +// else: +// if rank == 4: tfl.conv_2D +// else: +// tfl.transpose_conv TODO: b/352954597 - Add support. +void PopulateLegalizeConvPatterns(MLIRContext* ctx, RewritePatternSet& patterns, + ConversionTarget& target); + +void PopulatePrepareConvPatterns(MLIRContext* ctx, RewritePatternSet& patterns); + +} // namespace mlir::odml + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_CONV_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/conv_util.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/conv_util.cc new file mode 100644 index 0000000000000000000000000000000000000000..9cf9b71363fa5012d02c58aaa7c043c583775c83 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/conv_util.cc @@ -0,0 +1,274 @@ +/* Copyright 2024 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/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/conv_util.h" + +#include +#include +#include +#include +#include + +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallVector.h" +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/op_util_common.h" +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir::odml { + +llvm::SmallVector ResolveWindowReversal( + const int64_t num_spatials, + std::optional opt_reversals) { + if (!opt_reversals.has_value()) { + return llvm::SmallVector(num_spatials, false); + } + auto reversals = opt_reversals.value(); + if (reversals.isSplat()) { + return llvm::SmallVector(num_spatials, + reversals.getSplatValue()); + } + return llvm::SmallVector(reversals.getValues()); +} + +ConvView::ConvView(mhlo::ConvolutionOp op) + : input_layout_( + Layout{op.getDimensionNumbers().getInputBatchDimension(), + op.getDimensionNumbers().getInputFeatureDimension(), + op.getDimensionNumbers().getInputSpatialDimensions()}), + kernel_layout_( + Layout{op.getDimensionNumbers().getKernelInputFeatureDimension(), + op.getDimensionNumbers().getKernelOutputFeatureDimension(), + op.getDimensionNumbers().getKernelSpatialDimensions()}), + output_layout_( + Layout{op.getDimensionNumbers().getOutputBatchDimension(), + op.getDimensionNumbers().getOutputFeatureDimension(), + op.getDimensionNumbers().getOutputSpatialDimensions()}), + input_shape_( + llvm::SmallVector(op.getLhs().getType().getShape())), + kernel_shape_( + llvm::SmallVector(op.getRhs().getType().getShape())), + output_shape_( + llvm::SmallVector(op.getResult().getType().getShape())), + batch_group_count_(op.getBatchGroupCount()), + feature_group_count_(op.getFeatureGroupCount()), + element_type_(op.getLhs().getType().getElementType()) { + const int64_t num_spatials = InputLayout().NumSpatials(); + + strides_ = ResolveStridesOrDilations(num_spatials, op.getWindowStrides()); + + input_dilations_ = + ResolveStridesOrDilations(num_spatials, op.getLhsDilation()); + kernel_dilations_ = + ResolveStridesOrDilations(num_spatials, op.getRhsDilation()); + + padding_ = ResolvePadding(num_spatials, op.getPadding()); + + window_reversal_ = + ResolveWindowReversal(num_spatials, op.getWindowReversal()); +} + +Value CreatePadOpFromConvPadding(OpBuilder& b, mhlo::ConvolutionOp op) { + const ConvView data(op); + const auto rank = data.InputLayout().Rank(); + auto input_spatials = data.InputLayout().Spatials(); + + llvm::SmallVector hi_padding(rank, 0); + llvm::SmallVector lo_padding(rank, 0); + + for (const auto& [ind, dim_padding] : llvm::enumerate(data.Padding())) { + const size_t cur_input_spatial = input_spatials[ind]; + hi_padding[cur_input_spatial] = dim_padding.Hi(); + lo_padding[cur_input_spatial] = dim_padding.Lo(); + } + + const llvm::SmallVector interior_padding(rank, 0); + + auto padding_attr_type = RankedTensorType::get({rank}, b.getI64Type()); + auto hi_padding_attr = + DenseIntElementsAttr::get(padding_attr_type, hi_padding); + auto lo_padding_attr = + DenseIntElementsAttr::get(padding_attr_type, lo_padding); + auto interior_padding_attr = + DenseIntElementsAttr::get(padding_attr_type, interior_padding); + + auto padding_value_type = RankedTensorType::get({}, data.ElementType()); + auto padding_value_attr = b.getZeroAttr(padding_value_type); + auto padding_value_op = + arith::ConstantOp::create(b, op->getLoc(), padding_value_attr); + + auto pad_op = mhlo::PadOp::create(b, padding_value_op->getLoc(), op.getLhs(), + padding_value_op, lo_padding_attr, + hi_padding_attr, interior_padding_attr); + + return pad_op; +} + +bool MatchWithResizeBilinearOp(const ConvView& data, bool& align_corners) { + if (data.InputLayout().Rank() != 4 || data.KernelLayout().Rank() != 4 || + data.OutputLayout().Rank() != 4 || + data.InputLayout().Spatials() != data.OutputLayout().Spatials()) { + return false; + } + + if (data.InputDilations().size() != 2 || + !(llvm::all_of(data.KernelDilations(), [](auto d) { return d == 1; })) || + data.Strides().size() != 2 || data.Padding().size() != 2) { + return false; + } + + // This is based on method in compiler/tf2xla/kernels/image_resize_ops.cc + auto can_convert_to_bilinear = + [](bool align_corners, int64_t dilation, int64_t padding, int64_t stride, + int64_t input_spatial, int64_t output_spatial) { + int64_t input_spatial_size = + align_corners ? input_spatial - 1 : input_spatial; + int64_t output_spatial_size = + align_corners ? output_spatial - 1 : output_spatial; + + int64_t gcd = std::gcd(static_cast(input_spatial_size), + static_cast(output_spatial_size)); + + if ((gcd == 0) || (input_spatial_size % gcd != 0) || + (input_spatial_size / gcd != stride) || (dilation - 1 != padding)) { + return false; + } + return true; + }; + + if (data.InputDilations()[0] != 1 && data.InputDilations()[1] == 1) { + if (can_convert_to_bilinear( + /*align_corners=*/true, data.InputDilations()[0], + data.Padding()[0].Lo(), data.Strides()[0], + data.InputShape()[data.InputLayout().Spatials()[0]], + data.OutputShape()[data.OutputLayout().Spatials()[0]])) { + align_corners = true; + return true; + } else if (can_convert_to_bilinear( + /*align_corners=*/false, data.InputDilations()[0], + data.Padding()[0].Lo(), data.Strides()[0], + data.InputShape()[data.InputLayout().Spatials()[0]], + data.OutputShape()[data.OutputLayout().Spatials()[0]])) { + align_corners = false; + return true; + }; + } else if (data.InputDilations()[0] == 1 && data.InputDilations()[1] != 1) { + if (can_convert_to_bilinear( + /*align_corners=*/true, data.InputDilations()[1], + data.Padding()[1].Lo(), data.Strides()[1], + data.InputShape()[data.InputLayout().Spatials()[1]], + data.OutputShape()[data.OutputLayout().Spatials()[1]])) { + align_corners = true; + return true; + } else if (can_convert_to_bilinear( + /*align_corners=*/false, data.InputDilations()[1], + data.Padding()[1].Lo(), data.Strides()[1], + data.InputShape()[data.InputLayout().Spatials()[1]], + data.OutputShape()[data.OutputLayout().Spatials()[1]])) { + align_corners = false; + return true; + }; + } + + return false; +} + +bool IsTransposeConvPaddingValid(mhlo::ConvolutionOp conv_op, + size_t num_spatial_dims, + const ArrayRef& strides, + const ArrayRef& padding) { + auto dnums = conv_op.getDimensionNumbers(); + // The newly added spatial dimension requires zero left and right padding. + ArrayRef input_spatial_dims = dnums.getInputSpatialDimensions(); + ArrayRef kernel_spatial_dims = dnums.getKernelSpatialDimensions(); + ArrayRef output_spatial_dims = dnums.getOutputSpatialDimensions(); + + for (size_t i = 0; i < num_spatial_dims; ++i) { + int64_t stride = strides[i]; + int64_t input_size = mlir::cast(conv_op.getLhs().getType()) + .getDimSize(input_spatial_dims[i]); + int64_t kernel_size = mlir::cast(conv_op.getRhs().getType()) + .getDimSize(kernel_spatial_dims[i]); + int64_t output_size = mlir::cast(conv_op.getType()) + .getDimSize(output_spatial_dims[i]); + + // stablehlo.convolution op needs explicit padding to be set to model any + // Transposed-Convolution in JAX/PT. Checking to see if- + // 1. Pre set padding matches to the desired padding + // 2. Output size respects the `VALID` padding scenario + if ((padding[2 * i] == padding[2 * i + 1]) && + (((kernel_size - 1) != padding[2 * i]) || + (output_size != (stride * (input_size - 1)) + kernel_size))) { + // padding[2 * i] == padding[2 * i + 1] means equal padding is applied + // on both sides of a spatial dimension. + // This happens when kernel_dim >= stride + return false; + } else if ((padding[2 * i] != padding[2 * i + 1]) && + (((kernel_size - 1) != padding[2 * i]) || + ((stride - 1) != padding[2 * i + 1]) || + (output_size != (stride * input_size)))) { + return false; + } + } + + return true; +} + +bool IsTransposeConvPaddingSame(mhlo::ConvolutionOp conv_op, + size_t num_spatial_dims, + const ArrayRef& strides, + const ArrayRef& padding) { + auto dnums = conv_op.getDimensionNumbers(); + + // The newly added spatial dimension requires zero left and right padding. + ArrayRef input_spatial_dims = dnums.getInputSpatialDimensions(); + ArrayRef output_spatial_dims = dnums.getOutputSpatialDimensions(); + for (size_t i = 0; i < num_spatial_dims; ++i) { + // In some cases the total padding is odd, so we have 1 leftover, which is + // why below we check pad_delta > 1. + int64_t pad_delta = std::abs(padding[2 * i] - padding[2 * i + 1]); + if (pad_delta > 1) { + return false; + } + int64_t stride = strides[i]; + + auto input_shape = mlir::cast(conv_op.getLhs().getType()); + int64_t input_dim_size = input_shape.getDimSize(input_spatial_dims[i]); + int64_t output_dim_size = mlir::cast(conv_op.getType()) + .getDimSize(output_spatial_dims[i]); + // The reason for the below check is as follows: + // When computing the output, we have the following relation between + // o - output dim size, i - input dim size, s - stride, P - total pads + // o = (i-k+1) + (s-1)(i-1) + P + // Where the first term is the kernel applications on the input, + // the second term is the additional applications from the stride + // and P is a term that captures the total padding. After expanding we get + // o = si + k - s + 2 + P + // Here JAX sets P to cancel k-s+2, leading to the expression below + + // Check only if the dimension is not dynamic. + if (!input_shape.isDynamicDim(i) && + (output_dim_size != input_dim_size * stride)) { + return false; + } + } + return true; +} + +} // namespace mlir::odml diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/conv_util.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/conv_util.h new file mode 100644 index 0000000000000000000000000000000000000000..354d50d4cd93e64ef4ef1cfe340dfd1a65159594 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/conv_util.h @@ -0,0 +1,298 @@ +/* Copyright 2024 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_CONV_UTIL_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_CONV_UTIL_H_ + +#include +#include +#include + +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/Sequence.h" +#include "llvm/ADT/SmallVector.h" +#include "mlir/IR/Types.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/op_util_common.h" +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +// Helpers for working with mhlo.convolution attrs in the mlir api as +// native cc types. + +namespace mlir::odml { + +class ConvView { + public: + // int for each spatial dim. Default 1. + llvm::ArrayRef Strides() const { return strides_; } + + // 2d array for each spatial dim. Default 0. + llvm::ArrayRef Padding() const { return padding_; } + + int64_t BatchGroupCount() const { return batch_group_count_; } + + int64_t FeatureGroupCount() const { return feature_group_count_; } + + // int for each spatial dim. Default 1. + llvm::ArrayRef InputDilations() const { return input_dilations_; } + + // int for each spatial dim. Default 1. + llvm::ArrayRef KernelDilations() const { return kernel_dilations_; } + + // bool for each spatial dim. Default false. + llvm::ArrayRef WindowReversal() const { return window_reversal_; } + + llvm::ArrayRef InputShape() const { return input_shape_; } + + const Layout& InputLayout() const { return input_layout_; } + + llvm::ArrayRef KernelShape() const { return kernel_shape_; } + + const Layout& KernelLayout() const { return kernel_layout_; } + + llvm::ArrayRef OutputShape() const { return output_shape_; } + + const Layout& OutputLayout() const { return output_layout_; } + + mlir::Type ElementType() const { return element_type_; } + + explicit ConvView(mhlo::ConvolutionOp op); + + private: + llvm::SmallVector strides_; + + llvm::SmallVector padding_; + + llvm::SmallVector input_dilations_; + llvm::SmallVector kernel_dilations_; + + llvm::SmallVector window_reversal_; + + Layout input_layout_; + Layout kernel_layout_; + Layout output_layout_; + + llvm::SmallVector input_shape_; + llvm::SmallVector kernel_shape_; + llvm::SmallVector output_shape_; + + int64_t batch_group_count_; + int64_t feature_group_count_; + + mlir::Type element_type_; +}; + +inline bool HasSupportedRank(const ConvView& data) { + return data.InputLayout().Rank() == 4 || data.InputLayout().Rank() == 5; +} + +inline bool HasSupportedOutFeatureDims(const ConvView& data) { + const int64_t kernel_out_features = + data.KernelLayout().SpecialDim2(data.KernelShape()); + const int64_t out_features = + data.OutputLayout().SpecialDim2(data.OutputShape()); + return kernel_out_features == out_features; +} + +inline bool IsTrivialConv(const ConvView& data) { + return llvm::all_of(data.InputDilations(), [](auto d) { return d == 1; }); +} + +// +// Supported non-trivial conv predicates +//=----- + +bool MatchWithResizeBilinearOp(const ConvView& data, bool& align_corners); + +inline bool MatchWithResizeBilinearOp(const ConvView& data) { + bool align_corners = false; + return MatchWithResizeBilinearOp(data, align_corners); +} + +bool IsTransposeConvPaddingValid(mhlo::ConvolutionOp conv_op, + size_t num_spatial_dims, + const ArrayRef& strides, + const ArrayRef& padding); + +bool IsTransposeConvPaddingSame(mhlo::ConvolutionOp conv_op, + size_t num_spatial_dims, + const ArrayRef& strides, + const ArrayRef& padding); + +inline bool IsSupportedNonTrivialConv(const ConvView& data) { + // Only non-trivial 2d convolutions are supported. + const bool valid_rank = data.InputLayout().Rank() == 4; + + // Negative padding is unsupported. + bool has_nagative_padding = llvm::all_of( + data.Padding(), + [](const DimPadding& p) { return p.Hi() < 0 || p.Lo() < 0; }); + + return (valid_rank && !IsTrivialConv(data) && !has_nagative_padding); +} + +inline bool IsSupportedNonTrivialConv(mhlo::ConvolutionOp op) { + const ConvView data(op); + return IsSupportedNonTrivialConv(data); +} + +// +// Standard conv predicates +//=----- + +inline bool HasStandardConvInFeatureDims(const ConvView& data) { + // kernel_in_features * feature_groups = input_features by definition. + const int64_t input_features = + data.InputLayout().SpecialDim2(data.InputShape()); + + const bool trivial_kernel_in_features = + data.FeatureGroupCount() == input_features; + const bool is_grouped_conv = data.FeatureGroupCount() != 1; + + const int64_t rank = data.InputLayout().Rank(); + return !trivial_kernel_in_features && (!is_grouped_conv || rank == 4); +} + +inline bool IsStandardConv(const ConvView& data) { + return HasSupportedRank(data) && IsTrivialConv(data) && + HasStandardConvInFeatureDims(data) && HasSupportedOutFeatureDims(data); +} + +// Does this convolution map to a standard conv_2d or conv_3d +// (not depthwise or tranpose conv)? +inline bool IsStandardConv(mhlo::ConvolutionOp op) { + const ConvView data(op); + return IsStandardConv(data); +} + +// +// Depthwise conv predicates +//=----- + +inline bool IsDepthwiseConv(const ConvView& data) { + const bool valid_rank = data.InputLayout().Rank() == 4; + if (!valid_rank || !HasSupportedOutFeatureDims(data) || + !IsTrivialConv(data)) { + return false; + } + const int64_t in_channel_dim = + data.InputLayout().SpecialDim2(data.InputShape()); + return data.FeatureGroupCount() == in_channel_dim; +} + +// Does this convolution map to depthwise conv? +inline bool IsDepthwiseConv(mhlo::ConvolutionOp op) { + const ConvView data(op); + return IsDepthwiseConv(data); +} + +// +// Tfl native layouts +//=----- + +inline int64_t DnumRank(mhlo::ConvDimensionNumbersAttr dnums) { + return dnums.getInputSpatialDimensions().size() + 2; +} + +inline Layout GetTFLNativeInputOrOutputLayout(int64_t rank) { + auto spatials = llvm::to_vector(llvm::seq(1, rank - 1)); + return Layout(0, rank - 1, spatials); +} + +inline Layout GetTFLNativeInputOrOutputLayout( + mhlo::ConvDimensionNumbersAttr dnums) { + return GetTFLNativeInputOrOutputLayout((DnumRank(dnums))); +} + +inline Layout GetTFLNativeStandardConvKernelLayout(int64_t rank) { + if (rank != 5) { + auto spatials = llvm::to_vector(llvm::seq(1, rank - 1)); + return Layout(rank - 1, 0, spatials); + } + auto spatials = llvm::to_vector(llvm::seq(rank - 2)); + return Layout(rank - 2, rank - 1, spatials); +} + +inline Layout GetTFLNativeDepthwiseConvKernelLayout() { + return Layout(0, 3, {1, 2}); +} + +inline Layout GetTFLNativeStandardConvKernelLayout( + mhlo::ConvDimensionNumbersAttr dnums) { + return GetTFLNativeStandardConvKernelLayout(DnumRank(dnums)); +} + +inline bool IsTFLNativeLayout(const ConvView& data) { + const int64_t rank = data.KernelLayout().Rank(); + const auto native_io_layout = GetTFLNativeInputOrOutputLayout(rank); + + std::optional native_kernel_layout = std::nullopt; + if (IsDepthwiseConv(data)) { + native_kernel_layout = GetTFLNativeDepthwiseConvKernelLayout(); + } else if (IsStandardConv(data) || IsSupportedNonTrivialConv(data)) { + native_kernel_layout = GetTFLNativeStandardConvKernelLayout(rank); + } + if (!native_kernel_layout.has_value()) { + return false; + } + + return data.InputLayout() == native_io_layout && + data.KernelLayout() == *native_kernel_layout && + data.OutputLayout() == native_io_layout; +} + +// +// ConvDimensionNumbers utils +//=----- + +inline mhlo::ConvDimensionNumbersAttr CloneDnumsWithInputLayout( + OpBuilder& b, mhlo::ConvDimensionNumbersAttr dnums, const Layout& layout) { + return mhlo::ConvDimensionNumbersAttr::get( + b.getContext(), layout.SpecialDim1(), layout.SpecialDim2(), + layout.Spatials(), dnums.getKernelInputFeatureDimension(), + dnums.getKernelOutputFeatureDimension(), + dnums.getKernelSpatialDimensions(), dnums.getOutputBatchDimension(), + dnums.getOutputFeatureDimension(), dnums.getOutputSpatialDimensions()); +} + +inline mhlo::ConvDimensionNumbersAttr CloneDnumsWithKernelLayout( + OpBuilder& b, mhlo::ConvDimensionNumbersAttr dnums, const Layout& layout) { + return mhlo::ConvDimensionNumbersAttr::get( + b.getContext(), dnums.getInputBatchDimension(), + dnums.getInputFeatureDimension(), dnums.getInputSpatialDimensions(), + layout.SpecialDim1(), layout.SpecialDim2(), layout.Spatials(), + dnums.getOutputBatchDimension(), dnums.getOutputFeatureDimension(), + dnums.getOutputSpatialDimensions()); +} + +inline mhlo::ConvDimensionNumbersAttr CloneDnumsWithOutputLayout( + OpBuilder& b, mhlo::ConvDimensionNumbersAttr dnums, const Layout& layout) { + return mhlo::ConvDimensionNumbersAttr::get( + b.getContext(), dnums.getInputBatchDimension(), + dnums.getInputFeatureDimension(), dnums.getInputSpatialDimensions(), + dnums.getKernelInputFeatureDimension(), + dnums.getKernelOutputFeatureDimension(), + dnums.getKernelSpatialDimensions(), layout.SpecialDim1(), + layout.SpecialDim2(), layout.Spatials()); +} + +// Wraps the lhs of given conv op in an explicit pad op matching the same +// behavior implicit in the paddings attribute. Gets result of new pad op. +Value CreatePadOpFromConvPadding(OpBuilder& b, mhlo::ConvolutionOp op); + +} // namespace mlir::odml + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_CONV_UTIL_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/custom_call.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/custom_call.cc new file mode 100644 index 0000000000000000000000000000000000000000..7eab0776a54b2e3b50fca6ca14e77b58b99e6630 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/custom_call.cc @@ -0,0 +1,166 @@ +/* Copyright 2023 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/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/custom_call.h" + +#include + +#include "mlir/AsmParser/AsmParser.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/OperationSupport.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" // IWYU pragma: keep +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir { +namespace odml { +namespace { + +class ConvertCustomCallOp : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::CustomCallOp mhlo_custom_call, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final; +}; + +// TFL op on StableHLO CustomCall carrier must serialize its attributes in +// the CustomCallOp's backend_config StringAttr, following MLIR +// DictionaryAttr serialization format. If no attributes are specified, +// the backend_config should be the serialized empty DictionaryAttr. +mlir::DictionaryAttr ParseSerializedTFLOpAttributes( + std::optional backend_config, MLIRContext* ctx) { + if (!backend_config) { + return nullptr; + } + + auto serialized_attributes = + mlir::dyn_cast_or_null(*backend_config); + if (!serialized_attributes) { + return nullptr; + } + + auto dict_attribute = mlir::dyn_cast_or_null( + parseAttribute(serialized_attributes.getValue(), ctx)); + return dict_attribute; +} + +LogicalResult ConvertCustomCallOp::matchAndRewrite( + mhlo::CustomCallOp mhlo_custom_call, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const { + auto call_target_name = mhlo_custom_call.getCallTargetName(); + if (call_target_name.starts_with("tfl.")) { + auto bc = mhlo_custom_call.getBackendConfig(); + if (mlir::DictionaryAttr attributes = + ParseSerializedTFLOpAttributes(bc, getContext())) { + // Short-cut: TFL direct lowering on StableHLO CustomCall carrier. + mlir::OperationState new_op(mhlo_custom_call.getLoc(), call_target_name, + mhlo_custom_call.getOperands(), + mhlo_custom_call.getResultTypes(), + attributes.getValue()); + rewriter.replaceOp(mhlo_custom_call, rewriter.create(new_op)); + return success(); + } + } + + if (!call_target_name.starts_with("custom_call.")) { + return failure(); + } + auto tfl_custom = TFL::CustomOp::create(rewriter, mhlo_custom_call.getLoc(), + mhlo_custom_call.getResultTypes(), + mhlo_custom_call.getInputs()); + tfl_custom.setCustomCodeAttr(rewriter.getStringAttr(call_target_name)); + + if (auto bc = mhlo_custom_call.getBackendConfig()) { + if (auto stringattr = mlir::dyn_cast_or_null(*bc)) { + tfl_custom.setCustomOptionAttr( + TFL::ConstBytesAttr::get(rewriter.getContext(), stringattr)); + } + } else { + tfl_custom.setCustomOptionAttr( + TFL::ConstBytesAttr::get(rewriter.getContext(), "")); + } + + rewriter.replaceOp(mhlo_custom_call, tfl_custom); + return success(); +} + +// Removes the `mhlo.custom_call @shape_assertion` custom call which represents +// an assertion that the first operand (`assert_what`) evaluates to `true`. +// This is a temporary workaround for unblocking dynamic model conversion +// because starting from version 7, in presence of shape polymorphism JAX will +// emit stablehlo.custom_call @shape_assertion to verify at compile time that +// the code is used with compatible actual shapes. +// TFLite runtime kernels support shape checking and shape inference to some +// extent, it is okay to remove the shape assertion in most scenarios. However +// this is not always the case, JAX may trace the program differently based on +// the shape polymorphism specification, for example, if the program contains +// a conditional on "x.shape[0] % 2 == 0" that conditional would evaluate to +// True with x specified as (2*b, ...) and False otherwise. We can revisit +// this when need arises. See b/295316438 for details. +class RemoveCustomCallWithShapeAssertion + : public OpRewritePattern { + public: + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(mhlo::CustomCallOp op, + PatternRewriter& rewriter) const final; +}; + +LogicalResult RemoveCustomCallWithShapeAssertion::matchAndRewrite( + mhlo::CustomCallOp op, PatternRewriter& rewriter) const { + if (op.getCallTargetName() != "shape_assertion") { + return mlir::failure(); + } + rewriter.eraseOp(op); + return success(); +} + +std::optional IsCustomCallLegal(mhlo::CustomCallOp op) { + auto call_target_name = op.getCallTargetName(); + if (call_target_name.starts_with("custom_call.")) { + auto bc = op.getBackendConfig(); + if (!bc || mlir::isa(*bc)) { + return false; + } + } + if (call_target_name.starts_with("tfl.")) { + auto bc = op.getBackendConfig(); + if (!bc || mlir::isa(*bc)) { + return false; + } + } + + return true; +} +} // namespace + +void PopulateCustomCallPatterns(MLIRContext* ctx, RewritePatternSet& patterns, + ConversionTarget& target) { + patterns.add(ctx); + target.addDynamicallyLegalOp(IsCustomCallLegal); +} + +void PopulateCustomCallPreparePatterns(MLIRContext* ctx, + RewritePatternSet& patterns) { + patterns.add(ctx); +} + +} // namespace odml +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/custom_call.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/custom_call.h new file mode 100644 index 0000000000000000000000000000000000000000..8e62967e4509518a41b09b5cb4776b4fbb7602ef --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/custom_call.h @@ -0,0 +1,34 @@ +/* Copyright 2023 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_CUSTOM_CALL_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_CUSTOM_CALL_H_ + +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project + +namespace mlir { +namespace odml { + +void PopulateCustomCallPatterns(MLIRContext* ctx, RewritePatternSet& patterns, + ConversionTarget& target); + +void PopulateCustomCallPreparePatterns(MLIRContext* ctx, + RewritePatternSet& patterns); + +} // namespace odml +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_CUSTOM_CALL_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/dot_general.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/dot_general.cc new file mode 100644 index 0000000000000000000000000000000000000000..47b07a74a97124643817f5c9285a84e0cdfa5d2d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/dot_general.cc @@ -0,0 +1,435 @@ +/* Copyright 2023 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. +==============================================================================*/ + +// This file implements logic for legalizing mhlo.dot_general to +// tflite.batch_matmul. +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/dot_general.h" + +#include +#include +#include +#include + +#include "llvm/ADT/APFloat.h" +#include "llvm/ADT/APInt.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/ImplicitLocOpBuilder.h" // from @llvm-project +#include "mlir/IR/Location.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/IR/ValueRange.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir { +namespace odml { +namespace { +// A struct to hold axes and sizes for a set of dimensions. +struct DimensionVector { + llvm::ArrayRef AxesArray() const { return axes; } + llvm::ArrayRef SizesArray() const { return sizes; } + + llvm::SmallVector axes; + llvm::SmallVector sizes; +}; + +// Appends all elements in `range` to `values`. +template +void Append(llvm::SmallVectorImpl& values, Range&& range) { + values.insert(values.end(), range.begin(), range.end()); +} + +// Appends all elements in `range` to `values`. +template +void Append(llvm::SmallVectorImpl& values, Range&& range, + RangeTs&&... ranges) { + values.insert(values.end(), range.begin(), range.end()); + Append(values, ranges...); +} + +// Returns the number of elements in `range`. +template +size_t Size(Range&& range) { + return range.size(); +} + +// Returns the total number of elements in a variadic number of `ranges`. +template +size_t Size(Range&& range, RangeTs&&... ranges) { + return range.size() + Size(std::forward(ranges)...); +} + +// Concats all elements in `ranges` and returns a small vector as a result. +template +llvm::SmallVector Concat(RangeTs&&... ranges) { + llvm::SmallVector results; + results.reserve(Size(std::forward(ranges)...)); + Append(results, std::forward(ranges)...); + return results; +} + +// A struct to hold information about dimensions of dot_general operands. +class DotDimensionsInfo { + public: + DotDimensionsInfo(ShapedType type, ArrayRef batch_dimensions, + ArrayRef contracting_dimensions) { + const int64_t rank = type.getRank(); + for (const int64_t dim : batch_dimensions) { + batch_dimensions_.axes.push_back(dim); + batch_dimensions_.sizes.push_back(type.getDimSize(dim)); + } + + // Create a sorted contracting dimensions array. This currently doesn't + // support dynamic tensors. + llvm::SmallVector sorted_contracting_dimensions = + llvm::to_vector(contracting_dimensions); + if (type.hasStaticShape()) { + llvm::sort(sorted_contracting_dimensions); + } + for (const int64_t dim : sorted_contracting_dimensions) { + contracting_dimensions_.axes.push_back(dim); + contracting_dimensions_.sizes.push_back(type.getDimSize(dim)); + } + + for (int64_t dim = 0; dim < rank; ++dim) { + if (llvm::count(contracting_dimensions_.axes, dim) > 0 || + llvm::count(batch_dimensions_.axes, dim) > 0) { + continue; + } + out_dimensions_.axes.push_back(dim); + out_dimensions_.sizes.push_back(type.getDimSize(dim)); + } + } + + const DimensionVector& batch_dimensions() const { return batch_dimensions_; } + const DimensionVector& contracting_dimensions() const { + return contracting_dimensions_; + } + // Out dimensions are any dimensions that are neither batch nor contracting + // dimensions, hence will be propagated to output shape. + const DimensionVector& out_dimensions() const { return out_dimensions_; } + + // Returns the total dimension size after flattening all contracting + // dimensions. + int64_t FlattenedContractingDimensionSize() const { + if (ShapedType::isDynamicShape(contracting_dimensions_.sizes)) { + return ShapedType::kDynamic; + } + return std::accumulate(contracting_dimensions_.sizes.begin(), + contracting_dimensions_.sizes.end(), 1, + std::multiplies()); + } + + // Returns the total dimension size after flattening all out dimensions. + int64_t FlattenedOutDimensionSize() const { + if (ShapedType::isDynamicShape(out_dimensions_.sizes)) { + return ShapedType::kDynamic; + } + return std::accumulate(out_dimensions_.sizes.begin(), + out_dimensions_.sizes.end(), 1, + std::multiplies()); + } + + private: + DimensionVector batch_dimensions_; + DimensionVector contracting_dimensions_; + // Out dimensions are any dimensions that are neither batch nor contracting + // dimensions, hence will be propagated to output shape. + DimensionVector out_dimensions_; +}; + +// Calculates the flattened shapes for dynamic shaped operands in +// mhlo.dot_general: +// 1. flattened_out_dim = UnsortedSegmentProdOp(operand_shape, out_axes) +// 2. flattened_contracting_dim = UnsortedSegmentProdOp(operand_shape, +// contracting_axes) +// 3. batch_dimensions = Gather(operand_shape, batch_axes) +// 4. flattened_shape = Concat(batch_dimensions, flattened_out_dim, +// flattened_contracting_dim) +// The flattened shape for LHS +// is like [batch_dimensions, flattened_out_dimension, +// flattened_contracting_dimension] and [batch_dimensions, +// flattened_contracting_dimension, flattened_out_dimension] for RHS. +Value BuildDotOperandFlattenedShapeOp(Value operand, + DotDimensionsInfo dot_dimensions_info, + ImplicitLocOpBuilder& builder, + bool is_lhs) { + auto operand_type = mlir::cast(operand.getType()); + auto operand_shape = TFL::ShapeOp::create( + builder, + RankedTensorType::get(static_cast(operand_type.getRank()), + builder.getIntegerType(32)), + operand); + const int64_t operand_rank = operand_type.getRank(); + // Compute flattened out dimension and contracting dimension using + // TFL::UnsortedSegmentProdOp. + llvm::SmallVector flattened_out_segids = + llvm::SmallVector(operand_rank, static_cast(-1)); + for (int64_t i : dot_dimensions_info.out_dimensions().AxesArray()) { + flattened_out_segids[i] = 0; + } + llvm::SmallVector flattened_contracting_segids = + llvm::SmallVector(operand_rank, static_cast(-1)); + for (int64_t i : dot_dimensions_info.contracting_dimensions().AxesArray()) { + flattened_contracting_segids[i] = 0; + } + auto seg_prod_result_type = + RankedTensorType::get(static_cast(1), builder.getI32Type()); + auto out_segids_cst = TFL::ConstOp::create( + builder, builder.getI32TensorAttr(flattened_out_segids)); + auto contracting_segids_cst = TFL::ConstOp::create( + builder, builder.getI32TensorAttr(flattened_contracting_segids)); + auto num_segids_tensor = TFL::ConstOp::create( + builder, DenseIntElementsAttr::get( + RankedTensorType::get({}, builder.getIntegerType(32)), 1)); + auto flattened_out_dims = TFL::UnsortedSegmentProdOp::create( + builder, seg_prod_result_type, operand_shape, out_segids_cst, + num_segids_tensor); + auto flattened_contracting_dims = TFL::UnsortedSegmentProdOp::create( + builder, seg_prod_result_type, operand_shape, contracting_segids_cst, + num_segids_tensor); + llvm::SmallVector flattend_shape_values; + // Gather the batch dimensions. + if (!dot_dimensions_info.batch_dimensions().AxesArray().empty()) { + if (ShapedType::isDynamicShape( + dot_dimensions_info.batch_dimensions().SizesArray())) { + auto batch_axes_tensor = TFL::ConstOp::create( + builder, builder.getI64TensorAttr( + dot_dimensions_info.batch_dimensions().AxesArray())); + auto batch_dims = TFL::GatherOp::create( + builder, + RankedTensorType::get( + {static_cast( + dot_dimensions_info.batch_dimensions().AxesArray().size())}, + builder.getIntegerType(32)), + operand_shape, batch_axes_tensor, /*axis*/ 0, /*batch_dims*/ 0); + flattend_shape_values.push_back(batch_dims); + } else { + llvm::SmallVector batch_i32_vec; + for (int64_t element : + dot_dimensions_info.batch_dimensions().SizesArray()) { + batch_i32_vec.push_back(static_cast(element)); + } + auto batch_dims = TFL::ConstOp::create( + builder, builder.getI32TensorAttr(batch_i32_vec)); + flattend_shape_values.push_back(batch_dims); + } + } + flattend_shape_values.push_back( + (is_lhs ? flattened_out_dims : flattened_contracting_dims)); + flattend_shape_values.push_back( + (is_lhs ? flattened_contracting_dims : flattened_out_dims)); + + auto concat_result_type = RankedTensorType::get( + {static_cast( + dot_dimensions_info.batch_dimensions().AxesArray().size()) + + 2}, + builder.getIntegerType(32)); + // Concatenate the batch dimensions, flattened out dimension and flattened + // contracting dimension. + return TFL::ConcatenationOp::create(builder, concat_result_type, + flattend_shape_values, /*axis*/ 0, + /*fused_activation_function*/ "NONE"); +} +} // namespace + +Value ConvertDot(PatternRewriter& rewriter, Value lhs, Value rhs, + mhlo::DotDimensionNumbersAttr dot_dimension_numbers, + ShapedType result_type, mlir::Location loc) { + auto lhs_type = mlir::cast(lhs.getType()); + auto rhs_type = mlir::cast(rhs.getType()); + const int lhs_rank = lhs_type.getRank(); + const int rhs_rank = rhs_type.getRank(); + ImplicitLocOpBuilder builder(loc, rewriter); + + // Collects lhs and rhs dimensions information. + DotDimensionsInfo lhs_dot_dimensions_info( + lhs_type, dot_dimension_numbers.getLhsBatchingDimensions(), + dot_dimension_numbers.getLhsContractingDimensions()); + DotDimensionsInfo rhs_dot_dimensions_info( + rhs_type, dot_dimension_numbers.getRhsBatchingDimensions(), + dot_dimension_numbers.getRhsContractingDimensions()); + + // Transposes lhs shape to be in the order of {batch_dimensions, + // out_dimensions, contracting dimensions}. + llvm::SmallVector lhs_permutation = Concat( + lhs_dot_dimensions_info.batch_dimensions().AxesArray(), + lhs_dot_dimensions_info.out_dimensions().AxesArray(), + lhs_dot_dimensions_info.contracting_dimensions().AxesArray()); + llvm::SmallVector lhs_transposed_shape = Concat( + lhs_dot_dimensions_info.batch_dimensions().SizesArray(), + lhs_dot_dimensions_info.out_dimensions().SizesArray(), + lhs_dot_dimensions_info.contracting_dimensions().SizesArray()); + auto lhs_transposed = mhlo::TransposeOp::create( + rewriter, loc, + RankedTensorType::get(lhs_transposed_shape, lhs_type.getElementType()), + lhs, + DenseIntElementsAttr::get( + RankedTensorType::get({lhs_rank}, rewriter.getI64Type()), + lhs_permutation)); + + // Transposes rhs shape to be in the order of {batch_dimensions, contracting + // dimensions, out_dimensions}. + llvm::SmallVector rhs_permutation = Concat( + rhs_dot_dimensions_info.batch_dimensions().AxesArray(), + rhs_dot_dimensions_info.contracting_dimensions().AxesArray(), + rhs_dot_dimensions_info.out_dimensions().AxesArray()); + llvm::SmallVector rhs_transposed_shape = Concat( + rhs_dot_dimensions_info.batch_dimensions().SizesArray(), + rhs_dot_dimensions_info.contracting_dimensions().SizesArray(), + rhs_dot_dimensions_info.out_dimensions().SizesArray()); + auto rhs_transposed = mhlo::TransposeOp::create( + rewriter, loc, + RankedTensorType::get(rhs_transposed_shape, rhs_type.getElementType()), + rhs, + DenseIntElementsAttr::get( + RankedTensorType::get({rhs_rank}, rewriter.getI64Type()), + rhs_permutation)); + // Reshapes lhs to flatten out_dimensions and contracting_dimensions. + llvm::SmallVector lhs_flattened_shape = Concat( + lhs_dot_dimensions_info.batch_dimensions().SizesArray(), + llvm::ArrayRef{ + lhs_dot_dimensions_info.FlattenedOutDimensionSize()}, + llvm::ArrayRef{ + lhs_dot_dimensions_info.FlattenedContractingDimensionSize()}); + Value lhs_flattend; + if (lhs_type.hasStaticShape()) { + lhs_flattend = mhlo::ReshapeOp::create( + rewriter, loc, + RankedTensorType::get(lhs_flattened_shape, lhs_type.getElementType()), + lhs_transposed.getResult()); + } else { + auto lhs_flattend_shape_op = BuildDotOperandFlattenedShapeOp( + lhs, lhs_dot_dimensions_info, builder, /*is_lhs=*/true); + lhs_flattend = mhlo::DynamicReshapeOp::create( + rewriter, loc, + RankedTensorType::get(lhs_flattened_shape, lhs_type.getElementType()), + lhs_transposed, lhs_flattend_shape_op); + } + + // Reshapes rhs to flatten out_dimensions and contracting_dimensions. + llvm::SmallVector rhs_flattened_shape = Concat( + rhs_dot_dimensions_info.batch_dimensions().SizesArray(), + llvm::ArrayRef{ + rhs_dot_dimensions_info.FlattenedContractingDimensionSize()}, + llvm::ArrayRef{ + rhs_dot_dimensions_info.FlattenedOutDimensionSize()}); + Value rhs_flattend; + if (rhs_type.hasStaticShape()) { + rhs_flattend = mhlo::ReshapeOp::create( + rewriter, loc, + RankedTensorType::get(rhs_flattened_shape, rhs_type.getElementType()), + rhs_transposed.getResult()); + } else { + auto rhs_flattend_shape_op = BuildDotOperandFlattenedShapeOp( + rhs, rhs_dot_dimensions_info, builder, /*is_lhs=*/false); + rhs_flattend = mhlo::DynamicReshapeOp::create( + rewriter, loc, + RankedTensorType::get(rhs_flattened_shape, rhs_type.getElementType()), + rhs_transposed, rhs_flattend_shape_op); + } + + // Creates matmul op of `lhs_flattend` and `rhs_flattend`. + llvm::SmallVector matmul_shape = + Concat(lhs_dot_dimensions_info.batch_dimensions().SizesArray(), + llvm::ArrayRef{ + lhs_dot_dimensions_info.FlattenedOutDimensionSize()}, + llvm::ArrayRef{ + rhs_dot_dimensions_info.FlattenedOutDimensionSize()}); + BoolAttr false_attr = rewriter.getBoolAttr(false); + auto matmul = TFL::BatchMatMulOp::create( + rewriter, loc, + RankedTensorType::get(matmul_shape, result_type.getElementType()), + lhs_flattend, rhs_flattend, /*adj_x*/ false_attr, /*adj_y*/ false_attr, + /*asym_quant_input*/ false_attr); + if (result_type.hasStaticShape()) { + auto reshaped = + mhlo::ReshapeOp::create(rewriter, loc, result_type, matmul.getResult()); + return reshaped.getResult(); + } + + // Reshape for dynamic shaped operands. The result shape is + // [lhs_batch_dimensions, lhs_out_dimensions, rhs_out_dimensions]. + auto lhs_shape = TFL::ShapeOp::create( + rewriter, loc, + RankedTensorType::get(static_cast(lhs_type.getRank()), + builder.getIntegerType(32)), + lhs); + auto rhs_shape = TFL::ShapeOp::create( + rewriter, loc, + RankedTensorType::get(static_cast(rhs_type.getRank()), + builder.getIntegerType(32)), + rhs); + llvm::SmallVector lhs_batch_and_out = + Concat(lhs_dot_dimensions_info.batch_dimensions().AxesArray(), + lhs_dot_dimensions_info.out_dimensions().AxesArray()); + auto lhs_batch_and_out_cst = TFL::ConstOp::create( + rewriter, loc, rewriter.getI64TensorAttr(lhs_batch_and_out)); + auto lhs_batch_and_out_dims = TFL::GatherOp::create( + rewriter, loc, + RankedTensorType::get({static_cast(lhs_batch_and_out.size())}, + rewriter.getIntegerType(32)), + lhs_shape, lhs_batch_and_out_cst, + /*axis*/ 0, /*batch_dims*/ 0); + auto rhs_out_cst = TFL::ConstOp::create( + rewriter, loc, + rewriter.getI64TensorAttr( + rhs_dot_dimensions_info.out_dimensions().AxesArray())); + auto rhs_out_dims = TFL::GatherOp::create( + rewriter, loc, + RankedTensorType::get( + {static_cast( + rhs_dot_dimensions_info.out_dimensions().AxesArray().size())}, + rewriter.getIntegerType(32)), + rhs_shape, rhs_out_cst, + /*axis*/ 0, /*batch_dims*/ 0); + auto result_shape_type = RankedTensorType::get( + {static_cast( + lhs_dot_dimensions_info.batch_dimensions().AxesArray().size() + + lhs_dot_dimensions_info.out_dimensions().AxesArray().size() + + rhs_dot_dimensions_info.out_dimensions().AxesArray().size())}, + rewriter.getIntegerType(32)); + auto result_shape = TFL::ConcatenationOp::create( + rewriter, loc, result_shape_type, + ValueRange{lhs_batch_and_out_dims, rhs_out_dims}, 0, "NONE"); + + auto reshaped = mhlo::DynamicReshapeOp::create( + rewriter, loc, result_type, matmul.getResult(), result_shape); + return reshaped.getResult(); +} + +LogicalResult LowerDotGeneralOp::matchAndRewrite( + mhlo::DotGeneralOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const { + auto val = ConvertDot( + rewriter, op.getLhs(), op.getRhs(), op.getDotDimensionNumbers(), + mlir::cast(op.getResult().getType()), op.getLoc()); + rewriter.replaceOp(op, val.getDefiningOp()); + return mlir::success(); +} + +} // namespace odml +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/dot_general.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/dot_general.h new file mode 100644 index 0000000000000000000000000000000000000000..028f397747001653468e5ecb493d8692b14bc6c7 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/dot_general.h @@ -0,0 +1,53 @@ +/* Copyright 2023 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. +==============================================================================*/ + +// Legalize mhlo.dot_general to tflite.batch_matmul. + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_DOT_GENERAL_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_DOT_GENERAL_H_ + +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir { +namespace odml { +// Converts mhlo.dot_general to tfl.BatchMatMul. Reshape and Transpose ops will +// be inserted to convert to well-formed matrix multiply; i.e., mhlo.dot_general +// -> tfl.batch_matmul(mhlo.transpose(mhlo.reshape(operand)), ...). +// Note: +// 1) Reshape/transpose are inserted because tfl.BatchMatMul requires +// size(contracting_dimensions) = 1 and size(output_dim) = 1, whereas +// mhlo.dot_general has no such restriction. +// 2) Inserted mhlo.reshape/transpose will be legalized to tf.reshape/transpose +// in LegalizeHloToTf (then from tf to tfl later). +// 3) If the operands are dynamic shaped tensors, mhlo.DynamicReshapeOp is +// inserted instead of the regular reshape, and additional ops (e.g. Gather, +// Concat ) are inserted for shape inference purposes. +// 4) All the DotOp are converted to DotGeneral during the optimization pass +// (ConvertDotOp). +class LowerDotGeneralOp : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::DotGeneralOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final; +}; +} // namespace odml +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_DOT_GENERAL_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/fft.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/fft.cc new file mode 100644 index 0000000000000000000000000000000000000000..a9bf2b04fcdbabbe3d670143ba17847515202d19 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/fft.cc @@ -0,0 +1,291 @@ +/* Copyright 2024 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/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/fft.h" + +#include + +#include +#include +#include +#include + +#include "mhlo/IR/hlo_ops.h" +#include "llvm/ADT/SmallVector.h" +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" // IWYU pragma: keep +#include "tensorflow/compiler/mlir/tensorflow/utils/dynamic_shape_utils.h" +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir::odml { +namespace { + +// Convert a DenseIntElementsAttr to a vector of int64_t. +std::vector ConvertI64DenseIntAttr(DenseIntElementsAttr attr) { + auto values = attr.getValues(); + return {values.begin(), values.end()}; +} + +// Returns true if the fft op is a supported rfft op. +bool IsSupportedRfftOp(mhlo::FftOp fft_op) { + const auto fft_type = llvm::StringSwitch>( + mlir::mhlo::stringifyFftType(fft_op.getFftType())) + .Case("FFT", mhlo::FftType::FFT) + .Case("RFFT", mhlo::FftType::RFFT) + .Case("IFFT", mhlo::FftType::IFFT) + .Case("IRFFT", mhlo::FftType::IRFFT) + .Default(std::nullopt); + if (!fft_type || *fft_type != mhlo::FftType::RFFT) { + return false; + } + + const std::vector fft_lengths = + ConvertI64DenseIntAttr(fft_op.getFftLength()); + + if (fft_lengths.size() > 2) return false; // Only support 2D FFT. + + // Check if the trailing input shape matches the fft_lengths. + const std::vector input_shape = + mlir::cast(fft_op.getOperand().getType()).getShape(); + return std::equal(input_shape.end() - fft_lengths.size(), input_shape.end(), + fft_lengths.begin(), fft_lengths.end()); +} + +// Returns a tensor of the dimension size of the input tensor. Result of +// mhlo::GetDimensionSizeOp is always a scalar value, but we need a tensor to +// concatenate with other dimension sizes. +Value GetDimensionSizeTensor(OpBuilder& rewriter, Location loc, Value input, + int64_t dim) { + auto size_scalar = + mhlo::GetDimensionSizeOp::create(rewriter, loc, input, dim); + return mhlo::ReshapeOp::create( + rewriter, loc, RankedTensorType::get({1}, rewriter.getI32Type()), + size_scalar); +} + +// Convert rfft to rfft2d. +// The transformation pattern looks like below: +// +// input fft_len +// \ / +// rfft +// +// || +// \/ +// +// input fft_len +// \ / +// expand_dim concat with [1] at the front +// \ / +// rfft_2d +// | +// squeeze +class ConvertNDFftTo2DFftOp : public OpRewritePattern { + public: + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(mhlo::FftOp fft_op, + PatternRewriter& rewriter) const final { + if (!IsSupportedRfftOp(fft_op)) { + return rewriter.notifyMatchFailure(fft_op, "Unsupported fft op."); + } + + const auto fft_lengths = + llvm::to_vector(fft_op.getFftLength().getValues()); + if (fft_lengths.size() != 1) { + return rewriter.notifyMatchFailure( + fft_op, "Can only lower a single fft dimension"); + } + + auto input_type = + mlir::dyn_cast_or_null(fft_op.getOperand().getType()); + const std::vector input_shape = + input_type + ? input_type.getShape() + : mlir::cast(fft_op.getOperand().getType()).getShape(); + + Value fft_operand = fft_op.getOperand(); + auto output_type = mlir::cast(fft_op.getResult().getType()); + + // Create a new fft_length attribute for the 2D FFT. + SmallVector new_fft_lengths = {1, fft_lengths.back()}; + auto new_fft_lengths_attr = rewriter.getI64TensorAttr(new_fft_lengths); + + bool is_dynamic_shape = !input_type || !input_type.hasStaticShape(); + + // Input can have a single trivial batch dim next to the fft dimension, in + // which case we don't need to expand the input. + if (input_shape[input_shape.size() - 2] != 1) { + const std::vector output_shape = output_type.getShape(); + + // [a, b, c, d, e] -> [a, b, c, d, 1, e] + SmallVector expanded_input_shape{input_shape.begin(), + input_shape.end() - 1}; + expanded_input_shape.push_back(1); + expanded_input_shape.push_back(input_shape.back()); + auto expanded_input_type = tensorflow::GetTypeFromTFTensorShape( + expanded_input_shape, + input_type ? input_type.getElementType() + : mlir::cast(fft_op.getOperand().getType()) + .getElementType()); + + // Dynamic shape needs to be handled separately as mhlo::ReshapeOp does + // not support dynamic shape. + if (is_dynamic_shape) { + // Programmatically- + // 1. Get the dimensions of the input tensor and create shape vector. + // 2. Insert a 1 as the penultimate dimension size. + // 3. Concatenate the dimension sizes to create a new SHAPE tensor. + SmallVector expanded_input_shape_values; + for (int i = 0; i < input_shape.size() - 1; ++i) { + expanded_input_shape_values.push_back(GetDimensionSizeTensor( + rewriter, fft_op.getLoc(), fft_operand, i)); + } + expanded_input_shape_values.push_back(mhlo::ConstantOp::create( + rewriter, fft_op.getLoc(), rewriter.getI32TensorAttr({1}))); + expanded_input_shape_values.push_back(GetDimensionSizeTensor( + rewriter, fft_op.getLoc(), fft_operand, input_shape.size() - 1)); + + auto expanded_input_shape_tensor = mhlo::ConcatenateOp::create( + rewriter, fft_op.getLoc(), + RankedTensorType::get( + {static_cast(expanded_input_shape_values.size())}, + rewriter.getI32Type()), + expanded_input_shape_values, 0); + + // Create a new mhlo.dynamic_reshape op with the expanded input and + // expanded input shape. SHAPE tensor is created in the previous step. + fft_operand = mhlo::DynamicReshapeOp::create( + rewriter, fft_op.getLoc(), expanded_input_type, fft_operand, + expanded_input_shape_tensor); + } else { + fft_operand = mhlo::ReshapeOp::create(rewriter, fft_op.getLoc(), + expanded_input_type, fft_operand); + } + + SmallVector new_output_shape = {output_shape.begin(), + output_shape.end() - 1}; + new_output_shape.push_back(1); + new_output_shape.push_back(output_shape.back()); + // Create a new mhlo.fft op with the expanded input and fft_length. + output_type = mlir::RankedTensorType::get(new_output_shape, + output_type.getElementType()); + } + + auto new_fft = + mhlo::FftOp::create(rewriter, fft_op.getLoc(), output_type, fft_operand, + fft_op.getFftType(), new_fft_lengths_attr); + + if (input_shape[input_shape.size() - 2] != 1) { + // Squeeze the output dimensions back to 2D. + if (is_dynamic_shape) { + SmallVector output_shape_values; + for (int i = 0; i < new_fft.getResult().getType().getShape().size() - 2; + ++i) { + output_shape_values.push_back(GetDimensionSizeTensor( + rewriter, fft_op.getLoc(), new_fft.getResult(), i)); + } + output_shape_values.push_back(GetDimensionSizeTensor( + rewriter, fft_op.getLoc(), new_fft.getResult(), + new_fft.getResult().getType().getShape().size() - 1)); + + auto shape_tensor = mhlo::ConcatenateOp::create( + rewriter, fft_op.getLoc(), + RankedTensorType::get( + {static_cast(output_shape_values.size())}, + rewriter.getI32Type()), + output_shape_values, 0); + auto squeeze_op = mhlo::DynamicReshapeOp::create( + rewriter, fft_op.getLoc(), fft_op.getResult().getType(), + new_fft.getResult(), shape_tensor); + rewriter.replaceOp(fft_op, squeeze_op.getResult()); + } else { + auto squeeze_op = mhlo::ReshapeOp::create(rewriter, fft_op.getLoc(), + fft_op.getResult().getType(), + new_fft.getResult()); + rewriter.replaceOp(fft_op, squeeze_op.getResult()); + } + } else { + rewriter.replaceOp(fft_op, new_fft.getResult()); + } + + return success(); + } +}; + +class LegalizeRfftOp : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::FftOp fft_op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final { + if (!IsSupportedRfftOp(fft_op)) { + return rewriter.notifyMatchFailure(fft_op, "Unsupported fft op."); + } + + auto input_type = + mlir::dyn_cast_or_null(fft_op.getOperand().getType()); + if (!input_type) + return rewriter.notifyMatchFailure(fft_op, "Unsupported input type."); + + const auto fft_lengths = + llvm::to_vector(fft_op.getFftLength().getValues()); + if (fft_lengths.size() != 2) { + return rewriter.notifyMatchFailure( + fft_op, "TFLite RFFT2d requires 2D FFT Length."); + } + + llvm::SmallVector fft_lengths_i32{fft_lengths.begin(), + fft_lengths.end()}; + auto fft_len_f32_attr = mlir::DenseIntElementsAttr::get( + mlir::RankedTensorType::get(2, rewriter.getIntegerType(32)), + fft_lengths_i32); + + auto output_type = mlir::cast(fft_op.getResult().getType()); + auto fft_len_const = + arith::ConstantOp::create(rewriter, fft_op.getLoc(), fft_len_f32_attr); + auto tfl_rfft2d = + TFL::RFFT2dOp::create(rewriter, fft_op.getLoc(), output_type, + fft_op.getOperand(), fft_len_const); + + rewriter.replaceOp(fft_op, tfl_rfft2d.getResult()); + + return success(); + } +}; + +// Returns true if the fft op is a legal fft op. +bool IsLegalFftOp(mhlo::FftOp fft_op) { return !IsSupportedRfftOp(fft_op); } + +} // namespace + +void PopulateLegalizeFftPatterns(MLIRContext* ctx, RewritePatternSet& patterns, + ConversionTarget& target) { + patterns.add(ctx); + target.addDynamicallyLegalOp(IsLegalFftOp); +} + +void PopulatePrepareFftPatterns(MLIRContext* ctx, RewritePatternSet& patterns) { + patterns.add(ctx); +} + +} // namespace mlir::odml diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/fft.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/fft.h new file mode 100644 index 0000000000000000000000000000000000000000..b45e37dbf280302122ade3c0714582f14698bb50 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/fft.h @@ -0,0 +1,33 @@ +/* Copyright 2024 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_FFT_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_FFT_H_ + +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project + +namespace mlir::odml { + +// Patterns to legalize mhlo.fft to TFL. +void PopulateLegalizeFftPatterns(MLIRContext* ctx, RewritePatternSet& patterns, + ConversionTarget& target); + +// Patterns to prepare mhlo.fft to TFL. +void PopulatePrepareFftPatterns(MLIRContext* ctx, RewritePatternSet& patterns); + +} // namespace mlir::odml + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_FFT_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/gather.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/gather.cc new file mode 100644 index 0000000000000000000000000000000000000000..09842d1f379a767785419f06edc7ae297eb75250 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/gather.cc @@ -0,0 +1,689 @@ +/* Copyright 2024 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/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/gather.h" + +#include +#include +#include + +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallVector.h" +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/ImplicitLocOpBuilder.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/ValueRange.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/util.h" +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir::odml { + +std::optional IsGatherLegal(mhlo::GatherOp op) { return std::nullopt; } + +class LegalizeGatherToSlice : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + LogicalResult matchAndRewrite( + mhlo::GatherOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final; +}; + +LogicalResult LegalizeGatherToSlice::matchAndRewrite( + mhlo::GatherOp gather_op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const { + Value operand = gather_op.getOperand(); + Value start_indices = gather_op.getStartIndices(); + static const int rank_two = 2; + // This converts a gather op to multiple slice ops, cap the number of slice + // ops allowed. + static const int max_batch_size = 50; + + // Can only convert with static shaped gather. + ShapedType operand_type = mlir::cast(operand.getType()); + ShapedType start_indices_type = + mlir::cast(start_indices.getType()); + ShapedType result_type = + mlir::cast(gather_op.getResult().getType()); + if (!operand_type.hasStaticShape() || !start_indices_type.hasStaticShape() || + !result_type.hasStaticShape()) { + return rewriter.notifyMatchFailure( + gather_op, + "Dynamic shaped inputs are not supported when legalizing mhlo.gather " + "op to tf.slice."); + } + + auto start_index_map = gather_op.getDimensionNumbers().getStartIndexMap(); + auto collapsed_slice_dims = + gather_op.getDimensionNumbers().getCollapsedSliceDims(); + auto offset_dims = gather_op.getDimensionNumbers().getOffsetDims(); + auto slice_sizes = gather_op.getSliceSizes(); + llvm::SmallVector slice_sizes_vector; + slice_sizes_vector.reserve(slice_sizes.size()); + for (int64_t s : slice_sizes.getValues()) { + slice_sizes_vector.push_back(s); + } + + llvm::SmallVector batch_dims; + // Offset dims are guaranteed to be sorted. + int offset_index = 0; + for (int64_t i = 0; i < result_type.getRank(); ++i) { + if (offset_index >= offset_dims.size() || offset_dims[offset_index] != i) { + batch_dims.push_back(i); + } else { + ++offset_index; + } + } + // Here we only support gather with one batch dim and the batch dim is 0. + if (batch_dims.size() != 1 || batch_dims[0] != 0) { + return failure(); + } + int64_t batch_dim = batch_dims[0]; + // Batch dim in operand and start indices should match. + if (operand_type.getDimSize(batch_dim) > max_batch_size || + operand_type.getRank() != rank_two || + start_indices_type.getRank() != rank_two || + operand_type.getDimSize(batch_dim) != + start_indices_type.getDimSize(batch_dim) || + slice_sizes_vector[batch_dim] != 1) { + return failure(); + } + // Here we only support the case where [0, 1] in start_indices maps to + // operand[0, 1] + for (int64_t i = 0; i < start_index_map.size(); i++) { + if (start_index_map[i] != i) { + return failure(); + } + } + // Collapsed slice dims should contain the batch dim. + if (collapsed_slice_dims.size() != start_index_map.size() - 1 || + collapsed_slice_dims.size() != 1 || collapsed_slice_dims[0] != 0) { + return failure(); + } + + // Normalize start_indices so index_vector_dim == start_indices.rank() - 1. + int64_t index_vector_dim = + gather_op.getDimensionNumbers().getIndexVectorDim(); + if (failed(NormalizeIndexVector(gather_op, start_indices, start_indices_type, + index_vector_dim, rewriter))) { + return failure(); + } + + ImplicitLocOpBuilder builder(gather_op.getLoc(), rewriter); + // Clamp the start indices to ensure it is in bounds. + auto max_start_indices = BuildIntArrayConstOp( + builder, rewriter, + llvm::SmallVector( + {operand_type.getDimSize(0) - slice_sizes_vector[0], + operand_type.getDimSize(1) - slice_sizes_vector[1]}), + start_indices_type.getElementType()); + auto min_start_indices = BuildIntArrayConstOp( + builder, rewriter, llvm::SmallVector({0, 0}), + start_indices_type.getElementType()); + auto start_indices_max_op = TFL::MaximumOp::create( + rewriter, gather_op.getLoc(), start_indices, min_start_indices); + auto clamped_start_indices_op = TFL::MinimumOp::create( + rewriter, gather_op.getLoc(), start_indices_max_op, max_start_indices); + + int64_t batch_size = start_indices_type.getDimSize(batch_dim); + auto slice_size = BuildIntArrayConstOp( + builder, rewriter, slice_sizes_vector, rewriter.getI32Type()); + if (batch_size == 1) { + auto squeeze_op = TFL::SqueezeOp::create( + rewriter, gather_op.getLoc(), + RankedTensorType::get({rank_two}, start_indices_type.getElementType()), + clamped_start_indices_op, + rewriter.getI64ArrayAttr(llvm::ArrayRef({batch_dim}))); + auto slice_op = + TFL::SliceOp::create(rewriter, gather_op.getLoc(), gather_op.getType(), + operand, squeeze_op, slice_size); + rewriter.replaceOp(gather_op, slice_op); + return mlir::success(); + } + + llvm::SmallVector slices; + slices.reserve(batch_size); + for (int64_t i = 0; i < batch_size; ++i) { + auto zero = BuildIntArrayConstOp( + builder, rewriter, llvm::SmallVector({i, 0}), + rewriter.getI32Type()); + auto two = BuildIntArrayConstOp( + builder, rewriter, llvm::SmallVector({1, 2}), + rewriter.getI32Type()); + // TODO maybe cast to i32 here + auto begin = TFL::SliceOp::create( + rewriter, gather_op.getLoc(), + RankedTensorType::get({1, 2}, start_indices_type.getElementType()), + clamped_start_indices_op, zero, two); + // TODO maybe cast to i32 here + auto squeeze_op = TFL::SqueezeOp::create( + rewriter, gather_op.getLoc(), + RankedTensorType::get({rank_two}, start_indices_type.getElementType()), + begin, rewriter.getI64ArrayAttr(llvm::ArrayRef({batch_dim}))); + auto slice_op = TFL::SliceOp::create( + rewriter, gather_op.getLoc(), + RankedTensorType::get({1, slice_sizes_vector[1]}, + operand_type.getElementType()), + operand, squeeze_op, slice_size); + slices.push_back(slice_op); + } + auto concat_op = TFL::ConcatenationOp::create(rewriter, gather_op.getLoc(), + result_type, slices, 0, "NONE"); + rewriter.replaceOp(gather_op, concat_op); + return mlir::success(); +} + +namespace { + +DenseIntElementsAttr GetI64ElementsAttr(ArrayRef values, + Builder* builder) { + RankedTensorType ty = RankedTensorType::get( + {static_cast(values.size())}, builder->getIntegerType(64)); + return DenseIntElementsAttr::get(ty, values); +} + +// Transform the canonicalized result produced by tf.GatherNd with the +// canonicalized operand and start indices back into the original result. +// The canonicalized result will have the start indices batching dimensions +// flattened as leading dimension, and the offset dimensions as trailing +// dimensions. To transform back, we: +// - Unflatten the start indices batching dimensions. +// - Introduce trivial index dimensions that aren't in `collapsed_slice_dims`. +// - Transpose dimensions back based on `offset_dims` and +// `start_indices_batching_dims`. +Value UncanonicalizeResult(mhlo::GatherOp gather_op, Value canonical_result, + ShapedType canonical_result_type, + ShapedType original_result_type, + ArrayRef offset_dims, + ArrayRef operand_batching_dims, + ArrayRef start_indices_batching_dims, + ArrayRef start_index_map, + ArrayRef slice_sizes, + ArrayRef collapsed_slice_dims, + ConversionPatternRewriter& rewriter) { + // For those dims NOT inside the original_offset_dims are considered "batch + // dims". + std::vector batch_dims; + // Offset dims are guaranteed to be sorted. + int offset_index = 0; + for (int64_t i = 0; i < original_result_type.getRank(); ++i) { + if (offset_index >= offset_dims.size() || offset_dims[offset_index] != i) { + batch_dims.push_back(i); + } else { + ++offset_index; + } + } + + // Determine the canonical shape after unflattening the start indices + // batching dimensions (if they exist) and introducing any trivial index + // dimensions that weren't collapsed. Also compute the permutation to + // transform the original shape to the unflattened canonical shape. + llvm::SmallVector permutation_to_canonical; + llvm::SmallVector unflattened_shape; + for (int64_t i : start_indices_batching_dims) { + int64_t dim = batch_dims[i]; + permutation_to_canonical.push_back(dim); + unflattened_shape.push_back(original_result_type.getDimSize(dim)); + } + for (int64_t i = 0; i < batch_dims.size(); ++i) { + if (llvm::count(start_indices_batching_dims, i) == 0) { + int64_t dim = batch_dims[i]; + permutation_to_canonical.push_back(dim); + unflattened_shape.push_back(original_result_type.getDimSize(dim)); + } + } + // The remaining dimensions are the offset dims. We expect non-collapsed + // indexed dimensions first, followed by the rest of the operand dimensions. + llvm::SmallVector operand_dim_to_offset_dim_map(slice_sizes.size(), + -1); + int offset_dim_index = 0; + llvm::SmallVector remaining_operand_dims; + for (int64_t operand_dim = 0; operand_dim < slice_sizes.size(); + ++operand_dim) { + if (llvm::count(collapsed_slice_dims, operand_dim) || + llvm::count(operand_batching_dims, operand_dim)) { + continue; + } else { + if (llvm::count(start_index_map, operand_dim) == 0) { + remaining_operand_dims.push_back(operand_dim); + } + operand_dim_to_offset_dim_map[operand_dim] = + offset_dims[offset_dim_index++]; + } + } + for (int64_t s : start_index_map) { + if (llvm::count(collapsed_slice_dims, s) == 0) { + int64_t dim = operand_dim_to_offset_dim_map[s]; + permutation_to_canonical.push_back(dim); + unflattened_shape.push_back(original_result_type.getDimSize(dim)); + } + } + for (int64_t operand_dim : remaining_operand_dims) { + int64_t dim = operand_dim_to_offset_dim_map[operand_dim]; + permutation_to_canonical.push_back(dim); + unflattened_shape.push_back(original_result_type.getDimSize(dim)); + } + + // Reshape the result to unflatten the batching dimensions and add back any + // non-collapsed indexed dimensions. The caller should ensure that a + // reshape is not needed if the result has dynamic dimensions. + if (canonical_result_type.hasStaticShape()) { + auto unflattened_result_type = RankedTensorType::get( + unflattened_shape, original_result_type.getElementType()); + canonical_result = + mhlo::ReshapeOp::create(rewriter, gather_op.getLoc(), + unflattened_result_type, canonical_result); + } + // Transpose back to the original result shape. + return mhlo::TransposeOp::create( + rewriter, gather_op.getLoc(), original_result_type, canonical_result, + rewriter.getI64TensorAttr( + GetInversePermutationArray(permutation_to_canonical))); +} + +// Canonicalize `operand` to handle operand batching dimensions and non-iota +// start index map, so it can be used by tf.GatherNd: +// - Transpose so that the leading dimensions are the operand batching +// dimensions followed by the indexed dimensions (in order). +// - Flatten the batching dimensions. +Value CanonicalizeOperand(mhlo::GatherOp gather_op, Value operand, + ShapedType operand_type, + ArrayRef operand_batching_dims, + ArrayRef start_index_map, + ConversionPatternRewriter& rewriter) { + int batch_size = 1; + llvm::SmallVector permutation; + llvm::SmallVector transposed_shape; + llvm::SmallVector flattened_shape; + // First add the batching dimensions. + for (int64_t batch_dim : operand_batching_dims) { + permutation.push_back(batch_dim); + transposed_shape.push_back(operand_type.getDimSize(batch_dim)); + batch_size *= operand_type.getDimSize(batch_dim); + } + if (!operand_batching_dims.empty()) { + flattened_shape.push_back(batch_size); + } + // Add the indexed dimensions. + for (int64_t s : start_index_map) { + permutation.push_back(s); + transposed_shape.push_back(operand_type.getDimSize(s)); + flattened_shape.push_back(operand_type.getDimSize(s)); + } + // Finally, add the remaining dimensions. + for (int64_t i = 0; i < operand_type.getRank(); i++) { + if (llvm::count(operand_batching_dims, i) == 0 && + llvm::count(start_index_map, i) == 0) { + permutation.push_back(i); + transposed_shape.push_back(operand_type.getDimSize(i)); + flattened_shape.push_back(operand_type.getDimSize(i)); + } + } + + // Transpose the dimensions and flatten the batching dimensions. + RankedTensorType transposed_type = + RankedTensorType::get(transposed_shape, operand_type.getElementType()); + auto transposed_operand = mhlo::TransposeOp::create( + rewriter, gather_op.getLoc(), transposed_type, operand, + rewriter.getI64TensorAttr(permutation)); + auto flattened_type = + RankedTensorType::get(flattened_shape, operand_type.getElementType()); + auto flattened_operand = mhlo::ReshapeOp::create( + rewriter, gather_op.getLoc(), flattened_type, transposed_operand); + return flattened_operand; +} + +// Canonicalize `start_indices` to handle start indices batching dimensions so +// it can be used by tf.GatherNd: +// - Transpose so that the batching dimensions are the leading dimensions. +// - Flatten the batching dimensions if they exist. +// - For each indexed dimension with non-trivial slicing, introduce a new +// dimension, and broadcast and add iota values to the indices. +// - Add iota index values for the operand batching dimensions. +Value CanonicalizeStartIndices(mhlo::GatherOp gather_op, Value start_indices, + ShapedType start_indices_type, + ArrayRef start_indices_batching_dims, + ArrayRef start_index_map, + ArrayRef slice_sizes, + ConversionPatternRewriter& rewriter) { + int batch_size = 1; + llvm::SmallVector permutation; + llvm::SmallVector transposed_shape; + llvm::SmallVector reshaped_shape; + + // First add the batching dimensions. + for (int64_t batch_dim : start_indices_batching_dims) { + permutation.push_back(batch_dim); + transposed_shape.push_back(start_indices_type.getDimSize(batch_dim)); + batch_size *= start_indices_type.getDimSize(batch_dim); + } + if (!start_indices_batching_dims.empty()) { + reshaped_shape.push_back(batch_size); + } + + // Add remaining dimensions before the final index vector dim. + for (int64_t dim = 0; dim < start_indices_type.getRank() - 1; dim++) { + if (llvm::count(start_indices_batching_dims, dim) == 0) { + permutation.push_back(dim); + transposed_shape.push_back(start_indices_type.getDimSize(dim)); + reshaped_shape.push_back(start_indices_type.getDimSize(dim)); + } + } + + // Introduce new dimensions associated with each indexed operand dimension + // that is taking a non-trivial slice. We will broadcast and add iota values + // after reshaping. See comment below for more details. + int64_t first_non_trivial_sliced_dim = reshaped_shape.size(); + for (int64_t operand_dim : start_index_map) { + if (slice_sizes[operand_dim] > 1) { + reshaped_shape.push_back(1); + } + } + + // Add the index vector dimension. + int64_t index_vector_size = + start_indices_type.getDimSize(start_indices_type.getRank() - 1); + permutation.push_back(permutation.size()); + transposed_shape.push_back(index_vector_size); + reshaped_shape.push_back(index_vector_size); + + // Transpose the dimensions and flatten the batching dimensions. + auto transposed_start_indices = mhlo::TransposeOp::create( + rewriter, gather_op.getLoc(), + RankedTensorType::get(transposed_shape, + start_indices_type.getElementType()), + start_indices, rewriter.getI64TensorAttr(permutation)); + start_indices = mhlo::ReshapeOp::create( + rewriter, gather_op.getLoc(), + RankedTensorType::get(reshaped_shape, + start_indices_type.getElementType()), + transposed_start_indices); + + // Because tf.GatherNd does not support non-trivial slicing on indexed + // dimensions, we introduce new dimensions in start_indices and broadcast + // and add iota values to the indices. For example: + // + // operand_shape = [10, 10, 10] + // start_indices_original_shape = [1, 3] + // start_index_map = [0, 1, 2] + // slice_sizes = [1, 5, 1] + // + // We then transform the start indices by broadcasting the shape to + // [1, 5, 3], and adding the iota tensor with the following values: + // + // [[[ 0 0 0 ] + // [ 0 1 0 ] + // [ 0 2 0 ] + // [ 0 3 0 ] + // [ 0 4 0 ]]] + // + // This allows us to take trivial slices when indexing into operand + // dimension 1. + llvm::SmallVector start_indices_shape = reshaped_shape; + int64_t non_trivial_sliced_dim = first_non_trivial_sliced_dim; + for (int i = 0; i < start_index_map.size(); ++i) { + int64_t operand_dim = start_index_map[i]; + if (slice_sizes[operand_dim] == 1) { + continue; + } + // Create iota values along the sliced dimension. + llvm::SmallVector offsets_shape(start_indices_shape.size(), 1); + offsets_shape[non_trivial_sliced_dim] = slice_sizes[operand_dim]; + start_indices_shape[non_trivial_sliced_dim] = slice_sizes[operand_dim]; + auto offsets = mhlo::IotaOp::create( + rewriter, gather_op.getLoc(), + RankedTensorType::get(offsets_shape, + start_indices_type.getElementType()), + rewriter.getI64IntegerAttr(non_trivial_sliced_dim)); + non_trivial_sliced_dim++; + + // Pad with 0s on the other operand dimensions. + Value zero = arith::ConstantOp::create( + rewriter, gather_op.getLoc(), + rewriter.getZeroAttr( + RankedTensorType::get({}, start_indices_type.getElementType()))); + int rank = offsets_shape.size(); + llvm::SmallVector padding_low(rank, 0); + llvm::SmallVector padding_high(rank, 0); + llvm::SmallVector padding_interior(rank, 0); + padding_low.back() = i; + padding_high.back() = start_indices_shape.back() - i - 1; + auto padded_offsets = + mhlo::PadOp::create(rewriter, gather_op.getLoc(), offsets, zero, + GetI64ElementsAttr(padding_low, &rewriter), + GetI64ElementsAttr(padding_high, &rewriter), + GetI64ElementsAttr(padding_interior, &rewriter)); + + // Add the padded offsets to the start indices (with broadcasting). + start_indices = TFL::AddOp::create( + rewriter, gather_op.getLoc(), start_indices, padded_offsets, + /*fusedActivationFunction=*/ + mlir::StringAttr::get(rewriter.getContext(), "NONE")); + } + + if (!start_indices_batching_dims.empty()) { + // Concat iota values for indexing into the batching dimensions of the + // operand. + llvm::SmallVector offsets_shape = start_indices_shape; + offsets_shape.back() = 1; + auto offsets = mhlo::IotaOp::create( + rewriter, gather_op.getLoc(), + RankedTensorType::get(offsets_shape, + start_indices_type.getElementType()), + rewriter.getI64IntegerAttr(0)); + + start_indices_shape.back()++; + start_indices = mhlo::ConcatenateOp::create( + rewriter, gather_op.getLoc(), + RankedTensorType::get(start_indices_shape, + start_indices_type.getElementType()), + ValueRange{offsets, start_indices}, + rewriter.getI32IntegerAttr(start_indices_shape.size() - 1)); + } + + return start_indices; +} +} // namespace + +// Tries to convert an mhlo::GatherOp into a TFL::GatherNdOp. +// +// Consider the following example: +// operand_shape = [B1, I1, O1, B2, I2, O2] +// operand_batching_dims = [0, 3] +// +// start_indices_shape = [B2, B3, B1, 2] +// start_indices_batching_dims = [3, 0] +// index_vector_dim = 3 +// start_index_map = [4, 1] +// +// offset_dims: [2, 4] +// slice_sizes = [1, 1, O1, 1, 1, O2] +// collapsed_slice_dims = [1, 4] +// result_shape = [B2, B3, O1, B3, O2] +// +// To implement this with a tfl.GatherNd, we canonicalize the operand s.t. the +// operand batching dimensions are flattened into the leading dimensions, +// followed by the indexed dimensions in order: +// canonical_operand_shape = [B1 * B2, I2, I1, O1, O2] +// +// We canonicalize the start indices so the start indices batching dimensions +// are flattened (in order) into a leading dimension. In addition, we add iota +// indices to appropriately offset into the flattened operand batching +// dimension: +// canonical_start_indices_shape = [B1 * B2, B3, 3] +// (index_vector_dim is expanded to included indices for the operand +// batching dimensions) +// +// The result of tf.GatherNd(canonical_operand, canonical_start_indices) has the +// following shape: +// canonical_result_shape = [B1 * B2, B3, O1, O2] +// +// The canonical result is unflattened and transpose as needed to get back to +// the original result shape. +class LegalizeGatherToGatherND : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + LogicalResult matchAndRewrite( + mhlo::GatherOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final; +}; + +LogicalResult LegalizeGatherToGatherND::matchAndRewrite( + mhlo::GatherOp gather_op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const { + Value operand = gather_op.getOperand(); + Value start_indices = gather_op.getStartIndices(); + + // Can only convert with static shaped gather. + ShapedType operand_type = mlir::cast(operand.getType()); + ShapedType start_indices_type = + mlir::cast(start_indices.getType()); + ShapedType result_type = + mlir::cast(gather_op.getResult().getType()); + if (!operand_type.hasStaticShape()) { + gather_op.emitOpError() << "Dynamic shaped operand is not supported."; + return failure(); + } + + llvm::ArrayRef operand_batching_dims = + gather_op.getDimensionNumbers().getOperandBatchingDims(); + llvm::ArrayRef start_indices_batching_dims = + gather_op.getDimensionNumbers().getStartIndicesBatchingDims(); + llvm::ArrayRef start_index_map = + gather_op.getDimensionNumbers().getStartIndexMap(); + llvm::ArrayRef collapsed_slice_dims = + gather_op.getDimensionNumbers().getCollapsedSliceDims(); + if (!start_indices_type.hasStaticShape() || !result_type.hasStaticShape()) { + // Dynamic dimensions aren't supported in certain cases that require + // reshaping the indices or result. + if (!start_indices_batching_dims.empty()) { + gather_op.emitOpError() + << "Dynamic shaped start indices aren't supported when there are " + "batching dimensions."; + } + + // Verify that start_index_map and collapsed_slice_dims contains the same + // values. + if (start_index_map.size() != collapsed_slice_dims.size()) { + return rewriter.notifyMatchFailure( + gather_op, + "different size for start index map and collapsed slice dims"); + } + for (auto c : collapsed_slice_dims) { + if (llvm::count(start_index_map, c) == 0) { + return rewriter.notifyMatchFailure( + gather_op, "collapsed slice dim isn't present in start index map"); + } + } + } + + // Normalize start_indices so index_vector_dim == start_indices.rank() - 1. + int64_t index_vector_dim = + gather_op.getDimensionNumbers().getIndexVectorDim(); + if (failed(NormalizeIndexVector(gather_op, start_indices, start_indices_type, + index_vector_dim, rewriter))) { + return failure(); + } + start_indices_type = mlir::cast(start_indices.getType()); + + // Verify that slice_sizes is 1 for the batching dimensions and the full + // shape for non-indexed dimensions. + auto slice_sizes = gather_op.getSliceSizes(); + llvm::SmallVector slice_sizes_vector; + slice_sizes_vector.reserve(slice_sizes.size()); + for (int64_t s : slice_sizes.getValues()) { + slice_sizes_vector.push_back(s); + } + for (int i = 0; i < slice_sizes_vector.size(); ++i) { + int s = slice_sizes_vector[i]; + if (llvm::count(operand_batching_dims, i)) { + if (s != 1) { + return rewriter.notifyMatchFailure(gather_op, + "unsupported slice sizes"); + } + } else if (llvm::count(start_index_map, i) == 0) { + if (s != operand_type.getShape()[i]) { + return rewriter.notifyMatchFailure(gather_op, + "unsupported slice sizes"); + } + } + } + + // Canonicalize the operand and start indices. + auto canonical_operand = + CanonicalizeOperand(gather_op, operand, operand_type, + operand_batching_dims, start_index_map, rewriter); + auto canonical_operand_type = + mlir::cast(canonical_operand.getType()); + + auto canonical_start_indices = CanonicalizeStartIndices( + gather_op, start_indices, start_indices_type, start_indices_batching_dims, + start_index_map, slice_sizes_vector, rewriter); + auto canonical_start_indices_type = + mlir::cast(canonical_start_indices.getType()); + + TFL::CastOp cast_op = nullptr; + if (canonical_start_indices_type.getElementType().isUnsignedInteger(32)) { + cast_op = TFL::CastOp::create( + rewriter, gather_op->getLoc(), + RankedTensorType::get(canonical_start_indices_type.getShape(), + rewriter.getI64Type()), + canonical_start_indices); + } + + llvm::SmallVector canonical_result_shape; + for (int64_t i = 0; i < canonical_start_indices_type.getRank() - 1; ++i) { + canonical_result_shape.push_back( + canonical_start_indices_type.getDimSize(i)); + } + for (int64_t i = canonical_start_indices_type.getDimSize( + canonical_start_indices_type.getRank() - 1); + i < canonical_operand_type.getRank(); ++i) { + canonical_result_shape.push_back(canonical_operand_type.getDimSize(i)); + } + + auto canonical_result_type = RankedTensorType::get( + canonical_result_shape, result_type.getElementType()); + auto canonical_result = TFL::GatherNdOp::create( + rewriter, gather_op->getLoc(), canonical_result_type, canonical_operand, + cast_op ? cast_op.getResult() : canonical_start_indices); + + auto offset_dims = gather_op.getDimensionNumbers().getOffsetDims(); + auto final_result = UncanonicalizeResult( + gather_op, canonical_result, canonical_result_type, result_type, + offset_dims, operand_batching_dims, start_indices_batching_dims, + start_index_map, slice_sizes_vector, collapsed_slice_dims, rewriter); + + rewriter.replaceOp(gather_op, final_result); + return success(); +} + +void PopulateGatherPatterns(MLIRContext* ctx, RewritePatternSet& patterns, + ConversionTarget& target) { + // Prefer `LegalizeGatherToSlice` for the cases it handles, since it produces + // simpler IR. + patterns.add(ctx, /*benefit=*/2); + patterns.add(ctx); + target.addDynamicallyLegalOp(IsGatherLegal); +} + +} // namespace mlir::odml diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/gather.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/gather.h new file mode 100644 index 0000000000000000000000000000000000000000..f39bb383004293ab5fa6e7d286192c72067c0a26 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/gather.h @@ -0,0 +1,31 @@ +/* Copyright 2024 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_GATHER_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_GATHER_H_ + +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project + +namespace mlir::odml { + +// Patterns to legalize mhlo.gather to TFL +// +// Emits: tfl.gather_nd or a combination of tfl.slice, tfl.squeeze, tfl.concat +void PopulateGatherPatterns(MLIRContext* ctx, RewritePatternSet& patterns, + ConversionTarget& target); + +} // namespace mlir::odml + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_GATHER_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/gelu.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/gelu.cc new file mode 100644 index 0000000000000000000000000000000000000000..3199232359b850a84c3551a5fb92e0daac48aa39 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/gelu.cc @@ -0,0 +1,137 @@ +/* Copyright 2024 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/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/gelu.h" + +#include +#include + +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/Casting.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "stablehlo/dialect/ChloOps.h" // from @stablehlo +#include "stablehlo/dialect/StablehloOps.h" // from @stablehlo +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" // IWYU pragma: keep + +namespace mlir::odml { + +constexpr float kOne = 1.0; +const float kOneOverRoot2 = kOne / std::sqrt(2); +constexpr float kHalf = kOne / 2.0; +constexpr float kTolerance = kOne / 1000.0; + +// Gets the operation that uses the sole result of given operation +// if there is only one. +Operation* GetUserIfOnlyOne(Operation* op) { + if (op->getNumResults() != 1) return nullptr; + auto result = op->getResult(0); + if (!result.hasOneUse()) return nullptr; + return (*result.getUses().begin()).getOwner(); +} + +// Gets operation providing value for the given operand of given operation +// if the given operation is the only user. +Operation* GetInputOpWithOneUse(Operation* op, int opr_num) { + if (opr_num >= op->getNumOperands()) return nullptr; + auto opr = op->getOperand(opr_num); + if (llvm::isa(opr)) return nullptr; + auto* res = opr.getDefiningOp(); + if (!res->hasOneUse()) return nullptr; + return res; +} + +// Checks if the given operand of given operation refers to a splat constant +// with given val. +bool HasSplatArg(Operation* op, float val, int opr_num) { + auto* cst_input = GetInputOpWithOneUse(op, 1); + if (!cst_input) return false; + auto cst_op = llvm::dyn_cast_or_null(cst_input); + if (!cst_op) return false; + ElementsAttr value = cst_op.getValue(); + if (!value.isSplat()) return false; + if (!value.getElementType().isF32()) return false; + return std::abs(value.getSplatValue() - val) < kTolerance; +} + +// Determines if the given op is semantically that of the gauss error function. +bool MatchERF(Operation* op) { + if (auto custom_call = llvm::dyn_cast_or_null(op)) { + return custom_call.getCallTargetName() == "mhlo.erf"; + } + return llvm::isa(op); +} + +LogicalResult LowerGELU::matchAndRewrite(Operation* op, + PatternRewriter& rewriter) const { + if (!MatchERF(op)) return failure(); + // `add 1` + auto* erf_user = GetUserIfOnlyOne(op); + if (!erf_user) return failure(); + + // `mul` + auto* erf_user_user = GetUserIfOnlyOne(erf_user); + if (!erf_user_user) return failure(); + + // `mul 1/sqrt(2)` + auto* erf_input = GetInputOpWithOneUse(op, 0); + if (!erf_input) return failure(); + + // `mul 0.5` + auto* erf_user_user_input = GetInputOpWithOneUse(erf_user_user, 0); + if (!erf_user_user_input) return failure(); + + // Check `mul 0.5` and `mul 1/sqrt(2)` refer to the same input. + if (erf_user_user_input->getOperand(0) != erf_input->getOperand(0)) { + return failure(); + } + + // Check the structural matches have the correct op type and values. + auto rhs_mul = llvm::dyn_cast_or_null(erf_input); + if (!rhs_mul) return failure(); + + auto lhs_mul = llvm::dyn_cast_or_null(erf_user_user_input); + if (!lhs_mul) return failure(); + + auto output_mul = llvm::dyn_cast_or_null(erf_user_user); + if (!output_mul) return failure(); + + auto rhs_add = llvm::dyn_cast_or_null(erf_user); + if (!rhs_add) return failure(); + + if (!HasSplatArg(rhs_add, kOne, 1)) return failure(); + if (!HasSplatArg(lhs_mul, kHalf, 1)) return failure(); + if (!HasSplatArg(rhs_mul, kOneOverRoot2, 1)) return failure(); + + auto is_approx_attr = rewriter.getBoolAttr(false); + auto gelu = TFL::GeluOp::create(rewriter, output_mul.getLoc(), + output_mul.getResult().getType(), + erf_input->getOperand(0), is_approx_attr); + rewriter.replaceAllOpUsesWith(output_mul, gelu); + // Note these must be erased in reverse topo order to avoid + // failing in debug mode. + rewriter.eraseOp(output_mul); + rewriter.eraseOp(rhs_add); + rewriter.eraseOp(op); + rewriter.eraseOp(lhs_mul); + rewriter.eraseOp(rhs_mul); + + return success(); +} + +} // namespace mlir::odml diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/gelu.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/gelu.h new file mode 100644 index 0000000000000000000000000000000000000000..e3d750b3b9a4d472798dab204833479319fe98d9 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/gelu.h @@ -0,0 +1,47 @@ +/* Copyright 2024 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_GELU_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_GELU_H_ + +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project + +namespace mlir::odml { + +// Matches non-approximate GELU patterns. +// +// -> mul 1/sqrt(2) -> erf -> add 1 -> +// in mul +// ---------> mul 0.5 ---------------> +// +// This pattern assumes all binary ewise ops with one constant argument +// have that constant argument as the second operand. It works by +// identifying `erf` ops and validate the structure around them. +class LowerGELU : public RewritePattern { + public: + explicit LowerGELU(MLIRContext* context) + : RewritePattern(MatchAnyOpTypeTag(), /*benefit=*/1, context) {} + + LogicalResult matchAndRewrite(Operation* op, + PatternRewriter& rewriter) const override; +}; + +} // namespace mlir::odml + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_GELU_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/get_dimension_size.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/get_dimension_size.cc new file mode 100644 index 0000000000000000000000000000000000000000..09d461af31556f70b492da3f129806ab630b85f7 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/get_dimension_size.cc @@ -0,0 +1,88 @@ +/* Copyright 2024 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/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/get_dimension_size.h" + +#include + +#include "llvm/Support/Casting.h" +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/ImplicitLocOpBuilder.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" // IWYU pragma: keep +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/util.h" +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir::odml { +namespace { + +// Converts a MHLO::GetDimensionSizeOP to TFL ops. +class LeagalizeDimensionSizeOp + : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::GetDimensionSizeOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final { + ImplicitLocOpBuilder builder(op.getLoc(), rewriter); + auto operand_type = llvm::cast(op.getOperand().getType()); + + auto shaped_op_type = + RankedTensorType::get({operand_type.getRank()}, rewriter.getI64Type()); + Value shape_op = TFL::ShapeOp::create(rewriter, op.getLoc(), shaped_op_type, + op.getOperand()); + + Value size = BuildIntArrayConstOp(builder, rewriter, {1}, + rewriter.getI64Type()); + + auto begin = BuildIntArrayConstOp( + builder, rewriter, + llvm::SmallVector({static_cast(op.getDimension())}), + rewriter.getI64Type()); + + auto slice_type = RankedTensorType::get({1}, rewriter.getI64Type()); + Value slice = TFL::SliceOp::create(rewriter, op.getLoc(), slice_type, + shape_op, begin, size); + + auto op_el_type = llvm::cast(op.getType()).getElementType(); + if (op_el_type != slice_type.getElementType()) { + slice = TFL::CastOp::create(rewriter, op->getLoc(), + slice_type.clone(op_el_type), slice); + } + + rewriter.replaceOpWithNewOp(op, op.getType(), slice, + rewriter.getI64ArrayAttr({0})); + + return success(); + } +}; + +} // namespace + +void PopulateGetDimensionSizePatterns(MLIRContext* ctx, + RewritePatternSet& patterns, + ConversionTarget& target) { + target.addIllegalOp(); + patterns.add(ctx); +} + +} // namespace mlir::odml diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/get_dimension_size.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/get_dimension_size.h new file mode 100644 index 0000000000000000000000000000000000000000..a370407c1dd18802c2cffed224b7133fee473715 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/get_dimension_size.h @@ -0,0 +1,30 @@ +/* Copyright 2024 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_GET_DIMENSION_SIZE_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_GET_DIMENSION_SIZE_H_ + +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project + +namespace mlir::odml { + +void PopulateGetDimensionSizePatterns(MLIRContext* ctx, + RewritePatternSet& patterns, + ConversionTarget& target); + +} // namespace mlir::odml + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_GET_DIMENSION_SIZE_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/if.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/if.cc new file mode 100644 index 0000000000000000000000000000000000000000..bbcb5f6d714b4de468636b54869e1cf7e2b50e09 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/if.cc @@ -0,0 +1,58 @@ +/* Copyright 2024 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/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/if.h" + +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/Region.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" // IWYU pragma: keep +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/util.h" +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir::odml { +namespace { + +class LegalizeIfOp : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::IfOp if_op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final { + auto new_op = TFL::IfOp::create(rewriter, if_op.getLoc(), + if_op.getResultTypes(), if_op.getPred()); + + new_op.getThenRegion().takeBody(if_op.getTrueBranch()); + new_op.getElseRegion().takeBody(if_op.getFalseBranch()); + + ReplaceTerminatorWithYield(new_op.getThenRegion(), rewriter); + ReplaceTerminatorWithYield(new_op.getElseRegion(), rewriter); + + rewriter.replaceOp(if_op, new_op.getResults()); + return success(); + } +}; + +} // namespace + +void PopulateIfPatterns(MLIRContext* ctx, RewritePatternSet& patterns, + ConversionTarget& target) { + patterns.add(ctx); + target.addIllegalOp(); +} + +} // namespace mlir::odml diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/if.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/if.h new file mode 100644 index 0000000000000000000000000000000000000000..1ca2ee9ec19e39c0533254c78ccb78cde0f50468 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/if.h @@ -0,0 +1,30 @@ +/* Copyright 2024 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_IF_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_IF_H_ + +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir::odml { + +// Patterns to legalize mhlo.if to TFL. +void PopulateIfPatterns(MLIRContext* ctx, RewritePatternSet& patterns, + ConversionTarget& target); + +} // namespace mlir::odml + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_IF_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/iota.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/iota.cc new file mode 100644 index 0000000000000000000000000000000000000000..44d904c8a0312dffbcee6727fbfe21795082ba0b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/iota.cc @@ -0,0 +1,198 @@ +/* Copyright 2024 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/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/iota.h" + +#include +#include + +#include "llvm/ADT/SmallVector.h" +#include "llvm/Support/Casting.h" +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/op_util_common.h" +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir::odml { +namespace { + +//===------------------------------------------------------------------------=== +// mhlo.iota -> tfl.range +//===------------------------------------------------------------------------=== + +class LegalizeIota : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + LogicalResult matchAndRewrite( + mhlo::IotaOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final; +}; + +bool IsIotaLegal(mhlo::IotaOp op) { + auto e_type = llvm::cast(op.getType()).getElementType(); + return !(e_type.isF32() || e_type.isSignlessInteger(32) || + e_type.isSignlessInteger(64)); +} + +std::tuple +BuildRangeParams(Type e_type, int64_t iota_dim_size, OpBuilder& b) { + if (e_type.isInteger(32)) { + return std::tuple(BuildScalarDense(e_type, 0), + BuildScalarDense(e_type, iota_dim_size), + BuildScalarDense(e_type, 1)); + } else if (e_type.isInteger(64)) { + return std::tuple(BuildScalarDense(e_type, 0), + BuildScalarDense(e_type, iota_dim_size), + BuildScalarDense(e_type, 1)); + } + return std::tuple(BuildScalarDense(e_type, 0.0), + BuildScalarDense(e_type, iota_dim_size), + BuildScalarDense(e_type, 1.0)); +} + +LogicalResult LegalizeIota::matchAndRewrite( + mhlo::IotaOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const { + if (IsIotaLegal(op)) { + return rewriter.notifyMatchFailure(op, "Must be i32, i64 or f32"); + } + + auto type = llvm::cast(op.getType()); + auto e_type = type.getElementType(); + const int64_t iota_dim_size = type.getDimSize(op.getIotaDimension()); + + auto [start, limit, delta] = + BuildRangeParams(e_type, iota_dim_size, rewriter); + + auto start_op = arith::ConstantOp::create(rewriter, op->getLoc(), start); + auto limit_op = arith::ConstantOp::create(rewriter, op->getLoc(), limit); + auto delta_op = arith::ConstantOp::create(rewriter, op->getLoc(), delta); + + auto range_type = RankedTensorType::get({iota_dim_size}, e_type); + auto range_op = TFL::RangeOp::create(rewriter, op->getLoc(), range_type, + start_op, limit_op, delta_op); + + if (type.getRank() == 1) { + rewriter.replaceOp(op, range_op); + return success(); + } + + // mhlo.iota allows filling ND tensors iota-style. Reshape and broadcast + // tfl 1D range output. + + llvm::SmallVector reshape_shape(type.getRank(), 1); + reshape_shape[op.getIotaDimension()] = iota_dim_size; + Value reshape_shape_cst = arith::ConstantOp::create( + rewriter, op->getLoc(), rewriter.getI64TensorAttr(reshape_shape)); + reshape_shape_cst = + TFL::CastOp::create(rewriter, op->getLoc(), + llvm::cast(reshape_shape_cst.getType()) + .clone(rewriter.getI32Type()), + reshape_shape_cst); + + auto reshape_type = RankedTensorType::get(reshape_shape, e_type); + auto reshape_op = TFL::ReshapeOp::create(rewriter, op->getLoc(), reshape_type, + range_op, reshape_shape_cst); + + auto broad_cast_shape_cst = arith::ConstantOp::create( + rewriter, op->getLoc(), rewriter.getI64TensorAttr(type.getShape())); + + rewriter.replaceOpWithNewOp(op, type, reshape_op, + broad_cast_shape_cst); + + return success(); +} + +//===------------------------------------------------------------------------=== +// mhlo.dynamic_iota -> tfl.range +//===------------------------------------------------------------------------=== + +class LegalizeDynamicIotaOp : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::DynamicIotaOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final; +}; + +bool IsDynamicIotaLegal(mhlo::DynamicIotaOp op) { + auto type = llvm::cast(op.getType()); + auto element_type = type.getElementType(); + return (!element_type.isF32() && !element_type.isSignlessInteger(32) && + !element_type.isSignlessInteger(64)) || + type.getRank() > 1 || op.getIotaDimension() != 0; +} + +LogicalResult LegalizeDynamicIotaOp::matchAndRewrite( + mhlo::DynamicIotaOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const { + if (IsDynamicIotaLegal(op)) { + return failure(); + } + + auto type = llvm::cast(op.getType()); + Type element_type = type.getElementType(); + + auto [start, unused_limit, delta] = + BuildRangeParams(element_type, /*iota_dim_size*/ 0, rewriter); + + auto start_op = arith::ConstantOp::create(rewriter, op.getLoc(), start); + auto delta_op = arith::ConstantOp::create(rewriter, op.getLoc(), delta); + + auto output_shape = op.getOperand(); + if (mlir::isa(element_type)) { + auto cast_type = + mlir::cast(output_shape.getType()).clone(element_type); + output_shape = + TFL::CastOp::create(rewriter, op.getLoc(), cast_type, output_shape); + } + + DenseIntElementsAttr scalar_attr = DenseIntElementsAttr::get( + RankedTensorType::get({0}, rewriter.getI32Type()), + llvm::ArrayRef({})); + auto scalar_shape = + arith::ConstantOp::create(rewriter, op.getLoc(), scalar_attr); + auto limit_scalar = TFL::ReshapeOp::create( + rewriter, op.getLoc(), RankedTensorType::get({}, element_type), + output_shape, scalar_shape); + + const uint64_t dimension = op.getIotaDimension(); + auto range_type = + RankedTensorType::get({type.getShape()[dimension]}, element_type); + + rewriter.replaceOpWithNewOp(op, range_type, start_op, + limit_scalar, delta_op); + + return success(); +} + +} // namespace + +void PopulateIotaPatterns(MLIRContext* ctx, RewritePatternSet& patterns, + ConversionTarget& target) { + patterns.add(ctx); + target.addDynamicallyLegalOp(IsIotaLegal); + target.addDynamicallyLegalOp(IsDynamicIotaLegal); +} + +} // namespace mlir::odml diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/iota.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/iota.h new file mode 100644 index 0000000000000000000000000000000000000000..b4c1b9814ea00e0787ab0959c0ea2ee8efd2c8d9 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/iota.h @@ -0,0 +1,29 @@ +/* Copyright 2024 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_IOTA_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_IOTA_H_ + +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project + +namespace mlir::odml { + +void PopulateIotaPatterns(MLIRContext* ctx, RewritePatternSet& patterns, + ConversionTarget& target); + +} // namespace mlir::odml + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_IOTA_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/op_util_common.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/op_util_common.cc new file mode 100644 index 0000000000000000000000000000000000000000..13d9de74b20417fe04fe534fa64756c97e681c54 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/op_util_common.cc @@ -0,0 +1,111 @@ +/* Copyright 2024 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/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/op_util_common.h" + +#include +#include + +#include "llvm/ADT/STLExtras.h" +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project + +namespace mlir::odml { + +llvm::SmallVector Layout::GetPermForReLayout( + const Layout& to_layout) const { + llvm::SmallVector perm(to_layout.Rank()); + perm[to_layout.SpecialDim1()] = SpecialDim1(); + perm[to_layout.SpecialDim2()] = SpecialDim2(); + for (const auto [to_spatial, from_spatial] : + llvm::zip(to_layout.Spatials(), Spatials())) { + perm[to_spatial] = from_spatial; + } + return perm; +} + +llvm::SmallVector Layout::PermuteShape( + const Layout& to_layout, llvm::ArrayRef shape) const { + llvm::SmallVector new_shape(to_layout.Rank()); + const auto perm = GetPermForReLayout(to_layout); + for (const auto [ind, val] : llvm::enumerate(perm)) { + new_shape[ind] = shape[val]; + } + return new_shape; +} + +bool Layout::HasSpecialDims(int64_t special_dim1, int64_t special_dim2) const { + return SpecialDim1() == special_dim1 && SpecialDim2() == special_dim2; +} + +bool Layout::AreSpatialsIota() const { + llvm::ArrayRef spatials = Spatials(); + return llvm::all_of(llvm::enumerate(spatials), [&](const auto& it) { + return it.index() == 0 || (it.value() == spatials[it.index() - 1] + 1); + }); +} + +llvm::SmallVector ResolveStridesOrDilations( + int64_t rank, std::optional opt_attr) { + if (!opt_attr.has_value()) { + return llvm::SmallVector(rank, 1); + } + auto attr = opt_attr.value(); + if (attr.isSplat()) { + return llvm::SmallVector(rank, attr.getSplatValue()); + } + return llvm::SmallVector(attr.getValues()); +} + +llvm::SmallVector ResolvePadding( + int64_t rank, std::optional opt_padding) { + llvm::SmallVector res; + if (!opt_padding.has_value()) { + for (int i = 0; i < rank; ++i) { + res.push_back(DimPadding(0, 0)); + } + return res; + } + auto padding = opt_padding.value(); + if (padding.isSplat()) { + const int64_t val = padding.getSplatValue(); + for (int i = 0; i < rank; ++i) { + res.push_back(DimPadding(val, val)); + } + return res; + } + int64_t prev; + for (const auto [ind, val] : llvm::enumerate(padding.getValues())) { + const int64_t side = ind % 2; + if (side == 1) { + res.push_back(DimPadding(prev, val)); + } + prev = val; + } + return res; +} + +bool IsSamePaddingOnDim(int64_t in, int64_t dilate, int64_t stride, int64_t k, + const DimPadding& pad) { + const int64_t pad_diff = pad.Hi() - pad.Lo(); + if (pad_diff > 1 || pad_diff < 0) { + return false; + } + const int64_t pad_total = pad.Lo() + pad.Hi(); + const int64_t out = (in + stride - 1) / stride; + const int effective_filter = (k - 1) * dilate + 1; + return ((out - 1) * stride + effective_filter) == in + pad_total; +} + +} // namespace mlir::odml diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/op_util_common.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/op_util_common.h new file mode 100644 index 0000000000000000000000000000000000000000..b5b9dc4ceb25a412394c92571ee76f24d516b1bb --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/op_util_common.h @@ -0,0 +1,148 @@ +/* Copyright 2024 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_OP_UTIL_COMMON_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_OP_UTIL_COMMON_H_ + +#include +#include + +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/SmallVector.h" +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/Types.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project + +namespace mlir::odml { + +// Class that encodes the "layout" of a tensor. Layouts, generically +// are some naming of the dimensions of a tensor. In all cases, 2 dimensions +// are "special" (e.g. batch / feature) and the rest are referred to as "spatial +// dims". When the special dims are batch and feature, batch is special dim 1 +// and feature is special dim 2. When special dims are input and output features +// (conv filter), input features is special dim 1 and output features is special +// dim 2. +class Layout { + public: + llvm::ArrayRef Spatials() const { return spatials_; } + + int64_t NumSpatials() const { return spatials_.size(); } + + int64_t Rank() const { return NumSpatials() + 2; } + + Layout(int64_t special_dim1, int64_t special_dim2, ArrayRef spatials) + : special_dim1_(special_dim1), + special_dim2_(special_dim2), + spatials_(spatials) {} + + // TODO: b/351437662 - Consider just using 2 arrays for the case where + // there are more than 2 special dims. + int64_t SpecialDim1() const { return special_dim1_; } + + // Conveniance accesor for getting the dimension size of the first + // special dimension from a shape. + int64_t SpecialDim1(llvm::ArrayRef shape) const { + return shape[special_dim1_]; + } + + int64_t SpecialDim2() const { return special_dim2_; } + + // Convenience accesor for getting the dimension size of the second + // special dimension from a shape. + int64_t SpecialDim2(llvm::ArrayRef shape) const { + return shape[special_dim2_]; + } + + // Conveniance method for equality checking special dims. + bool HasSpecialDims(int64_t special_dim1, int64_t special_dim2) const; + + // Determines if the spatial dimensions are all adjacent and in + // ascending order. + bool AreSpatialsIota() const; + + // Gets a "permutation array" to be used for transposing a tensor + // of "this" layout to the given layout. A permutation array is some + // permutation of [0, 1, i...] for i < rank(layout). Assumes + // "this" and given layout have the same rank. + llvm::SmallVector GetPermForReLayout( + const Layout& to_layout) const; + + // Permutes given shape based on the permutaion implied to take this Layout to + // the given one. + llvm::SmallVector PermuteShape(const Layout& to_layout, + ArrayRef shape) const; + + bool operator==(const Layout& other) const { + return SpecialDim1() == other.SpecialDim1() && + SpecialDim2() == other.SpecialDim2() && + Spatials() == other.Spatials(); + } + + bool operator!=(const Layout& other) const { return !(*this == other); } + + private: + int64_t special_dim1_; + int64_t special_dim2_; + llvm::SmallVector spatials_; +}; + +// Wrapper for the padding attrs along a single dimension. +class DimPadding { + public: + int64_t Hi() const { return hi_; } + + int64_t Lo() const { return lo_; } + + bool Trivial() const { return Hi() == 0 && Lo() == 0; } + + DimPadding(int64_t lo, int64_t hi) : lo_(lo), hi_(hi) {} + + private: + int64_t lo_; + int64_t hi_; +}; + +inline llvm::SmallVector UnrollI64Splat(DenseElementsAttr data) { + if (!data.isSplat()) { + return llvm::SmallVector(data.getValues()); + } + return llvm::SmallVector(data.getType().getNumElements(), + data.getSplatValue()); +} + +// Resolves optional strides or dilations attributes. If not present, +// will return trivial 1's vector. +llvm::SmallVector ResolveStridesOrDilations( + int64_t rank, std::optional opt_attr); + +// Resolves optional paddings attributes. If not present, will return +// trivial [0, 0] paddings on each dim. +llvm::SmallVector ResolvePadding( + int64_t rank, std::optional opt_padding); + +// Does the padding correspond to "SAME" on given dimension configuration. +// Assumes given dimension configuration is well formed. +bool IsSamePaddingOnDim(int64_t in, int64_t dilate, int64_t stride, int64_t k, + const DimPadding& pad); + +template +inline DenseElementsAttr BuildScalarDense(Type e_type, T val) { + auto type = RankedTensorType::get({}, e_type); + return DenseElementsAttr::get(type, val); +} + +} // namespace mlir::odml + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_OP_UTIL_COMMON_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/pad.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/pad.cc new file mode 100644 index 0000000000000000000000000000000000000000..1c19c9e327d1e19671c1f2da89f1ad977b3857bd --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/pad.cc @@ -0,0 +1,141 @@ + +/* Copyright 2024 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/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/pad.h" + +#include + +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallVector.h" +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/Matchers.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" // IWYU pragma: keep +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/op_util_common.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/pad_util.h" +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir::odml { +namespace { + +bool IsPadLegal(mhlo::PadOp op) { + return AnyNegativePads(op) || !TrivialInterior(op); +} + +bool IsPadValCstZero(mhlo::PadOp op) { + if (matchPattern(op.getPaddingValue(), m_AnyZeroFloat())) { + return true; + } + if (matchPattern(op.getPaddingValue(), m_Zero())) { + return true; + } + return false; +} + +DenseIntElementsAttr BuildTFLPaddingAttr(OpBuilder& b, mhlo::PadOp op) { + auto lows = UnrollI64Splat(op.getEdgePaddingLow()); + auto highs = UnrollI64Splat(op.getEdgePaddingHigh()); + + llvm::SmallVector res; + for (auto [l, h] : llvm::zip(lows, highs)) { + res.push_back(l); + res.push_back(h); + } + + const int64_t n_dims = res.size(); + auto tfl_padding_type = + RankedTensorType::get({n_dims / 2, 2}, b.getI64Type()); + return DenseIntElementsAttr::get(tfl_padding_type, res); +} + +//===------------------------------------------------------------------------=== +// mhlo.pad -> tfl.pad +//===------------------------------------------------------------------------=== + +class LegalizePad : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + LogicalResult matchAndRewrite( + mhlo::PadOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final; +}; + +LogicalResult LegalizePad::matchAndRewrite( + mhlo::PadOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const { + if (IsPadLegal(op)) { + return rewriter.notifyMatchFailure(op, "Matching an already legal pad op."); + } + if (!IsPadValCstZero(op)) { + return rewriter.notifyMatchFailure( + op, "Legalizing to padv1 requires zero const padding values."); + } + + auto tfl_paddings = BuildTFLPaddingAttr(rewriter, op); + auto paddings_op = + arith::ConstantOp::create(rewriter, op->getLoc(), tfl_paddings); + + rewriter.replaceOpWithNewOp(op, op.getType(), op.getOperand(), + paddings_op); + return success(); +} + +//===------------------------------------------------------------------------=== +// mhlo.pad -> tfl.padv2 +//===------------------------------------------------------------------------=== + +class LegalizePadV2 : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + LogicalResult matchAndRewrite( + mhlo::PadOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final; +}; + +LogicalResult LegalizePadV2::matchAndRewrite( + mhlo::PadOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const { + if (IsPadLegal(op)) { + return rewriter.notifyMatchFailure(op, "Matching an already legal pad op."); + } + if (IsPadValCstZero(op)) { + return rewriter.notifyMatchFailure( + op, "Legalizing to padv2 requires non zero const padding values."); + } + + auto tfl_paddings = BuildTFLPaddingAttr(rewriter, op); + auto paddings_op = + arith::ConstantOp::create(rewriter, op->getLoc(), tfl_paddings); + + rewriter.replaceOpWithNewOp(op, op.getType(), op.getOperand(), + paddings_op, op.getPaddingValue()); + return success(); +} + +} // namespace + +void PopulatePadPatterns(MLIRContext* ctx, RewritePatternSet& patterns, + ConversionTarget& target) { + patterns.add(ctx); + patterns.add(ctx); + target.addDynamicallyLegalOp(IsPadLegal); +} + +} // namespace mlir::odml diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/pad.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/pad.h new file mode 100644 index 0000000000000000000000000000000000000000..5d056d2e658b46633a3053da153152809058560f --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/pad.h @@ -0,0 +1,39 @@ +/* Copyright 2024 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_PAD_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_PAD_H_ + +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir::odml { + +// Patterns to legalize mhlo.pad to TFL +// +// Prefers tfl.pad over tfl.padv2 when it can be asserted that the pad +// values are zero. +// +// Matches: mhlo.pad +// padding_high/low: all positive or zero +//. interior_padding: all zero +// +// Emits: tfl.pad, tfl.padv2 +void PopulatePadPatterns(MLIRContext* ctx, RewritePatternSet& patterns, + ConversionTarget& target); + +} // namespace mlir::odml + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_PAD_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/pad_util.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/pad_util.cc new file mode 100644 index 0000000000000000000000000000000000000000..f548ba18b3b751a5c10082297c45814795d1e68b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/pad_util.cc @@ -0,0 +1,74 @@ +/* Copyright 2023 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/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/pad_util.h" + +#include + +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallVector.h" +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/op_util_common.h" +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir::odml { + +ShapedType GetPaddingAttrType(mhlo::PadOp op) { + return op.getEdgePaddingLow().getType(); +} + +DenseIntElementsAttr SliceStartFromNegPadLows(mhlo::PadOp op) { + auto vals = UnrollI64Splat(op.getEdgePaddingLow()); + auto starts = llvm::map_range( + vals, [](auto v) -> int64_t { return (v >= 0) ? 0 : -1 * v; }); + return DenseIntElementsAttr::get(GetPaddingAttrType(op), + llvm::to_vector(starts)); +} + +DenseIntElementsAttr SliceEndFromNegPadHighs(mhlo::PadOp op) { + auto vals = UnrollI64Splat(op.getEdgePaddingHigh()); + auto zip = llvm::zip(vals, op.getOperand().getType().getShape()); + auto ends = llvm::map_range(zip, [](auto it) -> int64_t { + return (std::get<0>(it) >= 0) ? std::get<1>(it) + : std::get<1>(it) + std::get<0>(it); + }); + return DenseIntElementsAttr::get(GetPaddingAttrType(op), + llvm::to_vector(ends)); +} + +DenseIntElementsAttr ReplaceNegsWithZero(DenseElementsAttr data) { + auto vals = UnrollI64Splat(data); + auto res = + llvm::map_range(vals, [](auto v) -> int64_t { return (v < 0) ? 0 : v; }); + return DenseIntElementsAttr::get(data.getType(), llvm::to_vector(res)); +} + +bool AnyNegativePads(mhlo::PadOp op) { + auto is_neg = [](int64_t v) { return v < 0; }; + auto lows_data = UnrollI64Splat(op.getEdgePaddingLow()); + auto highs_data = UnrollI64Splat(op.getEdgePaddingHigh()); + return llvm::any_of(lows_data, is_neg) || llvm::any_of(highs_data, is_neg); +} + +bool TrivialInterior(mhlo::PadOp op) { + auto interior = op.getInteriorPadding(); + const bool trivial_splat = + interior.isSplat() && interior.getSplatValue() == 0; + const bool all_trivial = llvm::all_of(interior.getValues(), + [](auto v) { return v == 0; }); + return trivial_splat || all_trivial; +} + +} // namespace mlir::odml diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/pad_util.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/pad_util.h new file mode 100644 index 0000000000000000000000000000000000000000..1966a8bdb637d65aaeddbb2220615d7c9fe88a95 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/pad_util.h @@ -0,0 +1,41 @@ +/* Copyright 2023 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_PAD_UTIL_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_PAD_UTIL_H_ + +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir::odml { + +// Gets elements corresponding to slice starts from negative padding +// values. +DenseIntElementsAttr SliceStartFromNegPadLows(mhlo::PadOp op); + +// Gets elements corresponding to slice ends from negative padding +// values. +DenseIntElementsAttr SliceEndFromNegPadHighs(mhlo::PadOp op); + +// Gets a copy of `data` with negative values replaced with 0. +DenseIntElementsAttr ReplaceNegsWithZero(DenseElementsAttr data); + +bool AnyNegativePads(mhlo::PadOp op); + +bool TrivialInterior(mhlo::PadOp op); + +} // namespace mlir::odml + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_PAD_UTIL_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/reduce.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/reduce.cc new file mode 100644 index 0000000000000000000000000000000000000000..b4f826a349b54ce540f6a1b9a3e5824fad57efd9 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/reduce.cc @@ -0,0 +1,721 @@ +/* Copyright 2024 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/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/reduce.h" + +#include +#include + +#include "llvm/Support/Casting.h" +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/IR/Block.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/Matchers.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/hlo_matchers.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/util.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir { +namespace odml { + +//===------------------------------------------------------------------------=== +// mhlo.reduce -> arg min/max +//===------------------------------------------------------------------------=== + +// Pattern matches the following reduction function for ArgMax/ArgMin coming +// from PyTorch +// %0 = compare{GT}(%lhs_value, %rhs_value) +// %1 = select(%0, %lhs_value, %rhs_value) +// %2 = compare{EQ}(%lhs_value, %rhs_value) +// %3 = minimum(%lhs_index, %rhs_index) +// %4 = select(%0, %lhs_index, %rhs_index) +// %5 = select(%2, %3, %4) +// return %1, %5 +LogicalResult MatchReduceToArgMinMaxType2(mhlo::ReduceOp reduce_op, + bool is_argmax) { + Block& body = reduce_op.getBody().front(); + if (body.getNumArguments() != 4) return failure(); + + mhlo::ReturnOp return_op = dyn_cast(body.back()); + if (!return_op || return_op.getNumOperands() != 2) return failure(); + + mhlo::SelectOp value_select = llvm::dyn_cast_or_null( + return_op.getOperand(0).getDefiningOp()); + if (!value_select || value_select.getOnTrue() != body.getArgument(0) || + value_select.getOnFalse() != body.getArgument(2)) + return failure(); + + auto compare_direction_included = + is_argmax ? mhlo::ComparisonDirection::GE : mhlo::ComparisonDirection::LE; + mhlo::CompareOp value_gt = llvm::dyn_cast_or_null( + value_select.getOperand(0).getDefiningOp()); + if (!value_gt || + value_gt.getComparisonDirection() != compare_direction_included || + value_gt.getLhs() != body.getArgument(0) || + value_gt.getRhs() != body.getArgument(2)) + return failure(); + + mhlo::SelectOp index_select = llvm::dyn_cast_or_null( + return_op.getOperand(1).getDefiningOp()); + if (!index_select) return failure(); + + mhlo::MinOp index_select_min = llvm::dyn_cast_or_null( + index_select.getOnTrue().getDefiningOp()); + if (!index_select_min || index_select_min.getLhs() != body.getArgument(1) || + index_select_min.getRhs() != body.getArgument(3)) + return failure(); + + mhlo::SelectOp index_select_select = llvm::dyn_cast_or_null( + index_select.getOnFalse().getDefiningOp()); + if (!index_select_select || + index_select_select.getOnTrue() != body.getArgument(1) || + index_select_select.getOnFalse() != body.getArgument(3) || + index_select_select.getOperand(0).getDefiningOp() != value_gt) + return failure(); + + mhlo::CompareOp value_eq = llvm::dyn_cast_or_null( + index_select.getOperand(0).getDefiningOp()); + if (!value_eq || + value_eq.getComparisonDirection() != mhlo::ComparisonDirection::EQ || + value_eq.getLhs() != body.getArgument(0) || + value_eq.getRhs() != body.getArgument(2)) + return failure(); + + return success(); +} + +// Pattern matches the following reduction function for ArgMax/ArgMin: +// %0 = compare{GT}(%lhs_value, %rhs_value) +// %1 = compare{NE}(%lhs_value, %lhs_value) +// %2 = or(%0, %1) +// %3 = select(%2, %lhs_value, %rhs_value) +// %4 = compare{EQ}(%lhs_value, %rhs_value) +// %5 = compare{LT}(%lhs_index, %rhs_index) +// %6 = and(%4, %5) +// %7 = or(%2, %6) +// %8 = select(%7, %lhs_index, %rhs_index) +// return %3, %8 +// Also note that %1 may be folded if %lhs_value is of integer types. +LogicalResult MatchReduceToArgMinMaxType1(mhlo::ReduceOp reduce_op, + bool is_float, bool is_argmax) { + Block& body = reduce_op.getBody().front(); + if (body.getNumArguments() != 4) return failure(); + + mhlo::ReturnOp return_op = dyn_cast(body.back()); + if (!return_op || return_op.getNumOperands() != 2) return failure(); + + mhlo::SelectOp value_select = llvm::dyn_cast_or_null( + return_op.getOperand(0).getDefiningOp()); + if (!value_select || value_select.getOnTrue() != body.getArgument(0) || + value_select.getOnFalse() != body.getArgument(2)) + return failure(); + + auto compare_direction = + is_argmax ? mhlo::ComparisonDirection::GT : mhlo::ComparisonDirection::LT; + if (is_float) { + mhlo::OrOp value_or = llvm::dyn_cast_or_null( + value_select.getOperand(0).getDefiningOp()); + if (!value_or) return failure(); + + mhlo::CompareOp value_gt = llvm::dyn_cast_or_null( + value_or.getLhs().getDefiningOp()); + if (!value_gt || value_gt.getComparisonDirection() != compare_direction || + value_gt.getLhs() != body.getArgument(0) || + value_gt.getRhs() != body.getArgument(2)) + return failure(); + + mhlo::CompareOp value_ne = llvm::dyn_cast_or_null( + value_or.getRhs().getDefiningOp()); + if (!value_ne || + value_ne.getComparisonDirection() != mhlo::ComparisonDirection::NE || + value_ne.getLhs() != body.getArgument(0) || + value_ne.getRhs() != body.getArgument(0)) + return failure(); + } else { + mhlo::CompareOp value_gt = llvm::dyn_cast_or_null( + value_select.getOperand(0).getDefiningOp()); + if (!value_gt || value_gt.getComparisonDirection() != compare_direction || + value_gt.getLhs() != body.getArgument(0) || + value_gt.getRhs() != body.getArgument(2)) + return failure(); + } + + mhlo::SelectOp index_select = llvm::dyn_cast_or_null( + return_op.getOperand(1).getDefiningOp()); + if (!index_select || index_select.getOnTrue() != body.getArgument(1) || + index_select.getOnFalse() != body.getArgument(3)) + return failure(); + + mhlo::OrOp index_or = llvm::dyn_cast_or_null( + index_select.getPred().getDefiningOp()); + + if (!index_or || index_or.getLhs() != value_select.getPred()) + return failure(); + + mhlo::AndOp index_and = + llvm::dyn_cast_or_null(index_or.getRhs().getDefiningOp()); + if (!index_and) return failure(); + + mhlo::CompareOp value_eq = llvm::dyn_cast_or_null( + index_and.getLhs().getDefiningOp()); + if (!value_eq || + value_eq.getComparisonDirection() != mhlo::ComparisonDirection::EQ || + value_eq.getLhs() != body.getArgument(0) || + value_eq.getRhs() != body.getArgument(2)) + return failure(); + + mhlo::CompareOp index_lt = llvm::dyn_cast_or_null( + index_and.getRhs().getDefiningOp()); + if (!index_lt || + index_lt.getComparisonDirection() != mhlo::ComparisonDirection::LT || + index_lt.getLhs() != body.getArgument(1) || + index_lt.getRhs() != body.getArgument(3)) + return failure(); + + return success(); +} + +// Base class for converting mhlo::ReduceOp to TF/TFL ArgMax/ArgMin ops. +template +class ConvertReduceOpToArgMinMax : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + LogicalResult matchAndRewrite( + mhlo::ReduceOp reduce_op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final; + + virtual bool IsValueInitValue(const DenseElementsAttr& attr) const = 0; +}; + +template +LogicalResult ConvertReduceOpToArgMinMax< + Reduce, ArgReduce, BooleanReduce, + is_argmax>::matchAndRewrite(mhlo::ReduceOp reduce_op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const { + if (reduce_op.getInputs().size() != 2) return failure(); + if (reduce_op.getDimensions().getNumElements() != 1) return failure(); + + // Check that the operand init is the expected value. + DenseElementsAttr operand_init; + if (!matchPattern(reduce_op.getInitValues().front(), + m_Constant(&operand_init))) + return failure(); + if (!IsValueInitValue(operand_init)) return failure(); + + // Check that the iota init is zero. + DenseElementsAttr iota_init; + if (!matchPattern(reduce_op.getInitValues().back(), m_Constant(&iota_init))) + return failure(); + if (iota_init.getValues()[0] != 0) return failure(); + + // Verify that the second argument is an Iota op along the same dimension + // as the reduction. + Value iota = reduce_op.getInputs().back(); + if (!MatchIota(reduce_op.getDimensions(), iota)) return failure(); + + // Match the reduction computation. + const bool is_float = mlir::isa(operand_init.getElementType()); + if (failed(MatchReduceToArgMinMaxType1(reduce_op, is_float, is_argmax)) && + failed(MatchReduceToArgMinMaxType2(reduce_op, is_argmax))) + return rewriter.notifyMatchFailure( + reduce_op, "Unsupported Reduce -> ArgMax/ArgMin pattern"); + + Value operand = reduce_op.getInputs().front(); + int64_t axis = reduce_op.getDimensions().getValues()[0]; + + auto dim_type = RankedTensorType::get({1}, rewriter.getI32Type()); + auto reduction_indices = arith::ConstantOp::create( + rewriter, reduce_op.getLoc(), dim_type, + rewriter.getI32TensorAttr({static_cast(axis)})); + + // Generate a Max and an ArgMax of as the mhlo op returns both while in TF + // we have separate ops for them. If only one of them is used then the other + // one will be garbage collected later. + if (!mlir::isa(operand.getType())) return failure(); + auto operand_type = mlir::cast(operand.getType()); + if (operand_type.getElementType().isInteger(1)) { + // TF does not support min or max on boolean (int1) arguments. + // Use AnyOp for MaxOp and AllOp for MinOp. + auto tf_reduce_op = BooleanReduce::create( + rewriter, reduce_op.getLoc(), reduce_op->getResult(0).getType(), + operand, reduction_indices, + /*keep_dims=*/rewriter.getBoolAttr(false)); + auto tf_argreduce_op = ArgReduce::create(rewriter, reduce_op.getLoc(), + reduce_op->getResult(1).getType(), + operand, reduction_indices); + + rewriter.replaceOp(reduce_op, {tf_reduce_op, tf_argreduce_op}); + } else { + auto tf_reduce_op = Reduce::create( + rewriter, reduce_op.getLoc(), reduce_op->getResult(0).getType(), + operand, reduction_indices, + /*keep_dims=*/rewriter.getBoolAttr(false)); + + auto tf_argreduce_op = ArgReduce::create(rewriter, reduce_op.getLoc(), + reduce_op->getResult(1).getType(), + operand, reduction_indices); + + rewriter.replaceOp(reduce_op, {tf_reduce_op, tf_argreduce_op}); + } + return success(); +} + +// Base class for converting mhlo::ReduceOp to TF/TFL ArgMax/ArgMin ops. +template +class ConvertReduceOpToArgMax + : public ConvertReduceOpToArgMinMax { + public: + using ConvertReduceOpToArgMinMax::ConvertReduceOpToArgMinMax; + bool IsValueInitValue(const DenseElementsAttr& attr) const override; +}; + +template +bool ConvertReduceOpToArgMax:: + IsValueInitValue(const DenseElementsAttr& attr) const { + auto element_type = attr.getType().getElementType(); + if (attr.getNumElements() != 1 || !element_type.isIntOrFloat()) return false; + if (mlir::isa(element_type)) { + auto value = *attr.value_begin(); + return value.isNegative() && value.isInfinity(); + } else if (element_type.isInteger(1)) { + auto value = *attr.value_begin(); + return value.isZero(); + } else { + auto value = *attr.value_begin(); + return element_type.isUnsignedInteger() ? value.isMinValue() + : value.isMinSignedValue(); + } +} + +// Base class for converting mhlo::ReduceOp to TF/TFL ArgMax/ArgMin ops. +template +class ConvertReduceOpToArgMin + : public ConvertReduceOpToArgMinMax { + public: + using ConvertReduceOpToArgMinMax::ConvertReduceOpToArgMinMax; + bool IsValueInitValue(const DenseElementsAttr& attr) const override; +}; + +template +bool ConvertReduceOpToArgMin:: + IsValueInitValue(const DenseElementsAttr& attr) const { + auto element_type = attr.getType().getElementType(); + if (attr.getNumElements() != 1 || !element_type.isIntOrFloat()) return false; + if (mlir::isa(element_type)) { + auto value = *attr.value_begin(); + return !value.isNegative() && value.isInfinity(); + } else if (element_type.isInteger(1)) { + auto value = *attr.value_begin(); + return value.isZero(); + } else { + auto value = *attr.value_begin(); + return element_type.isUnsignedInteger() ? value.isMaxValue() + : value.isMaxSignedValue(); + } +} + +//===------------------------------------------------------------------------=== +// mhlo.reduce -> standard reductions +//===------------------------------------------------------------------------=== + +// If `value` is a splat constant, returns a success and set `splat_value` +// to the splate constant value. +// `SplatValueType` can be `APInt` or `APFloat`. +template +LogicalResult GetConstantSplatValue(Value value, SplatValueType& splat_value) { + DenseElementsAttr attr; + if (!matchPattern(value, m_Constant(&attr)) || !attr.isSplat()) { + return failure(); + } + + splat_value = attr.getSplatValue(); + return success(); +} + +// Replace BinaryOp with a combination of BinaryOp and ReduceOp if the +// init value doesn't match the expectation of ReduceOp. +template +LogicalResult rewriteNonMatchInitValue(mhlo::ReduceOp reduce_op, Value input, + arith::ConstantOp reduction_indices, + ConversionPatternRewriter& rewriter) { + Value reduce_result = + ReduceOp::create(rewriter, reduce_op.getLoc(), reduce_op.getType(0), + input, reduction_indices, + /*keep_dims=*/rewriter.getBoolAttr(false)); + + if constexpr (BuilderHasFAF) { + rewriter.replaceOpWithNewOp(reduce_op, reduce_result, + reduce_op.getInitValues()[0], + rewriter.getStringAttr("NONE")); + } else { + rewriter.replaceOpWithNewOp(reduce_op, reduce_result.getType(), + reduce_result, + reduce_op.getInitValues()[0]); + } + + return success(); +} + +DenseIntElementsAttr GetDimsAsI32Elements(OpBuilder& b, mhlo::ReduceOp op) { + auto dims_attr = op.getDimensions(); + const auto n_dims = dims_attr.getNumElements(); + + SmallVector reduce_dims; + reduce_dims.reserve(n_dims); + for (auto dim : dims_attr.getValues()) { + reduce_dims.push_back(dim); + } + + auto dim_type = RankedTensorType::get({n_dims}, b.getI32Type()); + return DenseIntElementsAttr::get(dim_type, reduce_dims); +} + +// Cannot replace BinaryOp if the init value doesn't match the expectation of +// ReduceOp and there is no corresponding BinaryOp. +template <> +LogicalResult rewriteNonMatchInitValue( + mhlo::ReduceOp reduce_op, Value input, arith::ConstantOp reduction_indices, + ConversionPatternRewriter& rewriter) { + return failure(); +} + +template <> +LogicalResult rewriteNonMatchInitValue( + mhlo::ReduceOp reduce_op, Value input, arith::ConstantOp reduction_indices, + ConversionPatternRewriter& rewriter) { + return failure(); +} + +template <> +LogicalResult rewriteNonMatchInitValue( + mhlo::ReduceOp reduce_op, Value input, arith::ConstantOp reduction_indices, + ConversionPatternRewriter& rewriter) { + return failure(); +} + +// Converts a mhlo.reduce op with a mlho binary operation into a tensorflow +// reduction operation. If the initial value can be ignored, then convert it +// into a single ReduceOp. Otherwise, convert it into a ReduceOp followed by +// a BinaryOp. +// For example: +// 1) A mhlo::ReduceOp on value `x` with a mhlo::AndOp and a constant initial +// value `true` is converted to a Any on value `x`. +// 2) A mhlo::ReduceOp on value `x` with a mhlo::AndOp with a non-constant +// initial value `y` is converted to a Any on value `x`, followed by a +// And with initial value `y`. +template +class ConvertReduce : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::ReduceOp reduce_op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final { + if (failed(MatchReduceOpOperand(reduce_op))) { + return failure(); + } + + if (failed(MatchBinaryReduceFunction(reduce_op.getBody()))) { + return failure(); + } + + auto operand = reduce_op.getInputs()[0]; + + // + // build reduction dims ConstOp from attr (cast to 32bit for tflite). + //=----- + + auto tfl_dims = GetDimsAsI32Elements(rewriter, reduce_op); + auto tfl_dims_op = + arith::ConstantOp::create(rewriter, reduce_op.getLoc(), tfl_dims); + + // + // replace with new reduce op, chaining binary op if needed. + //=----- + + if (succeeded(MatchInitValue(reduce_op.getInitValues()[0]))) { + rewriter.replaceOpWithNewOp( + reduce_op, reduce_op.getType(0), operand, tfl_dims_op, + /*keep_dim=*/rewriter.getBoolAttr(false)); + return success(); + } + return rewriteNonMatchInitValue(reduce_op, operand, + tfl_dims_op, rewriter); + } + + private: + // Checks that the init value matches with the init value expected for the + // target ReduceOp. + virtual LogicalResult MatchInitValue(Value init_value) const = 0; + + LogicalResult MatchReduceOpOperand(mhlo::ReduceOp reduce_op) const { + if (reduce_op.getInputs().size() != 1 || + reduce_op.getInitValues().size() != 1 || + reduce_op.getResults().size() != 1) + return failure(); + + if (!mlir::isa(reduce_op.getInputs()[0].getType())) + return failure(); + if (!mlir::isa(reduce_op.getType(0))) return failure(); + return success(); + } +}; + +class ConvertReduceMul + : public ConvertReduce { + public: + using ConvertReduce::ConvertReduce; + + LogicalResult MatchInitValue(Value init_value) const override { + auto type = mlir::cast(init_value.getType()).getElementType(); + if (mlir::isa(type)) { + float const_value; + if (failed(GetConstantSplatValue(init_value, const_value)) || + const_value != 1.0) + return failure(); + } else if (mlir::isa(type) && type.isSignlessInteger()) { + int32_t const_value; + if (failed(GetConstantSplatValue(init_value, const_value)) || + const_value != 1) + return failure(); + } else { + return failure(); + } + + return success(); + } +}; + +class ConvertReduceAdd + : public ConvertReduce { + public: + using ConvertReduce::ConvertReduce; + + LogicalResult MatchInitValue(Value init_value) const override { + auto type = mlir::cast(init_value.getType()).getElementType(); + if (mlir::isa(type)) { + APFloat const_value(.0); + if (failed(GetConstantSplatValue(init_value, const_value)) || + !const_value.isZero()) + return failure(); + } else if (mlir::isa(type) && type.isSignlessInteger()) { + APInt const_value; + if (failed(GetConstantSplatValue(init_value, const_value)) || + !const_value.isZero()) + return failure(); + } else { + return failure(); + } + + return success(); + } +}; + +class ConvertReduceMaxToReduceAny + : public ConvertReduce { + public: + using ConvertReduce::ConvertReduce; + + LogicalResult MatchInitValue(Value init_value) const override { + // This pattern is applicable only if the initial value is a boolean with + // False value. Only then will it make sense to convert a + // mhlo.reduce with mhlo.maximum reducer to a TFL.ReduceAnyOp. Because the + // maximum value across a slice of a tensor compared to False can be viewed + // as, checking if ANY value in the slice is True. + auto type = mlir::cast(init_value.getType()).getElementType(); + + if (!mlir::isa(type) || !type.isSignlessInteger() || + !(type.getIntOrFloatBitWidth() == 1)) + return failure(); + + APInt const_value; + if (failed(GetConstantSplatValue(init_value, const_value)) || + (const_value == 1)) + return failure(); + + return success(); + } +}; + +class ConvertReduceMax : public ConvertReduce { + public: + using ConvertReduce::ConvertReduce; + + LogicalResult MatchInitValue(Value init_value) const override { + auto type = mlir::cast(init_value.getType()).getElementType(); + if (mlir::isa(type)) { + APFloat const_value(.0); + if (failed(GetConstantSplatValue(init_value, const_value)) || + !const_value.isInfinity() || !const_value.isNegative()) + return failure(); + } else if (mlir::isa(type) && type.isSignlessInteger()) { + // Do not handle the case where the mhlo.reduce of mhlo.maximum can be + // legalized to TFL.ReduceAny. This can be possible if the dtype is i1 + if (type.getIntOrFloatBitWidth() == 1) return failure(); + APInt const_value; + if (failed(GetConstantSplatValue(init_value, const_value)) || + !const_value.isMinSignedValue()) + return failure(); + } else { + return failure(); + } + return success(); + } +}; + +class ConvertReduceMin : public ConvertReduce { + public: + using ConvertReduce::ConvertReduce; + + LogicalResult MatchInitValue(Value init_value) const override { + auto type = mlir::cast(init_value.getType()).getElementType(); + + if (mlir::isa(type)) { + APFloat const_value(.0); + if (failed(GetConstantSplatValue(init_value, const_value)) || + !const_value.isInfinity() || const_value.isNegative()) + return failure(); + } else if (mlir::isa(type) && type.isSignlessInteger()) { + APInt const_value; + if (failed(GetConstantSplatValue(init_value, const_value)) || + !const_value.isMaxSignedValue()) + return failure(); + } else { + return failure(); + } + return success(); + } +}; + +class ConvertReduceAnd + : public ConvertReduce { + public: + using ConvertReduce::ConvertReduce; + + LogicalResult MatchInitValue(Value init_value) const override { + DenseIntElementsAttr init_attr; + if (!matchPattern(init_value, m_Constant(&init_attr)) || + !init_attr.getType().getElementType().isInteger(1) || + !init_attr.isSplat() || !init_attr.getSplatValue().getValue()) + return failure(); + return success(); + } +}; + +class ConvertReduceOr + : public ConvertReduce { + public: + using ConvertReduce::ConvertReduce; + + LogicalResult MatchInitValue(Value init_value) const override { + DenseIntElementsAttr init_attr; + if (!matchPattern(init_value, m_Constant(&init_attr)) || + !init_attr.getType().getElementType().isInteger(1) || + !init_attr.isSplat() || init_attr.getSplatValue().getValue()) + return failure(); + return success(); + } +}; + +//===------------------------------------------------------------------------=== +// register patterns +//===------------------------------------------------------------------------=== + +// Returns false if the given reduce op can be legalized to ArgMax/ArgMin ops. +std::optional IsReduceOpLegal(mhlo::ReduceOp reduce_op) { + if (succeeded(MatchReduceToArgMinMaxType1(reduce_op, true, true)) || + succeeded(MatchReduceToArgMinMaxType1(reduce_op, false, true)) || + succeeded(MatchReduceToArgMinMaxType1(reduce_op, true, false)) || + succeeded(MatchReduceToArgMinMaxType1(reduce_op, false, false)) || + succeeded(MatchReduceToArgMinMaxType2(reduce_op, false)) || + succeeded(MatchReduceToArgMinMaxType2(reduce_op, true))) { + // If the ReduceOp matches to one of the patterns above, its illegal to + // have + // it in the model after the legalization is ran, because it should have + // been legalized to an ArgMax/ArgMin op. + return false; + } + return std::nullopt; +} + +template class ConvertReduceOpToArgMinMax; +template class ConvertReduceOpToArgMax; + +template class ConvertReduceOpToArgMinMax; +template class ConvertReduceOpToArgMin; + +template class ConvertReduceOpToArgMinMax; +template class ConvertReduceOpToArgMax; + +template class ConvertReduceOpToArgMinMax; +template class ConvertReduceOpToArgMin; + +void PopulateReduceArgMinMaxTFPatterns(MLIRContext* ctx, + RewritePatternSet& patterns) { + using ConvertReduceOpToTfArgmax = + ConvertReduceOpToArgMax; + + using ConvertReduceOpToTfArgmin = + ConvertReduceOpToArgMin; + + patterns.add(ctx); +} + +void PopulateReducePatterns(MLIRContext* ctx, RewritePatternSet& patterns, + ConversionTarget& target) { + using ConvertReduceOpToTFLiteArgmax = + ConvertReduceOpToArgMax; + + using ConvertReduceOpToTFLiteArgmin = + ConvertReduceOpToArgMin; + + patterns.add(ctx); + + target.addDynamicallyLegalOp(IsReduceOpLegal); +} + +} // namespace odml +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/reduce.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/reduce.h new file mode 100644 index 0000000000000000000000000000000000000000..1540f5103a7395a6469c86686fc4c5008d05d0d3 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/reduce.h @@ -0,0 +1,36 @@ +/* Copyright 2024 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_REDUCE_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_REDUCE_H_ + +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" // IWYU pragma: keep +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" // IWYU pragma: keep + +namespace mlir { +namespace odml { + +void PopulateReduceArgMinMaxTFPatterns(MLIRContext* ctx, + RewritePatternSet& patterns); + +void PopulateReducePatterns(MLIRContext* ctx, RewritePatternSet& patterns, + ConversionTarget& target); + +} // namespace odml +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_REDUCE_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/reduce_window.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/reduce_window.cc new file mode 100644 index 0000000000000000000000000000000000000000..b07e93ac73f685a3c81a0f8ef6ec9c8167bc0405 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/reduce_window.cc @@ -0,0 +1,800 @@ +/* Copyright 2024 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/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/reduce_window.h" + +#include +#include +#include +#include +#include + +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/Sequence.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/Support/Casting.h" +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/IRMapping.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/Matchers.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" // IWYU pragma: keep +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/op_util_common.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/reduce_window_util.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/util.h" +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir::odml { +namespace { + +// filters, strides, padding, faf. +using TFLPoolAttrsT = std::tuple; + +bool AreDilationsSupported(const ReduceWindowView& op) { + auto is_one = [](int64_t v) { return v == 1; }; + return llvm::all_of(op.BaseDilations(), is_one) && + llvm::all_of(op.WindowDilations(), is_one); +} + +bool IsRankSupported(const ReduceWindowView& op) { return op.Rank() == 4; } + +std::optional> GetViewIfAttrsSupported( + mhlo::ReduceWindowOp op) { + const ReduceWindowView view(op); + + if (!IsRankSupported(view)) { + return std::nullopt; + } + + if (!AreDilationsSupported(view)) { + return std::nullopt; + } + + auto opt_layout = view.GuessLayout(); + if (!opt_layout.has_value()) { + return std::nullopt; + } + auto layout = opt_layout.value(); + + const int64_t batch = layout.SpecialDim1(); + if (!view.Paddings()[batch].Trivial()) { + return std::nullopt; + } + + const int64_t chan = layout.SpecialDim2(); + if (!view.Paddings()[chan].Trivial()) { + return std::nullopt; + } + + return std::tuple(view, layout); +} + +std::optional IsReduceWindowLegal(mhlo::ReduceWindowOp op) { + return std::nullopt; +} + +std::optional IsDivideLegal(mhlo::DivOp op) { return std::nullopt; } + +Layout TFLNativePoolingLayout(int64_t rank) { + return Layout(0, rank - 1, llvm::to_vector(llvm::seq(1, rank - 1))); +} + +bool IsCstFloatZero(Value val) { + DenseFPElementsAttr initial_value; + return matchPattern(val, m_Constant(&initial_value)) && + initial_value.getNumElements() == 1 && + initial_value.getValues()[0].isZero(); +} + +bool IsCstIntZero(Value val) { + DenseIntElementsAttr initial_value; + return matchPattern(val, m_Constant(&initial_value)) && + initial_value.getNumElements() == 1 && + initial_value.getValues()[0].isZero(); +} + +llvm::SmallVector Permute(llvm::ArrayRef data, + llvm::ArrayRef perm) { + llvm::SmallVector res(data.size()); + for (int i = 0; i < data.size(); ++i) { + res[i] = data[perm[i]]; + } + return res; +} + +Value TransposeTensor(OpBuilder& b, Value tensor, + llvm::SmallVector perm) { + const int64_t perm_size = perm.size(); + auto perm_attr_type = RankedTensorType::get({perm_size}, b.getI64Type()); + auto perm_attr = DenseIntElementsAttr::get(perm_attr_type, perm); + return mhlo::TransposeOp::create(b, tensor.getLoc(), tensor, perm_attr); +} + +DenseIntElementsAttr BuildDenseI64(OpBuilder& b, ArrayRef shape, + ArrayRef data) { + return DenseIntElementsAttr::get(RankedTensorType::get(shape, b.getI64Type()), + data); +} + +DenseIntElementsAttr BuildDenseI64(OpBuilder& b, ArrayRef data) { + const int64_t dim = data.size(); + return BuildDenseI64(b, {dim}, data); +} + +std::optional> GetInputAndInitIfValid( + mhlo::ReduceWindowOp op) { + if (op->getNumResults() != 1) { + return std::nullopt; + } + if (op.getInputs().size() > 1) { + return std::nullopt; + } + if (op.getInitValues().size() > 1) { + return std::nullopt; + } + auto init_val = op.getInitValues().front(); + if (llvm::dyn_cast(init_val.getType()).getNumElements() != 1) { + return std::nullopt; + } + return std::tuple(op.getInputs().front(), op.getInitValues().front()); +} + +std::optional GetTFLPadding(ArrayRef paddings, + ArrayRef window_strides, + ArrayRef in_shape, + ArrayRef window_dims) { + const int64_t rank = paddings.size(); + std::string tfl_padding = "VALID"; + for (int i = 1; i < rank - 1; ++i) { + const auto& dim_pad = paddings[i]; + if (dim_pad.Trivial()) { + continue; + } + if (!IsSamePaddingOnDim(in_shape[i], 1, window_strides[i], window_dims[i], + dim_pad)) { + return std::nullopt; + } + tfl_padding = "SAME"; + } + return tfl_padding; +} + +TFLPoolAttrsT BuildTFLPoolAttrs(OpBuilder& b, const ReduceWindowView& view, + StringRef padding) { + const int32_t filter_h = view.WindowDims()[1]; + auto filter_h_attr = b.getI32IntegerAttr(filter_h); + + const int32_t filter_w = view.WindowDims()[2]; + auto filter_w_attr = b.getI32IntegerAttr(filter_w); + + const int32_t stride_h = view.WindowStrides()[1]; + auto stride_h_attr = b.getI32IntegerAttr(stride_h); + + const int32_t stride_w = view.WindowStrides()[2]; + auto stride_w_attr = b.getI32IntegerAttr(stride_w); + + auto padding_attr = b.getStringAttr(padding); + auto faf_attr = b.getStringAttr("NONE"); + + return std::tuple(filter_h_attr, filter_w_attr, stride_h_attr, stride_w_attr, + padding_attr, faf_attr); +} + +//===------------------------------------------------------------------------=== +// relayout reduce_window to channel last +//===------------------------------------------------------------------------=== + +class RelayoutReduceWindow : public OpRewritePattern { + public: + using OpRewritePattern::OpRewritePattern; + LogicalResult matchAndRewrite(mhlo::ReduceWindowOp op, + PatternRewriter& rewriter) const final; +}; + +LogicalResult RelayoutReduceWindow::matchAndRewrite( + mhlo::ReduceWindowOp op, PatternRewriter& rewriter) const { + // + // check and parse attributes + //=----- + + auto opt_view = GetViewIfAttrsSupported(op); + if (!opt_view.has_value()) { + return rewriter.notifyMatchFailure( + op, "Reduce window attributes not supported."); + } + const auto [view, layout] = opt_view.value(); + + // + // get inputs and inits if there are only one + //=----- + + auto opt_input_and_init = GetInputAndInitIfValid(op); + if (!opt_input_and_init.has_value()) { + return rewriter.notifyMatchFailure( + op, "Reduce window has wrong number of inputs or init values."); + } + auto [input, init_val] = opt_input_and_init.value(); + + // + // figure out permutations for layout change + //=----- + + const auto target_layout = TFLNativePoolingLayout(view.Rank()); + if (layout == target_layout) { + return rewriter.notifyMatchFailure( + op, "Reduce window does not need layout change"); + } + + llvm::SmallVector perm_for_inputs = + layout.GetPermForReLayout(target_layout); + + // + // permute layout sensitive attrs + //=----- + + // permute paddings + auto paddings = view.Paddings(); + llvm::SmallVector new_paddings(paddings.size() * 2); + for (int i = 0; i < new_paddings.size() / 2; ++i) { + const auto& dim_pad = paddings[perm_for_inputs[i]]; + new_paddings[2 * i] = dim_pad.Lo(); + new_paddings[2 * i + 1] = dim_pad.Hi(); + } + const int64_t new_paddings_size = paddings.size(); + auto new_paddings_type = + RankedTensorType::get({new_paddings_size, 2}, rewriter.getI64Type()); + auto new_paddings_attr = + DenseIntElementsAttr::get(new_paddings_type, new_paddings); + + // permute window dims + llvm::SmallVector new_window_dims = + Permute(view.WindowDims(), perm_for_inputs); + auto new_window_dims_attr = BuildDenseI64(rewriter, new_window_dims); + + // permute window strides + llvm::SmallVector new_window_strides = + Permute(view.WindowStrides(), perm_for_inputs); + auto new_window_strides_attr = BuildDenseI64(rewriter, new_window_strides); + + // + // permute params and build new op + //=----- + + // figure out permuted result type + llvm::SmallVector perm_for_outputs = + target_layout.GetPermForReLayout(layout); + auto cur_out_type = llvm::dyn_cast(op.getResult(0).getType()); + llvm::SmallVector new_rw_out_shape = + layout.PermuteShape(target_layout, cur_out_type.getShape()); + auto new_out_type = cur_out_type.clone(new_rw_out_shape); + + // transpose input and build new reduce_window + auto new_input = TransposeTensor(rewriter, input, perm_for_inputs); + auto new_rw = mhlo::ReduceWindowOp::create( + rewriter, op.getLoc(), new_out_type, new_input, init_val, + new_window_dims_attr, new_window_strides_attr, + BuildDenseI64(rewriter, view.BaseDilations()), + BuildDenseI64(rewriter, view.WindowDilations()), new_paddings_attr); + IRMapping ir_map; + op.getBody().cloneInto(&new_rw.getBody(), ir_map); + + // transpose output and update ir + auto new_output = + TransposeTensor(rewriter, new_rw.getResult(0), perm_for_outputs); + rewriter.replaceOp(op, new_output); + + return success(); +} + +//===------------------------------------------------------------------------=== +// mhlo.reduce_window -> tfl.cum_sum +//===------------------------------------------------------------------------=== + +class LegalizeCumSum : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::ReduceWindowOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final; +}; + +LogicalResult LegalizeCumSum::matchAndRewrite( + mhlo::ReduceWindowOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const { + // + // check singular params and trivial attrs + //=----- + + auto opt_input_init = GetInputAndInitIfValid(op); + if (!opt_input_init.has_value()) { + return rewriter.notifyMatchFailure(op, + "Must have 1 input, init and result."); + } + auto [input, init] = opt_input_init.value(); + + if (failed(MatchBinaryReduceFunction(op.getBody()))) { + return rewriter.notifyMatchFailure(op, "Requires scalar add in region."); + } + + if (!IsCstFloatZero(init) && !IsCstIntZero(init)) { + return rewriter.notifyMatchFailure(op, "Requires 0 for init value."); + } + + const ReduceWindowView view(op); + + auto trivial = [](int64_t v) { return v == 1; }; + const bool trivial_window_dilate = + llvm::all_of(view.WindowDilations(), trivial); + const bool trivial_base_dilate = llvm::all_of(view.BaseDilations(), trivial); + const bool trivial_stride = llvm::all_of(view.WindowStrides(), trivial); + if (!trivial_window_dilate || !trivial_stride || !trivial_base_dilate) { + return rewriter.notifyMatchFailure( + op, "Requires trivial strides and dilations attributes."); + } + + // + // figure out the implicit axis of reduction + //=----- + + auto input_type = llvm::cast(input.getType()); + if (view.WindowDims().size() != input_type.getRank()) { + return rewriter.notifyMatchFailure(op, "Splat window dims not supported."); + } + int64_t axis = -1; + for (auto [ind, val] : llvm::enumerate(view.WindowDims())) { + if (val == 1) { + continue; + } + + if (axis != -1) { + return rewriter.notifyMatchFailure(op, "Multiple non 1 dimensions."); + } + + if (val != input_type.getShape()[ind]) { + return rewriter.notifyMatchFailure( + op, "Axis dimension requires size be same as input shape's."); + } + axis = ind; + } + + if (axis == -1) { + return rewriter.notifyMatchFailure(op, "Could not identify axis."); + } + + const int64_t axis_size = input_type.getShape()[axis]; + + // + // validate padding is [N-1, 0] on axis and zero elsewhere + //=----- + + for (const auto& [ind, dim_pad] : llvm::enumerate(view.Paddings())) { + if (dim_pad.Hi() != 0) { + return rewriter.notifyMatchFailure(op, "Has non trivial high padding."); + } + + if (ind != axis) { + if (!dim_pad.Trivial()) { + return rewriter.notifyMatchFailure( + op, "Has non trivial padding on non axis dim."); + } + } else { + if (dim_pad.Lo() != axis_size - 1) { + return rewriter.notifyMatchFailure( + op, "Requires low padding on axis dim to be N - 1."); + } + } + } + + // + // build axis constant and tfl op + //=----- + + auto axis_cst_attr = DenseIntElementsAttr::get( + RankedTensorType::get({}, rewriter.getI32Type()), + static_cast(axis)); + auto axis_cst = + arith::ConstantOp::create(rewriter, op->getLoc(), axis_cst_attr); + + auto tfl_exclusive_attr = rewriter.getBoolAttr(false); + auto tfl_reverse_attr = rewriter.getBoolAttr(false); + + rewriter.replaceOpWithNewOp(op, op->getResultTypes()[0], input, + axis_cst, tfl_exclusive_attr, + tfl_reverse_attr); + + return success(); +} + +//===------------------------------------------------------------------------=== +// mhlo.reduce_window -> tfl.max_pool +//===------------------------------------------------------------------------=== + +bool isFloatMinusInfinity(Value value) { + DenseFPElementsAttr float_value; + if (!matchPattern(value, m_Constant(&float_value))) { + return false; + } + if (float_value.getNumElements() != 1) { + return false; + } + APFloat element = float_value.getValues()[0]; + return element.isInfinity() && element.isNegative(); +} + +class LegalizeMaxPool : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + LogicalResult matchAndRewrite( + mhlo::ReduceWindowOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final; + + private: + TFL::PadV2Op BuildExplicitPadOp(mhlo::ReduceWindowOp op, const Layout& layout, + const ShapedType& input_type, + const ShapedType& output_type, Value input, + Value init, const ReduceWindowView& view, + PatternRewriter& rewriter) const; +}; + +TFL::PadV2Op LegalizeMaxPool::BuildExplicitPadOp( + mhlo::ReduceWindowOp op, const Layout& layout, const ShapedType& input_type, + const ShapedType& output_type, Value input, Value init, + const ReduceWindowView& view, PatternRewriter& rewriter) const { + // The following works for rank=4 (see IsRankSupported()). + std::vector shape = {layout.Rank(), layout.NumSpatials()}; + + // Create attribute: padding_values + // For an NHWC with this padding: [[a, b], [c, d], [e, f], [g, h]] + // we want [a, b, c, d, e, f, g, h] + llvm::SmallVector padding_values; + for (auto& padding : view.Paddings()) { + padding_values.push_back(padding.Lo()); + padding_values.push_back(padding.Hi()); + } + + auto padding_dense_attr = mlir::DenseElementsAttr::get( + mlir::RankedTensorType::get(shape, rewriter.getIntegerType(64)), + llvm::ArrayRef(padding_values)); + + auto padding_values_op = + arith::ConstantOp::create(rewriter, op.getLoc(), padding_dense_attr); + + llvm::SmallVector pad_output_shape_vector; + pad_output_shape_vector.push_back(input_type.getDimSize(0)); + pad_output_shape_vector.push_back(input_type.getDimSize(1) + + view.Paddings()[1].Lo() + + view.Paddings()[1].Hi()); + pad_output_shape_vector.push_back(input_type.getDimSize(2) + + view.Paddings()[2].Lo() + + view.Paddings()[2].Hi()); + pad_output_shape_vector.push_back(input_type.getDimSize(3)); + auto pad_output_type = mlir::RankedTensorType::get( + pad_output_shape_vector, output_type.getElementType()); + return TFL::PadV2Op::create(rewriter, op.getLoc(), pad_output_type, input, + padding_values_op, init); +} + +LogicalResult LegalizeMaxPool::matchAndRewrite( + mhlo::ReduceWindowOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const { + // + // parse and validate lhs reduce window + //=----- + + const auto opt_view = GetViewIfAttrsSupported(op); + if (!opt_view.has_value()) { + return rewriter.notifyMatchFailure(op, "Reduce window is not valid."); + } + const auto [view, layout] = opt_view.value(); + if (layout != TFLNativePoolingLayout(layout.Rank())) { + return rewriter.notifyMatchFailure(op, "Not tfl standard layout."); + } + + // Check that the reduce-window is a max-reduce-window. + if (failed(MatchBinaryReduceFunction(op.getBody()))) { + return rewriter.notifyMatchFailure(op, "Must be a max pool."); + } + + auto type = mlir::dyn_cast(op.getResult(0).getType()); + if (!mlir::isa(type.getElementType())) { + return rewriter.notifyMatchFailure(op, "Not a floating point pool."); + } + + // + // validate inputs and init + //=----- + + auto opt_inputs_and_init = GetInputAndInitIfValid(op); + if (!opt_inputs_and_init.has_value()) { + return rewriter.notifyMatchFailure(op, "Too many inputs or inits."); + } + auto [input, init] = opt_inputs_and_init.value(); + auto input_type = llvm::dyn_cast(input.getType()); + + if (!isFloatMinusInfinity(init)) { + return rewriter.notifyMatchFailure(op, "Init not minus infinity."); + } + + // + // build tfl + //=----- + + auto opt_tfl_padding = + GetTFLPadding(view.Paddings(), view.WindowStrides(), + input_type.getShape(), view.WindowDims()); + + Value max_pool_input; + std::string tfl_padding_attr; + if (opt_tfl_padding.has_value()) { + max_pool_input = input; + tfl_padding_attr = opt_tfl_padding.value(); + } else { + max_pool_input = BuildExplicitPadOp(op, layout, input_type, type, input, + init, view, rewriter); + tfl_padding_attr = "VALID"; + } + + auto [fh, fw, sh, sw, p, faf] = + BuildTFLPoolAttrs(rewriter, view, tfl_padding_attr); + + rewriter.replaceOpWithNewOp(op, type, max_pool_input, p, sw, + sh, fw, fh, faf); + + return success(); +} + +//===------------------------------------------------------------------------=== +// mhlo.div(mhlo.reduce_window, cst | mhlo.reduce_window) -> tfl.avg_pool +//===------------------------------------------------------------------------=== + +void ReplaceWithAvgPool(mhlo::DivOp op, Value rw_lhs_input, + const ReduceWindowView& lhs_view, + llvm::StringRef padding, PatternRewriter& rewriter, + mhlo::TransposeOp opt_final_tpose) { + Type out_type = + opt_final_tpose ? opt_final_tpose.getOperand().getType() : op.getType(); + + auto [fh, fw, sh, sw, p, faf] = + BuildTFLPoolAttrs(rewriter, lhs_view, padding); + Value final_op = TFL::AveragePool2DOp::create( + rewriter, op->getLoc(), out_type, rw_lhs_input, fh, fw, p, sh, sw, faf); + + if (opt_final_tpose) { + final_op = mhlo::TransposeOp::create(rewriter, final_op.getLoc(), final_op, + opt_final_tpose.getPermutation()) + .getResult(); + } + + rewriter.replaceOp(op, final_op); +} + +// Walks up the op and ignore all precedding ops of type Tys. +// Returns the first producer op whose type is not in Tys. +template +Value RecursivelyWalkUp(Value op) { + while (llvm::isa_and_nonnull(op.getDefiningOp())) { + Operation* producer = op.getDefiningOp(); + op = producer->getOperand(/*idx=*/0); + } + + return op; +} + +class LegalizeAvgPool : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + explicit LegalizeAvgPool(MLIRContext* context) + : OpConversionPattern(context, 10) {} + LogicalResult matchAndRewrite( + mhlo::DivOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final; +}; + +LogicalResult LegalizeAvgPool::matchAndRewrite( + mhlo::DivOp div_op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const { + // + // parse and validate lhs reduce window + //=----- + + auto div_lhs = div_op.getLhs(); + // If div's input is transposed, save it to chain on the new pool op. + mhlo::TransposeOp opt_final_tpose; + if (auto div_lhs_op = div_lhs.getDefiningOp()) { + opt_final_tpose = llvm::dyn_cast_or_null(div_lhs_op); + } + + auto rw_lhs_val = RecursivelyWalkUp(div_lhs); + auto rw_lhs = + llvm::dyn_cast_or_null(rw_lhs_val.getDefiningOp()); + if (!rw_lhs) { + return rewriter.notifyMatchFailure( + div_op, "Could not match lhs of div on reduce window."); + } + + const auto opt_rw_lhs_view = GetViewIfAttrsSupported(rw_lhs); + if (!opt_rw_lhs_view.has_value()) { + return rewriter.notifyMatchFailure(div_op, "Lhs rw is not valid."); + } + const auto [rw_lhs_view, rw_lhs_layout] = opt_rw_lhs_view.value(); + if (rw_lhs_layout != TFLNativePoolingLayout(rw_lhs_layout.Rank())) { + return rewriter.notifyMatchFailure( + div_op, "Lhs reduce window not tfl standard layout."); + } + + // Check that the reduce-window is a sum-reduce-window. + if (failed(MatchBinaryReduceFunction(rw_lhs.getBody()))) { + return rewriter.notifyMatchFailure(div_op, + "Failed to match rw lhs binary func."); + } + + // + // validate inputs and init val + //=----- + + auto opt_rw_lhs_input_and_init = GetInputAndInitIfValid(rw_lhs); + if (!opt_rw_lhs_input_and_init.has_value()) { + return rewriter.notifyMatchFailure( + div_op, "Lhs reduce window has wrong number of inputs or init values."); + } + auto [rw_lhs_input, rw_lhs_init_val] = opt_rw_lhs_input_and_init.value(); + auto rw_lhs_input_type = llvm::dyn_cast(rw_lhs_input.getType()); + + auto rw_lhs_type = + mlir::dyn_cast(rw_lhs.getResult(0).getType()); + if (!mlir::isa(rw_lhs_type.getElementType())) { + return rewriter.notifyMatchFailure(div_op, + "Reduce window lhs most be float type."); + } + + // If the init value isn't zero then it can't be an average pool. + if (!IsCstFloatZero(rw_lhs_init_val)) { + return rewriter.notifyMatchFailure( + div_op, "Reduce window lhs init value is not zero."); + } + + // + // case 1: rhs is splat const with val == window_size + //=----- + + auto opt_tfl_padding = + GetTFLPadding(rw_lhs_view.Paddings(), rw_lhs_view.WindowStrides(), + rw_lhs_input_type.getShape(), rw_lhs_view.WindowDims()); + if (!opt_tfl_padding.has_value()) { + return rewriter.notifyMatchFailure(div_op, + "Padding must be VALID or SAME."); + } + const auto& tfl_padding = opt_tfl_padding.value(); + + { + DenseFPElementsAttr divisor; + auto div_rhs = RecursivelyWalkUp( + div_op.getRhs()); + if (matchPattern(div_rhs, m_Constant(&divisor))) { + if (!divisor.isSplat()) { + return failure(); + } + + if (!divisor.getSplatValue().isExactlyValue( + rw_lhs_view.WindowSize())) { + return rewriter.notifyMatchFailure( + div_op, "Rhs splat const is not equal to window size."); + } + + if (tfl_padding != "VALID") { + return rewriter.notifyMatchFailure(div_op, + "Matching on rhs splat const where " + "rw lhs has non-trivial padding."); + } + + ReplaceWithAvgPool(div_op, rw_lhs_input, rw_lhs_view, tfl_padding, + rewriter, opt_final_tpose); + return success(); + } + } + + // + // case 2: rhs is another reduce window over 1's with same config as lhs + //=----- + + { + Value divisor = RecursivelyWalkUp(div_op.getRhs()); + auto rw_rhs = + dyn_cast_or_null(divisor.getDefiningOp()); + if (!rw_rhs) { + return rewriter.notifyMatchFailure( + div_op, "Rhs of div op is not a reduce window."); + } + + const auto opt_rw_rhs_view = GetViewIfAttrsSupported(rw_rhs); + if (!opt_rw_rhs_view.has_value()) { + return rewriter.notifyMatchFailure(div_op, "Rhs rw is not valid."); + } + const auto [rw_rhs_view, rw_rhs_layout] = opt_rw_rhs_view.value(); + if (rw_rhs_layout != TFLNativePoolingLayout(rw_rhs_layout.Rank())) { + return rewriter.notifyMatchFailure( + div_op, "Rhs reduce window not tfl standard layout."); + } + + // Check that RHS is a sum-reduce-window. + if (failed(MatchBinaryReduceFunction(rw_rhs.getBody()))) { + return rewriter.notifyMatchFailure( + div_op, "Rhs rw body function is not an add op."); + } + + auto opt_rw_rhs_input_and_init = GetInputAndInitIfValid(rw_rhs); + if (!opt_rw_rhs_input_and_init.has_value()) { + return rewriter.notifyMatchFailure( + div_op, + "Rhs reduce window has wrong number of inputs or init values."); + } + auto [rw_rhs_input, rw_rhs_init_val] = opt_rw_rhs_input_and_init.value(); + + if (!IsCstFloatZero(rw_rhs_init_val)) { + return rewriter.notifyMatchFailure(div_op, + "Rhs rw init vals is not zero."); + } + + rw_rhs_input = RecursivelyWalkUp( + rw_rhs_input); + DenseFPElementsAttr rhs_input_data; + if (!matchPattern(rw_rhs_input, m_Constant(&rhs_input_data)) || + !rhs_input_data.isSplat() || + !rhs_input_data.getSplatValue().isExactlyValue(1.0)) { + return rewriter.notifyMatchFailure(div_op, + "Rw rhs input is not splat of 1.0."); + } + + // Check that the two reduce window have the same window configuration. + if (rw_lhs.getWindowDimensions() != rw_rhs.getWindowDimensions() || + rw_lhs.getWindowStrides() != rw_rhs.getWindowStrides() || + rw_lhs.getPadding() != rw_rhs.getPadding()) { + return rewriter.notifyMatchFailure( + div_op, "Lhs rw and Rhs rw do not have the same config."); + } + + ReplaceWithAvgPool(div_op, rw_lhs_input, rw_lhs_view, tfl_padding, rewriter, + opt_final_tpose); + return success(); + } + + return failure(); +} + +} // namespace + +void PopulateLegalizeReduceWindowPatterns(MLIRContext* ctx, + RewritePatternSet& patterns, + ConversionTarget& target) { + patterns.add(ctx); + target.addDynamicallyLegalOp(IsReduceWindowLegal); + target.addDynamicallyLegalOp(IsDivideLegal); +} + +void PopulatePrepareReduceWindowPatterns(MLIRContext* ctx, + RewritePatternSet& patterns) { + patterns.add(ctx); +} + +} // namespace mlir::odml diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/reduce_window.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/reduce_window.h new file mode 100644 index 0000000000000000000000000000000000000000..cd2ac29a04669697a075b0e74162dc705859c55d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/reduce_window.h @@ -0,0 +1,45 @@ +/* Copyright 2024 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_REDUCE_WINDOW_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_REDUCE_WINDOW_H_ + +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project + +namespace mlir::odml { + +// Patterns to legalize mhlo.reduce_window to TFL. +// +// Maps the following representations of AvgPool in MHLO into a tfl.avg_pool +// operation when they cleanly map to 2D or 3D average pool with VALID or SAME +// padding: +// * div(reduce_sum_window(x), constant(sizeof(window))) +// * div(reduce_sum_window(x), reduce_sum_window(constant(1))) +// +// Emits: tfl.average_pool2d +void PopulateLegalizeReduceWindowPatterns(MLIRContext* ctx, + RewritePatternSet& patterns, + ConversionTarget& target); + +// Patterns to prepare mhlo.reduce_window for legalization. +// Transposes reduce_windows to be NHWC. +// +// Emits: tfl.transpose +void PopulatePrepareReduceWindowPatterns(MLIRContext* ctx, + RewritePatternSet& patterns); + +} // namespace mlir::odml + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_REDUCE_WINDOW_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/reduce_window_util.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/reduce_window_util.cc new file mode 100644 index 0000000000000000000000000000000000000000..15a0ca5793418b57f2c24df095e42769cf3017a9 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/reduce_window_util.cc @@ -0,0 +1,72 @@ +/* Copyright 2024 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/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/reduce_window_util.h" + +#include +#include + +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/Sequence.h" +#include "llvm/ADT/SmallVector.h" +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/op_util_common.h" +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir::odml { + +ReduceWindowView::ReduceWindowView(mhlo::ReduceWindowOp op) { + rank_ = op.getWindowDimensions().size(); + window_dims_ = + SmallVector(op.getWindowDimensions().getValues()); + window_strides_ = ResolveStridesOrDilations(rank_, op.getWindowStrides()); + window_dilations_ = ResolveStridesOrDilations(rank_, op.getWindowDilations()); + base_dilations_ = ResolveStridesOrDilations(rank_, op.getBaseDilations()); + paddings_ = ResolvePadding(rank_, op.getPadding()); + window_size_ = 1; + for (auto d : window_dims_) { + window_size_ *= d; + } +} + +std::optional ReduceWindowView::GuessLayout() const { + auto zip_dims_strides = llvm::zip(WindowDims(), WindowStrides()); + auto simple_window_dims = + llvm::to_vector(llvm::map_range(zip_dims_strides, [](auto it) { + return std::get<0>(it) == 1 && std::get<1>(it) == 1; + })); + + if (llvm::count(simple_window_dims, 1) < 2) { + return std::nullopt; + } + + const bool is_channel_last = + simple_window_dims[0] && simple_window_dims[Rank() - 1]; + if (is_channel_last) { + return Layout(0, Rank() - 1, + llvm::to_vector(llvm::seq(1, Rank() - 1))); + } + + const bool is_channel_first = simple_window_dims[0] && simple_window_dims[1]; + if (is_channel_first) { + return Layout(0, 1, llvm::to_vector(llvm::seq(2, Rank()))); + } + + // In theory, we can support any layout with at least 2 1's in + // `simple_window_dims` by permuting layouts such that the 1's are + // the first and last position. Unclear if such a case ever comes up. + return std::nullopt; +} + +} // namespace mlir::odml diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/reduce_window_util.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/reduce_window_util.h new file mode 100644 index 0000000000000000000000000000000000000000..0cdbda8e81899b5f43e41b27aca6f8604ca3ec54 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/reduce_window_util.h @@ -0,0 +1,63 @@ +/* Copyright 2024 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_REDUCE_WINDOW_UTIL_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_REDUCE_WINDOW_UTIL_H_ + +#include +#include + +#include "llvm/ADT/ArrayRef.h" +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/Types.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/op_util_common.h" +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +// Helpers for working with mhlo.reduce_window attrs in the mlir api as +// native cc types. + +namespace mlir::odml { + +class ReduceWindowView { + public: + explicit ReduceWindowView(mhlo::ReduceWindowOp op); + + llvm::ArrayRef WindowDims() const { return window_dims_; } + int64_t WindowSize() const { return window_size_; } + llvm::ArrayRef WindowStrides() const { return window_strides_; } + llvm::ArrayRef Paddings() const { return paddings_; } + llvm::ArrayRef WindowDilations() const { return window_dilations_; } + llvm::ArrayRef BaseDilations() const { return base_dilations_; } + int64_t Rank() const { return rank_; } + + std::optional GuessLayout() const; + + private: + int64_t rank_; + + llvm::SmallVector window_dims_; + llvm::SmallVector window_strides_; + llvm::SmallVector window_dilations_; + + llvm::SmallVector paddings_; + + llvm::SmallVector base_dilations_; + + int64_t window_size_; +}; + +} // namespace mlir::odml + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_REDUCE_WINDOW_UTIL_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/scatter.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/scatter.cc new file mode 100644 index 0000000000000000000000000000000000000000..2ae7712c9b84718d1db177ca0847b65108f40c95 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/scatter.cc @@ -0,0 +1,216 @@ +/* Copyright 2023 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/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/scatter.h" + +#include +#include + +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/Sequence.h" +#include "llvm/ADT/SmallVector.h" +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/ImplicitLocOpBuilder.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/ValueRange.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/util.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir { +namespace odml { + +LogicalResult CanonicalizeScatterUpdates( + Operation* scatter_op, llvm::ArrayRef update_window_dims, + const Value& indices, const ShapedType& indices_type, Value& updates, + ShapedType& updates_type, ConversionPatternRewriter& rewriter) { + auto canonical_update_window_dims = llvm::to_vector( + llvm::seq(indices_type.getRank() - 1, updates_type.getRank())); + + if (canonical_update_window_dims == update_window_dims) return success(); + + // Permute updates if `update_window_dims` are leading indices. + // Other possibilities for `update_window_dims` are not supported yet. + if (!IsIotaAttr(update_window_dims, update_window_dims.size())) + return rewriter.notifyMatchFailure( + scatter_op, "update_window_dims are not leading or trailing indices"); + + SmallVector permutation_array(updates_type.getRank()); + int64_t dim = 0; + // Move leading indices to the back of the array. + const auto permutation_array_size = permutation_array.size(); + for (int64_t i = update_window_dims.size(); i < permutation_array_size; ++i) { + permutation_array[i] = dim; + ++dim; + } + // Move trailing indices to the front of the array. + for (int64_t i = 0; i < update_window_dims.size(); ++i) { + permutation_array[i] = dim; + ++dim; + } + + auto permutation_and_shape = GetPermutationAndTransposedShape( + permutation_array, updates_type, rewriter); + + auto transposed_updates = mhlo::TransposeOp::create( + rewriter, scatter_op->getLoc(), permutation_and_shape.shape, updates, + permutation_and_shape.permutation); + + updates = transposed_updates; + updates_type = permutation_and_shape.shape; + return success(); +} + +template +LogicalResult ConvertScatterOp::matchAndRewrite( + mhlo::ScatterOp scatter_op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const { + OperandRange operands = scatter_op.getInputs(); + Value indices = scatter_op.getScatterIndices(); + OperandRange updates = scatter_op.getUpdates(); + if (operands.size() != 1 || updates.size() != 1) return failure(); + + ShapedType operand_type = mlir::cast(operands[0].getType()); + ShapedType indices_type = mlir::cast(indices.getType()); + ShapedType updates_type = mlir::cast(updates[0].getType()); + + Value new_updates = updates[0]; + + // Can only convert with static shaped scatter. + if (!operand_type.hasStaticShape() || !indices_type.hasStaticShape() || + !updates_type.hasStaticShape()) { + return failure(); + } + + // Match the scatter computation against computations supported by TF. + if (failed(MatchBinaryReduceFunction( + scatter_op.getUpdateComputation()))) { + return failure(); + } + + auto scatter_dimension_numbers = scatter_op.getScatterDimensionNumbers(); + + // Normalize indices so index_vector_dim == indices.rank() - 1. + int64_t index_vector_dim = scatter_dimension_numbers.getIndexVectorDim(); + if (failed(NormalizeIndexVector(scatter_op, indices, indices_type, + index_vector_dim, rewriter))) { + return failure(); + } + + // Transform updates so that update window dims are the trailing + // dimensions in the update tensor. + auto update_window_dims = scatter_dimension_numbers.getUpdateWindowDims(); + if (failed(CanonicalizeScatterUpdates(scatter_op, update_window_dims, indices, + indices_type, new_updates, updates_type, + rewriter))) { + return failure(); + } + + auto inserted_window_dims = scatter_dimension_numbers.getInsertedWindowDims(); + auto scatter_dims_to_operand_dims = + scatter_dimension_numbers.getScatterDimsToOperandDims(); + + if (IsIotaAttr(inserted_window_dims, indices_type.getShape().back()) && + IsIotaAttr(scatter_dims_to_operand_dims, + indices_type.getShape().back())) { + rewriter.replaceOpWithNewOp(scatter_op, + scatter_op.getResult(0).getType(), + operands[0], indices, new_updates); + return success(); + } + // Insert tranposes to support scatter operations generated from + // numpy-like slice operations: + // nd_array[:, [i,j]] = [i_values, j_values] + // + if (scatter_dims_to_operand_dims != inserted_window_dims) { + // Support only dimension numbers generated by numpy-like slice + // operations. + return rewriter.notifyMatchFailure( + scatter_op, "unsupported scatter_dims_to_operand_dims"); + } + + // Transpose the operand and so that the trailing dimensions of the + // operand are being updated. Then apply a tf.scatter op and transpose + // back the result to get the same shape as the original operand. + + SmallVector permutation_array; + for (int64_t i = 0; i < scatter_dims_to_operand_dims.size(); ++i) { + permutation_array.push_back(scatter_dims_to_operand_dims[i]); + } + for (int64_t i = 0; i < operand_type.getRank(); ++i) { + if (!llvm::is_contained(scatter_dims_to_operand_dims, i)) { + permutation_array.push_back(i); + } + } + auto permutation_and_shape = GetPermutationAndTransposedShape( + permutation_array, operand_type, rewriter); + + Location loc = scatter_op.getLoc(); + auto transposed_operand = + mhlo::TransposeOp::create(rewriter, loc, permutation_and_shape.shape, + operands[0], permutation_and_shape.permutation); + + Value new_indices = indices; + int64_t index_depth = + permutation_and_shape.shape.getRank() - inserted_window_dims.size(); + int64_t num_updates = indices_type.getDimSize(0); + // For TF::TensorScatterUpdateOp, `indices` must have at least 2 axes: + // `(num_updates, index_depth)`. Reshape indices and updates if necessary. + if (std::is_same::value && + indices_type.getRank() == 1 && updates_type.getRank() == 1 && + index_depth == 1 && num_updates == 1) { + ImplicitLocOpBuilder builder(loc, rewriter); + auto indices_shape = BuildIntArrayConstOp( + builder, rewriter, + llvm::SmallVector({num_updates, index_depth}), + rewriter.getI32Type()); + new_indices = TF::ReshapeOp::create( + rewriter, loc, + RankedTensorType::get({num_updates, index_depth}, + indices_type.getElementType()), + indices, indices_shape); + auto updates_shape = BuildIntArrayConstOp( + builder, rewriter, + llvm::SmallVector({num_updates, updates_type.getDimSize(0)}), + rewriter.getI32Type()); + new_updates = TF::ReshapeOp::create( + rewriter, loc, + RankedTensorType::get({1, updates_type.getDimSize(0)}, + updates_type.getElementType()), + new_updates, updates_shape); + } + + // Apply TF scatter to update the trailing dimensions of the + // transposed operand. + auto tf_scatter_op = + TfOp::create(rewriter, loc, permutation_and_shape.shape, + transposed_operand, new_indices, new_updates); + + // Reverse the earlier transpose. + auto inverse_permutation = GetInversePermutation(permutation_array, rewriter); + rewriter.replaceOpWithNewOp( + scatter_op, scatter_op.getResult(0).getType(), tf_scatter_op, + inverse_permutation); + + return success(); +} + +} // namespace odml +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/scatter.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/scatter.h new file mode 100644 index 0000000000000000000000000000000000000000..cc9395db8d5632eb683635435979cf75c2dad6ec --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/scatter.h @@ -0,0 +1,109 @@ +/* Copyright 2023 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_SCATTER_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_SCATTER_H_ + +#include + +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/SmallVector.h" +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir { +namespace odml { + +// Convert updates into canonical form as expected by tf.scatter ops. +// +// tf.scatter expects `update_window_dims` to be the trailing dimensions. +// +// To support scatter ops generated by numpy-like slice updates: +// nd_array[:, [i,j]] = [i_values, j_values] +// +// `updates` must be transposed when the update_window_dims are the leading +// dimensions of `updates`. +// +// Other values of `update_window_dims` are left unsupported. +// +// Eg 1. An update in canonical form: +// * indices shape(A,B,C) +// * updates shape(A,B,D,E,F) +// Then: +// * D,E,F are the update window dims [2,3,4] +// * C is the index vector dimension +// * A,B iterate over the updates and indices +// +// If `update_window_dims` are not the trailing dimensions then updates must be +// transposed. +// +// Eg 2. An update in non-canonical form: +// * indices shape(a,b,c) +// * updates shape(d,e,f,a,b) +// Then: +// * d,e,f are the update window dims [0,1,2] +// * c is the index vector dimension +// * a,b iterate over the updates and indices +// +// The update needs permuting to be in the form (a,b,d,e,f) so that the update +// window dims are the trailing dimensions. +// +// To canonicalize the updates above, replace the updates with: +// transpose(updates, permutation={3,4,0,1,2}) +// +// Note: NormalizeIndexVector is assumed to have run on the indices already so +// that the index_vector_dim is the trailing dimension in `indices`. +LogicalResult CanonicalizeScatterUpdates( + Operation* scatter_op, llvm::ArrayRef update_window_dims, + const Value& indices, const ShapedType& indices_type, Value& updates, + ShapedType& updates_type, ConversionPatternRewriter& rewriter); + +template +class ConvertScatterOp : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::ScatterOp scatter_op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final; +}; + +using ConvertScatterAddOp = + ConvertScatterOp; +using ConvertScatterMaxOp = + ConvertScatterOp; +using ConvertScatterMinOp = + ConvertScatterOp; +using ConvertScatterSubOp = + ConvertScatterOp; +using ConvertScatterUpdateOp = + ConvertScatterOp; + +template class ConvertScatterOp; +template class ConvertScatterOp; +template class ConvertScatterOp; +template class ConvertScatterOp; +template class ConvertScatterOp; + +} // end namespace odml +} // end namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_SCATTER_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/slice.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/slice.cc new file mode 100644 index 0000000000000000000000000000000000000000..167f1e4d6fe2b411a48ab9fb07deb087bc8d7a09 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/slice.cc @@ -0,0 +1,429 @@ +/* Copyright 2024 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/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/slice.h" + +#include + +#include "llvm/ADT/STLExtras.h" +#include "llvm/Support/Casting.h" +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/Matchers.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/IR/ValueRange.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" // IWYU pragma: keep +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir::odml { +namespace { + +// mhlo encodes ND indice arguments as a variadiac of scalars. Pack them +// into a single tensor for use in TFL. +Value PackScalarIndices(mlir::ValueRange indices, OpBuilder& b) { + auto e_type = + llvm::cast(indices.front().getType()).getElementType(); + const int64_t num_indices = indices.size(); + auto packed_indices_type = RankedTensorType::get({num_indices}, e_type); + + auto values_count_attr = b.getI32IntegerAttr(num_indices); + auto pack_axis_attr = b.getI32IntegerAttr(0); + + return TFL::PackOp::create(b, indices.back().getLoc(), packed_indices_type, + indices, values_count_attr, pack_axis_attr); +} + +//===----------------------------------------------------------------------===// +// mhlo.slice +//===----------------------------------------------------------------------===// + +// Cast the value to i32. +Value BuildTFLCastOp(OpBuilder& b, Value value) { + auto type = mlir::cast(value.getType()); + if (type.getElementType().isInteger(32)) { + return value; + } + return TFL::CastOp::create( + b, value.getLoc(), RankedTensorType::get(type.getShape(), b.getI32Type()), + value); +} + +class LegalizeSliceOp : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::SliceOp slice_op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final { + auto input = adaptor.getOperand(); + + auto convert_to_i32 = [&](DenseIntElementsAttr attr) { + return attr.mapValues(rewriter.getI32Type(), [](const APInt& val) { + return val.sextOrTrunc(32); + }); + }; + + auto begin_const = + arith::ConstantOp::create(rewriter, slice_op.getLoc(), + convert_to_i32(slice_op.getStartIndices())); + auto limit_const = + arith::ConstantOp::create(rewriter, slice_op.getLoc(), + convert_to_i32(slice_op.getLimitIndices())); + auto strides_const = arith::ConstantOp::create( + rewriter, slice_op.getLoc(), convert_to_i32(slice_op.getStrides())); + + auto zero = rewriter.getIntegerAttr(rewriter.getI32Type(), 0); + auto no_offset = rewriter.getBoolAttr(false); + + rewriter.replaceOpWithNewOp( + slice_op, slice_op.getType(), input, begin_const, limit_const, + strides_const, zero, zero, zero, zero, zero, no_offset); + + return success(); + } +}; + +class CollapseStridedSliceRank : public OpRewritePattern { + public: + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(TFL::StridedSliceOp op, + PatternRewriter& rewriter) const final { + auto to_i32 = [](ArrayRef values) { + return llvm::map_to_vector( + values, [](int64_t v) { return static_cast(v); }); + }; + + auto input = op.getInput(); + auto input_type = mlir::cast(input.getType()); + if (!input_type.hasStaticShape() || input_type.getRank() <= 5) { + return rewriter.notifyMatchFailure(op, "Rank <= 5 or dynamic shape."); + } + + // Only handle simple strided slices (no masks). + if (op.getBeginMask() != 0 || op.getEndMask() != 0 || + op.getEllipsisMask() != 0 || op.getNewAxisMask() != 0 || + op.getShrinkAxisMask() != 0) { + return rewriter.notifyMatchFailure(op, "Has masks."); + } + + DenseIntElementsAttr start_indices_attr, limit_indices_attr, strides_attr; + if (!matchPattern(op.getBegin(), m_Constant(&start_indices_attr)) || + !matchPattern(op.getEnd(), m_Constant(&limit_indices_attr)) || + !matchPattern(op.getStrides(), m_Constant(&strides_attr))) { + return rewriter.notifyMatchFailure(op, "Indices are not constant."); + } + + SmallVector start_indices( + llvm::map_range(start_indices_attr.getValues(), + [](const APInt& val) { return val.getSExtValue(); })); + SmallVector limit_indices( + llvm::map_range(limit_indices_attr.getValues(), + [](const APInt& val) { return val.getSExtValue(); })); + SmallVector strides( + llvm::map_range(strides_attr.getValues(), + [](const APInt& val) { return val.getSExtValue(); })); + SmallVector input_shape(input_type.getShape().begin(), + input_type.getShape().end()); + + // Collapse dimensions if rank > 5. + while (input_shape.size() > 5) { + bool merged = false; + for (int i = 0; i < static_cast(input_shape.size()) - 1; ++i) { + // This merging condition ensures the inner dimension is fully covered + // by the slice with stride 1, making it safe to collapse. + if (strides[i] == 1 && strides[i + 1] == 1 && + start_indices[i + 1] == 0 && + limit_indices[i + 1] == input_shape[i + 1]) { + // Merge i and i+1 + start_indices[i] = start_indices[i] * input_shape[i + 1]; + limit_indices[i] = limit_indices[i] * input_shape[i + 1]; + input_shape[i] = input_shape[i] * input_shape[i + 1]; + + start_indices.erase(start_indices.begin() + i + 1); + limit_indices.erase(limit_indices.begin() + i + 1); + input_shape.erase(input_shape.begin() + i + 1); + strides.erase(strides.begin() + i + 1); + merged = true; + break; + } + } + if (!merged) break; + } + + if (input_shape.size() > 5) { + return rewriter.notifyMatchFailure( + op, "Input rank > 5 and could not be collapsed."); + } + + auto collapsed_type = + RankedTensorType::get(input_shape, input_type.getElementType()); + Value collapsed_input = TFL::ReshapeOp::create( + rewriter, op.getLoc(), collapsed_type, input, + arith::ConstantOp::create( + rewriter, op.getLoc(), + rewriter.getI32TensorAttr(to_i32(input_shape)))); + + auto begin_const = arith::ConstantOp::create( + rewriter, op.getLoc(), + rewriter.getI32TensorAttr(to_i32(start_indices))); + auto limit_const = arith::ConstantOp::create( + rewriter, op.getLoc(), + rewriter.getI32TensorAttr(to_i32(limit_indices))); + auto strides_const = arith::ConstantOp::create( + rewriter, op.getLoc(), rewriter.getI32TensorAttr(to_i32(strides))); + + auto zero = rewriter.getIntegerAttr(rewriter.getI32Type(), 0); + auto no_offset = rewriter.getBoolAttr(false); + + SmallVector intermediate_result_shape; + for (int i = 0; i < static_cast(input_shape.size()); ++i) { + intermediate_result_shape.push_back( + (limit_indices[i] - start_indices[i] + strides[i] - 1) / strides[i]); + } + auto intermediate_type = RankedTensorType::get(intermediate_result_shape, + input_type.getElementType()); + + Value slice_res = TFL::StridedSliceOp::create( + rewriter, op.getLoc(), intermediate_type, collapsed_input, begin_const, + limit_const, strides_const, zero, zero, zero, zero, zero, no_offset); + + auto result_type = mlir::cast(op.getType()); + auto reshape_op = TFL::ReshapeOp::create( + rewriter, op.getLoc(), op.getType(), slice_res, + arith::ConstantOp::create( + rewriter, op.getLoc(), + rewriter.getI32TensorAttr(to_i32(result_type.getShape())))); + rewriter.replaceOp(op, reshape_op); + + return success(); + } +}; + +//===----------------------------------------------------------------------===// +// mhlo.dynamic_slice +//===----------------------------------------------------------------------===// + +class CastSliceIndicesToSignless + : public OpRewritePattern { + public: + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(mhlo::DynamicSliceOp op, + PatternRewriter& rewriter) const final; +}; + +LogicalResult CastSliceIndicesToSignless::matchAndRewrite( + mhlo::DynamicSliceOp op, PatternRewriter& rewriter) const { + // All start inds have the same element type. + auto start_type = + llvm::cast(op.getStartIndices().front().getType()); + auto start_e_type = start_type.getElementType(); + + if (start_e_type.isSignlessIntOrFloat()) { + return rewriter.notifyMatchFailure(op, "Already signless."); + } + auto new_start_e_type = + rewriter.getIntegerType(start_e_type.getIntOrFloatBitWidth()); + + llvm::SmallVector casted_start_inds; + for (auto start_ind_opr : op.getStartIndices()) { + auto casted_start_ind_opr = mhlo::ConvertOp::create( + rewriter, start_ind_opr.getLoc(), start_ind_opr, new_start_e_type); + casted_start_inds.push_back(casted_start_ind_opr.getResult()); + } + + auto new_op = mhlo::DynamicSliceOp::create( + rewriter, op.getLoc(), op.getType(), op.getOperand(), casted_start_inds, + op.getSliceSizes()); + rewriter.replaceOp(op, new_op); + + return success(); +} + +bool IsDynamicSliceLegal(mhlo::DynamicSliceOp op) { + return !llvm::cast(op.getOperand().getType()).hasStaticShape(); +} + +class LegalizeDynamicSliceOp + : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::DynamicSliceOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final; +}; + +LogicalResult LegalizeDynamicSliceOp::matchAndRewrite( + mhlo::DynamicSliceOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const { + auto start_type = + llvm::cast(op.getStartIndices().front().getType()); + auto start_e_type = start_type.getElementType(); + if (!start_e_type.isSignlessIntOrFloat()) { + return rewriter.notifyMatchFailure( + op, "Must be signless integer for start indices."); + } + + auto input_type = llvm::cast(op.getOperand().getType()); + if (!input_type.hasStaticShape()) { + return rewriter.notifyMatchFailure(op, "Input must be statically shaped."); + } + + // + // clamp start indices between zero and shape(operand) - slice_sizes + //=----- + + Value clamp_left_cst = arith::ConstantOp::create( + rewriter, op->getLoc(), rewriter.getZeroAttr(start_type)); + + llvm::SmallVector new_start_indices; + + for (auto [dim_size, start_ind_opr, stride_size] : + llvm::zip(input_type.getShape(), op.getStartIndices(), + mlir::cast(op.getSliceSizes()) + .getValues())) { + const int64_t clamp_right_val = dim_size - stride_size; + auto clamp_right_cst = arith::ConstantOp::create( + rewriter, op->getLoc(), + DenseElementsAttr::get(start_type, rewriter.getIntegerAttr( + start_e_type, clamp_right_val))); + + Value new_start_ind = TFL::MaximumOp::create( + rewriter, op->getLoc(), start_type, clamp_left_cst, start_ind_opr); + new_start_ind = TFL::MinimumOp::create(rewriter, op->getLoc(), start_type, + clamp_right_cst, new_start_ind); + + new_start_indices.push_back(new_start_ind); + } + + // + // build tfl + //=----- + + auto packed_indices = PackScalarIndices(new_start_indices, rewriter); + + auto slice_sizes_cst = + arith::ConstantOp::create(rewriter, op->getLoc(), op.getSliceSizes()); + + auto slice_op = + TFL::SliceOp::create(rewriter, op.getLoc(), op.getType(), op.getOperand(), + packed_indices, slice_sizes_cst); + rewriter.replaceOp(op, slice_op); + + return success(); +} + +//===----------------------------------------------------------------------===// +// mhlo.real_dynamic_slice +//===----------------------------------------------------------------------===// + +class LegalizeRealDynamicSliceOp + : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::RealDynamicSliceOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final; +}; + +LogicalResult LegalizeRealDynamicSliceOp::matchAndRewrite( + mhlo::RealDynamicSliceOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const { + auto start_indices_type = + mlir::cast(op.getStartIndices().getType()); + auto end_indices_type = + mlir::cast(op.getLimitIndices().getType()); + + if (start_indices_type.getNumDynamicDims() != 0 || + end_indices_type.getNumDynamicDims() != 0) { + return rewriter.notifyMatchFailure( + op, + "Start indices and limit indices must not have dynamic dimensions."); + } + + auto zero = rewriter.getIntegerAttr(rewriter.getI32Type(), 0); + auto no_offset = rewriter.getBoolAttr(false); + + auto ss_op = TFL::StridedSliceOp::create( + rewriter, op.getLoc(), op.getType(), op.getOperand(), + BuildTFLCastOp(rewriter, op.getStartIndices()), + BuildTFLCastOp(rewriter, op.getLimitIndices()), + BuildTFLCastOp(rewriter, op.getStrides()), zero, zero, zero, zero, zero, + no_offset); + rewriter.replaceOp(op, ss_op); + return success(); +}; + +//===----------------------------------------------------------------------===// +// mhlo.dynamic_update_slice +//===----------------------------------------------------------------------===// + +class LegalizeDynamicUpdateSliceOp + : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::DynamicUpdateSliceOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final; +}; + +LogicalResult LegalizeDynamicUpdateSliceOp::matchAndRewrite( + mhlo::DynamicUpdateSliceOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const { + auto packed_indices = PackScalarIndices(op.getStartIndices(), rewriter); + auto dus_op = TFL::DynamicUpdateSliceOp::create( + rewriter, op.getLoc(), op.getType(), op.getOperand(), op.getUpdate(), + packed_indices); + rewriter.replaceOp(op, dus_op); + return success(); +}; + +} // namespace + +void PopulateLegalizeSlicePatterns(MLIRContext* ctx, + RewritePatternSet& patterns, + ConversionTarget& target) { + patterns.add(ctx); + + target.addIllegalOp(); + target.addDynamicallyLegalOp(IsDynamicSliceLegal); + // This dynamic legality check is crucial. It correctly marks + // `tfl.strided_slice` operations with rank > 5 as illegal, allowing the + // `CollapseStridedSliceRank` pattern to kick in and perform the necessary + // reshaping. + target.addDynamicallyLegalOp([](TFL::StridedSliceOp op) { + auto input_type = mlir::cast(op.getInput().getType()); + return input_type.getRank() <= 5; + }); +} + +void PopulatePrepareSlicePatterns(MLIRContext* ctx, + RewritePatternSet& patterns) { + patterns.add(ctx); +} + +} // namespace mlir::odml diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/slice.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/slice.h new file mode 100644 index 0000000000000000000000000000000000000000..d3920f2debe441348a52dfb11727e3a439237e37 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/slice.h @@ -0,0 +1,34 @@ +/* Copyright 2024 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_SLICE_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_SLICE_H_ + +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir::odml { + +// Patterns to legalize mhlo.slice to TFL. +void PopulateLegalizeSlicePatterns(MLIRContext* ctx, + RewritePatternSet& patterns, + ConversionTarget& target); + +void PopulatePrepareSlicePatterns(MLIRContext* ctx, + RewritePatternSet& patterns); + +} // namespace mlir::odml + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_SLICE_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/sort.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/sort.cc new file mode 100644 index 0000000000000000000000000000000000000000..7ce1fc1fefb9bd9edb1557c3ca061af29beff5a2 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/sort.cc @@ -0,0 +1,146 @@ +/* Copyright 2024 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/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/sort.h" + +#include + +#include "llvm/ADT/ilist.h" +#include "llvm/Support/Casting.h" +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/IR/Block.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/Region.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/hlo_matchers.h" +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir::odml { +namespace { + +using OpListType = llvm::iplist; + +template +bool MatchTopKComparator(Region& comparator) { + if (!comparator.hasOneBlock()) return false; + Block& comparator_blk = comparator.front(); + + OpListType& operations = comparator_blk.getOperations(); + if (operations.size() != 2) return false; + + auto compare_op = + llvm::dyn_cast_or_null(&operations.front()); + auto return_op = llvm::dyn_cast_or_null(&operations.back()); + if (!compare_op || !return_op) return false; + + if (compare_op.getComparisonDirection() != mhlo::ComparisonDirection::GT) { + return false; + } + + if (compare_op.getOperands()[0] != comparator_blk.getArgument(0) || + compare_op.getOperands()[1] != comparator_blk.getArgument(1)) { + return false; + } + + return return_op.getOperands().front() == compare_op.getResult(); +} + +bool IsSortOpNotTopK(mhlo::SortOp op) { + if (op->getNumOperands() != 2) { + return true; + } + + auto keys_opr = op.getInputs().front(); + auto keys_type = llvm::cast(keys_opr.getType()); + + if (!keys_type.hasStaticShape() || + !keys_type.getElementType().isIntOrFloat()) { + return true; + } + + auto indices_opr = op.getInputs().back(); + auto indices_type = llvm::cast(indices_opr.getType()); + + if (!indices_type.hasStaticShape() || + !indices_type.getElementType().isInteger(32)) { + return true; + } + + const int64_t sort_dim = op.getDimension(); + const auto k = indices_type.getDimSize(sort_dim); + const auto rank = keys_type.getRank(); + + if (sort_dim != rank - 1 || k < 1) { + return true; + } + + OpBuilder b(op->getContext()); + if (!MatchIota(b.getI64TensorAttr({sort_dim}), indices_opr)) { + return true; + } + + if (!MatchTopKComparator(op.getComparator())) { + return true; + } + + return false; +} + +class LegalizeSortOp : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::SortOp sort_op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final; +}; + +LogicalResult LegalizeSortOp::matchAndRewrite( + mhlo::SortOp op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const { + if (IsSortOpNotTopK(op)) { + return failure(); + } + + auto keys = op.getInputs().front(); + auto indices = op.getInputs().back(); + auto indices_type = llvm::cast(indices.getType()); + + const int32_t k = indices_type.getShape().back(); + auto k_cst_attr = DenseIntElementsAttr::get( + RankedTensorType::get({}, rewriter.getI32Type()), k); + auto k_cst = arith::ConstantOp::create(rewriter, op->getLoc(), k_cst_attr); + + rewriter.replaceOpWithNewOp(op, keys.getType(), + indices.getType(), keys, k_cst); + + return success(); +} + +} // namespace + +void PopulateSortPatterns(MLIRContext* ctx, RewritePatternSet& patterns, + ConversionTarget& target) { + patterns.add(ctx); + target.addDynamicallyLegalOp(IsSortOpNotTopK); +} + +} // namespace mlir::odml diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/sort.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/sort.h new file mode 100644 index 0000000000000000000000000000000000000000..3ac68c841da19bacbcf1101ac3b61141933c8f0a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/sort.h @@ -0,0 +1,29 @@ +/* Copyright 2024 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_SORT_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_SORT_H_ + +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project + +namespace mlir::odml { + +void PopulateSortPatterns(MLIRContext* ctx, RewritePatternSet& patterns, + ConversionTarget& target); + +} // namespace mlir::odml + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_SORT_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/util.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/util.cc new file mode 100644 index 0000000000000000000000000000000000000000..f98048313a2f7cd9ecadda10f71ebc9a15dd0668 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/util.cc @@ -0,0 +1,228 @@ +/* Copyright 2023 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/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/util.h" + +#include +#include +#include + +#include "llvm/ADT/Sequence.h" +#include "llvm/ADT/SmallVector.h" +#include "mlir/IR/Block.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/ImplicitLocOpBuilder.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/Types.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir { +namespace odml { + +bool IsIotaAttr(ArrayRef arr, int64_t size, int64_t start) { + if (arr.size() != size) return false; + int64_t iota = start; + for (auto s : arr) { + if (s != iota) return false; + ++iota; + } + return true; +} + +PermutationAndShape GetPermutationAndTransposedShape( + llvm::ArrayRef permutation_array, ShapedType input_type, + ConversionPatternRewriter& rewriter) { + assert(permutation_array.size() == input_type.getRank()); + llvm::SmallVector transposed_shape(permutation_array.size()); + for (int64_t i = 0; i < permutation_array.size(); ++i) { + transposed_shape[i] = input_type.getDimSize(permutation_array[i]); + } + auto transposed_type = + RankedTensorType::get(transposed_shape, input_type.getElementType()); + DenseIntElementsAttr permutation = DenseIntElementsAttr::get( + RankedTensorType::get(permutation_array.size(), rewriter.getI64Type()), + permutation_array); + return {permutation, transposed_type}; +} + +Value BuildIntConstOp(ImplicitLocOpBuilder& builder, + ConversionPatternRewriter& rewriter, int64_t const_value, + Type type) { + Value result_const = + TF::ConstOp::create(builder, rewriter.getIntegerAttr(type, const_value)); + return result_const; +} + +llvm::SmallVector GetInversePermutationArray( + llvm::ArrayRef permutation_array) { + llvm::SmallVector inverse_permutation_array( + permutation_array.size()); + const auto permutation_array_size = permutation_array.size(); + for (int64_t i = 0; i < permutation_array_size; ++i) { + inverse_permutation_array[permutation_array[i]] = i; + } + return inverse_permutation_array; +} + +DenseIntElementsAttr GetInversePermutation( + llvm::ArrayRef permutation_array, + ConversionPatternRewriter& rewriter) { + SmallVector inverse_permutation_array = + GetInversePermutationArray(permutation_array); + return DenseIntElementsAttr::get( + RankedTensorType::get(inverse_permutation_array.size(), + rewriter.getI64Type()), + inverse_permutation_array); +} + +PermutationAndShape GetInversePermutationAndShape( + llvm::ArrayRef permutation_array, ShapedType input_type, + ConversionPatternRewriter& rewriter) { + SmallVector inverse_permutation_array = + GetInversePermutationArray(permutation_array); + return GetPermutationAndTransposedShape(inverse_permutation_array, input_type, + rewriter); +} + +LogicalResult NormalizeIndexVector(Operation* parent_op, Value& indices, + ShapedType& indices_type, + int64_t index_vector_dim, + ConversionPatternRewriter& rewriter) { + if (index_vector_dim == indices_type.getRank()) { + llvm::SmallVector new_start_indices_shape( + indices_type.getShape().begin(), indices_type.getShape().end()); + new_start_indices_shape.push_back(1); + indices_type = RankedTensorType::get(new_start_indices_shape, + indices_type.getElementType()); + indices = mhlo::ReshapeOp::create(rewriter, parent_op->getLoc(), + indices_type, indices); + } else if (index_vector_dim != indices_type.getRank() - 1) { + // If index_vector_dim isn't the last dimension in indices then it isn't + // supported yet. + // TODO(tberghammer): Transpose indices to support this usecase. + return rewriter.notifyMatchFailure( + parent_op, + "index vector dim isn't the last dimension in start indices"); + } + return success(); +} + +// Check if the specified region is a binary reduction function that takes 2 +// inputs and returns the second input. Functions like this are used by update +// scatter like ops. +template <> +LogicalResult MatchBinaryReduceFunction(mlir::Region& function) { + Block& body = function.front(); + if (body.getNumArguments() != 2) return failure(); + + mhlo::ReturnOp return_op = dyn_cast(body.back()); + if (!return_op) return failure(); + if (return_op.getNumOperands() != 1) return failure(); + if (return_op.getOperands().front() != body.getArgument(1)) return failure(); + return success(); +} + +bool NeedsReformatTypeAndPermutation(int batch_dim, int feature_dim, + int spatial_dim_start, + int default_batch_dim, + int default_feature_dim, + int default_spatial_dim_start) { + return batch_dim != default_batch_dim || feature_dim != default_feature_dim || + spatial_dim_start != default_spatial_dim_start; +} + +std::pair GetReformatTypeAndPermutation( + int batch_dim, int feature_dim, int spatial_dim_start, + int default_batch_dim, int default_feature_dim, + int default_spatial_dim_start, int num_spatial_dims, RankedTensorType type, + ConversionPatternRewriter& rewriter) { + auto shape = type.getShape(); + llvm::SmallVector permutation_array(num_spatial_dims + 2); + permutation_array[default_batch_dim] = batch_dim; + permutation_array[default_feature_dim] = feature_dim; + llvm::SmallVector transposed_shape(num_spatial_dims + 2); + transposed_shape[default_batch_dim] = shape[batch_dim]; + transposed_shape[default_feature_dim] = shape[feature_dim]; + for (int i : llvm::seq(0, num_spatial_dims)) { + permutation_array[default_spatial_dim_start + i] = spatial_dim_start + i; + transposed_shape[default_spatial_dim_start + i] = + shape[spatial_dim_start + i]; + } + auto new_type = + RankedTensorType::get(transposed_shape, type.getElementType()); + auto permutation = DenseIntElementsAttr::get( + RankedTensorType::get({type.getRank()}, rewriter.getI64Type()), + permutation_array); + return {new_type, permutation}; +} + +Value InsertTranspose(Value value, int batch_dim, int feature_dim, + ArrayRef spatial_dimensions, + int default_batch_dim, int default_feature_dim, + int default_spatial_dim_start, int num_spatial_dims, + ConversionPatternRewriter& rewriter) { + auto type = mlir::cast(value.getType()); + DenseIntElementsAttr permutation; + const int spatial_dim_start = spatial_dimensions.front(); + if (!NeedsReformatTypeAndPermutation( + batch_dim, feature_dim, spatial_dim_start, default_batch_dim, + default_feature_dim, default_spatial_dim_start)) { + // Transpose is not needed because the current format is the same a default + // format. + return value; + } + std::pair(type, permutation) = + GetReformatTypeAndPermutation(batch_dim, feature_dim, spatial_dim_start, + default_batch_dim, default_feature_dim, + default_spatial_dim_start, num_spatial_dims, + type, rewriter); + return mhlo::TransposeOp::create(rewriter, value.getLoc(), type, value, + permutation); +} + +Value CreateCastToInt32(Value val, Location loc, PatternRewriter& rewriter) { + IntegerType new_ele_type = rewriter.getIntegerType(32); + if (auto shaped_type = mlir::dyn_cast(val.getType())) { + ShapedType new_type = + RankedTensorType::get(shaped_type.getShape(), new_ele_type); + return TFL::CastOp::create(rewriter, loc, new_type, val); + } + return TFL::CastOp::create(rewriter, loc, + UnrankedTensorType::get(new_ele_type), val); +} + +// Replaces `region`'s terminator to TFL::Yield. +void ReplaceTerminatorWithYield(Region& region, PatternRewriter& rewriter) { + OpBuilder::InsertionGuard guard(rewriter); + + for (auto& block : region.getBlocks()) { + auto* terminator = block.getTerminator(); + rewriter.setInsertionPoint(terminator); + rewriter.replaceOpWithNewOp(terminator, + terminator->getOperands()); + } +} + +} // namespace odml +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/util.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/util.h new file mode 100644 index 0000000000000000000000000000000000000000..d1f16c64f47efa3c5361d26ef9facb63619f3de8 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/util.h @@ -0,0 +1,168 @@ +/* Copyright 2023 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_UTIL_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_UTIL_H_ + +#include +#include + +#include "llvm/ADT/SmallVector.h" +#include "mlir/IR/Block.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/ImplicitLocOpBuilder.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/Region.h" // from @llvm-project +#include "mlir/IR/Types.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir { +namespace odml { + +struct PermutationAndShape { + DenseIntElementsAttr permutation; + ShapedType shape; +}; + +// Check that `arr` is an R1 iota with integer element type starting from +// `start` with `size` number of values. +bool IsIotaAttr(ArrayRef arr, int64_t size, int64_t start = 0); + +// Returns a DenseIntElementsAttr for a permutation and the shape after +// applying the permutation to a given shape through a transpose. +PermutationAndShape GetPermutationAndTransposedShape( + llvm::ArrayRef permutation_array, ShapedType input_type, + ConversionPatternRewriter& rewriter); + +// Create a single const integer. +Value BuildIntConstOp(ImplicitLocOpBuilder& builder, + ConversionPatternRewriter& rewriter, int64_t const_value, + Type type); + +// Create a const integer vector tensor (1-dim). +template +Value BuildIntArrayConstOp(ImplicitLocOpBuilder& builder, + ConversionPatternRewriter& rewriter, + ArrayRef const_value, Type type) { + DenseIntElementsAttr const_value_raw; + if (type == rewriter.getI64Type()) { + const_value_raw = rewriter.getI64TensorAttr(const_value); + } else { + // Convert I64 const array to I32. + llvm::SmallVector const_i32_vec; + for (auto element : const_value) { + const_i32_vec.push_back(static_cast(element)); + } + const_value_raw = rewriter.getI32TensorAttr(const_i32_vec); + } + Value result_const = ConstOpT::create(builder, const_value_raw); + return result_const; +} + +// Returns the inverse permutation array for a permutation array. +llvm::SmallVector GetInversePermutationArray( + llvm::ArrayRef permutation_array); + +// Returns the DenseIntElementsAttr for an inverse permutation given a +// permutation_array. +DenseIntElementsAttr GetInversePermutation( + llvm::ArrayRef permutation_array, + ConversionPatternRewriter& rewriter); + +// Returns a DenseIntElementsAttr for an inverse permutation and the shape after +// applying the inverse permutation to a given shape through a transpose. +PermutationAndShape GetInversePermutationAndShape( + llvm::ArrayRef permutation_array, ShapedType input_type, + ConversionPatternRewriter& rewriter); + +// Returns true if the op needs reformat. +bool NeedsReformatTypeAndPermutation(int batch_dim, int feature_dim, + int spatial_dim_start, + int default_batch_dim, + int default_feature_dim, + int default_spatial_dim_start); + +// Gets reformat type and permutation attribute. Call this function only if +// NeedsReformatTypeAndPermutation returns true. If +// NeedsReformatTypeAndPermutation returns false, this function returns the pair +// of input type and no-op permutation. + +std::pair GetReformatTypeAndPermutation( + int batch_dim, int feature_dim, int spatial_dim_start, + int default_batch_dim, int default_feature_dim, + int default_spatial_dim_start, int num_spatial_dims, RankedTensorType type, + ConversionPatternRewriter& rewriter); + +// Insert transpose so the input value is converted to the format specified by +// the default dims +Value InsertTranspose(Value value, int batch_dim, int feature_dim, + ArrayRef spatial_dimensions, + int default_batch_dim, int default_feature_dim, + int default_spatial_dim_start, int num_spatial_dims, + ConversionPatternRewriter& rewriter); + +// If index_vector_dim == indices.rank() then insert the implicit extra +// dimension into indices to normalize everything to index_vector_dim == +// indices.rank() - 1. +LogicalResult NormalizeIndexVector(Operation* parent_op, Value& indices, + ShapedType& indices_type, + int64_t index_vector_dim, + ConversionPatternRewriter& rewriter); + +// Checks if the specified region is a binary reduction function that takes 2 +// inputs, passes it to an instance of the specified reduction op and then +// returns the result. +template +LogicalResult MatchBinaryReduceFunction(mlir::Region& function) { + Block& body = function.front(); + if (body.getNumArguments() != 2) return failure(); + + mhlo::ReturnOp return_op = dyn_cast(body.back()); + if (!return_op) return failure(); + if (return_op.getNumOperands() != 1) return failure(); + + ReductionOp reduce_op = dyn_cast_or_null( + return_op.getOperands().front().getDefiningOp()); + if (!reduce_op) return failure(); + if (reduce_op.getLhs() != body.getArgument(0) || + reduce_op.getRhs() != body.getArgument(1)) + return failure(); + + return success(); +} + +// Check if the specified region is a binary reduction function that takes 2 +// inputs and returns the second input. Functions like this are used by update +// scatter like ops. +template <> +LogicalResult MatchBinaryReduceFunction(mlir::Region& function); + +// Util that casts 'val' to Int32 by adding a tfl cast Op. +Value CreateCastToInt32(Value val, Location loc, PatternRewriter& rewriter); + +// Replaces `region`'s terminator to TFL::Yield. +void ReplaceTerminatorWithYield(Region& region, PatternRewriter& rewriter); +} // namespace odml +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_UTIL_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/while.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/while.cc new file mode 100644 index 0000000000000000000000000000000000000000..c8949e6bc377a53d29b476a492f7079b921b8dfe --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/while.cc @@ -0,0 +1,82 @@ +/* Copyright 2024 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/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/while.h" + +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/Region.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" // IWYU pragma: keep +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir::odml { +namespace { + +// Replaces `region`'s terminator to TFL::Yield. +void TFLReplaceReturnOp(Region& region, PatternRewriter& rewriter) { + OpBuilder::InsertionGuard guard(rewriter); + + for (auto& block : region.getBlocks()) { + Operation* terminator = block.getTerminator(); + rewriter.setInsertionPoint(terminator); + rewriter.replaceOpWithNewOp(terminator, + terminator->getOperands()); + } +} + +class LeagalizeWhileOp : public OpConversionPattern { + public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult matchAndRewrite( + mhlo::WhileOp while_op, OpAdaptor adaptor, + ConversionPatternRewriter& rewriter) const final { + // Creates a TFL::WhileOp to replace the mhlo::WhileOp. HLO WhileOp + // currently doesn't support stateless, so this + // parameters are set to the default values. + auto is_stateless = rewriter.getBoolAttr(false); + auto new_while = TFL::WhileOp::create(rewriter, while_op.getLoc(), + while_op->getResultTypes(), + while_op->getOperands(), + /*is_stateless=*/is_stateless); + new_while.getCond().takeBody(while_op.getCond()); + new_while.getBody().takeBody(while_op.getBody()); + TFLReplaceReturnOp(new_while.getCond(), rewriter); + TFLReplaceReturnOp(new_while.getBody(), rewriter); + rewriter.replaceOp(while_op, new_while.getResults()); + return success(); + } +}; + +bool IsWhileLegal(mhlo::WhileOp while_op) { + for (auto type : while_op->getOperandTypes()) { + if (mlir::isa(type)) return true; + } + return false; +} + +} // namespace + +void PopulateWhilePatterns(MLIRContext* ctx, RewritePatternSet& patterns, + ConversionTarget& target) { + target.addDynamicallyLegalOp(IsWhileLegal); + patterns.add(ctx); +} + +} // namespace mlir::odml diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/while.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/while.h new file mode 100644 index 0000000000000000000000000000000000000000..edd418bc6ca2f01ea96154be9b69dccf6166b37d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/while.h @@ -0,0 +1,30 @@ +/* Copyright 2024 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_WHILE_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_WHILE_H_ + +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir::odml { + +void PopulateWhilePatterns(MLIRContext* ctx, RewritePatternSet& patterns, + ConversionTarget& target); + +} // namespace mlir::odml + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_HLO_CONVERSIONS_WHILE_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_patterns.td b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_patterns.td new file mode 100644 index 0000000000000000000000000000000000000000..2b7fd93edf3f331a4f5c14b02331d055bc30f662 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_patterns.td @@ -0,0 +1,767 @@ +/* Copyright 2023 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. +==============================================================================*/ + +// This is the legalization pattern definition file for HLO to TF. + +include "mlir/IR/OpBase.td" +include "mlir/Dialect/Func/IR/FuncOps.td" +include "stablehlo/dialect/ChloOps.td" +include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.td" +include "mhlo/IR/hlo_ops.td" + +// Check if broadcasting is compatible with TF ops. +def IsLegalNumpyRankedBroadcast : + Constraint{})">, + "broadcasting should be compatible with TF ops">; + +// Return a constant op that carries the shape of the given value. +def ShapeToConst : NativeCodeCall<"ShapeToConst($_builder, $0)">; + +// Check if broadcast dimensions match Tensorflow convention. +def IsTFStyleBroadcast : Constraint, + "new dimensions are added as prefix">; + +def IsNotWithinScatterRegion : Constraint($0)">, + "binary ops within scatter regions are not converted">; + +// Check if broadcast dimensions do not match Tensorflow convention. +def IsNotTFStyleBroadcast : Constraint>, + "new dimensions are inserted in intermediate positions">; + +// Return intermediate shape before broadcasting, wrapped in a constant op. +def ExpandedShape : NativeCodeCall<"ExpandedShape($_builder, $0, $1, $2)">; +def ExpandedDynamicShape : NativeCodeCall<"ExpandedDynamicShape($_builder, $0, $1, $2)">; + +def : Pat<(MHLO_ConstantOp:$output $value), (TF_ConstOp $value), + [(TF_Tensor $output)]>; + +//===----------------------------------------------------------------------===// +// Binary op patterns. +// Note that these are legalized from chlo.broadcast_* ops, since those are +// semantically compatible with the corresponding TF ops. Depending on +// context, getting to these ops may require some raising. +//===----------------------------------------------------------------------===// + +foreach fromToBinPair = [[MHLO_AddOp, CHLO_BroadcastAddOp, TF_AddV2Op], + [MHLO_DivOp, CHLO_BroadcastDivOp, TF_DivOp], + [MHLO_ShiftLeftOp, CHLO_BroadcastShiftLeftOp, TF_LeftShiftOp], + [MHLO_MaxOp, CHLO_BroadcastMaxOp, TF_MaximumOp], + [MHLO_MinOp, CHLO_BroadcastMinOp, TF_MinimumOp], + [MHLO_MulOp, CHLO_BroadcastMulOp, TF_MulOp], + [MHLO_PowOp, CHLO_BroadcastPowOp, TF_PowOp], + [MHLO_SubtractOp, CHLO_BroadcastSubOp, TF_SubOp], + [MHLO_Atan2Op, CHLO_BroadcastAtan2Op, TF_Atan2Op]] in { + def : Pat<(fromToBinPair[0]:$result $l, $r), + (fromToBinPair[2] $l, $r), + [(IsNotWithinScatterRegion $result)]>; + def : Pat<(fromToBinPair[1] $l, $r, $broadcast_dimensions), + (fromToBinPair[2] $l, $r), + [(IsLegalNumpyRankedBroadcast $l, $r, $broadcast_dimensions)]>; + def : Pat<(fromToBinPair[0] $l, + (MHLO_BroadcastInDimOp:$output + $bcast_operand, + $broadcast_dimensions)), + (fromToBinPair[2] $l, $bcast_operand), + [(IsTFStyleBroadcast $broadcast_dimensions, $output)]>; + def : Pat<(fromToBinPair[0] (MHLO_BroadcastInDimOp:$output + $bcast_operand, + $broadcast_dimensions), + $r), + (fromToBinPair[2] $bcast_operand, $r), + [(IsTFStyleBroadcast $broadcast_dimensions, $output)]>; +} + +foreach pair = [[MHLO_AndOp, CHLO_BroadcastAndOp, TF_BitwiseAndOp], + [MHLO_OrOp, CHLO_BroadcastOrOp, TF_BitwiseOrOp], + [MHLO_XorOp, CHLO_BroadcastXorOp, TF_BitwiseXorOp]] in { + def : Pat<(pair[0] TF_IntTensor:$l, TF_IntTensor:$r), (pair[2] $l, $r)>; + def : Pat<(pair[1] TF_IntTensor:$l, TF_IntTensor:$r, $broadcast_dimensions), + (pair[2] $l, $r), + [(IsLegalNumpyRankedBroadcast $l, $r, $broadcast_dimensions)]>; + def : Pat<(pair[0] (MHLO_BroadcastInDimOp:$output + TF_IntTensor:$bcast_operand, + $broadcast_dimensions), + TF_IntTensor:$r), + (pair[2] $bcast_operand, $r), + [(IsTFStyleBroadcast $broadcast_dimensions, $output)]>; + def : Pat<(pair[0] TF_IntTensor:$l, + (MHLO_BroadcastInDimOp:$output + TF_IntTensor:$bcast_operand, + $broadcast_dimensions)), + (pair[2] $l, $bcast_operand), + [(IsTFStyleBroadcast $broadcast_dimensions, $output)]>; +} + +foreach pair = [[MHLO_AndOp, CHLO_BroadcastAndOp, TF_LogicalAndOp], + [MHLO_OrOp, CHLO_BroadcastOrOp, TF_LogicalOrOp]] in { + def : Pat<(pair[0] I1Tensor:$l, I1Tensor:$r), (pair[2] $l, $r)>; + def : Pat<(pair[1] I1Tensor:$l, I1Tensor:$r, $broadcast_dimensions), + (pair[2] $l, $r), + [(IsLegalNumpyRankedBroadcast $l, $r, $broadcast_dimensions)]>; +} + +def : Pat<(MHLO_ShiftRightArithmeticOp $l, $r), (TF_RightShiftOp $l, $r)>; +def : Pat<(CHLO_BroadcastShiftRightArithmeticOp $l, $r, + $broadcast_dimensions), + (TF_RightShiftOp $l, $r), + [(IsLegalNumpyRankedBroadcast $l, $r, $broadcast_dimensions)]>; +def : Pat<(MHLO_ShiftRightLogicalOp $l, $r), (TF_RightShiftOp $l, $r)>; +def : Pat<(CHLO_BroadcastShiftRightLogicalOp $l, $r, + $broadcast_dimensions), + (TF_RightShiftOp $l, $r), + [(IsLegalNumpyRankedBroadcast $l, $r, $broadcast_dimensions)]>; + +def : Pat<(MHLO_FloorOp (MHLO_DivOp $l, $r)), (TF_FloorDivOp $l, $r)>; +def : Pat<(MHLO_FloorOp (CHLO_BroadcastDivOp $l, $r, + $broadcast_dimensions)), + (TF_FloorDivOp $l, $r), + [(IsLegalNumpyRankedBroadcast $l, $r, $broadcast_dimensions)]>; + +def : Pat<(MHLO_ComplexOp $r, $i), (TF_ComplexOp $r, $i)>; + +def : Pat<(MHLO_RemOp TF_FpOrI32OrI64Tensor:$l, TF_FpOrI32OrI64Tensor:$r), (TF_ModOp $l, $r)>; +def : Pat<(CHLO_BroadcastRemOp TF_FpOrI32OrI64Tensor:$l, TF_FpOrI32OrI64Tensor:$r, $broadcast_dimensions), + (TF_ModOp $l, $r), + [(IsLegalNumpyRankedBroadcast $l, $r, $broadcast_dimensions)]>; + +//===----------------------------------------------------------------------===// +// Unary op patterns. +//===----------------------------------------------------------------------===// + +def : Pat<(MHLO_ConvertOp MHLO_Tensor:$operand), + (TF_CastOp $operand, ConstBoolAttrFalse)>; + +foreach Mapping = [[MHLO_AbsOp, TF_AbsOp], + [MHLO_BitcastConvertOp, TF_BitcastOp], + [MHLO_CeilOp, TF_CeilOp], + [MHLO_FloorOp, TF_FloorOp], + [MHLO_ImagOp, TF_ImagOp], + [MHLO_IsFiniteOp, TF_IsFiniteOp], + [MHLO_NegOp, TF_NegOp], + [MHLO_RealOp, TF_RealOp], + [MHLO_SignOp, TF_SignOp]] in + def : Pat<(Mapping[0] TF_IntOrFpTensor:$input), (Mapping[1] $input)>; + +def ConstDefaultResultAccuracyAttr : + ConstantAttr; + +foreach Mapping = [[MHLO_CosineOp, TF_CosOp], + [MHLO_Expm1Op, TF_Expm1Op], + [MHLO_ExpOp, TF_ExpOp], + [MHLO_LogOp, TF_LogOp], + [MHLO_Log1pOp, TF_Log1pOp], + [MHLO_LogisticOp, TF_SigmoidOp], + [MHLO_RsqrtOp, TF_RsqrtOp], + [MHLO_SineOp, TF_SinOp], + [MHLO_SqrtOp, TF_SqrtOp], + [MHLO_TanhOp, TF_TanhOp]] in { + def : Pat<(Mapping[0] $input, ConstDefaultResultAccuracyAttr), + (Mapping[1] MHLO_Tensor:$input)>; +} + +class GetRankedScalarAttr : + NativeCodeCall<"DenseElementsAttr::get<" # prefix # "int" # width # "_t>(" + "RankedTensorType::get({}, $_builder.getIntegerType(" + # width # signed # "))," # value # ")">; + +def : Pat<(MHLO_NotOp TF_Int8Tensor:$input), + (TF_BitwiseXorOp $input, + (TF_ConstOp (GetRankedScalarAttr<"", 8, "", "-1">)))>; +def : Pat<(MHLO_NotOp TF_Int16Tensor:$input), + (TF_BitwiseXorOp $input, + (TF_ConstOp (GetRankedScalarAttr<"", 16, "", "-1">)))>; +def : Pat<(MHLO_NotOp TF_Int32Tensor:$input), + (TF_BitwiseXorOp $input, + (TF_ConstOp (GetRankedScalarAttr<"", 32, "", "-1">)))>; +def : Pat<(MHLO_NotOp TF_Int64Tensor:$input), + (TF_BitwiseXorOp $input, + (TF_ConstOp (GetRankedScalarAttr<"", 64, "", "-1">)))>; + +def : Pat<(MHLO_NotOp TF_Uint8Tensor:$input), + (TF_BitwiseXorOp $input, + (TF_ConstOp (GetRankedScalarAttr<"u", 8, ", false", "0xFFU">)))>; +def : Pat<(MHLO_NotOp TF_Uint16Tensor:$input), + (TF_BitwiseXorOp $input, + (TF_ConstOp (GetRankedScalarAttr<"u", 16, ", false", + "0xFFFFU">)))>; +def : Pat<(MHLO_NotOp TF_Uint32Tensor:$input), + (TF_BitwiseXorOp $input, + (TF_ConstOp (GetRankedScalarAttr<"u", 32, ", false", + "0xFFFFFFFFUL">)))>; +def : Pat<(MHLO_NotOp TF_Uint64Tensor:$input), + (TF_BitwiseXorOp $input, + (TF_ConstOp (GetRankedScalarAttr<"u", 64, ", false", + "0xFFFFFFFFFFFFFFFFULL">)))>; + +def : Pat<(MHLO_NotOp TF_BoolTensor:$input), (TF_LogicalNotOp $input)>; +def : Pat<(MHLO_AbsOp TF_ComplexTensor:$arg), (TF_ComplexAbsOp $arg)>; + +def : Pat<(MHLO_BroadcastOp $arg, $shape), + (TF_BroadcastToOp $arg, (TF_ConstOp $shape))>; +def : Pat<(MHLO_BroadcastInDimOp:$output $input, $broadcast_dimensions), + (TF_BroadcastToOp $input, (ShapeToConst $output)), + [(IsTFStyleBroadcast $broadcast_dimensions, $output)]>; +def : Pat<(MHLO_BroadcastInDimOp:$output $input, $broadcast_dimensions), + (TF_BroadcastToOp + (TF_ReshapeOp + $input, + (ExpandedShape $input, $broadcast_dimensions, $output)), + (ShapeToConst $output)), + [(IsNotTFStyleBroadcast $broadcast_dimensions, $output)]>; +// Dynamism op +def : Pat<(MHLO_DynamicBroadcastInDimOp:$output $input, $output_dimensions, + $broadcast_dimensions, $expanding_dimensions_unused, $nonexpanding_dimensions_unused), + (TF_BroadcastToOp $input, $output_dimensions), + [(IsTFStyleBroadcast $broadcast_dimensions, $output)]>; +def : Pat<(MHLO_DynamicBroadcastInDimOp:$output $input, $output_dimensions, + $broadcast_dimensions, $expanding_dimensions_unused, $nonexpanding_dimensions_unused), + (TF_BroadcastToOp (ExpandedDynamicShape $input, $broadcast_dimensions, $output), $output_dimensions), + [(IsNotTFStyleBroadcast $broadcast_dimensions, $output)]>; + + +def : Pat<(MHLO_TransposeOp $arg, $permutation), + (TF_TransposeOp $arg, (TF_ConstOp $permutation))>; +def : Pat<(MHLO_ReverseOp $op, $dims), (TF_ReverseV2Op $op, (TF_ConstOp $dims))>; +def : Pat<(MHLO_ReshapeOp:$output $input), + (TF_ReshapeOp $input, (ShapeToConst $output))>; +def : Pat<(MHLO_DynamicReshapeOp $input, $shape), + (TF_ReshapeOp $input, $shape)>; + +// Both implement the Banker's rounding. +def : Pat<(MHLO_RoundNearestEvenOp $input), (TF_RoundOp $input)>; + +//===----------------------------------------------------------------------===// +// Ternary op patterns. +//===----------------------------------------------------------------------===// + +def : Pat<(MHLO_ClampOp $min, $arg, $max), + (TF_MaximumOp (TF_MinimumOp $arg, $max), $min)>; +def : Pat<(MHLO_SelectOp $cond, $t, $e), (TF_SelectOp $cond, $t, $e)>; +def : Pat<(MHLO_SelectOp (MHLO_BroadcastInDimOp:$output + $bcast_cond, + $broadcast_dimensions), + $t, $e), + (TF_SelectV2Op $bcast_cond, $t, $e), + [(IsTFStyleBroadcast $broadcast_dimensions, $output)]>; +def : Pat<(MHLO_SelectOp $cond, $t, + (MHLO_BroadcastInDimOp:$output + $bcast_operand, + $broadcast_dimensions)), + (TF_SelectV2Op $cond, $t, $bcast_operand), + [(IsTFStyleBroadcast $broadcast_dimensions, $output)]>; +def : Pat<(MHLO_SelectOp $cond, (MHLO_BroadcastInDimOp:$output + $bcast_operand, + $broadcast_dimensions), + $e), + (TF_SelectV2Op $cond, $bcast_operand, $e), + [(IsTFStyleBroadcast $broadcast_dimensions, $output)]>; + +//===----------------------------------------------------------------------===// +// Variadic op patterns. +//===----------------------------------------------------------------------===// + +def : Pat<(MHLO_ConcatenateOp $inputs, $dim), + (TF_ConcatV2Op $inputs, (TF_ConstOp $dim))>; + +//===----------------------------------------------------------------------===// +// Compare op patterns. +// Note that these are legalized from chlo.broadcast_* ops, since those are +// semantically compatible with the corresponding TF ops. Depending on +// context, getting to these ops may require some raising. +//===----------------------------------------------------------------------===// + +class HasChloCompareType : + CPred<"llvm::cast<::mlir::chlo::ComparisonTypeAttr>($_self).getValue() == " # value>; + +// Attribute value should be such that it matches the comparison used by +// TensorFlow, if the attribute is present. +def IsChloTFCompareType : AttrConstraint< + Or<[CPred<"!$_self">, HasChloCompareType<"::mlir::chlo::ComparisonType::FLOAT">, + HasChloCompareType<"::mlir::chlo::ComparisonType::SIGNED">, + HasChloCompareType<"::mlir::chlo::ComparisonType::UNSIGNED">, + HasChloCompareType<"::mlir::chlo::ComparisonType::NOTYPE">]>, + "compare type supported by TensorFlow">; + +class CHLO_ComparisonDirectionValue : + ConstantAttr; + +class HasMhloCompareType : + CPred<"llvm::cast<::mlir::mhlo::ComparisonTypeAttr>($_self).getValue() == " # value>; + +// Attribute value should be such that it matches the comparison used by +// TensorFlow, if the attribute is present. +def IsMhloTFCompareType : AttrConstraint< + Or<[CPred<"!$_self">, HasMhloCompareType<"::mlir::mhlo::ComparisonType::FLOAT">, + HasMhloCompareType<"::mlir::mhlo::ComparisonType::SIGNED">, + HasMhloCompareType<"::mlir::mhlo::ComparisonType::UNSIGNED">, + HasMhloCompareType<"::mlir::mhlo::ComparisonType::NOTYPE">]>, + "compare type supported by TensorFlow">; + +class MHLO_ComparisonDirectionValue : + ConstantAttr; + +foreach p = [[TF_EqualOp, CHLO_ComparisonDirectionValue<"EQ">], + [TF_NotEqualOp, CHLO_ComparisonDirectionValue<"NE">]] in { + def : Pat<(CHLO_BroadcastCompareOp $l, $r, $broadcast_dimensions, p[1], + IsChloTFCompareType:$type), + (p[0] $l, $r, ConstBoolAttrTrue), + [(IsLegalNumpyRankedBroadcast $l, $r, $broadcast_dimensions)]>; +} + +foreach p = [[TF_EqualOp, MHLO_ComparisonDirectionValue<"EQ">], + [TF_NotEqualOp, MHLO_ComparisonDirectionValue<"NE">]] in { + def : Pat<(MHLO_CompareOp $l, $r, p[1], IsMhloTFCompareType:$type), + (p[0] $l, $r, ConstBoolAttrTrue)>; + def : Pat<(MHLO_CompareOp (MHLO_BroadcastInDimOp:$output + $bcast_operand, + $broadcast_dimensions), + $r, + p[1], + IsMhloTFCompareType:$type), + (p[0] $bcast_operand, $r, ConstBoolAttrTrue), + [(IsTFStyleBroadcast $broadcast_dimensions, $output)]>; + def : Pat<(MHLO_CompareOp $l, + (MHLO_BroadcastInDimOp:$output + $bcast_operand, + $broadcast_dimensions), + p[1], + IsMhloTFCompareType:$type), + (p[0] $l, $bcast_operand, ConstBoolAttrTrue), + [(IsTFStyleBroadcast $broadcast_dimensions, $output)]>; +} + +foreach p = [[TF_GreaterEqualOp, CHLO_ComparisonDirectionValue<"GE">], + [TF_GreaterOp, CHLO_ComparisonDirectionValue<"GT">], + [TF_LessEqualOp, CHLO_ComparisonDirectionValue<"LE">], + [TF_LessOp, CHLO_ComparisonDirectionValue<"LT">]] in { + def : Pat<(CHLO_BroadcastCompareOp $l, $r, $broadcast_dimensions, + p[1], IsChloTFCompareType:$type), + (p[0] $l, $r), + [(IsLegalNumpyRankedBroadcast $l, $r, $broadcast_dimensions)]>; +} + +foreach p = [[TF_GreaterEqualOp, MHLO_ComparisonDirectionValue<"GE">], + [TF_GreaterOp, MHLO_ComparisonDirectionValue<"GT">], + [TF_LessEqualOp, MHLO_ComparisonDirectionValue<"LE">], + [TF_LessOp, MHLO_ComparisonDirectionValue<"LT">]] in { + def : Pat<(MHLO_CompareOp $l, $r, p[1], IsMhloTFCompareType:$type), + (p[0] $l, $r)>; + def : Pat<(MHLO_CompareOp (MHLO_BroadcastInDimOp:$output + $bcast_operand, + $broadcast_dimensions), + $r, + p[1], + IsMhloTFCompareType:$type), + (p[0] $bcast_operand, $r), + [(IsTFStyleBroadcast $broadcast_dimensions, $output)]>; + def : Pat<(MHLO_CompareOp $l, + (MHLO_BroadcastInDimOp:$output + $bcast_operand, + $broadcast_dimensions), + p[1], + IsMhloTFCompareType:$type), + (p[0] $l, $bcast_operand), + [(IsTFStyleBroadcast $broadcast_dimensions, $output)]>; +} + +def ConvertDotOp : NativeCodeCall<"ConvertDotOp($_builder, " + "$0.getDefiningOp())">; +def : Pat<(MHLO_DotOp:$old_value StaticShapeTensorOf<[TF_ElementType]>:$lhs, + StaticShapeTensorOf<[TF_ElementType]>:$rhs, $precision_config), + (ConvertDotOp $old_value)>; + +def ConvertDotGeneralOp : NativeCodeCall<"ConvertDotGeneralOp($_builder, " + "$0.getDefiningOp())">; +def : Pat<(MHLO_DotGeneralOp:$old_value + RankedTensorOf<[TF_ElementType]>:$lhs, + RankedTensorOf<[TF_ElementType]>:$rhs, + $dot_dimension_numbers, $precision_config, $algorithm), + (ConvertDotGeneralOp $old_value)>; + + +def IsZero : Constraint() == 0">>; +def ConvertPadOp : NativeCodeCall< + "ConvertPadOp($_builder, $0.getDefiningOp())">; +def : Pat<(MHLO_PadOp:$old_value + StaticShapeTensorOf<[TF_ElementType]>:$input, + $pad_value, $pad_low, $pad_high, $pad_interior), + (ConvertPadOp $old_value), + [(IsZero $pad_interior)]>; + +class ValueEquals : Constraint>; +def ValueGreaterThanZero : Constraint>; +def ValueIsReciprocal : Constraint>; +def TensorIsSign : Constraint>; +def SameValue : Constraint>; +def FloatOrDefaultCompare : Constraint>; +def SameTypeOrDefaultCompare : Constraint>; + +// Converts a dag of HLOs representing banker rounding (round x.5 to nearest +// even) to tf.round. This only supports float types because mhlo.floor only +// supports float types. tf.round with integer input type will become an +// identity op, so we will never face an mhlo.floor with an integer input type. +// The pattern matched executes the following computation: +// frac = x - floor(x) +// to_even = (floor(x) - 2 * floor(0.5 * x)) == 1 +// if frac > 0.5 || (frac == 0.5 && to_even) +// return floor(x) + 1 +// else +// return floor(x) +def : Pat<(MHLO_SelectOp + (MHLO_OrOp + (MHLO_CompareOp (MHLO_SubtractOp:$frac + $input, + (MHLO_FloorOp:$floor $input)), + (MHLO_ConstantOp $half), + MHLO_ComparisonDirectionValue<"GT">, + $compare_type0), + (MHLO_AndOp + (MHLO_CompareOp + $frac1, + (MHLO_ConstantOp $half1), + MHLO_ComparisonDirectionValue<"EQ">, + $compare_type1), + (MHLO_CompareOp + (MHLO_SubtractOp + $floor1, + (MHLO_MulOp + (MHLO_FloorOp (MHLO_MulOp $input, (MHLO_ConstantOp $half2))), + (MHLO_ConstantOp $two))), + (MHLO_ConstantOp $one1), + MHLO_ComparisonDirectionValue<"EQ">, + $compare_type2))), + (MHLO_AddOp $floor2, (MHLO_ConstantOp $one)), + $floor3), + (TF_RoundOp $input), + [(ValueEquals<"1.0"> $one), + (ValueEquals<"1.0"> $one1), + (ValueEquals<"2.0"> $two), + (ValueEquals<"0.5"> $half), + (ValueEquals<"0.5"> $half1), + (ValueEquals<"0.5"> $half2), + (SameValue $floor, $floor1), + (SameValue $floor, $floor2), + (SameValue $floor, $floor3), + (SameValue $frac, $frac1), + (FloatOrDefaultCompare $compare_type0), + (FloatOrDefaultCompare $compare_type1), + (FloatOrDefaultCompare $compare_type2)]>; + +// Converts a dag of HLOs representing floor_mod to tf.FloorMod. +// The pattern matched executes the following computation: +// +// rem = remainder(arg0, arg1) +// for i in 0 to len(arg1): +// if ((rem[i] < 0) != (arg0[i] < 0) && arg0[i] != 0) +// rem[i] += arg1[i] +// return rem +def : Pat<(MHLO_SelectOp + (MHLO_AndOp + (MHLO_CompareOp + (MHLO_CompareOp:$rltz + (MHLO_RemOp:$rem $arg, $arg1), + (MHLO_ConstantOp $cst), + MHLO_ComparisonDirectionValue<"LT">, + $compare_type), + (MHLO_CompareOp:$arg1ltz $arg1, (MHLO_ConstantOp $cst1), MHLO_ComparisonDirectionValue<"LT">, $compare_type1), + MHLO_ComparisonDirectionValue<"NE">, + $compare_type2), + (MHLO_CompareOp:$rnz $rem1, (MHLO_ConstantOp $cst2), MHLO_ComparisonDirectionValue<"NE">, $compare_type3)), + (MHLO_AddOp $rem2, $arg1), + $rem3), + (TF_FloorModOp $arg, $arg1), + [(ValueEquals<"0.0"> $cst), + (ValueEquals<"0.0"> $cst1), + (ValueEquals<"0.0"> $cst2), + (SameValue $rem, $rem1), + (SameValue $rem, $rem2), + (SameValue $rem, $rem3), + (SameTypeOrDefaultCompare $compare_type, $cst), + (SameTypeOrDefaultCompare $compare_type1, $cst1)]>; + +// Converts a dag of HLOs representing floor_mod with a constant to +// tf.FloorMod. The pattern matched executes the following computation: +// +// cst = value that is > 0 +// rem = remainder(arg0, cst) +// for i in 0 to len(arg1): +// if (rem[i] < 0 && rem[i] != 0) +// rem[i] += cst +// return rem +def : Pat<(MHLO_SelectOp + (MHLO_AndOp + (MHLO_CompareOp:$rltz + (MHLO_RemOp:$rem $arg, (MHLO_ConstantOp $cst)), + (MHLO_ConstantOp $cst1), + MHLO_ComparisonDirectionValue<"LT">, + $compare_type), + (MHLO_CompareOp:$rnz $rem1, (MHLO_ConstantOp $cst2), MHLO_ComparisonDirectionValue<"NE">, $compare_type3)), + (MHLO_AddOp $rem2, (MHLO_ConstantOp $cst3)), + $rem3), + (TF_FloorModOp $arg, (TF_ConstOp $cst3)), + [(ValueGreaterThanZero $cst), + (ValueEquals<"0.0"> $cst1), + (ValueEquals<"0.0"> $cst2), + (SameValue $cst, $cst3), + (SameValue $rem, $rem1), + (SameValue $rem, $rem2), + (SameValue $rem, $rem3), + (SameTypeOrDefaultCompare $compare_type, $cst1), + (SameTypeOrDefaultCompare $compare_type3, $cst2)]>; + +// Converts a dag of HLOs representing floor_div to tf.FloorDiv. +// The pattern matched executes the following computation: +// +// rem = remainder(arg0, arg1) +// for i in 0 to len(arg1): +// rem[i] = arg0[i] - rem[i] / arg1[i] +// if (rem[i] != 0 && sign(arg1[i]) != sign(rem[i])) +// rem[i] -= 1.0 +// return round_nearest_afz(rem) +// As a dag this looks like the following: +// round +// | +// -------- select +// | | \ +// && + div +// / | / \ +// != != div -1 +// / | / | / | +// rem 0.0 sn sn1 - $1 +// / | | | / | +// $0 $1 $1 rem $0 rem +// Note that named operators like 'sn' and 'sn1' are different values produced by +// the same function in this case the sign function. Named values like 'div' +// refer to the same value produced by the same function, in this case division. +// Mathematical symbols do not indicate a re-use of the value. +def : Pat<(MHLO_RoundOp + (MHLO_SelectOp + (MHLO_AndOp + (MHLO_CompareOp + (MHLO_RemOp:$rem $arg0, $arg1), + (MHLO_ConstantOp $cst), + MHLO_ComparisonDirectionValue<"NE">, + $compare_type), + (MHLO_CompareOp + (MHLO_SignOp $arg1), + (MHLO_SignOp $rem1), + MHLO_ComparisonDirectionValue<"NE">, + $compare_type1)), + (MHLO_AddOp + (MHLO_DivOp:$div + (MHLO_SubtractOp $arg0, $rem2), + $arg1b), + (MHLO_ConstantOp $cst_neg1)), + $div1)), + (TF_FloorDivOp $arg0, $arg1), + [(ValueEquals<"0.0"> $cst), + (ValueEquals<"-1.0"> $cst_neg1), + (SameValue $div, $div1), + (SameValue $rem, $rem1), + (SameValue $rem, $rem2), + (FloatOrDefaultCompare $compare_type, $cst), + (FloatOrDefaultCompare $compare_type1, $cst)]>; + +// Converts a dag of HLOs representing floor_div with a splat constant to +// tf.FloorDiv. The pattern matched executes the following computation: +// This particular pattern matches multiplication with the reciprocal of the +// constant instead of dividing by the constant. +// rem = remainder(arg0, cst) +// for i in 0 to len(arg0): +// rem[i] = (arg0[i] - rem[i]) * 1 / cst +// if (rem[i] != 0 && sign(cst) != sign(rem[i])) +// rem[i] += -1.0 +// return round_nearest_afz(rem) +// As a dag this looks like the following: +// round +// | +// -------- select +// | | \ +// && + mul +// / | / \ +// != != mul -1 +// / | / | / | +// rem 0.0 cs1 sn1 - cs2 +// / | | / | +// $0 cst rem $0 rem +// cs1 == sign(cst) +// cs2 = 1 / cst i.e. the reciprocal +// Note that named operators like 'sn' and 'sn1' are different values produced by +// the same function in this case the sign function. Named values like 'div' +// refer to the same value produced by the same function, in this case division. +// Mathematical symbols do not indicate a re-use of the value. +def : Pat<(MHLO_RoundOp + (MHLO_SelectOp + (MHLO_AndOp + (MHLO_CompareOp + (MHLO_RemOp:$rem $arg0, (MHLO_ConstantOp:$cst $cstv)), + (MHLO_ConstantOp $cst_zero), + MHLO_ComparisonDirectionValue<"NE">, + $compare_type), + (MHLO_CompareOp + (MHLO_ConstantOp $cst_sgn), + (MHLO_SignOp $rem1), + MHLO_ComparisonDirectionValue<"NE">, + $compare_type1)), + (MHLO_AddOp + (MHLO_MulOp:$mul + (MHLO_SubtractOp $arg0, $rem2), + (MHLO_ConstantOp $cst_recip)), + (MHLO_ConstantOp $cst_neg1)), + $mul1)), + (TF_FloorDivOp $arg0, $cst), + [(ValueEquals<"0.0"> $cst_zero), + (ValueEquals<"-1.0"> $cst_neg1), + (TensorIsSign $cstv, $cst_sgn), + (ValueIsReciprocal $cstv, $cst_recip), + (SameValue $mul, $mul1), + (SameValue $rem, $rem1), + (SameValue $rem, $rem2), + (FloatOrDefaultCompare $compare_type, $cst_zero), + (FloatOrDefaultCompare $compare_type1, $cst_sgn)]>; + +// Converts a dag of HLOs representing floor_div with a splat constant to +// tf.FloorDiv. The pattern matched executes the following computation: +// This particular pattern matches division with the constant. +// . +// rem = remainder(arg0, cst) +// for i in 0 to len(arg0): +// rem[i] = (arg0[i] - rem[i]) / cst +// if (rem[i] != 0 && sign(cst) != sign(rem[i])) +// rem[i] -= 1.0 +// return round_nearest_afz(rem) +// As a dag this looks like the following: +// round +// | +// -------- select +// | | \ +// && + div +// / | / \ +// != != div -1 +// / | / | / | +// rem 0.0 cs1 sn1 - cs2 +// / | | / | +// $0 cst rem $0 rem +// cs1 == sign(cst) +// cs2 = 1 / cst i.e. the reciprocal +// Note that named operators like 'sn' and 'sn1' are different values produced by +// the same function in this case the sign function. Named values like 'div' +// refer to the same value produced by the same function, in this case division. +// Mathematical symbols do not indicate a re-use of the value. +def : Pat<(MHLO_RoundOp + (MHLO_SelectOp + (MHLO_AndOp + (MHLO_CompareOp + (MHLO_RemOp:$rem $arg0, (MHLO_ConstantOp:$cst $cstv)), + (MHLO_ConstantOp $cst_zero), + MHLO_ComparisonDirectionValue<"NE">, + $compare_type), + (MHLO_CompareOp + (MHLO_ConstantOp $cst_sgn), + (MHLO_SignOp $rem1), + MHLO_ComparisonDirectionValue<"NE">, + $compare_type1)), + (MHLO_AddOp + (MHLO_DivOp:$div + (MHLO_SubtractOp $arg0, $rem2), + (MHLO_ConstantOp $cstv1)), + (MHLO_ConstantOp $cst_neg1)), + $div1)), + (TF_FloorDivOp $arg0, $cst), + [(ValueEquals<"0.0"> $cst_zero), + (ValueEquals<"-1.0"> $cst_neg1), + (TensorIsSign $cstv, $cst_sgn), + (SameValue $div, $div1), + (SameValue $rem, $rem1), + (SameValue $rem, $rem2), + (SameValue $cstv1, $cstv), + (FloatOrDefaultCompare $compare_type, $cst_zero), + (FloatOrDefaultCompare $compare_type1, $cst_sgn)]>; + +// Converts a dag of HLOs representing floor_div with a broadcasted vector +// constant to tf.FloorDiv. The pattern matched executes the following +// computation: +// scs = sign(cst) +// bcst = broadcast(cst) +// rem = remainder(arg0, bcst) +// for i in 0 to len(arg0): +// rem[i] = arg0[i] - rem[i] * / bcst +// if (rem[i] != 0 && scs != sign(rem[i])) +// rem[i] -= 1.0 +// return round_nearest_afz(rem) +// Where scs is a splat constant folded sign on the unbroadcasted tensor. +// +// As a dag this looks like the following: +// round +// | +// -------- select +// | | \ +// && + div +// / | / \ +// != != div -1 +// / | / | / | +// rem 0.0 scs sn1 - bcst +// / | | / | +// $0 bcst rem $0 rem +// | +// cst +// scs == sign(cst) == sign(bcst) +// Note that named operators like 'sn' and 'sn1' are different values produced by +// the same function in this case the sign function. Named values like 'div' +// refer to the same value produced by the same function, in this case division. +// Mathematical symbols do not indicate a re-use of the value. +def : Pat<(MHLO_RoundOp + (MHLO_SelectOp + (MHLO_AndOp + (MHLO_CompareOp + (MHLO_RemOp:$rem $arg0, + (MHLO_BroadcastInDimOp:$bcst + (MHLO_ConstantOp $cstv), + $broadcast_dimension)), + (MHLO_ConstantOp $cst_zero), + MHLO_ComparisonDirectionValue<"NE">, + $compare_type), + (MHLO_CompareOp + (MHLO_ConstantOp $cst_sgn), + (MHLO_SignOp $rem1), + MHLO_ComparisonDirectionValue<"NE">, + $compare_type1)), + (MHLO_AddOp + (MHLO_DivOp:$div + (MHLO_SubtractOp $arg0, $rem2), + $bcst1), + (MHLO_ConstantOp $cst_neg1)), + $div1)), + (TF_FloorDivOp $arg0, $bcst), + [(ValueEquals<"0.0"> $cst_zero), + (ValueEquals<"-1.0"> $cst_neg1), + (TensorIsSign $cstv, $cst_sgn), + (SameValue $bcst, $bcst1), + (SameValue $div, $div1), + (SameValue $rem, $rem1), + (SameValue $rem, $rem2), + (FloatOrDefaultCompare $compare_type, $cst_zero), + (FloatOrDefaultCompare $compare_type1, $cst_sgn)]>; + +//===----------------------------------------------------------------------===// +// TorchIndexSelect op patterns. +//===----------------------------------------------------------------------===// + +def : Pat<(MHLO_TorchIndexSelectOp $params, $indices, $axis, $batch_dims), + (TF_GatherV2Op $params, $indices, (TF_ConstOp $axis), $batch_dims)>; + diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_stablehlo_composite_to_tfl_custom.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_stablehlo_composite_to_tfl_custom.cc new file mode 100644 index 0000000000000000000000000000000000000000..b8dd15925b80b18187a74f8c4f58f3917edd9ec4 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_stablehlo_composite_to_tfl_custom.cc @@ -0,0 +1,193 @@ +/* Copyright 2024 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 "flatbuffers/flexbuffers.h" // from @flatbuffers +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/Support/Casting.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/OperationSupport.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "stablehlo/dialect/StablehloOps.h" // from @stablehlo +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h" + +#define DEBUG_TYPE "composite-to-custom" + +namespace mlir { +namespace odml { + +#define GEN_PASS_DEF_LEGALIZECOMPOSITETOCUSTOMOPPASS +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h.inc" + +namespace { +bool IsSupportedComposite(::mlir::stablehlo::CompositeOp op) { + // List of supported composites to represent using CustomOp. + return llvm::is_contained( + {"odml.update_kv_cache", "odml.update_external_kv_cache", + "odml.quantize_and_dequantize", "odml.detector"}, + op.getName()) || + op.getName().starts_with("litert_custom_op."); +} + +bool IsKVCacheCompositeOp(::mlir::stablehlo::CompositeOp op) { + return op.getName() == "odml.update_kv_cache"; +} + +TFL::ConstBytesAttr CustomOption(OpBuilder* builder, + const std::string& content) { + return TFL::ConstBytesAttr::get(builder->getContext(), + StringRef(content.data(), content.size())); +} + +LogicalResult BuildOption(flexbuffers::Builder* fbb, Operation* op, + NamedAttribute pair) { + const char* key = pair.getName().data(); + const auto attr = pair.getValue(); + + if (mlir::isa<::mlir::IntegerAttr>(attr)) { + fbb->Int(key, mlir::dyn_cast(attr).getInt()); + return success(); + } + + if (mlir::isa<::mlir::FloatAttr>(attr)) { + fbb->Double(key, mlir::dyn_cast(attr).getValueAsDouble()); + return success(); + } + + if (mlir::isa<::mlir::StringAttr>(attr)) { + fbb->String( + key, mlir::dyn_cast(attr).getValue().str().c_str()); + return success(); + } + + return op->emitWarning("serialization not supported for : ") << key; +} + +TFL::CustomOp BuildCustomOp(stablehlo::CompositeOp composite, + const std::string& custom_option_buffer) { + OpBuilder builder(composite->getContext()); + builder.setInsertionPoint(composite); + if (IsKVCacheCompositeOp(composite)) { + return TFL::CustomOp::create( + builder, composite->getLoc(), composite->getResultTypes(), + composite->getOperands().slice(2, 3), composite.getName(), + CustomOption(&builder, custom_option_buffer)); + } + return TFL::CustomOp::create(builder, composite->getLoc(), + composite->getResultTypes(), + composite->getOperands(), composite.getName(), + CustomOption(&builder, custom_option_buffer)); +} + +} // namespace + +// Legalize stablehlo::CompositeOp to TFL::CustomOp for runtime-supported +// composites. See `IsSupportedComposite` for list of supported ops. +// +// Example: +// %0 = stablehlo.composite "odml.some_op" { +// composite_attrs = {}, +// version = 0 : i32 +// } +// ==> +// %0 = tfl.custom() { +// custom_code = "odml.some_op", +// custom_option = #tfl +// } +struct LegalizeCompositeToCustomOpPass + : public impl::LegalizeCompositeToCustomOpPassBase< + LegalizeCompositeToCustomOpPass> { + using LegalizeCompositeToCustomOpPassBase:: + LegalizeCompositeToCustomOpPassBase; + + void runOnOperation() override { + func::FuncOp fn = getOperation(); + + int num_layers = 0, current_layer_index = 0; + // First walk the function to count number of KV Caches. + fn.walk([&](Operation* op) { + auto composite = llvm::dyn_cast(op); + if (!composite || !IsKVCacheCompositeOp(composite)) return; + num_layers++; + }); + + fn.walk([&](Operation* op) { + // Process only StableHLO composite ops. + auto composite = llvm::dyn_cast(op); + if (!composite || !IsSupportedComposite(composite)) return; + + if (IsKVCacheCompositeOp(composite)) { + auto comp_attr = composite.getCompositeAttributes(); + mlir::Builder builder(composite->getContext()); + + // num_layers Composite Attribute. + mlir::StringAttr num_layers_str = builder.getStringAttr("num_layers"); + NamedAttribute num_layers_attr( + num_layers_str, + IntegerAttr::get(IntegerType::get(fn.getContext(), /*width=*/32), + num_layers)); + + // current_layer_index Composite Attribute. + mlir::StringAttr current_layer_str = + builder.getStringAttr("layer_index"); + NamedAttribute current_layer_attr( + current_layer_str, + IntegerAttr::get(IntegerType::get(fn.getContext(), /*width=*/32), + current_layer_index++)); + + // Build a new CompositeAttributes attr, add in the above, + // and set for the op. + mlir::NamedAttrList attributes(comp_attr); + attributes.append(num_layers_attr); + attributes.append(current_layer_attr); + comp_attr = attributes.getDictionary(builder.getContext()); + composite.setCompositeAttributesAttr(comp_attr); + } + + // Build flexbuffer options. + std::string custom_option_buffer; + auto fbb = std::make_unique(); + size_t map_start = fbb->StartMap(); + for (const NamedAttribute& pair : composite.getCompositeAttributes()) { + // Allows skipping unsupported attributes, will warn. + (void)BuildOption(fbb.get(), op, pair); + } + fbb->EndMap(map_start); + fbb->Finish(); + custom_option_buffer.assign(fbb->GetBuffer().begin(), + fbb->GetBuffer().end()); + + // Build TFL custom op, replace composite with custom op. + TFL::CustomOp tfl_custom_op = + BuildCustomOp(composite, custom_option_buffer); + composite->replaceAllUsesWith(tfl_custom_op); + composite->erase(); + }); + } +}; + +static PassRegistration pass; + +} // namespace odml +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_stablehlo_custom_call_to_composite.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_stablehlo_custom_call_to_composite.cc new file mode 100644 index 0000000000000000000000000000000000000000..4d5c601d04f179368ce9a41cfb5d389c79c54170 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_stablehlo_custom_call_to_composite.cc @@ -0,0 +1,111 @@ +/* Copyright 2022 The StableHLO Authors. +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 "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/Visitors.h" // from @llvm-project +#include "mlir/Pass/PassManager.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "stablehlo/dialect/StablehloOps.h" // from @stablehlo +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h" + +namespace mlir { +namespace odml { + +#define GEN_PASS_DEF_LEGALIZESTABLEHLOCUSTOMCALLTOCOMPOSITEPASS +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h.inc" + +struct ReplaceCustomCallWithComposite final + : OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + + explicit ReplaceCustomCallWithComposite(MLIRContext *context) + : OpRewritePattern(context) {} + + LogicalResult matchAndRewrite(mlir::stablehlo::CustomCallOp op, + PatternRewriter &rewriter) const override { + auto backendConfig = + mlir::dyn_cast(op->getAttr("composite.backend_config")); + if (!backendConfig) + return op->emitError( + "custom_call has no 'composite.backend_config' attribute or the " + "attribute is not a dictionary"); + + auto name = mlir::dyn_cast(backendConfig.get("name")); + if (!name) + return op->emitError( + "backend_config has no 'name' key or the name value is not a string"); + + auto attrs = + mlir::dyn_cast(backendConfig.get("attributes")); + if (!attrs) + return op->emitError( + "backend_config has no 'attributes' key or the attributes value is " + "not a dictionary"); + + auto calledComputations = op.getCalledComputations(); + if (!calledComputations || calledComputations.size() != 1) + return op->emitError("expected exactly one called_computation"); + + auto decomposition = mlir::cast(calledComputations[0]); + + auto composite = mlir::stablehlo::CompositeOp::create( + rewriter, op.getLoc(), op.getResultTypes(), op.getOperands(), + name.str(), attrs, decomposition.getValue()); + rewriter.replaceOp(op, composite.getResults()); + return success(); + } +}; + +struct LegalizeStablehloCustomCallToCompositePass + : public impl::LegalizeStablehloCustomCallToCompositePassBase< + LegalizeStablehloCustomCallToCompositePass> { + using LegalizeStablehloCustomCallToCompositePassBase:: + LegalizeStablehloCustomCallToCompositePassBase; + + void runOnOperation() override { + MLIRContext *context = &getContext(); + + ConversionTarget target(*context); + target.addLegalDialect(); + target.addLegalDialect(); + target.addDynamicallyLegalOp( + [&](mlir::stablehlo::CustomCallOp op) { + return op.getCallTargetName() != "stablehlo.composite"; + }); + + RewritePatternSet patterns(context); + patterns.add(context); + + if (failed(applyPartialConversion(getOperation(), target, + std::move(patterns)))) { + return signalPassFailure(); + } + } +}; + +static PassRegistration + pass_shlo_sc2c; + +} // namespace odml +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_stablehlo_to_vhlo.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_stablehlo_to_vhlo.cc new file mode 100644 index 0000000000000000000000000000000000000000..9387c18eb487b4f60f4acd0f9c18164b78d7b8b8 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_stablehlo_to_vhlo.cc @@ -0,0 +1,304 @@ +/* Copyright 2022 The StableHLO Authors. +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 "llvm/Support/Debug.h" +#include "mlir/Conversion/ReconcileUnrealizedCasts/ReconcileUnrealizedCasts.h" // from @llvm-project +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/ValueRange.h" // from @llvm-project +#include "mlir/IR/Visitors.h" // from @llvm-project +#include "mlir/Pass/PassManager.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" // from @llvm-project +#include "stablehlo/api/PortableApi.h" // from @stablehlo +#include "stablehlo/dialect/StablehloOps.h" // from @stablehlo +#include "stablehlo/dialect/VhloOps.h" // from @stablehlo +#include "stablehlo/dialect/VhloTypes.h" // from @stablehlo +#include "stablehlo/transforms/Passes.h" // from @stablehlo +#include "tensorflow/compiler/mlir/lite/core/macros.h" +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h" + +#define DEBUG_TYPE "compat-passes" + +namespace mlir { +namespace odml { + +#define GEN_PASS_DEF_LEGALIZESTABLEHLOTOVHLOPASS +#define GEN_PASS_DEF_LEGALIZEVHLOTOSTABLEHLOPASS +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h.inc" + +namespace { + +//===----------------------------------------------------------------------===// +// StableHLO --> VHLO types +//===----------------------------------------------------------------------===// + +Value MaterializeIllegalCast(OpBuilder &builder, Type type, + ValueRange inputs, Location loc) { + return UnrealizedConversionCastOp::create(builder, loc, type, inputs) + ->getResult(0); +} + +class StablehloToOdmlTypeConverter : public vhlo::VhloTypeConverter { + public: + StablehloToOdmlTypeConverter() : vhlo::VhloTypeConverter() { + addConversion([](Type type) { + if (type.getDialect().getNamespace() == + vhlo::VhloDialect::getDialectNamespace()) { + return type; + } + LLVM_DEBUG(llvm::dbgs() << "Invalid type: " << type << '\n'); + return Type(); + }); + addConversion([](stablehlo::TokenType token) { + return vhlo::TokenV1Type::get(token.getContext()); + }); + addBuiltinToVhloConversions(); + + addSourceMaterialization(MaterializeIllegalCast); + addTargetMaterialization(MaterializeIllegalCast); + } + + Attribute convertEncoding(Attribute attr) const final { + LLVM_DEBUG(llvm::dbgs() << "Converting encoding.\n" << attr << '\n'); + // Must be VHLO encoding, or convertible to VHLO encoding. + if (attr.getDialect().getNamespace() == + vhlo::VhloDialect::getDialectNamespace()) + return attr; + + if (auto stablehlo_attr = + mlir::dyn_cast_or_null(attr)) { + return vhlo::TypeExtensionsV1Attr::get(stablehlo_attr.getContext(), + stablehlo_attr.getBounds()); + } + + // Was not VHLO encoding, or convertible. + return {}; + } +}; + +class VhloToStablehloTypeConverter : public vhlo::VhloTypeConverter { + public: + VhloToStablehloTypeConverter() : vhlo::VhloTypeConverter() { + addConversion([](Type type) { return type; }); + addConversion([](vhlo::TokenV1Type token) { + LLVM_DEBUG(llvm::dbgs() << "Converting TokenType\n"); + return stablehlo::TokenType::get(token.getContext()); + }); + addVhloToBuiltinConversions(); + + addSourceMaterialization(MaterializeIllegalCast); + addTargetMaterialization(MaterializeIllegalCast); + } + + Attribute convertEncoding(Attribute attr) const final { + if (auto vhlo_attr = + mlir::dyn_cast_or_null(attr)) { + return stablehlo::TypeExtensionsAttr::get(vhlo_attr.getContext(), + vhlo_attr.getBounds()); + } + // All encodings supported in StableHLO. + return attr; + } +}; + +//===----------------------------------------------------------------------===// +// StableHLO+TFL --> VHLO+TFL Ops +//===----------------------------------------------------------------------===// + +// Wrap op result uses in an unrealized cast to create a cast to buffer +// any type changes to result, and apply type converter to result: +// result = op(V0) +// V1 = op2(result) +// ==> +// result = op(V0) +// V1 = unrealized_cast(result) +// V2 = op2(V1) +void ConvertAndWrapUsesInUnrealizedCast(Value result, TypeConverter &converter, + IRRewriter &rewriter) { + auto type = result.getType(); + result.setType(converter.convertType(result.getType())); + auto new_value = converter.materializeSourceConversion( + rewriter, result.getLoc(), type, {result}); + rewriter.replaceAllUsesExcept(result, new_value, new_value.getDefiningOp()); +} + +// Wrap operands in an an unrealized cast to create a cast to buffer any type +// changes to the operand, and apply type converter to operands: +// V0 = op(operand) +// ==> +// V0 = unrealized_cast(operand) +// V1 = op(V0) +void WrapOperandsInUnrealizedCastAndConvert(Operation *op, + TypeConverter &converter, + IRRewriter &rewriter) { + for (int i = 0; i < op->getNumOperands(); ++i) { + auto operand = op->getOperand(i); + auto new_operand = converter.materializeSourceConversion( + rewriter, op->getLoc(), converter.convertType(operand.getType()), + {operand}); + op->setOperand(i, new_operand); + } +} + +// vhlo.op %1 : vhlo.tensor<...> +// ==> +// vhlo.op %1 : tensor<...> +// +// TODO: There's likely a way to make MLIR manage the unrealized cast +// conversions using a specific rewriter. +LogicalResult ApplyTypeConverter(ModuleOp op, TypeConverter &converter) { + IRRewriter rewriter(op->getContext()); + + op->walk([&](Operation *op) { + if (op->getDialect()->getNamespace() != "vhlo") return; + + // Convert operands + rewriter.modifyOpInPlace(op, [&]() { + rewriter.setInsertionPoint(op); + WrapOperandsInUnrealizedCastAndConvert(op, converter, rewriter); + + // Convert op types + for (auto value : op->getResults()) { + rewriter.setInsertionPointAfter(value.getDefiningOp()); + ConvertAndWrapUsesInUnrealizedCast(value, converter, rewriter); + } + + // Convert block arguments + for (auto ®ion : op->getRegions()) { + for (auto &block : region.getBlocks()) { + rewriter.setInsertionPointToStart(&block); + for (auto arg : block.getArguments()) { + ConvertAndWrapUsesInUnrealizedCast(arg, converter, rewriter); + } + } + } + }); + }); + return success(); +} + +// Legalize StableHLO portion of program to VHLO, leaves TFL untouched +LogicalResult ApplyStablehloToVhloPatterns(ModuleOp module, + bool is_func_legal) { + MLIRContext *context = module.getContext(); + ConversionTarget target(*context); + target.addIllegalDialect(); + target.addDynamicallyLegalDialect( + [&](auto) { return is_func_legal; }); + target.addLegalDialect(); + target.addLegalDialect(); + target.addLegalDialect(); + + StablehloToOdmlTypeConverter converter; + RewritePatternSet patterns(context); + stablehlo::populateStablehloToVhloPatterns(context, &patterns, &converter); + + if (failed(applyPartialConversion(module, target, std::move(patterns)))) { + return module->emitError("Failed partial conversion to VHLO"); + } + return success(); +} + +LogicalResult ApplyVhloToVersionPatterns(ModuleOp module, + const std::string &version) { + PassManager pm(module.getContext()); + pm.addPass(stablehlo::createVhloToVersionPass({version})); + if (failed(pm.run(module))) { + return module->emitError("Failed VHLO to version ") << version; + } + return success(); +} + +// Legalize VHLO portion of program to StableHLO, leaves TFL untouched. +LogicalResult ApplyVhloToStablehloPatterns(ModuleOp module) { + MLIRContext *context = module.getContext(); + ConversionTarget target(*context); + target.addIllegalDialect(); + target.addLegalDialect(); + target.addLegalDialect(); + target.addLegalDialect(); + target.addLegalDialect(); + + VhloToStablehloTypeConverter converter; + RewritePatternSet patterns(context); + stablehlo::populateVhloToStablehloPatterns(context, &patterns, &converter); + + if (failed(applyPartialConversion(module, target, std::move(patterns)))) { + return module->emitError("Failed partial conversion to StableHLO"); + } + return success(); +} + +} // namespace + +struct LegalizeStablehloToVhloPass + : public impl::LegalizeStablehloToVhloPassBase< + LegalizeStablehloToVhloPass> { + void runOnOperation() override { + ModuleOp module = getOperation(); + std::string target_version = tflite_supported_stablehlo_version; + VhloToStablehloTypeConverter to_builtin_converter; + + // StableHLO --> VHLO (allow funcs) + // VHLO -> Downgrade to tflite_supported_stablehlo_version + // VHLO Tensor --> Builtin Tensor + // Remove cast(tensor->vhlo) -> cast(vhlo->tensor) pattern + if (failed(ApplyStablehloToVhloPatterns(module, + /*is_func_legal=*/true)) || + failed(ApplyVhloToVersionPatterns(module, target_version)) || + failed(ApplyTypeConverter(module, to_builtin_converter))) + return signalPassFailure(); + } +}; + +struct LegalizeVhloToStablehloPass + : public impl::LegalizeVhloToStablehloPassBase< + LegalizeVhloToStablehloPass> { + void runOnOperation() override { + // Revert the tensor types to VHLO + auto module = getOperation(); + StablehloToOdmlTypeConverter to_vhlo_converter; + + // Builtin Tensor --> VHLO Tensor + // StableHLO --> VHLO + // VHLO --> Upgrade to current + // VHLO --> StableHLO + // Remove cast(tensor->vhlo) -> cast(vhlo->tensor) pattern + if (failed(ApplyTypeConverter(module, to_vhlo_converter)) || + failed(ApplyVhloToVersionPatterns(module, + stablehlo::getCurrentVersion())) || + failed(ApplyVhloToStablehloPatterns(module))) + return signalPassFailure(); + } +}; + +static PassRegistration pass_s2v; +static PassRegistration pass_v2s; + +} // namespace odml +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_tf_xla_call_module_to_stablehlo_pass.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_tf_xla_call_module_to_stablehlo_pass.cc new file mode 100644 index 0000000000000000000000000000000000000000..3cdbda99c55d5fe22049414f2130dad1baa8fdec --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_tf_xla_call_module_to_stablehlo_pass.cc @@ -0,0 +1,265 @@ +/* Copyright 2023 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/compiler/mlir/lite/stablehlo/transforms/legalize_tf_xla_call_module_to_stablehlo_pass.h" + +#include +#include +#include +#include + +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallVector.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Dialect/Quant/IR/Quant.h" // from @llvm-project +#include "mlir/Dialect/Shape/IR/Shape.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/OwningOpRef.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/SymbolTable.h" // from @llvm-project +#include "mlir/IR/Types.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" // from @llvm-project +#include "stablehlo/dialect/Serialization.h" // from @stablehlo +#include "stablehlo/dialect/StablehloOps.h" // from @stablehlo +#include "stablehlo/dialect/VhloOps.h" // from @stablehlo +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" + +namespace mlir { +namespace odml { + +static constexpr absl::string_view kStablehloModuleDefaultEntryFuncName = + "main"; +static constexpr absl::string_view kStablehloFuncNamePrefix = "XlaCallModule"; +static constexpr char kShardingAttr[] = "mhlo.sharding"; +static constexpr char kShardingName[] = "Sharding"; + +class RemoveCustomCallWithSharding + : public OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(stablehlo::CustomCallOp op, + PatternRewriter &rewriter) const override { + // Removes the custom call with sharding op if the operand type is the + // same as the result type. + if (op->hasAttr(kShardingAttr) && op.getCallTargetName() == kShardingName && + op.getNumOperands() == 1 && op.getNumResults() == 1 && + op.getOperands().front().getType() == + op.getResults().front().getType()) { + rewriter.replaceOp(op, op.getOperands()); + return success(); + } + return failure(); + } +}; + +namespace { + +bool IsShloMainFuncOp(func::FuncOp func_op) { + if (func_op == nullptr) { + return false; + } + + if (!func_op.getSymName().contains(kStablehloModuleDefaultEntryFuncName)) { + return false; + } + + if (func_op.getSymVisibility() == "nested" || + func_op.getSymVisibility() == "private") { + return false; + } + + return true; +} + +// Returns true if XlaCallModuleOp has the "platform index argument". The +// platform index argument is an extra 0-dimensional i32 tensor argument at +// index 0 when the XlaCallModuleOp contains more than one platform specified at +// the "platform" attribute. +// +// See: +// https://github.com/tensorflow/tensorflow/blob/eba24f41ba9d661d2f58a515921720cf90708cd4/tensorflow/compiler/tf2xla/ops/xla_ops.cc#L1376-L1385 +bool ContainsPlatformIndexArg(TF::XlaCallModuleOp xla_call_module_op) { + return xla_call_module_op.getPlatforms().size() > 1; +} + +} // namespace + +class ConvertTFXlaCallModuleOp : public OpRewritePattern { + public: + explicit ConvertTFXlaCallModuleOp(MLIRContext *context, ModuleOp module_op) + : OpRewritePattern(context), module_op_(module_op) {} + using OpRewritePattern::OpRewritePattern; + + private: + ModuleOp module_op_; + LogicalResult matchAndRewrite(TF::XlaCallModuleOp op, + PatternRewriter &rewriter) const override { + OwningOpRef stablehlo_module_op = + stablehlo::deserializePortableArtifact(op.getModuleAttr(), + getContext()); + if (stablehlo_module_op.get() == nullptr) { + return failure(); + } + SymbolTable parent_module_symbol_table(module_op_); + SymbolTable stablehlo_module_symbol_table(stablehlo_module_op.get()); + { + auto main_func_op = stablehlo_module_symbol_table.lookup( + kStablehloModuleDefaultEntryFuncName); + // TODO(b/291988976): move enforcement of this variable outside of this + // rewrite pattern such that it's only checked once. Currently, this + // approach results in duplicate error messages as this pattern executes + // more than once. + if (!IsShloMainFuncOp(main_func_op)) { + auto error_msg = + "'main' FuncOp in XlaCallModuleOp missing or has visibility other " + "than 'public'"; + if (main_func_op) { + main_func_op->emitError(error_msg); + } + return rewriter.notifyMatchFailure(op, error_msg); + } + } + Builder stablehlo_builder(stablehlo_module_op.get().getContext()); + // Rename XlaCallModuleOp's functions to avoid naming conflicts. + for (auto func_op : stablehlo_module_op.get().getOps()) { + const std::string new_func_name = + CreateNewFuncName(func_op.getSymName(), parent_module_symbol_table); + if (failed(stablehlo_module_symbol_table.replaceAllSymbolUses( + func_op, stablehlo_builder.getStringAttr(new_func_name), + stablehlo_module_op.get()))) { + return failure(); + } + SymbolTable::setSymbolName(func_op, new_func_name); + } + // Move all functions from XlaCallModuleOp's stablehlo module, to parent + // module. Also marks the stablehlo module entry function as private. + func::FuncOp main_fn; + for (auto func_op : stablehlo_module_op.get().getOps()) { + func::FuncOp cloned_func_op = func_op.clone(); + if (IsShloMainFuncOp(cloned_func_op)) { + main_fn = cloned_func_op; + } + cloned_func_op.setSymVisibility( + stablehlo_builder.getStringAttr("private")); + parent_module_symbol_table.insert(cloned_func_op); + } + + // When the `XlaCallModuleOp`'s callee accepts a platform index argument, + // add a dummy platform index argument in order to match the number of + // the arguments of the callee function. + // + // This is because `XlaCallModuleOp` doesn't explicitly take it as an + // operand. See: + // https://github.com/tensorflow/tensorflow/blob/eba24f41ba9d661d2f58a515921720cf90708cd4/tensorflow/compiler/tf2xla/ops/xla_ops.cc#L1376-L1385 + + SmallVector call_op_operands(op.getOperands()); + if (ContainsPlatformIndexArg(op)) { + Value dummy_const = TF::ConstOp::create( + rewriter, op.getLoc(), + DenseIntElementsAttr::get( + RankedTensorType::get({}, rewriter.getIntegerType(32)), {0})); + call_op_operands.insert(call_op_operands.begin(), dummy_const); + } + + // The stablehlo module main function's input tensor types might be + // different from the XlaCallModuleOp's input tensor types. For example, + // The XlaCallModuleOp's input is tensor<*xf32> while the function's + // argument type is tensor<1x2f32>. + SmallVector casted_operands; + casted_operands.reserve(main_fn.getNumArguments()); + assert(call_op_operands.size() == main_fn.getNumArguments()); + for (const auto &operand_and_type : + zip(call_op_operands, main_fn.getFunctionType().getInputs())) { + Value operand = std::get<0>(operand_and_type); + Type expected_type = std::get<1>(operand_and_type); + if (operand.getType() != expected_type) { + operand = + TF::CastOp::create(rewriter, op.getLoc(), expected_type, operand, + /*Truncate=*/rewriter.getBoolAttr(false)); + } + casted_operands.push_back(operand); + } + + auto call = + func::CallOp::create(rewriter, op->getLoc(), main_fn.getSymName(), + main_fn.getResultTypes(), casted_operands); + rewriter.replaceOp(op, call->getResults()); + + return success(); + } + + // Creates a new function name to avoid collision. The naming scheme is + // XlaCallModule_%s_%d where %s is the original function name and %d is the + // counter. + std::string CreateNewFuncName(const StringRef func_name, + SymbolTable &symbol_table) const { + int suffix_id = 0; + std::string new_func_name = absl::StrCat(kStablehloFuncNamePrefix, "_", + func_name.str(), "_", suffix_id); + while (symbol_table.lookup(new_func_name)) { + suffix_id++; + new_func_name = absl::StrCat(kStablehloFuncNamePrefix, "_", + func_name.str(), "_", suffix_id); + } + return new_func_name; + } +}; + +class TFXlaCallModuleOpToStablehloPass + : public PassWrapper> { + public: + StringRef getArgument() const final { + return "tf-xla-call-module-op-to-stablehlo-pass"; + } + StringRef getDescription() const final { + return "Legalize TF_XlaCallModule Op to stablehlo"; + } + void getDependentDialects(DialectRegistry ®istry) const override { + registry.insert(); + } + + void runOnOperation() override { + ModuleOp module_op = getOperation(); + RewritePatternSet patterns(&getContext()); + patterns.add(&getContext(), module_op); + patterns.add(&getContext()); + if (failed(applyPatternsGreedily(module_op, std::move(patterns)))) { + return signalPassFailure(); + } + } +}; + +std::unique_ptr> +CreateLegalizeTFXlaCallModuleToStablehloPass() { + return std::make_unique(); +} + +static PassRegistration pass; + +} // namespace odml +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_tf_xla_call_module_to_stablehlo_pass.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_tf_xla_call_module_to_stablehlo_pass.h new file mode 100644 index 0000000000000000000000000000000000000000..c3c42a9b8f8b4a83673a3f0b041235b153427c97 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_tf_xla_call_module_to_stablehlo_pass.h @@ -0,0 +1,35 @@ +/* Copyright 2023 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_TF_XLA_CALL_MODULE_TO_STABLEHLO_PASS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_TF_XLA_CALL_MODULE_TO_STABLEHLO_PASS_H_ + +#include + +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project + +namespace mlir { +namespace odml { + +// Adds passes which transform TF_XlaCallModule Op to StableHLO Ops. +// Note that this pass only supports static shape tensors for now. +std::unique_ptr> +CreateLegalizeTFXlaCallModuleToStablehloPass(); + +} // namespace odml +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_LEGALIZE_TF_XLA_CALL_MODULE_TO_STABLEHLO_PASS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/mhlo_passes/README.md b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/mhlo_passes/README.md new file mode 100644 index 0000000000000000000000000000000000000000..36575dcb208fb2768fb619f96b93c4912c09d041 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/mhlo_passes/README.md @@ -0,0 +1,5 @@ +This temporary directory was created to store MHLO pass .cc and .h files. These +files have been migrated to StableHLO but are still used by inactive or +potentially outdated compilation paths. Once all MHLO passes have been migrated +to StableHLO, revisit this directory. At that point, we can replace the uses of +MHLO passes from this directory with the StableHLO passes. \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/op_stat_pass.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/op_stat_pass.cc new file mode 100644 index 0000000000000000000000000000000000000000..03a9f9d5703e6fe72ee5bb8ceeed87515a3aebbc --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/op_stat_pass.cc @@ -0,0 +1,233 @@ +/* Copyright 2021 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/compiler/mlir/lite/stablehlo/transforms/op_stat_pass.h" + +#include +#include +#include +#include + +#include "absl/log/log.h" +#include "absl/strings/ascii.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/str_format.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/StringMap.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/TypeSwitch.h" +#include "llvm/Support/raw_ostream.h" +#include "mlir/Dialect/Quant/IR/QuantTypes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/Dialect.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/TypeID.h" // from @llvm-project +#include "mlir/Transforms/Passes.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization_traits.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_util.h" + +namespace mlir { +namespace odml { + +// Defines a pass class for print the summary of non-converted ops similar to +// mlir::PrintOpStatsPass, but this pass will show the simpler information. +namespace { +class PrintOpStatsPass : public PassWrapper> { + public: + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(PrintOpStatsPass) + + explicit PrintOpStatsPass(raw_ostream *os = &llvm::errs(), + std::vector accepted_dialects = {}) + : accepted_dialects_(accepted_dialects), os_(os), total_ops_(0) {} + + // Prints the resultant operation statistics pos_t iterating over the module. + void runOnOperation() override; + + // Prints summary of op stats. + void PrintSummary(); + + // Keeps track of dtype counts per op. + void CountOp(DenseMap> &op_count_map, + StringRef op_name, StringRef dtype); + + private: + llvm::StringMap op_with_dialect_count_; + DenseMap> op_dtype_count_; + llvm::StringMap dialect_count_; + llvm::StringMap dialect_name_of_; + llvm::StringMap op_name_of_; + std::vector accepted_dialects_; + std::vector optional_accepted_dialects_; + raw_ostream *os_; + int total_ops_; +}; +} // namespace + +void PrintOpStatsPass::runOnOperation() { + op_with_dialect_count_.clear(); + dialect_count_.clear(); + dialect_name_of_.clear(); + + // Compute the operation statistics for the currently visited operation. + total_ops_ = 0; + + getOperation()->walk([&](Operation *op) { + auto op_with_dialect_name = op->getName().getStringRef(); + auto op_name = op->getName().stripDialect(); + auto dialect_name = op->getDialect()->getNamespace(); + + if (op->getNumResults() > 0 && + isa(op->getResult(0).getType())) { + // Use rhs operand to detect types for dynamic range quantizable ops. + Value value_for_deducing_op_type = + (dyn_cast_or_null(op)) + ? op->getOperand(1) + : op->getResult(0); + ShapedType value_shaped_type = mlir::dyn_cast_or_null( + value_for_deducing_op_type.getType()); + if (value_shaped_type != nullptr) { + auto operand_or_result = value_shaped_type.getElementType(); + std::string dtype; + + TypeSwitch(operand_or_result) + .Case([&](Type) { + dtype = + absl::StrCat("i", operand_or_result.getIntOrFloatBitWidth()); + }) + .Case([&](Type) { + dtype = + absl::StrCat("f", operand_or_result.getIntOrFloatBitWidth()); + }) + .Case([&](Type) { + auto uniform_quantized_dtype = + mlir::dyn_cast_or_null( + operand_or_result) + .getStorageType(); + dtype = absl::StrCat( + "uq_", uniform_quantized_dtype.getIntOrFloatBitWidth()); + }) + .Case([&](Type) { + auto uniform_quantized_dtype = + mlir::dyn_cast_or_null( + operand_or_result) + .getStorageType(); + dtype = absl::StrCat( + "uq_", uniform_quantized_dtype.getIntOrFloatBitWidth()); + }) + .Default([&](Type) { LOG(ERROR) << "Unsupported data type."; }); + CountOp(op_dtype_count_, op_with_dialect_name, dtype); + } + } + ++op_with_dialect_count_[op_with_dialect_name]; + ++dialect_count_[dialect_name]; + dialect_name_of_[op_with_dialect_name] = dialect_name; + op_name_of_[op_with_dialect_name] = op_name; + ++total_ops_; + }); + PrintSummary(); +} + +void PrintOpStatsPass::CountOp( + DenseMap> &op_count_map, + StringRef op_name, StringRef dtype) { + auto &op_counts = op_count_map[op_name]; + if (auto it = op_counts.find(dtype); it != op_counts.end()) { + it->second++; + } else { + op_counts[dtype] = 1; + } +} + +void PrintOpStatsPass::PrintSummary() { + *os_ << "Summary on the non-converted ops:\n"; + *os_ << "---------------------------------\n"; + SmallVector sorted_op(op_with_dialect_count_.keys()); + SmallVector sorted_dialect(dialect_count_.keys()); + llvm::sort(sorted_op); + llvm::sort(sorted_dialect); + + *os_ << " * Accepted dialects: "; + int num_dialect = 0; + // Print the accepted dialect list. + for (const auto &dialect_name : accepted_dialects_) { + *os_ << dialect_name; + if (++num_dialect < accepted_dialects_.size()) { + *os_ << ", "; + } + } + + int converted_ops = 0; + for (const auto &dialect_name : accepted_dialects_) { + converted_ops += dialect_count_[dialect_name]; + } + int non_converted_ops = total_ops_ - converted_ops; + float percentage = + (static_cast(non_converted_ops) / static_cast(total_ops_)) * + 100.0; + // Non-Converted Ops: 25, Total Ops 100, % non-converted = 25% + *os_ << absl::StrFormat( + "\n * Non-Converted Ops: %d, Total Ops %d, %% non-converted = %.2f %%", + non_converted_ops, total_ops_, percentage); + + *os_ << "\n * "; + int num_unaccepted = sorted_dialect.size() - accepted_dialects_.size(); + num_dialect = 0; + // Print the number of unconverted ops in the non-accepted dialects. + for (const auto &dialect_name : sorted_dialect) { + if (!IsAcceptedDialect(dialect_name, accepted_dialects_)) { + *os_ << absl::StrFormat("%d %s ops", dialect_count_[dialect_name], + absl::AsciiStrToUpper(dialect_name)); + if (++num_dialect < num_unaccepted) { + *os_ << ", "; + } + } + } + + *os_ << "\n\n"; + + for (const auto &op_with_dialect_name : sorted_op) { + if (!IsAcceptedOp(dialect_name_of_[op_with_dialect_name], + op_name_of_[op_with_dialect_name], accepted_dialects_)) { + *os_ << absl::StrFormat("- %s: %4d occurrences", op_with_dialect_name, + op_with_dialect_count_[op_with_dialect_name]); + } + + auto op_count_map = op_dtype_count_[op_with_dialect_name]; + if (!op_count_map.empty()) { + std::string delim; + *os_ << " ("; + for (const auto &[dtype, cnt] : op_count_map) { + *os_ << delim << absl::StrFormat("%s: %d", dtype, cnt); + delim = ", "; + } + *os_ << ")"; + } + *os_ << "\n"; + } +} + +} // namespace odml +} // namespace mlir + +std::unique_ptr mlir::odml::createPrintOpStatsPass( + std::vector accepted_dialects) { + return std::make_unique(&llvm::errs(), + accepted_dialects); +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/op_stat_pass.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/op_stat_pass.h new file mode 100644 index 0000000000000000000000000000000000000000..5ef8b33821cea9def257e2488460139f51c6ec64 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/op_stat_pass.h @@ -0,0 +1,36 @@ +/* Copyright 2021 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_OP_STAT_PASS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_OP_STAT_PASS_H_ + +#include +#include +#include + +#include "mlir/Pass/Pass.h" // from @llvm-project + +namespace mlir { +namespace odml { + +// Creates a pass which prints out a detailed report of conversion stats with: +// success or not, % of Ops non-converted, list of non-converted Ops, etc. +std::unique_ptr createPrintOpStatsPass( + std::vector accepted_dialects); + +} // namespace odml +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_OP_STAT_PASS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/optimize.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/optimize.cc new file mode 100644 index 0000000000000000000000000000000000000000..63c817cd00d661e3e9834851740e568624d4949b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/optimize.cc @@ -0,0 +1,777 @@ +/* Copyright 2022 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 "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallVector.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/Location.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/Types.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h" +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir { +namespace odml { + +// Convert mhlo.dot to mhlo.dot_general. +LogicalResult ConvertDotToDotGeneral(mhlo::DotOp op, + PatternRewriter &rewriter) { + auto lhs_type = mlir::cast(op.getLhs().getType()); + auto rhs_type = mlir::cast(op.getRhs().getType()); + if (!lhs_type.hasRank() || !rhs_type.hasRank()) { + return rewriter.notifyMatchFailure(op, "unsupported unranked input type"); + } + if (lhs_type.getRank() < 1 || 2 < lhs_type.getRank() || + rhs_type.getRank() < 1 || 2 < rhs_type.getRank()) { + return rewriter.notifyMatchFailure( + op, + "unsupported dot operation type; operands must be vectors or " + "matrices"); + } + rewriter.replaceOpWithNewOp( + op, op.getType(), op.getLhs(), op.getRhs(), + mhlo::DotDimensionNumbersAttr::get( + op.getContext(), + /*lhsBatchingDimensions=*/{}, + /*rhsBatchingDimensions=*/{}, + /*lhsContractingDimensions=*/{lhs_type.getRank() - 1}, + /*rhsContractingDimensions=*/{0}), + op.getPrecisionConfigAttr(), mhlo::DotAlgorithmAttr{}); + return success(); +} + +// Convert reshape(dot_general(reshape(%y), %z)) to +// dot_general(%y, %z) if possible. +LogicalResult RemoveReshapeAroundDotGeneral(mhlo::ReshapeOp reshape_after, + PatternRewriter &rewriter) { + auto dot = dyn_cast_or_null( + reshape_after.getOperand().getDefiningOp()); + if (!dot) return failure(); + + auto reshape_before = + dyn_cast_or_null(dot.getLhs().getDefiningOp()); + if (!reshape_before) return failure(); + + if (!dot.getLhs().getType().hasStaticShape() || + !dot.getRhs().getType().hasStaticShape() || + !reshape_before.getOperand().getType().hasStaticShape() || + !dot.getType().hasStaticShape() || + !reshape_after.getType().hasStaticShape()) { + return rewriter.notifyMatchFailure(reshape_after, + "dynamic shapes not supported"); + } + + const auto range = [](int64_t begin, int n) { + SmallVector result; + result.reserve(n); + for (int i = 0; i < n; ++i) { + result.push_back(i + begin); + } + return result; + }; + + // We only support the mhlo.dot style input layouts, i.e.: + // lhs: [batch, other dims, contract dims] + // rhs: [batch, contract dims, other dims] + auto dim_nums = dot.getDotDimensionNumbers(); + int batch_dims_count = dim_nums.getLhsBatchingDimensions().size(); + int contracting_dims_count = dim_nums.getLhsContractingDimensions().size(); + if (dim_nums.getLhsBatchingDimensions() != + ArrayRef(range(0, batch_dims_count)) || + dim_nums.getRhsBatchingDimensions() != + ArrayRef(range(0, batch_dims_count)) || + dim_nums.getLhsContractingDimensions() != + ArrayRef( + range(dot.getLhs().getType().getRank() - contracting_dims_count, + contracting_dims_count)) || + dim_nums.getRhsContractingDimensions() != + ArrayRef(range(batch_dims_count, contracting_dims_count))) { + return rewriter.notifyMatchFailure(reshape_after, + "unsupported dot_general layout"); + } + + // (B = batch dims, C = contracting dims, Y/Z = other dims) + // + // This pattern converts: + // %before = "mhlo.reshape"(%lhs : BxY1xC) : BxY2xC + // %dot = "mhlo.dot_general"(%before, %rhs : BxCxZ) : BxY2xZ + // %after = "mhlo.reshape"(%dot) : BxY1xZ + // to: + // %dot : "mhlo.dot_general"(%lhs : BxY1xC, %rhs : BxCxZ) : BxY1xZ + + // Extract B, Y1, C from %lhs. + ArrayRef shape_lhs = + reshape_before.getOperand().getType().getShape(); + ArrayRef shape_b = shape_lhs.take_front(batch_dims_count); + ArrayRef shape_c = shape_lhs.take_back(contracting_dims_count); + ArrayRef shape_y1 = + shape_lhs.drop_front(shape_b.size()).drop_back(shape_c.size()); + + // Check %before shape, and extract Y2 from it. + ArrayRef shape_before = reshape_before.getType().getShape(); + if (shape_before.take_front(shape_b.size()) != shape_b || + shape_before.take_back(shape_c.size()) != shape_c) { + return failure(); + } + ArrayRef shape_y2 = + shape_before.drop_front(shape_b.size()).drop_back(shape_c.size()); + + // No need to check %dot; dot_general verifier ensures correct shapes. + // Extract Z from %dot. + ArrayRef shape_z = + dot.getType().getShape().drop_front(shape_b.size() + shape_y2.size()); + + // Check %after shape. + if (reshape_after.getType().getShape() != + ArrayRef(llvm::to_vector( + llvm::concat(shape_b, shape_y1, shape_z)))) { + return failure(); + } + + rewriter.replaceOpWithNewOp( + reshape_after, reshape_after.getType(), reshape_before.getOperand(), + dot.getRhs(), + mhlo::DotDimensionNumbersAttr::get( + reshape_after.getContext(), + /*lhsBatchingDimensions=*/range(0, batch_dims_count), + /*rhsBatchingDimensions=*/range(0, batch_dims_count), + /*lhsContractingDimensions=*/ + range(batch_dims_count + shape_y1.size(), contracting_dims_count), + /*rhsContractingDimensions=*/ + range(batch_dims_count, contracting_dims_count)), + dot.getPrecisionConfigAttr(), dot.getAlgorithmAttr()); + return success(); +} + +// Convert: +// %y0 = dot_general(%x0, %w) +// %y1 = dot_general(%x1, %w) +// ... +// concatenate(%y0, %y1, ...) +// To: +// %x = concatenate(%x0, %x1, ...) +// dot_general(%x, %w) +LogicalResult LiftDotConcatLHS(mhlo::ConcatenateOp concat, + PatternRewriter &rewriter) { + if (concat.getVal().size() < 2) + return rewriter.notifyMatchFailure( + concat, "Concatenate op should have at least two operands"); + + auto first_dot = concat.getVal()[0].getDefiningOp(); + if (!first_dot) + return rewriter.notifyMatchFailure(concat, "Operand is not dot_general"); + if (!first_dot.getLhs().getType().hasStaticShape()) + return rewriter.notifyMatchFailure( + first_dot, "All dot_general LHS must be statically shaped"); + if (!first_dot->hasOneUse()) + return rewriter.notifyMatchFailure(first_dot, "Op has multiple uses"); + + SmallVector all_dot_lhs; + all_dot_lhs.reserve(concat.getVal().size()); + all_dot_lhs.push_back(first_dot.getLhs()); + + const uint64_t batch_dims_count = + first_dot.getDotDimensionNumbers().getLhsBatchingDimensions().size(); + const uint64_t contracting_dims_count = + first_dot.getDotDimensionNumbers().getLhsContractingDimensions().size(); + const uint64_t lhs_other_dims_count = first_dot.getLhs().getType().getRank() - + batch_dims_count - + contracting_dims_count; + + // This pattern only supports concating on LHS other dims (neither batch nor + // contracting). + if (concat.getDimension() < batch_dims_count || + concat.getDimension() >= batch_dims_count + lhs_other_dims_count) { + return rewriter.notifyMatchFailure(concat, + "Not concating on LHS other dims"); + } + + for (auto value : concat.getVal().drop_front()) { + auto dot = value.getDefiningOp(); + if (!dot) + return rewriter.notifyMatchFailure(concat, "Operand is not dot_general"); + + if (dot.getRhs() != first_dot.getRhs()) + return rewriter.notifyMatchFailure( + dot, "dot_general ops have different rhs parameters"); + if (dot.getDotDimensionNumbers() != first_dot.getDotDimensionNumbers()) + return rewriter.notifyMatchFailure( + dot, "dot_general ops have different dimension numbers"); + if (dot.getPrecisionConfig() != first_dot.getPrecisionConfig()) + return rewriter.notifyMatchFailure( + dot, "dot_general ops have different precision configs"); + if (!dot.getLhs().getType().hasStaticShape()) + return rewriter.notifyMatchFailure( + dot, "all dot_general LHS must be statically shaped"); + if (dot.getLhs().getType().getElementType() != + first_dot.getLhs().getType().getElementType() || + dot.getType().getElementType() != first_dot.getType().getElementType()) + return rewriter.notifyMatchFailure( + dot, "all dot_general ops must have the same element type"); + if (!dot->hasOneUse()) + return rewriter.notifyMatchFailure(dot, "Op has multiple uses"); + + all_dot_lhs.push_back(dot.getLhs()); + } + + const auto is_lhs_batch_or_contracting_dim = [&](uint64_t dim) { + auto dim_nums = first_dot.getDotDimensionNumbers(); + return llvm::is_contained(dim_nums.getLhsBatchingDimensions(), dim) || + llvm::is_contained(dim_nums.getLhsContractingDimensions(), dim); + }; + + // dot_general outputs are always in the + // [batch dims, LHS other dims, RHS other dims] + // layout, so the new concat dim is where the n-th (base-0 counting) LHS other + // dim appears in the original LHS layout, where: + // n = old_concat_dim - batch_dims_count + uint64_t n = concat.getDimension() - batch_dims_count; + + // Now try to answer where the n-th LHS other dim was originally placed. + // This is the dimension we should now concat on. + int new_concat_dim = -1; + for (int i = 0; i < first_dot.getLhs().getType().getRank(); ++i) { + if (!is_lhs_batch_or_contracting_dim(i) && n-- == 0) { + new_concat_dim = i; + break; + } + } + + // Now get the output shape of the lifted concat op. + SmallVector new_concat_shape( + first_dot.getLhs().getType().getShape()); + new_concat_shape[new_concat_dim] = 0; + for (auto v : all_dot_lhs) { + new_concat_shape[new_concat_dim] += + mlir::dyn_cast(v.getType()).getShape()[new_concat_dim]; + } + + auto new_concat = mhlo::ConcatenateOp::create( + rewriter, concat->getLoc(), concat.getType().clone(new_concat_shape), + all_dot_lhs, rewriter.getI64IntegerAttr(new_concat_dim)); + rewriter.replaceOpWithNewOp( + concat, concat.getType(), new_concat, first_dot.getRhs(), + first_dot.getDotDimensionNumbers(), first_dot.getPrecisionConfigAttr(), + first_dot.getAlgorithmAttr()); + return success(); +} + +// Convert: +// %y0 = dot_general(%x0, %w0) +// %y1 = dot_general(%x1, %w1) +// ... +// concatenate(%y0, %y1, ...) +// To: +// %x = concatenate(%x0, %x1, ...) +// %w = concatenate(%w0, %w1, ...) +// dot_general(%x, %w) +// +// To simplify the implementation, we only handle the case where the final +// concat is on the only batching dim. +LogicalResult LiftDotConcatLHSAndRHS(mhlo::ConcatenateOp concat, + PatternRewriter &rewriter) { + if (concat.getVal().size() < 2) + return rewriter.notifyMatchFailure( + concat, "Concatenate op should have at least two operands"); + + auto first_dot = concat.getVal()[0].getDefiningOp(); + if (!first_dot) + return rewriter.notifyMatchFailure(concat, "Operand is not dot_general"); + if (!first_dot.getLhs().getType().hasStaticShape()) + return rewriter.notifyMatchFailure( + first_dot, "All dot_general LHS must be statically shaped"); + if (!first_dot->hasOneUse()) + return rewriter.notifyMatchFailure(first_dot, "Op has multiple uses"); + + SmallVector all_dot_lhs; + all_dot_lhs.reserve(concat.getVal().size()); + all_dot_lhs.push_back(first_dot.getLhs()); + SmallVector all_dot_rhs; + all_dot_rhs.reserve(concat.getVal().size()); + all_dot_rhs.push_back(first_dot.getRhs()); + + if (first_dot.getDotDimensionNumbers().getLhsBatchingDimensions().size() != 1) + return rewriter.notifyMatchFailure(first_dot, "One batching dim required"); + if (concat.getDimension() != 0) + return rewriter.notifyMatchFailure( + concat, "Not concating on the first batching dim"); + + for (auto value : concat.getVal().drop_front()) { + auto dot = value.getDefiningOp(); + if (!dot) + return rewriter.notifyMatchFailure(concat, "Operand is not dot_general"); + + if (dot.getDotDimensionNumbers() != first_dot.getDotDimensionNumbers()) + return rewriter.notifyMatchFailure( + dot, "dot_general ops have different dimension numbers"); + if (dot.getPrecisionConfig() != first_dot.getPrecisionConfig()) + return rewriter.notifyMatchFailure( + dot, "dot_general ops have different precision configs"); + if (!dot.getLhs().getType().hasStaticShape() || + !dot.getRhs().getType().hasStaticShape()) + return rewriter.notifyMatchFailure( + dot, "all dot_general operands must be statically shaped"); + if (dot.getLhs().getType().getElementType() != + first_dot.getLhs().getType().getElementType() || + dot.getRhs().getType().getElementType() != + first_dot.getRhs().getType().getElementType() || + dot.getType().getElementType() != first_dot.getType().getElementType()) + return rewriter.notifyMatchFailure( + dot, "all dot_general ops must have the same element type"); + if (!dot->hasOneUse()) + return rewriter.notifyMatchFailure(dot, "Op has multiple uses"); + + all_dot_lhs.push_back(dot.getLhs()); + all_dot_rhs.push_back(dot.getRhs()); + } + + // Now get the output shapes of the lifted concat ops. + const int64_t lhs_batch_dim = + first_dot.getDotDimensionNumbers().getLhsBatchingDimensions()[0]; + SmallVector lhs_new_concat_shape( + first_dot.getLhs().getType().getShape()); + lhs_new_concat_shape[lhs_batch_dim] = 0; + for (auto v : all_dot_lhs) { + lhs_new_concat_shape[lhs_batch_dim] += + mlir::dyn_cast(v.getType()).getShape()[lhs_batch_dim]; + } + const int64_t rhs_batch_dim = + first_dot.getDotDimensionNumbers().getRhsBatchingDimensions()[0]; + SmallVector rhs_new_concat_shape( + first_dot.getRhs().getType().getShape()); + rhs_new_concat_shape[rhs_batch_dim] = 0; + for (auto v : all_dot_rhs) { + rhs_new_concat_shape[rhs_batch_dim] += + mlir::dyn_cast(v.getType()).getShape()[rhs_batch_dim]; + } + + auto lhs_new_concat = mhlo::ConcatenateOp::create( + rewriter, concat->getLoc(), concat.getType().clone(lhs_new_concat_shape), + all_dot_lhs, rewriter.getI64IntegerAttr(lhs_batch_dim)); + auto rhs_new_concat = mhlo::ConcatenateOp::create( + rewriter, concat->getLoc(), concat.getType().clone(rhs_new_concat_shape), + all_dot_rhs, rewriter.getI64IntegerAttr(rhs_batch_dim)); + rewriter.replaceOpWithNewOp( + concat, concat.getType(), lhs_new_concat, rhs_new_concat, + first_dot.getDotDimensionNumbers(), first_dot.getPrecisionConfigAttr(), + first_dot.getAlgorithmAttr()); + return success(); +} + +// Convert: +// %y0 = slice(%x, start=0, limit=2) +// %y1 = slice(%x, start=2, limit=3) +// concat(%y0, %y1, ...) +// To: +// %y = slice(%x, start=0, limit=3) +// concat(%y, ...) +LogicalResult FuseSliceConcat(mhlo::ConcatenateOp concat, + PatternRewriter &rewriter) { + if (concat.getVal().size() < 2) + return rewriter.notifyMatchFailure( + concat, "Concatenate op should have at least two operands"); + + auto first = concat.getVal()[0].getDefiningOp(); + auto second = concat.getVal()[1].getDefiningOp(); + if (!first || !second) + return rewriter.notifyMatchFailure(concat, "operands are not slice ops"); + if (first.getOperand() != second.getOperand()) + return rewriter.notifyMatchFailure(concat, "slice not on the same input"); + if (!first.getStrides().isSplat() || + first.getStrides().getSplatValue().getInt() != 1 || + first.getStrides() != second.getStrides()) + return rewriter.notifyMatchFailure(concat, "slice ops must have stride=1"); + if (!first->hasOneUse() || !second->hasOneUse()) + return rewriter.notifyMatchFailure(concat, "slice ops are used elsewhere"); + + SmallVector new_start; + SmallVector new_limit; + SmallVector new_slice_shape; + new_start.reserve(first.getStrides().size()); + new_limit.reserve(first.getStrides().size()); + new_slice_shape.reserve(first.getStrides().size()); + + for (int i = 0; i < first.getStrides().size(); ++i) { + const int64_t first_start = + first.getStartIndicesAttr().getValues()[i].getInt(); + const int64_t first_limit = + first.getLimitIndicesAttr().getValues()[i].getInt(); + const int64_t second_start = + second.getStartIndicesAttr().getValues()[i].getInt(); + const int64_t second_limit = + second.getLimitIndicesAttr().getValues()[i].getInt(); + + if (i == concat.getDimension()) { + if (first_limit != second_start) + return rewriter.notifyMatchFailure( + second, "slice is not continuous with previous slice"); + } else { + if (first_start != second_start || first_limit != second_limit) + return rewriter.notifyMatchFailure( + second, "non-concat dims have mismatching slice bounds"); + } + + new_start.push_back(first_start); + new_limit.push_back(second_limit); + new_slice_shape.push_back(second_limit - first_start); + } + + auto new_slice = mhlo::SliceOp::create( + rewriter, + FusedLoc::get(first->getContext(), {first.getLoc(), second.getLoc()}), + first.getType().clone(new_slice_shape), first.getOperand(), + /*start_indices=*/rewriter.getI64TensorAttr(new_start), + /*limit_indices=*/rewriter.getI64TensorAttr(new_limit), + /*strides=*/first.getStrides()); + + SmallVector new_concat_values; + new_concat_values.reserve(concat.getVal().size() - 1); + new_concat_values.push_back(new_slice); + llvm::append_range(new_concat_values, concat.getVal().drop_front(2)); + + rewriter.replaceOpWithNewOp( + concat, concat.getType(), new_concat_values, concat.getDimension()); + return success(); +} + +// TODO(b/296267494): Move this provided constfolding runs prior to it +// Converts: +// %y1 = pad(%x, pad_val, (p1_1,p1_2,p1_3, ...)) +// %y2 = pad(%y1, pad_val, (p2_1,p2_2,p2_3, ...)) +// To: +// %z = pad(%x, pad_val, (p1_1 + p2_1, p1_2 + p2_2, p1_3 + p2_3, ...)) +LogicalResult MergeConsecutivePad(mhlo::PadOp pad_op, + PatternRewriter &rewriter) { + // Fail for non-static shapes + if (!pad_op.getOperand().getType().hasStaticShape() || + !pad_op.getResult().getType().hasStaticShape() || + !pad_op.getPaddingValue().getType().hasStaticShape()) { + return rewriter.notifyMatchFailure(pad_op, "dynamic shapes not supported"); + } + + // Check if the operand is also a Pad op + auto parent_pad = + dyn_cast_or_null(pad_op.getOperand().getDefiningOp()); + if (!parent_pad) { + return rewriter.notifyMatchFailure(pad_op, "parent is not a pad operator"); + } + + // We need the parent pad to have exactly one use (which is the child pad), + // otherwise merging the two pads will create wrong shapes for the other + // users. + if (!parent_pad->hasOneUse()) { + return rewriter.notifyMatchFailure(pad_op, + "parent pad has more than one use"); + } + + // Fail for non-static shapes + if (!parent_pad.getOperand().getType().hasStaticShape() || + !parent_pad.getResult().getType().hasStaticShape() || + !parent_pad.getPaddingValue().getType().hasStaticShape()) { + return rewriter.notifyMatchFailure(parent_pad, + "dynamic shapes not supported"); + } + + // Check if the padding values are equal (otherwise merging is illegal) + // Because we are using the greedy pattern rewrite driver + // (applyPatternsGreedily), all different constant operators with the + // same value will be replaced by a single constant operator of that value. + // Due to this, if the padding values in the input are equal, they will become + // the same constant operator and the following check (which compares memory + // addresses) works. + if (pad_op.getPaddingValue() != parent_pad.getPaddingValue()) { + return rewriter.notifyMatchFailure( + pad_op, "parent and child pad have different padding values"); + } + + // NOTE: Because negative paddings are allowed, we assert that if + // `parent_pad < 0` then `child_pad <= 0` The effect of the negative pad is to + // remove values, so for example if we have parent_pad = - 1, child_pad = 1 + // the merged pad will not change anything, while the un-merged will remove a + // value, then insert a 0 at its place. This only holds for low and high pads, + // the spec does not allow negative interior pads, so we don't check there. + auto low_pads = pad_op.getEdgePaddingLow().getValues(); + auto parent_low_pads = + parent_pad.getEdgePaddingLow().getValues(); + auto high_pads = pad_op.getEdgePaddingHigh().getValues(); + auto parent_high_pads = + parent_pad.getEdgePaddingHigh().getValues(); + auto interior_pads = pad_op.getInteriorPadding().getValues(); + auto parent_interior_pads = + parent_pad.getInteriorPadding().getValues(); + + // NOTE: Low/High/Interior pads have the same size + for (int i = 0; i < low_pads.size(); ++i) { + if (parent_low_pads[i].getInt() < 0 && low_pads[i].getInt() > 0) { + return rewriter.notifyMatchFailure( + pad_op, "can't merge consecutive negative and positive low pads"); + } + if (parent_high_pads[i].getInt() < 0 && high_pads[i].getInt() > 0) { + return rewriter.notifyMatchFailure( + pad_op, "can't merge consecutive negative and positive high pads"); + } + } + + std::vector new_low_pads(low_pads.size(), 0); + std::vector new_high_pads(high_pads.size(), 0); + std::vector new_interior_pads(interior_pads.size(), 0); + + for (int i = 0; i < low_pads.size(); ++i) { + new_low_pads[i] = low_pads[i].getInt() + parent_low_pads[i].getInt(); + new_high_pads[i] = high_pads[i].getInt() + parent_high_pads[i].getInt(); + new_interior_pads[i] = + interior_pads[i].getInt() + parent_interior_pads[i].getInt(); + } + + // Replace pad_op with a new pad having new attributes, taking the + // parent_pad's operand. (After this parent_pad has no users and is removed). + rewriter.replaceOpWithNewOp( + pad_op, pad_op.getType(), parent_pad.getOperand(), + parent_pad.getPaddingValue(), rewriter.getI64TensorAttr(new_low_pads), + rewriter.getI64TensorAttr(new_high_pads), + rewriter.getI64TensorAttr(new_interior_pads)); + return success(); +} + +// Convert: +// %input : 1xYxC +// %1 = mhlo.reshape %param : (1xCxZ) -> CxZ +// mhlo.dot_general %input, %1 {batch_dims = []} +// To: +// mhlo.dot_general %input, %param {batch_dims = [0]} +// +// This usage will mostly come from tf-unroll-batch-matmul, so it's fine to only +// handle the case where batching dim is the leftmost dim. +LogicalResult ConvertReshapeDotRhsToBatchedDot(mhlo::DotGeneralOp dot, + PatternRewriter &rewriter) { + mhlo::DotDimensionNumbersAttr dim_nums = dot.getDotDimensionNumbers(); + if (!dim_nums.getLhsBatchingDimensions().empty()) return failure(); + + auto reshape = dot.getRhs().getDefiningOp(); + if (!reshape) return failure(); + if (!reshape->hasOneUse()) + return rewriter.notifyMatchFailure(reshape, "reshape has multiple usages"); + if (!reshape.getType().hasStaticShape() || + !reshape.getOperand().getType().hasStaticShape() || + !dot.getLhs().getType().hasStaticShape()) { + return rewriter.notifyMatchFailure(dot, "dynamic shaping not supported"); + } + + ArrayRef orig_param_shape = + reshape.getOperand().getType().getShape(); + ArrayRef dot_param_shape = reshape.getType().getShape(); + if (orig_param_shape.size() != dot_param_shape.size() + 1 || + orig_param_shape.front() != 1) { + return rewriter.notifyMatchFailure(reshape, "unsupported reshape pattern"); + } + + int lhs_first_other_dim = -1; + for (int i = 0; i < dot.getLhs().getType().getRank(); ++i) { + if (!llvm::is_contained(dim_nums.getLhsContractingDimensions(), i)) { + lhs_first_other_dim = i; + break; + } + } + if (lhs_first_other_dim == -1 || + dot.getLhs().getType().getShape()[lhs_first_other_dim] != 1) { + return rewriter.notifyMatchFailure(dot, "unsupported LHS shape"); + } + + SmallVector new_rhs_contracting_dims; + new_rhs_contracting_dims.reserve( + dim_nums.getRhsContractingDimensions().size()); + for (int64_t d : dim_nums.getRhsContractingDimensions()) { + new_rhs_contracting_dims.push_back(d + 1); + } + + rewriter.replaceOpWithNewOp( + dot, dot.getType(), dot.getLhs(), reshape.getOperand(), + mhlo::DotDimensionNumbersAttr::get( + dot.getContext(), + /*lhsBatchingDimensions=*/{lhs_first_other_dim}, + /*rhsBatchingDimensions=*/{0}, + /*lhsContractingDimensions=*/dim_nums.getLhsContractingDimensions(), + /*rhsContractingDimensions=*/new_rhs_contracting_dims), + dot.getPrecisionConfigAttr(), dot.getAlgorithmAttr()); + return success(); +} + +//===----------------------------------------------------------------------===// +// BroadcastInDimsOp +//===----------------------------------------------------------------------===// + +// Minimizing unit dimensions in reshape(broadcast(X)). +// +// There are situations where X, or broadcast(X) have some number of `1` (unit) +// sized dimensions which are not meaningful to the computation. E.g. +// +// ``` +// x = [1x1x1x3] +// b = broadast(x) : [1x2x1x3] +// r = reshape(b) : [2x3] +// ``` +// +// Provided the relative broadcast dims are preserved, removing any number +// of unit dims from the input or output shape of a broadcast has no effect on +// the semantic of the computation. +// +// Assume a reshape(broadcast(x)) where the shape of the broadcast and reshape +// have the same non-unit dims in the same order. In this case we can +// change the broadcast shape into the reshape shape simply by adding or +// removing unit-dims, and the reshape can be replaced with the broadcast. +// +// When removing unit dims from the broadcast in this way, we may also need +// to remove the corresponding unit dim from the input shape. This pattern takes +// the approach of removing all unit dims for the broadcast input +// rather than explicitly checking each. +// +// The result on the above example: +// +// ``` +// x = [1x1x1x3] +// r = reshape(x) : [3] +// b = broadast(r) : [2x3] +// ``` +// +// Note that the ability of removing unit dims from the input or output shape of +// a broascast is not contingent on matching and replacing a reshaped output. We +// require however for this pattern to not increase the net number of reshapes. +// Additionally, we want to minimize the rank of broadcasts so only considered +// are cases where rank(reshape) < rank(broadcast). +class SimplifyBroadcastInDimsReshape + : public OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + LogicalResult matchAndRewrite(mhlo::BroadcastInDimOp op, + PatternRewriter &rewriter) const override { + if (!op->hasOneUse()) { + return rewriter.notifyMatchFailure(op, "has more than one use."); + } + + auto reshape = mlir::dyn_cast(*op->getUsers().begin()); + if (!reshape) { + return rewriter.notifyMatchFailure(op, "user not reshape."); + } + + auto broadcast_type = mlir::cast(op.getType()); + auto broadcast_input_type = + mlir::cast(op.getOperand().getType()); + auto reshape_type = mlir::cast(reshape.getType()); + + // Reshape must be squeezing unit dimensions. + if (!(reshape_type.getRank() < broadcast_type.getRank())) { + return rewriter.notifyMatchFailure(op, "reshape doesn't reduce rank."); + } + + // Reshape and broadcast must have the same non-unit dims in the + // same order. + llvm::SmallVector broadcast_dim_to_reshape_dim( + broadcast_type.getRank()); + int64_t reshape_dim_idx = -1; + for (auto [idx, dim] : llvm::enumerate(broadcast_type.getShape())) { + if (dim == 1) { + continue; + } + + int64_t reshape_dim_size = 1; + while (reshape_dim_idx < reshape_type.getRank() - 1) { + reshape_dim_size = reshape_type.getDimSize(++reshape_dim_idx); + if (reshape_dim_size != 1) { + break; + } + } + + if (dim != reshape_dim_size) { + return rewriter.notifyMatchFailure( + op, "reshape and broadcast have different non-unit dim sizes."); + } + + // Maps index of non-unit broadcast dims to corresponding reshape dim. + broadcast_dim_to_reshape_dim[idx] = reshape_dim_idx; + } + // Unchecked reshape dim sizes are guaranteed to be unit at this point. + + llvm::SmallVector current_broadcast_dims( + op.getBroadcastDimensions().getValues()); + llvm::SmallVector new_broadcast_dims; + llvm::SmallVector new_broadcast_input_shape; + + for (auto [idx, dim] : llvm::enumerate(broadcast_input_type.getShape())) { + if (dim == 1) { + continue; + } + // If dim != 1 then it must be broadcasted to a non-unit dimension + // and must have a corresponding reshape dimension in our vectors. + new_broadcast_dims.push_back( + broadcast_dim_to_reshape_dim[current_broadcast_dims[idx]]); + new_broadcast_input_shape.push_back(dim); + } + + auto new_broadcast_input_type = RankedTensorType::get( + new_broadcast_input_shape, broadcast_type.getElementType()); + auto new_broadcast_input = mhlo::ReshapeOp::create( + rewriter, op->getLoc(), new_broadcast_input_type, op.getOperand()); + auto new_broadcast_dims_attr = + rewriter.getI64TensorAttr(new_broadcast_dims); + + rewriter.replaceOpWithNewOp( + reshape, reshape_type, new_broadcast_input, new_broadcast_dims_attr); + + return success(); + } +}; + +class OptimizePass + : public PassWrapper> { + public: + StringRef getArgument() const final { return "mhlo-optimize"; } + StringRef getDescription() const final { + return "Applies various optimizations on MHLO IR"; + } + + void runOnOperation() override { + RewritePatternSet patterns(&getContext()); + patterns.add(ConvertDotToDotGeneral); + patterns.add(RemoveReshapeAroundDotGeneral); + patterns.add(LiftDotConcatLHS); + patterns.add(LiftDotConcatLHSAndRHS); + patterns.add(FuseSliceConcat); + patterns.add(ConvertReshapeDotRhsToBatchedDot); + patterns.add(MergeConsecutivePad); + patterns.add(&getContext()); + if (failed(applyPatternsGreedily(getOperation(), std::move(patterns)))) { + return signalPassFailure(); + } + } +}; + +std::unique_ptr createOptimizePass() { + return std::make_unique(); +} + +static PassRegistration pass; + +} // namespace odml +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/optimize_layout.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/optimize_layout.cc new file mode 100644 index 0000000000000000000000000000000000000000..cee8ccccb27023c48cf8648132db55ae84fda8cb --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/optimize_layout.cc @@ -0,0 +1,209 @@ +/* Copyright 2024 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. +==============================================================================*/ + +// This file implements logic for legalizing HLO to TensorFlow. + +#include +#include +#include +#include +#include + +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/SmallVector.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Dialect/Utils/IndexingUtils.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/Diagnostics.h" // from @llvm-project +#include "mlir/IR/IRMapping.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/IR/ValueRange.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" // from @llvm-project +#include "stablehlo/dialect/StablehloOps.h" // from @stablehlo +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h" + +namespace mlir { +namespace odml { +namespace { + +#define DEBUG_TYPE "stablehlo-optimize-layout" + +#define GEN_PASS_DEF_TRANSPOSECOMMUTEOPSPASS +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h.inc" + +class TransposeCommuteOpsPass + : public impl::TransposeCommuteOpsPassBase { + void runOnOperation() override; +}; + +// Inversely permutate a given vector +static SmallVector InvertPermutationToVector(ArrayRef vec, + ArrayRef perm) { + return applyPermutation(vec, invertPermutationVector(perm)); +} + +static RankedTensorType GetPermutedTensorTypeHelper(RankedTensorType type, + ArrayRef perm, + bool isInvert) { + SmallVector permutedShape = applyPermutation( + type.getShape(), isInvert ? invertPermutationVector(perm) : perm); + return RankedTensorType::get(permutedShape, type.getElementType()); +} + +static RankedTensorType GetInvertPermutedTensorType(RankedTensorType type, + ArrayRef perm) { + return GetPermutedTensorTypeHelper(type, perm, true /*isInvert*/); +} + +static Value CreateTranspose(OpBuilder& builder, Value source, + ArrayRef perm) { + return stablehlo::TransposeOp::create(builder, source.getLoc(), source, perm) + ->getResult(0); +} + +// Transform pad(transpose(x)) to transpose(pad(x)) +struct TransposeCommuteWithPad : public OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(stablehlo::PadOp pad_op, + PatternRewriter& rewriter) const override { + Value pad_input = pad_op.getOperand(); + RankedTensorType pad_type = mlir::cast(pad_op.getType()); + + auto transpose_op = pad_input.getDefiningOp(); + if (!transpose_op || !transpose_op->hasOneUse()) return failure(); + Value transpose_input = transpose_op.getOperand(); + + ArrayRef transpose_perm = transpose_op.getPermutation(); + SmallVector new_padding_low = + InvertPermutationToVector(pad_op.getEdgePaddingLow(), transpose_perm); + SmallVector new_padding_high = + InvertPermutationToVector(pad_op.getEdgePaddingHigh(), transpose_perm); + SmallVector new_padding_interrier = + InvertPermutationToVector(pad_op.getInteriorPadding(), transpose_perm); + + RankedTensorType new_pad_type = + GetInvertPermutedTensorType(pad_type, transpose_perm); + Value new_pad = stablehlo::PadOp::create( + rewriter, pad_op.getLoc(), new_pad_type, transpose_input, + pad_op.getPaddingValue(), new_padding_low, new_padding_high, + new_padding_interrier); + + Value orig_pad = CreateTranspose(rewriter, new_pad, transpose_perm); + rewriter.replaceOp(pad_op, orig_pad); + return success(); + } +}; + +// Transform reduce_window(transpose(x)) to transpose(reduce_window(x)) +struct TransposeCommuteWithReduceWindow + : public OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(stablehlo::ReduceWindowOp reduce_op, + PatternRewriter& rewriter) const override { + MLIRContext* ctx = reduce_op.getContext(); + ValueRange inputs = reduce_op.getInputs(); + // Only handle binary reduce ops for now + if (inputs.size() != 1) return failure(); + Value reduce_input = inputs[0]; + + RankedTensorType reduce_type = + mlir::cast(reduce_op.getResultTypes()[0]); + + auto transpose_op = reduce_input.getDefiningOp(); + if (!transpose_op || !transpose_op->hasOneUse()) return failure(); + Value transpose_input = transpose_op.getOperand(); + + ArrayRef transpose_perm = transpose_op.getPermutation(); + + // Inversely transposes all the attributes to prepare for the new reduce op + auto getInvertPermutedAttr = + [&](std::optional> vals) -> DenseI64ArrayAttr { + return vals.has_value() + ? DenseI64ArrayAttr::get( + ctx, InvertPermutationToVector(*vals, transpose_perm)) + : nullptr; + }; + DenseI64ArrayAttr new_window_dimensions = + getInvertPermutedAttr(reduce_op.getWindowDimensions()); + DenseI64ArrayAttr new_window_strides = + getInvertPermutedAttr(reduce_op.getWindowStrides()); + DenseI64ArrayAttr new_base_dilations = + getInvertPermutedAttr(reduce_op.getBaseDilations()); + DenseI64ArrayAttr new_win_dilations = + getInvertPermutedAttr(reduce_op.getWindowDilations()); + + auto padding = reduce_op.getPadding(); + int64_t rank = transpose_perm.size(); + DenseIntElementsAttr new_padding_attr = nullptr; + if (padding.has_value()) { + SmallVector new_padding(rank * 2, 0); + auto old_padding = (*padding).getValues(); + for (int64_t idx = 0; idx < rank; ++idx) { + new_padding[2 * transpose_perm[idx]] = old_padding[2 * idx]; + new_padding[2 * transpose_perm[idx] + 1] = old_padding[2 * idx + 1]; + } + new_padding_attr = + DenseIntElementsAttr::get((*padding).getType(), new_padding); + } + + RankedTensorType new_reduce_type = + GetInvertPermutedTensorType(reduce_type, transpose_perm); + auto new_reduce_op = stablehlo::ReduceWindowOp::create( + rewriter, reduce_op.getLoc(), new_reduce_type, transpose_input, + reduce_op.getInitValues()[0], new_window_dimensions, new_window_strides, + new_base_dilations, new_win_dilations, new_padding_attr); + IRMapping mapping; + reduce_op.getBody().cloneInto(&new_reduce_op.getBody(), mapping); + + Value orig_reduce_op = + CreateTranspose(rewriter, new_reduce_op->getResult(0), transpose_perm); + rewriter.replaceOp(reduce_op, orig_reduce_op); + return success(); + } +}; + +void TransposeCommuteOpsPass::runOnOperation() { + auto* ctx = &getContext(); + + RewritePatternSet patterns(ctx); + patterns.add(ctx); + if (failed(applyPatternsGreedily(getOperation(), std::move(patterns)))) { + return signalPassFailure(); + } +} + +} // end namespace + +std::unique_ptr> CreateTransposeCommuteOpsPass() { + return std::make_unique(); +} + +static PassRegistration pass; + +} // end namespace odml +} // end namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/prepare_hlo.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/prepare_hlo.cc new file mode 100644 index 0000000000000000000000000000000000000000..0da201f1195bd882df5bc8fbe4a56a6c395e3741 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/prepare_hlo.cc @@ -0,0 +1,79 @@ +/* Copyright 2023 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 "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project // IWYU pragma: keep +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Support/TypeID.h" // from @llvm-project +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" // IWYU pragma: keep +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/conv.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/conv_util.h" // IWYU pragma: keep +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/custom_call.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/fft.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/pad_util.h" // IWYU pragma: keep +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/reduce_window.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/slice.h" +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" // IWYU pragma: keep + +namespace mlir { +namespace odml { +namespace { + +#define GEN_PASS_DEF_PREPAREHLOPASS +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h.inc" + +class PrepareHloPass : public impl::PrepareHloPassBase { + public: + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(PrepareHloPass); + + void runOnOperation() override; +}; + +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/generated_prepare_hlo.inc" +void PrepareHloPass::runOnOperation() { + MLIRContext* context = &getContext(); + auto func = getOperation(); + + RewritePatternSet patterns(context); + populateWithGenerated(patterns); + + PopulatePrepareConvPatterns(context, patterns); + PopulatePrepareReduceWindowPatterns(context, patterns); + PopulatePrepareSlicePatterns(context, patterns); + PopulateCustomCallPreparePatterns(context, patterns); + PopulatePrepareFftPatterns(context, patterns); + + if (failed(applyPatternsGreedily(func, std::move(patterns)))) { + signalPassFailure(); + } +} +} // namespace + +std::unique_ptr> CreatePrepareHloPass() { + return std::make_unique(); +} + +static PassRegistration pass; + +} // namespace odml +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/prepare_hlo.td b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/prepare_hlo.td new file mode 100644 index 0000000000000000000000000000000000000000..35d651352a0c70101bf29d8fbe807125f1140ddf --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/prepare_hlo.td @@ -0,0 +1,481 @@ +// /* Copyright 2024 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 "mlir/IR/OpBase.td" +include "mlir/Dialect/Func/IR/FuncOps.td" +include "mhlo/IR/hlo_ops.td" +include "mlir/Dialect/Arith/IR/ArithOps.td" + +//===------------------------------------------------------------------------=== +// mhlo.convolution +//===------------------------------------------------------------------------=== + +// Re-Layout Patterns +// +// mhlo.convolution can encode arbitrary layouts for all its parameters +// (mhlo::ConvDimensionNumbersAttr). TFL convolutions require parameter layouts +// to be of a specific configuration: +// +// TFL Native Standard Conv Layouts: +// 2D : [b, 0, 1, f]x[o, 0, 1, i]->[b, 0, 1, f] +// 3D : [b, 0, 1, 2, f]x[0, 1, 2, i, o]->[b, 0, 1, 2, f] +// 2D (depthwise) : [b, 0, 1, f]x[i, 0, 1, o]->[b, 0, 1, f] +// +// The following patterns transpose the inputs and output of mhlo.convolution +// ops until they are complicit with the TFL required layout. +// Some of these defs depend on types in `conv_util.h`. + +// Given from, to `Layout` get the permutation array for transposing one to +// the other. +def PermForReLayout : NativeCodeCall< + "DenseIntElementsAttr::get(" + "RankedTensorType::get(" + "{$0.Rank()}," + "$_builder.getI64Type())," + "$0.GetPermForReLayout($1))">; + +def AreDnumsFullyDefined : Constraint()" + ".getLhs().getType().getShape().size()" + "== $0.getDefiningOp()" + ".getDimensionNumbers().getInputSpatialDimensions().size() + 2">>; + +// Given $0: Layout (from layout), $1: Layout (to layout), $2: Value +// get a new Type with the same element type as value, but with +// shape permuted based on from and to layouts. +def PermuteShape : NativeCodeCall< + "llvm::cast($2.getType())" + ".clone($0.PermuteShape(" + "$1," + "llvm::cast($2.getType()).getShape()))">; + +def IsStandardConv : Constraint())">>; + +def IsDepthwiseConv : Constraint())">>; + +def IsSupportedNonTrivialConv : Constraint())">>; + +def IsSupportedConv : Constraint>; + +def IsSupportedStandardOrNonTrivialConv : Constraint>; + +def IsStandardOrDepthwiseConv : Constraint>; + +// +// Re-layout input (lhs) to [b, spatials..., f] +//===--------------------------------------------------------------------------- + +def IsInputNotTFLNativeLayout : Constraint>; + +def InputLayout : NativeCodeCall< + "Layout($0.getInputBatchDimension()," + "$0.getInputFeatureDimension()," + "$0.getInputSpatialDimensions())">; + +def TFLNativeInputLayout : NativeCodeCall< + "GetTFLNativeInputOrOutputLayout($0)">; + +def InputHasIotaSpatials : Constraint>; + +// Copy dnums with the input layout set to [b, spatials..., f]. +def CloneDnumsWithTFLNativeInputLayout : NativeCodeCall< + "CloneDnumsWithInputLayout(" + "$_builder," + "$0," + "GetTFLNativeInputOrOutputLayout($0))">; + +def ReLayoutConvInput : Pat<(MHLO_ConvolutionOp:$conv + $input, + $kernel, + $strides, + $padding, + $lhs_dilation, + $rhs_dilation, + $window_reversal, + $dnums, + $feature_groups, + $batch_groups, + $precision_config + ), + (MHLO_ConvolutionOp + (MHLO_TransposeOp + $input, + (PermForReLayout + (InputLayout $dnums), + (TFLNativeInputLayout $dnums)) + ), + $kernel, + $strides, + $padding, + $lhs_dilation, + $rhs_dilation, + $window_reversal, + (CloneDnumsWithTFLNativeInputLayout $dnums), + $feature_groups, + $batch_groups, + $precision_config + ), + [(AreDnumsFullyDefined $conv), + (InputHasIotaSpatials $dnums), + (IsInputNotTFLNativeLayout $dnums), + (IsSupportedConv $conv)], + [], + (addBenefit 1)>; + + +// +// Re-layout kernel +//===--------------------------------------------------------------------------- + +def KernelHasIotaSpatials : Constraint>; + +def KernelLayout : NativeCodeCall< + "Layout($0.getKernelInputFeatureDimension()," + "$0.getKernelOutputFeatureDimension()," + "$0.getKernelSpatialDimensions())">; + +// +// standard conv kernel = [o, spatials..., i]. +//=----- + +def IsKernelNotTFLNativeStandardConvLayout : Constraint>; + +def TFLNativeStandardConvKernelLayout : NativeCodeCall< + "GetTFLNativeStandardConvKernelLayout($0)">; + +// Copy dnums with the kernel layout set to [o, spatials..., i]. +def CloneDnumsWithTFLNativeStandardConvKernelLayout : NativeCodeCall< + "CloneDnumsWithKernelLayout(" + "$_builder," + "$0," + "GetTFLNativeStandardConvKernelLayout($0))">; + + +def ReLayoutConvKernel : Pat<(MHLO_ConvolutionOp:$conv + $input, + $kernel, + $strides, + $padding, + $lhs_dilation, + $rhs_dilation, + $window_reversal, + $dnums, + $feature_groups, + $batch_groups, + $precision_config + ), + (MHLO_ConvolutionOp + $input, + (MHLO_TransposeOp + $kernel, + (PermForReLayout + (KernelLayout $dnums), + (TFLNativeStandardConvKernelLayout $dnums)) + ), + $strides, + $padding, + $lhs_dilation, + $rhs_dilation, + $window_reversal, + (CloneDnumsWithTFLNativeStandardConvKernelLayout $dnums), + $feature_groups, + $batch_groups, + $precision_config + ), + [(AreDnumsFullyDefined $conv), + (KernelHasIotaSpatials $dnums), + (IsKernelNotTFLNativeStandardConvLayout $dnums), + (IsSupportedStandardOrNonTrivialConv $conv)], + [], + (addBenefit 1)>; + + +// +// depthwise conv kernel = [i, spatials..., o]. +//=----- + +def IsKernelNotTFLNativeDepthwiseLayout : Constraint>; + +def TFLNativeDepthwiseConvKernelLayout : NativeCodeCall< + "GetTFLNativeDepthwiseConvKernelLayout()">; + +def CloneDnumsWithTFLNativeDepthwiseConvKernelLayout : NativeCodeCall< + "CloneDnumsWithKernelLayout(" + "$_builder," + "$0," + "GetTFLNativeDepthwiseConvKernelLayout())">; + + +def ReLayoutConvKernelDepthwise : Pat<(MHLO_ConvolutionOp:$conv + $input, + $kernel, + $strides, + $padding, + $lhs_dilation, + $rhs_dilation, + $window_reversal, + $dnums, + $feature_groups, + $batch_groups, + $precision_config + ), + (MHLO_ConvolutionOp + $input, + (MHLO_TransposeOp + $kernel, + (PermForReLayout + (KernelLayout $dnums), + (TFLNativeDepthwiseConvKernelLayout)) + ), + $strides, + $padding, + $lhs_dilation, + $rhs_dilation, + $window_reversal, + (CloneDnumsWithTFLNativeDepthwiseConvKernelLayout $dnums), + $feature_groups, + $batch_groups, + $precision_config + ), + [(AreDnumsFullyDefined $conv), + (KernelHasIotaSpatials $dnums), + (IsKernelNotTFLNativeDepthwiseLayout $dnums), + (IsDepthwiseConv $conv)], + [], + (addBenefit 1)>; + +// +// Re-layout output to [b, spatials..., f] +//===--------------------------------------------------------------------------- + +def IsOutputNotTFLNativeLayout : Constraint>; + +def OutputLayout : NativeCodeCall< + "Layout($0.getOutputBatchDimension()," + "$0.getOutputFeatureDimension()," + "$0.getOutputSpatialDimensions())">; + +def TFLNativeOutputLayout : NativeCodeCall< + "GetTFLNativeInputOrOutputLayout($0)">; + +def OutputHasIotaSpatials : Constraint>; + +// Copy dnums with the output layout set to [b, spatials..., f]. +def CloneDnumsWithTFLNativeOutputLayout : NativeCodeCall< + "CloneDnumsWithOutputLayout(" + "$_builder," + "$0," + "GetTFLNativeInputOrOutputLayout($0))">; + +def ReLayoutConvOutput : Pat<(MHLO_ConvolutionOp:$conv + $input, + $kernel, + $strides, + $padding, + $lhs_dilation, + $rhs_dilation, + $window_reversal, + $dnums, + $feature_groups, + $batch_groups, + $precision_config + ), + (MHLO_TransposeOp + (MHLO_ConvolutionOp + $input, + $kernel, + $strides, + $padding, + $lhs_dilation, + $rhs_dilation, + $window_reversal, + (CloneDnumsWithTFLNativeOutputLayout $dnums), + $feature_groups, + $batch_groups, + $precision_config, + (returnType + (PermuteShape + (OutputLayout $dnums), + (TFLNativeOutputLayout $dnums), + $conv)) + ), + (PermForReLayout + (TFLNativeOutputLayout $dnums), + (OutputLayout $dnums)) + ), + [(AreDnumsFullyDefined $conv), + (KernelHasIotaSpatials $dnums), + (IsOutputNotTFLNativeLayout $dnums), + (IsSupportedConv $conv)]>; + + +// Pull out non-trivial padding into separate explicit pad_op. +// +// This has the benifit of allowing for a single point of control +// for turning negative padding into slices. TFL convs can fuse +// "SAME" padding back in post-legalization. Note when lhs dilations +// are non-trivial, the mhlo.convolution has the semantics of a deconvolution. +// In this case padding is interpreted differently and so we leave it in the op. +//===--------------------------------------------------------------------------- + +// Given DenseElements (i64), check if they are all equal to "val". +class AreI64ElementsAll : Constraint()," + "[](auto v) { return v == "# val #"; })">>; + +class AreI64ElementsNotAll : + Constraint.predicate>>; + +// Gets a tuple of DenseElements (i64) given result from mhlo.convolution. +def GetExplicitPaddingArgs : NativeCodeCall< + "GetExplicitPaddingArgs($_builder," + "$0.getDefiningOp())">; + +// Gets element type from Value. +def GetElementType : NativeCodeCall< + "llvm::cast($0.getType()).getElementType()">; + +// Given element type, get a DenseElements with scalar shape and 0 value. +def GetZeroScalarAttrFromType : NativeCodeCall< + "$_builder.getZeroAttr(" + "RankedTensorType::get({}, $0))">; + +// Given padding attr, get new padding attr for trivial (no) padding. +def GetTrivialPaddingAttr : NativeCodeCall< + "$_builder.getZeroAttr($0.getType())">; + +// Given mhlo.convolution result, build an explicit mhlo.pad op +// which is semantically equivalant. +def ExplicitlyPadInput : NativeCodeCall< + "CreatePadOpFromConvPadding($_builder," + "$0.getDefiningOp())">; + +def UnfuseConvWithExplicitPadding : Pat<(MHLO_ConvolutionOp:$conv + $input, + $kernel, + $strides, + $padding, + $lhs_dilation, + $rhs_dilation, + $window_reversal, + $dnums, + $feature_groups, + $batch_groups, + $precision_config + ), + (MHLO_ConvolutionOp + (ExplicitlyPadInput $conv), + $kernel, + $strides, + (GetTrivialPaddingAttr $padding), + $lhs_dilation, + $rhs_dilation, + $window_reversal, + $dnums, + $feature_groups, + $batch_groups, + $precision_config + ), + [(AreDnumsFullyDefined $conv), + (KernelHasIotaSpatials $dnums), + (IsStandardOrDepthwiseConv $conv), + (AreI64ElementsNotAll<0> $padding)]>; + + + +//===------------------------------------------------------------------------=== +// mhlo.pad +//===------------------------------------------------------------------------=== + +// Pull negative pads into slice +//===--------------------------- + +def TrivialStrides : NativeCodeCall< + "DenseIntElementsAttr::get(" + "RankedTensorType::get({llvm::cast($0.getType()).getRank()}," + "$_builder.getI64Type())," + "llvm::SmallVector(llvm::cast($0.getType()).getRank()," + "1))">; + +def SliceStart : NativeCodeCall< + "SliceStartFromNegPadLows($0.getDefiningOp())">; + +def SliceEnd : NativeCodeCall< + "SliceEndFromNegPadHighs($0.getDefiningOp())">; + +def AnyNegativePads : Constraint())">>; + +def MakePadPositive : NativeCodeCall< + "ReplaceNegsWithZero($0)">; + +def TrivialInterior : Constraint())">>; + +def PullOutNegativePads : Pat<(MHLO_PadOp:$pad + $input, + $val, + $low, + $high, + $interior + ), + (MHLO_PadOp + (MHLO_SliceOp + $input, + (SliceStart $pad), + (SliceEnd $pad), + (TrivialStrides $pad)), + $val, + (MakePadPositive $low), + (MakePadPositive $high), + $interior + ), + [(AnyNegativePads $pad), + (TrivialInterior $pad)]>; diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/smuggle_disallowed_ops.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/smuggle_disallowed_ops.cc new file mode 100644 index 0000000000000000000000000000000000000000..dbfffbd95e8dd27df7ce12c268c4c59f431172b9 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/smuggle_disallowed_ops.cc @@ -0,0 +1,100 @@ +/* Copyright 2021 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/compiler/mlir/lite/stablehlo/transforms/smuggle_disallowed_ops.h" + +#include +#include + +#include "llvm/ADT/StringRef.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/DialectRegistry.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "stablehlo/dialect/StablehloOps.h" // from @stablehlo +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" + +namespace mlir { +namespace odml { + +namespace { + +// Convert op to stablehlo.custom_call +// "tf.ResizeBilinear"(%341, %138) { +// align_corners = false, device = "", half_pixel_centers = true} +// ==> +// stablehlo.custom_call @tf.ResizeBilinear(%arg0, %arg1) { +// align_corners = false, device = "", half_pixel_centers = true} +LogicalResult SmuggleOp(Operation* op, PatternRewriter& rewriter) { + auto call_target = + rewriter.getNamedAttr("call_target_name", op->getName().getIdentifier()); + SmallVector attrs{op->getAttrs()}; + attrs.push_back(call_target); + auto custom_call = mlir::stablehlo::CustomCallOp::create( + rewriter, op->getLoc(), op->getResultTypes(), op->getOperands(), attrs); + rewriter.replaceOp(op, custom_call.getResults()); + return success(); +} + +} // namespace + +template +class SmuggleOpPattern : public OpRewritePattern { + public: + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(OpTy op, + PatternRewriter& rewriter) const override { + return SmuggleOp(op, rewriter); + } +}; + +class SmuggleDisallowedOpsPass + : public PassWrapper> { + public: + StringRef getArgument() const final { return "smuggle-disallowed-ops-pass"; } + StringRef getDescription() const final { + return "Smuggle disallowed ops via stablehlo.custom_calls"; + } + void getDependentDialects(DialectRegistry& registry) const final { + registry.insert(); + } + + void runOnOperation() override { + RewritePatternSet patterns(&getContext()); + patterns.add>(&getContext()); + patterns.add>(&getContext()); + + ConversionTarget target(getContext()); + target.addIllegalOp(); + target.addLegalDialect(); + if (failed(applyPartialConversion(getOperation(), target, + std::move(patterns)))) { + signalPassFailure(); + } + } +}; + +std::unique_ptr CreateSmuggleDisallowedOpsPass() { + return std::make_unique(); +} + +static PassRegistration pass; + +} // namespace odml +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/smuggle_disallowed_ops.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/smuggle_disallowed_ops.h new file mode 100644 index 0000000000000000000000000000000000000000..508a58c36827f8e59b4a5270ad8f6be954a71f4a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/smuggle_disallowed_ops.h @@ -0,0 +1,31 @@ +/* Copyright 2021 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_SMUGGLE_DISALLOWED_OPS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_SMUGGLE_DISALLOWED_OPS_H_ + +#include + +#include "mlir/Pass/Pass.h" // from @llvm-project + +namespace mlir { +namespace odml { + +std::unique_ptr CreateSmuggleDisallowedOpsPass(); + +} // namespace odml +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_SMUGGLE_DISALLOWED_OPS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_fuse_convolution_pass.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_fuse_convolution_pass.cc new file mode 100644 index 0000000000000000000000000000000000000000..c78cca37f086f5f43e650376460f153cfaaf3504 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_fuse_convolution_pass.cc @@ -0,0 +1,202 @@ +/* Copyright 2022 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 "stablehlo/dialect/StablehloOps.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallVector.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Dialect/Shape/IR/Shape.h" // from @llvm-project +#include "mlir/Dialect/Traits.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/Diagnostics.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/Types.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/utils/validators.h" +#include "tensorflow/compiler/mlir/quantization/common/attrs_and_constraints.h" + +namespace mlir { +namespace odml { + +#define GEN_PASS_DEF_STABLEHLOFUSECONVOLUTIONPASS +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h.inc" + +namespace { + +class FuseStablehloMulAndConvolutionPattern + : public OpRewritePattern { + public: + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(stablehlo::MulOp mul_op, + PatternRewriter &rewriter) const override { + // Variables for capturing values and attributes used while creating ops. + stablehlo::ConvolutionOp conv_op; + Operation *bcast_or_const_op; + shape::ShapeOfOp shape_of_op; + stablehlo::ConstantOp filter; + stablehlo::ConstantOp multiplier; + mlir::ElementsAttr filter_value, mul_value; + mlir::DenseI64ArrayAttr broadcast_dims; + + // Match and capture values/attributes. + Value lhs = mul_op.getLhs(); + Value rhs = mul_op.getRhs(); + conv_op = lhs.getDefiningOp(); + if (conv_op == nullptr) { + return failure(); + } + filter = conv_op.getRhs().getDefiningOp(); + if (filter == nullptr) { + return failure(); + } + // Try to match static broadcast or dynamic broadcast. + bcast_or_const_op = rhs.getDefiningOp(); + bool is_dynamic_broadcast = + isa(bcast_or_const_op); + multiplier = + isa(bcast_or_const_op) + ? dyn_cast_or_null(bcast_or_const_op) + : bcast_or_const_op->getOperand(0) + .getDefiningOp(); + if (multiplier == nullptr) { + return failure(); + } + + auto result_type = OpTrait::util::getBroadcastedType(filter.getType(), + multiplier.getType()); + if (!result_type) { + return rewriter.notifyMatchFailure(mul_op, [&](::mlir::Diagnostic &diag) { + diag << "entities 'filter, multiplier' failed to satisfy constraint: " + "non-broadcastable operands"; + }); + } + filter_value = filter.getValue(); + mul_value = multiplier.getValue(); + // In StableHLO, Conv filter is in HWIO format, Depthwise conv filter is in + // HW1O format and backprop input conv filter is in HWOI format. Only fuses + // multiplier if all dimensions other than the out channel dimension are + // equal to 1. + if (!TFL::IsDimensionsDegenerateExceptLastOne( + mul_value.getShapedType().getShape())) { + return rewriter.notifyMatchFailure(mul_op, [&](::mlir::Diagnostic &diag) { + diag << "entities 'mul_value' failed to satisfy constraint: " + "unsupported dimensions"; + }); + } + if (!is_dynamic_broadcast && + !((*conv_op.getODSResults(0).begin()).hasOneUse())) { + return rewriter.notifyMatchFailure(mul_op, [&](::mlir::Diagnostic &diag) { + diag << "entities 'conv' failed to satisfy constraint: has one use"; + }); + } + // For dynamic case, the result of conv should be used by shape_of and mul. + if (is_dynamic_broadcast) { + auto conv_uses = (*conv_op.getODSResults(0).begin()).getUses(); + if (std::distance(conv_uses.begin(), conv_uses.end()) != 2 || + quant::FindUserOfType(conv_op) == nullptr || + quant::FindUserOfType(conv_op) == nullptr) { + return rewriter.notifyMatchFailure(mul_op, [&](::mlir::Diagnostic + &diag) { + diag << "entities 'conv' failed to satisfy constraint: has two uses " + "for dynamic case"; + }); + } + } + + // Rewrite + // For dynamic case, we use filter's shape to create a static broadcast. + broadcast_dims = + !isa(bcast_or_const_op) && !is_dynamic_broadcast + ? DenseI64ArrayAttr::get( + rewriter.getContext(), + dyn_cast_or_null( + bcast_or_const_op) + .getBroadcastDimensions()) + : nullptr; + if (broadcast_dims == nullptr) { + const auto filter_rank = filter_value.getShapedType().getRank(); + broadcast_dims = + DenseI64ArrayAttr::get(rewriter.getContext(), {filter_rank - 1}); + } + Value broadcast_multiplier = stablehlo::BroadcastInDimOp::create( + rewriter, mul_op.getLoc(), filter.getType(), multiplier, + broadcast_dims); + Value new_filter = + stablehlo::MulOp::create(rewriter, mul_op.getLoc(), filter.getType(), + filter, broadcast_multiplier); + Value new_conv = stablehlo::ConvolutionOp::create( + rewriter, mul_op.getLoc(), conv_op.getType(), conv_op.getLhs(), + new_filter, conv_op.getWindowStridesAttr(), conv_op.getPaddingAttr(), + conv_op.getLhsDilationAttr(), conv_op.getRhsDilationAttr(), + conv_op.getWindowReversalAttr(), conv_op.getDimensionNumbers(), + conv_op.getFeatureGroupCount(), conv_op.getBatchGroupCount(), + conv_op.getPrecisionConfigAttr()); + // For static case, replace the convolution op now. + if (!is_dynamic_broadcast) { + rewriter.replaceOp(mul_op, {new_conv}); + } else { + // For dynamic case, create new shape_of op and replace uses. + shape_of_op = dyn_cast_or_null( + bcast_or_const_op) + .getOutputDimensions() + .getDefiningOp(); + // Check if the shape come from the original conv op. + if (!shape_of_op || + shape_of_op.getArg().getDefiningOp() != + conv_op) { + return failure(); + } + Value new_shape_of = shape::ShapeOfOp::create( + rewriter, mul_op.getLoc(), shape_of_op.getType(), new_conv); + shape_of_op.replaceAllUsesWith(new_shape_of); + rewriter.replaceOp(mul_op, {new_conv}); + } + + return success(); + } +}; + +class StablehloFuseConvolutionPass + : public impl::StablehloFuseConvolutionPassBase< + StablehloFuseConvolutionPass> { + public: + void runOnOperation() override { + RewritePatternSet patterns(&getContext()); + patterns.add(&getContext()); + if (failed(applyPatternsGreedily(getOperation(), std::move(patterns)))) { + return signalPassFailure(); + } + } +}; + +} // namespace + +static PassRegistration pass; + +} // namespace odml +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h new file mode 100644 index 0000000000000000000000000000000000000000..a62e4c2601ea014881eaf11a65f5466c24410c54 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h @@ -0,0 +1,75 @@ +/* Copyright 2022 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_STABLEHLO_PASSES_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_STABLEHLO_PASSES_H_ + +#include + +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project + +namespace mlir { +namespace odml { + +// Applies various optimizations on MHLO IR. +std::unique_ptr createOptimizePass(); + +// Finds quantization patterns and compose them to uniform +// quantized types. +std::unique_ptr> +CreateComposeUniformQuantizedTypePass(); + +// Finds stablehlo ops that accept or produce uniform +// quantized typed tensors and converts them to equivalent ops in the TFLite +// dialect. +std::unique_ptr> +CreateUniformQuantizedStableHloToTflPass(); + +// Commutes transposes through specific ops +std::unique_ptr> CreateTransposeCommuteOpsPass(); + +// Legalizes MHLO to TF dialect. +std::unique_ptr> CreateLegalizeHloToTfPass(); + +// Replaces a splat constant tensor with a BroadcastInDim +// op. +std::unique_ptr> CreateUnfoldSplatConstantPass(); + +// Legalizes MHLO to TFLite dialect. +std::unique_ptr> CreateLegalizeHloToTfLitePass(); + +// Lowers stablehlo composite ops to tflite ops. +std::unique_ptr> CreateCompositeLoweringPass(); + +// Legalizes CHLO to tflite dialect. +std::unique_ptr> CreateLegalizeChloToTflPass(); + +// Rewrites MHLO in preparation for tflite legalization. +std::unique_ptr> CreatePrepareHloPass(); + +// Adds the HLO to TF rewrite patterns to the specified pattern list. +void PopulateLegalizeHloToTfPatterns(RewritePatternSet* patterns, + MLIRContext* context); + +#define GEN_PASS_DECL +#define GEN_PASS_REGISTRATION +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h.inc" + +} // namespace odml +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_STABLEHLO_PASSES_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.td b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.td new file mode 100644 index 0000000000000000000000000000000000000000..e399e9f04d864e124c4d4cf2ff119aa98916857c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.td @@ -0,0 +1,188 @@ +/* Copyright 2023 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 "mlir/Pass/PassBase.td" + +def ComposeUniformQuantizedTypePass : Pass<"compose-uniform-quantized-type", "ModuleOp"> { + let summary = "Compose uniform quantized types in StableHLO."; + let constructor = "mlir::odml::CreateComposeUniformQuantizedTypePass()"; + let description = [{ + Identifies uniform quantization patterns and composes them to uniform + quantized types. This pass targets a specific set of models that are + quantized from the framework level, which produces "decomposed" + quantization patterns due to the limitations of the framework. + + In the long term it is desired that the framework produces uniform + quantized types directly and deprecate this pass. + + The target patterns should have the following characteristics, with some + variations depending on the op being quantized. + + * A tensor is quantized using a `func::FuncOp` whose name contains + "uniform_quantize". The first argument is the tensor to be quantized, + the second argument is the zero point constant (element type: int) and + the third argument is the inverse scale constant (element type: float). + * A tensor is dequantized using a `func::FuncOp` whose name contains + "uniform_dequantize". The first argument is the tensor to be quantized, + the second argument is the zero point constant (element type: int) and + the third argument is the inverse scale constant (element type: float). + * Inputs to the target quantized op is quantized and the outputs are + dequantized. + * All quantization parameters (zero points and scales) are available, + usually as constants. + + The resulting pattern for a quantized op looks roughly as follows: + + ``` + %0 = stablehlo.constant() : () -> tensor<...x!quant.uniform> + %1 = stablehlo.uniform_quantize %2 : (tensor<...xf32>) -> tensor<...x!quant.uniform> + %3 = stablehlo.my_quantized_op(%1, %0) : (tensor<...x!quant.uniform>, tensor<...x!quant.uniform>) -> tensor<...x!quant.uniform> + %4 = stablehlo.dequantize %3 : (tensor<...x!quant.uniform>) -> tensor<...xf32> + ``` + }]; + let dependentDialects = [ + "stablehlo::StablehloDialect", + "quant::QuantDialect", + ]; +} + +def UniformQuantizedStableHloToTflPass + : Pass<"uniform-quantized-stablehlo-to-tfl", "mlir::func::FuncOp"> { + let summary = "Converts StableHLO ops using uniform quantized types to equivalent TFL ops."; + let constructor = "mlir::odml::CreateUniformQuantizedStableHloToTflPass()"; + let description = [{ + Converts StableHLO ops that accept or return uniform quantized types to + equivalent ops in the TFLite dialect. + }]; + let dependentDialects = [ + "stablehlo::StablehloDialect", + "quant::QuantDialect", + "mlir::TFL::TFLDialect", + ]; +} + +def LegalizeHloToTfPass : Pass<"tf-legalize-hlo", "ModuleOp"> { + let summary = "Legalize from MHLO to the TF dialect"; + let dependentDialects = ["TF::TensorFlowDialect"]; + let constructor = "mlir::odml::CreateLegalizeHloToTfPass()"; +} + +def LegalizeHloToTfLitePass : Pass<"tfl-legalize-hlo", "mlir::ModuleOp"> { + let summary = "Legalize from MHLO to the TFLite dialect"; + let dependentDialects = ["TFL::TensorFlowLiteDialect"]; + let constructor = "mlir::odml::CreateLegalizeHloToTfLitePass()"; +} + +def LegalizeStablehloToVhloPass : Pass<"stablehlo-legalize-vhlo", "ModuleOp"> { + let summary = "Legalize StableHLO to VHLO for ODML."; + let dependentDialects = ["mlir::vhlo::VhloDialect"]; +} + +def LegalizeVhloToStablehloPass : Pass<"vhlo-legalize-stablehlo", "ModuleOp"> { + let summary = "Legalize VHLO to StableHLO for ODML."; + let dependentDialects = ["mlir::stablehlo::StablehloDialect"]; +} + +def LegalizeCompositeToCustomOpPass : Pass<"stablehlo-composite-legalize-tfl-custom", "func::FuncOp"> { + let summary = "Legalize supported StableHLO CompositeOps to TFL CustomOp"; + let dependentDialects = ["TFL::TensorFlowLiteDialect"]; +} +def LegalizeStablehloCustomCallToCompositePass : Pass<"stablehlo-custom-call-legalize-composite", "ModuleOp"> { + let summary = "Legalize StableHLO custom call ops where the call target is 'stablehlo.composite' to composite ops."; + let dependentDialects = ["mlir::stablehlo::StablehloDialect"]; +} + +def UnfoldSplatConstantPass : Pass<"unfold-splat-constant-pass", "ModuleOp"> { + let summary = "Replaces a splat constant tensor with a BroadcastInDim op."; + let constructor = "mlir::odml::CreateUnfoldSplatConstantPass()"; +} + +def CompositeLoweringPass : Pass<"composite-lowering", "ModuleOp"> { + let summary = "Lowers mhlo composites directly to tflite ops (when possible)."; + let dependentDialects = ["mlir::mhlo::MhloDialect", "TFL::TensorFlowLiteDialect"]; + let constructor = "mlir::odml::CreateCompositeLoweringPass()"; +} + +def TransposeCommuteOpsPass : Pass<"transpose-commute-ops", "ModuleOp"> { + let summary = "Move transpose through specific ops"; + let dependentDialects = ["mlir::stablehlo::StablehloDialect"]; + let constructor = "mlir::odml::CreateTransposeCommuteOpsPass()"; +} + +def LegalizeChloToTflPass + : Pass<"tfl-legalize-chlo", "mlir::func::FuncOp"> { + let summary = "Lower chlo directly to tfl."; + let constructor = "mlir::odml::CreateLegalizeChloToTflPass()"; + let description = [{ + Decompose chlo ops directly to tflite. Additionally, apply any chlo/shlo->tflite + patterns that must be applied before chlo->shlo decomposition. + }]; + let dependentDialects = [ + "stablehlo::StablehloDialect", + "mlir::TFL::TensorFlowLiteDialect", + "chlo::ChloDialect", + "func::FuncDialect" + ]; +} + +def PrepareHloPass + : Pass<"prepare-hlo", "mlir::func::FuncOp"> { + let summary = "Prepare mhlo for mhlo->tfl legalization."; + let constructor = "mlir::odml::CreatePrepareHloPass()"; + let description = [{ + Apply generic mhlo->mhlo rewrites to make the graph more amenable for + legalizing to tfl. + }]; + let dependentDialects = [ + "mlir::mhlo::MhloDialect", + "func::FuncDialect", + "TFL::TensorFlowLiteDialect" + ]; +} + +def LiftCallSiteLocCallerPass : Pass<"lift-callsite-loc-caller", "ModuleOp"> { + let summary = "Lifts CallSites in pytorch generated stablehlo."; + let description = [{ + Lifts CallSites in pytorch generated stablehlo to make the Loc's consitent + after inlining. + }]; + let dependentDialects = ["func::FuncDialect"]; +} + +def LegalizeQDQCustomCallPass : Pass<"legalize-qdq-custom-call", "ModuleOp"> { + let summary = "Legalize stablehlo.custom_call for QDQ ops from odml-torch."; + let description = [{ + Legalize stablehlo.custom_call for Q/DQ ops from odml-torch. Quant dialect + is not registered in Python, so odml-torch uses custom_call to as carriers. + }]; + let dependentDialects = ["func::FuncDialect", "quant::QuantDialect", "stablehlo::StablehloDialect"]; +} + +def BuildStableHLOCompositePass : Pass<"build-stablehlo-composite", "ModuleOp"> { + let summary = "Build stablehlo.composite from inlined stablehlo.custom_call mark_tensor ops."; + let description = [{ + Build stablehlo.composite from inlined stablehlo.custom_call mark_tensor ops. + }]; + let dependentDialects = ["func::FuncDialect", "stablehlo::StablehloDialect"]; +} + +def StablehloUnfuseBatchNormPass : Pass<"stablehlo-unfuse-batch-norm", "mlir::func::FuncOp"> { + let summary = "Unfuses stablehlo.batch_norm_(inference,training) ops into stablehlo arithmetic ops."; + let dependentDialects = ["stablehlo::StablehloDialect", "TF::TensorFlowDialect", "mlir::shape::ShapeDialect"]; +} + +def StablehloFuseConvolutionPass : Pass<"stablehlo-fuse-convolution", "mlir::func::FuncOp"> { + let summary = "Fuses stablehlo.convolution and binary element-wise ops."; +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_unfuse_batch_norm_pass.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_unfuse_batch_norm_pass.cc new file mode 100644 index 0000000000000000000000000000000000000000..b77a1af032475075c894343a514bba30723018ee --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_unfuse_batch_norm_pass.cc @@ -0,0 +1,261 @@ +/* Copyright 2022 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 "stablehlo/dialect/StablehloOps.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Dialect/Shape/IR/Shape.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/ImplicitLocOpBuilder.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/Types.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/IR/ValueRange.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h" // NOLINT: Used in stablehlo_passes.h.inc +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" + +namespace mlir { +namespace odml { + +#define GEN_PASS_DEF_STABLEHLOUNFUSEBATCHNORMPASS +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h.inc" + +namespace { + +// Broadcasts the 1D value tensor 'value_1d' to the shape of 'result_type'. If +// 'shape_value' is initialized, creates a dynamic broadcast, otherwise creates +// a static broadcast. +Value broadcastToFeatureDim(Location loc, RankedTensorType result_type, + Value value1d, Value shape_value, + int64_t feature_dim, PatternRewriter &rewriter) { + auto dims = mlir::DenseI64ArrayAttr::get(loc.getContext(), {feature_dim}); + if (shape_value) { + return rewriter.createOrFold( + loc, result_type, value1d, shape_value, dims); + } + assert(result_type.hasStaticShape()); + return stablehlo::BroadcastInDimOp::create(rewriter, loc, result_type, + value1d, dims); +} + +// Gets the shape of operand, assuming it is a dynamic shape with static rank. +Value getShapeValue(Location loc, Value operand, PatternRewriter &rewriter) { + RankedTensorType resultType = + mlir::dyn_cast(operand.getType()); + return shape::ShapeOfOp::create( + rewriter, loc, + RankedTensorType::get(/*shape=*/{resultType.getRank()}, + rewriter.getIndexType()), + operand); +} + +Value materializeEpsilon(Operation *op, FloatAttr epsilon_attr, + FloatType fp_type, Value broadcast_to, + RankedTensorType broadcast_to_type, + PatternRewriter &rewriter) { + ImplicitLocOpBuilder b(op->getLoc(), rewriter); + if (epsilon_attr.getType() != fp_type) { + // Need to convert. + bool loses_info; + APFloat epsilon_float = epsilon_attr.getValue(); + auto status = epsilon_float.convert( + fp_type.getFloatSemantics(), APFloat::rmNearestTiesToEven, &loses_info); + if ((status & (~APFloat::opInexact)) != APFloat::opOK) { + op->emitWarning() << "Could not convert batch_norm epsilon to target fp " + "type: opStatus = " + << static_cast(status); + return nullptr; + } + if (loses_info) { + op->emitWarning("Conversion of epsilon loses precision"); + } + epsilon_attr = b.getFloatAttr(fp_type, epsilon_float); + } + + auto scalar_type = RankedTensorType::get(/*shape=*/{}, fp_type); + auto epsilon_tensor_attr = DenseElementsAttr::get( + scalar_type, {mlir::cast(epsilon_attr)}); + Value epsilon = stablehlo::ConstantOp::create(b, epsilon_tensor_attr); + auto dims = + DenseI64ArrayAttr::get(op->getContext(), SmallVector{}); + if (broadcast_to_type.hasStaticShape()) { + return stablehlo::BroadcastInDimOp::create(b, broadcast_to_type, epsilon, + dims); + } + Value shape_value = getShapeValue(op->getLoc(), broadcast_to, rewriter); + return b.createOrFold( + broadcast_to_type, epsilon, shape_value, dims); +} + +class UnfuseBatchNormTrainingPattern + : public OpRewritePattern { + public: + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(stablehlo::BatchNormTrainingOp bn_op, + PatternRewriter &rewriter) const override { + auto inputs = bn_op.getOperand(); + auto input_type = mlir::dyn_cast(inputs.getType()); + if (!input_type) { + return failure(); + } + auto feature_index = bn_op.getFeatureIndex(); + + // Compute mean + int64_t input_last_dim = input_type.getRank() - 1; + auto dims_type = RankedTensorType::get(/*shape=*/{input_last_dim}, + rewriter.getIntegerType(32)); + ::mlir::SmallVector reduce_dim_axes; + for (int i = 0; i < input_type.getRank(); ++i) { + if (i != feature_index) { + reduce_dim_axes.push_back(i); + } + } + auto mean_dims = DenseIntElementsAttr::get(dims_type, reduce_dim_axes); + // TODO(b/299514833): Remove TensorFlowDialect usage. + ::mlir::TF::ConstOp reduce_dim_op = + TF::ConstOp::create(rewriter, bn_op.getLoc(), mean_dims); + int64_t feature_dim_size = input_type.getDimSize(feature_index); + auto mean_var_type = RankedTensorType::get(/*shape=*/{feature_dim_size}, + rewriter.getF32Type()); + ::mlir::Value mean = TF::MeanOp::create( + rewriter, bn_op.getLoc(), mean_var_type, inputs, reduce_dim_op, + /*keep_dims=*/rewriter.getBoolAttr(false)); + + // Compute variance + Value shape_value = + getShapeValue(bn_op.getLoc(), bn_op.getOperand(), rewriter); + auto broadcast_mean = broadcastToFeatureDim( + bn_op.getLoc(), input_type, mean, shape_value, feature_index, rewriter); + ::mlir::Value square_diff = TF::SquaredDifferenceOp::create( + rewriter, bn_op.getLoc(), inputs, broadcast_mean); + ::mlir::Value variance = TF::MeanOp::create( + rewriter, bn_op.getLoc(), mean_var_type, square_diff, reduce_dim_op, + /*keep_dims=*/rewriter.getBoolAttr(false)); + + // Invoke BatchNormInferenceOp + ::mlir::FloatAttr epsilon = bn_op.getEpsilonAttr(); + ::mlir::Value batch_norm = stablehlo::BatchNormInferenceOp::create( + rewriter, bn_op.getLoc(), inputs, bn_op.getScale(), bn_op.getOffset(), + mean, variance, epsilon, rewriter.getI64IntegerAttr(feature_index)); + + // Return normalized values, mean, variable. + rewriter.replaceOp(bn_op, ::mlir::ValueRange{batch_norm, mean, variance}); + return success(); + } +}; + +class UnfuseBatchNormInferencePattern + : public OpRewritePattern { + public: + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(stablehlo::BatchNormInferenceOp bn_op, + PatternRewriter &rewriter) const override { + // Enforce type invariants. + // Note that we deduce the actual element type from the variance, + // which should not be subject to quantization at a higher level. + auto input_type = + mlir::dyn_cast(bn_op.getOperand().getType()); + auto variance_type = + mlir::dyn_cast(bn_op.getVariance().getType()); + if (!input_type || !variance_type) { + return failure(); + } + auto fp_type = mlir::dyn_cast(variance_type.getElementType()); + if (!fp_type) { + return failure(); + } + + // result = (x - mean) * scale / sqrt(variance + epsilon) + offset + // Let multiplier = scale / sqrt(variance + epsilon), to compute + // (x - mean) * scale / sqrt(variance + epsilon) + offset, + // is then to compute (x * multiplier) + (offset - mean * multiplier). + + auto epsilon = materializeEpsilon( + bn_op.getOperation(), bn_op.getEpsilonAttr(), fp_type, + bn_op.getVariance(), variance_type, rewriter); + if (!epsilon) { + return failure(); + } + + // Compute multiplier = scale / sqrt(variance + epsilon) + Value multiplier = stablehlo::AddOp::create(rewriter, bn_op.getLoc(), + bn_op.getVariance(), epsilon); + multiplier = + stablehlo::RsqrtOp::create(rewriter, bn_op.getLoc(), multiplier); + multiplier = stablehlo::MulOp::create(rewriter, bn_op.getLoc(), multiplier, + bn_op.getScale()); + + // Compute rhs = offset - mean * multiplier + Value rhs = stablehlo::MulOp::create(rewriter, bn_op.getLoc(), multiplier, + bn_op.getMean()); + rhs = stablehlo::SubtractOp::create(rewriter, bn_op.getLoc(), + bn_op.getOffset(), rhs); + + // Broadcast `multiplier` and `rhs` + Value shape_value; + if (!input_type.hasStaticShape()) { + shape_value = getShapeValue(bn_op.getLoc(), bn_op.getOperand(), rewriter); + } + int64_t feature_dim = bn_op.getFeatureIndex(); + auto broadcast_multiplier = + broadcastToFeatureDim(bn_op.getLoc(), input_type, multiplier, + shape_value, feature_dim, rewriter); + + // Computes x * multiplier + rhs + Value lhs = stablehlo::MulOp::create( + rewriter, bn_op.getLoc(), bn_op.getOperand(), broadcast_multiplier); + auto broadcast_rhs = broadcastToFeatureDim( + bn_op.getLoc(), input_type, rhs, shape_value, feature_dim, rewriter); + rewriter.replaceOpWithNewOp(bn_op, lhs, broadcast_rhs); + + return success(); + } +}; + +class StablehloUnfuseBatchNormPass + : public impl::StablehloUnfuseBatchNormPassBase< + StablehloUnfuseBatchNormPass> { + public: + void runOnOperation() override { + RewritePatternSet patterns(&getContext()); + patterns.add(&getContext()); + patterns.add(&getContext()); + if (failed(applyPatternsGreedily(getOperation(), std::move(patterns)))) { + signalPassFailure(); + } + } +}; + +} // namespace + +static PassRegistration pass; + +} // namespace odml +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_util.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_util.cc new file mode 100644 index 0000000000000000000000000000000000000000..96450909445d761efeb558748765d0380c3dbb7d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_util.cc @@ -0,0 +1,51 @@ +/* Copyright 2021 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/compiler/mlir/lite/stablehlo/transforms/stablehlo_util.h" + +#include +#include +#include + +#include "llvm/ADT/DenseSet.h" + +namespace mlir { +namespace odml { + +std::vector GetAcceptedStableHLODialects() { + // It returns the default list of accepted dialects. + std::vector accepted_dialects({"stablehlo", "builtin", "func"}); + return accepted_dialects; +} + +std::vector GetAcceptedTFLiteDialects() { + // It returns the default list of accepted dialects. + std::vector accepted_dialects({"tfl", "builtin", "func"}); + return accepted_dialects; +} + +bool IsAcceptedDialect(llvm::StringRef dialect_name, + const std::vector& accepted_dialects) { + return std::find(accepted_dialects.begin(), accepted_dialects.end(), + dialect_name) != accepted_dialects.end(); +} + +bool IsAcceptedOp(llvm::StringRef dialect_name, llvm::StringRef op_name, + const std::vector& accepted_dialects) { + return IsAcceptedDialect(dialect_name, accepted_dialects); +} + +} // namespace odml +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_util.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_util.h new file mode 100644 index 0000000000000000000000000000000000000000..d504ad572bcd198626bded6f722568a1d7d17fbb --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_util.h @@ -0,0 +1,43 @@ +/* Copyright 2021 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_STABLEHLO_UTIL_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_STABLEHLO_UTIL_H_ + +#include +#include + +#include "llvm/ADT/StringRef.h" + +namespace mlir { +namespace odml { + +std::vector GetAcceptedStableHLODialects(); + +std::vector GetAcceptedTFLiteDialects(); + +// Can we find the given `dialect_name` in the `accepted_dialects`? +bool IsAcceptedDialect(llvm::StringRef dialect_name, + const std::vector &accepted_dialects); + +// The consolidated logic to verify if each final op is acceptable or not. +// Also see `PrintOpStatsPass` and `CheckAcceptedOpsPass`. +bool IsAcceptedOp(llvm::StringRef dialect_name, llvm::StringRef op_name, + const std::vector &accepted_dialects); + +} // namespace odml +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_STABLEHLO_UTIL_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/tfl_legalize_chlo.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/tfl_legalize_chlo.cc new file mode 100644 index 0000000000000000000000000000000000000000..ce4422d117bc5f699a3a83cef0abd1873e3992f5 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/tfl_legalize_chlo.cc @@ -0,0 +1,64 @@ +/* Copyright 2024 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 "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Support/TypeID.h" // from @llvm-project +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" // from @llvm-project +#include "stablehlo/dialect/ChloOps.h" // from @stablehlo // IWYU pragma: keep +#include "stablehlo/dialect/StablehloOps.h" // from @stablehlo // IWYU pragma: keep +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" // IWYU pragma: keep +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/gelu.h" +#include "tensorflow/compiler/mlir/lite/transforms/passes.h" // IWYU pragma: keep + +namespace mlir { +namespace odml { +namespace { + +#define GEN_PASS_DEF_LEGALIZECHLOTOTFLPASS +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h.inc" + +class LegalizeChloToTflPass + : public impl::LegalizeChloToTflPassBase { + public: + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(LegalizeChloToTflPass) + + void runOnOperation() override { + auto func = getOperation(); + auto* ctx = &getContext(); + + RewritePatternSet patterns(ctx); + patterns.add(ctx); + if (failed(applyPatternsGreedily(func, std::move(patterns)))) { + signalPassFailure(); + } + } +}; + +} // namespace + +std::unique_ptr> CreateLegalizeChloToTflPass() { + return std::make_unique(); +} + +static PassRegistration pass; + +} // namespace odml +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/tfl_stablehlo_pass.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/tfl_stablehlo_pass.cc new file mode 100644 index 0000000000000000000000000000000000000000..a7c626b08772dc616ef08a940c65c83e9680e31a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/tfl_stablehlo_pass.cc @@ -0,0 +1,271 @@ +/* Copyright 2021 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/compiler/mlir/lite/stablehlo/transforms/tfl_stablehlo_pass.h" + +#include +#include +#include +#include +#include + +#include "flatbuffers/flatbuffers.h" // from @flatbuffers +#include "flatbuffers/flexbuffers.h" // from @flatbuffers +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Block.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/OperationSupport.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "stablehlo/dialect/Register.h" // from @stablehlo +#include "stablehlo/dialect/StablehloOps.h" // from @stablehlo +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/tensorflow/utils/dynamic_shape_utils.h" + +namespace mlir { +namespace odml { + +static bool isDenseI64Array(llvm::StringRef op_name, + llvm::StringRef field_name) { + if (op_name == "stablehlo.broadcast" && field_name == "broadcast_sizes") + return true; + if (op_name == "stablehlo.broadcast_in_dim" && + field_name == "broadcast_dimensions") + return true; + if ((op_name == "stablehlo.convolution" || + op_name == "stablehlo.dynamic_conv") && + (field_name == "window_strides" || field_name == "lhs_dilation" || + field_name == "rhs_dilation")) + return true; + if (op_name == "stablehlo.dynamic_broadcast_in_dim" && + (field_name == "broadcast_dimensions" || + field_name == "known_expanding_dimensions" || + field_name == "known_nonexpanding_dimensions")) + return true; + if ((op_name == "stablehlo.dynamic_slice" || op_name == "stablehlo.gather") && + field_name == "slice_sizes") + return true; + if (op_name == "stablehlo.fft" && field_name == "fft_length") return true; + if ((op_name == "stablehlo.map" || op_name == "stablehlo.reduce" || + op_name == "stablehlo.reverse") && + field_name == "dimensions") + return true; + if (op_name == "stablehlo.pad" && + (field_name == "edge_padding_low" || field_name == "edge_padding_high" || + field_name == "interior_padding")) + return true; + if (op_name == "stablehlo.reduce_window" && + (field_name == "window_dimensions" || field_name == "window_strides" || + field_name == "base_dilations" || field_name == "window_dilations")) + return true; + if (op_name == "stablehlo.select_and_scatter" && + (field_name == "window_dimensions" || field_name == "window_strides")) + return true; + if (op_name == "stablehlo.slice" && + (field_name == "start_indices" || field_name == "limit_indices" || + field_name == "strides")) + return true; + if (op_name == "stablehlo.transpose" && field_name == "permutation") + return true; + return false; +} + +class TflToStablehloPass + : public mlir::PassWrapper> { + public: + explicit TflToStablehloPass() : PassWrapper() {} + StringRef getArgument() const final { return "tfl-parse-stablehlo-ops"; } + StringRef getDescription() const final { + return "This pass will legalize TFLite custom Ops to StableHLO ops."; + } + + private: + void runOnOperation() override; + + void getDependentDialects(DialectRegistry& registry) const override { + mlir::stablehlo::registerAllDialects(registry); + } + inline TFL::ConstBytesAttr CustomOption(OpBuilder* builder, + const std::string& content) { + return TFL::ConstBytesAttr::get(builder->getContext(), + StringRef(content.data(), content.size())); + } + + std::vector FlatbufferVecToMlirVec(const flexbuffers::Vector& vec) { + std::vector temp(vec.size(), 0); + for (int i = 0; i < vec.size(); i++) { + temp[i] = vec[i].AsInt64(); + } + return temp; + } + + llvm::SmallVector ReadAttr(const flexbuffers::Map& m, + Builder* builder, + std::string op_name) { + llvm::SmallVector attrs; + const auto& keys = m.Keys(); + for (size_t i = 0; i < keys.size(); ++i) { + const auto key = keys[i].AsKey(); + const auto& value = m[key]; + switch (value.GetType()) { + case flexbuffers::FBT_INT: { + auto attr = value.AsInt64(); + auto named_attr = builder->getNamedAttr( + key, builder->getIntegerAttr(builder->getIntegerType(64), attr)); + attrs.push_back(named_attr); + break; + } + case flexbuffers::FBT_VECTOR_BOOL: { + llvm::SmallVector vec; + const auto& vector = value.AsTypedVector(); + for (size_t i = 0; i < vector.size(); i++) { + vec.push_back(vector[i].AsBool()); + } + attrs.push_back( + builder->getNamedAttr(key, builder->getDenseBoolArrayAttr(vec))); + break; + } + case flexbuffers::FBT_VECTOR_INT: { + const auto& vector = value.AsTypedVector(); + std::vector vec; + vec.reserve(vector.size()); + for (size_t i = 0; i < vector.size(); i++) { + vec.push_back(vector[i].AsInt64()); + } + std::vector shape; + if (std::string{key} == "padding") { + shape.push_back(vec.size() / 2); + shape.push_back(2); + } else { + shape.push_back(vec.size()); + } + Attribute value; + if (isDenseI64Array(op_name, key)) { + value = builder->getDenseI64ArrayAttr(vec); + } else { + RankedTensorType ty = tensorflow::GetTypeFromTFTensorShape( + shape, builder->getIntegerType(64)); + value = DenseIntElementsAttr::get(ty, vec); + } + auto named_attr = builder->getNamedAttr(key, value); + attrs.push_back(named_attr); + break; + } + case flexbuffers::FBT_VECTOR_STRING_DEPRECATED: { + const auto& vector = value.AsTypedVector(); + + if (std::string{key} == "precision_config") { + llvm::SmallVector precision_attrs; + for (size_t i = 0; i < vector.size(); i++) { + auto conf_attr = mlir::stablehlo::PrecisionAttr::get( + builder->getContext(), mlir::stablehlo::symbolizePrecision( + vector[i].AsString().str()) + .value()); + precision_attrs.push_back(conf_attr); + } + auto named_attr = builder->getNamedAttr( + key, builder->getArrayAttr(precision_attrs)); + attrs.push_back(named_attr); + } else { + std::vector temp; + for (size_t i = 0; i < vector.size(); i++) { + auto conf_str = + builder->getStringAttr(vector[i].AsString().str()); + temp.push_back(conf_str); + } + ArrayRef values(temp); + auto named_attr = + builder->getNamedAttr(key, builder->getStrArrayAttr(values)); + attrs.push_back(named_attr); + } + + break; + } + case flexbuffers::FBT_VECTOR: { + if (std::string{key} == "dimension_numbers") { + auto value_vec = value.AsVector(); + auto vec1 = FlatbufferVecToMlirVec(value_vec[2].AsVector()); + auto vec2 = FlatbufferVecToMlirVec(value_vec[5].AsVector()); + auto vec3 = FlatbufferVecToMlirVec(value_vec[8].AsVector()); + auto conv_dimension_numbers_attr = + mlir::stablehlo::ConvDimensionNumbersAttr::get( + builder->getContext(), value_vec[0].AsInt64(), + value_vec[1].AsInt64(), llvm::ArrayRef(vec1), + value_vec[3].AsInt64(), value_vec[4].AsInt64(), + llvm::ArrayRef(vec2), value_vec[6].AsInt64(), + value_vec[7].AsInt64(), llvm::ArrayRef(vec3)); + auto named_attr = + builder->getNamedAttr(key, conv_dimension_numbers_attr); + attrs.push_back(named_attr); + } + break; + } + default: { + emitWarning(builder->getUnknownLoc(), + "seralization not supported for : ") + << key; + break; + } + } + } + return attrs; + } +}; + +void TflToStablehloPass::runOnOperation() { + func::FuncOp fn = getOperation(); + OpBuilder builder(fn.getContext()); + fn.walk([&](TFL::CustomOp custom_op) { + builder.setInsertionPoint(custom_op); + const uint8_t* option_buf = reinterpret_cast( + custom_op.getCustomOption().getValue().data()); + auto flex_buffer_map = + flexbuffers::GetRoot(option_buf, + custom_op.getCustomOption().getValue().size()) + .AsMap(); + auto attr = + ReadAttr(flex_buffer_map, &builder, custom_op.getCustomCode().str()); + OperationState op_state(custom_op.getLoc(), + custom_op.getCustomCode().str()); + op_state.addOperands(custom_op.getOperands()); + llvm::SmallVector output_tys; + for (int i = 0; i < custom_op.getNumResults(); i++) { + output_tys.push_back(custom_op.getType(i)); + } + op_state.addTypes(output_tys); + op_state.addAttributes(attr); + auto stablehlo_op = builder.create(op_state); + custom_op.replaceAllUsesWith(stablehlo_op); + custom_op.erase(); + }); +} + +std::unique_ptr> CreateTflToStablehloPass() { + return std::make_unique(); +} + +static PassRegistration pass; + +} // namespace odml +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/tfl_stablehlo_pass.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/tfl_stablehlo_pass.h new file mode 100644 index 0000000000000000000000000000000000000000..3ea149826e9719d2fba34e3981599c7784c99375 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/tfl_stablehlo_pass.h @@ -0,0 +1,34 @@ +/* Copyright 2021 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_TFL_STABLEHLO_PASS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_TFL_STABLEHLO_PASS_H_ + +#include +#include + +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project + +namespace mlir { +namespace odml { + +// Creates a pass which transforms TFLite to StableHLO Ops. +std::unique_ptr> CreateTflToStablehloPass(); + +} // namespace odml +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_TFL_STABLEHLO_PASS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/tflite_legalize_hlo.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/tflite_legalize_hlo.cc new file mode 100644 index 0000000000000000000000000000000000000000..544802a03ecb4f5a4ed5c357f5ee4168549b8c91 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/tflite_legalize_hlo.cc @@ -0,0 +1,507 @@ +/* Copyright 2023 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. +==============================================================================*/ + +// The kept headers are provided for the included file `passes.h.inc`. +#include +#include +#include +#include + +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallSet.h" +#include "llvm/Support/Casting.h" +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Support/TypeID.h" // from @llvm-project +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" // IWYU pragma: keep +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/case.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/conv.h" // IWYU pragma: keep +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/custom_call.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/dot_general.h" // IWYU pragma: keep +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/fft.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/gather.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/get_dimension_size.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/if.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/iota.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/pad.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/reduce.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/reduce_window.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/slice.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/sort.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/util.h" // IWYU pragma: keep +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_hlo_conversions/while.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h" +#include "tensorflow/compiler/mlir/lite/transforms/passes.h" // IWYU pragma: keep +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" // IWYU pragma: keep +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir { +namespace odml { +namespace { + +// Returns the shape of the given value in a Constant Op. +arith::ConstantOp ShapeToConst(PatternRewriter& rewriter, Value value) { + ArrayRef shape = mlir::cast(value.getType()).getShape(); + auto attr_type = RankedTensorType::get({static_cast(shape.size())}, + rewriter.getIntegerType(64)); + auto attr = DenseElementsAttr::get(attr_type, shape); + return arith::ConstantOp::create(rewriter, value.getLoc(), attr_type, attr); +} + +// Returns true if broadcast_dimensions obey Tensorflow convention, as in new +// dimensions are added as prefix. +bool IsTFLStyleBroadcast(DenseIntElementsAttr broadcast_dimensions, + Value output) { + // broadcast_dimensions is an increasing list by definition, thus it suffices + // to check the first element. + int64_t input_rank = broadcast_dimensions.getNumElements(); + int64_t output_rank = mlir::cast(output.getType()).getRank(); + return input_rank == 0 || + (broadcast_dimensions.getValues()[0].getSExtValue() == + output_rank - input_rank); +} + +// Returns the intermediate shape that input tensor should be reshaped to during +// legalization of BroadcastInDimOp. +arith::ConstantOp ExpandedShape(OpBuilder& b, Value input, + DenseIntElementsAttr broadcast_dimensions, + Value output) { + // Initialize expanded shape with output rank and dimensions of 1. + llvm::SmallVector expanded_shape( + llvm::cast(output.getType()).getRank(), + /*Value=*/b.getI32IntegerAttr(1)); + + // Set dimension sizes specified by broadcast_dimensions. + auto input_shape = llvm::cast(input.getType()).getShape(); + + for (auto x : llvm::enumerate(broadcast_dimensions)) { + expanded_shape[x.value().getSExtValue()] = + b.getI32IntegerAttr(static_cast(input_shape[x.index()])); + } + + // Create the expanded type wrapped in a arith::ConstantOp. + auto attr_type = RankedTensorType::get( + {static_cast(expanded_shape.size())}, b.getIntegerType(32)); + auto attr = DenseElementsAttr::get(attr_type, expanded_shape); + return arith::ConstantOp::create(b, output.getLoc(), attr_type, attr); +} + +Value ExpandedDynamicShape(OpBuilder& b, Value input, + DenseIntElementsAttr broadcast_dimensions, + Value output) { + int64_t output_rank = mlir::cast(output.getType()).getRank(); + llvm::SmallVector expanded_dimensions; + llvm::SmallSet broadcast_dimensions_values; + + for (auto x : llvm::enumerate(broadcast_dimensions)) { + broadcast_dimensions_values.insert(x.value().getSExtValue()); + } + + for (int64_t i = 0; i < output_rank; i++) { + if (!broadcast_dimensions_values.contains(i)) { + expanded_dimensions.push_back(i); + } + } + + Value expanded_input = input; + + for (int64_t i : expanded_dimensions) { + auto index_attr = DenseIntElementsAttr::get( + RankedTensorType::get({}, b.getI64Type()), {i}); + Value index = arith::ConstantOp::create(b, output.getLoc(), index_attr); + + auto cur_type = llvm::cast(expanded_input.getType()); + auto cur_shape = cur_type.getShape(); + llvm::SmallVector new_shape; + + auto begin = cur_shape.begin(); + new_shape.append(begin, begin + i); + new_shape.push_back(1); + new_shape.append(begin + i, cur_shape.end()); + + auto new_type = RankedTensorType::get(new_shape, cur_type.getElementType()); + + expanded_input = TFL::ExpandDimsOp::create(b, output.getLoc(), new_type, + expanded_input, index); + } + + return expanded_input; +} + +bool IsSign(APInt a, APInt sign) { + if (a.isZero()) return a == sign; + if (a.isNegative()) return sign == -1; + return sign == 1; +} + +bool IsSign(APFloat a, APFloat sign) { + if (a.isNaN() || a.isZero()) return a == sign; + if (a.isNegative()) return sign.isExactlyValue(-1.0); + return sign.isExactlyValue(1.0); +} + +bool IsDenseSplatIntAttr(ElementsAttr float_or_int) { + return mlir::isa(float_or_int) && + mlir::isa(float_or_int); +} + +bool IsDenseSplatFloatAttr(ElementsAttr float_or_int) { + return mlir::isa(float_or_int) && + mlir::isa(float_or_int); +} + +bool ValueEquals(ElementsAttr float_or_int, double rhs) { + if (IsDenseSplatFloatAttr(float_or_int)) { + return mlir::cast(float_or_int) + .getSplatValue() + .isExactlyValue(rhs); + } else if (IsDenseSplatIntAttr(float_or_int)) { + return mlir::cast(float_or_int).getSplatValue() == + static_cast(rhs); + } + return false; +} + +// Returns whether the splat constant is the sign of the int or float Tensor. +bool TensorIsSign(PatternRewriter& rewriter, ElementsAttr float_or_int, + ElementsAttr sgn_cst) { + auto sgn_splat = llvm::dyn_cast(sgn_cst); + if (!sgn_splat) return false; + + auto splat = dyn_cast(float_or_int); + if (auto float_spl = llvm::dyn_cast_if_present(splat), + sgn_cst_spl = llvm::dyn_cast_if_present(sgn_splat); + float_spl && sgn_cst_spl) { + return IsSign(float_spl.getValue(), sgn_cst_spl.getValue()); + } + if (auto int_spl = llvm::dyn_cast_if_present(splat), + sgn_cst_spl = llvm::dyn_cast_if_present(sgn_splat); + int_spl && sgn_cst_spl) { + return IsSign(int_spl.getValue(), sgn_cst_spl.getValue()); + } + if (mlir::isa(float_or_int)) { + auto sgn_splat_value = sgn_splat.getSplatValue(); + return llvm::all_of(float_or_int.getValues(), [&](APFloat value) { + return IsSign(value, sgn_splat_value); + }); + } + if (mlir::isa(float_or_int)) { + auto sgn_splat_value = sgn_splat.getSplatValue(); + return llvm::all_of(float_or_int.getValues(), [&](APInt value) { + return IsSign(value, sgn_splat_value); + }); + } + return false; +} + +bool SameTypeOrDefaultCompare(mhlo::ComparisonTypeAttr comparison_type_attr, + ElementsAttr cst) { + if (!comparison_type_attr) return true; + auto comparison_type_attr_value = comparison_type_attr.getValue(); + if (comparison_type_attr_value == mhlo::ComparisonType::FLOAT && + IsDenseSplatFloatAttr(cst)) { + return true; + } + if ((comparison_type_attr_value == mhlo::ComparisonType::SIGNED || + comparison_type_attr_value == mhlo::ComparisonType::UNSIGNED) && + IsDenseSplatIntAttr(cst)) { + return true; + } + return false; +} + +bool ValueIsReciprocal(ElementsAttr float_or_int, ElementsAttr rhs) { + if (IsDenseSplatFloatAttr(float_or_int) && + IsDenseSplatFloatAttr(float_or_int)) { + return (mlir::cast(float_or_int) + .getSplatValue() * + mlir::cast(rhs).getSplatValue()) + .isExactlyValue(1.0); + } else if (IsDenseSplatIntAttr(float_or_int) && + IsDenseSplatIntAttr(float_or_int)) { + return (mlir::cast(float_or_int).getSplatValue() * + mlir::cast(rhs).getSplatValue()) == 1; + } + return false; +} + +bool ValueGreaterThanZero(ElementsAttr float_or_int) { + if (IsDenseSplatIntAttr(float_or_int)) { + auto value = + mlir::cast(float_or_int).getSplatValue(); + return !value.isNegative() && !value.isZero(); + } else if (IsDenseSplatFloatAttr(float_or_int)) { + auto value = + mlir::cast(float_or_int).getSplatValue(); + return !value.isNaN() && !value.isNegative() && !value.isZero(); + } + return false; +} + +#define GEN_PASS_DEF_LEGALIZEHLOTOTFLITEPASS +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h.inc" + +bool SupportedComparisonType(mhlo::ComparisonTypeAttr comp_type) { + if (!comp_type) return true; + auto c_ty = comp_type.getValue(); + return c_ty == mhlo::ComparisonType::FLOAT || + c_ty == mhlo::ComparisonType::SIGNED || + c_ty == mhlo::ComparisonType::UNSIGNED || + c_ty == mhlo::ComparisonType::NOTYPE; +} + +class LegalizeHloToTfLitePass + : public impl::LegalizeHloToTfLitePassBase { + public: + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(LegalizeHloToTfLitePass); + + void runOnOperation() override; +}; + +std::optional IsCbrtLegal(mhlo::CbrtOp op) { + return !op.getType().getElementType().isF32(); +} + +bool IsNotOpLegal(mhlo::NotOp op) { + return op.getType().getElementType().isInteger(64); +} + +bool IsCompareLegal(mhlo::CompareOp op) { + return !SupportedComparisonType(op.getCompareTypeAttr()); +} + +bool IsAbsOpLegal(mhlo::AbsOp op) { + return !llvm::cast(op.getOperand().getType()) + .getElementType() + .isIntOrFloat() && + !llvm::isa( + op.getOperand().getType().getElementType()); +} + +bool IsImagOpLegal(mhlo::ImagOp op) { + return llvm::cast(op.getOperand().getType()) + .getElementType() + .isIntOrFloat() || + !llvm::isa( + op.getOperand().getType().getElementType()); +} + +bool IsRealOpLegal(mhlo::RealOp op) { + return llvm::cast(op.getOperand().getType()) + .getElementType() + .isIntOrFloat() || + !llvm::isa( + op.getOperand().getType().getElementType()); +} + +// shlo reference add kernel supports u32 which tfl does not. +bool IsAddOpLegal(mhlo::AddOp op) { + return llvm::cast(op.getOperand(0).getType()) + .getElementType() + .isUnsignedInteger(32); +} + +// shlo reference sub kernel support u32 which tfl does not. +bool IsSubtractOpLegal(mhlo::SubtractOp op) { + return llvm::cast(op.getOperand(0).getType()) + .getElementType() + .isUnsignedInteger(32); +} + +// shlo reference min kernels supports bool which tfl does not. +bool IsMinimumOpLegal(mhlo::MinOp op) { + return llvm::cast(op.getOperand(0).getType()) + .getElementType() + .isInteger(1); +} + +void SetUnaryOpLegal(ConversionTarget& target) { + auto is_legal = [](Operation* op) { + return !llvm::cast(op->getOperand(0).getType()) + .getElementType() + .isIntOrFloat(); + }; + target.addDynamicallyLegalOp< + // go/keep-sorted start + // clang-format off + mhlo::BitcastConvertOp, + mhlo::CeilOp, + mhlo::ConvertOp, + mhlo::CosineOp, + mhlo::ExpOp, + mhlo::Expm1Op, + mhlo::FloorOp, + mhlo::IsFiniteOp, + mhlo::Log1pOp, + mhlo::LogOp, + mhlo::LogisticOp, + mhlo::NegOp, + mhlo::RsqrtOp, + mhlo::SignOp, + mhlo::SineOp, + mhlo::SqrtOp, + mhlo::TanhOp + // clang-format on + // go/keep-sorted end + >(is_legal); +} + +// mhlo "bitwise ops" can be both bitwise (floats/ints) or logical (bools). +// TFL ops are only one of logical or bitwise. +void SetBinaryBitwiseLegal(ConversionTarget& target) { + auto is_logical = [](Operation* op) { + return llvm::cast(op->getResultTypes()[0]) + .getElementType() + .isInteger(1); + }; + auto is_bitwise = [&](Operation* op) { return !is_logical(op); }; + target.addDynamicallyLegalOp(is_bitwise); + target.addDynamicallyLegalOp(is_logical); +} + +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/generated_tflite_legalize_hlo.inc" +void LegalizeHloToTfLitePass::runOnOperation() { + MLIRContext* context = &getContext(); + + // Apply large rounding related patterns first without dialect conversion. + // This unlocks cleaner match/fold behavior, making these patterns less + // sensitive to broadcasted constants. + { + RewritePatternSet patterns(context); + patterns.add< + // clang-format off + Phase1_Round, + Phase1_FloorMod, + Phase1_FloorMod2, + Phase1_FloorDiv, + Phase1_FloorDiv2, + Phase1_FloorDiv3, + Phase1_FloorDiv4, + Phase1_FloorDiv5 + // clang-format on + >(context); + + (void)applyPatternsGreedily(getOperation().getOperation(), + std::move(patterns)); + } + + { + OpPassManager phase_2("builtin.module"); + phase_2.addPass(mlir::odml::CreateUnfoldSplatConstantPass()); + if (failed(runPipeline(phase_2, getOperation()))) { + return signalPassFailure(); + } + } + + ConversionTarget target(*context); + target.addLegalDialect(); + target.addLegalOp(); + + target.addDynamicallyLegalOp(IsCbrtLegal); + target.addDynamicallyLegalOp(IsAbsOpLegal); + target.addDynamicallyLegalOp(IsImagOpLegal); + target.addDynamicallyLegalOp(IsRealOpLegal); + target.addDynamicallyLegalOp(IsNotOpLegal); + target.addDynamicallyLegalOp(IsCompareLegal); + target.addDynamicallyLegalOp(IsAddOpLegal); + target.addDynamicallyLegalOp(IsSubtractOpLegal); + target.addDynamicallyLegalOp(IsMinimumOpLegal); + target.addDynamicallyLegalOp( + [](mhlo::TupleOp op) { return std::nullopt; }); + + target.addIllegalOp< + // go/keep-sorted start + // clang-format off + mhlo::Atan2Op, + mhlo::BroadcastInDimOp, + mhlo::ClampOp, + mhlo::ConcatenateOp, + mhlo::ConstantOp, + mhlo::DivOp, + mhlo::DotGeneralOp, + mhlo::DotOp, + mhlo::DynamicBroadcastInDimOp, + mhlo::DynamicReshapeOp, + mhlo::MaxOp, + mhlo::MulOp, + mhlo::PowOp, + mhlo::RemOp, + mhlo::ReshapeOp, + mhlo::ReverseOp, + mhlo::RoundNearestEvenOp, + mhlo::RoundOp, + mhlo::SelectOp, + mhlo::ShiftRightArithmeticOp, + mhlo::ShiftRightLogicalOp, + mhlo::TransposeOp + // clang-format on + // go/keep-sorted end + >(); + + RewritePatternSet patterns(context); + + populateWithGenerated(patterns); + + SetUnaryOpLegal(target); + SetBinaryBitwiseLegal(target); + + PopulatePadPatterns(context, patterns, target); + PopulateReducePatterns(context, patterns, target); + PopulateLegalizeReduceWindowPatterns(context, patterns, target); + PopulateGatherPatterns(context, patterns, target); + PopulateLegalizeConvPatterns(context, patterns, target); + PopulateLegalizeSlicePatterns(context, patterns, target); + PopulateSortPatterns(context, patterns, target); + PopulateIotaPatterns(context, patterns, target); + PopulateWhilePatterns(context, patterns, target); + PopulateGetDimensionSizePatterns(context, patterns, target); + PopulateIfPatterns(context, patterns, target); + PopulateCasePatterns(context, patterns, target); + PopulateLegalizeFftPatterns(context, patterns, target); + PopulateCustomCallPatterns(context, patterns, target); + + patterns.add(context); + + if (failed(applyPartialConversion(getOperation(), target, + std::move(patterns)))) { + getOperation().emitError("mhlo to TFLite legalization failed."); + signalPassFailure(); + } +} + +} // namespace + +// Creates an instance of the pass. +std::unique_ptr> CreateLegalizeHloToTfLitePass() { + return std::make_unique(); +} + +// Registers the pass implementation +static PassRegistration pass; + +} // namespace odml +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/tflite_legalize_hlo_patterns.td b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/tflite_legalize_hlo_patterns.td new file mode 100644 index 0000000000000000000000000000000000000000..6e5fc82dc6882f98b55d8615b726f67cde89202a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/tflite_legalize_hlo_patterns.td @@ -0,0 +1,815 @@ +/* Copyright 2023 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 "mlir/Dialect/Arith/IR/ArithOps.td" +include "mlir/Dialect/Func/IR/FuncOps.td" +include "mlir/IR/CommonAttrConstraints.td" +include "mlir/IR/CommonTypeConstraints.td" +include "mlir/IR/OpBase.td" +include "tensorflow/compiler/mlir/lite/ir/tfl_op_enums.td" +include "tensorflow/compiler/mlir/lite/ir/tfl_ops.td" +include "mhlo/IR/hlo_ops.td" + + +def ShapeToConst : NativeCodeCall<"ShapeToConst($_builder, $0)">; + +def CreateTFLCastToInt32Op : NativeCodeCall< + "CreateCastToInt32($0, $_loc, $_builder)">; + +def ConstDefaultResultAccuracyAttr : + ConstantAttr; + +def : Pat< + (MHLO_ConstantOp:$output $value), + (Arith_ConstantOp $value), + [(TFL_TensorOf<[AnyType]> $output)]>; + + +//===----------------------------------------------------------------------===// +// data movement +//===----------------------------------------------------------------------===// + +def : Pat< + (MHLO_TransposeOp $arg, $perm), + (TFL_TransposeOp $arg, + (CreateTFLCastToInt32Op + (TFL_ConstOp $perm)))>; + +def : Pat< + (MHLO_ReverseOp $input, $dims), + (TFL_ReverseV2Op $input, + (CreateTFLCastToInt32Op + (Arith_ConstantOp $dims)))>; + +def I64AttrToI32Attr: + NativeCodeCall< + "$_builder.getI32IntegerAttr(" + "static_cast(llvm::cast($0).getInt()))">; + +def : Pat< + (MHLO_ConcatenateOp $inputs, $dim), + (TFL_ConcatenationOp + $inputs, + (I64AttrToI32Attr $dim), + TFL_AF_None)>; + +// +// Reshapes +// + +def : Pat< + (MHLO_ReshapeOp:$output $input), + (TFL_ReshapeOp $input, + (CreateTFLCastToInt32Op + (ShapeToConst $output)))>; + +def : Pat< + (MHLO_DynamicReshapeOp $input, $shape), + (TFL_ReshapeOp $input, + (CreateTFLCastToInt32Op $shape))>; + +// +// Broadcasts +// + +def IsTFLStyleBroadcast : + Constraint>; + +def IsNotTFLStyleBroadcast : + Constraint>; + +def ExpandedShape : + NativeCodeCall<"ExpandedShape($_builder, $0, $1, $2)">; + +def ExpandedDynamicShape : + NativeCodeCall<"ExpandedDynamicShape($_builder, $0, $1, $2)">; + +def : Pat< + (MHLO_BroadcastInDimOp:$output + $input, + $broadcast_dimensions), + (TFL_BroadcastToOp + $input, + (ShapeToConst $output)), + [(IsTFLStyleBroadcast $broadcast_dimensions, $output)]>; + +def : Pat< + (MHLO_BroadcastInDimOp:$output + $input, + $broadcast_dimensions), + (TFL_BroadcastToOp + (TFL_ReshapeOp + $input, + (ExpandedShape + $input, + $broadcast_dimensions, + $output)), + (ShapeToConst $output)), + [(IsNotTFLStyleBroadcast $broadcast_dimensions, $output)]>; + +def : Pat< + (MHLO_DynamicBroadcastInDimOp:$output + $input, + $output_dimensions, + $broadcast_dimensions, + $expanding_dimensions_unused, + $nonexpanding_dimensions_unused), + (TFL_BroadcastToOp + $input, + $output_dimensions), + [(IsTFLStyleBroadcast $broadcast_dimensions, $output)]>; + +def : Pat< + (MHLO_DynamicBroadcastInDimOp:$output + $input, + $output_dimensions, + $broadcast_dimensions, + $expanding_dimensions_unused, + $nonexpanding_dimensions_unused), + (TFL_BroadcastToOp + (ExpandedDynamicShape + $input, + $broadcast_dimensions, + $output), + $output_dimensions), + [(IsNotTFLStyleBroadcast $broadcast_dimensions, $output)]>; + + + +//===----------------------------------------------------------------------===// +// logical and bitwise ops +//===----------------------------------------------------------------------===// + +class GetRankedScalarAttr : + NativeCodeCall<"DenseElementsAttr::get<" # prefix # "int" # width # "_t>(" + "RankedTensorType::get({}, $_builder.getIntegerType(" + # width # signed # "))," # value # ")">; + +def : Pat<(MHLO_NotOp I1Tensor:$input), (TFL_LogicalNotOp $input)>; + +// TFL does not support bitwise negation. not(x) is equivalant to xor(x, y) if +// y has a 1 in every bit position (xor(1, 1) = 0 and xor(0, 1) = 1). + +// Signed: The 2s complement of -1 has a 1 in every bit position. +def : Pat<(MHLO_NotOp I8Tensor:$input), + (TFL_BitwiseXorOp $input, + (Arith_ConstantOp + (GetRankedScalarAttr<"", 8, "", "-1">)))>; + +def : Pat<(MHLO_NotOp I16Tensor:$input), + (TFL_BitwiseXorOp $input, + (Arith_ConstantOp + (GetRankedScalarAttr<"", 16, "", "-1">)))>; + +def : Pat<(MHLO_NotOp I32Tensor:$input), + (TFL_BitwiseXorOp $input, + (Arith_ConstantOp + (GetRankedScalarAttr<"", 32, "", "-1">)))>; + + +// Unsigned: 0xFFF... has a 1 in every bit position. +def : Pat<(MHLO_NotOp TensorOf<[UI8]>:$input), + (TFL_BitwiseXorOp $input, + (TFL_ConstOp + (GetRankedScalarAttr<"u", 8, ", false", "0xFFU">)))>; + +def : Pat<(MHLO_NotOp TensorOf<[UI16]>:$input), + (TFL_BitwiseXorOp $input, + (TFL_ConstOp + (GetRankedScalarAttr<"u", 16, ", false", "0xFFFFU">)))>; + +def : Pat<(MHLO_NotOp TensorOf<[UI32]>:$input), + (TFL_BitwiseXorOp $input, + (TFL_ConstOp + (GetRankedScalarAttr<"u", 32, ", false", "0xFFFFFFFFUL">)))>; + +foreach pair = [ + [MHLO_AndOp, TFL_LogicalAndOp], + [MHLO_OrOp, TFL_LogicalOrOp], +] in { + def : Pat< + (pair[0] TFL_BoolTensor:$l, TFL_BoolTensor:$r), + (pair[1] $l, $r)>; +} + +def LegalizeXor : Pat< + (MHLO_XorOp + TFL_IntTensor:$l, + TFL_IntTensor:$r), + (TFL_BitwiseXorOp $l, $r)>; + +//===----------------------------------------------------------------------===// +// binary element-wise ops +//===----------------------------------------------------------------------===// + +def : Pat< + (MHLO_ShiftRightArithmeticOp $l, $r), + (TFL_RightShiftOp $l, $r)>; + +def : Pat< + (MHLO_ShiftRightLogicalOp $l, $r), + (TFL_RightShiftOp $l, $r)>; + +def : Pat< + (MHLO_Atan2Op $l, $r), + (TFL_Atan2Op $l, $r)>; + +def : Pat< + (MHLO_RemOp $l, $r), + (TFL_FloorModOp $l, $r)>; + +// Binary ops with no attrs. +foreach pair = [ + [MHLO_MaxOp, TFL_MaximumOp], + [MHLO_MinOp, TFL_MinimumOp], + [MHLO_PowOp, TFL_PowOp], +] in { + def : Pat< + (pair[0] $l, $r), + (pair[1] $l, $r)>; + + def : Pat< + (pair[0] + (MHLO_BroadcastInDimOp:$output + $bcast_operand, + $broadcast_dimensions), + $r), + (pair[1] $bcast_operand, $r), + [(IsTFLStyleBroadcast $broadcast_dimensions, $output)]>; + + def : Pat< + (pair[0] + $l, + (MHLO_BroadcastInDimOp:$output + $bcast_operand, + $broadcast_dimensions)), + (pair[1] $l, $bcast_operand), + [(IsTFLStyleBroadcast $broadcast_dimensions, $output)]>; +} + +// Binary ops with fused activiation attr. +foreach pair = [ + [MHLO_DivOp, TFL_DivOp], + [MHLO_AddOp, TFL_AddOp], + [MHLO_MulOp, TFL_MulOp], + [MHLO_SubtractOp, TFL_SubOp], +] in { + def : Pat< + (pair[0] $l, $r), + (pair[1] $l, $r, TFL_AF_None)>; + + def : Pat< + (pair[0] + (MHLO_BroadcastInDimOp:$output + $bcast_operand, + $broadcast_dimensions), + $r), + (pair[1] $bcast_operand, $r, TFL_AF_None), + [(IsTFLStyleBroadcast $broadcast_dimensions, $output)]>; + + def : Pat< + (pair[0] + $l, + (MHLO_BroadcastInDimOp:$output + $bcast_operand, + $broadcast_dimensions)), + (pair[1] $l, $bcast_operand, TFL_AF_None), + [(IsTFLStyleBroadcast $broadcast_dimensions, $output)]>; +} + + + +//===----------------------------------------------------------------------===// +// comparison ops +//===----------------------------------------------------------------------===// + +// Check implicit bool cast of `$_self` to ensure Attribute is non-null before +// casting. +def HasSupportedComparisonType : AttrConstraint< + CPred<"!$_self || SupportedComparisonType(llvm::cast($_self))">>; + +class MHLO_ComparisonDirectionValue : + ConstantAttr; + +foreach p = [ + [TFL_EqualOp, MHLO_ComparisonDirectionValue<"EQ">], + [TFL_NotEqualOp, MHLO_ComparisonDirectionValue<"NE">], + [TFL_GreaterEqualOp, MHLO_ComparisonDirectionValue<"GE">], + [TFL_LessEqualOp, MHLO_ComparisonDirectionValue<"LE">], + [TFL_GreaterOp, MHLO_ComparisonDirectionValue<"GT">], + [TFL_LessOp, MHLO_ComparisonDirectionValue<"LT">]] +in { + def : Pat< + (MHLO_CompareOp $l, $r, p[1], HasSupportedComparisonType), + (p[0] $l, $r)>; +} + +//===----------------------------------------------------------------------===// +// unary element-wise op +//===----------------------------------------------------------------------===// + +def LowerCbrt : Pat<(MHLO_CbrtOp $opr, ConstDefaultResultAccuracyAttr), + (TFL_PowOp $opr, + (TFL_DivOp + (Arith_ConstantOp ConstantAttr, "1.0f">), + (Arith_ConstantOp ConstantAttr, "3.0f">), + TFL_AF_None)), + [(F32Tensor $opr)]>; + +// Pattern to legacyze mhlo.abs(complex) to tfl.complex_abs. +def : Pat<(MHLO_AbsOp MHLO_ComplexTensor:$arg), (TFL_ComplexAbsOp $arg)>; + +// Pattern to match non-complex abs. +def : Pat<(MHLO_AbsOp MHLO_PredIntFpOrQuantizedTensor:$arg), (TFL_AbsOp $arg)>; + +foreach pair = [ + [MHLO_BitcastConvertOp, TFL_BitcastOp], + [MHLO_CeilOp, TFL_CeilOp], + [MHLO_FloorOp, TFL_FloorOp], + [MHLO_ImagOp, TFL_ImagOp], + [MHLO_NegOp, TFL_NegOp], + [MHLO_RealOp, TFL_RealOp], + [MHLO_SignOp, TFL_SignOp], +] in { + def : Pat< + (pair[0] $input), + (pair[1] $input)>; +} + +foreach pair = [ + [MHLO_CosineOp, TFL_CosOp], + [MHLO_ExpOp, TFL_ExpOp], + [MHLO_LogOp, TFL_LogOp], + [MHLO_LogisticOp, TFL_LogisticOp], + [MHLO_RsqrtOp, TFL_RsqrtOp], + [MHLO_SineOp, TFL_SinOp], + [MHLO_SqrtOp, TFL_SqrtOp], + [MHLO_TanhOp, TFL_TanhOp], +] in { + def : Pat< + (pair[0] $input, ConstDefaultResultAccuracyAttr), + (pair[1] $input)>; +} + + +def : Pat< + (MHLO_ConvertOp $input), + (TFL_CastOp $input)>; + +def : Pat< + (MHLO_Expm1Op F32Tensor:$x, ConstDefaultResultAccuracyAttr), + (TFL_SubOp + (TFL_ExpOp $x), + (Arith_ConstantOp + ConstantAttr, "1.0f">), + TFL_AF_None)>; + +def : Pat< + (MHLO_IsFiniteOp F32Tensor:$x), + (TFL_EqualOp + (TFL_SubOp $x, $x, TFL_AF_None), + (Arith_ConstantOp + ConstantAttr, "0.0f">))>; + +def : Pat< + (MHLO_Log1pOp F32Tensor:$x, ConstDefaultResultAccuracyAttr), + (TFL_LogOp + (TFL_AddOp + $x, + (Arith_ConstantOp + ConstantAttr, "1.0f">), + TFL_AF_None))>; + +//===----------------------------------------------------------------------===// +// rounding +//===----------------------------------------------------------------------===// + +class ValueEquals : + Constraint>; + +def SameValue : + Constraint>; + +def FloatOrDefaultCompare : + Constraint>; + +def SameTypeOrDefaultCompare : + Constraint>; + +def ValueIsReciprocal : + Constraint>; + +def TensorIsSign : + Constraint>; + +def ValueGreaterThanZero : + Constraint>; + +def : Pat< + (MHLO_RoundNearestEvenOp $input), + (TFL_RoundOp $input)>; + +// Converts a dag of HLOs representing banker rounding (round x.5 to nearest +// even) to tfl.round. This only supports float types because mhlo.floor only +// supports float types. tf.round with integer input type will become an +// identity op, so we will never face an mhlo.floor with an integer input type. +// The pattern matched executes the following computation: +// frac = x - floor(x) +// to_even = (floor(x) - 2 * floor(0.5 * x)) == 1 +// if frac > 0.5 || (frac == 0.5 && to_even) +// return floor(x) + 1 +// else +// return floor(x) +def Phase1_Round : Pat<(MHLO_SelectOp + (MHLO_OrOp + (MHLO_CompareOp (MHLO_SubtractOp:$frac + $input, + (MHLO_FloorOp:$floor $input)), + (MHLO_ConstantOp $half), + MHLO_ComparisonDirectionValue<"GT">, + $compare_type0), + (MHLO_AndOp + (MHLO_CompareOp + $frac1, + (MHLO_ConstantOp $half1), + MHLO_ComparisonDirectionValue<"EQ">, + $compare_type1), + (MHLO_CompareOp + (MHLO_SubtractOp + $floor1, + (MHLO_MulOp + (MHLO_FloorOp (MHLO_MulOp $input, (MHLO_ConstantOp $half2))), + (MHLO_ConstantOp $two))), + (MHLO_ConstantOp $one1), + MHLO_ComparisonDirectionValue<"EQ">, + $compare_type2))), + (MHLO_AddOp $floor2, (MHLO_ConstantOp $one)), + $floor3), + (TFL_RoundOp $input), + [(ValueEquals<"1.0"> $one), + (ValueEquals<"1.0"> $one1), + (ValueEquals<"2.0"> $two), + (ValueEquals<"0.5"> $half), + (ValueEquals<"0.5"> $half1), + (ValueEquals<"0.5"> $half2), + (SameValue $floor, $floor1), + (SameValue $floor, $floor2), + (SameValue $floor, $floor3), + (SameValue $frac, $frac1), + (FloatOrDefaultCompare $compare_type0), + (FloatOrDefaultCompare $compare_type1), + (FloatOrDefaultCompare $compare_type2)]>; + +// Converts a dag of HLOs representing floor_mod to tfl.floor_mod. +// The pattern matched executes the following computation: +// +// rem = remainder(arg0, arg1) +// for i in 0 to len(arg1): +// if ((rem[i] < 0) != (arg0[i] < 0) && arg0[i] != 0) +// rem[i] += arg1[i] +// return rem +def Phase1_FloorMod : Pat<(MHLO_SelectOp + (MHLO_AndOp + (MHLO_CompareOp + (MHLO_CompareOp:$rltz + (MHLO_RemOp:$rem $arg, $arg1), + (MHLO_ConstantOp $cst), + MHLO_ComparisonDirectionValue<"LT">, + $compare_type), + (MHLO_CompareOp:$arg1ltz $arg1, (MHLO_ConstantOp $cst1), MHLO_ComparisonDirectionValue<"LT">, $compare_type1), + MHLO_ComparisonDirectionValue<"NE">, + $compare_type2), + (MHLO_CompareOp:$rnz $rem1, (MHLO_ConstantOp $cst2), MHLO_ComparisonDirectionValue<"NE">, $compare_type3)), + (MHLO_AddOp $rem2, $arg1), + $rem3), + (TFL_FloorModOp $arg, $arg1), + [(ValueEquals<"0.0"> $cst), + (ValueEquals<"0.0"> $cst1), + (ValueEquals<"0.0"> $cst2), + (SameValue $rem, $rem1), + (SameValue $rem, $rem2), + (SameValue $rem, $rem3), + (SameTypeOrDefaultCompare $compare_type, $cst), + (SameTypeOrDefaultCompare $compare_type1, $cst1)]>; + +// Converts a dag of HLOs representing floor_mod with a constant to +// tfl.floor_mod. The pattern matched executes the following computation: +// +// cst = value that is > 0 +// rem = remainder(arg0, cst) +// for i in 0 to len(arg1): +// if (rem[i] < 0 && rem[i] != 0) +// rem[i] += cst +// return rem +def Phase1_FloorMod2 : Pat<(MHLO_SelectOp + (MHLO_AndOp + (MHLO_CompareOp:$rltz + (MHLO_RemOp:$rem $arg, (MHLO_ConstantOp $cst)), + (MHLO_ConstantOp $cst1), + MHLO_ComparisonDirectionValue<"LT">, + $compare_type), + (MHLO_CompareOp:$rnz $rem1, (MHLO_ConstantOp $cst2), MHLO_ComparisonDirectionValue<"NE">, $compare_type3)), + (MHLO_AddOp $rem2, (MHLO_ConstantOp $cst3)), + $rem3), + (TFL_FloorModOp $arg, (Arith_ConstantOp $cst3)), + [(ValueGreaterThanZero $cst), + (ValueEquals<"0.0"> $cst1), + (ValueEquals<"0.0"> $cst2), + (SameValue $cst, $cst3), + (SameValue $rem, $rem1), + (SameValue $rem, $rem2), + (SameValue $rem, $rem3), + (SameTypeOrDefaultCompare $compare_type, $cst1), + (SameTypeOrDefaultCompare $compare_type3, $cst2)]>; + +// Converts a dag of HLOs representing floor_div to tfl.floor_div. +// The pattern matched executes the following computation: +// +// rem = remainder(arg0, arg1) +// for i in 0 to len(arg1): +// rem[i] = arg0[i] - rem[i] / arg1[i] +// if (rem[i] != 0 && sign(arg1[i]) != sign(rem[i])) +// rem[i] -= 1.0 +// return round_nearest_afz(rem) +// As a dag this looks like the following: +// round +// | +// -------- select +// | | \ +// && + div +// / | / \ +// != != div -1 +// / | / | / | +// rem 0.0 sn sn1 - $1 +// / | | | / | +// $0 $1 $1 rem $0 rem +// Note that named operators like 'sn' and 'sn1' are different values produced by +// the same function in this case the sign function. Named values like 'div' +// refer to the same value produced by the same function, in this case division. +// Mathematical symbols do not indicate a re-use of the value. +def Phase1_FloorDiv : Pat<(MHLO_RoundOp + (MHLO_SelectOp + (MHLO_AndOp + (MHLO_CompareOp + (MHLO_RemOp:$rem $arg0, $arg1), + (MHLO_ConstantOp $cst), + MHLO_ComparisonDirectionValue<"NE">, + $compare_type), + (MHLO_CompareOp + (MHLO_SignOp $arg1), + (MHLO_SignOp $rem1), + MHLO_ComparisonDirectionValue<"NE">, + $compare_type1)), + (MHLO_AddOp + (MHLO_DivOp:$div + (MHLO_SubtractOp $arg0, $rem2), + $arg1b), + (MHLO_ConstantOp $cst_neg1)), + $div1)), + (TFL_FloorDivOp $arg0, $arg1), + [(ValueEquals<"0.0"> $cst), + (ValueEquals<"-1.0"> $cst_neg1), + (SameValue $div, $div1), + (SameValue $rem, $rem1), + (SameValue $rem, $rem2), + (FloatOrDefaultCompare $compare_type, $cst), + (FloatOrDefaultCompare $compare_type1, $cst)]>; + +// Converts a dag of HLOs representing floor_div with a splat constant to +// tfl.floor_div. The pattern matched executes the following computation: +// This particular pattern matches multiplication with the reciprocal of the +// constant instead of dividing by the constant. +// rem = remainder(arg0, cst) +// for i in 0 to len(arg0): +// rem[i] = (arg0[i] - rem[i]) * 1 / cst +// if (rem[i] != 0 && sign(cst) != sign(rem[i])) +// rem[i] += -1.0 +// return round_nearest_afz(rem) +// As a dag this looks like the following: +// round +// | +// -------- select +// | | \ +// && + mul +// / | / \ +// != != mul -1 +// / | / | / | +// rem 0.0 cs1 sn1 - cs2 +// / | | / | +// $0 cst rem $0 rem +// cs1 == sign(cst) +// cs2 = 1 / cst i.e. the reciprocal +// Note that named operators like 'sn' and 'sn1' are different values produced by +// the same function in this case the sign function. Named values like 'div' +// refer to the same value produced by the same function, in this case division. +// Mathematical symbols do not indicate a re-use of the value. +def Phase1_FloorDiv2 : Pat<(MHLO_RoundOp + (MHLO_SelectOp + (MHLO_AndOp + (MHLO_CompareOp + (MHLO_RemOp:$rem $arg0, (MHLO_ConstantOp:$cst $cstv)), + (MHLO_ConstantOp $cst_zero), + MHLO_ComparisonDirectionValue<"NE">, + $compare_type), + (MHLO_CompareOp + (MHLO_ConstantOp $cst_sgn), + (MHLO_SignOp $rem1), + MHLO_ComparisonDirectionValue<"NE">, + $compare_type1)), + (MHLO_AddOp + (MHLO_MulOp:$mul + (MHLO_SubtractOp $arg0, $rem2), + (MHLO_ConstantOp $cst_recip)), + (MHLO_ConstantOp $cst_neg1)), + $mul1)), + (TFL_FloorDivOp $arg0, $cst), + [(ValueEquals<"0.0"> $cst_zero), + (ValueEquals<"-1.0"> $cst_neg1), + (TensorIsSign $cstv, $cst_sgn), + (ValueIsReciprocal $cstv, $cst_recip), + (SameValue $mul, $mul1), + (SameValue $rem, $rem1), + (SameValue $rem, $rem2), + (FloatOrDefaultCompare $compare_type, $cst_zero), + (FloatOrDefaultCompare $compare_type1, $cst_sgn)]>; + +// Converts a dag of HLOs representing floor_div with a splat constant to +// tfl.floor_div. The pattern matched executes the following computation: +// This particular pattern matches division with the constant. +// . +// rem = remainder(arg0, cst) +// for i in 0 to len(arg0): +// rem[i] = (arg0[i] - rem[i]) / cst +// if (rem[i] != 0 && sign(cst) != sign(rem[i])) +// rem[i] -= 1.0 +// return round_nearest_afz(rem) +// As a dag this looks like the following: +// round +// | +// -------- select +// | | \ +// && + div +// / | / \ +// != != div -1 +// / | / | / | +// rem 0.0 cs1 sn1 - cs2 +// / | | / | +// $0 cst rem $0 rem +// cs1 == sign(cst) +// cs2 = 1 / cst i.e. the reciprocal +// Note that named operators like 'sn' and 'sn1' are different values produced by +// the same function in this case the sign function. Named values like 'div' +// refer to the same value produced by the same function, in this case division. +// Mathematical symbols do not indicate a re-use of the value. +def Phase1_FloorDiv3 : Pat<(MHLO_RoundOp + (MHLO_SelectOp + (MHLO_AndOp + (MHLO_CompareOp + (MHLO_RemOp:$rem $arg0, (MHLO_ConstantOp:$cst $cstv)), + (MHLO_ConstantOp $cst_zero), + MHLO_ComparisonDirectionValue<"NE">, + $compare_type), + (MHLO_CompareOp + (MHLO_ConstantOp $cst_sgn), + (MHLO_SignOp $rem1), + MHLO_ComparisonDirectionValue<"NE">, + $compare_type1)), + (MHLO_AddOp + (MHLO_DivOp:$div + (MHLO_SubtractOp $arg0, $rem2), + (MHLO_ConstantOp $cstv1)), + (MHLO_ConstantOp $cst_neg1)), + $div1)), + (TFL_FloorDivOp $arg0, $cst), + [(ValueEquals<"0.0"> $cst_zero), + (ValueEquals<"-1.0"> $cst_neg1), + (TensorIsSign $cstv, $cst_sgn), + (SameValue $div, $div1), + (SameValue $rem, $rem1), + (SameValue $rem, $rem2), + (SameValue $cstv1, $cstv), + (FloatOrDefaultCompare $compare_type, $cst_zero), + (FloatOrDefaultCompare $compare_type1, $cst_sgn)]>; + +// Converts a dag of HLOs representing floor_div with a broadcasted vector +// constant to tfl.floor_div. The pattern matched executes the following +// computation: +// scs = sign(cst) +// bcst = broadcast(cst) +// rem = remainder(arg0, bcst) +// for i in 0 to len(arg0): +// rem[i] = arg0[i] - rem[i] * / bcst +// if (rem[i] != 0 && scs != sign(rem[i])) +// rem[i] -= 1.0 +// return round_nearest_afz(rem) +// Where scs is a splat constant folded sign on the unbroadcasted tensor. +// +// As a dag this looks like the following: +// round +// | +// -------- select +// | | \ +// && + div +// / | / \ +// != != div -1 +// / | / | / | +// rem 0.0 scs sn1 - bcst +// / | | / | +// $0 bcst rem $0 rem +// | +// cst +// scs == sign(cst) == sign(bcst) +// Note that named operators like 'sn' and 'sn1' are different values produced by +// the same function in this case the sign function. Named values like 'div' +// refer to the same value produced by the same function, in this case division. +// Mathematical symbols do not indicate a re-use of the value. +def Phase1_FloorDiv4 : Pat<(MHLO_RoundOp + (MHLO_SelectOp + (MHLO_AndOp + (MHLO_CompareOp + (MHLO_RemOp:$rem $arg0, + (MHLO_BroadcastInDimOp:$bcst + (MHLO_ConstantOp $cstv), + $broadcast_dimension)), + (MHLO_ConstantOp $cst_zero), + MHLO_ComparisonDirectionValue<"NE">, + $compare_type), + (MHLO_CompareOp + (MHLO_ConstantOp $cst_sgn), + (MHLO_SignOp $rem1), + MHLO_ComparisonDirectionValue<"NE">, + $compare_type1)), + (MHLO_AddOp + (MHLO_DivOp:$div + (MHLO_SubtractOp $arg0, $rem2), + $bcst1), + (MHLO_ConstantOp $cst_neg1)), + $div1)), + (TFL_FloorDivOp $arg0, $bcst), + [(ValueEquals<"0.0"> $cst_zero), + (ValueEquals<"-1.0"> $cst_neg1), + (TensorIsSign $cstv, $cst_sgn), + (SameValue $bcst, $bcst1), + (SameValue $div, $div1), + (SameValue $rem, $rem1), + (SameValue $rem, $rem2), + (FloatOrDefaultCompare $compare_type, $cst_zero), + (FloatOrDefaultCompare $compare_type1, $cst_sgn)]>; + +// Semantically equivalant to above, just uses sub(x, 1) instead of +// add(x, -1). +def Phase1_FloorDiv5 : Pat< + (MHLO_RoundOp + (MHLO_SelectOp + (MHLO_AndOp + (MHLO_CompareOp + (MHLO_RemOp:$rem + $arg0, + (MHLO_ConstantOp:$cstv_op $cstv)), + (MHLO_ConstantOp $cst_zero), + MHLO_ComparisonDirectionValue<"NE">, + $compare_type), + (MHLO_CompareOp + (MHLO_ConstantOp $cst_sgn), + (MHLO_SignOp $rem1), + MHLO_ComparisonDirectionValue<"NE">, + $compare_type1)), + (MHLO_SubtractOp + (MHLO_DivOp:$div + (MHLO_SubtractOp $arg0, $rem2), + $cstv_op_2), + (MHLO_ConstantOp $cst_1)), + $div1)), + (TFL_FloorDivOp $arg0, $cstv_op), + [(ValueEquals<"0.0"> $cst_zero), + (ValueEquals<"1.0"> $cst_1), + (TensorIsSign $cstv, $cst_sgn), + (SameValue $cstv_op, $cstv_op_2), + (SameValue $div, $div1), + (SameValue $rem, $rem1), + (SameValue $rem, $rem2), + (FloatOrDefaultCompare $compare_type, $cst_zero), + (FloatOrDefaultCompare $compare_type1, $cst_sgn)]>; + + + +//===----------------------------------------------------------------------===// +// ternary op patterns. +//===----------------------------------------------------------------------===// + +def : Pat<(MHLO_ClampOp $min, $arg, $max), + (TFL_MaximumOp (TFL_MinimumOp $arg, $max), $min)>; +def : Pat<(MHLO_SelectOp $cond, $t, $e), (TFL_SelectOp $cond, $t, $e)>; diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/torch/build_stablehlo_composite_pass.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/torch/build_stablehlo_composite_pass.cc new file mode 100644 index 0000000000000000000000000000000000000000..97585b6ca13a1e10375d41f2801961f8de5dc4fb --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/torch/build_stablehlo_composite_pass.cc @@ -0,0 +1,635 @@ +/* Copyright 2024 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 +#include +#include +#include +#include +#include + +#include "absl/strings/str_cat.h" +#include "json/json.h" +#include "json/reader.h" +#include "json/value.h" +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SetVector.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/Casting.h" +#include "mlir/Analysis/TopologicalSortUtils.h" // from @llvm-project +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/IRMapping.h" // from @llvm-project +#include "mlir/IR/Location.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/OpDefinition.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/SymbolTable.h" // from @llvm-project +#include "mlir/IR/Types.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Support/TypeID.h" // from @llvm-project +#include "stablehlo/dialect/StablehloOps.h" // from @stablehlo +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h" // IWYU pragma: keep + +namespace mlir { +namespace odml { + +#define GEN_PASS_DEF_BUILDSTABLEHLOCOMPOSITEPASS +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h.inc" + +namespace { + +// Checks if this operation is a MarkTensor operation used to mark the +// boundaries of a composite. +static bool IsMarkTensorOp(mlir::Operation* op) { + if (op == nullptr) { + return false; + } + if (op->getNumOperands() != 1 || op->getNumResults() != 1) { + return false; + } + if (!llvm::isa(op)) { + return false; + } + auto target_name = + mlir::dyn_cast(op->getAttr("call_target_name")); + if (target_name == nullptr || target_name.str() != "mark_tensor") { + return false; + } + return true; +} + +struct BoundaryMetadata { + std::string name; + std::string id; + int64_t pos; + bool is_input; + std::unordered_map attrs; + + auto boundary_key() const { return absl::StrCat(name, "__@@__", id); } + + auto uid() const { return std::forward_as_tuple(name, id, pos, is_input); } + + bool operator==(const BoundaryMetadata& other) const { + return uid() == other.uid(); + } + bool operator<(const BoundaryMetadata& other) const { + return uid() < other.uid(); + } + + static std::unique_ptr Parse(llvm::StringRef str_ref) { + Json::Value root; + Json::Reader reader; + if (!reader.parse(str_ref.str(), root)) { + return nullptr; + } + return Build(root); + } + + private: + template + static bool CopyJsonValue(const Json::Value& json, llvm::StringRef key, + Json::ValueType expected_type, T* to) { + if (!json.isMember(key.str()) || json[key.str()].type() != expected_type) { + return false; + } + + *to = json[key.str()].as(); + return true; + } + + static std::unique_ptr Build(const Json::Value& json) { + BoundaryMetadata metadata; + + bool is_valid_metadata_json = + CopyJsonValue(json, "name", Json::stringValue, &metadata.name) && + CopyJsonValue(json, "id", Json::stringValue, &metadata.id) && + CopyJsonValue(json, "pos", Json::intValue, &metadata.pos) && + CopyJsonValue(json, "is_input", Json::booleanValue, &metadata.is_input); + + if (!is_valid_metadata_json) { + return nullptr; + } + + Json::Value attrs_value = json["attr"]; + if (attrs_value.type() == Json::objectValue) { + for (const auto& key_value : attrs_value.getMemberNames()) { + metadata.attrs.insert({key_value, attrs_value[key_value]}); + } + } + return std::make_unique(std::move(metadata)); + } +}; + +class BuildStableHLOCompositePass + : public impl::BuildStableHLOCompositePassBase< + BuildStableHLOCompositePass> { + public: + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(BuildStableHLOCompositePass); + + void runOnOperation() override { + mlir::ModuleOp module_op = getOperation(); + llvm::SmallVector func_ops( + module_op.getOps()); + for (mlir::func::FuncOp& func_op : func_ops) { + llvm::DenseMap op_order_map = + BuildOpOrderMap(func_op); + llvm::SmallVector> + boundary_output_ops_map = BuildBoundaryOutputOpsMap(func_op); + + for (const auto& ops : boundary_output_ops_map) { + if (mlir::failed(BuildStableHLOComposite(ops, op_order_map))) { + func_op.emitError() << "failed to build composite."; + return signalPassFailure(); + } + } + } + + // Remove mark_tensor custom_call ops. + getOperation()->walk([](mlir::stablehlo::CustomCallOp op) { + if (!IsMarkTensorOp(op.getOperation())) { + return; + } + mlir::Value original_value = op.getOperand(0); + + for (mlir::Value result : op.getResults()) { + result.replaceAllUsesWith(original_value); + } + op.erase(); + }); + + getOperation()->walk([&](mlir::stablehlo::CompositeOp composite_op) { + if (IsCompositeOpOutlined(composite_op)) { + // According to the design of torch API, each boundary has one and only + // one usage in the entire model, which corresponds to one unique + // composite op in module. If the composite op is marked as + // outlined, there should be a same composite op nested in another + // composite op, and this composite op should be unused and safe to + // erase. + EraseCompositeOp(composite_op); + } + }); + } + + private: + void MarkCompositeOpOutlined(mlir::stablehlo::CompositeOp composite_op) { + composite_op->setAttr("_outlined_to_be_erased", + mlir::UnitAttr::get(composite_op.getContext())); + } + + bool IsCompositeOpOutlined(mlir::stablehlo::CompositeOp composite_op) { + return composite_op->hasAttr("_outlined_to_be_erased"); + } + + void EraseCompositeOp(mlir::stablehlo::CompositeOp composite_op) { + // CompositeOps have side effects and are not eligible for CSE. This + // function replaces a CompositeOp with a semantically equivalent + // 'dummy_to_be_erased' CustomCallOp that *lacks* side effects. This allows + // the CSE pass to eliminate the dummy op and, transitively, any users of + // the original CompositeOp. + mlir::OpBuilder builder(&getContext()); + + builder.setInsertionPointAfter(composite_op); + auto dummy_op = mlir::stablehlo::CustomCallOp::create( + builder, composite_op.getLoc(), composite_op.getResultTypes(), + composite_op.getOperands(), + llvm::SmallVector{ + builder.getNamedAttr("call_target_name", + builder.getStringAttr("dummy_to_be_erased")), + builder.getNamedAttr( + "has_side_effect", + builder.getIntegerAttr(builder.getI1Type(), 0)), + }); + + for (auto [old_result, new_result] : + llvm::zip(composite_op.getResults(), dummy_op.getResults())) { + old_result.replaceAllUsesWith(new_result); + } + composite_op.erase(); + } + + llvm::DenseMap BuildOpOrderMap( + mlir::func::FuncOp func_op) const { + llvm::DenseMap op_order_map; + for (const auto& op : llvm::enumerate(func_op.getOps())) { + op_order_map[&op.value()] = op.index(); + } + return op_order_map; + } + + llvm::SmallVector> + BuildBoundaryOutputOpsMap(mlir::func::FuncOp func_op) { + std::unordered_map> + boundary_output_ops; + + llvm::SetVector, + std::unordered_set> + ordered_boundary_keys; + + for (auto op : func_op.getOps()) { + auto metadata_or = GetBoundaryMetadata(op); + if (mlir::failed(metadata_or)) { + continue; + } + + std::unique_ptr metadata = std::move(*metadata_or); + if (metadata == nullptr || metadata->is_input) { + continue; + } + + std::string boundary_key = metadata->boundary_key(); + auto& output_ops = boundary_output_ops[boundary_key]; + if (metadata->pos >= output_ops.size()) { + output_ops.resize(metadata->pos + 1, nullptr); + } + output_ops[metadata->pos] = op.getOperation(); + + // Update the boundary order, which is determined by the order of the + // last output op of each boundary. + ordered_boundary_keys.remove(boundary_key); + ordered_boundary_keys.insert(boundary_key); + } + + llvm::SmallVector> + ordered_boundary_output_ops; + for (const auto& boundary_key : ordered_boundary_keys) { + ordered_boundary_output_ops.push_back( + std::move(boundary_output_ops[boundary_key])); + } + return ordered_boundary_output_ops; + } + + mlir::FailureOr> GetBoundaryMetadata( + mlir::Operation* op) { + if (!IsMarkTensorOp(op)) { + return mlir::FailureOr>(nullptr); + } + auto backend_config = + mlir::dyn_cast(op->getAttr("backend_config")); + if (backend_config == nullptr) { + return mlir::FailureOr>(nullptr); + } + std::unique_ptr metadata = + BoundaryMetadata::Parse(backend_config); + if (metadata == nullptr) { + return op->emitError() << "invalid boundary metadata JSON."; + } + return metadata; + } + + mlir::FailureOr BuildAttrFromJson( + mlir::OpBuilder& builder, mlir::Operation* op, + const Json::Value& json_value) { + switch (json_value.type()) { + case Json::intValue: + case Json::uintValue: + return builder.getI64IntegerAttr(json_value.as()); + case Json::ValueType::realValue: + return builder.getF32FloatAttr(json_value.as()); + case Json::ValueType::booleanValue: + return builder.getBoolAttr(json_value.as()); + case Json::ValueType::stringValue: + return builder.getStringAttr(json_value.as()); + case Json::ValueType::arrayValue: { + if (json_value.empty()) { + return builder.getArrayAttr({}); + } + auto get_json_type = [](const Json::Value& json_value) { + auto ty = json_value.type(); + if (ty == Json::uintValue) { + return Json::intValue; + } + return ty; + }; + + auto head_type = get_json_type(json_value[0]); + bool is_homogeneous = llvm::all_of(json_value, [&](auto& el) { + return get_json_type(el) == head_type; + }); + if (!is_homogeneous) { + return op->emitError() + << "invalid JSON to MLIR, arrays must be homogeneous"; + } + + switch (head_type) { + case Json::intValue: { + llvm::SmallVector int_values; + for (const auto& json_value : json_value) { + int_values.push_back(json_value.as()); + } + return builder.getI64TensorAttr(int_values); + } + case Json::realValue: { + llvm::SmallVector float_values; + for (const auto& json_value : json_value) { + float_values.push_back(json_value.as()); + } + return mlir::DenseFPElementsAttr::get( + mlir::RankedTensorType::get(json_value.size(), + builder.getF32Type()), + float_values); + } + case Json::booleanValue: { + llvm::SmallVector bool_values; + for (const auto& json_value : json_value) { + bool_values.push_back(json_value.as()); + } + return mlir::DenseIntElementsAttr::get( + mlir::RankedTensorType::get(json_value.size(), + builder.getI1Type()), + bool_values); + } + default: + return op->emitError() + << "invalid JSON to MLIR: invalid array type. arrays must " + "be " + "1-D homogeneous arrays of supported primitive types"; + } + } + default: + return op->emitError() + << "invalid JSON to MLIR: unsupported json value type"; + } + } + + mlir::FailureOr BuildDictionaryAttrFromJsonMap( + mlir::OpBuilder& builder, mlir::Operation* op, + const std::unordered_map& json_map) { + llvm::SmallVector named_attrs; + for (auto& [key, json] : json_map) { + mlir::FailureOr attribute_or = + BuildAttrFromJson(builder, op, json); + if (mlir::failed(attribute_or)) { + return mlir::failure(); + } + named_attrs.push_back({builder.getStringAttr(key), *attribute_or}); + } + return builder.getDictionaryAttr(named_attrs); + } + + mlir::LogicalResult BuildStableHLOComposite( + const llvm::SmallVector& output_ops, + llvm::DenseMap& op_order_map) { + if (output_ops.empty()) { + return mlir::success(); + } + + // Get the output op with minimum order num as the representative. + mlir::Operation* first_output_op = output_ops[0]; + for (mlir::Operation* op : output_ops) { + if (op_order_map.at(op) < op_order_map.at(first_output_op)) { + first_output_op = op; + } + } + + auto metadata_or = GetBoundaryMetadata(first_output_op); + if (mlir::failed(metadata_or)) { + return mlir::failure(); + } + + std::unique_ptr metadata = std::move(*metadata_or); + if (metadata == nullptr || metadata->is_input) { + // There should always be a valid boundary output metadata associated with + // each op in output_ops. + return mlir::failure(); + } + + auto args_ops_or = + GetBoundaryArgsAndOps(output_ops, *metadata, op_order_map); + if (mlir::failed(args_ops_or)) { + return mlir::failure(); + } + + auto [args, impl_ops] = *args_ops_or; + + mlir::func::FuncOp impl_func = BuildStableHLOCompositeImplFunc( + output_ops, absl::StrCat(metadata->name, ".impl"), args, impl_ops); + mlir::FailureOr composite_op_or = + BuildStableHLOCompositeOp(first_output_op, impl_func, args, *metadata); + if (mlir::failed(composite_op_or)) { + return mlir::failure(); + } + mlir::Operation* composite_op = *composite_op_or; + + // Updates all users of this op's result(s) to use the results(s) of impl + // func call. + size_t composite_result_i = 0; + for (mlir::Operation* op : output_ops) { + for (size_t i = 0; i < op->getNumResults(); ++i) { + mlir::OpResult result = op->getResult(i); + result.replaceAllUsesWith( + composite_op->getResult(composite_result_i++)); + } + } + + // Composite op inherits the order of the first output op. The output ops + // are not going to be used after this anyway so the duplication is fine. + op_order_map[composite_op] = op_order_map[first_output_op]; + + if (!mlir::sortTopologically(composite_op->getBlock())) { + composite_op->emitError() + << "The graph is not acyclic after BuildStableHLOCompositePass pass."; + return mlir::failure(); + } + // The unused impl_ops will be eliminated with canonicalizer. + return mlir::success(); + } + + mlir::FailureOr, + llvm::SmallVector>> + GetBoundaryArgsAndOps( + const llvm::SmallVector boundary_output_ops, + const BoundaryMetadata& metadata, + const llvm::DenseMap& op_order_map) { + llvm::SetVector impl_ops_setvec; + llvm::SetVector> arg_pos_setvec; + llvm::SmallVector processing(boundary_output_ops.begin(), + boundary_output_ops.end()); + + // Reverse graph traversal: from boundary output op to boundary input op, + // global function arg, or stablehlo constant. + while (!processing.empty()) { + mlir::Operation* curr_op = processing.back(); + processing.pop_back(); + if (impl_ops_setvec.contains(curr_op)) { + continue; + } + + auto curr_metadata_or = GetBoundaryMetadata(curr_op); + if (mlir::failed(curr_metadata_or)) { + return mlir::failure(); + } + std::unique_ptr curr_metadata = + std::move(*curr_metadata_or); + if (curr_metadata != nullptr) { + if (curr_metadata->is_input && + curr_metadata->boundary_key() == metadata.boundary_key()) { + // Terminal condition: boundary input op. + + arg_pos_setvec.insert( + {mlir::dyn_cast(curr_op->getResult(0)), + curr_metadata->pos}); + continue; + } + } + + impl_ops_setvec.insert(curr_op); + for (mlir::Value value : curr_op->getOperands()) { + mlir::Operation* def_op = value.getDefiningOp(); + if (def_op == nullptr) { + // Terminal condition: global function arg + arg_pos_setvec.insert({value, std::numeric_limits::max()}); + } else if (def_op->hasTrait()) { + // Terminal condition: constant + impl_ops_setvec.insert(def_op); + } else { + processing.push_back(def_op); + } + } + } + // Sorts all ops within the boundary by their line numbers in the input + // MLIR. The ops will be duplicated to the impl function following this + // order. + llvm::SmallVector impl_ops = impl_ops_setvec.takeVector(); + for (auto& op : impl_ops) { + if (!op_order_map.contains(op)) { + return op->emitError() + << "does not have a ordering number in its outer func."; + } + } + std::sort(impl_ops.begin(), impl_ops.end(), + [&op_order_map](const auto& a, const auto& b) { + return op_order_map.at(a) < op_order_map.at(b); + }); + + // Sorts boundary args by their positions. Note that the args of the + // composite and impl function may be more than the boundary inputs, because + // the MLIR is lowered from the functionalized graph and additional args may + // be Pytorch constants. In such case the position of those args would be + // undetermined, while they would always come after boundary inputs. + auto arg_pos_pairs = arg_pos_setvec.takeVector(); + std::stable_sort( + arg_pos_pairs.begin(), arg_pos_pairs.end(), + [](const auto& a, const auto& b) { return a.second < b.second; }); + llvm::SmallVector args; + args.reserve(arg_pos_pairs.size()); + for (auto& [arg, unused] : arg_pos_pairs) { + args.push_back(arg); + } + + return std::make_pair(std::move(args), std::move(impl_ops)); + } + + mlir::func::FuncOp BuildStableHLOCompositeImplFunc( + const llvm::SmallVector boundary_output_ops, + llvm::StringRef func_name, const llvm::SmallVector& args, + const llvm::SmallVector& impl_ops) { + mlir::ModuleOp module_op = getOperation(); + mlir::MLIRContext* context = &getContext(); + mlir::OpBuilder builder(context); + + // Creates composite impl function and duplicates all ops within the + // boundary in the function. + llvm::SmallVector arg_locs; + llvm::SmallVector arg_types; + for (auto& arg : args) { + arg_types.push_back(arg.getType()); + arg_locs.push_back(arg.getLoc()); + } + llvm::SmallVector result_types; + for (mlir::Operation* op : boundary_output_ops) { + result_types.append(op->getResultTypes().begin(), + op->getResultTypes().end()); + } + + mlir::func::FuncOp impl_func = mlir::func::FuncOp::create( + builder, module_op.getLoc(), func_name, + mlir::FunctionType::get(context, arg_types, result_types)); + mlir::IRMapping mapping; + builder.createBlock(&impl_func.getBody(), impl_func.begin(), arg_types, + arg_locs); + for (const auto& arg : llvm::enumerate(args)) { + mapping.map(arg.value(), impl_func.getArgument(arg.index())); + } + for (mlir::Operation* original_op : impl_ops) { + mlir::Operation* cloned_op = builder.clone(*original_op, mapping); + mapping.map(original_op, cloned_op); + if (auto original_composite_op = + mlir::dyn_cast(original_op); + original_composite_op != nullptr) { + MarkCompositeOpOutlined(original_composite_op); + } + } + + llvm::SmallVector results; + for (mlir::Operation* op : boundary_output_ops) { + results.append(mapping.lookup(op)->getResults().begin(), + mapping.lookup(op)->getResults().end()); + } + mlir::func::ReturnOp::create(builder, impl_func.getBody().getLoc(), + results); + + // Adds the new function to symbol table. + mlir::SymbolTable symbol_table(module_op); + impl_func.setPrivate(); + symbol_table.insert(impl_func); + + return impl_func; + } + + mlir::FailureOr BuildStableHLOCompositeOp( + mlir::Operation* boundary_output_op, mlir::func::FuncOp impl_func, + const llvm::SmallVector& args, + const BoundaryMetadata& metadata) { + mlir::MLIRContext* context = &getContext(); + mlir::OpBuilder builder(context); + + mlir::FailureOr attributes_or = + BuildDictionaryAttrFromJsonMap(builder, boundary_output_op, + metadata.attrs); + if (mlir::failed(attributes_or)) { + return boundary_output_op->emitError() + << "failed to transform boundary attr " + "JSON into composite attributes."; + } + + // Creates and inserts composite call op. + builder.setInsertionPointAfter(boundary_output_op); + mlir::Operation* composite_op = mlir::stablehlo::CompositeOp::create( + builder, boundary_output_op->getLoc(), + impl_func.getFunctionType().getResults(), args, metadata.name, + *attributes_or, impl_func.getSymName()); + return composite_op; + } +}; + +static PassRegistration pass; + +} // namespace +} // namespace odml +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/torch/legalize_qdq_custom_call_pass.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/torch/legalize_qdq_custom_call_pass.cc new file mode 100644 index 0000000000000000000000000000000000000000..3b857cebf0ed6a79d4dd23cae02ac15d21b2ab86 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/torch/legalize_qdq_custom_call_pass.cc @@ -0,0 +1,76 @@ +/* Copyright 2024 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 "mlir/AsmParser/AsmParser.h" // from @llvm-project +#include "mlir/Dialect/Quant/IR/Quant.h" // from @llvm-project // IWYU pragma: keep +#include "mlir/Dialect/Quant/IR/QuantTypes.h" // from @llvm-project // IWYU pragma: keep +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/TypeID.h" // from @llvm-project +#include "stablehlo/dialect/StablehloOps.h" // from @stablehlo +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h" // IWYU pragma: keep + +namespace mlir { +namespace odml { +#define GEN_PASS_DEF_LEGALIZEQDQCUSTOMCALLPASS +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h.inc" + +namespace { + +// Quant dialect is not registered in the Python MLIR pybinding used by +// odml-torch. Therefore, stablehlo.uniform_quantize/uniform_dequantize ops and +// quant types are lowered as stablehlo.custom_call to pass MLIR verification +// and VHLO serialization before converter. This pass reconstruct Q/DQ ops and +// quant types from those custom_call ops. + +class LegalizeQDQCustomCallPass + : public impl::LegalizeQDQCustomCallPassBase { + public: + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(LegalizeQDQCustomCallPass); + + void runOnOperation() override { + mlir::MLIRContext* context = &getContext(); + mlir::OpBuilder builder(context); + + getOperation()->walk([&](stablehlo::CustomCallOp op) { + llvm::StringRef target_name = op.getCallTargetNameAttr().strref(); + if (target_name == "odml_torch.uniform_quantize") { + auto quant_result_ty = + mlir::dyn_cast(op.getBackendConfigAttr()); + if (!quant_result_ty) { + return; + } + builder.setInsertionPointAfter(op); + auto q_op = stablehlo::UniformQuantizeOp::create( + builder, op.getLoc(), mlir::parseType(quant_result_ty, context), + op.getOperand(0)); + op.getResult(0).replaceAllUsesWith(q_op.getResult()); + op.erase(); + } else if (target_name == "odml_torch.uniform_dequantize") { + builder.setInsertionPointAfter(op); + auto dq_op = stablehlo::UniformDequantizeOp::create( + builder, op.getLoc(), op.getResult(0).getType(), op.getOperand(0)); + op.getResult(0).replaceAllUsesWith(dq_op.getResult()); + op.erase(); + } + }); + } +}; + +} // namespace +} // namespace odml +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/torch/lift_callsite_loc_caller_pass.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/torch/lift_callsite_loc_caller_pass.cc new file mode 100644 index 0000000000000000000000000000000000000000..c35704aac58cf51756acff44b2b520e7c0a2960e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/torch/lift_callsite_loc_caller_pass.cc @@ -0,0 +1,59 @@ +/* Copyright 2024 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 "llvm/Support/Casting.h" +#include "mlir/IR/Location.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/TypeID.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h" // IWYU pragma: keep + +namespace mlir { +namespace odml { +#define GEN_PASS_DEF_LIFTCALLSITELOCCALLERPASS +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h.inc" + +namespace { + +// JAX bridge generates a func.call for each op lowering +// These are inlined but loc will be messed up after the inline pass. This pass +// normalize the loc after inline pass. + +class LiftCallSiteLocCallerPass + : public impl::LiftCallSiteLocCallerPassBase { + public: + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(LiftCallSiteLocCallerPass); + + void runOnOperation() override { + getOperation()->walk([](mlir::Operation* op) { + while (true) { + auto loc = mlir::dyn_cast_or_null(op->getLoc()); + if (loc == nullptr) { + return; + } + + if (llvm::isa(loc.getCallee())) { + op->setLoc(loc.getCaller()); + } else { + op->setLoc(loc.getCallee()); + } + } + }); + } +}; + +} // namespace +} // namespace odml +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/transforms.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/transforms.cc new file mode 100644 index 0000000000000000000000000000000000000000..f39022da914817218ce173e1d5b1ad7facd18ff6 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/transforms.cc @@ -0,0 +1,129 @@ +/* Copyright 2021 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/compiler/mlir/lite/stablehlo/transforms/transforms.h" + +#include "mlir/Pass/PassManager.h" // from @llvm-project +#include "mlir/Transforms/Passes.h" // from @llvm-project +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/legalize_tf_xla_call_module_to_stablehlo_pass.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/smuggle_disallowed_ops.h" +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h" +#include "tensorflow/compiler/mlir/quantization/stablehlo/passes/bridge/passes.h" +#include "tensorflow/compiler/mlir/stablehlo/transforms/fold_broadcast_pass.h" +#include "tensorflow/compiler/mlir/stablehlo/transforms/rename_entrypoint_to_main.h" +#include "tensorflow/compiler/mlir/stablehlo/transforms/tf_stablehlo_pass.h" +#include "tensorflow/compiler/mlir/tensorflow/transforms/passes.h" +#include "tensorflow/compiler/mlir/tensorflow/transforms/tf_saved_model_passes.h" +#include "tensorflow/compiler/mlir/tf2xla/transforms/passes.h" +#include "xla/mlir_hlo/mhlo/transforms/passes.h" + +namespace mlir { +namespace odml { + +void AddTFToStablehloPasses(OpPassManager& pm, bool skip_resize, + bool smuggle_disallowed_ops) { + pm.addPass(CreateRenameEntrypointToMainPass()); + + // if the input is a call_xla_module, then unwrap the content + pm.addPass(mlir::odml::CreateLegalizeTFXlaCallModuleToStablehloPass()); + // TODO: b/230572023 - Consider improving shape inference for While op instead + // of dropping the attribute. This need not be correct for models not trained + // on TPU. + + // Optimizes TF graph via cleanups, merges, rewrites, constant folding, + // and edge case handling where possible. + pm.addNestedPass(TF::CreateDropWhileShapeInvariantPass()); + pm.addNestedPass( + tf_executor::CreateTFExecutorGraphPruningPass()); + pm.addNestedPass( + tf_executor::CreateTFExecutorIslandCoarseningPass()); + pm.addPass(TF::CreateTFFunctionalControlFlowToRegions()); + pm.addPass(mlir::createInlinerPass()); + pm.addPass(mlir::createSymbolDCEPass()); + pm.addPass(mlir::createCanonicalizerPass()); + pm.addPass(mlir::TF::CreateTFShapeInferencePass()); + pm.addPass(mlir::createCanonicalizerPass()); + pm.addPass(mlir::TF::CreateTensorListOpsDecompositionPass()); + pm.addNestedPass( + mlir::TFDevice::CreateDecomposeResourceOpsPass()); + + // FreezeVariables only freezes variables for TF v1 types. Separately handle + // freezing of TF v2 GlobalTensor ops. (Ref: b/206855389) + pm.addPass(mlir::tf_saved_model::CreateOptimizeGlobalTensorsPass()); + pm.addPass(mlir::tf_saved_model::CreateFreezeGlobalTensorsPass( + /*allow_mutable_tensors=*/true)); + + // Generic MLIR optimization passes. + pm.addPass(mlir::createCanonicalizerPass()); + pm.addPass(mlir::TF::CreateTFShapeInferencePass()); + + // Legalizes TF UniformQuantized types into MHLO. + pm.addNestedPass( + mlir::quant::stablehlo::CreateConvertTFQuantOpsToMHLOPass()); + pm.addPass(mlir::createCanonicalizerPass()); + + // TF -> StableHLO legalization. + AddLegalizeTFToStablehloPasses(pm, /*skip_quantization_ops=*/false, + skip_resize, + /*skip_partitioned_calls=*/false); + + // Wrap disallowed ops in stablehlo.custom_call ops. + if (smuggle_disallowed_ops) { + pm.addNestedPass(CreateSmuggleDisallowedOpsPass()); + pm.addPass(mlir::createCanonicalizerPass()); + } +} + +void AddMhloOptimizationPasses(OpPassManager& pm, + const bool add_fold_broadcast_pass) { + pm.addNestedPass(createStablehloUnfuseBatchNormPass()); + pm.addNestedPass(createStablehloFuseConvolutionPass()); + // StableHLO -> MHLO legalization. + pm.addPass(mhlo::createStablehloLegalizeToHloPass()); + // Rewrites some patterns for better performance. + pm.addNestedPass(createOptimizePass()); + // Conditionally enable below pass because this causes unfused convolutions + // described in b/293149194. This problem is not replicated in + // StableHLO Quantizer. + if (add_fold_broadcast_pass) { + pm.addNestedPass(createFoldBroadcastPass()); + } + + // Rewrites legacy StableHLO ops. + pm.addNestedPass(mhlo::createLegalizeEinsumToDotGeneralPass()); + pm.addNestedPass( + mhlo::createLegalizeTorchIndexSelectToGatherPass()); + + pm.addPass(mlir::createCanonicalizerPass()); +} + +void AddStablehloOptimizationPasses(OpPassManager& pm) { + // The current plan of record is to avoid doing optimization passes + // on StableHLO, treating StableHLO purely as an input format, and do all + // optimizations via MHLO passes that can be shared with the OpenXLA compiler. + // Therefore, this function inserts a StableHLO <=> MHLO roundtrip to make + // this happen. + + AddMhloOptimizationPasses(pm, /*enable_stablehlo_quantizer=*/false); + // TODO: b/293149194 - Add `createFoldBroadcastPass` back to + // `AddMhloOptimizationPasses` + pm.addNestedPass(createFoldBroadcastPass()); + + // MHLO -> StableHLO legalization. + pm.addPass(mhlo::createHloLegalizeToStablehloPass()); +} + +} // namespace odml +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/transforms.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/transforms.h new file mode 100644 index 0000000000000000000000000000000000000000..6680071e0fc47d31107d3ad5d07bc59218f8f45f --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/transforms.h @@ -0,0 +1,44 @@ +/* Copyright 2021 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. +==============================================================================*/ + +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_TRANSFORMS_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_TRANSFORMS_H_ + +#include "mlir/Pass/PassManager.h" // from @llvm-project + +namespace mlir { +namespace odml { + +// Adds all the necessary passes to lower a TF module to StableHLO. +// `skip_resize` enables or disables skipping conversion of tf.ResizeBilinear +// and tf.ResizeNearestNeighbor ops. +// `smuggle_disallowed_ops` enables or disables converting disallowed ops +// like tf.ResizeBilinear or tf.ResizeNearestNeighbor to mhlo.custom_call ops. +void AddTFToStablehloPasses(OpPassManager& pm, bool skip_resize, + bool smuggle_disallowed_ops); + +// This function is a common entry point for all graph optimizations that are +// not specific to any hardware. It legalizes SHLO->MHLO, does MHLO->MHLO +// optimizations by calling `AddMhloOptimizationPasses` internally, and +// legalizes MHLO->SHLO +void AddStablehloOptimizationPasses(OpPassManager& pm); + +// Adds all the backend-agonstic stableHLO optimization passes +void AddMhloOptimizationPasses(OpPassManager& pm, bool add_fold_broadcast_pass); + +} // namespace odml +} // namespace mlir + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STABLEHLO_TRANSFORMS_TRANSFORMS_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/unfold_splat_constant_pass.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/unfold_splat_constant_pass.cc new file mode 100644 index 0000000000000000000000000000000000000000..81b60fc507b58a855faf961f9c6de507735d6109 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/unfold_splat_constant_pass.cc @@ -0,0 +1,102 @@ +/* Copyright 2023 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 "llvm/ADT/SmallVector.h" +#include "mlir/Dialect/Quant/IR/QuantTypes.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/Builders.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/Operation.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/IR/Visitors.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" + +namespace mlir { +namespace odml { +namespace { + +#define DEBUG_TYPE "unfold-splat-constant-pass" + +#define GEN_PASS_DEF_UNFOLDSPLATCONSTANTPASS +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h.inc" + +// Undo the MHLO::BroadcastInDimOp folding pattern on splat tensor. +// TODO(b/295966255): Remove this pass after moving MHLO folders to a separate +// pass and folders are not applied by default. +class UnfoldSplatConstantPass + : public impl::UnfoldSplatConstantPassBase { + public: + void runOnOperation() override { + auto module = getOperation(); + + mlir::OpBuilder op_builder(&module.getBodyRegion()); + // Cannot use the pattern rewriter because the driver applies folders by + // default. + module.walk([&](mhlo::ConstantOp const_op) { + UnfoldSplatConstant(&op_builder, const_op); + }); + } + + private: + void UnfoldSplatConstant(mlir::OpBuilder* op_builder, + mhlo::ConstantOp const_op) const { + auto splat_elements_attr = + mlir::dyn_cast(const_op.getValue()); + if (!splat_elements_attr) { + return; + } + if (splat_elements_attr.getNumElements() == 1) { + return; + } + auto element_type = splat_elements_attr.getType().getElementType(); + if (mlir::isa(element_type) || + mlir::isa(element_type)) { + return; + } + op_builder->setInsertionPoint(const_op); + Value scalar = mhlo::ConstantOp::create( + *op_builder, const_op->getLoc(), + DenseElementsAttr::get( + RankedTensorType::get(/*shape=*/{}, element_type), + splat_elements_attr.getSplatValue())); + auto broadcast_dims = DenseIntElementsAttr::get( + RankedTensorType::get(/*shape=*/{0}, op_builder->getI64Type()), + llvm::SmallVector{}); + mhlo::BroadcastInDimOp broadcast_in_dim_op = mhlo::BroadcastInDimOp::create( + *op_builder, const_op->getLoc(), splat_elements_attr.getType(), scalar, + broadcast_dims); + const_op->replaceAllUsesWith(broadcast_in_dim_op); + const_op->erase(); + } +}; + +} // namespace + +std::unique_ptr> CreateUnfoldSplatConstantPass() { + return std::make_unique(); +} + +static PassRegistration pass; + +} // namespace odml +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/uniform_quantized_stablehlo_to_tfl_pass.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/uniform_quantized_stablehlo_to_tfl_pass.cc new file mode 100644 index 0000000000000000000000000000000000000000..a4ea6a030e9c115b0bd98bdf57a10ecf04ac685c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stablehlo/transforms/uniform_quantized_stablehlo_to_tfl_pass.cc @@ -0,0 +1,2353 @@ +/* Copyright 2023 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 +#include + +#include "absl/algorithm/container.h" +#include "absl/log/check.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/Debug.h" +#include "mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project +#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project +#include "mlir/Dialect/Quant/IR/Quant.h" // from @llvm-project // NOLINT: Required to register quantization dialect. +#include "mlir/Dialect/Quant/IR/QuantTypes.h" // from @llvm-project +#include "mlir/IR/Attributes.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project +#include "mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/IR/Location.h" // from @llvm-project +#include "mlir/IR/MLIRContext.h" // from @llvm-project +#include "mlir/IR/PatternMatch.h" // from @llvm-project +#include "mlir/IR/Types.h" // from @llvm-project +#include "mlir/IR/Value.h" // from @llvm-project +#include "mlir/Pass/Pass.h" // from @llvm-project +#include "mlir/Pass/PassRegistry.h" // from @llvm-project +#include "mlir/Support/LLVM.h" // from @llvm-project +#include "mlir/Support/LogicalResult.h" // from @llvm-project +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" // from @llvm-project +#include "stablehlo/dialect/StablehloOps.h" // from @stablehlo +#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" +#include "tensorflow/compiler/mlir/quantization/common/attrs_and_constraints.h" +#include "tensorflow/compiler/mlir/quantization/common/uniform_quantized_types.h" + +#define DEBUG_TYPE "uniform-quantized-stablehlo-to-tfl" + +namespace mlir { +namespace odml { +namespace { + +using ::mlir::quant::CastI64ArrayToI32; +using ::mlir::quant::CastI64ToI32; +using ::mlir::quant::CreateI32F32UniformQuantizedPerAxisType; +using ::mlir::quant::CreateI32F32UniformQuantizedType; +using ::mlir::quant::CreateI8F32UniformQuantizedPerAxisType; +using ::mlir::quant::CreateI8F32UniformQuantizedType; +using ::mlir::quant::FindOperandOfType; +using ::mlir::quant::FindUserOfType; +using ::mlir::quant::GetElementType; +using ::mlir::quant::IsDotGeneralFullyConnected; +using ::mlir::quant::IsI32F32UniformQuantizedPerAxisType; +using ::mlir::quant::IsI32F32UniformQuantizedType; +using ::mlir::quant::IsI8F32UniformQuantizedPerAxisType; +using ::mlir::quant::IsI8F32UniformQuantizedType; +using ::mlir::quant::IsOpFullyQuantized; +using ::mlir::quant::IsQuantizedTensorType; +using ::mlir::quant::IsSupportedByTfliteQuantizeOrDequantizeOps; +using ::mlir::quant::QuantizedType; +using ::mlir::quant::UniformQuantizedPerAxisType; +using ::mlir::quant::UniformQuantizedType; + +const char* kPaddingSame = "SAME"; +const char* kPaddingValid = "VALID"; + +#define GEN_PASS_DEF_UNIFORMQUANTIZEDSTABLEHLOTOTFLPASS +#include "tensorflow/compiler/mlir/lite/stablehlo/transforms/stablehlo_passes.h.inc" + +class UniformQuantizedStableHloToTflPass + : public impl::UniformQuantizedStableHloToTflPassBase< + UniformQuantizedStableHloToTflPass> { + private: + void runOnOperation() override; +}; + +// TODO: b/323645515 - Refactor reference functions. +// Bias scales for matmul-like ops should be input scale * filter scale. Here it +// is assumed that the input is per-tensor quantized and filter is per-channel +// quantized. +SmallVector GetBiasScales(const double input_scale, + const ArrayRef filter_scales) { + SmallVector bias_scales; + absl::c_transform(filter_scales, std::back_inserter(bias_scales), + [input_scale](const double filter_scale) -> double { + return filter_scale * input_scale; + }); + return bias_scales; +} + +// Returns a bias scale for matmul-like ops. Here it is assumed that both input +// and filter are per-tensor quantized. +double GetBiasScale(const double input_scale, const double filter_scale) { + return filter_scale * input_scale; +} + +// Returns the optionally broadcasted bias constant op used for a given op. +// If no such constant op exists, returns a nullptr. +Operation* GetBiasConstOp(Operation* op) { + Operation* bias_const_op; + if (Operation* broadcast_in_dim_op = + FindOperandOfType(op); + broadcast_in_dim_op != nullptr) { + bias_const_op = broadcast_in_dim_op->getOperand(0).getDefiningOp(); + } else { + bias_const_op = FindOperandOfType(op); + } + return isa(bias_const_op) ? bias_const_op : nullptr; +} + +// Creates a new `tfl.qconst` op for the quantized filter. Transposes the +// filter value from [i, o] -> [o, i]. This is because we assume `[i, o]` +// format for `stablehlo.dot_general` (i.e. contracting dimension == 1) +// whereas `tfl.fully_connected` accepts an OI format. +TFL::QConstOp CreateTransposedTflConstOpForFilter( + stablehlo::ConstantOp filter_constant_op, PatternRewriter& rewriter, + bool is_per_channel) { + const auto filter_values = + llvm::cast(filter_constant_op.getValue()) + .getValues(); + + ArrayRef filter_shape = + llvm::cast(filter_constant_op.getType()).getShape(); + + // Reverse the shapes. This makes sense, assuming that the filter tensor has a + // rank of 2 (no batch dimension). + SmallVector new_filter_shape(filter_shape.rbegin(), + filter_shape.rend()); + + // Construct the value array of transposed filter. Assumes 2D matrix. + SmallVector new_filter_values(filter_values.size(), /*Value=*/0); + for (int i = 0; i < filter_shape[0]; ++i) { + for (int j = 0; j < filter_shape[1]; ++j) { + const int old_idx = i * filter_shape[1] + j; + const int new_idx = j * filter_shape[0] + i; + new_filter_values[new_idx] = filter_values[old_idx]; + } + } + + auto new_filter_value_attr_type = RankedTensorType::getChecked( + filter_constant_op.getLoc(), new_filter_shape, + /*elementType=*/rewriter.getI8Type()); + + Type new_filter_quantized_type; + + if (is_per_channel) { + auto filter_quantized_type = llvm::cast( + GetElementType(filter_constant_op.getResult())); + new_filter_quantized_type = CreateI8F32UniformQuantizedPerAxisType( + filter_constant_op->getLoc(), *rewriter.getContext(), + filter_quantized_type.getScales(), + filter_quantized_type.getZeroPoints(), + /*quantization_dimension=*/0, /*narrow_range=*/true); + } else { + auto filter_quantized_type = llvm::cast( + GetElementType(filter_constant_op.getResult())); + new_filter_quantized_type = CreateI8F32UniformQuantizedType( + filter_constant_op->getLoc(), *rewriter.getContext(), + filter_quantized_type.getScale(), filter_quantized_type.getZeroPoint(), + /*narrow_range=*/true); + } + + // Required because the quantized dimension is changed from 3 -> 0. + auto new_filter_result_type = RankedTensorType::getChecked( + filter_constant_op.getLoc(), /*shape=*/new_filter_shape, + /*type=*/new_filter_quantized_type); + + auto new_filter_constant_value_attr = + DenseIntElementsAttr::get(new_filter_value_attr_type, new_filter_values); + return TFL::QConstOp::create(rewriter, filter_constant_op.getLoc(), + /*output=*/TypeAttr::get(new_filter_result_type), + /*value=*/new_filter_constant_value_attr); +} + +// Creates the `tfl.qconst` for filter. If `rhs_op` is a `stablehlo.constant`, +// this transposes the filter value from [i, o] -> [o, i]. This is because we +// assume `[i, o]` format for `stablehlo.dot_general` (i.e. contracting +// dimension == 1) whereas `tfl.fully_connected` accepts an `[o, i]` format. +// If there is already a [i, o] -> [o, i] `stablehlo.transpose` in between the +// constant and `rhs_op`, simply create an equivalent `tfl.qconst` from the +// `stablehlo.constant` because it already suffices the desired format. +// +// It should be guaranteed that `rhs_op` is either: +// 1. `stablehlo.constant` +// 2. `stablehlo.constant`->`stablehlo.transpose`. +// +// TODO: b/328156969 - Support the case where the RHS doesn't come from a +// constant. +TFL::QConstOp CreateTflConstOpForFilter(Operation* rhs_op, + PatternRewriter& rewriter, + const bool is_per_channel) { + if (auto filter_constant_op = dyn_cast_or_null(rhs_op); + filter_constant_op != nullptr) { + return CreateTransposedTflConstOpForFilter(filter_constant_op, rewriter, + is_per_channel); + } else { + auto transpose_op = cast(rhs_op); + auto constant_op = + cast(transpose_op.getOperand().getDefiningOp()); + + return TFL::QConstOp::create( + rewriter, constant_op.getLoc(), + /*output=*/TypeAttr::get(constant_op.getResult().getType()), + /*value=*/constant_op.getValue()); + } +} + +// Creates a new `tfl.qconst` op for the bias. The bias values are 0s, because +// this bias a dummy bias (note that bias fusion is not considered for this +// transformation). The quantization scale for the bias is input scale * +// filter scale. `filter_const_op` is used to retrieve the filter scales and +// the size of the bias constant. +TFL::QConstOp CreateTflConstOpForDummyBias( + const Location loc, const double input_scale, TFL::QConstOp filter_const_op, + PatternRewriter& rewriter, const bool is_per_channel, MLIRContext& ctx) { + const ArrayRef filter_shape = + filter_const_op.getResult().getType().getShape(); + + Type bias_quantized_type; + if (is_per_channel) { + const auto filter_quantized_element_type = + llvm::cast( + GetElementType(filter_const_op.getResult())); + + // The storage type is i32 for bias, which is the precision used for + // accumulation. + bias_quantized_type = CreateI32F32UniformQuantizedPerAxisType( + loc, ctx, + GetBiasScales(input_scale, filter_quantized_element_type.getScales()), + filter_quantized_element_type.getZeroPoints(), + /*quantization_dimension=*/0); + } else { + const auto filter_quantized_element_type = + llvm::cast( + GetElementType(filter_const_op.getResult())); + + // The storage type is i32 for bias, which is the precision used for + // accumulation. + bias_quantized_type = CreateI32F32UniformQuantizedType( + loc, ctx, + GetBiasScale(input_scale, filter_quantized_element_type.getScale()), + filter_quantized_element_type.getZeroPoint()); + } + + SmallVector bias_shape = {filter_shape[0]}; + auto bias_type = + RankedTensorType::getChecked(loc, bias_shape, bias_quantized_type); + + auto bias_value_type = RankedTensorType::getChecked( + loc, std::move(bias_shape), rewriter.getI32Type()); + auto bias_value = DenseIntElementsAttr::get( + bias_value_type, APInt(/*numBits=*/32, /*value=*/0, /*isSigned=*/true)); + + return TFL::QConstOp::create( + rewriter, loc, /*output=*/TypeAttr::get(bias_type), /*value=*/bias_value); +} + +// Casts the given op shapes from i64 to i32 to fit TFLite spec requirement. +arith::ConstantOp CreateI32ShapeConstantOp(const TensorType op_type, + const Location loc, + PatternRewriter& rewriter) { + const SmallVector shape_i32 = + CastI64ArrayToI32(op_type.getShape()).value(); + const TensorType shape_type = op_type.cloneWith( + ArrayRef(shape_i32.size()), rewriter.getI32Type()); + const auto shape_attr = DenseIntElementsAttr::get(shape_type, shape_i32); + return arith::ConstantOp::create(rewriter, loc, shape_attr); +} + +// Returns the desired qi8 per-tensor quantized output type for a given gemm op. +Type GetQuantizedOutputType(Operation* op, PatternRewriter& rewriter, + const bool has_i32_output, + const bool fuse_bias_constant) { + Operation* uniform_quantize_op; + if (!has_i32_output) return op->getResult(0).getType(); + if (fuse_bias_constant) { + Operation* add_op = FindUserOfType(op); + uniform_quantize_op = FindUserOfType(add_op); + } else { + uniform_quantize_op = FindUserOfType(op); + } + // StableHLO Quantizer outputs an i32 type. Rewrite to i8 type result + // to meet TFLite op requirement. + auto result_quantized_type = llvm::cast( + GetElementType(uniform_quantize_op->getResult(0))); + auto new_result_quantized_type = CreateI8F32UniformQuantizedType( + uniform_quantize_op->getLoc(), *rewriter.getContext(), + result_quantized_type.getScale(), result_quantized_type.getZeroPoint()); + // Omit any bias and requantize ops as `tfl.{gemm_op}` outputs a + // fused `qi8` type. + rewriter.replaceAllUsesWith(uniform_quantize_op->getResult(0), + op->getResult(0)); + return llvm::cast(op->getResult(0).getType()) + .clone(new_result_quantized_type); +} + +// Matches kernel dimension numbers, ranks of input and output and constant +// kernel for legalization to TFLite convolution ops. +LogicalResult MatchConvolutionFormat(stablehlo::ConvolutionOp op) { + stablehlo::ConvDimensionNumbersAttr dimension_numbers = + op.getDimensionNumbers(); + const int64_t kernel_input_feature_dim = + dimension_numbers.getKernelInputFeatureDimension(); + if (kernel_input_feature_dim != 2) { + LLVM_DEBUG(llvm::dbgs() << "Expected kernel input feature == 2. Got: " + << kernel_input_feature_dim << ".\n"); + return failure(); + } + + const int64_t kernel_output_feature_dim = + dimension_numbers.getKernelOutputFeatureDimension(); + if (kernel_output_feature_dim != 3) { + LLVM_DEBUG(llvm::dbgs() << "Expected kernel output feature == 3. Got: " + << kernel_output_feature_dim << ".\n"); + return failure(); + } + + const auto input_type = llvm::cast(op.getLhs().getType()); + if (input_type.getRank() != 4) { + LLVM_DEBUG(llvm::dbgs() << "Only 2D convolution op is supported. " + "Expected input rank of 4. Got: " + << input_type.getRank() << ".\n"); + return failure(); + } + + const auto filter_type = llvm::cast(op.getRhs().getType()); + if (filter_type.getRank() != 4) { + LLVM_DEBUG(llvm::dbgs() << "Only 2D convolution op is supported. " + "Expected filter rank of 4. Got: " + << filter_type.getRank() << ".\n"); + return failure(); + } + + if (Operation* filter_op = op.getRhs().getDefiningOp(); + filter_op == nullptr || !isa(filter_op)) { + LLVM_DEBUG(llvm::dbgs() << "Filter should be a constant.\n"); + return failure(); + } + + return success(); +} + +// Transposes the convolution filter tensor of format [0, 1, i, o] to match the +// filter tensor format for TFLite convolution. The following transformations +// are supported: +// +// Depthwise case (`feature_group_count` > 1) +// * Permutates given filter to `[i, 0, 1, o]` format. +// General convolution (`feature_group_count` = 1) +// * Permutates given filter to `[o, 0, 1, i]` format. +// Using TransposeOp doesn't work because the quantized dimension +// changes which violates the constraint for the TransposeOp that the +// input's and output's element type should be the same. +DenseIntElementsAttr TransposeFilterInConvolution( + Location loc, PatternRewriter& rewriter, + const DenseIntElementsAttr& filter_value_attr, const bool is_depthwise) { + ArrayRef filter_shape = filter_value_attr.getShapedType().getShape(); + SmallVector filter_constant_values{ + filter_value_attr.getValues()}; + SmallVector new_filter_constant_values(filter_constant_values.size(), + 0); + SmallVector transpose_dims; + if (is_depthwise) { + transpose_dims = {2, 0, 1, 3}; + } else { + transpose_dims = {3, 0, 1, 2}; + } + + SmallVector new_filter_shape; + new_filter_shape.reserve(filter_shape.size()); + for (int i = 0; i < filter_shape.size(); ++i) { + new_filter_shape.push_back(filter_shape[transpose_dims[i]]); + } + + auto get_array_idx = [](ArrayRef shape, const int i, const int j, + const int k, const int l) -> int64_t { + return (i * shape[1] * shape[2] * shape[3]) + (j * shape[2] * shape[3]) + + (k * shape[3]) + l; + }; + + // Transpose the filter value. + // TODO: b/336203735 - Use `DenseElementsTransposer` instead of manual + // transpose. + for (int i = 0; i < filter_shape[0]; ++i) { + for (int j = 0; j < filter_shape[1]; ++j) { + for (int k = 0; k < filter_shape[2]; ++k) { + for (int l = 0; l < filter_shape[3]; ++l) { + // [o, 0, 1, i] for `tfl.conv_2d` case`, + // [i, 0, 1, o] for `tfl.depthwise_conv_2d` case. + int old_idx = get_array_idx(filter_shape, i, j, k, l); + int new_idx = is_depthwise + ? get_array_idx(new_filter_shape, k, i, j, l) + : get_array_idx(new_filter_shape, l, i, j, k); + new_filter_constant_values[new_idx] = filter_constant_values[old_idx]; + } + } + } + } + + // Create the new filter constant. + auto new_filter_value_attr_type = + RankedTensorType::getChecked(loc, new_filter_shape, + /*elementType=*/rewriter.getI8Type()); + auto new_filter_constant_value_attr = DenseIntElementsAttr::get( + new_filter_value_attr_type, new_filter_constant_values); + + return new_filter_constant_value_attr; +} + +// Checks if the given convolution op is depthwise. +bool IsDepthwiseConvolution(stablehlo::ConvolutionOp op) { + // `feature_group_count` controls how the input channel dimension is + // split. + // A value bigger than one signals depthwise convolution behavior. + return op.getFeatureGroupCount() > 1; +} + +// Returns kernel output feature dimension of TFLite convolutions. +int64_t GetConvolutionKernelOutputFeatureDimension(bool is_depthwise) { + return is_depthwise ? 3 : 0; +} + +// Returns kernel input feature dimension of TFLite convolutions. +int64_t GetConvolutionKernelInputFeatureDimension(bool is_depthwise) { + return is_depthwise ? 0 : 3; +} + +// stablehlo.uniform_quantize -> tfl.quantize +// TODO: b/322428814 - Add StableHLO quantizer integration tests for ODML. +class RewriteUniformQuantizeOp + : public OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + + // Determines whether the input and output types are compatible with + // `tfl.quantize`. See the definition for the `QUANTIZE` kernel for the + // detailed limitations + // (https://github.com/tensorflow/tensorflow/blob/8f145d579aa0ee7f4187af32dbbf4e12fdabbffe/tensorflow/lite/kernels/quantize.cc#L105). + LogicalResult matchAndRewrite(stablehlo::UniformQuantizeOp op, + PatternRewriter& rewriter) const override { + const Type input_element_type = GetElementType(op.getOperand()); + if (!(llvm::isa(input_element_type) || + IsI32F32UniformQuantizedType(input_element_type) || + IsI32F32UniformQuantizedPerAxisType(input_element_type))) { + LLVM_DEBUG(llvm::dbgs() << "Uniform quantize op's input should be a " + "float type or int32. Got: " + << input_element_type << ".\n"); + return failure(); + } + + // Output type of `UniformQuantizeOp` is guaranteed to be a quantized + // tensor with integer storage type. + const auto output_storage_type = llvm::cast( + llvm::cast(GetElementType(op.getResult())) + .getStorageType()); + if (!IsSupportedByTfliteQuantizeOrDequantizeOps(output_storage_type)) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match storage type of output quantized type.\n"); + return failure(); + } + + Type output_type = *op->getResultTypes().begin(); + rewriter.replaceOpWithNewOp( + op, output_type, /*input=*/op.getOperand(), + /*qtype=*/TypeAttr::get(output_type)); + return success(); + } +}; + +// stablehlo.uniform_dequantize -> tfl.dequantize +class RewriteUniformDequantizeOp + : public OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + + // Determines whether the input and output types are compatible with + // `tfl.dequantize`. See the definition for the `DEQUANTIZE` kernel for the + // detailed limitations + // (https://github.com/tensorflow/tensorflow/blob/8f145d579aa0ee7f4187af32dbbf4e12fdabbffe/tensorflow/lite/kernels/dequantize.cc#L52). + LogicalResult matchAndRewrite(stablehlo::UniformDequantizeOp op, + PatternRewriter& rewriter) const override { + const auto input_storage_type = llvm::cast( + llvm::cast(GetElementType(op.getOperand())) + .getStorageType()); + if (!IsSupportedByTfliteQuantizeOrDequantizeOps(input_storage_type)) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match storage type of input quantized type.\n"); + return failure(); + } + + // Output type is guaranteed to be a float tensor for a valid StableHLO. + const auto output_element_type = + llvm::cast(GetElementType(op.getResult())); + if (!llvm::isa(output_element_type)) { + LLVM_DEBUG(llvm::dbgs() << "Uniform dequantize op's output element type " + "should be f32. Got: " + << output_element_type << ".\n"); + return failure(); + } + + rewriter.replaceOpWithNewOp( + op, /*resultTypes=*/op->getResultTypes(), /*input=*/op.getOperand()); + return success(); + } +}; + +// Rewrites `stablehlo.dot_general` to `tfl.fully_connected` or +// `tfl.batch_matmul` when it accepts uniform quantized tensors. +// +// StableHLO Quantizer output: +// * input: per-tensor qi8 +// * filter: per-channel qi8 for non-batching op, per-tensor for batching op. +// * output: per-tensor qi32 +// JAX Quantizer output: +// * input: per-tensor qi8 +// * filter: per-channel qi8 +// * output: per-tensor qi8 +// +// Conditions for the `tfl.batch_matmul` conversion: +// * size(batching_dimensions) <= 3 (TFLite support restriction) +// * size(contracting_dimensions) = 1 +// * Input tensors are per-tensor uniform quantized (i8->f32) +// tensors (full integer) with shape [..., r_x, c_x] or [..., c_x, r_x]. +// * The filter tensor is a per-tensor uniform quantized (i8->f32) tensor +// (constant or activation) with shape [..., r_y, c_y] or [..., c_y, r_y]. +// * Output tensors are per-tensor uniform quantized (i8->f32) or +// per-channel uniform quantized (i32->f32) tensors. +// +// Conditions for `tfl.fully_connected` conversion: +// * Input tensors are per-tensor uniform quantized (i8->f32) +// tensors. +// * The filter tensor is constant a per-tensor uniform quantized (i8->f32) +// tensor. The quantization dimension should be 1 (the non-contracting +// dimension). +// * Output tensors are per-tensor uniform quantized (i8->f32) or +// per-channel uniform quantized (i32->f32) tensors. +// * The input tensor's rank is either 2 or 3. The last dimension of the input +// tensor should be the contracting dimension, i.e. [..., c_x, r_x]. +// * The filter tensor's rank is 2. The contracting dimension should be the +// first dimension (dim 0), i.e. [c_y, r_y] where c_y == r_x. +class RewriteQuantizedDotGeneralOpToTflFullyConnectedOrBatchMatmulOp + : public OpRewritePattern { + public: + // Sets benefit to 10 to make this pattern more preferred than smaller local + // transformations like `stablehlo.transpose`->`tfl.transpose`, as this + // pattern involves `stablehlo.transpose` in some cases. + explicit RewriteQuantizedDotGeneralOpToTflFullyConnectedOrBatchMatmulOp( + MLIRContext* ctx) + : OpRewritePattern(ctx, /*benefit=*/10) {} + + LogicalResult matchAndRewrite(stablehlo::DotGeneralOp op, + PatternRewriter& rewriter) const override { + if (match(op).failed()) { + return failure(); + } + rewrite(op, rewriter); + return success(); + } + + private: + LogicalResult match(stablehlo::DotGeneralOp op) const { + const stablehlo::DotDimensionNumbersAttr dot_dimension_nums = + op.getDotDimensionNumbers(); + const bool is_batch_matmul = !IsDotGeneralFullyConnected(op).value(); + const Type elem_type = GetElementType(op.getResult()); + const bool has_i32_output = IsI32F32UniformQuantizedType(elem_type) || + IsI32F32UniformQuantizedPerAxisType(elem_type); + + if (failed(MatchInputDotGeneralCommonPattern(op.getLhs()))) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match input for quantized dot_general.\n"); + return failure(); + } + if (failed(MatchFilterCommonPattern(op.getRhs()))) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match filter for quantized dot_general.\n"); + return failure(); + } + if (failed(MatchOutput(op.getResult(), has_i32_output, is_batch_matmul))) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match output for quantized dot_general.\n"); + return failure(); + } + + if (is_batch_matmul) { + return MatchDotGeneralToTflBatchMatmulOp(op, dot_dimension_nums, + has_i32_output); + } + return MatchDotGeneralToTflFullyConnectedOp(op, dot_dimension_nums, + has_i32_output); + } + + void rewrite(stablehlo::DotGeneralOp op, PatternRewriter& rewriter) const { + const Type output_type = GetElementType(op.getResult()); + const bool has_i32_output = + IsI32F32UniformQuantizedType(output_type) || + IsI32F32UniformQuantizedPerAxisType(output_type); + const stablehlo::DotDimensionNumbersAttr dot_dimension_nums = + op.getDotDimensionNumbers(); + const bool is_batch_matmul = !IsDotGeneralFullyConnected(op).value(); + + if (is_batch_matmul) { + RewriteDotGeneralToTflBatchMatmulOp(op, rewriter, dot_dimension_nums, + has_i32_output); + } else { + RewriteDotGeneralToTflFullyConnectedOp(op, rewriter, dot_dimension_nums, + has_i32_output); + } + } + + static LogicalResult MatchDotGeneralToTflBatchMatmulOp( + stablehlo::DotGeneralOp op, + const stablehlo::DotDimensionNumbersAttr dot_dimension_nums, + const bool has_i32_output) { + if (has_i32_output && !HasOneUseByQuantizeOp(op)) { + LLVM_DEBUG(llvm::dbgs() + << "When output type of dot_general is qi32, it should have " + "only one use of requantization.\n"); + return failure(); + } + + const int num_lhs_batching_dims = + dot_dimension_nums.getLhsBatchingDimensions().size(); + const int num_lhs_contracting_dims = + dot_dimension_nums.getLhsContractingDimensions().size(); + if (num_lhs_batching_dims > 3) { + LLVM_DEBUG(llvm::dbgs() << "Failed to match batching dimension for " + "quantized dot_general.\n"); + return failure(); + } + // Checking one side is enough since + // (C1) size(lhs_batching_dimensions) = size(rhs_batching_dimensions). + if (num_lhs_contracting_dims != 1) { + // Check one side is enough since + // (C2) size(lhs_contracting_dimensions) = + // size(rhs_contracting_dimensions). + LLVM_DEBUG(llvm::dbgs() << "Failed to match contract dimension for " + "quantized dot_general.\n"); + return failure(); + } + const auto input_type = llvm::cast(op.getLhs().getType()); + const int input_rank = input_type.getRank(); + const auto input_contracting_dim = + dot_dimension_nums.getLhsContractingDimensions()[0]; + if ((input_contracting_dim != input_rank - 1) && + (input_contracting_dim != input_rank - 2)) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match input contracting dimensions.\n"); + return failure(); + } + + const auto filter_type = llvm::cast(op.getRhs().getType()); + const Type filter_element_type = filter_type.getElementType(); + if (!IsI8F32UniformQuantizedType(filter_element_type)) { + LLVM_DEBUG(llvm::dbgs() + << "Expected a per-tensor uniform " + "quantized (i8->f32) weight for dot_general. Got: " + << filter_type << "\n"); + return failure(); + } + const int rhs_rank = llvm::cast(filter_type).getRank(); + const auto rhs_contracting_dim = + dot_dimension_nums.getRhsContractingDimensions()[0]; + if ((rhs_contracting_dim != rhs_rank - 1) && + (rhs_contracting_dim != rhs_rank - 2)) { + LLVM_DEBUG(llvm::dbgs() + << "Not supported rhs contracting dim for dot_general.\n"); + return failure(); + } + return success(); + } + + static LogicalResult MatchDotGeneralToTflFullyConnectedOp( + stablehlo::DotGeneralOp op, + const stablehlo::DotDimensionNumbersAttr dot_dimension_nums, + const bool has_i32_output) { + const int num_lhs_contracting_dims = + dot_dimension_nums.getLhsContractingDimensions().size(); + const int num_rhs_contracting_dims = + dot_dimension_nums.getRhsContractingDimensions().size(); + if (num_lhs_contracting_dims != 1 || num_rhs_contracting_dims != 1) { + LLVM_DEBUG(llvm::dbgs() + << "Expected number of contracting dimensions to be 1. Got: " + << num_rhs_contracting_dims << ".\n"); + return failure(); + } + + const auto input_type = llvm::cast(op.getLhs().getType()); + if (!(input_type.getRank() == 2 || input_type.getRank() == 3)) { + LLVM_DEBUG(llvm::dbgs() << "Input expected to have rank of 2 or 3. Got: " + << input_type << ".\n"); + return failure(); + } + + const Value filter = op.getRhs(); + const auto filter_type = llvm::cast(filter.getType()); + if (filter_type.getRank() != 2) { + LLVM_DEBUG(llvm::dbgs() + << "Filter tensor expected to have a tensor rank of 2. Got: " + << filter_type << ".\n"); + return failure(); + } + + if (!IsI8F32UniformQuantizedPerAxisType(filter_type.getElementType())) { + LLVM_DEBUG(llvm::dbgs() << "Expected a per-channel uniform quantized " + "(i8->f32) type filter. Got: " + << filter_type.getElementType() << "\n"); + return failure(); + } + + // If the op has a fusible bias, make sure the bias is a constant. + if (auto add_op = FindUserOfType(op); + add_op != nullptr && + !isa(add_op->getOperand(1).getDefiningOp())) { + LLVM_DEBUG(llvm::dbgs() << "Expected a `stablehlo.constant` as the " + << "rhs of `stablehlo.add`.\n"); + } + + // Make sure the filter is a constant or a constant transpose. + Operation* filter_op = filter.getDefiningOp(); + const bool is_constant = isa_and_nonnull(filter_op); + const bool is_constant_transpose = + isa_and_nonnull(filter_op) && + isa_and_nonnull( + filter_op->getOperand(0).getDefiningOp()); + if (!is_constant && !is_constant_transpose) { + LLVM_DEBUG( + llvm::dbgs() + << "Expected a `stablehlo.constant` or " + "`stablehlo.constant`->`stablehlo.transpose` for the rhs.\n"); + return failure(); + } + + return success(); + } + + static LogicalResult MatchInputDotGeneralCommonPattern(const Value input) { + const auto input_type = llvm::cast(input.getType()); + if (const auto input_element_type = input_type.getElementType(); + !IsI8F32UniformQuantizedType(input_element_type)) { + LLVM_DEBUG(llvm::dbgs() + << "Expected an i8->f32 uniform quantized type. Got: " + << input_element_type << ".\n"); + return failure(); + } + + if (!input_type.hasRank()) { + LLVM_DEBUG(llvm::dbgs() << "Expected input_type to have rank.\n"); + return failure(); + } + return success(); + } + + static LogicalResult MatchFilterCommonPattern(const Value filter) { + auto filter_type = llvm::cast(filter.getType()); + if (!filter_type.hasRank()) { + LLVM_DEBUG(llvm::dbgs() << "Expected rhs of dot_general has rank. Got: " + << filter.getType() << "\n"); + return failure(); + } + + return success(); + } + + static LogicalResult MatchOutput(const Value output, + const bool has_i32_output, + const bool is_batch_matmul) { + const Type output_element_type = GetElementType(output); + if (has_i32_output) { + if (is_batch_matmul && + !IsI32F32UniformQuantizedType(output_element_type)) { + LLVM_DEBUG(llvm::dbgs() << "Expected a per-tensor " + "uniform quantized (i32->f32) type " + "legalizable to `tfl.batch_matmul`. Got: " + << output_element_type << ".\n"); + return failure(); + } else if (!is_batch_matmul && + !IsI32F32UniformQuantizedPerAxisType(output_element_type)) { + LLVM_DEBUG(llvm::dbgs() << "Expected a per-channel " + "uniform quantized (i32->f32) type " + "legalizable to `tfl.fully_connected`. Got: " + << output_element_type << ".\n"); + return failure(); + } + return success(); + } + if (!IsI8F32UniformQuantizedType(output_element_type)) { + LLVM_DEBUG( + llvm::dbgs() + << "Expected a per-tensor uniform quantized (i8->f32) type. Got: " + << output_element_type << ".\n"); + return failure(); + } + return success(); + } + + static void RewriteDotGeneralToTflBatchMatmulOp( + stablehlo::DotGeneralOp op, PatternRewriter& rewriter, + const stablehlo::DotDimensionNumbersAttr dot_dimension_nums, + const bool has_i32_output) { + const auto rhs_contracting_dims = + dot_dimension_nums.getRhsContractingDimensions(); + const auto lhs_contracting_dims = + dot_dimension_nums.getLhsContractingDimensions(); + + const Value rhs_value = op.getRhs(); + const Value lhs_value = op.getLhs(); + + Operation* rhs_op = rhs_value.getDefiningOp(); + auto filter_constant_op = dyn_cast_or_null(rhs_op); + + // Set to `nullptr` because this attribute only matters when the input is + // dynamic-range quantized. + const BoolAttr asymmetric_quantize_inputs = nullptr; + + const int lhs_rank = llvm::cast(lhs_value.getType()).getRank(); + const BoolAttr adj_x = + (lhs_contracting_dims[0] == lhs_rank - 2 ? rewriter.getBoolAttr(true) + : rewriter.getBoolAttr(false)); + const int rhs_rank = llvm::cast(rhs_value.getType()).getRank(); + const BoolAttr adj_y = + (rhs_contracting_dims[0] == rhs_rank - 1 ? rewriter.getBoolAttr(true) + : rewriter.getBoolAttr(false)); + + Value result = op.getResult(); + Operation* result_user_op = *op->getUsers().begin(); + if (isa(result_user_op) || + isa(result_user_op)) { + result = result_user_op->getResult(0); + } + + // Create BMM assuming rhs is activation. + auto tfl_batchmatmul_op = TFL::BatchMatMulOp::create( + rewriter, op.getLoc(), /*output=*/result.getType(), + /*input=*/lhs_value, + /*filter=*/rhs_value, adj_x, adj_y, asymmetric_quantize_inputs); + + // Update BMM if rhs is a constant. + if (filter_constant_op != nullptr) { + const auto rhs_uniform_quantized_type = + llvm::cast(rhs_value.getType()); + const auto rhs_constant_value_attr = + cast(filter_constant_op.getValue()); + auto rhs_constant_op = TFL::QConstOp::create( + rewriter, rhs_op->getLoc(), + /*output=*/TypeAttr::get(rhs_uniform_quantized_type), + rhs_constant_value_attr); + tfl_batchmatmul_op = TFL::BatchMatMulOp::create( + rewriter, op.getLoc(), /*output=*/result.getType(), + /*input=*/lhs_value, /*filter=*/rhs_constant_op.getResult(), adj_x, + adj_y, asymmetric_quantize_inputs); + } + + rewriter.replaceAllUsesWith(result, tfl_batchmatmul_op.getResult()); + } + + static void RewriteDotGeneralToTflFullyConnectedOp( + stablehlo::DotGeneralOp op, PatternRewriter& rewriter, + const stablehlo::DotDimensionNumbersAttr dot_dimension_nums, + const bool has_i32_output) { + const Value rhs_value = op.getRhs(); + const Value lhs_value = op.getLhs(); + + // Set to `nullptr` because this attribute only matters when the input is + // dynamic-range quantized. + const BoolAttr asymmetric_quantize_inputs = nullptr; + + TFL::QConstOp filter_constant_op = CreateTflConstOpForFilter( + rhs_value.getDefiningOp(), rewriter, /*is_per_channel=*/true); + + const double input_scale = + llvm::cast(GetElementType(lhs_value)) + .getScale(); + TFL::QConstOp bias_tfl_op; + bool fuse_bias_constant = + FindUserOfType(op) && has_i32_output; + // Get the desired output type and extract any existing fusible bias + // as `TFL::QConstOp` so that it can be fused with TFL::FullyConnectedOp`. + const Type output_type = GetOutputTypeAndOptionallyUpdateBias( + op, rewriter, &bias_tfl_op, has_i32_output, fuse_bias_constant); + + // If there is no explicit bias, create a dummy value filled with zeroes. + if (!fuse_bias_constant) { + bias_tfl_op = CreateTflConstOpForDummyBias( + op.getLoc(), input_scale, filter_constant_op, rewriter, + /*is_per_channel=*/true, *op.getContext()); + } + rewriter.replaceOpWithNewOp( + op, /*output=*/output_type, + /*input=*/lhs_value, + /*filter=*/filter_constant_op.getResult(), + /*bias=*/bias_tfl_op.getResult(), + /*fused_activation_function=*/rewriter.getStringAttr("NONE"), + /*weights_format=*/rewriter.getStringAttr("DEFAULT"), + /*keep_num_dims=*/rewriter.getBoolAttr(false), + asymmetric_quantize_inputs); + } + + static Type GetOutputTypeAndOptionallyUpdateBias( + Operation* op, PatternRewriter& rewriter, TFL::QConstOp* bias_tfl_op, + const bool has_i32_output, const bool fuse_bias_constant) { + Type output_type; + if (has_i32_output) { + Operation* uniform_quantize_op; + if (fuse_bias_constant) { + Operation* add_op = FindUserOfType(op); + uniform_quantize_op = FindUserOfType(add_op); + const auto filter_quantized_type = + llvm::cast( + GetElementType(op->getOperand(1))); + const SmallVector bias_scales = GetBiasScales( + /*input_scale=*/llvm::cast( + GetElementType(op->getOperand(0))) + .getScale(), + /*filter_scales=*/filter_quantized_type.getScales()); + const ArrayRef output_shape = + llvm::cast(op->getResult(0).getType()).getShape(); + const SmallVector bias_shape = { + output_shape[output_shape.size() - 1]}; + // `tfl.fully_connected`'s `GetChannelDimIndex` is 0. + const auto bias_quantized_type = + CreateI32F32UniformQuantizedPerAxisType( + op->getLoc(), *op->getContext(), std::move(bias_scales), + llvm::cast( + GetElementType(op->getResult(0))) + .getZeroPoints(), + /*quantization_dimension=*/0); + Operation* bias_const_op = GetBiasConstOp(add_op); + if (bias_const_op != nullptr) { + const auto bias_type = RankedTensorType::getChecked( + op->getLoc(), bias_shape, bias_quantized_type); + const auto bias_value = cast( + cast(bias_const_op).getValue()); + + *bias_tfl_op = TFL::QConstOp::create( + rewriter, op->getLoc(), + /*output=*/TypeAttr::get(bias_type), /*value=*/bias_value); + } + } else { + uniform_quantize_op = FindUserOfType(op); + } + + const auto result_quantized_type = + llvm::cast( + GetElementType(uniform_quantize_op->getResult(0))); + const auto new_result_quantized_type = CreateI8F32UniformQuantizedType( + uniform_quantize_op->getLoc(), *rewriter.getContext(), + result_quantized_type.getScale(), + result_quantized_type.getZeroPoint()); + output_type = llvm::cast(op->getResult(0).getType()) + .clone(new_result_quantized_type); + // Omit any bias and requantize ops as `tfl.fully_connected` outputs a + // fused `qi8` type. + FindUserOfType<>(uniform_quantize_op)->setOperand(0, op->getResult(0)); + } else { + output_type = GetQuantizedOutputType(op, rewriter, has_i32_output, + fuse_bias_constant); + } + return output_type; + } + + static bool HasOneUseByQuantizeOp(Operation* op) { + return op->hasOneUse() && + (FindUserOfType(op) != nullptr || + FindUserOfType(op) != nullptr); + } +}; + +// Rewrites `stablehlo.convolution` into fused `tfl.conv_2d`. +// If available, fuse bias and activation adjacent to `stablehlo.convolution`. +// This RewritePattern rewrites both the following into `tfl.conv_2d` op: +// +// StableHLO Quantizer output: +// * input: per-tensor qi8 +// * filter: per-channel qi8 (`quantization_dimension` = 3) +// * output: per-channel qi32 (`quantization_dimension` = 3) +// JAX Quantizer output: +// * input: per-tensor qi8 +// * filter: per-channel qi8 (`quantization_dimension` = 3) +// * output: per-tensor qi8 +// +// Conditions for the conversion: +// * Input tensors are per-tensor uniform quantized (i8->f32) +// tensors. +// * The filter tensor is constant a per-channel uniform quantized (i8->f32) +// tensor. +// * Output tensors are per-tensor uniform quantized (i8->f32) or +// per-channel uniform quantized (i32->f32) tensors. +// * Convolution is a 2D convolution op and both the input's and filter's +// shape is 4 dimensional. +// * The filter tensor's format is `[0, 1, i, o]`. +// * Not a depthwise convolution. +class RewriteQuantizedConvolutionOp + : public OpRewritePattern { + public: + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(stablehlo::ConvolutionOp op, + PatternRewriter& rewriter) const override { + if (match(op).failed()) { + return failure(); + } + rewrite(op, rewriter); + return success(); + } + + private: + LogicalResult match(stablehlo::ConvolutionOp op) const { + const bool has_i32_output = + IsI32F32UniformQuantizedPerAxisType(GetElementType(op.getResult())); + const bool fuse_bias_constant = + FindUserOfType(op) && has_i32_output; + + if (failed(MatchInput(op.getOperand(0)))) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match input for quantized convolution_op.\n"); + return failure(); + } + + if (failed(MatchFilter(op.getOperand(1)))) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match filter for quantized convolution_op.\n"); + return failure(); + } + + if (failed(MatchOutput(op.getResult()))) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match output for quantized convolution_op.\n"); + return failure(); + } + + if (failed(MatchConvolutionFormat(op))) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match dimension format for convolution_op.\n"); + return failure(); + } + + if (fuse_bias_constant) { + Operation* add_op = FindUserOfType(op); + if (add_op == nullptr) { + LLVM_DEBUG(llvm::dbgs() << "Failed to find AddOp for bias fusion.\n"); + return failure(); + } + Operation* bias_const_op = GetBiasConstOp(add_op); + if (bias_const_op == nullptr) { + LLVM_DEBUG(llvm::dbgs() << "Failed to find bias constant.\n"); + return failure(); + } + } + + return success(); + } + + void rewrite(stablehlo::ConvolutionOp op, PatternRewriter& rewriter) const { + const bool has_i32_output = + IsI32F32UniformQuantizedPerAxisType(GetElementType(op.getResult())); + stablehlo::ConvDimensionNumbersAttr dimension_numbers = + op.getDimensionNumbers(); + + const bool is_depthwise = IsDepthwiseConvolution(op); + const bool is_transpose_conv = IsTransposeConv(op, dimension_numbers); + const bool fuse_bias_constant = + FindUserOfType(op) && has_i32_output; + TFL::QConstOp new_filter_constant_op; + TFL::QConstOp bias = GetBiasOpAndUpdateQuantizedFilterConstant( + /*op=*/op, /*new_filter_constant_op=*/new_filter_constant_op, + /*rewriter=*/rewriter, /*is_depthwise=*/is_depthwise, + /*has_i32_output=*/has_i32_output, + /*fuse_bias_constant=*/fuse_bias_constant); + + // Determine the attributes for the TFL::Conv2DOp. + Value input_value = op.getOperand(0); + if (const DenseIntElementsAttr padding_attr = op.getPaddingAttr(); + !HasProperPadding(op, dimension_numbers, padding_attr)) { + // Add an extra tfl.pad_op if there are explicit padding values. This + // extra pad op will allow us to always set the `padding` attribute of + // the newly created tfl.conv_2d op as "VALID". + TFL::PadOp pad_op = + CreateTflPadOp(op.getLoc(), padding_attr, input_value, rewriter); + input_value = pad_op.getResult(); + } + + const Type output_type = GetQuantizedOutputType( + op, rewriter, has_i32_output, fuse_bias_constant); + const auto [stride_h, stride_w] = GetStrides(op); + const auto [dilation_h_factor, dilation_w_factor] = GetDilationFactors(op); + if (is_depthwise) { + // The total number of depthwise convolution output channels will be + // equal to input channel * `depth_multiplier`. + const int64_t multiplier = dimension_numbers.getOutputFeatureDimension() / + dimension_numbers.getInputFeatureDimension(); + + rewriter.replaceOpWithNewOp( + // op result should be recasted to desired quantized type. + op, output_type, + /*input=*/input_value, + /*filter=*/new_filter_constant_op, /*bias=*/bias.getResult(), + /*dilation_h_factor=*/rewriter.getI32IntegerAttr(dilation_h_factor), + /*dilation_w_factor=*/rewriter.getI32IntegerAttr(dilation_w_factor), + /*fused_activation_function=*/rewriter.getStringAttr("NONE"), + /*padding=*/ + rewriter.getStringAttr(IsSamePadding(op, dimension_numbers) + ? kPaddingSame + : kPaddingValid), + /*stride_h=*/rewriter.getI32IntegerAttr(stride_h), + /*stride_w=*/rewriter.getI32IntegerAttr(stride_w), + /*depthwise_multiplier=*/rewriter.getI32IntegerAttr(multiplier)); + } else if (is_transpose_conv) { + // TODO: b/326332748 - For forward convolution in transpose_conv, + // IsSamePadding calculation may need to be updated. + // Reference: https://arxiv.org/pdf/1603.07285.pdf + // Section 4.6 > Relationship 13 states `stride_dim = dilation + 1`. + rewriter.replaceOpWithNewOp( + // op result should be recasted to desired quantized type. + op, output_type, /*output_shape=*/ + CreateI32ShapeConstantOp(op.getResult().getType(), op->getLoc(), + rewriter), + /*filter=*/new_filter_constant_op, /*input=*/input_value, + /*bias=*/bias.getResult(), + /*padding=*/ + rewriter.getStringAttr(IsSamePadding(op, dimension_numbers) + ? kPaddingSame + : kPaddingValid), + /*stride_h=*/rewriter.getI32IntegerAttr(dilation_h_factor + 1), + /*stride_w=*/rewriter.getI32IntegerAttr(dilation_w_factor + 1), + /*fused_activation_function=*/rewriter.getStringAttr("NONE")); + } else { + rewriter.replaceOpWithNewOp( + // op result should be recasted to desired quantized type. + op, output_type, + /*input=*/input_value, + /*filter=*/new_filter_constant_op, /*bias=*/bias.getResult(), + /*dilation_h_factor=*/rewriter.getI32IntegerAttr(dilation_h_factor), + /*dilation_w_factor=*/rewriter.getI32IntegerAttr(dilation_w_factor), + /*fused_activation_function=*/rewriter.getStringAttr("NONE"), + /*padding=*/ + rewriter.getStringAttr(IsSamePadding(op, dimension_numbers) + ? kPaddingSame + : kPaddingValid), + /*stride_h=*/rewriter.getI32IntegerAttr(stride_h), + /*stride_w=*/rewriter.getI32IntegerAttr(stride_w)); + } + } + + static LogicalResult MatchInput(Value input) { + auto input_type = llvm::cast(input.getType()); + if (const auto input_element_type = input_type.getElementType(); + !IsI8F32UniformQuantizedType(input_element_type)) { + LLVM_DEBUG(llvm::dbgs() + << "Expected an i8->f32 uniform quantized type. Got: " + << input_element_type << ".\n"); + return failure(); + } + + return success(); + } + + static LogicalResult MatchFilter(Value filter) { + auto filter_type = llvm::cast(filter.getType()); + const Type filter_element_type = filter_type.getElementType(); + if (!IsI8F32UniformQuantizedPerAxisType(filter_type.getElementType())) { + LLVM_DEBUG( + llvm::dbgs() + << "Expected a per-channel uniform quantized (i8->f32) type. Got: " + << filter_element_type << "\n"); + return failure(); + } + + if (llvm::cast(filter_element_type) + .getQuantizedDimension() != 3) { + LLVM_DEBUG(llvm::dbgs() << "Quantized dimension should be 3. Got: " + << filter_element_type << "\n"); + return failure(); + } + return success(); + } + + static LogicalResult MatchOutput(Value output) { + const Type output_element_type = GetElementType(output); + if (!IsI32F32UniformQuantizedPerAxisType(output_element_type) && + !IsI8F32UniformQuantizedType(output_element_type)) { + LLVM_DEBUG( + llvm::dbgs() + << "Expected a per-channel uniform quantized (i32->f32) type or " + << "per-tensor uniform quantized (i8->f32) type. Got: " + << output_element_type << ".\n"); + return failure(); + } + return success(); + } + // Create a `tfl.pad` op to apply explicit padding to the input tensor that + // correspond to the `padding` attribute from the `stablehlo.convolution` op. + TFL::PadOp CreateTflPadOp(Location loc, + const DenseIntElementsAttr padding_attr, + Value input_value, + PatternRewriter& rewriter) const { + auto padding_values = padding_attr.getValues(); + // [[h_low, h_high], [w_low, w_high]]. + DCHECK_EQ(padding_attr.size(), 4); + + // In StableHLO the padding attribute doesn't include the padding values for + // input and output feature dimensions (because they are 0 anyways). In + // TFLite, padding values for input and output feature dimensions should be + // explicitly set to 0s. Note that TFLite's input tensor is formatted as + // OHWI. The resulting pad values becomes: + // [[0, 0], [h_low, h_high], [w_low, w_high], [0, 0]] + SmallVector tfl_pad_values = {0, 0}; // For output feature dim. + for (const int64_t padding_value : padding_values) { + tfl_pad_values.push_back(CastI64ToI32(padding_value).value()); + } + // For input feature dim. + tfl_pad_values.push_back(0); + tfl_pad_values.push_back(0); + + const auto input_tensor_type = + llvm::cast(input_value.getType()); + const int64_t rank = input_tensor_type.getRank(); + + SmallVector padded_output_tensor_shape = + InferPaddedTensorShape(input_tensor_type.getShape(), tfl_pad_values); + + auto padded_output_tensor_type = RankedTensorType::get( + padded_output_tensor_shape, input_tensor_type.getElementType()); + + // The pad values is provided as a const op. + auto pad_value_const_op = TFL::ConstOp::create( + rewriter, loc, /*value=*/ + DenseIntElementsAttr::get( + RankedTensorType::get({rank, 2}, rewriter.getIntegerType(32)), + tfl_pad_values)); + + return TFL::PadOp::create(rewriter, loc, + /*output=*/padded_output_tensor_type, input_value, + /*padding=*/pad_value_const_op.getResult()); + } + + // Infers the output tensor's shape after padding `tfl_pad_values` to the + // `tensor_shape`. `tfl_pad_values` should be formatted as `[[low_0, high_0], + // [low_1, high_1], ..., [low_n, high_n]]`, where `low_x` and `high_x` are the + // low and high paddings for the x-th dimension, respectively. + SmallVector InferPaddedTensorShape( + const ArrayRef tensor_shape, + const ArrayRef tfl_pad_values) const { + SmallVector padded_shape(tensor_shape.begin(), tensor_shape.end()); + for (int i = 0; i < padded_shape.size(); ++i) { + // Left padding + right padding. + int32_t padding = tfl_pad_values[i * 2] + tfl_pad_values[i * 2 + 1]; + padded_shape[i] += padding; + } + + return padded_shape; + } + + std::pair GetDimSize( + const ArrayRef shape, const ArrayRef indexes) const { + return {shape[indexes[0]], shape[indexes[1]]}; + } + + bool IsTransposeConv( + stablehlo::ConvolutionOp op, + stablehlo::ConvDimensionNumbersAttr dimension_numbers) const { + const auto [input_height, input_width, output_height, output_width] = + GetInOutDimensions(op, dimension_numbers); + const auto [stride_height, stride_width] = GetStrides(op); + + // Reference: https://arxiv.org/pdf/1603.07285.pdf + // Section 4.6 > Relationship 13 states an associated transposed + // convolution should have `s = 1`. + // For `VALID` padding, the condition below will always hold true. + // For `SAME` padding, express via regular convolution. + return output_height > input_height && output_width > input_width && + stride_height == 1 && stride_width == 1; + } + + bool IsSamePadding( + stablehlo::ConvolutionOp op, + stablehlo::ConvDimensionNumbersAttr dimension_numbers) const { + const auto [input_height, input_width, output_height, output_width] = + GetInOutDimensions(op, dimension_numbers); + const auto [stride_height, stride_width] = GetStrides(op); + + // Below convolution arithmetic for `SAME` padding calculation is + // referenced from + // https://www.tensorflow.org/api_docs/python/tf/nn/convolution. The + // following condition must hold true for padding to be `SAME`: + // output_spatial_shape[i] = ceil(input_spatial_shape[i] / strides[i]) + auto get_output_dim_for_same_padding = [](int64_t input_dim, + int64_t stride_dim) -> int64_t { + return std::ceil(input_dim / static_cast(stride_dim)); + }; + return output_height == + get_output_dim_for_same_padding(input_height, stride_height) && + output_width == + get_output_dim_for_same_padding(input_width, stride_width); + } + + bool IsValidPadding( + stablehlo::ConvolutionOp op, + stablehlo::ConvDimensionNumbersAttr dimension_numbers) const { + const auto [input_height, input_width, output_height, output_width] = + GetInOutDimensions(op, dimension_numbers); + const auto [dilation_height, dilation_width] = GetDilationFactors(op); + const auto [stride_height, stride_width] = GetStrides(op); + + // Below convolution arithmetic for `VALID` padding calculation is + // referenced from + // https://www.tensorflow.org/api_docs/python/tf/nn/convolution. The + // following condition must hold true for padding to be `VALID`: + // output_spatial_shape[i] = ceil((input_spatial_shape[i] - + // (spatial_filter_shape[i]-1) * dilation_rate[i]) / strides[i]) + auto get_output_dim_for_valid_padding = + [](int64_t input_dim, int64_t dilation_dim, int64_t kernel_dim, + int64_t stride_dim) -> int64_t { + return std::ceil((input_dim - (kernel_dim - 1) * dilation_dim) / + stride_dim); + }; + return output_height == + get_output_dim_for_valid_padding(input_height, dilation_height, + input_width, stride_height) && + output_width == + get_output_dim_for_valid_padding(input_width, dilation_width, + input_height, stride_width); + } + + // Determines if the padding attribute is "VALID", "SAME", or unset. + // If not, the input's shape should be adjusted with explicit `tfl.pad` op. + // (https://www.tensorflow.org/api_docs/python/tf/nn). + bool HasProperPadding(stablehlo::ConvolutionOp op, + stablehlo::ConvDimensionNumbersAttr dimension_numbers, + const DenseIntElementsAttr padding_attr) const { + return !padding_attr || IsSamePadding(op, dimension_numbers) || + IsValidPadding(op, dimension_numbers); + } + + // Returns the padding amount for the height and width, respectively. + SmallVector GetPadding(stablehlo::ConvolutionOp op) const { + DenseIntElementsAttr padding_attr = op.getPaddingAttr(); + if (!padding_attr) { + return {0, 0, 0, 0}; + } + + auto padding_values = padding_attr.getValues(); + // Padding has [[h_low, h_high], [w_low, w_high]] format. + // https://github.com/openxla/stablehlo/blob/main/docs/spec.md#convolution. + return {padding_values[0], padding_values[1], padding_values[2], + padding_values[3]}; + } + + // Returns the input and output dimensions, respectively. + std::tuple GetInOutDimensions( + stablehlo::ConvolutionOp op, + stablehlo::ConvDimensionNumbersAttr dimension_numbers) const { + const auto [input_height, input_width] = GetDimSize( + llvm::cast(op->getOperand(0).getType()).getShape(), + dimension_numbers.getInputSpatialDimensions()); + const auto [output_height, output_width] = GetDimSize( + llvm::cast(op->getResult(0).getType()).getShape(), + dimension_numbers.getOutputSpatialDimensions()); + return {input_height, input_width, output_height, output_width}; + } + + // Returns the stride amount for the height and width, respectively. + std::pair GetStrides(stablehlo::ConvolutionOp op) const { + std::optional> window_strides_attr = + op.getWindowStrides(); + if (!window_strides_attr.has_value()) { + return {1, 1}; // Default values. + } + + auto window_strides_attr_value = window_strides_attr.value(); + // It is guaranteed from the spec that it has two values: + // https://github.com/openxla/stablehlo/blob/main/docs/spec.md#convolution. + return {window_strides_attr_value[0], window_strides_attr_value[1]}; + } + + // Returns the dilation amount for the height and width, respectively. + std::pair GetDilationFactors( + stablehlo::ConvolutionOp op) const { + std::optional> lhs_dilation_attr = op.getLhsDilation(); + if (!lhs_dilation_attr.has_value()) { + return {1, 1}; // Default values. + } + + auto lhs_dilation_attr_value = lhs_dilation_attr.value(); + // It is guaranteed from the spec that it has two values: + // https://github.com/openxla/stablehlo/blob/main/docs/spec.md#convolution. + return {lhs_dilation_attr_value[0], lhs_dilation_attr_value[1]}; + } + + TFL::QConstOp GetBiasOpAndUpdateQuantizedFilterConstant( + stablehlo::ConvolutionOp op, TFL::QConstOp& new_filter_constant_op, + PatternRewriter& rewriter, const bool is_depthwise, + const bool has_i32_output, const bool fuse_bias_constant) const { + Value filter_value = op.getOperand(1); + Operation* filter_op = filter_value.getDefiningOp(); + auto filter_uniform_quantized_type = + llvm::cast( + GetElementType(filter_value)); + auto filter_constant_value_attr = cast( + cast(filter_value.getDefiningOp()).getValue()); + const DenseIntElementsAttr new_filter_value_attr = + TransposeFilterInConvolution(filter_op->getLoc(), rewriter, + filter_constant_value_attr, is_depthwise); + int64_t kernel_output_feature_dim = + GetConvolutionKernelOutputFeatureDimension(is_depthwise); + // Create a new quantized tensor type for the filter. This is required + // because the quantized dimension is changed from 3 -> 0. `TFL::Conv2DOp` + // requires the quantized dimension to be 0 because it accepts a filter + // tensor of format OHWI + // (https://github.com/tensorflow/tensorflow/blob/5430e5e238f868ce977df96ba89c9c1d31fbe8fa/tensorflow/compiler/mlir/lite/ir/tfl_ops.td#L933). + // The quantized dimension should correspond to the output feature + // dimension. + auto new_filter_quantized_type = CreateI8F32UniformQuantizedPerAxisType( + filter_op->getLoc(), *op.getContext(), + filter_uniform_quantized_type.getScales(), + filter_uniform_quantized_type.getZeroPoints(), + /*quantization_dimension=*/kernel_output_feature_dim, + /*narrow_range=*/true); + const auto new_filter_result_type = RankedTensorType::getChecked( + filter_op->getLoc(), + /*shape=*/new_filter_value_attr.getShapedType().getShape(), + /*type=*/new_filter_quantized_type); + const int64_t num_output_features = + new_filter_result_type.getShape()[kernel_output_feature_dim]; + new_filter_constant_op = + TFL::QConstOp::create(rewriter, filter_op->getLoc(), + /*output=*/TypeAttr::get(new_filter_result_type), + new_filter_value_attr); + return GetBiasOp(op, rewriter, new_filter_result_type, + new_filter_quantized_type, + /*bias_shape=*/{num_output_features}, has_i32_output, + fuse_bias_constant); + } + + TFL::QConstOp GetBiasOp( + stablehlo::ConvolutionOp op, PatternRewriter& rewriter, + const RankedTensorType new_filter_result_type, + const UniformQuantizedPerAxisType new_filter_quantized_type, + const SmallVector bias_shape, const bool has_i32_output, + const bool fuse_bias_constant) const { + const SmallVector bias_scales = GetBiasScales( + /*input_scale=*/llvm::cast( + GetElementType(op.getOperand(0))) + .getScale(), + /*filter_scales=*/new_filter_quantized_type.getScales()); + + const auto bias_quantized_type = CreateI32F32UniformQuantizedPerAxisType( + op.getLoc(), *op.getContext(), std::move(bias_scales), + new_filter_quantized_type.getZeroPoints(), + /*quantization_dimension=*/0); + const auto bias_type = RankedTensorType::getChecked(op.getLoc(), bias_shape, + bias_quantized_type); + TFL::QConstOp bias; + if (fuse_bias_constant && has_i32_output) { + Operation* add_op = FindUserOfType(op); + Operation* bias_const_op = GetBiasConstOp(add_op); + const ElementsAttr bias_constant_value = + cast(bias_const_op).getValue(); + bias = TFL::QConstOp::create(rewriter, op.getLoc(), + /*output=*/TypeAttr::get(bias_type), + /*value=*/bias_constant_value); + } else { + // Create a bias constant. It should have values of 0. + const auto bias_value_type = RankedTensorType::getChecked( + op.getLoc(), bias_shape, rewriter.getI32Type()); + // Create a bias filled with zeros. Mimics the behavior of no bias add. + const auto bias_value = DenseIntElementsAttr::get( + bias_value_type, + APInt(/*numBits=*/32, /*value=*/0, /*isSigned=*/true)); + bias = TFL::QConstOp::create(rewriter, op.getLoc(), + /*output=*/TypeAttr::get(bias_type), + /*value=*/bias_value); + } + return bias; + } +}; + +// Rewrites quantized `stablehlo.transpose` to `tfl.transpose`. +class RewriteQuantizedTransposeOp + : public OpRewritePattern { + public: + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(stablehlo::TransposeOp op, + PatternRewriter& rewriter) const override { + if (!IsOpFullyQuantized(op)) { + return failure(); + } + auto operand_type = llvm::cast(op.getOperand().getType()); + const int64_t rank = operand_type.getRank(); + ArrayRef shape(rank); + TensorType permutation_type = + operand_type.cloneWith(shape, rewriter.getI32Type()); + // Cast permutation attribute from i64 to i32 as they are required to be i32 + // in TFLite. + SmallVector permutation_i32 = + CastI64ArrayToI32(op.getPermutation()).value(); + auto permutation_attr = + DenseIntElementsAttr::get(permutation_type, permutation_i32); + auto permutation = + arith::ConstantOp::create(rewriter, op.getLoc(), permutation_attr); + rewriter.replaceOpWithNewOp(op, op.getOperand(), + permutation); + return success(); + } +}; + +// Rewrites quantized stablehlo.reshape to tfl.reshape. +class RewriteQuantizedReshapeOp + : public OpRewritePattern { + public: + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(stablehlo::ReshapeOp op, + PatternRewriter& rewriter) const override { + if (!IsOpFullyQuantized(op)) { + return failure(); + } + rewriter.replaceOpWithNewOp( + op, op.getOperand(), + CreateI32ShapeConstantOp(op.getResult().getType(), op->getLoc(), + rewriter)); + return success(); + } +}; + +class RewriteQuantizedDynamicReshapeOp + : public OpRewritePattern { + public: + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(stablehlo::DynamicReshapeOp op, + PatternRewriter& rewriter) const override { + if (!IsQuantizedTensorType(op.getOperand().getType()) || + !IsQuantizedTensorType(op.getResult().getType())) { + return failure(); + } + rewriter.replaceOpWithNewOp(op, op.getOperand(), + op.getOutputShape()); + return success(); + } +}; + +// Rewrites quantized stablehlo.select to tfl.select_v2. +// TODO: b/322428814 - Add StableHLO quantizer integration tests for ODML. +class RewriteQuantizedSelectOp : public OpRewritePattern { + public: + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(stablehlo::SelectOp op, + PatternRewriter& rewriter) const override { + if (!IsQuantizedTensorType(op.getOperand(1).getType())) { + return failure(); + } + if (!IsQuantizedTensorType(op.getOperand(2).getType())) { + return failure(); + } + if (!IsQuantizedTensorType(op.getResult().getType())) { + return failure(); + } + Value pred = op.getOperand(0); + Value on_true = op.getOperand(1); + Value on_false = op.getOperand(2); + rewriter.replaceOpWithNewOp(op, pred, on_true, on_false); + return success(); + } +}; + +// Rewrites quantized stablehlo.concatenate to tfl.concatenation. +// TODO: b/322428814 - Add StableHLO quantizer integration tests for ODML. +class RewriteQuantizedConcatenateOp + : public OpRewritePattern { + public: + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(stablehlo::ConcatenateOp op, + PatternRewriter& rewriter) const override { + if (!IsOpFullyQuantized(op)) { + return failure(); + } + Type output_type = op.getResult().getType(); + uint32_t axis = CastI64ToI32(op.getDimension()).value(); + rewriter.replaceOpWithNewOp( + op, output_type, op.getOperands(), axis, + /*fused_activation_function=*/rewriter.getStringAttr("NONE")); + return success(); + } +}; + +// Rewrites quantized stablehlo.pad to tfl.padv2. +// tfl.dilate is introduced in between when interior padding exists. +// TODO: b/322428814 - Add StableHLO quantizer integration tests for ODML. +class RewriteQuantizedPadOp : public OpRewritePattern { + public: + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(stablehlo::PadOp op, + PatternRewriter& rewriter) const override { + if (!IsOpFullyQuantized(op)) { + return failure(); + } + Value input = op.getOperand(); + // If any of the interior padding is non-zero, operand should be dilated + // first, and then padded. + if (llvm::any_of(op.getInteriorPadding(), + [](int64_t pad) { return pad != 0; })) { + input = InsertDilateOp(op, rewriter); + } + + TensorType operand_type = llvm::cast(input.getType()); + const int64_t rank = operand_type.getRank(); + // Shape of padding should be [rank, 2]. + SmallVector shape{rank, 2}; + TensorType padding_type = + operand_type.cloneWith(shape, rewriter.getI32Type()); + + ArrayRef padding_low = op.getEdgePaddingLow(); + ArrayRef padding_high = op.getEdgePaddingHigh(); + SmallVector padding_value; + for (int i = 0; i < rank; ++i) { + padding_value.push_back(CastI64ToI32(padding_low[i]).value()); + padding_value.push_back(CastI64ToI32(padding_high[i]).value()); + } + + TensorType output_type = llvm::cast(op.getResult().getType()); + Value constant_values = op.getPaddingValue(); + auto padding_attr = DenseIntElementsAttr::get(padding_type, padding_value); + auto padding = + arith::ConstantOp::create(rewriter, op.getLoc(), padding_attr); + rewriter.replaceOpWithNewOp(op, output_type, input, padding, + constant_values); + return success(); + } + + Value InsertDilateOp(stablehlo::PadOp op, PatternRewriter& rewriter) const { + Value input = op.getOperand(); + TensorType operand_type = llvm::cast(input.getType()); + const int64_t rank = operand_type.getRank(); + + ArrayRef dilate_shape(rank); + TensorType dilate_type = + operand_type.cloneWith(dilate_shape, rewriter.getI32Type()); + ArrayRef interior_padding_i64 = op.getInteriorPadding(); + SmallVector interior_padding_i32 = + CastI64ArrayToI32(interior_padding_i64).value(); + auto dilate_attr = + DenseIntElementsAttr::get(dilate_type, interior_padding_i32); + auto dilate = arith::ConstantOp::create(rewriter, op.getLoc(), dilate_attr); + + // Shape after dilation. + SmallVector dilated_shape(rank); + ArrayRef operand_shape = operand_type.getShape(); + for (int i = 0; i < rank; ++i) { + dilated_shape[i] = + operand_shape[i] + interior_padding_i64[i] * (operand_shape[i] - 1); + } + TensorType output_type = llvm::cast(op.getResult().getType()); + Type dilated_output_type = output_type.clone(dilated_shape); + Value constant_values = op.getPaddingValue(); + + return TFL::DilateOp::create(rewriter, dilate.getLoc(), dilated_output_type, + input, dilate, constant_values); + } +}; + +// Rewrites quantized stablehlo.slice to tfl.slice or tfl.strided_slice. +class RewriteQuantizedSliceOp : public OpRewritePattern { + public: + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(stablehlo::SliceOp op, + PatternRewriter& rewriter) const override { + if (!IsOpFullyQuantized(op)) { + return failure(); + } + auto operand_type = llvm::cast(op.getOperand().getType()); + Type output_type = op.getResult().getType(); + const int64_t rank = operand_type.getRank(); + + ArrayRef idx_shape(rank); + TensorType idx_type = + operand_type.cloneWith(idx_shape, rewriter.getI32Type()); + + ArrayRef start_idx_i64 = op.getStartIndices(); + ArrayRef limit_idx_i64 = op.getLimitIndices(); + + SmallVector start_idx_i32 = + CastI64ArrayToI32(start_idx_i64).value(); + auto start_idx_attr = DenseIntElementsAttr::get(idx_type, start_idx_i32); + auto start_idx = + arith::ConstantOp::create(rewriter, op.getLoc(), start_idx_attr); + + SmallVector slice_size_i32(rank); + for (int i = 0; i < rank; ++i) { + slice_size_i32[i] = + CastI64ToI32(limit_idx_i64[i] - start_idx_i64[i]).value(); + } + auto slice_size_attr = DenseIntElementsAttr::get(idx_type, slice_size_i32); + auto slice_size = + arith::ConstantOp::create(rewriter, op.getLoc(), slice_size_attr); + + ArrayRef strides = op.getStrides(); + // If stride of every dimension is 1, create tfl.slice and return early. + // Otherwise, create tfl.strided_slice instead. + if (llvm::all_of(strides, [](int64_t stride) { return stride == 1; })) { + rewriter.replaceOpWithNewOp( + op, output_type, op.getOperand(), start_idx, slice_size); + return success(); + } + + SmallVector stride_i32 = CastI64ArrayToI32(strides).value(); + auto stride_attr = DenseIntElementsAttr::get(idx_type, stride_i32); + auto stride = arith::ConstantOp::create(rewriter, op.getLoc(), stride_attr); + rewriter.replaceOpWithNewOp( + op, output_type, op.getOperand(), start_idx, slice_size, stride, + /*begin_mask=*/0, /*end_mask=*/0, + /*ellipsis_mask=*/0, /*new_axis_mask=*/0, /*shrink_axis_mask=*/0, + /*offset=*/false); + return success(); + } +}; + +// Rewrites quantized stablehlo.broadcast_in_dim to tfl.broadcast_to. +// tfl.transpose is introduced when broadcast_dimensions is not in ascending +// order. Also, tfl.expand_dims is introduced when input rank is smaller than +// output rank. +// TODO: b/322428814 - Add StableHLO quantizer integration tests for ODML. +class RewriteQuantizedBroadcastInDimOp + : public OpRewritePattern { + public: + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(stablehlo::BroadcastInDimOp op, + PatternRewriter& rewriter) const override { + if (!IsOpFullyQuantized(op)) { + return failure(); + } + auto operand_type = llvm::cast(op.getOperand().getType()); + auto output_type = llvm::cast(op.getResult().getType()); + Value input = op.getOperand(); + + // If broadcast_dimensions is not in ascending order, transpose first. + if (!llvm::is_sorted(op.getBroadcastDimensions())) { + input = InsertTransposeOp(op, rewriter); + } + + // If rank of operand is smaller than that of the output, expand dimensions + // before broadcasting. + if (operand_type.getRank() < output_type.getRank()) { + input = InsertExpandDimsOp(op, rewriter, input, output_type.getRank()); + } + + SmallVector broadcast_shape = + CastI64ArrayToI32(output_type.getShape()).value(); + TensorType broadcast_shape_type = + output_type.cloneWith({output_type.getRank()}, rewriter.getI32Type()); + auto broadcast_shape_attr = + DenseIntElementsAttr::get(broadcast_shape_type, broadcast_shape); + auto shape = + arith::ConstantOp::create(rewriter, op.getLoc(), broadcast_shape_attr); + + rewriter.replaceOpWithNewOp(op, output_type, input, + shape); + return success(); + } + + Value InsertTransposeOp(stablehlo::BroadcastInDimOp op, + PatternRewriter& rewriter) const { + SmallVector sorted_dims = + llvm::to_vector(op.getBroadcastDimensions()); + llvm::sort(sorted_dims); + auto broadcast_dims = op.getBroadcastDimensions(); + SmallVector permutation( + llvm::map_range(broadcast_dims, [sorted_dims](int64_t dim) { + return static_cast(llvm::find(sorted_dims, dim) - + sorted_dims.begin()); + })); + auto operand_type = llvm::cast(op.getOperand().getType()); + TensorType perm_type = operand_type.cloneWith( + {static_cast(permutation.size())}, rewriter.getI32Type()); + auto perm_attr = DenseIntElementsAttr::get(perm_type, permutation); + auto perm = arith::ConstantOp::create(rewriter, op.getLoc(), perm_attr); + Value input = op.getOperand(); + + return TFL::TransposeOp::create(rewriter, op.getLoc(), input, perm); + } + + Value InsertExpandDimsOp(stablehlo::BroadcastInDimOp op, + PatternRewriter& rewriter, Value input, + int64_t output_rank) const { + auto input_type = llvm::cast(input.getType()); + SmallVector input_shape(input_type.getShape()); + SmallVector input_dims = + llvm::to_vector(op.getBroadcastDimensions()); + + while (input_dims.size() < output_rank) { + int32_t dim_to_expand = 0; + for (int32_t i = 0; i < output_rank; ++i) { + if (!llvm::is_contained(input_dims, i)) { + dim_to_expand = i; + break; + } + } + + TensorType dim_type = input_type.cloneWith({static_cast(1)}, + rewriter.getI32Type()); + ArrayRef dims(dim_to_expand); + auto dim_attr = DenseIntElementsAttr::get(dim_type, dims); + auto dim = arith::ConstantOp::create(rewriter, op.getLoc(), dim_attr); + + input_shape.insert(input_shape.begin() + dim_to_expand, 1); + TensorType expanded_type = input_type.clone(input_shape); + input = TFL::ExpandDimsOp::create(rewriter, op.getLoc(), expanded_type, + input, dim); + + // Update expanded dimension in the input dimensions for the next + // iteration. + input_dims.push_back(static_cast(dim_to_expand)); + } + return input; + } +}; + +// Rewrites quantized stablehlo.reduce_window with max to tfl.max_pool_2d. +class RewriteQuantizedReduceWindowOpWithMax + : public OpRewritePattern { + public: + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(stablehlo::ReduceWindowOp op, + PatternRewriter& rewriter) const override { + if (match(op).failed()) { + return failure(); + } + rewrite(op, rewriter); + return success(); + } + + private: + LogicalResult MatchBinaryReduceFunction(Region& function) const { + Block& body = function.front(); + if (body.getNumArguments() != 2) return failure(); + + auto return_op = dyn_cast(body.back()); + if (!return_op) return failure(); + if (return_op.getNumOperands() != 1) return failure(); + + auto reduce_op = dyn_cast_or_null( + return_op.getOperands().front().getDefiningOp()); + if (!reduce_op) return failure(); + return success(reduce_op.getLhs() == body.getArgument(0) && + reduce_op.getRhs() == body.getArgument(1)); + } + + LogicalResult match(stablehlo::ReduceWindowOp op) const { + // Check that the reduce-window is a max-reduce-window. + if (failed(MatchBinaryReduceFunction(op.getBody()))) { + return failure(); + } + + // Only 2d pooling is supported in TFLite. + if (op.getWindowDimensions().size() != 4) { + return failure(); + } + + // reduce_window op with dilations or padding will supported later. + // TODO: b/321099943 - Support reduce_window op with dilations and padding. + if (op.getBaseDilations().has_value() || + op.getWindowDilations().has_value() || op.getPadding().has_value()) { + return failure(); + } + + // Window_dimensions and window_strides should have batch and channel + // dimension of 1 as they cannot be specified in tfl.max_pool_2d. + ArrayRef window_dims = op.getWindowDimensions(); + if (window_dims[0] != 1 || window_dims[3] != 1) { + return failure(); + } + std::optional> window_strides = op.getWindowStrides(); + if (window_strides.has_value()) { + if ((*window_strides)[0] != 1 || (*window_strides)[3] != 1) { + return failure(); + } + } + + return success(IsOpFullyQuantized(op)); + } + + void rewrite(stablehlo::ReduceWindowOp op, PatternRewriter& rewriter) const { + Type result_type = op.getResult(0).getType(); + Value input = op.getOperand(0); + // Ops with padding is rejected in matching function, so we can use the + // padding to be 'VALID'. + StringAttr padding = rewriter.getStringAttr("VALID"); + + // Use NHWC format. + int32_t stride_h = 1; + int32_t stride_w = 1; + std::optional> window_strides = op.getWindowStrides(); + if (window_strides.has_value()) { + stride_h = CastI64ToI32((*window_strides)[1]).value(); + stride_w = CastI64ToI32((*window_strides)[2]).value(); + } + auto stride_h_attr = IntegerAttr::get(rewriter.getI32Type(), stride_h); + auto stride_w_attr = IntegerAttr::get(rewriter.getI32Type(), stride_w); + + ArrayRef window_dims = op.getWindowDimensions(); + auto window_w_attr = IntegerAttr::get(rewriter.getI32Type(), + CastI64ToI32(window_dims[2]).value()); + auto window_h_attr = IntegerAttr::get(rewriter.getI32Type(), + CastI64ToI32(window_dims[1]).value()); + StringAttr activation_function = rewriter.getStringAttr("NONE"); + + rewriter.replaceOpWithNewOp( + op, result_type, input, padding, stride_w_attr, stride_h_attr, + window_w_attr, window_h_attr, activation_function); + } +}; + +// Rewrites quantized `stablehlo.gather` to `tfl.gather_nd`. +// 4 conditions below are checked to filter out cases where `transpose` and +// `slice` are required for conversion to `tfl.gather_nd`. +// Condition 1 - `start_index_map` should be an increasing sequence starting +// from 0 with step 1. +// Condition 2 - `index_vector_dim` should be the last dimension of +// `start_indices`. +// Condition 3 - `offset_dims` should be the last dimensions of `output`. +// Condition 4 - shape of slice should be same with shape of input on the +// offset dimensions. +class RewriteQuantizedGatherOp : public OpRewritePattern { + public: + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(stablehlo::GatherOp op, + PatternRewriter& rewriter) const override { + const Type input_type = op.getOperand().getType(); + const Type output_type = op.getResult().getType(); + if (!IsQuantizedTensorType(input_type) || + !IsQuantizedTensorType(output_type)) { + return failure(); + } + + auto output_tensor_type = llvm::cast(output_type); + if (!output_tensor_type.hasRank()) { + return failure(); + } + int64_t output_rank = output_tensor_type.getRank(); + ::mlir::stablehlo::GatherDimensionNumbersAttr dim_numbers = + op.getDimensionNumbers(); + ArrayRef offset_dims = dim_numbers.getOffsetDims(); + ArrayRef start_index_map = dim_numbers.getStartIndexMap(); + + // Check for condition 1. + if (start_index_map.empty() || start_index_map[0] != 0) { + return failure(); + } + for (int64_t i = 0; i < start_index_map.size() - 1; ++i) { + if (start_index_map[i + 1] - start_index_map[i] != 1) { + return failure(); + } + } + + const int64_t index_vector_dim = dim_numbers.getIndexVectorDim(); + RankedTensorType start_indices_type = op.getStartIndices().getType(); + if (!start_indices_type.hasRank()) { + return failure(); + } + int64_t start_indices_rank = start_indices_type.getRank(); + // Check for condition 2. + if (index_vector_dim != start_indices_rank - 1) { + return failure(); + } + + int64_t offset_dims_len = offset_dims.size(); + // Check for condition 3. + for (const auto& [index, offset_dim] : llvm::enumerate(offset_dims)) { + if (offset_dim != output_rank - offset_dims_len + index) { + return failure(); + } + } + + ArrayRef slice_sizes = op.getSliceSizes(); + ArrayRef collapsed_slice_dims = + dim_numbers.getCollapsedSliceDims(); + SmallVector slice_shape; + for (int64_t i = 0; i < slice_sizes.size(); ++i) { + // `collapsed_slice_dims` are excluded for slice shape. + if (!llvm::is_contained(collapsed_slice_dims, i)) { + slice_shape.push_back(slice_sizes[i]); + } + } + // Rank of slice and offset should be the same by the op constraints. + if (slice_shape.size() != offset_dims.size()) { + return failure(); + } + + // Input type is checked to be quantized tensor type. + const auto input_shape = + llvm::cast(op.getOperand().getType()).getShape(); + SmallVector input_offset_shape; + for (int64_t i = 0; i < input_shape.size(); ++i) { + if (!llvm::is_contained(start_index_map, i)) { + input_offset_shape.push_back(input_shape[i]); + } + } + + // Check for condition 4. + for (auto [slice_size, input_offset_size] : + llvm::zip_equal(slice_shape, input_offset_shape)) { + if (slice_size != input_offset_size) { + return failure(); + } + } + + rewriter.replaceOpWithNewOp( + op, /*output=*/op.getResult().getType(), /*params=*/op.getOperand(), + /*indices=*/op.getStartIndices()); + return success(); + } +}; + +// Rewrites quantized stablehlo.dynamic_slice to tfl.slice. +// TODO: b/322428814 - Add StableHLO quantizer integration tests for ODML. +class RewriteQuantizedDynamicSliceOp + : public OpRewritePattern { + public: + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(stablehlo::DynamicSliceOp op, + PatternRewriter& rewriter) const override { + if (!IsQuantizedTensorType(op.getOperand().getType()) || + !IsQuantizedTensorType(op.getResult().getType()) || + !quant::HasStaticShape(op.getOperand())) { + return failure(); + } + + Type output = op.getResult().getType(); + Value input = op.getOperand(); + TensorType operand_type = llvm::cast(input.getType()); + ArrayRef operand_shape = operand_type.getShape(); + const int64_t rank = operand_type.getRank(); + const Type i64_type = rewriter.getI64Type(); + + ArrayRef slice_sizes = op.getSliceSizes(); + TensorType single_element_type = + operand_type.cloneWith({static_cast(1)}, i64_type); + + SmallVector start_indices(rank); + for (auto [i, start_index] : llvm::enumerate(op.getStartIndices())) { + // Start indices should be casted from tensor to tensor<1xi64>. + auto cast = TFL::BitcastOp::create(rewriter, op->getLoc(), + single_element_type, start_index); + int64_t upper_limit_idx = operand_shape[i] - slice_sizes[i]; + auto upper_limit_attr = + DenseIntElementsAttr::get(single_element_type, {upper_limit_idx}); + auto upper_limit_cst = + arith::ConstantOp::create(rewriter, op->getLoc(), upper_limit_attr); + // Dynamic start indices should be clamped with upper limit of + // `shape(operand) - slice_sizes)` as per semantics of + // `stablehlo.dynamic_slice`. + // (https://github.com/openxla/stablehlo/blob/main/docs/spec.md#dynamic_slice) + start_indices[i] = + TFL::MinimumOp::create(rewriter, op->getLoc(), cast, upper_limit_cst); + } + + Value concatenated = start_indices[0]; + if (rank > 1) { + SmallVector begin_shape{rank}; + Type begin_type = operand_type.cloneWith(begin_shape, i64_type); + concatenated = TFL::ConcatenationOp::create( + rewriter, op->getLoc(), begin_type, start_indices, + /*axis=*/rewriter.getI32IntegerAttr(0), + /*fused_activation_function=*/rewriter.getStringAttr("NONE")); + } + + // Clamp with lower limit. + auto lower_limit_attr = DenseIntElementsAttr::get( + single_element_type, {static_cast(0)}); + auto lower_limit_cst = + arith::ConstantOp::create(rewriter, op->getLoc(), lower_limit_attr); + // Dynamic start indices should be clamped with lower limit of + // 0 as per semantics of `stablehlo.dynamic_slice`. + // (https://github.com/openxla/stablehlo/blob/main/docs/spec.md#dynamic_slice) + auto begin = TFL::MaximumOp::create(rewriter, op->getLoc(), concatenated, + lower_limit_cst); + + SmallVector size_len{rank}; + TensorType size_type = operand_type.cloneWith(size_len, i64_type); + auto size_attr = DenseIntElementsAttr::get(size_type, slice_sizes); + auto size = arith::ConstantOp::create(rewriter, op.getLoc(), size_attr); + + rewriter.replaceOpWithNewOp(op, output, input, begin, size); + return success(); + } +}; + +class RewriteQuantizedAddOp : public OpRewritePattern { + public: + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(stablehlo::AddOp op, + PatternRewriter& rewriter) const override { + if (!IsI8F32UniformQuantizedType(GetElementType(op.getLhs())) || + !IsI8F32UniformQuantizedType(GetElementType(op.getRhs()))) { + return failure(); + } + TFL::QConstOp lhs_qconst_op; + TFL::QConstOp rhs_qconst_op; + + auto GetBroadcastedConstOp = [&](Value operand) -> TFL::QConstOp { + if (auto broadcast_op = dyn_cast_or_null( + operand.getDefiningOp())) { + auto stablehlo_const_op = dyn_cast_or_null( + broadcast_op.getOperand().getDefiningOp()); + auto const_uniform_quantized_type = + llvm::cast(stablehlo_const_op.getResult().getType()); + return TFL::QConstOp::create( + rewriter, op.getLoc(), TypeAttr::get(const_uniform_quantized_type), + cast(stablehlo_const_op.getValue())); + } + return nullptr; + }; + + lhs_qconst_op = GetBroadcastedConstOp(op.getLhs()); + rhs_qconst_op = GetBroadcastedConstOp(op.getRhs()); + + rewriter.replaceOpWithNewOp( + op, op.getResult().getType(), + lhs_qconst_op ? lhs_qconst_op : op.getOperand(0), + rhs_qconst_op ? rhs_qconst_op : op.getOperand(1), + /*fused_activation_function=*/rewriter.getStringAttr("NONE")); + return success(); + } +}; + +// Rewrites quantized `stablehlo.constant` to `tfl.pseudo_qconst`. +class RewriteQuantizedConstantOp + : public OpRewritePattern { + public: + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(stablehlo::ConstantOp op, + PatternRewriter& rewriter) const override { + if (!IsQuantizedTensorType(op.getOutput().getType())) { + return failure(); + } + rewriter.replaceOpWithNewOp( + op, /*qtype=*/TypeAttr::get(op.getOutput().getType()), + /*value=*/op.getValue()); + return success(); + } +}; + +// Splits hybrid quantized `stablehlo.dot_general` into `tfl.dequantize` and +// float `stablehlo.dot_general` op. Legalization of float +// `stablehlo.dot_general` op relies on existing passes for conversion of +// StableHLO -> MHLO -> TF -> TFL. +class RewriteHybridQuantizedDotGeneralOp + : public OpRewritePattern { + public: + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(stablehlo::DotGeneralOp op, + PatternRewriter& rewriter) const override { + // Lhs and result should not be quantized and rhs should be quantized. + if (IsQuantizedTensorType(op->getOperand(0).getType()) || + !IsQuantizedTensorType(op->getOperand(1).getType()) || + IsQuantizedTensorType(op->getResult(0).getType())) { + return failure(); + } + Value rhs = op.getRhs(); + Type lhs_element_type = + llvm::cast(op.getLhs().getType()).getElementType(); + Type dequantized_rhs_type = + quant::CloneTypeWithNewElementType(rhs.getType(), lhs_element_type); + auto dq = TFL::DequantizeOp::create(rewriter, op->getLoc(), + /*output=*/dequantized_rhs_type, + /*input=*/rhs); + rewriter.replaceAllUsesExcept(rhs, dq.getOutput(), dq); + return success(); + } +}; + +// Splits hybrid quantized `stablehlo.convolution` into `tfl.dequantize` and +// float `stablehlo.convolution` op. Weight tensor is transposed to match the +// filter tensor format for TFLite convolution. +// Legalization of float `stablehlo.convolution` op relies on existing passes +// for conversion of StableHLO -> MHLO -> TF -> TFL. +class RewriteHybridQuantizedConvolutionOp + : public OpRewritePattern { + public: + explicit RewriteHybridQuantizedConvolutionOp(MLIRContext* ctx) + : OpRewritePattern(ctx, /*benefit=*/5) {} + + LogicalResult matchAndRewrite(stablehlo::ConvolutionOp op, + PatternRewriter& rewriter) const override { + if (failed(MatchConvolutionFormat(op))) { + LLVM_DEBUG(llvm::dbgs() + << "Failed to match dimension format for convolution_op.\n"); + return failure(); + } + // Lhs and result should not be quantized and rhs should be quantized. + if (IsQuantizedTensorType(op->getOperand(0).getType()) || + !IsQuantizedTensorType(op->getOperand(1).getType()) || + IsQuantizedTensorType(op->getResult(0).getType())) { + return failure(); + } + const bool is_depthwise = IsDepthwiseConvolution(op); + + Operation* filter_op = op.getRhs().getDefiningOp(); + auto filter_constant_value_attr = cast( + cast(filter_op).getValue()); + const DenseIntElementsAttr new_filter_value_attr = + TransposeFilterInConvolution(filter_op->getLoc(), rewriter, + filter_constant_value_attr, is_depthwise); + + Type new_filter_type = GetNewWeightQuantizedType( + /*context=*/op.getContext(), /*location=*/filter_op->getLoc(), + /*new_shape=*/new_filter_value_attr.getShapedType().getShape(), + /*filter_type=*/op.getRhs().getType(), is_depthwise); + auto new_filter = TFL::QConstOp::create( + rewriter, filter_op->getLoc(), + /*output=*/TypeAttr::get(new_filter_type), new_filter_value_attr); + stablehlo::ConvDimensionNumbersAttr new_dimension_numbers = + GetTflDimensionNumbers(rewriter.getContext(), op.getDimensionNumbers(), + is_depthwise); + op.setDimensionNumbersAttr(new_dimension_numbers); + + Type lhs_element_type = + llvm::cast(op.getOperand(0).getType()).getElementType(); + Type dequantized_rhs_type = quant::CloneTypeWithNewElementType( + new_filter.getType(), lhs_element_type); + auto dq = TFL::DequantizeOp::create(rewriter, op->getLoc(), + /*output=*/dequantized_rhs_type, + /*input=*/new_filter); + rewriter.replaceAllUsesExcept(filter_op->getResult(0), dq.getOutput(), dq); + return success(); + } + + private: + // Returns new quantized type for weights after transpose. + Type GetNewWeightQuantizedType(MLIRContext* context, Location location, + ArrayRef new_shape, Type filter_type, + bool is_depthwise) const { + auto tensor_type = llvm::cast(filter_type); + auto element_type = tensor_type.getElementType(); + RankedTensorType new_filter_result_type; + if (llvm::isa(element_type)) { + auto per_axis_type = + llvm::cast(element_type); + int64_t kernel_output_feature_dim = + GetConvolutionKernelOutputFeatureDimension(is_depthwise); + auto new_filter_quantized_type = CreateI8F32UniformQuantizedPerAxisType( + location, *context, per_axis_type.getScales(), + per_axis_type.getZeroPoints(), + /*quantization_dimension=*/kernel_output_feature_dim, + /*narrow_range=*/true); + new_filter_result_type = + RankedTensorType::getChecked(location, + /*shape=*/new_shape, + /*type=*/new_filter_quantized_type); + } else if (llvm::isa(element_type)) { + auto per_tensor_type = + llvm::cast(element_type); + new_filter_result_type = + RankedTensorType::getChecked(location, + /*shape=*/new_shape, + /*type=*/per_tensor_type); + } else { + LLVM_DEBUG( + llvm::dbgs() + << "Weight tensor elements do not have uniform quantized type.\n"); + } + return new_filter_result_type; + } + + // Returns the dimension numbers of the given stablehlo's + // convolution attribute with transposed filter tensors to + // match TFLite format. + // Depthwise case (`feature_group_count` > 1) + // * `[0, 1, i, o]` -> `[i, 0, 1, o]` format. + // General convolution (`feature_group_count` = 1) + // * `[0, 1, i, o]` -> `[o, 0, 1, i]` format. + stablehlo::ConvDimensionNumbersAttr GetTflDimensionNumbers( + MLIRContext* context, + stablehlo::ConvDimensionNumbersAttr dimension_numbers, + bool is_depthwise) const { + int64_t kernel_input_feature_dim = + GetConvolutionKernelInputFeatureDimension(is_depthwise); + int64_t kernel_output_feature_dim = + GetConvolutionKernelOutputFeatureDimension(is_depthwise); + SmallVector kernel_spatial_dims{1, 2}; + + return stablehlo::ConvDimensionNumbersAttr::get( + context, dimension_numbers.getInputBatchDimension(), + dimension_numbers.getInputFeatureDimension(), + dimension_numbers.getInputSpatialDimensions(), kernel_input_feature_dim, + kernel_output_feature_dim, kernel_spatial_dims, + dimension_numbers.getOutputBatchDimension(), + dimension_numbers.getOutputFeatureDimension(), + dimension_numbers.getOutputSpatialDimensions()); + } +}; + +void UniformQuantizedStableHloToTflPass::runOnOperation() { + func::FuncOp func_op = getOperation(); + MLIRContext& ctx = getContext(); + + RewritePatternSet patterns(&ctx); + patterns.add(&ctx); + + if (failed(applyPatternsGreedily(func_op, std::move(patterns)))) { + func_op.emitError() << "Failed to convert stablehlo ops with uniform " + "quantized types to tflite ops."; + signalPassFailure(); + } +} + +} // namespace + +std::unique_ptr> +CreateUniformQuantizedStableHloToTflPass() { + return std::make_unique(); +} + +static PassRegistration pass; + +} // namespace odml +} // namespace mlir diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stateful_error_reporter.h b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stateful_error_reporter.h new file mode 100644 index 0000000000000000000000000000000000000000..cf987bb88dd97395f8d7063bdd922ac561d7359c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/stateful_error_reporter.h @@ -0,0 +1,36 @@ +/* Copyright 2024 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. +==============================================================================*/ +#ifndef TENSORFLOW_COMPILER_MLIR_LITE_STATEFUL_ERROR_REPORTER_H_ +#define TENSORFLOW_COMPILER_MLIR_LITE_STATEFUL_ERROR_REPORTER_H_ + +// LINT.IfChange +#include + +#include "tensorflow/compiler/mlir/lite/core/api/error_reporter.h" + +namespace tflite_migration { + +// Similar to tflite::ErrorReporter, except that it allows callers to get the +// last error message. +class StatefulErrorReporter : public tflite::ErrorReporter { + public: + // Returns last error message. Returns empty string if no error is reported. + virtual std::string message() = 0; +}; + +} // namespace tflite_migration +// LINT.ThenChange(//tensorflow/lite/stateful_error_reporter.h) + +#endif // TENSORFLOW_COMPILER_MLIR_LITE_STATEFUL_ERROR_REPORTER_H_ diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/symlink_files.bzl b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/symlink_files.bzl new file mode 100644 index 0000000000000000000000000000000000000000..d49ef074323be6a437713fc72f0f3ad401af3521 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/symlink_files.bzl @@ -0,0 +1,117 @@ +# Copyright 2025 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. +# ============================================================================== + +"""Macros for symlinking files into certain directories at build time.""" + +def _symlink_files_impl(ctx): + flatten = ctx.attr.flatten + strip_prefix = ctx.attr.strip_prefix + mapping = ctx.attr.mapping + outputs = [] + for src in ctx.files.srcs: + src_path = src.short_path + if src_path in mapping: + file_dst = mapping[src_path] + else: + file_dst = src.basename if flatten else src_path + if not file_dst.startswith(strip_prefix): + fail(("File {} has destination {} that does not begin with" + + " strip_prefix {}").format( + src, + file_dst, + strip_prefix, + )) + file_dst = file_dst[len(strip_prefix):] + outfile = ctx.attr.dst + "/" + file_dst + out = ctx.actions.declare_file(outfile) + outputs.append(out) + ctx.actions.symlink(output = out, target_file = src) + outputs = depset(outputs) + return [DefaultInfo( + files = outputs, + runfiles = ctx.runfiles(transitive_files = outputs), + )] + +symlink_files = rule( + implementation = _symlink_files_impl, + attrs = { + "dst": attr.string( + default = ".", + doc = "Destination directory into which to symlink `srcs`." + + " Relative to current directory.", + ), + "srcs": attr.label_list( + allow_files = True, + doc = "Files to symlink into `dst`.", + ), + "flatten": attr.bool( + default = False, + doc = "Whether files in `srcs` should all be flattened to be" + + " direct children of `dst` or preserve their existing" + + " directory structure.", + ), + "strip_prefix": attr.string( + default = "", + doc = "Literal string prefix to strip from the paths of all files" + + " in `srcs`. All files in `srcs` must begin with this" + + " prefix or be present mapping. Generally they would not be" + + " used together, but prefix stripping happens after flattening.", + ), + "mapping": attr.string_dict( + default = {}, + doc = "Dictionary indicating where individual files in `srcs`" + + " should be mapped to under `dst`. Keys are the origin" + + " path of the file (relative to the build system root) and" + + " values are the destination relative to `dst`. Files" + + " present in `mapping` ignore the `flatten` and" + + " `strip_prefix` attributes: their destination is based" + + " only on `dst` and the value for their key in `mapping`.", + ), + }, +) + +def symlink_inputs(name, rule, symlinked_inputs, **kwargs): + """Wraps a rule and symlinks input files into the current directory tree. + + Args: + rule: the rule (or macro) being wrapped. + name: name for the generated rule. + symlinked_inputs: a dictionary of dictionaries indicating label-list + arguments labels that should be passed to the generated rule after + being symlinked into the specified directory. + **kwargs: additional keyword arguments to forward to the generated rule. + """ + for kwarg, mapping in symlinked_inputs.items(): + for dst, files in mapping.items(): + if kwarg in kwargs: + fail( + "key %s is already present in this rule" % (kwarg,), + attr = "symlinked_inputs", + ) + if dst == None: + kwargs[kwarg] = files + else: + symlinked_target_name = "_{}_{}".format(name, kwarg) + symlink_files( + name = symlinked_target_name, + dst = dst, + srcs = files, + flatten = True, + ) + kwargs[kwarg] = [":" + symlinked_target_name] + rule( + name = name, + **kwargs + ) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/testdata/add.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/testdata/add.bin new file mode 100644 index 0000000000000000000000000000000000000000..d8408e0ed2cd099c072e7b30fa4d739372ceeb17 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/testdata/add.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:038f8d317c07b094e3b7d0c7aba60a3d9e441eb4188c8515920108b963c3873d +size 544 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/testdata/multi_signatures.bin b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/testdata/multi_signatures.bin new file mode 100644 index 0000000000000000000000000000000000000000..4958797db1e56545ff2b634ca90b39971e0756df --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/testdata/multi_signatures.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ab33613b98198cc7bcfa6c64b412c109e5f047374dfdfaab46eaa6b306c7cd9f +size 1216 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..eb16ffe0b6bc806790c00e06634f778a2eec868f --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/BUILD @@ -0,0 +1,46 @@ +load("//tensorflow:tensorflow.default.bzl", "filegroup") +load("//tensorflow/compiler/mlir:glob_lit_test.bzl", "glob_lit_tests") + +package( + # copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"], + licenses = ["notice"], +) + +glob_lit_tests( + name = "all_tests", + data = [":test_utilities"], + driver = "@llvm-project//mlir:run_lit.sh", + exclude = [ + "load-quantization-recipe.mlir", + # TODO(b/493282789): Enable DenseResourceElementsAttr constant folding with + # more peformant folder implementations. + "const-fold-unary-resource.mlir", + ], + size_override = { + "gpu_compatibility.mlir": "small", + "optimize.mlir": "large", + "prepare-tf.mlir": "medium", + "prepare-tf-fake-quant.mlir": "medium", + "prepare-tf-fake-quant-4bit.mlir": "medium", + "raise-custom-ops.mlir": "medium", + "rank_reduction.mlir": "small", + }, + tags_override = { + "legalize-tf.mlir": ["no_rocm"], + "optimize.mlir": ["no_rocm"], + "prepare-tf.mlir": ["no_rocm"], + "const-fold.mlir": ["no_mac_arm64"], + }, + test_file_exts = ["mlir"], +) + +# Bundle together all of the test utilities that are used by tests. +filegroup( + name = "test_utilities", + testonly = True, + data = [ + "//tensorflow/compiler/mlir/lite:litert-opt", + "@llvm-project//llvm:FileCheck", + "@llvm-project//llvm:not", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/analyze-variables.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/analyze-variables.mlir new file mode 100644 index 0000000000000000000000000000000000000000..d59d916933f5697914f27f5c80526d3f453e40db --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/analyze-variables.mlir @@ -0,0 +1,128 @@ +// RUN: litert-opt %s -split-input-file -tfl-analyze-variables-pass --cse | FileCheck %s + +// CHECK: module attributes {tfl._legalize_tfl_variables = true} +module { + func.func @f() -> tensor<*xi32> { + %0 = "tf.VarHandleOp"() {container = "c", shared_name = "v"} : () -> tensor<*x!tf_type.resource>> + %2 = "tf.ReadVariableOp"(%0) {dtype = i32} : (tensor<*x!tf_type.resource>>) -> tensor<*xi32> + func.return %2 : tensor<*xi32> + } +} + +// ----- + +// CHECK: module attributes {tfl._legalize_tfl_variables = true} +module { + func.func @main() -> tensor<*xi32> { + %0 = "tf.PartitionedCall"() {f = @f, config = "", config_proto = "", executor_type = ""} + : () -> tensor<*xi32> + func.return %0 : tensor<*xi32> + } + func.func @f() -> tensor<*xi32> { + %0 = "tf.VarHandleOp"() {container = "c", shared_name = "v"} : () -> tensor<*x!tf_type.resource>> + %1 = "tf.ReadVariableOp"(%0) {dtype = i32} : (tensor<*x!tf_type.resource>>) -> tensor<*xi32> + func.return %1 : tensor<*xi32> + } +} + + +// ----- + +// CHECK: module attributes {tfl._legalize_tfl_variables = false} +module { + func.func @main() -> tensor<*xi32> { + %0 = "tf.VarHandleOp"() {container = "c", shared_name = "v"} : () -> tensor<*x!tf_type.resource>> + %1 = "tf.PartitionedCall"(%0) {f = @f, config = "", config_proto = "", executor_type = ""} + : (tensor<*x!tf_type.resource>>) -> tensor<*xi32> + func.return %1 : tensor<*xi32> + } + func.func @f(%arg0 : tensor<*x!tf_type.resource>>) -> tensor<*xi32> { + %0 = "tf.ReadVariableOp"(%arg0) {dtype = i32} : (tensor<*x!tf_type.resource>>) -> tensor<*xi32> + func.return %0 : tensor<*xi32> + } +} + +// ----- + +// CHECK: module attributes {tfl._legalize_tfl_variables = false} +module { + func.func @main() -> tensor<*xi32> { + %0 = "tf.VarHandleOp"() {container = "c", shared_name = "v"} : () -> tensor<*x!tf_type.resource>> + %cst = arith.constant dense<2> : tensor<4xi32> + "tf.AssignAddVariableOp"(%0, %cst) {} : (tensor<*x!tf_type.resource>>, tensor<4xi32>) -> () + %1 = "tf.ReadVariableOp"(%0) {dtype = i32} : (tensor<*x!tf_type.resource>>) -> tensor<*xi32> + func.return %1 : tensor<*xi32> + } +} + +// ----- + +// CHECK: module attributes {tfl._legalize_tfl_variables = true} +module { + func.func @main() -> tensor { + %0 = "tf.VarHandleOp"() {container = "c", shared_name = "v"} : () -> tensor<*x!tf_type.resource>> + %cst = arith.constant dense<1> : tensor + %1:2 = "tfl.while"(%cst, %0) ({ + ^bb0(%arg1: tensor<*xi32>, %arg2: tensor<*x!tf_type.resource>>): + %2 = "tf.ReadVariableOp"(%arg2) {dtype = i32} : (tensor<*x!tf_type.resource>>) -> tensor<*xi32> + %3 = "tfl.greater"(%arg1, %2) : (tensor<*xi32>, tensor<*xi32>) -> tensor + "tfl.yield"(%3) : (tensor) -> () + }, { + ^bb0(%arg3: tensor<*xi32>, %arg4: tensor): + %4 = "tfl.sub"(%arg3, %arg4) {fused_activation_function = "NONE"} : + (tensor<*xi32>, tensor) -> tensor<*xi32> + "tfl.yield"(%4) : (tensor<*xi32>) -> () + }) : (tensor, tensor<*x!tf_type.resource>>) -> (tensor, tensor<*x!tf_type.resource>>) + func.return %1#0 : tensor + } +} + +// ----- + +// CHECK: module attributes {tfl._legalize_tfl_variables = false} +module { + func.func @main() -> tensor { + %0 = "tf.VarHandleOp"() {container = "c", shared_name = "v"} : () -> tensor<*x!tf_type.resource>> + %cst = arith.constant dense<1> : tensor + %1:2 = "tfl.while"(%cst, %0) ({ + ^bb0(%arg1: tensor<*xi32>, %arg2: tensor<*x!tf_type.resource>>): + %2 = "tf.ReadVariableOp"(%arg2) {dtype = i32} : (tensor<*x!tf_type.resource>>) -> tensor<*xi32> + %3 = "tfl.greater"(%arg1, %2) : (tensor<*xi32>, tensor<*xi32>) -> tensor + "tfl.yield"(%3) : (tensor) -> () + }, { + ^bb0(%arg3: tensor<*xi32>, %arg4: tensor<*x!tf_type.resource>>): + %cst1 = arith.constant dense<2> : tensor<4xi32> + "tf.AssignAddVariableOp"(%arg4, %cst1) {} : (tensor<*x!tf_type.resource>>, tensor<4xi32>) -> () + %4 = "tf.ReadVariableOp"(%arg4) {dtype = i32} : (tensor<*x!tf_type.resource>>) -> tensor<*xi32> + "tfl.yield"(%4) : (tensor<*xi32>) -> () + }) : (tensor, tensor<*x!tf_type.resource>>) -> (tensor, tensor<*x!tf_type.resource>>) + func.return %1#0 : tensor + } +} + +// ----- + +// CHECK: module attributes {tfl._legalize_tfl_variables = true} +module { + func.func @main(%arg0 : tensor>>, + %arg1 : tensor<*x!tf_type.variant>) { + %cst_0 = arith.constant dense<2> : tensor + %cst_1 = arith.constant dense<0> : tensor + %0 = "tf.RepeatDataset"(%arg1, %cst_0) {device = "", + output_shapes = [#tf_type.shape], + output_types = [!tf_type.string]} : (tensor<*x!tf_type.variant>, tensor) -> tensor + + %1 = "tf.ReduceDataset"(%0, %cst_1, %arg0) { + Targuments = [!tf_type.resource], + Tstate = [i32], device = "", + f = @__reduce_func, f._tf_data_function = true, + output_shapes = [#tf_type.shape<>], + output_types = [i32], use_inter_op_parallelism = true} : (tensor, tensor, tensor>>) -> (tensor<*xi32>) + func.return + } + + func.func private @__reduce_func(%arg0: tensor {tf._user_specified_name = "args_0"}) -> (tensor) attributes {tf._tf_data_function = true, tf.signature.is_stateful} { + %0 = "tf.JustPretend"() : () -> (tensor) + func.return %0: tensor + } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/bias-quantizer.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/bias-quantizer.mlir new file mode 100644 index 0000000000000000000000000000000000000000..77a40b1c8b473342c819c32dace64e6733e76d3f --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/bias-quantizer.mlir @@ -0,0 +1,223 @@ +// RUN: litert-opt %s -tfl-bias-quantizer | FileCheck %s + +// CHECK-LABEL: QuantizeConv2DPerChannel +func.func @QuantizeConv2DPerChannel(%arg0: tensor<1x224x224x3x!quant.uniform>, + %arg1: tensor<32x3x3x3x!quant.uniform:f32:3, {1.0,2.0,3.0}>>) -> tensor<1x112x112x32xf32> { + %bias = arith.constant dense<1.0> : tensor<32xf32> + %input = "tfl.dequantize"(%arg0) : (tensor<1x224x224x3x!quant.uniform>) -> tensor<1x224x224x3xf32> + %weight = "tfl.dequantize"(%arg1) : (tensor<32x3x3x3x!quant.uniform:f32:3, {1.0,2.0,3.0}>>) -> tensor<32x3x3x3xf32> + %conv = "tfl.conv_2d"(%input, %weight, %bias) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, + fused_activation_function = "NONE", padding = "SAME", stride_h = 2 : i32, stride_w = 2 : i32} + : (tensor<1x224x224x3xf32>, tensor<32x3x3x3xf32>, tensor<32xf32>) -> tensor<1x112x112x32xf32> + func.return %conv : tensor<1x112x112x32xf32> + +// CHECK-NEXT: %[[cst:.*]] = arith.constant dense<1.000000e+00> : tensor<32xf32> +// CHECK-NEXT: %[[qbias:.*]] = "tfl.quantize"(%[[cst]]) <{qtype = tensor<32x!quant.uniform>}> {propagated} +// CHECK-NEXT: %[[bias:.*]] = "tfl.dequantize"(%[[qbias]]) +// CHECK-NEXT: %[[in:.*]] = "tfl.dequantize"(%arg0) +// CHECK-NEXT: %[[w:.*]] = "tfl.dequantize"(%arg1) +// CHECK-NEXT: %[[conv:.*]] = "tfl.conv_2d"(%[[in]], %[[w]], %[[bias]]) +// CHECK-NEXT: return %[[conv]] +} + +// ----- + +// CHECK-LABEL: QuantizeConv2DPerChannelConst +func.func @QuantizeConv2DPerChannelConst(%arg0: tensor<1x224x224x3x!quant.uniform>, + %arg1: tensor<32x3x3x3x!quant.uniform:f32:3, {1.0,2.0,3.0}>>) -> tensor<1x112x112x32xf32> { + %bias = "tfl.pseudo_const"() {value = dense<1.000000e+00> : tensor<32xf32>} : () -> tensor<32xf32> + %input = "tfl.dequantize"(%arg0) : (tensor<1x224x224x3x!quant.uniform>) -> tensor<1x224x224x3xf32> + %weight = "tfl.dequantize"(%arg1) : (tensor<32x3x3x3x!quant.uniform:f32:3, {1.0,2.0,3.0}>>) -> tensor<32x3x3x3xf32> + %conv = "tfl.conv_2d"(%input, %weight, %bias) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, + fused_activation_function = "NONE", padding = "SAME", stride_h = 2 : i32, stride_w = 2 : i32} + : (tensor<1x224x224x3xf32>, tensor<32x3x3x3xf32>, tensor<32xf32>) -> tensor<1x112x112x32xf32> + func.return %conv : tensor<1x112x112x32xf32> + +// CHECK-NEXT: %[[cst:.*]] = "tfl.pseudo_const"() <{value = dense<1.000000e+00> : tensor<32xf32>}> : () -> tensor<32xf32> +// CHECK-NEXT: %[[qbias:.*]] = "tfl.quantize"(%[[cst]]) <{qtype = tensor<32x!quant.uniform>}> {propagated} +// CHECK-NEXT: %[[bias:.*]] = "tfl.dequantize"(%[[qbias]]) +// CHECK-NEXT: %[[in:.*]] = "tfl.dequantize"(%arg0) +// CHECK-NEXT: %[[w:.*]] = "tfl.dequantize"(%arg1) +// CHECK-NEXT: %[[conv:.*]] = "tfl.conv_2d"(%[[in]], %[[w]], %[[bias]]) +// CHECK-NEXT: return %[[conv]] +} + +// ----- + +// CHECK-LABEL: QuantizeConv2DPerChannels +func.func @QuantizeConv2DPerChannels(%arg0: tensor<1x224x224x3x!quant.uniform>, + %arg1: tensor<32x3x3x3x!quant.uniform:f32:3, {1.0,2.0,3.0}>>) -> tensor<1x112x112x32xf32> { + %bias = arith.constant dense<1.0> : tensor<32xf32> + %input = "tfl.dequantize"(%arg0) : (tensor<1x224x224x3x!quant.uniform>) -> tensor<1x224x224x3xf32> + %weight = "tfl.dequantize"(%arg1) : (tensor<32x3x3x3x!quant.uniform:f32:3, {1.0,2.0,3.0}>>) -> tensor<32x3x3x3xf32> + %conv = "tfl.conv_2d"(%input, %weight, %bias) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, + fused_activation_function = "NONE", padding = "SAME", stride_h = 2 : i32, stride_w = 2 : i32} + : (tensor<1x224x224x3xf32>, tensor<32x3x3x3xf32>, tensor<32xf32>) -> tensor<1x112x112x32xf32> + func.return %conv : tensor<1x112x112x32xf32> + +// CHECK-NEXT: %[[cst:.*]] = arith.constant dense<1.000000e+00> : tensor<32xf32> +// CHECK-NEXT: %[[qbias:.*]] = "tfl.quantize"(%[[cst]]) <{qtype = tensor<32x!quant.uniform>}> {propagated} +// CHECK-NEXT: %[[bias:.*]] = "tfl.dequantize"(%[[qbias]]) +// CHECK-NEXT: %[[in:.*]] = "tfl.dequantize"(%arg0) +// CHECK-NEXT: %[[w:.*]] = "tfl.dequantize"(%arg1) +// CHECK-NEXT: %[[conv:.*]] = "tfl.conv_2d"(%[[in]], %[[w]], %[[bias]]) +// CHECK-NEXT: return %[[conv]] +} + +// ----- + +// CHECK-LABEL: QuantizeConv2D +func.func @QuantizeConv2D(tensor<1x224x224x3x!quant.uniform>) -> tensor<1x112x112x32x!quant.uniform> { +^bb0(%arg0: tensor<1x224x224x3x!quant.uniform>): + %cst = arith.constant dense<-1.23697901> : tensor<32xf32> + %2 = "tfl.dequantize"(%arg0) : (tensor<1x224x224x3x!quant.uniform>) -> tensor<1x224x224x3xf32> + %3 = "tfl.pseudo_qconst"() {qtype = tensor<32x3x3x3x!quant.uniform:f32, 0.021826678373682216:151>>, value = dense<-76> : tensor<32x3x3x3xi8>} : () -> tensor<32x3x3x3x!quant.uniform:f32, 0.021826678373682216:151>> + %4 = "tfl.dequantize"(%3) : (tensor<32x3x3x3x!quant.uniform:f32, 0.021826678373682216:151>>) -> tensor<32x3x3x3xf32> + %5 = "tfl.conv_2d"(%2, %4, %cst) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 2 : i32, stride_w = 2 : i32} : (tensor<1x224x224x3xf32>, tensor<32x3x3x3xf32>, tensor<32xf32>) -> tensor<1x112x112x32xf32> + %6 = "tfl.quantize"(%5) {qtype = tensor<1x112x112x32x!quant.uniform>} : (tensor<1x112x112x32xf32>) -> tensor<1x112x112x32x!quant.uniform> + func.return %6 : tensor<1x112x112x32x!quant.uniform> + +// CHECK: %cst = arith.constant dense<-1.23697901> : tensor<32xf32> +// CHECK: %0 = "tfl.quantize"(%cst) <{qtype = tensor<32x!quant.uniform>}> {propagated} +// CHECK: %1 = "tfl.dequantize"(%0) : (tensor<32x!quant.uniform>) +// CHECK: %2 = "tfl.dequantize"(%arg0) +// CHECK: %3 = "tfl.pseudo_qconst"() +// CHECK: %4 = "tfl.dequantize"(%3) +// CHECK: %5 = "tfl.conv_2d"(%2, %4, %1) +// CHECK: %6 = "tfl.quantize"(%5) +// CHECK: return %6 +} + +// ----- + +// CHECK-LABEL: QuantizeFullyConnected +func.func @QuantizeFullyConnected(tensor<1x224x224x3x!quant.uniform>) -> tensor<1x112x112x32x!quant.uniform> { +^bb0(%arg0: tensor<1x224x224x3x!quant.uniform>): + %cst = arith.constant dense<-1.23697901> : tensor<32xf32> + %2 = "tfl.dequantize"(%arg0) : (tensor<1x224x224x3x!quant.uniform>) -> tensor<1x224x224x3xf32> + %3 = "tfl.pseudo_qconst"() {qtype = tensor<32x3x3x3x!quant.uniform:f32, 0.021826678373682216:151>>, value = dense<-76> : tensor<32x12xi8>} : () -> tensor<32x12x!quant.uniform:f32, 0.021826678373682216:151>> + %4 = "tfl.dequantize"(%3) : (tensor<32x12x!quant.uniform:f32, 0.021826678373682216:151>>) -> tensor<32x12xf32> + %5 = "tfl.fully_connected"(%2, %4, %cst) {fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"} : (tensor<1x224x224x3xf32>, tensor<32x12xf32>, tensor<32xf32>) -> tensor<1x112x112x32xf32> + %6 = "tfl.quantize"(%5) {qtype = tensor<1x112x112x32x!quant.uniform>} : (tensor<1x112x112x32xf32>) -> tensor<1x112x112x32x!quant.uniform> + func.return %6 : tensor<1x112x112x32x!quant.uniform> + +// CHECK: %cst = arith.constant dense<-1.23697901> : tensor<32xf32> +// CHECK: %0 = "tfl.quantize"(%cst) <{qtype = tensor<32x!quant.uniform>}> {propagated} +// CHECK: %1 = "tfl.dequantize"(%0) : (tensor<32x!quant.uniform>) +// CHECK: %2 = "tfl.dequantize"(%arg0) +// CHECK: %3 = "tfl.pseudo_qconst"() +// CHECK: %4 = "tfl.dequantize"(%3) +// CHECK: %5 = "tfl.fully_connected"(%2, %4, %1) +// CHECK: %6 = "tfl.quantize"(%5) +// CHECK: return %6 +} + +// ----- + +// CHECK-LABEL: QuantizeDepthwiseConv2D +func.func @QuantizeDepthwiseConv2D(tensor<1x224x224x3x!quant.uniform>) -> tensor<1x112x112x32x!quant.uniform> { +^bb0(%arg0: tensor<1x224x224x3x!quant.uniform>): + %cst = arith.constant dense<-1.23697901> : tensor<32xf32> + %2 = "tfl.dequantize"(%arg0) : (tensor<1x224x224x3x!quant.uniform>) -> tensor<1x224x224x3xf32> + %3 = "tfl.pseudo_qconst"() {qtype = tensor<32x3x3x3x!quant.uniform:f32, 0.021826678373682216:151>>, value = dense<-76> : tensor<32x3x3x3xi8>} : () -> tensor<32x3x3x3x!quant.uniform:f32, 0.021826678373682216:151>> + %4 = "tfl.dequantize"(%3) : (tensor<32x3x3x3x!quant.uniform:f32, 0.021826678373682216:151>>) -> tensor<32x3x3x3xf32> + %5 = "tfl.depthwise_conv_2d"(%2, %4, %cst) {depth_multiplier = 4 : i32, dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 4 : i32, stride_w = 5 : i32} : (tensor<1x224x224x3xf32>, tensor<32x3x3x3xf32>, tensor<32xf32>) -> tensor<1x112x112x32xf32> + %6 = "tfl.quantize"(%5) {qtype = tensor<1x112x112x32x!quant.uniform>} : (tensor<1x112x112x32xf32>) -> tensor<1x112x112x32x!quant.uniform> + func.return %6 : tensor<1x112x112x32x!quant.uniform> + +// CHECK: %cst = arith.constant dense<-1.23697901> : tensor<32xf32> +// CHECK: %0 = "tfl.quantize"(%cst) <{qtype = tensor<32x!quant.uniform>}> {propagated} +// CHECK: %1 = "tfl.dequantize"(%0) : (tensor<32x!quant.uniform>) +// CHECK: %2 = "tfl.dequantize"(%arg0) +// CHECK: %3 = "tfl.pseudo_qconst"() +// CHECK: %4 = "tfl.dequantize"(%3) +// CHECK: %5 = "tfl.depthwise_conv_2d"(%2, %4, %1) +// CHECK: %6 = "tfl.quantize"(%5) +// CHECK: return %6 +} + +// ----- + +// CHECK-LABEL: QuantizeSharedBiases +func.func @QuantizeSharedBiases( + %arg0: tensor<1x224x224x3x!quant.uniform>, + %arg1: tensor<32x3x3x3x!quant.uniform:f32, 1.0>>, + %arg2: tensor<32x3x3x3x!quant.uniform:f32, 2.0>>) -> (tensor<1x56x56x32x!quant.uniform>) { + %cst = arith.constant dense<1.0> : tensor<32xf32> + %1 = "tfl.dequantize"(%arg0) : (tensor<1x224x224x3x!quant.uniform>) -> tensor<1x224x224x3xf32> + %2 = "tfl.dequantize"(%arg1) : (tensor<32x3x3x3x!quant.uniform:f32, 1.0>>) -> tensor<32x3x3x3xf32> + %conv1 = "tfl.conv_2d"(%1, %2, %cst) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 2 : i32, stride_w = 2 : i32} : (tensor<1x224x224x3xf32>, tensor<32x3x3x3xf32>, tensor<32xf32>) -> tensor<1x112x112x32xf32> + %3 = "tfl.quantize"(%conv1) {qtype = tensor<1x112x112x32xf32>} : (tensor<1x112x112x32xf32>) -> tensor<1x112x112x32x!quant.uniform> + + %4 = "tfl.dequantize"(%3) : (tensor<1x112x112x32x!quant.uniform>) -> tensor<1x112x112x32xf32> + %5 = "tfl.dequantize"(%arg2) : (tensor<32x3x3x3x!quant.uniform:f32, 2.0>>) -> tensor<32x3x3x3xf32> + %conv2 = "tfl.conv_2d"(%4, %5, %cst) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 2 : i32, stride_w = 2 : i32} : (tensor<1x112x112x32xf32>, tensor<32x3x3x3xf32>, tensor<32xf32>) -> tensor<1x56x56x32xf32> + %6 = "tfl.quantize"(%conv2) {qtype = tensor<1x56x56x32x!quant.uniform>} : (tensor<1x56x56x32xf32>) -> tensor<1x56x56x32x!quant.uniform> + + func.return %6 : tensor<1x56x56x32x!quant.uniform> + +// CHECK: %[[CST:.*]] = arith.constant dense<1.000000e+00> : tensor<32xf32> + +// CHECK-DAG: %[[Q1:.*]] = "tfl.quantize"(%[[CST]]) +// CHECK-DAG: %[[DQ1:.*]] = "tfl.dequantize"(%[[Q1]]) : (tensor<32x!quant.uniform>) +// CHECK-DAG: %[[Q2:.*]] = "tfl.quantize"(%[[CST]]) +// CHECK-DAG: %[[DQ2:.*]] = "tfl.dequantize"(%[[Q2]]) : (tensor<32x!quant.uniform>) + +// CHECK-DAG: %{{.*}} = "tfl.conv_2d"(%{{.*}}, %{{.*}}, %[[DQ1]]) +// CHECK-DAG: %{{.*}} = "tfl.conv_2d"(%{{.*}}, %{{.*}}, %[[DQ2]]) +} + +// ----- + +// CHECK-LABEL: QuantizeSharedBiases2 +func.func @QuantizeSharedBiases2( + %arg0: tensor<32x!quant.uniform>, + %arg1: tensor<1x112x112x32x!quant.uniform>, + %arg2: tensor<32x3x3x3x!quant.uniform:f32, 2.0>>) -> (tensor<32x!quant.uniform>, tensor<1x56x56x32x!quant.uniform>) { + %cst = arith.constant dense<1.0> : tensor<32xf32> + %1 = "tfl.dequantize"(%arg0) : (tensor<32x!quant.uniform>) -> tensor<32xf32> + %add = "tfl.add"(%1, %cst) {fused_activation_function = "NONE"} : (tensor<32xf32>, tensor<32xf32>) -> tensor<32xf32> + %3 = "tfl.quantize"(%add) {qtype = tensor<32xf32>} : (tensor<32xf32>) -> tensor<32x!quant.uniform> + + %5 = "tfl.dequantize"(%arg1) : (tensor<1x112x112x32x!quant.uniform>) -> tensor<1x112x112x32xf32> + %6 = "tfl.dequantize"(%arg2) : (tensor<32x3x3x3x!quant.uniform:f32, 2.0>>) -> tensor<32x3x3x3xf32> + %conv2 = "tfl.conv_2d"(%5, %6, %cst) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 2 : i32, stride_w = 2 : i32} : (tensor<1x112x112x32xf32>, tensor<32x3x3x3xf32>, tensor<32xf32>) -> tensor<1x56x56x32xf32> + %7 = "tfl.quantize"(%conv2) {qtype = tensor<1x56x56x32x!quant.uniform>} : (tensor<1x56x56x32xf32>) -> tensor<1x56x56x32x!quant.uniform> + func.return %3, %7 : tensor<32x!quant.uniform>, tensor<1x56x56x32x!quant.uniform> + +// CHECK: %[[CST:.*]] = arith.constant dense<1.000000e+00> : tensor<32xf32> +// CHECK: %[[Q_BIAS:.*]] = "tfl.quantize"(%[[CST]]) <{qtype = tensor<32x!quant.uniform>}> {propagated} : (tensor<32xf32>) -> tensor<32x!quant.uniform> +// CHECK: %[[DQ_BIAS:.*]] = "tfl.dequantize"(%[[Q_BIAS]]) + +// CHECK-DAG: tfl.add {{.*}}, %[[CST]] +// CHECK-DAG: "tfl.conv_2d"({{.*}}, {{.*}}, %[[DQ_BIAS]]) + +} + + +// ----- + +// Make sure biases are not shared. +// CHECK-LABEL: QuantizeSharedBiases3 +func.func @QuantizeSharedBiases3( + %arg0: tensor<32x!quant.uniform>, + %arg1: tensor<1x112x112x32x!quant.uniform>, + %arg2: tensor<32x3x3x3x!quant.uniform:f32, 2.0>>) -> (tensor<32x!quant.uniform>, tensor<1x56x56x32x!quant.uniform>) { + %cst = arith.constant dense<1.0> : tensor<32xf32> + %5 = "tfl.dequantize"(%arg1) : (tensor<1x112x112x32x!quant.uniform>) -> tensor<1x112x112x32xf32> + %6 = "tfl.dequantize"(%arg2) : (tensor<32x3x3x3x!quant.uniform:f32, 2.0>>) -> tensor<32x3x3x3xf32> + %conv2 = "tfl.conv_2d"(%5, %6, %cst) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 2 : i32, stride_w = 2 : i32} : (tensor<1x112x112x32xf32>, tensor<32x3x3x3xf32>, tensor<32xf32>) -> tensor<1x56x56x32xf32> + %7 = "tfl.quantize"(%conv2) {qtype = tensor<1x56x56x32x!quant.uniform>} : (tensor<1x56x56x32xf32>) -> tensor<1x56x56x32x!quant.uniform> + + %1 = "tfl.dequantize"(%arg0) : (tensor<32x!quant.uniform>) -> tensor<32xf32> + %add = "tfl.add"(%1, %cst) {fused_activation_function = "NONE"} : (tensor<32xf32>, tensor<32xf32>) -> tensor<32xf32> + %3 = "tfl.quantize"(%add) {qtype = tensor<32xf32>} : (tensor<32xf32>) -> tensor<32x!quant.uniform> + + func.return %3, %7 : tensor<32x!quant.uniform>, tensor<1x56x56x32x!quant.uniform> + +// CHECK: %[[CST:.*]] = arith.constant dense<1.000000e+00> : tensor<32xf32> +// CHECK: %[[Q_BIAS:.*]] = "tfl.quantize"(%[[CST]]) <{qtype = tensor<32x!quant.uniform>}> +// CHECK: %[[DQ_BIAS:.*]] = "tfl.dequantize"(%[[Q_BIAS]]) +// CHECK-DAG: "tfl.conv_2d"({{.*}}, {{.*}}, %[[DQ_BIAS]]) +// CHECK-DAG: tfl.add {{.*}}, %[[CST]] +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/canonicalize.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/canonicalize.mlir new file mode 100644 index 0000000000000000000000000000000000000000..16fafc116985761c2e43aad4ae069a96f33f54ab --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/canonicalize.mlir @@ -0,0 +1,392 @@ +// RUN: litert-opt -canonicalize=test-convergence -tfl-runtime-verify -split-input-file -verify-diagnostics %s | FileCheck %s + +// CHECK-LABEL: @squeeze_folder +func.func @squeeze_folder(%arg0 : tensor) -> tensor { + %0 = "tfl.squeeze"(%arg0) : (tensor) -> tensor + // CHECK: return %arg0 + func.return %0 : tensor +} + +// ----- + +// CHECK-LABEL: @squeeze_folder +func.func @squeeze_folder(%arg0 : tensor) -> tensor<*xf32> { + %0 = "tfl.squeeze"(%arg0) : (tensor) -> tensor<*xf32> + // CHECK: "tfl.squeeze" + func.return %0 : tensor<*xf32> +} + +// ----- + +// Checks that tfl.reshape shape operand is converted to a vector if it is possible +func.func @reshape_vector_shape(tensor<4x4x4xf32>) -> tensor<16x4xf32> { +^bb0(%arg0: tensor<4x4x4xf32>) : + %shape0 = arith.constant dense<[[16, 4]]> : tensor<1x2xi32> + // expected-error @+1 {{'tfl.reshape' op requires 'shape' to be rank 1, but got 2}} + %1 = "tfl.reshape"(%arg0, %shape0) : (tensor<4x4x4xf32>, tensor<1x2xi32>) -> tensor<16x4xf32> + func.return %1 : tensor<16x4xf32> +} + +// ----- + +// Checks that tfl.reshape should be removed if its output's only user is +// another tfl.reshape +func.func @reshape_removeAdjacent(tensor<4x4x4xf32>) -> tensor<64xf32> { +^bb0(%arg0: tensor<4x4x4xf32>) : + %shape0 = arith.constant dense<[16, 4]> : tensor<2xi32> + %shape1 = arith.constant dense<[64]> : tensor<1xi32> + %0 = "tfl.reshape"(%arg0, %shape0) : (tensor<4x4x4xf32>, tensor<2xi32>) -> tensor<16x4xf32> + %1 = "tfl.reshape"(%0, %shape1) : (tensor<16x4xf32>, tensor<1xi32>) -> tensor<64xf32> + func.return %1 : tensor<64xf32> + +// CHECK-LABEL: func @reshape_removeAdjacent +// CHECK: %[[CST:.*]] = arith.constant dense<64> : tensor<1xi32> +// CHECK: %[[RESHAPE:.*]] = "tfl.reshape"(%arg0, %[[CST]]) : (tensor<4x4x4xf32>, tensor<1xi32>) -> tensor<64xf32> +// CHECK: return %[[RESHAPE]] +} + +// Checks that tfl.reshape should be removed if its output has more than one +// user but all users are tfl.reshape +func.func @reshape_removeAdjacentWithMultipleUse(tensor<4x4x4xf32>) -> tensor<64xf32> { +^bb0(%arg0: tensor<4x4x4xf32>) : + %shape0 = arith.constant dense<[16, 4]> : tensor<2xi32> + %shape1 = arith.constant dense<[64]> : tensor<1xi32> + %0 = "tfl.reshape"(%arg0, %shape0) : (tensor<4x4x4xf32>, tensor<2xi32>) -> tensor<16x4xf32> + %1 = "tfl.reshape"(%0, %shape1) : (tensor<16x4xf32>, tensor<1xi32>) -> tensor<64xf32> + %2 = "tfl.reshape"(%0, %shape1) : (tensor<16x4xf32>, tensor<1xi32>) -> tensor<64xf32> + %3 = arith.addf %1, %2 : tensor<64xf32> + func.return %3 : tensor<64xf32> + +// CHECK-LABEL: func @reshape_removeAdjacentWithMultipleUse +// CHECK: %[[CST:.*]] = arith.constant dense<64> : tensor<1xi32> +// CHECK: %[[RESHAPE_1:.*]] = "tfl.reshape"(%arg0, %[[CST]]) : (tensor<4x4x4xf32>, tensor<1xi32>) -> tensor<64xf32> +// CHECK: %[[RESHAPE_2:.*]] = "tfl.reshape"(%arg0, %[[CST]]) : (tensor<4x4x4xf32>, tensor<1xi32>) -> tensor<64xf32> +// CHECK: %[[RESULT:.*]] = arith.addf %[[RESHAPE_1]], %[[RESHAPE_2]] +// CHECK: return %[[RESULT]] +} + +// Checks that tfl.reshape should be kept if its output has more than one +// user and not all users are tfl.reshape +func.func @reshape_keepAdjacentWithMultipleUse(tensor<4x4x4xf32>) -> (tensor<16x4xf32>, tensor<64xf32>) { +^bb0(%arg0: tensor<4x4x4xf32>) : + %shape0 = arith.constant dense<[16, 4]> : tensor<2xi32> + %shape1 = arith.constant dense<[64]> : tensor<1xi32> + %0 = "tfl.reshape"(%arg0, %shape0) : (tensor<4x4x4xf32>, tensor<2xi32>) -> tensor<16x4xf32> + %1 = "tfl.reshape"(%0, %shape1) : (tensor<16x4xf32>, tensor<1xi32>) -> tensor<64xf32> + func.return %0, %1 : tensor<16x4xf32>, tensor<64xf32> + +// CHECK-LABEL: func @reshape_keepAdjacentWithMultipleUse +// CHECK-DAG: %[[CST:.*]] = arith.constant dense<[16, 4]> : tensor<2xi32> +// CHECK-DAG: %[[CST_0:.*]] = arith.constant dense<64> : tensor<1xi32> +// CHECK: %[[RESHAPE_1:.*]] = "tfl.reshape"(%arg0, %[[CST]]) : (tensor<4x4x4xf32>, tensor<2xi32>) -> tensor<16x4xf32> +// CHECK: %[[RESHAPE_2:.*]] = "tfl.reshape"(%arg0, %[[CST_0]]) : (tensor<4x4x4xf32>, tensor<1xi32>) -> tensor<64xf32> +// CHECK: return %[[RESHAPE_1]], %[[RESHAPE_2]] +} + +// Checks that tfl.reshape should be removed if its output type is the same +// as its input type and both are static. +func.func @reshape_removeIdentity(tensor<4x4x4xf32>) -> tensor<4x4x4xf32> { +^bb0(%arg0: tensor<4x4x4xf32>) : + %cst = arith.constant dense<[4, 4, 4]> : tensor<3xi32> + %0 = "tfl.reshape"(%arg0, %cst) : (tensor<4x4x4xf32>, tensor<3xi32>) -> tensor<4x4x4xf32> + func.return %0 : tensor<4x4x4xf32> + +// CHECK-LABEL: func @reshape_removeIdentity +// CHECK: return %arg0 : tensor<4x4x4xf32> +} + +// Checks that tfl.reshape shouldn't be removed if either output type or input +// type are dynamic. +func.func @reshape_not_removeIdentity(%arg0: tensor, %arg1: tensor<3xi32>) -> tensor { + %0 = "tfl.reshape"(%arg0, %arg1) : (tensor, tensor<3xi32>) -> tensor + func.return %0 : tensor + +// CHECK-LABEL: func @reshape_not_removeIdentity +// CHECK-NEXT: "tfl.reshape" +} + +// ----- + +// CHECK-LABEL: @RemoveRedundantUnpackPack +func.func @RemoveRedundantUnpackPack(%arg0: tensor<2x5xf32>) -> tensor<2x5xf32> { + %0:2 = "tfl.unpack"(%arg0) {axis = 0 : i32, num = 2 : i32} : (tensor<2x5xf32>) -> (tensor<5xf32>, tensor<5xf32>) + %1 = "tfl.pack"(%0#0, %0#1) {axis = 0 : i32, values_count = 2 : i32} : (tensor<5xf32>, tensor<5xf32>) -> (tensor<2x5xf32>) + func.return %1: tensor<2x5xf32> + // CHECK-NOT: pack + // CHECK: return %arg0 : tensor<2x5xf32> +} + +// ----- + +// CHECK-LABEL: @RemoveRedundantPack +func.func @RemoveRedundantPack(%arg0: tensor<2x5xf32>) -> (tensor<2x5xf32>, tensor<5xf32>) { + %0:2 = "tfl.unpack"(%arg0) {axis = 0 : i32, num = 2 : i32} : (tensor<2x5xf32>) -> (tensor<5xf32>, tensor<5xf32>) + %1 = "tfl.pack"(%0#0, %0#1) {axis = 0 : i32, values_count = 2 : i32} : (tensor<5xf32>, tensor<5xf32>) -> (tensor<2x5xf32>) + func.return %1, %0#0: tensor<2x5xf32>, tensor<5xf32> + // CHECK: %[[UNPACK:.*]]:2 = "tfl.unpack" + // CHECK-NOT: pack + // CHECK: return %arg0, %[[UNPACK]]#0 : tensor<2x5xf32>, tensor<5xf32> +} + +// ----- + +// CHECK-LABEL: @ReplacePackWithReshape +func.func @ReplacePackWithReshape(%arg0: tensor<5xf32>) -> tensor<1x5xf32> { + %1 = "tfl.pack"(%arg0) {axis = 0 : i32, values_count = 1 : i32} : (tensor<5xf32>) -> (tensor<1x5xf32>) + // CHECK: reshape + // CHECK-NOT: pack + func.return %1: tensor<1x5xf32> +} + +// ----- + +func.func @Int64SliceBeginSize(%arg0: tensor<4x128x32xf32>) -> tensor<1x128x32xf32> { + %0 = "tfl.pseudo_const"() {value = dense<0> : tensor<3xi64>} : () -> tensor<3xi64> + %1 = "tfl.pseudo_const"() {value = dense<[1, 128, 32]> : tensor<3xi64>} : () -> tensor<3xi64> + %2 = "tfl.slice"(%arg0, %0, %1) : (tensor<4x128x32xf32>, tensor<3xi64>, tensor<3xi64>) -> tensor<1x128x32xf32> + func.return %2 : tensor<1x128x32xf32> + +// CHECK-DAG: [[VAL_1:%.*]] = arith.constant dense<0> : tensor<3xi32> +// CHECK-DAG: [[VAL_2:%.*]] = arith.constant dense<[1, 128, 32]> : tensor<3xi32> +// CHECK: [[VAL_3:%.*]] = "tfl.slice"(%arg0, [[VAL_1]], [[VAL_2]]) : (tensor<4x128x32xf32>, tensor<3xi32>, tensor<3xi32>) -> tensor<1x128x32xf32> +} + +// ----- + +// CHECK-LABEL: @WhileCanonicalizeBug +// Make sure that second output of the tf.while is not incorrectly inferred as +// pass through just because the corresponding input is not used in either +// condition or body. The tensor result of the loop can be either %arg1 +// (if the body never executes, or 22.0 if the body executes at least once). +func.func @WhileCanonicalizeBug(%arg0: tensor, %arg1: tensor) -> tensor { + %0:2 = "tfl.while"(%arg0, %arg1) ({ + ^bb0(%arg2: tensor, %arg3: tensor): + %limit = arith.constant dense<100> : tensor + %test = "tfl.less"(%arg0, %limit) : (tensor, tensor) -> tensor + "tfl.yield"(%test) : (tensor) -> () + }, { + ^bb0(%arg2: tensor, %arg3: tensor): + %cst = arith.constant dense<22.0> : tensor + %stride = arith.constant dense<1> : tensor + %inc = tfl.add %arg2, %stride {fused_activation_function = "NONE"} : tensor + "tfl.yield"(%inc, %cst) : (tensor, tensor) -> () + }) : (tensor, tensor) -> (tensor, tensor) + // CHECK: return %0#1 : tensor + func.return %0#1 : tensor +} + +// ----- + +// Test case to test bug due to checking +// `while_op.getResult(arg_index).use_empty()` instead of +// `while_op.getResult(while_index).use_empty()` in the tfl.while +// canonicalization. +// arg0 is a pass through. After first iteration, arg_index = 0 +// and while_index = 1. Make arg1 use empty in block and condition, but not in +// result. Canonicalize will think it can remove both slot#0 and slot#1 and do +// so without replacing all operands, and in assert builds it will fail an +// assert failure ( op->use_empty() && "expected 'op' to have no uses") +// CHECK-LABEL: WhileCanonicalizeBug1 +func.func @WhileCanonicalizeBug1(%arg0: tensor, %arg1: tensor) -> tensor { + %0:2 = "tfl.while"(%arg0, %arg1) ({ + ^bb0(%carg0: tensor, %carg1: tensor): + %limit = arith.constant dense<100> : tensor + %test = "tfl.less"(%limit, %limit) : (tensor, tensor) -> tensor + "tfl.yield"(%test) : (tensor) -> () + }, { + ^bb0(%barg0: tensor, %barg1: tensor): + %cst = arith.constant dense<22.0> : tensor + "tfl.yield"(%barg0, %cst) : (tensor, tensor) -> () + }) : (tensor, tensor) -> (tensor, tensor) + func.return %0#1 : tensor +} + +// ----- + +// Test case to test While op with resources that are not read-only variables. +// Do not remove resource arugments if they are not read-only variables to keep +// the graph's control dependency. +// CHECK-LABEL: WhileWithNonReadOnlyVariableResources +func.func @WhileWithNonReadOnlyVariableResources(%arg0: tensor) -> tensor { + %0 = "tf.Const"() {value = dense<0.0> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<1.0> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<1> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<2> : tensor} : () -> tensor + %4 = "tf.StackV2"(%3) {elem_type = f32, stack_name = "s"} : (tensor) -> tensor + %5:5 = "tfl.while"(%2, %3, %2, %4, %0) ({ + ^bb0(%arg1: tensor, %arg2: tensor, %arg3: tensor, %arg4: tensor, %arg5: tensor): + %9 = "tf.Const"() {value = dense<10> : tensor} : () -> tensor + %10 = "tf.Less"(%arg3, %9) {device = ""} : (tensor, tensor) -> tensor + "tfl.yield"(%10) : (tensor) -> () + }, { + ^bb0(%arg1: tensor, %arg2: tensor, %arg3: tensor, %arg4: tensor, %arg5: tensor): + %9 = "tf.Const"() {value = dense<1> : tensor} : () -> tensor + %10 = "tf.Cast"(%arg3) {Truncate = false, device = ""} : (tensor) -> tensor + %11 = "tf.AddV2"(%arg3, %9) {device = ""} : (tensor, tensor) -> tensor + %12 = "tf.StackPushV2"(%arg4, %10) {device = "", swap_memory = false} : (tensor, tensor) -> tensor + %13 = "tf.AddV2"(%arg1, %9) {device = ""} : (tensor, tensor) -> tensor + "tfl.yield"(%13, %arg2, %11, %arg4, %12) : (tensor, tensor, tensor, tensor, tensor) -> () + }) {is_stateless = false} : (tensor, tensor, tensor, tensor, tensor) -> (tensor, tensor, tensor, tensor, tensor) + func.return %5#3 : tensor + +// CHECK: "tfl.while" +// CHECK: (tensor, tensor, tensor) -> (tensor, tensor, tensor) +} + +// CHECK-LABEL: @RemoveFcZeroBias +func.func @RemoveFcZeroBias(%arg0: tensor<1x37xf32>, %arg1: tensor<40x37xf32>) -> tensor<1x40xf32> { + %0 = "tfl.pseudo_const"() {value = dense<0.0> : tensor<40xf32>} : () -> tensor<40xf32> + %1 = "tfl.fully_connected"(%arg0, %arg1, %0) {fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"} : (tensor<1x37xf32>, tensor<40x37xf32>, tensor<40xf32>) -> tensor<1x40xf32> +// CHECK: "tfl.fully_connected" +// CHECK-SAME: (tensor<1x37xf32>, tensor<40x37xf32>, none) -> tensor<1x40xf32> + func.return %1 : tensor<1x40xf32> +} + +// CHECK-LABEL: RemoveLstmQuantZeroBias +func.func @RemoveLstmQuantZeroBias( + %arg0: tensor<1x528xf32>, + %arg1: tensor<2048x528xf32>, + %arg2: tensor<2048x528xf32>, + %arg3: tensor<2048x528xf32>, + %arg4: tensor<2048x528xf32>, + %arg5: tensor<2048x640xf32>, + %arg6: tensor<2048x640xf32>, + %arg7: tensor<2048x640xf32>, + %arg8: tensor<2048x640xf32>, + %arg9: tensor<2048xf32>, + %arg10: tensor<2048xf32>, + %arg11: tensor<2048xf32>, + %arg12: tensor<2048xf32>, + %arg13: tensor<640x2048xf32>, + %arg14: tensor<640xf32>, + %arg15: tensor<2048xf32>, + %arg16: tensor<2048xf32>, + %arg17: tensor<2048xf32>, + %arg18: tensor<2048xf32>, + %arg19: tensor<1x640xf32>, + %arg20: tensor<1x2048xf32> +) -> tensor<1x640xf32> { + %cst = "tfl.no_value"() {value = unit} : () -> none + %zero = "tfl.pseudo_const"() {value = dense<0.0> : tensor<640xf32>} : () -> tensor<640xf32> + %0 = "tfl.lstm"(%arg0, %arg1, %arg2, %arg3, %arg4, %arg5, %arg6, %arg7, %arg8, %cst, %cst, %cst, %arg9, %arg10, %arg11, %arg12, %arg13, %zero, %arg19, %arg20, %arg15, %arg16, %arg17, %arg18) ({}) { + cell_clip = 1.000000e+01 : f32, fused_activation_function = "TANH", kernel_type = #tfl, proj_clip = 0.01 : f32 + } : (tensor<1x528xf32>, tensor<2048x528xf32>, tensor<2048x528xf32>, tensor<2048x528xf32>, tensor<2048x528xf32>, tensor<2048x640xf32>, tensor<2048x640xf32>, tensor<2048x640xf32>, tensor<2048x640xf32>, none, none, none, tensor<2048xf32>, tensor<2048xf32>, tensor<2048xf32>, tensor<2048xf32>, tensor<640x2048xf32>, tensor<640xf32>, tensor<1x640xf32>, tensor<1x2048xf32>, tensor<2048xf32>, tensor<2048xf32>, tensor<2048xf32>, tensor<2048xf32>) -> tensor<1x640xf32> + func.return %0 : tensor<1x640xf32> +// CHECK: %[[NONE:.+]] = "tfl.no_value"() <{value}> : () -> none +// CHECK: "tfl.lstm"(%arg0, %arg1, %arg2, %arg3, %arg4, %arg5, %arg6, %arg7, %arg8, %[[NONE]], %[[NONE]], %[[NONE]], %arg9, %arg10, %arg11, %arg12, %arg13, %[[NONE]], %arg19, %arg20, %arg15, %arg16, %arg17, %arg18) +} + +func.func @keepCustomFlexOps(%arg0: tensor<1x10xf32>) -> tensor<1x10xf32> { + %0 = "tfl.custom"() {custom_code = "FlexVarHandleOp", custom_option = #tfl} : () -> tensor>> + %1 = "tfl.custom"(%0) {custom_code = "FlexReadVariableOp", custom_option = #tfl} : (tensor>>) -> tensor<1x10xf32> + %2 = "tfl.custom"(%1, %arg0) {custom_code = "FlexAddV2", custom_option = #tfl} : (tensor<1x10xf32>, tensor<1x10xf32>) -> tensor<1x10xf32> + "tfl.custom"(%0, %2) {custom_code = "FlexAssignVariableOp", custom_option = #tfl} : (tensor>>, tensor<1x10xf32>) -> () + %3 = "tfl.custom"(%0) {custom_code = "FlexReadVariableOp", custom_option = #tfl} : (tensor>>) -> tensor<1x10xf32> + // CHECK: %0 = "tfl.custom"() <{custom_code = "FlexVarHandleOp" + // CHECK-NEXT: %1 = "tfl.custom"(%0) <{custom_code = "FlexReadVariableOp" + // CHECK-NEXT: %2 = "tfl.custom"(%1, %arg0) <{custom_code = "FlexAddV2" + // CHECK-NEXT: "tfl.custom"(%0, %2) <{custom_code = "FlexAssignVariableOp" + // CHECK-NEXT: %3 = "tfl.custom"(%0) <{custom_code = "FlexReadVariableOp" + func.return %3 : tensor<1x10xf32> +} + +// ----- + +// Converts tfl.broadcast_to to tfl.reshape if input and output have the same +// number of elements. +// CHECK-LABEL: broadcast_to_to_reshape +func.func @broadcast_to_to_reshape(%arg0: tensor<4x4x4xf32>, %arg1 : tensor<4xi32>) -> tensor<1x4x4x4xf32> { + %0 = "tfl.broadcast_to"(%arg0, %arg1) : (tensor<4x4x4xf32>, tensor<4xi32>) -> tensor<1x4x4x4xf32> + // CHECK: "tfl.reshape" + // CHECK-SAME: (tensor<4x4x4xf32>, tensor<4xi32>) -> tensor<1x4x4x4xf32> + func.return %0 : tensor<1x4x4x4xf32> +} + +// Converts tfl.broadcast_to to tfl.reshape if input and output have the same +// number of elements. +// CHECK-LABEL: @broadcast_to_to_reshape_i64 +func.func @broadcast_to_to_reshape_i64(%arg0: tensor<4x4x4xf32>, %arg1 : tensor<4xi64>) -> tensor<1x4x4x4xf32> { + %0 = "tfl.broadcast_to"(%arg0, %arg1) : (tensor<4x4x4xf32>, tensor<4xi64>) -> tensor<1x4x4x4xf32> + // CHECK: "tfl.cast" + // CHECK-SAME: (tensor<4xi64>) -> tensor<4xi32> + // CHECK-NEXT: "tfl.reshape" + // CHECK-SAME: (tensor<4x4x4xf32>, tensor<4xi32>) -> tensor<1x4x4x4xf32> + func.return %0 : tensor<1x4x4x4xf32> +} + + +// Converts tfl.broadcast_to to tfl.reshape if input and output have the same +// number of elements. +// CHECK-LABEL: @broadcast_to_to_reshape_i64_const +func.func @broadcast_to_to_reshape_i64_const(%arg0: tensor<4x4x4xf32>) -> tensor<1x4x4x4xf32> { + %cst = arith.constant dense<[1, 4, 4, 4]> : tensor<4xi64> + %0 = "tfl.broadcast_to"(%arg0, %cst) : (tensor<4x4x4xf32>, tensor<4xi64>) -> tensor<1x4x4x4xf32> + // CHECK: arith.constant dense<[1, 4, 4, 4]> : tensor<4xi32> + // CHECK-NEXT: "tfl.reshape" + // CHECK-SAME: (tensor<4x4x4xf32>, tensor<4xi32>) -> tensor<1x4x4x4xf32> + func.return %0 : tensor<1x4x4x4xf32> +} + +// ----- + +// CHECK-LABEL: @trivial_dynamic_update_slice +func.func @trivial_dynamic_update_slice(%arg0: tensor<2x7x14xf32>, %arg1: tensor<2x7x14xf32>) -> tensor<2x7x14xf32> { + %0 = arith.constant dense<0> : tensor<3xi32> + %1 = "tfl.dynamic_update_slice"(%arg0, %arg1, %0) : (tensor<2x7x14xf32>, tensor<2x7x14xf32>, tensor<3xi32>) -> tensor<2x7x14xf32> + // CHECK: return %arg1 + func.return %1 : tensor<2x7x14xf32> +} + +// ----- + +// CHECK-LABEL: @trivial_dynamic_update_slice_wrong_update_shape +func.func @trivial_dynamic_update_slice_wrong_update_shape(%arg0: tensor<2x7x14xf32>, %arg1: tensor<2x7x7xf32>) -> tensor<2x7x14xf32> { + %0 = arith.constant dense<0> : tensor<3xi32> + %1 = "tfl.dynamic_update_slice"(%arg0, %arg1, %0) : (tensor<2x7x14xf32>, tensor<2x7x7xf32>, tensor<3xi32>) -> tensor<2x7x14xf32> + // CHECK: "tfl.dynamic_update_slice" + func.return %1 : tensor<2x7x14xf32> +} + +// CHECK-LABEL: OptimizeTranposeWithRank7orMoreEffectiveRank6 +func.func @OptimizeTranposeWithRank7orMoreEffectiveRank6(%arg0: tensor<7x6x5x4x3x2x1xf32> ) -> (tensor<1x2x3x4x5x6x7xf32>) { + %cst = arith.constant dense<[6, 5, 4, 3, 2, 1, 0]> : tensor<7xi32> + %0 = "tfl.transpose"(%arg0, %cst) : (tensor<7x6x5x4x3x2x1xf32>, tensor<7xi32>) -> tensor<1x2x3x4x5x6x7xf32> + return %0 : tensor<1x2x3x4x5x6x7xf32> + // CHECK-DAG: %[[cst:.*]] = arith.constant dense<[7, 6, 5, 4, 3, 2]> : tensor<6xi32> + // CHECK-DAG: %[[cst_0:.*]] = arith.constant dense<[5, 4, 3, 2, 1, 0]> : tensor<6xi32> + // CHECK-DAG: %[[cst_1:.*]] = arith.constant dense<[1, 2, 3, 4, 5, 6, 7]> : tensor<7xi32> + // CHECK: %0 = "tfl.reshape"(%arg0, %[[cst]]) : (tensor<7x6x5x4x3x2x1xf32>, tensor<6xi32>) -> tensor<7x6x5x4x3x2xf32> + // CHECK: %1 = "tfl.transpose"(%0, %[[cst_0]]) : (tensor<7x6x5x4x3x2xf32>, tensor<6xi32>) -> tensor<2x3x4x5x6x7xf32> + // CHECK: %2 = "tfl.reshape"(%1, %[[cst_1]]) : (tensor<2x3x4x5x6x7xf32>, tensor<7xi32>) -> tensor<1x2x3x4x5x6x7xf32> + // CHECK: return %2 +} + +// CHECK-LABEL: OptimizeTranposeWithRank7orMoreEffectiveRank4 +func.func @OptimizeTranposeWithRank7orMoreEffectiveRank4(%arg0: tensor<56x8x56x1x1x1x7xf32> ) -> (tensor<1x1x8x56x56x7x1xf32>) { + %cst = arith.constant dense<[4, 5, 1, 2, 0, 6, 3]> : tensor<7xi32> + %0 = "tfl.transpose"(%arg0, %cst) : (tensor<56x8x56x1x1x1x7xf32>, tensor<7xi32>) -> tensor<1x1x8x56x56x7x1xf32> + return %0 : tensor<1x1x8x56x56x7x1xf32> + // CHECK-DAG: %[[cst:.*]] = arith.constant dense<[56, 8, 56, 7]> : tensor<4xi32> + // CHECK-DAG: %[[cst_0:.*]] = arith.constant dense<[1, 2, 0, 3]> : tensor<4xi32> + // CHECK-DAG: %[[cst_1:.*]] = arith.constant dense<[1, 1, 8, 56, 56, 7, 1]> : tensor<7xi32> + // CHECK: %0 = "tfl.reshape"(%arg0, %[[cst]]) : (tensor<56x8x56x1x1x1x7xf32>, tensor<4xi32>) -> tensor<56x8x56x7xf32> + // CHECK: %1 = "tfl.transpose"(%0, %[[cst_0]]) : (tensor<56x8x56x7xf32>, tensor<4xi32>) -> tensor<8x56x56x7xf32> + // CHECK: %2 = "tfl.reshape"(%1, %[[cst_1]]) : (tensor<8x56x56x7xf32>, tensor<7xi32>) -> tensor<1x1x8x56x56x7x1xf32> + // CHECK: return %2 +} + +// CHECK-LABEL: @ConstPadToI32 +func.func @ConstPadToI32(%arg0: tensor<15600xf32>) -> tensor<15602xf32> { + %0 = "tfl.pseudo_const"() {value = dense<1> : tensor<1x2xi64>} : () -> tensor<1x2xi64> + %1 = "tfl.pad"(%arg0, %0) : (tensor<15600xf32>, tensor<1x2xi64>) -> tensor<15602xf32> + func.return %1 : tensor<15602xf32> + // CHECK: "tfl.pad"(%arg0, %cst) : (tensor<15600xf32>, tensor<1x2xi32>) -> tensor<15602xf32> +} + +// CHECK-LABEL: @RemoveNoopTranspose +func.func @RemoveNoopTranspose(%arg0: tensor<1x2x3x4xf32>) -> tensor<1x2x3x4xf32> { + %cst = arith.constant dense<[0, 1, 2, 3]> : tensor<4xi32> + %0 = "tfl.transpose"(%arg0, %cst) : (tensor<1x2x3x4xf32>, tensor<4xi32>) -> tensor<1x2x3x4xf32> + func.return %0 : tensor<1x2x3x4xf32> + // CHECK: return %arg0 +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/canonicalize_boundary_value.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/canonicalize_boundary_value.mlir new file mode 100644 index 0000000000000000000000000000000000000000..a8c6ed6f8efe982b30e773d017f928cf87d40fcc --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/canonicalize_boundary_value.mlir @@ -0,0 +1,91 @@ +// RUN: litert-opt %s --tfl-canonicalize-boundary-value --split-input-file | FileCheck %s + +// CHECK-LABEL: func.func @clamp_neg_inf_f32() -> tensor { +// CHECK: %[[CONST:.*]] = stablehlo.constant dense<-3.40282347E+38> : tensor +// CHECK: return %[[CONST]] : tensor + +func.func @clamp_neg_inf_f32() -> tensor { + %ret = stablehlo.constant dense<0xFF800000> : tensor + return %ret : tensor +} + +// ----- + +// CHECK-LABEL: func.func @clamp_pos_inf_f32() -> tensor { +// CHECK: %[[CONST:.*]] = stablehlo.constant dense<3.40282347E+38> : tensor +// CHECK: return %[[CONST]] : tensor +func.func @clamp_pos_inf_f32() -> tensor { + %ret = stablehlo.constant dense<0x7F800000> : tensor + return %ret : tensor +} + +// ----- + +// CHECK-LABEL: func.func @clamp_pos_inf_f32_tensor() -> tensor<1x4xf32> { +// CHECK: %[[CONST:.*]] = stablehlo.constant dense<{{\[\[}}3.40282347E+38, 1.000000e+01, 2.000000e+01, -3.40282347E+38]]> : tensor<1x4xf32> +// CHECK: return %[[CONST]] : tensor<1x4xf32> +func.func @clamp_pos_inf_f32_tensor() -> tensor<1x4xf32> { + %ret = stablehlo.constant dense<[[0x7F800000, 10.0, 20.0, 0xFF800000]]> : tensor<1x4xf32> + return %ret : tensor<1x4xf32> +} + +// ----- + +// CHECK-LABEL: func.func @clamp_neg_inf_f16() -> tensor { +// CHECK: %[[CONST:.*]] = stablehlo.constant dense<-6.550400e+04> : tensor +// CHECK: return %[[CONST]] : tensor +func.func @clamp_neg_inf_f16() -> tensor { + %ret = stablehlo.constant dense<0xFC00> : tensor + return %ret : tensor +} +// ----- + +// CHECK-LABEL: func.func @clamp_neg_inf_bf16() -> tensor { +// CHECK: %[[CONST:.*]] = stablehlo.constant dense<-1.038460e+34> : tensor +// CHECK: return %[[CONST]] : tensor +func.func @clamp_neg_inf_bf16() -> tensor { + %ret = stablehlo.constant dense<0xF800> : tensor + return %ret : tensor +} + +// ----- + +// CHECK-LABEL: func.func @clamp_pos_inf_f16() -> tensor { +// CHECK: %[[CONST:.*]] = stablehlo.constant dense<6.550400e+04> : tensor +// CHECK: return %[[CONST]] : tensor +func.func @clamp_pos_inf_f16() -> tensor { + %ret = stablehlo.constant dense<0x7C00> : tensor + return %ret : tensor +} + +// ----- + +// CHECK-LABEL: func.func @clamp_pos_inf_f16_tensor() -> tensor<1x4xf16> { +// CHECK: %[[CONST:.*]] = stablehlo.constant dense<{{\[\[}}6.550400e+04, 1.000000e+01, 2.000000e+01, -6.550400e+04]]> : tensor<1x4xf16> +// CHECK: return %[[CONST]] : tensor<1x4xf16> +func.func @clamp_pos_inf_f16_tensor() -> tensor<1x4xf16> { + %ret = stablehlo.constant dense<[[0x7C00, 10.0, 20.0, 0xFC00]]> : tensor<1x4xf16> + return %ret : tensor<1x4xf16> +} + +// ----- + +// CHECK-LABEL: func.func @clamp_pos_inf_f16_tensor_tf_const() -> tensor<3xf16> { +// CHECK: %[[CONST:.*]] = "tf.Const"() <{value = dense<6.550400e+04> : tensor<3xf16>}> : () -> tensor<3xf16> +// CHECK: return %[[CONST]] : tensor<3xf16> +func.func @clamp_pos_inf_f16_tensor_tf_const() -> tensor<3xf16> { + %ret = "tf.Const"() <{value = dense<0x7C00> : tensor<3xf16>}> : () -> tensor<3xf16> + return %ret : tensor<3xf16> +} + + +// ----- + +// CHECK-LABEL: func.func @clamp_pos_inf_f16_arith_op() -> f16 { +// CHECK: %[[CONST:.*]] = arith.constant 6.550400e+04 : f16 +// CHECK: return %[[CONST]] : f16 +func.func @clamp_pos_inf_f16_arith_op() -> f16 { + %ret = arith.constant 0x7C00 : f16 + return %ret : f16 +} + diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/cleanup_optimization_barrier.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/cleanup_optimization_barrier.mlir new file mode 100644 index 0000000000000000000000000000000000000000..dd09a20e6acdfbe89ab0cf7e97b83e8ad53f6666 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/cleanup_optimization_barrier.mlir @@ -0,0 +1,14 @@ +// RUN: litert-opt %s --tfl-cleanup-optimization-barrier --split-input-file | FileCheck %s + +// CHECK-LABEL: func.func @cleanup_barrier(%arg0: tensor<2x2xf32>) -> tensor<2x2xf32> { +// CHECK: %0 = tfl.add(%arg0, %cst) <{fused_activation_function = "NONE"}> : (tensor<2x2xf32>, tensor) -> tensor<2x2xf32> +// CHECK: %1 = tfl.add(%0, %cst) <{fused_activation_function = "NONE"}> : (tensor<2x2xf32>, tensor) -> tensor<2x2xf32> +// CHECK: return %1 : tensor<2x2xf32> + +func.func @cleanup_barrier(%arg0: tensor<2x2xf32>) -> tensor<2x2xf32> { + %cst = arith.constant dense<5.000000e+00> : tensor + %0 = tfl.add(%arg0, %cst) <{fused_activation_function = "NONE"}> : (tensor<2x2xf32>, tensor) -> tensor<2x2xf32> + %1 = stablehlo.optimization_barrier %0 : tensor<2x2xf32> + %2 = tfl.add(%1, %cst) <{fused_activation_function = "NONE"}> : (tensor<2x2xf32>, tensor) -> tensor<2x2xf32> + return %2 : tensor<2x2xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/const-fold-binary-ops.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/const-fold-binary-ops.mlir new file mode 100644 index 0000000000000000000000000000000000000000..38e8131c7be0e63f97031bf703cc1779c7eef442 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/const-fold-binary-ops.mlir @@ -0,0 +1,763 @@ +// RUN: litert-opt %s -canonicalize | env FILECHECK_OPTS="" FileCheck %s +// TODO(b/493282789): Enable DenseResourceElementsAttr constant folding with +// more peformant folder implementations. + +// CHECK-LABEL: @add_float +func.func @add_float() -> (tensor, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>) { + %0 = arith.constant dense<4.5> : tensor + %1 = arith.constant dense<1.5> : tensor + + %2 = arith.constant dense< 3.5> : tensor<4xf32> + %3 = arith.constant dense<-0.5> : tensor<4xf32> + + // CHECK-DAG: %[[CST:.*]] = arith.constant dense<3.500000e+00> : tensor<4xf32> + // CHECK-DAG: %[[CST_0:.*]] = arith.constant dense<-5.000000e-01> : tensor<4xf32> + // CHECK-DAG: %[[CST_1:.*]] = arith.constant dense<6.000000e+00> : tensor + // CHECK-DAG: %[[CST_2:.*]] = arith.constant dense<4.000000e+00> : tensor<4xf32> + // CHECK-DAG: %[[CST_3:.*]] = arith.constant dense<5.000000e+00> : tensor<4xf32> + // CHECK-DAG: %[[CST_4:.*]] = arith.constant dense<3.000000e+00> : tensor<4xf32> + // CHECK: %0 = tfl.add %[[CST]], %[[CST_0]] {fused_activation_function = "SIGN_BIT"} : tensor<4xf32> + + %5 = "tfl.add"(%0, %1) {fused_activation_function = "NONE"} : (tensor< f32>, tensor< f32>) -> tensor< f32> + %6 = "tfl.add"(%0, %3) {fused_activation_function = "NONE"} : (tensor< f32>, tensor<4xf32>) -> tensor<4xf32> + %7 = "tfl.add"(%2, %1) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor< f32>) -> tensor<4xf32> + %8 = "tfl.add"(%2, %3) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> + %9 = "tfl.add"(%2, %3) {fused_activation_function = "SIGN_BIT"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> + + func.return %5, %6, %7, %8, %9 : tensor, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32> +} + +// CHECK-LABEL: @add_int +func.func @add_int() -> (tensor, tensor<4xi32>, tensor<4xi32>, tensor<4xi32>) { + %0 = arith.constant dense<8> : tensor + %1 = arith.constant dense<1> : tensor + + %2 = arith.constant dense< 4> : tensor<4xi32> + %3 = arith.constant dense<-2> : tensor<4xi32> + + // CHECK-DAG: %[[CST:.*]] = arith.constant dense<9> : tensor + // CHECK-DAG: %[[CST_0:.*]] = arith.constant dense<6> : tensor<4xi32> + // CHECK-DAG: %[[CST_1:.*]] = arith.constant dense<5> : tensor<4xi32> + // CHECK-DAG: %[[CST_2:.*]] = arith.constant dense<2> : tensor<4xi32> + + %5 = "tfl.add"(%0, %1) {fused_activation_function = "NONE"} : (tensor< i32>, tensor< i32>) -> tensor< i32> + %6 = "tfl.add"(%0, %3) {fused_activation_function = "NONE"} : (tensor< i32>, tensor<4xi32>) -> tensor<4xi32> + %7 = "tfl.add"(%2, %1) {fused_activation_function = "NONE"} : (tensor<4xi32>, tensor< i32>) -> tensor<4xi32> + %8 = "tfl.add"(%2, %3) {fused_activation_function = "NONE"} : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi32> + + func.return %5, %6, %7, %8 : tensor, tensor<4xi32>, tensor<4xi32>, tensor<4xi32> +} + +// CHECK-LABEL: @sub_float +func.func @sub_float() -> (tensor, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>) { + %0 = arith.constant dense<4.5> : tensor + %1 = arith.constant dense<1.5> : tensor + + %2 = arith.constant dense< 3.5> : tensor<4xf32> + %3 = arith.constant dense<-0.5> : tensor<4xf32> + + // CHECK-DAG: %[[CST:.*]] = arith.constant dense<3.000000e+00> : tensor + // CHECK-DAG: %[[CST_0:.*]] = arith.constant dense<5.000000e+00> : tensor<4xf32> + // CHECK-DAG: %[[CST_1:.*]] = arith.constant dense<2.000000e+00> : tensor<4xf32> + // CHECK-DAG: %[[CST_2:.*]] = arith.constant dense<4.000000e+00> : tensor<4xf32> + + %5 = "tfl.sub"(%0, %1) {fused_activation_function = "NONE"} : (tensor< f32>, tensor< f32>) -> tensor< f32> + %6 = "tfl.sub"(%0, %3) {fused_activation_function = "NONE"} : (tensor< f32>, tensor<4xf32>) -> tensor<4xf32> + %7 = "tfl.sub"(%2, %1) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor< f32>) -> tensor<4xf32> + %8 = "tfl.sub"(%2, %3) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> + + func.return %5, %6, %7, %8 : tensor, tensor<4xf32>, tensor<4xf32>, tensor<4xf32> +} + +// CHECK-LABEL: @sub_int +func.func @sub_int() -> (tensor, tensor<4xi32>, tensor<4xi32>, tensor<4xi32>) { + %0 = arith.constant dense<8> : tensor + %1 = arith.constant dense<1> : tensor + + %2 = arith.constant dense< 4> : tensor<4xi32> + %3 = arith.constant dense<-2> : tensor<4xi32> + + // CHECK-DAG: %[[CST:.*]] = arith.constant dense<7> : tensor + // CHECK-DAG: %[[CST_0:.*]] = arith.constant dense<10> : tensor<4xi32> + // CHECK-DAG: %[[CST_1:.*]] = arith.constant dense<3> : tensor<4xi32> + // CHECK-DAG: %[[CST_2:.*]] = arith.constant dense<6> : tensor<4xi32> + + %5 = "tfl.sub"(%0, %1) {fused_activation_function = "NONE"} : (tensor< i32>, tensor< i32>) -> tensor< i32> + %6 = "tfl.sub"(%0, %3) {fused_activation_function = "NONE"} : (tensor< i32>, tensor<4xi32>) -> tensor<4xi32> + %7 = "tfl.sub"(%2, %1) {fused_activation_function = "NONE"} : (tensor<4xi32>, tensor< i32>) -> tensor<4xi32> + %8 = "tfl.sub"(%2, %3) {fused_activation_function = "NONE"} : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi32> + + func.return %5, %6, %7, %8 : tensor, tensor<4xi32>, tensor<4xi32>, tensor<4xi32> +} + +// CHECK-LABEL: @sub_zero +func.func @sub_zero(%arg0: tensor<4xi32>, %arg1: tensor<4xf32>) -> (tensor<4xi32>, tensor<4xf32>) { + %zero_int = arith.constant dense<0> : tensor<4xi32> + %zero_float = arith.constant dense<0.0> : tensor<4xf32> + + // CHECK-NOT: tfl.sub + // CHECK: return %arg0, %arg1 + + %0 = "tfl.sub"(%arg0, %zero_int) {fused_activation_function = "NONE"} : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi32> + %1 = "tfl.sub"(%arg1, %zero_float) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> + + func.return %0, %1 : tensor<4xi32>, tensor<4xf32> +} + +// CHECK-LABEL: @sub_zero_lhs +func.func @sub_zero_lhs(%arg0: tensor<4xi32>, %arg1: tensor<4xf32>) -> (tensor<4xi32>, tensor<4xf32>) { + %zero_int = arith.constant dense<0> : tensor<4xi32> + %zero_float = arith.constant dense<0.0> : tensor<4xf32> + + // CHECK: tfl.sub + // CHECK: return %0, %1 + + %0 = "tfl.sub"(%zero_int, %arg0) {fused_activation_function = "NONE"} : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi32> + %1 = "tfl.sub"(%zero_float, %arg1) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> + + func.return %0, %1 : tensor<4xi32>, tensor<4xf32> +} + +// CHECK-LABEL: @sub_zero_quant +func.func @sub_zero_quant(%arg0: tensor<32x!quant.uniform>) -> tensor<32x!quant.uniform> { + %zero = "tfl.pseudo_qconst"() {qtype = tensor<32x!quant.uniform>, value = dense<0> : tensor<32xi8>} : () -> tensor<32x!quant.uniform> + + // CHECK: %[[SUB:.*]] = tfl.sub + // CHECK: return %[[SUB]] + + %0 = "tfl.sub"(%arg0, %zero) {fused_activation_function = "NONE"} : (tensor<32x!quant.uniform>, tensor<32x!quant.uniform>) -> tensor<32x!quant.uniform> + + func.return %0 : tensor<32x!quant.uniform> +} + +// CHECK-LABEL: @mul_float +func.func @mul_float() -> (tensor, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>) { + %0 = arith.constant dense<4.5> : tensor + %1 = arith.constant dense<1.5> : tensor + + %2 = arith.constant dense< 3.5> : tensor<4xf32> + %3 = arith.constant dense<-0.5> : tensor<4xf32> + + // CHECK-DAG: %[[CST:.*]] = arith.constant dense<6.750000e+00> : tensor + // CHECK-DAG: %[[CST_0:.*]] = arith.constant dense<-2.250000e+00> : tensor<4xf32> + // CHECK-DAG: %[[CST_1:.*]] = arith.constant dense<5.250000e+00> : tensor<4xf32> + // CHECK-DAG: %[[CST_2:.*]] = arith.constant dense<-1.750000e+00> : tensor<4xf32> + + %5 = "tfl.mul"(%0, %1) {fused_activation_function = "NONE"} : (tensor< f32>, tensor< f32>) -> tensor< f32> + %6 = "tfl.mul"(%0, %3) {fused_activation_function = "NONE"} : (tensor< f32>, tensor<4xf32>) -> tensor<4xf32> + %7 = "tfl.mul"(%2, %1) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor< f32>) -> tensor<4xf32> + %8 = "tfl.mul"(%2, %3) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> + + func.return %5, %6, %7, %8 : tensor, tensor<4xf32>, tensor<4xf32>, tensor<4xf32> +} + +// CHECK-LABEL: @mul_bf16 +func.func @mul_bf16() -> (tensor, tensor<4xbf16>, tensor<4xbf16>, tensor<4xbf16>) { + %0 = arith.constant dense<4.5> : tensor + %1 = arith.constant dense<1.5> : tensor + + %2 = arith.constant dense< 3.5> : tensor<4xbf16> + %3 = arith.constant dense<-0.5> : tensor<4xbf16> + + // CHECK-DAG: %[[CST:.*]] = arith.constant dense<6.750000e+00> : tensor + // CHECK-DAG: %[[CST_0:.*]] = arith.constant dense<-2.250000e+00> : tensor<4xbf16> + // CHECK-DAG: %[[CST_1:.*]] = arith.constant dense<5.250000e+00> : tensor<4xbf16> + // CHECK-DAG: %[[CST_2:.*]] = arith.constant dense<-1.750000e+00> : tensor<4xbf16> + + %5 = "tfl.mul"(%0, %1) {fused_activation_function = "NONE"} : (tensor< bf16>, tensor< bf16>) -> tensor< bf16> + %6 = "tfl.mul"(%0, %3) {fused_activation_function = "NONE"} : (tensor< bf16>, tensor<4xbf16>) -> tensor<4xbf16> + %7 = "tfl.mul"(%2, %1) {fused_activation_function = "NONE"} : (tensor<4xbf16>, tensor< bf16>) -> tensor<4xbf16> + %8 = "tfl.mul"(%2, %3) {fused_activation_function = "NONE"} : (tensor<4xbf16>, tensor<4xbf16>) -> tensor<4xbf16> + + func.return %5, %6, %7, %8 : tensor, tensor<4xbf16>, tensor<4xbf16>, tensor<4xbf16> +} + +// CHECK-LABEL: @mul_f16 +func.func @mul_f16() -> (tensor, tensor<4xf16>, tensor<4xf16>, tensor<4xf16>) { + %0 = arith.constant dense<4.5> : tensor + %1 = arith.constant dense<1.5> : tensor + + %2 = arith.constant dense< 3.5> : tensor<4xf16> + %3 = arith.constant dense<-0.5> : tensor<4xf16> + + // CHECK-DAG: %[[CST:.*]] = arith.constant dense<6.750000e+00> : tensor + // CHECK-DAG: %[[CST_0:.*]] = arith.constant dense<-2.250000e+00> : tensor<4xf16> + // CHECK-DAG: %[[CST_1:.*]] = arith.constant dense<5.250000e+00> : tensor<4xf16> + // CHECK-DAG: %[[CST_2:.*]] = arith.constant dense<-1.750000e+00> : tensor<4xf16> + + %5 = "tfl.mul"(%0, %1) {fused_activation_function = "NONE"} : (tensor< f16>, tensor< f16>) -> tensor< f16> + %6 = "tfl.mul"(%0, %3) {fused_activation_function = "NONE"} : (tensor< f16>, tensor<4xf16>) -> tensor<4xf16> + %7 = "tfl.mul"(%2, %1) {fused_activation_function = "NONE"} : (tensor<4xf16>, tensor< f16>) -> tensor<4xf16> + %8 = "tfl.mul"(%2, %3) {fused_activation_function = "NONE"} : (tensor<4xf16>, tensor<4xf16>) -> tensor<4xf16> + + func.return %5, %6, %7, %8 : tensor, tensor<4xf16>, tensor<4xf16>, tensor<4xf16> +} + +// CHECK-LABEL: @mul_zero +func.func @mul_zero(%arg0: tensor<4xi32>, %arg1: tensor<4xf32>) -> (tensor<4xi32>, tensor<4xf32>) { + %zero_int = arith.constant dense<0> : tensor<4xi32> + %zero_float = arith.constant dense<0.0> : tensor<4xf32> + + // CHECK-NOT: tfl.mul + // CHECK: return %cst, %cst_0 + + %0 = "tfl.mul"(%arg0, %zero_int) {fused_activation_function = "NONE"} : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi32> + %1 = "tfl.mul"(%arg1, %zero_float) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> + + func.return %0, %1 : tensor<4xi32>, tensor<4xf32> +} + +// CHECK-LABEL: @mul_zero_lhs +func.func @mul_zero_lhs(%arg0: tensor<4xi32>, %arg1: tensor<4xf32>) -> (tensor<4xi32>, tensor<4xf32>) { + %zero_int = arith.constant dense<0> : tensor<4xi32> + %zero_float = arith.constant dense<0.0> : tensor<4xf32> + + // CHECK-NOT: tfl.mul + // CHECK: return %cst, %cst_0 + + %0 = "tfl.mul"(%zero_int, %arg0) {fused_activation_function = "NONE"} : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi32> + %1 = "tfl.mul"(%zero_float, %arg1) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> + + func.return %0, %1 : tensor<4xi32>, tensor<4xf32> +} + +// CHECK-LABEL: @mul_one +func.func @mul_one(%arg0: tensor<4xi32>, %arg1: tensor<4xf32>) -> (tensor<4xi32>, tensor<4xf32>) { + %one_int = arith.constant dense<1> : tensor<4xi32> + %one_float = arith.constant dense<1.0> : tensor<4xf32> + + // CHECK-NOT: tfl.mul + // CHECK: return %arg0, %arg1 + + %0 = "tfl.mul"(%arg0, %one_int) {fused_activation_function = "NONE"} : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi32> + %1 = "tfl.mul"(%arg1, %one_float) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> + + func.return %0, %1 : tensor<4xi32>, tensor<4xf32> +} + +// CHECK-LABEL: @mul_one_lhs +func.func @mul_one_lhs(%arg0: tensor<4xi32>, %arg1: tensor<4xf32>) -> (tensor<4xi32>, tensor<4xf32>) { + %one_int = arith.constant dense<1> : tensor<4xi32> + %one_float = arith.constant dense<1.0> : tensor<4xf32> + + // CHECK-NOT: tfl.mul + // CHECK: return %arg0, %arg1 + + %0 = "tfl.mul"(%one_int, %arg0) {fused_activation_function = "NONE"} : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi32> + %1 = "tfl.mul"(%one_float, %arg1) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> + + func.return %0, %1 : tensor<4xi32>, tensor<4xf32> +} + +// CHECK-LABEL: @mul_one_quant +func.func @mul_one_quant(%arg0: tensor<32x!quant.uniform>) -> tensor<32x!quant.uniform> { + %one = "tfl.pseudo_qconst"() {qtype = tensor<32x!quant.uniform>, value = dense<1> : tensor<32xi8>} : () -> tensor<32x!quant.uniform> + + // CHECK: %[[MUL:.*]] = tfl.mul + // CHECK: return %[[MUL]] + + %0 = "tfl.mul"(%one, %arg0) {fused_activation_function = "NONE"} : (tensor<32x!quant.uniform>, tensor<32x!quant.uniform>) -> tensor<32x!quant.uniform> + + func.return %0 : tensor<32x!quant.uniform> +} + +// CHECK-LABEL: @max_with_neg_f32_max_val +// CHECK-SAME: (%[[ARG0:.+]]: tensor) +func.func @max_with_neg_f32_max_val(%arg0 : tensor) -> (tensor, tensor) { + %neg_f32_max = arith.constant dense<-3.40282347E+38> : tensor + %0 = "tfl.maximum"(%arg0, %neg_f32_max) : (tensor, tensor) -> tensor + %1 = "tfl.maximum"(%neg_f32_max, %arg0) : (tensor, tensor) -> tensor + func.return %0, %1 : tensor, tensor + // CHECK: return %[[ARG0]], %[[ARG0]] +} + +// CHECK-LABEL: @max_with_neg_inf +func.func @max_with_neg_inf(%arg0 : tensor) -> (tensor, tensor) { + %neg_inf = arith.constant dense<0xFF800000> : tensor + %0 = "tfl.maximum"(%arg0, %neg_inf) : (tensor, tensor) -> tensor + %1 = "tfl.maximum"(%neg_inf, %arg0) : (tensor, tensor) -> tensor + func.return %0, %1 : tensor, tensor + // CHECK: return %[[ARG0]], %[[ARG0]] +} + +// CHECK-LABEL: @min_with_f32_max_val +// CHECK-SAME: (%[[ARG0:.+]]: tensor) +func.func @min_with_f32_max_val(%arg0 : tensor) -> (tensor, tensor) { + %f32_max = arith.constant dense<3.40282347E+38> : tensor + %0 = "tfl.minimum"(%arg0, %f32_max) : (tensor, tensor) -> tensor + %1 = "tfl.minimum"(%f32_max, %arg0) : (tensor, tensor) -> tensor + func.return %0, %1 : tensor, tensor + // CHECK: return %[[ARG0]], %[[ARG0]] +} + +// CHECK-LABEL: @min_with_inf +func.func @min_with_inf(%arg0 : tensor) -> (tensor, tensor) { + %inf = arith.constant dense<0x7F800000> : tensor + %0 = "tfl.minimum"(%arg0, %inf) : (tensor, tensor) -> tensor + %1 = "tfl.minimum"(%inf, %arg0) : (tensor, tensor) -> tensor + func.return %0, %1 : tensor, tensor + // CHECK: return %[[ARG0]], %[[ARG0]] +} + +// CHECK-LABEL: @max_with_neg_f64_max_val +// CHECK-SAME: (%[[ARG0:.+]]: tensor) +func.func @max_with_neg_f64_max_val(%arg0 : tensor) -> (tensor, tensor) { + %neg_f64_max = arith.constant dense<-1.7976931348623157E+308> : tensor + %0 = "tfl.maximum"(%arg0, %neg_f64_max) : (tensor, tensor) -> tensor + %1 = "tfl.maximum"(%neg_f64_max, %arg0) : (tensor, tensor) -> tensor + func.return %0, %1 : tensor, tensor + // CHECK: return %[[ARG0]], %[[ARG0]] +} + +// CHECK-LABEL: @min_with_f64_max_val +// CHECK-SAME: (%[[ARG0:.+]]: tensor) +func.func @min_with_f64_max_val(%arg0 : tensor) -> (tensor, tensor) { + %f64_max = arith.constant dense<1.7976931348623157E+308> : tensor + %0 = "tfl.minimum"(%arg0, %f64_max) : (tensor, tensor) -> tensor + %1 = "tfl.minimum"(%f64_max, %arg0) : (tensor, tensor) -> tensor + func.return %0, %1 : tensor, tensor + // CHECK: return %[[ARG0]], %[[ARG0]] +} + +// CHECK-LABEL: @min_dense_splat_int +func.func @min_dense_splat_int() -> tensor<4xi32> { + %0 = arith.constant dense<[-10, -1, 42, 100]> : tensor<4xi32> + %1 = arith.constant dense<5> : tensor<4xi32> + + %2 = "tfl.minimum"(%0, %1) {fused_activation_function = "NONE"} : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi32> + + func.return %2 : tensor<4xi32> +} + +// CHECK: arith.constant dense<[-10, -1, 5, 5]> : tensor<4xi32> + +// CHECK-LABEL: @min_dense_splat_float +func.func @min_dense_splat_float() -> tensor<4xf32> { + %0 = arith.constant dense<[-10.0, -1.0, 42.0, 100.0]> : tensor<4xf32> + %1 = arith.constant dense<5.0> : tensor<4xf32> + + %2 = "tfl.minimum"(%0, %1) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> + + func.return %2 : tensor<4xf32> +} + +// CHECK: arith.constant dense<[-1.000000e+01, -1.000000e+00, 5.000000e+00, 5.000000e+00]> : tensor<4xf32> + +// CHECK-LABEL: @min_dense_float +func.func @min_dense_float() -> tensor<2xf32> { + %0 = arith.constant dense<[-10.0, 10.0]> : tensor<2xf32> + %1 = arith.constant dense<[5.0, 5.0]> : tensor<2xf32> + + %2 = "tfl.minimum"(%0, %1) {fused_activation_function = "NONE"} : (tensor<2xf32>, tensor<2xf32>) -> tensor<2xf32> + + func.return %2 : tensor<2xf32> +} + +// CHECK: arith.constant dense<[-1.000000e+01, 5.000000e+00]> : tensor<2xf32> + +// CHECK-LABEL: @max_dense_splat_int +func.func @max_dense_splat_int() -> tensor<4xi32> { + %0 = arith.constant dense<[-10, -1, 42, 100]> : tensor<4xi32> + %1 = arith.constant dense<5> : tensor<4xi32> + + %2 = "tfl.maximum"(%0, %1) {fused_activation_function = "NONE"} : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi32> + + func.return %2 : tensor<4xi32> +} + +// CHECK: arith.constant dense<[5, 5, 42, 100]> : tensor<4xi32> + +// CHECK-LABEL: @max_dense_splat_float +func.func @max_dense_splat_float() -> tensor<4xf32> { + %0 = arith.constant dense<[-10.0, -1.0, 42.0, 100.0]> : tensor<4xf32> + %1 = arith.constant dense<5.0> : tensor<4xf32> + + %2 = "tfl.maximum"(%0, %1) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> + + func.return %2 : tensor<4xf32> +} + +// CHECK: arith.constant dense<[5.000000e+00, 5.000000e+00, 4.200000e+01, 1.000000e+02]> : tensor<4xf32> + +// CHECK-LABEL: @max_dense_float +func.func @max_dense_float() -> tensor<2xf32> { + %0 = arith.constant dense<[-10.0, 10.0]> : tensor<2xf32> + %1 = arith.constant dense<[5.0, 5.0]> : tensor<2xf32> + + %2 = "tfl.maximum"(%0, %1) {fused_activation_function = "NONE"} : (tensor<2xf32>, tensor<2xf32>) -> tensor<2xf32> + + func.return %2 : tensor<2xf32> +} + +// CHECK: arith.constant dense<[5.000000e+00, 1.000000e+01]> : tensor<2xf32> + +// CHECK-LABEL: @mul_int +func.func @mul_int() -> (tensor, tensor<4xi32>, tensor<4xi32>, tensor<4xi32>) { + %0 = arith.constant dense<8> : tensor + %1 = arith.constant dense<1> : tensor + + %2 = arith.constant dense< 4> : tensor<4xi32> + %3 = arith.constant dense<-2> : tensor<4xi32> + + // CHECK-DAG: [[cst0:%.*]] = arith.constant dense<8> : tensor + // CHECK-DAG: [[cst1:%.*]] = arith.constant dense<-16> : tensor<4xi32> + // CHECK-DAG: [[cst2:%.*]] = arith.constant dense<4> : tensor<4xi32> + // CHECK-DAG: [[cst3:%.*]] = arith.constant dense<-8> : tensor<4xi32> + // CHECK: return [[cst0]], [[cst1]], [[cst2]], [[cst3]] + + %5 = "tfl.mul"(%0, %1) {fused_activation_function = "NONE"} : (tensor< i32>, tensor< i32>) -> tensor< i32> + %6 = "tfl.mul"(%0, %3) {fused_activation_function = "NONE"} : (tensor< i32>, tensor<4xi32>) -> tensor<4xi32> + %7 = "tfl.mul"(%2, %1) {fused_activation_function = "NONE"} : (tensor<4xi32>, tensor< i32>) -> tensor<4xi32> + %8 = "tfl.mul"(%2, %3) {fused_activation_function = "NONE"} : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi32> + + func.return %5, %6, %7, %8 : tensor, tensor<4xi32>, tensor<4xi32>, tensor<4xi32> +} + +// CHECK-LABEL: @add_dense_splat_int +func.func @add_dense_splat_int() -> tensor<4xi32> { + %0 = arith.constant dense<[-10, -1, 42, 100]> : tensor<4xi32> + %1 = arith.constant dense< 5> : tensor<4xi32> + + %2 = "tfl.add"(%0, %1) {fused_activation_function = "NONE"} : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi32> + + func.return %2 : tensor<4xi32> + +// CHECK: %[[CST:.*]] = arith.constant dense<[-5, 4, 47, 105]> : tensor<4xi32> +// CHECK: return %[[CST]] +} + +// CHECK-LABEL: @add_splat_dense_int +func.func @add_splat_dense_int() -> tensor<4xi32> { + %0 = arith.constant dense< 5> : tensor<4xi32> + %1 = arith.constant dense<[-10, -1, 42, 100]> : tensor<4xi32> + + %2 = "tfl.add"(%0, %1) {fused_activation_function = "NONE"} : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi32> + + func.return %2 : tensor<4xi32> + +// CHECK: %[[CST:.*]] = arith.constant dense<[-5, 4, 47, 105]> : tensor<4xi32> +// CHECK: return %[[CST]] +} + +// CHECK-LABEL: @add_dense_dense_int_same_shape +func.func @add_dense_dense_int_same_shape() -> tensor<4xi32> { + %0 = arith.constant dense<[15, 23, -44, -2]> : tensor<4xi32> + %1 = arith.constant dense<[-10, -1, 42, 100]> : tensor<4xi32> + + %2 = "tfl.add"(%0, %1) {fused_activation_function = "NONE"} : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi32> + + func.return %2 : tensor<4xi32> + +// CHECK: %[[CST:.*]] = arith.constant dense<[5, 22, -2, 98]> : tensor<4xi32> +// CHECK: return %[[CST]] +} + +// CHECK-LABEL: @add_dense_dense_int_trailing_dim +func.func @add_dense_dense_int_trailing_dim() -> (tensor<2x2xi32>, tensor<2x2x2xi32>, tensor<2x2x2xi32>) { + %cst_0 = arith.constant dense<[10, 20]> : tensor<2xi32> + %cst_1 = arith.constant dense<[[1, 2], [3, 4]]> : tensor<2x2xi32> + %cst_2 = arith.constant dense<[[[1, 1], [2, 2]], [[3, 3], [4, 4]]]> : tensor<2x2x2xi32> + + %0 = "tfl.add"(%cst_0, %cst_1) {fused_activation_function = "NONE"} : (tensor< 2xi32>, tensor< 2x2xi32>) -> tensor< 2x2xi32> + %1 = "tfl.add"(%cst_2, %cst_1) {fused_activation_function = "NONE"} : (tensor<2x2x2xi32>, tensor< 2x2xi32>) -> tensor<2x2x2xi32> + %2 = "tfl.add"(%cst_0, %cst_2) {fused_activation_function = "NONE"} : (tensor< 2xi32>, tensor<2x2x2xi32>) -> tensor<2x2x2xi32> + + func.return %0, %1, %2 : tensor<2x2xi32>, tensor<2x2x2xi32>, tensor<2x2x2xi32> + +// CHECK-DAG: %[[CST:.*]] = arith.constant dense<{{\[\[}}11, 22], [13, 24]]> : tensor<2x2xi32> +// CHECK-DAG: %[[CST_0:.*]] = arith.constant dense<{{\[\[\[}}2, 3], [5, 6]], {{\[\[}}4, 5], [7, 8]]]> : tensor<2x2x2xi32> +// CHECK-DAG: %[[CST_1:.*]] = arith.constant dense<{{\[\[\[}}11, 21], [12, 22]], {{\[\[}}13, 23], [14, 24]]]> : tensor<2x2x2xi32> +// CHECK: return %[[CST]], %[[CST_0]], %[[CST_1]] +} + +// CHECK-LABEL: @add_dense_dense_int_mixing_1_n +func.func @add_dense_dense_int_mixing_1_n() -> tensor<2x2xi32> { + %cst_0 = arith.constant dense<[[1, 2]]> : tensor<1x2xi32> + %cst_1 = arith.constant dense<[[3], [4]]> : tensor<2x1xi32> + + %0 = "tfl.add"(%cst_0, %cst_1) {fused_activation_function = "NONE"} : (tensor<1x2xi32>, tensor<2x1xi32>) -> tensor<2x2xi32> + + func.return %0 : tensor<2x2xi32> +// CHECK: %[[CST:.*]] = arith.constant dense<{{\[\[}}4, 5], [5, 6]]> : tensor<2x2xi32> +// CHECK: return %[[CST]] +} + +// CHECK-LABEL: @add_dense_splat_float +func.func @add_dense_splat_float() -> tensor<4xf32> { + %0 = arith.constant dense<[-10.0, -1.5, 42.0, 7.25]> : tensor<4xf32> + %1 = arith.constant dense< 3.5> : tensor<4xf32> + + %2 = "tfl.add"(%0, %1) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> + + func.return %2 : tensor<4xf32> + +// CHECK: %[[CST:.*]] = arith.constant dense<[-6.500000e+00, 2.000000e+00, 4.550000e+01, 1.075000e+01]> : tensor<4xf32> +// CHECK: return %[[CST]] +} + +// CHECK-LABEL: @add_splat_dense_float +func.func @add_splat_dense_float() -> tensor<4xf32> { + %0 = arith.constant dense< 3.5> : tensor<4xf32> + %1 = arith.constant dense<[-10.0, -1.5, 42.0, 7.25]> : tensor<4xf32> + + %2 = "tfl.add"(%0, %1) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> + + func.return %2 : tensor<4xf32> + +// CHECK: %[[CST:.*]] = arith.constant dense<[-6.500000e+00, 2.000000e+00, 4.550000e+01, 1.075000e+01]> : tensor<4xf32> +// CHECK: return %[[CST]] +} + +// CHECK-LABEL: @add_dense_dense_float_same_shape +func.func @add_dense_dense_float_same_shape() -> (tensor<4xf32>) { + %0 = arith.constant dense<[1.5, 2.3, -4.4, -2.0]> : tensor<4xf32> + %1 = arith.constant dense<[-10.4, -1.3, 42.4, 100.0]> : tensor<4xf32> + + %2 = "tfl.add"(%0, %1) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> + + func.return %2 : tensor<4xf32> + +// CHECK: %[[CST:.*]] = arith.constant dense<[-8.89999961, 1.000000e+00, 3.800000e+01, 9.800000e+01]> : tensor<4xf32> +// CHECK: return %[[CST]] +} + +// CHECK-LABEL: @add_dense_dense_float_trailing_dim +func.func @add_dense_dense_float_trailing_dim() -> (tensor<2x2xf32>, tensor<2x2x2xf32>, tensor<2x2x2xf32>) { + %cst_0 = arith.constant dense<[1., -4.]> : tensor<2xf32> + %cst_1 = arith.constant dense<[[-5.5, 1.5], [7.5, -4.5]]> : tensor<2x2xf32> + %cst_2 = arith.constant dense<[[[1., 1.], [2., 2.]], [[3., 3.], [4., 4.]]]> : tensor<2x2x2xf32> + + %0 = "tfl.add"(%cst_0, %cst_1) {fused_activation_function = "NONE"} : (tensor< 2xf32>, tensor< 2x2xf32>) -> tensor< 2x2xf32> + %1 = "tfl.add"(%cst_2, %cst_1) {fused_activation_function = "NONE"} : (tensor<2x2x2xf32>, tensor< 2x2xf32>) -> tensor<2x2x2xf32> + %2 = "tfl.add"(%cst_0, %cst_2) {fused_activation_function = "NONE"} : (tensor< 2xf32>, tensor<2x2x2xf32>) -> tensor<2x2x2xf32> + + func.return %0, %1, %2 : tensor<2x2xf32>, tensor<2x2x2xf32>, tensor<2x2x2xf32> + +// CHECK-DAG: %[[CST:.*]] = arith.constant dense<{{\[\[}}-4.500000e+00, -2.500000e+00], [8.500000e+00, -8.500000e+00]]> : tensor<2x2xf32> +// CHECK-DAG: %[[CST_0:.*]] = arith.constant dense<{{\[\[\[}}-4.500000e+00, 2.500000e+00], [9.500000e+00, -2.500000e+00]], {{\[\[}}-2.500000e+00, 4.500000e+00], [1.150000e+01, -5.000000e-01]]]> : tensor<2x2x2xf32> +// CHECK-DAG: %[[CST_1:.*]] = arith.constant dense<{{\[\[\[}}2.000000e+00, -3.000000e+00], [3.000000e+00, -2.000000e+00]], {{\[\[}}4.000000e+00, -1.000000e+00], [5.000000e+00, 0.000000e+00]]]> : tensor<2x2x2xf32> +// CHECK: return %[[CST]], %[[CST_0]], %[[CST_1]] +} + +// CHECK-LABEL: @add_dense_dense_float_mixfng_1_n +func.func @add_dense_dense_float_mixfng_1_n() -> tensor<2x2xf32> { + %cst_0 = arith.constant dense<[[1.5, -2.5]]> : tensor<1x2xf32> + %cst_1 = arith.constant dense<[[-3.], [4.]]> : tensor<2x1xf32> + + %0 = "tfl.add"(%cst_0, %cst_1) {fused_activation_function = "NONE"} : (tensor<1x2xf32>, tensor<2x1xf32>) -> tensor<2x2xf32> + + func.return %0 : tensor<2x2xf32> + +// CHECK: %[[CST:.*]] = arith.constant dense<{{\[\[}}-1.500000e+00, -5.500000e+00], [5.500000e+00, 1.500000e+00]]> : tensor<2x2xf32> +// CHECK: return %[[CST]] +} + +// CHECK-LABEL: @add_zero +func.func @add_zero(%arg0: tensor<4xi32>, %arg1: tensor<4xf32>) -> (tensor<4xi32>, tensor<4xf32>) { + %zero_int = arith.constant dense<0> : tensor<4xi32> + %zero_float = arith.constant dense<0.0> : tensor<4xf32> + + // CHECK-NOT: tfl.add + // CHECK: return %arg0, %arg1 + + %0 = "tfl.add"(%arg0, %zero_int) {fused_activation_function = "NONE"} : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi32> + %1 = "tfl.add"(%arg1, %zero_float) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> + + func.return %0, %1 : tensor<4xi32>, tensor<4xf32> +} + +// CHECK-LABEL: @add_zero_broadcast +func.func @add_zero_broadcast(%arg0: tensor<4xi32>, %arg1: tensor<4xf32>) -> (tensor<4xi32>, tensor<4xf32>) { + %zero_int = arith.constant dense<0> : tensor<1xi32> + %zero_float = arith.constant dense<0.0> : tensor<1xf32> + + // CHECK-NOT: tfl.add + // CHECK: return %arg0, %arg1 + + %0 = "tfl.add"(%arg0, %zero_int) {fused_activation_function = "NONE"} : (tensor<4xi32>, tensor<1xi32>) -> tensor<4xi32> + %1 = "tfl.add"(%arg1, %zero_float) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<1xf32>) -> tensor<4xf32> + + func.return %0, %1 : tensor<4xi32>, tensor<4xf32> +} + +// CHECK-LABEL: @add_zero_dynamic +func.func @add_zero_dynamic(%arg0: tensor, %arg1: tensor) -> (tensor, tensor) { + %zero_int = arith.constant dense<0> : tensor<1xi32> + %zero_float = arith.constant dense<0.0> : tensor<1xf32> + + // CHECK-NOT: tfl.add + // CHECK: return %arg0, %arg1 + + %0 = "tfl.add"(%arg0, %zero_int) {fused_activation_function = "NONE"} : (tensor, tensor<1xi32>) -> tensor + %1 = "tfl.add"(%arg1, %zero_float) {fused_activation_function = "NONE"} : (tensor, tensor<1xf32>) -> tensor + + func.return %0, %1 : tensor, tensor +} + +// CHECK-LABEL: @add_zero_lhs +func.func @add_zero_lhs(%arg0: tensor<4xi32>, %arg1: tensor<4xf32>) -> (tensor<4xi32>, tensor<4xf32>) { + %zero_int = arith.constant dense<0> : tensor<4xi32> + %zero_float = arith.constant dense<0.0> : tensor<4xf32> + + // CHECK-NOT: tfl.add + // CHECK: return %arg0, %arg1 + + %0 = "tfl.add"(%zero_int, %arg0) {fused_activation_function = "NONE"} : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi32> + %1 = "tfl.add"(%zero_float, %arg1) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> + + func.return %0, %1 : tensor<4xi32>, tensor<4xf32> +} + +// CHECK-LABEL: @add_zero_quant +func.func @add_zero_quant(%arg0: tensor<32x!quant.uniform>) -> tensor<32x!quant.uniform> { + %zero = "tfl.pseudo_qconst"() {qtype = tensor<32x!quant.uniform>, value = dense<0> : tensor<32xi8>} : () -> tensor<32x!quant.uniform> + + // CHECK: %[[ADD:.*]] = tfl.add + // CHECK: return %[[ADD]] + + %0 = "tfl.add"(%zero, %arg0) {fused_activation_function = "NONE"} : (tensor<32x!quant.uniform>, tensor<32x!quant.uniform>) -> tensor<32x!quant.uniform> + + func.return %0 : tensor<32x!quant.uniform> +} + +// CHECK-LABEL: @reshape +func.func @reshape() -> tensor<4xi32> { + %input = arith.constant dense<[[1, 2], [3, 4]]> : tensor<2x2xi32> + %shape = arith.constant dense<[4]> : tensor<1xi32> + + // CHECK: %[[CST:.*]] = arith.constant dense<[1, 2, 3, 4]> : tensor<4xi32> + // CHECK: return %[[CST]] + %0 = "tfl.reshape"(%input, %shape) : (tensor<2x2xi32>, tensor<1xi32>) -> tensor<4xi32> + func.return %0 : tensor<4xi32> +} + +// CHECK-LABEL: @transpose_no_fold +func.func @transpose_no_fold(%arg0 : tensor<2xi32>) -> tensor<2x2xi32> { + %cst = arith.constant dense<[[0, 1], [2, 3]]> : tensor<2x2xi32> + + // CHECK: tfl.transpose + %0 = "tfl.transpose"(%cst, %arg0) : (tensor<2x2xi32>, tensor<2xi32>) -> tensor<2x2xi32> + func.return %0 : tensor<2x2xi32> +} + + +// CHECK-LABEL: @transpose_1d +// Basic 1D identity +func.func @transpose_1d() -> tensor<3xi32> { + %cst = arith.constant dense<[1, 2, 3]> : tensor<3xi32> + %cst_perm = arith.constant dense<0> : tensor<1xi32> + + // CHECK: %[[CST:.*]] = arith.constant dense<{{\[}}1, 2, 3]> : tensor<3xi32> + // CHECK: return %[[CST]] + %0 = "tfl.transpose"(%cst, %cst_perm) : (tensor<3xi32>, tensor<1xi32>) -> tensor<3xi32> + func.return %0 : tensor<3xi32> +} + +// CHECK-LABEL: @transpose_2d +func.func @transpose_2d() -> tensor<2x2xi32> { + %cst = arith.constant dense<[[0, 1], [2, 3]]> : tensor<2x2xi32> + %cst_perm = arith.constant dense<[1, 0]> : tensor<2xi32> + + // CHECK: %[[CST:.*]] = arith.constant dense<{{\[\[}}0, 2], {{\[}}1, 3]]> : tensor<2x2xi32> + // CHECK: return %[[CST]] + %0 = "tfl.transpose"(%cst, %cst_perm) : (tensor<2x2xi32>, tensor<2xi32>) -> tensor<2x2xi32> + func.return %0 : tensor<2x2xi32> +} + +// CHECK-LABEL: @transpose_2d_splat +func.func @transpose_2d_splat() -> tensor<3x2xi32> { + %cst = arith.constant dense<0> : tensor<2x3xi32> + %cst_perm = arith.constant dense<[1, 0]> : tensor<2xi32> + + // CHECK: %[[CST:.*]] = arith.constant dense<0> : tensor<3x2xi32> + // CHECK: return %[[CST]] + %0 = "tfl.transpose"(%cst, %cst_perm) : (tensor<2x3xi32>, tensor<2xi32>) -> tensor<3x2xi32> + func.return %0 : tensor<3x2xi32> +} + +// CHECK-LABEL: @transpose_2d_identity +func.func @transpose_2d_identity() -> tensor<2x2xi32> { + %cst = arith.constant dense<[[0, 1], [2, 3]]> : tensor<2x2xi32> + %cst_perm = arith.constant dense<[0, 1]> : tensor<2xi32> + + // CHECK: %[[CST:.*]] = arith.constant dense<{{\[\[}}0, 1], {{\[}}2, 3]]> : tensor<2x2xi32> + // CHECK: return %[[CST]] + %0 = "tfl.transpose"(%cst, %cst_perm) : (tensor<2x2xi32>, tensor<2xi32>) -> tensor<2x2xi32> + func.return %0 : tensor<2x2xi32> +} + +// CHECK-LABEL: @transpose_3d +// A test case adopted from TransposeTest.Test3DInputConstTensor in +// tensorflow/lite/kernels/transpose_test.cc +func.func @transpose_3d() -> tensor<4x2x3xi32> { + %cst = arith.constant dense<[[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]]> : tensor<2x3x4xi32> + %cst_perm = arith.constant dense<[2, 0, 1]> : tensor<3xi32> + + // CHECK: %[[CST:.*]] = arith.constant dense<{{\[\[\[}}0, 4, 8], {{\[}}12, 16, 20]], {{\[\[}}1, 5, 9], {{\[}}13, 17, 21]], {{\[\[}}2, 6, 10], {{\[}}14, 18, 22]], {{\[\[}}3, 7, 11], {{\[}}15, 19, 23]]]> : tensor<4x2x3xi32> + // CHECK: return %[[CST]] + %0 = "tfl.transpose"(%cst, %cst_perm) : (tensor<2x3x4xi32>, tensor<3xi32>) -> tensor<4x2x3xi32> + func.return %0 : tensor<4x2x3xi32> +} + +// CHECK-LABEL: @ConstantFoldBinaryOpDynamicOutput +func.func @ConstantFoldBinaryOpDynamicOutput() -> tensor { + %cst = arith.constant dense<10> : tensor + %cst_0 = "tfl.pseudo_const"() {value = dense<[5, 10]> : tensor<2xi32>} : () -> tensor + %87 = "tfl.sub"(%cst_0, %cst) {fused_activation_function = "NONE"} : (tensor, tensor) -> tensor + func.return %87 : tensor + + // CHECK: %[[CST:.*]] = "tfl.pseudo_const"() <{value = dense<[-5, 0]> : tensor<2xi32>}> : () -> tensor + // CHECK: return %[[CST]] +} + +// CHECK-LABEL: @div_dense_dense_float_mixfng_1_n +func.func @div_dense_dense_float_mixfng_1_n() -> tensor<2x2xf32> { + %cst_0 = arith.constant dense<[[1.5, -2.5]]> : tensor<1x2xf32> + %cst_1 = arith.constant dense<[[-3.], [4.]]> : tensor<2x1xf32> + + %0 = "tfl.div"(%cst_0, %cst_1) {fused_activation_function = "NONE"} : (tensor<1x2xf32>, tensor<2x1xf32>) -> tensor<2x2xf32> + + func.return %0 : tensor<2x2xf32> + +// CHECK: %[[CST:.*]] = arith.constant dense<{{\[\[}}-5.000000e-01, 0.833333313], [3.750000e-01, -6.250000e-01]]> : tensor<2x2xf32> +// CHECK: return %[[CST]] +} + +// CHECK-LABEL: @div_dense_different_rank +func.func @div_dense_different_rank() -> tensor<1x2x2xf32> { + %cst_0 = arith.constant dense<[[[1.0],[2.0]]]> : tensor<1x2x1xf32> + %cst_1 = arith.constant dense<[[2.0, 3.0]]> : tensor<1x2xf32> + + %0 = "tfl.div"(%cst_0, %cst_1) {fused_activation_function = "NONE"} : (tensor<1x2x1xf32>, tensor<1x2xf32>) -> tensor<1x2x2xf32> + + func.return %0 : tensor<1x2x2xf32> + +// CHECK: %[[CST:.*]] = arith.constant dense<[{{\[}}{{\[}}5.000000e-01, 0.333333343], [1.000000e+00, 0.666666686]]]> : tensor<1x2x2xf32> +// CHECK: return %[[CST]] +} + +// CHECK-LABEL: @div_one +func.func @div_one(%arg0: tensor<4xi32>, %arg1: tensor<4xf32>) -> (tensor<4xi32>, tensor<4xf32>) { + %one_int = arith.constant dense<1> : tensor<4xi32> + %one_float = arith.constant dense<1.0> : tensor<4xf32> + + // CHECK-NOT: tfl.div + // CHECK: return %arg0, %arg1 + + %0 = "tfl.div"(%arg0, %one_int) {fused_activation_function = "NONE"} : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi32> + %1 = "tfl.div"(%arg1, %one_float) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> + + func.return %0, %1 : tensor<4xi32>, tensor<4xf32> +} + +// CHECK-LABEL: @div_one_quant +func.func @div_one_quant(%arg0: tensor<32x!quant.uniform>) -> tensor<32x!quant.uniform> { + %one = "tfl.pseudo_qconst"() {qtype = tensor<32x!quant.uniform>, value = dense<1> : tensor<32xi8>} : () -> tensor<32x!quant.uniform> + + // CHECK: %[[DIV:.*]] = tfl.div + // CHECK: return %[[DIV]] + + %0 = "tfl.div"(%arg0, %one) {fused_activation_function = "NONE"} : (tensor<32x!quant.uniform>, tensor<32x!quant.uniform>) -> tensor<32x!quant.uniform> + + func.return %0 : tensor<32x!quant.uniform> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/const-fold-unary-resource.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/const-fold-unary-resource.mlir new file mode 100644 index 0000000000000000000000000000000000000000..42cb19e0b95b6f8fea5e0beaf6c1ba9c6cb476bf --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/const-fold-unary-resource.mlir @@ -0,0 +1,122 @@ +// RUN: litert-opt %s -canonicalize | FILECHECK_OPTS="" FileCheck %s +// RUN: litert-opt %s --tfl-dense-to-dense-resource-elements -canonicalize | litert-opt --tfl-dense-resource-to-dense-elements | FILECHECK_OPTS="" FileCheck %s + +// CHECK-LABEL: @test_logical_not_i1 +func.func @test_logical_not_i1() -> tensor<4xi1> { + // CHECK: arith.constant dense<[false, true, false, true]> : tensor<4xi1> + %0 = arith.constant dense<[true, false, true, false]> : tensor<4xi1> + %1 = "tfl.logical_not"(%0) : (tensor<4xi1>) -> tensor<4xi1> + func.return %1 : tensor<4xi1> +} + +// CHECK-LABEL: @test_relu_i32 +func.func @test_relu_i32() -> tensor<4xi32> { + // CHECK: arith.constant dense<[0, 2, 0, 4]> : tensor<4xi32> + %0 = arith.constant dense<[-1, 2, -3, 4]> : tensor<4xi32> + %1 = "tfl.relu"(%0) : (tensor<4xi32>) -> tensor<4xi32> + func.return %1 : tensor<4xi32> +} + +// CHECK-LABEL: @test_relu_f32 +func.func @test_relu_f32() -> tensor<4xf32> { + // CHECK: arith.constant dense<[0.000000e+00, 2.000000e+00, 0.000000e+00, 4.000000e+00]> : tensor<4xf32> + %0 = arith.constant dense<[-1.0, 2.0, -3.0, 4.0]> : tensor<4xf32> + %1 = "tfl.relu"(%0) : (tensor<4xf32>) -> tensor<4xf32> + func.return %1 : tensor<4xf32> +} + +// CHECK-LABEL: @test_abs_f32 +func.func @test_abs_f32() -> tensor<4xf32> { + // CHECK: arith.constant dense<[1.000000e+00, 2.000000e+00, 3.000000e+00, 4.000000e+00]> : tensor<4xf32> + %0 = arith.constant dense<[-1.0, 2.0, -3.0, 4.0]> : tensor<4xf32> + %1 = "tfl.abs"(%0) : (tensor<4xf32>) -> tensor<4xf32> + func.return %1 : tensor<4xf32> +} + +// CHECK-LABEL: @test_ceil_f32 +func.func @test_ceil_f32() -> tensor<4xf32> { + // CHECK: arith.constant dense<[-1.000000e+00, 2.000000e+00, -3.000000e+00, 4.000000e+00]> : tensor<4xf32> + %0 = arith.constant dense<[-1.5, 1.5, -3.5, 3.5]> : tensor<4xf32> + %1 = "tfl.ceil"(%0) : (tensor<4xf32>) -> tensor<4xf32> + func.return %1 : tensor<4xf32> +} + +// CHECK-LABEL: @test_floor_f32 +func.func @test_floor_f32() -> tensor<4xf32> { + // CHECK: arith.constant dense<[-2.000000e+00, 1.000000e+00, -4.000000e+00, 3.000000e+00]> : tensor<4xf32> + %0 = arith.constant dense<[-1.5, 1.5, -3.5, 3.5]> : tensor<4xf32> + %1 = "tfl.floor"(%0) : (tensor<4xf32>) -> tensor<4xf32> + func.return %1 : tensor<4xf32> +} + +// CHECK-LABEL: @test_neg_f32 +func.func @test_neg_f32() -> tensor<4xf32> { + // CHECK: arith.constant dense<[1.000000e+00, -2.000000e+00, 3.000000e+00, -4.000000e+00]> : tensor<4xf32> + %0 = arith.constant dense<[-1.0, 2.0, -3.0, 4.0]> : tensor<4xf32> + %1 = "tfl.neg"(%0) : (tensor<4xf32>) -> tensor<4xf32> + func.return %1 : tensor<4xf32> +} + +// CHECK-LABEL: @test_sin_f32 +func.func @test_sin_f32() -> tensor<1xf32> { + // CHECK: arith.constant dense<0.000000e+00> : tensor<1xf32> + %0 = arith.constant dense<[0.0]> : tensor<1xf32> + %1 = "tfl.sin"(%0) : (tensor<1xf32>) -> tensor<1xf32> + func.return %1 : tensor<1xf32> +} + +// CHECK-LABEL: @test_cos_f32 +func.func @test_cos_f32() -> tensor<1xf32> { + // CHECK: arith.constant dense<1.000000e+00> : tensor<1xf32> + %0 = arith.constant dense<[0.0]> : tensor<1xf32> + %1 = "tfl.cos"(%0) : (tensor<1xf32>) -> tensor<1xf32> + func.return %1 : tensor<1xf32> +} + +// CHECK-LABEL: @test_log_f32 +func.func @test_log_f32() -> tensor<1xf32> { + // CHECK: arith.constant dense<0.000000e+00> : tensor<1xf32> + %0 = arith.constant dense<[1.0]> : tensor<1xf32> + %1 = "tfl.log"(%0) : (tensor<1xf32>) -> tensor<1xf32> + func.return %1 : tensor<1xf32> +} + +// CHECK-LABEL: @test_sqrt_f32 +func.func @test_sqrt_f32() -> tensor<1xf32> { + // CHECK: arith.constant dense<2.000000e+00> : tensor<1xf32> + %0 = arith.constant dense<[4.0]> : tensor<1xf32> + %1 = "tfl.sqrt"(%0) : (tensor<1xf32>) -> tensor<1xf32> + func.return %1 : tensor<1xf32> +} + +// CHECK-LABEL: @test_rsqrt_f32 +func.func @test_rsqrt_f32() -> tensor<1xf32> { + // CHECK: arith.constant dense<5.000000e-01> : tensor<1xf32> + %0 = arith.constant dense<[4.0]> : tensor<1xf32> + %1 = "tfl.rsqrt"(%0) : (tensor<1xf32>) -> tensor<1xf32> + func.return %1 : tensor<1xf32> +} + +// CHECK-LABEL: @test_square_f32 +func.func @test_square_f32() -> tensor<1xf32> { + // CHECK: arith.constant dense<1.600000e+01> : tensor<1xf32> + %0 = arith.constant dense<[4.0]> : tensor<1xf32> + %1 = "tfl.square"(%0) : (tensor<1xf32>) -> tensor<1xf32> + func.return %1 : tensor<1xf32> +} + +// CHECK-LABEL: @test_tanh_f32 +func.func @test_tanh_f32() -> tensor<1xf32> { + // CHECK: arith.constant dense<0.000000e+00> : tensor<1xf32> + %0 = arith.constant dense<[0.0]> : tensor<1xf32> + %1 = "tfl.tanh"(%0) : (tensor<1xf32>) -> tensor<1xf32> + func.return %1 : tensor<1xf32> +} + +// CHECK-LABEL: @test_exp_f32 +func.func @test_exp_f32() -> tensor<1xf32> { + // CHECK: arith.constant dense<1.000000e+00> : tensor<1xf32> + %0 = arith.constant dense<[0.0]> : tensor<1xf32> + %1 = "tfl.exp"(%0) : (tensor<1xf32>) -> tensor<1xf32> + func.return %1 : tensor<1xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/const-fold.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/const-fold.mlir new file mode 100644 index 0000000000000000000000000000000000000000..a9306115aa35130f0e5cb93d2049ef69f923b4b5 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/const-fold.mlir @@ -0,0 +1,1219 @@ +// RUN: litert-opt %s -canonicalize | env FILECHECK_OPTS="" FileCheck %s + +// CHECK-LABEL: @elementwise_unary_ops +func.func @elementwise_unary_ops() -> (tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor) { + %0 = arith.constant dense<-1.0> : tensor + %1 = arith.constant dense<1.0> : tensor + %2 = arith.constant dense<1.0> : tensor + %3 = arith.constant dense<1.0> : tensor + %4 = arith.constant dense<4.0> : tensor + %5 = arith.constant dense<4.0> : tensor + %6 = arith.constant dense<2.0> : tensor + %one = arith.constant dense<1.0> : tensor + + // CHECK-DAG: [[cst0:%.*]] = arith.constant dense<1.000000e+00> : tensor + // CHECK-DAG: [[cst1:%.*]] = arith.constant dense<0.841470957> : tensor + // CHECK-DAG: [[cst2:%.*]] = arith.constant dense<0.540302277> : tensor + // CHECK-DAG: [[cst3:%.*]] = arith.constant dense<0.000000e+00> : tensor + // CHECK-DAG: [[cst4:%.*]] = arith.constant dense<2.000000e+00> : tensor + // CHECK-DAG: [[cst5:%.*]] = arith.constant dense<5.000000e-01> : tensor + // CHECK-DAG: [[cst6:%.*]] = arith.constant dense<4.000000e+00> : tensor + // CHECK-DAG: [[cst7:%.*]] = arith.constant dense<0.761594176> : tensor + // CHECK-DAG: [[cst8:%.*]] = arith.constant dense<0.73105859{{[78]}}> : tensor + // CHECK: return [[cst0]], [[cst1]], [[cst2]], [[cst3]], [[cst4]], [[cst5]], [[cst6]], [[cst7]], [[cst8]] + + %7 = "tfl.abs"(%0) : (tensor) -> tensor + %8 = "tfl.sin"(%1) : (tensor) -> tensor + %9 = "tfl.cos"(%2) : (tensor) -> tensor + %10 = "tfl.log"(%3) : (tensor) -> tensor + %11 = "tfl.sqrt"(%4) : (tensor) -> tensor + %12 = "tfl.rsqrt"(%5) : (tensor) -> tensor + %13 = "tfl.square"(%6) : (tensor) -> tensor + %14 = "tfl.tanh"(%one) : (tensor) -> tensor + %15 = "tfl.logistic"(%one) : (tensor) -> tensor + + func.return %7, %8, %9, %10, %11, %12, %13, %14, %15 : tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor +} + +// CHECK-LABEL: @cumsum_chained_pipeline +func.func @cumsum_chained_pipeline() -> tensor<3xf32> { + %input = arith.constant dense<[1.0, 2.0, 3.0]> : tensor<3xf32> + %axis = arith.constant dense<0> : tensor + %cst_two = arith.constant dense<2.0> : tensor<3xf32> + %one_tensor = arith.constant dense<1.0> : tensor<3xf32> + + // CHECK: %[[PIPELINE_RES:.*]] = arith.constant dense<[0.982013761, 0.999664664, 0.999999165]> : tensor<3xf32> + // CHECK: return %[[PIPELINE_RES]] + + %0 = "tfl.cumsum"(%input, %axis) {exclusive = false, reverse = false} : (tensor<3xf32>, tensor) -> tensor<3xf32> + %add = "tfl.add"(%0, %cst_two) {fused_activation_function = "NONE"} : (tensor<3xf32>, tensor<3xf32>) -> tensor<3xf32> + %sub = "tfl.sub"(%add, %one_tensor) {fused_activation_function = "NONE"} : (tensor<3xf32>, tensor<3xf32>) -> tensor<3xf32> + %mul = "tfl.mul"(%sub, %cst_two) {fused_activation_function = "NONE"} : (tensor<3xf32>, tensor<3xf32>) -> tensor<3xf32> + %res_pipeline = "tfl.logistic"(%mul) : (tensor<3xf32>) -> tensor<3xf32> + func.return %res_pipeline : tensor<3xf32> +} + +// CHECK-LABEL: @rank +func.func @rank() -> tensor<1xi32> { + %cst = arith.constant dense<[[1], [2]]> : tensor<2x1xi32> + + // CHECK: %[[CST:.*]] = arith.constant dense<2> : tensor<1xi32> + // CHECK: return %[[CST]] + %0 = "tfl.rank"(%cst) : (tensor<2x1xi32>) -> tensor<1xi32> + func.return %0 : tensor<1xi32> +} + +// CHECK-LABEL: @rank_input_known_rank +func.func @rank_input_known_rank(%arg0 : tensor<2x1xi32>) -> tensor<1xi32> { + // CHECK: %[[CST:.*]] = arith.constant dense<2> : tensor<1xi32> + // CHECK: return %[[CST]] + %0 = "tfl.rank"(%arg0) : (tensor<2x1xi32>) -> tensor<1xi32> + func.return %0 : tensor<1xi32> +} + +// CHECK-LABEL: @reshape_dynamic_output +func.func @reshape_dynamic_output() -> tensor { + %input = arith.constant dense<[[1, 2], [3, 4]]> : tensor<2x2xi32> + %shape = arith.constant dense<[4]> : tensor<1xi32> + + // CHECK: %[[CST:.*]] = "tfl.pseudo_const"() <{value = dense<[1, 2, 3, 4]> : tensor<4xi32>}> : () -> tensor + // CHECK: return %[[CST]] + %0 = "tfl.reshape"(%input, %shape) : (tensor<2x2xi32>, tensor<1xi32>) -> tensor + func.return %0 : tensor +} + + +// CHECK-LABEL: @pseudo_const +func.func @pseudo_const() -> tensor { + // CHECK: %[[CST:.*]] = arith.constant dense<1> : tensor + // CHECK: return %[[CST]] + %0 = "tfl.pseudo_const"() {value = dense<1> : tensor} : () -> tensor + func.return %0 : tensor +} + + +// CHECK-LABEL: @range_int +func.func @range_int() -> tensor { + %cst = arith.constant dense<0> : tensor + %cst_1 = arith.constant dense<4> : tensor + %cst_2 = arith.constant dense<1> : tensor + + // CHECK: %[[CST:.*]] = "tfl.pseudo_const"() <{value = dense<[0, 1, 2, 3]> : tensor<4xi32>}> : () -> tensor + // CHECK: return %[[CST]] + %0 = "tfl.range"(%cst, %cst_1, %cst_2) : (tensor, tensor, tensor) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: @range_float +func.func @range_float() -> tensor { + %cst = arith.constant dense<0.0> : tensor + %cst_1 = arith.constant dense<4.0> : tensor + %cst_2 = arith.constant dense<1.0> : tensor + + // CHECK: %[[CST:.*]] = "tfl.pseudo_const"() <{value = dense<[0.000000e+00, 1.000000e+00, 2.000000e+00, 3.000000e+00]> : tensor<4xf32>}> : () -> tensor + // CHECK: return %[[CST]] + %0 = "tfl.range"(%cst, %cst_1, %cst_2) : (tensor, tensor, tensor) -> tensor + func.return %0 : tensor +} + + +// CHECK-LABEL: @range_float_neg_delta +func.func @range_float_neg_delta() -> tensor { + %cst = arith.constant dense<0.0> : tensor + %cst_1 = arith.constant dense<-4.0> : tensor + %cst_2 = arith.constant dense<-1.0> : tensor + + // CHECK: %[[CST:.*]] = "tfl.pseudo_const"() <{value = dense<[0.000000e+00, -1.000000e+00, -2.000000e+00, -3.000000e+00]> : tensor<4xf32>}> : () -> tensor + // CHECK: return %[[CST]] + %0 = "tfl.range"(%cst, %cst_1, %cst_2) : (tensor, tensor, tensor) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: @range_float_nonzero_base +func.func @range_float_nonzero_base() -> tensor { + %cst = arith.constant dense<2.0> : tensor + %cst_1 = arith.constant dense<7.0> : tensor + %cst_2 = arith.constant dense<1.5> : tensor + + // CHECK: %[[CST:.*]] = "tfl.pseudo_const"() <{value = dense<[2.000000e+00, 3.500000e+00, 5.000000e+00, 6.500000e+00]> : tensor<4xf32>}> : () -> tensor + // CHECK: return %[[CST]] + %0 = "tfl.range"(%cst, %cst_1, %cst_2) : (tensor, tensor, tensor) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: @transpose_dynamic +func.func @transpose_dynamic() -> tensor { + %cst = arith.constant dense<[1, 2, 3]> : tensor<3xi32> + %cst_perm = arith.constant dense<0> : tensor<1xi32> + + // CHECK: %[[CST:.*]] = "tfl.pseudo_const"() <{value = dense<{{\[}}1, 2, 3]> : tensor<3xi32>}> : () -> tensor + // CHECK: return %[[CST]] + %0 = "tfl.transpose"(%cst, %cst_perm) : (tensor<3xi32>, tensor<1xi32>) -> tensor + func.return %0 : tensor +} + +// CHECK-LABEL: @add_dense_dense_int_same_shape_dynamic +func.func @add_dense_dense_int_same_shape_dynamic() -> tensor { + %0 = arith.constant dense<[15, 23, -44, -2]> : tensor<4xi32> + %1 = arith.constant dense<[-10, -1, 42, 100]> : tensor<4xi32> + + %2 = "tfl.add"(%0, %1) {fused_activation_function = "NONE"} : (tensor<4xi32>, tensor<4xi32>) -> tensor + + func.return %2 : tensor + + // CHECK: %[[CST:.*]] = "tfl.pseudo_const"() <{value = dense<[5, 22, -2, 98]> : tensor<4xi32>}> : () -> tensor + // CHECK: return %[[CST]] +} + +// CHECK-LABEL: @concat_2_tensors_1_empty +func.func @concat_2_tensors_1_empty() -> tensor<2xi32> { + %1 = arith.constant dense<1> : tensor<2xi32> + %2 = arith.constant dense<[]> : tensor<0xi32> + %3 = "tfl.concatenation"(%1, %2) {axis = 0 : i32, fused_activation_function = "NONE"} : (tensor<2xi32>, tensor<0xi32>) -> tensor<2xi32> + func.return %3 : tensor<2xi32> + + // CHECK: %[[CST:.*]] = arith.constant dense<1> : tensor<2xi32> + // CHECK: return %[[CST]] : tensor<2xi32> +} + +// CHECK-LABEL: @concat_3_tensors_1_empty +func.func @concat_3_tensors_1_empty() -> tensor { + %0 = arith.constant dense<1> : tensor<2xi32> + %1 = arith.constant dense<1> : tensor<2xi32> + %2 = arith.constant dense<[]> : tensor<0xi32> + %3 = "tfl.concatenation"(%0, %1, %2) {axis = 0 : i32, fused_activation_function = "NONE"} : (tensor<2xi32>, tensor<2xi32>, tensor<0xi32>) -> tensor + func.return %3 : tensor + + // CHECK: %0 = "tfl.concatenation"(%[[CST]], %[[CST]]) <{axis = 0 : i32, fused_activation_function = "NONE"}> + // CHECK: return %0 : tensor +} + +// CHECK-LABEL: @concatConstantTensorsFirstDim +func.func @concatConstantTensorsFirstDim() -> tensor<2x2x3xi32> { + %cst_0 = arith.constant dense<0> : tensor<1x2x3xi32> + %cst_1 = arith.constant dense<1> : tensor<1x2x3xi32> + %0 = "tfl.concatenation"(%cst_0, %cst_1) {axis = 0 : i32, fused_activation_function = "NONE"} : (tensor<1x2x3xi32>, tensor<1x2x3xi32>) -> tensor<2x2x3xi32> + func.return %0 : tensor<2x2x3xi32> + + // CHECK: %[[CST:.*]] = arith.constant dense<[{{\[}}{{\[}}0, 0, 0], {{\[}}0, 0, 0]], {{\[}}{{\[}}1, 1, 1], {{\[}}1, 1, 1]]]> : tensor<2x2x3xi32> + // CHECK-NOT: constant-dense + // CHECK-NOT: "tfl.concatenation" + // CHECK: return %[[CST]] +} + +// CHECK-LABEL: @concatConstantTensorsMiddleDim +func.func @concatConstantTensorsMiddleDim() -> tensor<1x4x3xi32> { + %cst_0 = arith.constant dense<0> : tensor<1x2x3xi32> + %cst_1 = arith.constant dense<1> : tensor<1x2x3xi32> + %0 = "tfl.concatenation"(%cst_0, %cst_1) {axis = 1 : i32, fused_activation_function = "NONE"} : (tensor<1x2x3xi32>, tensor<1x2x3xi32>) -> tensor<1x4x3xi32> + func.return %0 : tensor<1x4x3xi32> + + // CHECK: %[[CST:.*]] = arith.constant dense<[{{\[}}{{\[}}0, 0, 0], {{\[}}0, 0, 0], {{\[}}1, 1, 1], {{\[}}1, 1, 1]]]> : tensor<1x4x3xi32> + // CHECK-NOT: constant-dense + // CHECK-NOT: "tfl.concatenation" + // CHECK: return %[[CST]] +} + +// CHECK-LABEL: @concatConstantTensorsLastDim +func.func @concatConstantTensorsLastDim() -> tensor<1x2x6xi32> { + %cst_0 = arith.constant dense<0> : tensor<1x2x3xi32> + %cst_1 = arith.constant dense<1> : tensor<1x2x3xi32> + %0 = "tfl.concatenation"(%cst_0, %cst_1) {axis = 2 : i32, fused_activation_function = "NONE"} : (tensor<1x2x3xi32>, tensor<1x2x3xi32>) -> tensor<1x2x6xi32> + func.return %0 : tensor<1x2x6xi32> + + // CHECK: %[[CST:.*]] = arith.constant dense<[{{\[}}{{\[}}0, 0, 0, 1, 1, 1], {{\[}}0, 0, 0, 1, 1, 1]]]> : tensor<1x2x6xi32> + // CHECK-NOT: constant-dense + // CHECK-NOT: "tfl.concatenation" + // CHECK: return %[[CST]] +} + +// CHECK-LABEL: @rsqrt_bf16 +func.func @rsqrt_bf16() -> tensor { + %cst = arith.constant dense<4.0> : tensor + %0 = "tfl.rsqrt"(%cst) : (tensor) -> tensor + func.return %0 : tensor + +// CHECK: %[[CST:.*]] = arith.constant dense<5.000000e-01> : tensor +// CHECK: return %[[CST]] +} + +// CHECK-LABEL: @cast_i64_to_i32 +func.func @cast_i64_to_i32() -> tensor<5xi32> { + %cst = arith.constant dense<[-1, 0, 1, 2147483647, 2147483648]> : tensor<5xi64> + %0 = "tfl.cast"(%cst) : (tensor<5xi64>) -> tensor<5xi32> + func.return %0 : tensor<5xi32> + +// CHECK: %[[CST:.*]] = arith.constant dense<[-1, 0, 1, 2147483647, -2147483648]> : tensor<5xi32> +// CHECK: return %[[CST]] +} + +// CHECK-LABEL: @cast_i32_to_i8 +func.func @cast_i32_to_i8() -> tensor<6xi8> { + %cst = arith.constant dense<[0, -1, 256, 127, -128, -129]> : tensor<6xi32> + %0 = "tfl.cast"(%cst) : (tensor<6xi32>) -> tensor<6xi8> + func.return %0 : tensor<6xi8> + +// CHECK: %[[CST:.*]] = arith.constant dense<[0, -1, 0, 127, -128, 127]> : tensor<6xi8> +// CHECK: return %[[CST]] +} + +// CHECK-LABEL: @cast_i8_to_i32 +func.func @cast_i8_to_i32() -> tensor<4xi32> { + %cst = arith.constant dense<[0, 128, -1, -128]> : tensor<4xi8> + %0 = "tfl.cast"(%cst) : (tensor<4xi8>) -> tensor<4xi32> + func.return %0 : tensor<4xi32> + +// CHECK: %[[CST:.*]] = arith.constant dense<[0, -128, -1, -128]> : tensor<4xi32> +// CHECK: return %[[CST]] +} + + +// CHECK-LABEL: @cast_identity +func.func @cast_identity(%arg0 : tensor<7xf32>) -> tensor<7xf32> { + %0 = "tfl.cast"(%arg0) : (tensor<7xf32>) -> tensor<7xf32> + func.return %0 : tensor<7xf32> + // CHECK: return %arg0 : tensor<7xf32> +} + +// CHECK-LABEL: @cast_i1_to_i8 +func.func @cast_i1_to_i8() -> tensor<2xi8> { + %cst = arith.constant dense<[false, true]> : tensor<2xi1> + %0 = "tfl.cast"(%cst) : (tensor<2xi1>) -> tensor<2xi8> + func.return %0 : tensor<2xi8> + +// CHECK: %[[CST:.*]] = arith.constant dense<[0, 1]> : tensor<2xi8> +// CHECK: return %[[CST]] +} + +// CHECK-LABEL: @cast_i8_to_i1 +func.func @cast_i8_to_i1() -> tensor<4xi1> { + %cst = arith.constant dense<[0, 1, 2, -1]> : tensor<4xi8> + %0 = "tfl.cast"(%cst) : (tensor<4xi8>) -> tensor<4xi1> + func.return %0 : tensor<4xi1> + +// CHECK: %[[CST:.*]] = arith.constant dense<[false, true, true, true]> : tensor<4xi1> +// CHECK: return %[[CST]] +} + +// CHECK-LABEL: @cast_f32_to_i32 +func.func @cast_f32_to_i32() -> tensor<8xi32> { + %cst = arith.constant dense<[-1.0, 0.0, 1.5, 0.99, 1.175494351e-38, 3.402823466e+38, -3.402823466e+38, -1.175494351e-38]> : tensor<8xf32> + %0 = "tfl.cast"(%cst) : (tensor<8xf32>) -> tensor<8xi32> + func.return %0 : tensor<8xi32> +} + +// CHECK: %cst = arith.constant dense<[-1, 0, 1, 0, 0, 2147483647, -2147483648, 0]> : tensor<8xi32> + +// CHECK-LABEL: @cast_f32_to_i64 +func.func @cast_f32_to_i64() -> tensor<4xi64> { + %cst = arith.constant dense<[-1.0, 0.0, 1.5, 0.99]> : tensor<4xf32> + %0 = "tfl.cast"(%cst) : (tensor<4xf32>) -> tensor<4xi64> + func.return %0 : tensor<4xi64> +} + +// CHECK: %cst = arith.constant dense<[-1, 0, 1, 0]> : tensor<4xi64> + +// CHECK-LABEL: @cast_i32_to_f32 +func.func @cast_i32_to_f32() -> tensor<5xf32> { + %cst = arith.constant dense<[-1, 0, 2, 2147483647, -2147483648]> : tensor<5xi32> + %0 = "tfl.cast"(%cst) : (tensor<5xi32>) -> tensor<5xf32> + func.return %0 : tensor<5xf32> +} + +// CHECK: %cst = arith.constant dense<[-1.000000e+00, 0.000000e+00, 2.000000e+00, 2.14748365E+9, -2.14748365E+9]> : tensor<5xf32> + +// CHECK-LABEL: @cast_bool_to_f32 +func.func @cast_bool_to_f32() -> tensor<2xf32> { + %cst = arith.constant dense<[true, false]> : tensor<2xi1> + %0 = "tfl.cast"(%cst) : (tensor<2xi1>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK: %cst = arith.constant dense<[1.000000e+00, 0.000000e+00]> : tensor<2xf32> + +// CHECK-LABEL: @cast_f64_to_f32 +func.func @cast_f64_to_f32() -> tensor<4xf32> { + %cst = arith.constant dense<[-1.0, 0.0, 1.5, 100.0]> : tensor<4xf64> + %0 = "tfl.cast"(%cst) : (tensor<4xf64>) -> tensor<4xf32> + func.return %0 : tensor<4xf32> +} + +// CHECK: %cst = arith.constant dense<[-1.000000e+00, 0.000000e+00, 1.500000e+00, 1.000000e+02]> : tensor<4xf32> + +// CHECK-LABEL: @cast_f32_to_f64 +func.func @cast_f32_to_f64() -> tensor<4xf64> { + %cst = arith.constant dense<[-1.0, 0.0, 1.5, 100.0]> : tensor<4xf32> + %0 = "tfl.cast"(%cst) : (tensor<4xf32>) -> tensor<4xf64> + func.return %0 : tensor<4xf64> +} + +// CHECK: %cst = arith.constant dense<[-1.000000e+00, 0.000000e+00, 1.500000e+00, 1.000000e+02]> : tensor<4xf64> + +// CHECK-LABEL: @cast_f32_to_f16 +func.func @cast_f32_to_f16() -> tensor<4xf16> { + %cst = arith.constant dense<[-1.0, 0.0, 1.5, 100.0]> : tensor<4xf32> + %0 = "tfl.cast"(%cst) : (tensor<4xf32>) -> tensor<4xf16> + func.return %0 : tensor<4xf16> +} + +// CHECK: %cst = arith.constant dense<[-1.000000e+00, 0.000000e+00, 1.500000e+00, 1.000000e+02]> : tensor<4xf16> + +// CHECK-LABEL: @ConstantFoldFullyConnectedSmall +func.func @ConstantFoldFullyConnectedSmall() -> tensor<3xf32> { + %cst_input = arith.constant dense<[2.0, 3.0]> : tensor<2xf32> + %cst_weights = arith.constant dense<[[5.0, 7.0], [11.0, 13.0], [17.0, 19.0]]> : tensor<3x2xf32> + %cst_bias = arith.constant dense<[23.0, 29.0, 31.0]> : tensor<3xf32> + + %0 = "tfl.fully_connected" (%cst_input, %cst_weights, %cst_bias) {fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"} : (tensor<2xf32>, tensor<3x2xf32>, tensor<3xf32>) -> tensor<3xf32> + func.return %0 : tensor<3xf32> + + // [54, 90, 122] + // CHECK: %[[CST:.*]] = arith.constant dense<[5.400000e+01, 9.000000e+01, 1.220000e+02]> : tensor<3xf32> + // CHECK: return %[[CST]] +} + +// CHECK-LABEL: @ConstantFoldFullyConnectedLarge +func.func @ConstantFoldFullyConnectedLarge() -> tensor<1024xf32> { + %cst_input = arith.constant dense<1.0> : tensor<512xf32> + %cst_weights = arith.constant dense<2.0> : tensor<1024x512xf32> + %cst_bias = arith.constant dense<4.0> : tensor<1024xf32> + + %0 = "tfl.fully_connected" (%cst_input, %cst_weights, %cst_bias) {fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"} : (tensor<512xf32>, tensor<1024x512xf32>, tensor<1024xf32>) -> tensor<1024xf32> + + func.return %0 : tensor<1024xf32> + + // 1.0 * 2.0 * 512 + 4.0 = 1028.0 + // CHECK: %[[CST:.*]] = arith.constant dense<1.028000e+03> : tensor<1024xf32> + // CHECK: return %[[CST]] +} + +// CHECK-LABEL: @ConstantFoldFullyConnectedNoBias +func.func @ConstantFoldFullyConnectedNoBias() -> tensor<1024xf32> { + %cst_input = arith.constant dense<1.0> : tensor<512xf32> + %cst_weights = arith.constant dense<2.0> : tensor<1024x512xf32> + %cst_bias = "tfl.no_value"() {value = unit} : () -> none + + %0 = "tfl.fully_connected" (%cst_input, %cst_weights, %cst_bias) {fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"} : (tensor<512xf32>, tensor<1024x512xf32>, none) -> tensor<1024xf32> + + func.return %0 : tensor<1024xf32> + + // 1.0 * 2.0 * 512 = 1024.0 + // CHECK: %[[CST:.*]] = arith.constant dense<1.024000e+03> : tensor<1024xf32> + // CHECK: return %[[CST]] +} + +// CHECK-LABEL: @NoFoldFullyConnectedNonFloat +func.func @NoFoldFullyConnectedNonFloat() -> tensor<1024xf32> { + %cst_input = arith.constant dense<1.0> : tensor<512xf32> + %cst_weights = arith.constant dense<2> : tensor<1024x512xi8> + %cst_bias = arith.constant dense<4.0> : tensor<1024xf32> + + %0 = "tfl.fully_connected" (%cst_input, %cst_weights, %cst_bias) {fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"} : (tensor<512xf32>, tensor<1024x512xi8>, tensor<1024xf32>) -> tensor<1024xf32> + + func.return %0 : tensor<1024xf32> + // CHECK-DAG: %[[CST:.*]] = arith.constant dense<1.000000e+00> : tensor<512xf32> + // CHECK-DAG: %[[CST_0:.*]] = arith.constant dense<2> : tensor<1024x512xi8> + // CHECK-DAG: %[[CST_1:.*]] = arith.constant dense<4.000000e+00> : tensor<1024xf32> + // CHECK: %[[VAL:.*]] = "tfl.fully_connected"(%[[CST]], %[[CST_0]], %[[CST_1]]) <{fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"}> : (tensor<512xf32>, tensor<1024x512xi8>, tensor<1024xf32>) -> tensor<1024xf32> + // CHECK: return %[[VAL]] : tensor<1024xf32> +} + +// CHECK-LABEL: @ConstantFoldFullyConnectedHighRank +func.func @ConstantFoldFullyConnectedHighRank() -> tensor<2x1024xf32> { + %cst_input = arith.constant dense<1.0> : tensor<2x512xf32> + %cst_weights = arith.constant dense<2.0> : tensor<1024x512xf32> + %cst_bias = arith.constant dense<4.0> : tensor<1024xf32> + + %0 = "tfl.fully_connected" (%cst_input, %cst_weights, %cst_bias) {fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"} : (tensor<2x512xf32>, tensor<1024x512xf32>, tensor<1024xf32>) -> tensor<2x1024xf32> + + func.return %0 : tensor<2x1024xf32> + // 1.0 * 2.0 * 512 + 4.0 = 1028.0 + // CHECK: %[[CST:.*]] = arith.constant dense<1.028000e+03> : tensor<2x1024xf32> + // CHECK: return %[[CST]] +} + +// CHECK-LABEL: @ConstantFoldFullyConnectedCheckPrecision +func.func @ConstantFoldFullyConnectedCheckPrecision() -> tensor<1xf32> { + %cst_input = arith.constant dense<1.0> : tensor<4xf32> + %cst_weights = arith.constant dense<[[1.0, 1.0e38, 1.0, -1.0e38]]> : tensor<1x4xf32> + %cst_bias = arith.constant dense<0.0> : tensor<1xf32> + + %0 = "tfl.fully_connected" (%cst_input, %cst_weights, %cst_bias) {fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"} : (tensor<4xf32>, tensor<1x4xf32>, tensor<1xf32>) -> tensor<1xf32> + + func.return %0 : tensor<1xf32> + // CHECK: %[[CST:.*]] = arith.constant dense<2.000000e+00> : tensor<1xf32> + // CHECK: return %[[CST]] +} + +// CHECK-LABEL: fully_connected_with_unit_dim +func.func @fully_connected_with_unit_dim() -> tensor<1x5xf32> { + %0 = "tfl.pseudo_const"() <{value = dense<1.0> : tensor<1x5xf32>}> : () -> tensor<1x5xf32> + %1 = "tfl.pseudo_const"() <{value = dense<1.0> : tensor<5x5xf32>}> : () -> tensor<5x5xf32> + %2 = "tfl.pseudo_const"() <{value = dense<1.0> : tensor<1x5xf32>}> : () -> tensor<1x5xf32> + %3 = "tfl.fully_connected"(%0, %1, %2) <{asymmetric_quantize_inputs = false, fused_activation_function = "NONE", keep_num_dims = true, weights_format = "DEFAULT"}> : (tensor<1x5xf32>, tensor<5x5xf32>, tensor<1x5xf32>) -> tensor<1x5xf32> + return %3 : tensor<1x5xf32> +} + +// CHECK: %cst = arith.constant dense<6.000000e+00> : tensor<1x5xf32> +// CHECK-NOT: fully_connected + +// CHECK-LABEL: @ConstantFoldFullyConnectedBatched +func.func @ConstantFoldFullyConnectedBatched() -> tensor<13x1536xf32> { + %cst_input = arith.constant dense<1.0> : tensor<13x1536xf32> + %cst_weights = arith.constant dense<1.0> : tensor<1536x1536xf32> + %cst_bias = "tfl.no_value"() {value = unit} : () -> none + + %0 = "tfl.fully_connected" (%cst_input, %cst_weights, %cst_bias) {fused_activation_function = "NONE", keep_num_dims = true, weights_format = "DEFAULT"} : (tensor<13x1536xf32>, tensor<1536x1536xf32>, none) -> tensor<13x1536xf32> + func.return %0 : tensor<13x1536xf32> + + // 1.0 * 1.0 * 1536 = 1536.0 + // CHECK: %[[CST:.*]] = arith.constant dense<1.536000e+03> : tensor<13x1536xf32> + // CHECK: return %[[CST]] +} + +// CHECK-LABEL: @ShapeOpI32 +func.func @ShapeOpI32(%arg0 : tensor<576x72xf32>) -> tensor<2xi32> { + %0 = "tfl.shape"(%arg0) : (tensor<576x72xf32>) -> tensor<2xi32> + func.return %0 : tensor<2xi32> + // CHECK: %[[CST:.*]] = arith.constant dense<[576, 72]> : tensor<2xi32> + // CHECK: return %[[CST]] +} + +// CHECK-LABEL: @ShapeOpI64 +func.func @ShapeOpI64(%arg0 : tensor<576x72xf32>) -> tensor<2xi64> { + %0 = "tfl.shape"(%arg0) : (tensor<576x72xf32>) -> tensor<2xi64> + func.return %0 : tensor<2xi64> + // CHECK: %[[CST:.*]] = arith.constant dense<[576, 72]> : tensor<2xi64> + // CHECK: return %[[CST]] +} + +// CHECK-LABEL: @ConstFoldStridedSlice +func.func @ConstFoldStridedSlice(%arg0 : tensor<15600xf32>) -> tensor<15600xf32> { + %0 = "tfl.pseudo_const"() {value = dense<15600> : tensor<1xi32>} : () -> tensor<1xi32> + %1 = "tfl.pseudo_const"() {value = dense<0> : tensor<1xi32>} : () -> tensor<1xi32> + %2 = "tfl.pseudo_const"() {value = dense<1> : tensor<1xi32>} : () -> tensor<1xi32> + %3 = "tfl.strided_slice"(%arg0, %1, %0, %2) {begin_mask = 0 : i32, ellipsis_mask = 0 : i32, end_mask = 0 : i32, new_axis_mask = 0 : i32, shrink_axis_mask = 0 : i32, offset = false} : (tensor<15600xf32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<15600xf32> + func.return %3 : tensor<15600xf32> + // CHECK: return %arg0 +} + +func.func @ConstFoldStridedSliceMultiDims(%arg0 : tensor<10x10x10xf32>) -> tensor<10x10x10xf32> { + %0 = "tfl.pseudo_const"() {value = dense<[10, 10, 10]> : tensor<3xi32>} : () -> tensor<3xi32> + %1 = "tfl.pseudo_const"() {value = dense<0> : tensor<3xi32>} : () -> tensor<3xi32> + %2 = "tfl.pseudo_const"() {value = dense<1> : tensor<3xi32>} : () -> tensor<3xi32> + %3 = "tfl.strided_slice"(%arg0, %1, %0, %2) {begin_mask = 0 : i32, ellipsis_mask = 0 : i32, end_mask = 0 : i32, new_axis_mask = 0 : i32, shrink_axis_mask = 0 : i32, offset = false} : (tensor<10x10x10xf32>, tensor<3xi32>, tensor<3xi32>, tensor<3xi32>) -> tensor<10x10x10xf32> + func.return %3 : tensor<10x10x10xf32> + // CHECK: return %arg0 +} + +func.func @NotFoldStridedSlice(%arg0 : tensor<10x10x10xf32>) -> tensor<9x9x9xf32> { + %0 = "tfl.pseudo_const"() {value = dense<[9, 9, 9]> : tensor<3xi32>} : () -> tensor<3xi32> + %1 = "tfl.pseudo_const"() {value = dense<0> : tensor<3xi32>} : () -> tensor<3xi32> + %2 = "tfl.pseudo_const"() {value = dense<1> : tensor<3xi32>} : () -> tensor<3xi32> + %3 = "tfl.strided_slice"(%arg0, %1, %0, %2) {begin_mask = 0 : i32, ellipsis_mask = 0 : i32, end_mask = 0 : i32, new_axis_mask = 0 : i32, shrink_axis_mask = 0 : i32, offset = false} : (tensor<10x10x10xf32>, tensor<3xi32>, tensor<3xi32>, tensor<3xi32>) -> tensor<9x9x9xf32> + func.return %3 : tensor<9x9x9xf32> + // CHECK: %[[STRIDED_SLICE:.*]] = "tfl.strided_slice" + // CHECK: return %[[STRIDED_SLICE]] +} + +func.func @ConstFoldPad(%arg0: tensor<15600xf32>) -> tensor<15600xf32> { + %0 = "tfl.pseudo_const"() {value = dense<0> : tensor<1x2xi32>} : () -> tensor<1x2xi32> + %1 = "tfl.pad"(%arg0, %0) : (tensor<15600xf32>, tensor<1x2xi32>) -> tensor<15600xf32> + func.return %1 : tensor<15600xf32> + // CHECK: return %arg0 +} + +func.func @ConstFoldPadV2(%arg0: tensor<15600xf32>) -> tensor<15600xf32> { + %0 = "tfl.pseudo_const"() {value = dense<0> : tensor<1x2xi32>} : () -> tensor<1x2xi32> + %1 = "tfl.pseudo_const"() {value = dense<0.0> : tensor} : () -> tensor + %2 = "tfl.padv2"(%arg0, %0, %1) : (tensor<15600xf32>, tensor<1x2xi32>, tensor) -> tensor<15600xf32> + func.return %2 : tensor<15600xf32> + // CHECK: return %arg0 +} + +// CHECK-LABEL: @ConstFoldEmbeddingLookup +func.func @ConstFoldEmbeddingLookup() -> (tensor<5x2xf32>, tensor<3x2x2xf32>) { + %index0 = "tfl.pseudo_const"() {value = dense<[2, 1, 0, 0, 2]> : tensor<5xi32>} : () -> tensor<5xi32> + %index1 = "tfl.pseudo_const"() {value = dense<[0, 1, 0]> : tensor<3xi32>} : () -> tensor<3xi32> + %value0 = "tfl.pseudo_const"() {value = dense<[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]> : tensor<3x2xf32>} : () -> tensor<3x2xf32> + %value1 = "tfl.pseudo_const"() {value = dense<[[[1.0, 2.0], [3.0, 4.0]], [[5.0, 6.0], [7.0, 8.0]]]> : tensor<2x2x2xf32>} : () -> tensor<2x2x2xf32> + %lookup0 = "tfl.embedding_lookup"(%index0, %value0) : (tensor<5xi32>, tensor<3x2xf32>) -> tensor<5x2xf32> + %lookup1 = "tfl.embedding_lookup"(%index1, %value1) : (tensor<3xi32>, tensor<2x2x2xf32>) -> tensor<3x2x2xf32> + func.return %lookup0, %lookup1 : tensor<5x2xf32>, tensor<3x2x2xf32> + + // CHECK-DAG: %[[LOOKUP0:.*]] = arith.constant dense<{{\[\[}}5.000000e+00, 6.000000e+00], [3.000000e+00, 4.000000e+00], [1.000000e+00, 2.000000e+00], [1.000000e+00, 2.000000e+00], [5.000000e+00, 6.000000e+00]]> : tensor<5x2xf32> + // CHECK-DAG: %[[LOOKUP1:.*]] = arith.constant dense<{{\[\[\[}}1.000000e+00, 2.000000e+00], [3.000000e+00, 4.000000e+00]], {{\[\[}}5.000000e+00, 6.000000e+00], [7.000000e+00, 8.000000e+00]], {{\[\[}}1.000000e+00, 2.000000e+00], [3.000000e+00, 4.000000e+00]]]> : tensor<3x2x2xf32> + // CHECK: return %[[LOOKUP0]], %[[LOOKUP1]] : tensor<5x2xf32>, tensor<3x2x2xf32> +} + +// CHECK-LABEL: @less_int_both_splat +func.func @less_int_both_splat() -> tensor<4xi1> { + %0 = arith.constant dense<3> : tensor<4xi32> + %1 = arith.constant dense<10> : tensor<4xi32> + + %2 = "tfl.less"(%0, %1) : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi1> + + func.return %2 : tensor<4xi1> +} + +// CHECK: %cst = arith.constant dense : tensor<4xi1> + +// CHECK-LABEL: @less_int_one_splat +func.func @less_int_one_splat() -> tensor<4xi1> { + %0 = arith.constant dense<3> : tensor<4xi32> + %1 = arith.constant dense<[10, 2, -1, 3]> : tensor<4xi32> + + %2 = "tfl.less"(%0, %1) : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi1> + + func.return %2 : tensor<4xi1> +} + +// CHECK:%cst = arith.constant dense<[true, false, false, false]> : tensor<4xi1> + +// CHECK-LABEL: @less_int +func.func @less_int() -> tensor<4xi1> { + %0 = arith.constant dense<[11, 2, 0, 2]> : tensor<4xi32> + %1 = arith.constant dense<[10, 2, -1, 3]> : tensor<4xi32> + + %2 = "tfl.less"(%0, %1) : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi1> + + func.return %2 : tensor<4xi1> +} + +// CHECK: %cst = arith.constant dense<[false, false, false, true]> : tensor<4xi1> + +// CHECK-LABEL: @less_int64 +func.func @less_int64() -> tensor<4xi1> { + %0 = arith.constant dense<[11, 2, 0, 2]> : tensor<4xi64> + %1 = arith.constant dense<[10, 2, -1, 3]> : tensor<4xi64> + + %2 = "tfl.less"(%0, %1) : (tensor<4xi64>, tensor<4xi64>) -> tensor<4xi1> + + func.return %2 : tensor<4xi1> +} + +// CHECK: %cst = arith.constant dense<[false, false, false, true]> : tensor<4xi1> + +// CHECK-LABEL: @less_float +func.func @less_float() -> tensor<4xi1> { + %0 = arith.constant dense<[11.0, 2.0, 0.0, 2.0]> : tensor<4xf32> + %1 = arith.constant dense<[10.0, 2.0, -1.0, 3.0]> : tensor<4xf32> + + %2 = "tfl.less"(%0, %1) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xi1> + + func.return %2 : tensor<4xi1> +} + +// CHECK: %cst = arith.constant dense<[false, false, false, true]> : tensor<4xi1> + +// CHECK-LABEL: @less_equal_int +func.func @less_equal_int() -> tensor<4xi1> { + %0 = arith.constant dense<[11, 2, 0, 2]> : tensor<4xi32> + %1 = arith.constant dense<[10, 2, -1, 3]> : tensor<4xi32> + + %2 = "tfl.less_equal"(%0, %1) : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi1> + + func.return %2 : tensor<4xi1> +} + +// CHECK: %cst = arith.constant dense<[false, true, false, true]> : tensor<4xi1> + +// CHECK-LABEL: @less_equal_int64 +func.func @less_equal_int64() -> tensor<4xi1> { + %0 = arith.constant dense<[11, 2, 0, 2]> : tensor<4xi64> + %1 = arith.constant dense<[10, 2, -1, 3]> : tensor<4xi64> + + %2 = "tfl.less_equal"(%0, %1) : (tensor<4xi64>, tensor<4xi64>) -> tensor<4xi1> + + func.return %2 : tensor<4xi1> +} + +// CHECK: %cst = arith.constant dense<[false, true, false, true]> : tensor<4xi1> + +// CHECK-LABEL: @less_equal_float +func.func @less_equal_float() -> tensor<4xi1> { + %0 = arith.constant dense<[11.0, 2.0, 0.0, 2.0]> : tensor<4xf32> + %1 = arith.constant dense<[10.0, 2.0, -1.0, 3.0]> : tensor<4xf32> + + %2 = "tfl.less_equal"(%0, %1) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xi1> + + func.return %2 : tensor<4xi1> +} + +// CHECK: %cst = arith.constant dense<[false, true, false, true]> : tensor<4xi1> + +// CHECK-LABEL: @greater_int +func.func @greater_int() -> tensor<4xi1> { + %0 = arith.constant dense<[11, 2, 0, 2]> : tensor<4xi32> + %1 = arith.constant dense<[10, 2, -1, 3]> : tensor<4xi32> + + %2 = "tfl.greater"(%0, %1) : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi1> + + func.return %2 : tensor<4xi1> +} + +// CHECK: %cst = arith.constant dense<[true, false, true, false]> : tensor<4xi1> + +// CHECK-LABEL: @greater_int64 +func.func @greater_int64() -> tensor<4xi1> { + %0 = arith.constant dense<[11, 2, 0, 2]> : tensor<4xi64> + %1 = arith.constant dense<[10, 2, -1, 3]> : tensor<4xi64> + + %2 = "tfl.greater"(%0, %1) : (tensor<4xi64>, tensor<4xi64>) -> tensor<4xi1> + + func.return %2 : tensor<4xi1> +} + +// CHECK: %cst = arith.constant dense<[true, false, true, false]> : tensor<4xi1> + +// CHECK-LABEL: @greater_float +func.func @greater_float() -> tensor<4xi1> { + %0 = arith.constant dense<[11.0, 2.0, 0.0, 2.0]> : tensor<4xf32> + %1 = arith.constant dense<[10.0, 2.0, -1.0, 3.0]> : tensor<4xf32> + + %2 = "tfl.greater"(%0, %1) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xi1> + + func.return %2 : tensor<4xi1> +} + +// CHECK: %cst = arith.constant dense<[true, false, true, false]> : tensor<4xi1> + +// CHECK-LABEL: @greater_equal_int +func.func @greater_equal_int() -> tensor<4xi1> { + %0 = arith.constant dense<[11, 2, 0, 2]> : tensor<4xi32> + %1 = arith.constant dense<[10, 2, -1, 3]> : tensor<4xi32> + + %2 = "tfl.greater_equal"(%0, %1) : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi1> + + func.return %2 : tensor<4xi1> +} + +// CHECK: %cst = arith.constant dense<[true, true, true, false]> : tensor<4xi1> + +// CHECK-LABEL: @greater_equal_int64 +func.func @greater_equal_int64() -> tensor<4xi1> { + %0 = arith.constant dense<[11, 2, 0, 2]> : tensor<4xi64> + %1 = arith.constant dense<[10, 2, -1, 3]> : tensor<4xi64> + + %2 = "tfl.greater_equal"(%0, %1) : (tensor<4xi64>, tensor<4xi64>) -> tensor<4xi1> + + func.return %2 : tensor<4xi1> +} + +// CHECK: %cst = arith.constant dense<[true, true, true, false]> : tensor<4xi1> + +// CHECK-LABEL: @greater_equal_float +func.func @greater_equal_float() -> tensor<4xi1> { + %0 = arith.constant dense<[11.0, 2.0, 0.0, 2.0]> : tensor<4xf32> + %1 = arith.constant dense<[10.0, 2.0, -1.0, 3.0]> : tensor<4xf32> + + %2 = "tfl.greater_equal"(%0, %1) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xi1> + + func.return %2 : tensor<4xi1> +} + +// CHECK: %cst = arith.constant dense<[true, true, true, false]> : tensor<4xi1> + +// CHECK-LABEL: @equal_int +func.func @equal_int() -> tensor<4xi1> { + %0 = arith.constant dense<[11, 2, 0, 2]> : tensor<4xi32> + %1 = arith.constant dense<[10, 2, -1, 3]> : tensor<4xi32> + + %2 = "tfl.equal"(%0, %1) : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi1> + + func.return %2 : tensor<4xi1> +} + +// CHECK: %cst = arith.constant dense<[false, true, false, false]> : tensor<4xi1> + +// CHECK-LABEL: @equal_int64 +func.func @equal_int64() -> tensor<4xi1> { + %0 = arith.constant dense<[11, 2, 0, 2]> : tensor<4xi64> + %1 = arith.constant dense<[10, 2, -1, 3]> : tensor<4xi64> + + %2 = "tfl.equal"(%0, %1) : (tensor<4xi64>, tensor<4xi64>) -> tensor<4xi1> + + func.return %2 : tensor<4xi1> +} + +// CHECK: %cst = arith.constant dense<[false, true, false, false]> : tensor<4xi1> + +// CHECK-LABEL: @equal_float +func.func @equal_float() -> tensor<4xi1> { + %0 = arith.constant dense<[11.0, 2.0, 0.0, 2.0]> : tensor<4xf32> + %1 = arith.constant dense<[10.0, 2.0, -1.0, 3.0]> : tensor<4xf32> + + %2 = "tfl.equal"(%0, %1) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xi1> + + func.return %2 : tensor<4xi1> +} + +// CHECK: %cst = arith.constant dense<[false, true, false, false]> : tensor<4xi1> + +// CHECK-LABEL: @not_equal_int +func.func @not_equal_int() -> tensor<4xi1> { + %0 = arith.constant dense<[11, 2, 0, 2]> : tensor<4xi32> + %1 = arith.constant dense<[10, 2, -1, 3]> : tensor<4xi32> + + %2 = "tfl.not_equal"(%0, %1) : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi1> + + func.return %2 : tensor<4xi1> +} + +// CHECK: %cst = arith.constant dense<[true, false, true, true]> : tensor<4xi1> + +// CHECK-LABEL: @not_equal_int64 +func.func @not_equal_int64() -> tensor<4xi1> { + %0 = arith.constant dense<[11, 2, 0, 2]> : tensor<4xi64> + %1 = arith.constant dense<[10, 2, -1, 3]> : tensor<4xi64> + + %2 = "tfl.not_equal"(%0, %1) : (tensor<4xi64>, tensor<4xi64>) -> tensor<4xi1> + + func.return %2 : tensor<4xi1> +} + +// CHECK: %cst = arith.constant dense<[true, false, true, true]> : tensor<4xi1> + +// CHECK-LABEL: @not_equal_float +func.func @not_equal_float() -> tensor<4xi1> { + %0 = arith.constant dense<[11.0, 2.0, 0.0, 2.0]> : tensor<4xf32> + %1 = arith.constant dense<[10.0, 2.0, -1.0, 3.0]> : tensor<4xf32> + + %2 = "tfl.not_equal"(%0, %1) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xi1> + + func.return %2 : tensor<4xi1> +} + +// CHECK: %cst = arith.constant dense<[true, false, true, true]> : tensor<4xi1> + +// CHECK-LABEL: @logical_or +func.func @logical_or() -> tensor<3xi1> { + %0 = arith.constant dense<[true, false, true]> : tensor<3xi1> + %1 = arith.constant dense<[false, false, true]> : tensor<3xi1> + + %2 = "tfl.logical_or"(%0, %1) : (tensor<3xi1>, tensor<3xi1>) -> tensor<3xi1> + + func.return %2 : tensor<3xi1> +} + +// CHECK: %cst = arith.constant dense<[true, false, true]> : tensor<3xi1> + +// CHECK-LABEL: @logical_and +func.func @logical_and() -> tensor<3xi1> { + %0 = arith.constant dense<[true, false, true]> : tensor<3xi1> + %1 = arith.constant dense<[false, false, true]> : tensor<3xi1> + + %2 = "tfl.logical_and"(%0, %1) : (tensor<3xi1>, tensor<3xi1>) -> tensor<3xi1> + + func.return %2 : tensor<3xi1> +} + +// CHECK: %cst = arith.constant dense<[false, false, true]> : tensor<3xi1> + +// CHECK-LABEL: @select_splat_cond +func.func @select_splat_cond() -> tensor<4xi32> { + %cond = arith.constant dense : tensor<4xi1> + %0 = arith.constant dense<[1, 2, 3, 4]> : tensor<4xi32> + %1 = arith.constant dense<[-1, -2, -3, -4]> : tensor<4xi32> + + %2 = "tfl.select"(%cond, %0, %1) : (tensor<4xi1>, tensor<4xi32>, tensor<4xi32>) -> tensor<4xi32> + + func.return %2 : tensor<4xi32> +} + +// CHECK: %cst = arith.constant dense<[1, 2, 3, 4]> : tensor<4xi32> + +// CHECK-LABEL: select_splat_lhs +func.func @select_splat_lhs() -> tensor<4xi32> { + %cond = arith.constant dense<[true, true, false, false]> : tensor<4xi1> + %0 = arith.constant dense<0> : tensor<4xi32> + %1 = arith.constant dense<[-1, -2, -3, -4]> : tensor<4xi32> + + %2 = "tfl.select"(%cond, %0, %1) : (tensor<4xi1>, tensor<4xi32>, tensor<4xi32>) -> tensor<4xi32> + + func.return %2 : tensor<4xi32> +} + +// CHECK: %cst = arith.constant dense<[0, 0, -3, -4]> : tensor<4xi32> + +// CHECK-LABEL: select_float +func.func @select_float() -> tensor<4xf32> { + %cond = arith.constant dense<[true, true, false, false]> : tensor<4xi1> + %0 = arith.constant dense<[1.0, 2.0, 3.0, 4.0]> : tensor<4xf32> + %1 = arith.constant dense<[-1.0, -2.0, -3.0, -4.0]> : tensor<4xf32> + + %2 = "tfl.select"(%cond, %0, %1) : (tensor<4xi1>, tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> + + func.return %2 : tensor<4xf32> +} +// CHECK: %cst = arith.constant dense<[1.000000e+00, 2.000000e+00, -3.000000e+00, -4.000000e+00]> : tensor<4xf32 + +// CHECK-LABEL: ceil +func.func @ceil() -> tensor<3xf32> { + %cst = arith.constant dense<[-1.0, 0.0, 0.99]> : tensor<3xf32> + %0 = "tfl.ceil"(%cst) : (tensor<3xf32>) -> tensor<3xf32> + func.return %0 : tensor<3xf32> +} +// CHECK: %cst = arith.constant dense<[-1.000000e+00, 0.000000e+00, 1.000000e+00]> : tensor<3xf32> + +// CHECK-LABEL: ceil_f64 +func.func @ceil_f64() -> tensor<3xf64> { + %cst = arith.constant dense<[-1.0, 0.0, 0.99]> : tensor<3xf64> + %0 = "tfl.ceil"(%cst) : (tensor<3xf64>) -> tensor<3xf64> + func.return %0 : tensor<3xf64> +} +// CHECK: tfl.ceil + +// CHECK-LABEL: floor +func.func @floor() -> tensor<3xf32> { + %cst = arith.constant dense<[-1.0, 0.0, 0.99]> : tensor<3xf32> + %0 = "tfl.floor"(%cst) : (tensor<3xf32>) -> tensor<3xf32> + func.return %0 : tensor<3xf32> +} +// CHECK: %cst = arith.constant dense<[-1.000000e+00, 0.000000e+00, 0.000000e+00]> : tensor<3xf32> + +// CHECK-LABEL: floor_f64 +func.func @floor_f64() -> tensor<3xf64> { + %cst = arith.constant dense<[-1.0, 0.0, 0.99]> : tensor<3xf64> + %0 = "tfl.floor"(%cst) : (tensor<3xf64>) -> tensor<3xf64> + func.return %0 : tensor<3xf64> +} +// CHECK: tfl.floor + +// CHECK-LABEL: exp +func.func @exp() -> tensor<4xf32> { + %cst = arith.constant dense<[-1.0, 0.0, 0.99, 0.36787944117]> : tensor<4xf32> + %0 = "tfl.exp"(%cst) : (tensor<4xf32>) -> tensor<4xf32> + func.return %0 : tensor<4xf32> +} + +// CHECK: %cst = arith.constant dense<[0.36787945, 1.000000e+00, 2.69123459, 1.44466782]> : tensor<4xf32> + +// CHECK-LABEL: exp_f64 +func.func @exp_f64() -> tensor<4xf64> { + %cst = arith.constant dense<[-1.0, 0.0, 0.99, 0.36787944117]> : tensor<4xf64> + %0 = "tfl.exp"(%cst) : (tensor<4xf64>) -> tensor<4xf64> + func.return %0 : tensor<4xf64> +} + +// CHECK: tfl.exp + +// CHECK-LABEL: pow_float +func.func @pow_float() -> tensor<3xf32> { + %0 = arith.constant dense<[1.0, 0.0, 2.0]> : tensor<3xf32> + %1 = arith.constant dense<[2.0, 3.0, -1.5]> : tensor<3xf32> + + %2 = "tfl.pow"(%0, %1) : (tensor<3xf32>, tensor<3xf32>) -> tensor<3xf32> + + func.return %2 : tensor<3xf32> +} + +// CHECK: %cst = arith.constant dense<[1.000000e+00, 0.000000e+00, 0.353553385]> : tensor<3xf32> + +// CHECK-LABEL: pow_int +func.func @pow_int() -> tensor<3xi32> { + %0 = arith.constant dense<[1, 0, 2]> : tensor<3xi32> + %1 = arith.constant dense<[2, 3, -1]> : tensor<3xi32> + + %2 = "tfl.pow"(%0, %1) : (tensor<3xi32>, tensor<3xi32>) -> tensor<3xi32> + + func.return %2 : tensor<3xi32> +} + +// CHECK: %cst = arith.constant dense<[1, 0, 0]> : tensor<3xi32> + +// CHECK-LABEL: logical_not +func.func @logical_not() -> tensor<3xi1> { + %cst = arith.constant dense<[false, true, false]> : tensor<3xi1> + %0 = "tfl.logical_not"(%cst) : (tensor<3xi1>) -> tensor<3xi1> + func.return %0 : tensor<3xi1> +} + +// CHECK: %cst = arith.constant dense<[true, false, true]> : tensor<3xi1> + +// CHECK-LABEL: logical_not_splat +func.func @logical_not_splat() -> tensor<3xi1> { + %cst = arith.constant dense : tensor<3xi1> + %0 = "tfl.logical_not"(%cst) : (tensor<3xi1>) -> tensor<3xi1> + func.return %0 : tensor<3xi1> +} + +// CHECK: %cst = arith.constant dense : tensor<3xi1> + +// CHECK-LABEL: bitwise_xor_i32 +func.func @bitwise_xor_i32() -> tensor<3xi32> { + %0 = arith.constant dense<[0, 5, 3]> : tensor<3xi32> + %1 = arith.constant dense<[5, 0, 7]> : tensor<3xi32> + + %2 = "tfl.bitwise_xor"(%0, %1) : (tensor<3xi32>, tensor<3xi32>) -> tensor<3xi32> + + func.return %2 : tensor<3xi32> +} + +// CHECK: %cst = arith.constant dense<[5, 5, 4]> : tensor<3xi32> + +// CHECK-LABEL: bitwise_xor_i8 +func.func @bitwise_xor_i8() -> tensor<3xi8> { + %0 = arith.constant dense<[0, 5, 3]> : tensor<3xi8> + %1 = arith.constant dense<[5, 0, 7]> : tensor<3xi8> + + %2 = "tfl.bitwise_xor"(%0, %1) : (tensor<3xi8>, tensor<3xi8>) -> tensor<3xi8> + + func.return %2 : tensor<3xi8> +} + +// CHECK: %cst = arith.constant dense<[5, 5, 4]> : tensor<3xi8> + +// CHECK-LABEL: relu +func.func @relu() -> tensor<3xf32> { + %cst = arith.constant dense<[-1.0, 0.0, 0.99]> : tensor<3xf32> + %0 = "tfl.relu"(%cst) : (tensor<3xf32>) -> tensor<3xf32> + func.return %0 : tensor<3xf32> +} + +// CHECK: %cst = arith.constant dense<[0.000000e+00, 0.000000e+00, 9.900000e-01]> : tensor<3xf32> + +// CHECK-LABEL: slice +func.func @slice_first_dim() -> tensor<1x1x5x6xf32> { + %cst_0 = arith.constant dense<9.000000e+00> : tensor<2x1x5x6xf32> + %cst_1 = arith.constant dense<0> : tensor<4xi32> + %cst_2 = arith.constant dense<[1, 1, 5, 6]> : tensor<4xi32> + %0 = "tfl.slice"(%cst_0, %cst_1, %cst_2) : (tensor<2x1x5x6xf32>, tensor<4xi32>, tensor<4xi32>) -> tensor<1x1x5x6xf32> + func.return %0 : tensor<1x1x5x6xf32> +} + +// CHECK %cst = arith.constant dense<9.000000e+00> : tensor<1x1x5x6xf32> + +// CHECK-LABEL: slice_trivial +func.func @slice_trivial(%arg0: tensor<2x1x5x6xf32>) -> tensor<2x1x5x6xf32> { + %cst_1 = arith.constant dense<0> : tensor<4xi32> + %cst_2 = arith.constant dense<[2, 1, 5, 6]> : tensor<4xi32> + %0 = "tfl.slice"(%arg0, %cst_1, %cst_2) : (tensor<2x1x5x6xf32>, tensor<4xi32>, tensor<4xi32>) -> tensor<2x1x5x6xf32> + func.return %0 : tensor<2x1x5x6xf32> +} + +// CHECK-NOT: tfl.slice + + +// CHECK-LABEL: sum +func.func @sum() -> tensor<2xf32> { + %cst = arith.constant dense<[0, 1]> : tensor<2xi32> + %cst_1 = arith.constant dense<[[[0.0, 1.0], [2.0, 3.0]], [[4.0, 5.0], [6.0, 7.0]]]> : tensor<2x2x2xf32> + %0 = "tfl.sum"(%cst_1, %cst) <{keep_dims = false}> : (tensor<2x2x2xf32>, tensor<2xi32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> +} + +// CHECK: arith.constant dense<[1.200000e+01, 1.600000e+01]> : tensor<2xf32> + +// CHECK-LABEL: sum_keep_dims +func.func @sum_keep_dims() -> tensor<1x1x2xf32> { + %cst = arith.constant dense<[0, 1]> : tensor<2xi32> + %cst_1 = arith.constant dense<[[[0.0, 1.0], [2.0, 3.0]], [[4.0, 5.0], [6.0, 7.0]]]> : tensor<2x2x2xf32> + %0 = "tfl.sum"(%cst_1, %cst) <{keep_dims = true}> : (tensor<2x2x2xf32>, tensor<2xi32>) -> tensor<1x1x2xf32> + func.return %0 : tensor<1x1x2xf32> +} + +// CHECK-LITERAL: arith.constant dense<[[[1.200000e+01, 1.600000e+01]]]> : tensor<1x1x2xf32> + +// CHECK-LABEL: sum_general_reduction_dims +func.func @sum_general_reduction_dims() -> tensor<3xf32> { + %cst = arith.constant dense<[0, 2]> : tensor<2xi32> + %cst_1 = arith.constant dense<2.000000e+00> : tensor<1x3x2xf32> + %0 = "tfl.sum"(%cst_1, %cst) <{keep_dims = false}> : (tensor<1x3x2xf32>, tensor<2xi32>) -> tensor<3xf32> + func.return %0 : tensor<3xf32> +} + +// CHECK: %cst = arith.constant dense<4.000000e+00> : tensor<3xf32> + +// CHECK-LABEL: sum_general_reduction_dims_keep_dims +func.func @sum_general_reduction_dims_keep_dims() -> tensor<1x3x1xf32> { + %cst = arith.constant dense<[0, 2]> : tensor<2xi32> + %cst_1 = arith.constant dense<2.000000e+00> : tensor<1x3x2xf32> + %0 = "tfl.sum"(%cst_1, %cst) <{keep_dims = true}> : (tensor<1x3x2xf32>, tensor<2xi32>) -> tensor<1x3x1xf32> + func.return %0 : tensor<1x3x1xf32> +} + +// CHECK: %cst = arith.constant dense<4.000000e+00> : tensor<1x3x1xf32> + +// CHECK-LABEL: gather +func.func @gather() -> (tensor<2x3x4x5xi16>, tensor<2x3x4x5xi16>) { + %params = arith.constant dense<[ + [[[1111, 1112, 1113, 1114, 1115], + [1121, 1122, 1123, 1124, 1125], + [1131, 1132, 1133, 1134, 1135], + [1141, 1142, 1143, 1144, 1145], + [1151, 1152, 1153, 1154, 1155], + [1161, 1162, 1163, 1164, 1165]], + [[1211, 1212, 1213, 1214, 1215], + [1221, 1222, 1223, 1224, 1225], + [1231, 1232, 1233, 1234, 1235], + [1241, 1242, 1243, 1244, 1245], + [1251, 1252, 1253, 1254, 1255], + [1261, 1262, 1263, 1264, 1265]], + [[1311, 1312, 1313, 1314, 1315], + [1321, 1322, 1323, 1324, 1325], + [1331, 1332, 1333, 1334, 1335], + [1341, 1342, 1343, 1344, 1345], + [1351, 1352, 1353, 1354, 1355], + [1361, 1362, 1363, 1364, 1365]]], + [[[2111, 2112, 2113, 2114, 2115], + [2121, 2122, 2123, 2124, 2125], + [2131, 2132, 2133, 2134, 2135], + [2141, 2142, 2143, 2144, 2145], + [2151, 2152, 2153, 2154, 2155], + [2161, 2162, 2163, 2164, 2165]], + [[2211, 2212, 2213, 2214, 2215], + [2221, 2222, 2223, 2224, 2225], + [2231, 2232, 2233, 2234, 2235], + [2241, 2242, 2243, 2244, 2245], + [2251, 2252, 2253, 2254, 2255], + [2261, 2262, 2263, 2264, 2265]], + [[2311, 2312, 2313, 2314, 2315], + [2321, 2322, 2323, 2324, 2325], + [2331, 2332, 2333, 2334, 2335], + [2341, 2342, 2343, 2344, 2345], + [2351, 2352, 2353, 2354, 2355], + [2361, 2362, 2363, 2364, 2365]]]]> : tensor<2x3x6x5xi16> + %indices = arith.constant dense<[[5, 4, 3, 2], [3, 2, 1, 0]]> : tensor<2x4xi64> + %gathered = "tfl.gather"(%params, %indices) <{axis = 2 : i32, batch_dims = 1 : i32}> : (tensor<2x3x6x5xi16>, tensor<2x4xi64>) -> tensor<2x3x4x5xi16> + // This is the same tensor as the one on the CHECK line + %expected = arith.constant dense<[ + [[[1161, 1162, 1163, 1164, 1165], + [1151, 1152, 1153, 1154, 1155], + [1141, 1142, 1143, 1144, 1145], + [1131, 1132, 1133, 1134, 1135]], + [[1261, 1262, 1263, 1264, 1265], + [1251, 1252, 1253, 1254, 1255], + [1241, 1242, 1243, 1244, 1245], + [1231, 1232, 1233, 1234, 1235]], + [[1361, 1362, 1363, 1364, 1365], + [1351, 1352, 1353, 1354, 1355], + [1341, 1342, 1343, 1344, 1345], + [1331, 1332, 1333, 1334, 1335]]], + [[[2141, 2142, 2143, 2144, 2145], + [2131, 2132, 2133, 2134, 2135], + [2121, 2122, 2123, 2124, 2125], + [2111, 2112, 2113, 2114, 2115]], + [[2241, 2242, 2243, 2244, 2245], + [2231, 2232, 2233, 2234, 2235], + [2221, 2222, 2223, 2224, 2225], + [2211, 2212, 2213, 2214, 2215]], + [[2341, 2342, 2343, 2344, 2345], + [2331, 2332, 2333, 2334, 2335], + [2321, 2322, 2323, 2324, 2325], + [2311, 2312, 2313, 2314, 2315]]]]> : tensor<2x3x4x5xi16> + func.return %gathered, %expected : tensor<2x3x4x5xi16>, tensor<2x3x4x5xi16> + // CHECK-NOT: tfl.gather + // CHECK: [[CST:%.*]] = arith.constant dense<"0x89048A048B048C048D047F048004810482048304750476047704780479046B046C046D046E046F04ED04EE04EF04F004F104E304E404E504E604E704D904DA04DB04DC04DD04CF04D004D104D204D304510552055305540555054705480549054A054B053D053E053F0540054105330534053505360537055D085E085F08600861085308540855085608570849084A084B084C084D083F084008410842084308C108C208C308C408C508B708B808B908BA08BB08AD08AE08AF08B008B108A308A408A508A608A708250926092709280929091B091C091D091E091F09110912091309140915090709080909090A090B09"> : tensor<2x3x4x5xi16> + // If the return value is the same constant twice, the result is the same as expected + // CHECK: return [[CST]], [[CST]] +} + +// CHECK-LABEL: func @gather_nd_slices +func.func @gather_nd_slices() -> tensor<2x2xi32> { + %params = arith.constant dense<[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]> : tensor<2x2x2xi32> + %indices = arith.constant dense<[[0, 1], [1, 0]]> : tensor<2x2xi64> + %0 = "tfl.gather_nd"(%params, %indices) : (tensor<2x2x2xi32>, tensor<2x2xi64>) -> tensor<2x2xi32> + return %0 : tensor<2x2xi32> + + // CHECK-NOT: tfl.gather_nd + // CHECK: [[CST:%.*]] = arith.constant dense<{{\[\[}}3, 4], {{\[}}5, 6]]> : tensor<2x2xi32> + // CHECK: return [[CST]] +} + +// CHECK-LABEL: func @gather_nd_scalars +func.func @gather_nd_scalars() -> tensor<4xf32> { + %params = arith.constant dense<[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]> : tensor<3x3xf32> + %indices = arith.constant dense<[[0, 0], [2, 2], [1, 0], [2, 0]]> : tensor<4x2xi32> + %0 = "tfl.gather_nd"(%params, %indices) : (tensor<3x3xf32>, tensor<4x2xi32>) -> tensor<4xf32> + return %0 : tensor<4xf32> + + // CHECK-NOT: tfl.gather_nd + // CHECK: [[CST:%.+]] = arith.constant dense<[1.000000e+00, 9.000000e+00, 4.000000e+00, 7.000000e+00]> : tensor<4xf32> + // CHECK: return [[CST]] +} + +// CHECK-LABEL: reverse_2_dims +func.func @reverse_2_dims() -> tensor<2x3x2xi32> { + %input = "tfl.pseudo_const"() <{value = dense<[[[1, 2], [3, 4], [5, 6]], [[7, 8], [9, 10], [11, 12]]]> : tensor<2x3x2xi32>}> : () -> tensor<2x3x2xi32> + %axis = "tfl.pseudo_const"() <{value = dense<[0, 1]> : tensor<2xi32>}> : () -> tensor<2xi32> + %reverse = "tfl.reverse_v2"(%input, %axis) : (tensor<2x3x2xi32>, tensor<2xi32>) -> tensor<2x3x2xi32> + return %reverse : tensor<2x3x2xi32> +} + +// CHECK-LITERAL: %cst = arith.constant dense<[[[11, 12], [9, 10], [7, 8]], [[5, 6], [3, 4], [1, 2]]]> : tensor<2x3x2xi32> +// CHECK: return %cst : tensor<2x3x2xi32> + +// CHECK-LABEL: reverse_1_dim +func.func @reverse_1_dim() -> tensor<2x3x2xi32> { + %input = "tfl.pseudo_const"() <{value = dense<[[[1, 2], [3, 4], [5, 6]], [[7, 8], [9, 10], [11, 12]]]> : tensor<2x3x2xi32>}> : () -> tensor<2x3x2xi32> + %axis = "tfl.pseudo_const"() <{value = dense<[2]> : tensor<1xi32>}> : () -> tensor<1xi32> + %reverse = "tfl.reverse_v2"(%input, %axis) : (tensor<2x3x2xi32>, tensor<1xi32>) -> tensor<2x3x2xi32> + return %reverse : tensor<2x3x2xi32> +} + +// CHECK-LITERAL: %cst = arith.constant dense<[[[2, 1], [4, 3], [6, 5]], [[8, 7], [10, 9], [12, 11]]]> : tensor<2x3x2xi32> +// CHECK: return %cst : tensor<2x3x2xi32> + +// CHECK-LABEL: @slice_no_op +func.func @slice_no_op() -> tensor<2x3x2xi32> { + %cst_1 = arith.constant dense<[[[0, 1], [2, 3], [4, 5]], [[6, 7], [8, 9], [10, 11]]]> : tensor<2x3x2xi32> + %cst_2 = arith.constant dense<0> : tensor<3xi32> + %cst_3 = arith.constant dense<[2, 3, 2]> : tensor<3xi32> + %0 = "tfl.slice"(%cst_1, %cst_2, %cst_3) : (tensor<2x3x2xi32>, tensor<3xi32>, tensor<3xi32>) -> tensor<2x3x2xi32> + return %0 : tensor<2x3x2xi32> +} + +// CHECK-LITERAL: %cst = arith.constant dense<[[[0, 1], [2, 3], [4, 5]], [[6, 7], [8, 9], [10, 11]]]> : tensor<2x3x2xi32> +// CHECK-NOT: slice + +// CHECK-LABEL: @slice_some_dims +func.func @slice_some_dims() -> tensor<2x2x1xi32> { + %cst_1 = arith.constant dense<[[[0, 1], [2, 3], [4, 5]], [[6, 7], [8, 9], [10, 11]]]> : tensor<2x3x2xi32> + %cst_2 = arith.constant dense<[0, 1, 1]> : tensor<3xi32> + %cst_3 = arith.constant dense<[2, 2, 1]> : tensor<3xi32> + %0 = "tfl.slice"(%cst_1, %cst_2, %cst_3) : (tensor<2x3x2xi32>, tensor<3xi32>, tensor<3xi32>) -> tensor<2x2x1xi32> + return %0 : tensor<2x2x1xi32> +} + +// CHECK-LITERAL: %cst = arith.constant dense<[[[3], [5]], [[9], [11]]]> : tensor<2x2x1xi32> +// CHECK-NOT: slice + +// CHECK-LABEL: @slice_all_dims +func.func @slice_all_dims() -> tensor<1x2x1xi32> { + %cst_1 = arith.constant dense<[[[0, 1], [2, 3], [4, 5]], [[6, 7], [8, 9], [10, 11]]]> : tensor<2x3x2xi32> + %cst_2 = arith.constant dense<[1, 1, 1]> : tensor<3xi32> + %cst_3 = arith.constant dense<[1, 2, 1]> : tensor<3xi32> + %0 = "tfl.slice"(%cst_1, %cst_2, %cst_3) : (tensor<2x3x2xi32>, tensor<3xi32>, tensor<3xi32>) -> tensor<1x2x1xi32> + return %0 : tensor<1x2x1xi32> +} + +// CHECK-LITERAL: %cst = arith.constant dense<[[[9], [11]]]> : tensor<1x2x1xi32> +// CHECK-NOT: slice + +// CHECK-LABEL: @slice_some_dims_i64 +func.func @slice_some_dims_i64() -> tensor<2x2x1xi32> { + %cst_1 = arith.constant dense<[[[0, 1], [2, 3], [4, 5]], [[6, 7], [8, 9], [10, 11]]]> : tensor<2x3x2xi32> + %cst_2 = arith.constant dense<[0, 1, 1]> : tensor<3xi64> + %cst_3 = arith.constant dense<[2, 2, 1]> : tensor<3xi64> + %0 = "tfl.slice"(%cst_1, %cst_2, %cst_3) : (tensor<2x3x2xi32>, tensor<3xi64>, tensor<3xi64>) -> tensor<2x2x1xi32> + return %0 : tensor<2x2x1xi32> +} + +// CHECK-LITERAL: %cst = arith.constant dense<[[[3], [5]], [[9], [11]]]> : tensor<2x2x1xi32> +// CHECK-NOT: slice + +// CHECK-LABEL: @slice_big_float +func.func @slice_big_float() -> tensor<1x1x1792x256xf32> { + %cst_1 = arith.constant dense<9.000000e+00> : tensor<2x1x1792x256xf32> + %cst_2 = arith.constant dense<[1, 0, 0, 0]> : tensor<4xi32> + %cst_3 = arith.constant dense<[1, 1, 1792, 256]> : tensor<4xi32> + %0 = "tfl.slice"(%cst_1, %cst_2, %cst_3) : (tensor<2x1x1792x256xf32>, tensor<4xi32>, tensor<4xi32>) -> tensor<1x1x1792x256xf32> + return %0 : tensor<1x1x1792x256xf32> +} + +// CHECK-LITERAL: %cst = arith.constant dense<9.000000e+00> : tensor<1x1x1792x256xf32> +// CHECK-NOT: slice + + + diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/debuginfo/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/debuginfo/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..c5a5f857c957a9d80b333c060e62e98523529906 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/debuginfo/BUILD @@ -0,0 +1,79 @@ +load("@xla//third_party/rules_python/python:py_binary.bzl", "py_binary") +load("//tensorflow:tensorflow.bzl", "if_oss") +load("//tensorflow:tensorflow.default.bzl", "filegroup") +load("//tensorflow/compiler/mlir:glob_lit_test.bzl", "glob_lit_tests") + +# copybara:uncomment package(default_applicable_licenses = ["//tensorflow:LICENSE"]) + +licenses(["notice"]) + +glob_lit_tests( + name = "all_tests", + data = [ + ":debug_info_files", + ":test_utilities", + ], + driver = "@llvm-project//mlir:run_lit.sh", + test_file_exts = [ + "pbtxt", + # "py", TODO(b/150304798) + ], +) + +# Bundle together all the debug info files that are used by the tests. +filegroup( + name = "debug_info_files", + srcs = glob( + ["**/*.debug"], + ), +) + +# Bundle together all of the test utilities that are used by tests. +filegroup( + name = "test_utilities", + testonly = True, + data = [ + ":concrete_function_error", + ":saved_model_error", + "//tensorflow/compiler/mlir/lite:flatbuffer_to_string", + "//tensorflow/compiler/mlir/lite:tf_tfl_translate", + "@llvm-project//llvm:FileCheck", + "@llvm-project//llvm:not", + ], +) + +py_binary( + name = "saved_model_error", + srcs = ["saved_model_error.py"], + exec_properties = if_oss( + None, + select({ + "@bazel_tools//tools/cpp:asan_build": {"cpp_link.mem": "20g"}, + "//conditions:default": None, + }), + ), + main = "saved_model_error.py", + strict_deps = True, + deps = [ + "//tensorflow:tensorflow_py", + "@absl_py//absl:app", + ], +) + +py_binary( + name = "concrete_function_error", + srcs = ["concrete_function_error.py"], + exec_properties = if_oss( + None, + select({ + "@bazel_tools//tools/cpp:asan_build": {"cpp_link.mem": "20g"}, + "//conditions:default": None, + }), + ), + main = "concrete_function_error.py", + strict_deps = True, + deps = [ + "//tensorflow:tensorflow_py", + "@absl_py//absl:app", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/debuginfo/concrete_function_error.py b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/debuginfo/concrete_function_error.py new file mode 100644 index 0000000000000000000000000000000000000000..7fad30bb2915aca8a1c04454cba5055c38994f1b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/debuginfo/concrete_function_error.py @@ -0,0 +1,72 @@ +# Copyright 2019 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. +# ============================================================================== +"""Test file to display the error message and verify it with FileCheck.""" + +# RUN: %p/concrete_function_error | FileCheck %s + +import sys + +from absl import app + +import tensorflow.compat.v2 as tf +if hasattr(tf, 'enable_v2_behavior'): + tf.enable_v2_behavior() + + +class TestGraphDebugInfo(object): + """Test stack trace can be displayed.""" + + def testConcreteFunctionDebugInfo(self): + """Create a concrete func with unsupported ops, and convert it.""" + @tf.function( + input_signature=[tf.TensorSpec(shape=[3, 3], dtype=tf.float32)]) + def model(x): + y = tf.math.betainc(x, 0.5, 1.0) # Not supported + return y + y + + func = model.get_concrete_function() + converter = tf.lite.TFLiteConverter.from_concrete_functions([func], model) + converter.convert() + +# pylint: disable=line-too-long + +# CHECK-LABEL: testConcreteFunctionDebugInfo +# CHECK: error: 'tf.Betainc' op is neither a custom op nor a flex op +# CHECK: attrs=attr_protos, op_def=op_def) +# CHECK: ^ +# CHECK: {{.*tensorflow/python/ops/gen_math_ops.py:[0-9]+:[0-9]+: note: called from}} +# CHECK: "Betainc", a=a, b=b, x=x, name=name) +# CHECK: ^ +# CHECK: {{.*tensorflow/compiler/mlir/lite/tests/debuginfo/concrete_function_error.py:[0-9]+:[0-9]+: note: called from}} +# CHECK: y = tf.math.betainc(x, 0.5, 1.0) # Not supported +# CHECK: ^ +# CHECK: :0: error: failed while converting: 'main' + +# pylint: enable=line-too-long + + +def main(argv): + if len(argv) > 1: + raise app.UsageError('Too many command-line arguments.') + + try: + TestGraphDebugInfo().testConcreteFunctionDebugInfo() + except Exception as e: # pylint: disable=broad-except + sys.stdout.write('testConcreteFunctionDebugInfo') + sys.stdout.write(str(e)) + + +if __name__ == '__main__': + app.run(main) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/debuginfo/saved_model_error.py b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/debuginfo/saved_model_error.py new file mode 100644 index 0000000000000000000000000000000000000000..f05f9a7bfbd2f3bc018caca6a290220de7cea12f --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/debuginfo/saved_model_error.py @@ -0,0 +1,82 @@ +# Copyright 2019 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. +# ============================================================================== +"""Test file to display the error message and verify it with FileCheck.""" + +# RUN: %p/saved_model_error | FileCheck %s + +import sys + +from absl import app + +import tensorflow.compat.v2 as tf +if hasattr(tf, 'enable_v2_behavior'): + tf.enable_v2_behavior() + + +class TestModule(tf.Module): + """The test model has unsupported op.""" + + @tf.function(input_signature=[tf.TensorSpec(shape=[3, 3], dtype=tf.float32)]) + def model(self, x): + y = tf.math.betainc(x, 0.5, 1.0) # Not supported + return y + y + + +class TestGraphDebugInfo(object): + """Test stack trace can be displayed.""" + + def testSavedModelDebugInfo(self): + """Save a saved model with unsupported ops, and then load and convert it.""" + # saved the model + test_model = TestModule() + saved_model_path = '/tmp/test.saved_model' + save_options = tf.saved_model.SaveOptions(save_debug_info=True) + tf.saved_model.save(test_model, saved_model_path, options=save_options) + + # load the model and convert + converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_path) + converter.convert() + +# pylint: disable=line-too-long + +# CHECK-LABEL: testSavedModelDebugInfo +# CHECK: error: 'tf.Betainc' op is neither a custom op nor a flex op +# CHECK: attrs=attr_protos, op_def=op_def) +# CHECK: ^ +# CHECK: {{.*tensorflow/python/ops/gen_math_ops.py:[0-9]+:[0-9]+: note: called from}} +# CHECK: "Betainc", a=a, b=b, x=x, name=name) +# CHECK: ^ +# CHECK: {{.*tensorflow/compiler/mlir/lite/tests/debuginfo/saved_model_error.py:[0-9]+:[0-9]+: note: called from}} +# CHECK: y = tf.math.betainc(x, 0.5, 1.0) # Not supported +# CHECK: ^ +# CHECK: :0: error: failed while converting: 'main' + +# pylint: enable=line-too-long + + +def main(argv): + """test driver method writes the error message to stdout.""" + if len(argv) > 1: + raise app.UsageError('Too many command-line arguments.') + + try: + TestGraphDebugInfo().testSavedModelDebugInfo() + except Exception as e: # pylint: disable=broad-except + sys.stdout.write('testSavedModelDebugInfo') + sys.stdout.write(str(e)) + + +if __name__ == '__main__': + app.run(main) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/debuginfo/v1_1.0_224_frozen.wrong_attr.line.part.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/debuginfo/v1_1.0_224_frozen.wrong_attr.line.part.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..9ac96d94cc5f2d8a749aca06d3f3a7714b0c0f25 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/debuginfo/v1_1.0_224_frozen.wrong_attr.line.part.pbtxt @@ -0,0 +1,340 @@ +# RUN: not tf_tfl_translate -mlir-pretty-debuginfo -tf-input-arrays=input -tf-input-data-types=DT_FLOAT -tf-input-shapes=1,224,224,3 -tf-output-arrays=MobilenetV1/MobilenetV1/Conv2d_0/BatchNorm/FusedBatchNorm -tf-debug-info=%s.debug %s -o - 2>&1 | FileCheck %s + +# CHECK: fake/user/code/file_C.py:27:1: error: 'tf.Conv2D' op attribute 'data_format' failed to satisfy constraint: 'NHWC' or 'NCHW' convnet data format + +node { + name: "input" + op: "Placeholder" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: -1 + } + dim { + size: 224 + } + dim { + size: 224 + } + dim { + size: 3 + } + } + } + } +} +node { + name: "MobilenetV1/Conv2d_0/weights" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 32 + } + } + tensor_content: "\321K\0168\222\006\301\276\230J\032>L\344A7\205\214\251;\005\t2\275\3763\014\273/\r7\275\222\254Z\267\372j\'8\355]\274\266\367\227\003\277\3462|\2768]\257>\252\362C\272\'\240\013>T\317\336\275R\347%?\031\205s\276k5\010\276\270\204\306\271\320\235P\276\362\346\207\266)i\374\276J\317\220\267\030\210i\275\222\311Q>\343\007\3677\376Q\255\276\237b4\275<\324n7\226\326\357;\352\316\316\267\207!\337\276\370\206\010\276\027)\3317v\354\270<\262\'2\275i\256+:%b\372\275\323M\n\270d\014\r\267\243\016\0328\t\344N\277\344a\210=\266z_\277\274|M\272\301\206]>W\366G\275\034\3269\275)\020\"\274\204\'X\276\275\207\217\271G\350\227\276\226\035\204\266\251\001%\277ov\212\270\334\274/<)\342\010\277\034B\266\266Z}\025\277\311\375\032\275\253\005\321\267O\274\322=O\023y\270\364\223Q\276DR\r\276\275\33108\307k\016\272\002*]\274\233\352\004\274\320m\370>\225\332\330\267s<\3416/\024\0008^tw\276l\320\244>\210B\t?4\316E\272\244)l=\017\255\301\275,\213\234\275\361\251\264>\210\026p\275u\353\030\271\337\263\232\276\322m\331\270a\'J\276\203\260s\270\016V\022<\256\nW<\326\355=\2706\275\006\276\024cM\274$I\003\267\034\346&\274\306\320L8\215hX\276/;\215>,7\3206\375:\225\275\366s\031>=\374@\274\376o\007\276\357\2345\267\322\334\2208\020\013\312\267W\322\362>\363\327\232\276S\331\351>\003\021\367\2718\370\016>\0019\253>\352\305#?:\302\207\276U|\017>6\005\341\271{e\334>v\027\233\267\265\261\236\276\037\305\032\270\215\201_\275\225\257\213>D!\3067N\"\202\276lL\017=\335\222\3137\034\306\351>l\314\204\267\337\n\225\276\274(*\276zH\3237\003\347\300\275\021\007\202>M\031\351\273X\273\243\276\014=\266\2678\264\3157\033E^\267\005\320-?0]\'=\314#_\277h\3530\272@En>\371S\316>\243!\203\276\235\\Z\275\350\222\207>Hl\217\271\375%\313>6e\204\267\356t\315\276DL\216\270ZJ\t>\240\033+\277\246n\313\266J\224\341\276pG\365<\266p\351\267o\211,?\242CW\270\032G\370\275yb.\276\225\357\3157\211\245\030\275\374\324\255=\001\020y\274\200\003\025?/1\233\267\364\354\3617z\336\275\267f\255h>\211\227\220>\024\305\330>f\241&\272\314\374^=\303\344C>g\271A\276wd\271>\267\316\263=^0\031\271+\377,>/\371\347\270J\372\021\276\274\303\r\270\324\177\307=\362\007q>\274pi\270\202\013\004\276\275\030|=;\346\0036\347\025R>v\361X8\340\217\032>\211\223\324=\246\24527\205\016\202>\202\032s\275\022\260\343\272<\223r\274\024\305\033\267\216\234\2028\360\272\014\270HH\317<\244\013R\276\372\035\375=iKJ\272\227!\304\273\362\\\376<\351:\223>\321\311\207\276\240\202\274\2745\347\352\271PO\306>j\031\3356O\256\266m\356\2637\tJJ>4\340Z\274!\272\2367\222s\360>\2743\227\267X\260\364=\036\352\331\274n\227\2777\324c\254>Tv\037\276\346E\331\271\352.\205\276\236R\253\267\245\035!70(\r\267V\226\207=\220\330\276::Y\255\276\230\344\212\272Q\037d\274\352]\343\274\350\343K\276\342B\232\275+\271\244\275N\024\224\271\351V\255>\2730\2716F\205M<\377\326%\270\267\350\213>a\260\237\276\033\3743\266\016v\202>*\267\206\275\237\310\236\267\n\221\033?\014\247x\270\252\205\230=\350N\206\275\032\336\3717!\374\360=\277w\346\274,x<\274\014qm>\262\374\035\267\216\302\00581\0313\267\354J\306:\027\276S>\212bV>\345\313\211\272\257&\032<\237#\253\275\243\212B\276T%t>\2676!\275kj\357\270D\202!>k\311\247\270=\332\017\273\257\333\252\266\007\245i>)\3348>\363\036P\270\334\333\242=\304\013\326\273\017\362\3156\034^o>\241\213\210\265\\5\247\275\325\030\244>iz\322\267S>\205:.\013\031=v\016\004\272\tn\305\2753\314\030\267\037\267\3247{\311\232\267\214P\344\276\035\023\262\2762)\275>\220\310\202\272\036\310\343\276Zw\372\274\254\343\020?\262\t\254\276\270\217\363>\032\013\241\271K\006\353>y\246\324\267\221\334\217\276u\'\335\270\300\356.=1\264\224>\370\213V8k\202z>j\360\t<\031\244\2027\373\216\032\275\202H\235\270-\205a\275\352\257\034\276#\227\323\267\343\002\336;\264|p=]b`;\200\000\013\276z\021\341\267\205\2665\267%8\245\266\002Y!\277\345`6\274x\240g\277?\221\204\272/\254+\277\234\302\204\275\243w\022\276\334%:\275\372\375>?\213\326\202\271\216\344\363>\363\230\207\267J\340\305\276S\214\001\271\366\032g=\231^\014\277v\350\3007\237N\330>L\251\223=\211\257\224\267]uj\275y7\277\2701\310\361:\006\366\021\276<]\0167\201\276(\274\373\0132=\034\027d\273 l\n?\206[\350\267\250\242R\267glz\2654O-\276\374\010\211>\277\007\004?\277S\206\272\2437D\276\305\255\230\275\330\204#\276\326\271\302>o0\260>\307\206\201\271\231\335\335>^\262\004\271\304:\365\275\3054\276\270U\306\376<;HI<\2070\237\2671a\035>\300R\032=\237FD\267I\233\30298)x7v\025\017\276\316\014\343>H \261\267\226V\350\276\310\006\257>\207RM\274\315\372\360\274\271%\233\267\366Ld8\325\306\001\270\252\020\375>\250V\316\276\305e\t?\003\322y\272\205`\336\2763\261|>#\250\037?)\254\262\276\035\005\002\2770T\235\271\\\326\235\276\307\270\212\267\002\257,\276\030o\333\270l\2626>\216\330\241>\036\254\0048\377\225\013=\2267\036\274f\213\3427\313\363\331\275\221\022T\270\314\035\000\276>=\037\276lNC\267\247\242+\277\003\"\374>X\255\334\273\304\200m\276\"B\005\270\377\333\2667\361\236\2515~\310,?;\000\013\275\344`C\277\350\216\216\272\n_*\277\006\2267>^j\222\276\210.\233\275\362@U\277\274\201d\271{w\242\276\233\345\260\267\254vq\276DW\370\270\265\250\215>J\017J\277\013\305:6\371\336\343=:\226\240=\223;\246\267\313TH\276+\346\245\270\tK\204\275\260S\"\276\331_\0337\226uD\276\337\037\014>-\375d\274\306\317-?\307Q\343\2679w\265\266\234@\326\266^QA>\211\332V>\257cL>\245<\217\272\242\216$\276\376\3077=R\340\023\276)\203\314>;-\346\276\014xx\271\353u\224\275\222\254\342\270\376\021\243\275\034}\221\270nK7>[\227&>\'\016.\2704CO=\210P\232=\304\252\341\263x^=\275\227\322h\267\270\323o\275EF\304=\031L\327\266\226\301@>\351Wy\276@\004\032;^\223\243=\237I\277\267\023c\2468U\007K7\021\n+<\353\3207\276w\267#\275\r\277\224\272\303)\234\275\352\002\005>/\334\244>]\272\210\276\357X\261<\036\246\263\271\274N9\277\\[\2177L\272\232;`~\247\270\3016\234=\366Qt>\355\360\2617)\326J\274\027\246\311\274\275|\2067\024\325\237\275t\353\252\270\223\367\013\275\\\276\303\275\260\00097\374\307\206>\0346\240\276\342\312o;\356\374o\276{S\021\270\305E\3077t*18V\300+=r\273\235<\235c<\276P\"\267\272\305\344\365\275 \337\200\273\342y+\276\224\262\210\274Z\347\317=\327\336t\271`\177@\277\2270\r7=\370\216\273\310\252\276\270=\002\204>\373$\350\276\260\"\3115\355\206\267\275\371\317\221<=U\342\267\002\377\013\276]\212\313\270\204\252I\275\211\177\234\275\317\367\3427W\302\250=P)\333\275\336B\327\273?!d>\267\210\224\267\261\355)7d\014e8%\\\335;\203Tf>\243\352M>\311\314\270\272\017\\\200\2755iL\272\3742a\275\351 \267>u\213\355=\327\217j\271G`\254\276\336\217\254\270\220\341\220<\342\001%\270N\357\033>\022\343$>#\362&\270d\342\213\274\"5\026=\360OS6C(\035\275[E\2053\375y\347\274\227b\036>\352W6\270\344\342\254=\352\300L=\036j\257;\364\233\037\276\334\237\326\266\367o\2066\033\006c\267\336o\220\275\327i\013\276\217\347\215>O\336)\272J\243\306=\254\\q=:\037|>H\345\212\276P@:>\237\021\'\2715\235x\276\245\325\2266\224\2603>]\300L\270\376\255\313\274Y\220\366=\024\23248,\035\006>\032H\321;%F\2377`Vb\274\221\013b\270\254\034\030\274\333z\252\275\232M\347\267\033k\371=\347y>\274\220\217\244;\313\274\264\275\323\034\302\267\030\013!\270L\ra6\205\216\334\275TF\032=\374\273:\277d&\312\271N\305\006>\311\245\257\274\316L\372\275\354\037\302\274l\004o>\327\361\006\271\030\021]\276L\326\0007en+>It\247\270\300\302\212\275V\371:\276\316\026#\265\237\346\031>5\204\266=\331\323X\267L0\243<9Z\301\270\314_Y\2754\254\305\275-\260\313\266\201\237\025=\361\344\334<\276\225\204\273\275L\210>n\335\022\267\202/\005\270\334\225\350\265Mh\027\274\322~|>\225R\343>\177u\212\271lUR= >p=\237R.\276\3205n>;\231H\275\350\3351\271H\021%\2767\\\323\270\360\343\243=#\005x\270\206|@\275D\010S\275u\341P\270\036\366j;\007\310\335\273\250\233a\266\007\002\343\274I*\343\267\316D\025\275\271g!>\365\304 \270\241hl\276c\234y>cz\003\273Ya(\275\341\030\351\2671\347.8N\344A\267\202z\026>\342);\276\237\317\240>\211\315\313\271\207>\363=\272\264C\341`\214\276\371E\331\276\206E\'\2714\336\345\275\344\214N7\222t\277=\027\201\370\267\313X\357\275\314FD>L\344A8yDR>w\251\035=\177\27008\241\204\222;l\360\234\270\004\204\202\275\036\360\346\275\206\017/\270\333\312\200\276\3518z>\242\345\262\272j\301l\276\301\310\025\270\261\025t7\000\365\2647n\214\253=\355\014\346;]z\013\277zF\346\270\2471d>\347Z,\276\036`a\276`\232i\275\314e\005\277b\325\342\270\212\345\257\274\241\245\0216\341\333\352==3U\270\217\361\346\272v\006\340\276\032\036G7P\261\237>\372R)>\3029\0136]\367[\273\247\231\313\270\314g\220\273\034\250\253\275%\221\235\267\374\372\220\275\217m\256=\013\2260\274\243!\266>z\'\013\270$\345\024\267_@\2057\313\224+=G2$>\260\001_>L\350\272\270\222:0=\251\327\320\275;\3016\275\267\226r>e>\206v\246\001\270\277\201\236=\2570\331=\033\360\2767L\250\021;\343\037&\270J\346\307<\221\250\005\275\"e\355\267\022\3571>\'\334\020\276N\032\373;\377z\r>\014\270\325\267\221\010\3367,\303\3076Yx\252\275\236&\271\275\"\306\353\275d9\245\271\250\274+\275\223\240,=\036\0028>\327a9\276/\212\205>\017\237\031\271\334\334\241>\006\311\2447\206#\304\274\300a\205\266\315W\322\275\322\250\360=iO\0228e6\"\276\016\365S=\226\376\0108o\252\220\273 \004\311\270\rQ\265:\360\310\275o\374t;\350\224@\276DA\303\267\354\223\231\266\256\034x7r>\250\275\341Va\274T\252y<\321\231\274\270\2579\234\274m\220\024\276\344\265\324\275T\274\025=\206\214\242>\035\002\221\270\273z\311>h\004I6To5\274\244\"<\270\305\023\036>\312\2451\276\001\234\2157m\352/\276\016\033\004>\010\375\030\267\037\256\241\274-\367\312\270f\027H=)FG\275\037\273\246\267*\270\225=\334\340\222\275\371 $\274H9\031=I\222\321\267\300\'\321\267\354\241\0217K\346\364\274\021\254\020>\300\332\310=\234:>\271\210\001|:\243&f<\225]\371\341v\240<\0347\006\271c\352:>\343\371\235\270y\341\354:x\204\250\266\304\373)>}\025\032>p\234\361\267\371B\207\275_\270\276=l\256a6z\227l;" + } + } + } +} +node { + name: "MobilenetV1/Conv2d_0/weights/read" + op: "Identity" + input: "MobilenetV1/Conv2d_0/weights" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@MobilenetV1/Conv2d_0/weights" + } + } + } +} +node { + name: "MobilenetV1/MobilenetV1/Conv2d_0/Conv2D" + op: "Conv2D" + input: "input" + input: "MobilenetV1/Conv2d_0/weights/read" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NDHWC" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 2 + i: 2 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "MobilenetV1/Conv2d_0/BatchNorm/gamma" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 32 + } + } + tensor_content: "E\000\017?\025\245`?)L\201?\020Ed?#\016.?\331c\037?\327\024\246?\006v\236>Q\017w?\275\346\232>\242\002o?\203O\\?\2421\023@8\376\302>\356\224u?6\031\204?\351\302\264? P\033?\346U\206@\324[V>\346\"t?\346R\002?\360\016\232?\303\022|?\305\251~?J\220\235?1\312\r?lRs?\211\362\210?\352N\226?d\022l?\216\2432?" + } + } + } +} +node { + name: "MobilenetV1/Conv2d_0/BatchNorm/gamma/read" + op: "Identity" + input: "MobilenetV1/Conv2d_0/BatchNorm/gamma" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@MobilenetV1/Conv2d_0/BatchNorm/gamma" + } + } + } +} +node { + name: "MobilenetV1/Conv2d_0/BatchNorm/beta" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 32 + } + } + tensor_content: "\257\346\235\277K\257\033@\206\002\037=\217P\331\276\025\000\332?\200\341\022@\2348(?6,=@\036\352\273\276q\206\241\277\022Xe\277\031\'\036@j]\001@\006\272\340?\312n9?G\031O@\357\240\275\277\002.,@R\317\227>\021\236\n@\240\247\021\277\237\313\006\275K\315\r\300\217\212[@\t\363\007\300\"o\025@J\233\237@\246\001\342\276|Q!@7\310\271?w\221\273\276J\205U@" + } + } + } +} +node { + name: "MobilenetV1/Conv2d_0/BatchNorm/beta/read" + op: "Identity" + input: "MobilenetV1/Conv2d_0/BatchNorm/beta" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@MobilenetV1/Conv2d_0/BatchNorm/beta" + } + } + } +} +node { + name: "MobilenetV1/Conv2d_0/BatchNorm/moving_mean" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 32 + } + } + tensor_content: "\352\351C9\301\312?>\355\300Y>\361\347\020\267(\247G< \032\260\275\262\212\215<\2218\000\277$\362\2158w\020\233\267\360\025\217\267\326\2253;6\327\253\276\2153:\276\255\262\016;\372d\000>\263\240\367\274v\367>>Z/\335\276U0\343:\371\273\033:\273z\250:\013\271<9=&\201>,6(9\327\221\205\276+D\304=\031*i8-\310Q\273\022\273\373\275\233\231D6\302\357<\276" + } + } + } +} +node { + name: "MobilenetV1/Conv2d_0/BatchNorm/moving_mean/read" + op: "Identity" + input: "MobilenetV1/Conv2d_0/BatchNorm/moving_mean" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@MobilenetV1/Conv2d_0/BatchNorm/moving_mean" + } + } + } +} +node { + name: "MobilenetV1/Conv2d_0/BatchNorm/moving_variance" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 32 + } + } + tensor_content: "G\325\3074\"y\214?\036\014\235>V\316\3041@`b=\360\251\211>\202v6;\325\346\210?1x\3023\367\346k3\001\313\0311}\020\351=AV&?\03693?e:\3228\207P\022?\346\261G>^]\251?aY\204?\016\216F=\327g\"7!\306F=\004\222\2104\021\001\n@\333\301\"5\201L~?\327\007I?+\253\2162_rC=\032Zj>\235\332p1S\037\221?" + } + } + } +} +node { + name: "MobilenetV1/Conv2d_0/BatchNorm/moving_variance/read" + op: "Identity" + input: "MobilenetV1/Conv2d_0/BatchNorm/moving_variance" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@MobilenetV1/Conv2d_0/BatchNorm/moving_variance" + } + } + } +} +node { + name: "MobilenetV1/MobilenetV1/Conv2d_0/BatchNorm/FusedBatchNorm" + op: "FusedBatchNorm" + input: "MobilenetV1/MobilenetV1/Conv2d_0/Conv2D" + input: "MobilenetV1/Conv2d_0/BatchNorm/gamma/read" + input: "MobilenetV1/Conv2d_0/BatchNorm/beta/read" + input: "MobilenetV1/Conv2d_0/BatchNorm/moving_mean/read" + input: "MobilenetV1/Conv2d_0/BatchNorm/moving_variance/read" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NHWC" + } + } + attr { + key: "is_training" + value { + b: false + } + } +} +library { +} +versions { +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/debuginfo/v1_1.0_224_frozen.wrong_attr.line.part.pbtxt.debug b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/debuginfo/v1_1.0_224_frozen.wrong_attr.line.part.pbtxt.debug new file mode 100644 index 0000000000000000000000000000000000000000..3dac8d70a9408f6498e7d7a2325f8f369422fa84 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/debuginfo/v1_1.0_224_frozen.wrong_attr.line.part.pbtxt.debug @@ -0,0 +1,175 @@ +files: "fake/user/code/file_A.py" +files: "fake/user/code/file_B.py" +files: "fake/user/code/file_C.py" +files: "fake/user/code/file_D.py" +files: "fake/user/code/file_E.py" +files: "fake/user/code/file_F.py" +files: "fake/user/code/file_G.py" +files: "fake/user/code/file_H.py" +files: "fake/user/code/file_I.py" +files: "fake/user/code/file_J.py" +files: "fake/user/code/file_K.py" +files: "fake/user/code/file_L.py" +files: "fake/user/code/file_M.py" +files: "fake/user/code/file_N.py" +files: "fake/user/code/file_O.py" +files: "fake/user/code/file_P.py" +files: "fake/user/code/file_Q.py" +files: "fake/user/code/file_R.py" +files: "fake/user/code/file_S.py" +files: "fake/user/code/file_T.py" +files: "fake/user/code/file_U.py" +files: "fake/user/code/file_V.py" +files: "fake/user/code/file_W.py" +files: "fake/user/code/file_X.py" +files: "fake/user/code/file_Y.py" +files: "fake/user/code/file_Z.py" +files: "fake/user/code/file_1.py" +files: "fake/user/code/file_2.py" +files: "fake/user/code/file_3.py" +files: "fake/user/code/file_4.py" +files: "fake/user/code/file_5.py" +files: "fake/user/code/file_6.py" +files: "fake/user/code/file_a.py" +files: "fake/user/code/file_b.py" +files: "fake/user/code/file_c.py" +files: "fake/user/code/file_d.py" +files: "fake/user/code/file_e.py" +files: "fake/user/code/file_f.py" +files: "fake/user/code/file_g.py" +files: "fake/user/code/file_h.py" +files: "fake/user/code/file_i.py" +files: "fake/user/code/file_j.py" +files: "fake/user/code/file_k.py" +files: "fake/user/code/file_l.py" +files: "fake/user/code/file_m.py" +files: "fake/user/code/file_n.py" +files: "fake/user/code/file_o.py" +files: "fake/user/code/file_p.py" +files: "fake/user/code/file_q.py" +files: "fake/user/code/file_r.py" +files: "fake/user/code/file_s.py" +files: "fake/user/code/file_t.py" +files: "fake/user/code/file_u.py" +files: "fake/user/code/file_v.py" +files: "fake/user/code/file_w.py" +files: "fake/user/code/file_x.py" +files: "fake/user/code/file_y.py" +files: "fake/user/code/file_z.py" +traces { + key: "MobilenetV1/Conv2d_0/BatchNorm/beta@" + value { + file_line_cols { + file_index: 33 + line: 383 + } + } +} +traces { + key: "MobilenetV1/Conv2d_0/BatchNorm/beta/read@" + value { + file_line_cols { + file_index: 49 + line: 886 + } + } +} +traces { + key: "MobilenetV1/Conv2d_0/BatchNorm/gamma@" + value { + file_line_cols { + file_index: 38 + line: 777 + } + } +} +traces { + key: "MobilenetV1/Conv2d_0/BatchNorm/gamma/read@" + value { + file_line_cols { + file_index: 49 + line: 915 + } + } +} +traces { + key: "MobilenetV1/Conv2d_0/BatchNorm/moving_mean@" + value { + file_line_cols { + file_index: 44 + line: 793 + } + } +} +traces { + key: "MobilenetV1/Conv2d_0/BatchNorm/moving_mean/read@" + value { + file_line_cols { + file_index: 49 + line: 335 + } + } +} +traces { + key: "MobilenetV1/Conv2d_0/BatchNorm/moving_variance@" + value { + file_line_cols { + file_index: 44 + line: 386 + } + } +} +traces { + key: "MobilenetV1/Conv2d_0/BatchNorm/moving_variance/read@" + value { + file_line_cols { + file_index: 49 + line: 492 + } + } +} +traces { + key: "MobilenetV1/Conv2d_0/weights@" + value { + file_line_cols { + file_index: 54 + line: 649 + } + } +} +traces { + key: "MobilenetV1/Conv2d_0/weights/read@" + value { + file_line_cols { + file_index: 49 + line: 421 + } + } +} +traces { + key: "MobilenetV1/MobilenetV1/Conv2d_0/BatchNorm/FusedBatchNorm@" + value { + file_line_cols { + file_index: 5 + line: 362 + } + } +} +traces { + key: "MobilenetV1/MobilenetV1/Conv2d_0/Conv2D@" + value { + file_line_cols { + file_index: 2 + line: 27 + } + } +} +traces { + key: "input@" + value { + file_line_cols { + file_index: 40 + line: 690 + } + } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/debuginfo/v1_1.0_224_frozen.wrong_attr.stack.part.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/debuginfo/v1_1.0_224_frozen.wrong_attr.stack.part.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..759c41042472c7b0455c4156f40a3c40104b0942 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/debuginfo/v1_1.0_224_frozen.wrong_attr.stack.part.pbtxt @@ -0,0 +1,343 @@ +# RUN: not tf_tfl_translate -mlir-pretty-debuginfo -tf-input-arrays=input -tf-input-data-types=DT_FLOAT -tf-input-shapes=1,224,224,3 -tf-output-arrays=MobilenetV1/MobilenetV1/Conv2d_0/BatchNorm/FusedBatchNorm -tf-debug-info=%s.debug %s -o - 2>&1 | FileCheck %s + +# CHECK: fake/user/code/file_C.py:27:1: error: 'tf.Conv2D' op attribute 'data_format' failed to satisfy constraint: 'NHWC' or 'NCHW' convnet data format +# CHECK: fake/user/code/file_D.py:28:1: note: called from +# CHECK: fake/user/code/file_E.py:29:1: note: called from +# CHECK: fake/user/code/file_F.py:30:1: note: called from + +node { + name: "input" + op: "Placeholder" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: -1 + } + dim { + size: 224 + } + dim { + size: 224 + } + dim { + size: 3 + } + } + } + } +} +node { + name: "MobilenetV1/Conv2d_0/weights" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 3 + } + dim { + size: 32 + } + } + tensor_content: "\321K\0168\222\006\301\276\230J\032>L\344A7\205\214\251;\005\t2\275\3763\014\273/\r7\275\222\254Z\267\372j\'8\355]\274\266\367\227\003\277\3462|\2768]\257>\252\362C\272\'\240\013>T\317\336\275R\347%?\031\205s\276k5\010\276\270\204\306\271\320\235P\276\362\346\207\266)i\374\276J\317\220\267\030\210i\275\222\311Q>\343\007\3677\376Q\255\276\237b4\275<\324n7\226\326\357;\352\316\316\267\207!\337\276\370\206\010\276\027)\3317v\354\270<\262\'2\275i\256+:%b\372\275\323M\n\270d\014\r\267\243\016\0328\t\344N\277\344a\210=\266z_\277\274|M\272\301\206]>W\366G\275\034\3269\275)\020\"\274\204\'X\276\275\207\217\271G\350\227\276\226\035\204\266\251\001%\277ov\212\270\334\274/<)\342\010\277\034B\266\266Z}\025\277\311\375\032\275\253\005\321\267O\274\322=O\023y\270\364\223Q\276DR\r\276\275\33108\307k\016\272\002*]\274\233\352\004\274\320m\370>\225\332\330\267s<\3416/\024\0008^tw\276l\320\244>\210B\t?4\316E\272\244)l=\017\255\301\275,\213\234\275\361\251\264>\210\026p\275u\353\030\271\337\263\232\276\322m\331\270a\'J\276\203\260s\270\016V\022<\256\nW<\326\355=\2706\275\006\276\024cM\274$I\003\267\034\346&\274\306\320L8\215hX\276/;\215>,7\3206\375:\225\275\366s\031>=\374@\274\376o\007\276\357\2345\267\322\334\2208\020\013\312\267W\322\362>\363\327\232\276S\331\351>\003\021\367\2718\370\016>\0019\253>\352\305#?:\302\207\276U|\017>6\005\341\271{e\334>v\027\233\267\265\261\236\276\037\305\032\270\215\201_\275\225\257\213>D!\3067N\"\202\276lL\017=\335\222\3137\034\306\351>l\314\204\267\337\n\225\276\274(*\276zH\3237\003\347\300\275\021\007\202>M\031\351\273X\273\243\276\014=\266\2678\264\3157\033E^\267\005\320-?0]\'=\314#_\277h\3530\272@En>\371S\316>\243!\203\276\235\\Z\275\350\222\207>Hl\217\271\375%\313>6e\204\267\356t\315\276DL\216\270ZJ\t>\240\033+\277\246n\313\266J\224\341\276pG\365<\266p\351\267o\211,?\242CW\270\032G\370\275yb.\276\225\357\3157\211\245\030\275\374\324\255=\001\020y\274\200\003\025?/1\233\267\364\354\3617z\336\275\267f\255h>\211\227\220>\024\305\330>f\241&\272\314\374^=\303\344C>g\271A\276wd\271>\267\316\263=^0\031\271+\377,>/\371\347\270J\372\021\276\274\303\r\270\324\177\307=\362\007q>\274pi\270\202\013\004\276\275\030|=;\346\0036\347\025R>v\361X8\340\217\032>\211\223\324=\246\24527\205\016\202>\202\032s\275\022\260\343\272<\223r\274\024\305\033\267\216\234\2028\360\272\014\270HH\317<\244\013R\276\372\035\375=iKJ\272\227!\304\273\362\\\376<\351:\223>\321\311\207\276\240\202\274\2745\347\352\271PO\306>j\031\3356O\256\266m\356\2637\tJJ>4\340Z\274!\272\2367\222s\360>\2743\227\267X\260\364=\036\352\331\274n\227\2777\324c\254>Tv\037\276\346E\331\271\352.\205\276\236R\253\267\245\035!70(\r\267V\226\207=\220\330\276::Y\255\276\230\344\212\272Q\037d\274\352]\343\274\350\343K\276\342B\232\275+\271\244\275N\024\224\271\351V\255>\2730\2716F\205M<\377\326%\270\267\350\213>a\260\237\276\033\3743\266\016v\202>*\267\206\275\237\310\236\267\n\221\033?\014\247x\270\252\205\230=\350N\206\275\032\336\3717!\374\360=\277w\346\274,x<\274\014qm>\262\374\035\267\216\302\00581\0313\267\354J\306:\027\276S>\212bV>\345\313\211\272\257&\032<\237#\253\275\243\212B\276T%t>\2676!\275kj\357\270D\202!>k\311\247\270=\332\017\273\257\333\252\266\007\245i>)\3348>\363\036P\270\334\333\242=\304\013\326\273\017\362\3156\034^o>\241\213\210\265\\5\247\275\325\030\244>iz\322\267S>\205:.\013\031=v\016\004\272\tn\305\2753\314\030\267\037\267\3247{\311\232\267\214P\344\276\035\023\262\2762)\275>\220\310\202\272\036\310\343\276Zw\372\274\254\343\020?\262\t\254\276\270\217\363>\032\013\241\271K\006\353>y\246\324\267\221\334\217\276u\'\335\270\300\356.=1\264\224>\370\213V8k\202z>j\360\t<\031\244\2027\373\216\032\275\202H\235\270-\205a\275\352\257\034\276#\227\323\267\343\002\336;\264|p=]b`;\200\000\013\276z\021\341\267\205\2665\267%8\245\266\002Y!\277\345`6\274x\240g\277?\221\204\272/\254+\277\234\302\204\275\243w\022\276\334%:\275\372\375>?\213\326\202\271\216\344\363>\363\230\207\267J\340\305\276S\214\001\271\366\032g=\231^\014\277v\350\3007\237N\330>L\251\223=\211\257\224\267]uj\275y7\277\2701\310\361:\006\366\021\276<]\0167\201\276(\274\373\0132=\034\027d\273 l\n?\206[\350\267\250\242R\267glz\2654O-\276\374\010\211>\277\007\004?\277S\206\272\2437D\276\305\255\230\275\330\204#\276\326\271\302>o0\260>\307\206\201\271\231\335\335>^\262\004\271\304:\365\275\3054\276\270U\306\376<;HI<\2070\237\2671a\035>\300R\032=\237FD\267I\233\30298)x7v\025\017\276\316\014\343>H \261\267\226V\350\276\310\006\257>\207RM\274\315\372\360\274\271%\233\267\366Ld8\325\306\001\270\252\020\375>\250V\316\276\305e\t?\003\322y\272\205`\336\2763\261|>#\250\037?)\254\262\276\035\005\002\2770T\235\271\\\326\235\276\307\270\212\267\002\257,\276\030o\333\270l\2626>\216\330\241>\036\254\0048\377\225\013=\2267\036\274f\213\3427\313\363\331\275\221\022T\270\314\035\000\276>=\037\276lNC\267\247\242+\277\003\"\374>X\255\334\273\304\200m\276\"B\005\270\377\333\2667\361\236\2515~\310,?;\000\013\275\344`C\277\350\216\216\272\n_*\277\006\2267>^j\222\276\210.\233\275\362@U\277\274\201d\271{w\242\276\233\345\260\267\254vq\276DW\370\270\265\250\215>J\017J\277\013\305:6\371\336\343=:\226\240=\223;\246\267\313TH\276+\346\245\270\tK\204\275\260S\"\276\331_\0337\226uD\276\337\037\014>-\375d\274\306\317-?\307Q\343\2679w\265\266\234@\326\266^QA>\211\332V>\257cL>\245<\217\272\242\216$\276\376\3077=R\340\023\276)\203\314>;-\346\276\014xx\271\353u\224\275\222\254\342\270\376\021\243\275\034}\221\270nK7>[\227&>\'\016.\2704CO=\210P\232=\304\252\341\263x^=\275\227\322h\267\270\323o\275EF\304=\031L\327\266\226\301@>\351Wy\276@\004\032;^\223\243=\237I\277\267\023c\2468U\007K7\021\n+<\353\3207\276w\267#\275\r\277\224\272\303)\234\275\352\002\005>/\334\244>]\272\210\276\357X\261<\036\246\263\271\274N9\277\\[\2177L\272\232;`~\247\270\3016\234=\366Qt>\355\360\2617)\326J\274\027\246\311\274\275|\2067\024\325\237\275t\353\252\270\223\367\013\275\\\276\303\275\260\00097\374\307\206>\0346\240\276\342\312o;\356\374o\276{S\021\270\305E\3077t*18V\300+=r\273\235<\235c<\276P\"\267\272\305\344\365\275 \337\200\273\342y+\276\224\262\210\274Z\347\317=\327\336t\271`\177@\277\2270\r7=\370\216\273\310\252\276\270=\002\204>\373$\350\276\260\"\3115\355\206\267\275\371\317\221<=U\342\267\002\377\013\276]\212\313\270\204\252I\275\211\177\234\275\317\367\3427W\302\250=P)\333\275\336B\327\273?!d>\267\210\224\267\261\355)7d\014e8%\\\335;\203Tf>\243\352M>\311\314\270\272\017\\\200\2755iL\272\3742a\275\351 \267>u\213\355=\327\217j\271G`\254\276\336\217\254\270\220\341\220<\342\001%\270N\357\033>\022\343$>#\362&\270d\342\213\274\"5\026=\360OS6C(\035\275[E\2053\375y\347\274\227b\036>\352W6\270\344\342\254=\352\300L=\036j\257;\364\233\037\276\334\237\326\266\367o\2066\033\006c\267\336o\220\275\327i\013\276\217\347\215>O\336)\272J\243\306=\254\\q=:\037|>H\345\212\276P@:>\237\021\'\2715\235x\276\245\325\2266\224\2603>]\300L\270\376\255\313\274Y\220\366=\024\23248,\035\006>\032H\321;%F\2377`Vb\274\221\013b\270\254\034\030\274\333z\252\275\232M\347\267\033k\371=\347y>\274\220\217\244;\313\274\264\275\323\034\302\267\030\013!\270L\ra6\205\216\334\275TF\032=\374\273:\277d&\312\271N\305\006>\311\245\257\274\316L\372\275\354\037\302\274l\004o>\327\361\006\271\030\021]\276L\326\0007en+>It\247\270\300\302\212\275V\371:\276\316\026#\265\237\346\031>5\204\266=\331\323X\267L0\243<9Z\301\270\314_Y\2754\254\305\275-\260\313\266\201\237\025=\361\344\334<\276\225\204\273\275L\210>n\335\022\267\202/\005\270\334\225\350\265Mh\027\274\322~|>\225R\343>\177u\212\271lUR= >p=\237R.\276\3205n>;\231H\275\350\3351\271H\021%\2767\\\323\270\360\343\243=#\005x\270\206|@\275D\010S\275u\341P\270\036\366j;\007\310\335\273\250\233a\266\007\002\343\274I*\343\267\316D\025\275\271g!>\365\304 \270\241hl\276c\234y>cz\003\273Ya(\275\341\030\351\2671\347.8N\344A\267\202z\026>\342);\276\237\317\240>\211\315\313\271\207>\363=\272\264C\341`\214\276\371E\331\276\206E\'\2714\336\345\275\344\214N7\222t\277=\027\201\370\267\313X\357\275\314FD>L\344A8yDR>w\251\035=\177\27008\241\204\222;l\360\234\270\004\204\202\275\036\360\346\275\206\017/\270\333\312\200\276\3518z>\242\345\262\272j\301l\276\301\310\025\270\261\025t7\000\365\2647n\214\253=\355\014\346;]z\013\277zF\346\270\2471d>\347Z,\276\036`a\276`\232i\275\314e\005\277b\325\342\270\212\345\257\274\241\245\0216\341\333\352==3U\270\217\361\346\272v\006\340\276\032\036G7P\261\237>\372R)>\3029\0136]\367[\273\247\231\313\270\314g\220\273\034\250\253\275%\221\235\267\374\372\220\275\217m\256=\013\2260\274\243!\266>z\'\013\270$\345\024\267_@\2057\313\224+=G2$>\260\001_>L\350\272\270\222:0=\251\327\320\275;\3016\275\267\226r>e>\206v\246\001\270\277\201\236=\2570\331=\033\360\2767L\250\021;\343\037&\270J\346\307<\221\250\005\275\"e\355\267\022\3571>\'\334\020\276N\032\373;\377z\r>\014\270\325\267\221\010\3367,\303\3076Yx\252\275\236&\271\275\"\306\353\275d9\245\271\250\274+\275\223\240,=\036\0028>\327a9\276/\212\205>\017\237\031\271\334\334\241>\006\311\2447\206#\304\274\300a\205\266\315W\322\275\322\250\360=iO\0228e6\"\276\016\365S=\226\376\0108o\252\220\273 \004\311\270\rQ\265:\360\310\275o\374t;\350\224@\276DA\303\267\354\223\231\266\256\034x7r>\250\275\341Va\274T\252y<\321\231\274\270\2579\234\274m\220\024\276\344\265\324\275T\274\025=\206\214\242>\035\002\221\270\273z\311>h\004I6To5\274\244\"<\270\305\023\036>\312\2451\276\001\234\2157m\352/\276\016\033\004>\010\375\030\267\037\256\241\274-\367\312\270f\027H=)FG\275\037\273\246\267*\270\225=\334\340\222\275\371 $\274H9\031=I\222\321\267\300\'\321\267\354\241\0217K\346\364\274\021\254\020>\300\332\310=\234:>\271\210\001|:\243&f<\225]\371\341v\240<\0347\006\271c\352:>\343\371\235\270y\341\354:x\204\250\266\304\373)>}\025\032>p\234\361\267\371B\207\275_\270\276=l\256a6z\227l;" + } + } + } +} +node { + name: "MobilenetV1/Conv2d_0/weights/read" + op: "Identity" + input: "MobilenetV1/Conv2d_0/weights" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@MobilenetV1/Conv2d_0/weights" + } + } + } +} +node { + name: "MobilenetV1/MobilenetV1/Conv2d_0/Conv2D" + op: "Conv2D" + input: "input" + input: "MobilenetV1/Conv2d_0/weights/read" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NDHWC" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 2 + i: 2 + i: 1 + } + } + } + attr { + key: "use_cudnn_on_gpu" + value { + b: true + } + } +} +node { + name: "MobilenetV1/Conv2d_0/BatchNorm/gamma" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 32 + } + } + tensor_content: "E\000\017?\025\245`?)L\201?\020Ed?#\016.?\331c\037?\327\024\246?\006v\236>Q\017w?\275\346\232>\242\002o?\203O\\?\2421\023@8\376\302>\356\224u?6\031\204?\351\302\264? P\033?\346U\206@\324[V>\346\"t?\346R\002?\360\016\232?\303\022|?\305\251~?J\220\235?1\312\r?lRs?\211\362\210?\352N\226?d\022l?\216\2432?" + } + } + } +} +node { + name: "MobilenetV1/Conv2d_0/BatchNorm/gamma/read" + op: "Identity" + input: "MobilenetV1/Conv2d_0/BatchNorm/gamma" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@MobilenetV1/Conv2d_0/BatchNorm/gamma" + } + } + } +} +node { + name: "MobilenetV1/Conv2d_0/BatchNorm/beta" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 32 + } + } + tensor_content: "\257\346\235\277K\257\033@\206\002\037=\217P\331\276\025\000\332?\200\341\022@\2348(?6,=@\036\352\273\276q\206\241\277\022Xe\277\031\'\036@j]\001@\006\272\340?\312n9?G\031O@\357\240\275\277\002.,@R\317\227>\021\236\n@\240\247\021\277\237\313\006\275K\315\r\300\217\212[@\t\363\007\300\"o\025@J\233\237@\246\001\342\276|Q!@7\310\271?w\221\273\276J\205U@" + } + } + } +} +node { + name: "MobilenetV1/Conv2d_0/BatchNorm/beta/read" + op: "Identity" + input: "MobilenetV1/Conv2d_0/BatchNorm/beta" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@MobilenetV1/Conv2d_0/BatchNorm/beta" + } + } + } +} +node { + name: "MobilenetV1/Conv2d_0/BatchNorm/moving_mean" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 32 + } + } + tensor_content: "\352\351C9\301\312?>\355\300Y>\361\347\020\267(\247G< \032\260\275\262\212\215<\2218\000\277$\362\2158w\020\233\267\360\025\217\267\326\2253;6\327\253\276\2153:\276\255\262\016;\372d\000>\263\240\367\274v\367>>Z/\335\276U0\343:\371\273\033:\273z\250:\013\271<9=&\201>,6(9\327\221\205\276+D\304=\031*i8-\310Q\273\022\273\373\275\233\231D6\302\357<\276" + } + } + } +} +node { + name: "MobilenetV1/Conv2d_0/BatchNorm/moving_mean/read" + op: "Identity" + input: "MobilenetV1/Conv2d_0/BatchNorm/moving_mean" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@MobilenetV1/Conv2d_0/BatchNorm/moving_mean" + } + } + } +} +node { + name: "MobilenetV1/Conv2d_0/BatchNorm/moving_variance" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 32 + } + } + tensor_content: "G\325\3074\"y\214?\036\014\235>V\316\3041@`b=\360\251\211>\202v6;\325\346\210?1x\3023\367\346k3\001\313\0311}\020\351=AV&?\03693?e:\3228\207P\022?\346\261G>^]\251?aY\204?\016\216F=\327g\"7!\306F=\004\222\2104\021\001\n@\333\301\"5\201L~?\327\007I?+\253\2162_rC=\032Zj>\235\332p1S\037\221?" + } + } + } +} +node { + name: "MobilenetV1/Conv2d_0/BatchNorm/moving_variance/read" + op: "Identity" + input: "MobilenetV1/Conv2d_0/BatchNorm/moving_variance" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@MobilenetV1/Conv2d_0/BatchNorm/moving_variance" + } + } + } +} +node { + name: "MobilenetV1/MobilenetV1/Conv2d_0/BatchNorm/FusedBatchNorm" + op: "FusedBatchNorm" + input: "MobilenetV1/MobilenetV1/Conv2d_0/Conv2D" + input: "MobilenetV1/Conv2d_0/BatchNorm/gamma/read" + input: "MobilenetV1/Conv2d_0/BatchNorm/beta/read" + input: "MobilenetV1/Conv2d_0/BatchNorm/moving_mean/read" + input: "MobilenetV1/Conv2d_0/BatchNorm/moving_variance/read" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NHWC" + } + } + attr { + key: "is_training" + value { + b: false + } + } +} +library { +} +versions { +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/debuginfo/v1_1.0_224_frozen.wrong_attr.stack.part.pbtxt.debug b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/debuginfo/v1_1.0_224_frozen.wrong_attr.stack.part.pbtxt.debug new file mode 100644 index 0000000000000000000000000000000000000000..cd042d3a4ee4f3eeed6c4a3829002c44064db2d5 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/debuginfo/v1_1.0_224_frozen.wrong_attr.stack.part.pbtxt.debug @@ -0,0 +1,187 @@ +files: "fake/user/code/file_A.py" +files: "fake/user/code/file_B.py" +files: "fake/user/code/file_C.py" +files: "fake/user/code/file_D.py" +files: "fake/user/code/file_E.py" +files: "fake/user/code/file_F.py" +files: "fake/user/code/file_G.py" +files: "fake/user/code/file_H.py" +files: "fake/user/code/file_I.py" +files: "fake/user/code/file_J.py" +files: "fake/user/code/file_K.py" +files: "fake/user/code/file_L.py" +files: "fake/user/code/file_M.py" +files: "fake/user/code/file_N.py" +files: "fake/user/code/file_O.py" +files: "fake/user/code/file_P.py" +files: "fake/user/code/file_Q.py" +files: "fake/user/code/file_R.py" +files: "fake/user/code/file_S.py" +files: "fake/user/code/file_T.py" +files: "fake/user/code/file_U.py" +files: "fake/user/code/file_V.py" +files: "fake/user/code/file_W.py" +files: "fake/user/code/file_X.py" +files: "fake/user/code/file_Y.py" +files: "fake/user/code/file_Z.py" +files: "fake/user/code/file_1.py" +files: "fake/user/code/file_2.py" +files: "fake/user/code/file_3.py" +files: "fake/user/code/file_4.py" +files: "fake/user/code/file_5.py" +files: "fake/user/code/file_6.py" +files: "fake/user/code/file_a.py" +files: "fake/user/code/file_b.py" +files: "fake/user/code/file_c.py" +files: "fake/user/code/file_d.py" +files: "fake/user/code/file_e.py" +files: "fake/user/code/file_f.py" +files: "fake/user/code/file_g.py" +files: "fake/user/code/file_h.py" +files: "fake/user/code/file_i.py" +files: "fake/user/code/file_j.py" +files: "fake/user/code/file_k.py" +files: "fake/user/code/file_l.py" +files: "fake/user/code/file_m.py" +files: "fake/user/code/file_n.py" +files: "fake/user/code/file_o.py" +files: "fake/user/code/file_p.py" +files: "fake/user/code/file_q.py" +files: "fake/user/code/file_r.py" +files: "fake/user/code/file_s.py" +files: "fake/user/code/file_t.py" +files: "fake/user/code/file_u.py" +files: "fake/user/code/file_v.py" +files: "fake/user/code/file_w.py" +files: "fake/user/code/file_x.py" +files: "fake/user/code/file_y.py" +files: "fake/user/code/file_z.py" +traces { + key: "MobilenetV1/Conv2d_0/BatchNorm/beta@" + value { + file_line_cols { + file_index: 33 + line: 383 + } + } +} +traces { + key: "MobilenetV1/Conv2d_0/BatchNorm/beta/read@" + value { + file_line_cols { + file_index: 49 + line: 886 + } + } +} +traces { + key: "MobilenetV1/Conv2d_0/BatchNorm/gamma@" + value { + file_line_cols { + file_index: 38 + line: 777 + } + } +} +traces { + key: "MobilenetV1/Conv2d_0/BatchNorm/gamma/read@" + value { + file_line_cols { + file_index: 49 + line: 915 + } + } +} +traces { + key: "MobilenetV1/Conv2d_0/BatchNorm/moving_mean@" + value { + file_line_cols { + file_index: 44 + line: 793 + } + } +} +traces { + key: "MobilenetV1/Conv2d_0/BatchNorm/moving_mean/read@" + value { + file_line_cols { + file_index: 49 + line: 335 + } + } +} +traces { + key: "MobilenetV1/Conv2d_0/BatchNorm/moving_variance@" + value { + file_line_cols { + file_index: 44 + line: 386 + } + } +} +traces { + key: "MobilenetV1/Conv2d_0/BatchNorm/moving_variance/read@" + value { + file_line_cols { + file_index: 49 + line: 492 + } + } +} +traces { + key: "MobilenetV1/Conv2d_0/weights@" + value { + file_line_cols { + file_index: 54 + line: 649 + } + } +} +traces { + key: "MobilenetV1/Conv2d_0/weights/read@" + value { + file_line_cols { + file_index: 49 + line: 421 + } + } +} +traces { + key: "MobilenetV1/MobilenetV1/Conv2d_0/BatchNorm/FusedBatchNorm@" + value { + file_line_cols { + file_index: 5 + line: 362 + } + } +} +traces { + key: "MobilenetV1/MobilenetV1/Conv2d_0/Conv2D@" + value { + file_line_cols { + file_index: 2 + line: 27 + } + file_line_cols { + file_index: 3 + line: 28 + } + file_line_cols { + file_index: 4 + line: 29 + } + file_line_cols { + file_index: 5 + line: 30 + } + } +} +traces { + key: "input@" + value { + file_line_cols { + file_index: 40 + line: 690 + } + } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/decompose-hybrid-quantization.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/decompose-hybrid-quantization.mlir new file mode 100644 index 0000000000000000000000000000000000000000..2c225cb5357c0fe98924f9852efb1f7160a0e622 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/decompose-hybrid-quantization.mlir @@ -0,0 +1,139 @@ +// RUN: litert-opt --tfl-decompose-hybrid-quantization --verify-each %s | FileCheck %s + +// CHECK-LABEL: @test_conv2d_float +func.func @test_conv2d_float(%arg0: tensor<1x32x32x8xf32>) -> tensor<1x32x32x16xf32> { + // CHECK-DAG: %[[VAL0:.+]] = "tfl.pseudo_const"() <{value = dense<42> : tensor<16x1x1x8xi8>}> + // CHECK-DAG: %[[VAL1:.+]] = "tfl.pseudo_const"() <{value = dense<1> : tensor<16x1x1x8xi8>}> + // CHECK-DAG: %[[VAL2:.+]] = "tfl.conv_2d"(%arg0, %[[VAL0]], %[[VAL1]]) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32}> + // CHECK: return %[[VAL2]] + %0 = "tfl.pseudo_const"() {value = dense<42> : tensor<16x1x1x8xi8>} : () -> tensor<16x1x1x8xf32> + %1 = "tfl.pseudo_const"() {value = dense<1> : tensor<16x1x1x8xi8>} : () -> tensor<16xf32> + %2 = "tfl.conv_2d"(%arg0, %0, %1) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32} : (tensor<1x32x32x8xf32>, tensor<16x1x1x8xf32>, tensor<16xf32>) -> tensor<1x32x32x16xf32> + func.return %2 : tensor<1x32x32x16xf32> +} + +// ----- + +// CHECK-LABEL: @test_conv2d_qi8 +func.func @test_conv2d_qi8(%arg0: tensor<1x32x32x8x!quant.uniform>) -> tensor<1x32x32x16x!quant.uniform> { + // CHECK-DAG: %[[VAL0:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<{{.+}}>, value = dense<42> : tensor<16x1x1x8xi8>}> + // CHECK-DAG: %[[VAL1:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<{{.+}}>, value = dense<0> : tensor<16xi32>}> + // CHECK-DAG: %[[VAL2:.+]] = "tfl.conv_2d"(%arg0, %0, %1) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32}> + // CHECK: return %[[VAL2]] + %0 = "tfl.pseudo_qconst"() {qtype = tensor<16x1x1x8x!quant.uniform>, value = dense<42> : tensor<16x1x1x8xi8>} : () -> tensor<16x1x1x8x!quant.uniform> + %1 = "tfl.pseudo_qconst"() {qtype = tensor<16x!quant.uniform>, value = dense<0> : tensor<16xi32>} : () -> tensor<16x!quant.uniform> + %2 = "tfl.conv_2d"(%arg0, %0, %1) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32} : (tensor<1x32x32x8x!quant.uniform>, tensor<16x1x1x8x!quant.uniform>, tensor<16x!quant.uniform>) -> tensor<1x32x32x16x!quant.uniform> + func.return %2 : tensor<1x32x32x16x!quant.uniform> +} + +// ----- + +// CHECK-LABEL: @test_conv2d_qi16 +func.func @test_conv2d_qi16(%arg0: tensor<1x32x32x8x!quant.uniform>) -> tensor<1x32x32x16x!quant.uniform> { + // CHECK-DAG: %[[BIAS:.+]] = arith.constant dense<0> : tensor<16xi64> + // CHECK-DAG: %[[VAL0:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<{{.+}}>, value = dense<42> : tensor<16x1x1x8xi8>}> + // CHECK-DAG: %[[VAL1:.+]] = "tfl.conv_2d"(%arg0, %[[VAL0]], %[[BIAS]]) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32}> + // CHECK: return %[[VAL1]] + %0 = "tfl.pseudo_qconst"() {qtype = tensor<16x1x1x8x!quant.uniform>, value = dense<42> : tensor<16x1x1x8xi8>} : () -> tensor<16x1x1x8x!quant.uniform> + %1 = "arith.constant"() {value = dense<0> : tensor<16xi64>} : () -> tensor<16xi64> + %2 = "tfl.conv_2d"(%arg0, %0, %1) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32} : (tensor<1x32x32x8x!quant.uniform>, tensor<16x1x1x8x!quant.uniform>, tensor<16xi64>) -> tensor<1x32x32x16x!quant.uniform> + func.return %2 : tensor<1x32x32x16x!quant.uniform> +} + +// ----- + +// CHECK-LABEL: @test_conv2d_replace_qi8 +func.func @test_conv2d_replace_qi8(%arg0: tensor<1x32x32x8xf32>) -> tensor<1x32x32x16x!quant.uniform> { + // CHECK-DAG: %[[VAL0:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<{{.+}}>, value = dense<42> : tensor<16x1x1x8xi8>}> + // CHECK-DAG: %[[VAL1:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<{{.+}}>, value = dense<0> : tensor<16xi32>}> + // CHECK-DAG: %[[VAL2:.+]] = "tfl.dequantize"(%[[VAL0]]) + // CHECK-DAG: %[[VAL3:.+]] = "tfl.dequantize"(%[[VAL1]]) + // CHECK-DAG: %[[VAL4:.+]] = "tfl.conv_2d"(%arg0, %[[VAL2]], %[[VAL3]]) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32}> + // CHECK-DAG: %[[VAL5:.+]] = "tfl.quantize"(%4) <{qtype = tensor<1x32x32x16x!quant.uniform>}> + // CHECK: return %[[VAL5]] + %0 = "tfl.pseudo_qconst"() {qtype = tensor<16x1x1x8x!quant.uniform>, value = dense<42> : tensor<16x1x1x8xi8>} : () -> tensor<16x1x1x8x!quant.uniform> + %1 = "tfl.pseudo_qconst"() {qtype = tensor<16x!quant.uniform>, value = dense<0> : tensor<16xi32>} : () -> tensor<16x!quant.uniform> + %2 = "tfl.conv_2d"(%arg0, %0, %1) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32} : (tensor<1x32x32x8xf32>, tensor<16x1x1x8x!quant.uniform>, tensor<16x!quant.uniform>) -> tensor<1x32x32x16x!quant.uniform> + func.return %2 : tensor<1x32x32x16x!quant.uniform> +} + +// ----- + +// CHECK-LABEL: @test_conv2d_replace_float +func.func @test_conv2d_replace_float(%arg0: tensor<1x32x32x8xf32>) -> tensor<1x32x32x16xf32> { + // CHECK-DAG: %[[VAL0:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<16x1x1x8x{{.+}}>, value = dense<42> : tensor<16x1x1x8xi8>}> : () -> tensor<16x1x1x8x!quant.uniform<{{.+}}>> + // CHECK-DAG: %[[VAL1:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<16x{{.+}}>, value = dense<0> : tensor<16xi32>}> : () -> tensor<16x!quant.uniform<{{.+}}>> + // CHECK-DAG: %[[VAL2:.+]] = "tfl.dequantize"(%[[VAL0]]) + // CHECK-DAG: %[[VAL3:.+]] = "tfl.dequantize"(%[[VAL1]]) + // CHECK-DAG: %[[VAL4:.+]] = "tfl.conv_2d"(%arg0, %[[VAL2]], %[[VAL3]]) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32}> + // CHECK: return %[[VAL4]] + %0 = "tfl.pseudo_qconst"() {qtype = tensor<16x1x1x8x!quant.uniform>, value = dense<42> : tensor<16x1x1x8xi8>} : () -> tensor<16x1x1x8x!quant.uniform> + %1 = "tfl.pseudo_qconst"() {qtype = tensor<16x!quant.uniform>, value = dense<0> : tensor<16xi32>} : () -> tensor<16x!quant.uniform> + %2 = "tfl.conv_2d"(%arg0, %0, %1) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32} : (tensor<1x32x32x8xf32>, tensor<16x1x1x8x!quant.uniform>, tensor<16x!quant.uniform>) -> tensor<1x32x32x16xf32> + func.return %2 : tensor<1x32x32x16xf32> +} + +// ----- + +// CHECK-LABEL: @test_conv3d_float +func.func @test_conv3d_float(%arg0: tensor<1x32x32x32x8xf32>) -> tensor<1x32x32x32x16xf32> { + // CHECK-DAG: %[[VAL0:.+]] = "tfl.pseudo_const"() <{value = dense<1.000000e+00> : tensor<16xf32> + // CHECK-DAG: %[[VAL1:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<1x1x8x16x!quant.uniform<{{.+}}>>, value = dense<42> : tensor<1x1x1x8x16xi8>}> + // CHECK: %[[VAL2:.+]] = "tfl.dequantize"(%[[VAL1]]) : (tensor<1x1x1x8x16x!quant.uniform<{{.+}}>>) -> tensor<1x1x1x8x16xf32> + // CHECK: %[[VAL3:.+]] = "tfl.conv_3d"(%arg0, %[[VAL2]], %[[VAL0]]) <{dilation_d_factor = 1 : i32, dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_d = 1 : i32, stride_h = 1 : i32, stride_w = 1 : i32}> + // CHECK: return %[[VAL3]] + %0 = "tfl.pseudo_qconst"() {qtype = tensor<1x1x8x16x!quant.uniform>, value = dense<42> : tensor<1x1x1x8x16xi8>} : () -> tensor<1x1x1x8x16x!quant.uniform> + %1 = "tfl.pseudo_const"() { value = dense<1.0> : tensor<16xf32>} : () -> tensor<16xf32> + %2 = "tfl.conv_3d"(%arg0, %0, %1) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, dilation_d_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32, stride_d = 1 : i32} : (tensor<1x32x32x32x8xf32>, tensor<1x1x1x8x16x!quant.uniform>, tensor<16xf32>) -> tensor<1x32x32x32x16xf32> + func.return %2 : tensor<1x32x32x32x16xf32> +} + +// ----- + +// CHECK-LABEL: @test_transpose_conv2d +func.func @test_transpose_conv2d(%arg0: tensor<1x32x32x8xf32>) -> tensor<1x32x32x16xf32> { + // CHECK-DAG: %[[SHAPE:.+]] = "tfl.pseudo_const"() <{value = dense<[1, 32, 32, 16]> + // CHECK-DAG: %[[VAL0:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<16x{{.+}}>, value = dense<1> : tensor<16xi32>}> + // CHECK-DAG: %[[VAL1:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<16x{{.+}}>, value = dense<2> : tensor<16xi32>}> + // CHECK-DAG: %[[VAL2:.+]] = "tfl.dequantize"(%[[VAL0]]) + // CHECK-DAG: %[[VAL3:.+]] = "tfl.dequantize"(%[[VAL1]]) + // CHECK-DAG: %[[VAL4:.+]] = "tfl.transpose_conv"(%[[SHAPE]], %[[VAL2]], %arg0, %[[VAL3]]) <{fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32}> + // CHECK: return %[[VAL4]] + %0 = "tfl.pseudo_const"() { value = dense<[1, 32, 32, 16]> : tensor<4xi32> } : () -> tensor<4xi32> + %1 = "tfl.pseudo_qconst"() {qtype = tensor<16x!quant.uniform>, value = dense<1> : tensor<16xi32>} : () -> tensor<16x1x1x8x!quant.uniform> + %2 = "tfl.pseudo_qconst"() {qtype = tensor<16x!quant.uniform>, value = dense<2> : tensor<16xi32>} : () -> tensor<16x!quant.uniform> + %3 = "tfl.transpose_conv"(%0, %1, %arg0, %2) {fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32} : (tensor<4xi32>, tensor<16x1x1x8x!quant.uniform>, tensor<1x32x32x8xf32>, tensor<16x!quant.uniform>) -> tensor<1x32x32x16xf32> + func.return %3 : tensor<1x32x32x16xf32> +} + +// ----- + +// CHECK-LABEL: @test_depthwise_conv2d_replace_float +func.func @test_depthwise_conv2d_replace_float(%arg0: tensor<1x224x224x3xf32>) -> tensor<1x112x112x32xf32> { + // CHECK-DAG: %[[VAL0:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<32x3x3x3x!quant.uniform<{{.+}}>>, value = dense<42> : tensor<32x3x3x3xi8>}> + // CHECK-DAG: %[[VAL1:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<32x!quant.uniform<{{.+}}>>, value = dense<0> : tensor<32xi32>}> + // CHECK-DAG: %[[VAL2:.+]] = "tfl.dequantize"(%[[VAL0]]) : (tensor<32x3x3x3x!quant.uniform<{{.+}}>>) + // CHECK-DAG: %[[VAL3:.+]] = "tfl.dequantize"(%[[VAL1]]) : (tensor<32x!quant.uniform<{{.+}}) + // CHECK-DAG: %[[VAL4:.+]] = "tfl.depthwise_conv_2d"(%arg0, %[[VAL2]], %[[VAL3]]) <{depth_multiplier = 4 : i32, dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 4 : i32, stride_w = 5 : i32}> + // CHECK: return %[[VAL4]] + %0 = "tfl.pseudo_qconst"() {qtype = tensor<32x3x3x3x!quant.uniform>, value = dense<42> : tensor<32x3x3x3xi8>} : () -> tensor<32x3x3x3x!quant.uniform> + %1 = "tfl.pseudo_qconst"() {qtype = tensor<32x!quant.uniform>, value = dense<0> : tensor<32xi32>} : () -> tensor<32x!quant.uniform> + %2 = "tfl.depthwise_conv_2d"(%arg0, %0, %1) {depth_multiplier = 4 : i32, dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 4 : i32, stride_w = 5 : i32} : (tensor<1x224x224x3xf32>, tensor<32x3x3x3x!quant.uniform>, tensor<32x!quant.uniform>) -> tensor<1x112x112x32xf32> + func.return %2 : tensor<1x112x112x32xf32> +} + +// ----- + +// CHECK-LABEL: @test_fullyconnected_replace_float +func.func @test_fullyconnected_replace_float(%arg0: tensor<4x256x6x6xf32>) -> tensor<4x256x36xf32> { + // CHECK-DAG: %[[VAL0:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<36x36x!quant.uniform<{{.+}}>>, value = dense<42> : tensor<36x36xi8>}> + // CHECK-DAG: %[[VAL1:.+]] = "tfl.pseudo_qconst"() <{qtype = tensor<36x!quant.uniform<{{.+}}>>, value = dense<0> : tensor<36xi32>}> + // CHECK-DAG: %[[VAL2:.+]] = "tfl.dequantize"(%[[VAL0]]) : (tensor<36x36x!quant.uniform>) + // CHECK-DAG: %[[VAL3:.+]] = "tfl.dequantize"(%[[VAL1]]) : (tensor<36x!quant.uniform>) + // CHECK: %[[VAL4:.+]] = "tfl.fully_connected"(%arg0, %[[VAL2]], %[[VAL3]]) <{fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"}> + // CHECK: return %[[VAL4]] + %0 = "tfl.pseudo_qconst"() {qtype = tensor<36x36x!quant.uniform>, value = dense<42> : tensor<36x36xi8>} : () -> tensor<36x36x!quant.uniform> + %1 = "tfl.pseudo_qconst"() {qtype = tensor<36x!quant.uniform>, value = dense<0> : tensor<36xi32>} : () -> tensor<36x!quant.uniform> + %2 = "tfl.fully_connected"(%arg0, %0, %1) {fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"} : (tensor<4x256x6x6xf32>, tensor<36x36x!quant.uniform>, tensor<36x!quant.uniform>) -> tensor<4x256x36xf32> + func.return %2 : tensor<4x256x36xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/default_quant_params.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/default_quant_params.mlir new file mode 100644 index 0000000000000000000000000000000000000000..1af17f563f974886e1c65d5ef04f949366fb96b5 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/default_quant_params.mlir @@ -0,0 +1,113 @@ +// RUN: litert-opt %s --tfl-default-quant --tfl-quantize | FileCheck %s + +// CHECK-LABEL: hardcode_all +func.func @hardcode_all(%arg0: tensor<2x2xf32>, %arg1: tensor<2x1xf32>) -> tensor<2x2xf32> { + %0 = "tfl.add"(%arg0, %arg1) {fused_activation_function="NONE"}: (tensor<2x2xf32>, tensor<2x1xf32>) -> tensor<2x2xf32> + func.return %0 : tensor<2x2xf32> + +// CHECK: %[[q0:.*]] = "tfl.quantize"(%arg1) <{qtype = tensor<2x1x!quant.uniform>}> +// CHECK: %[[q1:.*]] = "tfl.quantize"(%arg0) <{qtype = tensor<2x2x!quant.uniform>}> +// Quantized tfl.add +// CHECK: %[[add:.*]] = tfl.add(%[[q1]], %[[q0]]) <{fused_activation_function = "NONE"}> : (tensor<2x2x!quant.uniform> +// CHECK: %[[dq:.*]] = "tfl.dequantize"(%[[add]]) : (tensor<2x2x!quant.uniform>) +// CHECK: return %[[dq]] +} + +// CHECK-LABEL: hardcode_input +func.func @hardcode_input(%arg0: tensor<2x2xf32>, %arg1: tensor<2x1xf32>) -> tensor<2x2xf32> { + %0 = "tfl.quantize"(%arg0) {qtype = tensor<2x2x!quant.uniform>}: (tensor<2x2xf32>) -> tensor<2x2x!quant.uniform> + %1 = "tfl.dequantize"(%0) : (tensor<2x2x!quant.uniform>) -> tensor<2x2xf32> + %4 = "tfl.add"(%1, %arg1) {fused_activation_function="NONE"}: (tensor<2x2xf32>, tensor<2x1xf32>) -> tensor<2x2xf32> + func.return %4 : tensor<2x2xf32> + +// CHECK: %[[q0:.*]] = "tfl.quantize"(%arg1) <{qtype = tensor<2x1x!quant.uniform>}> +// CHECK: %[[q1:.*]] = "tfl.quantize"(%arg0) <{qtype = tensor<2x2x!quant.uniform>}> +// CHECK: %[[add:.*]] = tfl.add(%[[q1]], %[[q0]]) <{fused_activation_function = "NONE"}> : (tensor<2x2x!quant.uniform> +// CHECK: %[[dq:.*]] = "tfl.dequantize"(%[[add]]) : (tensor<2x2x!quant.uniform>) +// CHECK: return %[[dq]] +} + +// CHECK-LABEL: hardcode_input_deq +func.func @hardcode_input_deq(%arg0: tensor<2x2x!quant.uniform>, %arg1: tensor<2x1xf32>) -> tensor<2x2xf32> { + %1 = "tfl.dequantize"(%arg0) : (tensor<2x2x!quant.uniform>) -> tensor<2x2xf32> + %4 = "tfl.add"(%1, %arg1) {fused_activation_function="NONE"}: (tensor<2x2xf32>, tensor<2x1xf32>) -> tensor<2x2xf32> + func.return %4 : tensor<2x2xf32> + +// CHECK: %[[q:.*]] = "tfl.quantize"(%arg1) <{qtype = tensor<2x1x!quant.uniform>}> +// CHECK: %[[add:.*]] = tfl.add(%arg0, %[[q]]) <{fused_activation_function = "NONE"}> : (tensor<2x2x!quant.uniform> +// CHECK: %[[dq:.*]] = "tfl.dequantize"(%[[add]]) : (tensor<2x2x!quant.uniform>) +// CHECK: return %[[dq]] +} + +// CHECK-LABEL: hardcode_output +func.func @hardcode_output(%arg0: tensor<2x2xf32>, %arg1: tensor<2x1xf32>) -> tensor<2x2xf32> { + %0 = "tfl.quantize"(%arg0) {qtype = tensor<2x2x!quant.uniform>}: (tensor<2x2xf32>) -> tensor<2x2x!quant.uniform> + %1 = "tfl.quantize"(%arg1) {qtype = tensor<2x1x!quant.uniform>}: (tensor<2x1xf32>) -> tensor<2x1x!quant.uniform> + %2 = "tfl.dequantize"(%0) : (tensor<2x2x!quant.uniform>) -> tensor<2x2xf32> + %3 = "tfl.dequantize"(%1) : (tensor<2x1x!quant.uniform>) -> tensor<2x1xf32> + %4 = "tfl.add"(%2, %3) {fused_activation_function="NONE"}: (tensor<2x2xf32>, tensor<2x1xf32>) -> tensor<2x2xf32> + func.return %4 : tensor<2x2xf32> + +// CHECK: %[[q0:.*]] = "tfl.quantize"(%arg0) <{qtype = tensor<2x2x!quant.uniform>}> +// CHECK: %[[q1:.*]] = "tfl.quantize"(%arg1) <{qtype = tensor<2x1x!quant.uniform>}> +// CHECK: %[[add:.*]] = tfl.add(%[[q0]], %[[q1]]) <{fused_activation_function = "NONE"}> : (tensor<2x2x!quant.uniform> +// CHECK: %[[dq:.*]] = "tfl.dequantize"(%[[add]]) : (tensor<2x2x!quant.uniform>) +// CHECK: return %[[dq]] +} + +// CHECK-LABEL: test_conv_2d_add +func.func @test_conv_2d_add(%arg0: tensor<1x224x224x3x!quant.uniform>, %arg1: tensor<32x3x3x3x!quant.uniform:f32, 1.0>>, %arg2: tensor<32x!quant.uniform>) -> tensor<1x112x112x32x!quant.uniform> { + %0 = "tfl.dequantize"(%arg0) : (tensor<1x224x224x3x!quant.uniform>) -> tensor<1x224x224x3xf32> + %1 = "tfl.dequantize"(%arg1) : (tensor<32x3x3x3x!quant.uniform:f32, 1.0>>) -> tensor<32x3x3x3xf32> + %2 = "tfl.dequantize"(%arg2) : (tensor<32x!quant.uniform>) -> tensor<32xf32> + %3 = "tfl.conv_2d"(%0, %1, %2) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 2 : i32, stride_w = 2 : i32} : (tensor<1x224x224x3xf32>, tensor<32x3x3x3xf32>, tensor<32xf32>) -> tensor<1x112x112x32xf32> + %4 = "tfl.pseudo_qconst"() {qtype = tensor<1x112x112x32x!quant.uniform>, value = dense<1> : tensor<1x112x112x32xi8>} : () -> tensor<1x112x112x32x!quant.uniform> + %5 = "tfl.dequantize"(%4) : (tensor<1x112x112x32x!quant.uniform>) -> tensor<1x112x112x32xf32> + %6 = "tfl.add"(%3, %5) {fused_activation_function="NONE"}: (tensor<1x112x112x32xf32>, tensor<1x112x112x32xf32>) -> tensor<1x112x112x32xf32> + %7 = "tfl.quantize"(%6) {qtype = tensor<1x112x112x32x!quant.uniform>} : (tensor<1x112x112x32xf32>) -> tensor<1x112x112x32x!quant.uniform> + func.return %7 : tensor<1x112x112x32x!quant.uniform> + +// CHECK: %[[conv:.*]] = "tfl.conv_2d"(%arg0, %arg1, %arg2) +// CHECK-SAME: -> tensor<1x112x112x32x!quant.uniform> +// CHECK: %[[cst:.*]] = "tfl.pseudo_qconst"() +// CHECK: %[[add:.*]] = tfl.add(%[[conv]], %[[cst]]) +// CHECK-SAME: -> tensor<1x112x112x32x!quant.uniform> +// CHECK: return %[[add]] +} + +// CHECK-LABEL: test_conv_2d_activation_and_bias +func.func @test_conv_2d_activation_and_bias(%arg0: tensor<1x224x224x3xf32>, %arg1: tensor<32x3x3x3x!quant.uniform:f32, 1.0>>, %arg2: tensor<32xf32>) -> tensor<1x112x112x32xf32> { + %0 = "tfl.dequantize"(%arg1) : (tensor<32x3x3x3x!quant.uniform:f32, 1.0>>) -> tensor<32x3x3x3xf32> + %1 = "tfl.conv_2d"(%arg0, %0, %arg2) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 2 : i32, stride_w = 2 : i32} : (tensor<1x224x224x3xf32>, tensor<32x3x3x3xf32>, tensor<32xf32>) -> tensor<1x112x112x32xf32> + func.return %1 : tensor<1x112x112x32xf32> + +// CHECK: %[[q0:.*]] = "tfl.quantize"(%arg2) <{qtype = tensor<32x!quant.uniform>}> +// CHECK: %[[q1:.*]] = "tfl.quantize"(%arg0) <{qtype = tensor<1x224x224x3x!quant.uniform>}> +// CHECK: %[[conv:.*]] = "tfl.conv_2d"(%[[q1]], %arg1, %[[q0]]) +// CHECK: %[[dq:.*]] = "tfl.dequantize"(%[[conv]]) : (tensor<1x112x112x32x!quant.uniform>) +// CHECK: return %[[dq]] +} + +// CHECK-LABEL: test_region +func.func @test_region(%arg0: tensor<128x128x!quant.uniform>, + %arg1: tensor<1x!quant.uniform>, %arg2: tensor<1x!quant.uniform>, + %arg3: tensor<1xi32>) -> (tensor<128x128x!quant.uniform>) { + %0 = "tfl.dequantize"(%arg0) : (tensor<128x128x!quant.uniform>) -> tensor<128x128xf32> + %1 = "tfl.dequantize"(%arg1) : (tensor<1x!quant.uniform>) -> tensor<1xf32> + %2 = "tfl.dequantize"(%arg2) : (tensor<1x!quant.uniform>) -> tensor<1xf32> + %3 = "tfl.custom_tf"(%0, %1, %2, %arg3) ({ + ^bb0(%a1: tensor<128x128xf32>, %a2: tensor<1xf32>, %a3: tensor<1xf32>, %a4: tensor<1xi32>): + %4 = "tf.LayerNorm"(%a1, %a2, %a3, %a4) {_tfl_quant_trait = "fully_quantizable", device = ""} : (tensor<128x128xf32>, tensor<1xf32>, tensor<1xf32>, tensor<1xi32>) -> tensor<128x128xf32> + "tfl.yield"(%4) : (tensor<128x128xf32>) -> () + }) {_tfl_quant_trait = "fully_quantizable", device = ""} : (tensor<128x128xf32>, tensor<1xf32>, tensor<1xf32>, tensor<1xi32>) -> tensor<128x128xf32> + %4 = "tfl.quantize"(%3) {qtype = tensor<128x128x!quant.uniform>} : (tensor<128x128xf32>) -> tensor<128x128x!quant.uniform> + func.return %4 : tensor<128x128x!quant.uniform> + +// CHECK: "tfl.custom_tf" +// CHECK-NEXT: ^bb0(%arg4: tensor<128x128xf32>, %arg5: tensor<1xf32>, %arg6: tensor<1xf32>, %arg7: tensor<1xi32>): +// CHECK-NEXT: "tf.LayerNorm"(%arg4, %arg5, %arg6, %arg7) {_tfl_quant_trait = "fully_quantizable", device = ""} : (tensor<128x128xf32>, tensor<1xf32>, tensor<1xf32>, tensor<1xi32>) -> tensor<128x128xf32> +// CHECK-NEXT: "tfl.yield" +// CHECK-NEXT: }) {_tfl_quant_trait = "fully_quantizable", device = ""} : +// CHECK-SAME: (tensor<128x128xf32>, tensor<1xf32>, tensor<1xf32>, tensor<1xi32>) -> tensor<128x128xf32> +} + diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/dilated-conv.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/dilated-conv.mlir new file mode 100644 index 0000000000000000000000000000000000000000..ed497805ac7b457c49434d2695b1b138fffb620d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/dilated-conv.mlir @@ -0,0 +1,545 @@ +// RUN: litert-opt %s -tfl-identify-dilated-conv | FileCheck %s + +func.func @testDilatedConv(%arg0: tensor<1x128x128x3xf32>, %arg1: tensor<5x5x3x8xf32>) -> tensor<1x120x120x8xf32> { + %cst = arith.constant dense<[2, 2]> : tensor<2xi32> + %cst_0 = arith.constant dense<4> : tensor<2x2xi32> + %0 = "tf.SpaceToBatchND"(%arg0, %cst, %cst_0) : (tensor<1x128x128x3xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<4x68x68x3xf32> + %1 = "tf.Conv2D"(%0, %arg1) {padding = "VALID", strides = [1, 1, 1, 1]} : (tensor<4x68x68x3xf32>, tensor<5x5x3x8xf32>) -> tensor<4x64x64x8xf32> + %2 = "tf.BatchToSpaceND"(%1, %cst, %cst_0) : (tensor<4x64x64x8xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<1x120x120x8xf32> + func.return %2 : tensor<1x120x120x8xf32> + + // CHECK-LABEL: testDilatedConv + // CHECK-SAME: ([[INPUT:%.*]]: tensor<1x128x128x3xf32>, [[FILTER:%.*]]: tensor<5x5x3x8xf32>) + // CHECK-NEXT: [[RESULT:%.*]] = "tf.Conv2D"([[INPUT]], [[FILTER]]) <{dilations = [1, 2, 2, 1], padding = "VALID", strides = [1, 1, 1, 1]}> : (tensor<1x128x128x3xf32>, tensor<5x5x3x8xf32>) -> tensor<1x120x120x8xf32> + // CHECK-NEXT: return [[RESULT]] : tensor<1x120x120x8xf32> +} + +func.func @testDilatedConvWithNonConstantPadAndCrops(%arg0: tensor<1x128x128x3xf32>, %arg1: tensor<5x5x3x8xf32>) -> tensor<1x120x120x8xf32> { + %cst = arith.constant dense<[2, 2]> : tensor<2xi32> + %cst_1 = arith.constant dense<0> : tensor<2x2xi32> + %0 = "tf.SpaceToBatchND"(%arg0, %cst, %cst_1) : (tensor<1x128x128x3xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<4x64x64x3xf32> + %1 = "tf.Conv2D"(%0, %arg1) {padding = "VALID", strides = [1, 1, 1, 1]} : (tensor<4x64x64x3xf32>, tensor<5x5x3x8xf32>) -> tensor<4x60x60x8xf32> + %2 = "tf.BatchToSpaceND"(%1, %cst, %cst_1) : (tensor<4x60x60x8xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<1x120x120x8xf32> + func.return %2 : tensor<1x120x120x8xf32> + + // CHECK-LABEL: testDilatedConvWithNonConstantPadAndCrops + // CHECK-SAME: ([[INPUT:%.*]]: tensor<1x128x128x3xf32>, [[FILTER:%.*]]: tensor<5x5x3x8xf32>) + // CHECK-NEXT: [[RESULT:%.*]] = "tf.Conv2D"([[INPUT]], [[FILTER]]) <{dilations = [1, 2, 2, 1], padding = "VALID", strides = [1, 1, 1, 1]}> : (tensor<1x128x128x3xf32>, tensor<5x5x3x8xf32>) -> tensor<1x120x120x8xf32> + // CHECK-NEXT: return [[RESULT]] : tensor<1x120x120x8xf32> +} + +func.func @testDilatedConvWithNonZeroBasePadding(%arg0: tensor<1x128x128x3xf32>, %arg1: tensor<5x5x3x8xf32>) -> tensor<1x128x128x8xf32> { + %cst = arith.constant dense<[2, 2]> : tensor<2xi32> + %cst_0 = arith.constant dense<4> : tensor<2x2xi32> + %cst_1 = arith.constant dense<0> : tensor<2x2xi32> + %0 = "tf.SpaceToBatchND"(%arg0, %cst, %cst_0) : (tensor<1x128x128x3xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<4x68x68x3xf32> + %1 = "tf.Conv2D"(%0, %arg1) {padding = "VALID", strides = [1, 1, 1, 1]} : (tensor<4x68x68x3xf32>, tensor<5x5x3x8xf32>) -> tensor<4x64x64x8xf32> + %2 = "tf.BatchToSpaceND"(%1, %cst, %cst_1) : (tensor<4x64x64x8xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<1x128x128x8xf32> + func.return %2 : tensor<1x128x128x8xf32> + + // CHECK-LABEL: testDilatedConvWithNonZeroBasePadding + // CHECK-SAME: ([[INPUT:%.*]]: tensor<1x128x128x3xf32>, [[FILTER:%.*]]: tensor<5x5x3x8xf32>) + // CHECK-NEXT: [[RESULT:%.*]] = "tf.Conv2D"([[INPUT]], [[FILTER]]) <{dilations = [1, 2, 2, 1], padding = "SAME", strides = [1, 1, 1, 1]}> : (tensor<1x128x128x3xf32>, tensor<5x5x3x8xf32>) -> tensor<1x128x128x8xf32> + // CHECK-NEXT: return [[RESULT]] : tensor<1x128x128x8xf32> +} + +func.func @testDilatedConvWithFp16(%arg0 : tensor<1x20x30x40xf16>, %arg1: tensor<5x5x40x32xf16>) -> tensor<1x20x30x32xf16> { + %block_shape = arith.constant dense<2> : tensor<2xi32> + %paddings = arith.constant dense<4> : tensor<2x2xi32> + %crops = arith.constant dense<0> : tensor<2x2xi32> + %0 = "tf.SpaceToBatchND"(%arg0, %block_shape, %paddings) : (tensor<1x20x30x40xf16>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<4x14x19x40xf16> + %1 = "tf.Conv2D"(%0, %arg1) {data_format = "NHWC", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "VALID", strides = [1, 1, 1, 1]} : (tensor<4x14x19x40xf16>, tensor<5x5x40x32xf16>) -> tensor<4x10x15x32xf16> + %2 = "tf.BatchToSpaceND"(%1, %block_shape, %crops) : (tensor<4x10x15x32xf16>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<1x20x30x32xf16> + func.return %2 : tensor<1x20x30x32xf16> + + // CHECK-LABEL: testDilatedConvWithFp16 + // CHECK-SAME: ([[INPUT:%.*]]: tensor<1x20x30x40xf16>, [[FILTER:%.*]]: tensor<5x5x40x32xf16>) + // CHECK-NEXT: [[RESULT:%.*]] = "tf.Conv2D"([[INPUT]], [[FILTER]]) <{data_format = "NHWC", dilations = [1, 2, 2, 1], explicit_paddings = [], padding = "SAME", strides = [1, 1, 1, 1]}> : (tensor<1x20x30x40xf16>, tensor<5x5x40x32xf16>) -> tensor<1x20x30x32xf16> + // CHECK-NEXT: return [[RESULT]] : tensor<1x20x30x32xf16> +} + +func.func @testDilatedConvWithNonTrivialDilations(%arg0: tensor<1x128x128x3xf32>, %arg1: tensor<5x5x3x8xf32>) -> tensor<1x128x128x8xf32> { + %cst = arith.constant dense<[2, 2]> : tensor<2xi32> + %cst_0 = arith.constant dense<4> : tensor<2x2xi32> + %cst_1 = arith.constant dense<0> : tensor<2x2xi32> + %0 = "tf.SpaceToBatchND"(%arg0, %cst, %cst_0) : (tensor<1x128x128x3xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<4x68x68x3xf32> + %1 = "tf.Conv2D"(%0, %arg1) {padding = "VALID", dilations = [1, 2, 2, 1], strides = [1, 1, 1, 1]} : (tensor<4x68x68x3xf32>, tensor<5x5x3x8xf32>) -> tensor<4x60x60x8xf32> + %2 = "tf.BatchToSpaceND"(%1, %cst, %cst_1) : (tensor<4x60x60x8xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<1x128x128x8xf32> + func.return %2 : tensor<1x128x128x8xf32> + + // CHECK-LABEL: testDilatedConvWithNonTrivialDilations + // CHECK: [[STB:%.*]] = "tf.SpaceToBatchND" + // CHECK-NEXT: [[CONV:%.*]] = "tf.Conv2D" + // CHECK-NEXT: [[RESULT:%.*]] = "tf.BatchToSpaceND" + // CHECK-NEXT: return [[RESULT]] +} + +func.func @testDilatedDepthWiseConv(%arg0: tensor<1x128x128x3xf32>, %arg1: tensor<5x5x3x8xf32>) -> tensor<1x128x128x8xf32> { + %cst = arith.constant dense<[2, 2]> : tensor<2xi32> + %cst_0 = arith.constant dense<4> : tensor<2x2xi32> + %cst_1 = arith.constant dense<0> : tensor<2x2xi32> + %0 = "tf.SpaceToBatchND"(%arg0, %cst, %cst_0) : (tensor<1x128x128x3xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<4x68x68x3xf32> + %1 = "tf.DepthwiseConv2dNative"(%0, %arg1) {padding = "VALID", strides = [1, 1, 1, 1]} : (tensor<4x68x68x3xf32>, tensor<5x5x3x8xf32>) -> tensor<4x64x64x8xf32> + %2 = "tf.BatchToSpaceND"(%1, %cst, %cst_1) : (tensor<4x64x64x8xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<1x128x128x8xf32> + func.return %2 : tensor<1x128x128x8xf32> + + // CHECK-LABEL: testDilatedDepthWiseConv + // CHECK-SAME: ([[INPUT:%.*]]: tensor<1x128x128x3xf32>, [[FILTER:%.*]]: tensor<5x5x3x8xf32>) + // CHECK-NEXT: [[RESULT:%.*]] = "tf.DepthwiseConv2dNative"([[INPUT]], [[FILTER]]) <{dilations = [1, 2, 2, 1], padding = "SAME", strides = [1, 1, 1, 1]}> : (tensor<1x128x128x3xf32>, tensor<5x5x3x8xf32>) -> tensor<1x128x128x8xf32> + // CHECK-NEXT: return [[RESULT]] : tensor<1x128x128x8xf32> +} + +func.func @testDilatedConvWithPad(%arg0: tensor<1x128x128x3xf32>, %arg1: tensor<5x5x3x8xf32>, %arg2: tensor<8xf32>) -> tensor<1x128x128x8xf32> { + %cst = arith.constant dense<[2, 2]> : tensor<2xi32> + %cst_0 = arith.constant dense<4> : tensor<2x2xi32> + %cst_1 = arith.constant dense<0> : tensor<2x2xi32> + %cst_2 = arith.constant dense<0> : tensor<4x2xi32> + %0 = "tf.SpaceToBatchND"(%arg0, %cst, %cst_0) : (tensor<1x128x128x3xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<4x68x68x3xf32> + %1 = "tf.Conv2D"(%0, %arg1) {padding = "VALID", strides = [1, 1, 1, 1]} : (tensor<4x68x68x3xf32>, tensor<5x5x3x8xf32>) -> tensor<4x64x64x8xf32> + %2 = "tf.Pad"(%1, %cst_2) : (tensor<4x64x64x8xf32>, tensor<4x2xi32>) -> tensor<4x64x64x8xf32> + %3 = "tf.BatchToSpaceND"(%2, %cst, %cst_1) : (tensor<4x64x64x8xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<1x128x128x8xf32> + %4 = "tf.BiasAdd"(%3, %arg2) : (tensor<1x128x128x8xf32>, tensor<8xf32>) -> tensor<1x128x128x8xf32> + func.return %4 : tensor<1x128x128x8xf32> + + // CHECK-LABEL: testDilatedConvWithPad + // CHECK-SAME: ([[INPUT:%.*]]: tensor<1x128x128x3xf32>, [[FILTER:%.*]]: tensor<5x5x3x8xf32>, [[BIAS:%.*]]: tensor<8xf32>) + // CHECK-NEXT: [[CONV:%.*]] = "tf.Conv2D"([[INPUT]], [[FILTER]]) <{dilations = [1, 2, 2, 1], padding = "SAME", strides = [1, 1, 1, 1]}> : (tensor<1x128x128x3xf32>, tensor<5x5x3x8xf32>) -> tensor<1x128x128x8xf32> + // CHECK-NEXT: [[RESULT:%.*]] = "tf.BiasAdd"([[CONV]], [[BIAS]]) : (tensor<1x128x128x8xf32>, tensor<8xf32>) -> tensor<1x128x128x8xf32> + // CHECK-NEXT: return [[RESULT]] : tensor<1x128x128x8xf32> +} + +func.func @testDilatedDepthWiseConvWithPad(%arg0: tensor<1x128x128x3xf32>, %arg1: tensor<5x5x3x8xf32>, %arg2: tensor<8xf32>) -> tensor<1x128x128x8xf32> { + %cst = arith.constant dense<[2, 2]> : tensor<2xi32> + %cst_0 = arith.constant dense<4> : tensor<2x2xi32> + %cst_1 = arith.constant dense<0> : tensor<2x2xi32> + %cst_2 = arith.constant dense<0> : tensor<4x2xi32> + %0 = "tf.SpaceToBatchND"(%arg0, %cst, %cst_0) : (tensor<1x128x128x3xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<4x68x68x3xf32> + %1 = "tf.DepthwiseConv2dNative"(%0, %arg1) {padding = "VALID", strides = [1, 1, 1, 1]} : (tensor<4x68x68x3xf32>, tensor<5x5x3x8xf32>) -> tensor<4x64x64x8xf32> + %2 = "tf.Pad"(%1, %cst_2) : (tensor<4x64x64x8xf32>, tensor<4x2xi32>) -> tensor<4x64x64x8xf32> + %3 = "tf.BatchToSpaceND"(%2, %cst, %cst_1) : (tensor<4x64x64x8xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<1x128x128x8xf32> + %4 = "tf.BiasAdd"(%3, %arg2) : (tensor<1x128x128x8xf32>, tensor<8xf32>) -> tensor<1x128x128x8xf32> + func.return %4 : tensor<1x128x128x8xf32> + + // CHECK-LABEL: testDilatedDepthWiseConvWithPad + // CHECK-SAME: ([[INPUT:%.*]]: tensor<1x128x128x3xf32>, [[FILTER:%.*]]: tensor<5x5x3x8xf32>, [[BIAS:%.*]]: tensor<8xf32>) + // CHECK-NEXT: [[CONV:%.*]] = "tf.DepthwiseConv2dNative"([[INPUT]], [[FILTER]]) <{dilations = [1, 2, 2, 1], padding = "SAME", strides = [1, 1, 1, 1]}> : (tensor<1x128x128x3xf32>, tensor<5x5x3x8xf32>) -> tensor<1x128x128x8xf32> + // CHECK-NEXT: [[RESULT:%.*]] = "tf.BiasAdd"([[CONV]], [[BIAS]]) : (tensor<1x128x128x8xf32>, tensor<8xf32>) -> tensor<1x128x128x8xf32> + // CHECK-NEXT: return [[RESULT]] : tensor<1x128x128x8xf32> +} + +func.func @testDilatedConvWithBiasAdd(%arg0: tensor<1x128x128x3xf32>, %arg1: tensor<5x5x3x8xf32>, %arg2: tensor<8xf32>) -> tensor<1x128x128x8xf32> { + %cst = arith.constant dense<[2, 2]> : tensor<2xi32> + %cst_0 = arith.constant dense<4> : tensor<2x2xi32> + %cst_1 = arith.constant dense<0> : tensor<2x2xi32> + %0 = "tf.SpaceToBatchND"(%arg0, %cst, %cst_0) : (tensor<1x128x128x3xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<4x68x68x3xf32> + %1 = "tf.Conv2D"(%0, %arg1) {padding = "VALID", strides = [1, 1, 1, 1]} : (tensor<4x68x68x3xf32>, tensor<5x5x3x8xf32>) -> tensor<4x64x64x8xf32> + %2 = "tf.BatchToSpaceND"(%1, %cst, %cst_1) : (tensor<4x64x64x8xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<1x128x128x8xf32> + %3 = "tf.BiasAdd"(%2, %arg2) : (tensor<1x128x128x8xf32>, tensor<8xf32>) -> tensor<1x128x128x8xf32> + func.return %3 : tensor<1x128x128x8xf32> + + // CHECK-LABEL: testDilatedConvWithBiasAdd + // CHECK-SAME: ([[INPUT:%.*]]: tensor<1x128x128x3xf32>, [[FILTER:%.*]]: tensor<5x5x3x8xf32>, [[BIAS:%.*]]: tensor<8xf32>) + // CHECK-NEXT: [[CONV:%.*]] = "tf.Conv2D"([[INPUT]], [[FILTER]]) <{dilations = [1, 2, 2, 1], padding = "SAME", strides = [1, 1, 1, 1]}> : (tensor<1x128x128x3xf32>, tensor<5x5x3x8xf32>) -> tensor<1x128x128x8xf32> + // CHECK-NEXT: [[RESULT:%.*]] = "tf.BiasAdd"([[CONV]], [[BIAS]]) : (tensor<1x128x128x8xf32>, tensor<8xf32>) -> tensor<1x128x128x8xf32> + // CHECK-NEXT: return [[RESULT]] : tensor<1x128x128x8xf32> +} + +func.func @testDilatedDepthWiseConvWithBiasAdd(%arg0: tensor<1x128x128x3xf32>, %arg1: tensor<5x5x3x8xf32>, %arg2: tensor<8xf32>) -> tensor<1x128x128x8xf32> { + %cst = arith.constant dense<[2, 2]> : tensor<2xi32> + %cst_0 = arith.constant dense<4> : tensor<2x2xi32> + %cst_1 = arith.constant dense<0> : tensor<2x2xi32> + %0 = "tf.SpaceToBatchND"(%arg0, %cst, %cst_0) : (tensor<1x128x128x3xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<4x68x68x3xf32> + %1 = "tf.DepthwiseConv2dNative"(%0, %arg1) {padding = "VALID", strides = [1, 1, 1, 1]} : (tensor<4x68x68x3xf32>, tensor<5x5x3x8xf32>) -> tensor<4x64x64x8xf32> + %2 = "tf.BatchToSpaceND"(%1, %cst, %cst_1) : (tensor<4x64x64x8xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<1x128x128x8xf32> + %3 = "tf.BiasAdd"(%2, %arg2) : (tensor<1x128x128x8xf32>, tensor<8xf32>) -> tensor<1x128x128x8xf32> + func.return %3 : tensor<1x128x128x8xf32> + + // CHECK-LABEL: testDilatedDepthWiseConvWithBiasAdd + // CHECK-SAME: ([[INPUT:%.*]]: tensor<1x128x128x3xf32>, [[FILTER:%.*]]: tensor<5x5x3x8xf32>, [[BIAS:%.*]]: tensor<8xf32>) + // CHECK-NEXT: [[CONV:%.*]] = "tf.DepthwiseConv2dNative"([[INPUT]], [[FILTER]]) <{dilations = [1, 2, 2, 1], padding = "SAME", strides = [1, 1, 1, 1]}> : (tensor<1x128x128x3xf32>, tensor<5x5x3x8xf32>) -> tensor<1x128x128x8xf32> + // CHECK-NEXT: [[RESULT:%.*]] = "tf.BiasAdd"([[CONV]], [[BIAS]]) : (tensor<1x128x128x8xf32>, tensor<8xf32>) -> tensor<1x128x128x8xf32> + // CHECK-NEXT: return [[RESULT]] : tensor<1x128x128x8xf32> +} + +func.func @testDilatedConvWithExpandSqueeze1(%arg0: tensor<1x128x128xf32>, %arg1: tensor<5x5x1x1xf32>, %arg2: tensor<128xf32>) -> tensor<1x128x128xf32> { + %cst = arith.constant dense<[2, 2]> : tensor<2xi32> + %cst_0 = "tf.Const"() { value = dense<3> : tensor } : () -> tensor + %cst_1 = arith.constant dense<4> : tensor<2x2xi32> + %cst_2 = arith.constant dense<0> : tensor<2x2xi32> + %0 = "tf.SpaceToBatchND"(%arg0, %cst, %cst_1) : (tensor<1x128x128xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<4x68x68xf32> + %1 = "tf.ExpandDims"(%0, %cst_0) : (tensor<4x68x68xf32>, tensor) -> tensor<4x68x68x1xf32> + %2 = "tf.Conv2D"(%1, %arg1) {padding = "VALID", strides = [1, 1, 1, 1]} : (tensor<4x68x68x1xf32>, tensor<5x5x1x1xf32>) -> tensor<4x64x64x1xf32> + %3 = "tf.Squeeze"(%2) {squeeze_dims = [3]} : (tensor<4x64x64x1xf32>) -> tensor<4x64x64xf32> + %4 = "tf.BatchToSpaceND"(%3, %cst, %cst_2) : (tensor<4x64x64xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<1x128x128xf32> + %5 = "tf.BiasAdd"(%4, %arg2) : (tensor<1x128x128xf32>, tensor<128xf32>) -> tensor<1x128x128xf32> + func.return %5 : tensor<1x128x128xf32> + + // CHECK-LABEL: testDilatedConvWithExpandSqueeze1 + // CHECK-SAME: ([[INPUT:%.*]]: tensor<1x128x128xf32>, [[FILTER:%.*]]: tensor<5x5x1x1xf32>, [[BIAS:%.*]]: tensor<128xf32>) + // CHECK-NEXT: [[AXIS:%.*]] = "tf.Const"() <{value = dense<3> : tensor}> : () -> tensor + // CHECK-NEXT: [[EXPAND:%.*]] = "tf.ExpandDims"([[INPUT]], [[AXIS]]) : (tensor<1x128x128xf32>, tensor) -> tensor<1x128x128x1xf32> + // CHECK-NEXT: [[CONV:%.*]] = "tf.Conv2D"([[EXPAND]], [[FILTER]]) <{dilations = [1, 2, 2, 1], padding = "SAME", strides = [1, 1, 1, 1]}> : (tensor<1x128x128x1xf32>, tensor<5x5x1x1xf32>) -> tensor<1x128x128x1xf32> + // CHECK-NEXT: [[SQUEEZE:%.*]] = "tf.Squeeze"([[CONV]]) <{squeeze_dims = [3]}> : (tensor<1x128x128x1xf32>) -> tensor<1x128x128xf32> + // CHECK-NEXT: [[RESULT:%.*]] = "tf.BiasAdd"([[SQUEEZE]], [[BIAS]]) : (tensor<1x128x128xf32>, tensor<128xf32>) -> tensor<1x128x128xf32> + // CHECK-NEXT: return [[RESULT]] : tensor<1x128x128xf32> +} + +func.func @testDilatedDepthWiseConvWithExpandSqueeze1(%arg0: tensor<1x128x128xf32>, %arg1: tensor<5x5x1x1xf32>, %arg2: tensor<128xf32>) -> tensor<1x128x128xf32> { + %cst = arith.constant dense<[2, 2]> : tensor<2xi32> + %cst_0 = "tf.Const"() { value = dense<3> : tensor } : () -> tensor + %cst_1 = arith.constant dense<4> : tensor<2x2xi32> + %cst_2 = arith.constant dense<0> : tensor<2x2xi32> + %0 = "tf.SpaceToBatchND"(%arg0, %cst, %cst_1) : (tensor<1x128x128xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<4x68x68xf32> + %1 = "tf.ExpandDims"(%0, %cst_0) : (tensor<4x68x68xf32>, tensor) -> tensor<4x68x68x1xf32> + %2 = "tf.DepthwiseConv2dNative"(%1, %arg1) {padding = "VALID", strides = [1, 1, 1, 1]} : (tensor<4x68x68x1xf32>, tensor<5x5x1x1xf32>) -> tensor<4x64x64x1xf32> + %3 = "tf.Squeeze"(%2) {squeeze_dims = [3]} : (tensor<4x64x64x1xf32>) -> tensor<4x64x64xf32> + %4 = "tf.BatchToSpaceND"(%3, %cst, %cst_2) : (tensor<4x64x64xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<1x128x128xf32> + %5 = "tf.BiasAdd"(%4, %arg2) : (tensor<1x128x128xf32>, tensor<128xf32>) -> tensor<1x128x128xf32> + func.return %5 : tensor<1x128x128xf32> + + // CHECK-LABEL: testDilatedDepthWiseConvWithExpandSqueeze1 + // CHECK-SAME: ([[INPUT:%.*]]: tensor<1x128x128xf32>, [[FILTER:%.*]]: tensor<5x5x1x1xf32>, [[BIAS:%.*]]: tensor<128xf32>) + // CHECK-NEXT: [[AXIS:%.*]] = "tf.Const"() <{value = dense<3> : tensor}> : () -> tensor + // CHECK-NEXT: [[EXPAND:%.*]] = "tf.ExpandDims"([[INPUT]], [[AXIS]]) : (tensor<1x128x128xf32>, tensor) -> tensor<1x128x128x1xf32> + // CHECK-NEXT: [[CONV:%.*]] = "tf.DepthwiseConv2dNative"([[EXPAND]], [[FILTER]]) <{dilations = [1, 2, 2, 1], padding = "SAME", strides = [1, 1, 1, 1]}> : (tensor<1x128x128x1xf32>, tensor<5x5x1x1xf32>) -> tensor<1x128x128x1xf32> + // CHECK-NEXT: [[SQUEEZE:%.*]] = "tf.Squeeze"([[CONV]]) <{squeeze_dims = [3]}> : (tensor<1x128x128x1xf32>) -> tensor<1x128x128xf32> + // CHECK-NEXT: [[RESULT:%.*]] = "tf.BiasAdd"([[SQUEEZE]], [[BIAS]]) : (tensor<1x128x128xf32>, tensor<128xf32>) -> tensor<1x128x128xf32> + // CHECK-NEXT: return [[RESULT]] : tensor<1x128x128xf32> +} + +func.func @testDilatedConvWithExpandSqueeze2(%arg0: tensor<1x128x128xf32>, %arg1: tensor<5x5x1x1xf32>, %arg2: tensor) -> tensor<1x128x128xf32> { + %cst = arith.constant dense<[2, 2]> : tensor<2xi32> + %cst_0 = "tf.Const"() { value = dense<3> : tensor } : () -> tensor + %cst_1 = arith.constant dense<4> : tensor<2x2xi32> + %cst_2 = arith.constant dense<0> : tensor<2x2xi32> + %0 = "tf.SpaceToBatchND"(%arg0, %cst, %cst_1) : (tensor<1x128x128xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<4x?x?xf32> + %1 = "tf.ExpandDims"(%0, %cst_0) : (tensor<4x?x?xf32>, tensor) -> tensor<4x?x?x1xf32> + %2 = "tf.Conv2D"(%1, %arg1) {padding = "VALID", strides = [1, 1, 1, 1]} : (tensor<4x?x?x1xf32>, tensor<5x5x1x1xf32>) -> tensor<4x?x?x1xf32> + %3 = "tf.Squeeze"(%2) {squeeze_dims = [3]} : (tensor<4x?x?x1xf32>) -> tensor<4x?x?xf32> + %4 = "tf.BiasAdd"(%3, %arg2) : (tensor<4x?x?xf32>, tensor) -> tensor<4x?x?xf32> + %5 = "tf.BatchToSpaceND"(%4, %cst, %cst_2) : (tensor<4x?x?xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<1x128x128xf32> + func.return %5 : tensor<1x128x128xf32> + + // CHECK-LABEL: testDilatedConvWithExpandSqueeze2 + // CHECK-SAME: ([[INPUT:%.*]]: tensor<1x128x128xf32>, [[FILTER:%.*]]: tensor<5x5x1x1xf32>, [[BIAS:%.*]]: tensor) + // CHECK-NEXT: [[AXIS:%.*]] = "tf.Const"() <{value = dense<3> : tensor}> : () -> tensor + // CHECK-NEXT: [[EXPAND:%.*]] = "tf.ExpandDims"([[INPUT]], [[AXIS]]) : (tensor<1x128x128xf32>, tensor) -> tensor<1x128x128x1xf32> + // CHECK-NEXT: [[CONV:%.*]] = "tf.Conv2D"([[EXPAND]], [[FILTER]]) <{dilations = [1, 2, 2, 1], padding = "SAME", strides = [1, 1, 1, 1]}> : (tensor<1x128x128x1xf32>, tensor<5x5x1x1xf32>) -> tensor<1x128x128x1xf32> + // CHECK-NEXT: [[SQUEEZE:%.*]] = "tf.Squeeze"([[CONV]]) <{squeeze_dims = [3]}> : (tensor<1x128x128x1xf32>) -> tensor<1x128x128xf32> + // CHECK-NEXT: [[RESULT:%.*]] = "tf.BiasAdd"([[SQUEEZE]], [[BIAS]]) : (tensor<1x128x128xf32>, tensor) -> tensor<1x128x128xf32> + // CHECK-NEXT: return [[RESULT]] : tensor<1x128x128xf32> +} + +func.func @testDilatedDepthWiseConvWithExpandSqueeze2(%arg0: tensor<1x128x128xf32>, %arg1: tensor<5x5x1x1xf32>, %arg2: tensor) -> tensor<1x128x128xf32> { + %cst = arith.constant dense<[2, 2]> : tensor<2xi32> + %cst_0 = "tf.Const"() { value = dense<3> : tensor } : () -> tensor + %cst_1 = arith.constant dense<4> : tensor<2x2xi32> + %cst_2 = arith.constant dense<0> : tensor<2x2xi32> + %0 = "tf.SpaceToBatchND"(%arg0, %cst, %cst_1) : (tensor<1x128x128xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<4x?x?xf32> + %1 = "tf.ExpandDims"(%0, %cst_0) : (tensor<4x?x?xf32>, tensor) -> tensor<4x?x?x1xf32> + %2 = "tf.DepthwiseConv2dNative"(%1, %arg1) {padding = "VALID", strides = [1, 1, 1, 1]} : (tensor<4x?x?x1xf32>, tensor<5x5x1x1xf32>) -> tensor<4x?x?x1xf32> + %3 = "tf.Squeeze"(%2) {squeeze_dims = [3]} : (tensor<4x?x?x1xf32>) -> tensor<4x?x?xf32> + %4 = "tf.BiasAdd"(%3, %arg2) : (tensor<4x?x?xf32>, tensor) -> tensor<4x?x?xf32> + %5 = "tf.BatchToSpaceND"(%4, %cst, %cst_2) : (tensor<4x?x?xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<1x128x128xf32> + func.return %5 : tensor<1x128x128xf32> + + // CHECK-LABEL: testDilatedDepthWiseConvWithExpandSqueeze2 + // CHECK-SAME: ([[INPUT:%.*]]: tensor<1x128x128xf32>, [[FILTER:%.*]]: tensor<5x5x1x1xf32>, [[BIAS:%.*]]: tensor) + // CHECK-NEXT: [[AXIS:%.*]] = "tf.Const"() <{value = dense<3> : tensor}> : () -> tensor + // CHECK-NEXT: [[EXPAND:%.*]] = "tf.ExpandDims"([[INPUT]], [[AXIS]]) : (tensor<1x128x128xf32>, tensor) -> tensor<1x128x128x1xf32> + // CHECK-NEXT: [[CONV:%.*]] = "tf.DepthwiseConv2dNative"([[EXPAND]], [[FILTER]]) <{dilations = [1, 2, 2, 1], padding = "SAME", strides = [1, 1, 1, 1]}> : (tensor<1x128x128x1xf32>, tensor<5x5x1x1xf32>) -> tensor<1x128x128x1xf32> + // CHECK-NEXT: [[SQUEEZE:%.*]] = "tf.Squeeze"([[CONV]]) <{squeeze_dims = [3]}> : (tensor<1x128x128x1xf32>) -> tensor<1x128x128xf32> + // CHECK-NEXT: [[RESULT:%.*]] = "tf.BiasAdd"([[SQUEEZE]], [[BIAS]]) : (tensor<1x128x128xf32>, tensor) -> tensor<1x128x128xf32> + // CHECK-NEXT: return [[RESULT]] : tensor<1x128x128xf32> +} + +func.func @testDilatedConvWithExpandSqueeze3(%arg0: tensor<1x128x128xf32>, %arg1: tensor<5x5x1x1xf32>, %arg2: tensor<128xf32>) -> tensor<1x128x128xf32> { + %cst = arith.constant dense<[2, 2]> : tensor<2xi32> + %cst_0 = "tf.Const"() { value = dense<3> : tensor } : () -> tensor + %cst_1 = arith.constant dense<4> : tensor<2x2xi32> + %cst_2 = arith.constant dense<0> : tensor<2x2xi32> + %cst_3 = arith.constant dense<0> : tensor<3x2xi32> + %0 = "tf.SpaceToBatchND"(%arg0, %cst, %cst_1) : (tensor<1x128x128xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<4x68x68xf32> + %1 = "tf.ExpandDims"(%0, %cst_0) : (tensor<4x68x68xf32>, tensor) -> tensor<4x68x68x1xf32> + %2 = "tf.Conv2D"(%1, %arg1) {padding = "VALID", strides = [1, 1, 1, 1]} : (tensor<4x68x68x1xf32>, tensor<5x5x1x1xf32>) -> tensor<4x64x64x1xf32> + %3 = "tf.Squeeze"(%2) {squeeze_dims = [3]} : (tensor<4x64x64x1xf32>) -> tensor<4x64x64xf32> + %4 = "tf.Pad"(%3, %cst_3) : (tensor<4x64x64xf32>, tensor<3x2xi32>) -> tensor<4x64x64xf32> + %5 = "tf.BatchToSpaceND"(%4, %cst, %cst_2) : (tensor<4x64x64xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<1x128x128xf32> + %6 = "tf.BiasAdd"(%5, %arg2) : (tensor<1x128x128xf32>, tensor<128xf32>) -> tensor<1x128x128xf32> + func.return %6 : tensor<1x128x128xf32> + + // CHECK-LABEL: testDilatedConvWithExpandSqueeze3 + // CHECK-SAME: ([[INPUT:%.*]]: tensor<1x128x128xf32>, [[FILTER:%.*]]: tensor<5x5x1x1xf32>, [[BIAS:%.*]]: tensor<128xf32>) + // CHECK-NEXT: [[AXIS:%.*]] = "tf.Const"() <{value = dense<3> : tensor}> : () -> tensor + // CHECK-NEXT: [[EXPAND:%.*]] = "tf.ExpandDims"([[INPUT]], [[AXIS]]) : (tensor<1x128x128xf32>, tensor) -> tensor<1x128x128x1xf32> + // CHECK-NEXT: [[CONV:%.*]] = "tf.Conv2D"([[EXPAND]], [[FILTER]]) <{dilations = [1, 2, 2, 1], padding = "SAME", strides = [1, 1, 1, 1]}> : (tensor<1x128x128x1xf32>, tensor<5x5x1x1xf32>) -> tensor<1x128x128x1xf32> + // CHECK-NEXT: [[SQUEEZE:%.*]] = "tf.Squeeze"([[CONV]]) <{squeeze_dims = [3]}> : (tensor<1x128x128x1xf32>) -> tensor<1x128x128xf32> + // CHECK-NEXT: [[RESULT:%.*]] = "tf.BiasAdd"([[SQUEEZE]], [[BIAS]]) : (tensor<1x128x128xf32>, tensor<128xf32>) -> tensor<1x128x128xf32> + // CHECK-NEXT: return [[RESULT]] : tensor<1x128x128xf32> +} + +func.func @testDilatedDepthWiseConvWithExpandSqueeze3(%arg0: tensor<1x128x128xf32>, %arg1: tensor<5x5x1x1xf32>, %arg2: tensor<128xf32>) -> tensor<1x128x128xf32> { + %cst = arith.constant dense<[2, 2]> : tensor<2xi32> + %cst_0 = "tf.Const"() { value = dense<3> : tensor } : () -> tensor + %cst_1 = arith.constant dense<4> : tensor<2x2xi32> + %cst_2 = arith.constant dense<0> : tensor<2x2xi32> + %cst_3 = arith.constant dense<0> : tensor<3x2xi32> + %0 = "tf.SpaceToBatchND"(%arg0, %cst, %cst_1) : (tensor<1x128x128xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<4x68x68xf32> + %1 = "tf.ExpandDims"(%0, %cst_0) : (tensor<4x68x68xf32>, tensor) -> tensor<4x68x68x1xf32> + %2 = "tf.DepthwiseConv2dNative"(%1, %arg1) {padding = "VALID", strides = [1, 1, 1, 1]} : (tensor<4x68x68x1xf32>, tensor<5x5x1x1xf32>) -> tensor<4x64x64x1xf32> + %3 = "tf.Squeeze"(%2) {squeeze_dims = [3]} : (tensor<4x64x64x1xf32>) -> tensor<4x64x64xf32> + %4 = "tf.Pad"(%3, %cst_3) : (tensor<4x64x64xf32>, tensor<3x2xi32>) -> tensor<4x64x64xf32> + %5 = "tf.BatchToSpaceND"(%4, %cst, %cst_2) : (tensor<4x64x64xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<1x128x128xf32> + %6 = "tf.BiasAdd"(%5, %arg2) : (tensor<1x128x128xf32>, tensor<128xf32>) -> tensor<1x128x128xf32> + func.return %6 : tensor<1x128x128xf32> + + // CHECK-LABEL: testDilatedDepthWiseConvWithExpandSqueeze3 + // CHECK-SAME: ([[INPUT:%.*]]: tensor<1x128x128xf32>, [[FILTER:%.*]]: tensor<5x5x1x1xf32>, [[BIAS:%.*]]: tensor<128xf32>) + // CHECK-NEXT: [[AXIS:%.*]] = "tf.Const"() <{value = dense<3> : tensor}> : () -> tensor + // CHECK-NEXT: [[EXPAND:%.*]] = "tf.ExpandDims"([[INPUT]], [[AXIS]]) : (tensor<1x128x128xf32>, tensor) -> tensor<1x128x128x1xf32> + // CHECK-NEXT: [[CONV:%.*]] = "tf.DepthwiseConv2dNative"([[EXPAND]], [[FILTER]]) <{dilations = [1, 2, 2, 1], padding = "SAME", strides = [1, 1, 1, 1]}> : (tensor<1x128x128x1xf32>, tensor<5x5x1x1xf32>) -> tensor<1x128x128x1xf32> + // CHECK-NEXT: [[SQUEEZE:%.*]] = "tf.Squeeze"([[CONV]]) <{squeeze_dims = [3]}> : (tensor<1x128x128x1xf32>) -> tensor<1x128x128xf32> + // CHECK-NEXT: [[RESULT:%.*]] = "tf.BiasAdd"([[SQUEEZE]], [[BIAS]]) : (tensor<1x128x128xf32>, tensor<128xf32>) -> tensor<1x128x128xf32> + // CHECK-NEXT: return [[RESULT]] : tensor<1x128x128xf32> +} + +func.func @testAvoidDilatedConvWithExpand(%arg0: tensor<*xf32>, %arg1: tensor<5x5x1x1xf32>, %arg2: tensor<128xf32>) -> tensor<1x128x128xf32> { + %cst = arith.constant dense<[2, 2]> : tensor<2xi32> + %cst_0 = "tf.Const"() { value = dense<3> : tensor } : () -> tensor + %cst_1 = arith.constant dense<4> : tensor<2x2xi32> + %cst_2 = arith.constant dense<0> : tensor<2x2xi32> + %0 = "tf.SpaceToBatchND"(%arg0, %cst, %cst_1) : (tensor<*xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<4x68x68xf32> + %1 = "tf.ExpandDims"(%0, %cst_0) : (tensor<4x68x68xf32>, tensor) -> tensor<4x68x68x1xf32> + %2 = "tf.Conv2D"(%1, %arg1) {padding = "VALID", strides = [1, 1, 1, 1]} : (tensor<4x68x68x1xf32>, tensor<5x5x1x1xf32>) -> tensor<4x64x64x1xf32> + %3 = "tf.Squeeze"(%2) {squeeze_dims = [3]} : (tensor<4x64x64x1xf32>) -> tensor<4x64x64xf32> + %4 = "tf.BatchToSpaceND"(%3, %cst, %cst_2) : (tensor<4x64x64xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<1x128x128xf32> + %5 = "tf.BiasAdd"(%4, %arg2) : (tensor<1x128x128xf32>, tensor<128xf32>) -> tensor<1x128x128xf32> + func.return %5 : tensor<1x128x128xf32> + + // CHECK-LABEL: testAvoidDilatedConvWithExpand + // CHECK: "tf.SpaceToBatchND" + // CHECK: "tf.ExpandDims" + // CHECK: "tf.Conv2D" + // CHECK: "tf.Squeeze" + // CHECK: "tf.BatchToSpaceND" + // CHECK: "tf.BiasAdd" +} + +func.func @testDilatedConvWithDifferentExpandSqueezeAxis(%arg0: tensor<1x128x128xf32>, %arg1: tensor<5x5x1x1xf32>) -> tensor<1x128x128x1xf32> { + %cst = arith.constant dense<[2, 2]> : tensor<2xi32> + %cst_0 = "tf.Const"() { value = dense<3> : tensor } : () -> tensor + %cst_1 = arith.constant dense<4> : tensor<2x2xi32> + %cst_2 = arith.constant dense<0> : tensor<2x2xi32> + %0 = "tf.SpaceToBatchND"(%arg0, %cst, %cst_1) : (tensor<1x128x128xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<4x68x68xf32> + %1 = "tf.ExpandDims"(%0, %cst_0) : (tensor<4x68x68xf32>, tensor) -> tensor<4x68x68x1xf32> + %2 = "tf.Conv2D"(%1, %arg1) {padding = "VALID", strides = [1, 1, 1, 1]} : (tensor<4x68x68x1xf32>, tensor<5x5x1x1xf32>) -> tensor<4x64x64x1xf32> + %3 = "tf.Squeeze"(%2) {squeeze_dims = [2]} : (tensor<4x64x64x1xf32>) -> tensor<4x64x64x1xf32> + %4 = "tf.BatchToSpaceND"(%3, %cst, %cst_2) : (tensor<4x64x64x1xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<1x128x128x1xf32> + func.return %4 : tensor<1x128x128x1xf32> + + // CHECK-LABEL: testDilatedConvWithDifferentExpandSqueezeAxis + // CHECK: [[STB:%.*]] = "tf.SpaceToBatchND" + // CHECK-NEXT: [[EXPAND:%.*]] = "tf.ExpandDims" + // CHECK-NEXT: [[CONV:%.*]] = "tf.Conv2D" + // CHECK-NEXT: [[SQUEEZE:%.*]] = "tf.Squeeze" + // CHECK-NEXT: [[RESULT:%.*]] = "tf.BatchToSpaceND" + // CHECK-NEXT: return [[RESULT]] +} + +func.func @testNoDilatedConvWhenFirstDimIsDynamic(%arg0: tensor, %arg1: tensor<5x5x3x8xf32>) -> tensor { + %cst = arith.constant dense<[2, 2]> : tensor<2xi32> + %cst_0 = arith.constant dense<4> : tensor<2x2xi32> + %cst_1 = arith.constant dense<0> : tensor<2x2xi32> + %0 = "tf.SpaceToBatchND"(%arg0, %cst, %cst_0) : (tensor, tensor<2xi32>, tensor<2x2xi32>) -> tensor + %1 = "tf.Conv2D"(%0, %arg1) {padding = "VALID", strides = [1, 1, 1, 1]} : (tensor, tensor<5x5x3x8xf32>) -> tensor + %2 = "tf.BatchToSpaceND"(%1, %cst, %cst_1) : (tensor, tensor<2xi32>, tensor<2x2xi32>) -> tensor + func.return %2 : tensor + + // CHECK-LABEL: testNoDilatedConvWhenFirstDimIsDynamic + // CHECK: [[STB:%.*]] = "tf.SpaceToBatchND" + // CHECK-NEXT: [[CONV:%.*]] = "tf.Conv2D" + // CHECK-NEXT: [[RESULT:%.*]] = "tf.BatchToSpaceND" + // CHECK-NEXT: return [[RESULT]] +} + +func.func @testNoDilatedConvWhenLastDimIsDynamic(%arg0: tensor<1x128x128x?xf32>, %arg1: tensor<5x5x3x8xf32>) -> tensor<1x128x128x?xf32> { + %cst = arith.constant dense<[2, 2]> : tensor<2xi32> + %cst_0 = arith.constant dense<4> : tensor<2x2xi32> + %cst_1 = arith.constant dense<0> : tensor<2x2xi32> + %0 = "tf.SpaceToBatchND"(%arg0, %cst, %cst_0) : (tensor<1x128x128x?xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<4x68x68x?xf32> + %1 = "tf.Conv2D"(%0, %arg1) {padding = "VALID", strides = [1, 1, 1, 1]} : (tensor<4x68x68x?xf32>, tensor<5x5x3x8xf32>) -> tensor<4x64x64x?xf32> + %2 = "tf.BatchToSpaceND"(%1, %cst, %cst_1) : (tensor<4x64x64x?xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<1x128x128x?xf32> + func.return %2 : tensor<1x128x128x?xf32> + + // CHECK-LABEL: testNoDilatedConvWhenLastDimIsDynamic + // CHECK: [[STB:%.*]] = "tf.SpaceToBatchND" + // CHECK-NEXT: [[CONV:%.*]] = "tf.Conv2D" + // CHECK-NEXT: [[RESULT:%.*]] = "tf.BatchToSpaceND" + // CHECK-NEXT: return [[RESULT]] +} + +func.func @testNoDilatedConvWhenGivenInputIsNonFloatType(%arg0: tensor<1x128x128x3xi32>, %arg1: tensor<5x5x3x8xi32>) -> tensor<1x120x120x8xi32> { + %cst = arith.constant dense<[2, 2]> : tensor<2xi32> + %cst_0 = arith.constant dense<4> : tensor<2x2xi32> + %0 = "tf.SpaceToBatchND"(%arg0, %cst, %cst_0) : (tensor<1x128x128x3xi32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<4x68x68x3xi32> + %1 = "tf.Conv2D"(%0, %arg1) {padding = "VALID", strides = [1, 1, 1, 1]} : (tensor<4x68x68x3xi32>, tensor<5x5x3x8xi32>) -> tensor<4x64x64x8xi32> + %2 = "tf.BatchToSpaceND"(%1, %cst, %cst_0) : (tensor<4x64x64x8xi32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<1x120x120x8xi32> + func.return %2 : tensor<1x120x120x8xi32> + + // CHECK-LABEL: testNoDilatedConvWhenGivenInputIsNonFloatType + // CHECK: [[STB:%.*]] = "tf.SpaceToBatchND" + // CHECK-NEXT: [[CONV:%.*]] = "tf.Conv2D" + // CHECK-NEXT: [[RESULT:%.*]] = "tf.BatchToSpaceND" + // CHECK-NEXT: return [[RESULT]] +} + +func.func @testDilatedConv1DExpandH(%arg0: tensor<1x128x3xf32>, %arg1: tensor<1x5x3x8xf32>) -> tensor<1x128x8xf32> { + %cst = "tf.Const"() {value = dense<0> : tensor<1x2xi32>} : () -> tensor<1x2xi32> + %cst_0 = "tf.Const"() {value = dense<-3> : tensor} : () -> tensor + %cst_1 = "tf.Const"() {value = dense<2> : tensor<1xi32>} : () -> tensor<1xi32> + %cst_2 = "tf.Const"() {value = dense<4> : tensor<1x2xi32>} : () -> tensor<1x2xi32> + %0 = "tf.SpaceToBatchND"(%arg0, %cst_1, %cst_2) : (tensor<1x128x3xf32>, tensor<1xi32>, tensor<1x2xi32>) -> tensor<2x68x3xf32> + %1 = "tf.ExpandDims"(%0, %cst_0) : (tensor<2x68x3xf32>, tensor) -> tensor<2x1x68x3xf32> + %2 = "tf.Conv2D"(%1, %arg1) {padding = "VALID", strides = [1, 1, 1, 1]} : (tensor<2x1x68x3xf32>, tensor<1x5x3x8xf32>) -> tensor<2x1x64x8xf32> + %3 = "tf.Squeeze"(%2) {squeeze_dims = [-3]} : (tensor<2x1x64x8xf32>) -> tensor<2x64x8xf32> + %4 = "tf.BatchToSpaceND"(%3, %cst_1, %cst) : (tensor<2x64x8xf32>, tensor<1xi32>, tensor<1x2xi32>) -> tensor<1x128x8xf32> + func.return %4 : tensor<1x128x8xf32> + + // CHECK-LABEL: testDilatedConv1DExpandH + // CHECK-SAME: ([[INPUT:%.*]]: tensor<1x128x3xf32>, [[FILTER:%.*]]: tensor<1x5x3x8xf32>) + // CHECK-NEXT: [[AXIS:%.*]] = "tf.Const"() <{value = dense<-3> : tensor}> : () -> tensor + // CHECK-NEXT: [[EXPAND:%.*]] = "tf.ExpandDims"([[INPUT]], [[AXIS]]) : (tensor<1x128x3xf32>, tensor) -> tensor<1x1x128x3xf32> + // CHECK-NEXT: [[CONV:%.*]] = "tf.Conv2D"([[EXPAND]], [[FILTER]]) <{dilations = [1, 1, 2, 1], padding = "SAME", strides = [1, 1, 1, 1]}> : (tensor<1x1x128x3xf32>, tensor<1x5x3x8xf32>) -> tensor<1x1x128x8xf32> + // CHECK-NEXT: [[RESULT:%.*]] = "tf.Squeeze"([[CONV]]) <{squeeze_dims = [-3]}> : (tensor<1x1x128x8xf32>) -> tensor<1x128x8xf32> + // CHECK-NEXT: return [[RESULT]] : tensor<1x128x8xf32> +} + +func.func @testDilatedConv1DExpandHWithBiasAdd(%arg0: tensor<1x128x3xf32>, %arg1: tensor<1x5x3x8xf32>, %arg2: tensor<8xf32>) -> tensor<1x128x8xf32> { + %cst = "tf.Const"() {value = dense<0> : tensor<1x2xi32>} : () -> tensor<1x2xi32> + %cst_0 = "tf.Const"() {value = dense<-3> : tensor} : () -> tensor + %cst_1 = "tf.Const"() {value = dense<2> : tensor<1xi32>} : () -> tensor<1xi32> + %cst_2 = "tf.Const"() {value = dense<4> : tensor<1x2xi32>} : () -> tensor<1x2xi32> + %0 = "tf.SpaceToBatchND"(%arg0, %cst_1, %cst_2) : (tensor<1x128x3xf32>, tensor<1xi32>, tensor<1x2xi32>) -> tensor<2x68x3xf32> + %1 = "tf.ExpandDims"(%0, %cst_0) : (tensor<2x68x3xf32>, tensor) -> tensor<2x1x68x3xf32> + %2 = "tf.Conv2D"(%1, %arg1) {padding = "VALID", strides = [1, 1, 1, 1]} : (tensor<2x1x68x3xf32>, tensor<1x5x3x8xf32>) -> tensor<2x1x64x8xf32> + %3 = "tf.Squeeze"(%2) {squeeze_dims = [-3]} : (tensor<2x1x64x8xf32>) -> tensor<2x64x8xf32> + %4 = "tf.BatchToSpaceND"(%3, %cst_1, %cst) : (tensor<2x64x8xf32>, tensor<1xi32>, tensor<1x2xi32>) -> tensor<1x128x8xf32> + %5 = "tf.BiasAdd"(%4, %arg2) : (tensor<1x128x8xf32>, tensor<8xf32>) -> tensor<1x128x8xf32> + func.return %5 : tensor<1x128x8xf32> + + // CHECK-LABEL: testDilatedConv1DExpandHWithBiasAdd + // CHECK-SAME: ([[INPUT:%.*]]: tensor<1x128x3xf32>, [[FILTER:%.*]]: tensor<1x5x3x8xf32>, [[BIAS:%.*]]: tensor<8xf32>) + // CHECK-NEXT: [[AXIS:%.*]] = "tf.Const"() <{value = dense<-3> : tensor}> : () -> tensor + // CHECK-NEXT: [[EXPAND:%.*]] = "tf.ExpandDims"([[INPUT]], [[AXIS]]) : (tensor<1x128x3xf32>, tensor) -> tensor<1x1x128x3xf32> + // CHECK-NEXT: [[CONV:%.*]] = "tf.Conv2D"([[EXPAND]], [[FILTER]]) <{dilations = [1, 1, 2, 1], padding = "SAME", strides = [1, 1, 1, 1]}> : (tensor<1x1x128x3xf32>, tensor<1x5x3x8xf32>) -> tensor<1x1x128x8xf32> + // CHECK-NEXT: [[SQUEEZE:%.*]] = "tf.Squeeze"([[CONV]]) <{squeeze_dims = [-3]}> : (tensor<1x1x128x8xf32>) -> tensor<1x128x8xf32> + // CHECK-NEXT: [[RESULT:%.*]] = "tf.BiasAdd"([[SQUEEZE]], [[BIAS]]) : (tensor<1x128x8xf32>, tensor<8xf32>) -> tensor<1x128x8xf32> + // CHECK-NEXT: return [[RESULT]] : tensor<1x128x8xf32> +} + +func.func @testDilatedConv1DExpandW(%arg0: tensor<1x128x3xf32>, %arg1: tensor<5x1x3x8xf32>) -> tensor<1x128x8xf32> { + %cst = "tf.Const"() {value = dense<0> : tensor<1x2xi32>} : () -> tensor<1x2xi32> + %cst_0 = "tf.Const"() {value = dense<-2> : tensor} : () -> tensor + %cst_1 = "tf.Const"() {value = dense<2> : tensor<1xi32>} : () -> tensor<1xi32> + %cst_2 = "tf.Const"() {value = dense<4> : tensor<1x2xi32>} : () -> tensor<1x2xi32> + %0 = "tf.SpaceToBatchND"(%arg0, %cst_1, %cst_2) : (tensor<1x128x3xf32>, tensor<1xi32>, tensor<1x2xi32>) -> tensor<2x68x3xf32> + %1 = "tf.ExpandDims"(%0, %cst_0) : (tensor<2x68x3xf32>, tensor) -> tensor<2x68x1x3xf32> + %2 = "tf.Conv2D"(%1, %arg1) {padding = "VALID", strides = [1, 1, 1, 1]} : (tensor<2x68x1x3xf32>, tensor<5x1x3x8xf32>) -> tensor<2x64x1x8xf32> + %3 = "tf.Squeeze"(%2) {squeeze_dims = [-2]} : (tensor<2x64x1x8xf32>) -> tensor<2x64x8xf32> + %4 = "tf.BatchToSpaceND"(%3, %cst_1, %cst) : (tensor<2x64x8xf32>, tensor<1xi32>, tensor<1x2xi32>) -> tensor<1x128x8xf32> + func.return %4 : tensor<1x128x8xf32> + + // CHECK-LABEL: testDilatedConv1DExpandW + // CHECK-SAME: ([[INPUT:%.*]]: tensor<1x128x3xf32>, [[FILTER:%.*]]: tensor<5x1x3x8xf32>) + // CHECK-NEXT: [[AXIS:%.*]] = "tf.Const"() <{value = dense<-2> : tensor}> : () -> tensor + // CHECK-NEXT: [[EXPAND:%.*]] = "tf.ExpandDims"([[INPUT]], [[AXIS]]) : (tensor<1x128x3xf32>, tensor) -> tensor<1x128x1x3xf32> + // CHECK-NEXT: [[CONV:%.*]] = "tf.Conv2D"([[EXPAND]], [[FILTER]]) <{dilations = [1, 2, 1, 1], padding = "SAME", strides = [1, 1, 1, 1]}> : (tensor<1x128x1x3xf32>, tensor<5x1x3x8xf32>) -> tensor<1x128x1x8xf32> + // CHECK-NEXT: [[RESULT:%.*]] = "tf.Squeeze"([[CONV]]) <{squeeze_dims = [-2]}> : (tensor<1x128x1x8xf32>) -> tensor<1x128x8xf32> + // CHECK-NEXT: return [[RESULT]] : tensor<1x128x8xf32> +} + +func.func @testDilatedConv1DExpandWWithBiasAdd(%arg0: tensor<1x128x3xf32>, %arg1: tensor<5x1x3x8xf32>, %arg2: tensor<8xf32>) -> tensor<1x128x8xf32> { + %cst = "tf.Const"() {value = dense<0> : tensor<1x2xi32>} : () -> tensor<1x2xi32> + %cst_0 = "tf.Const"() {value = dense<-2> : tensor} : () -> tensor + %cst_1 = "tf.Const"() {value = dense<2> : tensor<1xi32>} : () -> tensor<1xi32> + %cst_2 = "tf.Const"() {value = dense<4> : tensor<1x2xi32>} : () -> tensor<1x2xi32> + %0 = "tf.SpaceToBatchND"(%arg0, %cst_1, %cst_2) : (tensor<1x128x3xf32>, tensor<1xi32>, tensor<1x2xi32>) -> tensor<2x68x3xf32> + %1 = "tf.ExpandDims"(%0, %cst_0) : (tensor<2x68x3xf32>, tensor) -> tensor<2x68x1x3xf32> + %2 = "tf.Conv2D"(%1, %arg1) {padding = "VALID", strides = [1, 1, 1, 1]} : (tensor<2x68x1x3xf32>, tensor<5x1x3x8xf32>) -> tensor<2x64x1x8xf32> + %3 = "tf.Squeeze"(%2) {squeeze_dims = [-2]} : (tensor<2x64x1x8xf32>) -> tensor<2x64x8xf32> + %4 = "tf.BatchToSpaceND"(%3, %cst_1, %cst) : (tensor<2x64x8xf32>, tensor<1xi32>, tensor<1x2xi32>) -> tensor<1x128x8xf32> + %5 = "tf.BiasAdd"(%4, %arg2) : (tensor<1x128x8xf32>, tensor<8xf32>) -> tensor<1x128x8xf32> + func.return %5 : tensor<1x128x8xf32> + + // CHECK-LABEL: testDilatedConv1DExpandWWithBiasAdd + // CHECK-SAME: ([[INPUT:%.*]]: tensor<1x128x3xf32>, [[FILTER:%.*]]: tensor<5x1x3x8xf32>, [[BIAS:%.*]]: tensor<8xf32>) + // CHECK-NEXT: [[AXIS:%.*]] = "tf.Const"() <{value = dense<-2> : tensor}> : () -> tensor + // CHECK-NEXT: [[EXPAND:%.*]] = "tf.ExpandDims"([[INPUT]], [[AXIS]]) : (tensor<1x128x3xf32>, tensor) -> tensor<1x128x1x3xf32> + // CHECK-NEXT: [[CONV:%.*]] = "tf.Conv2D"([[EXPAND]], [[FILTER]]) <{dilations = [1, 2, 1, 1], padding = "SAME", strides = [1, 1, 1, 1]}> : (tensor<1x128x1x3xf32>, tensor<5x1x3x8xf32>) -> tensor<1x128x1x8xf32> + // CHECK-NEXT: [[SQUEEZE:%.*]] = "tf.Squeeze"([[CONV]]) <{squeeze_dims = [-2]}> : (tensor<1x128x1x8xf32>) -> tensor<1x128x8xf32> + // CHECK-NEXT: [[RESULT:%.*]] = "tf.BiasAdd"([[SQUEEZE]], [[BIAS]]) : (tensor<1x128x8xf32>, tensor<8xf32>) -> tensor<1x128x8xf32> + // CHECK-NEXT: return [[RESULT]] : tensor<1x128x8xf32> +} + +func.func @testDilatedConv1DWithMixedPostiveAndNegativeAxis(%arg0: tensor<1x128x3xf32>, %arg1: tensor<1x5x3x8xf32>) -> tensor<1x128x8xf32> { + %cst = "tf.Const"() {value = dense<0> : tensor<1x2xi32>} : () -> tensor<1x2xi32> + %cst_0 = "tf.Const"() {value = dense<1> : tensor} : () -> tensor + %cst_1 = "tf.Const"() {value = dense<2> : tensor<1xi32>} : () -> tensor<1xi32> + %cst_2 = "tf.Const"() {value = dense<4> : tensor<1x2xi32>} : () -> tensor<1x2xi32> + %0 = "tf.SpaceToBatchND"(%arg0, %cst_1, %cst_2) : (tensor<1x128x3xf32>, tensor<1xi32>, tensor<1x2xi32>) -> tensor<2x68x3xf32> + %1 = "tf.ExpandDims"(%0, %cst_0) : (tensor<2x68x3xf32>, tensor) -> tensor<2x1x68x3xf32> + %2 = "tf.Conv2D"(%1, %arg1) {padding = "VALID", strides = [1, 1, 1, 1]} : (tensor<2x1x68x3xf32>, tensor<1x5x3x8xf32>) -> tensor<2x1x64x8xf32> + %3 = "tf.Squeeze"(%2) {squeeze_dims = [-3]} : (tensor<2x1x64x8xf32>) -> tensor<2x64x8xf32> + %4 = "tf.BatchToSpaceND"(%3, %cst_1, %cst) : (tensor<2x64x8xf32>, tensor<1xi32>, tensor<1x2xi32>) -> tensor<1x128x8xf32> + func.return %4 : tensor<1x128x8xf32> + + // CHECK-LABEL: testDilatedConv1DWithMixedPostiveAndNegativeAxis + // CHECK-SAME: ([[INPUT:%.*]]: tensor<1x128x3xf32>, [[FILTER:%.*]]: tensor<1x5x3x8xf32>) + // CHECK-NEXT: [[AXIS:%.*]] = "tf.Const"() <{value = dense<1> : tensor}> : () -> tensor + // CHECK-NEXT: [[EXPAND:%.*]] = "tf.ExpandDims"([[INPUT]], [[AXIS]]) : (tensor<1x128x3xf32>, tensor) -> tensor<1x1x128x3xf32> + // CHECK-NEXT: [[CONV:%.*]] = "tf.Conv2D"([[EXPAND]], [[FILTER]]) <{dilations = [1, 1, 2, 1], padding = "SAME", strides = [1, 1, 1, 1]}> : (tensor<1x1x128x3xf32>, tensor<1x5x3x8xf32>) -> tensor<1x1x128x8xf32> + // CHECK-NEXT: [[RESULT:%.*]] = "tf.Squeeze"([[CONV]]) <{squeeze_dims = [-3]}> : (tensor<1x1x128x8xf32>) -> tensor<1x128x8xf32> + // CHECK-NEXT: return [[RESULT]] : tensor<1x128x8xf32> +} + +func.func @testPaddedDilatedConv(%arg0 : tensor<2x1920x64xf32>) -> tensor<2x1920x128xf32> { + %0 = "tf.Const"() {value = dense<[[0, 0], [2, 0], [0, 0]]> : tensor<3x2xi32>} : () -> tensor<3x2xi32> + %1 = "tf.Const"() {value = dense<2> : tensor<1xi32>} : () -> tensor<1xi32> + %2 = "tf.Const"() {value = dense<2> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<0> : tensor<1x2xi32>} : () -> tensor<1x2xi32> + %4 = "tf.Const"() {value = dense<0.0> : tensor<3x1x64x128xf32>} : () -> tensor<3x1x64x128xf32> + %5 = "tf.SpaceToBatchND"(%arg0, %1, %3) {device = ""} : (tensor<2x1920x64xf32>, tensor<1xi32>, tensor<1x2xi32>) -> tensor<4x960x64xf32> + %6 = "tf.ExpandDims"(%5, %2) {device = ""} : (tensor<4x960x64xf32>, tensor) -> tensor<4x960x1x64xf32> + %7 = "tf.Conv2D"(%6, %4) {data_format = "NHWC", device = "", dilations = [1, 1, 1, 1], explicit_paddings = [], padding = "VALID", strides = [1, 1, 1, 1], use_cudnn_on_gpu = true} : (tensor<4x960x1x64xf32>, tensor<3x1x64x128xf32>) -> tensor<4x958x1x128xf32> + %8 = "tf.Squeeze"(%7) {device = "", squeeze_dims = [2]} : (tensor<4x958x1x128xf32>) -> tensor<4x958x128xf32> + %9 = "tf.Pad"(%8, %0) {device = ""} : (tensor<4x958x128xf32>, tensor<3x2xi32>) -> tensor<4x960x128xf32> + %10 = "tf.BatchToSpaceND"(%9, %1, %3) {device = ""} : (tensor<4x960x128xf32>, tensor<1xi32>, tensor<1x2xi32>) -> tensor<2x1920x128xf32> + func.return %10 : tensor<2x1920x128xf32> + + // CHECK-LABEL: testPaddedDilatedConv + // CHECK-SAME: ([[INPUT:%.*]]: tensor<2x1920x64xf32>) + // CHECK-NEXT: [[AXIS:%.*]] = "tf.Const"() <{value = dense<2> : tensor}> : () -> tensor + // CHECK-NEXT: [[FILTER:%.*]] = "tf.Const"() <{value = dense<0.000000e+00> : tensor<3x1x64x128xf32>}> : () -> tensor<3x1x64x128xf32> + // CHECK-NEXT: [[EXPAND:%.*]] = "tf.ExpandDims"([[INPUT]], [[AXIS]]) {device = ""} : (tensor<2x1920x64xf32>, tensor) -> tensor<2x1920x1x64xf32> + // CHECK-NEXT: [[CONV:%.*]] = "tf.Conv2D"([[EXPAND]], [[FILTER]]) <{data_format = "NHWC", dilations = [1, 2, 1, 1], explicit_paddings = [], padding = "SAME", strides = [1, 1, 1, 1], use_cudnn_on_gpu = true}> {device = ""} : (tensor<2x1920x1x64xf32>, tensor<3x1x64x128xf32>) -> tensor<2x1920x1x128xf32> + // CHECK-NEXT: [[RESULT:%.*]] = "tf.Squeeze"([[CONV]]) <{squeeze_dims = [2]}> {device = ""} : (tensor<2x1920x1x128xf32>) -> tensor<2x1920x128xf32> + // CHECK-NEXT: return [[RESULT]] : tensor<2x1920x128xf32> +} + +func.func @testDilatedConvInterleaved(%arg0: tensor<1x128x128x3xf32>, %arg1: tensor<5x5x3x8xf32>) -> (tensor<1x120x120x8xf32>, tensor<1x120x120x8xf32>) { + %cst = arith.constant dense<[2, 2]> : tensor<2xi32> + %cst_0 = arith.constant dense<4> : tensor<2x2xi32> + %0 = "tf.SpaceToBatchND"(%arg0, %cst, %cst_0) : (tensor<1x128x128x3xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<4x68x68x3xf32> + %1 = "tf.SpaceToBatchND"(%arg0, %cst, %cst_0) : (tensor<1x128x128x3xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<4x68x68x3xf32> + %2 = "tf.Conv2D"(%0, %arg1) {padding = "VALID", strides = [1, 1, 1, 1]} : (tensor<4x68x68x3xf32>, tensor<5x5x3x8xf32>) -> tensor<4x64x64x8xf32> + %3 = "tf.Conv2D"(%1, %arg1) {padding = "VALID", strides = [1, 1, 1, 1]} : (tensor<4x68x68x3xf32>, tensor<5x5x3x8xf32>) -> tensor<4x64x64x8xf32> + %4 = "tf.BatchToSpaceND"(%2, %cst, %cst_0) : (tensor<4x64x64x8xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<1x120x120x8xf32> + %5 = "tf.BatchToSpaceND"(%3, %cst, %cst_0) : (tensor<4x64x64x8xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<1x120x120x8xf32> + func.return %4, %5: tensor<1x120x120x8xf32>, tensor<1x120x120x8xf32> + + // CHECK-LABEL: testDilatedConvInterleaved + // CHECK-SAME: ([[INPUT:%.*]]: tensor<1x128x128x3xf32>, [[FILTER:%.*]]: tensor<5x5x3x8xf32>) + // CHECK-NEXT: [[RESULT0:%.*]] = "tf.Conv2D"([[INPUT]], [[FILTER]]) <{dilations = [1, 2, 2, 1], padding = "VALID", strides = [1, 1, 1, 1]}> : (tensor<1x128x128x3xf32>, tensor<5x5x3x8xf32>) -> tensor<1x120x120x8xf32> + // CHECK-NEXT: [[RESULT1:%.*]] = "tf.Conv2D"([[INPUT]], [[FILTER]]) <{dilations = [1, 2, 2, 1], padding = "VALID", strides = [1, 1, 1, 1]}> : (tensor<1x128x128x3xf32>, tensor<5x5x3x8xf32>) -> tensor<1x120x120x8xf32> + // CHECK-NEXT: return [[RESULT0]], [[RESULT1]] : tensor<1x120x120x8xf32>, tensor<1x120x120x8xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/downcast-x64.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/downcast-x64.mlir new file mode 100644 index 0000000000000000000000000000000000000000..9098dfc2ad205d41186e979d986a2a68742787fd --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/downcast-x64.mlir @@ -0,0 +1,32 @@ +// RUN: litert-opt %s --tfl-downcast-x64 --canonicalize | FileCheck %s + +// CHECK-LABEL: testFuncSignature +// CHECK: (%arg0: tensor<4xi32>, %arg1: tensor<2x2xf32>) -> tensor<2x2xf32> +func.func @testFuncSignature(%arg0: tensor<4xi64>, %arg1: tensor<2x2xf64>) -> tensor<2x2xf64> { + // CHECK: return %arg1 : tensor<2x2xf32> + func.return %arg1 : tensor<2x2xf64> +} + +// CHECK-LABEL: testConstantDowncast +func.func @testConstantDowncast() -> tensor<2xf64> { + // CHECK: %[[CST:.*]] = arith.constant dense<[1.000000e+00, 2.000000e+00]> : tensor<2xf32> + // CHECK: return %[[CST]] + %0 = arith.constant dense<[1.0, 2.0]> : tensor<2xf64> + func.return %0 : tensor<2xf64> +} + +// CHECK-LABEL: testI64ConstantDowncast +func.func @testI64ConstantDowncast() -> tensor { + // CHECK: %[[CST:.*]] = arith.constant dense<42> : tensor + // CHECK: return %[[CST]] + %0 = arith.constant dense<42> : tensor + func.return %0 : tensor +} + +// CHECK-LABEL: testGeneralOpForceConvert +func.func @testGeneralOpForceConvert(%arg0: tensor<2xf64>) -> tensor<2xf64> { + // CHECK: %[[ADD:.*]] = tfl.add %arg0, %arg0 {fused_activation_function = "NONE"} : tensor<2xf32> + // CHECK: return %[[ADD]] + %0 = "tfl.add"(%arg0, %arg0) {fused_activation_function = "NONE"} : (tensor<2xf64>, tensor<2xf64>) -> tensor<2xf64> + func.return %0 : tensor<2xf64> +} \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/elements-attr-roundtrip.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/elements-attr-roundtrip.mlir new file mode 100644 index 0000000000000000000000000000000000000000..1f32e084ce4b53813f12e9068c58b646ef2554c9 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/elements-attr-roundtrip.mlir @@ -0,0 +1,47 @@ +// RUN: litert-opt %s --tfl-dense-to-dense-resource-elements | env FILECHECK_OPTS="" FileCheck %s +// RUN: litert-opt %s --tfl-dense-to-dense-resource-elements | litert-opt --tfl-dense-resource-to-dense-elements | env FILECHECK_OPTS="" FileCheck %s --check-prefix=ROUNDTRIP + +// CHECK-LABEL: @test_f32 +// ROUNDTRIP-LABEL: @test_f32 +func.func @test_f32() -> tensor<4xf32> { + // CHECK: arith.constant dense_resource : tensor<4xf32> + // ROUNDTRIP: arith.constant dense<[1.000000e+00, 2.000000e+00, 3.000000e+00, 4.000000e+00]> : tensor<4xf32> + %0 = arith.constant dense<[1.0, 2.0, 3.0, 4.0]> : tensor<4xf32> + func.return %0 : tensor<4xf32> +} + +// CHECK-LABEL: @test_i32 +// ROUNDTRIP-LABEL: @test_i32 +func.func @test_i32() -> tensor<4xi32> { + // CHECK: arith.constant dense_resource : tensor<4xi32> + // ROUNDTRIP: arith.constant dense<[1, 2, 3, 4]> : tensor<4xi32> + %0 = arith.constant dense<[1, 2, 3, 4]> : tensor<4xi32> + func.return %0 : tensor<4xi32> +} + +// CHECK-LABEL: @test_i1 +// ROUNDTRIP-LABEL: @test_i1 +func.func @test_i1() -> tensor<4xi1> { + // CHECK: arith.constant dense_resource : tensor<4xi1> + // ROUNDTRIP: arith.constant dense<[true, false, true, false]> : tensor<4xi1> + %0 = arith.constant dense<[true, false, true, false]> : tensor<4xi1> + func.return %0 : tensor<4xi1> +} + +// CHECK-LABEL: @test_i64 +// ROUNDTRIP-LABEL: @test_i64 +func.func @test_i64() -> tensor<2xi64> { + // CHECK: arith.constant dense_resource : tensor<2xi64> + // ROUNDTRIP: arith.constant dense<[9223372036854775807, -9223372036854775808]> : tensor<2xi64> + %0 = arith.constant dense<[9223372036854775807, -9223372036854775808]> : tensor<2xi64> + func.return %0 : tensor<2xi64> +} + +// CHECK-LABEL: @test_f64 +// ROUNDTRIP-LABEL: @test_f64 +func.func @test_f64() -> tensor<2xf64> { + // CHECK: arith.constant dense_resource : tensor<2xf64> + // ROUNDTRIP: arith.constant dense<[1.234567, 0.89101112000000005]> : tensor<2xf64> + %0 = arith.constant dense<[1.234567, 0.89101112]> : tensor<2xf64> + func.return %0 : tensor<2xf64> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..54d444767bcd46d7f8c2a7f0162b8b3b1538801b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/BUILD @@ -0,0 +1,45 @@ +load("//tensorflow:tensorflow.default.bzl", "filegroup") +load("//tensorflow/compiler/mlir:glob_lit_test.bzl", "glob_lit_tests") + +# copybara:uncomment package(default_applicable_licenses = ["//tensorflow:LICENSE"]) + +licenses(["notice"]) + +glob_lit_tests( + name = "all_tests", + data = [ + ":quant_stats_files", + ":test_utilities", + ], + driver = "@llvm-project//mlir:run_lit.sh", + size_override = { + "quant_stats.pbtxt": "medium", + }, + tags_override = { + "add.pbtxt": ["no_rocm"], + "fake_quant_per_channel.pbtxt": ["no_rocm"], + }, + test_file_exts = [ + "pbtxt", + ], +) + +# Bundle together all of the test utilities that are used by tests. +filegroup( + name = "test_utilities", + testonly = True, + data = [ + "//tensorflow/compiler/mlir/lite:flatbuffer_to_string", + "//tensorflow/compiler/mlir/lite:tf_tfl_translate", + "@llvm-project//llvm:FileCheck", + "@llvm-project//llvm:not", + ], +) + +# Bundle together all the quant stats files that are used by the tests. +filegroup( + name = "quant_stats_files", + srcs = glob( + ["**/*.stats"], + ), +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/add.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/add.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..13bf6dc1fe7f8793f9b7a6f05d5db8ddc0309660 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/add.pbtxt @@ -0,0 +1,104 @@ +# RUN: tf_tfl_translate -tf-input-arrays=input0,input1 -tf-input-shapes=4:4 -tf-input-data-types=DT_INT32,DT_INT32 -tf-output-arrays=Add %s -o - | flatbuffer_to_string - | FileCheck %s + +# Add two tensor<4xi32> inputs and return the result + +node { + name: "Add" + op: "Add" + input: "input0" + input: "input1" + attr { + key: "T" + value { + type: DT_INT32 + } + } +} +node { + name: "input0" + op: "Placeholder" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } +} +node { + name: "input1" + op: "Placeholder" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } +} +versions { + producer: 27 +} + +# CHECK: { +# CHECK-NEXT: version: 3, +# CHECK-NEXT: operator_codes: [ { +# CHECK-NEXT: version: 1 +# CHECK-NEXT: } ], +# CHECK-NEXT: subgraphs: [ { +# CHECK-NEXT: tensors: [ { +# CHECK-NEXT: shape: [ 4 ], +# CHECK-NEXT: type: INT32, +# CHECK-NEXT: buffer: 1, +# CHECK-NEXT: name: "input0", +# CHECK-NEXT: quantization: { +# CHECK-EMPTY: +# CHECK-NEXT: }, +# CHECK-NEXT: has_rank: true +# CHECK-NEXT: }, { +# CHECK-NEXT: shape: [ 4 ], +# CHECK-NEXT: type: INT32, +# CHECK-NEXT: buffer: 2, +# CHECK-NEXT: name: "input1", +# CHECK-NEXT: quantization: { +# CHECK-EMPTY: +# CHECK-NEXT: }, +# CHECK-NEXT: has_rank: true +# CHECK-NEXT: }, { +# CHECK-NEXT: shape: [ 4 ], +# CHECK-NEXT: type: INT32, +# CHECK-NEXT: buffer: 3, +# CHECK-NEXT: name: "Add", +# CHECK-NEXT: quantization: { +# CHECK-EMPTY: +# CHECK-NEXT: }, +# CHECK-NEXT: has_rank: true +# CHECK-NEXT: } ], +# CHECK-NEXT: inputs: [ 0, 1 ], +# CHECK-NEXT: outputs: [ 2 ], +# CHECK-NEXT: operators: [ { +# CHECK-NEXT: inputs: [ 0, 1 ], +# CHECK-NEXT: outputs: [ 2 ], +# CHECK-NEXT: builtin_options_type: AddOptions, +# CHECK-NEXT: builtin_options: { +# CHECK-EMPTY: +# CHECK-NEXT: } +# CHECK-NEXT: } ] +# CHECK-NEXT: name: "main" +# CHECK-NEXT: } ], +# CHECK-NEXT: description: "MLIR Converted.", +# CHECK-NEXT: buffers: [ { +# CHECK-EMPTY: +# CHECK-NEXT: }, { +# CHECK-EMPTY: +# CHECK-NEXT: }, { +# CHECK-EMPTY: +# CHECK-NEXT: }, { +# CHECK-EMPTY: +# CHECK-NEXT: }, { +# CHECK-NEXT: data: [ 49, 46, 53, 46, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] +# CHECK-NEXT: } ], +# CHECK-NEXT: metadata: [ { +# CHECK-NEXT: name: "min_runtime_version", +# CHECK-NEXT: buffer: 4 +# CHECK-NEXT: } ], +# CHECK-NEXT: signature_defs: [ ] +# CHECK-NEXT: } diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/control_flow_v1.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/control_flow_v1.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..16186e48cf9acc8fad05fbcafb13378ddef4e942 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/control_flow_v1.pbtxt @@ -0,0 +1,257 @@ +# RUN: not tf_tfl_translate -tf-upgrade-legacy=false -tf-input-arrays=Placeholder,Placeholder_1 -tf-input-shapes=1,2:1 -tf-output-arrays=cond/Merge -tf-enable-shape-inference-on-import=false -mlir-print-debuginfo -output-mlir %s -o - 2>&1 | FileCheck %s + +# CHECK: error: The graph has Control Flow V1 ops. TFLite converter doesn't support Control Flow V1 ops. Consider using Control Flow V2 ops instead. + +node { + name: "Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 2 + } + dim { + size: 2 + } + } + tensor_content: "\315\314\314=\315\314L>\232\231\231>\315\314\314>" + } + } + } +} +node { + name: "Placeholder" + op: "Placeholder" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: -1 + } + dim { + size: 2 + } + } + } + } +} +node { + name: "Placeholder_1" + op: "Placeholder" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "shape" + value { + shape { + } + } + } +} +node { + name: "cond/Switch" + op: "Switch" + input: "Placeholder_1" + input: "Placeholder_1" + attr { + key: "T" + value { + type: DT_BOOL + } + } +} +node { + name: "cond/switch_t" + op: "Identity" + input: "cond/Switch:1" + attr { + key: "T" + value { + type: DT_BOOL + } + } +} +node { + name: "cond/switch_f" + op: "Identity" + input: "cond/Switch" + attr { + key: "T" + value { + type: DT_BOOL + } + } +} +node { + name: "cond/pred_id" + op: "Identity" + input: "Placeholder_1" + attr { + key: "T" + value { + type: DT_BOOL + } + } +} +node { + name: "cond/MatMul" + op: "MatMul" + input: "cond/MatMul/Switch:1" + input: "cond/MatMul/Switch_1:1" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "transpose_a" + value { + b: false + } + } + attr { + key: "transpose_b" + value { + b: false + } + } +} +node { + name: "cond/MatMul/Switch" + op: "Switch" + input: "Placeholder" + input: "cond/pred_id" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@Placeholder" + } + } + } +} +node { + name: "cond/MatMul/Switch_1" + op: "Switch" + input: "Const" + input: "cond/pred_id" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@Const" + } + } + } +} +node { + name: "cond/Add" + op: "Add" + input: "cond/Add/Switch" + input: "cond/Add/Switch_1" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "cond/Add/Switch" + op: "Switch" + input: "Placeholder" + input: "cond/pred_id" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@Placeholder" + } + } + } +} +node { + name: "cond/Add/Switch_1" + op: "Switch" + input: "Const" + input: "cond/pred_id" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@Const" + } + } + } +} +node { + name: "cond/Merge" + op: "Merge" + input: "cond/Add" + input: "cond/MatMul" + attr { + key: "N" + value { + i: 2 + } + } + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "init" + op: "NoOp" +} +versions { + producer: 134 +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/custom_opdef.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/custom_opdef.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..47fd96703b3e4bed190647305ba80e92dfd77fdd --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/custom_opdef.pbtxt @@ -0,0 +1,47 @@ +# RUN: tf_tfl_translate -tf-input-arrays=input0,input1 -tf-input-shapes=4:4 -tf-input-data-types=DT_INT32,DT_INT32 -tf-output-arrays=output %s -o - --output-mlir -tf-custom-opdefs="name: 'BannaPotatoSaladWithColeslaw' input_arg: { name: 'a' type: DT_INT32 } input_arg: { name: 'b' type: DT_INT32 } output_arg: { name: 'c' type: DT_INT32 }" | FileCheck %s + +node { + name: "output" + op: "BannaPotatoSaladWithColeslaw" + input: "input0" + input: "input1" + attr { + key: "T" + value { + type: DT_INT32 + } + } +} +node { + name: "input0" + op: "Placeholder" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } +} +node { + name: "input1" + op: "Placeholder" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } +} +versions { + producer: 27 +} + +# CHECK-LABEL: func @main(%arg0: tensor<4xi32>, %arg1: tensor<4xi32>) -> tensor<*xi32> +# CHECK: attributes {tf.entry_function = {control_outputs = "", inputs = "input0,input1", outputs = "output"}} { +# CHECK-NEXT: %[[CUSTOM:.*]] = "tfl.custom_tf"(%arg0, %arg1) ({ +# CHECK-NEXT: ^bb0(%arg2: tensor<4xi32>, %arg3: tensor<4xi32>): +# CHECK-NEXT: %[[OUTPUTS:.*]] = "tf.BannaPotatoSaladWithColeslaw"(%arg2, %arg3) {T = i32, device = ""} : (tensor<4xi32>, tensor<4xi32>) -> tensor<*xi32> +# CHECK-NEXT: "tfl.yield"(%[[OUTPUTS]]) : (tensor<*xi32>) -> () +# CHECK-NEXT: }) {T = i32, device = ""} : (tensor<4xi32>, tensor<4xi32>) -> tensor<*xi32> +# CHECK-NEXT: return %[[CUSTOM]] : tensor<*xi32> +# CHECK-NEXT: } diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/disallow_stateful_partitioned_call.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/disallow_stateful_partitioned_call.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..31569d05e4b76a21207be870e7cf12f87c17f273 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/disallow_stateful_partitioned_call.pbtxt @@ -0,0 +1,195 @@ +# RUN: not tf_tfl_translate -tf-input-arrays=input0 -tf-input-shapes=-1 -tf-input-data-types=DT_FLOAT -tf-output-arrays=add %s 2>&1 | FileCheck %s +# CHECK: error: The Graph contains unsupported `StatefulPartionedCallOp`(s) + +node { + name: "input0" + op: "Placeholder" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "args_0" + op: "_Arg" + attr { + key: "T" + value { + type: DT_RESOURCE + } + } + attr { + key: "index" + value { + i: 0 + } + } +} +node { + name: "spc1" + op: "StatefulPartitionedCall" + input: "input0" + input: "args_0" + attr { + key: "Tin" + value { + list { + type: DT_FLOAT + type: DT_RESOURCE + } + } + } + attr { + key: "Tout" + value { + list { + type: DT_FLOAT + type: DT_RESOURCE + } + } + } + attr { + key: "config" + value { + s: "" + } + } + attr { + key: "config_proto" + value { + s: "" + } + } + attr { + key: "executor_type" + value { + s: "" + } + } + attr { + key: "f" + value { + func { + name: "function" + } + } + } +} +node { + name: "spc2" + op: "StatefulPartitionedCall" + input: "input0" + input: "args_0" + attr { + key: "Tin" + value { + list { + type: DT_FLOAT + type: DT_RESOURCE + } + } + } + attr { + key: "Tout" + value { + list { + type: DT_FLOAT + type: DT_RESOURCE + } + } + } + attr { + key: "config" + value { + s: "" + } + } + attr { + key: "config_proto" + value { + s: "" + } + } + attr { + key: "executor_type" + value { + s: "" + } + } + attr { + key: "f" + value { + func { + name: "function" + } + } + } +} +node { + name: "add" + op: "Add" + input: "spc1" + input: "spc2" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +library { + function { + signature { + name: "function" + input_arg { + name: "inputs" + type: DT_FLOAT + } + input_arg { + name: "statefulpartitionedcall_args_1" + type: DT_RESOURCE + } + output_arg { + name: "identity" + type: DT_FLOAT + } + is_stateful: true + } + node_def { + name: "Identity" + op: "Identity" + input: "inputs" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + } + ret { + key: "identity" + value: "Identity:output:0" + } + arg_attr { + key: 0 + value { + attr { + key: "_user_specified_name" + value { + s: "inputs" + } + } + } + } + arg_attr { + key: 1 + value { + } + } + } +} +versions { + producer: 121 +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/fake_quant_per_channel.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/fake_quant_per_channel.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..4b65732e975381acab912b47dec0c590fb0e78af --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/fake_quant_per_channel.pbtxt @@ -0,0 +1,559 @@ +# RUN: tf_tfl_translate -tf-input-arrays=input -tf-input-shapes=1,1,1,256 -tf-input-data-types=DT_FLOAT -tf-inference-type=DT_QINT8 -tf-input-min-values='-33.614346' -tf-input-max-values='21.54917' -tf-output-arrays=output %s -o - --output-mlir 2>&1 | FileCheck --check-prefix=MLIR %s +# TODO(fengliuai): Figure out what to do here. A "golden" end-to-end test is overly fragile. +# DISABLED_RUN: true || tf_tfl_translate -tf-input-arrays=input -tf-input-shapes=1,1,1,256 -tf-input-data-types=DT_FLOAT -tf-inference-type=DT_QINT8 -tf-input-min-values='-33.614346' -tf-input-max-values='21.54917' -tf-output-arrays=output %s -o - | flatbuffer_to_string - | FileCheck %s + +node { + name: "input" + op: "Placeholder" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/weights" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 186 + } + } + float_val: 1.0 + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/weights/read" + op: "Identity" + input: "BoxPredictor_4/ClassPredictor/weights" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@BoxPredictor_4/ClassPredictor/weights" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/weights_quant/min" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 186 + } + } + tensor_content: "\217\245\177\301\005@d\276C\030x\276S\320\177\276.\227q\277\260]\223\276j\016\250\276\305\017\202\276\200+\201\276\275\377\177\276\310&\202\276\314\210k\276\3178W\277\335\356_\276\332GG\276`\200>\276o_c\276\312\224k\2767\033S\276M=\376\300y-\271\276\225\021g\276\272lZ\276\353r\251\276\025\354G\276\357\220\\\2765j0\2765Q(\276\013>0\276Y\r\'\276\224\013\"\276G=\377\300\022\211E\276\344\035O\276\242\220l\276w\307\020\2773`\376\276\331\377\202\276\374\016Y\276\224\273^\2767`~\276V\016>\276\226\333g\276*\241K\276\224g\177\276\320\315(\276t\376\177\276B\025B\276@\214\226\276\377s\376\276\373\365\377\277\343\211~\276\205\320b\276\213\236\377\2763\0136\276u\237:\276\325\373~\276\344\r\037\276>\035(\276\247\364!\276\2613%\276\207]!\276\320S\376\301\354 _\276\337PT\276\342\226[\276\304\307\177\276\360\"x\276\312\023\200\276Wfp\2768$W\276\235\014\202\276\230\210m\2761\342L\276b\234\013\277\362XM\276QPL\276\006Va\276\3168>\276\262\367t\276x\252k\276\266\225\231\276z@B\276\244\340?\276>8]\276\214v\206\276\316\210Z\276\275\343U\276\366\0321\276b\2128\276\310\2611\276\340n/\276\312\272\'\276\207\274\207\276tl\034\276\214[\357\275\020\345@\276\1778\376\275\264~#\276\326U*\276\374g\003\276\377\013\000\276\361\375T\276\246\330\007\276\365\203~\276\311\367\033\276\261\240\177\2768`\"\276i\364 \276\321\365\363\275\0241~\276\005\365\376\275S\303\035\277\203\003\020\276\264\322\377\276$\241\177\276\264d\t\276H\000C\276\360/~\276\352\301\377\275\037\300\n\276\0000\033\276r|\377\275\025\004\002\276\276\021\262\276\343\245M\2763\302\351\276LE\005\276\n\"~\276\341q\341\276\265\022~\276\322^\036\276\206\216\031\276\306\n\310\276\322_T\276\367\037w\276\374\227A\276\010\272G\276T&\022\276y\217>\276\275H\033\276\002=\376\276\232x0\276\322C~\276\032\350\212\276\234\322\036\2762%\376\276\266\345#\276.1\021\276\302,\351\276\025x&\276\354\363.\276\362\210\025\276\035\032\036\276\267X\014\276zc\177\300\336L_\276\274,k\276{\266\203\276\366\266~\277\225F\211\276\302~\243\276\253.\214\2765\241l\276g\021\214\276\347\376\177\276H\026T\276\305\220\377\276\000\\Y\276\334\332>\276\224\2459\276\334ja\276\260\313p\276\024\026\\\2769\334\177\301\334\330\222\276\347\376e\276\014\230W\276\253P\252\276i\300C\276\340\261Y\276721\276#k)\276\035.#\276R(0\276\224\336!\276" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/weights_quant/min/read" + op: "Identity" + input: "BoxPredictor_4/ClassPredictor/weights_quant/min" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@BoxPredictor_4/ClassPredictor/weights_quant/min" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/weights_quant/max" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 186 + } + } + tensor_content: "\217\245\177A\005@d>C\030x>S\320\177>.\227q?\260]\223>j\016\250>\305\017\202>\200+\201>\275\377\177>\310&\202>\314\210k>\3178W?\335\356_>\332GG>`\200>>o_c>\312\224k>7\033S>M=\376@y-\271>\225\021g>\272lZ>\353r\251>\025\354G>\357\220\\>5j0>5Q(>\013>0>Y\r\'>\224\013\">G=\377@\022\211E>\344\035O>\242\220l>w\307\020?3`\376>\331\377\202>\374\016Y>\224\273^>7`~>V\016>>\226\333g>*\241K>\224g\177>\320\315(>t\376\177>B\025B>@\214\226>\377s\376>\373\365\377?\343\211~>\205\320b>\213\236\377>3\0136>u\237:>\325\373~>\344\r\037>>\035(>\247\364!>\2613%>\207]!>\320S\376A\354 _>\337PT>\342\226[>\304\307\177>\360\"x>\312\023\200>Wfp>8$W>\235\014\202>\230\210m>1\342L>b\234\013?\362XM>QPL>\006Va>\3168>>\262\367t>x\252k>\266\225\231>z@B>\244\340?>>8]>\214v\206>\316\210Z>\275\343U>\366\0321>b\2128>\310\2611>\340n/>\312\272\'>\207\274\207>tl\034>\214[\357=\020\345@>\1778\376=\264~#>\326U*>\374g\003>\377\013\000>\361\375T>\246\330\007>\365\203~>\311\367\033>\261\240\177>8`\">i\364 >\321\365\363=\0241~>\005\365\376=S\303\035?\203\003\020>\264\322\377>$\241\177>\264d\t>H\000C>\360/~>\352\301\377=\037\300\n>\0000\033>r|\377=\025\004\002>\276\021\262>\343\245M>3\302\351>LE\005>\n\"~>\341q\341>\265\022~>\322^\036>\206\216\031>\306\n\310>\322_T>\367\037w>\374\227A>\010\272G>T&\022>y\217>>\275H\033>\002=\376>\232x0>\322C~>\032\350\212>\234\322\036>2%\376>\266\345#>.1\021>\302,\351>\025x&>\354\363.>\362\210\025>\035\032\036>\267X\014>zc\177@\336L_>\274,k>{\266\203>\366\266~?\225F\211>\302~\243>\253.\214>5\241l>g\021\214>\347\376\177>H\026T>\305\220\377>\000\\Y>\334\332>>\224\2459>\334ja>\260\313p>\024\026\\>9\334\177A\334\330\222>\347\376e>\014\230W>\253P\252>i\300C>\340\261Y>721>#k)>\035.#>R(0>\224\336!>" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/weights_quant/max/read" + op: "Identity" + input: "BoxPredictor_4/ClassPredictor/weights_quant/max" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@BoxPredictor_4/ClassPredictor/weights_quant/max" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/weights_quant/FakeQuantWithMinMaxVarsPerChannel" + op: "FakeQuantWithMinMaxVarsPerChannel" + input: "BoxPredictor_4/ClassPredictor/weights/read" + input: "BoxPredictor_4/ClassPredictor/weights_quant/min/read" + input: "BoxPredictor_4/ClassPredictor/weights_quant/max/read" + attr { + key: "narrow_range" + value { + b: true + } + } + attr { + key: "num_bits" + value { + i: 8 + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/Conv2D" + op: "Conv2D" + input: "input" + input: "BoxPredictor_4/ClassPredictor/weights_quant/FakeQuantWithMinMaxVarsPerChannel" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NHWC" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/biases" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 186 + } + } + tensor_content: "\324\202\223\276\337\210\375\275\351\212\374\275p@\346\275HD\364\275W\236\362\275i\222\001\276\'\370\365\275:\250\345\275\244\214\373\275}\373\025\276^\351\000\2767_\017\276\362\t\322\2751%\376\275\364\020\324\275,\352\323\275\233\350\365\275\3777\014\276\371\022\277\2745{\366\275\270\207\366\275b\215\010\276\332\252\"\276\273\313\332\275\354\237\361\275\365W9\276o\364B\276\236HD\276$\250B\276CRC\276K\265\246\275f5\016\276[I\017\276\206\346\r\276\306\307\032\276\267\323\375\275\222\377\002\276\037\231\030\276\346W\001\2762F\004\276\021/\034\276\273\215\357\275\014m5\276\307[\021\276K\r\022\276Xv\365\275\312L\372\275A4\026\276\210[\024\276\254\242\003\276\207f\000\276\\C\023\276\322\233\000\276\266\0018\276\233\225\370\275\243\030\004\276]\201C\276\374j:\276\361\257@\276\017z<\276P?A\276a:\375\276\023\220\342\275W\026\377\275qD\005\276\'\271\371\275\342\207\n\276\273\221\003\276\372\205\363\275Ok\334\275\306\233\023\276y\271\"\276\313\345\n\276UH\"\276\233j\366\275\0026\377\275O\263\341\275\005\340\365\275,$\n\276\222\030\010\276\243\020\346\275\301\322\007\276n\252\374\275\325\251\007\276\325\033,\276\352\217\345\275\234\334\t\276\024\241@\276\256\365H\276zbA\276\262\361A\276\275C>\276\353Z\005>\2050&\276\035\222&\276\2457%\276\377/\'\276^\263\037\276\202\341%\276}\312\034\276&\2520\276Y\313,\276\232C*\276\233\375\032\276\212\250%\276\020\362\034\276\212\213-\276\211\211\030\276P\256\"\276\260\307\027\276?\332$\276\213\326\025\2761\243\023\276L\335\034\276\375\034\035\276L\2112\276\016\3571\276\224]\036\276\r\3165\276\232\340&\276?\2315\276^\342,\276\360{2\276\314\031\274=s\363\r\276C\227\035\276\332`\037\276\236\332\031\276Kj0\276$\034\035\276\227\262\033\276\231\260\033\2764\213\035\276\021\363$\276y\372&\276m`/\276<\272\027\276\267l\025\276\034x\031\276\202\313\034\276\026\363\027\276\307(\014\276<\332\005\276\231\316!\276\022\355\027\276-\251\037\2764\203+\276c \032\276\006#\036\276\22064\276\27450\2768O3\276\013\2321\276\31143\276a\367\n\2763\347\362\275?\037\003\276\241\375\001\276\340\003\332\275e\342\357\275\022\226\376\275\214\206\366\275\220\303\311\275\360\342\374\275\246\361\016\276\234\227\351\275\227\306\023\276\234\252\325\275\200\002\001\276\\\243\317\275\302\233\311\275\177\024\002\276\325\027\002\276\'\351\236=\316q\366\275\243>\010\276\203\204\373\275\365F/\276\225\323\346\275\250,\375\275E\0048\276\306\340?\276[/<\276\212\025B\276\251\264?\276" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/biases/read" + op: "Identity" + input: "BoxPredictor_4/ClassPredictor/biases" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@BoxPredictor_4/ClassPredictor/biases" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/BiasAdd" + op: "BiasAdd" + input: "BoxPredictor_4/ClassPredictor/Conv2D" + input: "BoxPredictor_4/ClassPredictor/biases/read" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NHWC" + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/act_quant/min" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: -14.066669 + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/act_quant/min/read" + op: "Identity" + input: "BoxPredictor_4/ClassPredictor/act_quant/min" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@BoxPredictor_4/ClassPredictor/act_quant/min" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/act_quant/max" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 9.810242 + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/act_quant/max/read" + op: "Identity" + input: "BoxPredictor_4/ClassPredictor/act_quant/max" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@BoxPredictor_4/ClassPredictor/act_quant/max" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/act_quant/FakeQuantWithMinMaxVars" + op: "FakeQuantWithMinMaxVars" + input: "BoxPredictor_4/ClassPredictor/BiasAdd" + input: "BoxPredictor_4/ClassPredictor/act_quant/min/read" + input: "BoxPredictor_4/ClassPredictor/act_quant/max/read" + attr { + key: "narrow_range" + value { + b: false + } + } + attr { + key: "num_bits" + value { + i: 8 + } + } +} +node { + name: "BoxPredictor_4/Reshape_1/shape" + op: "Const" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 3 + } + } + tensor_content: "\001\000\000\000\377\377\377\377\037\000\000\000" + } + } + } +} +node { + name: "output" + op: "Reshape" + input: "BoxPredictor_4/ClassPredictor/act_quant/FakeQuantWithMinMaxVars" + input: "BoxPredictor_4/Reshape_1/shape" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tshape" + value { + type: DT_INT32 + } + } +} + +# MLIR-LABEL: func @main +# MLIR-SAME: (%[[ARG_0:[a-z0-9]+]]: tensor<1x1x1x256x!quant.uniform>) -> tensor<1x6x31x!quant.uniform> +# MLIR-SAME: control_outputs = "" +# MLIR-SAME: inputs = "input" +# MLIR-SAME: outputs = "output" +# MLIR: %[[shape:.*]] = arith.constant dense<[1, -1, 31]> : tensor<3xi32> +# MLIR: %[[bias:.*]] = "tfl.pseudo_qconst"() <{qtype = tensor<186x!quant.uniform:f32:0, {0.12581039038230116, +# MLIR: %[[conv:.*]] = "tfl.conv_2d"(%[[ARG_0]], %[[weight]], %[[bias]]) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32} +# MLIR: %[[reshape:.*]] = "tfl.reshape"(%[[conv]], %[[shape]]) : (tensor<1x1x1x186x!quant.uniform>, tensor<3xi32>) +# MLIR: return %[[reshape]] : tensor<1x6x31x!quant.uniform> +# MLIR: } + +# CHECK-LABEL: { +# CHECK: version: 3, +# CHECK: operator_codes: [ { +# CHECK: deprecated_builtin_code: 3, +# CHECK: version: 3, +# CHECK: builtin_code: CONV_2D +# CHECK: }, { +# CHECK: deprecated_builtin_code: 22, +# CHECK: version: 1 +# CHECK: builtin_code: RESHAPE +# CHECK: } ], +# CHECK: subgraphs: [ { +# CHECK: tensors: [ { +# CHECK: shape: [ 1, 1, 1, 256 ], +# CHECK: type: INT8, +# CHECK: buffer: 1, +# CHECK: name: "input", +# CHECK: quantization: { +# CHECK: scale: [ 0.216328 ], +# CHECK: zero_point: [ 27 ] +# CHECK: } +# CHECK: }, { +# CHECK: shape: [ 3 ], +# CHECK: type: INT32, +# CHECK: buffer: 2, +# CHECK: name: "BoxPredictor_4/Reshape_1/shape", +# CHECK: quantization: { +# CHECK-EMPTY +# CHECK: } +# CHECK: }, { +# CHECK: shape: [ 186 ], +# CHECK: type: INT32, +# CHECK: buffer: 3, +# CHECK: name: "BoxPredictor_4/ClassPredictor/BiasAdd;BoxPredictor_4/ClassPredictor/Conv2D;BoxPredictor_4/ClassPredictor/biases", +# CHECK: quantization: { +# CHECK: scale: [ 0.027216, 0.00038, 0.000413, 0.000426, 0.001607, +# CHECK: zero_point: [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +# CHECK: } +# CHECK: }, { +# CHECK: shape: [ 186, 1, 1, 256 ], +# CHECK: type: INT8, +# CHECK: buffer: 4, +# CHECK: name: "BoxPredictor_4/ClassPredictor/Conv2D;BoxPredictor_4/ClassPredictor/weights_quant/FakeQuantWithMinMaxVarsPerChannel", +# CHECK: quantization: { +# CHECK: scale: [ 0.12581, 0.001755, 0.001908, 0.001967, 0.007431, +# CHECK: zero_point: [ 0, 0, 0, 0, 0, 0, 0, 0, 0, +# CHECK: } +# CHECK: }, { +# CHECK: shape: [ 1, 1, 1, 186 ], +# CHECK: type: INT8, +# CHECK: buffer: 5, +# CHECK: name: "BoxPredictor_4/ClassPredictor/BiasAdd;BoxPredictor_4/ClassPredictor/Conv2D;BoxPredictor_4/ClassPredictor/biases1", +# CHECK: quantization: { +# CHECK: scale: [ 0.093635 ], +# CHECK: zero_point: [ 22 ] +# CHECK: } +# CHECK: }, { +# CHECK: shape: [ 1, 6, 31 ], +# CHECK: type: INT8, +# CHECK: buffer: 6, +# CHECK: name: "output", +# CHECK: quantization: { +# CHECK: scale: [ 0.093635 ], +# CHECK: zero_point: [ 22 ] +# CHECK: } +# CHECK: } ], +# CHECK: inputs: [ 0 ], +# CHECK: outputs: [ 5 ], +# CHECK: operators: [ { +# CHECK: inputs: [ 0, 3, 2 ], +# CHECK: outputs: [ 4 ], +# CHECK: builtin_options_type: Conv2DOptions, +# CHECK: builtin_options: { +# CHECK: stride_w: 1, +# CHECK: stride_h: 1 +# CHECK: } +# CHECK: }, { +# CHECK: opcode_index: 1, +# CHECK: inputs: [ 4, 1 ], +# CHECK: outputs: [ 5 ] +# CHECK: } ], +# CHECK: name: "main" +# CHECK: } ], +# CHECK: description: "MLIR Converted.", +# CHECK: buffers: [ { +# CHECK-EMPTY +# CHECK: }, { +# CHECK-EMPTY +# CHECK: }, { +# CHECK: data: [ 1, 0, 0, 0, 255, 255, 255, 255, 31, 0, 0, 0 ] +# CHECK: }, { +# CHECK: data: [ 245, 255, 255, 255, 186, 254, 255, 255, 213, 254, 255, 255, +# CHECK: }, { +# CHECK: data: [ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, +# CHECK: }, { +# CHECK-EMPTY +# CHECK: }, { +# CHECK-EMPTY +# CHECK: } ] +# CHECK: } diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/fake_quant_per_channel_4bit.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/fake_quant_per_channel_4bit.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..e2a0f8da9a47b10cdec6e9ae579d6ce8fb2c831d --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/fake_quant_per_channel_4bit.pbtxt @@ -0,0 +1,559 @@ +# RUN: tf_tfl_translate -tf-input-arrays=input -tf-input-shapes=1,1,1,256 -tf-input-data-types=DT_FLOAT -tf-inference-type=DT_QINT8 -tf-input-min-values='-33.614346' -tf-input-max-values='21.54917' -tf-output-arrays=output %s -o - --output-mlir 2>&1 | FileCheck --check-prefix=MLIR %s +# TODO(fengliuai): Figure out what to do here. A "golden" end-to-end test is overly fragile. +# DISABLED_RUN: true || tf_tfl_translate -tf-input-arrays=input -tf-input-shapes=1,1,1,256 -tf-input-data-types=DT_FLOAT -tf-inference-type=DT_QINT8 -tf-input-min-values='-33.614346' -tf-input-max-values='21.54917' -tf-output-arrays=output %s -o - | flatbuffer_to_string - | FileCheck %s + +node { + name: "input" + op: "Placeholder" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/weights" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 186 + } + } + float_val: 1.0 + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/weights/read" + op: "Identity" + input: "BoxPredictor_4/ClassPredictor/weights" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@BoxPredictor_4/ClassPredictor/weights" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/weights_quant/min" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 186 + } + } + tensor_content: "\217\245\177\301\005@d\276C\030x\276S\320\177\276.\227q\277\260]\223\276j\016\250\276\305\017\202\276\200+\201\276\275\377\177\276\310&\202\276\314\210k\276\3178W\277\335\356_\276\332GG\276`\200>\276o_c\276\312\224k\2767\033S\276M=\376\300y-\271\276\225\021g\276\272lZ\276\353r\251\276\025\354G\276\357\220\\\2765j0\2765Q(\276\013>0\276Y\r\'\276\224\013\"\276G=\377\300\022\211E\276\344\035O\276\242\220l\276w\307\020\2773`\376\276\331\377\202\276\374\016Y\276\224\273^\2767`~\276V\016>\276\226\333g\276*\241K\276\224g\177\276\320\315(\276t\376\177\276B\025B\276@\214\226\276\377s\376\276\373\365\377\277\343\211~\276\205\320b\276\213\236\377\2763\0136\276u\237:\276\325\373~\276\344\r\037\276>\035(\276\247\364!\276\2613%\276\207]!\276\320S\376\301\354 _\276\337PT\276\342\226[\276\304\307\177\276\360\"x\276\312\023\200\276Wfp\2768$W\276\235\014\202\276\230\210m\2761\342L\276b\234\013\277\362XM\276QPL\276\006Va\276\3168>\276\262\367t\276x\252k\276\266\225\231\276z@B\276\244\340?\276>8]\276\214v\206\276\316\210Z\276\275\343U\276\366\0321\276b\2128\276\310\2611\276\340n/\276\312\272\'\276\207\274\207\276tl\034\276\214[\357\275\020\345@\276\1778\376\275\264~#\276\326U*\276\374g\003\276\377\013\000\276\361\375T\276\246\330\007\276\365\203~\276\311\367\033\276\261\240\177\2768`\"\276i\364 \276\321\365\363\275\0241~\276\005\365\376\275S\303\035\277\203\003\020\276\264\322\377\276$\241\177\276\264d\t\276H\000C\276\360/~\276\352\301\377\275\037\300\n\276\0000\033\276r|\377\275\025\004\002\276\276\021\262\276\343\245M\2763\302\351\276LE\005\276\n\"~\276\341q\341\276\265\022~\276\322^\036\276\206\216\031\276\306\n\310\276\322_T\276\367\037w\276\374\227A\276\010\272G\276T&\022\276y\217>\276\275H\033\276\002=\376\276\232x0\276\322C~\276\032\350\212\276\234\322\036\2762%\376\276\266\345#\276.1\021\276\302,\351\276\025x&\276\354\363.\276\362\210\025\276\035\032\036\276\267X\014\276zc\177\300\336L_\276\274,k\276{\266\203\276\366\266~\277\225F\211\276\302~\243\276\253.\214\2765\241l\276g\021\214\276\347\376\177\276H\026T\276\305\220\377\276\000\\Y\276\334\332>\276\224\2459\276\334ja\276\260\313p\276\024\026\\\2769\334\177\301\334\330\222\276\347\376e\276\014\230W\276\253P\252\276i\300C\276\340\261Y\276721\276#k)\276\035.#\276R(0\276\224\336!\276" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/weights_quant/min/read" + op: "Identity" + input: "BoxPredictor_4/ClassPredictor/weights_quant/min" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@BoxPredictor_4/ClassPredictor/weights_quant/min" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/weights_quant/max" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 186 + } + } + tensor_content: "\217\245\177A\005@d>C\030x>S\320\177>.\227q?\260]\223>j\016\250>\305\017\202>\200+\201>\275\377\177>\310&\202>\314\210k>\3178W?\335\356_>\332GG>`\200>>o_c>\312\224k>7\033S>M=\376@y-\271>\225\021g>\272lZ>\353r\251>\025\354G>\357\220\\>5j0>5Q(>\013>0>Y\r\'>\224\013\">G=\377@\022\211E>\344\035O>\242\220l>w\307\020?3`\376>\331\377\202>\374\016Y>\224\273^>7`~>V\016>>\226\333g>*\241K>\224g\177>\320\315(>t\376\177>B\025B>@\214\226>\377s\376>\373\365\377?\343\211~>\205\320b>\213\236\377>3\0136>u\237:>\325\373~>\344\r\037>>\035(>\247\364!>\2613%>\207]!>\320S\376A\354 _>\337PT>\342\226[>\304\307\177>\360\"x>\312\023\200>Wfp>8$W>\235\014\202>\230\210m>1\342L>b\234\013?\362XM>QPL>\006Va>\3168>>\262\367t>x\252k>\266\225\231>z@B>\244\340?>>8]>\214v\206>\316\210Z>\275\343U>\366\0321>b\2128>\310\2611>\340n/>\312\272\'>\207\274\207>tl\034>\214[\357=\020\345@>\1778\376=\264~#>\326U*>\374g\003>\377\013\000>\361\375T>\246\330\007>\365\203~>\311\367\033>\261\240\177>8`\">i\364 >\321\365\363=\0241~>\005\365\376=S\303\035?\203\003\020>\264\322\377>$\241\177>\264d\t>H\000C>\360/~>\352\301\377=\037\300\n>\0000\033>r|\377=\025\004\002>\276\021\262>\343\245M>3\302\351>LE\005>\n\"~>\341q\341>\265\022~>\322^\036>\206\216\031>\306\n\310>\322_T>\367\037w>\374\227A>\010\272G>T&\022>y\217>>\275H\033>\002=\376>\232x0>\322C~>\032\350\212>\234\322\036>2%\376>\266\345#>.1\021>\302,\351>\025x&>\354\363.>\362\210\025>\035\032\036>\267X\014>zc\177@\336L_>\274,k>{\266\203>\366\266~?\225F\211>\302~\243>\253.\214>5\241l>g\021\214>\347\376\177>H\026T>\305\220\377>\000\\Y>\334\332>>\224\2459>\334ja>\260\313p>\024\026\\>9\334\177A\334\330\222>\347\376e>\014\230W>\253P\252>i\300C>\340\261Y>721>#k)>\035.#>R(0>\224\336!>" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/weights_quant/max/read" + op: "Identity" + input: "BoxPredictor_4/ClassPredictor/weights_quant/max" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@BoxPredictor_4/ClassPredictor/weights_quant/max" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/weights_quant/FakeQuantWithMinMaxVarsPerChannel" + op: "FakeQuantWithMinMaxVarsPerChannel" + input: "BoxPredictor_4/ClassPredictor/weights/read" + input: "BoxPredictor_4/ClassPredictor/weights_quant/min/read" + input: "BoxPredictor_4/ClassPredictor/weights_quant/max/read" + attr { + key: "narrow_range" + value { + b: true + } + } + attr { + key: "num_bits" + value { + i: 4 + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/Conv2D" + op: "Conv2D" + input: "input" + input: "BoxPredictor_4/ClassPredictor/weights_quant/FakeQuantWithMinMaxVarsPerChannel" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NHWC" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/biases" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 186 + } + } + tensor_content: "\324\202\223\276\337\210\375\275\351\212\374\275p@\346\275HD\364\275W\236\362\275i\222\001\276\'\370\365\275:\250\345\275\244\214\373\275}\373\025\276^\351\000\2767_\017\276\362\t\322\2751%\376\275\364\020\324\275,\352\323\275\233\350\365\275\3777\014\276\371\022\277\2745{\366\275\270\207\366\275b\215\010\276\332\252\"\276\273\313\332\275\354\237\361\275\365W9\276o\364B\276\236HD\276$\250B\276CRC\276K\265\246\275f5\016\276[I\017\276\206\346\r\276\306\307\032\276\267\323\375\275\222\377\002\276\037\231\030\276\346W\001\2762F\004\276\021/\034\276\273\215\357\275\014m5\276\307[\021\276K\r\022\276Xv\365\275\312L\372\275A4\026\276\210[\024\276\254\242\003\276\207f\000\276\\C\023\276\322\233\000\276\266\0018\276\233\225\370\275\243\030\004\276]\201C\276\374j:\276\361\257@\276\017z<\276P?A\276a:\375\276\023\220\342\275W\026\377\275qD\005\276\'\271\371\275\342\207\n\276\273\221\003\276\372\205\363\275Ok\334\275\306\233\023\276y\271\"\276\313\345\n\276UH\"\276\233j\366\275\0026\377\275O\263\341\275\005\340\365\275,$\n\276\222\030\010\276\243\020\346\275\301\322\007\276n\252\374\275\325\251\007\276\325\033,\276\352\217\345\275\234\334\t\276\024\241@\276\256\365H\276zbA\276\262\361A\276\275C>\276\353Z\005>\2050&\276\035\222&\276\2457%\276\377/\'\276^\263\037\276\202\341%\276}\312\034\276&\2520\276Y\313,\276\232C*\276\233\375\032\276\212\250%\276\020\362\034\276\212\213-\276\211\211\030\276P\256\"\276\260\307\027\276?\332$\276\213\326\025\2761\243\023\276L\335\034\276\375\034\035\276L\2112\276\016\3571\276\224]\036\276\r\3165\276\232\340&\276?\2315\276^\342,\276\360{2\276\314\031\274=s\363\r\276C\227\035\276\332`\037\276\236\332\031\276Kj0\276$\034\035\276\227\262\033\276\231\260\033\2764\213\035\276\021\363$\276y\372&\276m`/\276<\272\027\276\267l\025\276\034x\031\276\202\313\034\276\026\363\027\276\307(\014\276<\332\005\276\231\316!\276\022\355\027\276-\251\037\2764\203+\276c \032\276\006#\036\276\22064\276\27450\2768O3\276\013\2321\276\31143\276a\367\n\2763\347\362\275?\037\003\276\241\375\001\276\340\003\332\275e\342\357\275\022\226\376\275\214\206\366\275\220\303\311\275\360\342\374\275\246\361\016\276\234\227\351\275\227\306\023\276\234\252\325\275\200\002\001\276\\\243\317\275\302\233\311\275\177\024\002\276\325\027\002\276\'\351\236=\316q\366\275\243>\010\276\203\204\373\275\365F/\276\225\323\346\275\250,\375\275E\0048\276\306\340?\276[/<\276\212\025B\276\251\264?\276" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/biases/read" + op: "Identity" + input: "BoxPredictor_4/ClassPredictor/biases" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@BoxPredictor_4/ClassPredictor/biases" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/BiasAdd" + op: "BiasAdd" + input: "BoxPredictor_4/ClassPredictor/Conv2D" + input: "BoxPredictor_4/ClassPredictor/biases/read" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NHWC" + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/act_quant/min" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: -14.066669 + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/act_quant/min/read" + op: "Identity" + input: "BoxPredictor_4/ClassPredictor/act_quant/min" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@BoxPredictor_4/ClassPredictor/act_quant/min" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/act_quant/max" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 9.810242 + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/act_quant/max/read" + op: "Identity" + input: "BoxPredictor_4/ClassPredictor/act_quant/max" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@BoxPredictor_4/ClassPredictor/act_quant/max" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/act_quant/FakeQuantWithMinMaxVars" + op: "FakeQuantWithMinMaxVars" + input: "BoxPredictor_4/ClassPredictor/BiasAdd" + input: "BoxPredictor_4/ClassPredictor/act_quant/min/read" + input: "BoxPredictor_4/ClassPredictor/act_quant/max/read" + attr { + key: "narrow_range" + value { + b: false + } + } + attr { + key: "num_bits" + value { + i: 8 + } + } +} +node { + name: "BoxPredictor_4/Reshape_1/shape" + op: "Const" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 3 + } + } + tensor_content: "\001\000\000\000\377\377\377\377\037\000\000\000" + } + } + } +} +node { + name: "output" + op: "Reshape" + input: "BoxPredictor_4/ClassPredictor/act_quant/FakeQuantWithMinMaxVars" + input: "BoxPredictor_4/Reshape_1/shape" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tshape" + value { + type: DT_INT32 + } + } +} + +# MLIR-LABEL: func @main +# MLIR-SAME: (%[[ARG_0:[a-z0-9]+]]: tensor<1x1x1x256x!quant.uniform>) -> tensor<1x6x31x!quant.uniform> +# MLIR-SAME: control_outputs = "" +# MLIR-SAME: inputs = "input" +# MLIR-SAME: outputs = "output" +# MLIR: %[[shape:.*]] = arith.constant dense<[1, -1, 31]> : tensor<3xi32> +# MLIR: %[[bias:.*]] = "tfl.pseudo_qconst"() <{qtype = tensor<186x!quant.uniform:f32:0, {2.2825599397931779, +# MLIR: %[[conv:.*]] = "tfl.conv_2d"(%[[ARG_0]], %[[weight]], %[[bias]]) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32} +# MLIR: %[[reshape:.*]] = "tfl.reshape"(%[[conv]], %[[shape]]) : (tensor<1x1x1x186x!quant.uniform>, tensor<3xi32>) +# MLIR: return %[[reshape]] : tensor<1x6x31x!quant.uniform> +# MLIR: } + +# CHECK-LABEL: { +# CHECK: version: 3, +# CHECK: operator_codes: [ { +# CHECK: deprecated_builtin_code: 3, +# CHECK: version: 3, +# CHECK: builtin_code: CONV_2D +# CHECK: }, { +# CHECK: deprecated_builtin_code: 22, +# CHECK: version: 1 +# CHECK: builtin_code: RESHAPE +# CHECK: } ], +# CHECK: subgraphs: [ { +# CHECK: tensors: [ { +# CHECK: shape: [ 1, 1, 1, 256 ], +# CHECK: type: INT8, +# CHECK: buffer: 1, +# CHECK: name: "input", +# CHECK: quantization: { +# CHECK: scale: [ 0.216328 ], +# CHECK: zero_point: [ 27 ] +# CHECK: } +# CHECK: }, { +# CHECK: shape: [ 3 ], +# CHECK: type: INT32, +# CHECK: buffer: 2, +# CHECK: name: "BoxPredictor_4/Reshape_1/shape", +# CHECK: quantization: { +# CHECK-EMPTY +# CHECK: } +# CHECK: }, { +# CHECK: shape: [ 186 ], +# CHECK: type: INT32, +# CHECK: buffer: 3, +# CHECK: name: "BoxPredictor_4/ClassPredictor/BiasAdd;BoxPredictor_4/ClassPredictor/Conv2D;BoxPredictor_4/ClassPredictor/biases", +# CHECK: quantization: { +# CHECK: scale: [ 0.027216, 0.00038, 0.000413, 0.000426, 0.001607, +# CHECK: zero_point: [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +# CHECK: } +# CHECK: }, { +# CHECK: shape: [ 186, 1, 1, 256 ], +# CHECK: type: INT8, +# CHECK: buffer: 4, +# CHECK: name: "BoxPredictor_4/ClassPredictor/Conv2D;BoxPredictor_4/ClassPredictor/weights_quant/FakeQuantWithMinMaxVarsPerChannel", +# CHECK: quantization: { +# CHECK: scale: [ 0.12581, 0.001755, 0.001908, 0.001967, 0.007431, +# CHECK: zero_point: [ 0, 0, 0, 0, 0, 0, 0, 0, 0, +# CHECK: } +# CHECK: }, { +# CHECK: shape: [ 1, 1, 1, 186 ], +# CHECK: type: INT8, +# CHECK: buffer: 5, +# CHECK: name: "BoxPredictor_4/ClassPredictor/BiasAdd;BoxPredictor_4/ClassPredictor/Conv2D;BoxPredictor_4/ClassPredictor/biases1", +# CHECK: quantization: { +# CHECK: scale: [ 0.093635 ], +# CHECK: zero_point: [ 22 ] +# CHECK: } +# CHECK: }, { +# CHECK: shape: [ 1, 6, 31 ], +# CHECK: type: INT8, +# CHECK: buffer: 6, +# CHECK: name: "output", +# CHECK: quantization: { +# CHECK: scale: [ 0.093635 ], +# CHECK: zero_point: [ 22 ] +# CHECK: } +# CHECK: } ], +# CHECK: inputs: [ 0 ], +# CHECK: outputs: [ 5 ], +# CHECK: operators: [ { +# CHECK: inputs: [ 0, 3, 2 ], +# CHECK: outputs: [ 4 ], +# CHECK: builtin_options_type: Conv2DOptions, +# CHECK: builtin_options: { +# CHECK: stride_w: 1, +# CHECK: stride_h: 1 +# CHECK: } +# CHECK: }, { +# CHECK: opcode_index: 1, +# CHECK: inputs: [ 4, 1 ], +# CHECK: outputs: [ 5 ] +# CHECK: } ], +# CHECK: name: "main" +# CHECK: } ], +# CHECK: description: "MLIR Converted.", +# CHECK: buffers: [ { +# CHECK-EMPTY +# CHECK: }, { +# CHECK-EMPTY +# CHECK: }, { +# CHECK: data: [ 1, 0, 0, 0, 255, 255, 255, 255, 31, 0, 0, 0 ] +# CHECK: }, { +# CHECK: data: [ 245, 255, 255, 255, 186, 254, 255, 255, 213, 254, 255, 255, +# CHECK: }, { +# CHECK: data: [ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, +# CHECK: }, { +# CHECK-EMPTY +# CHECK: }, { +# CHECK-EMPTY +# CHECK: } ] +# CHECK: } diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/fake_quant_without_identity.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/fake_quant_without_identity.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..c7a039f6cf06d967465f6d4e5466189facfa49cb --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/fake_quant_without_identity.pbtxt @@ -0,0 +1,418 @@ +# RUN: tf_tfl_translate -tf-input-arrays=input -tf-input-shapes=1,1,1,256 -tf-input-data-types=DT_FLOAT -tf-inference-type=DT_QINT8 -tf-input-min-values='-33.614346' -tf-input-max-values='21.54917' -tf-output-arrays=output %s -o - --output-mlir 2>&1 | FileCheck --check-prefix=MLIR %s + +node { + name: "input" + op: "Placeholder" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/weights" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 186 + } + } + float_val: 1.0 + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/weights/read" + op: "Identity" + input: "BoxPredictor_4/ClassPredictor/weights" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@BoxPredictor_4/ClassPredictor/weights" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/weights_quant/min" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 186 + } + } + tensor_content: "\217\245\177\301\005@d\276C\030x\276S\320\177\276.\227q\277\260]\223\276j\016\250\276\305\017\202\276\200+\201\276\275\377\177\276\310&\202\276\314\210k\276\3178W\277\335\356_\276\332GG\276`\200>\276o_c\276\312\224k\2767\033S\276M=\376\300y-\271\276\225\021g\276\272lZ\276\353r\251\276\025\354G\276\357\220\\\2765j0\2765Q(\276\013>0\276Y\r\'\276\224\013\"\276G=\377\300\022\211E\276\344\035O\276\242\220l\276w\307\020\2773`\376\276\331\377\202\276\374\016Y\276\224\273^\2767`~\276V\016>\276\226\333g\276*\241K\276\224g\177\276\320\315(\276t\376\177\276B\025B\276@\214\226\276\377s\376\276\373\365\377\277\343\211~\276\205\320b\276\213\236\377\2763\0136\276u\237:\276\325\373~\276\344\r\037\276>\035(\276\247\364!\276\2613%\276\207]!\276\320S\376\301\354 _\276\337PT\276\342\226[\276\304\307\177\276\360\"x\276\312\023\200\276Wfp\2768$W\276\235\014\202\276\230\210m\2761\342L\276b\234\013\277\362XM\276QPL\276\006Va\276\3168>\276\262\367t\276x\252k\276\266\225\231\276z@B\276\244\340?\276>8]\276\214v\206\276\316\210Z\276\275\343U\276\366\0321\276b\2128\276\310\2611\276\340n/\276\312\272\'\276\207\274\207\276tl\034\276\214[\357\275\020\345@\276\1778\376\275\264~#\276\326U*\276\374g\003\276\377\013\000\276\361\375T\276\246\330\007\276\365\203~\276\311\367\033\276\261\240\177\2768`\"\276i\364 \276\321\365\363\275\0241~\276\005\365\376\275S\303\035\277\203\003\020\276\264\322\377\276$\241\177\276\264d\t\276H\000C\276\360/~\276\352\301\377\275\037\300\n\276\0000\033\276r|\377\275\025\004\002\276\276\021\262\276\343\245M\2763\302\351\276LE\005\276\n\"~\276\341q\341\276\265\022~\276\322^\036\276\206\216\031\276\306\n\310\276\322_T\276\367\037w\276\374\227A\276\010\272G\276T&\022\276y\217>\276\275H\033\276\002=\376\276\232x0\276\322C~\276\032\350\212\276\234\322\036\2762%\376\276\266\345#\276.1\021\276\302,\351\276\025x&\276\354\363.\276\362\210\025\276\035\032\036\276\267X\014\276zc\177\300\336L_\276\274,k\276{\266\203\276\366\266~\277\225F\211\276\302~\243\276\253.\214\2765\241l\276g\021\214\276\347\376\177\276H\026T\276\305\220\377\276\000\\Y\276\334\332>\276\224\2459\276\334ja\276\260\313p\276\024\026\\\2769\334\177\301\334\330\222\276\347\376e\276\014\230W\276\253P\252\276i\300C\276\340\261Y\276721\276#k)\276\035.#\276R(0\276\224\336!\276" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/weights_quant/max" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 186 + } + } + tensor_content: "\217\245\177A\005@d>C\030x>S\320\177>.\227q?\260]\223>j\016\250>\305\017\202>\200+\201>\275\377\177>\310&\202>\314\210k>\3178W?\335\356_>\332GG>`\200>>o_c>\312\224k>7\033S>M=\376@y-\271>\225\021g>\272lZ>\353r\251>\025\354G>\357\220\\>5j0>5Q(>\013>0>Y\r\'>\224\013\">G=\377@\022\211E>\344\035O>\242\220l>w\307\020?3`\376>\331\377\202>\374\016Y>\224\273^>7`~>V\016>>\226\333g>*\241K>\224g\177>\320\315(>t\376\177>B\025B>@\214\226>\377s\376>\373\365\377?\343\211~>\205\320b>\213\236\377>3\0136>u\237:>\325\373~>\344\r\037>>\035(>\247\364!>\2613%>\207]!>\320S\376A\354 _>\337PT>\342\226[>\304\307\177>\360\"x>\312\023\200>Wfp>8$W>\235\014\202>\230\210m>1\342L>b\234\013?\362XM>QPL>\006Va>\3168>>\262\367t>x\252k>\266\225\231>z@B>\244\340?>>8]>\214v\206>\316\210Z>\275\343U>\366\0321>b\2128>\310\2611>\340n/>\312\272\'>\207\274\207>tl\034>\214[\357=\020\345@>\1778\376=\264~#>\326U*>\374g\003>\377\013\000>\361\375T>\246\330\007>\365\203~>\311\367\033>\261\240\177>8`\">i\364 >\321\365\363=\0241~>\005\365\376=S\303\035?\203\003\020>\264\322\377>$\241\177>\264d\t>H\000C>\360/~>\352\301\377=\037\300\n>\0000\033>r|\377=\025\004\002>\276\021\262>\343\245M>3\302\351>LE\005>\n\"~>\341q\341>\265\022~>\322^\036>\206\216\031>\306\n\310>\322_T>\367\037w>\374\227A>\010\272G>T&\022>y\217>>\275H\033>\002=\376>\232x0>\322C~>\032\350\212>\234\322\036>2%\376>\266\345#>.1\021>\302,\351>\025x&>\354\363.>\362\210\025>\035\032\036>\267X\014>zc\177@\336L_>\274,k>{\266\203>\366\266~?\225F\211>\302~\243>\253.\214>5\241l>g\021\214>\347\376\177>H\026T>\305\220\377>\000\\Y>\334\332>>\224\2459>\334ja>\260\313p>\024\026\\>9\334\177A\334\330\222>\347\376e>\014\230W>\253P\252>i\300C>\340\261Y>721>#k)>\035.#>R(0>\224\336!>" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/weights_quant/FakeQuantWithMinMaxVarsPerChannel" + op: "FakeQuantWithMinMaxVarsPerChannel" + input: "BoxPredictor_4/ClassPredictor/weights/read" + input: "BoxPredictor_4/ClassPredictor/weights_quant/min" + input: "BoxPredictor_4/ClassPredictor/weights_quant/max" + attr { + key: "narrow_range" + value { + b: true + } + } + attr { + key: "num_bits" + value { + i: 8 + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/Conv2D" + op: "Conv2D" + input: "input" + input: "BoxPredictor_4/ClassPredictor/weights_quant/FakeQuantWithMinMaxVarsPerChannel" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NHWC" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/biases" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 186 + } + } + tensor_content: "\324\202\223\276\337\210\375\275\351\212\374\275p@\346\275HD\364\275W\236\362\275i\222\001\276\'\370\365\275:\250\345\275\244\214\373\275}\373\025\276^\351\000\2767_\017\276\362\t\322\2751%\376\275\364\020\324\275,\352\323\275\233\350\365\275\3777\014\276\371\022\277\2745{\366\275\270\207\366\275b\215\010\276\332\252\"\276\273\313\332\275\354\237\361\275\365W9\276o\364B\276\236HD\276$\250B\276CRC\276K\265\246\275f5\016\276[I\017\276\206\346\r\276\306\307\032\276\267\323\375\275\222\377\002\276\037\231\030\276\346W\001\2762F\004\276\021/\034\276\273\215\357\275\014m5\276\307[\021\276K\r\022\276Xv\365\275\312L\372\275A4\026\276\210[\024\276\254\242\003\276\207f\000\276\\C\023\276\322\233\000\276\266\0018\276\233\225\370\275\243\030\004\276]\201C\276\374j:\276\361\257@\276\017z<\276P?A\276a:\375\276\023\220\342\275W\026\377\275qD\005\276\'\271\371\275\342\207\n\276\273\221\003\276\372\205\363\275Ok\334\275\306\233\023\276y\271\"\276\313\345\n\276UH\"\276\233j\366\275\0026\377\275O\263\341\275\005\340\365\275,$\n\276\222\030\010\276\243\020\346\275\301\322\007\276n\252\374\275\325\251\007\276\325\033,\276\352\217\345\275\234\334\t\276\024\241@\276\256\365H\276zbA\276\262\361A\276\275C>\276\353Z\005>\2050&\276\035\222&\276\2457%\276\377/\'\276^\263\037\276\202\341%\276}\312\034\276&\2520\276Y\313,\276\232C*\276\233\375\032\276\212\250%\276\020\362\034\276\212\213-\276\211\211\030\276P\256\"\276\260\307\027\276?\332$\276\213\326\025\2761\243\023\276L\335\034\276\375\034\035\276L\2112\276\016\3571\276\224]\036\276\r\3165\276\232\340&\276?\2315\276^\342,\276\360{2\276\314\031\274=s\363\r\276C\227\035\276\332`\037\276\236\332\031\276Kj0\276$\034\035\276\227\262\033\276\231\260\033\2764\213\035\276\021\363$\276y\372&\276m`/\276<\272\027\276\267l\025\276\034x\031\276\202\313\034\276\026\363\027\276\307(\014\276<\332\005\276\231\316!\276\022\355\027\276-\251\037\2764\203+\276c \032\276\006#\036\276\22064\276\27450\2768O3\276\013\2321\276\31143\276a\367\n\2763\347\362\275?\037\003\276\241\375\001\276\340\003\332\275e\342\357\275\022\226\376\275\214\206\366\275\220\303\311\275\360\342\374\275\246\361\016\276\234\227\351\275\227\306\023\276\234\252\325\275\200\002\001\276\\\243\317\275\302\233\311\275\177\024\002\276\325\027\002\276\'\351\236=\316q\366\275\243>\010\276\203\204\373\275\365F/\276\225\323\346\275\250,\375\275E\0048\276\306\340?\276[/<\276\212\025B\276\251\264?\276" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/biases/read" + op: "Identity" + input: "BoxPredictor_4/ClassPredictor/biases" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@BoxPredictor_4/ClassPredictor/biases" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/BiasAdd" + op: "BiasAdd" + input: "BoxPredictor_4/ClassPredictor/Conv2D" + input: "BoxPredictor_4/ClassPredictor/biases/read" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NHWC" + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/act_quant/min" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: -14.066669 + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/act_quant/min/read" + op: "Identity" + input: "BoxPredictor_4/ClassPredictor/act_quant/min" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@BoxPredictor_4/ClassPredictor/act_quant/min" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/act_quant/max" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 9.810242 + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/act_quant/max/read" + op: "Identity" + input: "BoxPredictor_4/ClassPredictor/act_quant/max" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@BoxPredictor_4/ClassPredictor/act_quant/max" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/act_quant/FakeQuantWithMinMaxVars" + op: "FakeQuantWithMinMaxVars" + input: "BoxPredictor_4/ClassPredictor/BiasAdd" + input: "BoxPredictor_4/ClassPredictor/act_quant/min/read" + input: "BoxPredictor_4/ClassPredictor/act_quant/max/read" + attr { + key: "narrow_range" + value { + b: false + } + } + attr { + key: "num_bits" + value { + i: 8 + } + } +} +node { + name: "BoxPredictor_4/Reshape_1/shape" + op: "Const" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 3 + } + } + tensor_content: "\001\000\000\000\377\377\377\377\037\000\000\000" + } + } + } +} +node { + name: "output" + op: "Reshape" + input: "BoxPredictor_4/ClassPredictor/act_quant/FakeQuantWithMinMaxVars" + input: "BoxPredictor_4/Reshape_1/shape" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tshape" + value { + type: DT_INT32 + } + } +} + +# MLIR-LABEL: func @main +# MLIR-SAME: (%[[ARG_0:[a-z0-9]+]]: tensor<1x1x1x256x!quant.uniform>) -> tensor<1x6x31x!quant.uniform> +# MLIR-SAME: control_outputs = "" +# MLIR-SAME: inputs = "input" +# MLIR-SAME: outputs = "output" +# MLIR: %[[shape:.*]] = arith.constant dense<[1, -1, 31]> : tensor<3xi32> +# MLIR: %[[bias:.*]] = "tfl.pseudo_qconst"() <{qtype = tensor<186x!quant.uniform:f32:0, {0.12581039038230116, +# MLIR: %[[conv:.*]] = "tfl.conv_2d"(%[[ARG_0]], %[[weight]], %[[bias]]) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32} +# MLIR: %[[reshape:.*]] = "tfl.reshape"(%[[conv]], %[[shape]]) : (tensor<1x1x1x186x!quant.uniform>, tensor<3xi32>) +# MLIR: return %[[reshape]] : tensor<1x6x31x!quant.uniform> +# MLIR: } diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/fake_quant_without_identity_4bit.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/fake_quant_without_identity_4bit.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..1f9742fc7637b7cbec8e09225425ab930ddb57de --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/fake_quant_without_identity_4bit.pbtxt @@ -0,0 +1,418 @@ +# RUN: tf_tfl_translate -tf-input-arrays=input -tf-input-shapes=1,1,1,256 -tf-input-data-types=DT_FLOAT -tf-inference-type=DT_QINT8 -tf-input-min-values='-33.614346' -tf-input-max-values='21.54917' -tf-output-arrays=output %s -o - --output-mlir 2>&1 | FileCheck --check-prefix=MLIR %s + +node { + name: "input" + op: "Placeholder" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/weights" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 1 + } + dim { + size: 1 + } + dim { + size: 256 + } + dim { + size: 186 + } + } + float_val: 1.0 + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/weights/read" + op: "Identity" + input: "BoxPredictor_4/ClassPredictor/weights" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@BoxPredictor_4/ClassPredictor/weights" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/weights_quant/min" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 186 + } + } + tensor_content: "\217\245\177\301\005@d\276C\030x\276S\320\177\276.\227q\277\260]\223\276j\016\250\276\305\017\202\276\200+\201\276\275\377\177\276\310&\202\276\314\210k\276\3178W\277\335\356_\276\332GG\276`\200>\276o_c\276\312\224k\2767\033S\276M=\376\300y-\271\276\225\021g\276\272lZ\276\353r\251\276\025\354G\276\357\220\\\2765j0\2765Q(\276\013>0\276Y\r\'\276\224\013\"\276G=\377\300\022\211E\276\344\035O\276\242\220l\276w\307\020\2773`\376\276\331\377\202\276\374\016Y\276\224\273^\2767`~\276V\016>\276\226\333g\276*\241K\276\224g\177\276\320\315(\276t\376\177\276B\025B\276@\214\226\276\377s\376\276\373\365\377\277\343\211~\276\205\320b\276\213\236\377\2763\0136\276u\237:\276\325\373~\276\344\r\037\276>\035(\276\247\364!\276\2613%\276\207]!\276\320S\376\301\354 _\276\337PT\276\342\226[\276\304\307\177\276\360\"x\276\312\023\200\276Wfp\2768$W\276\235\014\202\276\230\210m\2761\342L\276b\234\013\277\362XM\276QPL\276\006Va\276\3168>\276\262\367t\276x\252k\276\266\225\231\276z@B\276\244\340?\276>8]\276\214v\206\276\316\210Z\276\275\343U\276\366\0321\276b\2128\276\310\2611\276\340n/\276\312\272\'\276\207\274\207\276tl\034\276\214[\357\275\020\345@\276\1778\376\275\264~#\276\326U*\276\374g\003\276\377\013\000\276\361\375T\276\246\330\007\276\365\203~\276\311\367\033\276\261\240\177\2768`\"\276i\364 \276\321\365\363\275\0241~\276\005\365\376\275S\303\035\277\203\003\020\276\264\322\377\276$\241\177\276\264d\t\276H\000C\276\360/~\276\352\301\377\275\037\300\n\276\0000\033\276r|\377\275\025\004\002\276\276\021\262\276\343\245M\2763\302\351\276LE\005\276\n\"~\276\341q\341\276\265\022~\276\322^\036\276\206\216\031\276\306\n\310\276\322_T\276\367\037w\276\374\227A\276\010\272G\276T&\022\276y\217>\276\275H\033\276\002=\376\276\232x0\276\322C~\276\032\350\212\276\234\322\036\2762%\376\276\266\345#\276.1\021\276\302,\351\276\025x&\276\354\363.\276\362\210\025\276\035\032\036\276\267X\014\276zc\177\300\336L_\276\274,k\276{\266\203\276\366\266~\277\225F\211\276\302~\243\276\253.\214\2765\241l\276g\021\214\276\347\376\177\276H\026T\276\305\220\377\276\000\\Y\276\334\332>\276\224\2459\276\334ja\276\260\313p\276\024\026\\\2769\334\177\301\334\330\222\276\347\376e\276\014\230W\276\253P\252\276i\300C\276\340\261Y\276721\276#k)\276\035.#\276R(0\276\224\336!\276" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/weights_quant/max" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 186 + } + } + tensor_content: "\217\245\177A\005@d>C\030x>S\320\177>.\227q?\260]\223>j\016\250>\305\017\202>\200+\201>\275\377\177>\310&\202>\314\210k>\3178W?\335\356_>\332GG>`\200>>o_c>\312\224k>7\033S>M=\376@y-\271>\225\021g>\272lZ>\353r\251>\025\354G>\357\220\\>5j0>5Q(>\013>0>Y\r\'>\224\013\">G=\377@\022\211E>\344\035O>\242\220l>w\307\020?3`\376>\331\377\202>\374\016Y>\224\273^>7`~>V\016>>\226\333g>*\241K>\224g\177>\320\315(>t\376\177>B\025B>@\214\226>\377s\376>\373\365\377?\343\211~>\205\320b>\213\236\377>3\0136>u\237:>\325\373~>\344\r\037>>\035(>\247\364!>\2613%>\207]!>\320S\376A\354 _>\337PT>\342\226[>\304\307\177>\360\"x>\312\023\200>Wfp>8$W>\235\014\202>\230\210m>1\342L>b\234\013?\362XM>QPL>\006Va>\3168>>\262\367t>x\252k>\266\225\231>z@B>\244\340?>>8]>\214v\206>\316\210Z>\275\343U>\366\0321>b\2128>\310\2611>\340n/>\312\272\'>\207\274\207>tl\034>\214[\357=\020\345@>\1778\376=\264~#>\326U*>\374g\003>\377\013\000>\361\375T>\246\330\007>\365\203~>\311\367\033>\261\240\177>8`\">i\364 >\321\365\363=\0241~>\005\365\376=S\303\035?\203\003\020>\264\322\377>$\241\177>\264d\t>H\000C>\360/~>\352\301\377=\037\300\n>\0000\033>r|\377=\025\004\002>\276\021\262>\343\245M>3\302\351>LE\005>\n\"~>\341q\341>\265\022~>\322^\036>\206\216\031>\306\n\310>\322_T>\367\037w>\374\227A>\010\272G>T&\022>y\217>>\275H\033>\002=\376>\232x0>\322C~>\032\350\212>\234\322\036>2%\376>\266\345#>.1\021>\302,\351>\025x&>\354\363.>\362\210\025>\035\032\036>\267X\014>zc\177@\336L_>\274,k>{\266\203>\366\266~?\225F\211>\302~\243>\253.\214>5\241l>g\021\214>\347\376\177>H\026T>\305\220\377>\000\\Y>\334\332>>\224\2459>\334ja>\260\313p>\024\026\\>9\334\177A\334\330\222>\347\376e>\014\230W>\253P\252>i\300C>\340\261Y>721>#k)>\035.#>R(0>\224\336!>" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/weights_quant/FakeQuantWithMinMaxVarsPerChannel" + op: "FakeQuantWithMinMaxVarsPerChannel" + input: "BoxPredictor_4/ClassPredictor/weights/read" + input: "BoxPredictor_4/ClassPredictor/weights_quant/min" + input: "BoxPredictor_4/ClassPredictor/weights_quant/max" + attr { + key: "narrow_range" + value { + b: true + } + } + attr { + key: "num_bits" + value { + i: 4 + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/Conv2D" + op: "Conv2D" + input: "input" + input: "BoxPredictor_4/ClassPredictor/weights_quant/FakeQuantWithMinMaxVarsPerChannel" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NHWC" + } + } + attr { + key: "dilations" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } + attr { + key: "explicit_paddings" + value { + list { + } + } + } + attr { + key: "padding" + value { + s: "SAME" + } + } + attr { + key: "strides" + value { + list { + i: 1 + i: 1 + i: 1 + i: 1 + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/biases" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 186 + } + } + tensor_content: "\324\202\223\276\337\210\375\275\351\212\374\275p@\346\275HD\364\275W\236\362\275i\222\001\276\'\370\365\275:\250\345\275\244\214\373\275}\373\025\276^\351\000\2767_\017\276\362\t\322\2751%\376\275\364\020\324\275,\352\323\275\233\350\365\275\3777\014\276\371\022\277\2745{\366\275\270\207\366\275b\215\010\276\332\252\"\276\273\313\332\275\354\237\361\275\365W9\276o\364B\276\236HD\276$\250B\276CRC\276K\265\246\275f5\016\276[I\017\276\206\346\r\276\306\307\032\276\267\323\375\275\222\377\002\276\037\231\030\276\346W\001\2762F\004\276\021/\034\276\273\215\357\275\014m5\276\307[\021\276K\r\022\276Xv\365\275\312L\372\275A4\026\276\210[\024\276\254\242\003\276\207f\000\276\\C\023\276\322\233\000\276\266\0018\276\233\225\370\275\243\030\004\276]\201C\276\374j:\276\361\257@\276\017z<\276P?A\276a:\375\276\023\220\342\275W\026\377\275qD\005\276\'\271\371\275\342\207\n\276\273\221\003\276\372\205\363\275Ok\334\275\306\233\023\276y\271\"\276\313\345\n\276UH\"\276\233j\366\275\0026\377\275O\263\341\275\005\340\365\275,$\n\276\222\030\010\276\243\020\346\275\301\322\007\276n\252\374\275\325\251\007\276\325\033,\276\352\217\345\275\234\334\t\276\024\241@\276\256\365H\276zbA\276\262\361A\276\275C>\276\353Z\005>\2050&\276\035\222&\276\2457%\276\377/\'\276^\263\037\276\202\341%\276}\312\034\276&\2520\276Y\313,\276\232C*\276\233\375\032\276\212\250%\276\020\362\034\276\212\213-\276\211\211\030\276P\256\"\276\260\307\027\276?\332$\276\213\326\025\2761\243\023\276L\335\034\276\375\034\035\276L\2112\276\016\3571\276\224]\036\276\r\3165\276\232\340&\276?\2315\276^\342,\276\360{2\276\314\031\274=s\363\r\276C\227\035\276\332`\037\276\236\332\031\276Kj0\276$\034\035\276\227\262\033\276\231\260\033\2764\213\035\276\021\363$\276y\372&\276m`/\276<\272\027\276\267l\025\276\034x\031\276\202\313\034\276\026\363\027\276\307(\014\276<\332\005\276\231\316!\276\022\355\027\276-\251\037\2764\203+\276c \032\276\006#\036\276\22064\276\27450\2768O3\276\013\2321\276\31143\276a\367\n\2763\347\362\275?\037\003\276\241\375\001\276\340\003\332\275e\342\357\275\022\226\376\275\214\206\366\275\220\303\311\275\360\342\374\275\246\361\016\276\234\227\351\275\227\306\023\276\234\252\325\275\200\002\001\276\\\243\317\275\302\233\311\275\177\024\002\276\325\027\002\276\'\351\236=\316q\366\275\243>\010\276\203\204\373\275\365F/\276\225\323\346\275\250,\375\275E\0048\276\306\340?\276[/<\276\212\025B\276\251\264?\276" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/biases/read" + op: "Identity" + input: "BoxPredictor_4/ClassPredictor/biases" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@BoxPredictor_4/ClassPredictor/biases" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/BiasAdd" + op: "BiasAdd" + input: "BoxPredictor_4/ClassPredictor/Conv2D" + input: "BoxPredictor_4/ClassPredictor/biases/read" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "data_format" + value { + s: "NHWC" + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/act_quant/min" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: -14.066669 + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/act_quant/min/read" + op: "Identity" + input: "BoxPredictor_4/ClassPredictor/act_quant/min" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@BoxPredictor_4/ClassPredictor/act_quant/min" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/act_quant/max" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 9.810242 + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/act_quant/max/read" + op: "Identity" + input: "BoxPredictor_4/ClassPredictor/act_quant/max" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@BoxPredictor_4/ClassPredictor/act_quant/max" + } + } + } +} +node { + name: "BoxPredictor_4/ClassPredictor/act_quant/FakeQuantWithMinMaxVars" + op: "FakeQuantWithMinMaxVars" + input: "BoxPredictor_4/ClassPredictor/BiasAdd" + input: "BoxPredictor_4/ClassPredictor/act_quant/min/read" + input: "BoxPredictor_4/ClassPredictor/act_quant/max/read" + attr { + key: "narrow_range" + value { + b: false + } + } + attr { + key: "num_bits" + value { + i: 8 + } + } +} +node { + name: "BoxPredictor_4/Reshape_1/shape" + op: "Const" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 3 + } + } + tensor_content: "\001\000\000\000\377\377\377\377\037\000\000\000" + } + } + } +} +node { + name: "output" + op: "Reshape" + input: "BoxPredictor_4/ClassPredictor/act_quant/FakeQuantWithMinMaxVars" + input: "BoxPredictor_4/Reshape_1/shape" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tshape" + value { + type: DT_INT32 + } + } +} + +# MLIR-LABEL: func @main +# MLIR-SAME: (%[[ARG_0:[a-z0-9]+]]: tensor<1x1x1x256x!quant.uniform>) -> tensor<1x6x31x!quant.uniform> +# MLIR-SAME: control_outputs = "" +# MLIR-SAME: inputs = "input" +# MLIR-SAME: outputs = "output" +# MLIR: %[[shape:.*]] = arith.constant dense<[1, -1, 31]> : tensor<3xi32> +# MLIR: %[[bias:.*]] = "tfl.pseudo_qconst"() <{qtype = tensor<186x!quant.uniform:f32:0, {2.2825599397931779, +# MLIR: %[[conv:.*]] = "tfl.conv_2d"(%[[ARG_0]], %[[weight]], %[[bias]]) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32} +# MLIR: %[[reshape:.*]] = "tfl.reshape"(%[[conv]], %[[shape]]) : (tensor<1x1x1x186x!quant.uniform>, tensor<3xi32>) +# MLIR: return %[[reshape]] : tensor<1x6x31x!quant.uniform> +# MLIR: } diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/graph-input-node.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/graph-input-node.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..5e0f5170d095deeb77b6bb6c7a8f7517cb78e5a0 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/graph-input-node.pbtxt @@ -0,0 +1,56 @@ +# RUN: tf_tfl_translate -tf-input-arrays=input -tf-input-shapes=4 -tf-input-data-types=DT_INT32 -tf-output-arrays=output %s -o - --output-mlir | FileCheck %s + +node { + name: "default" + op: "Const" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + } + int_val: 0 + } + } + } +} +node { + name: "input" + op: "Identity" + input: "default" + attr { + key: "T" + value { + type: DT_INT32 + } + } +} +node { + name: "output" + op: "Identity" + input: "input" + attr { + key: "T" + value { + type: DT_INT32 + } + } +} +versions { + producer: 27 +} + +# CHECK-LABEL: func @main +# CHECK-SAME: (%[[ARG_0:[a-z0-9]+]]: tensor<4xi32>) -> tensor<4xi32> +# CHECK-SAME: control_outputs = "" +# CHECK-SAME: inputs = "input" +# CHECK-SAME: outputs = "output" +# CHECK-NEXT: return %[[ARG_0]] : tensor<4xi32> +# CHECK-NEXT: } diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/graph_with_placeholder_with_default.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/graph_with_placeholder_with_default.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..eae044d6b048966439126d68cd014da050538f5a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/graph_with_placeholder_with_default.pbtxt @@ -0,0 +1,148 @@ +# RUN: tf_tfl_translate -tf-input-arrays=unranked -tf-input-shapes=1,8,8,2 -tf-input-data-types=DT_INT32 -tf-output-arrays=unranked,static,static_10 %s -o - --output-mlir | FileCheck %s + +node { + name: "tf.Const" + op: "Const" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 10 + } + } + int_val: 0 + } + } + } + experimental_debug_info { + } +} +node { + name: "tf.Const1" + op: "Const" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + } + int_val: 0 + } + } + } + experimental_debug_info { + } +} +node { + name: "unranked" + op: "PlaceholderWithDefault" + input: "tf.Const1" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "shape" + value { + shape { + unknown_rank: true + } + } + } + attr { + key: "type" + value { + type: DT_INT32 + } + } + experimental_debug_info { + } +} +node { + name: "static" + op: "PlaceholderWithDefault" + input: "tf.Const1" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "shape" + value { + shape { + } + } + } + attr { + key: "type" + value { + type: DT_INT32 + } + } + experimental_debug_info { + } +} +node { + name: "static_10" + op: "PlaceholderWithDefault" + input: "tf.Const" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 10 + } + } + } + } + attr { + key: "type" + value { + type: DT_INT32 + } + } + experimental_debug_info { + } +} +library { +} +versions { + producer: 198 +} + +# CHECK-LABEL: func @main +# CHECK-SAME: ([[VAL_0:%.*]]: tensor<1x8x8x2xi32>) -> (tensor<1x8x8x2xi32>, tensor, tensor<10xi32>) +# CHECK-SAME: control_outputs = "" +# CHECK-SAME: inputs = "unranked" +# CHECK-SAME: outputs = "unranked,static,static_10" +# CHECK-DAG: [[VAL_2:%.*]] = arith.constant dense<0> : tensor<10xi32> +# CHECK-DAG: [[VAL_1:%.*]] = arith.constant dense<0> : tensor +# CHECK: return [[VAL_0]], [[VAL_1]], [[VAL_2]] : tensor<1x8x8x2xi32>, tensor, tensor<10xi32> +# CHECK: } diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/if_op.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/if_op.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..35196223ba61e43d0b3f98d3f2965650c4c141b7 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/if_op.pbtxt @@ -0,0 +1,421 @@ +# RUN: tf_tfl_translate -tf-input-arrays=a,b -tf-input-data-types=DT_FLOAT,DT_FLOAT -tf-input-shapes=: -tf-output-arrays=StatefulIf,StatelessIf %s -o - --output-mlir | FileCheck %s +node { + name: "tf.Less" + op: "Less" + input: "a" + input: "b" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + experimental_debug_info { + } +} +node { + name: "my_equal" + op: "Equal" + input: "a" + input: "b" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + experimental_debug_info { + } +} +node { + name: "cst0" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 4 + } + } + float_val: 1.0 + float_val: 2.0 + float_val: 3.0 + float_val: 4.0 + } + } + } +} +node { + name: "cst1" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 4 + } + } + float_val: 5.0 + float_val: 6.0 + float_val: 7.0 + float_val: 8.0 + } + } + } +} +node { + name: "StatefulIf" + op: "If" + input: "tf.Less" + input: "a" + input: "b" + input: "cst0" + input: "cst1" + attr { + key: "Tcond" + value { + type: DT_BOOL + } + } + attr { + key: "Tin" + value { + list { + type: DT_FLOAT + type: DT_FLOAT + type: DT_FLOAT + type: DT_FLOAT + } + } + } + attr { + key: "Tout" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "else_branch" + value { + func { + name: "cond_false" + } + } + } + attr { + key: "then_branch" + value { + func { + name: "cond_true" + } + } + } + experimental_debug_info { + } +} +node { + name: "StatelessIf" + op: "StatelessIf" + input: "my_equal" + input: "a" + input: "b" + attr { + key: "Tcond" + value { + type: DT_BOOL + } + } + attr { + key: "Tin" + value { + list { + type: DT_FLOAT + type: DT_FLOAT + } + } + } + attr { + key: "Tout" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "else_branch" + value { + func { + name: "cond_false_1" + } + } + } + attr { + key: "then_branch" + value { + func { + name: "cond_true_1" + } + } + } + experimental_debug_info { + } +} +node { + name: "main" + op: "_Retval" + input: "StatefulIf" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "index" + value { + i: 0 + } + } +} +node { + name: "main1" + op: "_Retval" + input: "StatelessIf" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "index" + value { + i: 1 + } + } +} +node { + name: "a" + op: "Placeholder" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + experimental_debug_info { + } +} +node { + name: "b" + op: "Placeholder" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + experimental_debug_info { + } +} +library { + function { + signature { + name: "cond_true" + input_arg { + name: "cond_true_arg0" + type: DT_FLOAT + } + input_arg { + name: "cond_true_arg1" + type: DT_FLOAT + } + input_arg { + name: "cond_true_arg2" + type: DT_FLOAT + } + input_arg { + name: "cond_true_arg3" + type: DT_FLOAT + } + output_arg { + name: "cond_true_ret" + type: DT_FLOAT + } + } + node_def { + name: "tf.Add" + op: "Add" + input: "cond_true_arg2" + input: "cond_true_arg3" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + experimental_debug_info { + original_node_names: "tf.Add" + } + } + ret { + key: "cond_true_ret" + value: "tf.Add:z:0" + } + } + function { + signature { + name: "cond_false" + input_arg { + name: "cond_false_arg0" + type: DT_FLOAT + } + input_arg { + name: "cond_false_arg1" + type: DT_FLOAT + } + input_arg { + name: "cond_false_arg2" + type: DT_FLOAT + } + input_arg { + name: "cond_false_arg3" + type: DT_FLOAT + } + output_arg { + name: "cond_false_ret" + type: DT_FLOAT + } + } + node_def { + name: "tf.Mul" + op: "Mul" + input: "cond_false_arg0" + input: "cond_false_arg3" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + experimental_debug_info { + original_node_names: "tf.Mul" + } + } + ret { + key: "cond_false_ret" + value: "tf.Mul:z:0" + } + } + function { + signature { + name: "cond_true_1" + input_arg { + name: "cond_true_arg0" + type: DT_FLOAT + } + input_arg { + name: "cond_true_arg1" + type: DT_FLOAT + } + output_arg { + name: "cond_true_ret" + type: DT_FLOAT + } + } + node_def { + name: "tf.Sub" + op: "Sub" + input: "cond_true_arg0" + input: "cond_true_arg1" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + experimental_debug_info { + original_node_names: "tf.Sub" + } + } + ret { + key: "cond_true_ret" + value: "tf.Sub:z:0" + } + } + function { + signature { + name: "cond_false_1" + input_arg { + name: "cond_false_arg0" + type: DT_FLOAT + } + input_arg { + name: "cond_false_arg1" + type: DT_FLOAT + } + output_arg { + name: "cond_false_ret" + type: DT_FLOAT + } + } + node_def { + name: "tf.Div" + op: "Div" + input: "cond_false_arg0" + input: "cond_false_arg1" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + experimental_debug_info { + original_node_names: "tf.Div" + } + } + ret { + key: "cond_false_ret" + value: "tf.Div:z:0" + } + } +} +versions { + producer: 115 + min_consumer: 12 +} + +# CHECK: func @main +# CHECK-DAG: constant dense<[1.000000e+00, 2.000000e+00, 3.000000e+00, 4.000000e+00]> +# CHECK-DAG: constant dense<[5.000000e+00, 6.000000e+00, 7.000000e+00, 8.000000e+00]> +# CHECK: "tf.If"{{.+}}else_branch = @cond_false_10{{.+}}is_stateless = true{{.+}}then_branch = @cond_true_10 +# CHECK: "tf.If"{{.+}}else_branch = @cond_false0{{.+}}is_stateless = false{{.+}}then_branch = @cond_true0 +# CHECK: func private @cond_false_10 +# CHECK-NEXT: tfl.div +# CHECK: func private @cond_true_10 +# CHECK-NEXT: tfl.sub +# CHECK: func private @cond_false0 +# CHECK-NEXT: tfl.mul +# CHECK: func private @cond_true0 +# CHECK-NEXT: tfl.add diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/quant_stats.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/quant_stats.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..52e00d2cb144ca48a0b61388f9dce49a78559b42 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/quant_stats.pbtxt @@ -0,0 +1,124 @@ +# RUN: tf_tfl_translate -tf-input-arrays=input0,input1 \ +# RUN: -tf-input-shapes=4:4 \ +# RUN: -tf-input-data-types=DT_FLOAT,DT_FLOAT \ +# RUN: -tf-output-arrays=Add \ +# RUN: -tf-inference-type=DT_QUINT8 \ +# RUN: -tf-input-min-values='-2,-3' \ +# RUN: -tf-input-max-values='2,3' \ +# RUN: --quant-stats=%s.stats \ +# RUN: %s -o - --output-mlir 2>&1 \ +# RUN: | FileCheck --check-prefix=MLIR %s +# RUN: tf_tfl_translate -tf-input-arrays=input0,input1 \ +# RUN: -tf-input-shapes=4:4 \ +# RUN: -tf-input-data-types=DT_FLOAT,DT_FLOAT \ +# RUN: -tf-output-arrays=Add \ +# RUN: -tf-inference-type=DT_QUINT8 \ +# RUN: -tf-input-min-values='-2,-3' \ +# RUN: -tf-input-max-values='2,3' \ +# RUN: --quant-stats=%s.stats \ +# RUN: %s -o - \ +# RUN: | flatbuffer_to_string - \ +# RUN: | FileCheck %s + +node { + name: "Add" + op: "Add" + input: "input0" + input: "input1" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "input0" + op: "Placeholder" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +node { + name: "input1" + op: "Placeholder" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } +} +versions { + producer: 27 +} + +# MLIR-LABEL: func @main(%arg0: tensor<4x!quant.uniform>, %arg1: tensor<4x!quant.uniform>) -> tensor<4x!quant.uniform> +# MLIR-SAME: attributes {tf.entry_function = {control_outputs = "", inputs = "input0,input1", outputs = "Add"}} { +# MLIR-NEXT: %[[add:.*]] = tfl.add(%arg0, %arg1) <{fused_activation_function = "NONE"}> : (tensor<4x!quant.uniform>, tensor<4x!quant.uniform>) -> tensor<4x!quant.uniform> +# MLIR-NEXT: return %[[add]] : tensor<4x!quant.uniform> +# MLIR-NEXT: } + +# CHECK-LABEL: { +# CHECK-NEXT: version: 3, +# CHECK-NEXT: operator_codes: [ { +# CHECK-NEXT: version: 1 +# CHECK-NEXT: } ], +# CHECK-NEXT: subgraphs: [ { +# CHECK-NEXT: tensors: [ { +# CHECK-NEXT: shape: [ 4 ], +# CHECK-NEXT: type: UINT8, +# CHECK-NEXT: buffer: 1, +# CHECK-NEXT: name: "input0", +# CHECK-NEXT: quantization: { +# CHECK-NEXT: scale: [ 0.015686 ], +# CHECK-NEXT: zero_point: [ 128 ] +# CHECK-NEXT: }, +# CHECK-NEXT: has_rank: true +# CHECK-NEXT: }, { +# CHECK-NEXT: shape: [ 4 ], +# CHECK-NEXT: type: UINT8, +# CHECK-NEXT: buffer: 2, +# CHECK-NEXT: name: "input1", +# CHECK-NEXT: quantization: { +# CHECK-NEXT: scale: [ 0.023529 ], +# CHECK-NEXT: zero_point: [ 128 ] +# CHECK-NEXT: }, +# CHECK-NEXT: has_rank: true +# CHECK-NEXT: }, { +# CHECK-NEXT: shape: [ 4 ], +# CHECK-NEXT: type: UINT8, +# CHECK-NEXT: buffer: 3, +# CHECK-NEXT: name: "Add", +# CHECK-NEXT: quantization: { +# CHECK-NEXT: scale: [ 0.007843 ], +# CHECK-NEXT: zero_point: [ 128 ] +# CHECK-NEXT: }, +# CHECK-NEXT: has_rank: true +# CHECK-NEXT: } ], +# CHECK-NEXT: inputs: [ 0, 1 ], +# CHECK-NEXT: outputs: [ 2 ], +# CHECK-NEXT: operators: [ { +# CHECK-NEXT: inputs: [ 0, 1 ], +# CHECK-NEXT: outputs: [ 2 ], +# CHECK-NEXT: builtin_options_type: AddOptions, +# CHECK-NEXT: builtin_options: { +# CHECK-EMPTY: +# CHECK-NEXT: } +# CHECK-NEXT: } ], +# CHECK-NEXT: name: "main" +# CHECK-NEXT: } ], +# CHECK-NEXT: description: "MLIR Converted.", +# CHECK-NEXT: buffers: [ { +# CHECK-EMPTY: +# CHECK-NEXT: }, { +# CHECK-EMPTY: +# CHECK-NEXT: }, { +# CHECK-EMPTY: +# CHECK-NEXT: }, { +# CHECK: data: [ 49, 46, 53, 46, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] +# CHECK-NEXT: } ] + diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/quant_stats.pbtxt.stats b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/quant_stats.pbtxt.stats new file mode 100644 index 0000000000000000000000000000000000000000..6bd6ae44770ca58ba0b7a8ae45aa10b2d3e7580e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/quant_stats.pbtxt.stats @@ -0,0 +1,9 @@ +entries { + name: "Add" + params { + min_max { + min: -1 + max: 1 + } + } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/unroll_batch_matmul.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/unroll_batch_matmul.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..ff3f6d414d447183143229f5874d7bd6326198de --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/unroll_batch_matmul.pbtxt @@ -0,0 +1,90 @@ +# RUN: tf_tfl_translate -tf-input-arrays=Placeholder,Placeholder_1 -tf-input-shapes=2,5,3:3,7 -tf-input-data-types=DT_FLOAT,DT_FLOAT -tf-output-arrays=MatMul -unfold_batchmatmul=true -output-mlir %s -o - 2>&1 | FileCheck %s + +node { + name: "Placeholder" + op: "Placeholder" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2 + } + dim { + size: 5 + } + dim { + size: 3 + } + } + } + } +} +node { + name: "Placeholder_1" + op: "Placeholder" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 7 + } + } + } + } +} +node { + name: "MatMul" + op: "BatchMatMulV2" + input: "Placeholder" + input: "Placeholder_1" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "adj_x" + value { + b: false + } + } + attr { + key: "adj_y" + value { + b: false + } + } +} +versions { + producer: 175 +} + +# CHECK: func @main(%[[VAL_0:.*]]: tensor<2x5x3xf32>, %[[VAL_1:.*]]: tensor<3x7xf32>) -> tensor<2x5x7xf32> attributes {tf.entry_function = {control_outputs = "", inputs = "Placeholder,Placeholder_1", outputs = "MatMul"}} { +# CHECK-DAG: %[[VAL_2:.*]] = arith.constant dense<[1, 0]> : tensor<2xi32> +# CHECK-DAG: %[[VAL_3:.*]] = "tfl.no_value"() <{value}> : () -> none +# CHECK-DAG: %[[VAL_6:.*]] = arith.constant dense<0> : tensor +# CHECK: %[[VAL_7:.*]]:2 = "tfl.split"(%[[VAL_6]], %[[VAL_0]]) <{num_splits = 2 : i32}> : (tensor, tensor<2x5x3xf32>) -> (tensor<1x5x3xf32>, tensor<1x5x3xf32>) +# CHECK: %[[VAL_9:.*]] = "tfl.transpose"(%[[VAL_1]], %[[VAL_2]]) : (tensor<3x7xf32>, tensor<2xi32>) -> tensor<7x3xf32> +# CHECK: %[[VAL_10:.*]] = "tfl.fully_connected"(%[[VAL_7]]#0, %[[VAL_9]], %[[VAL_3]]) <{fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"}> : (tensor<1x5x3xf32>, tensor<7x3xf32>, none) -> tensor<5x7xf32> +# CHECK: %[[VAL_11:.*]] = "tfl.fully_connected"(%[[VAL_7]]#1, %[[VAL_9]], %[[VAL_3]]) <{fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"}> : (tensor<1x5x3xf32>, tensor<7x3xf32>, none) -> tensor<5x7xf32> +# CHECK: %[[VAL_12:.*]] = "tfl.pack"(%[[VAL_10]], %[[VAL_11]]) <{axis = 0 : i32, values_count = 2 : i32}> : (tensor<5x7xf32>, tensor<5x7xf32>) -> tensor<2x5x7xf32> +# CHECK: return %[[VAL_12]] : tensor<2x5x7xf32> +# CHECK: } diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/unroll_batch_matmul_disabled.pbtxt b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/unroll_batch_matmul_disabled.pbtxt new file mode 100644 index 0000000000000000000000000000000000000000..75dc4b8f712147d32215eaac924145d7a9a734db --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/end2end/unroll_batch_matmul_disabled.pbtxt @@ -0,0 +1,83 @@ +# RUN: tf_tfl_translate -unfold_batchmatmul=false -tf-input-arrays=Placeholder,Placeholder_1 -tf-input-shapes=2,5,3:3,7 -tf-input-data-types=DT_FLOAT,DT_FLOAT -tf-output-arrays=MatMul -output-mlir %s -o - 2>&1 | FileCheck %s + +node { + name: "Placeholder" + op: "Placeholder" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2 + } + dim { + size: 5 + } + dim { + size: 3 + } + } + } + } +} +node { + name: "Placeholder_1" + op: "Placeholder" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + dim { + size: 7 + } + } + } + } +} +node { + name: "MatMul" + op: "BatchMatMulV2" + input: "Placeholder" + input: "Placeholder_1" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "adj_x" + value { + b: false + } + } + attr { + key: "adj_y" + value { + b: false + } + } +} +versions { + producer: 175 +} + +# CHECK: func @main(%[[VAL_0:.*]]: tensor<2x5x3xf32>, %[[VAL_1:.*]]: tensor<3x7xf32>) -> tensor<2x5x7xf32> attributes {tf.entry_function = {control_outputs = "", inputs = "Placeholder,Placeholder_1", outputs = "MatMul"}} { +# CHECK: %[[VAL_2:.*]] = "tfl.batch_matmul"(%[[VAL_0]], %[[VAL_1]]) <{adj_x = false, adj_y = false}> : (tensor<2x5x3xf32>, tensor<3x7xf32>) -> tensor<2x5x7xf32> +# CHECK: return %[[VAL_2]] : tensor<2x5x7xf32> +# CHECK: } diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..dc4e259fed1a1589dd8dcf06073d8e768f5563e4 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/BUILD @@ -0,0 +1,65 @@ +load("//tensorflow:tensorflow.bzl", "tf_native_cc_binary") +load("//tensorflow:tensorflow.default.bzl", "filegroup") +load("//tensorflow/compiler/mlir:glob_lit_test.bzl", "glob_lit_tests") + +# copybara:uncomment package(default_applicable_licenses = ["//tensorflow:LICENSE"]) + +licenses(["notice"]) + +glob_lit_tests( + name = "all_tests", + data = [ + ":extra_files", + ":test_utilities", + ], + driver = "@llvm-project//mlir:run_lit.sh", + test_file_exts = [ + "mlir", + "cc", + "json", + ], +) + +# Bundle together all of the test utilities that are used by tests. +filegroup( + name = "test_utilities", + testonly = True, + data = [ + ":importer_test_min_max", + ":test_schema.fbs", + "//tensorflow/compiler/mlir/lite:flatbuffer_to_string", + "//tensorflow/compiler/mlir/lite:flatbuffer_translate", + "//tensorflow/compiler/mlir/lite:json_to_flatbuffer", + "//tensorflow/compiler/mlir/lite:litert-opt", + "//tensorflow/compiler/mlir/lite:tf_tfl_translate", + "@llvm-project//llvm:FileCheck", + ], +) + +# Bundle together all the files that are used by the non-mlir file-based tests. +filegroup( + name = "extra_files", + srcs = glob( + [ + "**/importer_test_min_max.cc.mlir", + "**/reshape.mlir", + ], + ), +) + +# A binary to inject min/max to a tflite model. +# A file check command is used to verify the imported result from this +# binary format. +tf_native_cc_binary( + name = "importer_test_min_max", + srcs = [ + "importer_test_min_max.cc", + ], + deps = [ + "//tensorflow/compiler/mlir/lite/core:absl_error_model_builder", + "//tensorflow/compiler/mlir/lite/schema:schema_fbs", + "//tensorflow/compiler/mlir/lite/schema:schema_utils", + "@com_google_absl//absl/strings", + "@llvm-project//llvm:Support", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/basic_lstm.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/basic_lstm.mlir new file mode 100644 index 0000000000000000000000000000000000000000..06b333ed8ddb043ea57ece42e1f98c2c85476f58 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/basic_lstm.mlir @@ -0,0 +1,10 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s +// Ensure basic_lstm roundtrip exactly + +func.func @main(%arg0: tensor<1x384xf32>, %arg1: tensor<1x96xf32>, %arg2: tensor<384x480xf32>, %arg3: tensor<384xf32>, %arg4: tensor<1x96xf32>) -> tensor<1x96xf32> { +// CHECK-LABEL: @main +// CHECK: "tfl.basic_lstm"({{.*}}) <{cell_clip = 1.000000e+00 : f32, fused_activation_function = "RELU", kernel_type = #tfl, proj_clip = 2.000000e+00 : f32}> {asymmetric_quantize_inputs = false} : (tensor<1x384xf32>, tensor<1x96xf32>, tensor<384x480xf32>, tensor<384xf32>, tensor<1x96xf32>) -> (tensor<1x96xf32>, tensor<1x96xf32>, tensor<1x480xf32>, tensor<1x384xf32>) + + %0:4 = "tfl.basic_lstm"(%arg0, %arg1, %arg2, %arg3, %arg4) {fused_activation_function = "RELU", cell_clip = 1.0 : f32, proj_clip = 2.0 : f32} : (tensor<1x384xf32>, tensor<1x96xf32>, tensor<384x480xf32>, tensor<384xf32>, tensor<1x96xf32>) -> (tensor<1x96xf32>, tensor<1x96xf32>, tensor<1x480xf32>, tensor<1x384xf32>) + func.return %0#0 : tensor<1x96xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/batched_gather_round_trip.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/batched_gather_round_trip.mlir new file mode 100644 index 0000000000000000000000000000000000000000..19b7ff81e512f6798f1024462ffafcceec90cd12 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/batched_gather_round_trip.mlir @@ -0,0 +1,35 @@ +// RUN: tf_tfl_translate --enable-hlo-to-tf-conversion --input-mlir %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s --check-prefix=CHECK-ROUNDTRIP + + +module { + // CHECK-LABEL: func.func public @main + func.func public @main(%arg0: tensor<3x2x4x7x9xi32>, %arg1: tensor<4x3x5x2xi32>) -> tensor<4x3x5x8xi32> { + // CHECK-ROUNDTRIP: %0 = "tfl.pseudo_const"() <{value = dense<{{\[\[\[\[}}0]], {{\[\[}}1]], {{\[\[}}2]]]]> : tensor<1x3x1x1xi32>}> : () -> tensor<1x3x1x1xi32> + // CHECK-ROUNDTRIP: %1 = "tfl.pseudo_const"() <{value = dense<[4, 3, 5, 1]> : tensor<4xi64>}> : () -> tensor<4xi64> + // CHECK-ROUNDTRIP: %2 = "tfl.broadcast_to"(%0, %1) : (tensor<1x3x1x1xi32>, tensor<4xi64>) -> tensor<4x3x5x1xi32> + // CHECK-ROUNDTRIP: %3 = "tfl.pseudo_const"() <{value = dense<{{\[\[\[\[}}0]]], {{\[\[\[}}1]]], {{\[\[\[}}2]]], {{\[\[\[}}3]]]]> : tensor<4x1x1x1xi32>}> : () -> tensor<4x1x1x1xi32> + // CHECK-ROUNDTRIP: %4 = "tfl.broadcast_to"(%3, %1) : (tensor<4x1x1x1xi32>, tensor<4xi64>) -> tensor<4x3x5x1xi32> + // CHECK-ROUNDTRIP: %[[concat:.*]] = "tfl.concatenation"(%2, %4, %arg1) <{axis = 3 : i32, fused_activation_function = "NONE"}> : + // CHECK-ROUNDTRIP-SAME: (tensor<4x3x5x1xi32>, tensor<4x3x5x1xi32>, tensor<4x3x5x2xi32>) -> tensor<4x3x5x4xi32> + // CHECK-ROUNDTRIP: %[[gather:.*]] = "stablehlo.gather"(%arg0, %[[concat]]) <{ + // CHECK-ROUNDTRIP-SAME: dimension_numbers = #stablehlo.gather< + // CHECK-ROUNDTRIP-SAME: offset_dims = [3], collapsed_slice_dims = [0, 1, 2, 3], + // CHECK-ROUNDTRIP-SAME: start_index_map = [0, 2, 1, 3], index_vector_dim = 3>, + // CHECK-ROUNDTRIP-SAME: slice_sizes = array}> : + // CHECK-ROUNDTRIP-SAME: (tensor<3x2x4x7x9xi32>, tensor<4x3x5x4xi32>) -> tensor<4x3x5x8xi32> + // CHECK-ROUNDTRIP: return %[[gather]] + %0 = "stablehlo.gather"(%arg0, %arg1) { + dimension_numbers = #stablehlo.gather< + offset_dims = [3], + collapsed_slice_dims = [1, 3], + operand_batching_dims = [0, 2], + start_indices_batching_dims = [1, 0], + start_index_map = [1, 3], + index_vector_dim = 3 + >, + slice_sizes = array, + indices_are_sorted = false + } : (tensor<3x2x4x7x9xi32>, tensor<4x3x5x2xi32>) -> tensor<4x3x5x8xi32> + return %0 : tensor<4x3x5x8xi32> + } +} \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/batched_scatter_round_trip.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/batched_scatter_round_trip.mlir new file mode 100644 index 0000000000000000000000000000000000000000..38569978d02539fcfb8e9c4aca875587ab569597 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/batched_scatter_round_trip.mlir @@ -0,0 +1,37 @@ +// RUN: tf_tfl_translate --enable-hlo-to-tf-conversion --input-mlir %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s --check-prefix=CHECK-ROUNDTRIP + + +module { + // CHECK-LABEL: func.func public @main + func.func public @main(%arg0: tensor<3x2x4x7x9xi32>, %arg1: tensor<4x3x5x2xi32>, %arg2: tensor<4x3x5x8xi32>) -> tensor<3x2x4x7x9xi32> { + // CHECK-ROUNDTRIP: %0 = "tfl.pseudo_const"() <{value = dense<{{\[\[\[\[}}0]], {{\[\[}}1]], {{\[\[}}2]]]]> : tensor<1x3x1x1xi32>}> : () -> tensor<1x3x1x1xi32> + // CHECK-ROUNDTRIP: %1 = "tfl.pseudo_const"() <{value = dense<[4, 3, 5, 1]> : tensor<4xi64>}> : () -> tensor<4xi64> + // CHECK-ROUNDTRIP: %2 = "tfl.broadcast_to"(%0, %1) : (tensor<1x3x1x1xi32>, tensor<4xi64>) -> tensor<4x3x5x1xi32> + // CHECK-ROUNDTRIP: %3 = "tfl.pseudo_const"() <{value = dense<{{\[\[\[\[}}0]]], {{\[\[\[}}1]]], {{\[\[\[}}2]]], {{\[\[\[}}3]]]]> : tensor<4x1x1x1xi32>}> : () -> tensor<4x1x1x1xi32> + // CHECK-ROUNDTRIP: %4 = "tfl.broadcast_to"(%3, %1) : (tensor<4x1x1x1xi32>, tensor<4xi64>) -> tensor<4x3x5x1xi32> + // CHECK-ROUNDTRIP: %[[concat:.*]] = "tfl.concatenation"(%2, %4, %arg1) <{axis = 3 : i32, fused_activation_function = "NONE"}> : + // CHECK-ROUNDTRIP-SAME: (tensor<4x3x5x1xi32>, tensor<4x3x5x1xi32>, tensor<4x3x5x2xi32>) -> tensor<4x3x5x4xi32> + // CHECK-ROUNDTRIP: %[[scatter:.*]] = "stablehlo.scatter"(%arg0, %[[concat]], %arg2) <{ + // CHECK-ROUNDTRIP-SAME: scatter_dimension_numbers = #stablehlo.scatter + // CHECK-ROUNDTRIP-SAME: update_window_dims = [3], inserted_window_dims = [0, 1, 2, 3], + // CHECK-ROUNDTRIP-SAME: scatter_dims_to_operand_dims = [0, 2, 1, 3], index_vector_dim = 3>}> + // CHECK-ROUNDTRIP: (tensor<3x2x4x7x9xi32>, tensor<4x3x5x4xi32>, tensor<4x3x5x8xi32>) -> tensor<3x2x4x7x9xi32> + // CHECK-ROUNDTRIP: return %[[scatter]] + %0 = "stablehlo.scatter"(%arg0, %arg1, %arg2) <{ + indices_are_sorted = false, + scatter_dimension_numbers = #stablehlo.scatter< + update_window_dims = [3], + inserted_window_dims = [1, 3], + input_batching_dims = [0, 2], + scatter_indices_batching_dims = [1, 0], + scatter_dims_to_operand_dims = [1, 3], + index_vector_dim = 3 + >, + unique_indices = false + }> ({ + ^bb0(%arg3: tensor, %arg4: tensor): + stablehlo.return %arg4 : tensor + }) : (tensor<3x2x4x7x9xi32>, tensor<4x3x5x2xi32>, tensor<4x3x5x8xi32>) -> tensor<3x2x4x7x9xi32> + return %0 : tensor<3x2x4x7x9xi32> + } +} \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/bucketize.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/bucketize.mlir new file mode 100644 index 0000000000000000000000000000000000000000..15f41ec0342c2bae502be8f6a202bd1c33e1c95c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/bucketize.mlir @@ -0,0 +1,9 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s +// Ensure bucketize roundtrip exactly + +func.func @main(%arg0: tensor<3x2xf32>) -> tensor<3x2xi32> { + // CHECK-LABEL: @main + // CHECK: "tfl.bucketize"(%arg0) <{boundaries = [0.000000e+00 : f32, 1.000000e+01 : f32, 1.000000e+02 : f32]}> : (tensor<3x2xf32>) -> tensor<3x2xi32> + %0 = "tfl.bucketize"(%arg0) {boundaries = [0.0 : f32, 10.0 : f32, 100.0 : f32]} : (tensor<3x2xf32>) -> tensor<3x2xi32> + func.return %0 : tensor<3x2xi32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/cast_bf16.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/cast_bf16.mlir new file mode 100644 index 0000000000000000000000000000000000000000..7e921f4041d342e3ac6a744f5411f1f237872509 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/cast_bf16.mlir @@ -0,0 +1,12 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s +// Ensure cast with bfloat16 roundtrip exactly + +func.func @main(tensor<4x5xbf16>) -> tensor<4x5xbf16> { +^bb0(%arg0: tensor<4x5xbf16>): + // CHECK-LABEL: @main + // CHECK: (tensor<4x5xbf16>) -> tensor<4x5xf32> + // CHECK-NEXT: (tensor<4x5xf32>) -> tensor<4x5xbf16> + %0 = "tfl.cast" (%arg0) : (tensor<4x5xbf16>) -> tensor<4x5xf32> loc("cast1") + %1 = "tfl.cast" (%0) : (tensor<4x5xf32>) -> tensor<4x5xbf16> loc("cast2") + func.return %1 : tensor<4x5xbf16> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/composite_op_round_trip.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/composite_op_round_trip.mlir new file mode 100644 index 0000000000000000000000000000000000000000..1e70a3a6f9064208076fff3d44c4b59cb1e5ae20 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/composite_op_round_trip.mlir @@ -0,0 +1,25 @@ +// RUN: tf_tfl_translate --enable-hlo-to-tf-conversion --input-mlir %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s --check-prefix=CHECK-ROUNDTRIP + + +module { + func.func public @main( %arg0: tensor) -> tensor { + %0 = func.call @test_add_roundtrip(%arg0) : (tensor) -> tensor + + return %0 : tensor + } + + + // CHECK-LABEL: func.func private @test_add_roundtrip + func.func private @test_add_roundtrip(%arg0: tensor) -> tensor { + // CHECK-ROUNDTRIP{LITERAL}: %0 = stablehlo.composite "stablehlo.add_n" %arg0 {composite_attributes = {test_bool = false, test_f32_tensor = dense<[[0.000000e+00, 1.000000e+00], [2.000000e+00, 3.000000e+00]]> : tensor<2x2xf32>, test_i32_tensor = dense<5> : tensor<1xi32>, test_i64_tensor = dense<[6, 7]> : tensor<2xi64>, test_int = 2 : i64, test_string = "test"}, decomposition = @add_n.impl} : (tensor) -> tensor + %0 = stablehlo.composite "stablehlo.add_n" %arg0 { composite_attributes = { test_int = 2 : i64, test_bool = 0 : i1, test_string = "test", test_i32_tensor = dense<5> : tensor<1xi32>, test_i64_tensor = dense<[6, 7]> : tensor<2xi64>, test_f32_tensor = dense<[[0.0, 1.0],[2.0, 3.0]]> : tensor<2x2xf32>}, decomposition = @add_n.impl } : (tensor) -> tensor + return %0 : tensor + } + func.func private @add_n.impl(%arg0: tensor) -> tensor { + %0 = stablehlo.constant dense<2> : tensor + %1 = stablehlo.add %arg0, %0 : tensor + return %1 : tensor + } + +} + diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/constants.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/constants.mlir new file mode 100644 index 0000000000000000000000000000000000000000..850d2f4ee66349cf392f171dfa48a26df7221188 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/constants.mlir @@ -0,0 +1,148 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s +// Ensure constants roundtrip exactly + +func.func @bool() -> tensor<4xi1> { + // CHECK-LABEL: @bool + // CHECK: value = dense<[false, true, true, false]> : tensor<4xi1> + %0 = "tfl.pseudo_const"() { value = dense<[false, true, true, false]> : tensor<4xi1> } : () -> tensor<4xi1> + func.return %0 : tensor<4xi1> +} + +func.func @complex64() -> tensor<4xcomplex> { + // CHECK-LABEL: @complex64 + // CHECK: value = #tf_type : tensor<4xcomplex> + %0 = "tfl.pseudo_const"() { value = #tf_type : tensor<4xcomplex> } : () -> tensor<4xcomplex> + func.return %0 : tensor<4xcomplex> +} + +func.func @complex128() -> tensor<4xcomplex> { + // CHECK-LABEL: @complex128 + // CHECK: value = #tf_type : tensor<4xcomplex> + %0 = "tfl.pseudo_const"() { value = #tf_type : tensor<4xcomplex> } : () -> tensor<4xcomplex> + func.return %0 : tensor<4xcomplex> +} + +func.func @f16() -> tensor<4xf16> { + // CHECK-LABEL: @f16 + // CHECK: value = dense<[1.000000e+00, 2.000000e+00, 3.000000e+00, 4.000000e+00]> : tensor<4xf16> + %0 = "tfl.pseudo_const"() { value = dense<[1.0, 2.0, 3.0, 4.0]> : tensor<4xf16> } : () -> tensor<4xf16> + func.return %0 : tensor<4xf16> +} + +func.func @f32() -> tensor<4xf32> { + // CHECK-LABEL: @f32 + // CHECK: value = dense<[1.000000e+00, 2.000000e+00, 3.000000e+00, 4.000000e+00]> : tensor<4xf32> + %0 = "tfl.pseudo_const"() { value = dense<[1.0, 2.0, 3.0, 4.0]> : tensor<4xf32> } : () -> tensor<4xf32> + func.return %0 : tensor<4xf32> +} + +func.func @f64() -> tensor<4xf64> { + // CHECK-LABEL: @f64 + // CHECK: value = dense<[1.000000e+00, 2.000000e+00, 3.000000e+00, 4.000000e+00]> : tensor<4xf64> + %0 = "tfl.pseudo_const"() { value = dense<[1.0, 2.0, 3.0, 4.0]> : tensor<4xf64> } : () -> tensor<4xf64> + func.return %0 : tensor<4xf64> +} + +func.func @i8() -> tensor<4xi8> { + // CHECK-LABEL: @i8 + // CHECK: value = dense<[1, 2, 3, 4]> : tensor<4xi8> + %0 = "tfl.pseudo_const" () { value = dense<[1, 2, 3, 4]> : tensor<4xi8> } : () -> tensor<4xi8> + func.return %0 : tensor<4xi8> +} + +func.func @i16() -> tensor<4xi16> { + // CHECK-LABEL: @i16 + // CHECK: value = dense<[1, 2, 3, 258]> : tensor<4xi16> + %0 = "tfl.pseudo_const" () { value = dense<[1, 2, 3, 258]> : tensor<4xi16> } : () -> tensor<4xi16> + func.return %0 : tensor<4xi16> +} + +func.func @i32() -> tensor<4xi32> { + // CHECK-LABEL: @i32 + // CHECK: value = dense<[1, 2, 3, 16909060]> : tensor<4xi32> + // Check bytes come back in the right order + %0 = "tfl.pseudo_const" () { value = dense<[1, 2, 3, 16909060]> : tensor<4xi32> } : () -> tensor<4xi32> + func.return %0 : tensor<4xi32> +} + +func.func @i64() -> tensor<4xi64> { + // CHECK-LABEL: @i64 + // CHECK: value = dense<[1, 2, 3, 72623859790382856]> : tensor<4xi64> + %0 = "tfl.pseudo_const" () { value = dense<[1, 2, 3, 72623859790382856]> : tensor<4xi64> } : () -> tensor<4xi64> + func.return %0 : tensor<4xi64> +} + +func.func @string() -> tensor<2x2x!tf_type.string> { + // CHECK-LABEL: @string + // CHECK: value = dense<{{\[\["1", "12"\], \["123", "1234"\]\]}}> : tensor<2x2x!tf_type.string> + %0 = "tfl.pseudo_const"() { value = dense<[["1", "12"], ["123", "1234"]]> : tensor<2x2x!tf_type.string> } : () -> tensor<2x2x!tf_type.string> + func.return %0 : tensor<2x2x!tf_type.string> +} + +func.func @string_norank() -> tensor { + // CHECK-LABEL: @string_norank + // CHECK: value = dense<"test"> : tensor + %0 = "tfl.pseudo_const"() { value = dense<"test"> : tensor } : () -> tensor + func.return %0 : tensor +} + +func.func @uint8() -> tensor<4xui8> { + // CHECK-LABEL: @uint8 + // CHECK: value = dense<[222, 173, 190, 239]> : tensor<4xui8> + %0 = "tfl.pseudo_const"() {value = dense<[222, 173, 190, 239]> : tensor<4xui8>} : () -> tensor<4xui8> + func.return %0 : tensor<4xui8> +} + +func.func @int4() -> tensor<5xi4> { + // CHECK-LABEL: @int4 + // CHECK: value = dense<[6, 3, -2, -7, -8]> : tensor<5xi4> + %0 = "tfl.pseudo_const"() {value = dense<[6, 3, -2, -7, -8]> : tensor<5xi4>} : () -> tensor<5xi4> + func.return %0 : tensor<5xi4> +} + +func.func @qi32_per_axis() -> tensor<3x3x!quant.uniform> { + // CHECK-LABEL: @qi32_per_axis + // CHECK: <{qtype = tensor<3x3x!quant.uniform>, value = dense<1> : tensor<3x3xi32>}> : () -> tensor<3x3x!quant.uniform> + %0 = "tfl.pseudo_qconst"() { qtype = tensor<3x3x!quant.uniform>, value = dense<1> : tensor<3x3xi32>} : () -> tensor<3x3x!quant.uniform> + func.return %0 : tensor<3x3x!quant.uniform> +} + +func.func @qi32_per_axis_zero() -> tensor<3x3x!quant.uniform> { + // CHECK-LABEL: @qi32_per_axis_zero + // CHECK: <{qtype = tensor<3x3x!quant.uniform>, value = dense<1> : tensor<3x3xi32>}> : () -> tensor<3x3x!quant.uniform> + %0 = "tfl.pseudo_qconst"() { qtype = tensor<3x3x!quant.uniform>, value = dense<1> : tensor<3x3xi32>} : () -> tensor<3x3x!quant.uniform> + func.return %0 : tensor<3x3x!quant.uniform> +} + +func.func @qu8() -> tensor<3x!quant.uniform:f32, 1.0>> { + // CHECK-LABEL: @qu8 + // CHECK: <{qtype = tensor<3x!quant.uniform:f32, 1.000000e+00>>, value = dense<1> : tensor<3xi8>}> : () -> tensor<3x!quant.uniform:f32, 1.000000e+00>> + %0 = "tfl.pseudo_qconst"() { qtype = tensor<3x!quant.uniform:f32, 1.0>>, value = dense<1> : tensor<3xi8>} : () -> tensor<3x!quant.uniform:f32, 1.0>> + func.return %0 : tensor<3x!quant.uniform:f32, 1.0>> +} + +func.func @sparse_f32() -> tensor<3x2xf32> { + // CHECK-LABEL: @sparse_f32 + // CHECK: <{compressed_data = dense<[1.000000e+00, 2.000000e+00, 5.000000e-01, 2.500000e-01, -1.000000e+00, -2.000000e+00, -5.000000e-01, -2.500000e-01]> : tensor<8xf32>, s_param = #tfl.sparsity_parameter, , , >, value = dense<0.000000e+00> : tensor<3x2xf32>}> + %0 = "tfl.pseudo_sparse_const"() {compressed_data = dense<[1.0, 2.0, 0.5, 0.25, -1.0, -2.0, -0.5, -0.25]> : tensor<8xf32>, s_param = #tfl.sparsity_parameter, #tfl.dimension_metadata, #tfl.dimension_metadata, #tfl.dimension_metadata>, value = dense<0.000000e+00> : tensor<3x2xf32>} : () -> tensor<3x2xf32> + func.return %0: tensor<3x2xf32> +} + +func.func @sparse_f16() -> tensor<3x2xf16> { + // CHECK-LABEL: @sparse_f16 + // CHECK: <{compressed_data = dense<[1.000000e+00, 2.000000e+00, 5.000000e-01, 2.500000e-01, -1.000000e+00, -2.000000e+00, -5.000000e-01, -2.500000e-01]> : tensor<8xf16>, s_param = #tfl.sparsity_parameter, , , >, value = dense<0.000000e+00> : tensor<3x2xf16>}> + %0 = "tfl.pseudo_sparse_const"() {compressed_data = dense<[1.0, 2.0, 0.5, 0.25, -1.0, -2.0, -0.5, -0.25]> : tensor<8xf16>, s_param = #tfl.sparsity_parameter, #tfl.dimension_metadata, #tfl.dimension_metadata, #tfl.dimension_metadata>, value = dense<0.000000e+00> : tensor<3x2xf16>} : () -> tensor<3x2xf16> + func.return %0: tensor<3x2xf16> +} + +func.func @sparse_qu8() -> tensor<3x2x!quant.uniform:f32, 1.0>> { + // CHECK-LABEL: @sparse_qu8 + // CHECK: <{compressed_data = dense<[1, 2, 3, 4, -1, -2, -3, -4]> : tensor<8xi8>, qtype = tensor<3x2x!quant.uniform:f32, 1.000000e+00>>, s_param = #tfl.sparsity_parameter, , , >, value = dense<0> : tensor<3x2xi8>}> + %0 = "tfl.pseudo_sparse_qconst"() {compressed_data = dense<[1, 2, 3, 4, -1, -2, -3, -4]> : tensor<8xi8>, qtype = tensor<3x2x!quant.uniform:f32, 1.0>>, s_param = #tfl.sparsity_parameter, #tfl.dimension_metadata, #tfl.dimension_metadata, #tfl.dimension_metadata>, value = dense<42> : tensor<3x2xi8>} : () -> tensor<3x2x!quant.uniform:f32, 1.0>> + func.return %0: tensor<3x2x!quant.uniform:f32, 1.0>> +} + +// Identity function to make the exporter happy +func.func @main(%arg0: tensor<4xi8>) -> tensor<4xi8> { + func.return %arg0 : tensor<4xi8> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/constants_offset.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/constants_offset.mlir new file mode 100644 index 0000000000000000000000000000000000000000..7b598fa4ce8a2fc12f072f94ab79e3b4d105c417 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/constants_offset.mlir @@ -0,0 +1,148 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer --use-buffer-offset %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s +// Ensure constants roundtrip exactly + +func.func @bool() -> tensor<4xi1> { + // CHECK-LABEL: @bool + // CHECK: value = dense<[false, true, true, false]> : tensor<4xi1> + %0 = "tfl.pseudo_const"() { value = dense<[false, true, true, false]> : tensor<4xi1> } : () -> tensor<4xi1> + func.return %0 : tensor<4xi1> +} + +func.func @complex64() -> tensor<4xcomplex> { + // CHECK-LABEL: @complex64 + // CHECK: value = #tf_type : tensor<4xcomplex> + %0 = "tfl.pseudo_const"() { value = #tf_type : tensor<4xcomplex> } : () -> tensor<4xcomplex> + func.return %0 : tensor<4xcomplex> +} + +func.func @complex128() -> tensor<4xcomplex> { + // CHECK-LABEL: @complex128 + // CHECK: value = #tf_type : tensor<4xcomplex> + %0 = "tfl.pseudo_const"() { value = #tf_type : tensor<4xcomplex> } : () -> tensor<4xcomplex> + func.return %0 : tensor<4xcomplex> +} + +func.func @f16() -> tensor<4xf16> { + // CHECK-LABEL: @f16 + // CHECK: value = dense<[1.000000e+00, 2.000000e+00, 3.000000e+00, 4.000000e+00]> : tensor<4xf16> + %0 = "tfl.pseudo_const"() { value = dense<[1.0, 2.0, 3.0, 4.0]> : tensor<4xf16> } : () -> tensor<4xf16> + func.return %0 : tensor<4xf16> +} + +func.func @f32() -> tensor<4xf32> { + // CHECK-LABEL: @f32 + // CHECK: value = dense<[1.000000e+00, 2.000000e+00, 3.000000e+00, 4.000000e+00]> : tensor<4xf32> + %0 = "tfl.pseudo_const"() { value = dense<[1.0, 2.0, 3.0, 4.0]> : tensor<4xf32> } : () -> tensor<4xf32> + func.return %0 : tensor<4xf32> +} + +func.func @f64() -> tensor<4xf64> { + // CHECK-LABEL: @f64 + // CHECK: value = dense<[1.000000e+00, 2.000000e+00, 3.000000e+00, 4.000000e+00]> : tensor<4xf64> + %0 = "tfl.pseudo_const"() { value = dense<[1.0, 2.0, 3.0, 4.0]> : tensor<4xf64> } : () -> tensor<4xf64> + func.return %0 : tensor<4xf64> +} + +func.func @i8() -> tensor<4xi8> { + // CHECK-LABEL: @i8 + // CHECK: value = dense<[1, 2, 3, 4]> : tensor<4xi8> + %0 = "tfl.pseudo_const" () { value = dense<[1, 2, 3, 4]> : tensor<4xi8> } : () -> tensor<4xi8> + func.return %0 : tensor<4xi8> +} + +func.func @i16() -> tensor<4xi16> { + // CHECK-LABEL: @i16 + // CHECK: value = dense<[1, 2, 3, 258]> : tensor<4xi16> + %0 = "tfl.pseudo_const" () { value = dense<[1, 2, 3, 258]> : tensor<4xi16> } : () -> tensor<4xi16> + func.return %0 : tensor<4xi16> +} + +func.func @i32() -> tensor<4xi32> { + // CHECK-LABEL: @i32 + // CHECK: value = dense<[1, 2, 3, 16909060]> : tensor<4xi32> + // Check bytes come back in the right order + %0 = "tfl.pseudo_const" () { value = dense<[1, 2, 3, 16909060]> : tensor<4xi32> } : () -> tensor<4xi32> + func.return %0 : tensor<4xi32> +} + +func.func @i64() -> tensor<4xi64> { + // CHECK-LABEL: @i64 + // CHECK: value = dense<[1, 2, 3, 72623859790382856]> : tensor<4xi64> + %0 = "tfl.pseudo_const" () { value = dense<[1, 2, 3, 72623859790382856]> : tensor<4xi64> } : () -> tensor<4xi64> + func.return %0 : tensor<4xi64> +} + +func.func @string() -> tensor<2x2x!tf_type.string> { + // CHECK-LABEL: @string + // CHECK: value = dense<{{\[\["1", "12"\], \["123", "1234"\]\]}}> : tensor<2x2x!tf_type.string> + %0 = "tfl.pseudo_const"() { value = dense<[["1", "12"], ["123", "1234"]]> : tensor<2x2x!tf_type.string> } : () -> tensor<2x2x!tf_type.string> + func.return %0 : tensor<2x2x!tf_type.string> +} + +func.func @string_norank() -> tensor { + // CHECK-LABEL: @string_norank + // CHECK: value = dense<"test"> : tensor + %0 = "tfl.pseudo_const"() { value = dense<"test"> : tensor } : () -> tensor + func.return %0 : tensor +} + +func.func @uint8() -> tensor<4xui8> { + // CHECK-LABEL: @uint8 + // CHECK: value = dense<[222, 173, 190, 239]> : tensor<4xui8> + %0 = "tfl.pseudo_const"() {value = dense<[222, 173, 190, 239]> : tensor<4xui8>} : () -> tensor<4xui8> + func.return %0 : tensor<4xui8> +} + +func.func @int4() -> tensor<5xi4> { + // CHECK-LABEL: @int4 + // CHECK: value = dense<[6, 3, -2, -7, -8]> : tensor<5xi4> + %0 = "tfl.pseudo_const"() {value = dense<[6, 3, -2, -7, -8]> : tensor<5xi4>} : () -> tensor<5xi4> + func.return %0 : tensor<5xi4> +} + +func.func @qi32_per_axis() -> tensor<3x3x!quant.uniform> { + // CHECK-LABEL: @qi32_per_axis + // CHECK: <{qtype = tensor<3x3x!quant.uniform>, value = dense<1> : tensor<3x3xi32>}> : () -> tensor<3x3x!quant.uniform> + %0 = "tfl.pseudo_qconst"() { qtype = tensor<3x3x!quant.uniform>, value = dense<1> : tensor<3x3xi32>} : () -> tensor<3x3x!quant.uniform> + func.return %0 : tensor<3x3x!quant.uniform> +} + +func.func @qi32_per_axis_zero() -> tensor<3x3x!quant.uniform> { + // CHECK-LABEL: @qi32_per_axis_zero + // CHECK: <{qtype = tensor<3x3x!quant.uniform>, value = dense<1> : tensor<3x3xi32>}> : () -> tensor<3x3x!quant.uniform> + %0 = "tfl.pseudo_qconst"() { qtype = tensor<3x3x!quant.uniform>, value = dense<1> : tensor<3x3xi32>} : () -> tensor<3x3x!quant.uniform> + func.return %0 : tensor<3x3x!quant.uniform> +} + +func.func @qu8() -> tensor<3x!quant.uniform:f32, 1.0>> { + // CHECK-LABEL: @qu8 + // CHECK: <{qtype = tensor<3x!quant.uniform:f32, 1.000000e+00>>, value = dense<1> : tensor<3xi8>}> : () -> tensor<3x!quant.uniform:f32, 1.000000e+00>> + %0 = "tfl.pseudo_qconst"() { qtype = tensor<3x!quant.uniform:f32, 1.0>>, value = dense<1> : tensor<3xi8>} : () -> tensor<3x!quant.uniform:f32, 1.0>> + func.return %0 : tensor<3x!quant.uniform:f32, 1.0>> +} + +func.func @sparse_f32() -> tensor<3x2xf32> { + // CHECK-LABEL: @sparse_f32 + // CHECK: <{compressed_data = dense<[1.000000e+00, 2.000000e+00, 5.000000e-01, 2.500000e-01, -1.000000e+00, -2.000000e+00, -5.000000e-01, -2.500000e-01]> : tensor<8xf32>, s_param = #tfl.sparsity_parameter, , , >, value = dense<0.000000e+00> : tensor<3x2xf32>}> + %0 = "tfl.pseudo_sparse_const"() {compressed_data = dense<[1.0, 2.0, 0.5, 0.25, -1.0, -2.0, -0.5, -0.25]> : tensor<8xf32>, s_param = #tfl.sparsity_parameter, #tfl.dimension_metadata, #tfl.dimension_metadata, #tfl.dimension_metadata>, value = dense<0.000000e+00> : tensor<3x2xf32>} : () -> tensor<3x2xf32> + func.return %0: tensor<3x2xf32> +} + +func.func @sparse_f16() -> tensor<3x2xf16> { + // CHECK-LABEL: @sparse_f16 + // CHECK: <{compressed_data = dense<[1.000000e+00, 2.000000e+00, 5.000000e-01, 2.500000e-01, -1.000000e+00, -2.000000e+00, -5.000000e-01, -2.500000e-01]> : tensor<8xf16>, s_param = #tfl.sparsity_parameter, , , >, value = dense<0.000000e+00> : tensor<3x2xf16>}> + %0 = "tfl.pseudo_sparse_const"() {compressed_data = dense<[1.0, 2.0, 0.5, 0.25, -1.0, -2.0, -0.5, -0.25]> : tensor<8xf16>, s_param = #tfl.sparsity_parameter, #tfl.dimension_metadata, #tfl.dimension_metadata, #tfl.dimension_metadata>, value = dense<0.000000e+00> : tensor<3x2xf16>} : () -> tensor<3x2xf16> + func.return %0: tensor<3x2xf16> +} + +func.func @sparse_qu8() -> tensor<3x2x!quant.uniform:f32, 1.0>> { + // CHECK-LABEL: @sparse_qu8 + // CHECK: <{compressed_data = dense<[1, 2, 3, 4, -1, -2, -3, -4]> : tensor<8xi8>, qtype = tensor<3x2x!quant.uniform:f32, 1.000000e+00>>, s_param = #tfl.sparsity_parameter, , , >, value = dense<0> : tensor<3x2xi8>}> + %0 = "tfl.pseudo_sparse_qconst"() {compressed_data = dense<[1, 2, 3, 4, -1, -2, -3, -4]> : tensor<8xi8>, qtype = tensor<3x2x!quant.uniform:f32, 1.0>>, s_param = #tfl.sparsity_parameter, #tfl.dimension_metadata, #tfl.dimension_metadata, #tfl.dimension_metadata>, value = dense<42> : tensor<3x2xi8>} : () -> tensor<3x2x!quant.uniform:f32, 1.0>> + func.return %0: tensor<3x2x!quant.uniform:f32, 1.0>> +} + +// Identity function to make the exporter happy +func.func @main(%arg0: tensor<4xi8>) -> tensor<4xi8> { + func.return %arg0 : tensor<4xi8> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/control_edges.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/control_edges.mlir new file mode 100644 index 0000000000000000000000000000000000000000..5dd532b440700e075f296e3d6b73823d60573aa9 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/control_edges.mlir @@ -0,0 +1,61 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s + +// We need a "main" function, or the flatbuffer exporter won't export anything. +// CHECK-LABEL: @main +func.func @main(%arg0: tensor<1xf32>) -> tensor<1xf32> { + func.return %arg0: tensor<1xf32> +} +// CHECK-NEXT: return + +// CHECK-LABEL: @unneeded_control_node_gets_deleted +func.func @unneeded_control_node_gets_deleted(%arg0: tensor<1xf32>)->tensor<1xf32> { + %tmp0, %ctl0 = tfl.control_node controls "tfl.neg"(%arg0): (tensor<1xf32>) -> tensor<1xf32> + func.return %tmp0: tensor<1xf32> +} +// CHECK-NEXT: %[[tmp:.*]] = "tfl.neg"(%arg0) +// CHECK-NEXT: return %[[tmp]] + +// CHECK-LABEL: @long_chain +func.func @long_chain(%arg0: tensor<1xf32>)->tensor<1xf32> { + %tmp0, %ctl0 = tfl.control_node controls "tfl.neg"(%arg0): (tensor<1xf32>) -> tensor<1xf32> + %tmp1, %ctl1 = tfl.control_node(%ctl0) controls "tfl.neg"(%tmp0): (tensor<1xf32>) -> tensor<1xf32> + %tmp2, %ctl2 = tfl.control_node(%ctl1) controls "tfl.neg"(%tmp1): (tensor<1xf32>) -> tensor<1xf32> + %tmp3, %ctl3 = tfl.control_node(%ctl2) controls "tfl.neg"(%tmp2): (tensor<1xf32>) -> tensor<1xf32> + func.return %tmp3: tensor<1xf32> +} +// CHECK-NEXT: %[[t0:.*]], %[[c0:.*]] = tfl.control_node controls "tfl.neg"(%arg0) +// CHECK-NEXT: %[[t1:.*]], %[[c1:.*]] = tfl.control_node(%[[c0]]) controls "tfl.neg"(%[[t0]]) +// CHECK-NEXT: %[[t2:.*]], %[[c2:.*]] = tfl.control_node(%[[c1]]) controls "tfl.neg"(%[[t1]]) +// CHECK-NEXT: %[[t3:.*]], %[[c3:.*]] = tfl.control_node(%[[c2]]) controls "tfl.neg"(%[[t2]]) +// CHECK-NEXT: return %[[t3]] + + +// CHECK-LABEL: @overlapping_chains +func.func @overlapping_chains(%arg0: tensor<1xf32>)->tensor<1xf32> { + %tmp0, %ctl0 = tfl.control_node controls "tfl.neg"(%arg0): (tensor<1xf32>) -> tensor<1xf32> + %tmp1, %ctl1 = tfl.control_node controls "tfl.neg"(%tmp0): (tensor<1xf32>) -> tensor<1xf32> + %tmp2, %ctl2 = tfl.control_node(%ctl0) controls "tfl.neg"(%tmp1): (tensor<1xf32>) -> tensor<1xf32> + %tmp3, %ctl3 = tfl.control_node(%ctl1) controls "tfl.neg"(%tmp2): (tensor<1xf32>) -> tensor<1xf32> + func.return %tmp3: tensor<1xf32> +} +// CHECK-NEXT: %[[t0:.*]], %[[c0:.*]] = tfl.control_node controls "tfl.neg"(%arg0) +// CHECK-NEXT: %[[t1:.*]], %[[c1:.*]] = tfl.control_node controls "tfl.neg"(%[[t0]]) +// CHECK-NEXT: %[[t2:.*]], %[[c2:.*]] = tfl.control_node(%[[c0]]) controls "tfl.neg"(%[[t1]]) +// CHECK-NEXT: %[[t3:.*]], %[[c3:.*]] = tfl.control_node(%[[c1]]) controls "tfl.neg"(%[[t2]]) +// CHECK-NEXT: return %[[t3]] + +// CHECK-LABEL: @multiple_node_args +func.func @multiple_node_args(%arg0: tensor<1xf32>)->tensor<1xf32> { + %tmp0, %ctl0 = tfl.control_node controls "tfl.neg"(%arg0): (tensor<1xf32>) -> tensor<1xf32> + %tmp1, %ctl1 = tfl.control_node controls "tfl.neg"(%tmp0): (tensor<1xf32>) -> tensor<1xf32> + %tmp2, %ctl2 = tfl.control_node controls "tfl.neg"(%tmp1): (tensor<1xf32>) -> tensor<1xf32> + %tmp3, %ctl3 = tfl.control_node controls "tfl.neg"(%tmp2): (tensor<1xf32>) -> tensor<1xf32> + %tmp4, %ctl4 = tfl.control_node(%ctl2, %ctl0, %ctl3, %ctl2, %ctl1) controls "tfl.neg"(%tmp3): (tensor<1xf32>) -> tensor<1xf32> + func.return %tmp4: tensor<1xf32> +} +// CHECK-NEXT: %[[t0:.*]], %[[c0:.*]] = tfl.control_node controls "tfl.neg"(%arg0) +// CHECK-NEXT: %[[t1:.*]], %[[c1:.*]] = tfl.control_node controls "tfl.neg"(%[[t0]]) +// CHECK-NEXT: %[[t2:.*]], %[[c2:.*]] = tfl.control_node controls "tfl.neg"(%[[t1]]) +// CHECK-NEXT: %[[t3:.*]], %[[c3:.*]] = tfl.control_node controls "tfl.neg"(%[[t2]]) +// CHECK-NEXT: %[[t4:.*]], %[[c4:.*]] = tfl.control_node(%[[c0]], %[[c1]], %[[c2]], %[[c3]]) controls "tfl.neg"(%[[t3]]) +// CHECK-NEXT: return %[[t4]] diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/custom_op.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/custom_op.mlir new file mode 100644 index 0000000000000000000000000000000000000000..6cf03c1b455a5af25b3eeeaf71b60e9d803eb423 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/custom_op.mlir @@ -0,0 +1,16 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s + +func.func @main(%arg0: tensor<32x4x4x128xf32>, %arg1: tensor<1x32x42x128xf32>, %arg2: tensor<4xi32>) -> tensor<1x64x84x32xf32> { + %0 = "tfl.custom"(%arg0, %arg1, %arg2) {custom_code = "Convolution2DTransposeBias", custom_option = #tfl} : (tensor<32x4x4x128xf32>, tensor<1x32x42x128xf32>, tensor<4xi32>) -> tensor<1x64x84x32xf32> + func.return %0 : tensor<1x64x84x32xf32> +} +// CHECK-LABEL: main +// CHECK: "tfl.custom"(%arg0, %arg1, %arg2) <{custom_code = "Convolution2DTransposeBias", custom_option = #tfl}> : (tensor<32x4x4x128xf32>, tensor<1x32x42x128xf32>, tensor<4xi32>) -> tensor<1x64x84x32xf32> + +func.func @main_non_hex_bytes(%arg0: tensor<32x4x4x128xf32>, %arg1: tensor<1x32x42x128xf32>, %arg2: tensor<4xi32>) -> tensor<1x64x84x32xf32> { + %0 = "tfl.custom"(%arg0, %arg1, %arg2) {custom_code = "Convolution2DTransposeBias", custom_option = #tfl} : (tensor<32x4x4x128xf32>, tensor<1x32x42x128xf32>, tensor<4xi32>) -> tensor<1x64x84x32xf32> + func.return %0 : tensor<1x64x84x32xf32> +} +// CHECK-LABEL: main_non_hex_bytes +// Hex representation below determined by the following command: echo -n "this is a string" | od -An -t x1 | tr -d ' ' | tr '[:lower:]' '[:upper:'] +// CHECK: "tfl.custom"(%arg0, %arg1, %arg2) <{custom_code = "Convolution2DTransposeBias", custom_option = #tfl}> : (tensor<32x4x4x128xf32>, tensor<1x32x42x128xf32>, tensor<4xi32>) -> tensor<1x64x84x32xf32> diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/custom_op_offset.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/custom_op_offset.mlir new file mode 100644 index 0000000000000000000000000000000000000000..329791426c7f7b78c1870033e55df5a738d990ed --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/custom_op_offset.mlir @@ -0,0 +1,8 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer --use-buffer-offset %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s + +func.func @main(%arg0: tensor<32x4x4x128xf32>, %arg1: tensor<1x32x42x128xf32>, %arg2: tensor<4xi32>) -> tensor<1x64x84x32xf32> { + %0 = "tfl.custom"(%arg0, %arg1, %arg2) {custom_code = "Convolution2DTransposeBias", custom_option = #tfl} : (tensor<32x4x4x128xf32>, tensor<1x32x42x128xf32>, tensor<4xi32>) -> tensor<1x64x84x32xf32> + func.return %0 : tensor<1x64x84x32xf32> +} +// CHECK-LABEL: main +// CHECK: "tfl.custom"(%arg0, %arg1, %arg2) <{custom_code = "Convolution2DTransposeBias", custom_option = #tfl}> : (tensor<32x4x4x128xf32>, tensor<1x32x42x128xf32>, tensor<4xi32>) -> tensor<1x64x84x32xf32> diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/debug_metadata.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/debug_metadata.mlir new file mode 100644 index 0000000000000000000000000000000000000000..5c94f1bb29369a6cba40dfb75bedd2aa3c5f1337 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/debug_metadata.mlir @@ -0,0 +1,52 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer --serialize-debug-metadata=true %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir --mlir-print-debuginfo -o - | FileCheck %s +// This test verifies that debug locations are round-trippable. + +#loc = loc("":0:0) +#loc5 = loc("main"(#loc)) +#loc8 = loc("cond_true"(#loc)) +#loc10 = loc("cond_false"(#loc)) +module @jit_relu attributes {jax.uses_shape_polymorphism = false, mhlo.num_partitions = 1 : i32, mhlo.num_replicas = 1 : i32, tfl._legalize_tfl_variables = true} { + func.func @main(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>) -> tensor<1xf32> { + %0 = tfl.less(%arg0, %arg1) : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xi1> loc(#loc6) + // CHECK-DAG: {{.*}} = tfl.less(%arg0, %arg1) : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xi1> loc(#loc6) + %1 = "tf.If"(%0, %arg0, %arg1) <{else_branch = @cond_false, is_stateless = false, then_branch = @cond_true}> : (tensor<1xi1>, tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> loc(#loc7) + // CHECK-DAG: {{.*}} = "tf.If"(%0, %arg0, %arg1) {{.*}} -> tensor<1xf32> loc(#loc7) + func.return %1 : tensor<1xf32> loc(#loc) + } loc(#loc5) + func.func @cond_true(%arg0: tensor<*xf32>, %arg1: tensor<*xf32>) -> tensor<*xf32> { + %0 = tfl.add %arg0, %arg1 {fused_activation_function = "NONE"} : tensor<*xf32> loc(#loc16) + // CHECK-DAG: {{.*}} = tfl.add %arg0, %arg1 {fused_activation_function = "NONE"} : tensor<*xf32> loc(#loc16) + func.return %0 : tensor<*xf32> loc(#loc) + } loc(#loc8) + func.func @cond_false(%arg0: tensor<*xf32>, %arg1: tensor<*xf32>) -> tensor<*xf32> { + %0 = tfl.mul %arg0, %arg1 {fused_activation_function = "NONE"} : tensor<*xf32> loc(#loc15) + // CHECK-DAG: {{.*}} = tfl.mul %arg0, %arg1 {fused_activation_function = "NONE"} : tensor<*xf32> loc(#loc15) + func.return %0 : tensor<*xf32> loc(#loc) + } loc(#loc10) +} loc(#loc) +#loc1 = loc("tfl.less") +// CHECK-DAG: #loc1 = loc("tfl.less") +#loc2 = loc("tf.If") +// CHECK-DAG: #loc2 = loc("tf.If") +#loc3 = loc("":1:4) +// CHECK-DAG: #loc3 = loc("":1:4) +#loc4 = loc("third_party/py/IPython/v3_2_3/core/interactiveshell.py":3066:16) +// CHECK-DAG: #loc4 = loc("third_party/py/IPython/v3_2_3/core/interactiveshell.py":3066:16) +#loc6 = loc(fused<"tflite.importer_wrapper">[#loc1]) +// CHECK-DAG: #loc6 = loc(fused<"tflite.importer_wrapper">[#loc1]) +#loc7 = loc(fused<"tflite.importer_wrapper">[#loc2]) +// CHECK-DAG: #loc7 = loc(fused<"tflite.importer_wrapper">[#loc2]) +#loc9 = loc(callsite(#loc3 at #loc4)) +// CHECK-DAG: #loc9 = loc(callsite(#loc3 at #loc4)) +#loc11 = loc(fused<"">[#loc3, #loc4]) +// CHECK-DAG: #loc11 = loc(fused<"">[#loc3, #loc4]) +#loc12 = loc("jit(relu)/jit(main)/max"(#loc9)) +// CHECK-DAG: #loc12 = loc("jit(relu)/jit(main)/max"(#loc9)) +#loc13 = loc("tfl.mul"(#loc11)) +// CHECK-DAG: #loc13 = loc("tfl.mul"(#loc11)) +#loc14 = loc("jit(relu)/jit(main)/max"(#loc12)) +// CHECK-DAG: #loc14 = loc("jit(relu)/jit(main)/max"(#loc12)) +#loc15 = loc(fused<"tflite.importer_wrapper">[#loc13]) +// CHECK-DAG: #loc15 = loc(fused<"tflite.importer_wrapper">[#loc13]) +#loc16 = loc(fused<"tflite.importer_wrapper">[#loc14]) +// CHECK-DAG: #loc16 = loc(fused<"tflite.importer_wrapper">[#loc14]) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/dense_constants.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/dense_constants.mlir new file mode 100644 index 0000000000000000000000000000000000000000..002f78ef835901813546249e119436bd2f80f942 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/dense_constants.mlir @@ -0,0 +1,55 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s +// Ensure constants roundtrip exactly + +func.func @f32() -> tensor<4xf32> { + // CHECK-LABEL: @f32 + // CHECK: value = dense<[1.000000e+00, 2.000000e+00, 3.000000e+00, 4.000000e+00]> : tensor<4xf32> + %0 = "tfl.pseudo_const"() { value = dense_resource : tensor<4xf32> } : () -> tensor<4xf32> + func.return %0 : tensor<4xf32> +} + +func.func @i8() -> tensor<4xi8> { + // CHECK-LABEL: @i8 + // CHECK: value = dense<[1, 2, 3, 4]> : tensor<4xi8> + %0 = "tfl.pseudo_const" () { value = dense_resource : tensor<4xi8> } : () -> tensor<4xi8> + func.return %0 : tensor<4xi8> +} + +func.func @i16() -> tensor<4xi16> { + // CHECK-LABEL: @i16 + // CHECK: value = dense<[1, 2, 3, 258]> : tensor<4xi16> + %0 = "tfl.pseudo_const" () { value = dense_resource : tensor<4xi16> } : () -> tensor<4xi16> + func.return %0 : tensor<4xi16> +} + +func.func @i32() -> tensor<4xi32> { + // CHECK-LABEL: @i32 + // CHECK: value = dense<[1, 2, 3, 16909060]> : tensor<4xi32> + // Check bytes come back in the right order + %0 = "tfl.pseudo_const" () { value = dense_resource : tensor<4xi32> } : () -> tensor<4xi32> + func.return %0 : tensor<4xi32> +} + +func.func @uint8() -> tensor<4xui8> { + // CHECK-LABEL: @uint8 + // CHECK: value = dense<[222, 173, 190, 239]> : tensor<4xui8> + %0 = "tfl.pseudo_const"() {value = dense_resource : tensor<4xui8>} : () -> tensor<4xui8> + func.return %0 : tensor<4xui8> +} + +// Identity function to make the exporter happy +func.func @main(%arg0: tensor<4xi8>) -> tensor<4xi8> { + func.return %arg0 : tensor<4xi8> +} + +{-# + dialect_resources: { + builtin: { + dense_elements_f32: "0x400000000000803F000000400000404000008040", + dense_elements_i16: "0x400000000100020003000201", + dense_elements_i32: "0x4000000001000000020000000300000004030201", + dense_elements_i8: "0x4000000001020304", + dense_elements_i8_1: "0x40000000DEADBEEF" + } + } +#-} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/dense_constants_offset.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/dense_constants_offset.mlir new file mode 100644 index 0000000000000000000000000000000000000000..59020b2d3354816f149f0953c82a3f4dd2eba379 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/dense_constants_offset.mlir @@ -0,0 +1,55 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer --use-buffer-offset %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s +// Ensure constants roundtrip exactly + +func.func @f32() -> tensor<4xf32> { + // CHECK-LABEL: @f32 + // CHECK: value = dense<[1.000000e+00, 2.000000e+00, 3.000000e+00, 4.000000e+00]> : tensor<4xf32> + %0 = "tfl.pseudo_const"() { value = dense_resource : tensor<4xf32> } : () -> tensor<4xf32> + func.return %0 : tensor<4xf32> +} + +func.func @i8() -> tensor<4xi8> { + // CHECK-LABEL: @i8 + // CHECK: value = dense<[1, 2, 3, 4]> : tensor<4xi8> + %0 = "tfl.pseudo_const" () { value = dense_resource : tensor<4xi8> } : () -> tensor<4xi8> + func.return %0 : tensor<4xi8> +} + +func.func @i16() -> tensor<4xi16> { + // CHECK-LABEL: @i16 + // CHECK: value = dense<[1, 2, 3, 258]> : tensor<4xi16> + %0 = "tfl.pseudo_const" () { value = dense_resource : tensor<4xi16> } : () -> tensor<4xi16> + func.return %0 : tensor<4xi16> +} + +func.func @i32() -> tensor<4xi32> { + // CHECK-LABEL: @i32 + // CHECK: value = dense<[1, 2, 3, 16909060]> : tensor<4xi32> + // Check bytes come back in the right order + %0 = "tfl.pseudo_const" () { value = dense_resource : tensor<4xi32> } : () -> tensor<4xi32> + func.return %0 : tensor<4xi32> +} + +func.func @uint8() -> tensor<4xui8> { + // CHECK-LABEL: @uint8 + // CHECK: value = dense<[222, 173, 190, 239]> : tensor<4xui8> + %0 = "tfl.pseudo_const"() {value = dense_resource : tensor<4xui8>} : () -> tensor<4xui8> + func.return %0 : tensor<4xui8> +} + +// Identity function to make the exporter happy +func.func @main(%arg0: tensor<4xi8>) -> tensor<4xi8> { + func.return %arg0 : tensor<4xi8> +} + +{-# + dialect_resources: { + builtin: { + dense_elements_f32: "0x400000000000803F000000400000404000008040", + dense_elements_i16: "0x400000000100020003000201", + dense_elements_i32: "0x4000000001000000020000000300000004030201", + dense_elements_i8: "0x4000000001020304", + dense_elements_i8_1: "0x40000000DEADBEEF" + } + } +#-} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/dynamic_shape.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/dynamic_shape.mlir new file mode 100644 index 0000000000000000000000000000000000000000..57778c8d27a5d6f32deba8baf828ce62ed863ac2 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/dynamic_shape.mlir @@ -0,0 +1,9 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s + +// CHECK: func @main(%arg0: tensor) -> tensor +func.func @main(%arg0: tensor) -> tensor { + %cst = arith.constant dense<1.0> : tensor<4xf32> + %cst_3 = arith.constant dense<2.0> : tensor<4x3x3x3xf32> + %0 = "tfl.conv_2d"(%arg0, %cst_3, %cst) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "RELU6", padding = "VALID", stride_h = 2 : i32, stride_w = 2 : i32} : (tensor, tensor<4x3x3x3xf32>, tensor<4xf32>) -> tensor + func.return %0 : tensor +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/empty_input_output_names.json b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/empty_input_output_names.json new file mode 100644 index 0000000000000000000000000000000000000000..dbe260019980b076edbc3208c86d6d6658f4b339 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/empty_input_output_names.json @@ -0,0 +1,81 @@ +// RUN: json_to_flatbuffer %p/test_schema.fbs %s | flatbuffer_translate --tflite-flatbuffer-to-mlir -o - | FileCheck %s + +// If input and output tensors don't have names, there shouldn't be an +// `tf.entry_function` attribute created. +// CHECK-NOT: tf.entry_function + +{ + "version": 3, + "operator_codes": [ + { + "builtin_code": "CONV_2D" + } + ], + "subgraphs": [ + { + "tensors": [ + { + "shape": [ + 256, + 32, + 32, + 3 + ], + "quantization": { + } + }, + { + "shape": [ + 16, + 3, + 3, + 3 + ], + "quantization": { + } + }, + { + "shape": [ + 0 + ], + }, + { + "shape": [ + 256, + 32, + 32, + 16 + ], + "quantization": { + } + } + ], + "inputs": [ + 0, + 1 + ], + "outputs": [ + 3 + ], + "operators": [ + { + "inputs": [ + 0, + 1, + -1 + ], + "outputs": [ + 3 + ], + "builtin_options_type": "Conv2DOptions", + "builtin_options": { + "stride_w": 1, + "stride_h": 1 + } + } + ], + "name": "main" + } + ], + "description": "MLIR Converted." +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/external_buffer.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/external_buffer.mlir new file mode 100644 index 0000000000000000000000000000000000000000..bd478d5cdc20cf659c4bc4149b48e61d08fbce14 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/external_buffer.mlir @@ -0,0 +1,14 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s + +module { + func.func public @main(%arg0: tensor<2x2xf32>) -> tensor<2x2xf32> { + %0 = "tfl.external_const"() <{external_buffer = #tfl.external_buffer}> : () -> tensor<2x2xf32> + %1 = tfl.add %arg0, %0 {fused_activation_function = "NONE"} : tensor<2x2xf32> + return %1 : tensor<2x2xf32> + } +} + +// CHECK-LABEL: @main +// CHECK: %0 = "tfl.external_const"() <{external_buffer = #tfl.external_buffer}> +// CHECK-NEXT: %1 = tfl.add %arg0, %0 {fused_activation_function = "NONE"} : tensor<2x2xf32> +// CHECK-NEXT: return %1 diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/external_constant.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/external_constant.mlir new file mode 100644 index 0000000000000000000000000000000000000000..c7fa8e0b944dfdd704e40a971c132fa5fd2a06a3 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/external_constant.mlir @@ -0,0 +1,14 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir --use-external-constant - -o - | FileCheck %s +// Ensure that `tfl.external_const` is imported when the flag `-use-external-constant` is enabled. + +func.func @main(tensor<40x37xf32>, tensor<40x37xf32>) -> tensor<40x40xf32> { +^bb0(%arg0: tensor<40x37xf32>, %arg1: tensor<40x37xf32>): + %cst = arith.constant dense<1.0> : tensor<40xf32> + %0:2 = "tfl.fully_connected"(%arg0, %arg1, %cst) {fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"} : (tensor<40x37xf32>, tensor<40x37xf32>, tensor<40xf32>) -> (tensor<40x40xf32>, tensor<40x40xf32>) + func.return %0 : tensor<40x40xf32> + +// CHECK-LABEL: func @main(%arg0: tensor<40x37xf32>, %arg1: tensor<40x37xf32>) -> tensor<40x40xf32> +// CHECK: %[[CONST:[0-9]+]] = "tfl.external_const"() <{buffer_index = 3 : i32}> : () -> tensor<40xf32> +// CHECK-NEXT: %[[FULL:[0-9]+]]:2 = "tfl.fully_connected"(%arg0, %arg1, %[[CONST]]) <{asymmetric_quantize_inputs = false, fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"}> +// CHECK-NEXT: return %[[FULL]]#0 +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/if_op.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/if_op.mlir new file mode 100644 index 0000000000000000000000000000000000000000..95f83133423c7ec9fd1096d2543f90d66201471f --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/if_op.mlir @@ -0,0 +1,65 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s +// Confirm function references in if ops are preserved +func.func @main(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>) -> tensor<1xf32> { +// CHECK: %{{.*}} = "tf.If"(%{{.*}}, %{{.*}}, %{{.*}}) <{else_branch = @cond_false, is_stateless = false, then_branch = @cond_true}> : (tensor<1xi1>, tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + %0 = "tfl.less"(%arg0, %arg1) : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xi1> + %1 = "tf.If"(%0, %arg0, %arg1) {else_branch = @cond_false, then_branch = @cond_true, is_stateless = false} : (tensor<1xi1>, tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + func.return %1 : tensor<1xf32> +} + +func.func @cond_true(%arg0: tensor<*xf32>, %arg1: tensor<*xf32>) -> tensor<*xf32> { + %0 = tfl.add %arg0, %arg1 {fused_activation_function = "NONE"} : tensor<*xf32> + func.return %0 : tensor<*xf32> +} + +func.func @cond_false(%arg0: tensor<*xf32>, %arg1: tensor<*xf32>) -> tensor<*xf32> { + %0 = tfl.mul %arg0, %arg1 {fused_activation_function = "NONE"} : tensor<*xf32> + func.return %0 : tensor<*xf32> +} + +// ----- + +func.func @tfl_if(%arg0: tensor) -> tensor { +// CHECK: %{{.*}} = "tf.If"(%{{.*}}, %{{.*}}) <{else_branch = @tfl.if_else, is_stateless = false, then_branch = @tfl.if_then}> : (tensor, tensor) -> tensor + %cst = arith.constant dense<0> : tensor + %0 = tfl.add %cst, %cst {fused_activation_function = "NONE"} : tensor + %1 = "tfl.if"(%arg0) ({ + %2 = func.call @tfl.if_then(%0) : (tensor) -> tensor + "tfl.yield"(%2) : (tensor) -> () + }, { + %2 = func.call @tfl.if_else(%0) : (tensor) -> tensor + "tfl.yield"(%2) : (tensor) -> () + }) : (tensor) -> tensor + return %1 : tensor +} +func.func private @tfl.if_then(%arg0: tensor) -> tensor { + return %arg0 : tensor +} +func.func private @tfl.if_else(%arg0: tensor) -> tensor { + %0 = tfl.mul %arg0, %arg0 {fused_activation_function = "NONE"} : tensor + return %0 : tensor +} + +// ----- + +func.func @tfl_if_multi_args(%arg0: tensor) -> tensor { +// CHECK: %{{.*}} = "tf.If"(%{{.*}}, %{{.*}}, %{{.*}}) <{else_branch = @tfl.if_else_1, is_stateless = false, then_branch = @tfl.if_then_1}> : (tensor, tensor, tensor) -> tensor + %cst = arith.constant dense<0> : tensor + %0 = tfl.add %cst, %cst {fused_activation_function = "NONE"} : tensor + %1 = tfl.mul %cst, %cst {fused_activation_function = "NONE"} : tensor + %2 = "tfl.if"(%arg0) ({ + %2 = func.call @tfl.if_then_1(%0, %1) : (tensor, tensor) -> tensor + "tfl.yield"(%2) : (tensor) -> () + }, { + %2 = func.call @tfl.if_else_1(%0, %1) : (tensor, tensor) -> tensor + "tfl.yield"(%2) : (tensor) -> () + }) : (tensor) -> tensor + return %1 : tensor +} +func.func private @tfl.if_then_1(%arg0: tensor, %arg1: tensor) -> tensor { + return %arg0 : tensor +} +func.func private @tfl.if_else_1(%arg0: tensor, %arg1: tensor) -> tensor { + %0 = tfl.mul %arg0, %arg1 {fused_activation_function = "NONE"} : tensor + return %0 : tensor +} \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/import_json.json b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/import_json.json new file mode 100644 index 0000000000000000000000000000000000000000..65e15785ff7a4889e6092ea647ad96cf9b5d8fe8 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/import_json.json @@ -0,0 +1,85 @@ +// RUN: json_to_flatbuffer %p/test_schema.fbs %s | flatbuffer_translate --tflite-flatbuffer-to-mlir -o - | FileCheck %s + +// CHECK: %[[CST:.*]] = "tfl.no_value"() <{value}> : () -> none +// CHECK: %[[RES0:.*]] = "tfl.conv_2d"(%arg0, %arg1, %[[CST]]) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<256x32x32x3xf32>, tensor<16x3x3x3xf32>, none) -> tensor<256x32x32x16xf32> +// CHECK: return %[[RES0]] : tensor<256x32x32x16xf32> + +{ + "version": 3, + "operator_codes": [ + { + "builtin_code": "CONV_2D" + } + ], + "subgraphs": [ + { + "tensors": [ + { + "shape": [ + 256, + 32, + 32, + 3 + ], + "name": "arg0", + "quantization": { + } + }, + { + "shape": [ + 16, + 3, + 3, + 3 + ], + "name": "arg1", + "quantization": { + } + }, + { + "shape": [ + 0 + ], + "name": "cst" + }, + { + "shape": [ + 256, + 32, + 32, + 16 + ], + "name": "output", + "quantization": { + } + } + ], + "inputs": [ + 0, + 1 + ], + "outputs": [ + 3 + ], + "operators": [ + { + "inputs": [ + 0, + 1, + -1 + ], + "outputs": [ + 3 + ], + "builtin_options_type": "Conv2DOptions", + "builtin_options": { + "stride_w": 1, + "stride_h": 1 + } + } + ], + "name": "main" + } + ], + "description": "MLIR Converted." +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/importer_test_min_max.cc b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/importer_test_min_max.cc new file mode 100644 index 0000000000000000000000000000000000000000..5c5992acd8f2489ee834b324168912acf753d100 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/importer_test_min_max.cc @@ -0,0 +1,172 @@ +/* Copyright 2019 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 "llvm/Support/CommandLine.h" +#include "llvm/Support/InitLLVM.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/PrettyStackTrace.h" +#include "llvm/Support/raw_ostream.h" +#include "tensorflow/compiler/mlir/lite/core/absl_error_model_builder.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h" +#include "tensorflow/compiler/mlir/lite/schema/schema_utils.h" + +using llvm::cl::opt; + +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s.mlir -o - \ +// RUN: | %p/importer_test_min_max - \ +// RUN: | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - \ +// RUN: | FileCheck %s + +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s.mlir -o - \ +// RUN: | %p/importer_test_min_max - \ +// RUN: | flatbuffer_to_string - \ +// RUN: | FileCheck --check-prefix=FB %s + +// Tests for verifying the tflite model with min/max can be imported +// correctly. + +// NOLINTNEXTLINE +static opt inputFileName(llvm::cl::Positional, + llvm::cl::desc(""), + llvm::cl::init("-")); + +namespace mlir { +namespace { +std::optional> InjectStatsToFullyConnected( + llvm::StringRef buffer) { + auto model_ptr = TFL::FlatBufferModelAbslError::VerifyAndBuildFromBuffer( + buffer.data(), buffer.size()); + if (nullptr == model_ptr) { + return std::nullopt; + } + std::unique_ptr model(model_ptr->GetModel()->UnPack()); + + // FB-LABEL: name: "arg0", + // FB-NEXT: quantization: { + // FB-NEXT: min: [ -1.0 ], + // FB-NEXT: max: [ 1.0 ] + // FB-NEXT: } + + // FB-LABEL: name: "arg1", + // FB-NEXT: quantization: { + // FB-EMPTY: + // FB-NEXT: } + + // FB-LABEL: name: "tfl.fully_connected", + // FB-NEXT: quantization: { + // FB-NEXT: min: [ -0.0, -1.0, -2.0, -3.0, -4.0, -5.0, -6.0, -7.0, + // FB-SAME: -8.0, -9.0, -10.0, -11.0, -12.0, -13.0, -14.0, -15.0, -16.0, + // FB-SAME: -17.0, -18.0, -19.0, -20.0, -21.0, -22.0, -23.0, -24.0, -25.0, + // FB-SAME: -26.0, -27.0, -28.0, -29.0, -30.0, -31.0, -32.0, -33.0, -34.0, + // FB-SAME: -35.0, -36.0, -37.0, -38.0, -39.0 ], + // FB-NEXT: max: [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, + // FB-SAME: 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, + // FB-SAME: 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0, + // FB-SAME: 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0 ], + // FB-NEXT: quantized_dimension: 1 + // FB-NEXT: } + + // FB-LABEL: name: "tfl.fully_connected:1", + // FB-NEXT: quantization: { + // FB-EMPTY: + // FB-NEXT: } + + // FB-LABEL: operators: [ { + // FB-NEXT: inputs: [ 0, 1, 2 ], + // FB-NEXT: outputs: [ 3, 4 ], + // FB-NEXT: builtin_options_type: FullyConnectedOptions, + // FB-NEXT: builtin_options: { + // FB-EMPTY: + // FB-NEXT: } + // FB-NEXT: } ], + + // CHECK-LABEL: func @main(%arg0: tensor<40x37xf32>, %arg1: tensor<40x37xf32>) + // CHECK-SAME: -> tensor<40x40xf32> + // CHECK: %[[stat:.*]] = "quantfork.stats"(%arg0) <{layerStats = dense + // CHECK-SAME: <[-1.000000e+00, 1.000000e+00]> : tensor<2xf32>}> + // CHECK-SAME: : (tensor<40x37xf32>) -> tensor<40x37xf32> + // CHECK-NEXT: %[[cst:.*]] = "tfl.pseudo_const"() <{value = dense< + // CHECK-SAME: 1.000000e+00> : tensor<40xf32>}> : () -> tensor<40xf32> + // CHECK-NEXT: %[[fc:.*]]:2 = "tfl.fully_connected"(%[[stat]], %arg1, + // CHECK-NEXT: %[[stat1:.*]] = "quantfork.stats"(%[[fc]]#0) + // CHECK-SAME: <{axis = 1 : i64, + // CHECK-SAME: axisStats = dense<{{\[}}[-0.000000e+00, 0.000000e+00], + // CHECK-SAME: [-1.000000e+00, 1.000000e+00], + // CHECK-SAME: [-2.000000e+00, 2.000000e+00] + // CHECK-NEXT: return %[[stat1]] : tensor<40x40xf32> + // CHECK-NEXT: } + + // Find the tensors and inject the min and max to the input and output + for (auto& sub_graph : model->subgraphs) { + for (auto& op : sub_graph->operators) { + if (tflite::GetBuiltinCode( + model->operator_codes[op->opcode_index].get()) == + tflite::BuiltinOperator_FULLY_CONNECTED) { + // inject min/max to the input and output tensors + auto& input_tensor = sub_graph->tensors[op->inputs[0]]; + input_tensor->quantization->scale.clear(); + input_tensor->quantization->zero_point.clear(); + input_tensor->quantization->min.push_back(-1.0); + input_tensor->quantization->max.push_back(1.0); + + auto& output_tensor = sub_graph->tensors[op->outputs[0]]; + auto shape = output_tensor->shape; + output_tensor->quantization->scale.clear(); + output_tensor->quantization->zero_point.clear(); + for (int i = 0; i < shape.back(); ++i) { + output_tensor->quantization->min.push_back(-1.0 * i); + output_tensor->quantization->max.push_back(1.0 * i); + } + output_tensor->quantization->quantized_dimension = shape.size() - 1; + } + } + } + return model; +} + +} // namespace +} // namespace mlir + +int main(int argc, char** argv) { + llvm::InitLLVM y(argc, argv); + llvm::cl::ParseCommandLineOptions(argc, argv); + auto file_or_err = llvm::MemoryBuffer::getFileOrSTDIN(inputFileName.c_str()); + if (std::error_code error = file_or_err.getError()) { + llvm::errs() << argv[0] << ": could not open input file '" << inputFileName + << "': " << error.message() << "\n"; + return 1; + } + auto buffer = file_or_err->get(); + auto maybe_module = + mlir::InjectStatsToFullyConnected(buffer->getBuffer().str()); + if (!maybe_module.has_value()) { + return 1; + } + flatbuffers::FlatBufferBuilder builder; + flatbuffers::Offset output_model_location = + tflite::Model::Pack(builder, maybe_module.value().get()); + tflite::FinishModelBuffer(builder, output_model_location); + std::string output_model_content( + reinterpret_cast(builder.GetBufferPointer()), + builder.GetSize()); + std::cout << output_model_content << "\n"; + return 0; +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/importer_test_min_max.cc.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/importer_test_min_max.cc.mlir new file mode 100644 index 0000000000000000000000000000000000000000..1dd31be154d9ac38912f3b441dd69d0ff3dafccf --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/importer_test_min_max.cc.mlir @@ -0,0 +1,14 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s +// This only test the exporter and importer are working without min/max quantization parameters. + +func.func @main(tensor<40x37xf32>, tensor<40x37xf32>) -> tensor<40x40xf32> { +^bb0(%arg0: tensor<40x37xf32>, %arg1: tensor<40x37xf32>): + %cst = arith.constant dense<1.0> : tensor<40xf32> + %0:2 = "tfl.fully_connected"(%arg0, %arg1, %cst) {fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"} : (tensor<40x37xf32>, tensor<40x37xf32>, tensor<40xf32>) -> (tensor<40x40xf32>, tensor<40x40xf32>) + func.return %0 : tensor<40x40xf32> + +// CHECK-LABEL: func @main(%arg0: tensor<40x37xf32>, %arg1: tensor<40x37xf32>) -> tensor<40x40xf32> +// CHECK: %[[CONST:[0-9]+]] = "tfl.pseudo_const"() <{value = dense<1.000000e+00> : tensor<40xf32>}> +// CHECK-NEXT: %[[FULL:[0-9]+]]:2 = "tfl.fully_connected"(%arg0, %arg1, %[[CONST]]) <{asymmetric_quantize_inputs = false, fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"}> +// CHECK-NEXT: return %[[FULL]]#0 +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/input_arrays.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/input_arrays.mlir new file mode 100644 index 0000000000000000000000000000000000000000..312c8c6c731a5c0f75e717b91a14e949576233aa --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/input_arrays.mlir @@ -0,0 +1,14 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate -input-arrays=squared_difference --experimental-prune-unreachable-nodes-unconditionally --tflite-flatbuffer-to-mlir - -o - +// TODO(b/329300758): re-enable filecheck | FileCheck %s +// Tests -input-arrays flag. + +func.func @main(%arg0: tensor<4xf32>) -> tensor<4xf32> { + %0 = "tfl.pseudo_const" () {value = dense<1.0> : tensor<4xf32>} : () -> tensor<4xf32> loc("Const") + %1 = "tfl.squared_difference"(%arg0, %0) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> loc("squared_difference") + %2 = "tfl.mul"(%0, %1) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> loc("mul") + func.return %2 : tensor<4xf32> + +// CHECK-LABEL: main +// CHECK-NOT: tfl.squared_difference +// CHECK: tfl.mul %[[CONST:.*]], %arg0 +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/input_output_names_attr.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/input_output_names_attr.mlir new file mode 100644 index 0000000000000000000000000000000000000000..4c62ca98334a197ebfeea6d1322e785e5b8dfeb2 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/input_output_names_attr.mlir @@ -0,0 +1,12 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s + +// Tests input and output names from FlatBuffer are added to `tf.entry_function` attribute. + +// CHECK-LABEL: @main +func.func @main(%arg0: tensor<4xi8>, %arg1: tensor<4xi32>) -> (tensor<4xi32>, tensor<4xi8>) +// CHECK: attributes {tf.entry_function = {inputs = "input0,input1", outputs = "output0,output1"}} +attributes {tf.entry_function = {inputs = "input0,input1", outputs = "output0,output1"}} { + %0 = "tfl.neg"(%arg0) : (tensor<4xi8>) -> tensor<4xi8> loc("neg") + %1 = "tfl.neg"(%arg1) : (tensor<4xi32>) -> tensor<4xi32> loc("neg") + func.return %1, %0 : tensor<4xi32>, tensor<4xi8> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/legacy_reshape.json b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/legacy_reshape.json new file mode 100644 index 0000000000000000000000000000000000000000..3834b1883df970118ce794f0539291a366ffb6ac --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/legacy_reshape.json @@ -0,0 +1,44 @@ +// RUN: json_to_flatbuffer %p/test_schema.fbs %s | flatbuffer_translate --tflite-flatbuffer-to-mlir -o - | FileCheck %s + +// CHECK: %0 = "tfl.pseudo_const"() <{value = dense<2> : tensor<2xi32>}> : () -> tensor<2xi32> +// CHECK: %1 = "tfl.reshape"(%arg0, %0) : (tensor<1x4xf32>, tensor<2xi32>) -> tensor<2x2xf32> + +{ + "version": 3, + "operator_codes": [ + { + "builtin_code": "RESHAPE" + } + ], + "subgraphs": [ + { + "tensors": [ + { + "shape": [1, 4], + "name": "input", + "quantization": { + } + }, + { + "shape": [2, 2], + "name": "output", + "quantization": { + } + } + ], + "inputs": [0], + "outputs": [1], + "operators": [ + { + "inputs": [ 0 ], + "outputs": [ 1 ], + "builtin_options_type": "ReshapeOptions", + "builtin_options": { + "new_shape": [ 2, 2 ] + } + } + ] + } + ], + "buffers": [] +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/lstm.json b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/lstm.json new file mode 100644 index 0000000000000000000000000000000000000000..43e7f41d0c3b6f96377782286d51ca9841ebd148 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/lstm.json @@ -0,0 +1,346 @@ +// RUN: json_to_flatbuffer %p/test_schema.fbs %s | flatbuffer_translate --tflite-flatbuffer-to-mlir -o - | FileCheck %s +// RUN: json_to_flatbuffer %p/test_schema.fbs %s | flatbuffer_translate --tflite-flatbuffer-to-mlir -o - | flatbuffer_translate -mlir-to-tflite-flatbuffer - -o - | flatbuffer_to_string - | FileCheck --check-prefix=RoundTrip %s + +// CHECK-DAG: %[[input_18:.*]] = "quantfork.stats"({{.*}}) <{layerStats = dense<[-8.000000e-01, 1.600000e+00]> : tensor<2xf32>}> : (tensor<1x4xf32>) -> tensor<1x4xf32> +// CHECK-DAG: %[[input_19:.*]] = "quantfork.stats"({{.*}}) <{layerStats = dense<[-2.000000e+00, 4.000000e+00]> : tensor<2xf32>}> : (tensor<1x2xf32>) -> tensor<1x2xf32> + +// CHECK: "tfl.unidirectional_sequence_lstm"({{.*}}, %[[input_18]], %[[input_19]], %{{[0-9]+}}, %{{[0-9]+}}, %{{[0-9]+}}, %{{[0-9]+}}) +// CHECK-SAME: effective_hidden_scale_intermediate = tensor<*x!quant.calibrated>> +// CHECK-SAME: input_to_cell_intermediate = tensor<*x!quant.calibrated>> +// CHECK-SAME: input_to_forget_intermediate = tensor<*x!quant.calibrated>> +// CHECK-SAME: input_to_input_intermediate = tensor<*x!quant.calibrated>> +// CHECK-SAME: input_to_output_intermediate = tensor<*x!quant.calibrated>> + +// Checks if calibrated type is exported back to quantized type. +// RoundTrip: name: "effective_hidden_scale_intermediate", +// RoundTrip-NEXT: quantization: { +// RoundTrip-NEXT: min: [ -0.5 ], +// RoundTrip-NEXT: max: [ 0.5 ] + +{ + "version": 3, + "operator_codes": [ + { + "builtin_code": "UNIDIRECTIONAL_SEQUENCE_LSTM" + } + ], + "subgraphs": [ + { + "tensors": [ + { + "shape": [1, 5, 2], + "name": "input0" + }, + { + "shape": [2, 5], + "buffer": 1, + "name": "input2input_weights1" + }, + { + "shape": [2, 5], + "buffer": 2, + "name": "input2forget_weights2" + }, + { + "shape": [2, 5], + "buffer": 3, + "name": "input2cell_weights3" + }, + { + "shape": [2, 5], + "buffer": 4, + "name": "input2output_weights4" + }, + { + "shape": [2, 4], + "buffer": 5, + "name": "rec2input_weights5" + }, + { + "shape": [2, 4], + "buffer": 6, + "name": "rec2forget_weights6" + }, + { + "shape": [2, 4], + "buffer": 7, + "name": "rec2cell_weights7" + }, + { + "shape": [2, 4], + "buffer": 8, + "name": "rec2output_weights8" + }, + { + "shape": [2], + "buffer": 9, + "name": "cell2input_weights9" + }, + { + "shape": [2], + "buffer": 10, + "name": "cell2forget_weights10" + }, + { + "shape": [2], + "buffer": 11, + "name": "cell2output_weights11" + }, + { + "shape": [2], + "buffer": 12, + "name": "input_gate_bias12" + }, + { + "shape": [2], + "buffer": 13, + "name": "forget_gate_bias13" + }, + { + "shape": [2], + "buffer": 14, + "name": "cell_gate_bias14" + }, + { + "shape": [2], + "buffer": 15, + "name": "output_gate_bias15" + }, + { + "shape": [4, 2], + "buffer": 16, + "name": "proj_weights16" + }, + { + "shape": [4], + "buffer": 17, + "name": "proj_bias17" + }, + { + "shape": [1, 4], + "name": "input_activation_state18", + "is_variable": true, + "quantization": { + "min": [-0.8], + "max": [1.6] + } + }, + { + "shape": [1, 2], + "name": "input_cell_state19", + "is_variable": true, + "quantization": { + "min": [-2.0], + "max": [4.0] + } + }, + { + "shape": [2], + "buffer": 18, + "name": "input_norm20" + }, + { + "shape": [2], + "buffer": 19, + "name": "forget_norm21" + }, + { + "shape": [2], + "buffer": 20, + "name": "cell_norm22" + }, + { + "shape": [2], + "buffer": 21, + "name": "output_norm23" + }, + { + "shape": [], + "name": "output24" + }, + { + "shape": [], + "name": "intermediate_0", + "is_variable": true, + "quantization": { + "min": [-32], + "max": [32] + } + }, + { + "shape": [], + "name": "intermediate_1", + "is_variable": true, + "quantization": { + "min": [-16], + "max": [16] + } + }, + { + "shape": [], + "name": "intermediate_2", + "is_variable": true, + "quantization": { + "min": [-4], + "max": [4] + } + }, + { + "shape": [], + "name": "intermediate_3", + "is_variable": true, + "quantization": { + "min": [-1.0], + "max": [1.0] + } + }, + { + "shape": [], + "name": "intermediate_4", + "is_variable": true, + "quantization": { + "min": [-0.5], + "max": [0.5] + } + } + ], + "inputs": [0], + "outputs": [24], + "operators": [ + { + "inputs": [ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 + ], + "outputs": [24], + "intermediates": [ + 25, 26, 27, 28, 29 + ], + "builtin_options_type": "UnidirectionalSequenceLSTMOptions", + "builtin_options": { + "fused_activation_function": "TANH", + "cell_clip": 50.0 + }, + "mutating_variable_inputs": [ + false, + false, false, false, false, + false, false, false, false, + false, false, false, + false, false, false, false, + true, true, + false, false, false, false + ] + } + ] + } + ], + "buffers": [ + { + "data": [] + }, + { + "data": [ + 36, 167, 168, 63, 0, 140, 72, 191, 120, 20, 147, 62, 20, 152, 196, 190, 121, 98, 82, 187, 95, 128, 213, 61, 189, 3, 138, 63, 54, 103, 13, 62, 46, 224, 66, 63, 157, 204, 180, 191 + ] + }, + { + "data": [ + 223, 20, 21, 64, 246, 166, 31, 191, 6, 51, 157, 188, 114, 90, 167, 62, 118, 240, 59, 63, 49, 162, 255, 62, 17, 91, 160, 63, 32, 47, 26, 63, 40, 136, 178, 191, 243, 154, 236, 61 + ] + }, + { + "data": [ + 137, 231, 86, 63, 41, 154, 16, 63, 239, 37, 77, 191, 55, 189, 24, 189, 86, 63, 18, 63, 42, 55, 13, 191, 110, 139, 138, 191, 219, 148, 181, 63, 71, 232, 108, 191, 66, 226, 145, 191 + ] + }, + { + "data": [ + 245, 179, 225, 190, 51, 202, 176, 189, 132, 47, 53, 191, 155, 25, 50, 191, 197, 130, 240, 191, 98, 125, 45, 62, 243, 70, 83, 62, 85, 155, 139, 63, 113, 239, 11, 192, 35, 251, 139, 62 + ] + }, + { + "data": [ + 248, 188, 211, 191, 142, 11, 73, 62, 36, 8, 84, 63, 186, 208, 11, 191, 76, 208, 190, 191, 223, 200, 210, 63, 183, 170, 103, 63, 116, 129, 145, 63 + ] + }, + { + "data": [ + 235, 202, 222, 190, 159, 201, 112, 191, 217, 248, 166, 63, 165, 199, 131, 191, 130, 59, 47, 63, 179, 11, 186, 62, 55, 168, 18, 192, 152, 213, 26, 64 + ] + }, + { + "data": [ + 245, 123, 138, 62, 213, 106, 231, 59, 211, 218, 250, 62, 25, 157, 134, 63, 147, 22, 164, 63, 25, 221, 139, 62, 1, 230, 247, 62, 210, 185, 142, 63 + ] + }, + { + "data": [ + 197, 123, 23, 192, 45, 96, 178, 190, 174, 87, 165, 62, 213, 225, 200, 191, 119, 248, 15, 191, 128, 125, 171, 189, 90, 125, 222, 63, 4, 76, 95, 62 + ] + }, + { + "data": [ + 210, 73, 183, 63, 248, 177, 13, 191 + ] + }, + { + "data": [ + 78, 251, 212, 191, 169, 29, 147, 63 + ] + }, + { + "data": [ + 178, 227, 203, 191, 247, 155, 103, 63 + ] + }, + { + "data": [ + 206, 111, 165, 190, 153, 77, 227, 63 + ] + }, + { + "data": [ + 255, 114, 132, 191, 253, 202, 140, 191 + ] + }, + { + "data": [ + 90, 247, 1, 192, 125, 120, 209, 191 + ] + }, + { + "data": [ + 65, 75, 243, 191, 58, 122, 146, 190 + ] + }, + { + "data": [ + 40, 135, 20, 63, 109, 50, 220, 191, 56, 241, 189, 63, 65, 12, 92, 63, 61, 14, 162, 62, 157, 138, 81, 63, 125, 61, 191, 61, 102, 231, 20, 63 + ] + }, + { + "data": [ + 145, 79, 49, 189, 175, 235, 220, 190, 182, 111, 157, 190, 144, 236, 97, 191 + ] + }, + { + "data": [ + 76, 188, 109, 63, 228, 150, 201, 190 + ] + }, + { + "data": [ + 6, 146, 66, 191, 122, 127, 100, 191 + ] + }, + { + "data": [ + 216, 59, 169, 190, 161, 178, 215, 191 + ] + }, + { + "data": [ + 208, 144, 101, 191, 127, 233, 195, 190 + ] + } + ] +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/lstm.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/lstm.mlir new file mode 100644 index 0000000000000000000000000000000000000000..a2b64a46a72170762094f9a86443c504d8508acb --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/lstm.mlir @@ -0,0 +1,95 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s +// Ensure lstm roundtrip exactly + +func.func @main(%arg0: tensor<1x4xf32>, %arg1: tensor<4x4xf32>, %arg2: tensor<4x4xf32>, %arg3: tensor<4x4xf32>, %arg4: tensor<4x4xf32>, %arg5: tensor<4x4xf32>, %arg6: tensor<4x4xf32>, %arg7: tensor<4x4xf32>, %arg8: tensor<4x4xf32>, %arg9: tensor<4xf32>, %arg10: tensor<4xf32>, %arg11: tensor<4xf32>, %arg12: tensor<1x4xf32>, %arg13: tensor<4xf32>, %arg14: tensor<4xf32>, %arg15: tensor<4xf32>, %arg16: tensor<4x4xf32>, %arg17: tensor<4xf32>, %arg18: tensor<4xf32>, %arg19: tensor<4xf32>, %arg20: tensor<4xf32>, %arg21: tensor<4xf32>) -> tensor<1x4xf32> { + %cst0 = "tfl.pseudo_const" () {value = dense<0.0> : tensor<1x4xf32>} : () -> tensor<1x4xf32> loc("Const") + %cst1 = "tfl.pseudo_const" () {value = dense<0.0> : tensor<1x4xf32>} : () -> tensor<1x4xf32> loc("Const") + %24 = "tfl.lstm"(%arg0, %arg1, %arg2, %arg3, %arg4, %arg5, %arg6, %arg7, %arg8, %arg9, %arg10, %arg11, %arg12, %arg13, %arg14, %arg15, %arg16, %arg17, %cst0, %cst1, %arg18, %arg19, %arg20, %arg21) ({}) {cell_clip = 0.000000e+00 : f32, fused_activation_function = "NONE", kernel_type = #tfl, proj_clip = 0.000000e+00 : f32} : (tensor<1x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<1x4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4x4xf32>, tensor<4xf32>, tensor<1x4xf32>, tensor<1x4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>) -> tensor<1x4xf32> + func.return %24 : tensor<1x4xf32> +// CHECK-LABEL: main +// separate lines since there is no region for this op. third_party/tensorflow/compiler/mlir/lite/ir/tfl_ops.td: 3252 +// CHECK-DAG: %[[RES0:.*]] = "tfl.pseudo_const"() <{value = dense<{{.*}}> : tensor<1x4xf32>}> {tfl.is_variable} : () -> tensor<1x4xf32> +// CHECK-DAG: %[[RES1:.*]] = "tfl.pseudo_const"() <{value = dense<{{.*}}> : tensor<1x4xf32>}> {tfl.is_variable} : () -> tensor<1x4xf32> +// CHECK: %[[RES2:.*]] = "tfl.lstm"(%arg0, %arg1, %arg2, %arg3, %arg4, %arg5, %arg6, %arg7, %arg8, %arg9, %arg10, %arg11, %arg12, %arg13, %arg14, %arg15, %arg16, %arg17, %[[RES0]], %[[RES1]], %arg18, %arg19, %arg20, %arg21) +// CHECK-SAME: <{asymmetric_quantize_inputs = false, cell_clip = 0.000000e+00 : f32, fused_activation_function = "NONE", kernel_type = #tfl, proj_clip = 0.000000e+00 : f32}> ({ +// CHECK: }) : (tensor<1x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<1x4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4x4xf32>, tensor<4xf32>, tensor<1x4xf32>, tensor<1x4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>) -> tensor<1x4xf32> +// CHECK: return %[[RES2]] + +} + +// ----- + +func.func @testFullyQuantizedLSTM(%arg0: tensor<1x528x!quant.uniform>, %arg1: tensor<2048x528x!quant.uniform:f32, 0.059801999479532242>>, %arg2: tensor<2048x528x!quant.uniform:f32, 0.031925998628139496>>, %arg3: tensor<2048x528x!quant.uniform:f32, 0.056272000074386597>>, %arg4: tensor<2048x528x!quant.uniform:f32, 0.063763998448848724>>, %arg5: tensor<2048x640x!quant.uniform:f32, 0.013358999975025654>>, %arg6: tensor<2048x640x!quant.uniform:f32, 0.022830000147223473>>, %arg7: tensor<2048x640x!quant.uniform:f32, 0.032276000827550888>>, %arg8: tensor<2048x640x!quant.uniform:f32, 0.035427000373601913>>, %arg9: tensor<2048x!quant.uniform>, %arg10: tensor<2048x!quant.uniform>, %arg11: tensor<2048x!quant.uniform>, %arg12: tensor<2048x!quant.uniform>, %arg13: tensor<640x2048x!quant.uniform:f32, 0.021174000576138496>>, %arg14: tensor<640x!quant.uniform>, %arg15: tensor<2048x!quant.uniform>, %arg16: tensor<2048x!quant.uniform>, %arg17: tensor<2048x!quant.uniform>, %arg18: tensor<2048x!quant.uniform>, %arg19: tensor<1x640x!quant.uniform>, %arg20: tensor<1x2048x!quant.uniform>) -> tensor<1x640x!quant.uniform> { + %cst = "tfl.no_value"() {value = unit} : () -> none + %0 = "tfl.lstm"(%arg0, %arg1, %arg2, %arg3, %arg4, %arg5, %arg6, %arg7, %arg8, %cst, %cst, %cst, %arg9, %arg10, %arg11, %arg12, %arg13, %arg14, %arg19, %arg20, %arg15, %arg16, %arg17, %arg18) ({}) {cell_clip = 1.000000e+01 : f32, fused_activation_function = "TANH", input_to_input_intermediate = tensor<0x!quant.uniform>, input_to_forget_intermediate = tensor<0x!quant.uniform>, input_to_cell_intermediate = tensor<0x!quant.uniform>, input_to_output_intermediate = tensor<0x!quant.uniform>, effective_hidden_scale_intermediate = tensor<0x!quant.uniform>, kernel_type = #tfl, proj_clip = 0.01 : f32} : (tensor<1x528x!quant.uniform>, tensor<2048x528x!quant.uniform:f32, 0.059801999479532242>>, tensor<2048x528x!quant.uniform:f32, 0.031925998628139496>>, tensor<2048x528x!quant.uniform:f32, 0.056272000074386597>>, tensor<2048x528x!quant.uniform:f32, 0.063763998448848724>>, tensor<2048x640x!quant.uniform:f32, 0.013358999975025654>>, tensor<2048x640x!quant.uniform:f32, 0.022830000147223473>>, tensor<2048x640x!quant.uniform:f32, 0.032276000827550888>>, tensor<2048x640x!quant.uniform:f32, 0.035427000373601913>>, none, none, none, tensor<2048x!quant.uniform>, tensor<2048x!quant.uniform>, tensor<2048x!quant.uniform>, tensor<2048x!quant.uniform>, tensor<640x2048x!quant.uniform:f32, 0.021174000576138496>>, tensor<640x!quant.uniform>, tensor<1x640x!quant.uniform>, tensor<1x2048x!quant.uniform>, tensor<2048x!quant.uniform>, tensor<2048x!quant.uniform>, tensor<2048x!quant.uniform>, tensor<2048x!quant.uniform>) -> tensor<1x640x!quant.uniform> + func.return %0 : tensor<1x640x!quant.uniform> +// CHECK-LABEL: testFullyQuantizedLSTM +// CHECK: %[[CST:.*]] = "tfl.no_value"() <{value}> : () -> none +// CHECK: %[[RES0:.*]] = "tfl.lstm"(%arg0, %arg1, %arg2, %arg3, %arg4, %arg5, %arg6, %arg7, %arg8, %[[CST]], %[[CST]], %[[CST]], %arg9, %arg10, %arg11, %arg12, %arg13, %arg14, %arg19, %arg20, %arg15, %arg16, %arg17, %arg18) +// CHECK-SAME: <{asymmetric_quantize_inputs = false, cell_clip = 1.000000e+01 : f32, effective_hidden_scale_intermediate = tensor<0x!quant.uniform>, fused_activation_function = "TANH", input_to_cell_intermediate = tensor<0x!quant.uniform>, input_to_forget_intermediate = tensor<0x!quant.uniform>, input_to_input_intermediate = tensor<0x!quant.uniform>, input_to_output_intermediate = tensor<0x!quant.uniform>, kernel_type = #tfl, proj_clip = 0.00999999977 : f32}> ({ +// CHECK: }) : (tensor<1x528x!quant.uniform>, tensor<2048x528x!quant.uniform>, tensor<2048x528x!quant.uniform>, tensor<2048x528x!quant.uniform>, tensor<2048x528x!quant.uniform>, tensor<2048x640x!quant.uniform>, tensor<2048x640x!quant.uniform>, tensor<2048x640x!quant.uniform>, tensor<2048x640x!quant.uniform>, none, none, none, tensor<2048x!quant.uniform>, tensor<2048x!quant.uniform>, tensor<2048x!quant.uniform>, tensor<2048x!quant.uniform>, tensor<640x2048x!quant.uniform>, tensor<640x!quant.uniform>, tensor<1x640x!quant.uniform>, tensor<1x2048x!quant.uniform>, tensor<2048x!quant.uniform>, tensor<2048x!quant.uniform>, tensor<2048x!quant.uniform>, tensor<2048x!quant.uniform>) -> tensor<1x640x!quant.uniform> +} + +// ----- + +// CHECK-LABEL: testUnidirectionalSequenceLstmWithIntermediates +func.func @testUnidirectionalSequenceLstmWithIntermediates(%arg0: tensor, %arg1: tensor, %arg2: tensor, %arg3: tensor, %arg4: tensor, %arg5: tensor, %arg6: tensor, %arg7: tensor, %arg8: tensor, %arg9: tensor, %arg10: tensor, %arg11: tensor, %arg12: tensor, %arg13: tensor, %arg14: tensor, %arg15: tensor, %arg16: tensor, %arg17: tensor, %arg18: tensor, %arg19: tensor, %arg20: tensor, %arg21: tensor, %arg22: tensor, %arg23: tensor) -> tensor { + // CHECK: "tfl.unidirectional_sequence_lstm"(%arg0, %arg1, %arg2, %arg3, %arg4, %arg5, %arg6, %arg7, %arg8, %arg9, %arg10, %arg11, %arg12, %arg13, %arg14, %arg15, %arg16, %arg17, %arg18, %arg19, %arg20, %arg21, %arg22, %arg23) <{asymmetric_quantize_inputs = false, cell_clip = 1.000000e+01 : f32, diagonal_recurrent_tensors = false, effective_hidden_scale_intermediate = tensor<0x!quant.uniform>, fused_activation_function = "TANH", input_to_cell_intermediate = tensor<0xf32>, input_to_forget_intermediate = tensor<0xf32>, input_to_input_intermediate = tensor<0xf32>, input_to_output_intermediate = tensor<0xf32>, proj_clip = 0.000000e+00 : f32, time_major = false}> : (tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> tensor + %0 = "tfl.unidirectional_sequence_lstm"(%arg0, %arg1, %arg2, %arg3, %arg4, %arg5, %arg6, %arg7, %arg8, %arg9, %arg10, %arg11, %arg12, %arg13, %arg14, %arg15, %arg16, %arg17, %arg18, %arg19, %arg20, %arg21, %arg22, %arg23) {cell_clip = 1.000000e+01 : f32, effective_hidden_scale_intermediate = tensor<0x!quant.uniform>, fused_activation_function = "TANH", input_to_cell_intermediate = tensor<0xf32>, input_to_forget_intermediate = tensor<0xf32>, input_to_input_intermediate = tensor<0xf32>, input_to_output_intermediate = tensor<0xf32>, proj_clip = 0.000000e+00 : f32, time_major = false} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> tensor + func.return %0 : tensor +} + +// ----- + +// CHECK-LABEL: testLSTMAsymAttributeTrue +func.func @testLSTMAsymAttributeTrue(%arg0: tensor<1x4xf32>, %arg1: tensor<4x4xf32>, %arg2: tensor<4x4xf32>, %arg3: tensor<4x4xf32>, %arg4: tensor<4x4xf32>, %arg5: tensor<4x4xf32>, %arg6: tensor<4x4xf32>, %arg7: tensor<4x4xf32>, %arg8: tensor<4x4xf32>, %arg9: tensor<4xf32>, %arg10: tensor<4xf32>, %arg11: tensor<4xf32>, %arg12: tensor<1x4xf32>, %arg13: tensor<4xf32>, %arg14: tensor<4xf32>, %arg15: tensor<4xf32>, %arg16: tensor<4x4xf32>, %arg17: tensor<4xf32>, %arg18: tensor<4xf32>, %arg19: tensor<4xf32>, %arg20: tensor<4xf32>, %arg21: tensor<4xf32>) -> tensor<1x4xf32> { + %cst0 = "tfl.pseudo_const" () {value = dense<0.0> : tensor<1x4xf32>} : () -> tensor<1x4xf32> loc("Const") + %cst1 = "tfl.pseudo_const" () {value = dense<0.0> : tensor<1x4xf32>} : () -> tensor<1x4xf32> loc("Const") + %24 = "tfl.lstm"(%arg0, %arg1, %arg2, %arg3, %arg4, %arg5, %arg6, %arg7, %arg8, %arg9, %arg10, %arg11, %arg12, %arg13, %arg14, %arg15, %arg16, %arg17, %cst0, %cst1, %arg18, %arg19, %arg20, %arg21) ({}) {asymmetric_quantize_inputs = true, cell_clip = 0.000000e+00 : f32, fused_activation_function = "NONE", kernel_type = #tfl, proj_clip = 0.000000e+00 : f32} : (tensor<1x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<1x4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4x4xf32>, tensor<4xf32>, tensor<1x4xf32>, tensor<1x4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>) -> tensor<1x4xf32> + func.return %24 : tensor<1x4xf32> + +// CHECK-DAG: %[[RES0:.*]] = "tfl.pseudo_const"() <{value = dense<{{.*}}> : tensor<1x4xf32>}> {tfl.is_variable} : () -> tensor<1x4xf32> +// CHECK-DAG: %[[RES1:.*]] = "tfl.pseudo_const"() <{value = dense<{{.*}}> : tensor<1x4xf32>}> {tfl.is_variable} : () -> tensor<1x4xf32> +// CHECK: %[[RES2:.*]] = "tfl.lstm"(%arg0, %arg1, %arg2, %arg3, %arg4, %arg5, %arg6, %arg7, %arg8, %arg9, %arg10, %arg11, %arg12, %arg13, %arg14, %arg15, %arg16, %arg17, %[[RES0]], %[[RES1]], %arg18, %arg19, %arg20, %arg21) +// CHECK-SAME: <{asymmetric_quantize_inputs = true, cell_clip = 0.000000e+00 : f32, fused_activation_function = "NONE", kernel_type = #tfl, proj_clip = 0.000000e+00 : f32}> ({ +// CHECK: }) : (tensor<1x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<1x4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4x4xf32>, tensor<4xf32>, tensor<1x4xf32>, tensor<1x4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>) -> tensor<1x4xf32> +// CHECK: return %[[RES2]] + +} + +// ----- + +// CHECK-LABEL: testLSTMAsymAttributeFalse +func.func @testLSTMAsymAttributeFalse(%arg0: tensor<1x4xf32>, %arg1: tensor<4x4xf32>, %arg2: tensor<4x4xf32>, %arg3: tensor<4x4xf32>, %arg4: tensor<4x4xf32>, %arg5: tensor<4x4xf32>, %arg6: tensor<4x4xf32>, %arg7: tensor<4x4xf32>, %arg8: tensor<4x4xf32>, %arg9: tensor<4xf32>, %arg10: tensor<4xf32>, %arg11: tensor<4xf32>, %arg12: tensor<1x4xf32>, %arg13: tensor<4xf32>, %arg14: tensor<4xf32>, %arg15: tensor<4xf32>, %arg16: tensor<4x4xf32>, %arg17: tensor<4xf32>, %arg18: tensor<4xf32>, %arg19: tensor<4xf32>, %arg20: tensor<4xf32>, %arg21: tensor<4xf32>) -> tensor<1x4xf32> { + %cst0 = "tfl.pseudo_const" () {value = dense<0.0> : tensor<1x4xf32>} : () -> tensor<1x4xf32> loc("Const") + %cst1 = "tfl.pseudo_const" () {value = dense<0.0> : tensor<1x4xf32>} : () -> tensor<1x4xf32> loc("Const") + %24 = "tfl.lstm"(%arg0, %arg1, %arg2, %arg3, %arg4, %arg5, %arg6, %arg7, %arg8, %arg9, %arg10, %arg11, %arg12, %arg13, %arg14, %arg15, %arg16, %arg17, %cst0, %cst1, %arg18, %arg19, %arg20, %arg21) ({}) {asymmetric_quantize_inputs = false, cell_clip = 0.000000e+00 : f32, fused_activation_function = "NONE", kernel_type = #tfl, proj_clip = 0.000000e+00 : f32} : (tensor<1x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<1x4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4x4xf32>, tensor<4xf32>, tensor<1x4xf32>, tensor<1x4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>) -> tensor<1x4xf32> + func.return %24 : tensor<1x4xf32> + +// CHECK-DAG: %[[RES0:.*]] = "tfl.pseudo_const"() <{value = dense<{{.*}}> : tensor<1x4xf32>}> {tfl.is_variable} : () -> tensor<1x4xf32> +// CHECK-DAG: %[[RES1:.*]] = "tfl.pseudo_const"() <{value = dense<{{.*}}> : tensor<1x4xf32>}> {tfl.is_variable} : () -> tensor<1x4xf32> +// CHECK: %[[RES2:.*]] = "tfl.lstm"(%arg0, %arg1, %arg2, %arg3, %arg4, %arg5, %arg6, %arg7, %arg8, %arg9, %arg10, %arg11, %arg12, %arg13, %arg14, %arg15, %arg16, %arg17, %[[RES0]], %[[RES1]], %arg18, %arg19, %arg20, %arg21) +// CHECK-SAME: <{asymmetric_quantize_inputs = false, cell_clip = 0.000000e+00 : f32, fused_activation_function = "NONE", kernel_type = #tfl, proj_clip = 0.000000e+00 : f32}> ({ +// CHECK: }) : (tensor<1x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<1x4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4x4xf32>, tensor<4xf32>, tensor<1x4xf32>, tensor<1x4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>) -> tensor<1x4xf32> +// CHECK: return %[[RES2]] + +} + +// ----- + +// CHECK-LABEL: testLSTMAsymAttributeDefault +func.func @testLSTMAsymAttributeDefault(%arg0: tensor<1x4xf32>, %arg1: tensor<4x4xf32>, %arg2: tensor<4x4xf32>, %arg3: tensor<4x4xf32>, %arg4: tensor<4x4xf32>, %arg5: tensor<4x4xf32>, %arg6: tensor<4x4xf32>, %arg7: tensor<4x4xf32>, %arg8: tensor<4x4xf32>, %arg9: tensor<4xf32>, %arg10: tensor<4xf32>, %arg11: tensor<4xf32>, %arg12: tensor<1x4xf32>, %arg13: tensor<4xf32>, %arg14: tensor<4xf32>, %arg15: tensor<4xf32>, %arg16: tensor<4x4xf32>, %arg17: tensor<4xf32>, %arg18: tensor<4xf32>, %arg19: tensor<4xf32>, %arg20: tensor<4xf32>, %arg21: tensor<4xf32>) -> tensor<1x4xf32> { + %cst0 = "tfl.pseudo_const" () {value = dense<0.0> : tensor<1x4xf32>} : () -> tensor<1x4xf32> loc("Const") + %cst1 = "tfl.pseudo_const" () {value = dense<0.0> : tensor<1x4xf32>} : () -> tensor<1x4xf32> loc("Const") + %24 = "tfl.lstm"(%arg0, %arg1, %arg2, %arg3, %arg4, %arg5, %arg6, %arg7, %arg8, %arg9, %arg10, %arg11, %arg12, %arg13, %arg14, %arg15, %arg16, %arg17, %cst0, %cst1, %arg18, %arg19, %arg20, %arg21) ({}) {cell_clip = 0.000000e+00 : f32, fused_activation_function = "NONE", kernel_type = #tfl, proj_clip = 0.000000e+00 : f32} : (tensor<1x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<1x4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4x4xf32>, tensor<4xf32>, tensor<1x4xf32>, tensor<1x4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>) -> tensor<1x4xf32> + func.return %24 : tensor<1x4xf32> + +// CHECK-DAG: %[[RES0:.*]] = "tfl.pseudo_const"() <{value = dense<{{.*}}> : tensor<1x4xf32>}> {tfl.is_variable} : () -> tensor<1x4xf32> +// CHECK-DAG: %[[RES1:.*]] = "tfl.pseudo_const"() <{value = dense<{{.*}}> : tensor<1x4xf32>}> {tfl.is_variable} : () -> tensor<1x4xf32> +// CHECK: %[[RES2:.*]] = "tfl.lstm"(%arg0, %arg1, %arg2, %arg3, %arg4, %arg5, %arg6, %arg7, %arg8, %arg9, %arg10, %arg11, %arg12, %arg13, %arg14, %arg15, %arg16, %arg17, %[[RES0]], %[[RES1]], %arg18, %arg19, %arg20, %arg21) +// CHECK-SAME: <{asymmetric_quantize_inputs = false, cell_clip = 0.000000e+00 : f32, fused_activation_function = "NONE", kernel_type = #tfl, proj_clip = 0.000000e+00 : f32}> ({ +// CHECK: }) : (tensor<1x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<1x4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4x4xf32>, tensor<4xf32>, tensor<1x4xf32>, tensor<1x4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>, tensor<4xf32>) -> tensor<1x4xf32> +// CHECK: return %[[RES2]] + +} + diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/many_attribute_op.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/many_attribute_op.mlir new file mode 100644 index 0000000000000000000000000000000000000000..bd8e5738f8f520dba86c7f9ecedfa7a24039c44a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/many_attribute_op.mlir @@ -0,0 +1,9 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s + +// Confirm a wide array of attribute survives the round-trip +func.func @main(tensor<1x6x6x16xf32>) -> tensor<1x1x1x16xf32> { +^bb0(%arg0: tensor<1x6x6x16xf32>): + // CHECK: "tfl.average_pool_2d"(%{{.*}}) <{filter_height = 3 : i32, filter_width = 6 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 3 : i32, stride_w = 1 : i32}> : (tensor<1x6x6x16xf32>) -> tensor<1x1x1x16xf32> + %0 = "tfl.average_pool_2d"(%arg0) {filter_height = 3 : i32, filter_width = 6 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 3 : i32, stride_w = 1 : i32} : (tensor<1x6x6x16xf32>) -> tensor<1x1x1x16xf32> loc("avgpool") + func.return %0 : tensor<1x1x1x16xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/math.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/math.mlir new file mode 100644 index 0000000000000000000000000000000000000000..d31a99ea4b7fa8db95cc0a46de37b35f6aec82d9 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/math.mlir @@ -0,0 +1,18 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s +// Confirm float constants and operators survive a roundtrip + +func.func @main(tensor<4xf32>) -> tensor<4xf32> { +^bb0(%arg0: tensor<4xf32>): + // CHECK: [[CONST:%.*]] = "tfl.pseudo_const"() <{value = dense<1.000000e+00> : tensor<4xf32>}> : () -> tensor<4xf32> + // CHECK-NEXT: [[SQDIFF:%.*]] = tfl.squared_difference %arg0, [[CONST]] : tensor<4xf32> + // CHECK-NEXT: %{{.*}} = tfl.mul %arg0, [[SQDIFF]] {fused_activation_function = "NONE"} : tensor<4xf32> + %0 = "tfl.pseudo_const" () {value = dense<1.0> : tensor<4xf32>} : () -> tensor<4xf32> loc("Const") + // Confirm that attributes that cannot be stored in the flatbuffer options + // for a given operator are dropped silently. + %1 = "tfl.squared_difference"(%arg0, %0) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> loc("squared_difference") + %2 = "tfl.mul"(%arg0, %1) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> loc("mul") + %3 = "tfl.div"(%2, %1) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> loc("div") + %4 = "tfl.exp"(%3) : (tensor<4xf32>) -> tensor<4xf32> loc("exp") + %5 = "tfl.neg"(%4) : (tensor<4xf32>) -> tensor<4xf32> loc("neg") + func.return %5 : tensor<4xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/matmul.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/matmul.mlir new file mode 100644 index 0000000000000000000000000000000000000000..61b924a338495006ea8a62946bb8cd8f89715385 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/matmul.mlir @@ -0,0 +1,28 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s + +func.func @main(%arg0: tensor<4x10x15xf32>, %arg1: tensor<4x15x17xf32>) -> tensor<4x10x17xf32> { + %0 = "tfl.batch_matmul"(%arg0, %arg1) {adj_x = false, adj_y = false} : (tensor<4x10x15xf32>, tensor<4x15x17xf32>) -> tensor<4x10x17xf32> + func.return %0: tensor<4x10x17xf32> + +// CHECK-LABEL: main +// CHECK: %[[RESULT0:.*]] = "tfl.batch_matmul"(%arg0, %arg1) <{adj_x = false, adj_y = false, asymmetric_quantize_inputs = false}> : (tensor<4x10x15xf32>, tensor<4x15x17xf32>) -> tensor<4x10x17xf32> +// CHECK: return %[[RESULT0]] +} + +// CHECK-LABEL: testMatmulAsymAttributeTrue +func.func @testMatmulAsymAttributeTrue(%arg0: tensor<4x10x15xf32>, %arg1: tensor<4x15x17xf32>) -> tensor<4x10x17xf32> { + %0 = "tfl.batch_matmul"(%arg0, %arg1) {adj_x = false, adj_y = false, asymmetric_quantize_inputs = true} : (tensor<4x10x15xf32>, tensor<4x15x17xf32>) -> tensor<4x10x17xf32> + func.return %0: tensor<4x10x17xf32> + +// CHECK: %[[RESULT0:.*]] = "tfl.batch_matmul"(%arg0, %arg1) <{adj_x = false, adj_y = false, asymmetric_quantize_inputs = true}> : (tensor<4x10x15xf32>, tensor<4x15x17xf32>) -> tensor<4x10x17xf32> +// CHECK: return %[[RESULT0]] +} + +// CHECK-LABEL: testMatmulAsymAttributeFalse +func.func @testMatmulAsymAttributeFalse(%arg0: tensor<4x10x15xf32>, %arg1: tensor<4x15x17xf32>) -> tensor<4x10x17xf32> { + %0 = "tfl.batch_matmul"(%arg0, %arg1) {adj_x = false, adj_y = false, asymmetric_quantize_inputs = false} : (tensor<4x10x15xf32>, tensor<4x15x17xf32>) -> tensor<4x10x17xf32> + func.return %0: tensor<4x10x17xf32> + +// CHECK: %[[RESULT0:.*]] = "tfl.batch_matmul"(%arg0, %arg1) <{adj_x = false, adj_y = false, asymmetric_quantize_inputs = false}> : (tensor<4x10x15xf32>, tensor<4x15x17xf32>) -> tensor<4x10x17xf32> +// CHECK: return %[[RESULT0]] +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/mix_tflite_vhlo.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/mix_tflite_vhlo.mlir new file mode 100644 index 0000000000000000000000000000000000000000..e0dc70ac54308cb66bdc119f7e60b29e0cac0a8a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/mix_tflite_vhlo.mlir @@ -0,0 +1,16 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer --emit-stablehlo-ops=true %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir --disable-vhlo-to-stablehlo=true - -o - | FileCheck %s +// test stablehlo roundtrip + +module { +func.func @main(%arg0: tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> { + %0 = "vhlo.logistic_v1"(%arg0) : (tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> + %1 = "tfl.exp"(%0) : (tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> loc("exp") + func.return %1 : tensor<1x1x1x96xf32> +} +} + +// CHECK: func.func @main(%arg0: tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> attributes {tf.entry_function = {inputs = "arg0", outputs = "exp"}} { +// CHECK-NEXT: %0 = "vhlo.logistic_v1"(%arg0) : (tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> +// CHECK-NEXT: %1 = "tfl.exp"(%0) : (tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> +// CHECK-NEXT: return %1 : tensor<1x1x1x96xf32> +// CHECK-NEXT: } \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/multi_output_op.json b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/multi_output_op.json new file mode 100644 index 0000000000000000000000000000000000000000..d715271793986be169c01961c807a3f3b673d2e7 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/multi_output_op.json @@ -0,0 +1,90 @@ +// RUN: json_to_flatbuffer %p/test_schema.fbs %s | flatbuffer_translate --tflite-flatbuffer-to-mlir --mlir-print-debuginfo -o - | FileCheck %s + +// CHECK: #[[LOC0:.*]] = loc("":0:0) +// CHECK: "tfl.split" +// CHECK-SAME: loc(#[[SPLIT_LOC:.*]]) +// CHECK: #[[LOC1:.*]] = loc("output0"(#[[LOC0]])) +// CHECK: #[[LOC2:.*]] = loc("output1"(#[[LOC0]])) +// CHECK: #[[SPLIT_LOC]] = loc(fused[#[[LOC1]], #[[LOC2]]]) + +{ + "version": 3, + "operator_codes": [ + { + "builtin_code": "SPLIT" + } + ], + "subgraphs": [ + { + "tensors": [ + { + "shape": [ + 1 + ], + "name": "split_dim", + "quantization": { + } + }, + { + "shape": [ + 256, + 32, + 32, + 3 + ], + "name": "input", + "quantization": { + } + }, + { + "shape": [ + 128, + 32, + 32, + 3 + ], + "name": "output0", + "quantization": { + } + }, + { + "shape": [ + 128, + 32, + 32, + 3 + ], + "name": "output1", + "quantization": { + } + } + ], + "inputs": [ + 0, + 1 + ], + "outputs": [ + 2, + 3 + ], + "operators": [ + { + "inputs": [ + 0, + 1 + ], + "outputs": [ + 2, + 3 + ], + "builtin_options_type": "SplitOptions", + "builtin_options": { + "num_splits": 2 + } + } + ], + "name": "main" + } + ], + "description": "MLIR Converted." +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/optional.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/optional.mlir new file mode 100644 index 0000000000000000000000000000000000000000..14bb29d21dadb5cbe5b9aa8fdad1df15ffd173a8 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/optional.mlir @@ -0,0 +1,11 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s +// Test to make sure optional parameters survive a roundtrip + +func.func @main(%arg0: tensor<40x37xf32>, %arg1: tensor<40x37xf32>) -> tensor<40x40xf32> { +// CHECK: [[NONE:%.*]] = "tfl.no_value"() <{value}> : () -> none +// CHECK: "tfl.fully_connected"(%arg0, %arg1, [[NONE]]) +// CHECK-SAME: (tensor<40x37xf32>, tensor<40x37xf32>, none) -> (tensor<40x40xf32>, tensor<40x40xf32>) + %cst = "tfl.no_value"() {value = unit} : () -> none + %0:2 = "tfl.fully_connected"(%arg0, %arg1, %cst) {fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"} : (tensor<40x37xf32>, tensor<40x37xf32>, none) -> (tensor<40x40xf32>, tensor<40x40xf32>) + func.return %0 : tensor<40x40xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/optional_input.json b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/optional_input.json new file mode 100644 index 0000000000000000000000000000000000000000..cc6777a014a1bd32461bc22bf977711540458737 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/optional_input.json @@ -0,0 +1,80 @@ +// RUN: json_to_flatbuffer %p/test_schema.fbs %s | flatbuffer_translate --tflite-flatbuffer-to-mlir -o - | FileCheck %s + +// This test is to test that if the flatbuffer omits the last optional input `bias` of tfl.conv_2d op, the flatbuffer_importer will automatically adds `none` value to tfl.conv_2d. + +// CHECK: %[[CST:.*]] = "tfl.no_value"() <{value}> : () -> none +// CHECK: %[[RES0:.*]] = "tfl.conv_2d"(%arg0, %arg1, %[[CST]]) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<256x32x32x3xf32>, tensor<16x3x3x3xf32>, none) -> tensor<256x32x32x16xf32> +// CHECK: return %[[RES0]] : tensor<256x32x32x16xf32> + +{ + "version": 3, + "operator_codes": [ + { + "builtin_code": "CONV_2D" + } + ], + "subgraphs": [ + { + "tensors": [ + { + "shape": [ + 256, + 32, + 32, + 3 + ], + "name": "arg0", + "quantization": { + } + }, + { + "shape": [ + 16, + 3, + 3, + 3 + ], + "name": "arg1", + "quantization": { + } + }, + { + "shape": [ + 256, + 32, + 32, + 16 + ], + "name": "output", + "quantization": { + } + } + ], + "inputs": [ + 0, + 1 + ], + "outputs": [ + 2 + ], + "operators": [ + { + "inputs": [ + 0, + 1 + ], + "outputs": [ + 2 + ], + "builtin_options_type": "Conv2DOptions", + "builtin_options": { + "stride_w": 1, + "stride_h": 1 + } + } + ], + "name": "main" + } + ], + "description": "MLIR Converted." +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/output_arrays.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/output_arrays.mlir new file mode 100644 index 0000000000000000000000000000000000000000..921c315e8e21800974d6fad8efd213f55461f067 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/output_arrays.mlir @@ -0,0 +1,19 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate -output-arrays=mul,exp,div --tflite-flatbuffer-to-mlir - -o - | FileCheck %s +// Confirm output-arrays works. + +func.func @main(tensor<4xf32>) -> tensor<4xf32> { +^bb0(%arg0: tensor<4xf32>): + %0 = "tfl.pseudo_const" () {value = dense<2.0> : tensor<4xf32>} : () -> tensor<4xf32> loc("Const") + %1 = "tfl.squared_difference"(%arg0, %0) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> loc("squared_difference") + // CHECK: %[[MUL:.*]] = tfl.mul + %2 = "tfl.mul"(%0, %1) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> loc("mul") + // CHECK: %[[DIV:.*]] = tfl.div + %3 = "tfl.div"(%2, %1) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> loc("div") + // CHECK: %[[EXP:.*]] = "tfl.exp" + %4 = "tfl.exp"(%3) : (tensor<4xf32>) -> tensor<4xf32> loc("exp") + // tfl.neg should not be pruned + // CHECK: %[[NEG:.*]] = "tfl.neg" + %5 = "tfl.neg"(%4) : (tensor<4xf32>) -> tensor<4xf32> loc("neg") + // CHECK: return %[[MUL]], %[[EXP]], %[[DIV]] + func.return %5 : tensor<4xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/pruning.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/pruning.mlir new file mode 100644 index 0000000000000000000000000000000000000000..adf9251548bf64c451cd0b1224e87cd50ba5794a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/pruning.mlir @@ -0,0 +1,19 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate -output-arrays=mul,exp,div --experimental-prune-unreachable-nodes-unconditionally --tflite-flatbuffer-to-mlir - -o - | FileCheck %s +// Confirm graph pruning. + +func.func @main(tensor<4xf32>) -> tensor<4xf32> { +^bb0(%arg0: tensor<4xf32>): + %0 = "tfl.pseudo_const" () {value = dense<2.0> : tensor<4xf32>} : () -> tensor<4xf32> loc("Const") + %1 = "tfl.squared_difference"(%arg0, %0) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> loc("squared_difference") + // CHECK: %[[MUL:.*]] = tfl.mul + %2 = "tfl.mul"(%0, %1) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> loc("mul") + // CHECK: %[[DIV:.*]] = tfl.div + %3 = "tfl.div"(%2, %1) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> loc("div") + // CHECK: %[[EXP:.*]] = "tfl.exp" + %4 = "tfl.exp"(%3) : (tensor<4xf32>) -> tensor<4xf32> loc("exp") + // tfl.neg should be pruned + // CHECK-NOT: "tfl.neg" + %5 = "tfl.neg"(%4) : (tensor<4xf32>) -> tensor<4xf32> loc("neg") + // CHECK: return %[[MUL]], %[[EXP]], %[[DIV]] + func.return %5 : tensor<4xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/pruning_function_input_as_output.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/pruning_function_input_as_output.mlir new file mode 100644 index 0000000000000000000000000000000000000000..6f0d6229240bd3d41073de159e1c050d40fec299 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/pruning_function_input_as_output.mlir @@ -0,0 +1,16 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate -output-arrays=mul,div,exp --experimental-prune-unreachable-nodes-unconditionally --tflite-flatbuffer-to-mlir - -o - | FileCheck %s + +// CHECK: (%[[ARG:.*]]: tensor<4xf32>) -> (tensor<4xf32>, tensor<4xf32>, tensor<4xf32>) +func.func @main(%arg0: tensor<4xf32>) -> tensor<4xf32> attributes {tf.entry_function = {inputs = "mul"}} { + %0 = "tfl.pseudo_const" () {value = dense<1.0> : tensor<4xf32>} : () -> tensor<4xf32> loc("Const") + %1 = "tfl.squared_difference"(%arg0, %0) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> loc("squared_difference") + // CHECK: %[[DIV:.*]] = tfl.div + %2 = "tfl.div"(%1, %arg0) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> loc("div") + // CHECK: %[[EXP:.*]] = "tfl.exp" + %3 = "tfl.exp"(%2) : (tensor<4xf32>) -> tensor<4xf32> loc("exp") + // tfl.neg should be pruned + // CHECK-NOT: "tfl.neg" + %4 = "tfl.neg"(%3) : (tensor<4xf32>) -> tensor<4xf32> loc("neg") + // CHECK: return %[[ARG]], %[[DIV]], %[[EXP]] + func.return %4 : tensor<4xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/quant_stats.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/quant_stats.mlir new file mode 100644 index 0000000000000000000000000000000000000000..b6f1f3c28ee474f467a6bc5f71f4dc06dbd18e4c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/quant_stats.mlir @@ -0,0 +1,10 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s +// Ensure "quantfork.stats" roundtrip exactly + +func.func @main(%arg0: tensor<1x512x672x8xf32>) -> tensor<1x512x672x8xf32> { +// CHECK-LABEL: @main +// CHECK: %[[RES0:.*]] = "quantfork.stats"(%arg0) <{layerStats = dense<[0.000000e+00, 2.550000e+02]> : tensor<2xf32>}> : (tensor<1x512x672x8xf32>) -> tensor<1x512x672x8xf32> + + %0 = "quantfork.stats"(%arg0) {layerStats = dense<[0.000000e+00, 2.550000e+02]> : tensor<2xf32>} : (tensor<1x512x672x8xf32>) -> tensor<1x512x672x8xf32> + func.return %0 : tensor<1x512x672x8xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/quantization.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/quantization.mlir new file mode 100644 index 0000000000000000000000000000000000000000..ddf7173468a78e362a1c57edb4823f9b02d69557 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/quantization.mlir @@ -0,0 +1,35 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s + +// CHECK-LABEL: main +func.func @main(%arg0: tensor<1x224x224x3xf32>) -> tensor<1x401408xf32> { +// CHECK: %{{.*}} = "tfl.quantize"(%{{.*}}) <{qtype = tensor<1x224x224x3x!quant.uniform>}> : (tensor<1x224x224x3xf32>) -> tensor<1x224x224x3x!quant.uniform> +// The float values here doesn't match exactly because double -> float -> double is lossy +// CHECK-NEXT: %{{.*}} = "tfl.pseudo_qconst"() <{qtype = tensor<32x3x3x3x!quant.uniform:f32, 0.021826678{{[0-9]*}}:151>>, value = dense<-76> : tensor<32x3x3x3xi8>}> : () -> tensor<32x3x3x3x!quant.uniform:f32, 0.021826678{{[0-9]*}}:151>> +// CHECK-NEXT: %{{.*}} = "tfl.pseudo_qconst"() <{qtype = tensor<32x!quant.uniform>, value = dense<0> : tensor<32xi32>}> : () -> tensor<32x!quant.uniform> +// CHECK: %{{.*}} = "tfl.dequantize"(%{{.*}}) : (tensor<1x401408x!quant.uniform>) -> tensor<1x401408xf32> + + %cst = arith.constant dense<[1, 401408]> : tensor<2xi32> + %0 = "tfl.quantize"(%arg0) {qtype = tensor<1x224x224x3x!quant.uniform>} : (tensor<1x224x224x3xf32>) -> tensor<1x224x224x3x!quant.uniform> + %1 = "tfl.pseudo_qconst"() {qtype = tensor<32x3x3x3x!quant.uniform:f32, 0.021826678373682216:151>>, value = dense<-76> : tensor<32x3x3x3xi8>} : () -> tensor<32x3x3x3x!quant.uniform:f32, 0.021826678373682216:151>> + %2 = "tfl.pseudo_qconst"() {qtype = tensor<32x!quant.uniform>, value = dense<0> : tensor<32xi32>} : () -> tensor<32x!quant.uniform> + %3 = "tfl.conv_2d"(%0, %1, %2) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 2 : i32, stride_w = 2 : i32} : (tensor<1x224x224x3x!quant.uniform>, tensor<32x3x3x3x!quant.uniform:f32, 0.021826678373682216:151>>, tensor<32x!quant.uniform>) -> tensor<1x112x112x32x!quant.uniform> + %4 = "tfl.reshape"(%3, %cst) : (tensor<1x112x112x32x!quant.uniform>, tensor<2xi32>) -> tensor<1x401408x!quant.uniform> + %5 = "tfl.softmax"(%4) {beta = 1.000000e+00 : f32} : (tensor<1x401408x!quant.uniform>) -> tensor<1x401408x!quant.uniform> + %6 = "tfl.dequantize"(%5) : (tensor<1x401408x!quant.uniform>) -> tensor<1x401408xf32> + func.return %6 : tensor<1x401408xf32> +} + +// CHECK-LABEL: quantized_constant +func.func @quantized_constant(%arg0: tensor<1x2xf32>) -> tensor<2x2xf32> { + %1 = "tfl.quantize"(%arg0) {qtype = tensor<1x2x!quant.uniform>, volatile} : (tensor<1x2xf32>) -> tensor<1x2x!quant.uniform> + %cst = "tfl.pseudo_qconst"() {qtype = tensor<1x2x!quant.uniform>, value = dense<-76> : tensor<1x2xi8>} : () -> tensor<1x2x!quant.uniform> + %2 = "tfl.concatenation"(%1, %cst) {axis = 0 : i32, fused_activation_function = "NONE"} : (tensor<1x2x!quant.uniform>, tensor<1x2x!quant.uniform>) -> tensor<2x2x!quant.uniform> + %3 = "tfl.dequantize"(%2) : (tensor<2x2x!quant.uniform>) -> tensor<2x2xf32> + func.return %3 : tensor<2x2xf32> + +// CHECK-NEXT: %[[Q:.*]] = "tfl.quantize"(%arg0) <{qtype = tensor<1x2x!quant.uniform>}> : (tensor<1x2xf32>) -> tensor<1x2x!quant.uniform> +// CHECK-NEXT: %[[CST:.*]] = "tfl.pseudo_qconst"() <{qtype = tensor<1x2x!quant.uniform>, value = dense<-76> : tensor<1x2xi8>}> : () -> tensor<1x2x!quant.uniform> +// CHECK-NEXT: %[[CONCAT:.*]] = "tfl.concatenation"(%[[Q]], %[[CST]]) <{axis = 0 : i32, fused_activation_function = "NONE"}> : (tensor<1x2x!quant.uniform>, tensor<1x2x!quant.uniform>) -> tensor<2x2x!quant.uniform> +// CHECK-NEXT: %[[DQ:.*]] = "tfl.dequantize"(%[[CONCAT]]) : (tensor<2x2x!quant.uniform>) -> tensor<2x2xf32> +// CHECK-NEXT: return %[[DQ]] : tensor<2x2xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/reshape.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/reshape.mlir new file mode 100644 index 0000000000000000000000000000000000000000..cd1f6a342fb22e5bb9a8051c37be662a75cdccc7 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/reshape.mlir @@ -0,0 +1,11 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s +// Confirm we can extract type info from reshape + +func.func @main() -> tensor<2x2xf32> { + // CHECK: %[[cst:.*]] = "tfl.pseudo_const"() <{value = dense<2> : tensor<2xi32>}> : () -> tensor<2xi32> + // CHECK: %{{.*}} = "tfl.reshape"(%{{.*}}, %[[cst]]) : (tensor<4xf32>, tensor<2xi32>) -> tensor<2x2xf32> + %cst = arith.constant dense<[2, 2]> : tensor<2xi32> + %0 = "tfl.pseudo_const" () {value = dense<1.0> : tensor<4xf32>} : () -> tensor<4xf32> loc("Const") + %1 = "tfl.reshape" (%0, %cst) : (tensor<4xf32>, tensor<2xi32>) -> tensor<2x2xf32> loc("reshape") + func.return %1 : tensor<2x2xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/signature.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/signature.mlir new file mode 100644 index 0000000000000000000000000000000000000000..5d6e6f232285423e3c5ca7c49a5dbbdf4a2639ad --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/signature.mlir @@ -0,0 +1,18 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s + +// CHECK: {tf_saved_model.index_path = ["input2"]} +// CHECK-SAME: {tf_saved_model.index_path = ["input1"]} +// CHECK-SAME: {tf_saved_model.index_path = ["start_logits"]} +// CHECK-SAME: {tf_saved_model.index_path = ["end_logits"]} +// CHECK-SAME: tf.entry_function = {inputs = "serving_default_input2:0,serving_default_input1:0", outputs = "StatefulPartitionedCall:1,StatefulPartitionedCall:0"} +// CHECK-SAME: tf_saved_model.exported_names = ["serving_default"] +module attributes {tf.versions = {bad_consumers = [], min_consumer = 12 : i32, producer = 554 : i32}, tf_saved_model.semantics} { + func.func @main(%arg0: tensor {tf_saved_model.index_path = ["input2"]}, %arg1: tensor {tf_saved_model.index_path = ["input1"]}) -> (tensor {tf_saved_model.index_path = ["start_logits"]}, tensor {tf_saved_model.index_path = ["end_logits"]}) attributes {tf.entry_function = {control_outputs = "", inputs = "serving_default_input2:0,serving_default_input1:0", outputs = "StatefulPartitionedCall:1,StatefulPartitionedCall:0"}, tf_saved_model.exported_names = ["serving_default"]} { + %cst = arith.constant dense<0.000000e+00> : tensor<5xf32> + %cst_0 = arith.constant dense<1.0> : tensor<5x384xf32> + %cst_1 = arith.constant dense<1.0> : tensor<5x384xf32> + %0 = "tfl.fully_connected"(%arg0, %cst_0, %cst) {fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"} : (tensor, tensor<5x384xf32>, tensor<5xf32>) -> tensor + %1 = "tfl.fully_connected"(%arg0, %cst_1, %cst) {fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"} : (tensor, tensor<5x384xf32>, tensor<5xf32>) -> tensor + func.return %1, %0 : tensor, tensor + } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/signature_with_multiple_entry_points.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/signature_with_multiple_entry_points.mlir new file mode 100644 index 0000000000000000000000000000000000000000..b1b22e5451fd0dd6100e2eef1a691e0019457f24 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/signature_with_multiple_entry_points.mlir @@ -0,0 +1,28 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s + +// CHECK: func @add( +// CHECK-SAME: {tf_saved_model.index_path = ["input1"]} +// CHECK-SAME: {tf_saved_model.index_path = ["input2"]} +// CHECK-SAME: {tf_saved_model.index_path = ["result"]} +// CHECK-SAME: tf.entry_function = {inputs = "input1:0,input2:0", outputs = "result:0"} +// CHECK-SAME: tf_saved_model.exported_names = ["add"] + +// CHECK: func @sub( +// CHECK-SAME: {tf_saved_model.index_path = ["input2"]} +// CHECK-SAME: {tf_saved_model.index_path = ["input1"]} +// CHECK-SAME: {tf_saved_model.index_path = ["result"]} +// CHECK-SAME: tf.entry_function = {inputs = "input2:0,input1:0", outputs = "result:0"} +// CHECK-SAME: tf_saved_model.exported_names = ["sub"] + +module attributes {tf.versions = {bad_consumers = [], min_consumer = 12 : i32, producer = 554 : i32}, tf_saved_model.semantics} { + func.func @add(%arg0: tensor {tf_saved_model.index_path = ["input1"]}, %arg1: tensor {tf_saved_model.index_path = ["input2"]}) -> (tensor {tf_saved_model.index_path = ["result"]}) attributes {tf.entry_function = {control_outputs = "", inputs = "input1:0,input2:0", outputs = "result:0"}, tf_saved_model.exported_names = ["add"]} { + %0 = tfl.add %arg0, %arg1 {fused_activation_function = "NONE"} : tensor + %1 = tfl.add %0, %0 {fused_activation_function = "NONE"} : tensor + func.return %1 : tensor + } + + func.func @sub(%arg0: tensor {tf_saved_model.index_path = ["input2"]}, %arg1: tensor {tf_saved_model.index_path = ["input1"]}) -> (tensor {tf_saved_model.index_path = ["result"]}) attributes {tf.entry_function = {control_outputs = "", inputs = "input2:0,input1:0", outputs = "result:0"}, tf_saved_model.exported_names = ["sub"]} { + %0 = tfl.sub %arg0, %arg1 {fused_activation_function = "NONE"} : tensor + func.return %0 : tensor + } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/simple.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/simple.mlir new file mode 100644 index 0000000000000000000000000000000000000000..fb4caaf582b0607e2dcebdf399f2fe7ace5512f3 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/simple.mlir @@ -0,0 +1,24 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - +// TODO(b/329300758): add file check back after the cl is fixed | FileCheck %s +// Check a few basic properties of the import-export, +// including constants retaining their shape +// and the module including the TFLite version. + +func.func @main(tensor<3x2xi32>) -> tensor<3x2xi32> { +^bb0(%arg0: tensor<3x2xi32>): + // CHECK: module attributes + // CHECK-SAME: tfl.description = "MLIR Converted." + // CHECK-SAME: tfl.schema_version = 3 : i32 + + // CHECK: %{{.*}} = "tfl.pseudo_const"() <{value = dense<{{\[\[1, 2\], \[3, 4\], \[5, 6\]\]}}> : tensor<3x2xi32>}> + // CHECK-NEXT: [[SUB:%.*]] = tfl.sub %{{.*}}, %{{.*}} {fused_activation_function = "RELU6"} : tensor<3x2xi32> + // CHECK-NEXT: [[SCALAR:%.*]] = "tfl.pseudo_const"() <{value = dense<10> : tensor}> : () -> tensor + // CHECK-NEXT: [[ADD:%.*]] = tfl.add([[SCALAR]], [[SUB]]) {fused_activation_function = "NONE"} : (tensor, tensor<3x2xi32>) -> tensor<3x2xi32> + // CHECK-NEXT: return [[ADD]] : tensor<3x2xi32> + + %0 = "tfl.pseudo_const" () {value = dense<[[1, 2], [3, 4], [5, 6]]> : tensor<3x2xi32>} : () -> tensor<3x2xi32> loc("Const") + %1 = "tfl.sub" (%arg0, %0) {fused_activation_function = "RELU6"} : (tensor<3x2xi32>, tensor<3x2xi32>) -> tensor<3x2xi32> loc("sub") + %2 = "arith.constant" () {value = dense<10> : tensor} : () -> tensor loc("Const2") + %3 = "tfl.add" (%2, %1) {fused_activation_function = "NONE"} : (tensor, tensor<3x2xi32>) -> tensor<3x2xi32> loc("add") + func.return %3 : tensor<3x2xi32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/test_schema.fbs b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/test_schema.fbs new file mode 100644 index 0000000000000000000000000000000000000000..0ddd114068aabe3f51d4fa92ed80afaf598f081e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/test_schema.fbs @@ -0,0 +1,1091 @@ +// Copyright 2020 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. + +// Revision History +// Version 0: Initial version. +// Version 1: Add subgraphs to schema. +// Version 2: Rename operators to conform to NN API. +// Version 3: Move buffer data from Model.Subgraph.Tensors to Model.Buffers. + +namespace tflite; + +// This corresponds to the version. +file_identifier "TFL3"; +// File extension of any written files. +file_extension "tflite"; + +// IMPORTANT: All new members of tables, enums and unions must be added at the +// end to ensure backwards compatibility. + +// The type of data stored in a tensor. +enum TensorType : byte { + FLOAT32 = 0, + FLOAT16 = 1, + INT32 = 2, + UINT8 = 3, + INT64 = 4, + STRING = 5, + BOOL = 6, + INT16 = 7, + COMPLEX64 = 8, + INT8 = 9, + FLOAT64 = 10, +} + +// Custom quantization parameters for experimenting with new quantization +// techniques. +table CustomQuantization { + custom:[ubyte] (force_align: 16); +} + +// Represents a specific quantization technique's parameters. +union QuantizationDetails { + CustomQuantization, +} + +// Parameters for converting a quantized tensor back to float. +table QuantizationParameters { + // These four parameters are the asymmetric linear quantization parameters. + // Given a quantized value q, the corresponding float value f should be: + // f = scale * (q - zero_point) + // For other quantization types, the QuantizationDetails below is used. + min:[float]; // For importing back into tensorflow. + max:[float]; // For importing back into tensorflow. + scale:[float]; // For dequantizing the tensor's values. + zero_point:[long]; + + // If this is not none, the other quantization parameters (i.e. min, max, + // scale, zero_point fields above) are ignored and the value of the + // QuantizationDetails union should be used. + details:QuantizationDetails; + + // Specifies the dimension of the Tensor's shape that the scales and + // zero_points correspond to. For example, a tensor t, with dims=[4, 3, 2, 1] + // with quantization params: + // scale=[1.0, 2.0, 3.0], zero_point=[1, 2, 3], quantization_dimension=1 + // will be quantized across the second dimension of t. + // t[:, 0, :, :] will have scale[0]=1.0, zero_point[0]=1 + // t[:, 1, :, :] will have scale[1]=2.0, zero_point[0]=2 + // t[:, 2, :, :] will have scale[2]=3.0, zero_point[0]=3 + quantized_dimension:int; +} + +// Sparse tensors. +// We use a modification of the TACO format. +// Reference: http://tensor-compiler.org/kjolstad-oopsla17-tensor-compiler.pdf +// +// To encode a conceptual n-dimensional dense tensor with dims (d0, ..., dn-1), +// potentially with a k-dimensional block (0 <= k <= n) with dims +// (dn, ..., dn+k-1), the format needs to specify: +// 1. In what order to traverse these dimensions. For example, to store a 2-D +// matrix in row major order, the traversal order would be (d0, d1), +// whereas to store it in column major order, the traversal order would be +// (d1, d0). If the 2-D matrix has a 2-D inner block, the traversal order +// could be (d0, d1, d2, d3). +// 2. How each block dimension in (dn, ..., dn+k-1) maps to the original +// tensor dimension in (d0, ..., dn-1). +// 3. In the traversal order defined above, the format (dense vs. sparse) and +// index metadata for each dimension. For a dense dimension, this is just +// the size of that dimension. For a sparse dimension, it's the same as +// the compressed index defined in the Compressed Sparse Row (CSR) format. +// (http://scipy-lectures.org/advanced/scipy_sparse/csr_matrix.html) + +// The storage type for a dimension. Currently we support: +// 1. DENSE: each coordinate in this dimension is stored implicitly. +// 2. SPARSE_CSR: only the coordinates with non-zero elements are stored. The +// compression technique is the same what CSR uses. +// More types like a sparse dimension with a different compression technique +// could be added to the list in the future. +enum DimensionType : byte { + DENSE = 0, + SPARSE_CSR = 1, +} + +table Int32Vector { + values:[int]; +} + +table Uint16Vector { + values:[ushort] (force_align: 4); +} + +table Uint8Vector { + values:[ubyte] (force_align: 4); +} + +// Variable-typed buffer to store the index metadata for a sparse dimension. +// The widest type is Int32 instead of UInt32 because tensor's shape is a int32 +// vector. We don't want the per-dimensional index to overflow that range. +union SparseIndexVector { + Int32Vector, + Uint16Vector, + Uint8Vector +} + +table DimensionMetadata { + // Whether a dimension is dense or sparse. + format:DimensionType; + // Index metadata used for a dimension. + // - If format is DimensionType.DENSE then we use the dense_size field to + // store the size of that dimension. Each index in that dimension is + // stored implicitly. + // - If format is DimensionType.SPARSE_CSR then we use array_segments and + // array_indices to encode that dimension. array_segments represents how + // to segment the indices array, each segment corresponds to one element + // in the previous dimension. array_indices represents the index of the + // non-zero elements within this dimension (as those in the CSR matrix + // format, where the first array is row pointers and the second array is + // column indices). + dense_size:int; + array_segments:SparseIndexVector; + array_indices:SparseIndexVector; +} + +// Parameters to encode a sparse TfLite tensor. +table SparsityParameters { + // The traversal order of the dimensions defined in the `shape` field of the + // conceptual dense tensor. For a n-dimensional tensors with dims (d0, d1, + // ..., dn-1), + // - if not block sparse, the traversal_order is just a permutation of (d0, + // ..., dn-1). For example, a 2-D matrix stored in row-major order would + // have traversal_order = (d0, d1). + // - if block sparse with a k-dimensional block (0 <= k <= n), the + // traversal_order has n + k elements. The first n elements are still a + // permutation of (d0, ..., dn-1). The lask k elements are a permutation + // of (dn, ..., dn+k-1), defining how to traverse a block internally. For + // example, a 2-D matrix with 2-D blocks, both stored in row-major order + // would have traversal_order = (d0, d1, d2, d3). + traversal_order:[int]; + // For an n-dimensional tensor with a k-dimensional block (0 <= k <= n), + // stores how a block dimension in (dn, ..., dn+k-1) maps to the original + // tensor dimension in (d0, ..., dn). + // It's stored in the order of (dn, ..., dn+k-1). + // If not block-sparse, this field is NULL. + block_map:[int]; + // In the traversal order defined above, the metadata needed for + // each dimension to locate the non-zero values in the original dense tensor. + // The size of the dim_metadata array = the size of the traversal_order array + // = n + k. + dim_metadata:[DimensionMetadata]; +} + +table Tensor { + // The tensor shape. The meaning of each entry is operator-specific but + // builtin ops use: [batch size, height, width, number of channels] (That's + // Tensorflow's NHWC). + shape:[int]; + type:TensorType; + // An index that refers to the buffers table at the root of the model. Or, + // if there is no data buffer associated (i.e. intermediate results), then + // this is 0 (which refers to an always existent empty buffer). + // + // The data_buffer itself is an opaque container, with the assumption that the + // target device is little-endian. In addition, all builtin operators assume + // the memory is ordered such that if `shape` is [4, 3, 2], then index + // [i, j, k] maps to data_buffer[i*3*2 + j*2 + k]. + buffer:uint; + name:string; // For debugging and importing back into tensorflow. + quantization:QuantizationParameters; // Optional. + + is_variable:bool = false; + + // Parameters to encode a sparse tensor. See the example in + // tensorflow/lite/testdata/sparse_tensor.json. + sparsity:SparsityParameters; // Optional. + + // Encodes `shape` with unknown dimensions. Unknown dimensions are + // represented with -1. + shape_signature:[int]; // Optional. +} + +// A list of builtin operators. Builtin operators are slightly faster than custom +// ones, but not by much. Moreover, while custom operators accept an opaque +// object containing configuration parameters, builtins have a predetermined +// set of acceptable options. +// LINT.IfChange +enum BuiltinOperator : byte { + ADD = 0, + AVERAGE_POOL_2D = 1, + CONCATENATION = 2, + CONV_2D = 3, + DEPTHWISE_CONV_2D = 4, + DEPTH_TO_SPACE = 5, + DEQUANTIZE = 6, + EMBEDDING_LOOKUP = 7, + FLOOR = 8, + FULLY_CONNECTED = 9, + HASHTABLE_LOOKUP = 10, + L2_NORMALIZATION = 11, + L2_POOL_2D = 12, + LOCAL_RESPONSE_NORMALIZATION = 13, + LOGISTIC = 14, + LSH_PROJECTION = 15, + LSTM = 16, + MAX_POOL_2D = 17, + MUL = 18, + RELU = 19, + // NOTE(aselle): RELU_N1_TO_1 used to be called RELU1, but it was renamed + // since different model developers use RELU1 in different ways. Never + // create another op called RELU1. + RELU_N1_TO_1 = 20, + RELU6 = 21, + RESHAPE = 22, + RESIZE_BILINEAR = 23, + RNN = 24, + SOFTMAX = 25, + SPACE_TO_DEPTH = 26, + SVDF = 27, + TANH = 28, + CONCAT_EMBEDDINGS = 29, + SKIP_GRAM = 30, + CALL = 31, + CUSTOM = 32, + EMBEDDING_LOOKUP_SPARSE = 33, + PAD = 34, + UNIDIRECTIONAL_SEQUENCE_RNN = 35, + GATHER = 36, + BATCH_TO_SPACE_ND = 37, + SPACE_TO_BATCH_ND = 38, + TRANSPOSE = 39, + MEAN = 40, + SUB = 41, + DIV = 42, + SQUEEZE = 43, + UNIDIRECTIONAL_SEQUENCE_LSTM = 44, + STRIDED_SLICE = 45, + BIDIRECTIONAL_SEQUENCE_RNN = 46, + EXP = 47, + TOPK_V2 = 48, + SPLIT = 49, + LOG_SOFTMAX = 50, + // DELEGATE is a special op type for the operations which are delegated to + // other backends. + // WARNING: Experimental interface, subject to change + DELEGATE = 51, + BIDIRECTIONAL_SEQUENCE_LSTM = 52, + CAST = 53, + PRELU = 54, + MAXIMUM = 55, + ARG_MAX = 56, + MINIMUM = 57, + LESS = 58, + NEG = 59, + PADV2 = 60, + GREATER = 61, + GREATER_EQUAL = 62, + LESS_EQUAL = 63, + SELECT = 64, + SLICE = 65, + SIN = 66, + TRANSPOSE_CONV = 67, + SPARSE_TO_DENSE = 68, + TILE = 69, + EXPAND_DIMS = 70, + EQUAL = 71, + NOT_EQUAL = 72, + LOG = 73, + SUM = 74, + SQRT = 75, + RSQRT = 76, + SHAPE = 77, + POW = 78, + ARG_MIN = 79, + FAKE_QUANT = 80, + REDUCE_PROD = 81, + REDUCE_MAX = 82, + PACK = 83, + LOGICAL_OR = 84, + ONE_HOT = 85, + LOGICAL_AND = 86, + LOGICAL_NOT = 87, + UNPACK = 88, + REDUCE_MIN = 89, + FLOOR_DIV = 90, + REDUCE_ANY = 91, + SQUARE = 92, + ZEROS_LIKE = 93, + FILL = 94, + FLOOR_MOD = 95, + RANGE = 96, + RESIZE_NEAREST_NEIGHBOR = 97, + LEAKY_RELU = 98, + SQUARED_DIFFERENCE = 99, + MIRROR_PAD = 100, + ABS = 101, + SPLIT_V = 102, + UNIQUE = 103, + CEIL = 104, + REVERSE_V2 = 105, + ADD_N = 106, + GATHER_ND = 107, + COS = 108, + WHERE = 109, + RANK = 110, + ELU = 111, + REVERSE_SEQUENCE = 112, + MATRIX_DIAG = 113, + QUANTIZE = 114, + MATRIX_SET_DIAG = 115, + ROUND = 116, + HARD_SWISH = 117, + IF = 118, + WHILE = 119, + NON_MAX_SUPPRESSION_V4 = 120, + NON_MAX_SUPPRESSION_V5 = 121, + SCATTER_ND = 122, + SELECT_V2 = 123, + DENSIFY = 124, + SEGMENT_SUM = 125, + BATCH_MATMUL = 126 +} + +// Options for the builtin operators. +union BuiltinOptions { + Conv2DOptions, + DepthwiseConv2DOptions, + ConcatEmbeddingsOptions, + LSHProjectionOptions, + Pool2DOptions, + SVDFOptions, + RNNOptions, + FullyConnectedOptions, + SoftmaxOptions, + ConcatenationOptions, + AddOptions, + L2NormOptions, + LocalResponseNormalizationOptions, + LSTMOptions, + ResizeBilinearOptions, + CallOptions, + ReshapeOptions, + SkipGramOptions, + SpaceToDepthOptions, + EmbeddingLookupSparseOptions, + MulOptions, + PadOptions, + GatherOptions, + BatchToSpaceNDOptions, + SpaceToBatchNDOptions, + TransposeOptions, + ReducerOptions, + SubOptions, + DivOptions, + SqueezeOptions, + SequenceRNNOptions, + StridedSliceOptions, + ExpOptions, + TopKV2Options, + SplitOptions, + LogSoftmaxOptions, + CastOptions, + DequantizeOptions, + MaximumMinimumOptions, + ArgMaxOptions, + LessOptions, + NegOptions, + PadV2Options, + GreaterOptions, + GreaterEqualOptions, + LessEqualOptions, + SelectOptions, + SliceOptions, + TransposeConvOptions, + SparseToDenseOptions, + TileOptions, + ExpandDimsOptions, + EqualOptions, + NotEqualOptions, + ShapeOptions, + PowOptions, + ArgMinOptions, + FakeQuantOptions, + PackOptions, + LogicalOrOptions, + OneHotOptions, + LogicalAndOptions, + LogicalNotOptions, + UnpackOptions, + FloorDivOptions, + SquareOptions, + ZerosLikeOptions, + FillOptions, + BidirectionalSequenceLSTMOptions, + BidirectionalSequenceRNNOptions, + UnidirectionalSequenceLSTMOptions, + FloorModOptions, + RangeOptions, + ResizeNearestNeighborOptions, + LeakyReluOptions, + SquaredDifferenceOptions, + MirrorPadOptions, + AbsOptions, + SplitVOptions, + UniqueOptions, + ReverseV2Options, + AddNOptions, + GatherNdOptions, + CosOptions, + WhereOptions, + RankOptions, + ReverseSequenceOptions, + MatrixDiagOptions, + QuantizeOptions, + MatrixSetDiagOptions, + HardSwishOptions, + IfOptions, + WhileOptions, + DepthToSpaceOptions, + NonMaxSuppressionV4Options, + NonMaxSuppressionV5Options, + ScatterNdOptions, + SelectV2Options, + DensifyOptions, + SegmentSumOptions, + BatchMatMulOptions +} + +enum Padding : byte { SAME, VALID } + +enum ActivationFunctionType : byte { + NONE = 0, + RELU = 1, + RELU_N1_TO_1 = 2, + RELU6 = 3, + TANH = 4, + SIGN_BIT = 5, +} + +table Conv2DOptions { + padding:Padding; + stride_w:int; + stride_h:int; + fused_activation_function:ActivationFunctionType; + dilation_w_factor:int = 1; + dilation_h_factor:int = 1; +} + +table Pool2DOptions { + padding:Padding; + stride_w:int; + stride_h:int; + filter_width:int; + filter_height:int; + fused_activation_function:ActivationFunctionType; +} + +table DepthwiseConv2DOptions { + // Parameters for DepthwiseConv version 1 or above. + padding:Padding; + stride_w:int; + stride_h:int; + // `depth_multiplier` is redundant. It's used by CPU kernels in + // TensorFlow 2.0 or below, but ignored in versions above. + // See comments in lite/c/builtin_op_data.h for more details. + depth_multiplier:int; + fused_activation_function:ActivationFunctionType; + // Parameters for DepthwiseConv version 2 or above. + dilation_w_factor:int = 1; + dilation_h_factor:int = 1; +} + +table ConcatEmbeddingsOptions { + num_channels:int; + num_columns_per_channel:[int]; + embedding_dim_per_channel:[int]; // This could be inferred from parameters. +} + +enum LSHProjectionType: byte { + UNKNOWN = 0, + SPARSE = 1, + DENSE = 2, +} + +table LSHProjectionOptions { + type: LSHProjectionType; +} + +table SVDFOptions { + rank:int; + fused_activation_function:ActivationFunctionType; + // For weights-only quantization, use asymmetric quantization for non + // constant inputs at evaluation time. + asymmetric_quantize_inputs:bool; +} + +// An implementation of TensorFlow RNNCell. +table RNNOptions { + fused_activation_function:ActivationFunctionType; + asymmetric_quantize_inputs:bool; +} + +// An implementation of TensorFlow dynamic_rnn with RNNCell. +table SequenceRNNOptions { + time_major:bool; + fused_activation_function:ActivationFunctionType; + asymmetric_quantize_inputs:bool; +} + +// An implementation of TensorFlow bidrectional_dynamic_rnn with RNNCell. +table BidirectionalSequenceRNNOptions { + time_major:bool; + fused_activation_function:ActivationFunctionType; + merge_outputs: bool; + asymmetric_quantize_inputs:bool; +} + +enum FullyConnectedOptionsWeightsFormat: byte { + DEFAULT = 0, + SHUFFLED4x16INT8 = 1, +} + +// An implementation of TensorFlow fully_connected (a.k.a Dense) layer. +table FullyConnectedOptions { + // Parameters for FullyConnected version 1 or above. + fused_activation_function:ActivationFunctionType; + + // Parameters for FullyConnected version 2 or above. + weights_format:FullyConnectedOptionsWeightsFormat = DEFAULT; + + // Parameters for FullyConnected version 5 or above. + // If set to true, then the number of dimension is preserved. Furthermore, + // all but the last dimension of the input and output shapes will be equal. + keep_num_dims: bool; + + // Parameters for FullyConnected version 7 or above. + // If set to true, then weights-only op will use asymmetric quantization for + // inputs. + asymmetric_quantize_inputs: bool; +} + +table SoftmaxOptions { + beta: float; +} + +// An implementation of TensorFlow concat. +table ConcatenationOptions { + axis:int; + fused_activation_function:ActivationFunctionType; +} + +table AddOptions { + fused_activation_function:ActivationFunctionType; +} + +table MulOptions { + fused_activation_function:ActivationFunctionType; +} + +table L2NormOptions { + fused_activation_function:ActivationFunctionType; +} + +table LocalResponseNormalizationOptions { + radius:int; + bias:float; + alpha:float; + beta:float; +} + +enum LSTMKernelType : byte { + // Full LSTM kernel which supports peephole and projection. + FULL = 0, + // Basic LSTM kernels. Equivalent to TensorFlow BasicLSTMCell. + BASIC = 1, +} + +// An implementation of TensorFlow LSTMCell and CoupledInputForgetGateLSTMCell +table LSTMOptions { + // Parameters for LSTM version 1 or above. + fused_activation_function:ActivationFunctionType; + cell_clip: float; // Optional, 0.0 means no clipping + proj_clip: float; // Optional, 0.0 means no clipping + + // Parameters for LSTM version 2 or above. + // Basic kernel is only supported in version 2 or above. + kernel_type: LSTMKernelType = FULL; + + // Parameters for LSTM version 4 or above. + asymmetric_quantize_inputs: bool; +} + +// An implementation of TensorFlow dynamic_rnn with LSTMCell. +table UnidirectionalSequenceLSTMOptions { + fused_activation_function:ActivationFunctionType; + cell_clip: float; // Optional, 0.0 means no clipping + proj_clip: float; // Optional, 0.0 means no clipping + + // If true then first dimension is sequence, otherwise batch. + time_major:bool; + + // Parameter for Unidirectional Sequence LSTM version 4. + asymmetric_quantize_inputs:bool; +} + +table BidirectionalSequenceLSTMOptions { + // Parameters supported by version 1: + fused_activation_function:ActivationFunctionType; + cell_clip: float; // Optional, 0.0 means no clipping + proj_clip: float; // Optional, 0.0 means no clipping + + // If true, store the outputs of both directions into the first output. + merge_outputs: bool; + + // Parameters supported by version 2: + // If true then first dimension is sequence, otherwise batch. + // Version 1 implementations assumed time_major to be true, so this default + // value should never change. + time_major: bool = true; + + // Parameters for version 3 or above. + asymmetric_quantize_inputs:bool; +} + +table ResizeBilinearOptions { + new_height: int (deprecated); + new_width: int (deprecated); + align_corners: bool; + half_pixel_centers: bool; +} + +table ResizeNearestNeighborOptions { + align_corners: bool; +} + +// A call operation options +table CallOptions { + // The subgraph index that needs to be called. + subgraph:uint; +} + +table PadOptions { +} + +table PadV2Options { +} + +table ReshapeOptions { + new_shape:[int]; +} + +table SpaceToBatchNDOptions { +} + +table BatchToSpaceNDOptions { +} + +table SkipGramOptions { + ngram_size: int; + max_skip_size: int; + include_all_ngrams: bool; +} + +table SpaceToDepthOptions { + block_size: int; +} + +table DepthToSpaceOptions { + block_size: int; +} + +table SubOptions { + fused_activation_function:ActivationFunctionType; +} + +table DivOptions { + fused_activation_function:ActivationFunctionType; +} + +table TopKV2Options { +} + +enum CombinerType : byte { + SUM = 0, + MEAN = 1, + SQRTN = 2, +} + +table EmbeddingLookupSparseOptions { + combiner:CombinerType; +} + +table GatherOptions { + axis: int; +} + +table TransposeOptions { +} + +table ExpOptions { +} + +table CosOptions { +} + +table ReducerOptions { + keep_dims: bool; +} + +table SqueezeOptions { + squeeze_dims:[int]; +} + +table SplitOptions { + num_splits: int; +} + +table SplitVOptions { + num_splits: int; +} + +table StridedSliceOptions { + begin_mask: int; + end_mask: int; + ellipsis_mask: int; + new_axis_mask: int; + shrink_axis_mask: int; +} + +table LogSoftmaxOptions { +} + +table CastOptions { + in_data_type: TensorType; + out_data_type: TensorType; +} + +table DequantizeOptions { +} + +table MaximumMinimumOptions { +} + +table TileOptions { +} + +table ArgMaxOptions { + output_type : TensorType; +} + +table ArgMinOptions { + output_type : TensorType; +} + +table GreaterOptions { +} + +table GreaterEqualOptions { +} + +table LessOptions { +} + +table LessEqualOptions { +} + +table NegOptions { +} + +table SelectOptions { +} + +table SliceOptions { +} + +table TransposeConvOptions { + padding:Padding; + stride_w:int; + stride_h:int; +} + +table ExpandDimsOptions { +} + +table SparseToDenseOptions { + validate_indices:bool; +} + +table EqualOptions { +} + +table NotEqualOptions { +} + +table ShapeOptions { + // Optional output type of the operation (int32 or int64). Defaults to int32. + out_type : TensorType; +} + +table RankOptions { +} + +table PowOptions { +} + +table FakeQuantOptions { + // Parameters supported by version 1: + min:float; + max:float; + num_bits:int; + + // Parameters supported by version 2: + narrow_range:bool; +} + +table PackOptions { + values_count:int; + axis:int; +} + +table LogicalOrOptions { +} + +table OneHotOptions { + axis:int; +} + +table AbsOptions { +} + + +table HardSwishOptions { +} + +table LogicalAndOptions { +} + +table LogicalNotOptions { +} + +table UnpackOptions { + num:int; + axis:int; +} + +table FloorDivOptions { +} + +table SquareOptions { +} + +table ZerosLikeOptions { +} + +table FillOptions { +} + +table FloorModOptions { +} + +table RangeOptions { +} + +table LeakyReluOptions { + alpha:float; +} + +table SquaredDifferenceOptions { +} + +enum MirrorPadMode : byte { + // Doesn't include borders. + REFLECT = 0, + // Includes borders. + SYMMETRIC = 1, +} + +table MirrorPadOptions { + mode:MirrorPadMode; +} + +table UniqueOptions { + idx_out_type:TensorType = INT32; +} + +table ReverseV2Options { +} + +table AddNOptions { +} + +table GatherNdOptions { +} + +table WhereOptions { +} + +table ReverseSequenceOptions { + seq_dim:int; + batch_dim:int = 0; +} + +table MatrixDiagOptions { +} + +table QuantizeOptions { +} + +table MatrixSetDiagOptions { +} + +table IfOptions { + then_subgraph_index:int; + else_subgraph_index:int; +} + +table WhileOptions { + cond_subgraph_index:int; + body_subgraph_index:int; +} + +table NonMaxSuppressionV4Options { +} + +table NonMaxSuppressionV5Options { +} + +table ScatterNdOptions { +} + +table SelectV2Options { +} + +table DensifyOptions { +} + +table SegmentSumOptions { +} + +table BatchMatMulOptions { + adjoint_lhs:bool; + adjoint_rhs:bool; +} + +// An OperatorCode can be an enum value (BuiltinOperator) if the operator is a +// builtin, or a string if the operator is custom. +table OperatorCode { + builtin_code:BuiltinOperator; + custom_code:string; + + // The version of the operator. The version need to be bumped whenever new + // parameters are introduced into an op. + version:int = 1; +} + +enum CustomOptionsFormat : byte { + FLEXBUFFERS = 0, +} + +// An operator takes tensors as inputs and outputs. The type of operation being +// performed is determined by an index into the list of valid OperatorCodes, +// while the specifics of each operations is configured using builtin_options +// or custom_options. +table Operator { + // Index into the operator_codes array. Using an integer here avoids + // complicate map lookups. + opcode_index:uint; + + // Optional input are indicated by -1. + inputs:[int]; + outputs:[int]; + + builtin_options:BuiltinOptions; + custom_options:[ubyte]; + custom_options_format:CustomOptionsFormat; + + // A list of booleans indicating the input tensors which are being mutated by + // this operator.(e.g. used by RNN and LSTM). + // For example, if the "inputs" array refers to 5 tensors and the second and + // fifth are mutable variables, then this list will contain + // [false, true, false, false, true]. + // + // If the list is empty, no variable is mutated in this operator. + // The list either has the same length as `inputs`, or is empty. + mutating_variable_inputs:[bool]; + + // A list of indices to the subgraph's "tensors" that are internal to an Op. + // Internal tensors are those that do not flow in or out of the operation, + // but instead are part of internal computation. As such, the operation's + // implementation may manage its memory more efficiently. They are needed + // however (i.e. not just an implementation detail) since they are part of the + // computation, which may require relevant metadata such as quantization + // parameters. + intermediates:[int]; +} + +// The root type, defining a subgraph, which typically represents an entire +// model. +table SubGraph { + // A list of all tensors used in this subgraph. + tensors:[Tensor]; + + // Indices of the tensors that are inputs into this subgraph. Note this is + // the list of non-static tensors that feed into the subgraph for inference. + inputs:[int]; + + // Indices of the tensors that are outputs out of this subgraph. Note this is + // the list of output tensors that are considered the product of the + // subgraph's inference. + outputs:[int]; + + // All operators, in execution order. + operators:[Operator]; + + // Name of this subgraph (used for debugging). + name:string; +} + +// Table of raw data buffers (used for constant tensors). Referenced by tensors +// by index. The generous alignment accommodates mmap-friendly data structures. +table Buffer { + data:[ubyte] (force_align: 16); +} + +table Metadata { + // A human readable string to uniquely identify a Metadata. + name:string; + // An index to the buffers table. + buffer:uint; +} + +table Model { + // Version of the schema. + version:uint; + + // A list of all operator codes used in this model. This is + // kept in order because operators carry an index into this + // vector. + operator_codes:[OperatorCode]; + + // All the subgraphs of the model. The 0th is assumed to be the main + // model. + subgraphs:[SubGraph]; + + // A description of the model. + description:string; + + // Buffers of the model. + // Note the 0th entry of this array must be an empty buffer (sentinel). + // This is a convention so that tensors without a buffer can provide 0 as + // their buffer. + buffers:[Buffer]; + + // Metadata about the model. Indirects into the existings buffers list. + // Deprecated, prefer to use metadata field. + metadata_buffer:[int]; + + // Metadata about the model. + metadata:[Metadata]; +} + +root_type Model; diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/tf_variant_type.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/tf_variant_type.mlir new file mode 100644 index 0000000000000000000000000000000000000000..92c48adf97547777416dd3e60985cf5a7408df60 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/tf_variant_type.mlir @@ -0,0 +1,8 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s + +func.func @main(%arg0 : tensor>>, %arg1: tensor>>) -> tensor>> { + func.return %arg0 : tensor>> +} + +// CHECK: func.func @main(%[[ARG0:.*]]: tensor>>, %[[ARG1:.*]]: tensor>>) -> tensor>> +// CHECK-NEXT: return %[[ARG0]] : tensor>> diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/unranked_function_output.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/unranked_function_output.mlir new file mode 100644 index 0000000000000000000000000000000000000000..1aeca0fc189a77025e6a1d369b9842bf373daa63 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/unranked_function_output.mlir @@ -0,0 +1,28 @@ +// RUN: litert-opt --tfl-legalize-tf-while %s -o - | flatbuffer_translate -mlir-to-tflite-flatbuffer - -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s + +// This test is to test for unranked function output from input, the output type should be compatible with input type. + +// CHECK: func @main(%arg0: tensor<1xf32>) -> tensor<*xf32> attributes {tf.entry_function = {inputs = "arg0", outputs = "tfl.while"}} { +// CHECK: %0 = "tfl.while"(%arg0) ({ +// CHECK: ^bb0(%arg1: tensor<*xf32>): +// CHECK: %[[RES0:.*]] = func.call @cond(%arg1) : (tensor<*xf32>) -> tensor<*xf32> +// CHECK: "tfl.yield"(%[[RES0]]) : (tensor<*xf32>) -> () +// CHECK: }, { +// CHECK: ^bb0(%arg1: tensor<*xf32>): +// CHECK: %[[RES1:.*]] = func.call @body(%arg1) : (tensor<*xf32>) -> tensor<*xf32> +// CHECK: "tfl.yield"(%[[RES1]]) : (tensor<*xf32>) -> () +// CHECK: }) : (tensor<1xf32>) -> tensor<*xf32> +// CHECK: return %0 : tensor<*xf32> +// CHECK: } +func.func @main(%arg0: tensor<1xf32>) -> tensor<*xf32> { + %0 = "tf.While"(%arg0) {cond = @cond, body = @body, is_stateless = false} : (tensor<1xf32>) -> tensor<*xf32> + func.return %0 : tensor<*xf32> +} + +func.func @cond(%arg1: tensor<*xf32>) -> tensor<*xf32> { + func.return %arg1: tensor<*xf32> +} + +func.func @body(%arg1: tensor<*xf32>) -> tensor<*xf32> { + func.return %arg1: tensor<*xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/unranked_tensor.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/unranked_tensor.mlir new file mode 100644 index 0000000000000000000000000000000000000000..ed0128f8d43f6e53070c068768877da7ee66cac9 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/unranked_tensor.mlir @@ -0,0 +1,10 @@ +// RUN: litert-opt --tfl-legalize-tf-while %s -o - | flatbuffer_translate -mlir-to-tflite-flatbuffer - -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s + +// CHECK-LABEL: main +func.func @main(%arg0: tensor<*xf32>) -> tensor<*xf32> { + // CHECK: %{{.*}} = tfl.add %{{.*}}, %{{.*}} {fused_activation_function = "NONE"} : tensor<*xf32> + // CHECK: return %{{.*}} : tensor<*xf32> + + %0 = tfl.add(%arg0, %arg0) {fused_activation_function = "NONE"} : (tensor<*xf32>, tensor<*xf32>) -> tensor<*xf32> + return %0 : tensor<*xf32> +} \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/variable.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/variable.mlir new file mode 100644 index 0000000000000000000000000000000000000000..13ca1e9f784b98e7e4cf5fe58d14e631a96e4ad3 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/variable.mlir @@ -0,0 +1,8 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s + +// CHECK-LABEL: main +func.func @main() -> tensor<3x2xi32> { + // CHECK: "tfl.pseudo_const"() <{value = dense<0> : tensor<3x2xi32>}> {tfl.is_variable} : () -> tensor<3x2xi32> + %0 = "tfl.pseudo_const"() {value = dense<0> : tensor<3x2xi32>, tfl.is_variable} : () -> tensor<3x2xi32> loc("variable") + func.return %0 : tensor<3x2xi32> +} \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/vhlo.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/vhlo.mlir new file mode 100644 index 0000000000000000000000000000000000000000..e9163ea7dac98b8737f47c200d3319c28ba2fde2 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/vhlo.mlir @@ -0,0 +1,570 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer --emit-stablehlo-ops=true %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir --disable-vhlo-to-stablehlo=true - -o - | FileCheck %s +// test stablehlo roundtrip + +// Identity function to make the exporter happy +func.func @main(%arg0: tensor<4xi8>) -> tensor<4xi8> { + func.return %arg0 : tensor<4xi8> +} + +//CHECK:func.func @main(%arg0: tensor<4xi8>) -> tensor<4xi8> attributes {tf.entry_function = {inputs = "arg0", outputs = "arg0"}} { +//CHECK: return %arg0 : tensor<4xi8> +//CHECK:} + +func.func @logistic(%arg0: tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> { + %0 = "vhlo.logistic_v1"(%arg0) : (tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> + func.return %0 : tensor<1x1x1x96xf32> +} + +// CHECK:func.func private @logistic(%arg0: tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> { +// CHECK: %0 = "vhlo.logistic_v1"(%arg0) : (tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> +// CHECK: return %0 : tensor<1x1x1x96xf32> +// CHECK:} + +func.func @add(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>) -> tensor<1xf32> { + %0 = "vhlo.add_v1"(%arg0, %arg1) : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + func.return %0 : tensor<1xf32> +} + +// CHECK:func.func private @add(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>) -> tensor<1xf32> { +// CHECK: %0 = "vhlo.add_v1"(%arg0, %arg1) : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> +// CHECK: return %0 : tensor<1xf32> +// CHECK:} + +func.func @multiply(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>) -> tensor<1xf32> { + %0 = "vhlo.multiply_v1"(%arg0, %arg1) : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + func.return %0 : tensor<1xf32> +} + +// CHECK:func.func private @multiply(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>) -> tensor<1xf32> { +// CHECK: %0 = "vhlo.multiply_v1"(%arg0, %arg1) : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> +// CHECK: return %0 : tensor<1xf32> +// CHECK:} + +func.func @divide(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>) -> tensor<1xf32> { + %0 = "vhlo.divide_v1"(%arg0, %arg1) : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + func.return %0 : tensor<1xf32> +} + +// CHECK:func.func private @divide(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>) -> tensor<1xf32> { +// CHECK: %0 = "vhlo.divide_v1"(%arg0, %arg1) : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> +// CHECK: return %0 : tensor<1xf32> +// CHECK:} + +func.func @maximum(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>) -> tensor<1xf32> { + %0 = "vhlo.maximum_v1"(%arg0, %arg1) : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + func.return %0 : tensor<1xf32> +} + +// CHECK:func.func private @maximum(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>) -> tensor<1xf32> { +// CHECK: %0 = "vhlo.maximum_v1"(%arg0, %arg1) : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> +// CHECK: return %0 : tensor<1xf32> +// CHECK:} + +func.func @reshape(%arg0 : tensor<1x128xi32>) -> tensor<4x32x1xi32>{ + %0 = "vhlo.reshape_v1"(%arg0) : (tensor<1x128xi32>) -> tensor<4x32x1xi32> + func.return %0 : tensor<4x32x1xi32> +} + +//CHECK:func.func private @reshape(%arg0: tensor<1x128xi32>) -> tensor<4x32x1xi32> { +//CHECK-NEXT: %0 = "vhlo.reshape_v1"(%arg0) : (tensor<1x128xi32>) -> tensor<4x32x1xi32> +//CHECK-NEXT: return %0 : tensor<4x32x1xi32> +//CHECK-NEXT:} + +func.func @clamp(%arg0: tensor, %arg1: tensor<1x256x256x24xf32>, %arg2: tensor) -> tensor<1x256x256x24xf32>{ + %0 = "vhlo.clamp_v1"(%arg0, %arg1, %arg2) : (tensor, tensor<1x256x256x24xf32>, tensor) -> tensor<1x256x256x24xf32> + return %0 : tensor<1x256x256x24xf32> +} + +//CHECK:func.func private @clamp(%arg0: tensor, %arg1: tensor<1x256x256x24xf32>, %arg2: tensor) -> tensor<1x256x256x24xf32> { +//CHECK-NEXT: %0 = "vhlo.clamp_v1"(%arg0, %arg1, %arg2) : (tensor, tensor<1x256x256x24xf32>, tensor) -> tensor<1x256x256x24xf32> +//CHECK-NEXT: return %0 : tensor<1x256x256x24xf32> +//CHECK-NEXT:} + +func.func @concat(%arg0: tensor<1x30x1xi32>, %arg1: tensor<1x30x1xi32>) -> tensor<1x30x2xi32> { + %0 = "vhlo.concatenate_v1"(%arg0, %arg1) <{dimension = #vhlo.integer_v1<2 : i64>}> : (tensor<1x30x1xi32>, tensor<1x30x1xi32>) -> tensor<1x30x2xi32> + func.return %0 : tensor<1x30x2xi32> +} + +//CHECK:func.func private @concat(%arg0: tensor<1x30x1xi32>, %arg1: tensor<1x30x1xi32>) -> tensor<1x30x2xi32> { +//CHECK-NEXT: %0 = "vhlo.concatenate_v1"(%arg0, %arg1) <{dimension = #vhlo.integer_v1<2 : i64>}> : (tensor<1x30x1xi32>, tensor<1x30x1xi32>) -> tensor<1x30x2xi32> +//CHECK-NEXT: return %0 : tensor<1x30x2xi32> +//CHECK-NEXT:} + +func.func @broadcast_in_dim(%arg0: tensor<1x32x256xf32>) -> tensor<4x32x256xf32>{ + %0 = "vhlo.broadcast_in_dim_v1"(%arg0) <{broadcast_dimensions = #vhlo.tensor_v1 : tensor<3xi64>>}> : (tensor<1x32x256xf32>) -> tensor<4x32x256xf32> + return %0 : tensor<4x32x256xf32> +} + +//CHECK:func.func private @broadcast_in_dim(%arg0: tensor<1x32x256xf32>) -> tensor<4x32x256xf32> { +//CHECK-NEXT: %0 = "vhlo.broadcast_in_dim_v1"(%arg0) <{broadcast_dimensions = #vhlo.tensor_v1 : tensor<3xi64>>}> : (tensor<1x32x256xf32>) -> tensor<4x32x256xf32> +//CHECK-NEXT: return %0 : tensor<4x32x256xf32> +//CHECK-NEXT:} + +func.func @slice(%arg0: tensor<160x20x1xf32>) -> tensor<1x1x1xf32> { + %0 = "vhlo.slice_v1"(%arg0) <{limit_indices = #vhlo.tensor_v1 : tensor<3xi64>>, + start_indices = #vhlo.tensor_v1 : tensor<3xi64>>, + strides = #vhlo.tensor_v1 : tensor<3xi64>>}> : (tensor<160x20x1xf32>) -> tensor<1x1x1xf32> + return %0 : tensor<1x1x1xf32> +} + +//CHECK:func.func private @slice(%arg0: tensor<160x20x1xf32>) -> tensor<1x1x1xf32> { +//CHECK-NEXT: %0 = "vhlo.slice_v1"(%arg0) <{limit_indices = #vhlo.tensor_v1 : tensor<3xi64>>, start_indices = #vhlo.tensor_v1 : tensor<3xi64>>, strides = #vhlo.tensor_v1 : tensor<3xi64>>}> : (tensor<160x20x1xf32>) -> tensor<1x1x1xf32> +//CHECK-NEXT: return %0 : tensor<1x1x1xf32> +//CHECK-NEXT:} + +func.func @convolution(%arg0: tensor<1x1x1600x32xf32>, %arg1: tensor<1x13x1x32xf32>) -> tensor<1x1x1600x32xf32> { + %0 = "vhlo.convolution_v1"(%arg0, %arg1) <{batch_group_count = #vhlo.integer_v1<1 : i64>, + feature_group_count = #vhlo.integer_v1<32 : i64>, + input_batch_dimension = #vhlo.integer_v1<0 : i64>, + input_feature_dimension = #vhlo.integer_v1<3 : i64>, + input_spatial_dimensions = #vhlo.tensor_v1 : tensor<2xi64>>, + kernel_input_feature_dimension = #vhlo.integer_v1<2 : i64>, + kernel_output_feature_dimension = #vhlo.integer_v1<3 : i64>, + kernel_spatial_dimensions = #vhlo.tensor_v1 : tensor<2xi64>>, + lhs_dilation = #vhlo.tensor_v1 : tensor<2xi64>>, + output_batch_dimension = #vhlo.integer_v1<0 : i64>, + output_feature_dimension = #vhlo.integer_v1<3 : i64>, + output_spatial_dimensions = #vhlo.tensor_v1 : tensor<2xi64>>, + padding = #vhlo.tensor_v1 : tensor<2x2xi64>>, + precision_config = #vhlo.array_v1<[#vhlo, #vhlo]>, + rhs_dilation = #vhlo.tensor_v1 : tensor<2xi64>>, + window_reversal = #vhlo.tensor_v1 : tensor<2xi1>>, + window_strides = #vhlo.tensor_v1 : tensor<2xi64>>}> + : (tensor<1x1x1600x32xf32>, tensor<1x13x1x32xf32>) -> tensor<1x1x1600x32xf32> + return %0 : tensor<1x1x1600x32xf32> +} + +//CHECK:func.func private @convolution(%arg0: tensor<1x1x1600x32xf32>, %arg1: tensor<1x13x1x32xf32>) -> tensor<1x1x1600x32xf32> { +//CHECK: %0 = "vhlo.convolution_v1"(%arg0, %arg1) <{ +//CHECK-SAME: batch_group_count = #vhlo.integer_v1<1 : i64>, +//CHECK-SAME: feature_group_count = #vhlo.integer_v1<32 : i64>, +//CHECK-SAME: input_batch_dimension = #vhlo.integer_v1<0 : i64>, +//CHECK-SAME: input_feature_dimension = #vhlo.integer_v1<3 : i64>, +//CHECK-SAME: input_spatial_dimensions = #vhlo.tensor_v1 : tensor<2xi64>>, +//CHECK-SAME: kernel_input_feature_dimension = #vhlo.integer_v1<2 : i64>, +//CHECK-SAME: kernel_output_feature_dimension = #vhlo.integer_v1<3 : i64>, +//CHECK-SAME: kernel_spatial_dimensions = #vhlo.tensor_v1 : tensor<2xi64>>, +//CHECK-SAME: lhs_dilation = #vhlo.tensor_v1 : tensor<2xi64>>, +//CHECK-SAME: output_batch_dimension = #vhlo.integer_v1<0 : i64>, +//CHECK-SAME: output_feature_dimension = #vhlo.integer_v1<3 : i64>, +//CHECK-SAME: output_spatial_dimensions = #vhlo.tensor_v1 : tensor<2xi64>>, +//CHECK-SAME: padding = #vhlo.tensor_v1 : tensor<2x2xi64>>, +//CHECK-SAME: precision_config = #vhlo.array_v1<[#vhlo, #vhlo]>, +//CHECK-SAME: rhs_dilation = #vhlo.tensor_v1 : tensor<2xi64>>, +//CHECK-SAME: window_reversal = #vhlo.tensor_v1 : tensor<2xi1>>, +//CHECK-SAME: window_strides = #vhlo.tensor_v1 : tensor<2xi64>>}> : (tensor<1x1x1600x32xf32>, tensor<1x13x1x32xf32>) -> tensor<1x1x1600x32xf32> +//CHECK-NEXT: return %0 : tensor<1x1x1600x32xf32> +//CHECK-NEXT:} + +func.func @reduce(%arg0: tensor<1x16x16x320xf32>, %arg3 : tensor) -> tensor<1x320xf32> { + %0 = "vhlo.reduce_v1" (%arg0, %arg3) <{ + dimensions = #vhlo.tensor_v1 : tensor<2xi64>> + }> ({ + ^bb0(%arg1: tensor<1xf32>, %arg2: tensor<1xf32>): + %421 = "vhlo.add_v1"(%arg1, %arg2) : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + "vhlo.return_v1" (%421) : (tensor<1xf32>) -> () + }) : (tensor<1x16x16x320xf32>, tensor) -> tensor<1x320xf32> + return %0 : tensor<1x320xf32> +} + +//CHECK:func.func private @reduce(%arg0: tensor<1x16x16x320xf32>, %arg1: tensor) -> tensor<1x320xf32> { +//CHECK-NEXT: %0 = "vhlo.reduce_v1"(%arg0, %arg1) <{dimensions = #vhlo.tensor_v1 : tensor<2xi64>>}> ({ +//CHECK-NEXT: ^bb0(%arg2: tensor<1xf32>, %arg3: tensor<1xf32>): +//CHECK-NEXT: %1 = "vhlo.add_v1"(%arg2, %arg3) : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> +//CHECK-NEXT: "vhlo.return_v1"(%1) : (tensor<1xf32>) -> () +//CHECK-NEXT: }) : (tensor<1x16x16x320xf32>, tensor) -> tensor<1x320xf32> +//CHECK-NEXT: return %0 : tensor<1x320xf32> +//CHECK-NEXT:} + +func.func @abs(%arg0: tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> { + %0 = "vhlo.abs_v1" (%arg0) : (tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> + func.return %0 : tensor<1x1x1x96xf32> +} + +//CHECK:func.func private @abs(%arg0: tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> { +//CHECK-NEXT: %0 = "vhlo.abs_v1"(%arg0) : (tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> +//CHECK-NEXT: return %0 : tensor<1x1x1x96xf32> +//CHECK-NEXT:} + +func.func @and(%arg0: tensor<1x30x1xi32>, %arg1: tensor<1x30x1xi32>) -> tensor<1x30x1xi32> { + %0 = "vhlo.and_v1" (%arg0, %arg1) : (tensor<1x30x1xi32>, tensor<1x30x1xi32>) -> tensor<1x30x1xi32> + func.return %0 : tensor<1x30x1xi32> +} + +//CHECK:func.func private @and(%arg0: tensor<1x30x1xi32>, %arg1: tensor<1x30x1xi32>) -> tensor<1x30x1xi32> { +//CHECK-NEXT: %0 = "vhlo.and_v1"(%arg0, %arg1) : (tensor<1x30x1xi32>, tensor<1x30x1xi32>) -> tensor<1x30x1xi32> +//CHECK-NEXT: return %0 : tensor<1x30x1xi32> +//CHECK-NEXT:} + +func.func @cos(%arg0: tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> { + %0 = "vhlo.cosine_v1" (%arg0) : (tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> + func.return %0 : tensor<1x1x1x96xf32> +} + +//CHECK:func.func private @cos(%arg0: tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> { +//CHECK-NEXT: %0 = "vhlo.cosine_v1"(%arg0) : (tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> +//CHECK-NEXT: return %0 : tensor<1x1x1x96xf32> +//CHECK-NEXT:} + +func.func @exp(%arg0: tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> { + %0 = "vhlo.exponential_v1" (%arg0) : (tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> + func.return %0 : tensor<1x1x1x96xf32> +} + +//CHECK:func.func private @exp(%arg0: tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> { +//CHECK-NEXT: %0 = "vhlo.exponential_v1"(%arg0) : (tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> +//CHECK-NEXT: return %0 : tensor<1x1x1x96xf32> +//CHECK-NEXT:} + +func.func @floor(%arg0: tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> { + %0 = "vhlo.floor_v1" (%arg0) : (tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> + func.return %0 : tensor<1x1x1x96xf32> +} + +//CHECK:func.func private @floor(%arg0: tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> { +//CHECK-NEXT: %0 = "vhlo.floor_v1"(%arg0) : (tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> +//CHECK-NEXT: return %0 : tensor<1x1x1x96xf32> +//CHECK-NEXT:} + +func.func @log(%arg0: tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> { + %0 = "vhlo.log_v1" (%arg0) : (tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> + func.return %0 : tensor<1x1x1x96xf32> +} + +//CHECK:func.func private @log(%arg0: tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> { +//CHECK-NEXT: %0 = "vhlo.log_v1"(%arg0) : (tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> +//CHECK-NEXT: return %0 : tensor<1x1x1x96xf32> +//CHECK-NEXT:} + +func.func @min(%arg0: tensor<1x30x1xi32>, %arg1: tensor<1x30x1xi32>) -> tensor<1x30x1xi32> { + %0 = "vhlo.minimum_v1" (%arg0, %arg1) : (tensor<1x30x1xi32>, tensor<1x30x1xi32>) -> tensor<1x30x1xi32> + func.return %0 : tensor<1x30x1xi32> +} + +//CHECK:func.func private @min(%arg0: tensor<1x30x1xi32>, %arg1: tensor<1x30x1xi32>) -> tensor<1x30x1xi32> { +//CHECK-NEXT: %0 = "vhlo.minimum_v1"(%arg0, %arg1) : (tensor<1x30x1xi32>, tensor<1x30x1xi32>) -> tensor<1x30x1xi32> +//CHECK-NEXT: return %0 : tensor<1x30x1xi32> +//CHECK-NEXT:} + +func.func @neg(%arg0: tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> { + %0 = "vhlo.negate_v1" (%arg0) : (tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> + func.return %0 : tensor<1x1x1x96xf32> +} + +//CHECK:func.func private @neg(%arg0: tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> { +//CHECK-NEXT: %0 = "vhlo.negate_v1"(%arg0) : (tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> +//CHECK-NEXT: return %0 : tensor<1x1x1x96xf32> +//CHECK-NEXT:} + +func.func @or(%arg0: tensor<1x30x1xi32>, %arg1: tensor<1x30x1xi32>) -> tensor<1x30x1xi32> { + %0 = "vhlo.or_v1" (%arg0, %arg1) : (tensor<1x30x1xi32>, tensor<1x30x1xi32>) -> tensor<1x30x1xi32> + func.return %0 : tensor<1x30x1xi32> +} + +//CHECK:func.func private @or(%arg0: tensor<1x30x1xi32>, %arg1: tensor<1x30x1xi32>) -> tensor<1x30x1xi32> { +//CHECK-NEXT: %0 = "vhlo.or_v1"(%arg0, %arg1) : (tensor<1x30x1xi32>, tensor<1x30x1xi32>) -> tensor<1x30x1xi32> +//CHECK-NEXT: return %0 : tensor<1x30x1xi32> +//CHECK-NEXT:} + +func.func @power(%arg0: tensor<1x30x1xi32>, %arg1: tensor<1x30x1xi32>) -> tensor<1x30x1xi32> { + %0 = "vhlo.power_v1" (%arg0, %arg1) : (tensor<1x30x1xi32>, tensor<1x30x1xi32>) -> tensor<1x30x1xi32> + func.return %0 : tensor<1x30x1xi32> +} + +//CHECK:func.func private @power(%arg0: tensor<1x30x1xi32>, %arg1: tensor<1x30x1xi32>) -> tensor<1x30x1xi32> { +//CHECK-NEXT: %0 = "vhlo.power_v1"(%arg0, %arg1) : (tensor<1x30x1xi32>, tensor<1x30x1xi32>) -> tensor<1x30x1xi32> +//CHECK-NEXT: return %0 : tensor<1x30x1xi32> +//CHECK-NEXT:} + +func.func @remainder(%arg0: tensor<1x30x1xi32>, %arg1: tensor<1x30x1xi32>) -> tensor<1x30x1xi32> { + %0 = "vhlo.remainder_v1" (%arg0, %arg1) : (tensor<1x30x1xi32>, tensor<1x30x1xi32>) -> tensor<1x30x1xi32> + func.return %0 : tensor<1x30x1xi32> +} + +//CHECK:func.func private @remainder(%arg0: tensor<1x30x1xi32>, %arg1: tensor<1x30x1xi32>) -> tensor<1x30x1xi32> { +//CHECK-NEXT: %0 = "vhlo.remainder_v1"(%arg0, %arg1) : (tensor<1x30x1xi32>, tensor<1x30x1xi32>) -> tensor<1x30x1xi32> +//CHECK-NEXT: return %0 : tensor<1x30x1xi32> +//CHECK-NEXT:} + +func.func @rsqrt(%arg0: tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> { + %0 = "vhlo.rsqrt_v1" (%arg0) : (tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> + func.return %0 : tensor<1x1x1x96xf32> +} + +//CHECK:func.func private @rsqrt(%arg0: tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> { +//CHECK-NEXT: %0 = "vhlo.rsqrt_v1"(%arg0) : (tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> +//CHECK-NEXT: return %0 : tensor<1x1x1x96xf32> +//CHECK-NEXT:} + +func.func @select(%arg0: tensor<1x30x1xi1>, %arg1: tensor<1x30x1xi32>, %arg2: tensor<1x30x1xi32>) -> tensor<1x30x1xi32> { + %0 = "vhlo.select_v1" (%arg0, %arg1, %arg2) : (tensor<1x30x1xi1>, tensor<1x30x1xi32>, tensor<1x30x1xi32>) -> tensor<1x30x1xi32> + func.return %0 : tensor<1x30x1xi32> +} + +//CHECK:func.func private @select(%arg0: tensor<1x30x1xi1>, %arg1: tensor<1x30x1xi32>, %arg2: tensor<1x30x1xi32>) -> tensor<1x30x1xi32> { +//CHECK-NEXT: %0 = "vhlo.select_v1"(%arg0, %arg1, %arg2) : (tensor<1x30x1xi1>, tensor<1x30x1xi32>, tensor<1x30x1xi32>) -> tensor<1x30x1xi32> +//CHECK-NEXT: return %0 : tensor<1x30x1xi32> +//CHECK-NEXT:} + +func.func @sub(%arg0: tensor<1x30x1xi32>, %arg1: tensor<1x30x1xi32>) -> tensor<1x30x1xi32> { + %0 = "vhlo.subtract_v1" (%arg0, %arg1) : (tensor<1x30x1xi32>, tensor<1x30x1xi32>) -> tensor<1x30x1xi32> + func.return %0 : tensor<1x30x1xi32> +} + +//CHECK:func.func private @sub(%arg0: tensor<1x30x1xi32>, %arg1: tensor<1x30x1xi32>) -> tensor<1x30x1xi32> { +//CHECK-NEXT: %0 = "vhlo.subtract_v1"(%arg0, %arg1) : (tensor<1x30x1xi32>, tensor<1x30x1xi32>) -> tensor<1x30x1xi32> +//CHECK-NEXT: return %0 : tensor<1x30x1xi32> +//CHECK-NEXT:} + +func.func @tanh(%arg0: tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> { + %0 = "vhlo.tanh_v1" (%arg0) : (tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> + func.return %0 : tensor<1x1x1x96xf32> +} + +//CHECK:func.func private @tanh(%arg0: tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> { +//CHECK-NEXT: %0 = "vhlo.tanh_v1"(%arg0) : (tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> +//CHECK-NEXT: return %0 : tensor<1x1x1x96xf32> +//CHECK-NEXT:} + +func.func @iota() -> tensor<3x4xf32> { + %0 = "vhlo.iota_v1" () <{iota_dimension = #vhlo.integer_v1<0 : i64>}> : () -> tensor<3x4xf32> + return %0 : tensor<3x4xf32> +} + +//CHECK:func.func private @iota() -> tensor<3x4xf32> { +//CHECK-NEXT: %0 = "vhlo.iota_v1"() <{iota_dimension = #vhlo.integer_v1<0 : i64>}> : () -> tensor<3x4xf32> +//CHECK-NEXT: return %0 : tensor<3x4xf32> +//CHECK-NEXT:} + +func.func @compare(%arg0: tensor, %arg1: tensor) -> tensor { + %0 = "vhlo.compare_v1" (%arg0, %arg1) <{compare_type = #vhlo, comparison_direction = #vhlo}> : (tensor, tensor) -> tensor + func.return %0 : tensor +} + +//CHECK:func.func private @compare(%arg0: tensor, %arg1: tensor) -> tensor { +//CHECK-NEXT: %0 = "vhlo.compare_v1"(%arg0, %arg1) <{compare_type = #vhlo, comparison_direction = #vhlo}> : (tensor, tensor) -> tensor +//CHECK-NEXT: return %0 : tensor +//CHECK-NEXT:} + +func.func @dynamic_update_slice(%arg0: tensor<4x4xi64>, %arg1: tensor<2x3xi64>, %arg2: tensor, %arg3: tensor) -> tensor<4x4xi64> { + %0 = "vhlo.dynamic_update_slice_v1"(%arg0, %arg1, %arg2, %arg3) : (tensor<4x4xi64>, tensor<2x3xi64>, tensor, tensor) -> tensor<4x4xi64> + return %0 : tensor<4x4xi64> +} + +//CHECK:func.func private @dynamic_update_slice(%arg0: tensor<4x4xi64>, %arg1: tensor<2x3xi64>, %arg2: tensor, %arg3: tensor) -> tensor<4x4xi64> { +//CHECK-NEXT: %0 = "vhlo.dynamic_update_slice_v1"(%arg0, %arg1, %arg2, %arg3) : (tensor<4x4xi64>, tensor<2x3xi64>, tensor, tensor) -> tensor<4x4xi64> +//CHECK-NEXT: return %0 : tensor<4x4xi64> +//CHECK-NEXT:} + +func.func @dyanmic_slice(%arg0: tensor<3x3xi64>, %arg1: tensor, %arg2: tensor) -> tensor<3x3xi64> { + %0 = "vhlo.dynamic_slice_v1"(%arg0, %arg1, %arg2) <{ + slice_sizes = #vhlo.tensor_v1 : tensor<2xi64>> + }> : (tensor<3x3xi64>, tensor, tensor) -> tensor<3x3xi64> + return %0 : tensor<3x3xi64> +} + +//CHECK:func.func private @dyanmic_slice(%arg0: tensor<3x3xi64>, %arg1: tensor, %arg2: tensor) -> tensor<3x3xi64> { +//CHECK-NEXT: %0 = "vhlo.dynamic_slice_v1"(%arg0, %arg1, %arg2) <{ +//CHECK-SAME: slice_sizes = #vhlo.tensor_v1 : tensor<2xi64>> +//CHECK-SAME: }> : (tensor<3x3xi64>, tensor, tensor) -> tensor<3x3xi64> +//CHECK-NEXT: return %0 : tensor<3x3xi64> +//CHECK-NEXT:} + +func.func @pad(%arg0: tensor<1x160x1xf32>, %arg1: tensor) -> tensor<1x161x1xf32> { + %0 = "vhlo.pad_v1" (%arg0, %arg1) <{edge_padding_low = #vhlo.tensor_v1 : tensor<3xi64>>, + edge_padding_high = #vhlo.tensor_v1 : tensor<3xi64>>, + interior_padding = #vhlo.tensor_v1 : tensor<3xi64>>}> : (tensor<1x160x1xf32>, tensor) -> tensor<1x161x1xf32> + return %0 : tensor<1x161x1xf32> +} + +//CHECK:func.func private @pad(%arg0: tensor<1x160x1xf32>, %arg1: tensor) -> tensor<1x161x1xf32> { +//CHECK-NEXT: %0 = "vhlo.pad_v1"(%arg0, %arg1) <{edge_padding_high = #vhlo.tensor_v1 : tensor<3xi64>>, +//CHECK-SAME: edge_padding_low = #vhlo.tensor_v1 : tensor<3xi64>>, +//CHECK-SAME: interior_padding = #vhlo.tensor_v1 : tensor<3xi64>>}> : (tensor<1x160x1xf32>, tensor) -> tensor<1x161x1xf32> +//CHECK-NEXT: return %0 : tensor<1x161x1xf32> +//CHECK-NEXT:} + +func.func @convert(%arg0: tensor<2xf64>) -> tensor<2xf32> { + %0 = "vhlo.convert_v1" (%arg0) : (tensor<2xf64>) -> tensor<2xf32> + return %0 : tensor<2xf32> +} + +//CHECK:func.func private @convert(%arg0: tensor<2xf64>) -> tensor<2xf32> { +//CHECK-NEXT: %0 = "vhlo.convert_v1"(%arg0) : (tensor<2xf64>) -> tensor<2xf32> +//CHECK-NEXT: return %0 : tensor<2xf32> +//CHECK-NEXT:} + +func.func @reduce_window(%arg0: tensor<1x160x1xf32>, %arg1: tensor) -> tensor<1x160x1xf32> { + %0 = "vhlo.reduce_window_v1"(%arg0, %arg1) <{base_dilations = #vhlo.tensor_v1 : tensor<3xi64>>, + padding = #vhlo.tensor_v1 : tensor<3x2xi64>>, + window_dilations = #vhlo.tensor_v1 : tensor<3xi64>>, + window_dimensions = #vhlo.tensor_v1 : tensor<3xi64>>, + window_strides = #vhlo.tensor_v1 : tensor<3xi64>>}> ({ + ^bb0(%arg23: tensor, %arg24: tensor): + %1112 = "vhlo.add_v1" (%arg23, %arg24) : (tensor, tensor) -> tensor + "vhlo.return_v1" (%1112) : (tensor) -> () + }) : (tensor<1x160x1xf32>, tensor) -> tensor<1x160x1xf32> + return %0 : tensor<1x160x1xf32> +} + +//CHECK:func.func private @reduce_window(%arg0: tensor<1x160x1xf32>, %arg1: tensor) -> tensor<1x160x1xf32> { +//CHECK-NEXT: %0 = "vhlo.reduce_window_v1"(%arg0, %arg1) <{base_dilations = #vhlo.tensor_v1 : tensor<3xi64>>, +//CHECK-SAME{LITERAL}: padding = #vhlo.tensor_v1 : tensor<3x2xi64>>, +//CHECK-SAME: window_dilations = #vhlo.tensor_v1 : tensor<3xi64>>, +//CHECK-SAME: window_dimensions = #vhlo.tensor_v1 : tensor<3xi64>>, +//CHECK-SAME: window_strides = #vhlo.tensor_v1 : tensor<3xi64>>}> ({ +//CHECK-NEXT: ^bb0(%arg2: tensor, %arg3: tensor): +//CHECK-NEXT: %1 = "vhlo.add_v1"(%arg2, %arg3) : (tensor, tensor) -> tensor +//CHECK-NEXT: "vhlo.return_v1"(%1) : (tensor) -> () +//CHECK-NEXT: }) : (tensor<1x160x1xf32>, tensor) -> tensor<1x160x1xf32> +//CHECK-NEXT: return %0 : tensor<1x160x1xf32> +//CHECK-NEXT:} + +func.func @dot_general(%arg0: tensor<1x1x167xf32>, %arg1: tensor<167x64xf32>) -> tensor<1x1x64xf32> { + %0 = "vhlo.dot_general_v1"(%arg0, %arg1) <{ + lhs_batching_dimensions = #vhlo.tensor_v1 : tensor<1xi64>>, + lhs_contracting_dimensions = #vhlo.tensor_v1 : tensor<1xi64>>, + rhs_batching_dimensions = #vhlo.tensor_v1 : tensor<1xi64>>, + rhs_contracting_dimensions = #vhlo.tensor_v1 : tensor<1xi64>>, + precision_config = #vhlo.array_v1<[#vhlo, #vhlo]>}> : (tensor<1x1x167xf32>, tensor<167x64xf32>) -> tensor<1x1x64xf32> + return %0 : tensor<1x1x64xf32> +} + +//CHECK:func.func private @dot_general(%arg0: tensor<1x1x167xf32>, %arg1: tensor<167x64xf32>) -> tensor<1x1x64xf32> { +//CHECK-NEXT: %0 = "vhlo.dot_general_v1"(%arg0, %arg1) <{ +//CHECK-SAME: lhs_batching_dimensions = #vhlo.tensor_v1 : tensor<1xi64>>, +//CHECK-SAME: lhs_contracting_dimensions = #vhlo.tensor_v1 : tensor<1xi64>>, +//CHECK-SAME: precision_config = #vhlo.array_v1<[#vhlo, #vhlo]>, +//CHECK-SAME: rhs_batching_dimensions = #vhlo.tensor_v1 : tensor<1xi64>>, +//CHECK-SAME: rhs_contracting_dimensions = #vhlo.tensor_v1 : tensor<1xi64>>}> : (tensor<1x1x167xf32>, tensor<167x64xf32>) -> tensor<1x1x64xf32> +//CHECK-NEXT: return %0 : tensor<1x1x64xf32> +//CHECK-NEXT:} + +func.func @sort(%arg0: tensor<448xf32>, %arg1: tensor<448xi32>) -> tensor<448xf32> { + %0, %1 = "vhlo.sort_v1"(%arg0, %arg1) <{dimension = #vhlo.integer_v1<0 : i64>, is_stable = #vhlo.bool_v1}> ({ + ^bb0(%arg23: tensor, %arg24: tensor, %arg25: tensor, %arg26: tensor): + %1112 = "vhlo.compare_v1"(%arg23, %arg24) <{compare_type = #vhlo, comparison_direction = #vhlo}> : (tensor, tensor) -> tensor + "vhlo.return_v1"(%1112) : (tensor) -> () + }) : (tensor<448xf32>, tensor<448xi32>) -> (tensor<448xf32>, tensor<448xi32>) + return %0 : tensor<448xf32> +} + +//CHECK:func.func private @sort(%arg0: tensor<448xf32>, %arg1: tensor<448xi32>) -> tensor<448xf32> { +//CHECK-NEXT: %0:2 = "vhlo.sort_v1"(%arg0, %arg1) <{dimension = #vhlo.integer_v1<0 : i64>, is_stable = #vhlo.bool_v1}> ({ +//CHECK-NEXT: ^bb0(%arg2: tensor, %arg3: tensor, %arg4: tensor, %arg5: tensor): +//CHECK-NEXT: %1 = "vhlo.compare_v1"(%arg2, %arg3) <{compare_type = #vhlo, comparison_direction = #vhlo}> : (tensor, tensor) -> tensor +//CHECK-NEXT: "vhlo.return_v1"(%1) : (tensor) -> () +//CHECK-NEXT: }) : (tensor<448xf32>, tensor<448xi32>) -> (tensor<448xf32>, tensor<448xi32>) +//CHECK-NEXT: return %0#0 : tensor<448xf32> +//CHECK-NEXT:} + +func.func @while(%init_i: tensor, %init_sum: tensor) -> tensor{ + %0, %1 = "vhlo.while_v1"(%init_i, %init_sum) ({ + ^bb0(%arg0: tensor, %arg1: tensor): + %cond = "vhlo.compare_v1"(%arg0, %arg1) { + comparison_direction = #vhlo, + compare_type = #vhlo + } : (tensor, tensor) -> tensor + "vhlo.return_v1"(%cond) : (tensor) -> () + }, { + ^bb0(%arg0: tensor, %arg1: tensor): + %new_sum = "vhlo.add_v1"(%arg1, %arg1) : (tensor, tensor) -> tensor + %new_i = "vhlo.add_v1" (%arg0, %arg1) : (tensor, tensor) -> tensor + "vhlo.return_v1"(%new_i, %new_sum) : (tensor, tensor) ->() + }) : (tensor, tensor) -> (tensor, tensor) + return %0 : tensor +} + +//CHECK:func.func private @while(%arg0: tensor, %arg1: tensor) -> tensor { +//CHECK-NEXT: %0:2 = "vhlo.while_v1"(%arg0, %arg1) ({ +//CHECK-NEXT: ^bb0(%arg2: tensor, %arg3: tensor): +//CHECK-NEXT: %1 = "vhlo.compare_v1"(%arg2, %arg3) <{compare_type = #vhlo, comparison_direction = #vhlo}> : (tensor, tensor) -> tensor +//CHECK-NEXT: "vhlo.return_v1"(%1) : (tensor) -> () +//CHECK-NEXT: }, { +//CHECK-NEXT: ^bb0(%arg2: tensor, %arg3: tensor): +//CHECK-NEXT: %1 = "vhlo.add_v1"(%arg3, %arg3) : (tensor, tensor) -> tensor +//CHECK-NEXT: %2 = "vhlo.add_v1"(%arg2, %arg3) : (tensor, tensor) -> tensor +//CHECK-NEXT: "vhlo.return_v1"(%2, %1) : (tensor, tensor) -> () +//CHECK-NEXT: }) : (tensor, tensor) -> (tensor, tensor) +//CHECK-NEXT: return %0#0 : tensor +//CHECK-NEXT:} + +func.func @gather(%operand: tensor<3x4x2xi32>, %start_indices: tensor<2x3x2xi64>) -> tensor<2x3x2x2xi32>{ + %result = "vhlo.gather_v1"(%operand, %start_indices) <{ + offset_dims = #vhlo.tensor_v1 : tensor<2xi64>>, + collapsed_slice_dims = #vhlo.tensor_v1 : tensor<1xi64>>, + start_index_map = #vhlo.tensor_v1 : tensor<2xi64>>, + index_vector_dim = #vhlo.integer_v1<2 : i64>, + slice_sizes = #vhlo.tensor_v1 : tensor<3xi64>>, + indices_are_sorted = #vhlo.bool_v1 + }> : (tensor<3x4x2xi32>, tensor<2x3x2xi64>) -> tensor<2x3x2x2xi32> + return %result : tensor<2x3x2x2xi32> +} + + +// CHECK: func.func private @gather(%arg0: tensor<3x4x2xi32>, %arg1: tensor<2x3x2xi64>) -> tensor<2x3x2x2xi32> { +// CHECK-NEXT: %0 = "vhlo.gather_v1"(%arg0, %arg1) <{collapsed_slice_dims = #vhlo.tensor_v1 : tensor<1xi64>>, index_vector_dim = #vhlo.integer_v1<2 : i64>, indices_are_sorted = #vhlo.bool_v1, offset_dims = #vhlo.tensor_v1 : tensor<2xi64>>, slice_sizes = #vhlo.tensor_v1 : tensor<3xi64>>, start_index_map = #vhlo.tensor_v1 : tensor<2xi64>>}> : (tensor<3x4x2xi32>, tensor<2x3x2xi64>) -> tensor<2x3x2x2xi32> +// CHECK-NEXT: return %0 : tensor<2x3x2x2xi32> +// CHECK-NEXT:} + +func.func @transpose(%arg0: tensor<2x3x2xi32>) -> tensor<2x3x2xi32> { + %0 = "vhlo.transpose_v1"(%arg0) <{permutation = #vhlo.tensor_v1 : tensor<3xi64>>}> : (tensor<2x3x2xi32>) -> tensor<2x3x2xi32> + return %0 : tensor<2x3x2xi32> +} + +// CHECK:func.func private @transpose(%arg0: tensor<2x3x2xi32>) -> tensor<2x3x2xi32> { +// CHECK-NEXT: %0 = "vhlo.transpose_v1"(%arg0) <{permutation = #vhlo.tensor_v1 : tensor<3xi64>>}> : (tensor<2x3x2xi32>) -> tensor<2x3x2xi32> +// CHECK-NEXT: return %0 : tensor<2x3x2xi32> +// CHECK-NEXT:} + +func.func @rng_bit_generator(%arg0: tensor<2xui64>) -> (tensor<2xui64>, tensor<10x12xui32>) { + %output_state, %output = "vhlo.rng_bit_generator_v1"(%arg0) <{rng_algorithm = #vhlo}> : (tensor<2xui64>) -> (tensor<2xui64>, tensor<10x12xui32>) + func.return %output_state, %output : tensor<2xui64>, tensor<10x12xui32> +} + +// CHECK:func.func private @rng_bit_generator(%arg0: tensor<2xui64>) -> (tensor<2xui64>, tensor<10x12xui32>) { +// CHECK-NEXT: %output_state, %output = "vhlo.rng_bit_generator_v1"(%arg0) <{rng_algorithm = #vhlo}> : (tensor<2xui64>) -> (tensor<2xui64>, tensor<10x12xui32>) +// CHECK-NEXT: return %output_state, %output : tensor<2xui64>, tensor<10x12xui32> +// CHECK-NEXT:} + +func.func @scatter(%input_tensor: tensor<200x100x300xf32>, + %scatter_indices: tensor<10x2xi32>, %updates: tensor<10x300xf32>) -> + tensor<200x100x300xf32> { + %0 = "vhlo.scatter_v1" (%input_tensor, %scatter_indices, %updates) <{ + update_window_dims = #vhlo.tensor_v1 : tensor<1xi64>>, + inserted_window_dims = #vhlo.tensor_v1 : tensor<2xi64>>, + scatter_dims_to_operand_dims = #vhlo.tensor_v1 : tensor<2xi64>>, + index_vector_dim = #vhlo.integer_v1<1 : i64>, + indices_are_sorted = #vhlo.bool_v1, + unique_indices = #vhlo.bool_v1}> ({ + ^bb0(%lhs: tensor, %rhs: tensor): + "vhlo.return_v1"(%lhs) : (tensor) -> () + }): (tensor<200x100x300xf32>, tensor<10x2xi32>, tensor<10x300xf32>) -> + tensor<200x100x300xf32> + func.return %0 : tensor<200x100x300xf32> +} + +// CHECK-LABEL: func.func private @scatter(%arg0: tensor<200x100x300xf32>, %arg1: tensor<10x2xi32>, %arg2: tensor<10x300xf32>) -> tensor<200x100x300xf32> { +// CHECK-NEXT: %0 = "vhlo.scatter_v1"(%arg0, %arg1, %arg2) <{index_vector_dim = #vhlo.integer_v1<1 : i64>, indices_are_sorted = #vhlo.bool_v1, inserted_window_dims = #vhlo.tensor_v1 : tensor<2xi64>>, scatter_dims_to_operand_dims = #vhlo.tensor_v1 : tensor<2xi64>>, unique_indices = #vhlo.bool_v1, update_window_dims = #vhlo.tensor_v1 : tensor<1xi64>>}> ({ +// CHECK-NEXT: ^bb0(%arg3: tensor, %arg4: tensor): +// CHECK-NEXT: "vhlo.return_v1"(%arg3) : (tensor) -> () +// CHECK-NEXT: }) : (tensor<200x100x300xf32>, tensor<10x2xi32>, tensor<10x300xf32>) -> tensor<200x100x300xf32> +// CHECK-NEXT: return %0 : tensor<200x100x300xf32> +// CHECK-NEXT: } + +// CHECK-LABEL: @shift_left +func.func @shift_left(%arg0: tensor<2x2xi32>, %arg1: tensor<2x2xi32>) -> tensor<2x2xi32> { + %0 = "vhlo.shift_left_v1"(%arg0, %arg1) : (tensor<2x2xi32>, tensor<2x2xi32>) -> tensor<2x2xi32> + func.return %0 : tensor<2x2xi32> +} + +// CHECK: "vhlo.shift_left_v1"(%arg0, %arg1) : (tensor<2x2xi32>, tensor<2x2xi32>) -> tensor<2x2xi32> + +// CHECK-LABEL: @cbrt +func.func @cbrt(%arg0: tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> { + %0 = "vhlo.cbrt_v1" (%arg0) : (tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> + func.return %0 : tensor<1x1x1x96xf32> +} + +//CHECK: %0 = "vhlo.cbrt_v1"(%arg0) : (tensor<1x1x1x96xf32>) -> tensor<1x1x1x96xf32> +//CHECK: return %0 : tensor<1x1x1x96xf32> diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/vhlo_const.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/vhlo_const.mlir new file mode 100644 index 0000000000000000000000000000000000000000..f5e9e7dc96c3057c8a8bf01ecd13005c152f86b1 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/vhlo_const.mlir @@ -0,0 +1,14 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer --emit-stablehlo-ops=true %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir --disable-vhlo-to-stablehlo=true - -o - | FileCheck %s +// test stablehlo roundtrip + +module attributes {tfl.metadata = {"keep_stablehlo_constant" = "true"}} { + func.func @main () -> tensor<1x1x1x96xf32> { + %0 = "vhlo.constant_v1"() <{value = #vhlo.tensor_v1 : tensor>}> : () -> tensor<1x1x1x96xf32> + func.return %0 : tensor<1x1x1x96xf32> + } +} + +//CHECK: func.func @main() -> tensor<1x1x1x96xf32> attributes {tf.entry_function = {outputs = "vhlo.constant_v1"}} { +//CHECK-NEXT: %0 = "vhlo.constant_v1"() <{value = #vhlo.tensor_v1 : tensor<1x1x1x96xf32>>}> : () -> tensor<1x1x1x96xf32> +//CHECK-NEXT: return %0 : tensor<1x1x1x96xf32> +//CHECK-NEXT: } \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/vhlo_custom_call.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/vhlo_custom_call.mlir new file mode 100644 index 0000000000000000000000000000000000000000..04b8840540398c6ef2f5cb2af18312b68b86d7ca --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/vhlo_custom_call.mlir @@ -0,0 +1,63 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer --emit-stablehlo-ops=true %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir --disable-vhlo-to-stablehlo=true - -o - | FileCheck %s +// test stablehlo roundtrip + +//test TF ops wrapped in stablehlo custom_call + +// Identity function to make the exporter happy +func.func @main(%arg0: tensor<4xi8>) -> tensor<4xi8> { + func.return %arg0 : tensor<4xi8> +} + +//CHECK:func.func @main(%arg0: tensor<4xi8>) -> tensor<4xi8> attributes {tf.entry_function = {inputs = "arg0", outputs = "arg0"}} { +//CHECK: return %arg0 : tensor<4xi8> +//CHECK:} + +func.func @custom_tf_op(%arg0: tensor<1x320x1x1xf32>, %arg1: tensor<2xi32>) -> tensor<1x1600x1x1xf32> { + %0 = "vhlo.custom_call_v1" (%arg0, %arg1) <{api_version = #vhlo, + backend_config = #vhlo.string_v1<"">, + call_target_name = #vhlo.string_v1<"tf.ResizeBilinear">, + called_computations = #vhlo.array_v1<[]>, + has_side_effect = #vhlo.bool_v1, + operand_layouts = #vhlo.array_v1<[]>, + output_operand_aliases = #vhlo.array_v1<[]>, + result_layouts = #vhlo.array_v1<[]>}> {align_corners = #vhlo.bool_v1, device = #vhlo.string_v1<"">, half_pixel_centers = #vhlo.bool_v1} : (tensor<1x320x1x1xf32>, tensor<2xi32>) -> tensor<1x1600x1x1xf32> + return %0 : tensor<1x1600x1x1xf32> +} + +//CHECK:func.func private @custom_tf_op(%arg0: tensor<1x320x1x1xf32>, %arg1: tensor<2xi32>) -> tensor<1x1600x1x1xf32> { +//CHECK: %0 = "vhlo.custom_call_v1"(%arg0, %arg1) <{ +//CHECK-SAME: api_version = #vhlo, +//CHECK-SAME: backend_config = #vhlo.string_v1<"">, +//CHECK-SAME: call_target_name = #vhlo.string_v1<"tf.ResizeBilinear">, +//CHECK-SAME: called_computations = #vhlo.array_v1<[]>, +//CHECK-SAME: has_side_effect = #vhlo.bool_v1, +//CHECK-SAME: operand_layouts = #vhlo.array_v1<[]>, +//CHECK-SAME: output_operand_aliases = #vhlo.array_v1<[]>, +//CHECK-SAME: result_layouts = #vhlo.array_v1<[]>}> {align_corners = #vhlo.bool_v1, device = #vhlo.string_v1<"">, half_pixel_centers = #vhlo.bool_v1} : (tensor<1x320x1x1xf32>, tensor<2xi32>) -> tensor<1x1600x1x1xf32> +//CHECK-NEXT: return %0 : tensor<1x1600x1x1xf32> +//CHECK-NEXT:} + +func.func @custom_op_with_backend(%arg0: tensor<1x320x1x1xf32>, %arg1: tensor<2xi32>) -> tensor<1x1600x1x1xf32> { + %0 = "vhlo.custom_call_v1" (%arg0, %arg1) <{api_version = #vhlo, + backend_config = #vhlo.string_v1<"">, + call_target_name = #vhlo.string_v1<"custom_backend">, + called_computations = #vhlo.array_v1<[]>, + has_side_effect = #vhlo.bool_v1, + operand_layouts = #vhlo.array_v1<[]>, + output_operand_aliases = #vhlo.array_v1<[]>, + result_layouts = #vhlo.array_v1<[]>}> : (tensor<1x320x1x1xf32>, tensor<2xi32>) -> tensor<1x1600x1x1xf32> + return %0 : tensor<1x1600x1x1xf32> +} + +//CHECK:func.func private @custom_op_with_backend(%arg0: tensor<1x320x1x1xf32>, %arg1: tensor<2xi32>) -> tensor<1x1600x1x1xf32> { +//CHECK: "vhlo.custom_call_v1"(%arg0, %arg1) <{ +//CHECK-SAME: api_version = #vhlo, +//CHECK-SAME: backend_config = #vhlo.string_v1<"">, +//CHECK-SAME: call_target_name = #vhlo.string_v1<"custom_backend">, +//CHECK-SAME: called_computations = #vhlo.array_v1<[]>, +//CHECK-SAME: has_side_effect = #vhlo.bool_v1, +//CHECK-SAME: operand_layouts = #vhlo.array_v1<[]>, +//CHECK-SAME: output_operand_aliases = #vhlo.array_v1<[]>, +//CHECK-SAME: result_layouts = #vhlo.array_v1<[]>}> : (tensor<1x320x1x1xf32>, tensor<2xi32>) -> tensor<1x1600x1x1xf32> +//CHECK-NEXT: return %0 : tensor<1x1600x1x1xf32> +//CHECK-NEXT:} \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/while_op.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/while_op.mlir new file mode 100644 index 0000000000000000000000000000000000000000..c36181f61987576cbfcaf60b97dd35402098ebac --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/flatbuffer2mlir/while_op.mlir @@ -0,0 +1,37 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s + +// Check to see if nested regions in while loops are preserved +// CHECK: %{{.*}}:2 = "tfl.while"(%{{.*}}, %{{.*}}) ({ +// CHECK: ^bb0(%{{.*}}: tensor<*xi32>, %{{.*}}: tensor<*xf32>): +// CHECK: "tfl.yield"(%{{.*}}) : (tensor) -> () +// CHECK: }, { +// CHECK: ^bb0(%{{.*}}: tensor<*xi32>, %{{.*}}: tensor<*xf32>): +// CHECK: "tfl.yield"(%{{.*}}, %{{.*}}) : (tensor<*xi32>, tensor<*xf32>) -> () +// CHECK: }) : (tensor, tensor<1xf32>) -> (tensor, tensor<1xf32>) + +func.func @main(%arg0: tensor, %arg1: tensor<1xf32>) -> tensor<1xf32> { + // While %arg0 is greater than zero, element wise add %arg1 with itself. + %0:2 = "tfl.while"(%arg0, %arg1) ({ + ^bb0(%arg2: tensor<*xi32>, %arg3: tensor<*xf32>): + %1 = func.call @cond(%arg2, %arg3) : (tensor<*xi32>, tensor<*xf32>) -> tensor + "tfl.yield"(%1) : (tensor) -> () + }, { + ^bb0(%arg2: tensor<*xi32>, %arg3: tensor<*xf32>): + %1:2 = func.call @body(%arg2, %arg3) : (tensor<*xi32>, tensor<*xf32>) -> (tensor<*xi32>, tensor<*xf32>) + "tfl.yield"(%1#0, %1#1) : (tensor<*xi32>, tensor<*xf32>) -> () + }) {is_stateless = false} : (tensor, tensor<1xf32>) -> (tensor, tensor<1xf32>) + func.return %0#1 : tensor<1xf32> +} + +func.func @cond(%arg0: tensor<*xi32>, %arg1: tensor<*xf32>) -> tensor { + %cst = arith.constant dense<0> : tensor loc("Const") + %0 = "tfl.greater"(%arg0, %cst) : (tensor<*xi32>, tensor) -> tensor + func.return %0 : tensor +} + +func.func @body(%arg0: tensor<*xi32>, %arg1: tensor<*xf32>) -> (tensor<*xi32>, tensor<*xf32>) { + %cst = arith.constant dense<1> : tensor loc("Const") + %0 = "tfl.sub"(%arg0, %cst) {fused_activation_function = "NONE"} : (tensor<*xi32>, tensor) -> tensor<*xi32> + %1 = tfl.add %arg1, %arg1 {fused_activation_function = "NONE"} : tensor<*xf32> + func.return %0, %1 : tensor<*xi32>, tensor<*xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/fuse-qdq.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/fuse-qdq.mlir new file mode 100644 index 0000000000000000000000000000000000000000..ceacaf7804418dc3626c8efa4ed98370d64bd87a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/fuse-qdq.mlir @@ -0,0 +1,579 @@ +// RUN: litert-opt %s -tfl-fuse-qdq | FileCheck %s +// CHECK-LABEL: QuantizeConvDRQ +func.func private @XlaCallModule_quant.fake_quant.impl_0(%arg0: tensor<1x4x4x3xf32>) -> tensor<1x4x4x3xf32> +func.func @QuantizeConvDRQ(%arg0: tensor<1x4x4x3xf32>) -> (tensor<1x4x4x1xf32>) { + // CHECK: %cst = arith.constant dense<0.000000e+00> : tensor<1xf32> + %cst = arith.constant dense<0.000000e+00> : tensor<1xf32> + %cst_0 = arith.constant dense<[[[[1.76285899, -0.257785767, 0.20429258], [1.16310906, 0.23124367, 0.529797196]], [[0.348971426, -0.319283515, -0.772461354], [0.316666812, 1.88180697, -1.78054631]]]]> : tensor<1x2x2x3xf32> + %0 = stablehlo.composite "quant.fake_quant" %arg0 {composite_attributes = {dtype = "i8", narrow_range = false, quantization_dimension = 0 : i32, scale = dense<> : tensor<0xf64>, zero_point = dense<> : tensor<0xi64>}, decomposition = @XlaCallModule_quant.fake_quant.impl_0} : (tensor<1x4x4x3xf32>) -> tensor<1x4x4x3xf32> + // CHECK{LITERAL}: %0 = "tfl.pseudo_qconst"() <{qtype = tensor<1x2x2x3x!quant.uniform>, value = dense<[[[[119, -17, 14], [78, 16, 36]], [[24, -22, -52], [21, 127, -120]]]]> : tensor<1x2x2x3xi8>}> : () -> tensor<1x2x2x3x!quant.uniform> + %1 = "tfl.quantize"(%cst_0) <{qtype = tensor<1x2x2x3x!quant.uniform>}> : (tensor<1x2x2x3xf32>) -> tensor<1x2x2x3x!quant.uniform> + %2 = "tfl.dequantize"(%1) : (tensor<1x2x2x3x!quant.uniform>) -> tensor<1x2x2x3xf32> + // CHECK: %1 = "tfl.conv_2d"(%arg0, %0, %cst) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x4x4x3xf32>, tensor<1x2x2x3x!quant.uniform>, tensor<1xf32>) -> tensor<1x4x4x1xf32> + %3 = "tfl.conv_2d"(%0, %2, %cst) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x4x4x3xf32>, tensor<1x2x2x3xf32>, tensor<1xf32>) -> tensor<1x4x4x1xf32> + // CHECK: return %1 : tensor<1x4x4x1xf32> + return %3 : tensor<1x4x4x1xf32> +} + +// ----- + +// CHECK-LABEL: QuantizeConvDrqWithPad +func.func private @XlaCallModule_quant.fake_quant.impl_1(%arg0: tensor<1x4x4x3xf32>) -> tensor<1x4x4x3xf32> +func.func @QuantizeConvDrqWithPad(%arg0: tensor<1x4x4x3xf32>) -> (tensor<1x6x6x1xf32>) { + // CHECK: %cst_0 = arith.constant dense<0.000000e+00> : tensor<1xf32> + %cst = arith.constant dense<0.000000e+00> : tensor<1xf32> + %cst_0 = arith.constant dense<[[[[1.76285899, -0.257785767, 0.20429258], [1.16310906, 0.23124367, 0.529797196]], [[0.348971426, -0.319283515, -0.772461354], [0.316666812, 1.88180697, -1.78054631]]]]> : tensor<1x2x2x3xf32> + %0 = stablehlo.composite "quant.fake_quant" %arg0 {composite_attributes = {dtype = "i8", narrow_range = false, quantization_dimension = 0 : i32, scale = dense<> : tensor<0xf64>, zero_point = dense<> : tensor<0xi64>}, decomposition = @XlaCallModule_quant.fake_quant.impl_1} : (tensor<1x4x4x3xf32>) -> tensor<1x4x4x3xf32> + // CHECK-LITERAL: %cst = arith.constant dense<[[0, 0], [1, 1], [1, 1], [0, 0]]> : tensor<4x2xi32> + %paddings = arith.constant dense<[[0, 0], [1, 1], [1, 1], [0, 0]]> : tensor<4x2xi32> + // CHECK: %0 = "tfl.pad"(%arg0, %cst) : (tensor<1x4x4x3xf32>, tensor<4x2xi32>) -> tensor<1x6x6x3xf32> + %1 = "tfl.pad"(%0, %paddings) : (tensor<1x4x4x3xf32>, tensor<4x2xi32>) -> tensor<1x6x6x3xf32> + // CHECK{LITERAL}: %1 = "tfl.pseudo_qconst"() <{qtype = tensor<1x2x2x3x!quant.uniform>, value = dense<[[[[119, -17, 14], [78, 16, 36]], [[24, -22, -52], [21, 127, -120]]]]> : tensor<1x2x2x3xi8>}> : () -> tensor<1x2x2x3x!quant.uniform> + %2 = "tfl.quantize"(%cst_0) <{qtype = tensor<1x2x2x3x!quant.uniform>}> : (tensor<1x2x2x3xf32>) -> tensor<1x2x2x3x!quant.uniform> + %3 = "tfl.dequantize"(%2) : (tensor<1x2x2x3x!quant.uniform>) -> tensor<1x2x2x3xf32> + // CHECK: %2 = "tfl.conv_2d"(%0, %1, %cst_0) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x6x6x3xf32>, tensor<1x2x2x3x!quant.uniform>, tensor<1xf32>) -> tensor<1x6x6x1xf32> + %4 = "tfl.conv_2d"(%1, %3, %cst) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x6x6x3xf32>, tensor<1x2x2x3xf32>, tensor<1xf32>) -> tensor<1x6x6x1xf32> + // CHECK: return %2 : tensor<1x6x6x1xf32> + return %4 : tensor<1x6x6x1xf32> +} + +// ----- + +// CHECK-LABEL: QuantizeConvWithBiasDRQ +func.func @QuantizeConvWithBiasDRQ(%arg0: tensor<1x4x4x3xf32>) -> (tensor<1x4x4x1xf32>) { + // CHECK: %cst = arith.constant dense<1.14751196> : tensor<1xf32> + %cst = arith.constant dense<1.14751196> : tensor<1xf32> + %cst_0 = arith.constant dense<[[[[1.76285899, -0.257785767, 0.20429258], [1.16310906, 0.23124367, 0.529797196]], [[0.348971426, -0.319283515, -0.772461354], [0.316666812, 1.88180697, -1.78054631]]]]> : tensor<1x2x2x3xf32> + %0 = stablehlo.composite "quant.fake_quant" %arg0 {composite_attributes = {dtype = "i8", narrow_range = false, quantization_dimension = 0 : i32, scale = dense<> : tensor<0xf64>, zero_point = dense<> : tensor<0xi64>}, decomposition = @XlaCallModule_quant.fake_quant.impl_0} : (tensor<1x4x4x3xf32>) -> tensor<1x4x4x3xf32> + // CHECK{LITERAL}: %0 = "tfl.pseudo_qconst"() <{qtype = tensor<1x2x2x3x!quant.uniform>, value = dense<[[[[119, -17, 14], [78, 16, 36]], [[24, -22, -52], [21, 127, -120]]]]> : tensor<1x2x2x3xi8>}> : () -> tensor<1x2x2x3x!quant.uniform> + %1 = "tfl.quantize"(%cst_0) <{qtype = tensor<1x2x2x3x!quant.uniform>}> : (tensor<1x2x2x3xf32>) -> tensor<1x2x2x3x!quant.uniform> + %2 = "tfl.dequantize"(%1) : (tensor<1x2x2x3x!quant.uniform>) -> tensor<1x2x2x3xf32> + // CHECK: %1 = "tfl.conv_2d"(%arg0, %0, %cst) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x4x4x3xf32>, tensor<1x2x2x3x!quant.uniform>, tensor<1xf32>) -> tensor<1x4x4x1xf32> + %3 = "tfl.conv_2d"(%0, %2, %cst) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x4x4x3xf32>, tensor<1x2x2x3xf32>, tensor<1xf32>) -> tensor<1x4x4x1xf32> + // CHECK: return %1 : tensor<1x4x4x1xf32> + return %3 : tensor<1x4x4x1xf32> +} + +// ----- + +// CHECK-LABEL: QuantizeConvWithBiasAndReluDRQ +func.func @QuantizeConvWithBiasAndReluDRQ(%arg0: tensor<1x4x4x3xf32>) -> (tensor<1x4x4x1xf32>) { + // CHECK: %cst = arith.constant dense<1.14751196> : tensor<1xf32> + %cst = arith.constant dense<1.14751196> : tensor<1xf32> + %cst_0 = arith.constant dense<[[[[1.76285899, -0.257785767, 0.20429258], [1.16310906, 0.23124367, 0.529797196]], [[0.348971426, -0.319283515, -0.772461354], [0.316666812, 1.88180697, -1.78054631]]]]> : tensor<1x2x2x3xf32> + %0 = stablehlo.composite "quant.fake_quant" %arg0 {composite_attributes = {dtype = "i8", narrow_range = false, quantization_dimension = 0 : i32, scale = dense<> : tensor<0xf64>, zero_point = dense<> : tensor<0xi64>}, decomposition = @XlaCallModule_quant.fake_quant.impl_0} : (tensor<1x4x4x3xf32>) -> tensor<1x4x4x3xf32> + // CHECK{LITERAL}: %0 = "tfl.pseudo_qconst"() <{qtype = tensor<1x2x2x3x!quant.uniform>, value = dense<[[[[119, -17, 14], [78, 16, 36]], [[24, -22, -52], [21, 127, -120]]]]> : tensor<1x2x2x3xi8>}> : () -> tensor<1x2x2x3x!quant.uniform> + %1 = "tfl.quantize"(%cst_0) <{qtype = tensor<1x2x2x3x!quant.uniform>}> : (tensor<1x2x2x3xf32>) -> tensor<1x2x2x3x!quant.uniform> + %2 = "tfl.dequantize"(%1) : (tensor<1x2x2x3x!quant.uniform>) -> tensor<1x2x2x3xf32> + // CHECK: %1 = "tfl.conv_2d"(%arg0, %0, %cst) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "RELU", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x4x4x3xf32>, tensor<1x2x2x3x!quant.uniform>, tensor<1xf32>) -> tensor<1x4x4x1xf32> + %3 = "tfl.conv_2d"(%0, %2, %cst) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "RELU", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x4x4x3xf32>, tensor<1x2x2x3xf32>, tensor<1xf32>) -> tensor<1x4x4x1xf32> + // CHECK: return %1 : tensor<1x4x4x1xf32> + return %3 : tensor<1x4x4x1xf32> +} + +// ----- + +// CHECK-LABEL: QuantizeConvWithBiasAndReluWeightOnly +func.func @QuantizeConvWithBiasAndReluWeightOnly(%arg0: tensor<1x4x4x3xf32>) -> (tensor<1x4x4x1xf32>) { + // CHECK: %cst = arith.constant dense<1.14751196> : tensor<1xf32> + %cst = arith.constant dense<1.14751196> : tensor<1xf32> + %cst_0 = arith.constant dense<[[[[1.76285899, -0.257785767, 0.20429258], [1.16310906, 0.23124367, 0.529797196]], [[0.348971426, -0.319283515, -0.772461354], [0.316666812, 1.88180697, -1.78054631]]]]> : tensor<1x2x2x3xf32> + // CHECK{LITERAL}: %0 = "tfl.pseudo_qconst"() <{qtype = tensor<1x2x2x3x!quant.uniform>, value = dense<[[[[119, -17, 14], [78, 16, 36]], [[24, -22, -52], [21, 127, -120]]]]> : tensor<1x2x2x3xi8>}> : () -> tensor<1x2x2x3x!quant.uniform> + %0 = "tfl.quantize"(%cst_0) <{qtype = tensor<1x2x2x3x!quant.uniform>}> : (tensor<1x2x2x3xf32>) -> tensor<1x2x2x3x!quant.uniform> + // CHECK: %1 = "tfl.dequantize"(%0) : (tensor<1x2x2x3x!quant.uniform>) -> tensor<1x2x2x3xf32> + %1 = "tfl.dequantize"(%0) : (tensor<1x2x2x3x!quant.uniform>) -> tensor<1x2x2x3xf32> + // CHECK: %2 = "tfl.conv_2d"(%arg0, %1, %cst) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "RELU", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x4x4x3xf32>, tensor<1x2x2x3xf32>, tensor<1xf32>) -> tensor<1x4x4x1xf32> + %2 = "tfl.conv_2d"(%arg0, %1, %cst) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "RELU", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x4x4x3xf32>, tensor<1x2x2x3xf32>, tensor<1xf32>) -> tensor<1x4x4x1xf32> + // CHECK: return %2 : tensor<1x4x4x1xf32> + return %2 : tensor<1x4x4x1xf32> +} + +// ----- + +// CHECK-LABEL: QuantizeConvWithBiasAndReluSRQ +func.func @QuantizeConvWithBiasAndReluSRQ(%arg0: tensor<1x4x4x3xf32>) -> (tensor<1x4x4x1xf32>) { + %cst = arith.constant dense<1.14751196> : tensor<1xf32> + // CHECK: %0 = "tfl.pseudo_qconst"() <{qtype = tensor<1x!quant.uniform>, value = dense<20578> : tensor<1xi32>}> : () -> tensor<1x!quant.uniform> + %0 = "tfl.quantize"(%cst) <{qtype = tensor<1x!quant.uniform>}> : (tensor<1xf32>) -> tensor<1x!quant.uniform> + %1 = "tfl.dequantize"(%0) : (tensor<1x!quant.uniform>) -> tensor<1xf32> + %cst_0 = arith.constant dense<[[[[1.76285899, -0.257785767, 0.20429258], [1.16310906, 0.23124367, 0.529797196]], [[0.348971426, -0.319283515, -0.772461354], [0.316666812, 1.88180697, -1.78054631]]]]> : tensor<1x2x2x3xf32> + // CHECK: %1 = "tfl.quantize"(%arg0) <{qtype = tensor<1x4x4x3x!quant.uniform>}> : (tensor<1x4x4x3xf32>) -> tensor<1x4x4x3x!quant.uniform> + %2 = "tfl.quantize"(%arg0) <{qtype = tensor<1x4x4x3x!quant.uniform>}> : (tensor<1x4x4x3xf32>) -> tensor<1x4x4x3x!quant.uniform> + %3 = "tfl.dequantize"(%2) : (tensor<1x4x4x3x!quant.uniform>) -> tensor<1x4x4x3xf32> + // CHECK{LITERAL}: %2 = "tfl.pseudo_qconst"() <{qtype = tensor<1x2x2x3x!quant.uniform>, value = dense<[[[[119, -17, 14], [78, 16, 36]], [[24, -22, -52], [21, 127, -120]]]]> : tensor<1x2x2x3xi8>}> : () -> tensor<1x2x2x3x!quant.uniform> + %4 = "tfl.quantize"(%cst_0) <{qtype = tensor<1x2x2x3x!quant.uniform>}> : (tensor<1x2x2x3xf32>) -> tensor<1x2x2x3x!quant.uniform> + %5 = "tfl.dequantize"(%4) : (tensor<1x2x2x3x!quant.uniform>) -> tensor<1x2x2x3xf32> + // CHECK: %3 = "tfl.conv_2d"(%1, %2, %0) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "RELU", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x4x4x3x!quant.uniform>, tensor<1x2x2x3x!quant.uniform>, tensor<1x!quant.uniform>) -> tensor<1x4x4x1x!quant.uniform> + %6 = "tfl.conv_2d"(%3, %5, %1) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "RELU", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x4x4x3xf32>, tensor<1x2x2x3xf32>, tensor<1xf32>) -> tensor<1x4x4x1xf32> + %7 = "tfl.quantize"(%6) <{qtype = tensor<1x4x4x1x!quant.uniform>}> : (tensor<1x4x4x1xf32>) -> tensor<1x4x4x1x!quant.uniform> + // CHECK: %4 = "tfl.dequantize"(%3) : (tensor<1x4x4x1x!quant.uniform>) -> tensor<1x4x4x1xf32> + %8 = "tfl.dequantize"(%7) : (tensor<1x4x4x1x!quant.uniform>) -> tensor<1x4x4x1xf32> + // CHECK: return %4 : tensor<1x4x4x1xf32> + return %8 : tensor<1x4x4x1xf32> +} + +// ----- + +// CHECK-LABEL: QuantizeEmbeddingLookupDrq +func.func @QuantizeEmbeddingLookupDrq(%arg0: tensor<2xi32>) -> (tensor<2x4xf32>){ + %cst = arith.constant dense<[[1.0545162, -0.969288647, -0.594602108, -0.0318857245], [2.41093326, -1.87844908, -0.784769594, -0.313708425], [0.333708912, 1.76770353, -1.02776456, 1.41117179], [-0.508497119, -0.526377499, 0.503150403, 1.05497932], [-0.0874073281, 0.795816719, 2.65656161, -0.58229059]]> : tensor<5x4xf32> + // CHECK{LITERAL}: %0 = "tfl.pseudo_qconst"() <{qtype = tensor<5x4x!quant.uniform>, value = dense<[[127, -118, -72, -4], [127, -100, -42, -17], [24, 127, -74, 102], [-62, -64, 61, 127], [-4, 38, 127, -28]]> : tensor<5x4xi8>}> : () -> tensor<5x4x!quant.uniform> + %0 = "tfl.quantize"(%cst) <{qtype = tensor<5x4x!quant.uniform>}> : (tensor<5x4xf32>) -> tensor<5x4x!quant.uniform> + %1 = "tfl.dequantize"(%0) : (tensor<5x4x!quant.uniform>) -> tensor<5x4xf32> + // CHECK: %1 = "tfl.embedding_lookup"(%arg0, %0) : (tensor<2xi32>, tensor<5x4x!quant.uniform>) -> tensor<2x4xf32> + %2 = "tfl.embedding_lookup"(%arg0, %1) : (tensor<2xi32>, tensor<5x4xf32>) -> tensor<2x4xf32> + // CHECK: return %1 : tensor<2x4xf32> + return %2 : tensor<2x4xf32> +} + +// ----- + +// CHECK-LABEL: DQQToRequantize +func.func @DQQToRequantize(%arg0: tensor<1x128x128x320x!quant.uniform>) -> (tensor<1x128x128x320x!quant.uniform>) { + %0 = "tfl.dequantize"(%arg0) : (tensor<1x128x128x320x!quant.uniform>) -> tensor<1x128x128x320xf32> +// CHECK: %0 = "tfl.quantize"(%arg0) <{qtype = tensor<1x128x128x320x!quant.uniform>}> : (tensor<1x128x128x320x!quant.uniform>) -> tensor<1x128x128x320x!quant.uniform> + %1 = "tfl.quantize"(%0) <{qtype = tensor<1x128x128x320x!quant.uniform>}> : (tensor<1x128x128x320xf32>) -> tensor<1x128x128x320x!quant.uniform> +// CHECK: return %0 : tensor<1x128x128x320x!quant.uniform> + return %1 : tensor<1x128x128x320x!quant.uniform> +} + +// ----- + +func.func @VolatileQuantizeConst() -> (tensor<1xf32>) { + %cst = arith.constant dense<1.14751196> : tensor<1xf32> +// CHECK: %0 = "tfl.pseudo_qconst"() <{qtype = tensor<1x!quant.uniform>, value = dense<20578> : tensor<1xi32>}> {volatile} : () -> tensor<1x!quant.uniform> + %0 = "tfl.quantize"(%cst) <{qtype = tensor<1x!quant.uniform>}> {volatile} : (tensor<1xf32>) -> tensor<1x!quant.uniform> +// CHECK: %1 = "tfl.dequantize"(%0) : (tensor<1x!quant.uniform>) -> tensor<1xf32> + %1 = "tfl.dequantize"(%0) : (tensor<1x!quant.uniform>) -> tensor<1xf32> +// CHECK: return %1 : tensor<1xf32> + return %1 : tensor<1xf32> +} + +// ----- + +// CHECK-LABEL: QuantizeFloatConst +func.func @QuantizeFloatConst() -> tensor<2x2x!quant.uniform> { + %0 = arith.constant dense<-0.1> : tensor<2x2xf32> +// CHECK: %[[cst:.*]] = "tfl.pseudo_qconst"() <{qtype = tensor<2x2x!quant.uniform>, value = dense<0> : tensor<2x2xi8>}> + %1 = "tfl.quantize"(%0) {qtype = tensor<2x2x!quant.uniform>} : (tensor<2x2xf32>) -> tensor<2x2x!quant.uniform> +// CHECK: return %[[cst]] + func.return %1 : tensor<2x2x!quant.uniform> +} + +// ----- + +// CHECK-LABEL: QuantizeFloatConst4Bits +func.func @QuantizeFloatConst4Bits() -> tensor<2x4x!quant.uniform> { + %0 = arith.constant dense<[[-0.75, -0.5, -0.25, 0.0], [0.25, 0.5, 0.75, 1.0]]> : tensor<2x4xf32> +// CHECK: %[[cst:.*]] = "tfl.pseudo_qconst"() <{qtype = tensor<2x4x!quant.uniform>, value = dense<{{\[\[}}-4, -3, -2, -1{{\]}}, [0, 1, 2, 3{{\]\]}}> : tensor<2x4xi4>}> + %1 = "tfl.quantize"(%0) {qtype = tensor<2x4x!quant.uniform>} : (tensor<2x4xf32>) -> tensor<2x4x!quant.uniform> +// CHECK: return %[[cst]] + func.return %1 : tensor<2x4x!quant.uniform> +} + +// ----- + +// CHECK-LABEL: QuantizeDenseFloatConst +func.func @QuantizeDenseFloatConst() -> tensor<2x2x!quant.uniform> { + %0 = arith.constant dense<[[-0.1, 1.0], [1.0, 3.0]]> : tensor<2x2xf32> +// CHECK: %[[cst:.*]] = "tfl.pseudo_qconst"() <{qtype = tensor<2x2x!quant.uniform>, value = dense<{{\[\[}}0, -1], {{\[}}-1, -1]]> : tensor<2x2xi8>}> + %1 = "tfl.quantize"(%0) {qtype = tensor<2x2x!quant.uniform>} : (tensor<2x2xf32>) -> tensor<2x2x!quant.uniform> +// CHECK: return %[[cst]] + func.return %1 : tensor<2x2x!quant.uniform> +} + +// ----- + +// CHECK-LABEL: QuantizeSplatFloatConst +func.func @QuantizeSplatFloatConst() -> tensor<2x2x!quant.uniform> { + %0 = arith.constant dense<3.0> : tensor<2x2xf32> +// CHECK: %[[cst:.*]] = "tfl.pseudo_qconst"() <{qtype = tensor<2x2x!quant.uniform>, value = dense<-1> : tensor<2x2xi8>}> + %1 = "tfl.quantize"(%0) {qtype = tensor<2x2x!quant.uniform>} : (tensor<2x2xf32>) -> tensor<2x2x!quant.uniform> +// CHECK: return %[[cst]] + func.return %1 : tensor<2x2x!quant.uniform> +} + +// ----- + +// CHECK-LABEL: DequantizeAndQuantize +func.func @DequantizeAndQuantize() -> tensor<2x2x!quant.uniform> { +// CHECK: %[[cst:.*]] = "tfl.pseudo_qconst"() <{qtype = tensor<2x2x!quant.uniform>, value = dense<-1> : tensor<2x2xi8>}> + %cst = "tfl.pseudo_qconst"() {qtype = tensor<2x2x!quant.uniform>, value = dense<-1> : tensor<2x2xi8>} : () -> tensor<2x2x!quant.uniform> + %0 = "tfl.dequantize"(%cst) : (tensor<2x2x!quant.uniform>) -> tensor<2x2xf32> + %1 = "tfl.quantize"(%0) {qtype = tensor<2x2x!quant.uniform>} : (tensor<2x2xf32>) -> tensor<2x2x!quant.uniform> +// CHECK: return %[[cst]] : tensor<2x2x!quant.uniform> + func.return %1 : tensor<2x2x!quant.uniform> +} + +// ----- + +// CHECK-LABEL: QuantizeConv2D +func.func @QuantizeConv2D(tensor<1x224x224x3x!quant.uniform>) -> tensor<1x112x112x32x!quant.uniform> { +^bb0(%arg0: tensor<1x224x224x3x!quant.uniform>): + %cst = arith.constant dense<-1.23697901> : tensor<32xf32> +// CHECK: %[[cst0:.*]] = "tfl.pseudo_qconst"() <{qtype = tensor<32x!quant.uniform>, value = dense<-1583> : tensor<32xi32>}> + %0 = "tfl.quantize"(%cst) <{qtype = tensor<32x!quant.uniform>}> : (tensor<32xf32>) -> tensor<32x!quant.uniform> + %1 = "tfl.dequantize"(%0) : (tensor<32x!quant.uniform>) -> tensor<32xf32> + %2 = "tfl.dequantize"(%arg0) : (tensor<1x224x224x3x!quant.uniform>) -> tensor<1x224x224x3xf32> + %w = arith.constant dense<-1.0> : tensor<32x3x3x3xf32> +// CHECK: %[[cst1:.*]] = "tfl.pseudo_qconst"() <{qtype = tensor<32x3x3x3x!quant.uniform:f32, 1.000000e-01>>, value = dense<1> : tensor<32x3x3x3xi8>}> + %3 = "tfl.quantize"(%w) {qtype = tensor<32x3x3x3x!quant.uniform:f32, 0.1>>} : (tensor<32x3x3x3xf32>) -> tensor<32x3x3x3x!quant.uniform:f32, 0.1>> + %4 = "tfl.dequantize"(%3) : (tensor<32x3x3x3x!quant.uniform:f32, 0.1>>) -> tensor<32x3x3x3xf32> +// CHECK: %[[conv:.*]] = "tfl.conv_2d"(%arg0, %[[cst1]], %[[cst0]]) + %5 = "tfl.conv_2d"(%2, %4, %1) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 2 : i32, stride_w = 2 : i32} : (tensor<1x224x224x3xf32>, tensor<32x3x3x3xf32>, tensor<32xf32>) -> tensor<1x112x112x32xf32> + %6 = "tfl.quantize"(%5) {qtype = tensor<1x112x112x32x!quant.uniform>} : (tensor<1x112x112x32xf32>) -> tensor<1x112x112x32x!quant.uniform> +// CHECK: return %[[conv]] : tensor<1x112x112x32x!quant.uniform> + func.return %6 : tensor<1x112x112x32x!quant.uniform> +} + +// ----- + +// CHECK-LABEL: QuantizeConv2D4Bit +func.func @QuantizeConv2D4Bit(tensor<1x224x224x3x!quant.uniform>) -> tensor<1x112x112x32x!quant.uniform> { +^bb0(%arg0: tensor<1x224x224x3x!quant.uniform>): + %cst = arith.constant dense<-1.23697901> : tensor<32xf32> +// CHECK: %[[cst0:.*]] = "tfl.pseudo_qconst"() <{qtype = tensor<32x!quant.uniform>, value = dense<-1583> : tensor<32xi32>}> + %0 = "tfl.quantize"(%cst) <{qtype = tensor<32x!quant.uniform>}> : (tensor<32xf32>) -> tensor<32x!quant.uniform> + %1 = "tfl.dequantize"(%0) : (tensor<32x!quant.uniform>) -> tensor<32xf32> + %2 = "tfl.dequantize"(%arg0) : (tensor<1x224x224x3x!quant.uniform>) -> tensor<1x224x224x3xf32> + %w = arith.constant dense<-1.0> : tensor<32x3x3x3xf32> +// CHECK: %[[cst1:.*]] = "tfl.pseudo_qconst"() <{qtype = tensor<32x3x3x3x!quant.uniform:f32, 1.000000e-01>>, value = dense<1> : tensor<32x3x3x3xi4>}> + %3 = "tfl.quantize"(%w) {qtype = tensor<32x3x3x3x!quant.uniform:f32, 0.1>>} : (tensor<32x3x3x3xf32>) -> tensor<32x3x3x3x!quant.uniform:f32, 0.1>> + %4 = "tfl.dequantize"(%3) : (tensor<32x3x3x3x!quant.uniform:f32, 0.1>>) -> tensor<32x3x3x3xf32> +// CHECK: %[[conv:.*]] = "tfl.conv_2d"(%arg0, %[[cst1]], %[[cst0]]) + %5 = "tfl.conv_2d"(%2, %4, %1) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 2 : i32, stride_w = 2 : i32} : (tensor<1x224x224x3xf32>, tensor<32x3x3x3xf32>, tensor<32xf32>) -> tensor<1x112x112x32xf32> + %6 = "tfl.quantize"(%5) {qtype = tensor<1x112x112x32x!quant.uniform>} : (tensor<1x112x112x32xf32>) -> tensor<1x112x112x32x!quant.uniform> +// CHECK: return %[[conv]] : tensor<1x112x112x32x!quant.uniform> + func.return %6 : tensor<1x112x112x32x!quant.uniform> +} + +// ----- + +// CHECK-LABEL: QuantizeDepthwiseConv2D +func.func @QuantizeDepthwiseConv2D(tensor<1x224x224x3x!quant.uniform>) -> tensor<1x112x112x32x!quant.uniform> { +^bb0(%arg0: tensor<1x224x224x3x!quant.uniform>): + %cst = arith.constant dense<-1.23697901> : tensor<32xf32> +// CHECK: %[[cst0:.*]] = "tfl.pseudo_qconst"() <{qtype = tensor<32x!quant.uniform>, value = dense<-7254> : tensor<32xi32>}> + %0 = "tfl.quantize"(%cst) <{qtype = tensor<32x!quant.uniform>}> : (tensor<32xf32>) -> tensor<32x!quant.uniform> + %1 = "tfl.dequantize"(%0) : (tensor<32x!quant.uniform>) -> tensor<32xf32> + %2 = "tfl.dequantize"(%arg0) : (tensor<1x224x224x3x!quant.uniform>) -> tensor<1x224x224x3xf32> +// CHECK: %[[cst1:.*]] = "tfl.pseudo_qconst"() <{qtype = tensor<32x3x3x3x!quant.uniform:f32, 0.021826678373682216:151>>, value = dense<-76> : tensor<32x3x3x3xi8>}> + %3 = "tfl.pseudo_qconst"() {qtype = tensor<32x3x3x3x!quant.uniform:f32, 0.021826678373682216:151>>, value = dense<-76> : tensor<32x3x3x3xi8>} : () -> tensor<32x3x3x3x!quant.uniform:f32, 0.021826678373682216:151>> + %4 = "tfl.dequantize"(%3) : (tensor<32x3x3x3x!quant.uniform:f32, 0.021826678373682216:151>>) -> tensor<32x3x3x3xf32> +// CHECK: %[[conv:.*]] = "tfl.depthwise_conv_2d"(%arg0, %[[cst1]], %[[cst0]]) <{depth_multiplier = 4 : i32, dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 4 : i32, stride_w = 5 : i32}> + %5 = "tfl.depthwise_conv_2d"(%2, %4, %1) {depth_multiplier = 4 : i32, dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 4 : i32, stride_w = 5 : i32} : (tensor<1x224x224x3xf32>, tensor<32x3x3x3xf32>, tensor<32xf32>) -> tensor<1x112x112x32xf32> + %6 = "tfl.quantize"(%5) {qtype = tensor<1x112x112x32x!quant.uniform>} : (tensor<1x112x112x32xf32>) -> tensor<1x112x112x32x!quant.uniform> +// CHECK: return %[[conv]] + func.return %6 : tensor<1x112x112x32x!quant.uniform> +} + +// ----- + +// CHECK-LABEL: QuantizeFullyConnected +func.func @QuantizeFullyConnected(tensor<1x224x224x3x!quant.uniform>) -> tensor<1x112x112x32x!quant.uniform> { +^bb0(%arg0: tensor<1x224x224x3x!quant.uniform>): + %cst = arith.constant dense<-1.23697901> : tensor<32xf32> +// CHECK: %[[cst_0:.*]] = "tfl.pseudo_qconst"() <{qtype = tensor<32x!quant.uniform>, value = dense<-7254> : tensor<32xi32>}> + %0 = "tfl.quantize"(%cst) <{qtype = tensor<32x!quant.uniform>}> : (tensor<32xf32>) -> tensor<32x!quant.uniform> + %1 = "tfl.dequantize"(%0) : (tensor<32x!quant.uniform>) -> tensor<32xf32> + %2 = "tfl.dequantize"(%arg0) : (tensor<1x224x224x3x!quant.uniform>) -> tensor<1x224x224x3xf32> +// CHECK: %[[cst_1:.*]] = "tfl.pseudo_qconst"() <{qtype = tensor<32x12x!quant.uniform:f32, 0.021826678373682216:151>>, value = dense<-76> : tensor<32x12xi8>}> + %3 = "tfl.pseudo_qconst"() {qtype = tensor<32x12x!quant.uniform:f32, 0.021826678373682216:151>>, value = dense<-76> : tensor<32x12xi8>} : () -> tensor<32x12x!quant.uniform:f32, 0.021826678373682216:151>> + %4 = "tfl.dequantize"(%3) : (tensor<32x12x!quant.uniform:f32, 0.021826678373682216:151>>) -> tensor<32x12xf32> +// CHECK: %[[fc:.*]] = "tfl.fully_connected"(%arg0, %[[cst_1]], %[[cst_0]]) <{fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"}> + %5 = "tfl.fully_connected"(%2, %4, %1) {fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"} : (tensor<1x224x224x3xf32>, tensor<32x12xf32>, tensor<32xf32>) -> tensor<1x112x112x32xf32> + %6 = "tfl.quantize"(%5) {qtype = tensor<1x112x112x32x!quant.uniform>} : (tensor<1x112x112x32xf32>) -> tensor<1x112x112x32x!quant.uniform> +// CHECK: return %[[fc]] + func.return %6 : tensor<1x112x112x32x!quant.uniform> +} + +// ----- + +// CHECK-LABEL: QuantizeFullyConnected4Bit +func.func @QuantizeFullyConnected4Bit(tensor<1x224x224x3x!quant.uniform>) -> tensor<1x112x112x32x!quant.uniform> { +^bb0(%arg0: tensor<1x224x224x3x!quant.uniform>): + %cst = arith.constant dense<-1.23697901> : tensor<32xf32> +// CHECK: %[[cst_0:.*]] = "tfl.pseudo_qconst"() <{qtype = tensor<32x!quant.uniform>, value = dense<-400> : tensor<32xi32>}> + %0 = "tfl.quantize"(%cst) <{qtype = tensor<32x!quant.uniform>}> : (tensor<32xf32>) -> tensor<32x!quant.uniform> + %1 = "tfl.dequantize"(%0) : (tensor<32x!quant.uniform>) -> tensor<32xf32> + %2 = "tfl.dequantize"(%arg0) : (tensor<1x224x224x3x!quant.uniform>) -> tensor<1x224x224x3xf32> +// CHECK: %[[cst_1:.*]] = "tfl.pseudo_qconst"() <{qtype = tensor<32x12x!quant.uniform:f32, 0.39599830800000002:8>>, value = dense<-7> : tensor<32x12xi4>}> + %3 = "tfl.pseudo_qconst"() {qtype = tensor<32x12x!quant.uniform:f32, 0.395998308:8>>, value = dense<-7> : tensor<32x12xi4>} : () -> tensor<32x12x!quant.uniform:f32, 0.395998308:8>> + %4 = "tfl.dequantize"(%3) : (tensor<32x12x!quant.uniform:f32, 0.395998308:8>>) -> tensor<32x12xf32> +// CHECK: %[[fc:.*]] = "tfl.fully_connected"(%arg0, %[[cst_1]], %[[cst_0]]) <{fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"}> + %5 = "tfl.fully_connected"(%2, %4, %1) {fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"} : (tensor<1x224x224x3xf32>, tensor<32x12xf32>, tensor<32xf32>) -> tensor<1x112x112x32xf32> + %6 = "tfl.quantize"(%5) {qtype = tensor<1x112x112x32x!quant.uniform>} : (tensor<1x112x112x32xf32>) -> tensor<1x112x112x32x!quant.uniform> +// CHECK: return %[[fc]] + func.return %6 : tensor<1x112x112x32x!quant.uniform> +} + +// ----- + +// CHECK-LABEL: QuantizeNoBiasFullyConnected +func.func @QuantizeNoBiasFullyConnected(%arg0: tensor<3x!quant.uniform>, %arg1: tensor<3x3x!quant.uniform:f32, 1.0>>, %arg2: none) -> tensor<3x!quant.uniform> { + %0 = "tfl.dequantize"(%arg0) : (tensor<3x!quant.uniform>) -> tensor<3xf32> + %1 = "tfl.dequantize"(%arg1) : (tensor<3x3x!quant.uniform:f32, 1.0>>) -> tensor<3x3xf32> +// CHECK: %[[fc:.*]] = "tfl.fully_connected"(%arg0, %arg1, %arg2) + %2 = "tfl.fully_connected"(%0, %1, %arg2) {fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"} : (tensor<3xf32>, tensor<3x3xf32>, none) -> tensor<3xf32> + %3 = "tfl.quantize"(%2) {qtype = tensor<3x!quant.uniform>} : (tensor<3xf32>) -> tensor<3x!quant.uniform> +// CHECK: return %[[fc]] + func.return %3 : tensor<3x!quant.uniform> +} + +// ----- + +// CHECK-LABEL: QuantizeAveragePool2D +func.func @QuantizeAveragePool2D(tensor<1x6x6x16x!quant.uniform>) -> tensor<1x1x1x16x!quant.uniform> { +^bb0(%arg0: tensor<1x6x6x16x!quant.uniform>): + %0 = "tfl.dequantize"(%arg0) : (tensor<1x6x6x16x!quant.uniform>) -> tensor<1x6x6x16xf32> +// CHECK: %[[avgp:.*]] = "tfl.average_pool_2d"(%arg0) + %1 = "tfl.average_pool_2d"(%0) {name = "avgpool", filter_height = 3 : i32, filter_width = 6 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 3 : i32, stride_w = 1 : i32} : (tensor<1x6x6x16xf32>) -> tensor<1x1x1x16xf32> + %2 = "tfl.quantize"(%1) {qtype = tensor<1x1x1x16x!quant.uniform>} : (tensor<1x1x1x16xf32>) -> tensor<1x1x1x16x!quant.uniform> +// CHECK: return %[[avgp]] : tensor<1x1x1x16x!quant.uniform> + func.return %2 : tensor<1x1x1x16x!quant.uniform> +} + +// ----- + +// This behavior is intentioally different than the legacy quantized pass. +// [quantized value] -> [DQ] -> [Float] pattern is no longer quantized. +// CHECK-LABEL: NoQuantizeAveragePool2D +func.func @NoQuantizeAveragePool2D(tensor<1x6x6x16x!quant.uniform>) -> tensor<1x1x1x16xf32> { +^bb0(%arg0: tensor<1x6x6x16x!quant.uniform>): +// CHECK: %[[DQ:.*]] = "tfl.dequantize"(%arg0) + %0 = "tfl.dequantize"(%arg0) : (tensor<1x6x6x16x!quant.uniform>) -> tensor<1x6x6x16xf32> +// CHECK: %[[AVGP:.*]] = "tfl.average_pool_2d"(%[[DQ]]) + %1 = "tfl.average_pool_2d"(%0) {name = "avgpool", filter_height = 3 : i32, filter_width = 6 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 3 : i32, stride_w = 1 : i32} : (tensor<1x6x6x16xf32>) -> tensor<1x1x1x16xf32> +// CHECK: return %[[AVGP]] : tensor<1x1x1x16xf32> + func.return %1 : tensor<1x1x1x16xf32> +} + +// ----- + +// CHECK-LABEL: QuantizeReshape2D +func.func @QuantizeReshape2D(tensor<1x6x6x16x!quant.uniform>) -> tensor<1x36x16x!quant.uniform> { +^bb0(%arg0: tensor<1x6x6x16x!quant.uniform>): + %cst = arith.constant dense<[1, 36, 16]> : tensor<3xi32> + %0 = "tfl.dequantize"(%arg0) : (tensor<1x6x6x16x!quant.uniform>) -> tensor<1x6x6x16xf32> +// CHECK: %[[rs:.*]] = "tfl.reshape"(%arg0, %{{.*}}) + %1 = "tfl.reshape"(%0, %cst) : (tensor<1x6x6x16xf32>, tensor<3xi32>) -> tensor<1x36x16xf32> + %2 = "tfl.quantize"(%1) {qtype = tensor<1x36x16x!quant.uniform>} : (tensor<1x36x16xf32>) -> tensor<1x36x16x!quant.uniform> +// CHECK: return %[[rs]] : tensor<1x36x16x!quant.uniform> + func.return %2 : tensor<1x36x16x!quant.uniform> +} + + +// ----- + +// CHECK-LABEL: QuantizeSoftmax +func.func @QuantizeSoftmax(tensor<1x6x6x16x!quant.uniform>) -> tensor<1x6x6x16x!quant.uniform> { +^bb0(%arg0: tensor<1x6x6x16x!quant.uniform>): + %0 = "tfl.dequantize"(%arg0) : (tensor<1x6x6x16x!quant.uniform>) -> tensor<1x6x6x16xf32> +// CHECK: %[[sm:.*]] = "tfl.softmax"(%arg0) + %1 = "tfl.softmax"(%0) {beta = 1.000000e+00 : f32} : (tensor<1x6x6x16xf32>) -> tensor<1x6x6x16xf32> + %2 = "tfl.quantize"(%1) {qtype = tensor<1x6x6x16x!quant.uniform>} : (tensor<1x6x6x16xf32>) -> tensor<1x6x6x16x!quant.uniform> +// CHECK: return %[[sm]] : tensor<1x6x6x16x!quant.uniform> + func.return %2 : tensor<1x6x6x16x!quant.uniform> +} + +// ----- + +// CHECK-LABEL: QuantizeLogistic +func.func @QuantizeLogistic(tensor<1x6x6x16x!quant.uniform>) -> tensor<1x6x6x16x!quant.uniform> { +^bb0(%arg0: tensor<1x6x6x16x!quant.uniform>): + %0 = "tfl.dequantize"(%arg0) : (tensor<1x6x6x16x!quant.uniform>) -> tensor<1x6x6x16xf32> +// CHECK: %[[lg:.*]] = "tfl.logistic"(%arg0) : (tensor<1x6x6x16x!quant.uniform>) + %1 = "tfl.logistic"(%0) : (tensor<1x6x6x16xf32>) -> tensor<1x6x6x16xf32> + %2 = "tfl.quantize"(%1) {qtype = tensor<1x6x6x16x!quant.uniform>} : (tensor<1x6x6x16xf32>) -> tensor<1x6x6x16x!quant.uniform> +// CHECK: return %[[lg]] : tensor<1x6x6x16x!quant.uniform> + func.return %2 : tensor<1x6x6x16x!quant.uniform> +} + +// ----- + +// CHECK-LABEL: QuantizeAdd +func.func @QuantizeAdd(tensor<1x56x56x24x!quant.uniform>, tensor<1x56x56x24x!quant.uniform>) -> tensor<1x56x56x24x!quant.uniform> { +^bb0(%arg0: tensor<1x56x56x24x!quant.uniform>, %arg1: tensor<1x56x56x24x!quant.uniform>): + %0 = "tfl.dequantize"(%arg0) : (tensor<1x56x56x24x!quant.uniform>) -> tensor<1x56x56x24xf32> + %1 = "tfl.dequantize"(%arg1) : (tensor<1x56x56x24x!quant.uniform>) -> tensor<1x56x56x24xf32> +// CHECK: %[[add:.*]] = tfl.add(%arg0, %arg1) <{fused_activation_function = "NONE"}> : (tensor<1x56x56x24x!quant.uniform>, tensor<1x56x56x24x!quant.uniform>) + %2 = tfl.add %0, %1 {fused_activation_function = "NONE"} : tensor<1x56x56x24xf32> loc("Block") + %3 = "tfl.quantize"(%2) {qtype = tensor<1x56x56x24x!quant.uniform>} : (tensor<1x56x56x24xf32>) -> tensor<1x56x56x24x!quant.uniform> +// CHECK: return %[[add]] : tensor<1x56x56x24x!quant.uniform> + func.return %3 : tensor<1x56x56x24x!quant.uniform> +} + +// ----- + +// CHECK-LABEL: QuantizeConcat +func.func @QuantizeConcat(tensor<1x2x!quant.uniform>, tensor<1x2x!quant.uniform>) -> tensor<2x2x!quant.uniform> { +^bb0(%arg0: tensor<1x2x!quant.uniform>, %arg1: tensor<1x2x!quant.uniform>): + %0 = "tfl.dequantize"(%arg0) : (tensor<1x2x!quant.uniform>) -> tensor<1x2xf32> + %1 = "tfl.dequantize"(%arg1) : (tensor<1x2x!quant.uniform>) -> tensor<1x2xf32> +// CHECK: %[[cc:.*]] = "tfl.concatenation"(%arg0, %arg1) <{axis = 0 : i32, fused_activation_function = "NONE"}> + %2 = "tfl.concatenation"(%0, %1) {axis = 0 : i32, fused_activation_function = "NONE"} : (tensor<1x2xf32>, tensor<1x2xf32>) -> tensor<2x2xf32> + %3 = "tfl.quantize"(%2) {qtype = tensor<2x2x!quant.uniform>} : (tensor<2x2xf32>) -> tensor<2x2x!quant.uniform> +// CHECK: return %[[cc]] : tensor<2x2x!quant.uniform> + func.return %3 : tensor<2x2x!quant.uniform> +} + +// ----- + +// CHECK-LABEL: QuantizeMaxPool2D +func.func @QuantizeMaxPool2D(tensor<1x6x6x16x!quant.uniform>) -> tensor<1x1x1x16x!quant.uniform> { +^bb0(%arg0: tensor<1x6x6x16x!quant.uniform>): + %0 = "tfl.dequantize"(%arg0) : (tensor<1x6x6x16x!quant.uniform>) -> tensor<1x6x6x16xf32> +// CHECK: %[[mp:.*]] = "tfl.max_pool_2d"(%arg0) <{filter_height = 1 : i32, filter_width = 1 : i32, fused_activation_function = "RELU6", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x6x6x16x!quant.uniform>) -> tensor<1x1x1x16x!quant.uniform> + %1 = "tfl.max_pool_2d"(%0) {filter_height = 1 : i32, filter_width = 1 : i32, fused_activation_function = "RELU6", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32} : (tensor<1x6x6x16xf32>) -> tensor<1x1x1x16xf32> + %2 = "tfl.quantize"(%1) {qtype = tensor<1x1x1x16x!quant.uniform>} : (tensor<1x1x1x16xf32>) -> tensor<1x1x1x16x!quant.uniform> +// CHECK: return %[[mp]] : tensor<1x1x1x16x!quant.uniform> + func.return %2 : tensor<1x1x1x16x!quant.uniform> +} + +// ----- + +// CHECK-LABEL: QuantizeSplit +func.func @QuantizeSplit(%arg: tensor<4x!quant.uniform>, %cst: tensor) -> (tensor<2x!quant.uniform>,tensor<2x!quant.uniform>) { + %0 = "tfl.dequantize"(%arg) : (tensor<4x!quant.uniform>) -> tensor<4xf32> +// CHECK: %[[sp:.*]]:2 = "tfl.split"(%arg1, %arg0) <{num_splits = 2 : i32}> : (tensor, tensor<4x!quant.uniform>) + %1:2 = "tfl.split"(%cst, %0) {num_splits = 2 : i32} : (tensor, tensor<4xf32>) -> (tensor<2xf32>, tensor<2xf32>) + %2 = "tfl.quantize"(%1#0) {qtype = tensor<2x!quant.uniform>} : (tensor<2xf32>) -> tensor<2x!quant.uniform> + %3 = "tfl.quantize"(%1#1) {qtype = tensor<2x!quant.uniform>} : (tensor<2xf32>) -> tensor<2x!quant.uniform> +// CHECK: return %[[sp]]#0, %[[sp]]#1 + func.return %2, %3 : tensor<2x!quant.uniform>, tensor<2x!quant.uniform> +} + +// ----- + +// CHECK-LABEL: QuantizeCustomTfOp +func.func @QuantizeCustomTfOp(%arg0: tensor<128x128x!quant.uniform>, + %arg1: tensor<1x!quant.uniform>, %arg2: tensor<1x!quant.uniform>, + %arg3: tensor<1xi32>) -> (tensor<128x128x!quant.uniform>) { + %0 = "tfl.dequantize"(%arg0) : (tensor<128x128x!quant.uniform>) -> tensor<128x128xf32> + %1 = "tfl.dequantize"(%arg1) : (tensor<1x!quant.uniform>) -> tensor<1xf32> + %2 = "tfl.dequantize"(%arg2) : (tensor<1x!quant.uniform>) -> tensor<1xf32> +// CHECK: %4 = "tfl.custom_tf"(%arg0, %arg1, %arg2, %arg3) ({ +// CHECK-NEXT: ^bb0(%arg4: tensor<128x128xf32>, %arg5: tensor<1xf32>, %arg6: tensor<1xf32>, %arg7: tensor<1xi32>): +// CHECK-NEXT: "tf.LayerNorm"(%arg4, %arg5, %arg6, %arg7) {_tfl_quant_trait = "fully_quantizable", device = ""} : (tensor<128x128xf32>, tensor<1xf32>, tensor<1xf32>, tensor<1xi32>) -> tensor<128x128xf32> +// CHECK-NEXT: "tfl.yield" +// CHECK-NEXT: }) {_tfl_quant_trait = "fully_quantizable", device = ""} : +// CHECK-SAME: (tensor<128x128x!quant.uniform>, tensor<1x!quant.uniform>, tensor<1x!quant.uniform>, tensor<1xi32>) +// CHECK-SAME: -> tensor<128x128x!quant.uniform> + %3 = "tfl.custom_tf"(%0, %1, %2, %arg3) ({ + ^bb0(%a1: tensor<128x128xf32>, %a2: tensor<1xf32>, %a3: tensor<1xf32>, %a4: tensor<1xi32>): + %4 = "tf.LayerNorm"(%a1, %a2, %a3, %a4) {_tfl_quant_trait = "fully_quantizable", device = ""} : (tensor<128x128xf32>, tensor<1xf32>, tensor<1xf32>, tensor<1xi32>) -> tensor<128x128xf32> + "tfl.yield"(%4) : (tensor<128x128xf32>) -> () + }) {_tfl_quant_trait = "fully_quantizable", device = ""} : (tensor<128x128xf32>, tensor<1xf32>, tensor<1xf32>, tensor<1xi32>) -> tensor<128x128xf32> + %4 = "tfl.quantize"(%3) {qtype = tensor<128x128x!quant.uniform>} : (tensor<128x128xf32>) -> tensor<128x128x!quant.uniform> + func.return %4 : tensor<128x128x!quant.uniform> +} + +// ----- + +// Checks that legacy path correctly handles asymmetric quantized values. +// CHECK-LABEL: CheckLegacyQuantizeAdd +func.func @CheckLegacyQuantizeAdd() -> tensor<1x2x!quant.uniform> { + %cst = arith.constant dense<[[1.000000e+00, 2.000000e+00]]> : tensor<1x2xf32> +// CHECK: "tfl.pseudo_qconst"() <{qtype = tensor<1x2x!quant.uniform>, value = dense<{{\[\[}}-1, 127]]> : tensor<1x2xi8>}> + %0 = "tfl.quantize"(%cst) {qtype = tensor<1x2x!quant.uniform>, volatile} : (tensor<1x2xf32>) -> tensor<1x2x!quant.uniform> + func.return %0 : tensor<1x2x!quant.uniform> +} + +// ----- + +func.func private @testIfThen(tensor<*xf32>) -> tensor<*xf32> +func.func private @testIfElse(tensor<*xf32>) -> tensor<*xf32> + +// CHECK-LABEL: NotQuantizeIf +func.func @NotQuantizeIf(%arg0: tensor, + %arg1: tensor<4x!quant.uniform>) -> (tensor<4x!quant.uniform>) { + // CHECK: %[[dq:.*]] = "tfl.dequantize"(%arg1) + %0 = "tfl.dequantize"(%arg1) : (tensor<4x!quant.uniform>) -> tensor<4xf32> + // CHECK-NEXT: %[[if:.*]] = "tf.If"(%arg0, %[[dq]] + %1 = "tf.If"(%arg0, %0) {then_branch = @testIfThen, else_branch = @testIfElse, is_stateless = false} : (tensor, tensor<4xf32>) -> tensor<4xf32> + // CHECK-NEXT: %[[q:.*]] = "tfl.quantize"(%[[if]]) + %2 = "tfl.quantize"(%1) {qtype = tensor<4x!quant.uniform>} : (tensor<4xf32>) -> tensor<4x!quant.uniform> + + // CHECK-NEXT: return %[[q]] + func.return %2 : tensor<4x!quant.uniform> +} + +// ----- + +// CHECK-LABEL: NotQuantizeReadVariable +func.func @NotQuantizeReadVariable() -> tensor<1x2x3x!quant.uniform:f32, 0.047244094488188976:128>> { + // CHECK: %[[handle:.*]] = "tfl.var_handle"() <{container = "", shared_name = "states"}> : () -> tensor>> + %0 = "tfl.var_handle"() {container = "", shared_name = "states"} : () -> tensor>> + // CHECK-NEXT: %[[read:.*]] = "tfl.read_variable"(%[[handle]]) : (tensor>>) -> tensor<1x2x3xf32> + %1 = "tfl.read_variable"(%0) : (tensor>>) -> tensor<1x2x3xf32> + // CHECK-NEXT: %[[quantize:.*]] = "tfl.quantize"(%[[read]]) <{qtype = tensor<1x2x3x!quant.uniform:f32, 0.047244094488188976:128>>}> : (tensor<1x2x3xf32>) -> tensor<1x2x3x!quant.uniform:f32, 0.047244094488188976:128>> + %2 = "tfl.quantize"(%1) {qtype = tensor<1x2x3x!quant.uniform:f32, 0.047244094488188976:128>>} : (tensor<1x2x3xf32>) -> tensor<1x2x3x!quant.uniform:f32, 0.047244094488188976:128>> + // CHECK-NEXT: return %[[quantize]] + func.return %2 : tensor<1x2x3x!quant.uniform:f32, 0.047244094488188976:128>> +} + +// ----- + +// CHECK-LABEL: QuantizeTposeConv +func.func @QuantizeTposeConv(%arg0: tensor<2x2x3x2048xf32>) -> tensor<2x3x2x2048x!quant.uniform> { + %output_shape = arith.constant dense<[2, 3, 2, 2048]> : tensor<4xi32> + // CHECK: %[[QARG0:.*]] = "tfl.quantize"(%arg0) + %q_arg0 = "tfl.quantize"(%arg0) {qtype = tensor<2x2x3x2048x!quant.uniform>} : (tensor<2x2x3x2048xf32>) -> tensor<2x2x3x2048x!quant.uniform> + %dq_arg0 = "tfl.dequantize"(%q_arg0) : (tensor<2x2x3x2048x!quant.uniform>) -> tensor<2x2x3x2048xf32> + // CHECK: %[[W:.*]] = "tfl.pseudo_qconst"() + %q_weighs = "tfl.pseudo_qconst"() {qtype = tensor<4x2x2x2048x!quant.uniform:f32, 0.15:151>>, value = dense<-76> : tensor<4x2x2x2048xi8>} : () -> tensor<4x2x2x2048x!quant.uniform:f32, 0.15:151>> + %dq_weights = "tfl.dequantize"(%q_weighs) : (tensor<4x2x2x2048x!quant.uniform:f32, 0.15:151>>) -> tensor<4x2x2x2048xf32> + %bias = "tfl.no_value"() {value} : () -> none + // CHECK: %[[CONV:.*]] = "tfl.transpose_conv"(%cst, %[[W]], %[[QARG0]], %0) <{fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<4xi32>, tensor<4x2x2x2048x!quant.uniform:f32, 1.500000e-01:151>>, tensor<2x2x3x2048x!quant.uniform>, none) -> tensor<2x3x2x2048x!quant.uniform> + %out = "tfl.transpose_conv"(%output_shape, %dq_weights, %dq_arg0, %bias) {fused_activation_function = "NONE", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32} : (tensor<4xi32>, tensor<4x2x2x2048xf32>, tensor<2x2x3x2048xf32>, none) -> tensor<2x3x2x2048xf32> + %q_out = "tfl.quantize"(%out) {qtype = tensor<2x3x2x2048x!quant.uniform>} : (tensor<2x3x2x2048xf32>) -> tensor<2x3x2x2048x!quant.uniform> + // CHECK: return %[[CONV]] + func.return %q_out : tensor<2x3x2x2048x!quant.uniform> +} + +// ----- + +// CHECK-LABEL: foldQuantWeightsIntoEmbeddingLookup +func.func @foldQuantWeightsIntoEmbeddingLookup(%arg0: tensor<3xi32>) -> tensor<3x512xf32> { + %q_weighs = "tfl.pseudo_qconst"() {qtype = tensor<3074x512x!quant.uniform:f32, 0.15:151>>, value = dense<-76> : tensor<3074x512xi8>} : () -> tensor<3074x512x!quant.uniform:f32, 0.15:151>> + // CHECK-NOT: "tfl.dequantize" + %dq_weights = "tfl.dequantize"(%q_weighs) : (tensor<3074x512x!quant.uniform:f32, 0.15:151>>) -> tensor<3074x512xf32> + // CHECK: "tfl.embedding_lookup"(%arg0, %0) : (tensor<3xi32>, tensor<3074x512x!quant.uniform:f32 + %out = "tfl.embedding_lookup"(%arg0, %dq_weights) : (tensor<3xi32>, tensor<3074x512xf32>) -> tensor<3x512xf32> + func.return %out : tensor<3x512xf32> +} + +// ----- + +// CHECK-LABEL: RequantizationSquash +func.func @RequantizationSquash(%arg0: tensor<64x1x128xf32>) -> tensor<64x128xf32> { + %cst_132 = arith.constant dense<[64, 128]> : tensor<2xi32> + %683 = "tfl.quantize"(%arg0) <{qtype = tensor<64x1x128x!quant.uniform>}> {volatile} : (tensor<64x1x128xf32>) -> tensor<64x1x128x!quant.uniform> + // CHECK-NOT: "tfl.dequantize"(%{{.*}}) : (tensor<64x1x128x!quant.uniform>) -> tensor<64x1x128xf32> + %688 = "tfl.dequantize"(%683) : (tensor<64x1x128x!quant.uniform>) -> tensor<64x1x128xf32> + %698 = "tfl.quantize"(%688) <{qtype = tensor<64x1x128x!quant.uniform>}> : (tensor<64x1x128xf32>) -> tensor<64x1x128x!quant.uniform> + %699 = "tfl.dequantize"(%698) : (tensor<64x1x128x!quant.uniform>) -> tensor<64x1x128xf32> + // CHECK: "tfl.reshape" + // CHECK-SAME: (tensor<64x1x128x!quant.uniform>, tensor<2xi32>) -> tensor<64x128x!quant.uniform> + %700 = "tfl.reshape"(%699, %cst_132) : (tensor<64x1x128xf32>, tensor<2xi32>) -> tensor<64x128xf32> + %701 = "tfl.quantize"(%700) <{qtype = tensor<64x128x!quant.uniform>}> : (tensor<64x128xf32>) -> tensor<64x128x!quant.uniform> + %702 = "tfl.dequantize"(%701) : (tensor<64x128x!quant.uniform>) -> tensor<64x128xf32> + func.return %702 : tensor<64x128xf32> +} + +// ----- + +// CHECK-LABEL: RequantizationDifferentScalesNoSquash +func.func @RequantizationDifferentScalesNoSquash(%arg0: tensor<64x1x128xf32>) -> tensor<64x128xf32> { + %cst_132 = arith.constant dense<[64, 128]> : tensor<2xi32> + %683 = "tfl.quantize"(%arg0) <{qtype = tensor<64x1x128x!quant.uniform>}> {volatile} : (tensor<64x1x128xf32>) -> tensor<64x1x128x!quant.uniform> + %688 = "tfl.dequantize"(%683) : (tensor<64x1x128x!quant.uniform>) -> tensor<64x1x128xf32> + // CHECK: %[[REQUANT:.*]] = "tfl.quantize"(%{{.*}}) <{qtype = tensor<64x1x128x!quant.uniform>}> : (tensor<64x1x128xf32>) -> tensor<64x1x128x!quant.uniform> + %698 = "tfl.quantize"(%688) <{qtype = tensor<64x1x128x!quant.uniform>}> : (tensor<64x1x128xf32>) -> tensor<64x1x128x!quant.uniform> + %699 = "tfl.dequantize"(%698) : (tensor<64x1x128x!quant.uniform>) -> tensor<64x1x128xf32> + // CHECK: "tfl.reshape"(%[[REQUANT]], %{{.*}}) : (tensor<64x1x128x!quant.uniform>, tensor<2xi32>) -> tensor<64x128x!quant.uniform> + %700 = "tfl.reshape"(%699, %cst_132) : (tensor<64x1x128xf32>, tensor<2xi32>) -> tensor<64x128xf32> + %701 = "tfl.quantize"(%700) <{qtype = tensor<64x128x!quant.uniform>}> : (tensor<64x128xf32>) -> tensor<64x128x!quant.uniform> + %702 = "tfl.dequantize"(%701) : (tensor<64x128x!quant.uniform>) -> tensor<64x128xf32> + func.return %702 : tensor<64x128xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/fuse-tftext.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/fuse-tftext.mlir new file mode 100644 index 0000000000000000000000000000000000000000..11a9d27f67bde88810bcb76c059c2ad4be9e2e5e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/fuse-tftext.mlir @@ -0,0 +1,3453 @@ +// RUN: litert-opt -tfl-prepare-composite-funcs-tf="fuse-tftext=true" %s | FileCheck %s + +func.func private @whitespace_tokenizer_rank1(%arg0: tensor<1x!tf_type.string> {tf._user_specified_name = "input"}) -> (tensor, tensor) attributes {tf._input_shapes = [#tf_type.shape<1>], tf._implements = #tf_type.func<@"tftext:WhitespaceTokenizer", {}>, tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<[0, 1]> : tensor<2xi64>} : () -> tensor<2xi64> + %1 = "tf.Const"() {value = dense<[]> : tensor<0xi64>} : () -> tensor<0xi64> + %2 = "tf.Const"() {value = dense : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<-1> : tensor} : () -> tensor + %4 = "tf.Const"() {value = dense<[[0], [1]]> : tensor<2x1xi64>} : () -> tensor<2x1xi64> + %5 = "tf.Const"() {value = dense<-1> : tensor<1xi32>} : () -> tensor<1xi32> + %6 = "tf.Const"() {value = dense<2> : tensor<1xi32>} : () -> tensor<1xi32> + %7 = "tf.Const"() {value = dense<1> : tensor} : () -> tensor + %8 = "tf.Const"() {value = dense<2> : tensor} : () -> tensor + %9 = "tf.Const"() {value = dense<[]> : tensor<0xi32>} : () -> tensor<0xi32> + %10 = "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %11 = "tf.Const"() {value = dense<0> : tensor<1xi64>} : () -> tensor<1xi64> + %12 = "tf.Const"() {value = dense<1> : tensor<1xi64>} : () -> tensor<1xi64> + %13 = "tf.Const"() {value = dense<1> : tensor} : () -> tensor + %14 = "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %15 = "tf.Const"() {value = dense<0> : tensor<1xi32>} : () -> tensor<1xi32> + %16 = "tf.Const"() {value = dense<1> : tensor<1xi32>} : () -> tensor<1xi32> + %17 = "tf.If"(%2, %2, %13, %13) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_RaggedConcat_assert_equal_1_Assert_AssertGuard_false_3210, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_RaggedConcat_assert_equal_1_Assert_AssertGuard_true_3200} : (tensor, tensor, tensor, tensor) -> tensor + %18 = "tf.Identity"(%17) {device = ""} : (tensor) -> tensor + %19 = "tf.StringLength"(%arg0) {device = "", unit = "BYTE"} : (tensor<1x!tf_type.string>) -> tensor<1xi32> + %20 = "tf.ExpandDims"(%19, %7) {device = ""} : (tensor<1xi32>, tensor) -> tensor<1x1xi32> + %21 = "tf.Cast"(%20) {Truncate = false, device = ""} : (tensor<1x1xi32>) -> tensor<1x1xi64> + %22 = "tf.Reshape"(%21, %12) {device = ""} : (tensor<1x1xi64>, tensor<1xi64>) -> tensor<1xi64> + %23 = "tf.Reshape"(%arg0, %5) {device = ""} : (tensor<1x!tf_type.string>, tensor<1xi32>) -> tensor<1x!tf_type.string> + %24:3 = "tf.UnicodeDecodeWithOffsets"(%23) {Tsplits = i64, device = "", errors = "replace", input_encoding = "UTF-8", replace_control_characters = false, replacement_char = 65533 : i64} : (tensor<1x!tf_type.string>) -> (tensor<2xi64>, tensor, tensor) + %25 = "tf.StridedSlice"(%24#0, %15, %5, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1xi64> + %26 = "tf.AddV2"(%25, %13) {device = ""} : (tensor<1xi64>, tensor) -> tensor<1xi64> + %27 = "tf.StridedSlice"(%24#0, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1xi64> + %28 = "tf.Minimum"(%26, %27) {device = ""} : (tensor<1xi64>, tensor<1xi64>) -> tensor<1xi64> + %29:2 = "tf.RaggedRange"(%28, %27, %13) {T = i64, Tsplits = i64, device = ""} : (tensor<1xi64>, tensor<1xi64>, tensor) -> (tensor<2xi64>, tensor) + %30 = "tf.StridedSlice"(%29#0, %5, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %31 = "tf.AddV2"(%30, %12) {device = ""} : (tensor, tensor<1xi64>) -> tensor<1xi64> + %32 = "tf.ConcatV2"(%29#0, %31, %14) {device = ""} : (tensor<2xi64>, tensor<1xi64>, tensor) -> tensor<3xi64> + %33 = "tf.GatherV2"(%24#2, %29#1, %14) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %34 = "tf.ConcatV2"(%33, %22, %14) {device = ""} : (tensor, tensor<1xi64>, tensor) -> tensor + %35:2 = "tf.RaggedGather"(%32, %34, %0) {OUTPUT_RAGGED_RANK = 1 : i64, PARAMS_RAGGED_RANK = 1 : i64, Tindices = i64, Tsplits = i64, Tvalues = i64, device = ""} : (tensor<3xi64>, tensor, tensor<2xi64>) -> (tensor, tensor) + %36:5 = "tf.WhitespaceTokenizeWithOffsets"(%24#1, %24#0) {Tsplits = i64, device = ""} : (tensor, tensor<2xi64>) -> (tensor, tensor, tensor, tensor, tensor) + %37 = "tf.StridedSlice"(%36#1, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %38 = "tf.Equal"(%37, %10) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %39 = "tf.All"(%38, %9) {device = "", keep_dims = false} : (tensor, tensor<0xi32>) -> tensor + %40 = "tf.If"(%39, %39, %37, %10) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_false_3970, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_true_3960} : (tensor, tensor, tensor, tensor) -> tensor + %41 = "tf.Identity"(%40) {device = ""} : (tensor) -> tensor + %42 = "tf.StridedSlice"(%36#1, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %43 = "tf.StridedSlice"(%36#1, %15, %5, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %44 = "tf.Sub"(%42, %43) {device = ""} : (tensor, tensor) -> tensor + %45 = "tf.LessEqual"(%10, %44) {device = ""} : (tensor, tensor) -> tensor + %46 = "tf.All"(%45, %15) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %47 = "tf.If"(%46, %46, %44) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_false_4330, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_true_4320} : (tensor, tensor, tensor) -> tensor + %48 = "tf.Identity"(%47) {device = ""} : (tensor) -> tensor + %49 = "tf.Identity"(%36#1) {_class = ["loc:@WhitespaceTokenize/WhitespaceTokenizeWithOffsets"], device = ""} : (tensor) -> tensor + %50 = "tf.StridedSlice"(%49, %5, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %51 = "tf.Shape"(%36#0) {device = ""} : (tensor) -> tensor<1xi64> + %52 = "tf.StridedSlice"(%51, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %53 = "tf.Equal"(%50, %52) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %54 = "tf.All"(%53, %9) {device = "", keep_dims = false} : (tensor, tensor<0xi32>) -> tensor + %55 = "tf.If"(%54, %54, %50, %52) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_false_4670, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_true_4660} : (tensor, tensor, tensor, tensor) -> tensor + %56 = "tf.Identity"(%55) {device = ""} : (tensor) -> tensor + %57 = "tf.Identity"(%49) {_class = ["loc:@WhitespaceTokenize/WhitespaceTokenizeWithOffsets"], device = ""} : (tensor) -> tensor + %58 = "tf.Shape"(%57) {device = ""} : (tensor) -> tensor<1xi64> + %59 = "tf.StridedSlice"(%58, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %60 = "tf.Sub"(%59, %13) {device = ""} : (tensor, tensor) -> tensor + %61 = "tf.StridedSlice"(%36#4, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %62 = "tf.Equal"(%61, %10) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %63 = "tf.All"(%62, %9) {device = "", keep_dims = false} : (tensor, tensor<0xi32>) -> tensor + %64 = "tf.If"(%63, %63, %61, %10) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_false_5040, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_true_5030} : (tensor, tensor, tensor, tensor) -> tensor + %65 = "tf.Identity"(%64) {device = ""} : (tensor) -> tensor + %66 = "tf.StridedSlice"(%36#4, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %67 = "tf.StridedSlice"(%36#4, %15, %5, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %68 = "tf.Sub"(%66, %67) {device = ""} : (tensor, tensor) -> tensor + %69 = "tf.LessEqual"(%10, %68) {device = ""} : (tensor, tensor) -> tensor + %70 = "tf.All"(%69, %15) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %71 = "tf.If"(%70, %70, %68) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_false_5400, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_true_5390} : (tensor, tensor, tensor) -> tensor + %72 = "tf.Identity"(%71) {device = ""} : (tensor) -> tensor + %73 = "tf.Identity"(%36#4) {_class = ["loc:@WhitespaceTokenize/WhitespaceTokenizeWithOffsets"], device = ""} : (tensor) -> tensor + %74 = "tf.StridedSlice"(%73, %5, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %75 = "tf.Equal"(%74, %60) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %76 = "tf.All"(%75, %9) {device = "", keep_dims = false} : (tensor, tensor<0xi32>) -> tensor + %77 = "tf.If"(%76, %76, %74, %60) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_assert_equal_1_Assert_AssertGuard_false_5760, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_assert_equal_1_Assert_AssertGuard_true_5750} : (tensor, tensor, tensor, tensor) -> tensor + %78 = "tf.Identity"(%77) {device = ""} : (tensor) -> tensor + %79 = "tf.Identity"(%73) {_class = ["loc:@WhitespaceTokenize/WhitespaceTokenizeWithOffsets"], device = ""} : (tensor) -> tensor + %80 = "tf.StridedSlice"(%36#4, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %81 = "tf.Equal"(%80, %10) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %82 = "tf.All"(%81, %9) {device = "", keep_dims = false} : (tensor, tensor<0xi32>) -> tensor + %83 = "tf.If"(%82, %82, %80, %10) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_false_6110, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_true_6100} : (tensor, tensor, tensor, tensor) -> tensor + %84 = "tf.Identity"(%83) {device = ""} : (tensor) -> tensor + %85 = "tf.StridedSlice"(%36#4, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %86 = "tf.StridedSlice"(%36#4, %15, %5, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %87 = "tf.Sub"(%85, %86) {device = ""} : (tensor, tensor) -> tensor + %88 = "tf.LessEqual"(%10, %87) {device = ""} : (tensor, tensor) -> tensor + %89 = "tf.All"(%88, %15) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %90 = "tf.If"(%89, %89, %87) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_false_6470, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_true_6460} : (tensor, tensor, tensor) -> tensor + %91 = "tf.Identity"(%90) {device = ""} : (tensor) -> tensor + %92 = "tf.Identity"(%36#4) {_class = ["loc:@WhitespaceTokenize/WhitespaceTokenizeWithOffsets"], device = ""} : (tensor) -> tensor + %93 = "tf.StridedSlice"(%92, %5, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %94 = "tf.Shape"(%36#2) {device = ""} : (tensor) -> tensor<1xi64> + %95 = "tf.StridedSlice"(%94, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %96 = "tf.Equal"(%93, %95) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %97 = "tf.All"(%96, %9) {device = "", keep_dims = false} : (tensor, tensor<0xi32>) -> tensor + %98 = "tf.If"(%97, %97, %93, %95) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_false_6810, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_true_6800} : (tensor, tensor, tensor, tensor) -> tensor + %99 = "tf.Identity"(%98) {device = ""} : (tensor) -> tensor + %100 = "tf.Identity"(%92) {_class = ["loc:@WhitespaceTokenize/WhitespaceTokenizeWithOffsets"], device = ""} : (tensor) -> tensor + %101 = "tf.Shape"(%100) {device = ""} : (tensor) -> tensor<1xi64> + %102 = "tf.StridedSlice"(%101, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %103 = "tf.Sub"(%102, %13) {device = ""} : (tensor, tensor) -> tensor + %104 = "tf.Equal"(%103, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %105 = "tf.LogicalOr"(%104, %2) {device = ""} : (tensor, tensor) -> tensor + %106 = "tf.Equal"(%103, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %107 = "tf.LogicalOr"(%105, %106) {device = ""} : (tensor, tensor) -> tensor + %108 = "tf.StridedSlice"(%100, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %109 = "tf.StridedSlice"(%100, %15, %5, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %110 = "tf.Sub"(%108, %109) {device = ""} : (tensor, tensor) -> tensor + %111 = "tf.Shape"(%100) {device = ""} : (tensor) -> tensor<1xi64> + %112 = "tf.StridedSlice"(%111, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %113 = "tf.Sub"(%112, %13) {device = ""} : (tensor, tensor) -> tensor + %114 = "tf.Equal"(%113, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %115 = "tf.ExpandDims"(%100, %7) {device = ""} : (tensor, tensor) -> tensor + %116 = "tf.Shape"(%100) {device = ""} : (tensor) -> tensor<1xi32> + %117 = "tf.StridedSlice"(%116, %15, %15, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %118 = "tf.StridedSlice"(%116, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %119 = "tf.StridedSlice"(%116, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %120 = "tf.StridedSlice"(%36#4, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %121 = "tf.Equal"(%120, %10) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %122 = "tf.All"(%121, %9) {device = "", keep_dims = false} : (tensor, tensor<0xi32>) -> tensor + %123 = "tf.If"(%122, %122, %120, %10) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_false_7180, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_true_7170} : (tensor, tensor, tensor, tensor) -> tensor + %124 = "tf.Identity"(%123) {device = ""} : (tensor) -> tensor + %125 = "tf.StridedSlice"(%36#4, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %126 = "tf.StridedSlice"(%36#4, %15, %5, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %127 = "tf.Sub"(%125, %126) {device = ""} : (tensor, tensor) -> tensor + %128 = "tf.LessEqual"(%10, %127) {device = ""} : (tensor, tensor) -> tensor + %129 = "tf.All"(%128, %15) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %130 = "tf.If"(%129, %129, %127) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_false_7540, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_true_7530} : (tensor, tensor, tensor) -> tensor + %131 = "tf.Identity"(%130) {device = ""} : (tensor) -> tensor + %132 = "tf.Identity"(%36#4) {_class = ["loc:@WhitespaceTokenize/WhitespaceTokenizeWithOffsets"], device = ""} : (tensor) -> tensor + %133 = "tf.StridedSlice"(%132, %5, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %134 = "tf.Shape"(%36#3) {device = ""} : (tensor) -> tensor<1xi64> + %135 = "tf.StridedSlice"(%134, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %136 = "tf.Equal"(%133, %135) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %137 = "tf.All"(%136, %9) {device = "", keep_dims = false} : (tensor, tensor<0xi32>) -> tensor + %138 = "tf.If"(%137, %137, %133, %135) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_false_7880, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_true_7870} : (tensor, tensor, tensor, tensor) -> tensor + %139 = "tf.Identity"(%138) {device = ""} : (tensor) -> tensor + %140 = "tf.Identity"(%132) {_class = ["loc:@WhitespaceTokenize/WhitespaceTokenizeWithOffsets"], device = ""} : (tensor) -> tensor + %141 = "tf.Shape"(%140) {device = ""} : (tensor) -> tensor<1xi64> + %142 = "tf.StridedSlice"(%141, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %143 = "tf.Sub"(%142, %13) {device = ""} : (tensor, tensor) -> tensor + %144 = "tf.Equal"(%143, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %145 = "tf.LogicalOr"(%144, %2) {device = ""} : (tensor, tensor) -> tensor + %146 = "tf.Equal"(%143, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %147 = "tf.LogicalOr"(%145, %146) {device = ""} : (tensor, tensor) -> tensor + %148 = "tf.StridedSlice"(%140, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %149 = "tf.StridedSlice"(%140, %15, %5, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %150 = "tf.Sub"(%148, %149) {device = ""} : (tensor, tensor) -> tensor + %151 = "tf.Shape"(%140) {device = ""} : (tensor) -> tensor<1xi64> + %152 = "tf.StridedSlice"(%151, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %153 = "tf.Sub"(%152, %13) {device = ""} : (tensor, tensor) -> tensor + %154 = "tf.Equal"(%153, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %155 = "tf.ExpandDims"(%140, %7) {device = ""} : (tensor, tensor) -> tensor + %156 = "tf.Shape"(%140) {device = ""} : (tensor) -> tensor<1xi32> + %157 = "tf.StridedSlice"(%156, %15, %15, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %158 = "tf.StridedSlice"(%156, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %159 = "tf.StridedSlice"(%156, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %160 = "tf.StridedSlice"(%140, %5, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %161 = "tf.Range"(%10, %160, %13) {device = ""} : (tensor, tensor, tensor) -> tensor + %162 = "tf.StridedSlice"(%140, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %163 = "tf.StridedSlice"(%140, %15, %5, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %164 = "tf.Sub"(%162, %163) {device = ""} : (tensor, tensor) -> tensor + %165 = "tf.If"(%107, %107, %13, %103) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_RaggedGather_Assert_AssertGuard_false_8680, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_RaggedGather_Assert_AssertGuard_true_8670} : (tensor, tensor, tensor, tensor) -> tensor + %166 = "tf.Identity"(%165) {device = ""} : (tensor) -> tensor + %167 = "tf.Equal"(%103, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %168 = "tf.Select"(%167, %13, %103) {device = ""} : (tensor, tensor, tensor) -> tensor + %169 = "tf.Equal"(%168, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %170 = "tf.LogicalOr"(%169, %2) {device = ""} : (tensor, tensor) -> tensor + %171 = "tf.Equal"(%168, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %172 = "tf.LogicalOr"(%170, %171) {device = ""} : (tensor, tensor) -> tensor + %173 = "tf.Select"(%114, %168, %13) {device = ""} : (tensor, tensor, tensor) -> tensor + %174 = "tf.Pack"(%173, %13) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi64> + %175 = "tf.StridedSlice"(%174, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %176 = "tf.Cast"(%175) {Truncate = false, device = ""} : (tensor) -> tensor + %177 = "tf.Reshape"(%176, %9) {device = ""} : (tensor, tensor<0xi32>) -> tensor + %178 = "tf.Pack"(%7, %177) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %179 = "tf.Tile"(%115, %178) {device = ""} : (tensor, tensor<2xi32>) -> tensor + %180 = "tf.Mul"(%177, %118) {device = ""} : (tensor, tensor) -> tensor + %181 = "tf.Pack"(%180) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %182 = "tf.ConcatV2"(%117, %181, %119, %14) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %183 = "tf.Reshape"(%179, %182) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %184 = "tf.Shape"(%183) {device = ""} : (tensor) -> tensor<1xi64> + %185 = "tf.StridedSlice"(%184, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %186 = "tf.Pack"(%175) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %187 = "tf.StridedSlice"(%183, %186, %11, %12) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %188 = "tf.Sub"(%185, %175) {device = ""} : (tensor, tensor) -> tensor + %189 = "tf.Pack"(%188) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %190 = "tf.StridedSlice"(%183, %11, %189, %12) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %191:2 = "tf.RaggedRange"(%190, %187, %13) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %192 = "tf.Select"(%2, %168, %13) {device = ""} : (tensor, tensor, tensor) -> tensor + %193 = "tf.Pack"(%192, %13) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi64> + %194 = "tf.StridedSlice"(%193, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %195 = "tf.Cast"(%194) {Truncate = false, device = ""} : (tensor) -> tensor + %196 = "tf.Reshape"(%195, %9) {device = ""} : (tensor, tensor<0xi32>) -> tensor + %197 = "tf.Pack"(%7, %196) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %198 = "tf.Tile"(%4, %197) {device = ""} : (tensor<2x1xi64>, tensor<2xi32>) -> tensor<2x?xi64> + %199 = "tf.Mul"(%196, %8) {device = ""} : (tensor, tensor) -> tensor + %200 = "tf.Pack"(%199) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %201 = "tf.ConcatV2"(%9, %200, %9, %14) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %202 = "tf.Reshape"(%198, %201) {device = ""} : (tensor<2x?xi64>, tensor<1xi32>) -> tensor + %203 = "tf.Shape"(%202) {device = ""} : (tensor) -> tensor<1xi64> + %204 = "tf.StridedSlice"(%203, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %205 = "tf.Pack"(%194) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %206 = "tf.StridedSlice"(%202, %205, %11, %12) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %207 = "tf.Sub"(%204, %194) {device = ""} : (tensor, tensor) -> tensor + %208 = "tf.Pack"(%207) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %209 = "tf.StridedSlice"(%202, %11, %208, %12) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %210:2 = "tf.RaggedRange"(%209, %206, %13) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %211 = "tf.StridedSlice"(%193, %15, %16, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1xi64> + %212 = "tf.StridedSlice"(%193, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %213 = "tf.Mul"(%212, %12) {device = ""} : (tensor, tensor<1xi64>) -> tensor<1xi64> + %214 = "tf.Tile"(%213, %211) {device = ""} : (tensor<1xi64>, tensor<1xi64>) -> tensor + %215 = "tf.Cumsum"(%214, %14) {device = "", exclusive = false, reverse = false} : (tensor, tensor) -> tensor + %216 = "tf.ConcatV2"(%11, %215, %3) {device = ""} : (tensor<1xi64>, tensor, tensor) -> tensor + %217 = "tf.StridedSlice"(%216, %15, %5, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %218 = "tf.ExpandDims"(%217, %7) {device = ""} : (tensor, tensor) -> tensor + %219 = "tf.Shape"(%217) {device = ""} : (tensor) -> tensor<1xi32> + %220 = "tf.StridedSlice"(%219, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %221 = "tf.Pack"(%220) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %222 = "tf.StridedSlice"(%216, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %223 = "tf.ExpandDims"(%222, %7) {device = ""} : (tensor, tensor) -> tensor + %224 = "tf.Shape"(%222) {device = ""} : (tensor) -> tensor<1xi32> + %225 = "tf.StridedSlice"(%224, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %226 = "tf.Pack"(%225) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %227 = "tf.Equal"(%103, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %228 = "tf.Select"(%227, %168, %13) {device = ""} : (tensor, tensor, tensor) -> tensor + %229 = "tf.Cast"(%228) {Truncate = false, device = ""} : (tensor) -> tensor + %230 = "tf.Reshape"(%229, %9) {device = ""} : (tensor, tensor<0xi32>) -> tensor + %231 = "tf.Pack"(%7, %230) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %232 = "tf.Mul"(%230, %8) {device = ""} : (tensor, tensor) -> tensor + %233 = "tf.Pack"(%232) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %234 = "tf.ConcatV2"(%9, %233, %9, %14) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %235 = "tf.Pack"(%228) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %236 = "tf.Pack"(%10, %103) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi64> + %237 = "tf.ExpandDims"(%236, %7) {device = ""} : (tensor<2xi64>, tensor) -> tensor<2x1xi64> + %238 = "tf.Tile"(%237, %231) {device = ""} : (tensor<2x1xi64>, tensor<2xi32>) -> tensor<2x?xi64> + %239 = "tf.Reshape"(%238, %234) {device = ""} : (tensor<2x?xi64>, tensor<1xi32>) -> tensor + %240 = "tf.Shape"(%239) {device = ""} : (tensor) -> tensor<1xi64> + %241 = "tf.StridedSlice"(%240, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %242 = "tf.Sub"(%241, %228) {device = ""} : (tensor, tensor) -> tensor + %243 = "tf.Pack"(%242) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %244 = "tf.StridedSlice"(%239, %11, %243, %12) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %245 = "tf.StridedSlice"(%239, %235, %11, %12) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %246:2 = "tf.RaggedRange"(%244, %245, %13) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %247 = "tf.GatherV2"(%110, %246#1, %14) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %248 = "tf.Cast"(%247) {Truncate = false, device = ""} : (tensor) -> tensor + %249 = "tf.BroadcastTo"(%248, %221) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %250 = "tf.Max"(%249, %15) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %251 = "tf.Maximum"(%14, %250) {device = ""} : (tensor, tensor) -> tensor + %252 = "tf.Range"(%14, %251, %7) {device = ""} : (tensor, tensor, tensor) -> tensor + %253 = "tf.Pack"(%7, %251) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %254 = "tf.Tile"(%218, %253) {device = ""} : (tensor, tensor<2xi32>) -> tensor + %255 = "tf.Shape"(%254) {device = ""} : (tensor) -> tensor<2xi32> + %256 = "tf.StridedSlice"(%255, %15, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<2xi32> + %257 = "tf.Prod"(%256, %15) {device = "", keep_dims = false} : (tensor<2xi32>, tensor<1xi32>) -> tensor + %258 = "tf.Pack"(%257) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %259 = "tf.Shape"(%254) {device = ""} : (tensor) -> tensor<2xi32> + %260 = "tf.StridedSlice"(%259, %15, %15, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %261 = "tf.Shape"(%254) {device = ""} : (tensor) -> tensor<2xi32> + %262 = "tf.StridedSlice"(%261, %6, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %263 = "tf.ConcatV2"(%260, %258, %262, %14) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %264 = "tf.Reshape"(%254, %263) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %265 = "tf.ExpandDims"(%249, %3) {device = ""} : (tensor, tensor) -> tensor + %266 = "tf.Less"(%252, %265) {device = ""} : (tensor, tensor) -> tensor + %267 = "tf.Reshape"(%266, %5) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %268 = "tf.Where"(%267) {device = ""} : (tensor) -> tensor + %269 = "tf.Squeeze"(%268) {device = "", squeeze_dims = [1]} : (tensor) -> tensor + %270 = "tf.GatherV2"(%264, %269, %14) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %271 = "tf.Cast"(%247) {Truncate = false, device = ""} : (tensor) -> tensor + %272 = "tf.BroadcastTo"(%271, %226) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %273 = "tf.Max"(%272, %15) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %274 = "tf.Maximum"(%14, %273) {device = ""} : (tensor, tensor) -> tensor + %275 = "tf.Range"(%14, %274, %7) {device = ""} : (tensor, tensor, tensor) -> tensor + %276 = "tf.Pack"(%7, %274) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %277 = "tf.Tile"(%223, %276) {device = ""} : (tensor, tensor<2xi32>) -> tensor + %278 = "tf.Shape"(%277) {device = ""} : (tensor) -> tensor<2xi32> + %279 = "tf.StridedSlice"(%278, %15, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<2xi32> + %280 = "tf.Prod"(%279, %15) {device = "", keep_dims = false} : (tensor<2xi32>, tensor<1xi32>) -> tensor + %281 = "tf.Pack"(%280) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %282 = "tf.Shape"(%277) {device = ""} : (tensor) -> tensor<2xi32> + %283 = "tf.StridedSlice"(%282, %15, %15, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %284 = "tf.Shape"(%277) {device = ""} : (tensor) -> tensor<2xi32> + %285 = "tf.StridedSlice"(%284, %6, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %286 = "tf.ConcatV2"(%283, %281, %285, %14) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %287 = "tf.Reshape"(%277, %286) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %288 = "tf.ExpandDims"(%272, %3) {device = ""} : (tensor, tensor) -> tensor + %289 = "tf.Less"(%275, %288) {device = ""} : (tensor, tensor) -> tensor + %290 = "tf.Reshape"(%289, %5) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %291 = "tf.Where"(%290) {device = ""} : (tensor) -> tensor + %292 = "tf.Squeeze"(%291) {device = "", squeeze_dims = [1]} : (tensor) -> tensor + %293 = "tf.GatherV2"(%287, %292, %14) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %294:2 = "tf.RaggedRange"(%270, %293, %13) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %295 = "tf.If"(%172, %172, %168, %13) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_RaggedGather_Assert_1_AssertGuard_false_9750, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_RaggedGather_Assert_1_AssertGuard_true_9740} : (tensor, tensor, tensor, tensor) -> tensor + %296 = "tf.Identity"(%295) {device = ""} : (tensor) -> tensor + %297 = "tf.Select"(%2, %168, %13) {device = ""} : (tensor, tensor, tensor) -> tensor + %298 = "tf.Pack"(%297) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %299 = "tf.ConcatV2"(%1, %298, %12, %14) {device = ""} : (tensor<0xi64>, tensor<1xi64>, tensor<1xi64>, tensor) -> tensor<2xi64> + %300 = "tf.StridedSlice"(%299, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %301 = "tf.Equal"(%300, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %302 = "tf.StridedSlice"(%299, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %303 = "tf.StridedSlice"(%299, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %304 = "tf.Equal"(%303, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %305 = "tf.If"(%304, %304, %303, %247) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_RaggedGather_Assert_2_AssertGuard_false_10240, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_RaggedGather_Assert_2_AssertGuard_true_10230} : (tensor, tensor, tensor, tensor) -> tensor + %306 = "tf.Identity"(%305) {device = ""} : (tensor) -> tensor + %307 = "tf.If"(%301, %301, %247, %302) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_RaggedGather_Assert_3_AssertGuard_false_10600, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_RaggedGather_Assert_3_AssertGuard_true_10590} : (tensor, tensor, tensor, tensor) -> tensor + %308 = "tf.If"(%147, %147, %13, %143) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_Assert_AssertGuard_false_15300, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_Assert_AssertGuard_true_15290} : (tensor, tensor, tensor, tensor) -> tensor + %309 = "tf.Identity"(%308) {device = ""} : (tensor) -> tensor + %310 = "tf.Equal"(%143, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %311 = "tf.Select"(%310, %13, %143) {device = ""} : (tensor, tensor, tensor) -> tensor + %312 = "tf.Equal"(%311, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %313 = "tf.LogicalOr"(%312, %2) {device = ""} : (tensor, tensor) -> tensor + %314 = "tf.Equal"(%311, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %315 = "tf.LogicalOr"(%313, %314) {device = ""} : (tensor, tensor) -> tensor + %316 = "tf.Select"(%154, %311, %13) {device = ""} : (tensor, tensor, tensor) -> tensor + %317 = "tf.Pack"(%316, %13) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi64> + %318 = "tf.StridedSlice"(%317, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %319 = "tf.Cast"(%318) {Truncate = false, device = ""} : (tensor) -> tensor + %320 = "tf.Reshape"(%319, %9) {device = ""} : (tensor, tensor<0xi32>) -> tensor + %321 = "tf.Pack"(%7, %320) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %322 = "tf.Tile"(%155, %321) {device = ""} : (tensor, tensor<2xi32>) -> tensor + %323 = "tf.Mul"(%320, %158) {device = ""} : (tensor, tensor) -> tensor + %324 = "tf.Pack"(%323) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %325 = "tf.ConcatV2"(%157, %324, %159, %14) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %326 = "tf.Reshape"(%322, %325) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %327 = "tf.Shape"(%326) {device = ""} : (tensor) -> tensor<1xi64> + %328 = "tf.StridedSlice"(%327, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %329 = "tf.Pack"(%318) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %330 = "tf.StridedSlice"(%326, %329, %11, %12) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %331 = "tf.Sub"(%328, %318) {device = ""} : (tensor, tensor) -> tensor + %332 = "tf.Pack"(%331) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %333 = "tf.StridedSlice"(%326, %11, %332, %12) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %334:2 = "tf.RaggedRange"(%333, %330, %13) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %335 = "tf.GatherV2"(%161, %334#1, %14) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %336 = "tf.StridedSlice"(%317, %15, %16, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1xi64> + %337 = "tf.StridedSlice"(%317, %15, %16, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1xi64> + %338 = "tf.StridedSlice"(%317, %6, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi64> + %339 = "tf.ConcatV2"(%337, %338, %14) {device = ""} : (tensor<1xi64>, tensor<0xi64>, tensor) -> tensor<1xi64> + %340 = "tf.StridedSlice"(%317, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %341 = "tf.Mul"(%164, %340) {device = ""} : (tensor, tensor) -> tensor + %342 = "tf.Tile"(%341, %336) {device = ""} : (tensor, tensor<1xi64>) -> tensor + %343 = "tf.Cumsum"(%342, %14) {device = "", exclusive = false, reverse = false} : (tensor, tensor) -> tensor + %344 = "tf.ConcatV2"(%11, %343, %3) {device = ""} : (tensor<1xi64>, tensor, tensor) -> tensor + %345 = "tf.Shape"(%344) {device = ""} : (tensor) -> tensor<1xi64> + %346 = "tf.StridedSlice"(%345, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %347 = "tf.Sub"(%346, %13) {device = ""} : (tensor, tensor) -> tensor + %348 = "tf.Equal"(%347, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %349 = "tf.LogicalOr"(%348, %2) {device = ""} : (tensor, tensor) -> tensor + %350 = "tf.Equal"(%347, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %351 = "tf.LogicalOr"(%349, %350) {device = ""} : (tensor, tensor) -> tensor + %352 = "tf.StridedSlice"(%344, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %353 = "tf.StridedSlice"(%344, %15, %5, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %354 = "tf.Sub"(%352, %353) {device = ""} : (tensor, tensor) -> tensor + %355 = "tf.Shape"(%344) {device = ""} : (tensor) -> tensor<1xi64> + %356 = "tf.StridedSlice"(%355, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %357 = "tf.Sub"(%356, %13) {device = ""} : (tensor, tensor) -> tensor + %358 = "tf.Equal"(%357, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %359 = "tf.ExpandDims"(%344, %7) {device = ""} : (tensor, tensor) -> tensor + %360 = "tf.Shape"(%344) {device = ""} : (tensor) -> tensor<1xi32> + %361 = "tf.StridedSlice"(%360, %15, %15, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %362 = "tf.StridedSlice"(%360, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %363 = "tf.StridedSlice"(%360, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %364 = "tf.Select"(%2, %311, %13) {device = ""} : (tensor, tensor, tensor) -> tensor + %365 = "tf.Pack"(%364, %13) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi64> + %366 = "tf.StridedSlice"(%365, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %367 = "tf.Cast"(%366) {Truncate = false, device = ""} : (tensor) -> tensor + %368 = "tf.Reshape"(%367, %9) {device = ""} : (tensor, tensor<0xi32>) -> tensor + %369 = "tf.Pack"(%7, %368) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %370 = "tf.Tile"(%4, %369) {device = ""} : (tensor<2x1xi64>, tensor<2xi32>) -> tensor<2x?xi64> + %371 = "tf.Mul"(%368, %8) {device = ""} : (tensor, tensor) -> tensor + %372 = "tf.Pack"(%371) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %373 = "tf.ConcatV2"(%9, %372, %9, %14) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %374 = "tf.Reshape"(%370, %373) {device = ""} : (tensor<2x?xi64>, tensor<1xi32>) -> tensor + %375 = "tf.Shape"(%374) {device = ""} : (tensor) -> tensor<1xi64> + %376 = "tf.StridedSlice"(%375, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %377 = "tf.Pack"(%366) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %378 = "tf.StridedSlice"(%374, %377, %11, %12) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %379 = "tf.Sub"(%376, %366) {device = ""} : (tensor, tensor) -> tensor + %380 = "tf.Pack"(%379) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %381 = "tf.StridedSlice"(%374, %11, %380, %12) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %382:2 = "tf.RaggedRange"(%381, %378, %13) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %383 = "tf.GatherV2"(%11, %382#1, %14) {batch_dims = 0 : i64, device = ""} : (tensor<1xi64>, tensor, tensor) -> tensor + %384 = "tf.GatherV2"(%12, %383, %14) {batch_dims = 0 : i64, device = ""} : (tensor<1xi64>, tensor, tensor) -> tensor + %385 = "tf.StridedSlice"(%365, %15, %16, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1xi64> + %386 = "tf.StridedSlice"(%365, %15, %16, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1xi64> + %387 = "tf.StridedSlice"(%365, %6, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi64> + %388 = "tf.ConcatV2"(%386, %387, %14) {device = ""} : (tensor<1xi64>, tensor<0xi64>, tensor) -> tensor<1xi64> + %389 = "tf.Tile"(%384, %388) {device = ""} : (tensor, tensor<1xi64>) -> tensor + %390 = "tf.StridedSlice"(%365, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %391 = "tf.Mul"(%390, %12) {device = ""} : (tensor, tensor<1xi64>) -> tensor<1xi64> + %392 = "tf.Tile"(%391, %385) {device = ""} : (tensor<1xi64>, tensor<1xi64>) -> tensor + %393 = "tf.Cumsum"(%392, %14) {device = "", exclusive = false, reverse = false} : (tensor, tensor) -> tensor + %394 = "tf.ConcatV2"(%11, %393, %3) {device = ""} : (tensor<1xi64>, tensor, tensor) -> tensor + %395 = "tf.StridedSlice"(%394, %15, %5, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %396 = "tf.ExpandDims"(%395, %7) {device = ""} : (tensor, tensor) -> tensor + %397 = "tf.Shape"(%395) {device = ""} : (tensor) -> tensor<1xi32> + %398 = "tf.StridedSlice"(%397, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %399 = "tf.Pack"(%398) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %400 = "tf.StridedSlice"(%394, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %401 = "tf.ExpandDims"(%400, %7) {device = ""} : (tensor, tensor) -> tensor + %402 = "tf.Shape"(%400) {device = ""} : (tensor) -> tensor<1xi32> + %403 = "tf.StridedSlice"(%402, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %404 = "tf.Pack"(%403) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %405 = "tf.Equal"(%143, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %406 = "tf.Select"(%405, %311, %13) {device = ""} : (tensor, tensor, tensor) -> tensor + %407 = "tf.Cast"(%406) {Truncate = false, device = ""} : (tensor) -> tensor + %408 = "tf.Reshape"(%407, %9) {device = ""} : (tensor, tensor<0xi32>) -> tensor + %409 = "tf.Pack"(%7, %408) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %410 = "tf.Mul"(%408, %8) {device = ""} : (tensor, tensor) -> tensor + %411 = "tf.Pack"(%410) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %412 = "tf.ConcatV2"(%9, %411, %9, %14) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %413 = "tf.Pack"(%406) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %414 = "tf.Pack"(%10, %143) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi64> + %415 = "tf.ExpandDims"(%414, %7) {device = ""} : (tensor<2xi64>, tensor) -> tensor<2x1xi64> + %416 = "tf.Tile"(%415, %409) {device = ""} : (tensor<2x1xi64>, tensor<2xi32>) -> tensor<2x?xi64> + %417 = "tf.Reshape"(%416, %412) {device = ""} : (tensor<2x?xi64>, tensor<1xi32>) -> tensor + %418 = "tf.Shape"(%417) {device = ""} : (tensor) -> tensor<1xi64> + %419 = "tf.StridedSlice"(%418, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %420 = "tf.Sub"(%419, %406) {device = ""} : (tensor, tensor) -> tensor + %421 = "tf.Pack"(%420) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %422 = "tf.StridedSlice"(%417, %11, %421, %12) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %423 = "tf.StridedSlice"(%417, %413, %11, %12) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %424:2 = "tf.RaggedRange"(%422, %423, %13) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %425 = "tf.GatherV2"(%150, %424#1, %14) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %426 = "tf.Cast"(%425) {Truncate = false, device = ""} : (tensor) -> tensor + %427 = "tf.BroadcastTo"(%426, %399) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %428 = "tf.Max"(%427, %15) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %429 = "tf.Maximum"(%14, %428) {device = ""} : (tensor, tensor) -> tensor + %430 = "tf.Range"(%14, %429, %7) {device = ""} : (tensor, tensor, tensor) -> tensor + %431 = "tf.Pack"(%7, %429) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %432 = "tf.Tile"(%396, %431) {device = ""} : (tensor, tensor<2xi32>) -> tensor + %433 = "tf.Shape"(%432) {device = ""} : (tensor) -> tensor<2xi32> + %434 = "tf.StridedSlice"(%433, %15, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<2xi32> + %435 = "tf.Prod"(%434, %15) {device = "", keep_dims = false} : (tensor<2xi32>, tensor<1xi32>) -> tensor + %436 = "tf.Pack"(%435) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %437 = "tf.Shape"(%432) {device = ""} : (tensor) -> tensor<2xi32> + %438 = "tf.StridedSlice"(%437, %15, %15, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %439 = "tf.Shape"(%432) {device = ""} : (tensor) -> tensor<2xi32> + %440 = "tf.StridedSlice"(%439, %6, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %441 = "tf.ConcatV2"(%438, %436, %440, %14) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %442 = "tf.Reshape"(%432, %441) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %443 = "tf.ExpandDims"(%427, %3) {device = ""} : (tensor, tensor) -> tensor + %444 = "tf.Less"(%430, %443) {device = ""} : (tensor, tensor) -> tensor + %445 = "tf.Reshape"(%444, %5) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %446 = "tf.Where"(%445) {device = ""} : (tensor) -> tensor + %447 = "tf.Squeeze"(%446) {device = "", squeeze_dims = [1]} : (tensor) -> tensor + %448 = "tf.GatherV2"(%442, %447, %14) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %449 = "tf.Cast"(%425) {Truncate = false, device = ""} : (tensor) -> tensor + %450 = "tf.BroadcastTo"(%449, %404) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %451 = "tf.Max"(%450, %15) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %452 = "tf.Maximum"(%14, %451) {device = ""} : (tensor, tensor) -> tensor + %453 = "tf.Range"(%14, %452, %7) {device = ""} : (tensor, tensor, tensor) -> tensor + %454 = "tf.Pack"(%7, %452) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %455 = "tf.Tile"(%401, %454) {device = ""} : (tensor, tensor<2xi32>) -> tensor + %456 = "tf.Shape"(%455) {device = ""} : (tensor) -> tensor<2xi32> + %457 = "tf.StridedSlice"(%456, %15, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<2xi32> + %458 = "tf.Prod"(%457, %15) {device = "", keep_dims = false} : (tensor<2xi32>, tensor<1xi32>) -> tensor + %459 = "tf.Pack"(%458) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %460 = "tf.Shape"(%455) {device = ""} : (tensor) -> tensor<2xi32> + %461 = "tf.StridedSlice"(%460, %15, %15, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %462 = "tf.Shape"(%455) {device = ""} : (tensor) -> tensor<2xi32> + %463 = "tf.StridedSlice"(%462, %6, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %464 = "tf.ConcatV2"(%461, %459, %463, %14) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %465 = "tf.Reshape"(%455, %464) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %466 = "tf.ExpandDims"(%450, %3) {device = ""} : (tensor, tensor) -> tensor + %467 = "tf.Less"(%453, %466) {device = ""} : (tensor, tensor) -> tensor + %468 = "tf.Reshape"(%467, %5) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %469 = "tf.Where"(%468) {device = ""} : (tensor) -> tensor + %470 = "tf.Squeeze"(%469) {device = "", squeeze_dims = [1]} : (tensor) -> tensor + %471 = "tf.GatherV2"(%465, %470, %14) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %472:2 = "tf.RaggedRange"(%448, %471, %13) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %473 = "tf.GatherV2"(%389, %472#1, %14) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %474 = "tf.If"(%315, %315, %311, %13) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_Assert_1_AssertGuard_false_16370, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_Assert_1_AssertGuard_true_16360} : (tensor, tensor, tensor, tensor) -> tensor + %475 = "tf.Identity"(%474) {device = ""} : (tensor) -> tensor + %476 = "tf.Select"(%2, %311, %13) {device = ""} : (tensor, tensor, tensor) -> tensor + %477 = "tf.Pack"(%476) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %478 = "tf.ConcatV2"(%1, %477, %12, %14) {device = ""} : (tensor<0xi64>, tensor<1xi64>, tensor<1xi64>, tensor) -> tensor<2xi64> + %479 = "tf.StridedSlice"(%478, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %480 = "tf.Equal"(%479, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %481 = "tf.StridedSlice"(%478, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %482 = "tf.StridedSlice"(%478, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %483 = "tf.Equal"(%482, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %484 = "tf.If"(%483, %483, %482, %425) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_Assert_2_AssertGuard_false_16860, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_Assert_2_AssertGuard_true_16850} : (tensor, tensor, tensor, tensor) -> tensor + %485 = "tf.Identity"(%484) {device = ""} : (tensor) -> tensor + %486 = "tf.If"(%480, %480, %425, %481) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_Assert_3_AssertGuard_false_17220, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_Assert_3_AssertGuard_true_17210} : (tensor, tensor, tensor, tensor) -> tensor + %487 = "tf.Identity"(%486) {device = ""} : (tensor) -> tensor + %488 = "tf.If"(%351, %351, %13, %347) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_RaggedGather_1_Assert_AssertGuard_false_21900, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_RaggedGather_1_Assert_AssertGuard_true_21890} : (tensor, tensor, tensor, tensor) -> tensor + %489 = "tf.Identity"(%488) {device = ""} : (tensor) -> tensor + %490 = "tf.Equal"(%347, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %491 = "tf.Select"(%490, %13, %347) {device = ""} : (tensor, tensor, tensor) -> tensor + %492 = "tf.Equal"(%491, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %493 = "tf.LogicalOr"(%492, %2) {device = ""} : (tensor, tensor) -> tensor + %494 = "tf.Equal"(%491, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %495 = "tf.LogicalOr"(%493, %494) {device = ""} : (tensor, tensor) -> tensor + %496 = "tf.Select"(%358, %491, %13) {device = ""} : (tensor, tensor, tensor) -> tensor + %497 = "tf.Pack"(%496, %13) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi64> + %498 = "tf.StridedSlice"(%497, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %499 = "tf.Cast"(%498) {Truncate = false, device = ""} : (tensor) -> tensor + %500 = "tf.Reshape"(%499, %9) {device = ""} : (tensor, tensor<0xi32>) -> tensor + %501 = "tf.Pack"(%7, %500) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %502 = "tf.Tile"(%359, %501) {device = ""} : (tensor, tensor<2xi32>) -> tensor + %503 = "tf.Mul"(%500, %362) {device = ""} : (tensor, tensor) -> tensor + %504 = "tf.Pack"(%503) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %505 = "tf.ConcatV2"(%361, %504, %363, %14) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %506 = "tf.Reshape"(%502, %505) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %507 = "tf.Shape"(%506) {device = ""} : (tensor) -> tensor<1xi64> + %508 = "tf.StridedSlice"(%507, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %509 = "tf.Pack"(%498) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %510 = "tf.StridedSlice"(%506, %509, %11, %12) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %511 = "tf.Sub"(%508, %498) {device = ""} : (tensor, tensor) -> tensor + %512 = "tf.Pack"(%511) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %513 = "tf.StridedSlice"(%506, %11, %512, %12) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %514:2 = "tf.RaggedRange"(%513, %510, %13) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %515 = "tf.Select"(%2, %491, %13) {device = ""} : (tensor, tensor, tensor) -> tensor + %516 = "tf.Pack"(%515, %13) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi64> + %517 = "tf.StridedSlice"(%516, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %518 = "tf.Cast"(%517) {Truncate = false, device = ""} : (tensor) -> tensor + %519 = "tf.Reshape"(%518, %9) {device = ""} : (tensor, tensor<0xi32>) -> tensor + %520 = "tf.Pack"(%7, %519) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %521 = "tf.Tile"(%4, %520) {device = ""} : (tensor<2x1xi64>, tensor<2xi32>) -> tensor<2x?xi64> + %522 = "tf.Mul"(%519, %8) {device = ""} : (tensor, tensor) -> tensor + %523 = "tf.Pack"(%522) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %524 = "tf.ConcatV2"(%9, %523, %9, %14) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %525 = "tf.Reshape"(%521, %524) {device = ""} : (tensor<2x?xi64>, tensor<1xi32>) -> tensor + %526 = "tf.Shape"(%525) {device = ""} : (tensor) -> tensor<1xi64> + %527 = "tf.StridedSlice"(%526, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %528 = "tf.Pack"(%517) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %529 = "tf.StridedSlice"(%525, %528, %11, %12) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %530 = "tf.Sub"(%527, %517) {device = ""} : (tensor, tensor) -> tensor + %531 = "tf.Pack"(%530) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %532 = "tf.StridedSlice"(%525, %11, %531, %12) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %533:2 = "tf.RaggedRange"(%532, %529, %13) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %534 = "tf.StridedSlice"(%516, %15, %16, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1xi64> + %535 = "tf.StridedSlice"(%516, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %536 = "tf.Mul"(%535, %12) {device = ""} : (tensor, tensor<1xi64>) -> tensor<1xi64> + %537 = "tf.Tile"(%536, %534) {device = ""} : (tensor<1xi64>, tensor<1xi64>) -> tensor + %538 = "tf.Cumsum"(%537, %14) {device = "", exclusive = false, reverse = false} : (tensor, tensor) -> tensor + %539 = "tf.ConcatV2"(%11, %538, %3) {device = ""} : (tensor<1xi64>, tensor, tensor) -> tensor + %540 = "tf.StridedSlice"(%539, %15, %5, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %541 = "tf.ExpandDims"(%540, %7) {device = ""} : (tensor, tensor) -> tensor + %542 = "tf.Shape"(%540) {device = ""} : (tensor) -> tensor<1xi32> + %543 = "tf.StridedSlice"(%542, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %544 = "tf.Pack"(%543) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %545 = "tf.StridedSlice"(%539, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %546 = "tf.ExpandDims"(%545, %7) {device = ""} : (tensor, tensor) -> tensor + %547 = "tf.Shape"(%545) {device = ""} : (tensor) -> tensor<1xi32> + %548 = "tf.StridedSlice"(%547, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %549 = "tf.Pack"(%548) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %550 = "tf.Equal"(%347, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %551 = "tf.Select"(%550, %491, %13) {device = ""} : (tensor, tensor, tensor) -> tensor + %552 = "tf.Cast"(%551) {Truncate = false, device = ""} : (tensor) -> tensor + %553 = "tf.Reshape"(%552, %9) {device = ""} : (tensor, tensor<0xi32>) -> tensor + %554 = "tf.Pack"(%7, %553) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %555 = "tf.Mul"(%553, %8) {device = ""} : (tensor, tensor) -> tensor + %556 = "tf.Pack"(%555) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %557 = "tf.ConcatV2"(%9, %556, %9, %14) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %558 = "tf.Pack"(%551) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %559 = "tf.Pack"(%10, %347) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi64> + %560 = "tf.ExpandDims"(%559, %7) {device = ""} : (tensor<2xi64>, tensor) -> tensor<2x1xi64> + %561 = "tf.Tile"(%560, %554) {device = ""} : (tensor<2x1xi64>, tensor<2xi32>) -> tensor<2x?xi64> + %562 = "tf.Reshape"(%561, %557) {device = ""} : (tensor<2x?xi64>, tensor<1xi32>) -> tensor + %563 = "tf.Shape"(%562) {device = ""} : (tensor) -> tensor<1xi64> + %564 = "tf.StridedSlice"(%563, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %565 = "tf.Sub"(%564, %551) {device = ""} : (tensor, tensor) -> tensor + %566 = "tf.Pack"(%565) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %567 = "tf.StridedSlice"(%562, %11, %566, %12) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %568 = "tf.StridedSlice"(%562, %558, %11, %12) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %569:2 = "tf.RaggedRange"(%567, %568, %13) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %570 = "tf.GatherV2"(%354, %569#1, %14) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %571 = "tf.Cast"(%570) {Truncate = false, device = ""} : (tensor) -> tensor + %572 = "tf.BroadcastTo"(%571, %544) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %573 = "tf.Max"(%572, %15) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %574 = "tf.Maximum"(%14, %573) {device = ""} : (tensor, tensor) -> tensor + %575 = "tf.Range"(%14, %574, %7) {device = ""} : (tensor, tensor, tensor) -> tensor + %576 = "tf.Pack"(%7, %574) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %577 = "tf.Tile"(%541, %576) {device = ""} : (tensor, tensor<2xi32>) -> tensor + %578 = "tf.Shape"(%577) {device = ""} : (tensor) -> tensor<2xi32> + %579 = "tf.StridedSlice"(%578, %15, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<2xi32> + %580 = "tf.Prod"(%579, %15) {device = "", keep_dims = false} : (tensor<2xi32>, tensor<1xi32>) -> tensor + %581 = "tf.Pack"(%580) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %582 = "tf.Shape"(%577) {device = ""} : (tensor) -> tensor<2xi32> + %583 = "tf.StridedSlice"(%582, %15, %15, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %584 = "tf.Shape"(%577) {device = ""} : (tensor) -> tensor<2xi32> + %585 = "tf.StridedSlice"(%584, %6, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %586 = "tf.ConcatV2"(%583, %581, %585, %14) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %587 = "tf.Reshape"(%577, %586) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %588 = "tf.ExpandDims"(%572, %3) {device = ""} : (tensor, tensor) -> tensor + %589 = "tf.Less"(%575, %588) {device = ""} : (tensor, tensor) -> tensor + %590 = "tf.Reshape"(%589, %5) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %591 = "tf.Where"(%590) {device = ""} : (tensor) -> tensor + %592 = "tf.Squeeze"(%591) {device = "", squeeze_dims = [1]} : (tensor) -> tensor + %593 = "tf.GatherV2"(%587, %592, %14) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %594 = "tf.Cast"(%570) {Truncate = false, device = ""} : (tensor) -> tensor + %595 = "tf.BroadcastTo"(%594, %549) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %596 = "tf.Max"(%595, %15) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %597 = "tf.Maximum"(%14, %596) {device = ""} : (tensor, tensor) -> tensor + %598 = "tf.Range"(%14, %597, %7) {device = ""} : (tensor, tensor, tensor) -> tensor + %599 = "tf.Pack"(%7, %597) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %600 = "tf.Tile"(%546, %599) {device = ""} : (tensor, tensor<2xi32>) -> tensor + %601 = "tf.Shape"(%600) {device = ""} : (tensor) -> tensor<2xi32> + %602 = "tf.StridedSlice"(%601, %15, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<2xi32> + %603 = "tf.Prod"(%602, %15) {device = "", keep_dims = false} : (tensor<2xi32>, tensor<1xi32>) -> tensor + %604 = "tf.Pack"(%603) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %605 = "tf.Shape"(%600) {device = ""} : (tensor) -> tensor<2xi32> + %606 = "tf.StridedSlice"(%605, %15, %15, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %607 = "tf.Shape"(%600) {device = ""} : (tensor) -> tensor<2xi32> + %608 = "tf.StridedSlice"(%607, %6, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %609 = "tf.ConcatV2"(%606, %604, %608, %14) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %610 = "tf.Reshape"(%600, %609) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %611 = "tf.ExpandDims"(%595, %3) {device = ""} : (tensor, tensor) -> tensor + %612 = "tf.Less"(%598, %611) {device = ""} : (tensor, tensor) -> tensor + %613 = "tf.Reshape"(%612, %5) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %614 = "tf.Where"(%613) {device = ""} : (tensor) -> tensor + %615 = "tf.Squeeze"(%614) {device = "", squeeze_dims = [1]} : (tensor) -> tensor + %616 = "tf.GatherV2"(%610, %615, %14) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %617:2 = "tf.RaggedRange"(%593, %616, %13) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %618 = "tf.If"(%495, %495, %491, %13) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_RaggedGather_1_Assert_1_AssertGuard_false_22970, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_RaggedGather_1_Assert_1_AssertGuard_true_22960} : (tensor, tensor, tensor, tensor) -> tensor + %619 = "tf.Identity"(%618) {device = ""} : (tensor) -> tensor + %620 = "tf.Select"(%2, %491, %13) {device = ""} : (tensor, tensor, tensor) -> tensor + %621 = "tf.Pack"(%620) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %622 = "tf.ConcatV2"(%1, %621, %12, %14) {device = ""} : (tensor<0xi64>, tensor<1xi64>, tensor<1xi64>, tensor) -> tensor<2xi64> + %623 = "tf.StridedSlice"(%622, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %624 = "tf.Equal"(%623, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %625 = "tf.StridedSlice"(%622, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %626 = "tf.StridedSlice"(%622, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %627 = "tf.Equal"(%626, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %628 = "tf.If"(%627, %627, %626, %570) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_RaggedGather_1_Assert_2_AssertGuard_false_23460, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_RaggedGather_1_Assert_2_AssertGuard_true_23450} : (tensor, tensor, tensor, tensor) -> tensor + %629 = "tf.Identity"(%628) {device = ""} : (tensor) -> tensor + %630 = "tf.If"(%624, %624, %570, %625) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_RaggedGather_1_Assert_3_AssertGuard_false_23820, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_RaggedGather_1_Assert_3_AssertGuard_true_23810} : (tensor, tensor, tensor, tensor) -> tensor + %631 = "tf.Identity"(%79) {device = ""} : (tensor) -> tensor + %632 = "tf.Identity"(%630) {device = ""} : (tensor) -> tensor + %633 = "tf.Identity"(%307) {device = ""} : (tensor) -> tensor + %634 = "tf.Shape"(%36#2) {device = ""} : (tensor) -> tensor<1xi32> + %635 = "tf.StridedSlice"(%634, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %636 = "tf.Cast"(%635) {Truncate = false, device = ""} : (tensor<0xi32>) -> tensor<0xi64> + %637 = "tf.Identity"(%636) {device = ""} : (tensor<0xi64>) -> tensor<0xi64> + %638 = "tf.Shape"(%36#3) {device = ""} : (tensor) -> tensor<1xi32> + %639 = "tf.StridedSlice"(%638, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %640 = "tf.Cast"(%639) {Truncate = false, device = ""} : (tensor<0xi32>) -> tensor<0xi64> + %641 = "tf.Identity"(%640) {device = ""} : (tensor<0xi64>) -> tensor<0xi64> + %642 = "tf.GatherV2"(%36#3, %335, %14) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %643 = "tf.Tile"(%642, %339) {device = ""} : (tensor, tensor<1xi64>) -> tensor + %644 = "tf.Sub"(%643, %473) {device = ""} : (tensor, tensor) -> tensor + %645 = "tf.Shape"(%644) {device = ""} : (tensor) -> tensor<1xi32> + %646 = "tf.StridedSlice"(%645, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %647 = "tf.Cast"(%646) {Truncate = false, device = ""} : (tensor<0xi32>) -> tensor<0xi64> + %648 = "tf.Identity"(%647) {device = ""} : (tensor<0xi64>) -> tensor<0xi64> + %649 = "tf.UnicodeEncode"(%36#0, %57) {Tsplits = i64, device = "", errors = "replace", output_encoding = "UTF-8", replacement_char = 65533 : i64} : (tensor, tensor) -> tensor + %650 = "tf.Identity"(%649) {device = ""} : (tensor) -> tensor + func.return %650, %631 : tensor, tensor +} +func.func @WhitespaceTokenize_RaggedConcat_assert_equal_1_Assert_AssertGuard_false_3210(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Input tensors have incompatible shapes."> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/RaggedConcat/RaggedFromTensor/Const:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (WhitespaceTokenize/RaggedConcat/RaggedNRows/Const:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_RaggedConcat_assert_equal_1_Assert_AssertGuard_true_3200(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_false_3970(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to from_row_splits do not form a valid RaggedTensor:zero"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/RaggedFromNestedRowSplits/RaggedFromRowSplits/RowPartitionFromRowSplits/strided_slice:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (WhitespaceTokenize/RaggedFromNestedRowSplits/RaggedFromRowSplits/RowPartitionFromRowSplits/Const:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_true_3960(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_false_4330(%arg0: tensor, %arg1: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to from_row_splits do not form a valid RaggedTensor:monotonic"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x >= 0 did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/RaggedFromNestedRowSplits/RaggedFromRowSplits/RowPartitionFromRowSplits/sub:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor) -> () + %3 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %4 = "tf.Identity"(%3) {device = ""} : (tensor) -> tensor + func.return %4 : tensor +} +func.func @WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_true_4320(%arg0: tensor, %arg1: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_false_4670(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to _from_row_partition do not form a valid RaggedTensor"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/RaggedFromNestedRowSplits/RaggedFromRowSplits/strided_slice_1:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (WhitespaceTokenize/RaggedFromNestedRowSplits/RaggedFromRowSplits/strided_slice:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_true_4660(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_false_5040(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to from_row_splits do not form a valid RaggedTensor:zero"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/RaggedFromNestedRowSplits/RaggedFromRowSplits_1/RowPartitionFromRowSplits/strided_slice:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (WhitespaceTokenize/RaggedFromNestedRowSplits/RaggedFromRowSplits_1/RowPartitionFromRowSplits/Const:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_true_5030(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_false_5400(%arg0: tensor, %arg1: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to from_row_splits do not form a valid RaggedTensor:monotonic"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x >= 0 did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/RaggedFromNestedRowSplits/RaggedFromRowSplits_1/RowPartitionFromRowSplits/sub:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor) -> () + %3 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %4 = "tf.Identity"(%3) {device = ""} : (tensor) -> tensor + func.return %4 : tensor +} +func.func @WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_true_5390(%arg0: tensor, %arg1: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_assert_equal_1_Assert_AssertGuard_false_5760(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to _from_row_partition do not form a valid RaggedTensor"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/RaggedFromNestedRowSplits/RaggedFromRowSplits_1/strided_slice:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (WhitespaceTokenize/RaggedFromNestedRowSplits/RaggedFromRowSplits_1/RaggedNRows/sub:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_assert_equal_1_Assert_AssertGuard_true_5750(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_false_6110(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to from_row_splits do not form a valid RaggedTensor:zero"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/RaggedFromNestedRowSplits_1/RaggedFromRowSplits/RowPartitionFromRowSplits/strided_slice:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (WhitespaceTokenize/RaggedFromNestedRowSplits_1/RaggedFromRowSplits/RowPartitionFromRowSplits/Const:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_true_6100(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_false_6470(%arg0: tensor, %arg1: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to from_row_splits do not form a valid RaggedTensor:monotonic"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x >= 0 did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/RaggedFromNestedRowSplits_1/RaggedFromRowSplits/RowPartitionFromRowSplits/sub:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor) -> () + %3 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %4 = "tf.Identity"(%3) {device = ""} : (tensor) -> tensor + func.return %4 : tensor +} +func.func @WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_true_6460(%arg0: tensor, %arg1: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_false_6810(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to _from_row_partition do not form a valid RaggedTensor"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/RaggedFromNestedRowSplits_1/RaggedFromRowSplits/strided_slice_1:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (WhitespaceTokenize/RaggedFromNestedRowSplits_1/RaggedFromRowSplits/strided_slice:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_true_6800(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_false_7180(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to from_row_splits do not form a valid RaggedTensor:zero"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/RaggedFromNestedRowSplits_2/RaggedFromRowSplits/RowPartitionFromRowSplits/strided_slice:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (WhitespaceTokenize/RaggedFromNestedRowSplits_2/RaggedFromRowSplits/RowPartitionFromRowSplits/Const:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_true_7170(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_false_7540(%arg0: tensor, %arg1: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to from_row_splits do not form a valid RaggedTensor:monotonic"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x >= 0 did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/RaggedFromNestedRowSplits_2/RaggedFromRowSplits/RowPartitionFromRowSplits/sub:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor) -> () + %3 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %4 = "tf.Identity"(%3) {device = ""} : (tensor) -> tensor + func.return %4 : tensor +} +func.func @WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_true_7530(%arg0: tensor, %arg1: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_false_7880(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to _from_row_partition do not form a valid RaggedTensor"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/RaggedFromNestedRowSplits_2/RaggedFromRowSplits/strided_slice_1:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (WhitespaceTokenize/RaggedFromNestedRowSplits_2/RaggedFromRowSplits/strided_slice:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_true_7870(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_RaggedGather_Assert_AssertGuard_false_8680(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_RaggedGather_Assert_AssertGuard_true_8670(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_RaggedGather_Assert_1_AssertGuard_false_9750(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_RaggedGather_Assert_1_AssertGuard_true_9740(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_RaggedGather_Assert_2_AssertGuard_false_10240(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<1> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_RaggedGather_Assert_2_AssertGuard_true_10230(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_RaggedGather_Assert_3_AssertGuard_false_10600(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<1> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_RaggedGather_Assert_3_AssertGuard_true_10590(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_Assert_AssertGuard_false_15300(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_Assert_AssertGuard_true_15290(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_Assert_1_AssertGuard_false_16370(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_Assert_1_AssertGuard_true_16360(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_Assert_2_AssertGuard_false_16860(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<1> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_Assert_2_AssertGuard_true_16850(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_Assert_3_AssertGuard_false_17220(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<1> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_Assert_3_AssertGuard_true_17210(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_RaggedGather_1_Assert_AssertGuard_false_21900(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_RaggedGather_1_Assert_AssertGuard_true_21890(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_RaggedGather_1_Assert_1_AssertGuard_false_22970(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_RaggedGather_1_Assert_1_AssertGuard_true_22960(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_RaggedGather_1_Assert_2_AssertGuard_false_23460(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<1> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_RaggedGather_1_Assert_2_AssertGuard_true_23450(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_RaggedGather_1_Assert_3_AssertGuard_false_23820(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<1> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_RaggedGather_1_Assert_3_AssertGuard_true_23810(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} + +// CHECK: func private @whitespace_tokenizer_rank1(%arg0: tensor<1x!tf_type.string> {tf._user_specified_name = "input"}) -> (tensor, tensor) attributes {tf._implements = #tf_type.func<@"tftext:WhitespaceTokenizer", {}>, tf._input_shapes = [#tf_type.shape<1>], tf.signature.is_stateful} { +// CHECK: %0:2 = "tfl.custom"(%arg0) <{custom_code = "tftext:WhitespaceTokenizer", custom_option = #tfl}> : (tensor<1x!tf_type.string>) -> (tensor, tensor) +// CHECK: return %0#0, %0#1 : tensor, tensor + +func.func private @whitespace_tokenizer_rank2(%arg0: tensor {tf._user_specified_name = "input"}) -> (tensor, tensor, tensor) attributes {tf._input_shapes = [#tf_type.shape], tf._implements = #tf_type.func<@"tftext:WhitespaceTokenizer", {}>, tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<[]> : tensor<0xi64>} : () -> tensor<0xi64> + %1 = "tf.Const"() {value = dense : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<-1> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<[[0], [1]]> : tensor<2x1xi64>} : () -> tensor<2x1xi64> + %4 = "tf.Const"() {value = dense<[2, -1]> : tensor<2xi32>} : () -> tensor<2xi32> + %5 = "tf.Const"() {value = dense<2> : tensor} : () -> tensor + %6 = "tf.Const"() {value = dense<-1> : tensor<1xi32>} : () -> tensor<1xi32> + %7 = "tf.Const"() {value = dense<2> : tensor<1xi32>} : () -> tensor<1xi32> + %8 = "tf.Const"() {value = dense<[1, 0]> : tensor<2xi32>} : () -> tensor<2xi32> + %9 = "tf.Const"() {value = dense<1> : tensor} : () -> tensor + %10 = "tf.Const"() {value = dense<2> : tensor} : () -> tensor + %11 = "tf.Const"() {value = dense<[]> : tensor<0xi32>} : () -> tensor<0xi32> + %12 = "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %13 = "tf.Const"() {value = dense<0> : tensor<1xi64>} : () -> tensor<1xi64> + %14 = "tf.Const"() {value = dense<1> : tensor<1xi64>} : () -> tensor<1xi64> + %15 = "tf.Const"() {value = dense<1> : tensor} : () -> tensor + %16 = "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %17 = "tf.Const"() {value = dense<0> : tensor<1xi32>} : () -> tensor<1xi32> + %18 = "tf.Const"() {value = dense<1> : tensor<1xi32>} : () -> tensor<1xi32> + %19 = "tf.Shape"(%arg0) {device = ""} : (tensor) -> tensor<2xi64> + %20 = "tf.StridedSlice"(%19, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %21 = "tf.StridedSlice"(%19, %18, %7, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %22 = "tf.Mul"(%20, %21) {device = ""} : (tensor, tensor) -> tensor + %23 = "tf.Pack"(%22) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %24 = "tf.StridedSlice"(%19, %7, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi64> + %25 = "tf.ConcatV2"(%23, %24, %16) {device = ""} : (tensor<1xi64>, tensor<0xi64>, tensor) -> tensor<1xi64> + %26 = "tf.Reshape"(%arg0, %25) {device = ""} : (tensor, tensor<1xi64>) -> tensor + %27 = "tf.StringLength"(%26) {device = "", unit = "BYTE"} : (tensor) -> tensor + %28 = "tf.ExpandDims"(%27, %9) {device = ""} : (tensor, tensor) -> tensor + %29 = "tf.Cast"(%28) {Truncate = false, device = ""} : (tensor) -> tensor + %30 = "tf.Shape"(%29) {device = ""} : (tensor) -> tensor<2xi64> + %31 = "tf.StridedSlice"(%30, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %32 = "tf.StridedSlice"(%30, %18, %7, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %33 = "tf.Mul"(%31, %32) {device = ""} : (tensor, tensor) -> tensor + %34 = "tf.Pack"(%33) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %35 = "tf.StridedSlice"(%30, %7, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi64> + %36 = "tf.ConcatV2"(%34, %35, %16) {device = ""} : (tensor<1xi64>, tensor<0xi64>, tensor) -> tensor<1xi64> + %37 = "tf.Reshape"(%29, %36) {device = ""} : (tensor, tensor<1xi64>) -> tensor + %38 = "tf.StridedSlice"(%30, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %39 = "tf.AddV2"(%38, %15) {device = ""} : (tensor, tensor) -> tensor + %40 = "tf.Range"(%12, %39, %15) {device = ""} : (tensor, tensor, tensor) -> tensor + %41 = "tf.Mul"(%40, %15) {device = ""} : (tensor, tensor) -> tensor + %42 = "tf.Reshape"(%26, %6) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %43:3 = "tf.UnicodeDecodeWithOffsets"(%42) {Tsplits = i64, device = "", errors = "replace", input_encoding = "UTF-8", replace_control_characters = false, replacement_char = 65533 : i64} : (tensor) -> (tensor, tensor, tensor) + %44 = "tf.StridedSlice"(%43#0, %17, %6, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %45 = "tf.Shape"(%44) {device = ""} : (tensor) -> tensor<1xi32> + %46 = "tf.ConcatV2"(%45, %18, %16) {device = ""} : (tensor<1xi32>, tensor<1xi32>, tensor) -> tensor<2xi32> + %47 = "tf.Reshape"(%44, %46) {device = ""} : (tensor, tensor<2xi32>) -> tensor + %48 = "tf.Shape"(%47) {device = ""} : (tensor) -> tensor<2xi64> + %49 = "tf.StridedSlice"(%48, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %50 = "tf.AddV2"(%49, %15) {device = ""} : (tensor, tensor) -> tensor + %51 = "tf.Range"(%12, %50, %15) {device = ""} : (tensor, tensor, tensor) -> tensor + %52 = "tf.Mul"(%51, %15) {device = ""} : (tensor, tensor) -> tensor + %53 = "tf.ExpandDims"(%52, %9) {device = ""} : (tensor, tensor) -> tensor + %54 = "tf.Shape"(%52) {device = ""} : (tensor) -> tensor<1xi32> + %55 = "tf.StridedSlice"(%54, %17, %17, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %56 = "tf.StridedSlice"(%54, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %57 = "tf.StridedSlice"(%54, %18, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %58 = "tf.StridedSlice"(%52, %18, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %59 = "tf.StridedSlice"(%52, %17, %6, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %60 = "tf.Sub"(%58, %59) {device = ""} : (tensor, tensor) -> tensor + %61 = "tf.Shape"(%47) {device = ""} : (tensor) -> tensor<2xi32> + %62 = "tf.Cast"(%61) {Truncate = false, device = ""} : (tensor<2xi32>) -> tensor<2xi64> + %63 = "tf.StridedSlice"(%62, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %64 = "tf.Equal"(%63, %15) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %65 = "tf.StridedSlice"(%62, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %66 = "tf.Equal"(%65, %15) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %67 = "tf.StridedSlice"(%62, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %68 = "tf.Shape"(%47) {device = ""} : (tensor) -> tensor<2xi32> + %69 = "tf.Cast"(%68) {Truncate = false, device = ""} : (tensor<2xi32>) -> tensor<2xi64> + %70 = "tf.StridedSlice"(%69, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %71 = "tf.Equal"(%70, %15) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %72 = "tf.StridedSlice"(%43#0, %17, %6, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %73 = "tf.AddV2"(%72, %15) {device = ""} : (tensor, tensor) -> tensor + %74 = "tf.StridedSlice"(%43#0, %18, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %75 = "tf.Minimum"(%73, %74) {device = ""} : (tensor, tensor) -> tensor + %76:2 = "tf.RaggedRange"(%75, %74, %15) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %77 = "tf.Shape"(%76#0) {device = ""} : (tensor) -> tensor<1xi64> + %78 = "tf.StridedSlice"(%77, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %79 = "tf.Sub"(%78, %15) {device = ""} : (tensor, tensor) -> tensor + %80 = "tf.Equal"(%38, %79) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %81 = "tf.All"(%80, %11) {device = "", keep_dims = false} : (tensor, tensor<0xi32>) -> tensor + %82 = "tf.If"(%81, %81, %38, %79) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedConcat_assert_equal_1_Assert_AssertGuard_false_99640, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedConcat_assert_equal_1_Assert_AssertGuard_true_99630} : (tensor, tensor, tensor, tensor) -> tensor + %83 = "tf.Identity"(%82) {device = ""} : (tensor) -> tensor + %84 = "tf.StridedSlice"(%41, %18, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %85 = "tf.Mul"(%79, %5) {device = ""} : (tensor, tensor) -> tensor + %86 = "tf.Range"(%12, %85, %15) {device = ""} : (tensor, tensor, tensor) -> tensor + %87 = "tf.Reshape"(%86, %4) {device = ""} : (tensor, tensor<2xi32>) -> tensor<2x?xi64> + %88 = "tf.Transpose"(%87, %8) {device = ""} : (tensor<2x?xi64>, tensor<2xi32>) -> tensor + %89 = "tf.Reshape"(%88, %6) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %90 = "tf.StridedSlice"(%76#0, %6, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %91 = "tf.AddV2"(%84, %90) {device = ""} : (tensor, tensor) -> tensor + %92 = "tf.ConcatV2"(%76#0, %91, %16) {device = ""} : (tensor, tensor, tensor) -> tensor + %93 = "tf.GatherV2"(%43#2, %76#1, %16) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %94 = "tf.ConcatV2"(%93, %37, %16) {device = ""} : (tensor, tensor, tensor) -> tensor + %95:2 = "tf.RaggedGather"(%92, %94, %89) {OUTPUT_RAGGED_RANK = 1 : i64, PARAMS_RAGGED_RANK = 1 : i64, Tindices = i64, Tsplits = i64, Tvalues = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %96 = "tf.StridedSlice"(%95#0, %17, %17, %7) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %97 = "tf.StridedSlice"(%96, %17, %6, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %98 = "tf.Shape"(%97) {device = ""} : (tensor) -> tensor<1xi32> + %99 = "tf.ConcatV2"(%98, %18, %16) {device = ""} : (tensor<1xi32>, tensor<1xi32>, tensor) -> tensor<2xi32> + %100 = "tf.Reshape"(%97, %99) {device = ""} : (tensor, tensor<2xi32>) -> tensor + %101 = "tf.Shape"(%100) {device = ""} : (tensor) -> tensor<2xi64> + %102 = "tf.StridedSlice"(%101, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %103 = "tf.AddV2"(%102, %15) {device = ""} : (tensor, tensor) -> tensor + %104 = "tf.Range"(%12, %103, %15) {device = ""} : (tensor, tensor, tensor) -> tensor + %105 = "tf.Mul"(%104, %15) {device = ""} : (tensor, tensor) -> tensor + %106 = "tf.ExpandDims"(%105, %9) {device = ""} : (tensor, tensor) -> tensor + %107 = "tf.Shape"(%105) {device = ""} : (tensor) -> tensor<1xi32> + %108 = "tf.StridedSlice"(%107, %17, %17, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %109 = "tf.StridedSlice"(%107, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %110 = "tf.StridedSlice"(%107, %18, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %111 = "tf.StridedSlice"(%105, %18, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %112 = "tf.StridedSlice"(%105, %17, %6, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %113 = "tf.Sub"(%111, %112) {device = ""} : (tensor, tensor) -> tensor + %114 = "tf.Shape"(%100) {device = ""} : (tensor) -> tensor<2xi32> + %115 = "tf.Cast"(%114) {Truncate = false, device = ""} : (tensor<2xi32>) -> tensor<2xi64> + %116 = "tf.StridedSlice"(%115, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %117 = "tf.Equal"(%116, %15) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %118 = "tf.StridedSlice"(%115, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %119 = "tf.Equal"(%118, %15) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %120 = "tf.StridedSlice"(%115, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %121 = "tf.Shape"(%100) {device = ""} : (tensor) -> tensor<2xi32> + %122 = "tf.Cast"(%121) {Truncate = false, device = ""} : (tensor<2xi32>) -> tensor<2xi64> + %123 = "tf.StridedSlice"(%122, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %124 = "tf.Equal"(%123, %15) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %125:5 = "tf.WhitespaceTokenizeWithOffsets"(%43#1, %43#0) {Tsplits = i64, device = ""} : (tensor, tensor) -> (tensor, tensor, tensor, tensor, tensor) + %126 = "tf.StridedSlice"(%125#1, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %127 = "tf.Equal"(%126, %12) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %128 = "tf.All"(%127, %11) {device = "", keep_dims = false} : (tensor, tensor<0xi32>) -> tensor + %129 = "tf.If"(%128, %128, %126, %12) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_false_100400, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_true_100390} : (tensor, tensor, tensor, tensor) -> tensor + %130 = "tf.Identity"(%129) {device = ""} : (tensor) -> tensor + %131 = "tf.StridedSlice"(%125#1, %18, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %132 = "tf.StridedSlice"(%125#1, %17, %6, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %133 = "tf.Sub"(%131, %132) {device = ""} : (tensor, tensor) -> tensor + %134 = "tf.LessEqual"(%12, %133) {device = ""} : (tensor, tensor) -> tensor + %135 = "tf.All"(%134, %17) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %136 = "tf.If"(%135, %135, %133) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_false_100760, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_true_100750} : (tensor, tensor, tensor) -> tensor + %137 = "tf.Identity"(%136) {device = ""} : (tensor) -> tensor + %138 = "tf.Identity"(%125#1) {_class = ["loc:@WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenizeWithOffsets"], device = ""} : (tensor) -> tensor + %139 = "tf.StridedSlice"(%138, %6, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %140 = "tf.Shape"(%125#0) {device = ""} : (tensor) -> tensor<1xi64> + %141 = "tf.StridedSlice"(%140, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %142 = "tf.Equal"(%139, %141) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %143 = "tf.All"(%142, %11) {device = "", keep_dims = false} : (tensor, tensor<0xi32>) -> tensor + %144 = "tf.If"(%143, %143, %139, %141) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_false_101100, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_true_101090} : (tensor, tensor, tensor, tensor) -> tensor + %145 = "tf.Identity"(%144) {device = ""} : (tensor) -> tensor + %146 = "tf.Identity"(%138) {_class = ["loc:@WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenizeWithOffsets"], device = ""} : (tensor) -> tensor + %147 = "tf.Shape"(%146) {device = ""} : (tensor) -> tensor<1xi64> + %148 = "tf.StridedSlice"(%147, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %149 = "tf.Sub"(%148, %15) {device = ""} : (tensor, tensor) -> tensor + %150 = "tf.StridedSlice"(%125#4, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %151 = "tf.Equal"(%150, %12) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %152 = "tf.All"(%151, %11) {device = "", keep_dims = false} : (tensor, tensor<0xi32>) -> tensor + %153 = "tf.If"(%152, %152, %150, %12) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_false_101470, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_true_101460} : (tensor, tensor, tensor, tensor) -> tensor + %154 = "tf.Identity"(%153) {device = ""} : (tensor) -> tensor + %155 = "tf.StridedSlice"(%125#4, %18, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %156 = "tf.StridedSlice"(%125#4, %17, %6, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %157 = "tf.Sub"(%155, %156) {device = ""} : (tensor, tensor) -> tensor + %158 = "tf.LessEqual"(%12, %157) {device = ""} : (tensor, tensor) -> tensor + %159 = "tf.All"(%158, %17) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %160 = "tf.If"(%159, %159, %157) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_false_101830, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_true_101820} : (tensor, tensor, tensor) -> tensor + %161 = "tf.Identity"(%160) {device = ""} : (tensor) -> tensor + %162 = "tf.Identity"(%125#4) {_class = ["loc:@WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenizeWithOffsets"], device = ""} : (tensor) -> tensor + %163 = "tf.StridedSlice"(%162, %6, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %164 = "tf.Equal"(%163, %149) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %165 = "tf.All"(%164, %11) {device = "", keep_dims = false} : (tensor, tensor<0xi32>) -> tensor + %166 = "tf.If"(%165, %165, %163, %149) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_assert_equal_1_Assert_AssertGuard_false_102190, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_assert_equal_1_Assert_AssertGuard_true_102180} : (tensor, tensor, tensor, tensor) -> tensor + %167 = "tf.Identity"(%166) {device = ""} : (tensor) -> tensor + %168 = "tf.Identity"(%162) {_class = ["loc:@WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenizeWithOffsets"], device = ""} : (tensor) -> tensor + %169 = "tf.StridedSlice"(%125#4, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %170 = "tf.Equal"(%169, %12) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %171 = "tf.All"(%170, %11) {device = "", keep_dims = false} : (tensor, tensor<0xi32>) -> tensor + %172 = "tf.If"(%171, %171, %169, %12) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_false_102540, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_true_102530} : (tensor, tensor, tensor, tensor) -> tensor + %173 = "tf.Identity"(%172) {device = ""} : (tensor) -> tensor + %174 = "tf.StridedSlice"(%125#4, %18, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %175 = "tf.StridedSlice"(%125#4, %17, %6, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %176 = "tf.Sub"(%174, %175) {device = ""} : (tensor, tensor) -> tensor + %177 = "tf.LessEqual"(%12, %176) {device = ""} : (tensor, tensor) -> tensor + %178 = "tf.All"(%177, %17) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %179 = "tf.If"(%178, %178, %176) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_false_102900, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_true_102890} : (tensor, tensor, tensor) -> tensor + %180 = "tf.Identity"(%179) {device = ""} : (tensor) -> tensor + %181 = "tf.Identity"(%125#4) {_class = ["loc:@WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenizeWithOffsets"], device = ""} : (tensor) -> tensor + %182 = "tf.StridedSlice"(%181, %6, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %183 = "tf.Shape"(%125#2) {device = ""} : (tensor) -> tensor<1xi64> + %184 = "tf.StridedSlice"(%183, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %185 = "tf.Equal"(%182, %184) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %186 = "tf.All"(%185, %11) {device = "", keep_dims = false} : (tensor, tensor<0xi32>) -> tensor + %187 = "tf.If"(%186, %186, %182, %184) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_false_103240, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_true_103230} : (tensor, tensor, tensor, tensor) -> tensor + %188 = "tf.Identity"(%187) {device = ""} : (tensor) -> tensor + %189 = "tf.Identity"(%181) {_class = ["loc:@WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenizeWithOffsets"], device = ""} : (tensor) -> tensor + %190 = "tf.Shape"(%189) {device = ""} : (tensor) -> tensor<1xi64> + %191 = "tf.StridedSlice"(%190, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %192 = "tf.Sub"(%191, %15) {device = ""} : (tensor, tensor) -> tensor + %193 = "tf.Equal"(%192, %15) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %194 = "tf.LogicalOr"(%64, %193) {device = ""} : (tensor, tensor) -> tensor + %195 = "tf.Equal"(%192, %63) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %196 = "tf.LogicalOr"(%194, %195) {device = ""} : (tensor, tensor) -> tensor + %197 = "tf.StridedSlice"(%189, %18, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %198 = "tf.StridedSlice"(%189, %17, %6, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %199 = "tf.Sub"(%197, %198) {device = ""} : (tensor, tensor) -> tensor + %200 = "tf.Shape"(%189) {device = ""} : (tensor) -> tensor<1xi64> + %201 = "tf.StridedSlice"(%200, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %202 = "tf.Sub"(%201, %15) {device = ""} : (tensor, tensor) -> tensor + %203 = "tf.Equal"(%202, %15) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %204 = "tf.ExpandDims"(%189, %9) {device = ""} : (tensor, tensor) -> tensor + %205 = "tf.Shape"(%189) {device = ""} : (tensor) -> tensor<1xi32> + %206 = "tf.StridedSlice"(%205, %17, %17, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %207 = "tf.StridedSlice"(%205, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %208 = "tf.StridedSlice"(%205, %18, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %209 = "tf.StridedSlice"(%125#4, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %210 = "tf.Equal"(%209, %12) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %211 = "tf.All"(%210, %11) {device = "", keep_dims = false} : (tensor, tensor<0xi32>) -> tensor + %212 = "tf.If"(%211, %211, %209, %12) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_false_103610, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_true_103600} : (tensor, tensor, tensor, tensor) -> tensor + %213 = "tf.Identity"(%212) {device = ""} : (tensor) -> tensor + %214 = "tf.StridedSlice"(%125#4, %18, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %215 = "tf.StridedSlice"(%125#4, %17, %6, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %216 = "tf.Sub"(%214, %215) {device = ""} : (tensor, tensor) -> tensor + %217 = "tf.LessEqual"(%12, %216) {device = ""} : (tensor, tensor) -> tensor + %218 = "tf.All"(%217, %17) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %219 = "tf.If"(%218, %218, %216) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_false_103970, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_true_103960} : (tensor, tensor, tensor) -> tensor + %220 = "tf.Identity"(%219) {device = ""} : (tensor) -> tensor + %221 = "tf.Identity"(%125#4) {_class = ["loc:@WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenizeWithOffsets"], device = ""} : (tensor) -> tensor + %222 = "tf.StridedSlice"(%221, %6, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %223 = "tf.Shape"(%125#3) {device = ""} : (tensor) -> tensor<1xi64> + %224 = "tf.StridedSlice"(%223, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %225 = "tf.Equal"(%222, %224) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %226 = "tf.All"(%225, %11) {device = "", keep_dims = false} : (tensor, tensor<0xi32>) -> tensor + %227 = "tf.If"(%226, %226, %222, %224) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_false_104310, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_true_104300} : (tensor, tensor, tensor, tensor) -> tensor + %228 = "tf.Identity"(%227) {device = ""} : (tensor) -> tensor + %229 = "tf.Identity"(%221) {_class = ["loc:@WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenizeWithOffsets"], device = ""} : (tensor) -> tensor + %230 = "tf.Shape"(%229) {device = ""} : (tensor) -> tensor<1xi64> + %231 = "tf.StridedSlice"(%230, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %232 = "tf.Sub"(%231, %15) {device = ""} : (tensor, tensor) -> tensor + %233 = "tf.Equal"(%232, %15) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %234 = "tf.LogicalOr"(%233, %1) {device = ""} : (tensor, tensor) -> tensor + %235 = "tf.Equal"(%232, %15) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %236 = "tf.LogicalOr"(%234, %235) {device = ""} : (tensor, tensor) -> tensor + %237 = "tf.StridedSlice"(%229, %18, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %238 = "tf.StridedSlice"(%229, %17, %6, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %239 = "tf.Sub"(%237, %238) {device = ""} : (tensor, tensor) -> tensor + %240 = "tf.Shape"(%229) {device = ""} : (tensor) -> tensor<1xi64> + %241 = "tf.StridedSlice"(%240, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %242 = "tf.Sub"(%241, %15) {device = ""} : (tensor, tensor) -> tensor + %243 = "tf.Equal"(%242, %15) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %244 = "tf.ExpandDims"(%229, %9) {device = ""} : (tensor, tensor) -> tensor + %245 = "tf.Shape"(%229) {device = ""} : (tensor) -> tensor<1xi32> + %246 = "tf.StridedSlice"(%245, %17, %17, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %247 = "tf.StridedSlice"(%245, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %248 = "tf.StridedSlice"(%245, %18, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %249 = "tf.StridedSlice"(%229, %6, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %250 = "tf.Range"(%12, %249, %15) {device = ""} : (tensor, tensor, tensor) -> tensor + %251 = "tf.StridedSlice"(%229, %18, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %252 = "tf.StridedSlice"(%229, %17, %6, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %253 = "tf.Sub"(%251, %252) {device = ""} : (tensor, tensor) -> tensor + %254 = "tf.If"(%196, %196, %63, %192) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_AssertGuard_false_105110, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_AssertGuard_true_105100} : (tensor, tensor, tensor, tensor) -> tensor + %255 = "tf.Identity"(%254) {device = ""} : (tensor) -> tensor + %256 = "tf.Equal"(%192, %15) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %257 = "tf.Select"(%256, %63, %192) {device = ""} : (tensor, tensor, tensor) -> tensor + %258 = "tf.Equal"(%257, %15) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %259 = "tf.LogicalOr"(%258, %66) {device = ""} : (tensor, tensor) -> tensor + %260 = "tf.Equal"(%65, %257) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %261 = "tf.LogicalOr"(%259, %260) {device = ""} : (tensor, tensor) -> tensor + %262 = "tf.Select"(%203, %257, %15) {device = ""} : (tensor, tensor, tensor) -> tensor + %263 = "tf.Pack"(%262, %15) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi64> + %264 = "tf.StridedSlice"(%263, %18, %7, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %265 = "tf.Cast"(%264) {Truncate = false, device = ""} : (tensor) -> tensor + %266 = "tf.Reshape"(%265, %11) {device = ""} : (tensor, tensor<0xi32>) -> tensor + %267 = "tf.Pack"(%9, %266) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %268 = "tf.Tile"(%204, %267) {device = ""} : (tensor, tensor<2xi32>) -> tensor + %269 = "tf.Mul"(%266, %207) {device = ""} : (tensor, tensor) -> tensor + %270 = "tf.Pack"(%269) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %271 = "tf.ConcatV2"(%206, %270, %208, %16) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %272 = "tf.Reshape"(%268, %271) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %273 = "tf.Shape"(%272) {device = ""} : (tensor) -> tensor<1xi64> + %274 = "tf.StridedSlice"(%273, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %275 = "tf.Pack"(%264) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %276 = "tf.StridedSlice"(%272, %275, %13, %14) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %277 = "tf.Sub"(%274, %264) {device = ""} : (tensor, tensor) -> tensor + %278 = "tf.Pack"(%277) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %279 = "tf.StridedSlice"(%272, %13, %278, %14) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %280:2 = "tf.RaggedRange"(%279, %276, %15) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %281 = "tf.Select"(%71, %257, %15) {device = ""} : (tensor, tensor, tensor) -> tensor + %282 = "tf.Pack"(%281, %15) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi64> + %283 = "tf.StridedSlice"(%282, %18, %7, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %284 = "tf.Cast"(%283) {Truncate = false, device = ""} : (tensor) -> tensor + %285 = "tf.Reshape"(%284, %11) {device = ""} : (tensor, tensor<0xi32>) -> tensor + %286 = "tf.Pack"(%9, %285) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %287 = "tf.Tile"(%53, %286) {device = ""} : (tensor, tensor<2xi32>) -> tensor + %288 = "tf.Mul"(%285, %56) {device = ""} : (tensor, tensor) -> tensor + %289 = "tf.Pack"(%288) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %290 = "tf.ConcatV2"(%55, %289, %57, %16) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %291 = "tf.Reshape"(%287, %290) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %292 = "tf.Shape"(%291) {device = ""} : (tensor) -> tensor<1xi64> + %293 = "tf.StridedSlice"(%292, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %294 = "tf.Pack"(%283) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %295 = "tf.StridedSlice"(%291, %294, %13, %14) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %296 = "tf.Sub"(%293, %283) {device = ""} : (tensor, tensor) -> tensor + %297 = "tf.Pack"(%296) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %298 = "tf.StridedSlice"(%291, %13, %297, %14) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %299:2 = "tf.RaggedRange"(%298, %295, %15) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %300 = "tf.StridedSlice"(%282, %17, %18, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1xi64> + %301 = "tf.StridedSlice"(%282, %18, %7, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %302 = "tf.Mul"(%60, %301) {device = ""} : (tensor, tensor) -> tensor + %303 = "tf.Tile"(%302, %300) {device = ""} : (tensor, tensor<1xi64>) -> tensor + %304 = "tf.Cumsum"(%303, %16) {device = "", exclusive = false, reverse = false} : (tensor, tensor) -> tensor + %305 = "tf.ConcatV2"(%13, %304, %2) {device = ""} : (tensor<1xi64>, tensor, tensor) -> tensor + %306 = "tf.StridedSlice"(%305, %17, %6, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %307 = "tf.ExpandDims"(%306, %9) {device = ""} : (tensor, tensor) -> tensor + %308 = "tf.Shape"(%306) {device = ""} : (tensor) -> tensor<1xi32> + %309 = "tf.StridedSlice"(%308, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %310 = "tf.Pack"(%309) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %311 = "tf.StridedSlice"(%305, %18, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %312 = "tf.ExpandDims"(%311, %9) {device = ""} : (tensor, tensor) -> tensor + %313 = "tf.Shape"(%311) {device = ""} : (tensor) -> tensor<1xi32> + %314 = "tf.StridedSlice"(%313, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %315 = "tf.Pack"(%314) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %316 = "tf.Equal"(%192, %15) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %317 = "tf.Select"(%316, %257, %15) {device = ""} : (tensor, tensor, tensor) -> tensor + %318 = "tf.Cast"(%317) {Truncate = false, device = ""} : (tensor) -> tensor + %319 = "tf.Reshape"(%318, %11) {device = ""} : (tensor, tensor<0xi32>) -> tensor + %320 = "tf.Pack"(%9, %319) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %321 = "tf.Mul"(%319, %10) {device = ""} : (tensor, tensor) -> tensor + %322 = "tf.Pack"(%321) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %323 = "tf.ConcatV2"(%11, %322, %11, %16) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %324 = "tf.Pack"(%317) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %325 = "tf.Pack"(%12, %192) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi64> + %326 = "tf.ExpandDims"(%325, %9) {device = ""} : (tensor<2xi64>, tensor) -> tensor<2x1xi64> + %327 = "tf.Tile"(%326, %320) {device = ""} : (tensor<2x1xi64>, tensor<2xi32>) -> tensor<2x?xi64> + %328 = "tf.Reshape"(%327, %323) {device = ""} : (tensor<2x?xi64>, tensor<1xi32>) -> tensor + %329 = "tf.Shape"(%328) {device = ""} : (tensor) -> tensor<1xi64> + %330 = "tf.StridedSlice"(%329, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %331 = "tf.Sub"(%330, %317) {device = ""} : (tensor, tensor) -> tensor + %332 = "tf.Pack"(%331) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %333 = "tf.StridedSlice"(%328, %13, %332, %14) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %334 = "tf.StridedSlice"(%328, %324, %13, %14) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %335:2 = "tf.RaggedRange"(%333, %334, %15) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %336 = "tf.GatherV2"(%199, %335#1, %16) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %337 = "tf.Cast"(%336) {Truncate = false, device = ""} : (tensor) -> tensor + %338 = "tf.BroadcastTo"(%337, %310) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %339 = "tf.Max"(%338, %17) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %340 = "tf.Maximum"(%16, %339) {device = ""} : (tensor, tensor) -> tensor + %341 = "tf.Range"(%16, %340, %9) {device = ""} : (tensor, tensor, tensor) -> tensor + %342 = "tf.Pack"(%9, %340) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %343 = "tf.Tile"(%307, %342) {device = ""} : (tensor, tensor<2xi32>) -> tensor + %344 = "tf.Shape"(%343) {device = ""} : (tensor) -> tensor<2xi32> + %345 = "tf.StridedSlice"(%344, %17, %7, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<2xi32> + %346 = "tf.Prod"(%345, %17) {device = "", keep_dims = false} : (tensor<2xi32>, tensor<1xi32>) -> tensor + %347 = "tf.Pack"(%346) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %348 = "tf.Shape"(%343) {device = ""} : (tensor) -> tensor<2xi32> + %349 = "tf.StridedSlice"(%348, %17, %17, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %350 = "tf.Shape"(%343) {device = ""} : (tensor) -> tensor<2xi32> + %351 = "tf.StridedSlice"(%350, %7, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %352 = "tf.ConcatV2"(%349, %347, %351, %16) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %353 = "tf.Reshape"(%343, %352) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %354 = "tf.ExpandDims"(%338, %2) {device = ""} : (tensor, tensor) -> tensor + %355 = "tf.Less"(%341, %354) {device = ""} : (tensor, tensor) -> tensor + %356 = "tf.Reshape"(%355, %6) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %357 = "tf.Where"(%356) {device = ""} : (tensor) -> tensor + %358 = "tf.Squeeze"(%357) {device = "", squeeze_dims = [1]} : (tensor) -> tensor + %359 = "tf.GatherV2"(%353, %358, %16) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %360 = "tf.Cast"(%336) {Truncate = false, device = ""} : (tensor) -> tensor + %361 = "tf.BroadcastTo"(%360, %315) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %362 = "tf.Max"(%361, %17) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %363 = "tf.Maximum"(%16, %362) {device = ""} : (tensor, tensor) -> tensor + %364 = "tf.Range"(%16, %363, %9) {device = ""} : (tensor, tensor, tensor) -> tensor + %365 = "tf.Pack"(%9, %363) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %366 = "tf.Tile"(%312, %365) {device = ""} : (tensor, tensor<2xi32>) -> tensor + %367 = "tf.Shape"(%366) {device = ""} : (tensor) -> tensor<2xi32> + %368 = "tf.StridedSlice"(%367, %17, %7, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<2xi32> + %369 = "tf.Prod"(%368, %17) {device = "", keep_dims = false} : (tensor<2xi32>, tensor<1xi32>) -> tensor + %370 = "tf.Pack"(%369) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %371 = "tf.Shape"(%366) {device = ""} : (tensor) -> tensor<2xi32> + %372 = "tf.StridedSlice"(%371, %17, %17, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %373 = "tf.Shape"(%366) {device = ""} : (tensor) -> tensor<2xi32> + %374 = "tf.StridedSlice"(%373, %7, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %375 = "tf.ConcatV2"(%372, %370, %374, %16) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %376 = "tf.Reshape"(%366, %375) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %377 = "tf.ExpandDims"(%361, %2) {device = ""} : (tensor, tensor) -> tensor + %378 = "tf.Less"(%364, %377) {device = ""} : (tensor, tensor) -> tensor + %379 = "tf.Reshape"(%378, %6) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %380 = "tf.Where"(%379) {device = ""} : (tensor) -> tensor + %381 = "tf.Squeeze"(%380) {device = "", squeeze_dims = [1]} : (tensor) -> tensor + %382 = "tf.GatherV2"(%376, %381, %16) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %383:2 = "tf.RaggedRange"(%359, %382, %15) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %384 = "tf.If"(%261, %261, %257, %67) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_1_AssertGuard_false_106180, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_1_AssertGuard_true_106170} : (tensor, tensor, tensor, tensor) -> tensor + %385 = "tf.Identity"(%384) {device = ""} : (tensor) -> tensor + %386 = "tf.StridedSlice"(%62, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %387 = "tf.Equal"(%386, %15) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %388 = "tf.Select"(%387, %257, %386) {device = ""} : (tensor, tensor, tensor) -> tensor + %389 = "tf.Pack"(%388) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %390 = "tf.StridedSlice"(%62, %17, %17, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi64> + %391 = "tf.StridedSlice"(%62, %18, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1xi64> + %392 = "tf.ConcatV2"(%390, %389, %391, %16) {device = ""} : (tensor<0xi64>, tensor<1xi64>, tensor<1xi64>, tensor) -> tensor<2xi64> + %393 = "tf.StridedSlice"(%392, %18, %7, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %394 = "tf.Equal"(%393, %15) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %395 = "tf.StridedSlice"(%392, %18, %7, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %396 = "tf.StridedSlice"(%392, %18, %7, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %397 = "tf.Equal"(%396, %15) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %398 = "tf.If"(%397, %397, %396, %336) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_2_AssertGuard_false_106670, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_2_AssertGuard_true_106660} : (tensor, tensor, tensor, tensor) -> tensor + %399 = "tf.Identity"(%398) {device = ""} : (tensor) -> tensor + %400 = "tf.If"(%394, %394, %336, %395) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_3_AssertGuard_false_107030, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_3_AssertGuard_true_107020} : (tensor, tensor, tensor, tensor) -> tensor + %401 = "tf.If"(%236, %236, %15, %232) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_Assert_AssertGuard_false_111870, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_Assert_AssertGuard_true_111860} : (tensor, tensor, tensor, tensor) -> tensor + %402 = "tf.Identity"(%401) {device = ""} : (tensor) -> tensor + %403 = "tf.Equal"(%232, %15) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %404 = "tf.Select"(%403, %15, %232) {device = ""} : (tensor, tensor, tensor) -> tensor + %405 = "tf.Equal"(%404, %15) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %406 = "tf.LogicalOr"(%405, %1) {device = ""} : (tensor, tensor) -> tensor + %407 = "tf.Equal"(%404, %15) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %408 = "tf.LogicalOr"(%406, %407) {device = ""} : (tensor, tensor) -> tensor + %409 = "tf.Select"(%243, %404, %15) {device = ""} : (tensor, tensor, tensor) -> tensor + %410 = "tf.Pack"(%409, %15) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi64> + %411 = "tf.StridedSlice"(%410, %18, %7, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %412 = "tf.Cast"(%411) {Truncate = false, device = ""} : (tensor) -> tensor + %413 = "tf.Reshape"(%412, %11) {device = ""} : (tensor, tensor<0xi32>) -> tensor + %414 = "tf.Pack"(%9, %413) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %415 = "tf.Tile"(%244, %414) {device = ""} : (tensor, tensor<2xi32>) -> tensor + %416 = "tf.Mul"(%413, %247) {device = ""} : (tensor, tensor) -> tensor + %417 = "tf.Pack"(%416) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %418 = "tf.ConcatV2"(%246, %417, %248, %16) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %419 = "tf.Reshape"(%415, %418) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %420 = "tf.Shape"(%419) {device = ""} : (tensor) -> tensor<1xi64> + %421 = "tf.StridedSlice"(%420, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %422 = "tf.Pack"(%411) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %423 = "tf.StridedSlice"(%419, %422, %13, %14) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %424 = "tf.Sub"(%421, %411) {device = ""} : (tensor, tensor) -> tensor + %425 = "tf.Pack"(%424) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %426 = "tf.StridedSlice"(%419, %13, %425, %14) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %427:2 = "tf.RaggedRange"(%426, %423, %15) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %428 = "tf.GatherV2"(%250, %427#1, %16) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %429 = "tf.StridedSlice"(%410, %17, %18, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1xi64> + %430 = "tf.StridedSlice"(%410, %17, %18, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1xi64> + %431 = "tf.StridedSlice"(%410, %7, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi64> + %432 = "tf.ConcatV2"(%430, %431, %16) {device = ""} : (tensor<1xi64>, tensor<0xi64>, tensor) -> tensor<1xi64> + %433 = "tf.StridedSlice"(%410, %18, %7, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %434 = "tf.Mul"(%253, %433) {device = ""} : (tensor, tensor) -> tensor + %435 = "tf.Tile"(%434, %429) {device = ""} : (tensor, tensor<1xi64>) -> tensor + %436 = "tf.Cumsum"(%435, %16) {device = "", exclusive = false, reverse = false} : (tensor, tensor) -> tensor + %437 = "tf.ConcatV2"(%13, %436, %2) {device = ""} : (tensor<1xi64>, tensor, tensor) -> tensor + %438 = "tf.Shape"(%437) {device = ""} : (tensor) -> tensor<1xi64> + %439 = "tf.StridedSlice"(%438, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %440 = "tf.Sub"(%439, %15) {device = ""} : (tensor, tensor) -> tensor + %441 = "tf.Equal"(%440, %15) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %442 = "tf.LogicalOr"(%117, %441) {device = ""} : (tensor, tensor) -> tensor + %443 = "tf.Equal"(%440, %116) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %444 = "tf.LogicalOr"(%442, %443) {device = ""} : (tensor, tensor) -> tensor + %445 = "tf.StridedSlice"(%437, %18, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %446 = "tf.StridedSlice"(%437, %17, %6, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %447 = "tf.Sub"(%445, %446) {device = ""} : (tensor, tensor) -> tensor + %448 = "tf.Shape"(%437) {device = ""} : (tensor) -> tensor<1xi64> + %449 = "tf.StridedSlice"(%448, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %450 = "tf.Sub"(%449, %15) {device = ""} : (tensor, tensor) -> tensor + %451 = "tf.Equal"(%450, %15) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %452 = "tf.ExpandDims"(%437, %9) {device = ""} : (tensor, tensor) -> tensor + %453 = "tf.Shape"(%437) {device = ""} : (tensor) -> tensor<1xi32> + %454 = "tf.StridedSlice"(%453, %17, %17, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %455 = "tf.StridedSlice"(%453, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %456 = "tf.StridedSlice"(%453, %18, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %457 = "tf.Select"(%1, %404, %15) {device = ""} : (tensor, tensor, tensor) -> tensor + %458 = "tf.Pack"(%457, %15) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi64> + %459 = "tf.StridedSlice"(%458, %18, %7, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %460 = "tf.Cast"(%459) {Truncate = false, device = ""} : (tensor) -> tensor + %461 = "tf.Reshape"(%460, %11) {device = ""} : (tensor, tensor<0xi32>) -> tensor + %462 = "tf.Pack"(%9, %461) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %463 = "tf.Tile"(%3, %462) {device = ""} : (tensor<2x1xi64>, tensor<2xi32>) -> tensor<2x?xi64> + %464 = "tf.Mul"(%461, %10) {device = ""} : (tensor, tensor) -> tensor + %465 = "tf.Pack"(%464) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %466 = "tf.ConcatV2"(%11, %465, %11, %16) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %467 = "tf.Reshape"(%463, %466) {device = ""} : (tensor<2x?xi64>, tensor<1xi32>) -> tensor + %468 = "tf.Shape"(%467) {device = ""} : (tensor) -> tensor<1xi64> + %469 = "tf.StridedSlice"(%468, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %470 = "tf.Pack"(%459) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %471 = "tf.StridedSlice"(%467, %470, %13, %14) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %472 = "tf.Sub"(%469, %459) {device = ""} : (tensor, tensor) -> tensor + %473 = "tf.Pack"(%472) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %474 = "tf.StridedSlice"(%467, %13, %473, %14) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %475:2 = "tf.RaggedRange"(%474, %471, %15) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %476 = "tf.GatherV2"(%13, %475#1, %16) {batch_dims = 0 : i64, device = ""} : (tensor<1xi64>, tensor, tensor) -> tensor + %477 = "tf.GatherV2"(%14, %476, %16) {batch_dims = 0 : i64, device = ""} : (tensor<1xi64>, tensor, tensor) -> tensor + %478 = "tf.StridedSlice"(%458, %17, %18, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1xi64> + %479 = "tf.StridedSlice"(%458, %17, %18, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1xi64> + %480 = "tf.StridedSlice"(%458, %7, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi64> + %481 = "tf.ConcatV2"(%479, %480, %16) {device = ""} : (tensor<1xi64>, tensor<0xi64>, tensor) -> tensor<1xi64> + %482 = "tf.Tile"(%477, %481) {device = ""} : (tensor, tensor<1xi64>) -> tensor + %483 = "tf.StridedSlice"(%458, %18, %7, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %484 = "tf.Mul"(%483, %14) {device = ""} : (tensor, tensor<1xi64>) -> tensor<1xi64> + %485 = "tf.Tile"(%484, %478) {device = ""} : (tensor<1xi64>, tensor<1xi64>) -> tensor + %486 = "tf.Cumsum"(%485, %16) {device = "", exclusive = false, reverse = false} : (tensor, tensor) -> tensor + %487 = "tf.ConcatV2"(%13, %486, %2) {device = ""} : (tensor<1xi64>, tensor, tensor) -> tensor + %488 = "tf.StridedSlice"(%487, %17, %6, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %489 = "tf.ExpandDims"(%488, %9) {device = ""} : (tensor, tensor) -> tensor + %490 = "tf.Shape"(%488) {device = ""} : (tensor) -> tensor<1xi32> + %491 = "tf.StridedSlice"(%490, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %492 = "tf.Pack"(%491) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %493 = "tf.StridedSlice"(%487, %18, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %494 = "tf.ExpandDims"(%493, %9) {device = ""} : (tensor, tensor) -> tensor + %495 = "tf.Shape"(%493) {device = ""} : (tensor) -> tensor<1xi32> + %496 = "tf.StridedSlice"(%495, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %497 = "tf.Pack"(%496) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %498 = "tf.Equal"(%232, %15) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %499 = "tf.Select"(%498, %404, %15) {device = ""} : (tensor, tensor, tensor) -> tensor + %500 = "tf.Cast"(%499) {Truncate = false, device = ""} : (tensor) -> tensor + %501 = "tf.Reshape"(%500, %11) {device = ""} : (tensor, tensor<0xi32>) -> tensor + %502 = "tf.Pack"(%9, %501) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %503 = "tf.Mul"(%501, %10) {device = ""} : (tensor, tensor) -> tensor + %504 = "tf.Pack"(%503) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %505 = "tf.ConcatV2"(%11, %504, %11, %16) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %506 = "tf.Pack"(%499) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %507 = "tf.Pack"(%12, %232) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi64> + %508 = "tf.ExpandDims"(%507, %9) {device = ""} : (tensor<2xi64>, tensor) -> tensor<2x1xi64> + %509 = "tf.Tile"(%508, %502) {device = ""} : (tensor<2x1xi64>, tensor<2xi32>) -> tensor<2x?xi64> + %510 = "tf.Reshape"(%509, %505) {device = ""} : (tensor<2x?xi64>, tensor<1xi32>) -> tensor + %511 = "tf.Shape"(%510) {device = ""} : (tensor) -> tensor<1xi64> + %512 = "tf.StridedSlice"(%511, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %513 = "tf.Sub"(%512, %499) {device = ""} : (tensor, tensor) -> tensor + %514 = "tf.Pack"(%513) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %515 = "tf.StridedSlice"(%510, %13, %514, %14) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %516 = "tf.StridedSlice"(%510, %506, %13, %14) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %517:2 = "tf.RaggedRange"(%515, %516, %15) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %518 = "tf.GatherV2"(%239, %517#1, %16) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %519 = "tf.Cast"(%518) {Truncate = false, device = ""} : (tensor) -> tensor + %520 = "tf.BroadcastTo"(%519, %492) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %521 = "tf.Max"(%520, %17) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %522 = "tf.Maximum"(%16, %521) {device = ""} : (tensor, tensor) -> tensor + %523 = "tf.Range"(%16, %522, %9) {device = ""} : (tensor, tensor, tensor) -> tensor + %524 = "tf.Pack"(%9, %522) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %525 = "tf.Tile"(%489, %524) {device = ""} : (tensor, tensor<2xi32>) -> tensor + %526 = "tf.Shape"(%525) {device = ""} : (tensor) -> tensor<2xi32> + %527 = "tf.StridedSlice"(%526, %17, %7, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<2xi32> + %528 = "tf.Prod"(%527, %17) {device = "", keep_dims = false} : (tensor<2xi32>, tensor<1xi32>) -> tensor + %529 = "tf.Pack"(%528) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %530 = "tf.Shape"(%525) {device = ""} : (tensor) -> tensor<2xi32> + %531 = "tf.StridedSlice"(%530, %17, %17, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %532 = "tf.Shape"(%525) {device = ""} : (tensor) -> tensor<2xi32> + %533 = "tf.StridedSlice"(%532, %7, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %534 = "tf.ConcatV2"(%531, %529, %533, %16) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %535 = "tf.Reshape"(%525, %534) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %536 = "tf.ExpandDims"(%520, %2) {device = ""} : (tensor, tensor) -> tensor + %537 = "tf.Less"(%523, %536) {device = ""} : (tensor, tensor) -> tensor + %538 = "tf.Reshape"(%537, %6) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %539 = "tf.Where"(%538) {device = ""} : (tensor) -> tensor + %540 = "tf.Squeeze"(%539) {device = "", squeeze_dims = [1]} : (tensor) -> tensor + %541 = "tf.GatherV2"(%535, %540, %16) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %542 = "tf.Cast"(%518) {Truncate = false, device = ""} : (tensor) -> tensor + %543 = "tf.BroadcastTo"(%542, %497) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %544 = "tf.Max"(%543, %17) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %545 = "tf.Maximum"(%16, %544) {device = ""} : (tensor, tensor) -> tensor + %546 = "tf.Range"(%16, %545, %9) {device = ""} : (tensor, tensor, tensor) -> tensor + %547 = "tf.Pack"(%9, %545) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %548 = "tf.Tile"(%494, %547) {device = ""} : (tensor, tensor<2xi32>) -> tensor + %549 = "tf.Shape"(%548) {device = ""} : (tensor) -> tensor<2xi32> + %550 = "tf.StridedSlice"(%549, %17, %7, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<2xi32> + %551 = "tf.Prod"(%550, %17) {device = "", keep_dims = false} : (tensor<2xi32>, tensor<1xi32>) -> tensor + %552 = "tf.Pack"(%551) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %553 = "tf.Shape"(%548) {device = ""} : (tensor) -> tensor<2xi32> + %554 = "tf.StridedSlice"(%553, %17, %17, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %555 = "tf.Shape"(%548) {device = ""} : (tensor) -> tensor<2xi32> + %556 = "tf.StridedSlice"(%555, %7, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %557 = "tf.ConcatV2"(%554, %552, %556, %16) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %558 = "tf.Reshape"(%548, %557) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %559 = "tf.ExpandDims"(%543, %2) {device = ""} : (tensor, tensor) -> tensor + %560 = "tf.Less"(%546, %559) {device = ""} : (tensor, tensor) -> tensor + %561 = "tf.Reshape"(%560, %6) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %562 = "tf.Where"(%561) {device = ""} : (tensor) -> tensor + %563 = "tf.Squeeze"(%562) {device = "", squeeze_dims = [1]} : (tensor) -> tensor + %564 = "tf.GatherV2"(%558, %563, %16) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %565:2 = "tf.RaggedRange"(%541, %564, %15) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %566 = "tf.GatherV2"(%482, %565#1, %16) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %567 = "tf.If"(%408, %408, %404, %15) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_Assert_1_AssertGuard_false_112940, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_Assert_1_AssertGuard_true_112930} : (tensor, tensor, tensor, tensor) -> tensor + %568 = "tf.Identity"(%567) {device = ""} : (tensor) -> tensor + %569 = "tf.Select"(%1, %404, %15) {device = ""} : (tensor, tensor, tensor) -> tensor + %570 = "tf.Pack"(%569) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %571 = "tf.ConcatV2"(%0, %570, %14, %16) {device = ""} : (tensor<0xi64>, tensor<1xi64>, tensor<1xi64>, tensor) -> tensor<2xi64> + %572 = "tf.StridedSlice"(%571, %18, %7, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %573 = "tf.Equal"(%572, %15) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %574 = "tf.StridedSlice"(%571, %18, %7, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %575 = "tf.StridedSlice"(%571, %18, %7, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %576 = "tf.Equal"(%575, %15) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %577 = "tf.If"(%576, %576, %575, %518) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_Assert_2_AssertGuard_false_113430, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_Assert_2_AssertGuard_true_113420} : (tensor, tensor, tensor, tensor) -> tensor + %578 = "tf.Identity"(%577) {device = ""} : (tensor) -> tensor + %579 = "tf.If"(%573, %573, %518, %574) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_Assert_3_AssertGuard_false_113790, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_Assert_3_AssertGuard_true_113780} : (tensor, tensor, tensor, tensor) -> tensor + %580 = "tf.Identity"(%579) {device = ""} : (tensor) -> tensor + %581 = "tf.If"(%444, %444, %116, %440) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_AssertGuard_false_118470, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_AssertGuard_true_118460} : (tensor, tensor, tensor, tensor) -> tensor + %582 = "tf.Identity"(%581) {device = ""} : (tensor) -> tensor + %583 = "tf.Equal"(%440, %15) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %584 = "tf.Select"(%583, %116, %440) {device = ""} : (tensor, tensor, tensor) -> tensor + %585 = "tf.Equal"(%584, %15) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %586 = "tf.LogicalOr"(%585, %119) {device = ""} : (tensor, tensor) -> tensor + %587 = "tf.Equal"(%118, %584) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %588 = "tf.LogicalOr"(%586, %587) {device = ""} : (tensor, tensor) -> tensor + %589 = "tf.Select"(%451, %584, %15) {device = ""} : (tensor, tensor, tensor) -> tensor + %590 = "tf.Pack"(%589, %15) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi64> + %591 = "tf.StridedSlice"(%590, %18, %7, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %592 = "tf.Cast"(%591) {Truncate = false, device = ""} : (tensor) -> tensor + %593 = "tf.Reshape"(%592, %11) {device = ""} : (tensor, tensor<0xi32>) -> tensor + %594 = "tf.Pack"(%9, %593) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %595 = "tf.Tile"(%452, %594) {device = ""} : (tensor, tensor<2xi32>) -> tensor + %596 = "tf.Mul"(%593, %455) {device = ""} : (tensor, tensor) -> tensor + %597 = "tf.Pack"(%596) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %598 = "tf.ConcatV2"(%454, %597, %456, %16) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %599 = "tf.Reshape"(%595, %598) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %600 = "tf.Shape"(%599) {device = ""} : (tensor) -> tensor<1xi64> + %601 = "tf.StridedSlice"(%600, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %602 = "tf.Pack"(%591) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %603 = "tf.StridedSlice"(%599, %602, %13, %14) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %604 = "tf.Sub"(%601, %591) {device = ""} : (tensor, tensor) -> tensor + %605 = "tf.Pack"(%604) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %606 = "tf.StridedSlice"(%599, %13, %605, %14) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %607:2 = "tf.RaggedRange"(%606, %603, %15) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %608 = "tf.Select"(%124, %584, %15) {device = ""} : (tensor, tensor, tensor) -> tensor + %609 = "tf.Pack"(%608, %15) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi64> + %610 = "tf.StridedSlice"(%609, %18, %7, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %611 = "tf.Cast"(%610) {Truncate = false, device = ""} : (tensor) -> tensor + %612 = "tf.Reshape"(%611, %11) {device = ""} : (tensor, tensor<0xi32>) -> tensor + %613 = "tf.Pack"(%9, %612) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %614 = "tf.Tile"(%106, %613) {device = ""} : (tensor, tensor<2xi32>) -> tensor + %615 = "tf.Mul"(%612, %109) {device = ""} : (tensor, tensor) -> tensor + %616 = "tf.Pack"(%615) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %617 = "tf.ConcatV2"(%108, %616, %110, %16) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %618 = "tf.Reshape"(%614, %617) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %619 = "tf.Shape"(%618) {device = ""} : (tensor) -> tensor<1xi64> + %620 = "tf.StridedSlice"(%619, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %621 = "tf.Pack"(%610) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %622 = "tf.StridedSlice"(%618, %621, %13, %14) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %623 = "tf.Sub"(%620, %610) {device = ""} : (tensor, tensor) -> tensor + %624 = "tf.Pack"(%623) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %625 = "tf.StridedSlice"(%618, %13, %624, %14) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %626:2 = "tf.RaggedRange"(%625, %622, %15) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %627 = "tf.StridedSlice"(%609, %17, %18, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1xi64> + %628 = "tf.StridedSlice"(%609, %18, %7, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %629 = "tf.Mul"(%113, %628) {device = ""} : (tensor, tensor) -> tensor + %630 = "tf.Tile"(%629, %627) {device = ""} : (tensor, tensor<1xi64>) -> tensor + %631 = "tf.Cumsum"(%630, %16) {device = "", exclusive = false, reverse = false} : (tensor, tensor) -> tensor + %632 = "tf.ConcatV2"(%13, %631, %2) {device = ""} : (tensor<1xi64>, tensor, tensor) -> tensor + %633 = "tf.StridedSlice"(%632, %17, %6, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %634 = "tf.ExpandDims"(%633, %9) {device = ""} : (tensor, tensor) -> tensor + %635 = "tf.Shape"(%633) {device = ""} : (tensor) -> tensor<1xi32> + %636 = "tf.StridedSlice"(%635, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %637 = "tf.Pack"(%636) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %638 = "tf.StridedSlice"(%632, %18, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %639 = "tf.ExpandDims"(%638, %9) {device = ""} : (tensor, tensor) -> tensor + %640 = "tf.Shape"(%638) {device = ""} : (tensor) -> tensor<1xi32> + %641 = "tf.StridedSlice"(%640, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %642 = "tf.Pack"(%641) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %643 = "tf.Equal"(%440, %15) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %644 = "tf.Select"(%643, %584, %15) {device = ""} : (tensor, tensor, tensor) -> tensor + %645 = "tf.Cast"(%644) {Truncate = false, device = ""} : (tensor) -> tensor + %646 = "tf.Reshape"(%645, %11) {device = ""} : (tensor, tensor<0xi32>) -> tensor + %647 = "tf.Pack"(%9, %646) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %648 = "tf.Mul"(%646, %10) {device = ""} : (tensor, tensor) -> tensor + %649 = "tf.Pack"(%648) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %650 = "tf.ConcatV2"(%11, %649, %11, %16) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %651 = "tf.Pack"(%644) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %652 = "tf.Pack"(%12, %440) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi64> + %653 = "tf.ExpandDims"(%652, %9) {device = ""} : (tensor<2xi64>, tensor) -> tensor<2x1xi64> + %654 = "tf.Tile"(%653, %647) {device = ""} : (tensor<2x1xi64>, tensor<2xi32>) -> tensor<2x?xi64> + %655 = "tf.Reshape"(%654, %650) {device = ""} : (tensor<2x?xi64>, tensor<1xi32>) -> tensor + %656 = "tf.Shape"(%655) {device = ""} : (tensor) -> tensor<1xi64> + %657 = "tf.StridedSlice"(%656, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %658 = "tf.Sub"(%657, %644) {device = ""} : (tensor, tensor) -> tensor + %659 = "tf.Pack"(%658) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %660 = "tf.StridedSlice"(%655, %13, %659, %14) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %661 = "tf.StridedSlice"(%655, %651, %13, %14) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %662:2 = "tf.RaggedRange"(%660, %661, %15) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %663 = "tf.GatherV2"(%447, %662#1, %16) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %664 = "tf.Cast"(%663) {Truncate = false, device = ""} : (tensor) -> tensor + %665 = "tf.BroadcastTo"(%664, %637) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %666 = "tf.Max"(%665, %17) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %667 = "tf.Maximum"(%16, %666) {device = ""} : (tensor, tensor) -> tensor + %668 = "tf.Range"(%16, %667, %9) {device = ""} : (tensor, tensor, tensor) -> tensor + %669 = "tf.Pack"(%9, %667) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %670 = "tf.Tile"(%634, %669) {device = ""} : (tensor, tensor<2xi32>) -> tensor + %671 = "tf.Shape"(%670) {device = ""} : (tensor) -> tensor<2xi32> + %672 = "tf.StridedSlice"(%671, %17, %7, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<2xi32> + %673 = "tf.Prod"(%672, %17) {device = "", keep_dims = false} : (tensor<2xi32>, tensor<1xi32>) -> tensor + %674 = "tf.Pack"(%673) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %675 = "tf.Shape"(%670) {device = ""} : (tensor) -> tensor<2xi32> + %676 = "tf.StridedSlice"(%675, %17, %17, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %677 = "tf.Shape"(%670) {device = ""} : (tensor) -> tensor<2xi32> + %678 = "tf.StridedSlice"(%677, %7, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %679 = "tf.ConcatV2"(%676, %674, %678, %16) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %680 = "tf.Reshape"(%670, %679) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %681 = "tf.ExpandDims"(%665, %2) {device = ""} : (tensor, tensor) -> tensor + %682 = "tf.Less"(%668, %681) {device = ""} : (tensor, tensor) -> tensor + %683 = "tf.Reshape"(%682, %6) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %684 = "tf.Where"(%683) {device = ""} : (tensor) -> tensor + %685 = "tf.Squeeze"(%684) {device = "", squeeze_dims = [1]} : (tensor) -> tensor + %686 = "tf.GatherV2"(%680, %685, %16) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %687 = "tf.Cast"(%663) {Truncate = false, device = ""} : (tensor) -> tensor + %688 = "tf.BroadcastTo"(%687, %642) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %689 = "tf.Max"(%688, %17) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %690 = "tf.Maximum"(%16, %689) {device = ""} : (tensor, tensor) -> tensor + %691 = "tf.Range"(%16, %690, %9) {device = ""} : (tensor, tensor, tensor) -> tensor + %692 = "tf.Pack"(%9, %690) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %693 = "tf.Tile"(%639, %692) {device = ""} : (tensor, tensor<2xi32>) -> tensor + %694 = "tf.Shape"(%693) {device = ""} : (tensor) -> tensor<2xi32> + %695 = "tf.StridedSlice"(%694, %17, %7, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<2xi32> + %696 = "tf.Prod"(%695, %17) {device = "", keep_dims = false} : (tensor<2xi32>, tensor<1xi32>) -> tensor + %697 = "tf.Pack"(%696) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %698 = "tf.Shape"(%693) {device = ""} : (tensor) -> tensor<2xi32> + %699 = "tf.StridedSlice"(%698, %17, %17, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %700 = "tf.Shape"(%693) {device = ""} : (tensor) -> tensor<2xi32> + %701 = "tf.StridedSlice"(%700, %7, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %702 = "tf.ConcatV2"(%699, %697, %701, %16) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %703 = "tf.Reshape"(%693, %702) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %704 = "tf.ExpandDims"(%688, %2) {device = ""} : (tensor, tensor) -> tensor + %705 = "tf.Less"(%691, %704) {device = ""} : (tensor, tensor) -> tensor + %706 = "tf.Reshape"(%705, %6) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %707 = "tf.Where"(%706) {device = ""} : (tensor) -> tensor + %708 = "tf.Squeeze"(%707) {device = "", squeeze_dims = [1]} : (tensor) -> tensor + %709 = "tf.GatherV2"(%703, %708, %16) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %710:2 = "tf.RaggedRange"(%686, %709, %15) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %711 = "tf.If"(%588, %588, %584, %120) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_1_AssertGuard_false_119540, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_1_AssertGuard_true_119530} : (tensor, tensor, tensor, tensor) -> tensor + %712 = "tf.Identity"(%711) {device = ""} : (tensor) -> tensor + %713 = "tf.StridedSlice"(%115, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %714 = "tf.Equal"(%713, %15) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %715 = "tf.Select"(%714, %584, %713) {device = ""} : (tensor, tensor, tensor) -> tensor + %716 = "tf.Pack"(%715) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %717 = "tf.StridedSlice"(%115, %17, %17, %18) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi64> + %718 = "tf.StridedSlice"(%115, %18, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1xi64> + %719 = "tf.ConcatV2"(%717, %716, %718, %16) {device = ""} : (tensor<0xi64>, tensor<1xi64>, tensor<1xi64>, tensor) -> tensor<2xi64> + %720 = "tf.StridedSlice"(%719, %18, %7, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %721 = "tf.Equal"(%720, %15) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %722 = "tf.StridedSlice"(%719, %18, %7, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %723 = "tf.StridedSlice"(%719, %18, %7, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %724 = "tf.Equal"(%723, %15) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %725 = "tf.If"(%724, %724, %723, %663) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_2_AssertGuard_false_120030, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_2_AssertGuard_true_120020} : (tensor, tensor, tensor, tensor) -> tensor + %726 = "tf.Identity"(%725) {device = ""} : (tensor) -> tensor + %727 = "tf.If"(%721, %721, %663, %722) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_3_AssertGuard_false_120390, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_3_AssertGuard_true_120380} : (tensor, tensor, tensor, tensor) -> tensor + %728 = "tf.Identity"(%168) {device = ""} : (tensor) -> tensor + %729 = "tf.Identity"(%727) {device = ""} : (tensor) -> tensor + %730 = "tf.Identity"(%400) {device = ""} : (tensor) -> tensor + %731 = "tf.Shape"(%125#2) {device = ""} : (tensor) -> tensor<1xi32> + %732 = "tf.StridedSlice"(%731, %18, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %733 = "tf.Cast"(%732) {Truncate = false, device = ""} : (tensor<0xi32>) -> tensor<0xi64> + %734 = "tf.Identity"(%733) {device = ""} : (tensor<0xi64>) -> tensor<0xi64> + %735 = "tf.Shape"(%125#3) {device = ""} : (tensor) -> tensor<1xi32> + %736 = "tf.StridedSlice"(%735, %18, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %737 = "tf.Cast"(%736) {Truncate = false, device = ""} : (tensor<0xi32>) -> tensor<0xi64> + %738 = "tf.Identity"(%737) {device = ""} : (tensor<0xi64>) -> tensor<0xi64> + %739 = "tf.GatherV2"(%125#3, %428, %16) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %740 = "tf.Tile"(%739, %432) {device = ""} : (tensor, tensor<1xi64>) -> tensor + %741 = "tf.Sub"(%740, %566) {device = ""} : (tensor, tensor) -> tensor + %742 = "tf.Shape"(%741) {device = ""} : (tensor) -> tensor<1xi32> + %743 = "tf.StridedSlice"(%742, %18, %17, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %744 = "tf.Cast"(%743) {Truncate = false, device = ""} : (tensor<0xi32>) -> tensor<0xi64> + %745 = "tf.Identity"(%744) {device = ""} : (tensor<0xi64>) -> tensor<0xi64> + %746 = "tf.UnicodeEncode"(%125#0, %146) {Tsplits = i64, device = "", errors = "replace", output_encoding = "UTF-8", replacement_char = 65533 : i64} : (tensor, tensor) -> tensor + %747 = "tf.Identity"(%746) {device = ""} : (tensor) -> tensor + %748 = "tf.StridedSlice"(%19, %17, %18, %18) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %749 = "tf.AddV2"(%748, %15) {device = ""} : (tensor, tensor) -> tensor + %750 = "tf.Range"(%12, %749, %15) {device = ""} : (tensor, tensor, tensor) -> tensor + %751 = "tf.Mul"(%750, %15) {device = ""} : (tensor, tensor) -> tensor + %752 = "tf.Identity"(%751) {device = ""} : (tensor) -> tensor + func.return %747, %752, %728 : tensor, tensor, tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedConcat_assert_equal_1_Assert_AssertGuard_false_99640(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Input tensors have incompatible shapes."> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenize/RaggedConcat/RaggedFromTensor/strided_slice_4:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenize/RaggedConcat/RaggedNRows/sub:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedConcat_assert_equal_1_Assert_AssertGuard_true_99630(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_false_100400(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to from_row_splits do not form a valid RaggedTensor:zero"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits/RaggedFromRowSplits/RowPartitionFromRowSplits/strided_slice:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits/RaggedFromRowSplits/RowPartitionFromRowSplits/Const:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_true_100390(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_false_100760(%arg0: tensor, %arg1: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to from_row_splits do not form a valid RaggedTensor:monotonic"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x >= 0 did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits/RaggedFromRowSplits/RowPartitionFromRowSplits/sub:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor) -> () + %3 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %4 = "tf.Identity"(%3) {device = ""} : (tensor) -> tensor + func.return %4 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_true_100750(%arg0: tensor, %arg1: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_false_101100(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to _from_row_partition do not form a valid RaggedTensor"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits/RaggedFromRowSplits/strided_slice_1:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits/RaggedFromRowSplits/strided_slice:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_true_101090(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_false_101470(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to from_row_splits do not form a valid RaggedTensor:zero"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits/RaggedFromRowSplits_1/RowPartitionFromRowSplits/strided_slice:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits/RaggedFromRowSplits_1/RowPartitionFromRowSplits/Const:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_true_101460(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_false_101830(%arg0: tensor, %arg1: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to from_row_splits do not form a valid RaggedTensor:monotonic"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x >= 0 did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits/RaggedFromRowSplits_1/RowPartitionFromRowSplits/sub:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor) -> () + %3 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %4 = "tf.Identity"(%3) {device = ""} : (tensor) -> tensor + func.return %4 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_true_101820(%arg0: tensor, %arg1: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_assert_equal_1_Assert_AssertGuard_false_102190(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to _from_row_partition do not form a valid RaggedTensor"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits/RaggedFromRowSplits_1/strided_slice:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits/RaggedFromRowSplits_1/RaggedNRows/sub:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_assert_equal_1_Assert_AssertGuard_true_102180(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_false_102540(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to from_row_splits do not form a valid RaggedTensor:zero"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits_1/RaggedFromRowSplits/RowPartitionFromRowSplits/strided_slice:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits_1/RaggedFromRowSplits/RowPartitionFromRowSplits/Const:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_true_102530(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_false_102900(%arg0: tensor, %arg1: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to from_row_splits do not form a valid RaggedTensor:monotonic"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x >= 0 did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits_1/RaggedFromRowSplits/RowPartitionFromRowSplits/sub:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor) -> () + %3 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %4 = "tf.Identity"(%3) {device = ""} : (tensor) -> tensor + func.return %4 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_true_102890(%arg0: tensor, %arg1: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_false_103240(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to _from_row_partition do not form a valid RaggedTensor"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits_1/RaggedFromRowSplits/strided_slice_1:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits_1/RaggedFromRowSplits/strided_slice:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_true_103230(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_false_103610(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to from_row_splits do not form a valid RaggedTensor:zero"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits_2/RaggedFromRowSplits/RowPartitionFromRowSplits/strided_slice:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits_2/RaggedFromRowSplits/RowPartitionFromRowSplits/Const:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_true_103600(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_false_103970(%arg0: tensor, %arg1: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to from_row_splits do not form a valid RaggedTensor:monotonic"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x >= 0 did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits_2/RaggedFromRowSplits/RowPartitionFromRowSplits/sub:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor) -> () + %3 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %4 = "tf.Identity"(%3) {device = ""} : (tensor) -> tensor + func.return %4 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_true_103960(%arg0: tensor, %arg1: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_false_104310(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to _from_row_partition do not form a valid RaggedTensor"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits_2/RaggedFromRowSplits/strided_slice_1:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits_2/RaggedFromRowSplits/strided_slice:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_true_104300(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_AssertGuard_false_105110(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_AssertGuard_true_105100(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_1_AssertGuard_false_106180(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_1_AssertGuard_true_106170(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_2_AssertGuard_false_106670(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<1> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_2_AssertGuard_true_106660(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_3_AssertGuard_false_107030(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<1> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_3_AssertGuard_true_107020(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_Assert_AssertGuard_false_111870(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_Assert_AssertGuard_true_111860(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_Assert_1_AssertGuard_false_112940(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_Assert_1_AssertGuard_true_112930(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_Assert_2_AssertGuard_false_113430(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<1> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_Assert_2_AssertGuard_true_113420(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_Assert_3_AssertGuard_false_113790(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<1> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_Assert_3_AssertGuard_true_113780(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_AssertGuard_false_118470(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_AssertGuard_true_118460(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_1_AssertGuard_false_119540(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_1_AssertGuard_true_119530(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_2_AssertGuard_false_120030(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<1> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_2_AssertGuard_true_120020(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_3_AssertGuard_false_120390(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<1> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_3_AssertGuard_true_120380(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} + + + +// CHECK: func private @whitespace_tokenizer_rank2(%arg0: tensor {tf._user_specified_name = "input"}) -> (tensor, tensor, tensor) attributes {tf._implements = #tf_type.func<@"tftext:WhitespaceTokenizer", {}>, tf._input_shapes = [#tf_type.shape], tf.signature.is_stateful} { +// CHECK: %0:3 = "tfl.custom"(%arg0) <{custom_code = "tftext:WhitespaceTokenizer", custom_option = #tfl}> : (tensor) -> (tensor, tensor, tensor) +// CHECK: return %0#0, %0#1, %0#2 : tensor, tensor, tensor + +func.func private @whitespace_tokenizer_rank0(%arg0: tensor {tf._user_specified_name = "input"}) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>], tf._implements = #tf_type.func<@"tftext:WhitespaceTokenizer", {}>, tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<[0, 1]> : tensor<2xi64>} : () -> tensor<2xi64> + %1 = "tf.Const"() {value = dense<[]> : tensor<0xi64>} : () -> tensor<0xi64> + %2 = "tf.Const"() {value = dense : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<-1> : tensor} : () -> tensor + %4 = "tf.Const"() {value = dense<[[0], [1]]> : tensor<2x1xi64>} : () -> tensor<2x1xi64> + %5 = "tf.Const"() {value = dense<-1> : tensor<1xi32>} : () -> tensor<1xi32> + %6 = "tf.Const"() {value = dense<2> : tensor<1xi32>} : () -> tensor<1xi32> + %7 = "tf.Const"() {value = dense<1> : tensor} : () -> tensor + %8 = "tf.Const"() {value = dense<2> : tensor} : () -> tensor + %9 = "tf.Const"() {value = dense<[]> : tensor<0xi32>} : () -> tensor<0xi32> + %10 = "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %11 = "tf.Const"() {value = dense<0> : tensor<1xi64>} : () -> tensor<1xi64> + %12 = "tf.Const"() {value = dense<1> : tensor<1xi64>} : () -> tensor<1xi64> + %13 = "tf.Const"() {value = dense<1> : tensor} : () -> tensor + %14 = "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %15 = "tf.Const"() {value = dense<0> : tensor<1xi32>} : () -> tensor<1xi32> + %16 = "tf.Const"() {value = dense<1> : tensor<1xi32>} : () -> tensor<1xi32> + %17 = "tf.If"(%2, %2, %13, %13) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedConcat_assert_equal_1_Assert_AssertGuard_false_3220, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedConcat_assert_equal_1_Assert_AssertGuard_true_3210} : (tensor, tensor, tensor, tensor) -> tensor + %18 = "tf.Identity"(%17) {device = ""} : (tensor) -> tensor + %19 = "tf.Pack"(%arg0) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1x!tf_type.string> + %20 = "tf.StringLength"(%19) {device = "", unit = "BYTE"} : (tensor<1x!tf_type.string>) -> tensor<1xi32> + %21 = "tf.ExpandDims"(%20, %7) {device = ""} : (tensor<1xi32>, tensor) -> tensor<1x1xi32> + %22 = "tf.Cast"(%21) {Truncate = false, device = ""} : (tensor<1x1xi32>) -> tensor<1x1xi64> + %23 = "tf.Reshape"(%22, %12) {device = ""} : (tensor<1x1xi64>, tensor<1xi64>) -> tensor<1xi64> + %24 = "tf.Reshape"(%19, %5) {device = ""} : (tensor<1x!tf_type.string>, tensor<1xi32>) -> tensor<1x!tf_type.string> + %25:3 = "tf.UnicodeDecodeWithOffsets"(%24) {Tsplits = i64, device = "", errors = "replace", input_encoding = "UTF-8", replace_control_characters = false, replacement_char = 65533 : i64} : (tensor<1x!tf_type.string>) -> (tensor<2xi64>, tensor, tensor) + %26 = "tf.StridedSlice"(%25#0, %15, %5, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1xi64> + %27 = "tf.AddV2"(%26, %13) {device = ""} : (tensor<1xi64>, tensor) -> tensor<1xi64> + %28 = "tf.StridedSlice"(%25#0, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1xi64> + %29 = "tf.Minimum"(%27, %28) {device = ""} : (tensor<1xi64>, tensor<1xi64>) -> tensor<1xi64> + %30:2 = "tf.RaggedRange"(%29, %28, %13) {T = i64, Tsplits = i64, device = ""} : (tensor<1xi64>, tensor<1xi64>, tensor) -> (tensor<2xi64>, tensor) + %31 = "tf.StridedSlice"(%30#0, %5, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %32 = "tf.AddV2"(%31, %12) {device = ""} : (tensor, tensor<1xi64>) -> tensor<1xi64> + %33 = "tf.ConcatV2"(%30#0, %32, %14) {device = ""} : (tensor<2xi64>, tensor<1xi64>, tensor) -> tensor<3xi64> + %34 = "tf.GatherV2"(%25#2, %30#1, %14) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %35 = "tf.ConcatV2"(%34, %23, %14) {device = ""} : (tensor, tensor<1xi64>, tensor) -> tensor + %36:2 = "tf.RaggedGather"(%33, %35, %0) {OUTPUT_RAGGED_RANK = 1 : i64, PARAMS_RAGGED_RANK = 1 : i64, Tindices = i64, Tsplits = i64, Tvalues = i64, device = ""} : (tensor<3xi64>, tensor, tensor<2xi64>) -> (tensor, tensor) + %37:5 = "tf.WhitespaceTokenizeWithOffsets"(%25#1, %25#0) {Tsplits = i64, device = ""} : (tensor, tensor<2xi64>) -> (tensor, tensor, tensor, tensor, tensor) + %38 = "tf.StridedSlice"(%37#1, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %39 = "tf.Equal"(%38, %10) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %40 = "tf.All"(%39, %9) {device = "", keep_dims = false} : (tensor, tensor<0xi32>) -> tensor + %41 = "tf.If"(%40, %40, %38, %10) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_false_3980, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_true_3970} : (tensor, tensor, tensor, tensor) -> tensor + %42 = "tf.Identity"(%41) {device = ""} : (tensor) -> tensor + %43 = "tf.StridedSlice"(%37#1, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %44 = "tf.StridedSlice"(%37#1, %15, %5, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %45 = "tf.Sub"(%43, %44) {device = ""} : (tensor, tensor) -> tensor + %46 = "tf.LessEqual"(%10, %45) {device = ""} : (tensor, tensor) -> tensor + %47 = "tf.All"(%46, %15) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %48 = "tf.If"(%47, %47, %45) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_false_4340, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_true_4330} : (tensor, tensor, tensor) -> tensor + %49 = "tf.Identity"(%48) {device = ""} : (tensor) -> tensor + %50 = "tf.Identity"(%37#1) {_class = ["loc:@WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenizeWithOffsets"], device = ""} : (tensor) -> tensor + %51 = "tf.StridedSlice"(%50, %5, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %52 = "tf.Shape"(%37#0) {device = ""} : (tensor) -> tensor<1xi64> + %53 = "tf.StridedSlice"(%52, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %54 = "tf.Equal"(%51, %53) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %55 = "tf.All"(%54, %9) {device = "", keep_dims = false} : (tensor, tensor<0xi32>) -> tensor + %56 = "tf.If"(%55, %55, %51, %53) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_false_4680, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_true_4670} : (tensor, tensor, tensor, tensor) -> tensor + %57 = "tf.Identity"(%56) {device = ""} : (tensor) -> tensor + %58 = "tf.Identity"(%50) {_class = ["loc:@WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenizeWithOffsets"], device = ""} : (tensor) -> tensor + %59 = "tf.Shape"(%58) {device = ""} : (tensor) -> tensor<1xi64> + %60 = "tf.StridedSlice"(%59, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %61 = "tf.Sub"(%60, %13) {device = ""} : (tensor, tensor) -> tensor + %62 = "tf.StridedSlice"(%37#4, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %63 = "tf.Equal"(%62, %10) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %64 = "tf.All"(%63, %9) {device = "", keep_dims = false} : (tensor, tensor<0xi32>) -> tensor + %65 = "tf.If"(%64, %64, %62, %10) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_false_5050, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_true_5040} : (tensor, tensor, tensor, tensor) -> tensor + %66 = "tf.Identity"(%65) {device = ""} : (tensor) -> tensor + %67 = "tf.StridedSlice"(%37#4, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %68 = "tf.StridedSlice"(%37#4, %15, %5, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %69 = "tf.Sub"(%67, %68) {device = ""} : (tensor, tensor) -> tensor + %70 = "tf.LessEqual"(%10, %69) {device = ""} : (tensor, tensor) -> tensor + %71 = "tf.All"(%70, %15) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %72 = "tf.If"(%71, %71, %69) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_false_5410, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_true_5400} : (tensor, tensor, tensor) -> tensor + %73 = "tf.Identity"(%72) {device = ""} : (tensor) -> tensor + %74 = "tf.Identity"(%37#4) {_class = ["loc:@WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenizeWithOffsets"], device = ""} : (tensor) -> tensor + %75 = "tf.StridedSlice"(%74, %5, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %76 = "tf.Equal"(%75, %61) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %77 = "tf.All"(%76, %9) {device = "", keep_dims = false} : (tensor, tensor<0xi32>) -> tensor + %78 = "tf.If"(%77, %77, %75, %61) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_assert_equal_1_Assert_AssertGuard_false_5770, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_assert_equal_1_Assert_AssertGuard_true_5760} : (tensor, tensor, tensor, tensor) -> tensor + %79 = "tf.Identity"(%78) {device = ""} : (tensor) -> tensor + %80 = "tf.Identity"(%74) {_class = ["loc:@WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenizeWithOffsets"], device = ""} : (tensor) -> tensor + %81 = "tf.StridedSlice"(%37#4, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %82 = "tf.Equal"(%81, %10) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %83 = "tf.All"(%82, %9) {device = "", keep_dims = false} : (tensor, tensor<0xi32>) -> tensor + %84 = "tf.If"(%83, %83, %81, %10) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_false_6120, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_true_6110} : (tensor, tensor, tensor, tensor) -> tensor + %85 = "tf.Identity"(%84) {device = ""} : (tensor) -> tensor + %86 = "tf.StridedSlice"(%37#4, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %87 = "tf.StridedSlice"(%37#4, %15, %5, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %88 = "tf.Sub"(%86, %87) {device = ""} : (tensor, tensor) -> tensor + %89 = "tf.LessEqual"(%10, %88) {device = ""} : (tensor, tensor) -> tensor + %90 = "tf.All"(%89, %15) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %91 = "tf.If"(%90, %90, %88) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_false_6480, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_true_6470} : (tensor, tensor, tensor) -> tensor + %92 = "tf.Identity"(%91) {device = ""} : (tensor) -> tensor + %93 = "tf.Identity"(%37#4) {_class = ["loc:@WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenizeWithOffsets"], device = ""} : (tensor) -> tensor + %94 = "tf.StridedSlice"(%93, %5, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %95 = "tf.Shape"(%37#2) {device = ""} : (tensor) -> tensor<1xi64> + %96 = "tf.StridedSlice"(%95, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %97 = "tf.Equal"(%94, %96) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %98 = "tf.All"(%97, %9) {device = "", keep_dims = false} : (tensor, tensor<0xi32>) -> tensor + %99 = "tf.If"(%98, %98, %94, %96) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_false_6820, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_true_6810} : (tensor, tensor, tensor, tensor) -> tensor + %100 = "tf.Identity"(%99) {device = ""} : (tensor) -> tensor + %101 = "tf.Identity"(%93) {_class = ["loc:@WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenizeWithOffsets"], device = ""} : (tensor) -> tensor + %102 = "tf.Shape"(%101) {device = ""} : (tensor) -> tensor<1xi64> + %103 = "tf.StridedSlice"(%102, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %104 = "tf.Sub"(%103, %13) {device = ""} : (tensor, tensor) -> tensor + %105 = "tf.Equal"(%104, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %106 = "tf.LogicalOr"(%105, %2) {device = ""} : (tensor, tensor) -> tensor + %107 = "tf.Equal"(%104, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %108 = "tf.LogicalOr"(%106, %107) {device = ""} : (tensor, tensor) -> tensor + %109 = "tf.StridedSlice"(%101, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %110 = "tf.StridedSlice"(%101, %15, %5, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %111 = "tf.Sub"(%109, %110) {device = ""} : (tensor, tensor) -> tensor + %112 = "tf.Shape"(%101) {device = ""} : (tensor) -> tensor<1xi64> + %113 = "tf.StridedSlice"(%112, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %114 = "tf.Sub"(%113, %13) {device = ""} : (tensor, tensor) -> tensor + %115 = "tf.Equal"(%114, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %116 = "tf.ExpandDims"(%101, %7) {device = ""} : (tensor, tensor) -> tensor + %117 = "tf.Shape"(%101) {device = ""} : (tensor) -> tensor<1xi32> + %118 = "tf.StridedSlice"(%117, %15, %15, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %119 = "tf.StridedSlice"(%117, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %120 = "tf.StridedSlice"(%117, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %121 = "tf.StridedSlice"(%37#4, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %122 = "tf.Equal"(%121, %10) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %123 = "tf.All"(%122, %9) {device = "", keep_dims = false} : (tensor, tensor<0xi32>) -> tensor + %124 = "tf.If"(%123, %123, %121, %10) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_false_7190, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_true_7180} : (tensor, tensor, tensor, tensor) -> tensor + %125 = "tf.Identity"(%124) {device = ""} : (tensor) -> tensor + %126 = "tf.StridedSlice"(%37#4, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %127 = "tf.StridedSlice"(%37#4, %15, %5, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %128 = "tf.Sub"(%126, %127) {device = ""} : (tensor, tensor) -> tensor + %129 = "tf.LessEqual"(%10, %128) {device = ""} : (tensor, tensor) -> tensor + %130 = "tf.All"(%129, %15) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %131 = "tf.If"(%130, %130, %128) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_false_7550, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_true_7540} : (tensor, tensor, tensor) -> tensor + %132 = "tf.Identity"(%131) {device = ""} : (tensor) -> tensor + %133 = "tf.Identity"(%37#4) {_class = ["loc:@WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenizeWithOffsets"], device = ""} : (tensor) -> tensor + %134 = "tf.StridedSlice"(%133, %5, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %135 = "tf.Shape"(%37#3) {device = ""} : (tensor) -> tensor<1xi64> + %136 = "tf.StridedSlice"(%135, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %137 = "tf.Equal"(%134, %136) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %138 = "tf.All"(%137, %9) {device = "", keep_dims = false} : (tensor, tensor<0xi32>) -> tensor + %139 = "tf.If"(%138, %138, %134, %136) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_false_7890, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_true_7880} : (tensor, tensor, tensor, tensor) -> tensor + %140 = "tf.Identity"(%139) {device = ""} : (tensor) -> tensor + %141 = "tf.Identity"(%133) {_class = ["loc:@WhitespaceTokenize/WhitespaceTokenize/WhitespaceTokenizeWithOffsets"], device = ""} : (tensor) -> tensor + %142 = "tf.Shape"(%141) {device = ""} : (tensor) -> tensor<1xi64> + %143 = "tf.StridedSlice"(%142, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %144 = "tf.Sub"(%143, %13) {device = ""} : (tensor, tensor) -> tensor + %145 = "tf.Equal"(%144, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %146 = "tf.LogicalOr"(%145, %2) {device = ""} : (tensor, tensor) -> tensor + %147 = "tf.Equal"(%144, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %148 = "tf.LogicalOr"(%146, %147) {device = ""} : (tensor, tensor) -> tensor + %149 = "tf.StridedSlice"(%141, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %150 = "tf.StridedSlice"(%141, %15, %5, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %151 = "tf.Sub"(%149, %150) {device = ""} : (tensor, tensor) -> tensor + %152 = "tf.Shape"(%141) {device = ""} : (tensor) -> tensor<1xi64> + %153 = "tf.StridedSlice"(%152, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %154 = "tf.Sub"(%153, %13) {device = ""} : (tensor, tensor) -> tensor + %155 = "tf.Equal"(%154, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %156 = "tf.ExpandDims"(%141, %7) {device = ""} : (tensor, tensor) -> tensor + %157 = "tf.Shape"(%141) {device = ""} : (tensor) -> tensor<1xi32> + %158 = "tf.StridedSlice"(%157, %15, %15, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %159 = "tf.StridedSlice"(%157, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %160 = "tf.StridedSlice"(%157, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %161 = "tf.StridedSlice"(%141, %5, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %162 = "tf.Range"(%10, %161, %13) {device = ""} : (tensor, tensor, tensor) -> tensor + %163 = "tf.StridedSlice"(%141, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %164 = "tf.StridedSlice"(%141, %15, %5, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %165 = "tf.Sub"(%163, %164) {device = ""} : (tensor, tensor) -> tensor + %166 = "tf.If"(%108, %108, %13, %104) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_AssertGuard_false_8690, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_AssertGuard_true_8680} : (tensor, tensor, tensor, tensor) -> tensor + %167 = "tf.Identity"(%166) {device = ""} : (tensor) -> tensor + %168 = "tf.Equal"(%104, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %169 = "tf.Select"(%168, %13, %104) {device = ""} : (tensor, tensor, tensor) -> tensor + %170 = "tf.Equal"(%169, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %171 = "tf.LogicalOr"(%170, %2) {device = ""} : (tensor, tensor) -> tensor + %172 = "tf.Equal"(%169, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %173 = "tf.LogicalOr"(%171, %172) {device = ""} : (tensor, tensor) -> tensor + %174 = "tf.Select"(%115, %169, %13) {device = ""} : (tensor, tensor, tensor) -> tensor + %175 = "tf.Pack"(%174, %13) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi64> + %176 = "tf.StridedSlice"(%175, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %177 = "tf.Cast"(%176) {Truncate = false, device = ""} : (tensor) -> tensor + %178 = "tf.Reshape"(%177, %9) {device = ""} : (tensor, tensor<0xi32>) -> tensor + %179 = "tf.Pack"(%7, %178) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %180 = "tf.Tile"(%116, %179) {device = ""} : (tensor, tensor<2xi32>) -> tensor + %181 = "tf.Mul"(%178, %119) {device = ""} : (tensor, tensor) -> tensor + %182 = "tf.Pack"(%181) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %183 = "tf.ConcatV2"(%118, %182, %120, %14) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %184 = "tf.Reshape"(%180, %183) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %185 = "tf.Shape"(%184) {device = ""} : (tensor) -> tensor<1xi64> + %186 = "tf.StridedSlice"(%185, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %187 = "tf.Pack"(%176) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %188 = "tf.StridedSlice"(%184, %187, %11, %12) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %189 = "tf.Sub"(%186, %176) {device = ""} : (tensor, tensor) -> tensor + %190 = "tf.Pack"(%189) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %191 = "tf.StridedSlice"(%184, %11, %190, %12) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %192:2 = "tf.RaggedRange"(%191, %188, %13) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %193 = "tf.Select"(%2, %169, %13) {device = ""} : (tensor, tensor, tensor) -> tensor + %194 = "tf.Pack"(%193, %13) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi64> + %195 = "tf.StridedSlice"(%194, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %196 = "tf.Cast"(%195) {Truncate = false, device = ""} : (tensor) -> tensor + %197 = "tf.Reshape"(%196, %9) {device = ""} : (tensor, tensor<0xi32>) -> tensor + %198 = "tf.Pack"(%7, %197) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %199 = "tf.Tile"(%4, %198) {device = ""} : (tensor<2x1xi64>, tensor<2xi32>) -> tensor<2x?xi64> + %200 = "tf.Mul"(%197, %8) {device = ""} : (tensor, tensor) -> tensor + %201 = "tf.Pack"(%200) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %202 = "tf.ConcatV2"(%9, %201, %9, %14) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %203 = "tf.Reshape"(%199, %202) {device = ""} : (tensor<2x?xi64>, tensor<1xi32>) -> tensor + %204 = "tf.Shape"(%203) {device = ""} : (tensor) -> tensor<1xi64> + %205 = "tf.StridedSlice"(%204, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %206 = "tf.Pack"(%195) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %207 = "tf.StridedSlice"(%203, %206, %11, %12) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %208 = "tf.Sub"(%205, %195) {device = ""} : (tensor, tensor) -> tensor + %209 = "tf.Pack"(%208) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %210 = "tf.StridedSlice"(%203, %11, %209, %12) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %211:2 = "tf.RaggedRange"(%210, %207, %13) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %212 = "tf.StridedSlice"(%194, %15, %16, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1xi64> + %213 = "tf.StridedSlice"(%194, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %214 = "tf.Mul"(%213, %12) {device = ""} : (tensor, tensor<1xi64>) -> tensor<1xi64> + %215 = "tf.Tile"(%214, %212) {device = ""} : (tensor<1xi64>, tensor<1xi64>) -> tensor + %216 = "tf.Cumsum"(%215, %14) {device = "", exclusive = false, reverse = false} : (tensor, tensor) -> tensor + %217 = "tf.ConcatV2"(%11, %216, %3) {device = ""} : (tensor<1xi64>, tensor, tensor) -> tensor + %218 = "tf.StridedSlice"(%217, %15, %5, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %219 = "tf.ExpandDims"(%218, %7) {device = ""} : (tensor, tensor) -> tensor + %220 = "tf.Shape"(%218) {device = ""} : (tensor) -> tensor<1xi32> + %221 = "tf.StridedSlice"(%220, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %222 = "tf.Pack"(%221) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %223 = "tf.StridedSlice"(%217, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %224 = "tf.ExpandDims"(%223, %7) {device = ""} : (tensor, tensor) -> tensor + %225 = "tf.Shape"(%223) {device = ""} : (tensor) -> tensor<1xi32> + %226 = "tf.StridedSlice"(%225, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %227 = "tf.Pack"(%226) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %228 = "tf.Equal"(%104, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %229 = "tf.Select"(%228, %169, %13) {device = ""} : (tensor, tensor, tensor) -> tensor + %230 = "tf.Cast"(%229) {Truncate = false, device = ""} : (tensor) -> tensor + %231 = "tf.Reshape"(%230, %9) {device = ""} : (tensor, tensor<0xi32>) -> tensor + %232 = "tf.Pack"(%7, %231) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %233 = "tf.Mul"(%231, %8) {device = ""} : (tensor, tensor) -> tensor + %234 = "tf.Pack"(%233) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %235 = "tf.ConcatV2"(%9, %234, %9, %14) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %236 = "tf.Pack"(%229) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %237 = "tf.Pack"(%10, %104) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi64> + %238 = "tf.ExpandDims"(%237, %7) {device = ""} : (tensor<2xi64>, tensor) -> tensor<2x1xi64> + %239 = "tf.Tile"(%238, %232) {device = ""} : (tensor<2x1xi64>, tensor<2xi32>) -> tensor<2x?xi64> + %240 = "tf.Reshape"(%239, %235) {device = ""} : (tensor<2x?xi64>, tensor<1xi32>) -> tensor + %241 = "tf.Shape"(%240) {device = ""} : (tensor) -> tensor<1xi64> + %242 = "tf.StridedSlice"(%241, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %243 = "tf.Sub"(%242, %229) {device = ""} : (tensor, tensor) -> tensor + %244 = "tf.Pack"(%243) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %245 = "tf.StridedSlice"(%240, %11, %244, %12) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %246 = "tf.StridedSlice"(%240, %236, %11, %12) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %247:2 = "tf.RaggedRange"(%245, %246, %13) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %248 = "tf.GatherV2"(%111, %247#1, %14) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %249 = "tf.Cast"(%248) {Truncate = false, device = ""} : (tensor) -> tensor + %250 = "tf.BroadcastTo"(%249, %222) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %251 = "tf.Max"(%250, %15) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %252 = "tf.Maximum"(%14, %251) {device = ""} : (tensor, tensor) -> tensor + %253 = "tf.Range"(%14, %252, %7) {device = ""} : (tensor, tensor, tensor) -> tensor + %254 = "tf.Pack"(%7, %252) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %255 = "tf.Tile"(%219, %254) {device = ""} : (tensor, tensor<2xi32>) -> tensor + %256 = "tf.Shape"(%255) {device = ""} : (tensor) -> tensor<2xi32> + %257 = "tf.StridedSlice"(%256, %15, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<2xi32> + %258 = "tf.Prod"(%257, %15) {device = "", keep_dims = false} : (tensor<2xi32>, tensor<1xi32>) -> tensor + %259 = "tf.Pack"(%258) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %260 = "tf.Shape"(%255) {device = ""} : (tensor) -> tensor<2xi32> + %261 = "tf.StridedSlice"(%260, %15, %15, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %262 = "tf.Shape"(%255) {device = ""} : (tensor) -> tensor<2xi32> + %263 = "tf.StridedSlice"(%262, %6, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %264 = "tf.ConcatV2"(%261, %259, %263, %14) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %265 = "tf.Reshape"(%255, %264) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %266 = "tf.ExpandDims"(%250, %3) {device = ""} : (tensor, tensor) -> tensor + %267 = "tf.Less"(%253, %266) {device = ""} : (tensor, tensor) -> tensor + %268 = "tf.Reshape"(%267, %5) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %269 = "tf.Where"(%268) {device = ""} : (tensor) -> tensor + %270 = "tf.Squeeze"(%269) {device = "", squeeze_dims = [1]} : (tensor) -> tensor + %271 = "tf.GatherV2"(%265, %270, %14) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %272 = "tf.Cast"(%248) {Truncate = false, device = ""} : (tensor) -> tensor + %273 = "tf.BroadcastTo"(%272, %227) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %274 = "tf.Max"(%273, %15) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %275 = "tf.Maximum"(%14, %274) {device = ""} : (tensor, tensor) -> tensor + %276 = "tf.Range"(%14, %275, %7) {device = ""} : (tensor, tensor, tensor) -> tensor + %277 = "tf.Pack"(%7, %275) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %278 = "tf.Tile"(%224, %277) {device = ""} : (tensor, tensor<2xi32>) -> tensor + %279 = "tf.Shape"(%278) {device = ""} : (tensor) -> tensor<2xi32> + %280 = "tf.StridedSlice"(%279, %15, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<2xi32> + %281 = "tf.Prod"(%280, %15) {device = "", keep_dims = false} : (tensor<2xi32>, tensor<1xi32>) -> tensor + %282 = "tf.Pack"(%281) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %283 = "tf.Shape"(%278) {device = ""} : (tensor) -> tensor<2xi32> + %284 = "tf.StridedSlice"(%283, %15, %15, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %285 = "tf.Shape"(%278) {device = ""} : (tensor) -> tensor<2xi32> + %286 = "tf.StridedSlice"(%285, %6, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %287 = "tf.ConcatV2"(%284, %282, %286, %14) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %288 = "tf.Reshape"(%278, %287) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %289 = "tf.ExpandDims"(%273, %3) {device = ""} : (tensor, tensor) -> tensor + %290 = "tf.Less"(%276, %289) {device = ""} : (tensor, tensor) -> tensor + %291 = "tf.Reshape"(%290, %5) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %292 = "tf.Where"(%291) {device = ""} : (tensor) -> tensor + %293 = "tf.Squeeze"(%292) {device = "", squeeze_dims = [1]} : (tensor) -> tensor + %294 = "tf.GatherV2"(%288, %293, %14) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %295:2 = "tf.RaggedRange"(%271, %294, %13) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %296 = "tf.If"(%173, %173, %169, %13) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_1_AssertGuard_false_9760, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_1_AssertGuard_true_9750} : (tensor, tensor, tensor, tensor) -> tensor + %297 = "tf.Identity"(%296) {device = ""} : (tensor) -> tensor + %298 = "tf.Select"(%2, %169, %13) {device = ""} : (tensor, tensor, tensor) -> tensor + %299 = "tf.Pack"(%298) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %300 = "tf.ConcatV2"(%1, %299, %12, %14) {device = ""} : (tensor<0xi64>, tensor<1xi64>, tensor<1xi64>, tensor) -> tensor<2xi64> + %301 = "tf.StridedSlice"(%300, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %302 = "tf.Equal"(%301, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %303 = "tf.StridedSlice"(%300, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %304 = "tf.StridedSlice"(%300, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %305 = "tf.Equal"(%304, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %306 = "tf.If"(%305, %305, %304, %248) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_2_AssertGuard_false_10250, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_2_AssertGuard_true_10240} : (tensor, tensor, tensor, tensor) -> tensor + %307 = "tf.Identity"(%306) {device = ""} : (tensor) -> tensor + %308 = "tf.If"(%302, %302, %248, %303) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_3_AssertGuard_false_10610, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_3_AssertGuard_true_10600} : (tensor, tensor, tensor, tensor) -> tensor + %309 = "tf.If"(%148, %148, %13, %144) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_Assert_AssertGuard_false_15310, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_Assert_AssertGuard_true_15300} : (tensor, tensor, tensor, tensor) -> tensor + %310 = "tf.Identity"(%309) {device = ""} : (tensor) -> tensor + %311 = "tf.Equal"(%144, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %312 = "tf.Select"(%311, %13, %144) {device = ""} : (tensor, tensor, tensor) -> tensor + %313 = "tf.Equal"(%312, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %314 = "tf.LogicalOr"(%313, %2) {device = ""} : (tensor, tensor) -> tensor + %315 = "tf.Equal"(%312, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %316 = "tf.LogicalOr"(%314, %315) {device = ""} : (tensor, tensor) -> tensor + %317 = "tf.Select"(%155, %312, %13) {device = ""} : (tensor, tensor, tensor) -> tensor + %318 = "tf.Pack"(%317, %13) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi64> + %319 = "tf.StridedSlice"(%318, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %320 = "tf.Cast"(%319) {Truncate = false, device = ""} : (tensor) -> tensor + %321 = "tf.Reshape"(%320, %9) {device = ""} : (tensor, tensor<0xi32>) -> tensor + %322 = "tf.Pack"(%7, %321) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %323 = "tf.Tile"(%156, %322) {device = ""} : (tensor, tensor<2xi32>) -> tensor + %324 = "tf.Mul"(%321, %159) {device = ""} : (tensor, tensor) -> tensor + %325 = "tf.Pack"(%324) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %326 = "tf.ConcatV2"(%158, %325, %160, %14) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %327 = "tf.Reshape"(%323, %326) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %328 = "tf.Shape"(%327) {device = ""} : (tensor) -> tensor<1xi64> + %329 = "tf.StridedSlice"(%328, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %330 = "tf.Pack"(%319) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %331 = "tf.StridedSlice"(%327, %330, %11, %12) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %332 = "tf.Sub"(%329, %319) {device = ""} : (tensor, tensor) -> tensor + %333 = "tf.Pack"(%332) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %334 = "tf.StridedSlice"(%327, %11, %333, %12) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %335:2 = "tf.RaggedRange"(%334, %331, %13) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %336 = "tf.GatherV2"(%162, %335#1, %14) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %337 = "tf.StridedSlice"(%318, %15, %16, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1xi64> + %338 = "tf.StridedSlice"(%318, %15, %16, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1xi64> + %339 = "tf.StridedSlice"(%318, %6, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi64> + %340 = "tf.ConcatV2"(%338, %339, %14) {device = ""} : (tensor<1xi64>, tensor<0xi64>, tensor) -> tensor<1xi64> + %341 = "tf.StridedSlice"(%318, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %342 = "tf.Mul"(%165, %341) {device = ""} : (tensor, tensor) -> tensor + %343 = "tf.Tile"(%342, %337) {device = ""} : (tensor, tensor<1xi64>) -> tensor + %344 = "tf.Cumsum"(%343, %14) {device = "", exclusive = false, reverse = false} : (tensor, tensor) -> tensor + %345 = "tf.ConcatV2"(%11, %344, %3) {device = ""} : (tensor<1xi64>, tensor, tensor) -> tensor + %346 = "tf.Shape"(%345) {device = ""} : (tensor) -> tensor<1xi64> + %347 = "tf.StridedSlice"(%346, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %348 = "tf.Sub"(%347, %13) {device = ""} : (tensor, tensor) -> tensor + %349 = "tf.Equal"(%348, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %350 = "tf.LogicalOr"(%349, %2) {device = ""} : (tensor, tensor) -> tensor + %351 = "tf.Equal"(%348, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %352 = "tf.LogicalOr"(%350, %351) {device = ""} : (tensor, tensor) -> tensor + %353 = "tf.StridedSlice"(%345, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %354 = "tf.StridedSlice"(%345, %15, %5, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %355 = "tf.Sub"(%353, %354) {device = ""} : (tensor, tensor) -> tensor + %356 = "tf.Shape"(%345) {device = ""} : (tensor) -> tensor<1xi64> + %357 = "tf.StridedSlice"(%356, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %358 = "tf.Sub"(%357, %13) {device = ""} : (tensor, tensor) -> tensor + %359 = "tf.Equal"(%358, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %360 = "tf.ExpandDims"(%345, %7) {device = ""} : (tensor, tensor) -> tensor + %361 = "tf.Shape"(%345) {device = ""} : (tensor) -> tensor<1xi32> + %362 = "tf.StridedSlice"(%361, %15, %15, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %363 = "tf.StridedSlice"(%361, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %364 = "tf.StridedSlice"(%361, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %365 = "tf.Select"(%2, %312, %13) {device = ""} : (tensor, tensor, tensor) -> tensor + %366 = "tf.Pack"(%365, %13) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi64> + %367 = "tf.StridedSlice"(%366, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %368 = "tf.Cast"(%367) {Truncate = false, device = ""} : (tensor) -> tensor + %369 = "tf.Reshape"(%368, %9) {device = ""} : (tensor, tensor<0xi32>) -> tensor + %370 = "tf.Pack"(%7, %369) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %371 = "tf.Tile"(%4, %370) {device = ""} : (tensor<2x1xi64>, tensor<2xi32>) -> tensor<2x?xi64> + %372 = "tf.Mul"(%369, %8) {device = ""} : (tensor, tensor) -> tensor + %373 = "tf.Pack"(%372) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %374 = "tf.ConcatV2"(%9, %373, %9, %14) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %375 = "tf.Reshape"(%371, %374) {device = ""} : (tensor<2x?xi64>, tensor<1xi32>) -> tensor + %376 = "tf.Shape"(%375) {device = ""} : (tensor) -> tensor<1xi64> + %377 = "tf.StridedSlice"(%376, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %378 = "tf.Pack"(%367) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %379 = "tf.StridedSlice"(%375, %378, %11, %12) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %380 = "tf.Sub"(%377, %367) {device = ""} : (tensor, tensor) -> tensor + %381 = "tf.Pack"(%380) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %382 = "tf.StridedSlice"(%375, %11, %381, %12) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %383:2 = "tf.RaggedRange"(%382, %379, %13) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %384 = "tf.GatherV2"(%11, %383#1, %14) {batch_dims = 0 : i64, device = ""} : (tensor<1xi64>, tensor, tensor) -> tensor + %385 = "tf.GatherV2"(%12, %384, %14) {batch_dims = 0 : i64, device = ""} : (tensor<1xi64>, tensor, tensor) -> tensor + %386 = "tf.StridedSlice"(%366, %15, %16, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1xi64> + %387 = "tf.StridedSlice"(%366, %15, %16, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1xi64> + %388 = "tf.StridedSlice"(%366, %6, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi64> + %389 = "tf.ConcatV2"(%387, %388, %14) {device = ""} : (tensor<1xi64>, tensor<0xi64>, tensor) -> tensor<1xi64> + %390 = "tf.Tile"(%385, %389) {device = ""} : (tensor, tensor<1xi64>) -> tensor + %391 = "tf.StridedSlice"(%366, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %392 = "tf.Mul"(%391, %12) {device = ""} : (tensor, tensor<1xi64>) -> tensor<1xi64> + %393 = "tf.Tile"(%392, %386) {device = ""} : (tensor<1xi64>, tensor<1xi64>) -> tensor + %394 = "tf.Cumsum"(%393, %14) {device = "", exclusive = false, reverse = false} : (tensor, tensor) -> tensor + %395 = "tf.ConcatV2"(%11, %394, %3) {device = ""} : (tensor<1xi64>, tensor, tensor) -> tensor + %396 = "tf.StridedSlice"(%395, %15, %5, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %397 = "tf.ExpandDims"(%396, %7) {device = ""} : (tensor, tensor) -> tensor + %398 = "tf.Shape"(%396) {device = ""} : (tensor) -> tensor<1xi32> + %399 = "tf.StridedSlice"(%398, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %400 = "tf.Pack"(%399) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %401 = "tf.StridedSlice"(%395, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %402 = "tf.ExpandDims"(%401, %7) {device = ""} : (tensor, tensor) -> tensor + %403 = "tf.Shape"(%401) {device = ""} : (tensor) -> tensor<1xi32> + %404 = "tf.StridedSlice"(%403, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %405 = "tf.Pack"(%404) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %406 = "tf.Equal"(%144, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %407 = "tf.Select"(%406, %312, %13) {device = ""} : (tensor, tensor, tensor) -> tensor + %408 = "tf.Cast"(%407) {Truncate = false, device = ""} : (tensor) -> tensor + %409 = "tf.Reshape"(%408, %9) {device = ""} : (tensor, tensor<0xi32>) -> tensor + %410 = "tf.Pack"(%7, %409) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %411 = "tf.Mul"(%409, %8) {device = ""} : (tensor, tensor) -> tensor + %412 = "tf.Pack"(%411) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %413 = "tf.ConcatV2"(%9, %412, %9, %14) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %414 = "tf.Pack"(%407) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %415 = "tf.Pack"(%10, %144) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi64> + %416 = "tf.ExpandDims"(%415, %7) {device = ""} : (tensor<2xi64>, tensor) -> tensor<2x1xi64> + %417 = "tf.Tile"(%416, %410) {device = ""} : (tensor<2x1xi64>, tensor<2xi32>) -> tensor<2x?xi64> + %418 = "tf.Reshape"(%417, %413) {device = ""} : (tensor<2x?xi64>, tensor<1xi32>) -> tensor + %419 = "tf.Shape"(%418) {device = ""} : (tensor) -> tensor<1xi64> + %420 = "tf.StridedSlice"(%419, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %421 = "tf.Sub"(%420, %407) {device = ""} : (tensor, tensor) -> tensor + %422 = "tf.Pack"(%421) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %423 = "tf.StridedSlice"(%418, %11, %422, %12) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %424 = "tf.StridedSlice"(%418, %414, %11, %12) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %425:2 = "tf.RaggedRange"(%423, %424, %13) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %426 = "tf.GatherV2"(%151, %425#1, %14) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %427 = "tf.Cast"(%426) {Truncate = false, device = ""} : (tensor) -> tensor + %428 = "tf.BroadcastTo"(%427, %400) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %429 = "tf.Max"(%428, %15) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %430 = "tf.Maximum"(%14, %429) {device = ""} : (tensor, tensor) -> tensor + %431 = "tf.Range"(%14, %430, %7) {device = ""} : (tensor, tensor, tensor) -> tensor + %432 = "tf.Pack"(%7, %430) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %433 = "tf.Tile"(%397, %432) {device = ""} : (tensor, tensor<2xi32>) -> tensor + %434 = "tf.Shape"(%433) {device = ""} : (tensor) -> tensor<2xi32> + %435 = "tf.StridedSlice"(%434, %15, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<2xi32> + %436 = "tf.Prod"(%435, %15) {device = "", keep_dims = false} : (tensor<2xi32>, tensor<1xi32>) -> tensor + %437 = "tf.Pack"(%436) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %438 = "tf.Shape"(%433) {device = ""} : (tensor) -> tensor<2xi32> + %439 = "tf.StridedSlice"(%438, %15, %15, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %440 = "tf.Shape"(%433) {device = ""} : (tensor) -> tensor<2xi32> + %441 = "tf.StridedSlice"(%440, %6, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %442 = "tf.ConcatV2"(%439, %437, %441, %14) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %443 = "tf.Reshape"(%433, %442) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %444 = "tf.ExpandDims"(%428, %3) {device = ""} : (tensor, tensor) -> tensor + %445 = "tf.Less"(%431, %444) {device = ""} : (tensor, tensor) -> tensor + %446 = "tf.Reshape"(%445, %5) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %447 = "tf.Where"(%446) {device = ""} : (tensor) -> tensor + %448 = "tf.Squeeze"(%447) {device = "", squeeze_dims = [1]} : (tensor) -> tensor + %449 = "tf.GatherV2"(%443, %448, %14) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %450 = "tf.Cast"(%426) {Truncate = false, device = ""} : (tensor) -> tensor + %451 = "tf.BroadcastTo"(%450, %405) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %452 = "tf.Max"(%451, %15) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %453 = "tf.Maximum"(%14, %452) {device = ""} : (tensor, tensor) -> tensor + %454 = "tf.Range"(%14, %453, %7) {device = ""} : (tensor, tensor, tensor) -> tensor + %455 = "tf.Pack"(%7, %453) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %456 = "tf.Tile"(%402, %455) {device = ""} : (tensor, tensor<2xi32>) -> tensor + %457 = "tf.Shape"(%456) {device = ""} : (tensor) -> tensor<2xi32> + %458 = "tf.StridedSlice"(%457, %15, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<2xi32> + %459 = "tf.Prod"(%458, %15) {device = "", keep_dims = false} : (tensor<2xi32>, tensor<1xi32>) -> tensor + %460 = "tf.Pack"(%459) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %461 = "tf.Shape"(%456) {device = ""} : (tensor) -> tensor<2xi32> + %462 = "tf.StridedSlice"(%461, %15, %15, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %463 = "tf.Shape"(%456) {device = ""} : (tensor) -> tensor<2xi32> + %464 = "tf.StridedSlice"(%463, %6, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %465 = "tf.ConcatV2"(%462, %460, %464, %14) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %466 = "tf.Reshape"(%456, %465) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %467 = "tf.ExpandDims"(%451, %3) {device = ""} : (tensor, tensor) -> tensor + %468 = "tf.Less"(%454, %467) {device = ""} : (tensor, tensor) -> tensor + %469 = "tf.Reshape"(%468, %5) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %470 = "tf.Where"(%469) {device = ""} : (tensor) -> tensor + %471 = "tf.Squeeze"(%470) {device = "", squeeze_dims = [1]} : (tensor) -> tensor + %472 = "tf.GatherV2"(%466, %471, %14) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %473:2 = "tf.RaggedRange"(%449, %472, %13) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %474 = "tf.GatherV2"(%390, %473#1, %14) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %475 = "tf.If"(%316, %316, %312, %13) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_Assert_1_AssertGuard_false_16380, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_Assert_1_AssertGuard_true_16370} : (tensor, tensor, tensor, tensor) -> tensor + %476 = "tf.Identity"(%475) {device = ""} : (tensor) -> tensor + %477 = "tf.Select"(%2, %312, %13) {device = ""} : (tensor, tensor, tensor) -> tensor + %478 = "tf.Pack"(%477) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %479 = "tf.ConcatV2"(%1, %478, %12, %14) {device = ""} : (tensor<0xi64>, tensor<1xi64>, tensor<1xi64>, tensor) -> tensor<2xi64> + %480 = "tf.StridedSlice"(%479, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %481 = "tf.Equal"(%480, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %482 = "tf.StridedSlice"(%479, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %483 = "tf.StridedSlice"(%479, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %484 = "tf.Equal"(%483, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %485 = "tf.If"(%484, %484, %483, %426) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_Assert_2_AssertGuard_false_16870, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_Assert_2_AssertGuard_true_16860} : (tensor, tensor, tensor, tensor) -> tensor + %486 = "tf.Identity"(%485) {device = ""} : (tensor) -> tensor + %487 = "tf.If"(%481, %481, %426, %482) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_Assert_3_AssertGuard_false_17230, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_Assert_3_AssertGuard_true_17220} : (tensor, tensor, tensor, tensor) -> tensor + %488 = "tf.Identity"(%487) {device = ""} : (tensor) -> tensor + %489 = "tf.If"(%352, %352, %13, %348) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_AssertGuard_false_21910, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_AssertGuard_true_21900} : (tensor, tensor, tensor, tensor) -> tensor + %490 = "tf.Identity"(%489) {device = ""} : (tensor) -> tensor + %491 = "tf.Equal"(%348, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %492 = "tf.Select"(%491, %13, %348) {device = ""} : (tensor, tensor, tensor) -> tensor + %493 = "tf.Equal"(%492, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %494 = "tf.LogicalOr"(%493, %2) {device = ""} : (tensor, tensor) -> tensor + %495 = "tf.Equal"(%492, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %496 = "tf.LogicalOr"(%494, %495) {device = ""} : (tensor, tensor) -> tensor + %497 = "tf.Select"(%359, %492, %13) {device = ""} : (tensor, tensor, tensor) -> tensor + %498 = "tf.Pack"(%497, %13) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi64> + %499 = "tf.StridedSlice"(%498, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %500 = "tf.Cast"(%499) {Truncate = false, device = ""} : (tensor) -> tensor + %501 = "tf.Reshape"(%500, %9) {device = ""} : (tensor, tensor<0xi32>) -> tensor + %502 = "tf.Pack"(%7, %501) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %503 = "tf.Tile"(%360, %502) {device = ""} : (tensor, tensor<2xi32>) -> tensor + %504 = "tf.Mul"(%501, %363) {device = ""} : (tensor, tensor) -> tensor + %505 = "tf.Pack"(%504) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %506 = "tf.ConcatV2"(%362, %505, %364, %14) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %507 = "tf.Reshape"(%503, %506) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %508 = "tf.Shape"(%507) {device = ""} : (tensor) -> tensor<1xi64> + %509 = "tf.StridedSlice"(%508, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %510 = "tf.Pack"(%499) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %511 = "tf.StridedSlice"(%507, %510, %11, %12) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %512 = "tf.Sub"(%509, %499) {device = ""} : (tensor, tensor) -> tensor + %513 = "tf.Pack"(%512) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %514 = "tf.StridedSlice"(%507, %11, %513, %12) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %515:2 = "tf.RaggedRange"(%514, %511, %13) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %516 = "tf.Select"(%2, %492, %13) {device = ""} : (tensor, tensor, tensor) -> tensor + %517 = "tf.Pack"(%516, %13) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi64> + %518 = "tf.StridedSlice"(%517, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %519 = "tf.Cast"(%518) {Truncate = false, device = ""} : (tensor) -> tensor + %520 = "tf.Reshape"(%519, %9) {device = ""} : (tensor, tensor<0xi32>) -> tensor + %521 = "tf.Pack"(%7, %520) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %522 = "tf.Tile"(%4, %521) {device = ""} : (tensor<2x1xi64>, tensor<2xi32>) -> tensor<2x?xi64> + %523 = "tf.Mul"(%520, %8) {device = ""} : (tensor, tensor) -> tensor + %524 = "tf.Pack"(%523) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %525 = "tf.ConcatV2"(%9, %524, %9, %14) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %526 = "tf.Reshape"(%522, %525) {device = ""} : (tensor<2x?xi64>, tensor<1xi32>) -> tensor + %527 = "tf.Shape"(%526) {device = ""} : (tensor) -> tensor<1xi64> + %528 = "tf.StridedSlice"(%527, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %529 = "tf.Pack"(%518) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %530 = "tf.StridedSlice"(%526, %529, %11, %12) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %531 = "tf.Sub"(%528, %518) {device = ""} : (tensor, tensor) -> tensor + %532 = "tf.Pack"(%531) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %533 = "tf.StridedSlice"(%526, %11, %532, %12) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %534:2 = "tf.RaggedRange"(%533, %530, %13) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %535 = "tf.StridedSlice"(%517, %15, %16, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1xi64> + %536 = "tf.StridedSlice"(%517, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %537 = "tf.Mul"(%536, %12) {device = ""} : (tensor, tensor<1xi64>) -> tensor<1xi64> + %538 = "tf.Tile"(%537, %535) {device = ""} : (tensor<1xi64>, tensor<1xi64>) -> tensor + %539 = "tf.Cumsum"(%538, %14) {device = "", exclusive = false, reverse = false} : (tensor, tensor) -> tensor + %540 = "tf.ConcatV2"(%11, %539, %3) {device = ""} : (tensor<1xi64>, tensor, tensor) -> tensor + %541 = "tf.StridedSlice"(%540, %15, %5, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %542 = "tf.ExpandDims"(%541, %7) {device = ""} : (tensor, tensor) -> tensor + %543 = "tf.Shape"(%541) {device = ""} : (tensor) -> tensor<1xi32> + %544 = "tf.StridedSlice"(%543, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %545 = "tf.Pack"(%544) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %546 = "tf.StridedSlice"(%540, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %547 = "tf.ExpandDims"(%546, %7) {device = ""} : (tensor, tensor) -> tensor + %548 = "tf.Shape"(%546) {device = ""} : (tensor) -> tensor<1xi32> + %549 = "tf.StridedSlice"(%548, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %550 = "tf.Pack"(%549) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %551 = "tf.Equal"(%348, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %552 = "tf.Select"(%551, %492, %13) {device = ""} : (tensor, tensor, tensor) -> tensor + %553 = "tf.Cast"(%552) {Truncate = false, device = ""} : (tensor) -> tensor + %554 = "tf.Reshape"(%553, %9) {device = ""} : (tensor, tensor<0xi32>) -> tensor + %555 = "tf.Pack"(%7, %554) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %556 = "tf.Mul"(%554, %8) {device = ""} : (tensor, tensor) -> tensor + %557 = "tf.Pack"(%556) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %558 = "tf.ConcatV2"(%9, %557, %9, %14) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %559 = "tf.Pack"(%552) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %560 = "tf.Pack"(%10, %348) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi64> + %561 = "tf.ExpandDims"(%560, %7) {device = ""} : (tensor<2xi64>, tensor) -> tensor<2x1xi64> + %562 = "tf.Tile"(%561, %555) {device = ""} : (tensor<2x1xi64>, tensor<2xi32>) -> tensor<2x?xi64> + %563 = "tf.Reshape"(%562, %558) {device = ""} : (tensor<2x?xi64>, tensor<1xi32>) -> tensor + %564 = "tf.Shape"(%563) {device = ""} : (tensor) -> tensor<1xi64> + %565 = "tf.StridedSlice"(%564, %15, %16, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %566 = "tf.Sub"(%565, %552) {device = ""} : (tensor, tensor) -> tensor + %567 = "tf.Pack"(%566) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %568 = "tf.StridedSlice"(%563, %11, %567, %12) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %569 = "tf.StridedSlice"(%563, %559, %11, %12) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor + %570:2 = "tf.RaggedRange"(%568, %569, %13) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %571 = "tf.GatherV2"(%355, %570#1, %14) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %572 = "tf.Cast"(%571) {Truncate = false, device = ""} : (tensor) -> tensor + %573 = "tf.BroadcastTo"(%572, %545) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %574 = "tf.Max"(%573, %15) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %575 = "tf.Maximum"(%14, %574) {device = ""} : (tensor, tensor) -> tensor + %576 = "tf.Range"(%14, %575, %7) {device = ""} : (tensor, tensor, tensor) -> tensor + %577 = "tf.Pack"(%7, %575) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %578 = "tf.Tile"(%542, %577) {device = ""} : (tensor, tensor<2xi32>) -> tensor + %579 = "tf.Shape"(%578) {device = ""} : (tensor) -> tensor<2xi32> + %580 = "tf.StridedSlice"(%579, %15, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<2xi32> + %581 = "tf.Prod"(%580, %15) {device = "", keep_dims = false} : (tensor<2xi32>, tensor<1xi32>) -> tensor + %582 = "tf.Pack"(%581) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %583 = "tf.Shape"(%578) {device = ""} : (tensor) -> tensor<2xi32> + %584 = "tf.StridedSlice"(%583, %15, %15, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %585 = "tf.Shape"(%578) {device = ""} : (tensor) -> tensor<2xi32> + %586 = "tf.StridedSlice"(%585, %6, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %587 = "tf.ConcatV2"(%584, %582, %586, %14) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %588 = "tf.Reshape"(%578, %587) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %589 = "tf.ExpandDims"(%573, %3) {device = ""} : (tensor, tensor) -> tensor + %590 = "tf.Less"(%576, %589) {device = ""} : (tensor, tensor) -> tensor + %591 = "tf.Reshape"(%590, %5) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %592 = "tf.Where"(%591) {device = ""} : (tensor) -> tensor + %593 = "tf.Squeeze"(%592) {device = "", squeeze_dims = [1]} : (tensor) -> tensor + %594 = "tf.GatherV2"(%588, %593, %14) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %595 = "tf.Cast"(%571) {Truncate = false, device = ""} : (tensor) -> tensor + %596 = "tf.BroadcastTo"(%595, %550) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %597 = "tf.Max"(%596, %15) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %598 = "tf.Maximum"(%14, %597) {device = ""} : (tensor, tensor) -> tensor + %599 = "tf.Range"(%14, %598, %7) {device = ""} : (tensor, tensor, tensor) -> tensor + %600 = "tf.Pack"(%7, %598) {axis = 0 : i64, device = ""} : (tensor, tensor) -> tensor<2xi32> + %601 = "tf.Tile"(%547, %600) {device = ""} : (tensor, tensor<2xi32>) -> tensor + %602 = "tf.Shape"(%601) {device = ""} : (tensor) -> tensor<2xi32> + %603 = "tf.StridedSlice"(%602, %15, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<2xi32> + %604 = "tf.Prod"(%603, %15) {device = "", keep_dims = false} : (tensor<2xi32>, tensor<1xi32>) -> tensor + %605 = "tf.Pack"(%604) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi32> + %606 = "tf.Shape"(%601) {device = ""} : (tensor) -> tensor<2xi32> + %607 = "tf.StridedSlice"(%606, %15, %15, %16) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %608 = "tf.Shape"(%601) {device = ""} : (tensor) -> tensor<2xi32> + %609 = "tf.StridedSlice"(%608, %6, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<2xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %610 = "tf.ConcatV2"(%607, %605, %609, %14) {device = ""} : (tensor<0xi32>, tensor<1xi32>, tensor<0xi32>, tensor) -> tensor<1xi32> + %611 = "tf.Reshape"(%601, %610) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %612 = "tf.ExpandDims"(%596, %3) {device = ""} : (tensor, tensor) -> tensor + %613 = "tf.Less"(%599, %612) {device = ""} : (tensor, tensor) -> tensor + %614 = "tf.Reshape"(%613, %5) {device = ""} : (tensor, tensor<1xi32>) -> tensor + %615 = "tf.Where"(%614) {device = ""} : (tensor) -> tensor + %616 = "tf.Squeeze"(%615) {device = "", squeeze_dims = [1]} : (tensor) -> tensor + %617 = "tf.GatherV2"(%611, %616, %14) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %618:2 = "tf.RaggedRange"(%594, %617, %13) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %619 = "tf.If"(%496, %496, %492, %13) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_1_AssertGuard_false_22980, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_1_AssertGuard_true_22970} : (tensor, tensor, tensor, tensor) -> tensor + %620 = "tf.Identity"(%619) {device = ""} : (tensor) -> tensor + %621 = "tf.Select"(%2, %492, %13) {device = ""} : (tensor, tensor, tensor) -> tensor + %622 = "tf.Pack"(%621) {axis = 0 : i64, device = ""} : (tensor) -> tensor<1xi64> + %623 = "tf.ConcatV2"(%1, %622, %12, %14) {device = ""} : (tensor<0xi64>, tensor<1xi64>, tensor<1xi64>, tensor) -> tensor<2xi64> + %624 = "tf.StridedSlice"(%623, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %625 = "tf.Equal"(%624, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %626 = "tf.StridedSlice"(%623, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %627 = "tf.StridedSlice"(%623, %16, %6, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<2xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %628 = "tf.Equal"(%627, %13) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %629 = "tf.If"(%628, %628, %627, %571) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_2_AssertGuard_false_23470, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_2_AssertGuard_true_23460} : (tensor, tensor, tensor, tensor) -> tensor + %630 = "tf.Identity"(%629) {device = ""} : (tensor) -> tensor + %631 = "tf.If"(%625, %625, %571, %626) {_lower_using_switch_merge = true, _read_only_resource_inputs = [], device = "", else_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_3_AssertGuard_false_23830, is_stateless = false, output_shapes = [#tf_type.shape<>], then_branch = @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_3_AssertGuard_true_23820} : (tensor, tensor, tensor, tensor) -> tensor + %632 = "tf.Identity"(%631) {device = ""} : (tensor) -> tensor + %633 = "tf.Identity"(%308) {device = ""} : (tensor) -> tensor + %634 = "tf.Shape"(%37#2) {device = ""} : (tensor) -> tensor<1xi32> + %635 = "tf.StridedSlice"(%634, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %636 = "tf.Cast"(%635) {Truncate = false, device = ""} : (tensor<0xi32>) -> tensor<0xi64> + %637 = "tf.Identity"(%636) {device = ""} : (tensor<0xi64>) -> tensor<0xi64> + %638 = "tf.Shape"(%37#3) {device = ""} : (tensor) -> tensor<1xi32> + %639 = "tf.StridedSlice"(%638, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %640 = "tf.Cast"(%639) {Truncate = false, device = ""} : (tensor<0xi32>) -> tensor<0xi64> + %641 = "tf.Identity"(%640) {device = ""} : (tensor<0xi64>) -> tensor<0xi64> + %642 = "tf.GatherV2"(%37#3, %336, %14) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %643 = "tf.Tile"(%642, %340) {device = ""} : (tensor, tensor<1xi64>) -> tensor + %644 = "tf.Sub"(%643, %474) {device = ""} : (tensor, tensor) -> tensor + %645 = "tf.Shape"(%644) {device = ""} : (tensor) -> tensor<1xi32> + %646 = "tf.StridedSlice"(%645, %16, %15, %16) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<0xi32> + %647 = "tf.Cast"(%646) {Truncate = false, device = ""} : (tensor<0xi32>) -> tensor<0xi64> + %648 = "tf.Identity"(%647) {device = ""} : (tensor<0xi64>) -> tensor<0xi64> + %649 = "tf.UnicodeEncode"(%37#0, %58) {Tsplits = i64, device = "", errors = "replace", output_encoding = "UTF-8", replacement_char = 65533 : i64} : (tensor, tensor) -> tensor + %650 = "tf.Identity"(%649) {device = ""} : (tensor) -> tensor + func.return %650 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedConcat_assert_equal_1_Assert_AssertGuard_false_3220(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Input tensors have incompatible shapes."> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/WhitespaceTokenize/RaggedConcat/RaggedFromTensor/Const:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (WhitespaceTokenize/WhitespaceTokenize/RaggedConcat/RaggedNRows/Const:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedConcat_assert_equal_1_Assert_AssertGuard_true_3210(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_false_3980(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to from_row_splits do not form a valid RaggedTensor:zero"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits/RaggedFromRowSplits/RowPartitionFromRowSplits/strided_slice:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits/RaggedFromRowSplits/RowPartitionFromRowSplits/Const:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_true_3970(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_false_4340(%arg0: tensor, %arg1: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to from_row_splits do not form a valid RaggedTensor:monotonic"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x >= 0 did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits/RaggedFromRowSplits/RowPartitionFromRowSplits/sub:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor) -> () + %3 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %4 = "tf.Identity"(%3) {device = ""} : (tensor) -> tensor + func.return %4 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_true_4330(%arg0: tensor, %arg1: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_false_4680(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to _from_row_partition do not form a valid RaggedTensor"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits/RaggedFromRowSplits/strided_slice_1:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits/RaggedFromRowSplits/strided_slice:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_true_4670(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_false_5050(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to from_row_splits do not form a valid RaggedTensor:zero"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits/RaggedFromRowSplits_1/RowPartitionFromRowSplits/strided_slice:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits/RaggedFromRowSplits_1/RowPartitionFromRowSplits/Const:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_true_5040(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_false_5410(%arg0: tensor, %arg1: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to from_row_splits do not form a valid RaggedTensor:monotonic"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x >= 0 did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits/RaggedFromRowSplits_1/RowPartitionFromRowSplits/sub:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor) -> () + %3 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %4 = "tf.Identity"(%3) {device = ""} : (tensor) -> tensor + func.return %4 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_true_5400(%arg0: tensor, %arg1: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_assert_equal_1_Assert_AssertGuard_false_5770(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to _from_row_partition do not form a valid RaggedTensor"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits/RaggedFromRowSplits_1/strided_slice:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits/RaggedFromRowSplits_1/RaggedNRows/sub:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_RaggedFromRowSplits_1_assert_equal_1_Assert_AssertGuard_true_5760(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_false_6120(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to from_row_splits do not form a valid RaggedTensor:zero"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits_1/RaggedFromRowSplits/RowPartitionFromRowSplits/strided_slice:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits_1/RaggedFromRowSplits/RowPartitionFromRowSplits/Const:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_true_6110(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_false_6480(%arg0: tensor, %arg1: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to from_row_splits do not form a valid RaggedTensor:monotonic"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x >= 0 did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits_1/RaggedFromRowSplits/RowPartitionFromRowSplits/sub:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor) -> () + %3 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %4 = "tf.Identity"(%3) {device = ""} : (tensor) -> tensor + func.return %4 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_true_6470(%arg0: tensor, %arg1: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_false_6820(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to _from_row_partition do not form a valid RaggedTensor"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits_1/RaggedFromRowSplits/strided_slice_1:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits_1/RaggedFromRowSplits/strided_slice:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_1_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_true_6810(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_false_7190(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to from_row_splits do not form a valid RaggedTensor:zero"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits_2/RaggedFromRowSplits/RowPartitionFromRowSplits/strided_slice:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits_2/RaggedFromRowSplits/RowPartitionFromRowSplits/Const:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_true_7180(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_false_7550(%arg0: tensor, %arg1: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to from_row_splits do not form a valid RaggedTensor:monotonic"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x >= 0 did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits_2/RaggedFromRowSplits/RowPartitionFromRowSplits/sub:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor) -> () + %3 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %4 = "tf.Identity"(%3) {device = ""} : (tensor) -> tensor + func.return %4 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_true_7540(%arg0: tensor, %arg1: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_false_7890(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to _from_row_partition do not form a valid RaggedTensor"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits_2/RaggedFromRowSplits/strided_slice_1:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (WhitespaceTokenize/WhitespaceTokenize/RaggedFromNestedRowSplits_2/RaggedFromRowSplits/strided_slice:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedFromNestedRowSplits_2_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_true_7880(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_AssertGuard_false_8690(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_AssertGuard_true_8680(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_1_AssertGuard_false_9760(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_1_AssertGuard_true_9750(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_2_AssertGuard_false_10250(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<1> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_2_AssertGuard_true_10240(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_3_AssertGuard_false_10610(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<1> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_Assert_3_AssertGuard_true_10600(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_Assert_AssertGuard_false_15310(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_Assert_AssertGuard_true_15300(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_Assert_1_AssertGuard_false_16380(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_Assert_1_AssertGuard_true_16370(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_Assert_2_AssertGuard_false_16870(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<1> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_Assert_2_AssertGuard_true_16860(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_Assert_3_AssertGuard_false_17230(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<1> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_Assert_3_AssertGuard_true_17220(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_AssertGuard_false_21910(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_AssertGuard_true_21900(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_1_AssertGuard_false_22980(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_1_AssertGuard_true_22970(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_2_AssertGuard_false_23470(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<1> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_2_AssertGuard_true_23460(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_3_AssertGuard_false_23830(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Unable to broadcast: dimension size mismatch in dimension"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<1> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"lengths="> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"dim_size="> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 10 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func @WhitespaceTokenize_WhitespaceTokenize_RaggedGather_1_Assert_3_AssertGuard_true_23820(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} + +// CHECK: func private @whitespace_tokenizer_rank0(%arg0: tensor {tf._user_specified_name = "input"}) -> tensor attributes {tf._implements = #tf_type.func<@"tftext:WhitespaceTokenizer", {}>, tf._input_shapes = [#tf_type.shape<>], tf.signature.is_stateful} { +// CHECK: %0 = "tfl.custom"(%arg0) <{custom_code = "tftext:WhitespaceTokenizer", custom_option = #tfl}> : (tensor) -> tensor +// CHECK: return %0 : tensor + +func.func @ngrams(%arg0: tensor {tf._user_specified_name = "input"}) -> tensor attributes {tf._input_shapes = [#tf_type.shape], tf._implements = #tf_type.func<@"tftext:Ngrams", {axis = -1 : i64, reduction_type = "STRING_JOIN", string_separator = " ", width = 2 : i64}>} { + %0 = "tf.Const"() {value = dense<-1> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<[0, -1]> : tensor<2xi32>} : () -> tensor<2xi32> + %2 = "tf.Const"() {value = dense<[0, 1]> : tensor<2xi32>} : () -> tensor<2xi32> + %3 = "tf.Const"() {value = dense<0> : tensor<2xi32>} : () -> tensor<2xi32> + %4 = "tf.Const"() {value = dense<1> : tensor<2xi32>} : () -> tensor<2xi32> + %5 = "tf.StridedSlice"(%arg0, %3, %1, %4) {begin_mask = 0 : i64, device = "", ellipsis_mask = 1 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<2xi32>, tensor<2xi32>, tensor<2xi32>) -> tensor + %6 = "tf.StridedSlice"(%arg0, %2, %3, %4) {begin_mask = 0 : i64, device = "", ellipsis_mask = 1 : i64, end_mask = 2 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<2xi32>, tensor<2xi32>, tensor<2xi32>) -> tensor + %7 = "tf.Pack"(%5, %6) {axis = -1 : i64, device = ""} : (tensor, tensor) -> tensor + %8 = "tf.ReduceJoin"(%7, %0) {device = "", keep_dims = false, separator = " "} : (tensor, tensor) -> tensor + %9 = "tf.Identity"(%8) {device = ""} : (tensor) -> tensor + func.return %9 : tensor +} + +// CHECK: func @ngrams(%arg0: tensor {tf._user_specified_name = "input"}) -> tensor attributes {tf._implements = #tf_type.func<@"tftext:Ngrams", {axis = -1 : i64, reduction_type = "STRING_JOIN", string_separator = " ", width = 2 : i64}>, tf._input_shapes = [#tf_type.shape]} { +// CHECK: %0 = "tfl.custom"(%arg0) <{custom_code = "tftext:Ngrams", custom_option = #tfl}> : (tensor) -> tensor +// CHECK: return %0 : tensor +// CHECK: } + +func.func private @ngrams_ragged_rank_2(%arg0: tensor {tf._user_specified_name = "values"}, %arg1: tensor<3xi64> {tf._user_specified_name = "args_0"}, %arg2: tensor {tf._user_specified_name = "args_1"}) -> (tensor, tensor<3xi64>, tensor) attributes {tf._implements = #tf_type.func<@"tftext:Ngrams", {axis = -1 : i64, reduction_type = "STRING_JOIN", string_separator = "", width = 2 : i64}>, tf._input_shapes = [#tf_type.shape, #tf_type.shape<3>, #tf_type.shape], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<-1> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<-1> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<1> : tensor} : () -> tensor + %4 = "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %5 = "tf.Const"() {value = dense<> : tensor<0xi32>} : () -> tensor<0xi32> + %6 = "tf.Const"() {value = dense<-1> : tensor<1xi32>} : () -> tensor<1xi32> + %7 = "tf.Const"() {value = dense<0> : tensor<1xi32>} : () -> tensor<1xi32> + %8 = "tf.Const"() {value = dense<1> : tensor<1xi32>} : () -> tensor<1xi32> + %9 = "tf.StridedSlice"(%arg1, %7, %8, %8) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<3xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %10 = "tf.Equal"(%9, %4) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %11 = "tf.All"(%10, %5) {device = "", keep_dims = false} : (tensor, tensor<0xi32>) -> tensor + %12 = "tf.StridedSlice"(%arg1, %8, %7, %8) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<3xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<2xi64> + %13 = "tf.StridedSlice"(%arg1, %7, %6, %8) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor<3xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<2xi64> + %14 = "tf.Sub"(%12, %13) {device = ""} : (tensor<2xi64>, tensor<2xi64>) -> tensor<2xi64> + %15 = "tf.LessEqual"(%4, %14) {device = ""} : (tensor, tensor<2xi64>) -> tensor<2xi1> + %16 = "tf.All"(%15, %7) {device = "", keep_dims = false} : (tensor<2xi1>, tensor<1xi32>) -> tensor + %17 = "tf.StridedSlice"(%arg2, %7, %8, %8) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %18 = "tf.Equal"(%17, %4) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %19 = "tf.All"(%18, %5) {device = "", keep_dims = false} : (tensor, tensor<0xi32>) -> tensor + %20 = "tf.IfRegion"(%19) ({ + %72 = "func.call"(%19, %17, %4) {callee = @RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_true_27770} : (tensor, tensor, tensor) -> tensor + "tf.Yield"(%72) : (tensor) -> () + }, { + %72 = "func.call"(%19, %17, %4) {callee = @RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_false_27780} : (tensor, tensor, tensor) -> tensor + "tf.Yield"(%72) : (tensor) -> () + }) {is_stateless = false} : (tensor) -> tensor + %21 = "tf.Identity"(%20) {device = ""} : (tensor) -> tensor + %22 = "tf.StridedSlice"(%arg2, %8, %7, %8) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %23 = "tf.StridedSlice"(%arg2, %7, %6, %8) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %24 = "tf.Sub"(%22, %23) {device = ""} : (tensor, tensor) -> tensor + %25 = "tf.LessEqual"(%4, %24) {device = ""} : (tensor, tensor) -> tensor + %26 = "tf.All"(%25, %7) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %27 = "tf.IfRegion"(%26) ({ + %72 = "func.call"(%26, %24) {callee = @RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_true_28130} : (tensor, tensor) -> tensor + "tf.Yield"(%72) : (tensor) -> () + }, { + %72 = "func.call"(%26, %24) {callee = @RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_false_28140} : (tensor, tensor) -> tensor + "tf.Yield"(%72) : (tensor) -> () + }) {is_stateless = false} : (tensor) -> tensor + %28 = "tf.Identity"(%27) {device = ""} : (tensor) -> tensor + %29 = "tf.Identity"(%arg2) {_class = ["loc:@args_1"], device = ""} : (tensor) -> tensor + %30 = "tf.StridedSlice"(%29, %6, %7, %8) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %31 = "tf.Shape"(%arg0) {device = ""} : (tensor) -> tensor<1xi64> + %32 = "tf.StridedSlice"(%31, %7, %8, %8) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %33 = "tf.Equal"(%30, %32) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %34 = "tf.All"(%33, %5) {device = "", keep_dims = false} : (tensor, tensor<0xi32>) -> tensor + %35 = "tf.IfRegion"(%34) ({ + %72 = "func.call"(%34, %30, %32) {callee = @RaggedFromNestedRowSplits_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_true_28500} : (tensor, tensor, tensor) -> tensor + "tf.Yield"(%72) : (tensor) -> () + }, { + %72 = "func.call"(%34, %30, %32) {callee = @RaggedFromNestedRowSplits_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_false_28510} : (tensor, tensor, tensor) -> tensor + "tf.Yield"(%72) : (tensor) -> () + }) {is_stateless = false} : (tensor) -> tensor + %36 = "tf.Identity"(%35) {device = ""} : (tensor) -> tensor + %37 = "tf.Identity"(%29) {_class = ["loc:@args_1"], device = ""} : (tensor) -> tensor + %38 = "tf.StridedSlice"(%37, %7, %6, %8) {begin_mask = 1 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %39 = "tf.StridedSlice"(%37, %8, %7, %8) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 1 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64} : (tensor, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %40 = "tf.Minimum"(%38, %39) {device = ""} : (tensor, tensor) -> tensor + %41 = "tf.AddV2"(%39, %1) {device = ""} : (tensor, tensor) -> tensor + %42 = "tf.Maximum"(%41, %38) {device = ""} : (tensor, tensor) -> tensor + %43:2 = "tf.RaggedRange"(%40, %42, %3) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %44 = "tf.GatherV2"(%arg0, %43#1, %2) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %45 = "tf.AddV2"(%38, %3) {device = ""} : (tensor, tensor) -> tensor + %46 = "tf.Minimum"(%45, %39) {device = ""} : (tensor, tensor) -> tensor + %47:2 = "tf.RaggedRange"(%46, %39, %3) {T = i64, Tsplits = i64, device = ""} : (tensor, tensor, tensor) -> (tensor, tensor) + %48 = "tf.Equal"(%43#0, %47#0) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %49 = "tf.All"(%48, %7) {device = "", keep_dims = false} : (tensor, tensor<1xi32>) -> tensor + %50 = "tf.GatherV2"(%arg0, %47#1, %2) {batch_dims = 0 : i64, device = ""} : (tensor, tensor, tensor) -> tensor + %51 = "tf.Shape"(%37) {device = ""} : (tensor) -> tensor<1xi64> + %52 = "tf.StridedSlice"(%51, %7, %8, %8) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<1xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %53 = "tf.Sub"(%52, %3) {device = ""} : (tensor, tensor) -> tensor + %54 = "tf.IfRegion"(%11) ({ + %72 = "func.call"(%11, %9, %4) {callee = @RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_true_28900} : (tensor, tensor, tensor) -> tensor + "tf.Yield"(%72) : (tensor) -> () + }, { + %72 = "func.call"(%11, %9, %4) {callee = @RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_false_28910} : (tensor, tensor, tensor) -> tensor + "tf.Yield"(%72) : (tensor) -> () + }) {is_stateless = false} : (tensor) -> tensor + %55 = "tf.Identity"(%54) {device = ""} : (tensor) -> tensor + %56 = "tf.IfRegion"(%16) ({ + %72 = "func.call"(%16, %14) {callee = @RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_true_29260} : (tensor, tensor<2xi64>) -> tensor + "tf.Yield"(%72) : (tensor) -> () + }, { + %72 = "func.call"(%16, %14) {callee = @RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_false_29270} : (tensor, tensor<2xi64>) -> tensor + "tf.Yield"(%72) : (tensor) -> () + }) {is_stateless = false} : (tensor) -> tensor + %57 = "tf.Identity"(%56) {device = ""} : (tensor) -> tensor + %58 = "tf.Identity"(%arg1) {_class = ["loc:@args_0"], device = ""} : (tensor<3xi64>) -> tensor<3xi64> + %59 = "tf.StridedSlice"(%58, %6, %7, %8) {begin_mask = 0 : i64, device = "", ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64} : (tensor<3xi64>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor + %60 = "tf.Equal"(%59, %53) {device = "", incompatible_shape_error = true} : (tensor, tensor) -> tensor + %61 = "tf.All"(%60, %5) {device = "", keep_dims = false} : (tensor, tensor<0xi32>) -> tensor + %62 = "tf.IfRegion"(%61) ({ + %72 = "func.call"(%61, %59, %53) {callee = @RaggedFromNestedRowSplits_RaggedFromRowSplits_1_assert_equal_1_Assert_AssertGuard_true_29650} : (tensor, tensor, tensor) -> tensor + "tf.Yield"(%72) : (tensor) -> () + }, { + %72 = "func.call"(%61, %59, %53) {callee = @RaggedFromNestedRowSplits_RaggedFromRowSplits_1_assert_equal_1_Assert_AssertGuard_false_29660} : (tensor, tensor, tensor) -> tensor + "tf.Yield"(%72) : (tensor) -> () + }) {is_stateless = false} : (tensor) -> tensor + %63 = "tf.IfRegion"(%49) ({ + %72 = "func.call"(%49, %43#0, %47#0) {callee = @NGrams_SlidingWindow_RaggedConcat_assert_equal_2_Assert_AssertGuard_true_30330} : (tensor, tensor, tensor) -> tensor + "tf.Yield"(%72) : (tensor) -> () + }, { + %72 = "func.call"(%49, %43#0, %47#0) {callee = @NGrams_SlidingWindow_RaggedConcat_assert_equal_2_Assert_AssertGuard_false_30340} : (tensor, tensor, tensor) -> tensor + "tf.Yield"(%72) : (tensor) -> () + }) {is_stateless = false} : (tensor) -> tensor + %64 = "tf.Identity"(%43#0) {device = ""} : (tensor) -> tensor + %65 = "tf.Identity"(%63) {device = ""} : (tensor) -> tensor + %66 = "tf.Pack"(%44, %50) {axis = 1 : i64, device = ""} : (tensor, tensor) -> tensor + %67 = "tf.ReduceJoin"(%66, %0) {device = "", keep_dims = false, separator = ""} : (tensor, tensor) -> tensor + %68 = "tf.Identity"(%67) {device = ""} : (tensor) -> tensor + %69 = "tf.Identity"(%62) {device = ""} : (tensor) -> tensor + %70 = "tf.Identity"(%58) {_class = ["loc:@args_0"], device = ""} : (tensor<3xi64>) -> tensor<3xi64> + %71 = "tf.Identity"(%70) {device = ""} : (tensor<3xi64>) -> tensor<3xi64> + func.return %68, %71, %64 : tensor, tensor<3xi64>, tensor +} +func.func private @RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_true_27770(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func private @RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_false_27780(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to from_row_splits do not form a valid RaggedTensor:zero"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (RaggedFromNestedRowSplits/RaggedFromRowSplits/RowPartitionFromRowSplits/strided_slice:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (RaggedFromNestedRowSplits/RaggedFromRowSplits/RowPartitionFromRowSplits/Const:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func private @RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_true_28130(%arg0: tensor, %arg1: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func private @RaggedFromNestedRowSplits_RaggedFromRowSplits_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_false_28140(%arg0: tensor, %arg1: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to from_row_splits do not form a valid RaggedTensor:monotonic"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x >= 0 did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (RaggedFromNestedRowSplits/RaggedFromRowSplits/RowPartitionFromRowSplits/sub:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor) -> () + %3 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %4 = "tf.Identity"(%3) {device = ""} : (tensor) -> tensor + func.return %4 : tensor +} +func.func private @RaggedFromNestedRowSplits_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_true_28500(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func private @RaggedFromNestedRowSplits_RaggedFromRowSplits_assert_equal_1_Assert_AssertGuard_false_28510(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to _from_row_partition do not form a valid RaggedTensor"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (RaggedFromNestedRowSplits/RaggedFromRowSplits/strided_slice_1:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (RaggedFromNestedRowSplits/RaggedFromRowSplits/strided_slice:0) = "> : tensor} : () -> tensor +"tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func private @RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_true_28900(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func private @RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_equal_1_Assert_AssertGuard_false_28910(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to from_row_splits do not form a valid RaggedTensor:zero"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (RaggedFromNestedRowSplits/RaggedFromRowSplits_1/RowPartitionFromRowSplits/strided_slice:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (RaggedFromNestedRowSplits/RaggedFromRowSplits_1/RowPartitionFromRowSplits/Const:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func private @RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_true_29260(%arg0: tensor, %arg1: tensor<2xi64>) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<2>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func private @RaggedFromNestedRowSplits_RaggedFromRowSplits_1_RowPartitionFromRowSplits_assert_non_negative_assert_less_equal_Assert_AssertGuard_false_29270(%arg0: tensor, %arg1: tensor<2xi64>) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<2>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to from_row_splits do not form a valid RaggedTensor:monotonic"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x >= 0 did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (RaggedFromNestedRowSplits/RaggedFromRowSplits_1/RowPartitionFromRowSplits/sub:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor<2xi64>) -> () + %3 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %4 = "tf.Identity"(%3) {device = ""} : (tensor) -> tensor + func.return %4 : tensor +} +func.func private @RaggedFromNestedRowSplits_RaggedFromRowSplits_1_assert_equal_1_Assert_AssertGuard_true_29650(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func private @RaggedFromNestedRowSplits_RaggedFromRowSplits_1_assert_equal_1_Assert_AssertGuard_false_29660(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape<>, #tf_type.shape<>], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Arguments to _from_row_partition do not form a valid RaggedTensor"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (RaggedFromNestedRowSplits/RaggedFromRowSplits_1/strided_slice:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (RaggedFromNestedRowSplits/RaggedFromRowSplits_1/RaggedNRows/sub:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +func.func private @NGrams_SlidingWindow_RaggedConcat_assert_equal_2_Assert_AssertGuard_true_30330(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape, #tf_type.shape]} { + %0 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %1 = "tf.Identity"(%0) {device = ""} : (tensor) -> tensor + func.return %1 : tensor +} +func.func private @NGrams_SlidingWindow_RaggedConcat_assert_equal_2_Assert_AssertGuard_false_30340(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor attributes {tf._input_shapes = [#tf_type.shape<>, #tf_type.shape, #tf_type.shape], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<"Inputs must have identical ragged splits"> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<"Condition x == y did not hold element-wise:"> : tensor} : () -> tensor + %2 = "tf.Const"() {value = dense<"x (NGrams/SlidingWindow/RaggedGetItem/RaggedRange:0) = "> : tensor} : () -> tensor + %3 = "tf.Const"() {value = dense<"y (NGrams/SlidingWindow/RaggedGetItem_1/RaggedRange:0) = "> : tensor} : () -> tensor + "tf.Assert"(%arg0, %0, %1, %2, %arg1, %3, %arg2) {device = "", summarize = 3 : i64} : (tensor, tensor, tensor, tensor, tensor, tensor, tensor) -> () + %4 = "tf.Identity"(%arg0) {device = ""} : (tensor) -> tensor + %5 = "tf.Identity"(%4) {device = ""} : (tensor) -> tensor + func.return %5 : tensor +} +// CHECK: func private @ngrams_ragged_rank_2(%arg0: tensor {tf._user_specified_name = "values"}, %arg1: tensor<3xi64> {tf._user_specified_name = "args_0"}, %arg2: tensor {tf._user_specified_name = "args_1"}) -> (tensor, tensor<3xi64>, tensor) attributes {tf._implements = #tf_type.func<@"tftext:Ngrams", {axis = -1 : i64, reduction_type = "STRING_JOIN", string_separator = "", width = 2 : i64}>, tf._input_shapes = [#tf_type.shape, #tf_type.shape<3>, #tf_type.shape], tf.signature.is_stateful} { +// CHECK: %0:3 = "tfl.custom"(%arg0, %arg1, %arg2) <{custom_code = "tftext:Ngrams", custom_option = #tfl}> : (tensor, tensor<3xi64>, tensor) -> (tensor, tensor<3xi64>, tensor) +// CHECK: return %0#0, %0#1, %0#2 : tensor, tensor<3xi64>, tensor + + +func.func private @sgnn_projection(%arg0: tensor {tf._user_specified_name = "values"}, %arg1: tensor {tf._user_specified_name = "row_splits"}) -> tensor attributes {tf._implements = #tf_type.func<@"tftext:custom:SgnnProjection", {buckets = 2147483647 : i64, hash_seed = [1902835825, -1475704015, 473120514, 1254202069, 1558833093, 1756181982, 1906603252, -1034142694, 542842690, 535515822]}>, tf._input_shapes = [#tf_type.shape, #tf_type.shape], tf.signature.is_stateful} { + %0 = "tf.Const"() {value = dense<[[1902835825], [-1475704015], [473120514], [1254202069], [1558833093], [1756181982], [1906603252], [-1034142694], [542842690], [535515822]]> : tensor<10x1xi64>} : () -> tensor<10x1xi64> + %1 = "tf.StringToHashBucketFast"(%arg0) {device = "", num_buckets = 2147483647 : i64} : (tensor) -> tensor + %2 = "tf.Sgnn"(%1, %0) {device = ""} : (tensor, tensor<10x1xi64>) -> tensor<10x?xf64> + %3 = "tf.Const"() {value = dense<[-1, 10]> : tensor<2xi64>} : () -> tensor<2xi64> + %4 = "tf.Reshape"(%2, %3) : (tensor<10x?xf64>, tensor<2xi64>) -> tensor + func.return %4 : tensor +} + + +// CHECK: func private @sgnn_projection(%arg0: tensor {tf._user_specified_name = "values"}, %arg1: tensor {tf._user_specified_name = "row_splits"}) -> tensor attributes {tf._implements = #tf_type.func<@"tftext:custom:SgnnProjection", {buckets = 2147483647 : i64, hash_seed = [1902835825, -1475704015, 473120514, 1254202069, 1558833093, 1756181982, 1906603252, -1034142694, 542842690, 535515822]}>, tf._input_shapes = [#tf_type.shape, #tf_type.shape], tf.signature.is_stateful} { +// CHECK: %0 = "tfl.custom"(%arg0, %arg1) <{custom_code = "tftext:custom:SgnnProjection", custom_option = #tfl}> : (tensor, tensor) -> tensor +// CHECK: return %0 : tensor diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/get-arithmetic-count.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/get-arithmetic-count.mlir new file mode 100644 index 0000000000000000000000000000000000000000..097bc368562e6c01f2afe3101946a8ff1faea1cf --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/get-arithmetic-count.mlir @@ -0,0 +1,133 @@ +// RUN: litert-opt -split-input-file -verify-diagnostics -tfl-get-arithmetic-count %s | FileCheck %s + +func.func @testConv2D(tensor<256x32x32x3xf32>, tensor<16x3x3x3xf32>, tensor<16xf32>) -> tensor<256x32x32x16xf32> { +^bb0(%arg0: tensor<256x32x32x3xf32>, %arg1: tensor<16x3x3x3xf32>, %arg2: tensor<16xf32>): + // CHECK: _arithmetic_count = 230686720 : i64 + %0 = "tfl.conv_2d"(%arg0, %arg1, %arg2) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32, fused_activation_function = "RELU6"} : (tensor<256x32x32x3xf32>, tensor<16x3x3x3xf32>, tensor<16xf32>) -> tensor<256x32x32x16xf32> + func.return %0 : tensor<256x32x32x16xf32> +} + +func.func @testConv2DDynamicShape(tensor, tensor<16x3x3x3xf32>, tensor<16xf32>) -> tensor { +^bb0(%arg0: tensor, %arg1: tensor<16x3x3x3xf32>, %arg2: tensor<16xf32>): + // CHECK: _arithmetic_count = -1 : i64 + %0 = "tfl.conv_2d"(%arg0, %arg1, %arg2) {dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32, fused_activation_function = "RELU6"} : (tensor, tensor<16x3x3x3xf32>, tensor<16xf32>) -> tensor + func.return %0 : tensor +} + +func.func @testDepthwiseConv2D(tensor<1x112x112x3xf32>, tensor<1x3x3x32xf32>, tensor<32xf32>) -> tensor<1x112x112x32xf32> { +^bb0(%arg0: tensor<1x112x112x3xf32>, %arg1: tensor<1x3x3x32xf32>, %arg2: tensor<32xf32>): + // CHECK: _arithmetic_count = 7626752 : i64 + %0 = "tfl.depthwise_conv_2d"(%arg0, %arg1, %arg2) {depth_multiplier = 1 : i32, dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32, fused_activation_function = "RELU6"} : (tensor<1x112x112x3xf32>, tensor<1x3x3x32xf32>, tensor<32xf32>) -> tensor<1x112x112x32xf32> + func.return %0 : tensor<1x112x112x32xf32> +} + +func.func @fully_connected(%arg0: tensor<1x37xf32>, %arg1: tensor<40x37xf32>, %arg2: tensor<40xf32>) -> tensor<1x40xf32> { + // CHECK: _arithmetic_count = 3000 : i64 + %0 = "tfl.fully_connected"(%arg0, %arg1, %arg2) {fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"} : (tensor<1x37xf32>, tensor<40x37xf32>, tensor<40xf32>) -> tensor<1x40xf32> + func.return %0 : tensor<1x40xf32> +} + +func.func @testAdd(%arg0: tensor<10x10x10xf32>, %arg1: tensor<10x10x10xf32>) -> tensor<10x10x10xf32> { + // CHECK: _arithmetic_count = 1000 : i64 + %0 = "tfl.add"(%arg0, %arg1) {fused_activation_function = "NONE"} : (tensor<10x10x10xf32>, tensor<10x10x10xf32>) -> tensor<10x10x10xf32> + func.return %0 : tensor<10x10x10xf32> +} + +func.func @testAddBroadcast(%arg0: tensor<10x10x10xf32>, %arg1: tensor<10xf32>) -> tensor<10x10x10xf32> { + // CHECK: _arithmetic_count = 1000 : i64 + %0 = "tfl.add"(%arg0, %arg1) {fused_activation_function = "NONE"} : (tensor<10x10x10xf32>, tensor<10xf32>) -> tensor<10x10x10xf32> + func.return %0 : tensor<10x10x10xf32> +} + +func.func @testSub(%arg0: tensor<10x10x10xf32>, %arg1: tensor<10x10x10xf32>) -> tensor<10x10x10xf32> { + // CHECK: _arithmetic_count = 1000 : i64 + %0 = "tfl.sub"(%arg0, %arg1) {fused_activation_function = "NONE"} : (tensor<10x10x10xf32>, tensor<10x10x10xf32>) -> tensor<10x10x10xf32> + func.return %0 : tensor<10x10x10xf32> +} + +func.func @testSubBroadcast(%arg0: tensor<10x10x10xf32>, %arg1: tensor<10xf32>) -> tensor<10x10x10xf32> { + // CHECK: _arithmetic_count = 1000 : i64 + %0 = "tfl.sub"(%arg0, %arg1) {fused_activation_function = "NONE"} : (tensor<10x10x10xf32>, tensor<10xf32>) -> tensor<10x10x10xf32> + func.return %0 : tensor<10x10x10xf32> +} + +func.func @testMul(%arg0: tensor<10x10x10xf32>, %arg1: tensor<10x10x10xf32>) -> tensor<10x10x10xf32> { + // CHECK: _arithmetic_count = 1000 : i64 + %0 = "tfl.mul"(%arg0, %arg1) {fused_activation_function = "NONE"} : (tensor<10x10x10xf32>, tensor<10x10x10xf32>) -> tensor<10x10x10xf32> + func.return %0 : tensor<10x10x10xf32> +} + +func.func @testMulBroadcast(%arg0: tensor<10x10x10xf32>, %arg1: tensor<10xf32>) -> tensor<10x10x10xf32> { + // CHECK: _arithmetic_count = 1000 : i64 + %0 = "tfl.mul"(%arg0, %arg1) {fused_activation_function = "NONE"} : (tensor<10x10x10xf32>, tensor<10xf32>) -> tensor<10x10x10xf32> + func.return %0 : tensor<10x10x10xf32> +} + +func.func @testDiv(%arg0: tensor<10x10x10xf32>, %arg1: tensor<10x10x10xf32>) -> tensor<10x10x10xf32> { + // CHECK: _arithmetic_count = 1000 : i64 + %0 = "tfl.div"(%arg0, %arg1) {fused_activation_function = "NONE"} : (tensor<10x10x10xf32>, tensor<10x10x10xf32>) -> tensor<10x10x10xf32> + func.return %0 : tensor<10x10x10xf32> +} + +func.func @testDivBroadcast(%arg0: tensor<10x10x10xf32>, %arg1: tensor<10xf32>) -> tensor<10x10x10xf32> { + // CHECK: _arithmetic_count = 1000 : i64 + %0 = "tfl.div"(%arg0, %arg1) {fused_activation_function = "NONE"} : (tensor<10x10x10xf32>, tensor<10xf32>) -> tensor<10x10x10xf32> + func.return %0 : tensor<10x10x10xf32> +} + +func.func @testLogistic(%arg0: tensor<10x10xf32>) -> tensor<10x10xf32> { + // CHECK: _arithmetic_count = 6400 : i64 + %0 = "tfl.logistic"(%arg0) : (tensor<10x10xf32>) -> tensor<10x10xf32> + func.return %0 : tensor<10x10xf32> +} + +func.func @testLogSoftmax(%arg0: tensor<10x10xf32>) -> tensor<10x10xf32> { + // CHECK: _arithmetic_count = 6400 : i64 + %0 = "tfl.log_softmax"(%arg0) : (tensor<10x10xf32>) -> tensor<10x10xf32> + func.return %0 : tensor<10x10xf32> +} + +func.func @testSoftmax(%arg0: tensor<10x10xf32>) -> tensor<10x10xf32> { + // CHECK: _arithmetic_count = 6400 : i64 + %0 = "tfl.softmax"(%arg0) {beta = 1.000000e+00 : f32} : (tensor<10x10xf32>) -> tensor<10x10xf32> + func.return %0 : tensor<10x10xf32> +} + +func.func @testTanh(%arg0: tensor<10x10xf32>) -> tensor<10x10xf32> { + // CHECK: _arithmetic_count = 6400 : i64 + %0 = "tfl.tanh"(%arg0) : (tensor<10x10xf32>) -> tensor<10x10xf32> + func.return %0 : tensor<10x10xf32> +} + +func.func @testAddN(%arg0: tensor<10x10xf32>, %arg1: tensor<10x10xf32>, %arg2: tensor<10x10xf32>) -> tensor<10x10xf32> { + // CHECK: _arithmetic_count = 200 : i64 + %0 = "tfl.add_n"(%arg0, %arg1, %arg2) {fused_activation_function = "NONE"} : (tensor<10x10xf32>, tensor<10x10xf32>, tensor<10x10xf32>) -> tensor<10x10xf32> + func.return %0 : tensor<10x10xf32> +} + +func.func @testMaxPool2D(tensor<1x10x10x3xf32>) -> tensor<1x10x10x3xf32> { +^bb0(%arg0: tensor<1x10x10x3xf32>): + // CHECK: _arithmetic_count = 2700 : i64 + %0 = "tfl.max_pool_2d"(%arg0) {filter_height = 3 : i32, filter_width = 3 : i32, fused_activation_function = "RELU6", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32} : (tensor<1x10x10x3xf32>) -> tensor<1x10x10x3xf32> + func.return %0 : tensor<1x10x10x3xf32> +} + +func.func @testAveragePool2D(tensor<1x10x10x3xf32>) -> tensor<1x10x10x3xf32> { +^bb0(%arg0: tensor<1x10x10x3xf32>): + // CHECK: _arithmetic_count = 2700 : i64 + %0 = "tfl.average_pool_2d"(%arg0) {filter_height = 3 : i32, filter_width = 3 : i32, fused_activation_function = "RELU6", padding = "SAME", stride_h = 1 : i32, stride_w = 1 : i32} : (tensor<1x10x10x3xf32>) -> tensor<1x10x10x3xf32> + func.return %0 : tensor<1x10x10x3xf32> +} + +func.func @testTransposeConv(%arg0: tensor<4xi32>, %arg1: tensor<32x4x4x128xf32>, %arg2: tensor<1x32x42x128xf32>) -> tensor<1x64x84x32xf32> { + %cst = "tfl.no_value"() {value = unit} : () -> none + // CHECK: _arithmetic_count = 176160768 : i64 + %0 = "tfl.transpose_conv"(%arg0, %arg1, %arg2, %cst) {padding = "SAME", stride_h = 2 : i32, stride_w = 2 : i32, fused_activation_function = "NONE"} : (tensor<4xi32>, tensor<32x4x4x128xf32>, tensor<1x32x42x128xf32>, none) -> tensor<1x64x84x32xf32> + func.return %0 : tensor<1x64x84x32xf32> +} + +func.func @testL2Norm(%arg0: tensor<10x10xf32>) -> tensor<10x10xf32> { + // CHECK: _arithmetic_count = 300 : i64 + %0 = "tfl.l2_normalization"(%arg0) {fused_activation_function = "NONE"} : (tensor<10x10xf32>) -> tensor<10x10xf32> + func.return %0 : tensor<10x10xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/gpu_compatibility.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/gpu_compatibility.mlir new file mode 100644 index 0000000000000000000000000000000000000000..c0f24579c9c1839b4d9a36783cc9e2bc25460f26 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/gpu_compatibility.mlir @@ -0,0 +1,126 @@ +// RUN: litert-opt %s -tfl-gpu-compatibility | FileCheck %s + +module { + func.func @test_strided_slice_promotion(%arg0: tensor<1x12xi1>) -> tensor<1x3xi1> { + // CHECK-LABEL: func.func @test_strided_slice_promotion + // CHECK-DAG: %[[C0:.*]] = "tfl.pseudo_const"() <{value = dense<0> : tensor<2xi32>}> + // CHECK-DAG: %[[C1:.*]] = "tfl.pseudo_const"() <{value = dense<[1, 3]> : tensor<2xi32>}> + // CHECK-DAG: %[[C2:.*]] = "tfl.pseudo_const"() <{value = dense<1> : tensor<2xi32>}> + // CHECK: %[[CAST_IN:.*]] = "tfl.cast"(%arg0) : (tensor<1x12xi1>) -> tensor<1x12xf32> + // CHECK: %[[SS:.*]] = "tfl.strided_slice"(%[[CAST_IN]], %[[C0]], %[[C1]], %[[C2]]) + // CHECK-SAME: {{.*}} (tensor<1x12xf32>, tensor<2xi32>, tensor<2xi32>, tensor<2xi32>) -> tensor<1x3xf32> + // CHECK: %[[CAST_OUT:.*]] = "tfl.cast"(%[[SS]]) : (tensor<1x3xf32>) -> tensor<1x3xi1> + // CHECK: return %[[CAST_OUT]] + %cst = "tfl.pseudo_const"() <{value = dense<[0, 0]> : tensor<2xi32>}> : () -> tensor<2xi32> + %cst_0 = "tfl.pseudo_const"() <{value = dense<[1, 3]> : tensor<2xi32>}> : () -> tensor<2xi32> + %cst_1 = "tfl.pseudo_const"() <{value = dense<[1, 1]> : tensor<2xi32>}> : () -> tensor<2xi32> + %0 = "tfl.strided_slice"(%arg0, %cst, %cst_0, %cst_1) <{begin_mask = 0 : i32, ellipsis_mask = 0 : i32, end_mask = 0 : i32, new_axis_mask = 0 : i32, offset = false, shrink_axis_mask = 0 : i32}> : (tensor<1x12xi1>, tensor<2xi32>, tensor<2xi32>, tensor<2xi32>) -> tensor<1x3xi1> + func.return %0 : tensor<1x3xi1> + } + + func.func @test_pad_promotion_expansion(%arg0: tensor<1x36xi1>) -> tensor<1x48xi1> { + // CHECK-LABEL: func.func @test_pad_promotion_expansion + // CHECK-DAG: %[[C0:.*]] = "tfl.pseudo_const"() <{value = dense<[1, 48]> : tensor<2xi32>}> + // CHECK-DAG: %[[C1:.*]] = "tfl.pseudo_const"() <{value = {{.*}} : tensor<3x2xi32>}> + // CHECK-DAG: %[[C2:.*]] = "tfl.pseudo_const"() <{value = dense<[1, 1, 36]> : tensor<3xi32>}> + // CHECK: %[[RE_IN:.*]] = "tfl.reshape"(%arg0, %[[C2]]) : (tensor<1x36xi1>, tensor<3xi32>) -> tensor<1x1x36xi1> + // CHECK: %[[CAST_IN:.*]] = "tfl.cast"(%[[RE_IN]]) : (tensor<1x1x36xi1>) -> tensor<1x1x36xf32> + // CHECK: %[[PAD:.*]] = "tfl.pad"(%[[CAST_IN]], %[[C1]]) : (tensor<1x1x36xf32>, tensor<3x2xi32>) -> tensor<1x1x48xf32> + // CHECK: %[[CAST_OUT:.*]] = "tfl.cast"(%[[PAD]]) : (tensor<1x1x48xf32>) -> tensor<1x1x48xi1> + // CHECK: %[[RE_OUT:.*]] = "tfl.reshape"(%[[CAST_OUT]], %[[C0]]) : (tensor<1x1x48xi1>, tensor<2xi32>) -> tensor<1x48xi1> + // CHECK: return %[[RE_OUT]] + %cst = "tfl.pseudo_const"() <{value = dense<[[0, 0], [0, 12]]> : tensor<2x2xi32>}> : () -> tensor<2x2xi32> + %0 = "tfl.pad"(%arg0, %cst) : (tensor<1x36xi1>, tensor<2x2xi32>) -> tensor<1x48xi1> + func.return %0 : tensor<1x48xi1> + } + + func.func @test_batch_matmul_swap(%arg0: tensor<8x48x24xf32>) -> tensor<8x13x24xf32> { + // CHECK-LABEL: func.func @test_batch_matmul_swap + // CHECK-DAG: %[[CST:.*]] = "tfl.pseudo_const"() <{value = dense<1.000000e+00> : tensor<8x13x48xf32>}> : () -> tensor<8x13x48xf32> + // CHECK-DAG: %[[PERM:.*]] = "tfl.pseudo_const"() <{value = dense<[0, 2, 1]> : tensor<3xi32>}> : () -> tensor<3xi32> + // CHECK: %[[BMM:.*]] = "tfl.batch_matmul"(%arg0, %[[CST]]) <{adj_x = true, adj_y = true, asymmetric_quantize_inputs = false}> : (tensor<8x48x24xf32>, tensor<8x13x48xf32>) -> tensor<8x24x13xf32> + // CHECK: %[[RES:.*]] = "tfl.transpose"(%[[BMM]], %[[PERM]]) : (tensor<8x24x13xf32>, tensor<3xi32>) -> tensor<8x13x24xf32> + // CHECK: return %[[RES]] : tensor<8x13x24xf32> + %cst = "tfl.pseudo_const"() <{value = dense<1.0> : tensor<8x13x48xf32>}> : () -> tensor<8x13x48xf32> + %0 = "tfl.batch_matmul"(%cst, %arg0) <{adj_x = false, adj_y = false, asymmetric_quantize_inputs = false}> : (tensor<8x13x48xf32>, tensor<8x48x24xf32>) -> tensor<8x13x24xf32> + func.return %0 : tensor<8x13x24xf32> + } + + func.func @test_broadcast_to(%arg0: tensor<1x2x1x24xi1>, %arg1: tensor<1x1x12x24xi1>) -> tensor<1x2x12x24xi1> { + // CHECK-LABEL: func.func @test_broadcast_to + // CHECK-DAG: %[[MULT2:.*]] = "tfl.pseudo_const"() <{value = dense<[1, 2, 1, 1]> : tensor<4xi64>}> : () -> tensor<4xi64> + // CHECK-DAG: %[[MULT1:.*]] = "tfl.pseudo_const"() <{value = dense<[1, 1, 12, 1]> : tensor<4xi64>}> : () -> tensor<4xi64> + // CHECK: %[[CAST1:.*]] = "tfl.cast"(%arg0) : (tensor<1x2x1x24xi1>) -> tensor<1x2x1x24xi32> + // CHECK: %[[TILE1:.*]] = "tfl.tile"(%[[CAST1]], %[[MULT1]]) : (tensor<1x2x1x24xi32>, tensor<4xi64>) -> tensor<1x2x12x24xi32> + // CHECK: %[[CAST2:.*]] = "tfl.cast"(%[[TILE1]]) : (tensor<1x2x12x24xi32>) -> tensor<1x2x12x24xi1> + // CHECK: %[[CAST3:.*]] = "tfl.cast"(%arg1) : (tensor<1x1x12x24xi1>) -> tensor<1x1x12x24xi32> + // CHECK: %[[TILE2:.*]] = "tfl.tile"(%[[CAST3]], %[[MULT2]]) : (tensor<1x1x12x24xi32>, tensor<4xi64>) -> tensor<1x2x12x24xi32> + // CHECK: %[[CAST4:.*]] = "tfl.cast"(%[[TILE2]]) : (tensor<1x2x12x24xi32>) -> tensor<1x2x12x24xi1> + // CHECK: %[[RES:.*]] = tfl.logical_and %[[CAST2]], %[[CAST4]] : tensor<1x2x12x24xi1> + // CHECK: return %[[RES]] : tensor<1x2x12x24xi1> + %0 = "tfl.pseudo_const"() <{value = dense<[1, 2, 12, 24]> : tensor<4xi64>}> : () -> tensor<4xi64> + %1 = "tfl.broadcast_to"(%arg0, %0) : (tensor<1x2x1x24xi1>, tensor<4xi64>) -> tensor<1x2x12x24xi1> + %2 = "tfl.broadcast_to"(%arg1, %0) : (tensor<1x1x12x24xi1>, tensor<4xi64>) -> tensor<1x2x12x24xi1> + %3 = tfl.logical_and %1, %2 : tensor<1x2x12x24xi1> + func.return %3 : tensor<1x2x12x24xi1> + } + + func.func @test_broadcast_sum_to_mul(%arg0: tensor<25x1x1x1xf32>) -> tensor<25x1x1x1xf32> { + // CHECK-LABEL: func.func @test_broadcast_sum_to_mul + // CHECK: %[[SCALE:.*]] = "tfl.pseudo_const"() <{value = dense<8.192000e+03> : tensor}> : () -> tensor + // CHECK: %[[RES:.*]] = tfl.mul(%arg0, %[[SCALE]]) <{fused_activation_function = "NONE"}> : (tensor<25x1x1x1xf32>, tensor) -> tensor<25x1x1x1xf32> + // CHECK: return %[[RES]] : tensor<25x1x1x1xf32> + %0 = "tfl.pseudo_const"() <{value = dense<[25, 64, 1, 128]> : tensor<4xi64>}> : () -> tensor<4xi64> + %1 = "tfl.broadcast_to"(%arg0, %0) : (tensor<25x1x1x1xf32>, tensor<4xi64>) -> tensor<25x64x1x128xf32> + %2 = "tfl.pseudo_const"() <{value = dense<[1, 3]> : tensor<2xi32>}> : () -> tensor<2xi32> + %3 = "tfl.sum"(%1, %2) <{keep_dims = true}> : (tensor<25x64x1x128xf32>, tensor<2xi32>) -> tensor<25x1x1x1xf32> + func.return %3 : tensor<25x1x1x1xf32> + } + + func.func @test_fuse_dequantize_fc(%arg0: tensor<1x16x384xf32>, %arg1: tensor<1536x384x!quant.uniform>) -> tensor<1x16x1536xf32> { + // CHECK-LABEL: func.func @test_fuse_dequantize_fc + // CHECK: %[[CST:.*]] = "tfl.no_value"() + // CHECK: %[[FC:.*]] = "tfl.fully_connected"(%arg0, %arg1, %[[CST]]) + // CHECK-SAME: asymmetric_quantize_inputs = false, fused_activation_function = "NONE", keep_num_dims = true, weights_format = "DEFAULT" + // CHECK-NEXT: return %[[FC]] + %cst = "tfl.no_value"() {value} : () -> none + %0 = "tfl.dequantize"(%arg1) : (tensor<1536x384x!quant.uniform>) -> tensor<1536x384xf32> + %1 = "tfl.fully_connected"(%arg0, %0, %cst) <{asymmetric_quantize_inputs = false, fused_activation_function = "NONE", keep_num_dims = true, weights_format = "DEFAULT"}> : (tensor<1x16x384xf32>, tensor<1536x384xf32>, none) -> tensor<1x16x1536xf32> + return %1 : tensor<1x16x1536xf32> + } + + func.func @test_swap_add(%arg0: tensor, %arg1: tensor<25x1x1x1xf32>) -> tensor<25x1x1x1xf32> { + // CHECK-LABEL: func.func @test_swap_add + // CHECK: %[[CST:.*]] = "tfl.pseudo_const"() <{value = dense<1> : tensor<1xi32>}> : () -> tensor<1xi32> + // CHECK: %[[RESHAPE:.*]] = "tfl.reshape"(%arg0, %[[CST]]) : (tensor, tensor<1xi32>) -> tensor<1xf32> + // CHECK: tfl.add(%arg1, %[[RESHAPE]]) <{fused_activation_function = "NONE"}> : (tensor<25x1x1x1xf32>, tensor<1xf32>) -> tensor<25x1x1x1xf32> + %0 = "tfl.add"(%arg0, %arg1) <{fused_activation_function = "NONE"}> : (tensor, tensor<25x1x1x1xf32>) -> tensor<25x1x1x1xf32> + func.return %0 : tensor<25x1x1x1xf32> + } + + func.func @test_tile_non_splat(%arg0: tensor<1x1x12x24xi1>) -> (tensor<1x2x12x24xi1>, tensor<1x2x2xi32>) { + // CHECK-LABEL: func.func @test_tile_non_splat + // CHECK: %[[CST0:.*]] = arith.constant dense : tensor<1x2x12x24xi1> + // CHECK: %[[CST1:.*]] = "tfl.pseudo_const"() <{value = dense<{{.*}}> : tensor<1x2x2xi32>}> + // CHECK: return %[[CST0]], %[[CST1]] + %cst = "tfl.pseudo_const"() <{value = dense : tensor<1x1x12x24xi1>}> : () -> tensor<1x1x12x24xi1> + %multiples = "tfl.pseudo_const"() <{value = dense<[1, 2, 1, 1]> : tensor<4xi64>}> : () -> tensor<4xi64> + %0 = "tfl.tile"(%cst, %multiples) : (tensor<1x1x12x24xi1>, tensor<4xi64>) -> tensor<1x2x12x24xi1> + + %cst_non_splat = "tfl.pseudo_const"() <{value = dense<[[[1, 2], [3, 4]]]> : tensor<1x2x2xi32>}> : () -> tensor<1x2x2xi32> + %multiples_2 = "tfl.pseudo_const"() <{value = dense<[1, 2, 1]> : tensor<3xi64>}> : () -> tensor<3xi64> + %1 = "tfl.tile"(%cst_non_splat, %multiples_2) : (tensor<1x2x2xi32>, tensor<3xi64>) -> tensor<1x2x2xi32> + + return %0, %1 : tensor<1x2x12x24xi1>, tensor<1x2x2xi32> + } + + func.func @tile_bool(%arg0: tensor<1x2x1x24xi1>, %arg1: tensor<4xi64>) -> tensor<1x2x12x24xi1> { + // CHECK-LABEL: tile_bool + // CHECK: %[[CAST_IN:.*]] = "tfl.cast"(%arg0) : (tensor<1x2x1x24xi1>) -> tensor<1x2x1x24xi32> + // CHECK: %[[TILE:.*]] = "tfl.tile"(%[[CAST_IN]], %arg1) : (tensor<1x2x1x24xi32>, tensor<4xi64>) -> tensor<1x2x12x24xi32> + // CHECK: %[[CAST_OUT:.*]] = "tfl.cast"(%[[TILE]]) : (tensor<1x2x12x24xi32>) -> tensor<1x2x12x24xi1> + // CHECK: return %[[CAST_OUT]] + %0 = "tfl.tile"(%arg0, %arg1) : (tensor<1x2x1x24xi1>, tensor<4xi64>) -> tensor<1x2x12x24xi1> + func.return %0 : tensor<1x2x12x24xi1> + } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/guarantee_func_has_one_use.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/guarantee_func_has_one_use.mlir new file mode 100644 index 0000000000000000000000000000000000000000..7c0015b160a4bf6485c03c5c9d9f331087a54452 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/guarantee_func_has_one_use.mlir @@ -0,0 +1,33 @@ +// RUN: litert-opt %s --tf-guarantee-all-funcs-one-use --tf-shape-inference | FileCheck %s + +module attributes {tf.versions = {producer = 888 : i32}} { +// CHECK-LABEL: func @while_main +// CHECK: %0 = "tf.While"(%arg0) +// CHECK-SAME: body = @while_body +// CHECK-SAME: cond = @while_cond +// CHECK: "tf.While"(%arg1) +// CHECK-SAME: body = @while_body_0 +// CHECK-SAME: cond = @while_cond_1 + +// CHECK: func @while_body(%arg0: tensor<256x256xi32>) +// CHECK: func @while_cond(%arg0: tensor<256x256xi32>) +// CHECK: func private @while_body_0(%arg0: tensor<128xi32>) +// CHECK: func private @while_cond_1(%arg0: tensor<128xi32>) +func.func @while_main(%arg0: tensor<256x256xi32>, %arg1: tensor<128xi32>) -> (tensor<256x256xi32>, tensor<128xi32>) { + %0 = "tf.While"(%arg0) {body = @while_body, cond = @while_cond, device = "", is_stateless = true} : (tensor<256x256xi32>) -> (tensor<256x256xi32>) + %1 = "tf.While"(%arg1) {body = @while_body, cond = @while_cond, device = "", is_stateless = true} : (tensor<128xi32>) -> (tensor<128xi32>) + func.return %0, %1: tensor<256x256xi32>, tensor<128xi32> +} + +func.func @while_body(%arg0: tensor<*xi32>) -> (tensor<*xi32>) { + %0 = "tf.Rank"(%arg0) : (tensor<*xi32>) -> tensor + %1 = "tf.Add"(%0, %arg0): (tensor, tensor<*xi32>) -> tensor<*xi32> + func.return %1: tensor<*xi32> +} + +func.func @while_cond(%arg0: tensor<*xi32>) -> tensor<*xi1> { + %cst = arith.constant dense<10> : tensor + %0 = "tf.Less"(%arg0, %cst) {T = i32, device = ""} : (tensor<*xi32>, tensor) -> tensor<*xi1> + func.return %0 : tensor<*xi1> +} +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/inlining.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/inlining.mlir new file mode 100644 index 0000000000000000000000000000000000000000..d6dc03e631143d1d41f363a652a6706df8923701 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/inlining.mlir @@ -0,0 +1,18 @@ +// RUN: litert-opt %s -inline='default-pipeline=''' | FileCheck %s + +// Inline a function that contains only tfl ops. +func.func @func_with_tfl_ops(%arg0 : tensor<2xi32>) -> tensor<2xi32> { + %0 = "tfl.sub"(%arg0, %arg0) {fused_activation_function = "RELU6"} : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi32> + %1 = "tfl.add"(%0, %arg0) {fused_activation_function = "RELU6"} : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi32> + func.return %1: tensor<2xi32> +} + +// CHECK-LABEL: func @inline_with_arg +// CHECK-SAME: [[VAL_0:%.*]]: tensor<2xi32> +func.func @inline_with_arg(%arg0 : tensor<2xi32>) -> tensor<2xi32> { +// CHECK-NEXT: [[VAL_1:%.*]] = tfl.sub [[VAL_0]], [[VAL_0]] {fused_activation_function = "RELU6"} : tensor<2xi32> +// CHECK-NEXT: [[VAL_2:%.*]] = tfl.add [[VAL_1]], [[VAL_0]] {fused_activation_function = "RELU6"} : tensor<2xi32> +// CHECK-NEXT: return [[VAL_2]] : tensor<2xi32> + %0 = func.call @func_with_tfl_ops(%arg0) : (tensor<2xi32>) -> tensor<2xi32> + func.return %0 : tensor<2xi32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/insert_call_once_op.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/insert_call_once_op.mlir new file mode 100644 index 0000000000000000000000000000000000000000..91ea274bdb3257f1eb278f3d7c33f1c34b99d3b2 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/insert_call_once_op.mlir @@ -0,0 +1,40 @@ +// RUN: litert-opt -split-input-file -tfl-insert-call-once-op %s | FileCheck %s + +// Tests that new call_once op is added when there is a session initializer. + +module attributes {tf_saved_model.semantics} { + "tf_saved_model.session_initializer"() {initializers = [@init_all_tables]} : () -> () + + func.func @init_all_tables() + attributes {tf_saved_model.exported_names = ["__tf_saved_model_session_initializer"]} { + %cst = arith.constant dense<[1, 2, 3, 4]> : tensor<4xi64> + %cst_0 = arith.constant dense<["a", "b", "c", "d"]> : tensor<4x!tf_type.string> + %0 = "tf.HashTableV2"() {container = "", device = "", key_dtype = i64, shared_name = "hash_table_dba2ccaa-f1b1-46d6-b276-98008f69da71", use_node_name_sharing = false, value_dtype = !tf_type.string} : () -> tensor + "tf.LookupTableImportV2"(%0, %cst, %cst_0) {device = ""} : (tensor, tensor<4xi64>, tensor<4x!tf_type.string>) -> () + func.return + // CHECK-LABEL: @init_all_tables + } + + func.func @serving_default(%arg0: tensor {tf_saved_model.index_path = ["x"]}) -> (tensor<*x!tf_type.string> {tf_saved_model.index_path = ["r"]}) + attributes {tf.entry_function = {control_outputs = "", inputs = "input:0", outputs = "hash_table_Lookup/LookupTableFindV2:0"}, tf_saved_model.exported_names = ["serving_default"]} { + %cst = arith.constant dense<"f"> : tensor + %0 = "tf.HashTableV2"() {container = "", device = "", key_dtype = i64, shared_name = "hash_table_dba2ccaa-f1b1-46d6-b276-98008f69da71", use_node_name_sharing = false, value_dtype = !tf_type.string} : () -> tensor + %1 = "tf.LookupTableFindV2"(%0, %arg0, %cst) {device = ""} : (tensor, tensor, tensor) -> tensor<*x!tf_type.string> + func.return %1 : tensor<*x!tf_type.string> + // CHECK-LABEL: @serving_default + // CHECK: "tfl.call_once"() <{session_init_function = "init_all_tables"}> : () -> () + } +} + +// ----- + +// Tests that no call_once op is added. + +module attributes {tf_saved_model.semantics} { + func.func @no_call_once(%arg0: tensor {tf_saved_model.index_path = ["x"]}) -> (tensor {tf_saved_model.index_path = ["r"]}) + attributes {tf.entry_function = {control_outputs = "", inputs = "input:0", outputs = "output:0"}, tf_saved_model.exported_names = ["serving_default"]} { + func.return %arg0 : tensor + // CHECK-LABEL: no_call_once + // CHECK-NOT: "tfl.call_once" + } +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/legalize-tensorlist.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/legalize-tensorlist.mlir new file mode 100644 index 0000000000000000000000000000000000000000..a7172af64fcbb5685e15a4245457379eeab7e476 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/legalize-tensorlist.mlir @@ -0,0 +1,157 @@ + +// RUN: litert-opt -tfl-legalize-tensorlist -canonicalize -split-input-file %s | FileCheck %s + +// CHECK-LABEL: listReserveScalarShapeI32 +func.func @listReserveScalarShapeI32(%arg0: tensor, %arg1: tensor) -> tensor>> { + %0 = "tf.TensorListReserve"(%arg0, %arg1) : (tensor, tensor) -> tensor>> + // CHECK: %0 = "tfl.custom"(%arg0, %arg1) <{custom_code = "TensorListReserve", custom_option = #tfl}> : (tensor, tensor) -> tensor>> + func.return %0 : tensor>> +} + +// ----- + +// CHECK-LABEL: listReserve1DShapeI32 +func.func @listReserve1DShapeI32(%arg0: tensor<2xi32>, %arg1: tensor) -> tensor>> { + %0 = "tf.TensorListReserve"(%arg0, %arg1) : (tensor<2xi32>, tensor) -> tensor>> + // CHECK: %0 = "tfl.custom"(%arg0, %arg1) <{custom_code = "TensorListReserve", custom_option = #tfl}> : (tensor<2xi32>, tensor) -> tensor>> + func.return %0 : tensor>> +} + +// ----- + +// CHECK-LABEL: listReserveScalarShapeFloat +func.func @listReserveScalarShapeFloat(%arg0: tensor, %arg1: tensor) -> tensor>> { + %0 = "tf.TensorListReserve"(%arg0, %arg1) : (tensor, tensor) -> tensor>> + // CHECK: %0 = "tfl.custom"(%arg0, %arg1) <{custom_code = "TensorListReserve", custom_option = #tfl}> : (tensor, tensor) -> tensor>> + func.return %0 : tensor>> +} + +// ----- + +// CHECK-LABEL: listReserveScalarShapeLong +func.func @listReserveScalarShapeLong(%arg0: tensor, %arg1: tensor) -> tensor>> { + %0 = "tf.TensorListReserve"(%arg0, %arg1) : (tensor, tensor) -> tensor>> + // CHECK: %0 = "tfl.custom"(%arg0, %arg1) <{custom_code = "TensorListReserve", custom_option = #tfl}> : (tensor, tensor) -> tensor>> + func.return %0 : tensor>> +} + +// ----- + +// CHECK-LABEL: listReserveScalarShapeBool +func.func @listReserveScalarShapeBool(%arg0: tensor, %arg1: tensor) -> tensor>> { + %0 = "tf.TensorListReserve"(%arg0, %arg1) : (tensor, tensor) -> tensor>> + // CHECK: %0 = "tfl.custom"(%arg0, %arg1) <{custom_code = "TensorListReserve", custom_option = #tfl}> : (tensor, tensor) -> tensor>> + func.return %0 : tensor>> +} + +// ----- + +// CHECK-LABEL: listStack +func.func @listStack(%arg0: tensor>>, %arg1: tensor) -> tensor<*xi32> { + %0 = "tf.TensorListStack"(%arg0, %arg1) : (tensor>>, tensor) -> tensor<*xi32> + // CHECK: %0 = "tfl.custom"(%arg0, %arg1) <{custom_code = "TensorListStack", custom_option = #tfl}> : (tensor>>, tensor) -> tensor<*xi32> + func.return %0 : tensor<*xi32> +} + +// ----- + +// CHECK-LABEL: listSetItem +func.func @listSetItem(%arg0: tensor>>, %arg1: tensor, %arg2: tensor<*xi32>) -> tensor>> { + %0 = "tf.TensorListSetItem"(%arg0, %arg1, %arg2) : (tensor>>, tensor, tensor<*xi32>) -> tensor>> + // CHECK: %0 = "tfl.custom"(%arg0, %arg1, %arg2) <{custom_code = "TensorListSetItem", custom_option = #tfl}> : (tensor>>, tensor, tensor<*xi32>) -> tensor>> + func.return %0 : tensor>> +} + +// ----- + +// CHECK-LABEL: listGetItem +func.func @listGetItem(%arg0: tensor>>, %arg1: tensor, %arg2: tensor<2xi32>) -> tensor<2xi32> { + %0 = "tf.TensorListGetItem"(%arg0, %arg1, %arg2) : (tensor>>, tensor, tensor<2xi32>) -> tensor<2xi32> + // CHECK: %0 = "tfl.custom"(%arg0, %arg1, %arg2) <{custom_code = "TensorListGetItem", custom_option = #tfl}> : (tensor>>, tensor, tensor<2xi32>) -> tensor<2xi32> + func.return %0 : tensor<2xi32> +} + +// ----- + +// CHECK-LABEL: listFromTensor +func.func @listFromTensor(%tensor: tensor<3xi32>, %shape : tensor) -> tensor>> { + %0 = "tf.TensorListFromTensor"(%tensor, %shape) : (tensor<3xi32>, tensor) -> tensor>> + func.return %0 : tensor>> + // CHECK: %0 = "tfl.custom"(%arg0, %arg1) <{custom_code = "TensorListFromTensor", custom_option = #tfl}> : (tensor<3xi32>, tensor) -> tensor>> +} + +// ----- + +// CHECK-LABEL: typeNotSupportedNotLegalized +func.func @typeNotSupportedNotLegalized(%arg0: tensor>>, %arg1: tensor, %arg2: tensor<*xf64>) -> tensor>> { + %0 = "tf.TensorListSetItem"(%arg0, %arg1, %arg2) : (tensor>>, tensor, tensor<*xf64>) -> tensor>> + // CHECK-NOT: "tfl.custom" + // CHECK-MSG: Tried legalizing to tfl custom tensorlist ops, but not all can be supported. + func.return %0 : tensor>> +} + +// ----- + +// CHECK-LABEL: listLength +func.func @listLength(%arg0: tensor>>) -> tensor { + %0 = "tf.TensorListLength"(%arg0) : (tensor>>) -> tensor + // CHECK: %0 = "tfl.custom"(%arg0) <{custom_code = "TensorListLength", custom_option = #tfl}> : (tensor>>) -> tensor + func.return %0 : tensor +} + +// ----- + +// CHECK-LABEL: listEmptyToListReserve +func.func @listEmptyToListReserve(%arg0: tensor, %arg1: tensor) -> tensor>> { + %0 = "tf.EmptyTensorList"(%arg0, %arg1) : (tensor, tensor) -> tensor>> + // CHECK: %cst = arith.constant dense<0> : tensor + // CHECK: %0 = "tfl.custom"(%arg0, %cst) <{custom_code = "TensorListReserve", custom_option = #tfl}> : (tensor, tensor) -> tensor>> + func.return %0 : tensor>> +} + +// ----- + +// CHECK-LABEL: listElementShape +func.func @listElementShape(%arg0: tensor>>) -> tensor<*xi32> { + %0 = "tf.TensorListElementShape"(%arg0) : (tensor>>) -> tensor<*xi32> + // CHECK: %0 = "tfl.custom"(%arg0) <{custom_code = "TensorListElementShape", custom_option = #tfl}> : (tensor>>) -> tensor<*xi32> + func.return %0 : tensor<*xi32> +} + +// ----- + +// CHECK-LABEL: listPopBack +func.func @listPopBack(%arg0: tensor>>, %arg1: tensor<1xi32>) -> (tensor>>, tensor<2xi32>) { + %0, %1 = "tf.TensorListPopBack"(%arg0, %arg1) : (tensor>>, tensor<1xi32>) -> (tensor>>, tensor<2xi32>) + // CHECK: %0:2 = "tfl.custom"(%arg0, %arg1) <{custom_code = "TensorListPopBack", custom_option = #tfl}> : (tensor>>, tensor<1xi32>) -> (tensor>>, tensor<2xi32>) + func.return %0, %1 : tensor>>, tensor<2xi32> +} + +// ----- + +// CHECK-LABEL: listPushBack +func.func @listPushBack(%arg0: tensor>>, %arg1: tensor<16x1xf32>) -> tensor>> { + %0 = "tf.TensorListPushBack"(%arg0, %arg1) : (tensor>>, tensor<16x1xf32>) -> tensor>> + // CHECK: %0 = "tfl.custom"(%arg0, %arg1) <{custom_code = "TensorListPushBack", custom_option = #tfl}> : (tensor>>, tensor<16x1xf32>) -> tensor>> + func.return %0: tensor>> +} + +// ----- + +// CHECK-LABEL: variantAddN +func.func @variantAddN(%arg0: tensor>>, %arg1: tensor>>) -> tensor>> { + %1 = "tf.AddN"(%arg0, %arg1) : (tensor>>, tensor>>) -> tensor>> + // CHECK: %0 = "tfl.custom"(%arg0, %arg1) <{custom_code = "VariantAddN", custom_option = #tfl}> : (tensor>>, tensor>>) -> tensor>> + func.return %1 : tensor>> +} + +// ----- + +// CHECK-LABEL: variantZeroesLikeNoLegalize +func.func @variantZeroesLikeNoLegalize(%arg0: tensor, %arg1: tensor) -> tensor>> { + %0 = "tf.TensorListReserve"(%arg0, %arg1) : (tensor, tensor) -> tensor>> + %1 = "tf.ZerosLike"(%0) : (tensor>>) -> tensor>> + // CHECK-NOT: "tfl.custom" + // CHECK-MSG: Tried legalizing to tfl custom tensorlist ops, but not all can be supported. + func.return %1 : tensor>> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/legalize-tf-assert.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/legalize-tf-assert.mlir new file mode 100644 index 0000000000000000000000000000000000000000..ab5252b730a4c64a1aef012ee9f4cef1218a982f --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/legalize-tf-assert.mlir @@ -0,0 +1,11 @@ +// RUN: litert-opt %s -tfl-legalize-tf='preserve-assert-op=true' | FileCheck %s --dump-input=fail + +func.func @preserve_assert(%arg0: tensor<1xi32>, %arg1: tensor<1xi32>) -> tensor<1xi1> { + %0 = "tf.LessEqual"(%arg0, %arg1) : (tensor<1xi32>, tensor<1xi32>) -> tensor<1xi1> + "tf.Assert"(%0, %arg1) {summarize = 3} : (tensor<1xi1>, tensor<1xi32>) -> () + func.return %0 : tensor<1xi1> + // CHECK-LABEL: preserve_assert + // CHECK: tfl.less_equal + // CHECK: Assert + // CHECK: return +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/legalize-tf-hashtables.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/legalize-tf-hashtables.mlir new file mode 100644 index 0000000000000000000000000000000000000000..b98970c5c4bca77d85b2e9b3a50acc8b9a81f49a --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/legalize-tf-hashtables.mlir @@ -0,0 +1,134 @@ +// RUN: litert-opt %s -split-input-file -tfl-legalize-hashtables-tf --cse | FileCheck %s + +// Test for case with string -> int64 hashtable. +func.func @hashtable_string_to_int64(%arg0: tensor) -> tensor<*xi64> { + %cst = arith.constant dense<"f"> : tensor + %0 = "tf.HashTableV2"() {container = "", device = "", key_dtype = !tf_type.string, shared_name = "hash_table_e308c10b-91c8-416c-81f9-af5bf6aba847", use_node_name_sharing = false, value_dtype = i64} : () -> tensor + %1 = "tf.LookupTableFindV2"(%0, %cst, %arg0) {device = ""} : (tensor, tensor, tensor) -> tensor<*xi64> + // CHECK-LABEL: hashtable_string_to_int64 + // CHECK: [[CST:%.*]] = arith.constant dense<"f"> : tensor + // CHECK-NEXT: [[HASH_TABLE:%.*]] = "tfl.hashtable"() <{key_dtype = !tf_type.string, table_id = {{[-]?[0-9]+}} : i32, value_dtype = i64}> : () -> tensor<1x!tf_type.resource> + // CHECK-NEXT: [[FIND:%.*]] = "tfl.hashtable_find"([[HASH_TABLE]], [[CST]], %arg0) : (tensor<1x!tf_type.resource>, tensor, tensor) -> tensor<*xi64> + // CHECK-NEXT: return [[FIND]] : tensor<*xi64> + func.return %1 : tensor<*xi64> +} + +// ----- + +// Test for case with int64 -> string hashtable. +func.func @hashtable_int64_to_string(%arg0: tensor) -> tensor<*x!tf_type.string> { + %cst = arith.constant dense<"f"> : tensor + %0 = "tf.HashTableV2"() {container = "", device = "", key_dtype = i64, shared_name = "hash_table_e308c10b-91c8-416c-81f9-af5bf6aba847", use_node_name_sharing = false, value_dtype = !tf_type.string} : () -> tensor + %1 = "tf.LookupTableFindV2"(%0, %arg0, %cst) {device = ""} : (tensor, tensor, tensor) -> tensor<*x!tf_type.string> + // CHECK-LABEL: hashtable_int64_to_string + // CHECK: [[CST:%.*]] = arith.constant dense<"f"> : tensor + // CHECK-NEXT: [[HASH_TABLE:%.*]] = "tfl.hashtable"() <{key_dtype = i64, table_id = {{[-]?[0-9]+}} : i32, value_dtype = !tf_type.string}> : () -> tensor<1x!tf_type.resource> + // CHECK-NEXT: [[FIND:%.*]] = "tfl.hashtable_find"([[HASH_TABLE]], %arg0, [[CST]]) : (tensor<1x!tf_type.resource>, tensor, tensor) -> tensor<*x!tf_type.string> + // CHECK-NEXT: return [[FIND]] : tensor<*x!tf_type.string> + func.return %1 : tensor<*x!tf_type.string> +} + +// ----- + +// Test for case with unsupported string -> string mapping. +func.func @no_legalization_on_hashtable_string_to_string(%arg0: tensor) { + %0 = "tf.HashTableV2"() {container = "", device = "", key_dtype = !tf_type.string, shared_name = "hash_table_e308c10b-91c8-416c-81f9-af5bf6aba847", use_node_name_sharing = false, value_dtype = !tf_type.string} : () -> tensor + "tf.LookupTableRemoveV2"(%0, %arg0) {device = ""} : (tensor, tensor) -> () + // CHECK-LABEL: no_legalization_on_hashtable_string_to_string + // CHECK-NEXT: "tf.HashTableV2" + // CHECK-NEXT: "tf.LookupTableRemoveV2" + func.return +} + +// ----- + +// Test for case with import op. +func.func @hashtable_import(%arg0: tensor<5x!tf_type.string>) { + %cst = arith.constant dense<["emerson", "lake", "palmer"]> : tensor<3x!tf_type.string> + %cst_0 = arith.constant dense<[0, 1, 2]> : tensor<3xi64> + %0 = "tf.HashTableV2"() {container = "", device = "", key_dtype = !tf_type.string, shared_name = "hash_table_1dd4fef4-646d-491f-a3a8-bf5334f45813", use_node_name_sharing = false, value_dtype = i64} : () -> tensor + "tf.LookupTableImportV2"(%0, %cst, %cst_0) {device = ""} : (tensor, tensor<3x!tf_type.string>, tensor<3xi64>) -> () + func.return + // CHECK-LABEL: hashtable_import + // CHECK: [[CST:%.*]] = arith.constant dense<["emerson", "lake", "palmer"]> : tensor<3x!tf_type.string> + // CHECK-NEXT: [[CST_0:%.*]] = arith.constant dense<[0, 1, 2]> : tensor<3xi64> + // CHECK-NEXT: [[HASH_TABLE:%.*]] = "tfl.hashtable"() <{key_dtype = !tf_type.string, table_id = {{[-]?[0-9]+}} : i32, value_dtype = i64}> : () -> tensor<1x!tf_type.resource> + // CHECK-NEXT: "tfl.hashtable_import"([[HASH_TABLE]], [[CST]], [[CST_0]]) : (tensor<1x!tf_type.resource>, tensor<3x!tf_type.string>, tensor<3xi64>) -> () +} + +// ----- + +// Test for case with size op. +func.func @hashtable_size(%arg0: tensor<5x!tf_type.string>) -> tensor { + %0 = "tf.HashTableV2"() {container = "", device = "", key_dtype = !tf_type.string, shared_name = "hash_table_1dd4fef4-646d-491f-a3a8-bf5334f45813", use_node_name_sharing = false, value_dtype = i64} : () -> tensor + %1 = "tf.LookupTableSizeV2"(%0) {device = ""} : (tensor) -> tensor + // CHECK-LABEL: hashtable_size + // CHECK-NEXT: [[HASH_TABLE:%.*]] = "tfl.hashtable"() <{key_dtype = !tf_type.string, table_id = {{[-]?[0-9]+}} : i32, value_dtype = i64}> : () -> tensor<1x!tf_type.resource> + // CHECK-NEXT: [[SIZE:%.*]] = "tfl.hashtable_size"([[HASH_TABLE]]) : (tensor<1x!tf_type.resource>) -> tensor + // CHECK-NEXT: return [[SIZE]] : tensor + func.return %1 : tensor +} + +// ----- + +// Test for case with import and find ops. +func.func @hashtable_import_then_find(%arg0: tensor<5x!tf_type.string>) -> tensor<*xi64> { + %cst = arith.constant dense<["emerson", "lake", "palmer"]> : tensor<3x!tf_type.string> + %cst_0 = arith.constant dense<-1> : tensor + %cst_1 = arith.constant dense<[0, 1, 2]> : tensor<3xi64> + %0 = "tf.HashTableV2"() {container = "", device = "", key_dtype = !tf_type.string, shared_name = "hash_table_1dd4fef4-646d-491f-a3a8-bf5334f45813", use_node_name_sharing = false, value_dtype = i64} : () -> tensor + "tf.LookupTableImportV2"(%0, %cst, %cst_1) {device = ""} : (tensor, tensor<3x!tf_type.string>, tensor<3xi64>) -> () + %1 = "tf.LookupTableFindV2"(%0, %arg0, %cst_0) {device = ""} : (tensor, tensor<5x!tf_type.string>, tensor) -> tensor<*xi64> + // CHECK-LABEL: hashtable_import_then_find + // CHECK: [[CST:%.*]] = arith.constant dense<["emerson", "lake", "palmer"]> : tensor<3x!tf_type.string> + // CHECK-NEXT: [[CST_0:%.*]] = arith.constant dense<-1> : tensor + // CHECK-NEXT: [[CST_1:%.*]] = arith.constant dense<[0, 1, 2]> : tensor<3xi64> + // CHECK-NEXT: [[HASH_TABLE:%.*]] = "tfl.hashtable"() <{key_dtype = !tf_type.string, table_id = {{[-]?[0-9]+}} : i32, value_dtype = i64}> : () -> tensor<1x!tf_type.resource> + // CHECK-NEXT: "tfl.hashtable_import"([[HASH_TABLE]], [[CST]], [[CST_1]]) : (tensor<1x!tf_type.resource>, tensor<3x!tf_type.string>, tensor<3xi64>) -> () + // CHECK-NEXT: [[FIND:%.*]] = "tfl.hashtable_find"([[HASH_TABLE]], %arg0, [[CST_0]]) : (tensor<1x!tf_type.resource>, tensor<5x!tf_type.string>, tensor) -> tensor<*xi64> + // CHECK-NEXT: return [[FIND]] : tensor<*xi64> + func.return %1 : tensor<*xi64> +} + +// ----- + +// Test for case with import and size ops. +func.func @hashtable_import_then_size(%arg0: tensor<5x!tf_type.string>) -> tensor { + %cst = arith.constant dense<["emerson", "lake", "palmer"]> : tensor<3x!tf_type.string> + %cst_0 = arith.constant dense<[0, 1, 2]> : tensor<3xi64> + %0 = "tf.HashTableV2"() {container = "", device = "", key_dtype = !tf_type.string, shared_name = "hash_table_1dd4fef4-646d-491f-a3a8-bf5334f45813", use_node_name_sharing = false, value_dtype = i64} : () -> tensor + "tf.LookupTableImportV2"(%0, %cst, %cst_0) {device = ""} : (tensor, tensor<3x!tf_type.string>, tensor<3xi64>) -> () + %1 = "tf.LookupTableSizeV2"(%0) {device = ""} : (tensor) -> tensor + // CHECK-LABEL: hashtable_import_then_size + // CHECK: [[CST:%.*]] = arith.constant dense<["emerson", "lake", "palmer"]> : tensor<3x!tf_type.string> + // CHECK-NEXT: [[CST_0:%.*]] = arith.constant dense<[0, 1, 2]> : tensor<3xi64> + // CHECK-NEXT: [[HASH_TABLE:%.*]] = "tfl.hashtable"() <{key_dtype = !tf_type.string, table_id = {{[-]?[0-9]+}} : i32, value_dtype = i64}> : () -> tensor<1x!tf_type.resource> + // CHECK-NEXT: "tfl.hashtable_import"([[HASH_TABLE]], [[CST]], [[CST_0]]) : (tensor<1x!tf_type.resource>, tensor<3x!tf_type.string>, tensor<3xi64>) -> () + // CHECK-NEXT: [[SIZE:%.*]] = "tfl.hashtable_size"([[HASH_TABLE]]) : (tensor<1x!tf_type.resource>) -> tensor + // CHECK-NEXT: return [[SIZE]] : tensor + func.return %1 : tensor +} + +// ----- + +// Test for case with unsupported LookupTableRemoveV2 op. +func.func @no_legalization_on_hashtable_remove(%arg0: tensor) { + %0 = "tf.HashTableV2"() {container = "", device = "", key_dtype = i64, shared_name = "hash_table_e308c10b-91c8-416c-81f9-af5bf6aba847", use_node_name_sharing = false, value_dtype = !tf_type.string} : () -> tensor + "tf.LookupTableRemoveV2"(%0, %arg0) {device = ""} : (tensor, tensor) -> () + // CHECK-LABEL: no_legalization_on_hashtable_remove + // CHECK-NEXT: "tf.HashTableV2" + // CHECK-NEXT: "tf.LookupTableRemoveV2" + func.return +} + +// ----- + +// Test for case with unsupported MutableHashTableV2 op. +func.func @no_legalization_on_mutable_hashtable(%arg0: tensor) { + %0 = "tf.MutableHashTableV2Op"() {container = "", key_dtype = i64, use_node_name_sharing = false, value_dtype = !tf_type.string} : () -> tensor + "tf.LookupTableRemoveV2"(%0, %arg0) {device = ""} : (tensor, tensor) -> () + // CHECK-LABEL: no_legalization_on_mutable_hashtable + // CHECK-NEXT: "tf.MutableHashTableV2Op" + // CHECK-NEXT: "tf.LookupTableRemoveV2" + func.return +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/legalize-tf-no-runtime-verification.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/legalize-tf-no-runtime-verification.mlir new file mode 100644 index 0000000000000000000000000000000000000000..8953e25e852031dd4d0bb4e9abd2733ed7d8ea4b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/legalize-tf-no-runtime-verification.mlir @@ -0,0 +1,10 @@ +// RUN: litert-opt %s -tfl-prepare-tf -tfl-legalize-tf='run-tfl-runtime-verification=false' -tfl-optimize | FileCheck %s + +func.func @broadcast_to_bf16(%arg0: tensor<3xbf16>, %arg1: tensor<2xi64>) -> tensor<3x3xbf16> { + %0 = "tf.BroadcastTo"(%arg0, %arg1) : (tensor<3xbf16>, tensor<2xi64>) -> tensor<3x3xbf16> + func.return %0: tensor<3x3xbf16> + +// CHECK-LABEL: broadcast_to_bf16 +// CHECK: %0 = "tfl.broadcast_to"(%arg0, %arg1) : (tensor<3xbf16>, tensor<2xi64>) -> tensor<3x3xbf16> +// CHECK: return %0 : tensor<3x3xbf16> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/legalize-tf-variables.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/legalize-tf-variables.mlir new file mode 100644 index 0000000000000000000000000000000000000000..f02e024be48307022dc450d1a05bcbc772c9b35b --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/legalize-tf-variables.mlir @@ -0,0 +1,111 @@ +// RUN: litert-opt %s -split-input-file -tfl-legalize-variables-tf --cse | FileCheck %s + +// Test for case with no session initialize op. +module attributes {tf_saved_model.semantics} { + // CHECK-LABEL: serving_default + func.func @serving_default(%arg0: tensor<1x10xf32> {tf_saved_model.index_path = ["x"]}) -> + (tensor<1x10xf32> {tf_saved_model.index_path = ["output_0"]}, tensor<1x10xi64> {tf_saved_model.index_path = ["output_1"]}) attributes {tf.entry_function = {control_outputs = "", inputs = "serving_default_x:0", outputs = "StatefulPartitionedCall:0"}, tf_saved_model.exported_names = ["serving_default"]} { + %handle_0 = "tf.VarHandleOp"() {container="c", shared_name="a"} : () -> tensor>> + %handle_1 = "tf.VarHandleOp"() {container="c", shared_name="b"} : () -> tensor>> + %0 = "tf.ReadVariableOp"(%handle_0) : (tensor>>) -> tensor<1x10xf32> + %1 = tfl.add %0, %arg0 {fused_activation_function = "NONE"} : tensor<1x10xf32> + "tf.AssignVariableOp"(%handle_0, %1) : (tensor>>, tensor<1x10xf32>) -> () + %2 = "tf.ReadVariableOp"(%handle_0) {device = ""} : (tensor>>) -> tensor<1x10xf32> + %3 = "tf.ReadVariableOp"(%handle_1) {device = ""} : (tensor>>) -> tensor<1x10xi64> + func.return %2, %3 : tensor<1x10xf32>, tensor<1x10xi64> + } + + // CHECK: %[[RESOURCE:.*]] = "tfl.var_handle"() <{container = "c", shared_name = "a"}> : () -> tensor>> + // CHECK: %[[RESOURCE_1:.*]] = "tfl.var_handle"() <{container = "c", shared_name = "b"}> : () -> tensor>> + // CHECK: %[[VAR_VAL:.*]] = "tfl.read_variable"(%[[RESOURCE]]) : (tensor>>) -> tensor<1x10xf32> + // CHECK: %[[ADD:.*]] = tfl.add %[[VAR_VAL]], %arg0 {fused_activation_function = "NONE"} : tensor<1x10xf32> + // CHECK: "tfl.assign_variable"(%[[RESOURCE]], %[[ADD]]) : (tensor>>, tensor<1x10xf32>) -> () + // CHECK: %[[RESULT:.*]] = "tfl.read_variable"(%[[RESOURCE]]) : (tensor>>) -> tensor<1x10xf32> + // CHECK: %[[VAR:.*]] = "tfl.read_variable"(%[[RESOURCE_1]]) : (tensor>>) -> tensor<1x10xi64> + // CHECK: return %[[RESULT]], %[[VAR]] : tensor<1x10xf32>, tensor<1x10xi64> +} + +// ----- + +// Test for case with existing session initialize op. +module attributes {tf_saved_model.semantics} { + // CHECK-LABEL: init_all_tables + func.func @init_all_tables() + attributes {tf_saved_model.exported_names = ["__tf_saved_model_session_initializer"]} { + %cst = arith.constant dense<[1, 2, 3, 4]> : tensor<4xi64> + %cst_0 = arith.constant dense<["a", "b", "c", "d"]> : tensor<4x!tf_type.string> + %0 = "tf.HashTableV2"() {container = "", device = "", key_dtype = i64, shared_name = "hash_table_dba2ccaa-f1b1-46d6-b276-98008f69da71", use_node_name_sharing = false, value_dtype = !tf_type.string} : () -> tensor + "tf.LookupTableImportV2"(%0, %cst, %cst_0) {device = ""} : (tensor, tensor<4xi64>, tensor<4x!tf_type.string>) -> () + %handle_0 = "tf.VarHandleOp"() {container="c", shared_name="a"} : () -> tensor>> + %cst_1 = arith.constant dense<1.0> : tensor<1x10xf32> + "tf.AssignVariableOp"(%handle_0, %cst_1) : (tensor>>, tensor<1x10xf32>) -> () + func.return + // CHECK: %[[CST:.*]] = arith.constant dense<1.000000e+00> : tensor<1x10xf32> + // CHECK: %[[RESOURCE:.*]] = "tfl.var_handle"() <{container = "c", shared_name = "a"}> : () -> tensor>> + // CHECK: "tfl.assign_variable"(%[[RESOURCE]], %[[CST]]) : (tensor>>, tensor<1x10xf32>) -> () + } + + "tf_saved_model.session_initializer"() {initializers = [@init_all_tables]} : () -> () + // CHECK: "tf_saved_model.session_initializer"() <{initializers = [@init_all_tables]}> : () -> () + + // CHECK-LABEL: serving_default + func.func @serving_default(%arg0: tensor<1x10xf32> {tf_saved_model.index_path = ["x"]}) -> + (tensor<1x10xf32> {tf_saved_model.index_path = ["output_0"]}) attributes {tf.entry_function = {control_outputs = "", inputs = "serving_default_x:0", outputs = "StatefulPartitionedCall:0"}, tf_saved_model.exported_names = ["serving_default"]} { + %handle_0 = "tf.VarHandleOp"() {container="c", shared_name="a"} : () -> tensor>> + %0 = "tf.ReadVariableOp"(%handle_0) : (tensor>>) -> tensor<1x10xf32> + %1 = tfl.add %0, %arg0 {fused_activation_function = "NONE"} : tensor<1x10xf32> + "tf.AssignVariableOp"(%handle_0, %1) : (tensor>>, tensor<1x10xf32>) -> () + %2 = "tf.ReadVariableOp"(%handle_0) {device = ""} : (tensor>>) -> tensor<1x10xf32> + func.return %2 : tensor<1x10xf32> + // CHECK: %[[RESOURCE:.*]] = "tfl.var_handle"() <{container = "c", shared_name = "a"}> : () -> tensor>> + // CHECK: %[[VAR_VAL:.*]] = "tfl.read_variable"(%[[RESOURCE]]) : (tensor>>) -> tensor<1x10xf32> + // CHECK: %[[ADD:.*]] = tfl.add %[[VAR_VAL]], %arg0 {fused_activation_function = "NONE"} : tensor<1x10xf32> + // CHECK: "tfl.assign_variable"(%[[RESOURCE]], %[[ADD]]) : (tensor>>, tensor<1x10xf32>) -> () + // CHECK: %[[RESULT:.*]] = "tfl.read_variable"(%[[RESOURCE]]) : (tensor>>) -> tensor<1x10xf32> + // CHECK: return %[[RESULT]] : tensor<1x10xf32> + } +} + +// ----- + +// Don't legalize if type is not supported. +module attributes {tf_saved_model.semantics} { + func.func @serving_default() -> + (tensor<1x10xui64> {tf_saved_model.index_path = ["output_0"]}) attributes {tf.entry_function = {control_outputs = "", inputs = "", outputs = "StatefulPartitionedCall:0"}, tf_saved_model.exported_names = ["serving_default"]} { + %handle_0 = "tf.VarHandleOp"() {container="c", shared_name="a"} : () -> tensor>> + %0 = "tf.ReadVariableOp"(%handle_0) {device = ""} : (tensor>>) -> tensor<1x10xui64> + func.return %0 : tensor<1x10xui64> + } + + // CHECK: tf.ReadVariableOp +} + +// ----- + +// Don't legalize if type is not supported. +module attributes {tf_saved_model.semantics, tfl._legalize_tfl_variables = true} { + func.func @serving_default() -> + () attributes {tf.entry_function = {control_outputs = "", inputs = "", outputs = ""}, tf_saved_model.exported_names = ["serving_default"]} { + %handle_0 = "tf.VarHandleOp"() {container="c", shared_name="a"} : () -> tensor>> + %cst = "tf.Const"() {value = dense<2> : tensor<1x10xui64>} : () -> tensor<1x10xui64> + "tf.AssignVariableOp"(%handle_0, %cst) : (tensor>>, tensor<1x10xui64>) -> () + func.return + } + + // CHECK: tf.VarHandleOp + // CHECK: tf.AssignVariableOp +} + +// ----- + +// Don't legalize if disabled by earlier analysis. +module attributes {tf_saved_model.semantics, tfl._legalize_tfl_variables = false} { + func.func @serving_default() -> + (tensor<1x10xf32> {tf_saved_model.index_path = ["output_0"]}) attributes {tf.entry_function = {control_outputs = "", inputs = "", outputs = "StatefulPartitionedCall:0"}, tf_saved_model.exported_names = ["serving_default"]} { + %handle_0 = "tf.VarHandleOp"() {container="c", shared_name="a"} : () -> tensor>> + %0 = "tf.ReadVariableOp"(%handle_0) : (tensor>>) -> tensor<1x10xf32> + func.return %0 : tensor<1x10xf32> + } + + // CHECK: tf.ReadVariableOp +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/legalize-tf-while.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/legalize-tf-while.mlir new file mode 100644 index 0000000000000000000000000000000000000000..e4aae88bff747dea3a6eff1fd2c781d498d95c18 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/legalize-tf-while.mlir @@ -0,0 +1,76 @@ +// RUN: litert-opt --tfl-legalize-tf-while %s -o - | FileCheck %s +// RUN: litert-opt --tfl-legalize-tf-while %s -o - --tfl-legalize-tf-while --inline='default-pipeline=''' | FileCheck %s --check-prefix=INLINE +// RUN: litert-opt --tfl-legalize-tf-while %s -o - --tfl-legalize-tf-while --inline | FileCheck %s --check-prefix=CANON + +func.func @while_main(%arg0: tensor) -> (tensor, tensor<256x256xf32>, tensor) attributes {tf.entry_function = {inputs = "input", outputs = "Identity,Identity_1,Identity_2"}} { + %cst = arith.constant dense<1.000000e+00> : tensor<256x256xf32> + %cst_0 = arith.constant dense<0> : tensor + %cst_1 = arith.constant dense<-1> : tensor + %0:5 = "tf.While"(%cst_0, %cst_1, %cst_0, %cst, %arg0) {body = @while_body_11_frozen0, cond = @while_cond_10_frozen0, device = "", is_stateless = true} : (tensor, tensor, tensor, tensor<256x256xf32>, tensor) -> (tensor, tensor, tensor, tensor<256x256xf32>, tensor) + func.return %0#2, %0#3, %0#4 : tensor, tensor<256x256xf32>, tensor +} + +func.func @while_body_11_frozen0(%arg0: tensor<*xi32>, %arg1: tensor<*xi32>, %arg2: tensor<*xi32>, %arg3: tensor<*xf32>, %arg4: tensor<*xf32>) -> (tensor<*xi32>, tensor<*xi32>, tensor<*xi32>, tensor<*xf32>, tensor<*xf32>) { + %cst = arith.constant dense<[1, 0]> : tensor<2xi32> + %cst_0 = arith.constant dense<0> : tensor + %cst_1 = arith.constant dense<-1> : tensor + %cst_2 = arith.constant dense<1> : tensor + %0 = "tf.AddV2"(%arg2, %cst_2) {T = i32, device = ""} : (tensor<*xi32>, tensor) -> tensor<*xi32> + %1 = "tf.Transpose"(%arg3, %cst) {T = f32, Tperm = i32, device = ""} : (tensor<*xf32>, tensor<2xi32>) -> tensor + %2 = "tf.Rank"(%arg3) : (tensor<*xf32>) -> tensor + %3 = "tf.Range"(%2, %cst_0, %cst_1) : (tensor, tensor, tensor) -> tensor + %4 = "tf.Sub"(%3, %cst_2) : (tensor, tensor) -> tensor + %5 = "tf.Transpose"(%arg3, %4) : (tensor<*xf32>, tensor) -> tensor<*xf32> + %6 = "tf.MatMul"(%1, %5) {transpose_a = false, transpose_b = true} : (tensor, tensor<*xf32>) -> tensor + %7 = "tf.AddV2"(%arg4, %6) {T = f32, device = ""} : (tensor<*xf32>, tensor) -> tensor<*xf32> + %8 = "tf.AddV2"(%arg0, %cst_2) {T = i32, device = ""} : (tensor<*xi32>, tensor) -> tensor<*xi32> + func.return %8, %arg1, %0, %arg3, %7 : tensor<*xi32>, tensor<*xi32>, tensor<*xi32>, tensor<*xf32>, tensor<*xf32> +} + +func.func @while_cond_10_frozen0(%arg0: tensor<*xi32>, %arg1: tensor<*xi32>, %arg2: tensor<*xi32>, %arg3: tensor<*xf32>, %arg4: tensor<*xf32>) -> tensor<*xi1> { + %cst = arith.constant dense<10> : tensor + %0 = "tf.Less"(%arg2, %cst) {T = i32, device = ""} : (tensor<*xi32>, tensor) -> tensor<*xi1> + func.return %0 : tensor<*xi1> +} + +// CHECK: tfl.while +// CHECK: ^bb0([[ARGS:.*]]): +// CHECK: call @while_cond_10_frozen0 +// CHECK: yield +// CHECK: ^bb0([[ARGS]]): +// CHECK: call @while_body +// CHECK: yield +// CHECK: while_body +// CHECK: while_cond + +// INLINE: tfl.while +// INLINE: ^bb0([[ARGS:.*]]): +// INLINE: tf.Less +// INLINE: yield +// INLINE: ^bb0([[ARGS]]): +// INLINE: %cst_2 = arith.constant +// INLINE: yield +// INLINE-NOT: while_body +// INLINE-NOT: while_cond + +// CANON-LABEL: func @while_main +// CANON-SAME: ([[VAL_0:%.*]]: tensor) +// CANON-SAME: (tensor, tensor<256x256xf32>, tensor) +// CANON: [[VAL_1:%.*]] = arith.constant dense<1.000000e+00> : tensor<256x256xf32> +// CANON: [[VAL_2:%.*]] = arith.constant dense<0> : tensor +// CANON: [[VAL_6:%.*]]:3 = "tfl.while"([[VAL_2]], [[VAL_2]], [[VAL_0]]) <{is_stateless = true}> ({ +// CANON: ^bb0([[VAL_7:%.*]]: tensor<*xi32>, [[VAL_8:%.*]]: tensor<*xi32>, [[VAL_9:%.*]]: tensor<*xf32>): +// CANON: [[VAL_3:%.*]] = arith.constant dense<10> : tensor +// CANON: [[VAL_10:%.*]] = "tf.Less"([[VAL_8]], [[VAL_3]]) +// CANON: "tfl.yield"([[VAL_10]]) : (tensor<*xi1>) -> () +// CANON: }, { +// CANON: ^bb0([[VAL_11:%.*]]: tensor<*xi32>, [[VAL_12:%.*]]: tensor<*xi32>, [[VAL_13:%.*]]: tensor<*xf32>): +// CANON-DAG: [[VAL_4:%.*]] = arith.constant dense<1> : tensor +// CANON-DAG: [[VAL_5:%.*]] = "tf.Const"() <{value = dense<2.560000e+02> : tensor<256x256xf32>}> : () -> tensor +// CANON: [[VAL_14:%.*]] = "tf.AddV2"([[VAL_12]], [[VAL_4]]) +// CANON: [[VAL_15:%.*]] = "tf.AddV2"([[VAL_13]], [[VAL_5]]) +// CANON: [[VAL_16:%.*]] = "tf.AddV2"([[VAL_11]], [[VAL_4]]) +// CANON: "tfl.yield"([[VAL_16]], [[VAL_14]], [[VAL_15]]) : (tensor<*xi32>, tensor<*xi32>, tensor<*xf32>) -> () +// CANON: }) : (tensor, tensor, tensor) -> (tensor, tensor, tensor) +// CANON: return [[VAL_17:%.*]]#1, [[VAL_1]], [[VAL_17]]#2 : tensor, tensor<256x256xf32>, tensor +// CANON: } diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/legalize-tf.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/legalize-tf.mlir new file mode 100644 index 0000000000000000000000000000000000000000..983c3cd9116c5d94f44e0aef1ef4b1b1593ac6db --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/legalize-tf.mlir @@ -0,0 +1,2774 @@ +// RUN: litert-opt %s -tfl-legalize-tf --cse -split-input-file| FileCheck %s --dump-input=fail + +func.func @add(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>) -> tensor<1xf32> { + %0 = "tf.Add"(%arg0, %arg1) : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + func.return %0: tensor<1xf32> + +// CHECK-LABEL: add +// CHECK: tfl.add %arg0, %arg1 {fused_activation_function = "NONE"} : tensor<1xf32> +// CHECK: return +} + +func.func @sub(%arg0: tensor<1xi64>, %arg1: tensor<1xi64>) -> tensor<1xi64> { + %0 = "tf.Sub"(%arg0, %arg1) : (tensor<1xi64>, tensor<1xi64>) -> tensor<1xi64> + func.return %0: tensor<1xi64> + +// CHECK-LABEL: sub +// CHECK: tfl.sub %arg0, %arg1 {fused_activation_function = "NONE"} : tensor<1xi64> +// CHECK: return +} + +// CHECK-LABEL: testAddHighDimsHaveSameShape +func.func @testAddHighDimsHaveSameShape(%arg0: tensor<1x2x3x4x5x6x7x8xi32>, %arg1: tensor<1x2x3x4x5x6x7x8xi32>) -> tensor<1x2x3x4x5x6x7x8xi32> { + // CHECK: tfl.add %arg0, %arg1 {fused_activation_function = "NONE"} + %0 = "tf.Add"(%arg0, %arg1) : (tensor<1x2x3x4x5x6x7x8xi32>, tensor<1x2x3x4x5x6x7x8xi32>) -> tensor<1x2x3x4x5x6x7x8xi32> + func.return %0 : tensor<1x2x3x4x5x6x7x8xi32> +} + +func.func @LeakyRelu(%arg0: tensor<1xf32>) -> tensor<1xf32> { + %2 = "tf.LeakyRelu"(%arg0) {alpha = 0.1 : f32} : (tensor<1xf32>) -> tensor<1xf32> + func.return %2: tensor<1xf32> + +// CHECK-LABEL: LeakyRelu +// CHECK: "tfl.leaky_relu"(%arg0) <{alpha = 1.000000e-01 : f32}> : (tensor<1xf32>) -> tensor<1xf32> +} + +func.func @biasAdd(%arg0: tensor<1x10x10x32xf32>, %arg1: tensor<32xf32>) -> tensor<1x10x10x32xf32> { + %0 = "tf.BiasAdd"(%arg0, %arg1) {T = "tfdtype$DT_FLOAT", data_format = "NHWC"} : (tensor<1x10x10x32xf32>, tensor<32xf32>) -> tensor<1x10x10x32xf32> + func.return %0 : tensor<1x10x10x32xf32> + +// CHECK-LABEL: biasAdd +// CHECK: tfl.add(%arg0, %arg1) <{fused_activation_function = "NONE"}> : (tensor<1x10x10x32xf32>, tensor<32xf32>) -> tensor<1x10x10x32xf32> +} + +func.func @biasAddInt(%arg0: tensor<1x10x10x32xi32>, %arg1: tensor<32xi32>) -> tensor<1x10x10x32xi32> { + %0 = "tf.BiasAdd"(%arg0, %arg1) {T = "tfdtype$DT_FLOAT", data_format = "NHWC"} : (tensor<1x10x10x32xi32>, tensor<32xi32>) -> tensor<1x10x10x32xi32> + func.return %0 : tensor<1x10x10x32xi32> + +// CHECK-LABEL: biasAddInt +// CHECK: "tf.BiasAdd"(%arg0, %arg1) +} + +func.func @squeezeAndReshape(%arg0: tensor<1x1x10xf32>, %arg1: tensor) -> i32 { + %0 = "tf.Squeeze"(%arg0) {squeeze_dims = [0]} : (tensor<1x1x10xf32>) -> tensor<1x10xf32> + %1 = "tf.Squeeze"(%arg1) : (tensor) -> tensor<*xf32> + %2 = "tf.Const"() { value = dense<[2, 5]> : tensor<2xi32> } : () -> tensor<2xi32> + %3 = "tf.Reshape" (%0, %2) : (tensor<1x10xf32>, tensor<2xi32>) -> tensor<2x5xf32> + %4 = "tf.some_op"(%1, %3) : (tensor<*xf32>, tensor<2x5xf32>) -> i32 + func.return %4 : i32 +// CHECK-LABEL: squeezeAndReshape +// CHECK: "tfl.squeeze"(%arg0) <{squeeze_dims = [0]}> : (tensor<1x1x10xf32>) -> tensor<1x10xf32> +// CHECK: %1 = "tfl.squeeze"(%arg1) <{squeeze_dims = []}> : (tensor) -> tensor<*xf32> +// CHECK: %cst = arith.constant dense<[2, 5]> : tensor<2xi32> +// CHECK: %2 = "tfl.reshape"(%0, %cst) : (tensor<1x10xf32>, tensor<2xi32>) -> tensor<2x5xf32> +// CHECK: %3 = "tf.some_op"(%1, %2) : (tensor<*xf32>, tensor<2x5xf32>) -> i32 +// CHECK: return +} + +func.func @dynamicReshape(%arg0: tensor<*xf32>, %arg1: tensor<2xi32>) -> tensor { + %0 = "tf.Reshape"(%arg0, %arg1) : (tensor<*xf32>, tensor<2xi32>) -> tensor + func.return %0 : tensor + +// CHECK-LABEL: dynamicReshape +// CHECK-NEXT: %[[reshape:.*]] = "tfl.reshape"(%arg0, %arg1) : (tensor<*xf32>, tensor<2xi32>) -> tensor +// CHECK-NEXT: return %[[reshape]] : tensor +} + +func.func @dynamicReshapeI64(%arg0: tensor<*xf32>, %arg1: tensor<2xi64>) -> tensor { + %0 = "tf.Reshape"(%arg0, %arg1) : (tensor<*xf32>, tensor<2xi64>) -> tensor + func.return %0 : tensor + +// CHECK-LABEL: dynamicReshapeI64 +// CHECK-NEXT: %[[cast:.*]] = "tfl.cast"(%arg1) : (tensor<2xi64>) -> tensor<2xi32> +// CHECK-NEXT: %[[reshape:.*]] = "tfl.reshape"(%arg0, %[[cast]]) : (tensor<*xf32>, tensor<2xi32>) -> tensor +// CHECK-NEXT: return %[[reshape]] : tensor +} + +func.func @dynamicReshapeI64Fold(%arg0: tensor<*xf32>) -> tensor<1x2xf32> { + %cst = arith.constant dense<[1, 2]> : tensor<2xi64> + %0 = "tf.Reshape"(%arg0, %cst) : (tensor<*xf32>, tensor<2xi64>) -> tensor<1x2xf32> + func.return %0 : tensor<1x2xf32> + +// CHECK-LABEL: dynamicReshapeI64Fold +// CHECK: %[[cst:.*]] = arith.constant dense<[1, 2]> : tensor<2xi32> +// CHECK-NEXT: %[[reshape:.*]] = "tfl.reshape"(%arg0, %[[cst]]) : (tensor<*xf32>, tensor<2xi32>) -> tensor<1x2xf32> +// CHECK-NEXT: return %[[reshape]] : tensor<1x2xf32> +} + +func.func @dynamicReshapeI64Unranked(%arg0: tensor<*xf32>, %arg1: tensor<*xi64>) -> tensor<*xf32> { + %0 = "tf.Reshape"(%arg0, %arg1) : (tensor<*xf32>, tensor<*xi64>) -> tensor<*xf32> + func.return %0 : tensor<*xf32> + +// CHECK-LABEL: dynamicReshapeI64Unranked +// CHECK-NEXT: %[[cast:.*]] = "tfl.cast"(%arg1) : (tensor<*xi64>) -> tensor<*xi32> +// CHECK-NEXT: %[[reshape:.*]] = "tfl.reshape"(%arg0, %[[cast]]) : (tensor<*xf32>, tensor<*xi32>) -> tensor<*xf32> +// CHECK-NEXT: return %[[reshape]] : tensor<*xf32> +} + +func.func @avgPool2D(%arg0: tensor<1x6x6x16xf32>) -> tensor<1x1x1x16xf32> { + // OK + %0 = "tf.AvgPool"(%arg0) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", ksize = [1, 3, 6, 1], padding = "VALID", strides = [1, 3, 1, 1]} : (tensor<1x6x6x16xf32>) -> tensor<1x1x1x16xf32> + // Unsupported ksize + %1 = "tf.AvgPool"(%arg0) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", ksize = [3, 3, 6, 1], padding = "VALID", strides = [1, 3, 1, 1]} : (tensor<1x6x6x16xf32>) -> tensor<1x1x1x16xf32> + // Unsupported strides + %2 = "tf.AvgPool"(%arg0) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", ksize = [1, 3, 6, 1], padding = "VALID", strides = [1, 3, 1, 3]} : (tensor<1x6x6x16xf32>) -> tensor<1x1x1x16xf32> + + %5 = arith.addf %0, %1 : tensor<1x1x1x16xf32> + %6 = arith.addf %2, %5 : tensor<1x1x1x16xf32> + func.return %6 : tensor<1x1x1x16xf32> + +// CHECK-LABEL: func @avgPool2D +// CHECK: "tfl.average_pool_2d"(%arg0) <{filter_height = 3 : i32, filter_width = 6 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 3 : i32, stride_w = 1 : i32}> : (tensor<1x6x6x16xf32>) -> tensor<1x1x1x16xf32> +// CHECK: %1 = "tf.AvgPool"(%arg0) +// CHECK: %2 = "tf.AvgPool"(%arg0) +} + +func.func @avgPool2DChannelFirst(%arg0: tensor<1x16x6x6xf32>) -> tensor<1x16x1x1xf32> { + // OK + %0 = "tf.AvgPool"(%arg0) {T = "tfdtype$DT_FLOAT", data_format = "NCHW", ksize = [1, 1, 3, 6], padding = "VALID", strides = [1, 1, 3, 1]} : (tensor<1x16x6x6xf32>) -> tensor<1x16x1x1xf32> + // Unsupported ksize + %1 = "tf.AvgPool"(%arg0) {T = "tfdtype$DT_FLOAT", data_format = "NCHW", ksize = [3, 1, 3, 6], padding = "VALID", strides = [1, 1, 3, 1]} : (tensor<1x16x6x6xf32>) -> tensor<1x16x1x1xf32> + // Unsupported strides + %2 = "tf.AvgPool"(%arg0) {T = "tfdtype$DT_FLOAT", data_format = "NCHW", ksize = [1, 1, 3, 6], padding = "VALID", strides = [1, 3, 3, 1]} : (tensor<1x16x6x6xf32>) -> tensor<1x16x1x1xf32> + + %5 = arith.addf %0, %1 : tensor<1x16x1x1xf32> + %6 = arith.addf %2, %5 : tensor<1x16x1x1xf32> + func.return %6 : tensor<1x16x1x1xf32> + +// CHECK-LABEL: func @avgPool2DChannelFirst +// CHECK: %cst = arith.constant dense<[0, 2, 3, 1]> : tensor<4xi32> +// CHECK: %0 = "tfl.transpose"(%arg0, %cst) : (tensor<1x16x6x6xf32>, tensor<4xi32>) -> tensor<1x6x6x16xf32> +// CHECK: %1 = "tfl.average_pool_2d"(%0) <{filter_height = 3 : i32, filter_width = 6 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 3 : i32, stride_w = 1 : i32}> : (tensor<1x6x6x16xf32>) -> tensor<1x1x1x16xf32> +// CHECK: %cst_0 = arith.constant dense<[0, 3, 1, 2]> : tensor<4xi32> +// CHECK: %2 = "tfl.transpose"(%1, %cst_0) : (tensor<1x1x1x16xf32>, tensor<4xi32>) -> tensor<1x16x1x1xf32> +// CHECK: %3 = "tf.AvgPool"(%arg0) +// CHECK: %4 = "tf.AvgPool"(%arg0) +} + +func.func @softmax(%arg0: tensor<8x16xf32>) -> tensor<8x16xf32> { + %0 = "tf.Softmax"(%arg0) : (tensor<8x16xf32>) -> tensor<8x16xf32> + func.return %0 : tensor<8x16xf32> + +// CHECK-LABEL: softmax +// CHECK: "tfl.softmax"(%arg0) <{beta = 1.000000e+00 : f32}> : (tensor<8x16xf32>) -> tensor<8x16xf32> +} + +func.func @softsign(%arg0: tensor<8x16xf32>) -> tensor<8x16xf32> { + %0 = "tf.Softsign"(%arg0) : (tensor<8x16xf32>) -> tensor<8x16xf32> + func.return %0 : tensor<8x16xf32> + + // CHECK-LABEL: softsign + // CHECK: %[[abs:.*]] = "tfl.abs"(%arg0) : (tensor<8x16xf32>) -> tensor<8x16xf32> + // CHECK: %[[cst:.*]] = arith.constant dense<1.000000e+00> : tensor + // CHECK: %[[add_result:.*]] = tfl.add(%[[abs]], %[[cst]]) <{fused_activation_function = "NONE"}> : (tensor<8x16xf32>, tensor) -> tensor<8x16xf32> + // CHECK: %[[RES0:.*]] = tfl.div %arg0, %[[add_result]] {fused_activation_function = "NONE"} : tensor<8x16xf32> +} + +func.func @softplus(%arg0: tensor<8x16xf32>) -> tensor<8x16xf32> { + %0 = "tf.Softplus"(%arg0) : (tensor<8x16xf32>) -> tensor<8x16xf32> + func.return %0 : tensor<8x16xf32> + +// CHECK-LABEL: softplus +// CHECK: %[[exp:.*]] = "tfl.exp"(%arg0) : (tensor<8x16xf32>) -> tensor<8x16xf32> +// CHECK: %[[cst:.*]] = arith.constant dense<1.000000e+00> : tensor +// CHECK: %[[add:.*]] = tfl.add(%[[exp]], %[[cst]]) <{fused_activation_function = "NONE"}> : (tensor<8x16xf32>, tensor) -> tensor<8x16xf32> +// CHECK: %[[log:.*]] = "tfl.log"(%[[add]]) : (tensor<8x16xf32>) -> tensor<8x16xf32> +} + +func.func @fakeQuantArgsFalse(%arg0: tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32> { + %0 = "tf.FakeQuantWithMinMaxArgs"(%arg0) {min = -0.1 : f32, max = 0.2 : f32, num_bits = 5, narrow_range = false} : (tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32> + func.return %0 : tensor<8x8x8x8xf32> + + // CHECK-LABEL: fakeQuantArgsFalse + // CHECK: "tfl.quantize"(%arg0) <{qtype = tensor<8x8x8x8x!quant.uniform>}> + // CHECK: %1 = "tfl.dequantize"(%0) : (tensor<8x8x8x8x!quant.uniform>) -> tensor<8x8x8x8xf32> +} + +func.func @fakeQuantArgsTrue(%arg0: tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32> { + %0 = "tf.FakeQuantWithMinMaxArgs"(%arg0) {min = -0.1 : f32, max = 0.2 : f32, num_bits = 5, narrow_range = true} : (tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32> + func.return %0 : tensor<8x8x8x8xf32> + + // CHECK-LABEL: fakeQuantArgsTrue + // CHECK: "tfl.quantize"(%arg0) <{qtype = tensor<8x8x8x8x!quant.uniform:f32, 0.001181102379804521:86>>}> : (tensor<8x8x8x8xf32>) -> tensor<8x8x8x8x!quant.uniform:f32, 0.001181102379804521:86>> + // CHECK: %1 = "tfl.dequantize"(%0) : (tensor<8x8x8x8x!quant.uniform:f32, 0.001181102379804521:86>>) -> tensor<8x8x8x8xf32> +} + +func.func @fakeQuantVarsFalse(%arg0: tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32> { + %arg1 = "tf.Const"() { value = dense<-0.1> : tensor } : () -> tensor + %arg2 = "tf.Const"() { value = dense<0.2> : tensor } : () -> tensor + %0 = "tf.FakeQuantWithMinMaxVars"(%arg0, %arg1, %arg2) {num_bits = 5, narrow_range = false} : (tensor<8x8x8x8xf32>, tensor, tensor) -> tensor<8x8x8x8xf32> + func.return %0 : tensor<8x8x8x8xf32> + + // CHECK-LABEL: fakeQuantVarsFalse + // CHECK: "tfl.quantize"(%arg0) <{qtype = tensor<8x8x8x8x!quant.uniform>}> + // CHECK: %1 = "tfl.dequantize"(%0) : (tensor<8x8x8x8x!quant.uniform>) -> tensor<8x8x8x8xf32> +} + +func.func @fakeQuantVarsTrue(%arg0: tensor<8x8x8x8xf32>, %arg1: tensor, %arg2: tensor) -> tensor<8x8x8x8xf32> { + %0 = "tf.FakeQuantWithMinMaxVars"(%arg0, %arg1, %arg2) {min = 0.0 : f32, max = 1.0 : f32, num_bits = 5, narrow_range = true} : (tensor<8x8x8x8xf32>, tensor, tensor) -> tensor<8x8x8x8xf32> + func.return %0 : tensor<8x8x8x8xf32> + + // CHECK-LABEL: fakeQuantVarsTrue + // CHECK: "tf.FakeQuantWithMinMaxVars"(%arg0, %arg1, %arg2) <{narrow_range = true, num_bits = 5 : i64}> {max = 1.000000e+00 : f32, min = 0.000000e+00 : f32} +} + +func.func @fakeQuantArgsFalse4Bits(%arg0: tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32> { + %0 = "tf.FakeQuantWithMinMaxArgs"(%arg0) {min = -0.1 : f32, max = 0.2 : f32, num_bits = 3, narrow_range = false} : (tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32> + func.return %0 : tensor<8x8x8x8xf32> + + // CHECK-LABEL: fakeQuantArgsFalse + // CHECK: "tfl.quantize"(%arg0) <{qtype = tensor<8x8x8x8x!quant.uniform>}> + // CHECK: %1 = "tfl.dequantize"(%0) : (tensor<8x8x8x8x!quant.uniform>) -> tensor<8x8x8x8xf32> +} + +func.func @fakeQuantArgsTrue4Bits(%arg0: tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32> { + %0 = "tf.FakeQuantWithMinMaxArgs"(%arg0) {min = -0.1 : f32, max = 0.2 : f32, num_bits = 3, narrow_range = true} : (tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32> + func.return %0 : tensor<8x8x8x8xf32> + + // CHECK-LABEL: fakeQuantArgsTrue + // CHECK: "tfl.quantize"(%arg0) <{qtype = tensor<8x8x8x8x!quant.uniform:f32, 0.021428571747882024:6>>}> : (tensor<8x8x8x8xf32>) -> tensor<8x8x8x8x!quant.uniform:f32, 0.021428571747882024:6>> + // CHECK: %1 = "tfl.dequantize"(%0) : (tensor<8x8x8x8x!quant.uniform:f32, 0.021428571747882024:6>>) -> tensor<8x8x8x8xf32> +} + +func.func @fakeQuantVarsFalse4Bits(%arg0: tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32> { + %arg1 = "tf.Const"() { value = dense<-0.1> : tensor } : () -> tensor + %arg2 = "tf.Const"() { value = dense<0.2> : tensor } : () -> tensor + %0 = "tf.FakeQuantWithMinMaxVars"(%arg0, %arg1, %arg2) {num_bits = 3, narrow_range = false} : (tensor<8x8x8x8xf32>, tensor, tensor) -> tensor<8x8x8x8xf32> + func.return %0 : tensor<8x8x8x8xf32> + + // CHECK-LABEL: fakeQuantVarsFalse + // CHECK: "tfl.quantize"(%arg0) <{qtype = tensor<8x8x8x8x!quant.uniform>}> + // CHECK: %1 = "tfl.dequantize"(%0) : (tensor<8x8x8x8x!quant.uniform>) -> tensor<8x8x8x8xf32> +} + +func.func @fakeQuantVarsTrue4Bits(%arg0: tensor<8x8x8x8xf32>, %arg1: tensor, %arg2: tensor) -> tensor<8x8x8x8xf32> { + %0 = "tf.FakeQuantWithMinMaxVars"(%arg0, %arg1, %arg2) {min = 0.0 : f32, max = 1.0 : f32, num_bits = 3, narrow_range = true} : (tensor<8x8x8x8xf32>, tensor, tensor) -> tensor<8x8x8x8xf32> + func.return %0 : tensor<8x8x8x8xf32> + + // CHECK-LABEL: fakeQuantVarsTrue + // CHECK: "tf.FakeQuantWithMinMaxVars"(%arg0, %arg1, %arg2) <{narrow_range = true, num_bits = 3 : i64}> {max = 1.000000e+00 : f32, min = 0.000000e+00 : f32} +} + +func.func @const() -> tensor<2xi32> { + %0 = "tf.Const"() {device = "", name = "weights_quant/min", dtype = "tfdtype$DT_INT32", value = #tf_type : tensor<2xi32>} : () -> (tensor<2xi32>) + func.return %0: tensor<2xi32> + +// CHECK-LABEL: @const +// CHECK: "tfl.pseudo_const"() <{value = #tf_type : tensor<2xi32>}> : () -> tensor<2xi32> +} + +func.func @shape(%arg0: tensor) -> tensor<2xi32> { + %0 = "tf.Shape"(%arg0) {T = "tfdtype$DT_FLOAT", out_type = "tfdtype$DT_INT32"} : (tensor) -> tensor<2xi32> + %1 = "tf.Shape"(%arg0) {T = "tfdtype$DT_FLOAT"} : (tensor) -> tensor<2xi32> + %2 = "tf.Add"(%0, %1) : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi32> + func.return %2: tensor<2xi32> + +// CHECK-LABEL: shape +// CHECK: "tfl.shape"(%arg0) : (tensor) -> tensor<2xi32> +} + +func.func @fill(%arg0: tensor<3xi32>, %arg1: tensor) -> tensor { + %0 = "tf.Fill"(%arg0, %arg1) : (tensor<3xi32>, tensor) -> tensor + func.return %0 : tensor + +// CHECK-LABEL:fill +// CHECK: "tfl.fill"(%arg0, %arg1) : (tensor<3xi32>, tensor) -> tensor +} + +func.func @argmin(%arg0: tensor<3xi32>, %arg1: tensor) -> tensor { + %0 = "tf.ArgMin"(%arg0, %arg1) : (tensor<3xi32>, tensor) -> tensor + func.return %0 : tensor + +// CHECK-LABEL: argmin +// CHECK: "tfl.arg_min"(%arg0, %arg1) : (tensor<3xi32>, tensor) -> tensor +} + +func.func @sigmoid(%arg0: tensor) -> tensor { + %0 = "tf.Sigmoid"(%arg0) : (tensor) -> tensor + func.return %0 : tensor +// CHECK-LABEL: sigmoid +// CHECK: "tfl.logistic"(%arg0) : (tensor) -> tensor +} + +func.func @sqrt(%arg0: tensor<8x16xf32>) -> tensor<8x16xf32> { + %0 = "tf.Sqrt"(%arg0) : (tensor<8x16xf32>) -> tensor<8x16xf32> + func.return %0 : tensor<8x16xf32> +// CHECK-LABEL: sqrt +// CHECK: "tfl.sqrt"(%arg0) : (tensor<8x16xf32>) -> tensor<8x16xf32> +} + +func.func @square(%arg0: tensor<8x16xf32>) -> tensor<8x16xf32> { + %0 = "tf.Square"(%arg0) : (tensor<8x16xf32>) -> tensor<8x16xf32> + func.return %0 : tensor<8x16xf32> +// CHECK-LABEL: square +// CHECK: "tfl.square"(%arg0) : (tensor<8x16xf32>) -> tensor<8x16xf32> +} + +func.func @neg(%arg0: tensor<8x16xf32>) -> tensor<8x16xf32> { + %0 = "tf.Neg"(%arg0) : (tensor<8x16xf32>) -> tensor<8x16xf32> + func.return %0 : tensor<8x16xf32> +// CHECK-LABEL: neg +// CHECK: "tfl.neg"(%arg0) : (tensor<8x16xf32>) -> tensor<8x16xf32> +} + +func.func @log(%arg0: tensor<8x16xf32>) -> tensor<8x16xf32> { + %0 = "tf.Log"(%arg0) : (tensor<8x16xf32>) -> tensor<8x16xf32> + func.return %0 : tensor<8x16xf32> +// CHECK-LABEL: log +// CHECK: "tfl.log"(%arg0) : (tensor<8x16xf32>) -> tensor<8x16xf32> +} + +func.func @log_softmax(%arg0: tensor<8x16xf32>) -> tensor<8x16xf32> { + %0 = "tf.LogSoftmax"(%arg0) : (tensor<8x16xf32>) -> tensor<8x16xf32> + func.return %0 : tensor<8x16xf32> +// CHECK-LABEL: log_softmax +// CHECK: "tfl.log_softmax"(%arg0) : (tensor<8x16xf32>) -> tensor<8x16xf32> +} + +func.func @zeros_like(%arg0: tensor<8x16xf32>) -> tensor<8x16xf32> { + %0 = "tf.ZerosLike"(%arg0) : (tensor<8x16xf32>) -> tensor<8x16xf32> + func.return %0 : tensor<8x16xf32> +// CHECK-LABEL: zeros_like +// CHECK: "tfl.zeros_like"(%arg0) : (tensor<8x16xf32>) -> tensor<8x16xf32> +} + +func.func @div(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>) -> tensor<1xf32> { + %0 = "tf.Div"(%arg0, %arg1) : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + func.return %0: tensor<1xf32> + +// CHECK-LABEL: div +// CHECK: tfl.div %arg0, %arg1 {fused_activation_function = "NONE"} : tensor<1xf32> +// CHECK: return +} + +func.func @squaredDifferenceRelu(tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> { +^bb0(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>): + %0 = "tf.SquaredDifference"(%arg0, %arg1) : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> + %1 = "tf.Relu6"(%0) : (tensor<1xf32>) -> tensor<1xf32> + func.return %1: tensor<1xf32> + +// CHECK-LABEL: squaredDifferenceRelu +// CHECK: tfl.squared_difference %arg0, %arg1 : tensor<1xf32> +// CHECK: %1 = "tfl.relu6"(%0) : (tensor<1xf32>) -> tensor<1xf32> +// CHECK: return +} + +func.func @maxPool2D(%arg0: tensor<1x1x1x16xf32>) -> tensor<1x1x1x16xf32> { + // OK + %0 = "tf.MaxPool"(%arg0) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", ksize = [1, 3, 6, 1], padding = "VALID", strides = [1, 3, 1, 1]} : (tensor<1x1x1x16xf32>) -> tensor<1x1x1x16xf32> + + // Unsupported ksize + %1 = "tf.MaxPool"(%arg0) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", ksize = [3, 3, 6, 1], padding = "VALID", strides = [1, 3, 1, 1]} : (tensor<1x1x1x16xf32>) -> tensor<1x1x1x16xf32> + + // Unsupported strides + %2 = "tf.MaxPool"(%arg0) {T = "tfdtype$DT_FLOAT", data_format = "NHWC", ksize = [1, 3, 6, 1], padding = "VALID", strides = [1, 3, 1, 3]} : (tensor<1x1x1x16xf32>) -> tensor<1x1x1x16xf32> + + %5 = arith.addf %0, %1 : tensor<1x1x1x16xf32> + %6 = arith.addf %2, %5 : tensor<1x1x1x16xf32> + func.return %6 : tensor<1x1x1x16xf32> + +// CHECK-LABEL: func @maxPool2D +// CHECK: "tfl.max_pool_2d"(%arg0) <{filter_height = 3 : i32, filter_width = 6 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 3 : i32, stride_w = 1 : i32}> : (tensor<1x1x1x16xf32>) -> tensor<1x1x1x16xf32> +// CHECK: %1 = "tf.MaxPool"(%arg0) +// CHECK: %2 = "tf.MaxPool"(%arg0) +} + +func.func @maxPool2DChannelFirst(%arg0: tensor<1x16x6x6xf32>) -> tensor<1x16x1x1xf32> { + // OK + %0 = "tf.MaxPool"(%arg0) {T = "tfdtype$DT_FLOAT", data_format = "NCHW", ksize = [1, 1, 3, 6], padding = "VALID", strides = [1, 1, 3, 1]} : (tensor<1x16x6x6xf32>) -> tensor<1x16x1x1xf32> + + // Unsupported ksize + %1 = "tf.MaxPool"(%arg0) {T = "tfdtype$DT_FLOAT", data_format = "NCHW", ksize = [3, 1, 3, 6], padding = "VALID", strides = [1, 1, 3, 1]} : (tensor<1x16x6x6xf32>) -> tensor<1x16x1x1xf32> + + // Unsupported strides + %2 = "tf.MaxPool"(%arg0) {T = "tfdtype$DT_FLOAT", data_format = "NCHW", ksize = [1, 1, 3, 6], padding = "VALID", strides = [1, 3, 3, 1]} : (tensor<1x16x6x6xf32>) -> tensor<1x16x1x1xf32> + + %5 = arith.addf %0, %1 : tensor<1x16x1x1xf32> + %6 = arith.addf %2, %5 : tensor<1x16x1x1xf32> + func.return %6 : tensor<1x16x1x1xf32> + +// CHECK-LABEL: func @maxPool2DChannelFirst +// CHECK: %cst = arith.constant dense<[0, 2, 3, 1]> : tensor<4xi32> +// CHECK: %0 = "tfl.transpose"(%arg0, %cst) : (tensor<1x16x6x6xf32>, tensor<4xi32>) -> tensor<1x6x6x16xf32> +// CHECK: %1 = "tfl.max_pool_2d"(%0) <{filter_height = 3 : i32, filter_width = 6 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 3 : i32, stride_w = 1 : i32}> : (tensor<1x6x6x16xf32>) -> tensor<1x1x1x16xf32> +// CHECK: %cst_0 = arith.constant dense<[0, 3, 1, 2]> : tensor<4xi32> +// CHECK: %2 = "tfl.transpose"(%1, %cst_0) : (tensor<1x1x1x16xf32>, tensor<4xi32>) -> tensor<1x16x1x1xf32> +// CHECK: %3 = "tf.MaxPool"(%arg0) +// CHECK: %4 = "tf.MaxPool"(%arg0) +} + +func.func @abs(%arg0: tensor<8x16xf32>) -> tensor<8x16xf32> { + %0 = "tf.Abs"(%arg0) : (tensor<8x16xf32>) -> tensor<8x16xf32> + func.return %0 : tensor<8x16xf32> + +// CHECK-LABEL:abs +// CHECK: "tfl.abs"(%arg0) : (tensor<8x16xf32>) -> tensor<8x16xf32> +} + +func.func @any(%arg0: tensor<2x2xi1>, %arg1: tensor) -> tensor { + %0 = "tf.Any"(%arg0, %arg1) {keep_dims = false} : (tensor<2x2xi1>, tensor) -> tensor + func.return %0 : tensor + +// CHECK-LABEL:any +// CHECK: "tfl.reduce_any"(%arg0, %arg1) <{keep_dims = false}> : (tensor<2x2xi1>, tensor) -> tensor +} + +func.func @any_i64axes(%arg0: tensor<8x16x16xi1>, %arg1: tensor<2xi64>) -> tensor { + %0 = "tf.Any"(%arg0, %arg1) {keep_dims = false} : (tensor<8x16x16xi1>, tensor<2xi64>) -> tensor + func.return %0 : tensor + + // CHECK-LABEL: any_i64axes + // CHECK: %[[V0:.*]] = "tfl.cast"(%arg1) : (tensor<2xi64>) -> tensor<2xi32> + // CHECK: "tfl.reduce_any"(%arg0, %[[V0]]) <{keep_dims = false}> : (tensor<8x16x16xi1>, tensor<2xi32>) -> tensor +} + +func.func @ceil(%arg0: tensor<8x16xf32>) -> tensor<8x16xf32> { + %0 = "tf.Ceil"(%arg0) : (tensor<8x16xf32>) -> tensor<8x16xf32> + func.return %0 : tensor<8x16xf32> + +// CHECK-LABEL: ceil +// CHECK: "tfl.ceil"(%arg0) : (tensor<8x16xf32>) -> tensor<8x16xf32> +// CHECK: return +} + +func.func @cos(%arg0: tensor) -> tensor { + %0 = "tf.Cos"(%arg0) : (tensor) -> tensor + func.return %0 : tensor + +// CHECK-LABEL:cos +// CHECK: "tfl.cos"(%arg0) : (tensor) -> tensor +} + +func.func @elu(%arg0: tensor<11x16xf32>) -> tensor<11x16xf32> { + %0 = "tf.Elu"(%arg0) : (tensor<11x16xf32>) -> tensor<11x16xf32> + func.return %0 : tensor<11x16xf32> + +// CHECK-LABEL:elu +// CHECK: "tfl.elu"(%arg0) : (tensor<11x16xf32>) -> tensor<11x16xf32> +} + +func.func @expandDims(%arg0: tensor<2x2xf32>, %arg1: tensor) -> tensor<1x2x2xf32> { + %0 = "tf.ExpandDims"(%arg0, %arg1) : (tensor<2x2xf32>, tensor) -> tensor<1x2x2xf32> + func.return %0 : tensor<1x2x2xf32> + +// CHECK-LABEL:expandDims +// CHECK: "tfl.expand_dims"(%arg0, %arg1) : (tensor<2x2xf32>, tensor) -> tensor<1x2x2xf32> +} + +func.func @squeezeDefault(%arg0: tensor<1x2x2xf32>) -> tensor<2x2xf32> { + %0 = "tf.Squeeze"(%arg0) : (tensor<1x2x2xf32>) -> tensor<2x2xf32> + func.return %0 : tensor<2x2xf32> + +// CHECK-LABEL:squeezeDefault +// CHECK: "tfl.squeeze"(%arg0) <{squeeze_dims = []}> : (tensor<1x2x2xf32>) -> tensor<2x2xf32> +} + +func.func @squeezeSingleAxis(%arg0: tensor<2x1x2xf32>) -> tensor<2x2xf32> { + %0 = "tf.Squeeze"(%arg0) {squeeze_dims = [1]} : (tensor<2x1x2xf32>) -> tensor<2x2xf32> + func.return %0 : tensor<2x2xf32> + +// CHECK-LABEL:squeezeSingleAxis +// CHECK: "tfl.squeeze"(%arg0) <{squeeze_dims = [1]}> : (tensor<2x1x2xf32>) -> tensor<2x2xf32> +} + +func.func @squeezeTwoAxes(%arg0: tensor<1x2x1x2xf32>) -> tensor<2x2xf32> { + %0 = "tf.Squeeze"(%arg0) {squeeze_dims = [0, 2]} : (tensor<1x2x1x2xf32>) -> tensor<2x2xf32> + func.return %0 : tensor<2x2xf32> + +// CHECK-LABEL:squeezeTwoAxes +// CHECK: "tfl.squeeze"(%arg0) <{squeeze_dims = [0, 2]}> : (tensor<1x2x1x2xf32>) -> tensor<2x2xf32> +} + +func.func @gatherScalarIndices(%arg0 : tensor<3x2xf32>, %arg1 : tensor) -> tensor<2xf32> { + %0 = "tf.Gather"(%arg0, %arg1) : (tensor<3x2xf32>, tensor) -> tensor<2xf32> + func.return %0 : tensor<2xf32> + +// CHECK-LABEL:gatherScalarIndices +// CHECK: "tfl.gather"(%arg0, %arg1) <{axis = 0 : i32, batch_dims = 0 : i32}> : (tensor<3x2xf32>, tensor) -> tensor<2xf32> +} + +func.func @gatherVectorIndices(%arg0 : tensor<2xf32>, %arg1 : tensor<3xi32>) -> tensor<3xf32> { + %0 = "tf.Gather"(%arg0, %arg1) : (tensor<2xf32>, tensor<3xi32>) -> tensor<3xf32> + func.return %0 : tensor<3xf32> + +// CHECK-LABEL:gatherVectorIndices +// CHECK: "tfl.gather"(%arg0, %arg1) <{axis = 0 : i32, batch_dims = 0 : i32}> : (tensor<2xf32>, tensor<3xi32>) -> tensor<3xf32> +} + +func.func @gatherHigherRankIndices(%arg0 : tensor<2x3x6xf32>, %arg1 : tensor<4x5xi32>) -> tensor<4x5x3x6xf32> { + %0 = "tf.Gather"(%arg0, %arg1) : (tensor<2x3x6xf32>, tensor<4x5xi32>) -> tensor<4x5x3x6xf32> + func.return %0 : tensor<4x5x3x6xf32> + +// CHECK-LABEL:gatherHigherRankIndices +// CHECK: "tfl.gather"(%arg0, %arg1) <{axis = 0 : i32, batch_dims = 0 : i32}> : (tensor<2x3x6xf32>, tensor<4x5xi32>) -> tensor<4x5x3x6xf32> +} + +func.func @gatherNdVectorIndices(%arg0 : tensor<3x2x2xf32>, %arg1 : tensor<2xi32>) -> tensor<2xf32> { + %0 = "tf.GatherNd"(%arg0, %arg1) : (tensor<3x2x2xf32>, tensor<2xi32>) -> tensor<2xf32> + func.return %0 : tensor<2xf32> + +// CHECK-LABEL:gatherNdVectorIndices +// CHECK: "tfl.gather_nd"(%arg0, %arg1) : (tensor<3x2x2xf32>, tensor<2xi32>) -> tensor<2xf32> +} + +func.func @gatherNdHigherRankIndices(%arg0 : tensor<4x3x2xf32>, %arg1 : tensor<2x2xi32>) -> tensor<2x2xf32> { + %0 = "tf.GatherNd"(%arg0, %arg1) : (tensor<4x3x2xf32>, tensor<2x2xi32>) -> tensor<2x2xf32> + func.return %0 : tensor<2x2xf32> + +// CHECK-LABEL:gatherNdHigherRankIndices +// CHECK: "tfl.gather_nd"(%arg0, %arg1) : (tensor<4x3x2xf32>, tensor<2x2xi32>) -> tensor<2x2xf32> +} + +func.func @scatterNdVectorIndices(%arg0: tensor<5x1xi32>, %arg1: tensor<5x3x2xf32>) -> tensor<10x3x2xf32> { + %cst = "tf.Const"() { value = dense<[10, 3, 2]> : tensor<3xi32> } : () -> tensor<3xi32> + %1 = "tf.ScatterNd"(%arg0, %arg1, %cst) : (tensor<5x1xi32>, tensor<5x3x2xf32>, tensor<3xi32>) -> tensor<10x3x2xf32> + func.return %1 : tensor<10x3x2xf32> + +// CHECK-LABEL:scatterNdVectorIndices +// CHECK: %[[CST:.*]] = arith.constant dense<[10, 3, 2]> : tensor<3xi32> +// CHECK: %[[RES:.*]] = "tfl.scatter_nd"(%arg0, %arg1, %[[CST]]) : (tensor<5x1xi32>, tensor<5x3x2xf32>, tensor<3xi32>) -> tensor<10x3x2xf32> +// CHECK: return %[[RES]] +} + +func.func @scatterNdHigherRankIndices(%arg0: tensor<4x2x2xi32>, %arg1: tensor<4x2x3xf32>, %arg2: tensor<3xi32>) -> tensor<10x2x3xf32> { + %0 = "tf.ScatterNd"(%arg0, %arg1, %arg2) : (tensor<4x2x2xi32>, tensor<4x2x3xf32>, tensor<3xi32>) -> tensor<10x2x3xf32> + func.return %0 : tensor<10x2x3xf32> + +// CHECK-LABEL:scatterNdHigherRankIndices +// CHECK: %[[RES:.*]] = "tfl.scatter_nd"(%arg0, %arg1, %arg2) : (tensor<4x2x2xi32>, tensor<4x2x3xf32>, tensor<3xi32>) -> tensor<10x2x3xf32> +// CHECK: return %[[RES]] +} + +func.func @scatter_nd_i64(%arg0: tensor<4x2x2xi64>, %arg1: tensor<4x2x3xf32>, %arg2: tensor<3xi64>) -> tensor<10x2x3xf32> { + %0 = "tf.ScatterNd"(%arg0, %arg1, %arg2) : (tensor<4x2x2xi64>, tensor<4x2x3xf32>, tensor<3xi64>) -> tensor<10x2x3xf32> + func.return %0 : tensor<10x2x3xf32> + +// CHECK-LABEL:scatter_nd_i64 +// CHECK: "tfl.cast" +// CHECK: "tfl.cast" +// CHECK: "tfl.scatter_nd" +} + +func.func @gatherV2VectorIndices(%arg0 : tensor<1x2x20xf32>, %arg1 : tensor<3x5xi32>) -> tensor<1x3x5x20xf32> { + %0 = "tf.Const"() { value = dense<[1]> : tensor<1xi32> } : () -> tensor<1xi32> + %1 = "tf.GatherV2"(%arg0, %arg1, %0) : (tensor<1x2x20xf32>, tensor<3x5xi32>, tensor<1xi32>) -> tensor<1x3x5x20xf32> + func.return %1 : tensor<1x3x5x20xf32> + +// CHECK-LABEL:gatherV2VectorIndices +// CHECK: "tfl.gather"(%arg0, %arg1) <{axis = 1 : i32, batch_dims = 0 : i32}> : (tensor<1x2x20xf32>, tensor<3x5xi32>) -> tensor<1x3x5x20xf32> +} + +func.func @gatherV2VectorIndices_I64Axis(%arg0 : tensor<1x2x20xf32>, %arg1 : tensor<3x5xi32>) -> tensor<1x3x5x20xf32> { + %0 = "tf.Const"() { value = dense<[1]> : tensor<1xi64> } : () -> tensor<1xi64> + %1 = "tf.GatherV2"(%arg0, %arg1, %0) : (tensor<1x2x20xf32>, tensor<3x5xi32>, tensor<1xi64>) -> tensor<1x3x5x20xf32> + func.return %1 : tensor<1x3x5x20xf32> + +// CHECK-LABEL:gatherV2VectorIndices_I64Axis +// CHECK: "tfl.gather"(%arg0, %arg1) <{axis = 1 : i32, batch_dims = 0 : i32}> : (tensor<1x2x20xf32>, tensor<3x5xi32>) -> tensor<1x3x5x20xf32> +} + +func.func @gatherV2VectorIndicesNegAxis(%arg0 : tensor<1x2x20xf32>, %arg1 : tensor<3x5xi32>) -> tensor<1x2x3x5xf32> { + %0 = "tf.Const"() { value = dense<[-1]> : tensor<1xi32> } : () -> tensor<1xi32> + %1 = "tf.GatherV2"(%arg0, %arg1, %0) : (tensor<1x2x20xf32>, tensor<3x5xi32>, tensor<1xi32>) -> tensor<1x2x3x5xf32> + func.return %1 : tensor<1x2x3x5xf32> + +// CHECK-LABEL:gatherV2VectorIndices +// CHECK: "tfl.gather"(%arg0, %arg1) <{axis = -1 : i32, batch_dims = 0 : i32}> : (tensor<1x2x20xf32>, tensor<3x5xi32>) -> tensor<1x2x3x5xf32> +} + +func.func @gatherWithBatchDims(%arg0 : tensor<2x3x6xf32>, %arg1 : tensor<2x5xi32>) -> tensor<2x5x3x6xf32> { + %0 = "tf.Const"() { value = dense<[1]> : tensor<1xi32> } : () -> tensor<1xi32> + %1 = "tf.GatherV2"(%arg0, %arg1, %0) {batch_dims = 1 : i64} : (tensor<2x3x6xf32>, tensor<2x5xi32>, tensor<1xi32>) -> tensor<2x5x3x6xf32> + func.return %1 : tensor<2x5x3x6xf32> + +// CHECK-LABEL:gatherWithBatchDims +// CHECK: "tfl.gather"(%arg0, %arg1) <{axis = 1 : i32, batch_dims = 1 : i32}> : (tensor<2x3x6xf32>, tensor<2x5xi32>) -> tensor<2x5x3x6xf32> +} + + + +func.func @greater(%arg0: tensor<8x16xf32>, %arg1: tensor<8x16xf32>) -> tensor<8x16xi1> { + %0 = "tf.Greater"(%arg0, %arg1) : (tensor<8x16xf32>, tensor<8x16xf32>) -> tensor<8x16xi1> + func.return %0 : tensor<8x16xi1> + +// CHECK-LABEL: greater +// CHECK: tfl.greater(%arg0, %arg1) : (tensor<8x16xf32>, tensor<8x16xf32>) -> tensor<8x16xi1> +// CHECK: return +} + +func.func @greater_equal(%arg0: tensor<8x16xf32>, %arg1: tensor<8x16xf32>) -> tensor<8x16xi1> { + %0 = "tf.GreaterEqual"(%arg0, %arg1) : (tensor<8x16xf32>, tensor<8x16xf32>) -> tensor<8x16xi1> + func.return %0 : tensor<8x16xi1> + +// CHECK-LABEL: greater_equal +// CHECK: tfl.greater_equal(%arg0, %arg1) : (tensor<8x16xf32>, tensor<8x16xf32>) -> tensor<8x16xi1> +// CHECK: return +} + +//TODO(b/136498739): Add failure test for non-broadcastable types, since currently +// we can't catch this error. +func.func @less_equal(%arg0: tensor<8x16xf32>, %arg1: tensor<8x16xf32>) -> tensor<8x16xi1> { + %0 = "tf.LessEqual"(%arg0, %arg1) : (tensor<8x16xf32>, tensor<8x16xf32>) -> tensor<8x16xi1> + func.return %0 : tensor<8x16xi1> + +// CHECK-LABEL: less_equal +// CHECK: tfl.less_equal(%arg0, %arg1) : (tensor<8x16xf32>, tensor<8x16xf32>) -> tensor<8x16xi1> +// CHECK: return +} + +func.func @rank(%arg0: tensor<*xf32>) -> tensor<1xi32> { + %0 = "tf.Rank"(%arg0) : (tensor<*xf32>) -> tensor<1xi32> + func.return %0 : tensor<1xi32> + +// CHECK-LABEL:rank +// CHECK: "tfl.rank"(%arg0) : (tensor<*xf32>) -> tensor<1xi32> +} + +func.func @floor(%arg0: tensor<8x16xf32>) -> tensor<8x16xf32> { + %0 = "tf.Floor"(%arg0) : (tensor<8x16xf32>) -> tensor<8x16xf32> + func.return %0 : tensor<8x16xf32> + +// CHECK-LABEL: floor +// CHECK: "tfl.floor"(%arg0) : (tensor<8x16xf32>) -> tensor<8x16xf32> +// CHECK: return +} + +func.func @floor_div(tensor<8x16xf32>, tensor<8x16xf32>) -> tensor<8x16xf32> { +^bb0(%arg0: tensor<8x16xf32>, %arg1: tensor<8x16xf32>): + %0 = "tf.FloorDiv"(%arg0, %arg1) : (tensor<8x16xf32>, tensor<8x16xf32>) -> tensor<8x16xf32> + func.return %0 : tensor<8x16xf32> + +// CHECK-LABEL: floor_div +// CHECK: tfl.floor_div %arg0, %arg1 : tensor<8x16xf32> +// CHECK: return +} + +func.func @floor_div_i16(tensor<8x16xi16>, tensor<8x16xi16>) -> tensor<8x16xi16> { +^bb0(%arg0: tensor<8x16xi16>, %arg1: tensor<8x16xi16>): + %0 = "tf.FloorDiv"(%arg0, %arg1) : (tensor<8x16xi16>, tensor<8x16xi16>) -> tensor<8x16xi16> + func.return %0 : tensor<8x16xi16> + +// CHECK-LABEL: floor_div_i16 +// CHECK: tfl.floor_div %arg0, %arg1 : tensor<8x16xi16> +// CHECK: return +} + +func.func @not_equal(%arg0: tensor<8x16xf32>, %arg1: tensor<8x16xf32>) -> tensor<8x16xi1> { + %0 = "tf.NotEqual"(%arg0, %arg1) : (tensor<8x16xf32>, tensor<8x16xf32>) -> tensor<8x16xi1> + func.return %0 : tensor<8x16xi1> + +// CHECK-LABEL: not_equal +// CHECK: tfl.not_equal(%arg0, %arg1) : (tensor<8x16xf32>, tensor<8x16xf32>) -> tensor<8x16xi1> +// CHECK: return +} + +func.func @select(%arg0: tensor<8xi1>, %arg1: tensor<8xf32>, %arg2: tensor<8xf32>) -> tensor<8xf32> { + %0 = "tf.Select"(%arg0, %arg1, %arg2) : (tensor<8xi1>, tensor<8xf32>, tensor<8xf32>) -> tensor<8xf32> + func.return %0: tensor<8xf32> + +// CHECK-LABEL: select +// CHECK: "tfl.select"(%arg0, %arg1, %arg2) +// CHECK: return +} + +func.func @select_multidim(%arg0: tensor<8xi1>, %arg1: tensor<8x3xf32>, %arg2: tensor<8x3xf32>) -> tensor<8x3xf32> { + %0 = "tf.Select"(%arg0, %arg1, %arg2) : (tensor<8xi1>, tensor<8x3xf32>, tensor<8x3xf32>) -> tensor<8x3xf32> + func.return %0: tensor<8x3xf32> + +// CHECK-LABEL: select_multidim +// CHECK: "tfl.select"(%arg0, %arg1, %arg2) +// CHECK: return +} + +func.func @select_v2_same_shape(%arg0: tensor<8xi1>, %arg1: tensor<8xf32>, %arg2: tensor<8xf32>) -> tensor<8xf32> { + %0 = "tf.SelectV2"(%arg0, %arg1, %arg2) : (tensor<8xi1>, tensor<8xf32>, tensor<8xf32>) -> tensor<8xf32> + func.return %0: tensor<8xf32> + +// CHECK-LABEL: select_v2_same_shape +// CHECK: "tfl.select"(%arg0, %arg1, %arg2) +// CHECK: return +} + +func.func @select_v2_multidim(%arg0: tensor<3xi1>, %arg1: tensor<8x3xf32>, %arg2: tensor<8x3xf32>) -> tensor<8x3xf32> { + %0 = "tf.SelectV2"(%arg0, %arg1, %arg2) : (tensor<3xi1>, tensor<8x3xf32>, tensor<8x3xf32>) -> tensor<8x3xf32> + func.return %0: tensor<8x3xf32> + +// CHECK-LABEL: select_v2_multidim +// CHECK: "tfl.select_v2"(%arg0, %arg1, %arg2) +// CHECK: return +} + +func.func @select_v2_broadcast(%arg0: tensor<4xi1>, %arg1: tensor<3x4xf32>, %arg2: tensor<8x3x4xf32>) -> tensor<8x3x4xf32> { + %0 = "tf.SelectV2"(%arg0, %arg1, %arg2) : (tensor<4xi1>, tensor<3x4xf32>, tensor<8x3x4xf32>) -> tensor<8x3x4xf32> + func.return %0: tensor<8x3x4xf32> + +// CHECK-LABEL: select_v2_broadcast +// CHECK: "tfl.select_v2"(%arg0, %arg1, %arg2) +// CHECK: return +} + +func.func @sin(%arg0: tensor) -> tensor { + %0 = "tf.Sin"(%arg0) : (tensor) -> tensor + func.return %0 : tensor + +// CHECK-LABEL:sin +// CHECK: "tfl.sin"(%arg0) : (tensor) -> tensor +} + +func.func @topk(%arg0: tensor<8xf32>, %arg1: tensor) -> (tensor, tensor) { + %0, %1 = "tf.TopKV2"(%arg0, %arg1) : (tensor<8xf32>, tensor) -> (tensor, tensor) + func.return %0, %1: tensor, tensor + +// CHECK-LABEL: topk +// CHECK: "tfl.topk_v2"(%arg0, %arg1) +// CHECK: return +} + +func.func @topk_2(%arg0: tensor<8xf32>) -> (tensor<2xf32>, tensor<2xi32>) { + %0 = "tf.Const"() { value = dense<2> : tensor } : () -> tensor + %1:2 = "tf.TopKV2"(%arg0, %0) : (tensor<8xf32>, tensor) -> (tensor<2xf32>, tensor<2xi32>) + func.return %1#0, %1#1: tensor<2xf32>, tensor<2xi32> + +// CHECK-LABEL: topk_2 +// CHECK: "tfl.topk_v2"(%arg0, %cst) +// CHECK: return +} + +func.func @topk_3(%arg0: tensor) -> (tensor, tensor) { + %0 = "tf.Const"() { value = dense<2> : tensor } : () -> tensor + %1:2 = "tf.TopKV2"(%arg0, %0) : (tensor, tensor) -> (tensor, tensor) + func.return %1#0, %1#1: tensor, tensor + +// CHECK-LABEL: topk_3 +// CHECK: "tfl.topk_v2"(%arg0, %cst) : (tensor, tensor) -> (tensor, tensor) +// CHECK: return +} + +func.func @topk_4(%arg0: tensor<1x2x3x4xf32>) -> (tensor<1x2x3x2xf32>, tensor<1x2x3x2xi32>) { + %0 = "tf.Const"() { value = dense<2> : tensor } : () -> tensor + %1:2 = "tf.TopKV2"(%arg0, %0) : (tensor<1x2x3x4xf32>, tensor) -> (tensor<1x2x3x2xf32>, tensor<1x2x3x2xi32>) + func.return %1#0, %1#1: tensor<1x2x3x2xf32>, tensor<1x2x3x2xi32> + +// CHECK-LABEL: topk_4 +// CHECK: "tfl.topk_v2"(%arg0, %cst) +// CHECK: return +} + +func.func @topk_5(%arg0: tensor<*xf32>) -> (tensor<*xf32>, tensor<*xi32>) { + %0 = "tf.Const"() { value = dense<2> : tensor } : () -> tensor + %1:2 = "tf.TopKV2"(%arg0, %0) : (tensor<*xf32>, tensor) -> (tensor<*xf32>, tensor<*xi32>) + func.return %1#0, %1#1: tensor<*xf32>, tensor<*xi32> + +// CHECK-LABEL: topk_5 +// CHECK: "tfl.topk_v2"(%arg0, %cst) +// CHECK: return +} + +func.func @logicalAnd(%arg0: tensor<8xi1>, %arg1: tensor<8xi1>) -> tensor<8xi1> { + %0 = "tf.LogicalAnd"(%arg0, %arg1) : (tensor<8xi1>, tensor<8xi1>) -> tensor<8xi1> + func.return %0: tensor<8xi1> + +// CHECK-LABEL: logicalAnd +// CHECK: tfl.logical_and %arg0, %arg1 : tensor<8xi1> +// CHECK: return +} + +func.func @logicalNot(%arg0: tensor<8xi1>) -> tensor<8xi1> { + %0 = "tf.LogicalNot"(%arg0) : (tensor<8xi1>) -> tensor<8xi1> + func.return %0 : tensor<8xi1> +// CHECK-LABEL: logicalNot +// CHECK: "tfl.logical_not"(%arg0) : (tensor<8xi1>) -> tensor<8xi1> +} + +func.func @logicalOr(%arg0: tensor<8xi1>, %arg1: tensor<8xi1>) -> tensor<8xi1> { + %0 = "tf.LogicalOr"(%arg0, %arg1) : (tensor<8xi1>, tensor<8xi1>) -> tensor<8xi1> + func.return %0: tensor<8xi1> + +// CHECK-LABEL: logicalOr +// CHECK: tfl.logical_or %arg0, %arg1 : tensor<8xi1> +// CHECK: return +} + +func.func @addV2(%arg0: tensor<1xi32>, %arg1: tensor<1xi32>) -> tensor<1xi32> { + %0 = "tf.AddV2"(%arg0, %arg1) : (tensor<1xi32>, tensor<1xi32>) -> tensor<1xi32> + func.return %0 : tensor<1xi32> + +// CHECK-LABEL: addV2 +// CHECK: tfl.add %arg0, %arg1 {fused_activation_function = "NONE"} : tensor<1xi32> +} + +func.func @addV2I16(%arg0: tensor<1xi16>, %arg1: tensor<1xi16>) -> tensor<1xi16> { + %0 = "tf.AddV2"(%arg0, %arg1) : (tensor<1xi16>, tensor<1xi16>) -> tensor<1xi16> + func.return %0 : tensor<1xi16> + +// CHECK-LABEL: addV2I16 +// CHECK: tfl.add %arg0, %arg1 {fused_activation_function = "NONE"} : tensor<1xi16> +} + +func.func @addN(%arg0: tensor<2x3xi32>, %arg1: tensor<2x3xi32>, %arg2: tensor<2x3xi32>) -> tensor<2x3xi32> { + %0 = "tf.AddN"(%arg0, %arg1, %arg2) : (tensor<2x3xi32>, tensor<2x3xi32>, tensor<2x3xi32>) -> tensor<2x3xi32> + func.return %0 : tensor<2x3xi32> + +// CHECK-LABEL: addN +// CHECK: "tfl.add_n"(%arg0, %arg1, %arg2) : (tensor<2x3xi32>, tensor<2x3xi32>, tensor<2x3xi32>) -> tensor<2x3xi32> +// CHECK: return +} + +func.func @reverse_v2(%arg0: tensor<1x2x3x4xf32>, %arg1: tensor<1xi32>) -> tensor<1x2x3x4xf32> { + %0 = "tf.ReverseV2"(%arg0, %arg1) : (tensor<1x2x3x4xf32>, tensor<1xi32>) -> tensor<1x2x3x4xf32> + func.return %0 : tensor<1x2x3x4xf32> + +// CHECK-LABEL:reverse_v2 +// CHECK: "tfl.reverse_v2"(%arg0, %arg1) : (tensor<1x2x3x4xf32>, tensor<1xi32>) -> tensor<1x2x3x4xf32> +// CHECK: return +} + +func.func @reverse_v2_i64(%arg0: tensor<1x2x3x4xf32>, %arg1: tensor<1xi64>) -> tensor<1x2x3x4xf32> { + %0 = "tf.ReverseV2"(%arg0, %arg1) : (tensor<1x2x3x4xf32>, tensor<1xi64>) -> tensor<1x2x3x4xf32> + func.return %0 : tensor<1x2x3x4xf32> + +// CHECK-LABEL:reverse_v2_i64 +// CHECK: "tfl.cast" +// CHECK: "tfl.reverse_v2" +// CHECK: return +} + +func.func @matrix_diag(%arg0: tensor<8x16xf32>) -> tensor<8x16x16xf32> { + %0 = "tf.MatrixDiag"(%arg0) : (tensor<8x16xf32>) -> tensor<8x16x16xf32> + func.return %0 : tensor<8x16x16xf32> + +// CHECK-LABEL:matrix_diag +// CHECK: "tfl.matrix_diag"(%arg0) : (tensor<8x16xf32>) -> tensor<8x16x16xf32> +} + +func.func @matrix_diag_v2_no_match(%arg0: tensor<8x16xf32>) -> tensor<8x16x16xf32> { + // this should have been 0. + %0 = arith.constant dense<[1]> : tensor<1xi32> + + %1 = arith.constant dense<[-1]> : tensor<1xi32> + %2 = arith.constant dense<[-1]> : tensor<1xi32> + %3 = arith.constant dense<[0, 0]> : tensor<2xi32> + %4 = "tf.MatrixDiagV2"(%arg0, %0, %1, %2, %3) : (tensor<8x16xf32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<2xi32>) -> tensor<8x16x16xf32> + func.return %4 : tensor<8x16x16xf32> + +// CHECK-LABEL: func @matrix_diag_v2_no_match( +// CHECK-SAME: [[VAL_0:%.*]]: tensor<8x16xf32>) -> tensor<8x16x16xf32> { +// CHECK-DAG: [[VAL_1:%.*]] = arith.constant dense<1> : tensor<1xi32> +// CHECK-DAG: [[VAL_2:%.*]] = arith.constant dense<-1> : tensor<1xi32> +// CHECK-DAG: [[VAL_3:%.*]] = arith.constant dense<0> : tensor<2xi32> +// CHECK: [[VAL_4:%.*]] = "tf.MatrixDiagV2"([[VAL_0]], [[VAL_1]], [[VAL_2]], [[VAL_2]], [[VAL_3]]) : (tensor<8x16xf32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<2xi32>) -> tensor<8x16x16xf32> +// CHECK: return [[VAL_4]] : tensor<8x16x16xf32> +} + +func.func @matrix_diag_v2(%arg0: tensor<8x16xf32>) -> tensor<8x16x16xf32> { + %0 = arith.constant dense<[0]> : tensor<1xi32> + %1 = arith.constant dense<[-1]> : tensor<1xi32> + %2 = arith.constant dense<[-1]> : tensor<1xi32> + %3 = arith.constant dense<[0, 0]> : tensor<2xi32> + %4 = "tf.MatrixDiagV2"(%arg0, %0, %1, %2, %3) : (tensor<8x16xf32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<2xi32>) -> tensor<8x16x16xf32> + func.return %4 : tensor<8x16x16xf32> + +// CHECK-LABEL: func @matrix_diag_v2( +// CHECK-SAME: [[VAL_5:%.*]]: tensor<8x16xf32>) -> tensor<8x16x16xf32> { +// CHECK: [[VAL_6:%.*]] = "tfl.matrix_diag"([[VAL_5]]) : (tensor<8x16xf32>) -> tensor<8x16x16xf32> +// CHECK: return [[VAL_6]] : tensor<8x16x16xf32> +} + +func.func @matrix_diag_v2_f32_padding(%arg0: tensor<8x16xf32>) -> tensor<8x16x16xf32> { + %0 = arith.constant dense<[0]> : tensor<1xi32> + %1 = arith.constant dense<[-1]> : tensor<1xi32> + %2 = arith.constant dense<[-1]> : tensor<1xi32> + %3 = arith.constant dense<[0.0, 0.0]> : tensor<2xf32> + %4 = "tf.MatrixDiagV2"(%arg0, %0, %1, %2, %3) : (tensor<8x16xf32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<2xf32>) -> tensor<8x16x16xf32> + func.return %4 : tensor<8x16x16xf32> + +// CHECK-LABEL: func @matrix_diag_v2_f32_padding( +// CHECK-SAME: [[VAL_5:%.*]]: tensor<8x16xf32>) -> tensor<8x16x16xf32> { +// CHECK: [[VAL_6:%.*]] = "tfl.matrix_diag"([[VAL_5]]) : (tensor<8x16xf32>) -> tensor<8x16x16xf32> +// CHECK: return [[VAL_6]] : tensor<8x16x16xf32> +} + +func.func @matrix_diag_v3_no_match(%arg0: tensor<8x16xf32>) -> tensor<8x16x16xf32> { + // this should have been 0. + %0 = arith.constant dense<[1]> : tensor<1xi32> + + %1 = arith.constant dense<[-1]> : tensor<1xi32> + %2 = arith.constant dense<[-1]> : tensor<1xi32> + %3 = arith.constant dense<[0, 0]> : tensor<2xi32> + %4 = "tf.MatrixDiagV3"(%arg0, %0, %1, %2, %3) : (tensor<8x16xf32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<2xi32>) -> tensor<8x16x16xf32> + func.return %4 : tensor<8x16x16xf32> + +// CHECK-LABEL: func @matrix_diag_v3_no_match( +// CHECK-SAME: [[VAL_0:%.*]]: tensor<8x16xf32>) -> tensor<8x16x16xf32> { +// CHECK-DAG: [[VAL_1:%.*]] = arith.constant dense<1> : tensor<1xi32> +// CHECK-DAG: [[VAL_2:%.*]] = arith.constant dense<-1> : tensor<1xi32> +// CHECK-DAG: [[VAL_3:%.*]] = arith.constant dense<0> : tensor<2xi32> +// CHECK: [[VAL_4:%.*]] = "tf.MatrixDiagV3"([[VAL_0]], [[VAL_1]], [[VAL_2]], [[VAL_2]], [[VAL_3]]) : (tensor<8x16xf32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<2xi32>) -> tensor<8x16x16xf32> +// CHECK: return [[VAL_4]] : tensor<8x16x16xf32> +} + +func.func @matrix_diag_v3(%arg0: tensor<8x16xf32>) -> tensor<8x16x16xf32> { + %0 = arith.constant dense<[0]> : tensor<1xi32> + %1 = arith.constant dense<[-1]> : tensor<1xi32> + %2 = arith.constant dense<[-1]> : tensor<1xi32> + %3 = arith.constant dense<[0, 0]> : tensor<2xi32> + %4 = "tf.MatrixDiagV3"(%arg0, %0, %1, %2, %3) : (tensor<8x16xf32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>, tensor<2xi32>) -> tensor<8x16x16xf32> + func.return %4 : tensor<8x16x16xf32> + +// CHECK-LABEL: func @matrix_diag_v3( +// CHECK-SAME: [[VAL_5:%.*]]: tensor<8x16xf32>) -> tensor<8x16x16xf32> { +// CHECK: [[VAL_6:%.*]] = "tfl.matrix_diag"([[VAL_5]]) : (tensor<8x16xf32>) -> tensor<8x16x16xf32> +// CHECK: return [[VAL_6]] : tensor<8x16x16xf32> +} + +func.func @matrix_set_diag_v3(%arg0: tensor<3x3xi64>, %arg1: tensor<3xi32>) -> tensor<3x3xi64> { + %cst = arith.constant dense<0> : tensor + %0 = "tf.MatrixSetDiagV3"(%arg0, %arg1, %cst) {align = "RIGHT_LEFT"} : (tensor<3x3xi64>, tensor<3xi32>, tensor) -> tensor<3x3xi64> + func.return %0 : tensor<3x3xi64> + +// CHECK-LABEL: func @matrix_set_diag_v3 +// CHECK: "tfl.matrix_set_diag"(%arg0, %arg1) : (tensor<3x3xi64>, tensor<3xi32>) -> tensor<3x3xi64> +} + +func.func @matrix_set_diag_v3_non_zero_k(%arg0: tensor<3x3xi64>, %arg1: tensor<3xi32>) -> tensor<3x3xi64> { + %cst = arith.constant dense<1> : tensor + %0 = "tf.MatrixSetDiagV3"(%arg0, %arg1, %cst) : (tensor<3x3xi64>, tensor<3xi32>, tensor) -> tensor<3x3xi64> + func.return %0 : tensor<3x3xi64> + +// CHECK-LABEL: @matrix_set_diag_v3_non_zero_k +// CHECK: tf.MatrixSetDiagV3 +} + +func.func @matrix_set_diag_v3_default_align(%arg0: tensor<3x3xi64>, %arg1: tensor<3xi32>) -> tensor<3x3xi64> { + %cst = arith.constant dense<0> : tensor + %0 = "tf.MatrixSetDiagV3"(%arg0, %arg1, %cst) : (tensor<3x3xi64>, tensor<3xi32>, tensor) -> tensor<3x3xi64> + func.return %0 : tensor<3x3xi64> + +// CHECK-LABEL: @matrix_set_diag_v3_default_align +// CHECK: "tfl.matrix_set_diag"(%arg0, %arg1) : (tensor<3x3xi64>, tensor<3xi32>) -> tensor<3x3xi64> +} + +func.func @maximum(%arg0: tensor<8x16xf32>, %arg1: tensor<8x16xf32>) -> tensor<8x16xf32> { + %0 = "tf.Maximum"(%arg0, %arg1) : (tensor<8x16xf32>, tensor<8x16xf32>) -> tensor<8x16xf32> + func.return %0 : tensor<8x16xf32> + +// CHECK-LABEL:maximum +// CHECK: "tfl.maximum"(%arg0, %arg1) : (tensor<8x16xf32>, tensor<8x16xf32>) -> tensor<8x16xf32> +} + +func.func @minimum(%arg0: tensor<8x16xf32>, %arg1: tensor<8x16xf32>) -> tensor<8x16xf32> { + %0 = "tf.Minimum"(%arg0, %arg1) : (tensor<8x16xf32>, tensor<8x16xf32>) -> tensor<8x16xf32> + func.return %0 : tensor<8x16xf32> + +// CHECK-LABEL:minimum +// CHECK: "tfl.minimum"(%arg0, %arg1) : (tensor<8x16xf32>, tensor<8x16xf32>) -> tensor<8x16xf32> +} + +func.func @realDiv(%arg0: tensor<8x16xf32>, %arg1: tensor<8x16xf32>) -> tensor<8x16xf32> { + %0 = "tf.RealDiv"(%arg0, %arg1) : (tensor<8x16xf32>, tensor<8x16xf32>) -> tensor<8x16xf32> + func.return %0 : tensor<8x16xf32> + +// CHECK-LABEL: realDiv +// CHECK: tfl.div %arg0, %arg1 {fused_activation_function = "NONE"} : tensor<8x16xf32> +} + +func.func @equal(%arg0: tensor<8x16xf32>, %arg1: tensor<8x16xf32>) -> tensor<8x16xi1> { + %0 = "tf.Equal"(%arg0, %arg1) : (tensor<8x16xf32>, tensor<8x16xf32>) -> tensor<8x16xi1> + func.return %0 : tensor<8x16xi1> + +// CHECK-LABEL: equal +// CHECK: "tfl.equal"(%arg0, %arg1) : (tensor<8x16xf32>, tensor<8x16xf32>) -> tensor<8x16xi1> +// CHECK: return +} + +func.func @pad(tensor<2x1x3xf32>, tensor<3x2xi32>) -> tensor { +^bb0(%arg0: tensor<2x1x3xf32>, %arg1: tensor<3x2xi32>): + %0 = "tf.Pad"(%arg0, %arg1) : (tensor<2x1x3xf32>, tensor<3x2xi32>) -> tensor + func.return %0#0 : tensor + + // CHECK-LABEL: pad + // CHECK: "tfl.pad"(%arg0, %arg1) : (tensor<2x1x3xf32>, tensor<3x2xi32>) -> tensor + // CHECK: return +} + +func.func @pad_5D(tensor<2x1x3x1x1xf32>, tensor<5x2xi32>) -> tensor { +^bb0(%arg0: tensor<2x1x3x1x1xf32>, %arg1: tensor<5x2xi32>): + %0 = "tf.Pad"(%arg0, %arg1) : (tensor<2x1x3x1x1xf32>, tensor<5x2xi32>) -> tensor + func.return %0#0 : tensor + + // CHECK-LABEL: pad_5D + // CHECK: "tfl.pad"(%arg0, %arg1) : (tensor<2x1x3x1x1xf32>, tensor<5x2xi32>) -> tensor + // CHECK: return +} + +func.func @pow(%arg0: tensor<2x1x3xf32>, %arg1: tensor<2x1x1xf32>) -> tensor<2x1x3xf32> { + %0 = "tf.Pow"(%arg0, %arg1) : (tensor<2x1x3xf32>, tensor<2x1x1xf32>) -> tensor<2x1x3xf32> + func.return %0 : tensor<2x1x3xf32> + + // CHECK-LABEL: pow + // CHECK: %[[pow:.*]] = tfl.pow(%arg0, %arg1) : (tensor<2x1x3xf32>, tensor<2x1x1xf32>) -> tensor<2x1x3xf32> + // CHECK: return +} + +func.func @tile(tensor<2x3xf32>, tensor<2xi32>) -> tensor<2x6xf32> { +^bb0(%arg0: tensor<2x3xf32>, %arg1: tensor<2xi32>): + %cst = "tf.Const"() { value = dense<[1, 2]> : tensor<2xi32> } : () -> tensor<2xi32> + %0 = "tf.Tile"(%arg0, %cst) : (tensor<2x3xf32>, tensor<2xi32>) -> tensor<2x6xf32> + func.return %0 : tensor<2x6xf32> + + // CHECK-LABEL: tile + // CHECK: "tfl.tile"(%arg0, %cst) : (tensor<2x3xf32>, tensor<2xi32>) -> tensor<2x6xf32> + // CHECK: return +} + +func.func @padv2(tensor<2x1x3xf32>, tensor<3x2xi32>) -> tensor { +^bb0(%arg0: tensor<2x1x3xf32>, %arg1: tensor<3x2xi32>): + %cst = "tf.Const"() { value = dense<2.0> : tensor } : () -> tensor + %0 = "tf.PadV2"(%arg0, %arg1, %cst) : (tensor<2x1x3xf32>, tensor<3x2xi32>, tensor) -> tensor + func.return %0#0 : tensor + + // CHECK-LABEL: padv2 + // CHECK: "tfl.padv2"(%arg0, %arg1, %cst) : (tensor<2x1x3xf32>, tensor<3x2xi32>, tensor) -> tensor + // CHECK: return +} + +func.func @padv2_5D(tensor<2x1x3x1x1xf32>, tensor<5x2xi32>) -> tensor { +^bb0(%arg0: tensor<2x1x3x1x1xf32>, %arg1: tensor<5x2xi32>): + %cst = "tf.Const"() { value = dense<2.0> : tensor } : () -> tensor + %0 = "tf.PadV2"(%arg0, %arg1, %cst) : (tensor<2x1x3x1x1xf32>, tensor<5x2xi32>, tensor) -> tensor + func.return %0#0 : tensor + + // CHECK-LABEL: padv2_5D + // CHECK: "tfl.padv2"(%arg0, %arg1, %cst) : (tensor<2x1x3x1x1xf32>, tensor<5x2xi32>, tensor) -> tensor + // CHECK: return +} + +func.func @pack2Tensors(%arg0: tensor<2xi32>, %arg1: tensor<2xi32>) -> tensor<2x2xi32> { + %0 = "tf.Pack"(%arg0, %arg1) : (tensor<2xi32>, tensor<2xi32>) -> tensor<2x2xi32> + func.return %0 : tensor<2x2xi32> + +// CHECK-LABEL: pack2Tensors +// CHECK: "tfl.pack"(%arg0, %arg1) <{axis = 0 : i32, values_count = 2 : i32}> : (tensor<2xi32>, tensor<2xi32>) -> tensor<2x2xi32> +} + +func.func @pack3Tensors(%arg0: tensor<2xi32>, %arg1: tensor<2xi32>, %arg2 : tensor<2xi32>) -> tensor<2x3xi32> { + %0 = "tf.Pack"(%arg0, %arg1, %arg2) {axis = 1 : i64} : (tensor<2xi32>, tensor<2xi32>, tensor<2xi32>) -> tensor<2x3xi32> + func.return %0 : tensor<2x3xi32> + +// CHECK-LABEL: pack3Tensors +// CHECK: "tfl.pack"(%arg0, %arg1, %arg2) <{axis = 1 : i32, values_count = 3 : i32}> : (tensor<2xi32>, tensor<2xi32>, tensor<2xi32>) -> tensor<2x3xi32> +} + +func.func @packStringWithFlex(%arg0: tensor<2x!tf_type.string>, %arg1: tensor<2x!tf_type.string>) -> tensor<2x2x!tf_type.string> { + %0 = "tf.Pack"(%arg0, %arg1) : (tensor<2x!tf_type.string>, tensor<2x!tf_type.string>) -> tensor<2x2x!tf_type.string> + func.return %0 : tensor<2x2x!tf_type.string> + +// CHECK-LABEL: packStringWithFlex +// CHECK: "tf.Pack"(%arg0, %arg1) : (tensor<2x!tf_type.string>, tensor<2x!tf_type.string>) -> tensor<2x2x!tf_type.string> +} + +func.func @packNegAxis(%arg0: tensor<2xi32>, %arg1: tensor<2xi32>, %arg2 : tensor<2xi32>) -> tensor<2x3xi32> { + %0 = "tf.Pack"(%arg0, %arg1, %arg2) {axis = -1 : i64} : (tensor<2xi32>, tensor<2xi32>, tensor<2xi32>) -> tensor<2x3xi32> + func.return %0 : tensor<2x3xi32> + +// CHECK-LABEL: packNegAxis +// CHECK: "tfl.pack"(%arg0, %arg1, %arg2) <{axis = -1 : i32, values_count = 3 : i32}> : (tensor<2xi32>, tensor<2xi32>, tensor<2xi32>) -> tensor<2x3xi32> +} + +func.func @unpack2Tensors(%arg0: tensor<2x2xi32>) -> tensor<2xi32> { + %0:2 = "tf.Unpack"(%arg0) : (tensor<2x2xi32>) -> (tensor<2xi32>, tensor<2xi32>) + func.return %0#0 : tensor<2xi32> + +// CHECK-LABEL: unpack2Tensors +// CHECK: "tfl.unpack"(%arg0) <{axis = 0 : i32, num = 2 : i32}> : (tensor<2x2xi32>) -> (tensor<2xi32>, tensor<2xi32>) +} + +func.func @unpack3Tensors(%arg0: tensor<2x3xi32>) -> tensor<2xi32> { + %0:3 = "tf.Unpack"(%arg0) {axis = 1 : i64} : (tensor<2x3xi32>) -> (tensor<2xi32>, tensor<2xi32>, tensor<2xi32>) + func.return %0#0 : tensor<2xi32> + +// CHECK-LABEL: unpack3Tensors +// CHECK: "tfl.unpack"(%arg0) <{axis = 1 : i32, num = 3 : i32}> : (tensor<2x3xi32>) -> (tensor<2xi32>, tensor<2xi32>, tensor<2xi32>) +} + +func.func @unpackNegAxis(%arg0: tensor<2x3xi32>) -> tensor<2xi32> { + %0:3 = "tf.Unpack"(%arg0) {axis = -1 : i64} : (tensor<2x3xi32>) -> (tensor<2xi32>, tensor<2xi32>, tensor<2xi32>) + func.return %0#0 : tensor<2xi32> + +// CHECK-LABEL: unpackNegAxis +// CHECK: "tfl.unpack"(%arg0) <{axis = -1 : i32, num = 3 : i32}> : (tensor<2x3xi32>) -> (tensor<2xi32>, tensor<2xi32>, tensor<2xi32>) +} + +func.func @mean(%arg0: tensor<2x2xf32>, %arg1: tensor<1xi32>) -> tensor<1x2xf32> { + %0 = "tf.Mean"(%arg0, %arg1) : (tensor<2x2xf32>, tensor<1xi32>) -> tensor<1x2xf32> + func.return %0 : tensor<1x2xf32> + +// CHECK-LABEL: mean +// CHECK: "tfl.mean"(%arg0, %arg1) <{keep_dims = false}> : (tensor<2x2xf32>, tensor<1xi32>) -> tensor<1x2xf32> +} + +func.func @mean_true(%arg0: tensor<2x2xf32>, %arg1: tensor<1xi32>) -> tensor<1x2xf32> { + %0 = "tf.Mean"(%arg0, %arg1) {keep_dims = true} : (tensor<2x2xf32>, tensor<1xi32>) -> tensor<1x2xf32> + func.return %0 : tensor<1x2xf32> + +// CHECK-LABEL: mean_true +// CHECK: "tfl.mean"(%arg0, %arg1) <{keep_dims = true}> : (tensor<2x2xf32>, tensor<1xi32>) -> tensor<1x2xf32> +} + +func.func @sum(%arg0: tensor<8x16x16xf32>, %arg1: tensor<2xi32>) -> tensor { + %0 = "tf.Sum"(%arg0, %arg1) {keep_dims = false} : (tensor<8x16x16xf32>, tensor<2xi32>) -> tensor + func.return %0 : tensor + + // CHECK-LABEL: sum + // CHECK: "tfl.sum"(%arg0, %arg1) <{keep_dims = false}> : (tensor<8x16x16xf32>, tensor<2xi32>) -> tensor +} + +func.func @sum_true(%arg0: tensor<8x16x16xf32>, %arg1: tensor<2xi32>) -> tensor { + %0 = "tf.Sum"(%arg0, %arg1) {keep_dims = true} : (tensor<8x16x16xf32>, tensor<2xi32>) -> tensor + func.return %0 : tensor + + // CHECK-LABEL: sum_true + // CHECK: "tfl.sum"(%arg0, %arg1) <{keep_dims = true}> : (tensor<8x16x16xf32>, tensor<2xi32>) -> tensor +} + +func.func @sum_i64axes(%arg0: tensor<8x16x16xf32>, %arg1: tensor<2xi64>) -> tensor { + %0 = "tf.Sum"(%arg0, %arg1) {keep_dims = false} : (tensor<8x16x16xf32>, tensor<2xi64>) -> tensor + func.return %0 : tensor + + // CHECK-LABEL: sum_i64axes + // CHECK: %[[V0:.*]] = "tfl.cast"(%arg1) : (tensor<2xi64>) -> tensor<2xi32> + // CHECK: "tfl.sum"(%arg0, %[[V0]]) <{keep_dims = false}> : (tensor<8x16x16xf32>, tensor<2xi32>) -> tensor +} + +func.func @reduce_min(%arg0: tensor<8x16x16xf32>, %arg1: tensor<2xi32>) -> tensor { + %0 = "tf.Min"(%arg0, %arg1) {keep_dims = false} : (tensor<8x16x16xf32>, tensor<2xi32>) -> tensor + func.return %0 : tensor + + // CHECK-LABEL: reduce_min + // CHECK: "tfl.reduce_min"(%arg0, %arg1) <{keep_dims = false}> : (tensor<8x16x16xf32>, tensor<2xi32>) -> tensor +} + +func.func @reduce_min_true(%arg0: tensor<8x16x16xf32>, %arg1: tensor<2xi32>) -> tensor { + %0 = "tf.Min"(%arg0, %arg1) {keep_dims = true} : (tensor<8x16x16xf32>, tensor<2xi32>) -> tensor + func.return %0 : tensor + + // CHECK-LABEL: reduce_min_true + // CHECK: "tfl.reduce_min"(%arg0, %arg1) <{keep_dims = true}> : (tensor<8x16x16xf32>, tensor<2xi32>) -> tensor +} + +func.func @reduce_min_i64axes(%arg0: tensor<8x16x16xf32>, %arg1: tensor<2xi64>) -> tensor { + %0 = "tf.Min"(%arg0, %arg1) {keep_dims = false} : (tensor<8x16x16xf32>, tensor<2xi64>) -> tensor + func.return %0 : tensor + + // CHECK-LABEL: reduce_min_i64axes + // CHECK: %[[V0:.*]] = "tfl.cast"(%arg1) : (tensor<2xi64>) -> tensor<2xi32> + // CHECK: "tfl.reduce_min"(%arg0, %[[V0]]) <{keep_dims = false}> : (tensor<8x16x16xf32>, tensor<2xi32>) -> tensor +} + +func.func @reduce_max(%arg0: tensor<8x16x16xf32>, %arg1: tensor<2xi32>) -> tensor { + %0 = "tf.Max"(%arg0, %arg1) {keep_dims = false} : (tensor<8x16x16xf32>, tensor<2xi32>) -> tensor + func.return %0 : tensor + + // CHECK-LABEL: reduce_max + // CHECK: "tfl.reduce_max"(%arg0, %arg1) <{keep_dims = false}> : (tensor<8x16x16xf32>, tensor<2xi32>) -> tensor +} + +func.func @reduce_max_true(%arg0: tensor<8x16x16xf32>, %arg1: tensor<2xi32>) -> tensor { + %0 = "tf.Max"(%arg0, %arg1) {keep_dims = true} : (tensor<8x16x16xf32>, tensor<2xi32>) -> tensor + func.return %0 : tensor + + // CHECK-LABEL: reduce_max_true + // CHECK: "tfl.reduce_max"(%arg0, %arg1) <{keep_dims = true}> : (tensor<8x16x16xf32>, tensor<2xi32>) -> tensor +} + +func.func @reduce_max_i64axes(%arg0: tensor<8x16x16xf32>, %arg1: tensor<2xi64>) -> tensor { + %0 = "tf.Max"(%arg0, %arg1) {keep_dims = false} : (tensor<8x16x16xf32>, tensor<2xi64>) -> tensor + func.return %0 : tensor + + // CHECK-LABEL: reduce_max_i64axes + // CHECK: %[[V0:.*]] = "tfl.cast"(%arg1) : (tensor<2xi64>) -> tensor<2xi32> + // CHECK: "tfl.reduce_max"(%arg0, %[[V0]]) <{keep_dims = false}> : (tensor<8x16x16xf32>, tensor<2xi32>) -> tensor +} + +func.func @reduce_prod(%arg0: tensor<8x16x16xf32>, %arg1: tensor<2xi32>) -> tensor { + %0 = "tf.Prod"(%arg0, %arg1) {keep_dims = false} : (tensor<8x16x16xf32>, tensor<2xi32>) -> tensor + func.return %0 : tensor + + // CHECK-LABEL: reduce_prod + // CHECK: "tfl.reduce_prod"(%arg0, %arg1) <{keep_dims = false}> : (tensor<8x16x16xf32>, tensor<2xi32>) -> tensor +} + +func.func @reduce_prod_true(%arg0: tensor<8x16x16xf32>, %arg1: tensor<2xi32>) -> tensor { + %0 = "tf.Prod"(%arg0, %arg1) {keep_dims = true} : (tensor<8x16x16xf32>, tensor<2xi32>) -> tensor + func.return %0 : tensor + + // CHECK-LABEL: reduce_prod_true + // CHECK: "tfl.reduce_prod"(%arg0, %arg1) <{keep_dims = true}> : (tensor<8x16x16xf32>, tensor<2xi32>) -> tensor +} + +func.func @reduce_prod_i64axes(%arg0: tensor<8x16x16xf32>, %arg1: tensor<2xi64>) -> tensor { + %0 = "tf.Prod"(%arg0, %arg1) {keep_dims = false} : (tensor<8x16x16xf32>, tensor<2xi64>) -> tensor + func.return %0 : tensor + + // CHECK-LABEL: reduce_prod_i64axes + // CHECK: %[[V0:.*]] = "tfl.cast"(%arg1) : (tensor<2xi64>) -> tensor<2xi32> + // CHECK: "tfl.reduce_prod"(%arg0, %[[V0]]) <{keep_dims = false}> : (tensor<8x16x16xf32>, tensor<2xi32>) -> tensor +} + +func.func @batch_to_space_nd(%arg0: tensor<4x2x2x3xf32>, %arg1: tensor<2xi32>, %arg2: tensor<2x2xi32>) -> tensor { + %0 = "tf.BatchToSpaceND"(%arg0, %arg1, %arg2) : (tensor<4x2x2x3xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor + func.return %0 : tensor + // CHECK-LABEL: batch_to_space_nd + // CHECK: "tfl.batch_to_space_nd"(%arg0, %arg1, %arg2) : (tensor<4x2x2x3xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor +} + +func.func @batch_to_space_nd_unsupported(%arg0: tensor, %arg1: tensor<3xi32>, %arg2: tensor<3x2xi32>) -> tensor { + %0 = "tf.BatchToSpaceND"(%arg0, %arg1, %arg2) : (tensor, tensor<3xi32>, tensor<3x2xi32>) -> tensor + func.return %0 : tensor + // CHECK-LABEL: batch_to_space_nd_unsupported + // CHECK: "tf.BatchToSpaceND" +} + +func.func @batch_to_space_nd_i64(%arg0: tensor<4x2x2x3xf32>, %arg1: tensor<2xi64>, %arg2: tensor<2x2xi64>) -> tensor { + %0 = "tf.BatchToSpaceND"(%arg0, %arg1, %arg2) : (tensor<4x2x2x3xf32>, tensor<2xi64>, tensor<2x2xi64>) -> tensor + func.return %0 : tensor + // CHECK-LABEL: batch_to_space_nd_i64 + // CHECK: "tfl.cast" + // CHECK: "tfl.cast" + // CHECK: "tfl.batch_to_space_nd" +} + +func.func @space_to_batch_nd(%arg0: tensor<1x4x4x3xf32>, %arg1: tensor<2xi32>, %arg2: tensor<2x2xi32>) -> tensor<*xf32> { + %0 = "tf.SpaceToBatchND"(%arg0, %arg1, %arg2) : (tensor<1x4x4x3xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<*xf32> + func.return %0 : tensor<*xf32> + // CHECK-LABEL: space_to_batch_nd + // CHECK: "tfl.space_to_batch_nd"(%arg0, %arg1, %arg2) : (tensor<1x4x4x3xf32>, tensor<2xi32>, tensor<2x2xi32>) -> tensor<*xf32> +} + +func.func @space_to_batch_nd_i64(%arg0: tensor<1x4x4x3xf32>, %arg1: tensor<2xi64>, %arg2: tensor<2x2xi64>) -> tensor<*xf32> { + %0 = "tf.SpaceToBatchND"(%arg0, %arg1, %arg2) : (tensor<1x4x4x3xf32>, tensor<2xi64>, tensor<2x2xi64>) -> tensor<*xf32> + func.return %0 : tensor<*xf32> + // CHECK-LABEL: space_to_batch_nd_i64 + // CHECK: "tfl.cast" + // CHECK: "tfl.cast" + // CHECK: "tfl.space_to_batch_nd" +} + +func.func @split(%arg0: tensor, %arg1: tensor<1x4x3x3xf32>) -> tensor<1x4x3xf32> { + %0:3 = "tf.Split"(%arg0, %arg1) : (tensor, tensor<1x4x3x3xf32>) -> (tensor<1x4x3xf32>, tensor<1x4x3xf32>, tensor<1x4x3xf32>) + func.return %0#0 : tensor<1x4x3xf32> + + // CHECK-LABEL: split + // CHECK: "tfl.split"(%arg0, %arg1) <{num_splits = 3 : i32}> : (tensor, tensor<1x4x3x3xf32>) -> (tensor<1x4x3xf32>, tensor<1x4x3xf32>, tensor<1x4x3xf32>) +} + +func.func @splitv(%arg0: tensor<1x4x3x3xf32>, %arg1: tensor<2xi32>, %arg2: tensor) -> tensor<1x4x2x3xf32> { + %0:2 = "tf.SplitV"(%arg0, %arg1, %arg2) : (tensor<1x4x3x3xf32>, tensor<2xi32>, tensor) -> (tensor<1x4x2x3xf32>, tensor<1x4x1x3xf32>) + func.return %0#0 : tensor<1x4x2x3xf32> + + // CHECK-LABEL: splitv + // CHECK: "tfl.split_v"(%arg0, %arg1, %arg2) <{num_splits = 2 : i32}> : (tensor<1x4x3x3xf32>, tensor<2xi32>, tensor) -> (tensor<1x4x2x3xf32>, tensor<1x4x1x3xf32>) +} + +func.func @matmul(%arg0: tensor<40x37xf32>, %arg1: tensor<37x40xf32>) -> tensor<40x40xf32> { + %0 = "tf.MatMul"(%arg0, %arg1) {T = "tfdtype$DT_FLOAT", device = "/device:CPU:0", name = "MatMul", transpose_a = false, transpose_b = false} : +(tensor<40x37xf32>, tensor<37x40xf32>) -> tensor<40x40xf32> + func.return %0 : tensor<40x40xf32> +// CHECK-LABEL: matmul +// CHECK: %[[CST:.*]] = arith.constant dense<[1, 0]> : tensor<2xi32> +// CHECK: %[[ARG:.*]] = "tfl.transpose"(%arg1, %[[CST]]) : (tensor<37x40xf32>, tensor<2xi32>) -> tensor<40x37xf32> +// CHECK: %[[CST_0:.*]] = "tfl.no_value"() <{value}> : () -> none +// CHECK: "tfl.fully_connected"(%arg0, %[[ARG]], %[[CST_0]]) <{fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"}> : (tensor<40x37xf32>, tensor<40x37xf32>, none) -> tensor<40x40xf32> +} + +func.func @matmul_transposed_a(%arg0: tensor<37x40xf32>, %arg1: tensor<37x40xf32>) -> tensor<40x40xf32> { + %0 = "tf.MatMul"(%arg0, %arg1) {T = "tfdtype$DT_FLOAT", device = "/device:CPU:0", name = "MatMul", transpose_a = true, transpose_b = false} : +(tensor<37x40xf32>, tensor<37x40xf32>) -> tensor<40x40xf32> + func.return %0 : tensor<40x40xf32> +// CHECK-LABEL: matmul_transposed_a +// CHECK: %[[CST_0:.*]] = arith.constant dense<[1, 0]> : tensor<2xi32> +// CHECK: %[[ARG_0:.*]] = "tfl.transpose"(%arg0, %[[CST_0]]) : (tensor<37x40xf32>, tensor<2xi32>) -> tensor<40x37xf32> +// CHECK: %[[ARG_1:.*]] = "tfl.transpose"(%arg1, %[[CST_0]]) : (tensor<37x40xf32>, tensor<2xi32>) -> tensor<40x37xf32> +// CHECK: %[[CST_2:.*]] = "tfl.no_value"() <{value}> : () -> none +// CHECK: "tfl.fully_connected"(%[[ARG_0]], %[[ARG_1]], %[[CST_2]]) <{fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"}> : (tensor<40x37xf32>, tensor<40x37xf32>, none) -> tensor<40x40xf32> +} + +func.func @matmul_transposed_b(%arg0: tensor<40x37xf32>, %arg1: tensor<40x37xf32>) -> tensor<40x40xf32> { + %0 = "tf.MatMul"(%arg0, %arg1) {T = "tfdtype$DT_FLOAT", device = "/device:CPU:0", name = "MatMul", transpose_a = false, transpose_b = true} : +(tensor<40x37xf32>, tensor<40x37xf32>) -> tensor<40x40xf32> + func.return %0 : tensor<40x40xf32> +// CHECK-LABEL: matmul_transposed_b +// CHECK: %[[CST:.*]] = "tfl.no_value"() <{value}> : () -> none +// CHECK: "tfl.fully_connected"(%arg0, %arg1, %[[CST]]) <{fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"}> : (tensor<40x37xf32>, tensor<40x37xf32>, none) -> tensor<40x40xf32> +} + +func.func @matmul_transposed_ab(%arg0: tensor<37x40xf32>, %arg1: tensor<40x37xf32>) -> tensor<40x40xf32> { + %0 = "tf.MatMul"(%arg0, %arg1) {T = "tfdtype$DT_FLOAT", device = "/device:CPU:0", name = "MatMul", transpose_a = true, transpose_b = true} : +(tensor<37x40xf32>, tensor<40x37xf32>) -> tensor<40x40xf32> + func.return %0 : tensor<40x40xf32> +// CHECK-LABEL: matmul_transposed_ab +// CHECK: %[[CST_0:.*]] = arith.constant dense<[1, 0]> : tensor<2xi32> +// CHECK: %[[ARG_0:.*]] = "tfl.transpose"(%arg0, %[[CST_0]]) : (tensor<37x40xf32>, tensor<2xi32>) -> tensor<40x37xf32> +// CHECK: %[[CST_1:.*]] = "tfl.no_value"() <{value}> : () -> none +// CHECK: "tfl.fully_connected"(%[[ARG_0]], %arg1, %[[CST_1]]) <{fused_activation_function = "NONE", keep_num_dims = false, weights_format = "DEFAULT"}> : (tensor<40x37xf32>, tensor<40x37xf32>, none) -> tensor<40x40xf32> +} + +func.func @concat_v2_with_3_tensors(%arg0: tensor<2x1xi32>, %arg1: tensor<2x1xi32>, %arg2: tensor<2x1xi32>) -> tensor<2x3xi32> { + %0 = "tf.Const"() { value = dense<-1> : tensor } : () -> tensor + %1 = "tf.ConcatV2"(%arg0, %arg1, %arg2, %0) : (tensor<2x1xi32>, tensor<2x1xi32>, tensor<2x1xi32>, tensor) -> tensor<2x3xi32> + func.return %1 : tensor<2x3xi32> + +// CHECK-LABEL: concat_v2_with_3_tensors +// CHECK: "tfl.concatenation"(%arg0, %arg1, %arg2) <{axis = -1 : i32, fused_activation_function = "NONE"}> : (tensor<2x1xi32>, tensor<2x1xi32>, tensor<2x1xi32>) -> tensor<2x3xi32> +} + +func.func @concat_v2_i64_axis(%arg0: tensor<2x1xi32>, %arg1: tensor<2x1xi32>, %arg2: tensor<2x1xi32>) -> tensor<2x3xi32> { + %0 = "tf.Const"() { value = dense<-1> : tensor } : () -> tensor + %1 = "tf.ConcatV2"(%arg0, %arg1, %arg2, %0) : (tensor<2x1xi32>, tensor<2x1xi32>, tensor<2x1xi32>, tensor) -> tensor<2x3xi32> + func.return %1 : tensor<2x3xi32> + +// CHECK-LABEL: concat_v2_i64_axis +// CHECK: "tfl.concatenation"(%arg0, %arg1, %arg2) <{axis = -1 : i32, fused_activation_function = "NONE"}> : (tensor<2x1xi32>, tensor<2x1xi32>, tensor<2x1xi32>) -> tensor<2x3xi32> +} + +func.func @concat_v2_with_bool_type(%arg0: tensor, %arg1: tensor) -> tensor { + %0 = "tf.Const"() { value = dense<-1> : tensor } : () -> tensor + %1 = "tf.ConcatV2"(%arg0, %arg1, %0) : (tensor, tensor, tensor) -> tensor + func.return %1 : tensor + +// CHECK-LABEL: concat_v2_with_bool_type +// CHECK: "tfl.concatenation"(%arg0, %arg1) <{axis = -1 : i32, fused_activation_function = "NONE"}> : (tensor, tensor) -> tensor +} + +func.func @resize_with_bilinear(%arg0: tensor<1x100x100x3xf32>, %arg1: tensor<4xi32>) -> tensor { + %0 = "tf.ResizeBilinear"(%arg0, %arg1) {align_corners = true} : (tensor<1x100x100x3xf32>, tensor<4xi32>) -> tensor + func.return %0 : tensor + // CHECK-LABEL: resize_with_bilinear + // CHECK: "tfl.resize_bilinear"(%arg0, %arg1) <{align_corners = true, half_pixel_centers = false}> : (tensor<1x100x100x3xf32>, tensor<4xi32>) -> tensor +} + +func.func @resize_with_bilinear_with_half_pixel_centers(%arg0: tensor<1x100x100x3xf32>, %arg1: tensor<4xi32>) -> tensor { + %0 = "tf.ResizeBilinear"(%arg0, %arg1) {align_corners = false, half_pixel_centers = true} : (tensor<1x100x100x3xf32>, tensor<4xi32>) -> tensor + func.return %0 : tensor + // CHECK-LABEL: resize_with_bilinear_with_half_pixel_centers + // CHECK: "tfl.resize_bilinear"(%arg0, %arg1) <{align_corners = false, half_pixel_centers = true}> : (tensor<1x100x100x3xf32>, tensor<4xi32>) -> tensor +} + +func.func @strided_slice(%arg0: tensor<12x2x2x5xf32>, %arg1: tensor<1xi32>, %arg2: tensor<1xi32>, %arg3: tensor<1xi32>) -> tensor<1x2x2x5xf32> { + %0 = "tf.StridedSlice"(%arg0, %arg1, %arg2, %arg3) {begin_mask = 0 : i64, ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64, offset = false} : (tensor<12x2x2x5xf32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1x2x2x5xf32> + func.return %0 : tensor<1x2x2x5xf32> + // CHECK-LABEL: strided_slice + // CHECK: "tfl.strided_slice"(%arg0, %arg1, %arg2, %arg3) <{begin_mask = 0 : i32, ellipsis_mask = 0 : i32, end_mask = 0 : i32, new_axis_mask = 0 : i32, offset = false, shrink_axis_mask = 0 : i32}> : (tensor<12x2x2x5xf32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1x2x2x5xf32> +} + +func.func @strided_slice_with_constant_attributes(%arg0: tensor<10x10x10xf32>, %arg1: tensor<1xi32>, %arg2: tensor<1xi32>, %arg3: tensor<1xi32>) -> tensor<10x10xf32> { + %cst = arith.constant dense<-1> : tensor<1xi32> + %cst_1 = arith.constant dense<0> : tensor<1xi32> + %cst_2 = arith.constant dense<1> : tensor<1xi32> + %0 = "tf.StridedSlice"(%arg0, %cst, %cst_1, %cst_2) {begin_mask = 0 : i64, ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64, offset = false} : (tensor<10x10x10xf32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<10x10xf32> + func.return %0 : tensor<10x10xf32> + // CHECK-LABEL: strided_slice_with_constant_attributes + // CHECK-DAG: [[BEGIN:%cst.*]] = arith.constant dense<-1> : tensor<1xi32> + // CHECK-DAG: [[END:%cst.*]] = arith.constant dense<0> : tensor<1xi32> + // CHECK-DAG: [[STRIDES:%cst.*]] = arith.constant dense<1> : tensor<1xi32> + // CHECK-NEXT: "tfl.strided_slice"(%arg0, [[BEGIN]], [[END]], [[STRIDES]]) <{begin_mask = 0 : i32, ellipsis_mask = 0 : i32, end_mask = 0 : i32, new_axis_mask = 0 : i32, offset = false, shrink_axis_mask = 1 : i32}> : (tensor<10x10x10xf32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<10x10xf32> +} + +func.func @strided_slice_with_string(%arg0: tensor<12x2x2x5x!tf_type.string>, %arg1: tensor<1xi32>, %arg2: tensor<1xi32>, %arg3: tensor<1xi32>) -> tensor<1x2x2x5x!tf_type.string> { + %0 = "tf.StridedSlice"(%arg0, %arg1, %arg2, %arg3) {begin_mask = 0 : i64, ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64, offset = false} : (tensor<12x2x2x5x!tf_type.string>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1x2x2x5x!tf_type.string> + func.return %0 : tensor<1x2x2x5x!tf_type.string> + // CHECK-LABEL: strided_slice_with_string + // CHECK: "tfl.strided_slice"(%arg0, %arg1, %arg2, %arg3) <{begin_mask = 0 : i32, ellipsis_mask = 0 : i32, end_mask = 0 : i32, new_axis_mask = 0 : i32, offset = false, shrink_axis_mask = 0 : i32}> : (tensor<12x2x2x5x!tf_type.string>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1x2x2x5x!tf_type.string> +} + +func.func @strided_slice_with_unranked_input_and_i64_parameters(%arg0: tensor<*xf32>, %arg1: tensor<1xi64>, %arg2: tensor<1xi64>, %arg3: tensor<1xi64>) -> tensor<*xf32> { + %0 = "tf.StridedSlice"(%arg0, %arg1, %arg2, %arg3) {begin_mask = 0 : i64, ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64, offset = false} : (tensor<*xf32>, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor<*xf32> + func.return %0 : tensor<*xf32> + // CHECK-LABEL: strided_slice_with_unranked_input_and_i64_parameters + // CHECK-DAG: [[BEGIN:%.*]] = "tfl.cast"(%arg1) : (tensor<1xi64>) -> tensor<1xi32> + // CHECK-DAG: [[END:%.*]] = "tfl.cast"(%arg2) : (tensor<1xi64>) -> tensor<1xi32> + // CHECK-DAG: [[STRIDES:%.*]] = "tfl.cast"(%arg3) : (tensor<1xi64>) -> tensor<1xi32> + // CHECK-NEXT: "tfl.strided_slice"(%arg0, [[BEGIN]], [[END]], [[STRIDES]]) <{begin_mask = 0 : i32, ellipsis_mask = 0 : i32, end_mask = 0 : i32, new_axis_mask = 0 : i32, offset = false, shrink_axis_mask = 0 : i32}> : (tensor<*xf32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<*xf32> +} + +func.func @strided_slice_with_i64_parameters(%arg0: tensor<12x2x2x5xf32>, %arg1: tensor<1xi64>, %arg2: tensor<1xi64>, %arg3: tensor<1xi64>) -> tensor<1x2x2x5xf32> { + %0 = "tf.StridedSlice"(%arg0, %arg1, %arg2, %arg3) {begin_mask = 0 : i64, ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64, offset = false} : (tensor<12x2x2x5xf32>, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor<1x2x2x5xf32> + func.return %0 : tensor<1x2x2x5xf32> + // CHECK-LABEL: strided_slice_with_i64_parameters + // CHECK-DAG: [[BEGIN:%.*]] = "tfl.cast"(%arg1) : (tensor<1xi64>) -> tensor<1xi32> + // CHECK-DAG: [[END:%.*]] = "tfl.cast"(%arg2) : (tensor<1xi64>) -> tensor<1xi32> + // CHECK-DAG: [[STRIDES:%.*]] = "tfl.cast"(%arg3) : (tensor<1xi64>) -> tensor<1xi32> + // CHECK-NEXT: "tfl.strided_slice"(%arg0, [[BEGIN]], [[END]], [[STRIDES]]) <{begin_mask = 0 : i32, ellipsis_mask = 0 : i32, end_mask = 0 : i32, new_axis_mask = 0 : i32, offset = false, shrink_axis_mask = 0 : i32}> : (tensor<12x2x2x5xf32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1x2x2x5xf32> +} + +func.func @strided_slice_with_i64_constant_attributes(%arg0: tensor<10x10x10xf32>) -> tensor<10x10xf32> { + %cst = arith.constant dense<-1> : tensor<1xi64> + %cst_1 = arith.constant dense<0> : tensor<1xi64> + %cst_2 = arith.constant dense<1> : tensor<1xi64> + %0 = "tf.StridedSlice"(%arg0, %cst, %cst_1, %cst_2) {begin_mask = 0 : i64, ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 1 : i64, offset = false} : (tensor<10x10x10xf32>, tensor<1xi64>, tensor<1xi64>, tensor<1xi64>) -> tensor<10x10xf32> + func.return %0 : tensor<10x10xf32> + // CHECK-LABEL: strided_slice_with_i64_constant_attributes + // CHECK-DAG: [[BEGIN:%cst.*]] = arith.constant dense<-1> : tensor<1xi32> + // CHECK-DAG: [[END:%cst.*]] = arith.constant dense<0> : tensor<1xi32> + // CHECK-DAG: [[STRIDES:%cst.*]] = arith.constant dense<1> : tensor<1xi32> + // CHECK-NEXT: "tfl.strided_slice"(%arg0, [[BEGIN]], [[END]], [[STRIDES]]) <{begin_mask = 0 : i32, ellipsis_mask = 0 : i32, end_mask = 0 : i32, new_axis_mask = 0 : i32, offset = false, shrink_axis_mask = 1 : i32}> : (tensor<10x10x10xf32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<10x10xf32> +} + +func.func @strided_slice_non_zero_ellipsis_mask(%arg0: tensor<12x2x2x5xf32>, %arg1: tensor<1xi32>, %arg2: tensor<1xi32>, %arg3: tensor<1xi32>) -> tensor<1x2x2x5xf32> { + %0 = "tf.StridedSlice"(%arg0, %arg1, %arg2, %arg3) {begin_mask = 0 : i64, ellipsis_mask = 1 : i64, end_mask = 0 : i64, new_axis_mask = 0 : i64, shrink_axis_mask = 0 : i64, offset = false} : (tensor<12x2x2x5xf32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1x2x2x5xf32> + func.return %0 : tensor<1x2x2x5xf32> + // CHECK-LABEL: strided_slice_non_zero_ellipsis_mask + // CHECK: %0 = "tfl.strided_slice"(%arg0, %arg1, %arg2, %arg3) <{begin_mask = 0 : i32, ellipsis_mask = 1 : i32, end_mask = 0 : i32, new_axis_mask = 0 : i32, offset = false, shrink_axis_mask = 0 : i32}> : (tensor<12x2x2x5xf32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1x2x2x5xf32> +} + +func.func @strided_slice_non_zero_new_axis_mask(%arg0: tensor<12x2x2x5xf32>, %arg1: tensor<1xi32>, %arg2: tensor<1xi32>, %arg3: tensor<1xi32>) -> tensor<1x2x2x5xf32> { + %0 = "tf.StridedSlice"(%arg0, %arg1, %arg2, %arg3) {begin_mask = 0 : i64, ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 2 : i64, shrink_axis_mask = 0 : i64, offset = false} : (tensor<12x2x2x5xf32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1x2x2x5xf32> + func.return %0 : tensor<1x2x2x5xf32> + // CHECK-LABEL: strided_slice_non_zero_new_axis_mask + // CHECK: "tfl.strided_slice"(%arg0, %arg1, %arg2, %arg3) <{begin_mask = 0 : i32, ellipsis_mask = 0 : i32, end_mask = 0 : i32, new_axis_mask = 2 : i32, offset = false, shrink_axis_mask = 0 : i32}> : (tensor<12x2x2x5xf32>, tensor<1xi32>, tensor<1xi32>, tensor<1xi32>) -> tensor<1x2x2x5xf32> +} + +func.func @strided_slice_big_dims(%arg0: tensor<5x6x7xf32>, %arg1: tensor<3xi32>, %arg2: tensor<3xi32>, %arg3: tensor<3xi32>) -> tensor<1x1x5x6x7xf32> { + %0 = "tf.StridedSlice"(%arg0, %arg1, %arg2, %arg3) {begin_mask = 0 : i64, ellipsis_mask = 0 : i64, end_mask = 0 : i64, new_axis_mask = 7 : i64, shrink_axis_mask = 0 : i64, offset = false} : (tensor<5x6x7xf32>, tensor<3xi32>, tensor<3xi32>, tensor<3xi32>) -> tensor<1x1x5x6x7xf32> + func.return %0 : tensor<1x1x5x6x7xf32> + // CHECK-LABEL: strided_slice_big_dims + // CHECK: %0 = "tfl.strided_slice"(%arg0, %arg1, %arg2, %arg3) <{begin_mask = 0 : i32, ellipsis_mask = 0 : i32, end_mask = 0 : i32, new_axis_mask = 7 : i32, offset = false, shrink_axis_mask = 0 : i32}> : (tensor<5x6x7xf32>, tensor<3xi32>, tensor<3xi32>, tensor<3xi32>) -> tensor<1x1x5x6x7xf32> +} + +func.func @slice1Tensor(%arg0: tensor<2x3x5xf32>, %arg1: tensor<3xi32>, %arg2: tensor<3xi32>) -> tensor { + %0 = "tf.Slice"(%arg0, %arg1, %arg2) : (tensor<2x3x5xf32>, tensor<3xi32>, tensor<3xi32>) -> tensor + func.return %0 : tensor + // CHECK-LABEL: slice1Tensor + // CHECK: "tfl.slice"(%arg0, %arg1, %arg2) : (tensor<2x3x5xf32>, tensor<3xi32>, tensor<3xi32>) -> tensor +} + +func.func @mirror_pad(tensor<2x1x3xf32>, tensor<3x2xi32>) -> tensor { +^bb0(%arg0: tensor<2x1x3xf32>, %arg1: tensor<3x2xi32>): + %0 = "tf.MirrorPad"(%arg0, %arg1) { mode = "SYMMETRIC" }: (tensor<2x1x3xf32>, tensor<3x2xi32>) -> tensor + func.return %0#0 : tensor + + // CHECK-LABEL: mirror_pad + // CHECK: "tfl.mirror_pad"(%arg0, %arg1) <{mode = #tfl}> : (tensor<2x1x3xf32>, tensor<3x2xi32>) -> tensor + // CHECK: return +} + +func.func @mirror_pad_reflect(tensor<2x1x3xf32>, tensor<3x2xi32>) -> tensor { +^bb0(%arg0: tensor<2x1x3xf32>, %arg1: tensor<3x2xi32>): + %0 = "tf.MirrorPad"(%arg0, %arg1) { mode = "REFLECT" }: (tensor<2x1x3xf32>, tensor<3x2xi32>) -> tensor + func.return %0#0 : tensor + + // CHECK-LABEL: mirror_pad_reflect + // CHECK: "tfl.mirror_pad"(%arg0, %arg1) <{mode = #tfl}> : (tensor<2x1x3xf32>, tensor<3x2xi32>) -> tensor + // CHECK: return +} + +func.func @Tanh(%arg0: tensor<1xf32>) -> tensor<1xf32> { + %0 = "tf.Tanh"(%arg0) : (tensor<1xf32>) -> tensor<1xf32> + func.return %0: tensor<1xf32> + +// CHECK-LABEL: Tanh +// CHECK: "tfl.tanh"(%arg0) : (tensor<1xf32>) -> tensor<1xf32> +} + +func.func @cast(%arg0: tensor<1x2x2x5xi32>) -> tensor<1x2x2x5xf32> { + %0 = "tf.Cast"(%arg0) : (tensor<1x2x2x5xi32>) -> tensor<1x2x2x5xf32> + func.return %0 : tensor<1x2x2x5xf32> + + // CHECK-LABEL: cast + // CHECK: "tfl.cast"(%arg0) : (tensor<1x2x2x5xi32>) -> tensor<1x2x2x5xf32> +} + +func.func @castFloat32ToI16(%arg0: tensor<1x2x2x5xf32>) -> tensor<1x2x2x5xi16> { + %0 = "tf.Cast"(%arg0) : (tensor<1x2x2x5xf32>) -> tensor<1x2x2x5xi16> + func.return %0 : tensor<1x2x2x5xi16> + + // CHECK-LABEL: castFloat32ToI16 + // CHECK: "tfl.cast"(%arg0) : (tensor<1x2x2x5xf32>) -> tensor<1x2x2x5xi16> +} + +func.func @castI16ToFloat32(%arg0: tensor<1x2x2x5xi16>) -> tensor<1x2x2x5xf32> { + %0 = "tf.Cast"(%arg0) : (tensor<1x2x2x5xi16>) -> tensor<1x2x2x5xf32> + func.return %0 : tensor<1x2x2x5xf32> + + // CHECK-LABEL: castI16ToFloat32 + // CHECK: "tfl.cast"(%arg0) : (tensor<1x2x2x5xi16>) -> tensor<1x2x2x5xf32> +} + +func.func @castComplex(%arg0: tensor<1x2x2x5xf32>) -> tensor<1x2x2x5xcomplex> { + %0 = "tf.Cast"(%arg0) : (tensor<1x2x2x5xf32>) -> tensor<1x2x2x5xcomplex> + func.return %0 : tensor<1x2x2x5xcomplex> + + // CHECK-LABEL: castComplex + // CHECK: "tfl.cast"(%arg0) : (tensor<1x2x2x5xf32>) -> tensor<1x2x2x5xcomplex> +} + +func.func @unique(%arg0: tensor<5xf32>) -> (tensor, tensor) { + %0, %1 = "tf.Unique"(%arg0) : (tensor<5xf32>) -> (tensor, tensor) + func.return %0, %1 : tensor , tensor + + // CHECK-LABEL: unique + // CHECK: "tfl.unique"(%arg0) : (tensor<5xf32>) -> (tensor, tensor) +} + +func.func @unique64(%arg0: tensor<5xf32>) -> (tensor, tensor) { + %0, %1 = "tf.Unique"(%arg0) : (tensor<5xf32>) -> (tensor, tensor) + func.return %0, %1 : tensor , tensor + + // CHECK-LABEL: unique64 + // CHECK: "tfl.unique"(%arg0) : (tensor<5xf32>) -> (tensor, tensor) +} + +func.func @ReverseSequence(%arg0: tensor<2x3xf32>, %arg1: tensor<2xi32>) -> tensor<2x3xf32> { + %0 = "tf.ReverseSequence"(%arg0, %arg1) {seq_dim = 0 : i64, batch_dim = 0 : i64}: (tensor<2x3xf32>, tensor<2xi32>) -> tensor<2x3xf32> + func.return %0: tensor<2x3xf32> + +// CHECK-LABEL: ReverseSequence +// CHECK: "tfl.reverse_sequence"(%arg0, %arg1) <{batch_dim = 0 : i32, seq_dim = 0 : i32}> : (tensor<2x3xf32>, tensor<2xi32>) -> tensor<2x3xf32> +} + +func.func @LRN(%arg0: tensor<2x3x4x5xf32>) -> tensor<2x3x4x5xf32> { + %0 = "tf.LRN"(%arg0) {depth_radius = 5 :i64, bias = 1.0 :f32, alpha = 1.0 : f32, beta = 0.5 :f32} : (tensor<2x3x4x5xf32>) -> (tensor<2x3x4x5xf32>) + func.return %0: tensor<2x3x4x5xf32> + + // CHECK-LABEL: LRN + // CHECK: "tfl.local_response_normalization"(%arg0) <{alpha = 1.000000e+00 : f32, beta = 5.000000e-01 : f32, bias = 1.000000e+00 : f32, radius = 5 : i32}> : (tensor<2x3x4x5xf32>) -> tensor<2x3x4x5xf32> + // CHECK: return %0 : tensor<2x3x4x5xf32> +} + +func.func @OneHot(%arg0: tensor<3xi32>, %arg1: tensor, %arg2: tensor, %arg3: tensor) -> tensor<*xf32> { + %0 = "tf.OneHot"(%arg0, %arg1, %arg2, %arg3) {axis = -1 : i64} : (tensor<3xi32>, tensor, tensor, tensor) -> tensor<*xf32> + func.return %0: tensor<*xf32> + +// CHECK-LABEL: OneHot +// CHECK: "tfl.one_hot"(%arg0, %arg1, %arg2, %arg3) <{axis = -1 : i32}> : (tensor<3xi32>, tensor, tensor, tensor) -> tensor<*xf32> +} + +func.func @argmax(%arg0: tensor<3xi32>, %arg1: tensor) -> tensor { + %0 = "tf.ArgMax"(%arg0, %arg1) : (tensor<3xi32>, tensor) -> tensor + func.return %0 : tensor + +// CHECK-LABEL: argmax +// CHECK: "tfl.arg_max"(%arg0, %arg1) : (tensor<3xi32>, tensor) -> tensor +} + +func.func @argmax64(%arg0: tensor<3xi32>, %arg1: tensor) -> tensor { + %0 = "tf.ArgMax"(%arg0, %arg1) : (tensor<3xi32>, tensor) -> tensor + func.return %0 : tensor + +// CHECK-LABEL: argmax64 +// CHECK: "tfl.arg_max"(%arg0, %arg1) : (tensor<3xi32>, tensor) -> tensor +} + +func.func @space_to_depth(%arg0: tensor<1x2x2x1xf32>) -> tensor { + %0 = "tf.SpaceToDepth"(%arg0) {block_size = 2: i64, data_format = "NHWC"}: (tensor<1x2x2x1xf32>) -> tensor + func.return %0 : tensor + + // CHECK-LABEL: space_to_depth + // CHECK: %[[ARG:.*]]: tensor<1x2x2x1xf32> + // CHECK: "tfl.space_to_depth"(%[[ARG]]) <{block_size = 2 : i32}> : (tensor<1x2x2x1xf32>) -> tensor +} + +func.func @round(%arg0: tensor<8x16xf32>) -> tensor<8x16xf32> { + %0 = "tf.Round"(%arg0) : (tensor<8x16xf32>) -> tensor<8x16xf32> + func.return %0 : tensor<8x16xf32> + + // CHECK-LABEL: round + // CHECK: %[[ARG:.*]]: tensor<8x16xf32> + // CHECK: %[[RESULT:.*]] = "tfl.round"(%[[ARG]]) : (tensor<8x16xf32>) -> tensor<8x16xf32> + // CHECK: return %[[RESULT]] : tensor<8x16xf32> +} + +func.func @resize_nearest_neighbor(%arg0: tensor<1x100x100x3xf32>, %arg1: tensor<4xi32>) -> tensor { + %0 = "tf.ResizeNearestNeighbor"(%arg0, %arg1) {align_corners = true} : (tensor<1x100x100x3xf32>, tensor<4xi32>) -> tensor + func.return %0 : tensor + // CHECK-LABEL: resize_nearest_neighbor + // CHECK: "tfl.resize_nearest_neighbor"(%arg0, %arg1) <{align_corners = true, half_pixel_centers = false}> : (tensor<1x100x100x3xf32>, tensor<4xi32>) -> tensor +} + +func.func @resize_nearest_neighbor_with_half_pixel_centers(%arg0: tensor<1x100x100x3xf32>, %arg1: tensor<4xi32>) -> tensor { + %0 = "tf.ResizeNearestNeighbor"(%arg0, %arg1) {align_corners = false, half_pixel_centers = true} : (tensor<1x100x100x3xf32>, tensor<4xi32>) -> tensor + func.return %0 : tensor + // CHECK-LABEL: resize_nearest_neighbor_with_half_pixel_centers + // CHECK: "tfl.resize_nearest_neighbor"(%arg0, %arg1) <{align_corners = false, half_pixel_centers = true}> : (tensor<1x100x100x3xf32>, tensor<4xi32>) -> tensor +} + +func.func @sparse_to_dense_with_scalar_sparse_indices(%arg0: tensor, %arg1: tensor<3xi32>, %arg2: tensor, %arg3: tensor) -> tensor { + %0 = "tf.SparseToDense"(%arg0, %arg1, %arg2, %arg3) {validate_indices = true}: (tensor, tensor<3xi32>, tensor, tensor) -> tensor + func.return %0 : tensor + // CHECK-LABEL: sparse_to_dense_with_scalar_sparse_indices + // CHECK: "tfl.sparse_to_dense"(%arg0, %arg1, %arg2, %arg3) : (tensor, tensor<3xi32>, tensor, tensor) -> tensor +} + +func.func @sparse_to_dense_with_vector_sparse_indices(%arg0: tensor<3xi32>, %arg1: tensor<3xi32>, %arg2: tensor<3xf32>, %arg3: tensor) -> tensor { + %0 = "tf.SparseToDense"(%arg0, %arg1, %arg2, %arg3) {validate_indices = true}: (tensor<3xi32>, tensor<3xi32>, tensor<3xf32>, tensor) -> tensor + func.return %0 : tensor + // CHECK-LABEL: sparse_to_dense_with_vector_sparse_indices + // CHECK: "tfl.sparse_to_dense"(%arg0, %arg1, %arg2, %arg3) : (tensor<3xi32>, tensor<3xi32>, tensor<3xf32>, tensor) -> tensor +} + +func.func @sparse_to_dense_with_2d_sparse_indices(%arg0: tensor<3x2xi32>, %arg1: tensor<3xi32>, %arg2: tensor<2xf32>, %arg3: tensor) -> tensor { + %0 = "tf.SparseToDense"(%arg0, %arg1, %arg2, %arg3) {validate_indices = true}: (tensor<3x2xi32>, tensor<3xi32>, tensor<2xf32>, tensor) -> tensor + func.return %0 : tensor + // CHECK-LABEL: sparse_to_dense_with_2d_sparse_indices + // CHECK: "tfl.sparse_to_dense"(%arg0, %arg1, %arg2, %arg3) : (tensor<3x2xi32>, tensor<3xi32>, tensor<2xf32>, tensor) -> tensor +} + +func.func @sparse_to_dense_with_2d_sparse_indices_and_second_dim_greater_than_4(%arg0: tensor<3x5xi32>, %arg1: tensor<3xi32>, %arg2: tensor<2xf32>, %arg3: tensor) -> tensor { + %0 = "tf.SparseToDense"(%arg0, %arg1, %arg2, %arg3) {validate_indices = true}: (tensor<3x5xi32>, tensor<3xi32>, tensor<2xf32>, tensor) -> tensor + func.return %0 : tensor + // CHECK-LABEL: sparse_to_dense_with_2d_sparse_indices_and_second_dim_greater_than_4 + // CHECK: "tf.SparseToDense"(%arg0, %arg1, %arg2, %arg3) <{validate_indices = true}> : (tensor<3x5xi32>, tensor<3xi32>, tensor<2xf32>, tensor) -> tensor +} + +func.func @where(%arg0: tensor<3x5xi1>) -> tensor { + %0 = "tf.Where"(%arg0) : (tensor<3x5xi1>) -> tensor + func.return %0 : tensor + // CHECK-LABEL: where + // CHECK: "tfl.where"(%arg0) : (tensor<3x5xi1>) -> tensor +} + +func.func @floor_mod(%arg0: tensor<5xf32>, %arg1: tensor<5xf32>) -> tensor<5xf32> { + %0 = "tf.FloorMod"(%arg0, %arg1) : (tensor<5xf32>, tensor<5xf32>) -> tensor<5xf32> + func.return %0 : tensor<5xf32> + // CHECK-LABEL: floor_mod + // CHECK: "tfl.floor_mod"(%arg0, %arg1) : (tensor<5xf32>, tensor<5xf32>) -> tensor<5xf32> +} + +func.func @floor_mod_i16(%arg0: tensor<5xi16>, %arg1: tensor<5xi16>) -> tensor<5xi16> { + %0 = "tf.FloorMod"(%arg0, %arg1) : (tensor<5xi16>, tensor<5xi16>) -> tensor<5xi16> + func.return %0 : tensor<5xi16> + // CHECK-LABEL: floor_mod_i16 + // CHECK: "tfl.floor_mod"(%arg0, %arg1) : (tensor<5xi16>, tensor<5xi16>) -> tensor<5xi16> +} + +func.func @exp(%arg0: tensor<5xf32>) -> tensor<5xf32> { + %0 = "tf.Exp"(%arg0) : (tensor<5xf32>) -> tensor<5xf32> + func.return %0 : tensor<5xf32> + // CHECK-LABEL: exp + // CHECK: "tfl.exp"(%arg0) : (tensor<5xf32>) -> tensor<5xf32> +} + +func.func @depth_to_space(%arg0: tensor<1x1x1x4xf32>) -> tensor<1x2x2x1xf32> { + %0 = "tf.DepthToSpace"(%arg0) {block_size = 2: i64, data_format = "NHWC"}: (tensor<1x1x1x4xf32>) -> tensor<1x2x2x1xf32> + func.return %0 : tensor<1x2x2x1xf32> + + // CHECK-LABEL: depth_to_space + // CHECK: %[[ARG:.*]]: tensor<1x1x1x4xf32> + // CHECK: "tfl.depth_to_space"(%[[ARG]]) <{block_size = 2 : i32}> : (tensor<1x1x1x4xf32>) -> tensor<1x2x2x1xf32> +} + +func.func @non_max_suppression_v4(%arg0: tensor<3x4xf32>, %arg1: tensor<3xf32>, %arg2: tensor, %arg3: tensor, %arg4: tensor) -> tensor<2xi32> { + %0:2 = "tf.NonMaxSuppressionV4"(%arg0, %arg1, %arg2, %arg3, %arg4) {pad_to_max_output_size = true}: (tensor<3x4xf32>, tensor<3xf32>, tensor, tensor, tensor) -> (tensor<2xi32>, tensor) + func.return %0#0 : tensor<2xi32> + + // CHECK-LABEL: non_max_suppression_v4 + // CHECK: "tfl.non_max_suppression_v4"(%arg0, %arg1, %arg2, %arg3, %arg4) : (tensor<3x4xf32>, tensor<3xf32>, tensor, tensor, tensor) -> (tensor<2xi32>, tensor) +} + +func.func @non_max_suppression_v4_no_pad(%arg0: tensor<3x4xf32>, %arg1: tensor<3xf32>, %arg2: tensor, %arg3: tensor, %arg4: tensor) -> tensor<2xi32> { + %0:2 = "tf.NonMaxSuppressionV4"(%arg0, %arg1, %arg2, %arg3, %arg4) {pad_to_max_output_size = false}: (tensor<3x4xf32>, tensor<3xf32>, tensor, tensor, tensor) -> (tensor<2xi32>, tensor) + func.return %0#0 : tensor<2xi32> + + // CHECK-LABEL: non_max_suppression_v4_no_pad + // CHECK: "tfl.non_max_suppression_v4"(%arg0, %arg1, %arg2, %arg3, %arg4) : (tensor<3x4xf32>, tensor<3xf32>, tensor, tensor, tensor) -> (tensor<2xi32>, tensor) +} + +func.func @non_max_suppression_v5(%arg0: tensor<3x4xf32>, %arg1: tensor<3xf32>, %arg2: tensor, %arg3: tensor, %arg4: tensor, %arg5: tensor) -> tensor<2xi32> { + %0:3 = "tf.NonMaxSuppressionV5"(%arg0, %arg1, %arg2, %arg3, %arg4, %arg5) {pad_to_max_output_size = true}: (tensor<3x4xf32>, tensor<3xf32>, tensor, tensor, tensor, tensor) -> (tensor<2xi32>, tensor<2xf32>, tensor) + func.return %0#0 : tensor<2xi32> + + // CHECK-LABEL: non_max_suppression_v5 + // CHECK: "tfl.non_max_suppression_v5"(%arg0, %arg1, %arg2, %arg3, %arg4, %arg5) : (tensor<3x4xf32>, tensor<3xf32>, tensor, tensor, tensor, tensor) -> (tensor<2xi32>, tensor<2xf32>, tensor) +} + +func.func @non_max_suppression_v5_no_pad(%arg0: tensor<3x4xf32>, %arg1: tensor<3xf32>, %arg2: tensor, %arg3: tensor, %arg4: tensor, %arg5: tensor) -> tensor<2xi32> { + %0:3 = "tf.NonMaxSuppressionV5"(%arg0, %arg1, %arg2, %arg3, %arg4, %arg5) {pad_to_max_output_size = false}: (tensor<3x4xf32>, tensor<3xf32>, tensor, tensor, tensor, tensor) -> (tensor<2xi32>, tensor<2xf32>, tensor) + func.return %0#0 : tensor<2xi32> + + // CHECK-LABEL: non_max_suppression_v5_no_pad + // CHECK: "tfl.non_max_suppression_v5"(%arg0, %arg1, %arg2, %arg3, %arg4, %arg5) : (tensor<3x4xf32>, tensor<3xf32>, tensor, tensor, tensor, tensor) -> (tensor<2xi32>, tensor<2xf32>, tensor) +} + +func.func @conv2d_backprop_input(%arg0: tensor<4xi32>, %arg1: tensor<3x3x1x32xf32>, %arg2: tensor<15x14x14x32xf32>) -> tensor<15x28x28x1xf32> { + %0 = "tf.Conv2DBackpropInput"(%arg0, %arg1, %arg2) {strides = [1, 2, 2, 1], padding="SAME", dilations=[1, 1, 1, 1]}: (tensor<4xi32>, tensor<3x3x1x32xf32>, tensor<15x14x14x32xf32>) -> tensor<15x28x28x1xf32> + %1 = "tf.Conv2DBackpropInput"(%arg0, %arg1, %arg2) {strides = [1, 2, 2, 1], padding="VALID", dilations=[1, 1, 1, 1]}: (tensor<4xi32>, tensor<3x3x1x32xf32>, tensor<15x14x14x32xf32>) -> tensor<15x28x28x1xf32> + %2 = "tf.Add"(%0, %1): (tensor<15x28x28x1xf32>, tensor<15x28x28x1xf32>) -> tensor<15x28x28x1xf32> + func.return %2 : tensor<15x28x28x1xf32> + + // CHECK-LABEL: conv2d_backprop_input + // CHECK: %[[CST:.*]] = arith.constant dense<[2, 0, 1, 3]> : tensor<4xi32> + // CHECK: %[[ARG0:.*]] = "tfl.transpose"(%arg1, %[[CST]]) : (tensor<3x3x1x32xf32>, tensor<4xi32>) -> tensor<1x3x3x32xf32> + // CHECK: %[[CST_0:.*]] = "tfl.no_value"() <{value}> : () -> none + // CHECK: %[[ARG1:.*]] = "tfl.transpose_conv"(%arg0, %[[ARG0]], %arg2, %[[CST_0]]) <{fused_activation_function = "NONE", padding = "SAME", stride_h = 2 : i32, stride_w = 2 : i32}> : (tensor<4xi32>, tensor<1x3x3x32xf32>, tensor<15x14x14x32xf32>, none) -> tensor<15x28x28x1xf32> + // CHECK: %[[ARG3:.*]] = "tfl.transpose_conv"(%arg0, %[[ARG0]], %arg2, %[[CST_0]]) <{fused_activation_function = "NONE", padding = "VALID", stride_h = 2 : i32, stride_w = 2 : i32}> : (tensor<4xi32>, tensor<1x3x3x32xf32>, tensor<15x14x14x32xf32>, none) -> tensor<15x28x28x1xf32> + // CHECK: %[[RESULT:.*]] = tfl.add %[[ARG1]], %[[ARG3]] {fused_activation_function = "NONE"} : tensor<15x28x28x1xf32> + // CHECK: return %[[RESULT]] : tensor<15x28x28x1xf32> +} + +func.func @conv2d_backprop_input_unsupported_paddings(%arg0: tensor<4xi32>, %arg1: tensor<3x3x1x32xf32>, %arg2: tensor<15x14x14x32xf32>) -> tensor<15x28x28x1xf32> { + %0 = "tf.Conv2DBackpropInput"(%arg0, %arg1, %arg2) {strides = [1, 2, 2, 1], explicit_paddings = [1, 1, 1, 1, 1, 1, 1, 1], padding="EXPLICIT", dilations=[1, 1, 1, 1]}: (tensor<4xi32>, tensor<3x3x1x32xf32>, tensor<15x14x14x32xf32>) -> tensor<15x28x28x1xf32> + func.return %0 : tensor<15x28x28x1xf32> + + // CHECK-LABEL: conv2d_backprop_input_unsupported_paddings + // CHECK: tf.Conv2DBackpropInput +} + +func.func @conv2d_backprop_unsupported_strides(%arg0: tensor<4xi32>, %arg1: tensor<3x3x1x32xf32>, %arg2: tensor<15x14x14x32xf32>) -> tensor<15x28x28x1xf32> { + %0 = "tf.Conv2DBackpropInput"(%arg0, %arg1, %arg2) {dilations = [1, 1, 1, 1], padding="SAME", strides = [2, 2, 2, 2]}: (tensor<4xi32>, tensor<3x3x1x32xf32>, tensor<15x14x14x32xf32>) -> tensor<15x28x28x1xf32> + func.return %0 : tensor<15x28x28x1xf32> + + // CHECK-LABEL: conv2d_backprop_unsupported_strides + // CHECK: tf.Conv2DBackpropInput +} + +func.func @conv2d_backprop_unsupported_data_format(%arg0: tensor<4xi32>, %arg1: tensor<3x3x1x32xf32>, %arg2: tensor<15x14x14x32xf32>) -> tensor<15x28x28x1xf32> { + %0 = "tf.Conv2DBackpropInput"(%arg0, %arg1, %arg2) {data_format = "NCHW", dilations = [1, 1, 1, 1], padding="SAME", strides = [1, 2, 2, 1]}: (tensor<4xi32>, tensor<3x3x1x32xf32>, tensor<15x14x14x32xf32>) -> tensor<15x28x28x1xf32> + func.return %0 : tensor<15x28x28x1xf32> + + // CHECK-LABEL: conv2d_backprop_unsupported_data_format + // CHECK: tf.Conv2DBackpropInput +} + +func.func @assert_remove(%arg0: tensor<1xi32>, %arg1: tensor<1xi32>) -> tensor<1xi1> { + %0 = "tf.LessEqual"(%arg0, %arg1) : (tensor<1xi32>, tensor<1xi32>) -> tensor<1xi1> + "tf.Assert"(%0, %arg1) {summarize = 3} : (tensor<1xi1>, tensor<1xi32>) -> () + func.return %0 : tensor<1xi1> + // CHECK-LABEL: assert_remove + // CHECK: tfl.less_equal + // CHECK-NOT: Assert + // CHECK: return +} + +func.func @reciprocal_f32(%arg0: tensor<8xf32>) -> tensor<8xf32> { + %0 = "tf.Reciprocal"(%arg0) : (tensor<8xf32>) -> tensor<8xf32> + func.return %0: tensor<8xf32> + +// CHECK-LABEL: reciprocal_f32 +// CHECK: %cst = arith.constant dense<1.000000e+00> : tensor +// CHECK: tfl.div(%cst, %arg0) <{fused_activation_function = "NONE"}> : (tensor, tensor<8xf32>) -> tensor<8xf32> +// CHECK: return +} + +func.func @reciprocal_i32(%arg0: tensor<8xi32>) -> tensor<8xi32> { + %0 = "tf.Reciprocal"(%arg0) : (tensor<8xi32>) -> tensor<8xi32> + func.return %0: tensor<8xi32> + +// CHECK-LABEL: reciprocal_i32 +// CHECK: %cst = arith.constant dense<1> : tensor +// CHECK: tfl.div(%cst, %arg0) <{fused_activation_function = "NONE"}> : (tensor, tensor<8xi32>) -> tensor<8xi32> +// CHECK: return +} + +func.func @LstmWithoutProjection(%arg: tensor<28x1x28xf32>) -> (tensor<28x1x16xf32>) { + %1 = "tf.Const"() {device = "", dtype = f32, value = dense<0.000000e+00>: tensor<16x28xf32>} : () -> tensor<16x28xf32> + %2 = "tf.Const"() {device = "", dtype = f32, value = dense<0.000000e+00>: tensor<16x16xf32>} : () -> tensor<16x16xf32> + %3 = "tf.Const"() {device = "", dtype = f32, value = dense<0.000000e+00>: tensor<16xf32>} : () -> tensor<16xf32> + %4 = "tf.Const"() {device = "", dtype = f32, value = dense<0.000000e+00>: tensor<1x16xf32>} : () -> tensor<1x16xf32> + %5 = "tf.Const"() {device = "", dtype = f32, value = dense<-1.000000e+00> : tensor<1xf32>} : () -> tensor<1xf32> + %6:3 = "tf.UnidirectionalSequenceLstm"(%arg, %1, %1, %1, %1, %2, %2, %2, %2, %3, %3, %3, %3, %3, %3, %3, %5, %5, %4, %4) {_tflite_input_indices = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18, 19], device = ""} : (tensor<28x1x28xf32>, tensor<16x28xf32>, tensor<16x28xf32>, tensor<16x28xf32>, tensor<16x28xf32>, tensor<16x16xf32>, tensor<16x16xf32>, tensor<16x16xf32>, tensor<16x16xf32>, tensor<16xf32>, tensor<16xf32>, tensor<16xf32>, tensor<16xf32>, tensor<16xf32>, tensor<16xf32>, tensor<16xf32>, tensor<1xf32>, tensor<1xf32>, tensor<1x16xf32>, tensor<1x16xf32>) -> (tensor<*xf32>, tensor<*xf32>, tensor<28x1x16xf32>) + func.return %6#2 : tensor<28x1x16xf32> +} + +// CHECK: func @LstmWithoutProjection([[VAL_0:%.*]]: tensor<28x1x28xf32>) -> tensor<28x1x16xf32> { +// CHECK-DAG: [[VAL_1:%.*]] = arith.constant dense<0.000000e+00> : tensor<16x28xf32> +// CHECK-DAG: [[VAL_2:%.*]] = arith.constant dense<0.000000e+00> : tensor<16x16xf32> +// CHECK-DAG: [[VAL_3:%.*]] = arith.constant dense<0.000000e+00> : tensor<16xf32> +// CHECK-DAG: [[VAL_4:%.*]] = arith.constant dense<0.000000e+00> : tensor<1x16xf32> +// CHECK-DAG: [[VAL_5:%.*]] = "tfl.no_value"() <{value}> : () -> none +// CHECK: [[VAL_6:%.*]] = "tfl.unidirectional_sequence_lstm"([[VAL_0]], [[VAL_1]], [[VAL_1]], [[VAL_1]], [[VAL_1]], [[VAL_2]], [[VAL_2]], [[VAL_2]], [[VAL_2]], [[VAL_3]], [[VAL_3]], [[VAL_3]], [[VAL_3]], [[VAL_3]], [[VAL_3]], [[VAL_3]], [[VAL_5]], [[VAL_5]], [[VAL_4]], [[VAL_4]], [[VAL_5]], [[VAL_5]], [[VAL_5]], [[VAL_5]]) <{cell_clip = 0.000000e+00 : f32, fused_activation_function = "TANH", proj_clip = 0.000000e+00 : f32, time_major = true}> : (tensor<28x1x28xf32>, tensor<16x28xf32>, tensor<16x28xf32>, tensor<16x28xf32>, tensor<16x28xf32>, tensor<16x16xf32>, tensor<16x16xf32>, tensor<16x16xf32>, tensor<16x16xf32>, tensor<16xf32>, tensor<16xf32>, tensor<16xf32>, tensor<16xf32>, tensor<16xf32>, tensor<16xf32>, tensor<16xf32>, none, none, tensor<1x16xf32>, tensor<1x16xf32>, none, none, none, none) -> tensor<28x1x16xf32> +// CHECK: return [[VAL_6]] : tensor<28x1x16xf32> +// CHECK: } + +func.func @LstmWithProjection(%arg: tensor<28x1x16xf32>) -> (tensor<28x1x8xf32>) { + %1 = "tf.Const"() {device = "", dtype = f32, value = dense<0.000000e+00>: tensor<16x16xf32>} : () -> tensor<16x16xf32> + %2 = "tf.Const"() {device = "", dtype = f32, value = dense<0.000000e+00>: tensor<16x8xf32>} : () -> tensor<16x8xf32> + %3 = "tf.Const"() {device = "", dtype = f32, value = dense<0.000000e+00>: tensor<16xf32>} : () -> tensor<16xf32> + %4 = "tf.Const"() {device = "", dtype = f32, value = dense<0.000000e+00>: tensor<1x16xf32>} : () -> tensor<1x16xf32> + %5 = "tf.Const"() {device = "", dtype = f32, value = dense<0.000000e+00>: tensor<8x16xf32>} : () -> tensor<8x16xf32> + %6 = "tf.Const"() {device = "", dtype = f32, value = dense<0.000000e+00>: tensor<1x8xf32>} : () -> tensor<1x8xf32> + %7 = "tf.Const"() {device = "", dtype = f32, value = dense<-1.000000e+00> : tensor<1xf32>} : () -> tensor<1xf32> + %8:3 = "tf.UnidirectionalSequenceLstm"(%arg, %1, %1, %1, %1, %2, %2, %2, %2, %7, %7, %7, %3, %3, %3, %3, %5, %7, %6, %4) {_tflite_input_indices = [0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 13, 14, 15, 16, 18, 19], device = ""} : (tensor<28x1x16xf32>, tensor<16x16xf32>, tensor<16x16xf32>, tensor<16x16xf32>, tensor<16x16xf32>, tensor<16x8xf32>, tensor<16x8xf32>, tensor<16x8xf32>, tensor<16x8xf32>, tensor<1xf32>, tensor<1xf32>, tensor<1xf32>, tensor<16xf32>, tensor<16xf32>, tensor<16xf32>, tensor<16xf32>, tensor<8x16xf32>, tensor<1xf32>, tensor<1x8xf32>, tensor<1x16xf32>) -> (tensor<*xf32>, tensor<*xf32>, tensor<28x1x8xf32>) + func.return %8#2 : tensor<28x1x8xf32> +} + +// CHECK-LABEL: func @LstmWithProjection( +// CHECK-SAME: [[VAL_7:%.*]]: tensor<28x1x16xf32>) -> tensor<28x1x8xf32> { +// CHECK-DAG: [[VAL_8:%.*]] = arith.constant dense<0.000000e+00> : tensor<16x16xf32> +// CHECK-DAG: [[VAL_9:%.*]] = arith.constant dense<0.000000e+00> : tensor<16x8xf32> +// CHECK-DAG: [[VAL_10:%.*]] = arith.constant dense<0.000000e+00> : tensor<16xf32> +// CHECK-DAG: [[VAL_11:%.*]] = arith.constant dense<0.000000e+00> : tensor<1x16xf32> +// CHECK-DAG: [[VAL_12:%.*]] = arith.constant dense<0.000000e+00> : tensor<8x16xf32> +// CHECK-DAG: [[VAL_13:%.*]] = arith.constant dense<0.000000e+00> : tensor<1x8xf32> +// CHECK-DAG: [[VAL_14:%.*]] = "tfl.no_value"() <{value}> : () -> none +// CHECK: [[VAL_15:%.*]] = "tfl.unidirectional_sequence_lstm"([[VAL_7]], [[VAL_8]], [[VAL_8]], [[VAL_8]], [[VAL_8]], [[VAL_9]], [[VAL_9]], [[VAL_9]], [[VAL_9]], [[VAL_14]], [[VAL_14]], [[VAL_14]], [[VAL_10]], [[VAL_10]], [[VAL_10]], [[VAL_10]], [[VAL_12]], [[VAL_14]], [[VAL_13]], [[VAL_11]], [[VAL_14]], [[VAL_14]], [[VAL_14]], [[VAL_14]]) <{cell_clip = 0.000000e+00 : f32, fused_activation_function = "TANH", proj_clip = 0.000000e+00 : f32, time_major = true}> : (tensor<28x1x16xf32>, tensor<16x16xf32>, tensor<16x16xf32>, tensor<16x16xf32>, tensor<16x16xf32>, tensor<16x8xf32>, tensor<16x8xf32>, tensor<16x8xf32>, tensor<16x8xf32>, none, none, none, tensor<16xf32>, tensor<16xf32>, tensor<16xf32>, tensor<16xf32>, tensor<8x16xf32>, none, tensor<1x8xf32>, tensor<1x16xf32>, none, none, none, none) -> tensor<28x1x8xf32> +// CHECK: return [[VAL_15]] : tensor<28x1x8xf32> +// CHECK: } + +func.func @UnidirectionalRnn(%arg: tensor<28x1x28xf32>) -> (tensor<28x1x28xf32>) { + %1 = "tf.Const"() {device = "", dtype = f32, value = dense<0.000000e+00>: tensor<28x28xf32>} : () -> tensor<28x28xf32> + %2 = "tf.Const"() {device = "", dtype = f32, value = dense<0.000000e+00>: tensor<28xf32>} : () -> tensor<28xf32> + %3 = "tf.Const"() {device = "", dtype = f32, value = dense<0.000000e+00>: tensor<1x28xf32>} : () -> tensor<1x28xf32> + %4:2 = "tf.UnidirectionalSequenceRnn"(%arg, %1, %1, %2, %3) {_tflite_input_indices = [0, 1, 2, 3, 4], device = ""} : (tensor<28x1x28xf32>, tensor<28x28xf32>, tensor<28x28xf32>, tensor<28xf32>, tensor<1x28xf32>) -> (tensor<*xf32>, tensor<28x1x28xf32>) + func.return %4#1 : tensor<28x1x28xf32> +} + +// CHECK: func @UnidirectionalRnn([[VAL_0:%.*]]: tensor<28x1x28xf32>) -> tensor<28x1x28xf32> { +// CHECK-DAG: [[VAL_1:%.*]] = arith.constant dense<0.000000e+00> : tensor<28x28xf32> +// CHECK-DAG: [[VAL_2:%.*]] = arith.constant dense<0.000000e+00> : tensor<28xf32> +// CHECK-DAG: [[VAL_3:%.*]] = arith.constant dense<0.000000e+00> : tensor<1x28xf32> +// CHECK: [[VAL_4:%.*]] = "tfl.unidirectional_sequence_rnn"([[VAL_0]], [[VAL_1]], [[VAL_1]], [[VAL_2]], [[VAL_3]]) <{fused_activation_function = "TANH", time_major = true}> : (tensor<28x1x28xf32>, tensor<28x28xf32>, tensor<28x28xf32>, tensor<28xf32>, tensor<1x28xf32>) -> tensor<28x1x28xf32> +// CHECK: return [[VAL_4]] : tensor<28x1x28xf32> +// CHECK: } + +func.func @broadcast_to_f32(%arg0: tensor<3xf32>, %arg1: tensor<2xi32>) -> tensor<3x3xf32> { + %0 = "tf.BroadcastTo"(%arg0, %arg1) : (tensor<3xf32>, tensor<2xi32>) -> tensor<3x3xf32> + func.return %0: tensor<3x3xf32> + +// CHECK-LABEL: broadcast_to_f32 +// CHECK: [[BCT:%.*]] = "tfl.broadcast_to"(%arg0, %arg1) : (tensor<3xf32>, tensor<2xi32>) -> tensor<3x3xf32> +// CHECK: return [[BCT]] : tensor<3x3xf32> +} + +func.func @broadcast_to_i32(%input: tensor<3xi32>, %shape: tensor<2xi32>) -> tensor<3x3xi32> { + %0 = "tf.BroadcastTo"(%input, %shape) : (tensor<3xi32>, tensor<2xi32>) -> tensor<3x3xi32> + func.return %0: tensor<3x3xi32> + +// CHECK-LABEL: broadcast_to_i32 +// CHECK: [[BCT:%.*]] = "tfl.broadcast_to"(%arg0, %arg1) : (tensor<3xi32>, tensor<2xi32>) -> tensor<3x3xi32> +// CHECK: return [[BCT]] : tensor<3x3xi32> +} + +func.func @matmul_batch(%arg0: tensor<10x15xf32>, %arg1: tensor<15x17xf32>) -> tensor<10x17xf32> { + %0 = "tf.BatchMatMul"(%arg0, %arg1) {T = "tfdtype$DT_FLOAT", device = "/device:CPU:0", name = "MatMul", adj_x = false, adj_y = false} : +(tensor<10x15xf32>, tensor<15x17xf32>) -> tensor<10x17xf32> + func.return %0 : tensor<10x17xf32> +// CHECK-LABEL: matmul_batch +// CHECK: "tfl.batch_matmul"(%arg0, %arg1) <{adj_x = false, adj_y = false}> : (tensor<10x15xf32>, tensor<15x17xf32>) -> tensor<10x17xf32> +} + +func.func @matmul_batchv2(%arg0: tensor<2x10x15xf32>, %arg1: tensor<15x17xf32>) -> tensor<2x10x17xf32> { + %0 = "tf.BatchMatMulV2"(%arg0, %arg1) {T = "tfdtype$DT_FLOAT", device = "/device:CPU:0", name = "MatMul", adj_x = false, adj_y = false} : +(tensor<2x10x15xf32>, tensor<15x17xf32>) -> tensor<2x10x17xf32> + func.return %0 : tensor<2x10x17xf32> +// CHECK-LABEL: matmul_batchv2 +// CHECK: "tfl.batch_matmul"(%arg0, %arg1) <{adj_x = false, adj_y = false}> : (tensor<2x10x15xf32>, tensor<15x17xf32>) -> tensor<2x10x17xf32> +} + +func.func @matmul_batchv3(%arg0: tensor<2x10x15xf32>, %arg1: tensor<15x17xf32>) -> tensor<2x10x17xf32> { + %0 = "tf.BatchMatMulV3"(%arg0, %arg1) {Ta = "tfdtype$DT_FLOAT", Tb = "tfdtype$DT_FLOAT",device = "/device:CPU:0", name = "MatMul", adj_x = false, adj_y = false} : +(tensor<2x10x15xf32>, tensor<15x17xf32>) -> tensor<2x10x17xf32> + func.return %0 : tensor<2x10x17xf32> +// CHECK-LABEL: matmul_batchv3 +// CHECK: "tfl.batch_matmul"(%arg0, %arg1) <{adj_x = false, adj_y = false}> : (tensor<2x10x15xf32>, tensor<15x17xf32>) -> tensor<2x10x17xf32> +} + +func.func @matmul_batchv3_int8(%arg0: tensor<2x10x15xi8>, %arg1: tensor<15x17xi8>) -> tensor<2x10x17xi32> { + %0 = "tf.BatchMatMulV3"(%arg0, %arg1) {Ta = "tfdtype$DT_INT8", Tb = "tfdtype$DT_INT8", Tout = "tfdtype$DT_INT32", device = "/device:CPU:0", name = "MatMul", adj_x = false, adj_y = false} : +(tensor<2x10x15xi8>, tensor<15x17xi8>) -> tensor<2x10x17xi32> + func.return %0 : tensor<2x10x17xi32> +// CHECK-LABEL: matmul_batchv3_int8 +// CHECK: "tfl.batch_matmul"(%arg0, %arg1) <{adj_x = false, adj_y = false}> : (tensor<2x10x15xi8>, tensor<15x17xi8>) -> tensor<2x10x17xi32> +} + +func.func @matmul_batchv2_unknown_dim(%arg0: tensor, %arg1: tensor<15x17xf32>) -> tensor { + %0 = "tf.BatchMatMulV2"(%arg0, %arg1) {T = "tfdtype$DT_FLOAT", device = "/device:CPU:0", name = "MatMul", adj_x = false, adj_y = false} : +(tensor, tensor<15x17xf32>) -> tensor + func.return %0 : tensor +// CHECK-LABEL: matmul_batchv2_unknown_dim +// CHECK: "tfl.batch_matmul"(%arg0, %arg1) <{adj_x = false, adj_y = false}> : (tensor, tensor<15x17xf32>) -> tensor +} + +func.func @matmul_batchv3_unknown_dim(%arg0: tensor, %arg1: tensor<15x17xf32>) -> tensor { + %0 = "tf.BatchMatMulV3"(%arg0, %arg1) {Ta = "tfdtype$DT_FLOAT", Tb = "tfdtype$DT_FLOAT", device = "/device:CPU:0", name = "MatMul", adj_x = false, adj_y = false} : +(tensor, tensor<15x17xf32>) -> tensor + func.return %0 : tensor +// CHECK-LABEL: matmul_batchv3_unknown_dim +// CHECK: "tfl.batch_matmul"(%arg0, %arg1) <{adj_x = false, adj_y = false}> : (tensor, tensor<15x17xf32>) -> tensor +} + +func.func @matmul_batchv3_unknown_dim_bf16(%arg0: tensor, %arg1: tensor<5x6xf32>) -> tensor { + %0 = "tf.Cast"(%arg0) : (tensor) -> tensor + %1 = "tf.BatchMatMulV3"(%0, %arg1) {Ta = "tfdtype$DT_FLOAT", Tb = "tfdtype$DT_FLOAT", device = "/device:CPU:0", name = "MatMul", adj_x = false, adj_y = false} : +(tensor, tensor<5x6xf32>) -> tensor + %2 = "tf.Cast"(%1) : (tensor) -> tensor + func.return %2 : tensor +// CHECK-LABEL: matmul_batchv3_unknown_dim_bf16 +// CHECK: [[CST:%.*]] = "tfl.cast"(%arg0) : (tensor) -> tensor +// CHECK: [[BMM:%.*]] = "tfl.batch_matmul"([[CST]], %arg1) <{adj_x = false, adj_y = false}> : (tensor, tensor<5x6xf32>) -> tensor +// CHECK: "tfl.cast"([[BMM]]) : (tensor) -> tensor +} + +// ----- + +func.func @select_v2_with_6d_broadcasting(%arg0: tensor<1x1x1x1x3x1xi1>, %arg1 : tensor<1x1x1x1x1x4xf32>, %arg2 : tensor<1x1x1x2x1x1xf32>) -> tensor<1x1x1x2x3x4xf32> { + %0 = "tf.SelectV2"(%arg0, %arg1, %arg2): (tensor<1x1x1x1x3x1xi1>, tensor<1x1x1x1x1x4xf32>, tensor<1x1x1x2x1x1xf32>) -> tensor<1x1x1x2x3x4xf32> + func.return %0 : tensor<1x1x1x2x3x4xf32> +// CHECK-LABEL: select_v2_with_6d_broadcasting +// CHECK: [[CST:%.*]] = arith.constant dense<[1, 1, 1, 2, 3, 4]> : tensor<6xi64> +// CHECK: [[BCT:%.*]] = "tfl.broadcast_to"(%arg0, [[CST]]) +// CHECK: [[BCT_0:%.*]] = "tfl.broadcast_to"(%arg1, [[CST]]) +// CHECK: [[BCT_1:%.*]] = "tfl.broadcast_to"(%arg2, [[CST]]) +// CHECK: "tfl.select"([[BCT]], [[BCT_0]], [[BCT_1]]) +} + +// ----- + +func.func @maximum_with_6d_broadcasting(%arg0: tensor<1x1x1x1x8x16xf32>, %arg1: tensor<8x16xf32>) -> tensor<1x1x1x1x8x16xf32> { + %0 = "tf.Maximum"(%arg0, %arg1) : (tensor<1x1x1x1x8x16xf32>, tensor<8x16xf32>) -> tensor<1x1x1x1x8x16xf32> + func.return %0 : tensor<1x1x1x1x8x16xf32> + +// CHECK-LABEL: maximum_with_6d_broadcasting +// CHECK: [[CST:%.*]] = arith.constant dense<[1, 1, 1, 1, 8, 16]> : tensor<6xi64> +// CHECK: [[BCT:%.*]] = "tfl.broadcast_to"(%arg1, [[CST]]) +// CHECK: "tfl.maximum"(%arg0, [[BCT]]) +} + +// ----- + +func.func @test5DAddWithImplicitBroadcast(%arg0: tensor<1x1x1x3x1xi32>, %arg1 : tensor<1x1x1x1x4xi32>) -> tensor<1x1x1x3x4xi32> { + %0 = "tf.Add"(%arg0, %arg1): (tensor<1x1x1x3x1xi32>, tensor<1x1x1x1x4xi32>) -> tensor<1x1x1x3x4xi32> + func.return %0 : tensor<1x1x1x3x4xi32> +// CHECK-LABEL: test5DAddWithImplicitBroadcast +// CHECK: %0 = tfl.add(%arg0, %arg1) <{fused_activation_function = "NONE"}> : (tensor<1x1x1x3x1xi32>, tensor<1x1x1x1x4xi32>) -> tensor<1x1x1x3x4xi32> +} + +func.func @test6DAddWithImplicitBroadcast(%arg0: tensor<1x2x1x4x5x6xi32>, %arg1: tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi32> { +// CHECK-LABEL: test6DAddWithImplicitBroadcast +// CHECK: %0 = tfl.add(%arg0, %arg1) <{fused_activation_function = "NONE"}> : (tensor<1x2x1x4x5x6xi32>, tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi32> + %0 = "tf.Add"(%arg0, %arg1) : (tensor<1x2x1x4x5x6xi32>, tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi32> + func.return %0 : tensor<1x2x3x4x5x6xi32> +} + +func.func @add_with_int32_7d_inputs(%arg0: tensor<1x1x1x1x1x3x1xi32>, %arg1 : tensor<1x1x1x1x1x1x4xi32>) -> tensor<1x1x1x1x1x3x4xi32> { + %0 = "tf.Add"(%arg0, %arg1): (tensor<1x1x1x1x1x3x1xi32>, tensor<1x1x1x1x1x1x4xi32>) -> tensor<1x1x1x1x1x3x4xi32> + func.return %0 : tensor<1x1x1x1x1x3x4xi32> +// CHECK-LABEL: add_with_int32_7d_inputs +// CHECK: %0 = "tfl.broadcast_to"(%arg0, %cst) : (tensor<1x1x1x1x1x3x1xi32>, tensor<7xi64>) -> tensor<1x1x1x1x1x3x4xi32> +// CHECK: %1 = "tfl.broadcast_to"(%arg1, %cst) : (tensor<1x1x1x1x1x1x4xi32>, tensor<7xi64>) -> tensor<1x1x1x1x1x3x4xi32> +// CHECK: %2 = tfl.add %0, %1 {fused_activation_function = "NONE"} : tensor<1x1x1x1x1x3x4xi32> +} + +func.func @test5DSubWithImplicitBroadcast(%arg0: tensor<1x1x1x3x1xi32>, %arg1 : tensor<1x1x1x1x4xi32>) -> tensor<1x1x1x3x4xi32> { + %0 = "tf.Sub"(%arg0, %arg1): (tensor<1x1x1x3x1xi32>, tensor<1x1x1x1x4xi32>) -> tensor<1x1x1x3x4xi32> + func.return %0 : tensor<1x1x1x3x4xi32> +// CHECK-LABEL: test5DSubWithImplicitBroadcast +// CHECK: %0 = tfl.sub(%arg0, %arg1) <{fused_activation_function = "NONE"}> : (tensor<1x1x1x3x1xi32>, tensor<1x1x1x1x4xi32>) -> tensor<1x1x1x3x4xi32> +} + +func.func @test6DSubWithImplicitBroadcast(%arg0: tensor<1x2x1x4x5x6xi32>, %arg1: tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi32> { +// CHECK-LABEL: test6DSubWithImplicitBroadcast +// CHECK: %0 = tfl.sub(%arg0, %arg1) <{fused_activation_function = "NONE"}> : (tensor<1x2x1x4x5x6xi32>, tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi32> + %0 = "tf.Sub"(%arg0, %arg1) : (tensor<1x2x1x4x5x6xi32>, tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi32> + func.return %0 : tensor<1x2x3x4x5x6xi32> +} + +func.func @sub_with_int32_7d_inputs(%arg0: tensor<1x1x1x1x1x3x1xi32>, %arg1 : tensor<1x1x1x1x1x1x4xi32>) -> tensor<1x1x1x1x1x3x4xi32> { + %0 = "tf.Sub"(%arg0, %arg1): (tensor<1x1x1x1x1x3x1xi32>, tensor<1x1x1x1x1x1x4xi32>) -> tensor<1x1x1x1x1x3x4xi32> + func.return %0 : tensor<1x1x1x1x1x3x4xi32> +// CHECK-LABEL: sub_with_int32_7d_inputs +// CHECK: %0 = "tfl.broadcast_to"(%arg0, %cst) : (tensor<1x1x1x1x1x3x1xi32>, tensor<7xi64>) -> tensor<1x1x1x1x1x3x4xi32> +// CHECK: %1 = "tfl.broadcast_to"(%arg1, %cst) : (tensor<1x1x1x1x1x1x4xi32>, tensor<7xi64>) -> tensor<1x1x1x1x1x3x4xi32> +// CHECK: %2 = tfl.sub %0, %1 {fused_activation_function = "NONE"} : tensor<1x1x1x1x1x3x4xi32> +} + +func.func @test5DMulWithImplicitBroadcast(%arg0: tensor<1x1x1x3x1xi32>, %arg1 : tensor<1x1x1x1x4xi32>) -> tensor<1x1x1x3x4xi32> { + %0 = "tf.Mul"(%arg0, %arg1): (tensor<1x1x1x3x1xi32>, tensor<1x1x1x1x4xi32>) -> tensor<1x1x1x3x4xi32> + func.return %0 : tensor<1x1x1x3x4xi32> +// CHECK-LABEL: test5DMulWithImplicitBroadcast +// CHECK: %0 = tfl.mul(%arg0, %arg1) <{fused_activation_function = "NONE"}> : (tensor<1x1x1x3x1xi32>, tensor<1x1x1x1x4xi32>) -> tensor<1x1x1x3x4xi32> +} + +func.func @test6DMulWithImplicitBroadcast(%arg0: tensor<1x2x1x4x5x6xi32>, %arg1: tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi32> { +// CHECK-LABEL: test6DMulWithImplicitBroadcast +// CHECK: %0 = tfl.mul(%arg0, %arg1) <{fused_activation_function = "NONE"}> : (tensor<1x2x1x4x5x6xi32>, tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi32> + %0 = "tf.Mul"(%arg0, %arg1) : (tensor<1x2x1x4x5x6xi32>, tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi32> + func.return %0 : tensor<1x2x3x4x5x6xi32> +} + +func.func @mul_with_int32_7d_inputs(%arg0: tensor<1x1x1x1x1x3x1xi32>, %arg1 : tensor<1x1x1x1x1x1x4xi32>) -> tensor<1x1x1x1x1x3x4xi32> { + %0 = "tf.Mul"(%arg0, %arg1): (tensor<1x1x1x1x1x3x1xi32>, tensor<1x1x1x1x1x1x4xi32>) -> tensor<1x1x1x1x1x3x4xi32> + func.return %0 : tensor<1x1x1x1x1x3x4xi32> +// CHECK-LABEL: mul_with_int32_7d_inputs +// CHECK: %0 = "tfl.broadcast_to"(%arg0, %cst) : (tensor<1x1x1x1x1x3x1xi32>, tensor<7xi64>) -> tensor<1x1x1x1x1x3x4xi32> +// CHECK: %1 = "tfl.broadcast_to"(%arg1, %cst) : (tensor<1x1x1x1x1x1x4xi32>, tensor<7xi64>) -> tensor<1x1x1x1x1x3x4xi32> +// CHECK: %2 = tfl.mul %0, %1 {fused_activation_function = "NONE"} : tensor<1x1x1x1x1x3x4xi32> +} + +// CHECK-LABEL: testDivWithBroadcastToOps +func.func @testDivWithBroadcastToOps(%arg0: tensor<1x2x1x4x5x6xi32>, %arg1: tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi32> { + // CHECK: tfl.div(%arg0, %arg1) <{fused_activation_function = "NONE"}> : (tensor<1x2x1x4x5x6xi32>, tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi32> + %0 = "tf.Div"(%arg0, %arg1) : (tensor<1x2x1x4x5x6xi32>, tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi32> + func.return %0 : tensor<1x2x3x4x5x6xi32> +} + +// CHECK-LABEL: testDivWithBroadcastToOps7D +func.func @testDivWithBroadcastToOps7D(%arg0: tensor<1x2x1x4x5x6x7xi32>, %arg1: tensor<1x2x3x4x5x1x7xi32>) -> tensor<1x2x3x4x5x6x7xi32> { + // CHECK: [[CST:%.*]] = arith.constant dense<[1, 2, 3, 4, 5, 6, 7]> : tensor<7xi64> + // CHECK: [[BCAST:%.*]] = "tfl.broadcast_to"(%arg0, [[CST]]) + // CHECK: [[BCAST_1:%.*]] = "tfl.broadcast_to"(%arg1, [[CST]]) + // CHECK: tfl.div [[BCAST]], [[BCAST_1]] {fused_activation_function = "NONE"} : tensor<1x2x3x4x5x6x7xi32> + %0 = "tf.Div"(%arg0, %arg1) : (tensor<1x2x1x4x5x6x7xi32>, tensor<1x2x3x4x5x1x7xi32>) -> tensor<1x2x3x4x5x6x7xi32> + func.return %0 : tensor<1x2x3x4x5x6x7xi32> +} + +// CHECK-LABEL: testFloorDivWithBroadcastToOps +func.func @testFloorDivWithBroadcastToOps(%arg0: tensor<1x2x1x4x5x6xi32>, %arg1: tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi32> { + // CHECK: [[CST:%.*]] = arith.constant dense<[1, 2, 3, 4, 5, 6]> : tensor<6xi64> + // CHECK: [[BCAST:%.*]] = "tfl.broadcast_to"(%arg0, [[CST]]) + // CHECK: [[BCAST_1:%.*]] = "tfl.broadcast_to"(%arg1, [[CST]]) + // CHECK: tfl.floor_div [[BCAST]], [[BCAST_1]] : tensor<1x2x3x4x5x6xi32> + %0 = "tf.FloorDiv"(%arg0, %arg1) : (tensor<1x2x1x4x5x6xi32>, tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi32> + func.return %0 : tensor<1x2x3x4x5x6xi32> +} + +// CHECK-LABEL: testFloorModWithBroadcastToOps +func.func @testFloorModWithBroadcastToOps(%arg0: tensor<1x2x1x4x5x6xi32>, %arg1: tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi32> { + // CHECK: [[CST:%.*]] = arith.constant dense<[1, 2, 3, 4, 5, 6]> : tensor<6xi64> + // CHECK: [[BCAST:%.*]] = "tfl.broadcast_to"(%arg0, [[CST]]) + // CHECK: [[BCAST_1:%.*]] = "tfl.broadcast_to"(%arg1, [[CST]]) + // CHECK: "tfl.floor_mod"([[BCAST]], [[BCAST_1]]) : (tensor<1x2x3x4x5x6xi32>, tensor<1x2x3x4x5x6xi32>) -> tensor<1x2x3x4x5x6xi32> + %0 = "tf.FloorMod"(%arg0, %arg1) : (tensor<1x2x1x4x5x6xi32>, tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi32> + func.return %0 : tensor<1x2x3x4x5x6xi32> +} + +// CHECK-LABEL: testPowWithBroadcastToOps +func.func @testPowWithBroadcastToOps(%arg0: tensor<1x2x1x4x5x6xi32>, %arg1: tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi32> { + // CHECK: [[CST:%.*]] = arith.constant dense<[1, 2, 3, 4, 5, 6]> : tensor<6xi64> + // CHECK: [[BCAST:%.*]] = "tfl.broadcast_to"(%arg0, [[CST]]) + // CHECK: [[BCAST_1:%.*]] = "tfl.broadcast_to"(%arg1, [[CST]]) + // CHECK: tfl.pow [[BCAST]], [[BCAST_1]] : tensor<1x2x3x4x5x6xi32> + %0 = "tf.Pow"(%arg0, %arg1) : (tensor<1x2x1x4x5x6xi32>, tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi32> + func.return %0 : tensor<1x2x3x4x5x6xi32> +} + +// CHECK-LABEL: testMaximumWithBroadcastToOps +func.func @testMaximumWithBroadcastToOps(%arg0: tensor<1x2x1x4x5x6xi32>, %arg1: tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi32> { + // CHECK: [[CST:%.*]] = arith.constant dense<[1, 2, 3, 4, 5, 6]> : tensor<6xi64> + // CHECK: [[BCAST:%.*]] = "tfl.broadcast_to"(%arg0, [[CST]]) + // CHECK: [[BCAST_1:%.*]] = "tfl.broadcast_to"(%arg1, [[CST]]) + // CHECK: "tfl.maximum"([[BCAST]], [[BCAST_1]]) : (tensor<1x2x3x4x5x6xi32>, tensor<1x2x3x4x5x6xi32>) -> tensor<1x2x3x4x5x6xi32> + %0 = "tf.Maximum"(%arg0, %arg1) : (tensor<1x2x1x4x5x6xi32>, tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi32> + func.return %0 : tensor<1x2x3x4x5x6xi32> +} + +// CHECK-LABEL: testMinimumWithBroadcastToOps +func.func @testMinimumWithBroadcastToOps(%arg0: tensor<1x2x1x4x5x6xi32>, %arg1: tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi32> { + // CHECK: [[CST:%.*]] = arith.constant dense<[1, 2, 3, 4, 5, 6]> : tensor<6xi64> + // CHECK: [[BCAST:%.*]] = "tfl.broadcast_to"(%arg0, [[CST]]) + // CHECK: [[BCAST_1:%.*]] = "tfl.broadcast_to"(%arg1, [[CST]]) + // CHECK: "tfl.minimum"([[BCAST]], [[BCAST_1]]) : (tensor<1x2x3x4x5x6xi32>, tensor<1x2x3x4x5x6xi32>) -> tensor<1x2x3x4x5x6xi32> + %0 = "tf.Minimum"(%arg0, %arg1) : (tensor<1x2x1x4x5x6xi32>, tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi32> + func.return %0 : tensor<1x2x3x4x5x6xi32> +} + +// CHECK-LABEL: testSelectV2WithBroadcastToOps +func.func @testSelectV2WithBroadcastToOps(%arg0: tensor<1x2x1x4x1x6xi1>, %arg1: tensor<1x2x3x4x1x1xi32>, %arg2: tensor<1x2x1x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi32> { + // CHECK: [[CST:%.*]] = arith.constant dense<[1, 2, 3, 4, 5, 6]> : tensor<6xi64> + // CHECK: [[BCAST:%.*]] = "tfl.broadcast_to"(%arg0, [[CST]]) + // CHECK: [[BCAST_1:%.*]] = "tfl.broadcast_to"(%arg1, [[CST]]) + // CHECK: [[BCAST_2:%.*]] = "tfl.broadcast_to"(%arg2, [[CST]]) + // CHECK: "tfl.select"([[BCAST]], [[BCAST_1]], [[BCAST_2]]) + %0 = "tf.SelectV2"(%arg0, %arg1, %arg2) : (tensor<1x2x1x4x1x6xi1>, tensor<1x2x3x4x1x1xi32>, tensor<1x2x1x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi32> + func.return %0 : tensor<1x2x3x4x5x6xi32> +} + +// CHECK-LABEL: testLessEqualWithBroadcastToOps +func.func @testLessEqualWithBroadcastToOps(%arg0: tensor<1x2x1x4x5x6xi32>, %arg1: tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi1> { + // CHECK: [[CST:%.*]] = arith.constant dense<[1, 2, 3, 4, 5, 6]> : tensor<6xi64> + // CHECK: [[BCAST:%.*]] = "tfl.broadcast_to"(%arg0, [[CST]]) + // CHECK: [[BCAST_1:%.*]] = "tfl.broadcast_to"(%arg1, [[CST]]) + // CHECK: tfl.less_equal([[BCAST]], [[BCAST_1]]) : (tensor<1x2x3x4x5x6xi32>, tensor<1x2x3x4x5x6xi32>) -> tensor<1x2x3x4x5x6xi1> + %0 = "tf.LessEqual"(%arg0, %arg1) : (tensor<1x2x1x4x5x6xi32>, tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi1> + func.return %0 : tensor<1x2x3x4x5x6xi1> +} + +// CHECK-LABEL: testGreaterEqualWithBroadcastToOps +func.func @testGreaterEqualWithBroadcastToOps(%arg0: tensor<1x2x1x4x5x6xi32>, %arg1: tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi1> { + // CHECK: [[CST:%.*]] = arith.constant dense<[1, 2, 3, 4, 5, 6]> : tensor<6xi64> + // CHECK: [[BCAST:%.*]] = "tfl.broadcast_to"(%arg0, [[CST]]) + // CHECK: [[BCAST_1:%.*]] = "tfl.broadcast_to"(%arg1, [[CST]]) + // CHECK: tfl.greater_equal([[BCAST]], [[BCAST_1]]) : (tensor<1x2x3x4x5x6xi32>, tensor<1x2x3x4x5x6xi32>) -> tensor<1x2x3x4x5x6xi1> + %0 = "tf.GreaterEqual"(%arg0, %arg1) : (tensor<1x2x1x4x5x6xi32>, tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi1> + func.return %0 : tensor<1x2x3x4x5x6xi1> +} + +// CHECK-LABEL: testEqualWithBroadcastToOps +func.func @testEqualWithBroadcastToOps(%arg0: tensor<1x2x1x4x5x6xi32>, %arg1: tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi1> { + // CHECK: [[CST:%.*]] = arith.constant dense<[1, 2, 3, 4, 5, 6]> : tensor<6xi64> + // CHECK: [[BCAST:%.*]] = "tfl.broadcast_to"(%arg0, [[CST]]) + // CHECK: [[BCAST_1:%.*]] = "tfl.broadcast_to"(%arg1, [[CST]]) + // CHECK: "tfl.equal"([[BCAST]], [[BCAST_1]]) : (tensor<1x2x3x4x5x6xi32>, tensor<1x2x3x4x5x6xi32>) -> tensor<1x2x3x4x5x6xi1> + %0 = "tf.Equal"(%arg0, %arg1) : (tensor<1x2x1x4x5x6xi32>, tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi1> + func.return %0 : tensor<1x2x3x4x5x6xi1> +} + +// CHECK-LABEL: testNotEqualWithBroadcastToOps +func.func @testNotEqualWithBroadcastToOps(%arg0: tensor<1x2x1x4x5x6xi32>, %arg1: tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi1> { + // CHECK: [[CST:%.*]] = arith.constant dense<[1, 2, 3, 4, 5, 6]> : tensor<6xi64> + // CHECK: [[BCAST:%.*]] = "tfl.broadcast_to"(%arg0, [[CST]]) + // CHECK: [[BCAST_1:%.*]] = "tfl.broadcast_to"(%arg1, [[CST]]) + // CHECK: tfl.not_equal([[BCAST]], [[BCAST_1]]) : (tensor<1x2x3x4x5x6xi32>, tensor<1x2x3x4x5x6xi32>) -> tensor<1x2x3x4x5x6xi1> + %0 = "tf.NotEqual"(%arg0, %arg1) : (tensor<1x2x1x4x5x6xi32>, tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi1> + func.return %0 : tensor<1x2x3x4x5x6xi1> +} + +// CHECK-LABEL: testLessWithBroadcastToOps +func.func @testLessWithBroadcastToOps(%arg0: tensor<1x2x1x4x5x6xi32>, %arg1: tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi1> { + // CHECK: [[CST:%.*]] = arith.constant dense<[1, 2, 3, 4, 5, 6]> : tensor<6xi64> + // CHECK: [[BCAST:%.*]] = "tfl.broadcast_to"(%arg0, [[CST]]) + // CHECK: [[BCAST_1:%.*]] = "tfl.broadcast_to"(%arg1, [[CST]]) + // CHECK: tfl.less([[BCAST]], [[BCAST_1]]) : (tensor<1x2x3x4x5x6xi32>, tensor<1x2x3x4x5x6xi32>) -> tensor<1x2x3x4x5x6xi1> + %0 = "tf.Less"(%arg0, %arg1) : (tensor<1x2x1x4x5x6xi32>, tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi1> + func.return %0 : tensor<1x2x3x4x5x6xi1> +} + +// CHECK-LABEL: testGreaterWithBroadcastToOps +func.func @testGreaterWithBroadcastToOps(%arg0: tensor<1x2x1x4x5x6xi32>, %arg1: tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi1> { + // CHECK: [[CST:%.*]] = arith.constant dense<[1, 2, 3, 4, 5, 6]> : tensor<6xi64> + // CHECK: [[BCAST:%.*]] = "tfl.broadcast_to"(%arg0, [[CST]]) + // CHECK: [[BCAST_1:%.*]] = "tfl.broadcast_to"(%arg1, [[CST]]) + // CHECK: tfl.greater([[BCAST]], [[BCAST_1]]) : (tensor<1x2x3x4x5x6xi32>, tensor<1x2x3x4x5x6xi32>) -> tensor<1x2x3x4x5x6xi1> + %0 = "tf.Greater"(%arg0, %arg1) : (tensor<1x2x1x4x5x6xi32>, tensor<1x2x3x4x5x1xi32>) -> tensor<1x2x3x4x5x6xi1> + func.return %0 : tensor<1x2x3x4x5x6xi1> +} + +func.func @tranpose_int32_perm(%arg0: tensor<2x3xf32>) -> tensor<3x2xf32> { + %cst = "tf.Const"() { value = dense<[1, 0]> : tensor<2xi32> } : () -> tensor<2xi32> + %0 = "tf.Transpose"(%arg0, %cst): (tensor<2x3xf32>, tensor<2xi32>) -> tensor<3x2xf32> + func.return %0 : tensor<3x2xf32> + // CHECK-LABEL: tranpose_int32_perm + // CHECK: "tfl.transpose" +} + +func.func @tranpose_int64_perm(%arg0: tensor<2x3xf32>) -> tensor<3x2xf32> { + %cst = "tf.Const"() { value = dense<[1, 0]> : tensor<2xi64> } : () -> tensor<2xi64> + %0 = "tf.Transpose"(%arg0, %cst): (tensor<2x3xf32>, tensor<2xi64>) -> tensor<3x2xf32> + func.return %0 : tensor<3x2xf32> + // CHECK-LABEL: tranpose_int64_perm + // CHECK: "tfl.transpose" +} + +func.func @tranpose_arg32(%arg0: tensor<2x3xf32>, %arg1: tensor<2xi32>) -> tensor<3x2xf32> { + %0 = "tf.Transpose"(%arg0, %arg1): (tensor<2x3xf32>, tensor<2xi32>) -> tensor<3x2xf32> + func.return %0 : tensor<3x2xf32> + // CHECK-LABEL: tranpose_arg32 + // CHECK: "tfl.transpose" +} + +func.func @tranpose_arg64(%arg0: tensor<2x3xf32>, %arg1: tensor<2xi64>) -> tensor<3x2xf32> { + %0 = "tf.Transpose"(%arg0, %arg1): (tensor<2x3xf32>, tensor<2xi64>) -> tensor<3x2xf32> + func.return %0 : tensor<3x2xf32> + // CHECK-LABEL: tranpose_arg64 + // CHECK: "tfl.transpose" +} + +func.func @cumsum(%arg0: tensor<3x3xf32>, %arg1: tensor) -> tensor<3x3xf32> { + %0 = "tf.Cumsum"(%arg0, %arg1) {exclusive = false, reverse = false} : (tensor<3x3xf32>, tensor) -> tensor<3x3xf32> + func.return %0 : tensor<3x3xf32> + // CHECK-LABEL: cumsum + // CHECK: "tfl.cumsum"(%arg0, %arg1) <{exclusive = false, reverse = false}> : (tensor<3x3xf32>, tensor) -> tensor<3x3xf32> +} + +func.func @cumsum_i64(%arg0: tensor<3x3xf32>, %arg1: tensor) -> tensor<3x3xf32> { + %0 = "tf.Cumsum"(%arg0, %arg1) {exclusive = false, reverse = false} : (tensor<3x3xf32>, tensor) -> tensor<3x3xf32> + func.return %0 : tensor<3x3xf32> + // CHECK-LABEL: cumsum_i64 + // CHECK: "tfl.cast" + // CHECK: "tfl.cumsum" +} + +func.func @segmentsum(%arg0: tensor<3x3xf32>, %arg1: tensor) -> tensor<*xf32> { + %0 = "tf.SegmentSum"(%arg0, %arg1) : (tensor<3x3xf32>, tensor) -> tensor<*xf32> + func.return %0 : tensor<*xf32> + // CHECK-LABEL: segmentsum + // CHECK: "tfl.segment_sum"(%arg0, %arg1) : (tensor<3x3xf32>, tensor) -> tensor<*xf32> +} + +func.func @segmentsum_i64(%arg0: tensor<3x3xf32>, %arg1: tensor) -> tensor<*xf32> { + %0 = "tf.SegmentSum"(%arg0, %arg1) : (tensor<3x3xf32>, tensor) -> tensor<*xf32> + func.return %0 : tensor<*xf32> + // CHECK-LABEL: segmentsum_i64 + // CHECK: "tfl.cast" + // CHECK: "tfl.segment_sum" +} + +func.func @unsorted_segment_prod(%arg0: tensor<8xf32>, %arg1: tensor<8xi32>) -> tensor<8xf32> { + %num_segments = "tf.Const"() {value = dense<8> : tensor} : () -> tensor + %0 = "tf.UnsortedSegmentProd"(%arg0, %arg1, %num_segments) : (tensor<8xf32>, tensor<8xi32>, tensor) -> tensor<8xf32> + func.return %0 : tensor<8xf32> + // CHECK-LABEL: unsorted_segment_prod + // CHECK: [[BCT:%.*]] = "tfl.unsorted_segment_prod"(%arg0, %arg1, %cst) : (tensor<8xf32>, tensor<8xi32>, tensor) -> tensor<8xf32> + // CHECK: return [[BCT]] : tensor<8xf32> +} + + +// ----- + +func.func @unsorted_segment_max(%arg0: tensor<8xf32>, %arg1: tensor<8xi32>) -> tensor<8xf32> { + %num_segments = "tf.Const"() {value = dense<8> : tensor} : () -> tensor + %0 = "tf.UnsortedSegmentMax"(%arg0, %arg1, %num_segments) : (tensor<8xf32>, tensor<8xi32>, tensor) -> tensor<8xf32> + func.return %0 : tensor<8xf32> + // CHECK-LABEL: unsorted_segment_max + // CHECK: %[[CST:.*]] = arith.constant dense<8> : tensor + // CHECK: %[[BCT:.*]] = "tfl.unsorted_segment_max"(%arg0, %arg1, %[[CST]]) : (tensor<8xf32>, tensor<8xi32>, tensor) -> tensor<8xf32> + // CHECK: return %[[BCT]] : tensor<8xf32> +} + +// ----- + +func.func @unsorted_segment_max_3arg(%arg0: tensor<5xi32>, %arg1: tensor<5xi32>, %arg2: tensor) -> tensor<5xi32>{ + %0 = "tf.UnsortedSegmentMax"(%arg0, %arg1, %arg2) : (tensor<5xi32>, tensor<5xi32>, tensor) -> tensor<5xi32> + func.return %0 : tensor<5xi32> + // CHECK-LABEL: unsorted_segment_max_3arg + // CHECK: %[[BCT:.*]] = "tfl.cast"(%arg2) : (tensor) -> tensor + // CHECK: %[[RES:.*]] = "tfl.unsorted_segment_max"(%arg0, %arg1, %[[BCT]]) : (tensor<5xi32>, tensor<5xi32>, tensor) -> tensor<5xi32> + // CHECK: return %[[RES]] : tensor<5xi32> +} + +// ----- + +func.func @unsorted_segment_max_i64(%arg0: tensor<9xf32>, %arg1: tensor<9xi64>) -> tensor<9xf32> { + %num_segments = "tf.Const"() {value = dense<9> : tensor} : () -> tensor + %0 = "tf.UnsortedSegmentMax"(%arg0, %arg1, %num_segments) : (tensor<9xf32>, tensor<9xi64>, tensor) -> tensor<9xf32> + func.return %0 : tensor<9xf32> + // CHECK-LABEL: unsorted_segment_max_i64 + // CHECK: %[[CST:.*]] = arith.constant dense<9> : tensor + // CHECK: %[[CAST:.*]] = "tfl.cast"(%arg1) : (tensor<9xi64>) -> tensor<9xi32> + // CHECK: %[[RES:.*]] = "tfl.unsorted_segment_max"(%arg0, %[[CAST]], %[[CST]]) : (tensor<9xf32>, tensor<9xi32>, tensor) -> tensor<9xf32> + // CHECK: return %[[RES]] : tensor<9xf32> +} + +// ----- + +func.func @unsorted_segment_min(%arg0: tensor<8xf32>, %arg1: tensor<8xi32>) -> tensor<8xf32> { + %num_segments = "tf.Const"() {value = dense<8> : tensor} : () -> tensor + %0 = "tf.UnsortedSegmentMin"(%arg0, %arg1, %num_segments) : (tensor<8xf32>, tensor<8xi32>, tensor) -> tensor<8xf32> + func.return %0 : tensor<8xf32> + // CHECK-LABEL: unsorted_segment_min + // CHECK: %[[CST:.*]] = arith.constant dense<8> : tensor + // CHECK: %[[BCT:.*]] = "tfl.unsorted_segment_min"(%arg0, %arg1, %[[CST]]) : (tensor<8xf32>, tensor<8xi32>, tensor) -> tensor<8xf32> + // CHECK: return %[[BCT]] : tensor<8xf32> +} + +// ----- + +func.func @unsorted_segment_min_3arg(%arg0: tensor<5xi32>, %arg1: tensor<5xi32>, %arg2: tensor) -> tensor<5xi32>{ + %0 = "tf.UnsortedSegmentMin"(%arg0, %arg1, %arg2) : (tensor<5xi32>, tensor<5xi32>, tensor) -> tensor<5xi32> + func.return %0 : tensor<5xi32> + // CHECK-LABEL: unsorted_segment_min_3arg + // CHECK: %[[BCT:.*]] = "tfl.cast"(%arg2) : (tensor) -> tensor + // CHECK: %[[RES:.*]] = "tfl.unsorted_segment_min"(%arg0, %arg1, %[[BCT]]) : (tensor<5xi32>, tensor<5xi32>, tensor) -> tensor<5xi32> + // CHECK: return %[[RES]] : tensor<5xi32> +} + +// ----- + +func.func @unsorted_segment_min_i64(%arg0: tensor<9xf32>, %arg1: tensor<9xi64>) -> tensor<9xf32> { + %num_segments = "tf.Const"() {value = dense<9> : tensor} : () -> tensor + %0 = "tf.UnsortedSegmentMin"(%arg0, %arg1, %num_segments) : (tensor<9xf32>, tensor<9xi64>, tensor) -> tensor<9xf32> + func.return %0 : tensor<9xf32> + // CHECK-LABEL: unsorted_segment_min_i64 + // CHECK: %[[CST:.*]] = arith.constant dense<9> : tensor + // CHECK: %[[CAST:.*]] = "tfl.cast"(%arg1) : (tensor<9xi64>) -> tensor<9xi32> + // CHECK: %[[RES:.*]] = "tfl.unsorted_segment_min"(%arg0, %[[CAST]], %[[CST]]) : (tensor<9xf32>, tensor<9xi32>, tensor) -> tensor<9xf32> + // CHECK: return %[[RES]] : tensor<9xf32> +} + +// ----- + +func.func @unsorted_segment_sum(%arg0: tensor<8xf32>, %arg1: tensor<8xi32>) -> tensor<8xf32> { + %num_segments = "tf.Const"() {value = dense<8> : tensor} : () -> tensor + %0 = "tf.UnsortedSegmentSum"(%arg0, %arg1, %num_segments) : (tensor<8xf32>, tensor<8xi32>, tensor) -> tensor<8xf32> + func.return %0 : tensor<8xf32> + // CHECK-LABEL: unsorted_segment_sum + // CHECK: %[[CST:.*]] = arith.constant dense<8> : tensor + // CHECK: %[[BCT:.*]] = "tfl.unsorted_segment_sum"(%arg0, %arg1, %[[CST]]) : (tensor<8xf32>, tensor<8xi32>, tensor) -> tensor<8xf32> + // CHECK: return %[[BCT]] : tensor<8xf32> +} + +// ----- + +func.func @unsorted_segment_sum_3arg(%arg0: tensor<5xi32>, %arg1: tensor<5xi32>, %arg2: tensor) -> tensor<5xi32>{ + %0 = "tf.UnsortedSegmentSum"(%arg0, %arg1, %arg2) : (tensor<5xi32>, tensor<5xi32>, tensor) -> tensor<5xi32> + func.return %0 : tensor<5xi32> + // CHECK-LABEL: unsorted_segment_sum_3arg + // CHECK: %[[BCT:.*]] = "tfl.cast"(%arg2) : (tensor) -> tensor + // CHECK: %[[RES:.*]] = "tfl.unsorted_segment_sum"(%arg0, %arg1, %[[BCT]]) : (tensor<5xi32>, tensor<5xi32>, tensor) -> tensor<5xi32> + // CHECK: return %[[RES]] : tensor<5xi32> +} + +// ----- + +func.func @unsorted_segment_sum_i64(%arg0: tensor<9xf32>, %arg1: tensor<9xi64>) -> tensor<9xf32> { + %num_segments = "tf.Const"() {value = dense<9> : tensor} : () -> tensor + %0 = "tf.UnsortedSegmentSum"(%arg0, %arg1, %num_segments) : (tensor<9xf32>, tensor<9xi64>, tensor) -> tensor<9xf32> + func.return %0 : tensor<9xf32> + // CHECK-LABEL: unsorted_segment_sum_i64 + // CHECK: %[[CST:.*]] = arith.constant dense<9> : tensor + // CHECK: %[[CAST:.*]] = "tfl.cast"(%arg1) : (tensor<9xi64>) -> tensor<9xi32> + // CHECK: %[[RES:.*]] = "tfl.unsorted_segment_sum"(%arg0, %[[CAST]], %[[CST]]) : (tensor<9xf32>, tensor<9xi32>, tensor) -> tensor<9xf32> + // CHECK: return %[[RES]] : tensor<9xf32> +} + +// ----- + +func.func @rfft2d(%arg0: tensor<10x20x10x30xf32>, %arg1: tensor<2xi32>) -> tensor<10x20x10x30xcomplex> { + %0 = "tf.RFFT2D"(%arg0, %arg1) : (tensor<10x20x10x30xf32>, tensor<2xi32>) -> tensor<10x20x10x30xcomplex> + func.return %0 : tensor<10x20x10x30xcomplex> + // CHECK-LABEL: rfft2d + // CHECK: "tfl.rfft2d"(%arg0, %arg1) : (tensor<10x20x10x30xf32>, tensor<2xi32>) -> tensor<10x20x10x30xcomplex> +} + +func.func @rfft2d_invalid(%arg0: tensor<10x20x10x30xf64>, %arg1: tensor<2xi32>) -> tensor<10x20x10x30xcomplex> { + %0 = "tf.RFFT2D"(%arg0, %arg1) : (tensor<10x20x10x30xf64>, tensor<2xi32>) -> tensor<10x20x10x30xcomplex> + func.return %0 : tensor<10x20x10x30xcomplex> + // CHECK-LABEL: rfft2d_invalid + // CHECK-NOT: "tfl.rfft2d" +} + + +func.func @conv3d_valid(%arg0: tensor,%arg1: tensor) -> tensor { + %0 = "tf.Conv3D"(%arg0, %arg1) {padding = "SAME", strides = [1, 1, 1, 1, 1]} : (tensor, tensor) -> tensor + func.return %0: tensor + + // CHECK-LABEL: conv3d_valid + // CHECK: %[[CST:.*]] = "tfl.no_value"() <{value}> : () -> none + // CHECK: [[BCT:%.*]] = "tfl.conv_3d"(%arg0, %arg1, %[[CST]]) <{dilation_d_factor = 1 : i32, dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "SAME", stride_d = 1 : i32, stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor, tensor, none) -> tensor + // CHECK: return [[BCT]] : tensor +} + +func.func @conv3d_invalid_strides(%arg0: tensor,%arg1: tensor) -> tensor { + %0 = "tf.Conv3D"(%arg0, %arg1) {padding = "SAME", strides = [2, 1, 1, 1, 1]} : (tensor, tensor) -> tensor + func.return %0: tensor + // CHECK-LABEL: conv3d_invalid_strides + // CHECK: [[BCT:%.*]] = "tf.Conv3D"(%arg0, %arg1) <{padding = "SAME", strides = [2, 1, 1, 1, 1]}> : (tensor, tensor) -> tensor + // CHECK: return [[BCT]] : tensor +} + +func.func @complex_abs(%arg0: tensor<1 x complex>) -> tensor<1xf32> { + %0 = "tf.ComplexAbs"(%arg0) : (tensor<1 x complex>) -> tensor<1xf32> + func.return %0: tensor<1xf32> + +// CHECK-LABEL: complex_abs +// CHECK: "tfl.complex_abs"(%arg0) : (tensor<1xcomplex>) -> tensor<1xf32> +// CHECK: return +} + +func.func @real(%arg0: tensor<1 x complex>) -> tensor<1xf64> { + %0 = "tf.Real"(%arg0) : (tensor<1 x complex>) -> tensor<1xf64> + func.return %0: tensor<1xf64> + +// CHECK-LABEL: real +// CHECK: "tfl.real"(%arg0) : (tensor<1xcomplex>) -> tensor<1xf64> +// CHECK: return +} + +func.func @imag(%arg0: tensor<1 x complex>) -> tensor<1xf64> { + %0 = "tf.Imag"(%arg0) : (tensor<1 x complex>) -> tensor<1xf64> + func.return %0: tensor<1xf64> + +// CHECK-LABEL: imag +// CHECK: "tfl.imag"(%arg0) : (tensor<1xcomplex>) -> tensor<1xf64> +// CHECK: return +} + +func.func @all(%arg0: tensor<2x2xi1>, %arg1: tensor) -> tensor { + %0 = "tf.All"(%arg0, %arg1) {keep_dims = false} : (tensor<2x2xi1>, tensor) -> tensor + func.return %0 : tensor + +// CHECK-LABEL:all +// CHECK: "tfl.reduce_all"(%arg0, %arg1) <{keep_dims = false}> : (tensor<2x2xi1>, tensor) -> tensor +} + +func.func @all_i64axes(%arg0: tensor<8x16x16xi1>, %arg1: tensor<2xi64>) -> tensor { + %0 = "tf.All"(%arg0, %arg1) {keep_dims = false} : (tensor<8x16x16xi1>, tensor<2xi64>) -> tensor + func.return %0 : tensor + + // CHECK-LABEL: all_i64axes + // CHECK: %[[V0:.*]] = "tfl.cast"(%arg1) : (tensor<2xi64>) -> tensor<2xi32> + // CHECK: "tfl.reduce_all"(%arg0, %[[V0]]) <{keep_dims = false}> : (tensor<8x16x16xi1>, tensor<2xi32>) -> tensor +} + +func.func @quantize_dequantize_v4(%arg0 : tensor) -> tensor { + %cst = arith.constant dense<0.0> : tensor + %cst_0 = arith.constant dense<255.0> : tensor + %0 = "tf.QuantizeAndDequantizeV4"(%arg0, %cst, %cst_0) : (tensor, tensor, tensor) -> tensor + func.return %0 : tensor + +// CHECK-LABEL: quantize_dequantize_v4 +// CHECK: %[[QUANT:.*]] = "tfl.quantize"(%arg0) <{qtype = tensor>}> : (tensor) -> tensor> +// CHECK: %[[DEQUANT:.*]] = "tfl.dequantize"(%[[QUANT]]) : (tensor>) -> tensor +// CHECK: return %[[DEQUANT]] +} + +func.func @conv3d_transpose(%arg0: tensor<2x5x6x8x2xf32>, %arg1: tensor<1x2x2x3x2xf32>, %arg2: tensor<5xi64>) -> tensor { + %0 = "tf.Conv3DBackpropInputV2"(%arg2, %arg1, %arg0) {data_format = "NDHWC", dilations = [1, 1, 1, 1, 1], padding = "VALID", strides = [1, 2, 2, 2, 1]} : (tensor<5xi64>, tensor<1x2x2x3x2xf32>, tensor<2x5x6x8x2xf32>) -> tensor + func.return %0 : tensor + // CHECK-LABEL: conv3d_transpose + // CHECK: %[[CST:.*]] = "tfl.no_value"() <{value}> : () -> none + // CHECK: %[[OUT_SHAPE:.*]] = "tfl.cast"(%arg2) : (tensor<5xi64>) -> tensor<5xi32> + // CHECK: %[[RESULT:.*]] = "tfl.conv_3d_transpose"(%[[OUT_SHAPE]], %arg1, %arg0, %[[CST]]) <{dilation_d_factor = 1 : i32, dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_d = 2 : i32, stride_h = 2 : i32, stride_w = 2 : i32}> : (tensor<5xi32>, tensor<1x2x2x3x2xf32>, tensor<2x5x6x8x2xf32>, none) -> tensor + // CHECK: return %[[RESULT]] : tensor +} + +func.func @conv3d_transpose_unmatched_channels(%arg0: tensor<2x5x6x8x3xf32>, %arg1: tensor<1x2x2x3x2xf32>, %arg2: tensor<5xi64>) -> tensor { + %0 = "tf.Conv3DBackpropInputV2"(%arg2, %arg1, %arg0) {data_format = "NDHWC", dilations = [1, 1, 1, 1, 1], padding = "VALID", strides = [1, 2, 2, 2, 1]} : (tensor<5xi64>, tensor<1x2x2x3x2xf32>, tensor<2x5x6x8x3xf32>) -> tensor + func.return %0 : tensor + // CHECK-LABEL: conv3d_transpose_unmatched_channels + // CHECK: "tf.Conv3DBackpropInputV2" +} + +func.func @conv3d_transpose_unsupported_strides(%arg0: tensor<2x5x6x8x2xf32>, %arg1: tensor<1x2x2x3x2xf32>, %arg2: tensor<5xi64>) -> tensor { + %0 = "tf.Conv3DBackpropInputV2"(%arg2, %arg1, %arg0) {data_format = "NDHWC", dilations = [1, 1, 1, 1, 1], padding = "VALID", strides = [2, 2, 2, 2, 1]} : (tensor<5xi64>, tensor<1x2x2x3x2xf32>, tensor<2x5x6x8x2xf32>) -> tensor + func.return %0 : tensor + // CHECK-LABEL: conv3d_transpose_unsupported_strides + // CHECK: "tf.Conv3DBackpropInputV2" +} + +func.func @mul_i64(%arg0: tensor<14xi64>, %arg1: tensor<14xi64>) -> tensor<14xi64> { + %0 = "tf.Mul"(%arg0, %arg1) : (tensor<14xi64>, tensor<14xi64>) -> tensor<14xi64> + func.return %0: tensor<14xi64> + +// CHECK-LABEL: mul_i64 +// CHECK: tfl.mul %arg0, %arg1 {fused_activation_function = "NONE"} : tensor<14xi64> +// CHECK: return +} + +func.func @mul_i16(%arg0: tensor<14xi16>, %arg1: tensor<14xi16>) -> tensor<14xi16> { + %0 = "tf.Mul"(%arg0, %arg1) : (tensor<14xi16>, tensor<14xi16>) -> tensor<14xi16> + func.return %0: tensor<14xi16> + +// CHECK-LABEL: mul_i16 +// CHECK: tfl.mul %arg0, %arg1 {fused_activation_function = "NONE"} : tensor<14xi16> +// CHECK: return +} + +func.func @mul_ui32(%arg0: tensor<14xui32>, %arg1: tensor<14xui32>) -> tensor<14xui32> { + %0 = "tf.Mul"(%arg0, %arg1) : (tensor<14xui32>, tensor<14xui32>) -> tensor<14xui32> + func.return %0: tensor<14xui32> + +// CHECK-LABEL: mul_ui32 +// CHECK: tfl.mul %arg0, %arg1 {fused_activation_function = "NONE"} : tensor<14xui32> +// CHECK: return +} + +func.func @mul_complex32(%arg0: tensor<14xcomplex>, %arg1: tensor<14xcomplex>) -> tensor<14xcomplex> { + %0 = "tf.Mul"(%arg0, %arg1) : (tensor<14xcomplex>, tensor<14xcomplex>) -> tensor<14xcomplex> + func.return %0: tensor<14xcomplex> + +// CHECK-LABEL: mul_complex32 +// CHECK: tfl.mul %arg0, %arg1 {fused_activation_function = "NONE"} : tensor<14xcomplex> +// CHECK: return +} + +func.func @broadcast_args(%arg0: tensor<3xi32>, %arg1: tensor<1xi32>) -> tensor<3xi32> { + %0 = "tf.BroadcastArgs"(%arg0, %arg1) : (tensor<3xi32>, tensor<1xi32>) -> tensor<3xi32> + func.return %0 : tensor<3xi32> + +// CHECK-LABEL:broadcast_args +// CHECK: "tfl.broadcast_args"(%arg0, %arg1) : (tensor<3xi32>, tensor<1xi32>) -> tensor<3xi32> +} + +func.func @broadcast_args_i64(%arg0: tensor<3xi64>, %arg1: tensor<1xi64>) -> tensor<3xi64> { + %0 = "tf.BroadcastArgs"(%arg0, %arg1) : (tensor<3xi64>, tensor<1xi64>) -> tensor<3xi64> + func.return %0 : tensor<3xi64> + +// CHECK-LABEL:broadcast_args_i64 +// CHECK: "tfl.broadcast_args"(%arg0, %arg1) : (tensor<3xi64>, tensor<1xi64>) -> tensor<3xi64> +} + +func.func @mul_with_unranked_lhs(%arg0: tensor<*xf32>, %arg1: tensor) -> tensor { + %0 = "tf.Mul"(%arg0, %arg1): (tensor<*xf32>, tensor) -> tensor + func.return %0 : tensor + + // CHECK-LABEL:mul_with_unranked_lhs + // CHECK: %0 = tfl.mul(%arg0, %arg1) <{fused_activation_function = "NONE"}> : (tensor<*xf32>, tensor) -> tensor + // CHECK: return %0 : tensor +} + +func.func @mul_with_high_dims_dynamic_shape(%arg0: tensor<8x7x6x5x4x3x2x1xf32>, %arg1: tensor) -> tensor<8x7x6x5x4x3x2x1xf32> { + %0 = "tf.Mul"(%arg0, %arg1): (tensor<8x7x6x5x4x3x2x1xf32>, tensor) -> tensor<8x7x6x5x4x3x2x1xf32> + func.return %0 : tensor<8x7x6x5x4x3x2x1xf32> + + // CHECK-LABEL:mul_with_high_dims_dynamic_shape + // CHECK: %[[CST:.*]] = arith.constant dense<[8, 7, 6, 5, 4, 3, 2, 1]> : tensor<8xi64> + // CHECK: %[[SHAPE:.*]] = "tfl.shape"(%arg1) : (tensor) -> tensor<4xi64> + // CHECK: %[[BROADCAST_ARGS:.*]] = "tfl.broadcast_args"(%[[CST]], %[[SHAPE]]) : (tensor<8xi64>, tensor<4xi64>) -> tensor<8xi64> + // CHECK: %[[BROADCAST_TO:.*]] = "tfl.broadcast_to"(%arg1, %[[BROADCAST_ARGS]]) : (tensor, tensor<8xi64>) -> tensor<8x7x6x5x4x3x2x1xf32> + // CHECK: %[[MUL:.*]] = tfl.mul %arg0, %[[BROADCAST_TO]] {fused_activation_function = "NONE"} : tensor<8x7x6x5x4x3x2x1xf32> + // CHECK: return %[[MUL]] : tensor<8x7x6x5x4x3x2x1xf32> +} + +func.func @mul_with_high_dims_dynamic_shape_both_sides(%arg0: tensor<8x7x6x5x?x3x2x1xf32>, %arg1: tensor) -> tensor<8x7x6x5x?x3x2x1xf32> { + %0 = "tf.Mul"(%arg0, %arg1): (tensor<8x7x6x5x?x3x2x1xf32>, tensor) -> tensor<8x7x6x5x?x3x2x1xf32> + func.return %0 : tensor<8x7x6x5x?x3x2x1xf32> + + // CHECK-LABEL:mul_with_high_dims_dynamic_shape_both_sides + // CHECK: %[[SHAPE:.*]] = "tfl.shape"(%arg0) : (tensor<8x7x6x5x?x3x2x1xf32>) -> tensor<8xi64> + // CHECK: %[[SHAPE_1:.*]] = "tfl.shape"(%arg1) : (tensor) -> tensor<4xi64> + // CHECK: %[[BROADCAST_ARGS:.*]] = "tfl.broadcast_args"(%[[SHAPE]], %[[SHAPE_1]]) : (tensor<8xi64>, tensor<4xi64>) -> tensor<8xi64> + // CHECK: %[[BROADCAST_TO:.*]] = "tfl.broadcast_to"(%arg0, %[[BROADCAST_ARGS]]) : (tensor<8x7x6x5x?x3x2x1xf32>, tensor<8xi64>) -> tensor<8x7x6x5x?x3x2x1xf32> + // CHECK: %[[BROADCAST_TO_1:.*]] = "tfl.broadcast_to"(%arg1, %[[BROADCAST_ARGS]]) : (tensor, tensor<8xi64>) -> tensor<8x7x6x5x?x3x2x1xf32> + // CHECK: %[[MUL:.*]] = tfl.mul %[[BROADCAST_TO]], %[[BROADCAST_TO_1]] {fused_activation_function = "NONE"} : tensor<8x7x6x5x?x3x2x1xf32> + // CHECK: return %[[MUL]] : tensor<8x7x6x5x?x3x2x1xf32> +} + +func.func @select_v2_with_unranked_rhs(%arg0: tensor<8x7x6x5x?x3x2x1xf32>, %arg1: tensor<*xf32>, %arg2: tensor<8x7x6x5x?x3x2x1xi1>) -> tensor<8x7x6x5x?x3x2x1xf32> { + %0 = "tf.SelectV2"(%arg2, %arg0, %arg1) : (tensor<8x7x6x5x?x3x2x1xi1>, tensor<8x7x6x5x?x3x2x1xf32>, tensor<*xf32>) -> tensor<8x7x6x5x?x3x2x1xf32> + func.return %0 : tensor<8x7x6x5x?x3x2x1xf32> + + // CHECK-LABEL:select_v2_with_unranked_rhs + // CHECK: %0 = "tf.SelectV2"(%arg2, %arg0, %arg1) : (tensor<8x7x6x5x?x3x2x1xi1>, tensor<8x7x6x5x?x3x2x1xf32>, tensor<*xf32>) -> tensor<8x7x6x5x?x3x2x1xf32> + // CHECK: return %0 : tensor<8x7x6x5x?x3x2x1xf32> +} + +func.func @select_v2_with_high_dims_dynamic_shape_both_sides(%arg0: tensor<8x7x6x5x?x3x2x1xf32>, %arg1: tensor) -> tensor<8x7x6x5x?x3x2x1xf32> { + %0 = "tf.Less"(%arg0, %arg1) : (tensor<8x7x6x5x?x3x2x1xf32>, tensor) -> tensor<8x7x6x5x?x3x2x1xi1> + %1 = "tf.SelectV2"(%0, %arg0, %arg1) : (tensor<8x7x6x5x?x3x2x1xi1>, tensor<8x7x6x5x?x3x2x1xf32>, tensor) -> tensor<8x7x6x5x?x3x2x1xf32> + func.return %1 : tensor<8x7x6x5x?x3x2x1xf32> + + // CHECK-LABEL:select_v2_with_high_dims_dynamic_shape_both_sides + // CHECK: %[[SHAPE_0:.*]] = "tfl.shape"(%arg0) : (tensor<8x7x6x5x?x3x2x1xf32>) -> tensor<8xi64> + // CHECK: %[[SHAPE_1:.*]] = "tfl.shape"(%arg1) : (tensor) -> tensor<4xi64> + // CHECK: %[[BROADCAST_ARGS_0:.*]] = "tfl.broadcast_args"(%[[SHAPE_0]], %[[SHAPE_1]]) : (tensor<8xi64>, tensor<4xi64>) -> tensor<8xi64> + // CHECK: %[[BROADCAST_TO_0:.*]] = "tfl.broadcast_to"(%arg0, %[[BROADCAST_ARGS_0]]) : (tensor<8x7x6x5x?x3x2x1xf32>, tensor<8xi64>) -> tensor<8x7x6x5x?x3x2x1xf32> + // CHECK: %[[BROADCAST_TO_1:.*]] = "tfl.broadcast_to"(%arg1, %[[BROADCAST_ARGS_0]]) : (tensor, tensor<8xi64>) -> tensor<8x7x6x5x?x3x2x1xf32> + // CHECK: %[[LESS:.*]] = tfl.less(%[[BROADCAST_TO_0]], %[[BROADCAST_TO_1]]) : (tensor<8x7x6x5x?x3x2x1xf32>, tensor<8x7x6x5x?x3x2x1xf32>) -> tensor<8x7x6x5x?x3x2x1xi1> + // CHECK: %[[SHAPE_2:.*]] = "tfl.shape"(%[[LESS]]) : (tensor<8x7x6x5x?x3x2x1xi1>) -> tensor<8xi64> + // CHECK: %[[BROADCAST_ARGS_1:.*]] = "tfl.broadcast_args"(%[[BROADCAST_ARGS_0]], %[[SHAPE_2]]) : (tensor<8xi64>, tensor<8xi64>) -> tensor<8xi64> + // CHECK: %[[BROADCAST_TO_2:.*]] = "tfl.broadcast_to"(%[[LESS]], %[[BROADCAST_ARGS_1]]) : (tensor<8x7x6x5x?x3x2x1xi1>, tensor<8xi64>) -> tensor<8x7x6x5x?x3x2x1xi1> + // CHECK: %[[BROADCAST_TO_3:.*]] = "tfl.broadcast_to"(%arg0, %[[BROADCAST_ARGS_1]]) : (tensor<8x7x6x5x?x3x2x1xf32>, tensor<8xi64>) -> tensor<8x7x6x5x?x3x2x1xf32> + // CHECK: %[[BROADCAST_TO_4:.*]] = "tfl.broadcast_to"(%arg1, %[[BROADCAST_ARGS_1]]) : (tensor, tensor<8xi64>) -> tensor<8x7x6x5x?x3x2x1xf32> + // CHECK: %[[SELECT_V2:.*]] = "tfl.select_v2"(%[[BROADCAST_TO_2]], %[[BROADCAST_TO_3]], %[[BROADCAST_TO_4]]) : (tensor<8x7x6x5x?x3x2x1xi1>, tensor<8x7x6x5x?x3x2x1xf32>, tensor<8x7x6x5x?x3x2x1xf32>) -> tensor<8x7x6x5x?x3x2x1xf32> + // CHECK: return %[[SELECT_V2]] : tensor<8x7x6x5x?x3x2x1xf32> +} + +func.func @Bucketize(%arg0: tensor<3x2xf32>) -> tensor<3x2xi32> { + %0 = "tf.Bucketize"(%arg0) {boundaries = [1.0 : f32, 10.0 : f32, 100.0 : f32]} : (tensor<3x2xf32>) -> tensor<3x2xi32> + func.return %0: tensor<3x2xi32> + +// CHECK-LABEL: Bucketize +// CHECK: "tfl.bucketize"(%arg0) <{boundaries = [1.000000e+00 : f32, 1.000000e+01 : f32, 1.000000e+02 : f32]}> : (tensor<3x2xf32>) -> tensor<3x2xi32> +} + +func.func @random_uniform_f32(%arg0: tensor<3xi32>) -> tensor { + %0 = "tf.RandomUniform"(%arg0) {seed = 0 : i64, seed2 = 0: i64} : (tensor<3xi32>) -> tensor + func.return %0 : tensor + +// CHECK-LABEL:random_uniform_f32 +// CHECK: "tfl.random_uniform"(%arg0) <{seed = 0 : i64, seed2 = 0 : i64}> : (tensor<3xi32>) -> tensor +} + +func.func @random_standard_normal_f32(%arg0: tensor<3xi32>) -> tensor { + %0 = "tf.RandomStandardNormal"(%arg0) {seed = 0 : i64, seed2 = 0: i64} : (tensor<3xi32>) -> tensor + func.return %0 : tensor + +// CHECK-LABEL:random_standard_normal_f32 +// CHECK: "tfl.random_standard_normal"(%arg0) <{seed = 0 : i64, seed2 = 0 : i64}> : (tensor<3xi32>) -> tensor +} + +func.func @multinomial_i64(%arg0: tensor<2xf32>, %arg1: tensor<1xi32>) -> tensor<10xi64> { + %0 = "tf.Multinomial"(%arg0, %arg1) {seed = 0 : i64, seed2 = 0: i64} : (tensor<2xf32>, tensor<1xi32>) -> tensor<10xi64> + func.return %0 : tensor<10xi64> + +// CHECK-LABEL:multinomial_i64 +// CHECK: "tfl.multinomial"(%arg0, %arg1) <{seed = 0 : i64, seed2 = 0 : i64}> : (tensor<2xf32>, tensor<1xi32>) -> tensor<10xi64> +} + +func.func @multinomial_i32(%arg0: tensor<2xf32>, %arg1: tensor<1xi32>) -> tensor<10xi32> { + %0 = "tf.Multinomial"(%arg0, %arg1) {seed = 0 : i64, seed2 = 0: i64} : (tensor<2xf32>, tensor<1xi32>) -> tensor<10xi32> + func.return %0 : tensor<10xi32> + +// CHECK-LABEL:multinomial_i32 +// CHECK: "tfl.multinomial"(%arg0, %arg1) <{seed = 0 : i64, seed2 = 0 : i64}> : (tensor<2xf32>, tensor<1xi32>) -> tensor<10xi32> +} + +func.func @dynamic_update_slice(%arg0: tensor<4x5xi32>, %arg1: tensor<1x5xi32>, %arg2: tensor<2xi32>) -> tensor<4x5xi32> { + %0 = "tf.XlaDynamicUpdateSlice"(%arg0, %arg1, %arg2) : (tensor<4x5xi32>, tensor<1x5xi32>, tensor<2xi32>) -> tensor<4x5xi32> + func.return %0 : tensor<4x5xi32> + +// CHECK-LABEL:dynamic_update_slice +// CHECK: "tfl.dynamic_update_slice"(%arg0, %arg1, %arg2) : (tensor<4x5xi32>, tensor<1x5xi32>, tensor<2xi32>) -> tensor<4x5xi32> +} + +func.func @dynamic_update_slice_i64_indice(%arg0: tensor<4x5xi32>, %arg1: tensor<1x5xi32>, %arg2: tensor<2xi64>) -> tensor<4x5xi32> { + %0 = "tf.XlaDynamicUpdateSlice"(%arg0, %arg1, %arg2) : (tensor<4x5xi32>, tensor<1x5xi32>, tensor<2xi64>) -> tensor<4x5xi32> + func.return %0 : tensor<4x5xi32> + +// CHECK-LABEL:dynamic_update_slice_i64_indice +// CHECK: "tfl.dynamic_update_slice"(%arg0, %arg1, %arg2) : (tensor<4x5xi32>, tensor<1x5xi32>, tensor<2xi64>) -> tensor<4x5xi32> +} + +func.func @dynamic_update_slice_f16_arg(%arg0: tensor<4x5xf16>, %arg1: tensor<1x5xf16>, %arg2: tensor<2xi32>) -> tensor<4x5xf16> { + %0 = "tf.XlaDynamicUpdateSlice"(%arg0, %arg1, %arg2) : (tensor<4x5xf16>, tensor<1x5xf16>, tensor<2xi32>) -> tensor<4x5xf16> + func.return %0 : tensor<4x5xf16> + +// CHECK-LABEL:dynamic_update_slice_f16_arg +// CHECK: "tfl.dynamic_update_slice"(%arg0, %arg1, %arg2) : (tensor<4x5xf16>, tensor<1x5xf16>, tensor<2xi32>) -> tensor<4x5xf16> +} + +func.func @dynamic_update_slice_i16(%arg0: tensor<4x5xi16>, %arg1: tensor<1x5xi16>, %arg2: tensor<2xi32>) -> tensor<4x5xi16> { + %0 = "tf.XlaDynamicUpdateSlice"(%arg0, %arg1, %arg2) : (tensor<4x5xi16>, tensor<1x5xi16>, tensor<2xi32>) -> tensor<4x5xi16> + func.return %0 : tensor<4x5xi16> + +// CHECK-LABEL:dynamic_update_slice_i16 +// CHECK: "tfl.dynamic_update_slice"(%arg0, %arg1, %arg2) : (tensor<4x5xi16>, tensor<1x5xi16>, tensor<2xi32>) -> tensor<4x5xi16> +} + +func.func @testReluI32(%arg0: tensor<1xi32>) -> tensor<1xi32> { + %0 = "tf.Relu"(%arg0) : (tensor<1xi32>) -> tensor<1xi32> + func.return %0: tensor<1xi32> + +// CHECK-LABEL: testReluI32 +// CHECK: %[[CONST_0:.*]] = arith.constant dense<0> : tensor +// CHECK: %[[RES0:.*]] = "tfl.maximum"(%arg0, %[[CONST_0]]) : (tensor<1xi32>, tensor) -> tensor<1xi32> +// CHECK: return %[[RES0]] : tensor<1xi32> +} + +func.func @testReluI64(%arg0: tensor<1xi64>) -> tensor<1xi64> { + %0 = "tf.Relu"(%arg0) : (tensor<1xi64>) -> tensor<1xi64> + func.return %0: tensor<1xi64> + +// CHECK-LABEL: testReluI64 +// CHECK: %[[CONST_0:.*]] = arith.constant dense<0> : tensor +// CHECK: %[[RES0:.*]] = "tfl.maximum"(%arg0, %[[CONST_0]]) : (tensor<1xi64>, tensor) -> tensor<1xi64> +// CHECK: return %[[RES0]] : tensor<1xi64> +} + +func.func @atan2(%arg0: tensor<8xf32>, %arg1: tensor<8xf32>) -> tensor<8xf32> { + %0 = "tf.Atan2"(%arg0, %arg1) : (tensor<8xf32>, tensor<8xf32>) -> tensor<8xf32> + func.return %0 : tensor<8xf32> + +// CHECK-LABEL: atan2 +// CHECK: %[[RES0:.*]] = "tfl.atan2"(%arg0, %arg1) : (tensor<8xf32>, tensor<8xf32>) -> tensor<8xf32> +// CHECK: return %[[RES0]] : tensor<8xf32> +} + +func.func @sign(%arg0: tensor<8xf32>) -> tensor<8xf32> { + %0 = "tf.Sign"(%arg0) : (tensor<8xf32>) -> tensor<8xf32> + func.return %0 : tensor<8xf32> + +// CHECK-LABEL: sign +// CHECK: %[[RES0:.*]] = "tfl.sign"(%arg0) : (tensor<8xf32>) -> tensor<8xf32> +// CHECK: return %[[RES0]] : tensor<8xf32> +} + +func.func @bitcast(%arg0: tensor<8xi32>) -> tensor<8xui32> { + %0 = "tf.Bitcast"(%arg0) : (tensor<8xi32>) -> tensor<8xui32> + func.return %0 : tensor<8xui32> + +// CHECK-LABEL: bitcast +// CHECK: %[[RES0:.*]] = "tfl.bitcast"(%arg0) : (tensor<8xi32>) -> tensor<8xui32> +// CHECK: return %[[RES0]] : tensor<8xui32> +} + +func.func @bitcastI32ToI16(%arg0: tensor<8xi32>) -> tensor<8x2xi16> { + %0 = "tf.Bitcast"(%arg0) : (tensor<8xi32>) -> tensor<8x2xi16> + func.return %0 : tensor<8x2xi16> + +// CHECK-LABEL: bitcastI32ToI16 +// CHECK: %[[RES0:.*]] = "tfl.bitcast"(%arg0) : (tensor<8xi32>) -> tensor<8x2xi16> +// CHECK: return %[[RES0]] : tensor<8x2xi16> +} + +func.func @bitcastI16ToUI32(%arg0: tensor<8x2xi16>) -> tensor<8xui32> { + %0 = "tf.Bitcast"(%arg0) : (tensor<8x2xi16>) -> tensor<8xui32> + func.return %0 : tensor<8xui32> + +// CHECK-LABEL: bitcastI16ToUI32 +// CHECK: %[[RES0:.*]] = "tfl.bitcast"(%arg0) : (tensor<8x2xi16>) -> tensor<8xui32> +// CHECK: return %[[RES0]] : tensor<8xui32> +} + +func.func @bitcastFloatToI16(%arg0: tensor<8xf32>) -> tensor<8x2xi16> { + %0 = "tf.Bitcast"(%arg0) : (tensor<8xf32>) -> tensor<8x2xi16> + func.return %0 : tensor<8x2xi16> + +// CHECK-LABEL: bitcastFloatToI16 +// CHECK: %[[RES0:.*]] = "tfl.bitcast"(%arg0) : (tensor<8xf32>) -> tensor<8x2xi16> +// CHECK: return %[[RES0]] : tensor<8x2xi16> +} + +func.func @bitcastI16ToFloat(%arg0: tensor<8x2xi16>) -> tensor<8xf32> { + %0 = "tf.Bitcast"(%arg0) : (tensor<8x2xi16>) -> tensor<8xf32> + func.return %0 : tensor<8xf32> + +// CHECK-LABEL: bitcastI16ToFloat +// CHECK: %[[RES0:.*]] = "tfl.bitcast"(%arg0) : (tensor<8x2xi16>) -> tensor<8xf32> +// CHECK: return %[[RES0]] : tensor<8xf32> +} + +func.func @testBitwiseXor(%arg0: tensor<8xui32>, %arg1: tensor<8xui32>) -> tensor<8xui32> { + %0 = "tf.BitwiseXor"(%arg0, %arg1) : (tensor<8xui32>, tensor<8xui32>) -> tensor<8xui32> + func.return %0 : tensor<8xui32> + + // CHECK-LABEL: testBitwiseXor + // CHECK: %[[RES0:.*]] = "tfl.bitwise_xor"(%arg0, %arg1) : (tensor<8xui32>, tensor<8xui32>) -> tensor<8xui32> + // CHECK: return %[[RES0]] : tensor<8xui32> +} + +func.func @testRightShift(%arg0: tensor<8xui32>, %arg1: tensor<8xui32>) -> tensor<8xui32> { + %0 = "tf.RightShift"(%arg0, %arg1) : (tensor<8xui32>, tensor<8xui32>) -> tensor<8xui32> + func.return %0 : tensor<8xui32> + + // CHECK-LABEL: testRightShift + // CHECK: %[[RES0:.*]] = "tfl.right_shift"(%arg0, %arg1) : (tensor<8xui32>, tensor<8xui32>) -> tensor<8xui32> + // CHECK: return %[[RES0]] : tensor<8xui32> +} + +// ============================================================================= +// Training OPs +// ============================================================================= + +func.func @sigmoidGrad(%arg0: tensor, %arg1: tensor) -> tensor { + %0 = "tf.SigmoidGrad"(%arg0, %arg1) : (tensor, tensor) -> tensor + func.return %0 : tensor +// CHECK-LABEL: sigmoidGrad +// CHECK-NEXT: [[ONE:%.+]] = arith.constant dense<1.000000e+00> : tensor +// CHECK-NEXT: [[SUB:%.+]] = tfl.sub([[ONE]], %arg0) <{fused_activation_function = "NONE"}> : (tensor, tensor) -> tensor +// CHECK-NEXT: [[MUL0:%.+]] = tfl.mul %arg0, [[SUB]] {fused_activation_function = "NONE"} : tensor +// CHECK-NEXT: [[MUL1:%.+]] = tfl.mul %arg1, [[MUL0]] {fused_activation_function = "NONE"} : tensor +// CHECK: return [[MUL1]] +} + +func.func @batchmatmul2fullyconnected(%arg0: tensor<4x128x2xf32>) -> (tensor<4x128x1xf32>) { + %0 = "tf.Const"() {value = dense<[[1.0], [2.0]]> : tensor<2x1xf32>} : () -> tensor<2x1xf32> + %1 = "tf.BatchMatMulV2"(%arg0, %0) : (tensor<4x128x2xf32>, tensor<2x1xf32>) -> tensor<4x128x1xf32> + func.return %1 : tensor<4x128x1xf32> + + // CHECK-LABEL: batchmatmul2fullyconnected + // CHECK-DAG: %cst_0 = arith.constant dense<[1, 0]> : tensor<2xi32> + // CHECK: %0 = "tfl.transpose"(%cst, %cst_0) : (tensor<2x1xf32>, tensor<2xi32>) -> tensor<1x2xf32> + // CHECK-DAG: %1 = "tfl.no_value"() <{value}> : () -> none + // CHECK: %2 = "tfl.fully_connected"(%arg0, %0, %1) <{fused_activation_function = "NONE", keep_num_dims = true, weights_format = "DEFAULT"}> : (tensor<4x128x2xf32>, tensor<1x2xf32>, none) -> tensor<4x128x1xf32> + // CHECK: return %2 : tensor<4x128x1xf32> +} + +func.func @approx_top_k_with_max_k_last_reduction_dimension(%arg0: tensor<1x4xf32>) -> (tensor<1x4xf32>, tensor<1x4xi32>) { + %values, %indices = "tf.ApproxTopK"(%arg0) {aggregate_to_topk = true, is_max_k = true, k = 4 : i64, recall_target = 8.500000e-01 : f32, reduction_dimension = 1 : i64, reduction_input_size_override = -1 : i64} : (tensor<1x4xf32>) -> (tensor<1x4xf32>, tensor<1x4xi32>) + func.return %values, %indices: tensor<1x4xf32>, tensor<1x4xi32> + + // CHECK-LABEL: approx_top_k_with_max_k_last_reduction_dimension + // CHECK-DAG: %cst = arith.constant dense<4> : tensor + // CHECK: %values, %indices = "tfl.topk_v2"(%arg0, %cst) : (tensor<1x4xf32>, tensor) -> (tensor<1x4xf32>, tensor<1x4xi32>) + // CHECK: return %values, %indices : tensor<1x4xf32>, tensor<1x4xi32> +} + +func.func @approx_top_k_with_min_k(%arg0: tensor<1x4xf32>) -> (tensor<1x4xf32>, tensor<1x4xi32>) { + %values, %indices = "tf.ApproxTopK"(%arg0) {aggregate_to_topk = true, is_max_k = false, k = 4 : i64, recall_target = 8.500000e-01 : f32, reduction_dimension = 1 : i64, reduction_input_size_override = -1 : i64} : (tensor<1x4xf32>) -> (tensor<1x4xf32>, tensor<1x4xi32>) + func.return %values, %indices: tensor<1x4xf32>, tensor<1x4xi32> + + // CHECK-LABEL: approx_top_k_with_min_k + // CHECK: %values, %indices = "tf.ApproxTopK"(%arg0) <{aggregate_to_topk = true, is_max_k = false, k = 4 : i64, recall_target = 8.500000e-01 : f32, reduction_dimension = 1 : i64, reduction_input_size_override = -1 : i64}> : (tensor<1x4xf32>) -> (tensor<1x4xf32>, tensor<1x4xi32>) + // CHECK: return %values, %indices : tensor<1x4xf32>, tensor<1x4xi32> +} + +func.func @approx_top_k_reduction_dimension_not_last_dim(%arg0: tensor<1x4xf32>) -> (tensor<1x4xf32>, tensor<1x4xi32>) { + %values, %indices = "tf.ApproxTopK"(%arg0) {aggregate_to_topk = true, is_max_k = true, k = 4 : i64, recall_target = 8.500000e-01 : f32, reduction_dimension = 0 : i64, reduction_input_size_override = -1 : i64} : (tensor<1x4xf32>) -> (tensor<1x4xf32>, tensor<1x4xi32>) + func.return %values, %indices: tensor<1x4xf32>, tensor<1x4xi32> + + // CHECK-LABEL: approx_top_k_reduction_dimension_not_last_dim + // CHECK: %values, %indices = "tf.ApproxTopK"(%arg0) <{aggregate_to_topk = true, is_max_k = true, k = 4 : i64, recall_target = 8.500000e-01 : f32, reduction_dimension = 0 : i64, reduction_input_size_override = -1 : i64}> : (tensor<1x4xf32>) -> (tensor<1x4xf32>, tensor<1x4xi32>) + // CHECK: return %values, %indices : tensor<1x4xf32>, tensor<1x4xi32> +} + diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/legalize_jax_random.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/legalize_jax_random.mlir new file mode 100644 index 0000000000000000000000000000000000000000..3ef7a2b35358daa257b01627818485089e087f83 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/legalize_jax_random.mlir @@ -0,0 +1,33 @@ +// RUN: litert-opt -tfl-legalize-random %s | FileCheck %s + + +// CHECK-LABEL: func @tfl_wrapped_jax_random_normal( +// CHECK-SAME: %[[RNG:.*]]: tensor<2xui32>) -> tuple> { +// CHECK: %[[VAL_0:.*]] = stablehlo.constant dense<[3, 4]> : tensor<2xi32> +// CHECK: %[[VAL_1:.*]] = "tfl.custom"(%[[VAL_0]]) <{custom_code = "RandomStandardNormal", custom_option = #tfl}> : (tensor<2xi32>) -> tensor<3x4xf32> +// CHECK: %[[VAL_2:.*]] = stablehlo.tuple %[[VAL_1]] : tuple> +// CHECK: return %[[VAL_2]] : tuple> +// CHECK: } +func.func @tfl_wrapped_jax_random_normal(%arg0: tensor<2xui32>) -> tuple> { + // This is a fake jax random normal body. + %0 = stablehlo.constant dense<0.0> : tensor<12xf32> + %1 = "stablehlo.reshape"(%0) : (tensor<12xf32>) -> tensor<3x4xf32> + %2 = "stablehlo.tuple"(%1) : (tensor<3x4xf32>) -> tuple> + func.return %2 : tuple> +} + + +// CHECK-LABEL: func @tfl_wrapped_jax_random_uniform( +// CHECK-SAME: %[[RNG:.*]]: tensor<2xui32>) -> tuple> { +// CHECK: %[[VAL_0:.*]] = stablehlo.constant dense<[1, 2]> : tensor<2xi32> +// CHECK: %[[VAL_1:.*]] = "tfl.custom"(%[[VAL_0]]) <{custom_code = "RandomUniform", custom_option = #tfl}> : (tensor<2xi32>) -> tensor<1x2xf32> +// CHECK: %[[VAL_2:.*]] = stablehlo.tuple %[[VAL_1]] : tuple> +// CHECK: return %[[VAL_2]] : tuple> +// CHECK: } +func.func @tfl_wrapped_jax_random_uniform(%arg0: tensor<2xui32>) -> tuple> { + // This is a fake jax random uniform body. + %0 = stablehlo.constant dense<0.0> : tensor<2xf32> + %1 = "stablehlo.reshape"(%0) : (tensor<2xf32>) -> tensor<1x2xf32> + %2 = "stablehlo.tuple"(%1) : (tensor<1x2xf32>) -> tuple> + func.return %2 : tuple> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/lift_tflite_flex_ops.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/lift_tflite_flex_ops.mlir new file mode 100644 index 0000000000000000000000000000000000000000..4645edecf28d3e538a9a4d4af0a388515593ba15 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/lift_tflite_flex_ops.mlir @@ -0,0 +1,92 @@ +// RUN: litert-opt %s -tfl-lift-tflite-flex-ops | FileCheck %s + +// CHECK-LABEL: TfAdd +func.func @TfAdd(%arg0: tensor<4xf64>, %arg1: tensor<4xf64>) -> tensor<4xf64> { + %0 = "tfl.custom"(%arg0, %arg1) { + custom_code = "FlexAdd", + custom_option = #tfl + } : (tensor<4xf64>, tensor<4xf64>) -> tensor<4xf64> + +// CHECK: "tf.Add"(%arg0, %arg1) {T = f64} : (tensor<4xf64>, tensor<4xf64>) -> tensor<4xf64> + func.return %0 : tensor<4xf64> +} + + + +// CHECK-LABEL: TfBatchMatMulV2 +func.func @TfBatchMatMulV2(%arg0: tensor<4x128x2xf32>, %arg1: tensor<2x1xf32>) -> tensor<4x128x1xf32> { + %0 = "tfl.custom"(%arg0, %arg1) { + custom_code = "FlexBatchMatMulV2", + custom_option = #tfl + } : (tensor<4x128x2xf32>, tensor<2x1xf32>) -> tensor<4x128x1xf32> + +// CHECK: "tf.BatchMatMulV2"(%arg0, %arg1) <{adj_x = false, adj_y = false}> {T = f32} : (tensor<4x128x2xf32>, tensor<2x1xf32>) -> tensor<4x128x1xf32> + func.return %0 : tensor<4x128x1xf32> +} + + +// CHECK-LABEL: TfTensorArrayV3 +func.func @TfTensorArrayV3(%arg0: tensor) -> tensor { + %0:2 = "tfl.custom"(%arg0) { + custom_code = "FlexTensorArrayV3", + custom_option = #tfl + } : (tensor) -> (tensor<2xi32>, tensor<*xf32>) + +// CHECK: "tf.TensorArrayV3" +// CHECK-SAME: : (tensor) -> (tensor<2x!tf_type.resource>, tensor) + + %1 = "tfl.cast"(%0#1) : (tensor<*xf32>) -> tensor + func.return %1 : tensor +} + +// CHECK-LABEL: TfParseExample +func.func @TfParseExample(%arg0: tensor<1x!tf_type.string>) -> (tensor<1x1x!tf_type.string>, tensor<1x1x!tf_type.string>) { + %0 = "tfl.pseudo_const"() {value = dense<["image/encoded", "image/text"]> : tensor<2x!tf_type.string>} : () -> tensor<2x!tf_type.string> + %1 = "tfl.pseudo_const"() {value = dense<"image/encoded"> : tensor<1x!tf_type.string>} : () -> tensor<1x!tf_type.string> + %2 = "tfl.pseudo_const"() {value = dense<"image/text"> : tensor<1x!tf_type.string>} : () -> tensor<1x!tf_type.string> + %3 = "tfl.pseudo_const"() {value = dense<""> : tensor<1x!tf_type.string>} : () -> tensor<1x!tf_type.string> + %4:2 = "tfl.custom"(%arg0, %0, %1, %2, %3, %3) { + custom_code = "FlexParseExample", + custom_option = #tfl + } : ( + tensor<1x!tf_type.string>, tensor<2x!tf_type.string>, tensor<1x!tf_type.string>, + tensor<1x!tf_type.string>, tensor<1x!tf_type.string>, tensor<1x!tf_type.string> + ) -> (tensor<1x1x!tf_type.string>, tensor<1x1x!tf_type.string>) + func.return %4#0, %4#1 : tensor<1x1x!tf_type.string>, tensor<1x1x!tf_type.string> +// CHECK: "tf.ParseExample"( +// CHECK-SAME: operandSegmentSizes = array, resultSegmentSizes = array +} + +// CHECK-LABEL: TfMapDataset +func.func @TfMapDataset(%arg0: tensor) -> (tensor) { + %0 = "tfl.custom"(%arg0) { + custom_code = "FlexMapDataset", + custom_option = #tfl + } : (tensor) -> tensor + + func.return %0 : tensor +// CHECK: "tf.MapDataset"( +// CHECK-SAME: <{f = @{{.*}}, metadata = "", output_shapes = [#tf_type.shape<>], output_types = [!tf_type.string], preserve_cardinality = true, use_inter_op_parallelism = true}> {Targuments = []} +} + +// CHECK-LABEL: TfTakeWhileDataset +func.func @TfTakeWhileDataset(%arg0: tensor, %arg1: tensor) -> (tensor) { + %0 = "tfl.custom"(%arg0, %arg1) { + custom_code = "FlexTakeWhileDataset", + custom_option = #tfl + } : (tensor, tensor) -> tensor + + func.return %0 : tensor +// CHECK: "tf.TakeWhileDataset"( +// CHECK-SAME: <{metadata = "", output_shapes = [#tf_type.shape<>], output_types = [!tf_type.string], predicate = @{{.*}}}> {Targuments = [!tf_type.resource, !tf_type.resource, i64, !tf_type.resource, !tf_type.resource, !tf_type.resource, !tf_type.resource, i64]} +} + +// CHECK-LABEL: FailureOnInvalidOp +func.func @FailureOnInvalidOp(%arg0: tensor<4xf64>, %arg1: tensor<4xf64>) -> tensor<4xf64> { + // expected-error@+1 can't find registered TF op for Nop + %0 = "tfl.custom"(%arg0, %arg1) { + custom_code = "FlexNop", + custom_option = #tfl + } : (tensor<4xf64>, tensor<4xf64>) -> tensor<4xf64> + func.return %0 : tensor<4xf64> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/lower-static-tensor-list-default-to-single-batch.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/lower-static-tensor-list-default-to-single-batch.mlir new file mode 100644 index 0000000000000000000000000000000000000000..f7801b4d85137aaa32befd63c7b5cc617e4395ec --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/lower-static-tensor-list-default-to-single-batch.mlir @@ -0,0 +1,14 @@ +// RUN: litert-opt "-tfl-lower-static-tensor-list=allow-tensorlist-pass-through default-to-single-batch=false" -split-input-file %s | FileCheck %s + +// ----- + +func.func @tensorlistReserveConstantUnknownElementShapeDim(%arg0: tensor, %arg1: tensor) -> tensor { + %cst = arith.constant dense<[-1, 7]> : tensor<2xi32> + %0 = "tf.TensorListReserve"(%cst, %arg0) : (tensor<2xi32>, tensor) -> tensor>> + %1 = "tf.TensorListGetItem"(%0, %arg1, %cst) : (tensor>>, tensor, tensor<2xi32>) -> tensor + func.return %1 : tensor + +// CHECK-LABEL: tensorlistReserveConstantUnknownElementShapeDim +// CHECK: "tf.TensorListReserve" +// CHECK: "tf.TensorListGetItem" +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/lower-static-tensor-list-enable-dynamic-update-slice.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/lower-static-tensor-list-enable-dynamic-update-slice.mlir new file mode 100644 index 0000000000000000000000000000000000000000..18c76f688599e203a595c31696b806b273e6c31c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/lower-static-tensor-list-enable-dynamic-update-slice.mlir @@ -0,0 +1,25 @@ +// RUN: litert-opt "-tfl-lower-static-tensor-list=allow-tensorlist-pass-through default-to-single-batch enable-dynamic-update-slice" -split-input-file %s | FileCheck %s + +// ----- + +// CHECK-LABEL: tensorlistSetItem +func.func @tensorlistSetItem(%arg0: tensor<3x10xf32>, %arg1: tensor<1xi32>, %arg2: tensor, %arg3: tensor<10xf32>) -> tensor<3x10xf32> { + %0 = "tf.TensorListFromTensor"(%arg0, %arg1) : (tensor<3x10xf32>, tensor<1xi32>) -> tensor>> + %1 = "tf.TensorListSetItem"(%0, %arg2, %arg3) : (tensor>>, tensor, tensor<10xf32>) -> tensor>> + %2 = "tf.TensorListStack"(%1, %arg1) : (tensor>>, tensor<1xi32>) -> tensor<3x10xf32> + func.return %2 : tensor<3x10xf32> + +// CHECK-SAME: ([[INPUT:%.*]]: tensor<3x10xf32>, [[ELEM_SHAPE:%.*]]: tensor<1xi32>, [[INDEX:%.*]]: tensor, [[ITEM:%.*]]: tensor<10xf32>) +// CHECK-DAG: [[ITEM_RANK:%.*]] = "tf.Rank"([[ITEM]]) : (tensor<10xf32>) -> tensor +// CHECK-DAG: [[ZERO:%cst.*]] = arith.constant dense<0> : tensor +// CHECK-DAG: [[VECTOR_RANK:%.*]] = "tf.ExpandDims"([[ITEM_RANK]], [[ZERO]]) : (tensor, tensor) -> tensor<1xi32> +// CHECK-DAG: [[ZERO_1:%cst.*]] = arith.constant dense<0> : tensor +// CHECK-DAG: [[PARTIAL_INDEX:%.*]] = "tf.Fill"([[VECTOR_RANK]], [[ZERO_1]]) : (tensor<1xi32>, tensor) -> tensor +// CHECK-DAG: [[EXPANDED_INDIX:%.*]] = "tf.ExpandDims"([[INDEX]], [[ZERO]]) : (tensor, tensor) -> tensor<1xi32> +// CHECK-DAG: [[ITEM_INDEX:%.*]] = "tf.Concat"([[ZERO]], [[EXPANDED_INDIX]], [[PARTIAL_INDEX]]) : (tensor, tensor<1xi32>, tensor) -> tensor +// CHECK-DAG: [[EXPANDED_ITEM:%.*]] = "tf.ExpandDims"([[ITEM]], [[ZERO]]) : (tensor<10xf32>, tensor) -> tensor<*xf32> +// CHECK-DAG: [[RESULT:%.*]] = "tf.XlaDynamicUpdateSlice"([[INPUT]], [[EXPANDED_ITEM]], [[ITEM_INDEX]]) : (tensor<3x10xf32>, tensor<*xf32>, tensor) -> tensor<3x10xf32> +// CHECK-DAG: return [[RESULT]] : tensor<3x10xf32> +} + +// ----- diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/lower-static-tensor-list.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/lower-static-tensor-list.mlir new file mode 100644 index 0000000000000000000000000000000000000000..beb16d0675d43b12b94f361150000db9bf9552e3 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/lower-static-tensor-list.mlir @@ -0,0 +1,593 @@ +// RUN: litert-opt "-tfl-lower-static-tensor-list=allow-tensorlist-pass-through default-to-single-batch" -split-input-file %s | FileCheck %s + +// ----- + +// CHECK-LABEL: tensorlistConst +func.func @tensorlistConst(%arg0 : tensor<1xi32>) -> tensor<2x3xi32> { + // CHECK-DAG: %[[ELEMENT0:.*]] = "tf.Const"() <{value = dense<[0, 1, 2]> : tensor<3xi32>}> : () -> tensor<3xi32> + // CHECK-DAG: %[[ELEMENT1:.*]] = "tf.Const"() <{value = dense<[3, 4, 5]> : tensor<3xi32>}> : () -> tensor<3xi32> + // CHECK: %[[LIST:.*]] = "tf.Pack"(%[[ELEMENT0]], %[[ELEMENT1]]) <{axis = 0 : i64}> : (tensor<3xi32>, tensor<3xi32>) -> tensor<2x3xi32> + %0 = "tf.Const"() {value = #tf_type : tensor} : () -> tensor>> + + // CHECK: return %[[LIST]] + %1 = "tf.TensorListStack"(%0, %arg0) : (tensor>>, tensor<1xi32>) -> tensor<2x3xi32> + func.return %1 : tensor<2x3xi32> +} + +// ----- + +// CHECK-LABEL: func @emptyTensorlistConst +func.func @emptyTensorlistConst(%arg0 : tensor<1xi32>) -> tensor<0x3xi32> { + %0 = "tf.Const"() {value = #tf_type : tensor} : () -> tensor>> + + // CHECK: "tf.Const"() <{value = dense<> : tensor<0x3xi32>}> : () -> tensor<0x3xi32> + // CHECK-NOT: tf.TensorListStack + %1 = "tf.TensorListStack"(%0, %arg0) : (tensor>>, tensor<1xi32>) -> tensor<0x3xi32> + func.return %1 : tensor<0x3xi32> +} + +// ----- + +// CHECK-LABEL: func @tensorlistGetItem +func.func @tensorlistGetItem(%arg0: tensor<3x10xf32>, %arg1: tensor<1xi32>, %arg2: tensor) -> (tensor<10xf32>, tensor<3x10xf32>) { + %0 = "tf.TensorListFromTensor"(%arg0, %arg1) : (tensor<3x10xf32>, tensor<1xi32>) -> tensor>> + %1 = "tf.TensorListGetItem"(%0, %arg2, %arg1) : (tensor>>, tensor, tensor<1xi32>) -> tensor<10xf32> + %2 = "tf.TensorListStack"(%0, %arg1) : (tensor>>, tensor<1xi32>) -> tensor<3x10xf32> + func.return %1, %2 : tensor<10xf32>, tensor<3x10xf32> + +// CHECK: %0 = "tf.Gather"(%arg0, %arg2) <{validate_indices = true}> : (tensor<3x10xf32>, tensor) -> tensor<10xf32> +// CHECK: return %0, %arg0 : tensor<10xf32>, tensor<3x10xf32> +} + +// ----- + +// CHECK-LABEL: func @tensorlistGetItemWithUnknownRank +func.func @tensorlistGetItemWithUnknownRank(%arg0: tensor<*xf32>, %arg1: tensor<1xi32>, %arg2: tensor) -> (tensor<*xf32>, tensor<*xf32>) { + %0 = "tf.TensorListFromTensor"(%arg0, %arg1) : (tensor<*xf32>, tensor<1xi32>) -> tensor>> + %1 = "tf.TensorListGetItem"(%0, %arg2, %arg1) : (tensor>>, tensor, tensor<1xi32>) -> tensor<*xf32> + %2 = "tf.TensorListStack"(%0, %arg1) : (tensor>>, tensor<1xi32>) -> tensor<*xf32> + func.return %1, %2 : tensor<*xf32>, tensor<*xf32> + +// CHECK: %0 = "tf.Gather"(%arg0, %arg2) <{validate_indices = true}> : (tensor<*xf32>, tensor) -> tensor<*xf32> +// CHECK: return %0, %arg0 : tensor<*xf32>, tensor<*xf32> +} + +// ----- + +// CHECK-LABEL: tensorlistStackWithConstantElementShape +func.func @tensorlistStackWithConstantElementShape(%arg0: tensor) -> (tensor<2x3xf32>) { + %cst = arith.constant dense<3> : tensor<1xi32> + %0 = "tf.TensorListFromTensor"(%arg0, %cst) : (tensor, tensor<1xi32>) -> tensor>> + %1 = "tf.TensorListStack"(%0, %cst) {num_elements = 2 : i64} : (tensor>>, tensor<1xi32>) -> tensor<2x3xf32> + func.return %1 : tensor<2x3xf32> + +// CHECK: [[ELEM_SHAPE:%cst.*]] = arith.constant dense<3> : tensor<1xi32> +// CHECK-NEXT: [[SHAPE:%.*]] = "tf.Shape"(%arg0) : (tensor) -> tensor +// CHECK-NEXT: [[RESHAPE:%.*]] = "tf.Reshape"(%arg0, [[SHAPE]]) : (tensor, tensor) -> tensor<2x3xf32> +// CHECK-NEXT: return [[RESHAPE]] : tensor<2x3xf32> +} + +// ----- + +// CHECK-LABEL: tensorlistSetItem +func.func @tensorlistSetItem(%arg0: tensor<3x10xf32>, %arg1: tensor<1xi32>, %arg2: tensor, %arg3: tensor<10xf32>) -> tensor<3x10xf32> { + %0 = "tf.TensorListFromTensor"(%arg0, %arg1) : (tensor<3x10xf32>, tensor<1xi32>) -> tensor>> + %1 = "tf.TensorListSetItem"(%0, %arg2, %arg3) : (tensor>>, tensor, tensor<10xf32>) -> tensor>> + %2 = "tf.TensorListStack"(%1, %arg1) : (tensor>>, tensor<1xi32>) -> tensor<3x10xf32> + func.return %2 : tensor<3x10xf32> + +// CHECK-SAME: ([[INPUT:%.*]]: tensor<3x10xf32>, [[ELEM_SHAPE:%.*]]: tensor<1xi32>, [[INDEX:%.*]]: tensor, [[ITEM:%.*]]: tensor<10xf32>) +// CHECK-DAG: [[RANK:%.*]] = "tf.Rank"([[ITEM]]) : (tensor<10xf32>) -> tensor +// CHECK-DAG: [[ZERO:%cst.*]] = arith.constant dense<0> : tensor +// CHECK-DAG: [[ONE:%cst.*]] = arith.constant dense<1> : tensor +// CHECK: [[SUFFIX_START:%.*]] = "tf.Add"([[INDEX]], [[ONE]]) : (tensor, tensor) -> tensor +// CHECK: [[VECTOR_RANK:%.*]] = "tf.ExpandDims"([[RANK]], [[ZERO]]) : (tensor, tensor) -> tensor<1xi32> +// CHECK-DAG: [[NEG_ONE:%cst.*]] = arith.constant dense<-1> : tensor +// CHECK-DAG: [[ZERO_1:%cst.*]] = arith.constant dense<0> : tensor + + +// CHECK: [[PARTIAL_START_POS:%.*]] = "tf.Fill"([[VECTOR_RANK]], [[ZERO_1]]) : (tensor<1xi32>, tensor) -> tensor +// CHECK: [[ZERO_2:%cst.*]] = arith.constant dense<0> : tensor +// CHECK: [[EXPANDED_START_INDEX:%.*]] = "tf.ExpandDims"([[ZERO]], [[ZERO_2]]) : (tensor, tensor) -> tensor<1xi32> +// CHECK: [[START_POS:%.*]] = "tf.Concat"([[ZERO_2]], [[EXPANDED_START_INDEX]], [[PARTIAL_START_POS]]) : (tensor, tensor<1xi32>, tensor) -> tensor +// CHECK: [[LEADING_DIM:%.*]] = "tf.ExpandDims"([[INDEX]], [[ZERO_2]]) : (tensor, tensor) -> tensor<1xi32> +// CHECK: [[NEG_ONE_1:%cst.*]] = arith.constant dense<-1> : tensor +// CHECK: [[PARTIAL_SIZE:%.*]] = "tf.Fill"([[VECTOR_RANK]], [[NEG_ONE_1]]) : (tensor<1xi32>, tensor) -> tensor +// CHECK: [[SLICE_SIZE:%.*]] = "tf.Concat"([[ZERO_2]], [[LEADING_DIM]], [[PARTIAL_SIZE]]) : (tensor, tensor<1xi32>, tensor) -> tensor +// CHECK: [[SLICE:%.*]] = "tf.Slice"([[INPUT]], [[START_POS]], [[SLICE_SIZE]]) : (tensor<3x10xf32>, tensor, tensor) -> tensor<*xf32> + + +// CHECK: [[ZERO_3:%cst.*]] = arith.constant dense<0> : tensor +// CHECK: [[PARTIAL_START_POS_1:%.*]] = "tf.Fill"([[VECTOR_RANK]], [[ZERO_3]]) : (tensor<1xi32>, tensor) -> tensor +// CHECK: [[ZERO_4:%cst.*]] = arith.constant dense<0> : tensor +// CHECK: [[EXPANDED_START_INDEX_1:%.*]] = "tf.ExpandDims"([[SUFFIX_START]], [[ZERO_4]]) : (tensor, tensor) -> tensor<1xi32> +// CHECK: [[START_POS_1:%.*]] = "tf.Concat"([[ZERO_4]], [[EXPANDED_START_INDEX_1]], [[PARTIAL_START_POS_1]]) : (tensor, tensor<1xi32>, tensor) -> tensor +// CHECK: [[LEADING_DIM_1:%.*]] = "tf.ExpandDims"([[NEG_ONE]], [[ZERO_4]]) : (tensor, tensor) -> tensor<1xi32> +// CHECK: [[NEG_ONE_2:%cst.*]] = arith.constant dense<-1> : tensor +// CHECK: [[PARTIAL_SIZE_1:%.*]] = "tf.Fill"([[VECTOR_RANK]], [[NEG_ONE_2]]) : (tensor<1xi32>, tensor) -> tensor +// CHECK: [[SLICE_SIZE_1:%.*]] = "tf.Concat"([[ZERO_4]], [[LEADING_DIM_1]], [[PARTIAL_SIZE_1]]) : (tensor, tensor<1xi32>, tensor) -> tensor +// CHECK: [[SLICE_1:%.*]] = "tf.Slice"([[INPUT]], [[START_POS_1]], [[SLICE_SIZE_1]]) : (tensor<3x10xf32>, tensor, tensor) -> tensor<*xf32> + + +// CHECK: [[EXPANDED_ITEM:%.*]] = "tf.ExpandDims"([[ITEM]], [[ZERO]]) : (tensor<10xf32>, tensor) -> tensor<*xf32> +// CHECK: [[RESULT:%.*]] = "tf.Concat"([[ZERO]], [[SLICE]], [[EXPANDED_ITEM]], [[SLICE_1]]) : (tensor, tensor<*xf32>, tensor<*xf32>, tensor<*xf32>) -> tensor<3x10xf32> +// CHECK: return [[RESULT]] : tensor<3x10xf32> +} + +// ----- + +// CHECK-LABEL: tensorlistSetItemWithScalarElements +func.func @tensorlistSetItemWithScalarElements(%arg0: tensor<5xf32>, %arg1: tensor<0xi32>, %arg2: tensor, %arg3: tensor) -> tensor<5xf32> { + %0 = "tf.TensorListFromTensor"(%arg0, %arg1) : (tensor<5xf32>, tensor<0xi32>) -> tensor>> + %1 = "tf.TensorListSetItem"(%0, %arg2, %arg3) : (tensor>>, tensor, tensor) -> tensor>> + %2 = "tf.TensorListStack"(%1, %arg1) : (tensor>>, tensor<0xi32>) -> tensor<5xf32> + func.return %2 : tensor<5xf32> + +// CHECK-SAME: ([[INPUT:%.*]]: tensor<5xf32>, [[ELEM_SHAPE:%.*]]: tensor<0xi32>, [[INDEX:%.*]]: tensor, [[ITEM:%.*]]: tensor) +// CHECK-DAG: [[RANK:%.*]] = "tf.Rank"([[ITEM]]) : (tensor) -> tensor +// CHECK-DAG: [[ZERO:%cst.*]] = arith.constant dense<0> : tensor +// CHECK-DAG: [[ONE:%cst.*]] = arith.constant dense<1> : tensor +// CHECK: [[SUFFIX_START:%.*]] = "tf.Add"([[INDEX]], [[ONE]]) : (tensor, tensor) -> tensor +// CHECK: [[VECTOR_RANK:%.*]] = "tf.ExpandDims"([[RANK]], [[ZERO]]) : (tensor, tensor) -> tensor<1xi32> +// CHECK-DAG: [[NEG_ONE:%cst.*]] = arith.constant dense<-1> : tensor +// CHECK-DAG: [[ZERO_1:%cst.*]] = arith.constant dense<0> : tensor + + +// CHECK: [[PARTIAL_START_POS:%.*]] = "tf.Fill"([[VECTOR_RANK]], [[ZERO_1]]) : (tensor<1xi32>, tensor) -> tensor +// CHECK: [[ZERO_2:%cst.*]] = arith.constant dense<0> : tensor +// CHECK: [[EXPANDED_START_INDEX:%.*]] = "tf.ExpandDims"([[ZERO]], [[ZERO_2]]) : (tensor, tensor) -> tensor<1xi32> +// CHECK: [[START_POS:%.*]] = "tf.Concat"([[ZERO_2]], [[EXPANDED_START_INDEX]], [[PARTIAL_START_POS]]) : (tensor, tensor<1xi32>, tensor) -> tensor +// CHECK: [[LEADING_DIM:%.*]] = "tf.ExpandDims"([[INDEX]], [[ZERO_2]]) : (tensor, tensor) -> tensor<1xi32> +// CHECK: [[NEG_ONE_1:%cst.*]] = arith.constant dense<-1> : tensor +// CHECK: [[PARTIAL_SIZE:%.*]] = "tf.Fill"([[VECTOR_RANK]], [[NEG_ONE_1]]) : (tensor<1xi32>, tensor) -> tensor +// CHECK: [[SLICE_SIZE:%.*]] = "tf.Concat"([[ZERO_2]], [[LEADING_DIM]], [[PARTIAL_SIZE]]) : (tensor, tensor<1xi32>, tensor) -> tensor +// CHECK: [[SLICE:%.*]] = "tf.Slice"([[INPUT]], [[START_POS]], [[SLICE_SIZE]]) : (tensor<5xf32>, tensor, tensor) -> tensor<*xf32> + + +// CHECK: [[ZERO_3:%cst.*]] = arith.constant dense<0> : tensor +// CHECK: [[PARTIAL_START_POS_1:%.*]] = "tf.Fill"([[VECTOR_RANK]], [[ZERO_3]]) : (tensor<1xi32>, tensor) -> tensor +// CHECK: [[ZERO_4:%cst.*]] = arith.constant dense<0> : tensor +// CHECK: [[EXPANDED_START_INDEX_1:%.*]] = "tf.ExpandDims"([[SUFFIX_START]], [[ZERO_4]]) : (tensor, tensor) -> tensor<1xi32> +// CHECK: [[START_POS_1:%.*]] = "tf.Concat"([[ZERO_4]], [[EXPANDED_START_INDEX_1]], [[PARTIAL_START_POS_1]]) : (tensor, tensor<1xi32>, tensor) -> tensor +// CHECK: [[LEADING_DIM_1:%.*]] = "tf.ExpandDims"([[NEG_ONE]], [[ZERO_4]]) : (tensor, tensor) -> tensor<1xi32> +// CHECK: [[NEG_ONE_2:%cst.*]] = arith.constant dense<-1> : tensor +// CHECK: [[PARTIAL_SIZE_1:%.*]] = "tf.Fill"([[VECTOR_RANK]], [[NEG_ONE_2]]) : (tensor<1xi32>, tensor) -> tensor +// CHECK: [[SLICE_SIZE_1:%.*]] = "tf.Concat"([[ZERO_4]], [[LEADING_DIM_1]], [[PARTIAL_SIZE_1]]) : (tensor, tensor<1xi32>, tensor) -> tensor +// CHECK: [[SLICE_1:%.*]] = "tf.Slice"([[INPUT]], [[START_POS_1]], [[SLICE_SIZE_1]]) : (tensor<5xf32>, tensor, tensor) -> tensor<*xf32> + + +// CHECK: [[EXPANDED_ITEM:%.*]] = "tf.ExpandDims"([[ITEM]], [[ZERO]]) : (tensor, tensor) -> tensor<*xf32> +// CHECK: [[RESULT:%.*]] = "tf.Concat"([[ZERO]], [[SLICE]], [[EXPANDED_ITEM]], [[SLICE_1]]) : (tensor, tensor<*xf32>, tensor<*xf32>, tensor<*xf32>) -> tensor<5xf32> +// CHECK: return [[RESULT]] : tensor<5xf32> +} + +// ----- + +// CHECK-LABEL: tensorlistReserve +func.func @tensorlistReserve(%arg0: tensor<3xi32>, %arg1: tensor, %arg2: tensor) -> tensor { + %0 = "tf.TensorListReserve"(%arg0, %arg1) : (tensor<3xi32>, tensor) -> tensor>> + %1 = "tf.TensorListGetItem"(%0, %arg2, %arg0) : (tensor>>, tensor, tensor<3xi32>) -> tensor + func.return %1 : tensor + +// CHECK-DAG: [[ZERO1:%cst.*]] = arith.constant dense<0> : tensor +// CHECK-DAG: [[ZERO2:%cst.*]] = arith.constant dense<0> : tensor +// CHECK-DAG: [[DIM0:%.*]] = "tf.ExpandDims"(%arg1, [[ZERO1]]) : (tensor, tensor) -> tensor<1xi32> +// CHECK-DAG: [[SHAPE:%.*]] = "tf.Concat"([[ZERO2]], [[DIM0]], %arg0) : (tensor, tensor<1xi32>, tensor<3xi32>) -> tensor<4xi32> +// CHECK-DAG: [[VALUES:%.*]] = arith.constant dense<0.000000e+00> : tensor +// CHECK: [[LIST:%.*]] = "tf.Fill"([[SHAPE]], [[VALUES]]) : (tensor<4xi32>, tensor) -> tensor +// CHECK: [[RESULT:%.*]] = "tf.Gather"([[LIST]], %arg2) <{validate_indices = true}> : (tensor, tensor) -> tensor +// CHECK: return [[RESULT]] : tensor +} + +// ----- + +// CHECK-LABEL: tensorlistReserveUnrankedElements +func.func @tensorlistReserveUnrankedElements(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor<*xf32> { + %0 = "tf.TensorListReserve"(%arg0, %arg1) : (tensor, tensor) -> tensor>> + %1 = "tf.TensorListGetItem"(%0, %arg2, %arg0) : (tensor>>, tensor, tensor) -> tensor<*xf32> + func.return %1 : tensor<*xf32> + +// CHECK: [[RESULT:%[0-9]+]] = "tf.Fill"{{.*}}(tensor, tensor) -> tensor<*xf32> +// CHECK: [[RESULT2:%[0-9]+]] = "tf.Gather"{{.*}}<{validate_indices = true}> : (tensor<*xf32>, tensor) -> tensor<*xf32> +// CHECK: return [[RESULT2]] : tensor<*xf32> +} + +// ----- + +// CHECK-LABEL: tensorlistReserveConstantUnknownElementShapeDim +func.func @tensorlistReserveConstantUnknownElementShapeDim(%arg0: tensor, %arg1: tensor) -> tensor { + %cst = arith.constant dense<[-1, 7]> : tensor<2xi32> + %0 = "tf.TensorListReserve"(%cst, %arg0) : (tensor<2xi32>, tensor) -> tensor>> + %1 = "tf.TensorListGetItem"(%0, %arg1, %cst) : (tensor>>, tensor, tensor<2xi32>) -> tensor + func.return %1 : tensor + +// CHECK-DAG: [[ZERO1:%cst.*]] = arith.constant dense<0> : tensor +// CHECK-DAG: [[ZERO2:%cst.*]] = arith.constant dense<0> : tensor +// CHECK-DAG: [[ELEMENT_SHAPE:%cst.*]] = arith.constant dense<[1, 7]> : tensor<2xi32> +// CHECK-DAG: [[DIM0:%.*]] = "tf.ExpandDims"(%arg0, [[ZERO1]]) : (tensor, tensor) -> tensor<1xi32> +// CHECK-DAG: [[SHAPE:%.*]] = "tf.Concat"([[ZERO2]], [[DIM0]], [[ELEMENT_SHAPE]]) : (tensor, tensor<1xi32>, tensor<2xi32>) -> tensor<3xi32> +// CHECK-DAG: [[VALUES:%.*]] = arith.constant dense<0.000000e+00> : tensor +// CHECK: [[LIST:%.*]] = "tf.Fill"([[SHAPE]], [[VALUES]]) : (tensor<3xi32>, tensor) -> tensor +// CHECK: [[RESULT:%.*]] = "tf.Gather"([[LIST]], %arg1) <{validate_indices = true}> : (tensor, tensor) -> tensor +// CHECK: return [[RESULT]] : tensor +} + +// ----- + +// CHECK-LABEL: tensorlistReserveUnknownElementShape +func.func @tensorlistReserveUnknownElementShape(%arg0: tensor, %arg1: tensor, %arg2: tensor, %arg3: tensor<2xf32>) -> tensor<*xf32> { + %0 = "tf.TensorListReserve"(%arg0, %arg1) : (tensor, tensor) -> tensor>> + %1 = "tf.TensorListSetItem"(%0, %arg2, %arg3) : (tensor>>, tensor, tensor<2xf32>) -> tensor>> + %cst = arith.constant dense<-1> : tensor + %2 = "tf.TensorListStack"(%1, %cst) : (tensor>>, tensor) -> tensor<*xf32> + func.return %2 : tensor<*xf32> + +// CHECK-DAG: [[SHAPE:%[0-9]+]] = "tf.Shape"(%arg3) : (tensor<2xf32>) -> tensor +// CHECK-DAG: [[CST:%.*]] = arith.constant dense<0> : tensor +// CHECK-DAG: [[EXPAND_DIM:%[0-9]+]] = "tf.ExpandDims"(%arg1, [[CST]]) : (tensor, tensor) -> tensor<1xi32> +// CHECK-DAG: [[CST0:%.*]] = arith.constant dense<0> : tensor +// CHECK-DAG: [[FINAL_SHAPE:%[0-9]+]] = "tf.Concat"([[CST0]], [[EXPAND_DIM]], [[SHAPE]]) : (tensor, tensor<1xi32>, tensor) -> tensor +// CHECK-DAG: [[CST1:%.*]] = arith.constant dense<0.000000e+00> : tensor +// CHECK: [[FILL:%[0-9]+]] = "tf.Fill"([[FINAL_SHAPE]], [[CST1]]) : (tensor, tensor) -> tensor<*xf32> +} + +// ----- + +// CHECK-LABEL: tensorlistReserveUnrankedElementShape +func.func @tensorlistReserveUnrankedElementShape(%arg0: tensor<*xi32>, %arg1: tensor, %arg2: tensor) -> tensor<*xf32> { + %0 = "tf.TensorListReserve"(%arg0, %arg1) : (tensor<*xi32>, tensor) -> tensor>> + %1 = "tf.TensorListGetItem"(%0, %arg2, %arg0) : (tensor>>, tensor, tensor<*xi32>) -> tensor<*xf32> + func.return %1 : tensor<*xf32> + +// CHECK-DAG: [[AXIS:%.*]] = arith.constant dense<0> : tensor +// CHECK-DAG: [[EXPAND_DIM:%[0-9]+]] = "tf.ExpandDims"(%arg1, [[AXIS]]) : (tensor, tensor) -> tensor<1xi32> +// CHECK-DAG: [[AXIS_1:%.*]] = arith.constant dense<0> : tensor +// CHECK-DAG: [[CONCAT:%.*]] = "tf.Concat"([[AXIS_1]], [[EXPAND_DIM]], %arg0) : (tensor, tensor<1xi32>, tensor<*xi32>) -> tensor +// CHECK: [[CST:%.*]] = arith.constant dense<0.000000e+00> : tensor +// CHECK: [[FILL:%.*]] = "tf.Fill"([[CONCAT]], [[CST]]) : (tensor, tensor) -> tensor<*xf32> +// CHECK: [[GATHER:%.*]] = "tf.Gather"([[FILL]], %arg2) <{validate_indices = true}> : (tensor<*xf32>, tensor) -> tensor<*xf32> +// CHECK: return [[GATHER]] : tensor<*xf32> +} + +// ----- + +func.func @EmptyTensorList(%arg0: tensor<3xi32>, %arg1: tensor, %arg2: tensor) -> tensor { + %0 = "tf.EmptyTensorList"(%arg0, %arg1) : (tensor<3xi32>, tensor) -> tensor>> + %1 = "tf.TensorListGetItem"(%0, %arg2, %arg0) : (tensor>>, tensor, tensor<3xi32>) -> tensor + func.return %1 : tensor + +// CHECK-LABEL: EmptyTensorList +// CHECK-SAME: ([[ELEM_SHAPE:%.*]]: tensor<3xi32>, [[MAX_ELEMS:%.*]]: tensor, [[IDX:%.*]]: tensor) +// CHECK-DAG: [[DIM0:%cst.*]] = arith.constant dense<0> : tensor<1xi32> +// CHECK-DAG: [[ZERO:%cst.*]] = arith.constant dense<0> : tensor +// CHECK-DAG: [[SHAPE:%.*]] = "tf.Concat"([[ZERO]], [[DIM0]], [[ELEM_SHAPE]]) : (tensor, tensor<1xi32>, tensor<3xi32>) -> tensor<4xi32> +// CHECK-DAG: [[VALUES:%.*]] = arith.constant dense<0.000000e+00> : tensor +// CHECK: [[LIST:%.*]] = "tf.Fill"([[SHAPE]], [[VALUES]]) : (tensor<4xi32>, tensor) -> tensor<0x?x?x?xf32> +// CHECK: [[RESULT:%.*]] = "tf.Gather"([[LIST]], [[IDX]]) <{validate_indices = true}> : (tensor<0x?x?x?xf32>, tensor) -> tensor +// CHECK: return [[RESULT]] : tensor +} + +// ----- + +func.func @tensorlistPushBack(%arg0: tensor<3x10xf32>, %arg1: tensor<1xi32>, %arg2: tensor<10xf32>) -> tensor { + %0 = "tf.TensorListFromTensor"(%arg0, %arg1) : (tensor<3x10xf32>, tensor<1xi32>) -> tensor>> + %1 = "tf.TensorListPushBack"(%0, %arg2) : (tensor>>, tensor<10xf32>) -> tensor>> + %2 = "tf.TensorListStack"(%1, %arg1) : (tensor>>, tensor<1xi32>) -> tensor + func.return %2 : tensor + +// CHECK-LABEL: tensorlistPushBack +// CHECK-SAME: ([[INPUT:%.*]]: tensor<3x10xf32>, [[ELEM_SHAPE:%.*]]: tensor<1xi32>, [[ITEM:%.*]]: tensor<10xf32>) +// CHECK: [[ZERO:%.*]] = arith.constant dense<0> : tensor +// CHECK: [[EXP_ITEM:%.*]] = "tf.ExpandDims"([[ITEM]], [[ZERO]]) {{.*}} -> tensor<1x10xf32> +// CHECK: [[RESULT:%.*]] = "tf.Concat"(%cst, [[INPUT]], [[EXP_ITEM]]) : {{.*}} -> tensor +// CHECK: return [[RESULT]] : tensor +} + +// ----- + +func.func @tensorlistLength(%arg0: tensor<3x10xf32>, %arg1: tensor<1xi32>) -> (tensor) { + %0 = "tf.TensorListFromTensor"(%arg0, %arg1) : (tensor<3x10xf32>, tensor<1xi32>) -> tensor>> + %1 = "tf.TensorListLength"(%0) : (tensor>>) -> tensor + func.return %1: tensor + +// CHECK-LABEL: tensorlistLength +// CHECK-SAME: ([[INPUT:%.*]]: tensor<3x10xf32>, [[ELEM_SHAPE:%.*]]: tensor<1xi32>) +// CHECK-DAG: [[SHAPE:%.*]] = "tf.Shape"([[INPUT]]) {{.*}} -> tensor<2xi32> +// CHECK-DAG: [[ZERO:%cst.*]] = arith.constant dense<0> : tensor +// CHECK: [[RESULT:%.*]] = "tf.Gather"([[SHAPE]], [[ZERO]]) <{validate_indices = true}> : (tensor<2xi32>, tensor) -> tensor +// CHECK: return [[RESULT]] : tensor +} + +// ----- + +func.func @tensorlistWhileLoop(%arg0: tensor<2x3xf32>) -> tensor<2x3xf32> { + %cst = arith.constant dense<3> : tensor<1xi32> + %cst_0 = arith.constant dense<0> : tensor + %cst_1 = arith.constant dense<-1> : tensor + %0 = "tf.TensorListFromTensor"(%arg0, %cst) : (tensor<2x3xf32>, tensor<1xi32>) -> tensor>> + %1:2 = "tf.While"(%cst_0, %0) {T = ["tfdtype$DT_INT32", "tfdtype$DT_VARIANT"], body = @tensorlistWhileBody, cond = @tensorlistWhileCond, is_stateless = false} : (tensor, tensor>>) -> (tensor, tensor>>) + %2 = "tf.TensorListStack"(%1#1, %cst_1) : (tensor>>, tensor) -> tensor<2x3xf32> + func.return %2 : tensor<2x3xf32> + +// make sure the variant types in input/output have been updated, and `T` attribute +// is removed. +// CHECK-LABEL: func @tensorlistWhileLoop +// CHECK-NOT: "tf.While"{{.*}}T = +// CHECK: "tf.While" +// CHECK-SAME: (tensor, tensor<2x3xf32>) -> (tensor, tensor<2x3xf32>) +// CHECK: return %0#1 : tensor<2x3xf32> +} + +func.func @tensorlistWhileBody(%arg0: tensor, %arg1: tensor) -> (tensor, tensor) { + %0 = "tf.TensorListLength"(%arg1) : (tensor) -> tensor + %1 = "tf.Identity"(%arg1) : (tensor) -> tensor + func.return %0, %1 : tensor, tensor + +// verify `body` function's signature. +// CHECK: func @tensorlistWhileBody(%[[ARG0:.*]]: tensor, %[[ARG:.*]]: tensor<2x3xf32>) -> (tensor, tensor<2x3xf32>) +// CHECK-NOT: tensor +// CHECK: %[[LEN:.*]] = "tf.Gather" +// CHECK-NOT: tensor +// CHECK: %[[LIST:.*]] = "tf.Identity"(%arg1) : (tensor<2x3xf32>) -> tensor<2x3xf32> +// CHECK: return %[[LEN]], %[[LIST]] : tensor, tensor<2x3xf32> +} + +func.func @tensorlistWhileCond(%arg0: tensor, %arg1: tensor) -> tensor { + %cst = arith.constant dense<2> : tensor + %0 = "tf.Less"(%arg0, %cst) : (tensor, tensor) -> tensor + func.return %0 : tensor + +// verify `cond` function's signature. +// CHECK: func @tensorlistWhileCond(%[[ARG0:.*]]: tensor, %[[ARG1:.*]]: tensor<2x3xf32>) -> tensor +// CHECK: %[[RESULT:.*]] = "tf.Less"(%[[ARG0]], {{.*}}) : (tensor, tensor) -> tensor +// CHECK: return %[[RESULT]] : tensor +} + +// ----- + +// CHECK-LABEL: func @tensorlistWhileRegion +func.func @tensorlistWhileRegion(%arg0: tensor<2x3xf32>) -> tensor<*xf32> { + %cst = arith.constant dense<3> : tensor<1xi32> + %cst_0 = arith.constant dense<0> : tensor + %cst_1 = arith.constant dense<-1> : tensor + %0 = "tf.TensorListFromTensor"(%arg0, %cst) : (tensor<2x3xf32>, tensor<1xi32>) -> tensor>> + // CHECK: "tf.WhileRegion" + // CHECK: <{is_stateless = false}> + %1:2 = "tf.WhileRegion"(%cst_0, %0) <{is_stateless = false}> ({ + ^bb0(%carg0: tensor, %carg1: tensor): + %cst_2 = arith.constant dense<2> : tensor + %1 = "tf.Less"(%carg0, %cst_2) : (tensor, tensor) -> tensor + "tf.Yield"(%1) : (tensor) -> () + + // verify condition types + // CHECK: ^bb0(%[[CARG0:.*]]: tensor, %[[CARG1:.*]]: tensor<*xf32>): + // CHECK: %[[COND:.*]] = "tf.Less"(%[[CARG0]], {{.*}}) : (tensor, tensor) -> tensor + // CHECK: "tf.Yield"(%[[COND]]) : (tensor) -> () + + }, + { + ^bb0(%barg0: tensor, %barg1: tensor): + %1 = "tf.TensorListLength"(%barg1) : (tensor) -> tensor + "tf.Yield"(%1, %barg1) : (tensor, tensor) -> () + + // verify body types + // CHECK: ^bb0(%[[BARG0:.*]]: tensor, %[[BARG1:.*]]: tensor<*xf32>): + // CHECK-NOT: tensor + // CHECK: %[[LEN:.*]] = "tf.Gather" + // CHECK-NOT: tensor + // CHECK: "tf.Yield"(%[[LEN]], %[[BARG1]]) : (tensor, tensor<*xf32>) -> () + + }) : (tensor, tensor>>) -> (tensor, tensor>>) + // make sure the variant types in input/output have been updated + // : (tensor, tensor<2x3xf32>) -> (tensor, tensor<*xf32>) + %2 = "tf.TensorListStack"(%1#1, %cst_1) : (tensor>>, tensor) -> tensor<*xf32> + // CHECK: return %0#1 : tensor<*xf32> + func.return %2 : tensor<*xf32> +} + +// ----- + +func.func @otherVariantWhileLoop(%arg0: tensor<1xi32>) -> tensor<1xi32> { + %0 = "tf.Const"() {value = dense<0> : tensor} : () -> tensor + %1 = "tf.Const"() {value = dense<-1> : tensor} : () -> tensor + %2 = "tf.EmptyTensorMap"() {device = ""} : () -> tensor + %3:4 = "tf.While"(%0, %1, %0, %2) {_lower_using_switch_merge = true, _num_original_outputs = 4 : i64, _read_only_resource_inputs = [], body = @otherVariantWhileBody, cond = @otherVariantWhileCond, device = "", is_stateless = true, parallel_iterations = 10 : i64, shape_invariant} : (tensor, tensor, tensor, tensor) -> (tensor, tensor, tensor, tensor) + %4 = "tf.Identity"(%3#3) {device = ""} : (tensor) -> tensor + %5 = "tf.TensorMapSize"(%4) {device = ""} : (tensor) -> tensor + %6 = "tf.AddV2"(%arg0, %5) {device = ""} : (tensor<1xi32>, tensor) -> tensor<1xi32> + func.return %6 : tensor<1xi32> +} + +// Make sure the non TensorList variant types in input/output have remained. +// CHECK-LABEL: otherVariantWhileLoop +// CHECK: "tf.While" +// CHECK-SAME: (tensor, tensor, tensor, tensor) -> (tensor, tensor, tensor, tensor) + +func.func @otherVariantWhileBody(%arg0: tensor, %arg1: tensor, %arg2: tensor, %arg3: tensor) -> (tensor, tensor, tensor, tensor) { + %0 = "tf.Const"() {value = dense<1> : tensor} : () -> tensor + %1 = "tf.AddV2"(%arg2, %0) {device = ""} : (tensor, tensor) -> tensor + %2 = "tf.TensorMapInsert"(%arg3, %arg2, %arg2) {device = "", key_dtype = i32, value_dtype = i32} : (tensor, tensor, tensor) -> tensor + %3 = "tf.AddV2"(%arg0, %0) {device = ""} : (tensor, tensor) -> tensor + func.return %3, %arg1, %1, %2 : tensor, tensor, tensor, tensor +} + +// Verify `body` function's signature. +// CHECK-LABEL: func @otherVariantWhileBody +// CHECK: [[CST:%.*]] = "tf.Const"() +// CHECK-NEXT: [[ADD:%.*]] = "tf.AddV2"(%arg2, [[CST]]) +// CHECK-NEXT: [[TENSOR_MAP_INSERT_RESULT:%.*]] = "tf.TensorMapInsert"(%arg3, %arg2, %arg2) +// CHECK-NEXT: [[ADD_2:%.*]] = "tf.AddV2"(%arg0, [[CST]]) +// CHECK-NEXT: return [[ADD_2]], %arg1, [[ADD]], [[TENSOR_MAP_INSERT_RESULT]] + +func.func @otherVariantWhileCond(%arg0: tensor, %arg1: tensor, %arg2: tensor, %arg3: tensor) -> tensor { + %0 = "tf.Const"() {value = dense<10> : tensor} : () -> tensor + %1 = "tf.Less"(%arg2, %0) {device = ""} : (tensor, tensor) -> tensor + func.return %1 : tensor +} + +// Verify `cond` function's signature. +// CHECK-LABEL: func @otherVariantWhileCond +// CHECK: [[CST:%.*]] = "tf.Const"() +// CHECK-NEXT: [[LESS:%.*]] = "tf.Less"(%arg2, [[CST]]) +// CHECK-NEXT: return [[LESS]] + +// ----- + +func.func @tensorlistResize(%arg0: tensor<3x10xf32>, %arg1: tensor<1xi32>, %arg2: tensor) -> tensor { + %0 = "tf.TensorListFromTensor"(%arg0, %arg1) : (tensor<3x10xf32>, tensor<1xi32>) -> tensor>> + %1 = "tf.TensorListResize"(%0, %arg2) : (tensor>>, tensor) -> tensor>> + %2 = "tf.TensorListStack"(%1, %arg1) : (tensor>>, tensor<1xi32>) -> tensor + func.return %2: tensor + +// CHECK-LABEL: tensorlistResize +// CHECK-SAME: ([[INPUT:%.*]]: tensor<3x10xf32>, [[ELEM_SHAPE:%.*]]: tensor<1xi32>, [[SIZE:%.*]]: tensor) +// CHECK: [[ZERO:%.*]] = arith.constant dense<0> : tensor +// CHECK: [[SHAPE:%.*]] = "tf.Shape"([[INPUT]]) : (tensor<3x10xf32>) -> tensor<2xi32> +// CHECK: [[ZERO_1:%.*]] = arith.constant dense<0> : tensor +// CHECK: [[INPUT_SIZE:%.*]] = "tf.Gather"([[SHAPE]], [[ZERO_1]]) <{validate_indices = true}> : (tensor<2xi32>, tensor) -> tensor +// CHECK: [[SIZE_DIFF:%.*]] = "tf.Sub"([[SIZE]], [[INPUT_SIZE]]) : (tensor, tensor) -> tensor +// CHECK: [[DIFF_RES:%.*]] = "tf.Greater"([[SIZE_DIFF]], [[ZERO]]) : (tensor, tensor) -> tensor +// CHECK: [[SHAPE_1:%.*]] = "tf.Shape"([[INPUT]]) : (tensor<3x10xf32>) -> tensor +// CHECK: [[RESULT:%.*]] = "tf.If"([[DIFF_RES]], [[INPUT]], [[SHAPE_1]], [[SIZE_DIFF]], [[SIZE]]) <{else_branch = @cond_false, is_stateless = true, then_branch = @cond_true}> : (tensor, tensor<3x10xf32>, tensor, tensor, tensor) -> tensor +// CHECK: return [[RESULT]] : tensor +} + +// CHECK-LABEL: func private @cond_true +// CHECK-SAME: ([[INPUT:%.*]]: tensor<3x10xf32>, [[SHAPE:%.*]]: tensor, [[SIZE_DIFF:%.*]]: tensor, [[SIZE:%.*]]: tensor) +// CHECK-NEXT: [[NEG_ONE:%.*]] = arith.constant dense<-1> : tensor<1xi32> +// CHECK-NEXT: [[ZERO:%.*]] = arith.constant dense<0> : tensor +// CHECK-NEXT: [[ONE:%.*]] = arith.constant dense<1> : tensor<1xi32> +// CHECK-NEXT: [[ELEM_SHAPE:%.*]] = "tf.Slice"([[SHAPE]], [[ONE]], [[NEG_ONE]]) : (tensor, tensor<1xi32>, tensor<1xi32>) -> tensor +// CHECK-NEXT: [[ZERO_1:%.*]] = arith.constant dense<0> : tensor +// CHECK-NEXT: [[EXPANDED_SIZE_DIFF:%.*]] = "tf.ExpandDims"([[SIZE_DIFF]], [[ZERO_1]]) : (tensor, tensor) -> tensor<1xi32> +// CHECK-NEXT: [[ZERO_2:%.*]] = arith.constant dense<0> : tensor +// CHECK-NEXT: [[EXTENDED_SHAPE:%.*]] = "tf.Concat"([[ZERO_2]], [[EXPANDED_SIZE_DIFF]], [[ELEM_SHAPE]]) : (tensor, tensor<1xi32>, tensor) -> tensor<2xi32> +// CHECK-NEXT: [[ZERO_FLOAT:%.*]] = arith.constant dense<0.000000e+00> : tensor +// CHECK-NEXT: [[EXTENDED_PART:%.*]] = "tf.Fill"([[EXTENDED_SHAPE]], [[ZERO_FLOAT]]) : (tensor<2xi32>, tensor) -> tensor +// CHECK-NEXT: [[NEG_ONE_1:%.*]] = arith.constant dense<-1> : tensor +// CHECK-NEXT: [[RESULT:%.*]] = "tf.Concat"([[ZERO]], [[INPUT]], [[EXTENDED_PART]]) : (tensor, tensor<3x10xf32>, tensor) -> tensor +// CHECK-NEXT: return [[RESULT]] : tensor + + +// CHECK-LABEL: func private @cond_false +// CHECK-SAME: ([[INPUT:%.*]]: tensor<3x10xf32>, [[SHAPE:%.*]]: tensor, [[SIZE_DIFF:%.*]]: tensor, [[SIZE:%.*]]: tensor) +// CHECK-DAG: [[ZERO:%.*]] = arith.constant dense<0> : tensor +// CHECK-DAG: [[ONE:%.*]] = arith.constant dense<1> : tensor<1xi32> +// CHECK: [[RANK:%.*]] = "tf.Rank"([[INPUT]]) : (tensor<3x10xf32>) -> tensor +// CHECK: [[ELEM_RANK:%.*]] = "tf.Sub"([[RANK]], [[ONE]]) : (tensor, tensor<1xi32>) -> tensor<1xi32> +// CHECK: [[ZERO_1:%.*]] = arith.constant dense<0> : tensor +// CHECK: [[PARTIAL_POS:%.*]] = "tf.Fill"([[ELEM_RANK]], [[ZERO_1]]) : (tensor<1xi32>, tensor) -> tensor +// CHECK: [[ZERO_2:%.*]] = arith.constant dense<0> : tensor +// CHECK: [[START:%.*]] = "tf.ExpandDims"([[ZERO]], [[ZERO_2]]) : (tensor, tensor) -> tensor<1xi32> +// CHECK: [[SLICE_BEGIN:%.*]] = "tf.Concat"([[ZERO_2]], [[START]], [[PARTIAL_POS]]) : (tensor, tensor<1xi32>, tensor) -> tensor +// CHECK: [[SLICE_SIZE_HEAD:%.*]] = "tf.ExpandDims"([[SIZE]], [[ZERO_2]]) : (tensor, tensor) -> tensor<1xi32> +// CHECK: [[NEG_ONE:%.*]] = arith.constant dense<-1> : tensor +// CHECK: [[SLICE_SIZE_TAIL:%.*]] = "tf.Fill"([[ELEM_RANK]], [[NEG_ONE]]) : (tensor<1xi32>, tensor) -> tensor +// CHECK: [[SLICE_SIZE:%.*]] = "tf.Concat"([[ZERO_2]], [[SLICE_SIZE_HEAD]], [[SLICE_SIZE_TAIL]]) : (tensor, tensor<1xi32>, tensor) -> tensor +// CHECK: [[RESULT:%.*]] = "tf.Slice"([[INPUT]], [[SLICE_BEGIN]], [[SLICE_SIZE]]) : (tensor<3x10xf32>, tensor, tensor) -> tensor +// CHECK: return [[RESULT]] : tensor +// CHECK: } + +// ----- + +// CHECK-LABEL: tensorlistReserveWithDynamicShape +func.func @tensorlistReserveWithDynamicShape(%arg0: tensor, %arg1: tensor, %arg2: tensor) -> tensor { + %0 = "tf.TensorListReserve"(%arg0, %arg1) : (tensor, tensor) -> tensor>> + %1 = "tf.TensorListGetItem"(%0, %arg2, %arg0) : (tensor>>, tensor, tensor) -> tensor + func.return %1 : tensor + +// CHECK: %0 = "tf.TensorListReserve"(%arg0, %arg1) : (tensor, tensor) -> tensor>> +// CHECK: %1 = "tf.TensorListGetItem"(%0, %arg2, %arg0) : (tensor>>, tensor, tensor) -> tensor +// CHECK: return %1 : tensor +} + +// ----- + +// CHECK-LABEL: tensorlistConcat +func.func @tensorlistConcat(%arg0: tensor<3x2x2xf32>, %lead: tensor) -> (tensor, tensor<0xi64>) { + %cst = arith.constant dense<[2, 2]> : tensor<2xi32> + %list = "tf.TensorListFromTensor"(%arg0, %cst) : (tensor<3x2x2xf32>, tensor<2xi32>) -> tensor>> + %t:2 = "tf.TensorListConcatV2"(%list, %cst, %lead) : (tensor>>, tensor<2xi32>, tensor) -> (tensor, tensor<0xi64>) + func.return %t#0, %t#1 : tensor, tensor<0xi64> + +// CHECK: [[ELEMENT_SHAPE:%.*]] = arith.constant dense<2> : tensor<2xi32> +// CHECK: [[UNPACK:%.*]]:3 = "tf.Unpack"(%arg0) <{axis = 0 : i64}> : (tensor<3x2x2xf32>) -> (tensor<2x2xf32>, tensor<2x2xf32>, tensor<2x2xf32>) +// CHECK: [[SCALAR_ZERO:%.*]] = arith.constant dense<0> : tensor +// CHECK: [[CONCAT:%.*]] = "tf.Concat"([[SCALAR_ZERO]], [[UNPACK]]#0, [[UNPACK]]#1, [[UNPACK]]#2) : (tensor, tensor<2x2xf32>, tensor<2x2xf32>, tensor<2x2xf32>) -> tensor +// CHECK: [[LENGTHS:%.*]] = arith.constant dense<0> : tensor<0xi64> +// CHECK: return [[CONCAT]], [[LENGTHS]] : tensor, tensor<0xi64> +} + +// ----- + +func.func @whileLoopWithDynamicTensorList(%arg0: tensor, %arg1: tensor) -> tensor<*xf32> { + %cst = arith.constant dense<3> : tensor<1xi32> + %cst_0 = arith.constant dense<0> : tensor + %cst_1 = arith.constant dense<-1> : tensor + %0 = "tf.TensorListReserve"(%arg0, %arg1) : (tensor, tensor) -> tensor>> + %1:2 = "tf.While"(%cst_0, %0) {T = ["tfdtype$DT_INT32", "tfdtype$DT_VARIANT"], body = @tensorlistWhileBody, cond = @tensorlistWhileCond, is_stateless = false} : (tensor, tensor>>) -> (tensor, tensor>>) + %2 = "tf.TensorListStack"(%1#1, %cst_1) : (tensor>>, tensor) -> tensor<*xf32> + func.return %2 : tensor<*xf32> + +// verify tensorlist ops pass through. +// CHECK-LABEL: func @whileLoopWithDynamicTensorList +// CHECK: "tf.TensorListReserve" +// CHECK: "tf.While" +// CHECK-SAME: (tensor, tensor>>) -> (tensor, tensor>>) +// CHECK: "tf.TensorListStack" +} + +func.func @tensorlistWhileBody(%arg0: tensor, %arg1: tensor) -> (tensor, tensor) { + %0 = "tf.TensorListLength"(%arg1) : (tensor) -> tensor + %1 = "tf.Identity"(%arg1) : (tensor) -> tensor + func.return %0, %1 : tensor, tensor + +// verify `body` function's signature stays unchanged. +// CHECK: func @tensorlistWhileBody(%[[ARG0:.*]]: tensor, %[[ARG:.*]]: tensor) -> (tensor, tensor) +} + +func.func @tensorlistWhileCond(%arg0: tensor, %arg1: tensor) -> tensor { + %cst = arith.constant dense<2> : tensor + %0 = "tf.Less"(%arg0, %cst) : (tensor, tensor) -> tensor + func.return %0 : tensor + +// verify `cond` function's signature stays unchanged. +// CHECK: func @tensorlistWhileCond(%[[ARG0:.*]]: tensor, %[[ARG1:.*]]: tensor) -> tensor +} + +// ----- + +func.func @tensorListIf(%arg0: tensor<3x10xf32>, %arg1: tensor<1xi32>, %arg2: tensor) -> tensor<3x10xf32> { + %cst = arith.constant dense<2> : tensor + %0 = "tf.TensorListFromTensor"(%arg0, %arg1) : (tensor<3x10xf32>, tensor<1xi32>) -> tensor>> + %1 = "tf.Less"(%arg2, %cst) : (tensor, tensor) -> tensor + %res = "tf.If"(%1, %0) {else_branch = @tensorListIfCondFalse, is_stateless = true, then_branch = @tensorListIfCondTrue} : (tensor, tensor>>) -> tensor>> + %2 = "tf.TensorListStack"(%res, %arg1) : (tensor>>, tensor<1xi32>) -> tensor<3x10xf32> + func.return %2 : tensor<3x10xf32> + +// CHECK: func @tensorListIf +// CHECK-NEXT: %cst = arith.constant dense<2> : tensor +// CHECK-NEXT: %0 = "tf.Less"(%arg2, %cst) : (tensor, tensor) -> tensor +// CHECK-NEXT: %1 = "tf.If"(%0, %arg0) <{else_branch = @tensorListIfCondFalse, is_stateless = true, then_branch = @tensorListIfCondTrue}> : (tensor, tensor<3x10xf32>) -> tensor<3x10xf32> +// CHECK-NEXT: return %1 : tensor<3x10xf32> +} + +func.func @tensorListIfCondTrue(%arg0: tensor>>) -> (tensor>>) { + %cst = arith.constant dense<0> : tensor + %cst_0 = arith.constant dense<0.0> : tensor<10xf32> + %0 = "tf.TensorListSetItem"(%arg0, %cst, %cst_0) : (tensor>>, tensor, tensor<10xf32>) -> tensor>> + func.return %0 : tensor>> + +// CHECK: func @tensorListIfCondTrue(%arg0: tensor<3x10xf32>) -> tensor<3x10xf32> +// CHECK-NOT: tf.TensorListSetItem +} + +func.func @tensorListIfCondFalse(%arg0: tensor>>) -> (tensor>>) { + %cst = arith.constant dense<0> : tensor + %cst_0 = arith.constant dense<1.0> : tensor<10xf32> + %0 = "tf.TensorListSetItem"(%arg0, %cst, %cst_0) : (tensor>>, tensor, tensor<10xf32>) -> tensor>> + func.return %0 : tensor>> + +// CHECK: func @tensorListIfCondFalse(%arg0: tensor<3x10xf32>) -> tensor<3x10xf32> +// CHECK-NOT: tf.TensorListSetItem +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/lower_quant_annotations.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/lower_quant_annotations.mlir new file mode 100644 index 0000000000000000000000000000000000000000..80c574e4c72a6a681953bef97ca6399826a43790 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/lower_quant_annotations.mlir @@ -0,0 +1,51 @@ +// RUN: litert-opt %s --tfl-lower-quant-annotations | FileCheck %s + +func.func private @XlaCallModule_quant.fake_quant.impl_0(tensor<1x28x28x3xf32>) -> tensor<1x28x28x3xf32> +func.func private @XlaCallModule_quant.fake_quant.impl_5_0(tensor<2x1x1x1xf32>) -> tensor<2x1x1x1xf32> +func.func private @XlaCallModule_quant.fake_quant.impl_17_0(tensor<1x30x30x2xf32>) -> tensor<1x30x30x2xf32> +func.func private @XlaCallModule_quant.fake_quant.impl_i2_0(tensor<1x4xf32>) -> tensor<1x4xf32> +func.func private @XlaCallModule_quant.fake_quant.impl_i2_1(tensor<1x4xf32>) -> tensor<1x4xf32> +func.func private @XlaCallModule_quant.dequantize.impl_0(tensor<1x4xf32>) -> tensor<1x4xf32> + +// CHECK-LABEL: func.func @serving_default +func.func @serving_default(%arg0: tensor<1x28x28x3xf32>) -> (tensor<1x30x30x2xf32>) { + %cst = arith.constant dense<[[0, 0], [1, 1], [1, 1], [0, 0]]> : tensor<4x2xi32> + %cst_0 = arith.constant dense<[0, 2, 3, 1]> : tensor<4xi32> + %cst_1 = arith.constant dense<0.000000e+00> : tensor<2xf32> + %cst_2 = arith.constant dense<[[[[1.0]]], [[[2.0]]]]> : tensor<2x1x1x1xf32> + // CHECK: %[[QUANT0:.+]] = "tfl.quantize"(%arg0) <{qtype = tensor<1x28x28x3x!quant.uniform>}> : (tensor<1x28x28x3xf32>) -> tensor<1x28x28x3x!quant.uniform> + // CHECK: %[[DEQUANT0:.+]] = "tfl.dequantize"(%[[QUANT0]]) : (tensor<1x28x28x3x!quant.uniform>) -> tensor<1x28x28x3xf32> + %0 = stablehlo.composite "quant.fake_quant" %arg0 {composite_attributes = {dtype = "i8", narrow_range = false, scale = dense<0.00391972717> : tensor<1xf32>, zero_point = dense<-128> : tensor<1xi32>}, decomposition = @XlaCallModule_quant.fake_quant.impl_0} : (tensor<1x28x28x3xf32>) -> tensor<1x28x28x3xf32> + // CHECK: %[[QUANT1:.+]] = "tfl.quantize"(%{{.+}}) <{qtype = tensor<2x1x1x1x!quant.uniform:f32:0, {0.0058756377547979355,0.0049431771039962769}>>}> : (tensor<2x1x1x1xf32>) -> tensor<2x1x1x1x!quant.uniform:f32:0, {0.0058756377547979355,0.0049431771039962769}>> + // CHECK: %[[DEQUANT1:.+]] = "tfl.dequantize"(%[[QUANT1]]) : (tensor<2x1x1x1x!quant.uniform:f32:0, {0.0058756377547979355,0.0049431771039962769}>>) -> tensor<2x1x1x1xf32> + %1 = stablehlo.composite "quant.fake_quant" %cst_2 {composite_attributes = {dtype = "i8", narrow_range = true, quantization_dimension = 0 : i32, scale = dense<[0.00587563775, 0.0049431771]> : tensor<2xf32>}, decomposition = @XlaCallModule_quant.fake_quant.impl_5_0} : (tensor<2x1x1x1xf32>) -> tensor<2x1x1x1xf32> + %2 = "tfl.transpose"(%1, %cst_0) : (tensor<2x1x1x1xf32>, tensor<4xi32>) -> tensor<2x1x1x1xf32> + %3 = "tfl.pad"(%0, %cst) : (tensor<1x28x28x3xf32>, tensor<4x2xi32>) -> tensor<1x30x30x3xf32> + %4 = "tfl.conv_2d"(%3, %2, %cst_1) <{dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 1 : i32, stride_w = 1 : i32}> : (tensor<1x30x30x3xf32>, tensor<2x1x1x1xf32>, tensor<2xf32>) -> tensor<1x30x30x2xf32> + // CHECK-OFF: %[[QUANT2:.+]] = "tfl.quantize"(%{{.+}}) <{qtype = tensor<1x30x30x2x!quant.uniform>}> : (tensor<1x30x30x2xf32>) -> tensor<1x30x30x2x!quant.uniform> + // CHECK-OFF: %[[DEQUANT2:.+]] = "tfl.dequantize"(%[[QUANT2]]) : (tensor<1x30x30x2x!quant.uniform>) -> tensor<1x30x30x2xf32> + %5 = stablehlo.composite "quant.fake_quant" %4 {composite_attributes = {dtype = "i8", narrow_range = false, scale = dense<0.0180494692> : tensor<1xf32>, zero_point = dense<8> : tensor<1xi32>}, decomposition = @XlaCallModule_quant.fake_quant.impl_17_0} : (tensor<1x30x30x2xf32>) -> tensor<1x30x30x2xf32> + return %5 : tensor<1x30x30x2xf32> +} + +// CHECK-LABEL: func.func @i2_test +func.func @i2_test(%arg0: tensor<1x4xf32>) -> (tensor<1x4xf32>) { + // CHECK: %[[QUANT0:.+]] = "tfl.quantize"(%arg0) <{qtype = tensor<1x4x!quant.uniform>}> : (tensor<1x4xf32>) -> tensor<1x4x!quant.uniform> + // CHECK: %[[DEQUANT0:.+]] = "tfl.dequantize"(%[[QUANT0]]) : (tensor<1x4x!quant.uniform>) -> tensor<1x4xf32> + %0 = stablehlo.composite "quant.fake_quant" %arg0 {composite_attributes = {dtype = "i2", narrow_range = false, scale = dense<1.0> : tensor<1xf32>, zero_point = dense<0> : tensor<1xi32>}, decomposition = @XlaCallModule_quant.fake_quant.impl_i2_0} : (tensor<1x4xf32>) -> tensor<1x4xf32> + // CHECK: %[[QUANT1:.+]] = "tfl.quantize"(%[[DEQUANT0]]) <{qtype = tensor<1x4x!quant.uniform:f32:1, {1.000000e+00,2.000000e+00,3.000000e+00,4.000000e+00}>>}> : (tensor<1x4xf32>) -> tensor<1x4x!quant.uniform:f32:1, {1.000000e+00,2.000000e+00,3.000000e+00,4.000000e+00}>> + // CHECK: %[[DEQUANT1:.+]] = "tfl.dequantize"(%[[QUANT1]]) : (tensor<1x4x!quant.uniform:f32:1, {1.000000e+00,2.000000e+00,3.000000e+00,4.000000e+00}>>) -> tensor<1x4xf32> + %1 = stablehlo.composite "quant.fake_quant" %0 {composite_attributes = {dtype = "i2", narrow_range = true, quantization_dimension = 1 : i32, scale = dense<[1.0, 2.0, 3.0, 4.0]> : tensor<4xf32>}, decomposition = @XlaCallModule_quant.fake_quant.impl_i2_1} : (tensor<1x4xf32>) -> tensor<1x4xf32> + return %1 : tensor<1x4xf32> +} + +// CHECK-LABEL: func.func @multi_result_test +func.func @multi_result_test(%arg0: tensor<1x4xf32>) -> (tensor<1x4xf32>, tensor<1x4xf32>) { + // A multi-result producer op. + %0:2 = "stablehlo.custom_call"(%arg0) {call_target_name = "test_op"} : (tensor<1x4xf32>) -> (tensor<1x4xf32>, tensor<1x4xf32>) + + // CHECK: %[[DEQUANT:.+]] = "tfl.dequantize"(%{{.+}}) : (tensor<1x4x!quant.uniform>) -> tensor<1x4xf32> + %1 = stablehlo.composite "quant.dequantize" %0#1 {composite_attributes = {dtype = "i8", narrow_range = false, scale = dense<1.0> : tensor<1xf32>, zero_point = dense<0> : tensor<1xi32>}, decomposition = @XlaCallModule_quant.dequantize.impl_0} : (tensor<1x4xf32>) -> tensor<1x4xf32> + + return %0#0, %1 : tensor<1x4xf32>, tensor<1x4xf32> +} \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/BUILD b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..e6a68d1228791db6a20f23034a90fdac39dce43c --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/BUILD @@ -0,0 +1,25 @@ +load("//tensorflow:tensorflow.default.bzl", "filegroup") +load("//tensorflow/compiler/mlir:glob_lit_test.bzl", "glob_lit_tests") + +# copybara:uncomment package(default_applicable_licenses = ["//tensorflow:LICENSE"]) + +licenses(["notice"]) + +glob_lit_tests( + name = "all_tests", + data = [":test_utilities"], + driver = "@llvm-project//mlir:run_lit.sh", + test_file_exts = ["mlir"], +) + +# Bundle together all of the test utilities that are used by tests. +filegroup( + name = "test_utilities", + testonly = True, + data = [ + "//tensorflow/compiler/mlir/lite:flatbuffer_to_string", + "//tensorflow/compiler/mlir/lite:flatbuffer_translate", + "@llvm-project//llvm:FileCheck", + "@llvm-project//llvm:not", + ], +) diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/basic_lstm.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/basic_lstm.mlir new file mode 100644 index 0000000000000000000000000000000000000000..584904cc237aea21b2c898dcc7d0305c11843e68 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/basic_lstm.mlir @@ -0,0 +1,135 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_to_string - | FileCheck %s + +func.func @main(tensor<1x384xf32>, tensor<1x96xf32>, tensor<384x480xf32>, tensor<384xf32>, tensor<1x96xf32>) -> tensor<1x96xf32> { +// CHECK: { +// CHECK-NEXT: version: 3, +// CHECK-NEXT: operator_codes: [ { +// CHECK-NEXT: deprecated_builtin_code: 16, +// CHECK-NEXT: version: 2 +// CHECK-NEXT: builtin_code: LSTM +// CHECK-NEXT: } ], +// CHECK-NEXT: subgraphs: [ { +// CHECK-NEXT: tensors: [ { +// CHECK-NEXT: shape: [ 1, 384 ], +// CHECK-NEXT: buffer: 1, +// CHECK-NEXT: name: "arg0", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ 1, 96 ], +// CHECK-NEXT: buffer: 2, +// CHECK-NEXT: name: "arg1", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ 384, 480 ], +// CHECK-NEXT: buffer: 3, +// CHECK-NEXT: name: "arg2", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ 384 ], +// CHECK-NEXT: buffer: 4, +// CHECK-NEXT: name: "arg3", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ 1, 96 ], +// CHECK-NEXT: buffer: 5, +// CHECK-NEXT: name: "arg4", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ 1, 96 ], +// CHECK-NEXT: buffer: 6, +// CHECK-NEXT: name: "tfl.basic_lstm", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ 1, 96 ], +// CHECK-NEXT: buffer: 7, +// CHECK-NEXT: name: "tfl.basic_lstm:1", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ 1, 480 ], +// CHECK-NEXT: buffer: 8, +// CHECK-NEXT: name: "tfl.basic_lstm:2", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ 1, 384 ], +// CHECK-NEXT: buffer: 9, +// CHECK-NEXT: name: "tfl.basic_lstm:3", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: } ], +// CHECK-NEXT: inputs: [ 0, 1, 2, 3, 4 ], +// CHECK-NEXT: outputs: [ 5 ], +// CHECK-NEXT: operators: [ { +// CHECK-NEXT: inputs: [ 0, 1, 2, 3, 4 ], +// CHECK-NEXT: outputs: [ 5, 6, 7, 8 ], +// CHECK-NEXT: builtin_options_type: LSTMOptions, +// CHECK-NEXT: builtin_options: { +// CHECK-NEXT: fused_activation_function: RELU, +// CHECK-NEXT: cell_clip: 1.0, +// CHECK-NEXT: proj_clip: 2.0, +// CHECK-NEXT: kernel_type: BASIC +// CHECK-NEXT: }, +// CHECK-NEXT: intermediates: [ ] +// CHECK-NEXT: } ], +// CHECK-NEXT: name: "main" +// CHECK-NEXT: } ], +// CHECK-NEXT: description: "MLIR Converted.", +// CHECK-NEXT: buffers: [ { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-NEXT: data: [ 49, 46, 49, 48, 46, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] +// CHECK-NEXT: } ], +// CHECK-NEXT: metadata: [ { +// CHECK-NEXT: name: "min_runtime_version", +// CHECK-NEXT: buffer: 10 +// CHECK-NEXT: } ] +// CHECK-NEXT: signature_defs: [ ] +// CHECK-NEXT:} + +^bb0(%arg0: tensor<1x384xf32>, %arg1: tensor<1x96xf32>, %arg2: tensor<384x480xf32>, %arg3: tensor<384xf32>, %arg4: tensor<1x96xf32>): + %0:4 = "tfl.basic_lstm"(%arg0, %arg1, %arg2, %arg3, %arg4) {fused_activation_function = "RELU", cell_clip = 1.0 : f32, proj_clip = 2.0 : f32} : (tensor<1x384xf32>, tensor<1x96xf32>, tensor<384x480xf32>, tensor<384xf32>, tensor<1x96xf32>) -> (tensor<1x96xf32>, tensor<1x96xf32>, tensor<1x480xf32>, tensor<1x384xf32>) + func.return %0#0 : tensor<1x96xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/bucketize.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/bucketize.mlir new file mode 100644 index 0000000000000000000000000000000000000000..da4d27f698d42c4f5512e5b139893623ca4cc1cb --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/bucketize.mlir @@ -0,0 +1,74 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_to_string - | FileCheck %s + +func.func @main(tensor<3x2xf32>) -> tensor<3x2xi32> { +^bb0(%arg0: tensor<3x2xf32>): + // CHECK: { + // CHECK-NEXT: version: 3, + // CHECK-NEXT: operator_codes: [ { + // CHECK-NEXT: deprecated_builtin_code: 127, + // CHECK-NEXT: version: 1, + // CHECK-NEXT: builtin_code: BUCKETIZE + // CHECK-NEXT: } ], + // CHECK-NEXT: subgraphs: [ { + // CHECK-NEXT: tensors: [ { + // CHECK-NEXT: shape: [ 3, 2 ], + // CHECK-NEXT: buffer: 1, + // CHECK-NEXT: name: "arg0", + // CHECK-NEXT: quantization: { + // CHECK-EMPTY: + // CHECK-NEXT: }, + // CHECK-NEXT: has_rank: true + // CHECK-NEXT: }, { + // CHECK-NEXT: shape: [ 3, 2 ], + // CHECK-NEXT: buffer: 2, + // CHECK-NEXT: name: "Const", + // CHECK-NEXT: quantization: { + // CHECK-EMPTY: + // CHECK-NEXT: }, + // CHECK-NEXT: has_rank: true + // CHECK-NEXT: }, { + // CHECK-NEXT: shape: [ 3, 2 ], + // CHECK-NEXT: type: INT32, + // CHECK-NEXT: buffer: 3, + // CHECK-NEXT: name: "bucketize", + // CHECK-NEXT: quantization: { + // CHECK-EMPTY: + // CHECK-NEXT: }, + // CHECK-NEXT: has_rank: true + // CHECK-NEXT: } ], + // CHECK-NEXT: inputs: [ 0 ], + // CHECK-NEXT: outputs: [ 2 ], + // CHECK-NEXT: operators: [ { + // CHECK-NEXT: inputs: [ 1 ], + // CHECK-NEXT: outputs: [ 2 ], + // CHECK-NEXT: builtin_options_type: BucketizeOptions, + // CHECK-NEXT: builtin_options: { + // CHECK-NEXT: boundaries: [ 0.0, 10.0, 100.0 ] + // CHECK-NEXT: } + // CHECK-NEXT: } ], + // CHECK-NEXT: name: "main" + // CHECK-NEXT: } ], + // CHECK-NEXT: description: "MLIR Converted.", + // CHECK-NEXT: buffers: [ { + // CHECK-EMPTY: + // CHECK-NEXT: }, { + // CHECK-EMPTY: + // CHECK-NEXT: }, { + // CHECK-NEXT: data: [ 0, 0, 160, 192, 0, 64, 28, 70, 0, 0, 22, 67, 0, 0, 32, 65, 0, 0, 160, 64, 0, 0, 200, 66 ] + // CHECK-NEXT: }, { + // CHECK-EMPTY: + // CHECK-NEXT: }, { + // CHECK-NEXT: data: [ 50, 46, 56, 46, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] + // CHECK-NEXT: } ], + // CHECK-NEXT: metadata: [ { + // CHECK-NEXT: name: "min_runtime_version", + // CHECK-NEXT: buffer: 4 + // CHECK-NEXT: } ], + // CHECK-NEXT: signature_defs: [ ] + // CHECK-NEXT: } + // CHECK-EMPTY: + + %0 = "tfl.pseudo_const" () {value = dense<[[-5.0, 10000.0], [150.0, 10.0], [5.0, 100.0]]> : tensor<3x2xf32>} : () -> tensor<3x2xf32> loc("Const") + %1 = "tfl.bucketize"(%0) {boundaries = [0.0 : f32, 10.0 : f32, 100.0 : f32]} : (tensor<3x2xf32>) -> tensor<3x2xi32> loc("bucketize") + func.return %1 : tensor<3x2xi32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/cast_bf16.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/cast_bf16.mlir new file mode 100644 index 0000000000000000000000000000000000000000..a666cd1c20cfd59d552fe7d981e5eb67d71fb305 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/cast_bf16.mlir @@ -0,0 +1,74 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -emit-custom-ops -o - | flatbuffer_to_string - | FileCheck %s + +func.func @main(tensor<4x5xbf16>) -> tensor<4x5xbf16> { +^bb0(%arg0: tensor<4x5xbf16>): + +// CHECK: { +// CHECK-NEXT: version: 3, +// CHECK-NEXT: operator_codes: [ { +// CHECK-NEXT: deprecated_builtin_code: 53, +// CHECK-NEXT: version: 7, +// CHECK-NEXT: builtin_code: CAST +// CHECK-NEXT: } ], +// CHECK-NEXT: subgraphs: [ { +// CHECK-NEXT: tensors: [ { +// CHECK-NEXT: shape: [ 4, 5 ], +// CHECK-NEXT: type: BFLOAT16, +// CHECK-NEXT: buffer: 1, +// CHECK-NEXT: name: "arg0", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ 4, 5 ], +// CHECK-NEXT: buffer: 2, +// CHECK-NEXT: name: "cast1", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ 4, 5 ], +// CHECK-NEXT: type: BFLOAT16, +// CHECK-NEXT: buffer: 3, +// CHECK-NEXT: name: "cast2", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: } ], +// CHECK-NEXT: inputs: [ 0 ], +// CHECK-NEXT: outputs: [ 2 ], +// CHECK-NEXT: operators: [ { +// CHECK-NEXT: inputs: [ 0 ], +// CHECK-NEXT: outputs: [ 1 ] +// CHECK-NEXT: }, { +// CHECK-NEXT: inputs: [ 1 ], +// CHECK-NEXT: outputs: [ 2 ] +// CHECK-NEXT: } ], +// CHECK-NEXT: name: "main" +// CHECK-NEXT: } ], +// CHECK-NEXT: description: "MLIR Converted.", +// CHECK-NEXT: buffers: [ { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-NEXT: data: [ 50, 46, 49, 55, 46, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] +// CHECK-NEXT: } ], +// CHECK-NEXT: metadata: [ { +// CHECK-NEXT: name: "min_runtime_version", +// CHECK-NEXT: buffer: 4 +// CHECK-NEXT: } ], +// CHECK-NEXT: signature_defs: [ ] +// CHECK-NEXT: } + + %0 = "tfl.cast" (%arg0) : (tensor<4x5xbf16>) -> tensor<4x5xf32> loc("cast1") + %1 = "tfl.cast" (%0) : (tensor<4x5xf32>) -> tensor<4x5xbf16> loc("cast2") + func.return %1 : tensor<4x5xbf16> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/composite.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/composite.mlir new file mode 100644 index 0000000000000000000000000000000000000000..5c1a4f963dffb709b99ca9697d53896f78941a1e --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/composite.mlir @@ -0,0 +1,314 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_to_string - | FileCheck %s + +// CHECK: { +// CHECK-NEXT: version: 3, +// CHECK-NEXT: operator_codes: [ { +// CHECK-NEXT: deprecated_builtin_code: 127, +// CHECK-NEXT: version: 1, +// CHECK-NEXT: builtin_code: STABLEHLO_COMPOSITE +// CHECK-NEXT: }, { +// CHECK-NEXT: version: 1 +// CHECK-NEXT: }, { +// CHECK-NEXT: deprecated_builtin_code: 41, +// CHECK-NEXT: version: 1, +// CHECK-NEXT: builtin_code: SUB +// CHECK-NEXT: } ], +// CHECK-NEXT: subgraphs: [ { +// CHECK-NEXT: tensors: [ { +// CHECK-NEXT: shape: [ 10 ], +// CHECK-NEXT: buffer: 1, +// CHECK-NEXT: name: "arg0", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ 10 ], +// CHECK-NEXT: buffer: 2, +// CHECK-NEXT: name: "arg1", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ ], +// CHECK-NEXT: buffer: 3, +// CHECK-NEXT: name: "arith.constant", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ ], +// CHECK-NEXT: buffer: 4, +// CHECK-NEXT: name: "arith.constant1", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ 10 ], +// CHECK-NEXT: buffer: 5, +// CHECK-NEXT: name: "vhlo.composite_v1", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ 10 ], +// CHECK-NEXT: buffer: 6, +// CHECK-NEXT: name: "vhlo.composite_v11", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ 10 ], +// CHECK-NEXT: buffer: 7, +// CHECK-NEXT: name: "tfl.add", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ 10 ], +// CHECK-NEXT: buffer: 8, +// CHECK-NEXT: name: "tfl.sub", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: } ], +// CHECK-NEXT: inputs: [ 0, 1 ], +// CHECK-NEXT: outputs: [ 7 ], +// CHECK-NEXT: operators: [ { +// CHECK-NEXT: inputs: [ 0, 1 ], +// CHECK-NEXT: outputs: [ 4 ], +// CHECK-NEXT: builtin_options_2_type: StableHLOCompositeOptions, +// CHECK-NEXT: builtin_options_2: { +// CHECK-NEXT: name: "test.TEST_COMPOSITE", +// CHECK-NEXT: decomposition_subgraph_index: 2, +// CHECK-NEXT: composite_attributes: [ 109, 121, 95, 97, 114, 114, 97, 121, 0, 1, 97, 0, 1, 98, 0, 2, 6, 4, 20, 20, 1, 21, 1, 1, 1, 9, 40, 2, 36, 1 ] +// CHECK-NEXT: } +// CHECK-NEXT: }, { +// CHECK-NEXT: inputs: [ 4, 1 ], +// CHECK-NEXT: outputs: [ 5 ], +// CHECK-NEXT: builtin_options_2_type: StableHLOCompositeOptions, +// CHECK-NEXT: builtin_options_2: { +// CHECK-NEXT: name: "test.TEST_COMPOSITE", +// CHECK-NEXT: decomposition_subgraph_index: 1, +// CHECK-NEXT: composite_attributes: [ 109, 121, 95, 97, 114, 114, 97, 121, 0, 1, 97, 0, 1, 98, 0, 2, 6, 4, 20, 20, 1, 21, 1, 1, 1, 9, 40, 2, 36, 1 ] +// CHECK-NEXT: } +// CHECK-NEXT: }, { +// CHECK-NEXT: opcode_index: 1, +// CHECK-NEXT: inputs: [ 5, 2 ], +// CHECK-NEXT: outputs: [ 6 ], +// CHECK-NEXT: builtin_options_type: AddOptions, +// CHECK-NEXT: builtin_options: { +// CHECK-EMPTY: +// CHECK-NEXT: } +// CHECK-NEXT: }, { +// CHECK-NEXT: opcode_index: 2, +// CHECK-NEXT: inputs: [ 6, 3 ], +// CHECK-NEXT: outputs: [ 7 ], +// CHECK-NEXT: builtin_options_type: SubOptions, +// CHECK-NEXT: builtin_options: { +// CHECK-EMPTY: +// CHECK-NEXT: } +// CHECK-NEXT: } ], +// CHECK-NEXT: name: "main" +// CHECK-NEXT: }, { +// CHECK-NEXT: tensors: [ { +// CHECK-NEXT: shape: [ 10 ], +// CHECK-NEXT: buffer: 9, +// CHECK-NEXT: name: "XlaCallModule_test.TEST_COMPOSITE.impl_0_arg0", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ 10 ], +// CHECK-NEXT: buffer: 10, +// CHECK-NEXT: name: "XlaCallModule_test.TEST_COMPOSITE.impl_0_arg1", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ ], +// CHECK-NEXT: buffer: 11, +// CHECK-NEXT: name: "arith.constant2", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ 10 ], +// CHECK-NEXT: buffer: 12, +// CHECK-NEXT: name: "tfl.add1", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ 10 ], +// CHECK-NEXT: buffer: 13, +// CHECK-NEXT: name: "tfl.add2", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: } ], +// CHECK-NEXT: inputs: [ 0, 1 ], +// CHECK-NEXT: outputs: [ 4 ], +// CHECK-NEXT: operators: [ { +// CHECK-NEXT: opcode_index: 1, +// CHECK-NEXT: inputs: [ 0, 1 ], +// CHECK-NEXT: outputs: [ 3 ], +// CHECK-NEXT: builtin_options_type: AddOptions, +// CHECK-NEXT: builtin_options: { +// CHECK-EMPTY: +// CHECK-NEXT: } +// CHECK-NEXT: }, { +// CHECK-NEXT: opcode_index: 1, +// CHECK-NEXT: inputs: [ 3, 2 ], +// CHECK-NEXT: outputs: [ 4 ], +// CHECK-NEXT: builtin_options_type: AddOptions, +// CHECK-NEXT: builtin_options: { +// CHECK-EMPTY: +// CHECK-NEXT: } +// CHECK-NEXT: } ], +// CHECK-NEXT: name: "XlaCallModule_test.TEST_COMPOSITE.impl_0" +// CHECK-NEXT: }, { +// CHECK-NEXT: tensors: [ { +// CHECK-NEXT: shape: [ 10 ], +// CHECK-NEXT: buffer: 14, +// CHECK-NEXT: name: "XlaCallModule_test.TEST_COMPOSITE.impl_0_0_arg0", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ 10 ], +// CHECK-NEXT: buffer: 15, +// CHECK-NEXT: name: "XlaCallModule_test.TEST_COMPOSITE.impl_0_0_arg1", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ ], +// CHECK-NEXT: buffer: 11, +// CHECK-NEXT: name: "arith.constant3", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ 10 ], +// CHECK-NEXT: buffer: 17, +// CHECK-NEXT: name: "tfl.add3", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ 10 ], +// CHECK-NEXT: buffer: 18, +// CHECK-NEXT: name: "tfl.add4", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: } ], +// CHECK-NEXT: inputs: [ 0, 1 ], +// CHECK-NEXT: outputs: [ 4 ], +// CHECK-NEXT: operators: [ { +// CHECK-NEXT: opcode_index: 1, +// CHECK-NEXT: inputs: [ 0, 1 ], +// CHECK-NEXT: outputs: [ 3 ], +// CHECK-NEXT: builtin_options_type: AddOptions, +// CHECK-NEXT: builtin_options: { +// CHECK-EMPTY: +// CHECK-NEXT: } +// CHECK-NEXT: }, { +// CHECK-NEXT: opcode_index: 1, +// CHECK-NEXT: inputs: [ 3, 2 ], +// CHECK-NEXT: outputs: [ 4 ], +// CHECK-NEXT: builtin_options_type: AddOptions, +// CHECK-NEXT: builtin_options: { +// CHECK-EMPTY: +// CHECK-NEXT: } +// CHECK-NEXT: } ], +// CHECK-NEXT: name: "XlaCallModule_test.TEST_COMPOSITE.impl_0_0" +// CHECK-NEXT: } ], +// CHECK-NEXT: description: "MLIR Converted.", +// CHECK-NEXT: buffers: [ { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-NEXT: data: [ 0, 0, 32, 65 ] +// CHECK-NEXT: }, { +// CHECK-NEXT: data: [ 0, 0, 160, 65 ] +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-NEXT: data: [ 0, 0, 200, 66 ] +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-NEXT: data: [ 50, 46, 49, 55, 46, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] +// CHECK-NEXT: } ], +// CHECK-NEXT: metadata: [ { +// CHECK-NEXT: name: "min_runtime_version", +// CHECK-NEXT: buffer: 19 +// CHECK-NEXT: } ], +// CHECK-NEXT: signature_defs: [ ] +// CHECK-NEXT:} + +func.func @main(%arg0: tensor<10xf32>, %arg1: tensor<10xf32>) -> (tensor<10xf32>) { + %cst = arith.constant dense<1.000000e+01> : tensor + %cst_0 = arith.constant dense<2.000000e+01> : tensor + %0 = "vhlo.composite_v1"(%arg0, %arg1) <{composite_attributes = #vhlo.dict_v1<{#vhlo.string_v1<"my_array"> = #vhlo.array_v1<[#vhlo.string_v1<"a">, #vhlo.string_v1<"b">]>}>, decomposition = #vhlo.string_v1<"XlaCallModule_test.TEST_COMPOSITE.impl_0_0">, name = #vhlo.string_v1<"test.TEST_COMPOSITE">, version = #vhlo.integer_v1<0 : i64>}> : (tensor<10xf32>, tensor<10xf32>) -> tensor<10xf32> + %1 = "vhlo.composite_v1"(%0, %arg1) <{composite_attributes = #vhlo.dict_v1<{#vhlo.string_v1<"my_array"> = #vhlo.array_v1<[#vhlo.string_v1<"a">, #vhlo.string_v1<"b">]>}>, decomposition = #vhlo.string_v1<"XlaCallModule_test.TEST_COMPOSITE.impl_0">, name = #vhlo.string_v1<"test.TEST_COMPOSITE">, version = #vhlo.integer_v1<0 : i64>}> : (tensor<10xf32>, tensor<10xf32>) -> tensor<10xf32> + %2 = tfl.add(%1, %cst) <{fused_activation_function = "NONE"}> : (tensor<10xf32>, tensor) -> tensor<10xf32> + %3 = tfl.sub(%2, %cst_0) <{fused_activation_function = "NONE"}> : (tensor<10xf32>, tensor) -> tensor<10xf32> + return %3 : tensor<10xf32> +} +func.func private @XlaCallModule_test.TEST_COMPOSITE.impl_0(%arg0: tensor<10xf32>, %arg1: tensor<10xf32>) -> tensor<10xf32> { + %cst = arith.constant dense<1.000000e+02> : tensor + %0 = tfl.add %arg0, %arg1 {fused_activation_function = "NONE"} : tensor<10xf32> + %1 = tfl.add(%0, %cst) <{fused_activation_function = "NONE"}> : (tensor<10xf32>, tensor) -> tensor<10xf32> + return %1 : tensor<10xf32> +} +func.func private @XlaCallModule_test.TEST_COMPOSITE.impl_0_0(%arg0: tensor<10xf32>, %arg1: tensor<10xf32>) -> tensor<10xf32> { + %cst = arith.constant dense<1.000000e+02> : tensor + %0 = tfl.add %arg0, %arg1 {fused_activation_function = "NONE"} : tensor<10xf32> + %1 = tfl.add(%0, %cst) <{fused_activation_function = "NONE"}> : (tensor<10xf32>, tensor) -> tensor<10xf32> + return %1 : tensor<10xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/custom_op_with_tflite_op.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/custom_op_with_tflite_op.mlir new file mode 100644 index 0000000000000000000000000000000000000000..018deeb9032162f3aa3ebda6aad98d56835df8db --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/custom_op_with_tflite_op.mlir @@ -0,0 +1,117 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -emit-custom-ops -o - | flatbuffer_to_string - | FileCheck %s + +func.func @main(tensor<4xf32>) -> tensor<4xf32> { +^bb0(%arg0: tensor<4xf32>): + +// CHECK: { +// CHECK-NEXT: version: 3, +// CHECK-NEXT: operator_codes: [ { +// CHECK-NEXT: deprecated_builtin_code: 18, +// CHECK-NEXT: version: 1 +// CHECK-NEXT: builtin_code: MUL +// CHECK-NEXT: }, { +// CHECK-NEXT: deprecated_builtin_code: 32, +// CHECK-NEXT: custom_code: "MyCustomOp", +// CHECK-NEXT: builtin_code: CUSTOM +// CHECK-NEXT: }, { +// CHECK-NEXT: deprecated_builtin_code: 47, +// CHECK-NEXT: version: 1, +// CHECK-NEXT: builtin_code: EXP +// CHECK-NEXT: } ], +// CHECK-NEXT: subgraphs: [ { +// CHECK-NEXT: tensors: [ { +// CHECK-NEXT: shape: [ 4 ], +// CHECK-NEXT: buffer: 1, +// CHECK-NEXT: name: "arg0", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ 4 ], +// CHECK-NEXT: buffer: 2, +// CHECK-NEXT: name: "Const", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ 4 ], +// CHECK-NEXT: buffer: 3, +// CHECK-NEXT: name: "mul", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ 4 ], +// CHECK-NEXT: buffer: 4, +// CHECK-NEXT: name: "MyCustomOp", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ 4 ], +// CHECK-NEXT: buffer: 5, +// CHECK-NEXT: name: "exp", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: } ], +// CHECK-NEXT: inputs: [ 0 ], +// CHECK-NEXT: outputs: [ 4 ], +// CHECK-NEXT: operators: [ { +// CHECK-NEXT: inputs: [ 0, 1 ], +// CHECK-NEXT: outputs: [ 2 ], +// CHECK-NEXT: builtin_options_type: MulOptions, +// CHECK-NEXT: builtin_options: { +// CHECK-EMPTY: +// CHECK-NEXT: } +// CHECK-NEXT: }, { +// CHECK-NEXT: opcode_index: 1, +// CHECK-NEXT: inputs: [ 2, 1 ], +// CHECK-NEXT: outputs: [ 3 ], +// CHECK-NEXT: custom_options: [ 102, 117, 115, 101, 100, 95, 97, 99, 116, 105, 118, 97, 116, 105, 111, 110, 95, 102, 117, 110, 99, 116, 105, 111, 110, 0, 4, 82, 69, 76, 85, 0, 105, 110, 116, 95, 97, 116, 116, 114, 0, 2, 42, 11, 2, 1, 2, 20, 2, 20, 4, 4, 36, 1 ] +// CHECK-NEXT: }, { +// CHECK-NEXT: opcode_index: 2, +// CHECK-NEXT: inputs: [ 3 ], +// CHECK-NEXT: outputs: [ 4 ], +// CHECK-NEXT: builtin_options_type: ExpOptions, +// CHECK-NEXT: builtin_options: { +// CHECK-EMPTY: +// CHECK-NEXT: } +// CHECK-NEXT: } ], +// CHECK-NEXT: name: "main" +// CHECK-NEXT: } ], +// CHECK-NEXT: description: "MLIR Converted.", +// CHECK-NEXT: buffers: [ { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-NEXT: data: [ 0, 0, 128, 63, 0, 0, 128, 63, 0, 0, 128, 63, 0, 0, 128, 63 ] +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-NEXT: data: [ 49, 46, 55, 46, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] +// CHECK-NEXT: } ], +// CHECK-NEXT: metadata: [ { +// CHECK-NEXT: name: "min_runtime_version", +// CHECK-NEXT: buffer: 6 +// CHECK-NEXT: } ] +// CHECK-NEXT: signature_defs: [ ] +// CHECK-NEXT:} + + %0 = "tfl.pseudo_const" () {value = dense<1.0> : tensor<4xf32>} : () -> tensor<4xf32> loc("Const") + %1 = "tfl.mul"(%arg0, %0) {fused_activation_function = "NONE"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> loc("mul") + // tf.MyCustomOp is the result of conversion to a Custom op + %2 = "tf.MyCustomOp"(%1, %0) {fused_activation_function = "RELU", int_attr = 2 : i32} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> loc("MyCustomOp") + %3 = "tfl.exp"(%2) : (tensor<4xf32>) -> tensor<4xf32> loc("exp") + func.return %3 : tensor<4xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/custom_tensorlist_reserve.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/custom_tensorlist_reserve.mlir new file mode 100644 index 0000000000000000000000000000000000000000..b23b07663923aad8c2c223be059ae324a1cdceb5 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/custom_tensorlist_reserve.mlir @@ -0,0 +1,23 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s | flatbuffer_to_string - | FileCheck %s + +func.func @main(%arg0: tensor, %arg1: tensor) -> tensor>> { + %0 = "tfl.custom"(%arg0, %arg1) {custom_code = "TensorListReserve", custom_option = #tfl} : (tensor, tensor) -> tensor>> + func.return %0 : tensor>> +} + +// CHECK: operator_codes: [ { +// CHECK: custom_code: "TensorListReserve", +// CHECK: builtin_code: CUSTOM + +// CHECK: shape: [ ], +// CHECK: type: VARIANT, +// CHECK: name: "tfl.custom", + +// CHECK: variant_tensors: [ { +// CHECK: shape: [ ], +// CHECK: type: INT32 + +// CHECK: operators: [ { +// CHECK: inputs: [ 0, 1 ], +// CHECK: outputs: [ 2 ], +// CHECK: custom_options: [ 2 ] diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/debug_metadata.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/debug_metadata.mlir new file mode 100644 index 0000000000000000000000000000000000000000..07c468849b23e5cca2228f300d2eee392796d3f7 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/debug_metadata.mlir @@ -0,0 +1,44 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer --serialize-debug-metadata=true %s -o - | flatbuffer_to_string - | FileCheck %s + +#loc = loc("":0:0) +#loc5 = loc("main"(#loc)) +#loc8 = loc("cond_true"(#loc)) +#loc10 = loc("cond_false"(#loc)) +module @jit_relu attributes {jax.uses_shape_polymorphism = false, mhlo.num_partitions = 1 : i32, mhlo.num_replicas = 1 : i32, tfl._legalize_tfl_variables = true} { + func.func @main(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>) -> tensor<1xf32> { + %0 = tfl.less(%arg0, %arg1) : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xi1> loc(#loc6) + %1 = "tf.If"(%0, %arg0, %arg1) <{else_branch = @cond_false, is_stateless = false, then_branch = @cond_true}> : (tensor<1xi1>, tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32> loc(#loc7) + func.return %1 : tensor<1xf32> loc(#loc) + } loc(#loc5) + func.func @cond_true(%arg0: tensor<*xf32>, %arg1: tensor<*xf32>) -> tensor<*xf32> { + %0 = tfl.add %arg0, %arg1 {fused_activation_function = "NONE"} : tensor<*xf32> loc(#loc16) + func.return %0 : tensor<*xf32> loc(#loc) + } loc(#loc8) + func.func @cond_false(%arg0: tensor<*xf32>, %arg1: tensor<*xf32>) -> tensor<*xf32> { + %0 = tfl.mul %arg0, %arg1 {fused_activation_function = "NONE"} : tensor<*xf32> loc(#loc15) + func.return %0 : tensor<*xf32> loc(#loc) + } loc(#loc10) +} loc(#loc) +#loc1 = loc("tfl.less") +#loc2 = loc("tf.If") +#loc3 = loc("":1:4) +#loc4 = loc("third_party/py/IPython/v3_2_3/core/interactiveshell.py":3066:16) +#loc6 = loc(fused<"tflite.importer_wrapper">[#loc1]) +#loc7 = loc(fused<"tflite.importer_wrapper">[#loc2]) +#loc9 = loc(callsite(#loc3 at #loc4)) +#loc11 = loc(fused<"">[#loc3, #loc4]) +#loc12 = loc("jit(relu)/jit(main)/max"(#loc9)) +#loc13 = loc("tfl.mul"(#loc11)) +#loc14 = loc("jit(relu)/jit(main)/max"(#loc12)) +#loc15 = loc(fused<"tflite.importer_wrapper">[#loc13]) +#loc16 = loc(fused<"tflite.importer_wrapper">[#loc14]) + +// CHECK: operators: [ { +// CHECK: name: "main", +// CHECK: debug_metadata_index: +// CHECK: name: "cond_true", +// CHECK: debug_metadata_index: +// CHECK: name: "cond_false", +// CHECK: debug_metadata_index: +// CHECK: metadata: [ { +// CHECK: name: "debug_metadata", \ No newline at end of file diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/deduplicate_const.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/deduplicate_const.mlir new file mode 100644 index 0000000000000000000000000000000000000000..0238d3c0442684e8003733c43ae97d804afbd7d3 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/deduplicate_const.mlir @@ -0,0 +1,191 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_to_string - | FileCheck %s --check-prefix=CHECK +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer -disable-buffer-deduping %s -o - | flatbuffer_to_string - | FileCheck %s --check-prefix=NO_DEDUPE + +module { +func.func @add(%arg0: tensor<3x2xf32>) -> tensor<3x2xf32> attributes {tf.entry_function = {inputs = "serving_default_x", outputs = "outputs"}} { + %0 = "tfl.pseudo_const" () {value = dense<[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]> : tensor<3x2xf32>} : () -> tensor<3x2xf32> + %1 = "tfl.add" (%0, %arg0) {fused_activation_function = "NONE"} : (tensor<3x2xf32>, tensor<3x2xf32>) -> tensor<3x2xf32> + func.return %1 : tensor<3x2xf32> +} + +func.func @sub(%arg0: tensor<3x2xf32>) -> tensor<3x2xf32> attributes {tf.entry_function = {inputs = "serving_default_x", outputs = "outputs"}} { + %0 = "tfl.pseudo_const" () {value = dense<[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]> : tensor<3x2xf32>} : () -> tensor<3x2xf32> + %1 = "tfl.sub" (%0, %arg0) {fused_activation_function = "NONE"} : (tensor<3x2xf32>, tensor<3x2xf32>) -> tensor<3x2xf32> + func.return %1 : tensor<3x2xf32> +} +} + +// CHECK: { +// CHECK: subgraphs: [ { +// CHECK-NEXT: tensors: [ { +// CHECK-NEXT: shape: [ 3, 2 ], +// CHECK-NEXT: buffer: 1, +// CHECK-NEXT: name: "serving_default_x", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ 3, 2 ], +// CHECK-NEXT: buffer: 2, +// CHECK-NEXT: name: "tfl.pseudo_const", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ 3, 2 ], +// CHECK-NEXT: buffer: 3, +// CHECK-NEXT: name: "outputs", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: } ], +// CHECK: name: "add" +// CHECK-NEXT: }, { +// CHECK-NEXT: tensors: [ { +// CHECK-NEXT: shape: [ 3, 2 ], +// CHECK-NEXT: buffer: 4, +// CHECK-NEXT: name: "serving_default_x", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ 3, 2 ], +// CHECK-NEXT: buffer: 2, +// CHECK-NEXT: name: "tfl.pseudo_const1", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: }, { +// CHECK-NEXT: shape: [ 3, 2 ], +// CHECK-NEXT: buffer: 6, +// CHECK-NEXT: name: "outputs", +// CHECK-NEXT: quantization: { +// CHECK-EMPTY: +// CHECK-NEXT: }, +// CHECK-NEXT: has_rank: true +// CHECK-NEXT: } ], +// CHECK-NEXT: inputs: [ 0 ], +// CHECK-NEXT: outputs: [ 2 ], +// CHECK: name: "sub" +// CHECK-NEXT: } ], +// CHECK-NEXT: description: "MLIR Converted.", +// CHECK-NEXT: buffers: [ { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-NEXT: data: [ 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, 128, 64, 0, 0, 160, 64, 0, 0, 192, 64 ] +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-EMPTY: +// CHECK-NEXT: }, { +// CHECK-NEXT: data: [ 49, 46, 54, 46, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] +// CHECK-NEXT: } ], +// CHECK: } + +// NO_DEDUPE: { +// NO_DEDUPE: version: 3, +// NO_DEDUPE: operator_codes: [ { +// NO_DEDUPE: version: 1 +// NO_DEDUPE: }, { +// NO_DEDUPE: deprecated_builtin_code: 41, +// NO_DEDUPE: version: 1, +// NO_DEDUPE: builtin_code: SUB +// NO_DEDUPE: } ], +// NO_DEDUPE: subgraphs: [ { +// NO_DEDUPE: tensors: [ { +// NO_DEDUPE: shape: [ 3, 2 ], +// NO_DEDUPE: buffer: 1, +// NO_DEDUPE: name: "serving_default_x", +// NO_DEDUPE: quantization: { +// NO_DEDUPE: }, +// NO_DEDUPE: has_rank: true +// NO_DEDUPE: }, { +// NO_DEDUPE: shape: [ 3, 2 ], +// NO_DEDUPE: buffer: 2, +// NO_DEDUPE: name: "tfl.pseudo_const", +// NO_DEDUPE: quantization: { +// NO_DEDUPE: }, +// NO_DEDUPE: has_rank: true +// NO_DEDUPE: }, { +// NO_DEDUPE: shape: [ 3, 2 ], +// NO_DEDUPE: buffer: 3, +// NO_DEDUPE: name: "outputs", +// NO_DEDUPE: quantization: { +// NO_DEDUPE: }, +// NO_DEDUPE: has_rank: true +// NO_DEDUPE: } ], +// NO_DEDUPE: inputs: [ 0 ], +// NO_DEDUPE: outputs: [ 2 ], +// NO_DEDUPE: operators: [ { +// NO_DEDUPE: inputs: [ 1, 0 ], +// NO_DEDUPE: outputs: [ 2 ], +// NO_DEDUPE: builtin_options_type: AddOptions, +// NO_DEDUPE: builtin_options: { +// NO_DEDUPE: } +// NO_DEDUPE: } ], +// NO_DEDUPE: name: "add" +// NO_DEDUPE: }, { +// NO_DEDUPE: tensors: [ { +// NO_DEDUPE: shape: [ 3, 2 ], +// NO_DEDUPE: buffer: 4, +// NO_DEDUPE: name: "serving_default_x", +// NO_DEDUPE: quantization: { +// NO_DEDUPE: }, +// NO_DEDUPE: has_rank: true +// NO_DEDUPE: }, { +// NO_DEDUPE: shape: [ 3, 2 ], +// NO_DEDUPE: buffer: 5, +// NO_DEDUPE: name: "tfl.pseudo_const1", +// NO_DEDUPE: quantization: { +// NO_DEDUPE: }, +// NO_DEDUPE: has_rank: true +// NO_DEDUPE: }, { +// NO_DEDUPE: shape: [ 3, 2 ], +// NO_DEDUPE: buffer: 6, +// NO_DEDUPE: name: "outputs", +// NO_DEDUPE: quantization: { +// NO_DEDUPE: }, +// NO_DEDUPE: has_rank: true +// NO_DEDUPE: } ], +// NO_DEDUPE: inputs: [ 0 ], +// NO_DEDUPE: outputs: [ 2 ], +// NO_DEDUPE: operators: [ { +// NO_DEDUPE: opcode_index: 1, +// NO_DEDUPE: inputs: [ 1, 0 ], +// NO_DEDUPE: outputs: [ 2 ], +// NO_DEDUPE: builtin_options_type: SubOptions, +// NO_DEDUPE: builtin_options: { +// NO_DEDUPE: } +// NO_DEDUPE: } ], +// NO_DEDUPE: name: "sub" +// NO_DEDUPE: } ], +// NO_DEDUPE: description: "MLIR Converted.", +// NO_DEDUPE: buffers: [ { +// NO_DEDUPE: }, { +// NO_DEDUPE: }, { +// NO_DEDUPE: data: [ 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, 128, 64, 0, 0, 160, 64, 0, 0, 192, 64 ] +// NO_DEDUPE: }, { +// NO_DEDUPE: }, { +// NO_DEDUPE: }, { +// NO_DEDUPE: data: [ 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, 128, 64, 0, 0, 160, 64, 0, 0, 192, 64 ] +// NO_DEDUPE: }, { +// NO_DEDUPE: }, { +// NO_DEDUPE: data: [ 49, 46, 54, 46, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] +// NO_DEDUPE: } ], +// NO_DEDUPE: metadata: [ { +// NO_DEDUPE: name: "min_runtime_version", +// NO_DEDUPE: buffer: 7 +// NO_DEDUPE: } ], +// NO_DEDUPE: signature_defs: [ ] +// NO_DEDUPE: } diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/depthwise_conv2d.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/depthwise_conv2d.mlir new file mode 100644 index 0000000000000000000000000000000000000000..897cc68d62bf926f8398c6250aef720967cb4eba --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/depthwise_conv2d.mlir @@ -0,0 +1,108 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_to_string - | FileCheck %s + +func.func @main(tensor<1x224x224x3xf32>) -> tensor<1x112x112x32xf32> { +^bb0(%arg0: tensor<1x224x224x3xf32>): + // CHECK: { + // CHECK-NEXT: version: 3, + // CHECK-NEXT: operator_codes: [ { + // CHECK-NEXT: deprecated_builtin_code: 6, + // CHECK-NEXT: version: 1 + // CHECK-NEXT: builtin_code: DEQUANTIZE + // CHECK-NEXT: }, { + // CHECK-NEXT: deprecated_builtin_code: 4, + // CHECK-NEXT: version: 1 + // CHECK-NEXT: builtin_code: DEPTHWISE_CONV_2D + // CHECK-NEXT: } ], + // CHECK-NEXT: subgraphs: [ { + // CHECK-NEXT: tensors: [ { + // CHECK-NEXT: shape: [ 1, 224, 224, 3 ], + // CHECK-NEXT: buffer: 1, + // CHECK-NEXT: name: "arg0", + // CHECK-NEXT: quantization: { + // CHECK-EMPTY: + // CHECK-NEXT: }, + // CHECK-NEXT: has_rank: true + // CHECK-NEXT: }, { + // CHECK-NEXT: shape: [ 32 ], + // CHECK-NEXT: buffer: 2, + // CHECK-NEXT: name: "Const", + // CHECK-NEXT: quantization: { + // CHECK-EMPTY: + // CHECK-NEXT: }, + // CHECK-NEXT: has_rank: true + // CHECK-NEXT: }, { + // CHECK-NEXT: shape: [ 32, 3, 3, 3 ], + // CHECK-NEXT: type: UINT8, + // CHECK-NEXT: buffer: 3, + // CHECK-NEXT: name: "tfl.pseudo_qconst", + // CHECK-NEXT: quantization: { + // CHECK-NEXT: scale: [ 0.021827 ], + // CHECK-NEXT: zero_point: [ 151 ] + // CHECK-NEXT: }, + // CHECK-NEXT: has_rank: true + // CHECK-NEXT: }, { + // CHECK-NEXT: shape: [ 32, 3, 3, 3 ], + // CHECK-NEXT: buffer: 4, + // CHECK-NEXT: name: "tfl.dequantize", + // CHECK-NEXT: quantization: { + // CHECK-EMPTY: + // CHECK-NEXT: }, + // CHECK-NEXT: has_rank: true + // CHECK-NEXT: }, { + // CHECK-NEXT: shape: [ 1, 112, 112, 32 ], + // CHECK-NEXT: buffer: 5, + // CHECK-NEXT: name: "tfl.depthwise_conv_2d", + // CHECK-NEXT: quantization: { + // CHECK-EMPTY: + // CHECK-NEXT: }, + // CHECK-NEXT: has_rank: true + // CHECK-NEXT: } ], + // CHECK-NEXT: inputs: [ 0 ], + // CHECK-NEXT: outputs: [ 4 ], + // CHECK-NEXT: operators: [ { + // CHECK-NEXT: inputs: [ 2 ], + // CHECK-NEXT: outputs: [ 3 ] + // CHECK-NEXT: }, { + // CHECK-NEXT: opcode_index: 1, + // CHECK-NEXT: inputs: [ 0, 3, 1 ], + // CHECK-NEXT: outputs: [ 4 ], + // CHECK-NEXT: builtin_options_type: DepthwiseConv2DOptions, + // CHECK-NEXT: builtin_options: { + // CHECK-NEXT: padding: VALID, + // CHECK-NEXT: stride_w: 5, + // CHECK-NEXT: stride_h: 4, + // CHECK-NEXT: depth_multiplier: 4 + // CHECK-NEXT: } + // CHECK-NEXT: } ], + // CHECK-NEXT: name: "main" + // CHECK-NEXT: } ], + // CHECK-NEXT: description: "MLIR Converted.", + // CHECK-NEXT: buffers: [ { + // CHECK-EMPTY: + // CHECK-NEXT: }, { + // CHECK-EMPTY: + // CHECK-NEXT: }, { + // CHECK-NEXT: data: [ 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191 ] + // CHECK-NEXT: }, { + // CHECK-NEXT: data: [ 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180 ] + // CHECK-NEXT: }, { + // CHECK-EMPTY: + // CHECK-NEXT: }, { + // CHECK-EMPTY: + // CHECK-NEXT: }, { + // CHECK-NEXT: data: [ 49, 46, 49, 51, 46, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] + // CHECK-NEXT: } ], + // CHECK-NEXT: metadata: [ { + // CHECK-NEXT: name: "min_runtime_version", + // CHECK-NEXT: buffer: 6 + // CHECK-NEXT: } ] + // CHECK-NEXT: signature_defs: [ ] + // CHECK-NEXT:} + + %0 = "tfl.pseudo_const" () {value = dense<-1.23697901> : tensor<32xf32>} : () -> tensor<32xf32> loc("Const") + %1 = "tfl.pseudo_qconst"() {qtype = tensor<32x3x3x3x!quant.uniform:f32, 0.021826678373682216:151>>, value = dense<-76> : tensor<32x3x3x3xi8>} : () -> tensor<32x3x3x3x!quant.uniform:f32, 0.021826678373682216:151>> + %2 = "tfl.dequantize"(%1) : (tensor<32x3x3x3x!quant.uniform:f32, 0.021826678373682216:151>>) -> tensor<32x3x3x3xf32> + %3 = "tfl.depthwise_conv_2d"(%arg0, %2, %0) {depth_multiplier = 4 : i32, dilation_h_factor = 1 : i32, dilation_w_factor = 1 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 4 : i32, stride_w = 5 : i32} : (tensor<1x224x224x3xf32>, tensor<32x3x3x3xf32>, tensor<32xf32>) -> tensor<1x112x112x32xf32> + + func.return %3 : tensor<1x112x112x32xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/depthwise_conv2d_v2.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/depthwise_conv2d_v2.mlir new file mode 100644 index 0000000000000000000000000000000000000000..b5cd2516a5495f0f7df14a424873c556bb88df17 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/depthwise_conv2d_v2.mlir @@ -0,0 +1,110 @@ +// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_to_string - | FileCheck %s + +func.func @main(tensor<1x224x224x3xf32>) -> tensor<1x112x112x32xf32> { +^bb0(%arg0: tensor<1x224x224x3xf32>): + // CHECK: { + // CHECK-NEXT: version: 3, + // CHECK-NEXT: operator_codes: [ { + // CHECK-NEXT: deprecated_builtin_code: 6, + // CHECK-NEXT: version: 1, + // CHECK-NEXT: builtin_code: DEQUANTIZE + // CHECK-NEXT: }, { + // CHECK-NEXT: deprecated_builtin_code: 4, + // CHECK-NEXT: version: 2, + // CHECK-NEXT: builtin_code: DEPTHWISE_CONV_2D + // CHECK-NEXT: } ], + // CHECK-NEXT: subgraphs: [ { + // CHECK-NEXT: tensors: [ { + // CHECK-NEXT: shape: [ 1, 224, 224, 3 ], + // CHECK-NEXT: buffer: 1, + // CHECK-NEXT: name: "arg0", + // CHECK-NEXT: quantization: { + // CHECK-EMPTY: + // CHECK-NEXT: }, + // CHECK-NEXT: has_rank: true + // CHECK-NEXT: }, { + // CHECK-NEXT: shape: [ 32 ], + // CHECK-NEXT: buffer: 2, + // CHECK-NEXT: name: "Const", + // CHECK-NEXT: quantization: { + // CHECK-EMPTY: + // CHECK-NEXT: }, + // CHECK-NEXT: has_rank: true + // CHECK-NEXT: }, { + // CHECK-NEXT: shape: [ 32, 3, 3, 3 ], + // CHECK-NEXT: type: UINT8, + // CHECK-NEXT: buffer: 3, + // CHECK-NEXT: name: "tfl.pseudo_qconst", + // CHECK-NEXT: quantization: { + // CHECK-NEXT: scale: [ 0.021827 ], + // CHECK-NEXT: zero_point: [ 151 ] + // CHECK-NEXT: }, + // CHECK-NEXT: has_rank: true + // CHECK-NEXT: }, { + // CHECK-NEXT: shape: [ 32, 3, 3, 3 ], + // CHECK-NEXT: buffer: 4, + // CHECK-NEXT: name: "tfl.dequantize", + // CHECK-NEXT: quantization: { + // CHECK-EMPTY: + // CHECK-NEXT: }, + // CHECK-NEXT: has_rank: true + // CHECK-NEXT: }, { + // CHECK-NEXT: shape: [ 1, 112, 112, 32 ], + // CHECK-NEXT: buffer: 5, + // CHECK-NEXT: name: "tfl.depthwise_conv_2d", + // CHECK-NEXT: quantization: { + // CHECK-EMPTY: + // CHECK-NEXT: }, + // CHECK-NEXT: has_rank: true + // CHECK-NEXT: } ], + // CHECK-NEXT: inputs: [ 0 ], + // CHECK-NEXT: outputs: [ 4 ], + // CHECK-NEXT: operators: [ { + // CHECK-NEXT: inputs: [ 2 ], + // CHECK-NEXT: outputs: [ 3 ] + // CHECK-NEXT: }, { + // CHECK-NEXT: opcode_index: 1, + // CHECK-NEXT: inputs: [ 0, 3, 1 ], + // CHECK-NEXT: outputs: [ 4 ], + // CHECK-NEXT: builtin_options_type: DepthwiseConv2DOptions, + // CHECK-NEXT: builtin_options: { + // CHECK-NEXT: padding: VALID, + // CHECK-NEXT: stride_w: 5, + // CHECK-NEXT: stride_h: 4, + // CHECK-NEXT: depth_multiplier: 4, + // CHECK-NEXT: dilation_w_factor: 2, + // CHECK-NEXT: dilation_h_factor: 2 + // CHECK-NEXT: } + // CHECK-NEXT: } ], + // CHECK-NEXT: name: "main" + // CHECK-NEXT: } ], + // CHECK-NEXT: description: "MLIR Converted.", + // CHECK-NEXT: buffers: [ { + // CHECK-EMPTY: + // CHECK-NEXT: }, { + // CHECK-EMPTY: + // CHECK-NEXT: }, { + // CHECK-NEXT: data: [ 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191, 84, 85, 158, 191 ] + // CHECK-NEXT: }, { + // CHECK-NEXT: data: [ 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180 ] + // CHECK-NEXT: }, { + // CHECK-EMPTY: + // CHECK-NEXT: }, { + // CHECK-EMPTY: + // CHECK-NEXT: }, { + // CHECK-NEXT: data: [ 49, 46, 49, 51, 46, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] + // CHECK-NEXT: } ], + // CHECK-NEXT: metadata: [ { + // CHECK-NEXT: name: "min_runtime_version", + // CHECK-NEXT: buffer: 6 + // CHECK-NEXT: } ] + // CHECK-NEXT: signature_defs: [ ] + // CHECK-NEXT:} + + %0 = "tfl.pseudo_const" () {value = dense<-1.23697901> : tensor<32xf32>} : () -> tensor<32xf32> loc("Const") + %1 = "tfl.pseudo_qconst"() {qtype = tensor<32x3x3x3x!quant.uniform:f32, 0.021826678373682216:151>>, value = dense<-76> : tensor<32x3x3x3xi8>} : () -> tensor<32x3x3x3x!quant.uniform:f32, 0.021826678373682216:151>> + %2 = "tfl.dequantize"(%1) : (tensor<32x3x3x3x!quant.uniform:f32, 0.021826678373682216:151>>) -> tensor<32x3x3x3xf32> + %3 = "tfl.depthwise_conv_2d"(%arg0, %2, %0) {depth_multiplier = 4 : i32, dilation_h_factor = 2 : i32, dilation_w_factor = 2 : i32, fused_activation_function = "NONE", padding = "VALID", stride_h = 4 : i32, stride_w = 5 : i32} : (tensor<1x224x224x3xf32>, tensor<32x3x3x3xf32>, tensor<32xf32>) -> tensor<1x112x112x32xf32> + + func.return %3 : tensor<1x112x112x32xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/disable_builtin.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/disable_builtin.mlir new file mode 100644 index 0000000000000000000000000000000000000000..aee227c69cbca902b498afdf765b9beecba540b2 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/disable_builtin.mlir @@ -0,0 +1,10 @@ +// RUN: not flatbuffer_translate -mlir-to-tflite-flatbuffer -emit-builtin-tflite-ops=false %s 2>&1 | FileCheck %s + +// CHECK: 'tfl.add' op is a TFLite builtin op but builtin emission is not enabled + +func.func @main(tensor<3x2xi32>) -> tensor<3x2xi32> { +^bb0(%arg0: tensor<3x2xi32>): + %0 = "arith.constant"() {name = "Const2", value = dense<10> : tensor} : () -> tensor + %1 = "tfl.add"(%0, %arg0) {fused_activation_function = "NONE", name = "add"} : (tensor, tensor<3x2xi32>) -> tensor<3x2xi32> + func.return %1 : tensor<3x2xi32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/disable_custom.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/disable_custom.mlir new file mode 100644 index 0000000000000000000000000000000000000000..04e2eabe3b496a2182271f885a1dc664605eef37 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/disable_custom.mlir @@ -0,0 +1,15 @@ +// RUN: not flatbuffer_translate -mlir-to-tflite-flatbuffer %s 2>&1 | FileCheck %s + +// CHECK: error: 'tf.MyCustomOp' op is neither a custom op nor a flex op +// CHECK: error: failed while converting: 'main' +// CHECK: Some ops in the model are custom ops, See instructions to implement +// CHECK: tf.MyCustomOp(tensor<4xf32>, tensor<4xf32>) -> (tensor<4xf32>, tensor<3xf32>) : {name = "MyCustomOp"} + +func.func @main(tensor<4xf32>) -> tensor<4xf32> { +^bb0(%arg0: tensor<4xf32>): + %0 = "tfl.pseudo_const" () {name = "Const", value = dense<1.0> : tensor<4xf32>} : () -> tensor<4xf32> + %1 = "tfl.mul"(%arg0, %0) {fused_activation_function = "NONE", name = "mul"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> + %2:2 = "tf.MyCustomOp"(%1, %0) {name = "MyCustomOp"} : (tensor<4xf32>, tensor<4xf32>) -> (tensor<4xf32>, tensor<3xf32>) + %3 = "tfl.exp"(%2#0) {name = "exp"} : (tensor<4xf32>) -> tensor<4xf32> + func.return %3 : tensor<4xf32> +} diff --git a/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/disable_flex.mlir b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/disable_flex.mlir new file mode 100644 index 0000000000000000000000000000000000000000..841e325f5e54196799561075373b982a38b247a1 --- /dev/null +++ b/western_ai/usa/tensorflow/tensorflow/compiler/mlir/lite/tests/mlir2flatbuffer/disable_flex.mlir @@ -0,0 +1,16 @@ +// RUN: not flatbuffer_translate -mlir-to-tflite-flatbuffer %s 2>&1 | FileCheck %s + +// CHECK: error: 'tf.Div' op is neither a custom op nor a flex op +// CHECK: error: failed while converting: 'main' +// CHECK: Some ops are not supported by the native TFLite runtime +// CHECK: tf.Div(tensor<4xf32>, tensor<4xf32>) -> (tensor<4xf32>) : {name = "div"} + +func.func @main(tensor<4xf32>) -> tensor<4xf32> { +^bb0(%arg0: tensor<4xf32>): + %0 = "tfl.pseudo_const" () {name = "Const", value = dense<1.0> : tensor<4xf32>} : () -> tensor<4xf32> + %1 = "tfl.mul"(%arg0, %0) {fused_activation_function = "NONE", name = "mul"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> + // tf.div is the result of conversion to a Flex TF op + %2 = "tf.Div"(%1, %0) {name = "div"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> + %3 = "tfl.exp"(%2) {name = "exp"} : (tensor<4xf32>) -> tensor<4xf32> + func.return %3 : tensor<4xf32> +}